Skip -fwhole-program when merging LTO options.
[official-gcc.git] / gcc / gimple-range-cache.cc
blob9848d140242f7137325bbb8df06f78e70b31063e
1 /* Gimple ranger SSA cache implementation.
2 Copyright (C) 2017-2022 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 "value-range-storage.h"
32 #include "tree-cfg.h"
33 #include "target.h"
34 #include "attribs.h"
35 #include "gimple-iterator.h"
36 #include "gimple-walk.h"
37 #include "cfganal.h"
39 #define DEBUG_RANGE_CACHE (dump_file \
40 && (param_ranger_debug & RANGER_DEBUG_CACHE))
42 // This class represents the API into a cache of ranges for an SSA_NAME.
43 // Routines must be implemented to set, get, and query if a value is set.
45 class ssa_block_ranges
47 public:
48 ssa_block_ranges (tree t) : m_type (t) { }
49 virtual bool set_bb_range (const_basic_block bb, const vrange &r) = 0;
50 virtual bool get_bb_range (vrange &r, const_basic_block bb) = 0;
51 virtual bool bb_range_p (const_basic_block bb) = 0;
53 void dump(FILE *f);
54 private:
55 tree m_type;
58 // Print the list of known ranges for file F in a nice format.
60 void
61 ssa_block_ranges::dump (FILE *f)
63 basic_block bb;
64 Value_Range r (m_type);
66 FOR_EACH_BB_FN (bb, cfun)
67 if (get_bb_range (r, bb))
69 fprintf (f, "BB%d -> ", bb->index);
70 r.dump (f);
71 fprintf (f, "\n");
75 // This class implements the range cache as a linear vector, indexed by BB.
76 // It caches a varying and undefined range which are used instead of
77 // allocating new ones each time.
79 class sbr_vector : public ssa_block_ranges
81 public:
82 sbr_vector (tree t, vrange_allocator *allocator);
84 virtual bool set_bb_range (const_basic_block bb, const vrange &r) override;
85 virtual bool get_bb_range (vrange &r, const_basic_block bb) override;
86 virtual bool bb_range_p (const_basic_block bb) override;
87 protected:
88 vrange **m_tab; // Non growing vector.
89 int m_tab_size;
90 vrange *m_varying;
91 vrange *m_undefined;
92 tree m_type;
93 vrange_allocator *m_range_allocator;
94 void grow ();
98 // Initialize a block cache for an ssa_name of type T.
100 sbr_vector::sbr_vector (tree t, vrange_allocator *allocator)
101 : ssa_block_ranges (t)
103 gcc_checking_assert (TYPE_P (t));
104 m_type = t;
105 m_range_allocator = allocator;
106 m_tab_size = last_basic_block_for_fn (cfun) + 1;
107 m_tab = static_cast <vrange **>
108 (allocator->alloc (m_tab_size * sizeof (vrange *)));
109 memset (m_tab, 0, m_tab_size * sizeof (vrange *));
111 // Create the cached type range.
112 m_varying = m_range_allocator->alloc_vrange (t);
113 m_undefined = m_range_allocator->alloc_vrange (t);
114 m_varying->set_varying (t);
115 m_undefined->set_undefined ();
118 // Grow the vector when the CFG has increased in size.
120 void
121 sbr_vector::grow ()
123 int curr_bb_size = last_basic_block_for_fn (cfun);
124 gcc_checking_assert (curr_bb_size > m_tab_size);
126 // Increase the max of a)128, b)needed increase * 2, c)10% of current_size.
127 int inc = MAX ((curr_bb_size - m_tab_size) * 2, 128);
128 inc = MAX (inc, curr_bb_size / 10);
129 int new_size = inc + curr_bb_size;
131 // Allocate new memory, copy the old vector and clear the new space.
132 vrange **t = static_cast <vrange **>
133 (m_range_allocator->alloc (new_size * sizeof (vrange *)));
134 memcpy (t, m_tab, m_tab_size * sizeof (vrange *));
135 memset (t + m_tab_size, 0, (new_size - m_tab_size) * sizeof (vrange *));
137 m_tab = t;
138 m_tab_size = new_size;
141 // Set the range for block BB to be R.
143 bool
144 sbr_vector::set_bb_range (const_basic_block bb, const vrange &r)
146 vrange *m;
147 if (bb->index >= m_tab_size)
148 grow ();
149 if (r.varying_p ())
150 m = m_varying;
151 else if (r.undefined_p ())
152 m = m_undefined;
153 else
154 m = m_range_allocator->clone (r);
155 m_tab[bb->index] = m;
156 return true;
159 // Return the range associated with block BB in R. Return false if
160 // there is no range.
162 bool
163 sbr_vector::get_bb_range (vrange &r, const_basic_block bb)
165 if (bb->index >= m_tab_size)
166 return false;
167 vrange *m = m_tab[bb->index];
168 if (m)
170 r = *m;
171 return true;
173 return false;
176 // Return true if a range is present.
178 bool
179 sbr_vector::bb_range_p (const_basic_block bb)
181 if (bb->index < m_tab_size)
182 return m_tab[bb->index] != NULL;
183 return false;
186 // This class implements the on entry cache via a sparse bitmap.
187 // It uses the quad bit routines to access 4 bits at a time.
188 // A value of 0 (the default) means there is no entry, and a value of
189 // 1 thru SBR_NUM represents an element in the m_range vector.
190 // Varying is given the first value (1) and pre-cached.
191 // SBR_NUM + 1 represents the value of UNDEFINED, and is never stored.
192 // SBR_NUM is the number of values that can be cached.
193 // Indexes are 1..SBR_NUM and are stored locally at m_range[0..SBR_NUM-1]
195 #define SBR_NUM 14
196 #define SBR_UNDEF SBR_NUM + 1
197 #define SBR_VARYING 1
199 class sbr_sparse_bitmap : public ssa_block_ranges
201 public:
202 sbr_sparse_bitmap (tree t, vrange_allocator *allocator, bitmap_obstack *bm);
203 virtual bool set_bb_range (const_basic_block bb, const vrange &r) override;
204 virtual bool get_bb_range (vrange &r, const_basic_block bb) override;
205 virtual bool bb_range_p (const_basic_block bb) override;
206 private:
207 void bitmap_set_quad (bitmap head, int quad, int quad_value);
208 int bitmap_get_quad (const_bitmap head, int quad);
209 vrange_allocator *m_range_allocator;
210 vrange *m_range[SBR_NUM];
211 bitmap_head bitvec;
212 tree m_type;
215 // Initialize a block cache for an ssa_name of type T.
217 sbr_sparse_bitmap::sbr_sparse_bitmap (tree t, vrange_allocator *allocator,
218 bitmap_obstack *bm)
219 : ssa_block_ranges (t)
221 gcc_checking_assert (TYPE_P (t));
222 m_type = t;
223 bitmap_initialize (&bitvec, bm);
224 bitmap_tree_view (&bitvec);
225 m_range_allocator = allocator;
226 // Pre-cache varying.
227 m_range[0] = m_range_allocator->alloc_vrange (t);
228 m_range[0]->set_varying (t);
229 // Pre-cache zero and non-zero values for pointers.
230 if (POINTER_TYPE_P (t))
232 m_range[1] = m_range_allocator->alloc_vrange (t);
233 m_range[1]->set_nonzero (t);
234 m_range[2] = m_range_allocator->alloc_vrange (t);
235 m_range[2]->set_zero (t);
237 else
238 m_range[1] = m_range[2] = NULL;
239 // Clear SBR_NUM entries.
240 for (int x = 3; x < SBR_NUM; x++)
241 m_range[x] = 0;
244 // Set 4 bit values in a sparse bitmap. This allows a bitmap to
245 // function as a sparse array of 4 bit values.
246 // QUAD is the index, QUAD_VALUE is the 4 bit value to set.
248 inline void
249 sbr_sparse_bitmap::bitmap_set_quad (bitmap head, int quad, int quad_value)
251 bitmap_set_aligned_chunk (head, quad, 4, (BITMAP_WORD) quad_value);
254 // Get a 4 bit value from a sparse bitmap. This allows a bitmap to
255 // function as a sparse array of 4 bit values.
256 // QUAD is the index.
257 inline int
258 sbr_sparse_bitmap::bitmap_get_quad (const_bitmap head, int quad)
260 return (int) bitmap_get_aligned_chunk (head, quad, 4);
263 // Set the range on entry to basic block BB to R.
265 bool
266 sbr_sparse_bitmap::set_bb_range (const_basic_block bb, const vrange &r)
268 if (r.undefined_p ())
270 bitmap_set_quad (&bitvec, bb->index, SBR_UNDEF);
271 return true;
274 // Loop thru the values to see if R is already present.
275 for (int x = 0; x < SBR_NUM; x++)
276 if (!m_range[x] || r == *(m_range[x]))
278 if (!m_range[x])
279 m_range[x] = m_range_allocator->clone (r);
280 bitmap_set_quad (&bitvec, bb->index, x + 1);
281 return true;
283 // All values are taken, default to VARYING.
284 bitmap_set_quad (&bitvec, bb->index, SBR_VARYING);
285 return false;
288 // Return the range associated with block BB in R. Return false if
289 // there is no range.
291 bool
292 sbr_sparse_bitmap::get_bb_range (vrange &r, const_basic_block bb)
294 int value = bitmap_get_quad (&bitvec, bb->index);
296 if (!value)
297 return false;
299 gcc_checking_assert (value <= SBR_UNDEF);
300 if (value == SBR_UNDEF)
301 r.set_undefined ();
302 else
303 r = *(m_range[value - 1]);
304 return true;
307 // Return true if a range is present.
309 bool
310 sbr_sparse_bitmap::bb_range_p (const_basic_block bb)
312 return (bitmap_get_quad (&bitvec, bb->index) != 0);
315 // -------------------------------------------------------------------------
317 // Initialize the block cache.
319 block_range_cache::block_range_cache ()
321 bitmap_obstack_initialize (&m_bitmaps);
322 m_ssa_ranges.create (0);
323 m_ssa_ranges.safe_grow_cleared (num_ssa_names);
324 m_range_allocator = new obstack_vrange_allocator;
327 // Remove any m_block_caches which have been created.
329 block_range_cache::~block_range_cache ()
331 delete m_range_allocator;
332 // Release the vector itself.
333 m_ssa_ranges.release ();
334 bitmap_obstack_release (&m_bitmaps);
337 // Set the range for NAME on entry to block BB to R.
338 // If it has not been accessed yet, allocate it first.
340 bool
341 block_range_cache::set_bb_range (tree name, const_basic_block bb,
342 const vrange &r)
344 unsigned v = SSA_NAME_VERSION (name);
345 if (v >= m_ssa_ranges.length ())
346 m_ssa_ranges.safe_grow_cleared (num_ssa_names + 1);
348 if (!m_ssa_ranges[v])
350 // Use sparse representation if there are too many basic blocks.
351 if (last_basic_block_for_fn (cfun) > param_evrp_sparse_threshold)
353 void *r = m_range_allocator->alloc (sizeof (sbr_sparse_bitmap));
354 m_ssa_ranges[v] = new (r) sbr_sparse_bitmap (TREE_TYPE (name),
355 m_range_allocator,
356 &m_bitmaps);
358 else
360 // Otherwise use the default vector implemntation.
361 void *r = m_range_allocator->alloc (sizeof (sbr_vector));
362 m_ssa_ranges[v] = new (r) sbr_vector (TREE_TYPE (name),
363 m_range_allocator);
366 return m_ssa_ranges[v]->set_bb_range (bb, r);
370 // Return a pointer to the ssa_block_cache for NAME. If it has not been
371 // accessed yet, return NULL.
373 inline ssa_block_ranges *
374 block_range_cache::query_block_ranges (tree name)
376 unsigned v = SSA_NAME_VERSION (name);
377 if (v >= m_ssa_ranges.length () || !m_ssa_ranges[v])
378 return NULL;
379 return m_ssa_ranges[v];
384 // Return the range for NAME on entry to BB in R. Return true if there
385 // is one.
387 bool
388 block_range_cache::get_bb_range (vrange &r, tree name, const_basic_block bb)
390 ssa_block_ranges *ptr = query_block_ranges (name);
391 if (ptr)
392 return ptr->get_bb_range (r, bb);
393 return false;
396 // Return true if NAME has a range set in block BB.
398 bool
399 block_range_cache::bb_range_p (tree name, const_basic_block bb)
401 ssa_block_ranges *ptr = query_block_ranges (name);
402 if (ptr)
403 return ptr->bb_range_p (bb);
404 return false;
407 // Print all known block caches to file F.
409 void
410 block_range_cache::dump (FILE *f)
412 unsigned x;
413 for (x = 0; x < m_ssa_ranges.length (); ++x)
415 if (m_ssa_ranges[x])
417 fprintf (f, " Ranges for ");
418 print_generic_expr (f, ssa_name (x), TDF_NONE);
419 fprintf (f, ":\n");
420 m_ssa_ranges[x]->dump (f);
421 fprintf (f, "\n");
426 // Print all known ranges on entry to blobk BB to file F.
428 void
429 block_range_cache::dump (FILE *f, basic_block bb, bool print_varying)
431 unsigned x;
432 bool summarize_varying = false;
433 for (x = 1; x < m_ssa_ranges.length (); ++x)
435 if (!gimple_range_ssa_p (ssa_name (x)))
436 continue;
438 Value_Range r (TREE_TYPE (ssa_name (x)));
439 if (m_ssa_ranges[x] && m_ssa_ranges[x]->get_bb_range (r, bb))
441 if (!print_varying && r.varying_p ())
443 summarize_varying = true;
444 continue;
446 print_generic_expr (f, ssa_name (x), TDF_NONE);
447 fprintf (f, "\t");
448 r.dump(f);
449 fprintf (f, "\n");
452 // If there were any varying entries, lump them all together.
453 if (summarize_varying)
455 fprintf (f, "VARYING_P on entry : ");
456 for (x = 1; x < num_ssa_names; ++x)
458 if (!gimple_range_ssa_p (ssa_name (x)))
459 continue;
461 Value_Range r (TREE_TYPE (ssa_name (x)));
462 if (m_ssa_ranges[x] && m_ssa_ranges[x]->get_bb_range (r, bb))
464 if (r.varying_p ())
466 print_generic_expr (f, ssa_name (x), TDF_NONE);
467 fprintf (f, " ");
471 fprintf (f, "\n");
475 // -------------------------------------------------------------------------
477 // Initialize a global cache.
479 ssa_global_cache::ssa_global_cache ()
481 m_tab.create (0);
482 m_range_allocator = new obstack_vrange_allocator;
485 // Deconstruct a global cache.
487 ssa_global_cache::~ssa_global_cache ()
489 m_tab.release ();
490 delete m_range_allocator;
493 // Retrieve the global range of NAME from cache memory if it exists.
494 // Return the value in R.
496 bool
497 ssa_global_cache::get_global_range (vrange &r, tree name) const
499 unsigned v = SSA_NAME_VERSION (name);
500 if (v >= m_tab.length ())
501 return false;
503 vrange *stow = m_tab[v];
504 if (!stow)
505 return false;
506 r = *stow;
507 return true;
510 // Set the range for NAME to R in the global cache.
511 // Return TRUE if there was already a range set, otherwise false.
513 bool
514 ssa_global_cache::set_global_range (tree name, const vrange &r)
516 unsigned v = SSA_NAME_VERSION (name);
517 if (v >= m_tab.length ())
518 m_tab.safe_grow_cleared (num_ssa_names + 1);
520 vrange *m = m_tab[v];
521 if (m && m->fits_p (r))
522 *m = r;
523 else
524 m_tab[v] = m_range_allocator->clone (r);
525 return m != NULL;
528 // Set the range for NAME to R in the glonbal cache.
530 void
531 ssa_global_cache::clear_global_range (tree name)
533 unsigned v = SSA_NAME_VERSION (name);
534 if (v >= m_tab.length ())
535 m_tab.safe_grow_cleared (num_ssa_names + 1);
536 m_tab[v] = NULL;
539 // Clear the global cache.
541 void
542 ssa_global_cache::clear ()
544 if (m_tab.address ())
545 memset (m_tab.address(), 0, m_tab.length () * sizeof (vrange *));
548 // Dump the contents of the global cache to F.
550 void
551 ssa_global_cache::dump (FILE *f)
553 /* Cleared after the table header has been printed. */
554 bool print_header = true;
555 for (unsigned x = 1; x < num_ssa_names; x++)
557 if (!gimple_range_ssa_p (ssa_name (x)))
558 continue;
559 Value_Range r (TREE_TYPE (ssa_name (x)));
560 if (get_global_range (r, ssa_name (x)) && !r.varying_p ())
562 if (print_header)
564 /* Print the header only when there's something else
565 to print below. */
566 fprintf (f, "Non-varying global ranges:\n");
567 fprintf (f, "=========================:\n");
568 print_header = false;
571 print_generic_expr (f, ssa_name (x), TDF_NONE);
572 fprintf (f, " : ");
573 r.dump (f);
574 fprintf (f, "\n");
578 if (!print_header)
579 fputc ('\n', f);
582 // --------------------------------------------------------------------------
585 // This class will manage the timestamps for each ssa_name.
586 // When a value is calculated, the timestamp is set to the current time.
587 // Current time is then incremented. Any dependencies will already have
588 // been calculated, and will thus have older timestamps.
589 // If one of those values is ever calculated again, it will get a newer
590 // timestamp, and the "current_p" check will fail.
592 class temporal_cache
594 public:
595 temporal_cache ();
596 ~temporal_cache ();
597 bool current_p (tree name, tree dep1, tree dep2) const;
598 void set_timestamp (tree name);
599 void set_always_current (tree name);
600 private:
601 unsigned temporal_value (unsigned ssa) const;
603 unsigned m_current_time;
604 vec <unsigned> m_timestamp;
607 inline
608 temporal_cache::temporal_cache ()
610 m_current_time = 1;
611 m_timestamp.create (0);
612 m_timestamp.safe_grow_cleared (num_ssa_names);
615 inline
616 temporal_cache::~temporal_cache ()
618 m_timestamp.release ();
621 // Return the timestamp value for SSA, or 0 if there isnt one.
623 inline unsigned
624 temporal_cache::temporal_value (unsigned ssa) const
626 if (ssa >= m_timestamp.length ())
627 return 0;
628 return m_timestamp[ssa];
631 // Return TRUE if the timestampe for NAME is newer than any of its dependents.
632 // Up to 2 dependencies can be checked.
634 bool
635 temporal_cache::current_p (tree name, tree dep1, tree dep2) const
637 unsigned ts = temporal_value (SSA_NAME_VERSION (name));
638 if (ts == 0)
639 return true;
641 // Any non-registered dependencies will have a value of 0 and thus be older.
642 // Return true if time is newer than either dependent.
644 if (dep1 && ts < temporal_value (SSA_NAME_VERSION (dep1)))
645 return false;
646 if (dep2 && ts < temporal_value (SSA_NAME_VERSION (dep2)))
647 return false;
649 return true;
652 // This increments the global timer and sets the timestamp for NAME.
654 inline void
655 temporal_cache::set_timestamp (tree name)
657 unsigned v = SSA_NAME_VERSION (name);
658 if (v >= m_timestamp.length ())
659 m_timestamp.safe_grow_cleared (num_ssa_names + 20);
660 m_timestamp[v] = ++m_current_time;
663 // Set the timestamp to 0, marking it as "always up to date".
665 inline void
666 temporal_cache::set_always_current (tree name)
668 unsigned v = SSA_NAME_VERSION (name);
669 if (v >= m_timestamp.length ())
670 m_timestamp.safe_grow_cleared (num_ssa_names + 20);
671 m_timestamp[v] = 0;
674 // --------------------------------------------------------------------------
676 // This class provides an abstraction of a list of blocks to be updated
677 // by the cache. It is currently a stack but could be changed. It also
678 // maintains a list of blocks which have failed propagation, and does not
679 // enter any of those blocks into the list.
681 // A vector over the BBs is maintained, and an entry of 0 means it is not in
682 // a list. Otherwise, the entry is the next block in the list. -1 terminates
683 // the list. m_head points to the top of the list, -1 if the list is empty.
685 class update_list
687 public:
688 update_list ();
689 ~update_list ();
690 void add (basic_block bb);
691 basic_block pop ();
692 inline bool empty_p () { return m_update_head == -1; }
693 inline void clear_failures () { bitmap_clear (m_propfail); }
694 inline void propagation_failed (basic_block bb)
695 { bitmap_set_bit (m_propfail, bb->index); }
696 private:
697 vec<int> m_update_list;
698 int m_update_head;
699 bitmap m_propfail;
702 // Create an update list.
704 update_list::update_list ()
706 m_update_list.create (0);
707 m_update_list.safe_grow_cleared (last_basic_block_for_fn (cfun) + 64);
708 m_update_head = -1;
709 m_propfail = BITMAP_ALLOC (NULL);
712 // Destroy an update list.
714 update_list::~update_list ()
716 m_update_list.release ();
717 BITMAP_FREE (m_propfail);
720 // Add BB to the list of blocks to update, unless it's already in the list.
722 void
723 update_list::add (basic_block bb)
725 int i = bb->index;
726 // If propagation has failed for BB, or its already in the list, don't
727 // add it again.
728 if ((unsigned)i >= m_update_list.length ())
729 m_update_list.safe_grow_cleared (i + 64);
730 if (!m_update_list[i] && !bitmap_bit_p (m_propfail, i))
732 if (empty_p ())
734 m_update_head = i;
735 m_update_list[i] = -1;
737 else
739 gcc_checking_assert (m_update_head > 0);
740 m_update_list[i] = m_update_head;
741 m_update_head = i;
746 // Remove a block from the list.
748 basic_block
749 update_list::pop ()
751 gcc_checking_assert (!empty_p ());
752 basic_block bb = BASIC_BLOCK_FOR_FN (cfun, m_update_head);
753 int pop = m_update_head;
754 m_update_head = m_update_list[pop];
755 m_update_list[pop] = 0;
756 return bb;
759 // --------------------------------------------------------------------------
761 ranger_cache::ranger_cache (int not_executable_flag, bool use_imm_uses)
762 : m_gori (not_executable_flag),
763 m_exit (use_imm_uses)
765 m_workback.create (0);
766 m_workback.safe_grow_cleared (last_basic_block_for_fn (cfun));
767 m_workback.truncate (0);
768 m_temporal = new temporal_cache;
769 // If DOM info is available, spawn an oracle as well.
770 if (dom_info_available_p (CDI_DOMINATORS))
771 m_oracle = new dom_oracle ();
772 else
773 m_oracle = NULL;
775 unsigned x, lim = last_basic_block_for_fn (cfun);
776 // Calculate outgoing range info upfront. This will fully populate the
777 // m_maybe_variant bitmap which will help eliminate processing of names
778 // which never have their ranges adjusted.
779 for (x = 0; x < lim ; x++)
781 basic_block bb = BASIC_BLOCK_FOR_FN (cfun, x);
782 if (bb)
783 m_gori.exports (bb);
785 m_update = new update_list ();
788 ranger_cache::~ranger_cache ()
790 delete m_update;
791 if (m_oracle)
792 delete m_oracle;
793 delete m_temporal;
794 m_workback.release ();
797 // Dump the global caches to file F. if GORI_DUMP is true, dump the
798 // gori map as well.
800 void
801 ranger_cache::dump (FILE *f)
803 m_globals.dump (f);
804 fprintf (f, "\n");
807 // Dump the caches for basic block BB to file F.
809 void
810 ranger_cache::dump_bb (FILE *f, basic_block bb)
812 m_gori.gori_map::dump (f, bb, false);
813 m_on_entry.dump (f, bb);
814 if (m_oracle)
815 m_oracle->dump (f, bb);
818 // Get the global range for NAME, and return in R. Return false if the
819 // global range is not set, and return the legacy global value in R.
821 bool
822 ranger_cache::get_global_range (vrange &r, tree name) const
824 if (m_globals.get_global_range (r, name))
825 return true;
826 gimple_range_global (r, name);
827 return false;
830 // Get the global range for NAME, and return in R. Return false if the
831 // global range is not set, and R will contain the legacy global value.
832 // CURRENT_P is set to true if the value was in cache and not stale.
833 // Otherwise, set CURRENT_P to false and mark as it always current.
834 // If the global cache did not have a value, initialize it as well.
835 // After this call, the global cache will have a value.
837 bool
838 ranger_cache::get_global_range (vrange &r, tree name, bool &current_p)
840 bool had_global = get_global_range (r, name);
842 // If there was a global value, set current flag, otherwise set a value.
843 current_p = false;
844 if (had_global)
845 current_p = r.singleton_p ()
846 || m_temporal->current_p (name, m_gori.depend1 (name),
847 m_gori.depend2 (name));
848 else
849 m_globals.set_global_range (name, r);
851 // If the existing value was not current, mark it as always current.
852 if (!current_p)
853 m_temporal->set_always_current (name);
854 return had_global;
857 // Set the global range of NAME to R and give it a timestamp.
859 void
860 ranger_cache::set_global_range (tree name, const vrange &r)
862 if (m_globals.set_global_range (name, r))
864 // If there was already a range set, propagate the new value.
865 basic_block bb = gimple_bb (SSA_NAME_DEF_STMT (name));
866 if (!bb)
867 bb = ENTRY_BLOCK_PTR_FOR_FN (cfun);
869 if (DEBUG_RANGE_CACHE)
870 fprintf (dump_file, " GLOBAL :");
872 propagate_updated_value (name, bb);
874 // Constants no longer need to tracked. Any further refinement has to be
875 // undefined. Propagation works better with constants. PR 100512.
876 // Pointers which resolve to non-zero also do not need
877 // tracking in the cache as they will never change. See PR 98866.
878 // Timestamp must always be updated, or dependent calculations may
879 // not include this latest value. PR 100774.
881 if (r.singleton_p ()
882 || (POINTER_TYPE_P (TREE_TYPE (name)) && r.nonzero_p ()))
883 m_gori.set_range_invariant (name);
884 m_temporal->set_timestamp (name);
887 // Provide lookup for the gori-computes class to access the best known range
888 // of an ssa_name in any given basic block. Note, this does no additonal
889 // lookups, just accesses the data that is already known.
891 // Get the range of NAME when the def occurs in block BB. If BB is NULL
892 // get the best global value available.
894 void
895 ranger_cache::range_of_def (vrange &r, tree name, basic_block bb)
897 gcc_checking_assert (gimple_range_ssa_p (name));
898 gcc_checking_assert (!bb || bb == gimple_bb (SSA_NAME_DEF_STMT (name)));
900 // Pick up the best global range available.
901 if (!m_globals.get_global_range (r, name))
903 // If that fails, try to calculate the range using just global values.
904 gimple *s = SSA_NAME_DEF_STMT (name);
905 if (gimple_get_lhs (s) == name)
906 fold_range (r, s, get_global_range_query ());
907 else
908 gimple_range_global (r, name);
912 // Get the range of NAME as it occurs on entry to block BB. Use MODE for
913 // lookups.
915 void
916 ranger_cache::entry_range (vrange &r, tree name, basic_block bb,
917 enum rfd_mode mode)
919 if (bb == ENTRY_BLOCK_PTR_FOR_FN (cfun))
921 gimple_range_global (r, name);
922 return;
925 // Look for the on-entry value of name in BB from the cache.
926 // Otherwise pick up the best available global value.
927 if (!m_on_entry.get_bb_range (r, name, bb))
928 if (!range_from_dom (r, name, bb, mode))
929 range_of_def (r, name);
932 // Get the range of NAME as it occurs on exit from block BB. Use MODE for
933 // lookups.
935 void
936 ranger_cache::exit_range (vrange &r, tree name, basic_block bb,
937 enum rfd_mode mode)
939 if (bb == ENTRY_BLOCK_PTR_FOR_FN (cfun))
941 gimple_range_global (r, name);
942 return;
945 gimple *s = SSA_NAME_DEF_STMT (name);
946 basic_block def_bb = gimple_bb (s);
947 if (def_bb == bb)
948 range_of_def (r, name, bb);
949 else
950 entry_range (r, name, bb, mode);
953 // Get the range of NAME on edge E using MODE, return the result in R.
954 // Always returns a range and true.
956 bool
957 ranger_cache::edge_range (vrange &r, edge e, tree name, enum rfd_mode mode)
959 exit_range (r, name, e->src, mode);
960 // If this is not an abnormal edge, check for inferred ranges on exit.
961 if ((e->flags & (EDGE_EH | EDGE_ABNORMAL)) == 0)
962 m_exit.maybe_adjust_range (r, name, e->src);
963 Value_Range er (TREE_TYPE (name));
964 if (m_gori.outgoing_edge_range_p (er, e, name, *this))
965 r.intersect (er);
966 return true;
971 // Implement range_of_expr.
973 bool
974 ranger_cache::range_of_expr (vrange &r, tree name, gimple *stmt)
976 if (!gimple_range_ssa_p (name))
978 get_tree_range (r, name, stmt);
979 return true;
982 basic_block bb = gimple_bb (stmt);
983 gimple *def_stmt = SSA_NAME_DEF_STMT (name);
984 basic_block def_bb = gimple_bb (def_stmt);
986 if (bb == def_bb)
987 range_of_def (r, name, bb);
988 else
989 entry_range (r, name, bb, RFD_NONE);
990 return true;
994 // Implement range_on_edge. Always return the best available range using
995 // the current cache values.
997 bool
998 ranger_cache::range_on_edge (vrange &r, edge e, tree expr)
1000 if (gimple_range_ssa_p (expr))
1001 return edge_range (r, e, expr, RFD_NONE);
1002 return get_tree_range (r, expr, NULL);
1005 // Return a static range for NAME on entry to basic block BB in R. If
1006 // calc is true, fill any cache entries required between BB and the
1007 // def block for NAME. Otherwise, return false if the cache is empty.
1009 bool
1010 ranger_cache::block_range (vrange &r, basic_block bb, tree name, bool calc)
1012 gcc_checking_assert (gimple_range_ssa_p (name));
1014 // If there are no range calculations anywhere in the IL, global range
1015 // applies everywhere, so don't bother caching it.
1016 if (!m_gori.has_edge_range_p (name))
1017 return false;
1019 if (calc)
1021 gimple *def_stmt = SSA_NAME_DEF_STMT (name);
1022 basic_block def_bb = NULL;
1023 if (def_stmt)
1024 def_bb = gimple_bb (def_stmt);;
1025 if (!def_bb)
1027 // If we get to the entry block, this better be a default def
1028 // or range_on_entry was called for a block not dominated by
1029 // the def.
1030 gcc_checking_assert (SSA_NAME_IS_DEFAULT_DEF (name));
1031 def_bb = ENTRY_BLOCK_PTR_FOR_FN (cfun);
1034 // There is no range on entry for the definition block.
1035 if (def_bb == bb)
1036 return false;
1038 // Otherwise, go figure out what is known in predecessor blocks.
1039 fill_block_cache (name, bb, def_bb);
1040 gcc_checking_assert (m_on_entry.bb_range_p (name, bb));
1042 return m_on_entry.get_bb_range (r, name, bb);
1045 // If there is anything in the propagation update_list, continue
1046 // processing NAME until the list of blocks is empty.
1048 void
1049 ranger_cache::propagate_cache (tree name)
1051 basic_block bb;
1052 edge_iterator ei;
1053 edge e;
1054 tree type = TREE_TYPE (name);
1055 Value_Range new_range (type);
1056 Value_Range current_range (type);
1057 Value_Range e_range (type);
1059 // Process each block by seeing if its calculated range on entry is
1060 // the same as its cached value. If there is a difference, update
1061 // the cache to reflect the new value, and check to see if any
1062 // successors have cache entries which may need to be checked for
1063 // updates.
1065 while (!m_update->empty_p ())
1067 bb = m_update->pop ();
1068 gcc_checking_assert (m_on_entry.bb_range_p (name, bb));
1069 m_on_entry.get_bb_range (current_range, name, bb);
1071 if (DEBUG_RANGE_CACHE)
1073 fprintf (dump_file, "FWD visiting block %d for ", bb->index);
1074 print_generic_expr (dump_file, name, TDF_SLIM);
1075 fprintf (dump_file, " starting range : ");
1076 current_range.dump (dump_file);
1077 fprintf (dump_file, "\n");
1080 // Calculate the "new" range on entry by unioning the pred edges.
1081 new_range.set_undefined ();
1082 FOR_EACH_EDGE (e, ei, bb->preds)
1084 range_on_edge (e_range, e, name);
1085 if (DEBUG_RANGE_CACHE)
1087 fprintf (dump_file, " edge %d->%d :", e->src->index, bb->index);
1088 e_range.dump (dump_file);
1089 fprintf (dump_file, "\n");
1091 new_range.union_ (e_range);
1092 if (new_range.varying_p ())
1093 break;
1096 // If the range on entry has changed, update it.
1097 if (new_range != current_range)
1099 bool ok_p = m_on_entry.set_bb_range (name, bb, new_range);
1100 // If the cache couldn't set the value, mark it as failed.
1101 if (!ok_p)
1102 m_update->propagation_failed (bb);
1103 if (DEBUG_RANGE_CACHE)
1105 if (!ok_p)
1107 fprintf (dump_file, " Cache failure to store value:");
1108 print_generic_expr (dump_file, name, TDF_SLIM);
1109 fprintf (dump_file, " ");
1111 else
1113 fprintf (dump_file, " Updating range to ");
1114 new_range.dump (dump_file);
1116 fprintf (dump_file, "\n Updating blocks :");
1118 // Mark each successor that has a range to re-check its range
1119 FOR_EACH_EDGE (e, ei, bb->succs)
1120 if (m_on_entry.bb_range_p (name, e->dest))
1122 if (DEBUG_RANGE_CACHE)
1123 fprintf (dump_file, " bb%d",e->dest->index);
1124 m_update->add (e->dest);
1126 if (DEBUG_RANGE_CACHE)
1127 fprintf (dump_file, "\n");
1130 if (DEBUG_RANGE_CACHE)
1132 fprintf (dump_file, "DONE visiting blocks for ");
1133 print_generic_expr (dump_file, name, TDF_SLIM);
1134 fprintf (dump_file, "\n");
1136 m_update->clear_failures ();
1139 // Check to see if an update to the value for NAME in BB has any effect
1140 // on values already in the on-entry cache for successor blocks.
1141 // If it does, update them. Don't visit any blocks which dont have a cache
1142 // entry.
1144 void
1145 ranger_cache::propagate_updated_value (tree name, basic_block bb)
1147 edge e;
1148 edge_iterator ei;
1150 // The update work list should be empty at this point.
1151 gcc_checking_assert (m_update->empty_p ());
1152 gcc_checking_assert (bb);
1154 if (DEBUG_RANGE_CACHE)
1156 fprintf (dump_file, " UPDATE cache for ");
1157 print_generic_expr (dump_file, name, TDF_SLIM);
1158 fprintf (dump_file, " in BB %d : successors : ", bb->index);
1160 FOR_EACH_EDGE (e, ei, bb->succs)
1162 // Only update active cache entries.
1163 if (m_on_entry.bb_range_p (name, e->dest))
1165 m_update->add (e->dest);
1166 if (DEBUG_RANGE_CACHE)
1167 fprintf (dump_file, " UPDATE: bb%d", e->dest->index);
1170 if (!m_update->empty_p ())
1172 if (DEBUG_RANGE_CACHE)
1173 fprintf (dump_file, "\n");
1174 propagate_cache (name);
1176 else
1178 if (DEBUG_RANGE_CACHE)
1179 fprintf (dump_file, " : No updates!\n");
1183 // Make sure that the range-on-entry cache for NAME is set for block BB.
1184 // Work back through the CFG to DEF_BB ensuring the range is calculated
1185 // on the block/edges leading back to that point.
1187 void
1188 ranger_cache::fill_block_cache (tree name, basic_block bb, basic_block def_bb)
1190 edge_iterator ei;
1191 edge e;
1192 tree type = TREE_TYPE (name);
1193 Value_Range block_result (type);
1194 Value_Range undefined (type);
1196 // At this point we shouldn't be looking at the def, entry block.
1197 gcc_checking_assert (bb != def_bb && bb != ENTRY_BLOCK_PTR_FOR_FN (cfun));
1198 gcc_checking_assert (m_workback.length () == 0);
1200 // If the block cache is set, then we've already visited this block.
1201 if (m_on_entry.bb_range_p (name, bb))
1202 return;
1204 if (DEBUG_RANGE_CACHE)
1206 fprintf (dump_file, "\n");
1207 print_generic_expr (dump_file, name, TDF_SLIM);
1208 fprintf (dump_file, " : ");
1211 // Check if a dominators can supply the range.
1212 if (range_from_dom (block_result, name, bb, RFD_FILL))
1214 if (DEBUG_RANGE_CACHE)
1216 fprintf (dump_file, "Filled from dominator! : ");
1217 block_result.dump (dump_file);
1218 fprintf (dump_file, "\n");
1220 // See if any equivalences can refine it.
1221 if (m_oracle)
1223 tree equiv_name;
1224 relation_kind rel;
1225 int prec = TYPE_PRECISION (type);
1226 FOR_EACH_PARTIAL_AND_FULL_EQUIV (m_oracle, bb, name, equiv_name, rel)
1228 basic_block equiv_bb = gimple_bb (SSA_NAME_DEF_STMT (equiv_name));
1230 // Ignore partial equivs that are smaller than this object.
1231 if (rel != VREL_EQ && prec > pe_to_bits (rel))
1232 continue;
1234 // Check if the equiv has any ranges calculated.
1235 if (!m_gori.has_edge_range_p (equiv_name))
1236 continue;
1238 // PR 108139. It is hazardous to assume an equivalence with
1239 // a PHI is the same value. The PHI may be an equivalence
1240 // via UNDEFINED arguments which is really a one way equivalence.
1241 // PHIDEF == name, but name may not be == PHIDEF.
1242 if (is_a<gphi *> (SSA_NAME_DEF_STMT (equiv_name)))
1243 continue;
1245 // Check if the equiv definition dominates this block
1246 if (equiv_bb == bb ||
1247 (equiv_bb && !dominated_by_p (CDI_DOMINATORS, bb, equiv_bb)))
1248 continue;
1250 if (DEBUG_RANGE_CACHE)
1252 if (rel == VREL_EQ)
1253 fprintf (dump_file, "Checking Equivalence (");
1254 else
1255 fprintf (dump_file, "Checking Partial equiv (");
1256 print_relation (dump_file, rel);
1257 fprintf (dump_file, ") ");
1258 print_generic_expr (dump_file, equiv_name, TDF_SLIM);
1259 fprintf (dump_file, "\n");
1261 Value_Range equiv_range (TREE_TYPE (equiv_name));
1262 if (range_from_dom (equiv_range, equiv_name, bb, RFD_READ_ONLY))
1264 if (rel != VREL_EQ)
1265 range_cast (equiv_range, type);
1266 if (block_result.intersect (equiv_range))
1268 if (DEBUG_RANGE_CACHE)
1270 if (rel == VREL_EQ)
1271 fprintf (dump_file, "Equivalence update! : ");
1272 else
1273 fprintf (dump_file, "Partial equiv update! : ");
1274 print_generic_expr (dump_file, equiv_name, TDF_SLIM);
1275 fprintf (dump_file, " has range : ");
1276 equiv_range.dump (dump_file);
1277 fprintf (dump_file, " refining range to :");
1278 block_result.dump (dump_file);
1279 fprintf (dump_file, "\n");
1286 m_on_entry.set_bb_range (name, bb, block_result);
1287 gcc_checking_assert (m_workback.length () == 0);
1288 return;
1291 // Visit each block back to the DEF. Initialize each one to UNDEFINED.
1292 // m_visited at the end will contain all the blocks that we needed to set
1293 // the range_on_entry cache for.
1294 m_workback.quick_push (bb);
1295 undefined.set_undefined ();
1296 m_on_entry.set_bb_range (name, bb, undefined);
1297 gcc_checking_assert (m_update->empty_p ());
1299 while (m_workback.length () > 0)
1301 basic_block node = m_workback.pop ();
1302 if (DEBUG_RANGE_CACHE)
1304 fprintf (dump_file, "BACK visiting block %d for ", node->index);
1305 print_generic_expr (dump_file, name, TDF_SLIM);
1306 fprintf (dump_file, "\n");
1309 FOR_EACH_EDGE (e, ei, node->preds)
1311 basic_block pred = e->src;
1312 Value_Range r (TREE_TYPE (name));
1314 if (DEBUG_RANGE_CACHE)
1315 fprintf (dump_file, " %d->%d ",e->src->index, e->dest->index);
1317 // If the pred block is the def block add this BB to update list.
1318 if (pred == def_bb)
1320 m_update->add (node);
1321 continue;
1324 // If the pred is entry but NOT def, then it is used before
1325 // defined, it'll get set to [] and no need to update it.
1326 if (pred == ENTRY_BLOCK_PTR_FOR_FN (cfun))
1328 if (DEBUG_RANGE_CACHE)
1329 fprintf (dump_file, "entry: bail.");
1330 continue;
1333 // Regardless of whether we have visited pred or not, if the
1334 // pred has inferred ranges, revisit this block.
1335 // Don't search the DOM tree.
1336 if (m_exit.has_range_p (name, pred))
1338 if (DEBUG_RANGE_CACHE)
1339 fprintf (dump_file, "Inferred range: update ");
1340 m_update->add (node);
1343 // If the pred block already has a range, or if it can contribute
1344 // something new. Ie, the edge generates a range of some sort.
1345 if (m_on_entry.get_bb_range (r, name, pred))
1347 if (DEBUG_RANGE_CACHE)
1349 fprintf (dump_file, "has cache, ");
1350 r.dump (dump_file);
1351 fprintf (dump_file, ", ");
1353 if (!r.undefined_p () || m_gori.has_edge_range_p (name, e))
1355 m_update->add (node);
1356 if (DEBUG_RANGE_CACHE)
1357 fprintf (dump_file, "update. ");
1359 continue;
1362 if (DEBUG_RANGE_CACHE)
1363 fprintf (dump_file, "pushing undefined pred block.\n");
1364 // If the pred hasn't been visited (has no range), add it to
1365 // the list.
1366 gcc_checking_assert (!m_on_entry.bb_range_p (name, pred));
1367 m_on_entry.set_bb_range (name, pred, undefined);
1368 m_workback.quick_push (pred);
1372 if (DEBUG_RANGE_CACHE)
1373 fprintf (dump_file, "\n");
1375 // Now fill in the marked blocks with values.
1376 propagate_cache (name);
1377 if (DEBUG_RANGE_CACHE)
1378 fprintf (dump_file, " Propagation update done.\n");
1381 // Resolve the range of BB if the dominators range is R by calculating incoming
1382 // edges to this block. All lead back to the dominator so should be cheap.
1383 // The range for BB is set and returned in R.
1385 void
1386 ranger_cache::resolve_dom (vrange &r, tree name, basic_block bb)
1388 basic_block def_bb = gimple_bb (SSA_NAME_DEF_STMT (name));
1389 basic_block dom_bb = get_immediate_dominator (CDI_DOMINATORS, bb);
1391 // if it doesn't already have a value, store the incoming range.
1392 if (!m_on_entry.bb_range_p (name, dom_bb) && def_bb != dom_bb)
1394 // If the range can't be store, don't try to accumulate
1395 // the range in PREV_BB due to excessive recalculations.
1396 if (!m_on_entry.set_bb_range (name, dom_bb, r))
1397 return;
1399 // With the dominator set, we should be able to cheaply query
1400 // each incoming edge now and accumulate the results.
1401 r.set_undefined ();
1402 edge e;
1403 edge_iterator ei;
1404 Value_Range er (TREE_TYPE (name));
1405 FOR_EACH_EDGE (e, ei, bb->preds)
1407 edge_range (er, e, name, RFD_READ_ONLY);
1408 r.union_ (er);
1410 // Set the cache in PREV_BB so it is not calculated again.
1411 m_on_entry.set_bb_range (name, bb, r);
1414 // Get the range of NAME from dominators of BB and return it in R. Search the
1415 // dominator tree based on MODE.
1417 bool
1418 ranger_cache::range_from_dom (vrange &r, tree name, basic_block start_bb,
1419 enum rfd_mode mode)
1421 if (mode == RFD_NONE || !dom_info_available_p (CDI_DOMINATORS))
1422 return false;
1424 // Search back to the definition block or entry block.
1425 basic_block def_bb = gimple_bb (SSA_NAME_DEF_STMT (name));
1426 if (def_bb == NULL)
1427 def_bb = ENTRY_BLOCK_PTR_FOR_FN (cfun);
1429 basic_block bb;
1430 basic_block prev_bb = start_bb;
1432 // Track any inferred ranges seen.
1433 Value_Range infer (TREE_TYPE (name));
1434 infer.set_varying (TREE_TYPE (name));
1436 // Range on entry to the DEF block should not be queried.
1437 gcc_checking_assert (start_bb != def_bb);
1438 unsigned start_limit = m_workback.length ();
1440 // Default value is global range.
1441 get_global_range (r, name);
1443 // The dominator of EXIT_BLOCK doesn't seem to be set, so at least handle
1444 // the common single exit cases.
1445 if (start_bb == EXIT_BLOCK_PTR_FOR_FN (cfun) && single_pred_p (start_bb))
1446 bb = single_pred_edge (start_bb)->src;
1447 else
1448 bb = get_immediate_dominator (CDI_DOMINATORS, start_bb);
1450 // Search until a value is found, pushing blocks which may need calculating.
1451 for ( ; bb; prev_bb = bb, bb = get_immediate_dominator (CDI_DOMINATORS, bb))
1453 // Accumulate any block exit inferred ranges.
1454 m_exit.maybe_adjust_range (infer, name, bb);
1456 // This block has an outgoing range.
1457 if (m_gori.has_edge_range_p (name, bb))
1458 m_workback.quick_push (prev_bb);
1459 else
1461 // Normally join blocks don't carry any new range information on
1462 // incoming edges. If the first incoming edge to this block does
1463 // generate a range, calculate the ranges if all incoming edges
1464 // are also dominated by the dominator. (Avoids backedges which
1465 // will break the rule of moving only upward in the domniator tree).
1466 // If the first pred does not generate a range, then we will be
1467 // using the dominator range anyway, so thats all the check needed.
1468 if (EDGE_COUNT (prev_bb->preds) > 1
1469 && m_gori.has_edge_range_p (name, EDGE_PRED (prev_bb, 0)->src))
1471 edge e;
1472 edge_iterator ei;
1473 bool all_dom = true;
1474 FOR_EACH_EDGE (e, ei, prev_bb->preds)
1475 if (e->src != bb
1476 && !dominated_by_p (CDI_DOMINATORS, e->src, bb))
1478 all_dom = false;
1479 break;
1481 if (all_dom)
1482 m_workback.quick_push (prev_bb);
1486 if (def_bb == bb)
1487 break;
1489 if (m_on_entry.get_bb_range (r, name, bb))
1490 break;
1493 if (DEBUG_RANGE_CACHE)
1495 fprintf (dump_file, "CACHE: BB %d DOM query for ", start_bb->index);
1496 print_generic_expr (dump_file, name, TDF_SLIM);
1497 fprintf (dump_file, ", found ");
1498 r.dump (dump_file);
1499 if (bb)
1500 fprintf (dump_file, " at BB%d\n", bb->index);
1501 else
1502 fprintf (dump_file, " at function top\n");
1505 // Now process any blocks wit incoming edges that nay have adjustemnts.
1506 while (m_workback.length () > start_limit)
1508 Value_Range er (TREE_TYPE (name));
1509 prev_bb = m_workback.pop ();
1510 if (!single_pred_p (prev_bb))
1512 // Non single pred means we need to cache a vsalue in the dominator
1513 // so we can cheaply calculate incoming edges to this block, and
1514 // then store the resulting value. If processing mode is not
1515 // RFD_FILL, then the cache cant be stored to, so don't try.
1516 // Otherwise this becomes a quadratic timed calculation.
1517 if (mode == RFD_FILL)
1518 resolve_dom (r, name, prev_bb);
1519 continue;
1522 edge e = single_pred_edge (prev_bb);
1523 bb = e->src;
1524 if (m_gori.outgoing_edge_range_p (er, e, name, *this))
1526 r.intersect (er);
1527 // If this is a normal edge, apply any inferred ranges.
1528 if ((e->flags & (EDGE_EH | EDGE_ABNORMAL)) == 0)
1529 m_exit.maybe_adjust_range (r, name, bb);
1531 if (DEBUG_RANGE_CACHE)
1533 fprintf (dump_file, "CACHE: Adjusted edge range for %d->%d : ",
1534 bb->index, prev_bb->index);
1535 r.dump (dump_file);
1536 fprintf (dump_file, "\n");
1541 // Apply non-null if appropriate.
1542 if (!has_abnormal_call_or_eh_pred_edge_p (start_bb))
1543 r.intersect (infer);
1545 if (DEBUG_RANGE_CACHE)
1547 fprintf (dump_file, "CACHE: Range for DOM returns : ");
1548 r.dump (dump_file);
1549 fprintf (dump_file, "\n");
1551 return true;
1554 // This routine will register an inferred value in block BB, and possibly
1555 // update the on-entry cache if appropriate.
1557 void
1558 ranger_cache::register_inferred_value (const vrange &ir, tree name,
1559 basic_block bb)
1561 Value_Range r (TREE_TYPE (name));
1562 if (!m_on_entry.get_bb_range (r, name, bb))
1563 exit_range (r, name, bb, RFD_READ_ONLY);
1564 if (r.intersect (ir))
1566 m_on_entry.set_bb_range (name, bb, r);
1567 // If this range was invariant before, remove invariance.
1568 if (!m_gori.has_edge_range_p (name))
1569 m_gori.set_range_invariant (name, false);
1573 // This routine is used during a block walk to adjust any inferred ranges
1574 // of operands on stmt S.
1576 void
1577 ranger_cache::apply_inferred_ranges (gimple *s)
1579 bool update = true;
1581 basic_block bb = gimple_bb (s);
1582 gimple_infer_range infer(s);
1583 if (infer.num () == 0)
1584 return;
1586 // Do not update the on-entry cache for block ending stmts.
1587 if (stmt_ends_bb_p (s))
1589 edge_iterator ei;
1590 edge e;
1591 FOR_EACH_EDGE (e, ei, gimple_bb (s)->succs)
1592 if (!(e->flags & (EDGE_ABNORMAL|EDGE_EH)))
1593 break;
1594 if (e == NULL)
1595 update = false;
1598 for (unsigned x = 0; x < infer.num (); x++)
1600 tree name = infer.name (x);
1601 m_exit.add_range (name, bb, infer.range (x));
1602 if (update)
1603 register_inferred_value (infer.range (x), name, bb);