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 if (TREE_CODE (val1
) == INTEGER_CST
735 && TREE_CODE (val2
) == INTEGER_CST
)
736 return tree_int_cst_compare (val1
, val2
);
738 if (poly_int_tree_p (val1
) && poly_int_tree_p (val2
))
740 if (known_eq (wi::to_poly_widest (val1
),
741 wi::to_poly_widest (val2
)))
743 if (known_lt (wi::to_poly_widest (val1
),
744 wi::to_poly_widest (val2
)))
746 if (known_gt (wi::to_poly_widest (val1
),
747 wi::to_poly_widest (val2
)))
757 /* First see if VAL1 and VAL2 are not the same. */
758 if (val1
== val2
|| operand_equal_p (val1
, val2
, 0))
761 /* If VAL1 is a lower address than VAL2, return -1. */
762 if (operand_less_p (val1
, val2
) == 1)
765 /* If VAL1 is a higher address than VAL2, return +1. */
766 if (operand_less_p (val2
, val1
) == 1)
769 /* If VAL1 is different than VAL2, return +2.
770 For integer constants we either have already returned -1 or 1
771 or they are equivalent. We still might succeed in proving
772 something about non-trivial operands. */
773 if (TREE_CODE (val1
) != INTEGER_CST
774 || TREE_CODE (val2
) != INTEGER_CST
)
776 t
= fold_binary_to_constant (NE_EXPR
, boolean_type_node
, val1
, val2
);
777 if (t
&& integer_onep (t
))
785 /* Compare values like compare_values_warnv. */
788 compare_values (tree val1
, tree val2
)
791 return compare_values_warnv (val1
, val2
, &sop
);
795 /* Return 1 if VAL is inside value range MIN <= VAL <= MAX,
796 0 if VAL is not inside [MIN, MAX],
797 -2 if we cannot tell either way.
799 Benchmark compile/20001226-1.c compilation time after changing this
803 value_inside_range (tree val
, tree min
, tree max
)
807 cmp1
= operand_less_p (val
, min
);
813 cmp2
= operand_less_p (max
, val
);
821 /* Return true if value ranges VR0 and VR1 have a non-empty
824 Benchmark compile/20001226-1.c compilation time after changing this
829 value_ranges_intersect_p (value_range
*vr0
, value_range
*vr1
)
831 /* The value ranges do not intersect if the maximum of the first range is
832 less than the minimum of the second range or vice versa.
833 When those relations are unknown, we can't do any better. */
834 if (operand_less_p (vr0
->max
, vr1
->min
) != 0)
836 if (operand_less_p (vr1
->max
, vr0
->min
) != 0)
842 /* Return 1 if [MIN, MAX] includes the value zero, 0 if it does not
843 include the value zero, -2 if we cannot tell. */
846 range_includes_zero_p (tree min
, tree max
)
848 tree zero
= build_int_cst (TREE_TYPE (min
), 0);
849 return value_inside_range (zero
, min
, max
);
852 /* Return true if *VR is know to only contain nonnegative values. */
855 value_range_nonnegative_p (value_range
*vr
)
857 /* Testing for VR_ANTI_RANGE is not useful here as any anti-range
858 which would return a useful value should be encoded as a
860 if (vr
->type
== VR_RANGE
)
862 int result
= compare_values (vr
->min
, integer_zero_node
);
863 return (result
== 0 || result
== 1);
869 /* If *VR has a value rante that is a single constant value return that,
870 otherwise return NULL_TREE. */
873 value_range_constant_singleton (value_range
*vr
)
875 if (vr
->type
== VR_RANGE
876 && vrp_operand_equal_p (vr
->min
, vr
->max
)
877 && is_gimple_min_invariant (vr
->min
))
883 /* Wrapper around int_const_binop. Return true if we can compute the
884 result; i.e. if the operation doesn't overflow or if the overflow is
885 undefined. In the latter case (if the operation overflows and
886 overflow is undefined), then adjust the result to be -INF or +INF
887 depending on CODE, VAL1 and VAL2. Return the value in *RES.
889 Return false for division by zero, for which the result is
893 vrp_int_const_binop (enum tree_code code
, tree val1
, tree val2
, wide_int
*res
)
895 bool overflow
= false;
896 signop sign
= TYPE_SIGN (TREE_TYPE (val1
));
903 wide_int wval2
= wi::to_wide (val2
, TYPE_PRECISION (TREE_TYPE (val1
)));
904 if (wi::neg_p (wval2
))
907 if (code
== RSHIFT_EXPR
)
913 if (code
== RSHIFT_EXPR
)
914 /* It's unclear from the C standard whether shifts can overflow.
915 The following code ignores overflow; perhaps a C standard
916 interpretation ruling is needed. */
917 *res
= wi::rshift (wi::to_wide (val1
), wval2
, sign
);
919 *res
= wi::lshift (wi::to_wide (val1
), wval2
);
924 *res
= wi::mul (wi::to_wide (val1
),
925 wi::to_wide (val2
), sign
, &overflow
);
933 *res
= wi::div_trunc (wi::to_wide (val1
),
934 wi::to_wide (val2
), sign
, &overflow
);
940 *res
= wi::div_floor (wi::to_wide (val1
),
941 wi::to_wide (val2
), sign
, &overflow
);
947 *res
= wi::div_ceil (wi::to_wide (val1
),
948 wi::to_wide (val2
), sign
, &overflow
);
954 *res
= wi::div_round (wi::to_wide (val1
),
955 wi::to_wide (val2
), sign
, &overflow
);
963 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (val1
)))
965 /* If the operation overflowed return -INF or +INF depending
966 on the operation and the combination of signs of the operands. */
967 int sgn1
= tree_int_cst_sgn (val1
);
968 int sgn2
= tree_int_cst_sgn (val2
);
970 /* Notice that we only need to handle the restricted set of
971 operations handled by extract_range_from_binary_expr.
972 Among them, only multiplication, addition and subtraction
973 can yield overflow without overflown operands because we
974 are working with integral types only... except in the
975 case VAL1 = -INF and VAL2 = -1 which overflows to +INF
978 /* For multiplication, the sign of the overflow is given
979 by the comparison of the signs of the operands. */
980 if ((code
== MULT_EXPR
&& sgn1
== sgn2
)
981 /* For addition, the operands must be of the same sign
982 to yield an overflow. Its sign is therefore that
983 of one of the operands, for example the first. */
984 || (code
== PLUS_EXPR
&& sgn1
>= 0)
985 /* For subtraction, operands must be of
986 different signs to yield an overflow. Its sign is
987 therefore that of the first operand or the opposite of
988 that of the second operand. A first operand of 0 counts
989 as positive here, for the corner case 0 - (-INF), which
990 overflows, but must yield +INF. */
991 || (code
== MINUS_EXPR
&& sgn1
>= 0)
992 /* For division, the only case is -INF / -1 = +INF. */
993 || code
== TRUNC_DIV_EXPR
994 || code
== FLOOR_DIV_EXPR
995 || code
== CEIL_DIV_EXPR
996 || code
== EXACT_DIV_EXPR
997 || code
== ROUND_DIV_EXPR
)
998 *res
= wi::max_value (TYPE_PRECISION (TREE_TYPE (val1
)),
999 TYPE_SIGN (TREE_TYPE (val1
)));
1001 *res
= wi::min_value (TYPE_PRECISION (TREE_TYPE (val1
)),
1002 TYPE_SIGN (TREE_TYPE (val1
)));
1010 /* For range VR compute two wide_int bitmasks. In *MAY_BE_NONZERO
1011 bitmask if some bit is unset, it means for all numbers in the range
1012 the bit is 0, otherwise it might be 0 or 1. In *MUST_BE_NONZERO
1013 bitmask if some bit is set, it means for all numbers in the range
1014 the bit is 1, otherwise it might be 0 or 1. */
1017 zero_nonzero_bits_from_vr (const tree expr_type
,
1019 wide_int
*may_be_nonzero
,
1020 wide_int
*must_be_nonzero
)
1022 *may_be_nonzero
= wi::minus_one (TYPE_PRECISION (expr_type
));
1023 *must_be_nonzero
= wi::zero (TYPE_PRECISION (expr_type
));
1024 if (!range_int_cst_p (vr
))
1027 if (range_int_cst_singleton_p (vr
))
1029 *may_be_nonzero
= wi::to_wide (vr
->min
);
1030 *must_be_nonzero
= *may_be_nonzero
;
1032 else if (tree_int_cst_sgn (vr
->min
) >= 0
1033 || tree_int_cst_sgn (vr
->max
) < 0)
1035 wide_int xor_mask
= wi::to_wide (vr
->min
) ^ wi::to_wide (vr
->max
);
1036 *may_be_nonzero
= wi::to_wide (vr
->min
) | wi::to_wide (vr
->max
);
1037 *must_be_nonzero
= wi::to_wide (vr
->min
) & wi::to_wide (vr
->max
);
1040 wide_int mask
= wi::mask (wi::floor_log2 (xor_mask
), false,
1041 may_be_nonzero
->get_precision ());
1042 *may_be_nonzero
= *may_be_nonzero
| mask
;
1043 *must_be_nonzero
= wi::bit_and_not (*must_be_nonzero
, mask
);
1050 /* Create two value-ranges in *VR0 and *VR1 from the anti-range *AR
1051 so that *VR0 U *VR1 == *AR. Returns true if that is possible,
1052 false otherwise. If *AR can be represented with a single range
1053 *VR1 will be VR_UNDEFINED. */
1056 ranges_from_anti_range (value_range
*ar
,
1057 value_range
*vr0
, value_range
*vr1
)
1059 tree type
= TREE_TYPE (ar
->min
);
1061 vr0
->type
= VR_UNDEFINED
;
1062 vr1
->type
= VR_UNDEFINED
;
1064 if (ar
->type
!= VR_ANTI_RANGE
1065 || TREE_CODE (ar
->min
) != INTEGER_CST
1066 || TREE_CODE (ar
->max
) != INTEGER_CST
1067 || !vrp_val_min (type
)
1068 || !vrp_val_max (type
))
1071 if (!vrp_val_is_min (ar
->min
))
1073 vr0
->type
= VR_RANGE
;
1074 vr0
->min
= vrp_val_min (type
);
1075 vr0
->max
= wide_int_to_tree (type
, wi::to_wide (ar
->min
) - 1);
1077 if (!vrp_val_is_max (ar
->max
))
1079 vr1
->type
= VR_RANGE
;
1080 vr1
->min
= wide_int_to_tree (type
, wi::to_wide (ar
->max
) + 1);
1081 vr1
->max
= vrp_val_max (type
);
1083 if (vr0
->type
== VR_UNDEFINED
)
1086 vr1
->type
= VR_UNDEFINED
;
1089 return vr0
->type
!= VR_UNDEFINED
;
1092 /* Helper to extract a value-range *VR for a multiplicative operation
1096 extract_range_from_multiplicative_op_1 (value_range
*vr
,
1097 enum tree_code code
,
1098 value_range
*vr0
, value_range
*vr1
)
1100 enum value_range_type rtype
;
1101 wide_int val
, min
, max
;
1104 /* Multiplications, divisions and shifts are a bit tricky to handle,
1105 depending on the mix of signs we have in the two ranges, we
1106 need to operate on different values to get the minimum and
1107 maximum values for the new range. One approach is to figure
1108 out all the variations of range combinations and do the
1111 However, this involves several calls to compare_values and it
1112 is pretty convoluted. It's simpler to do the 4 operations
1113 (MIN0 OP MIN1, MIN0 OP MAX1, MAX0 OP MIN1 and MAX0 OP MAX0 OP
1114 MAX1) and then figure the smallest and largest values to form
1116 gcc_assert (code
== MULT_EXPR
1117 || code
== TRUNC_DIV_EXPR
1118 || code
== FLOOR_DIV_EXPR
1119 || code
== CEIL_DIV_EXPR
1120 || code
== EXACT_DIV_EXPR
1121 || code
== ROUND_DIV_EXPR
1122 || code
== RSHIFT_EXPR
1123 || code
== LSHIFT_EXPR
);
1124 gcc_assert (vr0
->type
== VR_RANGE
1125 && vr0
->type
== vr1
->type
);
1128 type
= TREE_TYPE (vr0
->min
);
1129 signop sgn
= TYPE_SIGN (type
);
1131 /* Compute the 4 cross operations and their minimum and maximum value. */
1132 if (!vrp_int_const_binop (code
, vr0
->min
, vr1
->min
, &val
))
1134 set_value_range_to_varying (vr
);
1139 if (vr1
->max
!= vr1
->min
)
1141 if (!vrp_int_const_binop (code
, vr0
->min
, vr1
->max
, &val
))
1143 set_value_range_to_varying (vr
);
1146 if (wi::lt_p (val
, min
, sgn
))
1148 else if (wi::gt_p (val
, max
, sgn
))
1152 if (vr0
->max
!= vr0
->min
)
1154 if (!vrp_int_const_binop (code
, vr0
->max
, vr1
->min
, &val
))
1156 set_value_range_to_varying (vr
);
1159 if (wi::lt_p (val
, min
, sgn
))
1161 else if (wi::gt_p (val
, max
, sgn
))
1165 if (vr0
->min
!= vr0
->max
&& vr1
->min
!= vr1
->max
)
1167 if (!vrp_int_const_binop (code
, vr0
->max
, vr1
->max
, &val
))
1169 set_value_range_to_varying (vr
);
1172 if (wi::lt_p (val
, min
, sgn
))
1174 else if (wi::gt_p (val
, max
, sgn
))
1178 /* If the new range has its limits swapped around (MIN > MAX),
1179 then the operation caused one of them to wrap around, mark
1180 the new range VARYING. */
1181 if (wi::gt_p (min
, max
, sgn
))
1183 set_value_range_to_varying (vr
);
1187 /* We punt for [-INF, +INF].
1188 We learn nothing when we have INF on both sides.
1189 Note that we do accept [-INF, -INF] and [+INF, +INF]. */
1190 if (wi::eq_p (min
, wi::min_value (TYPE_PRECISION (type
), sgn
))
1191 && wi::eq_p (max
, wi::max_value (TYPE_PRECISION (type
), sgn
)))
1193 set_value_range_to_varying (vr
);
1197 set_value_range (vr
, rtype
,
1198 wide_int_to_tree (type
, min
),
1199 wide_int_to_tree (type
, max
), NULL
);
1202 /* Extract range information from a binary operation CODE based on
1203 the ranges of each of its operands *VR0 and *VR1 with resulting
1204 type EXPR_TYPE. The resulting range is stored in *VR. */
1207 extract_range_from_binary_expr_1 (value_range
*vr
,
1208 enum tree_code code
, tree expr_type
,
1209 value_range
*vr0_
, value_range
*vr1_
)
1211 value_range vr0
= *vr0_
, vr1
= *vr1_
;
1212 value_range vrtem0
= VR_INITIALIZER
, vrtem1
= VR_INITIALIZER
;
1213 enum value_range_type type
;
1214 tree min
= NULL_TREE
, max
= NULL_TREE
;
1217 if (!INTEGRAL_TYPE_P (expr_type
)
1218 && !POINTER_TYPE_P (expr_type
))
1220 set_value_range_to_varying (vr
);
1224 /* Not all binary expressions can be applied to ranges in a
1225 meaningful way. Handle only arithmetic operations. */
1226 if (code
!= PLUS_EXPR
1227 && code
!= MINUS_EXPR
1228 && code
!= POINTER_PLUS_EXPR
1229 && code
!= MULT_EXPR
1230 && code
!= TRUNC_DIV_EXPR
1231 && code
!= FLOOR_DIV_EXPR
1232 && code
!= CEIL_DIV_EXPR
1233 && code
!= EXACT_DIV_EXPR
1234 && code
!= ROUND_DIV_EXPR
1235 && code
!= TRUNC_MOD_EXPR
1236 && code
!= RSHIFT_EXPR
1237 && code
!= LSHIFT_EXPR
1240 && code
!= BIT_AND_EXPR
1241 && code
!= BIT_IOR_EXPR
1242 && code
!= BIT_XOR_EXPR
)
1244 set_value_range_to_varying (vr
);
1248 /* If both ranges are UNDEFINED, so is the result. */
1249 if (vr0
.type
== VR_UNDEFINED
&& vr1
.type
== VR_UNDEFINED
)
1251 set_value_range_to_undefined (vr
);
1254 /* If one of the ranges is UNDEFINED drop it to VARYING for the following
1255 code. At some point we may want to special-case operations that
1256 have UNDEFINED result for all or some value-ranges of the not UNDEFINED
1258 else if (vr0
.type
== VR_UNDEFINED
)
1259 set_value_range_to_varying (&vr0
);
1260 else if (vr1
.type
== VR_UNDEFINED
)
1261 set_value_range_to_varying (&vr1
);
1263 /* We get imprecise results from ranges_from_anti_range when
1264 code is EXACT_DIV_EXPR. We could mask out bits in the resulting
1265 range, but then we also need to hack up vrp_meet. It's just
1266 easier to special case when vr0 is ~[0,0] for EXACT_DIV_EXPR. */
1267 if (code
== EXACT_DIV_EXPR
1268 && vr0
.type
== VR_ANTI_RANGE
1269 && vr0
.min
== vr0
.max
1270 && integer_zerop (vr0
.min
))
1272 set_value_range_to_nonnull (vr
, expr_type
);
1276 /* Now canonicalize anti-ranges to ranges when they are not symbolic
1277 and express ~[] op X as ([]' op X) U ([]'' op X). */
1278 if (vr0
.type
== VR_ANTI_RANGE
1279 && ranges_from_anti_range (&vr0
, &vrtem0
, &vrtem1
))
1281 extract_range_from_binary_expr_1 (vr
, code
, expr_type
, &vrtem0
, vr1_
);
1282 if (vrtem1
.type
!= VR_UNDEFINED
)
1284 value_range vrres
= VR_INITIALIZER
;
1285 extract_range_from_binary_expr_1 (&vrres
, code
, expr_type
,
1287 vrp_meet (vr
, &vrres
);
1291 /* Likewise for X op ~[]. */
1292 if (vr1
.type
== VR_ANTI_RANGE
1293 && ranges_from_anti_range (&vr1
, &vrtem0
, &vrtem1
))
1295 extract_range_from_binary_expr_1 (vr
, code
, expr_type
, vr0_
, &vrtem0
);
1296 if (vrtem1
.type
!= VR_UNDEFINED
)
1298 value_range vrres
= VR_INITIALIZER
;
1299 extract_range_from_binary_expr_1 (&vrres
, code
, expr_type
,
1301 vrp_meet (vr
, &vrres
);
1306 /* The type of the resulting value range defaults to VR0.TYPE. */
1309 /* Refuse to operate on VARYING ranges, ranges of different kinds
1310 and symbolic ranges. As an exception, we allow BIT_{AND,IOR}
1311 because we may be able to derive a useful range even if one of
1312 the operands is VR_VARYING or symbolic range. Similarly for
1313 divisions, MIN/MAX and PLUS/MINUS.
1315 TODO, we may be able to derive anti-ranges in some cases. */
1316 if (code
!= BIT_AND_EXPR
1317 && code
!= BIT_IOR_EXPR
1318 && code
!= TRUNC_DIV_EXPR
1319 && code
!= FLOOR_DIV_EXPR
1320 && code
!= CEIL_DIV_EXPR
1321 && code
!= EXACT_DIV_EXPR
1322 && code
!= ROUND_DIV_EXPR
1323 && code
!= TRUNC_MOD_EXPR
1326 && code
!= PLUS_EXPR
1327 && code
!= MINUS_EXPR
1328 && code
!= RSHIFT_EXPR
1329 && (vr0
.type
== VR_VARYING
1330 || vr1
.type
== VR_VARYING
1331 || vr0
.type
!= vr1
.type
1332 || symbolic_range_p (&vr0
)
1333 || symbolic_range_p (&vr1
)))
1335 set_value_range_to_varying (vr
);
1339 /* Now evaluate the expression to determine the new range. */
1340 if (POINTER_TYPE_P (expr_type
))
1342 if (code
== MIN_EXPR
|| code
== MAX_EXPR
)
1344 /* For MIN/MAX expressions with pointers, we only care about
1345 nullness, if both are non null, then the result is nonnull.
1346 If both are null, then the result is null. Otherwise they
1348 if (range_is_nonnull (&vr0
) && range_is_nonnull (&vr1
))
1349 set_value_range_to_nonnull (vr
, expr_type
);
1350 else if (range_is_null (&vr0
) && range_is_null (&vr1
))
1351 set_value_range_to_null (vr
, expr_type
);
1353 set_value_range_to_varying (vr
);
1355 else if (code
== POINTER_PLUS_EXPR
)
1357 /* For pointer types, we are really only interested in asserting
1358 whether the expression evaluates to non-NULL. */
1359 if (range_is_nonnull (&vr0
) || range_is_nonnull (&vr1
))
1360 set_value_range_to_nonnull (vr
, expr_type
);
1361 else if (range_is_null (&vr0
) && range_is_null (&vr1
))
1362 set_value_range_to_null (vr
, expr_type
);
1364 set_value_range_to_varying (vr
);
1366 else if (code
== BIT_AND_EXPR
)
1368 /* For pointer types, we are really only interested in asserting
1369 whether the expression evaluates to non-NULL. */
1370 if (range_is_nonnull (&vr0
) && range_is_nonnull (&vr1
))
1371 set_value_range_to_nonnull (vr
, expr_type
);
1372 else if (range_is_null (&vr0
) || range_is_null (&vr1
))
1373 set_value_range_to_null (vr
, expr_type
);
1375 set_value_range_to_varying (vr
);
1378 set_value_range_to_varying (vr
);
1383 /* For integer ranges, apply the operation to each end of the
1384 range and see what we end up with. */
1385 if (code
== PLUS_EXPR
|| code
== MINUS_EXPR
)
1387 const bool minus_p
= (code
== MINUS_EXPR
);
1388 tree min_op0
= vr0
.min
;
1389 tree min_op1
= minus_p
? vr1
.max
: vr1
.min
;
1390 tree max_op0
= vr0
.max
;
1391 tree max_op1
= minus_p
? vr1
.min
: vr1
.max
;
1392 tree sym_min_op0
= NULL_TREE
;
1393 tree sym_min_op1
= NULL_TREE
;
1394 tree sym_max_op0
= NULL_TREE
;
1395 tree sym_max_op1
= NULL_TREE
;
1396 bool neg_min_op0
, neg_min_op1
, neg_max_op0
, neg_max_op1
;
1398 /* If we have a PLUS or MINUS with two VR_RANGEs, either constant or
1399 single-symbolic ranges, try to compute the precise resulting range,
1400 but only if we know that this resulting range will also be constant
1401 or single-symbolic. */
1402 if (vr0
.type
== VR_RANGE
&& vr1
.type
== VR_RANGE
1403 && (TREE_CODE (min_op0
) == INTEGER_CST
1405 = get_single_symbol (min_op0
, &neg_min_op0
, &min_op0
)))
1406 && (TREE_CODE (min_op1
) == INTEGER_CST
1408 = get_single_symbol (min_op1
, &neg_min_op1
, &min_op1
)))
1409 && (!(sym_min_op0
&& sym_min_op1
)
1410 || (sym_min_op0
== sym_min_op1
1411 && neg_min_op0
== (minus_p
? neg_min_op1
: !neg_min_op1
)))
1412 && (TREE_CODE (max_op0
) == INTEGER_CST
1414 = get_single_symbol (max_op0
, &neg_max_op0
, &max_op0
)))
1415 && (TREE_CODE (max_op1
) == INTEGER_CST
1417 = get_single_symbol (max_op1
, &neg_max_op1
, &max_op1
)))
1418 && (!(sym_max_op0
&& sym_max_op1
)
1419 || (sym_max_op0
== sym_max_op1
1420 && neg_max_op0
== (minus_p
? neg_max_op1
: !neg_max_op1
))))
1422 const signop sgn
= TYPE_SIGN (expr_type
);
1423 const unsigned int prec
= TYPE_PRECISION (expr_type
);
1424 wide_int type_min
, type_max
, wmin
, wmax
;
1428 /* Get the lower and upper bounds of the type. */
1429 if (TYPE_OVERFLOW_WRAPS (expr_type
))
1431 type_min
= wi::min_value (prec
, sgn
);
1432 type_max
= wi::max_value (prec
, sgn
);
1436 type_min
= wi::to_wide (vrp_val_min (expr_type
));
1437 type_max
= wi::to_wide (vrp_val_max (expr_type
));
1440 /* Combine the lower bounds, if any. */
1441 if (min_op0
&& min_op1
)
1445 wmin
= wi::to_wide (min_op0
) - wi::to_wide (min_op1
);
1447 /* Check for overflow. */
1448 if (wi::cmp (0, wi::to_wide (min_op1
), sgn
)
1449 != wi::cmp (wmin
, wi::to_wide (min_op0
), sgn
))
1450 min_ovf
= wi::cmp (wi::to_wide (min_op0
),
1451 wi::to_wide (min_op1
), sgn
);
1455 wmin
= wi::to_wide (min_op0
) + wi::to_wide (min_op1
);
1457 /* Check for overflow. */
1458 if (wi::cmp (wi::to_wide (min_op1
), 0, sgn
)
1459 != wi::cmp (wmin
, wi::to_wide (min_op0
), sgn
))
1460 min_ovf
= wi::cmp (wi::to_wide (min_op0
), wmin
, sgn
);
1464 wmin
= wi::to_wide (min_op0
);
1469 wmin
= -wi::to_wide (min_op1
);
1471 /* Check for overflow. */
1473 && wi::neg_p (wi::to_wide (min_op1
))
1474 && wi::neg_p (wmin
))
1476 else if (sgn
== UNSIGNED
&& wi::to_wide (min_op1
) != 0)
1480 wmin
= wi::to_wide (min_op1
);
1483 wmin
= wi::shwi (0, prec
);
1485 /* Combine the upper bounds, if any. */
1486 if (max_op0
&& max_op1
)
1490 wmax
= wi::to_wide (max_op0
) - wi::to_wide (max_op1
);
1492 /* Check for overflow. */
1493 if (wi::cmp (0, wi::to_wide (max_op1
), sgn
)
1494 != wi::cmp (wmax
, wi::to_wide (max_op0
), sgn
))
1495 max_ovf
= wi::cmp (wi::to_wide (max_op0
),
1496 wi::to_wide (max_op1
), sgn
);
1500 wmax
= wi::to_wide (max_op0
) + wi::to_wide (max_op1
);
1502 if (wi::cmp (wi::to_wide (max_op1
), 0, sgn
)
1503 != wi::cmp (wmax
, wi::to_wide (max_op0
), sgn
))
1504 max_ovf
= wi::cmp (wi::to_wide (max_op0
), wmax
, sgn
);
1508 wmax
= wi::to_wide (max_op0
);
1513 wmax
= -wi::to_wide (max_op1
);
1515 /* Check for overflow. */
1517 && wi::neg_p (wi::to_wide (max_op1
))
1518 && wi::neg_p (wmax
))
1520 else if (sgn
== UNSIGNED
&& wi::to_wide (max_op1
) != 0)
1524 wmax
= wi::to_wide (max_op1
);
1527 wmax
= wi::shwi (0, prec
);
1529 /* Check for type overflow. */
1532 if (wi::cmp (wmin
, type_min
, sgn
) == -1)
1534 else if (wi::cmp (wmin
, type_max
, sgn
) == 1)
1539 if (wi::cmp (wmax
, type_min
, sgn
) == -1)
1541 else if (wi::cmp (wmax
, type_max
, sgn
) == 1)
1545 /* If we have overflow for the constant part and the resulting
1546 range will be symbolic, drop to VR_VARYING. */
1547 if ((min_ovf
&& sym_min_op0
!= sym_min_op1
)
1548 || (max_ovf
&& sym_max_op0
!= sym_max_op1
))
1550 set_value_range_to_varying (vr
);
1554 if (TYPE_OVERFLOW_WRAPS (expr_type
))
1556 /* If overflow wraps, truncate the values and adjust the
1557 range kind and bounds appropriately. */
1558 wide_int tmin
= wide_int::from (wmin
, prec
, sgn
);
1559 wide_int tmax
= wide_int::from (wmax
, prec
, sgn
);
1560 if (min_ovf
== max_ovf
)
1562 /* No overflow or both overflow or underflow. The
1563 range kind stays VR_RANGE. */
1564 min
= wide_int_to_tree (expr_type
, tmin
);
1565 max
= wide_int_to_tree (expr_type
, tmax
);
1567 else if ((min_ovf
== -1 && max_ovf
== 0)
1568 || (max_ovf
== 1 && min_ovf
== 0))
1570 /* Min underflow or max overflow. The range kind
1571 changes to VR_ANTI_RANGE. */
1572 bool covers
= false;
1573 wide_int tem
= tmin
;
1574 type
= VR_ANTI_RANGE
;
1576 if (wi::cmp (tmin
, tmax
, sgn
) < 0)
1579 if (wi::cmp (tmax
, tem
, sgn
) > 0)
1581 /* If the anti-range would cover nothing, drop to varying.
1582 Likewise if the anti-range bounds are outside of the
1584 if (covers
|| wi::cmp (tmin
, tmax
, sgn
) > 0)
1586 set_value_range_to_varying (vr
);
1589 min
= wide_int_to_tree (expr_type
, tmin
);
1590 max
= wide_int_to_tree (expr_type
, tmax
);
1594 /* Other underflow and/or overflow, drop to VR_VARYING. */
1595 set_value_range_to_varying (vr
);
1601 /* If overflow does not wrap, saturate to the types min/max
1604 min
= wide_int_to_tree (expr_type
, type_min
);
1605 else if (min_ovf
== 1)
1606 min
= wide_int_to_tree (expr_type
, type_max
);
1608 min
= wide_int_to_tree (expr_type
, wmin
);
1611 max
= wide_int_to_tree (expr_type
, type_min
);
1612 else if (max_ovf
== 1)
1613 max
= wide_int_to_tree (expr_type
, type_max
);
1615 max
= wide_int_to_tree (expr_type
, wmax
);
1618 /* If the result lower bound is constant, we're done;
1619 otherwise, build the symbolic lower bound. */
1620 if (sym_min_op0
== sym_min_op1
)
1622 else if (sym_min_op0
)
1623 min
= build_symbolic_expr (expr_type
, sym_min_op0
,
1625 else if (sym_min_op1
)
1627 /* We may not negate if that might introduce
1628 undefined overflow. */
1631 || TYPE_OVERFLOW_WRAPS (expr_type
))
1632 min
= build_symbolic_expr (expr_type
, sym_min_op1
,
1633 neg_min_op1
^ minus_p
, min
);
1638 /* Likewise for the upper bound. */
1639 if (sym_max_op0
== sym_max_op1
)
1641 else if (sym_max_op0
)
1642 max
= build_symbolic_expr (expr_type
, sym_max_op0
,
1644 else if (sym_max_op1
)
1646 /* We may not negate if that might introduce
1647 undefined overflow. */
1650 || TYPE_OVERFLOW_WRAPS (expr_type
))
1651 max
= build_symbolic_expr (expr_type
, sym_max_op1
,
1652 neg_max_op1
^ minus_p
, max
);
1659 /* For other cases, for example if we have a PLUS_EXPR with two
1660 VR_ANTI_RANGEs, drop to VR_VARYING. It would take more effort
1661 to compute a precise range for such a case.
1662 ??? General even mixed range kind operations can be expressed
1663 by for example transforming ~[3, 5] + [1, 2] to range-only
1664 operations and a union primitive:
1665 [-INF, 2] + [1, 2] U [5, +INF] + [1, 2]
1666 [-INF+1, 4] U [6, +INF(OVF)]
1667 though usually the union is not exactly representable with
1668 a single range or anti-range as the above is
1669 [-INF+1, +INF(OVF)] intersected with ~[5, 5]
1670 but one could use a scheme similar to equivalences for this. */
1671 set_value_range_to_varying (vr
);
1675 else if (code
== MIN_EXPR
1676 || code
== MAX_EXPR
)
1678 if (vr0
.type
== VR_RANGE
1679 && !symbolic_range_p (&vr0
))
1682 if (vr1
.type
== VR_RANGE
1683 && !symbolic_range_p (&vr1
))
1685 /* For operations that make the resulting range directly
1686 proportional to the original ranges, apply the operation to
1687 the same end of each range. */
1688 min
= int_const_binop (code
, vr0
.min
, vr1
.min
);
1689 max
= int_const_binop (code
, vr0
.max
, vr1
.max
);
1691 else if (code
== MIN_EXPR
)
1693 min
= vrp_val_min (expr_type
);
1696 else if (code
== MAX_EXPR
)
1699 max
= vrp_val_max (expr_type
);
1702 else if (vr1
.type
== VR_RANGE
1703 && !symbolic_range_p (&vr1
))
1706 if (code
== MIN_EXPR
)
1708 min
= vrp_val_min (expr_type
);
1711 else if (code
== MAX_EXPR
)
1714 max
= vrp_val_max (expr_type
);
1719 set_value_range_to_varying (vr
);
1723 else if (code
== MULT_EXPR
)
1725 /* Fancy code so that with unsigned, [-3,-1]*[-3,-1] does not
1726 drop to varying. This test requires 2*prec bits if both
1727 operands are signed and 2*prec + 2 bits if either is not. */
1729 signop sign
= TYPE_SIGN (expr_type
);
1730 unsigned int prec
= TYPE_PRECISION (expr_type
);
1732 if (!range_int_cst_p (&vr0
)
1733 || !range_int_cst_p (&vr1
))
1735 set_value_range_to_varying (vr
);
1739 if (TYPE_OVERFLOW_WRAPS (expr_type
))
1741 typedef FIXED_WIDE_INT (WIDE_INT_MAX_PRECISION
* 2) vrp_int
;
1742 typedef generic_wide_int
1743 <wi::extended_tree
<WIDE_INT_MAX_PRECISION
* 2> > vrp_int_cst
;
1744 vrp_int sizem1
= wi::mask
<vrp_int
> (prec
, false);
1745 vrp_int size
= sizem1
+ 1;
1747 /* Extend the values using the sign of the result to PREC2.
1748 From here on out, everthing is just signed math no matter
1749 what the input types were. */
1750 vrp_int min0
= vrp_int_cst (vr0
.min
);
1751 vrp_int max0
= vrp_int_cst (vr0
.max
);
1752 vrp_int min1
= vrp_int_cst (vr1
.min
);
1753 vrp_int max1
= vrp_int_cst (vr1
.max
);
1754 /* Canonicalize the intervals. */
1755 if (sign
== UNSIGNED
)
1757 if (wi::ltu_p (size
, min0
+ max0
))
1763 if (wi::ltu_p (size
, min1
+ max1
))
1770 vrp_int prod0
= min0
* min1
;
1771 vrp_int prod1
= min0
* max1
;
1772 vrp_int prod2
= max0
* min1
;
1773 vrp_int prod3
= max0
* max1
;
1775 /* Sort the 4 products so that min is in prod0 and max is in
1777 /* min0min1 > max0max1 */
1779 std::swap (prod0
, prod3
);
1781 /* min0max1 > max0min1 */
1783 std::swap (prod1
, prod2
);
1786 std::swap (prod0
, prod1
);
1789 std::swap (prod2
, prod3
);
1791 /* diff = max - min. */
1792 prod2
= prod3
- prod0
;
1793 if (wi::geu_p (prod2
, sizem1
))
1795 /* the range covers all values. */
1796 set_value_range_to_varying (vr
);
1800 /* The following should handle the wrapping and selecting
1801 VR_ANTI_RANGE for us. */
1802 min
= wide_int_to_tree (expr_type
, prod0
);
1803 max
= wide_int_to_tree (expr_type
, prod3
);
1804 set_and_canonicalize_value_range (vr
, VR_RANGE
, min
, max
, NULL
);
1808 /* If we have an unsigned MULT_EXPR with two VR_ANTI_RANGEs,
1809 drop to VR_VARYING. It would take more effort to compute a
1810 precise range for such a case. For example, if we have
1811 op0 == 65536 and op1 == 65536 with their ranges both being
1812 ~[0,0] on a 32-bit machine, we would have op0 * op1 == 0, so
1813 we cannot claim that the product is in ~[0,0]. Note that we
1814 are guaranteed to have vr0.type == vr1.type at this
1816 if (vr0
.type
== VR_ANTI_RANGE
1817 && !TYPE_OVERFLOW_UNDEFINED (expr_type
))
1819 set_value_range_to_varying (vr
);
1823 extract_range_from_multiplicative_op_1 (vr
, code
, &vr0
, &vr1
);
1826 else if (code
== RSHIFT_EXPR
1827 || code
== LSHIFT_EXPR
)
1829 /* If we have a RSHIFT_EXPR with any shift values outside [0..prec-1],
1830 then drop to VR_VARYING. Outside of this range we get undefined
1831 behavior from the shift operation. We cannot even trust
1832 SHIFT_COUNT_TRUNCATED at this stage, because that applies to rtl
1833 shifts, and the operation at the tree level may be widened. */
1834 if (range_int_cst_p (&vr1
)
1835 && compare_tree_int (vr1
.min
, 0) >= 0
1836 && compare_tree_int (vr1
.max
, TYPE_PRECISION (expr_type
)) == -1)
1838 if (code
== RSHIFT_EXPR
)
1840 /* Even if vr0 is VARYING or otherwise not usable, we can derive
1841 useful ranges just from the shift count. E.g.
1842 x >> 63 for signed 64-bit x is always [-1, 0]. */
1843 if (vr0
.type
!= VR_RANGE
|| symbolic_range_p (&vr0
))
1845 vr0
.type
= type
= VR_RANGE
;
1846 vr0
.min
= vrp_val_min (expr_type
);
1847 vr0
.max
= vrp_val_max (expr_type
);
1849 extract_range_from_multiplicative_op_1 (vr
, code
, &vr0
, &vr1
);
1852 /* We can map lshifts by constants to MULT_EXPR handling. */
1853 else if (code
== LSHIFT_EXPR
1854 && range_int_cst_singleton_p (&vr1
))
1856 bool saved_flag_wrapv
;
1857 value_range vr1p
= VR_INITIALIZER
;
1858 vr1p
.type
= VR_RANGE
;
1859 vr1p
.min
= (wide_int_to_tree
1861 wi::set_bit_in_zero (tree_to_shwi (vr1
.min
),
1862 TYPE_PRECISION (expr_type
))));
1863 vr1p
.max
= vr1p
.min
;
1864 /* We have to use a wrapping multiply though as signed overflow
1865 on lshifts is implementation defined in C89. */
1866 saved_flag_wrapv
= flag_wrapv
;
1868 extract_range_from_binary_expr_1 (vr
, MULT_EXPR
, expr_type
,
1870 flag_wrapv
= saved_flag_wrapv
;
1873 else if (code
== LSHIFT_EXPR
1874 && range_int_cst_p (&vr0
))
1876 int prec
= TYPE_PRECISION (expr_type
);
1877 int overflow_pos
= prec
;
1879 wide_int low_bound
, high_bound
;
1880 bool uns
= TYPE_UNSIGNED (expr_type
);
1881 bool in_bounds
= false;
1886 bound_shift
= overflow_pos
- tree_to_shwi (vr1
.max
);
1887 /* If bound_shift == HOST_BITS_PER_WIDE_INT, the llshift can
1888 overflow. However, for that to happen, vr1.max needs to be
1889 zero, which means vr1 is a singleton range of zero, which
1890 means it should be handled by the previous LSHIFT_EXPR
1892 wide_int bound
= wi::set_bit_in_zero (bound_shift
, prec
);
1893 wide_int complement
= ~(bound
- 1);
1898 high_bound
= complement
;
1899 if (wi::ltu_p (wi::to_wide (vr0
.max
), low_bound
))
1901 /* [5, 6] << [1, 2] == [10, 24]. */
1902 /* We're shifting out only zeroes, the value increases
1906 else if (wi::ltu_p (high_bound
, wi::to_wide (vr0
.min
)))
1908 /* [0xffffff00, 0xffffffff] << [1, 2]
1909 == [0xfffffc00, 0xfffffffe]. */
1910 /* We're shifting out only ones, the value decreases
1917 /* [-1, 1] << [1, 2] == [-4, 4]. */
1918 low_bound
= complement
;
1920 if (wi::lts_p (wi::to_wide (vr0
.max
), high_bound
)
1921 && wi::lts_p (low_bound
, wi::to_wide (vr0
.min
)))
1923 /* For non-negative numbers, we're shifting out only
1924 zeroes, the value increases monotonically.
1925 For negative numbers, we're shifting out only ones, the
1926 value decreases monotomically. */
1933 extract_range_from_multiplicative_op_1 (vr
, code
, &vr0
, &vr1
);
1938 set_value_range_to_varying (vr
);
1941 else if (code
== TRUNC_DIV_EXPR
1942 || code
== FLOOR_DIV_EXPR
1943 || code
== CEIL_DIV_EXPR
1944 || code
== EXACT_DIV_EXPR
1945 || code
== ROUND_DIV_EXPR
)
1947 if (vr0
.type
!= VR_RANGE
|| symbolic_range_p (&vr0
))
1949 /* For division, if op1 has VR_RANGE but op0 does not, something
1950 can be deduced just from that range. Say [min, max] / [4, max]
1951 gives [min / 4, max / 4] range. */
1952 if (vr1
.type
== VR_RANGE
1953 && !symbolic_range_p (&vr1
)
1954 && range_includes_zero_p (vr1
.min
, vr1
.max
) == 0)
1956 vr0
.type
= type
= VR_RANGE
;
1957 vr0
.min
= vrp_val_min (expr_type
);
1958 vr0
.max
= vrp_val_max (expr_type
);
1962 set_value_range_to_varying (vr
);
1967 /* For divisions, if flag_non_call_exceptions is true, we must
1968 not eliminate a division by zero. */
1969 if (cfun
->can_throw_non_call_exceptions
1970 && (vr1
.type
!= VR_RANGE
1971 || range_includes_zero_p (vr1
.min
, vr1
.max
) != 0))
1973 set_value_range_to_varying (vr
);
1977 /* For divisions, if op0 is VR_RANGE, we can deduce a range
1978 even if op1 is VR_VARYING, VR_ANTI_RANGE, symbolic or can
1980 if (vr0
.type
== VR_RANGE
1981 && (vr1
.type
!= VR_RANGE
1982 || range_includes_zero_p (vr1
.min
, vr1
.max
) != 0))
1984 tree zero
= build_int_cst (TREE_TYPE (vr0
.min
), 0);
1989 if (TYPE_UNSIGNED (expr_type
)
1990 || value_range_nonnegative_p (&vr1
))
1992 /* For unsigned division or when divisor is known
1993 to be non-negative, the range has to cover
1994 all numbers from 0 to max for positive max
1995 and all numbers from min to 0 for negative min. */
1996 cmp
= compare_values (vr0
.max
, zero
);
1999 /* When vr0.max < 0, vr1.min != 0 and value
2000 ranges for dividend and divisor are available. */
2001 if (vr1
.type
== VR_RANGE
2002 && !symbolic_range_p (&vr0
)
2003 && !symbolic_range_p (&vr1
)
2004 && compare_values (vr1
.min
, zero
) != 0)
2005 max
= int_const_binop (code
, vr0
.max
, vr1
.min
);
2009 else if (cmp
== 0 || cmp
== 1)
2013 cmp
= compare_values (vr0
.min
, zero
);
2016 /* For unsigned division when value ranges for dividend
2017 and divisor are available. */
2018 if (vr1
.type
== VR_RANGE
2019 && !symbolic_range_p (&vr0
)
2020 && !symbolic_range_p (&vr1
)
2021 && compare_values (vr1
.max
, zero
) != 0)
2022 min
= int_const_binop (code
, vr0
.min
, vr1
.max
);
2026 else if (cmp
== 0 || cmp
== -1)
2033 /* Otherwise the range is -max .. max or min .. -min
2034 depending on which bound is bigger in absolute value,
2035 as the division can change the sign. */
2036 abs_extent_range (vr
, vr0
.min
, vr0
.max
);
2039 if (type
== VR_VARYING
)
2041 set_value_range_to_varying (vr
);
2045 else if (!symbolic_range_p (&vr0
) && !symbolic_range_p (&vr1
))
2047 extract_range_from_multiplicative_op_1 (vr
, code
, &vr0
, &vr1
);
2051 else if (code
== TRUNC_MOD_EXPR
)
2053 if (range_is_null (&vr1
))
2055 set_value_range_to_undefined (vr
);
2058 /* ABS (A % B) < ABS (B) and either
2059 0 <= A % B <= A or A <= A % B <= 0. */
2061 signop sgn
= TYPE_SIGN (expr_type
);
2062 unsigned int prec
= TYPE_PRECISION (expr_type
);
2063 wide_int wmin
, wmax
, tmp
;
2064 if (vr1
.type
== VR_RANGE
&& !symbolic_range_p (&vr1
))
2066 wmax
= wi::to_wide (vr1
.max
) - 1;
2069 tmp
= -1 - wi::to_wide (vr1
.min
);
2070 wmax
= wi::smax (wmax
, tmp
);
2075 wmax
= wi::max_value (prec
, sgn
);
2076 /* X % INT_MIN may be INT_MAX. */
2077 if (sgn
== UNSIGNED
)
2081 if (sgn
== UNSIGNED
)
2082 wmin
= wi::zero (prec
);
2086 if (vr0
.type
== VR_RANGE
&& TREE_CODE (vr0
.min
) == INTEGER_CST
)
2088 tmp
= wi::to_wide (vr0
.min
);
2089 if (wi::gts_p (tmp
, 0))
2090 tmp
= wi::zero (prec
);
2091 wmin
= wi::smax (wmin
, tmp
);
2095 if (vr0
.type
== VR_RANGE
&& TREE_CODE (vr0
.max
) == INTEGER_CST
)
2097 tmp
= wi::to_wide (vr0
.max
);
2098 if (sgn
== SIGNED
&& wi::neg_p (tmp
))
2099 tmp
= wi::zero (prec
);
2100 wmax
= wi::min (wmax
, tmp
, sgn
);
2103 min
= wide_int_to_tree (expr_type
, wmin
);
2104 max
= wide_int_to_tree (expr_type
, wmax
);
2106 else if (code
== BIT_AND_EXPR
|| code
== BIT_IOR_EXPR
|| code
== BIT_XOR_EXPR
)
2108 bool int_cst_range0
, int_cst_range1
;
2109 wide_int may_be_nonzero0
, may_be_nonzero1
;
2110 wide_int must_be_nonzero0
, must_be_nonzero1
;
2112 int_cst_range0
= zero_nonzero_bits_from_vr (expr_type
, &vr0
,
2115 int_cst_range1
= zero_nonzero_bits_from_vr (expr_type
, &vr1
,
2119 if (code
== BIT_AND_EXPR
|| code
== BIT_IOR_EXPR
)
2121 value_range
*vr0p
= NULL
, *vr1p
= NULL
;
2122 if (range_int_cst_singleton_p (&vr1
))
2127 else if (range_int_cst_singleton_p (&vr0
))
2132 /* For op & or | attempt to optimize:
2133 [x, y] op z into [x op z, y op z]
2134 if z is a constant which (for op | its bitwise not) has n
2135 consecutive least significant bits cleared followed by m 1
2136 consecutive bits set immediately above it and either
2137 m + n == precision, or (x >> (m + n)) == (y >> (m + n)).
2138 The least significant n bits of all the values in the range are
2139 cleared or set, the m bits above it are preserved and any bits
2140 above these are required to be the same for all values in the
2142 if (vr0p
&& range_int_cst_p (vr0p
))
2144 wide_int w
= wi::to_wide (vr1p
->min
);
2146 if (code
== BIT_IOR_EXPR
)
2148 if (wi::eq_p (w
, 0))
2149 n
= TYPE_PRECISION (expr_type
);
2153 w
= ~(w
| wi::mask (n
, false, w
.get_precision ()));
2154 if (wi::eq_p (w
, 0))
2155 m
= TYPE_PRECISION (expr_type
) - n
;
2157 m
= wi::ctz (w
) - n
;
2159 wide_int mask
= wi::mask (m
+ n
, true, w
.get_precision ());
2160 if ((mask
& wi::to_wide (vr0p
->min
))
2161 == (mask
& wi::to_wide (vr0p
->max
)))
2163 min
= int_const_binop (code
, vr0p
->min
, vr1p
->min
);
2164 max
= int_const_binop (code
, vr0p
->max
, vr1p
->min
);
2171 /* Optimized above already. */;
2172 else if (code
== BIT_AND_EXPR
)
2174 min
= wide_int_to_tree (expr_type
,
2175 must_be_nonzero0
& must_be_nonzero1
);
2176 wide_int wmax
= may_be_nonzero0
& may_be_nonzero1
;
2177 /* If both input ranges contain only negative values we can
2178 truncate the result range maximum to the minimum of the
2179 input range maxima. */
2180 if (int_cst_range0
&& int_cst_range1
2181 && tree_int_cst_sgn (vr0
.max
) < 0
2182 && tree_int_cst_sgn (vr1
.max
) < 0)
2184 wmax
= wi::min (wmax
, wi::to_wide (vr0
.max
),
2185 TYPE_SIGN (expr_type
));
2186 wmax
= wi::min (wmax
, wi::to_wide (vr1
.max
),
2187 TYPE_SIGN (expr_type
));
2189 /* If either input range contains only non-negative values
2190 we can truncate the result range maximum to the respective
2191 maximum of the input range. */
2192 if (int_cst_range0
&& tree_int_cst_sgn (vr0
.min
) >= 0)
2193 wmax
= wi::min (wmax
, wi::to_wide (vr0
.max
),
2194 TYPE_SIGN (expr_type
));
2195 if (int_cst_range1
&& tree_int_cst_sgn (vr1
.min
) >= 0)
2196 wmax
= wi::min (wmax
, wi::to_wide (vr1
.max
),
2197 TYPE_SIGN (expr_type
));
2198 max
= wide_int_to_tree (expr_type
, wmax
);
2199 cmp
= compare_values (min
, max
);
2200 /* PR68217: In case of signed & sign-bit-CST should
2201 result in [-INF, 0] instead of [-INF, INF]. */
2202 if (cmp
== -2 || cmp
== 1)
2205 = wi::set_bit_in_zero (TYPE_PRECISION (expr_type
) - 1,
2206 TYPE_PRECISION (expr_type
));
2207 if (!TYPE_UNSIGNED (expr_type
)
2209 && value_range_constant_singleton (&vr0
)
2210 && !wi::cmps (wi::to_wide (vr0
.min
), sign_bit
))
2212 && value_range_constant_singleton (&vr1
)
2213 && !wi::cmps (wi::to_wide (vr1
.min
), sign_bit
))))
2215 min
= TYPE_MIN_VALUE (expr_type
);
2216 max
= build_int_cst (expr_type
, 0);
2220 else if (code
== BIT_IOR_EXPR
)
2222 max
= wide_int_to_tree (expr_type
,
2223 may_be_nonzero0
| may_be_nonzero1
);
2224 wide_int wmin
= must_be_nonzero0
| must_be_nonzero1
;
2225 /* If the input ranges contain only positive values we can
2226 truncate the minimum of the result range to the maximum
2227 of the input range minima. */
2228 if (int_cst_range0
&& int_cst_range1
2229 && tree_int_cst_sgn (vr0
.min
) >= 0
2230 && tree_int_cst_sgn (vr1
.min
) >= 0)
2232 wmin
= wi::max (wmin
, wi::to_wide (vr0
.min
),
2233 TYPE_SIGN (expr_type
));
2234 wmin
= wi::max (wmin
, wi::to_wide (vr1
.min
),
2235 TYPE_SIGN (expr_type
));
2237 /* If either input range contains only negative values
2238 we can truncate the minimum of the result range to the
2239 respective minimum range. */
2240 if (int_cst_range0
&& tree_int_cst_sgn (vr0
.max
) < 0)
2241 wmin
= wi::max (wmin
, wi::to_wide (vr0
.min
),
2242 TYPE_SIGN (expr_type
));
2243 if (int_cst_range1
&& tree_int_cst_sgn (vr1
.max
) < 0)
2244 wmin
= wi::max (wmin
, wi::to_wide (vr1
.min
),
2245 TYPE_SIGN (expr_type
));
2246 min
= wide_int_to_tree (expr_type
, wmin
);
2248 else if (code
== BIT_XOR_EXPR
)
2250 wide_int result_zero_bits
= ((must_be_nonzero0
& must_be_nonzero1
)
2251 | ~(may_be_nonzero0
| may_be_nonzero1
));
2252 wide_int result_one_bits
2253 = (wi::bit_and_not (must_be_nonzero0
, may_be_nonzero1
)
2254 | wi::bit_and_not (must_be_nonzero1
, may_be_nonzero0
));
2255 max
= wide_int_to_tree (expr_type
, ~result_zero_bits
);
2256 min
= wide_int_to_tree (expr_type
, result_one_bits
);
2257 /* If the range has all positive or all negative values the
2258 result is better than VARYING. */
2259 if (tree_int_cst_sgn (min
) < 0
2260 || tree_int_cst_sgn (max
) >= 0)
2263 max
= min
= NULL_TREE
;
2269 /* If either MIN or MAX overflowed, then set the resulting range to
2271 if (min
== NULL_TREE
2272 || TREE_OVERFLOW_P (min
)
2274 || TREE_OVERFLOW_P (max
))
2276 set_value_range_to_varying (vr
);
2280 /* We punt for [-INF, +INF].
2281 We learn nothing when we have INF on both sides.
2282 Note that we do accept [-INF, -INF] and [+INF, +INF]. */
2283 if (vrp_val_is_min (min
) && vrp_val_is_max (max
))
2285 set_value_range_to_varying (vr
);
2289 cmp
= compare_values (min
, max
);
2290 if (cmp
== -2 || cmp
== 1)
2292 /* If the new range has its limits swapped around (MIN > MAX),
2293 then the operation caused one of them to wrap around, mark
2294 the new range VARYING. */
2295 set_value_range_to_varying (vr
);
2298 set_value_range (vr
, type
, min
, max
, NULL
);
2301 /* Extract range information from a unary operation CODE based on
2302 the range of its operand *VR0 with type OP0_TYPE with resulting type TYPE.
2303 The resulting range is stored in *VR. */
2306 extract_range_from_unary_expr (value_range
*vr
,
2307 enum tree_code code
, tree type
,
2308 value_range
*vr0_
, tree op0_type
)
2310 value_range vr0
= *vr0_
, vrtem0
= VR_INITIALIZER
, vrtem1
= VR_INITIALIZER
;
2312 /* VRP only operates on integral and pointer types. */
2313 if (!(INTEGRAL_TYPE_P (op0_type
)
2314 || POINTER_TYPE_P (op0_type
))
2315 || !(INTEGRAL_TYPE_P (type
)
2316 || POINTER_TYPE_P (type
)))
2318 set_value_range_to_varying (vr
);
2322 /* If VR0 is UNDEFINED, so is the result. */
2323 if (vr0
.type
== VR_UNDEFINED
)
2325 set_value_range_to_undefined (vr
);
2329 /* Handle operations that we express in terms of others. */
2330 if (code
== PAREN_EXPR
|| code
== OBJ_TYPE_REF
)
2332 /* PAREN_EXPR and OBJ_TYPE_REF are simple copies. */
2333 copy_value_range (vr
, &vr0
);
2336 else if (code
== NEGATE_EXPR
)
2338 /* -X is simply 0 - X, so re-use existing code that also handles
2339 anti-ranges fine. */
2340 value_range zero
= VR_INITIALIZER
;
2341 set_value_range_to_value (&zero
, build_int_cst (type
, 0), NULL
);
2342 extract_range_from_binary_expr_1 (vr
, MINUS_EXPR
, type
, &zero
, &vr0
);
2345 else if (code
== BIT_NOT_EXPR
)
2347 /* ~X is simply -1 - X, so re-use existing code that also handles
2348 anti-ranges fine. */
2349 value_range minusone
= VR_INITIALIZER
;
2350 set_value_range_to_value (&minusone
, build_int_cst (type
, -1), NULL
);
2351 extract_range_from_binary_expr_1 (vr
, MINUS_EXPR
,
2352 type
, &minusone
, &vr0
);
2356 /* Now canonicalize anti-ranges to ranges when they are not symbolic
2357 and express op ~[] as (op []') U (op []''). */
2358 if (vr0
.type
== VR_ANTI_RANGE
2359 && ranges_from_anti_range (&vr0
, &vrtem0
, &vrtem1
))
2361 extract_range_from_unary_expr (vr
, code
, type
, &vrtem0
, op0_type
);
2362 if (vrtem1
.type
!= VR_UNDEFINED
)
2364 value_range vrres
= VR_INITIALIZER
;
2365 extract_range_from_unary_expr (&vrres
, code
, type
,
2367 vrp_meet (vr
, &vrres
);
2372 if (CONVERT_EXPR_CODE_P (code
))
2374 tree inner_type
= op0_type
;
2375 tree outer_type
= type
;
2377 /* If the expression evaluates to a pointer, we are only interested in
2378 determining if it evaluates to NULL [0, 0] or non-NULL (~[0, 0]). */
2379 if (POINTER_TYPE_P (type
))
2381 if (range_is_nonnull (&vr0
))
2382 set_value_range_to_nonnull (vr
, type
);
2383 else if (range_is_null (&vr0
))
2384 set_value_range_to_null (vr
, type
);
2386 set_value_range_to_varying (vr
);
2390 /* If VR0 is varying and we increase the type precision, assume
2391 a full range for the following transformation. */
2392 if (vr0
.type
== VR_VARYING
2393 && INTEGRAL_TYPE_P (inner_type
)
2394 && TYPE_PRECISION (inner_type
) < TYPE_PRECISION (outer_type
))
2396 vr0
.type
= VR_RANGE
;
2397 vr0
.min
= TYPE_MIN_VALUE (inner_type
);
2398 vr0
.max
= TYPE_MAX_VALUE (inner_type
);
2401 /* If VR0 is a constant range or anti-range and the conversion is
2402 not truncating we can convert the min and max values and
2403 canonicalize the resulting range. Otherwise we can do the
2404 conversion if the size of the range is less than what the
2405 precision of the target type can represent and the range is
2406 not an anti-range. */
2407 if ((vr0
.type
== VR_RANGE
2408 || vr0
.type
== VR_ANTI_RANGE
)
2409 && TREE_CODE (vr0
.min
) == INTEGER_CST
2410 && TREE_CODE (vr0
.max
) == INTEGER_CST
2411 && (TYPE_PRECISION (outer_type
) >= TYPE_PRECISION (inner_type
)
2412 || (vr0
.type
== VR_RANGE
2413 && integer_zerop (int_const_binop (RSHIFT_EXPR
,
2414 int_const_binop (MINUS_EXPR
, vr0
.max
, vr0
.min
),
2415 size_int (TYPE_PRECISION (outer_type
)))))))
2417 tree new_min
, new_max
;
2418 new_min
= force_fit_type (outer_type
, wi::to_widest (vr0
.min
),
2420 new_max
= force_fit_type (outer_type
, wi::to_widest (vr0
.max
),
2422 set_and_canonicalize_value_range (vr
, vr0
.type
,
2423 new_min
, new_max
, NULL
);
2427 set_value_range_to_varying (vr
);
2430 else if (code
== ABS_EXPR
)
2435 /* Pass through vr0 in the easy cases. */
2436 if (TYPE_UNSIGNED (type
)
2437 || value_range_nonnegative_p (&vr0
))
2439 copy_value_range (vr
, &vr0
);
2443 /* For the remaining varying or symbolic ranges we can't do anything
2445 if (vr0
.type
== VR_VARYING
2446 || symbolic_range_p (&vr0
))
2448 set_value_range_to_varying (vr
);
2452 /* -TYPE_MIN_VALUE = TYPE_MIN_VALUE with flag_wrapv so we can't get a
2454 if (!TYPE_OVERFLOW_UNDEFINED (type
)
2455 && ((vr0
.type
== VR_RANGE
2456 && vrp_val_is_min (vr0
.min
))
2457 || (vr0
.type
== VR_ANTI_RANGE
2458 && !vrp_val_is_min (vr0
.min
))))
2460 set_value_range_to_varying (vr
);
2464 /* ABS_EXPR may flip the range around, if the original range
2465 included negative values. */
2466 if (!vrp_val_is_min (vr0
.min
))
2467 min
= fold_unary_to_constant (code
, type
, vr0
.min
);
2469 min
= TYPE_MAX_VALUE (type
);
2471 if (!vrp_val_is_min (vr0
.max
))
2472 max
= fold_unary_to_constant (code
, type
, vr0
.max
);
2474 max
= TYPE_MAX_VALUE (type
);
2476 cmp
= compare_values (min
, max
);
2478 /* If a VR_ANTI_RANGEs contains zero, then we have
2479 ~[-INF, min(MIN, MAX)]. */
2480 if (vr0
.type
== VR_ANTI_RANGE
)
2482 if (range_includes_zero_p (vr0
.min
, vr0
.max
) == 1)
2484 /* Take the lower of the two values. */
2488 /* Create ~[-INF, min (abs(MIN), abs(MAX))]
2489 or ~[-INF + 1, min (abs(MIN), abs(MAX))] when
2490 flag_wrapv is set and the original anti-range doesn't include
2491 TYPE_MIN_VALUE, remember -TYPE_MIN_VALUE = TYPE_MIN_VALUE. */
2492 if (TYPE_OVERFLOW_WRAPS (type
))
2494 tree type_min_value
= TYPE_MIN_VALUE (type
);
2496 min
= (vr0
.min
!= type_min_value
2497 ? int_const_binop (PLUS_EXPR
, type_min_value
,
2498 build_int_cst (TREE_TYPE (type_min_value
), 1))
2502 min
= TYPE_MIN_VALUE (type
);
2506 /* All else has failed, so create the range [0, INF], even for
2507 flag_wrapv since TYPE_MIN_VALUE is in the original
2509 vr0
.type
= VR_RANGE
;
2510 min
= build_int_cst (type
, 0);
2511 max
= TYPE_MAX_VALUE (type
);
2515 /* If the range contains zero then we know that the minimum value in the
2516 range will be zero. */
2517 else if (range_includes_zero_p (vr0
.min
, vr0
.max
) == 1)
2521 min
= build_int_cst (type
, 0);
2525 /* If the range was reversed, swap MIN and MAX. */
2527 std::swap (min
, max
);
2530 cmp
= compare_values (min
, max
);
2531 if (cmp
== -2 || cmp
== 1)
2533 /* If the new range has its limits swapped around (MIN > MAX),
2534 then the operation caused one of them to wrap around, mark
2535 the new range VARYING. */
2536 set_value_range_to_varying (vr
);
2539 set_value_range (vr
, vr0
.type
, min
, max
, NULL
);
2543 /* For unhandled operations fall back to varying. */
2544 set_value_range_to_varying (vr
);
2548 /* Debugging dumps. */
2550 void dump_value_range (FILE *, const value_range
*);
2551 void debug_value_range (value_range
*);
2552 void dump_all_value_ranges (FILE *);
2553 void dump_vr_equiv (FILE *, bitmap
);
2554 void debug_vr_equiv (bitmap
);
2557 /* Dump value range VR to FILE. */
2560 dump_value_range (FILE *file
, const value_range
*vr
)
2563 fprintf (file
, "[]");
2564 else if (vr
->type
== VR_UNDEFINED
)
2565 fprintf (file
, "UNDEFINED");
2566 else if (vr
->type
== VR_RANGE
|| vr
->type
== VR_ANTI_RANGE
)
2568 tree type
= TREE_TYPE (vr
->min
);
2570 fprintf (file
, "%s[", (vr
->type
== VR_ANTI_RANGE
) ? "~" : "");
2572 if (INTEGRAL_TYPE_P (type
)
2573 && !TYPE_UNSIGNED (type
)
2574 && vrp_val_is_min (vr
->min
))
2575 fprintf (file
, "-INF");
2577 print_generic_expr (file
, vr
->min
);
2579 fprintf (file
, ", ");
2581 if (INTEGRAL_TYPE_P (type
)
2582 && vrp_val_is_max (vr
->max
))
2583 fprintf (file
, "+INF");
2585 print_generic_expr (file
, vr
->max
);
2587 fprintf (file
, "]");
2594 fprintf (file
, " EQUIVALENCES: { ");
2596 EXECUTE_IF_SET_IN_BITMAP (vr
->equiv
, 0, i
, bi
)
2598 print_generic_expr (file
, ssa_name (i
));
2599 fprintf (file
, " ");
2603 fprintf (file
, "} (%u elements)", c
);
2606 else if (vr
->type
== VR_VARYING
)
2607 fprintf (file
, "VARYING");
2609 fprintf (file
, "INVALID RANGE");
2613 /* Dump value range VR to stderr. */
2616 debug_value_range (value_range
*vr
)
2618 dump_value_range (stderr
, vr
);
2619 fprintf (stderr
, "\n");
2623 /* Given a COND_EXPR COND of the form 'V OP W', and an SSA name V,
2624 create a new SSA name N and return the assertion assignment
2625 'N = ASSERT_EXPR <V, V OP W>'. */
2628 build_assert_expr_for (tree cond
, tree v
)
2633 gcc_assert (TREE_CODE (v
) == SSA_NAME
2634 && COMPARISON_CLASS_P (cond
));
2636 a
= build2 (ASSERT_EXPR
, TREE_TYPE (v
), v
, cond
);
2637 assertion
= gimple_build_assign (NULL_TREE
, a
);
2639 /* The new ASSERT_EXPR, creates a new SSA name that replaces the
2640 operand of the ASSERT_EXPR. Create it so the new name and the old one
2641 are registered in the replacement table so that we can fix the SSA web
2642 after adding all the ASSERT_EXPRs. */
2643 tree new_def
= create_new_def_for (v
, assertion
, NULL
);
2644 /* Make sure we preserve abnormalness throughout an ASSERT_EXPR chain
2645 given we have to be able to fully propagate those out to re-create
2646 valid SSA when removing the asserts. */
2647 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (v
))
2648 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (new_def
) = 1;
2654 /* Return false if EXPR is a predicate expression involving floating
2658 fp_predicate (gimple
*stmt
)
2660 GIMPLE_CHECK (stmt
, GIMPLE_COND
);
2662 return FLOAT_TYPE_P (TREE_TYPE (gimple_cond_lhs (stmt
)));
2665 /* If the range of values taken by OP can be inferred after STMT executes,
2666 return the comparison code (COMP_CODE_P) and value (VAL_P) that
2667 describes the inferred range. Return true if a range could be
2671 infer_value_range (gimple
*stmt
, tree op
, tree_code
*comp_code_p
, tree
*val_p
)
2674 *comp_code_p
= ERROR_MARK
;
2676 /* Do not attempt to infer anything in names that flow through
2678 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op
))
2681 /* If STMT is the last statement of a basic block with no normal
2682 successors, there is no point inferring anything about any of its
2683 operands. We would not be able to find a proper insertion point
2684 for the assertion, anyway. */
2685 if (stmt_ends_bb_p (stmt
))
2690 FOR_EACH_EDGE (e
, ei
, gimple_bb (stmt
)->succs
)
2691 if (!(e
->flags
& (EDGE_ABNORMAL
|EDGE_EH
)))
2697 if (infer_nonnull_range (stmt
, op
))
2699 *val_p
= build_int_cst (TREE_TYPE (op
), 0);
2700 *comp_code_p
= NE_EXPR
;
2708 void dump_asserts_for (FILE *, tree
);
2709 void debug_asserts_for (tree
);
2710 void dump_all_asserts (FILE *);
2711 void debug_all_asserts (void);
2713 /* Dump all the registered assertions for NAME to FILE. */
2716 dump_asserts_for (FILE *file
, tree name
)
2720 fprintf (file
, "Assertions to be inserted for ");
2721 print_generic_expr (file
, name
);
2722 fprintf (file
, "\n");
2724 loc
= asserts_for
[SSA_NAME_VERSION (name
)];
2727 fprintf (file
, "\t");
2728 print_gimple_stmt (file
, gsi_stmt (loc
->si
), 0);
2729 fprintf (file
, "\n\tBB #%d", loc
->bb
->index
);
2732 fprintf (file
, "\n\tEDGE %d->%d", loc
->e
->src
->index
,
2733 loc
->e
->dest
->index
);
2734 dump_edge_info (file
, loc
->e
, dump_flags
, 0);
2736 fprintf (file
, "\n\tPREDICATE: ");
2737 print_generic_expr (file
, loc
->expr
);
2738 fprintf (file
, " %s ", get_tree_code_name (loc
->comp_code
));
2739 print_generic_expr (file
, loc
->val
);
2740 fprintf (file
, "\n\n");
2744 fprintf (file
, "\n");
2748 /* Dump all the registered assertions for NAME to stderr. */
2751 debug_asserts_for (tree name
)
2753 dump_asserts_for (stderr
, name
);
2757 /* Dump all the registered assertions for all the names to FILE. */
2760 dump_all_asserts (FILE *file
)
2765 fprintf (file
, "\nASSERT_EXPRs to be inserted\n\n");
2766 EXECUTE_IF_SET_IN_BITMAP (need_assert_for
, 0, i
, bi
)
2767 dump_asserts_for (file
, ssa_name (i
));
2768 fprintf (file
, "\n");
2772 /* Dump all the registered assertions for all the names to stderr. */
2775 debug_all_asserts (void)
2777 dump_all_asserts (stderr
);
2780 /* Push the assert info for NAME, EXPR, COMP_CODE and VAL to ASSERTS. */
2783 add_assert_info (vec
<assert_info
> &asserts
,
2784 tree name
, tree expr
, enum tree_code comp_code
, tree val
)
2787 info
.comp_code
= comp_code
;
2791 asserts
.safe_push (info
);
2794 /* If NAME doesn't have an ASSERT_EXPR registered for asserting
2795 'EXPR COMP_CODE VAL' at a location that dominates block BB or
2796 E->DEST, then register this location as a possible insertion point
2797 for ASSERT_EXPR <NAME, EXPR COMP_CODE VAL>.
2799 BB, E and SI provide the exact insertion point for the new
2800 ASSERT_EXPR. If BB is NULL, then the ASSERT_EXPR is to be inserted
2801 on edge E. Otherwise, if E is NULL, the ASSERT_EXPR is inserted on
2802 BB. If SI points to a COND_EXPR or a SWITCH_EXPR statement, then E
2803 must not be NULL. */
2806 register_new_assert_for (tree name
, tree expr
,
2807 enum tree_code comp_code
,
2811 gimple_stmt_iterator si
)
2813 assert_locus
*n
, *loc
, *last_loc
;
2814 basic_block dest_bb
;
2816 gcc_checking_assert (bb
== NULL
|| e
== NULL
);
2819 gcc_checking_assert (gimple_code (gsi_stmt (si
)) != GIMPLE_COND
2820 && gimple_code (gsi_stmt (si
)) != GIMPLE_SWITCH
);
2822 /* Never build an assert comparing against an integer constant with
2823 TREE_OVERFLOW set. This confuses our undefined overflow warning
2825 if (TREE_OVERFLOW_P (val
))
2826 val
= drop_tree_overflow (val
);
2828 /* The new assertion A will be inserted at BB or E. We need to
2829 determine if the new location is dominated by a previously
2830 registered location for A. If we are doing an edge insertion,
2831 assume that A will be inserted at E->DEST. Note that this is not
2834 If E is a critical edge, it will be split. But even if E is
2835 split, the new block will dominate the same set of blocks that
2838 The reverse, however, is not true, blocks dominated by E->DEST
2839 will not be dominated by the new block created to split E. So,
2840 if the insertion location is on a critical edge, we will not use
2841 the new location to move another assertion previously registered
2842 at a block dominated by E->DEST. */
2843 dest_bb
= (bb
) ? bb
: e
->dest
;
2845 /* If NAME already has an ASSERT_EXPR registered for COMP_CODE and
2846 VAL at a block dominating DEST_BB, then we don't need to insert a new
2847 one. Similarly, if the same assertion already exists at a block
2848 dominated by DEST_BB and the new location is not on a critical
2849 edge, then update the existing location for the assertion (i.e.,
2850 move the assertion up in the dominance tree).
2852 Note, this is implemented as a simple linked list because there
2853 should not be more than a handful of assertions registered per
2854 name. If this becomes a performance problem, a table hashed by
2855 COMP_CODE and VAL could be implemented. */
2856 loc
= asserts_for
[SSA_NAME_VERSION (name
)];
2860 if (loc
->comp_code
== comp_code
2862 || operand_equal_p (loc
->val
, val
, 0))
2863 && (loc
->expr
== expr
2864 || operand_equal_p (loc
->expr
, expr
, 0)))
2866 /* If E is not a critical edge and DEST_BB
2867 dominates the existing location for the assertion, move
2868 the assertion up in the dominance tree by updating its
2869 location information. */
2870 if ((e
== NULL
|| !EDGE_CRITICAL_P (e
))
2871 && dominated_by_p (CDI_DOMINATORS
, loc
->bb
, dest_bb
))
2880 /* Update the last node of the list and move to the next one. */
2885 /* If we didn't find an assertion already registered for
2886 NAME COMP_CODE VAL, add a new one at the end of the list of
2887 assertions associated with NAME. */
2888 n
= XNEW (struct assert_locus
);
2892 n
->comp_code
= comp_code
;
2900 asserts_for
[SSA_NAME_VERSION (name
)] = n
;
2902 bitmap_set_bit (need_assert_for
, SSA_NAME_VERSION (name
));
2905 /* (COND_OP0 COND_CODE COND_OP1) is a predicate which uses NAME.
2906 Extract a suitable test code and value and store them into *CODE_P and
2907 *VAL_P so the predicate is normalized to NAME *CODE_P *VAL_P.
2909 If no extraction was possible, return FALSE, otherwise return TRUE.
2911 If INVERT is true, then we invert the result stored into *CODE_P. */
2914 extract_code_and_val_from_cond_with_ops (tree name
, enum tree_code cond_code
,
2915 tree cond_op0
, tree cond_op1
,
2916 bool invert
, enum tree_code
*code_p
,
2919 enum tree_code comp_code
;
2922 /* Otherwise, we have a comparison of the form NAME COMP VAL
2923 or VAL COMP NAME. */
2924 if (name
== cond_op1
)
2926 /* If the predicate is of the form VAL COMP NAME, flip
2927 COMP around because we need to register NAME as the
2928 first operand in the predicate. */
2929 comp_code
= swap_tree_comparison (cond_code
);
2932 else if (name
== cond_op0
)
2934 /* The comparison is of the form NAME COMP VAL, so the
2935 comparison code remains unchanged. */
2936 comp_code
= cond_code
;
2942 /* Invert the comparison code as necessary. */
2944 comp_code
= invert_tree_comparison (comp_code
, 0);
2946 /* VRP only handles integral and pointer types. */
2947 if (! INTEGRAL_TYPE_P (TREE_TYPE (val
))
2948 && ! POINTER_TYPE_P (TREE_TYPE (val
)))
2951 /* Do not register always-false predicates.
2952 FIXME: this works around a limitation in fold() when dealing with
2953 enumerations. Given 'enum { N1, N2 } x;', fold will not
2954 fold 'if (x > N2)' to 'if (0)'. */
2955 if ((comp_code
== GT_EXPR
|| comp_code
== LT_EXPR
)
2956 && INTEGRAL_TYPE_P (TREE_TYPE (val
)))
2958 tree min
= TYPE_MIN_VALUE (TREE_TYPE (val
));
2959 tree max
= TYPE_MAX_VALUE (TREE_TYPE (val
));
2961 if (comp_code
== GT_EXPR
2963 || compare_values (val
, max
) == 0))
2966 if (comp_code
== LT_EXPR
2968 || compare_values (val
, min
) == 0))
2971 *code_p
= comp_code
;
2976 /* Find out smallest RES where RES > VAL && (RES & MASK) == RES, if any
2977 (otherwise return VAL). VAL and MASK must be zero-extended for
2978 precision PREC. If SGNBIT is non-zero, first xor VAL with SGNBIT
2979 (to transform signed values into unsigned) and at the end xor
2983 masked_increment (const wide_int
&val_in
, const wide_int
&mask
,
2984 const wide_int
&sgnbit
, unsigned int prec
)
2986 wide_int bit
= wi::one (prec
), res
;
2989 wide_int val
= val_in
^ sgnbit
;
2990 for (i
= 0; i
< prec
; i
++, bit
+= bit
)
2993 if ((res
& bit
) == 0)
2996 res
= wi::bit_and_not (val
+ bit
, res
);
2998 if (wi::gtu_p (res
, val
))
2999 return res
^ sgnbit
;
3001 return val
^ sgnbit
;
3004 /* Helper for overflow_comparison_p
3006 OP0 CODE OP1 is a comparison. Examine the comparison and potentially
3007 OP1's defining statement to see if it ultimately has the form
3008 OP0 CODE (OP0 PLUS INTEGER_CST)
3010 If so, return TRUE indicating this is an overflow test and store into
3011 *NEW_CST an updated constant that can be used in a narrowed range test.
3013 REVERSED indicates if the comparison was originally:
3017 This affects how we build the updated constant. */
3020 overflow_comparison_p_1 (enum tree_code code
, tree op0
, tree op1
,
3021 bool follow_assert_exprs
, bool reversed
, tree
*new_cst
)
3023 /* See if this is a relational operation between two SSA_NAMES with
3024 unsigned, overflow wrapping values. If so, check it more deeply. */
3025 if ((code
== LT_EXPR
|| code
== LE_EXPR
3026 || code
== GE_EXPR
|| code
== GT_EXPR
)
3027 && TREE_CODE (op0
) == SSA_NAME
3028 && TREE_CODE (op1
) == SSA_NAME
3029 && INTEGRAL_TYPE_P (TREE_TYPE (op0
))
3030 && TYPE_UNSIGNED (TREE_TYPE (op0
))
3031 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (op0
)))
3033 gimple
*op1_def
= SSA_NAME_DEF_STMT (op1
);
3035 /* If requested, follow any ASSERT_EXPRs backwards for OP1. */
3036 if (follow_assert_exprs
)
3038 while (gimple_assign_single_p (op1_def
)
3039 && TREE_CODE (gimple_assign_rhs1 (op1_def
)) == ASSERT_EXPR
)
3041 op1
= TREE_OPERAND (gimple_assign_rhs1 (op1_def
), 0);
3042 if (TREE_CODE (op1
) != SSA_NAME
)
3044 op1_def
= SSA_NAME_DEF_STMT (op1
);
3048 /* Now look at the defining statement of OP1 to see if it adds
3049 or subtracts a nonzero constant from another operand. */
3051 && is_gimple_assign (op1_def
)
3052 && gimple_assign_rhs_code (op1_def
) == PLUS_EXPR
3053 && TREE_CODE (gimple_assign_rhs2 (op1_def
)) == INTEGER_CST
3054 && !integer_zerop (gimple_assign_rhs2 (op1_def
)))
3056 tree target
= gimple_assign_rhs1 (op1_def
);
3058 /* If requested, follow ASSERT_EXPRs backwards for op0 looking
3059 for one where TARGET appears on the RHS. */
3060 if (follow_assert_exprs
)
3062 /* Now see if that "other operand" is op0, following the chain
3063 of ASSERT_EXPRs if necessary. */
3064 gimple
*op0_def
= SSA_NAME_DEF_STMT (op0
);
3065 while (op0
!= target
3066 && gimple_assign_single_p (op0_def
)
3067 && TREE_CODE (gimple_assign_rhs1 (op0_def
)) == ASSERT_EXPR
)
3069 op0
= TREE_OPERAND (gimple_assign_rhs1 (op0_def
), 0);
3070 if (TREE_CODE (op0
) != SSA_NAME
)
3072 op0_def
= SSA_NAME_DEF_STMT (op0
);
3076 /* If we did not find our target SSA_NAME, then this is not
3077 an overflow test. */
3081 tree type
= TREE_TYPE (op0
);
3082 wide_int max
= wi::max_value (TYPE_PRECISION (type
), UNSIGNED
);
3083 tree inc
= gimple_assign_rhs2 (op1_def
);
3085 *new_cst
= wide_int_to_tree (type
, max
+ wi::to_wide (inc
));
3087 *new_cst
= wide_int_to_tree (type
, max
- wi::to_wide (inc
));
3094 /* OP0 CODE OP1 is a comparison. Examine the comparison and potentially
3095 OP1's defining statement to see if it ultimately has the form
3096 OP0 CODE (OP0 PLUS INTEGER_CST)
3098 If so, return TRUE indicating this is an overflow test and store into
3099 *NEW_CST an updated constant that can be used in a narrowed range test.
3101 These statements are left as-is in the IL to facilitate discovery of
3102 {ADD,SUB}_OVERFLOW sequences later in the optimizer pipeline. But
3103 the alternate range representation is often useful within VRP. */
3106 overflow_comparison_p (tree_code code
, tree name
, tree val
,
3107 bool use_equiv_p
, tree
*new_cst
)
3109 if (overflow_comparison_p_1 (code
, name
, val
, use_equiv_p
, false, new_cst
))
3111 return overflow_comparison_p_1 (swap_tree_comparison (code
), val
, name
,
3112 use_equiv_p
, true, new_cst
);
3116 /* Try to register an edge assertion for SSA name NAME on edge E for
3117 the condition COND contributing to the conditional jump pointed to by BSI.
3118 Invert the condition COND if INVERT is true. */
3121 register_edge_assert_for_2 (tree name
, edge e
,
3122 enum tree_code cond_code
,
3123 tree cond_op0
, tree cond_op1
, bool invert
,
3124 vec
<assert_info
> &asserts
)
3127 enum tree_code comp_code
;
3129 if (!extract_code_and_val_from_cond_with_ops (name
, cond_code
,
3132 invert
, &comp_code
, &val
))
3135 /* Queue the assert. */
3137 if (overflow_comparison_p (comp_code
, name
, val
, false, &x
))
3139 enum tree_code new_code
= ((comp_code
== GT_EXPR
|| comp_code
== GE_EXPR
)
3140 ? GT_EXPR
: LE_EXPR
);
3141 add_assert_info (asserts
, name
, name
, new_code
, x
);
3143 add_assert_info (asserts
, name
, name
, comp_code
, val
);
3145 /* In the case of NAME <= CST and NAME being defined as
3146 NAME = (unsigned) NAME2 + CST2 we can assert NAME2 >= -CST2
3147 and NAME2 <= CST - CST2. We can do the same for NAME > CST.
3148 This catches range and anti-range tests. */
3149 if ((comp_code
== LE_EXPR
3150 || comp_code
== GT_EXPR
)
3151 && TREE_CODE (val
) == INTEGER_CST
3152 && TYPE_UNSIGNED (TREE_TYPE (val
)))
3154 gimple
*def_stmt
= SSA_NAME_DEF_STMT (name
);
3155 tree cst2
= NULL_TREE
, name2
= NULL_TREE
, name3
= NULL_TREE
;
3157 /* Extract CST2 from the (optional) addition. */
3158 if (is_gimple_assign (def_stmt
)
3159 && gimple_assign_rhs_code (def_stmt
) == PLUS_EXPR
)
3161 name2
= gimple_assign_rhs1 (def_stmt
);
3162 cst2
= gimple_assign_rhs2 (def_stmt
);
3163 if (TREE_CODE (name2
) == SSA_NAME
3164 && TREE_CODE (cst2
) == INTEGER_CST
)
3165 def_stmt
= SSA_NAME_DEF_STMT (name2
);
3168 /* Extract NAME2 from the (optional) sign-changing cast. */
3169 if (gimple_assign_cast_p (def_stmt
))
3171 if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt
))
3172 && ! TYPE_UNSIGNED (TREE_TYPE (gimple_assign_rhs1 (def_stmt
)))
3173 && (TYPE_PRECISION (gimple_expr_type (def_stmt
))
3174 == TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (def_stmt
)))))
3175 name3
= gimple_assign_rhs1 (def_stmt
);
3178 /* If name3 is used later, create an ASSERT_EXPR for it. */
3179 if (name3
!= NULL_TREE
3180 && TREE_CODE (name3
) == SSA_NAME
3181 && (cst2
== NULL_TREE
3182 || TREE_CODE (cst2
) == INTEGER_CST
)
3183 && INTEGRAL_TYPE_P (TREE_TYPE (name3
)))
3187 /* Build an expression for the range test. */
3188 tmp
= build1 (NOP_EXPR
, TREE_TYPE (name
), name3
);
3189 if (cst2
!= NULL_TREE
)
3190 tmp
= build2 (PLUS_EXPR
, TREE_TYPE (name
), tmp
, cst2
);
3194 fprintf (dump_file
, "Adding assert for ");
3195 print_generic_expr (dump_file
, name3
);
3196 fprintf (dump_file
, " from ");
3197 print_generic_expr (dump_file
, tmp
);
3198 fprintf (dump_file
, "\n");
3201 add_assert_info (asserts
, name3
, tmp
, comp_code
, val
);
3204 /* If name2 is used later, create an ASSERT_EXPR for it. */
3205 if (name2
!= NULL_TREE
3206 && TREE_CODE (name2
) == SSA_NAME
3207 && TREE_CODE (cst2
) == INTEGER_CST
3208 && INTEGRAL_TYPE_P (TREE_TYPE (name2
)))
3212 /* Build an expression for the range test. */
3214 if (TREE_TYPE (name
) != TREE_TYPE (name2
))
3215 tmp
= build1 (NOP_EXPR
, TREE_TYPE (name
), tmp
);
3216 if (cst2
!= NULL_TREE
)
3217 tmp
= build2 (PLUS_EXPR
, TREE_TYPE (name
), tmp
, cst2
);
3221 fprintf (dump_file
, "Adding assert for ");
3222 print_generic_expr (dump_file
, name2
);
3223 fprintf (dump_file
, " from ");
3224 print_generic_expr (dump_file
, tmp
);
3225 fprintf (dump_file
, "\n");
3228 add_assert_info (asserts
, name2
, tmp
, comp_code
, val
);
3232 /* In the case of post-in/decrement tests like if (i++) ... and uses
3233 of the in/decremented value on the edge the extra name we want to
3234 assert for is not on the def chain of the name compared. Instead
3235 it is in the set of use stmts.
3236 Similar cases happen for conversions that were simplified through
3237 fold_{sign_changed,widened}_comparison. */
3238 if ((comp_code
== NE_EXPR
3239 || comp_code
== EQ_EXPR
)
3240 && TREE_CODE (val
) == INTEGER_CST
)
3242 imm_use_iterator ui
;
3244 FOR_EACH_IMM_USE_STMT (use_stmt
, ui
, name
)
3246 if (!is_gimple_assign (use_stmt
))
3249 /* Cut off to use-stmts that are dominating the predecessor. */
3250 if (!dominated_by_p (CDI_DOMINATORS
, e
->src
, gimple_bb (use_stmt
)))
3253 tree name2
= gimple_assign_lhs (use_stmt
);
3254 if (TREE_CODE (name2
) != SSA_NAME
)
3257 enum tree_code code
= gimple_assign_rhs_code (use_stmt
);
3259 if (code
== PLUS_EXPR
3260 || code
== MINUS_EXPR
)
3262 cst
= gimple_assign_rhs2 (use_stmt
);
3263 if (TREE_CODE (cst
) != INTEGER_CST
)
3265 cst
= int_const_binop (code
, val
, cst
);
3267 else if (CONVERT_EXPR_CODE_P (code
))
3269 /* For truncating conversions we cannot record
3271 if (comp_code
== NE_EXPR
3272 && (TYPE_PRECISION (TREE_TYPE (name2
))
3273 < TYPE_PRECISION (TREE_TYPE (name
))))
3275 cst
= fold_convert (TREE_TYPE (name2
), val
);
3280 if (TREE_OVERFLOW_P (cst
))
3281 cst
= drop_tree_overflow (cst
);
3282 add_assert_info (asserts
, name2
, name2
, comp_code
, cst
);
3286 if (TREE_CODE_CLASS (comp_code
) == tcc_comparison
3287 && TREE_CODE (val
) == INTEGER_CST
)
3289 gimple
*def_stmt
= SSA_NAME_DEF_STMT (name
);
3290 tree name2
= NULL_TREE
, names
[2], cst2
= NULL_TREE
;
3291 tree val2
= NULL_TREE
;
3292 unsigned int prec
= TYPE_PRECISION (TREE_TYPE (val
));
3293 wide_int mask
= wi::zero (prec
);
3294 unsigned int nprec
= prec
;
3295 enum tree_code rhs_code
= ERROR_MARK
;
3297 if (is_gimple_assign (def_stmt
))
3298 rhs_code
= gimple_assign_rhs_code (def_stmt
);
3300 /* In the case of NAME != CST1 where NAME = A +- CST2 we can
3301 assert that A != CST1 -+ CST2. */
3302 if ((comp_code
== EQ_EXPR
|| comp_code
== NE_EXPR
)
3303 && (rhs_code
== PLUS_EXPR
|| rhs_code
== MINUS_EXPR
))
3305 tree op0
= gimple_assign_rhs1 (def_stmt
);
3306 tree op1
= gimple_assign_rhs2 (def_stmt
);
3307 if (TREE_CODE (op0
) == SSA_NAME
3308 && TREE_CODE (op1
) == INTEGER_CST
)
3310 enum tree_code reverse_op
= (rhs_code
== PLUS_EXPR
3311 ? MINUS_EXPR
: PLUS_EXPR
);
3312 op1
= int_const_binop (reverse_op
, val
, op1
);
3313 if (TREE_OVERFLOW (op1
))
3314 op1
= drop_tree_overflow (op1
);
3315 add_assert_info (asserts
, op0
, op0
, comp_code
, op1
);
3319 /* Add asserts for NAME cmp CST and NAME being defined
3320 as NAME = (int) NAME2. */
3321 if (!TYPE_UNSIGNED (TREE_TYPE (val
))
3322 && (comp_code
== LE_EXPR
|| comp_code
== LT_EXPR
3323 || comp_code
== GT_EXPR
|| comp_code
== GE_EXPR
)
3324 && gimple_assign_cast_p (def_stmt
))
3326 name2
= gimple_assign_rhs1 (def_stmt
);
3327 if (CONVERT_EXPR_CODE_P (rhs_code
)
3328 && INTEGRAL_TYPE_P (TREE_TYPE (name2
))
3329 && TYPE_UNSIGNED (TREE_TYPE (name2
))
3330 && prec
== TYPE_PRECISION (TREE_TYPE (name2
))
3331 && (comp_code
== LE_EXPR
|| comp_code
== GT_EXPR
3332 || !tree_int_cst_equal (val
,
3333 TYPE_MIN_VALUE (TREE_TYPE (val
)))))
3336 enum tree_code new_comp_code
= comp_code
;
3338 cst
= fold_convert (TREE_TYPE (name2
),
3339 TYPE_MIN_VALUE (TREE_TYPE (val
)));
3340 /* Build an expression for the range test. */
3341 tmp
= build2 (PLUS_EXPR
, TREE_TYPE (name2
), name2
, cst
);
3342 cst
= fold_build2 (PLUS_EXPR
, TREE_TYPE (name2
), cst
,
3343 fold_convert (TREE_TYPE (name2
), val
));
3344 if (comp_code
== LT_EXPR
|| comp_code
== GE_EXPR
)
3346 new_comp_code
= comp_code
== LT_EXPR
? LE_EXPR
: GT_EXPR
;
3347 cst
= fold_build2 (MINUS_EXPR
, TREE_TYPE (name2
), cst
,
3348 build_int_cst (TREE_TYPE (name2
), 1));
3353 fprintf (dump_file
, "Adding assert for ");
3354 print_generic_expr (dump_file
, name2
);
3355 fprintf (dump_file
, " from ");
3356 print_generic_expr (dump_file
, tmp
);
3357 fprintf (dump_file
, "\n");
3360 add_assert_info (asserts
, name2
, tmp
, new_comp_code
, cst
);
3364 /* Add asserts for NAME cmp CST and NAME being defined as
3365 NAME = NAME2 >> CST2.
3367 Extract CST2 from the right shift. */
3368 if (rhs_code
== RSHIFT_EXPR
)
3370 name2
= gimple_assign_rhs1 (def_stmt
);
3371 cst2
= gimple_assign_rhs2 (def_stmt
);
3372 if (TREE_CODE (name2
) == SSA_NAME
3373 && tree_fits_uhwi_p (cst2
)
3374 && INTEGRAL_TYPE_P (TREE_TYPE (name2
))
3375 && IN_RANGE (tree_to_uhwi (cst2
), 1, prec
- 1)
3376 && type_has_mode_precision_p (TREE_TYPE (val
)))
3378 mask
= wi::mask (tree_to_uhwi (cst2
), false, prec
);
3379 val2
= fold_binary (LSHIFT_EXPR
, TREE_TYPE (val
), val
, cst2
);
3382 if (val2
!= NULL_TREE
3383 && TREE_CODE (val2
) == INTEGER_CST
3384 && simple_cst_equal (fold_build2 (RSHIFT_EXPR
,
3388 enum tree_code new_comp_code
= comp_code
;
3392 if (comp_code
== EQ_EXPR
|| comp_code
== NE_EXPR
)
3394 if (!TYPE_UNSIGNED (TREE_TYPE (val
)))
3396 tree type
= build_nonstandard_integer_type (prec
, 1);
3397 tmp
= build1 (NOP_EXPR
, type
, name2
);
3398 val2
= fold_convert (type
, val2
);
3400 tmp
= fold_build2 (MINUS_EXPR
, TREE_TYPE (tmp
), tmp
, val2
);
3401 new_val
= wide_int_to_tree (TREE_TYPE (tmp
), mask
);
3402 new_comp_code
= comp_code
== EQ_EXPR
? LE_EXPR
: GT_EXPR
;
3404 else if (comp_code
== LT_EXPR
|| comp_code
== GE_EXPR
)
3407 = wi::min_value (prec
, TYPE_SIGN (TREE_TYPE (val
)));
3409 if (minval
== wi::to_wide (new_val
))
3410 new_val
= NULL_TREE
;
3415 = wi::max_value (prec
, TYPE_SIGN (TREE_TYPE (val
)));
3416 mask
|= wi::to_wide (val2
);
3417 if (wi::eq_p (mask
, maxval
))
3418 new_val
= NULL_TREE
;
3420 new_val
= wide_int_to_tree (TREE_TYPE (val2
), mask
);
3427 fprintf (dump_file
, "Adding assert for ");
3428 print_generic_expr (dump_file
, name2
);
3429 fprintf (dump_file
, " from ");
3430 print_generic_expr (dump_file
, tmp
);
3431 fprintf (dump_file
, "\n");
3434 add_assert_info (asserts
, name2
, tmp
, new_comp_code
, new_val
);
3438 /* Add asserts for NAME cmp CST and NAME being defined as
3439 NAME = NAME2 & CST2.
3441 Extract CST2 from the and.
3444 NAME = (unsigned) NAME2;
3445 casts where NAME's type is unsigned and has smaller precision
3446 than NAME2's type as if it was NAME = NAME2 & MASK. */
3447 names
[0] = NULL_TREE
;
3448 names
[1] = NULL_TREE
;
3450 if (rhs_code
== BIT_AND_EXPR
3451 || (CONVERT_EXPR_CODE_P (rhs_code
)
3452 && INTEGRAL_TYPE_P (TREE_TYPE (val
))
3453 && TYPE_UNSIGNED (TREE_TYPE (val
))
3454 && TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (def_stmt
)))
3457 name2
= gimple_assign_rhs1 (def_stmt
);
3458 if (rhs_code
== BIT_AND_EXPR
)
3459 cst2
= gimple_assign_rhs2 (def_stmt
);
3462 cst2
= TYPE_MAX_VALUE (TREE_TYPE (val
));
3463 nprec
= TYPE_PRECISION (TREE_TYPE (name2
));
3465 if (TREE_CODE (name2
) == SSA_NAME
3466 && INTEGRAL_TYPE_P (TREE_TYPE (name2
))
3467 && TREE_CODE (cst2
) == INTEGER_CST
3468 && !integer_zerop (cst2
)
3470 || TYPE_UNSIGNED (TREE_TYPE (val
))))
3472 gimple
*def_stmt2
= SSA_NAME_DEF_STMT (name2
);
3473 if (gimple_assign_cast_p (def_stmt2
))
3475 names
[1] = gimple_assign_rhs1 (def_stmt2
);
3476 if (!CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt2
))
3477 || !INTEGRAL_TYPE_P (TREE_TYPE (names
[1]))
3478 || (TYPE_PRECISION (TREE_TYPE (name2
))
3479 != TYPE_PRECISION (TREE_TYPE (names
[1]))))
3480 names
[1] = NULL_TREE
;
3485 if (names
[0] || names
[1])
3487 wide_int minv
, maxv
, valv
, cst2v
;
3488 wide_int tem
, sgnbit
;
3489 bool valid_p
= false, valn
, cst2n
;
3490 enum tree_code ccode
= comp_code
;
3492 valv
= wide_int::from (wi::to_wide (val
), nprec
, UNSIGNED
);
3493 cst2v
= wide_int::from (wi::to_wide (cst2
), nprec
, UNSIGNED
);
3494 valn
= wi::neg_p (valv
, TYPE_SIGN (TREE_TYPE (val
)));
3495 cst2n
= wi::neg_p (cst2v
, TYPE_SIGN (TREE_TYPE (val
)));
3496 /* If CST2 doesn't have most significant bit set,
3497 but VAL is negative, we have comparison like
3498 if ((x & 0x123) > -4) (always true). Just give up. */
3502 sgnbit
= wi::set_bit_in_zero (nprec
- 1, nprec
);
3504 sgnbit
= wi::zero (nprec
);
3505 minv
= valv
& cst2v
;
3509 /* Minimum unsigned value for equality is VAL & CST2
3510 (should be equal to VAL, otherwise we probably should
3511 have folded the comparison into false) and
3512 maximum unsigned value is VAL | ~CST2. */
3513 maxv
= valv
| ~cst2v
;
3518 tem
= valv
| ~cst2v
;
3519 /* If VAL is 0, handle (X & CST2) != 0 as (X & CST2) > 0U. */
3523 sgnbit
= wi::zero (nprec
);
3526 /* If (VAL | ~CST2) is all ones, handle it as
3527 (X & CST2) < VAL. */
3532 sgnbit
= wi::zero (nprec
);
3535 if (!cst2n
&& wi::neg_p (cst2v
))
3536 sgnbit
= wi::set_bit_in_zero (nprec
- 1, nprec
);
3545 if (tem
== wi::mask (nprec
- 1, false, nprec
))
3551 sgnbit
= wi::zero (nprec
);
3556 /* Minimum unsigned value for >= if (VAL & CST2) == VAL
3557 is VAL and maximum unsigned value is ~0. For signed
3558 comparison, if CST2 doesn't have most significant bit
3559 set, handle it similarly. If CST2 has MSB set,
3560 the minimum is the same, and maximum is ~0U/2. */
3563 /* If (VAL & CST2) != VAL, X & CST2 can't be equal to
3565 minv
= masked_increment (valv
, cst2v
, sgnbit
, nprec
);
3569 maxv
= wi::mask (nprec
- (cst2n
? 1 : 0), false, nprec
);
3575 /* Find out smallest MINV where MINV > VAL
3576 && (MINV & CST2) == MINV, if any. If VAL is signed and
3577 CST2 has MSB set, compute it biased by 1 << (nprec - 1). */
3578 minv
= masked_increment (valv
, cst2v
, sgnbit
, nprec
);
3581 maxv
= wi::mask (nprec
- (cst2n
? 1 : 0), false, nprec
);
3586 /* Minimum unsigned value for <= is 0 and maximum
3587 unsigned value is VAL | ~CST2 if (VAL & CST2) == VAL.
3588 Otherwise, find smallest VAL2 where VAL2 > VAL
3589 && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
3591 For signed comparison, if CST2 doesn't have most
3592 significant bit set, handle it similarly. If CST2 has
3593 MSB set, the maximum is the same and minimum is INT_MIN. */
3598 maxv
= masked_increment (valv
, cst2v
, sgnbit
, nprec
);
3610 /* Minimum unsigned value for < is 0 and maximum
3611 unsigned value is (VAL-1) | ~CST2 if (VAL & CST2) == VAL.
3612 Otherwise, find smallest VAL2 where VAL2 > VAL
3613 && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
3615 For signed comparison, if CST2 doesn't have most
3616 significant bit set, handle it similarly. If CST2 has
3617 MSB set, the maximum is the same and minimum is INT_MIN. */
3626 maxv
= masked_increment (valv
, cst2v
, sgnbit
, nprec
);
3640 && (maxv
- minv
) != -1)
3642 tree tmp
, new_val
, type
;
3645 for (i
= 0; i
< 2; i
++)
3648 wide_int maxv2
= maxv
;
3650 type
= TREE_TYPE (names
[i
]);
3651 if (!TYPE_UNSIGNED (type
))
3653 type
= build_nonstandard_integer_type (nprec
, 1);
3654 tmp
= build1 (NOP_EXPR
, type
, names
[i
]);
3658 tmp
= build2 (PLUS_EXPR
, type
, tmp
,
3659 wide_int_to_tree (type
, -minv
));
3660 maxv2
= maxv
- minv
;
3662 new_val
= wide_int_to_tree (type
, maxv2
);
3666 fprintf (dump_file
, "Adding assert for ");
3667 print_generic_expr (dump_file
, names
[i
]);
3668 fprintf (dump_file
, " from ");
3669 print_generic_expr (dump_file
, tmp
);
3670 fprintf (dump_file
, "\n");
3673 add_assert_info (asserts
, names
[i
], tmp
, LE_EXPR
, new_val
);
3680 /* OP is an operand of a truth value expression which is known to have
3681 a particular value. Register any asserts for OP and for any
3682 operands in OP's defining statement.
3684 If CODE is EQ_EXPR, then we want to register OP is zero (false),
3685 if CODE is NE_EXPR, then we want to register OP is nonzero (true). */
3688 register_edge_assert_for_1 (tree op
, enum tree_code code
,
3689 edge e
, vec
<assert_info
> &asserts
)
3693 enum tree_code rhs_code
;
3695 /* We only care about SSA_NAMEs. */
3696 if (TREE_CODE (op
) != SSA_NAME
)
3699 /* We know that OP will have a zero or nonzero value. */
3700 val
= build_int_cst (TREE_TYPE (op
), 0);
3701 add_assert_info (asserts
, op
, op
, code
, val
);
3703 /* Now look at how OP is set. If it's set from a comparison,
3704 a truth operation or some bit operations, then we may be able
3705 to register information about the operands of that assignment. */
3706 op_def
= SSA_NAME_DEF_STMT (op
);
3707 if (gimple_code (op_def
) != GIMPLE_ASSIGN
)
3710 rhs_code
= gimple_assign_rhs_code (op_def
);
3712 if (TREE_CODE_CLASS (rhs_code
) == tcc_comparison
)
3714 bool invert
= (code
== EQ_EXPR
? true : false);
3715 tree op0
= gimple_assign_rhs1 (op_def
);
3716 tree op1
= gimple_assign_rhs2 (op_def
);
3718 if (TREE_CODE (op0
) == SSA_NAME
)
3719 register_edge_assert_for_2 (op0
, e
, rhs_code
, op0
, op1
, invert
, asserts
);
3720 if (TREE_CODE (op1
) == SSA_NAME
)
3721 register_edge_assert_for_2 (op1
, e
, rhs_code
, op0
, op1
, invert
, asserts
);
3723 else if ((code
== NE_EXPR
3724 && gimple_assign_rhs_code (op_def
) == BIT_AND_EXPR
)
3726 && gimple_assign_rhs_code (op_def
) == BIT_IOR_EXPR
))
3728 /* Recurse on each operand. */
3729 tree op0
= gimple_assign_rhs1 (op_def
);
3730 tree op1
= gimple_assign_rhs2 (op_def
);
3731 if (TREE_CODE (op0
) == SSA_NAME
3732 && has_single_use (op0
))
3733 register_edge_assert_for_1 (op0
, code
, e
, asserts
);
3734 if (TREE_CODE (op1
) == SSA_NAME
3735 && has_single_use (op1
))
3736 register_edge_assert_for_1 (op1
, code
, e
, asserts
);
3738 else if (gimple_assign_rhs_code (op_def
) == BIT_NOT_EXPR
3739 && TYPE_PRECISION (TREE_TYPE (gimple_assign_lhs (op_def
))) == 1)
3741 /* Recurse, flipping CODE. */
3742 code
= invert_tree_comparison (code
, false);
3743 register_edge_assert_for_1 (gimple_assign_rhs1 (op_def
), code
, e
, asserts
);
3745 else if (gimple_assign_rhs_code (op_def
) == SSA_NAME
)
3747 /* Recurse through the copy. */
3748 register_edge_assert_for_1 (gimple_assign_rhs1 (op_def
), code
, e
, asserts
);
3750 else if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (op_def
)))
3752 /* Recurse through the type conversion, unless it is a narrowing
3753 conversion or conversion from non-integral type. */
3754 tree rhs
= gimple_assign_rhs1 (op_def
);
3755 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs
))
3756 && (TYPE_PRECISION (TREE_TYPE (rhs
))
3757 <= TYPE_PRECISION (TREE_TYPE (op
))))
3758 register_edge_assert_for_1 (rhs
, code
, e
, asserts
);
3762 /* Check if comparison
3763 NAME COND_OP INTEGER_CST
3765 (X & 11...100..0) COND_OP XX...X00...0
3766 Such comparison can yield assertions like
3769 in case of COND_OP being NE_EXPR or
3772 in case of EQ_EXPR. */
3775 is_masked_range_test (tree name
, tree valt
, enum tree_code cond_code
,
3776 tree
*new_name
, tree
*low
, enum tree_code
*low_code
,
3777 tree
*high
, enum tree_code
*high_code
)
3779 gimple
*def_stmt
= SSA_NAME_DEF_STMT (name
);
3781 if (!is_gimple_assign (def_stmt
)
3782 || gimple_assign_rhs_code (def_stmt
) != BIT_AND_EXPR
)
3785 tree t
= gimple_assign_rhs1 (def_stmt
);
3786 tree maskt
= gimple_assign_rhs2 (def_stmt
);
3787 if (TREE_CODE (t
) != SSA_NAME
|| TREE_CODE (maskt
) != INTEGER_CST
)
3790 wi::tree_to_wide_ref mask
= wi::to_wide (maskt
);
3791 wide_int inv_mask
= ~mask
;
3792 /* Assume VALT is INTEGER_CST. */
3793 wi::tree_to_wide_ref val
= wi::to_wide (valt
);
3795 if ((inv_mask
& (inv_mask
+ 1)) != 0
3796 || (val
& mask
) != val
)
3799 bool is_range
= cond_code
== EQ_EXPR
;
3801 tree type
= TREE_TYPE (t
);
3802 wide_int min
= wi::min_value (type
),
3803 max
= wi::max_value (type
);
3807 *low_code
= val
== min
? ERROR_MARK
: GE_EXPR
;
3808 *high_code
= val
== max
? ERROR_MARK
: LE_EXPR
;
3812 /* We can still generate assertion if one of alternatives
3813 is known to always be false. */
3816 *low_code
= (enum tree_code
) 0;
3817 *high_code
= GT_EXPR
;
3819 else if ((val
| inv_mask
) == max
)
3821 *low_code
= LT_EXPR
;
3822 *high_code
= (enum tree_code
) 0;
3829 *low
= wide_int_to_tree (type
, val
);
3830 *high
= wide_int_to_tree (type
, val
| inv_mask
);
3832 if (wi::neg_p (val
, TYPE_SIGN (type
)))
3833 std::swap (*low
, *high
);
3838 /* Try to register an edge assertion for SSA name NAME on edge E for
3839 the condition COND contributing to the conditional jump pointed to by
3843 register_edge_assert_for (tree name
, edge e
,
3844 enum tree_code cond_code
, tree cond_op0
,
3845 tree cond_op1
, vec
<assert_info
> &asserts
)
3848 enum tree_code comp_code
;
3849 bool is_else_edge
= (e
->flags
& EDGE_FALSE_VALUE
) != 0;
3851 /* Do not attempt to infer anything in names that flow through
3853 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name
))
3856 if (!extract_code_and_val_from_cond_with_ops (name
, cond_code
,
3862 /* Register ASSERT_EXPRs for name. */
3863 register_edge_assert_for_2 (name
, e
, cond_code
, cond_op0
,
3864 cond_op1
, is_else_edge
, asserts
);
3867 /* If COND is effectively an equality test of an SSA_NAME against
3868 the value zero or one, then we may be able to assert values
3869 for SSA_NAMEs which flow into COND. */
3871 /* In the case of NAME == 1 or NAME != 0, for BIT_AND_EXPR defining
3872 statement of NAME we can assert both operands of the BIT_AND_EXPR
3873 have nonzero value. */
3874 if (((comp_code
== EQ_EXPR
&& integer_onep (val
))
3875 || (comp_code
== NE_EXPR
&& integer_zerop (val
))))
3877 gimple
*def_stmt
= SSA_NAME_DEF_STMT (name
);
3879 if (is_gimple_assign (def_stmt
)
3880 && gimple_assign_rhs_code (def_stmt
) == BIT_AND_EXPR
)
3882 tree op0
= gimple_assign_rhs1 (def_stmt
);
3883 tree op1
= gimple_assign_rhs2 (def_stmt
);
3884 register_edge_assert_for_1 (op0
, NE_EXPR
, e
, asserts
);
3885 register_edge_assert_for_1 (op1
, NE_EXPR
, e
, asserts
);
3889 /* In the case of NAME == 0 or NAME != 1, for BIT_IOR_EXPR defining
3890 statement of NAME we can assert both operands of the BIT_IOR_EXPR
3892 if (((comp_code
== EQ_EXPR
&& integer_zerop (val
))
3893 || (comp_code
== NE_EXPR
&& integer_onep (val
))))
3895 gimple
*def_stmt
= SSA_NAME_DEF_STMT (name
);
3897 /* For BIT_IOR_EXPR only if NAME == 0 both operands have
3898 necessarily zero value, or if type-precision is one. */
3899 if (is_gimple_assign (def_stmt
)
3900 && (gimple_assign_rhs_code (def_stmt
) == BIT_IOR_EXPR
3901 && (TYPE_PRECISION (TREE_TYPE (name
)) == 1
3902 || comp_code
== EQ_EXPR
)))
3904 tree op0
= gimple_assign_rhs1 (def_stmt
);
3905 tree op1
= gimple_assign_rhs2 (def_stmt
);
3906 register_edge_assert_for_1 (op0
, EQ_EXPR
, e
, asserts
);
3907 register_edge_assert_for_1 (op1
, EQ_EXPR
, e
, asserts
);
3911 /* Sometimes we can infer ranges from (NAME & MASK) == VALUE. */
3912 if ((comp_code
== EQ_EXPR
|| comp_code
== NE_EXPR
)
3913 && TREE_CODE (val
) == INTEGER_CST
)
3915 enum tree_code low_code
, high_code
;
3917 if (is_masked_range_test (name
, val
, comp_code
, &name
, &low
,
3918 &low_code
, &high
, &high_code
))
3920 if (low_code
!= ERROR_MARK
)
3921 register_edge_assert_for_2 (name
, e
, low_code
, name
,
3922 low
, /*invert*/false, asserts
);
3923 if (high_code
!= ERROR_MARK
)
3924 register_edge_assert_for_2 (name
, e
, high_code
, name
,
3925 high
, /*invert*/false, asserts
);
3930 /* Finish found ASSERTS for E and register them at GSI. */
3933 finish_register_edge_assert_for (edge e
, gimple_stmt_iterator gsi
,
3934 vec
<assert_info
> &asserts
)
3936 for (unsigned i
= 0; i
< asserts
.length (); ++i
)
3937 /* Only register an ASSERT_EXPR if NAME was found in the sub-graph
3938 reachable from E. */
3939 if (live_on_edge (e
, asserts
[i
].name
))
3940 register_new_assert_for (asserts
[i
].name
, asserts
[i
].expr
,
3941 asserts
[i
].comp_code
, asserts
[i
].val
,
3947 /* Determine whether the outgoing edges of BB should receive an
3948 ASSERT_EXPR for each of the operands of BB's LAST statement.
3949 The last statement of BB must be a COND_EXPR.
3951 If any of the sub-graphs rooted at BB have an interesting use of
3952 the predicate operands, an assert location node is added to the
3953 list of assertions for the corresponding operands. */
3956 find_conditional_asserts (basic_block bb
, gcond
*last
)
3958 gimple_stmt_iterator bsi
;
3964 bsi
= gsi_for_stmt (last
);
3966 /* Look for uses of the operands in each of the sub-graphs
3967 rooted at BB. We need to check each of the outgoing edges
3968 separately, so that we know what kind of ASSERT_EXPR to
3970 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
3975 /* Register the necessary assertions for each operand in the
3976 conditional predicate. */
3977 auto_vec
<assert_info
, 8> asserts
;
3978 FOR_EACH_SSA_TREE_OPERAND (op
, last
, iter
, SSA_OP_USE
)
3979 register_edge_assert_for (op
, e
,
3980 gimple_cond_code (last
),
3981 gimple_cond_lhs (last
),
3982 gimple_cond_rhs (last
), asserts
);
3983 finish_register_edge_assert_for (e
, bsi
, asserts
);
3993 /* Compare two case labels sorting first by the destination bb index
3994 and then by the case value. */
3997 compare_case_labels (const void *p1
, const void *p2
)
3999 const struct case_info
*ci1
= (const struct case_info
*) p1
;
4000 const struct case_info
*ci2
= (const struct case_info
*) p2
;
4001 int idx1
= ci1
->bb
->index
;
4002 int idx2
= ci2
->bb
->index
;
4006 else if (idx1
== idx2
)
4008 /* Make sure the default label is first in a group. */
4009 if (!CASE_LOW (ci1
->expr
))
4011 else if (!CASE_LOW (ci2
->expr
))
4014 return tree_int_cst_compare (CASE_LOW (ci1
->expr
),
4015 CASE_LOW (ci2
->expr
));
4021 /* Determine whether the outgoing edges of BB should receive an
4022 ASSERT_EXPR for each of the operands of BB's LAST statement.
4023 The last statement of BB must be a SWITCH_EXPR.
4025 If any of the sub-graphs rooted at BB have an interesting use of
4026 the predicate operands, an assert location node is added to the
4027 list of assertions for the corresponding operands. */
4030 find_switch_asserts (basic_block bb
, gswitch
*last
)
4032 gimple_stmt_iterator bsi
;
4035 struct case_info
*ci
;
4036 size_t n
= gimple_switch_num_labels (last
);
4037 #if GCC_VERSION >= 4000
4040 /* Work around GCC 3.4 bug (PR 37086). */
4041 volatile unsigned int idx
;
4044 bsi
= gsi_for_stmt (last
);
4045 op
= gimple_switch_index (last
);
4046 if (TREE_CODE (op
) != SSA_NAME
)
4049 /* Build a vector of case labels sorted by destination label. */
4050 ci
= XNEWVEC (struct case_info
, n
);
4051 for (idx
= 0; idx
< n
; ++idx
)
4053 ci
[idx
].expr
= gimple_switch_label (last
, idx
);
4054 ci
[idx
].bb
= label_to_block (CASE_LABEL (ci
[idx
].expr
));
4056 edge default_edge
= find_edge (bb
, ci
[0].bb
);
4057 qsort (ci
, n
, sizeof (struct case_info
), compare_case_labels
);
4059 for (idx
= 0; idx
< n
; ++idx
)
4062 tree cl
= ci
[idx
].expr
;
4063 basic_block cbb
= ci
[idx
].bb
;
4065 min
= CASE_LOW (cl
);
4066 max
= CASE_HIGH (cl
);
4068 /* If there are multiple case labels with the same destination
4069 we need to combine them to a single value range for the edge. */
4070 if (idx
+ 1 < n
&& cbb
== ci
[idx
+ 1].bb
)
4072 /* Skip labels until the last of the group. */
4075 } while (idx
< n
&& cbb
== ci
[idx
].bb
);
4078 /* Pick up the maximum of the case label range. */
4079 if (CASE_HIGH (ci
[idx
].expr
))
4080 max
= CASE_HIGH (ci
[idx
].expr
);
4082 max
= CASE_LOW (ci
[idx
].expr
);
4085 /* Can't extract a useful assertion out of a range that includes the
4087 if (min
== NULL_TREE
)
4090 /* Find the edge to register the assert expr on. */
4091 e
= find_edge (bb
, cbb
);
4093 /* Register the necessary assertions for the operand in the
4095 auto_vec
<assert_info
, 8> asserts
;
4096 register_edge_assert_for (op
, e
,
4097 max
? GE_EXPR
: EQ_EXPR
,
4098 op
, fold_convert (TREE_TYPE (op
), min
),
4101 register_edge_assert_for (op
, e
, LE_EXPR
, op
,
4102 fold_convert (TREE_TYPE (op
), max
),
4104 finish_register_edge_assert_for (e
, bsi
, asserts
);
4109 if (!live_on_edge (default_edge
, op
))
4112 /* Now register along the default label assertions that correspond to the
4113 anti-range of each label. */
4114 int insertion_limit
= PARAM_VALUE (PARAM_MAX_VRP_SWITCH_ASSERTIONS
);
4115 if (insertion_limit
== 0)
4118 /* We can't do this if the default case shares a label with another case. */
4119 tree default_cl
= gimple_switch_default_label (last
);
4120 for (idx
= 1; idx
< n
; idx
++)
4123 tree cl
= gimple_switch_label (last
, idx
);
4124 if (CASE_LABEL (cl
) == CASE_LABEL (default_cl
))
4127 min
= CASE_LOW (cl
);
4128 max
= CASE_HIGH (cl
);
4130 /* Combine contiguous case ranges to reduce the number of assertions
4132 for (idx
= idx
+ 1; idx
< n
; idx
++)
4134 tree next_min
, next_max
;
4135 tree next_cl
= gimple_switch_label (last
, idx
);
4136 if (CASE_LABEL (next_cl
) == CASE_LABEL (default_cl
))
4139 next_min
= CASE_LOW (next_cl
);
4140 next_max
= CASE_HIGH (next_cl
);
4142 wide_int difference
= (wi::to_wide (next_min
)
4143 - wi::to_wide (max
? max
: min
));
4144 if (wi::eq_p (difference
, 1))
4145 max
= next_max
? next_max
: next_min
;
4151 if (max
== NULL_TREE
)
4153 /* Register the assertion OP != MIN. */
4154 auto_vec
<assert_info
, 8> asserts
;
4155 min
= fold_convert (TREE_TYPE (op
), min
);
4156 register_edge_assert_for (op
, default_edge
, NE_EXPR
, op
, min
,
4158 finish_register_edge_assert_for (default_edge
, bsi
, asserts
);
4162 /* Register the assertion (unsigned)OP - MIN > (MAX - MIN),
4163 which will give OP the anti-range ~[MIN,MAX]. */
4164 tree uop
= fold_convert (unsigned_type_for (TREE_TYPE (op
)), op
);
4165 min
= fold_convert (TREE_TYPE (uop
), min
);
4166 max
= fold_convert (TREE_TYPE (uop
), max
);
4168 tree lhs
= fold_build2 (MINUS_EXPR
, TREE_TYPE (uop
), uop
, min
);
4169 tree rhs
= int_const_binop (MINUS_EXPR
, max
, min
);
4170 register_new_assert_for (op
, lhs
, GT_EXPR
, rhs
,
4171 NULL
, default_edge
, bsi
);
4174 if (--insertion_limit
== 0)
4180 /* Traverse all the statements in block BB looking for statements that
4181 may generate useful assertions for the SSA names in their operand.
4182 If a statement produces a useful assertion A for name N_i, then the
4183 list of assertions already generated for N_i is scanned to
4184 determine if A is actually needed.
4186 If N_i already had the assertion A at a location dominating the
4187 current location, then nothing needs to be done. Otherwise, the
4188 new location for A is recorded instead.
4190 1- For every statement S in BB, all the variables used by S are
4191 added to bitmap FOUND_IN_SUBGRAPH.
4193 2- If statement S uses an operand N in a way that exposes a known
4194 value range for N, then if N was not already generated by an
4195 ASSERT_EXPR, create a new assert location for N. For instance,
4196 if N is a pointer and the statement dereferences it, we can
4197 assume that N is not NULL.
4199 3- COND_EXPRs are a special case of #2. We can derive range
4200 information from the predicate but need to insert different
4201 ASSERT_EXPRs for each of the sub-graphs rooted at the
4202 conditional block. If the last statement of BB is a conditional
4203 expression of the form 'X op Y', then
4205 a) Remove X and Y from the set FOUND_IN_SUBGRAPH.
4207 b) If the conditional is the only entry point to the sub-graph
4208 corresponding to the THEN_CLAUSE, recurse into it. On
4209 return, if X and/or Y are marked in FOUND_IN_SUBGRAPH, then
4210 an ASSERT_EXPR is added for the corresponding variable.
4212 c) Repeat step (b) on the ELSE_CLAUSE.
4214 d) Mark X and Y in FOUND_IN_SUBGRAPH.
4223 In this case, an assertion on the THEN clause is useful to
4224 determine that 'a' is always 9 on that edge. However, an assertion
4225 on the ELSE clause would be unnecessary.
4227 4- If BB does not end in a conditional expression, then we recurse
4228 into BB's dominator children.
4230 At the end of the recursive traversal, every SSA name will have a
4231 list of locations where ASSERT_EXPRs should be added. When a new
4232 location for name N is found, it is registered by calling
4233 register_new_assert_for. That function keeps track of all the
4234 registered assertions to prevent adding unnecessary assertions.
4235 For instance, if a pointer P_4 is dereferenced more than once in a
4236 dominator tree, only the location dominating all the dereference of
4237 P_4 will receive an ASSERT_EXPR. */
4240 find_assert_locations_1 (basic_block bb
, sbitmap live
)
4244 last
= last_stmt (bb
);
4246 /* If BB's last statement is a conditional statement involving integer
4247 operands, determine if we need to add ASSERT_EXPRs. */
4249 && gimple_code (last
) == GIMPLE_COND
4250 && !fp_predicate (last
)
4251 && !ZERO_SSA_OPERANDS (last
, SSA_OP_USE
))
4252 find_conditional_asserts (bb
, as_a
<gcond
*> (last
));
4254 /* If BB's last statement is a switch statement involving integer
4255 operands, determine if we need to add ASSERT_EXPRs. */
4257 && gimple_code (last
) == GIMPLE_SWITCH
4258 && !ZERO_SSA_OPERANDS (last
, SSA_OP_USE
))
4259 find_switch_asserts (bb
, as_a
<gswitch
*> (last
));
4261 /* Traverse all the statements in BB marking used names and looking
4262 for statements that may infer assertions for their used operands. */
4263 for (gimple_stmt_iterator si
= gsi_last_bb (bb
); !gsi_end_p (si
);
4270 stmt
= gsi_stmt (si
);
4272 if (is_gimple_debug (stmt
))
4275 /* See if we can derive an assertion for any of STMT's operands. */
4276 FOR_EACH_SSA_TREE_OPERAND (op
, stmt
, i
, SSA_OP_USE
)
4279 enum tree_code comp_code
;
4281 /* If op is not live beyond this stmt, do not bother to insert
4283 if (!bitmap_bit_p (live
, SSA_NAME_VERSION (op
)))
4286 /* If OP is used in such a way that we can infer a value
4287 range for it, and we don't find a previous assertion for
4288 it, create a new assertion location node for OP. */
4289 if (infer_value_range (stmt
, op
, &comp_code
, &value
))
4291 /* If we are able to infer a nonzero value range for OP,
4292 then walk backwards through the use-def chain to see if OP
4293 was set via a typecast.
4295 If so, then we can also infer a nonzero value range
4296 for the operand of the NOP_EXPR. */
4297 if (comp_code
== NE_EXPR
&& integer_zerop (value
))
4300 gimple
*def_stmt
= SSA_NAME_DEF_STMT (t
);
4302 while (is_gimple_assign (def_stmt
)
4303 && CONVERT_EXPR_CODE_P
4304 (gimple_assign_rhs_code (def_stmt
))
4306 (gimple_assign_rhs1 (def_stmt
)) == SSA_NAME
4308 (TREE_TYPE (gimple_assign_rhs1 (def_stmt
))))
4310 t
= gimple_assign_rhs1 (def_stmt
);
4311 def_stmt
= SSA_NAME_DEF_STMT (t
);
4313 /* Note we want to register the assert for the
4314 operand of the NOP_EXPR after SI, not after the
4316 if (bitmap_bit_p (live
, SSA_NAME_VERSION (t
)))
4317 register_new_assert_for (t
, t
, comp_code
, value
,
4322 register_new_assert_for (op
, op
, comp_code
, value
, bb
, NULL
, si
);
4327 FOR_EACH_SSA_TREE_OPERAND (op
, stmt
, i
, SSA_OP_USE
)
4328 bitmap_set_bit (live
, SSA_NAME_VERSION (op
));
4329 FOR_EACH_SSA_TREE_OPERAND (op
, stmt
, i
, SSA_OP_DEF
)
4330 bitmap_clear_bit (live
, SSA_NAME_VERSION (op
));
4333 /* Traverse all PHI nodes in BB, updating live. */
4334 for (gphi_iterator si
= gsi_start_phis (bb
); !gsi_end_p (si
);
4337 use_operand_p arg_p
;
4339 gphi
*phi
= si
.phi ();
4340 tree res
= gimple_phi_result (phi
);
4342 if (virtual_operand_p (res
))
4345 FOR_EACH_PHI_ARG (arg_p
, phi
, i
, SSA_OP_USE
)
4347 tree arg
= USE_FROM_PTR (arg_p
);
4348 if (TREE_CODE (arg
) == SSA_NAME
)
4349 bitmap_set_bit (live
, SSA_NAME_VERSION (arg
));
4352 bitmap_clear_bit (live
, SSA_NAME_VERSION (res
));
4356 /* Do an RPO walk over the function computing SSA name liveness
4357 on-the-fly and deciding on assert expressions to insert. */
4360 find_assert_locations (void)
4362 int *rpo
= XNEWVEC (int, last_basic_block_for_fn (cfun
));
4363 int *bb_rpo
= XNEWVEC (int, last_basic_block_for_fn (cfun
));
4364 int *last_rpo
= XCNEWVEC (int, last_basic_block_for_fn (cfun
));
4367 live
= XCNEWVEC (sbitmap
, last_basic_block_for_fn (cfun
));
4368 rpo_cnt
= pre_and_rev_post_order_compute (NULL
, rpo
, false);
4369 for (i
= 0; i
< rpo_cnt
; ++i
)
4372 /* Pre-seed loop latch liveness from loop header PHI nodes. Due to
4373 the order we compute liveness and insert asserts we otherwise
4374 fail to insert asserts into the loop latch. */
4376 FOR_EACH_LOOP (loop
, 0)
4378 i
= loop
->latch
->index
;
4379 unsigned int j
= single_succ_edge (loop
->latch
)->dest_idx
;
4380 for (gphi_iterator gsi
= gsi_start_phis (loop
->header
);
4381 !gsi_end_p (gsi
); gsi_next (&gsi
))
4383 gphi
*phi
= gsi
.phi ();
4384 if (virtual_operand_p (gimple_phi_result (phi
)))
4386 tree arg
= gimple_phi_arg_def (phi
, j
);
4387 if (TREE_CODE (arg
) == SSA_NAME
)
4389 if (live
[i
] == NULL
)
4391 live
[i
] = sbitmap_alloc (num_ssa_names
);
4392 bitmap_clear (live
[i
]);
4394 bitmap_set_bit (live
[i
], SSA_NAME_VERSION (arg
));
4399 for (i
= rpo_cnt
- 1; i
>= 0; --i
)
4401 basic_block bb
= BASIC_BLOCK_FOR_FN (cfun
, rpo
[i
]);
4407 live
[rpo
[i
]] = sbitmap_alloc (num_ssa_names
);
4408 bitmap_clear (live
[rpo
[i
]]);
4411 /* Process BB and update the live information with uses in
4413 find_assert_locations_1 (bb
, live
[rpo
[i
]]);
4415 /* Merge liveness into the predecessor blocks and free it. */
4416 if (!bitmap_empty_p (live
[rpo
[i
]]))
4419 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
4421 int pred
= e
->src
->index
;
4422 if ((e
->flags
& EDGE_DFS_BACK
) || pred
== ENTRY_BLOCK
)
4427 live
[pred
] = sbitmap_alloc (num_ssa_names
);
4428 bitmap_clear (live
[pred
]);
4430 bitmap_ior (live
[pred
], live
[pred
], live
[rpo
[i
]]);
4432 if (bb_rpo
[pred
] < pred_rpo
)
4433 pred_rpo
= bb_rpo
[pred
];
4436 /* Record the RPO number of the last visited block that needs
4437 live information from this block. */
4438 last_rpo
[rpo
[i
]] = pred_rpo
;
4442 sbitmap_free (live
[rpo
[i
]]);
4443 live
[rpo
[i
]] = NULL
;
4446 /* We can free all successors live bitmaps if all their
4447 predecessors have been visited already. */
4448 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
4449 if (last_rpo
[e
->dest
->index
] == i
4450 && live
[e
->dest
->index
])
4452 sbitmap_free (live
[e
->dest
->index
]);
4453 live
[e
->dest
->index
] = NULL
;
4458 XDELETEVEC (bb_rpo
);
4459 XDELETEVEC (last_rpo
);
4460 for (i
= 0; i
< last_basic_block_for_fn (cfun
); ++i
)
4462 sbitmap_free (live
[i
]);
4466 /* Create an ASSERT_EXPR for NAME and insert it in the location
4467 indicated by LOC. Return true if we made any edge insertions. */
4470 process_assert_insertions_for (tree name
, assert_locus
*loc
)
4472 /* Build the comparison expression NAME_i COMP_CODE VAL. */
4475 gimple
*assert_stmt
;
4479 /* If we have X <=> X do not insert an assert expr for that. */
4480 if (loc
->expr
== loc
->val
)
4483 cond
= build2 (loc
->comp_code
, boolean_type_node
, loc
->expr
, loc
->val
);
4484 assert_stmt
= build_assert_expr_for (cond
, name
);
4487 /* We have been asked to insert the assertion on an edge. This
4488 is used only by COND_EXPR and SWITCH_EXPR assertions. */
4489 gcc_checking_assert (gimple_code (gsi_stmt (loc
->si
)) == GIMPLE_COND
4490 || (gimple_code (gsi_stmt (loc
->si
))
4493 gsi_insert_on_edge (loc
->e
, assert_stmt
);
4497 /* If the stmt iterator points at the end then this is an insertion
4498 at the beginning of a block. */
4499 if (gsi_end_p (loc
->si
))
4501 gimple_stmt_iterator si
= gsi_after_labels (loc
->bb
);
4502 gsi_insert_before (&si
, assert_stmt
, GSI_SAME_STMT
);
4506 /* Otherwise, we can insert right after LOC->SI iff the
4507 statement must not be the last statement in the block. */
4508 stmt
= gsi_stmt (loc
->si
);
4509 if (!stmt_ends_bb_p (stmt
))
4511 gsi_insert_after (&loc
->si
, assert_stmt
, GSI_SAME_STMT
);
4515 /* If STMT must be the last statement in BB, we can only insert new
4516 assertions on the non-abnormal edge out of BB. Note that since
4517 STMT is not control flow, there may only be one non-abnormal/eh edge
4519 FOR_EACH_EDGE (e
, ei
, loc
->bb
->succs
)
4520 if (!(e
->flags
& (EDGE_ABNORMAL
|EDGE_EH
)))
4522 gsi_insert_on_edge (e
, assert_stmt
);
4529 /* Qsort helper for sorting assert locations. If stable is true, don't
4530 use iterative_hash_expr because it can be unstable for -fcompare-debug,
4531 on the other side some pointers might be NULL. */
4533 template <bool stable
>
4535 compare_assert_loc (const void *pa
, const void *pb
)
4537 assert_locus
* const a
= *(assert_locus
* const *)pa
;
4538 assert_locus
* const b
= *(assert_locus
* const *)pb
;
4540 /* If stable, some asserts might be optimized away already, sort
4550 if (a
->e
== NULL
&& b
->e
!= NULL
)
4552 else if (a
->e
!= NULL
&& b
->e
== NULL
)
4555 /* After the above checks, we know that (a->e == NULL) == (b->e == NULL),
4556 no need to test both a->e and b->e. */
4558 /* Sort after destination index. */
4561 else if (a
->e
->dest
->index
> b
->e
->dest
->index
)
4563 else if (a
->e
->dest
->index
< b
->e
->dest
->index
)
4566 /* Sort after comp_code. */
4567 if (a
->comp_code
> b
->comp_code
)
4569 else if (a
->comp_code
< b
->comp_code
)
4574 /* E.g. if a->val is ADDR_EXPR of a VAR_DECL, iterative_hash_expr
4575 uses DECL_UID of the VAR_DECL, so sorting might differ between
4576 -g and -g0. When doing the removal of redundant assert exprs
4577 and commonization to successors, this does not matter, but for
4578 the final sort needs to be stable. */
4586 ha
= iterative_hash_expr (a
->expr
, iterative_hash_expr (a
->val
, 0));
4587 hb
= iterative_hash_expr (b
->expr
, iterative_hash_expr (b
->val
, 0));
4590 /* Break the tie using hashing and source/bb index. */
4592 return (a
->e
!= NULL
4593 ? a
->e
->src
->index
- b
->e
->src
->index
4594 : a
->bb
->index
- b
->bb
->index
);
4595 return ha
> hb
? 1 : -1;
4598 /* Process all the insertions registered for every name N_i registered
4599 in NEED_ASSERT_FOR. The list of assertions to be inserted are
4600 found in ASSERTS_FOR[i]. */
4603 process_assert_insertions (void)
4607 bool update_edges_p
= false;
4608 int num_asserts
= 0;
4610 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4611 dump_all_asserts (dump_file
);
4613 EXECUTE_IF_SET_IN_BITMAP (need_assert_for
, 0, i
, bi
)
4615 assert_locus
*loc
= asserts_for
[i
];
4618 auto_vec
<assert_locus
*, 16> asserts
;
4619 for (; loc
; loc
= loc
->next
)
4620 asserts
.safe_push (loc
);
4621 asserts
.qsort (compare_assert_loc
<false>);
4623 /* Push down common asserts to successors and remove redundant ones. */
4625 assert_locus
*common
= NULL
;
4626 unsigned commonj
= 0;
4627 for (unsigned j
= 0; j
< asserts
.length (); ++j
)
4633 || loc
->e
->dest
!= common
->e
->dest
4634 || loc
->comp_code
!= common
->comp_code
4635 || ! operand_equal_p (loc
->val
, common
->val
, 0)
4636 || ! operand_equal_p (loc
->expr
, common
->expr
, 0))
4642 else if (loc
->e
== asserts
[j
-1]->e
)
4644 /* Remove duplicate asserts. */
4645 if (commonj
== j
- 1)
4650 free (asserts
[j
-1]);
4651 asserts
[j
-1] = NULL
;
4656 if (EDGE_COUNT (common
->e
->dest
->preds
) == ecnt
)
4658 /* We have the same assertion on all incoming edges of a BB.
4659 Insert it at the beginning of that block. */
4660 loc
->bb
= loc
->e
->dest
;
4662 loc
->si
= gsi_none ();
4664 /* Clear asserts commoned. */
4665 for (; commonj
!= j
; ++commonj
)
4666 if (asserts
[commonj
])
4668 free (asserts
[commonj
]);
4669 asserts
[commonj
] = NULL
;
4675 /* The asserts vector sorting above might be unstable for
4676 -fcompare-debug, sort again to ensure a stable sort. */
4677 asserts
.qsort (compare_assert_loc
<true>);
4678 for (unsigned j
= 0; j
< asserts
.length (); ++j
)
4683 update_edges_p
|= process_assert_insertions_for (ssa_name (i
), loc
);
4690 gsi_commit_edge_inserts ();
4692 statistics_counter_event (cfun
, "Number of ASSERT_EXPR expressions inserted",
4697 /* Traverse the flowgraph looking for conditional jumps to insert range
4698 expressions. These range expressions are meant to provide information
4699 to optimizations that need to reason in terms of value ranges. They
4700 will not be expanded into RTL. For instance, given:
4709 this pass will transform the code into:
4715 x = ASSERT_EXPR <x, x < y>
4720 y = ASSERT_EXPR <y, x >= y>
4724 The idea is that once copy and constant propagation have run, other
4725 optimizations will be able to determine what ranges of values can 'x'
4726 take in different paths of the code, simply by checking the reaching
4727 definition of 'x'. */
4730 insert_range_assertions (void)
4732 need_assert_for
= BITMAP_ALLOC (NULL
);
4733 asserts_for
= XCNEWVEC (assert_locus
*, num_ssa_names
);
4735 calculate_dominance_info (CDI_DOMINATORS
);
4737 find_assert_locations ();
4738 if (!bitmap_empty_p (need_assert_for
))
4740 process_assert_insertions ();
4741 update_ssa (TODO_update_ssa_no_phi
);
4744 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4746 fprintf (dump_file
, "\nSSA form after inserting ASSERT_EXPRs\n");
4747 dump_function_to_file (current_function_decl
, dump_file
, dump_flags
);
4751 BITMAP_FREE (need_assert_for
);
4754 class vrp_prop
: public ssa_propagation_engine
4757 enum ssa_prop_result
visit_stmt (gimple
*, edge
*, tree
*) FINAL OVERRIDE
;
4758 enum ssa_prop_result
visit_phi (gphi
*) FINAL OVERRIDE
;
4760 void vrp_initialize (void);
4761 void vrp_finalize (bool);
4762 void check_all_array_refs (void);
4763 void check_array_ref (location_t
, tree
, bool);
4764 void search_for_addr_array (tree
, location_t
);
4766 class vr_values vr_values
;
4767 /* Temporary delegator to minimize code churn. */
4768 value_range
*get_value_range (const_tree op
)
4769 { return vr_values
.get_value_range (op
); }
4770 void set_defs_to_varying (gimple
*stmt
)
4771 { return vr_values
.set_defs_to_varying (stmt
); }
4772 void extract_range_from_stmt (gimple
*stmt
, edge
*taken_edge_p
,
4773 tree
*output_p
, value_range
*vr
)
4774 { vr_values
.extract_range_from_stmt (stmt
, taken_edge_p
, output_p
, vr
); }
4775 bool update_value_range (const_tree op
, value_range
*vr
)
4776 { return vr_values
.update_value_range (op
, vr
); }
4777 void extract_range_basic (value_range
*vr
, gimple
*stmt
)
4778 { vr_values
.extract_range_basic (vr
, stmt
); }
4779 void extract_range_from_phi_node (gphi
*phi
, value_range
*vr
)
4780 { vr_values
.extract_range_from_phi_node (phi
, vr
); }
4782 /* Checks one ARRAY_REF in REF, located at LOCUS. Ignores flexible arrays
4783 and "struct" hacks. If VRP can determine that the
4784 array subscript is a constant, check if it is outside valid
4785 range. If the array subscript is a RANGE, warn if it is
4786 non-overlapping with valid range.
4787 IGNORE_OFF_BY_ONE is true if the ARRAY_REF is inside a ADDR_EXPR. */
4790 vrp_prop::check_array_ref (location_t location
, tree ref
,
4791 bool ignore_off_by_one
)
4793 value_range
*vr
= NULL
;
4794 tree low_sub
, up_sub
;
4795 tree low_bound
, up_bound
, up_bound_p1
;
4797 if (TREE_NO_WARNING (ref
))
4800 low_sub
= up_sub
= TREE_OPERAND (ref
, 1);
4801 up_bound
= array_ref_up_bound (ref
);
4804 || TREE_CODE (up_bound
) != INTEGER_CST
4805 || (warn_array_bounds
< 2
4806 && array_at_struct_end_p (ref
)))
4808 /* Accesses to trailing arrays via pointers may access storage
4809 beyond the types array bounds. For such arrays, or for flexible
4810 array members, as well as for other arrays of an unknown size,
4811 replace the upper bound with a more permissive one that assumes
4812 the size of the largest object is PTRDIFF_MAX. */
4813 tree eltsize
= array_ref_element_size (ref
);
4815 if (TREE_CODE (eltsize
) != INTEGER_CST
4816 || integer_zerop (eltsize
))
4818 up_bound
= NULL_TREE
;
4819 up_bound_p1
= NULL_TREE
;
4823 tree maxbound
= TYPE_MAX_VALUE (ptrdiff_type_node
);
4824 tree arg
= TREE_OPERAND (ref
, 0);
4827 if (get_addr_base_and_unit_offset (arg
, &off
) && known_gt (off
, 0))
4828 maxbound
= wide_int_to_tree (sizetype
,
4829 wi::sub (wi::to_wide (maxbound
),
4832 maxbound
= fold_convert (sizetype
, maxbound
);
4834 up_bound_p1
= int_const_binop (TRUNC_DIV_EXPR
, maxbound
, eltsize
);
4836 up_bound
= int_const_binop (MINUS_EXPR
, up_bound_p1
,
4837 build_int_cst (ptrdiff_type_node
, 1));
4841 up_bound_p1
= int_const_binop (PLUS_EXPR
, up_bound
,
4842 build_int_cst (TREE_TYPE (up_bound
), 1));
4844 low_bound
= array_ref_low_bound (ref
);
4846 tree artype
= TREE_TYPE (TREE_OPERAND (ref
, 0));
4849 if (up_bound
&& tree_int_cst_equal (low_bound
, up_bound_p1
))
4851 warning_at (location
, OPT_Warray_bounds
,
4852 "array subscript %E is above array bounds of %qT",
4854 TREE_NO_WARNING (ref
) = 1;
4857 if (TREE_CODE (low_sub
) == SSA_NAME
)
4859 vr
= get_value_range (low_sub
);
4860 if (vr
->type
== VR_RANGE
|| vr
->type
== VR_ANTI_RANGE
)
4862 low_sub
= vr
->type
== VR_RANGE
? vr
->max
: vr
->min
;
4863 up_sub
= vr
->type
== VR_RANGE
? vr
->min
: vr
->max
;
4867 if (vr
&& vr
->type
== VR_ANTI_RANGE
)
4870 && TREE_CODE (up_sub
) == INTEGER_CST
4871 && (ignore_off_by_one
4872 ? tree_int_cst_lt (up_bound
, up_sub
)
4873 : tree_int_cst_le (up_bound
, up_sub
))
4874 && TREE_CODE (low_sub
) == INTEGER_CST
4875 && tree_int_cst_le (low_sub
, low_bound
))
4877 warning_at (location
, OPT_Warray_bounds
,
4878 "array subscript [%E, %E] is outside array bounds of %qT",
4879 low_sub
, up_sub
, artype
);
4880 TREE_NO_WARNING (ref
) = 1;
4884 && TREE_CODE (up_sub
) == INTEGER_CST
4885 && (ignore_off_by_one
4886 ? !tree_int_cst_le (up_sub
, up_bound_p1
)
4887 : !tree_int_cst_le (up_sub
, up_bound
)))
4889 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4891 fprintf (dump_file
, "Array bound warning for ");
4892 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, ref
);
4893 fprintf (dump_file
, "\n");
4895 warning_at (location
, OPT_Warray_bounds
,
4896 "array subscript %E is above array bounds of %qT",
4898 TREE_NO_WARNING (ref
) = 1;
4900 else if (TREE_CODE (low_sub
) == INTEGER_CST
4901 && tree_int_cst_lt (low_sub
, low_bound
))
4903 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4905 fprintf (dump_file
, "Array bound warning for ");
4906 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, ref
);
4907 fprintf (dump_file
, "\n");
4909 warning_at (location
, OPT_Warray_bounds
,
4910 "array subscript %E is below array bounds of %qT",
4912 TREE_NO_WARNING (ref
) = 1;
4916 /* Searches if the expr T, located at LOCATION computes
4917 address of an ARRAY_REF, and call check_array_ref on it. */
4920 vrp_prop::search_for_addr_array (tree t
, location_t location
)
4922 /* Check each ARRAY_REFs in the reference chain. */
4925 if (TREE_CODE (t
) == ARRAY_REF
)
4926 check_array_ref (location
, t
, true /*ignore_off_by_one*/);
4928 t
= TREE_OPERAND (t
, 0);
4930 while (handled_component_p (t
));
4932 if (TREE_CODE (t
) == MEM_REF
4933 && TREE_CODE (TREE_OPERAND (t
, 0)) == ADDR_EXPR
4934 && !TREE_NO_WARNING (t
))
4936 tree tem
= TREE_OPERAND (TREE_OPERAND (t
, 0), 0);
4937 tree low_bound
, up_bound
, el_sz
;
4939 if (TREE_CODE (TREE_TYPE (tem
)) != ARRAY_TYPE
4940 || TREE_CODE (TREE_TYPE (TREE_TYPE (tem
))) == ARRAY_TYPE
4941 || !TYPE_DOMAIN (TREE_TYPE (tem
)))
4944 low_bound
= TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (tem
)));
4945 up_bound
= TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (tem
)));
4946 el_sz
= TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (tem
)));
4948 || TREE_CODE (low_bound
) != INTEGER_CST
4950 || TREE_CODE (up_bound
) != INTEGER_CST
4952 || TREE_CODE (el_sz
) != INTEGER_CST
)
4955 if (!mem_ref_offset (t
).is_constant (&idx
))
4958 idx
= wi::sdiv_trunc (idx
, wi::to_offset (el_sz
));
4961 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4963 fprintf (dump_file
, "Array bound warning for ");
4964 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, t
);
4965 fprintf (dump_file
, "\n");
4967 warning_at (location
, OPT_Warray_bounds
,
4968 "array subscript %wi is below array bounds of %qT",
4969 idx
.to_shwi (), TREE_TYPE (tem
));
4970 TREE_NO_WARNING (t
) = 1;
4972 else if (idx
> (wi::to_offset (up_bound
)
4973 - wi::to_offset (low_bound
) + 1))
4975 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4977 fprintf (dump_file
, "Array bound warning for ");
4978 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, t
);
4979 fprintf (dump_file
, "\n");
4981 warning_at (location
, OPT_Warray_bounds
,
4982 "array subscript %wu is above array bounds of %qT",
4983 idx
.to_uhwi (), TREE_TYPE (tem
));
4984 TREE_NO_WARNING (t
) = 1;
4989 /* walk_tree() callback that checks if *TP is
4990 an ARRAY_REF inside an ADDR_EXPR (in which an array
4991 subscript one outside the valid range is allowed). Call
4992 check_array_ref for each ARRAY_REF found. The location is
4996 check_array_bounds (tree
*tp
, int *walk_subtree
, void *data
)
4999 struct walk_stmt_info
*wi
= (struct walk_stmt_info
*) data
;
5000 location_t location
;
5002 if (EXPR_HAS_LOCATION (t
))
5003 location
= EXPR_LOCATION (t
);
5005 location
= gimple_location (wi
->stmt
);
5007 *walk_subtree
= TRUE
;
5009 vrp_prop
*vrp_prop
= (class vrp_prop
*)wi
->info
;
5010 if (TREE_CODE (t
) == ARRAY_REF
)
5011 vrp_prop
->check_array_ref (location
, t
, false /*ignore_off_by_one*/);
5013 else if (TREE_CODE (t
) == ADDR_EXPR
)
5015 vrp_prop
->search_for_addr_array (t
, location
);
5016 *walk_subtree
= FALSE
;
5022 /* A dom_walker subclass for use by vrp_prop::check_all_array_refs,
5023 to walk over all statements of all reachable BBs and call
5024 check_array_bounds on them. */
5026 class check_array_bounds_dom_walker
: public dom_walker
5029 check_array_bounds_dom_walker (vrp_prop
*prop
)
5030 : dom_walker (CDI_DOMINATORS
, true), m_prop (prop
) {}
5031 ~check_array_bounds_dom_walker () {}
5033 edge
before_dom_children (basic_block
) FINAL OVERRIDE
;
5039 /* Implementation of dom_walker::before_dom_children.
5041 Walk over all statements of BB and call check_array_bounds on them,
5042 and determine if there's a unique successor edge. */
5045 check_array_bounds_dom_walker::before_dom_children (basic_block bb
)
5047 gimple_stmt_iterator si
;
5048 for (si
= gsi_start_bb (bb
); !gsi_end_p (si
); gsi_next (&si
))
5050 gimple
*stmt
= gsi_stmt (si
);
5051 struct walk_stmt_info wi
;
5052 if (!gimple_has_location (stmt
)
5053 || is_gimple_debug (stmt
))
5056 memset (&wi
, 0, sizeof (wi
));
5060 walk_gimple_op (stmt
, check_array_bounds
, &wi
);
5063 /* Determine if there's a unique successor edge, and if so, return
5064 that back to dom_walker, ensuring that we don't visit blocks that
5065 became unreachable during the VRP propagation
5066 (PR tree-optimization/83312). */
5067 return find_taken_edge (bb
, NULL_TREE
);
5070 /* Walk over all statements of all reachable BBs and call check_array_bounds
5074 vrp_prop::check_all_array_refs ()
5076 check_array_bounds_dom_walker
w (this);
5077 w
.walk (ENTRY_BLOCK_PTR_FOR_FN (cfun
));
5080 /* Return true if all imm uses of VAR are either in STMT, or
5081 feed (optionally through a chain of single imm uses) GIMPLE_COND
5082 in basic block COND_BB. */
5085 all_imm_uses_in_stmt_or_feed_cond (tree var
, gimple
*stmt
, basic_block cond_bb
)
5087 use_operand_p use_p
, use2_p
;
5088 imm_use_iterator iter
;
5090 FOR_EACH_IMM_USE_FAST (use_p
, iter
, var
)
5091 if (USE_STMT (use_p
) != stmt
)
5093 gimple
*use_stmt
= USE_STMT (use_p
), *use_stmt2
;
5094 if (is_gimple_debug (use_stmt
))
5096 while (is_gimple_assign (use_stmt
)
5097 && TREE_CODE (gimple_assign_lhs (use_stmt
)) == SSA_NAME
5098 && single_imm_use (gimple_assign_lhs (use_stmt
),
5099 &use2_p
, &use_stmt2
))
5100 use_stmt
= use_stmt2
;
5101 if (gimple_code (use_stmt
) != GIMPLE_COND
5102 || gimple_bb (use_stmt
) != cond_bb
)
5115 __builtin_unreachable ();
5117 x_5 = ASSERT_EXPR <x_3, ...>;
5118 If x_3 has no other immediate uses (checked by caller),
5119 var is the x_3 var from ASSERT_EXPR, we can clear low 5 bits
5120 from the non-zero bitmask. */
5123 maybe_set_nonzero_bits (edge e
, tree var
)
5125 basic_block cond_bb
= e
->src
;
5126 gimple
*stmt
= last_stmt (cond_bb
);
5130 || gimple_code (stmt
) != GIMPLE_COND
5131 || gimple_cond_code (stmt
) != ((e
->flags
& EDGE_TRUE_VALUE
)
5132 ? EQ_EXPR
: NE_EXPR
)
5133 || TREE_CODE (gimple_cond_lhs (stmt
)) != SSA_NAME
5134 || !integer_zerop (gimple_cond_rhs (stmt
)))
5137 stmt
= SSA_NAME_DEF_STMT (gimple_cond_lhs (stmt
));
5138 if (!is_gimple_assign (stmt
)
5139 || gimple_assign_rhs_code (stmt
) != BIT_AND_EXPR
5140 || TREE_CODE (gimple_assign_rhs2 (stmt
)) != INTEGER_CST
)
5142 if (gimple_assign_rhs1 (stmt
) != var
)
5146 if (TREE_CODE (gimple_assign_rhs1 (stmt
)) != SSA_NAME
)
5148 stmt2
= SSA_NAME_DEF_STMT (gimple_assign_rhs1 (stmt
));
5149 if (!gimple_assign_cast_p (stmt2
)
5150 || gimple_assign_rhs1 (stmt2
) != var
5151 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (stmt2
))
5152 || (TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (stmt
)))
5153 != TYPE_PRECISION (TREE_TYPE (var
))))
5156 cst
= gimple_assign_rhs2 (stmt
);
5157 set_nonzero_bits (var
, wi::bit_and_not (get_nonzero_bits (var
),
5158 wi::to_wide (cst
)));
5161 /* Convert range assertion expressions into the implied copies and
5162 copy propagate away the copies. Doing the trivial copy propagation
5163 here avoids the need to run the full copy propagation pass after
5166 FIXME, this will eventually lead to copy propagation removing the
5167 names that had useful range information attached to them. For
5168 instance, if we had the assertion N_i = ASSERT_EXPR <N_j, N_j > 3>,
5169 then N_i will have the range [3, +INF].
5171 However, by converting the assertion into the implied copy
5172 operation N_i = N_j, we will then copy-propagate N_j into the uses
5173 of N_i and lose the range information. We may want to hold on to
5174 ASSERT_EXPRs a little while longer as the ranges could be used in
5175 things like jump threading.
5177 The problem with keeping ASSERT_EXPRs around is that passes after
5178 VRP need to handle them appropriately.
5180 Another approach would be to make the range information a first
5181 class property of the SSA_NAME so that it can be queried from
5182 any pass. This is made somewhat more complex by the need for
5183 multiple ranges to be associated with one SSA_NAME. */
5186 remove_range_assertions (void)
5189 gimple_stmt_iterator si
;
5190 /* 1 if looking at ASSERT_EXPRs immediately at the beginning of
5191 a basic block preceeded by GIMPLE_COND branching to it and
5192 __builtin_trap, -1 if not yet checked, 0 otherwise. */
5195 /* Note that the BSI iterator bump happens at the bottom of the
5196 loop and no bump is necessary if we're removing the statement
5197 referenced by the current BSI. */
5198 FOR_EACH_BB_FN (bb
, cfun
)
5199 for (si
= gsi_after_labels (bb
), is_unreachable
= -1; !gsi_end_p (si
);)
5201 gimple
*stmt
= gsi_stmt (si
);
5203 if (is_gimple_assign (stmt
)
5204 && gimple_assign_rhs_code (stmt
) == ASSERT_EXPR
)
5206 tree lhs
= gimple_assign_lhs (stmt
);
5207 tree rhs
= gimple_assign_rhs1 (stmt
);
5210 var
= ASSERT_EXPR_VAR (rhs
);
5212 if (TREE_CODE (var
) == SSA_NAME
5213 && !POINTER_TYPE_P (TREE_TYPE (lhs
))
5214 && SSA_NAME_RANGE_INFO (lhs
))
5216 if (is_unreachable
== -1)
5219 if (single_pred_p (bb
)
5220 && assert_unreachable_fallthru_edge_p
5221 (single_pred_edge (bb
)))
5225 if (x_7 >= 10 && x_7 < 20)
5226 __builtin_unreachable ();
5227 x_8 = ASSERT_EXPR <x_7, ...>;
5228 if the only uses of x_7 are in the ASSERT_EXPR and
5229 in the condition. In that case, we can copy the
5230 range info from x_8 computed in this pass also
5233 && all_imm_uses_in_stmt_or_feed_cond (var
, stmt
,
5236 set_range_info (var
, SSA_NAME_RANGE_TYPE (lhs
),
5237 SSA_NAME_RANGE_INFO (lhs
)->get_min (),
5238 SSA_NAME_RANGE_INFO (lhs
)->get_max ());
5239 maybe_set_nonzero_bits (single_pred_edge (bb
), var
);
5243 /* Propagate the RHS into every use of the LHS. For SSA names
5244 also propagate abnormals as it merely restores the original
5245 IL in this case (an replace_uses_by would assert). */
5246 if (TREE_CODE (var
) == SSA_NAME
)
5248 imm_use_iterator iter
;
5249 use_operand_p use_p
;
5251 FOR_EACH_IMM_USE_STMT (use_stmt
, iter
, lhs
)
5252 FOR_EACH_IMM_USE_ON_STMT (use_p
, iter
)
5253 SET_USE (use_p
, var
);
5256 replace_uses_by (lhs
, var
);
5258 /* And finally, remove the copy, it is not needed. */
5259 gsi_remove (&si
, true);
5260 release_defs (stmt
);
5264 if (!is_gimple_debug (gsi_stmt (si
)))
5271 /* Return true if STMT is interesting for VRP. */
5274 stmt_interesting_for_vrp (gimple
*stmt
)
5276 if (gimple_code (stmt
) == GIMPLE_PHI
)
5278 tree res
= gimple_phi_result (stmt
);
5279 return (!virtual_operand_p (res
)
5280 && (INTEGRAL_TYPE_P (TREE_TYPE (res
))
5281 || POINTER_TYPE_P (TREE_TYPE (res
))));
5283 else if (is_gimple_assign (stmt
) || is_gimple_call (stmt
))
5285 tree lhs
= gimple_get_lhs (stmt
);
5287 /* In general, assignments with virtual operands are not useful
5288 for deriving ranges, with the obvious exception of calls to
5289 builtin functions. */
5290 if (lhs
&& TREE_CODE (lhs
) == SSA_NAME
5291 && (INTEGRAL_TYPE_P (TREE_TYPE (lhs
))
5292 || POINTER_TYPE_P (TREE_TYPE (lhs
)))
5293 && (is_gimple_call (stmt
)
5294 || !gimple_vuse (stmt
)))
5296 else if (is_gimple_call (stmt
) && gimple_call_internal_p (stmt
))
5297 switch (gimple_call_internal_fn (stmt
))
5299 case IFN_ADD_OVERFLOW
:
5300 case IFN_SUB_OVERFLOW
:
5301 case IFN_MUL_OVERFLOW
:
5302 case IFN_ATOMIC_COMPARE_EXCHANGE
:
5303 /* These internal calls return _Complex integer type,
5304 but are interesting to VRP nevertheless. */
5305 if (lhs
&& TREE_CODE (lhs
) == SSA_NAME
)
5312 else if (gimple_code (stmt
) == GIMPLE_COND
5313 || gimple_code (stmt
) == GIMPLE_SWITCH
)
5319 /* Initialization required by ssa_propagate engine. */
5322 vrp_prop::vrp_initialize ()
5326 FOR_EACH_BB_FN (bb
, cfun
)
5328 for (gphi_iterator si
= gsi_start_phis (bb
); !gsi_end_p (si
);
5331 gphi
*phi
= si
.phi ();
5332 if (!stmt_interesting_for_vrp (phi
))
5334 tree lhs
= PHI_RESULT (phi
);
5335 set_value_range_to_varying (get_value_range (lhs
));
5336 prop_set_simulate_again (phi
, false);
5339 prop_set_simulate_again (phi
, true);
5342 for (gimple_stmt_iterator si
= gsi_start_bb (bb
); !gsi_end_p (si
);
5345 gimple
*stmt
= gsi_stmt (si
);
5347 /* If the statement is a control insn, then we do not
5348 want to avoid simulating the statement once. Failure
5349 to do so means that those edges will never get added. */
5350 if (stmt_ends_bb_p (stmt
))
5351 prop_set_simulate_again (stmt
, true);
5352 else if (!stmt_interesting_for_vrp (stmt
))
5354 set_defs_to_varying (stmt
);
5355 prop_set_simulate_again (stmt
, false);
5358 prop_set_simulate_again (stmt
, true);
5363 /* Searches the case label vector VEC for the index *IDX of the CASE_LABEL
5364 that includes the value VAL. The search is restricted to the range
5365 [START_IDX, n - 1] where n is the size of VEC.
5367 If there is a CASE_LABEL for VAL, its index is placed in IDX and true is
5370 If there is no CASE_LABEL for VAL and there is one that is larger than VAL,
5371 it is placed in IDX and false is returned.
5373 If VAL is larger than any CASE_LABEL, n is placed on IDX and false is
5377 find_case_label_index (gswitch
*stmt
, size_t start_idx
, tree val
, size_t *idx
)
5379 size_t n
= gimple_switch_num_labels (stmt
);
5382 /* Find case label for minimum of the value range or the next one.
5383 At each iteration we are searching in [low, high - 1]. */
5385 for (low
= start_idx
, high
= n
; high
!= low
; )
5389 /* Note that i != high, so we never ask for n. */
5390 size_t i
= (high
+ low
) / 2;
5391 t
= gimple_switch_label (stmt
, i
);
5393 /* Cache the result of comparing CASE_LOW and val. */
5394 cmp
= tree_int_cst_compare (CASE_LOW (t
), val
);
5398 /* Ranges cannot be empty. */
5407 if (CASE_HIGH (t
) != NULL
5408 && tree_int_cst_compare (CASE_HIGH (t
), val
) >= 0)
5420 /* Searches the case label vector VEC for the range of CASE_LABELs that is used
5421 for values between MIN and MAX. The first index is placed in MIN_IDX. The
5422 last index is placed in MAX_IDX. If the range of CASE_LABELs is empty
5423 then MAX_IDX < MIN_IDX.
5424 Returns true if the default label is not needed. */
5427 find_case_label_range (gswitch
*stmt
, tree min
, tree max
, size_t *min_idx
,
5431 bool min_take_default
= !find_case_label_index (stmt
, 1, min
, &i
);
5432 bool max_take_default
= !find_case_label_index (stmt
, i
, max
, &j
);
5436 && max_take_default
)
5438 /* Only the default case label reached.
5439 Return an empty range. */
5446 bool take_default
= min_take_default
|| max_take_default
;
5450 if (max_take_default
)
5453 /* If the case label range is continuous, we do not need
5454 the default case label. Verify that. */
5455 high
= CASE_LOW (gimple_switch_label (stmt
, i
));
5456 if (CASE_HIGH (gimple_switch_label (stmt
, i
)))
5457 high
= CASE_HIGH (gimple_switch_label (stmt
, i
));
5458 for (k
= i
+ 1; k
<= j
; ++k
)
5460 low
= CASE_LOW (gimple_switch_label (stmt
, k
));
5461 if (!integer_onep (int_const_binop (MINUS_EXPR
, low
, high
)))
5463 take_default
= true;
5467 if (CASE_HIGH (gimple_switch_label (stmt
, k
)))
5468 high
= CASE_HIGH (gimple_switch_label (stmt
, k
));
5473 return !take_default
;
5477 /* Evaluate statement STMT. If the statement produces a useful range,
5478 return SSA_PROP_INTERESTING and record the SSA name with the
5479 interesting range into *OUTPUT_P.
5481 If STMT is a conditional branch and we can determine its truth
5482 value, the taken edge is recorded in *TAKEN_EDGE_P.
5484 If STMT produces a varying value, return SSA_PROP_VARYING. */
5486 enum ssa_prop_result
5487 vrp_prop::visit_stmt (gimple
*stmt
, edge
*taken_edge_p
, tree
*output_p
)
5489 value_range vr
= VR_INITIALIZER
;
5490 tree lhs
= gimple_get_lhs (stmt
);
5491 extract_range_from_stmt (stmt
, taken_edge_p
, output_p
, &vr
);
5495 if (update_value_range (*output_p
, &vr
))
5497 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
5499 fprintf (dump_file
, "Found new range for ");
5500 print_generic_expr (dump_file
, *output_p
);
5501 fprintf (dump_file
, ": ");
5502 dump_value_range (dump_file
, &vr
);
5503 fprintf (dump_file
, "\n");
5506 if (vr
.type
== VR_VARYING
)
5507 return SSA_PROP_VARYING
;
5509 return SSA_PROP_INTERESTING
;
5511 return SSA_PROP_NOT_INTERESTING
;
5514 if (is_gimple_call (stmt
) && gimple_call_internal_p (stmt
))
5515 switch (gimple_call_internal_fn (stmt
))
5517 case IFN_ADD_OVERFLOW
:
5518 case IFN_SUB_OVERFLOW
:
5519 case IFN_MUL_OVERFLOW
:
5520 case IFN_ATOMIC_COMPARE_EXCHANGE
:
5521 /* These internal calls return _Complex integer type,
5522 which VRP does not track, but the immediate uses
5523 thereof might be interesting. */
5524 if (lhs
&& TREE_CODE (lhs
) == SSA_NAME
)
5526 imm_use_iterator iter
;
5527 use_operand_p use_p
;
5528 enum ssa_prop_result res
= SSA_PROP_VARYING
;
5530 set_value_range_to_varying (get_value_range (lhs
));
5532 FOR_EACH_IMM_USE_FAST (use_p
, iter
, lhs
)
5534 gimple
*use_stmt
= USE_STMT (use_p
);
5535 if (!is_gimple_assign (use_stmt
))
5537 enum tree_code rhs_code
= gimple_assign_rhs_code (use_stmt
);
5538 if (rhs_code
!= REALPART_EXPR
&& rhs_code
!= IMAGPART_EXPR
)
5540 tree rhs1
= gimple_assign_rhs1 (use_stmt
);
5541 tree use_lhs
= gimple_assign_lhs (use_stmt
);
5542 if (TREE_CODE (rhs1
) != rhs_code
5543 || TREE_OPERAND (rhs1
, 0) != lhs
5544 || TREE_CODE (use_lhs
) != SSA_NAME
5545 || !stmt_interesting_for_vrp (use_stmt
)
5546 || (!INTEGRAL_TYPE_P (TREE_TYPE (use_lhs
))
5547 || !TYPE_MIN_VALUE (TREE_TYPE (use_lhs
))
5548 || !TYPE_MAX_VALUE (TREE_TYPE (use_lhs
))))
5551 /* If there is a change in the value range for any of the
5552 REALPART_EXPR/IMAGPART_EXPR immediate uses, return
5553 SSA_PROP_INTERESTING. If there are any REALPART_EXPR
5554 or IMAGPART_EXPR immediate uses, but none of them have
5555 a change in their value ranges, return
5556 SSA_PROP_NOT_INTERESTING. If there are no
5557 {REAL,IMAG}PART_EXPR uses at all,
5558 return SSA_PROP_VARYING. */
5559 value_range new_vr
= VR_INITIALIZER
;
5560 extract_range_basic (&new_vr
, use_stmt
);
5561 value_range
*old_vr
= get_value_range (use_lhs
);
5562 if (old_vr
->type
!= new_vr
.type
5563 || !vrp_operand_equal_p (old_vr
->min
, new_vr
.min
)
5564 || !vrp_operand_equal_p (old_vr
->max
, new_vr
.max
)
5565 || !vrp_bitmap_equal_p (old_vr
->equiv
, new_vr
.equiv
))
5566 res
= SSA_PROP_INTERESTING
;
5568 res
= SSA_PROP_NOT_INTERESTING
;
5569 BITMAP_FREE (new_vr
.equiv
);
5570 if (res
== SSA_PROP_INTERESTING
)
5584 /* All other statements produce nothing of interest for VRP, so mark
5585 their outputs varying and prevent further simulation. */
5586 set_defs_to_varying (stmt
);
5588 return (*taken_edge_p
) ? SSA_PROP_INTERESTING
: SSA_PROP_VARYING
;
5591 /* Union the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
5592 { VR1TYPE, VR0MIN, VR0MAX } and store the result
5593 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
5594 possible such range. The resulting range is not canonicalized. */
5597 union_ranges (enum value_range_type
*vr0type
,
5598 tree
*vr0min
, tree
*vr0max
,
5599 enum value_range_type vr1type
,
5600 tree vr1min
, tree vr1max
)
5602 bool mineq
= vrp_operand_equal_p (*vr0min
, vr1min
);
5603 bool maxeq
= vrp_operand_equal_p (*vr0max
, vr1max
);
5605 /* [] is vr0, () is vr1 in the following classification comments. */
5609 if (*vr0type
== vr1type
)
5610 /* Nothing to do for equal ranges. */
5612 else if ((*vr0type
== VR_RANGE
5613 && vr1type
== VR_ANTI_RANGE
)
5614 || (*vr0type
== VR_ANTI_RANGE
5615 && vr1type
== VR_RANGE
))
5617 /* For anti-range with range union the result is varying. */
5623 else if (operand_less_p (*vr0max
, vr1min
) == 1
5624 || operand_less_p (vr1max
, *vr0min
) == 1)
5626 /* [ ] ( ) or ( ) [ ]
5627 If the ranges have an empty intersection, result of the union
5628 operation is the anti-range or if both are anti-ranges
5630 if (*vr0type
== VR_ANTI_RANGE
5631 && vr1type
== VR_ANTI_RANGE
)
5633 else if (*vr0type
== VR_ANTI_RANGE
5634 && vr1type
== VR_RANGE
)
5636 else if (*vr0type
== VR_RANGE
5637 && vr1type
== VR_ANTI_RANGE
)
5643 else if (*vr0type
== VR_RANGE
5644 && vr1type
== VR_RANGE
)
5646 /* The result is the convex hull of both ranges. */
5647 if (operand_less_p (*vr0max
, vr1min
) == 1)
5649 /* If the result can be an anti-range, create one. */
5650 if (TREE_CODE (*vr0max
) == INTEGER_CST
5651 && TREE_CODE (vr1min
) == INTEGER_CST
5652 && vrp_val_is_min (*vr0min
)
5653 && vrp_val_is_max (vr1max
))
5655 tree min
= int_const_binop (PLUS_EXPR
,
5657 build_int_cst (TREE_TYPE (*vr0max
), 1));
5658 tree max
= int_const_binop (MINUS_EXPR
,
5660 build_int_cst (TREE_TYPE (vr1min
), 1));
5661 if (!operand_less_p (max
, min
))
5663 *vr0type
= VR_ANTI_RANGE
;
5675 /* If the result can be an anti-range, create one. */
5676 if (TREE_CODE (vr1max
) == INTEGER_CST
5677 && TREE_CODE (*vr0min
) == INTEGER_CST
5678 && vrp_val_is_min (vr1min
)
5679 && vrp_val_is_max (*vr0max
))
5681 tree min
= int_const_binop (PLUS_EXPR
,
5683 build_int_cst (TREE_TYPE (vr1max
), 1));
5684 tree max
= int_const_binop (MINUS_EXPR
,
5686 build_int_cst (TREE_TYPE (*vr0min
), 1));
5687 if (!operand_less_p (max
, min
))
5689 *vr0type
= VR_ANTI_RANGE
;
5703 else if ((maxeq
|| operand_less_p (vr1max
, *vr0max
) == 1)
5704 && (mineq
|| operand_less_p (*vr0min
, vr1min
) == 1))
5706 /* [ ( ) ] or [( ) ] or [ ( )] */
5707 if (*vr0type
== VR_RANGE
5708 && vr1type
== VR_RANGE
)
5710 else if (*vr0type
== VR_ANTI_RANGE
5711 && vr1type
== VR_ANTI_RANGE
)
5717 else if (*vr0type
== VR_ANTI_RANGE
5718 && vr1type
== VR_RANGE
)
5720 /* Arbitrarily choose the right or left gap. */
5721 if (!mineq
&& TREE_CODE (vr1min
) == INTEGER_CST
)
5722 *vr0max
= int_const_binop (MINUS_EXPR
, vr1min
,
5723 build_int_cst (TREE_TYPE (vr1min
), 1));
5724 else if (!maxeq
&& TREE_CODE (vr1max
) == INTEGER_CST
)
5725 *vr0min
= int_const_binop (PLUS_EXPR
, vr1max
,
5726 build_int_cst (TREE_TYPE (vr1max
), 1));
5730 else if (*vr0type
== VR_RANGE
5731 && vr1type
== VR_ANTI_RANGE
)
5732 /* The result covers everything. */
5737 else if ((maxeq
|| operand_less_p (*vr0max
, vr1max
) == 1)
5738 && (mineq
|| operand_less_p (vr1min
, *vr0min
) == 1))
5740 /* ( [ ] ) or ([ ] ) or ( [ ]) */
5741 if (*vr0type
== VR_RANGE
5742 && vr1type
== VR_RANGE
)
5748 else if (*vr0type
== VR_ANTI_RANGE
5749 && vr1type
== VR_ANTI_RANGE
)
5751 else if (*vr0type
== VR_RANGE
5752 && vr1type
== VR_ANTI_RANGE
)
5754 *vr0type
= VR_ANTI_RANGE
;
5755 if (!mineq
&& TREE_CODE (*vr0min
) == INTEGER_CST
)
5757 *vr0max
= int_const_binop (MINUS_EXPR
, *vr0min
,
5758 build_int_cst (TREE_TYPE (*vr0min
), 1));
5761 else if (!maxeq
&& TREE_CODE (*vr0max
) == INTEGER_CST
)
5763 *vr0min
= int_const_binop (PLUS_EXPR
, *vr0max
,
5764 build_int_cst (TREE_TYPE (*vr0max
), 1));
5770 else if (*vr0type
== VR_ANTI_RANGE
5771 && vr1type
== VR_RANGE
)
5772 /* The result covers everything. */
5777 else if ((operand_less_p (vr1min
, *vr0max
) == 1
5778 || operand_equal_p (vr1min
, *vr0max
, 0))
5779 && operand_less_p (*vr0min
, vr1min
) == 1
5780 && operand_less_p (*vr0max
, vr1max
) == 1)
5782 /* [ ( ] ) or [ ]( ) */
5783 if (*vr0type
== VR_RANGE
5784 && vr1type
== VR_RANGE
)
5786 else if (*vr0type
== VR_ANTI_RANGE
5787 && vr1type
== VR_ANTI_RANGE
)
5789 else if (*vr0type
== VR_ANTI_RANGE
5790 && vr1type
== VR_RANGE
)
5792 if (TREE_CODE (vr1min
) == INTEGER_CST
)
5793 *vr0max
= int_const_binop (MINUS_EXPR
, vr1min
,
5794 build_int_cst (TREE_TYPE (vr1min
), 1));
5798 else if (*vr0type
== VR_RANGE
5799 && vr1type
== VR_ANTI_RANGE
)
5801 if (TREE_CODE (*vr0max
) == INTEGER_CST
)
5804 *vr0min
= int_const_binop (PLUS_EXPR
, *vr0max
,
5805 build_int_cst (TREE_TYPE (*vr0max
), 1));
5814 else if ((operand_less_p (*vr0min
, vr1max
) == 1
5815 || operand_equal_p (*vr0min
, vr1max
, 0))
5816 && operand_less_p (vr1min
, *vr0min
) == 1
5817 && operand_less_p (vr1max
, *vr0max
) == 1)
5819 /* ( [ ) ] or ( )[ ] */
5820 if (*vr0type
== VR_RANGE
5821 && vr1type
== VR_RANGE
)
5823 else if (*vr0type
== VR_ANTI_RANGE
5824 && vr1type
== VR_ANTI_RANGE
)
5826 else if (*vr0type
== VR_ANTI_RANGE
5827 && vr1type
== VR_RANGE
)
5829 if (TREE_CODE (vr1max
) == INTEGER_CST
)
5830 *vr0min
= int_const_binop (PLUS_EXPR
, vr1max
,
5831 build_int_cst (TREE_TYPE (vr1max
), 1));
5835 else if (*vr0type
== VR_RANGE
5836 && vr1type
== VR_ANTI_RANGE
)
5838 if (TREE_CODE (*vr0min
) == INTEGER_CST
)
5842 *vr0max
= int_const_binop (MINUS_EXPR
, *vr0min
,
5843 build_int_cst (TREE_TYPE (*vr0min
), 1));
5857 *vr0type
= VR_VARYING
;
5858 *vr0min
= NULL_TREE
;
5859 *vr0max
= NULL_TREE
;
5862 /* Intersect the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
5863 { VR1TYPE, VR0MIN, VR0MAX } and store the result
5864 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
5865 possible such range. The resulting range is not canonicalized. */
5868 intersect_ranges (enum value_range_type
*vr0type
,
5869 tree
*vr0min
, tree
*vr0max
,
5870 enum value_range_type vr1type
,
5871 tree vr1min
, tree vr1max
)
5873 bool mineq
= vrp_operand_equal_p (*vr0min
, vr1min
);
5874 bool maxeq
= vrp_operand_equal_p (*vr0max
, vr1max
);
5876 /* [] is vr0, () is vr1 in the following classification comments. */
5880 if (*vr0type
== vr1type
)
5881 /* Nothing to do for equal ranges. */
5883 else if ((*vr0type
== VR_RANGE
5884 && vr1type
== VR_ANTI_RANGE
)
5885 || (*vr0type
== VR_ANTI_RANGE
5886 && vr1type
== VR_RANGE
))
5888 /* For anti-range with range intersection the result is empty. */
5889 *vr0type
= VR_UNDEFINED
;
5890 *vr0min
= NULL_TREE
;
5891 *vr0max
= NULL_TREE
;
5896 else if (operand_less_p (*vr0max
, vr1min
) == 1
5897 || operand_less_p (vr1max
, *vr0min
) == 1)
5899 /* [ ] ( ) or ( ) [ ]
5900 If the ranges have an empty intersection, the result of the
5901 intersect operation is the range for intersecting an
5902 anti-range with a range or empty when intersecting two ranges. */
5903 if (*vr0type
== VR_RANGE
5904 && vr1type
== VR_ANTI_RANGE
)
5906 else if (*vr0type
== VR_ANTI_RANGE
5907 && vr1type
== VR_RANGE
)
5913 else if (*vr0type
== VR_RANGE
5914 && vr1type
== VR_RANGE
)
5916 *vr0type
= VR_UNDEFINED
;
5917 *vr0min
= NULL_TREE
;
5918 *vr0max
= NULL_TREE
;
5920 else if (*vr0type
== VR_ANTI_RANGE
5921 && vr1type
== VR_ANTI_RANGE
)
5923 /* If the anti-ranges are adjacent to each other merge them. */
5924 if (TREE_CODE (*vr0max
) == INTEGER_CST
5925 && TREE_CODE (vr1min
) == INTEGER_CST
5926 && operand_less_p (*vr0max
, vr1min
) == 1
5927 && integer_onep (int_const_binop (MINUS_EXPR
,
5930 else if (TREE_CODE (vr1max
) == INTEGER_CST
5931 && TREE_CODE (*vr0min
) == INTEGER_CST
5932 && operand_less_p (vr1max
, *vr0min
) == 1
5933 && integer_onep (int_const_binop (MINUS_EXPR
,
5936 /* Else arbitrarily take VR0. */
5939 else if ((maxeq
|| operand_less_p (vr1max
, *vr0max
) == 1)
5940 && (mineq
|| operand_less_p (*vr0min
, vr1min
) == 1))
5942 /* [ ( ) ] or [( ) ] or [ ( )] */
5943 if (*vr0type
== VR_RANGE
5944 && vr1type
== VR_RANGE
)
5946 /* If both are ranges the result is the inner one. */
5951 else if (*vr0type
== VR_RANGE
5952 && vr1type
== VR_ANTI_RANGE
)
5954 /* Choose the right gap if the left one is empty. */
5957 if (TREE_CODE (vr1max
) != INTEGER_CST
)
5959 else if (TYPE_PRECISION (TREE_TYPE (vr1max
)) == 1
5960 && !TYPE_UNSIGNED (TREE_TYPE (vr1max
)))
5962 = int_const_binop (MINUS_EXPR
, vr1max
,
5963 build_int_cst (TREE_TYPE (vr1max
), -1));
5966 = int_const_binop (PLUS_EXPR
, vr1max
,
5967 build_int_cst (TREE_TYPE (vr1max
), 1));
5969 /* Choose the left gap if the right one is empty. */
5972 if (TREE_CODE (vr1min
) != INTEGER_CST
)
5974 else if (TYPE_PRECISION (TREE_TYPE (vr1min
)) == 1
5975 && !TYPE_UNSIGNED (TREE_TYPE (vr1min
)))
5977 = int_const_binop (PLUS_EXPR
, vr1min
,
5978 build_int_cst (TREE_TYPE (vr1min
), -1));
5981 = int_const_binop (MINUS_EXPR
, vr1min
,
5982 build_int_cst (TREE_TYPE (vr1min
), 1));
5984 /* Choose the anti-range if the range is effectively varying. */
5985 else if (vrp_val_is_min (*vr0min
)
5986 && vrp_val_is_max (*vr0max
))
5992 /* Else choose the range. */
5994 else if (*vr0type
== VR_ANTI_RANGE
5995 && vr1type
== VR_ANTI_RANGE
)
5996 /* If both are anti-ranges the result is the outer one. */
5998 else if (*vr0type
== VR_ANTI_RANGE
5999 && vr1type
== VR_RANGE
)
6001 /* The intersection is empty. */
6002 *vr0type
= VR_UNDEFINED
;
6003 *vr0min
= NULL_TREE
;
6004 *vr0max
= NULL_TREE
;
6009 else if ((maxeq
|| operand_less_p (*vr0max
, vr1max
) == 1)
6010 && (mineq
|| operand_less_p (vr1min
, *vr0min
) == 1))
6012 /* ( [ ] ) or ([ ] ) or ( [ ]) */
6013 if (*vr0type
== VR_RANGE
6014 && vr1type
== VR_RANGE
)
6015 /* Choose the inner range. */
6017 else if (*vr0type
== VR_ANTI_RANGE
6018 && vr1type
== VR_RANGE
)
6020 /* Choose the right gap if the left is empty. */
6023 *vr0type
= VR_RANGE
;
6024 if (TREE_CODE (*vr0max
) != INTEGER_CST
)
6026 else if (TYPE_PRECISION (TREE_TYPE (*vr0max
)) == 1
6027 && !TYPE_UNSIGNED (TREE_TYPE (*vr0max
)))
6029 = int_const_binop (MINUS_EXPR
, *vr0max
,
6030 build_int_cst (TREE_TYPE (*vr0max
), -1));
6033 = int_const_binop (PLUS_EXPR
, *vr0max
,
6034 build_int_cst (TREE_TYPE (*vr0max
), 1));
6037 /* Choose the left gap if the right is empty. */
6040 *vr0type
= VR_RANGE
;
6041 if (TREE_CODE (*vr0min
) != INTEGER_CST
)
6043 else if (TYPE_PRECISION (TREE_TYPE (*vr0min
)) == 1
6044 && !TYPE_UNSIGNED (TREE_TYPE (*vr0min
)))
6046 = int_const_binop (PLUS_EXPR
, *vr0min
,
6047 build_int_cst (TREE_TYPE (*vr0min
), -1));
6050 = int_const_binop (MINUS_EXPR
, *vr0min
,
6051 build_int_cst (TREE_TYPE (*vr0min
), 1));
6054 /* Choose the anti-range if the range is effectively varying. */
6055 else if (vrp_val_is_min (vr1min
)
6056 && vrp_val_is_max (vr1max
))
6058 /* Choose the anti-range if it is ~[0,0], that range is special
6059 enough to special case when vr1's range is relatively wide.
6060 At least for types bigger than int - this covers pointers
6061 and arguments to functions like ctz. */
6062 else if (*vr0min
== *vr0max
6063 && integer_zerop (*vr0min
)
6064 && ((TYPE_PRECISION (TREE_TYPE (*vr0min
))
6065 >= TYPE_PRECISION (integer_type_node
))
6066 || POINTER_TYPE_P (TREE_TYPE (*vr0min
)))
6067 && TREE_CODE (vr1max
) == INTEGER_CST
6068 && TREE_CODE (vr1min
) == INTEGER_CST
6069 && (wi::clz (wi::to_wide (vr1max
) - wi::to_wide (vr1min
))
6070 < TYPE_PRECISION (TREE_TYPE (*vr0min
)) / 2))
6072 /* Else choose the range. */
6080 else if (*vr0type
== VR_ANTI_RANGE
6081 && vr1type
== VR_ANTI_RANGE
)
6083 /* If both are anti-ranges the result is the outer one. */
6088 else if (vr1type
== VR_ANTI_RANGE
6089 && *vr0type
== VR_RANGE
)
6091 /* The intersection is empty. */
6092 *vr0type
= VR_UNDEFINED
;
6093 *vr0min
= NULL_TREE
;
6094 *vr0max
= NULL_TREE
;
6099 else if ((operand_less_p (vr1min
, *vr0max
) == 1
6100 || operand_equal_p (vr1min
, *vr0max
, 0))
6101 && operand_less_p (*vr0min
, vr1min
) == 1)
6103 /* [ ( ] ) or [ ]( ) */
6104 if (*vr0type
== VR_ANTI_RANGE
6105 && vr1type
== VR_ANTI_RANGE
)
6107 else if (*vr0type
== VR_RANGE
6108 && vr1type
== VR_RANGE
)
6110 else if (*vr0type
== VR_RANGE
6111 && vr1type
== VR_ANTI_RANGE
)
6113 if (TREE_CODE (vr1min
) == INTEGER_CST
)
6114 *vr0max
= int_const_binop (MINUS_EXPR
, vr1min
,
6115 build_int_cst (TREE_TYPE (vr1min
), 1));
6119 else if (*vr0type
== VR_ANTI_RANGE
6120 && vr1type
== VR_RANGE
)
6122 *vr0type
= VR_RANGE
;
6123 if (TREE_CODE (*vr0max
) == INTEGER_CST
)
6124 *vr0min
= int_const_binop (PLUS_EXPR
, *vr0max
,
6125 build_int_cst (TREE_TYPE (*vr0max
), 1));
6133 else if ((operand_less_p (*vr0min
, vr1max
) == 1
6134 || operand_equal_p (*vr0min
, vr1max
, 0))
6135 && operand_less_p (vr1min
, *vr0min
) == 1)
6137 /* ( [ ) ] or ( )[ ] */
6138 if (*vr0type
== VR_ANTI_RANGE
6139 && vr1type
== VR_ANTI_RANGE
)
6141 else if (*vr0type
== VR_RANGE
6142 && vr1type
== VR_RANGE
)
6144 else if (*vr0type
== VR_RANGE
6145 && vr1type
== VR_ANTI_RANGE
)
6147 if (TREE_CODE (vr1max
) == INTEGER_CST
)
6148 *vr0min
= int_const_binop (PLUS_EXPR
, vr1max
,
6149 build_int_cst (TREE_TYPE (vr1max
), 1));
6153 else if (*vr0type
== VR_ANTI_RANGE
6154 && vr1type
== VR_RANGE
)
6156 *vr0type
= VR_RANGE
;
6157 if (TREE_CODE (*vr0min
) == INTEGER_CST
)
6158 *vr0max
= int_const_binop (MINUS_EXPR
, *vr0min
,
6159 build_int_cst (TREE_TYPE (*vr0min
), 1));
6168 /* As a fallback simply use { *VRTYPE, *VR0MIN, *VR0MAX } as
6169 result for the intersection. That's always a conservative
6170 correct estimate unless VR1 is a constant singleton range
6171 in which case we choose that. */
6172 if (vr1type
== VR_RANGE
6173 && is_gimple_min_invariant (vr1min
)
6174 && vrp_operand_equal_p (vr1min
, vr1max
))
6185 /* Intersect the two value-ranges *VR0 and *VR1 and store the result
6186 in *VR0. This may not be the smallest possible such range. */
6189 vrp_intersect_ranges_1 (value_range
*vr0
, value_range
*vr1
)
6193 /* If either range is VR_VARYING the other one wins. */
6194 if (vr1
->type
== VR_VARYING
)
6196 if (vr0
->type
== VR_VARYING
)
6198 copy_value_range (vr0
, vr1
);
6202 /* When either range is VR_UNDEFINED the resulting range is
6203 VR_UNDEFINED, too. */
6204 if (vr0
->type
== VR_UNDEFINED
)
6206 if (vr1
->type
== VR_UNDEFINED
)
6208 set_value_range_to_undefined (vr0
);
6212 /* Save the original vr0 so we can return it as conservative intersection
6213 result when our worker turns things to varying. */
6215 intersect_ranges (&vr0
->type
, &vr0
->min
, &vr0
->max
,
6216 vr1
->type
, vr1
->min
, vr1
->max
);
6217 /* Make sure to canonicalize the result though as the inversion of a
6218 VR_RANGE can still be a VR_RANGE. */
6219 set_and_canonicalize_value_range (vr0
, vr0
->type
,
6220 vr0
->min
, vr0
->max
, vr0
->equiv
);
6221 /* If that failed, use the saved original VR0. */
6222 if (vr0
->type
== VR_VARYING
)
6227 /* If the result is VR_UNDEFINED there is no need to mess with
6228 the equivalencies. */
6229 if (vr0
->type
== VR_UNDEFINED
)
6232 /* The resulting set of equivalences for range intersection is the union of
6234 if (vr0
->equiv
&& vr1
->equiv
&& vr0
->equiv
!= vr1
->equiv
)
6235 bitmap_ior_into (vr0
->equiv
, vr1
->equiv
);
6236 else if (vr1
->equiv
&& !vr0
->equiv
)
6238 /* All equivalence bitmaps are allocated from the same obstack. So
6239 we can use the obstack associated with VR to allocate vr0->equiv. */
6240 vr0
->equiv
= BITMAP_ALLOC (vr1
->equiv
->obstack
);
6241 bitmap_copy (vr0
->equiv
, vr1
->equiv
);
6246 vrp_intersect_ranges (value_range
*vr0
, value_range
*vr1
)
6248 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6250 fprintf (dump_file
, "Intersecting\n ");
6251 dump_value_range (dump_file
, vr0
);
6252 fprintf (dump_file
, "\nand\n ");
6253 dump_value_range (dump_file
, vr1
);
6254 fprintf (dump_file
, "\n");
6256 vrp_intersect_ranges_1 (vr0
, vr1
);
6257 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6259 fprintf (dump_file
, "to\n ");
6260 dump_value_range (dump_file
, vr0
);
6261 fprintf (dump_file
, "\n");
6265 /* Meet operation for value ranges. Given two value ranges VR0 and
6266 VR1, store in VR0 a range that contains both VR0 and VR1. This
6267 may not be the smallest possible such range. */
6270 vrp_meet_1 (value_range
*vr0
, const value_range
*vr1
)
6274 if (vr0
->type
== VR_UNDEFINED
)
6276 set_value_range (vr0
, vr1
->type
, vr1
->min
, vr1
->max
, vr1
->equiv
);
6280 if (vr1
->type
== VR_UNDEFINED
)
6282 /* VR0 already has the resulting range. */
6286 if (vr0
->type
== VR_VARYING
)
6288 /* Nothing to do. VR0 already has the resulting range. */
6292 if (vr1
->type
== VR_VARYING
)
6294 set_value_range_to_varying (vr0
);
6299 union_ranges (&vr0
->type
, &vr0
->min
, &vr0
->max
,
6300 vr1
->type
, vr1
->min
, vr1
->max
);
6301 if (vr0
->type
== VR_VARYING
)
6303 /* Failed to find an efficient meet. Before giving up and setting
6304 the result to VARYING, see if we can at least derive a useful
6305 anti-range. FIXME, all this nonsense about distinguishing
6306 anti-ranges from ranges is necessary because of the odd
6307 semantics of range_includes_zero_p and friends. */
6308 if (((saved
.type
== VR_RANGE
6309 && range_includes_zero_p (saved
.min
, saved
.max
) == 0)
6310 || (saved
.type
== VR_ANTI_RANGE
6311 && range_includes_zero_p (saved
.min
, saved
.max
) == 1))
6312 && ((vr1
->type
== VR_RANGE
6313 && range_includes_zero_p (vr1
->min
, vr1
->max
) == 0)
6314 || (vr1
->type
== VR_ANTI_RANGE
6315 && range_includes_zero_p (vr1
->min
, vr1
->max
) == 1)))
6317 set_value_range_to_nonnull (vr0
, TREE_TYPE (saved
.min
));
6319 /* Since this meet operation did not result from the meeting of
6320 two equivalent names, VR0 cannot have any equivalences. */
6322 bitmap_clear (vr0
->equiv
);
6326 set_value_range_to_varying (vr0
);
6329 set_and_canonicalize_value_range (vr0
, vr0
->type
, vr0
->min
, vr0
->max
,
6331 if (vr0
->type
== VR_VARYING
)
6334 /* The resulting set of equivalences is always the intersection of
6336 if (vr0
->equiv
&& vr1
->equiv
&& vr0
->equiv
!= vr1
->equiv
)
6337 bitmap_and_into (vr0
->equiv
, vr1
->equiv
);
6338 else if (vr0
->equiv
&& !vr1
->equiv
)
6339 bitmap_clear (vr0
->equiv
);
6343 vrp_meet (value_range
*vr0
, const value_range
*vr1
)
6345 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6347 fprintf (dump_file
, "Meeting\n ");
6348 dump_value_range (dump_file
, vr0
);
6349 fprintf (dump_file
, "\nand\n ");
6350 dump_value_range (dump_file
, vr1
);
6351 fprintf (dump_file
, "\n");
6353 vrp_meet_1 (vr0
, vr1
);
6354 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6356 fprintf (dump_file
, "to\n ");
6357 dump_value_range (dump_file
, vr0
);
6358 fprintf (dump_file
, "\n");
6363 /* Visit all arguments for PHI node PHI that flow through executable
6364 edges. If a valid value range can be derived from all the incoming
6365 value ranges, set a new range for the LHS of PHI. */
6367 enum ssa_prop_result
6368 vrp_prop::visit_phi (gphi
*phi
)
6370 tree lhs
= PHI_RESULT (phi
);
6371 value_range vr_result
= VR_INITIALIZER
;
6372 extract_range_from_phi_node (phi
, &vr_result
);
6373 if (update_value_range (lhs
, &vr_result
))
6375 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6377 fprintf (dump_file
, "Found new range for ");
6378 print_generic_expr (dump_file
, lhs
);
6379 fprintf (dump_file
, ": ");
6380 dump_value_range (dump_file
, &vr_result
);
6381 fprintf (dump_file
, "\n");
6384 if (vr_result
.type
== VR_VARYING
)
6385 return SSA_PROP_VARYING
;
6387 return SSA_PROP_INTERESTING
;
6390 /* Nothing changed, don't add outgoing edges. */
6391 return SSA_PROP_NOT_INTERESTING
;
6394 class vrp_folder
: public substitute_and_fold_engine
6397 tree
get_value (tree
) FINAL OVERRIDE
;
6398 bool fold_stmt (gimple_stmt_iterator
*) FINAL OVERRIDE
;
6399 bool fold_predicate_in (gimple_stmt_iterator
*);
6401 class vr_values
*vr_values
;
6404 tree
vrp_evaluate_conditional (tree_code code
, tree op0
,
6405 tree op1
, gimple
*stmt
)
6406 { return vr_values
->vrp_evaluate_conditional (code
, op0
, op1
, stmt
); }
6407 bool simplify_stmt_using_ranges (gimple_stmt_iterator
*gsi
)
6408 { return vr_values
->simplify_stmt_using_ranges (gsi
); }
6409 tree
op_with_constant_singleton_value_range (tree op
)
6410 { return vr_values
->op_with_constant_singleton_value_range (op
); }
6413 /* If the statement pointed by SI has a predicate whose value can be
6414 computed using the value range information computed by VRP, compute
6415 its value and return true. Otherwise, return false. */
6418 vrp_folder::fold_predicate_in (gimple_stmt_iterator
*si
)
6420 bool assignment_p
= false;
6422 gimple
*stmt
= gsi_stmt (*si
);
6424 if (is_gimple_assign (stmt
)
6425 && TREE_CODE_CLASS (gimple_assign_rhs_code (stmt
)) == tcc_comparison
)
6427 assignment_p
= true;
6428 val
= vrp_evaluate_conditional (gimple_assign_rhs_code (stmt
),
6429 gimple_assign_rhs1 (stmt
),
6430 gimple_assign_rhs2 (stmt
),
6433 else if (gcond
*cond_stmt
= dyn_cast
<gcond
*> (stmt
))
6434 val
= vrp_evaluate_conditional (gimple_cond_code (cond_stmt
),
6435 gimple_cond_lhs (cond_stmt
),
6436 gimple_cond_rhs (cond_stmt
),
6444 val
= fold_convert (gimple_expr_type (stmt
), val
);
6448 fprintf (dump_file
, "Folding predicate ");
6449 print_gimple_expr (dump_file
, stmt
, 0);
6450 fprintf (dump_file
, " to ");
6451 print_generic_expr (dump_file
, val
);
6452 fprintf (dump_file
, "\n");
6455 if (is_gimple_assign (stmt
))
6456 gimple_assign_set_rhs_from_tree (si
, val
);
6459 gcc_assert (gimple_code (stmt
) == GIMPLE_COND
);
6460 gcond
*cond_stmt
= as_a
<gcond
*> (stmt
);
6461 if (integer_zerop (val
))
6462 gimple_cond_make_false (cond_stmt
);
6463 else if (integer_onep (val
))
6464 gimple_cond_make_true (cond_stmt
);
6475 /* Callback for substitute_and_fold folding the stmt at *SI. */
6478 vrp_folder::fold_stmt (gimple_stmt_iterator
*si
)
6480 if (fold_predicate_in (si
))
6483 return simplify_stmt_using_ranges (si
);
6486 /* If OP has a value range with a single constant value return that,
6487 otherwise return NULL_TREE. This returns OP itself if OP is a
6490 Implemented as a pure wrapper right now, but this will change. */
6493 vrp_folder::get_value (tree op
)
6495 return op_with_constant_singleton_value_range (op
);
6498 /* Return the LHS of any ASSERT_EXPR where OP appears as the first
6499 argument to the ASSERT_EXPR and in which the ASSERT_EXPR dominates
6500 BB. If no such ASSERT_EXPR is found, return OP. */
6503 lhs_of_dominating_assert (tree op
, basic_block bb
, gimple
*stmt
)
6505 imm_use_iterator imm_iter
;
6507 use_operand_p use_p
;
6509 if (TREE_CODE (op
) == SSA_NAME
)
6511 FOR_EACH_IMM_USE_FAST (use_p
, imm_iter
, op
)
6513 use_stmt
= USE_STMT (use_p
);
6514 if (use_stmt
!= stmt
6515 && gimple_assign_single_p (use_stmt
)
6516 && TREE_CODE (gimple_assign_rhs1 (use_stmt
)) == ASSERT_EXPR
6517 && TREE_OPERAND (gimple_assign_rhs1 (use_stmt
), 0) == op
6518 && dominated_by_p (CDI_DOMINATORS
, bb
, gimple_bb (use_stmt
)))
6519 return gimple_assign_lhs (use_stmt
);
6526 static class vr_values
*x_vr_values
;
6528 /* A trivial wrapper so that we can present the generic jump threading
6529 code with a simple API for simplifying statements. STMT is the
6530 statement we want to simplify, WITHIN_STMT provides the location
6531 for any overflow warnings. */
6534 simplify_stmt_for_jump_threading (gimple
*stmt
, gimple
*within_stmt
,
6535 class avail_exprs_stack
*avail_exprs_stack ATTRIBUTE_UNUSED
,
6538 /* First see if the conditional is in the hash table. */
6539 tree cached_lhs
= avail_exprs_stack
->lookup_avail_expr (stmt
, false, true);
6540 if (cached_lhs
&& is_gimple_min_invariant (cached_lhs
))
6543 vr_values
*vr_values
= x_vr_values
;
6544 if (gcond
*cond_stmt
= dyn_cast
<gcond
*> (stmt
))
6546 tree op0
= gimple_cond_lhs (cond_stmt
);
6547 op0
= lhs_of_dominating_assert (op0
, bb
, stmt
);
6549 tree op1
= gimple_cond_rhs (cond_stmt
);
6550 op1
= lhs_of_dominating_assert (op1
, bb
, stmt
);
6552 return vr_values
->vrp_evaluate_conditional (gimple_cond_code (cond_stmt
),
6553 op0
, op1
, within_stmt
);
6556 /* We simplify a switch statement by trying to determine which case label
6557 will be taken. If we are successful then we return the corresponding
6559 if (gswitch
*switch_stmt
= dyn_cast
<gswitch
*> (stmt
))
6561 tree op
= gimple_switch_index (switch_stmt
);
6562 if (TREE_CODE (op
) != SSA_NAME
)
6565 op
= lhs_of_dominating_assert (op
, bb
, stmt
);
6567 value_range
*vr
= vr_values
->get_value_range (op
);
6568 if ((vr
->type
!= VR_RANGE
&& vr
->type
!= VR_ANTI_RANGE
)
6569 || symbolic_range_p (vr
))
6572 if (vr
->type
== VR_RANGE
)
6575 /* Get the range of labels that contain a part of the operand's
6577 find_case_label_range (switch_stmt
, vr
->min
, vr
->max
, &i
, &j
);
6579 /* Is there only one such label? */
6582 tree label
= gimple_switch_label (switch_stmt
, i
);
6584 /* The i'th label will be taken only if the value range of the
6585 operand is entirely within the bounds of this label. */
6586 if (CASE_HIGH (label
) != NULL_TREE
6587 ? (tree_int_cst_compare (CASE_LOW (label
), vr
->min
) <= 0
6588 && tree_int_cst_compare (CASE_HIGH (label
), vr
->max
) >= 0)
6589 : (tree_int_cst_equal (CASE_LOW (label
), vr
->min
)
6590 && tree_int_cst_equal (vr
->min
, vr
->max
)))
6594 /* If there are no such labels then the default label will be
6597 return gimple_switch_label (switch_stmt
, 0);
6600 if (vr
->type
== VR_ANTI_RANGE
)
6602 unsigned n
= gimple_switch_num_labels (switch_stmt
);
6603 tree min_label
= gimple_switch_label (switch_stmt
, 1);
6604 tree max_label
= gimple_switch_label (switch_stmt
, n
- 1);
6606 /* The default label will be taken only if the anti-range of the
6607 operand is entirely outside the bounds of all the (non-default)
6609 if (tree_int_cst_compare (vr
->min
, CASE_LOW (min_label
)) <= 0
6610 && (CASE_HIGH (max_label
) != NULL_TREE
6611 ? tree_int_cst_compare (vr
->max
, CASE_HIGH (max_label
)) >= 0
6612 : tree_int_cst_compare (vr
->max
, CASE_LOW (max_label
)) >= 0))
6613 return gimple_switch_label (switch_stmt
, 0);
6619 if (gassign
*assign_stmt
= dyn_cast
<gassign
*> (stmt
))
6621 tree lhs
= gimple_assign_lhs (assign_stmt
);
6622 if (TREE_CODE (lhs
) == SSA_NAME
6623 && (INTEGRAL_TYPE_P (TREE_TYPE (lhs
))
6624 || POINTER_TYPE_P (TREE_TYPE (lhs
)))
6625 && stmt_interesting_for_vrp (stmt
))
6629 value_range new_vr
= VR_INITIALIZER
;
6630 vr_values
->extract_range_from_stmt (stmt
, &dummy_e
,
6631 &dummy_tree
, &new_vr
);
6632 if (range_int_cst_singleton_p (&new_vr
))
6640 class vrp_dom_walker
: public dom_walker
6643 vrp_dom_walker (cdi_direction direction
,
6644 class const_and_copies
*const_and_copies
,
6645 class avail_exprs_stack
*avail_exprs_stack
)
6646 : dom_walker (direction
, true),
6647 m_const_and_copies (const_and_copies
),
6648 m_avail_exprs_stack (avail_exprs_stack
),
6649 m_dummy_cond (NULL
) {}
6651 virtual edge
before_dom_children (basic_block
);
6652 virtual void after_dom_children (basic_block
);
6654 class vr_values
*vr_values
;
6657 class const_and_copies
*m_const_and_copies
;
6658 class avail_exprs_stack
*m_avail_exprs_stack
;
6660 gcond
*m_dummy_cond
;
6664 /* Called before processing dominator children of BB. We want to look
6665 at ASSERT_EXPRs and record information from them in the appropriate
6668 We could look at other statements here. It's not seen as likely
6669 to significantly increase the jump threads we discover. */
6672 vrp_dom_walker::before_dom_children (basic_block bb
)
6674 gimple_stmt_iterator gsi
;
6676 m_avail_exprs_stack
->push_marker ();
6677 m_const_and_copies
->push_marker ();
6678 for (gsi
= gsi_start_nondebug_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
6680 gimple
*stmt
= gsi_stmt (gsi
);
6681 if (gimple_assign_single_p (stmt
)
6682 && TREE_CODE (gimple_assign_rhs1 (stmt
)) == ASSERT_EXPR
)
6684 tree rhs1
= gimple_assign_rhs1 (stmt
);
6685 tree cond
= TREE_OPERAND (rhs1
, 1);
6686 tree inverted
= invert_truthvalue (cond
);
6687 vec
<cond_equivalence
> p
;
6689 record_conditions (&p
, cond
, inverted
);
6690 for (unsigned int i
= 0; i
< p
.length (); i
++)
6691 m_avail_exprs_stack
->record_cond (&p
[i
]);
6693 tree lhs
= gimple_assign_lhs (stmt
);
6694 m_const_and_copies
->record_const_or_copy (lhs
,
6695 TREE_OPERAND (rhs1
, 0));
6704 /* Called after processing dominator children of BB. This is where we
6705 actually call into the threader. */
6707 vrp_dom_walker::after_dom_children (basic_block bb
)
6710 m_dummy_cond
= gimple_build_cond (NE_EXPR
,
6711 integer_zero_node
, integer_zero_node
,
6714 x_vr_values
= vr_values
;
6715 thread_outgoing_edges (bb
, m_dummy_cond
, m_const_and_copies
,
6716 m_avail_exprs_stack
, NULL
,
6717 simplify_stmt_for_jump_threading
);
6720 m_avail_exprs_stack
->pop_to_marker ();
6721 m_const_and_copies
->pop_to_marker ();
6724 /* Blocks which have more than one predecessor and more than
6725 one successor present jump threading opportunities, i.e.,
6726 when the block is reached from a specific predecessor, we
6727 may be able to determine which of the outgoing edges will
6728 be traversed. When this optimization applies, we are able
6729 to avoid conditionals at runtime and we may expose secondary
6730 optimization opportunities.
6732 This routine is effectively a driver for the generic jump
6733 threading code. It basically just presents the generic code
6734 with edges that may be suitable for jump threading.
6736 Unlike DOM, we do not iterate VRP if jump threading was successful.
6737 While iterating may expose new opportunities for VRP, it is expected
6738 those opportunities would be very limited and the compile time cost
6739 to expose those opportunities would be significant.
6741 As jump threading opportunities are discovered, they are registered
6742 for later realization. */
6745 identify_jump_threads (class vr_values
*vr_values
)
6750 /* Ugh. When substituting values earlier in this pass we can
6751 wipe the dominance information. So rebuild the dominator
6752 information as we need it within the jump threading code. */
6753 calculate_dominance_info (CDI_DOMINATORS
);
6755 /* We do not allow VRP information to be used for jump threading
6756 across a back edge in the CFG. Otherwise it becomes too
6757 difficult to avoid eliminating loop exit tests. Of course
6758 EDGE_DFS_BACK is not accurate at this time so we have to
6760 mark_dfs_back_edges ();
6762 /* Do not thread across edges we are about to remove. Just marking
6763 them as EDGE_IGNORE will do. */
6764 FOR_EACH_VEC_ELT (to_remove_edges
, i
, e
)
6765 e
->flags
|= EDGE_IGNORE
;
6767 /* Allocate our unwinder stack to unwind any temporary equivalences
6768 that might be recorded. */
6769 const_and_copies
*equiv_stack
= new const_and_copies ();
6771 hash_table
<expr_elt_hasher
> *avail_exprs
6772 = new hash_table
<expr_elt_hasher
> (1024);
6773 avail_exprs_stack
*avail_exprs_stack
6774 = new class avail_exprs_stack (avail_exprs
);
6776 vrp_dom_walker
walker (CDI_DOMINATORS
, equiv_stack
, avail_exprs_stack
);
6777 walker
.vr_values
= vr_values
;
6778 walker
.walk (cfun
->cfg
->x_entry_block_ptr
);
6780 /* Clear EDGE_IGNORE. */
6781 FOR_EACH_VEC_ELT (to_remove_edges
, i
, e
)
6782 e
->flags
&= ~EDGE_IGNORE
;
6784 /* We do not actually update the CFG or SSA graphs at this point as
6785 ASSERT_EXPRs are still in the IL and cfg cleanup code does not yet
6786 handle ASSERT_EXPRs gracefully. */
6789 delete avail_exprs_stack
;
6792 /* Traverse all the blocks folding conditionals with known ranges. */
6795 vrp_prop::vrp_finalize (bool warn_array_bounds_p
)
6799 /* We have completed propagating through the lattice. */
6800 vr_values
.set_lattice_propagation_complete ();
6804 fprintf (dump_file
, "\nValue ranges after VRP:\n\n");
6805 vr_values
.dump_all_value_ranges (dump_file
);
6806 fprintf (dump_file
, "\n");
6809 /* Set value range to non pointer SSA_NAMEs. */
6810 for (i
= 0; i
< num_ssa_names
; i
++)
6812 tree name
= ssa_name (i
);
6816 value_range
*vr
= get_value_range (name
);
6818 || (vr
->type
== VR_VARYING
)
6819 || (vr
->type
== VR_UNDEFINED
)
6820 || (TREE_CODE (vr
->min
) != INTEGER_CST
)
6821 || (TREE_CODE (vr
->max
) != INTEGER_CST
))
6824 if (POINTER_TYPE_P (TREE_TYPE (name
))
6825 && ((vr
->type
== VR_RANGE
6826 && range_includes_zero_p (vr
->min
, vr
->max
) == 0)
6827 || (vr
->type
== VR_ANTI_RANGE
6828 && range_includes_zero_p (vr
->min
, vr
->max
) == 1)))
6829 set_ptr_nonnull (name
);
6830 else if (!POINTER_TYPE_P (TREE_TYPE (name
)))
6831 set_range_info (name
, vr
->type
,
6832 wi::to_wide (vr
->min
),
6833 wi::to_wide (vr
->max
));
6836 class vrp_folder vrp_folder
;
6837 vrp_folder
.vr_values
= &vr_values
;
6838 vrp_folder
.substitute_and_fold ();
6840 if (warn_array_bounds
&& warn_array_bounds_p
)
6841 check_all_array_refs ();
6844 /* Main entry point to VRP (Value Range Propagation). This pass is
6845 loosely based on J. R. C. Patterson, ``Accurate Static Branch
6846 Prediction by Value Range Propagation,'' in SIGPLAN Conference on
6847 Programming Language Design and Implementation, pp. 67-78, 1995.
6848 Also available at http://citeseer.ist.psu.edu/patterson95accurate.html
6850 This is essentially an SSA-CCP pass modified to deal with ranges
6851 instead of constants.
6853 While propagating ranges, we may find that two or more SSA name
6854 have equivalent, though distinct ranges. For instance,
6857 2 p_4 = ASSERT_EXPR <p_3, p_3 != 0>
6859 4 p_5 = ASSERT_EXPR <p_4, p_4 == q_2>;
6863 In the code above, pointer p_5 has range [q_2, q_2], but from the
6864 code we can also determine that p_5 cannot be NULL and, if q_2 had
6865 a non-varying range, p_5's range should also be compatible with it.
6867 These equivalences are created by two expressions: ASSERT_EXPR and
6868 copy operations. Since p_5 is an assertion on p_4, and p_4 was the
6869 result of another assertion, then we can use the fact that p_5 and
6870 p_4 are equivalent when evaluating p_5's range.
6872 Together with value ranges, we also propagate these equivalences
6873 between names so that we can take advantage of information from
6874 multiple ranges when doing final replacement. Note that this
6875 equivalency relation is transitive but not symmetric.
6877 In the example above, p_5 is equivalent to p_4, q_2 and p_3, but we
6878 cannot assert that q_2 is equivalent to p_5 because q_2 may be used
6879 in contexts where that assertion does not hold (e.g., in line 6).
6881 TODO, the main difference between this pass and Patterson's is that
6882 we do not propagate edge probabilities. We only compute whether
6883 edges can be taken or not. That is, instead of having a spectrum
6884 of jump probabilities between 0 and 1, we only deal with 0, 1 and
6885 DON'T KNOW. In the future, it may be worthwhile to propagate
6886 probabilities to aid branch prediction. */
6889 execute_vrp (bool warn_array_bounds_p
)
6895 loop_optimizer_init (LOOPS_NORMAL
| LOOPS_HAVE_RECORDED_EXITS
);
6896 rewrite_into_loop_closed_ssa (NULL
, TODO_update_ssa
);
6899 /* ??? This ends up using stale EDGE_DFS_BACK for liveness computation.
6900 Inserting assertions may split edges which will invalidate
6902 insert_range_assertions ();
6904 to_remove_edges
.create (10);
6905 to_update_switch_stmts
.create (5);
6906 threadedge_initialize_values ();
6908 /* For visiting PHI nodes we need EDGE_DFS_BACK computed. */
6909 mark_dfs_back_edges ();
6911 class vrp_prop vrp_prop
;
6912 vrp_prop
.vrp_initialize ();
6913 vrp_prop
.ssa_propagate ();
6914 vrp_prop
.vrp_finalize (warn_array_bounds_p
);
6916 /* We must identify jump threading opportunities before we release
6917 the datastructures built by VRP. */
6918 identify_jump_threads (&vrp_prop
.vr_values
);
6920 /* A comparison of an SSA_NAME against a constant where the SSA_NAME
6921 was set by a type conversion can often be rewritten to use the
6922 RHS of the type conversion.
6924 However, doing so inhibits jump threading through the comparison.
6925 So that transformation is not performed until after jump threading
6928 FOR_EACH_BB_FN (bb
, cfun
)
6930 gimple
*last
= last_stmt (bb
);
6931 if (last
&& gimple_code (last
) == GIMPLE_COND
)
6932 vrp_prop
.vr_values
.simplify_cond_using_ranges_2 (as_a
<gcond
*> (last
));
6935 free_numbers_of_iterations_estimates (cfun
);
6937 /* ASSERT_EXPRs must be removed before finalizing jump threads
6938 as finalizing jump threads calls the CFG cleanup code which
6939 does not properly handle ASSERT_EXPRs. */
6940 remove_range_assertions ();
6942 /* If we exposed any new variables, go ahead and put them into
6943 SSA form now, before we handle jump threading. This simplifies
6944 interactions between rewriting of _DECL nodes into SSA form
6945 and rewriting SSA_NAME nodes into SSA form after block
6946 duplication and CFG manipulation. */
6947 update_ssa (TODO_update_ssa
);
6949 /* We identified all the jump threading opportunities earlier, but could
6950 not transform the CFG at that time. This routine transforms the
6951 CFG and arranges for the dominator tree to be rebuilt if necessary.
6953 Note the SSA graph update will occur during the normal TODO
6954 processing by the pass manager. */
6955 thread_through_all_blocks (false);
6957 /* Remove dead edges from SWITCH_EXPR optimization. This leaves the
6958 CFG in a broken state and requires a cfg_cleanup run. */
6959 FOR_EACH_VEC_ELT (to_remove_edges
, i
, e
)
6961 /* Update SWITCH_EXPR case label vector. */
6962 FOR_EACH_VEC_ELT (to_update_switch_stmts
, i
, su
)
6965 size_t n
= TREE_VEC_LENGTH (su
->vec
);
6967 gimple_switch_set_num_labels (su
->stmt
, n
);
6968 for (j
= 0; j
< n
; j
++)
6969 gimple_switch_set_label (su
->stmt
, j
, TREE_VEC_ELT (su
->vec
, j
));
6970 /* As we may have replaced the default label with a regular one
6971 make sure to make it a real default label again. This ensures
6972 optimal expansion. */
6973 label
= gimple_switch_label (su
->stmt
, 0);
6974 CASE_LOW (label
) = NULL_TREE
;
6975 CASE_HIGH (label
) = NULL_TREE
;
6978 if (to_remove_edges
.length () > 0)
6980 free_dominance_info (CDI_DOMINATORS
);
6981 loops_state_set (LOOPS_NEED_FIXUP
);
6984 to_remove_edges
.release ();
6985 to_update_switch_stmts
.release ();
6986 threadedge_finalize_values ();
6989 loop_optimizer_finalize ();
6995 const pass_data pass_data_vrp
=
6997 GIMPLE_PASS
, /* type */
6999 OPTGROUP_NONE
, /* optinfo_flags */
7000 TV_TREE_VRP
, /* tv_id */
7001 PROP_ssa
, /* properties_required */
7002 0, /* properties_provided */
7003 0, /* properties_destroyed */
7004 0, /* todo_flags_start */
7005 ( TODO_cleanup_cfg
| TODO_update_ssa
), /* todo_flags_finish */
7008 class pass_vrp
: public gimple_opt_pass
7011 pass_vrp (gcc::context
*ctxt
)
7012 : gimple_opt_pass (pass_data_vrp
, ctxt
), warn_array_bounds_p (false)
7015 /* opt_pass methods: */
7016 opt_pass
* clone () { return new pass_vrp (m_ctxt
); }
7017 void set_pass_param (unsigned int n
, bool param
)
7019 gcc_assert (n
== 0);
7020 warn_array_bounds_p
= param
;
7022 virtual bool gate (function
*) { return flag_tree_vrp
!= 0; }
7023 virtual unsigned int execute (function
*)
7024 { return execute_vrp (warn_array_bounds_p
); }
7027 bool warn_array_bounds_p
;
7028 }; // class pass_vrp
7033 make_pass_vrp (gcc::context
*ctxt
)
7035 return new pass_vrp (ctxt
);