aix: TLS DWARF symbol decorations.
[official-gcc.git] / gcc / gimple-range-cache.cc
blob38e4fe1c7c0933adba6cb1b274f5813f5a426755
1 /* Gimple ranger SSA cache implementation.
2 Copyright (C) 2017-2021 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 but should, then mark this as a poor value.
783 // Its not a poor value if it does not have *any* edge ranges,
784 // Then global range is as good as it gets.
785 if (has_edge_range_p (name) && push_poor_value (bb, name))
787 if (DEBUG_RANGE_CACHE)
789 fprintf (dump_file,
790 "*CACHE* no on entry range in bb %d for ", bb->index);
791 print_generic_expr (dump_file, name, TDF_SLIM);
792 fprintf (dump_file, " depth : %d\n", m_poor_value_list.length ());
795 // Try to pick up any known global value as a best guess for now.
796 if (!m_globals.get_global_range (r, name))
797 r = gimple_range_global (name);
800 // Check if pointers have any non-null dereferences. Non-call
801 // exceptions mean we could throw in the middle of the block, so just
802 // punt for now on those.
803 if (r.varying_p () && m_non_null.non_null_deref_p (name, bb) &&
804 !cfun->can_throw_non_call_exceptions)
805 r = range_nonzero (TREE_TYPE (name));
808 // Return a static range for NAME on entry to basic block BB in R. If
809 // calc is true, fill any cache entries required between BB and the
810 // def block for NAME. Otherwise, return false if the cache is empty.
812 bool
813 ranger_cache::block_range (irange &r, basic_block bb, tree name, bool calc)
815 gcc_checking_assert (gimple_range_ssa_p (name));
817 // If there are no range calculations anywhere in the IL, global range
818 // applies everywhere, so don't bother caching it.
819 if (!has_edge_range_p (name))
820 return false;
822 if (calc)
824 gimple *def_stmt = SSA_NAME_DEF_STMT (name);
825 basic_block def_bb = NULL;
826 if (def_stmt)
827 def_bb = gimple_bb (def_stmt);;
828 if (!def_bb)
830 // If we get to the entry block, this better be a default def
831 // or range_on_entry was called for a block not dominated by
832 // the def.
833 gcc_checking_assert (SSA_NAME_IS_DEFAULT_DEF (name));
834 def_bb = ENTRY_BLOCK_PTR_FOR_FN (cfun);
837 // There is no range on entry for the definition block.
838 if (def_bb == bb)
839 return false;
841 // Otherwise, go figure out what is known in predecessor blocks.
842 fill_block_cache (name, bb, def_bb);
843 gcc_checking_assert (m_on_entry.bb_range_p (name, bb));
845 return m_on_entry.get_bb_range (r, name, bb);
848 // Add BB to the list of blocks to update, unless it's already in the list.
850 void
851 ranger_cache::add_to_update (basic_block bb)
853 if (!m_update_list.contains (bb))
854 m_update_list.quick_push (bb);
857 // If there is anything in the propagation update_list, continue
858 // processing NAME until the list of blocks is empty.
860 void
861 ranger_cache::propagate_cache (tree name)
863 basic_block bb;
864 edge_iterator ei;
865 edge e;
866 int_range_max new_range;
867 int_range_max current_range;
868 int_range_max e_range;
870 // Process each block by seeing if its calculated range on entry is
871 // the same as its cached value. If there is a difference, update
872 // the cache to reflect the new value, and check to see if any
873 // successors have cache entries which may need to be checked for
874 // updates.
876 while (m_update_list.length () > 0)
878 bb = m_update_list.pop ();
879 gcc_checking_assert (m_on_entry.bb_range_p (name, bb));
880 m_on_entry.get_bb_range (current_range, name, bb);
882 // Calculate the "new" range on entry by unioning the pred edges.
883 new_range.set_undefined ();
884 FOR_EACH_EDGE (e, ei, bb->preds)
886 if (DEBUG_RANGE_CACHE)
887 fprintf (dump_file, " edge %d->%d :", e->src->index, bb->index);
888 // Get whatever range we can for this edge.
889 if (!outgoing_edge_range_p (e_range, e, name))
891 ssa_range_in_bb (e_range, name, e->src);
892 if (DEBUG_RANGE_CACHE)
894 fprintf (dump_file, "No outgoing edge range, picked up ");
895 e_range.dump(dump_file);
896 fprintf (dump_file, "\n");
899 else
901 if (DEBUG_RANGE_CACHE)
903 fprintf (dump_file, "outgoing range :");
904 e_range.dump(dump_file);
905 fprintf (dump_file, "\n");
908 new_range.union_ (e_range);
909 if (new_range.varying_p ())
910 break;
913 if (DEBUG_RANGE_CACHE)
915 fprintf (dump_file, "FWD visiting block %d for ", bb->index);
916 print_generic_expr (dump_file, name, TDF_SLIM);
917 fprintf (dump_file, " starting range : ");
918 current_range.dump (dump_file);
919 fprintf (dump_file, "\n");
922 // If the range on entry has changed, update it.
923 if (new_range != current_range)
925 if (DEBUG_RANGE_CACHE)
927 fprintf (dump_file, " Updating range to ");
928 new_range.dump (dump_file);
929 fprintf (dump_file, "\n Updating blocks :");
931 m_on_entry.set_bb_range (name, bb, new_range);
932 // Mark each successor that has a range to re-check its range
933 FOR_EACH_EDGE (e, ei, bb->succs)
934 if (m_on_entry.bb_range_p (name, e->dest))
936 if (DEBUG_RANGE_CACHE)
937 fprintf (dump_file, " bb%d",e->dest->index);
938 add_to_update (e->dest);
940 if (DEBUG_RANGE_CACHE)
941 fprintf (dump_file, "\n");
944 if (DEBUG_RANGE_CACHE)
946 fprintf (dump_file, "DONE visiting blocks for ");
947 print_generic_expr (dump_file, name, TDF_SLIM);
948 fprintf (dump_file, "\n");
952 // Check to see if an update to the value for NAME in BB has any effect
953 // on values already in the on-entry cache for successor blocks.
954 // If it does, update them. Don't visit any blocks which dont have a cache
955 // entry.
957 void
958 ranger_cache::propagate_updated_value (tree name, basic_block bb)
960 edge e;
961 edge_iterator ei;
963 // The update work list should be empty at this point.
964 gcc_checking_assert (m_update_list.length () == 0);
965 gcc_checking_assert (bb);
967 if (DEBUG_RANGE_CACHE)
969 fprintf (dump_file, " UPDATE cache for ");
970 print_generic_expr (dump_file, name, TDF_SLIM);
971 fprintf (dump_file, " in BB %d : successors : ", bb->index);
973 FOR_EACH_EDGE (e, ei, bb->succs)
975 // Only update active cache entries.
976 if (m_on_entry.bb_range_p (name, e->dest))
978 add_to_update (e->dest);
979 if (DEBUG_RANGE_CACHE)
980 fprintf (dump_file, " UPDATE: bb%d", e->dest->index);
983 if (m_update_list.length () != 0)
985 if (DEBUG_RANGE_CACHE)
986 fprintf (dump_file, "\n");
987 propagate_cache (name);
989 else
991 if (DEBUG_RANGE_CACHE)
992 fprintf (dump_file, " : No updates!\n");
996 // Make sure that the range-on-entry cache for NAME is set for block BB.
997 // Work back through the CFG to DEF_BB ensuring the range is calculated
998 // on the block/edges leading back to that point.
1000 void
1001 ranger_cache::fill_block_cache (tree name, basic_block bb, basic_block def_bb)
1003 edge_iterator ei;
1004 edge e;
1005 int_range_max block_result;
1006 int_range_max undefined;
1007 unsigned poor_list_start = m_poor_value_list.length ();
1009 // At this point we shouldn't be looking at the def, entry or exit block.
1010 gcc_checking_assert (bb != def_bb && bb != ENTRY_BLOCK_PTR_FOR_FN (cfun) &&
1011 bb != EXIT_BLOCK_PTR_FOR_FN (cfun));
1013 // If the block cache is set, then we've already visited this block.
1014 if (m_on_entry.bb_range_p (name, bb))
1015 return;
1017 // Visit each block back to the DEF. Initialize each one to UNDEFINED.
1018 // m_visited at the end will contain all the blocks that we needed to set
1019 // the range_on_entry cache for.
1020 m_workback.truncate (0);
1021 m_workback.quick_push (bb);
1022 undefined.set_undefined ();
1023 m_on_entry.set_bb_range (name, bb, undefined);
1024 gcc_checking_assert (m_update_list.length () == 0);
1026 if (DEBUG_RANGE_CACHE)
1028 fprintf (dump_file, "\n");
1029 print_generic_expr (dump_file, name, TDF_SLIM);
1030 fprintf (dump_file, " : ");
1033 while (m_workback.length () > 0)
1035 basic_block node = m_workback.pop ();
1036 if (DEBUG_RANGE_CACHE)
1038 fprintf (dump_file, "BACK visiting block %d for ", node->index);
1039 print_generic_expr (dump_file, name, TDF_SLIM);
1040 fprintf (dump_file, "\n");
1043 FOR_EACH_EDGE (e, ei, node->preds)
1045 basic_block pred = e->src;
1046 int_range_max r;
1048 if (DEBUG_RANGE_CACHE)
1049 fprintf (dump_file, " %d->%d ",e->src->index, e->dest->index);
1051 // If the pred block is the def block add this BB to update list.
1052 if (pred == def_bb)
1054 add_to_update (node);
1055 continue;
1058 // If the pred is entry but NOT def, then it is used before
1059 // defined, it'll get set to [] and no need to update it.
1060 if (pred == ENTRY_BLOCK_PTR_FOR_FN (cfun))
1062 if (DEBUG_RANGE_CACHE)
1063 fprintf (dump_file, "entry: bail.");
1064 continue;
1067 // Regardless of whether we have visited pred or not, if the
1068 // pred has a non-null reference, revisit this block.
1069 if (m_non_null.non_null_deref_p (name, pred))
1071 if (DEBUG_RANGE_CACHE)
1072 fprintf (dump_file, "nonnull: update ");
1073 add_to_update (node);
1076 // If the pred block already has a range, or if it can contribute
1077 // something new. Ie, the edge generates a range of some sort.
1078 if (m_on_entry.get_bb_range (r, name, pred))
1080 if (DEBUG_RANGE_CACHE)
1081 fprintf (dump_file, "has cache, ");
1082 if (!r.undefined_p () || has_edge_range_p (name, e))
1084 add_to_update (node);
1085 if (DEBUG_RANGE_CACHE)
1086 fprintf (dump_file, "update. ");
1088 continue;
1091 if (DEBUG_RANGE_CACHE)
1092 fprintf (dump_file, "pushing undefined pred block. ");
1093 // If the pred hasn't been visited (has no range), add it to
1094 // the list.
1095 gcc_checking_assert (!m_on_entry.bb_range_p (name, pred));
1096 m_on_entry.set_bb_range (name, pred, undefined);
1097 m_workback.quick_push (pred);
1101 if (DEBUG_RANGE_CACHE)
1102 fprintf (dump_file, "\n");
1104 // Now fill in the marked blocks with values.
1105 propagate_cache (name);
1106 if (DEBUG_RANGE_CACHE)
1107 fprintf (dump_file, " Propagation update done.\n");
1109 // Now that the cache has been updated, check to see if there were any
1110 // SSA_NAMES used in filling the cache which were "poor values".
1111 // Evaluate them, and inject any new values into the propagation
1112 // list, and see if it improves any on-entry values.
1113 if (poor_list_start != m_poor_value_list.length ())
1115 gcc_checking_assert (poor_list_start < m_poor_value_list.length ());
1116 while (poor_list_start < m_poor_value_list.length ())
1118 // Find a range for this unresolved value.
1119 // Note, this may spawn new cache filling cycles, but by the time it
1120 // is finished, the work vectors will all be back to the same state
1121 // as before the call. The update record vector will always be
1122 // returned to the current state upon return.
1123 struct update_record rec = m_poor_value_list.pop ();
1124 basic_block calc_bb = rec.bb;
1125 int_range_max tmp;
1127 if (DEBUG_RANGE_CACHE)
1129 fprintf (dump_file, "(%d:%d)Calculating ",
1130 m_poor_value_list.length () + 1, poor_list_start);
1131 print_generic_expr (dump_file, name, TDF_SLIM);
1132 fprintf (dump_file, " used POOR VALUE for ");
1133 print_generic_expr (dump_file, rec.calc, TDF_SLIM);
1134 fprintf (dump_file, " in bb%d, trying to improve:\n",
1135 calc_bb->index);
1138 // Calculate a range at the exit from the block so the caches feeding
1139 // this block will be filled, and we'll get a "better" value.
1140 query.range_on_exit (tmp, calc_bb, rec.calc);
1142 // Then ask for NAME to be re-evaluated on outgoing edges and
1143 // use any new values.
1144 propagate_updated_value (name, calc_bb);