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"
34 #include "gimple-iterator.h"
35 #include "gimple-walk.h"
38 #define DEBUG_RANGE_CACHE (dump_file \
39 && (param_ranger_debug & RANGER_DEBUG_CACHE))
41 // During contructor, allocate the vector of ssa_names.
43 non_null_ref::non_null_ref ()
45 m_nn
.create (num_ssa_names
);
46 m_nn
.quick_grow_cleared (num_ssa_names
);
47 bitmap_obstack_initialize (&m_bitmaps
);
50 // Free any bitmaps which were allocated,a swell as the vector itself.
52 non_null_ref::~non_null_ref ()
54 bitmap_obstack_release (&m_bitmaps
);
58 // This routine will update NAME in BB to be nonnull if it is not already.
59 // return TRUE if the update happens.
62 non_null_ref::set_nonnull (basic_block bb
, tree name
)
64 gcc_checking_assert (gimple_range_ssa_p (name
)
65 && POINTER_TYPE_P (TREE_TYPE (name
)));
66 // Only process when its not already set.
67 if (non_null_deref_p (name
, bb
, false))
69 bitmap_set_bit (m_nn
[SSA_NAME_VERSION (name
)], bb
->index
);
73 // Return true if NAME has a non-null dereference in block bb. If this is the
74 // first query for NAME, calculate the summary first.
75 // If SEARCH_DOM is true, the search the dominator tree as well.
78 non_null_ref::non_null_deref_p (tree name
, basic_block bb
, bool search_dom
)
80 if (!POINTER_TYPE_P (TREE_TYPE (name
)))
83 unsigned v
= SSA_NAME_VERSION (name
);
84 if (v
>= m_nn
.length ())
85 m_nn
.safe_grow_cleared (num_ssa_names
+ 1);
90 if (bitmap_bit_p (m_nn
[v
], bb
->index
))
93 // See if any dominator has set non-zero.
94 if (search_dom
&& dom_info_available_p (CDI_DOMINATORS
))
96 // Search back to the Def block, or the top, whichever is closer.
97 basic_block def_bb
= gimple_bb (SSA_NAME_DEF_STMT (name
));
98 basic_block def_dom
= def_bb
99 ? get_immediate_dominator (CDI_DOMINATORS
, def_bb
)
103 bb
= get_immediate_dominator (CDI_DOMINATORS
, bb
))
104 if (bitmap_bit_p (m_nn
[v
], bb
->index
))
110 // Allocate an populate the bitmap for NAME. An ON bit for a block
111 // index indicates there is a non-null reference in that block. In
112 // order to populate the bitmap, a quick run of all the immediate uses
113 // are made and the statement checked to see if a non-null dereference
114 // is made on that statement.
117 non_null_ref::process_name (tree name
)
119 unsigned v
= SSA_NAME_VERSION (name
);
121 imm_use_iterator iter
;
124 // Only tracked for pointers.
125 if (!POINTER_TYPE_P (TREE_TYPE (name
)))
128 // Already processed if a bitmap has been allocated.
132 b
= BITMAP_ALLOC (&m_bitmaps
);
134 // Loop over each immediate use and see if it implies a non-null value.
135 FOR_EACH_IMM_USE_FAST (use_p
, iter
, name
)
137 gimple
*s
= USE_STMT (use_p
);
138 unsigned index
= gimple_bb (s
)->index
;
140 // If bit is already set for this block, dont bother looking again.
141 if (bitmap_bit_p (b
, index
))
144 // If we can infer a nonnull range, then set the bit for this BB
145 if (!SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name
)
146 && infer_nonnull_range (s
, name
))
147 bitmap_set_bit (b
, index
);
153 // -------------------------------------------------------------------------
155 // This class represents the API into a cache of ranges for an SSA_NAME.
156 // Routines must be implemented to set, get, and query if a value is set.
158 class ssa_block_ranges
161 virtual bool set_bb_range (const_basic_block bb
, const irange
&r
) = 0;
162 virtual bool get_bb_range (irange
&r
, const_basic_block bb
) = 0;
163 virtual bool bb_range_p (const_basic_block bb
) = 0;
168 // Print the list of known ranges for file F in a nice format.
171 ssa_block_ranges::dump (FILE *f
)
176 FOR_EACH_BB_FN (bb
, cfun
)
177 if (get_bb_range (r
, bb
))
179 fprintf (f
, "BB%d -> ", bb
->index
);
185 // This class implements the range cache as a linear vector, indexed by BB.
186 // It caches a varying and undefined range which are used instead of
187 // allocating new ones each time.
189 class sbr_vector
: public ssa_block_ranges
192 sbr_vector (tree t
, irange_allocator
*allocator
);
194 virtual bool set_bb_range (const_basic_block bb
, const irange
&r
) OVERRIDE
;
195 virtual bool get_bb_range (irange
&r
, const_basic_block bb
) OVERRIDE
;
196 virtual bool bb_range_p (const_basic_block bb
) OVERRIDE
;
198 irange
**m_tab
; // Non growing vector.
200 int_range
<2> m_varying
;
201 int_range
<2> m_undefined
;
203 irange_allocator
*m_irange_allocator
;
208 // Initialize a block cache for an ssa_name of type T.
210 sbr_vector::sbr_vector (tree t
, irange_allocator
*allocator
)
212 gcc_checking_assert (TYPE_P (t
));
214 m_irange_allocator
= allocator
;
215 m_tab_size
= last_basic_block_for_fn (cfun
) + 1;
216 m_tab
= (irange
**)allocator
->get_memory (m_tab_size
* sizeof (irange
*));
217 memset (m_tab
, 0, m_tab_size
* sizeof (irange
*));
219 // Create the cached type range.
220 m_varying
.set_varying (t
);
221 m_undefined
.set_undefined ();
224 // Grow the vector when the CFG has increased in size.
229 int curr_bb_size
= last_basic_block_for_fn (cfun
);
230 gcc_checking_assert (curr_bb_size
> m_tab_size
);
232 // Increase the max of a)128, b)needed increase * 2, c)10% of current_size.
233 int inc
= MAX ((curr_bb_size
- m_tab_size
) * 2, 128);
234 inc
= MAX (inc
, curr_bb_size
/ 10);
235 int new_size
= inc
+ curr_bb_size
;
237 // Allocate new memory, copy the old vector and clear the new space.
238 irange
**t
= (irange
**)m_irange_allocator
->get_memory (new_size
239 * sizeof (irange
*));
240 memcpy (t
, m_tab
, m_tab_size
* sizeof (irange
*));
241 memset (t
+ m_tab_size
, 0, (new_size
- m_tab_size
) * sizeof (irange
*));
244 m_tab_size
= new_size
;
247 // Set the range for block BB to be R.
250 sbr_vector::set_bb_range (const_basic_block bb
, const irange
&r
)
253 if (bb
->index
>= m_tab_size
)
257 else if (r
.undefined_p ())
260 m
= m_irange_allocator
->allocate (r
);
261 m_tab
[bb
->index
] = m
;
265 // Return the range associated with block BB in R. Return false if
266 // there is no range.
269 sbr_vector::get_bb_range (irange
&r
, const_basic_block bb
)
271 if (bb
->index
>= m_tab_size
)
273 irange
*m
= m_tab
[bb
->index
];
282 // Return true if a range is present.
285 sbr_vector::bb_range_p (const_basic_block bb
)
287 if (bb
->index
< m_tab_size
)
288 return m_tab
[bb
->index
] != NULL
;
292 // This class implements the on entry cache via a sparse bitmap.
293 // It uses the quad bit routines to access 4 bits at a time.
294 // A value of 0 (the default) means there is no entry, and a value of
295 // 1 thru SBR_NUM represents an element in the m_range vector.
296 // Varying is given the first value (1) and pre-cached.
297 // SBR_NUM + 1 represents the value of UNDEFINED, and is never stored.
298 // SBR_NUM is the number of values that can be cached.
299 // Indexes are 1..SBR_NUM and are stored locally at m_range[0..SBR_NUM-1]
302 #define SBR_UNDEF SBR_NUM + 1
303 #define SBR_VARYING 1
305 class sbr_sparse_bitmap
: public ssa_block_ranges
308 sbr_sparse_bitmap (tree t
, irange_allocator
*allocator
, bitmap_obstack
*bm
);
309 virtual bool set_bb_range (const_basic_block bb
, const irange
&r
) OVERRIDE
;
310 virtual bool get_bb_range (irange
&r
, const_basic_block bb
) OVERRIDE
;
311 virtual bool bb_range_p (const_basic_block bb
) OVERRIDE
;
313 void bitmap_set_quad (bitmap head
, int quad
, int quad_value
);
314 int bitmap_get_quad (const_bitmap head
, int quad
);
315 irange_allocator
*m_irange_allocator
;
316 irange
*m_range
[SBR_NUM
];
321 // Initialize a block cache for an ssa_name of type T.
323 sbr_sparse_bitmap::sbr_sparse_bitmap (tree t
, irange_allocator
*allocator
,
326 gcc_checking_assert (TYPE_P (t
));
328 bitmap_initialize (&bitvec
, bm
);
329 bitmap_tree_view (&bitvec
);
330 m_irange_allocator
= allocator
;
331 // Pre-cache varying.
332 m_range
[0] = m_irange_allocator
->allocate (2);
333 m_range
[0]->set_varying (t
);
334 // Pre-cache zero and non-zero values for pointers.
335 if (POINTER_TYPE_P (t
))
337 m_range
[1] = m_irange_allocator
->allocate (2);
338 m_range
[1]->set_nonzero (t
);
339 m_range
[2] = m_irange_allocator
->allocate (2);
340 m_range
[2]->set_zero (t
);
343 m_range
[1] = m_range
[2] = NULL
;
344 // Clear SBR_NUM entries.
345 for (int x
= 3; x
< SBR_NUM
; x
++)
349 // Set 4 bit values in a sparse bitmap. This allows a bitmap to
350 // function as a sparse array of 4 bit values.
351 // QUAD is the index, QUAD_VALUE is the 4 bit value to set.
354 sbr_sparse_bitmap::bitmap_set_quad (bitmap head
, int quad
, int quad_value
)
356 bitmap_set_aligned_chunk (head
, quad
, 4, (BITMAP_WORD
) quad_value
);
359 // Get a 4 bit value from a sparse bitmap. This allows a bitmap to
360 // function as a sparse array of 4 bit values.
361 // QUAD is the index.
363 sbr_sparse_bitmap::bitmap_get_quad (const_bitmap head
, int quad
)
365 return (int) bitmap_get_aligned_chunk (head
, quad
, 4);
368 // Set the range on entry to basic block BB to R.
371 sbr_sparse_bitmap::set_bb_range (const_basic_block bb
, const irange
&r
)
373 if (r
.undefined_p ())
375 bitmap_set_quad (&bitvec
, bb
->index
, SBR_UNDEF
);
379 // Loop thru the values to see if R is already present.
380 for (int x
= 0; x
< SBR_NUM
; x
++)
381 if (!m_range
[x
] || r
== *(m_range
[x
]))
384 m_range
[x
] = m_irange_allocator
->allocate (r
);
385 bitmap_set_quad (&bitvec
, bb
->index
, x
+ 1);
388 // All values are taken, default to VARYING.
389 bitmap_set_quad (&bitvec
, bb
->index
, SBR_VARYING
);
393 // Return the range associated with block BB in R. Return false if
394 // there is no range.
397 sbr_sparse_bitmap::get_bb_range (irange
&r
, const_basic_block bb
)
399 int value
= bitmap_get_quad (&bitvec
, bb
->index
);
404 gcc_checking_assert (value
<= SBR_UNDEF
);
405 if (value
== SBR_UNDEF
)
408 r
= *(m_range
[value
- 1]);
412 // Return true if a range is present.
415 sbr_sparse_bitmap::bb_range_p (const_basic_block bb
)
417 return (bitmap_get_quad (&bitvec
, bb
->index
) != 0);
420 // -------------------------------------------------------------------------
422 // Initialize the block cache.
424 block_range_cache::block_range_cache ()
426 bitmap_obstack_initialize (&m_bitmaps
);
427 m_ssa_ranges
.create (0);
428 m_ssa_ranges
.safe_grow_cleared (num_ssa_names
);
429 m_irange_allocator
= new irange_allocator
;
432 // Remove any m_block_caches which have been created.
434 block_range_cache::~block_range_cache ()
436 delete m_irange_allocator
;
437 // Release the vector itself.
438 m_ssa_ranges
.release ();
439 bitmap_obstack_release (&m_bitmaps
);
442 // Set the range for NAME on entry to block BB to R.
443 // If it has not been accessed yet, allocate it first.
446 block_range_cache::set_bb_range (tree name
, const_basic_block bb
,
449 unsigned v
= SSA_NAME_VERSION (name
);
450 if (v
>= m_ssa_ranges
.length ())
451 m_ssa_ranges
.safe_grow_cleared (num_ssa_names
+ 1);
453 if (!m_ssa_ranges
[v
])
455 // Use sparse representation if there are too many basic blocks.
456 if (last_basic_block_for_fn (cfun
) > param_evrp_sparse_threshold
)
458 void *r
= m_irange_allocator
->get_memory (sizeof (sbr_sparse_bitmap
));
459 m_ssa_ranges
[v
] = new (r
) sbr_sparse_bitmap (TREE_TYPE (name
),
465 // Otherwise use the default vector implemntation.
466 void *r
= m_irange_allocator
->get_memory (sizeof (sbr_vector
));
467 m_ssa_ranges
[v
] = new (r
) sbr_vector (TREE_TYPE (name
),
471 return m_ssa_ranges
[v
]->set_bb_range (bb
, r
);
475 // Return a pointer to the ssa_block_cache for NAME. If it has not been
476 // accessed yet, return NULL.
478 inline ssa_block_ranges
*
479 block_range_cache::query_block_ranges (tree name
)
481 unsigned v
= SSA_NAME_VERSION (name
);
482 if (v
>= m_ssa_ranges
.length () || !m_ssa_ranges
[v
])
484 return m_ssa_ranges
[v
];
489 // Return the range for NAME on entry to BB in R. Return true if there
493 block_range_cache::get_bb_range (irange
&r
, tree name
, const_basic_block bb
)
495 ssa_block_ranges
*ptr
= query_block_ranges (name
);
497 return ptr
->get_bb_range (r
, bb
);
501 // Return true if NAME has a range set in block BB.
504 block_range_cache::bb_range_p (tree name
, const_basic_block bb
)
506 ssa_block_ranges
*ptr
= query_block_ranges (name
);
508 return ptr
->bb_range_p (bb
);
512 // Print all known block caches to file F.
515 block_range_cache::dump (FILE *f
)
518 for (x
= 0; x
< m_ssa_ranges
.length (); ++x
)
522 fprintf (f
, " Ranges for ");
523 print_generic_expr (f
, ssa_name (x
), TDF_NONE
);
525 m_ssa_ranges
[x
]->dump (f
);
531 // Print all known ranges on entry to blobk BB to file F.
534 block_range_cache::dump (FILE *f
, basic_block bb
, bool print_varying
)
538 bool summarize_varying
= false;
539 for (x
= 1; x
< m_ssa_ranges
.length (); ++x
)
541 if (!gimple_range_ssa_p (ssa_name (x
)))
543 if (m_ssa_ranges
[x
] && m_ssa_ranges
[x
]->get_bb_range (r
, bb
))
545 if (!print_varying
&& r
.varying_p ())
547 summarize_varying
= true;
550 print_generic_expr (f
, ssa_name (x
), TDF_NONE
);
556 // If there were any varying entries, lump them all together.
557 if (summarize_varying
)
559 fprintf (f
, "VARYING_P on entry : ");
560 for (x
= 1; x
< num_ssa_names
; ++x
)
562 if (!gimple_range_ssa_p (ssa_name (x
)))
564 if (m_ssa_ranges
[x
] && m_ssa_ranges
[x
]->get_bb_range (r
, bb
))
568 print_generic_expr (f
, ssa_name (x
), TDF_NONE
);
577 // -------------------------------------------------------------------------
579 // Initialize a global cache.
581 ssa_global_cache::ssa_global_cache ()
584 m_irange_allocator
= new irange_allocator
;
587 // Deconstruct a global cache.
589 ssa_global_cache::~ssa_global_cache ()
592 delete m_irange_allocator
;
595 // Retrieve the global range of NAME from cache memory if it exists.
596 // Return the value in R.
599 ssa_global_cache::get_global_range (irange
&r
, tree name
) const
601 unsigned v
= SSA_NAME_VERSION (name
);
602 if (v
>= m_tab
.length ())
605 irange
*stow
= m_tab
[v
];
612 // Set the range for NAME to R in the global cache.
613 // Return TRUE if there was already a range set, otherwise false.
616 ssa_global_cache::set_global_range (tree name
, const irange
&r
)
618 unsigned v
= SSA_NAME_VERSION (name
);
619 if (v
>= m_tab
.length ())
620 m_tab
.safe_grow_cleared (num_ssa_names
+ 1);
622 irange
*m
= m_tab
[v
];
623 if (m
&& m
->fits_p (r
))
626 m_tab
[v
] = m_irange_allocator
->allocate (r
);
630 // Set the range for NAME to R in the glonbal cache.
633 ssa_global_cache::clear_global_range (tree name
)
635 unsigned v
= SSA_NAME_VERSION (name
);
636 if (v
>= m_tab
.length ())
637 m_tab
.safe_grow_cleared (num_ssa_names
+ 1);
641 // Clear the global cache.
644 ssa_global_cache::clear ()
646 if (m_tab
.address ())
647 memset (m_tab
.address(), 0, m_tab
.length () * sizeof (irange
*));
650 // Dump the contents of the global cache to F.
653 ssa_global_cache::dump (FILE *f
)
655 /* Cleared after the table header has been printed. */
656 bool print_header
= true;
657 for (unsigned x
= 1; x
< num_ssa_names
; x
++)
660 if (gimple_range_ssa_p (ssa_name (x
)) &&
661 get_global_range (r
, ssa_name (x
)) && !r
.varying_p ())
665 /* Print the header only when there's something else
667 fprintf (f
, "Non-varying global ranges:\n");
668 fprintf (f
, "=========================:\n");
669 print_header
= false;
672 print_generic_expr (f
, ssa_name (x
), TDF_NONE
);
683 // --------------------------------------------------------------------------
686 // This class will manage the timestamps for each ssa_name.
687 // When a value is calculated, the timestamp is set to the current time.
688 // Current time is then incremented. Any dependencies will already have
689 // been calculated, and will thus have older timestamps.
690 // If one of those values is ever calculated again, it will get a newer
691 // timestamp, and the "current_p" check will fail.
698 bool current_p (tree name
, tree dep1
, tree dep2
) const;
699 void set_timestamp (tree name
);
700 void set_always_current (tree name
);
702 unsigned temporal_value (unsigned ssa
) const;
704 unsigned m_current_time
;
705 vec
<unsigned> m_timestamp
;
709 temporal_cache::temporal_cache ()
712 m_timestamp
.create (0);
713 m_timestamp
.safe_grow_cleared (num_ssa_names
);
717 temporal_cache::~temporal_cache ()
719 m_timestamp
.release ();
722 // Return the timestamp value for SSA, or 0 if there isnt one.
725 temporal_cache::temporal_value (unsigned ssa
) const
727 if (ssa
>= m_timestamp
.length ())
729 return m_timestamp
[ssa
];
732 // Return TRUE if the timestampe for NAME is newer than any of its dependents.
733 // Up to 2 dependencies can be checked.
736 temporal_cache::current_p (tree name
, tree dep1
, tree dep2
) const
738 unsigned ts
= temporal_value (SSA_NAME_VERSION (name
));
742 // Any non-registered dependencies will have a value of 0 and thus be older.
743 // Return true if time is newer than either dependent.
745 if (dep1
&& ts
< temporal_value (SSA_NAME_VERSION (dep1
)))
747 if (dep2
&& ts
< temporal_value (SSA_NAME_VERSION (dep2
)))
753 // This increments the global timer and sets the timestamp for NAME.
756 temporal_cache::set_timestamp (tree name
)
758 unsigned v
= SSA_NAME_VERSION (name
);
759 if (v
>= m_timestamp
.length ())
760 m_timestamp
.safe_grow_cleared (num_ssa_names
+ 20);
761 m_timestamp
[v
] = ++m_current_time
;
764 // Set the timestamp to 0, marking it as "always up to date".
767 temporal_cache::set_always_current (tree name
)
769 unsigned v
= SSA_NAME_VERSION (name
);
770 if (v
>= m_timestamp
.length ())
771 m_timestamp
.safe_grow_cleared (num_ssa_names
+ 20);
775 // --------------------------------------------------------------------------
777 // This class provides an abstraction of a list of blocks to be updated
778 // by the cache. It is currently a stack but could be changed. It also
779 // maintains a list of blocks which have failed propagation, and does not
780 // enter any of those blocks into the list.
782 // A vector over the BBs is maintained, and an entry of 0 means it is not in
783 // a list. Otherwise, the entry is the next block in the list. -1 terminates
784 // the list. m_head points to the top of the list, -1 if the list is empty.
791 void add (basic_block bb
);
793 inline bool empty_p () { return m_update_head
== -1; }
794 inline void clear_failures () { bitmap_clear (m_propfail
); }
795 inline void propagation_failed (basic_block bb
)
796 { bitmap_set_bit (m_propfail
, bb
->index
); }
798 vec
<int> m_update_list
;
803 // Create an update list.
805 update_list::update_list ()
807 m_update_list
.create (0);
808 m_update_list
.safe_grow_cleared (last_basic_block_for_fn (cfun
) + 64);
810 m_propfail
= BITMAP_ALLOC (NULL
);
813 // Destroy an update list.
815 update_list::~update_list ()
817 m_update_list
.release ();
818 BITMAP_FREE (m_propfail
);
821 // Add BB to the list of blocks to update, unless it's already in the list.
824 update_list::add (basic_block bb
)
827 // If propagation has failed for BB, or its already in the list, don't
829 if ((unsigned)i
>= m_update_list
.length ())
830 m_update_list
.safe_grow_cleared (i
+ 64);
831 if (!m_update_list
[i
] && !bitmap_bit_p (m_propfail
, i
))
836 m_update_list
[i
] = -1;
840 gcc_checking_assert (m_update_head
> 0);
841 m_update_list
[i
] = m_update_head
;
847 // Remove a block from the list.
852 gcc_checking_assert (!empty_p ());
853 basic_block bb
= BASIC_BLOCK_FOR_FN (cfun
, m_update_head
);
854 int pop
= m_update_head
;
855 m_update_head
= m_update_list
[pop
];
856 m_update_list
[pop
] = 0;
860 // --------------------------------------------------------------------------
862 ranger_cache::ranger_cache (int not_executable_flag
)
863 : m_gori (not_executable_flag
)
865 m_workback
.create (0);
866 m_workback
.safe_grow_cleared (last_basic_block_for_fn (cfun
));
867 m_temporal
= new temporal_cache
;
868 // If DOM info is available, spawn an oracle as well.
869 if (dom_info_available_p (CDI_DOMINATORS
))
870 m_oracle
= new dom_oracle ();
874 unsigned x
, lim
= last_basic_block_for_fn (cfun
);
875 // Calculate outgoing range info upfront. This will fully populate the
876 // m_maybe_variant bitmap which will help eliminate processing of names
877 // which never have their ranges adjusted.
878 for (x
= 0; x
< lim
; x
++)
880 basic_block bb
= BASIC_BLOCK_FOR_FN (cfun
, x
);
884 m_update
= new update_list ();
887 ranger_cache::~ranger_cache ()
893 m_workback
.release ();
896 // Dump the global caches to file F. if GORI_DUMP is true, dump the
900 ranger_cache::dump (FILE *f
)
906 // Dump the caches for basic block BB to file F.
909 ranger_cache::dump_bb (FILE *f
, basic_block bb
)
911 m_gori
.gori_map::dump (f
, bb
, false);
912 m_on_entry
.dump (f
, bb
);
914 m_oracle
->dump (f
, bb
);
917 // Get the global range for NAME, and return in R. Return false if the
918 // global range is not set, and return the legacy global value in R.
921 ranger_cache::get_global_range (irange
&r
, tree name
) const
923 if (m_globals
.get_global_range (r
, name
))
925 r
= gimple_range_global (name
);
929 // Get the global range for NAME, and return in R. Return false if the
930 // global range is not set, and R will contain the legacy global value.
931 // CURRENT_P is set to true if the value was in cache and not stale.
932 // Otherwise, set CURRENT_P to false and mark as it always current.
933 // If the global cache did not have a value, initialize it as well.
934 // After this call, the global cache will have a value.
937 ranger_cache::get_global_range (irange
&r
, tree name
, bool ¤t_p
)
939 bool had_global
= get_global_range (r
, name
);
941 // If there was a global value, set current flag, otherwise set a value.
944 current_p
= r
.singleton_p ()
945 || m_temporal
->current_p (name
, m_gori
.depend1 (name
),
946 m_gori
.depend2 (name
));
948 m_globals
.set_global_range (name
, r
);
950 // If the existing value was not current, mark it as always current.
952 m_temporal
->set_always_current (name
);
956 // Set the global range of NAME to R and give it a timestamp.
959 ranger_cache::set_global_range (tree name
, const irange
&r
)
961 if (m_globals
.set_global_range (name
, r
))
963 // If there was already a range set, propagate the new value.
964 basic_block bb
= gimple_bb (SSA_NAME_DEF_STMT (name
));
966 bb
= ENTRY_BLOCK_PTR_FOR_FN (cfun
);
968 if (DEBUG_RANGE_CACHE
)
969 fprintf (dump_file
, " GLOBAL :");
971 propagate_updated_value (name
, bb
);
973 // Constants no longer need to tracked. Any further refinement has to be
974 // undefined. Propagation works better with constants. PR 100512.
975 // Pointers which resolve to non-zero also do not need
976 // tracking in the cache as they will never change. See PR 98866.
977 // Timestamp must always be updated, or dependent calculations may
978 // not include this latest value. PR 100774.
981 || (POINTER_TYPE_P (TREE_TYPE (name
)) && r
.nonzero_p ()))
982 m_gori
.set_range_invariant (name
);
983 m_temporal
->set_timestamp (name
);
986 // Provide lookup for the gori-computes class to access the best known range
987 // of an ssa_name in any given basic block. Note, this does no additonal
988 // lookups, just accesses the data that is already known.
990 // Get the range of NAME when the def occurs in block BB. If BB is NULL
991 // get the best global value available.
994 ranger_cache::range_of_def (irange
&r
, tree name
, basic_block bb
)
996 gcc_checking_assert (gimple_range_ssa_p (name
));
997 gcc_checking_assert (!bb
|| bb
== gimple_bb (SSA_NAME_DEF_STMT (name
)));
999 // Pick up the best global range available.
1000 if (!m_globals
.get_global_range (r
, name
))
1002 // If that fails, try to calculate the range using just global values.
1003 gimple
*s
= SSA_NAME_DEF_STMT (name
);
1004 if (gimple_get_lhs (s
) == name
)
1005 fold_range (r
, s
, get_global_range_query ());
1007 r
= gimple_range_global (name
);
1011 // Get the range of NAME as it occurs on entry to block BB.
1014 ranger_cache::entry_range (irange
&r
, tree name
, basic_block bb
)
1016 if (bb
== ENTRY_BLOCK_PTR_FOR_FN (cfun
))
1018 r
= gimple_range_global (name
);
1022 // Look for the on-entry value of name in BB from the cache.
1023 // Otherwise pick up the best available global value.
1024 if (!m_on_entry
.get_bb_range (r
, name
, bb
))
1025 range_of_def (r
, name
);
1028 // Get the range of NAME as it occurs on exit from block BB.
1031 ranger_cache::exit_range (irange
&r
, tree name
, basic_block bb
)
1033 if (bb
== ENTRY_BLOCK_PTR_FOR_FN (cfun
))
1035 r
= gimple_range_global (name
);
1039 gimple
*s
= SSA_NAME_DEF_STMT (name
);
1040 basic_block def_bb
= gimple_bb (s
);
1042 range_of_def (r
, name
, bb
);
1044 entry_range (r
, name
, bb
);
1048 // Implement range_of_expr.
1051 ranger_cache::range_of_expr (irange
&r
, tree name
, gimple
*stmt
)
1053 if (!gimple_range_ssa_p (name
))
1055 get_tree_range (r
, name
, stmt
);
1059 basic_block bb
= gimple_bb (stmt
);
1060 gimple
*def_stmt
= SSA_NAME_DEF_STMT (name
);
1061 basic_block def_bb
= gimple_bb (def_stmt
);
1064 range_of_def (r
, name
, bb
);
1066 entry_range (r
, name
, bb
);
1071 // Implement range_on_edge. Always return the best available range.
1074 ranger_cache::range_on_edge (irange
&r
, edge e
, tree expr
)
1076 if (gimple_range_ssa_p (expr
))
1078 exit_range (r
, expr
, e
->src
);
1079 // If this is not an abnormal edge, check for a non-null exit.
1080 if ((e
->flags
& (EDGE_EH
| EDGE_ABNORMAL
)) == 0)
1081 m_non_null
.adjust_range (r
, expr
, e
->src
, false);
1082 int_range_max edge_range
;
1083 if (m_gori
.outgoing_edge_range_p (edge_range
, e
, expr
, *this))
1084 r
.intersect (edge_range
);
1088 return get_tree_range (r
, expr
, NULL
);
1092 // Return a static range for NAME on entry to basic block BB in R. If
1093 // calc is true, fill any cache entries required between BB and the
1094 // def block for NAME. Otherwise, return false if the cache is empty.
1097 ranger_cache::block_range (irange
&r
, basic_block bb
, tree name
, bool calc
)
1099 gcc_checking_assert (gimple_range_ssa_p (name
));
1101 // If there are no range calculations anywhere in the IL, global range
1102 // applies everywhere, so don't bother caching it.
1103 if (!m_gori
.has_edge_range_p (name
))
1108 gimple
*def_stmt
= SSA_NAME_DEF_STMT (name
);
1109 basic_block def_bb
= NULL
;
1111 def_bb
= gimple_bb (def_stmt
);;
1114 // If we get to the entry block, this better be a default def
1115 // or range_on_entry was called for a block not dominated by
1117 gcc_checking_assert (SSA_NAME_IS_DEFAULT_DEF (name
));
1118 def_bb
= ENTRY_BLOCK_PTR_FOR_FN (cfun
);
1121 // There is no range on entry for the definition block.
1125 // Otherwise, go figure out what is known in predecessor blocks.
1126 fill_block_cache (name
, bb
, def_bb
);
1127 gcc_checking_assert (m_on_entry
.bb_range_p (name
, bb
));
1129 return m_on_entry
.get_bb_range (r
, name
, bb
);
1132 // If there is anything in the propagation update_list, continue
1133 // processing NAME until the list of blocks is empty.
1136 ranger_cache::propagate_cache (tree name
)
1141 int_range_max new_range
;
1142 int_range_max current_range
;
1143 int_range_max e_range
;
1145 // Process each block by seeing if its calculated range on entry is
1146 // the same as its cached value. If there is a difference, update
1147 // the cache to reflect the new value, and check to see if any
1148 // successors have cache entries which may need to be checked for
1151 while (!m_update
->empty_p ())
1153 bb
= m_update
->pop ();
1154 gcc_checking_assert (m_on_entry
.bb_range_p (name
, bb
));
1155 m_on_entry
.get_bb_range (current_range
, name
, bb
);
1157 if (DEBUG_RANGE_CACHE
)
1159 fprintf (dump_file
, "FWD visiting block %d for ", bb
->index
);
1160 print_generic_expr (dump_file
, name
, TDF_SLIM
);
1161 fprintf (dump_file
, " starting range : ");
1162 current_range
.dump (dump_file
);
1163 fprintf (dump_file
, "\n");
1166 // Calculate the "new" range on entry by unioning the pred edges.
1167 new_range
.set_undefined ();
1168 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
1170 range_on_edge (e_range
, e
, name
);
1171 if (DEBUG_RANGE_CACHE
)
1173 fprintf (dump_file
, " edge %d->%d :", e
->src
->index
, bb
->index
);
1174 e_range
.dump (dump_file
);
1175 fprintf (dump_file
, "\n");
1177 new_range
.union_ (e_range
);
1178 if (new_range
.varying_p ())
1182 // If the range on entry has changed, update it.
1183 if (new_range
!= current_range
)
1185 bool ok_p
= m_on_entry
.set_bb_range (name
, bb
, new_range
);
1186 // If the cache couldn't set the value, mark it as failed.
1188 m_update
->propagation_failed (bb
);
1189 if (DEBUG_RANGE_CACHE
)
1193 fprintf (dump_file
, " Cache failure to store value:");
1194 print_generic_expr (dump_file
, name
, TDF_SLIM
);
1195 fprintf (dump_file
, " ");
1199 fprintf (dump_file
, " Updating range to ");
1200 new_range
.dump (dump_file
);
1202 fprintf (dump_file
, "\n Updating blocks :");
1204 // Mark each successor that has a range to re-check its range
1205 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
1206 if (m_on_entry
.bb_range_p (name
, e
->dest
))
1208 if (DEBUG_RANGE_CACHE
)
1209 fprintf (dump_file
, " bb%d",e
->dest
->index
);
1210 m_update
->add (e
->dest
);
1212 if (DEBUG_RANGE_CACHE
)
1213 fprintf (dump_file
, "\n");
1216 if (DEBUG_RANGE_CACHE
)
1218 fprintf (dump_file
, "DONE visiting blocks for ");
1219 print_generic_expr (dump_file
, name
, TDF_SLIM
);
1220 fprintf (dump_file
, "\n");
1222 m_update
->clear_failures ();
1225 // Check to see if an update to the value for NAME in BB has any effect
1226 // on values already in the on-entry cache for successor blocks.
1227 // If it does, update them. Don't visit any blocks which dont have a cache
1231 ranger_cache::propagate_updated_value (tree name
, basic_block bb
)
1236 // The update work list should be empty at this point.
1237 gcc_checking_assert (m_update
->empty_p ());
1238 gcc_checking_assert (bb
);
1240 if (DEBUG_RANGE_CACHE
)
1242 fprintf (dump_file
, " UPDATE cache for ");
1243 print_generic_expr (dump_file
, name
, TDF_SLIM
);
1244 fprintf (dump_file
, " in BB %d : successors : ", bb
->index
);
1246 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
1248 // Only update active cache entries.
1249 if (m_on_entry
.bb_range_p (name
, e
->dest
))
1251 m_update
->add (e
->dest
);
1252 if (DEBUG_RANGE_CACHE
)
1253 fprintf (dump_file
, " UPDATE: bb%d", e
->dest
->index
);
1256 if (!m_update
->empty_p ())
1258 if (DEBUG_RANGE_CACHE
)
1259 fprintf (dump_file
, "\n");
1260 propagate_cache (name
);
1264 if (DEBUG_RANGE_CACHE
)
1265 fprintf (dump_file
, " : No updates!\n");
1269 // Make sure that the range-on-entry cache for NAME is set for block BB.
1270 // Work back through the CFG to DEF_BB ensuring the range is calculated
1271 // on the block/edges leading back to that point.
1274 ranger_cache::fill_block_cache (tree name
, basic_block bb
, basic_block def_bb
)
1278 int_range_max block_result
;
1279 int_range_max undefined
;
1281 // At this point we shouldn't be looking at the def, entry or exit block.
1282 gcc_checking_assert (bb
!= def_bb
&& bb
!= ENTRY_BLOCK_PTR_FOR_FN (cfun
) &&
1283 bb
!= EXIT_BLOCK_PTR_FOR_FN (cfun
));
1285 // If the block cache is set, then we've already visited this block.
1286 if (m_on_entry
.bb_range_p (name
, bb
))
1289 // Visit each block back to the DEF. Initialize each one to UNDEFINED.
1290 // m_visited at the end will contain all the blocks that we needed to set
1291 // the range_on_entry cache for.
1292 m_workback
.truncate (0);
1293 m_workback
.quick_push (bb
);
1294 undefined
.set_undefined ();
1295 m_on_entry
.set_bb_range (name
, bb
, undefined
);
1296 gcc_checking_assert (m_update
->empty_p ());
1298 if (DEBUG_RANGE_CACHE
)
1300 fprintf (dump_file
, "\n");
1301 print_generic_expr (dump_file
, name
, TDF_SLIM
);
1302 fprintf (dump_file
, " : ");
1305 // If there are dominators, check if a dominators can supply the range.
1306 if (dom_info_available_p (CDI_DOMINATORS
)
1307 && range_from_dom (block_result
, name
, bb
))
1309 m_on_entry
.set_bb_range (name
, bb
, block_result
);
1310 if (DEBUG_RANGE_CACHE
)
1312 fprintf (dump_file
, "Filled from dominator! : ");
1313 block_result
.dump (dump_file
);
1314 fprintf (dump_file
, "\n");
1319 while (m_workback
.length () > 0)
1321 basic_block node
= m_workback
.pop ();
1322 if (DEBUG_RANGE_CACHE
)
1324 fprintf (dump_file
, "BACK visiting block %d for ", node
->index
);
1325 print_generic_expr (dump_file
, name
, TDF_SLIM
);
1326 fprintf (dump_file
, "\n");
1329 FOR_EACH_EDGE (e
, ei
, node
->preds
)
1331 basic_block pred
= e
->src
;
1334 if (DEBUG_RANGE_CACHE
)
1335 fprintf (dump_file
, " %d->%d ",e
->src
->index
, e
->dest
->index
);
1337 // If the pred block is the def block add this BB to update list.
1340 m_update
->add (node
);
1344 // If the pred is entry but NOT def, then it is used before
1345 // defined, it'll get set to [] and no need to update it.
1346 if (pred
== ENTRY_BLOCK_PTR_FOR_FN (cfun
))
1348 if (DEBUG_RANGE_CACHE
)
1349 fprintf (dump_file
, "entry: bail.");
1353 // Regardless of whether we have visited pred or not, if the
1354 // pred has a non-null reference, revisit this block.
1355 // Don't search the DOM tree.
1356 if (m_non_null
.non_null_deref_p (name
, pred
, false))
1358 if (DEBUG_RANGE_CACHE
)
1359 fprintf (dump_file
, "nonnull: update ");
1360 m_update
->add (node
);
1363 // If the pred block already has a range, or if it can contribute
1364 // something new. Ie, the edge generates a range of some sort.
1365 if (m_on_entry
.get_bb_range (r
, name
, pred
))
1367 if (DEBUG_RANGE_CACHE
)
1369 fprintf (dump_file
, "has cache, ");
1371 fprintf (dump_file
, ", ");
1373 if (!r
.undefined_p () || m_gori
.has_edge_range_p (name
, e
))
1375 m_update
->add (node
);
1376 if (DEBUG_RANGE_CACHE
)
1377 fprintf (dump_file
, "update. ");
1382 if (DEBUG_RANGE_CACHE
)
1383 fprintf (dump_file
, "pushing undefined pred block.\n");
1384 // If the pred hasn't been visited (has no range), add it to
1386 gcc_checking_assert (!m_on_entry
.bb_range_p (name
, pred
));
1387 m_on_entry
.set_bb_range (name
, pred
, undefined
);
1388 m_workback
.quick_push (pred
);
1392 if (DEBUG_RANGE_CACHE
)
1393 fprintf (dump_file
, "\n");
1395 // Now fill in the marked blocks with values.
1396 propagate_cache (name
);
1397 if (DEBUG_RANGE_CACHE
)
1398 fprintf (dump_file
, " Propagation update done.\n");
1402 // Get the range of NAME from dominators of BB and return it in R.
1405 ranger_cache::range_from_dom (irange
&r
, tree name
, basic_block start_bb
)
1407 if (!dom_info_available_p (CDI_DOMINATORS
))
1410 // Search back to the definition block or entry block.
1411 basic_block def_bb
= gimple_bb (SSA_NAME_DEF_STMT (name
));
1413 def_bb
= ENTRY_BLOCK_PTR_FOR_FN (cfun
);
1416 basic_block prev_bb
= start_bb
;
1417 // Flag if we encounter a block with non-null set.
1418 bool non_null
= false;
1420 // Range on entry to the DEF block should not be queried.
1421 gcc_checking_assert (start_bb
!= def_bb
);
1422 m_workback
.truncate (0);
1424 // Default value is global range.
1425 get_global_range (r
, name
);
1427 // Search until a value is found, pushing outgoing edges encountered.
1428 for (bb
= get_immediate_dominator (CDI_DOMINATORS
, start_bb
);
1430 prev_bb
= bb
, bb
= get_immediate_dominator (CDI_DOMINATORS
, bb
))
1433 non_null
|= m_non_null
.non_null_deref_p (name
, bb
, false);
1435 // This block has an outgoing range.
1436 if (m_gori
.has_edge_range_p (name
, bb
))
1438 // Only outgoing ranges to single_pred blocks are dominated by
1439 // outgoing edge ranges, so only those need to be considered.
1440 edge e
= find_edge (bb
, prev_bb
);
1441 if (e
&& single_pred_p (prev_bb
))
1442 m_workback
.quick_push (prev_bb
);
1448 if (m_on_entry
.get_bb_range (r
, name
, bb
))
1452 if (DEBUG_RANGE_CACHE
)
1454 fprintf (dump_file
, "CACHE: BB %d DOM query, found ", start_bb
->index
);
1457 fprintf (dump_file
, " at BB%d\n", bb
->index
);
1459 fprintf (dump_file
, " at function top\n");
1462 // Now process any outgoing edges that we seen along the way.
1463 while (m_workback
.length () > 0)
1465 int_range_max edge_range
;
1466 prev_bb
= m_workback
.pop ();
1467 edge e
= single_pred_edge (prev_bb
);
1470 if (m_gori
.outgoing_edge_range_p (edge_range
, e
, name
, *this))
1472 r
.intersect (edge_range
);
1473 if (r
.varying_p () && ((e
->flags
& (EDGE_EH
| EDGE_ABNORMAL
)) == 0))
1475 if (m_non_null
.non_null_deref_p (name
, bb
, false))
1477 gcc_checking_assert (POINTER_TYPE_P (TREE_TYPE (name
)));
1478 r
.set_nonzero (TREE_TYPE (name
));
1481 if (DEBUG_RANGE_CACHE
)
1483 fprintf (dump_file
, "CACHE: Adjusted edge range for %d->%d : ",
1484 bb
->index
, prev_bb
->index
);
1486 fprintf (dump_file
, "\n");
1491 // Apply non-null if appropriate.
1492 if (non_null
&& r
.varying_p ()
1493 && !has_abnormal_call_or_eh_pred_edge_p (start_bb
))
1495 gcc_checking_assert (POINTER_TYPE_P (TREE_TYPE (name
)));
1496 r
.set_nonzero (TREE_TYPE (name
));
1498 if (DEBUG_RANGE_CACHE
)
1500 fprintf (dump_file
, "CACHE: Range for DOM returns : ");
1502 fprintf (dump_file
, "\n");
1507 // This routine will update NAME in block BB to the nonnull state.
1508 // It will then update the on-entry cache for this block to be non-null
1509 // if it isn't already.
1512 ranger_cache::update_to_nonnull (basic_block bb
, tree name
)
1514 tree type
= TREE_TYPE (name
);
1515 if (gimple_range_ssa_p (name
) && POINTER_TYPE_P (type
))
1517 m_non_null
.set_nonnull (bb
, name
);
1518 // Update the on-entry cache for BB to be non-zero. Note this can set
1519 // the on entry value in the DEF block, which can override the def.
1521 exit_range (r
, name
, bb
);
1524 r
.set_nonzero (type
);
1525 m_on_entry
.set_bb_range (name
, bb
, r
);
1530 // Adapted from infer_nonnull_range_by_dereference and check_loadstore
1531 // to process nonnull ssa_name OP in S. DATA contains the ranger_cache.
1534 non_null_loadstore (gimple
*s
, tree op
, tree
, void *data
)
1536 if (TREE_CODE (op
) == MEM_REF
|| TREE_CODE (op
) == TARGET_MEM_REF
)
1538 /* Some address spaces may legitimately dereference zero. */
1539 addr_space_t as
= TYPE_ADDR_SPACE (TREE_TYPE (op
));
1540 if (!targetm
.addr_space
.zero_address_valid (as
))
1542 tree ssa
= TREE_OPERAND (op
, 0);
1543 basic_block bb
= gimple_bb (s
);
1544 ((ranger_cache
*)data
)->update_to_nonnull (bb
, ssa
);
1550 // This routine is used during a block walk to move the state of non-null for
1551 // any operands on stmt S to nonnull.
1554 ranger_cache::block_apply_nonnull (gimple
*s
)
1556 if (!flag_delete_null_pointer_checks
)
1558 if (is_a
<gphi
*> (s
))
1560 if (gimple_code (s
) == GIMPLE_ASM
|| gimple_clobber_p (s
))
1562 if (is_a
<gcall
*> (s
))
1564 tree fntype
= gimple_call_fntype (s
);
1565 bitmap nonnullargs
= get_nonnull_args (fntype
);
1566 // Process any non-null arguments
1569 basic_block bb
= gimple_bb (s
);
1570 for (unsigned i
= 0; i
< gimple_call_num_args (s
); i
++)
1572 if (bitmap_empty_p (nonnullargs
) || bitmap_bit_p (nonnullargs
, i
))
1574 tree op
= gimple_call_arg (s
, i
);
1575 update_to_nonnull (bb
, op
);
1578 BITMAP_FREE (nonnullargs
);
1580 // Fallthru and walk load/store ops now.
1582 walk_stmt_load_store_ops (s
, (void *)this, non_null_loadstore
,
1583 non_null_loadstore
);