1 /* Support routines for Value Range Propagation (VRP).
2 Copyright (C) 2005-2018 Free Software Foundation, Inc.
3 Contributed by Diego Novillo <dnovillo@redhat.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
23 #include "coretypes.h"
25 #include "insn-codes.h"
30 #include "tree-pass.h"
32 #include "optabs-tree.h"
33 #include "gimple-pretty-print.h"
34 #include "diagnostic-core.h"
36 #include "fold-const.h"
37 #include "stor-layout.h"
40 #include "gimple-fold.h"
42 #include "gimple-iterator.h"
43 #include "gimple-walk.h"
46 #include "tree-ssa-loop-manip.h"
47 #include "tree-ssa-loop-niter.h"
48 #include "tree-ssa-loop.h"
49 #include "tree-into-ssa.h"
53 #include "tree-scalar-evolution.h"
54 #include "tree-ssa-propagate.h"
55 #include "tree-chrec.h"
56 #include "tree-ssa-threadupdate.h"
57 #include "tree-ssa-scopedtables.h"
58 #include "tree-ssa-threadedge.h"
59 #include "omp-general.h"
61 #include "case-cfn-macros.h"
63 #include "alloc-pool.h"
65 #include "tree-cfgcleanup.h"
66 #include "stringpool.h"
68 #include "vr-values.h"
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 (range_int_cst_p (&vr0
) && range_int_cst_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
;
2789 if (TREE_OVERFLOW_P (val
))
2790 val
= drop_tree_overflow (val
);
2793 asserts
.safe_push (info
);
2796 /* If NAME doesn't have an ASSERT_EXPR registered for asserting
2797 'EXPR COMP_CODE VAL' at a location that dominates block BB or
2798 E->DEST, then register this location as a possible insertion point
2799 for ASSERT_EXPR <NAME, EXPR COMP_CODE VAL>.
2801 BB, E and SI provide the exact insertion point for the new
2802 ASSERT_EXPR. If BB is NULL, then the ASSERT_EXPR is to be inserted
2803 on edge E. Otherwise, if E is NULL, the ASSERT_EXPR is inserted on
2804 BB. If SI points to a COND_EXPR or a SWITCH_EXPR statement, then E
2805 must not be NULL. */
2808 register_new_assert_for (tree name
, tree expr
,
2809 enum tree_code comp_code
,
2813 gimple_stmt_iterator si
)
2815 assert_locus
*n
, *loc
, *last_loc
;
2816 basic_block dest_bb
;
2818 gcc_checking_assert (bb
== NULL
|| e
== NULL
);
2821 gcc_checking_assert (gimple_code (gsi_stmt (si
)) != GIMPLE_COND
2822 && gimple_code (gsi_stmt (si
)) != GIMPLE_SWITCH
);
2824 /* Never build an assert comparing against an integer constant with
2825 TREE_OVERFLOW set. This confuses our undefined overflow warning
2827 if (TREE_OVERFLOW_P (val
))
2828 val
= drop_tree_overflow (val
);
2830 /* The new assertion A will be inserted at BB or E. We need to
2831 determine if the new location is dominated by a previously
2832 registered location for A. If we are doing an edge insertion,
2833 assume that A will be inserted at E->DEST. Note that this is not
2836 If E is a critical edge, it will be split. But even if E is
2837 split, the new block will dominate the same set of blocks that
2840 The reverse, however, is not true, blocks dominated by E->DEST
2841 will not be dominated by the new block created to split E. So,
2842 if the insertion location is on a critical edge, we will not use
2843 the new location to move another assertion previously registered
2844 at a block dominated by E->DEST. */
2845 dest_bb
= (bb
) ? bb
: e
->dest
;
2847 /* If NAME already has an ASSERT_EXPR registered for COMP_CODE and
2848 VAL at a block dominating DEST_BB, then we don't need to insert a new
2849 one. Similarly, if the same assertion already exists at a block
2850 dominated by DEST_BB and the new location is not on a critical
2851 edge, then update the existing location for the assertion (i.e.,
2852 move the assertion up in the dominance tree).
2854 Note, this is implemented as a simple linked list because there
2855 should not be more than a handful of assertions registered per
2856 name. If this becomes a performance problem, a table hashed by
2857 COMP_CODE and VAL could be implemented. */
2858 loc
= asserts_for
[SSA_NAME_VERSION (name
)];
2862 if (loc
->comp_code
== comp_code
2864 || operand_equal_p (loc
->val
, val
, 0))
2865 && (loc
->expr
== expr
2866 || operand_equal_p (loc
->expr
, expr
, 0)))
2868 /* If E is not a critical edge and DEST_BB
2869 dominates the existing location for the assertion, move
2870 the assertion up in the dominance tree by updating its
2871 location information. */
2872 if ((e
== NULL
|| !EDGE_CRITICAL_P (e
))
2873 && dominated_by_p (CDI_DOMINATORS
, loc
->bb
, dest_bb
))
2882 /* Update the last node of the list and move to the next one. */
2887 /* If we didn't find an assertion already registered for
2888 NAME COMP_CODE VAL, add a new one at the end of the list of
2889 assertions associated with NAME. */
2890 n
= XNEW (struct assert_locus
);
2894 n
->comp_code
= comp_code
;
2902 asserts_for
[SSA_NAME_VERSION (name
)] = n
;
2904 bitmap_set_bit (need_assert_for
, SSA_NAME_VERSION (name
));
2907 /* (COND_OP0 COND_CODE COND_OP1) is a predicate which uses NAME.
2908 Extract a suitable test code and value and store them into *CODE_P and
2909 *VAL_P so the predicate is normalized to NAME *CODE_P *VAL_P.
2911 If no extraction was possible, return FALSE, otherwise return TRUE.
2913 If INVERT is true, then we invert the result stored into *CODE_P. */
2916 extract_code_and_val_from_cond_with_ops (tree name
, enum tree_code cond_code
,
2917 tree cond_op0
, tree cond_op1
,
2918 bool invert
, enum tree_code
*code_p
,
2921 enum tree_code comp_code
;
2924 /* Otherwise, we have a comparison of the form NAME COMP VAL
2925 or VAL COMP NAME. */
2926 if (name
== cond_op1
)
2928 /* If the predicate is of the form VAL COMP NAME, flip
2929 COMP around because we need to register NAME as the
2930 first operand in the predicate. */
2931 comp_code
= swap_tree_comparison (cond_code
);
2934 else if (name
== cond_op0
)
2936 /* The comparison is of the form NAME COMP VAL, so the
2937 comparison code remains unchanged. */
2938 comp_code
= cond_code
;
2944 /* Invert the comparison code as necessary. */
2946 comp_code
= invert_tree_comparison (comp_code
, 0);
2948 /* VRP only handles integral and pointer types. */
2949 if (! INTEGRAL_TYPE_P (TREE_TYPE (val
))
2950 && ! POINTER_TYPE_P (TREE_TYPE (val
)))
2953 /* Do not register always-false predicates.
2954 FIXME: this works around a limitation in fold() when dealing with
2955 enumerations. Given 'enum { N1, N2 } x;', fold will not
2956 fold 'if (x > N2)' to 'if (0)'. */
2957 if ((comp_code
== GT_EXPR
|| comp_code
== LT_EXPR
)
2958 && INTEGRAL_TYPE_P (TREE_TYPE (val
)))
2960 tree min
= TYPE_MIN_VALUE (TREE_TYPE (val
));
2961 tree max
= TYPE_MAX_VALUE (TREE_TYPE (val
));
2963 if (comp_code
== GT_EXPR
2965 || compare_values (val
, max
) == 0))
2968 if (comp_code
== LT_EXPR
2970 || compare_values (val
, min
) == 0))
2973 *code_p
= comp_code
;
2978 /* Find out smallest RES where RES > VAL && (RES & MASK) == RES, if any
2979 (otherwise return VAL). VAL and MASK must be zero-extended for
2980 precision PREC. If SGNBIT is non-zero, first xor VAL with SGNBIT
2981 (to transform signed values into unsigned) and at the end xor
2985 masked_increment (const wide_int
&val_in
, const wide_int
&mask
,
2986 const wide_int
&sgnbit
, unsigned int prec
)
2988 wide_int bit
= wi::one (prec
), res
;
2991 wide_int val
= val_in
^ sgnbit
;
2992 for (i
= 0; i
< prec
; i
++, bit
+= bit
)
2995 if ((res
& bit
) == 0)
2998 res
= wi::bit_and_not (val
+ bit
, res
);
3000 if (wi::gtu_p (res
, val
))
3001 return res
^ sgnbit
;
3003 return val
^ sgnbit
;
3006 /* Helper for overflow_comparison_p
3008 OP0 CODE OP1 is a comparison. Examine the comparison and potentially
3009 OP1's defining statement to see if it ultimately has the form
3010 OP0 CODE (OP0 PLUS INTEGER_CST)
3012 If so, return TRUE indicating this is an overflow test and store into
3013 *NEW_CST an updated constant that can be used in a narrowed range test.
3015 REVERSED indicates if the comparison was originally:
3019 This affects how we build the updated constant. */
3022 overflow_comparison_p_1 (enum tree_code code
, tree op0
, tree op1
,
3023 bool follow_assert_exprs
, bool reversed
, tree
*new_cst
)
3025 /* See if this is a relational operation between two SSA_NAMES with
3026 unsigned, overflow wrapping values. If so, check it more deeply. */
3027 if ((code
== LT_EXPR
|| code
== LE_EXPR
3028 || code
== GE_EXPR
|| code
== GT_EXPR
)
3029 && TREE_CODE (op0
) == SSA_NAME
3030 && TREE_CODE (op1
) == SSA_NAME
3031 && INTEGRAL_TYPE_P (TREE_TYPE (op0
))
3032 && TYPE_UNSIGNED (TREE_TYPE (op0
))
3033 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (op0
)))
3035 gimple
*op1_def
= SSA_NAME_DEF_STMT (op1
);
3037 /* If requested, follow any ASSERT_EXPRs backwards for OP1. */
3038 if (follow_assert_exprs
)
3040 while (gimple_assign_single_p (op1_def
)
3041 && TREE_CODE (gimple_assign_rhs1 (op1_def
)) == ASSERT_EXPR
)
3043 op1
= TREE_OPERAND (gimple_assign_rhs1 (op1_def
), 0);
3044 if (TREE_CODE (op1
) != SSA_NAME
)
3046 op1_def
= SSA_NAME_DEF_STMT (op1
);
3050 /* Now look at the defining statement of OP1 to see if it adds
3051 or subtracts a nonzero constant from another operand. */
3053 && is_gimple_assign (op1_def
)
3054 && gimple_assign_rhs_code (op1_def
) == PLUS_EXPR
3055 && TREE_CODE (gimple_assign_rhs2 (op1_def
)) == INTEGER_CST
3056 && !integer_zerop (gimple_assign_rhs2 (op1_def
)))
3058 tree target
= gimple_assign_rhs1 (op1_def
);
3060 /* If requested, follow ASSERT_EXPRs backwards for op0 looking
3061 for one where TARGET appears on the RHS. */
3062 if (follow_assert_exprs
)
3064 /* Now see if that "other operand" is op0, following the chain
3065 of ASSERT_EXPRs if necessary. */
3066 gimple
*op0_def
= SSA_NAME_DEF_STMT (op0
);
3067 while (op0
!= target
3068 && gimple_assign_single_p (op0_def
)
3069 && TREE_CODE (gimple_assign_rhs1 (op0_def
)) == ASSERT_EXPR
)
3071 op0
= TREE_OPERAND (gimple_assign_rhs1 (op0_def
), 0);
3072 if (TREE_CODE (op0
) != SSA_NAME
)
3074 op0_def
= SSA_NAME_DEF_STMT (op0
);
3078 /* If we did not find our target SSA_NAME, then this is not
3079 an overflow test. */
3083 tree type
= TREE_TYPE (op0
);
3084 wide_int max
= wi::max_value (TYPE_PRECISION (type
), UNSIGNED
);
3085 tree inc
= gimple_assign_rhs2 (op1_def
);
3087 *new_cst
= wide_int_to_tree (type
, max
+ wi::to_wide (inc
));
3089 *new_cst
= wide_int_to_tree (type
, max
- wi::to_wide (inc
));
3096 /* OP0 CODE OP1 is a comparison. Examine the comparison and potentially
3097 OP1's defining statement to see if it ultimately has the form
3098 OP0 CODE (OP0 PLUS INTEGER_CST)
3100 If so, return TRUE indicating this is an overflow test and store into
3101 *NEW_CST an updated constant that can be used in a narrowed range test.
3103 These statements are left as-is in the IL to facilitate discovery of
3104 {ADD,SUB}_OVERFLOW sequences later in the optimizer pipeline. But
3105 the alternate range representation is often useful within VRP. */
3108 overflow_comparison_p (tree_code code
, tree name
, tree val
,
3109 bool use_equiv_p
, tree
*new_cst
)
3111 if (overflow_comparison_p_1 (code
, name
, val
, use_equiv_p
, false, new_cst
))
3113 return overflow_comparison_p_1 (swap_tree_comparison (code
), val
, name
,
3114 use_equiv_p
, true, new_cst
);
3118 /* Try to register an edge assertion for SSA name NAME on edge E for
3119 the condition COND contributing to the conditional jump pointed to by BSI.
3120 Invert the condition COND if INVERT is true. */
3123 register_edge_assert_for_2 (tree name
, edge e
,
3124 enum tree_code cond_code
,
3125 tree cond_op0
, tree cond_op1
, bool invert
,
3126 vec
<assert_info
> &asserts
)
3129 enum tree_code comp_code
;
3131 if (!extract_code_and_val_from_cond_with_ops (name
, cond_code
,
3134 invert
, &comp_code
, &val
))
3137 /* Queue the assert. */
3139 if (overflow_comparison_p (comp_code
, name
, val
, false, &x
))
3141 enum tree_code new_code
= ((comp_code
== GT_EXPR
|| comp_code
== GE_EXPR
)
3142 ? GT_EXPR
: LE_EXPR
);
3143 add_assert_info (asserts
, name
, name
, new_code
, x
);
3145 add_assert_info (asserts
, name
, name
, comp_code
, val
);
3147 /* In the case of NAME <= CST and NAME being defined as
3148 NAME = (unsigned) NAME2 + CST2 we can assert NAME2 >= -CST2
3149 and NAME2 <= CST - CST2. We can do the same for NAME > CST.
3150 This catches range and anti-range tests. */
3151 if ((comp_code
== LE_EXPR
3152 || comp_code
== GT_EXPR
)
3153 && TREE_CODE (val
) == INTEGER_CST
3154 && TYPE_UNSIGNED (TREE_TYPE (val
)))
3156 gimple
*def_stmt
= SSA_NAME_DEF_STMT (name
);
3157 tree cst2
= NULL_TREE
, name2
= NULL_TREE
, name3
= NULL_TREE
;
3159 /* Extract CST2 from the (optional) addition. */
3160 if (is_gimple_assign (def_stmt
)
3161 && gimple_assign_rhs_code (def_stmt
) == PLUS_EXPR
)
3163 name2
= gimple_assign_rhs1 (def_stmt
);
3164 cst2
= gimple_assign_rhs2 (def_stmt
);
3165 if (TREE_CODE (name2
) == SSA_NAME
3166 && TREE_CODE (cst2
) == INTEGER_CST
)
3167 def_stmt
= SSA_NAME_DEF_STMT (name2
);
3170 /* Extract NAME2 from the (optional) sign-changing cast. */
3171 if (gimple_assign_cast_p (def_stmt
))
3173 if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt
))
3174 && ! TYPE_UNSIGNED (TREE_TYPE (gimple_assign_rhs1 (def_stmt
)))
3175 && (TYPE_PRECISION (gimple_expr_type (def_stmt
))
3176 == TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (def_stmt
)))))
3177 name3
= gimple_assign_rhs1 (def_stmt
);
3180 /* If name3 is used later, create an ASSERT_EXPR for it. */
3181 if (name3
!= NULL_TREE
3182 && TREE_CODE (name3
) == SSA_NAME
3183 && (cst2
== NULL_TREE
3184 || TREE_CODE (cst2
) == INTEGER_CST
)
3185 && INTEGRAL_TYPE_P (TREE_TYPE (name3
)))
3189 /* Build an expression for the range test. */
3190 tmp
= build1 (NOP_EXPR
, TREE_TYPE (name
), name3
);
3191 if (cst2
!= NULL_TREE
)
3192 tmp
= build2 (PLUS_EXPR
, TREE_TYPE (name
), tmp
, cst2
);
3196 fprintf (dump_file
, "Adding assert for ");
3197 print_generic_expr (dump_file
, name3
);
3198 fprintf (dump_file
, " from ");
3199 print_generic_expr (dump_file
, tmp
);
3200 fprintf (dump_file
, "\n");
3203 add_assert_info (asserts
, name3
, tmp
, comp_code
, val
);
3206 /* If name2 is used later, create an ASSERT_EXPR for it. */
3207 if (name2
!= NULL_TREE
3208 && TREE_CODE (name2
) == SSA_NAME
3209 && TREE_CODE (cst2
) == INTEGER_CST
3210 && INTEGRAL_TYPE_P (TREE_TYPE (name2
)))
3214 /* Build an expression for the range test. */
3216 if (TREE_TYPE (name
) != TREE_TYPE (name2
))
3217 tmp
= build1 (NOP_EXPR
, TREE_TYPE (name
), tmp
);
3218 if (cst2
!= NULL_TREE
)
3219 tmp
= build2 (PLUS_EXPR
, TREE_TYPE (name
), tmp
, cst2
);
3223 fprintf (dump_file
, "Adding assert for ");
3224 print_generic_expr (dump_file
, name2
);
3225 fprintf (dump_file
, " from ");
3226 print_generic_expr (dump_file
, tmp
);
3227 fprintf (dump_file
, "\n");
3230 add_assert_info (asserts
, name2
, tmp
, comp_code
, val
);
3234 /* In the case of post-in/decrement tests like if (i++) ... and uses
3235 of the in/decremented value on the edge the extra name we want to
3236 assert for is not on the def chain of the name compared. Instead
3237 it is in the set of use stmts.
3238 Similar cases happen for conversions that were simplified through
3239 fold_{sign_changed,widened}_comparison. */
3240 if ((comp_code
== NE_EXPR
3241 || comp_code
== EQ_EXPR
)
3242 && TREE_CODE (val
) == INTEGER_CST
)
3244 imm_use_iterator ui
;
3246 FOR_EACH_IMM_USE_STMT (use_stmt
, ui
, name
)
3248 if (!is_gimple_assign (use_stmt
))
3251 /* Cut off to use-stmts that are dominating the predecessor. */
3252 if (!dominated_by_p (CDI_DOMINATORS
, e
->src
, gimple_bb (use_stmt
)))
3255 tree name2
= gimple_assign_lhs (use_stmt
);
3256 if (TREE_CODE (name2
) != SSA_NAME
)
3259 enum tree_code code
= gimple_assign_rhs_code (use_stmt
);
3261 if (code
== PLUS_EXPR
3262 || code
== MINUS_EXPR
)
3264 cst
= gimple_assign_rhs2 (use_stmt
);
3265 if (TREE_CODE (cst
) != INTEGER_CST
)
3267 cst
= int_const_binop (code
, val
, cst
);
3269 else if (CONVERT_EXPR_CODE_P (code
))
3271 /* For truncating conversions we cannot record
3273 if (comp_code
== NE_EXPR
3274 && (TYPE_PRECISION (TREE_TYPE (name2
))
3275 < TYPE_PRECISION (TREE_TYPE (name
))))
3277 cst
= fold_convert (TREE_TYPE (name2
), val
);
3282 if (TREE_OVERFLOW_P (cst
))
3283 cst
= drop_tree_overflow (cst
);
3284 add_assert_info (asserts
, name2
, name2
, comp_code
, cst
);
3288 if (TREE_CODE_CLASS (comp_code
) == tcc_comparison
3289 && TREE_CODE (val
) == INTEGER_CST
)
3291 gimple
*def_stmt
= SSA_NAME_DEF_STMT (name
);
3292 tree name2
= NULL_TREE
, names
[2], cst2
= NULL_TREE
;
3293 tree val2
= NULL_TREE
;
3294 unsigned int prec
= TYPE_PRECISION (TREE_TYPE (val
));
3295 wide_int mask
= wi::zero (prec
);
3296 unsigned int nprec
= prec
;
3297 enum tree_code rhs_code
= ERROR_MARK
;
3299 if (is_gimple_assign (def_stmt
))
3300 rhs_code
= gimple_assign_rhs_code (def_stmt
);
3302 /* In the case of NAME != CST1 where NAME = A +- CST2 we can
3303 assert that A != CST1 -+ CST2. */
3304 if ((comp_code
== EQ_EXPR
|| comp_code
== NE_EXPR
)
3305 && (rhs_code
== PLUS_EXPR
|| rhs_code
== MINUS_EXPR
))
3307 tree op0
= gimple_assign_rhs1 (def_stmt
);
3308 tree op1
= gimple_assign_rhs2 (def_stmt
);
3309 if (TREE_CODE (op0
) == SSA_NAME
3310 && TREE_CODE (op1
) == INTEGER_CST
)
3312 enum tree_code reverse_op
= (rhs_code
== PLUS_EXPR
3313 ? MINUS_EXPR
: PLUS_EXPR
);
3314 op1
= int_const_binop (reverse_op
, val
, op1
);
3315 if (TREE_OVERFLOW (op1
))
3316 op1
= drop_tree_overflow (op1
);
3317 add_assert_info (asserts
, op0
, op0
, comp_code
, op1
);
3321 /* Add asserts for NAME cmp CST and NAME being defined
3322 as NAME = (int) NAME2. */
3323 if (!TYPE_UNSIGNED (TREE_TYPE (val
))
3324 && (comp_code
== LE_EXPR
|| comp_code
== LT_EXPR
3325 || comp_code
== GT_EXPR
|| comp_code
== GE_EXPR
)
3326 && gimple_assign_cast_p (def_stmt
))
3328 name2
= gimple_assign_rhs1 (def_stmt
);
3329 if (CONVERT_EXPR_CODE_P (rhs_code
)
3330 && INTEGRAL_TYPE_P (TREE_TYPE (name2
))
3331 && TYPE_UNSIGNED (TREE_TYPE (name2
))
3332 && prec
== TYPE_PRECISION (TREE_TYPE (name2
))
3333 && (comp_code
== LE_EXPR
|| comp_code
== GT_EXPR
3334 || !tree_int_cst_equal (val
,
3335 TYPE_MIN_VALUE (TREE_TYPE (val
)))))
3338 enum tree_code new_comp_code
= comp_code
;
3340 cst
= fold_convert (TREE_TYPE (name2
),
3341 TYPE_MIN_VALUE (TREE_TYPE (val
)));
3342 /* Build an expression for the range test. */
3343 tmp
= build2 (PLUS_EXPR
, TREE_TYPE (name2
), name2
, cst
);
3344 cst
= fold_build2 (PLUS_EXPR
, TREE_TYPE (name2
), cst
,
3345 fold_convert (TREE_TYPE (name2
), val
));
3346 if (comp_code
== LT_EXPR
|| comp_code
== GE_EXPR
)
3348 new_comp_code
= comp_code
== LT_EXPR
? LE_EXPR
: GT_EXPR
;
3349 cst
= fold_build2 (MINUS_EXPR
, TREE_TYPE (name2
), cst
,
3350 build_int_cst (TREE_TYPE (name2
), 1));
3355 fprintf (dump_file
, "Adding assert for ");
3356 print_generic_expr (dump_file
, name2
);
3357 fprintf (dump_file
, " from ");
3358 print_generic_expr (dump_file
, tmp
);
3359 fprintf (dump_file
, "\n");
3362 add_assert_info (asserts
, name2
, tmp
, new_comp_code
, cst
);
3366 /* Add asserts for NAME cmp CST and NAME being defined as
3367 NAME = NAME2 >> CST2.
3369 Extract CST2 from the right shift. */
3370 if (rhs_code
== RSHIFT_EXPR
)
3372 name2
= gimple_assign_rhs1 (def_stmt
);
3373 cst2
= gimple_assign_rhs2 (def_stmt
);
3374 if (TREE_CODE (name2
) == SSA_NAME
3375 && tree_fits_uhwi_p (cst2
)
3376 && INTEGRAL_TYPE_P (TREE_TYPE (name2
))
3377 && IN_RANGE (tree_to_uhwi (cst2
), 1, prec
- 1)
3378 && type_has_mode_precision_p (TREE_TYPE (val
)))
3380 mask
= wi::mask (tree_to_uhwi (cst2
), false, prec
);
3381 val2
= fold_binary (LSHIFT_EXPR
, TREE_TYPE (val
), val
, cst2
);
3384 if (val2
!= NULL_TREE
3385 && TREE_CODE (val2
) == INTEGER_CST
3386 && simple_cst_equal (fold_build2 (RSHIFT_EXPR
,
3390 enum tree_code new_comp_code
= comp_code
;
3394 if (comp_code
== EQ_EXPR
|| comp_code
== NE_EXPR
)
3396 if (!TYPE_UNSIGNED (TREE_TYPE (val
)))
3398 tree type
= build_nonstandard_integer_type (prec
, 1);
3399 tmp
= build1 (NOP_EXPR
, type
, name2
);
3400 val2
= fold_convert (type
, val2
);
3402 tmp
= fold_build2 (MINUS_EXPR
, TREE_TYPE (tmp
), tmp
, val2
);
3403 new_val
= wide_int_to_tree (TREE_TYPE (tmp
), mask
);
3404 new_comp_code
= comp_code
== EQ_EXPR
? LE_EXPR
: GT_EXPR
;
3406 else if (comp_code
== LT_EXPR
|| comp_code
== GE_EXPR
)
3409 = wi::min_value (prec
, TYPE_SIGN (TREE_TYPE (val
)));
3411 if (minval
== wi::to_wide (new_val
))
3412 new_val
= NULL_TREE
;
3417 = wi::max_value (prec
, TYPE_SIGN (TREE_TYPE (val
)));
3418 mask
|= wi::to_wide (val2
);
3419 if (wi::eq_p (mask
, maxval
))
3420 new_val
= NULL_TREE
;
3422 new_val
= wide_int_to_tree (TREE_TYPE (val2
), mask
);
3429 fprintf (dump_file
, "Adding assert for ");
3430 print_generic_expr (dump_file
, name2
);
3431 fprintf (dump_file
, " from ");
3432 print_generic_expr (dump_file
, tmp
);
3433 fprintf (dump_file
, "\n");
3436 add_assert_info (asserts
, name2
, tmp
, new_comp_code
, new_val
);
3440 /* Add asserts for NAME cmp CST and NAME being defined as
3441 NAME = NAME2 & CST2.
3443 Extract CST2 from the and.
3446 NAME = (unsigned) NAME2;
3447 casts where NAME's type is unsigned and has smaller precision
3448 than NAME2's type as if it was NAME = NAME2 & MASK. */
3449 names
[0] = NULL_TREE
;
3450 names
[1] = NULL_TREE
;
3452 if (rhs_code
== BIT_AND_EXPR
3453 || (CONVERT_EXPR_CODE_P (rhs_code
)
3454 && INTEGRAL_TYPE_P (TREE_TYPE (val
))
3455 && TYPE_UNSIGNED (TREE_TYPE (val
))
3456 && TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (def_stmt
)))
3459 name2
= gimple_assign_rhs1 (def_stmt
);
3460 if (rhs_code
== BIT_AND_EXPR
)
3461 cst2
= gimple_assign_rhs2 (def_stmt
);
3464 cst2
= TYPE_MAX_VALUE (TREE_TYPE (val
));
3465 nprec
= TYPE_PRECISION (TREE_TYPE (name2
));
3467 if (TREE_CODE (name2
) == SSA_NAME
3468 && INTEGRAL_TYPE_P (TREE_TYPE (name2
))
3469 && TREE_CODE (cst2
) == INTEGER_CST
3470 && !integer_zerop (cst2
)
3472 || TYPE_UNSIGNED (TREE_TYPE (val
))))
3474 gimple
*def_stmt2
= SSA_NAME_DEF_STMT (name2
);
3475 if (gimple_assign_cast_p (def_stmt2
))
3477 names
[1] = gimple_assign_rhs1 (def_stmt2
);
3478 if (!CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt2
))
3479 || !INTEGRAL_TYPE_P (TREE_TYPE (names
[1]))
3480 || (TYPE_PRECISION (TREE_TYPE (name2
))
3481 != TYPE_PRECISION (TREE_TYPE (names
[1]))))
3482 names
[1] = NULL_TREE
;
3487 if (names
[0] || names
[1])
3489 wide_int minv
, maxv
, valv
, cst2v
;
3490 wide_int tem
, sgnbit
;
3491 bool valid_p
= false, valn
, cst2n
;
3492 enum tree_code ccode
= comp_code
;
3494 valv
= wide_int::from (wi::to_wide (val
), nprec
, UNSIGNED
);
3495 cst2v
= wide_int::from (wi::to_wide (cst2
), nprec
, UNSIGNED
);
3496 valn
= wi::neg_p (valv
, TYPE_SIGN (TREE_TYPE (val
)));
3497 cst2n
= wi::neg_p (cst2v
, TYPE_SIGN (TREE_TYPE (val
)));
3498 /* If CST2 doesn't have most significant bit set,
3499 but VAL is negative, we have comparison like
3500 if ((x & 0x123) > -4) (always true). Just give up. */
3504 sgnbit
= wi::set_bit_in_zero (nprec
- 1, nprec
);
3506 sgnbit
= wi::zero (nprec
);
3507 minv
= valv
& cst2v
;
3511 /* Minimum unsigned value for equality is VAL & CST2
3512 (should be equal to VAL, otherwise we probably should
3513 have folded the comparison into false) and
3514 maximum unsigned value is VAL | ~CST2. */
3515 maxv
= valv
| ~cst2v
;
3520 tem
= valv
| ~cst2v
;
3521 /* If VAL is 0, handle (X & CST2) != 0 as (X & CST2) > 0U. */
3525 sgnbit
= wi::zero (nprec
);
3528 /* If (VAL | ~CST2) is all ones, handle it as
3529 (X & CST2) < VAL. */
3534 sgnbit
= wi::zero (nprec
);
3537 if (!cst2n
&& wi::neg_p (cst2v
))
3538 sgnbit
= wi::set_bit_in_zero (nprec
- 1, nprec
);
3547 if (tem
== wi::mask (nprec
- 1, false, nprec
))
3553 sgnbit
= wi::zero (nprec
);
3558 /* Minimum unsigned value for >= if (VAL & CST2) == VAL
3559 is VAL and maximum unsigned value is ~0. For signed
3560 comparison, if CST2 doesn't have most significant bit
3561 set, handle it similarly. If CST2 has MSB set,
3562 the minimum is the same, and maximum is ~0U/2. */
3565 /* If (VAL & CST2) != VAL, X & CST2 can't be equal to
3567 minv
= masked_increment (valv
, cst2v
, sgnbit
, nprec
);
3571 maxv
= wi::mask (nprec
- (cst2n
? 1 : 0), false, nprec
);
3577 /* Find out smallest MINV where MINV > VAL
3578 && (MINV & CST2) == MINV, if any. If VAL is signed and
3579 CST2 has MSB set, compute it biased by 1 << (nprec - 1). */
3580 minv
= masked_increment (valv
, cst2v
, sgnbit
, nprec
);
3583 maxv
= wi::mask (nprec
- (cst2n
? 1 : 0), false, nprec
);
3588 /* Minimum unsigned value for <= is 0 and maximum
3589 unsigned value is VAL | ~CST2 if (VAL & CST2) == VAL.
3590 Otherwise, find smallest VAL2 where VAL2 > VAL
3591 && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
3593 For signed comparison, if CST2 doesn't have most
3594 significant bit set, handle it similarly. If CST2 has
3595 MSB set, the maximum is the same and minimum is INT_MIN. */
3600 maxv
= masked_increment (valv
, cst2v
, sgnbit
, nprec
);
3612 /* Minimum unsigned value for < is 0 and maximum
3613 unsigned value is (VAL-1) | ~CST2 if (VAL & CST2) == VAL.
3614 Otherwise, find smallest VAL2 where VAL2 > VAL
3615 && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
3617 For signed comparison, if CST2 doesn't have most
3618 significant bit set, handle it similarly. If CST2 has
3619 MSB set, the maximum is the same and minimum is INT_MIN. */
3628 maxv
= masked_increment (valv
, cst2v
, sgnbit
, nprec
);
3642 && (maxv
- minv
) != -1)
3644 tree tmp
, new_val
, type
;
3647 for (i
= 0; i
< 2; i
++)
3650 wide_int maxv2
= maxv
;
3652 type
= TREE_TYPE (names
[i
]);
3653 if (!TYPE_UNSIGNED (type
))
3655 type
= build_nonstandard_integer_type (nprec
, 1);
3656 tmp
= build1 (NOP_EXPR
, type
, names
[i
]);
3660 tmp
= build2 (PLUS_EXPR
, type
, tmp
,
3661 wide_int_to_tree (type
, -minv
));
3662 maxv2
= maxv
- minv
;
3664 new_val
= wide_int_to_tree (type
, maxv2
);
3668 fprintf (dump_file
, "Adding assert for ");
3669 print_generic_expr (dump_file
, names
[i
]);
3670 fprintf (dump_file
, " from ");
3671 print_generic_expr (dump_file
, tmp
);
3672 fprintf (dump_file
, "\n");
3675 add_assert_info (asserts
, names
[i
], tmp
, LE_EXPR
, new_val
);
3682 /* OP is an operand of a truth value expression which is known to have
3683 a particular value. Register any asserts for OP and for any
3684 operands in OP's defining statement.
3686 If CODE is EQ_EXPR, then we want to register OP is zero (false),
3687 if CODE is NE_EXPR, then we want to register OP is nonzero (true). */
3690 register_edge_assert_for_1 (tree op
, enum tree_code code
,
3691 edge e
, vec
<assert_info
> &asserts
)
3695 enum tree_code rhs_code
;
3697 /* We only care about SSA_NAMEs. */
3698 if (TREE_CODE (op
) != SSA_NAME
)
3701 /* We know that OP will have a zero or nonzero value. */
3702 val
= build_int_cst (TREE_TYPE (op
), 0);
3703 add_assert_info (asserts
, op
, op
, code
, val
);
3705 /* Now look at how OP is set. If it's set from a comparison,
3706 a truth operation or some bit operations, then we may be able
3707 to register information about the operands of that assignment. */
3708 op_def
= SSA_NAME_DEF_STMT (op
);
3709 if (gimple_code (op_def
) != GIMPLE_ASSIGN
)
3712 rhs_code
= gimple_assign_rhs_code (op_def
);
3714 if (TREE_CODE_CLASS (rhs_code
) == tcc_comparison
)
3716 bool invert
= (code
== EQ_EXPR
? true : false);
3717 tree op0
= gimple_assign_rhs1 (op_def
);
3718 tree op1
= gimple_assign_rhs2 (op_def
);
3720 if (TREE_CODE (op0
) == SSA_NAME
)
3721 register_edge_assert_for_2 (op0
, e
, rhs_code
, op0
, op1
, invert
, asserts
);
3722 if (TREE_CODE (op1
) == SSA_NAME
)
3723 register_edge_assert_for_2 (op1
, e
, rhs_code
, op0
, op1
, invert
, asserts
);
3725 else if ((code
== NE_EXPR
3726 && gimple_assign_rhs_code (op_def
) == BIT_AND_EXPR
)
3728 && gimple_assign_rhs_code (op_def
) == BIT_IOR_EXPR
))
3730 /* Recurse on each operand. */
3731 tree op0
= gimple_assign_rhs1 (op_def
);
3732 tree op1
= gimple_assign_rhs2 (op_def
);
3733 if (TREE_CODE (op0
) == SSA_NAME
3734 && has_single_use (op0
))
3735 register_edge_assert_for_1 (op0
, code
, e
, asserts
);
3736 if (TREE_CODE (op1
) == SSA_NAME
3737 && has_single_use (op1
))
3738 register_edge_assert_for_1 (op1
, code
, e
, asserts
);
3740 else if (gimple_assign_rhs_code (op_def
) == BIT_NOT_EXPR
3741 && TYPE_PRECISION (TREE_TYPE (gimple_assign_lhs (op_def
))) == 1)
3743 /* Recurse, flipping CODE. */
3744 code
= invert_tree_comparison (code
, false);
3745 register_edge_assert_for_1 (gimple_assign_rhs1 (op_def
), code
, e
, asserts
);
3747 else if (gimple_assign_rhs_code (op_def
) == SSA_NAME
)
3749 /* Recurse through the copy. */
3750 register_edge_assert_for_1 (gimple_assign_rhs1 (op_def
), code
, e
, asserts
);
3752 else if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (op_def
)))
3754 /* Recurse through the type conversion, unless it is a narrowing
3755 conversion or conversion from non-integral type. */
3756 tree rhs
= gimple_assign_rhs1 (op_def
);
3757 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs
))
3758 && (TYPE_PRECISION (TREE_TYPE (rhs
))
3759 <= TYPE_PRECISION (TREE_TYPE (op
))))
3760 register_edge_assert_for_1 (rhs
, code
, e
, asserts
);
3764 /* Check if comparison
3765 NAME COND_OP INTEGER_CST
3767 (X & 11...100..0) COND_OP XX...X00...0
3768 Such comparison can yield assertions like
3771 in case of COND_OP being NE_EXPR or
3774 in case of EQ_EXPR. */
3777 is_masked_range_test (tree name
, tree valt
, enum tree_code cond_code
,
3778 tree
*new_name
, tree
*low
, enum tree_code
*low_code
,
3779 tree
*high
, enum tree_code
*high_code
)
3781 gimple
*def_stmt
= SSA_NAME_DEF_STMT (name
);
3783 if (!is_gimple_assign (def_stmt
)
3784 || gimple_assign_rhs_code (def_stmt
) != BIT_AND_EXPR
)
3787 tree t
= gimple_assign_rhs1 (def_stmt
);
3788 tree maskt
= gimple_assign_rhs2 (def_stmt
);
3789 if (TREE_CODE (t
) != SSA_NAME
|| TREE_CODE (maskt
) != INTEGER_CST
)
3792 wi::tree_to_wide_ref mask
= wi::to_wide (maskt
);
3793 wide_int inv_mask
= ~mask
;
3794 /* Assume VALT is INTEGER_CST. */
3795 wi::tree_to_wide_ref val
= wi::to_wide (valt
);
3797 if ((inv_mask
& (inv_mask
+ 1)) != 0
3798 || (val
& mask
) != val
)
3801 bool is_range
= cond_code
== EQ_EXPR
;
3803 tree type
= TREE_TYPE (t
);
3804 wide_int min
= wi::min_value (type
),
3805 max
= wi::max_value (type
);
3809 *low_code
= val
== min
? ERROR_MARK
: GE_EXPR
;
3810 *high_code
= val
== max
? ERROR_MARK
: LE_EXPR
;
3814 /* We can still generate assertion if one of alternatives
3815 is known to always be false. */
3818 *low_code
= (enum tree_code
) 0;
3819 *high_code
= GT_EXPR
;
3821 else if ((val
| inv_mask
) == max
)
3823 *low_code
= LT_EXPR
;
3824 *high_code
= (enum tree_code
) 0;
3831 *low
= wide_int_to_tree (type
, val
);
3832 *high
= wide_int_to_tree (type
, val
| inv_mask
);
3834 if (wi::neg_p (val
, TYPE_SIGN (type
)))
3835 std::swap (*low
, *high
);
3840 /* Try to register an edge assertion for SSA name NAME on edge E for
3841 the condition COND contributing to the conditional jump pointed to by
3845 register_edge_assert_for (tree name
, edge e
,
3846 enum tree_code cond_code
, tree cond_op0
,
3847 tree cond_op1
, vec
<assert_info
> &asserts
)
3850 enum tree_code comp_code
;
3851 bool is_else_edge
= (e
->flags
& EDGE_FALSE_VALUE
) != 0;
3853 /* Do not attempt to infer anything in names that flow through
3855 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name
))
3858 if (!extract_code_and_val_from_cond_with_ops (name
, cond_code
,
3864 /* Register ASSERT_EXPRs for name. */
3865 register_edge_assert_for_2 (name
, e
, cond_code
, cond_op0
,
3866 cond_op1
, is_else_edge
, asserts
);
3869 /* If COND is effectively an equality test of an SSA_NAME against
3870 the value zero or one, then we may be able to assert values
3871 for SSA_NAMEs which flow into COND. */
3873 /* In the case of NAME == 1 or NAME != 0, for BIT_AND_EXPR defining
3874 statement of NAME we can assert both operands of the BIT_AND_EXPR
3875 have nonzero value. */
3876 if (((comp_code
== EQ_EXPR
&& integer_onep (val
))
3877 || (comp_code
== NE_EXPR
&& integer_zerop (val
))))
3879 gimple
*def_stmt
= SSA_NAME_DEF_STMT (name
);
3881 if (is_gimple_assign (def_stmt
)
3882 && gimple_assign_rhs_code (def_stmt
) == BIT_AND_EXPR
)
3884 tree op0
= gimple_assign_rhs1 (def_stmt
);
3885 tree op1
= gimple_assign_rhs2 (def_stmt
);
3886 register_edge_assert_for_1 (op0
, NE_EXPR
, e
, asserts
);
3887 register_edge_assert_for_1 (op1
, NE_EXPR
, e
, asserts
);
3891 /* In the case of NAME == 0 or NAME != 1, for BIT_IOR_EXPR defining
3892 statement of NAME we can assert both operands of the BIT_IOR_EXPR
3894 if (((comp_code
== EQ_EXPR
&& integer_zerop (val
))
3895 || (comp_code
== NE_EXPR
&& integer_onep (val
))))
3897 gimple
*def_stmt
= SSA_NAME_DEF_STMT (name
);
3899 /* For BIT_IOR_EXPR only if NAME == 0 both operands have
3900 necessarily zero value, or if type-precision is one. */
3901 if (is_gimple_assign (def_stmt
)
3902 && (gimple_assign_rhs_code (def_stmt
) == BIT_IOR_EXPR
3903 && (TYPE_PRECISION (TREE_TYPE (name
)) == 1
3904 || comp_code
== EQ_EXPR
)))
3906 tree op0
= gimple_assign_rhs1 (def_stmt
);
3907 tree op1
= gimple_assign_rhs2 (def_stmt
);
3908 register_edge_assert_for_1 (op0
, EQ_EXPR
, e
, asserts
);
3909 register_edge_assert_for_1 (op1
, EQ_EXPR
, e
, asserts
);
3913 /* Sometimes we can infer ranges from (NAME & MASK) == VALUE. */
3914 if ((comp_code
== EQ_EXPR
|| comp_code
== NE_EXPR
)
3915 && TREE_CODE (val
) == INTEGER_CST
)
3917 enum tree_code low_code
, high_code
;
3919 if (is_masked_range_test (name
, val
, comp_code
, &name
, &low
,
3920 &low_code
, &high
, &high_code
))
3922 if (low_code
!= ERROR_MARK
)
3923 register_edge_assert_for_2 (name
, e
, low_code
, name
,
3924 low
, /*invert*/false, asserts
);
3925 if (high_code
!= ERROR_MARK
)
3926 register_edge_assert_for_2 (name
, e
, high_code
, name
,
3927 high
, /*invert*/false, asserts
);
3932 /* Finish found ASSERTS for E and register them at GSI. */
3935 finish_register_edge_assert_for (edge e
, gimple_stmt_iterator gsi
,
3936 vec
<assert_info
> &asserts
)
3938 for (unsigned i
= 0; i
< asserts
.length (); ++i
)
3939 /* Only register an ASSERT_EXPR if NAME was found in the sub-graph
3940 reachable from E. */
3941 if (live_on_edge (e
, asserts
[i
].name
))
3942 register_new_assert_for (asserts
[i
].name
, asserts
[i
].expr
,
3943 asserts
[i
].comp_code
, asserts
[i
].val
,
3949 /* Determine whether the outgoing edges of BB should receive an
3950 ASSERT_EXPR for each of the operands of BB's LAST statement.
3951 The last statement of BB must be a COND_EXPR.
3953 If any of the sub-graphs rooted at BB have an interesting use of
3954 the predicate operands, an assert location node is added to the
3955 list of assertions for the corresponding operands. */
3958 find_conditional_asserts (basic_block bb
, gcond
*last
)
3960 gimple_stmt_iterator bsi
;
3966 bsi
= gsi_for_stmt (last
);
3968 /* Look for uses of the operands in each of the sub-graphs
3969 rooted at BB. We need to check each of the outgoing edges
3970 separately, so that we know what kind of ASSERT_EXPR to
3972 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
3977 /* Register the necessary assertions for each operand in the
3978 conditional predicate. */
3979 auto_vec
<assert_info
, 8> asserts
;
3980 FOR_EACH_SSA_TREE_OPERAND (op
, last
, iter
, SSA_OP_USE
)
3981 register_edge_assert_for (op
, e
,
3982 gimple_cond_code (last
),
3983 gimple_cond_lhs (last
),
3984 gimple_cond_rhs (last
), asserts
);
3985 finish_register_edge_assert_for (e
, bsi
, asserts
);
3995 /* Compare two case labels sorting first by the destination bb index
3996 and then by the case value. */
3999 compare_case_labels (const void *p1
, const void *p2
)
4001 const struct case_info
*ci1
= (const struct case_info
*) p1
;
4002 const struct case_info
*ci2
= (const struct case_info
*) p2
;
4003 int idx1
= ci1
->bb
->index
;
4004 int idx2
= ci2
->bb
->index
;
4008 else if (idx1
== idx2
)
4010 /* Make sure the default label is first in a group. */
4011 if (!CASE_LOW (ci1
->expr
))
4013 else if (!CASE_LOW (ci2
->expr
))
4016 return tree_int_cst_compare (CASE_LOW (ci1
->expr
),
4017 CASE_LOW (ci2
->expr
));
4023 /* Determine whether the outgoing edges of BB should receive an
4024 ASSERT_EXPR for each of the operands of BB's LAST statement.
4025 The last statement of BB must be a SWITCH_EXPR.
4027 If any of the sub-graphs rooted at BB have an interesting use of
4028 the predicate operands, an assert location node is added to the
4029 list of assertions for the corresponding operands. */
4032 find_switch_asserts (basic_block bb
, gswitch
*last
)
4034 gimple_stmt_iterator bsi
;
4037 struct case_info
*ci
;
4038 size_t n
= gimple_switch_num_labels (last
);
4039 #if GCC_VERSION >= 4000
4042 /* Work around GCC 3.4 bug (PR 37086). */
4043 volatile unsigned int idx
;
4046 bsi
= gsi_for_stmt (last
);
4047 op
= gimple_switch_index (last
);
4048 if (TREE_CODE (op
) != SSA_NAME
)
4051 /* Build a vector of case labels sorted by destination label. */
4052 ci
= XNEWVEC (struct case_info
, n
);
4053 for (idx
= 0; idx
< n
; ++idx
)
4055 ci
[idx
].expr
= gimple_switch_label (last
, idx
);
4056 ci
[idx
].bb
= label_to_block (CASE_LABEL (ci
[idx
].expr
));
4058 edge default_edge
= find_edge (bb
, ci
[0].bb
);
4059 qsort (ci
, n
, sizeof (struct case_info
), compare_case_labels
);
4061 for (idx
= 0; idx
< n
; ++idx
)
4064 tree cl
= ci
[idx
].expr
;
4065 basic_block cbb
= ci
[idx
].bb
;
4067 min
= CASE_LOW (cl
);
4068 max
= CASE_HIGH (cl
);
4070 /* If there are multiple case labels with the same destination
4071 we need to combine them to a single value range for the edge. */
4072 if (idx
+ 1 < n
&& cbb
== ci
[idx
+ 1].bb
)
4074 /* Skip labels until the last of the group. */
4077 } while (idx
< n
&& cbb
== ci
[idx
].bb
);
4080 /* Pick up the maximum of the case label range. */
4081 if (CASE_HIGH (ci
[idx
].expr
))
4082 max
= CASE_HIGH (ci
[idx
].expr
);
4084 max
= CASE_LOW (ci
[idx
].expr
);
4087 /* Can't extract a useful assertion out of a range that includes the
4089 if (min
== NULL_TREE
)
4092 /* Find the edge to register the assert expr on. */
4093 e
= find_edge (bb
, cbb
);
4095 /* Register the necessary assertions for the operand in the
4097 auto_vec
<assert_info
, 8> asserts
;
4098 register_edge_assert_for (op
, e
,
4099 max
? GE_EXPR
: EQ_EXPR
,
4100 op
, fold_convert (TREE_TYPE (op
), min
),
4103 register_edge_assert_for (op
, e
, LE_EXPR
, op
,
4104 fold_convert (TREE_TYPE (op
), max
),
4106 finish_register_edge_assert_for (e
, bsi
, asserts
);
4111 if (!live_on_edge (default_edge
, op
))
4114 /* Now register along the default label assertions that correspond to the
4115 anti-range of each label. */
4116 int insertion_limit
= PARAM_VALUE (PARAM_MAX_VRP_SWITCH_ASSERTIONS
);
4117 if (insertion_limit
== 0)
4120 /* We can't do this if the default case shares a label with another case. */
4121 tree default_cl
= gimple_switch_default_label (last
);
4122 for (idx
= 1; idx
< n
; idx
++)
4125 tree cl
= gimple_switch_label (last
, idx
);
4126 if (CASE_LABEL (cl
) == CASE_LABEL (default_cl
))
4129 min
= CASE_LOW (cl
);
4130 max
= CASE_HIGH (cl
);
4132 /* Combine contiguous case ranges to reduce the number of assertions
4134 for (idx
= idx
+ 1; idx
< n
; idx
++)
4136 tree next_min
, next_max
;
4137 tree next_cl
= gimple_switch_label (last
, idx
);
4138 if (CASE_LABEL (next_cl
) == CASE_LABEL (default_cl
))
4141 next_min
= CASE_LOW (next_cl
);
4142 next_max
= CASE_HIGH (next_cl
);
4144 wide_int difference
= (wi::to_wide (next_min
)
4145 - wi::to_wide (max
? max
: min
));
4146 if (wi::eq_p (difference
, 1))
4147 max
= next_max
? next_max
: next_min
;
4153 if (max
== NULL_TREE
)
4155 /* Register the assertion OP != MIN. */
4156 auto_vec
<assert_info
, 8> asserts
;
4157 min
= fold_convert (TREE_TYPE (op
), min
);
4158 register_edge_assert_for (op
, default_edge
, NE_EXPR
, op
, min
,
4160 finish_register_edge_assert_for (default_edge
, bsi
, asserts
);
4164 /* Register the assertion (unsigned)OP - MIN > (MAX - MIN),
4165 which will give OP the anti-range ~[MIN,MAX]. */
4166 tree uop
= fold_convert (unsigned_type_for (TREE_TYPE (op
)), op
);
4167 min
= fold_convert (TREE_TYPE (uop
), min
);
4168 max
= fold_convert (TREE_TYPE (uop
), max
);
4170 tree lhs
= fold_build2 (MINUS_EXPR
, TREE_TYPE (uop
), uop
, min
);
4171 tree rhs
= int_const_binop (MINUS_EXPR
, max
, min
);
4172 register_new_assert_for (op
, lhs
, GT_EXPR
, rhs
,
4173 NULL
, default_edge
, bsi
);
4176 if (--insertion_limit
== 0)
4182 /* Traverse all the statements in block BB looking for statements that
4183 may generate useful assertions for the SSA names in their operand.
4184 If a statement produces a useful assertion A for name N_i, then the
4185 list of assertions already generated for N_i is scanned to
4186 determine if A is actually needed.
4188 If N_i already had the assertion A at a location dominating the
4189 current location, then nothing needs to be done. Otherwise, the
4190 new location for A is recorded instead.
4192 1- For every statement S in BB, all the variables used by S are
4193 added to bitmap FOUND_IN_SUBGRAPH.
4195 2- If statement S uses an operand N in a way that exposes a known
4196 value range for N, then if N was not already generated by an
4197 ASSERT_EXPR, create a new assert location for N. For instance,
4198 if N is a pointer and the statement dereferences it, we can
4199 assume that N is not NULL.
4201 3- COND_EXPRs are a special case of #2. We can derive range
4202 information from the predicate but need to insert different
4203 ASSERT_EXPRs for each of the sub-graphs rooted at the
4204 conditional block. If the last statement of BB is a conditional
4205 expression of the form 'X op Y', then
4207 a) Remove X and Y from the set FOUND_IN_SUBGRAPH.
4209 b) If the conditional is the only entry point to the sub-graph
4210 corresponding to the THEN_CLAUSE, recurse into it. On
4211 return, if X and/or Y are marked in FOUND_IN_SUBGRAPH, then
4212 an ASSERT_EXPR is added for the corresponding variable.
4214 c) Repeat step (b) on the ELSE_CLAUSE.
4216 d) Mark X and Y in FOUND_IN_SUBGRAPH.
4225 In this case, an assertion on the THEN clause is useful to
4226 determine that 'a' is always 9 on that edge. However, an assertion
4227 on the ELSE clause would be unnecessary.
4229 4- If BB does not end in a conditional expression, then we recurse
4230 into BB's dominator children.
4232 At the end of the recursive traversal, every SSA name will have a
4233 list of locations where ASSERT_EXPRs should be added. When a new
4234 location for name N is found, it is registered by calling
4235 register_new_assert_for. That function keeps track of all the
4236 registered assertions to prevent adding unnecessary assertions.
4237 For instance, if a pointer P_4 is dereferenced more than once in a
4238 dominator tree, only the location dominating all the dereference of
4239 P_4 will receive an ASSERT_EXPR. */
4242 find_assert_locations_1 (basic_block bb
, sbitmap live
)
4246 last
= last_stmt (bb
);
4248 /* If BB's last statement is a conditional statement involving integer
4249 operands, determine if we need to add ASSERT_EXPRs. */
4251 && gimple_code (last
) == GIMPLE_COND
4252 && !fp_predicate (last
)
4253 && !ZERO_SSA_OPERANDS (last
, SSA_OP_USE
))
4254 find_conditional_asserts (bb
, as_a
<gcond
*> (last
));
4256 /* If BB's last statement is a switch statement involving integer
4257 operands, determine if we need to add ASSERT_EXPRs. */
4259 && gimple_code (last
) == GIMPLE_SWITCH
4260 && !ZERO_SSA_OPERANDS (last
, SSA_OP_USE
))
4261 find_switch_asserts (bb
, as_a
<gswitch
*> (last
));
4263 /* Traverse all the statements in BB marking used names and looking
4264 for statements that may infer assertions for their used operands. */
4265 for (gimple_stmt_iterator si
= gsi_last_bb (bb
); !gsi_end_p (si
);
4272 stmt
= gsi_stmt (si
);
4274 if (is_gimple_debug (stmt
))
4277 /* See if we can derive an assertion for any of STMT's operands. */
4278 FOR_EACH_SSA_TREE_OPERAND (op
, stmt
, i
, SSA_OP_USE
)
4281 enum tree_code comp_code
;
4283 /* If op is not live beyond this stmt, do not bother to insert
4285 if (!bitmap_bit_p (live
, SSA_NAME_VERSION (op
)))
4288 /* If OP is used in such a way that we can infer a value
4289 range for it, and we don't find a previous assertion for
4290 it, create a new assertion location node for OP. */
4291 if (infer_value_range (stmt
, op
, &comp_code
, &value
))
4293 /* If we are able to infer a nonzero value range for OP,
4294 then walk backwards through the use-def chain to see if OP
4295 was set via a typecast.
4297 If so, then we can also infer a nonzero value range
4298 for the operand of the NOP_EXPR. */
4299 if (comp_code
== NE_EXPR
&& integer_zerop (value
))
4302 gimple
*def_stmt
= SSA_NAME_DEF_STMT (t
);
4304 while (is_gimple_assign (def_stmt
)
4305 && CONVERT_EXPR_CODE_P
4306 (gimple_assign_rhs_code (def_stmt
))
4308 (gimple_assign_rhs1 (def_stmt
)) == SSA_NAME
4310 (TREE_TYPE (gimple_assign_rhs1 (def_stmt
))))
4312 t
= gimple_assign_rhs1 (def_stmt
);
4313 def_stmt
= SSA_NAME_DEF_STMT (t
);
4315 /* Note we want to register the assert for the
4316 operand of the NOP_EXPR after SI, not after the
4318 if (bitmap_bit_p (live
, SSA_NAME_VERSION (t
)))
4319 register_new_assert_for (t
, t
, comp_code
, value
,
4324 register_new_assert_for (op
, op
, comp_code
, value
, bb
, NULL
, si
);
4329 FOR_EACH_SSA_TREE_OPERAND (op
, stmt
, i
, SSA_OP_USE
)
4330 bitmap_set_bit (live
, SSA_NAME_VERSION (op
));
4331 FOR_EACH_SSA_TREE_OPERAND (op
, stmt
, i
, SSA_OP_DEF
)
4332 bitmap_clear_bit (live
, SSA_NAME_VERSION (op
));
4335 /* Traverse all PHI nodes in BB, updating live. */
4336 for (gphi_iterator si
= gsi_start_phis (bb
); !gsi_end_p (si
);
4339 use_operand_p arg_p
;
4341 gphi
*phi
= si
.phi ();
4342 tree res
= gimple_phi_result (phi
);
4344 if (virtual_operand_p (res
))
4347 FOR_EACH_PHI_ARG (arg_p
, phi
, i
, SSA_OP_USE
)
4349 tree arg
= USE_FROM_PTR (arg_p
);
4350 if (TREE_CODE (arg
) == SSA_NAME
)
4351 bitmap_set_bit (live
, SSA_NAME_VERSION (arg
));
4354 bitmap_clear_bit (live
, SSA_NAME_VERSION (res
));
4358 /* Do an RPO walk over the function computing SSA name liveness
4359 on-the-fly and deciding on assert expressions to insert. */
4362 find_assert_locations (void)
4364 int *rpo
= XNEWVEC (int, last_basic_block_for_fn (cfun
));
4365 int *bb_rpo
= XNEWVEC (int, last_basic_block_for_fn (cfun
));
4366 int *last_rpo
= XCNEWVEC (int, last_basic_block_for_fn (cfun
));
4369 live
= XCNEWVEC (sbitmap
, last_basic_block_for_fn (cfun
));
4370 rpo_cnt
= pre_and_rev_post_order_compute (NULL
, rpo
, false);
4371 for (i
= 0; i
< rpo_cnt
; ++i
)
4374 /* Pre-seed loop latch liveness from loop header PHI nodes. Due to
4375 the order we compute liveness and insert asserts we otherwise
4376 fail to insert asserts into the loop latch. */
4378 FOR_EACH_LOOP (loop
, 0)
4380 i
= loop
->latch
->index
;
4381 unsigned int j
= single_succ_edge (loop
->latch
)->dest_idx
;
4382 for (gphi_iterator gsi
= gsi_start_phis (loop
->header
);
4383 !gsi_end_p (gsi
); gsi_next (&gsi
))
4385 gphi
*phi
= gsi
.phi ();
4386 if (virtual_operand_p (gimple_phi_result (phi
)))
4388 tree arg
= gimple_phi_arg_def (phi
, j
);
4389 if (TREE_CODE (arg
) == SSA_NAME
)
4391 if (live
[i
] == NULL
)
4393 live
[i
] = sbitmap_alloc (num_ssa_names
);
4394 bitmap_clear (live
[i
]);
4396 bitmap_set_bit (live
[i
], SSA_NAME_VERSION (arg
));
4401 for (i
= rpo_cnt
- 1; i
>= 0; --i
)
4403 basic_block bb
= BASIC_BLOCK_FOR_FN (cfun
, rpo
[i
]);
4409 live
[rpo
[i
]] = sbitmap_alloc (num_ssa_names
);
4410 bitmap_clear (live
[rpo
[i
]]);
4413 /* Process BB and update the live information with uses in
4415 find_assert_locations_1 (bb
, live
[rpo
[i
]]);
4417 /* Merge liveness into the predecessor blocks and free it. */
4418 if (!bitmap_empty_p (live
[rpo
[i
]]))
4421 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
4423 int pred
= e
->src
->index
;
4424 if ((e
->flags
& EDGE_DFS_BACK
) || pred
== ENTRY_BLOCK
)
4429 live
[pred
] = sbitmap_alloc (num_ssa_names
);
4430 bitmap_clear (live
[pred
]);
4432 bitmap_ior (live
[pred
], live
[pred
], live
[rpo
[i
]]);
4434 if (bb_rpo
[pred
] < pred_rpo
)
4435 pred_rpo
= bb_rpo
[pred
];
4438 /* Record the RPO number of the last visited block that needs
4439 live information from this block. */
4440 last_rpo
[rpo
[i
]] = pred_rpo
;
4444 sbitmap_free (live
[rpo
[i
]]);
4445 live
[rpo
[i
]] = NULL
;
4448 /* We can free all successors live bitmaps if all their
4449 predecessors have been visited already. */
4450 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
4451 if (last_rpo
[e
->dest
->index
] == i
4452 && live
[e
->dest
->index
])
4454 sbitmap_free (live
[e
->dest
->index
]);
4455 live
[e
->dest
->index
] = NULL
;
4460 XDELETEVEC (bb_rpo
);
4461 XDELETEVEC (last_rpo
);
4462 for (i
= 0; i
< last_basic_block_for_fn (cfun
); ++i
)
4464 sbitmap_free (live
[i
]);
4468 /* Create an ASSERT_EXPR for NAME and insert it in the location
4469 indicated by LOC. Return true if we made any edge insertions. */
4472 process_assert_insertions_for (tree name
, assert_locus
*loc
)
4474 /* Build the comparison expression NAME_i COMP_CODE VAL. */
4477 gimple
*assert_stmt
;
4481 /* If we have X <=> X do not insert an assert expr for that. */
4482 if (loc
->expr
== loc
->val
)
4485 cond
= build2 (loc
->comp_code
, boolean_type_node
, loc
->expr
, loc
->val
);
4486 assert_stmt
= build_assert_expr_for (cond
, name
);
4489 /* We have been asked to insert the assertion on an edge. This
4490 is used only by COND_EXPR and SWITCH_EXPR assertions. */
4491 gcc_checking_assert (gimple_code (gsi_stmt (loc
->si
)) == GIMPLE_COND
4492 || (gimple_code (gsi_stmt (loc
->si
))
4495 gsi_insert_on_edge (loc
->e
, assert_stmt
);
4499 /* If the stmt iterator points at the end then this is an insertion
4500 at the beginning of a block. */
4501 if (gsi_end_p (loc
->si
))
4503 gimple_stmt_iterator si
= gsi_after_labels (loc
->bb
);
4504 gsi_insert_before (&si
, assert_stmt
, GSI_SAME_STMT
);
4508 /* Otherwise, we can insert right after LOC->SI iff the
4509 statement must not be the last statement in the block. */
4510 stmt
= gsi_stmt (loc
->si
);
4511 if (!stmt_ends_bb_p (stmt
))
4513 gsi_insert_after (&loc
->si
, assert_stmt
, GSI_SAME_STMT
);
4517 /* If STMT must be the last statement in BB, we can only insert new
4518 assertions on the non-abnormal edge out of BB. Note that since
4519 STMT is not control flow, there may only be one non-abnormal/eh edge
4521 FOR_EACH_EDGE (e
, ei
, loc
->bb
->succs
)
4522 if (!(e
->flags
& (EDGE_ABNORMAL
|EDGE_EH
)))
4524 gsi_insert_on_edge (e
, assert_stmt
);
4531 /* Qsort helper for sorting assert locations. If stable is true, don't
4532 use iterative_hash_expr because it can be unstable for -fcompare-debug,
4533 on the other side some pointers might be NULL. */
4535 template <bool stable
>
4537 compare_assert_loc (const void *pa
, const void *pb
)
4539 assert_locus
* const a
= *(assert_locus
* const *)pa
;
4540 assert_locus
* const b
= *(assert_locus
* const *)pb
;
4542 /* If stable, some asserts might be optimized away already, sort
4552 if (a
->e
== NULL
&& b
->e
!= NULL
)
4554 else if (a
->e
!= NULL
&& b
->e
== NULL
)
4557 /* After the above checks, we know that (a->e == NULL) == (b->e == NULL),
4558 no need to test both a->e and b->e. */
4560 /* Sort after destination index. */
4563 else if (a
->e
->dest
->index
> b
->e
->dest
->index
)
4565 else if (a
->e
->dest
->index
< b
->e
->dest
->index
)
4568 /* Sort after comp_code. */
4569 if (a
->comp_code
> b
->comp_code
)
4571 else if (a
->comp_code
< b
->comp_code
)
4576 /* E.g. if a->val is ADDR_EXPR of a VAR_DECL, iterative_hash_expr
4577 uses DECL_UID of the VAR_DECL, so sorting might differ between
4578 -g and -g0. When doing the removal of redundant assert exprs
4579 and commonization to successors, this does not matter, but for
4580 the final sort needs to be stable. */
4588 ha
= iterative_hash_expr (a
->expr
, iterative_hash_expr (a
->val
, 0));
4589 hb
= iterative_hash_expr (b
->expr
, iterative_hash_expr (b
->val
, 0));
4592 /* Break the tie using hashing and source/bb index. */
4594 return (a
->e
!= NULL
4595 ? a
->e
->src
->index
- b
->e
->src
->index
4596 : a
->bb
->index
- b
->bb
->index
);
4597 return ha
> hb
? 1 : -1;
4600 /* Process all the insertions registered for every name N_i registered
4601 in NEED_ASSERT_FOR. The list of assertions to be inserted are
4602 found in ASSERTS_FOR[i]. */
4605 process_assert_insertions (void)
4609 bool update_edges_p
= false;
4610 int num_asserts
= 0;
4612 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4613 dump_all_asserts (dump_file
);
4615 EXECUTE_IF_SET_IN_BITMAP (need_assert_for
, 0, i
, bi
)
4617 assert_locus
*loc
= asserts_for
[i
];
4620 auto_vec
<assert_locus
*, 16> asserts
;
4621 for (; loc
; loc
= loc
->next
)
4622 asserts
.safe_push (loc
);
4623 asserts
.qsort (compare_assert_loc
<false>);
4625 /* Push down common asserts to successors and remove redundant ones. */
4627 assert_locus
*common
= NULL
;
4628 unsigned commonj
= 0;
4629 for (unsigned j
= 0; j
< asserts
.length (); ++j
)
4635 || loc
->e
->dest
!= common
->e
->dest
4636 || loc
->comp_code
!= common
->comp_code
4637 || ! operand_equal_p (loc
->val
, common
->val
, 0)
4638 || ! operand_equal_p (loc
->expr
, common
->expr
, 0))
4644 else if (loc
->e
== asserts
[j
-1]->e
)
4646 /* Remove duplicate asserts. */
4647 if (commonj
== j
- 1)
4652 free (asserts
[j
-1]);
4653 asserts
[j
-1] = NULL
;
4658 if (EDGE_COUNT (common
->e
->dest
->preds
) == ecnt
)
4660 /* We have the same assertion on all incoming edges of a BB.
4661 Insert it at the beginning of that block. */
4662 loc
->bb
= loc
->e
->dest
;
4664 loc
->si
= gsi_none ();
4666 /* Clear asserts commoned. */
4667 for (; commonj
!= j
; ++commonj
)
4668 if (asserts
[commonj
])
4670 free (asserts
[commonj
]);
4671 asserts
[commonj
] = NULL
;
4677 /* The asserts vector sorting above might be unstable for
4678 -fcompare-debug, sort again to ensure a stable sort. */
4679 asserts
.qsort (compare_assert_loc
<true>);
4680 for (unsigned j
= 0; j
< asserts
.length (); ++j
)
4685 update_edges_p
|= process_assert_insertions_for (ssa_name (i
), loc
);
4692 gsi_commit_edge_inserts ();
4694 statistics_counter_event (cfun
, "Number of ASSERT_EXPR expressions inserted",
4699 /* Traverse the flowgraph looking for conditional jumps to insert range
4700 expressions. These range expressions are meant to provide information
4701 to optimizations that need to reason in terms of value ranges. They
4702 will not be expanded into RTL. For instance, given:
4711 this pass will transform the code into:
4717 x = ASSERT_EXPR <x, x < y>
4722 y = ASSERT_EXPR <y, x >= y>
4726 The idea is that once copy and constant propagation have run, other
4727 optimizations will be able to determine what ranges of values can 'x'
4728 take in different paths of the code, simply by checking the reaching
4729 definition of 'x'. */
4732 insert_range_assertions (void)
4734 need_assert_for
= BITMAP_ALLOC (NULL
);
4735 asserts_for
= XCNEWVEC (assert_locus
*, num_ssa_names
);
4737 calculate_dominance_info (CDI_DOMINATORS
);
4739 find_assert_locations ();
4740 if (!bitmap_empty_p (need_assert_for
))
4742 process_assert_insertions ();
4743 update_ssa (TODO_update_ssa_no_phi
);
4746 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4748 fprintf (dump_file
, "\nSSA form after inserting ASSERT_EXPRs\n");
4749 dump_function_to_file (current_function_decl
, dump_file
, dump_flags
);
4753 BITMAP_FREE (need_assert_for
);
4756 class vrp_prop
: public ssa_propagation_engine
4759 enum ssa_prop_result
visit_stmt (gimple
*, edge
*, tree
*) FINAL OVERRIDE
;
4760 enum ssa_prop_result
visit_phi (gphi
*) FINAL OVERRIDE
;
4762 void vrp_initialize (void);
4763 void vrp_finalize (bool);
4764 void check_all_array_refs (void);
4765 void check_array_ref (location_t
, tree
, bool);
4766 void search_for_addr_array (tree
, location_t
);
4768 class vr_values vr_values
;
4769 /* Temporary delegator to minimize code churn. */
4770 value_range
*get_value_range (const_tree op
)
4771 { return vr_values
.get_value_range (op
); }
4772 void set_defs_to_varying (gimple
*stmt
)
4773 { return vr_values
.set_defs_to_varying (stmt
); }
4774 void extract_range_from_stmt (gimple
*stmt
, edge
*taken_edge_p
,
4775 tree
*output_p
, value_range
*vr
)
4776 { vr_values
.extract_range_from_stmt (stmt
, taken_edge_p
, output_p
, vr
); }
4777 bool update_value_range (const_tree op
, value_range
*vr
)
4778 { return vr_values
.update_value_range (op
, vr
); }
4779 void extract_range_basic (value_range
*vr
, gimple
*stmt
)
4780 { vr_values
.extract_range_basic (vr
, stmt
); }
4781 void extract_range_from_phi_node (gphi
*phi
, value_range
*vr
)
4782 { vr_values
.extract_range_from_phi_node (phi
, vr
); }
4784 /* Checks one ARRAY_REF in REF, located at LOCUS. Ignores flexible arrays
4785 and "struct" hacks. If VRP can determine that the
4786 array subscript is a constant, check if it is outside valid
4787 range. If the array subscript is a RANGE, warn if it is
4788 non-overlapping with valid range.
4789 IGNORE_OFF_BY_ONE is true if the ARRAY_REF is inside a ADDR_EXPR. */
4792 vrp_prop::check_array_ref (location_t location
, tree ref
,
4793 bool ignore_off_by_one
)
4795 value_range
*vr
= NULL
;
4796 tree low_sub
, up_sub
;
4797 tree low_bound
, up_bound
, up_bound_p1
;
4799 if (TREE_NO_WARNING (ref
))
4802 low_sub
= up_sub
= TREE_OPERAND (ref
, 1);
4803 up_bound
= array_ref_up_bound (ref
);
4806 || TREE_CODE (up_bound
) != INTEGER_CST
4807 || (warn_array_bounds
< 2
4808 && array_at_struct_end_p (ref
)))
4810 /* Accesses to trailing arrays via pointers may access storage
4811 beyond the types array bounds. For such arrays, or for flexible
4812 array members, as well as for other arrays of an unknown size,
4813 replace the upper bound with a more permissive one that assumes
4814 the size of the largest object is PTRDIFF_MAX. */
4815 tree eltsize
= array_ref_element_size (ref
);
4817 if (TREE_CODE (eltsize
) != INTEGER_CST
4818 || integer_zerop (eltsize
))
4820 up_bound
= NULL_TREE
;
4821 up_bound_p1
= NULL_TREE
;
4825 tree maxbound
= TYPE_MAX_VALUE (ptrdiff_type_node
);
4826 tree arg
= TREE_OPERAND (ref
, 0);
4829 if (get_addr_base_and_unit_offset (arg
, &off
) && known_gt (off
, 0))
4830 maxbound
= wide_int_to_tree (sizetype
,
4831 wi::sub (wi::to_wide (maxbound
),
4834 maxbound
= fold_convert (sizetype
, maxbound
);
4836 up_bound_p1
= int_const_binop (TRUNC_DIV_EXPR
, maxbound
, eltsize
);
4838 up_bound
= int_const_binop (MINUS_EXPR
, up_bound_p1
,
4839 build_int_cst (ptrdiff_type_node
, 1));
4843 up_bound_p1
= int_const_binop (PLUS_EXPR
, up_bound
,
4844 build_int_cst (TREE_TYPE (up_bound
), 1));
4846 low_bound
= array_ref_low_bound (ref
);
4848 tree artype
= TREE_TYPE (TREE_OPERAND (ref
, 0));
4851 if (up_bound
&& tree_int_cst_equal (low_bound
, up_bound_p1
))
4853 warning_at (location
, OPT_Warray_bounds
,
4854 "array subscript %E is above array bounds of %qT",
4856 TREE_NO_WARNING (ref
) = 1;
4859 if (TREE_CODE (low_sub
) == SSA_NAME
)
4861 vr
= get_value_range (low_sub
);
4862 if (vr
->type
== VR_RANGE
|| vr
->type
== VR_ANTI_RANGE
)
4864 low_sub
= vr
->type
== VR_RANGE
? vr
->max
: vr
->min
;
4865 up_sub
= vr
->type
== VR_RANGE
? vr
->min
: vr
->max
;
4869 if (vr
&& vr
->type
== VR_ANTI_RANGE
)
4872 && TREE_CODE (up_sub
) == INTEGER_CST
4873 && (ignore_off_by_one
4874 ? tree_int_cst_lt (up_bound
, up_sub
)
4875 : tree_int_cst_le (up_bound
, up_sub
))
4876 && TREE_CODE (low_sub
) == INTEGER_CST
4877 && tree_int_cst_le (low_sub
, low_bound
))
4879 warning_at (location
, OPT_Warray_bounds
,
4880 "array subscript [%E, %E] is outside array bounds of %qT",
4881 low_sub
, up_sub
, artype
);
4882 TREE_NO_WARNING (ref
) = 1;
4886 && TREE_CODE (up_sub
) == INTEGER_CST
4887 && (ignore_off_by_one
4888 ? !tree_int_cst_le (up_sub
, up_bound_p1
)
4889 : !tree_int_cst_le (up_sub
, up_bound
)))
4891 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4893 fprintf (dump_file
, "Array bound warning for ");
4894 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, ref
);
4895 fprintf (dump_file
, "\n");
4897 warning_at (location
, OPT_Warray_bounds
,
4898 "array subscript %E is above array bounds of %qT",
4900 TREE_NO_WARNING (ref
) = 1;
4902 else if (TREE_CODE (low_sub
) == INTEGER_CST
4903 && tree_int_cst_lt (low_sub
, low_bound
))
4905 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4907 fprintf (dump_file
, "Array bound warning for ");
4908 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, ref
);
4909 fprintf (dump_file
, "\n");
4911 warning_at (location
, OPT_Warray_bounds
,
4912 "array subscript %E is below array bounds of %qT",
4914 TREE_NO_WARNING (ref
) = 1;
4918 /* Searches if the expr T, located at LOCATION computes
4919 address of an ARRAY_REF, and call check_array_ref on it. */
4922 vrp_prop::search_for_addr_array (tree t
, location_t location
)
4924 /* Check each ARRAY_REFs in the reference chain. */
4927 if (TREE_CODE (t
) == ARRAY_REF
)
4928 check_array_ref (location
, t
, true /*ignore_off_by_one*/);
4930 t
= TREE_OPERAND (t
, 0);
4932 while (handled_component_p (t
));
4934 if (TREE_CODE (t
) == MEM_REF
4935 && TREE_CODE (TREE_OPERAND (t
, 0)) == ADDR_EXPR
4936 && !TREE_NO_WARNING (t
))
4938 tree tem
= TREE_OPERAND (TREE_OPERAND (t
, 0), 0);
4939 tree low_bound
, up_bound
, el_sz
;
4941 if (TREE_CODE (TREE_TYPE (tem
)) != ARRAY_TYPE
4942 || TREE_CODE (TREE_TYPE (TREE_TYPE (tem
))) == ARRAY_TYPE
4943 || !TYPE_DOMAIN (TREE_TYPE (tem
)))
4946 low_bound
= TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (tem
)));
4947 up_bound
= TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (tem
)));
4948 el_sz
= TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (tem
)));
4950 || TREE_CODE (low_bound
) != INTEGER_CST
4952 || TREE_CODE (up_bound
) != INTEGER_CST
4954 || TREE_CODE (el_sz
) != INTEGER_CST
)
4957 if (!mem_ref_offset (t
).is_constant (&idx
))
4960 idx
= wi::sdiv_trunc (idx
, wi::to_offset (el_sz
));
4963 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4965 fprintf (dump_file
, "Array bound warning for ");
4966 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, t
);
4967 fprintf (dump_file
, "\n");
4969 warning_at (location
, OPT_Warray_bounds
,
4970 "array subscript %wi is below array bounds of %qT",
4971 idx
.to_shwi (), TREE_TYPE (tem
));
4972 TREE_NO_WARNING (t
) = 1;
4974 else if (idx
> (wi::to_offset (up_bound
)
4975 - wi::to_offset (low_bound
) + 1))
4977 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4979 fprintf (dump_file
, "Array bound warning for ");
4980 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, t
);
4981 fprintf (dump_file
, "\n");
4983 warning_at (location
, OPT_Warray_bounds
,
4984 "array subscript %wu is above array bounds of %qT",
4985 idx
.to_uhwi (), TREE_TYPE (tem
));
4986 TREE_NO_WARNING (t
) = 1;
4991 /* walk_tree() callback that checks if *TP is
4992 an ARRAY_REF inside an ADDR_EXPR (in which an array
4993 subscript one outside the valid range is allowed). Call
4994 check_array_ref for each ARRAY_REF found. The location is
4998 check_array_bounds (tree
*tp
, int *walk_subtree
, void *data
)
5001 struct walk_stmt_info
*wi
= (struct walk_stmt_info
*) data
;
5002 location_t location
;
5004 if (EXPR_HAS_LOCATION (t
))
5005 location
= EXPR_LOCATION (t
);
5007 location
= gimple_location (wi
->stmt
);
5009 *walk_subtree
= TRUE
;
5011 vrp_prop
*vrp_prop
= (class vrp_prop
*)wi
->info
;
5012 if (TREE_CODE (t
) == ARRAY_REF
)
5013 vrp_prop
->check_array_ref (location
, t
, false /*ignore_off_by_one*/);
5015 else if (TREE_CODE (t
) == ADDR_EXPR
)
5017 vrp_prop
->search_for_addr_array (t
, location
);
5018 *walk_subtree
= FALSE
;
5024 /* A dom_walker subclass for use by vrp_prop::check_all_array_refs,
5025 to walk over all statements of all reachable BBs and call
5026 check_array_bounds on them. */
5028 class check_array_bounds_dom_walker
: public dom_walker
5031 check_array_bounds_dom_walker (vrp_prop
*prop
)
5032 : dom_walker (CDI_DOMINATORS
,
5033 /* Discover non-executable edges, preserving EDGE_EXECUTABLE
5034 flags, so that we can merge in information on
5035 non-executable edges from vrp_folder . */
5036 REACHABLE_BLOCKS_PRESERVING_FLAGS
),
5038 ~check_array_bounds_dom_walker () {}
5040 edge
before_dom_children (basic_block
) FINAL OVERRIDE
;
5046 /* Implementation of dom_walker::before_dom_children.
5048 Walk over all statements of BB and call check_array_bounds on them,
5049 and determine if there's a unique successor edge. */
5052 check_array_bounds_dom_walker::before_dom_children (basic_block bb
)
5054 gimple_stmt_iterator si
;
5055 for (si
= gsi_start_bb (bb
); !gsi_end_p (si
); gsi_next (&si
))
5057 gimple
*stmt
= gsi_stmt (si
);
5058 struct walk_stmt_info wi
;
5059 if (!gimple_has_location (stmt
)
5060 || is_gimple_debug (stmt
))
5063 memset (&wi
, 0, sizeof (wi
));
5067 walk_gimple_op (stmt
, check_array_bounds
, &wi
);
5070 /* Determine if there's a unique successor edge, and if so, return
5071 that back to dom_walker, ensuring that we don't visit blocks that
5072 became unreachable during the VRP propagation
5073 (PR tree-optimization/83312). */
5074 return find_taken_edge (bb
, NULL_TREE
);
5077 /* Walk over all statements of all reachable BBs and call check_array_bounds
5081 vrp_prop::check_all_array_refs ()
5083 check_array_bounds_dom_walker
w (this);
5084 w
.walk (ENTRY_BLOCK_PTR_FOR_FN (cfun
));
5087 /* Return true if all imm uses of VAR are either in STMT, or
5088 feed (optionally through a chain of single imm uses) GIMPLE_COND
5089 in basic block COND_BB. */
5092 all_imm_uses_in_stmt_or_feed_cond (tree var
, gimple
*stmt
, basic_block cond_bb
)
5094 use_operand_p use_p
, use2_p
;
5095 imm_use_iterator iter
;
5097 FOR_EACH_IMM_USE_FAST (use_p
, iter
, var
)
5098 if (USE_STMT (use_p
) != stmt
)
5100 gimple
*use_stmt
= USE_STMT (use_p
), *use_stmt2
;
5101 if (is_gimple_debug (use_stmt
))
5103 while (is_gimple_assign (use_stmt
)
5104 && TREE_CODE (gimple_assign_lhs (use_stmt
)) == SSA_NAME
5105 && single_imm_use (gimple_assign_lhs (use_stmt
),
5106 &use2_p
, &use_stmt2
))
5107 use_stmt
= use_stmt2
;
5108 if (gimple_code (use_stmt
) != GIMPLE_COND
5109 || gimple_bb (use_stmt
) != cond_bb
)
5122 __builtin_unreachable ();
5124 x_5 = ASSERT_EXPR <x_3, ...>;
5125 If x_3 has no other immediate uses (checked by caller),
5126 var is the x_3 var from ASSERT_EXPR, we can clear low 5 bits
5127 from the non-zero bitmask. */
5130 maybe_set_nonzero_bits (edge e
, tree var
)
5132 basic_block cond_bb
= e
->src
;
5133 gimple
*stmt
= last_stmt (cond_bb
);
5137 || gimple_code (stmt
) != GIMPLE_COND
5138 || gimple_cond_code (stmt
) != ((e
->flags
& EDGE_TRUE_VALUE
)
5139 ? EQ_EXPR
: NE_EXPR
)
5140 || TREE_CODE (gimple_cond_lhs (stmt
)) != SSA_NAME
5141 || !integer_zerop (gimple_cond_rhs (stmt
)))
5144 stmt
= SSA_NAME_DEF_STMT (gimple_cond_lhs (stmt
));
5145 if (!is_gimple_assign (stmt
)
5146 || gimple_assign_rhs_code (stmt
) != BIT_AND_EXPR
5147 || TREE_CODE (gimple_assign_rhs2 (stmt
)) != INTEGER_CST
)
5149 if (gimple_assign_rhs1 (stmt
) != var
)
5153 if (TREE_CODE (gimple_assign_rhs1 (stmt
)) != SSA_NAME
)
5155 stmt2
= SSA_NAME_DEF_STMT (gimple_assign_rhs1 (stmt
));
5156 if (!gimple_assign_cast_p (stmt2
)
5157 || gimple_assign_rhs1 (stmt2
) != var
5158 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (stmt2
))
5159 || (TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (stmt
)))
5160 != TYPE_PRECISION (TREE_TYPE (var
))))
5163 cst
= gimple_assign_rhs2 (stmt
);
5164 set_nonzero_bits (var
, wi::bit_and_not (get_nonzero_bits (var
),
5165 wi::to_wide (cst
)));
5168 /* Convert range assertion expressions into the implied copies and
5169 copy propagate away the copies. Doing the trivial copy propagation
5170 here avoids the need to run the full copy propagation pass after
5173 FIXME, this will eventually lead to copy propagation removing the
5174 names that had useful range information attached to them. For
5175 instance, if we had the assertion N_i = ASSERT_EXPR <N_j, N_j > 3>,
5176 then N_i will have the range [3, +INF].
5178 However, by converting the assertion into the implied copy
5179 operation N_i = N_j, we will then copy-propagate N_j into the uses
5180 of N_i and lose the range information. We may want to hold on to
5181 ASSERT_EXPRs a little while longer as the ranges could be used in
5182 things like jump threading.
5184 The problem with keeping ASSERT_EXPRs around is that passes after
5185 VRP need to handle them appropriately.
5187 Another approach would be to make the range information a first
5188 class property of the SSA_NAME so that it can be queried from
5189 any pass. This is made somewhat more complex by the need for
5190 multiple ranges to be associated with one SSA_NAME. */
5193 remove_range_assertions (void)
5196 gimple_stmt_iterator si
;
5197 /* 1 if looking at ASSERT_EXPRs immediately at the beginning of
5198 a basic block preceeded by GIMPLE_COND branching to it and
5199 __builtin_trap, -1 if not yet checked, 0 otherwise. */
5202 /* Note that the BSI iterator bump happens at the bottom of the
5203 loop and no bump is necessary if we're removing the statement
5204 referenced by the current BSI. */
5205 FOR_EACH_BB_FN (bb
, cfun
)
5206 for (si
= gsi_after_labels (bb
), is_unreachable
= -1; !gsi_end_p (si
);)
5208 gimple
*stmt
= gsi_stmt (si
);
5210 if (is_gimple_assign (stmt
)
5211 && gimple_assign_rhs_code (stmt
) == ASSERT_EXPR
)
5213 tree lhs
= gimple_assign_lhs (stmt
);
5214 tree rhs
= gimple_assign_rhs1 (stmt
);
5217 var
= ASSERT_EXPR_VAR (rhs
);
5219 if (TREE_CODE (var
) == SSA_NAME
5220 && !POINTER_TYPE_P (TREE_TYPE (lhs
))
5221 && SSA_NAME_RANGE_INFO (lhs
))
5223 if (is_unreachable
== -1)
5226 if (single_pred_p (bb
)
5227 && assert_unreachable_fallthru_edge_p
5228 (single_pred_edge (bb
)))
5232 if (x_7 >= 10 && x_7 < 20)
5233 __builtin_unreachable ();
5234 x_8 = ASSERT_EXPR <x_7, ...>;
5235 if the only uses of x_7 are in the ASSERT_EXPR and
5236 in the condition. In that case, we can copy the
5237 range info from x_8 computed in this pass also
5240 && all_imm_uses_in_stmt_or_feed_cond (var
, stmt
,
5243 set_range_info (var
, SSA_NAME_RANGE_TYPE (lhs
),
5244 SSA_NAME_RANGE_INFO (lhs
)->get_min (),
5245 SSA_NAME_RANGE_INFO (lhs
)->get_max ());
5246 maybe_set_nonzero_bits (single_pred_edge (bb
), var
);
5250 /* Propagate the RHS into every use of the LHS. For SSA names
5251 also propagate abnormals as it merely restores the original
5252 IL in this case (an replace_uses_by would assert). */
5253 if (TREE_CODE (var
) == SSA_NAME
)
5255 imm_use_iterator iter
;
5256 use_operand_p use_p
;
5258 FOR_EACH_IMM_USE_STMT (use_stmt
, iter
, lhs
)
5259 FOR_EACH_IMM_USE_ON_STMT (use_p
, iter
)
5260 SET_USE (use_p
, var
);
5263 replace_uses_by (lhs
, var
);
5265 /* And finally, remove the copy, it is not needed. */
5266 gsi_remove (&si
, true);
5267 release_defs (stmt
);
5271 if (!is_gimple_debug (gsi_stmt (si
)))
5278 /* Return true if STMT is interesting for VRP. */
5281 stmt_interesting_for_vrp (gimple
*stmt
)
5283 if (gimple_code (stmt
) == GIMPLE_PHI
)
5285 tree res
= gimple_phi_result (stmt
);
5286 return (!virtual_operand_p (res
)
5287 && (INTEGRAL_TYPE_P (TREE_TYPE (res
))
5288 || POINTER_TYPE_P (TREE_TYPE (res
))));
5290 else if (is_gimple_assign (stmt
) || is_gimple_call (stmt
))
5292 tree lhs
= gimple_get_lhs (stmt
);
5294 /* In general, assignments with virtual operands are not useful
5295 for deriving ranges, with the obvious exception of calls to
5296 builtin functions. */
5297 if (lhs
&& TREE_CODE (lhs
) == SSA_NAME
5298 && (INTEGRAL_TYPE_P (TREE_TYPE (lhs
))
5299 || POINTER_TYPE_P (TREE_TYPE (lhs
)))
5300 && (is_gimple_call (stmt
)
5301 || !gimple_vuse (stmt
)))
5303 else if (is_gimple_call (stmt
) && gimple_call_internal_p (stmt
))
5304 switch (gimple_call_internal_fn (stmt
))
5306 case IFN_ADD_OVERFLOW
:
5307 case IFN_SUB_OVERFLOW
:
5308 case IFN_MUL_OVERFLOW
:
5309 case IFN_ATOMIC_COMPARE_EXCHANGE
:
5310 /* These internal calls return _Complex integer type,
5311 but are interesting to VRP nevertheless. */
5312 if (lhs
&& TREE_CODE (lhs
) == SSA_NAME
)
5319 else if (gimple_code (stmt
) == GIMPLE_COND
5320 || gimple_code (stmt
) == GIMPLE_SWITCH
)
5326 /* Initialization required by ssa_propagate engine. */
5329 vrp_prop::vrp_initialize ()
5333 FOR_EACH_BB_FN (bb
, cfun
)
5335 for (gphi_iterator si
= gsi_start_phis (bb
); !gsi_end_p (si
);
5338 gphi
*phi
= si
.phi ();
5339 if (!stmt_interesting_for_vrp (phi
))
5341 tree lhs
= PHI_RESULT (phi
);
5342 set_value_range_to_varying (get_value_range (lhs
));
5343 prop_set_simulate_again (phi
, false);
5346 prop_set_simulate_again (phi
, true);
5349 for (gimple_stmt_iterator si
= gsi_start_bb (bb
); !gsi_end_p (si
);
5352 gimple
*stmt
= gsi_stmt (si
);
5354 /* If the statement is a control insn, then we do not
5355 want to avoid simulating the statement once. Failure
5356 to do so means that those edges will never get added. */
5357 if (stmt_ends_bb_p (stmt
))
5358 prop_set_simulate_again (stmt
, true);
5359 else if (!stmt_interesting_for_vrp (stmt
))
5361 set_defs_to_varying (stmt
);
5362 prop_set_simulate_again (stmt
, false);
5365 prop_set_simulate_again (stmt
, true);
5370 /* Searches the case label vector VEC for the index *IDX of the CASE_LABEL
5371 that includes the value VAL. The search is restricted to the range
5372 [START_IDX, n - 1] where n is the size of VEC.
5374 If there is a CASE_LABEL for VAL, its index is placed in IDX and true is
5377 If there is no CASE_LABEL for VAL and there is one that is larger than VAL,
5378 it is placed in IDX and false is returned.
5380 If VAL is larger than any CASE_LABEL, n is placed on IDX and false is
5384 find_case_label_index (gswitch
*stmt
, size_t start_idx
, tree val
, size_t *idx
)
5386 size_t n
= gimple_switch_num_labels (stmt
);
5389 /* Find case label for minimum of the value range or the next one.
5390 At each iteration we are searching in [low, high - 1]. */
5392 for (low
= start_idx
, high
= n
; high
!= low
; )
5396 /* Note that i != high, so we never ask for n. */
5397 size_t i
= (high
+ low
) / 2;
5398 t
= gimple_switch_label (stmt
, i
);
5400 /* Cache the result of comparing CASE_LOW and val. */
5401 cmp
= tree_int_cst_compare (CASE_LOW (t
), val
);
5405 /* Ranges cannot be empty. */
5414 if (CASE_HIGH (t
) != NULL
5415 && tree_int_cst_compare (CASE_HIGH (t
), val
) >= 0)
5427 /* Searches the case label vector VEC for the range of CASE_LABELs that is used
5428 for values between MIN and MAX. The first index is placed in MIN_IDX. The
5429 last index is placed in MAX_IDX. If the range of CASE_LABELs is empty
5430 then MAX_IDX < MIN_IDX.
5431 Returns true if the default label is not needed. */
5434 find_case_label_range (gswitch
*stmt
, tree min
, tree max
, size_t *min_idx
,
5438 bool min_take_default
= !find_case_label_index (stmt
, 1, min
, &i
);
5439 bool max_take_default
= !find_case_label_index (stmt
, i
, max
, &j
);
5443 && max_take_default
)
5445 /* Only the default case label reached.
5446 Return an empty range. */
5453 bool take_default
= min_take_default
|| max_take_default
;
5457 if (max_take_default
)
5460 /* If the case label range is continuous, we do not need
5461 the default case label. Verify that. */
5462 high
= CASE_LOW (gimple_switch_label (stmt
, i
));
5463 if (CASE_HIGH (gimple_switch_label (stmt
, i
)))
5464 high
= CASE_HIGH (gimple_switch_label (stmt
, i
));
5465 for (k
= i
+ 1; k
<= j
; ++k
)
5467 low
= CASE_LOW (gimple_switch_label (stmt
, k
));
5468 if (!integer_onep (int_const_binop (MINUS_EXPR
, low
, high
)))
5470 take_default
= true;
5474 if (CASE_HIGH (gimple_switch_label (stmt
, k
)))
5475 high
= CASE_HIGH (gimple_switch_label (stmt
, k
));
5480 return !take_default
;
5484 /* Evaluate statement STMT. If the statement produces a useful range,
5485 return SSA_PROP_INTERESTING and record the SSA name with the
5486 interesting range into *OUTPUT_P.
5488 If STMT is a conditional branch and we can determine its truth
5489 value, the taken edge is recorded in *TAKEN_EDGE_P.
5491 If STMT produces a varying value, return SSA_PROP_VARYING. */
5493 enum ssa_prop_result
5494 vrp_prop::visit_stmt (gimple
*stmt
, edge
*taken_edge_p
, tree
*output_p
)
5496 value_range vr
= VR_INITIALIZER
;
5497 tree lhs
= gimple_get_lhs (stmt
);
5498 extract_range_from_stmt (stmt
, taken_edge_p
, output_p
, &vr
);
5502 if (update_value_range (*output_p
, &vr
))
5504 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
5506 fprintf (dump_file
, "Found new range for ");
5507 print_generic_expr (dump_file
, *output_p
);
5508 fprintf (dump_file
, ": ");
5509 dump_value_range (dump_file
, &vr
);
5510 fprintf (dump_file
, "\n");
5513 if (vr
.type
== VR_VARYING
)
5514 return SSA_PROP_VARYING
;
5516 return SSA_PROP_INTERESTING
;
5518 return SSA_PROP_NOT_INTERESTING
;
5521 if (is_gimple_call (stmt
) && gimple_call_internal_p (stmt
))
5522 switch (gimple_call_internal_fn (stmt
))
5524 case IFN_ADD_OVERFLOW
:
5525 case IFN_SUB_OVERFLOW
:
5526 case IFN_MUL_OVERFLOW
:
5527 case IFN_ATOMIC_COMPARE_EXCHANGE
:
5528 /* These internal calls return _Complex integer type,
5529 which VRP does not track, but the immediate uses
5530 thereof might be interesting. */
5531 if (lhs
&& TREE_CODE (lhs
) == SSA_NAME
)
5533 imm_use_iterator iter
;
5534 use_operand_p use_p
;
5535 enum ssa_prop_result res
= SSA_PROP_VARYING
;
5537 set_value_range_to_varying (get_value_range (lhs
));
5539 FOR_EACH_IMM_USE_FAST (use_p
, iter
, lhs
)
5541 gimple
*use_stmt
= USE_STMT (use_p
);
5542 if (!is_gimple_assign (use_stmt
))
5544 enum tree_code rhs_code
= gimple_assign_rhs_code (use_stmt
);
5545 if (rhs_code
!= REALPART_EXPR
&& rhs_code
!= IMAGPART_EXPR
)
5547 tree rhs1
= gimple_assign_rhs1 (use_stmt
);
5548 tree use_lhs
= gimple_assign_lhs (use_stmt
);
5549 if (TREE_CODE (rhs1
) != rhs_code
5550 || TREE_OPERAND (rhs1
, 0) != lhs
5551 || TREE_CODE (use_lhs
) != SSA_NAME
5552 || !stmt_interesting_for_vrp (use_stmt
)
5553 || (!INTEGRAL_TYPE_P (TREE_TYPE (use_lhs
))
5554 || !TYPE_MIN_VALUE (TREE_TYPE (use_lhs
))
5555 || !TYPE_MAX_VALUE (TREE_TYPE (use_lhs
))))
5558 /* If there is a change in the value range for any of the
5559 REALPART_EXPR/IMAGPART_EXPR immediate uses, return
5560 SSA_PROP_INTERESTING. If there are any REALPART_EXPR
5561 or IMAGPART_EXPR immediate uses, but none of them have
5562 a change in their value ranges, return
5563 SSA_PROP_NOT_INTERESTING. If there are no
5564 {REAL,IMAG}PART_EXPR uses at all,
5565 return SSA_PROP_VARYING. */
5566 value_range new_vr
= VR_INITIALIZER
;
5567 extract_range_basic (&new_vr
, use_stmt
);
5568 value_range
*old_vr
= get_value_range (use_lhs
);
5569 if (old_vr
->type
!= new_vr
.type
5570 || !vrp_operand_equal_p (old_vr
->min
, new_vr
.min
)
5571 || !vrp_operand_equal_p (old_vr
->max
, new_vr
.max
)
5572 || !vrp_bitmap_equal_p (old_vr
->equiv
, new_vr
.equiv
))
5573 res
= SSA_PROP_INTERESTING
;
5575 res
= SSA_PROP_NOT_INTERESTING
;
5576 BITMAP_FREE (new_vr
.equiv
);
5577 if (res
== SSA_PROP_INTERESTING
)
5591 /* All other statements produce nothing of interest for VRP, so mark
5592 their outputs varying and prevent further simulation. */
5593 set_defs_to_varying (stmt
);
5595 return (*taken_edge_p
) ? SSA_PROP_INTERESTING
: SSA_PROP_VARYING
;
5598 /* Union the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
5599 { VR1TYPE, VR0MIN, VR0MAX } and store the result
5600 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
5601 possible such range. The resulting range is not canonicalized. */
5604 union_ranges (enum value_range_type
*vr0type
,
5605 tree
*vr0min
, tree
*vr0max
,
5606 enum value_range_type vr1type
,
5607 tree vr1min
, tree vr1max
)
5609 bool mineq
= vrp_operand_equal_p (*vr0min
, vr1min
);
5610 bool maxeq
= vrp_operand_equal_p (*vr0max
, vr1max
);
5612 /* [] is vr0, () is vr1 in the following classification comments. */
5616 if (*vr0type
== vr1type
)
5617 /* Nothing to do for equal ranges. */
5619 else if ((*vr0type
== VR_RANGE
5620 && vr1type
== VR_ANTI_RANGE
)
5621 || (*vr0type
== VR_ANTI_RANGE
5622 && vr1type
== VR_RANGE
))
5624 /* For anti-range with range union the result is varying. */
5630 else if (operand_less_p (*vr0max
, vr1min
) == 1
5631 || operand_less_p (vr1max
, *vr0min
) == 1)
5633 /* [ ] ( ) or ( ) [ ]
5634 If the ranges have an empty intersection, result of the union
5635 operation is the anti-range or if both are anti-ranges
5637 if (*vr0type
== VR_ANTI_RANGE
5638 && vr1type
== VR_ANTI_RANGE
)
5640 else if (*vr0type
== VR_ANTI_RANGE
5641 && vr1type
== VR_RANGE
)
5643 else if (*vr0type
== VR_RANGE
5644 && vr1type
== VR_ANTI_RANGE
)
5650 else if (*vr0type
== VR_RANGE
5651 && vr1type
== VR_RANGE
)
5653 /* The result is the convex hull of both ranges. */
5654 if (operand_less_p (*vr0max
, vr1min
) == 1)
5656 /* If the result can be an anti-range, create one. */
5657 if (TREE_CODE (*vr0max
) == INTEGER_CST
5658 && TREE_CODE (vr1min
) == INTEGER_CST
5659 && vrp_val_is_min (*vr0min
)
5660 && vrp_val_is_max (vr1max
))
5662 tree min
= int_const_binop (PLUS_EXPR
,
5664 build_int_cst (TREE_TYPE (*vr0max
), 1));
5665 tree max
= int_const_binop (MINUS_EXPR
,
5667 build_int_cst (TREE_TYPE (vr1min
), 1));
5668 if (!operand_less_p (max
, min
))
5670 *vr0type
= VR_ANTI_RANGE
;
5682 /* If the result can be an anti-range, create one. */
5683 if (TREE_CODE (vr1max
) == INTEGER_CST
5684 && TREE_CODE (*vr0min
) == INTEGER_CST
5685 && vrp_val_is_min (vr1min
)
5686 && vrp_val_is_max (*vr0max
))
5688 tree min
= int_const_binop (PLUS_EXPR
,
5690 build_int_cst (TREE_TYPE (vr1max
), 1));
5691 tree max
= int_const_binop (MINUS_EXPR
,
5693 build_int_cst (TREE_TYPE (*vr0min
), 1));
5694 if (!operand_less_p (max
, min
))
5696 *vr0type
= VR_ANTI_RANGE
;
5710 else if ((maxeq
|| operand_less_p (vr1max
, *vr0max
) == 1)
5711 && (mineq
|| operand_less_p (*vr0min
, vr1min
) == 1))
5713 /* [ ( ) ] or [( ) ] or [ ( )] */
5714 if (*vr0type
== VR_RANGE
5715 && vr1type
== VR_RANGE
)
5717 else if (*vr0type
== VR_ANTI_RANGE
5718 && vr1type
== VR_ANTI_RANGE
)
5724 else if (*vr0type
== VR_ANTI_RANGE
5725 && vr1type
== VR_RANGE
)
5727 /* Arbitrarily choose the right or left gap. */
5728 if (!mineq
&& TREE_CODE (vr1min
) == INTEGER_CST
)
5729 *vr0max
= int_const_binop (MINUS_EXPR
, vr1min
,
5730 build_int_cst (TREE_TYPE (vr1min
), 1));
5731 else if (!maxeq
&& TREE_CODE (vr1max
) == INTEGER_CST
)
5732 *vr0min
= int_const_binop (PLUS_EXPR
, vr1max
,
5733 build_int_cst (TREE_TYPE (vr1max
), 1));
5737 else if (*vr0type
== VR_RANGE
5738 && vr1type
== VR_ANTI_RANGE
)
5739 /* The result covers everything. */
5744 else if ((maxeq
|| operand_less_p (*vr0max
, vr1max
) == 1)
5745 && (mineq
|| operand_less_p (vr1min
, *vr0min
) == 1))
5747 /* ( [ ] ) or ([ ] ) or ( [ ]) */
5748 if (*vr0type
== VR_RANGE
5749 && vr1type
== VR_RANGE
)
5755 else if (*vr0type
== VR_ANTI_RANGE
5756 && vr1type
== VR_ANTI_RANGE
)
5758 else if (*vr0type
== VR_RANGE
5759 && vr1type
== VR_ANTI_RANGE
)
5761 *vr0type
= VR_ANTI_RANGE
;
5762 if (!mineq
&& TREE_CODE (*vr0min
) == INTEGER_CST
)
5764 *vr0max
= int_const_binop (MINUS_EXPR
, *vr0min
,
5765 build_int_cst (TREE_TYPE (*vr0min
), 1));
5768 else if (!maxeq
&& TREE_CODE (*vr0max
) == INTEGER_CST
)
5770 *vr0min
= int_const_binop (PLUS_EXPR
, *vr0max
,
5771 build_int_cst (TREE_TYPE (*vr0max
), 1));
5777 else if (*vr0type
== VR_ANTI_RANGE
5778 && vr1type
== VR_RANGE
)
5779 /* The result covers everything. */
5784 else if ((operand_less_p (vr1min
, *vr0max
) == 1
5785 || operand_equal_p (vr1min
, *vr0max
, 0))
5786 && operand_less_p (*vr0min
, vr1min
) == 1
5787 && operand_less_p (*vr0max
, vr1max
) == 1)
5789 /* [ ( ] ) or [ ]( ) */
5790 if (*vr0type
== VR_RANGE
5791 && vr1type
== VR_RANGE
)
5793 else if (*vr0type
== VR_ANTI_RANGE
5794 && vr1type
== VR_ANTI_RANGE
)
5796 else if (*vr0type
== VR_ANTI_RANGE
5797 && vr1type
== VR_RANGE
)
5799 if (TREE_CODE (vr1min
) == INTEGER_CST
)
5800 *vr0max
= int_const_binop (MINUS_EXPR
, vr1min
,
5801 build_int_cst (TREE_TYPE (vr1min
), 1));
5805 else if (*vr0type
== VR_RANGE
5806 && vr1type
== VR_ANTI_RANGE
)
5808 if (TREE_CODE (*vr0max
) == INTEGER_CST
)
5811 *vr0min
= int_const_binop (PLUS_EXPR
, *vr0max
,
5812 build_int_cst (TREE_TYPE (*vr0max
), 1));
5821 else if ((operand_less_p (*vr0min
, vr1max
) == 1
5822 || operand_equal_p (*vr0min
, vr1max
, 0))
5823 && operand_less_p (vr1min
, *vr0min
) == 1
5824 && operand_less_p (vr1max
, *vr0max
) == 1)
5826 /* ( [ ) ] or ( )[ ] */
5827 if (*vr0type
== VR_RANGE
5828 && vr1type
== VR_RANGE
)
5830 else if (*vr0type
== VR_ANTI_RANGE
5831 && vr1type
== VR_ANTI_RANGE
)
5833 else if (*vr0type
== VR_ANTI_RANGE
5834 && vr1type
== VR_RANGE
)
5836 if (TREE_CODE (vr1max
) == INTEGER_CST
)
5837 *vr0min
= int_const_binop (PLUS_EXPR
, vr1max
,
5838 build_int_cst (TREE_TYPE (vr1max
), 1));
5842 else if (*vr0type
== VR_RANGE
5843 && vr1type
== VR_ANTI_RANGE
)
5845 if (TREE_CODE (*vr0min
) == INTEGER_CST
)
5849 *vr0max
= int_const_binop (MINUS_EXPR
, *vr0min
,
5850 build_int_cst (TREE_TYPE (*vr0min
), 1));
5864 *vr0type
= VR_VARYING
;
5865 *vr0min
= NULL_TREE
;
5866 *vr0max
= NULL_TREE
;
5869 /* Intersect the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
5870 { VR1TYPE, VR0MIN, VR0MAX } and store the result
5871 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
5872 possible such range. The resulting range is not canonicalized. */
5875 intersect_ranges (enum value_range_type
*vr0type
,
5876 tree
*vr0min
, tree
*vr0max
,
5877 enum value_range_type vr1type
,
5878 tree vr1min
, tree vr1max
)
5880 bool mineq
= vrp_operand_equal_p (*vr0min
, vr1min
);
5881 bool maxeq
= vrp_operand_equal_p (*vr0max
, vr1max
);
5883 /* [] is vr0, () is vr1 in the following classification comments. */
5887 if (*vr0type
== vr1type
)
5888 /* Nothing to do for equal ranges. */
5890 else if ((*vr0type
== VR_RANGE
5891 && vr1type
== VR_ANTI_RANGE
)
5892 || (*vr0type
== VR_ANTI_RANGE
5893 && vr1type
== VR_RANGE
))
5895 /* For anti-range with range intersection the result is empty. */
5896 *vr0type
= VR_UNDEFINED
;
5897 *vr0min
= NULL_TREE
;
5898 *vr0max
= NULL_TREE
;
5903 else if (operand_less_p (*vr0max
, vr1min
) == 1
5904 || operand_less_p (vr1max
, *vr0min
) == 1)
5906 /* [ ] ( ) or ( ) [ ]
5907 If the ranges have an empty intersection, the result of the
5908 intersect operation is the range for intersecting an
5909 anti-range with a range or empty when intersecting two ranges. */
5910 if (*vr0type
== VR_RANGE
5911 && vr1type
== VR_ANTI_RANGE
)
5913 else if (*vr0type
== VR_ANTI_RANGE
5914 && vr1type
== VR_RANGE
)
5920 else if (*vr0type
== VR_RANGE
5921 && vr1type
== VR_RANGE
)
5923 *vr0type
= VR_UNDEFINED
;
5924 *vr0min
= NULL_TREE
;
5925 *vr0max
= NULL_TREE
;
5927 else if (*vr0type
== VR_ANTI_RANGE
5928 && vr1type
== VR_ANTI_RANGE
)
5930 /* If the anti-ranges are adjacent to each other merge them. */
5931 if (TREE_CODE (*vr0max
) == INTEGER_CST
5932 && TREE_CODE (vr1min
) == INTEGER_CST
5933 && operand_less_p (*vr0max
, vr1min
) == 1
5934 && integer_onep (int_const_binop (MINUS_EXPR
,
5937 else if (TREE_CODE (vr1max
) == INTEGER_CST
5938 && TREE_CODE (*vr0min
) == INTEGER_CST
5939 && operand_less_p (vr1max
, *vr0min
) == 1
5940 && integer_onep (int_const_binop (MINUS_EXPR
,
5943 /* Else arbitrarily take VR0. */
5946 else if ((maxeq
|| operand_less_p (vr1max
, *vr0max
) == 1)
5947 && (mineq
|| operand_less_p (*vr0min
, vr1min
) == 1))
5949 /* [ ( ) ] or [( ) ] or [ ( )] */
5950 if (*vr0type
== VR_RANGE
5951 && vr1type
== VR_RANGE
)
5953 /* If both are ranges the result is the inner one. */
5958 else if (*vr0type
== VR_RANGE
5959 && vr1type
== VR_ANTI_RANGE
)
5961 /* Choose the right gap if the left one is empty. */
5964 if (TREE_CODE (vr1max
) != INTEGER_CST
)
5966 else if (TYPE_PRECISION (TREE_TYPE (vr1max
)) == 1
5967 && !TYPE_UNSIGNED (TREE_TYPE (vr1max
)))
5969 = int_const_binop (MINUS_EXPR
, vr1max
,
5970 build_int_cst (TREE_TYPE (vr1max
), -1));
5973 = int_const_binop (PLUS_EXPR
, vr1max
,
5974 build_int_cst (TREE_TYPE (vr1max
), 1));
5976 /* Choose the left gap if the right one is empty. */
5979 if (TREE_CODE (vr1min
) != INTEGER_CST
)
5981 else if (TYPE_PRECISION (TREE_TYPE (vr1min
)) == 1
5982 && !TYPE_UNSIGNED (TREE_TYPE (vr1min
)))
5984 = int_const_binop (PLUS_EXPR
, vr1min
,
5985 build_int_cst (TREE_TYPE (vr1min
), -1));
5988 = int_const_binop (MINUS_EXPR
, vr1min
,
5989 build_int_cst (TREE_TYPE (vr1min
), 1));
5991 /* Choose the anti-range if the range is effectively varying. */
5992 else if (vrp_val_is_min (*vr0min
)
5993 && vrp_val_is_max (*vr0max
))
5999 /* Else choose the range. */
6001 else if (*vr0type
== VR_ANTI_RANGE
6002 && vr1type
== VR_ANTI_RANGE
)
6003 /* If both are anti-ranges the result is the outer one. */
6005 else if (*vr0type
== VR_ANTI_RANGE
6006 && vr1type
== VR_RANGE
)
6008 /* The intersection is empty. */
6009 *vr0type
= VR_UNDEFINED
;
6010 *vr0min
= NULL_TREE
;
6011 *vr0max
= NULL_TREE
;
6016 else if ((maxeq
|| operand_less_p (*vr0max
, vr1max
) == 1)
6017 && (mineq
|| operand_less_p (vr1min
, *vr0min
) == 1))
6019 /* ( [ ] ) or ([ ] ) or ( [ ]) */
6020 if (*vr0type
== VR_RANGE
6021 && vr1type
== VR_RANGE
)
6022 /* Choose the inner range. */
6024 else if (*vr0type
== VR_ANTI_RANGE
6025 && vr1type
== VR_RANGE
)
6027 /* Choose the right gap if the left is empty. */
6030 *vr0type
= VR_RANGE
;
6031 if (TREE_CODE (*vr0max
) != INTEGER_CST
)
6033 else if (TYPE_PRECISION (TREE_TYPE (*vr0max
)) == 1
6034 && !TYPE_UNSIGNED (TREE_TYPE (*vr0max
)))
6036 = int_const_binop (MINUS_EXPR
, *vr0max
,
6037 build_int_cst (TREE_TYPE (*vr0max
), -1));
6040 = int_const_binop (PLUS_EXPR
, *vr0max
,
6041 build_int_cst (TREE_TYPE (*vr0max
), 1));
6044 /* Choose the left gap if the right is empty. */
6047 *vr0type
= VR_RANGE
;
6048 if (TREE_CODE (*vr0min
) != INTEGER_CST
)
6050 else if (TYPE_PRECISION (TREE_TYPE (*vr0min
)) == 1
6051 && !TYPE_UNSIGNED (TREE_TYPE (*vr0min
)))
6053 = int_const_binop (PLUS_EXPR
, *vr0min
,
6054 build_int_cst (TREE_TYPE (*vr0min
), -1));
6057 = int_const_binop (MINUS_EXPR
, *vr0min
,
6058 build_int_cst (TREE_TYPE (*vr0min
), 1));
6061 /* Choose the anti-range if the range is effectively varying. */
6062 else if (vrp_val_is_min (vr1min
)
6063 && vrp_val_is_max (vr1max
))
6065 /* Choose the anti-range if it is ~[0,0], that range is special
6066 enough to special case when vr1's range is relatively wide.
6067 At least for types bigger than int - this covers pointers
6068 and arguments to functions like ctz. */
6069 else if (*vr0min
== *vr0max
6070 && integer_zerop (*vr0min
)
6071 && ((TYPE_PRECISION (TREE_TYPE (*vr0min
))
6072 >= TYPE_PRECISION (integer_type_node
))
6073 || POINTER_TYPE_P (TREE_TYPE (*vr0min
)))
6074 && TREE_CODE (vr1max
) == INTEGER_CST
6075 && TREE_CODE (vr1min
) == INTEGER_CST
6076 && (wi::clz (wi::to_wide (vr1max
) - wi::to_wide (vr1min
))
6077 < TYPE_PRECISION (TREE_TYPE (*vr0min
)) / 2))
6079 /* Else choose the range. */
6087 else if (*vr0type
== VR_ANTI_RANGE
6088 && vr1type
== VR_ANTI_RANGE
)
6090 /* If both are anti-ranges the result is the outer one. */
6095 else if (vr1type
== VR_ANTI_RANGE
6096 && *vr0type
== VR_RANGE
)
6098 /* The intersection is empty. */
6099 *vr0type
= VR_UNDEFINED
;
6100 *vr0min
= NULL_TREE
;
6101 *vr0max
= NULL_TREE
;
6106 else if ((operand_less_p (vr1min
, *vr0max
) == 1
6107 || operand_equal_p (vr1min
, *vr0max
, 0))
6108 && operand_less_p (*vr0min
, vr1min
) == 1)
6110 /* [ ( ] ) or [ ]( ) */
6111 if (*vr0type
== VR_ANTI_RANGE
6112 && vr1type
== VR_ANTI_RANGE
)
6114 else if (*vr0type
== VR_RANGE
6115 && vr1type
== VR_RANGE
)
6117 else if (*vr0type
== VR_RANGE
6118 && vr1type
== VR_ANTI_RANGE
)
6120 if (TREE_CODE (vr1min
) == INTEGER_CST
)
6121 *vr0max
= int_const_binop (MINUS_EXPR
, vr1min
,
6122 build_int_cst (TREE_TYPE (vr1min
), 1));
6126 else if (*vr0type
== VR_ANTI_RANGE
6127 && vr1type
== VR_RANGE
)
6129 *vr0type
= VR_RANGE
;
6130 if (TREE_CODE (*vr0max
) == INTEGER_CST
)
6131 *vr0min
= int_const_binop (PLUS_EXPR
, *vr0max
,
6132 build_int_cst (TREE_TYPE (*vr0max
), 1));
6140 else if ((operand_less_p (*vr0min
, vr1max
) == 1
6141 || operand_equal_p (*vr0min
, vr1max
, 0))
6142 && operand_less_p (vr1min
, *vr0min
) == 1)
6144 /* ( [ ) ] or ( )[ ] */
6145 if (*vr0type
== VR_ANTI_RANGE
6146 && vr1type
== VR_ANTI_RANGE
)
6148 else if (*vr0type
== VR_RANGE
6149 && vr1type
== VR_RANGE
)
6151 else if (*vr0type
== VR_RANGE
6152 && vr1type
== VR_ANTI_RANGE
)
6154 if (TREE_CODE (vr1max
) == INTEGER_CST
)
6155 *vr0min
= int_const_binop (PLUS_EXPR
, vr1max
,
6156 build_int_cst (TREE_TYPE (vr1max
), 1));
6160 else if (*vr0type
== VR_ANTI_RANGE
6161 && vr1type
== VR_RANGE
)
6163 *vr0type
= VR_RANGE
;
6164 if (TREE_CODE (*vr0min
) == INTEGER_CST
)
6165 *vr0max
= int_const_binop (MINUS_EXPR
, *vr0min
,
6166 build_int_cst (TREE_TYPE (*vr0min
), 1));
6175 /* As a fallback simply use { *VRTYPE, *VR0MIN, *VR0MAX } as
6176 result for the intersection. That's always a conservative
6177 correct estimate unless VR1 is a constant singleton range
6178 in which case we choose that. */
6179 if (vr1type
== VR_RANGE
6180 && is_gimple_min_invariant (vr1min
)
6181 && vrp_operand_equal_p (vr1min
, vr1max
))
6192 /* Intersect the two value-ranges *VR0 and *VR1 and store the result
6193 in *VR0. This may not be the smallest possible such range. */
6196 vrp_intersect_ranges_1 (value_range
*vr0
, value_range
*vr1
)
6200 /* If either range is VR_VARYING the other one wins. */
6201 if (vr1
->type
== VR_VARYING
)
6203 if (vr0
->type
== VR_VARYING
)
6205 copy_value_range (vr0
, vr1
);
6209 /* When either range is VR_UNDEFINED the resulting range is
6210 VR_UNDEFINED, too. */
6211 if (vr0
->type
== VR_UNDEFINED
)
6213 if (vr1
->type
== VR_UNDEFINED
)
6215 set_value_range_to_undefined (vr0
);
6219 /* Save the original vr0 so we can return it as conservative intersection
6220 result when our worker turns things to varying. */
6222 intersect_ranges (&vr0
->type
, &vr0
->min
, &vr0
->max
,
6223 vr1
->type
, vr1
->min
, vr1
->max
);
6224 /* Make sure to canonicalize the result though as the inversion of a
6225 VR_RANGE can still be a VR_RANGE. */
6226 set_and_canonicalize_value_range (vr0
, vr0
->type
,
6227 vr0
->min
, vr0
->max
, vr0
->equiv
);
6228 /* If that failed, use the saved original VR0. */
6229 if (vr0
->type
== VR_VARYING
)
6234 /* If the result is VR_UNDEFINED there is no need to mess with
6235 the equivalencies. */
6236 if (vr0
->type
== VR_UNDEFINED
)
6239 /* The resulting set of equivalences for range intersection is the union of
6241 if (vr0
->equiv
&& vr1
->equiv
&& vr0
->equiv
!= vr1
->equiv
)
6242 bitmap_ior_into (vr0
->equiv
, vr1
->equiv
);
6243 else if (vr1
->equiv
&& !vr0
->equiv
)
6245 /* All equivalence bitmaps are allocated from the same obstack. So
6246 we can use the obstack associated with VR to allocate vr0->equiv. */
6247 vr0
->equiv
= BITMAP_ALLOC (vr1
->equiv
->obstack
);
6248 bitmap_copy (vr0
->equiv
, vr1
->equiv
);
6253 vrp_intersect_ranges (value_range
*vr0
, value_range
*vr1
)
6255 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6257 fprintf (dump_file
, "Intersecting\n ");
6258 dump_value_range (dump_file
, vr0
);
6259 fprintf (dump_file
, "\nand\n ");
6260 dump_value_range (dump_file
, vr1
);
6261 fprintf (dump_file
, "\n");
6263 vrp_intersect_ranges_1 (vr0
, vr1
);
6264 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6266 fprintf (dump_file
, "to\n ");
6267 dump_value_range (dump_file
, vr0
);
6268 fprintf (dump_file
, "\n");
6272 /* Meet operation for value ranges. Given two value ranges VR0 and
6273 VR1, store in VR0 a range that contains both VR0 and VR1. This
6274 may not be the smallest possible such range. */
6277 vrp_meet_1 (value_range
*vr0
, const value_range
*vr1
)
6281 if (vr0
->type
== VR_UNDEFINED
)
6283 set_value_range (vr0
, vr1
->type
, vr1
->min
, vr1
->max
, vr1
->equiv
);
6287 if (vr1
->type
== VR_UNDEFINED
)
6289 /* VR0 already has the resulting range. */
6293 if (vr0
->type
== VR_VARYING
)
6295 /* Nothing to do. VR0 already has the resulting range. */
6299 if (vr1
->type
== VR_VARYING
)
6301 set_value_range_to_varying (vr0
);
6306 union_ranges (&vr0
->type
, &vr0
->min
, &vr0
->max
,
6307 vr1
->type
, vr1
->min
, vr1
->max
);
6308 if (vr0
->type
== VR_VARYING
)
6310 /* Failed to find an efficient meet. Before giving up and setting
6311 the result to VARYING, see if we can at least derive a useful
6312 anti-range. FIXME, all this nonsense about distinguishing
6313 anti-ranges from ranges is necessary because of the odd
6314 semantics of range_includes_zero_p and friends. */
6315 if (((saved
.type
== VR_RANGE
6316 && range_includes_zero_p (saved
.min
, saved
.max
) == 0)
6317 || (saved
.type
== VR_ANTI_RANGE
6318 && range_includes_zero_p (saved
.min
, saved
.max
) == 1))
6319 && ((vr1
->type
== VR_RANGE
6320 && range_includes_zero_p (vr1
->min
, vr1
->max
) == 0)
6321 || (vr1
->type
== VR_ANTI_RANGE
6322 && range_includes_zero_p (vr1
->min
, vr1
->max
) == 1)))
6324 set_value_range_to_nonnull (vr0
, TREE_TYPE (saved
.min
));
6326 /* Since this meet operation did not result from the meeting of
6327 two equivalent names, VR0 cannot have any equivalences. */
6329 bitmap_clear (vr0
->equiv
);
6333 set_value_range_to_varying (vr0
);
6336 set_and_canonicalize_value_range (vr0
, vr0
->type
, vr0
->min
, vr0
->max
,
6338 if (vr0
->type
== VR_VARYING
)
6341 /* The resulting set of equivalences is always the intersection of
6343 if (vr0
->equiv
&& vr1
->equiv
&& vr0
->equiv
!= vr1
->equiv
)
6344 bitmap_and_into (vr0
->equiv
, vr1
->equiv
);
6345 else if (vr0
->equiv
&& !vr1
->equiv
)
6346 bitmap_clear (vr0
->equiv
);
6350 vrp_meet (value_range
*vr0
, const value_range
*vr1
)
6352 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6354 fprintf (dump_file
, "Meeting\n ");
6355 dump_value_range (dump_file
, vr0
);
6356 fprintf (dump_file
, "\nand\n ");
6357 dump_value_range (dump_file
, vr1
);
6358 fprintf (dump_file
, "\n");
6360 vrp_meet_1 (vr0
, vr1
);
6361 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6363 fprintf (dump_file
, "to\n ");
6364 dump_value_range (dump_file
, vr0
);
6365 fprintf (dump_file
, "\n");
6370 /* Visit all arguments for PHI node PHI that flow through executable
6371 edges. If a valid value range can be derived from all the incoming
6372 value ranges, set a new range for the LHS of PHI. */
6374 enum ssa_prop_result
6375 vrp_prop::visit_phi (gphi
*phi
)
6377 tree lhs
= PHI_RESULT (phi
);
6378 value_range vr_result
= VR_INITIALIZER
;
6379 extract_range_from_phi_node (phi
, &vr_result
);
6380 if (update_value_range (lhs
, &vr_result
))
6382 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6384 fprintf (dump_file
, "Found new range for ");
6385 print_generic_expr (dump_file
, lhs
);
6386 fprintf (dump_file
, ": ");
6387 dump_value_range (dump_file
, &vr_result
);
6388 fprintf (dump_file
, "\n");
6391 if (vr_result
.type
== VR_VARYING
)
6392 return SSA_PROP_VARYING
;
6394 return SSA_PROP_INTERESTING
;
6397 /* Nothing changed, don't add outgoing edges. */
6398 return SSA_PROP_NOT_INTERESTING
;
6401 class vrp_folder
: public substitute_and_fold_engine
6404 tree
get_value (tree
) FINAL OVERRIDE
;
6405 bool fold_stmt (gimple_stmt_iterator
*) FINAL OVERRIDE
;
6406 bool fold_predicate_in (gimple_stmt_iterator
*);
6408 class vr_values
*vr_values
;
6411 tree
vrp_evaluate_conditional (tree_code code
, tree op0
,
6412 tree op1
, gimple
*stmt
)
6413 { return vr_values
->vrp_evaluate_conditional (code
, op0
, op1
, stmt
); }
6414 bool simplify_stmt_using_ranges (gimple_stmt_iterator
*gsi
)
6415 { return vr_values
->simplify_stmt_using_ranges (gsi
); }
6416 tree
op_with_constant_singleton_value_range (tree op
)
6417 { return vr_values
->op_with_constant_singleton_value_range (op
); }
6420 /* If the statement pointed by SI has a predicate whose value can be
6421 computed using the value range information computed by VRP, compute
6422 its value and return true. Otherwise, return false. */
6425 vrp_folder::fold_predicate_in (gimple_stmt_iterator
*si
)
6427 bool assignment_p
= false;
6429 gimple
*stmt
= gsi_stmt (*si
);
6431 if (is_gimple_assign (stmt
)
6432 && TREE_CODE_CLASS (gimple_assign_rhs_code (stmt
)) == tcc_comparison
)
6434 assignment_p
= true;
6435 val
= vrp_evaluate_conditional (gimple_assign_rhs_code (stmt
),
6436 gimple_assign_rhs1 (stmt
),
6437 gimple_assign_rhs2 (stmt
),
6440 else if (gcond
*cond_stmt
= dyn_cast
<gcond
*> (stmt
))
6441 val
= vrp_evaluate_conditional (gimple_cond_code (cond_stmt
),
6442 gimple_cond_lhs (cond_stmt
),
6443 gimple_cond_rhs (cond_stmt
),
6451 val
= fold_convert (gimple_expr_type (stmt
), val
);
6455 fprintf (dump_file
, "Folding predicate ");
6456 print_gimple_expr (dump_file
, stmt
, 0);
6457 fprintf (dump_file
, " to ");
6458 print_generic_expr (dump_file
, val
);
6459 fprintf (dump_file
, "\n");
6462 if (is_gimple_assign (stmt
))
6463 gimple_assign_set_rhs_from_tree (si
, val
);
6466 gcc_assert (gimple_code (stmt
) == GIMPLE_COND
);
6467 gcond
*cond_stmt
= as_a
<gcond
*> (stmt
);
6468 if (integer_zerop (val
))
6469 gimple_cond_make_false (cond_stmt
);
6470 else if (integer_onep (val
))
6471 gimple_cond_make_true (cond_stmt
);
6482 /* Callback for substitute_and_fold folding the stmt at *SI. */
6485 vrp_folder::fold_stmt (gimple_stmt_iterator
*si
)
6487 if (fold_predicate_in (si
))
6490 return simplify_stmt_using_ranges (si
);
6493 /* If OP has a value range with a single constant value return that,
6494 otherwise return NULL_TREE. This returns OP itself if OP is a
6497 Implemented as a pure wrapper right now, but this will change. */
6500 vrp_folder::get_value (tree op
)
6502 return op_with_constant_singleton_value_range (op
);
6505 /* Return the LHS of any ASSERT_EXPR where OP appears as the first
6506 argument to the ASSERT_EXPR and in which the ASSERT_EXPR dominates
6507 BB. If no such ASSERT_EXPR is found, return OP. */
6510 lhs_of_dominating_assert (tree op
, basic_block bb
, gimple
*stmt
)
6512 imm_use_iterator imm_iter
;
6514 use_operand_p use_p
;
6516 if (TREE_CODE (op
) == SSA_NAME
)
6518 FOR_EACH_IMM_USE_FAST (use_p
, imm_iter
, op
)
6520 use_stmt
= USE_STMT (use_p
);
6521 if (use_stmt
!= stmt
6522 && gimple_assign_single_p (use_stmt
)
6523 && TREE_CODE (gimple_assign_rhs1 (use_stmt
)) == ASSERT_EXPR
6524 && TREE_OPERAND (gimple_assign_rhs1 (use_stmt
), 0) == op
6525 && dominated_by_p (CDI_DOMINATORS
, bb
, gimple_bb (use_stmt
)))
6526 return gimple_assign_lhs (use_stmt
);
6533 static class vr_values
*x_vr_values
;
6535 /* A trivial wrapper so that we can present the generic jump threading
6536 code with a simple API for simplifying statements. STMT is the
6537 statement we want to simplify, WITHIN_STMT provides the location
6538 for any overflow warnings. */
6541 simplify_stmt_for_jump_threading (gimple
*stmt
, gimple
*within_stmt
,
6542 class avail_exprs_stack
*avail_exprs_stack ATTRIBUTE_UNUSED
,
6545 /* First see if the conditional is in the hash table. */
6546 tree cached_lhs
= avail_exprs_stack
->lookup_avail_expr (stmt
, false, true);
6547 if (cached_lhs
&& is_gimple_min_invariant (cached_lhs
))
6550 vr_values
*vr_values
= x_vr_values
;
6551 if (gcond
*cond_stmt
= dyn_cast
<gcond
*> (stmt
))
6553 tree op0
= gimple_cond_lhs (cond_stmt
);
6554 op0
= lhs_of_dominating_assert (op0
, bb
, stmt
);
6556 tree op1
= gimple_cond_rhs (cond_stmt
);
6557 op1
= lhs_of_dominating_assert (op1
, bb
, stmt
);
6559 return vr_values
->vrp_evaluate_conditional (gimple_cond_code (cond_stmt
),
6560 op0
, op1
, within_stmt
);
6563 /* We simplify a switch statement by trying to determine which case label
6564 will be taken. If we are successful then we return the corresponding
6566 if (gswitch
*switch_stmt
= dyn_cast
<gswitch
*> (stmt
))
6568 tree op
= gimple_switch_index (switch_stmt
);
6569 if (TREE_CODE (op
) != SSA_NAME
)
6572 op
= lhs_of_dominating_assert (op
, bb
, stmt
);
6574 value_range
*vr
= vr_values
->get_value_range (op
);
6575 if ((vr
->type
!= VR_RANGE
&& vr
->type
!= VR_ANTI_RANGE
)
6576 || symbolic_range_p (vr
))
6579 if (vr
->type
== VR_RANGE
)
6582 /* Get the range of labels that contain a part of the operand's
6584 find_case_label_range (switch_stmt
, vr
->min
, vr
->max
, &i
, &j
);
6586 /* Is there only one such label? */
6589 tree label
= gimple_switch_label (switch_stmt
, i
);
6591 /* The i'th label will be taken only if the value range of the
6592 operand is entirely within the bounds of this label. */
6593 if (CASE_HIGH (label
) != NULL_TREE
6594 ? (tree_int_cst_compare (CASE_LOW (label
), vr
->min
) <= 0
6595 && tree_int_cst_compare (CASE_HIGH (label
), vr
->max
) >= 0)
6596 : (tree_int_cst_equal (CASE_LOW (label
), vr
->min
)
6597 && tree_int_cst_equal (vr
->min
, vr
->max
)))
6601 /* If there are no such labels then the default label will be
6604 return gimple_switch_label (switch_stmt
, 0);
6607 if (vr
->type
== VR_ANTI_RANGE
)
6609 unsigned n
= gimple_switch_num_labels (switch_stmt
);
6610 tree min_label
= gimple_switch_label (switch_stmt
, 1);
6611 tree max_label
= gimple_switch_label (switch_stmt
, n
- 1);
6613 /* The default label will be taken only if the anti-range of the
6614 operand is entirely outside the bounds of all the (non-default)
6616 if (tree_int_cst_compare (vr
->min
, CASE_LOW (min_label
)) <= 0
6617 && (CASE_HIGH (max_label
) != NULL_TREE
6618 ? tree_int_cst_compare (vr
->max
, CASE_HIGH (max_label
)) >= 0
6619 : tree_int_cst_compare (vr
->max
, CASE_LOW (max_label
)) >= 0))
6620 return gimple_switch_label (switch_stmt
, 0);
6626 if (gassign
*assign_stmt
= dyn_cast
<gassign
*> (stmt
))
6628 tree lhs
= gimple_assign_lhs (assign_stmt
);
6629 if (TREE_CODE (lhs
) == SSA_NAME
6630 && (INTEGRAL_TYPE_P (TREE_TYPE (lhs
))
6631 || POINTER_TYPE_P (TREE_TYPE (lhs
)))
6632 && stmt_interesting_for_vrp (stmt
))
6636 value_range new_vr
= VR_INITIALIZER
;
6637 vr_values
->extract_range_from_stmt (stmt
, &dummy_e
,
6638 &dummy_tree
, &new_vr
);
6639 if (range_int_cst_singleton_p (&new_vr
))
6647 class vrp_dom_walker
: public dom_walker
6650 vrp_dom_walker (cdi_direction direction
,
6651 class const_and_copies
*const_and_copies
,
6652 class avail_exprs_stack
*avail_exprs_stack
)
6653 : dom_walker (direction
, REACHABLE_BLOCKS
),
6654 m_const_and_copies (const_and_copies
),
6655 m_avail_exprs_stack (avail_exprs_stack
),
6656 m_dummy_cond (NULL
) {}
6658 virtual edge
before_dom_children (basic_block
);
6659 virtual void after_dom_children (basic_block
);
6661 class vr_values
*vr_values
;
6664 class const_and_copies
*m_const_and_copies
;
6665 class avail_exprs_stack
*m_avail_exprs_stack
;
6667 gcond
*m_dummy_cond
;
6671 /* Called before processing dominator children of BB. We want to look
6672 at ASSERT_EXPRs and record information from them in the appropriate
6675 We could look at other statements here. It's not seen as likely
6676 to significantly increase the jump threads we discover. */
6679 vrp_dom_walker::before_dom_children (basic_block bb
)
6681 gimple_stmt_iterator gsi
;
6683 m_avail_exprs_stack
->push_marker ();
6684 m_const_and_copies
->push_marker ();
6685 for (gsi
= gsi_start_nondebug_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
6687 gimple
*stmt
= gsi_stmt (gsi
);
6688 if (gimple_assign_single_p (stmt
)
6689 && TREE_CODE (gimple_assign_rhs1 (stmt
)) == ASSERT_EXPR
)
6691 tree rhs1
= gimple_assign_rhs1 (stmt
);
6692 tree cond
= TREE_OPERAND (rhs1
, 1);
6693 tree inverted
= invert_truthvalue (cond
);
6694 vec
<cond_equivalence
> p
;
6696 record_conditions (&p
, cond
, inverted
);
6697 for (unsigned int i
= 0; i
< p
.length (); i
++)
6698 m_avail_exprs_stack
->record_cond (&p
[i
]);
6700 tree lhs
= gimple_assign_lhs (stmt
);
6701 m_const_and_copies
->record_const_or_copy (lhs
,
6702 TREE_OPERAND (rhs1
, 0));
6711 /* Called after processing dominator children of BB. This is where we
6712 actually call into the threader. */
6714 vrp_dom_walker::after_dom_children (basic_block bb
)
6717 m_dummy_cond
= gimple_build_cond (NE_EXPR
,
6718 integer_zero_node
, integer_zero_node
,
6721 x_vr_values
= vr_values
;
6722 thread_outgoing_edges (bb
, m_dummy_cond
, m_const_and_copies
,
6723 m_avail_exprs_stack
, NULL
,
6724 simplify_stmt_for_jump_threading
);
6727 m_avail_exprs_stack
->pop_to_marker ();
6728 m_const_and_copies
->pop_to_marker ();
6731 /* Blocks which have more than one predecessor and more than
6732 one successor present jump threading opportunities, i.e.,
6733 when the block is reached from a specific predecessor, we
6734 may be able to determine which of the outgoing edges will
6735 be traversed. When this optimization applies, we are able
6736 to avoid conditionals at runtime and we may expose secondary
6737 optimization opportunities.
6739 This routine is effectively a driver for the generic jump
6740 threading code. It basically just presents the generic code
6741 with edges that may be suitable for jump threading.
6743 Unlike DOM, we do not iterate VRP if jump threading was successful.
6744 While iterating may expose new opportunities for VRP, it is expected
6745 those opportunities would be very limited and the compile time cost
6746 to expose those opportunities would be significant.
6748 As jump threading opportunities are discovered, they are registered
6749 for later realization. */
6752 identify_jump_threads (class vr_values
*vr_values
)
6757 /* Ugh. When substituting values earlier in this pass we can
6758 wipe the dominance information. So rebuild the dominator
6759 information as we need it within the jump threading code. */
6760 calculate_dominance_info (CDI_DOMINATORS
);
6762 /* We do not allow VRP information to be used for jump threading
6763 across a back edge in the CFG. Otherwise it becomes too
6764 difficult to avoid eliminating loop exit tests. Of course
6765 EDGE_DFS_BACK is not accurate at this time so we have to
6767 mark_dfs_back_edges ();
6769 /* Do not thread across edges we are about to remove. Just marking
6770 them as EDGE_IGNORE will do. */
6771 FOR_EACH_VEC_ELT (to_remove_edges
, i
, e
)
6772 e
->flags
|= EDGE_IGNORE
;
6774 /* Allocate our unwinder stack to unwind any temporary equivalences
6775 that might be recorded. */
6776 const_and_copies
*equiv_stack
= new const_and_copies ();
6778 hash_table
<expr_elt_hasher
> *avail_exprs
6779 = new hash_table
<expr_elt_hasher
> (1024);
6780 avail_exprs_stack
*avail_exprs_stack
6781 = new class avail_exprs_stack (avail_exprs
);
6783 vrp_dom_walker
walker (CDI_DOMINATORS
, equiv_stack
, avail_exprs_stack
);
6784 walker
.vr_values
= vr_values
;
6785 walker
.walk (cfun
->cfg
->x_entry_block_ptr
);
6787 /* Clear EDGE_IGNORE. */
6788 FOR_EACH_VEC_ELT (to_remove_edges
, i
, e
)
6789 e
->flags
&= ~EDGE_IGNORE
;
6791 /* We do not actually update the CFG or SSA graphs at this point as
6792 ASSERT_EXPRs are still in the IL and cfg cleanup code does not yet
6793 handle ASSERT_EXPRs gracefully. */
6796 delete avail_exprs_stack
;
6799 /* Traverse all the blocks folding conditionals with known ranges. */
6802 vrp_prop::vrp_finalize (bool warn_array_bounds_p
)
6806 /* We have completed propagating through the lattice. */
6807 vr_values
.set_lattice_propagation_complete ();
6811 fprintf (dump_file
, "\nValue ranges after VRP:\n\n");
6812 vr_values
.dump_all_value_ranges (dump_file
);
6813 fprintf (dump_file
, "\n");
6816 /* Set value range to non pointer SSA_NAMEs. */
6817 for (i
= 0; i
< num_ssa_names
; i
++)
6819 tree name
= ssa_name (i
);
6823 value_range
*vr
= get_value_range (name
);
6825 || (vr
->type
== VR_VARYING
)
6826 || (vr
->type
== VR_UNDEFINED
)
6827 || (TREE_CODE (vr
->min
) != INTEGER_CST
)
6828 || (TREE_CODE (vr
->max
) != INTEGER_CST
))
6831 if (POINTER_TYPE_P (TREE_TYPE (name
))
6832 && ((vr
->type
== VR_RANGE
6833 && range_includes_zero_p (vr
->min
, vr
->max
) == 0)
6834 || (vr
->type
== VR_ANTI_RANGE
6835 && range_includes_zero_p (vr
->min
, vr
->max
) == 1)))
6836 set_ptr_nonnull (name
);
6837 else if (!POINTER_TYPE_P (TREE_TYPE (name
)))
6838 set_range_info (name
, vr
->type
,
6839 wi::to_wide (vr
->min
),
6840 wi::to_wide (vr
->max
));
6843 /* If we're checking array refs, we want to merge information on
6844 the executability of each edge between vrp_folder and the
6845 check_array_bounds_dom_walker: each can clear the
6846 EDGE_EXECUTABLE flag on edges, in different ways.
6848 Hence, if we're going to call check_all_array_refs, set
6849 the flag on every edge now, rather than in
6850 check_array_bounds_dom_walker's ctor; vrp_folder may clear
6851 it from some edges. */
6852 if (warn_array_bounds
&& warn_array_bounds_p
)
6853 set_all_edges_as_executable (cfun
);
6855 class vrp_folder vrp_folder
;
6856 vrp_folder
.vr_values
= &vr_values
;
6857 vrp_folder
.substitute_and_fold ();
6859 if (warn_array_bounds
&& warn_array_bounds_p
)
6860 check_all_array_refs ();
6863 /* Main entry point to VRP (Value Range Propagation). This pass is
6864 loosely based on J. R. C. Patterson, ``Accurate Static Branch
6865 Prediction by Value Range Propagation,'' in SIGPLAN Conference on
6866 Programming Language Design and Implementation, pp. 67-78, 1995.
6867 Also available at http://citeseer.ist.psu.edu/patterson95accurate.html
6869 This is essentially an SSA-CCP pass modified to deal with ranges
6870 instead of constants.
6872 While propagating ranges, we may find that two or more SSA name
6873 have equivalent, though distinct ranges. For instance,
6876 2 p_4 = ASSERT_EXPR <p_3, p_3 != 0>
6878 4 p_5 = ASSERT_EXPR <p_4, p_4 == q_2>;
6882 In the code above, pointer p_5 has range [q_2, q_2], but from the
6883 code we can also determine that p_5 cannot be NULL and, if q_2 had
6884 a non-varying range, p_5's range should also be compatible with it.
6886 These equivalences are created by two expressions: ASSERT_EXPR and
6887 copy operations. Since p_5 is an assertion on p_4, and p_4 was the
6888 result of another assertion, then we can use the fact that p_5 and
6889 p_4 are equivalent when evaluating p_5's range.
6891 Together with value ranges, we also propagate these equivalences
6892 between names so that we can take advantage of information from
6893 multiple ranges when doing final replacement. Note that this
6894 equivalency relation is transitive but not symmetric.
6896 In the example above, p_5 is equivalent to p_4, q_2 and p_3, but we
6897 cannot assert that q_2 is equivalent to p_5 because q_2 may be used
6898 in contexts where that assertion does not hold (e.g., in line 6).
6900 TODO, the main difference between this pass and Patterson's is that
6901 we do not propagate edge probabilities. We only compute whether
6902 edges can be taken or not. That is, instead of having a spectrum
6903 of jump probabilities between 0 and 1, we only deal with 0, 1 and
6904 DON'T KNOW. In the future, it may be worthwhile to propagate
6905 probabilities to aid branch prediction. */
6908 execute_vrp (bool warn_array_bounds_p
)
6914 loop_optimizer_init (LOOPS_NORMAL
| LOOPS_HAVE_RECORDED_EXITS
);
6915 rewrite_into_loop_closed_ssa (NULL
, TODO_update_ssa
);
6918 /* ??? This ends up using stale EDGE_DFS_BACK for liveness computation.
6919 Inserting assertions may split edges which will invalidate
6921 insert_range_assertions ();
6923 to_remove_edges
.create (10);
6924 to_update_switch_stmts
.create (5);
6925 threadedge_initialize_values ();
6927 /* For visiting PHI nodes we need EDGE_DFS_BACK computed. */
6928 mark_dfs_back_edges ();
6930 class vrp_prop vrp_prop
;
6931 vrp_prop
.vrp_initialize ();
6932 vrp_prop
.ssa_propagate ();
6933 vrp_prop
.vrp_finalize (warn_array_bounds_p
);
6935 /* We must identify jump threading opportunities before we release
6936 the datastructures built by VRP. */
6937 identify_jump_threads (&vrp_prop
.vr_values
);
6939 /* A comparison of an SSA_NAME against a constant where the SSA_NAME
6940 was set by a type conversion can often be rewritten to use the
6941 RHS of the type conversion.
6943 However, doing so inhibits jump threading through the comparison.
6944 So that transformation is not performed until after jump threading
6947 FOR_EACH_BB_FN (bb
, cfun
)
6949 gimple
*last
= last_stmt (bb
);
6950 if (last
&& gimple_code (last
) == GIMPLE_COND
)
6951 vrp_prop
.vr_values
.simplify_cond_using_ranges_2 (as_a
<gcond
*> (last
));
6954 free_numbers_of_iterations_estimates (cfun
);
6956 /* ASSERT_EXPRs must be removed before finalizing jump threads
6957 as finalizing jump threads calls the CFG cleanup code which
6958 does not properly handle ASSERT_EXPRs. */
6959 remove_range_assertions ();
6961 /* If we exposed any new variables, go ahead and put them into
6962 SSA form now, before we handle jump threading. This simplifies
6963 interactions between rewriting of _DECL nodes into SSA form
6964 and rewriting SSA_NAME nodes into SSA form after block
6965 duplication and CFG manipulation. */
6966 update_ssa (TODO_update_ssa
);
6968 /* We identified all the jump threading opportunities earlier, but could
6969 not transform the CFG at that time. This routine transforms the
6970 CFG and arranges for the dominator tree to be rebuilt if necessary.
6972 Note the SSA graph update will occur during the normal TODO
6973 processing by the pass manager. */
6974 thread_through_all_blocks (false);
6976 /* Remove dead edges from SWITCH_EXPR optimization. This leaves the
6977 CFG in a broken state and requires a cfg_cleanup run. */
6978 FOR_EACH_VEC_ELT (to_remove_edges
, i
, e
)
6980 /* Update SWITCH_EXPR case label vector. */
6981 FOR_EACH_VEC_ELT (to_update_switch_stmts
, i
, su
)
6984 size_t n
= TREE_VEC_LENGTH (su
->vec
);
6986 gimple_switch_set_num_labels (su
->stmt
, n
);
6987 for (j
= 0; j
< n
; j
++)
6988 gimple_switch_set_label (su
->stmt
, j
, TREE_VEC_ELT (su
->vec
, j
));
6989 /* As we may have replaced the default label with a regular one
6990 make sure to make it a real default label again. This ensures
6991 optimal expansion. */
6992 label
= gimple_switch_label (su
->stmt
, 0);
6993 CASE_LOW (label
) = NULL_TREE
;
6994 CASE_HIGH (label
) = NULL_TREE
;
6997 if (to_remove_edges
.length () > 0)
6999 free_dominance_info (CDI_DOMINATORS
);
7000 loops_state_set (LOOPS_NEED_FIXUP
);
7003 to_remove_edges
.release ();
7004 to_update_switch_stmts
.release ();
7005 threadedge_finalize_values ();
7008 loop_optimizer_finalize ();
7014 const pass_data pass_data_vrp
=
7016 GIMPLE_PASS
, /* type */
7018 OPTGROUP_NONE
, /* optinfo_flags */
7019 TV_TREE_VRP
, /* tv_id */
7020 PROP_ssa
, /* properties_required */
7021 0, /* properties_provided */
7022 0, /* properties_destroyed */
7023 0, /* todo_flags_start */
7024 ( TODO_cleanup_cfg
| TODO_update_ssa
), /* todo_flags_finish */
7027 class pass_vrp
: public gimple_opt_pass
7030 pass_vrp (gcc::context
*ctxt
)
7031 : gimple_opt_pass (pass_data_vrp
, ctxt
), warn_array_bounds_p (false)
7034 /* opt_pass methods: */
7035 opt_pass
* clone () { return new pass_vrp (m_ctxt
); }
7036 void set_pass_param (unsigned int n
, bool param
)
7038 gcc_assert (n
== 0);
7039 warn_array_bounds_p
= param
;
7041 virtual bool gate (function
*) { return flag_tree_vrp
!= 0; }
7042 virtual unsigned int execute (function
*)
7043 { return execute_vrp (warn_array_bounds_p
); }
7046 bool warn_array_bounds_p
;
7047 }; // class pass_vrp
7052 make_pass_vrp (gcc::context
*ctxt
)
7054 return new pass_vrp (ctxt
);