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)
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 implemntation.
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 blobk 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 glonbal 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 isnt 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 timestampe 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 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.
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 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 ())
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 dont 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 Value_Range
block_result (TREE_TYPE (name
));
1193 Value_Range
undefined (TREE_TYPE (name
));
1195 // At this point we shouldn't be looking at the def, entry or exit block.
1196 gcc_checking_assert (bb
!= def_bb
&& bb
!= ENTRY_BLOCK_PTR_FOR_FN (cfun
) &&
1197 bb
!= EXIT_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 // Query equivalences in read-only mode.
1226 const_bitmap equiv
= m_oracle
->equiv_set (name
, bb
);
1227 EXECUTE_IF_SET_IN_BITMAP (equiv
, 0, i
, bi
)
1229 if (i
== SSA_NAME_VERSION (name
))
1231 tree equiv_name
= ssa_name (i
);
1232 basic_block equiv_bb
= gimple_bb (SSA_NAME_DEF_STMT (equiv_name
));
1234 // Check if the equiv has any ranges calculated.
1235 if (!m_gori
.has_edge_range_p (equiv_name
))
1238 // Check if the equiv definition dominates this block
1239 if (equiv_bb
== bb
||
1240 (equiv_bb
&& !dominated_by_p (CDI_DOMINATORS
, bb
, equiv_bb
)))
1243 Value_Range
equiv_range (TREE_TYPE (equiv_name
));
1244 if (range_from_dom (equiv_range
, equiv_name
, bb
, RFD_READ_ONLY
))
1246 if (block_result
.intersect (equiv_range
))
1248 if (DEBUG_RANGE_CACHE
)
1250 fprintf (dump_file
, "Equivalence update! : ");
1251 print_generic_expr (dump_file
, equiv_name
, TDF_SLIM
);
1252 fprintf (dump_file
, "had range : ");
1253 equiv_range
.dump (dump_file
);
1254 fprintf (dump_file
, " refining range to :");
1255 block_result
.dump (dump_file
);
1256 fprintf (dump_file
, "\n");
1263 m_on_entry
.set_bb_range (name
, bb
, block_result
);
1264 gcc_checking_assert (m_workback
.length () == 0);
1268 // Visit each block back to the DEF. Initialize each one to UNDEFINED.
1269 // m_visited at the end will contain all the blocks that we needed to set
1270 // the range_on_entry cache for.
1271 m_workback
.quick_push (bb
);
1272 undefined
.set_undefined ();
1273 m_on_entry
.set_bb_range (name
, bb
, undefined
);
1274 gcc_checking_assert (m_update
->empty_p ());
1276 while (m_workback
.length () > 0)
1278 basic_block node
= m_workback
.pop ();
1279 if (DEBUG_RANGE_CACHE
)
1281 fprintf (dump_file
, "BACK visiting block %d for ", node
->index
);
1282 print_generic_expr (dump_file
, name
, TDF_SLIM
);
1283 fprintf (dump_file
, "\n");
1286 FOR_EACH_EDGE (e
, ei
, node
->preds
)
1288 basic_block pred
= e
->src
;
1289 Value_Range
r (TREE_TYPE (name
));
1291 if (DEBUG_RANGE_CACHE
)
1292 fprintf (dump_file
, " %d->%d ",e
->src
->index
, e
->dest
->index
);
1294 // If the pred block is the def block add this BB to update list.
1297 m_update
->add (node
);
1301 // If the pred is entry but NOT def, then it is used before
1302 // defined, it'll get set to [] and no need to update it.
1303 if (pred
== ENTRY_BLOCK_PTR_FOR_FN (cfun
))
1305 if (DEBUG_RANGE_CACHE
)
1306 fprintf (dump_file
, "entry: bail.");
1310 // Regardless of whether we have visited pred or not, if the
1311 // pred has inferred ranges, revisit this block.
1312 // Don't search the DOM tree.
1313 if (m_exit
.has_range_p (name
, pred
))
1315 if (DEBUG_RANGE_CACHE
)
1316 fprintf (dump_file
, "Inferred range: update ");
1317 m_update
->add (node
);
1320 // If the pred block already has a range, or if it can contribute
1321 // something new. Ie, the edge generates a range of some sort.
1322 if (m_on_entry
.get_bb_range (r
, name
, pred
))
1324 if (DEBUG_RANGE_CACHE
)
1326 fprintf (dump_file
, "has cache, ");
1328 fprintf (dump_file
, ", ");
1330 if (!r
.undefined_p () || m_gori
.has_edge_range_p (name
, e
))
1332 m_update
->add (node
);
1333 if (DEBUG_RANGE_CACHE
)
1334 fprintf (dump_file
, "update. ");
1339 if (DEBUG_RANGE_CACHE
)
1340 fprintf (dump_file
, "pushing undefined pred block.\n");
1341 // If the pred hasn't been visited (has no range), add it to
1343 gcc_checking_assert (!m_on_entry
.bb_range_p (name
, pred
));
1344 m_on_entry
.set_bb_range (name
, pred
, undefined
);
1345 m_workback
.quick_push (pred
);
1349 if (DEBUG_RANGE_CACHE
)
1350 fprintf (dump_file
, "\n");
1352 // Now fill in the marked blocks with values.
1353 propagate_cache (name
);
1354 if (DEBUG_RANGE_CACHE
)
1355 fprintf (dump_file
, " Propagation update done.\n");
1358 // Resolve the range of BB if the dominators range is R by calculating incoming
1359 // edges to this block. All lead back to the dominator so should be cheap.
1360 // The range for BB is set and returned in R.
1363 ranger_cache::resolve_dom (vrange
&r
, tree name
, basic_block bb
)
1365 basic_block def_bb
= gimple_bb (SSA_NAME_DEF_STMT (name
));
1366 basic_block dom_bb
= get_immediate_dominator (CDI_DOMINATORS
, bb
);
1368 // if it doesn't already have a value, store the incoming range.
1369 if (!m_on_entry
.bb_range_p (name
, dom_bb
) && def_bb
!= dom_bb
)
1371 // If the range can't be store, don't try to accumulate
1372 // the range in PREV_BB due to excessive recalculations.
1373 if (!m_on_entry
.set_bb_range (name
, dom_bb
, r
))
1376 // With the dominator set, we should be able to cheaply query
1377 // each incoming edge now and accumulate the results.
1381 Value_Range
er (TREE_TYPE (name
));
1382 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
1384 edge_range (er
, e
, name
, RFD_READ_ONLY
);
1387 // Set the cache in PREV_BB so it is not calculated again.
1388 m_on_entry
.set_bb_range (name
, bb
, r
);
1391 // Get the range of NAME from dominators of BB and return it in R. Search the
1392 // dominator tree based on MODE.
1395 ranger_cache::range_from_dom (vrange
&r
, tree name
, basic_block start_bb
,
1398 if (mode
== RFD_NONE
|| !dom_info_available_p (CDI_DOMINATORS
))
1401 // Search back to the definition block or entry block.
1402 basic_block def_bb
= gimple_bb (SSA_NAME_DEF_STMT (name
));
1404 def_bb
= ENTRY_BLOCK_PTR_FOR_FN (cfun
);
1407 basic_block prev_bb
= start_bb
;
1409 // Track any inferred ranges seen.
1410 Value_Range
infer (TREE_TYPE (name
));
1411 infer
.set_varying (TREE_TYPE (name
));
1413 // Range on entry to the DEF block should not be queried.
1414 gcc_checking_assert (start_bb
!= def_bb
);
1415 unsigned start_limit
= m_workback
.length ();
1417 // Default value is global range.
1418 get_global_range (r
, name
);
1420 // Search until a value is found, pushing blocks which may need calculating.
1421 for (bb
= get_immediate_dominator (CDI_DOMINATORS
, start_bb
);
1423 prev_bb
= bb
, bb
= get_immediate_dominator (CDI_DOMINATORS
, bb
))
1425 // Accumulate any block exit inferred ranges.
1426 m_exit
.maybe_adjust_range (infer
, name
, bb
);
1428 // This block has an outgoing range.
1429 if (m_gori
.has_edge_range_p (name
, bb
))
1430 m_workback
.quick_push (prev_bb
);
1433 // Normally join blocks don't carry any new range information on
1434 // incoming edges. If the first incoming edge to this block does
1435 // generate a range, calculate the ranges if all incoming edges
1436 // are also dominated by the dominator. (Avoids backedges which
1437 // will break the rule of moving only upward in the domniator tree).
1438 // If the first pred does not generate a range, then we will be
1439 // using the dominator range anyway, so thats all the check needed.
1440 if (EDGE_COUNT (prev_bb
->preds
) > 1
1441 && m_gori
.has_edge_range_p (name
, EDGE_PRED (prev_bb
, 0)->src
))
1445 bool all_dom
= true;
1446 FOR_EACH_EDGE (e
, ei
, prev_bb
->preds
)
1448 && !dominated_by_p (CDI_DOMINATORS
, e
->src
, bb
))
1454 m_workback
.quick_push (prev_bb
);
1461 if (m_on_entry
.get_bb_range (r
, name
, bb
))
1465 if (DEBUG_RANGE_CACHE
)
1467 fprintf (dump_file
, "CACHE: BB %d DOM query, found ", start_bb
->index
);
1470 fprintf (dump_file
, " at BB%d\n", bb
->index
);
1472 fprintf (dump_file
, " at function top\n");
1475 // Now process any blocks wit incoming edges that nay have adjustemnts.
1476 while (m_workback
.length () > start_limit
)
1478 Value_Range
er (TREE_TYPE (name
));
1479 prev_bb
= m_workback
.pop ();
1480 if (!single_pred_p (prev_bb
))
1482 // Non single pred means we need to cache a vsalue in the dominator
1483 // so we can cheaply calculate incoming edges to this block, and
1484 // then store the resulting value. If processing mode is not
1485 // RFD_FILL, then the cache cant be stored to, so don't try.
1486 // Otherwise this becomes a quadratic timed calculation.
1487 if (mode
== RFD_FILL
)
1488 resolve_dom (r
, name
, prev_bb
);
1492 edge e
= single_pred_edge (prev_bb
);
1494 if (m_gori
.outgoing_edge_range_p (er
, e
, name
, *this))
1497 // If this is a normal edge, apply any inferred ranges.
1498 if ((e
->flags
& (EDGE_EH
| EDGE_ABNORMAL
)) == 0)
1499 m_exit
.maybe_adjust_range (r
, name
, bb
);
1501 if (DEBUG_RANGE_CACHE
)
1503 fprintf (dump_file
, "CACHE: Adjusted edge range for %d->%d : ",
1504 bb
->index
, prev_bb
->index
);
1506 fprintf (dump_file
, "\n");
1511 // Apply non-null if appropriate.
1512 if (!has_abnormal_call_or_eh_pred_edge_p (start_bb
))
1513 r
.intersect (infer
);
1515 if (DEBUG_RANGE_CACHE
)
1517 fprintf (dump_file
, "CACHE: Range for DOM returns : ");
1519 fprintf (dump_file
, "\n");
1524 // This routine is used during a block walk to move the state of non-null for
1525 // any operands on stmt S to nonnull.
1528 ranger_cache::apply_inferred_ranges (gimple
*s
)
1533 basic_block bb
= gimple_bb (s
);
1534 gimple_infer_range
infer(s
);
1535 if (infer
.num () == 0)
1538 // Do not update the on-entry cache for block ending stmts.
1539 if (stmt_ends_bb_p (s
))
1543 FOR_EACH_EDGE (e
, ei
, gimple_bb (s
)->succs
)
1544 if (!(e
->flags
& (EDGE_ABNORMAL
|EDGE_EH
)))
1550 for (unsigned x
= 0; x
< infer
.num (); x
++)
1552 tree name
= infer
.name (x
);
1553 m_exit
.add_range (name
, bb
, infer
.range (x
));
1556 if (!m_on_entry
.get_bb_range (r
, name
, bb
))
1557 exit_range (r
, name
, bb
, RFD_READ_ONLY
);
1558 if (r
.intersect (infer
.range (x
)))
1560 m_on_entry
.set_bb_range (name
, bb
, r
);
1561 // If this range was invariant before, remove invariance.
1562 if (!m_gori
.has_edge_range_p (name
))
1563 m_gori
.set_range_invariant (name
, false);