gcov: make profile merging smarter
[official-gcc.git] / gcc / gimple-range-cache.cc
blob9cbc63d8a405f45aa3a6cd478c5b73add5800d10
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"
31 #include "tree-cfg.h"
33 #define DEBUG_RANGE_CACHE (dump_file && (param_evrp_mode & EVRP_MODE_CACHE) \
34 == EVRP_MODE_CACHE)
36 // During contructor, allocate the vector of ssa_names.
38 non_null_ref::non_null_ref ()
40 m_nn.create (num_ssa_names);
41 m_nn.quick_grow_cleared (num_ssa_names);
42 bitmap_obstack_initialize (&m_bitmaps);
45 // Free any bitmaps which were allocated,a swell as the vector itself.
47 non_null_ref::~non_null_ref ()
49 bitmap_obstack_release (&m_bitmaps);
50 m_nn.release ();
53 // Return true if NAME has a non-null dereference in block bb. If this is the
54 // first query for NAME, calculate the summary first.
55 // If SEARCH_DOM is true, the search the dominator tree as well.
57 bool
58 non_null_ref::non_null_deref_p (tree name, basic_block bb, bool search_dom)
60 if (!POINTER_TYPE_P (TREE_TYPE (name)))
61 return false;
63 unsigned v = SSA_NAME_VERSION (name);
64 if (v >= m_nn.length ())
65 m_nn.safe_grow_cleared (num_ssa_names + 1);
67 if (!m_nn[v])
68 process_name (name);
70 if (bitmap_bit_p (m_nn[v], bb->index))
71 return true;
73 // See if any dominator has set non-zero.
74 if (search_dom && dom_info_available_p (CDI_DOMINATORS))
76 // Search back to the Def block, or the top, whichever is closer.
77 basic_block def_bb = gimple_bb (SSA_NAME_DEF_STMT (name));
78 basic_block def_dom = def_bb
79 ? get_immediate_dominator (CDI_DOMINATORS, def_bb)
80 : NULL;
81 for ( ;
82 bb && bb != def_dom;
83 bb = get_immediate_dominator (CDI_DOMINATORS, bb))
84 if (bitmap_bit_p (m_nn[v], bb->index))
85 return true;
87 return false;
90 // If NAME has a non-null dereference in block BB, adjust R with the
91 // non-zero information from non_null_deref_p, and return TRUE. If
92 // SEARCH_DOM is true, non_null_deref_p should search the dominator tree.
94 bool
95 non_null_ref::adjust_range (irange &r, tree name, basic_block bb,
96 bool search_dom)
98 // Non-call exceptions mean we could throw in the middle of the
99 // block, so just punt on those for now.
100 if (cfun->can_throw_non_call_exceptions)
101 return false;
103 // We only care about the null / non-null property of pointers.
104 if (!POINTER_TYPE_P (TREE_TYPE (name)))
105 return false;
106 if (r.undefined_p () || r.lower_bound () != 0 || r.upper_bound () == 0)
107 return false;
108 // Check if pointers have any non-null dereferences.
109 if (non_null_deref_p (name, bb, search_dom))
111 // Remove zero from the range.
112 unsigned prec = TYPE_PRECISION (TREE_TYPE (name));
113 r.intersect (wi::one (prec), wi::max_value (prec, UNSIGNED));
114 return true;
116 return false;
119 // Allocate an populate the bitmap for NAME. An ON bit for a block
120 // index indicates there is a non-null reference in that block. In
121 // order to populate the bitmap, a quick run of all the immediate uses
122 // are made and the statement checked to see if a non-null dereference
123 // is made on that statement.
125 void
126 non_null_ref::process_name (tree name)
128 unsigned v = SSA_NAME_VERSION (name);
129 use_operand_p use_p;
130 imm_use_iterator iter;
131 bitmap b;
133 // Only tracked for pointers.
134 if (!POINTER_TYPE_P (TREE_TYPE (name)))
135 return;
137 // Already processed if a bitmap has been allocated.
138 if (m_nn[v])
139 return;
141 b = BITMAP_ALLOC (&m_bitmaps);
143 // Loop over each immediate use and see if it implies a non-null value.
144 FOR_EACH_IMM_USE_FAST (use_p, iter, name)
146 gimple *s = USE_STMT (use_p);
147 unsigned index = gimple_bb (s)->index;
149 // If bit is already set for this block, dont bother looking again.
150 if (bitmap_bit_p (b, index))
151 continue;
153 // If we can infer a nonnull range, then set the bit for this BB
154 if (!SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name)
155 && infer_nonnull_range (s, name))
156 bitmap_set_bit (b, index);
159 m_nn[v] = b;
162 // -------------------------------------------------------------------------
164 // This class represents the API into a cache of ranges for an SSA_NAME.
165 // Routines must be implemented to set, get, and query if a value is set.
167 class ssa_block_ranges
169 public:
170 virtual bool set_bb_range (const_basic_block bb, const irange &r) = 0;
171 virtual bool get_bb_range (irange &r, const_basic_block bb) = 0;
172 virtual bool bb_range_p (const_basic_block bb) = 0;
174 void dump(FILE *f);
177 // Print the list of known ranges for file F in a nice format.
179 void
180 ssa_block_ranges::dump (FILE *f)
182 basic_block bb;
183 int_range_max r;
185 FOR_EACH_BB_FN (bb, cfun)
186 if (get_bb_range (r, bb))
188 fprintf (f, "BB%d -> ", bb->index);
189 r.dump (f);
190 fprintf (f, "\n");
194 // This class implements the range cache as a linear vector, indexed by BB.
195 // It caches a varying and undefined range which are used instead of
196 // allocating new ones each time.
198 class sbr_vector : public ssa_block_ranges
200 public:
201 sbr_vector (tree t, irange_allocator *allocator);
203 virtual bool set_bb_range (const_basic_block bb, const irange &r) OVERRIDE;
204 virtual bool get_bb_range (irange &r, const_basic_block bb) OVERRIDE;
205 virtual bool bb_range_p (const_basic_block bb) OVERRIDE;
206 protected:
207 irange **m_tab; // Non growing vector.
208 int m_tab_size;
209 int_range<2> m_varying;
210 int_range<2> m_undefined;
211 tree m_type;
212 irange_allocator *m_irange_allocator;
216 // Initialize a block cache for an ssa_name of type T.
218 sbr_vector::sbr_vector (tree t, irange_allocator *allocator)
220 gcc_checking_assert (TYPE_P (t));
221 m_type = t;
222 m_irange_allocator = allocator;
223 m_tab_size = last_basic_block_for_fn (cfun) + 1;
224 m_tab = (irange **)allocator->get_memory (m_tab_size * sizeof (irange *));
225 memset (m_tab, 0, m_tab_size * sizeof (irange *));
227 // Create the cached type range.
228 m_varying.set_varying (t);
229 m_undefined.set_undefined ();
232 // Set the range for block BB to be R.
234 bool
235 sbr_vector::set_bb_range (const_basic_block bb, const irange &r)
237 irange *m;
238 gcc_checking_assert (bb->index < m_tab_size);
239 if (r.varying_p ())
240 m = &m_varying;
241 else if (r.undefined_p ())
242 m = &m_undefined;
243 else
244 m = m_irange_allocator->allocate (r);
245 m_tab[bb->index] = m;
246 return true;
249 // Return the range associated with block BB in R. Return false if
250 // there is no range.
252 bool
253 sbr_vector::get_bb_range (irange &r, const_basic_block bb)
255 gcc_checking_assert (bb->index < m_tab_size);
256 irange *m = m_tab[bb->index];
257 if (m)
259 r = *m;
260 return true;
262 return false;
265 // Return true if a range is present.
267 bool
268 sbr_vector::bb_range_p (const_basic_block bb)
270 gcc_checking_assert (bb->index < m_tab_size);
271 return m_tab[bb->index] != NULL;
274 // This class implements the on entry cache via a sparse bitmap.
275 // It uses the quad bit routines to access 4 bits at a time.
276 // A value of 0 (the default) means there is no entry, and a value of
277 // 1 thru SBR_NUM represents an element in the m_range vector.
278 // Varying is given the first value (1) and pre-cached.
279 // SBR_NUM + 1 represents the value of UNDEFINED, and is never stored.
280 // SBR_NUM is the number of values that can be cached.
281 // Indexes are 1..SBR_NUM and are stored locally at m_range[0..SBR_NUM-1]
283 #define SBR_NUM 14
284 #define SBR_UNDEF SBR_NUM + 1
285 #define SBR_VARYING 1
287 class sbr_sparse_bitmap : public ssa_block_ranges
289 public:
290 sbr_sparse_bitmap (tree t, irange_allocator *allocator, bitmap_obstack *bm);
291 virtual bool set_bb_range (const_basic_block bb, const irange &r) OVERRIDE;
292 virtual bool get_bb_range (irange &r, const_basic_block bb) OVERRIDE;
293 virtual bool bb_range_p (const_basic_block bb) OVERRIDE;
294 private:
295 void bitmap_set_quad (bitmap head, int quad, int quad_value);
296 int bitmap_get_quad (const_bitmap head, int quad);
297 irange_allocator *m_irange_allocator;
298 irange *m_range[SBR_NUM];
299 bitmap bitvec;
300 tree m_type;
303 // Initialize a block cache for an ssa_name of type T.
305 sbr_sparse_bitmap::sbr_sparse_bitmap (tree t, irange_allocator *allocator,
306 bitmap_obstack *bm)
308 gcc_checking_assert (TYPE_P (t));
309 m_type = t;
310 bitvec = BITMAP_ALLOC (bm);
311 m_irange_allocator = allocator;
312 // Pre-cache varying.
313 m_range[0] = m_irange_allocator->allocate (2);
314 m_range[0]->set_varying (t);
315 // Pre-cache zero and non-zero values for pointers.
316 if (POINTER_TYPE_P (t))
318 m_range[1] = m_irange_allocator->allocate (2);
319 m_range[1]->set_nonzero (t);
320 m_range[2] = m_irange_allocator->allocate (2);
321 m_range[2]->set_zero (t);
323 else
324 m_range[1] = m_range[2] = NULL;
325 // Clear SBR_NUM entries.
326 for (int x = 3; x < SBR_NUM; x++)
327 m_range[x] = 0;
330 // Set 4 bit values in a sparse bitmap. This allows a bitmap to
331 // function as a sparse array of 4 bit values.
332 // QUAD is the index, QUAD_VALUE is the 4 bit value to set.
334 inline void
335 sbr_sparse_bitmap::bitmap_set_quad (bitmap head, int quad, int quad_value)
337 bitmap_set_aligned_chunk (head, quad, 4, (BITMAP_WORD) quad_value);
340 // Get a 4 bit value from a sparse bitmap. This allows a bitmap to
341 // function as a sparse array of 4 bit values.
342 // QUAD is the index.
343 inline int
344 sbr_sparse_bitmap::bitmap_get_quad (const_bitmap head, int quad)
346 return (int) bitmap_get_aligned_chunk (head, quad, 4);
349 // Set the range on entry to basic block BB to R.
351 bool
352 sbr_sparse_bitmap::set_bb_range (const_basic_block bb, const irange &r)
354 if (r.undefined_p ())
356 bitmap_set_quad (bitvec, bb->index, SBR_UNDEF);
357 return true;
360 // Loop thru the values to see if R is already present.
361 for (int x = 0; x < SBR_NUM; x++)
362 if (!m_range[x] || r == *(m_range[x]))
364 if (!m_range[x])
365 m_range[x] = m_irange_allocator->allocate (r);
366 bitmap_set_quad (bitvec, bb->index, x + 1);
367 return true;
369 // All values are taken, default to VARYING.
370 bitmap_set_quad (bitvec, bb->index, SBR_VARYING);
371 return false;
374 // Return the range associated with block BB in R. Return false if
375 // there is no range.
377 bool
378 sbr_sparse_bitmap::get_bb_range (irange &r, const_basic_block bb)
380 int value = bitmap_get_quad (bitvec, bb->index);
382 if (!value)
383 return false;
385 gcc_checking_assert (value <= SBR_UNDEF);
386 if (value == SBR_UNDEF)
387 r.set_undefined ();
388 else
389 r = *(m_range[value - 1]);
390 return true;
393 // Return true if a range is present.
395 bool
396 sbr_sparse_bitmap::bb_range_p (const_basic_block bb)
398 return (bitmap_get_quad (bitvec, bb->index) != 0);
401 // -------------------------------------------------------------------------
403 // Initialize the block cache.
405 block_range_cache::block_range_cache ()
407 bitmap_obstack_initialize (&m_bitmaps);
408 m_ssa_ranges.create (0);
409 m_ssa_ranges.safe_grow_cleared (num_ssa_names);
410 m_irange_allocator = new irange_allocator;
413 // Remove any m_block_caches which have been created.
415 block_range_cache::~block_range_cache ()
417 delete m_irange_allocator;
418 // Release the vector itself.
419 m_ssa_ranges.release ();
420 bitmap_obstack_release (&m_bitmaps);
423 // Set the range for NAME on entry to block BB to R.
424 // If it has not been accessed yet, allocate it first.
426 bool
427 block_range_cache::set_bb_range (tree name, const_basic_block bb,
428 const irange &r)
430 unsigned v = SSA_NAME_VERSION (name);
431 if (v >= m_ssa_ranges.length ())
432 m_ssa_ranges.safe_grow_cleared (num_ssa_names + 1);
434 if (!m_ssa_ranges[v])
436 // Use sparse representation if there are too many basic blocks.
437 if (last_basic_block_for_fn (cfun) > param_evrp_sparse_threshold)
439 void *r = m_irange_allocator->get_memory (sizeof (sbr_sparse_bitmap));
440 m_ssa_ranges[v] = new (r) sbr_sparse_bitmap (TREE_TYPE (name),
441 m_irange_allocator,
442 &m_bitmaps);
444 else
446 // Otherwise use the default vector implemntation.
447 void *r = m_irange_allocator->get_memory (sizeof (sbr_vector));
448 m_ssa_ranges[v] = new (r) sbr_vector (TREE_TYPE (name),
449 m_irange_allocator);
452 return m_ssa_ranges[v]->set_bb_range (bb, r);
456 // Return a pointer to the ssa_block_cache for NAME. If it has not been
457 // accessed yet, return NULL.
459 inline ssa_block_ranges *
460 block_range_cache::query_block_ranges (tree name)
462 unsigned v = SSA_NAME_VERSION (name);
463 if (v >= m_ssa_ranges.length () || !m_ssa_ranges[v])
464 return NULL;
465 return m_ssa_ranges[v];
470 // Return the range for NAME on entry to BB in R. Return true if there
471 // is one.
473 bool
474 block_range_cache::get_bb_range (irange &r, tree name, const_basic_block bb)
476 ssa_block_ranges *ptr = query_block_ranges (name);
477 if (ptr)
478 return ptr->get_bb_range (r, bb);
479 return false;
482 // Return true if NAME has a range set in block BB.
484 bool
485 block_range_cache::bb_range_p (tree name, const_basic_block bb)
487 ssa_block_ranges *ptr = query_block_ranges (name);
488 if (ptr)
489 return ptr->bb_range_p (bb);
490 return false;
493 // Print all known block caches to file F.
495 void
496 block_range_cache::dump (FILE *f)
498 unsigned x;
499 for (x = 0; x < m_ssa_ranges.length (); ++x)
501 if (m_ssa_ranges[x])
503 fprintf (f, " Ranges for ");
504 print_generic_expr (f, ssa_name (x), TDF_NONE);
505 fprintf (f, ":\n");
506 m_ssa_ranges[x]->dump (f);
507 fprintf (f, "\n");
512 // Print all known ranges on entry to blobk BB to file F.
514 void
515 block_range_cache::dump (FILE *f, basic_block bb, bool print_varying)
517 unsigned x;
518 int_range_max r;
519 bool summarize_varying = false;
520 for (x = 1; x < m_ssa_ranges.length (); ++x)
522 if (!gimple_range_ssa_p (ssa_name (x)))
523 continue;
524 if (m_ssa_ranges[x] && m_ssa_ranges[x]->get_bb_range (r, bb))
526 if (!print_varying && r.varying_p ())
528 summarize_varying = true;
529 continue;
531 print_generic_expr (f, ssa_name (x), TDF_NONE);
532 fprintf (f, "\t");
533 r.dump(f);
534 fprintf (f, "\n");
537 // If there were any varying entries, lump them all together.
538 if (summarize_varying)
540 fprintf (f, "VARYING_P on entry : ");
541 for (x = 1; x < num_ssa_names; ++x)
543 if (!gimple_range_ssa_p (ssa_name (x)))
544 continue;
545 if (m_ssa_ranges[x] && m_ssa_ranges[x]->get_bb_range (r, bb))
547 if (r.varying_p ())
549 print_generic_expr (f, ssa_name (x), TDF_NONE);
550 fprintf (f, " ");
554 fprintf (f, "\n");
558 // -------------------------------------------------------------------------
560 // Initialize a global cache.
562 ssa_global_cache::ssa_global_cache ()
564 m_tab.create (0);
565 m_irange_allocator = new irange_allocator;
568 // Deconstruct a global cache.
570 ssa_global_cache::~ssa_global_cache ()
572 m_tab.release ();
573 delete m_irange_allocator;
576 // Retrieve the global range of NAME from cache memory if it exists.
577 // Return the value in R.
579 bool
580 ssa_global_cache::get_global_range (irange &r, tree name) const
582 unsigned v = SSA_NAME_VERSION (name);
583 if (v >= m_tab.length ())
584 return false;
586 irange *stow = m_tab[v];
587 if (!stow)
588 return false;
589 r = *stow;
590 return true;
593 // Set the range for NAME to R in the global cache.
594 // Return TRUE if there was already a range set, otherwise false.
596 bool
597 ssa_global_cache::set_global_range (tree name, const irange &r)
599 unsigned v = SSA_NAME_VERSION (name);
600 if (v >= m_tab.length ())
601 m_tab.safe_grow_cleared (num_ssa_names + 1);
603 irange *m = m_tab[v];
604 if (m && m->fits_p (r))
605 *m = r;
606 else
607 m_tab[v] = m_irange_allocator->allocate (r);
608 return m != NULL;
611 // Set the range for NAME to R in the glonbal cache.
613 void
614 ssa_global_cache::clear_global_range (tree name)
616 unsigned v = SSA_NAME_VERSION (name);
617 if (v >= m_tab.length ())
618 m_tab.safe_grow_cleared (num_ssa_names + 1);
619 m_tab[v] = NULL;
622 // Clear the global cache.
624 void
625 ssa_global_cache::clear ()
627 memset (m_tab.address(), 0, m_tab.length () * sizeof (irange *));
630 // Dump the contents of the global cache to F.
632 void
633 ssa_global_cache::dump (FILE *f)
635 /* Cleared after the table header has been printed. */
636 bool print_header = true;
637 for (unsigned x = 1; x < num_ssa_names; x++)
639 int_range_max r;
640 if (gimple_range_ssa_p (ssa_name (x)) &&
641 get_global_range (r, ssa_name (x)) && !r.varying_p ())
643 if (print_header)
645 /* Print the header only when there's something else
646 to print below. */
647 fprintf (f, "Non-varying global ranges:\n");
648 fprintf (f, "=========================:\n");
649 print_header = false;
652 print_generic_expr (f, ssa_name (x), TDF_NONE);
653 fprintf (f, " : ");
654 r.dump (f);
655 fprintf (f, "\n");
659 if (!print_header)
660 fputc ('\n', f);
663 // --------------------------------------------------------------------------
666 // This class will manage the timestamps for each ssa_name.
667 // When a value is calculated, the timestamp is set to the current time.
668 // Current time is then incremented. Any dependencies will already have
669 // been calculated, and will thus have older timestamps.
670 // If one of those values is ever calculated again, it will get a newer
671 // timestamp, and the "current_p" check will fail.
673 class temporal_cache
675 public:
676 temporal_cache ();
677 ~temporal_cache ();
678 bool current_p (tree name, tree dep1, tree dep2) const;
679 void set_timestamp (tree name);
680 void set_always_current (tree name);
681 private:
682 unsigned temporal_value (unsigned ssa) const;
684 unsigned m_current_time;
685 vec <unsigned> m_timestamp;
688 inline
689 temporal_cache::temporal_cache ()
691 m_current_time = 1;
692 m_timestamp.create (0);
693 m_timestamp.safe_grow_cleared (num_ssa_names);
696 inline
697 temporal_cache::~temporal_cache ()
699 m_timestamp.release ();
702 // Return the timestamp value for SSA, or 0 if there isnt one.
704 inline unsigned
705 temporal_cache::temporal_value (unsigned ssa) const
707 if (ssa >= m_timestamp.length ())
708 return 0;
709 return m_timestamp[ssa];
712 // Return TRUE if the timestampe for NAME is newer than any of its dependents.
713 // Up to 2 dependencies can be checked.
715 bool
716 temporal_cache::current_p (tree name, tree dep1, tree dep2) const
718 unsigned ts = temporal_value (SSA_NAME_VERSION (name));
719 if (ts == 0)
720 return true;
722 // Any non-registered dependencies will have a value of 0 and thus be older.
723 // Return true if time is newer than either dependent.
725 if (dep1 && ts < temporal_value (SSA_NAME_VERSION (dep1)))
726 return false;
727 if (dep2 && ts < temporal_value (SSA_NAME_VERSION (dep2)))
728 return false;
730 return true;
733 // This increments the global timer and sets the timestamp for NAME.
735 inline void
736 temporal_cache::set_timestamp (tree name)
738 unsigned v = SSA_NAME_VERSION (name);
739 if (v >= m_timestamp.length ())
740 m_timestamp.safe_grow_cleared (num_ssa_names + 20);
741 m_timestamp[v] = ++m_current_time;
744 // Set the timestamp to 0, marking it as "always up to date".
746 inline void
747 temporal_cache::set_always_current (tree name)
749 unsigned v = SSA_NAME_VERSION (name);
750 if (v >= m_timestamp.length ())
751 m_timestamp.safe_grow_cleared (num_ssa_names + 20);
752 m_timestamp[v] = 0;
755 // --------------------------------------------------------------------------
757 ranger_cache::ranger_cache (int not_executable_flag)
758 : m_gori (not_executable_flag)
760 m_workback.create (0);
761 m_workback.safe_grow_cleared (last_basic_block_for_fn (cfun));
762 m_update_list.create (0);
763 m_update_list.safe_grow_cleared (last_basic_block_for_fn (cfun));
764 m_update_list.truncate (0);
765 m_temporal = new temporal_cache;
766 // If DOM info is available, spawn an oracle as well.
767 if (dom_info_available_p (CDI_DOMINATORS))
768 m_oracle = new dom_oracle ();
769 else
770 m_oracle = NULL;
772 unsigned x, lim = last_basic_block_for_fn (cfun);
773 // Calculate outgoing range info upfront. This will fully populate the
774 // m_maybe_variant bitmap which will help eliminate processing of names
775 // which never have their ranges adjusted.
776 for (x = 0; x < lim ; x++)
778 basic_block bb = BASIC_BLOCK_FOR_FN (cfun, x);
779 if (bb)
780 m_gori.exports (bb);
782 m_propfail = BITMAP_ALLOC (NULL);
785 ranger_cache::~ranger_cache ()
787 BITMAP_FREE (m_propfail);
788 if (m_oracle)
789 delete m_oracle;
790 delete m_temporal;
791 m_workback.release ();
792 m_update_list.release ();
795 // Dump the global caches to file F. if GORI_DUMP is true, dump the
796 // gori map as well.
798 void
799 ranger_cache::dump (FILE *f)
801 m_globals.dump (f);
802 fprintf (f, "\n");
805 // Dump the caches for basic block BB to file F.
807 void
808 ranger_cache::dump_bb (FILE *f, basic_block bb)
810 m_gori.gori_map::dump (f, bb, false);
811 m_on_entry.dump (f, bb);
812 if (m_oracle)
813 m_oracle->dump (f, bb);
816 // Get the global range for NAME, and return in R. Return false if the
817 // global range is not set.
819 bool
820 ranger_cache::get_global_range (irange &r, tree name) const
822 return m_globals.get_global_range (r, name);
825 // Get the global range for NAME, and return in R if the value is not stale.
826 // If the range is set, but is stale, mark it current and return false.
827 // If it is not set pick up the legacy global value, mark it current, and
828 // return false.
829 // Note there is always a value returned in R. The return value indicates
830 // whether that value is an up-to-date calculated value or not..
832 bool
833 ranger_cache::get_non_stale_global_range (irange &r, tree name)
835 if (m_globals.get_global_range (r, name))
837 // Use this value if the range is constant or current.
838 if (r.singleton_p ()
839 || m_temporal->current_p (name, m_gori.depend1 (name),
840 m_gori.depend2 (name)))
841 return true;
843 else
845 // Global has never been accessed, so pickup the legacy global value.
846 r = gimple_range_global (name);
847 m_globals.set_global_range (name, r);
849 // After a stale check failure, mark the value as always current until a
850 // new one is set.
851 m_temporal->set_always_current (name);
852 return false;
854 // Set the global range of NAME to R.
856 void
857 ranger_cache::set_global_range (tree name, const irange &r)
859 if (m_globals.set_global_range (name, r))
861 // If there was already a range set, propagate the new value.
862 basic_block bb = gimple_bb (SSA_NAME_DEF_STMT (name));
863 if (!bb)
864 bb = ENTRY_BLOCK_PTR_FOR_FN (cfun);
866 if (DEBUG_RANGE_CACHE)
867 fprintf (dump_file, " GLOBAL :");
869 propagate_updated_value (name, bb);
871 // Constants no longer need to tracked. Any further refinement has to be
872 // undefined. Propagation works better with constants. PR 100512.
873 // Pointers which resolve to non-zero also do not need
874 // tracking in the cache as they will never change. See PR 98866.
875 // Timestamp must always be updated, or dependent calculations may
876 // not include this latest value. PR 100774.
878 if (r.singleton_p ()
879 || (POINTER_TYPE_P (TREE_TYPE (name)) && r.nonzero_p ()))
880 m_gori.set_range_invariant (name);
881 m_temporal->set_timestamp (name);
884 // Provide lookup for the gori-computes class to access the best known range
885 // of an ssa_name in any given basic block. Note, this does no additonal
886 // lookups, just accesses the data that is already known.
888 // Get the range of NAME when the def occurs in block BB. If BB is NULL
889 // get the best global value available.
891 void
892 ranger_cache::range_of_def (irange &r, tree name, basic_block bb)
894 gcc_checking_assert (gimple_range_ssa_p (name));
895 gcc_checking_assert (!bb || bb == gimple_bb (SSA_NAME_DEF_STMT (name)));
897 // Pick up the best global range available.
898 if (!m_globals.get_global_range (r, name))
900 // If that fails, try to calculate the range using just global values.
901 gimple *s = SSA_NAME_DEF_STMT (name);
902 if (gimple_get_lhs (s) == name)
903 fold_range (r, s, get_global_range_query ());
904 else
905 r = gimple_range_global (name);
908 if (bb)
909 m_non_null.adjust_range (r, name, bb, false);
912 // Get the range of NAME as it occurs on entry to block BB.
914 void
915 ranger_cache::entry_range (irange &r, tree name, basic_block bb)
917 if (bb == ENTRY_BLOCK_PTR_FOR_FN (cfun))
919 r = gimple_range_global (name);
920 return;
923 // Look for the on-entry value of name in BB from the cache.
924 // Otherwise pick up the best available global value.
925 if (!m_on_entry.get_bb_range (r, name, bb))
926 range_of_def (r, name);
928 m_non_null.adjust_range (r, name, bb, false);
931 // Get the range of NAME as it occurs on exit from block BB.
933 void
934 ranger_cache::exit_range (irange &r, tree name, basic_block bb)
936 if (bb == ENTRY_BLOCK_PTR_FOR_FN (cfun))
938 r = gimple_range_global (name);
939 return;
942 gimple *s = SSA_NAME_DEF_STMT (name);
943 basic_block def_bb = gimple_bb (s);
944 if (def_bb == bb)
945 range_of_def (r, name, bb);
946 else
947 entry_range (r, name, bb);
951 // Implement range_of_expr.
953 bool
954 ranger_cache::range_of_expr (irange &r, tree name, gimple *stmt)
956 if (!gimple_range_ssa_p (name))
958 get_tree_range (r, name, stmt);
959 return true;
962 basic_block bb = gimple_bb (stmt);
963 gimple *def_stmt = SSA_NAME_DEF_STMT (name);
964 basic_block def_bb = gimple_bb (def_stmt);
966 if (bb == def_bb)
967 range_of_def (r, name, bb);
968 else
969 entry_range (r, name, bb);
970 return true;
974 // Implement range_on_edge. Always return the best available range.
976 bool
977 ranger_cache::range_on_edge (irange &r, edge e, tree expr)
979 if (gimple_range_ssa_p (expr))
981 exit_range (r, expr, e->src);
982 int_range_max edge_range;
983 if (m_gori.outgoing_edge_range_p (edge_range, e, expr, *this))
984 r.intersect (edge_range);
985 return true;
988 return get_tree_range (r, expr, NULL);
992 // Return a static range for NAME on entry to basic block BB in R. If
993 // calc is true, fill any cache entries required between BB and the
994 // def block for NAME. Otherwise, return false if the cache is empty.
996 bool
997 ranger_cache::block_range (irange &r, basic_block bb, tree name, bool calc)
999 gcc_checking_assert (gimple_range_ssa_p (name));
1001 // If there are no range calculations anywhere in the IL, global range
1002 // applies everywhere, so don't bother caching it.
1003 if (!m_gori.has_edge_range_p (name))
1004 return false;
1006 if (calc)
1008 gimple *def_stmt = SSA_NAME_DEF_STMT (name);
1009 basic_block def_bb = NULL;
1010 if (def_stmt)
1011 def_bb = gimple_bb (def_stmt);;
1012 if (!def_bb)
1014 // If we get to the entry block, this better be a default def
1015 // or range_on_entry was called for a block not dominated by
1016 // the def.
1017 gcc_checking_assert (SSA_NAME_IS_DEFAULT_DEF (name));
1018 def_bb = ENTRY_BLOCK_PTR_FOR_FN (cfun);
1021 // There is no range on entry for the definition block.
1022 if (def_bb == bb)
1023 return false;
1025 // Otherwise, go figure out what is known in predecessor blocks.
1026 fill_block_cache (name, bb, def_bb);
1027 gcc_checking_assert (m_on_entry.bb_range_p (name, bb));
1029 return m_on_entry.get_bb_range (r, name, bb);
1032 // Add BB to the list of blocks to update, unless it's already in the list.
1034 void
1035 ranger_cache::add_to_update (basic_block bb)
1037 // If propagation has failed for BB, or its already in the list, don't
1038 // add it again.
1039 if (!bitmap_bit_p (m_propfail, bb->index) && !m_update_list.contains (bb))
1040 m_update_list.quick_push (bb);
1043 // If there is anything in the propagation update_list, continue
1044 // processing NAME until the list of blocks is empty.
1046 void
1047 ranger_cache::propagate_cache (tree name)
1049 basic_block bb;
1050 edge_iterator ei;
1051 edge e;
1052 int_range_max new_range;
1053 int_range_max current_range;
1054 int_range_max e_range;
1056 gcc_checking_assert (bitmap_empty_p (m_propfail));
1057 // Process each block by seeing if its calculated range on entry is
1058 // the same as its cached value. If there is a difference, update
1059 // the cache to reflect the new value, and check to see if any
1060 // successors have cache entries which may need to be checked for
1061 // updates.
1063 while (m_update_list.length () > 0)
1065 bb = m_update_list.pop ();
1066 gcc_checking_assert (m_on_entry.bb_range_p (name, bb));
1067 m_on_entry.get_bb_range (current_range, name, bb);
1069 if (DEBUG_RANGE_CACHE)
1071 fprintf (dump_file, "FWD visiting block %d for ", bb->index);
1072 print_generic_expr (dump_file, name, TDF_SLIM);
1073 fprintf (dump_file, " starting range : ");
1074 current_range.dump (dump_file);
1075 fprintf (dump_file, "\n");
1078 // Calculate the "new" range on entry by unioning the pred edges.
1079 new_range.set_undefined ();
1080 FOR_EACH_EDGE (e, ei, bb->preds)
1082 range_on_edge (e_range, e, name);
1083 if (DEBUG_RANGE_CACHE)
1085 fprintf (dump_file, " edge %d->%d :", e->src->index, bb->index);
1086 e_range.dump (dump_file);
1087 fprintf (dump_file, "\n");
1089 new_range.union_ (e_range);
1090 if (new_range.varying_p ())
1091 break;
1094 // If the range on entry has changed, update it.
1095 if (new_range != current_range)
1097 bool ok_p = m_on_entry.set_bb_range (name, bb, new_range);
1098 // If the cache couldn't set the value, mark it as failed.
1099 if (!ok_p)
1100 bitmap_set_bit (m_propfail, bb->index);
1101 if (DEBUG_RANGE_CACHE)
1103 if (!ok_p)
1105 fprintf (dump_file, " Cache failure to store value:");
1106 print_generic_expr (dump_file, name, TDF_SLIM);
1107 fprintf (dump_file, " ");
1109 else
1111 fprintf (dump_file, " Updating range to ");
1112 new_range.dump (dump_file);
1114 fprintf (dump_file, "\n Updating blocks :");
1116 // Mark each successor that has a range to re-check its range
1117 FOR_EACH_EDGE (e, ei, bb->succs)
1118 if (m_on_entry.bb_range_p (name, e->dest))
1120 if (DEBUG_RANGE_CACHE)
1121 fprintf (dump_file, " bb%d",e->dest->index);
1122 add_to_update (e->dest);
1124 if (DEBUG_RANGE_CACHE)
1125 fprintf (dump_file, "\n");
1128 if (DEBUG_RANGE_CACHE)
1130 fprintf (dump_file, "DONE visiting blocks for ");
1131 print_generic_expr (dump_file, name, TDF_SLIM);
1132 fprintf (dump_file, "\n");
1134 bitmap_clear (m_propfail);
1137 // Check to see if an update to the value for NAME in BB has any effect
1138 // on values already in the on-entry cache for successor blocks.
1139 // If it does, update them. Don't visit any blocks which dont have a cache
1140 // entry.
1142 void
1143 ranger_cache::propagate_updated_value (tree name, basic_block bb)
1145 edge e;
1146 edge_iterator ei;
1148 // The update work list should be empty at this point.
1149 gcc_checking_assert (m_update_list.length () == 0);
1150 gcc_checking_assert (bb);
1152 if (DEBUG_RANGE_CACHE)
1154 fprintf (dump_file, " UPDATE cache for ");
1155 print_generic_expr (dump_file, name, TDF_SLIM);
1156 fprintf (dump_file, " in BB %d : successors : ", bb->index);
1158 FOR_EACH_EDGE (e, ei, bb->succs)
1160 // Only update active cache entries.
1161 if (m_on_entry.bb_range_p (name, e->dest))
1163 add_to_update (e->dest);
1164 if (DEBUG_RANGE_CACHE)
1165 fprintf (dump_file, " UPDATE: bb%d", e->dest->index);
1168 if (m_update_list.length () != 0)
1170 if (DEBUG_RANGE_CACHE)
1171 fprintf (dump_file, "\n");
1172 propagate_cache (name);
1174 else
1176 if (DEBUG_RANGE_CACHE)
1177 fprintf (dump_file, " : No updates!\n");
1181 // Make sure that the range-on-entry cache for NAME is set for block BB.
1182 // Work back through the CFG to DEF_BB ensuring the range is calculated
1183 // on the block/edges leading back to that point.
1185 void
1186 ranger_cache::fill_block_cache (tree name, basic_block bb, basic_block def_bb)
1188 edge_iterator ei;
1189 edge e;
1190 int_range_max block_result;
1191 int_range_max undefined;
1193 // At this point we shouldn't be looking at the def, entry or exit block.
1194 gcc_checking_assert (bb != def_bb && bb != ENTRY_BLOCK_PTR_FOR_FN (cfun) &&
1195 bb != EXIT_BLOCK_PTR_FOR_FN (cfun));
1197 // If the block cache is set, then we've already visited this block.
1198 if (m_on_entry.bb_range_p (name, bb))
1199 return;
1201 // Visit each block back to the DEF. Initialize each one to UNDEFINED.
1202 // m_visited at the end will contain all the blocks that we needed to set
1203 // the range_on_entry cache for.
1204 m_workback.truncate (0);
1205 m_workback.quick_push (bb);
1206 undefined.set_undefined ();
1207 m_on_entry.set_bb_range (name, bb, undefined);
1208 gcc_checking_assert (m_update_list.length () == 0);
1210 if (DEBUG_RANGE_CACHE)
1212 fprintf (dump_file, "\n");
1213 print_generic_expr (dump_file, name, TDF_SLIM);
1214 fprintf (dump_file, " : ");
1217 while (m_workback.length () > 0)
1219 basic_block node = m_workback.pop ();
1220 if (DEBUG_RANGE_CACHE)
1222 fprintf (dump_file, "BACK visiting block %d for ", node->index);
1223 print_generic_expr (dump_file, name, TDF_SLIM);
1224 fprintf (dump_file, "\n");
1227 FOR_EACH_EDGE (e, ei, node->preds)
1229 basic_block pred = e->src;
1230 int_range_max r;
1232 if (DEBUG_RANGE_CACHE)
1233 fprintf (dump_file, " %d->%d ",e->src->index, e->dest->index);
1235 // If the pred block is the def block add this BB to update list.
1236 if (pred == def_bb)
1238 add_to_update (node);
1239 continue;
1242 // If the pred is entry but NOT def, then it is used before
1243 // defined, it'll get set to [] and no need to update it.
1244 if (pred == ENTRY_BLOCK_PTR_FOR_FN (cfun))
1246 if (DEBUG_RANGE_CACHE)
1247 fprintf (dump_file, "entry: bail.");
1248 continue;
1251 // Regardless of whether we have visited pred or not, if the
1252 // pred has a non-null reference, revisit this block.
1253 // Don't search the DOM tree.
1254 if (m_non_null.non_null_deref_p (name, pred, false))
1256 if (DEBUG_RANGE_CACHE)
1257 fprintf (dump_file, "nonnull: update ");
1258 add_to_update (node);
1261 // If the pred block already has a range, or if it can contribute
1262 // something new. Ie, the edge generates a range of some sort.
1263 if (m_on_entry.get_bb_range (r, name, pred))
1265 if (DEBUG_RANGE_CACHE)
1267 fprintf (dump_file, "has cache, ");
1268 r.dump (dump_file);
1269 fprintf (dump_file, ", ");
1271 if (!r.undefined_p () || m_gori.has_edge_range_p (name, e))
1273 add_to_update (node);
1274 if (DEBUG_RANGE_CACHE)
1275 fprintf (dump_file, "update. ");
1277 continue;
1280 if (DEBUG_RANGE_CACHE)
1281 fprintf (dump_file, "pushing undefined pred block.\n");
1282 // If the pred hasn't been visited (has no range), add it to
1283 // the list.
1284 gcc_checking_assert (!m_on_entry.bb_range_p (name, pred));
1285 m_on_entry.set_bb_range (name, pred, undefined);
1286 m_workback.quick_push (pred);
1290 if (DEBUG_RANGE_CACHE)
1291 fprintf (dump_file, "\n");
1293 // Now fill in the marked blocks with values.
1294 propagate_cache (name);
1295 if (DEBUG_RANGE_CACHE)
1296 fprintf (dump_file, " Propagation update done.\n");