1 /* Support routines for Value Range Propagation (VRP).
2 Copyright (C) 2005-2017 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"
71 /* Set of SSA names found live during the RPO traversal of the function
72 for still active basic-blocks. */
75 /* Return true if the SSA name NAME is live on the edge E. */
78 live_on_edge (edge e
, tree name
)
80 return (live
[e
->dest
->index
]
81 && bitmap_bit_p (live
[e
->dest
->index
], SSA_NAME_VERSION (name
)));
84 /* Location information for ASSERT_EXPRs. Each instance of this
85 structure describes an ASSERT_EXPR for an SSA name. Since a single
86 SSA name may have more than one assertion associated with it, these
87 locations are kept in a linked list attached to the corresponding
91 /* Basic block where the assertion would be inserted. */
94 /* Some assertions need to be inserted on an edge (e.g., assertions
95 generated by COND_EXPRs). In those cases, BB will be NULL. */
98 /* Pointer to the statement that generated this assertion. */
99 gimple_stmt_iterator si
;
101 /* Predicate code for the ASSERT_EXPR. Must be COMPARISON_CLASS_P. */
102 enum tree_code comp_code
;
104 /* Value being compared against. */
107 /* Expression to compare. */
110 /* Next node in the linked list. */
114 /* If bit I is present, it means that SSA name N_i has a list of
115 assertions that should be inserted in the IL. */
116 static bitmap need_assert_for
;
118 /* Array of locations lists where to insert assertions. ASSERTS_FOR[I]
119 holds a list of ASSERT_LOCUS_T nodes that describe where
120 ASSERT_EXPRs for SSA name N_I should be inserted. */
121 static assert_locus
**asserts_for
;
123 vec
<edge
> to_remove_edges
;
124 vec
<switch_update
> to_update_switch_stmts
;
127 /* Return the maximum value for TYPE. */
130 vrp_val_max (const_tree type
)
132 if (!INTEGRAL_TYPE_P (type
))
135 return TYPE_MAX_VALUE (type
);
138 /* Return the minimum value for TYPE. */
141 vrp_val_min (const_tree type
)
143 if (!INTEGRAL_TYPE_P (type
))
146 return TYPE_MIN_VALUE (type
);
149 /* Return whether VAL is equal to the maximum value of its type.
150 We can't do a simple equality comparison with TYPE_MAX_VALUE because
151 C typedefs and Ada subtypes can produce types whose TYPE_MAX_VALUE
152 is not == to the integer constant with the same value in the type. */
155 vrp_val_is_max (const_tree val
)
157 tree type_max
= vrp_val_max (TREE_TYPE (val
));
158 return (val
== type_max
159 || (type_max
!= NULL_TREE
160 && operand_equal_p (val
, type_max
, 0)));
163 /* Return whether VAL is equal to the minimum value of its type. */
166 vrp_val_is_min (const_tree val
)
168 tree type_min
= vrp_val_min (TREE_TYPE (val
));
169 return (val
== type_min
170 || (type_min
!= NULL_TREE
171 && operand_equal_p (val
, type_min
, 0)));
175 /* Set value range VR to VR_UNDEFINED. */
178 set_value_range_to_undefined (value_range
*vr
)
180 vr
->type
= VR_UNDEFINED
;
181 vr
->min
= vr
->max
= NULL_TREE
;
183 bitmap_clear (vr
->equiv
);
186 /* Set value range VR to VR_VARYING. */
189 set_value_range_to_varying (value_range
*vr
)
191 vr
->type
= VR_VARYING
;
192 vr
->min
= vr
->max
= NULL_TREE
;
194 bitmap_clear (vr
->equiv
);
197 /* Set value range VR to {T, MIN, MAX, EQUIV}. */
200 set_value_range (value_range
*vr
, enum value_range_type t
, tree min
,
201 tree max
, bitmap equiv
)
203 /* Check the validity of the range. */
205 && (t
== VR_RANGE
|| t
== VR_ANTI_RANGE
))
209 gcc_assert (min
&& max
);
211 gcc_assert (!TREE_OVERFLOW_P (min
) && !TREE_OVERFLOW_P (max
));
213 if (INTEGRAL_TYPE_P (TREE_TYPE (min
)) && t
== VR_ANTI_RANGE
)
214 gcc_assert (!vrp_val_is_min (min
) || !vrp_val_is_max (max
));
216 cmp
= compare_values (min
, max
);
217 gcc_assert (cmp
== 0 || cmp
== -1 || cmp
== -2);
221 && (t
== VR_UNDEFINED
|| t
== VR_VARYING
))
223 gcc_assert (min
== NULL_TREE
&& max
== NULL_TREE
);
224 gcc_assert (equiv
== NULL
|| bitmap_empty_p (equiv
));
231 /* Since updating the equivalence set involves deep copying the
232 bitmaps, only do it if absolutely necessary.
234 All equivalence bitmaps are allocated from the same obstack. So
235 we can use the obstack associated with EQUIV to allocate vr->equiv. */
236 if (vr
->equiv
== NULL
238 vr
->equiv
= BITMAP_ALLOC (equiv
->obstack
);
240 if (equiv
!= vr
->equiv
)
242 if (equiv
&& !bitmap_empty_p (equiv
))
243 bitmap_copy (vr
->equiv
, equiv
);
245 bitmap_clear (vr
->equiv
);
250 /* Set value range VR to the canonical form of {T, MIN, MAX, EQUIV}.
251 This means adjusting T, MIN and MAX representing the case of a
252 wrapping range with MAX < MIN covering [MIN, type_max] U [type_min, MAX]
253 as anti-rage ~[MAX+1, MIN-1]. Likewise for wrapping anti-ranges.
254 In corner cases where MAX+1 or MIN-1 wraps this will fall back
256 This routine exists to ease canonicalization in the case where we
257 extract ranges from var + CST op limit. */
260 set_and_canonicalize_value_range (value_range
*vr
, enum value_range_type t
,
261 tree min
, tree max
, bitmap equiv
)
263 /* Use the canonical setters for VR_UNDEFINED and VR_VARYING. */
264 if (t
== VR_UNDEFINED
)
266 set_value_range_to_undefined (vr
);
269 else if (t
== VR_VARYING
)
271 set_value_range_to_varying (vr
);
275 /* Nothing to canonicalize for symbolic ranges. */
276 if (TREE_CODE (min
) != INTEGER_CST
277 || TREE_CODE (max
) != INTEGER_CST
)
279 set_value_range (vr
, t
, min
, max
, equiv
);
283 /* Wrong order for min and max, to swap them and the VR type we need
285 if (tree_int_cst_lt (max
, min
))
289 /* For one bit precision if max < min, then the swapped
290 range covers all values, so for VR_RANGE it is varying and
291 for VR_ANTI_RANGE empty range, so drop to varying as well. */
292 if (TYPE_PRECISION (TREE_TYPE (min
)) == 1)
294 set_value_range_to_varying (vr
);
298 one
= build_int_cst (TREE_TYPE (min
), 1);
299 tmp
= int_const_binop (PLUS_EXPR
, max
, one
);
300 max
= int_const_binop (MINUS_EXPR
, min
, one
);
303 /* There's one corner case, if we had [C+1, C] before we now have
304 that again. But this represents an empty value range, so drop
305 to varying in this case. */
306 if (tree_int_cst_lt (max
, min
))
308 set_value_range_to_varying (vr
);
312 t
= t
== VR_RANGE
? VR_ANTI_RANGE
: VR_RANGE
;
315 /* Anti-ranges that can be represented as ranges should be so. */
316 if (t
== VR_ANTI_RANGE
)
318 bool is_min
= vrp_val_is_min (min
);
319 bool is_max
= vrp_val_is_max (max
);
321 if (is_min
&& is_max
)
323 /* We cannot deal with empty ranges, drop to varying.
324 ??? This could be VR_UNDEFINED instead. */
325 set_value_range_to_varying (vr
);
328 else if (TYPE_PRECISION (TREE_TYPE (min
)) == 1
329 && (is_min
|| is_max
))
331 /* Non-empty boolean ranges can always be represented
332 as a singleton range. */
334 min
= max
= vrp_val_max (TREE_TYPE (min
));
336 min
= max
= vrp_val_min (TREE_TYPE (min
));
340 /* As a special exception preserve non-null ranges. */
341 && !(TYPE_UNSIGNED (TREE_TYPE (min
))
342 && integer_zerop (max
)))
344 tree one
= build_int_cst (TREE_TYPE (max
), 1);
345 min
= int_const_binop (PLUS_EXPR
, max
, one
);
346 max
= vrp_val_max (TREE_TYPE (max
));
351 tree one
= build_int_cst (TREE_TYPE (min
), 1);
352 max
= int_const_binop (MINUS_EXPR
, min
, one
);
353 min
= vrp_val_min (TREE_TYPE (min
));
358 /* Do not drop [-INF(OVF), +INF(OVF)] to varying. (OVF) has to be sticky
359 to make sure VRP iteration terminates, otherwise we can get into
362 set_value_range (vr
, t
, min
, max
, equiv
);
365 /* Copy value range FROM into value range TO. */
368 copy_value_range (value_range
*to
, value_range
*from
)
370 set_value_range (to
, from
->type
, from
->min
, from
->max
, from
->equiv
);
373 /* Set value range VR to a single value. This function is only called
374 with values we get from statements, and exists to clear the
375 TREE_OVERFLOW flag. */
378 set_value_range_to_value (value_range
*vr
, tree val
, bitmap equiv
)
380 gcc_assert (is_gimple_min_invariant (val
));
381 if (TREE_OVERFLOW_P (val
))
382 val
= drop_tree_overflow (val
);
383 set_value_range (vr
, VR_RANGE
, val
, val
, equiv
);
386 /* Set value range VR to a non-NULL range of type TYPE. */
389 set_value_range_to_nonnull (value_range
*vr
, tree type
)
391 tree zero
= build_int_cst (type
, 0);
392 set_value_range (vr
, VR_ANTI_RANGE
, zero
, zero
, vr
->equiv
);
396 /* Set value range VR to a NULL range of type TYPE. */
399 set_value_range_to_null (value_range
*vr
, tree type
)
401 set_value_range_to_value (vr
, build_int_cst (type
, 0), vr
->equiv
);
405 /* If abs (min) < abs (max), set VR to [-max, max], if
406 abs (min) >= abs (max), set VR to [-min, min]. */
409 abs_extent_range (value_range
*vr
, tree min
, tree max
)
413 gcc_assert (TREE_CODE (min
) == INTEGER_CST
);
414 gcc_assert (TREE_CODE (max
) == INTEGER_CST
);
415 gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (min
)));
416 gcc_assert (!TYPE_UNSIGNED (TREE_TYPE (min
)));
417 min
= fold_unary (ABS_EXPR
, TREE_TYPE (min
), min
);
418 max
= fold_unary (ABS_EXPR
, TREE_TYPE (max
), max
);
419 if (TREE_OVERFLOW (min
) || TREE_OVERFLOW (max
))
421 set_value_range_to_varying (vr
);
424 cmp
= compare_values (min
, max
);
426 min
= fold_unary (NEGATE_EXPR
, TREE_TYPE (min
), max
);
427 else if (cmp
== 0 || cmp
== 1)
430 min
= fold_unary (NEGATE_EXPR
, TREE_TYPE (min
), min
);
434 set_value_range_to_varying (vr
);
437 set_and_canonicalize_value_range (vr
, VR_RANGE
, min
, max
, NULL
);
440 /* Return true, if VAL1 and VAL2 are equal values for VRP purposes. */
443 vrp_operand_equal_p (const_tree val1
, const_tree val2
)
447 if (!val1
|| !val2
|| !operand_equal_p (val1
, val2
, 0))
452 /* Return true, if the bitmaps B1 and B2 are equal. */
455 vrp_bitmap_equal_p (const_bitmap b1
, const_bitmap b2
)
458 || ((!b1
|| bitmap_empty_p (b1
))
459 && (!b2
|| bitmap_empty_p (b2
)))
461 && bitmap_equal_p (b1
, b2
)));
464 /* Return true if VR is ~[0, 0]. */
467 range_is_nonnull (value_range
*vr
)
469 return vr
->type
== VR_ANTI_RANGE
470 && integer_zerop (vr
->min
)
471 && integer_zerop (vr
->max
);
475 /* Return true if VR is [0, 0]. */
478 range_is_null (value_range
*vr
)
480 return vr
->type
== VR_RANGE
481 && integer_zerop (vr
->min
)
482 && integer_zerop (vr
->max
);
485 /* Return true if max and min of VR are INTEGER_CST. It's not necessary
489 range_int_cst_p (value_range
*vr
)
491 return (vr
->type
== VR_RANGE
492 && TREE_CODE (vr
->max
) == INTEGER_CST
493 && TREE_CODE (vr
->min
) == INTEGER_CST
);
496 /* Return true if VR is a INTEGER_CST singleton. */
499 range_int_cst_singleton_p (value_range
*vr
)
501 return (range_int_cst_p (vr
)
502 && tree_int_cst_equal (vr
->min
, vr
->max
));
505 /* Return true if value range VR involves at least one symbol. */
508 symbolic_range_p (value_range
*vr
)
510 return (!is_gimple_min_invariant (vr
->min
)
511 || !is_gimple_min_invariant (vr
->max
));
514 /* Return the single symbol (an SSA_NAME) contained in T if any, or NULL_TREE
515 otherwise. We only handle additive operations and set NEG to true if the
516 symbol is negated and INV to the invariant part, if any. */
519 get_single_symbol (tree t
, bool *neg
, tree
*inv
)
527 if (TREE_CODE (t
) == PLUS_EXPR
528 || TREE_CODE (t
) == POINTER_PLUS_EXPR
529 || TREE_CODE (t
) == MINUS_EXPR
)
531 if (is_gimple_min_invariant (TREE_OPERAND (t
, 0)))
533 neg_
= (TREE_CODE (t
) == MINUS_EXPR
);
534 inv_
= TREE_OPERAND (t
, 0);
535 t
= TREE_OPERAND (t
, 1);
537 else if (is_gimple_min_invariant (TREE_OPERAND (t
, 1)))
540 inv_
= TREE_OPERAND (t
, 1);
541 t
= TREE_OPERAND (t
, 0);
552 if (TREE_CODE (t
) == NEGATE_EXPR
)
554 t
= TREE_OPERAND (t
, 0);
558 if (TREE_CODE (t
) != SSA_NAME
)
561 if (inv_
&& TREE_OVERFLOW_P (inv_
))
562 inv_
= drop_tree_overflow (inv_
);
569 /* The reverse operation: build a symbolic expression with TYPE
570 from symbol SYM, negated according to NEG, and invariant INV. */
573 build_symbolic_expr (tree type
, tree sym
, bool neg
, tree inv
)
575 const bool pointer_p
= POINTER_TYPE_P (type
);
579 t
= build1 (NEGATE_EXPR
, type
, t
);
581 if (integer_zerop (inv
))
584 return build2 (pointer_p
? POINTER_PLUS_EXPR
: PLUS_EXPR
, type
, t
, inv
);
590 -2 if those are incomparable. */
592 operand_less_p (tree val
, tree val2
)
594 /* LT is folded faster than GE and others. Inline the common case. */
595 if (TREE_CODE (val
) == INTEGER_CST
&& TREE_CODE (val2
) == INTEGER_CST
)
596 return tree_int_cst_lt (val
, val2
);
601 fold_defer_overflow_warnings ();
603 tcmp
= fold_binary_to_constant (LT_EXPR
, boolean_type_node
, val
, val2
);
605 fold_undefer_and_ignore_overflow_warnings ();
608 || TREE_CODE (tcmp
) != INTEGER_CST
)
611 if (!integer_zerop (tcmp
))
618 /* Compare two values VAL1 and VAL2. Return
620 -2 if VAL1 and VAL2 cannot be compared at compile-time,
623 +1 if VAL1 > VAL2, and
626 This is similar to tree_int_cst_compare but supports pointer values
627 and values that cannot be compared at compile time.
629 If STRICT_OVERFLOW_P is not NULL, then set *STRICT_OVERFLOW_P to
630 true if the return value is only valid if we assume that signed
631 overflow is undefined. */
634 compare_values_warnv (tree val1
, tree val2
, bool *strict_overflow_p
)
639 /* Below we rely on the fact that VAL1 and VAL2 are both pointers or
641 gcc_assert (POINTER_TYPE_P (TREE_TYPE (val1
))
642 == POINTER_TYPE_P (TREE_TYPE (val2
)));
644 /* Convert the two values into the same type. This is needed because
645 sizetype causes sign extension even for unsigned types. */
646 val2
= fold_convert (TREE_TYPE (val1
), val2
);
647 STRIP_USELESS_TYPE_CONVERSION (val2
);
649 const bool overflow_undefined
650 = INTEGRAL_TYPE_P (TREE_TYPE (val1
))
651 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (val1
));
654 tree sym1
= get_single_symbol (val1
, &neg1
, &inv1
);
655 tree sym2
= get_single_symbol (val2
, &neg2
, &inv2
);
657 /* If VAL1 and VAL2 are of the form '[-]NAME [+ CST]', return -1 or +1
658 accordingly. If VAL1 and VAL2 don't use the same name, return -2. */
661 /* Both values must use the same name with the same sign. */
662 if (sym1
!= sym2
|| neg1
!= neg2
)
665 /* [-]NAME + CST == [-]NAME + CST. */
669 /* If overflow is defined we cannot simplify more. */
670 if (!overflow_undefined
)
673 if (strict_overflow_p
!= NULL
674 /* Symbolic range building sets TREE_NO_WARNING to declare
675 that overflow doesn't happen. */
676 && (!inv1
|| !TREE_NO_WARNING (val1
))
677 && (!inv2
|| !TREE_NO_WARNING (val2
)))
678 *strict_overflow_p
= true;
681 inv1
= build_int_cst (TREE_TYPE (val1
), 0);
683 inv2
= build_int_cst (TREE_TYPE (val2
), 0);
685 return wi::cmp (wi::to_wide (inv1
), wi::to_wide (inv2
),
686 TYPE_SIGN (TREE_TYPE (val1
)));
689 const bool cst1
= is_gimple_min_invariant (val1
);
690 const bool cst2
= is_gimple_min_invariant (val2
);
692 /* If one is of the form '[-]NAME + CST' and the other is constant, then
693 it might be possible to say something depending on the constants. */
694 if ((sym1
&& inv1
&& cst2
) || (sym2
&& inv2
&& cst1
))
696 if (!overflow_undefined
)
699 if (strict_overflow_p
!= NULL
700 /* Symbolic range building sets TREE_NO_WARNING to declare
701 that overflow doesn't happen. */
702 && (!sym1
|| !TREE_NO_WARNING (val1
))
703 && (!sym2
|| !TREE_NO_WARNING (val2
)))
704 *strict_overflow_p
= true;
706 const signop sgn
= TYPE_SIGN (TREE_TYPE (val1
));
707 tree cst
= cst1
? val1
: val2
;
708 tree inv
= cst1
? inv2
: inv1
;
710 /* Compute the difference between the constants. If it overflows or
711 underflows, this means that we can trivially compare the NAME with
712 it and, consequently, the two values with each other. */
713 wide_int diff
= wi::to_wide (cst
) - wi::to_wide (inv
);
714 if (wi::cmp (0, wi::to_wide (inv
), sgn
)
715 != wi::cmp (diff
, wi::to_wide (cst
), sgn
))
717 const int res
= wi::cmp (wi::to_wide (cst
), wi::to_wide (inv
), sgn
);
718 return cst1
? res
: -res
;
724 /* We cannot say anything more for non-constants. */
728 if (!POINTER_TYPE_P (TREE_TYPE (val1
)))
730 /* We cannot compare overflowed values. */
731 if (TREE_OVERFLOW (val1
) || TREE_OVERFLOW (val2
))
734 return tree_int_cst_compare (val1
, val2
);
740 /* First see if VAL1 and VAL2 are not the same. */
741 if (val1
== val2
|| operand_equal_p (val1
, val2
, 0))
744 /* If VAL1 is a lower address than VAL2, return -1. */
745 if (operand_less_p (val1
, val2
) == 1)
748 /* If VAL1 is a higher address than VAL2, return +1. */
749 if (operand_less_p (val2
, val1
) == 1)
752 /* If VAL1 is different than VAL2, return +2.
753 For integer constants we either have already returned -1 or 1
754 or they are equivalent. We still might succeed in proving
755 something about non-trivial operands. */
756 if (TREE_CODE (val1
) != INTEGER_CST
757 || TREE_CODE (val2
) != INTEGER_CST
)
759 t
= fold_binary_to_constant (NE_EXPR
, boolean_type_node
, val1
, val2
);
760 if (t
&& integer_onep (t
))
768 /* Compare values like compare_values_warnv. */
771 compare_values (tree val1
, tree val2
)
774 return compare_values_warnv (val1
, val2
, &sop
);
778 /* Return 1 if VAL is inside value range MIN <= VAL <= MAX,
779 0 if VAL is not inside [MIN, MAX],
780 -2 if we cannot tell either way.
782 Benchmark compile/20001226-1.c compilation time after changing this
786 value_inside_range (tree val
, tree min
, tree max
)
790 cmp1
= operand_less_p (val
, min
);
796 cmp2
= operand_less_p (max
, val
);
804 /* Return true if value ranges VR0 and VR1 have a non-empty
807 Benchmark compile/20001226-1.c compilation time after changing this
812 value_ranges_intersect_p (value_range
*vr0
, value_range
*vr1
)
814 /* The value ranges do not intersect if the maximum of the first range is
815 less than the minimum of the second range or vice versa.
816 When those relations are unknown, we can't do any better. */
817 if (operand_less_p (vr0
->max
, vr1
->min
) != 0)
819 if (operand_less_p (vr1
->max
, vr0
->min
) != 0)
825 /* Return 1 if [MIN, MAX] includes the value zero, 0 if it does not
826 include the value zero, -2 if we cannot tell. */
829 range_includes_zero_p (tree min
, tree max
)
831 tree zero
= build_int_cst (TREE_TYPE (min
), 0);
832 return value_inside_range (zero
, min
, max
);
835 /* Return true if *VR is know to only contain nonnegative values. */
838 value_range_nonnegative_p (value_range
*vr
)
840 /* Testing for VR_ANTI_RANGE is not useful here as any anti-range
841 which would return a useful value should be encoded as a
843 if (vr
->type
== VR_RANGE
)
845 int result
= compare_values (vr
->min
, integer_zero_node
);
846 return (result
== 0 || result
== 1);
852 /* If *VR has a value rante that is a single constant value return that,
853 otherwise return NULL_TREE. */
856 value_range_constant_singleton (value_range
*vr
)
858 if (vr
->type
== VR_RANGE
859 && vrp_operand_equal_p (vr
->min
, vr
->max
)
860 && is_gimple_min_invariant (vr
->min
))
866 /* Wrapper around int_const_binop. Return true if we can compute the
867 result; i.e. if the operation doesn't overflow or if the overflow is
868 undefined. In the latter case (if the operation overflows and
869 overflow is undefined), then adjust the result to be -INF or +INF
870 depending on CODE, VAL1 and VAL2. Return the value in *RES.
872 Return false for division by zero, for which the result is
876 vrp_int_const_binop (enum tree_code code
, tree val1
, tree val2
, wide_int
*res
)
878 bool overflow
= false;
879 signop sign
= TYPE_SIGN (TREE_TYPE (val1
));
886 wide_int wval2
= wi::to_wide (val2
, TYPE_PRECISION (TREE_TYPE (val1
)));
887 if (wi::neg_p (wval2
))
890 if (code
== RSHIFT_EXPR
)
896 if (code
== RSHIFT_EXPR
)
897 /* It's unclear from the C standard whether shifts can overflow.
898 The following code ignores overflow; perhaps a C standard
899 interpretation ruling is needed. */
900 *res
= wi::rshift (wi::to_wide (val1
), wval2
, sign
);
902 *res
= wi::lshift (wi::to_wide (val1
), wval2
);
907 *res
= wi::mul (wi::to_wide (val1
),
908 wi::to_wide (val2
), sign
, &overflow
);
916 *res
= wi::div_trunc (wi::to_wide (val1
),
917 wi::to_wide (val2
), sign
, &overflow
);
923 *res
= wi::div_floor (wi::to_wide (val1
),
924 wi::to_wide (val2
), sign
, &overflow
);
930 *res
= wi::div_ceil (wi::to_wide (val1
),
931 wi::to_wide (val2
), sign
, &overflow
);
937 *res
= wi::div_round (wi::to_wide (val1
),
938 wi::to_wide (val2
), sign
, &overflow
);
946 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (val1
)))
948 /* If the operation overflowed return -INF or +INF depending
949 on the operation and the combination of signs of the operands. */
950 int sgn1
= tree_int_cst_sgn (val1
);
951 int sgn2
= tree_int_cst_sgn (val2
);
953 /* Notice that we only need to handle the restricted set of
954 operations handled by extract_range_from_binary_expr.
955 Among them, only multiplication, addition and subtraction
956 can yield overflow without overflown operands because we
957 are working with integral types only... except in the
958 case VAL1 = -INF and VAL2 = -1 which overflows to +INF
961 /* For multiplication, the sign of the overflow is given
962 by the comparison of the signs of the operands. */
963 if ((code
== MULT_EXPR
&& sgn1
== sgn2
)
964 /* For addition, the operands must be of the same sign
965 to yield an overflow. Its sign is therefore that
966 of one of the operands, for example the first. */
967 || (code
== PLUS_EXPR
&& sgn1
>= 0)
968 /* For subtraction, operands must be of
969 different signs to yield an overflow. Its sign is
970 therefore that of the first operand or the opposite of
971 that of the second operand. A first operand of 0 counts
972 as positive here, for the corner case 0 - (-INF), which
973 overflows, but must yield +INF. */
974 || (code
== MINUS_EXPR
&& sgn1
>= 0)
975 /* For division, the only case is -INF / -1 = +INF. */
976 || code
== TRUNC_DIV_EXPR
977 || code
== FLOOR_DIV_EXPR
978 || code
== CEIL_DIV_EXPR
979 || code
== EXACT_DIV_EXPR
980 || code
== ROUND_DIV_EXPR
)
981 *res
= wi::max_value (TYPE_PRECISION (TREE_TYPE (val1
)),
982 TYPE_SIGN (TREE_TYPE (val1
)));
984 *res
= wi::min_value (TYPE_PRECISION (TREE_TYPE (val1
)),
985 TYPE_SIGN (TREE_TYPE (val1
)));
993 /* For range VR compute two wide_int bitmasks. In *MAY_BE_NONZERO
994 bitmask if some bit is unset, it means for all numbers in the range
995 the bit is 0, otherwise it might be 0 or 1. In *MUST_BE_NONZERO
996 bitmask if some bit is set, it means for all numbers in the range
997 the bit is 1, otherwise it might be 0 or 1. */
1000 zero_nonzero_bits_from_vr (const tree expr_type
,
1002 wide_int
*may_be_nonzero
,
1003 wide_int
*must_be_nonzero
)
1005 *may_be_nonzero
= wi::minus_one (TYPE_PRECISION (expr_type
));
1006 *must_be_nonzero
= wi::zero (TYPE_PRECISION (expr_type
));
1007 if (!range_int_cst_p (vr
))
1010 if (range_int_cst_singleton_p (vr
))
1012 *may_be_nonzero
= wi::to_wide (vr
->min
);
1013 *must_be_nonzero
= *may_be_nonzero
;
1015 else if (tree_int_cst_sgn (vr
->min
) >= 0
1016 || tree_int_cst_sgn (vr
->max
) < 0)
1018 wide_int xor_mask
= wi::to_wide (vr
->min
) ^ wi::to_wide (vr
->max
);
1019 *may_be_nonzero
= wi::to_wide (vr
->min
) | wi::to_wide (vr
->max
);
1020 *must_be_nonzero
= wi::to_wide (vr
->min
) & wi::to_wide (vr
->max
);
1023 wide_int mask
= wi::mask (wi::floor_log2 (xor_mask
), false,
1024 may_be_nonzero
->get_precision ());
1025 *may_be_nonzero
= *may_be_nonzero
| mask
;
1026 *must_be_nonzero
= wi::bit_and_not (*must_be_nonzero
, mask
);
1033 /* Create two value-ranges in *VR0 and *VR1 from the anti-range *AR
1034 so that *VR0 U *VR1 == *AR. Returns true if that is possible,
1035 false otherwise. If *AR can be represented with a single range
1036 *VR1 will be VR_UNDEFINED. */
1039 ranges_from_anti_range (value_range
*ar
,
1040 value_range
*vr0
, value_range
*vr1
)
1042 tree type
= TREE_TYPE (ar
->min
);
1044 vr0
->type
= VR_UNDEFINED
;
1045 vr1
->type
= VR_UNDEFINED
;
1047 if (ar
->type
!= VR_ANTI_RANGE
1048 || TREE_CODE (ar
->min
) != INTEGER_CST
1049 || TREE_CODE (ar
->max
) != INTEGER_CST
1050 || !vrp_val_min (type
)
1051 || !vrp_val_max (type
))
1054 if (!vrp_val_is_min (ar
->min
))
1056 vr0
->type
= VR_RANGE
;
1057 vr0
->min
= vrp_val_min (type
);
1058 vr0
->max
= wide_int_to_tree (type
, wi::to_wide (ar
->min
) - 1);
1060 if (!vrp_val_is_max (ar
->max
))
1062 vr1
->type
= VR_RANGE
;
1063 vr1
->min
= wide_int_to_tree (type
, wi::to_wide (ar
->max
) + 1);
1064 vr1
->max
= vrp_val_max (type
);
1066 if (vr0
->type
== VR_UNDEFINED
)
1069 vr1
->type
= VR_UNDEFINED
;
1072 return vr0
->type
!= VR_UNDEFINED
;
1075 /* Helper to extract a value-range *VR for a multiplicative operation
1079 extract_range_from_multiplicative_op_1 (value_range
*vr
,
1080 enum tree_code code
,
1081 value_range
*vr0
, value_range
*vr1
)
1083 enum value_range_type rtype
;
1084 wide_int val
, min
, max
;
1087 /* Multiplications, divisions and shifts are a bit tricky to handle,
1088 depending on the mix of signs we have in the two ranges, we
1089 need to operate on different values to get the minimum and
1090 maximum values for the new range. One approach is to figure
1091 out all the variations of range combinations and do the
1094 However, this involves several calls to compare_values and it
1095 is pretty convoluted. It's simpler to do the 4 operations
1096 (MIN0 OP MIN1, MIN0 OP MAX1, MAX0 OP MIN1 and MAX0 OP MAX0 OP
1097 MAX1) and then figure the smallest and largest values to form
1099 gcc_assert (code
== MULT_EXPR
1100 || code
== TRUNC_DIV_EXPR
1101 || code
== FLOOR_DIV_EXPR
1102 || code
== CEIL_DIV_EXPR
1103 || code
== EXACT_DIV_EXPR
1104 || code
== ROUND_DIV_EXPR
1105 || code
== RSHIFT_EXPR
1106 || code
== LSHIFT_EXPR
);
1107 gcc_assert (vr0
->type
== VR_RANGE
1108 && vr0
->type
== vr1
->type
);
1111 type
= TREE_TYPE (vr0
->min
);
1112 signop sgn
= TYPE_SIGN (type
);
1114 /* Compute the 4 cross operations and their minimum and maximum value. */
1115 if (!vrp_int_const_binop (code
, vr0
->min
, vr1
->min
, &val
))
1117 set_value_range_to_varying (vr
);
1122 if (vr1
->max
!= vr1
->min
)
1124 if (!vrp_int_const_binop (code
, vr0
->min
, vr1
->max
, &val
))
1126 set_value_range_to_varying (vr
);
1129 if (wi::lt_p (val
, min
, sgn
))
1131 else if (wi::gt_p (val
, max
, sgn
))
1135 if (vr0
->max
!= vr0
->min
)
1137 if (!vrp_int_const_binop (code
, vr0
->max
, vr1
->min
, &val
))
1139 set_value_range_to_varying (vr
);
1142 if (wi::lt_p (val
, min
, sgn
))
1144 else if (wi::gt_p (val
, max
, sgn
))
1148 if (vr0
->min
!= vr0
->max
&& vr1
->min
!= vr1
->max
)
1150 if (!vrp_int_const_binop (code
, vr0
->max
, vr1
->max
, &val
))
1152 set_value_range_to_varying (vr
);
1155 if (wi::lt_p (val
, min
, sgn
))
1157 else if (wi::gt_p (val
, max
, sgn
))
1161 /* If the new range has its limits swapped around (MIN > MAX),
1162 then the operation caused one of them to wrap around, mark
1163 the new range VARYING. */
1164 if (wi::gt_p (min
, max
, sgn
))
1166 set_value_range_to_varying (vr
);
1170 /* We punt for [-INF, +INF].
1171 We learn nothing when we have INF on both sides.
1172 Note that we do accept [-INF, -INF] and [+INF, +INF]. */
1173 if (wi::eq_p (min
, wi::min_value (TYPE_PRECISION (type
), sgn
))
1174 && wi::eq_p (max
, wi::max_value (TYPE_PRECISION (type
), sgn
)))
1176 set_value_range_to_varying (vr
);
1180 set_value_range (vr
, rtype
,
1181 wide_int_to_tree (type
, min
),
1182 wide_int_to_tree (type
, max
), NULL
);
1185 /* Extract range information from a binary operation CODE based on
1186 the ranges of each of its operands *VR0 and *VR1 with resulting
1187 type EXPR_TYPE. The resulting range is stored in *VR. */
1190 extract_range_from_binary_expr_1 (value_range
*vr
,
1191 enum tree_code code
, tree expr_type
,
1192 value_range
*vr0_
, value_range
*vr1_
)
1194 value_range vr0
= *vr0_
, vr1
= *vr1_
;
1195 value_range vrtem0
= VR_INITIALIZER
, vrtem1
= VR_INITIALIZER
;
1196 enum value_range_type type
;
1197 tree min
= NULL_TREE
, max
= NULL_TREE
;
1200 if (!INTEGRAL_TYPE_P (expr_type
)
1201 && !POINTER_TYPE_P (expr_type
))
1203 set_value_range_to_varying (vr
);
1207 /* Not all binary expressions can be applied to ranges in a
1208 meaningful way. Handle only arithmetic operations. */
1209 if (code
!= PLUS_EXPR
1210 && code
!= MINUS_EXPR
1211 && code
!= POINTER_PLUS_EXPR
1212 && code
!= MULT_EXPR
1213 && code
!= TRUNC_DIV_EXPR
1214 && code
!= FLOOR_DIV_EXPR
1215 && code
!= CEIL_DIV_EXPR
1216 && code
!= EXACT_DIV_EXPR
1217 && code
!= ROUND_DIV_EXPR
1218 && code
!= TRUNC_MOD_EXPR
1219 && code
!= RSHIFT_EXPR
1220 && code
!= LSHIFT_EXPR
1223 && code
!= BIT_AND_EXPR
1224 && code
!= BIT_IOR_EXPR
1225 && code
!= BIT_XOR_EXPR
)
1227 set_value_range_to_varying (vr
);
1231 /* If both ranges are UNDEFINED, so is the result. */
1232 if (vr0
.type
== VR_UNDEFINED
&& vr1
.type
== VR_UNDEFINED
)
1234 set_value_range_to_undefined (vr
);
1237 /* If one of the ranges is UNDEFINED drop it to VARYING for the following
1238 code. At some point we may want to special-case operations that
1239 have UNDEFINED result for all or some value-ranges of the not UNDEFINED
1241 else if (vr0
.type
== VR_UNDEFINED
)
1242 set_value_range_to_varying (&vr0
);
1243 else if (vr1
.type
== VR_UNDEFINED
)
1244 set_value_range_to_varying (&vr1
);
1246 /* We get imprecise results from ranges_from_anti_range when
1247 code is EXACT_DIV_EXPR. We could mask out bits in the resulting
1248 range, but then we also need to hack up vrp_meet. It's just
1249 easier to special case when vr0 is ~[0,0] for EXACT_DIV_EXPR. */
1250 if (code
== EXACT_DIV_EXPR
1251 && vr0
.type
== VR_ANTI_RANGE
1252 && vr0
.min
== vr0
.max
1253 && integer_zerop (vr0
.min
))
1255 set_value_range_to_nonnull (vr
, expr_type
);
1259 /* Now canonicalize anti-ranges to ranges when they are not symbolic
1260 and express ~[] op X as ([]' op X) U ([]'' op X). */
1261 if (vr0
.type
== VR_ANTI_RANGE
1262 && ranges_from_anti_range (&vr0
, &vrtem0
, &vrtem1
))
1264 extract_range_from_binary_expr_1 (vr
, code
, expr_type
, &vrtem0
, vr1_
);
1265 if (vrtem1
.type
!= VR_UNDEFINED
)
1267 value_range vrres
= VR_INITIALIZER
;
1268 extract_range_from_binary_expr_1 (&vrres
, code
, expr_type
,
1270 vrp_meet (vr
, &vrres
);
1274 /* Likewise for X op ~[]. */
1275 if (vr1
.type
== VR_ANTI_RANGE
1276 && ranges_from_anti_range (&vr1
, &vrtem0
, &vrtem1
))
1278 extract_range_from_binary_expr_1 (vr
, code
, expr_type
, vr0_
, &vrtem0
);
1279 if (vrtem1
.type
!= VR_UNDEFINED
)
1281 value_range vrres
= VR_INITIALIZER
;
1282 extract_range_from_binary_expr_1 (&vrres
, code
, expr_type
,
1284 vrp_meet (vr
, &vrres
);
1289 /* The type of the resulting value range defaults to VR0.TYPE. */
1292 /* Refuse to operate on VARYING ranges, ranges of different kinds
1293 and symbolic ranges. As an exception, we allow BIT_{AND,IOR}
1294 because we may be able to derive a useful range even if one of
1295 the operands is VR_VARYING or symbolic range. Similarly for
1296 divisions, MIN/MAX and PLUS/MINUS.
1298 TODO, we may be able to derive anti-ranges in some cases. */
1299 if (code
!= BIT_AND_EXPR
1300 && code
!= BIT_IOR_EXPR
1301 && code
!= TRUNC_DIV_EXPR
1302 && code
!= FLOOR_DIV_EXPR
1303 && code
!= CEIL_DIV_EXPR
1304 && code
!= EXACT_DIV_EXPR
1305 && code
!= ROUND_DIV_EXPR
1306 && code
!= TRUNC_MOD_EXPR
1309 && code
!= PLUS_EXPR
1310 && code
!= MINUS_EXPR
1311 && code
!= RSHIFT_EXPR
1312 && (vr0
.type
== VR_VARYING
1313 || vr1
.type
== VR_VARYING
1314 || vr0
.type
!= vr1
.type
1315 || symbolic_range_p (&vr0
)
1316 || symbolic_range_p (&vr1
)))
1318 set_value_range_to_varying (vr
);
1322 /* Now evaluate the expression to determine the new range. */
1323 if (POINTER_TYPE_P (expr_type
))
1325 if (code
== MIN_EXPR
|| code
== MAX_EXPR
)
1327 /* For MIN/MAX expressions with pointers, we only care about
1328 nullness, if both are non null, then the result is nonnull.
1329 If both are null, then the result is null. Otherwise they
1331 if (range_is_nonnull (&vr0
) && range_is_nonnull (&vr1
))
1332 set_value_range_to_nonnull (vr
, expr_type
);
1333 else if (range_is_null (&vr0
) && range_is_null (&vr1
))
1334 set_value_range_to_null (vr
, expr_type
);
1336 set_value_range_to_varying (vr
);
1338 else if (code
== POINTER_PLUS_EXPR
)
1340 /* For pointer types, we are really only interested in asserting
1341 whether the expression evaluates to non-NULL. */
1342 if (range_is_nonnull (&vr0
) || range_is_nonnull (&vr1
))
1343 set_value_range_to_nonnull (vr
, expr_type
);
1344 else if (range_is_null (&vr0
) && range_is_null (&vr1
))
1345 set_value_range_to_null (vr
, expr_type
);
1347 set_value_range_to_varying (vr
);
1349 else if (code
== BIT_AND_EXPR
)
1351 /* For pointer types, we are really only interested in asserting
1352 whether the expression evaluates to non-NULL. */
1353 if (range_is_nonnull (&vr0
) && range_is_nonnull (&vr1
))
1354 set_value_range_to_nonnull (vr
, expr_type
);
1355 else if (range_is_null (&vr0
) || range_is_null (&vr1
))
1356 set_value_range_to_null (vr
, expr_type
);
1358 set_value_range_to_varying (vr
);
1361 set_value_range_to_varying (vr
);
1366 /* For integer ranges, apply the operation to each end of the
1367 range and see what we end up with. */
1368 if (code
== PLUS_EXPR
|| code
== MINUS_EXPR
)
1370 const bool minus_p
= (code
== MINUS_EXPR
);
1371 tree min_op0
= vr0
.min
;
1372 tree min_op1
= minus_p
? vr1
.max
: vr1
.min
;
1373 tree max_op0
= vr0
.max
;
1374 tree max_op1
= minus_p
? vr1
.min
: vr1
.max
;
1375 tree sym_min_op0
= NULL_TREE
;
1376 tree sym_min_op1
= NULL_TREE
;
1377 tree sym_max_op0
= NULL_TREE
;
1378 tree sym_max_op1
= NULL_TREE
;
1379 bool neg_min_op0
, neg_min_op1
, neg_max_op0
, neg_max_op1
;
1381 /* If we have a PLUS or MINUS with two VR_RANGEs, either constant or
1382 single-symbolic ranges, try to compute the precise resulting range,
1383 but only if we know that this resulting range will also be constant
1384 or single-symbolic. */
1385 if (vr0
.type
== VR_RANGE
&& vr1
.type
== VR_RANGE
1386 && (TREE_CODE (min_op0
) == INTEGER_CST
1388 = get_single_symbol (min_op0
, &neg_min_op0
, &min_op0
)))
1389 && (TREE_CODE (min_op1
) == INTEGER_CST
1391 = get_single_symbol (min_op1
, &neg_min_op1
, &min_op1
)))
1392 && (!(sym_min_op0
&& sym_min_op1
)
1393 || (sym_min_op0
== sym_min_op1
1394 && neg_min_op0
== (minus_p
? neg_min_op1
: !neg_min_op1
)))
1395 && (TREE_CODE (max_op0
) == INTEGER_CST
1397 = get_single_symbol (max_op0
, &neg_max_op0
, &max_op0
)))
1398 && (TREE_CODE (max_op1
) == INTEGER_CST
1400 = get_single_symbol (max_op1
, &neg_max_op1
, &max_op1
)))
1401 && (!(sym_max_op0
&& sym_max_op1
)
1402 || (sym_max_op0
== sym_max_op1
1403 && neg_max_op0
== (minus_p
? neg_max_op1
: !neg_max_op1
))))
1405 const signop sgn
= TYPE_SIGN (expr_type
);
1406 const unsigned int prec
= TYPE_PRECISION (expr_type
);
1407 wide_int type_min
, type_max
, wmin
, wmax
;
1411 /* Get the lower and upper bounds of the type. */
1412 if (TYPE_OVERFLOW_WRAPS (expr_type
))
1414 type_min
= wi::min_value (prec
, sgn
);
1415 type_max
= wi::max_value (prec
, sgn
);
1419 type_min
= wi::to_wide (vrp_val_min (expr_type
));
1420 type_max
= wi::to_wide (vrp_val_max (expr_type
));
1423 /* Combine the lower bounds, if any. */
1424 if (min_op0
&& min_op1
)
1428 wmin
= wi::to_wide (min_op0
) - wi::to_wide (min_op1
);
1430 /* Check for overflow. */
1431 if (wi::cmp (0, wi::to_wide (min_op1
), sgn
)
1432 != wi::cmp (wmin
, wi::to_wide (min_op0
), sgn
))
1433 min_ovf
= wi::cmp (wi::to_wide (min_op0
),
1434 wi::to_wide (min_op1
), sgn
);
1438 wmin
= wi::to_wide (min_op0
) + wi::to_wide (min_op1
);
1440 /* Check for overflow. */
1441 if (wi::cmp (wi::to_wide (min_op1
), 0, sgn
)
1442 != wi::cmp (wmin
, wi::to_wide (min_op0
), sgn
))
1443 min_ovf
= wi::cmp (wi::to_wide (min_op0
), wmin
, sgn
);
1447 wmin
= wi::to_wide (min_op0
);
1452 wmin
= -wi::to_wide (min_op1
);
1454 /* Check for overflow. */
1456 && wi::neg_p (wi::to_wide (min_op1
))
1457 && wi::neg_p (wmin
))
1459 else if (sgn
== UNSIGNED
&& wi::to_wide (min_op1
) != 0)
1463 wmin
= wi::to_wide (min_op1
);
1466 wmin
= wi::shwi (0, prec
);
1468 /* Combine the upper bounds, if any. */
1469 if (max_op0
&& max_op1
)
1473 wmax
= wi::to_wide (max_op0
) - wi::to_wide (max_op1
);
1475 /* Check for overflow. */
1476 if (wi::cmp (0, wi::to_wide (max_op1
), sgn
)
1477 != wi::cmp (wmax
, wi::to_wide (max_op0
), sgn
))
1478 max_ovf
= wi::cmp (wi::to_wide (max_op0
),
1479 wi::to_wide (max_op1
), sgn
);
1483 wmax
= wi::to_wide (max_op0
) + wi::to_wide (max_op1
);
1485 if (wi::cmp (wi::to_wide (max_op1
), 0, sgn
)
1486 != wi::cmp (wmax
, wi::to_wide (max_op0
), sgn
))
1487 max_ovf
= wi::cmp (wi::to_wide (max_op0
), wmax
, sgn
);
1491 wmax
= wi::to_wide (max_op0
);
1496 wmax
= -wi::to_wide (max_op1
);
1498 /* Check for overflow. */
1500 && wi::neg_p (wi::to_wide (max_op1
))
1501 && wi::neg_p (wmax
))
1503 else if (sgn
== UNSIGNED
&& wi::to_wide (max_op1
) != 0)
1507 wmax
= wi::to_wide (max_op1
);
1510 wmax
= wi::shwi (0, prec
);
1512 /* Check for type overflow. */
1515 if (wi::cmp (wmin
, type_min
, sgn
) == -1)
1517 else if (wi::cmp (wmin
, type_max
, sgn
) == 1)
1522 if (wi::cmp (wmax
, type_min
, sgn
) == -1)
1524 else if (wi::cmp (wmax
, type_max
, sgn
) == 1)
1528 /* If we have overflow for the constant part and the resulting
1529 range will be symbolic, drop to VR_VARYING. */
1530 if ((min_ovf
&& sym_min_op0
!= sym_min_op1
)
1531 || (max_ovf
&& sym_max_op0
!= sym_max_op1
))
1533 set_value_range_to_varying (vr
);
1537 if (TYPE_OVERFLOW_WRAPS (expr_type
))
1539 /* If overflow wraps, truncate the values and adjust the
1540 range kind and bounds appropriately. */
1541 wide_int tmin
= wide_int::from (wmin
, prec
, sgn
);
1542 wide_int tmax
= wide_int::from (wmax
, prec
, sgn
);
1543 if (min_ovf
== max_ovf
)
1545 /* No overflow or both overflow or underflow. The
1546 range kind stays VR_RANGE. */
1547 min
= wide_int_to_tree (expr_type
, tmin
);
1548 max
= wide_int_to_tree (expr_type
, tmax
);
1550 else if ((min_ovf
== -1 && max_ovf
== 0)
1551 || (max_ovf
== 1 && min_ovf
== 0))
1553 /* Min underflow or max overflow. The range kind
1554 changes to VR_ANTI_RANGE. */
1555 bool covers
= false;
1556 wide_int tem
= tmin
;
1557 type
= VR_ANTI_RANGE
;
1559 if (wi::cmp (tmin
, tmax
, sgn
) < 0)
1562 if (wi::cmp (tmax
, tem
, sgn
) > 0)
1564 /* If the anti-range would cover nothing, drop to varying.
1565 Likewise if the anti-range bounds are outside of the
1567 if (covers
|| wi::cmp (tmin
, tmax
, sgn
) > 0)
1569 set_value_range_to_varying (vr
);
1572 min
= wide_int_to_tree (expr_type
, tmin
);
1573 max
= wide_int_to_tree (expr_type
, tmax
);
1577 /* Other underflow and/or overflow, drop to VR_VARYING. */
1578 set_value_range_to_varying (vr
);
1584 /* If overflow does not wrap, saturate to the types min/max
1587 min
= wide_int_to_tree (expr_type
, type_min
);
1588 else if (min_ovf
== 1)
1589 min
= wide_int_to_tree (expr_type
, type_max
);
1591 min
= wide_int_to_tree (expr_type
, wmin
);
1594 max
= wide_int_to_tree (expr_type
, type_min
);
1595 else if (max_ovf
== 1)
1596 max
= wide_int_to_tree (expr_type
, type_max
);
1598 max
= wide_int_to_tree (expr_type
, wmax
);
1601 /* If the result lower bound is constant, we're done;
1602 otherwise, build the symbolic lower bound. */
1603 if (sym_min_op0
== sym_min_op1
)
1605 else if (sym_min_op0
)
1606 min
= build_symbolic_expr (expr_type
, sym_min_op0
,
1608 else if (sym_min_op1
)
1610 /* We may not negate if that might introduce
1611 undefined overflow. */
1614 || TYPE_OVERFLOW_WRAPS (expr_type
))
1615 min
= build_symbolic_expr (expr_type
, sym_min_op1
,
1616 neg_min_op1
^ minus_p
, min
);
1621 /* Likewise for the upper bound. */
1622 if (sym_max_op0
== sym_max_op1
)
1624 else if (sym_max_op0
)
1625 max
= build_symbolic_expr (expr_type
, sym_max_op0
,
1627 else if (sym_max_op1
)
1629 /* We may not negate if that might introduce
1630 undefined overflow. */
1633 || TYPE_OVERFLOW_WRAPS (expr_type
))
1634 max
= build_symbolic_expr (expr_type
, sym_max_op1
,
1635 neg_max_op1
^ minus_p
, max
);
1642 /* For other cases, for example if we have a PLUS_EXPR with two
1643 VR_ANTI_RANGEs, drop to VR_VARYING. It would take more effort
1644 to compute a precise range for such a case.
1645 ??? General even mixed range kind operations can be expressed
1646 by for example transforming ~[3, 5] + [1, 2] to range-only
1647 operations and a union primitive:
1648 [-INF, 2] + [1, 2] U [5, +INF] + [1, 2]
1649 [-INF+1, 4] U [6, +INF(OVF)]
1650 though usually the union is not exactly representable with
1651 a single range or anti-range as the above is
1652 [-INF+1, +INF(OVF)] intersected with ~[5, 5]
1653 but one could use a scheme similar to equivalences for this. */
1654 set_value_range_to_varying (vr
);
1658 else if (code
== MIN_EXPR
1659 || code
== MAX_EXPR
)
1661 if (vr0
.type
== VR_RANGE
1662 && !symbolic_range_p (&vr0
))
1665 if (vr1
.type
== VR_RANGE
1666 && !symbolic_range_p (&vr1
))
1668 /* For operations that make the resulting range directly
1669 proportional to the original ranges, apply the operation to
1670 the same end of each range. */
1671 min
= int_const_binop (code
, vr0
.min
, vr1
.min
);
1672 max
= int_const_binop (code
, vr0
.max
, vr1
.max
);
1674 else if (code
== MIN_EXPR
)
1676 min
= vrp_val_min (expr_type
);
1679 else if (code
== MAX_EXPR
)
1682 max
= vrp_val_max (expr_type
);
1685 else if (vr1
.type
== VR_RANGE
1686 && !symbolic_range_p (&vr1
))
1689 if (code
== MIN_EXPR
)
1691 min
= vrp_val_min (expr_type
);
1694 else if (code
== MAX_EXPR
)
1697 max
= vrp_val_max (expr_type
);
1702 set_value_range_to_varying (vr
);
1706 else if (code
== MULT_EXPR
)
1708 /* Fancy code so that with unsigned, [-3,-1]*[-3,-1] does not
1709 drop to varying. This test requires 2*prec bits if both
1710 operands are signed and 2*prec + 2 bits if either is not. */
1712 signop sign
= TYPE_SIGN (expr_type
);
1713 unsigned int prec
= TYPE_PRECISION (expr_type
);
1715 if (!range_int_cst_p (&vr0
)
1716 || !range_int_cst_p (&vr1
))
1718 set_value_range_to_varying (vr
);
1722 if (TYPE_OVERFLOW_WRAPS (expr_type
))
1724 typedef FIXED_WIDE_INT (WIDE_INT_MAX_PRECISION
* 2) vrp_int
;
1725 typedef generic_wide_int
1726 <wi::extended_tree
<WIDE_INT_MAX_PRECISION
* 2> > vrp_int_cst
;
1727 vrp_int sizem1
= wi::mask
<vrp_int
> (prec
, false);
1728 vrp_int size
= sizem1
+ 1;
1730 /* Extend the values using the sign of the result to PREC2.
1731 From here on out, everthing is just signed math no matter
1732 what the input types were. */
1733 vrp_int min0
= vrp_int_cst (vr0
.min
);
1734 vrp_int max0
= vrp_int_cst (vr0
.max
);
1735 vrp_int min1
= vrp_int_cst (vr1
.min
);
1736 vrp_int max1
= vrp_int_cst (vr1
.max
);
1737 /* Canonicalize the intervals. */
1738 if (sign
== UNSIGNED
)
1740 if (wi::ltu_p (size
, min0
+ max0
))
1746 if (wi::ltu_p (size
, min1
+ max1
))
1753 vrp_int prod0
= min0
* min1
;
1754 vrp_int prod1
= min0
* max1
;
1755 vrp_int prod2
= max0
* min1
;
1756 vrp_int prod3
= max0
* max1
;
1758 /* Sort the 4 products so that min is in prod0 and max is in
1760 /* min0min1 > max0max1 */
1762 std::swap (prod0
, prod3
);
1764 /* min0max1 > max0min1 */
1766 std::swap (prod1
, prod2
);
1769 std::swap (prod0
, prod1
);
1772 std::swap (prod2
, prod3
);
1774 /* diff = max - min. */
1775 prod2
= prod3
- prod0
;
1776 if (wi::geu_p (prod2
, sizem1
))
1778 /* the range covers all values. */
1779 set_value_range_to_varying (vr
);
1783 /* The following should handle the wrapping and selecting
1784 VR_ANTI_RANGE for us. */
1785 min
= wide_int_to_tree (expr_type
, prod0
);
1786 max
= wide_int_to_tree (expr_type
, prod3
);
1787 set_and_canonicalize_value_range (vr
, VR_RANGE
, min
, max
, NULL
);
1791 /* If we have an unsigned MULT_EXPR with two VR_ANTI_RANGEs,
1792 drop to VR_VARYING. It would take more effort to compute a
1793 precise range for such a case. For example, if we have
1794 op0 == 65536 and op1 == 65536 with their ranges both being
1795 ~[0,0] on a 32-bit machine, we would have op0 * op1 == 0, so
1796 we cannot claim that the product is in ~[0,0]. Note that we
1797 are guaranteed to have vr0.type == vr1.type at this
1799 if (vr0
.type
== VR_ANTI_RANGE
1800 && !TYPE_OVERFLOW_UNDEFINED (expr_type
))
1802 set_value_range_to_varying (vr
);
1806 extract_range_from_multiplicative_op_1 (vr
, code
, &vr0
, &vr1
);
1809 else if (code
== RSHIFT_EXPR
1810 || code
== LSHIFT_EXPR
)
1812 /* If we have a RSHIFT_EXPR with any shift values outside [0..prec-1],
1813 then drop to VR_VARYING. Outside of this range we get undefined
1814 behavior from the shift operation. We cannot even trust
1815 SHIFT_COUNT_TRUNCATED at this stage, because that applies to rtl
1816 shifts, and the operation at the tree level may be widened. */
1817 if (range_int_cst_p (&vr1
)
1818 && compare_tree_int (vr1
.min
, 0) >= 0
1819 && compare_tree_int (vr1
.max
, TYPE_PRECISION (expr_type
)) == -1)
1821 if (code
== RSHIFT_EXPR
)
1823 /* Even if vr0 is VARYING or otherwise not usable, we can derive
1824 useful ranges just from the shift count. E.g.
1825 x >> 63 for signed 64-bit x is always [-1, 0]. */
1826 if (vr0
.type
!= VR_RANGE
|| symbolic_range_p (&vr0
))
1828 vr0
.type
= type
= VR_RANGE
;
1829 vr0
.min
= vrp_val_min (expr_type
);
1830 vr0
.max
= vrp_val_max (expr_type
);
1832 extract_range_from_multiplicative_op_1 (vr
, code
, &vr0
, &vr1
);
1835 /* We can map lshifts by constants to MULT_EXPR handling. */
1836 else if (code
== LSHIFT_EXPR
1837 && range_int_cst_singleton_p (&vr1
))
1839 bool saved_flag_wrapv
;
1840 value_range vr1p
= VR_INITIALIZER
;
1841 vr1p
.type
= VR_RANGE
;
1842 vr1p
.min
= (wide_int_to_tree
1844 wi::set_bit_in_zero (tree_to_shwi (vr1
.min
),
1845 TYPE_PRECISION (expr_type
))));
1846 vr1p
.max
= vr1p
.min
;
1847 /* We have to use a wrapping multiply though as signed overflow
1848 on lshifts is implementation defined in C89. */
1849 saved_flag_wrapv
= flag_wrapv
;
1851 extract_range_from_binary_expr_1 (vr
, MULT_EXPR
, expr_type
,
1853 flag_wrapv
= saved_flag_wrapv
;
1856 else if (code
== LSHIFT_EXPR
1857 && range_int_cst_p (&vr0
))
1859 int prec
= TYPE_PRECISION (expr_type
);
1860 int overflow_pos
= prec
;
1862 wide_int low_bound
, high_bound
;
1863 bool uns
= TYPE_UNSIGNED (expr_type
);
1864 bool in_bounds
= false;
1869 bound_shift
= overflow_pos
- tree_to_shwi (vr1
.max
);
1870 /* If bound_shift == HOST_BITS_PER_WIDE_INT, the llshift can
1871 overflow. However, for that to happen, vr1.max needs to be
1872 zero, which means vr1 is a singleton range of zero, which
1873 means it should be handled by the previous LSHIFT_EXPR
1875 wide_int bound
= wi::set_bit_in_zero (bound_shift
, prec
);
1876 wide_int complement
= ~(bound
- 1);
1881 high_bound
= complement
;
1882 if (wi::ltu_p (wi::to_wide (vr0
.max
), low_bound
))
1884 /* [5, 6] << [1, 2] == [10, 24]. */
1885 /* We're shifting out only zeroes, the value increases
1889 else if (wi::ltu_p (high_bound
, wi::to_wide (vr0
.min
)))
1891 /* [0xffffff00, 0xffffffff] << [1, 2]
1892 == [0xfffffc00, 0xfffffffe]. */
1893 /* We're shifting out only ones, the value decreases
1900 /* [-1, 1] << [1, 2] == [-4, 4]. */
1901 low_bound
= complement
;
1903 if (wi::lts_p (wi::to_wide (vr0
.max
), high_bound
)
1904 && wi::lts_p (low_bound
, wi::to_wide (vr0
.min
)))
1906 /* For non-negative numbers, we're shifting out only
1907 zeroes, the value increases monotonically.
1908 For negative numbers, we're shifting out only ones, the
1909 value decreases monotomically. */
1916 extract_range_from_multiplicative_op_1 (vr
, code
, &vr0
, &vr1
);
1921 set_value_range_to_varying (vr
);
1924 else if (code
== TRUNC_DIV_EXPR
1925 || code
== FLOOR_DIV_EXPR
1926 || code
== CEIL_DIV_EXPR
1927 || code
== EXACT_DIV_EXPR
1928 || code
== ROUND_DIV_EXPR
)
1930 if (vr0
.type
!= VR_RANGE
|| symbolic_range_p (&vr0
))
1932 /* For division, if op1 has VR_RANGE but op0 does not, something
1933 can be deduced just from that range. Say [min, max] / [4, max]
1934 gives [min / 4, max / 4] range. */
1935 if (vr1
.type
== VR_RANGE
1936 && !symbolic_range_p (&vr1
)
1937 && range_includes_zero_p (vr1
.min
, vr1
.max
) == 0)
1939 vr0
.type
= type
= VR_RANGE
;
1940 vr0
.min
= vrp_val_min (expr_type
);
1941 vr0
.max
= vrp_val_max (expr_type
);
1945 set_value_range_to_varying (vr
);
1950 /* For divisions, if flag_non_call_exceptions is true, we must
1951 not eliminate a division by zero. */
1952 if (cfun
->can_throw_non_call_exceptions
1953 && (vr1
.type
!= VR_RANGE
1954 || range_includes_zero_p (vr1
.min
, vr1
.max
) != 0))
1956 set_value_range_to_varying (vr
);
1960 /* For divisions, if op0 is VR_RANGE, we can deduce a range
1961 even if op1 is VR_VARYING, VR_ANTI_RANGE, symbolic or can
1963 if (vr0
.type
== VR_RANGE
1964 && (vr1
.type
!= VR_RANGE
1965 || range_includes_zero_p (vr1
.min
, vr1
.max
) != 0))
1967 tree zero
= build_int_cst (TREE_TYPE (vr0
.min
), 0);
1972 if (TYPE_UNSIGNED (expr_type
)
1973 || value_range_nonnegative_p (&vr1
))
1975 /* For unsigned division or when divisor is known
1976 to be non-negative, the range has to cover
1977 all numbers from 0 to max for positive max
1978 and all numbers from min to 0 for negative min. */
1979 cmp
= compare_values (vr0
.max
, zero
);
1982 /* When vr0.max < 0, vr1.min != 0 and value
1983 ranges for dividend and divisor are available. */
1984 if (vr1
.type
== VR_RANGE
1985 && !symbolic_range_p (&vr0
)
1986 && !symbolic_range_p (&vr1
)
1987 && compare_values (vr1
.min
, zero
) != 0)
1988 max
= int_const_binop (code
, vr0
.max
, vr1
.min
);
1992 else if (cmp
== 0 || cmp
== 1)
1996 cmp
= compare_values (vr0
.min
, zero
);
1999 /* For unsigned division when value ranges for dividend
2000 and divisor are available. */
2001 if (vr1
.type
== VR_RANGE
2002 && !symbolic_range_p (&vr0
)
2003 && !symbolic_range_p (&vr1
)
2004 && compare_values (vr1
.max
, zero
) != 0)
2005 min
= int_const_binop (code
, vr0
.min
, vr1
.max
);
2009 else if (cmp
== 0 || cmp
== -1)
2016 /* Otherwise the range is -max .. max or min .. -min
2017 depending on which bound is bigger in absolute value,
2018 as the division can change the sign. */
2019 abs_extent_range (vr
, vr0
.min
, vr0
.max
);
2022 if (type
== VR_VARYING
)
2024 set_value_range_to_varying (vr
);
2028 else if (!symbolic_range_p (&vr0
) && !symbolic_range_p (&vr1
))
2030 extract_range_from_multiplicative_op_1 (vr
, code
, &vr0
, &vr1
);
2034 else if (code
== TRUNC_MOD_EXPR
)
2036 if (range_is_null (&vr1
))
2038 set_value_range_to_undefined (vr
);
2041 /* ABS (A % B) < ABS (B) and either
2042 0 <= A % B <= A or A <= A % B <= 0. */
2044 signop sgn
= TYPE_SIGN (expr_type
);
2045 unsigned int prec
= TYPE_PRECISION (expr_type
);
2046 wide_int wmin
, wmax
, tmp
;
2047 if (vr1
.type
== VR_RANGE
&& !symbolic_range_p (&vr1
))
2049 wmax
= wi::to_wide (vr1
.max
) - 1;
2052 tmp
= -1 - wi::to_wide (vr1
.min
);
2053 wmax
= wi::smax (wmax
, tmp
);
2058 wmax
= wi::max_value (prec
, sgn
);
2059 /* X % INT_MIN may be INT_MAX. */
2060 if (sgn
== UNSIGNED
)
2064 if (sgn
== UNSIGNED
)
2065 wmin
= wi::zero (prec
);
2069 if (vr0
.type
== VR_RANGE
&& TREE_CODE (vr0
.min
) == INTEGER_CST
)
2071 tmp
= wi::to_wide (vr0
.min
);
2072 if (wi::gts_p (tmp
, 0))
2073 tmp
= wi::zero (prec
);
2074 wmin
= wi::smax (wmin
, tmp
);
2078 if (vr0
.type
== VR_RANGE
&& TREE_CODE (vr0
.max
) == INTEGER_CST
)
2080 tmp
= wi::to_wide (vr0
.max
);
2081 if (sgn
== SIGNED
&& wi::neg_p (tmp
))
2082 tmp
= wi::zero (prec
);
2083 wmax
= wi::min (wmax
, tmp
, sgn
);
2086 min
= wide_int_to_tree (expr_type
, wmin
);
2087 max
= wide_int_to_tree (expr_type
, wmax
);
2089 else if (code
== BIT_AND_EXPR
|| code
== BIT_IOR_EXPR
|| code
== BIT_XOR_EXPR
)
2091 bool int_cst_range0
, int_cst_range1
;
2092 wide_int may_be_nonzero0
, may_be_nonzero1
;
2093 wide_int must_be_nonzero0
, must_be_nonzero1
;
2095 int_cst_range0
= zero_nonzero_bits_from_vr (expr_type
, &vr0
,
2098 int_cst_range1
= zero_nonzero_bits_from_vr (expr_type
, &vr1
,
2102 if (code
== BIT_AND_EXPR
|| code
== BIT_IOR_EXPR
)
2104 value_range
*vr0p
= NULL
, *vr1p
= NULL
;
2105 if (range_int_cst_singleton_p (&vr1
))
2110 else if (range_int_cst_singleton_p (&vr0
))
2115 /* For op & or | attempt to optimize:
2116 [x, y] op z into [x op z, y op z]
2117 if z is a constant which (for op | its bitwise not) has n
2118 consecutive least significant bits cleared followed by m 1
2119 consecutive bits set immediately above it and either
2120 m + n == precision, or (x >> (m + n)) == (y >> (m + n)).
2121 The least significant n bits of all the values in the range are
2122 cleared or set, the m bits above it are preserved and any bits
2123 above these are required to be the same for all values in the
2125 if (vr0p
&& range_int_cst_p (vr0p
))
2127 wide_int w
= wi::to_wide (vr1p
->min
);
2129 if (code
== BIT_IOR_EXPR
)
2131 if (wi::eq_p (w
, 0))
2132 n
= TYPE_PRECISION (expr_type
);
2136 w
= ~(w
| wi::mask (n
, false, w
.get_precision ()));
2137 if (wi::eq_p (w
, 0))
2138 m
= TYPE_PRECISION (expr_type
) - n
;
2140 m
= wi::ctz (w
) - n
;
2142 wide_int mask
= wi::mask (m
+ n
, true, w
.get_precision ());
2143 if ((mask
& wi::to_wide (vr0p
->min
))
2144 == (mask
& wi::to_wide (vr0p
->max
)))
2146 min
= int_const_binop (code
, vr0p
->min
, vr1p
->min
);
2147 max
= int_const_binop (code
, vr0p
->max
, vr1p
->min
);
2154 /* Optimized above already. */;
2155 else if (code
== BIT_AND_EXPR
)
2157 min
= wide_int_to_tree (expr_type
,
2158 must_be_nonzero0
& must_be_nonzero1
);
2159 wide_int wmax
= may_be_nonzero0
& may_be_nonzero1
;
2160 /* If both input ranges contain only negative values we can
2161 truncate the result range maximum to the minimum of the
2162 input range maxima. */
2163 if (int_cst_range0
&& int_cst_range1
2164 && tree_int_cst_sgn (vr0
.max
) < 0
2165 && tree_int_cst_sgn (vr1
.max
) < 0)
2167 wmax
= wi::min (wmax
, wi::to_wide (vr0
.max
),
2168 TYPE_SIGN (expr_type
));
2169 wmax
= wi::min (wmax
, wi::to_wide (vr1
.max
),
2170 TYPE_SIGN (expr_type
));
2172 /* If either input range contains only non-negative values
2173 we can truncate the result range maximum to the respective
2174 maximum of the input range. */
2175 if (int_cst_range0
&& tree_int_cst_sgn (vr0
.min
) >= 0)
2176 wmax
= wi::min (wmax
, wi::to_wide (vr0
.max
),
2177 TYPE_SIGN (expr_type
));
2178 if (int_cst_range1
&& tree_int_cst_sgn (vr1
.min
) >= 0)
2179 wmax
= wi::min (wmax
, wi::to_wide (vr1
.max
),
2180 TYPE_SIGN (expr_type
));
2181 max
= wide_int_to_tree (expr_type
, wmax
);
2182 cmp
= compare_values (min
, max
);
2183 /* PR68217: In case of signed & sign-bit-CST should
2184 result in [-INF, 0] instead of [-INF, INF]. */
2185 if (cmp
== -2 || cmp
== 1)
2188 = wi::set_bit_in_zero (TYPE_PRECISION (expr_type
) - 1,
2189 TYPE_PRECISION (expr_type
));
2190 if (!TYPE_UNSIGNED (expr_type
)
2192 && value_range_constant_singleton (&vr0
)
2193 && !wi::cmps (wi::to_wide (vr0
.min
), sign_bit
))
2195 && value_range_constant_singleton (&vr1
)
2196 && !wi::cmps (wi::to_wide (vr1
.min
), sign_bit
))))
2198 min
= TYPE_MIN_VALUE (expr_type
);
2199 max
= build_int_cst (expr_type
, 0);
2203 else if (code
== BIT_IOR_EXPR
)
2205 max
= wide_int_to_tree (expr_type
,
2206 may_be_nonzero0
| may_be_nonzero1
);
2207 wide_int wmin
= must_be_nonzero0
| must_be_nonzero1
;
2208 /* If the input ranges contain only positive values we can
2209 truncate the minimum of the result range to the maximum
2210 of the input range minima. */
2211 if (int_cst_range0
&& int_cst_range1
2212 && tree_int_cst_sgn (vr0
.min
) >= 0
2213 && tree_int_cst_sgn (vr1
.min
) >= 0)
2215 wmin
= wi::max (wmin
, wi::to_wide (vr0
.min
),
2216 TYPE_SIGN (expr_type
));
2217 wmin
= wi::max (wmin
, wi::to_wide (vr1
.min
),
2218 TYPE_SIGN (expr_type
));
2220 /* If either input range contains only negative values
2221 we can truncate the minimum of the result range to the
2222 respective minimum range. */
2223 if (int_cst_range0
&& tree_int_cst_sgn (vr0
.max
) < 0)
2224 wmin
= wi::max (wmin
, wi::to_wide (vr0
.min
),
2225 TYPE_SIGN (expr_type
));
2226 if (int_cst_range1
&& tree_int_cst_sgn (vr1
.max
) < 0)
2227 wmin
= wi::max (wmin
, wi::to_wide (vr1
.min
),
2228 TYPE_SIGN (expr_type
));
2229 min
= wide_int_to_tree (expr_type
, wmin
);
2231 else if (code
== BIT_XOR_EXPR
)
2233 wide_int result_zero_bits
= ((must_be_nonzero0
& must_be_nonzero1
)
2234 | ~(may_be_nonzero0
| may_be_nonzero1
));
2235 wide_int result_one_bits
2236 = (wi::bit_and_not (must_be_nonzero0
, may_be_nonzero1
)
2237 | wi::bit_and_not (must_be_nonzero1
, may_be_nonzero0
));
2238 max
= wide_int_to_tree (expr_type
, ~result_zero_bits
);
2239 min
= wide_int_to_tree (expr_type
, result_one_bits
);
2240 /* If the range has all positive or all negative values the
2241 result is better than VARYING. */
2242 if (tree_int_cst_sgn (min
) < 0
2243 || tree_int_cst_sgn (max
) >= 0)
2246 max
= min
= NULL_TREE
;
2252 /* If either MIN or MAX overflowed, then set the resulting range to
2254 if (min
== NULL_TREE
2255 || TREE_OVERFLOW_P (min
)
2257 || TREE_OVERFLOW_P (max
))
2259 set_value_range_to_varying (vr
);
2263 /* We punt for [-INF, +INF].
2264 We learn nothing when we have INF on both sides.
2265 Note that we do accept [-INF, -INF] and [+INF, +INF]. */
2266 if (vrp_val_is_min (min
) && vrp_val_is_max (max
))
2268 set_value_range_to_varying (vr
);
2272 cmp
= compare_values (min
, max
);
2273 if (cmp
== -2 || cmp
== 1)
2275 /* If the new range has its limits swapped around (MIN > MAX),
2276 then the operation caused one of them to wrap around, mark
2277 the new range VARYING. */
2278 set_value_range_to_varying (vr
);
2281 set_value_range (vr
, type
, min
, max
, NULL
);
2284 /* Extract range information from a unary operation CODE based on
2285 the range of its operand *VR0 with type OP0_TYPE with resulting type TYPE.
2286 The resulting range is stored in *VR. */
2289 extract_range_from_unary_expr (value_range
*vr
,
2290 enum tree_code code
, tree type
,
2291 value_range
*vr0_
, tree op0_type
)
2293 value_range vr0
= *vr0_
, vrtem0
= VR_INITIALIZER
, vrtem1
= VR_INITIALIZER
;
2295 /* VRP only operates on integral and pointer types. */
2296 if (!(INTEGRAL_TYPE_P (op0_type
)
2297 || POINTER_TYPE_P (op0_type
))
2298 || !(INTEGRAL_TYPE_P (type
)
2299 || POINTER_TYPE_P (type
)))
2301 set_value_range_to_varying (vr
);
2305 /* If VR0 is UNDEFINED, so is the result. */
2306 if (vr0
.type
== VR_UNDEFINED
)
2308 set_value_range_to_undefined (vr
);
2312 /* Handle operations that we express in terms of others. */
2313 if (code
== PAREN_EXPR
|| code
== OBJ_TYPE_REF
)
2315 /* PAREN_EXPR and OBJ_TYPE_REF are simple copies. */
2316 copy_value_range (vr
, &vr0
);
2319 else if (code
== NEGATE_EXPR
)
2321 /* -X is simply 0 - X, so re-use existing code that also handles
2322 anti-ranges fine. */
2323 value_range zero
= VR_INITIALIZER
;
2324 set_value_range_to_value (&zero
, build_int_cst (type
, 0), NULL
);
2325 extract_range_from_binary_expr_1 (vr
, MINUS_EXPR
, type
, &zero
, &vr0
);
2328 else if (code
== BIT_NOT_EXPR
)
2330 /* ~X is simply -1 - X, so re-use existing code that also handles
2331 anti-ranges fine. */
2332 value_range minusone
= VR_INITIALIZER
;
2333 set_value_range_to_value (&minusone
, build_int_cst (type
, -1), NULL
);
2334 extract_range_from_binary_expr_1 (vr
, MINUS_EXPR
,
2335 type
, &minusone
, &vr0
);
2339 /* Now canonicalize anti-ranges to ranges when they are not symbolic
2340 and express op ~[] as (op []') U (op []''). */
2341 if (vr0
.type
== VR_ANTI_RANGE
2342 && ranges_from_anti_range (&vr0
, &vrtem0
, &vrtem1
))
2344 extract_range_from_unary_expr (vr
, code
, type
, &vrtem0
, op0_type
);
2345 if (vrtem1
.type
!= VR_UNDEFINED
)
2347 value_range vrres
= VR_INITIALIZER
;
2348 extract_range_from_unary_expr (&vrres
, code
, type
,
2350 vrp_meet (vr
, &vrres
);
2355 if (CONVERT_EXPR_CODE_P (code
))
2357 tree inner_type
= op0_type
;
2358 tree outer_type
= type
;
2360 /* If the expression evaluates to a pointer, we are only interested in
2361 determining if it evaluates to NULL [0, 0] or non-NULL (~[0, 0]). */
2362 if (POINTER_TYPE_P (type
))
2364 if (range_is_nonnull (&vr0
))
2365 set_value_range_to_nonnull (vr
, type
);
2366 else if (range_is_null (&vr0
))
2367 set_value_range_to_null (vr
, type
);
2369 set_value_range_to_varying (vr
);
2373 /* If VR0 is varying and we increase the type precision, assume
2374 a full range for the following transformation. */
2375 if (vr0
.type
== VR_VARYING
2376 && INTEGRAL_TYPE_P (inner_type
)
2377 && TYPE_PRECISION (inner_type
) < TYPE_PRECISION (outer_type
))
2379 vr0
.type
= VR_RANGE
;
2380 vr0
.min
= TYPE_MIN_VALUE (inner_type
);
2381 vr0
.max
= TYPE_MAX_VALUE (inner_type
);
2384 /* If VR0 is a constant range or anti-range and the conversion is
2385 not truncating we can convert the min and max values and
2386 canonicalize the resulting range. Otherwise we can do the
2387 conversion if the size of the range is less than what the
2388 precision of the target type can represent and the range is
2389 not an anti-range. */
2390 if ((vr0
.type
== VR_RANGE
2391 || vr0
.type
== VR_ANTI_RANGE
)
2392 && TREE_CODE (vr0
.min
) == INTEGER_CST
2393 && TREE_CODE (vr0
.max
) == INTEGER_CST
2394 && (TYPE_PRECISION (outer_type
) >= TYPE_PRECISION (inner_type
)
2395 || (vr0
.type
== VR_RANGE
2396 && integer_zerop (int_const_binop (RSHIFT_EXPR
,
2397 int_const_binop (MINUS_EXPR
, vr0
.max
, vr0
.min
),
2398 size_int (TYPE_PRECISION (outer_type
)))))))
2400 tree new_min
, new_max
;
2401 new_min
= force_fit_type (outer_type
, wi::to_widest (vr0
.min
),
2403 new_max
= force_fit_type (outer_type
, wi::to_widest (vr0
.max
),
2405 set_and_canonicalize_value_range (vr
, vr0
.type
,
2406 new_min
, new_max
, NULL
);
2410 set_value_range_to_varying (vr
);
2413 else if (code
== ABS_EXPR
)
2418 /* Pass through vr0 in the easy cases. */
2419 if (TYPE_UNSIGNED (type
)
2420 || value_range_nonnegative_p (&vr0
))
2422 copy_value_range (vr
, &vr0
);
2426 /* For the remaining varying or symbolic ranges we can't do anything
2428 if (vr0
.type
== VR_VARYING
2429 || symbolic_range_p (&vr0
))
2431 set_value_range_to_varying (vr
);
2435 /* -TYPE_MIN_VALUE = TYPE_MIN_VALUE with flag_wrapv so we can't get a
2437 if (!TYPE_OVERFLOW_UNDEFINED (type
)
2438 && ((vr0
.type
== VR_RANGE
2439 && vrp_val_is_min (vr0
.min
))
2440 || (vr0
.type
== VR_ANTI_RANGE
2441 && !vrp_val_is_min (vr0
.min
))))
2443 set_value_range_to_varying (vr
);
2447 /* ABS_EXPR may flip the range around, if the original range
2448 included negative values. */
2449 if (!vrp_val_is_min (vr0
.min
))
2450 min
= fold_unary_to_constant (code
, type
, vr0
.min
);
2452 min
= TYPE_MAX_VALUE (type
);
2454 if (!vrp_val_is_min (vr0
.max
))
2455 max
= fold_unary_to_constant (code
, type
, vr0
.max
);
2457 max
= TYPE_MAX_VALUE (type
);
2459 cmp
= compare_values (min
, max
);
2461 /* If a VR_ANTI_RANGEs contains zero, then we have
2462 ~[-INF, min(MIN, MAX)]. */
2463 if (vr0
.type
== VR_ANTI_RANGE
)
2465 if (range_includes_zero_p (vr0
.min
, vr0
.max
) == 1)
2467 /* Take the lower of the two values. */
2471 /* Create ~[-INF, min (abs(MIN), abs(MAX))]
2472 or ~[-INF + 1, min (abs(MIN), abs(MAX))] when
2473 flag_wrapv is set and the original anti-range doesn't include
2474 TYPE_MIN_VALUE, remember -TYPE_MIN_VALUE = TYPE_MIN_VALUE. */
2475 if (TYPE_OVERFLOW_WRAPS (type
))
2477 tree type_min_value
= TYPE_MIN_VALUE (type
);
2479 min
= (vr0
.min
!= type_min_value
2480 ? int_const_binop (PLUS_EXPR
, type_min_value
,
2481 build_int_cst (TREE_TYPE (type_min_value
), 1))
2485 min
= TYPE_MIN_VALUE (type
);
2489 /* All else has failed, so create the range [0, INF], even for
2490 flag_wrapv since TYPE_MIN_VALUE is in the original
2492 vr0
.type
= VR_RANGE
;
2493 min
= build_int_cst (type
, 0);
2494 max
= TYPE_MAX_VALUE (type
);
2498 /* If the range contains zero then we know that the minimum value in the
2499 range will be zero. */
2500 else if (range_includes_zero_p (vr0
.min
, vr0
.max
) == 1)
2504 min
= build_int_cst (type
, 0);
2508 /* If the range was reversed, swap MIN and MAX. */
2510 std::swap (min
, max
);
2513 cmp
= compare_values (min
, max
);
2514 if (cmp
== -2 || cmp
== 1)
2516 /* If the new range has its limits swapped around (MIN > MAX),
2517 then the operation caused one of them to wrap around, mark
2518 the new range VARYING. */
2519 set_value_range_to_varying (vr
);
2522 set_value_range (vr
, vr0
.type
, min
, max
, NULL
);
2526 /* For unhandled operations fall back to varying. */
2527 set_value_range_to_varying (vr
);
2531 /* Debugging dumps. */
2533 void dump_value_range (FILE *, const value_range
*);
2534 void debug_value_range (value_range
*);
2535 void dump_all_value_ranges (FILE *);
2536 void dump_vr_equiv (FILE *, bitmap
);
2537 void debug_vr_equiv (bitmap
);
2540 /* Dump value range VR to FILE. */
2543 dump_value_range (FILE *file
, const value_range
*vr
)
2546 fprintf (file
, "[]");
2547 else if (vr
->type
== VR_UNDEFINED
)
2548 fprintf (file
, "UNDEFINED");
2549 else if (vr
->type
== VR_RANGE
|| vr
->type
== VR_ANTI_RANGE
)
2551 tree type
= TREE_TYPE (vr
->min
);
2553 fprintf (file
, "%s[", (vr
->type
== VR_ANTI_RANGE
) ? "~" : "");
2555 if (INTEGRAL_TYPE_P (type
)
2556 && !TYPE_UNSIGNED (type
)
2557 && vrp_val_is_min (vr
->min
))
2558 fprintf (file
, "-INF");
2560 print_generic_expr (file
, vr
->min
);
2562 fprintf (file
, ", ");
2564 if (INTEGRAL_TYPE_P (type
)
2565 && vrp_val_is_max (vr
->max
))
2566 fprintf (file
, "+INF");
2568 print_generic_expr (file
, vr
->max
);
2570 fprintf (file
, "]");
2577 fprintf (file
, " EQUIVALENCES: { ");
2579 EXECUTE_IF_SET_IN_BITMAP (vr
->equiv
, 0, i
, bi
)
2581 print_generic_expr (file
, ssa_name (i
));
2582 fprintf (file
, " ");
2586 fprintf (file
, "} (%u elements)", c
);
2589 else if (vr
->type
== VR_VARYING
)
2590 fprintf (file
, "VARYING");
2592 fprintf (file
, "INVALID RANGE");
2596 /* Dump value range VR to stderr. */
2599 debug_value_range (value_range
*vr
)
2601 dump_value_range (stderr
, vr
);
2602 fprintf (stderr
, "\n");
2606 /* Given a COND_EXPR COND of the form 'V OP W', and an SSA name V,
2607 create a new SSA name N and return the assertion assignment
2608 'N = ASSERT_EXPR <V, V OP W>'. */
2611 build_assert_expr_for (tree cond
, tree v
)
2616 gcc_assert (TREE_CODE (v
) == SSA_NAME
2617 && COMPARISON_CLASS_P (cond
));
2619 a
= build2 (ASSERT_EXPR
, TREE_TYPE (v
), v
, cond
);
2620 assertion
= gimple_build_assign (NULL_TREE
, a
);
2622 /* The new ASSERT_EXPR, creates a new SSA name that replaces the
2623 operand of the ASSERT_EXPR. Create it so the new name and the old one
2624 are registered in the replacement table so that we can fix the SSA web
2625 after adding all the ASSERT_EXPRs. */
2626 tree new_def
= create_new_def_for (v
, assertion
, NULL
);
2627 /* Make sure we preserve abnormalness throughout an ASSERT_EXPR chain
2628 given we have to be able to fully propagate those out to re-create
2629 valid SSA when removing the asserts. */
2630 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (v
))
2631 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (new_def
) = 1;
2637 /* Return false if EXPR is a predicate expression involving floating
2641 fp_predicate (gimple
*stmt
)
2643 GIMPLE_CHECK (stmt
, GIMPLE_COND
);
2645 return FLOAT_TYPE_P (TREE_TYPE (gimple_cond_lhs (stmt
)));
2648 /* If the range of values taken by OP can be inferred after STMT executes,
2649 return the comparison code (COMP_CODE_P) and value (VAL_P) that
2650 describes the inferred range. Return true if a range could be
2654 infer_value_range (gimple
*stmt
, tree op
, tree_code
*comp_code_p
, tree
*val_p
)
2657 *comp_code_p
= ERROR_MARK
;
2659 /* Do not attempt to infer anything in names that flow through
2661 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op
))
2664 /* If STMT is the last statement of a basic block with no normal
2665 successors, there is no point inferring anything about any of its
2666 operands. We would not be able to find a proper insertion point
2667 for the assertion, anyway. */
2668 if (stmt_ends_bb_p (stmt
))
2673 FOR_EACH_EDGE (e
, ei
, gimple_bb (stmt
)->succs
)
2674 if (!(e
->flags
& (EDGE_ABNORMAL
|EDGE_EH
)))
2680 if (infer_nonnull_range (stmt
, op
))
2682 *val_p
= build_int_cst (TREE_TYPE (op
), 0);
2683 *comp_code_p
= NE_EXPR
;
2691 void dump_asserts_for (FILE *, tree
);
2692 void debug_asserts_for (tree
);
2693 void dump_all_asserts (FILE *);
2694 void debug_all_asserts (void);
2696 /* Dump all the registered assertions for NAME to FILE. */
2699 dump_asserts_for (FILE *file
, tree name
)
2703 fprintf (file
, "Assertions to be inserted for ");
2704 print_generic_expr (file
, name
);
2705 fprintf (file
, "\n");
2707 loc
= asserts_for
[SSA_NAME_VERSION (name
)];
2710 fprintf (file
, "\t");
2711 print_gimple_stmt (file
, gsi_stmt (loc
->si
), 0);
2712 fprintf (file
, "\n\tBB #%d", loc
->bb
->index
);
2715 fprintf (file
, "\n\tEDGE %d->%d", loc
->e
->src
->index
,
2716 loc
->e
->dest
->index
);
2717 dump_edge_info (file
, loc
->e
, dump_flags
, 0);
2719 fprintf (file
, "\n\tPREDICATE: ");
2720 print_generic_expr (file
, loc
->expr
);
2721 fprintf (file
, " %s ", get_tree_code_name (loc
->comp_code
));
2722 print_generic_expr (file
, loc
->val
);
2723 fprintf (file
, "\n\n");
2727 fprintf (file
, "\n");
2731 /* Dump all the registered assertions for NAME to stderr. */
2734 debug_asserts_for (tree name
)
2736 dump_asserts_for (stderr
, name
);
2740 /* Dump all the registered assertions for all the names to FILE. */
2743 dump_all_asserts (FILE *file
)
2748 fprintf (file
, "\nASSERT_EXPRs to be inserted\n\n");
2749 EXECUTE_IF_SET_IN_BITMAP (need_assert_for
, 0, i
, bi
)
2750 dump_asserts_for (file
, ssa_name (i
));
2751 fprintf (file
, "\n");
2755 /* Dump all the registered assertions for all the names to stderr. */
2758 debug_all_asserts (void)
2760 dump_all_asserts (stderr
);
2763 /* Push the assert info for NAME, EXPR, COMP_CODE and VAL to ASSERTS. */
2766 add_assert_info (vec
<assert_info
> &asserts
,
2767 tree name
, tree expr
, enum tree_code comp_code
, tree val
)
2770 info
.comp_code
= comp_code
;
2774 asserts
.safe_push (info
);
2777 /* If NAME doesn't have an ASSERT_EXPR registered for asserting
2778 'EXPR COMP_CODE VAL' at a location that dominates block BB or
2779 E->DEST, then register this location as a possible insertion point
2780 for ASSERT_EXPR <NAME, EXPR COMP_CODE VAL>.
2782 BB, E and SI provide the exact insertion point for the new
2783 ASSERT_EXPR. If BB is NULL, then the ASSERT_EXPR is to be inserted
2784 on edge E. Otherwise, if E is NULL, the ASSERT_EXPR is inserted on
2785 BB. If SI points to a COND_EXPR or a SWITCH_EXPR statement, then E
2786 must not be NULL. */
2789 register_new_assert_for (tree name
, tree expr
,
2790 enum tree_code comp_code
,
2794 gimple_stmt_iterator si
)
2796 assert_locus
*n
, *loc
, *last_loc
;
2797 basic_block dest_bb
;
2799 gcc_checking_assert (bb
== NULL
|| e
== NULL
);
2802 gcc_checking_assert (gimple_code (gsi_stmt (si
)) != GIMPLE_COND
2803 && gimple_code (gsi_stmt (si
)) != GIMPLE_SWITCH
);
2805 /* Never build an assert comparing against an integer constant with
2806 TREE_OVERFLOW set. This confuses our undefined overflow warning
2808 if (TREE_OVERFLOW_P (val
))
2809 val
= drop_tree_overflow (val
);
2811 /* The new assertion A will be inserted at BB or E. We need to
2812 determine if the new location is dominated by a previously
2813 registered location for A. If we are doing an edge insertion,
2814 assume that A will be inserted at E->DEST. Note that this is not
2817 If E is a critical edge, it will be split. But even if E is
2818 split, the new block will dominate the same set of blocks that
2821 The reverse, however, is not true, blocks dominated by E->DEST
2822 will not be dominated by the new block created to split E. So,
2823 if the insertion location is on a critical edge, we will not use
2824 the new location to move another assertion previously registered
2825 at a block dominated by E->DEST. */
2826 dest_bb
= (bb
) ? bb
: e
->dest
;
2828 /* If NAME already has an ASSERT_EXPR registered for COMP_CODE and
2829 VAL at a block dominating DEST_BB, then we don't need to insert a new
2830 one. Similarly, if the same assertion already exists at a block
2831 dominated by DEST_BB and the new location is not on a critical
2832 edge, then update the existing location for the assertion (i.e.,
2833 move the assertion up in the dominance tree).
2835 Note, this is implemented as a simple linked list because there
2836 should not be more than a handful of assertions registered per
2837 name. If this becomes a performance problem, a table hashed by
2838 COMP_CODE and VAL could be implemented. */
2839 loc
= asserts_for
[SSA_NAME_VERSION (name
)];
2843 if (loc
->comp_code
== comp_code
2845 || operand_equal_p (loc
->val
, val
, 0))
2846 && (loc
->expr
== expr
2847 || operand_equal_p (loc
->expr
, expr
, 0)))
2849 /* If E is not a critical edge and DEST_BB
2850 dominates the existing location for the assertion, move
2851 the assertion up in the dominance tree by updating its
2852 location information. */
2853 if ((e
== NULL
|| !EDGE_CRITICAL_P (e
))
2854 && dominated_by_p (CDI_DOMINATORS
, loc
->bb
, dest_bb
))
2863 /* Update the last node of the list and move to the next one. */
2868 /* If we didn't find an assertion already registered for
2869 NAME COMP_CODE VAL, add a new one at the end of the list of
2870 assertions associated with NAME. */
2871 n
= XNEW (struct assert_locus
);
2875 n
->comp_code
= comp_code
;
2883 asserts_for
[SSA_NAME_VERSION (name
)] = n
;
2885 bitmap_set_bit (need_assert_for
, SSA_NAME_VERSION (name
));
2888 /* (COND_OP0 COND_CODE COND_OP1) is a predicate which uses NAME.
2889 Extract a suitable test code and value and store them into *CODE_P and
2890 *VAL_P so the predicate is normalized to NAME *CODE_P *VAL_P.
2892 If no extraction was possible, return FALSE, otherwise return TRUE.
2894 If INVERT is true, then we invert the result stored into *CODE_P. */
2897 extract_code_and_val_from_cond_with_ops (tree name
, enum tree_code cond_code
,
2898 tree cond_op0
, tree cond_op1
,
2899 bool invert
, enum tree_code
*code_p
,
2902 enum tree_code comp_code
;
2905 /* Otherwise, we have a comparison of the form NAME COMP VAL
2906 or VAL COMP NAME. */
2907 if (name
== cond_op1
)
2909 /* If the predicate is of the form VAL COMP NAME, flip
2910 COMP around because we need to register NAME as the
2911 first operand in the predicate. */
2912 comp_code
= swap_tree_comparison (cond_code
);
2915 else if (name
== cond_op0
)
2917 /* The comparison is of the form NAME COMP VAL, so the
2918 comparison code remains unchanged. */
2919 comp_code
= cond_code
;
2925 /* Invert the comparison code as necessary. */
2927 comp_code
= invert_tree_comparison (comp_code
, 0);
2929 /* VRP only handles integral and pointer types. */
2930 if (! INTEGRAL_TYPE_P (TREE_TYPE (val
))
2931 && ! POINTER_TYPE_P (TREE_TYPE (val
)))
2934 /* Do not register always-false predicates.
2935 FIXME: this works around a limitation in fold() when dealing with
2936 enumerations. Given 'enum { N1, N2 } x;', fold will not
2937 fold 'if (x > N2)' to 'if (0)'. */
2938 if ((comp_code
== GT_EXPR
|| comp_code
== LT_EXPR
)
2939 && INTEGRAL_TYPE_P (TREE_TYPE (val
)))
2941 tree min
= TYPE_MIN_VALUE (TREE_TYPE (val
));
2942 tree max
= TYPE_MAX_VALUE (TREE_TYPE (val
));
2944 if (comp_code
== GT_EXPR
2946 || compare_values (val
, max
) == 0))
2949 if (comp_code
== LT_EXPR
2951 || compare_values (val
, min
) == 0))
2954 *code_p
= comp_code
;
2959 /* Find out smallest RES where RES > VAL && (RES & MASK) == RES, if any
2960 (otherwise return VAL). VAL and MASK must be zero-extended for
2961 precision PREC. If SGNBIT is non-zero, first xor VAL with SGNBIT
2962 (to transform signed values into unsigned) and at the end xor
2966 masked_increment (const wide_int
&val_in
, const wide_int
&mask
,
2967 const wide_int
&sgnbit
, unsigned int prec
)
2969 wide_int bit
= wi::one (prec
), res
;
2972 wide_int val
= val_in
^ sgnbit
;
2973 for (i
= 0; i
< prec
; i
++, bit
+= bit
)
2976 if ((res
& bit
) == 0)
2979 res
= wi::bit_and_not (val
+ bit
, res
);
2981 if (wi::gtu_p (res
, val
))
2982 return res
^ sgnbit
;
2984 return val
^ sgnbit
;
2987 /* Helper for overflow_comparison_p
2989 OP0 CODE OP1 is a comparison. Examine the comparison and potentially
2990 OP1's defining statement to see if it ultimately has the form
2991 OP0 CODE (OP0 PLUS INTEGER_CST)
2993 If so, return TRUE indicating this is an overflow test and store into
2994 *NEW_CST an updated constant that can be used in a narrowed range test.
2996 REVERSED indicates if the comparison was originally:
3000 This affects how we build the updated constant. */
3003 overflow_comparison_p_1 (enum tree_code code
, tree op0
, tree op1
,
3004 bool follow_assert_exprs
, bool reversed
, tree
*new_cst
)
3006 /* See if this is a relational operation between two SSA_NAMES with
3007 unsigned, overflow wrapping values. If so, check it more deeply. */
3008 if ((code
== LT_EXPR
|| code
== LE_EXPR
3009 || code
== GE_EXPR
|| code
== GT_EXPR
)
3010 && TREE_CODE (op0
) == SSA_NAME
3011 && TREE_CODE (op1
) == SSA_NAME
3012 && INTEGRAL_TYPE_P (TREE_TYPE (op0
))
3013 && TYPE_UNSIGNED (TREE_TYPE (op0
))
3014 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (op0
)))
3016 gimple
*op1_def
= SSA_NAME_DEF_STMT (op1
);
3018 /* If requested, follow any ASSERT_EXPRs backwards for OP1. */
3019 if (follow_assert_exprs
)
3021 while (gimple_assign_single_p (op1_def
)
3022 && TREE_CODE (gimple_assign_rhs1 (op1_def
)) == ASSERT_EXPR
)
3024 op1
= TREE_OPERAND (gimple_assign_rhs1 (op1_def
), 0);
3025 if (TREE_CODE (op1
) != SSA_NAME
)
3027 op1_def
= SSA_NAME_DEF_STMT (op1
);
3031 /* Now look at the defining statement of OP1 to see if it adds
3032 or subtracts a nonzero constant from another operand. */
3034 && is_gimple_assign (op1_def
)
3035 && gimple_assign_rhs_code (op1_def
) == PLUS_EXPR
3036 && TREE_CODE (gimple_assign_rhs2 (op1_def
)) == INTEGER_CST
3037 && !integer_zerop (gimple_assign_rhs2 (op1_def
)))
3039 tree target
= gimple_assign_rhs1 (op1_def
);
3041 /* If requested, follow ASSERT_EXPRs backwards for op0 looking
3042 for one where TARGET appears on the RHS. */
3043 if (follow_assert_exprs
)
3045 /* Now see if that "other operand" is op0, following the chain
3046 of ASSERT_EXPRs if necessary. */
3047 gimple
*op0_def
= SSA_NAME_DEF_STMT (op0
);
3048 while (op0
!= target
3049 && gimple_assign_single_p (op0_def
)
3050 && TREE_CODE (gimple_assign_rhs1 (op0_def
)) == ASSERT_EXPR
)
3052 op0
= TREE_OPERAND (gimple_assign_rhs1 (op0_def
), 0);
3053 if (TREE_CODE (op0
) != SSA_NAME
)
3055 op0_def
= SSA_NAME_DEF_STMT (op0
);
3059 /* If we did not find our target SSA_NAME, then this is not
3060 an overflow test. */
3064 tree type
= TREE_TYPE (op0
);
3065 wide_int max
= wi::max_value (TYPE_PRECISION (type
), UNSIGNED
);
3066 tree inc
= gimple_assign_rhs2 (op1_def
);
3068 *new_cst
= wide_int_to_tree (type
, max
+ wi::to_wide (inc
));
3070 *new_cst
= wide_int_to_tree (type
, max
- wi::to_wide (inc
));
3077 /* OP0 CODE OP1 is a comparison. Examine the comparison and potentially
3078 OP1's defining statement to see if it ultimately has the form
3079 OP0 CODE (OP0 PLUS INTEGER_CST)
3081 If so, return TRUE indicating this is an overflow test and store into
3082 *NEW_CST an updated constant that can be used in a narrowed range test.
3084 These statements are left as-is in the IL to facilitate discovery of
3085 {ADD,SUB}_OVERFLOW sequences later in the optimizer pipeline. But
3086 the alternate range representation is often useful within VRP. */
3089 overflow_comparison_p (tree_code code
, tree name
, tree val
,
3090 bool use_equiv_p
, tree
*new_cst
)
3092 if (overflow_comparison_p_1 (code
, name
, val
, use_equiv_p
, false, new_cst
))
3094 return overflow_comparison_p_1 (swap_tree_comparison (code
), val
, name
,
3095 use_equiv_p
, true, new_cst
);
3099 /* Try to register an edge assertion for SSA name NAME on edge E for
3100 the condition COND contributing to the conditional jump pointed to by BSI.
3101 Invert the condition COND if INVERT is true. */
3104 register_edge_assert_for_2 (tree name
, edge e
,
3105 enum tree_code cond_code
,
3106 tree cond_op0
, tree cond_op1
, bool invert
,
3107 vec
<assert_info
> &asserts
)
3110 enum tree_code comp_code
;
3112 if (!extract_code_and_val_from_cond_with_ops (name
, cond_code
,
3115 invert
, &comp_code
, &val
))
3118 /* Queue the assert. */
3120 if (overflow_comparison_p (comp_code
, name
, val
, false, &x
))
3122 enum tree_code new_code
= ((comp_code
== GT_EXPR
|| comp_code
== GE_EXPR
)
3123 ? GT_EXPR
: LE_EXPR
);
3124 add_assert_info (asserts
, name
, name
, new_code
, x
);
3126 add_assert_info (asserts
, name
, name
, comp_code
, val
);
3128 /* In the case of NAME <= CST and NAME being defined as
3129 NAME = (unsigned) NAME2 + CST2 we can assert NAME2 >= -CST2
3130 and NAME2 <= CST - CST2. We can do the same for NAME > CST.
3131 This catches range and anti-range tests. */
3132 if ((comp_code
== LE_EXPR
3133 || comp_code
== GT_EXPR
)
3134 && TREE_CODE (val
) == INTEGER_CST
3135 && TYPE_UNSIGNED (TREE_TYPE (val
)))
3137 gimple
*def_stmt
= SSA_NAME_DEF_STMT (name
);
3138 tree cst2
= NULL_TREE
, name2
= NULL_TREE
, name3
= NULL_TREE
;
3140 /* Extract CST2 from the (optional) addition. */
3141 if (is_gimple_assign (def_stmt
)
3142 && gimple_assign_rhs_code (def_stmt
) == PLUS_EXPR
)
3144 name2
= gimple_assign_rhs1 (def_stmt
);
3145 cst2
= gimple_assign_rhs2 (def_stmt
);
3146 if (TREE_CODE (name2
) == SSA_NAME
3147 && TREE_CODE (cst2
) == INTEGER_CST
)
3148 def_stmt
= SSA_NAME_DEF_STMT (name2
);
3151 /* Extract NAME2 from the (optional) sign-changing cast. */
3152 if (gimple_assign_cast_p (def_stmt
))
3154 if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt
))
3155 && ! TYPE_UNSIGNED (TREE_TYPE (gimple_assign_rhs1 (def_stmt
)))
3156 && (TYPE_PRECISION (gimple_expr_type (def_stmt
))
3157 == TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (def_stmt
)))))
3158 name3
= gimple_assign_rhs1 (def_stmt
);
3161 /* If name3 is used later, create an ASSERT_EXPR for it. */
3162 if (name3
!= NULL_TREE
3163 && TREE_CODE (name3
) == SSA_NAME
3164 && (cst2
== NULL_TREE
3165 || TREE_CODE (cst2
) == INTEGER_CST
)
3166 && INTEGRAL_TYPE_P (TREE_TYPE (name3
)))
3170 /* Build an expression for the range test. */
3171 tmp
= build1 (NOP_EXPR
, TREE_TYPE (name
), name3
);
3172 if (cst2
!= NULL_TREE
)
3173 tmp
= build2 (PLUS_EXPR
, TREE_TYPE (name
), tmp
, cst2
);
3177 fprintf (dump_file
, "Adding assert for ");
3178 print_generic_expr (dump_file
, name3
);
3179 fprintf (dump_file
, " from ");
3180 print_generic_expr (dump_file
, tmp
);
3181 fprintf (dump_file
, "\n");
3184 add_assert_info (asserts
, name3
, tmp
, comp_code
, val
);
3187 /* If name2 is used later, create an ASSERT_EXPR for it. */
3188 if (name2
!= NULL_TREE
3189 && TREE_CODE (name2
) == SSA_NAME
3190 && TREE_CODE (cst2
) == INTEGER_CST
3191 && INTEGRAL_TYPE_P (TREE_TYPE (name2
)))
3195 /* Build an expression for the range test. */
3197 if (TREE_TYPE (name
) != TREE_TYPE (name2
))
3198 tmp
= build1 (NOP_EXPR
, TREE_TYPE (name
), tmp
);
3199 if (cst2
!= NULL_TREE
)
3200 tmp
= build2 (PLUS_EXPR
, TREE_TYPE (name
), tmp
, cst2
);
3204 fprintf (dump_file
, "Adding assert for ");
3205 print_generic_expr (dump_file
, name2
);
3206 fprintf (dump_file
, " from ");
3207 print_generic_expr (dump_file
, tmp
);
3208 fprintf (dump_file
, "\n");
3211 add_assert_info (asserts
, name2
, tmp
, comp_code
, val
);
3215 /* In the case of post-in/decrement tests like if (i++) ... and uses
3216 of the in/decremented value on the edge the extra name we want to
3217 assert for is not on the def chain of the name compared. Instead
3218 it is in the set of use stmts.
3219 Similar cases happen for conversions that were simplified through
3220 fold_{sign_changed,widened}_comparison. */
3221 if ((comp_code
== NE_EXPR
3222 || comp_code
== EQ_EXPR
)
3223 && TREE_CODE (val
) == INTEGER_CST
)
3225 imm_use_iterator ui
;
3227 FOR_EACH_IMM_USE_STMT (use_stmt
, ui
, name
)
3229 if (!is_gimple_assign (use_stmt
))
3232 /* Cut off to use-stmts that are dominating the predecessor. */
3233 if (!dominated_by_p (CDI_DOMINATORS
, e
->src
, gimple_bb (use_stmt
)))
3236 tree name2
= gimple_assign_lhs (use_stmt
);
3237 if (TREE_CODE (name2
) != SSA_NAME
)
3240 enum tree_code code
= gimple_assign_rhs_code (use_stmt
);
3242 if (code
== PLUS_EXPR
3243 || code
== MINUS_EXPR
)
3245 cst
= gimple_assign_rhs2 (use_stmt
);
3246 if (TREE_CODE (cst
) != INTEGER_CST
)
3248 cst
= int_const_binop (code
, val
, cst
);
3250 else if (CONVERT_EXPR_CODE_P (code
))
3252 /* For truncating conversions we cannot record
3254 if (comp_code
== NE_EXPR
3255 && (TYPE_PRECISION (TREE_TYPE (name2
))
3256 < TYPE_PRECISION (TREE_TYPE (name
))))
3258 cst
= fold_convert (TREE_TYPE (name2
), val
);
3263 if (TREE_OVERFLOW_P (cst
))
3264 cst
= drop_tree_overflow (cst
);
3265 add_assert_info (asserts
, name2
, name2
, comp_code
, cst
);
3269 if (TREE_CODE_CLASS (comp_code
) == tcc_comparison
3270 && TREE_CODE (val
) == INTEGER_CST
)
3272 gimple
*def_stmt
= SSA_NAME_DEF_STMT (name
);
3273 tree name2
= NULL_TREE
, names
[2], cst2
= NULL_TREE
;
3274 tree val2
= NULL_TREE
;
3275 unsigned int prec
= TYPE_PRECISION (TREE_TYPE (val
));
3276 wide_int mask
= wi::zero (prec
);
3277 unsigned int nprec
= prec
;
3278 enum tree_code rhs_code
= ERROR_MARK
;
3280 if (is_gimple_assign (def_stmt
))
3281 rhs_code
= gimple_assign_rhs_code (def_stmt
);
3283 /* In the case of NAME != CST1 where NAME = A +- CST2 we can
3284 assert that A != CST1 -+ CST2. */
3285 if ((comp_code
== EQ_EXPR
|| comp_code
== NE_EXPR
)
3286 && (rhs_code
== PLUS_EXPR
|| rhs_code
== MINUS_EXPR
))
3288 tree op0
= gimple_assign_rhs1 (def_stmt
);
3289 tree op1
= gimple_assign_rhs2 (def_stmt
);
3290 if (TREE_CODE (op0
) == SSA_NAME
3291 && TREE_CODE (op1
) == INTEGER_CST
)
3293 enum tree_code reverse_op
= (rhs_code
== PLUS_EXPR
3294 ? MINUS_EXPR
: PLUS_EXPR
);
3295 op1
= int_const_binop (reverse_op
, val
, op1
);
3296 if (TREE_OVERFLOW (op1
))
3297 op1
= drop_tree_overflow (op1
);
3298 add_assert_info (asserts
, op0
, op0
, comp_code
, op1
);
3302 /* Add asserts for NAME cmp CST and NAME being defined
3303 as NAME = (int) NAME2. */
3304 if (!TYPE_UNSIGNED (TREE_TYPE (val
))
3305 && (comp_code
== LE_EXPR
|| comp_code
== LT_EXPR
3306 || comp_code
== GT_EXPR
|| comp_code
== GE_EXPR
)
3307 && gimple_assign_cast_p (def_stmt
))
3309 name2
= gimple_assign_rhs1 (def_stmt
);
3310 if (CONVERT_EXPR_CODE_P (rhs_code
)
3311 && INTEGRAL_TYPE_P (TREE_TYPE (name2
))
3312 && TYPE_UNSIGNED (TREE_TYPE (name2
))
3313 && prec
== TYPE_PRECISION (TREE_TYPE (name2
))
3314 && (comp_code
== LE_EXPR
|| comp_code
== GT_EXPR
3315 || !tree_int_cst_equal (val
,
3316 TYPE_MIN_VALUE (TREE_TYPE (val
)))))
3319 enum tree_code new_comp_code
= comp_code
;
3321 cst
= fold_convert (TREE_TYPE (name2
),
3322 TYPE_MIN_VALUE (TREE_TYPE (val
)));
3323 /* Build an expression for the range test. */
3324 tmp
= build2 (PLUS_EXPR
, TREE_TYPE (name2
), name2
, cst
);
3325 cst
= fold_build2 (PLUS_EXPR
, TREE_TYPE (name2
), cst
,
3326 fold_convert (TREE_TYPE (name2
), val
));
3327 if (comp_code
== LT_EXPR
|| comp_code
== GE_EXPR
)
3329 new_comp_code
= comp_code
== LT_EXPR
? LE_EXPR
: GT_EXPR
;
3330 cst
= fold_build2 (MINUS_EXPR
, TREE_TYPE (name2
), cst
,
3331 build_int_cst (TREE_TYPE (name2
), 1));
3336 fprintf (dump_file
, "Adding assert for ");
3337 print_generic_expr (dump_file
, name2
);
3338 fprintf (dump_file
, " from ");
3339 print_generic_expr (dump_file
, tmp
);
3340 fprintf (dump_file
, "\n");
3343 add_assert_info (asserts
, name2
, tmp
, new_comp_code
, cst
);
3347 /* Add asserts for NAME cmp CST and NAME being defined as
3348 NAME = NAME2 >> CST2.
3350 Extract CST2 from the right shift. */
3351 if (rhs_code
== RSHIFT_EXPR
)
3353 name2
= gimple_assign_rhs1 (def_stmt
);
3354 cst2
= gimple_assign_rhs2 (def_stmt
);
3355 if (TREE_CODE (name2
) == SSA_NAME
3356 && tree_fits_uhwi_p (cst2
)
3357 && INTEGRAL_TYPE_P (TREE_TYPE (name2
))
3358 && IN_RANGE (tree_to_uhwi (cst2
), 1, prec
- 1)
3359 && type_has_mode_precision_p (TREE_TYPE (val
)))
3361 mask
= wi::mask (tree_to_uhwi (cst2
), false, prec
);
3362 val2
= fold_binary (LSHIFT_EXPR
, TREE_TYPE (val
), val
, cst2
);
3365 if (val2
!= NULL_TREE
3366 && TREE_CODE (val2
) == INTEGER_CST
3367 && simple_cst_equal (fold_build2 (RSHIFT_EXPR
,
3371 enum tree_code new_comp_code
= comp_code
;
3375 if (comp_code
== EQ_EXPR
|| comp_code
== NE_EXPR
)
3377 if (!TYPE_UNSIGNED (TREE_TYPE (val
)))
3379 tree type
= build_nonstandard_integer_type (prec
, 1);
3380 tmp
= build1 (NOP_EXPR
, type
, name2
);
3381 val2
= fold_convert (type
, val2
);
3383 tmp
= fold_build2 (MINUS_EXPR
, TREE_TYPE (tmp
), tmp
, val2
);
3384 new_val
= wide_int_to_tree (TREE_TYPE (tmp
), mask
);
3385 new_comp_code
= comp_code
== EQ_EXPR
? LE_EXPR
: GT_EXPR
;
3387 else if (comp_code
== LT_EXPR
|| comp_code
== GE_EXPR
)
3390 = wi::min_value (prec
, TYPE_SIGN (TREE_TYPE (val
)));
3392 if (minval
== wi::to_wide (new_val
))
3393 new_val
= NULL_TREE
;
3398 = wi::max_value (prec
, TYPE_SIGN (TREE_TYPE (val
)));
3399 mask
|= wi::to_wide (val2
);
3400 if (wi::eq_p (mask
, maxval
))
3401 new_val
= NULL_TREE
;
3403 new_val
= wide_int_to_tree (TREE_TYPE (val2
), mask
);
3410 fprintf (dump_file
, "Adding assert for ");
3411 print_generic_expr (dump_file
, name2
);
3412 fprintf (dump_file
, " from ");
3413 print_generic_expr (dump_file
, tmp
);
3414 fprintf (dump_file
, "\n");
3417 add_assert_info (asserts
, name2
, tmp
, new_comp_code
, new_val
);
3421 /* Add asserts for NAME cmp CST and NAME being defined as
3422 NAME = NAME2 & CST2.
3424 Extract CST2 from the and.
3427 NAME = (unsigned) NAME2;
3428 casts where NAME's type is unsigned and has smaller precision
3429 than NAME2's type as if it was NAME = NAME2 & MASK. */
3430 names
[0] = NULL_TREE
;
3431 names
[1] = NULL_TREE
;
3433 if (rhs_code
== BIT_AND_EXPR
3434 || (CONVERT_EXPR_CODE_P (rhs_code
)
3435 && INTEGRAL_TYPE_P (TREE_TYPE (val
))
3436 && TYPE_UNSIGNED (TREE_TYPE (val
))
3437 && TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (def_stmt
)))
3440 name2
= gimple_assign_rhs1 (def_stmt
);
3441 if (rhs_code
== BIT_AND_EXPR
)
3442 cst2
= gimple_assign_rhs2 (def_stmt
);
3445 cst2
= TYPE_MAX_VALUE (TREE_TYPE (val
));
3446 nprec
= TYPE_PRECISION (TREE_TYPE (name2
));
3448 if (TREE_CODE (name2
) == SSA_NAME
3449 && INTEGRAL_TYPE_P (TREE_TYPE (name2
))
3450 && TREE_CODE (cst2
) == INTEGER_CST
3451 && !integer_zerop (cst2
)
3453 || TYPE_UNSIGNED (TREE_TYPE (val
))))
3455 gimple
*def_stmt2
= SSA_NAME_DEF_STMT (name2
);
3456 if (gimple_assign_cast_p (def_stmt2
))
3458 names
[1] = gimple_assign_rhs1 (def_stmt2
);
3459 if (!CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt2
))
3460 || !INTEGRAL_TYPE_P (TREE_TYPE (names
[1]))
3461 || (TYPE_PRECISION (TREE_TYPE (name2
))
3462 != TYPE_PRECISION (TREE_TYPE (names
[1]))))
3463 names
[1] = NULL_TREE
;
3468 if (names
[0] || names
[1])
3470 wide_int minv
, maxv
, valv
, cst2v
;
3471 wide_int tem
, sgnbit
;
3472 bool valid_p
= false, valn
, cst2n
;
3473 enum tree_code ccode
= comp_code
;
3475 valv
= wide_int::from (wi::to_wide (val
), nprec
, UNSIGNED
);
3476 cst2v
= wide_int::from (wi::to_wide (cst2
), nprec
, UNSIGNED
);
3477 valn
= wi::neg_p (valv
, TYPE_SIGN (TREE_TYPE (val
)));
3478 cst2n
= wi::neg_p (cst2v
, TYPE_SIGN (TREE_TYPE (val
)));
3479 /* If CST2 doesn't have most significant bit set,
3480 but VAL is negative, we have comparison like
3481 if ((x & 0x123) > -4) (always true). Just give up. */
3485 sgnbit
= wi::set_bit_in_zero (nprec
- 1, nprec
);
3487 sgnbit
= wi::zero (nprec
);
3488 minv
= valv
& cst2v
;
3492 /* Minimum unsigned value for equality is VAL & CST2
3493 (should be equal to VAL, otherwise we probably should
3494 have folded the comparison into false) and
3495 maximum unsigned value is VAL | ~CST2. */
3496 maxv
= valv
| ~cst2v
;
3501 tem
= valv
| ~cst2v
;
3502 /* If VAL is 0, handle (X & CST2) != 0 as (X & CST2) > 0U. */
3506 sgnbit
= wi::zero (nprec
);
3509 /* If (VAL | ~CST2) is all ones, handle it as
3510 (X & CST2) < VAL. */
3515 sgnbit
= wi::zero (nprec
);
3518 if (!cst2n
&& wi::neg_p (cst2v
))
3519 sgnbit
= wi::set_bit_in_zero (nprec
- 1, nprec
);
3528 if (tem
== wi::mask (nprec
- 1, false, nprec
))
3534 sgnbit
= wi::zero (nprec
);
3539 /* Minimum unsigned value for >= if (VAL & CST2) == VAL
3540 is VAL and maximum unsigned value is ~0. For signed
3541 comparison, if CST2 doesn't have most significant bit
3542 set, handle it similarly. If CST2 has MSB set,
3543 the minimum is the same, and maximum is ~0U/2. */
3546 /* If (VAL & CST2) != VAL, X & CST2 can't be equal to
3548 minv
= masked_increment (valv
, cst2v
, sgnbit
, nprec
);
3552 maxv
= wi::mask (nprec
- (cst2n
? 1 : 0), false, nprec
);
3558 /* Find out smallest MINV where MINV > VAL
3559 && (MINV & CST2) == MINV, if any. If VAL is signed and
3560 CST2 has MSB set, compute it biased by 1 << (nprec - 1). */
3561 minv
= masked_increment (valv
, cst2v
, sgnbit
, nprec
);
3564 maxv
= wi::mask (nprec
- (cst2n
? 1 : 0), false, nprec
);
3569 /* Minimum unsigned value for <= is 0 and maximum
3570 unsigned value is VAL | ~CST2 if (VAL & CST2) == VAL.
3571 Otherwise, find smallest VAL2 where VAL2 > VAL
3572 && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
3574 For signed comparison, if CST2 doesn't have most
3575 significant bit set, handle it similarly. If CST2 has
3576 MSB set, the maximum is the same and minimum is INT_MIN. */
3581 maxv
= masked_increment (valv
, cst2v
, sgnbit
, nprec
);
3593 /* Minimum unsigned value for < is 0 and maximum
3594 unsigned value is (VAL-1) | ~CST2 if (VAL & CST2) == VAL.
3595 Otherwise, find smallest VAL2 where VAL2 > VAL
3596 && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
3598 For signed comparison, if CST2 doesn't have most
3599 significant bit set, handle it similarly. If CST2 has
3600 MSB set, the maximum is the same and minimum is INT_MIN. */
3609 maxv
= masked_increment (valv
, cst2v
, sgnbit
, nprec
);
3623 && (maxv
- minv
) != -1)
3625 tree tmp
, new_val
, type
;
3628 for (i
= 0; i
< 2; i
++)
3631 wide_int maxv2
= maxv
;
3633 type
= TREE_TYPE (names
[i
]);
3634 if (!TYPE_UNSIGNED (type
))
3636 type
= build_nonstandard_integer_type (nprec
, 1);
3637 tmp
= build1 (NOP_EXPR
, type
, names
[i
]);
3641 tmp
= build2 (PLUS_EXPR
, type
, tmp
,
3642 wide_int_to_tree (type
, -minv
));
3643 maxv2
= maxv
- minv
;
3645 new_val
= wide_int_to_tree (type
, maxv2
);
3649 fprintf (dump_file
, "Adding assert for ");
3650 print_generic_expr (dump_file
, names
[i
]);
3651 fprintf (dump_file
, " from ");
3652 print_generic_expr (dump_file
, tmp
);
3653 fprintf (dump_file
, "\n");
3656 add_assert_info (asserts
, names
[i
], tmp
, LE_EXPR
, new_val
);
3663 /* OP is an operand of a truth value expression which is known to have
3664 a particular value. Register any asserts for OP and for any
3665 operands in OP's defining statement.
3667 If CODE is EQ_EXPR, then we want to register OP is zero (false),
3668 if CODE is NE_EXPR, then we want to register OP is nonzero (true). */
3671 register_edge_assert_for_1 (tree op
, enum tree_code code
,
3672 edge e
, vec
<assert_info
> &asserts
)
3676 enum tree_code rhs_code
;
3678 /* We only care about SSA_NAMEs. */
3679 if (TREE_CODE (op
) != SSA_NAME
)
3682 /* We know that OP will have a zero or nonzero value. */
3683 val
= build_int_cst (TREE_TYPE (op
), 0);
3684 add_assert_info (asserts
, op
, op
, code
, val
);
3686 /* Now look at how OP is set. If it's set from a comparison,
3687 a truth operation or some bit operations, then we may be able
3688 to register information about the operands of that assignment. */
3689 op_def
= SSA_NAME_DEF_STMT (op
);
3690 if (gimple_code (op_def
) != GIMPLE_ASSIGN
)
3693 rhs_code
= gimple_assign_rhs_code (op_def
);
3695 if (TREE_CODE_CLASS (rhs_code
) == tcc_comparison
)
3697 bool invert
= (code
== EQ_EXPR
? true : false);
3698 tree op0
= gimple_assign_rhs1 (op_def
);
3699 tree op1
= gimple_assign_rhs2 (op_def
);
3701 if (TREE_CODE (op0
) == SSA_NAME
)
3702 register_edge_assert_for_2 (op0
, e
, rhs_code
, op0
, op1
, invert
, asserts
);
3703 if (TREE_CODE (op1
) == SSA_NAME
)
3704 register_edge_assert_for_2 (op1
, e
, rhs_code
, op0
, op1
, invert
, asserts
);
3706 else if ((code
== NE_EXPR
3707 && gimple_assign_rhs_code (op_def
) == BIT_AND_EXPR
)
3709 && gimple_assign_rhs_code (op_def
) == BIT_IOR_EXPR
))
3711 /* Recurse on each operand. */
3712 tree op0
= gimple_assign_rhs1 (op_def
);
3713 tree op1
= gimple_assign_rhs2 (op_def
);
3714 if (TREE_CODE (op0
) == SSA_NAME
3715 && has_single_use (op0
))
3716 register_edge_assert_for_1 (op0
, code
, e
, asserts
);
3717 if (TREE_CODE (op1
) == SSA_NAME
3718 && has_single_use (op1
))
3719 register_edge_assert_for_1 (op1
, code
, e
, asserts
);
3721 else if (gimple_assign_rhs_code (op_def
) == BIT_NOT_EXPR
3722 && TYPE_PRECISION (TREE_TYPE (gimple_assign_lhs (op_def
))) == 1)
3724 /* Recurse, flipping CODE. */
3725 code
= invert_tree_comparison (code
, false);
3726 register_edge_assert_for_1 (gimple_assign_rhs1 (op_def
), code
, e
, asserts
);
3728 else if (gimple_assign_rhs_code (op_def
) == SSA_NAME
)
3730 /* Recurse through the copy. */
3731 register_edge_assert_for_1 (gimple_assign_rhs1 (op_def
), code
, e
, asserts
);
3733 else if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (op_def
)))
3735 /* Recurse through the type conversion, unless it is a narrowing
3736 conversion or conversion from non-integral type. */
3737 tree rhs
= gimple_assign_rhs1 (op_def
);
3738 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs
))
3739 && (TYPE_PRECISION (TREE_TYPE (rhs
))
3740 <= TYPE_PRECISION (TREE_TYPE (op
))))
3741 register_edge_assert_for_1 (rhs
, code
, e
, asserts
);
3745 /* Check if comparison
3746 NAME COND_OP INTEGER_CST
3748 (X & 11...100..0) COND_OP XX...X00...0
3749 Such comparison can yield assertions like
3752 in case of COND_OP being NE_EXPR or
3755 in case of EQ_EXPR. */
3758 is_masked_range_test (tree name
, tree valt
, enum tree_code cond_code
,
3759 tree
*new_name
, tree
*low
, enum tree_code
*low_code
,
3760 tree
*high
, enum tree_code
*high_code
)
3762 gimple
*def_stmt
= SSA_NAME_DEF_STMT (name
);
3764 if (!is_gimple_assign (def_stmt
)
3765 || gimple_assign_rhs_code (def_stmt
) != BIT_AND_EXPR
)
3768 tree t
= gimple_assign_rhs1 (def_stmt
);
3769 tree maskt
= gimple_assign_rhs2 (def_stmt
);
3770 if (TREE_CODE (t
) != SSA_NAME
|| TREE_CODE (maskt
) != INTEGER_CST
)
3773 wi::tree_to_wide_ref mask
= wi::to_wide (maskt
);
3774 wide_int inv_mask
= ~mask
;
3775 /* Assume VALT is INTEGER_CST. */
3776 wi::tree_to_wide_ref val
= wi::to_wide (valt
);
3778 if ((inv_mask
& (inv_mask
+ 1)) != 0
3779 || (val
& mask
) != val
)
3782 bool is_range
= cond_code
== EQ_EXPR
;
3784 tree type
= TREE_TYPE (t
);
3785 wide_int min
= wi::min_value (type
),
3786 max
= wi::max_value (type
);
3790 *low_code
= val
== min
? ERROR_MARK
: GE_EXPR
;
3791 *high_code
= val
== max
? ERROR_MARK
: LE_EXPR
;
3795 /* We can still generate assertion if one of alternatives
3796 is known to always be false. */
3799 *low_code
= (enum tree_code
) 0;
3800 *high_code
= GT_EXPR
;
3802 else if ((val
| inv_mask
) == max
)
3804 *low_code
= LT_EXPR
;
3805 *high_code
= (enum tree_code
) 0;
3812 *low
= wide_int_to_tree (type
, val
);
3813 *high
= wide_int_to_tree (type
, val
| inv_mask
);
3815 if (wi::neg_p (val
, TYPE_SIGN (type
)))
3816 std::swap (*low
, *high
);
3821 /* Try to register an edge assertion for SSA name NAME on edge E for
3822 the condition COND contributing to the conditional jump pointed to by
3826 register_edge_assert_for (tree name
, edge e
,
3827 enum tree_code cond_code
, tree cond_op0
,
3828 tree cond_op1
, vec
<assert_info
> &asserts
)
3831 enum tree_code comp_code
;
3832 bool is_else_edge
= (e
->flags
& EDGE_FALSE_VALUE
) != 0;
3834 /* Do not attempt to infer anything in names that flow through
3836 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name
))
3839 if (!extract_code_and_val_from_cond_with_ops (name
, cond_code
,
3845 /* Register ASSERT_EXPRs for name. */
3846 register_edge_assert_for_2 (name
, e
, cond_code
, cond_op0
,
3847 cond_op1
, is_else_edge
, asserts
);
3850 /* If COND is effectively an equality test of an SSA_NAME against
3851 the value zero or one, then we may be able to assert values
3852 for SSA_NAMEs which flow into COND. */
3854 /* In the case of NAME == 1 or NAME != 0, for BIT_AND_EXPR defining
3855 statement of NAME we can assert both operands of the BIT_AND_EXPR
3856 have nonzero value. */
3857 if (((comp_code
== EQ_EXPR
&& integer_onep (val
))
3858 || (comp_code
== NE_EXPR
&& integer_zerop (val
))))
3860 gimple
*def_stmt
= SSA_NAME_DEF_STMT (name
);
3862 if (is_gimple_assign (def_stmt
)
3863 && gimple_assign_rhs_code (def_stmt
) == BIT_AND_EXPR
)
3865 tree op0
= gimple_assign_rhs1 (def_stmt
);
3866 tree op1
= gimple_assign_rhs2 (def_stmt
);
3867 register_edge_assert_for_1 (op0
, NE_EXPR
, e
, asserts
);
3868 register_edge_assert_for_1 (op1
, NE_EXPR
, e
, asserts
);
3872 /* In the case of NAME == 0 or NAME != 1, for BIT_IOR_EXPR defining
3873 statement of NAME we can assert both operands of the BIT_IOR_EXPR
3875 if (((comp_code
== EQ_EXPR
&& integer_zerop (val
))
3876 || (comp_code
== NE_EXPR
&& integer_onep (val
))))
3878 gimple
*def_stmt
= SSA_NAME_DEF_STMT (name
);
3880 /* For BIT_IOR_EXPR only if NAME == 0 both operands have
3881 necessarily zero value, or if type-precision is one. */
3882 if (is_gimple_assign (def_stmt
)
3883 && (gimple_assign_rhs_code (def_stmt
) == BIT_IOR_EXPR
3884 && (TYPE_PRECISION (TREE_TYPE (name
)) == 1
3885 || comp_code
== EQ_EXPR
)))
3887 tree op0
= gimple_assign_rhs1 (def_stmt
);
3888 tree op1
= gimple_assign_rhs2 (def_stmt
);
3889 register_edge_assert_for_1 (op0
, EQ_EXPR
, e
, asserts
);
3890 register_edge_assert_for_1 (op1
, EQ_EXPR
, e
, asserts
);
3894 /* Sometimes we can infer ranges from (NAME & MASK) == VALUE. */
3895 if ((comp_code
== EQ_EXPR
|| comp_code
== NE_EXPR
)
3896 && TREE_CODE (val
) == INTEGER_CST
)
3898 enum tree_code low_code
, high_code
;
3900 if (is_masked_range_test (name
, val
, comp_code
, &name
, &low
,
3901 &low_code
, &high
, &high_code
))
3903 if (low_code
!= ERROR_MARK
)
3904 register_edge_assert_for_2 (name
, e
, low_code
, name
,
3905 low
, /*invert*/false, asserts
);
3906 if (high_code
!= ERROR_MARK
)
3907 register_edge_assert_for_2 (name
, e
, high_code
, name
,
3908 high
, /*invert*/false, asserts
);
3913 /* Finish found ASSERTS for E and register them at GSI. */
3916 finish_register_edge_assert_for (edge e
, gimple_stmt_iterator gsi
,
3917 vec
<assert_info
> &asserts
)
3919 for (unsigned i
= 0; i
< asserts
.length (); ++i
)
3920 /* Only register an ASSERT_EXPR if NAME was found in the sub-graph
3921 reachable from E. */
3922 if (live_on_edge (e
, asserts
[i
].name
))
3923 register_new_assert_for (asserts
[i
].name
, asserts
[i
].expr
,
3924 asserts
[i
].comp_code
, asserts
[i
].val
,
3930 /* Determine whether the outgoing edges of BB should receive an
3931 ASSERT_EXPR for each of the operands of BB's LAST statement.
3932 The last statement of BB must be a COND_EXPR.
3934 If any of the sub-graphs rooted at BB have an interesting use of
3935 the predicate operands, an assert location node is added to the
3936 list of assertions for the corresponding operands. */
3939 find_conditional_asserts (basic_block bb
, gcond
*last
)
3941 gimple_stmt_iterator bsi
;
3947 bsi
= gsi_for_stmt (last
);
3949 /* Look for uses of the operands in each of the sub-graphs
3950 rooted at BB. We need to check each of the outgoing edges
3951 separately, so that we know what kind of ASSERT_EXPR to
3953 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
3958 /* Register the necessary assertions for each operand in the
3959 conditional predicate. */
3960 auto_vec
<assert_info
, 8> asserts
;
3961 FOR_EACH_SSA_TREE_OPERAND (op
, last
, iter
, SSA_OP_USE
)
3962 register_edge_assert_for (op
, e
,
3963 gimple_cond_code (last
),
3964 gimple_cond_lhs (last
),
3965 gimple_cond_rhs (last
), asserts
);
3966 finish_register_edge_assert_for (e
, bsi
, asserts
);
3976 /* Compare two case labels sorting first by the destination bb index
3977 and then by the case value. */
3980 compare_case_labels (const void *p1
, const void *p2
)
3982 const struct case_info
*ci1
= (const struct case_info
*) p1
;
3983 const struct case_info
*ci2
= (const struct case_info
*) p2
;
3984 int idx1
= ci1
->bb
->index
;
3985 int idx2
= ci2
->bb
->index
;
3989 else if (idx1
== idx2
)
3991 /* Make sure the default label is first in a group. */
3992 if (!CASE_LOW (ci1
->expr
))
3994 else if (!CASE_LOW (ci2
->expr
))
3997 return tree_int_cst_compare (CASE_LOW (ci1
->expr
),
3998 CASE_LOW (ci2
->expr
));
4004 /* Determine whether the outgoing edges of BB should receive an
4005 ASSERT_EXPR for each of the operands of BB's LAST statement.
4006 The last statement of BB must be a SWITCH_EXPR.
4008 If any of the sub-graphs rooted at BB have an interesting use of
4009 the predicate operands, an assert location node is added to the
4010 list of assertions for the corresponding operands. */
4013 find_switch_asserts (basic_block bb
, gswitch
*last
)
4015 gimple_stmt_iterator bsi
;
4018 struct case_info
*ci
;
4019 size_t n
= gimple_switch_num_labels (last
);
4020 #if GCC_VERSION >= 4000
4023 /* Work around GCC 3.4 bug (PR 37086). */
4024 volatile unsigned int idx
;
4027 bsi
= gsi_for_stmt (last
);
4028 op
= gimple_switch_index (last
);
4029 if (TREE_CODE (op
) != SSA_NAME
)
4032 /* Build a vector of case labels sorted by destination label. */
4033 ci
= XNEWVEC (struct case_info
, n
);
4034 for (idx
= 0; idx
< n
; ++idx
)
4036 ci
[idx
].expr
= gimple_switch_label (last
, idx
);
4037 ci
[idx
].bb
= label_to_block (CASE_LABEL (ci
[idx
].expr
));
4039 edge default_edge
= find_edge (bb
, ci
[0].bb
);
4040 qsort (ci
, n
, sizeof (struct case_info
), compare_case_labels
);
4042 for (idx
= 0; idx
< n
; ++idx
)
4045 tree cl
= ci
[idx
].expr
;
4046 basic_block cbb
= ci
[idx
].bb
;
4048 min
= CASE_LOW (cl
);
4049 max
= CASE_HIGH (cl
);
4051 /* If there are multiple case labels with the same destination
4052 we need to combine them to a single value range for the edge. */
4053 if (idx
+ 1 < n
&& cbb
== ci
[idx
+ 1].bb
)
4055 /* Skip labels until the last of the group. */
4058 } while (idx
< n
&& cbb
== ci
[idx
].bb
);
4061 /* Pick up the maximum of the case label range. */
4062 if (CASE_HIGH (ci
[idx
].expr
))
4063 max
= CASE_HIGH (ci
[idx
].expr
);
4065 max
= CASE_LOW (ci
[idx
].expr
);
4068 /* Can't extract a useful assertion out of a range that includes the
4070 if (min
== NULL_TREE
)
4073 /* Find the edge to register the assert expr on. */
4074 e
= find_edge (bb
, cbb
);
4076 /* Register the necessary assertions for the operand in the
4078 auto_vec
<assert_info
, 8> asserts
;
4079 register_edge_assert_for (op
, e
,
4080 max
? GE_EXPR
: EQ_EXPR
,
4081 op
, fold_convert (TREE_TYPE (op
), min
),
4084 register_edge_assert_for (op
, e
, LE_EXPR
, op
,
4085 fold_convert (TREE_TYPE (op
), max
),
4087 finish_register_edge_assert_for (e
, bsi
, asserts
);
4092 if (!live_on_edge (default_edge
, op
))
4095 /* Now register along the default label assertions that correspond to the
4096 anti-range of each label. */
4097 int insertion_limit
= PARAM_VALUE (PARAM_MAX_VRP_SWITCH_ASSERTIONS
);
4098 if (insertion_limit
== 0)
4101 /* We can't do this if the default case shares a label with another case. */
4102 tree default_cl
= gimple_switch_default_label (last
);
4103 for (idx
= 1; idx
< n
; idx
++)
4106 tree cl
= gimple_switch_label (last
, idx
);
4107 if (CASE_LABEL (cl
) == CASE_LABEL (default_cl
))
4110 min
= CASE_LOW (cl
);
4111 max
= CASE_HIGH (cl
);
4113 /* Combine contiguous case ranges to reduce the number of assertions
4115 for (idx
= idx
+ 1; idx
< n
; idx
++)
4117 tree next_min
, next_max
;
4118 tree next_cl
= gimple_switch_label (last
, idx
);
4119 if (CASE_LABEL (next_cl
) == CASE_LABEL (default_cl
))
4122 next_min
= CASE_LOW (next_cl
);
4123 next_max
= CASE_HIGH (next_cl
);
4125 wide_int difference
= (wi::to_wide (next_min
)
4126 - wi::to_wide (max
? max
: min
));
4127 if (wi::eq_p (difference
, 1))
4128 max
= next_max
? next_max
: next_min
;
4134 if (max
== NULL_TREE
)
4136 /* Register the assertion OP != MIN. */
4137 auto_vec
<assert_info
, 8> asserts
;
4138 min
= fold_convert (TREE_TYPE (op
), min
);
4139 register_edge_assert_for (op
, default_edge
, NE_EXPR
, op
, min
,
4141 finish_register_edge_assert_for (default_edge
, bsi
, asserts
);
4145 /* Register the assertion (unsigned)OP - MIN > (MAX - MIN),
4146 which will give OP the anti-range ~[MIN,MAX]. */
4147 tree uop
= fold_convert (unsigned_type_for (TREE_TYPE (op
)), op
);
4148 min
= fold_convert (TREE_TYPE (uop
), min
);
4149 max
= fold_convert (TREE_TYPE (uop
), max
);
4151 tree lhs
= fold_build2 (MINUS_EXPR
, TREE_TYPE (uop
), uop
, min
);
4152 tree rhs
= int_const_binop (MINUS_EXPR
, max
, min
);
4153 register_new_assert_for (op
, lhs
, GT_EXPR
, rhs
,
4154 NULL
, default_edge
, bsi
);
4157 if (--insertion_limit
== 0)
4163 /* Traverse all the statements in block BB looking for statements that
4164 may generate useful assertions for the SSA names in their operand.
4165 If a statement produces a useful assertion A for name N_i, then the
4166 list of assertions already generated for N_i is scanned to
4167 determine if A is actually needed.
4169 If N_i already had the assertion A at a location dominating the
4170 current location, then nothing needs to be done. Otherwise, the
4171 new location for A is recorded instead.
4173 1- For every statement S in BB, all the variables used by S are
4174 added to bitmap FOUND_IN_SUBGRAPH.
4176 2- If statement S uses an operand N in a way that exposes a known
4177 value range for N, then if N was not already generated by an
4178 ASSERT_EXPR, create a new assert location for N. For instance,
4179 if N is a pointer and the statement dereferences it, we can
4180 assume that N is not NULL.
4182 3- COND_EXPRs are a special case of #2. We can derive range
4183 information from the predicate but need to insert different
4184 ASSERT_EXPRs for each of the sub-graphs rooted at the
4185 conditional block. If the last statement of BB is a conditional
4186 expression of the form 'X op Y', then
4188 a) Remove X and Y from the set FOUND_IN_SUBGRAPH.
4190 b) If the conditional is the only entry point to the sub-graph
4191 corresponding to the THEN_CLAUSE, recurse into it. On
4192 return, if X and/or Y are marked in FOUND_IN_SUBGRAPH, then
4193 an ASSERT_EXPR is added for the corresponding variable.
4195 c) Repeat step (b) on the ELSE_CLAUSE.
4197 d) Mark X and Y in FOUND_IN_SUBGRAPH.
4206 In this case, an assertion on the THEN clause is useful to
4207 determine that 'a' is always 9 on that edge. However, an assertion
4208 on the ELSE clause would be unnecessary.
4210 4- If BB does not end in a conditional expression, then we recurse
4211 into BB's dominator children.
4213 At the end of the recursive traversal, every SSA name will have a
4214 list of locations where ASSERT_EXPRs should be added. When a new
4215 location for name N is found, it is registered by calling
4216 register_new_assert_for. That function keeps track of all the
4217 registered assertions to prevent adding unnecessary assertions.
4218 For instance, if a pointer P_4 is dereferenced more than once in a
4219 dominator tree, only the location dominating all the dereference of
4220 P_4 will receive an ASSERT_EXPR. */
4223 find_assert_locations_1 (basic_block bb
, sbitmap live
)
4227 last
= last_stmt (bb
);
4229 /* If BB's last statement is a conditional statement involving integer
4230 operands, determine if we need to add ASSERT_EXPRs. */
4232 && gimple_code (last
) == GIMPLE_COND
4233 && !fp_predicate (last
)
4234 && !ZERO_SSA_OPERANDS (last
, SSA_OP_USE
))
4235 find_conditional_asserts (bb
, as_a
<gcond
*> (last
));
4237 /* If BB's last statement is a switch statement involving integer
4238 operands, determine if we need to add ASSERT_EXPRs. */
4240 && gimple_code (last
) == GIMPLE_SWITCH
4241 && !ZERO_SSA_OPERANDS (last
, SSA_OP_USE
))
4242 find_switch_asserts (bb
, as_a
<gswitch
*> (last
));
4244 /* Traverse all the statements in BB marking used names and looking
4245 for statements that may infer assertions for their used operands. */
4246 for (gimple_stmt_iterator si
= gsi_last_bb (bb
); !gsi_end_p (si
);
4253 stmt
= gsi_stmt (si
);
4255 if (is_gimple_debug (stmt
))
4258 /* See if we can derive an assertion for any of STMT's operands. */
4259 FOR_EACH_SSA_TREE_OPERAND (op
, stmt
, i
, SSA_OP_USE
)
4262 enum tree_code comp_code
;
4264 /* If op is not live beyond this stmt, do not bother to insert
4266 if (!bitmap_bit_p (live
, SSA_NAME_VERSION (op
)))
4269 /* If OP is used in such a way that we can infer a value
4270 range for it, and we don't find a previous assertion for
4271 it, create a new assertion location node for OP. */
4272 if (infer_value_range (stmt
, op
, &comp_code
, &value
))
4274 /* If we are able to infer a nonzero value range for OP,
4275 then walk backwards through the use-def chain to see if OP
4276 was set via a typecast.
4278 If so, then we can also infer a nonzero value range
4279 for the operand of the NOP_EXPR. */
4280 if (comp_code
== NE_EXPR
&& integer_zerop (value
))
4283 gimple
*def_stmt
= SSA_NAME_DEF_STMT (t
);
4285 while (is_gimple_assign (def_stmt
)
4286 && CONVERT_EXPR_CODE_P
4287 (gimple_assign_rhs_code (def_stmt
))
4289 (gimple_assign_rhs1 (def_stmt
)) == SSA_NAME
4291 (TREE_TYPE (gimple_assign_rhs1 (def_stmt
))))
4293 t
= gimple_assign_rhs1 (def_stmt
);
4294 def_stmt
= SSA_NAME_DEF_STMT (t
);
4296 /* Note we want to register the assert for the
4297 operand of the NOP_EXPR after SI, not after the
4299 if (bitmap_bit_p (live
, SSA_NAME_VERSION (t
)))
4300 register_new_assert_for (t
, t
, comp_code
, value
,
4305 register_new_assert_for (op
, op
, comp_code
, value
, bb
, NULL
, si
);
4310 FOR_EACH_SSA_TREE_OPERAND (op
, stmt
, i
, SSA_OP_USE
)
4311 bitmap_set_bit (live
, SSA_NAME_VERSION (op
));
4312 FOR_EACH_SSA_TREE_OPERAND (op
, stmt
, i
, SSA_OP_DEF
)
4313 bitmap_clear_bit (live
, SSA_NAME_VERSION (op
));
4316 /* Traverse all PHI nodes in BB, updating live. */
4317 for (gphi_iterator si
= gsi_start_phis (bb
); !gsi_end_p (si
);
4320 use_operand_p arg_p
;
4322 gphi
*phi
= si
.phi ();
4323 tree res
= gimple_phi_result (phi
);
4325 if (virtual_operand_p (res
))
4328 FOR_EACH_PHI_ARG (arg_p
, phi
, i
, SSA_OP_USE
)
4330 tree arg
= USE_FROM_PTR (arg_p
);
4331 if (TREE_CODE (arg
) == SSA_NAME
)
4332 bitmap_set_bit (live
, SSA_NAME_VERSION (arg
));
4335 bitmap_clear_bit (live
, SSA_NAME_VERSION (res
));
4339 /* Do an RPO walk over the function computing SSA name liveness
4340 on-the-fly and deciding on assert expressions to insert. */
4343 find_assert_locations (void)
4345 int *rpo
= XNEWVEC (int, last_basic_block_for_fn (cfun
));
4346 int *bb_rpo
= XNEWVEC (int, last_basic_block_for_fn (cfun
));
4347 int *last_rpo
= XCNEWVEC (int, last_basic_block_for_fn (cfun
));
4350 live
= XCNEWVEC (sbitmap
, last_basic_block_for_fn (cfun
));
4351 rpo_cnt
= pre_and_rev_post_order_compute (NULL
, rpo
, false);
4352 for (i
= 0; i
< rpo_cnt
; ++i
)
4355 /* Pre-seed loop latch liveness from loop header PHI nodes. Due to
4356 the order we compute liveness and insert asserts we otherwise
4357 fail to insert asserts into the loop latch. */
4359 FOR_EACH_LOOP (loop
, 0)
4361 i
= loop
->latch
->index
;
4362 unsigned int j
= single_succ_edge (loop
->latch
)->dest_idx
;
4363 for (gphi_iterator gsi
= gsi_start_phis (loop
->header
);
4364 !gsi_end_p (gsi
); gsi_next (&gsi
))
4366 gphi
*phi
= gsi
.phi ();
4367 if (virtual_operand_p (gimple_phi_result (phi
)))
4369 tree arg
= gimple_phi_arg_def (phi
, j
);
4370 if (TREE_CODE (arg
) == SSA_NAME
)
4372 if (live
[i
] == NULL
)
4374 live
[i
] = sbitmap_alloc (num_ssa_names
);
4375 bitmap_clear (live
[i
]);
4377 bitmap_set_bit (live
[i
], SSA_NAME_VERSION (arg
));
4382 for (i
= rpo_cnt
- 1; i
>= 0; --i
)
4384 basic_block bb
= BASIC_BLOCK_FOR_FN (cfun
, rpo
[i
]);
4390 live
[rpo
[i
]] = sbitmap_alloc (num_ssa_names
);
4391 bitmap_clear (live
[rpo
[i
]]);
4394 /* Process BB and update the live information with uses in
4396 find_assert_locations_1 (bb
, live
[rpo
[i
]]);
4398 /* Merge liveness into the predecessor blocks and free it. */
4399 if (!bitmap_empty_p (live
[rpo
[i
]]))
4402 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
4404 int pred
= e
->src
->index
;
4405 if ((e
->flags
& EDGE_DFS_BACK
) || pred
== ENTRY_BLOCK
)
4410 live
[pred
] = sbitmap_alloc (num_ssa_names
);
4411 bitmap_clear (live
[pred
]);
4413 bitmap_ior (live
[pred
], live
[pred
], live
[rpo
[i
]]);
4415 if (bb_rpo
[pred
] < pred_rpo
)
4416 pred_rpo
= bb_rpo
[pred
];
4419 /* Record the RPO number of the last visited block that needs
4420 live information from this block. */
4421 last_rpo
[rpo
[i
]] = pred_rpo
;
4425 sbitmap_free (live
[rpo
[i
]]);
4426 live
[rpo
[i
]] = NULL
;
4429 /* We can free all successors live bitmaps if all their
4430 predecessors have been visited already. */
4431 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
4432 if (last_rpo
[e
->dest
->index
] == i
4433 && live
[e
->dest
->index
])
4435 sbitmap_free (live
[e
->dest
->index
]);
4436 live
[e
->dest
->index
] = NULL
;
4441 XDELETEVEC (bb_rpo
);
4442 XDELETEVEC (last_rpo
);
4443 for (i
= 0; i
< last_basic_block_for_fn (cfun
); ++i
)
4445 sbitmap_free (live
[i
]);
4449 /* Create an ASSERT_EXPR for NAME and insert it in the location
4450 indicated by LOC. Return true if we made any edge insertions. */
4453 process_assert_insertions_for (tree name
, assert_locus
*loc
)
4455 /* Build the comparison expression NAME_i COMP_CODE VAL. */
4458 gimple
*assert_stmt
;
4462 /* If we have X <=> X do not insert an assert expr for that. */
4463 if (loc
->expr
== loc
->val
)
4466 cond
= build2 (loc
->comp_code
, boolean_type_node
, loc
->expr
, loc
->val
);
4467 assert_stmt
= build_assert_expr_for (cond
, name
);
4470 /* We have been asked to insert the assertion on an edge. This
4471 is used only by COND_EXPR and SWITCH_EXPR assertions. */
4472 gcc_checking_assert (gimple_code (gsi_stmt (loc
->si
)) == GIMPLE_COND
4473 || (gimple_code (gsi_stmt (loc
->si
))
4476 gsi_insert_on_edge (loc
->e
, assert_stmt
);
4480 /* If the stmt iterator points at the end then this is an insertion
4481 at the beginning of a block. */
4482 if (gsi_end_p (loc
->si
))
4484 gimple_stmt_iterator si
= gsi_after_labels (loc
->bb
);
4485 gsi_insert_before (&si
, assert_stmt
, GSI_SAME_STMT
);
4489 /* Otherwise, we can insert right after LOC->SI iff the
4490 statement must not be the last statement in the block. */
4491 stmt
= gsi_stmt (loc
->si
);
4492 if (!stmt_ends_bb_p (stmt
))
4494 gsi_insert_after (&loc
->si
, assert_stmt
, GSI_SAME_STMT
);
4498 /* If STMT must be the last statement in BB, we can only insert new
4499 assertions on the non-abnormal edge out of BB. Note that since
4500 STMT is not control flow, there may only be one non-abnormal/eh edge
4502 FOR_EACH_EDGE (e
, ei
, loc
->bb
->succs
)
4503 if (!(e
->flags
& (EDGE_ABNORMAL
|EDGE_EH
)))
4505 gsi_insert_on_edge (e
, assert_stmt
);
4512 /* Qsort helper for sorting assert locations. If stable is true, don't
4513 use iterative_hash_expr because it can be unstable for -fcompare-debug,
4514 on the other side some pointers might be NULL. */
4516 template <bool stable
>
4518 compare_assert_loc (const void *pa
, const void *pb
)
4520 assert_locus
* const a
= *(assert_locus
* const *)pa
;
4521 assert_locus
* const b
= *(assert_locus
* const *)pb
;
4523 /* If stable, some asserts might be optimized away already, sort
4533 if (a
->e
== NULL
&& b
->e
!= NULL
)
4535 else if (a
->e
!= NULL
&& b
->e
== NULL
)
4538 /* After the above checks, we know that (a->e == NULL) == (b->e == NULL),
4539 no need to test both a->e and b->e. */
4541 /* Sort after destination index. */
4544 else if (a
->e
->dest
->index
> b
->e
->dest
->index
)
4546 else if (a
->e
->dest
->index
< b
->e
->dest
->index
)
4549 /* Sort after comp_code. */
4550 if (a
->comp_code
> b
->comp_code
)
4552 else if (a
->comp_code
< b
->comp_code
)
4557 /* E.g. if a->val is ADDR_EXPR of a VAR_DECL, iterative_hash_expr
4558 uses DECL_UID of the VAR_DECL, so sorting might differ between
4559 -g and -g0. When doing the removal of redundant assert exprs
4560 and commonization to successors, this does not matter, but for
4561 the final sort needs to be stable. */
4569 ha
= iterative_hash_expr (a
->expr
, iterative_hash_expr (a
->val
, 0));
4570 hb
= iterative_hash_expr (b
->expr
, iterative_hash_expr (b
->val
, 0));
4573 /* Break the tie using hashing and source/bb index. */
4575 return (a
->e
!= NULL
4576 ? a
->e
->src
->index
- b
->e
->src
->index
4577 : a
->bb
->index
- b
->bb
->index
);
4578 return ha
> hb
? 1 : -1;
4581 /* Process all the insertions registered for every name N_i registered
4582 in NEED_ASSERT_FOR. The list of assertions to be inserted are
4583 found in ASSERTS_FOR[i]. */
4586 process_assert_insertions (void)
4590 bool update_edges_p
= false;
4591 int num_asserts
= 0;
4593 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4594 dump_all_asserts (dump_file
);
4596 EXECUTE_IF_SET_IN_BITMAP (need_assert_for
, 0, i
, bi
)
4598 assert_locus
*loc
= asserts_for
[i
];
4601 auto_vec
<assert_locus
*, 16> asserts
;
4602 for (; loc
; loc
= loc
->next
)
4603 asserts
.safe_push (loc
);
4604 asserts
.qsort (compare_assert_loc
<false>);
4606 /* Push down common asserts to successors and remove redundant ones. */
4608 assert_locus
*common
= NULL
;
4609 unsigned commonj
= 0;
4610 for (unsigned j
= 0; j
< asserts
.length (); ++j
)
4616 || loc
->e
->dest
!= common
->e
->dest
4617 || loc
->comp_code
!= common
->comp_code
4618 || ! operand_equal_p (loc
->val
, common
->val
, 0)
4619 || ! operand_equal_p (loc
->expr
, common
->expr
, 0))
4625 else if (loc
->e
== asserts
[j
-1]->e
)
4627 /* Remove duplicate asserts. */
4628 if (commonj
== j
- 1)
4633 free (asserts
[j
-1]);
4634 asserts
[j
-1] = NULL
;
4639 if (EDGE_COUNT (common
->e
->dest
->preds
) == ecnt
)
4641 /* We have the same assertion on all incoming edges of a BB.
4642 Insert it at the beginning of that block. */
4643 loc
->bb
= loc
->e
->dest
;
4645 loc
->si
= gsi_none ();
4647 /* Clear asserts commoned. */
4648 for (; commonj
!= j
; ++commonj
)
4649 if (asserts
[commonj
])
4651 free (asserts
[commonj
]);
4652 asserts
[commonj
] = NULL
;
4658 /* The asserts vector sorting above might be unstable for
4659 -fcompare-debug, sort again to ensure a stable sort. */
4660 asserts
.qsort (compare_assert_loc
<true>);
4661 for (unsigned j
= 0; j
< asserts
.length (); ++j
)
4666 update_edges_p
|= process_assert_insertions_for (ssa_name (i
), loc
);
4673 gsi_commit_edge_inserts ();
4675 statistics_counter_event (cfun
, "Number of ASSERT_EXPR expressions inserted",
4680 /* Traverse the flowgraph looking for conditional jumps to insert range
4681 expressions. These range expressions are meant to provide information
4682 to optimizations that need to reason in terms of value ranges. They
4683 will not be expanded into RTL. For instance, given:
4692 this pass will transform the code into:
4698 x = ASSERT_EXPR <x, x < y>
4703 y = ASSERT_EXPR <y, x >= y>
4707 The idea is that once copy and constant propagation have run, other
4708 optimizations will be able to determine what ranges of values can 'x'
4709 take in different paths of the code, simply by checking the reaching
4710 definition of 'x'. */
4713 insert_range_assertions (void)
4715 need_assert_for
= BITMAP_ALLOC (NULL
);
4716 asserts_for
= XCNEWVEC (assert_locus
*, num_ssa_names
);
4718 calculate_dominance_info (CDI_DOMINATORS
);
4720 find_assert_locations ();
4721 if (!bitmap_empty_p (need_assert_for
))
4723 process_assert_insertions ();
4724 update_ssa (TODO_update_ssa_no_phi
);
4727 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4729 fprintf (dump_file
, "\nSSA form after inserting ASSERT_EXPRs\n");
4730 dump_function_to_file (current_function_decl
, dump_file
, dump_flags
);
4734 BITMAP_FREE (need_assert_for
);
4737 class vrp_prop
: public ssa_propagation_engine
4740 enum ssa_prop_result
visit_stmt (gimple
*, edge
*, tree
*) FINAL OVERRIDE
;
4741 enum ssa_prop_result
visit_phi (gphi
*) FINAL OVERRIDE
;
4743 void vrp_initialize (void);
4744 void vrp_finalize (bool);
4745 void check_all_array_refs (void);
4746 void check_array_ref (location_t
, tree
, bool);
4747 void search_for_addr_array (tree
, location_t
);
4749 class vr_values vr_values
;
4750 /* Temporary delegator to minimize code churn. */
4751 value_range
*get_value_range (const_tree op
)
4752 { return vr_values
.get_value_range (op
); }
4753 void set_defs_to_varying (gimple
*stmt
)
4754 { return vr_values
.set_defs_to_varying (stmt
); }
4755 void extract_range_from_stmt (gimple
*stmt
, edge
*taken_edge_p
,
4756 tree
*output_p
, value_range
*vr
)
4757 { vr_values
.extract_range_from_stmt (stmt
, taken_edge_p
, output_p
, vr
); }
4758 bool update_value_range (const_tree op
, value_range
*vr
)
4759 { return vr_values
.update_value_range (op
, vr
); }
4760 void extract_range_basic (value_range
*vr
, gimple
*stmt
)
4761 { vr_values
.extract_range_basic (vr
, stmt
); }
4762 void extract_range_from_phi_node (gphi
*phi
, value_range
*vr
)
4763 { vr_values
.extract_range_from_phi_node (phi
, vr
); }
4765 /* Checks one ARRAY_REF in REF, located at LOCUS. Ignores flexible arrays
4766 and "struct" hacks. If VRP can determine that the
4767 array subscript is a constant, check if it is outside valid
4768 range. If the array subscript is a RANGE, warn if it is
4769 non-overlapping with valid range.
4770 IGNORE_OFF_BY_ONE is true if the ARRAY_REF is inside a ADDR_EXPR. */
4773 vrp_prop::check_array_ref (location_t location
, tree ref
,
4774 bool ignore_off_by_one
)
4776 value_range
*vr
= NULL
;
4777 tree low_sub
, up_sub
;
4778 tree low_bound
, up_bound
, up_bound_p1
;
4780 if (TREE_NO_WARNING (ref
))
4783 low_sub
= up_sub
= TREE_OPERAND (ref
, 1);
4784 up_bound
= array_ref_up_bound (ref
);
4787 || TREE_CODE (up_bound
) != INTEGER_CST
4788 || (warn_array_bounds
< 2
4789 && array_at_struct_end_p (ref
)))
4791 /* Accesses to trailing arrays via pointers may access storage
4792 beyond the types array bounds. For such arrays, or for flexible
4793 array members, as well as for other arrays of an unknown size,
4794 replace the upper bound with a more permissive one that assumes
4795 the size of the largest object is PTRDIFF_MAX. */
4796 tree eltsize
= array_ref_element_size (ref
);
4798 if (TREE_CODE (eltsize
) != INTEGER_CST
4799 || integer_zerop (eltsize
))
4801 up_bound
= NULL_TREE
;
4802 up_bound_p1
= NULL_TREE
;
4806 tree maxbound
= TYPE_MAX_VALUE (ptrdiff_type_node
);
4807 tree arg
= TREE_OPERAND (ref
, 0);
4810 if (get_addr_base_and_unit_offset (arg
, &off
) && off
> 0)
4811 maxbound
= wide_int_to_tree (sizetype
,
4812 wi::sub (wi::to_wide (maxbound
),
4815 maxbound
= fold_convert (sizetype
, maxbound
);
4817 up_bound_p1
= int_const_binop (TRUNC_DIV_EXPR
, maxbound
, eltsize
);
4819 up_bound
= int_const_binop (MINUS_EXPR
, up_bound_p1
,
4820 build_int_cst (ptrdiff_type_node
, 1));
4824 up_bound_p1
= int_const_binop (PLUS_EXPR
, up_bound
,
4825 build_int_cst (TREE_TYPE (up_bound
), 1));
4827 low_bound
= array_ref_low_bound (ref
);
4829 tree artype
= TREE_TYPE (TREE_OPERAND (ref
, 0));
4832 if (up_bound
&& tree_int_cst_equal (low_bound
, up_bound_p1
))
4834 warning_at (location
, OPT_Warray_bounds
,
4835 "array subscript %E is above array bounds of %qT",
4837 TREE_NO_WARNING (ref
) = 1;
4840 if (TREE_CODE (low_sub
) == SSA_NAME
)
4842 vr
= get_value_range (low_sub
);
4843 if (vr
->type
== VR_RANGE
|| vr
->type
== VR_ANTI_RANGE
)
4845 low_sub
= vr
->type
== VR_RANGE
? vr
->max
: vr
->min
;
4846 up_sub
= vr
->type
== VR_RANGE
? vr
->min
: vr
->max
;
4850 if (vr
&& vr
->type
== VR_ANTI_RANGE
)
4853 && TREE_CODE (up_sub
) == INTEGER_CST
4854 && (ignore_off_by_one
4855 ? tree_int_cst_lt (up_bound
, up_sub
)
4856 : tree_int_cst_le (up_bound
, up_sub
))
4857 && TREE_CODE (low_sub
) == INTEGER_CST
4858 && tree_int_cst_le (low_sub
, low_bound
))
4860 warning_at (location
, OPT_Warray_bounds
,
4861 "array subscript [%E, %E] is outside array bounds of %qT",
4862 low_sub
, up_sub
, artype
);
4863 TREE_NO_WARNING (ref
) = 1;
4867 && TREE_CODE (up_sub
) == INTEGER_CST
4868 && (ignore_off_by_one
4869 ? !tree_int_cst_le (up_sub
, up_bound_p1
)
4870 : !tree_int_cst_le (up_sub
, up_bound
)))
4872 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4874 fprintf (dump_file
, "Array bound warning for ");
4875 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, ref
);
4876 fprintf (dump_file
, "\n");
4878 warning_at (location
, OPT_Warray_bounds
,
4879 "array subscript %E is above array bounds of %qT",
4881 TREE_NO_WARNING (ref
) = 1;
4883 else if (TREE_CODE (low_sub
) == INTEGER_CST
4884 && tree_int_cst_lt (low_sub
, low_bound
))
4886 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4888 fprintf (dump_file
, "Array bound warning for ");
4889 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, ref
);
4890 fprintf (dump_file
, "\n");
4892 warning_at (location
, OPT_Warray_bounds
,
4893 "array subscript %E is below array bounds of %qT",
4895 TREE_NO_WARNING (ref
) = 1;
4899 /* Searches if the expr T, located at LOCATION computes
4900 address of an ARRAY_REF, and call check_array_ref on it. */
4903 vrp_prop::search_for_addr_array (tree t
, location_t location
)
4905 /* Check each ARRAY_REFs in the reference chain. */
4908 if (TREE_CODE (t
) == ARRAY_REF
)
4909 check_array_ref (location
, t
, true /*ignore_off_by_one*/);
4911 t
= TREE_OPERAND (t
, 0);
4913 while (handled_component_p (t
));
4915 if (TREE_CODE (t
) == MEM_REF
4916 && TREE_CODE (TREE_OPERAND (t
, 0)) == ADDR_EXPR
4917 && !TREE_NO_WARNING (t
))
4919 tree tem
= TREE_OPERAND (TREE_OPERAND (t
, 0), 0);
4920 tree low_bound
, up_bound
, el_sz
;
4922 if (TREE_CODE (TREE_TYPE (tem
)) != ARRAY_TYPE
4923 || TREE_CODE (TREE_TYPE (TREE_TYPE (tem
))) == ARRAY_TYPE
4924 || !TYPE_DOMAIN (TREE_TYPE (tem
)))
4927 low_bound
= TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (tem
)));
4928 up_bound
= TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (tem
)));
4929 el_sz
= TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (tem
)));
4931 || TREE_CODE (low_bound
) != INTEGER_CST
4933 || TREE_CODE (up_bound
) != INTEGER_CST
4935 || TREE_CODE (el_sz
) != INTEGER_CST
)
4938 idx
= mem_ref_offset (t
);
4939 idx
= wi::sdiv_trunc (idx
, wi::to_offset (el_sz
));
4942 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4944 fprintf (dump_file
, "Array bound warning for ");
4945 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, t
);
4946 fprintf (dump_file
, "\n");
4948 warning_at (location
, OPT_Warray_bounds
,
4949 "array subscript %wi is below array bounds of %qT",
4950 idx
.to_shwi (), TREE_TYPE (tem
));
4951 TREE_NO_WARNING (t
) = 1;
4953 else if (idx
> (wi::to_offset (up_bound
)
4954 - wi::to_offset (low_bound
) + 1))
4956 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4958 fprintf (dump_file
, "Array bound warning for ");
4959 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, t
);
4960 fprintf (dump_file
, "\n");
4962 warning_at (location
, OPT_Warray_bounds
,
4963 "array subscript %wu is above array bounds of %qT",
4964 idx
.to_uhwi (), TREE_TYPE (tem
));
4965 TREE_NO_WARNING (t
) = 1;
4970 /* walk_tree() callback that checks if *TP is
4971 an ARRAY_REF inside an ADDR_EXPR (in which an array
4972 subscript one outside the valid range is allowed). Call
4973 check_array_ref for each ARRAY_REF found. The location is
4977 check_array_bounds (tree
*tp
, int *walk_subtree
, void *data
)
4980 struct walk_stmt_info
*wi
= (struct walk_stmt_info
*) data
;
4981 location_t location
;
4983 if (EXPR_HAS_LOCATION (t
))
4984 location
= EXPR_LOCATION (t
);
4986 location
= gimple_location (wi
->stmt
);
4988 *walk_subtree
= TRUE
;
4990 vrp_prop
*vrp_prop
= (class vrp_prop
*)wi
->info
;
4991 if (TREE_CODE (t
) == ARRAY_REF
)
4992 vrp_prop
->check_array_ref (location
, t
, false /*ignore_off_by_one*/);
4994 else if (TREE_CODE (t
) == ADDR_EXPR
)
4996 vrp_prop
->search_for_addr_array (t
, location
);
4997 *walk_subtree
= FALSE
;
5003 /* Walk over all statements of all reachable BBs and call check_array_bounds
5007 vrp_prop::check_all_array_refs ()
5010 gimple_stmt_iterator si
;
5012 FOR_EACH_BB_FN (bb
, cfun
)
5016 bool executable
= false;
5018 /* Skip blocks that were found to be unreachable. */
5019 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
5020 executable
|= !!(e
->flags
& EDGE_EXECUTABLE
);
5024 for (si
= gsi_start_bb (bb
); !gsi_end_p (si
); gsi_next (&si
))
5026 gimple
*stmt
= gsi_stmt (si
);
5027 struct walk_stmt_info wi
;
5028 if (!gimple_has_location (stmt
)
5029 || is_gimple_debug (stmt
))
5032 memset (&wi
, 0, sizeof (wi
));
5036 walk_gimple_op (gsi_stmt (si
),
5043 /* Return true if all imm uses of VAR are either in STMT, or
5044 feed (optionally through a chain of single imm uses) GIMPLE_COND
5045 in basic block COND_BB. */
5048 all_imm_uses_in_stmt_or_feed_cond (tree var
, gimple
*stmt
, basic_block cond_bb
)
5050 use_operand_p use_p
, use2_p
;
5051 imm_use_iterator iter
;
5053 FOR_EACH_IMM_USE_FAST (use_p
, iter
, var
)
5054 if (USE_STMT (use_p
) != stmt
)
5056 gimple
*use_stmt
= USE_STMT (use_p
), *use_stmt2
;
5057 if (is_gimple_debug (use_stmt
))
5059 while (is_gimple_assign (use_stmt
)
5060 && TREE_CODE (gimple_assign_lhs (use_stmt
)) == SSA_NAME
5061 && single_imm_use (gimple_assign_lhs (use_stmt
),
5062 &use2_p
, &use_stmt2
))
5063 use_stmt
= use_stmt2
;
5064 if (gimple_code (use_stmt
) != GIMPLE_COND
5065 || gimple_bb (use_stmt
) != cond_bb
)
5078 __builtin_unreachable ();
5080 x_5 = ASSERT_EXPR <x_3, ...>;
5081 If x_3 has no other immediate uses (checked by caller),
5082 var is the x_3 var from ASSERT_EXPR, we can clear low 5 bits
5083 from the non-zero bitmask. */
5086 maybe_set_nonzero_bits (edge e
, tree var
)
5088 basic_block cond_bb
= e
->src
;
5089 gimple
*stmt
= last_stmt (cond_bb
);
5093 || gimple_code (stmt
) != GIMPLE_COND
5094 || gimple_cond_code (stmt
) != ((e
->flags
& EDGE_TRUE_VALUE
)
5095 ? EQ_EXPR
: NE_EXPR
)
5096 || TREE_CODE (gimple_cond_lhs (stmt
)) != SSA_NAME
5097 || !integer_zerop (gimple_cond_rhs (stmt
)))
5100 stmt
= SSA_NAME_DEF_STMT (gimple_cond_lhs (stmt
));
5101 if (!is_gimple_assign (stmt
)
5102 || gimple_assign_rhs_code (stmt
) != BIT_AND_EXPR
5103 || TREE_CODE (gimple_assign_rhs2 (stmt
)) != INTEGER_CST
)
5105 if (gimple_assign_rhs1 (stmt
) != var
)
5109 if (TREE_CODE (gimple_assign_rhs1 (stmt
)) != SSA_NAME
)
5111 stmt2
= SSA_NAME_DEF_STMT (gimple_assign_rhs1 (stmt
));
5112 if (!gimple_assign_cast_p (stmt2
)
5113 || gimple_assign_rhs1 (stmt2
) != var
5114 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (stmt2
))
5115 || (TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (stmt
)))
5116 != TYPE_PRECISION (TREE_TYPE (var
))))
5119 cst
= gimple_assign_rhs2 (stmt
);
5120 set_nonzero_bits (var
, wi::bit_and_not (get_nonzero_bits (var
),
5121 wi::to_wide (cst
)));
5124 /* Convert range assertion expressions into the implied copies and
5125 copy propagate away the copies. Doing the trivial copy propagation
5126 here avoids the need to run the full copy propagation pass after
5129 FIXME, this will eventually lead to copy propagation removing the
5130 names that had useful range information attached to them. For
5131 instance, if we had the assertion N_i = ASSERT_EXPR <N_j, N_j > 3>,
5132 then N_i will have the range [3, +INF].
5134 However, by converting the assertion into the implied copy
5135 operation N_i = N_j, we will then copy-propagate N_j into the uses
5136 of N_i and lose the range information. We may want to hold on to
5137 ASSERT_EXPRs a little while longer as the ranges could be used in
5138 things like jump threading.
5140 The problem with keeping ASSERT_EXPRs around is that passes after
5141 VRP need to handle them appropriately.
5143 Another approach would be to make the range information a first
5144 class property of the SSA_NAME so that it can be queried from
5145 any pass. This is made somewhat more complex by the need for
5146 multiple ranges to be associated with one SSA_NAME. */
5149 remove_range_assertions (void)
5152 gimple_stmt_iterator si
;
5153 /* 1 if looking at ASSERT_EXPRs immediately at the beginning of
5154 a basic block preceeded by GIMPLE_COND branching to it and
5155 __builtin_trap, -1 if not yet checked, 0 otherwise. */
5158 /* Note that the BSI iterator bump happens at the bottom of the
5159 loop and no bump is necessary if we're removing the statement
5160 referenced by the current BSI. */
5161 FOR_EACH_BB_FN (bb
, cfun
)
5162 for (si
= gsi_after_labels (bb
), is_unreachable
= -1; !gsi_end_p (si
);)
5164 gimple
*stmt
= gsi_stmt (si
);
5166 if (is_gimple_assign (stmt
)
5167 && gimple_assign_rhs_code (stmt
) == ASSERT_EXPR
)
5169 tree lhs
= gimple_assign_lhs (stmt
);
5170 tree rhs
= gimple_assign_rhs1 (stmt
);
5173 var
= ASSERT_EXPR_VAR (rhs
);
5175 if (TREE_CODE (var
) == SSA_NAME
5176 && !POINTER_TYPE_P (TREE_TYPE (lhs
))
5177 && SSA_NAME_RANGE_INFO (lhs
))
5179 if (is_unreachable
== -1)
5182 if (single_pred_p (bb
)
5183 && assert_unreachable_fallthru_edge_p
5184 (single_pred_edge (bb
)))
5188 if (x_7 >= 10 && x_7 < 20)
5189 __builtin_unreachable ();
5190 x_8 = ASSERT_EXPR <x_7, ...>;
5191 if the only uses of x_7 are in the ASSERT_EXPR and
5192 in the condition. In that case, we can copy the
5193 range info from x_8 computed in this pass also
5196 && all_imm_uses_in_stmt_or_feed_cond (var
, stmt
,
5199 set_range_info (var
, SSA_NAME_RANGE_TYPE (lhs
),
5200 SSA_NAME_RANGE_INFO (lhs
)->get_min (),
5201 SSA_NAME_RANGE_INFO (lhs
)->get_max ());
5202 maybe_set_nonzero_bits (single_pred_edge (bb
), var
);
5206 /* Propagate the RHS into every use of the LHS. For SSA names
5207 also propagate abnormals as it merely restores the original
5208 IL in this case (an replace_uses_by would assert). */
5209 if (TREE_CODE (var
) == SSA_NAME
)
5211 imm_use_iterator iter
;
5212 use_operand_p use_p
;
5214 FOR_EACH_IMM_USE_STMT (use_stmt
, iter
, lhs
)
5215 FOR_EACH_IMM_USE_ON_STMT (use_p
, iter
)
5216 SET_USE (use_p
, var
);
5219 replace_uses_by (lhs
, var
);
5221 /* And finally, remove the copy, it is not needed. */
5222 gsi_remove (&si
, true);
5223 release_defs (stmt
);
5227 if (!is_gimple_debug (gsi_stmt (si
)))
5235 /* Return true if STMT is interesting for VRP. */
5238 stmt_interesting_for_vrp (gimple
*stmt
)
5240 if (gimple_code (stmt
) == GIMPLE_PHI
)
5242 tree res
= gimple_phi_result (stmt
);
5243 return (!virtual_operand_p (res
)
5244 && (INTEGRAL_TYPE_P (TREE_TYPE (res
))
5245 || POINTER_TYPE_P (TREE_TYPE (res
))));
5247 else if (is_gimple_assign (stmt
) || is_gimple_call (stmt
))
5249 tree lhs
= gimple_get_lhs (stmt
);
5251 /* In general, assignments with virtual operands are not useful
5252 for deriving ranges, with the obvious exception of calls to
5253 builtin functions. */
5254 if (lhs
&& TREE_CODE (lhs
) == SSA_NAME
5255 && (INTEGRAL_TYPE_P (TREE_TYPE (lhs
))
5256 || POINTER_TYPE_P (TREE_TYPE (lhs
)))
5257 && (is_gimple_call (stmt
)
5258 || !gimple_vuse (stmt
)))
5260 else if (is_gimple_call (stmt
) && gimple_call_internal_p (stmt
))
5261 switch (gimple_call_internal_fn (stmt
))
5263 case IFN_ADD_OVERFLOW
:
5264 case IFN_SUB_OVERFLOW
:
5265 case IFN_MUL_OVERFLOW
:
5266 case IFN_ATOMIC_COMPARE_EXCHANGE
:
5267 /* These internal calls return _Complex integer type,
5268 but are interesting to VRP nevertheless. */
5269 if (lhs
&& TREE_CODE (lhs
) == SSA_NAME
)
5276 else if (gimple_code (stmt
) == GIMPLE_COND
5277 || gimple_code (stmt
) == GIMPLE_SWITCH
)
5283 /* Initialization required by ssa_propagate engine. */
5286 vrp_prop::vrp_initialize ()
5290 FOR_EACH_BB_FN (bb
, cfun
)
5292 for (gphi_iterator si
= gsi_start_phis (bb
); !gsi_end_p (si
);
5295 gphi
*phi
= si
.phi ();
5296 if (!stmt_interesting_for_vrp (phi
))
5298 tree lhs
= PHI_RESULT (phi
);
5299 set_value_range_to_varying (get_value_range (lhs
));
5300 prop_set_simulate_again (phi
, false);
5303 prop_set_simulate_again (phi
, true);
5306 for (gimple_stmt_iterator si
= gsi_start_bb (bb
); !gsi_end_p (si
);
5309 gimple
*stmt
= gsi_stmt (si
);
5311 /* If the statement is a control insn, then we do not
5312 want to avoid simulating the statement once. Failure
5313 to do so means that those edges will never get added. */
5314 if (stmt_ends_bb_p (stmt
))
5315 prop_set_simulate_again (stmt
, true);
5316 else if (!stmt_interesting_for_vrp (stmt
))
5318 set_defs_to_varying (stmt
);
5319 prop_set_simulate_again (stmt
, false);
5322 prop_set_simulate_again (stmt
, true);
5327 /* Searches the case label vector VEC for the index *IDX of the CASE_LABEL
5328 that includes the value VAL. The search is restricted to the range
5329 [START_IDX, n - 1] where n is the size of VEC.
5331 If there is a CASE_LABEL for VAL, its index is placed in IDX and true is
5334 If there is no CASE_LABEL for VAL and there is one that is larger than VAL,
5335 it is placed in IDX and false is returned.
5337 If VAL is larger than any CASE_LABEL, n is placed on IDX and false is
5341 find_case_label_index (gswitch
*stmt
, size_t start_idx
, tree val
, size_t *idx
)
5343 size_t n
= gimple_switch_num_labels (stmt
);
5346 /* Find case label for minimum of the value range or the next one.
5347 At each iteration we are searching in [low, high - 1]. */
5349 for (low
= start_idx
, high
= n
; high
!= low
; )
5353 /* Note that i != high, so we never ask for n. */
5354 size_t i
= (high
+ low
) / 2;
5355 t
= gimple_switch_label (stmt
, i
);
5357 /* Cache the result of comparing CASE_LOW and val. */
5358 cmp
= tree_int_cst_compare (CASE_LOW (t
), val
);
5362 /* Ranges cannot be empty. */
5371 if (CASE_HIGH (t
) != NULL
5372 && tree_int_cst_compare (CASE_HIGH (t
), val
) >= 0)
5384 /* Searches the case label vector VEC for the range of CASE_LABELs that is used
5385 for values between MIN and MAX. The first index is placed in MIN_IDX. The
5386 last index is placed in MAX_IDX. If the range of CASE_LABELs is empty
5387 then MAX_IDX < MIN_IDX.
5388 Returns true if the default label is not needed. */
5391 find_case_label_range (gswitch
*stmt
, tree min
, tree max
, size_t *min_idx
,
5395 bool min_take_default
= !find_case_label_index (stmt
, 1, min
, &i
);
5396 bool max_take_default
= !find_case_label_index (stmt
, i
, max
, &j
);
5400 && max_take_default
)
5402 /* Only the default case label reached.
5403 Return an empty range. */
5410 bool take_default
= min_take_default
|| max_take_default
;
5414 if (max_take_default
)
5417 /* If the case label range is continuous, we do not need
5418 the default case label. Verify that. */
5419 high
= CASE_LOW (gimple_switch_label (stmt
, i
));
5420 if (CASE_HIGH (gimple_switch_label (stmt
, i
)))
5421 high
= CASE_HIGH (gimple_switch_label (stmt
, i
));
5422 for (k
= i
+ 1; k
<= j
; ++k
)
5424 low
= CASE_LOW (gimple_switch_label (stmt
, k
));
5425 if (!integer_onep (int_const_binop (MINUS_EXPR
, low
, high
)))
5427 take_default
= true;
5431 if (CASE_HIGH (gimple_switch_label (stmt
, k
)))
5432 high
= CASE_HIGH (gimple_switch_label (stmt
, k
));
5437 return !take_default
;
5441 /* Evaluate statement STMT. If the statement produces a useful range,
5442 return SSA_PROP_INTERESTING and record the SSA name with the
5443 interesting range into *OUTPUT_P.
5445 If STMT is a conditional branch and we can determine its truth
5446 value, the taken edge is recorded in *TAKEN_EDGE_P.
5448 If STMT produces a varying value, return SSA_PROP_VARYING. */
5450 enum ssa_prop_result
5451 vrp_prop::visit_stmt (gimple
*stmt
, edge
*taken_edge_p
, tree
*output_p
)
5453 value_range vr
= VR_INITIALIZER
;
5454 tree lhs
= gimple_get_lhs (stmt
);
5455 extract_range_from_stmt (stmt
, taken_edge_p
, output_p
, &vr
);
5459 if (update_value_range (*output_p
, &vr
))
5461 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
5463 fprintf (dump_file
, "Found new range for ");
5464 print_generic_expr (dump_file
, *output_p
);
5465 fprintf (dump_file
, ": ");
5466 dump_value_range (dump_file
, &vr
);
5467 fprintf (dump_file
, "\n");
5470 if (vr
.type
== VR_VARYING
)
5471 return SSA_PROP_VARYING
;
5473 return SSA_PROP_INTERESTING
;
5475 return SSA_PROP_NOT_INTERESTING
;
5478 if (is_gimple_call (stmt
) && gimple_call_internal_p (stmt
))
5479 switch (gimple_call_internal_fn (stmt
))
5481 case IFN_ADD_OVERFLOW
:
5482 case IFN_SUB_OVERFLOW
:
5483 case IFN_MUL_OVERFLOW
:
5484 case IFN_ATOMIC_COMPARE_EXCHANGE
:
5485 /* These internal calls return _Complex integer type,
5486 which VRP does not track, but the immediate uses
5487 thereof might be interesting. */
5488 if (lhs
&& TREE_CODE (lhs
) == SSA_NAME
)
5490 imm_use_iterator iter
;
5491 use_operand_p use_p
;
5492 enum ssa_prop_result res
= SSA_PROP_VARYING
;
5494 set_value_range_to_varying (get_value_range (lhs
));
5496 FOR_EACH_IMM_USE_FAST (use_p
, iter
, lhs
)
5498 gimple
*use_stmt
= USE_STMT (use_p
);
5499 if (!is_gimple_assign (use_stmt
))
5501 enum tree_code rhs_code
= gimple_assign_rhs_code (use_stmt
);
5502 if (rhs_code
!= REALPART_EXPR
&& rhs_code
!= IMAGPART_EXPR
)
5504 tree rhs1
= gimple_assign_rhs1 (use_stmt
);
5505 tree use_lhs
= gimple_assign_lhs (use_stmt
);
5506 if (TREE_CODE (rhs1
) != rhs_code
5507 || TREE_OPERAND (rhs1
, 0) != lhs
5508 || TREE_CODE (use_lhs
) != SSA_NAME
5509 || !stmt_interesting_for_vrp (use_stmt
)
5510 || (!INTEGRAL_TYPE_P (TREE_TYPE (use_lhs
))
5511 || !TYPE_MIN_VALUE (TREE_TYPE (use_lhs
))
5512 || !TYPE_MAX_VALUE (TREE_TYPE (use_lhs
))))
5515 /* If there is a change in the value range for any of the
5516 REALPART_EXPR/IMAGPART_EXPR immediate uses, return
5517 SSA_PROP_INTERESTING. If there are any REALPART_EXPR
5518 or IMAGPART_EXPR immediate uses, but none of them have
5519 a change in their value ranges, return
5520 SSA_PROP_NOT_INTERESTING. If there are no
5521 {REAL,IMAG}PART_EXPR uses at all,
5522 return SSA_PROP_VARYING. */
5523 value_range new_vr
= VR_INITIALIZER
;
5524 extract_range_basic (&new_vr
, use_stmt
);
5525 value_range
*old_vr
= get_value_range (use_lhs
);
5526 if (old_vr
->type
!= new_vr
.type
5527 || !vrp_operand_equal_p (old_vr
->min
, new_vr
.min
)
5528 || !vrp_operand_equal_p (old_vr
->max
, new_vr
.max
)
5529 || !vrp_bitmap_equal_p (old_vr
->equiv
, new_vr
.equiv
))
5530 res
= SSA_PROP_INTERESTING
;
5532 res
= SSA_PROP_NOT_INTERESTING
;
5533 BITMAP_FREE (new_vr
.equiv
);
5534 if (res
== SSA_PROP_INTERESTING
)
5548 /* All other statements produce nothing of interest for VRP, so mark
5549 their outputs varying and prevent further simulation. */
5550 set_defs_to_varying (stmt
);
5552 return (*taken_edge_p
) ? SSA_PROP_INTERESTING
: SSA_PROP_VARYING
;
5555 /* Union the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
5556 { VR1TYPE, VR0MIN, VR0MAX } and store the result
5557 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
5558 possible such range. The resulting range is not canonicalized. */
5561 union_ranges (enum value_range_type
*vr0type
,
5562 tree
*vr0min
, tree
*vr0max
,
5563 enum value_range_type vr1type
,
5564 tree vr1min
, tree vr1max
)
5566 bool mineq
= vrp_operand_equal_p (*vr0min
, vr1min
);
5567 bool maxeq
= vrp_operand_equal_p (*vr0max
, vr1max
);
5569 /* [] is vr0, () is vr1 in the following classification comments. */
5573 if (*vr0type
== vr1type
)
5574 /* Nothing to do for equal ranges. */
5576 else if ((*vr0type
== VR_RANGE
5577 && vr1type
== VR_ANTI_RANGE
)
5578 || (*vr0type
== VR_ANTI_RANGE
5579 && vr1type
== VR_RANGE
))
5581 /* For anti-range with range union the result is varying. */
5587 else if (operand_less_p (*vr0max
, vr1min
) == 1
5588 || operand_less_p (vr1max
, *vr0min
) == 1)
5590 /* [ ] ( ) or ( ) [ ]
5591 If the ranges have an empty intersection, result of the union
5592 operation is the anti-range or if both are anti-ranges
5594 if (*vr0type
== VR_ANTI_RANGE
5595 && vr1type
== VR_ANTI_RANGE
)
5597 else if (*vr0type
== VR_ANTI_RANGE
5598 && vr1type
== VR_RANGE
)
5600 else if (*vr0type
== VR_RANGE
5601 && vr1type
== VR_ANTI_RANGE
)
5607 else if (*vr0type
== VR_RANGE
5608 && vr1type
== VR_RANGE
)
5610 /* The result is the convex hull of both ranges. */
5611 if (operand_less_p (*vr0max
, vr1min
) == 1)
5613 /* If the result can be an anti-range, create one. */
5614 if (TREE_CODE (*vr0max
) == INTEGER_CST
5615 && TREE_CODE (vr1min
) == INTEGER_CST
5616 && vrp_val_is_min (*vr0min
)
5617 && vrp_val_is_max (vr1max
))
5619 tree min
= int_const_binop (PLUS_EXPR
,
5621 build_int_cst (TREE_TYPE (*vr0max
), 1));
5622 tree max
= int_const_binop (MINUS_EXPR
,
5624 build_int_cst (TREE_TYPE (vr1min
), 1));
5625 if (!operand_less_p (max
, min
))
5627 *vr0type
= VR_ANTI_RANGE
;
5639 /* If the result can be an anti-range, create one. */
5640 if (TREE_CODE (vr1max
) == INTEGER_CST
5641 && TREE_CODE (*vr0min
) == INTEGER_CST
5642 && vrp_val_is_min (vr1min
)
5643 && vrp_val_is_max (*vr0max
))
5645 tree min
= int_const_binop (PLUS_EXPR
,
5647 build_int_cst (TREE_TYPE (vr1max
), 1));
5648 tree max
= int_const_binop (MINUS_EXPR
,
5650 build_int_cst (TREE_TYPE (*vr0min
), 1));
5651 if (!operand_less_p (max
, min
))
5653 *vr0type
= VR_ANTI_RANGE
;
5667 else if ((maxeq
|| operand_less_p (vr1max
, *vr0max
) == 1)
5668 && (mineq
|| operand_less_p (*vr0min
, vr1min
) == 1))
5670 /* [ ( ) ] or [( ) ] or [ ( )] */
5671 if (*vr0type
== VR_RANGE
5672 && vr1type
== VR_RANGE
)
5674 else if (*vr0type
== VR_ANTI_RANGE
5675 && vr1type
== VR_ANTI_RANGE
)
5681 else if (*vr0type
== VR_ANTI_RANGE
5682 && vr1type
== VR_RANGE
)
5684 /* Arbitrarily choose the right or left gap. */
5685 if (!mineq
&& TREE_CODE (vr1min
) == INTEGER_CST
)
5686 *vr0max
= int_const_binop (MINUS_EXPR
, vr1min
,
5687 build_int_cst (TREE_TYPE (vr1min
), 1));
5688 else if (!maxeq
&& TREE_CODE (vr1max
) == INTEGER_CST
)
5689 *vr0min
= int_const_binop (PLUS_EXPR
, vr1max
,
5690 build_int_cst (TREE_TYPE (vr1max
), 1));
5694 else if (*vr0type
== VR_RANGE
5695 && vr1type
== VR_ANTI_RANGE
)
5696 /* The result covers everything. */
5701 else if ((maxeq
|| operand_less_p (*vr0max
, vr1max
) == 1)
5702 && (mineq
|| operand_less_p (vr1min
, *vr0min
) == 1))
5704 /* ( [ ] ) or ([ ] ) or ( [ ]) */
5705 if (*vr0type
== VR_RANGE
5706 && vr1type
== VR_RANGE
)
5712 else if (*vr0type
== VR_ANTI_RANGE
5713 && vr1type
== VR_ANTI_RANGE
)
5715 else if (*vr0type
== VR_RANGE
5716 && vr1type
== VR_ANTI_RANGE
)
5718 *vr0type
= VR_ANTI_RANGE
;
5719 if (!mineq
&& TREE_CODE (*vr0min
) == INTEGER_CST
)
5721 *vr0max
= int_const_binop (MINUS_EXPR
, *vr0min
,
5722 build_int_cst (TREE_TYPE (*vr0min
), 1));
5725 else if (!maxeq
&& TREE_CODE (*vr0max
) == INTEGER_CST
)
5727 *vr0min
= int_const_binop (PLUS_EXPR
, *vr0max
,
5728 build_int_cst (TREE_TYPE (*vr0max
), 1));
5734 else if (*vr0type
== VR_ANTI_RANGE
5735 && vr1type
== VR_RANGE
)
5736 /* The result covers everything. */
5741 else if ((operand_less_p (vr1min
, *vr0max
) == 1
5742 || operand_equal_p (vr1min
, *vr0max
, 0))
5743 && operand_less_p (*vr0min
, vr1min
) == 1
5744 && operand_less_p (*vr0max
, vr1max
) == 1)
5746 /* [ ( ] ) or [ ]( ) */
5747 if (*vr0type
== VR_RANGE
5748 && vr1type
== VR_RANGE
)
5750 else if (*vr0type
== VR_ANTI_RANGE
5751 && vr1type
== VR_ANTI_RANGE
)
5753 else if (*vr0type
== VR_ANTI_RANGE
5754 && vr1type
== VR_RANGE
)
5756 if (TREE_CODE (vr1min
) == INTEGER_CST
)
5757 *vr0max
= int_const_binop (MINUS_EXPR
, vr1min
,
5758 build_int_cst (TREE_TYPE (vr1min
), 1));
5762 else if (*vr0type
== VR_RANGE
5763 && vr1type
== VR_ANTI_RANGE
)
5765 if (TREE_CODE (*vr0max
) == INTEGER_CST
)
5768 *vr0min
= int_const_binop (PLUS_EXPR
, *vr0max
,
5769 build_int_cst (TREE_TYPE (*vr0max
), 1));
5778 else if ((operand_less_p (*vr0min
, vr1max
) == 1
5779 || operand_equal_p (*vr0min
, vr1max
, 0))
5780 && operand_less_p (vr1min
, *vr0min
) == 1
5781 && operand_less_p (vr1max
, *vr0max
) == 1)
5783 /* ( [ ) ] or ( )[ ] */
5784 if (*vr0type
== VR_RANGE
5785 && vr1type
== VR_RANGE
)
5787 else if (*vr0type
== VR_ANTI_RANGE
5788 && vr1type
== VR_ANTI_RANGE
)
5790 else if (*vr0type
== VR_ANTI_RANGE
5791 && vr1type
== VR_RANGE
)
5793 if (TREE_CODE (vr1max
) == INTEGER_CST
)
5794 *vr0min
= int_const_binop (PLUS_EXPR
, vr1max
,
5795 build_int_cst (TREE_TYPE (vr1max
), 1));
5799 else if (*vr0type
== VR_RANGE
5800 && vr1type
== VR_ANTI_RANGE
)
5802 if (TREE_CODE (*vr0min
) == INTEGER_CST
)
5806 *vr0max
= int_const_binop (MINUS_EXPR
, *vr0min
,
5807 build_int_cst (TREE_TYPE (*vr0min
), 1));
5821 *vr0type
= VR_VARYING
;
5822 *vr0min
= NULL_TREE
;
5823 *vr0max
= NULL_TREE
;
5826 /* Intersect the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
5827 { VR1TYPE, VR0MIN, VR0MAX } and store the result
5828 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
5829 possible such range. The resulting range is not canonicalized. */
5832 intersect_ranges (enum value_range_type
*vr0type
,
5833 tree
*vr0min
, tree
*vr0max
,
5834 enum value_range_type vr1type
,
5835 tree vr1min
, tree vr1max
)
5837 bool mineq
= vrp_operand_equal_p (*vr0min
, vr1min
);
5838 bool maxeq
= vrp_operand_equal_p (*vr0max
, vr1max
);
5840 /* [] is vr0, () is vr1 in the following classification comments. */
5844 if (*vr0type
== vr1type
)
5845 /* Nothing to do for equal ranges. */
5847 else if ((*vr0type
== VR_RANGE
5848 && vr1type
== VR_ANTI_RANGE
)
5849 || (*vr0type
== VR_ANTI_RANGE
5850 && vr1type
== VR_RANGE
))
5852 /* For anti-range with range intersection the result is empty. */
5853 *vr0type
= VR_UNDEFINED
;
5854 *vr0min
= NULL_TREE
;
5855 *vr0max
= NULL_TREE
;
5860 else if (operand_less_p (*vr0max
, vr1min
) == 1
5861 || operand_less_p (vr1max
, *vr0min
) == 1)
5863 /* [ ] ( ) or ( ) [ ]
5864 If the ranges have an empty intersection, the result of the
5865 intersect operation is the range for intersecting an
5866 anti-range with a range or empty when intersecting two ranges. */
5867 if (*vr0type
== VR_RANGE
5868 && vr1type
== VR_ANTI_RANGE
)
5870 else if (*vr0type
== VR_ANTI_RANGE
5871 && vr1type
== VR_RANGE
)
5877 else if (*vr0type
== VR_RANGE
5878 && vr1type
== VR_RANGE
)
5880 *vr0type
= VR_UNDEFINED
;
5881 *vr0min
= NULL_TREE
;
5882 *vr0max
= NULL_TREE
;
5884 else if (*vr0type
== VR_ANTI_RANGE
5885 && vr1type
== VR_ANTI_RANGE
)
5887 /* If the anti-ranges are adjacent to each other merge them. */
5888 if (TREE_CODE (*vr0max
) == INTEGER_CST
5889 && TREE_CODE (vr1min
) == INTEGER_CST
5890 && operand_less_p (*vr0max
, vr1min
) == 1
5891 && integer_onep (int_const_binop (MINUS_EXPR
,
5894 else if (TREE_CODE (vr1max
) == INTEGER_CST
5895 && TREE_CODE (*vr0min
) == INTEGER_CST
5896 && operand_less_p (vr1max
, *vr0min
) == 1
5897 && integer_onep (int_const_binop (MINUS_EXPR
,
5900 /* Else arbitrarily take VR0. */
5903 else if ((maxeq
|| operand_less_p (vr1max
, *vr0max
) == 1)
5904 && (mineq
|| operand_less_p (*vr0min
, vr1min
) == 1))
5906 /* [ ( ) ] or [( ) ] or [ ( )] */
5907 if (*vr0type
== VR_RANGE
5908 && vr1type
== VR_RANGE
)
5910 /* If both are ranges the result is the inner one. */
5915 else if (*vr0type
== VR_RANGE
5916 && vr1type
== VR_ANTI_RANGE
)
5918 /* Choose the right gap if the left one is empty. */
5921 if (TREE_CODE (vr1max
) != INTEGER_CST
)
5923 else if (TYPE_PRECISION (TREE_TYPE (vr1max
)) == 1
5924 && !TYPE_UNSIGNED (TREE_TYPE (vr1max
)))
5926 = int_const_binop (MINUS_EXPR
, vr1max
,
5927 build_int_cst (TREE_TYPE (vr1max
), -1));
5930 = int_const_binop (PLUS_EXPR
, vr1max
,
5931 build_int_cst (TREE_TYPE (vr1max
), 1));
5933 /* Choose the left gap if the right one is empty. */
5936 if (TREE_CODE (vr1min
) != INTEGER_CST
)
5938 else if (TYPE_PRECISION (TREE_TYPE (vr1min
)) == 1
5939 && !TYPE_UNSIGNED (TREE_TYPE (vr1min
)))
5941 = int_const_binop (PLUS_EXPR
, vr1min
,
5942 build_int_cst (TREE_TYPE (vr1min
), -1));
5945 = int_const_binop (MINUS_EXPR
, vr1min
,
5946 build_int_cst (TREE_TYPE (vr1min
), 1));
5948 /* Choose the anti-range if the range is effectively varying. */
5949 else if (vrp_val_is_min (*vr0min
)
5950 && vrp_val_is_max (*vr0max
))
5956 /* Else choose the range. */
5958 else if (*vr0type
== VR_ANTI_RANGE
5959 && vr1type
== VR_ANTI_RANGE
)
5960 /* If both are anti-ranges the result is the outer one. */
5962 else if (*vr0type
== VR_ANTI_RANGE
5963 && vr1type
== VR_RANGE
)
5965 /* The intersection is empty. */
5966 *vr0type
= VR_UNDEFINED
;
5967 *vr0min
= NULL_TREE
;
5968 *vr0max
= NULL_TREE
;
5973 else if ((maxeq
|| operand_less_p (*vr0max
, vr1max
) == 1)
5974 && (mineq
|| operand_less_p (vr1min
, *vr0min
) == 1))
5976 /* ( [ ] ) or ([ ] ) or ( [ ]) */
5977 if (*vr0type
== VR_RANGE
5978 && vr1type
== VR_RANGE
)
5979 /* Choose the inner range. */
5981 else if (*vr0type
== VR_ANTI_RANGE
5982 && vr1type
== VR_RANGE
)
5984 /* Choose the right gap if the left is empty. */
5987 *vr0type
= VR_RANGE
;
5988 if (TREE_CODE (*vr0max
) != INTEGER_CST
)
5990 else if (TYPE_PRECISION (TREE_TYPE (*vr0max
)) == 1
5991 && !TYPE_UNSIGNED (TREE_TYPE (*vr0max
)))
5993 = int_const_binop (MINUS_EXPR
, *vr0max
,
5994 build_int_cst (TREE_TYPE (*vr0max
), -1));
5997 = int_const_binop (PLUS_EXPR
, *vr0max
,
5998 build_int_cst (TREE_TYPE (*vr0max
), 1));
6001 /* Choose the left gap if the right is empty. */
6004 *vr0type
= VR_RANGE
;
6005 if (TREE_CODE (*vr0min
) != INTEGER_CST
)
6007 else if (TYPE_PRECISION (TREE_TYPE (*vr0min
)) == 1
6008 && !TYPE_UNSIGNED (TREE_TYPE (*vr0min
)))
6010 = int_const_binop (PLUS_EXPR
, *vr0min
,
6011 build_int_cst (TREE_TYPE (*vr0min
), -1));
6014 = int_const_binop (MINUS_EXPR
, *vr0min
,
6015 build_int_cst (TREE_TYPE (*vr0min
), 1));
6018 /* Choose the anti-range if the range is effectively varying. */
6019 else if (vrp_val_is_min (vr1min
)
6020 && vrp_val_is_max (vr1max
))
6022 /* Choose the anti-range if it is ~[0,0], that range is special
6023 enough to special case when vr1's range is relatively wide.
6024 At least for types bigger than int - this covers pointers
6025 and arguments to functions like ctz. */
6026 else if (*vr0min
== *vr0max
6027 && integer_zerop (*vr0min
)
6028 && ((TYPE_PRECISION (TREE_TYPE (*vr0min
))
6029 >= TYPE_PRECISION (integer_type_node
))
6030 || POINTER_TYPE_P (TREE_TYPE (*vr0min
)))
6031 && TREE_CODE (vr1max
) == INTEGER_CST
6032 && TREE_CODE (vr1min
) == INTEGER_CST
6033 && (wi::clz (wi::to_wide (vr1max
) - wi::to_wide (vr1min
))
6034 < TYPE_PRECISION (TREE_TYPE (*vr0min
)) / 2))
6036 /* Else choose the range. */
6044 else if (*vr0type
== VR_ANTI_RANGE
6045 && vr1type
== VR_ANTI_RANGE
)
6047 /* If both are anti-ranges the result is the outer one. */
6052 else if (vr1type
== VR_ANTI_RANGE
6053 && *vr0type
== VR_RANGE
)
6055 /* The intersection is empty. */
6056 *vr0type
= VR_UNDEFINED
;
6057 *vr0min
= NULL_TREE
;
6058 *vr0max
= NULL_TREE
;
6063 else if ((operand_less_p (vr1min
, *vr0max
) == 1
6064 || operand_equal_p (vr1min
, *vr0max
, 0))
6065 && operand_less_p (*vr0min
, vr1min
) == 1)
6067 /* [ ( ] ) or [ ]( ) */
6068 if (*vr0type
== VR_ANTI_RANGE
6069 && vr1type
== VR_ANTI_RANGE
)
6071 else if (*vr0type
== VR_RANGE
6072 && vr1type
== VR_RANGE
)
6074 else if (*vr0type
== VR_RANGE
6075 && vr1type
== VR_ANTI_RANGE
)
6077 if (TREE_CODE (vr1min
) == INTEGER_CST
)
6078 *vr0max
= int_const_binop (MINUS_EXPR
, vr1min
,
6079 build_int_cst (TREE_TYPE (vr1min
), 1));
6083 else if (*vr0type
== VR_ANTI_RANGE
6084 && vr1type
== VR_RANGE
)
6086 *vr0type
= VR_RANGE
;
6087 if (TREE_CODE (*vr0max
) == INTEGER_CST
)
6088 *vr0min
= int_const_binop (PLUS_EXPR
, *vr0max
,
6089 build_int_cst (TREE_TYPE (*vr0max
), 1));
6097 else if ((operand_less_p (*vr0min
, vr1max
) == 1
6098 || operand_equal_p (*vr0min
, vr1max
, 0))
6099 && operand_less_p (vr1min
, *vr0min
) == 1)
6101 /* ( [ ) ] or ( )[ ] */
6102 if (*vr0type
== VR_ANTI_RANGE
6103 && vr1type
== VR_ANTI_RANGE
)
6105 else if (*vr0type
== VR_RANGE
6106 && vr1type
== VR_RANGE
)
6108 else if (*vr0type
== VR_RANGE
6109 && vr1type
== VR_ANTI_RANGE
)
6111 if (TREE_CODE (vr1max
) == INTEGER_CST
)
6112 *vr0min
= int_const_binop (PLUS_EXPR
, vr1max
,
6113 build_int_cst (TREE_TYPE (vr1max
), 1));
6117 else if (*vr0type
== VR_ANTI_RANGE
6118 && vr1type
== VR_RANGE
)
6120 *vr0type
= VR_RANGE
;
6121 if (TREE_CODE (*vr0min
) == INTEGER_CST
)
6122 *vr0max
= int_const_binop (MINUS_EXPR
, *vr0min
,
6123 build_int_cst (TREE_TYPE (*vr0min
), 1));
6132 /* As a fallback simply use { *VRTYPE, *VR0MIN, *VR0MAX } as
6133 result for the intersection. That's always a conservative
6134 correct estimate unless VR1 is a constant singleton range
6135 in which case we choose that. */
6136 if (vr1type
== VR_RANGE
6137 && is_gimple_min_invariant (vr1min
)
6138 && vrp_operand_equal_p (vr1min
, vr1max
))
6149 /* Intersect the two value-ranges *VR0 and *VR1 and store the result
6150 in *VR0. This may not be the smallest possible such range. */
6153 vrp_intersect_ranges_1 (value_range
*vr0
, value_range
*vr1
)
6157 /* If either range is VR_VARYING the other one wins. */
6158 if (vr1
->type
== VR_VARYING
)
6160 if (vr0
->type
== VR_VARYING
)
6162 copy_value_range (vr0
, vr1
);
6166 /* When either range is VR_UNDEFINED the resulting range is
6167 VR_UNDEFINED, too. */
6168 if (vr0
->type
== VR_UNDEFINED
)
6170 if (vr1
->type
== VR_UNDEFINED
)
6172 set_value_range_to_undefined (vr0
);
6176 /* Save the original vr0 so we can return it as conservative intersection
6177 result when our worker turns things to varying. */
6179 intersect_ranges (&vr0
->type
, &vr0
->min
, &vr0
->max
,
6180 vr1
->type
, vr1
->min
, vr1
->max
);
6181 /* Make sure to canonicalize the result though as the inversion of a
6182 VR_RANGE can still be a VR_RANGE. */
6183 set_and_canonicalize_value_range (vr0
, vr0
->type
,
6184 vr0
->min
, vr0
->max
, vr0
->equiv
);
6185 /* If that failed, use the saved original VR0. */
6186 if (vr0
->type
== VR_VARYING
)
6191 /* If the result is VR_UNDEFINED there is no need to mess with
6192 the equivalencies. */
6193 if (vr0
->type
== VR_UNDEFINED
)
6196 /* The resulting set of equivalences for range intersection is the union of
6198 if (vr0
->equiv
&& vr1
->equiv
&& vr0
->equiv
!= vr1
->equiv
)
6199 bitmap_ior_into (vr0
->equiv
, vr1
->equiv
);
6200 else if (vr1
->equiv
&& !vr0
->equiv
)
6202 /* All equivalence bitmaps are allocated from the same obstack. So
6203 we can use the obstack associated with VR to allocate vr0->equiv. */
6204 vr0
->equiv
= BITMAP_ALLOC (vr1
->equiv
->obstack
);
6205 bitmap_copy (vr0
->equiv
, vr1
->equiv
);
6210 vrp_intersect_ranges (value_range
*vr0
, value_range
*vr1
)
6212 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6214 fprintf (dump_file
, "Intersecting\n ");
6215 dump_value_range (dump_file
, vr0
);
6216 fprintf (dump_file
, "\nand\n ");
6217 dump_value_range (dump_file
, vr1
);
6218 fprintf (dump_file
, "\n");
6220 vrp_intersect_ranges_1 (vr0
, vr1
);
6221 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6223 fprintf (dump_file
, "to\n ");
6224 dump_value_range (dump_file
, vr0
);
6225 fprintf (dump_file
, "\n");
6229 /* Meet operation for value ranges. Given two value ranges VR0 and
6230 VR1, store in VR0 a range that contains both VR0 and VR1. This
6231 may not be the smallest possible such range. */
6234 vrp_meet_1 (value_range
*vr0
, const value_range
*vr1
)
6238 if (vr0
->type
== VR_UNDEFINED
)
6240 set_value_range (vr0
, vr1
->type
, vr1
->min
, vr1
->max
, vr1
->equiv
);
6244 if (vr1
->type
== VR_UNDEFINED
)
6246 /* VR0 already has the resulting range. */
6250 if (vr0
->type
== VR_VARYING
)
6252 /* Nothing to do. VR0 already has the resulting range. */
6256 if (vr1
->type
== VR_VARYING
)
6258 set_value_range_to_varying (vr0
);
6263 union_ranges (&vr0
->type
, &vr0
->min
, &vr0
->max
,
6264 vr1
->type
, vr1
->min
, vr1
->max
);
6265 if (vr0
->type
== VR_VARYING
)
6267 /* Failed to find an efficient meet. Before giving up and setting
6268 the result to VARYING, see if we can at least derive a useful
6269 anti-range. FIXME, all this nonsense about distinguishing
6270 anti-ranges from ranges is necessary because of the odd
6271 semantics of range_includes_zero_p and friends. */
6272 if (((saved
.type
== VR_RANGE
6273 && range_includes_zero_p (saved
.min
, saved
.max
) == 0)
6274 || (saved
.type
== VR_ANTI_RANGE
6275 && range_includes_zero_p (saved
.min
, saved
.max
) == 1))
6276 && ((vr1
->type
== VR_RANGE
6277 && range_includes_zero_p (vr1
->min
, vr1
->max
) == 0)
6278 || (vr1
->type
== VR_ANTI_RANGE
6279 && range_includes_zero_p (vr1
->min
, vr1
->max
) == 1)))
6281 set_value_range_to_nonnull (vr0
, TREE_TYPE (saved
.min
));
6283 /* Since this meet operation did not result from the meeting of
6284 two equivalent names, VR0 cannot have any equivalences. */
6286 bitmap_clear (vr0
->equiv
);
6290 set_value_range_to_varying (vr0
);
6293 set_and_canonicalize_value_range (vr0
, vr0
->type
, vr0
->min
, vr0
->max
,
6295 if (vr0
->type
== VR_VARYING
)
6298 /* The resulting set of equivalences is always the intersection of
6300 if (vr0
->equiv
&& vr1
->equiv
&& vr0
->equiv
!= vr1
->equiv
)
6301 bitmap_and_into (vr0
->equiv
, vr1
->equiv
);
6302 else if (vr0
->equiv
&& !vr1
->equiv
)
6303 bitmap_clear (vr0
->equiv
);
6307 vrp_meet (value_range
*vr0
, const value_range
*vr1
)
6309 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6311 fprintf (dump_file
, "Meeting\n ");
6312 dump_value_range (dump_file
, vr0
);
6313 fprintf (dump_file
, "\nand\n ");
6314 dump_value_range (dump_file
, vr1
);
6315 fprintf (dump_file
, "\n");
6317 vrp_meet_1 (vr0
, vr1
);
6318 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6320 fprintf (dump_file
, "to\n ");
6321 dump_value_range (dump_file
, vr0
);
6322 fprintf (dump_file
, "\n");
6327 /* Visit all arguments for PHI node PHI that flow through executable
6328 edges. If a valid value range can be derived from all the incoming
6329 value ranges, set a new range for the LHS of PHI. */
6331 enum ssa_prop_result
6332 vrp_prop::visit_phi (gphi
*phi
)
6334 tree lhs
= PHI_RESULT (phi
);
6335 value_range vr_result
= VR_INITIALIZER
;
6336 extract_range_from_phi_node (phi
, &vr_result
);
6337 if (update_value_range (lhs
, &vr_result
))
6339 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6341 fprintf (dump_file
, "Found new range for ");
6342 print_generic_expr (dump_file
, lhs
);
6343 fprintf (dump_file
, ": ");
6344 dump_value_range (dump_file
, &vr_result
);
6345 fprintf (dump_file
, "\n");
6348 if (vr_result
.type
== VR_VARYING
)
6349 return SSA_PROP_VARYING
;
6351 return SSA_PROP_INTERESTING
;
6354 /* Nothing changed, don't add outgoing edges. */
6355 return SSA_PROP_NOT_INTERESTING
;
6358 class vrp_folder
: public substitute_and_fold_engine
6361 tree
get_value (tree
) FINAL OVERRIDE
;
6362 bool fold_stmt (gimple_stmt_iterator
*) FINAL OVERRIDE
;
6363 bool fold_predicate_in (gimple_stmt_iterator
*);
6365 class vr_values
*vr_values
;
6368 tree
vrp_evaluate_conditional (tree_code code
, tree op0
,
6369 tree op1
, gimple
*stmt
)
6370 { return vr_values
->vrp_evaluate_conditional (code
, op0
, op1
, stmt
); }
6371 bool simplify_stmt_using_ranges (gimple_stmt_iterator
*gsi
)
6372 { return vr_values
->simplify_stmt_using_ranges (gsi
); }
6373 tree
op_with_constant_singleton_value_range (tree op
)
6374 { return vr_values
->op_with_constant_singleton_value_range (op
); }
6377 /* If the statement pointed by SI has a predicate whose value can be
6378 computed using the value range information computed by VRP, compute
6379 its value and return true. Otherwise, return false. */
6382 vrp_folder::fold_predicate_in (gimple_stmt_iterator
*si
)
6384 bool assignment_p
= false;
6386 gimple
*stmt
= gsi_stmt (*si
);
6388 if (is_gimple_assign (stmt
)
6389 && TREE_CODE_CLASS (gimple_assign_rhs_code (stmt
)) == tcc_comparison
)
6391 assignment_p
= true;
6392 val
= vrp_evaluate_conditional (gimple_assign_rhs_code (stmt
),
6393 gimple_assign_rhs1 (stmt
),
6394 gimple_assign_rhs2 (stmt
),
6397 else if (gcond
*cond_stmt
= dyn_cast
<gcond
*> (stmt
))
6398 val
= vrp_evaluate_conditional (gimple_cond_code (cond_stmt
),
6399 gimple_cond_lhs (cond_stmt
),
6400 gimple_cond_rhs (cond_stmt
),
6408 val
= fold_convert (gimple_expr_type (stmt
), val
);
6412 fprintf (dump_file
, "Folding predicate ");
6413 print_gimple_expr (dump_file
, stmt
, 0);
6414 fprintf (dump_file
, " to ");
6415 print_generic_expr (dump_file
, val
);
6416 fprintf (dump_file
, "\n");
6419 if (is_gimple_assign (stmt
))
6420 gimple_assign_set_rhs_from_tree (si
, val
);
6423 gcc_assert (gimple_code (stmt
) == GIMPLE_COND
);
6424 gcond
*cond_stmt
= as_a
<gcond
*> (stmt
);
6425 if (integer_zerop (val
))
6426 gimple_cond_make_false (cond_stmt
);
6427 else if (integer_onep (val
))
6428 gimple_cond_make_true (cond_stmt
);
6439 /* Callback for substitute_and_fold folding the stmt at *SI. */
6442 vrp_folder::fold_stmt (gimple_stmt_iterator
*si
)
6444 if (fold_predicate_in (si
))
6447 return simplify_stmt_using_ranges (si
);
6450 /* If OP has a value range with a single constant value return that,
6451 otherwise return NULL_TREE. This returns OP itself if OP is a
6454 Implemented as a pure wrapper right now, but this will change. */
6457 vrp_folder::get_value (tree op
)
6459 return op_with_constant_singleton_value_range (op
);
6462 /* Return the LHS of any ASSERT_EXPR where OP appears as the first
6463 argument to the ASSERT_EXPR and in which the ASSERT_EXPR dominates
6464 BB. If no such ASSERT_EXPR is found, return OP. */
6467 lhs_of_dominating_assert (tree op
, basic_block bb
, gimple
*stmt
)
6469 imm_use_iterator imm_iter
;
6471 use_operand_p use_p
;
6473 if (TREE_CODE (op
) == SSA_NAME
)
6475 FOR_EACH_IMM_USE_FAST (use_p
, imm_iter
, op
)
6477 use_stmt
= USE_STMT (use_p
);
6478 if (use_stmt
!= stmt
6479 && gimple_assign_single_p (use_stmt
)
6480 && TREE_CODE (gimple_assign_rhs1 (use_stmt
)) == ASSERT_EXPR
6481 && TREE_OPERAND (gimple_assign_rhs1 (use_stmt
), 0) == op
6482 && dominated_by_p (CDI_DOMINATORS
, bb
, gimple_bb (use_stmt
)))
6483 return gimple_assign_lhs (use_stmt
);
6490 static class vr_values
*x_vr_values
;
6492 /* A trivial wrapper so that we can present the generic jump threading
6493 code with a simple API for simplifying statements. STMT is the
6494 statement we want to simplify, WITHIN_STMT provides the location
6495 for any overflow warnings. */
6498 simplify_stmt_for_jump_threading (gimple
*stmt
, gimple
*within_stmt
,
6499 class avail_exprs_stack
*avail_exprs_stack ATTRIBUTE_UNUSED
,
6502 /* First see if the conditional is in the hash table. */
6503 tree cached_lhs
= avail_exprs_stack
->lookup_avail_expr (stmt
, false, true);
6504 if (cached_lhs
&& is_gimple_min_invariant (cached_lhs
))
6507 vr_values
*vr_values
= x_vr_values
;
6508 if (gcond
*cond_stmt
= dyn_cast
<gcond
*> (stmt
))
6510 tree op0
= gimple_cond_lhs (cond_stmt
);
6511 op0
= lhs_of_dominating_assert (op0
, bb
, stmt
);
6513 tree op1
= gimple_cond_rhs (cond_stmt
);
6514 op1
= lhs_of_dominating_assert (op1
, bb
, stmt
);
6516 return vr_values
->vrp_evaluate_conditional (gimple_cond_code (cond_stmt
),
6517 op0
, op1
, within_stmt
);
6520 /* We simplify a switch statement by trying to determine which case label
6521 will be taken. If we are successful then we return the corresponding
6523 if (gswitch
*switch_stmt
= dyn_cast
<gswitch
*> (stmt
))
6525 tree op
= gimple_switch_index (switch_stmt
);
6526 if (TREE_CODE (op
) != SSA_NAME
)
6529 op
= lhs_of_dominating_assert (op
, bb
, stmt
);
6531 value_range
*vr
= vr_values
->get_value_range (op
);
6532 if ((vr
->type
!= VR_RANGE
&& vr
->type
!= VR_ANTI_RANGE
)
6533 || symbolic_range_p (vr
))
6536 if (vr
->type
== VR_RANGE
)
6539 /* Get the range of labels that contain a part of the operand's
6541 find_case_label_range (switch_stmt
, vr
->min
, vr
->max
, &i
, &j
);
6543 /* Is there only one such label? */
6546 tree label
= gimple_switch_label (switch_stmt
, i
);
6548 /* The i'th label will be taken only if the value range of the
6549 operand is entirely within the bounds of this label. */
6550 if (CASE_HIGH (label
) != NULL_TREE
6551 ? (tree_int_cst_compare (CASE_LOW (label
), vr
->min
) <= 0
6552 && tree_int_cst_compare (CASE_HIGH (label
), vr
->max
) >= 0)
6553 : (tree_int_cst_equal (CASE_LOW (label
), vr
->min
)
6554 && tree_int_cst_equal (vr
->min
, vr
->max
)))
6558 /* If there are no such labels then the default label will be
6561 return gimple_switch_label (switch_stmt
, 0);
6564 if (vr
->type
== VR_ANTI_RANGE
)
6566 unsigned n
= gimple_switch_num_labels (switch_stmt
);
6567 tree min_label
= gimple_switch_label (switch_stmt
, 1);
6568 tree max_label
= gimple_switch_label (switch_stmt
, n
- 1);
6570 /* The default label will be taken only if the anti-range of the
6571 operand is entirely outside the bounds of all the (non-default)
6573 if (tree_int_cst_compare (vr
->min
, CASE_LOW (min_label
)) <= 0
6574 && (CASE_HIGH (max_label
) != NULL_TREE
6575 ? tree_int_cst_compare (vr
->max
, CASE_HIGH (max_label
)) >= 0
6576 : tree_int_cst_compare (vr
->max
, CASE_LOW (max_label
)) >= 0))
6577 return gimple_switch_label (switch_stmt
, 0);
6583 if (gassign
*assign_stmt
= dyn_cast
<gassign
*> (stmt
))
6585 tree lhs
= gimple_assign_lhs (assign_stmt
);
6586 if (TREE_CODE (lhs
) == SSA_NAME
6587 && (INTEGRAL_TYPE_P (TREE_TYPE (lhs
))
6588 || POINTER_TYPE_P (TREE_TYPE (lhs
)))
6589 && stmt_interesting_for_vrp (stmt
))
6593 value_range new_vr
= VR_INITIALIZER
;
6594 vr_values
->extract_range_from_stmt (stmt
, &dummy_e
,
6595 &dummy_tree
, &new_vr
);
6596 if (range_int_cst_singleton_p (&new_vr
))
6604 class vrp_dom_walker
: public dom_walker
6607 vrp_dom_walker (cdi_direction direction
,
6608 class const_and_copies
*const_and_copies
,
6609 class avail_exprs_stack
*avail_exprs_stack
)
6610 : dom_walker (direction
, true),
6611 m_const_and_copies (const_and_copies
),
6612 m_avail_exprs_stack (avail_exprs_stack
),
6613 m_dummy_cond (NULL
) {}
6615 virtual edge
before_dom_children (basic_block
);
6616 virtual void after_dom_children (basic_block
);
6618 class vr_values
*vr_values
;
6621 class const_and_copies
*m_const_and_copies
;
6622 class avail_exprs_stack
*m_avail_exprs_stack
;
6624 gcond
*m_dummy_cond
;
6628 /* Called before processing dominator children of BB. We want to look
6629 at ASSERT_EXPRs and record information from them in the appropriate
6632 We could look at other statements here. It's not seen as likely
6633 to significantly increase the jump threads we discover. */
6636 vrp_dom_walker::before_dom_children (basic_block bb
)
6638 gimple_stmt_iterator gsi
;
6640 m_avail_exprs_stack
->push_marker ();
6641 m_const_and_copies
->push_marker ();
6642 for (gsi
= gsi_start_nondebug_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
6644 gimple
*stmt
= gsi_stmt (gsi
);
6645 if (gimple_assign_single_p (stmt
)
6646 && TREE_CODE (gimple_assign_rhs1 (stmt
)) == ASSERT_EXPR
)
6648 tree rhs1
= gimple_assign_rhs1 (stmt
);
6649 tree cond
= TREE_OPERAND (rhs1
, 1);
6650 tree inverted
= invert_truthvalue (cond
);
6651 vec
<cond_equivalence
> p
;
6653 record_conditions (&p
, cond
, inverted
);
6654 for (unsigned int i
= 0; i
< p
.length (); i
++)
6655 m_avail_exprs_stack
->record_cond (&p
[i
]);
6657 tree lhs
= gimple_assign_lhs (stmt
);
6658 m_const_and_copies
->record_const_or_copy (lhs
,
6659 TREE_OPERAND (rhs1
, 0));
6668 /* Called after processing dominator children of BB. This is where we
6669 actually call into the threader. */
6671 vrp_dom_walker::after_dom_children (basic_block bb
)
6674 m_dummy_cond
= gimple_build_cond (NE_EXPR
,
6675 integer_zero_node
, integer_zero_node
,
6678 x_vr_values
= vr_values
;
6679 thread_outgoing_edges (bb
, m_dummy_cond
, m_const_and_copies
,
6680 m_avail_exprs_stack
,
6681 simplify_stmt_for_jump_threading
);
6684 m_avail_exprs_stack
->pop_to_marker ();
6685 m_const_and_copies
->pop_to_marker ();
6688 /* Blocks which have more than one predecessor and more than
6689 one successor present jump threading opportunities, i.e.,
6690 when the block is reached from a specific predecessor, we
6691 may be able to determine which of the outgoing edges will
6692 be traversed. When this optimization applies, we are able
6693 to avoid conditionals at runtime and we may expose secondary
6694 optimization opportunities.
6696 This routine is effectively a driver for the generic jump
6697 threading code. It basically just presents the generic code
6698 with edges that may be suitable for jump threading.
6700 Unlike DOM, we do not iterate VRP if jump threading was successful.
6701 While iterating may expose new opportunities for VRP, it is expected
6702 those opportunities would be very limited and the compile time cost
6703 to expose those opportunities would be significant.
6705 As jump threading opportunities are discovered, they are registered
6706 for later realization. */
6709 identify_jump_threads (class vr_values
*vr_values
)
6714 /* Ugh. When substituting values earlier in this pass we can
6715 wipe the dominance information. So rebuild the dominator
6716 information as we need it within the jump threading code. */
6717 calculate_dominance_info (CDI_DOMINATORS
);
6719 /* We do not allow VRP information to be used for jump threading
6720 across a back edge in the CFG. Otherwise it becomes too
6721 difficult to avoid eliminating loop exit tests. Of course
6722 EDGE_DFS_BACK is not accurate at this time so we have to
6724 mark_dfs_back_edges ();
6726 /* Do not thread across edges we are about to remove. Just marking
6727 them as EDGE_IGNORE will do. */
6728 FOR_EACH_VEC_ELT (to_remove_edges
, i
, e
)
6729 e
->flags
|= EDGE_IGNORE
;
6731 /* Allocate our unwinder stack to unwind any temporary equivalences
6732 that might be recorded. */
6733 const_and_copies
*equiv_stack
= new const_and_copies ();
6735 hash_table
<expr_elt_hasher
> *avail_exprs
6736 = new hash_table
<expr_elt_hasher
> (1024);
6737 avail_exprs_stack
*avail_exprs_stack
6738 = new class avail_exprs_stack (avail_exprs
);
6740 vrp_dom_walker
walker (CDI_DOMINATORS
, equiv_stack
, avail_exprs_stack
);
6741 walker
.vr_values
= vr_values
;
6742 walker
.walk (cfun
->cfg
->x_entry_block_ptr
);
6744 /* Clear EDGE_IGNORE. */
6745 FOR_EACH_VEC_ELT (to_remove_edges
, i
, e
)
6746 e
->flags
&= ~EDGE_IGNORE
;
6748 /* We do not actually update the CFG or SSA graphs at this point as
6749 ASSERT_EXPRs are still in the IL and cfg cleanup code does not yet
6750 handle ASSERT_EXPRs gracefully. */
6753 delete avail_exprs_stack
;
6756 /* Traverse all the blocks folding conditionals with known ranges. */
6759 vrp_prop::vrp_finalize (bool warn_array_bounds_p
)
6763 /* We have completed propagating through the lattice. */
6764 vr_values
.set_lattice_propagation_complete ();
6768 fprintf (dump_file
, "\nValue ranges after VRP:\n\n");
6769 vr_values
.dump_all_value_ranges (dump_file
);
6770 fprintf (dump_file
, "\n");
6773 /* Set value range to non pointer SSA_NAMEs. */
6774 for (i
= 0; i
< num_ssa_names
; i
++)
6776 tree name
= ssa_name (i
);
6780 value_range
*vr
= get_value_range (name
);
6782 || (vr
->type
== VR_VARYING
)
6783 || (vr
->type
== VR_UNDEFINED
)
6784 || (TREE_CODE (vr
->min
) != INTEGER_CST
)
6785 || (TREE_CODE (vr
->max
) != INTEGER_CST
))
6788 if (POINTER_TYPE_P (TREE_TYPE (name
))
6789 && ((vr
->type
== VR_RANGE
6790 && range_includes_zero_p (vr
->min
, vr
->max
) == 0)
6791 || (vr
->type
== VR_ANTI_RANGE
6792 && range_includes_zero_p (vr
->min
, vr
->max
) == 1)))
6793 set_ptr_nonnull (name
);
6794 else if (!POINTER_TYPE_P (TREE_TYPE (name
)))
6795 set_range_info (name
, vr
->type
,
6796 wi::to_wide (vr
->min
),
6797 wi::to_wide (vr
->max
));
6800 class vrp_folder vrp_folder
;
6801 vrp_folder
.vr_values
= &vr_values
;
6802 vrp_folder
.substitute_and_fold ();
6804 if (warn_array_bounds
&& warn_array_bounds_p
)
6805 check_all_array_refs ();
6808 /* Main entry point to VRP (Value Range Propagation). This pass is
6809 loosely based on J. R. C. Patterson, ``Accurate Static Branch
6810 Prediction by Value Range Propagation,'' in SIGPLAN Conference on
6811 Programming Language Design and Implementation, pp. 67-78, 1995.
6812 Also available at http://citeseer.ist.psu.edu/patterson95accurate.html
6814 This is essentially an SSA-CCP pass modified to deal with ranges
6815 instead of constants.
6817 While propagating ranges, we may find that two or more SSA name
6818 have equivalent, though distinct ranges. For instance,
6821 2 p_4 = ASSERT_EXPR <p_3, p_3 != 0>
6823 4 p_5 = ASSERT_EXPR <p_4, p_4 == q_2>;
6827 In the code above, pointer p_5 has range [q_2, q_2], but from the
6828 code we can also determine that p_5 cannot be NULL and, if q_2 had
6829 a non-varying range, p_5's range should also be compatible with it.
6831 These equivalences are created by two expressions: ASSERT_EXPR and
6832 copy operations. Since p_5 is an assertion on p_4, and p_4 was the
6833 result of another assertion, then we can use the fact that p_5 and
6834 p_4 are equivalent when evaluating p_5's range.
6836 Together with value ranges, we also propagate these equivalences
6837 between names so that we can take advantage of information from
6838 multiple ranges when doing final replacement. Note that this
6839 equivalency relation is transitive but not symmetric.
6841 In the example above, p_5 is equivalent to p_4, q_2 and p_3, but we
6842 cannot assert that q_2 is equivalent to p_5 because q_2 may be used
6843 in contexts where that assertion does not hold (e.g., in line 6).
6845 TODO, the main difference between this pass and Patterson's is that
6846 we do not propagate edge probabilities. We only compute whether
6847 edges can be taken or not. That is, instead of having a spectrum
6848 of jump probabilities between 0 and 1, we only deal with 0, 1 and
6849 DON'T KNOW. In the future, it may be worthwhile to propagate
6850 probabilities to aid branch prediction. */
6853 execute_vrp (bool warn_array_bounds_p
)
6859 loop_optimizer_init (LOOPS_NORMAL
| LOOPS_HAVE_RECORDED_EXITS
);
6860 rewrite_into_loop_closed_ssa (NULL
, TODO_update_ssa
);
6863 /* ??? This ends up using stale EDGE_DFS_BACK for liveness computation.
6864 Inserting assertions may split edges which will invalidate
6866 insert_range_assertions ();
6868 to_remove_edges
.create (10);
6869 to_update_switch_stmts
.create (5);
6870 threadedge_initialize_values ();
6872 /* For visiting PHI nodes we need EDGE_DFS_BACK computed. */
6873 mark_dfs_back_edges ();
6875 class vrp_prop vrp_prop
;
6876 vrp_prop
.vrp_initialize ();
6877 vrp_prop
.ssa_propagate ();
6878 vrp_prop
.vrp_finalize (warn_array_bounds_p
);
6880 /* We must identify jump threading opportunities before we release
6881 the datastructures built by VRP. */
6882 identify_jump_threads (&vrp_prop
.vr_values
);
6884 /* A comparison of an SSA_NAME against a constant where the SSA_NAME
6885 was set by a type conversion can often be rewritten to use the
6886 RHS of the type conversion.
6888 However, doing so inhibits jump threading through the comparison.
6889 So that transformation is not performed until after jump threading
6892 FOR_EACH_BB_FN (bb
, cfun
)
6894 gimple
*last
= last_stmt (bb
);
6895 if (last
&& gimple_code (last
) == GIMPLE_COND
)
6896 vrp_prop
.vr_values
.simplify_cond_using_ranges_2 (as_a
<gcond
*> (last
));
6899 free_numbers_of_iterations_estimates (cfun
);
6901 /* ASSERT_EXPRs must be removed before finalizing jump threads
6902 as finalizing jump threads calls the CFG cleanup code which
6903 does not properly handle ASSERT_EXPRs. */
6904 remove_range_assertions ();
6906 /* If we exposed any new variables, go ahead and put them into
6907 SSA form now, before we handle jump threading. This simplifies
6908 interactions between rewriting of _DECL nodes into SSA form
6909 and rewriting SSA_NAME nodes into SSA form after block
6910 duplication and CFG manipulation. */
6911 update_ssa (TODO_update_ssa
);
6913 /* We identified all the jump threading opportunities earlier, but could
6914 not transform the CFG at that time. This routine transforms the
6915 CFG and arranges for the dominator tree to be rebuilt if necessary.
6917 Note the SSA graph update will occur during the normal TODO
6918 processing by the pass manager. */
6919 thread_through_all_blocks (false);
6921 /* Remove dead edges from SWITCH_EXPR optimization. This leaves the
6922 CFG in a broken state and requires a cfg_cleanup run. */
6923 FOR_EACH_VEC_ELT (to_remove_edges
, i
, e
)
6925 /* Update SWITCH_EXPR case label vector. */
6926 FOR_EACH_VEC_ELT (to_update_switch_stmts
, i
, su
)
6929 size_t n
= TREE_VEC_LENGTH (su
->vec
);
6931 gimple_switch_set_num_labels (su
->stmt
, n
);
6932 for (j
= 0; j
< n
; j
++)
6933 gimple_switch_set_label (su
->stmt
, j
, TREE_VEC_ELT (su
->vec
, j
));
6934 /* As we may have replaced the default label with a regular one
6935 make sure to make it a real default label again. This ensures
6936 optimal expansion. */
6937 label
= gimple_switch_label (su
->stmt
, 0);
6938 CASE_LOW (label
) = NULL_TREE
;
6939 CASE_HIGH (label
) = NULL_TREE
;
6942 if (to_remove_edges
.length () > 0)
6944 free_dominance_info (CDI_DOMINATORS
);
6945 loops_state_set (LOOPS_NEED_FIXUP
);
6948 to_remove_edges
.release ();
6949 to_update_switch_stmts
.release ();
6950 threadedge_finalize_values ();
6953 loop_optimizer_finalize ();
6959 const pass_data pass_data_vrp
=
6961 GIMPLE_PASS
, /* type */
6963 OPTGROUP_NONE
, /* optinfo_flags */
6964 TV_TREE_VRP
, /* tv_id */
6965 PROP_ssa
, /* properties_required */
6966 0, /* properties_provided */
6967 0, /* properties_destroyed */
6968 0, /* todo_flags_start */
6969 ( TODO_cleanup_cfg
| TODO_update_ssa
), /* todo_flags_finish */
6972 class pass_vrp
: public gimple_opt_pass
6975 pass_vrp (gcc::context
*ctxt
)
6976 : gimple_opt_pass (pass_data_vrp
, ctxt
), warn_array_bounds_p (false)
6979 /* opt_pass methods: */
6980 opt_pass
* clone () { return new pass_vrp (m_ctxt
); }
6981 void set_pass_param (unsigned int n
, bool param
)
6983 gcc_assert (n
== 0);
6984 warn_array_bounds_p
= param
;
6986 virtual bool gate (function
*) { return flag_tree_vrp
!= 0; }
6987 virtual unsigned int execute (function
*)
6988 { return execute_vrp (warn_array_bounds_p
); }
6991 bool warn_array_bounds_p
;
6992 }; // class pass_vrp
6997 make_pass_vrp (gcc::context
*ctxt
)
6999 return new pass_vrp (ctxt
);