1 /* Gimple ranger SSA cache implementation.
2 Copyright (C) 2017-2023 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)
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/>. */
23 #include "coretypes.h"
25 #include "insn-codes.h"
29 #include "gimple-pretty-print.h"
30 #include "gimple-range.h"
31 #include "value-range-storage.h"
35 #include "gimple-iterator.h"
36 #include "gimple-walk.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
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;
58 // Print the list of known ranges for file F in a nice format.
61 ssa_block_ranges::dump (FILE *f
)
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
);
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
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
;
88 vrange
**m_tab
; // Non growing vector.
93 vrange_allocator
*m_range_allocator
;
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
));
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.
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
*));
138 m_tab_size
= new_size
;
141 // Set the range for block BB to be R.
144 sbr_vector::set_bb_range (const_basic_block bb
, const vrange
&r
)
147 if (bb
->index
>= m_tab_size
)
151 else if (r
.undefined_p ())
154 m
= m_range_allocator
->clone (r
);
155 m_tab
[bb
->index
] = m
;
159 // Return the range associated with block BB in R. Return false if
160 // there is no range.
163 sbr_vector::get_bb_range (vrange
&r
, const_basic_block bb
)
165 if (bb
->index
>= m_tab_size
)
167 vrange
*m
= m_tab
[bb
->index
];
176 // Return true if a range is present.
179 sbr_vector::bb_range_p (const_basic_block bb
)
181 if (bb
->index
< m_tab_size
)
182 return m_tab
[bb
->index
] != NULL
;
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]
196 #define SBR_UNDEF SBR_NUM + 1
197 #define SBR_VARYING 1
199 class sbr_sparse_bitmap
: public ssa_block_ranges
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
;
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
];
215 // Initialize a block cache for an ssa_name of type T.
217 sbr_sparse_bitmap::sbr_sparse_bitmap (tree t
, vrange_allocator
*allocator
,
219 : ssa_block_ranges (t
)
221 gcc_checking_assert (TYPE_P (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
);
238 m_range
[1] = m_range
[2] = NULL
;
239 // Clear SBR_NUM entries.
240 for (int x
= 3; x
< SBR_NUM
; x
++)
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.
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.
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.
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
);
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
]))
279 m_range
[x
] = m_range_allocator
->clone (r
);
280 bitmap_set_quad (&bitvec
, bb
->index
, x
+ 1);
283 // All values are taken, default to VARYING.
284 bitmap_set_quad (&bitvec
, bb
->index
, SBR_VARYING
);
288 // Return the range associated with block BB in R. Return false if
289 // there is no range.
292 sbr_sparse_bitmap::get_bb_range (vrange
&r
, const_basic_block bb
)
294 int value
= bitmap_get_quad (&bitvec
, bb
->index
);
299 gcc_checking_assert (value
<= SBR_UNDEF
);
300 if (value
== SBR_UNDEF
)
303 r
= *(m_range
[value
- 1]);
307 // Return true if a range is present.
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.
341 block_range_cache::set_bb_range (tree name
, const_basic_block bb
,
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
),
360 // Otherwise use the default vector implementation.
361 void *r
= m_range_allocator
->alloc (sizeof (sbr_vector
));
362 m_ssa_ranges
[v
] = new (r
) sbr_vector (TREE_TYPE (name
),
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
])
379 return m_ssa_ranges
[v
];
384 // Return the range for NAME on entry to BB in R. Return true if there
388 block_range_cache::get_bb_range (vrange
&r
, tree name
, const_basic_block bb
)
390 ssa_block_ranges
*ptr
= query_block_ranges (name
);
392 return ptr
->get_bb_range (r
, bb
);
396 // Return true if NAME has a range set in block BB.
399 block_range_cache::bb_range_p (tree name
, const_basic_block bb
)
401 ssa_block_ranges
*ptr
= query_block_ranges (name
);
403 return ptr
->bb_range_p (bb
);
407 // Print all known block caches to file F.
410 block_range_cache::dump (FILE *f
)
413 for (x
= 0; x
< m_ssa_ranges
.length (); ++x
)
417 fprintf (f
, " Ranges for ");
418 print_generic_expr (f
, ssa_name (x
), TDF_NONE
);
420 m_ssa_ranges
[x
]->dump (f
);
426 // Print all known ranges on entry to block BB to file F.
429 block_range_cache::dump (FILE *f
, basic_block bb
, bool print_varying
)
432 bool summarize_varying
= false;
433 for (x
= 1; x
< m_ssa_ranges
.length (); ++x
)
435 if (!gimple_range_ssa_p (ssa_name (x
)))
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;
446 print_generic_expr (f
, ssa_name (x
), TDF_NONE
);
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
)))
461 Value_Range
r (TREE_TYPE (ssa_name (x
)));
462 if (m_ssa_ranges
[x
] && m_ssa_ranges
[x
]->get_bb_range (r
, bb
))
466 print_generic_expr (f
, ssa_name (x
), TDF_NONE
);
475 // -------------------------------------------------------------------------
477 // Initialize a global cache.
479 ssa_global_cache::ssa_global_cache ()
482 m_range_allocator
= new obstack_vrange_allocator
;
485 // Deconstruct a global cache.
487 ssa_global_cache::~ssa_global_cache ()
490 delete m_range_allocator
;
493 // Retrieve the global range of NAME from cache memory if it exists.
494 // Return the value in R.
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 ())
503 vrange
*stow
= m_tab
[v
];
510 // Set the range for NAME to R in the global cache.
511 // Return TRUE if there was already a range set, otherwise false.
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
))
524 m_tab
[v
] = m_range_allocator
->clone (r
);
528 // Set the range for NAME to R in the global cache.
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);
539 // Clear the global cache.
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.
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
)))
559 Value_Range
r (TREE_TYPE (ssa_name (x
)));
560 if (get_global_range (r
, ssa_name (x
)) && !r
.varying_p ())
564 /* Print the header only when there's something else
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
);
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.
597 bool current_p (tree name
, tree dep1
, tree dep2
) const;
598 void set_timestamp (tree name
);
599 void set_always_current (tree name
);
601 unsigned temporal_value (unsigned ssa
) const;
603 unsigned m_current_time
;
604 vec
<unsigned> m_timestamp
;
608 temporal_cache::temporal_cache ()
611 m_timestamp
.create (0);
612 m_timestamp
.safe_grow_cleared (num_ssa_names
);
616 temporal_cache::~temporal_cache ()
618 m_timestamp
.release ();
621 // Return the timestamp value for SSA, or 0 if there isn't one.
624 temporal_cache::temporal_value (unsigned ssa
) const
626 if (ssa
>= m_timestamp
.length ())
628 return m_timestamp
[ssa
];
631 // Return TRUE if the timestamp for NAME is newer than any of its dependents.
632 // Up to 2 dependencies can be checked.
635 temporal_cache::current_p (tree name
, tree dep1
, tree dep2
) const
637 unsigned ts
= temporal_value (SSA_NAME_VERSION (name
));
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
)))
646 if (dep2
&& ts
< temporal_value (SSA_NAME_VERSION (dep2
)))
652 // This increments the global timer and sets the timestamp for NAME.
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".
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);
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.
690 void add (basic_block bb
);
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
); }
697 vec
<int> m_update_list
;
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);
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.
723 update_list::add (basic_block bb
)
726 // If propagation has failed for BB, or its already in the list, don't
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
))
735 m_update_list
[i
] = -1;
739 gcc_checking_assert (m_update_head
> 0);
740 m_update_list
[i
] = m_update_head
;
746 // Remove a block from the list.
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;
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 ();
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
);
785 m_update
= new update_list ();
788 ranger_cache::~ranger_cache ()
794 m_workback
.release ();
797 // Dump the global caches to file F. if GORI_DUMP is true, dump the
801 ranger_cache::dump (FILE *f
)
807 // Dump the caches for basic block BB to file F.
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
);
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.
822 ranger_cache::get_global_range (vrange
&r
, tree name
) const
824 if (m_globals
.get_global_range (r
, name
))
826 gimple_range_global (r
, name
);
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.
838 ranger_cache::get_global_range (vrange
&r
, tree name
, bool ¤t_p
)
840 bool had_global
= get_global_range (r
, name
);
842 // If there was a global value, set current flag, otherwise set a value.
845 current_p
= r
.singleton_p ()
846 || m_temporal
->current_p (name
, m_gori
.depend1 (name
),
847 m_gori
.depend2 (name
));
849 m_globals
.set_global_range (name
, r
);
851 // If the existing value was not current, mark it as always current.
853 m_temporal
->set_always_current (name
);
857 // Set the global range of NAME to R and give it a timestamp.
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
));
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.
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 additional
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.
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 ());
908 gimple_range_global (r
, name
);
912 // Get the range of NAME as it occurs on entry to block BB. Use MODE for
916 ranger_cache::entry_range (vrange
&r
, tree name
, basic_block bb
,
919 if (bb
== ENTRY_BLOCK_PTR_FOR_FN (cfun
))
921 gimple_range_global (r
, name
);
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
936 ranger_cache::exit_range (vrange
&r
, tree name
, basic_block bb
,
939 if (bb
== ENTRY_BLOCK_PTR_FOR_FN (cfun
))
941 gimple_range_global (r
, name
);
945 gimple
*s
= SSA_NAME_DEF_STMT (name
);
946 basic_block def_bb
= gimple_bb (s
);
948 range_of_def (r
, name
, bb
);
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.
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))
971 // Implement range_of_expr.
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
);
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
);
987 range_of_def (r
, name
, bb
);
989 entry_range (r
, name
, bb
, RFD_NONE
);
994 // Implement range_on_edge. Always return the best available range using
995 // the current cache values.
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.
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
))
1021 gimple
*def_stmt
= SSA_NAME_DEF_STMT (name
);
1022 basic_block def_bb
= NULL
;
1024 def_bb
= gimple_bb (def_stmt
);;
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
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.
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.
1049 ranger_cache::propagate_cache (tree name
)
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
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 edge_range (e_range
, e
, name
, RFD_READ_ONLY
);
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 ())
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.
1102 m_update
->propagation_failed (bb
);
1103 if (DEBUG_RANGE_CACHE
)
1107 fprintf (dump_file
, " Cache failure to store value:");
1108 print_generic_expr (dump_file
, name
, TDF_SLIM
);
1109 fprintf (dump_file
, " ");
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 don't have a cache
1145 ranger_cache::propagate_updated_value (tree name
, basic_block bb
)
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
);
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.
1188 ranger_cache::fill_block_cache (tree name
, basic_block bb
, basic_block def_bb
)
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
))
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.
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
))
1234 // Check if the equiv has any ranges calculated.
1235 if (!m_gori
.has_edge_range_p (equiv_name
))
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
)))
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
)))
1250 if (DEBUG_RANGE_CACHE
)
1253 fprintf (dump_file
, "Checking Equivalence (");
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
))
1265 range_cast (equiv_range
, type
);
1266 if (block_result
.intersect (equiv_range
))
1268 if (DEBUG_RANGE_CACHE
)
1271 fprintf (dump_file
, "Equivalence update! : ");
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);
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.
1320 m_update
->add (node
);
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.");
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, ");
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. ");
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
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.
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
))
1399 // With the dominator set, we should be able to cheaply query
1400 // each incoming edge now and accumulate the results.
1404 Value_Range
er (TREE_TYPE (name
));
1405 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
1407 // If the predecessor is dominated by this block, then there is a back
1408 // edge, and won't provide anything useful. We'll actually end up with
1409 // VARYING as we will not resolve this node.
1410 if (dominated_by_p (CDI_DOMINATORS
, e
->src
, bb
))
1412 edge_range (er
, e
, name
, RFD_READ_ONLY
);
1415 // Set the cache in PREV_BB so it is not calculated again.
1416 m_on_entry
.set_bb_range (name
, bb
, r
);
1419 // Get the range of NAME from dominators of BB and return it in R. Search the
1420 // dominator tree based on MODE.
1423 ranger_cache::range_from_dom (vrange
&r
, tree name
, basic_block start_bb
,
1426 if (mode
== RFD_NONE
|| !dom_info_available_p (CDI_DOMINATORS
))
1429 // Search back to the definition block or entry block.
1430 basic_block def_bb
= gimple_bb (SSA_NAME_DEF_STMT (name
));
1432 def_bb
= ENTRY_BLOCK_PTR_FOR_FN (cfun
);
1435 basic_block prev_bb
= start_bb
;
1437 // Track any inferred ranges seen.
1438 Value_Range
infer (TREE_TYPE (name
));
1439 infer
.set_varying (TREE_TYPE (name
));
1441 // Range on entry to the DEF block should not be queried.
1442 gcc_checking_assert (start_bb
!= def_bb
);
1443 unsigned start_limit
= m_workback
.length ();
1445 // Default value is global range.
1446 get_global_range (r
, name
);
1448 // The dominator of EXIT_BLOCK doesn't seem to be set, so at least handle
1449 // the common single exit cases.
1450 if (start_bb
== EXIT_BLOCK_PTR_FOR_FN (cfun
) && single_pred_p (start_bb
))
1451 bb
= single_pred_edge (start_bb
)->src
;
1453 bb
= get_immediate_dominator (CDI_DOMINATORS
, start_bb
);
1455 // Search until a value is found, pushing blocks which may need calculating.
1456 for ( ; bb
; prev_bb
= bb
, bb
= get_immediate_dominator (CDI_DOMINATORS
, bb
))
1458 // Accumulate any block exit inferred ranges.
1459 m_exit
.maybe_adjust_range (infer
, name
, bb
);
1461 // This block has an outgoing range.
1462 if (m_gori
.has_edge_range_p (name
, bb
))
1463 m_workback
.quick_push (prev_bb
);
1466 // Normally join blocks don't carry any new range information on
1467 // incoming edges. If the first incoming edge to this block does
1468 // generate a range, calculate the ranges if all incoming edges
1469 // are also dominated by the dominator. (Avoids backedges which
1470 // will break the rule of moving only upward in the dominator tree).
1471 // If the first pred does not generate a range, then we will be
1472 // using the dominator range anyway, so that's all the check needed.
1473 if (EDGE_COUNT (prev_bb
->preds
) > 1
1474 && m_gori
.has_edge_range_p (name
, EDGE_PRED (prev_bb
, 0)->src
))
1478 bool all_dom
= true;
1479 FOR_EACH_EDGE (e
, ei
, prev_bb
->preds
)
1481 && !dominated_by_p (CDI_DOMINATORS
, e
->src
, bb
))
1487 m_workback
.quick_push (prev_bb
);
1494 if (m_on_entry
.get_bb_range (r
, name
, bb
))
1498 if (DEBUG_RANGE_CACHE
)
1500 fprintf (dump_file
, "CACHE: BB %d DOM query for ", start_bb
->index
);
1501 print_generic_expr (dump_file
, name
, TDF_SLIM
);
1502 fprintf (dump_file
, ", found ");
1505 fprintf (dump_file
, " at BB%d\n", bb
->index
);
1507 fprintf (dump_file
, " at function top\n");
1510 // Now process any blocks wit incoming edges that nay have adjustments.
1511 while (m_workback
.length () > start_limit
)
1513 Value_Range
er (TREE_TYPE (name
));
1514 prev_bb
= m_workback
.pop ();
1515 if (!single_pred_p (prev_bb
))
1517 // Non single pred means we need to cache a value in the dominator
1518 // so we can cheaply calculate incoming edges to this block, and
1519 // then store the resulting value. If processing mode is not
1520 // RFD_FILL, then the cache cant be stored to, so don't try.
1521 // Otherwise this becomes a quadratic timed calculation.
1522 if (mode
== RFD_FILL
)
1523 resolve_dom (r
, name
, prev_bb
);
1527 edge e
= single_pred_edge (prev_bb
);
1529 if (m_gori
.outgoing_edge_range_p (er
, e
, name
, *this))
1532 // If this is a normal edge, apply any inferred ranges.
1533 if ((e
->flags
& (EDGE_EH
| EDGE_ABNORMAL
)) == 0)
1534 m_exit
.maybe_adjust_range (r
, name
, bb
);
1536 if (DEBUG_RANGE_CACHE
)
1538 fprintf (dump_file
, "CACHE: Adjusted edge range for %d->%d : ",
1539 bb
->index
, prev_bb
->index
);
1541 fprintf (dump_file
, "\n");
1546 // Apply non-null if appropriate.
1547 if (!has_abnormal_call_or_eh_pred_edge_p (start_bb
))
1548 r
.intersect (infer
);
1550 if (DEBUG_RANGE_CACHE
)
1552 fprintf (dump_file
, "CACHE: Range for DOM returns : ");
1554 fprintf (dump_file
, "\n");
1559 // This routine will register an inferred value in block BB, and possibly
1560 // update the on-entry cache if appropriate.
1563 ranger_cache::register_inferred_value (const vrange
&ir
, tree name
,
1566 Value_Range
r (TREE_TYPE (name
));
1567 if (!m_on_entry
.get_bb_range (r
, name
, bb
))
1568 exit_range (r
, name
, bb
, RFD_READ_ONLY
);
1569 if (r
.intersect (ir
))
1571 m_on_entry
.set_bb_range (name
, bb
, r
);
1572 // If this range was invariant before, remove invariant.
1573 if (!m_gori
.has_edge_range_p (name
))
1574 m_gori
.set_range_invariant (name
, false);
1578 // This routine is used during a block walk to adjust any inferred ranges
1579 // of operands on stmt S.
1582 ranger_cache::apply_inferred_ranges (gimple
*s
)
1586 basic_block bb
= gimple_bb (s
);
1587 gimple_infer_range
infer(s
);
1588 if (infer
.num () == 0)
1591 // Do not update the on-entry cache for block ending stmts.
1592 if (stmt_ends_bb_p (s
))
1596 FOR_EACH_EDGE (e
, ei
, gimple_bb (s
)->succs
)
1597 if (!(e
->flags
& (EDGE_ABNORMAL
|EDGE_EH
)))
1603 for (unsigned x
= 0; x
< infer
.num (); x
++)
1605 tree name
= infer
.name (x
);
1606 m_exit
.add_range (name
, bb
, infer
.range (x
));
1608 register_inferred_value (infer
.range (x
), name
, bb
);