Don't warn when alignment of global common data exceeds maximum alignment.
[official-gcc.git] / gcc / gimple-range-cache.cc
blob4138d0556c678b5baacd8a7d83338cba7b7591b2
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 (0);
41 m_nn.safe_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 (!m_nn[v])
65 process_name (name);
67 if (bitmap_bit_p (m_nn[v], bb->index))
68 return true;
70 // See if any dominator has set non-zero.
71 if (search_dom && dom_info_available_p (CDI_DOMINATORS))
73 // Search back to the Def block, or the top, whichever is closer.
74 basic_block def_bb = gimple_bb (SSA_NAME_DEF_STMT (name));
75 basic_block def_dom = def_bb
76 ? get_immediate_dominator (CDI_DOMINATORS, def_bb)
77 : NULL;
78 for ( ;
79 bb && bb != def_dom;
80 bb = get_immediate_dominator (CDI_DOMINATORS, bb))
81 if (bitmap_bit_p (m_nn[v], bb->index))
82 return true;
84 return false;
87 // If NAME has a non-null dereference in block BB, adjust R with the
88 // non-zero information from non_null_deref_p, and return TRUE. If
89 // SEARCH_DOM is true, non_null_deref_p should search the dominator tree.
91 bool
92 non_null_ref::adjust_range (irange &r, tree name, basic_block bb,
93 bool search_dom)
95 // Non-call exceptions mean we could throw in the middle of the
96 // block, so just punt on those for now.
97 if (cfun->can_throw_non_call_exceptions)
98 return false;
100 // We only care about the null / non-null property of pointers.
101 if (!POINTER_TYPE_P (TREE_TYPE (name)) || r.zero_p () || r.nonzero_p ())
102 return false;
104 // Check if pointers have any non-null dereferences.
105 if (non_null_deref_p (name, bb, search_dom))
107 int_range<2> nz;
108 nz.set_nonzero (TREE_TYPE (name));
109 r.intersect (nz);
110 return true;
112 return false;
115 // Allocate an populate the bitmap for NAME. An ON bit for a block
116 // index indicates there is a non-null reference in that block. In
117 // order to populate the bitmap, a quick run of all the immediate uses
118 // are made and the statement checked to see if a non-null dereference
119 // is made on that statement.
121 void
122 non_null_ref::process_name (tree name)
124 unsigned v = SSA_NAME_VERSION (name);
125 use_operand_p use_p;
126 imm_use_iterator iter;
127 bitmap b;
129 // Only tracked for pointers.
130 if (!POINTER_TYPE_P (TREE_TYPE (name)))
131 return;
133 // Already processed if a bitmap has been allocated.
134 if (m_nn[v])
135 return;
137 b = BITMAP_ALLOC (&m_bitmaps);
139 // Loop over each immediate use and see if it implies a non-null value.
140 FOR_EACH_IMM_USE_FAST (use_p, iter, name)
142 gimple *s = USE_STMT (use_p);
143 unsigned index = gimple_bb (s)->index;
145 // If bit is already set for this block, dont bother looking again.
146 if (bitmap_bit_p (b, index))
147 continue;
149 // If we can infer a nonnull range, then set the bit for this BB
150 if (!SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name)
151 && infer_nonnull_range (s, name))
152 bitmap_set_bit (b, index);
155 m_nn[v] = b;
158 // -------------------------------------------------------------------------
160 // This class represents the API into a cache of ranges for an SSA_NAME.
161 // Routines must be implemented to set, get, and query if a value is set.
163 class ssa_block_ranges
165 public:
166 virtual bool set_bb_range (const_basic_block bb, const irange &r) = 0;
167 virtual bool get_bb_range (irange &r, const_basic_block bb) = 0;
168 virtual bool bb_range_p (const_basic_block bb) = 0;
170 void dump(FILE *f);
173 // Print the list of known ranges for file F in a nice format.
175 void
176 ssa_block_ranges::dump (FILE *f)
178 basic_block bb;
179 int_range_max r;
181 FOR_EACH_BB_FN (bb, cfun)
182 if (get_bb_range (r, bb))
184 fprintf (f, "BB%d -> ", bb->index);
185 r.dump (f);
186 fprintf (f, "\n");
190 // This class implements the range cache as a linear vector, indexed by BB.
191 // It caches a varying and undefined range which are used instead of
192 // allocating new ones each time.
194 class sbr_vector : public ssa_block_ranges
196 public:
197 sbr_vector (tree t, irange_allocator *allocator);
199 virtual bool set_bb_range (const_basic_block bb, const irange &r) OVERRIDE;
200 virtual bool get_bb_range (irange &r, const_basic_block bb) OVERRIDE;
201 virtual bool bb_range_p (const_basic_block bb) OVERRIDE;
202 protected:
203 irange **m_tab; // Non growing vector.
204 int m_tab_size;
205 int_range<2> m_varying;
206 int_range<2> m_undefined;
207 tree m_type;
208 irange_allocator *m_irange_allocator;
212 // Initialize a block cache for an ssa_name of type T.
214 sbr_vector::sbr_vector (tree t, irange_allocator *allocator)
216 gcc_checking_assert (TYPE_P (t));
217 m_type = t;
218 m_irange_allocator = allocator;
219 m_tab_size = last_basic_block_for_fn (cfun) + 1;
220 m_tab = (irange **)allocator->get_memory (m_tab_size * sizeof (irange *));
221 memset (m_tab, 0, m_tab_size * sizeof (irange *));
223 // Create the cached type range.
224 m_varying.set_varying (t);
225 m_undefined.set_undefined ();
228 // Set the range for block BB to be R.
230 bool
231 sbr_vector::set_bb_range (const_basic_block bb, const irange &r)
233 irange *m;
234 gcc_checking_assert (bb->index < m_tab_size);
235 if (r.varying_p ())
236 m = &m_varying;
237 else if (r.undefined_p ())
238 m = &m_undefined;
239 else
240 m = m_irange_allocator->allocate (r);
241 m_tab[bb->index] = m;
242 return true;
245 // Return the range associated with block BB in R. Return false if
246 // there is no range.
248 bool
249 sbr_vector::get_bb_range (irange &r, const_basic_block bb)
251 gcc_checking_assert (bb->index < m_tab_size);
252 irange *m = m_tab[bb->index];
253 if (m)
255 r = *m;
256 return true;
258 return false;
261 // Return true if a range is present.
263 bool
264 sbr_vector::bb_range_p (const_basic_block bb)
266 gcc_checking_assert (bb->index < m_tab_size);
267 return m_tab[bb->index] != NULL;
270 // This class implements the on entry cache via a sparse bitmap.
271 // It uses the quad bit routines to access 4 bits at a time.
272 // A value of 0 (the default) means there is no entry, and a value of
273 // 1 thru SBR_NUM represents an element in the m_range vector.
274 // Varying is given the first value (1) and pre-cached.
275 // SBR_NUM + 1 represents the value of UNDEFINED, and is never stored.
276 // SBR_NUM is the number of values that can be cached.
277 // Indexes are 1..SBR_NUM and are stored locally at m_range[0..SBR_NUM-1]
279 #define SBR_NUM 14
280 #define SBR_UNDEF SBR_NUM + 1
281 #define SBR_VARYING 1
283 class sbr_sparse_bitmap : public ssa_block_ranges
285 public:
286 sbr_sparse_bitmap (tree t, irange_allocator *allocator, bitmap_obstack *bm);
287 virtual bool set_bb_range (const_basic_block bb, const irange &r) OVERRIDE;
288 virtual bool get_bb_range (irange &r, const_basic_block bb) OVERRIDE;
289 virtual bool bb_range_p (const_basic_block bb) OVERRIDE;
290 private:
291 void bitmap_set_quad (bitmap head, int quad, int quad_value);
292 int bitmap_get_quad (const_bitmap head, int quad);
293 irange_allocator *m_irange_allocator;
294 irange *m_range[SBR_NUM];
295 bitmap bitvec;
296 tree m_type;
299 // Initialize a block cache for an ssa_name of type T.
301 sbr_sparse_bitmap::sbr_sparse_bitmap (tree t, irange_allocator *allocator,
302 bitmap_obstack *bm)
304 gcc_checking_assert (TYPE_P (t));
305 m_type = t;
306 bitvec = BITMAP_ALLOC (bm);
307 m_irange_allocator = allocator;
308 // Pre-cache varying.
309 m_range[0] = m_irange_allocator->allocate (2);
310 m_range[0]->set_varying (t);
311 // Pre-cache zero and non-zero values for pointers.
312 if (POINTER_TYPE_P (t))
314 m_range[1] = m_irange_allocator->allocate (2);
315 m_range[1]->set_nonzero (t);
316 m_range[2] = m_irange_allocator->allocate (2);
317 m_range[2]->set_zero (t);
319 else
320 m_range[1] = m_range[2] = NULL;
321 // Clear SBR_NUM entries.
322 for (int x = 3; x < SBR_NUM; x++)
323 m_range[x] = 0;
326 // Set 4 bit values in a sparse bitmap. This allows a bitmap to
327 // function as a sparse array of 4 bit values.
328 // QUAD is the index, QUAD_VALUE is the 4 bit value to set.
330 inline void
331 sbr_sparse_bitmap::bitmap_set_quad (bitmap head, int quad, int quad_value)
333 bitmap_set_aligned_chunk (head, quad, 4, (BITMAP_WORD) quad_value);
336 // Get a 4 bit value from a sparse bitmap. This allows a bitmap to
337 // function as a sparse array of 4 bit values.
338 // QUAD is the index.
339 inline int
340 sbr_sparse_bitmap::bitmap_get_quad (const_bitmap head, int quad)
342 return (int) bitmap_get_aligned_chunk (head, quad, 4);
345 // Set the range on entry to basic block BB to R.
347 bool
348 sbr_sparse_bitmap::set_bb_range (const_basic_block bb, const irange &r)
350 if (r.undefined_p ())
352 bitmap_set_quad (bitvec, bb->index, SBR_UNDEF);
353 return true;
356 // Loop thru the values to see if R is already present.
357 for (int x = 0; x < SBR_NUM; x++)
358 if (!m_range[x] || r == *(m_range[x]))
360 if (!m_range[x])
361 m_range[x] = m_irange_allocator->allocate (r);
362 bitmap_set_quad (bitvec, bb->index, x + 1);
363 return true;
365 // All values are taken, default to VARYING.
366 bitmap_set_quad (bitvec, bb->index, SBR_VARYING);
367 return false;
370 // Return the range associated with block BB in R. Return false if
371 // there is no range.
373 bool
374 sbr_sparse_bitmap::get_bb_range (irange &r, const_basic_block bb)
376 int value = bitmap_get_quad (bitvec, bb->index);
378 if (!value)
379 return false;
381 gcc_checking_assert (value <= SBR_UNDEF);
382 if (value == SBR_UNDEF)
383 r.set_undefined ();
384 else
385 r = *(m_range[value - 1]);
386 return true;
389 // Return true if a range is present.
391 bool
392 sbr_sparse_bitmap::bb_range_p (const_basic_block bb)
394 return (bitmap_get_quad (bitvec, bb->index) != 0);
397 // -------------------------------------------------------------------------
399 // Initialize the block cache.
401 block_range_cache::block_range_cache ()
403 bitmap_obstack_initialize (&m_bitmaps);
404 m_ssa_ranges.create (0);
405 m_ssa_ranges.safe_grow_cleared (num_ssa_names);
406 m_irange_allocator = new irange_allocator;
409 // Remove any m_block_caches which have been created.
411 block_range_cache::~block_range_cache ()
413 delete m_irange_allocator;
414 // Release the vector itself.
415 m_ssa_ranges.release ();
416 bitmap_obstack_release (&m_bitmaps);
419 // Set the range for NAME on entry to block BB to R.
420 // If it has not been accessed yet, allocate it first.
422 bool
423 block_range_cache::set_bb_range (tree name, const_basic_block bb,
424 const irange &r)
426 unsigned v = SSA_NAME_VERSION (name);
427 if (v >= m_ssa_ranges.length ())
428 m_ssa_ranges.safe_grow_cleared (num_ssa_names + 1);
430 if (!m_ssa_ranges[v])
432 // Use sparse representation if there are too many basic blocks.
433 if (last_basic_block_for_fn (cfun) > param_evrp_sparse_threshold)
435 void *r = m_irange_allocator->get_memory (sizeof (sbr_sparse_bitmap));
436 m_ssa_ranges[v] = new (r) sbr_sparse_bitmap (TREE_TYPE (name),
437 m_irange_allocator,
438 &m_bitmaps);
440 else
442 // Otherwise use the default vector implemntation.
443 void *r = m_irange_allocator->get_memory (sizeof (sbr_vector));
444 m_ssa_ranges[v] = new (r) sbr_vector (TREE_TYPE (name),
445 m_irange_allocator);
448 return m_ssa_ranges[v]->set_bb_range (bb, r);
452 // Return a pointer to the ssa_block_cache for NAME. If it has not been
453 // accessed yet, return NULL.
455 inline ssa_block_ranges *
456 block_range_cache::query_block_ranges (tree name)
458 unsigned v = SSA_NAME_VERSION (name);
459 if (v >= m_ssa_ranges.length () || !m_ssa_ranges[v])
460 return NULL;
461 return m_ssa_ranges[v];
466 // Return the range for NAME on entry to BB in R. Return true if there
467 // is one.
469 bool
470 block_range_cache::get_bb_range (irange &r, tree name, const_basic_block bb)
472 ssa_block_ranges *ptr = query_block_ranges (name);
473 if (ptr)
474 return ptr->get_bb_range (r, bb);
475 return false;
478 // Return true if NAME has a range set in block BB.
480 bool
481 block_range_cache::bb_range_p (tree name, const_basic_block bb)
483 ssa_block_ranges *ptr = query_block_ranges (name);
484 if (ptr)
485 return ptr->bb_range_p (bb);
486 return false;
489 // Print all known block caches to file F.
491 void
492 block_range_cache::dump (FILE *f)
494 unsigned x;
495 for (x = 0; x < m_ssa_ranges.length (); ++x)
497 if (m_ssa_ranges[x])
499 fprintf (f, " Ranges for ");
500 print_generic_expr (f, ssa_name (x), TDF_NONE);
501 fprintf (f, ":\n");
502 m_ssa_ranges[x]->dump (f);
503 fprintf (f, "\n");
508 // Print all known ranges on entry to blobk BB to file F.
510 void
511 block_range_cache::dump (FILE *f, basic_block bb, bool print_varying)
513 unsigned x;
514 int_range_max r;
515 bool summarize_varying = false;
516 for (x = 1; x < m_ssa_ranges.length (); ++x)
518 if (!gimple_range_ssa_p (ssa_name (x)))
519 continue;
520 if (m_ssa_ranges[x] && m_ssa_ranges[x]->get_bb_range (r, bb))
522 if (!print_varying && r.varying_p ())
524 summarize_varying = true;
525 continue;
527 print_generic_expr (f, ssa_name (x), TDF_NONE);
528 fprintf (f, "\t");
529 r.dump(f);
530 fprintf (f, "\n");
533 // If there were any varying entries, lump them all together.
534 if (summarize_varying)
536 fprintf (f, "VARYING_P on entry : ");
537 for (x = 1; x < num_ssa_names; ++x)
539 if (!gimple_range_ssa_p (ssa_name (x)))
540 continue;
541 if (m_ssa_ranges[x] && m_ssa_ranges[x]->get_bb_range (r, bb))
543 if (r.varying_p ())
545 print_generic_expr (f, ssa_name (x), TDF_NONE);
546 fprintf (f, " ");
550 fprintf (f, "\n");
554 // -------------------------------------------------------------------------
556 // Initialize a global cache.
558 ssa_global_cache::ssa_global_cache ()
560 m_tab.create (0);
561 m_irange_allocator = new irange_allocator;
564 // Deconstruct a global cache.
566 ssa_global_cache::~ssa_global_cache ()
568 m_tab.release ();
569 delete m_irange_allocator;
572 // Retrieve the global range of NAME from cache memory if it exists.
573 // Return the value in R.
575 bool
576 ssa_global_cache::get_global_range (irange &r, tree name) const
578 unsigned v = SSA_NAME_VERSION (name);
579 if (v >= m_tab.length ())
580 return false;
582 irange *stow = m_tab[v];
583 if (!stow)
584 return false;
585 r = *stow;
586 return true;
589 // Set the range for NAME to R in the global cache.
590 // Return TRUE if there was already a range set, otherwise false.
592 bool
593 ssa_global_cache::set_global_range (tree name, const irange &r)
595 unsigned v = SSA_NAME_VERSION (name);
596 if (v >= m_tab.length ())
597 m_tab.safe_grow_cleared (num_ssa_names + 1);
599 irange *m = m_tab[v];
600 if (m && m->fits_p (r))
601 *m = r;
602 else
603 m_tab[v] = m_irange_allocator->allocate (r);
604 return m != NULL;
607 // Set the range for NAME to R in the glonbal cache.
609 void
610 ssa_global_cache::clear_global_range (tree name)
612 unsigned v = SSA_NAME_VERSION (name);
613 if (v >= m_tab.length ())
614 m_tab.safe_grow_cleared (num_ssa_names + 1);
615 m_tab[v] = NULL;
618 // Clear the global cache.
620 void
621 ssa_global_cache::clear ()
623 memset (m_tab.address(), 0, m_tab.length () * sizeof (irange *));
626 // Dump the contents of the global cache to F.
628 void
629 ssa_global_cache::dump (FILE *f)
631 unsigned x;
632 int_range_max r;
633 fprintf (f, "Non-varying global ranges:\n");
634 fprintf (f, "=========================:\n");
635 for ( x = 1; x < num_ssa_names; x++)
636 if (gimple_range_ssa_p (ssa_name (x)) &&
637 get_global_range (r, ssa_name (x)) && !r.varying_p ())
639 print_generic_expr (f, ssa_name (x), TDF_NONE);
640 fprintf (f, " : ");
641 r.dump (f);
642 fprintf (f, "\n");
644 fputc ('\n', f);
647 // --------------------------------------------------------------------------
650 // This class will manage the timestamps for each ssa_name.
651 // When a value is calculated, the timestamp is set to the current time.
652 // Current time is then incremented. Any dependencies will already have
653 // been calculated, and will thus have older timestamps.
654 // If one of those values is ever calculated again, it will get a newer
655 // timestamp, and the "current_p" check will fail.
657 class temporal_cache
659 public:
660 temporal_cache ();
661 ~temporal_cache ();
662 bool current_p (tree name, tree dep1, tree dep2) const;
663 void set_timestamp (tree name);
664 void set_always_current (tree name);
665 private:
666 unsigned temporal_value (unsigned ssa) const;
668 unsigned m_current_time;
669 vec <unsigned> m_timestamp;
672 inline
673 temporal_cache::temporal_cache ()
675 m_current_time = 1;
676 m_timestamp.create (0);
677 m_timestamp.safe_grow_cleared (num_ssa_names);
680 inline
681 temporal_cache::~temporal_cache ()
683 m_timestamp.release ();
686 // Return the timestamp value for SSA, or 0 if there isnt one.
688 inline unsigned
689 temporal_cache::temporal_value (unsigned ssa) const
691 if (ssa >= m_timestamp.length ())
692 return 0;
693 return m_timestamp[ssa];
696 // Return TRUE if the timestampe for NAME is newer than any of its dependents.
697 // Up to 2 dependencies can be checked.
699 bool
700 temporal_cache::current_p (tree name, tree dep1, tree dep2) const
702 unsigned ts = temporal_value (SSA_NAME_VERSION (name));
703 if (ts == 0)
704 return true;
706 // Any non-registered dependencies will have a value of 0 and thus be older.
707 // Return true if time is newer than either dependent.
709 if (dep1 && ts < temporal_value (SSA_NAME_VERSION (dep1)))
710 return false;
711 if (dep2 && ts < temporal_value (SSA_NAME_VERSION (dep2)))
712 return false;
714 return true;
717 // This increments the global timer and sets the timestamp for NAME.
719 inline void
720 temporal_cache::set_timestamp (tree name)
722 unsigned v = SSA_NAME_VERSION (name);
723 if (v >= m_timestamp.length ())
724 m_timestamp.safe_grow_cleared (num_ssa_names + 20);
725 m_timestamp[v] = ++m_current_time;
728 // Set the timestamp to 0, marking it as "always up to date".
730 inline void
731 temporal_cache::set_always_current (tree name)
733 unsigned v = SSA_NAME_VERSION (name);
734 if (v >= m_timestamp.length ())
735 m_timestamp.safe_grow_cleared (num_ssa_names + 20);
736 m_timestamp[v] = 0;
739 // --------------------------------------------------------------------------
741 ranger_cache::ranger_cache ()
743 m_workback.create (0);
744 m_workback.safe_grow_cleared (last_basic_block_for_fn (cfun));
745 m_update_list.create (0);
746 m_update_list.safe_grow_cleared (last_basic_block_for_fn (cfun));
747 m_update_list.truncate (0);
748 m_temporal = new temporal_cache;
749 // If DOM info is available, spawn an oracle as well.
750 if (dom_info_available_p (CDI_DOMINATORS))
751 m_oracle = new relation_oracle ();
752 else
753 m_oracle = NULL;
755 unsigned x, lim = last_basic_block_for_fn (cfun);
756 // Calculate outgoing range info upfront. This will fully populate the
757 // m_maybe_variant bitmap which will help eliminate processing of names
758 // which never have their ranges adjusted.
759 for (x = 0; x < lim ; x++)
761 basic_block bb = BASIC_BLOCK_FOR_FN (cfun, x);
762 if (bb)
763 m_gori.exports (bb);
765 m_propfail = BITMAP_ALLOC (NULL);
768 ranger_cache::~ranger_cache ()
770 BITMAP_FREE (m_propfail);
771 if (m_oracle)
772 delete m_oracle;
773 delete m_temporal;
774 m_workback.release ();
775 m_update_list.release ();
778 // Dump the global caches to file F. if GORI_DUMP is true, dump the
779 // gori map as well.
781 void
782 ranger_cache::dump (FILE *f)
784 m_globals.dump (f);
785 fprintf (f, "\n");
788 // Dump the caches for basic block BB to file F.
790 void
791 ranger_cache::dump_bb (FILE *f, basic_block bb)
793 m_gori.gori_map::dump (f, bb, false);
794 m_on_entry.dump (f, bb);
795 if (m_oracle)
796 m_oracle->dump (f, bb);
799 // Get the global range for NAME, and return in R. Return false if the
800 // global range is not set.
802 bool
803 ranger_cache::get_global_range (irange &r, tree name) const
805 return m_globals.get_global_range (r, name);
808 // Get the global range for NAME, and return in R if the value is not stale.
809 // If the range is set, but is stale, mark it current and return false.
810 // If it is not set pick up the legacy global value, mark it current, and
811 // return false.
812 // Note there is always a value returned in R. The return value indicates
813 // whether that value is an up-to-date calculated value or not..
815 bool
816 ranger_cache::get_non_stale_global_range (irange &r, tree name)
818 if (m_globals.get_global_range (r, name))
820 // Use this value if the range is constant or current.
821 if (r.singleton_p ()
822 || m_temporal->current_p (name, m_gori.depend1 (name),
823 m_gori.depend2 (name)))
824 return true;
826 else
828 // Global has never been accessed, so pickup the legacy global value.
829 r = gimple_range_global (name);
830 m_globals.set_global_range (name, r);
832 // After a stale check failure, mark the value as always current until a
833 // new one is set.
834 m_temporal->set_always_current (name);
835 return false;
837 // Set the global range of NAME to R.
839 void
840 ranger_cache::set_global_range (tree name, const irange &r)
842 if (m_globals.set_global_range (name, r))
844 // If there was already a range set, propagate the new value.
845 basic_block bb = gimple_bb (SSA_NAME_DEF_STMT (name));
846 if (!bb)
847 bb = ENTRY_BLOCK_PTR_FOR_FN (cfun);
849 if (DEBUG_RANGE_CACHE)
850 fprintf (dump_file, " GLOBAL :");
852 propagate_updated_value (name, bb);
854 // Constants no longer need to tracked. Any further refinement has to be
855 // undefined. Propagation works better with constants. PR 100512.
856 // Pointers which resolve to non-zero also do not need
857 // tracking in the cache as they will never change. See PR 98866.
858 // Timestamp must always be updated, or dependent calculations may
859 // not include this latest value. PR 100774.
861 if (r.singleton_p ()
862 || (POINTER_TYPE_P (TREE_TYPE (name)) && r.nonzero_p ()))
863 m_gori.set_range_invariant (name);
864 m_temporal->set_timestamp (name);
867 // Provide lookup for the gori-computes class to access the best known range
868 // of an ssa_name in any given basic block. Note, this does no additonal
869 // lookups, just accesses the data that is already known.
871 // Get the range of NAME when the def occurs in block BB. If BB is NULL
872 // get the best global value available.
874 void
875 ranger_cache::range_of_def (irange &r, tree name, basic_block bb)
877 gcc_checking_assert (gimple_range_ssa_p (name));
878 gcc_checking_assert (!bb || bb == gimple_bb (SSA_NAME_DEF_STMT (name)));
880 // Pick up the best global range available.
881 if (!m_globals.get_global_range (r, name))
883 // If that fails, try to calculate the range using just global values.
884 gimple *s = SSA_NAME_DEF_STMT (name);
885 if (gimple_get_lhs (s) == name)
886 fold_range (r, s, get_global_range_query ());
887 else
888 r = gimple_range_global (name);
891 if (bb)
892 m_non_null.adjust_range (r, name, bb, false);
895 // Get the range of NAME as it occurs on entry to block BB.
897 void
898 ranger_cache::entry_range (irange &r, tree name, basic_block bb)
900 if (bb == ENTRY_BLOCK_PTR_FOR_FN (cfun))
902 r = gimple_range_global (name);
903 return;
906 // Look for the on-entry value of name in BB from the cache.
907 // Otherwise pick up the best available global value.
908 if (!m_on_entry.get_bb_range (r, name, bb))
909 range_of_def (r, name);
911 m_non_null.adjust_range (r, name, bb, false);
914 // Get the range of NAME as it occurs on exit from block BB.
916 void
917 ranger_cache::exit_range (irange &r, tree name, basic_block bb)
919 if (bb == ENTRY_BLOCK_PTR_FOR_FN (cfun))
921 r = gimple_range_global (name);
922 return;
925 gimple *s = SSA_NAME_DEF_STMT (name);
926 basic_block def_bb = gimple_bb (s);
927 if (def_bb == bb)
928 range_of_def (r, name, bb);
929 else
930 entry_range (r, name, bb);
934 // Implement range_of_expr.
936 bool
937 ranger_cache::range_of_expr (irange &r, tree name, gimple *stmt)
939 if (!gimple_range_ssa_p (name))
941 get_tree_range (r, name, stmt);
942 return true;
945 basic_block bb = gimple_bb (stmt);
946 gimple *def_stmt = SSA_NAME_DEF_STMT (name);
947 basic_block def_bb = gimple_bb (def_stmt);
949 if (bb == def_bb)
950 range_of_def (r, name, bb);
951 else
952 entry_range (r, name, bb);
953 return true;
957 // Implement range_on_edge. Always return the best available range.
959 bool
960 ranger_cache::range_on_edge (irange &r, edge e, tree expr)
962 if (gimple_range_ssa_p (expr))
964 exit_range (r, expr, e->src);
965 int_range_max edge_range;
966 if (m_gori.outgoing_edge_range_p (edge_range, e, expr, *this))
967 r.intersect (edge_range);
968 return true;
971 return get_tree_range (r, expr, NULL);
975 // Return a static range for NAME on entry to basic block BB in R. If
976 // calc is true, fill any cache entries required between BB and the
977 // def block for NAME. Otherwise, return false if the cache is empty.
979 bool
980 ranger_cache::block_range (irange &r, basic_block bb, tree name, bool calc)
982 gcc_checking_assert (gimple_range_ssa_p (name));
984 // If there are no range calculations anywhere in the IL, global range
985 // applies everywhere, so don't bother caching it.
986 if (!m_gori.has_edge_range_p (name))
987 return false;
989 if (calc)
991 gimple *def_stmt = SSA_NAME_DEF_STMT (name);
992 basic_block def_bb = NULL;
993 if (def_stmt)
994 def_bb = gimple_bb (def_stmt);;
995 if (!def_bb)
997 // If we get to the entry block, this better be a default def
998 // or range_on_entry was called for a block not dominated by
999 // the def.
1000 gcc_checking_assert (SSA_NAME_IS_DEFAULT_DEF (name));
1001 def_bb = ENTRY_BLOCK_PTR_FOR_FN (cfun);
1004 // There is no range on entry for the definition block.
1005 if (def_bb == bb)
1006 return false;
1008 // Otherwise, go figure out what is known in predecessor blocks.
1009 fill_block_cache (name, bb, def_bb);
1010 gcc_checking_assert (m_on_entry.bb_range_p (name, bb));
1012 return m_on_entry.get_bb_range (r, name, bb);
1015 // Add BB to the list of blocks to update, unless it's already in the list.
1017 void
1018 ranger_cache::add_to_update (basic_block bb)
1020 // If propagation has failed for BB, or its already in the list, don't
1021 // add it again.
1022 if (!bitmap_bit_p (m_propfail, bb->index) && !m_update_list.contains (bb))
1023 m_update_list.quick_push (bb);
1026 // If there is anything in the propagation update_list, continue
1027 // processing NAME until the list of blocks is empty.
1029 void
1030 ranger_cache::propagate_cache (tree name)
1032 basic_block bb;
1033 edge_iterator ei;
1034 edge e;
1035 int_range_max new_range;
1036 int_range_max current_range;
1037 int_range_max e_range;
1039 gcc_checking_assert (bitmap_empty_p (m_propfail));
1040 // Process each block by seeing if its calculated range on entry is
1041 // the same as its cached value. If there is a difference, update
1042 // the cache to reflect the new value, and check to see if any
1043 // successors have cache entries which may need to be checked for
1044 // updates.
1046 while (m_update_list.length () > 0)
1048 bb = m_update_list.pop ();
1049 gcc_checking_assert (m_on_entry.bb_range_p (name, bb));
1050 m_on_entry.get_bb_range (current_range, name, bb);
1052 if (DEBUG_RANGE_CACHE)
1054 fprintf (dump_file, "FWD visiting block %d for ", bb->index);
1055 print_generic_expr (dump_file, name, TDF_SLIM);
1056 fprintf (dump_file, " starting range : ");
1057 current_range.dump (dump_file);
1058 fprintf (dump_file, "\n");
1061 // Calculate the "new" range on entry by unioning the pred edges.
1062 new_range.set_undefined ();
1063 FOR_EACH_EDGE (e, ei, bb->preds)
1065 range_on_edge (e_range, e, name);
1066 if (DEBUG_RANGE_CACHE)
1068 fprintf (dump_file, " edge %d->%d :", e->src->index, bb->index);
1069 e_range.dump (dump_file);
1070 fprintf (dump_file, "\n");
1072 new_range.union_ (e_range);
1073 if (new_range.varying_p ())
1074 break;
1077 // If the range on entry has changed, update it.
1078 if (new_range != current_range)
1080 bool ok_p = m_on_entry.set_bb_range (name, bb, new_range);
1081 // If the cache couldn't set the value, mark it as failed.
1082 if (!ok_p)
1083 bitmap_set_bit (m_propfail, bb->index);
1084 if (DEBUG_RANGE_CACHE)
1086 if (!ok_p)
1088 fprintf (dump_file, " Cache failure to store value:");
1089 print_generic_expr (dump_file, name, TDF_SLIM);
1090 fprintf (dump_file, " ");
1092 else
1094 fprintf (dump_file, " Updating range to ");
1095 new_range.dump (dump_file);
1097 fprintf (dump_file, "\n Updating blocks :");
1099 // Mark each successor that has a range to re-check its range
1100 FOR_EACH_EDGE (e, ei, bb->succs)
1101 if (m_on_entry.bb_range_p (name, e->dest))
1103 if (DEBUG_RANGE_CACHE)
1104 fprintf (dump_file, " bb%d",e->dest->index);
1105 add_to_update (e->dest);
1107 if (DEBUG_RANGE_CACHE)
1108 fprintf (dump_file, "\n");
1111 if (DEBUG_RANGE_CACHE)
1113 fprintf (dump_file, "DONE visiting blocks for ");
1114 print_generic_expr (dump_file, name, TDF_SLIM);
1115 fprintf (dump_file, "\n");
1117 bitmap_clear (m_propfail);
1120 // Check to see if an update to the value for NAME in BB has any effect
1121 // on values already in the on-entry cache for successor blocks.
1122 // If it does, update them. Don't visit any blocks which dont have a cache
1123 // entry.
1125 void
1126 ranger_cache::propagate_updated_value (tree name, basic_block bb)
1128 edge e;
1129 edge_iterator ei;
1131 // The update work list should be empty at this point.
1132 gcc_checking_assert (m_update_list.length () == 0);
1133 gcc_checking_assert (bb);
1135 if (DEBUG_RANGE_CACHE)
1137 fprintf (dump_file, " UPDATE cache for ");
1138 print_generic_expr (dump_file, name, TDF_SLIM);
1139 fprintf (dump_file, " in BB %d : successors : ", bb->index);
1141 FOR_EACH_EDGE (e, ei, bb->succs)
1143 // Only update active cache entries.
1144 if (m_on_entry.bb_range_p (name, e->dest))
1146 add_to_update (e->dest);
1147 if (DEBUG_RANGE_CACHE)
1148 fprintf (dump_file, " UPDATE: bb%d", e->dest->index);
1151 if (m_update_list.length () != 0)
1153 if (DEBUG_RANGE_CACHE)
1154 fprintf (dump_file, "\n");
1155 propagate_cache (name);
1157 else
1159 if (DEBUG_RANGE_CACHE)
1160 fprintf (dump_file, " : No updates!\n");
1164 // Make sure that the range-on-entry cache for NAME is set for block BB.
1165 // Work back through the CFG to DEF_BB ensuring the range is calculated
1166 // on the block/edges leading back to that point.
1168 void
1169 ranger_cache::fill_block_cache (tree name, basic_block bb, basic_block def_bb)
1171 edge_iterator ei;
1172 edge e;
1173 int_range_max block_result;
1174 int_range_max undefined;
1176 // At this point we shouldn't be looking at the def, entry or exit block.
1177 gcc_checking_assert (bb != def_bb && bb != ENTRY_BLOCK_PTR_FOR_FN (cfun) &&
1178 bb != EXIT_BLOCK_PTR_FOR_FN (cfun));
1180 // If the block cache is set, then we've already visited this block.
1181 if (m_on_entry.bb_range_p (name, bb))
1182 return;
1184 // Visit each block back to the DEF. Initialize each one to UNDEFINED.
1185 // m_visited at the end will contain all the blocks that we needed to set
1186 // the range_on_entry cache for.
1187 m_workback.truncate (0);
1188 m_workback.quick_push (bb);
1189 undefined.set_undefined ();
1190 m_on_entry.set_bb_range (name, bb, undefined);
1191 gcc_checking_assert (m_update_list.length () == 0);
1193 if (DEBUG_RANGE_CACHE)
1195 fprintf (dump_file, "\n");
1196 print_generic_expr (dump_file, name, TDF_SLIM);
1197 fprintf (dump_file, " : ");
1200 while (m_workback.length () > 0)
1202 basic_block node = m_workback.pop ();
1203 if (DEBUG_RANGE_CACHE)
1205 fprintf (dump_file, "BACK visiting block %d for ", node->index);
1206 print_generic_expr (dump_file, name, TDF_SLIM);
1207 fprintf (dump_file, "\n");
1210 FOR_EACH_EDGE (e, ei, node->preds)
1212 basic_block pred = e->src;
1213 int_range_max r;
1215 if (DEBUG_RANGE_CACHE)
1216 fprintf (dump_file, " %d->%d ",e->src->index, e->dest->index);
1218 // If the pred block is the def block add this BB to update list.
1219 if (pred == def_bb)
1221 add_to_update (node);
1222 continue;
1225 // If the pred is entry but NOT def, then it is used before
1226 // defined, it'll get set to [] and no need to update it.
1227 if (pred == ENTRY_BLOCK_PTR_FOR_FN (cfun))
1229 if (DEBUG_RANGE_CACHE)
1230 fprintf (dump_file, "entry: bail.");
1231 continue;
1234 // Regardless of whether we have visited pred or not, if the
1235 // pred has a non-null reference, revisit this block.
1236 // Don't search the DOM tree.
1237 if (m_non_null.non_null_deref_p (name, pred, false))
1239 if (DEBUG_RANGE_CACHE)
1240 fprintf (dump_file, "nonnull: update ");
1241 add_to_update (node);
1244 // If the pred block already has a range, or if it can contribute
1245 // something new. Ie, the edge generates a range of some sort.
1246 if (m_on_entry.get_bb_range (r, name, pred))
1248 if (DEBUG_RANGE_CACHE)
1250 fprintf (dump_file, "has cache, ");
1251 r.dump (dump_file);
1252 fprintf (dump_file, ", ");
1254 if (!r.undefined_p () || m_gori.has_edge_range_p (name, e))
1256 add_to_update (node);
1257 if (DEBUG_RANGE_CACHE)
1258 fprintf (dump_file, "update. ");
1260 continue;
1263 if (DEBUG_RANGE_CACHE)
1264 fprintf (dump_file, "pushing undefined pred block.\n");
1265 // If the pred hasn't been visited (has no range), add it to
1266 // the list.
1267 gcc_checking_assert (!m_on_entry.bb_range_p (name, pred));
1268 m_on_entry.set_bb_range (name, pred, undefined);
1269 m_workback.quick_push (pred);
1273 if (DEBUG_RANGE_CACHE)
1274 fprintf (dump_file, "\n");
1276 // Now fill in the marked blocks with values.
1277 propagate_cache (name);
1278 if (DEBUG_RANGE_CACHE)
1279 fprintf (dump_file, " Propagation update done.\n");