1 /* Support routines for Value Range Propagation (VRP).
2 Copyright (C) 2005-2018 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"
25 #include "insn-codes.h"
30 #include "tree-pass.h"
32 #include "optabs-tree.h"
33 #include "gimple-pretty-print.h"
34 #include "diagnostic-core.h"
36 #include "fold-const.h"
37 #include "stor-layout.h"
40 #include "gimple-fold.h"
42 #include "gimple-iterator.h"
43 #include "gimple-walk.h"
46 #include "tree-ssa-loop-manip.h"
47 #include "tree-ssa-loop-niter.h"
48 #include "tree-ssa-loop.h"
49 #include "tree-into-ssa.h"
53 #include "tree-scalar-evolution.h"
54 #include "tree-ssa-propagate.h"
55 #include "tree-chrec.h"
56 #include "tree-ssa-threadupdate.h"
57 #include "tree-ssa-scopedtables.h"
58 #include "tree-ssa-threadedge.h"
59 #include "omp-general.h"
61 #include "case-cfn-macros.h"
63 #include "alloc-pool.h"
65 #include "tree-cfgcleanup.h"
66 #include "stringpool.h"
68 #include "vr-values.h"
70 #include "wide-int-range.h"
72 /* Set of SSA names found live during the RPO traversal of the function
73 for still active basic-blocks. */
76 /* Return true if the SSA name NAME is live on the edge E. */
79 live_on_edge (edge e
, tree name
)
81 return (live
[e
->dest
->index
]
82 && bitmap_bit_p (live
[e
->dest
->index
], SSA_NAME_VERSION (name
)));
85 /* Location information for ASSERT_EXPRs. Each instance of this
86 structure describes an ASSERT_EXPR for an SSA name. Since a single
87 SSA name may have more than one assertion associated with it, these
88 locations are kept in a linked list attached to the corresponding
92 /* Basic block where the assertion would be inserted. */
95 /* Some assertions need to be inserted on an edge (e.g., assertions
96 generated by COND_EXPRs). In those cases, BB will be NULL. */
99 /* Pointer to the statement that generated this assertion. */
100 gimple_stmt_iterator si
;
102 /* Predicate code for the ASSERT_EXPR. Must be COMPARISON_CLASS_P. */
103 enum tree_code comp_code
;
105 /* Value being compared against. */
108 /* Expression to compare. */
111 /* Next node in the linked list. */
115 /* If bit I is present, it means that SSA name N_i has a list of
116 assertions that should be inserted in the IL. */
117 static bitmap need_assert_for
;
119 /* Array of locations lists where to insert assertions. ASSERTS_FOR[I]
120 holds a list of ASSERT_LOCUS_T nodes that describe where
121 ASSERT_EXPRs for SSA name N_I should be inserted. */
122 static assert_locus
**asserts_for
;
124 vec
<edge
> to_remove_edges
;
125 vec
<switch_update
> to_update_switch_stmts
;
128 /* Return the maximum value for TYPE. */
131 vrp_val_max (const_tree type
)
133 if (!INTEGRAL_TYPE_P (type
))
136 return TYPE_MAX_VALUE (type
);
139 /* Return the minimum value for TYPE. */
142 vrp_val_min (const_tree type
)
144 if (!INTEGRAL_TYPE_P (type
))
147 return TYPE_MIN_VALUE (type
);
150 /* Return whether VAL is equal to the maximum value of its type.
151 We can't do a simple equality comparison with TYPE_MAX_VALUE because
152 C typedefs and Ada subtypes can produce types whose TYPE_MAX_VALUE
153 is not == to the integer constant with the same value in the type. */
156 vrp_val_is_max (const_tree val
)
158 tree type_max
= vrp_val_max (TREE_TYPE (val
));
159 return (val
== type_max
160 || (type_max
!= NULL_TREE
161 && operand_equal_p (val
, type_max
, 0)));
164 /* Return whether VAL is equal to the minimum value of its type. */
167 vrp_val_is_min (const_tree val
)
169 tree type_min
= vrp_val_min (TREE_TYPE (val
));
170 return (val
== type_min
171 || (type_min
!= NULL_TREE
172 && operand_equal_p (val
, type_min
, 0)));
175 /* VR_TYPE describes a range with mininum value *MIN and maximum
176 value *MAX. Restrict the range to the set of values that have
177 no bits set outside NONZERO_BITS. Update *MIN and *MAX and
178 return the new range type.
180 SGN gives the sign of the values described by the range. */
182 enum value_range_type
183 intersect_range_with_nonzero_bits (enum value_range_type vr_type
,
184 wide_int
*min
, wide_int
*max
,
185 const wide_int
&nonzero_bits
,
188 if (vr_type
== VR_ANTI_RANGE
)
190 /* The VR_ANTI_RANGE is equivalent to the union of the ranges
191 A: [-INF, *MIN) and B: (*MAX, +INF]. First use NONZERO_BITS
192 to create an inclusive upper bound for A and an inclusive lower
194 wide_int a_max
= wi::round_down_for_mask (*min
- 1, nonzero_bits
);
195 wide_int b_min
= wi::round_up_for_mask (*max
+ 1, nonzero_bits
);
197 /* If the calculation of A_MAX wrapped, A is effectively empty
198 and A_MAX is the highest value that satisfies NONZERO_BITS.
199 Likewise if the calculation of B_MIN wrapped, B is effectively
200 empty and B_MIN is the lowest value that satisfies NONZERO_BITS. */
201 bool a_empty
= wi::ge_p (a_max
, *min
, sgn
);
202 bool b_empty
= wi::le_p (b_min
, *max
, sgn
);
204 /* If both A and B are empty, there are no valid values. */
205 if (a_empty
&& b_empty
)
208 /* If exactly one of A or B is empty, return a VR_RANGE for the
210 if (a_empty
|| b_empty
)
214 gcc_checking_assert (wi::le_p (*min
, *max
, sgn
));
218 /* Update the VR_ANTI_RANGE bounds. */
221 gcc_checking_assert (wi::le_p (*min
, *max
, sgn
));
223 /* Now check whether the excluded range includes any values that
224 satisfy NONZERO_BITS. If not, switch to a full VR_RANGE. */
225 if (wi::round_up_for_mask (*min
, nonzero_bits
) == b_min
)
227 unsigned int precision
= min
->get_precision ();
228 *min
= wi::min_value (precision
, sgn
);
229 *max
= wi::max_value (precision
, sgn
);
233 if (vr_type
== VR_RANGE
)
235 *max
= wi::round_down_for_mask (*max
, nonzero_bits
);
237 /* Check that the range contains at least one valid value. */
238 if (wi::gt_p (*min
, *max
, sgn
))
241 *min
= wi::round_up_for_mask (*min
, nonzero_bits
);
242 gcc_checking_assert (wi::le_p (*min
, *max
, sgn
));
247 /* Set value range VR to VR_UNDEFINED. */
250 set_value_range_to_undefined (value_range
*vr
)
252 vr
->type
= VR_UNDEFINED
;
253 vr
->min
= vr
->max
= NULL_TREE
;
255 bitmap_clear (vr
->equiv
);
258 /* Set value range VR to VR_VARYING. */
261 set_value_range_to_varying (value_range
*vr
)
263 vr
->type
= VR_VARYING
;
264 vr
->min
= vr
->max
= NULL_TREE
;
266 bitmap_clear (vr
->equiv
);
269 /* Set value range VR to {T, MIN, MAX, EQUIV}. */
272 set_value_range (value_range
*vr
, enum value_range_type t
, tree min
,
273 tree max
, bitmap equiv
)
275 /* Check the validity of the range. */
277 && (t
== VR_RANGE
|| t
== VR_ANTI_RANGE
))
281 gcc_assert (min
&& max
);
283 gcc_assert (!TREE_OVERFLOW_P (min
) && !TREE_OVERFLOW_P (max
));
285 if (INTEGRAL_TYPE_P (TREE_TYPE (min
)) && t
== VR_ANTI_RANGE
)
286 gcc_assert (!vrp_val_is_min (min
) || !vrp_val_is_max (max
));
288 cmp
= compare_values (min
, max
);
289 gcc_assert (cmp
== 0 || cmp
== -1 || cmp
== -2);
293 && (t
== VR_UNDEFINED
|| t
== VR_VARYING
))
295 gcc_assert (min
== NULL_TREE
&& max
== NULL_TREE
);
296 gcc_assert (equiv
== NULL
|| bitmap_empty_p (equiv
));
303 /* Since updating the equivalence set involves deep copying the
304 bitmaps, only do it if absolutely necessary.
306 All equivalence bitmaps are allocated from the same obstack. So
307 we can use the obstack associated with EQUIV to allocate vr->equiv. */
308 if (vr
->equiv
== NULL
310 vr
->equiv
= BITMAP_ALLOC (equiv
->obstack
);
312 if (equiv
!= vr
->equiv
)
314 if (equiv
&& !bitmap_empty_p (equiv
))
315 bitmap_copy (vr
->equiv
, equiv
);
317 bitmap_clear (vr
->equiv
);
322 /* Set value range VR to the canonical form of {T, MIN, MAX, EQUIV}.
323 This means adjusting T, MIN and MAX representing the case of a
324 wrapping range with MAX < MIN covering [MIN, type_max] U [type_min, MAX]
325 as anti-rage ~[MAX+1, MIN-1]. Likewise for wrapping anti-ranges.
326 In corner cases where MAX+1 or MIN-1 wraps this will fall back
328 This routine exists to ease canonicalization in the case where we
329 extract ranges from var + CST op limit. */
332 set_and_canonicalize_value_range (value_range
*vr
, enum value_range_type t
,
333 tree min
, tree max
, bitmap equiv
)
335 /* Use the canonical setters for VR_UNDEFINED and VR_VARYING. */
336 if (t
== VR_UNDEFINED
)
338 set_value_range_to_undefined (vr
);
341 else if (t
== VR_VARYING
)
343 set_value_range_to_varying (vr
);
347 /* Nothing to canonicalize for symbolic ranges. */
348 if (TREE_CODE (min
) != INTEGER_CST
349 || TREE_CODE (max
) != INTEGER_CST
)
351 set_value_range (vr
, t
, min
, max
, equiv
);
355 /* Wrong order for min and max, to swap them and the VR type we need
357 if (tree_int_cst_lt (max
, min
))
361 /* For one bit precision if max < min, then the swapped
362 range covers all values, so for VR_RANGE it is varying and
363 for VR_ANTI_RANGE empty range, so drop to varying as well. */
364 if (TYPE_PRECISION (TREE_TYPE (min
)) == 1)
366 set_value_range_to_varying (vr
);
370 one
= build_int_cst (TREE_TYPE (min
), 1);
371 tmp
= int_const_binop (PLUS_EXPR
, max
, one
);
372 max
= int_const_binop (MINUS_EXPR
, min
, one
);
375 /* There's one corner case, if we had [C+1, C] before we now have
376 that again. But this represents an empty value range, so drop
377 to varying in this case. */
378 if (tree_int_cst_lt (max
, min
))
380 set_value_range_to_varying (vr
);
384 t
= t
== VR_RANGE
? VR_ANTI_RANGE
: VR_RANGE
;
387 /* Anti-ranges that can be represented as ranges should be so. */
388 if (t
== VR_ANTI_RANGE
)
390 /* For -fstrict-enums we may receive out-of-range ranges so consider
391 values < -INF and values > INF as -INF/INF as well. */
392 tree type
= TREE_TYPE (min
);
393 bool is_min
= (INTEGRAL_TYPE_P (type
)
394 && tree_int_cst_compare (min
, TYPE_MIN_VALUE (type
)) <= 0);
395 bool is_max
= (INTEGRAL_TYPE_P (type
)
396 && tree_int_cst_compare (max
, TYPE_MAX_VALUE (type
)) >= 0);
398 if (is_min
&& is_max
)
400 /* We cannot deal with empty ranges, drop to varying.
401 ??? This could be VR_UNDEFINED instead. */
402 set_value_range_to_varying (vr
);
405 else if (TYPE_PRECISION (TREE_TYPE (min
)) == 1
406 && (is_min
|| is_max
))
408 /* Non-empty boolean ranges can always be represented
409 as a singleton range. */
411 min
= max
= vrp_val_max (TREE_TYPE (min
));
413 min
= max
= vrp_val_min (TREE_TYPE (min
));
417 /* As a special exception preserve non-null ranges. */
418 && !(TYPE_UNSIGNED (TREE_TYPE (min
))
419 && integer_zerop (max
)))
421 tree one
= build_int_cst (TREE_TYPE (max
), 1);
422 min
= int_const_binop (PLUS_EXPR
, max
, one
);
423 max
= vrp_val_max (TREE_TYPE (max
));
428 tree one
= build_int_cst (TREE_TYPE (min
), 1);
429 max
= int_const_binop (MINUS_EXPR
, min
, one
);
430 min
= vrp_val_min (TREE_TYPE (min
));
435 /* Do not drop [-INF(OVF), +INF(OVF)] to varying. (OVF) has to be sticky
436 to make sure VRP iteration terminates, otherwise we can get into
439 set_value_range (vr
, t
, min
, max
, equiv
);
442 /* Copy value range FROM into value range TO. */
445 copy_value_range (value_range
*to
, const value_range
*from
)
447 set_value_range (to
, from
->type
, from
->min
, from
->max
, from
->equiv
);
450 /* Set value range VR to a single value. This function is only called
451 with values we get from statements, and exists to clear the
452 TREE_OVERFLOW flag. */
455 set_value_range_to_value (value_range
*vr
, tree val
, bitmap equiv
)
457 gcc_assert (is_gimple_min_invariant (val
));
458 if (TREE_OVERFLOW_P (val
))
459 val
= drop_tree_overflow (val
);
460 set_value_range (vr
, VR_RANGE
, val
, val
, equiv
);
463 /* Set value range VR to a non-NULL range of type TYPE. */
466 set_value_range_to_nonnull (value_range
*vr
, tree type
)
468 tree zero
= build_int_cst (type
, 0);
469 set_value_range (vr
, VR_ANTI_RANGE
, zero
, zero
, vr
->equiv
);
473 /* Set value range VR to a NULL range of type TYPE. */
476 set_value_range_to_null (value_range
*vr
, tree type
)
478 set_value_range_to_value (vr
, build_int_cst (type
, 0), vr
->equiv
);
481 /* Return true, if VAL1 and VAL2 are equal values for VRP purposes. */
484 vrp_operand_equal_p (const_tree val1
, const_tree val2
)
488 if (!val1
|| !val2
|| !operand_equal_p (val1
, val2
, 0))
493 /* Return true, if the bitmaps B1 and B2 are equal. */
496 vrp_bitmap_equal_p (const_bitmap b1
, const_bitmap b2
)
499 || ((!b1
|| bitmap_empty_p (b1
))
500 && (!b2
|| bitmap_empty_p (b2
)))
502 && bitmap_equal_p (b1
, b2
)));
505 /* Return true if VR is [0, 0]. */
508 range_is_null (const value_range
*vr
)
510 return vr
->type
== VR_RANGE
511 && integer_zerop (vr
->min
)
512 && integer_zerop (vr
->max
);
515 /* Return true if max and min of VR are INTEGER_CST. It's not necessary
519 range_int_cst_p (const value_range
*vr
)
521 return (vr
->type
== VR_RANGE
522 && TREE_CODE (vr
->max
) == INTEGER_CST
523 && TREE_CODE (vr
->min
) == INTEGER_CST
);
526 /* Return true if VR is a INTEGER_CST singleton. */
529 range_int_cst_singleton_p (const value_range
*vr
)
531 return (range_int_cst_p (vr
)
532 && tree_int_cst_equal (vr
->min
, vr
->max
));
535 /* Return true if value range VR involves at least one symbol. */
538 symbolic_range_p (const value_range
*vr
)
540 return (!is_gimple_min_invariant (vr
->min
)
541 || !is_gimple_min_invariant (vr
->max
));
544 /* Return the single symbol (an SSA_NAME) contained in T if any, or NULL_TREE
545 otherwise. We only handle additive operations and set NEG to true if the
546 symbol is negated and INV to the invariant part, if any. */
549 get_single_symbol (tree t
, bool *neg
, tree
*inv
)
557 if (TREE_CODE (t
) == PLUS_EXPR
558 || TREE_CODE (t
) == POINTER_PLUS_EXPR
559 || TREE_CODE (t
) == MINUS_EXPR
)
561 if (is_gimple_min_invariant (TREE_OPERAND (t
, 0)))
563 neg_
= (TREE_CODE (t
) == MINUS_EXPR
);
564 inv_
= TREE_OPERAND (t
, 0);
565 t
= TREE_OPERAND (t
, 1);
567 else if (is_gimple_min_invariant (TREE_OPERAND (t
, 1)))
570 inv_
= TREE_OPERAND (t
, 1);
571 t
= TREE_OPERAND (t
, 0);
582 if (TREE_CODE (t
) == NEGATE_EXPR
)
584 t
= TREE_OPERAND (t
, 0);
588 if (TREE_CODE (t
) != SSA_NAME
)
591 if (inv_
&& TREE_OVERFLOW_P (inv_
))
592 inv_
= drop_tree_overflow (inv_
);
599 /* The reverse operation: build a symbolic expression with TYPE
600 from symbol SYM, negated according to NEG, and invariant INV. */
603 build_symbolic_expr (tree type
, tree sym
, bool neg
, tree inv
)
605 const bool pointer_p
= POINTER_TYPE_P (type
);
609 t
= build1 (NEGATE_EXPR
, type
, t
);
611 if (integer_zerop (inv
))
614 return build2 (pointer_p
? POINTER_PLUS_EXPR
: PLUS_EXPR
, type
, t
, inv
);
620 -2 if those are incomparable. */
622 operand_less_p (tree val
, tree val2
)
624 /* LT is folded faster than GE and others. Inline the common case. */
625 if (TREE_CODE (val
) == INTEGER_CST
&& TREE_CODE (val2
) == INTEGER_CST
)
626 return tree_int_cst_lt (val
, val2
);
631 fold_defer_overflow_warnings ();
633 tcmp
= fold_binary_to_constant (LT_EXPR
, boolean_type_node
, val
, val2
);
635 fold_undefer_and_ignore_overflow_warnings ();
638 || TREE_CODE (tcmp
) != INTEGER_CST
)
641 if (!integer_zerop (tcmp
))
648 /* Compare two values VAL1 and VAL2. Return
650 -2 if VAL1 and VAL2 cannot be compared at compile-time,
653 +1 if VAL1 > VAL2, and
656 This is similar to tree_int_cst_compare but supports pointer values
657 and values that cannot be compared at compile time.
659 If STRICT_OVERFLOW_P is not NULL, then set *STRICT_OVERFLOW_P to
660 true if the return value is only valid if we assume that signed
661 overflow is undefined. */
664 compare_values_warnv (tree val1
, tree val2
, bool *strict_overflow_p
)
669 /* Below we rely on the fact that VAL1 and VAL2 are both pointers or
671 gcc_assert (POINTER_TYPE_P (TREE_TYPE (val1
))
672 == POINTER_TYPE_P (TREE_TYPE (val2
)));
674 /* Convert the two values into the same type. This is needed because
675 sizetype causes sign extension even for unsigned types. */
676 val2
= fold_convert (TREE_TYPE (val1
), val2
);
677 STRIP_USELESS_TYPE_CONVERSION (val2
);
679 const bool overflow_undefined
680 = INTEGRAL_TYPE_P (TREE_TYPE (val1
))
681 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (val1
));
684 tree sym1
= get_single_symbol (val1
, &neg1
, &inv1
);
685 tree sym2
= get_single_symbol (val2
, &neg2
, &inv2
);
687 /* If VAL1 and VAL2 are of the form '[-]NAME [+ CST]', return -1 or +1
688 accordingly. If VAL1 and VAL2 don't use the same name, return -2. */
691 /* Both values must use the same name with the same sign. */
692 if (sym1
!= sym2
|| neg1
!= neg2
)
695 /* [-]NAME + CST == [-]NAME + CST. */
699 /* If overflow is defined we cannot simplify more. */
700 if (!overflow_undefined
)
703 if (strict_overflow_p
!= NULL
704 /* Symbolic range building sets TREE_NO_WARNING to declare
705 that overflow doesn't happen. */
706 && (!inv1
|| !TREE_NO_WARNING (val1
))
707 && (!inv2
|| !TREE_NO_WARNING (val2
)))
708 *strict_overflow_p
= true;
711 inv1
= build_int_cst (TREE_TYPE (val1
), 0);
713 inv2
= build_int_cst (TREE_TYPE (val2
), 0);
715 return wi::cmp (wi::to_wide (inv1
), wi::to_wide (inv2
),
716 TYPE_SIGN (TREE_TYPE (val1
)));
719 const bool cst1
= is_gimple_min_invariant (val1
);
720 const bool cst2
= is_gimple_min_invariant (val2
);
722 /* If one is of the form '[-]NAME + CST' and the other is constant, then
723 it might be possible to say something depending on the constants. */
724 if ((sym1
&& inv1
&& cst2
) || (sym2
&& inv2
&& cst1
))
726 if (!overflow_undefined
)
729 if (strict_overflow_p
!= NULL
730 /* Symbolic range building sets TREE_NO_WARNING to declare
731 that overflow doesn't happen. */
732 && (!sym1
|| !TREE_NO_WARNING (val1
))
733 && (!sym2
|| !TREE_NO_WARNING (val2
)))
734 *strict_overflow_p
= true;
736 const signop sgn
= TYPE_SIGN (TREE_TYPE (val1
));
737 tree cst
= cst1
? val1
: val2
;
738 tree inv
= cst1
? inv2
: inv1
;
740 /* Compute the difference between the constants. If it overflows or
741 underflows, this means that we can trivially compare the NAME with
742 it and, consequently, the two values with each other. */
743 wide_int diff
= wi::to_wide (cst
) - wi::to_wide (inv
);
744 if (wi::cmp (0, wi::to_wide (inv
), sgn
)
745 != wi::cmp (diff
, wi::to_wide (cst
), sgn
))
747 const int res
= wi::cmp (wi::to_wide (cst
), wi::to_wide (inv
), sgn
);
748 return cst1
? res
: -res
;
754 /* We cannot say anything more for non-constants. */
758 if (!POINTER_TYPE_P (TREE_TYPE (val1
)))
760 /* We cannot compare overflowed values. */
761 if (TREE_OVERFLOW (val1
) || TREE_OVERFLOW (val2
))
764 if (TREE_CODE (val1
) == INTEGER_CST
765 && TREE_CODE (val2
) == INTEGER_CST
)
766 return tree_int_cst_compare (val1
, val2
);
768 if (poly_int_tree_p (val1
) && poly_int_tree_p (val2
))
770 if (known_eq (wi::to_poly_widest (val1
),
771 wi::to_poly_widest (val2
)))
773 if (known_lt (wi::to_poly_widest (val1
),
774 wi::to_poly_widest (val2
)))
776 if (known_gt (wi::to_poly_widest (val1
),
777 wi::to_poly_widest (val2
)))
787 /* First see if VAL1 and VAL2 are not the same. */
788 if (val1
== val2
|| operand_equal_p (val1
, val2
, 0))
791 /* If VAL1 is a lower address than VAL2, return -1. */
792 if (operand_less_p (val1
, val2
) == 1)
795 /* If VAL1 is a higher address than VAL2, return +1. */
796 if (operand_less_p (val2
, val1
) == 1)
799 /* If VAL1 is different than VAL2, return +2.
800 For integer constants we either have already returned -1 or 1
801 or they are equivalent. We still might succeed in proving
802 something about non-trivial operands. */
803 if (TREE_CODE (val1
) != INTEGER_CST
804 || TREE_CODE (val2
) != INTEGER_CST
)
806 t
= fold_binary_to_constant (NE_EXPR
, boolean_type_node
, val1
, val2
);
807 if (t
&& integer_onep (t
))
815 /* Compare values like compare_values_warnv. */
818 compare_values (tree val1
, tree val2
)
821 return compare_values_warnv (val1
, val2
, &sop
);
825 /* Return 1 if VAL is inside value range MIN <= VAL <= MAX,
826 0 if VAL is not inside [MIN, MAX],
827 -2 if we cannot tell either way.
829 Benchmark compile/20001226-1.c compilation time after changing this
833 value_inside_range (tree val
, tree min
, tree max
)
837 cmp1
= operand_less_p (val
, min
);
843 cmp2
= operand_less_p (max
, val
);
851 /* Return true if value ranges VR0 and VR1 have a non-empty
854 Benchmark compile/20001226-1.c compilation time after changing this
859 value_ranges_intersect_p (const value_range
*vr0
, const value_range
*vr1
)
861 /* The value ranges do not intersect if the maximum of the first range is
862 less than the minimum of the second range or vice versa.
863 When those relations are unknown, we can't do any better. */
864 if (operand_less_p (vr0
->max
, vr1
->min
) != 0)
866 if (operand_less_p (vr1
->max
, vr0
->min
) != 0)
872 /* Return TRUE if *VR includes the value zero. */
875 range_includes_zero_p (const value_range
*vr
)
877 if (vr
->type
== VR_VARYING
)
880 /* Ughh, we don't know. We choose not to optimize. */
881 if (vr
->type
== VR_UNDEFINED
)
884 tree zero
= build_int_cst (TREE_TYPE (vr
->min
), 0);
885 if (vr
->type
== VR_ANTI_RANGE
)
887 int res
= value_inside_range (zero
, vr
->min
, vr
->max
);
888 return res
== 0 || res
== -2;
890 return value_inside_range (zero
, vr
->min
, vr
->max
) != 0;
893 /* Return true if *VR is know to only contain nonnegative values. */
896 value_range_nonnegative_p (const value_range
*vr
)
898 /* Testing for VR_ANTI_RANGE is not useful here as any anti-range
899 which would return a useful value should be encoded as a
901 if (vr
->type
== VR_RANGE
)
903 int result
= compare_values (vr
->min
, integer_zero_node
);
904 return (result
== 0 || result
== 1);
910 /* If *VR has a value rante that is a single constant value return that,
911 otherwise return NULL_TREE. */
914 value_range_constant_singleton (const value_range
*vr
)
916 if (vr
->type
== VR_RANGE
917 && vrp_operand_equal_p (vr
->min
, vr
->max
)
918 && is_gimple_min_invariant (vr
->min
))
924 /* Value range wrapper for wide_int_range_set_zero_nonzero_bits.
926 Compute MAY_BE_NONZERO and MUST_BE_NONZERO bit masks for range in VR.
928 Return TRUE if VR was a constant range and we were able to compute
932 vrp_set_zero_nonzero_bits (const tree expr_type
,
933 const value_range
*vr
,
934 wide_int
*may_be_nonzero
,
935 wide_int
*must_be_nonzero
)
937 if (!range_int_cst_p (vr
))
939 *may_be_nonzero
= wi::minus_one (TYPE_PRECISION (expr_type
));
940 *must_be_nonzero
= wi::zero (TYPE_PRECISION (expr_type
));
943 wide_int_range_set_zero_nonzero_bits (TYPE_SIGN (expr_type
),
944 wi::to_wide (vr
->min
),
945 wi::to_wide (vr
->max
),
946 *may_be_nonzero
, *must_be_nonzero
);
950 /* Create two value-ranges in *VR0 and *VR1 from the anti-range *AR
951 so that *VR0 U *VR1 == *AR. Returns true if that is possible,
952 false otherwise. If *AR can be represented with a single range
953 *VR1 will be VR_UNDEFINED. */
956 ranges_from_anti_range (const value_range
*ar
,
957 value_range
*vr0
, value_range
*vr1
)
959 tree type
= TREE_TYPE (ar
->min
);
961 vr0
->type
= VR_UNDEFINED
;
962 vr1
->type
= VR_UNDEFINED
;
964 /* As a future improvement, we could handle ~[0, A] as: [-INF, -1] U
965 [A+1, +INF]. Not sure if this helps in practice, though. */
967 if (ar
->type
!= VR_ANTI_RANGE
968 || TREE_CODE (ar
->min
) != INTEGER_CST
969 || TREE_CODE (ar
->max
) != INTEGER_CST
970 || !vrp_val_min (type
)
971 || !vrp_val_max (type
))
974 if (!vrp_val_is_min (ar
->min
))
976 vr0
->type
= VR_RANGE
;
977 vr0
->min
= vrp_val_min (type
);
978 vr0
->max
= wide_int_to_tree (type
, wi::to_wide (ar
->min
) - 1);
980 if (!vrp_val_is_max (ar
->max
))
982 vr1
->type
= VR_RANGE
;
983 vr1
->min
= wide_int_to_tree (type
, wi::to_wide (ar
->max
) + 1);
984 vr1
->max
= vrp_val_max (type
);
986 if (vr0
->type
== VR_UNDEFINED
)
989 vr1
->type
= VR_UNDEFINED
;
992 return vr0
->type
!= VR_UNDEFINED
;
995 /* Extract the components of a value range into a pair of wide ints in
998 If the value range is anything but a VR_*RANGE of constants, the
999 resulting wide ints are set to [-MIN, +MAX] for the type. */
1002 extract_range_into_wide_ints (const value_range
*vr
,
1003 signop sign
, unsigned prec
,
1004 wide_int
&wmin
, wide_int
&wmax
)
1006 if ((vr
->type
== VR_RANGE
1007 || vr
->type
== VR_ANTI_RANGE
)
1008 && TREE_CODE (vr
->min
) == INTEGER_CST
1009 && TREE_CODE (vr
->max
) == INTEGER_CST
)
1011 wmin
= wi::to_wide (vr
->min
);
1012 wmax
= wi::to_wide (vr
->max
);
1016 wmin
= wi::min_value (prec
, sign
);
1017 wmax
= wi::max_value (prec
, sign
);
1021 /* Value range wrapper for wide_int_range_shift_undefined_p. */
1024 vrp_shift_undefined_p (const value_range
&shifter
, unsigned prec
)
1026 tree type
= TREE_TYPE (shifter
.min
);
1027 return wide_int_range_shift_undefined_p (TYPE_SIGN (type
), prec
,
1028 wi::to_wide (shifter
.min
),
1029 wi::to_wide (shifter
.max
));
1032 /* Value range wrapper for wide_int_range_multiplicative_op:
1034 *VR = *VR0 .CODE. *VR1. */
1037 extract_range_from_multiplicative_op (value_range
*vr
,
1038 enum tree_code code
,
1039 const value_range
*vr0
,
1040 const value_range
*vr1
)
1042 gcc_assert (code
== MULT_EXPR
1043 || code
== TRUNC_DIV_EXPR
1044 || code
== FLOOR_DIV_EXPR
1045 || code
== CEIL_DIV_EXPR
1046 || code
== EXACT_DIV_EXPR
1047 || code
== ROUND_DIV_EXPR
1048 || code
== RSHIFT_EXPR
1049 || code
== LSHIFT_EXPR
);
1050 gcc_assert (vr0
->type
== VR_RANGE
&& vr0
->type
== vr1
->type
);
1052 tree type
= TREE_TYPE (vr0
->min
);
1053 wide_int res_lb
, res_ub
;
1054 wide_int vr0_lb
= wi::to_wide (vr0
->min
);
1055 wide_int vr0_ub
= wi::to_wide (vr0
->max
);
1056 wide_int vr1_lb
= wi::to_wide (vr1
->min
);
1057 wide_int vr1_ub
= wi::to_wide (vr1
->max
);
1058 bool overflow_undefined
= TYPE_OVERFLOW_UNDEFINED (type
);
1059 bool overflow_wraps
= TYPE_OVERFLOW_WRAPS (type
);
1060 unsigned prec
= TYPE_PRECISION (type
);
1062 if (wide_int_range_multiplicative_op (res_lb
, res_ub
,
1063 code
, TYPE_SIGN (type
), prec
,
1064 vr0_lb
, vr0_ub
, vr1_lb
, vr1_ub
,
1065 overflow_undefined
, overflow_wraps
))
1066 set_and_canonicalize_value_range (vr
, VR_RANGE
,
1067 wide_int_to_tree (type
, res_lb
),
1068 wide_int_to_tree (type
, res_ub
), NULL
);
1070 set_value_range_to_varying (vr
);
1073 /* If BOUND will include a symbolic bound, adjust it accordingly,
1074 otherwise leave it as is.
1076 CODE is the original operation that combined the bounds (PLUS_EXPR
1079 TYPE is the type of the original operation.
1081 SYM_OPn is the symbolic for OPn if it has a symbolic.
1083 NEG_OPn is TRUE if the OPn was negated. */
1086 adjust_symbolic_bound (tree
&bound
, enum tree_code code
, tree type
,
1087 tree sym_op0
, tree sym_op1
,
1088 bool neg_op0
, bool neg_op1
)
1090 bool minus_p
= (code
== MINUS_EXPR
);
1091 /* If the result bound is constant, we're done; otherwise, build the
1092 symbolic lower bound. */
1093 if (sym_op0
== sym_op1
)
1096 bound
= build_symbolic_expr (type
, sym_op0
,
1100 /* We may not negate if that might introduce
1101 undefined overflow. */
1104 || TYPE_OVERFLOW_WRAPS (type
))
1105 bound
= build_symbolic_expr (type
, sym_op1
,
1106 neg_op1
^ minus_p
, bound
);
1112 /* Combine OP1 and OP1, which are two parts of a bound, into one wide
1113 int bound according to CODE. CODE is the operation combining the
1114 bound (either a PLUS_EXPR or a MINUS_EXPR).
1116 TYPE is the type of the combine operation.
1118 WI is the wide int to store the result.
1120 OVF is -1 if an underflow occurred, +1 if an overflow occurred or 0
1121 if over/underflow occurred. */
1124 combine_bound (enum tree_code code
, wide_int
&wi
, wi::overflow_type
&ovf
,
1125 tree type
, tree op0
, tree op1
)
1127 bool minus_p
= (code
== MINUS_EXPR
);
1128 const signop sgn
= TYPE_SIGN (type
);
1129 const unsigned int prec
= TYPE_PRECISION (type
);
1131 /* Combine the bounds, if any. */
1135 wi
= wi::sub (wi::to_wide (op0
), wi::to_wide (op1
), sgn
, &ovf
);
1137 wi
= wi::add (wi::to_wide (op0
), wi::to_wide (op1
), sgn
, &ovf
);
1140 wi
= wi::to_wide (op0
);
1144 wi
= wi::neg (wi::to_wide (op1
), &ovf
);
1146 wi
= wi::to_wide (op1
);
1149 wi
= wi::shwi (0, prec
);
1152 /* Given a range in [WMIN, WMAX], adjust it for possible overflow and
1153 put the result in VR.
1155 TYPE is the type of the range.
1157 MIN_OVF and MAX_OVF indicate what type of overflow, if any,
1158 occurred while originally calculating WMIN or WMAX. -1 indicates
1159 underflow. +1 indicates overflow. 0 indicates neither. */
1162 set_value_range_with_overflow (value_range
&vr
,
1164 const wide_int
&wmin
, const wide_int
&wmax
,
1165 wi::overflow_type min_ovf
,
1166 wi::overflow_type max_ovf
)
1168 const signop sgn
= TYPE_SIGN (type
);
1169 const unsigned int prec
= TYPE_PRECISION (type
);
1172 if (TYPE_OVERFLOW_WRAPS (type
))
1174 /* If overflow wraps, truncate the values and adjust the
1175 range kind and bounds appropriately. */
1176 wide_int tmin
= wide_int::from (wmin
, prec
, sgn
);
1177 wide_int tmax
= wide_int::from (wmax
, prec
, sgn
);
1178 if ((min_ovf
!= wi::OVF_NONE
) == (max_ovf
!= wi::OVF_NONE
))
1180 /* No overflow or both overflow or underflow. The
1181 range kind stays VR_RANGE. */
1182 vr
.min
= wide_int_to_tree (type
, tmin
);
1183 vr
.max
= wide_int_to_tree (type
, tmax
);
1185 else if ((min_ovf
== wi::OVF_UNDERFLOW
&& max_ovf
== wi::OVF_NONE
)
1186 || (max_ovf
== wi::OVF_OVERFLOW
&& min_ovf
== wi::OVF_NONE
))
1188 /* Min underflow or max overflow. The range kind
1189 changes to VR_ANTI_RANGE. */
1190 bool covers
= false;
1191 wide_int tem
= tmin
;
1192 vr
.type
= VR_ANTI_RANGE
;
1194 if (wi::cmp (tmin
, tmax
, sgn
) < 0)
1197 if (wi::cmp (tmax
, tem
, sgn
) > 0)
1199 /* If the anti-range would cover nothing, drop to varying.
1200 Likewise if the anti-range bounds are outside of the
1202 if (covers
|| wi::cmp (tmin
, tmax
, sgn
) > 0)
1204 set_value_range_to_varying (&vr
);
1207 vr
.min
= wide_int_to_tree (type
, tmin
);
1208 vr
.max
= wide_int_to_tree (type
, tmax
);
1212 /* Other underflow and/or overflow, drop to VR_VARYING. */
1213 set_value_range_to_varying (&vr
);
1219 /* If overflow does not wrap, saturate to the types min/max
1221 wide_int type_min
= wi::min_value (prec
, sgn
);
1222 wide_int type_max
= wi::max_value (prec
, sgn
);
1223 if (min_ovf
== wi::OVF_UNDERFLOW
)
1224 vr
.min
= wide_int_to_tree (type
, type_min
);
1225 else if (min_ovf
== wi::OVF_OVERFLOW
)
1226 vr
.min
= wide_int_to_tree (type
, type_max
);
1228 vr
.min
= wide_int_to_tree (type
, wmin
);
1230 if (max_ovf
== wi::OVF_UNDERFLOW
)
1231 vr
.max
= wide_int_to_tree (type
, type_min
);
1232 else if (max_ovf
== wi::OVF_OVERFLOW
)
1233 vr
.max
= wide_int_to_tree (type
, type_max
);
1235 vr
.max
= wide_int_to_tree (type
, wmax
);
1239 /* Extract range information from a binary operation CODE based on
1240 the ranges of each of its operands *VR0 and *VR1 with resulting
1241 type EXPR_TYPE. The resulting range is stored in *VR. */
1244 extract_range_from_binary_expr_1 (value_range
*vr
,
1245 enum tree_code code
, tree expr_type
,
1246 const value_range
*vr0_
,
1247 const value_range
*vr1_
)
1249 signop sign
= TYPE_SIGN (expr_type
);
1250 unsigned int prec
= TYPE_PRECISION (expr_type
);
1251 value_range vr0
= *vr0_
, vr1
= *vr1_
;
1252 value_range vrtem0
= VR_INITIALIZER
, vrtem1
= VR_INITIALIZER
;
1253 enum value_range_type type
;
1254 tree min
= NULL_TREE
, max
= NULL_TREE
;
1257 if (!INTEGRAL_TYPE_P (expr_type
)
1258 && !POINTER_TYPE_P (expr_type
))
1260 set_value_range_to_varying (vr
);
1264 /* Not all binary expressions can be applied to ranges in a
1265 meaningful way. Handle only arithmetic operations. */
1266 if (code
!= PLUS_EXPR
1267 && code
!= MINUS_EXPR
1268 && code
!= POINTER_PLUS_EXPR
1269 && code
!= MULT_EXPR
1270 && code
!= TRUNC_DIV_EXPR
1271 && code
!= FLOOR_DIV_EXPR
1272 && code
!= CEIL_DIV_EXPR
1273 && code
!= EXACT_DIV_EXPR
1274 && code
!= ROUND_DIV_EXPR
1275 && code
!= TRUNC_MOD_EXPR
1276 && code
!= RSHIFT_EXPR
1277 && code
!= LSHIFT_EXPR
1280 && code
!= BIT_AND_EXPR
1281 && code
!= BIT_IOR_EXPR
1282 && code
!= BIT_XOR_EXPR
)
1284 set_value_range_to_varying (vr
);
1288 /* If both ranges are UNDEFINED, so is the result. */
1289 if (vr0
.type
== VR_UNDEFINED
&& vr1
.type
== VR_UNDEFINED
)
1291 set_value_range_to_undefined (vr
);
1294 /* If one of the ranges is UNDEFINED drop it to VARYING for the following
1295 code. At some point we may want to special-case operations that
1296 have UNDEFINED result for all or some value-ranges of the not UNDEFINED
1298 else if (vr0
.type
== VR_UNDEFINED
)
1299 set_value_range_to_varying (&vr0
);
1300 else if (vr1
.type
== VR_UNDEFINED
)
1301 set_value_range_to_varying (&vr1
);
1303 /* We get imprecise results from ranges_from_anti_range when
1304 code is EXACT_DIV_EXPR. We could mask out bits in the resulting
1305 range, but then we also need to hack up vrp_meet. It's just
1306 easier to special case when vr0 is ~[0,0] for EXACT_DIV_EXPR. */
1307 if (code
== EXACT_DIV_EXPR
1308 && vr0
.type
== VR_ANTI_RANGE
1309 && vr0
.min
== vr0
.max
1310 && integer_zerop (vr0
.min
))
1312 set_value_range_to_nonnull (vr
, expr_type
);
1316 /* Now canonicalize anti-ranges to ranges when they are not symbolic
1317 and express ~[] op X as ([]' op X) U ([]'' op X). */
1318 if (vr0
.type
== VR_ANTI_RANGE
1319 && ranges_from_anti_range (&vr0
, &vrtem0
, &vrtem1
))
1321 extract_range_from_binary_expr_1 (vr
, code
, expr_type
, &vrtem0
, vr1_
);
1322 if (vrtem1
.type
!= VR_UNDEFINED
)
1324 value_range vrres
= VR_INITIALIZER
;
1325 extract_range_from_binary_expr_1 (&vrres
, code
, expr_type
,
1327 vrp_meet (vr
, &vrres
);
1331 /* Likewise for X op ~[]. */
1332 if (vr1
.type
== VR_ANTI_RANGE
1333 && ranges_from_anti_range (&vr1
, &vrtem0
, &vrtem1
))
1335 extract_range_from_binary_expr_1 (vr
, code
, expr_type
, vr0_
, &vrtem0
);
1336 if (vrtem1
.type
!= VR_UNDEFINED
)
1338 value_range vrres
= VR_INITIALIZER
;
1339 extract_range_from_binary_expr_1 (&vrres
, code
, expr_type
,
1341 vrp_meet (vr
, &vrres
);
1346 /* The type of the resulting value range defaults to VR0.TYPE. */
1349 /* Refuse to operate on VARYING ranges, ranges of different kinds
1350 and symbolic ranges. As an exception, we allow BIT_{AND,IOR}
1351 because we may be able to derive a useful range even if one of
1352 the operands is VR_VARYING or symbolic range. Similarly for
1353 divisions, MIN/MAX and PLUS/MINUS.
1355 TODO, we may be able to derive anti-ranges in some cases. */
1356 if (code
!= BIT_AND_EXPR
1357 && code
!= BIT_IOR_EXPR
1358 && code
!= TRUNC_DIV_EXPR
1359 && code
!= FLOOR_DIV_EXPR
1360 && code
!= CEIL_DIV_EXPR
1361 && code
!= EXACT_DIV_EXPR
1362 && code
!= ROUND_DIV_EXPR
1363 && code
!= TRUNC_MOD_EXPR
1366 && code
!= PLUS_EXPR
1367 && code
!= MINUS_EXPR
1368 && code
!= RSHIFT_EXPR
1369 && code
!= POINTER_PLUS_EXPR
1370 && (vr0
.type
== VR_VARYING
1371 || vr1
.type
== VR_VARYING
1372 || vr0
.type
!= vr1
.type
1373 || symbolic_range_p (&vr0
)
1374 || symbolic_range_p (&vr1
)))
1376 set_value_range_to_varying (vr
);
1380 /* Now evaluate the expression to determine the new range. */
1381 if (POINTER_TYPE_P (expr_type
))
1383 if (code
== MIN_EXPR
|| code
== MAX_EXPR
)
1385 /* For MIN/MAX expressions with pointers, we only care about
1386 nullness, if both are non null, then the result is nonnull.
1387 If both are null, then the result is null. Otherwise they
1389 if (!range_includes_zero_p (&vr0
) && !range_includes_zero_p (&vr1
))
1390 set_value_range_to_nonnull (vr
, expr_type
);
1391 else if (range_is_null (&vr0
) && range_is_null (&vr1
))
1392 set_value_range_to_null (vr
, expr_type
);
1394 set_value_range_to_varying (vr
);
1396 else if (code
== POINTER_PLUS_EXPR
)
1398 /* For pointer types, we are really only interested in asserting
1399 whether the expression evaluates to non-NULL. */
1400 if (!range_includes_zero_p (&vr0
)
1401 || !range_includes_zero_p (&vr1
))
1402 set_value_range_to_nonnull (vr
, expr_type
);
1403 else if (range_is_null (&vr0
) && range_is_null (&vr1
))
1404 set_value_range_to_null (vr
, expr_type
);
1406 set_value_range_to_varying (vr
);
1408 else if (code
== BIT_AND_EXPR
)
1410 /* For pointer types, we are really only interested in asserting
1411 whether the expression evaluates to non-NULL. */
1412 if (!range_includes_zero_p (&vr0
) && !range_includes_zero_p (&vr1
))
1413 set_value_range_to_nonnull (vr
, expr_type
);
1414 else if (range_is_null (&vr0
) || range_is_null (&vr1
))
1415 set_value_range_to_null (vr
, expr_type
);
1417 set_value_range_to_varying (vr
);
1420 set_value_range_to_varying (vr
);
1425 /* For integer ranges, apply the operation to each end of the
1426 range and see what we end up with. */
1427 if (code
== PLUS_EXPR
|| code
== MINUS_EXPR
)
1429 const bool minus_p
= (code
== MINUS_EXPR
);
1430 tree min_op0
= vr0
.min
;
1431 tree min_op1
= minus_p
? vr1
.max
: vr1
.min
;
1432 tree max_op0
= vr0
.max
;
1433 tree max_op1
= minus_p
? vr1
.min
: vr1
.max
;
1434 tree sym_min_op0
= NULL_TREE
;
1435 tree sym_min_op1
= NULL_TREE
;
1436 tree sym_max_op0
= NULL_TREE
;
1437 tree sym_max_op1
= NULL_TREE
;
1438 bool neg_min_op0
, neg_min_op1
, neg_max_op0
, neg_max_op1
;
1440 neg_min_op0
= neg_min_op1
= neg_max_op0
= neg_max_op1
= false;
1442 /* If we have a PLUS or MINUS with two VR_RANGEs, either constant or
1443 single-symbolic ranges, try to compute the precise resulting range,
1444 but only if we know that this resulting range will also be constant
1445 or single-symbolic. */
1446 if (vr0
.type
== VR_RANGE
&& vr1
.type
== VR_RANGE
1447 && (TREE_CODE (min_op0
) == INTEGER_CST
1449 = get_single_symbol (min_op0
, &neg_min_op0
, &min_op0
)))
1450 && (TREE_CODE (min_op1
) == INTEGER_CST
1452 = get_single_symbol (min_op1
, &neg_min_op1
, &min_op1
)))
1453 && (!(sym_min_op0
&& sym_min_op1
)
1454 || (sym_min_op0
== sym_min_op1
1455 && neg_min_op0
== (minus_p
? neg_min_op1
: !neg_min_op1
)))
1456 && (TREE_CODE (max_op0
) == INTEGER_CST
1458 = get_single_symbol (max_op0
, &neg_max_op0
, &max_op0
)))
1459 && (TREE_CODE (max_op1
) == INTEGER_CST
1461 = get_single_symbol (max_op1
, &neg_max_op1
, &max_op1
)))
1462 && (!(sym_max_op0
&& sym_max_op1
)
1463 || (sym_max_op0
== sym_max_op1
1464 && neg_max_op0
== (minus_p
? neg_max_op1
: !neg_max_op1
))))
1466 wide_int wmin
, wmax
;
1467 wi::overflow_type min_ovf
= wi::OVF_NONE
;
1468 wi::overflow_type max_ovf
= wi::OVF_NONE
;
1470 /* Build the bounds. */
1471 combine_bound (code
, wmin
, min_ovf
, expr_type
, min_op0
, min_op1
);
1472 combine_bound (code
, wmax
, max_ovf
, expr_type
, max_op0
, max_op1
);
1474 /* If we have overflow for the constant part and the resulting
1475 range will be symbolic, drop to VR_VARYING. */
1476 if (((bool)min_ovf
&& sym_min_op0
!= sym_min_op1
)
1477 || ((bool)max_ovf
&& sym_max_op0
!= sym_max_op1
))
1479 set_value_range_to_varying (vr
);
1483 /* Adjust the range for possible overflow. */
1484 set_value_range_with_overflow (*vr
, expr_type
,
1485 wmin
, wmax
, min_ovf
, max_ovf
);
1486 if (vr
->type
== VR_VARYING
)
1489 /* Build the symbolic bounds if needed. */
1490 adjust_symbolic_bound (vr
->min
, code
, expr_type
,
1491 sym_min_op0
, sym_min_op1
,
1492 neg_min_op0
, neg_min_op1
);
1493 adjust_symbolic_bound (vr
->max
, code
, expr_type
,
1494 sym_max_op0
, sym_max_op1
,
1495 neg_max_op0
, neg_max_op1
);
1496 /* ?? It would probably be cleaner to eliminate min/max/type
1497 entirely and hold these values in VR directly. */
1504 /* For other cases, for example if we have a PLUS_EXPR with two
1505 VR_ANTI_RANGEs, drop to VR_VARYING. It would take more effort
1506 to compute a precise range for such a case.
1507 ??? General even mixed range kind operations can be expressed
1508 by for example transforming ~[3, 5] + [1, 2] to range-only
1509 operations and a union primitive:
1510 [-INF, 2] + [1, 2] U [5, +INF] + [1, 2]
1511 [-INF+1, 4] U [6, +INF(OVF)]
1512 though usually the union is not exactly representable with
1513 a single range or anti-range as the above is
1514 [-INF+1, +INF(OVF)] intersected with ~[5, 5]
1515 but one could use a scheme similar to equivalences for this. */
1516 set_value_range_to_varying (vr
);
1520 else if (code
== MIN_EXPR
1521 || code
== MAX_EXPR
)
1523 wide_int wmin
, wmax
;
1524 wide_int vr0_min
, vr0_max
;
1525 wide_int vr1_min
, vr1_max
;
1526 extract_range_into_wide_ints (&vr0
, sign
, prec
, vr0_min
, vr0_max
);
1527 extract_range_into_wide_ints (&vr1
, sign
, prec
, vr1_min
, vr1_max
);
1528 if (wide_int_range_min_max (wmin
, wmax
, code
, sign
, prec
,
1529 vr0_min
, vr0_max
, vr1_min
, vr1_max
))
1530 set_value_range (vr
, VR_RANGE
,
1531 wide_int_to_tree (expr_type
, wmin
),
1532 wide_int_to_tree (expr_type
, wmax
), NULL
);
1534 set_value_range_to_varying (vr
);
1537 else if (code
== MULT_EXPR
)
1539 if (!range_int_cst_p (&vr0
)
1540 || !range_int_cst_p (&vr1
))
1542 set_value_range_to_varying (vr
);
1545 extract_range_from_multiplicative_op (vr
, code
, &vr0
, &vr1
);
1548 else if (code
== RSHIFT_EXPR
1549 || code
== LSHIFT_EXPR
)
1551 if (range_int_cst_p (&vr1
)
1552 && !vrp_shift_undefined_p (vr1
, prec
))
1554 if (code
== RSHIFT_EXPR
)
1556 /* Even if vr0 is VARYING or otherwise not usable, we can derive
1557 useful ranges just from the shift count. E.g.
1558 x >> 63 for signed 64-bit x is always [-1, 0]. */
1559 if (vr0
.type
!= VR_RANGE
|| symbolic_range_p (&vr0
))
1561 vr0
.type
= type
= VR_RANGE
;
1562 vr0
.min
= vrp_val_min (expr_type
);
1563 vr0
.max
= vrp_val_max (expr_type
);
1565 extract_range_from_multiplicative_op (vr
, code
, &vr0
, &vr1
);
1568 else if (code
== LSHIFT_EXPR
1569 && range_int_cst_p (&vr0
))
1571 wide_int res_lb
, res_ub
;
1572 if (wide_int_range_lshift (res_lb
, res_ub
, sign
, prec
,
1573 wi::to_wide (vr0
.min
),
1574 wi::to_wide (vr0
.max
),
1575 wi::to_wide (vr1
.min
),
1576 wi::to_wide (vr1
.max
),
1577 TYPE_OVERFLOW_UNDEFINED (expr_type
),
1578 TYPE_OVERFLOW_WRAPS (expr_type
)))
1580 min
= wide_int_to_tree (expr_type
, res_lb
);
1581 max
= wide_int_to_tree (expr_type
, res_ub
);
1582 set_and_canonicalize_value_range (vr
, VR_RANGE
,
1588 set_value_range_to_varying (vr
);
1591 else if (code
== TRUNC_DIV_EXPR
1592 || code
== FLOOR_DIV_EXPR
1593 || code
== CEIL_DIV_EXPR
1594 || code
== EXACT_DIV_EXPR
1595 || code
== ROUND_DIV_EXPR
)
1597 wide_int dividend_min
, dividend_max
, divisor_min
, divisor_max
;
1598 wide_int wmin
, wmax
, extra_min
, extra_max
;
1601 /* Special case explicit division by zero as undefined. */
1602 if (range_is_null (&vr1
))
1604 /* However, we must not eliminate a division by zero if
1605 flag_non_call_exceptions. */
1606 if (cfun
->can_throw_non_call_exceptions
)
1607 set_value_range_to_varying (vr
);
1609 set_value_range_to_undefined (vr
);
1613 /* First, normalize ranges into constants we can handle. Note
1614 that VR_ANTI_RANGE's of constants were already normalized
1615 before arriving here.
1617 NOTE: As a future improvement, we may be able to do better
1618 with mixed symbolic (anti-)ranges like [0, A]. See note in
1619 ranges_from_anti_range. */
1620 extract_range_into_wide_ints (&vr0
, sign
, prec
,
1621 dividend_min
, dividend_max
);
1622 extract_range_into_wide_ints (&vr1
, sign
, prec
,
1623 divisor_min
, divisor_max
);
1624 if (!wide_int_range_div (wmin
, wmax
, code
, sign
, prec
,
1625 dividend_min
, dividend_max
,
1626 divisor_min
, divisor_max
,
1627 TYPE_OVERFLOW_UNDEFINED (expr_type
),
1628 TYPE_OVERFLOW_WRAPS (expr_type
),
1629 extra_range_p
, extra_min
, extra_max
))
1631 set_value_range_to_varying (vr
);
1634 set_value_range (vr
, VR_RANGE
,
1635 wide_int_to_tree (expr_type
, wmin
),
1636 wide_int_to_tree (expr_type
, wmax
), NULL
);
1639 value_range extra_range
= VR_INITIALIZER
;
1640 set_value_range (&extra_range
, VR_RANGE
,
1641 wide_int_to_tree (expr_type
, extra_min
),
1642 wide_int_to_tree (expr_type
, extra_max
), NULL
);
1643 vrp_meet (vr
, &extra_range
);
1647 else if (code
== TRUNC_MOD_EXPR
)
1649 if (range_is_null (&vr1
))
1651 set_value_range_to_undefined (vr
);
1654 wide_int wmin
, wmax
, tmp
;
1655 wide_int vr0_min
, vr0_max
, vr1_min
, vr1_max
;
1656 extract_range_into_wide_ints (&vr0
, sign
, prec
, vr0_min
, vr0_max
);
1657 extract_range_into_wide_ints (&vr1
, sign
, prec
, vr1_min
, vr1_max
);
1658 wide_int_range_trunc_mod (wmin
, wmax
, sign
, prec
,
1659 vr0_min
, vr0_max
, vr1_min
, vr1_max
);
1660 min
= wide_int_to_tree (expr_type
, wmin
);
1661 max
= wide_int_to_tree (expr_type
, wmax
);
1662 set_value_range (vr
, VR_RANGE
, min
, max
, NULL
);
1665 else if (code
== BIT_AND_EXPR
|| code
== BIT_IOR_EXPR
|| code
== BIT_XOR_EXPR
)
1667 wide_int may_be_nonzero0
, may_be_nonzero1
;
1668 wide_int must_be_nonzero0
, must_be_nonzero1
;
1669 wide_int wmin
, wmax
;
1670 wide_int vr0_min
, vr0_max
, vr1_min
, vr1_max
;
1671 vrp_set_zero_nonzero_bits (expr_type
, &vr0
,
1672 &may_be_nonzero0
, &must_be_nonzero0
);
1673 vrp_set_zero_nonzero_bits (expr_type
, &vr1
,
1674 &may_be_nonzero1
, &must_be_nonzero1
);
1675 extract_range_into_wide_ints (&vr0
, sign
, prec
, vr0_min
, vr0_max
);
1676 extract_range_into_wide_ints (&vr1
, sign
, prec
, vr1_min
, vr1_max
);
1677 if (code
== BIT_AND_EXPR
)
1679 if (wide_int_range_bit_and (wmin
, wmax
, sign
, prec
,
1687 min
= wide_int_to_tree (expr_type
, wmin
);
1688 max
= wide_int_to_tree (expr_type
, wmax
);
1689 set_value_range (vr
, VR_RANGE
, min
, max
, NULL
);
1692 set_value_range_to_varying (vr
);
1695 else if (code
== BIT_IOR_EXPR
)
1697 if (wide_int_range_bit_ior (wmin
, wmax
, sign
,
1705 min
= wide_int_to_tree (expr_type
, wmin
);
1706 max
= wide_int_to_tree (expr_type
, wmax
);
1707 set_value_range (vr
, VR_RANGE
, min
, max
, NULL
);
1710 set_value_range_to_varying (vr
);
1713 else if (code
== BIT_XOR_EXPR
)
1715 if (wide_int_range_bit_xor (wmin
, wmax
, sign
, prec
,
1721 min
= wide_int_to_tree (expr_type
, wmin
);
1722 max
= wide_int_to_tree (expr_type
, wmax
);
1723 set_value_range (vr
, VR_RANGE
, min
, max
, NULL
);
1726 set_value_range_to_varying (vr
);
1733 /* If either MIN or MAX overflowed, then set the resulting range to
1735 if (min
== NULL_TREE
1736 || TREE_OVERFLOW_P (min
)
1738 || TREE_OVERFLOW_P (max
))
1740 set_value_range_to_varying (vr
);
1744 /* We punt for [-INF, +INF].
1745 We learn nothing when we have INF on both sides.
1746 Note that we do accept [-INF, -INF] and [+INF, +INF]. */
1747 if (vrp_val_is_min (min
) && vrp_val_is_max (max
))
1749 set_value_range_to_varying (vr
);
1753 cmp
= compare_values (min
, max
);
1754 if (cmp
== -2 || cmp
== 1)
1756 /* If the new range has its limits swapped around (MIN > MAX),
1757 then the operation caused one of them to wrap around, mark
1758 the new range VARYING. */
1759 set_value_range_to_varying (vr
);
1762 set_value_range (vr
, type
, min
, max
, NULL
);
1765 /* Extract range information from a unary operation CODE based on
1766 the range of its operand *VR0 with type OP0_TYPE with resulting type TYPE.
1767 The resulting range is stored in *VR. */
1770 extract_range_from_unary_expr (value_range
*vr
,
1771 enum tree_code code
, tree type
,
1772 const value_range
*vr0_
, tree op0_type
)
1774 signop sign
= TYPE_SIGN (type
);
1775 unsigned int prec
= TYPE_PRECISION (type
);
1776 value_range vr0
= *vr0_
, vrtem0
= VR_INITIALIZER
, vrtem1
= VR_INITIALIZER
;
1778 /* VRP only operates on integral and pointer types. */
1779 if (!(INTEGRAL_TYPE_P (op0_type
)
1780 || POINTER_TYPE_P (op0_type
))
1781 || !(INTEGRAL_TYPE_P (type
)
1782 || POINTER_TYPE_P (type
)))
1784 set_value_range_to_varying (vr
);
1788 /* If VR0 is UNDEFINED, so is the result. */
1789 if (vr0
.type
== VR_UNDEFINED
)
1791 set_value_range_to_undefined (vr
);
1795 /* Handle operations that we express in terms of others. */
1796 if (code
== PAREN_EXPR
|| code
== OBJ_TYPE_REF
)
1798 /* PAREN_EXPR and OBJ_TYPE_REF are simple copies. */
1799 copy_value_range (vr
, &vr0
);
1802 else if (code
== NEGATE_EXPR
)
1804 /* -X is simply 0 - X, so re-use existing code that also handles
1805 anti-ranges fine. */
1806 value_range zero
= VR_INITIALIZER
;
1807 set_value_range_to_value (&zero
, build_int_cst (type
, 0), NULL
);
1808 extract_range_from_binary_expr_1 (vr
, MINUS_EXPR
, type
, &zero
, &vr0
);
1811 else if (code
== BIT_NOT_EXPR
)
1813 /* ~X is simply -1 - X, so re-use existing code that also handles
1814 anti-ranges fine. */
1815 value_range minusone
= VR_INITIALIZER
;
1816 set_value_range_to_value (&minusone
, build_int_cst (type
, -1), NULL
);
1817 extract_range_from_binary_expr_1 (vr
, MINUS_EXPR
,
1818 type
, &minusone
, &vr0
);
1822 /* Now canonicalize anti-ranges to ranges when they are not symbolic
1823 and express op ~[] as (op []') U (op []''). */
1824 if (vr0
.type
== VR_ANTI_RANGE
1825 && ranges_from_anti_range (&vr0
, &vrtem0
, &vrtem1
))
1827 extract_range_from_unary_expr (vr
, code
, type
, &vrtem0
, op0_type
);
1828 if (vrtem1
.type
!= VR_UNDEFINED
)
1830 value_range vrres
= VR_INITIALIZER
;
1831 extract_range_from_unary_expr (&vrres
, code
, type
,
1833 vrp_meet (vr
, &vrres
);
1838 if (CONVERT_EXPR_CODE_P (code
))
1840 tree inner_type
= op0_type
;
1841 tree outer_type
= type
;
1843 /* If the expression evaluates to a pointer, we are only interested in
1844 determining if it evaluates to NULL [0, 0] or non-NULL (~[0, 0]). */
1845 if (POINTER_TYPE_P (type
))
1847 if (!range_includes_zero_p (&vr0
))
1848 set_value_range_to_nonnull (vr
, type
);
1849 else if (range_is_null (&vr0
))
1850 set_value_range_to_null (vr
, type
);
1852 set_value_range_to_varying (vr
);
1856 /* We normalize everything to a VR_RANGE, but for constant
1857 anti-ranges we must handle them by leaving the final result
1858 as an anti range. This allows us to convert things like
1859 ~[0,5] seamlessly. */
1860 value_range_type vr_type
= VR_RANGE
;
1861 if (vr0
.type
== VR_ANTI_RANGE
1862 && TREE_CODE (vr0
.min
) == INTEGER_CST
1863 && TREE_CODE (vr0
.max
) == INTEGER_CST
)
1864 vr_type
= VR_ANTI_RANGE
;
1866 /* NOTES: Previously we were returning VARYING for all symbolics, but
1867 we can do better by treating them as [-MIN, +MAX]. For
1868 example, converting [SYM, SYM] from INT to LONG UNSIGNED,
1869 we can return: ~[0x8000000, 0xffffffff7fffffff].
1871 We were also failing to convert ~[0,0] from char* to unsigned,
1872 instead choosing to return VR_VARYING. Now we return ~[0,0]. */
1873 wide_int vr0_min
, vr0_max
, wmin
, wmax
;
1874 signop inner_sign
= TYPE_SIGN (inner_type
);
1875 signop outer_sign
= TYPE_SIGN (outer_type
);
1876 unsigned inner_prec
= TYPE_PRECISION (inner_type
);
1877 unsigned outer_prec
= TYPE_PRECISION (outer_type
);
1878 extract_range_into_wide_ints (&vr0
, inner_sign
, inner_prec
,
1880 if (wide_int_range_convert (wmin
, wmax
,
1881 inner_sign
, inner_prec
,
1882 outer_sign
, outer_prec
,
1885 tree min
= wide_int_to_tree (outer_type
, wmin
);
1886 tree max
= wide_int_to_tree (outer_type
, wmax
);
1887 set_and_canonicalize_value_range (vr
, vr_type
, min
, max
, NULL
);
1890 set_value_range_to_varying (vr
);
1893 else if (code
== ABS_EXPR
)
1895 if (vr0
.type
!= VR_RANGE
|| symbolic_range_p (&vr0
))
1897 set_value_range_to_varying (vr
);
1900 wide_int wmin
, wmax
;
1901 wide_int vr0_min
, vr0_max
;
1902 extract_range_into_wide_ints (&vr0
, sign
, prec
, vr0_min
, vr0_max
);
1903 if (wide_int_range_abs (wmin
, wmax
, sign
, prec
, vr0_min
, vr0_max
,
1904 TYPE_OVERFLOW_UNDEFINED (type
)))
1905 set_value_range (vr
, VR_RANGE
,
1906 wide_int_to_tree (type
, wmin
),
1907 wide_int_to_tree (type
, wmax
), NULL
);
1909 set_value_range_to_varying (vr
);
1913 /* For unhandled operations fall back to varying. */
1914 set_value_range_to_varying (vr
);
1918 /* Debugging dumps. */
1920 void dump_value_range (FILE *, const value_range
*);
1921 void debug_value_range (const value_range
*);
1922 void dump_all_value_ranges (FILE *);
1923 void dump_vr_equiv (FILE *, bitmap
);
1924 void debug_vr_equiv (bitmap
);
1927 /* Dump value range VR to FILE. */
1930 dump_value_range (FILE *file
, const value_range
*vr
)
1933 fprintf (file
, "[]");
1934 else if (vr
->type
== VR_UNDEFINED
)
1935 fprintf (file
, "UNDEFINED");
1936 else if (vr
->type
== VR_RANGE
|| vr
->type
== VR_ANTI_RANGE
)
1938 tree type
= TREE_TYPE (vr
->min
);
1940 fprintf (file
, "%s[", (vr
->type
== VR_ANTI_RANGE
) ? "~" : "");
1942 if (INTEGRAL_TYPE_P (type
)
1943 && !TYPE_UNSIGNED (type
)
1944 && vrp_val_is_min (vr
->min
))
1945 fprintf (file
, "-INF");
1947 print_generic_expr (file
, vr
->min
);
1949 fprintf (file
, ", ");
1951 if (INTEGRAL_TYPE_P (type
)
1952 && vrp_val_is_max (vr
->max
))
1953 fprintf (file
, "+INF");
1955 print_generic_expr (file
, vr
->max
);
1957 fprintf (file
, "]");
1964 fprintf (file
, " EQUIVALENCES: { ");
1966 EXECUTE_IF_SET_IN_BITMAP (vr
->equiv
, 0, i
, bi
)
1968 print_generic_expr (file
, ssa_name (i
));
1969 fprintf (file
, " ");
1973 fprintf (file
, "} (%u elements)", c
);
1976 else if (vr
->type
== VR_VARYING
)
1977 fprintf (file
, "VARYING");
1979 fprintf (file
, "INVALID RANGE");
1983 /* Dump value range VR to stderr. */
1986 debug_value_range (const value_range
*vr
)
1988 dump_value_range (stderr
, vr
);
1989 fprintf (stderr
, "\n");
1993 value_range::dump () const
1995 debug_value_range (this);
1999 /* Given a COND_EXPR COND of the form 'V OP W', and an SSA name V,
2000 create a new SSA name N and return the assertion assignment
2001 'N = ASSERT_EXPR <V, V OP W>'. */
2004 build_assert_expr_for (tree cond
, tree v
)
2009 gcc_assert (TREE_CODE (v
) == SSA_NAME
2010 && COMPARISON_CLASS_P (cond
));
2012 a
= build2 (ASSERT_EXPR
, TREE_TYPE (v
), v
, cond
);
2013 assertion
= gimple_build_assign (NULL_TREE
, a
);
2015 /* The new ASSERT_EXPR, creates a new SSA name that replaces the
2016 operand of the ASSERT_EXPR. Create it so the new name and the old one
2017 are registered in the replacement table so that we can fix the SSA web
2018 after adding all the ASSERT_EXPRs. */
2019 tree new_def
= create_new_def_for (v
, assertion
, NULL
);
2020 /* Make sure we preserve abnormalness throughout an ASSERT_EXPR chain
2021 given we have to be able to fully propagate those out to re-create
2022 valid SSA when removing the asserts. */
2023 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (v
))
2024 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (new_def
) = 1;
2030 /* Return false if EXPR is a predicate expression involving floating
2034 fp_predicate (gimple
*stmt
)
2036 GIMPLE_CHECK (stmt
, GIMPLE_COND
);
2038 return FLOAT_TYPE_P (TREE_TYPE (gimple_cond_lhs (stmt
)));
2041 /* If the range of values taken by OP can be inferred after STMT executes,
2042 return the comparison code (COMP_CODE_P) and value (VAL_P) that
2043 describes the inferred range. Return true if a range could be
2047 infer_value_range (gimple
*stmt
, tree op
, tree_code
*comp_code_p
, tree
*val_p
)
2050 *comp_code_p
= ERROR_MARK
;
2052 /* Do not attempt to infer anything in names that flow through
2054 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op
))
2057 /* If STMT is the last statement of a basic block with no normal
2058 successors, there is no point inferring anything about any of its
2059 operands. We would not be able to find a proper insertion point
2060 for the assertion, anyway. */
2061 if (stmt_ends_bb_p (stmt
))
2066 FOR_EACH_EDGE (e
, ei
, gimple_bb (stmt
)->succs
)
2067 if (!(e
->flags
& (EDGE_ABNORMAL
|EDGE_EH
)))
2073 if (infer_nonnull_range (stmt
, op
))
2075 *val_p
= build_int_cst (TREE_TYPE (op
), 0);
2076 *comp_code_p
= NE_EXPR
;
2084 void dump_asserts_for (FILE *, tree
);
2085 void debug_asserts_for (tree
);
2086 void dump_all_asserts (FILE *);
2087 void debug_all_asserts (void);
2089 /* Dump all the registered assertions for NAME to FILE. */
2092 dump_asserts_for (FILE *file
, tree name
)
2096 fprintf (file
, "Assertions to be inserted for ");
2097 print_generic_expr (file
, name
);
2098 fprintf (file
, "\n");
2100 loc
= asserts_for
[SSA_NAME_VERSION (name
)];
2103 fprintf (file
, "\t");
2104 print_gimple_stmt (file
, gsi_stmt (loc
->si
), 0);
2105 fprintf (file
, "\n\tBB #%d", loc
->bb
->index
);
2108 fprintf (file
, "\n\tEDGE %d->%d", loc
->e
->src
->index
,
2109 loc
->e
->dest
->index
);
2110 dump_edge_info (file
, loc
->e
, dump_flags
, 0);
2112 fprintf (file
, "\n\tPREDICATE: ");
2113 print_generic_expr (file
, loc
->expr
);
2114 fprintf (file
, " %s ", get_tree_code_name (loc
->comp_code
));
2115 print_generic_expr (file
, loc
->val
);
2116 fprintf (file
, "\n\n");
2120 fprintf (file
, "\n");
2124 /* Dump all the registered assertions for NAME to stderr. */
2127 debug_asserts_for (tree name
)
2129 dump_asserts_for (stderr
, name
);
2133 /* Dump all the registered assertions for all the names to FILE. */
2136 dump_all_asserts (FILE *file
)
2141 fprintf (file
, "\nASSERT_EXPRs to be inserted\n\n");
2142 EXECUTE_IF_SET_IN_BITMAP (need_assert_for
, 0, i
, bi
)
2143 dump_asserts_for (file
, ssa_name (i
));
2144 fprintf (file
, "\n");
2148 /* Dump all the registered assertions for all the names to stderr. */
2151 debug_all_asserts (void)
2153 dump_all_asserts (stderr
);
2156 /* Push the assert info for NAME, EXPR, COMP_CODE and VAL to ASSERTS. */
2159 add_assert_info (vec
<assert_info
> &asserts
,
2160 tree name
, tree expr
, enum tree_code comp_code
, tree val
)
2163 info
.comp_code
= comp_code
;
2165 if (TREE_OVERFLOW_P (val
))
2166 val
= drop_tree_overflow (val
);
2169 asserts
.safe_push (info
);
2172 /* If NAME doesn't have an ASSERT_EXPR registered for asserting
2173 'EXPR COMP_CODE VAL' at a location that dominates block BB or
2174 E->DEST, then register this location as a possible insertion point
2175 for ASSERT_EXPR <NAME, EXPR COMP_CODE VAL>.
2177 BB, E and SI provide the exact insertion point for the new
2178 ASSERT_EXPR. If BB is NULL, then the ASSERT_EXPR is to be inserted
2179 on edge E. Otherwise, if E is NULL, the ASSERT_EXPR is inserted on
2180 BB. If SI points to a COND_EXPR or a SWITCH_EXPR statement, then E
2181 must not be NULL. */
2184 register_new_assert_for (tree name
, tree expr
,
2185 enum tree_code comp_code
,
2189 gimple_stmt_iterator si
)
2191 assert_locus
*n
, *loc
, *last_loc
;
2192 basic_block dest_bb
;
2194 gcc_checking_assert (bb
== NULL
|| e
== NULL
);
2197 gcc_checking_assert (gimple_code (gsi_stmt (si
)) != GIMPLE_COND
2198 && gimple_code (gsi_stmt (si
)) != GIMPLE_SWITCH
);
2200 /* Never build an assert comparing against an integer constant with
2201 TREE_OVERFLOW set. This confuses our undefined overflow warning
2203 if (TREE_OVERFLOW_P (val
))
2204 val
= drop_tree_overflow (val
);
2206 /* The new assertion A will be inserted at BB or E. We need to
2207 determine if the new location is dominated by a previously
2208 registered location for A. If we are doing an edge insertion,
2209 assume that A will be inserted at E->DEST. Note that this is not
2212 If E is a critical edge, it will be split. But even if E is
2213 split, the new block will dominate the same set of blocks that
2216 The reverse, however, is not true, blocks dominated by E->DEST
2217 will not be dominated by the new block created to split E. So,
2218 if the insertion location is on a critical edge, we will not use
2219 the new location to move another assertion previously registered
2220 at a block dominated by E->DEST. */
2221 dest_bb
= (bb
) ? bb
: e
->dest
;
2223 /* If NAME already has an ASSERT_EXPR registered for COMP_CODE and
2224 VAL at a block dominating DEST_BB, then we don't need to insert a new
2225 one. Similarly, if the same assertion already exists at a block
2226 dominated by DEST_BB and the new location is not on a critical
2227 edge, then update the existing location for the assertion (i.e.,
2228 move the assertion up in the dominance tree).
2230 Note, this is implemented as a simple linked list because there
2231 should not be more than a handful of assertions registered per
2232 name. If this becomes a performance problem, a table hashed by
2233 COMP_CODE and VAL could be implemented. */
2234 loc
= asserts_for
[SSA_NAME_VERSION (name
)];
2238 if (loc
->comp_code
== comp_code
2240 || operand_equal_p (loc
->val
, val
, 0))
2241 && (loc
->expr
== expr
2242 || operand_equal_p (loc
->expr
, expr
, 0)))
2244 /* If E is not a critical edge and DEST_BB
2245 dominates the existing location for the assertion, move
2246 the assertion up in the dominance tree by updating its
2247 location information. */
2248 if ((e
== NULL
|| !EDGE_CRITICAL_P (e
))
2249 && dominated_by_p (CDI_DOMINATORS
, loc
->bb
, dest_bb
))
2258 /* Update the last node of the list and move to the next one. */
2263 /* If we didn't find an assertion already registered for
2264 NAME COMP_CODE VAL, add a new one at the end of the list of
2265 assertions associated with NAME. */
2266 n
= XNEW (struct assert_locus
);
2270 n
->comp_code
= comp_code
;
2278 asserts_for
[SSA_NAME_VERSION (name
)] = n
;
2280 bitmap_set_bit (need_assert_for
, SSA_NAME_VERSION (name
));
2283 /* (COND_OP0 COND_CODE COND_OP1) is a predicate which uses NAME.
2284 Extract a suitable test code and value and store them into *CODE_P and
2285 *VAL_P so the predicate is normalized to NAME *CODE_P *VAL_P.
2287 If no extraction was possible, return FALSE, otherwise return TRUE.
2289 If INVERT is true, then we invert the result stored into *CODE_P. */
2292 extract_code_and_val_from_cond_with_ops (tree name
, enum tree_code cond_code
,
2293 tree cond_op0
, tree cond_op1
,
2294 bool invert
, enum tree_code
*code_p
,
2297 enum tree_code comp_code
;
2300 /* Otherwise, we have a comparison of the form NAME COMP VAL
2301 or VAL COMP NAME. */
2302 if (name
== cond_op1
)
2304 /* If the predicate is of the form VAL COMP NAME, flip
2305 COMP around because we need to register NAME as the
2306 first operand in the predicate. */
2307 comp_code
= swap_tree_comparison (cond_code
);
2310 else if (name
== cond_op0
)
2312 /* The comparison is of the form NAME COMP VAL, so the
2313 comparison code remains unchanged. */
2314 comp_code
= cond_code
;
2320 /* Invert the comparison code as necessary. */
2322 comp_code
= invert_tree_comparison (comp_code
, 0);
2324 /* VRP only handles integral and pointer types. */
2325 if (! INTEGRAL_TYPE_P (TREE_TYPE (val
))
2326 && ! POINTER_TYPE_P (TREE_TYPE (val
)))
2329 /* Do not register always-false predicates.
2330 FIXME: this works around a limitation in fold() when dealing with
2331 enumerations. Given 'enum { N1, N2 } x;', fold will not
2332 fold 'if (x > N2)' to 'if (0)'. */
2333 if ((comp_code
== GT_EXPR
|| comp_code
== LT_EXPR
)
2334 && INTEGRAL_TYPE_P (TREE_TYPE (val
)))
2336 tree min
= TYPE_MIN_VALUE (TREE_TYPE (val
));
2337 tree max
= TYPE_MAX_VALUE (TREE_TYPE (val
));
2339 if (comp_code
== GT_EXPR
2341 || compare_values (val
, max
) == 0))
2344 if (comp_code
== LT_EXPR
2346 || compare_values (val
, min
) == 0))
2349 *code_p
= comp_code
;
2354 /* Find out smallest RES where RES > VAL && (RES & MASK) == RES, if any
2355 (otherwise return VAL). VAL and MASK must be zero-extended for
2356 precision PREC. If SGNBIT is non-zero, first xor VAL with SGNBIT
2357 (to transform signed values into unsigned) and at the end xor
2361 masked_increment (const wide_int
&val_in
, const wide_int
&mask
,
2362 const wide_int
&sgnbit
, unsigned int prec
)
2364 wide_int bit
= wi::one (prec
), res
;
2367 wide_int val
= val_in
^ sgnbit
;
2368 for (i
= 0; i
< prec
; i
++, bit
+= bit
)
2371 if ((res
& bit
) == 0)
2374 res
= wi::bit_and_not (val
+ bit
, res
);
2376 if (wi::gtu_p (res
, val
))
2377 return res
^ sgnbit
;
2379 return val
^ sgnbit
;
2382 /* Helper for overflow_comparison_p
2384 OP0 CODE OP1 is a comparison. Examine the comparison and potentially
2385 OP1's defining statement to see if it ultimately has the form
2386 OP0 CODE (OP0 PLUS INTEGER_CST)
2388 If so, return TRUE indicating this is an overflow test and store into
2389 *NEW_CST an updated constant that can be used in a narrowed range test.
2391 REVERSED indicates if the comparison was originally:
2395 This affects how we build the updated constant. */
2398 overflow_comparison_p_1 (enum tree_code code
, tree op0
, tree op1
,
2399 bool follow_assert_exprs
, bool reversed
, tree
*new_cst
)
2401 /* See if this is a relational operation between two SSA_NAMES with
2402 unsigned, overflow wrapping values. If so, check it more deeply. */
2403 if ((code
== LT_EXPR
|| code
== LE_EXPR
2404 || code
== GE_EXPR
|| code
== GT_EXPR
)
2405 && TREE_CODE (op0
) == SSA_NAME
2406 && TREE_CODE (op1
) == SSA_NAME
2407 && INTEGRAL_TYPE_P (TREE_TYPE (op0
))
2408 && TYPE_UNSIGNED (TREE_TYPE (op0
))
2409 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (op0
)))
2411 gimple
*op1_def
= SSA_NAME_DEF_STMT (op1
);
2413 /* If requested, follow any ASSERT_EXPRs backwards for OP1. */
2414 if (follow_assert_exprs
)
2416 while (gimple_assign_single_p (op1_def
)
2417 && TREE_CODE (gimple_assign_rhs1 (op1_def
)) == ASSERT_EXPR
)
2419 op1
= TREE_OPERAND (gimple_assign_rhs1 (op1_def
), 0);
2420 if (TREE_CODE (op1
) != SSA_NAME
)
2422 op1_def
= SSA_NAME_DEF_STMT (op1
);
2426 /* Now look at the defining statement of OP1 to see if it adds
2427 or subtracts a nonzero constant from another operand. */
2429 && is_gimple_assign (op1_def
)
2430 && gimple_assign_rhs_code (op1_def
) == PLUS_EXPR
2431 && TREE_CODE (gimple_assign_rhs2 (op1_def
)) == INTEGER_CST
2432 && !integer_zerop (gimple_assign_rhs2 (op1_def
)))
2434 tree target
= gimple_assign_rhs1 (op1_def
);
2436 /* If requested, follow ASSERT_EXPRs backwards for op0 looking
2437 for one where TARGET appears on the RHS. */
2438 if (follow_assert_exprs
)
2440 /* Now see if that "other operand" is op0, following the chain
2441 of ASSERT_EXPRs if necessary. */
2442 gimple
*op0_def
= SSA_NAME_DEF_STMT (op0
);
2443 while (op0
!= target
2444 && gimple_assign_single_p (op0_def
)
2445 && TREE_CODE (gimple_assign_rhs1 (op0_def
)) == ASSERT_EXPR
)
2447 op0
= TREE_OPERAND (gimple_assign_rhs1 (op0_def
), 0);
2448 if (TREE_CODE (op0
) != SSA_NAME
)
2450 op0_def
= SSA_NAME_DEF_STMT (op0
);
2454 /* If we did not find our target SSA_NAME, then this is not
2455 an overflow test. */
2459 tree type
= TREE_TYPE (op0
);
2460 wide_int max
= wi::max_value (TYPE_PRECISION (type
), UNSIGNED
);
2461 tree inc
= gimple_assign_rhs2 (op1_def
);
2463 *new_cst
= wide_int_to_tree (type
, max
+ wi::to_wide (inc
));
2465 *new_cst
= wide_int_to_tree (type
, max
- wi::to_wide (inc
));
2472 /* OP0 CODE OP1 is a comparison. Examine the comparison and potentially
2473 OP1's defining statement to see if it ultimately has the form
2474 OP0 CODE (OP0 PLUS INTEGER_CST)
2476 If so, return TRUE indicating this is an overflow test and store into
2477 *NEW_CST an updated constant that can be used in a narrowed range test.
2479 These statements are left as-is in the IL to facilitate discovery of
2480 {ADD,SUB}_OVERFLOW sequences later in the optimizer pipeline. But
2481 the alternate range representation is often useful within VRP. */
2484 overflow_comparison_p (tree_code code
, tree name
, tree val
,
2485 bool use_equiv_p
, tree
*new_cst
)
2487 if (overflow_comparison_p_1 (code
, name
, val
, use_equiv_p
, false, new_cst
))
2489 return overflow_comparison_p_1 (swap_tree_comparison (code
), val
, name
,
2490 use_equiv_p
, true, new_cst
);
2494 /* Try to register an edge assertion for SSA name NAME on edge E for
2495 the condition COND contributing to the conditional jump pointed to by BSI.
2496 Invert the condition COND if INVERT is true. */
2499 register_edge_assert_for_2 (tree name
, edge e
,
2500 enum tree_code cond_code
,
2501 tree cond_op0
, tree cond_op1
, bool invert
,
2502 vec
<assert_info
> &asserts
)
2505 enum tree_code comp_code
;
2507 if (!extract_code_and_val_from_cond_with_ops (name
, cond_code
,
2510 invert
, &comp_code
, &val
))
2513 /* Queue the assert. */
2515 if (overflow_comparison_p (comp_code
, name
, val
, false, &x
))
2517 enum tree_code new_code
= ((comp_code
== GT_EXPR
|| comp_code
== GE_EXPR
)
2518 ? GT_EXPR
: LE_EXPR
);
2519 add_assert_info (asserts
, name
, name
, new_code
, x
);
2521 add_assert_info (asserts
, name
, name
, comp_code
, val
);
2523 /* In the case of NAME <= CST and NAME being defined as
2524 NAME = (unsigned) NAME2 + CST2 we can assert NAME2 >= -CST2
2525 and NAME2 <= CST - CST2. We can do the same for NAME > CST.
2526 This catches range and anti-range tests. */
2527 if ((comp_code
== LE_EXPR
2528 || comp_code
== GT_EXPR
)
2529 && TREE_CODE (val
) == INTEGER_CST
2530 && TYPE_UNSIGNED (TREE_TYPE (val
)))
2532 gimple
*def_stmt
= SSA_NAME_DEF_STMT (name
);
2533 tree cst2
= NULL_TREE
, name2
= NULL_TREE
, name3
= NULL_TREE
;
2535 /* Extract CST2 from the (optional) addition. */
2536 if (is_gimple_assign (def_stmt
)
2537 && gimple_assign_rhs_code (def_stmt
) == PLUS_EXPR
)
2539 name2
= gimple_assign_rhs1 (def_stmt
);
2540 cst2
= gimple_assign_rhs2 (def_stmt
);
2541 if (TREE_CODE (name2
) == SSA_NAME
2542 && TREE_CODE (cst2
) == INTEGER_CST
)
2543 def_stmt
= SSA_NAME_DEF_STMT (name2
);
2546 /* Extract NAME2 from the (optional) sign-changing cast. */
2547 if (gimple_assign_cast_p (def_stmt
))
2549 if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt
))
2550 && ! TYPE_UNSIGNED (TREE_TYPE (gimple_assign_rhs1 (def_stmt
)))
2551 && (TYPE_PRECISION (gimple_expr_type (def_stmt
))
2552 == TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (def_stmt
)))))
2553 name3
= gimple_assign_rhs1 (def_stmt
);
2556 /* If name3 is used later, create an ASSERT_EXPR for it. */
2557 if (name3
!= NULL_TREE
2558 && TREE_CODE (name3
) == SSA_NAME
2559 && (cst2
== NULL_TREE
2560 || TREE_CODE (cst2
) == INTEGER_CST
)
2561 && INTEGRAL_TYPE_P (TREE_TYPE (name3
)))
2565 /* Build an expression for the range test. */
2566 tmp
= build1 (NOP_EXPR
, TREE_TYPE (name
), name3
);
2567 if (cst2
!= NULL_TREE
)
2568 tmp
= build2 (PLUS_EXPR
, TREE_TYPE (name
), tmp
, cst2
);
2572 fprintf (dump_file
, "Adding assert for ");
2573 print_generic_expr (dump_file
, name3
);
2574 fprintf (dump_file
, " from ");
2575 print_generic_expr (dump_file
, tmp
);
2576 fprintf (dump_file
, "\n");
2579 add_assert_info (asserts
, name3
, tmp
, comp_code
, val
);
2582 /* If name2 is used later, create an ASSERT_EXPR for it. */
2583 if (name2
!= NULL_TREE
2584 && TREE_CODE (name2
) == SSA_NAME
2585 && TREE_CODE (cst2
) == INTEGER_CST
2586 && INTEGRAL_TYPE_P (TREE_TYPE (name2
)))
2590 /* Build an expression for the range test. */
2592 if (TREE_TYPE (name
) != TREE_TYPE (name2
))
2593 tmp
= build1 (NOP_EXPR
, TREE_TYPE (name
), tmp
);
2594 if (cst2
!= NULL_TREE
)
2595 tmp
= build2 (PLUS_EXPR
, TREE_TYPE (name
), tmp
, cst2
);
2599 fprintf (dump_file
, "Adding assert for ");
2600 print_generic_expr (dump_file
, name2
);
2601 fprintf (dump_file
, " from ");
2602 print_generic_expr (dump_file
, tmp
);
2603 fprintf (dump_file
, "\n");
2606 add_assert_info (asserts
, name2
, tmp
, comp_code
, val
);
2610 /* In the case of post-in/decrement tests like if (i++) ... and uses
2611 of the in/decremented value on the edge the extra name we want to
2612 assert for is not on the def chain of the name compared. Instead
2613 it is in the set of use stmts.
2614 Similar cases happen for conversions that were simplified through
2615 fold_{sign_changed,widened}_comparison. */
2616 if ((comp_code
== NE_EXPR
2617 || comp_code
== EQ_EXPR
)
2618 && TREE_CODE (val
) == INTEGER_CST
)
2620 imm_use_iterator ui
;
2622 FOR_EACH_IMM_USE_STMT (use_stmt
, ui
, name
)
2624 if (!is_gimple_assign (use_stmt
))
2627 /* Cut off to use-stmts that are dominating the predecessor. */
2628 if (!dominated_by_p (CDI_DOMINATORS
, e
->src
, gimple_bb (use_stmt
)))
2631 tree name2
= gimple_assign_lhs (use_stmt
);
2632 if (TREE_CODE (name2
) != SSA_NAME
)
2635 enum tree_code code
= gimple_assign_rhs_code (use_stmt
);
2637 if (code
== PLUS_EXPR
2638 || code
== MINUS_EXPR
)
2640 cst
= gimple_assign_rhs2 (use_stmt
);
2641 if (TREE_CODE (cst
) != INTEGER_CST
)
2643 cst
= int_const_binop (code
, val
, cst
);
2645 else if (CONVERT_EXPR_CODE_P (code
))
2647 /* For truncating conversions we cannot record
2649 if (comp_code
== NE_EXPR
2650 && (TYPE_PRECISION (TREE_TYPE (name2
))
2651 < TYPE_PRECISION (TREE_TYPE (name
))))
2653 cst
= fold_convert (TREE_TYPE (name2
), val
);
2658 if (TREE_OVERFLOW_P (cst
))
2659 cst
= drop_tree_overflow (cst
);
2660 add_assert_info (asserts
, name2
, name2
, comp_code
, cst
);
2664 if (TREE_CODE_CLASS (comp_code
) == tcc_comparison
2665 && TREE_CODE (val
) == INTEGER_CST
)
2667 gimple
*def_stmt
= SSA_NAME_DEF_STMT (name
);
2668 tree name2
= NULL_TREE
, names
[2], cst2
= NULL_TREE
;
2669 tree val2
= NULL_TREE
;
2670 unsigned int prec
= TYPE_PRECISION (TREE_TYPE (val
));
2671 wide_int mask
= wi::zero (prec
);
2672 unsigned int nprec
= prec
;
2673 enum tree_code rhs_code
= ERROR_MARK
;
2675 if (is_gimple_assign (def_stmt
))
2676 rhs_code
= gimple_assign_rhs_code (def_stmt
);
2678 /* In the case of NAME != CST1 where NAME = A +- CST2 we can
2679 assert that A != CST1 -+ CST2. */
2680 if ((comp_code
== EQ_EXPR
|| comp_code
== NE_EXPR
)
2681 && (rhs_code
== PLUS_EXPR
|| rhs_code
== MINUS_EXPR
))
2683 tree op0
= gimple_assign_rhs1 (def_stmt
);
2684 tree op1
= gimple_assign_rhs2 (def_stmt
);
2685 if (TREE_CODE (op0
) == SSA_NAME
2686 && TREE_CODE (op1
) == INTEGER_CST
)
2688 enum tree_code reverse_op
= (rhs_code
== PLUS_EXPR
2689 ? MINUS_EXPR
: PLUS_EXPR
);
2690 op1
= int_const_binop (reverse_op
, val
, op1
);
2691 if (TREE_OVERFLOW (op1
))
2692 op1
= drop_tree_overflow (op1
);
2693 add_assert_info (asserts
, op0
, op0
, comp_code
, op1
);
2697 /* Add asserts for NAME cmp CST and NAME being defined
2698 as NAME = (int) NAME2. */
2699 if (!TYPE_UNSIGNED (TREE_TYPE (val
))
2700 && (comp_code
== LE_EXPR
|| comp_code
== LT_EXPR
2701 || comp_code
== GT_EXPR
|| comp_code
== GE_EXPR
)
2702 && gimple_assign_cast_p (def_stmt
))
2704 name2
= gimple_assign_rhs1 (def_stmt
);
2705 if (CONVERT_EXPR_CODE_P (rhs_code
)
2706 && INTEGRAL_TYPE_P (TREE_TYPE (name2
))
2707 && TYPE_UNSIGNED (TREE_TYPE (name2
))
2708 && prec
== TYPE_PRECISION (TREE_TYPE (name2
))
2709 && (comp_code
== LE_EXPR
|| comp_code
== GT_EXPR
2710 || !tree_int_cst_equal (val
,
2711 TYPE_MIN_VALUE (TREE_TYPE (val
)))))
2714 enum tree_code new_comp_code
= comp_code
;
2716 cst
= fold_convert (TREE_TYPE (name2
),
2717 TYPE_MIN_VALUE (TREE_TYPE (val
)));
2718 /* Build an expression for the range test. */
2719 tmp
= build2 (PLUS_EXPR
, TREE_TYPE (name2
), name2
, cst
);
2720 cst
= fold_build2 (PLUS_EXPR
, TREE_TYPE (name2
), cst
,
2721 fold_convert (TREE_TYPE (name2
), val
));
2722 if (comp_code
== LT_EXPR
|| comp_code
== GE_EXPR
)
2724 new_comp_code
= comp_code
== LT_EXPR
? LE_EXPR
: GT_EXPR
;
2725 cst
= fold_build2 (MINUS_EXPR
, TREE_TYPE (name2
), cst
,
2726 build_int_cst (TREE_TYPE (name2
), 1));
2731 fprintf (dump_file
, "Adding assert for ");
2732 print_generic_expr (dump_file
, name2
);
2733 fprintf (dump_file
, " from ");
2734 print_generic_expr (dump_file
, tmp
);
2735 fprintf (dump_file
, "\n");
2738 add_assert_info (asserts
, name2
, tmp
, new_comp_code
, cst
);
2742 /* Add asserts for NAME cmp CST and NAME being defined as
2743 NAME = NAME2 >> CST2.
2745 Extract CST2 from the right shift. */
2746 if (rhs_code
== RSHIFT_EXPR
)
2748 name2
= gimple_assign_rhs1 (def_stmt
);
2749 cst2
= gimple_assign_rhs2 (def_stmt
);
2750 if (TREE_CODE (name2
) == SSA_NAME
2751 && tree_fits_uhwi_p (cst2
)
2752 && INTEGRAL_TYPE_P (TREE_TYPE (name2
))
2753 && IN_RANGE (tree_to_uhwi (cst2
), 1, prec
- 1)
2754 && type_has_mode_precision_p (TREE_TYPE (val
)))
2756 mask
= wi::mask (tree_to_uhwi (cst2
), false, prec
);
2757 val2
= fold_binary (LSHIFT_EXPR
, TREE_TYPE (val
), val
, cst2
);
2760 if (val2
!= NULL_TREE
2761 && TREE_CODE (val2
) == INTEGER_CST
2762 && simple_cst_equal (fold_build2 (RSHIFT_EXPR
,
2766 enum tree_code new_comp_code
= comp_code
;
2770 if (comp_code
== EQ_EXPR
|| comp_code
== NE_EXPR
)
2772 if (!TYPE_UNSIGNED (TREE_TYPE (val
)))
2774 tree type
= build_nonstandard_integer_type (prec
, 1);
2775 tmp
= build1 (NOP_EXPR
, type
, name2
);
2776 val2
= fold_convert (type
, val2
);
2778 tmp
= fold_build2 (MINUS_EXPR
, TREE_TYPE (tmp
), tmp
, val2
);
2779 new_val
= wide_int_to_tree (TREE_TYPE (tmp
), mask
);
2780 new_comp_code
= comp_code
== EQ_EXPR
? LE_EXPR
: GT_EXPR
;
2782 else if (comp_code
== LT_EXPR
|| comp_code
== GE_EXPR
)
2785 = wi::min_value (prec
, TYPE_SIGN (TREE_TYPE (val
)));
2787 if (minval
== wi::to_wide (new_val
))
2788 new_val
= NULL_TREE
;
2793 = wi::max_value (prec
, TYPE_SIGN (TREE_TYPE (val
)));
2794 mask
|= wi::to_wide (val2
);
2795 if (wi::eq_p (mask
, maxval
))
2796 new_val
= NULL_TREE
;
2798 new_val
= wide_int_to_tree (TREE_TYPE (val2
), mask
);
2805 fprintf (dump_file
, "Adding assert for ");
2806 print_generic_expr (dump_file
, name2
);
2807 fprintf (dump_file
, " from ");
2808 print_generic_expr (dump_file
, tmp
);
2809 fprintf (dump_file
, "\n");
2812 add_assert_info (asserts
, name2
, tmp
, new_comp_code
, new_val
);
2816 /* Add asserts for NAME cmp CST and NAME being defined as
2817 NAME = NAME2 & CST2.
2819 Extract CST2 from the and.
2822 NAME = (unsigned) NAME2;
2823 casts where NAME's type is unsigned and has smaller precision
2824 than NAME2's type as if it was NAME = NAME2 & MASK. */
2825 names
[0] = NULL_TREE
;
2826 names
[1] = NULL_TREE
;
2828 if (rhs_code
== BIT_AND_EXPR
2829 || (CONVERT_EXPR_CODE_P (rhs_code
)
2830 && INTEGRAL_TYPE_P (TREE_TYPE (val
))
2831 && TYPE_UNSIGNED (TREE_TYPE (val
))
2832 && TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (def_stmt
)))
2835 name2
= gimple_assign_rhs1 (def_stmt
);
2836 if (rhs_code
== BIT_AND_EXPR
)
2837 cst2
= gimple_assign_rhs2 (def_stmt
);
2840 cst2
= TYPE_MAX_VALUE (TREE_TYPE (val
));
2841 nprec
= TYPE_PRECISION (TREE_TYPE (name2
));
2843 if (TREE_CODE (name2
) == SSA_NAME
2844 && INTEGRAL_TYPE_P (TREE_TYPE (name2
))
2845 && TREE_CODE (cst2
) == INTEGER_CST
2846 && !integer_zerop (cst2
)
2848 || TYPE_UNSIGNED (TREE_TYPE (val
))))
2850 gimple
*def_stmt2
= SSA_NAME_DEF_STMT (name2
);
2851 if (gimple_assign_cast_p (def_stmt2
))
2853 names
[1] = gimple_assign_rhs1 (def_stmt2
);
2854 if (!CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt2
))
2855 || !INTEGRAL_TYPE_P (TREE_TYPE (names
[1]))
2856 || (TYPE_PRECISION (TREE_TYPE (name2
))
2857 != TYPE_PRECISION (TREE_TYPE (names
[1]))))
2858 names
[1] = NULL_TREE
;
2863 if (names
[0] || names
[1])
2865 wide_int minv
, maxv
, valv
, cst2v
;
2866 wide_int tem
, sgnbit
;
2867 bool valid_p
= false, valn
, cst2n
;
2868 enum tree_code ccode
= comp_code
;
2870 valv
= wide_int::from (wi::to_wide (val
), nprec
, UNSIGNED
);
2871 cst2v
= wide_int::from (wi::to_wide (cst2
), nprec
, UNSIGNED
);
2872 valn
= wi::neg_p (valv
, TYPE_SIGN (TREE_TYPE (val
)));
2873 cst2n
= wi::neg_p (cst2v
, TYPE_SIGN (TREE_TYPE (val
)));
2874 /* If CST2 doesn't have most significant bit set,
2875 but VAL is negative, we have comparison like
2876 if ((x & 0x123) > -4) (always true). Just give up. */
2880 sgnbit
= wi::set_bit_in_zero (nprec
- 1, nprec
);
2882 sgnbit
= wi::zero (nprec
);
2883 minv
= valv
& cst2v
;
2887 /* Minimum unsigned value for equality is VAL & CST2
2888 (should be equal to VAL, otherwise we probably should
2889 have folded the comparison into false) and
2890 maximum unsigned value is VAL | ~CST2. */
2891 maxv
= valv
| ~cst2v
;
2896 tem
= valv
| ~cst2v
;
2897 /* If VAL is 0, handle (X & CST2) != 0 as (X & CST2) > 0U. */
2901 sgnbit
= wi::zero (nprec
);
2904 /* If (VAL | ~CST2) is all ones, handle it as
2905 (X & CST2) < VAL. */
2910 sgnbit
= wi::zero (nprec
);
2913 if (!cst2n
&& wi::neg_p (cst2v
))
2914 sgnbit
= wi::set_bit_in_zero (nprec
- 1, nprec
);
2923 if (tem
== wi::mask (nprec
- 1, false, nprec
))
2929 sgnbit
= wi::zero (nprec
);
2934 /* Minimum unsigned value for >= if (VAL & CST2) == VAL
2935 is VAL and maximum unsigned value is ~0. For signed
2936 comparison, if CST2 doesn't have most significant bit
2937 set, handle it similarly. If CST2 has MSB set,
2938 the minimum is the same, and maximum is ~0U/2. */
2941 /* If (VAL & CST2) != VAL, X & CST2 can't be equal to
2943 minv
= masked_increment (valv
, cst2v
, sgnbit
, nprec
);
2947 maxv
= wi::mask (nprec
- (cst2n
? 1 : 0), false, nprec
);
2953 /* Find out smallest MINV where MINV > VAL
2954 && (MINV & CST2) == MINV, if any. If VAL is signed and
2955 CST2 has MSB set, compute it biased by 1 << (nprec - 1). */
2956 minv
= masked_increment (valv
, cst2v
, sgnbit
, nprec
);
2959 maxv
= wi::mask (nprec
- (cst2n
? 1 : 0), false, nprec
);
2964 /* Minimum unsigned value for <= is 0 and maximum
2965 unsigned value is VAL | ~CST2 if (VAL & CST2) == VAL.
2966 Otherwise, find smallest VAL2 where VAL2 > VAL
2967 && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
2969 For signed comparison, if CST2 doesn't have most
2970 significant bit set, handle it similarly. If CST2 has
2971 MSB set, the maximum is the same and minimum is INT_MIN. */
2976 maxv
= masked_increment (valv
, cst2v
, sgnbit
, nprec
);
2988 /* Minimum unsigned value for < is 0 and maximum
2989 unsigned value is (VAL-1) | ~CST2 if (VAL & CST2) == VAL.
2990 Otherwise, find smallest VAL2 where VAL2 > VAL
2991 && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
2993 For signed comparison, if CST2 doesn't have most
2994 significant bit set, handle it similarly. If CST2 has
2995 MSB set, the maximum is the same and minimum is INT_MIN. */
3004 maxv
= masked_increment (valv
, cst2v
, sgnbit
, nprec
);
3018 && (maxv
- minv
) != -1)
3020 tree tmp
, new_val
, type
;
3023 for (i
= 0; i
< 2; i
++)
3026 wide_int maxv2
= maxv
;
3028 type
= TREE_TYPE (names
[i
]);
3029 if (!TYPE_UNSIGNED (type
))
3031 type
= build_nonstandard_integer_type (nprec
, 1);
3032 tmp
= build1 (NOP_EXPR
, type
, names
[i
]);
3036 tmp
= build2 (PLUS_EXPR
, type
, tmp
,
3037 wide_int_to_tree (type
, -minv
));
3038 maxv2
= maxv
- minv
;
3040 new_val
= wide_int_to_tree (type
, maxv2
);
3044 fprintf (dump_file
, "Adding assert for ");
3045 print_generic_expr (dump_file
, names
[i
]);
3046 fprintf (dump_file
, " from ");
3047 print_generic_expr (dump_file
, tmp
);
3048 fprintf (dump_file
, "\n");
3051 add_assert_info (asserts
, names
[i
], tmp
, LE_EXPR
, new_val
);
3058 /* OP is an operand of a truth value expression which is known to have
3059 a particular value. Register any asserts for OP and for any
3060 operands in OP's defining statement.
3062 If CODE is EQ_EXPR, then we want to register OP is zero (false),
3063 if CODE is NE_EXPR, then we want to register OP is nonzero (true). */
3066 register_edge_assert_for_1 (tree op
, enum tree_code code
,
3067 edge e
, vec
<assert_info
> &asserts
)
3071 enum tree_code rhs_code
;
3073 /* We only care about SSA_NAMEs. */
3074 if (TREE_CODE (op
) != SSA_NAME
)
3077 /* We know that OP will have a zero or nonzero value. */
3078 val
= build_int_cst (TREE_TYPE (op
), 0);
3079 add_assert_info (asserts
, op
, op
, code
, val
);
3081 /* Now look at how OP is set. If it's set from a comparison,
3082 a truth operation or some bit operations, then we may be able
3083 to register information about the operands of that assignment. */
3084 op_def
= SSA_NAME_DEF_STMT (op
);
3085 if (gimple_code (op_def
) != GIMPLE_ASSIGN
)
3088 rhs_code
= gimple_assign_rhs_code (op_def
);
3090 if (TREE_CODE_CLASS (rhs_code
) == tcc_comparison
)
3092 bool invert
= (code
== EQ_EXPR
? true : false);
3093 tree op0
= gimple_assign_rhs1 (op_def
);
3094 tree op1
= gimple_assign_rhs2 (op_def
);
3096 if (TREE_CODE (op0
) == SSA_NAME
)
3097 register_edge_assert_for_2 (op0
, e
, rhs_code
, op0
, op1
, invert
, asserts
);
3098 if (TREE_CODE (op1
) == SSA_NAME
)
3099 register_edge_assert_for_2 (op1
, e
, rhs_code
, op0
, op1
, invert
, asserts
);
3101 else if ((code
== NE_EXPR
3102 && gimple_assign_rhs_code (op_def
) == BIT_AND_EXPR
)
3104 && gimple_assign_rhs_code (op_def
) == BIT_IOR_EXPR
))
3106 /* Recurse on each operand. */
3107 tree op0
= gimple_assign_rhs1 (op_def
);
3108 tree op1
= gimple_assign_rhs2 (op_def
);
3109 if (TREE_CODE (op0
) == SSA_NAME
3110 && has_single_use (op0
))
3111 register_edge_assert_for_1 (op0
, code
, e
, asserts
);
3112 if (TREE_CODE (op1
) == SSA_NAME
3113 && has_single_use (op1
))
3114 register_edge_assert_for_1 (op1
, code
, e
, asserts
);
3116 else if (gimple_assign_rhs_code (op_def
) == BIT_NOT_EXPR
3117 && TYPE_PRECISION (TREE_TYPE (gimple_assign_lhs (op_def
))) == 1)
3119 /* Recurse, flipping CODE. */
3120 code
= invert_tree_comparison (code
, false);
3121 register_edge_assert_for_1 (gimple_assign_rhs1 (op_def
), code
, e
, asserts
);
3123 else if (gimple_assign_rhs_code (op_def
) == SSA_NAME
)
3125 /* Recurse through the copy. */
3126 register_edge_assert_for_1 (gimple_assign_rhs1 (op_def
), code
, e
, asserts
);
3128 else if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (op_def
)))
3130 /* Recurse through the type conversion, unless it is a narrowing
3131 conversion or conversion from non-integral type. */
3132 tree rhs
= gimple_assign_rhs1 (op_def
);
3133 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs
))
3134 && (TYPE_PRECISION (TREE_TYPE (rhs
))
3135 <= TYPE_PRECISION (TREE_TYPE (op
))))
3136 register_edge_assert_for_1 (rhs
, code
, e
, asserts
);
3140 /* Check if comparison
3141 NAME COND_OP INTEGER_CST
3143 (X & 11...100..0) COND_OP XX...X00...0
3144 Such comparison can yield assertions like
3147 in case of COND_OP being EQ_EXPR or
3150 in case of NE_EXPR. */
3153 is_masked_range_test (tree name
, tree valt
, enum tree_code cond_code
,
3154 tree
*new_name
, tree
*low
, enum tree_code
*low_code
,
3155 tree
*high
, enum tree_code
*high_code
)
3157 gimple
*def_stmt
= SSA_NAME_DEF_STMT (name
);
3159 if (!is_gimple_assign (def_stmt
)
3160 || gimple_assign_rhs_code (def_stmt
) != BIT_AND_EXPR
)
3163 tree t
= gimple_assign_rhs1 (def_stmt
);
3164 tree maskt
= gimple_assign_rhs2 (def_stmt
);
3165 if (TREE_CODE (t
) != SSA_NAME
|| TREE_CODE (maskt
) != INTEGER_CST
)
3168 wi::tree_to_wide_ref mask
= wi::to_wide (maskt
);
3169 wide_int inv_mask
= ~mask
;
3170 /* Must have been removed by now so don't bother optimizing. */
3171 if (mask
== 0 || inv_mask
== 0)
3174 /* Assume VALT is INTEGER_CST. */
3175 wi::tree_to_wide_ref val
= wi::to_wide (valt
);
3177 if ((inv_mask
& (inv_mask
+ 1)) != 0
3178 || (val
& mask
) != val
)
3181 bool is_range
= cond_code
== EQ_EXPR
;
3183 tree type
= TREE_TYPE (t
);
3184 wide_int min
= wi::min_value (type
),
3185 max
= wi::max_value (type
);
3189 *low_code
= val
== min
? ERROR_MARK
: GE_EXPR
;
3190 *high_code
= val
== max
? ERROR_MARK
: LE_EXPR
;
3194 /* We can still generate assertion if one of alternatives
3195 is known to always be false. */
3198 *low_code
= (enum tree_code
) 0;
3199 *high_code
= GT_EXPR
;
3201 else if ((val
| inv_mask
) == max
)
3203 *low_code
= LT_EXPR
;
3204 *high_code
= (enum tree_code
) 0;
3211 *low
= wide_int_to_tree (type
, val
);
3212 *high
= wide_int_to_tree (type
, val
| inv_mask
);
3217 /* Try to register an edge assertion for SSA name NAME on edge E for
3218 the condition COND contributing to the conditional jump pointed to by
3222 register_edge_assert_for (tree name
, edge e
,
3223 enum tree_code cond_code
, tree cond_op0
,
3224 tree cond_op1
, vec
<assert_info
> &asserts
)
3227 enum tree_code comp_code
;
3228 bool is_else_edge
= (e
->flags
& EDGE_FALSE_VALUE
) != 0;
3230 /* Do not attempt to infer anything in names that flow through
3232 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name
))
3235 if (!extract_code_and_val_from_cond_with_ops (name
, cond_code
,
3241 /* Register ASSERT_EXPRs for name. */
3242 register_edge_assert_for_2 (name
, e
, cond_code
, cond_op0
,
3243 cond_op1
, is_else_edge
, asserts
);
3246 /* If COND is effectively an equality test of an SSA_NAME against
3247 the value zero or one, then we may be able to assert values
3248 for SSA_NAMEs which flow into COND. */
3250 /* In the case of NAME == 1 or NAME != 0, for BIT_AND_EXPR defining
3251 statement of NAME we can assert both operands of the BIT_AND_EXPR
3252 have nonzero value. */
3253 if (((comp_code
== EQ_EXPR
&& integer_onep (val
))
3254 || (comp_code
== NE_EXPR
&& integer_zerop (val
))))
3256 gimple
*def_stmt
= SSA_NAME_DEF_STMT (name
);
3258 if (is_gimple_assign (def_stmt
)
3259 && gimple_assign_rhs_code (def_stmt
) == BIT_AND_EXPR
)
3261 tree op0
= gimple_assign_rhs1 (def_stmt
);
3262 tree op1
= gimple_assign_rhs2 (def_stmt
);
3263 register_edge_assert_for_1 (op0
, NE_EXPR
, e
, asserts
);
3264 register_edge_assert_for_1 (op1
, NE_EXPR
, e
, asserts
);
3268 /* In the case of NAME == 0 or NAME != 1, for BIT_IOR_EXPR defining
3269 statement of NAME we can assert both operands of the BIT_IOR_EXPR
3271 if (((comp_code
== EQ_EXPR
&& integer_zerop (val
))
3272 || (comp_code
== NE_EXPR
&& integer_onep (val
))))
3274 gimple
*def_stmt
= SSA_NAME_DEF_STMT (name
);
3276 /* For BIT_IOR_EXPR only if NAME == 0 both operands have
3277 necessarily zero value, or if type-precision is one. */
3278 if (is_gimple_assign (def_stmt
)
3279 && (gimple_assign_rhs_code (def_stmt
) == BIT_IOR_EXPR
3280 && (TYPE_PRECISION (TREE_TYPE (name
)) == 1
3281 || comp_code
== EQ_EXPR
)))
3283 tree op0
= gimple_assign_rhs1 (def_stmt
);
3284 tree op1
= gimple_assign_rhs2 (def_stmt
);
3285 register_edge_assert_for_1 (op0
, EQ_EXPR
, e
, asserts
);
3286 register_edge_assert_for_1 (op1
, EQ_EXPR
, e
, asserts
);
3290 /* Sometimes we can infer ranges from (NAME & MASK) == VALUE. */
3291 if ((comp_code
== EQ_EXPR
|| comp_code
== NE_EXPR
)
3292 && TREE_CODE (val
) == INTEGER_CST
)
3294 enum tree_code low_code
, high_code
;
3296 if (is_masked_range_test (name
, val
, comp_code
, &name
, &low
,
3297 &low_code
, &high
, &high_code
))
3299 if (low_code
!= ERROR_MARK
)
3300 register_edge_assert_for_2 (name
, e
, low_code
, name
,
3301 low
, /*invert*/false, asserts
);
3302 if (high_code
!= ERROR_MARK
)
3303 register_edge_assert_for_2 (name
, e
, high_code
, name
,
3304 high
, /*invert*/false, asserts
);
3309 /* Finish found ASSERTS for E and register them at GSI. */
3312 finish_register_edge_assert_for (edge e
, gimple_stmt_iterator gsi
,
3313 vec
<assert_info
> &asserts
)
3315 for (unsigned i
= 0; i
< asserts
.length (); ++i
)
3316 /* Only register an ASSERT_EXPR if NAME was found in the sub-graph
3317 reachable from E. */
3318 if (live_on_edge (e
, asserts
[i
].name
))
3319 register_new_assert_for (asserts
[i
].name
, asserts
[i
].expr
,
3320 asserts
[i
].comp_code
, asserts
[i
].val
,
3326 /* Determine whether the outgoing edges of BB should receive an
3327 ASSERT_EXPR for each of the operands of BB's LAST statement.
3328 The last statement of BB must be a COND_EXPR.
3330 If any of the sub-graphs rooted at BB have an interesting use of
3331 the predicate operands, an assert location node is added to the
3332 list of assertions for the corresponding operands. */
3335 find_conditional_asserts (basic_block bb
, gcond
*last
)
3337 gimple_stmt_iterator bsi
;
3343 bsi
= gsi_for_stmt (last
);
3345 /* Look for uses of the operands in each of the sub-graphs
3346 rooted at BB. We need to check each of the outgoing edges
3347 separately, so that we know what kind of ASSERT_EXPR to
3349 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
3354 /* Register the necessary assertions for each operand in the
3355 conditional predicate. */
3356 auto_vec
<assert_info
, 8> asserts
;
3357 FOR_EACH_SSA_TREE_OPERAND (op
, last
, iter
, SSA_OP_USE
)
3358 register_edge_assert_for (op
, e
,
3359 gimple_cond_code (last
),
3360 gimple_cond_lhs (last
),
3361 gimple_cond_rhs (last
), asserts
);
3362 finish_register_edge_assert_for (e
, bsi
, asserts
);
3372 /* Compare two case labels sorting first by the destination bb index
3373 and then by the case value. */
3376 compare_case_labels (const void *p1
, const void *p2
)
3378 const struct case_info
*ci1
= (const struct case_info
*) p1
;
3379 const struct case_info
*ci2
= (const struct case_info
*) p2
;
3380 int idx1
= ci1
->bb
->index
;
3381 int idx2
= ci2
->bb
->index
;
3385 else if (idx1
== idx2
)
3387 /* Make sure the default label is first in a group. */
3388 if (!CASE_LOW (ci1
->expr
))
3390 else if (!CASE_LOW (ci2
->expr
))
3393 return tree_int_cst_compare (CASE_LOW (ci1
->expr
),
3394 CASE_LOW (ci2
->expr
));
3400 /* Determine whether the outgoing edges of BB should receive an
3401 ASSERT_EXPR for each of the operands of BB's LAST statement.
3402 The last statement of BB must be a SWITCH_EXPR.
3404 If any of the sub-graphs rooted at BB have an interesting use of
3405 the predicate operands, an assert location node is added to the
3406 list of assertions for the corresponding operands. */
3409 find_switch_asserts (basic_block bb
, gswitch
*last
)
3411 gimple_stmt_iterator bsi
;
3414 struct case_info
*ci
;
3415 size_t n
= gimple_switch_num_labels (last
);
3416 #if GCC_VERSION >= 4000
3419 /* Work around GCC 3.4 bug (PR 37086). */
3420 volatile unsigned int idx
;
3423 bsi
= gsi_for_stmt (last
);
3424 op
= gimple_switch_index (last
);
3425 if (TREE_CODE (op
) != SSA_NAME
)
3428 /* Build a vector of case labels sorted by destination label. */
3429 ci
= XNEWVEC (struct case_info
, n
);
3430 for (idx
= 0; idx
< n
; ++idx
)
3432 ci
[idx
].expr
= gimple_switch_label (last
, idx
);
3433 ci
[idx
].bb
= label_to_block (cfun
, CASE_LABEL (ci
[idx
].expr
));
3435 edge default_edge
= find_edge (bb
, ci
[0].bb
);
3436 qsort (ci
, n
, sizeof (struct case_info
), compare_case_labels
);
3438 for (idx
= 0; idx
< n
; ++idx
)
3441 tree cl
= ci
[idx
].expr
;
3442 basic_block cbb
= ci
[idx
].bb
;
3444 min
= CASE_LOW (cl
);
3445 max
= CASE_HIGH (cl
);
3447 /* If there are multiple case labels with the same destination
3448 we need to combine them to a single value range for the edge. */
3449 if (idx
+ 1 < n
&& cbb
== ci
[idx
+ 1].bb
)
3451 /* Skip labels until the last of the group. */
3454 } while (idx
< n
&& cbb
== ci
[idx
].bb
);
3457 /* Pick up the maximum of the case label range. */
3458 if (CASE_HIGH (ci
[idx
].expr
))
3459 max
= CASE_HIGH (ci
[idx
].expr
);
3461 max
= CASE_LOW (ci
[idx
].expr
);
3464 /* Can't extract a useful assertion out of a range that includes the
3466 if (min
== NULL_TREE
)
3469 /* Find the edge to register the assert expr on. */
3470 e
= find_edge (bb
, cbb
);
3472 /* Register the necessary assertions for the operand in the
3474 auto_vec
<assert_info
, 8> asserts
;
3475 register_edge_assert_for (op
, e
,
3476 max
? GE_EXPR
: EQ_EXPR
,
3477 op
, fold_convert (TREE_TYPE (op
), min
),
3480 register_edge_assert_for (op
, e
, LE_EXPR
, op
,
3481 fold_convert (TREE_TYPE (op
), max
),
3483 finish_register_edge_assert_for (e
, bsi
, asserts
);
3488 if (!live_on_edge (default_edge
, op
))
3491 /* Now register along the default label assertions that correspond to the
3492 anti-range of each label. */
3493 int insertion_limit
= PARAM_VALUE (PARAM_MAX_VRP_SWITCH_ASSERTIONS
);
3494 if (insertion_limit
== 0)
3497 /* We can't do this if the default case shares a label with another case. */
3498 tree default_cl
= gimple_switch_default_label (last
);
3499 for (idx
= 1; idx
< n
; idx
++)
3502 tree cl
= gimple_switch_label (last
, idx
);
3503 if (CASE_LABEL (cl
) == CASE_LABEL (default_cl
))
3506 min
= CASE_LOW (cl
);
3507 max
= CASE_HIGH (cl
);
3509 /* Combine contiguous case ranges to reduce the number of assertions
3511 for (idx
= idx
+ 1; idx
< n
; idx
++)
3513 tree next_min
, next_max
;
3514 tree next_cl
= gimple_switch_label (last
, idx
);
3515 if (CASE_LABEL (next_cl
) == CASE_LABEL (default_cl
))
3518 next_min
= CASE_LOW (next_cl
);
3519 next_max
= CASE_HIGH (next_cl
);
3521 wide_int difference
= (wi::to_wide (next_min
)
3522 - wi::to_wide (max
? max
: min
));
3523 if (wi::eq_p (difference
, 1))
3524 max
= next_max
? next_max
: next_min
;
3530 if (max
== NULL_TREE
)
3532 /* Register the assertion OP != MIN. */
3533 auto_vec
<assert_info
, 8> asserts
;
3534 min
= fold_convert (TREE_TYPE (op
), min
);
3535 register_edge_assert_for (op
, default_edge
, NE_EXPR
, op
, min
,
3537 finish_register_edge_assert_for (default_edge
, bsi
, asserts
);
3541 /* Register the assertion (unsigned)OP - MIN > (MAX - MIN),
3542 which will give OP the anti-range ~[MIN,MAX]. */
3543 tree uop
= fold_convert (unsigned_type_for (TREE_TYPE (op
)), op
);
3544 min
= fold_convert (TREE_TYPE (uop
), min
);
3545 max
= fold_convert (TREE_TYPE (uop
), max
);
3547 tree lhs
= fold_build2 (MINUS_EXPR
, TREE_TYPE (uop
), uop
, min
);
3548 tree rhs
= int_const_binop (MINUS_EXPR
, max
, min
);
3549 register_new_assert_for (op
, lhs
, GT_EXPR
, rhs
,
3550 NULL
, default_edge
, bsi
);
3553 if (--insertion_limit
== 0)
3559 /* Traverse all the statements in block BB looking for statements that
3560 may generate useful assertions for the SSA names in their operand.
3561 If a statement produces a useful assertion A for name N_i, then the
3562 list of assertions already generated for N_i is scanned to
3563 determine if A is actually needed.
3565 If N_i already had the assertion A at a location dominating the
3566 current location, then nothing needs to be done. Otherwise, the
3567 new location for A is recorded instead.
3569 1- For every statement S in BB, all the variables used by S are
3570 added to bitmap FOUND_IN_SUBGRAPH.
3572 2- If statement S uses an operand N in a way that exposes a known
3573 value range for N, then if N was not already generated by an
3574 ASSERT_EXPR, create a new assert location for N. For instance,
3575 if N is a pointer and the statement dereferences it, we can
3576 assume that N is not NULL.
3578 3- COND_EXPRs are a special case of #2. We can derive range
3579 information from the predicate but need to insert different
3580 ASSERT_EXPRs for each of the sub-graphs rooted at the
3581 conditional block. If the last statement of BB is a conditional
3582 expression of the form 'X op Y', then
3584 a) Remove X and Y from the set FOUND_IN_SUBGRAPH.
3586 b) If the conditional is the only entry point to the sub-graph
3587 corresponding to the THEN_CLAUSE, recurse into it. On
3588 return, if X and/or Y are marked in FOUND_IN_SUBGRAPH, then
3589 an ASSERT_EXPR is added for the corresponding variable.
3591 c) Repeat step (b) on the ELSE_CLAUSE.
3593 d) Mark X and Y in FOUND_IN_SUBGRAPH.
3602 In this case, an assertion on the THEN clause is useful to
3603 determine that 'a' is always 9 on that edge. However, an assertion
3604 on the ELSE clause would be unnecessary.
3606 4- If BB does not end in a conditional expression, then we recurse
3607 into BB's dominator children.
3609 At the end of the recursive traversal, every SSA name will have a
3610 list of locations where ASSERT_EXPRs should be added. When a new
3611 location for name N is found, it is registered by calling
3612 register_new_assert_for. That function keeps track of all the
3613 registered assertions to prevent adding unnecessary assertions.
3614 For instance, if a pointer P_4 is dereferenced more than once in a
3615 dominator tree, only the location dominating all the dereference of
3616 P_4 will receive an ASSERT_EXPR. */
3619 find_assert_locations_1 (basic_block bb
, sbitmap live
)
3623 last
= last_stmt (bb
);
3625 /* If BB's last statement is a conditional statement involving integer
3626 operands, determine if we need to add ASSERT_EXPRs. */
3628 && gimple_code (last
) == GIMPLE_COND
3629 && !fp_predicate (last
)
3630 && !ZERO_SSA_OPERANDS (last
, SSA_OP_USE
))
3631 find_conditional_asserts (bb
, as_a
<gcond
*> (last
));
3633 /* If BB's last statement is a switch statement involving integer
3634 operands, determine if we need to add ASSERT_EXPRs. */
3636 && gimple_code (last
) == GIMPLE_SWITCH
3637 && !ZERO_SSA_OPERANDS (last
, SSA_OP_USE
))
3638 find_switch_asserts (bb
, as_a
<gswitch
*> (last
));
3640 /* Traverse all the statements in BB marking used names and looking
3641 for statements that may infer assertions for their used operands. */
3642 for (gimple_stmt_iterator si
= gsi_last_bb (bb
); !gsi_end_p (si
);
3649 stmt
= gsi_stmt (si
);
3651 if (is_gimple_debug (stmt
))
3654 /* See if we can derive an assertion for any of STMT's operands. */
3655 FOR_EACH_SSA_TREE_OPERAND (op
, stmt
, i
, SSA_OP_USE
)
3658 enum tree_code comp_code
;
3660 /* If op is not live beyond this stmt, do not bother to insert
3662 if (!bitmap_bit_p (live
, SSA_NAME_VERSION (op
)))
3665 /* If OP is used in such a way that we can infer a value
3666 range for it, and we don't find a previous assertion for
3667 it, create a new assertion location node for OP. */
3668 if (infer_value_range (stmt
, op
, &comp_code
, &value
))
3670 /* If we are able to infer a nonzero value range for OP,
3671 then walk backwards through the use-def chain to see if OP
3672 was set via a typecast.
3674 If so, then we can also infer a nonzero value range
3675 for the operand of the NOP_EXPR. */
3676 if (comp_code
== NE_EXPR
&& integer_zerop (value
))
3679 gimple
*def_stmt
= SSA_NAME_DEF_STMT (t
);
3681 while (is_gimple_assign (def_stmt
)
3682 && CONVERT_EXPR_CODE_P
3683 (gimple_assign_rhs_code (def_stmt
))
3685 (gimple_assign_rhs1 (def_stmt
)) == SSA_NAME
3687 (TREE_TYPE (gimple_assign_rhs1 (def_stmt
))))
3689 t
= gimple_assign_rhs1 (def_stmt
);
3690 def_stmt
= SSA_NAME_DEF_STMT (t
);
3692 /* Note we want to register the assert for the
3693 operand of the NOP_EXPR after SI, not after the
3695 if (bitmap_bit_p (live
, SSA_NAME_VERSION (t
)))
3696 register_new_assert_for (t
, t
, comp_code
, value
,
3701 register_new_assert_for (op
, op
, comp_code
, value
, bb
, NULL
, si
);
3706 FOR_EACH_SSA_TREE_OPERAND (op
, stmt
, i
, SSA_OP_USE
)
3707 bitmap_set_bit (live
, SSA_NAME_VERSION (op
));
3708 FOR_EACH_SSA_TREE_OPERAND (op
, stmt
, i
, SSA_OP_DEF
)
3709 bitmap_clear_bit (live
, SSA_NAME_VERSION (op
));
3712 /* Traverse all PHI nodes in BB, updating live. */
3713 for (gphi_iterator si
= gsi_start_phis (bb
); !gsi_end_p (si
);
3716 use_operand_p arg_p
;
3718 gphi
*phi
= si
.phi ();
3719 tree res
= gimple_phi_result (phi
);
3721 if (virtual_operand_p (res
))
3724 FOR_EACH_PHI_ARG (arg_p
, phi
, i
, SSA_OP_USE
)
3726 tree arg
= USE_FROM_PTR (arg_p
);
3727 if (TREE_CODE (arg
) == SSA_NAME
)
3728 bitmap_set_bit (live
, SSA_NAME_VERSION (arg
));
3731 bitmap_clear_bit (live
, SSA_NAME_VERSION (res
));
3735 /* Do an RPO walk over the function computing SSA name liveness
3736 on-the-fly and deciding on assert expressions to insert. */
3739 find_assert_locations (void)
3741 int *rpo
= XNEWVEC (int, last_basic_block_for_fn (cfun
));
3742 int *bb_rpo
= XNEWVEC (int, last_basic_block_for_fn (cfun
));
3743 int *last_rpo
= XCNEWVEC (int, last_basic_block_for_fn (cfun
));
3746 live
= XCNEWVEC (sbitmap
, last_basic_block_for_fn (cfun
));
3747 rpo_cnt
= pre_and_rev_post_order_compute (NULL
, rpo
, false);
3748 for (i
= 0; i
< rpo_cnt
; ++i
)
3751 /* Pre-seed loop latch liveness from loop header PHI nodes. Due to
3752 the order we compute liveness and insert asserts we otherwise
3753 fail to insert asserts into the loop latch. */
3755 FOR_EACH_LOOP (loop
, 0)
3757 i
= loop
->latch
->index
;
3758 unsigned int j
= single_succ_edge (loop
->latch
)->dest_idx
;
3759 for (gphi_iterator gsi
= gsi_start_phis (loop
->header
);
3760 !gsi_end_p (gsi
); gsi_next (&gsi
))
3762 gphi
*phi
= gsi
.phi ();
3763 if (virtual_operand_p (gimple_phi_result (phi
)))
3765 tree arg
= gimple_phi_arg_def (phi
, j
);
3766 if (TREE_CODE (arg
) == SSA_NAME
)
3768 if (live
[i
] == NULL
)
3770 live
[i
] = sbitmap_alloc (num_ssa_names
);
3771 bitmap_clear (live
[i
]);
3773 bitmap_set_bit (live
[i
], SSA_NAME_VERSION (arg
));
3778 for (i
= rpo_cnt
- 1; i
>= 0; --i
)
3780 basic_block bb
= BASIC_BLOCK_FOR_FN (cfun
, rpo
[i
]);
3786 live
[rpo
[i
]] = sbitmap_alloc (num_ssa_names
);
3787 bitmap_clear (live
[rpo
[i
]]);
3790 /* Process BB and update the live information with uses in
3792 find_assert_locations_1 (bb
, live
[rpo
[i
]]);
3794 /* Merge liveness into the predecessor blocks and free it. */
3795 if (!bitmap_empty_p (live
[rpo
[i
]]))
3798 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
3800 int pred
= e
->src
->index
;
3801 if ((e
->flags
& EDGE_DFS_BACK
) || pred
== ENTRY_BLOCK
)
3806 live
[pred
] = sbitmap_alloc (num_ssa_names
);
3807 bitmap_clear (live
[pred
]);
3809 bitmap_ior (live
[pred
], live
[pred
], live
[rpo
[i
]]);
3811 if (bb_rpo
[pred
] < pred_rpo
)
3812 pred_rpo
= bb_rpo
[pred
];
3815 /* Record the RPO number of the last visited block that needs
3816 live information from this block. */
3817 last_rpo
[rpo
[i
]] = pred_rpo
;
3821 sbitmap_free (live
[rpo
[i
]]);
3822 live
[rpo
[i
]] = NULL
;
3825 /* We can free all successors live bitmaps if all their
3826 predecessors have been visited already. */
3827 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
3828 if (last_rpo
[e
->dest
->index
] == i
3829 && live
[e
->dest
->index
])
3831 sbitmap_free (live
[e
->dest
->index
]);
3832 live
[e
->dest
->index
] = NULL
;
3837 XDELETEVEC (bb_rpo
);
3838 XDELETEVEC (last_rpo
);
3839 for (i
= 0; i
< last_basic_block_for_fn (cfun
); ++i
)
3841 sbitmap_free (live
[i
]);
3845 /* Create an ASSERT_EXPR for NAME and insert it in the location
3846 indicated by LOC. Return true if we made any edge insertions. */
3849 process_assert_insertions_for (tree name
, assert_locus
*loc
)
3851 /* Build the comparison expression NAME_i COMP_CODE VAL. */
3854 gimple
*assert_stmt
;
3858 /* If we have X <=> X do not insert an assert expr for that. */
3859 if (loc
->expr
== loc
->val
)
3862 cond
= build2 (loc
->comp_code
, boolean_type_node
, loc
->expr
, loc
->val
);
3863 assert_stmt
= build_assert_expr_for (cond
, name
);
3866 /* We have been asked to insert the assertion on an edge. This
3867 is used only by COND_EXPR and SWITCH_EXPR assertions. */
3868 gcc_checking_assert (gimple_code (gsi_stmt (loc
->si
)) == GIMPLE_COND
3869 || (gimple_code (gsi_stmt (loc
->si
))
3872 gsi_insert_on_edge (loc
->e
, assert_stmt
);
3876 /* If the stmt iterator points at the end then this is an insertion
3877 at the beginning of a block. */
3878 if (gsi_end_p (loc
->si
))
3880 gimple_stmt_iterator si
= gsi_after_labels (loc
->bb
);
3881 gsi_insert_before (&si
, assert_stmt
, GSI_SAME_STMT
);
3885 /* Otherwise, we can insert right after LOC->SI iff the
3886 statement must not be the last statement in the block. */
3887 stmt
= gsi_stmt (loc
->si
);
3888 if (!stmt_ends_bb_p (stmt
))
3890 gsi_insert_after (&loc
->si
, assert_stmt
, GSI_SAME_STMT
);
3894 /* If STMT must be the last statement in BB, we can only insert new
3895 assertions on the non-abnormal edge out of BB. Note that since
3896 STMT is not control flow, there may only be one non-abnormal/eh edge
3898 FOR_EACH_EDGE (e
, ei
, loc
->bb
->succs
)
3899 if (!(e
->flags
& (EDGE_ABNORMAL
|EDGE_EH
)))
3901 gsi_insert_on_edge (e
, assert_stmt
);
3908 /* Qsort helper for sorting assert locations. If stable is true, don't
3909 use iterative_hash_expr because it can be unstable for -fcompare-debug,
3910 on the other side some pointers might be NULL. */
3912 template <bool stable
>
3914 compare_assert_loc (const void *pa
, const void *pb
)
3916 assert_locus
* const a
= *(assert_locus
* const *)pa
;
3917 assert_locus
* const b
= *(assert_locus
* const *)pb
;
3919 /* If stable, some asserts might be optimized away already, sort
3929 if (a
->e
== NULL
&& b
->e
!= NULL
)
3931 else if (a
->e
!= NULL
&& b
->e
== NULL
)
3934 /* After the above checks, we know that (a->e == NULL) == (b->e == NULL),
3935 no need to test both a->e and b->e. */
3937 /* Sort after destination index. */
3940 else if (a
->e
->dest
->index
> b
->e
->dest
->index
)
3942 else if (a
->e
->dest
->index
< b
->e
->dest
->index
)
3945 /* Sort after comp_code. */
3946 if (a
->comp_code
> b
->comp_code
)
3948 else if (a
->comp_code
< b
->comp_code
)
3953 /* E.g. if a->val is ADDR_EXPR of a VAR_DECL, iterative_hash_expr
3954 uses DECL_UID of the VAR_DECL, so sorting might differ between
3955 -g and -g0. When doing the removal of redundant assert exprs
3956 and commonization to successors, this does not matter, but for
3957 the final sort needs to be stable. */
3965 ha
= iterative_hash_expr (a
->expr
, iterative_hash_expr (a
->val
, 0));
3966 hb
= iterative_hash_expr (b
->expr
, iterative_hash_expr (b
->val
, 0));
3969 /* Break the tie using hashing and source/bb index. */
3971 return (a
->e
!= NULL
3972 ? a
->e
->src
->index
- b
->e
->src
->index
3973 : a
->bb
->index
- b
->bb
->index
);
3974 return ha
> hb
? 1 : -1;
3977 /* Process all the insertions registered for every name N_i registered
3978 in NEED_ASSERT_FOR. The list of assertions to be inserted are
3979 found in ASSERTS_FOR[i]. */
3982 process_assert_insertions (void)
3986 bool update_edges_p
= false;
3987 int num_asserts
= 0;
3989 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3990 dump_all_asserts (dump_file
);
3992 EXECUTE_IF_SET_IN_BITMAP (need_assert_for
, 0, i
, bi
)
3994 assert_locus
*loc
= asserts_for
[i
];
3997 auto_vec
<assert_locus
*, 16> asserts
;
3998 for (; loc
; loc
= loc
->next
)
3999 asserts
.safe_push (loc
);
4000 asserts
.qsort (compare_assert_loc
<false>);
4002 /* Push down common asserts to successors and remove redundant ones. */
4004 assert_locus
*common
= NULL
;
4005 unsigned commonj
= 0;
4006 for (unsigned j
= 0; j
< asserts
.length (); ++j
)
4012 || loc
->e
->dest
!= common
->e
->dest
4013 || loc
->comp_code
!= common
->comp_code
4014 || ! operand_equal_p (loc
->val
, common
->val
, 0)
4015 || ! operand_equal_p (loc
->expr
, common
->expr
, 0))
4021 else if (loc
->e
== asserts
[j
-1]->e
)
4023 /* Remove duplicate asserts. */
4024 if (commonj
== j
- 1)
4029 free (asserts
[j
-1]);
4030 asserts
[j
-1] = NULL
;
4035 if (EDGE_COUNT (common
->e
->dest
->preds
) == ecnt
)
4037 /* We have the same assertion on all incoming edges of a BB.
4038 Insert it at the beginning of that block. */
4039 loc
->bb
= loc
->e
->dest
;
4041 loc
->si
= gsi_none ();
4043 /* Clear asserts commoned. */
4044 for (; commonj
!= j
; ++commonj
)
4045 if (asserts
[commonj
])
4047 free (asserts
[commonj
]);
4048 asserts
[commonj
] = NULL
;
4054 /* The asserts vector sorting above might be unstable for
4055 -fcompare-debug, sort again to ensure a stable sort. */
4056 asserts
.qsort (compare_assert_loc
<true>);
4057 for (unsigned j
= 0; j
< asserts
.length (); ++j
)
4062 update_edges_p
|= process_assert_insertions_for (ssa_name (i
), loc
);
4069 gsi_commit_edge_inserts ();
4071 statistics_counter_event (cfun
, "Number of ASSERT_EXPR expressions inserted",
4076 /* Traverse the flowgraph looking for conditional jumps to insert range
4077 expressions. These range expressions are meant to provide information
4078 to optimizations that need to reason in terms of value ranges. They
4079 will not be expanded into RTL. For instance, given:
4088 this pass will transform the code into:
4094 x = ASSERT_EXPR <x, x < y>
4099 y = ASSERT_EXPR <y, x >= y>
4103 The idea is that once copy and constant propagation have run, other
4104 optimizations will be able to determine what ranges of values can 'x'
4105 take in different paths of the code, simply by checking the reaching
4106 definition of 'x'. */
4109 insert_range_assertions (void)
4111 need_assert_for
= BITMAP_ALLOC (NULL
);
4112 asserts_for
= XCNEWVEC (assert_locus
*, num_ssa_names
);
4114 calculate_dominance_info (CDI_DOMINATORS
);
4116 find_assert_locations ();
4117 if (!bitmap_empty_p (need_assert_for
))
4119 process_assert_insertions ();
4120 update_ssa (TODO_update_ssa_no_phi
);
4123 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4125 fprintf (dump_file
, "\nSSA form after inserting ASSERT_EXPRs\n");
4126 dump_function_to_file (current_function_decl
, dump_file
, dump_flags
);
4130 BITMAP_FREE (need_assert_for
);
4133 class vrp_prop
: public ssa_propagation_engine
4136 enum ssa_prop_result
visit_stmt (gimple
*, edge
*, tree
*) FINAL OVERRIDE
;
4137 enum ssa_prop_result
visit_phi (gphi
*) FINAL OVERRIDE
;
4139 void vrp_initialize (void);
4140 void vrp_finalize (bool);
4141 void check_all_array_refs (void);
4142 void check_array_ref (location_t
, tree
, bool);
4143 void check_mem_ref (location_t
, tree
, bool);
4144 void search_for_addr_array (tree
, location_t
);
4146 class vr_values vr_values
;
4147 /* Temporary delegator to minimize code churn. */
4148 value_range
*get_value_range (const_tree op
)
4149 { return vr_values
.get_value_range (op
); }
4150 void set_defs_to_varying (gimple
*stmt
)
4151 { return vr_values
.set_defs_to_varying (stmt
); }
4152 void extract_range_from_stmt (gimple
*stmt
, edge
*taken_edge_p
,
4153 tree
*output_p
, value_range
*vr
)
4154 { vr_values
.extract_range_from_stmt (stmt
, taken_edge_p
, output_p
, vr
); }
4155 bool update_value_range (const_tree op
, value_range
*vr
)
4156 { return vr_values
.update_value_range (op
, vr
); }
4157 void extract_range_basic (value_range
*vr
, gimple
*stmt
)
4158 { vr_values
.extract_range_basic (vr
, stmt
); }
4159 void extract_range_from_phi_node (gphi
*phi
, value_range
*vr
)
4160 { vr_values
.extract_range_from_phi_node (phi
, vr
); }
4162 /* Checks one ARRAY_REF in REF, located at LOCUS. Ignores flexible arrays
4163 and "struct" hacks. If VRP can determine that the
4164 array subscript is a constant, check if it is outside valid
4165 range. If the array subscript is a RANGE, warn if it is
4166 non-overlapping with valid range.
4167 IGNORE_OFF_BY_ONE is true if the ARRAY_REF is inside a ADDR_EXPR. */
4170 vrp_prop::check_array_ref (location_t location
, tree ref
,
4171 bool ignore_off_by_one
)
4173 const value_range
*vr
= NULL
;
4174 tree low_sub
, up_sub
;
4175 tree low_bound
, up_bound
, up_bound_p1
;
4177 if (TREE_NO_WARNING (ref
))
4180 low_sub
= up_sub
= TREE_OPERAND (ref
, 1);
4181 up_bound
= array_ref_up_bound (ref
);
4184 || TREE_CODE (up_bound
) != INTEGER_CST
4185 || (warn_array_bounds
< 2
4186 && array_at_struct_end_p (ref
)))
4188 /* Accesses to trailing arrays via pointers may access storage
4189 beyond the types array bounds. For such arrays, or for flexible
4190 array members, as well as for other arrays of an unknown size,
4191 replace the upper bound with a more permissive one that assumes
4192 the size of the largest object is PTRDIFF_MAX. */
4193 tree eltsize
= array_ref_element_size (ref
);
4195 if (TREE_CODE (eltsize
) != INTEGER_CST
4196 || integer_zerop (eltsize
))
4198 up_bound
= NULL_TREE
;
4199 up_bound_p1
= NULL_TREE
;
4203 tree maxbound
= TYPE_MAX_VALUE (ptrdiff_type_node
);
4204 tree arg
= TREE_OPERAND (ref
, 0);
4207 if (get_addr_base_and_unit_offset (arg
, &off
) && known_gt (off
, 0))
4208 maxbound
= wide_int_to_tree (sizetype
,
4209 wi::sub (wi::to_wide (maxbound
),
4212 maxbound
= fold_convert (sizetype
, maxbound
);
4214 up_bound_p1
= int_const_binop (TRUNC_DIV_EXPR
, maxbound
, eltsize
);
4216 up_bound
= int_const_binop (MINUS_EXPR
, up_bound_p1
,
4217 build_int_cst (ptrdiff_type_node
, 1));
4221 up_bound_p1
= int_const_binop (PLUS_EXPR
, up_bound
,
4222 build_int_cst (TREE_TYPE (up_bound
), 1));
4224 low_bound
= array_ref_low_bound (ref
);
4226 tree artype
= TREE_TYPE (TREE_OPERAND (ref
, 0));
4228 bool warned
= false;
4231 if (up_bound
&& tree_int_cst_equal (low_bound
, up_bound_p1
))
4232 warned
= warning_at (location
, OPT_Warray_bounds
,
4233 "array subscript %E is above array bounds of %qT",
4236 if (TREE_CODE (low_sub
) == SSA_NAME
)
4238 vr
= get_value_range (low_sub
);
4239 if (vr
->type
== VR_RANGE
|| vr
->type
== VR_ANTI_RANGE
)
4241 low_sub
= vr
->type
== VR_RANGE
? vr
->max
: vr
->min
;
4242 up_sub
= vr
->type
== VR_RANGE
? vr
->min
: vr
->max
;
4246 if (vr
&& vr
->type
== VR_ANTI_RANGE
)
4249 && TREE_CODE (up_sub
) == INTEGER_CST
4250 && (ignore_off_by_one
4251 ? tree_int_cst_lt (up_bound
, up_sub
)
4252 : tree_int_cst_le (up_bound
, up_sub
))
4253 && TREE_CODE (low_sub
) == INTEGER_CST
4254 && tree_int_cst_le (low_sub
, low_bound
))
4255 warned
= warning_at (location
, OPT_Warray_bounds
,
4256 "array subscript [%E, %E] is outside "
4257 "array bounds of %qT",
4258 low_sub
, up_sub
, artype
);
4261 && TREE_CODE (up_sub
) == INTEGER_CST
4262 && (ignore_off_by_one
4263 ? !tree_int_cst_le (up_sub
, up_bound_p1
)
4264 : !tree_int_cst_le (up_sub
, up_bound
)))
4266 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4268 fprintf (dump_file
, "Array bound warning for ");
4269 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, ref
);
4270 fprintf (dump_file
, "\n");
4272 warned
= warning_at (location
, OPT_Warray_bounds
,
4273 "array subscript %E is above array bounds of %qT",
4276 else if (TREE_CODE (low_sub
) == INTEGER_CST
4277 && tree_int_cst_lt (low_sub
, low_bound
))
4279 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4281 fprintf (dump_file
, "Array bound warning for ");
4282 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, ref
);
4283 fprintf (dump_file
, "\n");
4285 warned
= warning_at (location
, OPT_Warray_bounds
,
4286 "array subscript %E is below array bounds of %qT",
4292 ref
= TREE_OPERAND (ref
, 0);
4295 inform (DECL_SOURCE_LOCATION (ref
), "while referencing %qD", ref
);
4297 TREE_NO_WARNING (ref
) = 1;
4301 /* Checks one MEM_REF in REF, located at LOCATION, for out-of-bounds
4302 references to string constants. If VRP can determine that the array
4303 subscript is a constant, check if it is outside valid range.
4304 If the array subscript is a RANGE, warn if it is non-overlapping
4306 IGNORE_OFF_BY_ONE is true if the MEM_REF is inside an ADDR_EXPR
4307 (used to allow one-past-the-end indices for code that takes
4308 the address of the just-past-the-end element of an array). */
4311 vrp_prop::check_mem_ref (location_t location
, tree ref
,
4312 bool ignore_off_by_one
)
4314 if (TREE_NO_WARNING (ref
))
4317 tree arg
= TREE_OPERAND (ref
, 0);
4318 /* The constant and variable offset of the reference. */
4319 tree cstoff
= TREE_OPERAND (ref
, 1);
4320 tree varoff
= NULL_TREE
;
4322 const offset_int maxobjsize
= tree_to_shwi (max_object_size ());
4324 /* The array or string constant bounds in bytes. Initially set
4325 to [-MAXOBJSIZE - 1, MAXOBJSIZE] until a tighter bound is
4327 offset_int arrbounds
[2] = { -maxobjsize
- 1, maxobjsize
};
4329 /* The minimum and maximum intermediate offset. For a reference
4330 to be valid, not only does the final offset/subscript must be
4331 in bounds but all intermediate offsets should be as well.
4332 GCC may be able to deal gracefully with such out-of-bounds
4333 offsets so the checking is only enbaled at -Warray-bounds=2
4334 where it may help detect bugs in uses of the intermediate
4335 offsets that could otherwise not be detectable. */
4336 offset_int ioff
= wi::to_offset (fold_convert (ptrdiff_type_node
, cstoff
));
4337 offset_int extrema
[2] = { 0, wi::abs (ioff
) };
4339 /* The range of the byte offset into the reference. */
4340 offset_int offrange
[2] = { 0, 0 };
4342 const value_range
*vr
= NULL
;
4344 /* Determine the offsets and increment OFFRANGE for the bounds of each.
4345 The loop computes the the range of the final offset for expressions
4346 such as (A + i0 + ... + iN)[CSTOFF] where i0 through iN are SSA_NAMEs
4348 while (TREE_CODE (arg
) == SSA_NAME
)
4350 gimple
*def
= SSA_NAME_DEF_STMT (arg
);
4351 if (!is_gimple_assign (def
))
4354 tree_code code
= gimple_assign_rhs_code (def
);
4355 if (code
== POINTER_PLUS_EXPR
)
4357 arg
= gimple_assign_rhs1 (def
);
4358 varoff
= gimple_assign_rhs2 (def
);
4360 else if (code
== ASSERT_EXPR
)
4362 arg
= TREE_OPERAND (gimple_assign_rhs1 (def
), 0);
4368 /* VAROFF should always be a SSA_NAME here (and not even
4369 INTEGER_CST) but there's no point in taking chances. */
4370 if (TREE_CODE (varoff
) != SSA_NAME
)
4373 vr
= get_value_range (varoff
);
4374 if (!vr
|| vr
->type
== VR_UNDEFINED
|| !vr
->min
|| !vr
->max
)
4377 if (TREE_CODE (vr
->min
) != INTEGER_CST
4378 || TREE_CODE (vr
->max
) != INTEGER_CST
)
4381 if (vr
->type
== VR_RANGE
)
4383 if (tree_int_cst_lt (vr
->min
, vr
->max
))
4386 = wi::to_offset (fold_convert (ptrdiff_type_node
, vr
->min
));
4388 = wi::to_offset (fold_convert (ptrdiff_type_node
, vr
->max
));
4402 /* Conservatively add [-MAXOBJSIZE -1, MAXOBJSIZE]
4404 offrange
[0] += arrbounds
[0];
4405 offrange
[1] += arrbounds
[1];
4410 /* For an anti-range, analogously to the above, conservatively
4411 add [-MAXOBJSIZE -1, MAXOBJSIZE] to OFFRANGE. */
4412 offrange
[0] += arrbounds
[0];
4413 offrange
[1] += arrbounds
[1];
4416 /* Keep track of the minimum and maximum offset. */
4417 if (offrange
[1] < 0 && offrange
[1] < extrema
[0])
4418 extrema
[0] = offrange
[1];
4419 if (offrange
[0] > 0 && offrange
[0] > extrema
[1])
4420 extrema
[1] = offrange
[0];
4422 if (offrange
[0] < arrbounds
[0])
4423 offrange
[0] = arrbounds
[0];
4425 if (offrange
[1] > arrbounds
[1])
4426 offrange
[1] = arrbounds
[1];
4429 if (TREE_CODE (arg
) == ADDR_EXPR
)
4431 arg
= TREE_OPERAND (arg
, 0);
4432 if (TREE_CODE (arg
) != STRING_CST
4433 && TREE_CODE (arg
) != VAR_DECL
)
4439 /* The type of the object being referred to. It can be an array,
4440 string literal, or a non-array type when the MEM_REF represents
4441 a reference/subscript via a pointer to an object that is not
4442 an element of an array. References to members of structs and
4443 unions are excluded because MEM_REF doesn't make it possible
4444 to identify the member where the reference originated.
4445 Incomplete types are excluded as well because their size is
4447 tree reftype
= TREE_TYPE (arg
);
4448 if (POINTER_TYPE_P (reftype
)
4449 || !COMPLETE_TYPE_P (reftype
)
4450 || TREE_CODE (TYPE_SIZE_UNIT (reftype
)) != INTEGER_CST
4451 || RECORD_OR_UNION_TYPE_P (reftype
))
4455 if (TREE_CODE (reftype
) == ARRAY_TYPE
)
4457 eltsize
= wi::to_offset (TYPE_SIZE_UNIT (TREE_TYPE (reftype
)));
4459 if (tree dom
= TYPE_DOMAIN (reftype
))
4461 tree bnds
[] = { TYPE_MIN_VALUE (dom
), TYPE_MAX_VALUE (dom
) };
4462 if (array_at_struct_end_p (arg
)
4463 || !bnds
[0] || !bnds
[1])
4466 arrbounds
[1] = wi::lrshift (maxobjsize
, wi::floor_log2 (eltsize
));
4470 arrbounds
[0] = wi::to_offset (bnds
[0]) * eltsize
;
4471 arrbounds
[1] = (wi::to_offset (bnds
[1]) + 1) * eltsize
;
4477 arrbounds
[1] = wi::lrshift (maxobjsize
, wi::floor_log2 (eltsize
));
4480 if (TREE_CODE (ref
) == MEM_REF
)
4482 /* For MEM_REF determine a tighter bound of the non-array
4484 tree eltype
= TREE_TYPE (reftype
);
4485 while (TREE_CODE (eltype
) == ARRAY_TYPE
)
4486 eltype
= TREE_TYPE (eltype
);
4487 eltsize
= wi::to_offset (TYPE_SIZE_UNIT (eltype
));
4494 arrbounds
[1] = wi::to_offset (TYPE_SIZE_UNIT (reftype
));
4497 offrange
[0] += ioff
;
4498 offrange
[1] += ioff
;
4500 /* Compute the more permissive upper bound when IGNORE_OFF_BY_ONE
4501 is set (when taking the address of the one-past-last element
4502 of an array) but always use the stricter bound in diagnostics. */
4503 offset_int ubound
= arrbounds
[1];
4504 if (ignore_off_by_one
)
4507 if (offrange
[0] >= ubound
|| offrange
[1] < arrbounds
[0])
4509 /* Treat a reference to a non-array object as one to an array
4510 of a single element. */
4511 if (TREE_CODE (reftype
) != ARRAY_TYPE
)
4512 reftype
= build_array_type_nelts (reftype
, 1);
4514 if (TREE_CODE (ref
) == MEM_REF
)
4516 /* Extract the element type out of MEM_REF and use its size
4517 to compute the index to print in the diagnostic; arrays
4518 in MEM_REF don't mean anything. */
4519 tree type
= TREE_TYPE (ref
);
4520 while (TREE_CODE (type
) == ARRAY_TYPE
)
4521 type
= TREE_TYPE (type
);
4522 tree size
= TYPE_SIZE_UNIT (type
);
4523 offrange
[0] = offrange
[0] / wi::to_offset (size
);
4524 offrange
[1] = offrange
[1] / wi::to_offset (size
);
4528 /* For anything other than MEM_REF, compute the index to
4529 print in the diagnostic as the offset over element size. */
4530 offrange
[0] = offrange
[0] / eltsize
;
4531 offrange
[1] = offrange
[1] / eltsize
;
4535 if (offrange
[0] == offrange
[1])
4536 warned
= warning_at (location
, OPT_Warray_bounds
,
4537 "array subscript %wi is outside array bounds "
4539 offrange
[0].to_shwi (), reftype
);
4541 warned
= warning_at (location
, OPT_Warray_bounds
,
4542 "array subscript [%wi, %wi] is outside "
4543 "array bounds of %qT",
4544 offrange
[0].to_shwi (),
4545 offrange
[1].to_shwi (), reftype
);
4546 if (warned
&& DECL_P (arg
))
4547 inform (DECL_SOURCE_LOCATION (arg
), "while referencing %qD", arg
);
4549 TREE_NO_WARNING (ref
) = 1;
4553 if (warn_array_bounds
< 2)
4556 /* At level 2 check also intermediate offsets. */
4558 if (extrema
[i
] < -arrbounds
[1] || extrema
[i
= 1] > ubound
)
4560 HOST_WIDE_INT tmpidx
= extrema
[i
].to_shwi () / eltsize
.to_shwi ();
4562 warning_at (location
, OPT_Warray_bounds
,
4563 "intermediate array offset %wi is outside array bounds "
4566 TREE_NO_WARNING (ref
) = 1;
4570 /* Searches if the expr T, located at LOCATION computes
4571 address of an ARRAY_REF, and call check_array_ref on it. */
4574 vrp_prop::search_for_addr_array (tree t
, location_t location
)
4576 /* Check each ARRAY_REF and MEM_REF in the reference chain. */
4579 if (TREE_CODE (t
) == ARRAY_REF
)
4580 check_array_ref (location
, t
, true /*ignore_off_by_one*/);
4581 else if (TREE_CODE (t
) == MEM_REF
)
4582 check_mem_ref (location
, t
, true /*ignore_off_by_one*/);
4584 t
= TREE_OPERAND (t
, 0);
4586 while (handled_component_p (t
) || TREE_CODE (t
) == MEM_REF
);
4588 if (TREE_CODE (t
) != MEM_REF
4589 || TREE_CODE (TREE_OPERAND (t
, 0)) != ADDR_EXPR
4590 || TREE_NO_WARNING (t
))
4593 tree tem
= TREE_OPERAND (TREE_OPERAND (t
, 0), 0);
4594 tree low_bound
, up_bound
, el_sz
;
4595 if (TREE_CODE (TREE_TYPE (tem
)) != ARRAY_TYPE
4596 || TREE_CODE (TREE_TYPE (TREE_TYPE (tem
))) == ARRAY_TYPE
4597 || !TYPE_DOMAIN (TREE_TYPE (tem
)))
4600 low_bound
= TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (tem
)));
4601 up_bound
= TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (tem
)));
4602 el_sz
= TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (tem
)));
4604 || TREE_CODE (low_bound
) != INTEGER_CST
4606 || TREE_CODE (up_bound
) != INTEGER_CST
4608 || TREE_CODE (el_sz
) != INTEGER_CST
)
4612 if (!mem_ref_offset (t
).is_constant (&idx
))
4615 bool warned
= false;
4616 idx
= wi::sdiv_trunc (idx
, wi::to_offset (el_sz
));
4619 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4621 fprintf (dump_file
, "Array bound warning for ");
4622 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, t
);
4623 fprintf (dump_file
, "\n");
4625 warned
= warning_at (location
, OPT_Warray_bounds
,
4626 "array subscript %wi is below "
4627 "array bounds of %qT",
4628 idx
.to_shwi (), TREE_TYPE (tem
));
4630 else if (idx
> (wi::to_offset (up_bound
)
4631 - wi::to_offset (low_bound
) + 1))
4633 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4635 fprintf (dump_file
, "Array bound warning for ");
4636 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, t
);
4637 fprintf (dump_file
, "\n");
4639 warned
= warning_at (location
, OPT_Warray_bounds
,
4640 "array subscript %wu is above "
4641 "array bounds of %qT",
4642 idx
.to_uhwi (), TREE_TYPE (tem
));
4648 inform (DECL_SOURCE_LOCATION (t
), "while referencing %qD", t
);
4650 TREE_NO_WARNING (t
) = 1;
4654 /* walk_tree() callback that checks if *TP is
4655 an ARRAY_REF inside an ADDR_EXPR (in which an array
4656 subscript one outside the valid range is allowed). Call
4657 check_array_ref for each ARRAY_REF found. The location is
4661 check_array_bounds (tree
*tp
, int *walk_subtree
, void *data
)
4664 struct walk_stmt_info
*wi
= (struct walk_stmt_info
*) data
;
4665 location_t location
;
4667 if (EXPR_HAS_LOCATION (t
))
4668 location
= EXPR_LOCATION (t
);
4670 location
= gimple_location (wi
->stmt
);
4672 *walk_subtree
= TRUE
;
4674 vrp_prop
*vrp_prop
= (class vrp_prop
*)wi
->info
;
4675 if (TREE_CODE (t
) == ARRAY_REF
)
4676 vrp_prop
->check_array_ref (location
, t
, false /*ignore_off_by_one*/);
4677 else if (TREE_CODE (t
) == MEM_REF
)
4678 vrp_prop
->check_mem_ref (location
, t
, false /*ignore_off_by_one*/);
4679 else if (TREE_CODE (t
) == ADDR_EXPR
)
4681 vrp_prop
->search_for_addr_array (t
, location
);
4682 *walk_subtree
= FALSE
;
4688 /* A dom_walker subclass for use by vrp_prop::check_all_array_refs,
4689 to walk over all statements of all reachable BBs and call
4690 check_array_bounds on them. */
4692 class check_array_bounds_dom_walker
: public dom_walker
4695 check_array_bounds_dom_walker (vrp_prop
*prop
)
4696 : dom_walker (CDI_DOMINATORS
,
4697 /* Discover non-executable edges, preserving EDGE_EXECUTABLE
4698 flags, so that we can merge in information on
4699 non-executable edges from vrp_folder . */
4700 REACHABLE_BLOCKS_PRESERVING_FLAGS
),
4702 ~check_array_bounds_dom_walker () {}
4704 edge
before_dom_children (basic_block
) FINAL OVERRIDE
;
4710 /* Implementation of dom_walker::before_dom_children.
4712 Walk over all statements of BB and call check_array_bounds on them,
4713 and determine if there's a unique successor edge. */
4716 check_array_bounds_dom_walker::before_dom_children (basic_block bb
)
4718 gimple_stmt_iterator si
;
4719 for (si
= gsi_start_bb (bb
); !gsi_end_p (si
); gsi_next (&si
))
4721 gimple
*stmt
= gsi_stmt (si
);
4722 struct walk_stmt_info wi
;
4723 if (!gimple_has_location (stmt
)
4724 || is_gimple_debug (stmt
))
4727 memset (&wi
, 0, sizeof (wi
));
4731 walk_gimple_op (stmt
, check_array_bounds
, &wi
);
4734 /* Determine if there's a unique successor edge, and if so, return
4735 that back to dom_walker, ensuring that we don't visit blocks that
4736 became unreachable during the VRP propagation
4737 (PR tree-optimization/83312). */
4738 return find_taken_edge (bb
, NULL_TREE
);
4741 /* Walk over all statements of all reachable BBs and call check_array_bounds
4745 vrp_prop::check_all_array_refs ()
4747 check_array_bounds_dom_walker
w (this);
4748 w
.walk (ENTRY_BLOCK_PTR_FOR_FN (cfun
));
4751 /* Return true if all imm uses of VAR are either in STMT, or
4752 feed (optionally through a chain of single imm uses) GIMPLE_COND
4753 in basic block COND_BB. */
4756 all_imm_uses_in_stmt_or_feed_cond (tree var
, gimple
*stmt
, basic_block cond_bb
)
4758 use_operand_p use_p
, use2_p
;
4759 imm_use_iterator iter
;
4761 FOR_EACH_IMM_USE_FAST (use_p
, iter
, var
)
4762 if (USE_STMT (use_p
) != stmt
)
4764 gimple
*use_stmt
= USE_STMT (use_p
), *use_stmt2
;
4765 if (is_gimple_debug (use_stmt
))
4767 while (is_gimple_assign (use_stmt
)
4768 && TREE_CODE (gimple_assign_lhs (use_stmt
)) == SSA_NAME
4769 && single_imm_use (gimple_assign_lhs (use_stmt
),
4770 &use2_p
, &use_stmt2
))
4771 use_stmt
= use_stmt2
;
4772 if (gimple_code (use_stmt
) != GIMPLE_COND
4773 || gimple_bb (use_stmt
) != cond_bb
)
4786 __builtin_unreachable ();
4788 x_5 = ASSERT_EXPR <x_3, ...>;
4789 If x_3 has no other immediate uses (checked by caller),
4790 var is the x_3 var from ASSERT_EXPR, we can clear low 5 bits
4791 from the non-zero bitmask. */
4794 maybe_set_nonzero_bits (edge e
, tree var
)
4796 basic_block cond_bb
= e
->src
;
4797 gimple
*stmt
= last_stmt (cond_bb
);
4801 || gimple_code (stmt
) != GIMPLE_COND
4802 || gimple_cond_code (stmt
) != ((e
->flags
& EDGE_TRUE_VALUE
)
4803 ? EQ_EXPR
: NE_EXPR
)
4804 || TREE_CODE (gimple_cond_lhs (stmt
)) != SSA_NAME
4805 || !integer_zerop (gimple_cond_rhs (stmt
)))
4808 stmt
= SSA_NAME_DEF_STMT (gimple_cond_lhs (stmt
));
4809 if (!is_gimple_assign (stmt
)
4810 || gimple_assign_rhs_code (stmt
) != BIT_AND_EXPR
4811 || TREE_CODE (gimple_assign_rhs2 (stmt
)) != INTEGER_CST
)
4813 if (gimple_assign_rhs1 (stmt
) != var
)
4817 if (TREE_CODE (gimple_assign_rhs1 (stmt
)) != SSA_NAME
)
4819 stmt2
= SSA_NAME_DEF_STMT (gimple_assign_rhs1 (stmt
));
4820 if (!gimple_assign_cast_p (stmt2
)
4821 || gimple_assign_rhs1 (stmt2
) != var
4822 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (stmt2
))
4823 || (TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (stmt
)))
4824 != TYPE_PRECISION (TREE_TYPE (var
))))
4827 cst
= gimple_assign_rhs2 (stmt
);
4828 set_nonzero_bits (var
, wi::bit_and_not (get_nonzero_bits (var
),
4829 wi::to_wide (cst
)));
4832 /* Convert range assertion expressions into the implied copies and
4833 copy propagate away the copies. Doing the trivial copy propagation
4834 here avoids the need to run the full copy propagation pass after
4837 FIXME, this will eventually lead to copy propagation removing the
4838 names that had useful range information attached to them. For
4839 instance, if we had the assertion N_i = ASSERT_EXPR <N_j, N_j > 3>,
4840 then N_i will have the range [3, +INF].
4842 However, by converting the assertion into the implied copy
4843 operation N_i = N_j, we will then copy-propagate N_j into the uses
4844 of N_i and lose the range information. We may want to hold on to
4845 ASSERT_EXPRs a little while longer as the ranges could be used in
4846 things like jump threading.
4848 The problem with keeping ASSERT_EXPRs around is that passes after
4849 VRP need to handle them appropriately.
4851 Another approach would be to make the range information a first
4852 class property of the SSA_NAME so that it can be queried from
4853 any pass. This is made somewhat more complex by the need for
4854 multiple ranges to be associated with one SSA_NAME. */
4857 remove_range_assertions (void)
4860 gimple_stmt_iterator si
;
4861 /* 1 if looking at ASSERT_EXPRs immediately at the beginning of
4862 a basic block preceeded by GIMPLE_COND branching to it and
4863 __builtin_trap, -1 if not yet checked, 0 otherwise. */
4866 /* Note that the BSI iterator bump happens at the bottom of the
4867 loop and no bump is necessary if we're removing the statement
4868 referenced by the current BSI. */
4869 FOR_EACH_BB_FN (bb
, cfun
)
4870 for (si
= gsi_after_labels (bb
), is_unreachable
= -1; !gsi_end_p (si
);)
4872 gimple
*stmt
= gsi_stmt (si
);
4874 if (is_gimple_assign (stmt
)
4875 && gimple_assign_rhs_code (stmt
) == ASSERT_EXPR
)
4877 tree lhs
= gimple_assign_lhs (stmt
);
4878 tree rhs
= gimple_assign_rhs1 (stmt
);
4881 var
= ASSERT_EXPR_VAR (rhs
);
4883 if (TREE_CODE (var
) == SSA_NAME
4884 && !POINTER_TYPE_P (TREE_TYPE (lhs
))
4885 && SSA_NAME_RANGE_INFO (lhs
))
4887 if (is_unreachable
== -1)
4890 if (single_pred_p (bb
)
4891 && assert_unreachable_fallthru_edge_p
4892 (single_pred_edge (bb
)))
4896 if (x_7 >= 10 && x_7 < 20)
4897 __builtin_unreachable ();
4898 x_8 = ASSERT_EXPR <x_7, ...>;
4899 if the only uses of x_7 are in the ASSERT_EXPR and
4900 in the condition. In that case, we can copy the
4901 range info from x_8 computed in this pass also
4904 && all_imm_uses_in_stmt_or_feed_cond (var
, stmt
,
4907 set_range_info (var
, SSA_NAME_RANGE_TYPE (lhs
),
4908 SSA_NAME_RANGE_INFO (lhs
)->get_min (),
4909 SSA_NAME_RANGE_INFO (lhs
)->get_max ());
4910 maybe_set_nonzero_bits (single_pred_edge (bb
), var
);
4914 /* Propagate the RHS into every use of the LHS. For SSA names
4915 also propagate abnormals as it merely restores the original
4916 IL in this case (an replace_uses_by would assert). */
4917 if (TREE_CODE (var
) == SSA_NAME
)
4919 imm_use_iterator iter
;
4920 use_operand_p use_p
;
4922 FOR_EACH_IMM_USE_STMT (use_stmt
, iter
, lhs
)
4923 FOR_EACH_IMM_USE_ON_STMT (use_p
, iter
)
4924 SET_USE (use_p
, var
);
4927 replace_uses_by (lhs
, var
);
4929 /* And finally, remove the copy, it is not needed. */
4930 gsi_remove (&si
, true);
4931 release_defs (stmt
);
4935 if (!is_gimple_debug (gsi_stmt (si
)))
4942 /* Return true if STMT is interesting for VRP. */
4945 stmt_interesting_for_vrp (gimple
*stmt
)
4947 if (gimple_code (stmt
) == GIMPLE_PHI
)
4949 tree res
= gimple_phi_result (stmt
);
4950 return (!virtual_operand_p (res
)
4951 && (INTEGRAL_TYPE_P (TREE_TYPE (res
))
4952 || POINTER_TYPE_P (TREE_TYPE (res
))));
4954 else if (is_gimple_assign (stmt
) || is_gimple_call (stmt
))
4956 tree lhs
= gimple_get_lhs (stmt
);
4958 /* In general, assignments with virtual operands are not useful
4959 for deriving ranges, with the obvious exception of calls to
4960 builtin functions. */
4961 if (lhs
&& TREE_CODE (lhs
) == SSA_NAME
4962 && (INTEGRAL_TYPE_P (TREE_TYPE (lhs
))
4963 || POINTER_TYPE_P (TREE_TYPE (lhs
)))
4964 && (is_gimple_call (stmt
)
4965 || !gimple_vuse (stmt
)))
4967 else if (is_gimple_call (stmt
) && gimple_call_internal_p (stmt
))
4968 switch (gimple_call_internal_fn (stmt
))
4970 case IFN_ADD_OVERFLOW
:
4971 case IFN_SUB_OVERFLOW
:
4972 case IFN_MUL_OVERFLOW
:
4973 case IFN_ATOMIC_COMPARE_EXCHANGE
:
4974 /* These internal calls return _Complex integer type,
4975 but are interesting to VRP nevertheless. */
4976 if (lhs
&& TREE_CODE (lhs
) == SSA_NAME
)
4983 else if (gimple_code (stmt
) == GIMPLE_COND
4984 || gimple_code (stmt
) == GIMPLE_SWITCH
)
4990 /* Initialization required by ssa_propagate engine. */
4993 vrp_prop::vrp_initialize ()
4997 FOR_EACH_BB_FN (bb
, cfun
)
4999 for (gphi_iterator si
= gsi_start_phis (bb
); !gsi_end_p (si
);
5002 gphi
*phi
= si
.phi ();
5003 if (!stmt_interesting_for_vrp (phi
))
5005 tree lhs
= PHI_RESULT (phi
);
5006 set_value_range_to_varying (get_value_range (lhs
));
5007 prop_set_simulate_again (phi
, false);
5010 prop_set_simulate_again (phi
, true);
5013 for (gimple_stmt_iterator si
= gsi_start_bb (bb
); !gsi_end_p (si
);
5016 gimple
*stmt
= gsi_stmt (si
);
5018 /* If the statement is a control insn, then we do not
5019 want to avoid simulating the statement once. Failure
5020 to do so means that those edges will never get added. */
5021 if (stmt_ends_bb_p (stmt
))
5022 prop_set_simulate_again (stmt
, true);
5023 else if (!stmt_interesting_for_vrp (stmt
))
5025 set_defs_to_varying (stmt
);
5026 prop_set_simulate_again (stmt
, false);
5029 prop_set_simulate_again (stmt
, true);
5034 /* Searches the case label vector VEC for the index *IDX of the CASE_LABEL
5035 that includes the value VAL. The search is restricted to the range
5036 [START_IDX, n - 1] where n is the size of VEC.
5038 If there is a CASE_LABEL for VAL, its index is placed in IDX and true is
5041 If there is no CASE_LABEL for VAL and there is one that is larger than VAL,
5042 it is placed in IDX and false is returned.
5044 If VAL is larger than any CASE_LABEL, n is placed on IDX and false is
5048 find_case_label_index (gswitch
*stmt
, size_t start_idx
, tree val
, size_t *idx
)
5050 size_t n
= gimple_switch_num_labels (stmt
);
5053 /* Find case label for minimum of the value range or the next one.
5054 At each iteration we are searching in [low, high - 1]. */
5056 for (low
= start_idx
, high
= n
; high
!= low
; )
5060 /* Note that i != high, so we never ask for n. */
5061 size_t i
= (high
+ low
) / 2;
5062 t
= gimple_switch_label (stmt
, i
);
5064 /* Cache the result of comparing CASE_LOW and val. */
5065 cmp
= tree_int_cst_compare (CASE_LOW (t
), val
);
5069 /* Ranges cannot be empty. */
5078 if (CASE_HIGH (t
) != NULL
5079 && tree_int_cst_compare (CASE_HIGH (t
), val
) >= 0)
5091 /* Searches the case label vector VEC for the range of CASE_LABELs that is used
5092 for values between MIN and MAX. The first index is placed in MIN_IDX. The
5093 last index is placed in MAX_IDX. If the range of CASE_LABELs is empty
5094 then MAX_IDX < MIN_IDX.
5095 Returns true if the default label is not needed. */
5098 find_case_label_range (gswitch
*stmt
, tree min
, tree max
, size_t *min_idx
,
5102 bool min_take_default
= !find_case_label_index (stmt
, 1, min
, &i
);
5103 bool max_take_default
= !find_case_label_index (stmt
, i
, max
, &j
);
5107 && max_take_default
)
5109 /* Only the default case label reached.
5110 Return an empty range. */
5117 bool take_default
= min_take_default
|| max_take_default
;
5121 if (max_take_default
)
5124 /* If the case label range is continuous, we do not need
5125 the default case label. Verify that. */
5126 high
= CASE_LOW (gimple_switch_label (stmt
, i
));
5127 if (CASE_HIGH (gimple_switch_label (stmt
, i
)))
5128 high
= CASE_HIGH (gimple_switch_label (stmt
, i
));
5129 for (k
= i
+ 1; k
<= j
; ++k
)
5131 low
= CASE_LOW (gimple_switch_label (stmt
, k
));
5132 if (!integer_onep (int_const_binop (MINUS_EXPR
, low
, high
)))
5134 take_default
= true;
5138 if (CASE_HIGH (gimple_switch_label (stmt
, k
)))
5139 high
= CASE_HIGH (gimple_switch_label (stmt
, k
));
5144 return !take_default
;
5148 /* Evaluate statement STMT. If the statement produces a useful range,
5149 return SSA_PROP_INTERESTING and record the SSA name with the
5150 interesting range into *OUTPUT_P.
5152 If STMT is a conditional branch and we can determine its truth
5153 value, the taken edge is recorded in *TAKEN_EDGE_P.
5155 If STMT produces a varying value, return SSA_PROP_VARYING. */
5157 enum ssa_prop_result
5158 vrp_prop::visit_stmt (gimple
*stmt
, edge
*taken_edge_p
, tree
*output_p
)
5160 value_range vr
= VR_INITIALIZER
;
5161 tree lhs
= gimple_get_lhs (stmt
);
5162 extract_range_from_stmt (stmt
, taken_edge_p
, output_p
, &vr
);
5166 if (update_value_range (*output_p
, &vr
))
5168 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
5170 fprintf (dump_file
, "Found new range for ");
5171 print_generic_expr (dump_file
, *output_p
);
5172 fprintf (dump_file
, ": ");
5173 dump_value_range (dump_file
, &vr
);
5174 fprintf (dump_file
, "\n");
5177 if (vr
.type
== VR_VARYING
)
5178 return SSA_PROP_VARYING
;
5180 return SSA_PROP_INTERESTING
;
5182 return SSA_PROP_NOT_INTERESTING
;
5185 if (is_gimple_call (stmt
) && gimple_call_internal_p (stmt
))
5186 switch (gimple_call_internal_fn (stmt
))
5188 case IFN_ADD_OVERFLOW
:
5189 case IFN_SUB_OVERFLOW
:
5190 case IFN_MUL_OVERFLOW
:
5191 case IFN_ATOMIC_COMPARE_EXCHANGE
:
5192 /* These internal calls return _Complex integer type,
5193 which VRP does not track, but the immediate uses
5194 thereof might be interesting. */
5195 if (lhs
&& TREE_CODE (lhs
) == SSA_NAME
)
5197 imm_use_iterator iter
;
5198 use_operand_p use_p
;
5199 enum ssa_prop_result res
= SSA_PROP_VARYING
;
5201 set_value_range_to_varying (get_value_range (lhs
));
5203 FOR_EACH_IMM_USE_FAST (use_p
, iter
, lhs
)
5205 gimple
*use_stmt
= USE_STMT (use_p
);
5206 if (!is_gimple_assign (use_stmt
))
5208 enum tree_code rhs_code
= gimple_assign_rhs_code (use_stmt
);
5209 if (rhs_code
!= REALPART_EXPR
&& rhs_code
!= IMAGPART_EXPR
)
5211 tree rhs1
= gimple_assign_rhs1 (use_stmt
);
5212 tree use_lhs
= gimple_assign_lhs (use_stmt
);
5213 if (TREE_CODE (rhs1
) != rhs_code
5214 || TREE_OPERAND (rhs1
, 0) != lhs
5215 || TREE_CODE (use_lhs
) != SSA_NAME
5216 || !stmt_interesting_for_vrp (use_stmt
)
5217 || (!INTEGRAL_TYPE_P (TREE_TYPE (use_lhs
))
5218 || !TYPE_MIN_VALUE (TREE_TYPE (use_lhs
))
5219 || !TYPE_MAX_VALUE (TREE_TYPE (use_lhs
))))
5222 /* If there is a change in the value range for any of the
5223 REALPART_EXPR/IMAGPART_EXPR immediate uses, return
5224 SSA_PROP_INTERESTING. If there are any REALPART_EXPR
5225 or IMAGPART_EXPR immediate uses, but none of them have
5226 a change in their value ranges, return
5227 SSA_PROP_NOT_INTERESTING. If there are no
5228 {REAL,IMAG}PART_EXPR uses at all,
5229 return SSA_PROP_VARYING. */
5230 value_range new_vr
= VR_INITIALIZER
;
5231 extract_range_basic (&new_vr
, use_stmt
);
5232 const value_range
*old_vr
= get_value_range (use_lhs
);
5233 if (old_vr
->type
!= new_vr
.type
5234 || !vrp_operand_equal_p (old_vr
->min
, new_vr
.min
)
5235 || !vrp_operand_equal_p (old_vr
->max
, new_vr
.max
)
5236 || !vrp_bitmap_equal_p (old_vr
->equiv
, new_vr
.equiv
))
5237 res
= SSA_PROP_INTERESTING
;
5239 res
= SSA_PROP_NOT_INTERESTING
;
5240 BITMAP_FREE (new_vr
.equiv
);
5241 if (res
== SSA_PROP_INTERESTING
)
5255 /* All other statements produce nothing of interest for VRP, so mark
5256 their outputs varying and prevent further simulation. */
5257 set_defs_to_varying (stmt
);
5259 return (*taken_edge_p
) ? SSA_PROP_INTERESTING
: SSA_PROP_VARYING
;
5262 /* Union the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
5263 { VR1TYPE, VR0MIN, VR0MAX } and store the result
5264 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
5265 possible such range. The resulting range is not canonicalized. */
5268 union_ranges (enum value_range_type
*vr0type
,
5269 tree
*vr0min
, tree
*vr0max
,
5270 enum value_range_type vr1type
,
5271 tree vr1min
, tree vr1max
)
5273 bool mineq
= vrp_operand_equal_p (*vr0min
, vr1min
);
5274 bool maxeq
= vrp_operand_equal_p (*vr0max
, vr1max
);
5276 /* [] is vr0, () is vr1 in the following classification comments. */
5280 if (*vr0type
== vr1type
)
5281 /* Nothing to do for equal ranges. */
5283 else if ((*vr0type
== VR_RANGE
5284 && vr1type
== VR_ANTI_RANGE
)
5285 || (*vr0type
== VR_ANTI_RANGE
5286 && vr1type
== VR_RANGE
))
5288 /* For anti-range with range union the result is varying. */
5294 else if (operand_less_p (*vr0max
, vr1min
) == 1
5295 || operand_less_p (vr1max
, *vr0min
) == 1)
5297 /* [ ] ( ) or ( ) [ ]
5298 If the ranges have an empty intersection, result of the union
5299 operation is the anti-range or if both are anti-ranges
5301 if (*vr0type
== VR_ANTI_RANGE
5302 && vr1type
== VR_ANTI_RANGE
)
5304 else if (*vr0type
== VR_ANTI_RANGE
5305 && vr1type
== VR_RANGE
)
5307 else if (*vr0type
== VR_RANGE
5308 && vr1type
== VR_ANTI_RANGE
)
5314 else if (*vr0type
== VR_RANGE
5315 && vr1type
== VR_RANGE
)
5317 /* The result is the convex hull of both ranges. */
5318 if (operand_less_p (*vr0max
, vr1min
) == 1)
5320 /* If the result can be an anti-range, create one. */
5321 if (TREE_CODE (*vr0max
) == INTEGER_CST
5322 && TREE_CODE (vr1min
) == INTEGER_CST
5323 && vrp_val_is_min (*vr0min
)
5324 && vrp_val_is_max (vr1max
))
5326 tree min
= int_const_binop (PLUS_EXPR
,
5328 build_int_cst (TREE_TYPE (*vr0max
), 1));
5329 tree max
= int_const_binop (MINUS_EXPR
,
5331 build_int_cst (TREE_TYPE (vr1min
), 1));
5332 if (!operand_less_p (max
, min
))
5334 *vr0type
= VR_ANTI_RANGE
;
5346 /* If the result can be an anti-range, create one. */
5347 if (TREE_CODE (vr1max
) == INTEGER_CST
5348 && TREE_CODE (*vr0min
) == INTEGER_CST
5349 && vrp_val_is_min (vr1min
)
5350 && vrp_val_is_max (*vr0max
))
5352 tree min
= int_const_binop (PLUS_EXPR
,
5354 build_int_cst (TREE_TYPE (vr1max
), 1));
5355 tree max
= int_const_binop (MINUS_EXPR
,
5357 build_int_cst (TREE_TYPE (*vr0min
), 1));
5358 if (!operand_less_p (max
, min
))
5360 *vr0type
= VR_ANTI_RANGE
;
5374 else if ((maxeq
|| operand_less_p (vr1max
, *vr0max
) == 1)
5375 && (mineq
|| operand_less_p (*vr0min
, vr1min
) == 1))
5377 /* [ ( ) ] or [( ) ] or [ ( )] */
5378 if (*vr0type
== VR_RANGE
5379 && vr1type
== VR_RANGE
)
5381 else if (*vr0type
== VR_ANTI_RANGE
5382 && vr1type
== VR_ANTI_RANGE
)
5388 else if (*vr0type
== VR_ANTI_RANGE
5389 && vr1type
== VR_RANGE
)
5391 /* Arbitrarily choose the right or left gap. */
5392 if (!mineq
&& TREE_CODE (vr1min
) == INTEGER_CST
)
5393 *vr0max
= int_const_binop (MINUS_EXPR
, vr1min
,
5394 build_int_cst (TREE_TYPE (vr1min
), 1));
5395 else if (!maxeq
&& TREE_CODE (vr1max
) == INTEGER_CST
)
5396 *vr0min
= int_const_binop (PLUS_EXPR
, vr1max
,
5397 build_int_cst (TREE_TYPE (vr1max
), 1));
5401 else if (*vr0type
== VR_RANGE
5402 && vr1type
== VR_ANTI_RANGE
)
5403 /* The result covers everything. */
5408 else if ((maxeq
|| operand_less_p (*vr0max
, vr1max
) == 1)
5409 && (mineq
|| operand_less_p (vr1min
, *vr0min
) == 1))
5411 /* ( [ ] ) or ([ ] ) or ( [ ]) */
5412 if (*vr0type
== VR_RANGE
5413 && vr1type
== VR_RANGE
)
5419 else if (*vr0type
== VR_ANTI_RANGE
5420 && vr1type
== VR_ANTI_RANGE
)
5422 else if (*vr0type
== VR_RANGE
5423 && vr1type
== VR_ANTI_RANGE
)
5425 *vr0type
= VR_ANTI_RANGE
;
5426 if (!mineq
&& TREE_CODE (*vr0min
) == INTEGER_CST
)
5428 *vr0max
= int_const_binop (MINUS_EXPR
, *vr0min
,
5429 build_int_cst (TREE_TYPE (*vr0min
), 1));
5432 else if (!maxeq
&& TREE_CODE (*vr0max
) == INTEGER_CST
)
5434 *vr0min
= int_const_binop (PLUS_EXPR
, *vr0max
,
5435 build_int_cst (TREE_TYPE (*vr0max
), 1));
5441 else if (*vr0type
== VR_ANTI_RANGE
5442 && vr1type
== VR_RANGE
)
5443 /* The result covers everything. */
5448 else if ((operand_less_p (vr1min
, *vr0max
) == 1
5449 || operand_equal_p (vr1min
, *vr0max
, 0))
5450 && operand_less_p (*vr0min
, vr1min
) == 1
5451 && operand_less_p (*vr0max
, vr1max
) == 1)
5453 /* [ ( ] ) or [ ]( ) */
5454 if (*vr0type
== VR_RANGE
5455 && vr1type
== VR_RANGE
)
5457 else if (*vr0type
== VR_ANTI_RANGE
5458 && vr1type
== VR_ANTI_RANGE
)
5460 else if (*vr0type
== VR_ANTI_RANGE
5461 && vr1type
== VR_RANGE
)
5463 if (TREE_CODE (vr1min
) == INTEGER_CST
)
5464 *vr0max
= int_const_binop (MINUS_EXPR
, vr1min
,
5465 build_int_cst (TREE_TYPE (vr1min
), 1));
5469 else if (*vr0type
== VR_RANGE
5470 && vr1type
== VR_ANTI_RANGE
)
5472 if (TREE_CODE (*vr0max
) == INTEGER_CST
)
5475 *vr0min
= int_const_binop (PLUS_EXPR
, *vr0max
,
5476 build_int_cst (TREE_TYPE (*vr0max
), 1));
5485 else if ((operand_less_p (*vr0min
, vr1max
) == 1
5486 || operand_equal_p (*vr0min
, vr1max
, 0))
5487 && operand_less_p (vr1min
, *vr0min
) == 1
5488 && operand_less_p (vr1max
, *vr0max
) == 1)
5490 /* ( [ ) ] or ( )[ ] */
5491 if (*vr0type
== VR_RANGE
5492 && vr1type
== VR_RANGE
)
5494 else if (*vr0type
== VR_ANTI_RANGE
5495 && vr1type
== VR_ANTI_RANGE
)
5497 else if (*vr0type
== VR_ANTI_RANGE
5498 && vr1type
== VR_RANGE
)
5500 if (TREE_CODE (vr1max
) == INTEGER_CST
)
5501 *vr0min
= int_const_binop (PLUS_EXPR
, vr1max
,
5502 build_int_cst (TREE_TYPE (vr1max
), 1));
5506 else if (*vr0type
== VR_RANGE
5507 && vr1type
== VR_ANTI_RANGE
)
5509 if (TREE_CODE (*vr0min
) == INTEGER_CST
)
5512 *vr0max
= int_const_binop (MINUS_EXPR
, *vr0min
,
5513 build_int_cst (TREE_TYPE (*vr0min
), 1));
5528 *vr0type
= VR_VARYING
;
5529 *vr0min
= NULL_TREE
;
5530 *vr0max
= NULL_TREE
;
5533 /* Intersect the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
5534 { VR1TYPE, VR0MIN, VR0MAX } and store the result
5535 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
5536 possible such range. The resulting range is not canonicalized. */
5539 intersect_ranges (enum value_range_type
*vr0type
,
5540 tree
*vr0min
, tree
*vr0max
,
5541 enum value_range_type vr1type
,
5542 tree vr1min
, tree vr1max
)
5544 bool mineq
= vrp_operand_equal_p (*vr0min
, vr1min
);
5545 bool maxeq
= vrp_operand_equal_p (*vr0max
, vr1max
);
5547 /* [] is vr0, () is vr1 in the following classification comments. */
5551 if (*vr0type
== vr1type
)
5552 /* Nothing to do for equal ranges. */
5554 else if ((*vr0type
== VR_RANGE
5555 && vr1type
== VR_ANTI_RANGE
)
5556 || (*vr0type
== VR_ANTI_RANGE
5557 && vr1type
== VR_RANGE
))
5559 /* For anti-range with range intersection the result is empty. */
5560 *vr0type
= VR_UNDEFINED
;
5561 *vr0min
= NULL_TREE
;
5562 *vr0max
= NULL_TREE
;
5567 else if (operand_less_p (*vr0max
, vr1min
) == 1
5568 || operand_less_p (vr1max
, *vr0min
) == 1)
5570 /* [ ] ( ) or ( ) [ ]
5571 If the ranges have an empty intersection, the result of the
5572 intersect operation is the range for intersecting an
5573 anti-range with a range or empty when intersecting two ranges. */
5574 if (*vr0type
== VR_RANGE
5575 && vr1type
== VR_ANTI_RANGE
)
5577 else if (*vr0type
== VR_ANTI_RANGE
5578 && vr1type
== VR_RANGE
)
5584 else if (*vr0type
== VR_RANGE
5585 && vr1type
== VR_RANGE
)
5587 *vr0type
= VR_UNDEFINED
;
5588 *vr0min
= NULL_TREE
;
5589 *vr0max
= NULL_TREE
;
5591 else if (*vr0type
== VR_ANTI_RANGE
5592 && vr1type
== VR_ANTI_RANGE
)
5594 /* If the anti-ranges are adjacent to each other merge them. */
5595 if (TREE_CODE (*vr0max
) == INTEGER_CST
5596 && TREE_CODE (vr1min
) == INTEGER_CST
5597 && operand_less_p (*vr0max
, vr1min
) == 1
5598 && integer_onep (int_const_binop (MINUS_EXPR
,
5601 else if (TREE_CODE (vr1max
) == INTEGER_CST
5602 && TREE_CODE (*vr0min
) == INTEGER_CST
5603 && operand_less_p (vr1max
, *vr0min
) == 1
5604 && integer_onep (int_const_binop (MINUS_EXPR
,
5607 /* Else arbitrarily take VR0. */
5610 else if ((maxeq
|| operand_less_p (vr1max
, *vr0max
) == 1)
5611 && (mineq
|| operand_less_p (*vr0min
, vr1min
) == 1))
5613 /* [ ( ) ] or [( ) ] or [ ( )] */
5614 if (*vr0type
== VR_RANGE
5615 && vr1type
== VR_RANGE
)
5617 /* If both are ranges the result is the inner one. */
5622 else if (*vr0type
== VR_RANGE
5623 && vr1type
== VR_ANTI_RANGE
)
5625 /* Choose the right gap if the left one is empty. */
5628 if (TREE_CODE (vr1max
) != INTEGER_CST
)
5630 else if (TYPE_PRECISION (TREE_TYPE (vr1max
)) == 1
5631 && !TYPE_UNSIGNED (TREE_TYPE (vr1max
)))
5633 = int_const_binop (MINUS_EXPR
, vr1max
,
5634 build_int_cst (TREE_TYPE (vr1max
), -1));
5637 = int_const_binop (PLUS_EXPR
, vr1max
,
5638 build_int_cst (TREE_TYPE (vr1max
), 1));
5640 /* Choose the left gap if the right one is empty. */
5643 if (TREE_CODE (vr1min
) != INTEGER_CST
)
5645 else if (TYPE_PRECISION (TREE_TYPE (vr1min
)) == 1
5646 && !TYPE_UNSIGNED (TREE_TYPE (vr1min
)))
5648 = int_const_binop (PLUS_EXPR
, vr1min
,
5649 build_int_cst (TREE_TYPE (vr1min
), -1));
5652 = int_const_binop (MINUS_EXPR
, vr1min
,
5653 build_int_cst (TREE_TYPE (vr1min
), 1));
5655 /* Choose the anti-range if the range is effectively varying. */
5656 else if (vrp_val_is_min (*vr0min
)
5657 && vrp_val_is_max (*vr0max
))
5663 /* Else choose the range. */
5665 else if (*vr0type
== VR_ANTI_RANGE
5666 && vr1type
== VR_ANTI_RANGE
)
5667 /* If both are anti-ranges the result is the outer one. */
5669 else if (*vr0type
== VR_ANTI_RANGE
5670 && vr1type
== VR_RANGE
)
5672 /* The intersection is empty. */
5673 *vr0type
= VR_UNDEFINED
;
5674 *vr0min
= NULL_TREE
;
5675 *vr0max
= NULL_TREE
;
5680 else if ((maxeq
|| operand_less_p (*vr0max
, vr1max
) == 1)
5681 && (mineq
|| operand_less_p (vr1min
, *vr0min
) == 1))
5683 /* ( [ ] ) or ([ ] ) or ( [ ]) */
5684 if (*vr0type
== VR_RANGE
5685 && vr1type
== VR_RANGE
)
5686 /* Choose the inner range. */
5688 else if (*vr0type
== VR_ANTI_RANGE
5689 && vr1type
== VR_RANGE
)
5691 /* Choose the right gap if the left is empty. */
5694 *vr0type
= VR_RANGE
;
5695 if (TREE_CODE (*vr0max
) != INTEGER_CST
)
5697 else if (TYPE_PRECISION (TREE_TYPE (*vr0max
)) == 1
5698 && !TYPE_UNSIGNED (TREE_TYPE (*vr0max
)))
5700 = int_const_binop (MINUS_EXPR
, *vr0max
,
5701 build_int_cst (TREE_TYPE (*vr0max
), -1));
5704 = int_const_binop (PLUS_EXPR
, *vr0max
,
5705 build_int_cst (TREE_TYPE (*vr0max
), 1));
5708 /* Choose the left gap if the right is empty. */
5711 *vr0type
= VR_RANGE
;
5712 if (TREE_CODE (*vr0min
) != INTEGER_CST
)
5714 else if (TYPE_PRECISION (TREE_TYPE (*vr0min
)) == 1
5715 && !TYPE_UNSIGNED (TREE_TYPE (*vr0min
)))
5717 = int_const_binop (PLUS_EXPR
, *vr0min
,
5718 build_int_cst (TREE_TYPE (*vr0min
), -1));
5721 = int_const_binop (MINUS_EXPR
, *vr0min
,
5722 build_int_cst (TREE_TYPE (*vr0min
), 1));
5725 /* Choose the anti-range if the range is effectively varying. */
5726 else if (vrp_val_is_min (vr1min
)
5727 && vrp_val_is_max (vr1max
))
5729 /* Choose the anti-range if it is ~[0,0], that range is special
5730 enough to special case when vr1's range is relatively wide.
5731 At least for types bigger than int - this covers pointers
5732 and arguments to functions like ctz. */
5733 else if (*vr0min
== *vr0max
5734 && integer_zerop (*vr0min
)
5735 && ((TYPE_PRECISION (TREE_TYPE (*vr0min
))
5736 >= TYPE_PRECISION (integer_type_node
))
5737 || POINTER_TYPE_P (TREE_TYPE (*vr0min
)))
5738 && TREE_CODE (vr1max
) == INTEGER_CST
5739 && TREE_CODE (vr1min
) == INTEGER_CST
5740 && (wi::clz (wi::to_wide (vr1max
) - wi::to_wide (vr1min
))
5741 < TYPE_PRECISION (TREE_TYPE (*vr0min
)) / 2))
5743 /* Else choose the range. */
5751 else if (*vr0type
== VR_ANTI_RANGE
5752 && vr1type
== VR_ANTI_RANGE
)
5754 /* If both are anti-ranges the result is the outer one. */
5759 else if (vr1type
== VR_ANTI_RANGE
5760 && *vr0type
== VR_RANGE
)
5762 /* The intersection is empty. */
5763 *vr0type
= VR_UNDEFINED
;
5764 *vr0min
= NULL_TREE
;
5765 *vr0max
= NULL_TREE
;
5770 else if ((operand_less_p (vr1min
, *vr0max
) == 1
5771 || operand_equal_p (vr1min
, *vr0max
, 0))
5772 && operand_less_p (*vr0min
, vr1min
) == 1)
5774 /* [ ( ] ) or [ ]( ) */
5775 if (*vr0type
== VR_ANTI_RANGE
5776 && vr1type
== VR_ANTI_RANGE
)
5778 else if (*vr0type
== VR_RANGE
5779 && vr1type
== VR_RANGE
)
5781 else if (*vr0type
== VR_RANGE
5782 && vr1type
== VR_ANTI_RANGE
)
5784 if (TREE_CODE (vr1min
) == INTEGER_CST
)
5785 *vr0max
= int_const_binop (MINUS_EXPR
, vr1min
,
5786 build_int_cst (TREE_TYPE (vr1min
), 1));
5790 else if (*vr0type
== VR_ANTI_RANGE
5791 && vr1type
== VR_RANGE
)
5793 *vr0type
= VR_RANGE
;
5794 if (TREE_CODE (*vr0max
) == INTEGER_CST
)
5795 *vr0min
= int_const_binop (PLUS_EXPR
, *vr0max
,
5796 build_int_cst (TREE_TYPE (*vr0max
), 1));
5804 else if ((operand_less_p (*vr0min
, vr1max
) == 1
5805 || operand_equal_p (*vr0min
, vr1max
, 0))
5806 && operand_less_p (vr1min
, *vr0min
) == 1)
5808 /* ( [ ) ] or ( )[ ] */
5809 if (*vr0type
== VR_ANTI_RANGE
5810 && vr1type
== VR_ANTI_RANGE
)
5812 else if (*vr0type
== VR_RANGE
5813 && vr1type
== VR_RANGE
)
5815 else if (*vr0type
== VR_RANGE
5816 && vr1type
== VR_ANTI_RANGE
)
5818 if (TREE_CODE (vr1max
) == INTEGER_CST
)
5819 *vr0min
= int_const_binop (PLUS_EXPR
, vr1max
,
5820 build_int_cst (TREE_TYPE (vr1max
), 1));
5824 else if (*vr0type
== VR_ANTI_RANGE
5825 && vr1type
== VR_RANGE
)
5827 *vr0type
= VR_RANGE
;
5828 if (TREE_CODE (*vr0min
) == INTEGER_CST
)
5829 *vr0max
= int_const_binop (MINUS_EXPR
, *vr0min
,
5830 build_int_cst (TREE_TYPE (*vr0min
), 1));
5839 /* As a fallback simply use { *VRTYPE, *VR0MIN, *VR0MAX } as
5840 result for the intersection. That's always a conservative
5841 correct estimate unless VR1 is a constant singleton range
5842 in which case we choose that. */
5843 if (vr1type
== VR_RANGE
5844 && is_gimple_min_invariant (vr1min
)
5845 && vrp_operand_equal_p (vr1min
, vr1max
))
5856 /* Intersect the two value-ranges *VR0 and *VR1 and store the result
5857 in *VR0. This may not be the smallest possible such range. */
5860 vrp_intersect_ranges_1 (value_range
*vr0
, const value_range
*vr1
)
5864 /* If either range is VR_VARYING the other one wins. */
5865 if (vr1
->type
== VR_VARYING
)
5867 if (vr0
->type
== VR_VARYING
)
5869 copy_value_range (vr0
, vr1
);
5873 /* When either range is VR_UNDEFINED the resulting range is
5874 VR_UNDEFINED, too. */
5875 if (vr0
->type
== VR_UNDEFINED
)
5877 if (vr1
->type
== VR_UNDEFINED
)
5879 set_value_range_to_undefined (vr0
);
5883 /* Save the original vr0 so we can return it as conservative intersection
5884 result when our worker turns things to varying. */
5886 intersect_ranges (&vr0
->type
, &vr0
->min
, &vr0
->max
,
5887 vr1
->type
, vr1
->min
, vr1
->max
);
5888 /* Make sure to canonicalize the result though as the inversion of a
5889 VR_RANGE can still be a VR_RANGE. */
5890 set_and_canonicalize_value_range (vr0
, vr0
->type
,
5891 vr0
->min
, vr0
->max
, vr0
->equiv
);
5892 /* If that failed, use the saved original VR0. */
5893 if (vr0
->type
== VR_VARYING
)
5898 /* If the result is VR_UNDEFINED there is no need to mess with
5899 the equivalencies. */
5900 if (vr0
->type
== VR_UNDEFINED
)
5903 /* The resulting set of equivalences for range intersection is the union of
5905 if (vr0
->equiv
&& vr1
->equiv
&& vr0
->equiv
!= vr1
->equiv
)
5906 bitmap_ior_into (vr0
->equiv
, vr1
->equiv
);
5907 else if (vr1
->equiv
&& !vr0
->equiv
)
5909 /* All equivalence bitmaps are allocated from the same obstack. So
5910 we can use the obstack associated with VR to allocate vr0->equiv. */
5911 vr0
->equiv
= BITMAP_ALLOC (vr1
->equiv
->obstack
);
5912 bitmap_copy (vr0
->equiv
, vr1
->equiv
);
5917 vrp_intersect_ranges (value_range
*vr0
, const value_range
*vr1
)
5919 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
5921 fprintf (dump_file
, "Intersecting\n ");
5922 dump_value_range (dump_file
, vr0
);
5923 fprintf (dump_file
, "\nand\n ");
5924 dump_value_range (dump_file
, vr1
);
5925 fprintf (dump_file
, "\n");
5927 vrp_intersect_ranges_1 (vr0
, vr1
);
5928 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
5930 fprintf (dump_file
, "to\n ");
5931 dump_value_range (dump_file
, vr0
);
5932 fprintf (dump_file
, "\n");
5936 /* Meet operation for value ranges. Given two value ranges VR0 and
5937 VR1, store in VR0 a range that contains both VR0 and VR1. This
5938 may not be the smallest possible such range. */
5941 vrp_meet_1 (value_range
*vr0
, const value_range
*vr1
)
5945 if (vr0
->type
== VR_UNDEFINED
)
5947 set_value_range (vr0
, vr1
->type
, vr1
->min
, vr1
->max
, vr1
->equiv
);
5951 if (vr1
->type
== VR_UNDEFINED
)
5953 /* VR0 already has the resulting range. */
5957 if (vr0
->type
== VR_VARYING
)
5959 /* Nothing to do. VR0 already has the resulting range. */
5963 if (vr1
->type
== VR_VARYING
)
5965 set_value_range_to_varying (vr0
);
5970 union_ranges (&vr0
->type
, &vr0
->min
, &vr0
->max
,
5971 vr1
->type
, vr1
->min
, vr1
->max
);
5972 if (vr0
->type
== VR_VARYING
)
5974 /* Failed to find an efficient meet. Before giving up and setting
5975 the result to VARYING, see if we can at least derive a useful
5977 if (range_includes_zero_p (&saved
) == 0
5978 && range_includes_zero_p (vr1
) == 0)
5980 set_value_range_to_nonnull (vr0
, TREE_TYPE (saved
.min
));
5982 /* Since this meet operation did not result from the meeting of
5983 two equivalent names, VR0 cannot have any equivalences. */
5985 bitmap_clear (vr0
->equiv
);
5989 set_value_range_to_varying (vr0
);
5992 set_and_canonicalize_value_range (vr0
, vr0
->type
, vr0
->min
, vr0
->max
,
5994 if (vr0
->type
== VR_VARYING
)
5997 /* The resulting set of equivalences is always the intersection of
5999 if (vr0
->equiv
&& vr1
->equiv
&& vr0
->equiv
!= vr1
->equiv
)
6000 bitmap_and_into (vr0
->equiv
, vr1
->equiv
);
6001 else if (vr0
->equiv
&& !vr1
->equiv
)
6002 bitmap_clear (vr0
->equiv
);
6006 vrp_meet (value_range
*vr0
, const value_range
*vr1
)
6008 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6010 fprintf (dump_file
, "Meeting\n ");
6011 dump_value_range (dump_file
, vr0
);
6012 fprintf (dump_file
, "\nand\n ");
6013 dump_value_range (dump_file
, vr1
);
6014 fprintf (dump_file
, "\n");
6016 vrp_meet_1 (vr0
, vr1
);
6017 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6019 fprintf (dump_file
, "to\n ");
6020 dump_value_range (dump_file
, vr0
);
6021 fprintf (dump_file
, "\n");
6026 /* Visit all arguments for PHI node PHI that flow through executable
6027 edges. If a valid value range can be derived from all the incoming
6028 value ranges, set a new range for the LHS of PHI. */
6030 enum ssa_prop_result
6031 vrp_prop::visit_phi (gphi
*phi
)
6033 tree lhs
= PHI_RESULT (phi
);
6034 value_range vr_result
= VR_INITIALIZER
;
6035 extract_range_from_phi_node (phi
, &vr_result
);
6036 if (update_value_range (lhs
, &vr_result
))
6038 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6040 fprintf (dump_file
, "Found new range for ");
6041 print_generic_expr (dump_file
, lhs
);
6042 fprintf (dump_file
, ": ");
6043 dump_value_range (dump_file
, &vr_result
);
6044 fprintf (dump_file
, "\n");
6047 if (vr_result
.type
== VR_VARYING
)
6048 return SSA_PROP_VARYING
;
6050 return SSA_PROP_INTERESTING
;
6053 /* Nothing changed, don't add outgoing edges. */
6054 return SSA_PROP_NOT_INTERESTING
;
6057 class vrp_folder
: public substitute_and_fold_engine
6060 tree
get_value (tree
) FINAL OVERRIDE
;
6061 bool fold_stmt (gimple_stmt_iterator
*) FINAL OVERRIDE
;
6062 bool fold_predicate_in (gimple_stmt_iterator
*);
6064 class vr_values
*vr_values
;
6067 tree
vrp_evaluate_conditional (tree_code code
, tree op0
,
6068 tree op1
, gimple
*stmt
)
6069 { return vr_values
->vrp_evaluate_conditional (code
, op0
, op1
, stmt
); }
6070 bool simplify_stmt_using_ranges (gimple_stmt_iterator
*gsi
)
6071 { return vr_values
->simplify_stmt_using_ranges (gsi
); }
6072 tree
op_with_constant_singleton_value_range (tree op
)
6073 { return vr_values
->op_with_constant_singleton_value_range (op
); }
6076 /* If the statement pointed by SI has a predicate whose value can be
6077 computed using the value range information computed by VRP, compute
6078 its value and return true. Otherwise, return false. */
6081 vrp_folder::fold_predicate_in (gimple_stmt_iterator
*si
)
6083 bool assignment_p
= false;
6085 gimple
*stmt
= gsi_stmt (*si
);
6087 if (is_gimple_assign (stmt
)
6088 && TREE_CODE_CLASS (gimple_assign_rhs_code (stmt
)) == tcc_comparison
)
6090 assignment_p
= true;
6091 val
= vrp_evaluate_conditional (gimple_assign_rhs_code (stmt
),
6092 gimple_assign_rhs1 (stmt
),
6093 gimple_assign_rhs2 (stmt
),
6096 else if (gcond
*cond_stmt
= dyn_cast
<gcond
*> (stmt
))
6097 val
= vrp_evaluate_conditional (gimple_cond_code (cond_stmt
),
6098 gimple_cond_lhs (cond_stmt
),
6099 gimple_cond_rhs (cond_stmt
),
6107 val
= fold_convert (gimple_expr_type (stmt
), val
);
6111 fprintf (dump_file
, "Folding predicate ");
6112 print_gimple_expr (dump_file
, stmt
, 0);
6113 fprintf (dump_file
, " to ");
6114 print_generic_expr (dump_file
, val
);
6115 fprintf (dump_file
, "\n");
6118 if (is_gimple_assign (stmt
))
6119 gimple_assign_set_rhs_from_tree (si
, val
);
6122 gcc_assert (gimple_code (stmt
) == GIMPLE_COND
);
6123 gcond
*cond_stmt
= as_a
<gcond
*> (stmt
);
6124 if (integer_zerop (val
))
6125 gimple_cond_make_false (cond_stmt
);
6126 else if (integer_onep (val
))
6127 gimple_cond_make_true (cond_stmt
);
6138 /* Callback for substitute_and_fold folding the stmt at *SI. */
6141 vrp_folder::fold_stmt (gimple_stmt_iterator
*si
)
6143 if (fold_predicate_in (si
))
6146 return simplify_stmt_using_ranges (si
);
6149 /* If OP has a value range with a single constant value return that,
6150 otherwise return NULL_TREE. This returns OP itself if OP is a
6153 Implemented as a pure wrapper right now, but this will change. */
6156 vrp_folder::get_value (tree op
)
6158 return op_with_constant_singleton_value_range (op
);
6161 /* Return the LHS of any ASSERT_EXPR where OP appears as the first
6162 argument to the ASSERT_EXPR and in which the ASSERT_EXPR dominates
6163 BB. If no such ASSERT_EXPR is found, return OP. */
6166 lhs_of_dominating_assert (tree op
, basic_block bb
, gimple
*stmt
)
6168 imm_use_iterator imm_iter
;
6170 use_operand_p use_p
;
6172 if (TREE_CODE (op
) == SSA_NAME
)
6174 FOR_EACH_IMM_USE_FAST (use_p
, imm_iter
, op
)
6176 use_stmt
= USE_STMT (use_p
);
6177 if (use_stmt
!= stmt
6178 && gimple_assign_single_p (use_stmt
)
6179 && TREE_CODE (gimple_assign_rhs1 (use_stmt
)) == ASSERT_EXPR
6180 && TREE_OPERAND (gimple_assign_rhs1 (use_stmt
), 0) == op
6181 && dominated_by_p (CDI_DOMINATORS
, bb
, gimple_bb (use_stmt
)))
6182 return gimple_assign_lhs (use_stmt
);
6189 static class vr_values
*x_vr_values
;
6191 /* A trivial wrapper so that we can present the generic jump threading
6192 code with a simple API for simplifying statements. STMT is the
6193 statement we want to simplify, WITHIN_STMT provides the location
6194 for any overflow warnings. */
6197 simplify_stmt_for_jump_threading (gimple
*stmt
, gimple
*within_stmt
,
6198 class avail_exprs_stack
*avail_exprs_stack ATTRIBUTE_UNUSED
,
6201 /* First see if the conditional is in the hash table. */
6202 tree cached_lhs
= avail_exprs_stack
->lookup_avail_expr (stmt
, false, true);
6203 if (cached_lhs
&& is_gimple_min_invariant (cached_lhs
))
6206 vr_values
*vr_values
= x_vr_values
;
6207 if (gcond
*cond_stmt
= dyn_cast
<gcond
*> (stmt
))
6209 tree op0
= gimple_cond_lhs (cond_stmt
);
6210 op0
= lhs_of_dominating_assert (op0
, bb
, stmt
);
6212 tree op1
= gimple_cond_rhs (cond_stmt
);
6213 op1
= lhs_of_dominating_assert (op1
, bb
, stmt
);
6215 return vr_values
->vrp_evaluate_conditional (gimple_cond_code (cond_stmt
),
6216 op0
, op1
, within_stmt
);
6219 /* We simplify a switch statement by trying to determine which case label
6220 will be taken. If we are successful then we return the corresponding
6222 if (gswitch
*switch_stmt
= dyn_cast
<gswitch
*> (stmt
))
6224 tree op
= gimple_switch_index (switch_stmt
);
6225 if (TREE_CODE (op
) != SSA_NAME
)
6228 op
= lhs_of_dominating_assert (op
, bb
, stmt
);
6230 const value_range
*vr
= vr_values
->get_value_range (op
);
6231 if ((vr
->type
!= VR_RANGE
&& vr
->type
!= VR_ANTI_RANGE
)
6232 || symbolic_range_p (vr
))
6235 if (vr
->type
== VR_RANGE
)
6238 /* Get the range of labels that contain a part of the operand's
6240 find_case_label_range (switch_stmt
, vr
->min
, vr
->max
, &i
, &j
);
6242 /* Is there only one such label? */
6245 tree label
= gimple_switch_label (switch_stmt
, i
);
6247 /* The i'th label will be taken only if the value range of the
6248 operand is entirely within the bounds of this label. */
6249 if (CASE_HIGH (label
) != NULL_TREE
6250 ? (tree_int_cst_compare (CASE_LOW (label
), vr
->min
) <= 0
6251 && tree_int_cst_compare (CASE_HIGH (label
), vr
->max
) >= 0)
6252 : (tree_int_cst_equal (CASE_LOW (label
), vr
->min
)
6253 && tree_int_cst_equal (vr
->min
, vr
->max
)))
6257 /* If there are no such labels then the default label will be
6260 return gimple_switch_label (switch_stmt
, 0);
6263 if (vr
->type
== VR_ANTI_RANGE
)
6265 unsigned n
= gimple_switch_num_labels (switch_stmt
);
6266 tree min_label
= gimple_switch_label (switch_stmt
, 1);
6267 tree max_label
= gimple_switch_label (switch_stmt
, n
- 1);
6269 /* The default label will be taken only if the anti-range of the
6270 operand is entirely outside the bounds of all the (non-default)
6272 if (tree_int_cst_compare (vr
->min
, CASE_LOW (min_label
)) <= 0
6273 && (CASE_HIGH (max_label
) != NULL_TREE
6274 ? tree_int_cst_compare (vr
->max
, CASE_HIGH (max_label
)) >= 0
6275 : tree_int_cst_compare (vr
->max
, CASE_LOW (max_label
)) >= 0))
6276 return gimple_switch_label (switch_stmt
, 0);
6282 if (gassign
*assign_stmt
= dyn_cast
<gassign
*> (stmt
))
6284 tree lhs
= gimple_assign_lhs (assign_stmt
);
6285 if (TREE_CODE (lhs
) == SSA_NAME
6286 && (INTEGRAL_TYPE_P (TREE_TYPE (lhs
))
6287 || POINTER_TYPE_P (TREE_TYPE (lhs
)))
6288 && stmt_interesting_for_vrp (stmt
))
6292 value_range new_vr
= VR_INITIALIZER
;
6293 vr_values
->extract_range_from_stmt (stmt
, &dummy_e
,
6294 &dummy_tree
, &new_vr
);
6295 if (range_int_cst_singleton_p (&new_vr
))
6303 class vrp_dom_walker
: public dom_walker
6306 vrp_dom_walker (cdi_direction direction
,
6307 class const_and_copies
*const_and_copies
,
6308 class avail_exprs_stack
*avail_exprs_stack
)
6309 : dom_walker (direction
, REACHABLE_BLOCKS
),
6310 m_const_and_copies (const_and_copies
),
6311 m_avail_exprs_stack (avail_exprs_stack
),
6312 m_dummy_cond (NULL
) {}
6314 virtual edge
before_dom_children (basic_block
);
6315 virtual void after_dom_children (basic_block
);
6317 class vr_values
*vr_values
;
6320 class const_and_copies
*m_const_and_copies
;
6321 class avail_exprs_stack
*m_avail_exprs_stack
;
6323 gcond
*m_dummy_cond
;
6327 /* Called before processing dominator children of BB. We want to look
6328 at ASSERT_EXPRs and record information from them in the appropriate
6331 We could look at other statements here. It's not seen as likely
6332 to significantly increase the jump threads we discover. */
6335 vrp_dom_walker::before_dom_children (basic_block bb
)
6337 gimple_stmt_iterator gsi
;
6339 m_avail_exprs_stack
->push_marker ();
6340 m_const_and_copies
->push_marker ();
6341 for (gsi
= gsi_start_nondebug_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
6343 gimple
*stmt
= gsi_stmt (gsi
);
6344 if (gimple_assign_single_p (stmt
)
6345 && TREE_CODE (gimple_assign_rhs1 (stmt
)) == ASSERT_EXPR
)
6347 tree rhs1
= gimple_assign_rhs1 (stmt
);
6348 tree cond
= TREE_OPERAND (rhs1
, 1);
6349 tree inverted
= invert_truthvalue (cond
);
6350 vec
<cond_equivalence
> p
;
6352 record_conditions (&p
, cond
, inverted
);
6353 for (unsigned int i
= 0; i
< p
.length (); i
++)
6354 m_avail_exprs_stack
->record_cond (&p
[i
]);
6356 tree lhs
= gimple_assign_lhs (stmt
);
6357 m_const_and_copies
->record_const_or_copy (lhs
,
6358 TREE_OPERAND (rhs1
, 0));
6367 /* Called after processing dominator children of BB. This is where we
6368 actually call into the threader. */
6370 vrp_dom_walker::after_dom_children (basic_block bb
)
6373 m_dummy_cond
= gimple_build_cond (NE_EXPR
,
6374 integer_zero_node
, integer_zero_node
,
6377 x_vr_values
= vr_values
;
6378 thread_outgoing_edges (bb
, m_dummy_cond
, m_const_and_copies
,
6379 m_avail_exprs_stack
, NULL
,
6380 simplify_stmt_for_jump_threading
);
6383 m_avail_exprs_stack
->pop_to_marker ();
6384 m_const_and_copies
->pop_to_marker ();
6387 /* Blocks which have more than one predecessor and more than
6388 one successor present jump threading opportunities, i.e.,
6389 when the block is reached from a specific predecessor, we
6390 may be able to determine which of the outgoing edges will
6391 be traversed. When this optimization applies, we are able
6392 to avoid conditionals at runtime and we may expose secondary
6393 optimization opportunities.
6395 This routine is effectively a driver for the generic jump
6396 threading code. It basically just presents the generic code
6397 with edges that may be suitable for jump threading.
6399 Unlike DOM, we do not iterate VRP if jump threading was successful.
6400 While iterating may expose new opportunities for VRP, it is expected
6401 those opportunities would be very limited and the compile time cost
6402 to expose those opportunities would be significant.
6404 As jump threading opportunities are discovered, they are registered
6405 for later realization. */
6408 identify_jump_threads (class vr_values
*vr_values
)
6413 /* Ugh. When substituting values earlier in this pass we can
6414 wipe the dominance information. So rebuild the dominator
6415 information as we need it within the jump threading code. */
6416 calculate_dominance_info (CDI_DOMINATORS
);
6418 /* We do not allow VRP information to be used for jump threading
6419 across a back edge in the CFG. Otherwise it becomes too
6420 difficult to avoid eliminating loop exit tests. Of course
6421 EDGE_DFS_BACK is not accurate at this time so we have to
6423 mark_dfs_back_edges ();
6425 /* Do not thread across edges we are about to remove. Just marking
6426 them as EDGE_IGNORE will do. */
6427 FOR_EACH_VEC_ELT (to_remove_edges
, i
, e
)
6428 e
->flags
|= EDGE_IGNORE
;
6430 /* Allocate our unwinder stack to unwind any temporary equivalences
6431 that might be recorded. */
6432 const_and_copies
*equiv_stack
= new const_and_copies ();
6434 hash_table
<expr_elt_hasher
> *avail_exprs
6435 = new hash_table
<expr_elt_hasher
> (1024);
6436 avail_exprs_stack
*avail_exprs_stack
6437 = new class avail_exprs_stack (avail_exprs
);
6439 vrp_dom_walker
walker (CDI_DOMINATORS
, equiv_stack
, avail_exprs_stack
);
6440 walker
.vr_values
= vr_values
;
6441 walker
.walk (cfun
->cfg
->x_entry_block_ptr
);
6443 /* Clear EDGE_IGNORE. */
6444 FOR_EACH_VEC_ELT (to_remove_edges
, i
, e
)
6445 e
->flags
&= ~EDGE_IGNORE
;
6447 /* We do not actually update the CFG or SSA graphs at this point as
6448 ASSERT_EXPRs are still in the IL and cfg cleanup code does not yet
6449 handle ASSERT_EXPRs gracefully. */
6452 delete avail_exprs_stack
;
6455 /* Traverse all the blocks folding conditionals with known ranges. */
6458 vrp_prop::vrp_finalize (bool warn_array_bounds_p
)
6462 /* We have completed propagating through the lattice. */
6463 vr_values
.set_lattice_propagation_complete ();
6467 fprintf (dump_file
, "\nValue ranges after VRP:\n\n");
6468 vr_values
.dump_all_value_ranges (dump_file
);
6469 fprintf (dump_file
, "\n");
6472 /* Set value range to non pointer SSA_NAMEs. */
6473 for (i
= 0; i
< num_ssa_names
; i
++)
6475 tree name
= ssa_name (i
);
6479 const value_range
*vr
= get_value_range (name
);
6481 || (vr
->type
== VR_VARYING
)
6482 || (vr
->type
== VR_UNDEFINED
)
6483 || (TREE_CODE (vr
->min
) != INTEGER_CST
)
6484 || (TREE_CODE (vr
->max
) != INTEGER_CST
))
6487 if (POINTER_TYPE_P (TREE_TYPE (name
))
6488 && range_includes_zero_p (vr
) == 0)
6489 set_ptr_nonnull (name
);
6490 else if (!POINTER_TYPE_P (TREE_TYPE (name
)))
6491 set_range_info (name
, vr
->type
,
6492 wi::to_wide (vr
->min
),
6493 wi::to_wide (vr
->max
));
6496 /* If we're checking array refs, we want to merge information on
6497 the executability of each edge between vrp_folder and the
6498 check_array_bounds_dom_walker: each can clear the
6499 EDGE_EXECUTABLE flag on edges, in different ways.
6501 Hence, if we're going to call check_all_array_refs, set
6502 the flag on every edge now, rather than in
6503 check_array_bounds_dom_walker's ctor; vrp_folder may clear
6504 it from some edges. */
6505 if (warn_array_bounds
&& warn_array_bounds_p
)
6506 set_all_edges_as_executable (cfun
);
6508 class vrp_folder vrp_folder
;
6509 vrp_folder
.vr_values
= &vr_values
;
6510 vrp_folder
.substitute_and_fold ();
6512 if (warn_array_bounds
&& warn_array_bounds_p
)
6513 check_all_array_refs ();
6516 /* Main entry point to VRP (Value Range Propagation). This pass is
6517 loosely based on J. R. C. Patterson, ``Accurate Static Branch
6518 Prediction by Value Range Propagation,'' in SIGPLAN Conference on
6519 Programming Language Design and Implementation, pp. 67-78, 1995.
6520 Also available at http://citeseer.ist.psu.edu/patterson95accurate.html
6522 This is essentially an SSA-CCP pass modified to deal with ranges
6523 instead of constants.
6525 While propagating ranges, we may find that two or more SSA name
6526 have equivalent, though distinct ranges. For instance,
6529 2 p_4 = ASSERT_EXPR <p_3, p_3 != 0>
6531 4 p_5 = ASSERT_EXPR <p_4, p_4 == q_2>;
6535 In the code above, pointer p_5 has range [q_2, q_2], but from the
6536 code we can also determine that p_5 cannot be NULL and, if q_2 had
6537 a non-varying range, p_5's range should also be compatible with it.
6539 These equivalences are created by two expressions: ASSERT_EXPR and
6540 copy operations. Since p_5 is an assertion on p_4, and p_4 was the
6541 result of another assertion, then we can use the fact that p_5 and
6542 p_4 are equivalent when evaluating p_5's range.
6544 Together with value ranges, we also propagate these equivalences
6545 between names so that we can take advantage of information from
6546 multiple ranges when doing final replacement. Note that this
6547 equivalency relation is transitive but not symmetric.
6549 In the example above, p_5 is equivalent to p_4, q_2 and p_3, but we
6550 cannot assert that q_2 is equivalent to p_5 because q_2 may be used
6551 in contexts where that assertion does not hold (e.g., in line 6).
6553 TODO, the main difference between this pass and Patterson's is that
6554 we do not propagate edge probabilities. We only compute whether
6555 edges can be taken or not. That is, instead of having a spectrum
6556 of jump probabilities between 0 and 1, we only deal with 0, 1 and
6557 DON'T KNOW. In the future, it may be worthwhile to propagate
6558 probabilities to aid branch prediction. */
6561 execute_vrp (bool warn_array_bounds_p
)
6567 loop_optimizer_init (LOOPS_NORMAL
| LOOPS_HAVE_RECORDED_EXITS
);
6568 rewrite_into_loop_closed_ssa (NULL
, TODO_update_ssa
);
6571 /* ??? This ends up using stale EDGE_DFS_BACK for liveness computation.
6572 Inserting assertions may split edges which will invalidate
6574 insert_range_assertions ();
6576 to_remove_edges
.create (10);
6577 to_update_switch_stmts
.create (5);
6578 threadedge_initialize_values ();
6580 /* For visiting PHI nodes we need EDGE_DFS_BACK computed. */
6581 mark_dfs_back_edges ();
6583 class vrp_prop vrp_prop
;
6584 vrp_prop
.vrp_initialize ();
6585 vrp_prop
.ssa_propagate ();
6586 vrp_prop
.vrp_finalize (warn_array_bounds_p
);
6588 /* We must identify jump threading opportunities before we release
6589 the datastructures built by VRP. */
6590 identify_jump_threads (&vrp_prop
.vr_values
);
6592 /* A comparison of an SSA_NAME against a constant where the SSA_NAME
6593 was set by a type conversion can often be rewritten to use the
6594 RHS of the type conversion.
6596 However, doing so inhibits jump threading through the comparison.
6597 So that transformation is not performed until after jump threading
6600 FOR_EACH_BB_FN (bb
, cfun
)
6602 gimple
*last
= last_stmt (bb
);
6603 if (last
&& gimple_code (last
) == GIMPLE_COND
)
6604 vrp_prop
.vr_values
.simplify_cond_using_ranges_2 (as_a
<gcond
*> (last
));
6607 free_numbers_of_iterations_estimates (cfun
);
6609 /* ASSERT_EXPRs must be removed before finalizing jump threads
6610 as finalizing jump threads calls the CFG cleanup code which
6611 does not properly handle ASSERT_EXPRs. */
6612 remove_range_assertions ();
6614 /* If we exposed any new variables, go ahead and put them into
6615 SSA form now, before we handle jump threading. This simplifies
6616 interactions between rewriting of _DECL nodes into SSA form
6617 and rewriting SSA_NAME nodes into SSA form after block
6618 duplication and CFG manipulation. */
6619 update_ssa (TODO_update_ssa
);
6621 /* We identified all the jump threading opportunities earlier, but could
6622 not transform the CFG at that time. This routine transforms the
6623 CFG and arranges for the dominator tree to be rebuilt if necessary.
6625 Note the SSA graph update will occur during the normal TODO
6626 processing by the pass manager. */
6627 thread_through_all_blocks (false);
6629 /* Remove dead edges from SWITCH_EXPR optimization. This leaves the
6630 CFG in a broken state and requires a cfg_cleanup run. */
6631 FOR_EACH_VEC_ELT (to_remove_edges
, i
, e
)
6633 /* Update SWITCH_EXPR case label vector. */
6634 FOR_EACH_VEC_ELT (to_update_switch_stmts
, i
, su
)
6637 size_t n
= TREE_VEC_LENGTH (su
->vec
);
6639 gimple_switch_set_num_labels (su
->stmt
, n
);
6640 for (j
= 0; j
< n
; j
++)
6641 gimple_switch_set_label (su
->stmt
, j
, TREE_VEC_ELT (su
->vec
, j
));
6642 /* As we may have replaced the default label with a regular one
6643 make sure to make it a real default label again. This ensures
6644 optimal expansion. */
6645 label
= gimple_switch_label (su
->stmt
, 0);
6646 CASE_LOW (label
) = NULL_TREE
;
6647 CASE_HIGH (label
) = NULL_TREE
;
6650 if (to_remove_edges
.length () > 0)
6652 free_dominance_info (CDI_DOMINATORS
);
6653 loops_state_set (LOOPS_NEED_FIXUP
);
6656 to_remove_edges
.release ();
6657 to_update_switch_stmts
.release ();
6658 threadedge_finalize_values ();
6661 loop_optimizer_finalize ();
6667 const pass_data pass_data_vrp
=
6669 GIMPLE_PASS
, /* type */
6671 OPTGROUP_NONE
, /* optinfo_flags */
6672 TV_TREE_VRP
, /* tv_id */
6673 PROP_ssa
, /* properties_required */
6674 0, /* properties_provided */
6675 0, /* properties_destroyed */
6676 0, /* todo_flags_start */
6677 ( TODO_cleanup_cfg
| TODO_update_ssa
), /* todo_flags_finish */
6680 class pass_vrp
: public gimple_opt_pass
6683 pass_vrp (gcc::context
*ctxt
)
6684 : gimple_opt_pass (pass_data_vrp
, ctxt
), warn_array_bounds_p (false)
6687 /* opt_pass methods: */
6688 opt_pass
* clone () { return new pass_vrp (m_ctxt
); }
6689 void set_pass_param (unsigned int n
, bool param
)
6691 gcc_assert (n
== 0);
6692 warn_array_bounds_p
= param
;
6694 virtual bool gate (function
*) { return flag_tree_vrp
!= 0; }
6695 virtual unsigned int execute (function
*)
6696 { return execute_vrp (warn_array_bounds_p
); }
6699 bool warn_array_bounds_p
;
6700 }; // class pass_vrp
6705 make_pass_vrp (gcc::context
*ctxt
)
6707 return new pass_vrp (ctxt
);
6711 /* Worker for determine_value_range. */
6714 determine_value_range_1 (value_range
*vr
, tree expr
)
6716 if (BINARY_CLASS_P (expr
))
6718 value_range vr0
= VR_INITIALIZER
, vr1
= VR_INITIALIZER
;
6719 determine_value_range_1 (&vr0
, TREE_OPERAND (expr
, 0));
6720 determine_value_range_1 (&vr1
, TREE_OPERAND (expr
, 1));
6721 extract_range_from_binary_expr_1 (vr
, TREE_CODE (expr
), TREE_TYPE (expr
),
6724 else if (UNARY_CLASS_P (expr
))
6726 value_range vr0
= VR_INITIALIZER
;
6727 determine_value_range_1 (&vr0
, TREE_OPERAND (expr
, 0));
6728 extract_range_from_unary_expr (vr
, TREE_CODE (expr
), TREE_TYPE (expr
),
6729 &vr0
, TREE_TYPE (TREE_OPERAND (expr
, 0)));
6731 else if (TREE_CODE (expr
) == INTEGER_CST
)
6732 set_value_range_to_value (vr
, expr
, NULL
);
6735 value_range_type kind
;
6737 /* For SSA names try to extract range info computed by VRP. Otherwise
6738 fall back to varying. */
6739 if (TREE_CODE (expr
) == SSA_NAME
6740 && INTEGRAL_TYPE_P (TREE_TYPE (expr
))
6741 && (kind
= get_range_info (expr
, &min
, &max
)) != VR_VARYING
)
6742 set_value_range (vr
, kind
, wide_int_to_tree (TREE_TYPE (expr
), min
),
6743 wide_int_to_tree (TREE_TYPE (expr
), max
), NULL
);
6745 set_value_range_to_varying (vr
);
6749 /* Compute a value-range for EXPR and set it in *MIN and *MAX. Return
6750 the determined range type. */
6753 determine_value_range (tree expr
, wide_int
*min
, wide_int
*max
)
6755 value_range vr
= VR_INITIALIZER
;
6756 determine_value_range_1 (&vr
, expr
);
6757 if ((vr
.type
== VR_RANGE
6758 || vr
.type
== VR_ANTI_RANGE
)
6759 && !symbolic_range_p (&vr
))
6761 *min
= wi::to_wide (vr
.min
);
6762 *max
= wi::to_wide (vr
.max
);