1 /* Support routines for Value Range Propagation (VRP).
2 Copyright (C) 2005-2016 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"
45 #include "tree-ssa-loop-manip.h"
46 #include "tree-ssa-loop-niter.h"
47 #include "tree-ssa-loop.h"
48 #include "tree-into-ssa.h"
52 #include "tree-scalar-evolution.h"
53 #include "tree-ssa-propagate.h"
54 #include "tree-chrec.h"
55 #include "tree-ssa-threadupdate.h"
56 #include "tree-ssa-scopedtables.h"
57 #include "tree-ssa-threadedge.h"
60 #include "case-cfn-macros.h"
62 /* Range of values that can be associated with an SSA_NAME after VRP
66 /* Lattice value represented by this range. */
67 enum value_range_type type;
69 /* Minimum and maximum values represented by this range. These
70 values should be interpreted as follows:
72 - If TYPE is VR_UNDEFINED or VR_VARYING then MIN and MAX must
75 - If TYPE == VR_RANGE then MIN holds the minimum value and
76 MAX holds the maximum value of the range [MIN, MAX].
78 - If TYPE == ANTI_RANGE the variable is known to NOT
79 take any values in the range [MIN, MAX]. */
83 /* Set of SSA names whose value ranges are equivalent to this one.
84 This set is only valid when TYPE is VR_RANGE or VR_ANTI_RANGE. */
88 #define VR_INITIALIZER { VR_UNDEFINED, NULL_TREE, NULL_TREE, NULL }
90 /* Set of SSA names found live during the RPO traversal of the function
91 for still active basic-blocks. */
94 /* Return true if the SSA name NAME is live on the edge E. */
97 live_on_edge (edge e, tree name)
99 return (live[e->dest->index]
100 && bitmap_bit_p (live[e->dest->index], SSA_NAME_VERSION (name)));
103 /* Local functions. */
104 static int compare_values (tree val1, tree val2);
105 static int compare_values_warnv (tree val1, tree val2, bool *);
106 static void vrp_meet (value_range *, value_range *);
107 static void vrp_intersect_ranges (value_range *, value_range *);
108 static tree vrp_evaluate_conditional_warnv_with_ops (enum tree_code,
109 tree, tree, bool, bool *,
112 /* Location information for ASSERT_EXPRs. Each instance of this
113 structure describes an ASSERT_EXPR for an SSA name. Since a single
114 SSA name may have more than one assertion associated with it, these
115 locations are kept in a linked list attached to the corresponding
119 /* Basic block where the assertion would be inserted. */
122 /* Some assertions need to be inserted on an edge (e.g., assertions
123 generated by COND_EXPRs). In those cases, BB will be NULL. */
126 /* Pointer to the statement that generated this assertion. */
127 gimple_stmt_iterator si;
129 /* Predicate code for the ASSERT_EXPR. Must be COMPARISON_CLASS_P. */
130 enum tree_code comp_code;
132 /* Value being compared against. */
135 /* Expression to compare. */
138 /* Next node in the linked list. */
142 /* If bit I is present, it means that SSA name N_i has a list of
143 assertions that should be inserted in the IL. */
144 static bitmap need_assert_for;
146 /* Array of locations lists where to insert assertions. ASSERTS_FOR[I]
147 holds a list of ASSERT_LOCUS_T nodes that describe where
148 ASSERT_EXPRs for SSA name N_I should be inserted. */
149 static assert_locus **asserts_for;
151 /* Value range array. After propagation, VR_VALUE[I] holds the range
152 of values that SSA name N_I may take. */
153 static unsigned num_vr_values;
154 static value_range **vr_value;
155 static bool values_propagated;
157 /* For a PHI node which sets SSA name N_I, VR_COUNTS[I] holds the
158 number of executable edges we saw the last time we visited the
160 static int *vr_phi_edge_counts;
162 struct switch_update {
167 static vec<edge> to_remove_edges;
168 static vec<switch_update> to_update_switch_stmts;
171 /* Return the maximum value for TYPE. */
174 vrp_val_max (const_tree type)
176 if (!INTEGRAL_TYPE_P (type))
179 return TYPE_MAX_VALUE (type);
182 /* Return the minimum value for TYPE. */
185 vrp_val_min (const_tree type)
187 if (!INTEGRAL_TYPE_P (type))
190 return TYPE_MIN_VALUE (type);
193 /* Return whether VAL is equal to the maximum value of its type. This
194 will be true for a positive overflow infinity. We can't do a
195 simple equality comparison with TYPE_MAX_VALUE because C typedefs
196 and Ada subtypes can produce types whose TYPE_MAX_VALUE is not ==
197 to the integer constant with the same value in the type. */
200 vrp_val_is_max (const_tree val)
202 tree type_max = vrp_val_max (TREE_TYPE (val));
203 return (val == type_max
204 || (type_max != NULL_TREE
205 && operand_equal_p (val, type_max, 0)));
208 /* Return whether VAL is equal to the minimum value of its type. This
209 will be true for a negative overflow infinity. */
212 vrp_val_is_min (const_tree val)
214 tree type_min = vrp_val_min (TREE_TYPE (val));
215 return (val == type_min
216 || (type_min != NULL_TREE
217 && operand_equal_p (val, type_min, 0)));
221 /* Return whether TYPE should use an overflow infinity distinct from
222 TYPE_{MIN,MAX}_VALUE. We use an overflow infinity value to
223 represent a signed overflow during VRP computations. An infinity
224 is distinct from a half-range, which will go from some number to
225 TYPE_{MIN,MAX}_VALUE. */
228 needs_overflow_infinity (const_tree type)
230 return INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_WRAPS (type);
233 /* Return whether TYPE can support our overflow infinity
234 representation: we use the TREE_OVERFLOW flag, which only exists
235 for constants. If TYPE doesn't support this, we don't optimize
236 cases which would require signed overflow--we drop them to
240 supports_overflow_infinity (const_tree type)
242 tree min = vrp_val_min (type), max = vrp_val_max (type);
243 gcc_checking_assert (needs_overflow_infinity (type));
244 return (min != NULL_TREE
245 && CONSTANT_CLASS_P (min)
247 && CONSTANT_CLASS_P (max));
250 /* VAL is the maximum or minimum value of a type. Return a
251 corresponding overflow infinity. */
254 make_overflow_infinity (tree val)
256 gcc_checking_assert (val != NULL_TREE && CONSTANT_CLASS_P (val));
257 val = copy_node (val);
258 TREE_OVERFLOW (val) = 1;
262 /* Return a negative overflow infinity for TYPE. */
265 negative_overflow_infinity (tree type)
267 gcc_checking_assert (supports_overflow_infinity (type));
268 return make_overflow_infinity (vrp_val_min (type));
271 /* Return a positive overflow infinity for TYPE. */
274 positive_overflow_infinity (tree type)
276 gcc_checking_assert (supports_overflow_infinity (type));
277 return make_overflow_infinity (vrp_val_max (type));
280 /* Return whether VAL is a negative overflow infinity. */
283 is_negative_overflow_infinity (const_tree val)
285 return (TREE_OVERFLOW_P (val)
286 && needs_overflow_infinity (TREE_TYPE (val))
287 && vrp_val_is_min (val));
290 /* Return whether VAL is a positive overflow infinity. */
293 is_positive_overflow_infinity (const_tree val)
295 return (TREE_OVERFLOW_P (val)
296 && needs_overflow_infinity (TREE_TYPE (val))
297 && vrp_val_is_max (val));
300 /* Return whether VAL is a positive or negative overflow infinity. */
303 is_overflow_infinity (const_tree val)
305 return (TREE_OVERFLOW_P (val)
306 && needs_overflow_infinity (TREE_TYPE (val))
307 && (vrp_val_is_min (val) || vrp_val_is_max (val)));
310 /* Return whether STMT has a constant rhs that is_overflow_infinity. */
313 stmt_overflow_infinity (gimple *stmt)
315 if (is_gimple_assign (stmt)
316 && get_gimple_rhs_class (gimple_assign_rhs_code (stmt)) ==
318 return is_overflow_infinity (gimple_assign_rhs1 (stmt));
322 /* If VAL is now an overflow infinity, return VAL. Otherwise, return
323 the same value with TREE_OVERFLOW clear. This can be used to avoid
324 confusing a regular value with an overflow value. */
327 avoid_overflow_infinity (tree val)
329 if (!is_overflow_infinity (val))
332 if (vrp_val_is_max (val))
333 return vrp_val_max (TREE_TYPE (val));
336 gcc_checking_assert (vrp_val_is_min (val));
337 return vrp_val_min (TREE_TYPE (val));
342 /* Set value range VR to VR_UNDEFINED. */
345 set_value_range_to_undefined (value_range *vr)
347 vr->type = VR_UNDEFINED;
348 vr->min = vr->max = NULL_TREE;
350 bitmap_clear (vr->equiv);
354 /* Set value range VR to VR_VARYING. */
357 set_value_range_to_varying (value_range *vr)
359 vr->type = VR_VARYING;
360 vr->min = vr->max = NULL_TREE;
362 bitmap_clear (vr->equiv);
366 /* Set value range VR to {T, MIN, MAX, EQUIV}. */
369 set_value_range (value_range *vr, enum value_range_type t, tree min,
370 tree max, bitmap equiv)
372 /* Check the validity of the range. */
374 && (t == VR_RANGE || t == VR_ANTI_RANGE))
378 gcc_assert (min && max);
380 gcc_assert ((!TREE_OVERFLOW_P (min) || is_overflow_infinity (min))
381 && (!TREE_OVERFLOW_P (max) || is_overflow_infinity (max)));
383 if (INTEGRAL_TYPE_P (TREE_TYPE (min)) && t == VR_ANTI_RANGE)
384 gcc_assert (!vrp_val_is_min (min) || !vrp_val_is_max (max));
386 cmp = compare_values (min, max);
387 gcc_assert (cmp == 0 || cmp == -1 || cmp == -2);
389 if (needs_overflow_infinity (TREE_TYPE (min)))
390 gcc_assert (!is_overflow_infinity (min)
391 || !is_overflow_infinity (max));
395 && (t == VR_UNDEFINED || t == VR_VARYING))
397 gcc_assert (min == NULL_TREE && max == NULL_TREE);
398 gcc_assert (equiv == NULL || bitmap_empty_p (equiv));
405 /* Since updating the equivalence set involves deep copying the
406 bitmaps, only do it if absolutely necessary. */
407 if (vr->equiv == NULL
409 vr->equiv = BITMAP_ALLOC (NULL);
411 if (equiv != vr->equiv)
413 if (equiv && !bitmap_empty_p (equiv))
414 bitmap_copy (vr->equiv, equiv);
416 bitmap_clear (vr->equiv);
421 /* Set value range VR to the canonical form of {T, MIN, MAX, EQUIV}.
422 This means adjusting T, MIN and MAX representing the case of a
423 wrapping range with MAX < MIN covering [MIN, type_max] U [type_min, MAX]
424 as anti-rage ~[MAX+1, MIN-1]. Likewise for wrapping anti-ranges.
425 In corner cases where MAX+1 or MIN-1 wraps this will fall back
427 This routine exists to ease canonicalization in the case where we
428 extract ranges from var + CST op limit. */
431 set_and_canonicalize_value_range (value_range *vr, enum value_range_type t,
432 tree min, tree max, bitmap equiv)
434 /* Use the canonical setters for VR_UNDEFINED and VR_VARYING. */
435 if (t == VR_UNDEFINED)
437 set_value_range_to_undefined (vr);
440 else if (t == VR_VARYING)
442 set_value_range_to_varying (vr);
446 /* Nothing to canonicalize for symbolic ranges. */
447 if (TREE_CODE (min) != INTEGER_CST
448 || TREE_CODE (max) != INTEGER_CST)
450 set_value_range (vr, t, min, max, equiv);
454 /* Wrong order for min and max, to swap them and the VR type we need
456 if (tree_int_cst_lt (max, min))
460 /* For one bit precision if max < min, then the swapped
461 range covers all values, so for VR_RANGE it is varying and
462 for VR_ANTI_RANGE empty range, so drop to varying as well. */
463 if (TYPE_PRECISION (TREE_TYPE (min)) == 1)
465 set_value_range_to_varying (vr);
469 one = build_int_cst (TREE_TYPE (min), 1);
470 tmp = int_const_binop (PLUS_EXPR, max, one);
471 max = int_const_binop (MINUS_EXPR, min, one);
474 /* There's one corner case, if we had [C+1, C] before we now have
475 that again. But this represents an empty value range, so drop
476 to varying in this case. */
477 if (tree_int_cst_lt (max, min))
479 set_value_range_to_varying (vr);
483 t = t == VR_RANGE ? VR_ANTI_RANGE : VR_RANGE;
486 /* Anti-ranges that can be represented as ranges should be so. */
487 if (t == VR_ANTI_RANGE)
489 bool is_min = vrp_val_is_min (min);
490 bool is_max = vrp_val_is_max (max);
492 if (is_min && is_max)
494 /* We cannot deal with empty ranges, drop to varying.
495 ??? This could be VR_UNDEFINED instead. */
496 set_value_range_to_varying (vr);
499 else if (TYPE_PRECISION (TREE_TYPE (min)) == 1
500 && (is_min || is_max))
502 /* Non-empty boolean ranges can always be represented
503 as a singleton range. */
505 min = max = vrp_val_max (TREE_TYPE (min));
507 min = max = vrp_val_min (TREE_TYPE (min));
511 /* As a special exception preserve non-null ranges. */
512 && !(TYPE_UNSIGNED (TREE_TYPE (min))
513 && integer_zerop (max)))
515 tree one = build_int_cst (TREE_TYPE (max), 1);
516 min = int_const_binop (PLUS_EXPR, max, one);
517 max = vrp_val_max (TREE_TYPE (max));
522 tree one = build_int_cst (TREE_TYPE (min), 1);
523 max = int_const_binop (MINUS_EXPR, min, one);
524 min = vrp_val_min (TREE_TYPE (min));
529 /* Drop [-INF(OVF), +INF(OVF)] to varying. */
530 if (needs_overflow_infinity (TREE_TYPE (min))
531 && is_overflow_infinity (min)
532 && is_overflow_infinity (max))
534 set_value_range_to_varying (vr);
538 set_value_range (vr, t, min, max, equiv);
541 /* Copy value range FROM into value range TO. */
544 copy_value_range (value_range *to, value_range *from)
546 set_value_range (to, from->type, from->min, from->max, from->equiv);
549 /* Set value range VR to a single value. This function is only called
550 with values we get from statements, and exists to clear the
551 TREE_OVERFLOW flag so that we don't think we have an overflow
552 infinity when we shouldn't. */
555 set_value_range_to_value (value_range *vr, tree val, bitmap equiv)
557 gcc_assert (is_gimple_min_invariant (val));
558 if (TREE_OVERFLOW_P (val))
559 val = drop_tree_overflow (val);
560 set_value_range (vr, VR_RANGE, val, val, equiv);
563 /* Set value range VR to a non-negative range of type TYPE.
564 OVERFLOW_INFINITY indicates whether to use an overflow infinity
565 rather than TYPE_MAX_VALUE; this should be true if we determine
566 that the range is nonnegative based on the assumption that signed
567 overflow does not occur. */
570 set_value_range_to_nonnegative (value_range *vr, tree type,
571 bool overflow_infinity)
575 if (overflow_infinity && !supports_overflow_infinity (type))
577 set_value_range_to_varying (vr);
581 zero = build_int_cst (type, 0);
582 set_value_range (vr, VR_RANGE, zero,
584 ? positive_overflow_infinity (type)
585 : TYPE_MAX_VALUE (type)),
589 /* Set value range VR to a non-NULL range of type TYPE. */
592 set_value_range_to_nonnull (value_range *vr, tree type)
594 tree zero = build_int_cst (type, 0);
595 set_value_range (vr, VR_ANTI_RANGE, zero, zero, vr->equiv);
599 /* Set value range VR to a NULL range of type TYPE. */
602 set_value_range_to_null (value_range *vr, tree type)
604 set_value_range_to_value (vr, build_int_cst (type, 0), vr->equiv);
608 /* Set value range VR to a range of a truthvalue of type TYPE. */
611 set_value_range_to_truthvalue (value_range *vr, tree type)
613 if (TYPE_PRECISION (type) == 1)
614 set_value_range_to_varying (vr);
616 set_value_range (vr, VR_RANGE,
617 build_int_cst (type, 0), build_int_cst (type, 1),
622 /* If abs (min) < abs (max), set VR to [-max, max], if
623 abs (min) >= abs (max), set VR to [-min, min]. */
626 abs_extent_range (value_range *vr, tree min, tree max)
630 gcc_assert (TREE_CODE (min) == INTEGER_CST);
631 gcc_assert (TREE_CODE (max) == INTEGER_CST);
632 gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (min)));
633 gcc_assert (!TYPE_UNSIGNED (TREE_TYPE (min)));
634 min = fold_unary (ABS_EXPR, TREE_TYPE (min), min);
635 max = fold_unary (ABS_EXPR, TREE_TYPE (max), max);
636 if (TREE_OVERFLOW (min) || TREE_OVERFLOW (max))
638 set_value_range_to_varying (vr);
641 cmp = compare_values (min, max);
643 min = fold_unary (NEGATE_EXPR, TREE_TYPE (min), max);
644 else if (cmp == 0 || cmp == 1)
647 min = fold_unary (NEGATE_EXPR, TREE_TYPE (min), min);
651 set_value_range_to_varying (vr);
654 set_and_canonicalize_value_range (vr, VR_RANGE, min, max, NULL);
658 /* Return value range information for VAR.
660 If we have no values ranges recorded (ie, VRP is not running), then
661 return NULL. Otherwise create an empty range if none existed for VAR. */
664 get_value_range (const_tree var)
666 static const value_range vr_const_varying
667 = { VR_VARYING, NULL_TREE, NULL_TREE, NULL };
670 unsigned ver = SSA_NAME_VERSION (var);
672 /* If we have no recorded ranges, then return NULL. */
676 /* If we query the range for a new SSA name return an unmodifiable VARYING.
677 We should get here at most from the substitute-and-fold stage which
678 will never try to change values. */
679 if (ver >= num_vr_values)
680 return CONST_CAST (value_range *, &vr_const_varying);
686 /* After propagation finished do not allocate new value-ranges. */
687 if (values_propagated)
688 return CONST_CAST (value_range *, &vr_const_varying);
690 /* Create a default value range. */
691 vr_value[ver] = vr = XCNEW (value_range);
693 /* Defer allocating the equivalence set. */
696 /* If VAR is a default definition of a parameter, the variable can
697 take any value in VAR's type. */
698 if (SSA_NAME_IS_DEFAULT_DEF (var))
700 sym = SSA_NAME_VAR (var);
701 if (TREE_CODE (sym) == PARM_DECL)
703 /* Try to use the "nonnull" attribute to create ~[0, 0]
704 anti-ranges for pointers. Note that this is only valid with
705 default definitions of PARM_DECLs. */
706 if (POINTER_TYPE_P (TREE_TYPE (sym))
707 && nonnull_arg_p (sym))
708 set_value_range_to_nonnull (vr, TREE_TYPE (sym));
710 set_value_range_to_varying (vr);
712 else if (TREE_CODE (sym) == RESULT_DECL
713 && DECL_BY_REFERENCE (sym))
714 set_value_range_to_nonnull (vr, TREE_TYPE (sym));
720 /* Set value-ranges of all SSA names defined by STMT to varying. */
723 set_defs_to_varying (gimple *stmt)
727 FOR_EACH_SSA_TREE_OPERAND (def, stmt, i, SSA_OP_DEF)
729 value_range *vr = get_value_range (def);
730 /* Avoid writing to vr_const_varying get_value_range may return. */
731 if (vr->type != VR_VARYING)
732 set_value_range_to_varying (vr);
737 /* Return true, if VAL1 and VAL2 are equal values for VRP purposes. */
740 vrp_operand_equal_p (const_tree val1, const_tree val2)
744 if (!val1 || !val2 || !operand_equal_p (val1, val2, 0))
746 return is_overflow_infinity (val1) == is_overflow_infinity (val2);
749 /* Return true, if the bitmaps B1 and B2 are equal. */
752 vrp_bitmap_equal_p (const_bitmap b1, const_bitmap b2)
755 || ((!b1 || bitmap_empty_p (b1))
756 && (!b2 || bitmap_empty_p (b2)))
758 && bitmap_equal_p (b1, b2)));
761 /* Update the value range and equivalence set for variable VAR to
762 NEW_VR. Return true if NEW_VR is different from VAR's previous
765 NOTE: This function assumes that NEW_VR is a temporary value range
766 object created for the sole purpose of updating VAR's range. The
767 storage used by the equivalence set from NEW_VR will be freed by
768 this function. Do not call update_value_range when NEW_VR
769 is the range object associated with another SSA name. */
772 update_value_range (const_tree var, value_range *new_vr)
777 /* If there is a value-range on the SSA name from earlier analysis
779 if (INTEGRAL_TYPE_P (TREE_TYPE (var)))
782 value_range_type rtype = get_range_info (var, &min, &max);
783 if (rtype == VR_RANGE || rtype == VR_ANTI_RANGE)
787 /* Range info on SSA names doesn't carry overflow information
788 so make sure to preserve the overflow bit on the lattice. */
789 if (new_vr->type == VR_RANGE
790 && is_negative_overflow_infinity (new_vr->min)
791 && wi::eq_p (new_vr->min, min))
792 nr.min = new_vr->min;
794 nr.min = wide_int_to_tree (TREE_TYPE (var), min);
795 if (new_vr->type == VR_RANGE
796 && is_positive_overflow_infinity (new_vr->max)
797 && wi::eq_p (new_vr->max, max))
798 nr.max = new_vr->max;
800 nr.max = wide_int_to_tree (TREE_TYPE (var), max);
802 vrp_intersect_ranges (new_vr, &nr);
806 /* Update the value range, if necessary. */
807 old_vr = get_value_range (var);
808 is_new = old_vr->type != new_vr->type
809 || !vrp_operand_equal_p (old_vr->min, new_vr->min)
810 || !vrp_operand_equal_p (old_vr->max, new_vr->max)
811 || !vrp_bitmap_equal_p (old_vr->equiv, new_vr->equiv);
815 /* Do not allow transitions up the lattice. The following
816 is slightly more awkward than just new_vr->type < old_vr->type
817 because VR_RANGE and VR_ANTI_RANGE need to be considered
818 the same. We may not have is_new when transitioning to
819 UNDEFINED. If old_vr->type is VARYING, we shouldn't be
821 if (new_vr->type == VR_UNDEFINED)
823 BITMAP_FREE (new_vr->equiv);
824 set_value_range_to_varying (old_vr);
825 set_value_range_to_varying (new_vr);
829 set_value_range (old_vr, new_vr->type, new_vr->min, new_vr->max,
833 BITMAP_FREE (new_vr->equiv);
839 /* Add VAR and VAR's equivalence set to EQUIV. This is the central
840 point where equivalence processing can be turned on/off. */
843 add_equivalence (bitmap *equiv, const_tree var)
845 unsigned ver = SSA_NAME_VERSION (var);
846 value_range *vr = vr_value[ver];
849 *equiv = BITMAP_ALLOC (NULL);
850 bitmap_set_bit (*equiv, ver);
852 bitmap_ior_into (*equiv, vr->equiv);
856 /* Return true if VR is ~[0, 0]. */
859 range_is_nonnull (value_range *vr)
861 return vr->type == VR_ANTI_RANGE
862 && integer_zerop (vr->min)
863 && integer_zerop (vr->max);
867 /* Return true if VR is [0, 0]. */
870 range_is_null (value_range *vr)
872 return vr->type == VR_RANGE
873 && integer_zerop (vr->min)
874 && integer_zerop (vr->max);
877 /* Return true if max and min of VR are INTEGER_CST. It's not necessary
881 range_int_cst_p (value_range *vr)
883 return (vr->type == VR_RANGE
884 && TREE_CODE (vr->max) == INTEGER_CST
885 && TREE_CODE (vr->min) == INTEGER_CST);
888 /* Return true if VR is a INTEGER_CST singleton. */
891 range_int_cst_singleton_p (value_range *vr)
893 return (range_int_cst_p (vr)
894 && !is_overflow_infinity (vr->min)
895 && !is_overflow_infinity (vr->max)
896 && tree_int_cst_equal (vr->min, vr->max));
899 /* Return true if value range VR involves at least one symbol. */
902 symbolic_range_p (value_range *vr)
904 return (!is_gimple_min_invariant (vr->min)
905 || !is_gimple_min_invariant (vr->max));
908 /* Return the single symbol (an SSA_NAME) contained in T if any, or NULL_TREE
909 otherwise. We only handle additive operations and set NEG to true if the
910 symbol is negated and INV to the invariant part, if any. */
913 get_single_symbol (tree t, bool *neg, tree *inv)
918 if (TREE_CODE (t) == PLUS_EXPR
919 || TREE_CODE (t) == POINTER_PLUS_EXPR
920 || TREE_CODE (t) == MINUS_EXPR)
922 if (is_gimple_min_invariant (TREE_OPERAND (t, 0)))
924 neg_ = (TREE_CODE (t) == MINUS_EXPR);
925 inv_ = TREE_OPERAND (t, 0);
926 t = TREE_OPERAND (t, 1);
928 else if (is_gimple_min_invariant (TREE_OPERAND (t, 1)))
931 inv_ = TREE_OPERAND (t, 1);
932 t = TREE_OPERAND (t, 0);
943 if (TREE_CODE (t) == NEGATE_EXPR)
945 t = TREE_OPERAND (t, 0);
949 if (TREE_CODE (t) != SSA_NAME)
957 /* The reverse operation: build a symbolic expression with TYPE
958 from symbol SYM, negated according to NEG, and invariant INV. */
961 build_symbolic_expr (tree type, tree sym, bool neg, tree inv)
963 const bool pointer_p = POINTER_TYPE_P (type);
967 t = build1 (NEGATE_EXPR, type, t);
969 if (integer_zerop (inv))
972 return build2 (pointer_p ? POINTER_PLUS_EXPR : PLUS_EXPR, type, t, inv);
975 /* Return true if value range VR involves exactly one symbol SYM. */
978 symbolic_range_based_on_p (value_range *vr, const_tree sym)
980 bool neg, min_has_symbol, max_has_symbol;
983 if (is_gimple_min_invariant (vr->min))
984 min_has_symbol = false;
985 else if (get_single_symbol (vr->min, &neg, &inv) == sym)
986 min_has_symbol = true;
990 if (is_gimple_min_invariant (vr->max))
991 max_has_symbol = false;
992 else if (get_single_symbol (vr->max, &neg, &inv) == sym)
993 max_has_symbol = true;
997 return (min_has_symbol || max_has_symbol);
1000 /* Return true if value range VR uses an overflow infinity. */
1003 overflow_infinity_range_p (value_range *vr)
1005 return (vr->type == VR_RANGE
1006 && (is_overflow_infinity (vr->min)
1007 || is_overflow_infinity (vr->max)));
1010 /* Return false if we can not make a valid comparison based on VR;
1011 this will be the case if it uses an overflow infinity and overflow
1012 is not undefined (i.e., -fno-strict-overflow is in effect).
1013 Otherwise return true, and set *STRICT_OVERFLOW_P to true if VR
1014 uses an overflow infinity. */
1017 usable_range_p (value_range *vr, bool *strict_overflow_p)
1019 gcc_assert (vr->type == VR_RANGE);
1020 if (is_overflow_infinity (vr->min))
1022 *strict_overflow_p = true;
1023 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (vr->min)))
1026 if (is_overflow_infinity (vr->max))
1028 *strict_overflow_p = true;
1029 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (vr->max)))
1035 /* Return true if the result of assignment STMT is know to be non-zero.
1036 If the return value is based on the assumption that signed overflow is
1037 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1038 *STRICT_OVERFLOW_P.*/
1041 gimple_assign_nonzero_warnv_p (gimple *stmt, bool *strict_overflow_p)
1043 enum tree_code code = gimple_assign_rhs_code (stmt);
1044 switch (get_gimple_rhs_class (code))
1046 case GIMPLE_UNARY_RHS:
1047 return tree_unary_nonzero_warnv_p (gimple_assign_rhs_code (stmt),
1048 gimple_expr_type (stmt),
1049 gimple_assign_rhs1 (stmt),
1051 case GIMPLE_BINARY_RHS:
1052 return tree_binary_nonzero_warnv_p (gimple_assign_rhs_code (stmt),
1053 gimple_expr_type (stmt),
1054 gimple_assign_rhs1 (stmt),
1055 gimple_assign_rhs2 (stmt),
1057 case GIMPLE_TERNARY_RHS:
1059 case GIMPLE_SINGLE_RHS:
1060 return tree_single_nonzero_warnv_p (gimple_assign_rhs1 (stmt),
1062 case GIMPLE_INVALID_RHS:
1069 /* Return true if STMT is known to compute a non-zero value.
1070 If the return value is based on the assumption that signed overflow is
1071 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1072 *STRICT_OVERFLOW_P.*/
1075 gimple_stmt_nonzero_warnv_p (gimple *stmt, bool *strict_overflow_p)
1077 switch (gimple_code (stmt))
1080 return gimple_assign_nonzero_warnv_p (stmt, strict_overflow_p);
1083 tree fndecl = gimple_call_fndecl (stmt);
1084 if (!fndecl) return false;
1085 if (flag_delete_null_pointer_checks && !flag_check_new
1086 && DECL_IS_OPERATOR_NEW (fndecl)
1087 && !TREE_NOTHROW (fndecl))
1089 /* References are always non-NULL. */
1090 if (flag_delete_null_pointer_checks
1091 && TREE_CODE (TREE_TYPE (fndecl)) == REFERENCE_TYPE)
1093 if (flag_delete_null_pointer_checks &&
1094 lookup_attribute ("returns_nonnull",
1095 TYPE_ATTRIBUTES (gimple_call_fntype (stmt))))
1097 return gimple_alloca_call_p (stmt);
1104 /* Like tree_expr_nonzero_warnv_p, but this function uses value ranges
1108 vrp_stmt_computes_nonzero (gimple *stmt, bool *strict_overflow_p)
1110 if (gimple_stmt_nonzero_warnv_p (stmt, strict_overflow_p))
1113 /* If we have an expression of the form &X->a, then the expression
1114 is nonnull if X is nonnull. */
1115 if (is_gimple_assign (stmt)
1116 && gimple_assign_rhs_code (stmt) == ADDR_EXPR)
1118 tree expr = gimple_assign_rhs1 (stmt);
1119 tree base = get_base_address (TREE_OPERAND (expr, 0));
1121 if (base != NULL_TREE
1122 && TREE_CODE (base) == MEM_REF
1123 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
1125 value_range *vr = get_value_range (TREE_OPERAND (base, 0));
1126 if (range_is_nonnull (vr))
1134 /* Returns true if EXPR is a valid value (as expected by compare_values) --
1135 a gimple invariant, or SSA_NAME +- CST. */
1138 valid_value_p (tree expr)
1140 if (TREE_CODE (expr) == SSA_NAME)
1143 if (TREE_CODE (expr) == PLUS_EXPR
1144 || TREE_CODE (expr) == MINUS_EXPR)
1145 return (TREE_CODE (TREE_OPERAND (expr, 0)) == SSA_NAME
1146 && TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST);
1148 return is_gimple_min_invariant (expr);
1154 -2 if those are incomparable. */
1156 operand_less_p (tree val, tree val2)
1158 /* LT is folded faster than GE and others. Inline the common case. */
1159 if (TREE_CODE (val) == INTEGER_CST && TREE_CODE (val2) == INTEGER_CST)
1161 if (! is_positive_overflow_infinity (val2))
1162 return tree_int_cst_lt (val, val2);
1168 fold_defer_overflow_warnings ();
1170 tcmp = fold_binary_to_constant (LT_EXPR, boolean_type_node, val, val2);
1172 fold_undefer_and_ignore_overflow_warnings ();
1175 || TREE_CODE (tcmp) != INTEGER_CST)
1178 if (!integer_zerop (tcmp))
1182 /* val >= val2, not considering overflow infinity. */
1183 if (is_negative_overflow_infinity (val))
1184 return is_negative_overflow_infinity (val2) ? 0 : 1;
1185 else if (is_positive_overflow_infinity (val2))
1186 return is_positive_overflow_infinity (val) ? 0 : 1;
1191 /* Compare two values VAL1 and VAL2. Return
1193 -2 if VAL1 and VAL2 cannot be compared at compile-time,
1196 +1 if VAL1 > VAL2, and
1199 This is similar to tree_int_cst_compare but supports pointer values
1200 and values that cannot be compared at compile time.
1202 If STRICT_OVERFLOW_P is not NULL, then set *STRICT_OVERFLOW_P to
1203 true if the return value is only valid if we assume that signed
1204 overflow is undefined. */
1207 compare_values_warnv (tree val1, tree val2, bool *strict_overflow_p)
1212 /* Below we rely on the fact that VAL1 and VAL2 are both pointers or
1214 gcc_assert (POINTER_TYPE_P (TREE_TYPE (val1))
1215 == POINTER_TYPE_P (TREE_TYPE (val2)));
1217 /* Convert the two values into the same type. This is needed because
1218 sizetype causes sign extension even for unsigned types. */
1219 val2 = fold_convert (TREE_TYPE (val1), val2);
1220 STRIP_USELESS_TYPE_CONVERSION (val2);
1222 if ((TREE_CODE (val1) == SSA_NAME
1223 || (TREE_CODE (val1) == NEGATE_EXPR
1224 && TREE_CODE (TREE_OPERAND (val1, 0)) == SSA_NAME)
1225 || TREE_CODE (val1) == PLUS_EXPR
1226 || TREE_CODE (val1) == MINUS_EXPR)
1227 && (TREE_CODE (val2) == SSA_NAME
1228 || (TREE_CODE (val2) == NEGATE_EXPR
1229 && TREE_CODE (TREE_OPERAND (val2, 0)) == SSA_NAME)
1230 || TREE_CODE (val2) == PLUS_EXPR
1231 || TREE_CODE (val2) == MINUS_EXPR))
1233 tree n1, c1, n2, c2;
1234 enum tree_code code1, code2;
1236 /* If VAL1 and VAL2 are of the form '[-]NAME [+-] CST' or 'NAME',
1237 return -1 or +1 accordingly. If VAL1 and VAL2 don't use the
1238 same name, return -2. */
1239 if (TREE_CODE (val1) == SSA_NAME || TREE_CODE (val1) == NEGATE_EXPR)
1247 code1 = TREE_CODE (val1);
1248 n1 = TREE_OPERAND (val1, 0);
1249 c1 = TREE_OPERAND (val1, 1);
1250 if (tree_int_cst_sgn (c1) == -1)
1252 if (is_negative_overflow_infinity (c1))
1254 c1 = fold_unary_to_constant (NEGATE_EXPR, TREE_TYPE (c1), c1);
1257 code1 = code1 == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR;
1261 if (TREE_CODE (val2) == SSA_NAME || TREE_CODE (val2) == NEGATE_EXPR)
1269 code2 = TREE_CODE (val2);
1270 n2 = TREE_OPERAND (val2, 0);
1271 c2 = TREE_OPERAND (val2, 1);
1272 if (tree_int_cst_sgn (c2) == -1)
1274 if (is_negative_overflow_infinity (c2))
1276 c2 = fold_unary_to_constant (NEGATE_EXPR, TREE_TYPE (c2), c2);
1279 code2 = code2 == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR;
1283 /* Both values must use the same name. */
1284 if (TREE_CODE (n1) == NEGATE_EXPR && TREE_CODE (n2) == NEGATE_EXPR)
1286 n1 = TREE_OPERAND (n1, 0);
1287 n2 = TREE_OPERAND (n2, 0);
1292 if (code1 == SSA_NAME && code2 == SSA_NAME)
1296 /* If overflow is defined we cannot simplify more. */
1297 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (val1)))
1300 if (strict_overflow_p != NULL
1301 && (code1 == SSA_NAME || !TREE_NO_WARNING (val1))
1302 && (code2 == SSA_NAME || !TREE_NO_WARNING (val2)))
1303 *strict_overflow_p = true;
1305 if (code1 == SSA_NAME)
1307 if (code2 == PLUS_EXPR)
1308 /* NAME < NAME + CST */
1310 else if (code2 == MINUS_EXPR)
1311 /* NAME > NAME - CST */
1314 else if (code1 == PLUS_EXPR)
1316 if (code2 == SSA_NAME)
1317 /* NAME + CST > NAME */
1319 else if (code2 == PLUS_EXPR)
1320 /* NAME + CST1 > NAME + CST2, if CST1 > CST2 */
1321 return compare_values_warnv (c1, c2, strict_overflow_p);
1322 else if (code2 == MINUS_EXPR)
1323 /* NAME + CST1 > NAME - CST2 */
1326 else if (code1 == MINUS_EXPR)
1328 if (code2 == SSA_NAME)
1329 /* NAME - CST < NAME */
1331 else if (code2 == PLUS_EXPR)
1332 /* NAME - CST1 < NAME + CST2 */
1334 else if (code2 == MINUS_EXPR)
1335 /* NAME - CST1 > NAME - CST2, if CST1 < CST2. Notice that
1336 C1 and C2 are swapped in the call to compare_values. */
1337 return compare_values_warnv (c2, c1, strict_overflow_p);
1343 /* We cannot compare non-constants. */
1344 if (!is_gimple_min_invariant (val1) || !is_gimple_min_invariant (val2))
1347 if (!POINTER_TYPE_P (TREE_TYPE (val1)))
1349 /* We cannot compare overflowed values, except for overflow
1351 if (TREE_OVERFLOW (val1) || TREE_OVERFLOW (val2))
1353 if (strict_overflow_p != NULL)
1354 *strict_overflow_p = true;
1355 if (is_negative_overflow_infinity (val1))
1356 return is_negative_overflow_infinity (val2) ? 0 : -1;
1357 else if (is_negative_overflow_infinity (val2))
1359 else if (is_positive_overflow_infinity (val1))
1360 return is_positive_overflow_infinity (val2) ? 0 : 1;
1361 else if (is_positive_overflow_infinity (val2))
1366 return tree_int_cst_compare (val1, val2);
1372 /* First see if VAL1 and VAL2 are not the same. */
1373 if (val1 == val2 || operand_equal_p (val1, val2, 0))
1376 /* If VAL1 is a lower address than VAL2, return -1. */
1377 if (operand_less_p (val1, val2) == 1)
1380 /* If VAL1 is a higher address than VAL2, return +1. */
1381 if (operand_less_p (val2, val1) == 1)
1384 /* If VAL1 is different than VAL2, return +2.
1385 For integer constants we either have already returned -1 or 1
1386 or they are equivalent. We still might succeed in proving
1387 something about non-trivial operands. */
1388 if (TREE_CODE (val1) != INTEGER_CST
1389 || TREE_CODE (val2) != INTEGER_CST)
1391 t = fold_binary_to_constant (NE_EXPR, boolean_type_node, val1, val2);
1392 if (t && integer_onep (t))
1400 /* Compare values like compare_values_warnv, but treat comparisons of
1401 nonconstants which rely on undefined overflow as incomparable. */
1404 compare_values (tree val1, tree val2)
1410 ret = compare_values_warnv (val1, val2, &sop);
1412 && (!is_gimple_min_invariant (val1) || !is_gimple_min_invariant (val2)))
1418 /* Return 1 if VAL is inside value range MIN <= VAL <= MAX,
1419 0 if VAL is not inside [MIN, MAX],
1420 -2 if we cannot tell either way.
1422 Benchmark compile/20001226-1.c compilation time after changing this
1426 value_inside_range (tree val, tree min, tree max)
1430 cmp1 = operand_less_p (val, min);
1436 cmp2 = operand_less_p (max, val);
1444 /* Return true if value ranges VR0 and VR1 have a non-empty
1447 Benchmark compile/20001226-1.c compilation time after changing this
1452 value_ranges_intersect_p (value_range *vr0, value_range *vr1)
1454 /* The value ranges do not intersect if the maximum of the first range is
1455 less than the minimum of the second range or vice versa.
1456 When those relations are unknown, we can't do any better. */
1457 if (operand_less_p (vr0->max, vr1->min) != 0)
1459 if (operand_less_p (vr1->max, vr0->min) != 0)
1465 /* Return 1 if [MIN, MAX] includes the value zero, 0 if it does not
1466 include the value zero, -2 if we cannot tell. */
1469 range_includes_zero_p (tree min, tree max)
1471 tree zero = build_int_cst (TREE_TYPE (min), 0);
1472 return value_inside_range (zero, min, max);
1475 /* Return true if *VR is know to only contain nonnegative values. */
1478 value_range_nonnegative_p (value_range *vr)
1480 /* Testing for VR_ANTI_RANGE is not useful here as any anti-range
1481 which would return a useful value should be encoded as a
1483 if (vr->type == VR_RANGE)
1485 int result = compare_values (vr->min, integer_zero_node);
1486 return (result == 0 || result == 1);
1492 /* If *VR has a value rante that is a single constant value return that,
1493 otherwise return NULL_TREE. */
1496 value_range_constant_singleton (value_range *vr)
1498 if (vr->type == VR_RANGE
1499 && vrp_operand_equal_p (vr->min, vr->max)
1500 && is_gimple_min_invariant (vr->min))
1506 /* If OP has a value range with a single constant value return that,
1507 otherwise return NULL_TREE. This returns OP itself if OP is a
1511 op_with_constant_singleton_value_range (tree op)
1513 if (is_gimple_min_invariant (op))
1516 if (TREE_CODE (op) != SSA_NAME)
1519 return value_range_constant_singleton (get_value_range (op));
1522 /* Return true if op is in a boolean [0, 1] value-range. */
1525 op_with_boolean_value_range_p (tree op)
1529 if (TYPE_PRECISION (TREE_TYPE (op)) == 1)
1532 if (integer_zerop (op)
1533 || integer_onep (op))
1536 if (TREE_CODE (op) != SSA_NAME)
1539 vr = get_value_range (op);
1540 return (vr->type == VR_RANGE
1541 && integer_zerop (vr->min)
1542 && integer_onep (vr->max));
1545 /* Extract value range information from an ASSERT_EXPR EXPR and store
1549 extract_range_from_assert (value_range *vr_p, tree expr)
1551 tree var, cond, limit, min, max, type;
1552 value_range *limit_vr;
1553 enum tree_code cond_code;
1555 var = ASSERT_EXPR_VAR (expr);
1556 cond = ASSERT_EXPR_COND (expr);
1558 gcc_assert (COMPARISON_CLASS_P (cond));
1560 /* Find VAR in the ASSERT_EXPR conditional. */
1561 if (var == TREE_OPERAND (cond, 0)
1562 || TREE_CODE (TREE_OPERAND (cond, 0)) == PLUS_EXPR
1563 || TREE_CODE (TREE_OPERAND (cond, 0)) == NOP_EXPR)
1565 /* If the predicate is of the form VAR COMP LIMIT, then we just
1566 take LIMIT from the RHS and use the same comparison code. */
1567 cond_code = TREE_CODE (cond);
1568 limit = TREE_OPERAND (cond, 1);
1569 cond = TREE_OPERAND (cond, 0);
1573 /* If the predicate is of the form LIMIT COMP VAR, then we need
1574 to flip around the comparison code to create the proper range
1576 cond_code = swap_tree_comparison (TREE_CODE (cond));
1577 limit = TREE_OPERAND (cond, 0);
1578 cond = TREE_OPERAND (cond, 1);
1581 limit = avoid_overflow_infinity (limit);
1583 type = TREE_TYPE (var);
1584 gcc_assert (limit != var);
1586 /* For pointer arithmetic, we only keep track of pointer equality
1588 if (POINTER_TYPE_P (type) && cond_code != NE_EXPR && cond_code != EQ_EXPR)
1590 set_value_range_to_varying (vr_p);
1594 /* If LIMIT is another SSA name and LIMIT has a range of its own,
1595 try to use LIMIT's range to avoid creating symbolic ranges
1597 limit_vr = (TREE_CODE (limit) == SSA_NAME) ? get_value_range (limit) : NULL;
1599 /* LIMIT's range is only interesting if it has any useful information. */
1601 && (limit_vr->type == VR_UNDEFINED
1602 || limit_vr->type == VR_VARYING
1603 || symbolic_range_p (limit_vr)))
1606 /* Initially, the new range has the same set of equivalences of
1607 VAR's range. This will be revised before returning the final
1608 value. Since assertions may be chained via mutually exclusive
1609 predicates, we will need to trim the set of equivalences before
1611 gcc_assert (vr_p->equiv == NULL);
1612 add_equivalence (&vr_p->equiv, var);
1614 /* Extract a new range based on the asserted comparison for VAR and
1615 LIMIT's value range. Notice that if LIMIT has an anti-range, we
1616 will only use it for equality comparisons (EQ_EXPR). For any
1617 other kind of assertion, we cannot derive a range from LIMIT's
1618 anti-range that can be used to describe the new range. For
1619 instance, ASSERT_EXPR <x_2, x_2 <= b_4>. If b_4 is ~[2, 10],
1620 then b_4 takes on the ranges [-INF, 1] and [11, +INF]. There is
1621 no single range for x_2 that could describe LE_EXPR, so we might
1622 as well build the range [b_4, +INF] for it.
1623 One special case we handle is extracting a range from a
1624 range test encoded as (unsigned)var + CST <= limit. */
1625 if (TREE_CODE (cond) == NOP_EXPR
1626 || TREE_CODE (cond) == PLUS_EXPR)
1628 if (TREE_CODE (cond) == PLUS_EXPR)
1630 min = fold_build1 (NEGATE_EXPR, TREE_TYPE (TREE_OPERAND (cond, 1)),
1631 TREE_OPERAND (cond, 1));
1632 max = int_const_binop (PLUS_EXPR, limit, min);
1633 cond = TREE_OPERAND (cond, 0);
1637 min = build_int_cst (TREE_TYPE (var), 0);
1641 /* Make sure to not set TREE_OVERFLOW on the final type
1642 conversion. We are willingly interpreting large positive
1643 unsigned values as negative signed values here. */
1644 min = force_fit_type (TREE_TYPE (var), wi::to_widest (min), 0, false);
1645 max = force_fit_type (TREE_TYPE (var), wi::to_widest (max), 0, false);
1647 /* We can transform a max, min range to an anti-range or
1648 vice-versa. Use set_and_canonicalize_value_range which does
1650 if (cond_code == LE_EXPR)
1651 set_and_canonicalize_value_range (vr_p, VR_RANGE,
1652 min, max, vr_p->equiv);
1653 else if (cond_code == GT_EXPR)
1654 set_and_canonicalize_value_range (vr_p, VR_ANTI_RANGE,
1655 min, max, vr_p->equiv);
1659 else if (cond_code == EQ_EXPR)
1661 enum value_range_type range_type;
1665 range_type = limit_vr->type;
1666 min = limit_vr->min;
1667 max = limit_vr->max;
1671 range_type = VR_RANGE;
1676 set_value_range (vr_p, range_type, min, max, vr_p->equiv);
1678 /* When asserting the equality VAR == LIMIT and LIMIT is another
1679 SSA name, the new range will also inherit the equivalence set
1681 if (TREE_CODE (limit) == SSA_NAME)
1682 add_equivalence (&vr_p->equiv, limit);
1684 else if (cond_code == NE_EXPR)
1686 /* As described above, when LIMIT's range is an anti-range and
1687 this assertion is an inequality (NE_EXPR), then we cannot
1688 derive anything from the anti-range. For instance, if
1689 LIMIT's range was ~[0, 0], the assertion 'VAR != LIMIT' does
1690 not imply that VAR's range is [0, 0]. So, in the case of
1691 anti-ranges, we just assert the inequality using LIMIT and
1694 If LIMIT_VR is a range, we can only use it to build a new
1695 anti-range if LIMIT_VR is a single-valued range. For
1696 instance, if LIMIT_VR is [0, 1], the predicate
1697 VAR != [0, 1] does not mean that VAR's range is ~[0, 1].
1698 Rather, it means that for value 0 VAR should be ~[0, 0]
1699 and for value 1, VAR should be ~[1, 1]. We cannot
1700 represent these ranges.
1702 The only situation in which we can build a valid
1703 anti-range is when LIMIT_VR is a single-valued range
1704 (i.e., LIMIT_VR->MIN == LIMIT_VR->MAX). In that case,
1705 build the anti-range ~[LIMIT_VR->MIN, LIMIT_VR->MAX]. */
1707 && limit_vr->type == VR_RANGE
1708 && compare_values (limit_vr->min, limit_vr->max) == 0)
1710 min = limit_vr->min;
1711 max = limit_vr->max;
1715 /* In any other case, we cannot use LIMIT's range to build a
1716 valid anti-range. */
1720 /* If MIN and MAX cover the whole range for their type, then
1721 just use the original LIMIT. */
1722 if (INTEGRAL_TYPE_P (type)
1723 && vrp_val_is_min (min)
1724 && vrp_val_is_max (max))
1727 set_and_canonicalize_value_range (vr_p, VR_ANTI_RANGE,
1728 min, max, vr_p->equiv);
1730 else if (cond_code == LE_EXPR || cond_code == LT_EXPR)
1732 min = TYPE_MIN_VALUE (type);
1734 if (limit_vr == NULL || limit_vr->type == VR_ANTI_RANGE)
1738 /* If LIMIT_VR is of the form [N1, N2], we need to build the
1739 range [MIN, N2] for LE_EXPR and [MIN, N2 - 1] for
1741 max = limit_vr->max;
1744 /* If the maximum value forces us to be out of bounds, simply punt.
1745 It would be pointless to try and do anything more since this
1746 all should be optimized away above us. */
1747 if ((cond_code == LT_EXPR
1748 && compare_values (max, min) == 0)
1749 || is_overflow_infinity (max))
1750 set_value_range_to_varying (vr_p);
1753 /* For LT_EXPR, we create the range [MIN, MAX - 1]. */
1754 if (cond_code == LT_EXPR)
1756 if (TYPE_PRECISION (TREE_TYPE (max)) == 1
1757 && !TYPE_UNSIGNED (TREE_TYPE (max)))
1758 max = fold_build2 (PLUS_EXPR, TREE_TYPE (max), max,
1759 build_int_cst (TREE_TYPE (max), -1));
1761 max = fold_build2 (MINUS_EXPR, TREE_TYPE (max), max,
1762 build_int_cst (TREE_TYPE (max), 1));
1764 TREE_NO_WARNING (max) = 1;
1767 set_value_range (vr_p, VR_RANGE, min, max, vr_p->equiv);
1770 else if (cond_code == GE_EXPR || cond_code == GT_EXPR)
1772 max = TYPE_MAX_VALUE (type);
1774 if (limit_vr == NULL || limit_vr->type == VR_ANTI_RANGE)
1778 /* If LIMIT_VR is of the form [N1, N2], we need to build the
1779 range [N1, MAX] for GE_EXPR and [N1 + 1, MAX] for
1781 min = limit_vr->min;
1784 /* If the minimum value forces us to be out of bounds, simply punt.
1785 It would be pointless to try and do anything more since this
1786 all should be optimized away above us. */
1787 if ((cond_code == GT_EXPR
1788 && compare_values (min, max) == 0)
1789 || is_overflow_infinity (min))
1790 set_value_range_to_varying (vr_p);
1793 /* For GT_EXPR, we create the range [MIN + 1, MAX]. */
1794 if (cond_code == GT_EXPR)
1796 if (TYPE_PRECISION (TREE_TYPE (min)) == 1
1797 && !TYPE_UNSIGNED (TREE_TYPE (min)))
1798 min = fold_build2 (MINUS_EXPR, TREE_TYPE (min), min,
1799 build_int_cst (TREE_TYPE (min), -1));
1801 min = fold_build2 (PLUS_EXPR, TREE_TYPE (min), min,
1802 build_int_cst (TREE_TYPE (min), 1));
1804 TREE_NO_WARNING (min) = 1;
1807 set_value_range (vr_p, VR_RANGE, min, max, vr_p->equiv);
1813 /* Finally intersect the new range with what we already know about var. */
1814 vrp_intersect_ranges (vr_p, get_value_range (var));
1818 /* Extract range information from SSA name VAR and store it in VR. If
1819 VAR has an interesting range, use it. Otherwise, create the
1820 range [VAR, VAR] and return it. This is useful in situations where
1821 we may have conditionals testing values of VARYING names. For
1828 Even if y_5 is deemed VARYING, we can determine that x_3 > y_5 is
1832 extract_range_from_ssa_name (value_range *vr, tree var)
1834 value_range *var_vr = get_value_range (var);
1836 if (var_vr->type != VR_VARYING)
1837 copy_value_range (vr, var_vr);
1839 set_value_range (vr, VR_RANGE, var, var, NULL);
1841 add_equivalence (&vr->equiv, var);
1845 /* Wrapper around int_const_binop. If the operation overflows and we
1846 are not using wrapping arithmetic, then adjust the result to be
1847 -INF or +INF depending on CODE, VAL1 and VAL2. This can return
1848 NULL_TREE if we need to use an overflow infinity representation but
1849 the type does not support it. */
1852 vrp_int_const_binop (enum tree_code code, tree val1, tree val2)
1856 res = int_const_binop (code, val1, val2);
1858 /* If we are using unsigned arithmetic, operate symbolically
1859 on -INF and +INF as int_const_binop only handles signed overflow. */
1860 if (TYPE_UNSIGNED (TREE_TYPE (val1)))
1862 int checkz = compare_values (res, val1);
1863 bool overflow = false;
1865 /* Ensure that res = val1 [+*] val2 >= val1
1866 or that res = val1 - val2 <= val1. */
1867 if ((code == PLUS_EXPR
1868 && !(checkz == 1 || checkz == 0))
1869 || (code == MINUS_EXPR
1870 && !(checkz == 0 || checkz == -1)))
1874 /* Checking for multiplication overflow is done by dividing the
1875 output of the multiplication by the first input of the
1876 multiplication. If the result of that division operation is
1877 not equal to the second input of the multiplication, then the
1878 multiplication overflowed. */
1879 else if (code == MULT_EXPR && !integer_zerop (val1))
1881 tree tmp = int_const_binop (TRUNC_DIV_EXPR,
1884 int check = compare_values (tmp, val2);
1892 res = copy_node (res);
1893 TREE_OVERFLOW (res) = 1;
1897 else if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (val1)))
1898 /* If the singed operation wraps then int_const_binop has done
1899 everything we want. */
1901 /* Signed division of -1/0 overflows and by the time it gets here
1902 returns NULL_TREE. */
1905 else if ((TREE_OVERFLOW (res)
1906 && !TREE_OVERFLOW (val1)
1907 && !TREE_OVERFLOW (val2))
1908 || is_overflow_infinity (val1)
1909 || is_overflow_infinity (val2))
1911 /* If the operation overflowed but neither VAL1 nor VAL2 are
1912 overflown, return -INF or +INF depending on the operation
1913 and the combination of signs of the operands. */
1914 int sgn1 = tree_int_cst_sgn (val1);
1915 int sgn2 = tree_int_cst_sgn (val2);
1917 if (needs_overflow_infinity (TREE_TYPE (res))
1918 && !supports_overflow_infinity (TREE_TYPE (res)))
1921 /* We have to punt on adding infinities of different signs,
1922 since we can't tell what the sign of the result should be.
1923 Likewise for subtracting infinities of the same sign. */
1924 if (((code == PLUS_EXPR && sgn1 != sgn2)
1925 || (code == MINUS_EXPR && sgn1 == sgn2))
1926 && is_overflow_infinity (val1)
1927 && is_overflow_infinity (val2))
1930 /* Don't try to handle division or shifting of infinities. */
1931 if ((code == TRUNC_DIV_EXPR
1932 || code == FLOOR_DIV_EXPR
1933 || code == CEIL_DIV_EXPR
1934 || code == EXACT_DIV_EXPR
1935 || code == ROUND_DIV_EXPR
1936 || code == RSHIFT_EXPR)
1937 && (is_overflow_infinity (val1)
1938 || is_overflow_infinity (val2)))
1941 /* Notice that we only need to handle the restricted set of
1942 operations handled by extract_range_from_binary_expr.
1943 Among them, only multiplication, addition and subtraction
1944 can yield overflow without overflown operands because we
1945 are working with integral types only... except in the
1946 case VAL1 = -INF and VAL2 = -1 which overflows to +INF
1947 for division too. */
1949 /* For multiplication, the sign of the overflow is given
1950 by the comparison of the signs of the operands. */
1951 if ((code == MULT_EXPR && sgn1 == sgn2)
1952 /* For addition, the operands must be of the same sign
1953 to yield an overflow. Its sign is therefore that
1954 of one of the operands, for example the first. For
1955 infinite operands X + -INF is negative, not positive. */
1956 || (code == PLUS_EXPR
1958 ? !is_negative_overflow_infinity (val2)
1959 : is_positive_overflow_infinity (val2)))
1960 /* For subtraction, non-infinite operands must be of
1961 different signs to yield an overflow. Its sign is
1962 therefore that of the first operand or the opposite of
1963 that of the second operand. A first operand of 0 counts
1964 as positive here, for the corner case 0 - (-INF), which
1965 overflows, but must yield +INF. For infinite operands 0
1966 - INF is negative, not positive. */
1967 || (code == MINUS_EXPR
1969 ? !is_positive_overflow_infinity (val2)
1970 : is_negative_overflow_infinity (val2)))
1971 /* We only get in here with positive shift count, so the
1972 overflow direction is the same as the sign of val1.
1973 Actually rshift does not overflow at all, but we only
1974 handle the case of shifting overflowed -INF and +INF. */
1975 || (code == RSHIFT_EXPR
1977 /* For division, the only case is -INF / -1 = +INF. */
1978 || code == TRUNC_DIV_EXPR
1979 || code == FLOOR_DIV_EXPR
1980 || code == CEIL_DIV_EXPR
1981 || code == EXACT_DIV_EXPR
1982 || code == ROUND_DIV_EXPR)
1983 return (needs_overflow_infinity (TREE_TYPE (res))
1984 ? positive_overflow_infinity (TREE_TYPE (res))
1985 : TYPE_MAX_VALUE (TREE_TYPE (res)));
1987 return (needs_overflow_infinity (TREE_TYPE (res))
1988 ? negative_overflow_infinity (TREE_TYPE (res))
1989 : TYPE_MIN_VALUE (TREE_TYPE (res)));
1996 /* For range VR compute two wide_int bitmasks. In *MAY_BE_NONZERO
1997 bitmask if some bit is unset, it means for all numbers in the range
1998 the bit is 0, otherwise it might be 0 or 1. In *MUST_BE_NONZERO
1999 bitmask if some bit is set, it means for all numbers in the range
2000 the bit is 1, otherwise it might be 0 or 1. */
2003 zero_nonzero_bits_from_vr (const tree expr_type,
2005 wide_int *may_be_nonzero,
2006 wide_int *must_be_nonzero)
2008 *may_be_nonzero = wi::minus_one (TYPE_PRECISION (expr_type));
2009 *must_be_nonzero = wi::zero (TYPE_PRECISION (expr_type));
2010 if (!range_int_cst_p (vr)
2011 || is_overflow_infinity (vr->min)
2012 || is_overflow_infinity (vr->max))
2015 if (range_int_cst_singleton_p (vr))
2017 *may_be_nonzero = vr->min;
2018 *must_be_nonzero = *may_be_nonzero;
2020 else if (tree_int_cst_sgn (vr->min) >= 0
2021 || tree_int_cst_sgn (vr->max) < 0)
2023 wide_int xor_mask = wi::bit_xor (vr->min, vr->max);
2024 *may_be_nonzero = wi::bit_or (vr->min, vr->max);
2025 *must_be_nonzero = wi::bit_and (vr->min, vr->max);
2028 wide_int mask = wi::mask (wi::floor_log2 (xor_mask), false,
2029 may_be_nonzero->get_precision ());
2030 *may_be_nonzero = *may_be_nonzero | mask;
2031 *must_be_nonzero = must_be_nonzero->and_not (mask);
2038 /* Create two value-ranges in *VR0 and *VR1 from the anti-range *AR
2039 so that *VR0 U *VR1 == *AR. Returns true if that is possible,
2040 false otherwise. If *AR can be represented with a single range
2041 *VR1 will be VR_UNDEFINED. */
2044 ranges_from_anti_range (value_range *ar,
2045 value_range *vr0, value_range *vr1)
2047 tree type = TREE_TYPE (ar->min);
2049 vr0->type = VR_UNDEFINED;
2050 vr1->type = VR_UNDEFINED;
2052 if (ar->type != VR_ANTI_RANGE
2053 || TREE_CODE (ar->min) != INTEGER_CST
2054 || TREE_CODE (ar->max) != INTEGER_CST
2055 || !vrp_val_min (type)
2056 || !vrp_val_max (type))
2059 if (!vrp_val_is_min (ar->min))
2061 vr0->type = VR_RANGE;
2062 vr0->min = vrp_val_min (type);
2063 vr0->max = wide_int_to_tree (type, wi::sub (ar->min, 1));
2065 if (!vrp_val_is_max (ar->max))
2067 vr1->type = VR_RANGE;
2068 vr1->min = wide_int_to_tree (type, wi::add (ar->max, 1));
2069 vr1->max = vrp_val_max (type);
2071 if (vr0->type == VR_UNDEFINED)
2074 vr1->type = VR_UNDEFINED;
2077 return vr0->type != VR_UNDEFINED;
2080 /* Helper to extract a value-range *VR for a multiplicative operation
2084 extract_range_from_multiplicative_op_1 (value_range *vr,
2085 enum tree_code code,
2086 value_range *vr0, value_range *vr1)
2088 enum value_range_type type;
2095 /* Multiplications, divisions and shifts are a bit tricky to handle,
2096 depending on the mix of signs we have in the two ranges, we
2097 need to operate on different values to get the minimum and
2098 maximum values for the new range. One approach is to figure
2099 out all the variations of range combinations and do the
2102 However, this involves several calls to compare_values and it
2103 is pretty convoluted. It's simpler to do the 4 operations
2104 (MIN0 OP MIN1, MIN0 OP MAX1, MAX0 OP MIN1 and MAX0 OP MAX0 OP
2105 MAX1) and then figure the smallest and largest values to form
2107 gcc_assert (code == MULT_EXPR
2108 || code == TRUNC_DIV_EXPR
2109 || code == FLOOR_DIV_EXPR
2110 || code == CEIL_DIV_EXPR
2111 || code == EXACT_DIV_EXPR
2112 || code == ROUND_DIV_EXPR
2113 || code == RSHIFT_EXPR
2114 || code == LSHIFT_EXPR);
2115 gcc_assert ((vr0->type == VR_RANGE
2116 || (code == MULT_EXPR && vr0->type == VR_ANTI_RANGE))
2117 && vr0->type == vr1->type);
2121 /* Compute the 4 cross operations. */
2123 val[0] = vrp_int_const_binop (code, vr0->min, vr1->min);
2124 if (val[0] == NULL_TREE)
2127 if (vr1->max == vr1->min)
2131 val[1] = vrp_int_const_binop (code, vr0->min, vr1->max);
2132 if (val[1] == NULL_TREE)
2136 if (vr0->max == vr0->min)
2140 val[2] = vrp_int_const_binop (code, vr0->max, vr1->min);
2141 if (val[2] == NULL_TREE)
2145 if (vr0->min == vr0->max || vr1->min == vr1->max)
2149 val[3] = vrp_int_const_binop (code, vr0->max, vr1->max);
2150 if (val[3] == NULL_TREE)
2156 set_value_range_to_varying (vr);
2160 /* Set MIN to the minimum of VAL[i] and MAX to the maximum
2164 for (i = 1; i < 4; i++)
2166 if (!is_gimple_min_invariant (min)
2167 || (TREE_OVERFLOW (min) && !is_overflow_infinity (min))
2168 || !is_gimple_min_invariant (max)
2169 || (TREE_OVERFLOW (max) && !is_overflow_infinity (max)))
2174 if (!is_gimple_min_invariant (val[i])
2175 || (TREE_OVERFLOW (val[i])
2176 && !is_overflow_infinity (val[i])))
2178 /* If we found an overflowed value, set MIN and MAX
2179 to it so that we set the resulting range to
2185 if (compare_values (val[i], min) == -1)
2188 if (compare_values (val[i], max) == 1)
2193 /* If either MIN or MAX overflowed, then set the resulting range to
2194 VARYING. But we do accept an overflow infinity
2196 if (min == NULL_TREE
2197 || !is_gimple_min_invariant (min)
2198 || (TREE_OVERFLOW (min) && !is_overflow_infinity (min))
2200 || !is_gimple_min_invariant (max)
2201 || (TREE_OVERFLOW (max) && !is_overflow_infinity (max)))
2203 set_value_range_to_varying (vr);
2209 2) [-INF, +-INF(OVF)]
2210 3) [+-INF(OVF), +INF]
2211 4) [+-INF(OVF), +-INF(OVF)]
2212 We learn nothing when we have INF and INF(OVF) on both sides.
2213 Note that we do accept [-INF, -INF] and [+INF, +INF] without
2215 if ((vrp_val_is_min (min) || is_overflow_infinity (min))
2216 && (vrp_val_is_max (max) || is_overflow_infinity (max)))
2218 set_value_range_to_varying (vr);
2222 cmp = compare_values (min, max);
2223 if (cmp == -2 || cmp == 1)
2225 /* If the new range has its limits swapped around (MIN > MAX),
2226 then the operation caused one of them to wrap around, mark
2227 the new range VARYING. */
2228 set_value_range_to_varying (vr);
2231 set_value_range (vr, type, min, max, NULL);
2234 /* Extract range information from a binary operation CODE based on
2235 the ranges of each of its operands *VR0 and *VR1 with resulting
2236 type EXPR_TYPE. The resulting range is stored in *VR. */
2239 extract_range_from_binary_expr_1 (value_range *vr,
2240 enum tree_code code, tree expr_type,
2241 value_range *vr0_, value_range *vr1_)
2243 value_range vr0 = *vr0_, vr1 = *vr1_;
2244 value_range vrtem0 = VR_INITIALIZER, vrtem1 = VR_INITIALIZER;
2245 enum value_range_type type;
2246 tree min = NULL_TREE, max = NULL_TREE;
2249 if (!INTEGRAL_TYPE_P (expr_type)
2250 && !POINTER_TYPE_P (expr_type))
2252 set_value_range_to_varying (vr);
2256 /* Not all binary expressions can be applied to ranges in a
2257 meaningful way. Handle only arithmetic operations. */
2258 if (code != PLUS_EXPR
2259 && code != MINUS_EXPR
2260 && code != POINTER_PLUS_EXPR
2261 && code != MULT_EXPR
2262 && code != TRUNC_DIV_EXPR
2263 && code != FLOOR_DIV_EXPR
2264 && code != CEIL_DIV_EXPR
2265 && code != EXACT_DIV_EXPR
2266 && code != ROUND_DIV_EXPR
2267 && code != TRUNC_MOD_EXPR
2268 && code != RSHIFT_EXPR
2269 && code != LSHIFT_EXPR
2272 && code != BIT_AND_EXPR
2273 && code != BIT_IOR_EXPR
2274 && code != BIT_XOR_EXPR)
2276 set_value_range_to_varying (vr);
2280 /* If both ranges are UNDEFINED, so is the result. */
2281 if (vr0.type == VR_UNDEFINED && vr1.type == VR_UNDEFINED)
2283 set_value_range_to_undefined (vr);
2286 /* If one of the ranges is UNDEFINED drop it to VARYING for the following
2287 code. At some point we may want to special-case operations that
2288 have UNDEFINED result for all or some value-ranges of the not UNDEFINED
2290 else if (vr0.type == VR_UNDEFINED)
2291 set_value_range_to_varying (&vr0);
2292 else if (vr1.type == VR_UNDEFINED)
2293 set_value_range_to_varying (&vr1);
2295 /* Now canonicalize anti-ranges to ranges when they are not symbolic
2296 and express ~[] op X as ([]' op X) U ([]'' op X). */
2297 if (vr0.type == VR_ANTI_RANGE
2298 && ranges_from_anti_range (&vr0, &vrtem0, &vrtem1))
2300 extract_range_from_binary_expr_1 (vr, code, expr_type, &vrtem0, vr1_);
2301 if (vrtem1.type != VR_UNDEFINED)
2303 value_range vrres = VR_INITIALIZER;
2304 extract_range_from_binary_expr_1 (&vrres, code, expr_type,
2306 vrp_meet (vr, &vrres);
2310 /* Likewise for X op ~[]. */
2311 if (vr1.type == VR_ANTI_RANGE
2312 && ranges_from_anti_range (&vr1, &vrtem0, &vrtem1))
2314 extract_range_from_binary_expr_1 (vr, code, expr_type, vr0_, &vrtem0);
2315 if (vrtem1.type != VR_UNDEFINED)
2317 value_range vrres = VR_INITIALIZER;
2318 extract_range_from_binary_expr_1 (&vrres, code, expr_type,
2320 vrp_meet (vr, &vrres);
2325 /* The type of the resulting value range defaults to VR0.TYPE. */
2328 /* Refuse to operate on VARYING ranges, ranges of different kinds
2329 and symbolic ranges. As an exception, we allow BIT_{AND,IOR}
2330 because we may be able to derive a useful range even if one of
2331 the operands is VR_VARYING or symbolic range. Similarly for
2332 divisions, MIN/MAX and PLUS/MINUS.
2334 TODO, we may be able to derive anti-ranges in some cases. */
2335 if (code != BIT_AND_EXPR
2336 && code != BIT_IOR_EXPR
2337 && code != TRUNC_DIV_EXPR
2338 && code != FLOOR_DIV_EXPR
2339 && code != CEIL_DIV_EXPR
2340 && code != EXACT_DIV_EXPR
2341 && code != ROUND_DIV_EXPR
2342 && code != TRUNC_MOD_EXPR
2345 && code != PLUS_EXPR
2346 && code != MINUS_EXPR
2347 && code != RSHIFT_EXPR
2348 && (vr0.type == VR_VARYING
2349 || vr1.type == VR_VARYING
2350 || vr0.type != vr1.type
2351 || symbolic_range_p (&vr0)
2352 || symbolic_range_p (&vr1)))
2354 set_value_range_to_varying (vr);
2358 /* Now evaluate the expression to determine the new range. */
2359 if (POINTER_TYPE_P (expr_type))
2361 if (code == MIN_EXPR || code == MAX_EXPR)
2363 /* For MIN/MAX expressions with pointers, we only care about
2364 nullness, if both are non null, then the result is nonnull.
2365 If both are null, then the result is null. Otherwise they
2367 if (range_is_nonnull (&vr0) && range_is_nonnull (&vr1))
2368 set_value_range_to_nonnull (vr, expr_type);
2369 else if (range_is_null (&vr0) && range_is_null (&vr1))
2370 set_value_range_to_null (vr, expr_type);
2372 set_value_range_to_varying (vr);
2374 else if (code == POINTER_PLUS_EXPR)
2376 /* For pointer types, we are really only interested in asserting
2377 whether the expression evaluates to non-NULL. */
2378 if (range_is_nonnull (&vr0) || range_is_nonnull (&vr1))
2379 set_value_range_to_nonnull (vr, expr_type);
2380 else if (range_is_null (&vr0) && range_is_null (&vr1))
2381 set_value_range_to_null (vr, expr_type);
2383 set_value_range_to_varying (vr);
2385 else if (code == BIT_AND_EXPR)
2387 /* For pointer types, we are really only interested in asserting
2388 whether the expression evaluates to non-NULL. */
2389 if (range_is_nonnull (&vr0) && range_is_nonnull (&vr1))
2390 set_value_range_to_nonnull (vr, expr_type);
2391 else if (range_is_null (&vr0) || range_is_null (&vr1))
2392 set_value_range_to_null (vr, expr_type);
2394 set_value_range_to_varying (vr);
2397 set_value_range_to_varying (vr);
2402 /* For integer ranges, apply the operation to each end of the
2403 range and see what we end up with. */
2404 if (code == PLUS_EXPR || code == MINUS_EXPR)
2406 const bool minus_p = (code == MINUS_EXPR);
2407 tree min_op0 = vr0.min;
2408 tree min_op1 = minus_p ? vr1.max : vr1.min;
2409 tree max_op0 = vr0.max;
2410 tree max_op1 = minus_p ? vr1.min : vr1.max;
2411 tree sym_min_op0 = NULL_TREE;
2412 tree sym_min_op1 = NULL_TREE;
2413 tree sym_max_op0 = NULL_TREE;
2414 tree sym_max_op1 = NULL_TREE;
2415 bool neg_min_op0, neg_min_op1, neg_max_op0, neg_max_op1;
2417 /* If we have a PLUS or MINUS with two VR_RANGEs, either constant or
2418 single-symbolic ranges, try to compute the precise resulting range,
2419 but only if we know that this resulting range will also be constant
2420 or single-symbolic. */
2421 if (vr0.type == VR_RANGE && vr1.type == VR_RANGE
2422 && (TREE_CODE (min_op0) == INTEGER_CST
2424 = get_single_symbol (min_op0, &neg_min_op0, &min_op0)))
2425 && (TREE_CODE (min_op1) == INTEGER_CST
2427 = get_single_symbol (min_op1, &neg_min_op1, &min_op1)))
2428 && (!(sym_min_op0 && sym_min_op1)
2429 || (sym_min_op0 == sym_min_op1
2430 && neg_min_op0 == (minus_p ? neg_min_op1 : !neg_min_op1)))
2431 && (TREE_CODE (max_op0) == INTEGER_CST
2433 = get_single_symbol (max_op0, &neg_max_op0, &max_op0)))
2434 && (TREE_CODE (max_op1) == INTEGER_CST
2436 = get_single_symbol (max_op1, &neg_max_op1, &max_op1)))
2437 && (!(sym_max_op0 && sym_max_op1)
2438 || (sym_max_op0 == sym_max_op1
2439 && neg_max_op0 == (minus_p ? neg_max_op1 : !neg_max_op1))))
2441 const signop sgn = TYPE_SIGN (expr_type);
2442 const unsigned int prec = TYPE_PRECISION (expr_type);
2443 wide_int type_min, type_max, wmin, wmax;
2447 /* Get the lower and upper bounds of the type. */
2448 if (TYPE_OVERFLOW_WRAPS (expr_type))
2450 type_min = wi::min_value (prec, sgn);
2451 type_max = wi::max_value (prec, sgn);
2455 type_min = vrp_val_min (expr_type);
2456 type_max = vrp_val_max (expr_type);
2459 /* Combine the lower bounds, if any. */
2460 if (min_op0 && min_op1)
2464 wmin = wi::sub (min_op0, min_op1);
2466 /* Check for overflow. */
2467 if (wi::cmp (0, min_op1, sgn)
2468 != wi::cmp (wmin, min_op0, sgn))
2469 min_ovf = wi::cmp (min_op0, min_op1, sgn);
2473 wmin = wi::add (min_op0, min_op1);
2475 /* Check for overflow. */
2476 if (wi::cmp (min_op1, 0, sgn)
2477 != wi::cmp (wmin, min_op0, sgn))
2478 min_ovf = wi::cmp (min_op0, wmin, sgn);
2484 wmin = minus_p ? wi::neg (min_op1) : min_op1;
2486 wmin = wi::shwi (0, prec);
2488 /* Combine the upper bounds, if any. */
2489 if (max_op0 && max_op1)
2493 wmax = wi::sub (max_op0, max_op1);
2495 /* Check for overflow. */
2496 if (wi::cmp (0, max_op1, sgn)
2497 != wi::cmp (wmax, max_op0, sgn))
2498 max_ovf = wi::cmp (max_op0, max_op1, sgn);
2502 wmax = wi::add (max_op0, max_op1);
2504 if (wi::cmp (max_op1, 0, sgn)
2505 != wi::cmp (wmax, max_op0, sgn))
2506 max_ovf = wi::cmp (max_op0, wmax, sgn);
2512 wmax = minus_p ? wi::neg (max_op1) : max_op1;
2514 wmax = wi::shwi (0, prec);
2516 /* Check for type overflow. */
2519 if (wi::cmp (wmin, type_min, sgn) == -1)
2521 else if (wi::cmp (wmin, type_max, sgn) == 1)
2526 if (wi::cmp (wmax, type_min, sgn) == -1)
2528 else if (wi::cmp (wmax, type_max, sgn) == 1)
2532 /* If we have overflow for the constant part and the resulting
2533 range will be symbolic, drop to VR_VARYING. */
2534 if ((min_ovf && sym_min_op0 != sym_min_op1)
2535 || (max_ovf && sym_max_op0 != sym_max_op1))
2537 set_value_range_to_varying (vr);
2541 if (TYPE_OVERFLOW_WRAPS (expr_type))
2543 /* If overflow wraps, truncate the values and adjust the
2544 range kind and bounds appropriately. */
2545 wide_int tmin = wide_int::from (wmin, prec, sgn);
2546 wide_int tmax = wide_int::from (wmax, prec, sgn);
2547 if (min_ovf == max_ovf)
2549 /* No overflow or both overflow or underflow. The
2550 range kind stays VR_RANGE. */
2551 min = wide_int_to_tree (expr_type, tmin);
2552 max = wide_int_to_tree (expr_type, tmax);
2554 else if ((min_ovf == -1 && max_ovf == 0)
2555 || (max_ovf == 1 && min_ovf == 0))
2557 /* Min underflow or max overflow. The range kind
2558 changes to VR_ANTI_RANGE. */
2559 bool covers = false;
2560 wide_int tem = tmin;
2561 type = VR_ANTI_RANGE;
2563 if (wi::cmp (tmin, tmax, sgn) < 0)
2566 if (wi::cmp (tmax, tem, sgn) > 0)
2568 /* If the anti-range would cover nothing, drop to varying.
2569 Likewise if the anti-range bounds are outside of the
2571 if (covers || wi::cmp (tmin, tmax, sgn) > 0)
2573 set_value_range_to_varying (vr);
2576 min = wide_int_to_tree (expr_type, tmin);
2577 max = wide_int_to_tree (expr_type, tmax);
2581 /* Other underflow and/or overflow, drop to VR_VARYING. */
2582 set_value_range_to_varying (vr);
2588 /* If overflow does not wrap, saturate to the types min/max
2592 if (needs_overflow_infinity (expr_type)
2593 && supports_overflow_infinity (expr_type))
2594 min = negative_overflow_infinity (expr_type);
2596 min = wide_int_to_tree (expr_type, type_min);
2598 else if (min_ovf == 1)
2600 if (needs_overflow_infinity (expr_type)
2601 && supports_overflow_infinity (expr_type))
2602 min = positive_overflow_infinity (expr_type);
2604 min = wide_int_to_tree (expr_type, type_max);
2607 min = wide_int_to_tree (expr_type, wmin);
2611 if (needs_overflow_infinity (expr_type)
2612 && supports_overflow_infinity (expr_type))
2613 max = negative_overflow_infinity (expr_type);
2615 max = wide_int_to_tree (expr_type, type_min);
2617 else if (max_ovf == 1)
2619 if (needs_overflow_infinity (expr_type)
2620 && supports_overflow_infinity (expr_type))
2621 max = positive_overflow_infinity (expr_type);
2623 max = wide_int_to_tree (expr_type, type_max);
2626 max = wide_int_to_tree (expr_type, wmax);
2629 if (needs_overflow_infinity (expr_type)
2630 && supports_overflow_infinity (expr_type))
2632 if ((min_op0 && is_negative_overflow_infinity (min_op0))
2635 ? is_positive_overflow_infinity (min_op1)
2636 : is_negative_overflow_infinity (min_op1))))
2637 min = negative_overflow_infinity (expr_type);
2638 if ((max_op0 && is_positive_overflow_infinity (max_op0))
2641 ? is_negative_overflow_infinity (max_op1)
2642 : is_positive_overflow_infinity (max_op1))))
2643 max = positive_overflow_infinity (expr_type);
2646 /* If the result lower bound is constant, we're done;
2647 otherwise, build the symbolic lower bound. */
2648 if (sym_min_op0 == sym_min_op1)
2650 else if (sym_min_op0)
2651 min = build_symbolic_expr (expr_type, sym_min_op0,
2653 else if (sym_min_op1)
2654 min = build_symbolic_expr (expr_type, sym_min_op1,
2655 neg_min_op1 ^ minus_p, min);
2657 /* Likewise for the upper bound. */
2658 if (sym_max_op0 == sym_max_op1)
2660 else if (sym_max_op0)
2661 max = build_symbolic_expr (expr_type, sym_max_op0,
2663 else if (sym_max_op1)
2664 max = build_symbolic_expr (expr_type, sym_max_op1,
2665 neg_max_op1 ^ minus_p, max);
2669 /* For other cases, for example if we have a PLUS_EXPR with two
2670 VR_ANTI_RANGEs, drop to VR_VARYING. It would take more effort
2671 to compute a precise range for such a case.
2672 ??? General even mixed range kind operations can be expressed
2673 by for example transforming ~[3, 5] + [1, 2] to range-only
2674 operations and a union primitive:
2675 [-INF, 2] + [1, 2] U [5, +INF] + [1, 2]
2676 [-INF+1, 4] U [6, +INF(OVF)]
2677 though usually the union is not exactly representable with
2678 a single range or anti-range as the above is
2679 [-INF+1, +INF(OVF)] intersected with ~[5, 5]
2680 but one could use a scheme similar to equivalences for this. */
2681 set_value_range_to_varying (vr);
2685 else if (code == MIN_EXPR
2686 || code == MAX_EXPR)
2688 if (vr0.type == VR_RANGE
2689 && !symbolic_range_p (&vr0))
2692 if (vr1.type == VR_RANGE
2693 && !symbolic_range_p (&vr1))
2695 /* For operations that make the resulting range directly
2696 proportional to the original ranges, apply the operation to
2697 the same end of each range. */
2698 min = vrp_int_const_binop (code, vr0.min, vr1.min);
2699 max = vrp_int_const_binop (code, vr0.max, vr1.max);
2701 else if (code == MIN_EXPR)
2703 min = vrp_val_min (expr_type);
2706 else if (code == MAX_EXPR)
2709 max = vrp_val_max (expr_type);
2712 else if (vr1.type == VR_RANGE
2713 && !symbolic_range_p (&vr1))
2716 if (code == MIN_EXPR)
2718 min = vrp_val_min (expr_type);
2721 else if (code == MAX_EXPR)
2724 max = vrp_val_max (expr_type);
2729 set_value_range_to_varying (vr);
2733 else if (code == MULT_EXPR)
2735 /* Fancy code so that with unsigned, [-3,-1]*[-3,-1] does not
2736 drop to varying. This test requires 2*prec bits if both
2737 operands are signed and 2*prec + 2 bits if either is not. */
2739 signop sign = TYPE_SIGN (expr_type);
2740 unsigned int prec = TYPE_PRECISION (expr_type);
2742 if (range_int_cst_p (&vr0)
2743 && range_int_cst_p (&vr1)
2744 && TYPE_OVERFLOW_WRAPS (expr_type))
2746 typedef FIXED_WIDE_INT (WIDE_INT_MAX_PRECISION * 2) vrp_int;
2747 typedef generic_wide_int
2748 <wi::extended_tree <WIDE_INT_MAX_PRECISION * 2> > vrp_int_cst;
2749 vrp_int sizem1 = wi::mask <vrp_int> (prec, false);
2750 vrp_int size = sizem1 + 1;
2752 /* Extend the values using the sign of the result to PREC2.
2753 From here on out, everthing is just signed math no matter
2754 what the input types were. */
2755 vrp_int min0 = vrp_int_cst (vr0.min);
2756 vrp_int max0 = vrp_int_cst (vr0.max);
2757 vrp_int min1 = vrp_int_cst (vr1.min);
2758 vrp_int max1 = vrp_int_cst (vr1.max);
2759 /* Canonicalize the intervals. */
2760 if (sign == UNSIGNED)
2762 if (wi::ltu_p (size, min0 + max0))
2768 if (wi::ltu_p (size, min1 + max1))
2775 vrp_int prod0 = min0 * min1;
2776 vrp_int prod1 = min0 * max1;
2777 vrp_int prod2 = max0 * min1;
2778 vrp_int prod3 = max0 * max1;
2780 /* Sort the 4 products so that min is in prod0 and max is in
2782 /* min0min1 > max0max1 */
2783 if (wi::gts_p (prod0, prod3))
2784 std::swap (prod0, prod3);
2786 /* min0max1 > max0min1 */
2787 if (wi::gts_p (prod1, prod2))
2788 std::swap (prod1, prod2);
2790 if (wi::gts_p (prod0, prod1))
2791 std::swap (prod0, prod1);
2793 if (wi::gts_p (prod2, prod3))
2794 std::swap (prod2, prod3);
2796 /* diff = max - min. */
2797 prod2 = prod3 - prod0;
2798 if (wi::geu_p (prod2, sizem1))
2800 /* the range covers all values. */
2801 set_value_range_to_varying (vr);
2805 /* The following should handle the wrapping and selecting
2806 VR_ANTI_RANGE for us. */
2807 min = wide_int_to_tree (expr_type, prod0);
2808 max = wide_int_to_tree (expr_type, prod3);
2809 set_and_canonicalize_value_range (vr, VR_RANGE, min, max, NULL);
2813 /* If we have an unsigned MULT_EXPR with two VR_ANTI_RANGEs,
2814 drop to VR_VARYING. It would take more effort to compute a
2815 precise range for such a case. For example, if we have
2816 op0 == 65536 and op1 == 65536 with their ranges both being
2817 ~[0,0] on a 32-bit machine, we would have op0 * op1 == 0, so
2818 we cannot claim that the product is in ~[0,0]. Note that we
2819 are guaranteed to have vr0.type == vr1.type at this
2821 if (vr0.type == VR_ANTI_RANGE
2822 && !TYPE_OVERFLOW_UNDEFINED (expr_type))
2824 set_value_range_to_varying (vr);
2828 extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1);
2831 else if (code == RSHIFT_EXPR
2832 || code == LSHIFT_EXPR)
2834 /* If we have a RSHIFT_EXPR with any shift values outside [0..prec-1],
2835 then drop to VR_VARYING. Outside of this range we get undefined
2836 behavior from the shift operation. We cannot even trust
2837 SHIFT_COUNT_TRUNCATED at this stage, because that applies to rtl
2838 shifts, and the operation at the tree level may be widened. */
2839 if (range_int_cst_p (&vr1)
2840 && compare_tree_int (vr1.min, 0) >= 0
2841 && compare_tree_int (vr1.max, TYPE_PRECISION (expr_type)) == -1)
2843 if (code == RSHIFT_EXPR)
2845 /* Even if vr0 is VARYING or otherwise not usable, we can derive
2846 useful ranges just from the shift count. E.g.
2847 x >> 63 for signed 64-bit x is always [-1, 0]. */
2848 if (vr0.type != VR_RANGE || symbolic_range_p (&vr0))
2850 vr0.type = type = VR_RANGE;
2851 vr0.min = vrp_val_min (expr_type);
2852 vr0.max = vrp_val_max (expr_type);
2854 extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1);
2857 /* We can map lshifts by constants to MULT_EXPR handling. */
2858 else if (code == LSHIFT_EXPR
2859 && range_int_cst_singleton_p (&vr1))
2861 bool saved_flag_wrapv;
2862 value_range vr1p = VR_INITIALIZER;
2863 vr1p.type = VR_RANGE;
2864 vr1p.min = (wide_int_to_tree
2866 wi::set_bit_in_zero (tree_to_shwi (vr1.min),
2867 TYPE_PRECISION (expr_type))));
2868 vr1p.max = vr1p.min;
2869 /* We have to use a wrapping multiply though as signed overflow
2870 on lshifts is implementation defined in C89. */
2871 saved_flag_wrapv = flag_wrapv;
2873 extract_range_from_binary_expr_1 (vr, MULT_EXPR, expr_type,
2875 flag_wrapv = saved_flag_wrapv;
2878 else if (code == LSHIFT_EXPR
2879 && range_int_cst_p (&vr0))
2881 int prec = TYPE_PRECISION (expr_type);
2882 int overflow_pos = prec;
2884 wide_int low_bound, high_bound;
2885 bool uns = TYPE_UNSIGNED (expr_type);
2886 bool in_bounds = false;
2891 bound_shift = overflow_pos - tree_to_shwi (vr1.max);
2892 /* If bound_shift == HOST_BITS_PER_WIDE_INT, the llshift can
2893 overflow. However, for that to happen, vr1.max needs to be
2894 zero, which means vr1 is a singleton range of zero, which
2895 means it should be handled by the previous LSHIFT_EXPR
2897 wide_int bound = wi::set_bit_in_zero (bound_shift, prec);
2898 wide_int complement = ~(bound - 1);
2903 high_bound = complement;
2904 if (wi::ltu_p (vr0.max, low_bound))
2906 /* [5, 6] << [1, 2] == [10, 24]. */
2907 /* We're shifting out only zeroes, the value increases
2911 else if (wi::ltu_p (high_bound, vr0.min))
2913 /* [0xffffff00, 0xffffffff] << [1, 2]
2914 == [0xfffffc00, 0xfffffffe]. */
2915 /* We're shifting out only ones, the value decreases
2922 /* [-1, 1] << [1, 2] == [-4, 4]. */
2923 low_bound = complement;
2925 if (wi::lts_p (vr0.max, high_bound)
2926 && wi::lts_p (low_bound, vr0.min))
2928 /* For non-negative numbers, we're shifting out only
2929 zeroes, the value increases monotonically.
2930 For negative numbers, we're shifting out only ones, the
2931 value decreases monotomically. */
2938 extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1);
2943 set_value_range_to_varying (vr);
2946 else if (code == TRUNC_DIV_EXPR
2947 || code == FLOOR_DIV_EXPR
2948 || code == CEIL_DIV_EXPR
2949 || code == EXACT_DIV_EXPR
2950 || code == ROUND_DIV_EXPR)
2952 if (vr0.type != VR_RANGE || symbolic_range_p (&vr0))
2954 /* For division, if op1 has VR_RANGE but op0 does not, something
2955 can be deduced just from that range. Say [min, max] / [4, max]
2956 gives [min / 4, max / 4] range. */
2957 if (vr1.type == VR_RANGE
2958 && !symbolic_range_p (&vr1)
2959 && range_includes_zero_p (vr1.min, vr1.max) == 0)
2961 vr0.type = type = VR_RANGE;
2962 vr0.min = vrp_val_min (expr_type);
2963 vr0.max = vrp_val_max (expr_type);
2967 set_value_range_to_varying (vr);
2972 /* For divisions, if flag_non_call_exceptions is true, we must
2973 not eliminate a division by zero. */
2974 if (cfun->can_throw_non_call_exceptions
2975 && (vr1.type != VR_RANGE
2976 || range_includes_zero_p (vr1.min, vr1.max) != 0))
2978 set_value_range_to_varying (vr);
2982 /* For divisions, if op0 is VR_RANGE, we can deduce a range
2983 even if op1 is VR_VARYING, VR_ANTI_RANGE, symbolic or can
2985 if (vr0.type == VR_RANGE
2986 && (vr1.type != VR_RANGE
2987 || range_includes_zero_p (vr1.min, vr1.max) != 0))
2989 tree zero = build_int_cst (TREE_TYPE (vr0.min), 0);
2994 if (TYPE_UNSIGNED (expr_type)
2995 || value_range_nonnegative_p (&vr1))
2997 /* For unsigned division or when divisor is known
2998 to be non-negative, the range has to cover
2999 all numbers from 0 to max for positive max
3000 and all numbers from min to 0 for negative min. */
3001 cmp = compare_values (vr0.max, zero);
3004 /* When vr0.max < 0, vr1.min != 0 and value
3005 ranges for dividend and divisor are available. */
3006 if (vr1.type == VR_RANGE
3007 && !symbolic_range_p (&vr0)
3008 && !symbolic_range_p (&vr1)
3009 && compare_values (vr1.min, zero) != 0)
3010 max = int_const_binop (code, vr0.max, vr1.min);
3014 else if (cmp == 0 || cmp == 1)
3018 cmp = compare_values (vr0.min, zero);
3021 /* For unsigned division when value ranges for dividend
3022 and divisor are available. */
3023 if (vr1.type == VR_RANGE
3024 && !symbolic_range_p (&vr0)
3025 && !symbolic_range_p (&vr1)
3026 && compare_values (vr1.max, zero) != 0)
3027 min = int_const_binop (code, vr0.min, vr1.max);
3031 else if (cmp == 0 || cmp == -1)
3038 /* Otherwise the range is -max .. max or min .. -min
3039 depending on which bound is bigger in absolute value,
3040 as the division can change the sign. */
3041 abs_extent_range (vr, vr0.min, vr0.max);
3044 if (type == VR_VARYING)
3046 set_value_range_to_varying (vr);
3050 else if (!symbolic_range_p (&vr0) && !symbolic_range_p (&vr1))
3052 extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1);
3056 else if (code == TRUNC_MOD_EXPR)
3058 if (range_is_null (&vr1))
3060 set_value_range_to_undefined (vr);
3063 /* ABS (A % B) < ABS (B) and either
3064 0 <= A % B <= A or A <= A % B <= 0. */
3066 signop sgn = TYPE_SIGN (expr_type);
3067 unsigned int prec = TYPE_PRECISION (expr_type);
3068 wide_int wmin, wmax, tmp;
3069 wide_int zero = wi::zero (prec);
3070 wide_int one = wi::one (prec);
3071 if (vr1.type == VR_RANGE && !symbolic_range_p (&vr1))
3073 wmax = wi::sub (vr1.max, one);
3076 tmp = wi::sub (wi::minus_one (prec), vr1.min);
3077 wmax = wi::smax (wmax, tmp);
3082 wmax = wi::max_value (prec, sgn);
3083 /* X % INT_MIN may be INT_MAX. */
3084 if (sgn == UNSIGNED)
3088 if (sgn == UNSIGNED)
3093 if (vr0.type == VR_RANGE && TREE_CODE (vr0.min) == INTEGER_CST)
3096 if (wi::gts_p (tmp, zero))
3098 wmin = wi::smax (wmin, tmp);
3102 if (vr0.type == VR_RANGE && TREE_CODE (vr0.max) == INTEGER_CST)
3105 if (sgn == SIGNED && wi::neg_p (tmp))
3107 wmax = wi::min (wmax, tmp, sgn);
3110 min = wide_int_to_tree (expr_type, wmin);
3111 max = wide_int_to_tree (expr_type, wmax);
3113 else if (code == BIT_AND_EXPR || code == BIT_IOR_EXPR || code == BIT_XOR_EXPR)
3115 bool int_cst_range0, int_cst_range1;
3116 wide_int may_be_nonzero0, may_be_nonzero1;
3117 wide_int must_be_nonzero0, must_be_nonzero1;
3119 int_cst_range0 = zero_nonzero_bits_from_vr (expr_type, &vr0,
3122 int_cst_range1 = zero_nonzero_bits_from_vr (expr_type, &vr1,
3127 if (code == BIT_AND_EXPR)
3129 min = wide_int_to_tree (expr_type,
3130 must_be_nonzero0 & must_be_nonzero1);
3131 wide_int wmax = may_be_nonzero0 & may_be_nonzero1;
3132 /* If both input ranges contain only negative values we can
3133 truncate the result range maximum to the minimum of the
3134 input range maxima. */
3135 if (int_cst_range0 && int_cst_range1
3136 && tree_int_cst_sgn (vr0.max) < 0
3137 && tree_int_cst_sgn (vr1.max) < 0)
3139 wmax = wi::min (wmax, vr0.max, TYPE_SIGN (expr_type));
3140 wmax = wi::min (wmax, vr1.max, TYPE_SIGN (expr_type));
3142 /* If either input range contains only non-negative values
3143 we can truncate the result range maximum to the respective
3144 maximum of the input range. */
3145 if (int_cst_range0 && tree_int_cst_sgn (vr0.min) >= 0)
3146 wmax = wi::min (wmax, vr0.max, TYPE_SIGN (expr_type));
3147 if (int_cst_range1 && tree_int_cst_sgn (vr1.min) >= 0)
3148 wmax = wi::min (wmax, vr1.max, TYPE_SIGN (expr_type));
3149 max = wide_int_to_tree (expr_type, wmax);
3150 cmp = compare_values (min, max);
3151 /* PR68217: In case of signed & sign-bit-CST should
3152 result in [-INF, 0] instead of [-INF, INF]. */
3153 if (cmp == -2 || cmp == 1)
3156 = wi::set_bit_in_zero (TYPE_PRECISION (expr_type) - 1,
3157 TYPE_PRECISION (expr_type));
3158 if (!TYPE_UNSIGNED (expr_type)
3159 && ((value_range_constant_singleton (&vr0)
3160 && !wi::cmps (vr0.min, sign_bit))
3161 || (value_range_constant_singleton (&vr1)
3162 && !wi::cmps (vr1.min, sign_bit))))
3164 min = TYPE_MIN_VALUE (expr_type);
3165 max = build_int_cst (expr_type, 0);
3169 else if (code == BIT_IOR_EXPR)
3171 max = wide_int_to_tree (expr_type,
3172 may_be_nonzero0 | may_be_nonzero1);
3173 wide_int wmin = must_be_nonzero0 | must_be_nonzero1;
3174 /* If the input ranges contain only positive values we can
3175 truncate the minimum of the result range to the maximum
3176 of the input range minima. */
3177 if (int_cst_range0 && int_cst_range1
3178 && tree_int_cst_sgn (vr0.min) >= 0
3179 && tree_int_cst_sgn (vr1.min) >= 0)
3181 wmin = wi::max (wmin, vr0.min, TYPE_SIGN (expr_type));
3182 wmin = wi::max (wmin, vr1.min, TYPE_SIGN (expr_type));
3184 /* If either input range contains only negative values
3185 we can truncate the minimum of the result range to the
3186 respective minimum range. */
3187 if (int_cst_range0 && tree_int_cst_sgn (vr0.max) < 0)
3188 wmin = wi::max (wmin, vr0.min, TYPE_SIGN (expr_type));
3189 if (int_cst_range1 && tree_int_cst_sgn (vr1.max) < 0)
3190 wmin = wi::max (wmin, vr1.min, TYPE_SIGN (expr_type));
3191 min = wide_int_to_tree (expr_type, wmin);
3193 else if (code == BIT_XOR_EXPR)
3195 wide_int result_zero_bits = ((must_be_nonzero0 & must_be_nonzero1)
3196 | ~(may_be_nonzero0 | may_be_nonzero1));
3197 wide_int result_one_bits
3198 = (must_be_nonzero0.and_not (may_be_nonzero1)
3199 | must_be_nonzero1.and_not (may_be_nonzero0));
3200 max = wide_int_to_tree (expr_type, ~result_zero_bits);
3201 min = wide_int_to_tree (expr_type, result_one_bits);
3202 /* If the range has all positive or all negative values the
3203 result is better than VARYING. */
3204 if (tree_int_cst_sgn (min) < 0
3205 || tree_int_cst_sgn (max) >= 0)
3208 max = min = NULL_TREE;
3214 /* If either MIN or MAX overflowed, then set the resulting range to
3215 VARYING. But we do accept an overflow infinity representation. */
3216 if (min == NULL_TREE
3217 || (TREE_OVERFLOW_P (min) && !is_overflow_infinity (min))
3219 || (TREE_OVERFLOW_P (max) && !is_overflow_infinity (max)))
3221 set_value_range_to_varying (vr);
3227 2) [-INF, +-INF(OVF)]
3228 3) [+-INF(OVF), +INF]
3229 4) [+-INF(OVF), +-INF(OVF)]
3230 We learn nothing when we have INF and INF(OVF) on both sides.
3231 Note that we do accept [-INF, -INF] and [+INF, +INF] without
3233 if ((vrp_val_is_min (min) || is_overflow_infinity (min))
3234 && (vrp_val_is_max (max) || is_overflow_infinity (max)))
3236 set_value_range_to_varying (vr);
3240 cmp = compare_values (min, max);
3241 if (cmp == -2 || cmp == 1)
3243 /* If the new range has its limits swapped around (MIN > MAX),
3244 then the operation caused one of them to wrap around, mark
3245 the new range VARYING. */
3246 set_value_range_to_varying (vr);
3249 set_value_range (vr, type, min, max, NULL);
3252 /* Extract range information from a binary expression OP0 CODE OP1 based on
3253 the ranges of each of its operands with resulting type EXPR_TYPE.
3254 The resulting range is stored in *VR. */
3257 extract_range_from_binary_expr (value_range *vr,
3258 enum tree_code code,
3259 tree expr_type, tree op0, tree op1)
3261 value_range vr0 = VR_INITIALIZER;
3262 value_range vr1 = VR_INITIALIZER;
3264 /* Get value ranges for each operand. For constant operands, create
3265 a new value range with the operand to simplify processing. */
3266 if (TREE_CODE (op0) == SSA_NAME)
3267 vr0 = *(get_value_range (op0));
3268 else if (is_gimple_min_invariant (op0))
3269 set_value_range_to_value (&vr0, op0, NULL);
3271 set_value_range_to_varying (&vr0);
3273 if (TREE_CODE (op1) == SSA_NAME)
3274 vr1 = *(get_value_range (op1));
3275 else if (is_gimple_min_invariant (op1))
3276 set_value_range_to_value (&vr1, op1, NULL);
3278 set_value_range_to_varying (&vr1);
3280 extract_range_from_binary_expr_1 (vr, code, expr_type, &vr0, &vr1);
3282 /* Try harder for PLUS and MINUS if the range of one operand is symbolic
3283 and based on the other operand, for example if it was deduced from a
3284 symbolic comparison. When a bound of the range of the first operand
3285 is invariant, we set the corresponding bound of the new range to INF
3286 in order to avoid recursing on the range of the second operand. */
3287 if (vr->type == VR_VARYING
3288 && (code == PLUS_EXPR || code == MINUS_EXPR)
3289 && TREE_CODE (op1) == SSA_NAME
3290 && vr0.type == VR_RANGE
3291 && symbolic_range_based_on_p (&vr0, op1))
3293 const bool minus_p = (code == MINUS_EXPR);
3294 value_range n_vr1 = VR_INITIALIZER;
3296 /* Try with VR0 and [-INF, OP1]. */
3297 if (is_gimple_min_invariant (minus_p ? vr0.max : vr0.min))
3298 set_value_range (&n_vr1, VR_RANGE, vrp_val_min (expr_type), op1, NULL);
3300 /* Try with VR0 and [OP1, +INF]. */
3301 else if (is_gimple_min_invariant (minus_p ? vr0.min : vr0.max))
3302 set_value_range (&n_vr1, VR_RANGE, op1, vrp_val_max (expr_type), NULL);
3304 /* Try with VR0 and [OP1, OP1]. */
3306 set_value_range (&n_vr1, VR_RANGE, op1, op1, NULL);
3308 extract_range_from_binary_expr_1 (vr, code, expr_type, &vr0, &n_vr1);
3311 if (vr->type == VR_VARYING
3312 && (code == PLUS_EXPR || code == MINUS_EXPR)
3313 && TREE_CODE (op0) == SSA_NAME
3314 && vr1.type == VR_RANGE
3315 && symbolic_range_based_on_p (&vr1, op0))
3317 const bool minus_p = (code == MINUS_EXPR);
3318 value_range n_vr0 = VR_INITIALIZER;
3320 /* Try with [-INF, OP0] and VR1. */
3321 if (is_gimple_min_invariant (minus_p ? vr1.max : vr1.min))
3322 set_value_range (&n_vr0, VR_RANGE, vrp_val_min (expr_type), op0, NULL);
3324 /* Try with [OP0, +INF] and VR1. */
3325 else if (is_gimple_min_invariant (minus_p ? vr1.min : vr1.max))
3326 set_value_range (&n_vr0, VR_RANGE, op0, vrp_val_max (expr_type), NULL);
3328 /* Try with [OP0, OP0] and VR1. */
3330 set_value_range (&n_vr0, VR_RANGE, op0, op0, NULL);
3332 extract_range_from_binary_expr_1 (vr, code, expr_type, &n_vr0, &vr1);
3336 /* Extract range information from a unary operation CODE based on
3337 the range of its operand *VR0 with type OP0_TYPE with resulting type TYPE.
3338 The resulting range is stored in *VR. */
3341 extract_range_from_unary_expr_1 (value_range *vr,
3342 enum tree_code code, tree type,
3343 value_range *vr0_, tree op0_type)
3345 value_range vr0 = *vr0_, vrtem0 = VR_INITIALIZER, vrtem1 = VR_INITIALIZER;
3347 /* VRP only operates on integral and pointer types. */
3348 if (!(INTEGRAL_TYPE_P (op0_type)
3349 || POINTER_TYPE_P (op0_type))
3350 || !(INTEGRAL_TYPE_P (type)
3351 || POINTER_TYPE_P (type)))
3353 set_value_range_to_varying (vr);
3357 /* If VR0 is UNDEFINED, so is the result. */
3358 if (vr0.type == VR_UNDEFINED)
3360 set_value_range_to_undefined (vr);
3364 /* Handle operations that we express in terms of others. */
3365 if (code == PAREN_EXPR || code == OBJ_TYPE_REF)
3367 /* PAREN_EXPR and OBJ_TYPE_REF are simple copies. */
3368 copy_value_range (vr, &vr0);
3371 else if (code == NEGATE_EXPR)
3373 /* -X is simply 0 - X, so re-use existing code that also handles
3374 anti-ranges fine. */
3375 value_range zero = VR_INITIALIZER;
3376 set_value_range_to_value (&zero, build_int_cst (type, 0), NULL);
3377 extract_range_from_binary_expr_1 (vr, MINUS_EXPR, type, &zero, &vr0);
3380 else if (code == BIT_NOT_EXPR)
3382 /* ~X is simply -1 - X, so re-use existing code that also handles
3383 anti-ranges fine. */
3384 value_range minusone = VR_INITIALIZER;
3385 set_value_range_to_value (&minusone, build_int_cst (type, -1), NULL);
3386 extract_range_from_binary_expr_1 (vr, MINUS_EXPR,
3387 type, &minusone, &vr0);
3391 /* Now canonicalize anti-ranges to ranges when they are not symbolic
3392 and express op ~[] as (op []') U (op []''). */
3393 if (vr0.type == VR_ANTI_RANGE
3394 && ranges_from_anti_range (&vr0, &vrtem0, &vrtem1))
3396 extract_range_from_unary_expr_1 (vr, code, type, &vrtem0, op0_type);
3397 if (vrtem1.type != VR_UNDEFINED)
3399 value_range vrres = VR_INITIALIZER;
3400 extract_range_from_unary_expr_1 (&vrres, code, type,
3402 vrp_meet (vr, &vrres);
3407 if (CONVERT_EXPR_CODE_P (code))
3409 tree inner_type = op0_type;
3410 tree outer_type = type;
3412 /* If the expression evaluates to a pointer, we are only interested in
3413 determining if it evaluates to NULL [0, 0] or non-NULL (~[0, 0]). */
3414 if (POINTER_TYPE_P (type))
3416 if (range_is_nonnull (&vr0))
3417 set_value_range_to_nonnull (vr, type);
3418 else if (range_is_null (&vr0))
3419 set_value_range_to_null (vr, type);
3421 set_value_range_to_varying (vr);
3425 /* If VR0 is varying and we increase the type precision, assume
3426 a full range for the following transformation. */
3427 if (vr0.type == VR_VARYING
3428 && INTEGRAL_TYPE_P (inner_type)
3429 && TYPE_PRECISION (inner_type) < TYPE_PRECISION (outer_type))
3431 vr0.type = VR_RANGE;
3432 vr0.min = TYPE_MIN_VALUE (inner_type);
3433 vr0.max = TYPE_MAX_VALUE (inner_type);
3436 /* If VR0 is a constant range or anti-range and the conversion is
3437 not truncating we can convert the min and max values and
3438 canonicalize the resulting range. Otherwise we can do the
3439 conversion if the size of the range is less than what the
3440 precision of the target type can represent and the range is
3441 not an anti-range. */
3442 if ((vr0.type == VR_RANGE
3443 || vr0.type == VR_ANTI_RANGE)
3444 && TREE_CODE (vr0.min) == INTEGER_CST
3445 && TREE_CODE (vr0.max) == INTEGER_CST
3446 && (!is_overflow_infinity (vr0.min)
3447 || (vr0.type == VR_RANGE
3448 && TYPE_PRECISION (outer_type) > TYPE_PRECISION (inner_type)
3449 && needs_overflow_infinity (outer_type)
3450 && supports_overflow_infinity (outer_type)))
3451 && (!is_overflow_infinity (vr0.max)
3452 || (vr0.type == VR_RANGE
3453 && TYPE_PRECISION (outer_type) > TYPE_PRECISION (inner_type)
3454 && needs_overflow_infinity (outer_type)
3455 && supports_overflow_infinity (outer_type)))
3456 && (TYPE_PRECISION (outer_type) >= TYPE_PRECISION (inner_type)
3457 || (vr0.type == VR_RANGE
3458 && integer_zerop (int_const_binop (RSHIFT_EXPR,
3459 int_const_binop (MINUS_EXPR, vr0.max, vr0.min),
3460 size_int (TYPE_PRECISION (outer_type)))))))
3462 tree new_min, new_max;
3463 if (is_overflow_infinity (vr0.min))
3464 new_min = negative_overflow_infinity (outer_type);
3466 new_min = force_fit_type (outer_type, wi::to_widest (vr0.min),
3468 if (is_overflow_infinity (vr0.max))
3469 new_max = positive_overflow_infinity (outer_type);
3471 new_max = force_fit_type (outer_type, wi::to_widest (vr0.max),
3473 set_and_canonicalize_value_range (vr, vr0.type,
3474 new_min, new_max, NULL);
3478 set_value_range_to_varying (vr);
3481 else if (code == ABS_EXPR)
3486 /* Pass through vr0 in the easy cases. */
3487 if (TYPE_UNSIGNED (type)
3488 || value_range_nonnegative_p (&vr0))
3490 copy_value_range (vr, &vr0);
3494 /* For the remaining varying or symbolic ranges we can't do anything
3496 if (vr0.type == VR_VARYING
3497 || symbolic_range_p (&vr0))
3499 set_value_range_to_varying (vr);
3503 /* -TYPE_MIN_VALUE = TYPE_MIN_VALUE with flag_wrapv so we can't get a
3505 if (!TYPE_OVERFLOW_UNDEFINED (type)
3506 && ((vr0.type == VR_RANGE
3507 && vrp_val_is_min (vr0.min))
3508 || (vr0.type == VR_ANTI_RANGE
3509 && !vrp_val_is_min (vr0.min))))
3511 set_value_range_to_varying (vr);
3515 /* ABS_EXPR may flip the range around, if the original range
3516 included negative values. */
3517 if (is_overflow_infinity (vr0.min))
3518 min = positive_overflow_infinity (type);
3519 else if (!vrp_val_is_min (vr0.min))
3520 min = fold_unary_to_constant (code, type, vr0.min);
3521 else if (!needs_overflow_infinity (type))
3522 min = TYPE_MAX_VALUE (type);
3523 else if (supports_overflow_infinity (type))
3524 min = positive_overflow_infinity (type);
3527 set_value_range_to_varying (vr);
3531 if (is_overflow_infinity (vr0.max))
3532 max = positive_overflow_infinity (type);
3533 else if (!vrp_val_is_min (vr0.max))
3534 max = fold_unary_to_constant (code, type, vr0.max);
3535 else if (!needs_overflow_infinity (type))
3536 max = TYPE_MAX_VALUE (type);
3537 else if (supports_overflow_infinity (type)
3538 /* We shouldn't generate [+INF, +INF] as set_value_range
3539 doesn't like this and ICEs. */
3540 && !is_positive_overflow_infinity (min))
3541 max = positive_overflow_infinity (type);
3544 set_value_range_to_varying (vr);
3548 cmp = compare_values (min, max);
3550 /* If a VR_ANTI_RANGEs contains zero, then we have
3551 ~[-INF, min(MIN, MAX)]. */
3552 if (vr0.type == VR_ANTI_RANGE)
3554 if (range_includes_zero_p (vr0.min, vr0.max) == 1)
3556 /* Take the lower of the two values. */
3560 /* Create ~[-INF, min (abs(MIN), abs(MAX))]
3561 or ~[-INF + 1, min (abs(MIN), abs(MAX))] when
3562 flag_wrapv is set and the original anti-range doesn't include
3563 TYPE_MIN_VALUE, remember -TYPE_MIN_VALUE = TYPE_MIN_VALUE. */
3564 if (TYPE_OVERFLOW_WRAPS (type))
3566 tree type_min_value = TYPE_MIN_VALUE (type);
3568 min = (vr0.min != type_min_value
3569 ? int_const_binop (PLUS_EXPR, type_min_value,
3570 build_int_cst (TREE_TYPE (type_min_value), 1))
3575 if (overflow_infinity_range_p (&vr0))
3576 min = negative_overflow_infinity (type);
3578 min = TYPE_MIN_VALUE (type);
3583 /* All else has failed, so create the range [0, INF], even for
3584 flag_wrapv since TYPE_MIN_VALUE is in the original
3586 vr0.type = VR_RANGE;
3587 min = build_int_cst (type, 0);
3588 if (needs_overflow_infinity (type))
3590 if (supports_overflow_infinity (type))
3591 max = positive_overflow_infinity (type);
3594 set_value_range_to_varying (vr);
3599 max = TYPE_MAX_VALUE (type);
3603 /* If the range contains zero then we know that the minimum value in the
3604 range will be zero. */
3605 else if (range_includes_zero_p (vr0.min, vr0.max) == 1)
3609 min = build_int_cst (type, 0);
3613 /* If the range was reversed, swap MIN and MAX. */
3615 std::swap (min, max);
3618 cmp = compare_values (min, max);
3619 if (cmp == -2 || cmp == 1)
3621 /* If the new range has its limits swapped around (MIN > MAX),
3622 then the operation caused one of them to wrap around, mark
3623 the new range VARYING. */
3624 set_value_range_to_varying (vr);
3627 set_value_range (vr, vr0.type, min, max, NULL);
3631 /* For unhandled operations fall back to varying. */
3632 set_value_range_to_varying (vr);
3637 /* Extract range information from a unary expression CODE OP0 based on
3638 the range of its operand with resulting type TYPE.
3639 The resulting range is stored in *VR. */
3642 extract_range_from_unary_expr (value_range *vr, enum tree_code code,
3643 tree type, tree op0)
3645 value_range vr0 = VR_INITIALIZER;
3647 /* Get value ranges for the operand. For constant operands, create
3648 a new value range with the operand to simplify processing. */
3649 if (TREE_CODE (op0) == SSA_NAME)
3650 vr0 = *(get_value_range (op0));
3651 else if (is_gimple_min_invariant (op0))
3652 set_value_range_to_value (&vr0, op0, NULL);
3654 set_value_range_to_varying (&vr0);
3656 extract_range_from_unary_expr_1 (vr, code, type, &vr0, TREE_TYPE (op0));
3660 /* Extract range information from a conditional expression STMT based on
3661 the ranges of each of its operands and the expression code. */
3664 extract_range_from_cond_expr (value_range *vr, gassign *stmt)
3667 value_range vr0 = VR_INITIALIZER;
3668 value_range vr1 = VR_INITIALIZER;
3670 /* Get value ranges for each operand. For constant operands, create
3671 a new value range with the operand to simplify processing. */
3672 op0 = gimple_assign_rhs2 (stmt);
3673 if (TREE_CODE (op0) == SSA_NAME)
3674 vr0 = *(get_value_range (op0));
3675 else if (is_gimple_min_invariant (op0))
3676 set_value_range_to_value (&vr0, op0, NULL);
3678 set_value_range_to_varying (&vr0);
3680 op1 = gimple_assign_rhs3 (stmt);
3681 if (TREE_CODE (op1) == SSA_NAME)
3682 vr1 = *(get_value_range (op1));
3683 else if (is_gimple_min_invariant (op1))
3684 set_value_range_to_value (&vr1, op1, NULL);
3686 set_value_range_to_varying (&vr1);
3688 /* The resulting value range is the union of the operand ranges */
3689 copy_value_range (vr, &vr0);
3690 vrp_meet (vr, &vr1);
3694 /* Extract range information from a comparison expression EXPR based
3695 on the range of its operand and the expression code. */
3698 extract_range_from_comparison (value_range *vr, enum tree_code code,
3699 tree type, tree op0, tree op1)
3704 val = vrp_evaluate_conditional_warnv_with_ops (code, op0, op1, false, &sop,
3707 /* A disadvantage of using a special infinity as an overflow
3708 representation is that we lose the ability to record overflow
3709 when we don't have an infinity. So we have to ignore a result
3710 which relies on overflow. */
3712 if (val && !is_overflow_infinity (val) && !sop)
3714 /* Since this expression was found on the RHS of an assignment,
3715 its type may be different from _Bool. Convert VAL to EXPR's
3717 val = fold_convert (type, val);
3718 if (is_gimple_min_invariant (val))
3719 set_value_range_to_value (vr, val, vr->equiv);
3721 set_value_range (vr, VR_RANGE, val, val, vr->equiv);
3724 /* The result of a comparison is always true or false. */
3725 set_value_range_to_truthvalue (vr, type);
3728 /* Helper function for simplify_internal_call_using_ranges and
3729 extract_range_basic. Return true if OP0 SUBCODE OP1 for
3730 SUBCODE {PLUS,MINUS,MULT}_EXPR is known to never overflow or
3731 always overflow. Set *OVF to true if it is known to always
3735 check_for_binary_op_overflow (enum tree_code subcode, tree type,
3736 tree op0, tree op1, bool *ovf)
3738 value_range vr0 = VR_INITIALIZER;
3739 value_range vr1 = VR_INITIALIZER;
3740 if (TREE_CODE (op0) == SSA_NAME)
3741 vr0 = *get_value_range (op0);
3742 else if (TREE_CODE (op0) == INTEGER_CST)
3743 set_value_range_to_value (&vr0, op0, NULL);
3745 set_value_range_to_varying (&vr0);
3747 if (TREE_CODE (op1) == SSA_NAME)
3748 vr1 = *get_value_range (op1);
3749 else if (TREE_CODE (op1) == INTEGER_CST)
3750 set_value_range_to_value (&vr1, op1, NULL);
3752 set_value_range_to_varying (&vr1);
3754 if (!range_int_cst_p (&vr0)
3755 || TREE_OVERFLOW (vr0.min)
3756 || TREE_OVERFLOW (vr0.max))
3758 vr0.min = vrp_val_min (TREE_TYPE (op0));
3759 vr0.max = vrp_val_max (TREE_TYPE (op0));
3761 if (!range_int_cst_p (&vr1)
3762 || TREE_OVERFLOW (vr1.min)
3763 || TREE_OVERFLOW (vr1.max))
3765 vr1.min = vrp_val_min (TREE_TYPE (op1));
3766 vr1.max = vrp_val_max (TREE_TYPE (op1));
3768 *ovf = arith_overflowed_p (subcode, type, vr0.min,
3769 subcode == MINUS_EXPR ? vr1.max : vr1.min);
3770 if (arith_overflowed_p (subcode, type, vr0.max,
3771 subcode == MINUS_EXPR ? vr1.min : vr1.max) != *ovf)
3773 if (subcode == MULT_EXPR)
3775 if (arith_overflowed_p (subcode, type, vr0.min, vr1.max) != *ovf
3776 || arith_overflowed_p (subcode, type, vr0.max, vr1.min) != *ovf)
3781 /* So far we found that there is an overflow on the boundaries.
3782 That doesn't prove that there is an overflow even for all values
3783 in between the boundaries. For that compute widest_int range
3784 of the result and see if it doesn't overlap the range of
3786 widest_int wmin, wmax;
3789 w[0] = wi::to_widest (vr0.min);
3790 w[1] = wi::to_widest (vr0.max);
3791 w[2] = wi::to_widest (vr1.min);
3792 w[3] = wi::to_widest (vr1.max);
3793 for (i = 0; i < 4; i++)
3799 wt = wi::add (w[i & 1], w[2 + (i & 2) / 2]);
3802 wt = wi::sub (w[i & 1], w[2 + (i & 2) / 2]);
3805 wt = wi::mul (w[i & 1], w[2 + (i & 2) / 2]);
3817 wmin = wi::smin (wmin, wt);
3818 wmax = wi::smax (wmax, wt);
3821 /* The result of op0 CODE op1 is known to be in range
3823 widest_int wtmin = wi::to_widest (vrp_val_min (type));
3824 widest_int wtmax = wi::to_widest (vrp_val_max (type));
3825 /* If all values in [wmin, wmax] are smaller than
3826 [wtmin, wtmax] or all are larger than [wtmin, wtmax],
3827 the arithmetic operation will always overflow. */
3828 if (wi::lts_p (wmax, wtmin) || wi::gts_p (wmin, wtmax))
3835 /* Try to derive a nonnegative or nonzero range out of STMT relying
3836 primarily on generic routines in fold in conjunction with range data.
3837 Store the result in *VR */
3840 extract_range_basic (value_range *vr, gimple *stmt)
3843 tree type = gimple_expr_type (stmt);
3845 if (is_gimple_call (stmt))
3848 int mini, maxi, zerov = 0, prec;
3849 enum tree_code subcode = ERROR_MARK;
3850 combined_fn cfn = gimple_call_combined_fn (stmt);
3854 case CFN_BUILT_IN_CONSTANT_P:
3855 /* If the call is __builtin_constant_p and the argument is a
3856 function parameter resolve it to false. This avoids bogus
3857 array bound warnings.
3858 ??? We could do this as early as inlining is finished. */
3859 arg = gimple_call_arg (stmt, 0);
3860 if (TREE_CODE (arg) == SSA_NAME
3861 && SSA_NAME_IS_DEFAULT_DEF (arg)
3862 && TREE_CODE (SSA_NAME_VAR (arg)) == PARM_DECL
3863 && cfun->after_inlining)
3865 set_value_range_to_null (vr, type);
3869 /* Both __builtin_ffs* and __builtin_popcount return
3873 arg = gimple_call_arg (stmt, 0);
3874 prec = TYPE_PRECISION (TREE_TYPE (arg));
3877 if (TREE_CODE (arg) == SSA_NAME)
3879 value_range *vr0 = get_value_range (arg);
3880 /* If arg is non-zero, then ffs or popcount
3882 if (((vr0->type == VR_RANGE
3883 && range_includes_zero_p (vr0->min, vr0->max) == 0)
3884 || (vr0->type == VR_ANTI_RANGE
3885 && range_includes_zero_p (vr0->min, vr0->max) == 1))
3886 && !is_overflow_infinity (vr0->min)
3887 && !is_overflow_infinity (vr0->max))
3889 /* If some high bits are known to be zero,
3890 we can decrease the maximum. */
3891 if (vr0->type == VR_RANGE
3892 && TREE_CODE (vr0->max) == INTEGER_CST
3893 && !operand_less_p (vr0->min,
3894 build_zero_cst (TREE_TYPE (vr0->min)))
3895 && !is_overflow_infinity (vr0->max))
3896 maxi = tree_floor_log2 (vr0->max) + 1;
3899 /* __builtin_parity* returns [0, 1]. */
3904 /* __builtin_c[lt]z* return [0, prec-1], except for
3905 when the argument is 0, but that is undefined behavior.
3906 On many targets where the CLZ RTL or optab value is defined
3907 for 0 the value is prec, so include that in the range
3910 arg = gimple_call_arg (stmt, 0);
3911 prec = TYPE_PRECISION (TREE_TYPE (arg));
3914 if (optab_handler (clz_optab, TYPE_MODE (TREE_TYPE (arg)))
3916 && CLZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (TREE_TYPE (arg)),
3918 /* Handle only the single common value. */
3920 /* Magic value to give up, unless vr0 proves
3923 if (TREE_CODE (arg) == SSA_NAME)
3925 value_range *vr0 = get_value_range (arg);
3926 /* From clz of VR_RANGE minimum we can compute
3928 if (vr0->type == VR_RANGE
3929 && TREE_CODE (vr0->min) == INTEGER_CST
3930 && !is_overflow_infinity (vr0->min))
3932 maxi = prec - 1 - tree_floor_log2 (vr0->min);
3936 else if (vr0->type == VR_ANTI_RANGE
3937 && integer_zerop (vr0->min)
3938 && !is_overflow_infinity (vr0->min))
3945 /* From clz of VR_RANGE maximum we can compute
3947 if (vr0->type == VR_RANGE
3948 && TREE_CODE (vr0->max) == INTEGER_CST
3949 && !is_overflow_infinity (vr0->max))
3951 mini = prec - 1 - tree_floor_log2 (vr0->max);
3959 /* __builtin_ctz* return [0, prec-1], except for
3960 when the argument is 0, but that is undefined behavior.
3961 If there is a ctz optab for this mode and
3962 CTZ_DEFINED_VALUE_AT_ZERO, include that in the range,
3963 otherwise just assume 0 won't be seen. */
3965 arg = gimple_call_arg (stmt, 0);
3966 prec = TYPE_PRECISION (TREE_TYPE (arg));
3969 if (optab_handler (ctz_optab, TYPE_MODE (TREE_TYPE (arg)))
3971 && CTZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (TREE_TYPE (arg)),
3974 /* Handle only the two common values. */
3977 else if (zerov == prec)
3980 /* Magic value to give up, unless vr0 proves
3984 if (TREE_CODE (arg) == SSA_NAME)
3986 value_range *vr0 = get_value_range (arg);
3987 /* If arg is non-zero, then use [0, prec - 1]. */
3988 if (((vr0->type == VR_RANGE
3989 && integer_nonzerop (vr0->min))
3990 || (vr0->type == VR_ANTI_RANGE
3991 && integer_zerop (vr0->min)))
3992 && !is_overflow_infinity (vr0->min))
3997 /* If some high bits are known to be zero,
3998 we can decrease the result maximum. */
3999 if (vr0->type == VR_RANGE
4000 && TREE_CODE (vr0->max) == INTEGER_CST
4001 && !is_overflow_infinity (vr0->max))
4003 maxi = tree_floor_log2 (vr0->max);
4004 /* For vr0 [0, 0] give up. */
4012 /* __builtin_clrsb* returns [0, prec-1]. */
4014 arg = gimple_call_arg (stmt, 0);
4015 prec = TYPE_PRECISION (TREE_TYPE (arg));
4020 set_value_range (vr, VR_RANGE, build_int_cst (type, mini),
4021 build_int_cst (type, maxi), NULL);
4023 case CFN_UBSAN_CHECK_ADD:
4024 subcode = PLUS_EXPR;
4026 case CFN_UBSAN_CHECK_SUB:
4027 subcode = MINUS_EXPR;
4029 case CFN_UBSAN_CHECK_MUL:
4030 subcode = MULT_EXPR;
4032 case CFN_GOACC_DIM_SIZE:
4033 case CFN_GOACC_DIM_POS:
4034 /* Optimizing these two internal functions helps the loop
4035 optimizer eliminate outer comparisons. Size is [1,N]
4036 and pos is [0,N-1]. */
4038 bool is_pos = cfn == CFN_GOACC_DIM_POS;
4039 int axis = get_oacc_ifn_dim_arg (stmt);
4040 int size = get_oacc_fn_dim_size (current_function_decl, axis);
4043 /* If it's dynamic, the backend might know a hardware
4045 size = targetm.goacc.dim_limit (axis);
4047 tree type = TREE_TYPE (gimple_call_lhs (stmt));
4048 set_value_range (vr, VR_RANGE,
4049 build_int_cst (type, is_pos ? 0 : 1),
4050 size ? build_int_cst (type, size - is_pos)
4051 : vrp_val_max (type), NULL);
4057 if (subcode != ERROR_MARK)
4059 bool saved_flag_wrapv = flag_wrapv;
4060 /* Pretend the arithmetics is wrapping. If there is
4061 any overflow, we'll complain, but will actually do
4062 wrapping operation. */
4064 extract_range_from_binary_expr (vr, subcode, type,
4065 gimple_call_arg (stmt, 0),
4066 gimple_call_arg (stmt, 1));
4067 flag_wrapv = saved_flag_wrapv;
4069 /* If for both arguments vrp_valueize returned non-NULL,
4070 this should have been already folded and if not, it
4071 wasn't folded because of overflow. Avoid removing the
4072 UBSAN_CHECK_* calls in that case. */
4073 if (vr->type == VR_RANGE
4074 && (vr->min == vr->max
4075 || operand_equal_p (vr->min, vr->max, 0)))
4076 set_value_range_to_varying (vr);
4080 /* Handle extraction of the two results (result of arithmetics and
4081 a flag whether arithmetics overflowed) from {ADD,SUB,MUL}_OVERFLOW
4082 internal function. */
4083 else if (is_gimple_assign (stmt)
4084 && (gimple_assign_rhs_code (stmt) == REALPART_EXPR
4085 || gimple_assign_rhs_code (stmt) == IMAGPART_EXPR)
4086 && INTEGRAL_TYPE_P (type))
4088 enum tree_code code = gimple_assign_rhs_code (stmt);
4089 tree op = gimple_assign_rhs1 (stmt);
4090 if (TREE_CODE (op) == code && TREE_CODE (TREE_OPERAND (op, 0)) == SSA_NAME)
4092 gimple *g = SSA_NAME_DEF_STMT (TREE_OPERAND (op, 0));
4093 if (is_gimple_call (g) && gimple_call_internal_p (g))
4095 enum tree_code subcode = ERROR_MARK;
4096 switch (gimple_call_internal_fn (g))
4098 case IFN_ADD_OVERFLOW:
4099 subcode = PLUS_EXPR;
4101 case IFN_SUB_OVERFLOW:
4102 subcode = MINUS_EXPR;
4104 case IFN_MUL_OVERFLOW:
4105 subcode = MULT_EXPR;
4110 if (subcode != ERROR_MARK)
4112 tree op0 = gimple_call_arg (g, 0);
4113 tree op1 = gimple_call_arg (g, 1);
4114 if (code == IMAGPART_EXPR)
4117 if (check_for_binary_op_overflow (subcode, type,
4119 set_value_range_to_value (vr,
4120 build_int_cst (type, ovf),
4123 set_value_range (vr, VR_RANGE, build_int_cst (type, 0),
4124 build_int_cst (type, 1), NULL);
4126 else if (types_compatible_p (type, TREE_TYPE (op0))
4127 && types_compatible_p (type, TREE_TYPE (op1)))
4129 bool saved_flag_wrapv = flag_wrapv;
4130 /* Pretend the arithmetics is wrapping. If there is
4131 any overflow, IMAGPART_EXPR will be set. */
4133 extract_range_from_binary_expr (vr, subcode, type,
4135 flag_wrapv = saved_flag_wrapv;
4139 value_range vr0 = VR_INITIALIZER;
4140 value_range vr1 = VR_INITIALIZER;
4141 bool saved_flag_wrapv = flag_wrapv;
4142 /* Pretend the arithmetics is wrapping. If there is
4143 any overflow, IMAGPART_EXPR will be set. */
4145 extract_range_from_unary_expr (&vr0, NOP_EXPR,
4147 extract_range_from_unary_expr (&vr1, NOP_EXPR,
4149 extract_range_from_binary_expr_1 (vr, subcode, type,
4151 flag_wrapv = saved_flag_wrapv;
4158 if (INTEGRAL_TYPE_P (type)
4159 && gimple_stmt_nonnegative_warnv_p (stmt, &sop))
4160 set_value_range_to_nonnegative (vr, type,
4161 sop || stmt_overflow_infinity (stmt));
4162 else if (vrp_stmt_computes_nonzero (stmt, &sop)
4164 set_value_range_to_nonnull (vr, type);
4166 set_value_range_to_varying (vr);
4170 /* Try to compute a useful range out of assignment STMT and store it
4174 extract_range_from_assignment (value_range *vr, gassign *stmt)
4176 enum tree_code code = gimple_assign_rhs_code (stmt);
4178 if (code == ASSERT_EXPR)
4179 extract_range_from_assert (vr, gimple_assign_rhs1 (stmt));
4180 else if (code == SSA_NAME)
4181 extract_range_from_ssa_name (vr, gimple_assign_rhs1 (stmt));
4182 else if (TREE_CODE_CLASS (code) == tcc_binary)
4183 extract_range_from_binary_expr (vr, gimple_assign_rhs_code (stmt),
4184 gimple_expr_type (stmt),
4185 gimple_assign_rhs1 (stmt),
4186 gimple_assign_rhs2 (stmt));
4187 else if (TREE_CODE_CLASS (code) == tcc_unary)
4188 extract_range_from_unary_expr (vr, gimple_assign_rhs_code (stmt),
4189 gimple_expr_type (stmt),
4190 gimple_assign_rhs1 (stmt));
4191 else if (code == COND_EXPR)
4192 extract_range_from_cond_expr (vr, stmt);
4193 else if (TREE_CODE_CLASS (code) == tcc_comparison)
4194 extract_range_from_comparison (vr, gimple_assign_rhs_code (stmt),
4195 gimple_expr_type (stmt),
4196 gimple_assign_rhs1 (stmt),
4197 gimple_assign_rhs2 (stmt));
4198 else if (get_gimple_rhs_class (code) == GIMPLE_SINGLE_RHS
4199 && is_gimple_min_invariant (gimple_assign_rhs1 (stmt)))
4200 set_value_range_to_value (vr, gimple_assign_rhs1 (stmt), NULL);
4202 set_value_range_to_varying (vr);
4204 if (vr->type == VR_VARYING)
4205 extract_range_basic (vr, stmt);
4208 /* Given a range VR, a LOOP and a variable VAR, determine whether it
4209 would be profitable to adjust VR using scalar evolution information
4210 for VAR. If so, update VR with the new limits. */
4213 adjust_range_with_scev (value_range *vr, struct loop *loop,
4214 gimple *stmt, tree var)
4216 tree init, step, chrec, tmin, tmax, min, max, type, tem;
4217 enum ev_direction dir;
4219 /* TODO. Don't adjust anti-ranges. An anti-range may provide
4220 better opportunities than a regular range, but I'm not sure. */
4221 if (vr->type == VR_ANTI_RANGE)
4224 chrec = instantiate_parameters (loop, analyze_scalar_evolution (loop, var));
4226 /* Like in PR19590, scev can return a constant function. */
4227 if (is_gimple_min_invariant (chrec))
4229 set_value_range_to_value (vr, chrec, vr->equiv);
4233 if (TREE_CODE (chrec) != POLYNOMIAL_CHREC)
4236 init = initial_condition_in_loop_num (chrec, loop->num);
4237 tem = op_with_constant_singleton_value_range (init);
4240 step = evolution_part_in_loop_num (chrec, loop->num);
4241 tem = op_with_constant_singleton_value_range (step);
4245 /* If STEP is symbolic, we can't know whether INIT will be the
4246 minimum or maximum value in the range. Also, unless INIT is
4247 a simple expression, compare_values and possibly other functions
4248 in tree-vrp won't be able to handle it. */
4249 if (step == NULL_TREE
4250 || !is_gimple_min_invariant (step)
4251 || !valid_value_p (init))
4254 dir = scev_direction (chrec);
4255 if (/* Do not adjust ranges if we do not know whether the iv increases
4256 or decreases, ... */
4257 dir == EV_DIR_UNKNOWN
4258 /* ... or if it may wrap. */
4259 || scev_probably_wraps_p (init, step, stmt, get_chrec_loop (chrec),
4263 /* We use TYPE_MIN_VALUE and TYPE_MAX_VALUE here instead of
4264 negative_overflow_infinity and positive_overflow_infinity,
4265 because we have concluded that the loop probably does not
4268 type = TREE_TYPE (var);
4269 if (POINTER_TYPE_P (type) || !TYPE_MIN_VALUE (type))
4270 tmin = lower_bound_in_type (type, type);
4272 tmin = TYPE_MIN_VALUE (type);
4273 if (POINTER_TYPE_P (type) || !TYPE_MAX_VALUE (type))
4274 tmax = upper_bound_in_type (type, type);
4276 tmax = TYPE_MAX_VALUE (type);
4278 /* Try to use estimated number of iterations for the loop to constrain the
4279 final value in the evolution. */
4280 if (TREE_CODE (step) == INTEGER_CST
4281 && is_gimple_val (init)
4282 && (TREE_CODE (init) != SSA_NAME
4283 || get_value_range (init)->type == VR_RANGE))
4287 /* We are only entering here for loop header PHI nodes, so using
4288 the number of latch executions is the correct thing to use. */
4289 if (max_loop_iterations (loop, &nit))
4291 value_range maxvr = VR_INITIALIZER;
4292 signop sgn = TYPE_SIGN (TREE_TYPE (step));
4295 widest_int wtmp = wi::mul (wi::to_widest (step), nit, sgn,
4297 /* If the multiplication overflowed we can't do a meaningful
4298 adjustment. Likewise if the result doesn't fit in the type
4299 of the induction variable. For a signed type we have to
4300 check whether the result has the expected signedness which
4301 is that of the step as number of iterations is unsigned. */
4303 && wi::fits_to_tree_p (wtmp, TREE_TYPE (init))
4305 || wi::gts_p (wtmp, 0) == wi::gts_p (step, 0)))
4307 tem = wide_int_to_tree (TREE_TYPE (init), wtmp);
4308 extract_range_from_binary_expr (&maxvr, PLUS_EXPR,
4309 TREE_TYPE (init), init, tem);
4310 /* Likewise if the addition did. */
4311 if (maxvr.type == VR_RANGE)
4313 value_range initvr = VR_INITIALIZER;
4315 if (TREE_CODE (init) == SSA_NAME)
4316 initvr = *(get_value_range (init));
4317 else if (is_gimple_min_invariant (init))
4318 set_value_range_to_value (&initvr, init, NULL);
4322 /* Check if init + nit * step overflows. Though we checked
4323 scev {init, step}_loop doesn't wrap, it is not enough
4324 because the loop may exit immediately. Overflow could
4325 happen in the plus expression in this case. */
4326 if ((dir == EV_DIR_DECREASES
4327 && (is_negative_overflow_infinity (maxvr.min)
4328 || compare_values (maxvr.min, initvr.min) != -1))
4329 || (dir == EV_DIR_GROWS
4330 && (is_positive_overflow_infinity (maxvr.max)
4331 || compare_values (maxvr.max, initvr.max) != 1)))
4341 if (vr->type == VR_VARYING || vr->type == VR_UNDEFINED)
4346 /* For VARYING or UNDEFINED ranges, just about anything we get
4347 from scalar evolutions should be better. */
4349 if (dir == EV_DIR_DECREASES)
4354 else if (vr->type == VR_RANGE)
4359 if (dir == EV_DIR_DECREASES)
4361 /* INIT is the maximum value. If INIT is lower than VR->MAX
4362 but no smaller than VR->MIN, set VR->MAX to INIT. */
4363 if (compare_values (init, max) == -1)
4366 /* According to the loop information, the variable does not
4367 overflow. If we think it does, probably because of an
4368 overflow due to arithmetic on a different INF value,
4370 if (is_negative_overflow_infinity (min)
4371 || compare_values (min, tmin) == -1)
4377 /* If INIT is bigger than VR->MIN, set VR->MIN to INIT. */
4378 if (compare_values (init, min) == 1)
4381 if (is_positive_overflow_infinity (max)
4382 || compare_values (tmax, max) == -1)
4389 /* If we just created an invalid range with the minimum
4390 greater than the maximum, we fail conservatively.
4391 This should happen only in unreachable
4392 parts of code, or for invalid programs. */
4393 if (compare_values (min, max) == 1
4394 || (is_negative_overflow_infinity (min)
4395 && is_positive_overflow_infinity (max)))
4398 /* Even for valid range info, sometimes overflow flag will leak in.
4399 As GIMPLE IL should have no constants with TREE_OVERFLOW set, we
4400 drop them except for +-overflow_infinity which still need special
4401 handling in vrp pass. */
4402 if (TREE_OVERFLOW_P (min)
4403 && ! is_negative_overflow_infinity (min))
4404 min = drop_tree_overflow (min);
4405 if (TREE_OVERFLOW_P (max)
4406 && ! is_positive_overflow_infinity (max))
4407 max = drop_tree_overflow (max);
4409 set_value_range (vr, VR_RANGE, min, max, vr->equiv);
4413 /* Given two numeric value ranges VR0, VR1 and a comparison code COMP:
4415 - Return BOOLEAN_TRUE_NODE if VR0 COMP VR1 always returns true for
4416 all the values in the ranges.
4418 - Return BOOLEAN_FALSE_NODE if the comparison always returns false.
4420 - Return NULL_TREE if it is not always possible to determine the
4421 value of the comparison.
4423 Also set *STRICT_OVERFLOW_P to indicate whether a range with an
4424 overflow infinity was used in the test. */
4428 compare_ranges (enum tree_code comp, value_range *vr0, value_range *vr1,
4429 bool *strict_overflow_p)
4431 /* VARYING or UNDEFINED ranges cannot be compared. */
4432 if (vr0->type == VR_VARYING
4433 || vr0->type == VR_UNDEFINED
4434 || vr1->type == VR_VARYING
4435 || vr1->type == VR_UNDEFINED)
4438 /* Anti-ranges need to be handled separately. */
4439 if (vr0->type == VR_ANTI_RANGE || vr1->type == VR_ANTI_RANGE)
4441 /* If both are anti-ranges, then we cannot compute any
4443 if (vr0->type == VR_ANTI_RANGE && vr1->type == VR_ANTI_RANGE)
4446 /* These comparisons are never statically computable. */
4453 /* Equality can be computed only between a range and an
4454 anti-range. ~[VAL1, VAL2] == [VAL1, VAL2] is always false. */
4455 if (vr0->type == VR_RANGE)
4457 /* To simplify processing, make VR0 the anti-range. */
4458 value_range *tmp = vr0;
4463 gcc_assert (comp == NE_EXPR || comp == EQ_EXPR);
4465 if (compare_values_warnv (vr0->min, vr1->min, strict_overflow_p) == 0
4466 && compare_values_warnv (vr0->max, vr1->max, strict_overflow_p) == 0)
4467 return (comp == NE_EXPR) ? boolean_true_node : boolean_false_node;
4472 if (!usable_range_p (vr0, strict_overflow_p)
4473 || !usable_range_p (vr1, strict_overflow_p))
4476 /* Simplify processing. If COMP is GT_EXPR or GE_EXPR, switch the
4477 operands around and change the comparison code. */
4478 if (comp == GT_EXPR || comp == GE_EXPR)
4480 comp = (comp == GT_EXPR) ? LT_EXPR : LE_EXPR;
4481 std::swap (vr0, vr1);
4484 if (comp == EQ_EXPR)
4486 /* Equality may only be computed if both ranges represent
4487 exactly one value. */
4488 if (compare_values_warnv (vr0->min, vr0->max, strict_overflow_p) == 0
4489 && compare_values_warnv (vr1->min, vr1->max, strict_overflow_p) == 0)
4491 int cmp_min = compare_values_warnv (vr0->min, vr1->min,
4493 int cmp_max = compare_values_warnv (vr0->max, vr1->max,
4495 if (cmp_min == 0 && cmp_max == 0)
4496 return boolean_true_node;
4497 else if (cmp_min != -2 && cmp_max != -2)
4498 return boolean_false_node;
4500 /* If [V0_MIN, V1_MAX] < [V1_MIN, V1_MAX] then V0 != V1. */
4501 else if (compare_values_warnv (vr0->min, vr1->max,
4502 strict_overflow_p) == 1
4503 || compare_values_warnv (vr1->min, vr0->max,
4504 strict_overflow_p) == 1)
4505 return boolean_false_node;
4509 else if (comp == NE_EXPR)
4513 /* If VR0 is completely to the left or completely to the right
4514 of VR1, they are always different. Notice that we need to
4515 make sure that both comparisons yield similar results to
4516 avoid comparing values that cannot be compared at
4518 cmp1 = compare_values_warnv (vr0->max, vr1->min, strict_overflow_p);
4519 cmp2 = compare_values_warnv (vr0->min, vr1->max, strict_overflow_p);
4520 if ((cmp1 == -1 && cmp2 == -1) || (cmp1 == 1 && cmp2 == 1))
4521 return boolean_true_node;
4523 /* If VR0 and VR1 represent a single value and are identical,
4525 else if (compare_values_warnv (vr0->min, vr0->max,
4526 strict_overflow_p) == 0
4527 && compare_values_warnv (vr1->min, vr1->max,
4528 strict_overflow_p) == 0
4529 && compare_values_warnv (vr0->min, vr1->min,
4530 strict_overflow_p) == 0
4531 && compare_values_warnv (vr0->max, vr1->max,
4532 strict_overflow_p) == 0)
4533 return boolean_false_node;
4535 /* Otherwise, they may or may not be different. */
4539 else if (comp == LT_EXPR || comp == LE_EXPR)
4543 /* If VR0 is to the left of VR1, return true. */
4544 tst = compare_values_warnv (vr0->max, vr1->min, strict_overflow_p);
4545 if ((comp == LT_EXPR && tst == -1)
4546 || (comp == LE_EXPR && (tst == -1 || tst == 0)))
4548 if (overflow_infinity_range_p (vr0)
4549 || overflow_infinity_range_p (vr1))
4550 *strict_overflow_p = true;
4551 return boolean_true_node;
4554 /* If VR0 is to the right of VR1, return false. */
4555 tst = compare_values_warnv (vr0->min, vr1->max, strict_overflow_p);
4556 if ((comp == LT_EXPR && (tst == 0 || tst == 1))
4557 || (comp == LE_EXPR && tst == 1))
4559 if (overflow_infinity_range_p (vr0)
4560 || overflow_infinity_range_p (vr1))
4561 *strict_overflow_p = true;
4562 return boolean_false_node;
4565 /* Otherwise, we don't know. */
4573 /* Given a value range VR, a value VAL and a comparison code COMP, return
4574 BOOLEAN_TRUE_NODE if VR COMP VAL always returns true for all the
4575 values in VR. Return BOOLEAN_FALSE_NODE if the comparison
4576 always returns false. Return NULL_TREE if it is not always
4577 possible to determine the value of the comparison. Also set
4578 *STRICT_OVERFLOW_P to indicate whether a range with an overflow
4579 infinity was used in the test. */
4582 compare_range_with_value (enum tree_code comp, value_range *vr, tree val,
4583 bool *strict_overflow_p)
4585 if (vr->type == VR_VARYING || vr->type == VR_UNDEFINED)
4588 /* Anti-ranges need to be handled separately. */
4589 if (vr->type == VR_ANTI_RANGE)
4591 /* For anti-ranges, the only predicates that we can compute at
4592 compile time are equality and inequality. */
4599 /* ~[VAL_1, VAL_2] OP VAL is known if VAL_1 <= VAL <= VAL_2. */
4600 if (value_inside_range (val, vr->min, vr->max) == 1)
4601 return (comp == NE_EXPR) ? boolean_true_node : boolean_false_node;
4606 if (!usable_range_p (vr, strict_overflow_p))
4609 if (comp == EQ_EXPR)
4611 /* EQ_EXPR may only be computed if VR represents exactly
4613 if (compare_values_warnv (vr->min, vr->max, strict_overflow_p) == 0)
4615 int cmp = compare_values_warnv (vr->min, val, strict_overflow_p);
4617 return boolean_true_node;
4618 else if (cmp == -1 || cmp == 1 || cmp == 2)
4619 return boolean_false_node;
4621 else if (compare_values_warnv (val, vr->min, strict_overflow_p) == -1
4622 || compare_values_warnv (vr->max, val, strict_overflow_p) == -1)
4623 return boolean_false_node;
4627 else if (comp == NE_EXPR)
4629 /* If VAL is not inside VR, then they are always different. */
4630 if (compare_values_warnv (vr->max, val, strict_overflow_p) == -1
4631 || compare_values_warnv (vr->min, val, strict_overflow_p) == 1)
4632 return boolean_true_node;
4634 /* If VR represents exactly one value equal to VAL, then return
4636 if (compare_values_warnv (vr->min, vr->max, strict_overflow_p) == 0
4637 && compare_values_warnv (vr->min, val, strict_overflow_p) == 0)
4638 return boolean_false_node;
4640 /* Otherwise, they may or may not be different. */
4643 else if (comp == LT_EXPR || comp == LE_EXPR)
4647 /* If VR is to the left of VAL, return true. */
4648 tst = compare_values_warnv (vr->max, val, strict_overflow_p);
4649 if ((comp == LT_EXPR && tst == -1)
4650 || (comp == LE_EXPR && (tst == -1 || tst == 0)))
4652 if (overflow_infinity_range_p (vr))
4653 *strict_overflow_p = true;
4654 return boolean_true_node;
4657 /* If VR is to the right of VAL, return false. */
4658 tst = compare_values_warnv (vr->min, val, strict_overflow_p);
4659 if ((comp == LT_EXPR && (tst == 0 || tst == 1))
4660 || (comp == LE_EXPR && tst == 1))
4662 if (overflow_infinity_range_p (vr))
4663 *strict_overflow_p = true;
4664 return boolean_false_node;
4667 /* Otherwise, we don't know. */
4670 else if (comp == GT_EXPR || comp == GE_EXPR)
4674 /* If VR is to the right of VAL, return true. */
4675 tst = compare_values_warnv (vr->min, val, strict_overflow_p);
4676 if ((comp == GT_EXPR && tst == 1)
4677 || (comp == GE_EXPR && (tst == 0 || tst == 1)))
4679 if (overflow_infinity_range_p (vr))
4680 *strict_overflow_p = true;
4681 return boolean_true_node;
4684 /* If VR is to the left of VAL, return false. */
4685 tst = compare_values_warnv (vr->max, val, strict_overflow_p);
4686 if ((comp == GT_EXPR && (tst == -1 || tst == 0))
4687 || (comp == GE_EXPR && tst == -1))
4689 if (overflow_infinity_range_p (vr))
4690 *strict_overflow_p = true;
4691 return boolean_false_node;
4694 /* Otherwise, we don't know. */
4702 /* Debugging dumps. */
4704 void dump_value_range (FILE *, value_range *);
4705 void debug_value_range (value_range *);
4706 void dump_all_value_ranges (FILE *);
4707 void debug_all_value_ranges (void);
4708 void dump_vr_equiv (FILE *, bitmap);
4709 void debug_vr_equiv (bitmap);
4712 /* Dump value range VR to FILE. */
4715 dump_value_range (FILE *file, value_range *vr)
4718 fprintf (file, "[]");
4719 else if (vr->type == VR_UNDEFINED)
4720 fprintf (file, "UNDEFINED");
4721 else if (vr->type == VR_RANGE || vr->type == VR_ANTI_RANGE)
4723 tree type = TREE_TYPE (vr->min);
4725 fprintf (file, "%s[", (vr->type == VR_ANTI_RANGE) ? "~" : "");
4727 if (is_negative_overflow_infinity (vr->min))
4728 fprintf (file, "-INF(OVF)");
4729 else if (INTEGRAL_TYPE_P (type)
4730 && !TYPE_UNSIGNED (type)
4731 && vrp_val_is_min (vr->min))
4732 fprintf (file, "-INF");
4734 print_generic_expr (file, vr->min, 0);
4736 fprintf (file, ", ");
4738 if (is_positive_overflow_infinity (vr->max))
4739 fprintf (file, "+INF(OVF)");
4740 else if (INTEGRAL_TYPE_P (type)
4741 && vrp_val_is_max (vr->max))
4742 fprintf (file, "+INF");
4744 print_generic_expr (file, vr->max, 0);
4746 fprintf (file, "]");
4753 fprintf (file, " EQUIVALENCES: { ");
4755 EXECUTE_IF_SET_IN_BITMAP (vr->equiv, 0, i, bi)
4757 print_generic_expr (file, ssa_name (i), 0);
4758 fprintf (file, " ");
4762 fprintf (file, "} (%u elements)", c);
4765 else if (vr->type == VR_VARYING)
4766 fprintf (file, "VARYING");
4768 fprintf (file, "INVALID RANGE");
4772 /* Dump value range VR to stderr. */
4775 debug_value_range (value_range *vr)
4777 dump_value_range (stderr, vr);
4778 fprintf (stderr, "\n");
4782 /* Dump value ranges of all SSA_NAMEs to FILE. */
4785 dump_all_value_ranges (FILE *file)
4789 for (i = 0; i < num_vr_values; i++)
4793 print_generic_expr (file, ssa_name (i), 0);
4794 fprintf (file, ": ");
4795 dump_value_range (file, vr_value[i]);
4796 fprintf (file, "\n");
4800 fprintf (file, "\n");
4804 /* Dump all value ranges to stderr. */
4807 debug_all_value_ranges (void)
4809 dump_all_value_ranges (stderr);
4813 /* Given a COND_EXPR COND of the form 'V OP W', and an SSA name V,
4814 create a new SSA name N and return the assertion assignment
4815 'N = ASSERT_EXPR <V, V OP W>'. */
4818 build_assert_expr_for (tree cond, tree v)
4823 gcc_assert (TREE_CODE (v) == SSA_NAME
4824 && COMPARISON_CLASS_P (cond));
4826 a = build2 (ASSERT_EXPR, TREE_TYPE (v), v, cond);
4827 assertion = gimple_build_assign (NULL_TREE, a);
4829 /* The new ASSERT_EXPR, creates a new SSA name that replaces the
4830 operand of the ASSERT_EXPR. Create it so the new name and the old one
4831 are registered in the replacement table so that we can fix the SSA web
4832 after adding all the ASSERT_EXPRs. */
4833 create_new_def_for (v, assertion, NULL);
4839 /* Return false if EXPR is a predicate expression involving floating
4843 fp_predicate (gimple *stmt)
4845 GIMPLE_CHECK (stmt, GIMPLE_COND);
4847 return FLOAT_TYPE_P (TREE_TYPE (gimple_cond_lhs (stmt)));
4850 /* If the range of values taken by OP can be inferred after STMT executes,
4851 return the comparison code (COMP_CODE_P) and value (VAL_P) that
4852 describes the inferred range. Return true if a range could be
4856 infer_value_range (gimple *stmt, tree op, tree_code *comp_code_p, tree *val_p)
4859 *comp_code_p = ERROR_MARK;
4861 /* Do not attempt to infer anything in names that flow through
4863 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op))
4866 /* Similarly, don't infer anything from statements that may throw
4867 exceptions. ??? Relax this requirement? */
4868 if (stmt_could_throw_p (stmt))
4871 /* If STMT is the last statement of a basic block with no normal
4872 successors, there is no point inferring anything about any of its
4873 operands. We would not be able to find a proper insertion point
4874 for the assertion, anyway. */
4875 if (stmt_ends_bb_p (stmt))
4880 FOR_EACH_EDGE (e, ei, gimple_bb (stmt)->succs)
4881 if (!(e->flags & EDGE_ABNORMAL))
4887 if (infer_nonnull_range (stmt, op))
4889 *val_p = build_int_cst (TREE_TYPE (op), 0);
4890 *comp_code_p = NE_EXPR;
4898 void dump_asserts_for (FILE *, tree);
4899 void debug_asserts_for (tree);
4900 void dump_all_asserts (FILE *);
4901 void debug_all_asserts (void);
4903 /* Dump all the registered assertions for NAME to FILE. */
4906 dump_asserts_for (FILE *file, tree name)
4910 fprintf (file, "Assertions to be inserted for ");
4911 print_generic_expr (file, name, 0);
4912 fprintf (file, "\n");
4914 loc = asserts_for[SSA_NAME_VERSION (name)];
4917 fprintf (file, "\t");
4918 print_gimple_stmt (file, gsi_stmt (loc->si), 0, 0);
4919 fprintf (file, "\n\tBB #%d", loc->bb->index);
4922 fprintf (file, "\n\tEDGE %d->%d", loc->e->src->index,
4923 loc->e->dest->index);
4924 dump_edge_info (file, loc->e, dump_flags, 0);
4926 fprintf (file, "\n\tPREDICATE: ");
4927 print_generic_expr (file, name, 0);
4928 fprintf (file, " %s ", get_tree_code_name (loc->comp_code));
4929 print_generic_expr (file, loc->val, 0);
4930 fprintf (file, "\n\n");
4934 fprintf (file, "\n");
4938 /* Dump all the registered assertions for NAME to stderr. */
4941 debug_asserts_for (tree name)
4943 dump_asserts_for (stderr, name);
4947 /* Dump all the registered assertions for all the names to FILE. */
4950 dump_all_asserts (FILE *file)
4955 fprintf (file, "\nASSERT_EXPRs to be inserted\n\n");
4956 EXECUTE_IF_SET_IN_BITMAP (need_assert_for, 0, i, bi)
4957 dump_asserts_for (file, ssa_name (i));
4958 fprintf (file, "\n");
4962 /* Dump all the registered assertions for all the names to stderr. */
4965 debug_all_asserts (void)
4967 dump_all_asserts (stderr);
4971 /* If NAME doesn't have an ASSERT_EXPR registered for asserting
4972 'EXPR COMP_CODE VAL' at a location that dominates block BB or
4973 E->DEST, then register this location as a possible insertion point
4974 for ASSERT_EXPR <NAME, EXPR COMP_CODE VAL>.
4976 BB, E and SI provide the exact insertion point for the new
4977 ASSERT_EXPR. If BB is NULL, then the ASSERT_EXPR is to be inserted
4978 on edge E. Otherwise, if E is NULL, the ASSERT_EXPR is inserted on
4979 BB. If SI points to a COND_EXPR or a SWITCH_EXPR statement, then E
4980 must not be NULL. */
4983 register_new_assert_for (tree name, tree expr,
4984 enum tree_code comp_code,
4988 gimple_stmt_iterator si)
4990 assert_locus *n, *loc, *last_loc;
4991 basic_block dest_bb;
4993 gcc_checking_assert (bb == NULL || e == NULL);
4996 gcc_checking_assert (gimple_code (gsi_stmt (si)) != GIMPLE_COND
4997 && gimple_code (gsi_stmt (si)) != GIMPLE_SWITCH);
4999 /* Never build an assert comparing against an integer constant with
5000 TREE_OVERFLOW set. This confuses our undefined overflow warning
5002 if (TREE_OVERFLOW_P (val))
5003 val = drop_tree_overflow (val);
5005 /* The new assertion A will be inserted at BB or E. We need to
5006 determine if the new location is dominated by a previously
5007 registered location for A. If we are doing an edge insertion,
5008 assume that A will be inserted at E->DEST. Note that this is not
5011 If E is a critical edge, it will be split. But even if E is
5012 split, the new block will dominate the same set of blocks that
5015 The reverse, however, is not true, blocks dominated by E->DEST
5016 will not be dominated by the new block created to split E. So,
5017 if the insertion location is on a critical edge, we will not use
5018 the new location to move another assertion previously registered
5019 at a block dominated by E->DEST. */
5020 dest_bb = (bb) ? bb : e->dest;
5022 /* If NAME already has an ASSERT_EXPR registered for COMP_CODE and
5023 VAL at a block dominating DEST_BB, then we don't need to insert a new
5024 one. Similarly, if the same assertion already exists at a block
5025 dominated by DEST_BB and the new location is not on a critical
5026 edge, then update the existing location for the assertion (i.e.,
5027 move the assertion up in the dominance tree).
5029 Note, this is implemented as a simple linked list because there
5030 should not be more than a handful of assertions registered per
5031 name. If this becomes a performance problem, a table hashed by
5032 COMP_CODE and VAL could be implemented. */
5033 loc = asserts_for[SSA_NAME_VERSION (name)];
5037 if (loc->comp_code == comp_code
5039 || operand_equal_p (loc->val, val, 0))
5040 && (loc->expr == expr
5041 || operand_equal_p (loc->expr, expr, 0)))
5043 /* If E is not a critical edge and DEST_BB
5044 dominates the existing location for the assertion, move
5045 the assertion up in the dominance tree by updating its
5046 location information. */
5047 if ((e == NULL || !EDGE_CRITICAL_P (e))
5048 && dominated_by_p (CDI_DOMINATORS, loc->bb, dest_bb))
5057 /* Update the last node of the list and move to the next one. */
5062 /* If we didn't find an assertion already registered for
5063 NAME COMP_CODE VAL, add a new one at the end of the list of
5064 assertions associated with NAME. */
5065 n = XNEW (struct assert_locus);
5069 n->comp_code = comp_code;
5077 asserts_for[SSA_NAME_VERSION (name)] = n;
5079 bitmap_set_bit (need_assert_for, SSA_NAME_VERSION (name));
5082 /* (COND_OP0 COND_CODE COND_OP1) is a predicate which uses NAME.
5083 Extract a suitable test code and value and store them into *CODE_P and
5084 *VAL_P so the predicate is normalized to NAME *CODE_P *VAL_P.
5086 If no extraction was possible, return FALSE, otherwise return TRUE.
5088 If INVERT is true, then we invert the result stored into *CODE_P. */
5091 extract_code_and_val_from_cond_with_ops (tree name, enum tree_code cond_code,
5092 tree cond_op0, tree cond_op1,
5093 bool invert, enum tree_code *code_p,
5096 enum tree_code comp_code;
5099 /* Otherwise, we have a comparison of the form NAME COMP VAL
5100 or VAL COMP NAME. */
5101 if (name == cond_op1)
5103 /* If the predicate is of the form VAL COMP NAME, flip
5104 COMP around because we need to register NAME as the
5105 first operand in the predicate. */
5106 comp_code = swap_tree_comparison (cond_code);
5111 /* The comparison is of the form NAME COMP VAL, so the
5112 comparison code remains unchanged. */
5113 comp_code = cond_code;
5117 /* Invert the comparison code as necessary. */
5119 comp_code = invert_tree_comparison (comp_code, 0);
5121 /* VRP only handles integral and pointer types. */
5122 if (! INTEGRAL_TYPE_P (TREE_TYPE (val))
5123 && ! POINTER_TYPE_P (TREE_TYPE (val)))
5126 /* Do not register always-false predicates.
5127 FIXME: this works around a limitation in fold() when dealing with
5128 enumerations. Given 'enum { N1, N2 } x;', fold will not
5129 fold 'if (x > N2)' to 'if (0)'. */
5130 if ((comp_code == GT_EXPR || comp_code == LT_EXPR)
5131 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
5133 tree min = TYPE_MIN_VALUE (TREE_TYPE (val));
5134 tree max = TYPE_MAX_VALUE (TREE_TYPE (val));
5136 if (comp_code == GT_EXPR
5138 || compare_values (val, max) == 0))
5141 if (comp_code == LT_EXPR
5143 || compare_values (val, min) == 0))
5146 *code_p = comp_code;
5151 /* Find out smallest RES where RES > VAL && (RES & MASK) == RES, if any
5152 (otherwise return VAL). VAL and MASK must be zero-extended for
5153 precision PREC. If SGNBIT is non-zero, first xor VAL with SGNBIT
5154 (to transform signed values into unsigned) and at the end xor
5158 masked_increment (const wide_int &val_in, const wide_int &mask,
5159 const wide_int &sgnbit, unsigned int prec)
5161 wide_int bit = wi::one (prec), res;
5164 wide_int val = val_in ^ sgnbit;
5165 for (i = 0; i < prec; i++, bit += bit)
5168 if ((res & bit) == 0)
5171 res = (val + bit).and_not (res);
5173 if (wi::gtu_p (res, val))
5174 return res ^ sgnbit;
5176 return val ^ sgnbit;
5179 /* Try to register an edge assertion for SSA name NAME on edge E for
5180 the condition COND contributing to the conditional jump pointed to by BSI.
5181 Invert the condition COND if INVERT is true. */
5184 register_edge_assert_for_2 (tree name, edge e, gimple_stmt_iterator bsi,
5185 enum tree_code cond_code,
5186 tree cond_op0, tree cond_op1, bool invert)
5189 enum tree_code comp_code;
5191 if (!extract_code_and_val_from_cond_with_ops (name, cond_code,
5194 invert, &comp_code, &val))
5197 /* Only register an ASSERT_EXPR if NAME was found in the sub-graph
5198 reachable from E. */
5199 if (live_on_edge (e, name)
5200 && !has_single_use (name))
5201 register_new_assert_for (name, name, comp_code, val, NULL, e, bsi);
5203 /* In the case of NAME <= CST and NAME being defined as
5204 NAME = (unsigned) NAME2 + CST2 we can assert NAME2 >= -CST2
5205 and NAME2 <= CST - CST2. We can do the same for NAME > CST.
5206 This catches range and anti-range tests. */
5207 if ((comp_code == LE_EXPR
5208 || comp_code == GT_EXPR)
5209 && TREE_CODE (val) == INTEGER_CST
5210 && TYPE_UNSIGNED (TREE_TYPE (val)))
5212 gimple *def_stmt = SSA_NAME_DEF_STMT (name);
5213 tree cst2 = NULL_TREE, name2 = NULL_TREE, name3 = NULL_TREE;
5215 /* Extract CST2 from the (optional) addition. */
5216 if (is_gimple_assign (def_stmt)
5217 && gimple_assign_rhs_code (def_stmt) == PLUS_EXPR)
5219 name2 = gimple_assign_rhs1 (def_stmt);
5220 cst2 = gimple_assign_rhs2 (def_stmt);
5221 if (TREE_CODE (name2) == SSA_NAME
5222 && TREE_CODE (cst2) == INTEGER_CST)
5223 def_stmt = SSA_NAME_DEF_STMT (name2);
5226 /* Extract NAME2 from the (optional) sign-changing cast. */
5227 if (gimple_assign_cast_p (def_stmt))
5229 if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt))
5230 && ! TYPE_UNSIGNED (TREE_TYPE (gimple_assign_rhs1 (def_stmt)))
5231 && (TYPE_PRECISION (gimple_expr_type (def_stmt))
5232 == TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (def_stmt)))))
5233 name3 = gimple_assign_rhs1 (def_stmt);
5236 /* If name3 is used later, create an ASSERT_EXPR for it. */
5237 if (name3 != NULL_TREE
5238 && TREE_CODE (name3) == SSA_NAME
5239 && (cst2 == NULL_TREE
5240 || TREE_CODE (cst2) == INTEGER_CST)
5241 && INTEGRAL_TYPE_P (TREE_TYPE (name3))
5242 && live_on_edge (e, name3)
5243 && !has_single_use (name3))
5247 /* Build an expression for the range test. */
5248 tmp = build1 (NOP_EXPR, TREE_TYPE (name), name3);
5249 if (cst2 != NULL_TREE)
5250 tmp = build2 (PLUS_EXPR, TREE_TYPE (name), tmp, cst2);
5254 fprintf (dump_file, "Adding assert for ");
5255 print_generic_expr (dump_file, name3, 0);
5256 fprintf (dump_file, " from ");
5257 print_generic_expr (dump_file, tmp, 0);
5258 fprintf (dump_file, "\n");
5261 register_new_assert_for (name3, tmp, comp_code, val, NULL, e, bsi);
5264 /* If name2 is used later, create an ASSERT_EXPR for it. */
5265 if (name2 != NULL_TREE
5266 && TREE_CODE (name2) == SSA_NAME
5267 && TREE_CODE (cst2) == INTEGER_CST
5268 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
5269 && live_on_edge (e, name2)
5270 && !has_single_use (name2))
5274 /* Build an expression for the range test. */
5276 if (TREE_TYPE (name) != TREE_TYPE (name2))
5277 tmp = build1 (NOP_EXPR, TREE_TYPE (name), tmp);
5278 if (cst2 != NULL_TREE)
5279 tmp = build2 (PLUS_EXPR, TREE_TYPE (name), tmp, cst2);
5283 fprintf (dump_file, "Adding assert for ");
5284 print_generic_expr (dump_file, name2, 0);
5285 fprintf (dump_file, " from ");
5286 print_generic_expr (dump_file, tmp, 0);
5287 fprintf (dump_file, "\n");
5290 register_new_assert_for (name2, tmp, comp_code, val, NULL, e, bsi);
5294 /* In the case of post-in/decrement tests like if (i++) ... and uses
5295 of the in/decremented value on the edge the extra name we want to
5296 assert for is not on the def chain of the name compared. Instead
5297 it is in the set of use stmts.
5298 Similar cases happen for conversions that were simplified through
5299 fold_{sign_changed,widened}_comparison. */
5300 if ((comp_code == NE_EXPR
5301 || comp_code == EQ_EXPR)
5302 && TREE_CODE (val) == INTEGER_CST)
5304 imm_use_iterator ui;
5306 FOR_EACH_IMM_USE_STMT (use_stmt, ui, name)
5308 if (!is_gimple_assign (use_stmt))
5311 /* Cut off to use-stmts that are dominating the predecessor. */
5312 if (!dominated_by_p (CDI_DOMINATORS, e->src, gimple_bb (use_stmt)))
5315 tree name2 = gimple_assign_lhs (use_stmt);
5316 if (TREE_CODE (name2) != SSA_NAME
5317 || !live_on_edge (e, name2))
5320 enum tree_code code = gimple_assign_rhs_code (use_stmt);
5322 if (code == PLUS_EXPR
5323 || code == MINUS_EXPR)
5325 cst = gimple_assign_rhs2 (use_stmt);
5326 if (TREE_CODE (cst) != INTEGER_CST)
5328 cst = int_const_binop (code, val, cst);
5330 else if (CONVERT_EXPR_CODE_P (code))
5332 /* For truncating conversions we cannot record
5334 if (comp_code == NE_EXPR
5335 && (TYPE_PRECISION (TREE_TYPE (name2))
5336 < TYPE_PRECISION (TREE_TYPE (name))))
5338 cst = fold_convert (TREE_TYPE (name2), val);
5343 if (TREE_OVERFLOW_P (cst))
5344 cst = drop_tree_overflow (cst);
5345 register_new_assert_for (name2, name2, comp_code, cst,
5350 if (TREE_CODE_CLASS (comp_code) == tcc_comparison
5351 && TREE_CODE (val) == INTEGER_CST)
5353 gimple *def_stmt = SSA_NAME_DEF_STMT (name);
5354 tree name2 = NULL_TREE, names[2], cst2 = NULL_TREE;
5355 tree val2 = NULL_TREE;
5356 unsigned int prec = TYPE_PRECISION (TREE_TYPE (val));
5357 wide_int mask = wi::zero (prec);
5358 unsigned int nprec = prec;
5359 enum tree_code rhs_code = ERROR_MARK;
5361 if (is_gimple_assign (def_stmt))
5362 rhs_code = gimple_assign_rhs_code (def_stmt);
5364 /* In the case of NAME != CST1 where NAME = A +- CST2 we can
5365 assert that A != CST1 -+ CST2. */
5366 if ((comp_code == EQ_EXPR || comp_code == NE_EXPR)
5367 && (rhs_code == PLUS_EXPR || rhs_code == MINUS_EXPR))
5369 tree op0 = gimple_assign_rhs1 (def_stmt);
5370 tree op1 = gimple_assign_rhs2 (def_stmt);
5371 if (TREE_CODE (op0) == SSA_NAME
5372 && TREE_CODE (op1) == INTEGER_CST
5373 && live_on_edge (e, op0)
5374 && !has_single_use (op0))
5376 enum tree_code reverse_op = (rhs_code == PLUS_EXPR
5377 ? MINUS_EXPR : PLUS_EXPR);
5378 op1 = int_const_binop (reverse_op, val, op1);
5379 if (TREE_OVERFLOW (op1))
5380 op1 = drop_tree_overflow (op1);
5381 register_new_assert_for (op0, op0, comp_code, op1, NULL, e, bsi);
5385 /* Add asserts for NAME cmp CST and NAME being defined
5386 as NAME = (int) NAME2. */
5387 if (!TYPE_UNSIGNED (TREE_TYPE (val))
5388 && (comp_code == LE_EXPR || comp_code == LT_EXPR
5389 || comp_code == GT_EXPR || comp_code == GE_EXPR)
5390 && gimple_assign_cast_p (def_stmt))
5392 name2 = gimple_assign_rhs1 (def_stmt);
5393 if (CONVERT_EXPR_CODE_P (rhs_code)
5394 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
5395 && TYPE_UNSIGNED (TREE_TYPE (name2))
5396 && prec == TYPE_PRECISION (TREE_TYPE (name2))
5397 && (comp_code == LE_EXPR || comp_code == GT_EXPR
5398 || !tree_int_cst_equal (val,
5399 TYPE_MIN_VALUE (TREE_TYPE (val))))
5400 && live_on_edge (e, name2)
5401 && !has_single_use (name2))
5404 enum tree_code new_comp_code = comp_code;
5406 cst = fold_convert (TREE_TYPE (name2),
5407 TYPE_MIN_VALUE (TREE_TYPE (val)));
5408 /* Build an expression for the range test. */
5409 tmp = build2 (PLUS_EXPR, TREE_TYPE (name2), name2, cst);
5410 cst = fold_build2 (PLUS_EXPR, TREE_TYPE (name2), cst,
5411 fold_convert (TREE_TYPE (name2), val));
5412 if (comp_code == LT_EXPR || comp_code == GE_EXPR)
5414 new_comp_code = comp_code == LT_EXPR ? LE_EXPR : GT_EXPR;
5415 cst = fold_build2 (MINUS_EXPR, TREE_TYPE (name2), cst,
5416 build_int_cst (TREE_TYPE (name2), 1));
5421 fprintf (dump_file, "Adding assert for ");
5422 print_generic_expr (dump_file, name2, 0);
5423 fprintf (dump_file, " from ");
5424 print_generic_expr (dump_file, tmp, 0);
5425 fprintf (dump_file, "\n");
5428 register_new_assert_for (name2, tmp, new_comp_code, cst, NULL,
5433 /* Add asserts for NAME cmp CST and NAME being defined as
5434 NAME = NAME2 >> CST2.
5436 Extract CST2 from the right shift. */
5437 if (rhs_code == RSHIFT_EXPR)
5439 name2 = gimple_assign_rhs1 (def_stmt);
5440 cst2 = gimple_assign_rhs2 (def_stmt);
5441 if (TREE_CODE (name2) == SSA_NAME
5442 && tree_fits_uhwi_p (cst2)
5443 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
5444 && IN_RANGE (tree_to_uhwi (cst2), 1, prec - 1)
5445 && prec == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (val)))
5446 && live_on_edge (e, name2)
5447 && !has_single_use (name2))
5449 mask = wi::mask (tree_to_uhwi (cst2), false, prec);
5450 val2 = fold_binary (LSHIFT_EXPR, TREE_TYPE (val), val, cst2);
5453 if (val2 != NULL_TREE
5454 && TREE_CODE (val2) == INTEGER_CST
5455 && simple_cst_equal (fold_build2 (RSHIFT_EXPR,
5459 enum tree_code new_comp_code = comp_code;
5463 if (comp_code == EQ_EXPR || comp_code == NE_EXPR)
5465 if (!TYPE_UNSIGNED (TREE_TYPE (val)))
5467 tree type = build_nonstandard_integer_type (prec, 1);
5468 tmp = build1 (NOP_EXPR, type, name2);
5469 val2 = fold_convert (type, val2);
5471 tmp = fold_build2 (MINUS_EXPR, TREE_TYPE (tmp), tmp, val2);
5472 new_val = wide_int_to_tree (TREE_TYPE (tmp), mask);
5473 new_comp_code = comp_code == EQ_EXPR ? LE_EXPR : GT_EXPR;
5475 else if (comp_code == LT_EXPR || comp_code == GE_EXPR)
5478 = wi::min_value (prec, TYPE_SIGN (TREE_TYPE (val)));
5480 if (minval == new_val)
5481 new_val = NULL_TREE;
5486 = wi::max_value (prec, TYPE_SIGN (TREE_TYPE (val)));
5489 new_val = NULL_TREE;
5491 new_val = wide_int_to_tree (TREE_TYPE (val2), mask);
5498 fprintf (dump_file, "Adding assert for ");
5499 print_generic_expr (dump_file, name2, 0);
5500 fprintf (dump_file, " from ");
5501 print_generic_expr (dump_file, tmp, 0);
5502 fprintf (dump_file, "\n");
5505 register_new_assert_for (name2, tmp, new_comp_code, new_val,
5510 /* Add asserts for NAME cmp CST and NAME being defined as
5511 NAME = NAME2 & CST2.
5513 Extract CST2 from the and.
5516 NAME = (unsigned) NAME2;
5517 casts where NAME's type is unsigned and has smaller precision
5518 than NAME2's type as if it was NAME = NAME2 & MASK. */
5519 names[0] = NULL_TREE;
5520 names[1] = NULL_TREE;
5522 if (rhs_code == BIT_AND_EXPR
5523 || (CONVERT_EXPR_CODE_P (rhs_code)
5524 && INTEGRAL_TYPE_P (TREE_TYPE (val))
5525 && TYPE_UNSIGNED (TREE_TYPE (val))
5526 && TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (def_stmt)))
5529 name2 = gimple_assign_rhs1 (def_stmt);
5530 if (rhs_code == BIT_AND_EXPR)
5531 cst2 = gimple_assign_rhs2 (def_stmt);
5534 cst2 = TYPE_MAX_VALUE (TREE_TYPE (val));
5535 nprec = TYPE_PRECISION (TREE_TYPE (name2));
5537 if (TREE_CODE (name2) == SSA_NAME
5538 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
5539 && TREE_CODE (cst2) == INTEGER_CST
5540 && !integer_zerop (cst2)
5542 || TYPE_UNSIGNED (TREE_TYPE (val))))
5544 gimple *def_stmt2 = SSA_NAME_DEF_STMT (name2);
5545 if (gimple_assign_cast_p (def_stmt2))
5547 names[1] = gimple_assign_rhs1 (def_stmt2);
5548 if (!CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt2))
5549 || !INTEGRAL_TYPE_P (TREE_TYPE (names[1]))
5550 || (TYPE_PRECISION (TREE_TYPE (name2))
5551 != TYPE_PRECISION (TREE_TYPE (names[1])))
5552 || !live_on_edge (e, names[1])
5553 || has_single_use (names[1]))
5554 names[1] = NULL_TREE;
5556 if (live_on_edge (e, name2)
5557 && !has_single_use (name2))
5561 if (names[0] || names[1])
5563 wide_int minv, maxv, valv, cst2v;
5564 wide_int tem, sgnbit;
5565 bool valid_p = false, valn, cst2n;
5566 enum tree_code ccode = comp_code;
5568 valv = wide_int::from (val, nprec, UNSIGNED);
5569 cst2v = wide_int::from (cst2, nprec, UNSIGNED);
5570 valn = wi::neg_p (valv, TYPE_SIGN (TREE_TYPE (val)));
5571 cst2n = wi::neg_p (cst2v, TYPE_SIGN (TREE_TYPE (val)));
5572 /* If CST2 doesn't have most significant bit set,
5573 but VAL is negative, we have comparison like
5574 if ((x & 0x123) > -4) (always true). Just give up. */
5578 sgnbit = wi::set_bit_in_zero (nprec - 1, nprec);
5580 sgnbit = wi::zero (nprec);
5581 minv = valv & cst2v;
5585 /* Minimum unsigned value for equality is VAL & CST2
5586 (should be equal to VAL, otherwise we probably should
5587 have folded the comparison into false) and
5588 maximum unsigned value is VAL | ~CST2. */
5589 maxv = valv | ~cst2v;
5594 tem = valv | ~cst2v;
5595 /* If VAL is 0, handle (X & CST2) != 0 as (X & CST2) > 0U. */
5599 sgnbit = wi::zero (nprec);
5602 /* If (VAL | ~CST2) is all ones, handle it as
5603 (X & CST2) < VAL. */
5608 sgnbit = wi::zero (nprec);
5611 if (!cst2n && wi::neg_p (cst2v))
5612 sgnbit = wi::set_bit_in_zero (nprec - 1, nprec);
5621 if (tem == wi::mask (nprec - 1, false, nprec))
5627 sgnbit = wi::zero (nprec);
5632 /* Minimum unsigned value for >= if (VAL & CST2) == VAL
5633 is VAL and maximum unsigned value is ~0. For signed
5634 comparison, if CST2 doesn't have most significant bit
5635 set, handle it similarly. If CST2 has MSB set,
5636 the minimum is the same, and maximum is ~0U/2. */
5639 /* If (VAL & CST2) != VAL, X & CST2 can't be equal to
5641 minv = masked_increment (valv, cst2v, sgnbit, nprec);
5645 maxv = wi::mask (nprec - (cst2n ? 1 : 0), false, nprec);
5651 /* Find out smallest MINV where MINV > VAL
5652 && (MINV & CST2) == MINV, if any. If VAL is signed and
5653 CST2 has MSB set, compute it biased by 1 << (nprec - 1). */
5654 minv = masked_increment (valv, cst2v, sgnbit, nprec);
5657 maxv = wi::mask (nprec - (cst2n ? 1 : 0), false, nprec);
5662 /* Minimum unsigned value for <= is 0 and maximum
5663 unsigned value is VAL | ~CST2 if (VAL & CST2) == VAL.
5664 Otherwise, find smallest VAL2 where VAL2 > VAL
5665 && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
5667 For signed comparison, if CST2 doesn't have most
5668 significant bit set, handle it similarly. If CST2 has
5669 MSB set, the maximum is the same and minimum is INT_MIN. */
5674 maxv = masked_increment (valv, cst2v, sgnbit, nprec);
5686 /* Minimum unsigned value for < is 0 and maximum
5687 unsigned value is (VAL-1) | ~CST2 if (VAL & CST2) == VAL.
5688 Otherwise, find smallest VAL2 where VAL2 > VAL
5689 && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
5691 For signed comparison, if CST2 doesn't have most
5692 significant bit set, handle it similarly. If CST2 has
5693 MSB set, the maximum is the same and minimum is INT_MIN. */
5702 maxv = masked_increment (valv, cst2v, sgnbit, nprec);
5716 && (maxv - minv) != -1)
5718 tree tmp, new_val, type;
5721 for (i = 0; i < 2; i++)
5724 wide_int maxv2 = maxv;
5726 type = TREE_TYPE (names[i]);
5727 if (!TYPE_UNSIGNED (type))
5729 type = build_nonstandard_integer_type (nprec, 1);
5730 tmp = build1 (NOP_EXPR, type, names[i]);
5734 tmp = build2 (PLUS_EXPR, type, tmp,
5735 wide_int_to_tree (type, -minv));
5736 maxv2 = maxv - minv;
5738 new_val = wide_int_to_tree (type, maxv2);
5742 fprintf (dump_file, "Adding assert for ");
5743 print_generic_expr (dump_file, names[i], 0);
5744 fprintf (dump_file, " from ");
5745 print_generic_expr (dump_file, tmp, 0);
5746 fprintf (dump_file, "\n");
5749 register_new_assert_for (names[i], tmp, LE_EXPR,
5750 new_val, NULL, e, bsi);
5757 /* OP is an operand of a truth value expression which is known to have
5758 a particular value. Register any asserts for OP and for any
5759 operands in OP's defining statement.
5761 If CODE is EQ_EXPR, then we want to register OP is zero (false),
5762 if CODE is NE_EXPR, then we want to register OP is nonzero (true). */
5765 register_edge_assert_for_1 (tree op, enum tree_code code,
5766 edge e, gimple_stmt_iterator bsi)
5770 enum tree_code rhs_code;
5772 /* We only care about SSA_NAMEs. */
5773 if (TREE_CODE (op) != SSA_NAME)
5776 /* We know that OP will have a zero or nonzero value. If OP is used
5777 more than once go ahead and register an assert for OP. */
5778 if (live_on_edge (e, op)
5779 && !has_single_use (op))
5781 val = build_int_cst (TREE_TYPE (op), 0);
5782 register_new_assert_for (op, op, code, val, NULL, e, bsi);
5785 /* Now look at how OP is set. If it's set from a comparison,
5786 a truth operation or some bit operations, then we may be able
5787 to register information about the operands of that assignment. */
5788 op_def = SSA_NAME_DEF_STMT (op);
5789 if (gimple_code (op_def) != GIMPLE_ASSIGN)
5792 rhs_code = gimple_assign_rhs_code (op_def);
5794 if (TREE_CODE_CLASS (rhs_code) == tcc_comparison)
5796 bool invert = (code == EQ_EXPR ? true : false);
5797 tree op0 = gimple_assign_rhs1 (op_def);
5798 tree op1 = gimple_assign_rhs2 (op_def);
5800 if (TREE_CODE (op0) == SSA_NAME)
5801 register_edge_assert_for_2 (op0, e, bsi, rhs_code, op0, op1, invert);
5802 if (TREE_CODE (op1) == SSA_NAME)
5803 register_edge_assert_for_2 (op1, e, bsi, rhs_code, op0, op1, invert);
5805 else if ((code == NE_EXPR
5806 && gimple_assign_rhs_code (op_def) == BIT_AND_EXPR)
5808 && gimple_assign_rhs_code (op_def) == BIT_IOR_EXPR))
5810 /* Recurse on each operand. */
5811 tree op0 = gimple_assign_rhs1 (op_def);
5812 tree op1 = gimple_assign_rhs2 (op_def);
5813 if (TREE_CODE (op0) == SSA_NAME
5814 && has_single_use (op0))
5815 register_edge_assert_for_1 (op0, code, e, bsi);
5816 if (TREE_CODE (op1) == SSA_NAME
5817 && has_single_use (op1))
5818 register_edge_assert_for_1 (op1, code, e, bsi);
5820 else if (gimple_assign_rhs_code (op_def) == BIT_NOT_EXPR
5821 && TYPE_PRECISION (TREE_TYPE (gimple_assign_lhs (op_def))) == 1)
5823 /* Recurse, flipping CODE. */
5824 code = invert_tree_comparison (code, false);
5825 register_edge_assert_for_1 (gimple_assign_rhs1 (op_def), code, e, bsi);
5827 else if (gimple_assign_rhs_code (op_def) == SSA_NAME)
5829 /* Recurse through the copy. */
5830 register_edge_assert_for_1 (gimple_assign_rhs1 (op_def), code, e, bsi);
5832 else if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (op_def)))
5834 /* Recurse through the type conversion, unless it is a narrowing
5835 conversion or conversion from non-integral type. */
5836 tree rhs = gimple_assign_rhs1 (op_def);
5837 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs))
5838 && (TYPE_PRECISION (TREE_TYPE (rhs))
5839 <= TYPE_PRECISION (TREE_TYPE (op))))
5840 register_edge_assert_for_1 (rhs, code, e, bsi);
5844 /* Try to register an edge assertion for SSA name NAME on edge E for
5845 the condition COND contributing to the conditional jump pointed to by
5849 register_edge_assert_for (tree name, edge e, gimple_stmt_iterator si,
5850 enum tree_code cond_code, tree cond_op0,
5854 enum tree_code comp_code;
5855 bool is_else_edge = (e->flags & EDGE_FALSE_VALUE) != 0;
5857 /* Do not attempt to infer anything in names that flow through
5859 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name))
5862 if (!extract_code_and_val_from_cond_with_ops (name, cond_code,
5868 /* Register ASSERT_EXPRs for name. */
5869 register_edge_assert_for_2 (name, e, si, cond_code, cond_op0,
5870 cond_op1, is_else_edge);
5873 /* If COND is effectively an equality test of an SSA_NAME against
5874 the value zero or one, then we may be able to assert values
5875 for SSA_NAMEs which flow into COND. */
5877 /* In the case of NAME == 1 or NAME != 0, for BIT_AND_EXPR defining
5878 statement of NAME we can assert both operands of the BIT_AND_EXPR
5879 have nonzero value. */
5880 if (((comp_code == EQ_EXPR && integer_onep (val))
5881 || (comp_code == NE_EXPR && integer_zerop (val))))
5883 gimple *def_stmt = SSA_NAME_DEF_STMT (name);
5885 if (is_gimple_assign (def_stmt)
5886 && gimple_assign_rhs_code (def_stmt) == BIT_AND_EXPR)
5888 tree op0 = gimple_assign_rhs1 (def_stmt);
5889 tree op1 = gimple_assign_rhs2 (def_stmt);
5890 register_edge_assert_for_1 (op0, NE_EXPR, e, si);
5891 register_edge_assert_for_1 (op1, NE_EXPR, e, si);
5895 /* In the case of NAME == 0 or NAME != 1, for BIT_IOR_EXPR defining
5896 statement of NAME we can assert both operands of the BIT_IOR_EXPR
5898 if (((comp_code == EQ_EXPR && integer_zerop (val))
5899 || (comp_code == NE_EXPR && integer_onep (val))))
5901 gimple *def_stmt = SSA_NAME_DEF_STMT (name);
5903 /* For BIT_IOR_EXPR only if NAME == 0 both operands have
5904 necessarily zero value, or if type-precision is one. */
5905 if (is_gimple_assign (def_stmt)
5906 && (gimple_assign_rhs_code (def_stmt) == BIT_IOR_EXPR
5907 && (TYPE_PRECISION (TREE_TYPE (name)) == 1
5908 || comp_code == EQ_EXPR)))
5910 tree op0 = gimple_assign_rhs1 (def_stmt);
5911 tree op1 = gimple_assign_rhs2 (def_stmt);
5912 register_edge_assert_for_1 (op0, EQ_EXPR, e, si);
5913 register_edge_assert_for_1 (op1, EQ_EXPR, e, si);
5919 /* Determine whether the outgoing edges of BB should receive an
5920 ASSERT_EXPR for each of the operands of BB's LAST statement.
5921 The last statement of BB must be a COND_EXPR.
5923 If any of the sub-graphs rooted at BB have an interesting use of
5924 the predicate operands, an assert location node is added to the
5925 list of assertions for the corresponding operands. */
5928 find_conditional_asserts (basic_block bb, gcond *last)
5930 gimple_stmt_iterator bsi;
5936 bsi = gsi_for_stmt (last);
5938 /* Look for uses of the operands in each of the sub-graphs
5939 rooted at BB. We need to check each of the outgoing edges
5940 separately, so that we know what kind of ASSERT_EXPR to
5942 FOR_EACH_EDGE (e, ei, bb->succs)
5947 /* Register the necessary assertions for each operand in the
5948 conditional predicate. */
5949 FOR_EACH_SSA_TREE_OPERAND (op, last, iter, SSA_OP_USE)
5950 register_edge_assert_for (op, e, bsi,
5951 gimple_cond_code (last),
5952 gimple_cond_lhs (last),
5953 gimple_cond_rhs (last));
5963 /* Compare two case labels sorting first by the destination bb index
5964 and then by the case value. */
5967 compare_case_labels (const void *p1, const void *p2)
5969 const struct case_info *ci1 = (const struct case_info *) p1;
5970 const struct case_info *ci2 = (const struct case_info *) p2;
5971 int idx1 = ci1->bb->index;
5972 int idx2 = ci2->bb->index;
5976 else if (idx1 == idx2)
5978 /* Make sure the default label is first in a group. */
5979 if (!CASE_LOW (ci1->expr))
5981 else if (!CASE_LOW (ci2->expr))
5984 return tree_int_cst_compare (CASE_LOW (ci1->expr),
5985 CASE_LOW (ci2->expr));
5991 /* Determine whether the outgoing edges of BB should receive an
5992 ASSERT_EXPR for each of the operands of BB's LAST statement.
5993 The last statement of BB must be a SWITCH_EXPR.
5995 If any of the sub-graphs rooted at BB have an interesting use of
5996 the predicate operands, an assert location node is added to the
5997 list of assertions for the corresponding operands. */
6000 find_switch_asserts (basic_block bb, gswitch *last)
6002 gimple_stmt_iterator bsi;
6005 struct case_info *ci;
6006 size_t n = gimple_switch_num_labels (last);
6007 #if GCC_VERSION >= 4000
6010 /* Work around GCC 3.4 bug (PR 37086). */
6011 volatile unsigned int idx;
6014 bsi = gsi_for_stmt (last);
6015 op = gimple_switch_index (last);
6016 if (TREE_CODE (op) != SSA_NAME)
6019 /* Build a vector of case labels sorted by destination label. */
6020 ci = XNEWVEC (struct case_info, n);
6021 for (idx = 0; idx < n; ++idx)
6023 ci[idx].expr = gimple_switch_label (last, idx);
6024 ci[idx].bb = label_to_block (CASE_LABEL (ci[idx].expr));
6026 qsort (ci, n, sizeof (struct case_info), compare_case_labels);
6028 for (idx = 0; idx < n; ++idx)
6031 tree cl = ci[idx].expr;
6032 basic_block cbb = ci[idx].bb;
6034 min = CASE_LOW (cl);
6035 max = CASE_HIGH (cl);
6037 /* If there are multiple case labels with the same destination
6038 we need to combine them to a single value range for the edge. */
6039 if (idx + 1 < n && cbb == ci[idx + 1].bb)
6041 /* Skip labels until the last of the group. */
6044 } while (idx < n && cbb == ci[idx].bb);
6047 /* Pick up the maximum of the case label range. */
6048 if (CASE_HIGH (ci[idx].expr))
6049 max = CASE_HIGH (ci[idx].expr);
6051 max = CASE_LOW (ci[idx].expr);
6054 /* Nothing to do if the range includes the default label until we
6055 can register anti-ranges. */
6056 if (min == NULL_TREE)
6059 /* Find the edge to register the assert expr on. */
6060 e = find_edge (bb, cbb);
6062 /* Register the necessary assertions for the operand in the
6064 register_edge_assert_for (op, e, bsi,
6065 max ? GE_EXPR : EQ_EXPR,
6066 op, fold_convert (TREE_TYPE (op), min));
6068 register_edge_assert_for (op, e, bsi, LE_EXPR, op,
6069 fold_convert (TREE_TYPE (op), max));
6076 /* Traverse all the statements in block BB looking for statements that
6077 may generate useful assertions for the SSA names in their operand.
6078 If a statement produces a useful assertion A for name N_i, then the
6079 list of assertions already generated for N_i is scanned to
6080 determine if A is actually needed.
6082 If N_i already had the assertion A at a location dominating the
6083 current location, then nothing needs to be done. Otherwise, the
6084 new location for A is recorded instead.
6086 1- For every statement S in BB, all the variables used by S are
6087 added to bitmap FOUND_IN_SUBGRAPH.
6089 2- If statement S uses an operand N in a way that exposes a known
6090 value range for N, then if N was not already generated by an
6091 ASSERT_EXPR, create a new assert location for N. For instance,
6092 if N is a pointer and the statement dereferences it, we can
6093 assume that N is not NULL.
6095 3- COND_EXPRs are a special case of #2. We can derive range
6096 information from the predicate but need to insert different
6097 ASSERT_EXPRs for each of the sub-graphs rooted at the
6098 conditional block. If the last statement of BB is a conditional
6099 expression of the form 'X op Y', then
6101 a) Remove X and Y from the set FOUND_IN_SUBGRAPH.
6103 b) If the conditional is the only entry point to the sub-graph
6104 corresponding to the THEN_CLAUSE, recurse into it. On
6105 return, if X and/or Y are marked in FOUND_IN_SUBGRAPH, then
6106 an ASSERT_EXPR is added for the corresponding variable.
6108 c) Repeat step (b) on the ELSE_CLAUSE.
6110 d) Mark X and Y in FOUND_IN_SUBGRAPH.
6119 In this case, an assertion on the THEN clause is useful to
6120 determine that 'a' is always 9 on that edge. However, an assertion
6121 on the ELSE clause would be unnecessary.
6123 4- If BB does not end in a conditional expression, then we recurse
6124 into BB's dominator children.
6126 At the end of the recursive traversal, every SSA name will have a
6127 list of locations where ASSERT_EXPRs should be added. When a new
6128 location for name N is found, it is registered by calling
6129 register_new_assert_for. That function keeps track of all the
6130 registered assertions to prevent adding unnecessary assertions.
6131 For instance, if a pointer P_4 is dereferenced more than once in a
6132 dominator tree, only the location dominating all the dereference of
6133 P_4 will receive an ASSERT_EXPR. */
6136 find_assert_locations_1 (basic_block bb, sbitmap live)
6140 last = last_stmt (bb);
6142 /* If BB's last statement is a conditional statement involving integer
6143 operands, determine if we need to add ASSERT_EXPRs. */
6145 && gimple_code (last) == GIMPLE_COND
6146 && !fp_predicate (last)
6147 && !ZERO_SSA_OPERANDS (last, SSA_OP_USE))
6148 find_conditional_asserts (bb, as_a <gcond *> (last));
6150 /* If BB's last statement is a switch statement involving integer
6151 operands, determine if we need to add ASSERT_EXPRs. */
6153 && gimple_code (last) == GIMPLE_SWITCH
6154 && !ZERO_SSA_OPERANDS (last, SSA_OP_USE))
6155 find_switch_asserts (bb, as_a <gswitch *> (last));
6157 /* Traverse all the statements in BB marking used names and looking
6158 for statements that may infer assertions for their used operands. */
6159 for (gimple_stmt_iterator si = gsi_last_bb (bb); !gsi_end_p (si);
6166 stmt = gsi_stmt (si);
6168 if (is_gimple_debug (stmt))
6171 /* See if we can derive an assertion for any of STMT's operands. */
6172 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_USE)
6175 enum tree_code comp_code;
6177 /* If op is not live beyond this stmt, do not bother to insert
6179 if (!bitmap_bit_p (live, SSA_NAME_VERSION (op)))
6182 /* If OP is used in such a way that we can infer a value
6183 range for it, and we don't find a previous assertion for
6184 it, create a new assertion location node for OP. */
6185 if (infer_value_range (stmt, op, &comp_code, &value))
6187 /* If we are able to infer a nonzero value range for OP,
6188 then walk backwards through the use-def chain to see if OP
6189 was set via a typecast.
6191 If so, then we can also infer a nonzero value range
6192 for the operand of the NOP_EXPR. */
6193 if (comp_code == NE_EXPR && integer_zerop (value))
6196 gimple *def_stmt = SSA_NAME_DEF_STMT (t);
6198 while (is_gimple_assign (def_stmt)
6199 && CONVERT_EXPR_CODE_P
6200 (gimple_assign_rhs_code (def_stmt))
6202 (gimple_assign_rhs1 (def_stmt)) == SSA_NAME
6204 (TREE_TYPE (gimple_assign_rhs1 (def_stmt))))
6206 t = gimple_assign_rhs1 (def_stmt);
6207 def_stmt = SSA_NAME_DEF_STMT (t);
6209 /* Note we want to register the assert for the
6210 operand of the NOP_EXPR after SI, not after the
6212 if (! has_single_use (t))
6213 register_new_assert_for (t, t, comp_code, value,
6218 register_new_assert_for (op, op, comp_code, value, bb, NULL, si);
6223 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_USE)
6224 bitmap_set_bit (live, SSA_NAME_VERSION (op));
6225 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_DEF)
6226 bitmap_clear_bit (live, SSA_NAME_VERSION (op));
6229 /* Traverse all PHI nodes in BB, updating live. */
6230 for (gphi_iterator si = gsi_start_phis (bb); !gsi_end_p (si);
6233 use_operand_p arg_p;
6235 gphi *phi = si.phi ();
6236 tree res = gimple_phi_result (phi);
6238 if (virtual_operand_p (res))
6241 FOR_EACH_PHI_ARG (arg_p, phi, i, SSA_OP_USE)
6243 tree arg = USE_FROM_PTR (arg_p);
6244 if (TREE_CODE (arg) == SSA_NAME)
6245 bitmap_set_bit (live, SSA_NAME_VERSION (arg));
6248 bitmap_clear_bit (live, SSA_NAME_VERSION (res));
6252 /* Do an RPO walk over the function computing SSA name liveness
6253 on-the-fly and deciding on assert expressions to insert. */
6256 find_assert_locations (void)
6258 int *rpo = XNEWVEC (int, last_basic_block_for_fn (cfun));
6259 int *bb_rpo = XNEWVEC (int, last_basic_block_for_fn (cfun));
6260 int *last_rpo = XCNEWVEC (int, last_basic_block_for_fn (cfun));
6263 live = XCNEWVEC (sbitmap, last_basic_block_for_fn (cfun));
6264 rpo_cnt = pre_and_rev_post_order_compute (NULL, rpo, false);
6265 for (i = 0; i < rpo_cnt; ++i)
6268 /* Pre-seed loop latch liveness from loop header PHI nodes. Due to
6269 the order we compute liveness and insert asserts we otherwise
6270 fail to insert asserts into the loop latch. */
6272 FOR_EACH_LOOP (loop, 0)
6274 i = loop->latch->index;
6275 unsigned int j = single_succ_edge (loop->latch)->dest_idx;
6276 for (gphi_iterator gsi = gsi_start_phis (loop->header);
6277 !gsi_end_p (gsi); gsi_next (&gsi))
6279 gphi *phi = gsi.phi ();
6280 if (virtual_operand_p (gimple_phi_result (phi)))
6282 tree arg = gimple_phi_arg_def (phi, j);
6283 if (TREE_CODE (arg) == SSA_NAME)
6285 if (live[i] == NULL)
6287 live[i] = sbitmap_alloc (num_ssa_names);
6288 bitmap_clear (live[i]);
6290 bitmap_set_bit (live[i], SSA_NAME_VERSION (arg));
6295 for (i = rpo_cnt - 1; i >= 0; --i)
6297 basic_block bb = BASIC_BLOCK_FOR_FN (cfun, rpo[i]);
6303 live[rpo[i]] = sbitmap_alloc (num_ssa_names);
6304 bitmap_clear (live[rpo[i]]);
6307 /* Process BB and update the live information with uses in
6309 find_assert_locations_1 (bb, live[rpo[i]]);
6311 /* Merge liveness into the predecessor blocks and free it. */
6312 if (!bitmap_empty_p (live[rpo[i]]))
6315 FOR_EACH_EDGE (e, ei, bb->preds)
6317 int pred = e->src->index;
6318 if ((e->flags & EDGE_DFS_BACK) || pred == ENTRY_BLOCK)
6323 live[pred] = sbitmap_alloc (num_ssa_names);
6324 bitmap_clear (live[pred]);
6326 bitmap_ior (live[pred], live[pred], live[rpo[i]]);
6328 if (bb_rpo[pred] < pred_rpo)
6329 pred_rpo = bb_rpo[pred];
6332 /* Record the RPO number of the last visited block that needs
6333 live information from this block. */
6334 last_rpo[rpo[i]] = pred_rpo;
6338 sbitmap_free (live[rpo[i]]);
6339 live[rpo[i]] = NULL;
6342 /* We can free all successors live bitmaps if all their
6343 predecessors have been visited already. */
6344 FOR_EACH_EDGE (e, ei, bb->succs)
6345 if (last_rpo[e->dest->index] == i
6346 && live[e->dest->index])
6348 sbitmap_free (live[e->dest->index]);
6349 live[e->dest->index] = NULL;
6354 XDELETEVEC (bb_rpo);
6355 XDELETEVEC (last_rpo);
6356 for (i = 0; i < last_basic_block_for_fn (cfun); ++i)
6358 sbitmap_free (live[i]);
6362 /* Create an ASSERT_EXPR for NAME and insert it in the location
6363 indicated by LOC. Return true if we made any edge insertions. */
6366 process_assert_insertions_for (tree name, assert_locus *loc)
6368 /* Build the comparison expression NAME_i COMP_CODE VAL. */
6371 gimple *assert_stmt;
6375 /* If we have X <=> X do not insert an assert expr for that. */
6376 if (loc->expr == loc->val)
6379 cond = build2 (loc->comp_code, boolean_type_node, loc->expr, loc->val);
6380 assert_stmt = build_assert_expr_for (cond, name);
6383 /* We have been asked to insert the assertion on an edge. This
6384 is used only by COND_EXPR and SWITCH_EXPR assertions. */
6385 gcc_checking_assert (gimple_code (gsi_stmt (loc->si)) == GIMPLE_COND
6386 || (gimple_code (gsi_stmt (loc->si))
6389 gsi_insert_on_edge (loc->e, assert_stmt);
6393 /* Otherwise, we can insert right after LOC->SI iff the
6394 statement must not be the last statement in the block. */
6395 stmt = gsi_stmt (loc->si);
6396 if (!stmt_ends_bb_p (stmt))
6398 gsi_insert_after (&loc->si, assert_stmt, GSI_SAME_STMT);
6402 /* If STMT must be the last statement in BB, we can only insert new
6403 assertions on the non-abnormal edge out of BB. Note that since
6404 STMT is not control flow, there may only be one non-abnormal edge
6406 FOR_EACH_EDGE (e, ei, loc->bb->succs)
6407 if (!(e->flags & EDGE_ABNORMAL))
6409 gsi_insert_on_edge (e, assert_stmt);
6417 /* Process all the insertions registered for every name N_i registered
6418 in NEED_ASSERT_FOR. The list of assertions to be inserted are
6419 found in ASSERTS_FOR[i]. */
6422 process_assert_insertions (void)
6426 bool update_edges_p = false;
6427 int num_asserts = 0;
6429 if (dump_file && (dump_flags & TDF_DETAILS))
6430 dump_all_asserts (dump_file);
6432 EXECUTE_IF_SET_IN_BITMAP (need_assert_for, 0, i, bi)
6434 assert_locus *loc = asserts_for[i];
6439 assert_locus *next = loc->next;
6440 update_edges_p |= process_assert_insertions_for (ssa_name (i), loc);
6448 gsi_commit_edge_inserts ();
6450 statistics_counter_event (cfun, "Number of ASSERT_EXPR expressions inserted",
6455 /* Traverse the flowgraph looking for conditional jumps to insert range
6456 expressions. These range expressions are meant to provide information
6457 to optimizations that need to reason in terms of value ranges. They
6458 will not be expanded into RTL. For instance, given:
6467 this pass will transform the code into:
6473 x = ASSERT_EXPR <x, x < y>
6478 y = ASSERT_EXPR <y, x >= y>
6482 The idea is that once copy and constant propagation have run, other
6483 optimizations will be able to determine what ranges of values can 'x'
6484 take in different paths of the code, simply by checking the reaching
6485 definition of 'x'. */
6488 insert_range_assertions (void)
6490 need_assert_for = BITMAP_ALLOC (NULL);
6491 asserts_for = XCNEWVEC (assert_locus *, num_ssa_names);
6493 calculate_dominance_info (CDI_DOMINATORS);
6495 find_assert_locations ();
6496 if (!bitmap_empty_p (need_assert_for))
6498 process_assert_insertions ();
6499 update_ssa (TODO_update_ssa_no_phi);
6502 if (dump_file && (dump_flags & TDF_DETAILS))
6504 fprintf (dump_file, "\nSSA form after inserting ASSERT_EXPRs\n");
6505 dump_function_to_file (current_function_decl, dump_file, dump_flags);
6509 BITMAP_FREE (need_assert_for);
6512 /* Checks one ARRAY_REF in REF, located at LOCUS. Ignores flexible arrays
6513 and "struct" hacks. If VRP can determine that the
6514 array subscript is a constant, check if it is outside valid
6515 range. If the array subscript is a RANGE, warn if it is
6516 non-overlapping with valid range.
6517 IGNORE_OFF_BY_ONE is true if the ARRAY_REF is inside a ADDR_EXPR. */
6520 check_array_ref (location_t location, tree ref, bool ignore_off_by_one)
6522 value_range *vr = NULL;
6523 tree low_sub, up_sub;
6524 tree low_bound, up_bound, up_bound_p1;
6526 if (TREE_NO_WARNING (ref))
6529 low_sub = up_sub = TREE_OPERAND (ref, 1);
6530 up_bound = array_ref_up_bound (ref);
6532 /* Can not check flexible arrays. */
6534 || TREE_CODE (up_bound) != INTEGER_CST)
6537 /* Accesses to trailing arrays via pointers may access storage
6538 beyond the types array bounds. */
6539 if (warn_array_bounds < 2
6540 && array_at_struct_end_p (ref))
6543 low_bound = array_ref_low_bound (ref);
6544 up_bound_p1 = int_const_binop (PLUS_EXPR, up_bound,
6545 build_int_cst (TREE_TYPE (up_bound), 1));
6548 if (tree_int_cst_equal (low_bound, up_bound_p1))
6550 warning_at (location, OPT_Warray_bounds,
6551 "array subscript is above array bounds");
6552 TREE_NO_WARNING (ref) = 1;
6555 if (TREE_CODE (low_sub) == SSA_NAME)
6557 vr = get_value_range (low_sub);
6558 if (vr->type == VR_RANGE || vr->type == VR_ANTI_RANGE)
6560 low_sub = vr->type == VR_RANGE ? vr->max : vr->min;
6561 up_sub = vr->type == VR_RANGE ? vr->min : vr->max;
6565 if (vr && vr->type == VR_ANTI_RANGE)
6567 if (TREE_CODE (up_sub) == INTEGER_CST
6568 && (ignore_off_by_one
6569 ? tree_int_cst_lt (up_bound, up_sub)
6570 : tree_int_cst_le (up_bound, up_sub))
6571 && TREE_CODE (low_sub) == INTEGER_CST
6572 && tree_int_cst_le (low_sub, low_bound))
6574 warning_at (location, OPT_Warray_bounds,
6575 "array subscript is outside array bounds");
6576 TREE_NO_WARNING (ref) = 1;
6579 else if (TREE_CODE (up_sub) == INTEGER_CST
6580 && (ignore_off_by_one
6581 ? !tree_int_cst_le (up_sub, up_bound_p1)
6582 : !tree_int_cst_le (up_sub, up_bound)))
6584 if (dump_file && (dump_flags & TDF_DETAILS))
6586 fprintf (dump_file, "Array bound warning for ");
6587 dump_generic_expr (MSG_NOTE, TDF_SLIM, ref);
6588 fprintf (dump_file, "\n");
6590 warning_at (location, OPT_Warray_bounds,
6591 "array subscript is above array bounds");
6592 TREE_NO_WARNING (ref) = 1;
6594 else if (TREE_CODE (low_sub) == INTEGER_CST
6595 && tree_int_cst_lt (low_sub, low_bound))
6597 if (dump_file && (dump_flags & TDF_DETAILS))
6599 fprintf (dump_file, "Array bound warning for ");
6600 dump_generic_expr (MSG_NOTE, TDF_SLIM, ref);
6601 fprintf (dump_file, "\n");
6603 warning_at (location, OPT_Warray_bounds,
6604 "array subscript is below array bounds");
6605 TREE_NO_WARNING (ref) = 1;
6609 /* Searches if the expr T, located at LOCATION computes
6610 address of an ARRAY_REF, and call check_array_ref on it. */
6613 search_for_addr_array (tree t, location_t location)
6615 /* Check each ARRAY_REFs in the reference chain. */
6618 if (TREE_CODE (t) == ARRAY_REF)
6619 check_array_ref (location, t, true /*ignore_off_by_one*/);
6621 t = TREE_OPERAND (t, 0);
6623 while (handled_component_p (t));
6625 if (TREE_CODE (t) == MEM_REF
6626 && TREE_CODE (TREE_OPERAND (t, 0)) == ADDR_EXPR
6627 && !TREE_NO_WARNING (t))
6629 tree tem = TREE_OPERAND (TREE_OPERAND (t, 0), 0);
6630 tree low_bound, up_bound, el_sz;
6632 if (TREE_CODE (TREE_TYPE (tem)) != ARRAY_TYPE
6633 || TREE_CODE (TREE_TYPE (TREE_TYPE (tem))) == ARRAY_TYPE
6634 || !TYPE_DOMAIN (TREE_TYPE (tem)))
6637 low_bound = TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (tem)));
6638 up_bound = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (tem)));
6639 el_sz = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (tem)));
6641 || TREE_CODE (low_bound) != INTEGER_CST
6643 || TREE_CODE (up_bound) != INTEGER_CST
6645 || TREE_CODE (el_sz) != INTEGER_CST)
6648 idx = mem_ref_offset (t);
6649 idx = wi::sdiv_trunc (idx, wi::to_offset (el_sz));
6650 if (wi::lts_p (idx, 0))
6652 if (dump_file && (dump_flags & TDF_DETAILS))
6654 fprintf (dump_file, "Array bound warning for ");
6655 dump_generic_expr (MSG_NOTE, TDF_SLIM, t);
6656 fprintf (dump_file, "\n");
6658 warning_at (location, OPT_Warray_bounds,
6659 "array subscript is below array bounds");
6660 TREE_NO_WARNING (t) = 1;
6662 else if (wi::gts_p (idx, (wi::to_offset (up_bound)
6663 - wi::to_offset (low_bound) + 1)))
6665 if (dump_file && (dump_flags & TDF_DETAILS))
6667 fprintf (dump_file, "Array bound warning for ");
6668 dump_generic_expr (MSG_NOTE, TDF_SLIM, t);
6669 fprintf (dump_file, "\n");
6671 warning_at (location, OPT_Warray_bounds,
6672 "array subscript is above array bounds");
6673 TREE_NO_WARNING (t) = 1;
6678 /* walk_tree() callback that checks if *TP is
6679 an ARRAY_REF inside an ADDR_EXPR (in which an array
6680 subscript one outside the valid range is allowed). Call
6681 check_array_ref for each ARRAY_REF found. The location is
6685 check_array_bounds (tree *tp, int *walk_subtree, void *data)
6688 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
6689 location_t location;
6691 if (EXPR_HAS_LOCATION (t))
6692 location = EXPR_LOCATION (t);
6695 location_t *locp = (location_t *) wi->info;
6699 *walk_subtree = TRUE;
6701 if (TREE_CODE (t) == ARRAY_REF)
6702 check_array_ref (location, t, false /*ignore_off_by_one*/);
6704 else if (TREE_CODE (t) == ADDR_EXPR)
6706 search_for_addr_array (t, location);
6707 *walk_subtree = FALSE;
6713 /* Walk over all statements of all reachable BBs and call check_array_bounds
6717 check_all_array_refs (void)
6720 gimple_stmt_iterator si;
6722 FOR_EACH_BB_FN (bb, cfun)
6726 bool executable = false;
6728 /* Skip blocks that were found to be unreachable. */
6729 FOR_EACH_EDGE (e, ei, bb->preds)
6730 executable |= !!(e->flags & EDGE_EXECUTABLE);
6734 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
6736 gimple *stmt = gsi_stmt (si);
6737 struct walk_stmt_info wi;
6738 if (!gimple_has_location (stmt)
6739 || is_gimple_debug (stmt))
6742 memset (&wi, 0, sizeof (wi));
6744 location_t loc = gimple_location (stmt);
6747 walk_gimple_op (gsi_stmt (si),
6754 /* Return true if all imm uses of VAR are either in STMT, or
6755 feed (optionally through a chain of single imm uses) GIMPLE_COND
6756 in basic block COND_BB. */
6759 all_imm_uses_in_stmt_or_feed_cond (tree var, gimple *stmt, basic_block cond_bb)
6761 use_operand_p use_p, use2_p;
6762 imm_use_iterator iter;
6764 FOR_EACH_IMM_USE_FAST (use_p, iter, var)
6765 if (USE_STMT (use_p) != stmt)
6767 gimple *use_stmt = USE_STMT (use_p), *use_stmt2;
6768 if (is_gimple_debug (use_stmt))
6770 while (is_gimple_assign (use_stmt)
6771 && TREE_CODE (gimple_assign_lhs (use_stmt)) == SSA_NAME
6772 && single_imm_use (gimple_assign_lhs (use_stmt),
6773 &use2_p, &use_stmt2))
6774 use_stmt = use_stmt2;
6775 if (gimple_code (use_stmt) != GIMPLE_COND
6776 || gimple_bb (use_stmt) != cond_bb)
6789 __builtin_unreachable ();
6791 x_5 = ASSERT_EXPR <x_3, ...>;
6792 If x_3 has no other immediate uses (checked by caller),
6793 var is the x_3 var from ASSERT_EXPR, we can clear low 5 bits
6794 from the non-zero bitmask. */
6797 maybe_set_nonzero_bits (basic_block bb, tree var)
6799 edge e = single_pred_edge (bb);
6800 basic_block cond_bb = e->src;
6801 gimple *stmt = last_stmt (cond_bb);
6805 || gimple_code (stmt) != GIMPLE_COND
6806 || gimple_cond_code (stmt) != ((e->flags & EDGE_TRUE_VALUE)
6807 ? EQ_EXPR : NE_EXPR)
6808 || TREE_CODE (gimple_cond_lhs (stmt)) != SSA_NAME
6809 || !integer_zerop (gimple_cond_rhs (stmt)))
6812 stmt = SSA_NAME_DEF_STMT (gimple_cond_lhs (stmt));
6813 if (!is_gimple_assign (stmt)
6814 || gimple_assign_rhs_code (stmt) != BIT_AND_EXPR
6815 || TREE_CODE (gimple_assign_rhs2 (stmt)) != INTEGER_CST)
6817 if (gimple_assign_rhs1 (stmt) != var)
6821 if (TREE_CODE (gimple_assign_rhs1 (stmt)) != SSA_NAME)
6823 stmt2 = SSA_NAME_DEF_STMT (gimple_assign_rhs1 (stmt));
6824 if (!gimple_assign_cast_p (stmt2)
6825 || gimple_assign_rhs1 (stmt2) != var
6826 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (stmt2))
6827 || (TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (stmt)))
6828 != TYPE_PRECISION (TREE_TYPE (var))))
6831 cst = gimple_assign_rhs2 (stmt);
6832 set_nonzero_bits (var, wi::bit_and_not (get_nonzero_bits (var), cst));
6835 /* Convert range assertion expressions into the implied copies and
6836 copy propagate away the copies. Doing the trivial copy propagation
6837 here avoids the need to run the full copy propagation pass after
6840 FIXME, this will eventually lead to copy propagation removing the
6841 names that had useful range information attached to them. For
6842 instance, if we had the assertion N_i = ASSERT_EXPR <N_j, N_j > 3>,
6843 then N_i will have the range [3, +INF].
6845 However, by converting the assertion into the implied copy
6846 operation N_i = N_j, we will then copy-propagate N_j into the uses
6847 of N_i and lose the range information. We may want to hold on to
6848 ASSERT_EXPRs a little while longer as the ranges could be used in
6849 things like jump threading.
6851 The problem with keeping ASSERT_EXPRs around is that passes after
6852 VRP need to handle them appropriately.
6854 Another approach would be to make the range information a first
6855 class property of the SSA_NAME so that it can be queried from
6856 any pass. This is made somewhat more complex by the need for
6857 multiple ranges to be associated with one SSA_NAME. */
6860 remove_range_assertions (void)
6863 gimple_stmt_iterator si;
6864 /* 1 if looking at ASSERT_EXPRs immediately at the beginning of
6865 a basic block preceeded by GIMPLE_COND branching to it and
6866 __builtin_trap, -1 if not yet checked, 0 otherwise. */
6869 /* Note that the BSI iterator bump happens at the bottom of the
6870 loop and no bump is necessary if we're removing the statement
6871 referenced by the current BSI. */
6872 FOR_EACH_BB_FN (bb, cfun)
6873 for (si = gsi_after_labels (bb), is_unreachable = -1; !gsi_end_p (si);)
6875 gimple *stmt = gsi_stmt (si);
6878 if (is_gimple_assign (stmt)
6879 && gimple_assign_rhs_code (stmt) == ASSERT_EXPR)
6881 tree lhs = gimple_assign_lhs (stmt);
6882 tree rhs = gimple_assign_rhs1 (stmt);
6884 use_operand_p use_p;
6885 imm_use_iterator iter;
6887 var = ASSERT_EXPR_VAR (rhs);
6888 gcc_assert (TREE_CODE (var) == SSA_NAME);
6890 if (!POINTER_TYPE_P (TREE_TYPE (lhs))
6891 && SSA_NAME_RANGE_INFO (lhs))
6893 if (is_unreachable == -1)
6896 if (single_pred_p (bb)
6897 && assert_unreachable_fallthru_edge_p
6898 (single_pred_edge (bb)))
6902 if (x_7 >= 10 && x_7 < 20)
6903 __builtin_unreachable ();
6904 x_8 = ASSERT_EXPR <x_7, ...>;
6905 if the only uses of x_7 are in the ASSERT_EXPR and
6906 in the condition. In that case, we can copy the
6907 range info from x_8 computed in this pass also
6910 && all_imm_uses_in_stmt_or_feed_cond (var, stmt,
6913 set_range_info (var, SSA_NAME_RANGE_TYPE (lhs),
6914 SSA_NAME_RANGE_INFO (lhs)->get_min (),
6915 SSA_NAME_RANGE_INFO (lhs)->get_max ());
6916 maybe_set_nonzero_bits (bb, var);
6920 /* Propagate the RHS into every use of the LHS. */
6921 FOR_EACH_IMM_USE_STMT (use_stmt, iter, lhs)
6922 FOR_EACH_IMM_USE_ON_STMT (use_p, iter)
6923 SET_USE (use_p, var);
6925 /* And finally, remove the copy, it is not needed. */
6926 gsi_remove (&si, true);
6927 release_defs (stmt);
6931 if (!is_gimple_debug (gsi_stmt (si)))
6939 /* Return true if STMT is interesting for VRP. */
6942 stmt_interesting_for_vrp (gimple *stmt)
6944 if (gimple_code (stmt) == GIMPLE_PHI)
6946 tree res = gimple_phi_result (stmt);
6947 return (!virtual_operand_p (res)
6948 && (INTEGRAL_TYPE_P (TREE_TYPE (res))
6949 || POINTER_TYPE_P (TREE_TYPE (res))));
6951 else if (is_gimple_assign (stmt) || is_gimple_call (stmt))
6953 tree lhs = gimple_get_lhs (stmt);
6955 /* In general, assignments with virtual operands are not useful
6956 for deriving ranges, with the obvious exception of calls to
6957 builtin functions. */
6958 if (lhs && TREE_CODE (lhs) == SSA_NAME
6959 && (INTEGRAL_TYPE_P (TREE_TYPE (lhs))
6960 || POINTER_TYPE_P (TREE_TYPE (lhs)))
6961 && (is_gimple_call (stmt)
6962 || !gimple_vuse (stmt)))
6964 else if (is_gimple_call (stmt) && gimple_call_internal_p (stmt))
6965 switch (gimple_call_internal_fn (stmt))
6967 case IFN_ADD_OVERFLOW:
6968 case IFN_SUB_OVERFLOW:
6969 case IFN_MUL_OVERFLOW:
6970 /* These internal calls return _Complex integer type,
6971 but are interesting to VRP nevertheless. */
6972 if (lhs && TREE_CODE (lhs) == SSA_NAME)
6979 else if (gimple_code (stmt) == GIMPLE_COND
6980 || gimple_code (stmt) == GIMPLE_SWITCH)
6987 /* Initialize local data structures for VRP. */
6990 vrp_initialize (void)
6994 values_propagated = false;
6995 num_vr_values = num_ssa_names;
6996 vr_value = XCNEWVEC (value_range *, num_vr_values);
6997 vr_phi_edge_counts = XCNEWVEC (int, num_ssa_names);
6999 FOR_EACH_BB_FN (bb, cfun)
7001 for (gphi_iterator si = gsi_start_phis (bb); !gsi_end_p (si);
7004 gphi *phi = si.phi ();
7005 if (!stmt_interesting_for_vrp (phi))
7007 tree lhs = PHI_RESULT (phi);
7008 set_value_range_to_varying (get_value_range (lhs));
7009 prop_set_simulate_again (phi, false);
7012 prop_set_simulate_again (phi, true);
7015 for (gimple_stmt_iterator si = gsi_start_bb (bb); !gsi_end_p (si);
7018 gimple *stmt = gsi_stmt (si);
7020 /* If the statement is a control insn, then we do not
7021 want to avoid simulating the statement once. Failure
7022 to do so means that those edges will never get added. */
7023 if (stmt_ends_bb_p (stmt))
7024 prop_set_simulate_again (stmt, true);
7025 else if (!stmt_interesting_for_vrp (stmt))
7027 set_defs_to_varying (stmt);
7028 prop_set_simulate_again (stmt, false);
7031 prop_set_simulate_again (stmt, true);
7036 /* Return the singleton value-range for NAME or NAME. */
7039 vrp_valueize (tree name)
7041 if (TREE_CODE (name) == SSA_NAME)
7043 value_range *vr = get_value_range (name);
7044 if (vr->type == VR_RANGE
7045 && vrp_operand_equal_p (vr->min, vr->max))
7051 /* Return the singleton value-range for NAME if that is a constant
7052 but signal to not follow SSA edges. */
7055 vrp_valueize_1 (tree name)
7057 if (TREE_CODE (name) == SSA_NAME)
7059 /* If the definition may be simulated again we cannot follow
7060 this SSA edge as the SSA propagator does not necessarily
7061 re-visit the use. */
7062 gimple *def_stmt = SSA_NAME_DEF_STMT (name);
7063 if (!gimple_nop_p (def_stmt)
7064 && prop_simulate_again_p (def_stmt))
7066 value_range *vr = get_value_range (name);
7067 if (range_int_cst_singleton_p (vr))
7073 /* Visit assignment STMT. If it produces an interesting range, record
7074 the SSA name in *OUTPUT_P. */
7076 static enum ssa_prop_result
7077 vrp_visit_assignment_or_call (gimple *stmt, tree *output_p)
7080 enum gimple_code code = gimple_code (stmt);
7081 lhs = gimple_get_lhs (stmt);
7083 /* We only keep track of ranges in integral and pointer types. */
7084 if (TREE_CODE (lhs) == SSA_NAME
7085 && ((INTEGRAL_TYPE_P (TREE_TYPE (lhs))
7086 /* It is valid to have NULL MIN/MAX values on a type. See
7087 build_range_type. */
7088 && TYPE_MIN_VALUE (TREE_TYPE (lhs))
7089 && TYPE_MAX_VALUE (TREE_TYPE (lhs)))
7090 || POINTER_TYPE_P (TREE_TYPE (lhs))))
7092 value_range new_vr = VR_INITIALIZER;
7094 /* Try folding the statement to a constant first. */
7095 tree tem = gimple_fold_stmt_to_constant_1 (stmt, vrp_valueize,
7097 if (tem && is_gimple_min_invariant (tem))
7098 set_value_range_to_value (&new_vr, tem, NULL);
7099 /* Then dispatch to value-range extracting functions. */
7100 else if (code == GIMPLE_CALL)
7101 extract_range_basic (&new_vr, stmt);
7103 extract_range_from_assignment (&new_vr, as_a <gassign *> (stmt));
7105 if (update_value_range (lhs, &new_vr))
7109 if (dump_file && (dump_flags & TDF_DETAILS))
7111 fprintf (dump_file, "Found new range for ");
7112 print_generic_expr (dump_file, lhs, 0);
7113 fprintf (dump_file, ": ");
7114 dump_value_range (dump_file, &new_vr);
7115 fprintf (dump_file, "\n");
7118 if (new_vr.type == VR_VARYING)
7119 return SSA_PROP_VARYING;
7121 return SSA_PROP_INTERESTING;
7124 return SSA_PROP_NOT_INTERESTING;
7126 else if (is_gimple_call (stmt) && gimple_call_internal_p (stmt))
7127 switch (gimple_call_internal_fn (stmt))
7129 case IFN_ADD_OVERFLOW:
7130 case IFN_SUB_OVERFLOW:
7131 case IFN_MUL_OVERFLOW:
7132 /* These internal calls return _Complex integer type,
7133 which VRP does not track, but the immediate uses
7134 thereof might be interesting. */
7135 if (lhs && TREE_CODE (lhs) == SSA_NAME)
7137 imm_use_iterator iter;
7138 use_operand_p use_p;
7139 enum ssa_prop_result res = SSA_PROP_VARYING;
7141 set_value_range_to_varying (get_value_range (lhs));
7143 FOR_EACH_IMM_USE_FAST (use_p, iter, lhs)
7145 gimple *use_stmt = USE_STMT (use_p);
7146 if (!is_gimple_assign (use_stmt))
7148 enum tree_code rhs_code = gimple_assign_rhs_code (use_stmt);
7149 if (rhs_code != REALPART_EXPR && rhs_code != IMAGPART_EXPR)
7151 tree rhs1 = gimple_assign_rhs1 (use_stmt);
7152 tree use_lhs = gimple_assign_lhs (use_stmt);
7153 if (TREE_CODE (rhs1) != rhs_code
7154 || TREE_OPERAND (rhs1, 0) != lhs
7155 || TREE_CODE (use_lhs) != SSA_NAME
7156 || !stmt_interesting_for_vrp (use_stmt)
7157 || (!INTEGRAL_TYPE_P (TREE_TYPE (use_lhs))
7158 || !TYPE_MIN_VALUE (TREE_TYPE (use_lhs))
7159 || !TYPE_MAX_VALUE (TREE_TYPE (use_lhs))))
7162 /* If there is a change in the value range for any of the
7163 REALPART_EXPR/IMAGPART_EXPR immediate uses, return
7164 SSA_PROP_INTERESTING. If there are any REALPART_EXPR
7165 or IMAGPART_EXPR immediate uses, but none of them have
7166 a change in their value ranges, return
7167 SSA_PROP_NOT_INTERESTING. If there are no
7168 {REAL,IMAG}PART_EXPR uses at all,
7169 return SSA_PROP_VARYING. */
7170 value_range new_vr = VR_INITIALIZER;
7171 extract_range_basic (&new_vr, use_stmt);
7172 value_range *old_vr = get_value_range (use_lhs);
7173 if (old_vr->type != new_vr.type
7174 || !vrp_operand_equal_p (old_vr->min, new_vr.min)
7175 || !vrp_operand_equal_p (old_vr->max, new_vr.max)
7176 || !vrp_bitmap_equal_p (old_vr->equiv, new_vr.equiv))
7177 res = SSA_PROP_INTERESTING;
7179 res = SSA_PROP_NOT_INTERESTING;
7180 BITMAP_FREE (new_vr.equiv);
7181 if (res == SSA_PROP_INTERESTING)
7195 /* Every other statement produces no useful ranges. */
7196 set_defs_to_varying (stmt);
7198 return SSA_PROP_VARYING;
7201 /* Helper that gets the value range of the SSA_NAME with version I
7202 or a symbolic range containing the SSA_NAME only if the value range
7203 is varying or undefined. */
7205 static inline value_range
7206 get_vr_for_comparison (int i)
7208 value_range vr = *get_value_range (ssa_name (i));
7210 /* If name N_i does not have a valid range, use N_i as its own
7211 range. This allows us to compare against names that may
7212 have N_i in their ranges. */
7213 if (vr.type == VR_VARYING || vr.type == VR_UNDEFINED)
7216 vr.min = ssa_name (i);
7217 vr.max = ssa_name (i);
7223 /* Compare all the value ranges for names equivalent to VAR with VAL
7224 using comparison code COMP. Return the same value returned by
7225 compare_range_with_value, including the setting of
7226 *STRICT_OVERFLOW_P. */
7229 compare_name_with_value (enum tree_code comp, tree var, tree val,
7230 bool *strict_overflow_p, bool use_equiv_p)
7236 int used_strict_overflow;
7238 value_range equiv_vr;
7240 /* Get the set of equivalences for VAR. */
7241 e = get_value_range (var)->equiv;
7243 /* Start at -1. Set it to 0 if we do a comparison without relying
7244 on overflow, or 1 if all comparisons rely on overflow. */
7245 used_strict_overflow = -1;
7247 /* Compare vars' value range with val. */
7248 equiv_vr = get_vr_for_comparison (SSA_NAME_VERSION (var));
7250 retval = compare_range_with_value (comp, &equiv_vr, val, &sop);
7252 used_strict_overflow = sop ? 1 : 0;
7254 /* If the equiv set is empty we have done all work we need to do. */
7258 && used_strict_overflow > 0)
7259 *strict_overflow_p = true;
7263 EXECUTE_IF_SET_IN_BITMAP (e, 0, i, bi)
7266 && ! SSA_NAME_IS_DEFAULT_DEF (ssa_name (i))
7267 && prop_simulate_again_p (SSA_NAME_DEF_STMT (ssa_name (i))))
7270 equiv_vr = get_vr_for_comparison (i);
7272 t = compare_range_with_value (comp, &equiv_vr, val, &sop);
7275 /* If we get different answers from different members
7276 of the equivalence set this check must be in a dead
7277 code region. Folding it to a trap representation
7278 would be correct here. For now just return don't-know. */
7288 used_strict_overflow = 0;
7289 else if (used_strict_overflow < 0)
7290 used_strict_overflow = 1;
7295 && used_strict_overflow > 0)
7296 *strict_overflow_p = true;
7302 /* Given a comparison code COMP and names N1 and N2, compare all the
7303 ranges equivalent to N1 against all the ranges equivalent to N2
7304 to determine the value of N1 COMP N2. Return the same value
7305 returned by compare_ranges. Set *STRICT_OVERFLOW_P to indicate
7306 whether we relied on an overflow infinity in the comparison. */
7310 compare_names (enum tree_code comp, tree n1, tree n2,
7311 bool *strict_overflow_p)
7315 bitmap_iterator bi1, bi2;
7317 int used_strict_overflow;
7318 static bitmap_obstack *s_obstack = NULL;
7319 static bitmap s_e1 = NULL, s_e2 = NULL;
7321 /* Compare the ranges of every name equivalent to N1 against the
7322 ranges of every name equivalent to N2. */
7323 e1 = get_value_range (n1)->equiv;
7324 e2 = get_value_range (n2)->equiv;
7326 /* Use the fake bitmaps if e1 or e2 are not available. */
7327 if (s_obstack == NULL)
7329 s_obstack = XNEW (bitmap_obstack);
7330 bitmap_obstack_initialize (s_obstack);
7331 s_e1 = BITMAP_ALLOC (s_obstack);
7332 s_e2 = BITMAP_ALLOC (s_obstack);
7339 /* Add N1 and N2 to their own set of equivalences to avoid
7340 duplicating the body of the loop just to check N1 and N2
7342 bitmap_set_bit (e1, SSA_NAME_VERSION (n1));
7343 bitmap_set_bit (e2, SSA_NAME_VERSION (n2));
7345 /* If the equivalence sets have a common intersection, then the two
7346 names can be compared without checking their ranges. */
7347 if (bitmap_intersect_p (e1, e2))
7349 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
7350 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
7352 return (comp == EQ_EXPR || comp == GE_EXPR || comp == LE_EXPR)
7354 : boolean_false_node;
7357 /* Start at -1. Set it to 0 if we do a comparison without relying
7358 on overflow, or 1 if all comparisons rely on overflow. */
7359 used_strict_overflow = -1;
7361 /* Otherwise, compare all the equivalent ranges. First, add N1 and
7362 N2 to their own set of equivalences to avoid duplicating the body
7363 of the loop just to check N1 and N2 ranges. */
7364 EXECUTE_IF_SET_IN_BITMAP (e1, 0, i1, bi1)
7366 value_range vr1 = get_vr_for_comparison (i1);
7368 t = retval = NULL_TREE;
7369 EXECUTE_IF_SET_IN_BITMAP (e2, 0, i2, bi2)
7373 value_range vr2 = get_vr_for_comparison (i2);
7375 t = compare_ranges (comp, &vr1, &vr2, &sop);
7378 /* If we get different answers from different members
7379 of the equivalence set this check must be in a dead
7380 code region. Folding it to a trap representation
7381 would be correct here. For now just return don't-know. */
7385 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
7386 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
7392 used_strict_overflow = 0;
7393 else if (used_strict_overflow < 0)
7394 used_strict_overflow = 1;
7400 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
7401 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
7402 if (used_strict_overflow > 0)
7403 *strict_overflow_p = true;
7408 /* None of the equivalent ranges are useful in computing this
7410 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
7411 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
7415 /* Helper function for vrp_evaluate_conditional_warnv & other
7419 vrp_evaluate_conditional_warnv_with_ops_using_ranges (enum tree_code code,
7421 bool * strict_overflow_p)
7423 value_range *vr0, *vr1;
7425 vr0 = (TREE_CODE (op0) == SSA_NAME) ? get_value_range (op0) : NULL;
7426 vr1 = (TREE_CODE (op1) == SSA_NAME) ? get_value_range (op1) : NULL;
7428 tree res = NULL_TREE;
7430 res = compare_ranges (code, vr0, vr1, strict_overflow_p);
7432 res = compare_range_with_value (code, vr0, op1, strict_overflow_p);
7434 res = (compare_range_with_value
7435 (swap_tree_comparison (code), vr1, op0, strict_overflow_p));
7439 /* Helper function for vrp_evaluate_conditional_warnv. */
7442 vrp_evaluate_conditional_warnv_with_ops (enum tree_code code, tree op0,
7443 tree op1, bool use_equiv_p,
7444 bool *strict_overflow_p, bool *only_ranges)
7448 *only_ranges = true;
7450 /* We only deal with integral and pointer types. */
7451 if (!INTEGRAL_TYPE_P (TREE_TYPE (op0))
7452 && !POINTER_TYPE_P (TREE_TYPE (op0)))
7455 if ((ret = vrp_evaluate_conditional_warnv_with_ops_using_ranges
7456 (code, op0, op1, strict_overflow_p)))
7459 *only_ranges = false;
7460 /* Do not use compare_names during propagation, it's quadratic. */
7461 if (TREE_CODE (op0) == SSA_NAME && TREE_CODE (op1) == SSA_NAME
7463 return compare_names (code, op0, op1, strict_overflow_p);
7464 else if (TREE_CODE (op0) == SSA_NAME)
7465 return compare_name_with_value (code, op0, op1,
7466 strict_overflow_p, use_equiv_p);
7467 else if (TREE_CODE (op1) == SSA_NAME)
7468 return compare_name_with_value (swap_tree_comparison (code), op1, op0,
7469 strict_overflow_p, use_equiv_p);
7473 /* Given (CODE OP0 OP1) within STMT, try to simplify it based on value range
7474 information. Return NULL if the conditional can not be evaluated.
7475 The ranges of all the names equivalent with the operands in COND
7476 will be used when trying to compute the value. If the result is
7477 based on undefined signed overflow, issue a warning if
7481 vrp_evaluate_conditional (tree_code code, tree op0, tree op1, gimple *stmt)
7487 /* Some passes and foldings leak constants with overflow flag set
7488 into the IL. Avoid doing wrong things with these and bail out. */
7489 if ((TREE_CODE (op0) == INTEGER_CST
7490 && TREE_OVERFLOW (op0))
7491 || (TREE_CODE (op1) == INTEGER_CST
7492 && TREE_OVERFLOW (op1)))
7496 ret = vrp_evaluate_conditional_warnv_with_ops (code, op0, op1, true, &sop,
7501 enum warn_strict_overflow_code wc;
7502 const char* warnmsg;
7504 if (is_gimple_min_invariant (ret))
7506 wc = WARN_STRICT_OVERFLOW_CONDITIONAL;
7507 warnmsg = G_("assuming signed overflow does not occur when "
7508 "simplifying conditional to constant");
7512 wc = WARN_STRICT_OVERFLOW_COMPARISON;
7513 warnmsg = G_("assuming signed overflow does not occur when "
7514 "simplifying conditional");
7517 if (issue_strict_overflow_warning (wc))
7519 location_t location;
7521 if (!gimple_has_location (stmt))
7522 location = input_location;
7524 location = gimple_location (stmt);
7525 warning_at (location, OPT_Wstrict_overflow, "%s", warnmsg);
7529 if (warn_type_limits
7530 && ret && only_ranges
7531 && TREE_CODE_CLASS (code) == tcc_comparison
7532 && TREE_CODE (op0) == SSA_NAME)
7534 /* If the comparison is being folded and the operand on the LHS
7535 is being compared against a constant value that is outside of
7536 the natural range of OP0's type, then the predicate will
7537 always fold regardless of the value of OP0. If -Wtype-limits
7538 was specified, emit a warning. */
7539 tree type = TREE_TYPE (op0);
7540 value_range *vr0 = get_value_range (op0);
7542 if (vr0->type == VR_RANGE
7543 && INTEGRAL_TYPE_P (type)
7544 && vrp_val_is_min (vr0->min)
7545 && vrp_val_is_max (vr0->max)
7546 && is_gimple_min_invariant (op1))
7548 location_t location;
7550 if (!gimple_has_location (stmt))
7551 location = input_location;
7553 location = gimple_location (stmt);
7555 warning_at (location, OPT_Wtype_limits,
7557 ? G_("comparison always false "
7558 "due to limited range of data type")
7559 : G_("comparison always true "
7560 "due to limited range of data type"));
7568 /* Visit conditional statement STMT. If we can determine which edge
7569 will be taken out of STMT's basic block, record it in
7570 *TAKEN_EDGE_P and return SSA_PROP_INTERESTING. Otherwise, return
7571 SSA_PROP_VARYING. */
7573 static enum ssa_prop_result
7574 vrp_visit_cond_stmt (gcond *stmt, edge *taken_edge_p)
7579 *taken_edge_p = NULL;
7581 if (dump_file && (dump_flags & TDF_DETAILS))
7586 fprintf (dump_file, "\nVisiting conditional with predicate: ");
7587 print_gimple_stmt (dump_file, stmt, 0, 0);
7588 fprintf (dump_file, "\nWith known ranges\n");
7590 FOR_EACH_SSA_TREE_OPERAND (use, stmt, i, SSA_OP_USE)
7592 fprintf (dump_file, "\t");
7593 print_generic_expr (dump_file, use, 0);
7594 fprintf (dump_file, ": ");
7595 dump_value_range (dump_file, vr_value[SSA_NAME_VERSION (use)]);
7598 fprintf (dump_file, "\n");
7601 /* Compute the value of the predicate COND by checking the known
7602 ranges of each of its operands.
7604 Note that we cannot evaluate all the equivalent ranges here
7605 because those ranges may not yet be final and with the current
7606 propagation strategy, we cannot determine when the value ranges
7607 of the names in the equivalence set have changed.
7609 For instance, given the following code fragment
7613 i_14 = ASSERT_EXPR <i_5, i_5 != 0>
7617 Assume that on the first visit to i_14, i_5 has the temporary
7618 range [8, 8] because the second argument to the PHI function is
7619 not yet executable. We derive the range ~[0, 0] for i_14 and the
7620 equivalence set { i_5 }. So, when we visit 'if (i_14 == 1)' for
7621 the first time, since i_14 is equivalent to the range [8, 8], we
7622 determine that the predicate is always false.
7624 On the next round of propagation, i_13 is determined to be
7625 VARYING, which causes i_5 to drop down to VARYING. So, another
7626 visit to i_14 is scheduled. In this second visit, we compute the
7627 exact same range and equivalence set for i_14, namely ~[0, 0] and
7628 { i_5 }. But we did not have the previous range for i_5
7629 registered, so vrp_visit_assignment thinks that the range for
7630 i_14 has not changed. Therefore, the predicate 'if (i_14 == 1)'
7631 is not visited again, which stops propagation from visiting
7632 statements in the THEN clause of that if().
7634 To properly fix this we would need to keep the previous range
7635 value for the names in the equivalence set. This way we would've
7636 discovered that from one visit to the other i_5 changed from
7637 range [8, 8] to VR_VARYING.
7639 However, fixing this apparent limitation may not be worth the
7640 additional checking. Testing on several code bases (GCC, DLV,
7641 MICO, TRAMP3D and SPEC2000) showed that doing this results in
7642 4 more predicates folded in SPEC. */
7645 val = vrp_evaluate_conditional_warnv_with_ops (gimple_cond_code (stmt),
7646 gimple_cond_lhs (stmt),
7647 gimple_cond_rhs (stmt),
7652 *taken_edge_p = find_taken_edge (gimple_bb (stmt), val);
7655 if (dump_file && (dump_flags & TDF_DETAILS))
7657 "\nIgnoring predicate evaluation because "
7658 "it assumes that signed overflow is undefined");
7663 if (dump_file && (dump_flags & TDF_DETAILS))
7665 fprintf (dump_file, "\nPredicate evaluates to: ");
7666 if (val == NULL_TREE)
7667 fprintf (dump_file, "DON'T KNOW\n");
7669 print_generic_stmt (dump_file, val, 0);
7672 return (*taken_edge_p) ? SSA_PROP_INTERESTING : SSA_PROP_VARYING;
7675 /* Searches the case label vector VEC for the index *IDX of the CASE_LABEL
7676 that includes the value VAL. The search is restricted to the range
7677 [START_IDX, n - 1] where n is the size of VEC.
7679 If there is a CASE_LABEL for VAL, its index is placed in IDX and true is
7682 If there is no CASE_LABEL for VAL and there is one that is larger than VAL,
7683 it is placed in IDX and false is returned.
7685 If VAL is larger than any CASE_LABEL, n is placed on IDX and false is
7689 find_case_label_index (gswitch *stmt, size_t start_idx, tree val, size_t *idx)
7691 size_t n = gimple_switch_num_labels (stmt);
7694 /* Find case label for minimum of the value range or the next one.
7695 At each iteration we are searching in [low, high - 1]. */
7697 for (low = start_idx, high = n; high != low; )
7701 /* Note that i != high, so we never ask for n. */
7702 size_t i = (high + low) / 2;
7703 t = gimple_switch_label (stmt, i);
7705 /* Cache the result of comparing CASE_LOW and val. */
7706 cmp = tree_int_cst_compare (CASE_LOW (t), val);
7710 /* Ranges cannot be empty. */
7719 if (CASE_HIGH (t) != NULL
7720 && tree_int_cst_compare (CASE_HIGH (t), val) >= 0)
7732 /* Searches the case label vector VEC for the range of CASE_LABELs that is used
7733 for values between MIN and MAX. The first index is placed in MIN_IDX. The
7734 last index is placed in MAX_IDX. If the range of CASE_LABELs is empty
7735 then MAX_IDX < MIN_IDX.
7736 Returns true if the default label is not needed. */
7739 find_case_label_range (gswitch *stmt, tree min, tree max, size_t *min_idx,
7743 bool min_take_default = !find_case_label_index (stmt, 1, min, &i);
7744 bool max_take_default = !find_case_label_index (stmt, i, max, &j);
7748 && max_take_default)
7750 /* Only the default case label reached.
7751 Return an empty range. */
7758 bool take_default = min_take_default || max_take_default;
7762 if (max_take_default)
7765 /* If the case label range is continuous, we do not need
7766 the default case label. Verify that. */
7767 high = CASE_LOW (gimple_switch_label (stmt, i));
7768 if (CASE_HIGH (gimple_switch_label (stmt, i)))
7769 high = CASE_HIGH (gimple_switch_label (stmt, i));
7770 for (k = i + 1; k <= j; ++k)
7772 low = CASE_LOW (gimple_switch_label (stmt, k));
7773 if (!integer_onep (int_const_binop (MINUS_EXPR, low, high)))
7775 take_default = true;
7779 if (CASE_HIGH (gimple_switch_label (stmt, k)))
7780 high = CASE_HIGH (gimple_switch_label (stmt, k));
7785 return !take_default;
7789 /* Searches the case label vector VEC for the ranges of CASE_LABELs that are
7790 used in range VR. The indices are placed in MIN_IDX1, MAX_IDX, MIN_IDX2 and
7791 MAX_IDX2. If the ranges of CASE_LABELs are empty then MAX_IDX1 < MIN_IDX1.
7792 Returns true if the default label is not needed. */
7795 find_case_label_ranges (gswitch *stmt, value_range *vr, size_t *min_idx1,
7796 size_t *max_idx1, size_t *min_idx2,
7800 unsigned int n = gimple_switch_num_labels (stmt);
7802 tree case_low, case_high;
7803 tree min = vr->min, max = vr->max;
7805 gcc_checking_assert (vr->type == VR_RANGE || vr->type == VR_ANTI_RANGE);
7807 take_default = !find_case_label_range (stmt, min, max, &i, &j);
7809 /* Set second range to emtpy. */
7813 if (vr->type == VR_RANGE)
7817 return !take_default;
7820 /* Set first range to all case labels. */
7827 /* Make sure all the values of case labels [i , j] are contained in
7828 range [MIN, MAX]. */
7829 case_low = CASE_LOW (gimple_switch_label (stmt, i));
7830 case_high = CASE_HIGH (gimple_switch_label (stmt, j));
7831 if (tree_int_cst_compare (case_low, min) < 0)
7833 if (case_high != NULL_TREE
7834 && tree_int_cst_compare (max, case_high) < 0)
7840 /* If the range spans case labels [i, j], the corresponding anti-range spans
7841 the labels [1, i - 1] and [j + 1, n - 1]. */
7867 /* Visit switch statement STMT. If we can determine which edge
7868 will be taken out of STMT's basic block, record it in
7869 *TAKEN_EDGE_P and return SSA_PROP_INTERESTING. Otherwise, return
7870 SSA_PROP_VARYING. */
7872 static enum ssa_prop_result
7873 vrp_visit_switch_stmt (gswitch *stmt, edge *taken_edge_p)
7877 size_t i = 0, j = 0, k, l;
7880 *taken_edge_p = NULL;
7881 op = gimple_switch_index (stmt);
7882 if (TREE_CODE (op) != SSA_NAME)
7883 return SSA_PROP_VARYING;
7885 vr = get_value_range (op);
7886 if (dump_file && (dump_flags & TDF_DETAILS))
7888 fprintf (dump_file, "\nVisiting switch expression with operand ");
7889 print_generic_expr (dump_file, op, 0);
7890 fprintf (dump_file, " with known range ");
7891 dump_value_range (dump_file, vr);
7892 fprintf (dump_file, "\n");
7895 if ((vr->type != VR_RANGE
7896 && vr->type != VR_ANTI_RANGE)
7897 || symbolic_range_p (vr))
7898 return SSA_PROP_VARYING;
7900 /* Find the single edge that is taken from the switch expression. */
7901 take_default = !find_case_label_ranges (stmt, vr, &i, &j, &k, &l);
7903 /* Check if the range spans no CASE_LABEL. If so, we only reach the default
7907 gcc_assert (take_default);
7908 val = gimple_switch_default_label (stmt);
7912 /* Check if labels with index i to j and maybe the default label
7913 are all reaching the same label. */
7915 val = gimple_switch_label (stmt, i);
7917 && CASE_LABEL (gimple_switch_default_label (stmt))
7918 != CASE_LABEL (val))
7920 if (dump_file && (dump_flags & TDF_DETAILS))
7921 fprintf (dump_file, " not a single destination for this "
7923 return SSA_PROP_VARYING;
7925 for (++i; i <= j; ++i)
7927 if (CASE_LABEL (gimple_switch_label (stmt, i)) != CASE_LABEL (val))
7929 if (dump_file && (dump_flags & TDF_DETAILS))
7930 fprintf (dump_file, " not a single destination for this "
7932 return SSA_PROP_VARYING;
7937 if (CASE_LABEL (gimple_switch_label (stmt, k)) != CASE_LABEL (val))
7939 if (dump_file && (dump_flags & TDF_DETAILS))
7940 fprintf (dump_file, " not a single destination for this "
7942 return SSA_PROP_VARYING;
7947 *taken_edge_p = find_edge (gimple_bb (stmt),
7948 label_to_block (CASE_LABEL (val)));
7950 if (dump_file && (dump_flags & TDF_DETAILS))
7952 fprintf (dump_file, " will take edge to ");
7953 print_generic_stmt (dump_file, CASE_LABEL (val), 0);
7956 return SSA_PROP_INTERESTING;
7960 /* Evaluate statement STMT. If the statement produces a useful range,
7961 return SSA_PROP_INTERESTING and record the SSA name with the
7962 interesting range into *OUTPUT_P.
7964 If STMT is a conditional branch and we can determine its truth
7965 value, the taken edge is recorded in *TAKEN_EDGE_P.
7967 If STMT produces a varying value, return SSA_PROP_VARYING. */
7969 static enum ssa_prop_result
7970 vrp_visit_stmt (gimple *stmt, edge *taken_edge_p, tree *output_p)
7972 if (dump_file && (dump_flags & TDF_DETAILS))
7974 fprintf (dump_file, "\nVisiting statement:\n");
7975 print_gimple_stmt (dump_file, stmt, 0, dump_flags);
7978 if (!stmt_interesting_for_vrp (stmt))
7979 gcc_assert (stmt_ends_bb_p (stmt));
7980 else if (is_gimple_assign (stmt) || is_gimple_call (stmt))
7981 return vrp_visit_assignment_or_call (stmt, output_p);
7982 else if (gimple_code (stmt) == GIMPLE_COND)
7983 return vrp_visit_cond_stmt (as_a <gcond *> (stmt), taken_edge_p);
7984 else if (gimple_code (stmt) == GIMPLE_SWITCH)
7985 return vrp_visit_switch_stmt (as_a <gswitch *> (stmt), taken_edge_p);
7987 /* All other statements produce nothing of interest for VRP, so mark
7988 their outputs varying and prevent further simulation. */
7989 set_defs_to_varying (stmt);
7991 return SSA_PROP_VARYING;
7994 /* Union the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
7995 { VR1TYPE, VR0MIN, VR0MAX } and store the result
7996 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
7997 possible such range. The resulting range is not canonicalized. */
8000 union_ranges (enum value_range_type *vr0type,
8001 tree *vr0min, tree *vr0max,
8002 enum value_range_type vr1type,
8003 tree vr1min, tree vr1max)
8005 bool mineq = vrp_operand_equal_p (*vr0min, vr1min);
8006 bool maxeq = vrp_operand_equal_p (*vr0max, vr1max);
8008 /* [] is vr0, () is vr1 in the following classification comments. */
8012 if (*vr0type == vr1type)
8013 /* Nothing to do for equal ranges. */
8015 else if ((*vr0type == VR_RANGE
8016 && vr1type == VR_ANTI_RANGE)
8017 || (*vr0type == VR_ANTI_RANGE
8018 && vr1type == VR_RANGE))
8020 /* For anti-range with range union the result is varying. */
8026 else if (operand_less_p (*vr0max, vr1min) == 1
8027 || operand_less_p (vr1max, *vr0min) == 1)
8029 /* [ ] ( ) or ( ) [ ]
8030 If the ranges have an empty intersection, result of the union
8031 operation is the anti-range or if both are anti-ranges
8033 if (*vr0type == VR_ANTI_RANGE
8034 && vr1type == VR_ANTI_RANGE)
8036 else if (*vr0type == VR_ANTI_RANGE
8037 && vr1type == VR_RANGE)
8039 else if (*vr0type == VR_RANGE
8040 && vr1type == VR_ANTI_RANGE)
8046 else if (*vr0type == VR_RANGE
8047 && vr1type == VR_RANGE)
8049 /* The result is the convex hull of both ranges. */
8050 if (operand_less_p (*vr0max, vr1min) == 1)
8052 /* If the result can be an anti-range, create one. */
8053 if (TREE_CODE (*vr0max) == INTEGER_CST
8054 && TREE_CODE (vr1min) == INTEGER_CST
8055 && vrp_val_is_min (*vr0min)
8056 && vrp_val_is_max (vr1max))
8058 tree min = int_const_binop (PLUS_EXPR,
8060 build_int_cst (TREE_TYPE (*vr0max), 1));
8061 tree max = int_const_binop (MINUS_EXPR,
8063 build_int_cst (TREE_TYPE (vr1min), 1));
8064 if (!operand_less_p (max, min))
8066 *vr0type = VR_ANTI_RANGE;
8078 /* If the result can be an anti-range, create one. */
8079 if (TREE_CODE (vr1max) == INTEGER_CST
8080 && TREE_CODE (*vr0min) == INTEGER_CST
8081 && vrp_val_is_min (vr1min)
8082 && vrp_val_is_max (*vr0max))
8084 tree min = int_const_binop (PLUS_EXPR,
8086 build_int_cst (TREE_TYPE (vr1max), 1));
8087 tree max = int_const_binop (MINUS_EXPR,
8089 build_int_cst (TREE_TYPE (*vr0min), 1));
8090 if (!operand_less_p (max, min))
8092 *vr0type = VR_ANTI_RANGE;
8106 else if ((maxeq || operand_less_p (vr1max, *vr0max) == 1)
8107 && (mineq || operand_less_p (*vr0min, vr1min) == 1))
8109 /* [ ( ) ] or [( ) ] or [ ( )] */
8110 if (*vr0type == VR_RANGE
8111 && vr1type == VR_RANGE)
8113 else if (*vr0type == VR_ANTI_RANGE
8114 && vr1type == VR_ANTI_RANGE)
8120 else if (*vr0type == VR_ANTI_RANGE
8121 && vr1type == VR_RANGE)
8123 /* Arbitrarily choose the right or left gap. */
8124 if (!mineq && TREE_CODE (vr1min) == INTEGER_CST)
8125 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
8126 build_int_cst (TREE_TYPE (vr1min), 1));
8127 else if (!maxeq && TREE_CODE (vr1max) == INTEGER_CST)
8128 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
8129 build_int_cst (TREE_TYPE (vr1max), 1));
8133 else if (*vr0type == VR_RANGE
8134 && vr1type == VR_ANTI_RANGE)
8135 /* The result covers everything. */
8140 else if ((maxeq || operand_less_p (*vr0max, vr1max) == 1)
8141 && (mineq || operand_less_p (vr1min, *vr0min) == 1))
8143 /* ( [ ] ) or ([ ] ) or ( [ ]) */
8144 if (*vr0type == VR_RANGE
8145 && vr1type == VR_RANGE)
8151 else if (*vr0type == VR_ANTI_RANGE
8152 && vr1type == VR_ANTI_RANGE)
8154 else if (*vr0type == VR_RANGE
8155 && vr1type == VR_ANTI_RANGE)
8157 *vr0type = VR_ANTI_RANGE;
8158 if (!mineq && TREE_CODE (*vr0min) == INTEGER_CST)
8160 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
8161 build_int_cst (TREE_TYPE (*vr0min), 1));
8164 else if (!maxeq && TREE_CODE (*vr0max) == INTEGER_CST)
8166 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
8167 build_int_cst (TREE_TYPE (*vr0max), 1));
8173 else if (*vr0type == VR_ANTI_RANGE
8174 && vr1type == VR_RANGE)
8175 /* The result covers everything. */
8180 else if ((operand_less_p (vr1min, *vr0max) == 1
8181 || operand_equal_p (vr1min, *vr0max, 0))
8182 && operand_less_p (*vr0min, vr1min) == 1
8183 && operand_less_p (*vr0max, vr1max) == 1)
8185 /* [ ( ] ) or [ ]( ) */
8186 if (*vr0type == VR_RANGE
8187 && vr1type == VR_RANGE)
8189 else if (*vr0type == VR_ANTI_RANGE
8190 && vr1type == VR_ANTI_RANGE)
8192 else if (*vr0type == VR_ANTI_RANGE
8193 && vr1type == VR_RANGE)
8195 if (TREE_CODE (vr1min) == INTEGER_CST)
8196 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
8197 build_int_cst (TREE_TYPE (vr1min), 1));
8201 else if (*vr0type == VR_RANGE
8202 && vr1type == VR_ANTI_RANGE)
8204 if (TREE_CODE (*vr0max) == INTEGER_CST)
8207 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
8208 build_int_cst (TREE_TYPE (*vr0max), 1));
8217 else if ((operand_less_p (*vr0min, vr1max) == 1
8218 || operand_equal_p (*vr0min, vr1max, 0))
8219 && operand_less_p (vr1min, *vr0min) == 1
8220 && operand_less_p (vr1max, *vr0max) == 1)
8222 /* ( [ ) ] or ( )[ ] */
8223 if (*vr0type == VR_RANGE
8224 && vr1type == VR_RANGE)
8226 else if (*vr0type == VR_ANTI_RANGE
8227 && vr1type == VR_ANTI_RANGE)
8229 else if (*vr0type == VR_ANTI_RANGE
8230 && vr1type == VR_RANGE)
8232 if (TREE_CODE (vr1max) == INTEGER_CST)
8233 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
8234 build_int_cst (TREE_TYPE (vr1max), 1));
8238 else if (*vr0type == VR_RANGE
8239 && vr1type == VR_ANTI_RANGE)
8241 if (TREE_CODE (*vr0min) == INTEGER_CST)
8245 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
8246 build_int_cst (TREE_TYPE (*vr0min), 1));
8260 *vr0type = VR_VARYING;
8261 *vr0min = NULL_TREE;
8262 *vr0max = NULL_TREE;
8265 /* Intersect the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
8266 { VR1TYPE, VR0MIN, VR0MAX } and store the result
8267 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
8268 possible such range. The resulting range is not canonicalized. */
8271 intersect_ranges (enum value_range_type *vr0type,
8272 tree *vr0min, tree *vr0max,
8273 enum value_range_type vr1type,
8274 tree vr1min, tree vr1max)
8276 bool mineq = vrp_operand_equal_p (*vr0min, vr1min);
8277 bool maxeq = vrp_operand_equal_p (*vr0max, vr1max);
8279 /* [] is vr0, () is vr1 in the following classification comments. */
8283 if (*vr0type == vr1type)
8284 /* Nothing to do for equal ranges. */
8286 else if ((*vr0type == VR_RANGE
8287 && vr1type == VR_ANTI_RANGE)
8288 || (*vr0type == VR_ANTI_RANGE
8289 && vr1type == VR_RANGE))
8291 /* For anti-range with range intersection the result is empty. */
8292 *vr0type = VR_UNDEFINED;
8293 *vr0min = NULL_TREE;
8294 *vr0max = NULL_TREE;
8299 else if (operand_less_p (*vr0max, vr1min) == 1
8300 || operand_less_p (vr1max, *vr0min) == 1)
8302 /* [ ] ( ) or ( ) [ ]
8303 If the ranges have an empty intersection, the result of the
8304 intersect operation is the range for intersecting an
8305 anti-range with a range or empty when intersecting two ranges. */
8306 if (*vr0type == VR_RANGE
8307 && vr1type == VR_ANTI_RANGE)
8309 else if (*vr0type == VR_ANTI_RANGE
8310 && vr1type == VR_RANGE)
8316 else if (*vr0type == VR_RANGE
8317 && vr1type == VR_RANGE)
8319 *vr0type = VR_UNDEFINED;
8320 *vr0min = NULL_TREE;
8321 *vr0max = NULL_TREE;
8323 else if (*vr0type == VR_ANTI_RANGE
8324 && vr1type == VR_ANTI_RANGE)
8326 /* If the anti-ranges are adjacent to each other merge them. */
8327 if (TREE_CODE (*vr0max) == INTEGER_CST
8328 && TREE_CODE (vr1min) == INTEGER_CST
8329 && operand_less_p (*vr0max, vr1min) == 1
8330 && integer_onep (int_const_binop (MINUS_EXPR,
8333 else if (TREE_CODE (vr1max) == INTEGER_CST
8334 && TREE_CODE (*vr0min) == INTEGER_CST
8335 && operand_less_p (vr1max, *vr0min) == 1
8336 && integer_onep (int_const_binop (MINUS_EXPR,
8339 /* Else arbitrarily take VR0. */
8342 else if ((maxeq || operand_less_p (vr1max, *vr0max) == 1)
8343 && (mineq || operand_less_p (*vr0min, vr1min) == 1))
8345 /* [ ( ) ] or [( ) ] or [ ( )] */
8346 if (*vr0type == VR_RANGE
8347 && vr1type == VR_RANGE)
8349 /* If both are ranges the result is the inner one. */
8354 else if (*vr0type == VR_RANGE
8355 && vr1type == VR_ANTI_RANGE)
8357 /* Choose the right gap if the left one is empty. */
8360 if (TREE_CODE (vr1max) == INTEGER_CST)
8361 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
8362 build_int_cst (TREE_TYPE (vr1max), 1));
8366 /* Choose the left gap if the right one is empty. */
8369 if (TREE_CODE (vr1min) == INTEGER_CST)
8370 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
8371 build_int_cst (TREE_TYPE (vr1min), 1));
8375 /* Choose the anti-range if the range is effectively varying. */
8376 else if (vrp_val_is_min (*vr0min)
8377 && vrp_val_is_max (*vr0max))
8383 /* Else choose the range. */
8385 else if (*vr0type == VR_ANTI_RANGE
8386 && vr1type == VR_ANTI_RANGE)
8387 /* If both are anti-ranges the result is the outer one. */
8389 else if (*vr0type == VR_ANTI_RANGE
8390 && vr1type == VR_RANGE)
8392 /* The intersection is empty. */
8393 *vr0type = VR_UNDEFINED;
8394 *vr0min = NULL_TREE;
8395 *vr0max = NULL_TREE;
8400 else if ((maxeq || operand_less_p (*vr0max, vr1max) == 1)
8401 && (mineq || operand_less_p (vr1min, *vr0min) == 1))
8403 /* ( [ ] ) or ([ ] ) or ( [ ]) */
8404 if (*vr0type == VR_RANGE
8405 && vr1type == VR_RANGE)
8406 /* Choose the inner range. */
8408 else if (*vr0type == VR_ANTI_RANGE
8409 && vr1type == VR_RANGE)
8411 /* Choose the right gap if the left is empty. */
8414 *vr0type = VR_RANGE;
8415 if (TREE_CODE (*vr0max) == INTEGER_CST)
8416 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
8417 build_int_cst (TREE_TYPE (*vr0max), 1));
8422 /* Choose the left gap if the right is empty. */
8425 *vr0type = VR_RANGE;
8426 if (TREE_CODE (*vr0min) == INTEGER_CST)
8427 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
8428 build_int_cst (TREE_TYPE (*vr0min), 1));
8433 /* Choose the anti-range if the range is effectively varying. */
8434 else if (vrp_val_is_min (vr1min)
8435 && vrp_val_is_max (vr1max))
8437 /* Else choose the range. */
8445 else if (*vr0type == VR_ANTI_RANGE
8446 && vr1type == VR_ANTI_RANGE)
8448 /* If both are anti-ranges the result is the outer one. */
8453 else if (vr1type == VR_ANTI_RANGE
8454 && *vr0type == VR_RANGE)
8456 /* The intersection is empty. */
8457 *vr0type = VR_UNDEFINED;
8458 *vr0min = NULL_TREE;
8459 *vr0max = NULL_TREE;
8464 else if ((operand_less_p (vr1min, *vr0max) == 1
8465 || operand_equal_p (vr1min, *vr0max, 0))
8466 && operand_less_p (*vr0min, vr1min) == 1)
8468 /* [ ( ] ) or [ ]( ) */
8469 if (*vr0type == VR_ANTI_RANGE
8470 && vr1type == VR_ANTI_RANGE)
8472 else if (*vr0type == VR_RANGE
8473 && vr1type == VR_RANGE)
8475 else if (*vr0type == VR_RANGE
8476 && vr1type == VR_ANTI_RANGE)
8478 if (TREE_CODE (vr1min) == INTEGER_CST)
8479 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
8480 build_int_cst (TREE_TYPE (vr1min), 1));
8484 else if (*vr0type == VR_ANTI_RANGE
8485 && vr1type == VR_RANGE)
8487 *vr0type = VR_RANGE;
8488 if (TREE_CODE (*vr0max) == INTEGER_CST)
8489 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
8490 build_int_cst (TREE_TYPE (*vr0max), 1));
8498 else if ((operand_less_p (*vr0min, vr1max) == 1
8499 || operand_equal_p (*vr0min, vr1max, 0))
8500 && operand_less_p (vr1min, *vr0min) == 1)
8502 /* ( [ ) ] or ( )[ ] */
8503 if (*vr0type == VR_ANTI_RANGE
8504 && vr1type == VR_ANTI_RANGE)
8506 else if (*vr0type == VR_RANGE
8507 && vr1type == VR_RANGE)
8509 else if (*vr0type == VR_RANGE
8510 && vr1type == VR_ANTI_RANGE)
8512 if (TREE_CODE (vr1max) == INTEGER_CST)
8513 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
8514 build_int_cst (TREE_TYPE (vr1max), 1));
8518 else if (*vr0type == VR_ANTI_RANGE
8519 && vr1type == VR_RANGE)
8521 *vr0type = VR_RANGE;
8522 if (TREE_CODE (*vr0min) == INTEGER_CST)
8523 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
8524 build_int_cst (TREE_TYPE (*vr0min), 1));
8533 /* As a fallback simply use { *VRTYPE, *VR0MIN, *VR0MAX } as
8534 result for the intersection. That's always a conservative
8535 correct estimate. */
8541 /* Intersect the two value-ranges *VR0 and *VR1 and store the result
8542 in *VR0. This may not be the smallest possible such range. */
8545 vrp_intersect_ranges_1 (value_range *vr0, value_range *vr1)
8549 /* If either range is VR_VARYING the other one wins. */
8550 if (vr1->type == VR_VARYING)
8552 if (vr0->type == VR_VARYING)
8554 copy_value_range (vr0, vr1);
8558 /* When either range is VR_UNDEFINED the resulting range is
8559 VR_UNDEFINED, too. */
8560 if (vr0->type == VR_UNDEFINED)
8562 if (vr1->type == VR_UNDEFINED)
8564 set_value_range_to_undefined (vr0);
8568 /* Save the original vr0 so we can return it as conservative intersection
8569 result when our worker turns things to varying. */
8571 intersect_ranges (&vr0->type, &vr0->min, &vr0->max,
8572 vr1->type, vr1->min, vr1->max);
8573 /* Make sure to canonicalize the result though as the inversion of a
8574 VR_RANGE can still be a VR_RANGE. */
8575 set_and_canonicalize_value_range (vr0, vr0->type,
8576 vr0->min, vr0->max, vr0->equiv);
8577 /* If that failed, use the saved original VR0. */
8578 if (vr0->type == VR_VARYING)
8583 /* If the result is VR_UNDEFINED there is no need to mess with
8584 the equivalencies. */
8585 if (vr0->type == VR_UNDEFINED)
8588 /* The resulting set of equivalences for range intersection is the union of
8590 if (vr0->equiv && vr1->equiv && vr0->equiv != vr1->equiv)
8591 bitmap_ior_into (vr0->equiv, vr1->equiv);
8592 else if (vr1->equiv && !vr0->equiv)
8594 vr0->equiv = BITMAP_ALLOC (NULL);
8595 bitmap_copy (vr0->equiv, vr1->equiv);
8600 vrp_intersect_ranges (value_range *vr0, value_range *vr1)
8602 if (dump_file && (dump_flags & TDF_DETAILS))
8604 fprintf (dump_file, "Intersecting\n ");
8605 dump_value_range (dump_file, vr0);
8606 fprintf (dump_file, "\nand\n ");
8607 dump_value_range (dump_file, vr1);
8608 fprintf (dump_file, "\n");
8610 vrp_intersect_ranges_1 (vr0, vr1);
8611 if (dump_file && (dump_flags & TDF_DETAILS))
8613 fprintf (dump_file, "to\n ");
8614 dump_value_range (dump_file, vr0);
8615 fprintf (dump_file, "\n");
8619 /* Meet operation for value ranges. Given two value ranges VR0 and
8620 VR1, store in VR0 a range that contains both VR0 and VR1. This
8621 may not be the smallest possible such range. */
8624 vrp_meet_1 (value_range *vr0, value_range *vr1)
8628 if (vr0->type == VR_UNDEFINED)
8630 set_value_range (vr0, vr1->type, vr1->min, vr1->max, vr1->equiv);
8634 if (vr1->type == VR_UNDEFINED)
8636 /* VR0 already has the resulting range. */
8640 if (vr0->type == VR_VARYING)
8642 /* Nothing to do. VR0 already has the resulting range. */
8646 if (vr1->type == VR_VARYING)
8648 set_value_range_to_varying (vr0);
8653 union_ranges (&vr0->type, &vr0->min, &vr0->max,
8654 vr1->type, vr1->min, vr1->max);
8655 if (vr0->type == VR_VARYING)
8657 /* Failed to find an efficient meet. Before giving up and setting
8658 the result to VARYING, see if we can at least derive a useful
8659 anti-range. FIXME, all this nonsense about distinguishing
8660 anti-ranges from ranges is necessary because of the odd
8661 semantics of range_includes_zero_p and friends. */
8662 if (((saved.type == VR_RANGE
8663 && range_includes_zero_p (saved.min, saved.max) == 0)
8664 || (saved.type == VR_ANTI_RANGE
8665 && range_includes_zero_p (saved.min, saved.max) == 1))
8666 && ((vr1->type == VR_RANGE
8667 && range_includes_zero_p (vr1->min, vr1->max) == 0)
8668 || (vr1->type == VR_ANTI_RANGE
8669 && range_includes_zero_p (vr1->min, vr1->max) == 1)))
8671 set_value_range_to_nonnull (vr0, TREE_TYPE (saved.min));
8673 /* Since this meet operation did not result from the meeting of
8674 two equivalent names, VR0 cannot have any equivalences. */
8676 bitmap_clear (vr0->equiv);
8680 set_value_range_to_varying (vr0);
8683 set_and_canonicalize_value_range (vr0, vr0->type, vr0->min, vr0->max,
8685 if (vr0->type == VR_VARYING)
8688 /* The resulting set of equivalences is always the intersection of
8690 if (vr0->equiv && vr1->equiv && vr0->equiv != vr1->equiv)
8691 bitmap_and_into (vr0->equiv, vr1->equiv);
8692 else if (vr0->equiv && !vr1->equiv)
8693 bitmap_clear (vr0->equiv);
8697 vrp_meet (value_range *vr0, value_range *vr1)
8699 if (dump_file && (dump_flags & TDF_DETAILS))
8701 fprintf (dump_file, "Meeting\n ");
8702 dump_value_range (dump_file, vr0);
8703 fprintf (dump_file, "\nand\n ");
8704 dump_value_range (dump_file, vr1);
8705 fprintf (dump_file, "\n");
8707 vrp_meet_1 (vr0, vr1);
8708 if (dump_file && (dump_flags & TDF_DETAILS))
8710 fprintf (dump_file, "to\n ");
8711 dump_value_range (dump_file, vr0);
8712 fprintf (dump_file, "\n");
8717 /* Visit all arguments for PHI node PHI that flow through executable
8718 edges. If a valid value range can be derived from all the incoming
8719 value ranges, set a new range for the LHS of PHI. */
8721 static enum ssa_prop_result
8722 vrp_visit_phi_node (gphi *phi)
8725 tree lhs = PHI_RESULT (phi);
8726 value_range *lhs_vr = get_value_range (lhs);
8727 value_range vr_result = VR_INITIALIZER;
8729 int edges, old_edges;
8732 if (dump_file && (dump_flags & TDF_DETAILS))
8734 fprintf (dump_file, "\nVisiting PHI node: ");
8735 print_gimple_stmt (dump_file, phi, 0, dump_flags);
8739 for (i = 0; i < gimple_phi_num_args (phi); i++)
8741 edge e = gimple_phi_arg_edge (phi, i);
8743 if (dump_file && (dump_flags & TDF_DETAILS))
8746 " Argument #%d (%d -> %d %sexecutable)\n",
8747 (int) i, e->src->index, e->dest->index,
8748 (e->flags & EDGE_EXECUTABLE) ? "" : "not ");
8751 if (e->flags & EDGE_EXECUTABLE)
8753 tree arg = PHI_ARG_DEF (phi, i);
8758 if (TREE_CODE (arg) == SSA_NAME)
8760 vr_arg = *(get_value_range (arg));
8761 /* Do not allow equivalences or symbolic ranges to leak in from
8762 backedges. That creates invalid equivalencies.
8763 See PR53465 and PR54767. */
8764 if (e->flags & EDGE_DFS_BACK)
8766 if (vr_arg.type == VR_RANGE
8767 || vr_arg.type == VR_ANTI_RANGE)
8769 vr_arg.equiv = NULL;
8770 if (symbolic_range_p (&vr_arg))
8772 vr_arg.type = VR_VARYING;
8773 vr_arg.min = NULL_TREE;
8774 vr_arg.max = NULL_TREE;
8780 /* If the non-backedge arguments range is VR_VARYING then
8781 we can still try recording a simple equivalence. */
8782 if (vr_arg.type == VR_VARYING)
8784 vr_arg.type = VR_RANGE;
8787 vr_arg.equiv = NULL;
8793 if (TREE_OVERFLOW_P (arg))
8794 arg = drop_tree_overflow (arg);
8796 vr_arg.type = VR_RANGE;
8799 vr_arg.equiv = NULL;
8802 if (dump_file && (dump_flags & TDF_DETAILS))
8804 fprintf (dump_file, "\t");
8805 print_generic_expr (dump_file, arg, dump_flags);
8806 fprintf (dump_file, ": ");
8807 dump_value_range (dump_file, &vr_arg);
8808 fprintf (dump_file, "\n");
8812 copy_value_range (&vr_result, &vr_arg);
8814 vrp_meet (&vr_result, &vr_arg);
8817 if (vr_result.type == VR_VARYING)
8822 if (vr_result.type == VR_VARYING)
8824 else if (vr_result.type == VR_UNDEFINED)
8827 old_edges = vr_phi_edge_counts[SSA_NAME_VERSION (lhs)];
8828 vr_phi_edge_counts[SSA_NAME_VERSION (lhs)] = edges;
8830 /* To prevent infinite iterations in the algorithm, derive ranges
8831 when the new value is slightly bigger or smaller than the
8832 previous one. We don't do this if we have seen a new executable
8833 edge; this helps us avoid an overflow infinity for conditionals
8834 which are not in a loop. If the old value-range was VR_UNDEFINED
8835 use the updated range and iterate one more time. */
8837 && gimple_phi_num_args (phi) > 1
8838 && edges == old_edges
8839 && lhs_vr->type != VR_UNDEFINED)
8841 /* Compare old and new ranges, fall back to varying if the
8842 values are not comparable. */
8843 int cmp_min = compare_values (lhs_vr->min, vr_result.min);
8846 int cmp_max = compare_values (lhs_vr->max, vr_result.max);
8850 /* For non VR_RANGE or for pointers fall back to varying if
8851 the range changed. */
8852 if ((lhs_vr->type != VR_RANGE || vr_result.type != VR_RANGE
8853 || POINTER_TYPE_P (TREE_TYPE (lhs)))
8854 && (cmp_min != 0 || cmp_max != 0))
8857 /* If the new minimum is larger than the previous one
8858 retain the old value. If the new minimum value is smaller
8859 than the previous one and not -INF go all the way to -INF + 1.
8860 In the first case, to avoid infinite bouncing between different
8861 minimums, and in the other case to avoid iterating millions of
8862 times to reach -INF. Going to -INF + 1 also lets the following
8863 iteration compute whether there will be any overflow, at the
8864 expense of one additional iteration. */
8866 vr_result.min = lhs_vr->min;
8867 else if (cmp_min > 0
8868 && !vrp_val_is_min (vr_result.min))
8870 = int_const_binop (PLUS_EXPR,
8871 vrp_val_min (TREE_TYPE (vr_result.min)),
8872 build_int_cst (TREE_TYPE (vr_result.min), 1));
8874 /* Similarly for the maximum value. */
8876 vr_result.max = lhs_vr->max;
8877 else if (cmp_max < 0
8878 && !vrp_val_is_max (vr_result.max))
8880 = int_const_binop (MINUS_EXPR,
8881 vrp_val_max (TREE_TYPE (vr_result.min)),
8882 build_int_cst (TREE_TYPE (vr_result.min), 1));
8884 /* If we dropped either bound to +-INF then if this is a loop
8885 PHI node SCEV may known more about its value-range. */
8886 if (cmp_min > 0 || cmp_min < 0
8887 || cmp_max < 0 || cmp_max > 0)
8890 goto infinite_check;
8893 /* If the new range is different than the previous value, keep
8896 if (update_value_range (lhs, &vr_result))
8898 if (dump_file && (dump_flags & TDF_DETAILS))
8900 fprintf (dump_file, "Found new range for ");
8901 print_generic_expr (dump_file, lhs, 0);
8902 fprintf (dump_file, ": ");
8903 dump_value_range (dump_file, &vr_result);
8904 fprintf (dump_file, "\n");
8907 if (vr_result.type == VR_VARYING)
8908 return SSA_PROP_VARYING;
8910 return SSA_PROP_INTERESTING;
8913 /* Nothing changed, don't add outgoing edges. */
8914 return SSA_PROP_NOT_INTERESTING;
8917 set_value_range_to_varying (&vr_result);
8920 /* If this is a loop PHI node SCEV may known more about its value-range.
8921 scev_check can be reached from two paths, one is a fall through from above
8922 "varying" label, the other is direct goto from code block which tries to
8923 avoid infinite simulation. */
8924 if ((l = loop_containing_stmt (phi))
8925 && l->header == gimple_bb (phi))
8926 adjust_range_with_scev (&vr_result, l, phi, lhs);
8929 /* If we will end up with a (-INF, +INF) range, set it to
8930 VARYING. Same if the previous max value was invalid for
8931 the type and we end up with vr_result.min > vr_result.max. */
8932 if ((vr_result.type == VR_RANGE || vr_result.type == VR_ANTI_RANGE)
8933 && !((vrp_val_is_max (vr_result.max) && vrp_val_is_min (vr_result.min))
8934 || compare_values (vr_result.min, vr_result.max) > 0))
8937 /* No match found. Set the LHS to VARYING. */
8938 set_value_range_to_varying (lhs_vr);
8939 return SSA_PROP_VARYING;
8942 /* Simplify boolean operations if the source is known
8943 to be already a boolean. */
8945 simplify_truth_ops_using_ranges (gimple_stmt_iterator *gsi, gimple *stmt)
8947 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
8949 bool need_conversion;
8951 /* We handle only !=/== case here. */
8952 gcc_assert (rhs_code == EQ_EXPR || rhs_code == NE_EXPR);
8954 op0 = gimple_assign_rhs1 (stmt);
8955 if (!op_with_boolean_value_range_p (op0))
8958 op1 = gimple_assign_rhs2 (stmt);
8959 if (!op_with_boolean_value_range_p (op1))
8962 /* Reduce number of cases to handle to NE_EXPR. As there is no
8963 BIT_XNOR_EXPR we cannot replace A == B with a single statement. */
8964 if (rhs_code == EQ_EXPR)
8966 if (TREE_CODE (op1) == INTEGER_CST)
8967 op1 = int_const_binop (BIT_XOR_EXPR, op1,
8968 build_int_cst (TREE_TYPE (op1), 1));
8973 lhs = gimple_assign_lhs (stmt);
8975 = !useless_type_conversion_p (TREE_TYPE (lhs), TREE_TYPE (op0));
8977 /* Make sure to not sign-extend a 1-bit 1 when converting the result. */
8979 && !TYPE_UNSIGNED (TREE_TYPE (op0))
8980 && TYPE_PRECISION (TREE_TYPE (op0)) == 1
8981 && TYPE_PRECISION (TREE_TYPE (lhs)) > 1)
8984 /* For A != 0 we can substitute A itself. */
8985 if (integer_zerop (op1))
8986 gimple_assign_set_rhs_with_ops (gsi,
8988 ? NOP_EXPR : TREE_CODE (op0), op0);
8989 /* For A != B we substitute A ^ B. Either with conversion. */
8990 else if (need_conversion)
8992 tree tem = make_ssa_name (TREE_TYPE (op0));
8994 = gimple_build_assign (tem, BIT_XOR_EXPR, op0, op1);
8995 gsi_insert_before (gsi, newop, GSI_SAME_STMT);
8996 gimple_assign_set_rhs_with_ops (gsi, NOP_EXPR, tem);
9000 gimple_assign_set_rhs_with_ops (gsi, BIT_XOR_EXPR, op0, op1);
9001 update_stmt (gsi_stmt (*gsi));
9006 /* Simplify a division or modulo operator to a right shift or
9007 bitwise and if the first operand is unsigned or is greater
9008 than zero and the second operand is an exact power of two.
9009 For TRUNC_MOD_EXPR op0 % op1 with constant op1, optimize it
9010 into just op0 if op0's range is known to be a subset of
9011 [-op1 + 1, op1 - 1] for signed and [0, op1 - 1] for unsigned
9015 simplify_div_or_mod_using_ranges (gimple_stmt_iterator *gsi, gimple *stmt)
9017 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
9019 tree op0 = gimple_assign_rhs1 (stmt);
9020 tree op1 = gimple_assign_rhs2 (stmt);
9021 value_range *vr = get_value_range (op0);
9023 if (rhs_code == TRUNC_MOD_EXPR
9024 && TREE_CODE (op1) == INTEGER_CST
9025 && tree_int_cst_sgn (op1) == 1
9026 && range_int_cst_p (vr)
9027 && tree_int_cst_lt (vr->max, op1))
9029 if (TYPE_UNSIGNED (TREE_TYPE (op0))
9030 || tree_int_cst_sgn (vr->min) >= 0
9031 || tree_int_cst_lt (fold_unary (NEGATE_EXPR, TREE_TYPE (op1), op1),
9034 /* If op0 already has the range op0 % op1 has,
9035 then TRUNC_MOD_EXPR won't change anything. */
9036 gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
9037 gimple_assign_set_rhs_from_tree (&gsi, op0);
9043 if (!integer_pow2p (op1))
9045 /* X % -Y can be only optimized into X % Y either if
9046 X is not INT_MIN, or Y is not -1. Fold it now, as after
9047 remove_range_assertions the range info might be not available
9049 if (rhs_code == TRUNC_MOD_EXPR
9050 && fold_stmt (gsi, follow_single_use_edges))
9055 if (TYPE_UNSIGNED (TREE_TYPE (op0)))
9056 val = integer_one_node;
9061 val = compare_range_with_value (GE_EXPR, vr, integer_zero_node, &sop);
9065 && integer_onep (val)
9066 && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC))
9068 location_t location;
9070 if (!gimple_has_location (stmt))
9071 location = input_location;
9073 location = gimple_location (stmt);
9074 warning_at (location, OPT_Wstrict_overflow,
9075 "assuming signed overflow does not occur when "
9076 "simplifying %</%> or %<%%%> to %<>>%> or %<&%>");
9080 if (val && integer_onep (val))
9084 if (rhs_code == TRUNC_DIV_EXPR)
9086 t = build_int_cst (integer_type_node, tree_log2 (op1));
9087 gimple_assign_set_rhs_code (stmt, RSHIFT_EXPR);
9088 gimple_assign_set_rhs1 (stmt, op0);
9089 gimple_assign_set_rhs2 (stmt, t);
9093 t = build_int_cst (TREE_TYPE (op1), 1);
9094 t = int_const_binop (MINUS_EXPR, op1, t);
9095 t = fold_convert (TREE_TYPE (op0), t);
9097 gimple_assign_set_rhs_code (stmt, BIT_AND_EXPR);
9098 gimple_assign_set_rhs1 (stmt, op0);
9099 gimple_assign_set_rhs2 (stmt, t);
9109 /* Simplify a min or max if the ranges of the two operands are
9110 disjoint. Return true if we do simplify. */
9113 simplify_min_or_max_using_ranges (gimple *stmt)
9115 tree op0 = gimple_assign_rhs1 (stmt);
9116 tree op1 = gimple_assign_rhs2 (stmt);
9120 val = (vrp_evaluate_conditional_warnv_with_ops_using_ranges
9121 (LE_EXPR, op0, op1, &sop));
9125 val = (vrp_evaluate_conditional_warnv_with_ops_using_ranges
9126 (LT_EXPR, op0, op1, &sop));
9131 if (sop && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC))
9133 location_t location;
9135 if (!gimple_has_location (stmt))
9136 location = input_location;
9138 location = gimple_location (stmt);
9139 warning_at (location, OPT_Wstrict_overflow,
9140 "assuming signed overflow does not occur when "
9141 "simplifying %<min/max (X,Y)%> to %<X%> or %<Y%>");
9144 /* VAL == TRUE -> OP0 < or <= op1
9145 VAL == FALSE -> OP0 > or >= op1. */
9146 tree res = ((gimple_assign_rhs_code (stmt) == MAX_EXPR)
9147 == integer_zerop (val)) ? op0 : op1;
9148 gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
9149 gimple_assign_set_rhs_from_tree (&gsi, res);
9157 /* If the operand to an ABS_EXPR is >= 0, then eliminate the
9158 ABS_EXPR. If the operand is <= 0, then simplify the
9159 ABS_EXPR into a NEGATE_EXPR. */
9162 simplify_abs_using_ranges (gimple *stmt)
9164 tree op = gimple_assign_rhs1 (stmt);
9165 value_range *vr = get_value_range (op);
9172 val = compare_range_with_value (LE_EXPR, vr, integer_zero_node, &sop);
9175 /* The range is neither <= 0 nor > 0. Now see if it is
9176 either < 0 or >= 0. */
9178 val = compare_range_with_value (LT_EXPR, vr, integer_zero_node,
9184 if (sop && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC))
9186 location_t location;
9188 if (!gimple_has_location (stmt))
9189 location = input_location;
9191 location = gimple_location (stmt);
9192 warning_at (location, OPT_Wstrict_overflow,
9193 "assuming signed overflow does not occur when "
9194 "simplifying %<abs (X)%> to %<X%> or %<-X%>");
9197 gimple_assign_set_rhs1 (stmt, op);
9198 if (integer_zerop (val))
9199 gimple_assign_set_rhs_code (stmt, SSA_NAME);
9201 gimple_assign_set_rhs_code (stmt, NEGATE_EXPR);
9210 /* Optimize away redundant BIT_AND_EXPR and BIT_IOR_EXPR.
9211 If all the bits that are being cleared by & are already
9212 known to be zero from VR, or all the bits that are being
9213 set by | are already known to be one from VR, the bit
9214 operation is redundant. */
9217 simplify_bit_ops_using_ranges (gimple_stmt_iterator *gsi, gimple *stmt)
9219 tree op0 = gimple_assign_rhs1 (stmt);
9220 tree op1 = gimple_assign_rhs2 (stmt);
9221 tree op = NULL_TREE;
9222 value_range vr0 = VR_INITIALIZER;
9223 value_range vr1 = VR_INITIALIZER;
9224 wide_int may_be_nonzero0, may_be_nonzero1;
9225 wide_int must_be_nonzero0, must_be_nonzero1;
9228 if (TREE_CODE (op0) == SSA_NAME)
9229 vr0 = *(get_value_range (op0));
9230 else if (is_gimple_min_invariant (op0))
9231 set_value_range_to_value (&vr0, op0, NULL);
9235 if (TREE_CODE (op1) == SSA_NAME)
9236 vr1 = *(get_value_range (op1));
9237 else if (is_gimple_min_invariant (op1))
9238 set_value_range_to_value (&vr1, op1, NULL);
9242 if (!zero_nonzero_bits_from_vr (TREE_TYPE (op0), &vr0, &may_be_nonzero0,
9245 if (!zero_nonzero_bits_from_vr (TREE_TYPE (op1), &vr1, &may_be_nonzero1,
9249 switch (gimple_assign_rhs_code (stmt))
9252 mask = may_be_nonzero0.and_not (must_be_nonzero1);
9258 mask = may_be_nonzero1.and_not (must_be_nonzero0);
9266 mask = may_be_nonzero0.and_not (must_be_nonzero1);
9272 mask = may_be_nonzero1.and_not (must_be_nonzero0);
9283 if (op == NULL_TREE)
9286 gimple_assign_set_rhs_with_ops (gsi, TREE_CODE (op), op);
9287 update_stmt (gsi_stmt (*gsi));
9291 /* We are comparing trees OP0 and OP1 using COND_CODE. OP0 has
9292 a known value range VR.
9294 If there is one and only one value which will satisfy the
9295 conditional, then return that value. Else return NULL.
9297 If signed overflow must be undefined for the value to satisfy
9298 the conditional, then set *STRICT_OVERFLOW_P to true. */
9301 test_for_singularity (enum tree_code cond_code, tree op0,
9302 tree op1, value_range *vr,
9303 bool *strict_overflow_p)
9308 /* Extract minimum/maximum values which satisfy the conditional as it was
9310 if (cond_code == LE_EXPR || cond_code == LT_EXPR)
9312 /* This should not be negative infinity; there is no overflow
9314 min = TYPE_MIN_VALUE (TREE_TYPE (op0));
9317 if (cond_code == LT_EXPR && !is_overflow_infinity (max))
9319 tree one = build_int_cst (TREE_TYPE (op0), 1);
9320 max = fold_build2 (MINUS_EXPR, TREE_TYPE (op0), max, one);
9322 TREE_NO_WARNING (max) = 1;
9325 else if (cond_code == GE_EXPR || cond_code == GT_EXPR)
9327 /* This should not be positive infinity; there is no overflow
9329 max = TYPE_MAX_VALUE (TREE_TYPE (op0));
9332 if (cond_code == GT_EXPR && !is_overflow_infinity (min))
9334 tree one = build_int_cst (TREE_TYPE (op0), 1);
9335 min = fold_build2 (PLUS_EXPR, TREE_TYPE (op0), min, one);
9337 TREE_NO_WARNING (min) = 1;
9341 /* Now refine the minimum and maximum values using any
9342 value range information we have for op0. */
9345 if (compare_values (vr->min, min) == 1)
9347 if (compare_values (vr->max, max) == -1)
9350 /* If the new min/max values have converged to a single value,
9351 then there is only one value which can satisfy the condition,
9352 return that value. */
9353 if (operand_equal_p (min, max, 0) && is_gimple_min_invariant (min))
9355 if ((cond_code == LE_EXPR || cond_code == LT_EXPR)
9356 && is_overflow_infinity (vr->max))
9357 *strict_overflow_p = true;
9358 if ((cond_code == GE_EXPR || cond_code == GT_EXPR)
9359 && is_overflow_infinity (vr->min))
9360 *strict_overflow_p = true;
9368 /* Return whether the value range *VR fits in an integer type specified
9369 by PRECISION and UNSIGNED_P. */
9372 range_fits_type_p (value_range *vr, unsigned dest_precision, signop dest_sgn)
9375 unsigned src_precision;
9379 /* We can only handle integral and pointer types. */
9380 src_type = TREE_TYPE (vr->min);
9381 if (!INTEGRAL_TYPE_P (src_type)
9382 && !POINTER_TYPE_P (src_type))
9385 /* An extension is fine unless VR is SIGNED and dest_sgn is UNSIGNED,
9386 and so is an identity transform. */
9387 src_precision = TYPE_PRECISION (TREE_TYPE (vr->min));
9388 src_sgn = TYPE_SIGN (src_type);
9389 if ((src_precision < dest_precision
9390 && !(dest_sgn == UNSIGNED && src_sgn == SIGNED))
9391 || (src_precision == dest_precision && src_sgn == dest_sgn))
9394 /* Now we can only handle ranges with constant bounds. */
9395 if (vr->type != VR_RANGE
9396 || TREE_CODE (vr->min) != INTEGER_CST
9397 || TREE_CODE (vr->max) != INTEGER_CST)
9400 /* For sign changes, the MSB of the wide_int has to be clear.
9401 An unsigned value with its MSB set cannot be represented by
9402 a signed wide_int, while a negative value cannot be represented
9403 by an unsigned wide_int. */
9404 if (src_sgn != dest_sgn
9405 && (wi::lts_p (vr->min, 0) || wi::lts_p (vr->max, 0)))
9408 /* Then we can perform the conversion on both ends and compare
9409 the result for equality. */
9410 tem = wi::ext (wi::to_widest (vr->min), dest_precision, dest_sgn);
9411 if (tem != wi::to_widest (vr->min))
9413 tem = wi::ext (wi::to_widest (vr->max), dest_precision, dest_sgn);
9414 if (tem != wi::to_widest (vr->max))
9420 /* Simplify a conditional using a relational operator to an equality
9421 test if the range information indicates only one value can satisfy
9422 the original conditional. */
9425 simplify_cond_using_ranges (gcond *stmt)
9427 tree op0 = gimple_cond_lhs (stmt);
9428 tree op1 = gimple_cond_rhs (stmt);
9429 enum tree_code cond_code = gimple_cond_code (stmt);
9431 if (cond_code != NE_EXPR
9432 && cond_code != EQ_EXPR
9433 && TREE_CODE (op0) == SSA_NAME
9434 && INTEGRAL_TYPE_P (TREE_TYPE (op0))
9435 && is_gimple_min_invariant (op1))
9437 value_range *vr = get_value_range (op0);
9439 /* If we have range information for OP0, then we might be
9440 able to simplify this conditional. */
9441 if (vr->type == VR_RANGE)
9443 enum warn_strict_overflow_code wc = WARN_STRICT_OVERFLOW_COMPARISON;
9445 tree new_tree = test_for_singularity (cond_code, op0, op1, vr, &sop);
9448 && (!sop || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0))))
9452 fprintf (dump_file, "Simplified relational ");
9453 print_gimple_stmt (dump_file, stmt, 0, 0);
9454 fprintf (dump_file, " into ");
9457 gimple_cond_set_code (stmt, EQ_EXPR);
9458 gimple_cond_set_lhs (stmt, op0);
9459 gimple_cond_set_rhs (stmt, new_tree);
9465 print_gimple_stmt (dump_file, stmt, 0, 0);
9466 fprintf (dump_file, "\n");
9469 if (sop && issue_strict_overflow_warning (wc))
9471 location_t location = input_location;
9472 if (gimple_has_location (stmt))
9473 location = gimple_location (stmt);
9475 warning_at (location, OPT_Wstrict_overflow,
9476 "assuming signed overflow does not occur when "
9477 "simplifying conditional");
9483 /* Try again after inverting the condition. We only deal
9484 with integral types here, so no need to worry about
9485 issues with inverting FP comparisons. */
9487 new_tree = test_for_singularity
9488 (invert_tree_comparison (cond_code, false),
9489 op0, op1, vr, &sop);
9492 && (!sop || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0))))
9496 fprintf (dump_file, "Simplified relational ");
9497 print_gimple_stmt (dump_file, stmt, 0, 0);
9498 fprintf (dump_file, " into ");
9501 gimple_cond_set_code (stmt, NE_EXPR);
9502 gimple_cond_set_lhs (stmt, op0);
9503 gimple_cond_set_rhs (stmt, new_tree);
9509 print_gimple_stmt (dump_file, stmt, 0, 0);
9510 fprintf (dump_file, "\n");
9513 if (sop && issue_strict_overflow_warning (wc))
9515 location_t location = input_location;
9516 if (gimple_has_location (stmt))
9517 location = gimple_location (stmt);
9519 warning_at (location, OPT_Wstrict_overflow,
9520 "assuming signed overflow does not occur when "
9521 "simplifying conditional");
9529 /* If we have a comparison of an SSA_NAME (OP0) against a constant,
9530 see if OP0 was set by a type conversion where the source of
9531 the conversion is another SSA_NAME with a range that fits
9532 into the range of OP0's type.
9534 If so, the conversion is redundant as the earlier SSA_NAME can be
9535 used for the comparison directly if we just massage the constant in the
9537 if (TREE_CODE (op0) == SSA_NAME
9538 && TREE_CODE (op1) == INTEGER_CST)
9540 gimple *def_stmt = SSA_NAME_DEF_STMT (op0);
9543 if (!is_gimple_assign (def_stmt)
9544 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt)))
9547 innerop = gimple_assign_rhs1 (def_stmt);
9549 if (TREE_CODE (innerop) == SSA_NAME
9550 && !POINTER_TYPE_P (TREE_TYPE (innerop))
9551 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (innerop)
9552 && desired_pro_or_demotion_p (TREE_TYPE (innerop), TREE_TYPE (op0)))
9554 value_range *vr = get_value_range (innerop);
9556 if (range_int_cst_p (vr)
9557 && range_fits_type_p (vr,
9558 TYPE_PRECISION (TREE_TYPE (op0)),
9559 TYPE_SIGN (TREE_TYPE (op0)))
9560 && int_fits_type_p (op1, TREE_TYPE (innerop))
9561 /* The range must not have overflowed, or if it did overflow
9562 we must not be wrapping/trapping overflow and optimizing
9563 with strict overflow semantics. */
9564 && ((!is_negative_overflow_infinity (vr->min)
9565 && !is_positive_overflow_infinity (vr->max))
9566 || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (innerop))))
9568 /* If the range overflowed and the user has asked for warnings
9569 when strict overflow semantics were used to optimize code,
9570 issue an appropriate warning. */
9571 if (cond_code != EQ_EXPR && cond_code != NE_EXPR
9572 && (is_negative_overflow_infinity (vr->min)
9573 || is_positive_overflow_infinity (vr->max))
9574 && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_CONDITIONAL))
9576 location_t location;
9578 if (!gimple_has_location (stmt))
9579 location = input_location;
9581 location = gimple_location (stmt);
9582 warning_at (location, OPT_Wstrict_overflow,
9583 "assuming signed overflow does not occur when "
9584 "simplifying conditional");
9587 tree newconst = fold_convert (TREE_TYPE (innerop), op1);
9588 gimple_cond_set_lhs (stmt, innerop);
9589 gimple_cond_set_rhs (stmt, newconst);
9598 /* Simplify a switch statement using the value range of the switch
9602 simplify_switch_using_ranges (gswitch *stmt)
9604 tree op = gimple_switch_index (stmt);
9609 size_t i = 0, j = 0, n, n2;
9612 size_t k = 1, l = 0;
9614 if (TREE_CODE (op) == SSA_NAME)
9616 vr = get_value_range (op);
9618 /* We can only handle integer ranges. */
9619 if ((vr->type != VR_RANGE
9620 && vr->type != VR_ANTI_RANGE)
9621 || symbolic_range_p (vr))
9624 /* Find case label for min/max of the value range. */
9625 take_default = !find_case_label_ranges (stmt, vr, &i, &j, &k, &l);
9627 else if (TREE_CODE (op) == INTEGER_CST)
9629 take_default = !find_case_label_index (stmt, 1, op, &i);
9643 n = gimple_switch_num_labels (stmt);
9645 /* Bail out if this is just all edges taken. */
9651 /* Build a new vector of taken case labels. */
9652 vec2 = make_tree_vec (j - i + 1 + l - k + 1 + (int)take_default);
9655 /* Add the default edge, if necessary. */
9657 TREE_VEC_ELT (vec2, n2++) = gimple_switch_default_label (stmt);
9659 for (; i <= j; ++i, ++n2)
9660 TREE_VEC_ELT (vec2, n2) = gimple_switch_label (stmt, i);
9662 for (; k <= l; ++k, ++n2)
9663 TREE_VEC_ELT (vec2, n2) = gimple_switch_label (stmt, k);
9665 /* Mark needed edges. */
9666 for (i = 0; i < n2; ++i)
9668 e = find_edge (gimple_bb (stmt),
9669 label_to_block (CASE_LABEL (TREE_VEC_ELT (vec2, i))));
9670 e->aux = (void *)-1;
9673 /* Queue not needed edges for later removal. */
9674 FOR_EACH_EDGE (e, ei, gimple_bb (stmt)->succs)
9676 if (e->aux == (void *)-1)
9682 if (dump_file && (dump_flags & TDF_DETAILS))
9684 fprintf (dump_file, "removing unreachable case label\n");
9686 to_remove_edges.safe_push (e);
9687 e->flags &= ~EDGE_EXECUTABLE;
9690 /* And queue an update for the stmt. */
9693 to_update_switch_stmts.safe_push (su);
9697 /* Simplify an integral conversion from an SSA name in STMT. */
9700 simplify_conversion_using_ranges (gimple *stmt)
9702 tree innerop, middleop, finaltype;
9704 value_range *innervr;
9705 signop inner_sgn, middle_sgn, final_sgn;
9706 unsigned inner_prec, middle_prec, final_prec;
9707 widest_int innermin, innermed, innermax, middlemin, middlemed, middlemax;
9709 finaltype = TREE_TYPE (gimple_assign_lhs (stmt));
9710 if (!INTEGRAL_TYPE_P (finaltype))
9712 middleop = gimple_assign_rhs1 (stmt);
9713 def_stmt = SSA_NAME_DEF_STMT (middleop);
9714 if (!is_gimple_assign (def_stmt)
9715 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt)))
9717 innerop = gimple_assign_rhs1 (def_stmt);
9718 if (TREE_CODE (innerop) != SSA_NAME
9719 || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (innerop))
9722 /* Get the value-range of the inner operand. */
9723 innervr = get_value_range (innerop);
9724 if (innervr->type != VR_RANGE
9725 || TREE_CODE (innervr->min) != INTEGER_CST
9726 || TREE_CODE (innervr->max) != INTEGER_CST)
9729 /* Simulate the conversion chain to check if the result is equal if
9730 the middle conversion is removed. */
9731 innermin = wi::to_widest (innervr->min);
9732 innermax = wi::to_widest (innervr->max);
9734 inner_prec = TYPE_PRECISION (TREE_TYPE (innerop));
9735 middle_prec = TYPE_PRECISION (TREE_TYPE (middleop));
9736 final_prec = TYPE_PRECISION (finaltype);
9738 /* If the first conversion is not injective, the second must not
9740 if (wi::gtu_p (innermax - innermin,
9741 wi::mask <widest_int> (middle_prec, false))
9742 && middle_prec < final_prec)
9744 /* We also want a medium value so that we can track the effect that
9745 narrowing conversions with sign change have. */
9746 inner_sgn = TYPE_SIGN (TREE_TYPE (innerop));
9747 if (inner_sgn == UNSIGNED)
9748 innermed = wi::shifted_mask <widest_int> (1, inner_prec - 1, false);
9751 if (wi::cmp (innermin, innermed, inner_sgn) >= 0
9752 || wi::cmp (innermed, innermax, inner_sgn) >= 0)
9753 innermed = innermin;
9755 middle_sgn = TYPE_SIGN (TREE_TYPE (middleop));
9756 middlemin = wi::ext (innermin, middle_prec, middle_sgn);
9757 middlemed = wi::ext (innermed, middle_prec, middle_sgn);
9758 middlemax = wi::ext (innermax, middle_prec, middle_sgn);
9760 /* Require that the final conversion applied to both the original
9761 and the intermediate range produces the same result. */
9762 final_sgn = TYPE_SIGN (finaltype);
9763 if (wi::ext (middlemin, final_prec, final_sgn)
9764 != wi::ext (innermin, final_prec, final_sgn)
9765 || wi::ext (middlemed, final_prec, final_sgn)
9766 != wi::ext (innermed, final_prec, final_sgn)
9767 || wi::ext (middlemax, final_prec, final_sgn)
9768 != wi::ext (innermax, final_prec, final_sgn))
9771 gimple_assign_set_rhs1 (stmt, innerop);
9776 /* Simplify a conversion from integral SSA name to float in STMT. */
9779 simplify_float_conversion_using_ranges (gimple_stmt_iterator *gsi,
9782 tree rhs1 = gimple_assign_rhs1 (stmt);
9783 value_range *vr = get_value_range (rhs1);
9784 machine_mode fltmode = TYPE_MODE (TREE_TYPE (gimple_assign_lhs (stmt)));
9789 /* We can only handle constant ranges. */
9790 if (vr->type != VR_RANGE
9791 || TREE_CODE (vr->min) != INTEGER_CST
9792 || TREE_CODE (vr->max) != INTEGER_CST)
9795 /* First check if we can use a signed type in place of an unsigned. */
9796 if (TYPE_UNSIGNED (TREE_TYPE (rhs1))
9797 && (can_float_p (fltmode, TYPE_MODE (TREE_TYPE (rhs1)), 0)
9798 != CODE_FOR_nothing)
9799 && range_fits_type_p (vr, TYPE_PRECISION (TREE_TYPE (rhs1)), SIGNED))
9800 mode = TYPE_MODE (TREE_TYPE (rhs1));
9801 /* If we can do the conversion in the current input mode do nothing. */
9802 else if (can_float_p (fltmode, TYPE_MODE (TREE_TYPE (rhs1)),
9803 TYPE_UNSIGNED (TREE_TYPE (rhs1))) != CODE_FOR_nothing)
9805 /* Otherwise search for a mode we can use, starting from the narrowest
9806 integer mode available. */
9809 mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
9812 /* If we cannot do a signed conversion to float from mode
9813 or if the value-range does not fit in the signed type
9814 try with a wider mode. */
9815 if (can_float_p (fltmode, mode, 0) != CODE_FOR_nothing
9816 && range_fits_type_p (vr, GET_MODE_PRECISION (mode), SIGNED))
9819 mode = GET_MODE_WIDER_MODE (mode);
9820 /* But do not widen the input. Instead leave that to the
9821 optabs expansion code. */
9822 if (GET_MODE_PRECISION (mode) > TYPE_PRECISION (TREE_TYPE (rhs1)))
9825 while (mode != VOIDmode);
9826 if (mode == VOIDmode)
9830 /* It works, insert a truncation or sign-change before the
9831 float conversion. */
9832 tem = make_ssa_name (build_nonstandard_integer_type
9833 (GET_MODE_PRECISION (mode), 0));
9834 conv = gimple_build_assign (tem, NOP_EXPR, rhs1);
9835 gsi_insert_before (gsi, conv, GSI_SAME_STMT);
9836 gimple_assign_set_rhs1 (stmt, tem);
9842 /* Simplify an internal fn call using ranges if possible. */
9845 simplify_internal_call_using_ranges (gimple_stmt_iterator *gsi, gimple *stmt)
9847 enum tree_code subcode;
9848 bool is_ubsan = false;
9850 switch (gimple_call_internal_fn (stmt))
9852 case IFN_UBSAN_CHECK_ADD:
9853 subcode = PLUS_EXPR;
9856 case IFN_UBSAN_CHECK_SUB:
9857 subcode = MINUS_EXPR;
9860 case IFN_UBSAN_CHECK_MUL:
9861 subcode = MULT_EXPR;
9864 case IFN_ADD_OVERFLOW:
9865 subcode = PLUS_EXPR;
9867 case IFN_SUB_OVERFLOW:
9868 subcode = MINUS_EXPR;
9870 case IFN_MUL_OVERFLOW:
9871 subcode = MULT_EXPR;
9877 tree op0 = gimple_call_arg (stmt, 0);
9878 tree op1 = gimple_call_arg (stmt, 1);
9881 type = TREE_TYPE (op0);
9882 else if (gimple_call_lhs (stmt) == NULL_TREE)
9885 type = TREE_TYPE (TREE_TYPE (gimple_call_lhs (stmt)));
9886 if (!check_for_binary_op_overflow (subcode, type, op0, op1, &ovf)
9887 || (is_ubsan && ovf))
9891 location_t loc = gimple_location (stmt);
9893 g = gimple_build_assign (gimple_call_lhs (stmt), subcode, op0, op1);
9896 int prec = TYPE_PRECISION (type);
9899 || !useless_type_conversion_p (type, TREE_TYPE (op0))
9900 || !useless_type_conversion_p (type, TREE_TYPE (op1)))
9901 utype = build_nonstandard_integer_type (prec, 1);
9902 if (TREE_CODE (op0) == INTEGER_CST)
9903 op0 = fold_convert (utype, op0);
9904 else if (!useless_type_conversion_p (utype, TREE_TYPE (op0)))
9906 g = gimple_build_assign (make_ssa_name (utype), NOP_EXPR, op0);
9907 gimple_set_location (g, loc);
9908 gsi_insert_before (gsi, g, GSI_SAME_STMT);
9909 op0 = gimple_assign_lhs (g);
9911 if (TREE_CODE (op1) == INTEGER_CST)
9912 op1 = fold_convert (utype, op1);
9913 else if (!useless_type_conversion_p (utype, TREE_TYPE (op1)))
9915 g = gimple_build_assign (make_ssa_name (utype), NOP_EXPR, op1);
9916 gimple_set_location (g, loc);
9917 gsi_insert_before (gsi, g, GSI_SAME_STMT);
9918 op1 = gimple_assign_lhs (g);
9920 g = gimple_build_assign (make_ssa_name (utype), subcode, op0, op1);
9921 gimple_set_location (g, loc);
9922 gsi_insert_before (gsi, g, GSI_SAME_STMT);
9925 g = gimple_build_assign (make_ssa_name (type), NOP_EXPR,
9926 gimple_assign_lhs (g));
9927 gimple_set_location (g, loc);
9928 gsi_insert_before (gsi, g, GSI_SAME_STMT);
9930 g = gimple_build_assign (gimple_call_lhs (stmt), COMPLEX_EXPR,
9931 gimple_assign_lhs (g),
9932 build_int_cst (type, ovf));
9934 gimple_set_location (g, loc);
9935 gsi_replace (gsi, g, false);
9939 /* Return true if VAR is a two-valued variable. Set a and b with the
9940 two-values when it is true. Return false otherwise. */
9943 two_valued_val_range_p (tree var, tree *a, tree *b)
9945 value_range *vr = get_value_range (var);
9946 if ((vr->type != VR_RANGE
9947 && vr->type != VR_ANTI_RANGE)
9948 || TREE_CODE (vr->min) != INTEGER_CST
9949 || TREE_CODE (vr->max) != INTEGER_CST)
9952 if (vr->type == VR_RANGE
9953 && wi::sub (vr->max, vr->min) == 1)
9960 /* ~[TYPE_MIN + 1, TYPE_MAX - 1] */
9961 if (vr->type == VR_ANTI_RANGE
9962 && wi::sub (vr->min, vrp_val_min (TREE_TYPE (var))) == 1
9963 && wi::sub (vrp_val_max (TREE_TYPE (var)), vr->max) == 1)
9965 *a = vrp_val_min (TREE_TYPE (var));
9966 *b = vrp_val_max (TREE_TYPE (var));
9973 /* Simplify STMT using ranges if possible. */
9976 simplify_stmt_using_ranges (gimple_stmt_iterator *gsi)
9978 gimple *stmt = gsi_stmt (*gsi);
9979 if (is_gimple_assign (stmt))
9981 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
9982 tree rhs1 = gimple_assign_rhs1 (stmt);
9983 tree rhs2 = gimple_assign_rhs2 (stmt);
9984 tree lhs = gimple_assign_lhs (stmt);
9985 tree val1 = NULL_TREE, val2 = NULL_TREE;
9986 use_operand_p use_p;
9991 Where VAR is two-valued and LHS is used in GIMPLE_COND only
9993 LHS = VAR == VAL1 ? (CST BINOP VAL1) : (CST BINOP VAL2)
9997 Where VAR is two-valued and LHS is used in GIMPLE_COND only
9999 LHS = VAR == VAL1 ? (VAL1 BINOP CST) : (VAL2 BINOP CST) */
10001 if (TREE_CODE_CLASS (rhs_code) == tcc_binary
10002 && INTEGRAL_TYPE_P (TREE_TYPE (lhs))
10003 && ((TREE_CODE (rhs1) == INTEGER_CST
10004 && TREE_CODE (rhs2) == SSA_NAME)
10005 || (TREE_CODE (rhs2) == INTEGER_CST
10006 && TREE_CODE (rhs1) == SSA_NAME))
10007 && single_imm_use (lhs, &use_p, &use_stmt)
10008 && gimple_code (use_stmt) == GIMPLE_COND)
10011 tree new_rhs1 = NULL_TREE;
10012 tree new_rhs2 = NULL_TREE;
10013 tree cmp_var = NULL_TREE;
10015 if (TREE_CODE (rhs2) == SSA_NAME
10016 && two_valued_val_range_p (rhs2, &val1, &val2))
10018 /* Optimize RHS1 OP [VAL1, VAL2]. */
10019 new_rhs1 = int_const_binop (rhs_code, rhs1, val1);
10020 new_rhs2 = int_const_binop (rhs_code, rhs1, val2);
10023 else if (TREE_CODE (rhs1) == SSA_NAME
10024 && two_valued_val_range_p (rhs1, &val1, &val2))
10026 /* Optimize [VAL1, VAL2] OP RHS2. */
10027 new_rhs1 = int_const_binop (rhs_code, val1, rhs2);
10028 new_rhs2 = int_const_binop (rhs_code, val2, rhs2);
10032 /* If we could not find two-vals or the optimzation is invalid as
10033 in divide by zero, new_rhs1 / new_rhs will be NULL_TREE. */
10034 if (new_rhs1 && new_rhs2)
10036 tree cond = build2 (EQ_EXPR, TREE_TYPE (cmp_var), cmp_var, val1);
10037 gimple_assign_set_rhs_with_ops (gsi,
10041 update_stmt (gsi_stmt (*gsi));
10050 /* Transform EQ_EXPR, NE_EXPR into BIT_XOR_EXPR or identity
10051 if the RHS is zero or one, and the LHS are known to be boolean
10053 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
10054 return simplify_truth_ops_using_ranges (gsi, stmt);
10057 /* Transform TRUNC_DIV_EXPR and TRUNC_MOD_EXPR into RSHIFT_EXPR
10058 and BIT_AND_EXPR respectively if the first operand is greater
10059 than zero and the second operand is an exact power of two.
10060 Also optimize TRUNC_MOD_EXPR away if the second operand is
10061 constant and the first operand already has the right value
10063 case TRUNC_DIV_EXPR:
10064 case TRUNC_MOD_EXPR:
10065 if (TREE_CODE (rhs1) == SSA_NAME
10066 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
10067 return simplify_div_or_mod_using_ranges (gsi, stmt);
10070 /* Transform ABS (X) into X or -X as appropriate. */
10072 if (TREE_CODE (rhs1) == SSA_NAME
10073 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
10074 return simplify_abs_using_ranges (stmt);
10079 /* Optimize away BIT_AND_EXPR and BIT_IOR_EXPR
10080 if all the bits being cleared are already cleared or
10081 all the bits being set are already set. */
10082 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
10083 return simplify_bit_ops_using_ranges (gsi, stmt);
10087 if (TREE_CODE (rhs1) == SSA_NAME
10088 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
10089 return simplify_conversion_using_ranges (stmt);
10093 if (TREE_CODE (rhs1) == SSA_NAME
10094 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
10095 return simplify_float_conversion_using_ranges (gsi, stmt);
10100 return simplify_min_or_max_using_ranges (stmt);
10107 else if (gimple_code (stmt) == GIMPLE_COND)
10108 return simplify_cond_using_ranges (as_a <gcond *> (stmt));
10109 else if (gimple_code (stmt) == GIMPLE_SWITCH)
10110 return simplify_switch_using_ranges (as_a <gswitch *> (stmt));
10111 else if (is_gimple_call (stmt)
10112 && gimple_call_internal_p (stmt))
10113 return simplify_internal_call_using_ranges (gsi, stmt);
10118 /* If the statement pointed by SI has a predicate whose value can be
10119 computed using the value range information computed by VRP, compute
10120 its value and return true. Otherwise, return false. */
10123 fold_predicate_in (gimple_stmt_iterator *si)
10125 bool assignment_p = false;
10127 gimple *stmt = gsi_stmt (*si);
10129 if (is_gimple_assign (stmt)
10130 && TREE_CODE_CLASS (gimple_assign_rhs_code (stmt)) == tcc_comparison)
10132 assignment_p = true;
10133 val = vrp_evaluate_conditional (gimple_assign_rhs_code (stmt),
10134 gimple_assign_rhs1 (stmt),
10135 gimple_assign_rhs2 (stmt),
10138 else if (gcond *cond_stmt = dyn_cast <gcond *> (stmt))
10139 val = vrp_evaluate_conditional (gimple_cond_code (cond_stmt),
10140 gimple_cond_lhs (cond_stmt),
10141 gimple_cond_rhs (cond_stmt),
10149 val = fold_convert (gimple_expr_type (stmt), val);
10153 fprintf (dump_file, "Folding predicate ");
10154 print_gimple_expr (dump_file, stmt, 0, 0);
10155 fprintf (dump_file, " to ");
10156 print_generic_expr (dump_file, val, 0);
10157 fprintf (dump_file, "\n");
10160 if (is_gimple_assign (stmt))
10161 gimple_assign_set_rhs_from_tree (si, val);
10164 gcc_assert (gimple_code (stmt) == GIMPLE_COND);
10165 gcond *cond_stmt = as_a <gcond *> (stmt);
10166 if (integer_zerop (val))
10167 gimple_cond_make_false (cond_stmt);
10168 else if (integer_onep (val))
10169 gimple_cond_make_true (cond_stmt);
10171 gcc_unreachable ();
10180 /* Callback for substitute_and_fold folding the stmt at *SI. */
10183 vrp_fold_stmt (gimple_stmt_iterator *si)
10185 if (fold_predicate_in (si))
10188 return simplify_stmt_using_ranges (si);
10191 /* Unwindable const/copy equivalences. */
10192 const_and_copies *equiv_stack;
10194 /* A trivial wrapper so that we can present the generic jump threading
10195 code with a simple API for simplifying statements. STMT is the
10196 statement we want to simplify, WITHIN_STMT provides the location
10197 for any overflow warnings. */
10200 simplify_stmt_for_jump_threading (gimple *stmt, gimple *within_stmt,
10201 class avail_exprs_stack *avail_exprs_stack ATTRIBUTE_UNUSED)
10203 if (gcond *cond_stmt = dyn_cast <gcond *> (stmt))
10204 return vrp_evaluate_conditional (gimple_cond_code (cond_stmt),
10205 gimple_cond_lhs (cond_stmt),
10206 gimple_cond_rhs (cond_stmt),
10209 if (gassign *assign_stmt = dyn_cast <gassign *> (stmt))
10211 value_range new_vr = VR_INITIALIZER;
10212 tree lhs = gimple_assign_lhs (assign_stmt);
10214 if (TREE_CODE (lhs) == SSA_NAME
10215 && (INTEGRAL_TYPE_P (TREE_TYPE (lhs))
10216 || POINTER_TYPE_P (TREE_TYPE (lhs))))
10218 extract_range_from_assignment (&new_vr, assign_stmt);
10219 if (range_int_cst_singleton_p (&new_vr))
10227 /* Blocks which have more than one predecessor and more than
10228 one successor present jump threading opportunities, i.e.,
10229 when the block is reached from a specific predecessor, we
10230 may be able to determine which of the outgoing edges will
10231 be traversed. When this optimization applies, we are able
10232 to avoid conditionals at runtime and we may expose secondary
10233 optimization opportunities.
10235 This routine is effectively a driver for the generic jump
10236 threading code. It basically just presents the generic code
10237 with edges that may be suitable for jump threading.
10239 Unlike DOM, we do not iterate VRP if jump threading was successful.
10240 While iterating may expose new opportunities for VRP, it is expected
10241 those opportunities would be very limited and the compile time cost
10242 to expose those opportunities would be significant.
10244 As jump threading opportunities are discovered, they are registered
10245 for later realization. */
10248 identify_jump_threads (void)
10255 /* Ugh. When substituting values earlier in this pass we can
10256 wipe the dominance information. So rebuild the dominator
10257 information as we need it within the jump threading code. */
10258 calculate_dominance_info (CDI_DOMINATORS);
10260 /* We do not allow VRP information to be used for jump threading
10261 across a back edge in the CFG. Otherwise it becomes too
10262 difficult to avoid eliminating loop exit tests. Of course
10263 EDGE_DFS_BACK is not accurate at this time so we have to
10265 mark_dfs_back_edges ();
10267 /* Do not thread across edges we are about to remove. Just marking
10268 them as EDGE_IGNORE will do. */
10269 FOR_EACH_VEC_ELT (to_remove_edges, i, e)
10270 e->flags |= EDGE_IGNORE;
10272 /* Allocate our unwinder stack to unwind any temporary equivalences
10273 that might be recorded. */
10274 equiv_stack = new const_and_copies ();
10276 /* To avoid lots of silly node creation, we create a single
10277 conditional and just modify it in-place when attempting to
10279 dummy = gimple_build_cond (EQ_EXPR,
10280 integer_zero_node, integer_zero_node,
10283 /* Walk through all the blocks finding those which present a
10284 potential jump threading opportunity. We could set this up
10285 as a dominator walker and record data during the walk, but
10286 I doubt it's worth the effort for the classes of jump
10287 threading opportunities we are trying to identify at this
10288 point in compilation. */
10289 FOR_EACH_BB_FN (bb, cfun)
10293 /* If the generic jump threading code does not find this block
10294 interesting, then there is nothing to do. */
10295 if (! potentially_threadable_block (bb))
10298 last = last_stmt (bb);
10300 /* We're basically looking for a switch or any kind of conditional with
10301 integral or pointer type arguments. Note the type of the second
10302 argument will be the same as the first argument, so no need to
10303 check it explicitly.
10305 We also handle the case where there are no statements in the
10306 block. This come up with forwarder blocks that are not
10307 optimized away because they lead to a loop header. But we do
10308 want to thread through them as we can sometimes thread to the
10309 loop exit which is obviously profitable. */
10311 || gimple_code (last) == GIMPLE_SWITCH
10312 || (gimple_code (last) == GIMPLE_COND
10313 && TREE_CODE (gimple_cond_lhs (last)) == SSA_NAME
10314 && (INTEGRAL_TYPE_P (TREE_TYPE (gimple_cond_lhs (last)))
10315 || POINTER_TYPE_P (TREE_TYPE (gimple_cond_lhs (last))))
10316 && (TREE_CODE (gimple_cond_rhs (last)) == SSA_NAME
10317 || is_gimple_min_invariant (gimple_cond_rhs (last)))))
10321 /* We've got a block with multiple predecessors and multiple
10322 successors which also ends in a suitable conditional or
10323 switch statement. For each predecessor, see if we can thread
10324 it to a specific successor. */
10325 FOR_EACH_EDGE (e, ei, bb->preds)
10327 /* Do not thread across edges marked to ignoreor abnormal
10328 edges in the CFG. */
10329 if (e->flags & (EDGE_IGNORE | EDGE_COMPLEX))
10332 thread_across_edge (dummy, e, true, equiv_stack, NULL,
10333 simplify_stmt_for_jump_threading);
10338 /* Clear EDGE_IGNORE. */
10339 FOR_EACH_VEC_ELT (to_remove_edges, i, e)
10340 e->flags &= ~EDGE_IGNORE;
10342 /* We do not actually update the CFG or SSA graphs at this point as
10343 ASSERT_EXPRs are still in the IL and cfg cleanup code does not yet
10344 handle ASSERT_EXPRs gracefully. */
10347 /* We identified all the jump threading opportunities earlier, but could
10348 not transform the CFG at that time. This routine transforms the
10349 CFG and arranges for the dominator tree to be rebuilt if necessary.
10351 Note the SSA graph update will occur during the normal TODO
10352 processing by the pass manager. */
10354 finalize_jump_threads (void)
10356 thread_through_all_blocks (false);
10357 delete equiv_stack;
10361 /* Traverse all the blocks folding conditionals with known ranges. */
10364 vrp_finalize (bool warn_array_bounds_p)
10368 values_propagated = true;
10372 fprintf (dump_file, "\nValue ranges after VRP:\n\n");
10373 dump_all_value_ranges (dump_file);
10374 fprintf (dump_file, "\n");
10377 /* Set value range to non pointer SSA_NAMEs. */
10378 for (i = 0; i < num_vr_values; i++)
10381 tree name = ssa_name (i);
10384 || POINTER_TYPE_P (TREE_TYPE (name))
10385 || (vr_value[i]->type == VR_VARYING)
10386 || (vr_value[i]->type == VR_UNDEFINED))
10389 if ((TREE_CODE (vr_value[i]->min) == INTEGER_CST)
10390 && (TREE_CODE (vr_value[i]->max) == INTEGER_CST)
10391 && (vr_value[i]->type == VR_RANGE
10392 || vr_value[i]->type == VR_ANTI_RANGE))
10393 set_range_info (name, vr_value[i]->type, vr_value[i]->min,
10397 substitute_and_fold (op_with_constant_singleton_value_range,
10398 vrp_fold_stmt, false);
10400 if (warn_array_bounds && warn_array_bounds_p)
10401 check_all_array_refs ();
10403 /* We must identify jump threading opportunities before we release
10404 the datastructures built by VRP. */
10405 identify_jump_threads ();
10407 /* Free allocated memory. */
10408 for (i = 0; i < num_vr_values; i++)
10411 BITMAP_FREE (vr_value[i]->equiv);
10412 free (vr_value[i]);
10416 free (vr_phi_edge_counts);
10418 /* So that we can distinguish between VRP data being available
10419 and not available. */
10421 vr_phi_edge_counts = NULL;
10425 /* Main entry point to VRP (Value Range Propagation). This pass is
10426 loosely based on J. R. C. Patterson, ``Accurate Static Branch
10427 Prediction by Value Range Propagation,'' in SIGPLAN Conference on
10428 Programming Language Design and Implementation, pp. 67-78, 1995.
10429 Also available at http://citeseer.ist.psu.edu/patterson95accurate.html
10431 This is essentially an SSA-CCP pass modified to deal with ranges
10432 instead of constants.
10434 While propagating ranges, we may find that two or more SSA name
10435 have equivalent, though distinct ranges. For instance,
10438 2 p_4 = ASSERT_EXPR <p_3, p_3 != 0>
10440 4 p_5 = ASSERT_EXPR <p_4, p_4 == q_2>;
10444 In the code above, pointer p_5 has range [q_2, q_2], but from the
10445 code we can also determine that p_5 cannot be NULL and, if q_2 had
10446 a non-varying range, p_5's range should also be compatible with it.
10448 These equivalences are created by two expressions: ASSERT_EXPR and
10449 copy operations. Since p_5 is an assertion on p_4, and p_4 was the
10450 result of another assertion, then we can use the fact that p_5 and
10451 p_4 are equivalent when evaluating p_5's range.
10453 Together with value ranges, we also propagate these equivalences
10454 between names so that we can take advantage of information from
10455 multiple ranges when doing final replacement. Note that this
10456 equivalency relation is transitive but not symmetric.
10458 In the example above, p_5 is equivalent to p_4, q_2 and p_3, but we
10459 cannot assert that q_2 is equivalent to p_5 because q_2 may be used
10460 in contexts where that assertion does not hold (e.g., in line 6).
10462 TODO, the main difference between this pass and Patterson's is that
10463 we do not propagate edge probabilities. We only compute whether
10464 edges can be taken or not. That is, instead of having a spectrum
10465 of jump probabilities between 0 and 1, we only deal with 0, 1 and
10466 DON'T KNOW. In the future, it may be worthwhile to propagate
10467 probabilities to aid branch prediction. */
10469 static unsigned int
10470 execute_vrp (bool warn_array_bounds_p)
10476 loop_optimizer_init (LOOPS_NORMAL | LOOPS_HAVE_RECORDED_EXITS);
10477 rewrite_into_loop_closed_ssa (NULL, TODO_update_ssa);
10478 scev_initialize ();
10480 /* ??? This ends up using stale EDGE_DFS_BACK for liveness computation.
10481 Inserting assertions may split edges which will invalidate
10483 insert_range_assertions ();
10485 to_remove_edges.create (10);
10486 to_update_switch_stmts.create (5);
10487 threadedge_initialize_values ();
10489 /* For visiting PHI nodes we need EDGE_DFS_BACK computed. */
10490 mark_dfs_back_edges ();
10493 ssa_propagate (vrp_visit_stmt, vrp_visit_phi_node);
10494 vrp_finalize (warn_array_bounds_p);
10496 free_numbers_of_iterations_estimates (cfun);
10498 /* ASSERT_EXPRs must be removed before finalizing jump threads
10499 as finalizing jump threads calls the CFG cleanup code which
10500 does not properly handle ASSERT_EXPRs. */
10501 remove_range_assertions ();
10503 /* If we exposed any new variables, go ahead and put them into
10504 SSA form now, before we handle jump threading. This simplifies
10505 interactions between rewriting of _DECL nodes into SSA form
10506 and rewriting SSA_NAME nodes into SSA form after block
10507 duplication and CFG manipulation. */
10508 update_ssa (TODO_update_ssa);
10510 finalize_jump_threads ();
10512 /* Remove dead edges from SWITCH_EXPR optimization. This leaves the
10513 CFG in a broken state and requires a cfg_cleanup run. */
10514 FOR_EACH_VEC_ELT (to_remove_edges, i, e)
10516 /* Update SWITCH_EXPR case label vector. */
10517 FOR_EACH_VEC_ELT (to_update_switch_stmts, i, su)
10520 size_t n = TREE_VEC_LENGTH (su->vec);
10522 gimple_switch_set_num_labels (su->stmt, n);
10523 for (j = 0; j < n; j++)
10524 gimple_switch_set_label (su->stmt, j, TREE_VEC_ELT (su->vec, j));
10525 /* As we may have replaced the default label with a regular one
10526 make sure to make it a real default label again. This ensures
10527 optimal expansion. */
10528 label = gimple_switch_label (su->stmt, 0);
10529 CASE_LOW (label) = NULL_TREE;
10530 CASE_HIGH (label) = NULL_TREE;
10533 if (to_remove_edges.length () > 0)
10535 free_dominance_info (CDI_DOMINATORS);
10536 loops_state_set (LOOPS_NEED_FIXUP);
10539 to_remove_edges.release ();
10540 to_update_switch_stmts.release ();
10541 threadedge_finalize_values ();
10544 loop_optimizer_finalize ();
10550 const pass_data pass_data_vrp =
10552 GIMPLE_PASS, /* type */
10554 OPTGROUP_NONE, /* optinfo_flags */
10555 TV_TREE_VRP, /* tv_id */
10556 PROP_ssa, /* properties_required */
10557 0, /* properties_provided */
10558 0, /* properties_destroyed */
10559 0, /* todo_flags_start */
10560 ( TODO_cleanup_cfg | TODO_update_ssa ), /* todo_flags_finish */
10563 class pass_vrp : public gimple_opt_pass
10566 pass_vrp (gcc::context *ctxt)
10567 : gimple_opt_pass (pass_data_vrp, ctxt), warn_array_bounds_p (false)
10570 /* opt_pass methods: */
10571 opt_pass * clone () { return new pass_vrp (m_ctxt); }
10572 void set_pass_param (unsigned int n, bool param)
10574 gcc_assert (n == 0);
10575 warn_array_bounds_p = param;
10577 virtual bool gate (function *) { return flag_tree_vrp != 0; }
10578 virtual unsigned int execute (function *)
10579 { return execute_vrp (warn_array_bounds_p); }
10582 bool warn_array_bounds_p;
10583 }; // class pass_vrp
10585 } // anon namespace
10588 make_pass_vrp (gcc::context *ctxt)
10590 return new pass_vrp (ctxt);