1 /* Support routines for Value Range Propagation (VRP).
2 Copyright (C) 2005-2023 Free Software Foundation, Inc.
3 Contributed by Diego Novillo <dnovillo@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"
24 #include "basic-block.h"
28 #include "dominance.h"
33 #include "tree-pass.h"
35 #include "gimple-pretty-print.h"
36 #include "fold-const.h"
38 #include "gimple-iterator.h"
40 #include "tree-ssa-loop-manip.h"
41 #include "tree-ssa-loop-niter.h"
42 #include "tree-into-ssa.h"
44 #include "tree-scalar-evolution.h"
45 #include "tree-ssa-propagate.h"
47 #include "vr-values.h"
48 #include "gimple-array-bounds.h"
49 #include "gimple-range.h"
50 #include "gimple-range-path.h"
51 #include "value-pointer-equiv.h"
52 #include "gimple-fold.h"
54 #include "tree-ssa-dce.h"
55 #include "alloc-pool.h"
57 #include "symbol-summary.h"
58 #include "ipa-utils.h"
62 // This class is utilized by VRP and ranger to remove __builtin_unreachable
63 // calls, and reflect any resulting global ranges.
65 // maybe_register() is called on condition statements , and if that
66 // matches the pattern of one branch being a builtin_unreachable, either check
67 // for early removal or register the resulting executable edge in a list.
69 // During early/non-final processing, we check to see if ALL exports from the
70 // block can be safely updated with a new global value. If they can, then
71 // we rewrite the condition and update those values immediately. Otherwise
72 // the unreachable condition is left in the IL until the final pass.
74 // During final processing, after all blocks have been registered,
75 // remove_and_update_globals() will
76 // - check all exports from registered blocks
77 // - ensure the cache entry of each export is set with the appropriate range
78 // - rewrite the conditions to take the executable edge
79 // - perform DCE on any feeding instructions to those rewritten conditions
81 // Then each of the immediate use chain of each export is walked, and a new
82 // global range created by unioning the ranges at all remaining use locations.
84 class remove_unreachable
{
86 remove_unreachable (gimple_ranger
&r
, bool all
) : m_ranger (r
), final_p (all
)
87 { m_list
.create (30); }
88 ~remove_unreachable () { m_list
.release (); }
89 void handle_early (gimple
*s
, edge e
);
90 void maybe_register (gimple
*s
);
91 bool remove_and_update_globals ();
92 vec
<std::pair
<int, int> > m_list
;
93 gimple_ranger
&m_ranger
;
97 // Check if block BB has a __builtin_unreachable () call on one arm, and
98 // register the executable edge if so.
101 remove_unreachable::maybe_register (gimple
*s
)
103 gcc_checking_assert (gimple_code (s
) == GIMPLE_COND
);
104 basic_block bb
= gimple_bb (s
);
106 edge e0
= EDGE_SUCC (bb
, 0);
107 basic_block bb0
= e0
->dest
;
108 bool un0
= EDGE_COUNT (bb0
->succs
) == 0
109 && gimple_seq_unreachable_p (bb_seq (bb0
));
110 edge e1
= EDGE_SUCC (bb
, 1);
111 basic_block bb1
= e1
->dest
;
112 bool un1
= EDGE_COUNT (bb1
->succs
) == 0
113 && gimple_seq_unreachable_p (bb_seq (bb1
));
115 // If the 2 blocks are not different, ignore.
119 // Constant expressions are ignored.
120 if (TREE_CODE (gimple_cond_lhs (s
)) != SSA_NAME
121 && TREE_CODE (gimple_cond_rhs (s
)) != SSA_NAME
)
124 edge e
= un0
? e1
: e0
;
129 m_list
.safe_push (std::make_pair (e
->src
->index
, e
->dest
->index
));
132 // Return true if all uses of NAME are dominated by by block BB. 1 use
133 // is allowed in block BB, This is one we hope to remove.
137 // goto <bb 3>; [0.00%]
138 // Any additional use of _1 or _2 in this block invalidates early replacement.
141 fully_replaceable (tree name
, basic_block bb
)
144 imm_use_iterator iter
;
145 bool saw_in_bb
= false;
147 // If a name loads from memory, we may lose information used in
148 // commoning opportunities later. See tree-ssa/ssa-pre-34.c.
149 gimple
*def_stmt
= SSA_NAME_DEF_STMT (name
);
150 if (gimple_vuse (def_stmt
))
153 FOR_EACH_IMM_USE_FAST (use_p
, iter
, name
)
155 gimple
*use_stmt
= USE_STMT (use_p
);
156 // Ignore debug stmts and the branch.
157 if (is_gimple_debug (use_stmt
))
159 basic_block use_bb
= gimple_bb (use_stmt
);
160 // Only one use in the block allowed to avoid complicated cases.
168 else if (!dominated_by_p (CDI_DOMINATORS
, use_bb
, bb
))
174 // This routine is called to check builtin_unreachable calls during any
175 // time before final removal. The only way we can be sure it does not
176 // provide any additional information is to expect that we can update the
177 // global values of all exports from a block. This means the branch
178 // to the unreachable call must dominate all uses of those ssa-names, with
179 // the exception that there can be a single use in the block containing
180 // the branch. IF the name used in the branch is defined in the block, it may
181 // contain the name of something else that will be an export. And likewise
182 // that may also use another name that is an export etc.
184 // As long as there is only a single use, we can be sure that there are no other
185 // side effects (like being passed to a call, or stored to a global, etc.
186 // This means we will miss cases where there are 2 or more uses that have
187 // no interveneing statements that may had side effects, but it catches most
188 // of the caes we care about, and prevents expensive in depth analysis.
190 // Ranger will still reflect the proper ranges at other places in these missed
191 // cases, we simply will not remove/set globals early.
194 remove_unreachable::handle_early (gimple
*s
, edge e
)
196 bool lhs_p
= TREE_CODE (gimple_cond_lhs (s
)) == SSA_NAME
;
197 bool rhs_p
= TREE_CODE (gimple_cond_rhs (s
)) == SSA_NAME
;
198 // Do not remove __builtin_unreachable if it confers a relation, or
199 // that relation may be lost in subsequent passes.
202 // Do not remove addresses early. ie if (x == &y)
203 if (lhs_p
&& TREE_CODE (gimple_cond_rhs (s
)) == ADDR_EXPR
)
206 gcc_checking_assert (gimple_outgoing_range_stmt_p (e
->src
) == s
);
207 gcc_checking_assert (!final_p
);
209 // Check if every export use is dominated by this branch.
211 FOR_EACH_GORI_EXPORT_NAME (m_ranger
.gori (), e
->src
, name
)
213 if (!fully_replaceable (name
, e
->src
))
217 // Set the global value for each.
218 FOR_EACH_GORI_EXPORT_NAME (m_ranger
.gori (), e
->src
, name
)
220 Value_Range
r (TREE_TYPE (name
));
221 m_ranger
.range_on_entry (r
, e
->dest
, name
);
222 // Nothing at this late stage we can do if the write fails.
223 if (!set_range_info (name
, r
))
227 fprintf (dump_file
, "Global Exported (via early unreachable): ");
228 print_generic_expr (dump_file
, name
, TDF_SLIM
);
229 fprintf (dump_file
, " = ");
230 gimple_range_global (r
, name
);
232 fputc ('\n', dump_file
);
236 tree ssa
= lhs_p
? gimple_cond_lhs (s
) : gimple_cond_rhs (s
);
238 // Rewrite the condition.
239 if (e
->flags
& EDGE_TRUE_VALUE
)
240 gimple_cond_make_true (as_a
<gcond
*> (s
));
242 gimple_cond_make_false (as_a
<gcond
*> (s
));
245 // If the name on S is defined in this block, see if there is DCE work to do.
246 if (gimple_bb (SSA_NAME_DEF_STMT (ssa
)) == e
->src
)
249 bitmap_set_bit (dce
, SSA_NAME_VERSION (ssa
));
250 simple_dce_from_worklist (dce
);
255 // Process the edges in the list, change the conditions and removing any
256 // dead code feeding those conditions. Calculate the range of any
257 // names that may have been exported from those blocks, and determine if
258 // there is any updates to their global ranges..
259 // Return true if any builtin_unreachables/globals eliminated/updated.
262 remove_unreachable::remove_and_update_globals ()
264 if (m_list
.length () == 0)
267 // Ensure the cache in SCEV has been cleared before processing
268 // globals to be removed.
275 auto_bitmap all_exports
;
276 for (i
= 0; i
< m_list
.length (); i
++)
279 basic_block src
= BASIC_BLOCK_FOR_FN (cfun
, eb
.first
);
280 basic_block dest
= BASIC_BLOCK_FOR_FN (cfun
, eb
.second
);
283 edge e
= find_edge (src
, dest
);
284 gimple
*s
= gimple_outgoing_range_stmt_p (e
->src
);
285 gcc_checking_assert (gimple_code (s
) == GIMPLE_COND
);
287 bool dominate_exit_p
= true;
288 FOR_EACH_GORI_EXPORT_NAME (m_ranger
.gori (), e
->src
, name
)
290 // Ensure the cache is set for NAME in the succ block.
291 Value_Range
r(TREE_TYPE (name
));
292 Value_Range
ex(TREE_TYPE (name
));
293 m_ranger
.range_on_entry (r
, e
->dest
, name
);
294 m_ranger
.range_on_entry (ex
, EXIT_BLOCK_PTR_FOR_FN (cfun
), name
);
295 // If the range produced by this __builtin_unreachacble expression
296 // is not fully reflected in the range at exit, then it does not
297 // dominate the exit of the function.
298 if (ex
.intersect (r
))
299 dominate_exit_p
= false;
302 // If the exit is dominated, add to the export list. Otherwise if this
303 // isn't the final VRP pass, leave the call in the IL.
305 bitmap_ior_into (all_exports
, m_ranger
.gori ().exports (e
->src
));
310 // Rewrite the condition.
311 if (e
->flags
& EDGE_TRUE_VALUE
)
312 gimple_cond_make_true (as_a
<gcond
*> (s
));
314 gimple_cond_make_false (as_a
<gcond
*> (s
));
318 if (bitmap_empty_p (all_exports
))
320 // Invoke DCE on all exported names to eliminate dead feeding defs.
322 bitmap_copy (dce
, all_exports
);
323 // Don't attempt to DCE parameters.
324 EXECUTE_IF_SET_IN_BITMAP (all_exports
, 0, i
, bi
)
325 if (!ssa_name (i
) || SSA_NAME_IS_DEFAULT_DEF (ssa_name (i
)))
326 bitmap_clear_bit (dce
, i
);
327 simple_dce_from_worklist (dce
);
329 // Loop over all uses of each name and find maximal range. This is the
332 imm_use_iterator iter
;
333 EXECUTE_IF_SET_IN_BITMAP (all_exports
, 0, i
, bi
)
336 if (!name
|| SSA_NAME_IN_FREE_LIST (name
))
338 Value_Range
r (TREE_TYPE (name
));
339 Value_Range
exp_range (TREE_TYPE (name
));
341 FOR_EACH_IMM_USE_FAST (use_p
, iter
, name
)
343 gimple
*use_stmt
= USE_STMT (use_p
);
344 if (is_gimple_debug (use_stmt
))
346 if (!m_ranger
.range_of_expr (exp_range
, name
, use_stmt
))
347 exp_range
.set_varying (TREE_TYPE (name
));
348 r
.union_ (exp_range
);
352 // Include the on-exit range to ensure non-dominated unreachables
353 // don't incorrectly impact the global range.
354 m_ranger
.range_on_entry (exp_range
, EXIT_BLOCK_PTR_FOR_FN (cfun
), name
);
355 r
.union_ (exp_range
);
356 if (r
.varying_p () || r
.undefined_p ())
358 if (!set_range_info (name
, r
))
363 fprintf (dump_file
, "Global Exported (via unreachable): ");
364 print_generic_expr (dump_file
, name
, TDF_SLIM
);
365 fprintf (dump_file
, " = ");
366 gimple_range_global (r
, name
);
368 fputc ('\n', dump_file
);
374 /* VR_TYPE describes a range with minimum value *MIN and maximum
375 value *MAX. Restrict the range to the set of values that have
376 no bits set outside NONZERO_BITS. Update *MIN and *MAX and
377 return the new range type.
379 SGN gives the sign of the values described by the range. */
381 enum value_range_kind
382 intersect_range_with_nonzero_bits (enum value_range_kind vr_type
,
383 wide_int
*min
, wide_int
*max
,
384 const wide_int
&nonzero_bits
,
387 if (vr_type
== VR_ANTI_RANGE
)
389 /* The VR_ANTI_RANGE is equivalent to the union of the ranges
390 A: [-INF, *MIN) and B: (*MAX, +INF]. First use NONZERO_BITS
391 to create an inclusive upper bound for A and an inclusive lower
393 wide_int a_max
= wi::round_down_for_mask (*min
- 1, nonzero_bits
);
394 wide_int b_min
= wi::round_up_for_mask (*max
+ 1, nonzero_bits
);
396 /* If the calculation of A_MAX wrapped, A is effectively empty
397 and A_MAX is the highest value that satisfies NONZERO_BITS.
398 Likewise if the calculation of B_MIN wrapped, B is effectively
399 empty and B_MIN is the lowest value that satisfies NONZERO_BITS. */
400 bool a_empty
= wi::ge_p (a_max
, *min
, sgn
);
401 bool b_empty
= wi::le_p (b_min
, *max
, sgn
);
403 /* If both A and B are empty, there are no valid values. */
404 if (a_empty
&& b_empty
)
407 /* If exactly one of A or B is empty, return a VR_RANGE for the
409 if (a_empty
|| b_empty
)
413 gcc_checking_assert (wi::le_p (*min
, *max
, sgn
));
417 /* Update the VR_ANTI_RANGE bounds. */
420 gcc_checking_assert (wi::le_p (*min
, *max
, sgn
));
422 /* Now check whether the excluded range includes any values that
423 satisfy NONZERO_BITS. If not, switch to a full VR_RANGE. */
424 if (wi::round_up_for_mask (*min
, nonzero_bits
) == b_min
)
426 unsigned int precision
= min
->get_precision ();
427 *min
= wi::min_value (precision
, sgn
);
428 *max
= wi::max_value (precision
, sgn
);
432 if (vr_type
== VR_RANGE
|| vr_type
== VR_VARYING
)
434 *max
= wi::round_down_for_mask (*max
, nonzero_bits
);
436 /* Check that the range contains at least one valid value. */
437 if (wi::gt_p (*min
, *max
, sgn
))
440 *min
= wi::round_up_for_mask (*min
, nonzero_bits
);
441 gcc_checking_assert (wi::le_p (*min
, *max
, sgn
));
446 /* Return the single symbol (an SSA_NAME) contained in T if any, or NULL_TREE
447 otherwise. We only handle additive operations and set NEG to true if the
448 symbol is negated and INV to the invariant part, if any. */
451 get_single_symbol (tree t
, bool *neg
, tree
*inv
)
459 if (TREE_CODE (t
) == PLUS_EXPR
460 || TREE_CODE (t
) == POINTER_PLUS_EXPR
461 || TREE_CODE (t
) == MINUS_EXPR
)
463 if (is_gimple_min_invariant (TREE_OPERAND (t
, 0)))
465 neg_
= (TREE_CODE (t
) == MINUS_EXPR
);
466 inv_
= TREE_OPERAND (t
, 0);
467 t
= TREE_OPERAND (t
, 1);
469 else if (is_gimple_min_invariant (TREE_OPERAND (t
, 1)))
472 inv_
= TREE_OPERAND (t
, 1);
473 t
= TREE_OPERAND (t
, 0);
484 if (TREE_CODE (t
) == NEGATE_EXPR
)
486 t
= TREE_OPERAND (t
, 0);
490 if (TREE_CODE (t
) != SSA_NAME
)
493 if (inv_
&& TREE_OVERFLOW_P (inv_
))
494 inv_
= drop_tree_overflow (inv_
);
501 /* Compare two values VAL1 and VAL2. Return
503 -2 if VAL1 and VAL2 cannot be compared at compile-time,
506 +1 if VAL1 > VAL2, and
509 This is similar to tree_int_cst_compare but supports pointer values
510 and values that cannot be compared at compile time.
512 If STRICT_OVERFLOW_P is not NULL, then set *STRICT_OVERFLOW_P to
513 true if the return value is only valid if we assume that signed
514 overflow is undefined. */
517 compare_values_warnv (tree val1
, tree val2
, bool *strict_overflow_p
)
522 /* Below we rely on the fact that VAL1 and VAL2 are both pointers or
524 gcc_assert (POINTER_TYPE_P (TREE_TYPE (val1
))
525 == POINTER_TYPE_P (TREE_TYPE (val2
)));
527 /* Convert the two values into the same type. This is needed because
528 sizetype causes sign extension even for unsigned types. */
529 if (!useless_type_conversion_p (TREE_TYPE (val1
), TREE_TYPE (val2
)))
530 val2
= fold_convert (TREE_TYPE (val1
), val2
);
532 const bool overflow_undefined
533 = INTEGRAL_TYPE_P (TREE_TYPE (val1
))
534 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (val1
));
537 tree sym1
= get_single_symbol (val1
, &neg1
, &inv1
);
538 tree sym2
= get_single_symbol (val2
, &neg2
, &inv2
);
540 /* If VAL1 and VAL2 are of the form '[-]NAME [+ CST]', return -1 or +1
541 accordingly. If VAL1 and VAL2 don't use the same name, return -2. */
544 /* Both values must use the same name with the same sign. */
545 if (sym1
!= sym2
|| neg1
!= neg2
)
548 /* [-]NAME + CST == [-]NAME + CST. */
552 /* If overflow is defined we cannot simplify more. */
553 if (!overflow_undefined
)
556 if (strict_overflow_p
!= NULL
557 /* Symbolic range building sets the no-warning bit to declare
558 that overflow doesn't happen. */
559 && (!inv1
|| !warning_suppressed_p (val1
, OPT_Woverflow
))
560 && (!inv2
|| !warning_suppressed_p (val2
, OPT_Woverflow
)))
561 *strict_overflow_p
= true;
564 inv1
= build_int_cst (TREE_TYPE (val1
), 0);
566 inv2
= build_int_cst (TREE_TYPE (val2
), 0);
568 return wi::cmp (wi::to_wide (inv1
), wi::to_wide (inv2
),
569 TYPE_SIGN (TREE_TYPE (val1
)));
572 const bool cst1
= is_gimple_min_invariant (val1
);
573 const bool cst2
= is_gimple_min_invariant (val2
);
575 /* If one is of the form '[-]NAME + CST' and the other is constant, then
576 it might be possible to say something depending on the constants. */
577 if ((sym1
&& inv1
&& cst2
) || (sym2
&& inv2
&& cst1
))
579 if (!overflow_undefined
)
582 if (strict_overflow_p
!= NULL
583 /* Symbolic range building sets the no-warning bit to declare
584 that overflow doesn't happen. */
585 && (!sym1
|| !warning_suppressed_p (val1
, OPT_Woverflow
))
586 && (!sym2
|| !warning_suppressed_p (val2
, OPT_Woverflow
)))
587 *strict_overflow_p
= true;
589 const signop sgn
= TYPE_SIGN (TREE_TYPE (val1
));
590 tree cst
= cst1
? val1
: val2
;
591 tree inv
= cst1
? inv2
: inv1
;
593 /* Compute the difference between the constants. If it overflows or
594 underflows, this means that we can trivially compare the NAME with
595 it and, consequently, the two values with each other. */
596 wide_int diff
= wi::to_wide (cst
) - wi::to_wide (inv
);
597 if (wi::cmp (0, wi::to_wide (inv
), sgn
)
598 != wi::cmp (diff
, wi::to_wide (cst
), sgn
))
600 const int res
= wi::cmp (wi::to_wide (cst
), wi::to_wide (inv
), sgn
);
601 return cst1
? res
: -res
;
607 /* We cannot say anything more for non-constants. */
611 if (!POINTER_TYPE_P (TREE_TYPE (val1
)))
613 /* We cannot compare overflowed values. */
614 if (TREE_OVERFLOW (val1
) || TREE_OVERFLOW (val2
))
617 if (TREE_CODE (val1
) == INTEGER_CST
618 && TREE_CODE (val2
) == INTEGER_CST
)
619 return tree_int_cst_compare (val1
, val2
);
621 if (poly_int_tree_p (val1
) && poly_int_tree_p (val2
))
623 if (known_eq (wi::to_poly_widest (val1
),
624 wi::to_poly_widest (val2
)))
626 if (known_lt (wi::to_poly_widest (val1
),
627 wi::to_poly_widest (val2
)))
629 if (known_gt (wi::to_poly_widest (val1
),
630 wi::to_poly_widest (val2
)))
638 if (TREE_CODE (val1
) == INTEGER_CST
&& TREE_CODE (val2
) == INTEGER_CST
)
640 /* We cannot compare overflowed values. */
641 if (TREE_OVERFLOW (val1
) || TREE_OVERFLOW (val2
))
644 return tree_int_cst_compare (val1
, val2
);
647 /* First see if VAL1 and VAL2 are not the same. */
648 if (operand_equal_p (val1
, val2
, 0))
651 fold_defer_overflow_warnings ();
653 /* If VAL1 is a lower address than VAL2, return -1. */
654 tree t
= fold_binary_to_constant (LT_EXPR
, boolean_type_node
, val1
, val2
);
655 if (t
&& integer_onep (t
))
657 fold_undefer_and_ignore_overflow_warnings ();
661 /* If VAL1 is a higher address than VAL2, return +1. */
662 t
= fold_binary_to_constant (LT_EXPR
, boolean_type_node
, val2
, val1
);
663 if (t
&& integer_onep (t
))
665 fold_undefer_and_ignore_overflow_warnings ();
669 /* If VAL1 is different than VAL2, return +2. */
670 t
= fold_binary_to_constant (NE_EXPR
, boolean_type_node
, val1
, val2
);
671 fold_undefer_and_ignore_overflow_warnings ();
672 if (t
&& integer_onep (t
))
679 /* Compare values like compare_values_warnv. */
682 compare_values (tree val1
, tree val2
)
685 return compare_values_warnv (val1
, val2
, &sop
);
688 /* Helper for overflow_comparison_p
690 OP0 CODE OP1 is a comparison. Examine the comparison and potentially
691 OP1's defining statement to see if it ultimately has the form
692 OP0 CODE (OP0 PLUS INTEGER_CST)
694 If so, return TRUE indicating this is an overflow test and store into
695 *NEW_CST an updated constant that can be used in a narrowed range test.
697 REVERSED indicates if the comparison was originally:
701 This affects how we build the updated constant. */
704 overflow_comparison_p_1 (enum tree_code code
, tree op0
, tree op1
,
705 bool reversed
, tree
*new_cst
)
707 /* See if this is a relational operation between two SSA_NAMES with
708 unsigned, overflow wrapping values. If so, check it more deeply. */
709 if ((code
== LT_EXPR
|| code
== LE_EXPR
710 || code
== GE_EXPR
|| code
== GT_EXPR
)
711 && TREE_CODE (op0
) == SSA_NAME
712 && TREE_CODE (op1
) == SSA_NAME
713 && INTEGRAL_TYPE_P (TREE_TYPE (op0
))
714 && TYPE_UNSIGNED (TREE_TYPE (op0
))
715 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (op0
)))
717 gimple
*op1_def
= SSA_NAME_DEF_STMT (op1
);
719 /* Now look at the defining statement of OP1 to see if it adds
720 or subtracts a nonzero constant from another operand. */
722 && is_gimple_assign (op1_def
)
723 && gimple_assign_rhs_code (op1_def
) == PLUS_EXPR
724 && TREE_CODE (gimple_assign_rhs2 (op1_def
)) == INTEGER_CST
725 && !integer_zerop (gimple_assign_rhs2 (op1_def
)))
727 tree target
= gimple_assign_rhs1 (op1_def
);
729 /* If we did not find our target SSA_NAME, then this is not
734 tree type
= TREE_TYPE (op0
);
735 wide_int max
= wi::max_value (TYPE_PRECISION (type
), UNSIGNED
);
736 tree inc
= gimple_assign_rhs2 (op1_def
);
738 *new_cst
= wide_int_to_tree (type
, max
+ wi::to_wide (inc
));
740 *new_cst
= wide_int_to_tree (type
, max
- wi::to_wide (inc
));
747 /* OP0 CODE OP1 is a comparison. Examine the comparison and potentially
748 OP1's defining statement to see if it ultimately has the form
749 OP0 CODE (OP0 PLUS INTEGER_CST)
751 If so, return TRUE indicating this is an overflow test and store into
752 *NEW_CST an updated constant that can be used in a narrowed range test.
754 These statements are left as-is in the IL to facilitate discovery of
755 {ADD,SUB}_OVERFLOW sequences later in the optimizer pipeline. But
756 the alternate range representation is often useful within VRP. */
759 overflow_comparison_p (tree_code code
, tree name
, tree val
, tree
*new_cst
)
761 if (overflow_comparison_p_1 (code
, name
, val
, false, new_cst
))
763 return overflow_comparison_p_1 (swap_tree_comparison (code
), val
, name
,
767 /* Searches the case label vector VEC for the index *IDX of the CASE_LABEL
768 that includes the value VAL. The search is restricted to the range
769 [START_IDX, n - 1] where n is the size of VEC.
771 If there is a CASE_LABEL for VAL, its index is placed in IDX and true is
774 If there is no CASE_LABEL for VAL and there is one that is larger than VAL,
775 it is placed in IDX and false is returned.
777 If VAL is larger than any CASE_LABEL, n is placed on IDX and false is
781 find_case_label_index (gswitch
*stmt
, size_t start_idx
, tree val
, size_t *idx
)
783 size_t n
= gimple_switch_num_labels (stmt
);
786 /* Find case label for minimum of the value range or the next one.
787 At each iteration we are searching in [low, high - 1]. */
789 for (low
= start_idx
, high
= n
; high
!= low
; )
793 /* Note that i != high, so we never ask for n. */
794 size_t i
= (high
+ low
) / 2;
795 t
= gimple_switch_label (stmt
, i
);
797 /* Cache the result of comparing CASE_LOW and val. */
798 cmp
= tree_int_cst_compare (CASE_LOW (t
), val
);
802 /* Ranges cannot be empty. */
811 if (CASE_HIGH (t
) != NULL
812 && tree_int_cst_compare (CASE_HIGH (t
), val
) >= 0)
824 /* Searches the case label vector VEC for the range of CASE_LABELs that is used
825 for values between MIN and MAX. The first index is placed in MIN_IDX. The
826 last index is placed in MAX_IDX. If the range of CASE_LABELs is empty
827 then MAX_IDX < MIN_IDX.
828 Returns true if the default label is not needed. */
831 find_case_label_range (gswitch
*stmt
, tree min
, tree max
, size_t *min_idx
,
835 bool min_take_default
= !find_case_label_index (stmt
, 1, min
, &i
);
836 bool max_take_default
= !find_case_label_index (stmt
, i
, max
, &j
);
842 /* Only the default case label reached.
843 Return an empty range. */
850 bool take_default
= min_take_default
|| max_take_default
;
854 if (max_take_default
)
857 /* If the case label range is continuous, we do not need
858 the default case label. Verify that. */
859 high
= CASE_LOW (gimple_switch_label (stmt
, i
));
860 if (CASE_HIGH (gimple_switch_label (stmt
, i
)))
861 high
= CASE_HIGH (gimple_switch_label (stmt
, i
));
862 for (k
= i
+ 1; k
<= j
; ++k
)
864 low
= CASE_LOW (gimple_switch_label (stmt
, k
));
865 if (!integer_onep (int_const_binop (MINUS_EXPR
, low
, high
)))
871 if (CASE_HIGH (gimple_switch_label (stmt
, k
)))
872 high
= CASE_HIGH (gimple_switch_label (stmt
, k
));
877 return !take_default
;
881 /* Given a SWITCH_STMT, return the case label that encompasses the
882 known possible values for the switch operand. RANGE_OF_OP is a
883 range for the known values of the switch operand. */
886 find_case_label_range (gswitch
*switch_stmt
, const irange
*range_of_op
)
888 if (range_of_op
->undefined_p ()
889 || range_of_op
->varying_p ())
893 tree op
= gimple_switch_index (switch_stmt
);
894 tree type
= TREE_TYPE (op
);
895 tree tmin
= wide_int_to_tree (type
, range_of_op
->lower_bound ());
896 tree tmax
= wide_int_to_tree (type
, range_of_op
->upper_bound ());
897 find_case_label_range (switch_stmt
, tmin
, tmax
, &i
, &j
);
900 /* Look for exactly one label that encompasses the range of
902 tree label
= gimple_switch_label (switch_stmt
, i
);
904 = CASE_HIGH (label
) ? CASE_HIGH (label
) : CASE_LOW (label
);
905 wide_int wlow
= wi::to_wide (CASE_LOW (label
));
906 wide_int whigh
= wi::to_wide (case_high
);
907 int_range_max
label_range (TREE_TYPE (case_high
), wlow
, whigh
);
908 if (!types_compatible_p (label_range
.type (), range_of_op
->type ()))
909 range_cast (label_range
, range_of_op
->type ());
910 label_range
.intersect (*range_of_op
);
911 if (label_range
== *range_of_op
)
916 /* If there are no labels at all, take the default. */
917 return gimple_switch_label (switch_stmt
, 0);
921 /* Otherwise, there are various labels that can encompass
922 the range of operand. In which case, see if the range of
923 the operand is entirely *outside* the bounds of all the
924 (non-default) case labels. If so, take the default. */
925 unsigned n
= gimple_switch_num_labels (switch_stmt
);
926 tree min_label
= gimple_switch_label (switch_stmt
, 1);
927 tree max_label
= gimple_switch_label (switch_stmt
, n
- 1);
928 tree case_high
= CASE_HIGH (max_label
);
930 case_high
= CASE_LOW (max_label
);
931 int_range_max
label_range (TREE_TYPE (CASE_LOW (min_label
)),
932 wi::to_wide (CASE_LOW (min_label
)),
933 wi::to_wide (case_high
));
934 if (!types_compatible_p (label_range
.type (), range_of_op
->type ()))
935 range_cast (label_range
, range_of_op
->type ());
936 label_range
.intersect (*range_of_op
);
937 if (label_range
.undefined_p ())
938 return gimple_switch_label (switch_stmt
, 0);
949 // This is a ranger based folder which continues to use the dominator
950 // walk to access the substitute and fold machinery. Ranges are calculated
953 class rvrp_folder
: public substitute_and_fold_engine
957 rvrp_folder (gimple_ranger
*r
, bool all
)
958 : substitute_and_fold_engine (),
959 m_unreachable (*r
, all
),
960 m_simplifier (r
, r
->non_executable_edge_flag
)
963 m_pta
= new pointer_equiv_analyzer (m_ranger
);
964 m_last_bb_stmt
= NULL
;
972 tree
value_of_expr (tree name
, gimple
*s
= NULL
) override
974 // Shortcircuit subst_and_fold callbacks for abnormal ssa_names.
975 if (TREE_CODE (name
) == SSA_NAME
&& SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name
))
977 tree ret
= m_ranger
->value_of_expr (name
, s
);
978 if (!ret
&& supported_pointer_equiv_p (name
))
979 ret
= m_pta
->get_equiv (name
);
983 tree
value_on_edge (edge e
, tree name
) override
985 // Shortcircuit subst_and_fold callbacks for abnormal ssa_names.
986 if (TREE_CODE (name
) == SSA_NAME
&& SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name
))
988 tree ret
= m_ranger
->value_on_edge (e
, name
);
989 if (!ret
&& supported_pointer_equiv_p (name
))
990 ret
= m_pta
->get_equiv (name
);
994 tree
value_of_stmt (gimple
*s
, tree name
= NULL
) override
996 // Shortcircuit subst_and_fold callbacks for abnormal ssa_names.
997 if (TREE_CODE (name
) == SSA_NAME
&& SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name
))
999 return m_ranger
->value_of_stmt (s
, name
);
1002 void pre_fold_bb (basic_block bb
) override
1005 for (gphi_iterator gsi
= gsi_start_phis (bb
); !gsi_end_p (gsi
);
1007 m_ranger
->register_inferred_ranges (gsi
.phi ());
1008 m_last_bb_stmt
= last_nondebug_stmt (bb
);
1011 void post_fold_bb (basic_block bb
) override
1016 void pre_fold_stmt (gimple
*stmt
) override
1018 m_pta
->visit_stmt (stmt
);
1019 // If this is the last stmt and there are inferred ranges, reparse the
1020 // block for transitive inferred ranges that occur earlier in the block.
1021 if (stmt
== m_last_bb_stmt
)
1023 m_ranger
->register_transitive_inferred_ranges (gimple_bb (stmt
));
1024 // Also check for builtin_unreachable calls.
1025 if (cfun
->after_inlining
&& gimple_code (stmt
) == GIMPLE_COND
)
1026 m_unreachable
.maybe_register (stmt
);
1030 bool fold_stmt (gimple_stmt_iterator
*gsi
) override
1032 bool ret
= m_simplifier
.simplify (gsi
);
1034 ret
= m_ranger
->fold_stmt (gsi
, follow_single_use_edges
);
1035 m_ranger
->register_inferred_ranges (gsi_stmt (*gsi
));
1039 remove_unreachable m_unreachable
;
1041 DISABLE_COPY_AND_ASSIGN (rvrp_folder
);
1042 gimple_ranger
*m_ranger
;
1043 simplify_using_ranges m_simplifier
;
1044 pointer_equiv_analyzer
*m_pta
;
1045 gimple
*m_last_bb_stmt
;
1048 /* Main entry point for a VRP pass using just ranger. This can be called
1049 from anywhere to perform a VRP pass, including from EVRP. */
1052 execute_ranger_vrp (struct function
*fun
, bool warn_array_bounds_p
,
1055 loop_optimizer_init (LOOPS_NORMAL
| LOOPS_HAVE_RECORDED_EXITS
);
1056 rewrite_into_loop_closed_ssa (NULL
, TODO_update_ssa
);
1058 calculate_dominance_info (CDI_DOMINATORS
);
1060 set_all_edges_as_executable (fun
);
1061 gimple_ranger
*ranger
= enable_ranger (fun
, false);
1062 rvrp_folder
folder (ranger
, final_p
);
1063 phi_analysis_initialize (ranger
->const_query ());
1064 folder
.substitute_and_fold ();
1065 // Remove tagged builtin-unreachable and maybe update globals.
1066 folder
.m_unreachable
.remove_and_update_globals ();
1067 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1068 ranger
->dump (dump_file
);
1070 if ((warn_array_bounds
|| warn_strict_flex_arrays
) && warn_array_bounds_p
)
1072 // Set all edges as executable, except those ranger says aren't.
1073 int non_exec_flag
= ranger
->non_executable_edge_flag
;
1075 FOR_ALL_BB_FN (bb
, fun
)
1079 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
1080 if (e
->flags
& non_exec_flag
)
1081 e
->flags
&= ~EDGE_EXECUTABLE
;
1083 e
->flags
|= EDGE_EXECUTABLE
;
1086 array_bounds_checker
array_checker (fun
, ranger
);
1087 array_checker
.check ();
1091 if (Value_Range::supports_type_p (TREE_TYPE
1092 (TREE_TYPE (current_function_decl
)))
1094 && !lookup_attribute ("noipa", DECL_ATTRIBUTES (current_function_decl
)))
1099 Value_Range
return_range (TREE_TYPE (TREE_TYPE (current_function_decl
)));
1100 FOR_EACH_EDGE (e
, ei
, EXIT_BLOCK_PTR_FOR_FN (cfun
)->preds
)
1101 if (greturn
*ret
= dyn_cast
<greturn
*> (*gsi_last_bb (e
->src
)))
1103 tree retval
= gimple_return_retval (ret
);
1106 return_range
.set_varying (TREE_TYPE (TREE_TYPE (current_function_decl
)));
1110 Value_Range
r (TREE_TYPE (retval
));
1111 if (ranger
->range_of_expr (r
, retval
, ret
)
1112 && !r
.undefined_p ()
1118 return_range
.union_ (r
);
1121 return_range
.set_varying (TREE_TYPE (retval
));
1124 if (found
&& !return_range
.varying_p ())
1126 ipa_record_return_value_range (return_range
);
1127 if (POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl
)))
1128 && return_range
.nonzero_p ()
1129 && cgraph_node::get (current_function_decl
)
1130 ->add_detected_attribute ("returns_nonnull"))
1131 warn_function_returns_nonnull (current_function_decl
);
1135 phi_analysis_finalize ();
1136 disable_ranger (fun
);
1138 loop_optimizer_finalize ();
1142 // Implement a Fast VRP folder. Not quite as effective but faster.
1144 class fvrp_folder
: public substitute_and_fold_engine
1147 fvrp_folder (dom_ranger
*dr
) : substitute_and_fold_engine (),
1149 { m_dom_ranger
= dr
; }
1153 tree
value_of_expr (tree name
, gimple
*s
= NULL
) override
1155 // Shortcircuit subst_and_fold callbacks for abnormal ssa_names.
1156 if (TREE_CODE (name
) == SSA_NAME
&& SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name
))
1158 return m_dom_ranger
->value_of_expr (name
, s
);
1161 tree
value_on_edge (edge e
, tree name
) override
1163 // Shortcircuit subst_and_fold callbacks for abnormal ssa_names.
1164 if (TREE_CODE (name
) == SSA_NAME
&& SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name
))
1166 return m_dom_ranger
->value_on_edge (e
, name
);
1169 tree
value_of_stmt (gimple
*s
, tree name
= NULL
) override
1171 // Shortcircuit subst_and_fold callbacks for abnormal ssa_names.
1172 if (TREE_CODE (name
) == SSA_NAME
&& SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name
))
1174 return m_dom_ranger
->value_of_stmt (s
, name
);
1177 void pre_fold_bb (basic_block bb
) override
1179 m_dom_ranger
->pre_bb (bb
);
1180 // Now process the PHIs in advance.
1181 gphi_iterator psi
= gsi_start_phis (bb
);
1182 for ( ; !gsi_end_p (psi
); gsi_next (&psi
))
1184 tree name
= gimple_range_ssa_p (PHI_RESULT (psi
.phi ()));
1187 Value_Range
vr(TREE_TYPE (name
));
1188 m_dom_ranger
->range_of_stmt (vr
, psi
.phi (), name
);
1193 void post_fold_bb (basic_block bb
) override
1195 m_dom_ranger
->post_bb (bb
);
1198 void pre_fold_stmt (gimple
*s
) override
1200 // Ensure range_of_stmt has been called.
1201 tree type
= gimple_range_type (s
);
1204 Value_Range
vr(type
);
1205 m_dom_ranger
->range_of_stmt (vr
, s
);
1209 bool fold_stmt (gimple_stmt_iterator
*gsi
) override
1211 bool ret
= m_simplifier
.simplify (gsi
);
1213 ret
= ::fold_stmt (gsi
, follow_single_use_edges
);
1218 DISABLE_COPY_AND_ASSIGN (fvrp_folder
);
1219 simplify_using_ranges m_simplifier
;
1220 dom_ranger
*m_dom_ranger
;
1224 // Main entry point for a FAST VRP pass using a dom ranger.
1227 execute_fast_vrp (struct function
*fun
)
1229 calculate_dominance_info (CDI_DOMINATORS
);
1231 fvrp_folder
folder (&dr
);
1233 gcc_checking_assert (!fun
->x_range_query
);
1234 fun
->x_range_query
= &dr
;
1236 folder
.substitute_and_fold ();
1238 fun
->x_range_query
= NULL
;
1244 const pass_data pass_data_vrp
=
1246 GIMPLE_PASS
, /* type */
1248 OPTGROUP_NONE
, /* optinfo_flags */
1249 TV_TREE_VRP
, /* tv_id */
1250 PROP_ssa
, /* properties_required */
1251 0, /* properties_provided */
1252 0, /* properties_destroyed */
1253 0, /* todo_flags_start */
1254 ( TODO_cleanup_cfg
| TODO_update_ssa
), /* todo_flags_finish */
1257 const pass_data pass_data_early_vrp
=
1259 GIMPLE_PASS
, /* type */
1261 OPTGROUP_NONE
, /* optinfo_flags */
1262 TV_TREE_EARLY_VRP
, /* tv_id */
1263 PROP_ssa
, /* properties_required */
1264 0, /* properties_provided */
1265 0, /* properties_destroyed */
1266 0, /* todo_flags_start */
1267 ( TODO_cleanup_cfg
| TODO_update_ssa
| TODO_verify_all
),
1270 const pass_data pass_data_fast_vrp
=
1272 GIMPLE_PASS
, /* type */
1274 OPTGROUP_NONE
, /* optinfo_flags */
1275 TV_TREE_FAST_VRP
, /* tv_id */
1276 PROP_ssa
, /* properties_required */
1277 0, /* properties_provided */
1278 0, /* properties_destroyed */
1279 0, /* todo_flags_start */
1280 ( TODO_cleanup_cfg
| TODO_update_ssa
| TODO_verify_all
),
1284 class pass_vrp
: public gimple_opt_pass
1287 pass_vrp (gcc::context
*ctxt
, const pass_data
&data_
, bool warn_p
)
1288 : gimple_opt_pass (data_
, ctxt
), data (data_
),
1289 warn_array_bounds_p (warn_p
), final_p (false)
1292 /* opt_pass methods: */
1293 opt_pass
* clone () final override
1294 { return new pass_vrp (m_ctxt
, data
, false); }
1295 void set_pass_param (unsigned int n
, bool param
) final override
1297 gcc_assert (n
== 0);
1300 bool gate (function
*) final override
{ return flag_tree_vrp
!= 0; }
1301 unsigned int execute (function
*fun
) final override
1303 // Check for fast vrp.
1304 if (&data
== &pass_data_fast_vrp
)
1305 return execute_fast_vrp (fun
);
1307 return execute_ranger_vrp (fun
, warn_array_bounds_p
, final_p
);
1311 const pass_data
&data
;
1312 bool warn_array_bounds_p
;
1314 }; // class pass_vrp
1316 const pass_data pass_data_assumptions
=
1318 GIMPLE_PASS
, /* type */
1319 "assumptions", /* name */
1320 OPTGROUP_NONE
, /* optinfo_flags */
1321 TV_TREE_ASSUMPTIONS
, /* tv_id */
1322 PROP_ssa
, /* properties_required */
1323 PROP_assumptions_done
, /* properties_provided */
1324 0, /* properties_destroyed */
1325 0, /* todo_flags_start */
1326 0, /* todo_flags_end */
1329 class pass_assumptions
: public gimple_opt_pass
1332 pass_assumptions (gcc::context
*ctxt
)
1333 : gimple_opt_pass (pass_data_assumptions
, ctxt
)
1336 /* opt_pass methods: */
1337 bool gate (function
*fun
) final override
{ return fun
->assume_function
; }
1338 unsigned int execute (function
*) final override
1342 fprintf (dump_file
, "Assumptions :\n--------------\n");
1344 for (tree arg
= DECL_ARGUMENTS (cfun
->decl
); arg
; arg
= DECL_CHAIN (arg
))
1346 tree name
= ssa_default_def (cfun
, arg
);
1347 if (!name
|| !gimple_range_ssa_p (name
))
1349 tree type
= TREE_TYPE (name
);
1350 if (!Value_Range::supports_type_p (type
))
1352 Value_Range
assume_range (type
);
1353 if (query
.assume_range_p (assume_range
, name
))
1355 // Set the global range of NAME to anything calculated.
1356 set_range_info (name
, assume_range
);
1359 print_generic_expr (dump_file
, name
, TDF_SLIM
);
1360 fprintf (dump_file
, " -> ");
1361 assume_range
.dump (dump_file
);
1362 fputc ('\n', dump_file
);
1368 fputc ('\n', dump_file
);
1369 gimple_dump_cfg (dump_file
, dump_flags
& ~TDF_DETAILS
);
1370 if (dump_flags
& TDF_DETAILS
)
1371 query
.dump (dump_file
);
1373 return TODO_discard_function
;
1376 }; // class pass_assumptions
1381 make_pass_vrp (gcc::context
*ctxt
)
1383 return new pass_vrp (ctxt
, pass_data_vrp
, true);
1387 make_pass_early_vrp (gcc::context
*ctxt
)
1389 return new pass_vrp (ctxt
, pass_data_early_vrp
, false);
1393 make_pass_fast_vrp (gcc::context
*ctxt
)
1395 return new pass_vrp (ctxt
, pass_data_fast_vrp
, false);
1399 make_pass_assumptions (gcc::context
*ctx
)
1401 return new pass_assumptions (ctx
);