1 /* Support routines for Value Range Propagation (VRP).
2 Copyright (C) 2005-2022 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"
56 // This class is utilized by VRP and ranger to remove __builtin_unreachable
57 // calls, and reflect any resulting global ranges.
59 // maybe_register_block () is called on basic blocks, and if that block
60 // matches the pattern of one branch being a builtin_unreachable, register
61 // the resulting executable edge in a list.
63 // After all blocks have been processed, remove_and_update_globals() will
64 // - check all exports from registered blocks
65 // - ensure the cache entry of each export is set with the appropriate range
66 // - rewrite the conditions to take the executable edge
67 // - perform DCE on any feeding instructions to those rewritten conditions
69 // Then each of the immediate use chain of each export is walked, and a new
70 // global range created by unioning the ranges at all remaining use locations.
72 class remove_unreachable
{
74 remove_unreachable (gimple_ranger
&r
) : m_ranger (r
) { m_list
.create (30); }
75 ~remove_unreachable () { m_list
.release (); }
76 void maybe_register_block (basic_block bb
);
77 bool remove_and_update_globals (bool final_p
);
79 gimple_ranger
&m_ranger
;
82 // Check if block BB has a __builtin_unreachable () call on one arm, and
83 // register the executable edge if so.
86 remove_unreachable::maybe_register_block (basic_block bb
)
88 gimple
*s
= gimple_outgoing_range_stmt_p (bb
);
89 if (!s
|| gimple_code (s
) != GIMPLE_COND
)
92 edge e0
= EDGE_SUCC (bb
, 0);
93 basic_block bb0
= e0
->dest
;
94 bool un0
= EDGE_COUNT (bb0
->succs
) == 0
95 && gimple_seq_unreachable_p (bb_seq (bb0
));
96 edge e1
= EDGE_SUCC (bb
, 1);
97 basic_block bb1
= e1
->dest
;
98 bool un1
= EDGE_COUNT (bb1
->succs
) == 0
99 && gimple_seq_unreachable_p (bb_seq (bb1
));
101 // If the 2 blocks are not different, ignore.
106 m_list
.safe_push (e1
);
108 m_list
.safe_push (e0
);
111 // Process the edges in the list, change the conditions and removing any
112 // dead code feeding those conditions. Calculate the range of any
113 // names that may have been exported from those blocks, and determine if
114 // there is any updates to their global ranges..
115 // FINAL_P indicates all builtin_unreachable calls should be removed.
116 // Return true if any builtin_unreachables/globals eliminated/updated.
119 remove_unreachable::remove_and_update_globals (bool final_p
)
121 if (m_list
.length () == 0)
128 auto_bitmap all_exports
;
129 for (i
= 0; i
< m_list
.length (); i
++)
132 gimple
*s
= gimple_outgoing_range_stmt_p (e
->src
);
133 gcc_checking_assert (gimple_code (s
) == GIMPLE_COND
);
134 bool lhs_p
= TREE_CODE (gimple_cond_lhs (s
)) == SSA_NAME
;
135 bool rhs_p
= TREE_CODE (gimple_cond_rhs (s
)) == SSA_NAME
;
136 // Do not remove __builtin_unreachable if it confers a relation, or
137 // that relation will be lost in subsequent passes. Unless its the
139 if (!final_p
&& lhs_p
&& rhs_p
)
141 // If this is already a constant condition, don't look either
142 if (!lhs_p
&& !rhs_p
)
145 bool dominate_exit_p
= true;
146 FOR_EACH_GORI_EXPORT_NAME (m_ranger
.gori (), e
->src
, name
)
148 // Ensure the cache is set for NAME in the succ block.
149 Value_Range
r(TREE_TYPE (name
));
150 Value_Range
ex(TREE_TYPE (name
));
151 m_ranger
.range_on_entry (r
, e
->dest
, name
);
152 m_ranger
.range_on_entry (ex
, EXIT_BLOCK_PTR_FOR_FN (cfun
), name
);
153 // If the range produced by this __builtin_unreachacble expression
154 // is not fully reflected in the range at exit, then it does not
155 // dominate the exit of the funciton.
156 if (ex
.intersect (r
))
157 dominate_exit_p
= false;
160 // If the exit is dominated, add to the export list. Otherwise if this
161 // isn't the final VRP pass, leave the call in the IL.
163 bitmap_ior_into (all_exports
, m_ranger
.gori ().exports (e
->src
));
168 // Rewrite the condition.
169 if (e
->flags
& EDGE_TRUE_VALUE
)
170 gimple_cond_make_true (as_a
<gcond
*> (s
));
172 gimple_cond_make_false (as_a
<gcond
*> (s
));
176 if (bitmap_empty_p (all_exports
))
178 // Invoke DCE on all exported names to elimnate dead feeding defs
180 bitmap_copy (dce
, all_exports
);
181 // Don't attempt to DCE parameters.
182 EXECUTE_IF_SET_IN_BITMAP (all_exports
, 0, i
, bi
)
183 if (!ssa_name (i
) || SSA_NAME_IS_DEFAULT_DEF (ssa_name (i
)))
184 bitmap_clear_bit (dce
, i
);
185 simple_dce_from_worklist (dce
);
187 // Loop over all uses of each name and find maximal range. This is the
190 imm_use_iterator iter
;
191 EXECUTE_IF_SET_IN_BITMAP (all_exports
, 0, i
, bi
)
194 if (!name
|| SSA_NAME_IN_FREE_LIST (name
))
196 Value_Range
r (TREE_TYPE (name
));
197 Value_Range
exp_range (TREE_TYPE (name
));
199 FOR_EACH_IMM_USE_FAST (use_p
, iter
, name
)
201 gimple
*use_stmt
= USE_STMT (use_p
);
202 if (is_gimple_debug (use_stmt
))
204 if (!m_ranger
.range_of_expr (exp_range
, name
, use_stmt
))
205 exp_range
.set_varying (TREE_TYPE (name
));
206 r
.union_ (exp_range
);
210 // Include the on-exit range to ensure non-dominated unreachables
211 // don't incorrectly impact the global range.
212 m_ranger
.range_on_entry (exp_range
, EXIT_BLOCK_PTR_FOR_FN (cfun
), name
);
213 r
.union_ (exp_range
);
214 if (r
.varying_p () || r
.undefined_p ())
216 if (!set_range_info (name
, r
))
221 fprintf (dump_file
, "Global Exported (via unreachable): ");
222 print_generic_expr (dump_file
, name
, TDF_SLIM
);
223 fprintf (dump_file
, " = ");
224 gimple_range_global (r
, name
);
226 fputc ('\n', dump_file
);
232 /* VR_TYPE describes a range with mininum value *MIN and maximum
233 value *MAX. Restrict the range to the set of values that have
234 no bits set outside NONZERO_BITS. Update *MIN and *MAX and
235 return the new range type.
237 SGN gives the sign of the values described by the range. */
239 enum value_range_kind
240 intersect_range_with_nonzero_bits (enum value_range_kind vr_type
,
241 wide_int
*min
, wide_int
*max
,
242 const wide_int
&nonzero_bits
,
245 if (vr_type
== VR_ANTI_RANGE
)
247 /* The VR_ANTI_RANGE is equivalent to the union of the ranges
248 A: [-INF, *MIN) and B: (*MAX, +INF]. First use NONZERO_BITS
249 to create an inclusive upper bound for A and an inclusive lower
251 wide_int a_max
= wi::round_down_for_mask (*min
- 1, nonzero_bits
);
252 wide_int b_min
= wi::round_up_for_mask (*max
+ 1, nonzero_bits
);
254 /* If the calculation of A_MAX wrapped, A is effectively empty
255 and A_MAX is the highest value that satisfies NONZERO_BITS.
256 Likewise if the calculation of B_MIN wrapped, B is effectively
257 empty and B_MIN is the lowest value that satisfies NONZERO_BITS. */
258 bool a_empty
= wi::ge_p (a_max
, *min
, sgn
);
259 bool b_empty
= wi::le_p (b_min
, *max
, sgn
);
261 /* If both A and B are empty, there are no valid values. */
262 if (a_empty
&& b_empty
)
265 /* If exactly one of A or B is empty, return a VR_RANGE for the
267 if (a_empty
|| b_empty
)
271 gcc_checking_assert (wi::le_p (*min
, *max
, sgn
));
275 /* Update the VR_ANTI_RANGE bounds. */
278 gcc_checking_assert (wi::le_p (*min
, *max
, sgn
));
280 /* Now check whether the excluded range includes any values that
281 satisfy NONZERO_BITS. If not, switch to a full VR_RANGE. */
282 if (wi::round_up_for_mask (*min
, nonzero_bits
) == b_min
)
284 unsigned int precision
= min
->get_precision ();
285 *min
= wi::min_value (precision
, sgn
);
286 *max
= wi::max_value (precision
, sgn
);
290 if (vr_type
== VR_RANGE
|| vr_type
== VR_VARYING
)
292 *max
= wi::round_down_for_mask (*max
, nonzero_bits
);
294 /* Check that the range contains at least one valid value. */
295 if (wi::gt_p (*min
, *max
, sgn
))
298 *min
= wi::round_up_for_mask (*min
, nonzero_bits
);
299 gcc_checking_assert (wi::le_p (*min
, *max
, sgn
));
304 /* Return true if max and min of VR are INTEGER_CST. It's not necessary
308 range_int_cst_p (const value_range
*vr
)
310 return (vr
->kind () == VR_RANGE
&& range_has_numeric_bounds_p (vr
));
313 /* Return the single symbol (an SSA_NAME) contained in T if any, or NULL_TREE
314 otherwise. We only handle additive operations and set NEG to true if the
315 symbol is negated and INV to the invariant part, if any. */
318 get_single_symbol (tree t
, bool *neg
, tree
*inv
)
326 if (TREE_CODE (t
) == PLUS_EXPR
327 || TREE_CODE (t
) == POINTER_PLUS_EXPR
328 || TREE_CODE (t
) == MINUS_EXPR
)
330 if (is_gimple_min_invariant (TREE_OPERAND (t
, 0)))
332 neg_
= (TREE_CODE (t
) == MINUS_EXPR
);
333 inv_
= TREE_OPERAND (t
, 0);
334 t
= TREE_OPERAND (t
, 1);
336 else if (is_gimple_min_invariant (TREE_OPERAND (t
, 1)))
339 inv_
= TREE_OPERAND (t
, 1);
340 t
= TREE_OPERAND (t
, 0);
351 if (TREE_CODE (t
) == NEGATE_EXPR
)
353 t
= TREE_OPERAND (t
, 0);
357 if (TREE_CODE (t
) != SSA_NAME
)
360 if (inv_
&& TREE_OVERFLOW_P (inv_
))
361 inv_
= drop_tree_overflow (inv_
);
371 -2 if those are incomparable. */
373 operand_less_p (tree val
, tree val2
)
375 /* LT is folded faster than GE and others. Inline the common case. */
376 if (TREE_CODE (val
) == INTEGER_CST
&& TREE_CODE (val2
) == INTEGER_CST
)
377 return tree_int_cst_lt (val
, val2
);
378 else if (TREE_CODE (val
) == SSA_NAME
&& TREE_CODE (val2
) == SSA_NAME
)
379 return val
== val2
? 0 : -2;
382 int cmp
= compare_values (val
, val2
);
385 else if (cmp
== 0 || cmp
== 1)
392 /* Compare two values VAL1 and VAL2. Return
394 -2 if VAL1 and VAL2 cannot be compared at compile-time,
397 +1 if VAL1 > VAL2, and
400 This is similar to tree_int_cst_compare but supports pointer values
401 and values that cannot be compared at compile time.
403 If STRICT_OVERFLOW_P is not NULL, then set *STRICT_OVERFLOW_P to
404 true if the return value is only valid if we assume that signed
405 overflow is undefined. */
408 compare_values_warnv (tree val1
, tree val2
, bool *strict_overflow_p
)
413 /* Below we rely on the fact that VAL1 and VAL2 are both pointers or
415 gcc_assert (POINTER_TYPE_P (TREE_TYPE (val1
))
416 == POINTER_TYPE_P (TREE_TYPE (val2
)));
418 /* Convert the two values into the same type. This is needed because
419 sizetype causes sign extension even for unsigned types. */
420 if (!useless_type_conversion_p (TREE_TYPE (val1
), TREE_TYPE (val2
)))
421 val2
= fold_convert (TREE_TYPE (val1
), val2
);
423 const bool overflow_undefined
424 = INTEGRAL_TYPE_P (TREE_TYPE (val1
))
425 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (val1
));
428 tree sym1
= get_single_symbol (val1
, &neg1
, &inv1
);
429 tree sym2
= get_single_symbol (val2
, &neg2
, &inv2
);
431 /* If VAL1 and VAL2 are of the form '[-]NAME [+ CST]', return -1 or +1
432 accordingly. If VAL1 and VAL2 don't use the same name, return -2. */
435 /* Both values must use the same name with the same sign. */
436 if (sym1
!= sym2
|| neg1
!= neg2
)
439 /* [-]NAME + CST == [-]NAME + CST. */
443 /* If overflow is defined we cannot simplify more. */
444 if (!overflow_undefined
)
447 if (strict_overflow_p
!= NULL
448 /* Symbolic range building sets the no-warning bit to declare
449 that overflow doesn't happen. */
450 && (!inv1
|| !warning_suppressed_p (val1
, OPT_Woverflow
))
451 && (!inv2
|| !warning_suppressed_p (val2
, OPT_Woverflow
)))
452 *strict_overflow_p
= true;
455 inv1
= build_int_cst (TREE_TYPE (val1
), 0);
457 inv2
= build_int_cst (TREE_TYPE (val2
), 0);
459 return wi::cmp (wi::to_wide (inv1
), wi::to_wide (inv2
),
460 TYPE_SIGN (TREE_TYPE (val1
)));
463 const bool cst1
= is_gimple_min_invariant (val1
);
464 const bool cst2
= is_gimple_min_invariant (val2
);
466 /* If one is of the form '[-]NAME + CST' and the other is constant, then
467 it might be possible to say something depending on the constants. */
468 if ((sym1
&& inv1
&& cst2
) || (sym2
&& inv2
&& cst1
))
470 if (!overflow_undefined
)
473 if (strict_overflow_p
!= NULL
474 /* Symbolic range building sets the no-warning bit to declare
475 that overflow doesn't happen. */
476 && (!sym1
|| !warning_suppressed_p (val1
, OPT_Woverflow
))
477 && (!sym2
|| !warning_suppressed_p (val2
, OPT_Woverflow
)))
478 *strict_overflow_p
= true;
480 const signop sgn
= TYPE_SIGN (TREE_TYPE (val1
));
481 tree cst
= cst1
? val1
: val2
;
482 tree inv
= cst1
? inv2
: inv1
;
484 /* Compute the difference between the constants. If it overflows or
485 underflows, this means that we can trivially compare the NAME with
486 it and, consequently, the two values with each other. */
487 wide_int diff
= wi::to_wide (cst
) - wi::to_wide (inv
);
488 if (wi::cmp (0, wi::to_wide (inv
), sgn
)
489 != wi::cmp (diff
, wi::to_wide (cst
), sgn
))
491 const int res
= wi::cmp (wi::to_wide (cst
), wi::to_wide (inv
), sgn
);
492 return cst1
? res
: -res
;
498 /* We cannot say anything more for non-constants. */
502 if (!POINTER_TYPE_P (TREE_TYPE (val1
)))
504 /* We cannot compare overflowed values. */
505 if (TREE_OVERFLOW (val1
) || TREE_OVERFLOW (val2
))
508 if (TREE_CODE (val1
) == INTEGER_CST
509 && TREE_CODE (val2
) == INTEGER_CST
)
510 return tree_int_cst_compare (val1
, val2
);
512 if (poly_int_tree_p (val1
) && poly_int_tree_p (val2
))
514 if (known_eq (wi::to_poly_widest (val1
),
515 wi::to_poly_widest (val2
)))
517 if (known_lt (wi::to_poly_widest (val1
),
518 wi::to_poly_widest (val2
)))
520 if (known_gt (wi::to_poly_widest (val1
),
521 wi::to_poly_widest (val2
)))
529 if (TREE_CODE (val1
) == INTEGER_CST
&& TREE_CODE (val2
) == INTEGER_CST
)
531 /* We cannot compare overflowed values. */
532 if (TREE_OVERFLOW (val1
) || TREE_OVERFLOW (val2
))
535 return tree_int_cst_compare (val1
, val2
);
538 /* First see if VAL1 and VAL2 are not the same. */
539 if (operand_equal_p (val1
, val2
, 0))
542 fold_defer_overflow_warnings ();
544 /* If VAL1 is a lower address than VAL2, return -1. */
545 tree t
= fold_binary_to_constant (LT_EXPR
, boolean_type_node
, val1
, val2
);
546 if (t
&& integer_onep (t
))
548 fold_undefer_and_ignore_overflow_warnings ();
552 /* If VAL1 is a higher address than VAL2, return +1. */
553 t
= fold_binary_to_constant (LT_EXPR
, boolean_type_node
, val2
, val1
);
554 if (t
&& integer_onep (t
))
556 fold_undefer_and_ignore_overflow_warnings ();
560 /* If VAL1 is different than VAL2, return +2. */
561 t
= fold_binary_to_constant (NE_EXPR
, boolean_type_node
, val1
, val2
);
562 fold_undefer_and_ignore_overflow_warnings ();
563 if (t
&& integer_onep (t
))
570 /* Compare values like compare_values_warnv. */
573 compare_values (tree val1
, tree val2
)
576 return compare_values_warnv (val1
, val2
, &sop
);
579 /* If the types passed are supported, return TRUE, otherwise set VR to
580 VARYING and return FALSE. */
583 supported_types_p (value_range
*vr
,
587 if (!value_range::supports_p (type0
))
589 vr
->set_varying (type0
);
595 /* If any of the ranges passed are defined, return TRUE, otherwise set
596 VR to UNDEFINED and return FALSE. */
599 defined_ranges_p (value_range
*vr
,
600 const value_range
*vr0
, const value_range
*vr1
= NULL
)
602 if (vr0
->undefined_p () && (!vr1
|| vr1
->undefined_p ()))
604 vr
->set_undefined ();
610 /* Perform a binary operation on a pair of ranges. */
613 range_fold_binary_expr (value_range
*vr
,
616 const value_range
*vr0_
,
617 const value_range
*vr1_
)
619 if (!supported_types_p (vr
, expr_type
)
620 || !defined_ranges_p (vr
, vr0_
, vr1_
))
622 range_op_handler
op (code
, expr_type
);
625 vr
->set_varying (expr_type
);
629 value_range
vr0 (*vr0_
);
630 value_range
vr1 (*vr1_
);
631 if (vr0
.undefined_p ())
632 vr0
.set_varying (expr_type
);
633 if (vr1
.undefined_p ())
634 vr1
.set_varying (expr_type
);
635 vr0
.normalize_addresses ();
636 vr1
.normalize_addresses ();
637 if (!op
.fold_range (*vr
, expr_type
, vr0
, vr1
))
638 vr
->set_varying (expr_type
);
641 /* Perform a unary operation on a range. */
644 range_fold_unary_expr (value_range
*vr
,
645 enum tree_code code
, tree expr_type
,
646 const value_range
*vr0
,
649 if (!supported_types_p (vr
, expr_type
, vr0_type
)
650 || !defined_ranges_p (vr
, vr0
))
652 range_op_handler
op (code
, expr_type
);
655 vr
->set_varying (expr_type
);
659 value_range
vr0_cst (*vr0
);
660 vr0_cst
.normalize_addresses ();
661 if (!op
.fold_range (*vr
, expr_type
, vr0_cst
, value_range (expr_type
)))
662 vr
->set_varying (expr_type
);
665 /* Helper for overflow_comparison_p
667 OP0 CODE OP1 is a comparison. Examine the comparison and potentially
668 OP1's defining statement to see if it ultimately has the form
669 OP0 CODE (OP0 PLUS INTEGER_CST)
671 If so, return TRUE indicating this is an overflow test and store into
672 *NEW_CST an updated constant that can be used in a narrowed range test.
674 REVERSED indicates if the comparison was originally:
678 This affects how we build the updated constant. */
681 overflow_comparison_p_1 (enum tree_code code
, tree op0
, tree op1
,
682 bool reversed
, tree
*new_cst
)
684 /* See if this is a relational operation between two SSA_NAMES with
685 unsigned, overflow wrapping values. If so, check it more deeply. */
686 if ((code
== LT_EXPR
|| code
== LE_EXPR
687 || code
== GE_EXPR
|| code
== GT_EXPR
)
688 && TREE_CODE (op0
) == SSA_NAME
689 && TREE_CODE (op1
) == SSA_NAME
690 && INTEGRAL_TYPE_P (TREE_TYPE (op0
))
691 && TYPE_UNSIGNED (TREE_TYPE (op0
))
692 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (op0
)))
694 gimple
*op1_def
= SSA_NAME_DEF_STMT (op1
);
696 /* Now look at the defining statement of OP1 to see if it adds
697 or subtracts a nonzero constant from another operand. */
699 && is_gimple_assign (op1_def
)
700 && gimple_assign_rhs_code (op1_def
) == PLUS_EXPR
701 && TREE_CODE (gimple_assign_rhs2 (op1_def
)) == INTEGER_CST
702 && !integer_zerop (gimple_assign_rhs2 (op1_def
)))
704 tree target
= gimple_assign_rhs1 (op1_def
);
706 /* If we did not find our target SSA_NAME, then this is not
711 tree type
= TREE_TYPE (op0
);
712 wide_int max
= wi::max_value (TYPE_PRECISION (type
), UNSIGNED
);
713 tree inc
= gimple_assign_rhs2 (op1_def
);
715 *new_cst
= wide_int_to_tree (type
, max
+ wi::to_wide (inc
));
717 *new_cst
= wide_int_to_tree (type
, max
- wi::to_wide (inc
));
724 /* OP0 CODE OP1 is a comparison. Examine the comparison and potentially
725 OP1's defining statement to see if it ultimately has the form
726 OP0 CODE (OP0 PLUS INTEGER_CST)
728 If so, return TRUE indicating this is an overflow test and store into
729 *NEW_CST an updated constant that can be used in a narrowed range test.
731 These statements are left as-is in the IL to facilitate discovery of
732 {ADD,SUB}_OVERFLOW sequences later in the optimizer pipeline. But
733 the alternate range representation is often useful within VRP. */
736 overflow_comparison_p (tree_code code
, tree name
, tree val
, tree
*new_cst
)
738 if (overflow_comparison_p_1 (code
, name
, val
, false, new_cst
))
740 return overflow_comparison_p_1 (swap_tree_comparison (code
), val
, name
,
751 __builtin_unreachable ();
754 If x_3 has no other immediate uses (checked by caller), var is the
755 x_3 var, we can clear low 5 bits from the non-zero bitmask. */
758 maybe_set_nonzero_bits (edge e
, tree var
)
760 basic_block cond_bb
= e
->src
;
761 gimple
*stmt
= last_stmt (cond_bb
);
765 || gimple_code (stmt
) != GIMPLE_COND
766 || gimple_cond_code (stmt
) != ((e
->flags
& EDGE_TRUE_VALUE
)
768 || TREE_CODE (gimple_cond_lhs (stmt
)) != SSA_NAME
769 || !integer_zerop (gimple_cond_rhs (stmt
)))
772 stmt
= SSA_NAME_DEF_STMT (gimple_cond_lhs (stmt
));
773 if (!is_gimple_assign (stmt
)
774 || gimple_assign_rhs_code (stmt
) != BIT_AND_EXPR
775 || TREE_CODE (gimple_assign_rhs2 (stmt
)) != INTEGER_CST
)
777 if (gimple_assign_rhs1 (stmt
) != var
)
781 if (TREE_CODE (gimple_assign_rhs1 (stmt
)) != SSA_NAME
)
783 stmt2
= SSA_NAME_DEF_STMT (gimple_assign_rhs1 (stmt
));
784 if (!gimple_assign_cast_p (stmt2
)
785 || gimple_assign_rhs1 (stmt2
) != var
786 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (stmt2
))
787 || (TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (stmt
)))
788 != TYPE_PRECISION (TREE_TYPE (var
))))
791 cst
= gimple_assign_rhs2 (stmt
);
792 set_nonzero_bits (var
, wi::bit_and_not (get_nonzero_bits (var
),
796 /* Searches the case label vector VEC for the index *IDX of the CASE_LABEL
797 that includes the value VAL. The search is restricted to the range
798 [START_IDX, n - 1] where n is the size of VEC.
800 If there is a CASE_LABEL for VAL, its index is placed in IDX and true is
803 If there is no CASE_LABEL for VAL and there is one that is larger than VAL,
804 it is placed in IDX and false is returned.
806 If VAL is larger than any CASE_LABEL, n is placed on IDX and false is
810 find_case_label_index (gswitch
*stmt
, size_t start_idx
, tree val
, size_t *idx
)
812 size_t n
= gimple_switch_num_labels (stmt
);
815 /* Find case label for minimum of the value range or the next one.
816 At each iteration we are searching in [low, high - 1]. */
818 for (low
= start_idx
, high
= n
; high
!= low
; )
822 /* Note that i != high, so we never ask for n. */
823 size_t i
= (high
+ low
) / 2;
824 t
= gimple_switch_label (stmt
, i
);
826 /* Cache the result of comparing CASE_LOW and val. */
827 cmp
= tree_int_cst_compare (CASE_LOW (t
), val
);
831 /* Ranges cannot be empty. */
840 if (CASE_HIGH (t
) != NULL
841 && tree_int_cst_compare (CASE_HIGH (t
), val
) >= 0)
853 /* Searches the case label vector VEC for the range of CASE_LABELs that is used
854 for values between MIN and MAX. The first index is placed in MIN_IDX. The
855 last index is placed in MAX_IDX. If the range of CASE_LABELs is empty
856 then MAX_IDX < MIN_IDX.
857 Returns true if the default label is not needed. */
860 find_case_label_range (gswitch
*stmt
, tree min
, tree max
, size_t *min_idx
,
864 bool min_take_default
= !find_case_label_index (stmt
, 1, min
, &i
);
865 bool max_take_default
= !find_case_label_index (stmt
, i
, max
, &j
);
871 /* Only the default case label reached.
872 Return an empty range. */
879 bool take_default
= min_take_default
|| max_take_default
;
883 if (max_take_default
)
886 /* If the case label range is continuous, we do not need
887 the default case label. Verify that. */
888 high
= CASE_LOW (gimple_switch_label (stmt
, i
));
889 if (CASE_HIGH (gimple_switch_label (stmt
, i
)))
890 high
= CASE_HIGH (gimple_switch_label (stmt
, i
));
891 for (k
= i
+ 1; k
<= j
; ++k
)
893 low
= CASE_LOW (gimple_switch_label (stmt
, k
));
894 if (!integer_onep (int_const_binop (MINUS_EXPR
, low
, high
)))
900 if (CASE_HIGH (gimple_switch_label (stmt
, k
)))
901 high
= CASE_HIGH (gimple_switch_label (stmt
, k
));
906 return !take_default
;
910 /* Given a SWITCH_STMT, return the case label that encompasses the
911 known possible values for the switch operand. RANGE_OF_OP is a
912 range for the known values of the switch operand. */
915 find_case_label_range (gswitch
*switch_stmt
, const irange
*range_of_op
)
917 if (range_of_op
->undefined_p ()
918 || range_of_op
->varying_p ())
922 tree op
= gimple_switch_index (switch_stmt
);
923 tree type
= TREE_TYPE (op
);
924 tree tmin
= wide_int_to_tree (type
, range_of_op
->lower_bound ());
925 tree tmax
= wide_int_to_tree (type
, range_of_op
->upper_bound ());
926 find_case_label_range (switch_stmt
, tmin
, tmax
, &i
, &j
);
929 /* Look for exactly one label that encompasses the range of
931 tree label
= gimple_switch_label (switch_stmt
, i
);
933 = CASE_HIGH (label
) ? CASE_HIGH (label
) : CASE_LOW (label
);
934 int_range_max
label_range (CASE_LOW (label
), case_high
);
935 if (!types_compatible_p (label_range
.type (), range_of_op
->type ()))
936 range_cast (label_range
, range_of_op
->type ());
937 label_range
.intersect (*range_of_op
);
938 if (label_range
== *range_of_op
)
943 /* If there are no labels at all, take the default. */
944 return gimple_switch_label (switch_stmt
, 0);
948 /* Otherwise, there are various labels that can encompass
949 the range of operand. In which case, see if the range of
950 the operand is entirely *outside* the bounds of all the
951 (non-default) case labels. If so, take the default. */
952 unsigned n
= gimple_switch_num_labels (switch_stmt
);
953 tree min_label
= gimple_switch_label (switch_stmt
, 1);
954 tree max_label
= gimple_switch_label (switch_stmt
, n
- 1);
955 tree case_high
= CASE_HIGH (max_label
);
957 case_high
= CASE_LOW (max_label
);
958 int_range_max
label_range (CASE_LOW (min_label
), case_high
);
959 if (!types_compatible_p (label_range
.type (), range_of_op
->type ()))
960 range_cast (label_range
, range_of_op
->type ());
961 label_range
.intersect (*range_of_op
);
962 if (label_range
.undefined_p ())
963 return gimple_switch_label (switch_stmt
, 0);
974 // This is a ranger based folder which continues to use the dominator
975 // walk to access the substitute and fold machinery. Ranges are calculated
978 class rvrp_folder
: public substitute_and_fold_engine
982 rvrp_folder (gimple_ranger
*r
) : substitute_and_fold_engine (),
984 m_simplifier (r
, r
->non_executable_edge_flag
)
987 m_pta
= new pointer_equiv_analyzer (m_ranger
);
988 m_last_bb_stmt
= NULL
;
996 tree
value_of_expr (tree name
, gimple
*s
= NULL
) override
998 // Shortcircuit subst_and_fold callbacks for abnormal ssa_names.
999 if (TREE_CODE (name
) == SSA_NAME
&& SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name
))
1001 tree ret
= m_ranger
->value_of_expr (name
, s
);
1002 if (!ret
&& supported_pointer_equiv_p (name
))
1003 ret
= m_pta
->get_equiv (name
);
1007 tree
value_on_edge (edge e
, tree name
) override
1009 // Shortcircuit subst_and_fold callbacks for abnormal ssa_names.
1010 if (TREE_CODE (name
) == SSA_NAME
&& SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name
))
1012 tree ret
= m_ranger
->value_on_edge (e
, name
);
1013 if (!ret
&& supported_pointer_equiv_p (name
))
1014 ret
= m_pta
->get_equiv (name
);
1018 tree
value_of_stmt (gimple
*s
, tree name
= NULL
) override
1020 // Shortcircuit subst_and_fold callbacks for abnormal ssa_names.
1021 if (TREE_CODE (name
) == SSA_NAME
&& SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name
))
1023 return m_ranger
->value_of_stmt (s
, name
);
1026 void pre_fold_bb (basic_block bb
) override
1029 for (gphi_iterator gsi
= gsi_start_phis (bb
); !gsi_end_p (gsi
);
1031 m_ranger
->register_inferred_ranges (gsi
.phi ());
1032 m_last_bb_stmt
= last_stmt (bb
);
1035 void post_fold_bb (basic_block bb
) override
1038 if (cfun
->after_inlining
)
1039 m_unreachable
.maybe_register_block (bb
);
1042 void pre_fold_stmt (gimple
*stmt
) override
1044 m_pta
->visit_stmt (stmt
);
1045 // If this is the last stmt and there are inferred ranges, reparse the
1046 // block for transitive inferred ranges that occur earlier in the block.
1047 if (stmt
== m_last_bb_stmt
)
1048 m_ranger
->register_transitive_inferred_ranges (gimple_bb (stmt
));
1051 bool fold_stmt (gimple_stmt_iterator
*gsi
) override
1053 bool ret
= m_simplifier
.simplify (gsi
);
1055 ret
= m_ranger
->fold_stmt (gsi
, follow_single_use_edges
);
1056 m_ranger
->register_inferred_ranges (gsi_stmt (*gsi
));
1060 remove_unreachable m_unreachable
;
1062 DISABLE_COPY_AND_ASSIGN (rvrp_folder
);
1063 gimple_ranger
*m_ranger
;
1064 simplify_using_ranges m_simplifier
;
1065 pointer_equiv_analyzer
*m_pta
;
1066 gimple
*m_last_bb_stmt
;
1069 /* Main entry point for a VRP pass using just ranger. This can be called
1070 from anywhere to perform a VRP pass, including from EVRP. */
1073 execute_ranger_vrp (struct function
*fun
, bool warn_array_bounds_p
,
1076 loop_optimizer_init (LOOPS_NORMAL
| LOOPS_HAVE_RECORDED_EXITS
);
1077 rewrite_into_loop_closed_ssa (NULL
, TODO_update_ssa
);
1079 calculate_dominance_info (CDI_DOMINATORS
);
1081 set_all_edges_as_executable (fun
);
1082 gimple_ranger
*ranger
= enable_ranger (fun
, false);
1083 rvrp_folder
folder (ranger
);
1084 folder
.substitute_and_fold ();
1085 // Remove tagged builtin-unreachable and maybe update globals.
1086 folder
.m_unreachable
.remove_and_update_globals (final_p
);
1087 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1088 ranger
->dump (dump_file
);
1090 if (warn_array_bounds
&& warn_array_bounds_p
)
1092 // Set all edges as executable, except those ranger says aren't.
1093 int non_exec_flag
= ranger
->non_executable_edge_flag
;
1095 FOR_ALL_BB_FN (bb
, fun
)
1099 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
1100 if (e
->flags
& non_exec_flag
)
1101 e
->flags
&= ~EDGE_EXECUTABLE
;
1103 e
->flags
|= EDGE_EXECUTABLE
;
1106 array_bounds_checker
array_checker (fun
, ranger
);
1107 array_checker
.check ();
1110 disable_ranger (fun
);
1112 loop_optimizer_finalize ();
1118 const pass_data pass_data_vrp
=
1120 GIMPLE_PASS
, /* type */
1122 OPTGROUP_NONE
, /* optinfo_flags */
1123 TV_TREE_VRP
, /* tv_id */
1124 PROP_ssa
, /* properties_required */
1125 0, /* properties_provided */
1126 0, /* properties_destroyed */
1127 0, /* todo_flags_start */
1128 ( TODO_cleanup_cfg
| TODO_update_ssa
), /* todo_flags_finish */
1131 const pass_data pass_data_early_vrp
=
1133 GIMPLE_PASS
, /* type */
1135 OPTGROUP_NONE
, /* optinfo_flags */
1136 TV_TREE_EARLY_VRP
, /* tv_id */
1137 PROP_ssa
, /* properties_required */
1138 0, /* properties_provided */
1139 0, /* properties_destroyed */
1140 0, /* todo_flags_start */
1141 ( TODO_cleanup_cfg
| TODO_update_ssa
| TODO_verify_all
),
1144 static int vrp_pass_num
= 0;
1145 class pass_vrp
: public gimple_opt_pass
1148 pass_vrp (gcc::context
*ctxt
, const pass_data
&data_
)
1149 : gimple_opt_pass (data_
, ctxt
), data (data_
), warn_array_bounds_p (false),
1150 my_pass (vrp_pass_num
++)
1153 /* opt_pass methods: */
1154 opt_pass
* clone () final override
{ return new pass_vrp (m_ctxt
, data
); }
1155 void set_pass_param (unsigned int n
, bool param
) final override
1157 gcc_assert (n
== 0);
1158 warn_array_bounds_p
= param
;
1160 bool gate (function
*) final override
{ return flag_tree_vrp
!= 0; }
1161 unsigned int execute (function
*fun
) final override
1165 return execute_ranger_vrp (fun
, /*warn_array_bounds_p=*/false, false);
1167 return execute_ranger_vrp (fun
, warn_array_bounds_p
, my_pass
== 2);
1171 const pass_data
&data
;
1172 bool warn_array_bounds_p
;
1174 }; // class pass_vrp
1176 const pass_data pass_data_assumptions
=
1178 GIMPLE_PASS
, /* type */
1179 "assumptions", /* name */
1180 OPTGROUP_NONE
, /* optinfo_flags */
1181 TV_TREE_ASSUMPTIONS
, /* tv_id */
1182 PROP_ssa
, /* properties_required */
1183 PROP_assumptions_done
, /* properties_provided */
1184 0, /* properties_destroyed */
1185 0, /* todo_flags_start */
1186 0, /* todo_flags_end */
1189 class pass_assumptions
: public gimple_opt_pass
1192 pass_assumptions (gcc::context
*ctxt
)
1193 : gimple_opt_pass (pass_data_assumptions
, ctxt
)
1196 /* opt_pass methods: */
1197 bool gate (function
*fun
) final override
{ return fun
->assume_function
; }
1198 unsigned int execute (function
*) final override
1202 fprintf (dump_file
, "Assumptions :\n--------------\n");
1204 for (tree arg
= DECL_ARGUMENTS (cfun
->decl
); arg
; arg
= DECL_CHAIN (arg
))
1206 tree name
= ssa_default_def (cfun
, arg
);
1207 if (!name
|| !gimple_range_ssa_p (name
))
1209 tree type
= TREE_TYPE (name
);
1210 if (!Value_Range::supports_type_p (type
))
1212 Value_Range
assume_range (type
);
1213 if (query
.assume_range_p (assume_range
, name
))
1215 // Set the global range of NAME to anything calculated.
1216 set_range_info (name
, assume_range
);
1219 print_generic_expr (dump_file
, name
, TDF_SLIM
);
1220 fprintf (dump_file
, " -> ");
1221 assume_range
.dump (dump_file
);
1222 fputc ('\n', dump_file
);
1228 fputc ('\n', dump_file
);
1229 gimple_dump_cfg (dump_file
, dump_flags
& ~TDF_DETAILS
);
1230 if (dump_flags
& TDF_DETAILS
)
1231 query
.dump (dump_file
);
1233 return TODO_discard_function
;
1236 }; // class pass_assumptions
1241 make_pass_vrp (gcc::context
*ctxt
)
1243 return new pass_vrp (ctxt
, pass_data_vrp
);
1247 make_pass_early_vrp (gcc::context
*ctxt
)
1249 return new pass_vrp (ctxt
, pass_data_early_vrp
);
1253 make_pass_assumptions (gcc::context
*ctx
)
1255 return new pass_assumptions (ctx
);