testsuite: Update scanning symbol sections to support AIX.
[official-gcc.git] / gcc / gimple-range-cache.cc
blobb01563c83f9dc340d2605d7fb3b2d49d9912fa9b
1 /* Gimple ranger SSA cache implementation.
2 Copyright (C) 2017-2020 Free Software Foundation, Inc.
3 Contributed by Andrew MacLeod <amacleod@redhat.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "backend.h"
25 #include "insn-codes.h"
26 #include "tree.h"
27 #include "gimple.h"
28 #include "ssa.h"
29 #include "gimple-pretty-print.h"
30 #include "gimple-range.h"
32 // During contructor, allocate the vector of ssa_names.
34 non_null_ref::non_null_ref ()
36 m_nn.create (0);
37 m_nn.safe_grow_cleared (num_ssa_names);
38 bitmap_obstack_initialize (&m_bitmaps);
41 // Free any bitmaps which were allocated,a swell as the vector itself.
43 non_null_ref::~non_null_ref ()
45 bitmap_obstack_release (&m_bitmaps);
46 m_nn.release ();
49 // Return true if NAME has a non-null dereference in block bb. If this is the
50 // first query for NAME, calculate the summary first.
52 bool
53 non_null_ref::non_null_deref_p (tree name, basic_block bb)
55 if (!POINTER_TYPE_P (TREE_TYPE (name)))
56 return false;
58 unsigned v = SSA_NAME_VERSION (name);
59 if (!m_nn[v])
60 process_name (name);
62 return bitmap_bit_p (m_nn[v], bb->index);
65 // Allocate an populate the bitmap for NAME. An ON bit for a block
66 // index indicates there is a non-null reference in that block. In
67 // order to populate the bitmap, a quick run of all the immediate uses
68 // are made and the statement checked to see if a non-null dereference
69 // is made on that statement.
71 void
72 non_null_ref::process_name (tree name)
74 unsigned v = SSA_NAME_VERSION (name);
75 use_operand_p use_p;
76 imm_use_iterator iter;
77 bitmap b;
79 // Only tracked for pointers.
80 if (!POINTER_TYPE_P (TREE_TYPE (name)))
81 return;
83 // Already processed if a bitmap has been allocated.
84 if (m_nn[v])
85 return;
87 b = BITMAP_ALLOC (&m_bitmaps);
89 // Loop over each immediate use and see if it implies a non-null value.
90 FOR_EACH_IMM_USE_FAST (use_p, iter, name)
92 gimple *s = USE_STMT (use_p);
93 unsigned index = gimple_bb (s)->index;
95 // If bit is already set for this block, dont bother looking again.
96 if (bitmap_bit_p (b, index))
97 continue;
99 // If we can infer a nonnull range, then set the bit for this BB
100 if (!SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name)
101 && infer_nonnull_range (s, name))
102 bitmap_set_bit (b, index);
105 m_nn[v] = b;
108 // -------------------------------------------------------------------------
110 // This class implements a cache of ranges indexed by basic block. It
111 // represents all that is known about an SSA_NAME on entry to each
112 // block. It caches a range-for-type varying range so it doesn't need
113 // to be reformed all the time. If a range is ever always associated
114 // with a type, we can use that instead. Whenever varying is being
115 // set for a block, the cache simply points to this cached one rather
116 // than create a new one each time.
118 class ssa_block_ranges
120 public:
121 ssa_block_ranges (tree t, irange_allocator *allocator);
122 ~ssa_block_ranges ();
124 void set_bb_range (const basic_block bb, const irange &r);
125 void set_bb_varying (const basic_block bb);
126 bool get_bb_range (irange &r, const basic_block bb);
127 bool bb_range_p (const basic_block bb);
129 void dump(FILE *f);
130 private:
131 vec<irange *> m_tab;
132 irange *m_type_range;
133 tree m_type;
134 irange_allocator *m_irange_allocator;
138 // Initialize a block cache for an ssa_name of type T.
140 ssa_block_ranges::ssa_block_ranges (tree t, irange_allocator *allocator)
142 gcc_checking_assert (TYPE_P (t));
143 m_type = t;
144 m_irange_allocator = allocator;
146 m_tab.create (0);
147 m_tab.safe_grow_cleared (last_basic_block_for_fn (cfun));
149 // Create the cached type range.
150 m_type_range = m_irange_allocator->allocate (2);
151 m_type_range->set_varying (t);
153 m_tab[ENTRY_BLOCK_PTR_FOR_FN (cfun)->index] = m_type_range;
156 // Destruct block range.
158 ssa_block_ranges::~ssa_block_ranges ()
160 m_tab.release ();
163 // Set the range for block BB to be R.
165 void
166 ssa_block_ranges::set_bb_range (const basic_block bb, const irange &r)
168 gcc_checking_assert ((unsigned) bb->index < m_tab.length ());
169 irange *m = m_irange_allocator->allocate (r);
170 m_tab[bb->index] = m;
173 // Set the range for block BB to the range for the type.
175 void
176 ssa_block_ranges::set_bb_varying (const basic_block bb)
178 gcc_checking_assert ((unsigned) bb->index < m_tab.length ());
179 m_tab[bb->index] = m_type_range;
182 // Return the range associated with block BB in R. Return false if
183 // there is no range.
185 bool
186 ssa_block_ranges::get_bb_range (irange &r, const basic_block bb)
188 gcc_checking_assert ((unsigned) bb->index < m_tab.length ());
189 irange *m = m_tab[bb->index];
190 if (m)
192 r = *m;
193 return true;
195 return false;
198 // Return true if a range is present.
200 bool
201 ssa_block_ranges::bb_range_p (const basic_block bb)
203 gcc_checking_assert ((unsigned) bb->index < m_tab.length ());
204 return m_tab[bb->index] != NULL;
208 // Print the list of known ranges for file F in a nice format.
210 void
211 ssa_block_ranges::dump (FILE *f)
213 basic_block bb;
214 int_range_max r;
216 FOR_EACH_BB_FN (bb, cfun)
217 if (get_bb_range (r, bb))
219 fprintf (f, "BB%d -> ", bb->index);
220 r.dump (f);
221 fprintf (f, "\n");
225 // -------------------------------------------------------------------------
227 // Initialize the block cache.
229 block_range_cache::block_range_cache ()
231 m_ssa_ranges.create (0);
232 m_ssa_ranges.safe_grow_cleared (num_ssa_names);
233 m_irange_allocator = new irange_allocator;
236 // Remove any m_block_caches which have been created.
238 block_range_cache::~block_range_cache ()
240 unsigned x;
241 for (x = 0; x < m_ssa_ranges.length (); ++x)
243 if (m_ssa_ranges[x])
244 delete m_ssa_ranges[x];
246 delete m_irange_allocator;
247 // Release the vector itself.
248 m_ssa_ranges.release ();
251 // Return a reference to the ssa_block_cache for NAME. If it has not been
252 // accessed yet, allocate it first.
254 ssa_block_ranges &
255 block_range_cache::get_block_ranges (tree name)
257 unsigned v = SSA_NAME_VERSION (name);
258 if (v >= m_ssa_ranges.length ())
259 m_ssa_ranges.safe_grow_cleared (num_ssa_names + 1);
261 if (!m_ssa_ranges[v])
262 m_ssa_ranges[v] = new ssa_block_ranges (TREE_TYPE (name),
263 m_irange_allocator);
264 return *(m_ssa_ranges[v]);
268 // Return a pointer to the ssa_block_cache for NAME. If it has not been
269 // accessed yet, return NULL.
271 ssa_block_ranges *
272 block_range_cache::query_block_ranges (tree name)
274 unsigned v = SSA_NAME_VERSION (name);
275 if (v >= m_ssa_ranges.length () || !m_ssa_ranges[v])
276 return NULL;
277 return m_ssa_ranges[v];
280 // Set the range for NAME on entry to block BB to R.
282 void
283 block_range_cache::set_bb_range (tree name, const basic_block bb,
284 const irange &r)
286 return get_block_ranges (name).set_bb_range (bb, r);
289 // Set the range for NAME on entry to block BB to varying.
291 void
292 block_range_cache::set_bb_varying (tree name, const basic_block bb)
294 return get_block_ranges (name).set_bb_varying (bb);
297 // Return the range for NAME on entry to BB in R. Return true if there
298 // is one.
300 bool
301 block_range_cache::get_bb_range (irange &r, tree name, const basic_block bb)
303 ssa_block_ranges *ptr = query_block_ranges (name);
304 if (ptr)
305 return ptr->get_bb_range (r, bb);
306 return false;
309 // Return true if NAME has a range set in block BB.
311 bool
312 block_range_cache::bb_range_p (tree name, const basic_block bb)
314 ssa_block_ranges *ptr = query_block_ranges (name);
315 if (ptr)
316 return ptr->bb_range_p (bb);
317 return false;
320 // Print all known block caches to file F.
322 void
323 block_range_cache::dump (FILE *f)
325 unsigned x;
326 for (x = 0; x < m_ssa_ranges.length (); ++x)
328 if (m_ssa_ranges[x])
330 fprintf (f, " Ranges for ");
331 print_generic_expr (f, ssa_name (x), TDF_NONE);
332 fprintf (f, ":\n");
333 m_ssa_ranges[x]->dump (f);
334 fprintf (f, "\n");
339 // Print all known ranges on entry to blobk BB to file F.
341 void
342 block_range_cache::dump (FILE *f, basic_block bb, bool print_varying)
344 unsigned x;
345 int_range_max r;
346 bool summarize_varying = false;
347 for (x = 1; x < m_ssa_ranges.length (); ++x)
349 if (!gimple_range_ssa_p (ssa_name (x)))
350 continue;
351 if (m_ssa_ranges[x] && m_ssa_ranges[x]->get_bb_range (r, bb))
353 if (!print_varying && r.varying_p ())
355 summarize_varying = true;
356 continue;
358 print_generic_expr (f, ssa_name (x), TDF_NONE);
359 fprintf (f, "\t");
360 r.dump(f);
361 fprintf (f, "\n");
364 // If there were any varying entries, lump them all together.
365 if (summarize_varying)
367 fprintf (f, "VARYING_P on entry : ");
368 for (x = 1; x < num_ssa_names; ++x)
370 if (!gimple_range_ssa_p (ssa_name (x)))
371 continue;
372 if (m_ssa_ranges[x] && m_ssa_ranges[x]->get_bb_range (r, bb))
374 if (r.varying_p ())
376 print_generic_expr (f, ssa_name (x), TDF_NONE);
377 fprintf (f, " ");
381 fprintf (f, "\n");
385 // -------------------------------------------------------------------------
387 // Initialize a global cache.
389 ssa_global_cache::ssa_global_cache ()
391 m_tab.create (0);
392 m_tab.safe_grow_cleared (num_ssa_names);
393 m_irange_allocator = new irange_allocator;
396 // Deconstruct a global cache.
398 ssa_global_cache::~ssa_global_cache ()
400 m_tab.release ();
401 delete m_irange_allocator;
404 // Retrieve the global range of NAME from cache memory if it exists.
405 // Return the value in R.
407 bool
408 ssa_global_cache::get_global_range (irange &r, tree name) const
410 unsigned v = SSA_NAME_VERSION (name);
411 if (v >= m_tab.length ())
412 return false;
414 irange *stow = m_tab[v];
415 if (!stow)
416 return false;
417 r = *stow;
418 return true;
421 // Set the range for NAME to R in the global cache.
422 // Return TRUE if there was already a range set, otherwise false.
424 bool
425 ssa_global_cache::set_global_range (tree name, const irange &r)
427 unsigned v = SSA_NAME_VERSION (name);
428 if (v >= m_tab.length ())
429 m_tab.safe_grow_cleared (num_ssa_names + 1);
431 irange *m = m_tab[v];
432 if (m && m->fits_p (r))
433 *m = r;
434 else
435 m_tab[v] = m_irange_allocator->allocate (r);
436 return m != NULL;
439 // Set the range for NAME to R in the glonbal cache.
441 void
442 ssa_global_cache::clear_global_range (tree name)
444 unsigned v = SSA_NAME_VERSION (name);
445 if (v >= m_tab.length ())
446 m_tab.safe_grow_cleared (num_ssa_names + 1);
447 m_tab[v] = NULL;
450 // Clear the global cache.
452 void
453 ssa_global_cache::clear ()
455 memset (m_tab.address(), 0, m_tab.length () * sizeof (irange *));
458 // Dump the contents of the global cache to F.
460 void
461 ssa_global_cache::dump (FILE *f)
463 unsigned x;
464 int_range_max r;
465 fprintf (f, "Non-varying global ranges:\n");
466 fprintf (f, "=========================:\n");
467 for ( x = 1; x < num_ssa_names; x++)
468 if (gimple_range_ssa_p (ssa_name (x)) &&
469 get_global_range (r, ssa_name (x)) && !r.varying_p ())
471 print_generic_expr (f, ssa_name (x), TDF_NONE);
472 fprintf (f, " : ");
473 r.dump (f);
474 fprintf (f, "\n");
476 fputc ('\n', f);
479 // --------------------------------------------------------------------------
482 // This struct provides a timestamp for a global range calculation.
483 // it contains the time counter, as well as a limited number of ssa-names
484 // that it is dependent upon. If the timestamp for any of the dependent names
485 // Are newer, then this range could need updating.
487 struct range_timestamp
489 unsigned time;
490 unsigned ssa1;
491 unsigned ssa2;
494 // This class will manage the timestamps for each ssa_name.
495 // When a value is calcualted, its timestamp is set to the current time.
496 // The ssanames it is dependent on have already been calculated, so they will
497 // have older times. If one fo those values is ever calculated again, it
498 // will get a newer timestamp, and the "current_p" check will fail.
500 class temporal_cache
502 public:
503 temporal_cache ();
504 ~temporal_cache ();
505 bool current_p (tree name) const;
506 void set_timestamp (tree name);
507 void set_dependency (tree name, tree dep);
508 void set_always_current (tree name);
509 private:
510 unsigned temporal_value (unsigned ssa) const;
511 const range_timestamp *get_timestamp (unsigned ssa) const;
512 range_timestamp *get_timestamp (unsigned ssa);
514 unsigned m_current_time;
515 vec <range_timestamp> m_timestamp;
519 inline
520 temporal_cache::temporal_cache ()
522 m_current_time = 1;
523 m_timestamp.create (0);
524 m_timestamp.safe_grow_cleared (num_ssa_names);
527 inline
528 temporal_cache::~temporal_cache ()
530 m_timestamp.release ();
533 // Return a pointer to the timetamp for ssa-name at index SSA, if there is
534 // one, otherwise return NULL.
536 inline const range_timestamp *
537 temporal_cache::get_timestamp (unsigned ssa) const
539 if (ssa >= m_timestamp.length ())
540 return NULL;
541 return &(m_timestamp[ssa]);
544 // Return a reference to the timetamp for ssa-name at index SSA. If the index
545 // is past the end of the vector, extend the vector.
547 inline range_timestamp *
548 temporal_cache::get_timestamp (unsigned ssa)
550 if (ssa >= m_timestamp.length ())
551 m_timestamp.safe_grow_cleared (num_ssa_names + 20);
552 return &(m_timestamp[ssa]);
555 // This routine will fill NAME's next operand slot with DEP if DEP is a valid
556 // SSA_NAME and there is a free slot.
558 inline void
559 temporal_cache::set_dependency (tree name, tree dep)
561 if (dep && TREE_CODE (dep) == SSA_NAME)
563 gcc_checking_assert (get_timestamp (SSA_NAME_VERSION (name)));
564 range_timestamp& ts = *(get_timestamp (SSA_NAME_VERSION (name)));
565 if (!ts.ssa1)
566 ts.ssa1 = SSA_NAME_VERSION (dep);
567 else if (!ts.ssa2 && ts.ssa1 != SSA_NAME_VERSION (name))
568 ts.ssa2 = SSA_NAME_VERSION (dep);
572 // Return the timestamp value for SSA, or 0 if there isnt one.
573 inline unsigned
574 temporal_cache::temporal_value (unsigned ssa) const
576 const range_timestamp *ts = get_timestamp (ssa);
577 return ts ? ts->time : 0;
580 // Return TRUE if the timestampe for NAME is newer than any of its dependents.
582 bool
583 temporal_cache::current_p (tree name) const
585 const range_timestamp *ts = get_timestamp (SSA_NAME_VERSION (name));
586 if (!ts || ts->time == 0)
587 return true;
588 // Any non-registered dependencies will have a value of 0 and thus be older.
589 // Return true if time is newer than either dependent.
590 return ts->time > temporal_value (ts->ssa1)
591 && ts->time > temporal_value (ts->ssa2);
594 // This increments the global timer and sets the timestamp for NAME.
596 inline void
597 temporal_cache::set_timestamp (tree name)
599 gcc_checking_assert (get_timestamp (SSA_NAME_VERSION (name)));
600 get_timestamp (SSA_NAME_VERSION (name))->time = ++m_current_time;
603 // Set the timestamp to 0, marking it as "always up to date".
605 inline void
606 temporal_cache::set_always_current (tree name)
608 gcc_checking_assert (get_timestamp (SSA_NAME_VERSION (name)));
609 get_timestamp (SSA_NAME_VERSION (name))->time = 0;
613 // --------------------------------------------------------------------------
615 ranger_cache::ranger_cache (gimple_ranger &q) : query (q)
617 m_workback.create (0);
618 m_workback.safe_grow_cleared (last_basic_block_for_fn (cfun));
619 m_update_list.create (0);
620 m_update_list.safe_grow_cleared (last_basic_block_for_fn (cfun));
621 m_update_list.truncate (0);
622 m_poor_value_list.create (0);
623 m_poor_value_list.safe_grow_cleared (20);
624 m_poor_value_list.truncate (0);
625 m_temporal = new temporal_cache;
628 ranger_cache::~ranger_cache ()
630 delete m_temporal;
631 m_poor_value_list.release ();
632 m_workback.release ();
633 m_update_list.release ();
636 // Dump the global caches to file F. if GORI_DUMP is true, dump the
637 // gori map as well.
639 void
640 ranger_cache::dump (FILE *f, bool gori_dump)
642 m_globals.dump (f);
643 if (gori_dump)
645 fprintf (f, "\nDUMPING GORI MAP\n");
646 gori_compute::dump (f);
648 fprintf (f, "\n");
651 // Dump the caches for basic block BB to file F.
653 void
654 ranger_cache::dump (FILE *f, basic_block bb)
656 m_on_entry.dump (f, bb);
659 // Get the global range for NAME, and return in R. Return false if the
660 // global range is not set.
662 bool
663 ranger_cache::get_global_range (irange &r, tree name) const
665 return m_globals.get_global_range (r, name);
668 // Get the global range for NAME, and return in R if the value is not stale.
669 // If the range is set, but is stale, mark it current and return false.
670 // If it is not set pick up the legacy global value, mark it current, and
671 // return false.
672 // Note there is always a value returned in R. The return value indicates
673 // whether that value is an up-to-date calculated value or not..
675 bool
676 ranger_cache::get_non_stale_global_range (irange &r, tree name)
678 if (m_globals.get_global_range (r, name))
680 if (m_temporal->current_p (name))
681 return true;
683 else
685 // Global has never been accessed, so pickup the legacy global value.
686 r = gimple_range_global (name);
687 m_globals.set_global_range (name, r);
689 // After a stale check failure, mark the value as always current until a
690 // new one is set.
691 m_temporal->set_always_current (name);
692 return false;
694 // Set the global range of NAME to R.
696 void
697 ranger_cache::set_global_range (tree name, const irange &r)
699 if (m_globals.set_global_range (name, r))
701 // If there was already a range set, propagate the new value.
702 basic_block bb = gimple_bb (SSA_NAME_DEF_STMT (name));
703 if (!bb)
704 bb = ENTRY_BLOCK_PTR_FOR_FN (cfun);
706 if (DEBUG_RANGE_CACHE)
707 fprintf (dump_file, " GLOBAL :");
709 propagate_updated_value (name, bb);
711 // Mark the value as up-to-date.
712 m_temporal->set_timestamp (name);
715 // Register a dependency on DEP to name. If the timestamp for DEP is ever
716 // greateer than the timestamp for NAME, then it is newer and NAMEs value
717 // becomes stale.
719 void
720 ranger_cache::register_dependency (tree name, tree dep)
722 m_temporal->set_dependency (name, dep);
725 // Push a request for a new lookup in block BB of name. Return true if
726 // the request is actually made (ie, isn't a duplicate).
728 bool
729 ranger_cache::push_poor_value (basic_block bb, tree name)
731 if (m_poor_value_list.length ())
733 // Don't push anything else to the same block. If there are multiple
734 // things required, another request will come during a later evaluation
735 // and this prevents oscillation building uneccessary depth.
736 if ((m_poor_value_list.last ()).bb == bb)
737 return false;
740 struct update_record rec;
741 rec.bb = bb;
742 rec.calc = name;
743 m_poor_value_list.safe_push (rec);
744 return true;
747 // Provide lookup for the gori-computes class to access the best known range
748 // of an ssa_name in any given basic block. Note, this does no additonal
749 // lookups, just accesses the data that is already known.
751 void
752 ranger_cache::ssa_range_in_bb (irange &r, tree name, basic_block bb)
754 gimple *s = SSA_NAME_DEF_STMT (name);
755 basic_block def_bb = ((s && gimple_bb (s)) ? gimple_bb (s) :
756 ENTRY_BLOCK_PTR_FOR_FN (cfun));
757 if (bb == def_bb)
759 // NAME is defined in this block, so request its current value
760 if (!m_globals.get_global_range (r, name))
762 // If it doesn't have a value calculated, it means it's a
763 // "poor" value being used in some calculation. Queue it up
764 // as a poor value to be improved later.
765 r = gimple_range_global (name);
766 if (push_poor_value (bb, name))
768 if (DEBUG_RANGE_CACHE)
770 fprintf (dump_file,
771 "*CACHE* no global def in bb %d for ", bb->index);
772 print_generic_expr (dump_file, name, TDF_SLIM);
773 fprintf (dump_file, " depth : %d\n",
774 m_poor_value_list.length ());
779 // Look for the on-entry value of name in BB from the cache.
780 else if (!m_on_entry.get_bb_range (r, name, bb))
782 // If it has no entry then mark this as a poor value.
783 if (push_poor_value (bb, name))
785 if (DEBUG_RANGE_CACHE)
787 fprintf (dump_file,
788 "*CACHE* no on entry range in bb %d for ", bb->index);
789 print_generic_expr (dump_file, name, TDF_SLIM);
790 fprintf (dump_file, " depth : %d\n", m_poor_value_list.length ());
793 // Try to pick up any known global value as a best guess for now.
794 if (!m_globals.get_global_range (r, name))
795 r = gimple_range_global (name);
798 // Check if pointers have any non-null dereferences. Non-call
799 // exceptions mean we could throw in the middle of the block, so just
800 // punt for now on those.
801 if (r.varying_p () && m_non_null.non_null_deref_p (name, bb) &&
802 !cfun->can_throw_non_call_exceptions)
803 r = range_nonzero (TREE_TYPE (name));
806 // Return a static range for NAME on entry to basic block BB in R. If
807 // calc is true, fill any cache entries required between BB and the
808 // def block for NAME. Otherwise, return false if the cache is empty.
810 bool
811 ranger_cache::block_range (irange &r, basic_block bb, tree name, bool calc)
813 gcc_checking_assert (gimple_range_ssa_p (name));
815 if (calc)
817 gimple *def_stmt = SSA_NAME_DEF_STMT (name);
818 basic_block def_bb = NULL;
819 if (def_stmt)
820 def_bb = gimple_bb (def_stmt);;
821 if (!def_bb)
823 // If we get to the entry block, this better be a default def
824 // or range_on_entry was called for a block not dominated by
825 // the def.
826 gcc_checking_assert (SSA_NAME_IS_DEFAULT_DEF (name));
827 def_bb = ENTRY_BLOCK_PTR_FOR_FN (cfun);
830 // There is no range on entry for the definition block.
831 if (def_bb == bb)
832 return false;
834 // Otherwise, go figure out what is known in predecessor blocks.
835 fill_block_cache (name, bb, def_bb);
836 gcc_checking_assert (m_on_entry.bb_range_p (name, bb));
838 return m_on_entry.get_bb_range (r, name, bb);
841 // Add BB to the list of blocks to update, unless it's already in the list.
843 void
844 ranger_cache::add_to_update (basic_block bb)
846 if (!m_update_list.contains (bb))
847 m_update_list.quick_push (bb);
850 // If there is anything in the propagation update_list, continue
851 // processing NAME until the list of blocks is empty.
853 void
854 ranger_cache::propagate_cache (tree name)
856 basic_block bb;
857 edge_iterator ei;
858 edge e;
859 int_range_max new_range;
860 int_range_max current_range;
861 int_range_max e_range;
863 // Process each block by seeing if its calculated range on entry is
864 // the same as its cached value. If there is a difference, update
865 // the cache to reflect the new value, and check to see if any
866 // successors have cache entries which may need to be checked for
867 // updates.
869 while (m_update_list.length () > 0)
871 bb = m_update_list.pop ();
872 gcc_checking_assert (m_on_entry.bb_range_p (name, bb));
873 m_on_entry.get_bb_range (current_range, name, bb);
875 // Calculate the "new" range on entry by unioning the pred edges.
876 new_range.set_undefined ();
877 FOR_EACH_EDGE (e, ei, bb->preds)
879 if (DEBUG_RANGE_CACHE)
880 fprintf (dump_file, " edge %d->%d :", e->src->index, bb->index);
881 // Get whatever range we can for this edge.
882 if (!outgoing_edge_range_p (e_range, e, name))
884 ssa_range_in_bb (e_range, name, e->src);
885 if (DEBUG_RANGE_CACHE)
887 fprintf (dump_file, "No outgoing edge range, picked up ");
888 e_range.dump(dump_file);
889 fprintf (dump_file, "\n");
892 else
894 if (DEBUG_RANGE_CACHE)
896 fprintf (dump_file, "outgoing range :");
897 e_range.dump(dump_file);
898 fprintf (dump_file, "\n");
901 new_range.union_ (e_range);
902 if (new_range.varying_p ())
903 break;
906 if (DEBUG_RANGE_CACHE)
908 fprintf (dump_file, "FWD visiting block %d for ", bb->index);
909 print_generic_expr (dump_file, name, TDF_SLIM);
910 fprintf (dump_file, " starting range : ");
911 current_range.dump (dump_file);
912 fprintf (dump_file, "\n");
915 // If the range on entry has changed, update it.
916 if (new_range != current_range)
918 if (DEBUG_RANGE_CACHE)
920 fprintf (dump_file, " Updating range to ");
921 new_range.dump (dump_file);
922 fprintf (dump_file, "\n Updating blocks :");
924 m_on_entry.set_bb_range (name, bb, new_range);
925 // Mark each successor that has a range to re-check its range
926 FOR_EACH_EDGE (e, ei, bb->succs)
927 if (m_on_entry.bb_range_p (name, e->dest))
929 if (DEBUG_RANGE_CACHE)
930 fprintf (dump_file, " bb%d",e->dest->index);
931 add_to_update (e->dest);
933 if (DEBUG_RANGE_CACHE)
934 fprintf (dump_file, "\n");
937 if (DEBUG_RANGE_CACHE)
939 fprintf (dump_file, "DONE visiting blocks for ");
940 print_generic_expr (dump_file, name, TDF_SLIM);
941 fprintf (dump_file, "\n");
945 // Check to see if an update to the value for NAME in BB has any effect
946 // on values already in the on-entry cache for successor blocks.
947 // If it does, update them. Don't visit any blocks which dont have a cache
948 // entry.
950 void
951 ranger_cache::propagate_updated_value (tree name, basic_block bb)
953 edge e;
954 edge_iterator ei;
956 // The update work list should be empty at this point.
957 gcc_checking_assert (m_update_list.length () == 0);
958 gcc_checking_assert (bb);
960 if (DEBUG_RANGE_CACHE)
962 fprintf (dump_file, " UPDATE cache for ");
963 print_generic_expr (dump_file, name, TDF_SLIM);
964 fprintf (dump_file, " in BB %d : successors : ", bb->index);
966 FOR_EACH_EDGE (e, ei, bb->succs)
968 // Only update active cache entries.
969 if (m_on_entry.bb_range_p (name, e->dest))
971 add_to_update (e->dest);
972 if (DEBUG_RANGE_CACHE)
973 fprintf (dump_file, " UPDATE: bb%d", e->dest->index);
976 if (m_update_list.length () != 0)
978 if (DEBUG_RANGE_CACHE)
979 fprintf (dump_file, "\n");
980 propagate_cache (name);
982 else
984 if (DEBUG_RANGE_CACHE)
985 fprintf (dump_file, " : No updates!\n");
989 // Make sure that the range-on-entry cache for NAME is set for block BB.
990 // Work back through the CFG to DEF_BB ensuring the range is calculated
991 // on the block/edges leading back to that point.
993 void
994 ranger_cache::fill_block_cache (tree name, basic_block bb, basic_block def_bb)
996 edge_iterator ei;
997 edge e;
998 int_range_max block_result;
999 int_range_max undefined;
1000 unsigned poor_list_start = m_poor_value_list.length ();
1002 // At this point we shouldn't be looking at the def, entry or exit block.
1003 gcc_checking_assert (bb != def_bb && bb != ENTRY_BLOCK_PTR_FOR_FN (cfun) &&
1004 bb != EXIT_BLOCK_PTR_FOR_FN (cfun));
1006 // If the block cache is set, then we've already visited this block.
1007 if (m_on_entry.bb_range_p (name, bb))
1008 return;
1010 // Visit each block back to the DEF. Initialize each one to UNDEFINED.
1011 // m_visited at the end will contain all the blocks that we needed to set
1012 // the range_on_entry cache for.
1013 m_workback.truncate (0);
1014 m_workback.quick_push (bb);
1015 undefined.set_undefined ();
1016 m_on_entry.set_bb_range (name, bb, undefined);
1017 gcc_checking_assert (m_update_list.length () == 0);
1019 if (DEBUG_RANGE_CACHE)
1021 fprintf (dump_file, "\n");
1022 print_generic_expr (dump_file, name, TDF_SLIM);
1023 fprintf (dump_file, " : ");
1026 while (m_workback.length () > 0)
1028 basic_block node = m_workback.pop ();
1029 if (DEBUG_RANGE_CACHE)
1031 fprintf (dump_file, "BACK visiting block %d for ", node->index);
1032 print_generic_expr (dump_file, name, TDF_SLIM);
1033 fprintf (dump_file, "\n");
1036 FOR_EACH_EDGE (e, ei, node->preds)
1038 basic_block pred = e->src;
1039 int_range_max r;
1041 if (DEBUG_RANGE_CACHE)
1042 fprintf (dump_file, " %d->%d ",e->src->index, e->dest->index);
1044 // If the pred block is the def block add this BB to update list.
1045 if (pred == def_bb)
1047 add_to_update (node);
1048 continue;
1051 // If the pred is entry but NOT def, then it is used before
1052 // defined, it'll get set to [] and no need to update it.
1053 if (pred == ENTRY_BLOCK_PTR_FOR_FN (cfun))
1055 if (DEBUG_RANGE_CACHE)
1056 fprintf (dump_file, "entry: bail.");
1057 continue;
1060 // Regardless of whether we have visited pred or not, if the
1061 // pred has a non-null reference, revisit this block.
1062 if (m_non_null.non_null_deref_p (name, pred))
1064 if (DEBUG_RANGE_CACHE)
1065 fprintf (dump_file, "nonnull: update ");
1066 add_to_update (node);
1069 // If the pred block already has a range, or if it can contribute
1070 // something new. Ie, the edge generates a range of some sort.
1071 if (m_on_entry.get_bb_range (r, name, pred))
1073 if (DEBUG_RANGE_CACHE)
1074 fprintf (dump_file, "has cache, ");
1075 if (!r.undefined_p () || has_edge_range_p (e, name))
1077 add_to_update (node);
1078 if (DEBUG_RANGE_CACHE)
1079 fprintf (dump_file, "update. ");
1081 continue;
1084 if (DEBUG_RANGE_CACHE)
1085 fprintf (dump_file, "pushing undefined pred block. ");
1086 // If the pred hasn't been visited (has no range), add it to
1087 // the list.
1088 gcc_checking_assert (!m_on_entry.bb_range_p (name, pred));
1089 m_on_entry.set_bb_range (name, pred, undefined);
1090 m_workback.quick_push (pred);
1094 if (DEBUG_RANGE_CACHE)
1095 fprintf (dump_file, "\n");
1097 // Now fill in the marked blocks with values.
1098 propagate_cache (name);
1099 if (DEBUG_RANGE_CACHE)
1100 fprintf (dump_file, " Propagation update done.\n");
1102 // Now that the cache has been updated, check to see if there were any
1103 // SSA_NAMES used in filling the cache which were "poor values".
1104 // Evaluate them, and inject any new values into the propagation
1105 // list, and see if it improves any on-entry values.
1106 if (poor_list_start != m_poor_value_list.length ())
1108 gcc_checking_assert (poor_list_start < m_poor_value_list.length ());
1109 while (poor_list_start < m_poor_value_list.length ())
1111 // Find a range for this unresolved value.
1112 // Note, this may spawn new cache filling cycles, but by the time it
1113 // is finished, the work vectors will all be back to the same state
1114 // as before the call. The update record vector will always be
1115 // returned to the current state upon return.
1116 struct update_record rec = m_poor_value_list.pop ();
1117 basic_block calc_bb = rec.bb;
1118 int_range_max tmp;
1120 if (DEBUG_RANGE_CACHE)
1122 fprintf (dump_file, "(%d:%d)Calculating ",
1123 m_poor_value_list.length () + 1, poor_list_start);
1124 print_generic_expr (dump_file, name, TDF_SLIM);
1125 fprintf (dump_file, " used POOR VALUE for ");
1126 print_generic_expr (dump_file, rec.calc, TDF_SLIM);
1127 fprintf (dump_file, " in bb%d, trying to improve:\n",
1128 calc_bb->index);
1131 // Calculate a range at the exit from the block so the caches feeding
1132 // this block will be filled, and we'll get a "better" value.
1133 query.range_on_exit (tmp, calc_bb, rec.calc);
1135 // Then ask for NAME to be re-evaluated on outgoing edges and
1136 // use any new values.
1137 propagate_updated_value (name, calc_bb);