c++: 'this' adjustment for devirtualized call
[official-gcc.git] / gcc / gimple-range-cache.cc
blob889cac1ea65d3bced4f1011eb71adadd30e192ca
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.
51 // If SEARCH_DOM is true, the search the dominator tree as well.
53 bool
54 non_null_ref::non_null_deref_p (tree name, basic_block bb, bool search_dom)
56 if (!POINTER_TYPE_P (TREE_TYPE (name)))
57 return false;
59 unsigned v = SSA_NAME_VERSION (name);
60 if (!m_nn[v])
61 process_name (name);
63 if (bitmap_bit_p (m_nn[v], bb->index))
64 return true;
66 // See if any dominator has set non-zero.
67 if (search_dom && dom_info_available_p (CDI_DOMINATORS))
69 // Search back to the Def block, or the top, whichever is closer.
70 basic_block def_bb = gimple_bb (SSA_NAME_DEF_STMT (name));
71 basic_block def_dom = def_bb
72 ? get_immediate_dominator (CDI_DOMINATORS, def_bb)
73 : NULL;
74 for ( ;
75 bb && bb != def_dom;
76 bb = get_immediate_dominator (CDI_DOMINATORS, bb))
77 if (bitmap_bit_p (m_nn[v], bb->index))
78 return true;
80 return false;
83 // Allocate an populate the bitmap for NAME. An ON bit for a block
84 // index indicates there is a non-null reference in that block. In
85 // order to populate the bitmap, a quick run of all the immediate uses
86 // are made and the statement checked to see if a non-null dereference
87 // is made on that statement.
89 void
90 non_null_ref::process_name (tree name)
92 unsigned v = SSA_NAME_VERSION (name);
93 use_operand_p use_p;
94 imm_use_iterator iter;
95 bitmap b;
97 // Only tracked for pointers.
98 if (!POINTER_TYPE_P (TREE_TYPE (name)))
99 return;
101 // Already processed if a bitmap has been allocated.
102 if (m_nn[v])
103 return;
105 b = BITMAP_ALLOC (&m_bitmaps);
107 // Loop over each immediate use and see if it implies a non-null value.
108 FOR_EACH_IMM_USE_FAST (use_p, iter, name)
110 gimple *s = USE_STMT (use_p);
111 unsigned index = gimple_bb (s)->index;
113 // If bit is already set for this block, dont bother looking again.
114 if (bitmap_bit_p (b, index))
115 continue;
117 // If we can infer a nonnull range, then set the bit for this BB
118 if (!SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name)
119 && infer_nonnull_range (s, name))
120 bitmap_set_bit (b, index);
123 m_nn[v] = b;
126 // -------------------------------------------------------------------------
128 // This class represents the API into a cache of ranges for an SSA_NAME.
129 // Routines must be implemented to set, get, and query if a value is set.
131 class ssa_block_ranges
133 public:
134 virtual void set_bb_range (const basic_block bb, const irange &r) = 0;
135 virtual bool get_bb_range (irange &r, const basic_block bb) = 0;
136 virtual bool bb_range_p (const basic_block bb) = 0;
138 void dump(FILE *f);
141 // Print the list of known ranges for file F in a nice format.
143 void
144 ssa_block_ranges::dump (FILE *f)
146 basic_block bb;
147 int_range_max r;
149 FOR_EACH_BB_FN (bb, cfun)
150 if (get_bb_range (r, bb))
152 fprintf (f, "BB%d -> ", bb->index);
153 r.dump (f);
154 fprintf (f, "\n");
158 // This class implements the range cache as a linear vector, indexed by BB.
159 // It caches a varying and undefined range which are used instead of
160 // allocating new ones each time.
162 class sbr_vector : public ssa_block_ranges
164 public:
165 sbr_vector (tree t, irange_allocator *allocator);
167 virtual void set_bb_range (const basic_block bb, const irange &r) OVERRIDE;
168 virtual bool get_bb_range (irange &r, const basic_block bb) OVERRIDE;
169 virtual bool bb_range_p (const basic_block bb) OVERRIDE;
170 protected:
171 irange **m_tab; // Non growing vector.
172 int m_tab_size;
173 int_range<2> m_varying;
174 int_range<2> m_undefined;
175 tree m_type;
176 irange_allocator *m_irange_allocator;
180 // Initialize a block cache for an ssa_name of type T.
182 sbr_vector::sbr_vector (tree t, irange_allocator *allocator)
184 gcc_checking_assert (TYPE_P (t));
185 m_type = t;
186 m_irange_allocator = allocator;
187 m_tab_size = last_basic_block_for_fn (cfun) + 1;
188 m_tab = (irange **)allocator->get_memory (m_tab_size * sizeof (irange *));
189 memset (m_tab, 0, m_tab_size * sizeof (irange *));
191 // Create the cached type range.
192 m_varying.set_varying (t);
193 m_undefined.set_undefined ();
196 // Set the range for block BB to be R.
198 void
199 sbr_vector::set_bb_range (const basic_block bb, const irange &r)
201 irange *m;
202 gcc_checking_assert (bb->index < m_tab_size);
203 if (r.varying_p ())
204 m = &m_varying;
205 else if (r.undefined_p ())
206 m = &m_undefined;
207 else
208 m = m_irange_allocator->allocate (r);
209 m_tab[bb->index] = m;
212 // Return the range associated with block BB in R. Return false if
213 // there is no range.
215 bool
216 sbr_vector::get_bb_range (irange &r, const basic_block bb)
218 gcc_checking_assert (bb->index < m_tab_size);
219 irange *m = m_tab[bb->index];
220 if (m)
222 r = *m;
223 return true;
225 return false;
228 // Return true if a range is present.
230 bool
231 sbr_vector::bb_range_p (const basic_block bb)
233 gcc_checking_assert (bb->index < m_tab_size);
234 return m_tab[bb->index] != NULL;
237 // -------------------------------------------------------------------------
239 // Initialize the block cache.
241 block_range_cache::block_range_cache ()
243 m_ssa_ranges.create (0);
244 m_ssa_ranges.safe_grow_cleared (num_ssa_names);
245 m_irange_allocator = new irange_allocator;
248 // Remove any m_block_caches which have been created.
250 block_range_cache::~block_range_cache ()
252 delete m_irange_allocator;
253 // Release the vector itself.
254 m_ssa_ranges.release ();
257 // Set the range for NAME on entry to block BB to R.
258 // If it has not been // accessed yet, allocate it first.
260 void
261 block_range_cache::set_bb_range (tree name, const basic_block bb,
262 const irange &r)
264 unsigned v = SSA_NAME_VERSION (name);
265 if (v >= m_ssa_ranges.length ())
266 m_ssa_ranges.safe_grow_cleared (num_ssa_names + 1);
268 if (!m_ssa_ranges[v])
270 void *r = m_irange_allocator->get_memory (sizeof (sbr_vector));
271 m_ssa_ranges[v] = new (r) sbr_vector (TREE_TYPE (name),
272 m_irange_allocator);
274 m_ssa_ranges[v]->set_bb_range (bb, r);
278 // Return a pointer to the ssa_block_cache for NAME. If it has not been
279 // accessed yet, return NULL.
281 inline ssa_block_ranges *
282 block_range_cache::query_block_ranges (tree name)
284 unsigned v = SSA_NAME_VERSION (name);
285 if (v >= m_ssa_ranges.length () || !m_ssa_ranges[v])
286 return NULL;
287 return m_ssa_ranges[v];
292 // Return the range for NAME on entry to BB in R. Return true if there
293 // is one.
295 bool
296 block_range_cache::get_bb_range (irange &r, tree name, const basic_block bb)
298 ssa_block_ranges *ptr = query_block_ranges (name);
299 if (ptr)
300 return ptr->get_bb_range (r, bb);
301 return false;
304 // Return true if NAME has a range set in block BB.
306 bool
307 block_range_cache::bb_range_p (tree name, const basic_block bb)
309 ssa_block_ranges *ptr = query_block_ranges (name);
310 if (ptr)
311 return ptr->bb_range_p (bb);
312 return false;
315 // Print all known block caches to file F.
317 void
318 block_range_cache::dump (FILE *f)
320 unsigned x;
321 for (x = 0; x < m_ssa_ranges.length (); ++x)
323 if (m_ssa_ranges[x])
325 fprintf (f, " Ranges for ");
326 print_generic_expr (f, ssa_name (x), TDF_NONE);
327 fprintf (f, ":\n");
328 m_ssa_ranges[x]->dump (f);
329 fprintf (f, "\n");
334 // Print all known ranges on entry to blobk BB to file F.
336 void
337 block_range_cache::dump (FILE *f, basic_block bb, bool print_varying)
339 unsigned x;
340 int_range_max r;
341 bool summarize_varying = false;
342 for (x = 1; x < m_ssa_ranges.length (); ++x)
344 if (!gimple_range_ssa_p (ssa_name (x)))
345 continue;
346 if (m_ssa_ranges[x] && m_ssa_ranges[x]->get_bb_range (r, bb))
348 if (!print_varying && r.varying_p ())
350 summarize_varying = true;
351 continue;
353 print_generic_expr (f, ssa_name (x), TDF_NONE);
354 fprintf (f, "\t");
355 r.dump(f);
356 fprintf (f, "\n");
359 // If there were any varying entries, lump them all together.
360 if (summarize_varying)
362 fprintf (f, "VARYING_P on entry : ");
363 for (x = 1; x < num_ssa_names; ++x)
365 if (!gimple_range_ssa_p (ssa_name (x)))
366 continue;
367 if (m_ssa_ranges[x] && m_ssa_ranges[x]->get_bb_range (r, bb))
369 if (r.varying_p ())
371 print_generic_expr (f, ssa_name (x), TDF_NONE);
372 fprintf (f, " ");
376 fprintf (f, "\n");
380 // -------------------------------------------------------------------------
382 // Initialize a global cache.
384 ssa_global_cache::ssa_global_cache ()
386 m_tab.create (0);
387 m_irange_allocator = new irange_allocator;
390 // Deconstruct a global cache.
392 ssa_global_cache::~ssa_global_cache ()
394 m_tab.release ();
395 delete m_irange_allocator;
398 // Retrieve the global range of NAME from cache memory if it exists.
399 // Return the value in R.
401 bool
402 ssa_global_cache::get_global_range (irange &r, tree name) const
404 unsigned v = SSA_NAME_VERSION (name);
405 if (v >= m_tab.length ())
406 return false;
408 irange *stow = m_tab[v];
409 if (!stow)
410 return false;
411 r = *stow;
412 return true;
415 // Set the range for NAME to R in the global cache.
416 // Return TRUE if there was already a range set, otherwise false.
418 bool
419 ssa_global_cache::set_global_range (tree name, const irange &r)
421 unsigned v = SSA_NAME_VERSION (name);
422 if (v >= m_tab.length ())
423 m_tab.safe_grow_cleared (num_ssa_names + 1);
425 irange *m = m_tab[v];
426 if (m && m->fits_p (r))
427 *m = r;
428 else
429 m_tab[v] = m_irange_allocator->allocate (r);
430 return m != NULL;
433 // Set the range for NAME to R in the glonbal cache.
435 void
436 ssa_global_cache::clear_global_range (tree name)
438 unsigned v = SSA_NAME_VERSION (name);
439 if (v >= m_tab.length ())
440 m_tab.safe_grow_cleared (num_ssa_names + 1);
441 m_tab[v] = NULL;
444 // Clear the global cache.
446 void
447 ssa_global_cache::clear ()
449 memset (m_tab.address(), 0, m_tab.length () * sizeof (irange *));
452 // Dump the contents of the global cache to F.
454 void
455 ssa_global_cache::dump (FILE *f)
457 unsigned x;
458 int_range_max r;
459 fprintf (f, "Non-varying global ranges:\n");
460 fprintf (f, "=========================:\n");
461 for ( x = 1; x < num_ssa_names; x++)
462 if (gimple_range_ssa_p (ssa_name (x)) &&
463 get_global_range (r, ssa_name (x)) && !r.varying_p ())
465 print_generic_expr (f, ssa_name (x), TDF_NONE);
466 fprintf (f, " : ");
467 r.dump (f);
468 fprintf (f, "\n");
470 fputc ('\n', f);
473 // --------------------------------------------------------------------------
476 // This class will manage the timestamps for each ssa_name.
477 // When a value is calculated, the timestamp is set to the current time.
478 // Current time is then incremented. Any dependencies will already have
479 // been calculated, and will thus have older timestamps.
480 // If one of those values is ever calculated again, it will get a newer
481 // timestamp, and the "current_p" check will fail.
483 class temporal_cache
485 public:
486 temporal_cache ();
487 ~temporal_cache ();
488 bool current_p (tree name, tree dep1, tree dep2) const;
489 void set_timestamp (tree name);
490 void set_always_current (tree name);
491 private:
492 unsigned temporal_value (unsigned ssa) const;
494 unsigned m_current_time;
495 vec <unsigned> m_timestamp;
498 inline
499 temporal_cache::temporal_cache ()
501 m_current_time = 1;
502 m_timestamp.create (0);
503 m_timestamp.safe_grow_cleared (num_ssa_names);
506 inline
507 temporal_cache::~temporal_cache ()
509 m_timestamp.release ();
512 // Return the timestamp value for SSA, or 0 if there isnt one.
514 inline unsigned
515 temporal_cache::temporal_value (unsigned ssa) const
517 if (ssa >= m_timestamp.length ())
518 return 0;
519 return m_timestamp[ssa];
522 // Return TRUE if the timestampe for NAME is newer than any of its dependents.
523 // Up to 2 dependencies can be checked.
525 bool
526 temporal_cache::current_p (tree name, tree dep1, tree dep2) const
528 unsigned ts = temporal_value (SSA_NAME_VERSION (name));
529 if (ts == 0)
530 return true;
532 // Any non-registered dependencies will have a value of 0 and thus be older.
533 // Return true if time is newer than either dependent.
535 if (dep1 && ts < temporal_value (SSA_NAME_VERSION (dep1)))
536 return false;
537 if (dep2 && ts < temporal_value (SSA_NAME_VERSION (dep2)))
538 return false;
540 return true;
543 // This increments the global timer and sets the timestamp for NAME.
545 inline void
546 temporal_cache::set_timestamp (tree name)
548 unsigned v = SSA_NAME_VERSION (name);
549 if (v >= m_timestamp.length ())
550 m_timestamp.safe_grow_cleared (num_ssa_names + 20);
551 m_timestamp[v] = ++m_current_time;
554 // Set the timestamp to 0, marking it as "always up to date".
556 inline void
557 temporal_cache::set_always_current (tree name)
559 unsigned v = SSA_NAME_VERSION (name);
560 if (v >= m_timestamp.length ())
561 m_timestamp.safe_grow_cleared (num_ssa_names + 20);
562 m_timestamp[v] = 0;
565 // --------------------------------------------------------------------------
567 ranger_cache::ranger_cache (gimple_ranger &q) : query (q)
569 m_workback.create (0);
570 m_workback.safe_grow_cleared (last_basic_block_for_fn (cfun));
571 m_update_list.create (0);
572 m_update_list.safe_grow_cleared (last_basic_block_for_fn (cfun));
573 m_update_list.truncate (0);
574 m_poor_value_list.create (0);
575 m_poor_value_list.safe_grow_cleared (20);
576 m_poor_value_list.truncate (0);
577 m_temporal = new temporal_cache;
578 unsigned x, lim = last_basic_block_for_fn (cfun);
579 // Calculate outgoing range info upfront. This will fully populate the
580 // m_maybe_variant bitmap which will help eliminate processing of names
581 // which never have their ranges adjusted.
582 for (x = 0; x < lim ; x++)
584 basic_block bb = BASIC_BLOCK_FOR_FN (cfun, x);
585 if (bb)
586 exports (bb);
590 ranger_cache::~ranger_cache ()
592 delete m_temporal;
593 m_poor_value_list.release ();
594 m_workback.release ();
595 m_update_list.release ();
598 // Dump the global caches to file F. if GORI_DUMP is true, dump the
599 // gori map as well.
601 void
602 ranger_cache::dump (FILE *f, bool gori_dump)
604 m_globals.dump (f);
605 if (gori_dump)
607 fprintf (f, "\nDUMPING GORI MAP\n");
608 gori_compute::dump (f);
610 fprintf (f, "\n");
613 // Dump the caches for basic block BB to file F.
615 void
616 ranger_cache::dump (FILE *f, basic_block bb)
618 m_on_entry.dump (f, bb);
621 // Get the global range for NAME, and return in R. Return false if the
622 // global range is not set.
624 bool
625 ranger_cache::get_global_range (irange &r, tree name) const
627 return m_globals.get_global_range (r, name);
630 // Get the global range for NAME, and return in R if the value is not stale.
631 // If the range is set, but is stale, mark it current and return false.
632 // If it is not set pick up the legacy global value, mark it current, and
633 // return false.
634 // Note there is always a value returned in R. The return value indicates
635 // whether that value is an up-to-date calculated value or not..
637 bool
638 ranger_cache::get_non_stale_global_range (irange &r, tree name)
640 if (m_globals.get_global_range (r, name))
642 if (m_temporal->current_p (name, depend1 (name), depend2 (name)))
643 return true;
645 else
647 // Global has never been accessed, so pickup the legacy global value.
648 r = gimple_range_global (name);
649 m_globals.set_global_range (name, r);
651 // After a stale check failure, mark the value as always current until a
652 // new one is set.
653 m_temporal->set_always_current (name);
654 return false;
656 // Set the global range of NAME to R.
658 void
659 ranger_cache::set_global_range (tree name, const irange &r)
661 if (m_globals.set_global_range (name, r))
663 // If there was already a range set, propagate the new value.
664 basic_block bb = gimple_bb (SSA_NAME_DEF_STMT (name));
665 if (!bb)
666 bb = ENTRY_BLOCK_PTR_FOR_FN (cfun);
668 if (DEBUG_RANGE_CACHE)
669 fprintf (dump_file, " GLOBAL :");
671 propagate_updated_value (name, bb);
673 // Constants no longer need to tracked. Any further refinement has to be
674 // undefined. Propagation works better with constants. PR 100512.
675 // Pointers which resolve to non-zero also do not need
676 // tracking in the cache as they will never change. See PR 98866.
677 // Otherwise mark the value as up-to-date.
678 if (r.singleton_p ()
679 || (POINTER_TYPE_P (TREE_TYPE (name)) && r.nonzero_p ()))
681 set_range_invariant (name);
682 m_temporal->set_always_current (name);
684 else
685 m_temporal->set_timestamp (name);
688 // Push a request for a new lookup in block BB of name. Return true if
689 // the request is actually made (ie, isn't a duplicate).
691 bool
692 ranger_cache::push_poor_value (basic_block bb, tree name)
694 if (m_poor_value_list.length ())
696 // Don't push anything else to the same block. If there are multiple
697 // things required, another request will come during a later evaluation
698 // and this prevents oscillation building uneccessary depth.
699 if ((m_poor_value_list.last ()).bb == bb)
700 return false;
703 struct update_record rec;
704 rec.bb = bb;
705 rec.calc = name;
706 m_poor_value_list.safe_push (rec);
707 return true;
710 // Provide lookup for the gori-computes class to access the best known range
711 // of an ssa_name in any given basic block. Note, this does no additonal
712 // lookups, just accesses the data that is already known.
714 void
715 ranger_cache::ssa_range_in_bb (irange &r, tree name, basic_block bb)
717 gimple *s = SSA_NAME_DEF_STMT (name);
718 basic_block def_bb = ((s && gimple_bb (s)) ? gimple_bb (s) :
719 ENTRY_BLOCK_PTR_FOR_FN (cfun));
720 if (bb == def_bb)
722 // NAME is defined in this block, so request its current value
723 if (!m_globals.get_global_range (r, name))
725 // If it doesn't have a value calculated, it means it's a
726 // "poor" value being used in some calculation. Queue it up
727 // as a poor value to be improved later.
728 r = gimple_range_global (name);
729 if (push_poor_value (bb, name))
731 if (DEBUG_RANGE_CACHE)
733 fprintf (dump_file,
734 "*CACHE* no global def in bb %d for ", bb->index);
735 print_generic_expr (dump_file, name, TDF_SLIM);
736 fprintf (dump_file, " depth : %d\n",
737 m_poor_value_list.length ());
742 // Look for the on-entry value of name in BB from the cache.
743 else if (!m_on_entry.get_bb_range (r, name, bb))
745 // If it has no entry but should, then mark this as a poor value.
746 // Its not a poor value if it does not have *any* edge ranges,
747 // Then global range is as good as it gets.
748 if (has_edge_range_p (name) && push_poor_value (bb, name))
750 if (DEBUG_RANGE_CACHE)
752 fprintf (dump_file,
753 "*CACHE* no on entry range in bb %d for ", bb->index);
754 print_generic_expr (dump_file, name, TDF_SLIM);
755 fprintf (dump_file, " depth : %d\n", m_poor_value_list.length ());
758 // Try to pick up any known global value as a best guess for now.
759 if (!m_globals.get_global_range (r, name))
760 r = gimple_range_global (name);
763 // Check if pointers have any non-null dereferences. Non-call
764 // exceptions mean we could throw in the middle of the block, so just
765 // punt for now on those.
766 if (r.varying_p () && m_non_null.non_null_deref_p (name, bb, false) &&
767 !cfun->can_throw_non_call_exceptions)
768 r = range_nonzero (TREE_TYPE (name));
771 // Return a static range for NAME on entry to basic block BB in R. If
772 // calc is true, fill any cache entries required between BB and the
773 // def block for NAME. Otherwise, return false if the cache is empty.
775 bool
776 ranger_cache::block_range (irange &r, basic_block bb, tree name, bool calc)
778 gcc_checking_assert (gimple_range_ssa_p (name));
780 // If there are no range calculations anywhere in the IL, global range
781 // applies everywhere, so don't bother caching it.
782 if (!has_edge_range_p (name))
783 return false;
785 if (calc)
787 gimple *def_stmt = SSA_NAME_DEF_STMT (name);
788 basic_block def_bb = NULL;
789 if (def_stmt)
790 def_bb = gimple_bb (def_stmt);;
791 if (!def_bb)
793 // If we get to the entry block, this better be a default def
794 // or range_on_entry was called for a block not dominated by
795 // the def.
796 gcc_checking_assert (SSA_NAME_IS_DEFAULT_DEF (name));
797 def_bb = ENTRY_BLOCK_PTR_FOR_FN (cfun);
800 // There is no range on entry for the definition block.
801 if (def_bb == bb)
802 return false;
804 // Otherwise, go figure out what is known in predecessor blocks.
805 fill_block_cache (name, bb, def_bb);
806 gcc_checking_assert (m_on_entry.bb_range_p (name, bb));
808 return m_on_entry.get_bb_range (r, name, bb);
811 // Add BB to the list of blocks to update, unless it's already in the list.
813 void
814 ranger_cache::add_to_update (basic_block bb)
816 if (!m_update_list.contains (bb))
817 m_update_list.quick_push (bb);
820 // If there is anything in the propagation update_list, continue
821 // processing NAME until the list of blocks is empty.
823 void
824 ranger_cache::propagate_cache (tree name)
826 basic_block bb;
827 edge_iterator ei;
828 edge e;
829 int_range_max new_range;
830 int_range_max current_range;
831 int_range_max e_range;
833 // Process each block by seeing if its calculated range on entry is
834 // the same as its cached value. If there is a difference, update
835 // the cache to reflect the new value, and check to see if any
836 // successors have cache entries which may need to be checked for
837 // updates.
839 while (m_update_list.length () > 0)
841 bb = m_update_list.pop ();
842 gcc_checking_assert (m_on_entry.bb_range_p (name, bb));
843 m_on_entry.get_bb_range (current_range, name, bb);
845 // Calculate the "new" range on entry by unioning the pred edges.
846 new_range.set_undefined ();
847 FOR_EACH_EDGE (e, ei, bb->preds)
849 if (DEBUG_RANGE_CACHE)
850 fprintf (dump_file, " edge %d->%d :", e->src->index, bb->index);
851 // Get whatever range we can for this edge.
852 if (!outgoing_edge_range_p (e_range, e, name))
854 ssa_range_in_bb (e_range, name, e->src);
855 if (DEBUG_RANGE_CACHE)
857 fprintf (dump_file, "No outgoing edge range, picked up ");
858 e_range.dump(dump_file);
859 fprintf (dump_file, "\n");
862 else
864 if (DEBUG_RANGE_CACHE)
866 fprintf (dump_file, "outgoing range :");
867 e_range.dump(dump_file);
868 fprintf (dump_file, "\n");
871 new_range.union_ (e_range);
872 if (new_range.varying_p ())
873 break;
876 if (DEBUG_RANGE_CACHE)
878 fprintf (dump_file, "FWD visiting block %d for ", bb->index);
879 print_generic_expr (dump_file, name, TDF_SLIM);
880 fprintf (dump_file, " starting range : ");
881 current_range.dump (dump_file);
882 fprintf (dump_file, "\n");
885 // If the range on entry has changed, update it.
886 if (new_range != current_range)
888 if (DEBUG_RANGE_CACHE)
890 fprintf (dump_file, " Updating range to ");
891 new_range.dump (dump_file);
892 fprintf (dump_file, "\n Updating blocks :");
894 m_on_entry.set_bb_range (name, bb, new_range);
895 // Mark each successor that has a range to re-check its range
896 FOR_EACH_EDGE (e, ei, bb->succs)
897 if (m_on_entry.bb_range_p (name, e->dest))
899 if (DEBUG_RANGE_CACHE)
900 fprintf (dump_file, " bb%d",e->dest->index);
901 add_to_update (e->dest);
903 if (DEBUG_RANGE_CACHE)
904 fprintf (dump_file, "\n");
907 if (DEBUG_RANGE_CACHE)
909 fprintf (dump_file, "DONE visiting blocks for ");
910 print_generic_expr (dump_file, name, TDF_SLIM);
911 fprintf (dump_file, "\n");
915 // Check to see if an update to the value for NAME in BB has any effect
916 // on values already in the on-entry cache for successor blocks.
917 // If it does, update them. Don't visit any blocks which dont have a cache
918 // entry.
920 void
921 ranger_cache::propagate_updated_value (tree name, basic_block bb)
923 edge e;
924 edge_iterator ei;
926 // The update work list should be empty at this point.
927 gcc_checking_assert (m_update_list.length () == 0);
928 gcc_checking_assert (bb);
930 if (DEBUG_RANGE_CACHE)
932 fprintf (dump_file, " UPDATE cache for ");
933 print_generic_expr (dump_file, name, TDF_SLIM);
934 fprintf (dump_file, " in BB %d : successors : ", bb->index);
936 FOR_EACH_EDGE (e, ei, bb->succs)
938 // Only update active cache entries.
939 if (m_on_entry.bb_range_p (name, e->dest))
941 add_to_update (e->dest);
942 if (DEBUG_RANGE_CACHE)
943 fprintf (dump_file, " UPDATE: bb%d", e->dest->index);
946 if (m_update_list.length () != 0)
948 if (DEBUG_RANGE_CACHE)
949 fprintf (dump_file, "\n");
950 propagate_cache (name);
952 else
954 if (DEBUG_RANGE_CACHE)
955 fprintf (dump_file, " : No updates!\n");
959 // Make sure that the range-on-entry cache for NAME is set for block BB.
960 // Work back through the CFG to DEF_BB ensuring the range is calculated
961 // on the block/edges leading back to that point.
963 void
964 ranger_cache::fill_block_cache (tree name, basic_block bb, basic_block def_bb)
966 edge_iterator ei;
967 edge e;
968 int_range_max block_result;
969 int_range_max undefined;
970 unsigned poor_list_start = m_poor_value_list.length ();
972 // At this point we shouldn't be looking at the def, entry or exit block.
973 gcc_checking_assert (bb != def_bb && bb != ENTRY_BLOCK_PTR_FOR_FN (cfun) &&
974 bb != EXIT_BLOCK_PTR_FOR_FN (cfun));
976 // If the block cache is set, then we've already visited this block.
977 if (m_on_entry.bb_range_p (name, bb))
978 return;
980 // Visit each block back to the DEF. Initialize each one to UNDEFINED.
981 // m_visited at the end will contain all the blocks that we needed to set
982 // the range_on_entry cache for.
983 m_workback.truncate (0);
984 m_workback.quick_push (bb);
985 undefined.set_undefined ();
986 m_on_entry.set_bb_range (name, bb, undefined);
987 gcc_checking_assert (m_update_list.length () == 0);
989 if (DEBUG_RANGE_CACHE)
991 fprintf (dump_file, "\n");
992 print_generic_expr (dump_file, name, TDF_SLIM);
993 fprintf (dump_file, " : ");
996 while (m_workback.length () > 0)
998 basic_block node = m_workback.pop ();
999 if (DEBUG_RANGE_CACHE)
1001 fprintf (dump_file, "BACK visiting block %d for ", node->index);
1002 print_generic_expr (dump_file, name, TDF_SLIM);
1003 fprintf (dump_file, "\n");
1006 FOR_EACH_EDGE (e, ei, node->preds)
1008 basic_block pred = e->src;
1009 int_range_max r;
1011 if (DEBUG_RANGE_CACHE)
1012 fprintf (dump_file, " %d->%d ",e->src->index, e->dest->index);
1014 // If the pred block is the def block add this BB to update list.
1015 if (pred == def_bb)
1017 add_to_update (node);
1018 continue;
1021 // If the pred is entry but NOT def, then it is used before
1022 // defined, it'll get set to [] and no need to update it.
1023 if (pred == ENTRY_BLOCK_PTR_FOR_FN (cfun))
1025 if (DEBUG_RANGE_CACHE)
1026 fprintf (dump_file, "entry: bail.");
1027 continue;
1030 // Regardless of whether we have visited pred or not, if the
1031 // pred has a non-null reference, revisit this block.
1032 // Don't search the DOM tree.
1033 if (m_non_null.non_null_deref_p (name, pred, false))
1035 if (DEBUG_RANGE_CACHE)
1036 fprintf (dump_file, "nonnull: update ");
1037 add_to_update (node);
1040 // If the pred block already has a range, or if it can contribute
1041 // something new. Ie, the edge generates a range of some sort.
1042 if (m_on_entry.get_bb_range (r, name, pred))
1044 if (DEBUG_RANGE_CACHE)
1045 fprintf (dump_file, "has cache, ");
1046 if (!r.undefined_p () || has_edge_range_p (name, e))
1048 add_to_update (node);
1049 if (DEBUG_RANGE_CACHE)
1050 fprintf (dump_file, "update. ");
1052 continue;
1055 if (DEBUG_RANGE_CACHE)
1056 fprintf (dump_file, "pushing undefined pred block. ");
1057 // If the pred hasn't been visited (has no range), add it to
1058 // the list.
1059 gcc_checking_assert (!m_on_entry.bb_range_p (name, pred));
1060 m_on_entry.set_bb_range (name, pred, undefined);
1061 m_workback.quick_push (pred);
1065 if (DEBUG_RANGE_CACHE)
1066 fprintf (dump_file, "\n");
1068 // Now fill in the marked blocks with values.
1069 propagate_cache (name);
1070 if (DEBUG_RANGE_CACHE)
1071 fprintf (dump_file, " Propagation update done.\n");
1073 // Now that the cache has been updated, check to see if there were any
1074 // SSA_NAMES used in filling the cache which were "poor values".
1075 // Evaluate them, and inject any new values into the propagation
1076 // list, and see if it improves any on-entry values.
1077 if (poor_list_start != m_poor_value_list.length ())
1079 gcc_checking_assert (poor_list_start < m_poor_value_list.length ());
1080 while (poor_list_start < m_poor_value_list.length ())
1082 // Find a range for this unresolved value.
1083 // Note, this may spawn new cache filling cycles, but by the time it
1084 // is finished, the work vectors will all be back to the same state
1085 // as before the call. The update record vector will always be
1086 // returned to the current state upon return.
1087 struct update_record rec = m_poor_value_list.pop ();
1088 basic_block calc_bb = rec.bb;
1089 int_range_max tmp;
1091 if (DEBUG_RANGE_CACHE)
1093 fprintf (dump_file, "(%d:%d)Calculating ",
1094 m_poor_value_list.length () + 1, poor_list_start);
1095 print_generic_expr (dump_file, name, TDF_SLIM);
1096 fprintf (dump_file, " used POOR VALUE for ");
1097 print_generic_expr (dump_file, rec.calc, TDF_SLIM);
1098 fprintf (dump_file, " in bb%d, trying to improve:\n",
1099 calc_bb->index);
1102 // Calculate a range at the exit from the block so the caches feeding
1103 // this block will be filled, and we'll get a "better" value.
1104 query.range_on_exit (tmp, calc_bb, rec.calc);
1106 // Then ask for NAME to be re-evaluated on outgoing edges and
1107 // use any new values.
1108 propagate_updated_value (name, calc_bb);