1 /* Support routines for Value Range Propagation (VRP).
2 Copyright (C) 2005-2013 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"
28 #include "basic-block.h"
29 #include "tree-flow.h"
30 #include "tree-pass.h"
31 #include "tree-dump.h"
32 #include "gimple-pretty-print.h"
33 #include "diagnostic-core.h"
36 #include "tree-scalar-evolution.h"
37 #include "tree-ssa-propagate.h"
38 #include "tree-chrec.h"
39 #include "gimple-fold.h"
44 /* Type of value ranges. See value_range_d for a description of these
46 enum value_range_type { VR_UNDEFINED, VR_RANGE, VR_ANTI_RANGE, VR_VARYING };
48 /* Range of values that can be associated with an SSA_NAME after VRP
52 /* Lattice value represented by this range. */
53 enum value_range_type type;
55 /* Minimum and maximum values represented by this range. These
56 values should be interpreted as follows:
58 - If TYPE is VR_UNDEFINED or VR_VARYING then MIN and MAX must
61 - If TYPE == VR_RANGE then MIN holds the minimum value and
62 MAX holds the maximum value of the range [MIN, MAX].
64 - If TYPE == ANTI_RANGE the variable is known to NOT
65 take any values in the range [MIN, MAX]. */
69 /* Set of SSA names whose value ranges are equivalent to this one.
70 This set is only valid when TYPE is VR_RANGE or VR_ANTI_RANGE. */
74 typedef struct value_range_d value_range_t;
76 #define VR_INITIALIZER { VR_UNDEFINED, NULL_TREE, NULL_TREE, NULL }
78 /* Set of SSA names found live during the RPO traversal of the function
79 for still active basic-blocks. */
82 /* Return true if the SSA name NAME is live on the edge E. */
85 live_on_edge (edge e, tree name)
87 return (live[e->dest->index]
88 && bitmap_bit_p (live[e->dest->index], SSA_NAME_VERSION (name)));
91 /* Local functions. */
92 static int compare_values (tree val1, tree val2);
93 static int compare_values_warnv (tree val1, tree val2, bool *);
94 static void vrp_meet (value_range_t *, value_range_t *);
95 static void vrp_intersect_ranges (value_range_t *, value_range_t *);
96 static tree vrp_evaluate_conditional_warnv_with_ops (enum tree_code,
97 tree, tree, bool, bool *,
100 /* Location information for ASSERT_EXPRs. Each instance of this
101 structure describes an ASSERT_EXPR for an SSA name. Since a single
102 SSA name may have more than one assertion associated with it, these
103 locations are kept in a linked list attached to the corresponding
105 struct assert_locus_d
107 /* Basic block where the assertion would be inserted. */
110 /* Some assertions need to be inserted on an edge (e.g., assertions
111 generated by COND_EXPRs). In those cases, BB will be NULL. */
114 /* Pointer to the statement that generated this assertion. */
115 gimple_stmt_iterator si;
117 /* Predicate code for the ASSERT_EXPR. Must be COMPARISON_CLASS_P. */
118 enum tree_code comp_code;
120 /* Value being compared against. */
123 /* Expression to compare. */
126 /* Next node in the linked list. */
127 struct assert_locus_d *next;
130 typedef struct assert_locus_d *assert_locus_t;
132 /* If bit I is present, it means that SSA name N_i has a list of
133 assertions that should be inserted in the IL. */
134 static bitmap need_assert_for;
136 /* Array of locations lists where to insert assertions. ASSERTS_FOR[I]
137 holds a list of ASSERT_LOCUS_T nodes that describe where
138 ASSERT_EXPRs for SSA name N_I should be inserted. */
139 static assert_locus_t *asserts_for;
141 /* Value range array. After propagation, VR_VALUE[I] holds the range
142 of values that SSA name N_I may take. */
143 static unsigned num_vr_values;
144 static value_range_t **vr_value;
145 static bool values_propagated;
147 /* For a PHI node which sets SSA name N_I, VR_COUNTS[I] holds the
148 number of executable edges we saw the last time we visited the
150 static int *vr_phi_edge_counts;
157 static vec<edge> to_remove_edges;
158 static vec<switch_update> to_update_switch_stmts;
161 /* Return the maximum value for TYPE. */
164 vrp_val_max (const_tree type)
166 if (!INTEGRAL_TYPE_P (type))
169 return TYPE_MAX_VALUE (type);
172 /* Return the minimum value for TYPE. */
175 vrp_val_min (const_tree type)
177 if (!INTEGRAL_TYPE_P (type))
180 return TYPE_MIN_VALUE (type);
183 /* Return whether VAL is equal to the maximum value of its type. This
184 will be true for a positive overflow infinity. We can't do a
185 simple equality comparison with TYPE_MAX_VALUE because C typedefs
186 and Ada subtypes can produce types whose TYPE_MAX_VALUE is not ==
187 to the integer constant with the same value in the type. */
190 vrp_val_is_max (const_tree val)
192 tree type_max = vrp_val_max (TREE_TYPE (val));
193 return (val == type_max
194 || (type_max != NULL_TREE
195 && operand_equal_p (val, type_max, 0)));
198 /* Return whether VAL is equal to the minimum value of its type. This
199 will be true for a negative overflow infinity. */
202 vrp_val_is_min (const_tree val)
204 tree type_min = vrp_val_min (TREE_TYPE (val));
205 return (val == type_min
206 || (type_min != NULL_TREE
207 && operand_equal_p (val, type_min, 0)));
211 /* Return whether TYPE should use an overflow infinity distinct from
212 TYPE_{MIN,MAX}_VALUE. We use an overflow infinity value to
213 represent a signed overflow during VRP computations. An infinity
214 is distinct from a half-range, which will go from some number to
215 TYPE_{MIN,MAX}_VALUE. */
218 needs_overflow_infinity (const_tree type)
220 return INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_WRAPS (type);
223 /* Return whether TYPE can support our overflow infinity
224 representation: we use the TREE_OVERFLOW flag, which only exists
225 for constants. If TYPE doesn't support this, we don't optimize
226 cases which would require signed overflow--we drop them to
230 supports_overflow_infinity (const_tree type)
232 tree min = vrp_val_min (type), max = vrp_val_max (type);
233 #ifdef ENABLE_CHECKING
234 gcc_assert (needs_overflow_infinity (type));
236 return (min != NULL_TREE
237 && CONSTANT_CLASS_P (min)
239 && CONSTANT_CLASS_P (max));
242 /* VAL is the maximum or minimum value of a type. Return a
243 corresponding overflow infinity. */
246 make_overflow_infinity (tree val)
248 gcc_checking_assert (val != NULL_TREE && CONSTANT_CLASS_P (val));
249 val = copy_node (val);
250 TREE_OVERFLOW (val) = 1;
254 /* Return a negative overflow infinity for TYPE. */
257 negative_overflow_infinity (tree type)
259 gcc_checking_assert (supports_overflow_infinity (type));
260 return make_overflow_infinity (vrp_val_min (type));
263 /* Return a positive overflow infinity for TYPE. */
266 positive_overflow_infinity (tree type)
268 gcc_checking_assert (supports_overflow_infinity (type));
269 return make_overflow_infinity (vrp_val_max (type));
272 /* Return whether VAL is a negative overflow infinity. */
275 is_negative_overflow_infinity (const_tree val)
277 return (needs_overflow_infinity (TREE_TYPE (val))
278 && CONSTANT_CLASS_P (val)
279 && TREE_OVERFLOW (val)
280 && vrp_val_is_min (val));
283 /* Return whether VAL is a positive overflow infinity. */
286 is_positive_overflow_infinity (const_tree val)
288 return (needs_overflow_infinity (TREE_TYPE (val))
289 && CONSTANT_CLASS_P (val)
290 && TREE_OVERFLOW (val)
291 && vrp_val_is_max (val));
294 /* Return whether VAL is a positive or negative overflow infinity. */
297 is_overflow_infinity (const_tree val)
299 return (needs_overflow_infinity (TREE_TYPE (val))
300 && CONSTANT_CLASS_P (val)
301 && TREE_OVERFLOW (val)
302 && (vrp_val_is_min (val) || vrp_val_is_max (val)));
305 /* Return whether STMT has a constant rhs that is_overflow_infinity. */
308 stmt_overflow_infinity (gimple stmt)
310 if (is_gimple_assign (stmt)
311 && get_gimple_rhs_class (gimple_assign_rhs_code (stmt)) ==
313 return is_overflow_infinity (gimple_assign_rhs1 (stmt));
317 /* If VAL is now an overflow infinity, return VAL. Otherwise, return
318 the same value with TREE_OVERFLOW clear. This can be used to avoid
319 confusing a regular value with an overflow value. */
322 avoid_overflow_infinity (tree val)
324 if (!is_overflow_infinity (val))
327 if (vrp_val_is_max (val))
328 return vrp_val_max (TREE_TYPE (val));
331 gcc_checking_assert (vrp_val_is_min (val));
332 return vrp_val_min (TREE_TYPE (val));
337 /* Return true if ARG is marked with the nonnull attribute in the
338 current function signature. */
341 nonnull_arg_p (const_tree arg)
343 tree t, attrs, fntype;
344 unsigned HOST_WIDE_INT arg_num;
346 gcc_assert (TREE_CODE (arg) == PARM_DECL && POINTER_TYPE_P (TREE_TYPE (arg)));
348 /* The static chain decl is always non null. */
349 if (arg == cfun->static_chain_decl)
352 fntype = TREE_TYPE (current_function_decl);
353 for (attrs = TYPE_ATTRIBUTES (fntype); attrs; attrs = TREE_CHAIN (attrs))
355 attrs = lookup_attribute ("nonnull", attrs);
357 /* If "nonnull" wasn't specified, we know nothing about the argument. */
358 if (attrs == NULL_TREE)
361 /* If "nonnull" applies to all the arguments, then ARG is non-null. */
362 if (TREE_VALUE (attrs) == NULL_TREE)
365 /* Get the position number for ARG in the function signature. */
366 for (arg_num = 1, t = DECL_ARGUMENTS (current_function_decl);
368 t = DECL_CHAIN (t), arg_num++)
374 gcc_assert (t == arg);
376 /* Now see if ARG_NUM is mentioned in the nonnull list. */
377 for (t = TREE_VALUE (attrs); t; t = TREE_CHAIN (t))
379 if (compare_tree_int (TREE_VALUE (t), arg_num) == 0)
388 /* Set value range VR to VR_UNDEFINED. */
391 set_value_range_to_undefined (value_range_t *vr)
393 vr->type = VR_UNDEFINED;
394 vr->min = vr->max = NULL_TREE;
396 bitmap_clear (vr->equiv);
400 /* Set value range VR to VR_VARYING. */
403 set_value_range_to_varying (value_range_t *vr)
405 vr->type = VR_VARYING;
406 vr->min = vr->max = NULL_TREE;
408 bitmap_clear (vr->equiv);
412 /* Set value range VR to {T, MIN, MAX, EQUIV}. */
415 set_value_range (value_range_t *vr, enum value_range_type t, tree min,
416 tree max, bitmap equiv)
418 #if defined ENABLE_CHECKING
419 /* Check the validity of the range. */
420 if (t == VR_RANGE || t == VR_ANTI_RANGE)
424 gcc_assert (min && max);
426 if (INTEGRAL_TYPE_P (TREE_TYPE (min)) && t == VR_ANTI_RANGE)
427 gcc_assert (!vrp_val_is_min (min) || !vrp_val_is_max (max));
429 cmp = compare_values (min, max);
430 gcc_assert (cmp == 0 || cmp == -1 || cmp == -2);
432 if (needs_overflow_infinity (TREE_TYPE (min)))
433 gcc_assert (!is_overflow_infinity (min)
434 || !is_overflow_infinity (max));
437 if (t == VR_UNDEFINED || t == VR_VARYING)
438 gcc_assert (min == NULL_TREE && max == NULL_TREE);
440 if (t == VR_UNDEFINED || t == VR_VARYING)
441 gcc_assert (equiv == NULL || bitmap_empty_p (equiv));
448 /* Since updating the equivalence set involves deep copying the
449 bitmaps, only do it if absolutely necessary. */
450 if (vr->equiv == NULL
452 vr->equiv = BITMAP_ALLOC (NULL);
454 if (equiv != vr->equiv)
456 if (equiv && !bitmap_empty_p (equiv))
457 bitmap_copy (vr->equiv, equiv);
459 bitmap_clear (vr->equiv);
464 /* Set value range VR to the canonical form of {T, MIN, MAX, EQUIV}.
465 This means adjusting T, MIN and MAX representing the case of a
466 wrapping range with MAX < MIN covering [MIN, type_max] U [type_min, MAX]
467 as anti-rage ~[MAX+1, MIN-1]. Likewise for wrapping anti-ranges.
468 In corner cases where MAX+1 or MIN-1 wraps this will fall back
470 This routine exists to ease canonicalization in the case where we
471 extract ranges from var + CST op limit. */
474 set_and_canonicalize_value_range (value_range_t *vr, enum value_range_type t,
475 tree min, tree max, bitmap equiv)
477 /* Use the canonical setters for VR_UNDEFINED and VR_VARYING. */
478 if (t == VR_UNDEFINED)
480 set_value_range_to_undefined (vr);
483 else if (t == VR_VARYING)
485 set_value_range_to_varying (vr);
489 /* Nothing to canonicalize for symbolic ranges. */
490 if (TREE_CODE (min) != INTEGER_CST
491 || TREE_CODE (max) != INTEGER_CST)
493 set_value_range (vr, t, min, max, equiv);
497 /* Wrong order for min and max, to swap them and the VR type we need
499 if (tree_int_cst_lt (max, min))
503 /* For one bit precision if max < min, then the swapped
504 range covers all values, so for VR_RANGE it is varying and
505 for VR_ANTI_RANGE empty range, so drop to varying as well. */
506 if (TYPE_PRECISION (TREE_TYPE (min)) == 1)
508 set_value_range_to_varying (vr);
512 one = build_int_cst (TREE_TYPE (min), 1);
513 tmp = int_const_binop (PLUS_EXPR, max, one);
514 max = int_const_binop (MINUS_EXPR, min, one);
517 /* There's one corner case, if we had [C+1, C] before we now have
518 that again. But this represents an empty value range, so drop
519 to varying in this case. */
520 if (tree_int_cst_lt (max, min))
522 set_value_range_to_varying (vr);
526 t = t == VR_RANGE ? VR_ANTI_RANGE : VR_RANGE;
529 /* Anti-ranges that can be represented as ranges should be so. */
530 if (t == VR_ANTI_RANGE)
532 bool is_min = vrp_val_is_min (min);
533 bool is_max = vrp_val_is_max (max);
535 if (is_min && is_max)
537 /* We cannot deal with empty ranges, drop to varying.
538 ??? This could be VR_UNDEFINED instead. */
539 set_value_range_to_varying (vr);
542 else if (TYPE_PRECISION (TREE_TYPE (min)) == 1
543 && (is_min || is_max))
545 /* Non-empty boolean ranges can always be represented
546 as a singleton range. */
548 min = max = vrp_val_max (TREE_TYPE (min));
550 min = max = vrp_val_min (TREE_TYPE (min));
554 /* As a special exception preserve non-null ranges. */
555 && !(TYPE_UNSIGNED (TREE_TYPE (min))
556 && integer_zerop (max)))
558 tree one = build_int_cst (TREE_TYPE (max), 1);
559 min = int_const_binop (PLUS_EXPR, max, one);
560 max = vrp_val_max (TREE_TYPE (max));
565 tree one = build_int_cst (TREE_TYPE (min), 1);
566 max = int_const_binop (MINUS_EXPR, min, one);
567 min = vrp_val_min (TREE_TYPE (min));
572 /* Drop [-INF(OVF), +INF(OVF)] to varying. */
573 if (needs_overflow_infinity (TREE_TYPE (min))
574 && is_overflow_infinity (min)
575 && is_overflow_infinity (max))
577 set_value_range_to_varying (vr);
581 set_value_range (vr, t, min, max, equiv);
584 /* Copy value range FROM into value range TO. */
587 copy_value_range (value_range_t *to, value_range_t *from)
589 set_value_range (to, from->type, from->min, from->max, from->equiv);
592 /* Set value range VR to a single value. This function is only called
593 with values we get from statements, and exists to clear the
594 TREE_OVERFLOW flag so that we don't think we have an overflow
595 infinity when we shouldn't. */
598 set_value_range_to_value (value_range_t *vr, tree val, bitmap equiv)
600 gcc_assert (is_gimple_min_invariant (val));
601 val = avoid_overflow_infinity (val);
602 set_value_range (vr, VR_RANGE, val, val, equiv);
605 /* Set value range VR to a non-negative range of type TYPE.
606 OVERFLOW_INFINITY indicates whether to use an overflow infinity
607 rather than TYPE_MAX_VALUE; this should be true if we determine
608 that the range is nonnegative based on the assumption that signed
609 overflow does not occur. */
612 set_value_range_to_nonnegative (value_range_t *vr, tree type,
613 bool overflow_infinity)
617 if (overflow_infinity && !supports_overflow_infinity (type))
619 set_value_range_to_varying (vr);
623 zero = build_int_cst (type, 0);
624 set_value_range (vr, VR_RANGE, zero,
626 ? positive_overflow_infinity (type)
627 : TYPE_MAX_VALUE (type)),
631 /* Set value range VR to a non-NULL range of type TYPE. */
634 set_value_range_to_nonnull (value_range_t *vr, tree type)
636 tree zero = build_int_cst (type, 0);
637 set_value_range (vr, VR_ANTI_RANGE, zero, zero, vr->equiv);
641 /* Set value range VR to a NULL range of type TYPE. */
644 set_value_range_to_null (value_range_t *vr, tree type)
646 set_value_range_to_value (vr, build_int_cst (type, 0), vr->equiv);
650 /* Set value range VR to a range of a truthvalue of type TYPE. */
653 set_value_range_to_truthvalue (value_range_t *vr, tree type)
655 if (TYPE_PRECISION (type) == 1)
656 set_value_range_to_varying (vr);
658 set_value_range (vr, VR_RANGE,
659 build_int_cst (type, 0), build_int_cst (type, 1),
664 /* If abs (min) < abs (max), set VR to [-max, max], if
665 abs (min) >= abs (max), set VR to [-min, min]. */
668 abs_extent_range (value_range_t *vr, tree min, tree max)
672 gcc_assert (TREE_CODE (min) == INTEGER_CST);
673 gcc_assert (TREE_CODE (max) == INTEGER_CST);
674 gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (min)));
675 gcc_assert (!TYPE_UNSIGNED (TREE_TYPE (min)));
676 min = fold_unary (ABS_EXPR, TREE_TYPE (min), min);
677 max = fold_unary (ABS_EXPR, TREE_TYPE (max), max);
678 if (TREE_OVERFLOW (min) || TREE_OVERFLOW (max))
680 set_value_range_to_varying (vr);
683 cmp = compare_values (min, max);
685 min = fold_unary (NEGATE_EXPR, TREE_TYPE (min), max);
686 else if (cmp == 0 || cmp == 1)
689 min = fold_unary (NEGATE_EXPR, TREE_TYPE (min), min);
693 set_value_range_to_varying (vr);
696 set_and_canonicalize_value_range (vr, VR_RANGE, min, max, NULL);
700 /* Return value range information for VAR.
702 If we have no values ranges recorded (ie, VRP is not running), then
703 return NULL. Otherwise create an empty range if none existed for VAR. */
705 static value_range_t *
706 get_value_range (const_tree var)
708 static const struct value_range_d vr_const_varying
709 = { VR_VARYING, NULL_TREE, NULL_TREE, NULL };
712 unsigned ver = SSA_NAME_VERSION (var);
714 /* If we have no recorded ranges, then return NULL. */
718 /* If we query the range for a new SSA name return an unmodifiable VARYING.
719 We should get here at most from the substitute-and-fold stage which
720 will never try to change values. */
721 if (ver >= num_vr_values)
722 return CONST_CAST (value_range_t *, &vr_const_varying);
728 /* After propagation finished do not allocate new value-ranges. */
729 if (values_propagated)
730 return CONST_CAST (value_range_t *, &vr_const_varying);
732 /* Create a default value range. */
733 vr_value[ver] = vr = XCNEW (value_range_t);
735 /* Defer allocating the equivalence set. */
738 /* If VAR is a default definition of a parameter, the variable can
739 take any value in VAR's type. */
740 if (SSA_NAME_IS_DEFAULT_DEF (var))
742 sym = SSA_NAME_VAR (var);
743 if (TREE_CODE (sym) == PARM_DECL)
745 /* Try to use the "nonnull" attribute to create ~[0, 0]
746 anti-ranges for pointers. Note that this is only valid with
747 default definitions of PARM_DECLs. */
748 if (POINTER_TYPE_P (TREE_TYPE (sym))
749 && nonnull_arg_p (sym))
750 set_value_range_to_nonnull (vr, TREE_TYPE (sym));
752 set_value_range_to_varying (vr);
754 else if (TREE_CODE (sym) == RESULT_DECL
755 && DECL_BY_REFERENCE (sym))
756 set_value_range_to_nonnull (vr, TREE_TYPE (sym));
762 /* Return true, if VAL1 and VAL2 are equal values for VRP purposes. */
765 vrp_operand_equal_p (const_tree val1, const_tree val2)
769 if (!val1 || !val2 || !operand_equal_p (val1, val2, 0))
771 if (is_overflow_infinity (val1))
772 return is_overflow_infinity (val2);
776 /* Return true, if the bitmaps B1 and B2 are equal. */
779 vrp_bitmap_equal_p (const_bitmap b1, const_bitmap b2)
782 || ((!b1 || bitmap_empty_p (b1))
783 && (!b2 || bitmap_empty_p (b2)))
785 && bitmap_equal_p (b1, b2)));
788 /* Update the value range and equivalence set for variable VAR to
789 NEW_VR. Return true if NEW_VR is different from VAR's previous
792 NOTE: This function assumes that NEW_VR is a temporary value range
793 object created for the sole purpose of updating VAR's range. The
794 storage used by the equivalence set from NEW_VR will be freed by
795 this function. Do not call update_value_range when NEW_VR
796 is the range object associated with another SSA name. */
799 update_value_range (const_tree var, value_range_t *new_vr)
801 value_range_t *old_vr;
804 /* Update the value range, if necessary. */
805 old_vr = get_value_range (var);
806 is_new = old_vr->type != new_vr->type
807 || !vrp_operand_equal_p (old_vr->min, new_vr->min)
808 || !vrp_operand_equal_p (old_vr->max, new_vr->max)
809 || !vrp_bitmap_equal_p (old_vr->equiv, new_vr->equiv);
813 /* Do not allow transitions up the lattice. The following
814 is slightly more awkward than just new_vr->type < old_vr->type
815 because VR_RANGE and VR_ANTI_RANGE need to be considered
816 the same. We may not have is_new when transitioning to
817 UNDEFINED or from VARYING. */
818 if (new_vr->type == VR_UNDEFINED
819 || old_vr->type == VR_VARYING)
820 set_value_range_to_varying (old_vr);
822 set_value_range (old_vr, new_vr->type, new_vr->min, new_vr->max,
826 BITMAP_FREE (new_vr->equiv);
832 /* Add VAR and VAR's equivalence set to EQUIV. This is the central
833 point where equivalence processing can be turned on/off. */
836 add_equivalence (bitmap *equiv, const_tree var)
838 unsigned ver = SSA_NAME_VERSION (var);
839 value_range_t *vr = vr_value[ver];
842 *equiv = BITMAP_ALLOC (NULL);
843 bitmap_set_bit (*equiv, ver);
845 bitmap_ior_into (*equiv, vr->equiv);
849 /* Return true if VR is ~[0, 0]. */
852 range_is_nonnull (value_range_t *vr)
854 return vr->type == VR_ANTI_RANGE
855 && integer_zerop (vr->min)
856 && integer_zerop (vr->max);
860 /* Return true if VR is [0, 0]. */
863 range_is_null (value_range_t *vr)
865 return vr->type == VR_RANGE
866 && integer_zerop (vr->min)
867 && integer_zerop (vr->max);
870 /* Return true if max and min of VR are INTEGER_CST. It's not necessary
874 range_int_cst_p (value_range_t *vr)
876 return (vr->type == VR_RANGE
877 && TREE_CODE (vr->max) == INTEGER_CST
878 && TREE_CODE (vr->min) == INTEGER_CST);
881 /* Return true if VR is a INTEGER_CST singleton. */
884 range_int_cst_singleton_p (value_range_t *vr)
886 return (range_int_cst_p (vr)
887 && !TREE_OVERFLOW (vr->min)
888 && !TREE_OVERFLOW (vr->max)
889 && tree_int_cst_equal (vr->min, vr->max));
892 /* Return true if value range VR involves at least one symbol. */
895 symbolic_range_p (value_range_t *vr)
897 return (!is_gimple_min_invariant (vr->min)
898 || !is_gimple_min_invariant (vr->max));
901 /* Return true if value range VR uses an overflow infinity. */
904 overflow_infinity_range_p (value_range_t *vr)
906 return (vr->type == VR_RANGE
907 && (is_overflow_infinity (vr->min)
908 || is_overflow_infinity (vr->max)));
911 /* Return false if we can not make a valid comparison based on VR;
912 this will be the case if it uses an overflow infinity and overflow
913 is not undefined (i.e., -fno-strict-overflow is in effect).
914 Otherwise return true, and set *STRICT_OVERFLOW_P to true if VR
915 uses an overflow infinity. */
918 usable_range_p (value_range_t *vr, bool *strict_overflow_p)
920 gcc_assert (vr->type == VR_RANGE);
921 if (is_overflow_infinity (vr->min))
923 *strict_overflow_p = true;
924 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (vr->min)))
927 if (is_overflow_infinity (vr->max))
929 *strict_overflow_p = true;
930 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (vr->max)))
937 /* Return true if the result of assignment STMT is know to be non-negative.
938 If the return value is based on the assumption that signed overflow is
939 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
940 *STRICT_OVERFLOW_P.*/
943 gimple_assign_nonnegative_warnv_p (gimple stmt, bool *strict_overflow_p)
945 enum tree_code code = gimple_assign_rhs_code (stmt);
946 switch (get_gimple_rhs_class (code))
948 case GIMPLE_UNARY_RHS:
949 return tree_unary_nonnegative_warnv_p (gimple_assign_rhs_code (stmt),
950 gimple_expr_type (stmt),
951 gimple_assign_rhs1 (stmt),
953 case GIMPLE_BINARY_RHS:
954 return tree_binary_nonnegative_warnv_p (gimple_assign_rhs_code (stmt),
955 gimple_expr_type (stmt),
956 gimple_assign_rhs1 (stmt),
957 gimple_assign_rhs2 (stmt),
959 case GIMPLE_TERNARY_RHS:
961 case GIMPLE_SINGLE_RHS:
962 return tree_single_nonnegative_warnv_p (gimple_assign_rhs1 (stmt),
964 case GIMPLE_INVALID_RHS:
971 /* Return true if return value of call STMT is know to be non-negative.
972 If the return value is based on the assumption that signed overflow is
973 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
974 *STRICT_OVERFLOW_P.*/
977 gimple_call_nonnegative_warnv_p (gimple stmt, bool *strict_overflow_p)
979 tree arg0 = gimple_call_num_args (stmt) > 0 ?
980 gimple_call_arg (stmt, 0) : NULL_TREE;
981 tree arg1 = gimple_call_num_args (stmt) > 1 ?
982 gimple_call_arg (stmt, 1) : NULL_TREE;
984 return tree_call_nonnegative_warnv_p (gimple_expr_type (stmt),
985 gimple_call_fndecl (stmt),
991 /* Return true if STMT is know to to compute a non-negative value.
992 If the return value is based on the assumption that signed overflow is
993 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
994 *STRICT_OVERFLOW_P.*/
997 gimple_stmt_nonnegative_warnv_p (gimple stmt, bool *strict_overflow_p)
999 switch (gimple_code (stmt))
1002 return gimple_assign_nonnegative_warnv_p (stmt, strict_overflow_p);
1004 return gimple_call_nonnegative_warnv_p (stmt, strict_overflow_p);
1010 /* Return true if the result of assignment STMT is know to be non-zero.
1011 If the return value is based on the assumption that signed overflow is
1012 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1013 *STRICT_OVERFLOW_P.*/
1016 gimple_assign_nonzero_warnv_p (gimple stmt, bool *strict_overflow_p)
1018 enum tree_code code = gimple_assign_rhs_code (stmt);
1019 switch (get_gimple_rhs_class (code))
1021 case GIMPLE_UNARY_RHS:
1022 return tree_unary_nonzero_warnv_p (gimple_assign_rhs_code (stmt),
1023 gimple_expr_type (stmt),
1024 gimple_assign_rhs1 (stmt),
1026 case GIMPLE_BINARY_RHS:
1027 return tree_binary_nonzero_warnv_p (gimple_assign_rhs_code (stmt),
1028 gimple_expr_type (stmt),
1029 gimple_assign_rhs1 (stmt),
1030 gimple_assign_rhs2 (stmt),
1032 case GIMPLE_TERNARY_RHS:
1034 case GIMPLE_SINGLE_RHS:
1035 return tree_single_nonzero_warnv_p (gimple_assign_rhs1 (stmt),
1037 case GIMPLE_INVALID_RHS:
1044 /* Return true if STMT is know to to compute a non-zero value.
1045 If the return value is based on the assumption that signed overflow is
1046 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1047 *STRICT_OVERFLOW_P.*/
1050 gimple_stmt_nonzero_warnv_p (gimple stmt, bool *strict_overflow_p)
1052 switch (gimple_code (stmt))
1055 return gimple_assign_nonzero_warnv_p (stmt, strict_overflow_p);
1057 return gimple_alloca_call_p (stmt);
1063 /* Like tree_expr_nonzero_warnv_p, but this function uses value ranges
1067 vrp_stmt_computes_nonzero (gimple stmt, bool *strict_overflow_p)
1069 if (gimple_stmt_nonzero_warnv_p (stmt, strict_overflow_p))
1072 /* If we have an expression of the form &X->a, then the expression
1073 is nonnull if X is nonnull. */
1074 if (is_gimple_assign (stmt)
1075 && gimple_assign_rhs_code (stmt) == ADDR_EXPR)
1077 tree expr = gimple_assign_rhs1 (stmt);
1078 tree base = get_base_address (TREE_OPERAND (expr, 0));
1080 if (base != NULL_TREE
1081 && TREE_CODE (base) == MEM_REF
1082 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
1084 value_range_t *vr = get_value_range (TREE_OPERAND (base, 0));
1085 if (range_is_nonnull (vr))
1093 /* Returns true if EXPR is a valid value (as expected by compare_values) --
1094 a gimple invariant, or SSA_NAME +- CST. */
1097 valid_value_p (tree expr)
1099 if (TREE_CODE (expr) == SSA_NAME)
1102 if (TREE_CODE (expr) == PLUS_EXPR
1103 || TREE_CODE (expr) == MINUS_EXPR)
1104 return (TREE_CODE (TREE_OPERAND (expr, 0)) == SSA_NAME
1105 && TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST);
1107 return is_gimple_min_invariant (expr);
1113 -2 if those are incomparable. */
1115 operand_less_p (tree val, tree val2)
1117 /* LT is folded faster than GE and others. Inline the common case. */
1118 if (TREE_CODE (val) == INTEGER_CST && TREE_CODE (val2) == INTEGER_CST)
1120 if (TYPE_UNSIGNED (TREE_TYPE (val)))
1121 return INT_CST_LT_UNSIGNED (val, val2);
1124 if (INT_CST_LT (val, val2))
1132 fold_defer_overflow_warnings ();
1134 tcmp = fold_binary_to_constant (LT_EXPR, boolean_type_node, val, val2);
1136 fold_undefer_and_ignore_overflow_warnings ();
1139 || TREE_CODE (tcmp) != INTEGER_CST)
1142 if (!integer_zerop (tcmp))
1146 /* val >= val2, not considering overflow infinity. */
1147 if (is_negative_overflow_infinity (val))
1148 return is_negative_overflow_infinity (val2) ? 0 : 1;
1149 else if (is_positive_overflow_infinity (val2))
1150 return is_positive_overflow_infinity (val) ? 0 : 1;
1155 /* Compare two values VAL1 and VAL2. Return
1157 -2 if VAL1 and VAL2 cannot be compared at compile-time,
1160 +1 if VAL1 > VAL2, and
1163 This is similar to tree_int_cst_compare but supports pointer values
1164 and values that cannot be compared at compile time.
1166 If STRICT_OVERFLOW_P is not NULL, then set *STRICT_OVERFLOW_P to
1167 true if the return value is only valid if we assume that signed
1168 overflow is undefined. */
1171 compare_values_warnv (tree val1, tree val2, bool *strict_overflow_p)
1176 /* Below we rely on the fact that VAL1 and VAL2 are both pointers or
1178 gcc_assert (POINTER_TYPE_P (TREE_TYPE (val1))
1179 == POINTER_TYPE_P (TREE_TYPE (val2)));
1180 /* Convert the two values into the same type. This is needed because
1181 sizetype causes sign extension even for unsigned types. */
1182 val2 = fold_convert (TREE_TYPE (val1), val2);
1183 STRIP_USELESS_TYPE_CONVERSION (val2);
1185 if ((TREE_CODE (val1) == SSA_NAME
1186 || TREE_CODE (val1) == PLUS_EXPR
1187 || TREE_CODE (val1) == MINUS_EXPR)
1188 && (TREE_CODE (val2) == SSA_NAME
1189 || TREE_CODE (val2) == PLUS_EXPR
1190 || TREE_CODE (val2) == MINUS_EXPR))
1192 tree n1, c1, n2, c2;
1193 enum tree_code code1, code2;
1195 /* If VAL1 and VAL2 are of the form 'NAME [+-] CST' or 'NAME',
1196 return -1 or +1 accordingly. If VAL1 and VAL2 don't use the
1197 same name, return -2. */
1198 if (TREE_CODE (val1) == SSA_NAME)
1206 code1 = TREE_CODE (val1);
1207 n1 = TREE_OPERAND (val1, 0);
1208 c1 = TREE_OPERAND (val1, 1);
1209 if (tree_int_cst_sgn (c1) == -1)
1211 if (is_negative_overflow_infinity (c1))
1213 c1 = fold_unary_to_constant (NEGATE_EXPR, TREE_TYPE (c1), c1);
1216 code1 = code1 == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR;
1220 if (TREE_CODE (val2) == SSA_NAME)
1228 code2 = TREE_CODE (val2);
1229 n2 = TREE_OPERAND (val2, 0);
1230 c2 = TREE_OPERAND (val2, 1);
1231 if (tree_int_cst_sgn (c2) == -1)
1233 if (is_negative_overflow_infinity (c2))
1235 c2 = fold_unary_to_constant (NEGATE_EXPR, TREE_TYPE (c2), c2);
1238 code2 = code2 == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR;
1242 /* Both values must use the same name. */
1246 if (code1 == SSA_NAME
1247 && code2 == SSA_NAME)
1251 /* If overflow is defined we cannot simplify more. */
1252 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (val1)))
1255 if (strict_overflow_p != NULL
1256 && (code1 == SSA_NAME || !TREE_NO_WARNING (val1))
1257 && (code2 == SSA_NAME || !TREE_NO_WARNING (val2)))
1258 *strict_overflow_p = true;
1260 if (code1 == SSA_NAME)
1262 if (code2 == PLUS_EXPR)
1263 /* NAME < NAME + CST */
1265 else if (code2 == MINUS_EXPR)
1266 /* NAME > NAME - CST */
1269 else if (code1 == PLUS_EXPR)
1271 if (code2 == SSA_NAME)
1272 /* NAME + CST > NAME */
1274 else if (code2 == PLUS_EXPR)
1275 /* NAME + CST1 > NAME + CST2, if CST1 > CST2 */
1276 return compare_values_warnv (c1, c2, strict_overflow_p);
1277 else if (code2 == MINUS_EXPR)
1278 /* NAME + CST1 > NAME - CST2 */
1281 else if (code1 == MINUS_EXPR)
1283 if (code2 == SSA_NAME)
1284 /* NAME - CST < NAME */
1286 else if (code2 == PLUS_EXPR)
1287 /* NAME - CST1 < NAME + CST2 */
1289 else if (code2 == MINUS_EXPR)
1290 /* NAME - CST1 > NAME - CST2, if CST1 < CST2. Notice that
1291 C1 and C2 are swapped in the call to compare_values. */
1292 return compare_values_warnv (c2, c1, strict_overflow_p);
1298 /* We cannot compare non-constants. */
1299 if (!is_gimple_min_invariant (val1) || !is_gimple_min_invariant (val2))
1302 if (!POINTER_TYPE_P (TREE_TYPE (val1)))
1304 /* We cannot compare overflowed values, except for overflow
1306 if (TREE_OVERFLOW (val1) || TREE_OVERFLOW (val2))
1308 if (strict_overflow_p != NULL)
1309 *strict_overflow_p = true;
1310 if (is_negative_overflow_infinity (val1))
1311 return is_negative_overflow_infinity (val2) ? 0 : -1;
1312 else if (is_negative_overflow_infinity (val2))
1314 else if (is_positive_overflow_infinity (val1))
1315 return is_positive_overflow_infinity (val2) ? 0 : 1;
1316 else if (is_positive_overflow_infinity (val2))
1321 return tree_int_cst_compare (val1, val2);
1327 /* First see if VAL1 and VAL2 are not the same. */
1328 if (val1 == val2 || operand_equal_p (val1, val2, 0))
1331 /* If VAL1 is a lower address than VAL2, return -1. */
1332 if (operand_less_p (val1, val2) == 1)
1335 /* If VAL1 is a higher address than VAL2, return +1. */
1336 if (operand_less_p (val2, val1) == 1)
1339 /* If VAL1 is different than VAL2, return +2.
1340 For integer constants we either have already returned -1 or 1
1341 or they are equivalent. We still might succeed in proving
1342 something about non-trivial operands. */
1343 if (TREE_CODE (val1) != INTEGER_CST
1344 || TREE_CODE (val2) != INTEGER_CST)
1346 t = fold_binary_to_constant (NE_EXPR, boolean_type_node, val1, val2);
1347 if (t && integer_onep (t))
1355 /* Compare values like compare_values_warnv, but treat comparisons of
1356 nonconstants which rely on undefined overflow as incomparable. */
1359 compare_values (tree val1, tree val2)
1365 ret = compare_values_warnv (val1, val2, &sop);
1367 && (!is_gimple_min_invariant (val1) || !is_gimple_min_invariant (val2)))
1373 /* Return 1 if VAL is inside value range MIN <= VAL <= MAX,
1374 0 if VAL is not inside [MIN, MAX],
1375 -2 if we cannot tell either way.
1377 Benchmark compile/20001226-1.c compilation time after changing this
1381 value_inside_range (tree val, tree min, tree max)
1385 cmp1 = operand_less_p (val, min);
1391 cmp2 = operand_less_p (max, val);
1399 /* Return true if value ranges VR0 and VR1 have a non-empty
1402 Benchmark compile/20001226-1.c compilation time after changing this
1407 value_ranges_intersect_p (value_range_t *vr0, value_range_t *vr1)
1409 /* The value ranges do not intersect if the maximum of the first range is
1410 less than the minimum of the second range or vice versa.
1411 When those relations are unknown, we can't do any better. */
1412 if (operand_less_p (vr0->max, vr1->min) != 0)
1414 if (operand_less_p (vr1->max, vr0->min) != 0)
1420 /* Return 1 if [MIN, MAX] includes the value zero, 0 if it does not
1421 include the value zero, -2 if we cannot tell. */
1424 range_includes_zero_p (tree min, tree max)
1426 tree zero = build_int_cst (TREE_TYPE (min), 0);
1427 return value_inside_range (zero, min, max);
1430 /* Return true if *VR is know to only contain nonnegative values. */
1433 value_range_nonnegative_p (value_range_t *vr)
1435 /* Testing for VR_ANTI_RANGE is not useful here as any anti-range
1436 which would return a useful value should be encoded as a
1438 if (vr->type == VR_RANGE)
1440 int result = compare_values (vr->min, integer_zero_node);
1441 return (result == 0 || result == 1);
1447 /* Return true if T, an SSA_NAME, is known to be nonnegative. Return
1448 false otherwise or if no value range information is available. */
1451 ssa_name_nonnegative_p (const_tree t)
1453 value_range_t *vr = get_value_range (t);
1455 if (INTEGRAL_TYPE_P (t)
1456 && TYPE_UNSIGNED (t))
1462 return value_range_nonnegative_p (vr);
1465 /* If *VR has a value rante that is a single constant value return that,
1466 otherwise return NULL_TREE. */
1469 value_range_constant_singleton (value_range_t *vr)
1471 if (vr->type == VR_RANGE
1472 && operand_equal_p (vr->min, vr->max, 0)
1473 && is_gimple_min_invariant (vr->min))
1479 /* If OP has a value range with a single constant value return that,
1480 otherwise return NULL_TREE. This returns OP itself if OP is a
1484 op_with_constant_singleton_value_range (tree op)
1486 if (is_gimple_min_invariant (op))
1489 if (TREE_CODE (op) != SSA_NAME)
1492 return value_range_constant_singleton (get_value_range (op));
1495 /* Return true if op is in a boolean [0, 1] value-range. */
1498 op_with_boolean_value_range_p (tree op)
1502 if (TYPE_PRECISION (TREE_TYPE (op)) == 1)
1505 if (integer_zerop (op)
1506 || integer_onep (op))
1509 if (TREE_CODE (op) != SSA_NAME)
1512 vr = get_value_range (op);
1513 return (vr->type == VR_RANGE
1514 && integer_zerop (vr->min)
1515 && integer_onep (vr->max));
1518 /* Extract value range information from an ASSERT_EXPR EXPR and store
1522 extract_range_from_assert (value_range_t *vr_p, tree expr)
1524 tree var, cond, limit, min, max, type;
1525 value_range_t *limit_vr;
1526 enum tree_code cond_code;
1528 var = ASSERT_EXPR_VAR (expr);
1529 cond = ASSERT_EXPR_COND (expr);
1531 gcc_assert (COMPARISON_CLASS_P (cond));
1533 /* Find VAR in the ASSERT_EXPR conditional. */
1534 if (var == TREE_OPERAND (cond, 0)
1535 || TREE_CODE (TREE_OPERAND (cond, 0)) == PLUS_EXPR
1536 || TREE_CODE (TREE_OPERAND (cond, 0)) == NOP_EXPR)
1538 /* If the predicate is of the form VAR COMP LIMIT, then we just
1539 take LIMIT from the RHS and use the same comparison code. */
1540 cond_code = TREE_CODE (cond);
1541 limit = TREE_OPERAND (cond, 1);
1542 cond = TREE_OPERAND (cond, 0);
1546 /* If the predicate is of the form LIMIT COMP VAR, then we need
1547 to flip around the comparison code to create the proper range
1549 cond_code = swap_tree_comparison (TREE_CODE (cond));
1550 limit = TREE_OPERAND (cond, 0);
1551 cond = TREE_OPERAND (cond, 1);
1554 limit = avoid_overflow_infinity (limit);
1556 type = TREE_TYPE (var);
1557 gcc_assert (limit != var);
1559 /* For pointer arithmetic, we only keep track of pointer equality
1561 if (POINTER_TYPE_P (type) && cond_code != NE_EXPR && cond_code != EQ_EXPR)
1563 set_value_range_to_varying (vr_p);
1567 /* If LIMIT is another SSA name and LIMIT has a range of its own,
1568 try to use LIMIT's range to avoid creating symbolic ranges
1570 limit_vr = (TREE_CODE (limit) == SSA_NAME) ? get_value_range (limit) : NULL;
1572 /* LIMIT's range is only interesting if it has any useful information. */
1574 && (limit_vr->type == VR_UNDEFINED
1575 || limit_vr->type == VR_VARYING
1576 || symbolic_range_p (limit_vr)))
1579 /* Initially, the new range has the same set of equivalences of
1580 VAR's range. This will be revised before returning the final
1581 value. Since assertions may be chained via mutually exclusive
1582 predicates, we will need to trim the set of equivalences before
1584 gcc_assert (vr_p->equiv == NULL);
1585 add_equivalence (&vr_p->equiv, var);
1587 /* Extract a new range based on the asserted comparison for VAR and
1588 LIMIT's value range. Notice that if LIMIT has an anti-range, we
1589 will only use it for equality comparisons (EQ_EXPR). For any
1590 other kind of assertion, we cannot derive a range from LIMIT's
1591 anti-range that can be used to describe the new range. For
1592 instance, ASSERT_EXPR <x_2, x_2 <= b_4>. If b_4 is ~[2, 10],
1593 then b_4 takes on the ranges [-INF, 1] and [11, +INF]. There is
1594 no single range for x_2 that could describe LE_EXPR, so we might
1595 as well build the range [b_4, +INF] for it.
1596 One special case we handle is extracting a range from a
1597 range test encoded as (unsigned)var + CST <= limit. */
1598 if (TREE_CODE (cond) == NOP_EXPR
1599 || TREE_CODE (cond) == PLUS_EXPR)
1601 if (TREE_CODE (cond) == PLUS_EXPR)
1603 min = fold_build1 (NEGATE_EXPR, TREE_TYPE (TREE_OPERAND (cond, 1)),
1604 TREE_OPERAND (cond, 1));
1605 max = int_const_binop (PLUS_EXPR, limit, min);
1606 cond = TREE_OPERAND (cond, 0);
1610 min = build_int_cst (TREE_TYPE (var), 0);
1614 /* Make sure to not set TREE_OVERFLOW on the final type
1615 conversion. We are willingly interpreting large positive
1616 unsigned values as negative singed values here. */
1617 min = force_fit_type_double (TREE_TYPE (var), tree_to_double_int (min),
1619 max = force_fit_type_double (TREE_TYPE (var), tree_to_double_int (max),
1622 /* We can transform a max, min range to an anti-range or
1623 vice-versa. Use set_and_canonicalize_value_range which does
1625 if (cond_code == LE_EXPR)
1626 set_and_canonicalize_value_range (vr_p, VR_RANGE,
1627 min, max, vr_p->equiv);
1628 else if (cond_code == GT_EXPR)
1629 set_and_canonicalize_value_range (vr_p, VR_ANTI_RANGE,
1630 min, max, vr_p->equiv);
1634 else if (cond_code == EQ_EXPR)
1636 enum value_range_type range_type;
1640 range_type = limit_vr->type;
1641 min = limit_vr->min;
1642 max = limit_vr->max;
1646 range_type = VR_RANGE;
1651 set_value_range (vr_p, range_type, min, max, vr_p->equiv);
1653 /* When asserting the equality VAR == LIMIT and LIMIT is another
1654 SSA name, the new range will also inherit the equivalence set
1656 if (TREE_CODE (limit) == SSA_NAME)
1657 add_equivalence (&vr_p->equiv, limit);
1659 else if (cond_code == NE_EXPR)
1661 /* As described above, when LIMIT's range is an anti-range and
1662 this assertion is an inequality (NE_EXPR), then we cannot
1663 derive anything from the anti-range. For instance, if
1664 LIMIT's range was ~[0, 0], the assertion 'VAR != LIMIT' does
1665 not imply that VAR's range is [0, 0]. So, in the case of
1666 anti-ranges, we just assert the inequality using LIMIT and
1669 If LIMIT_VR is a range, we can only use it to build a new
1670 anti-range if LIMIT_VR is a single-valued range. For
1671 instance, if LIMIT_VR is [0, 1], the predicate
1672 VAR != [0, 1] does not mean that VAR's range is ~[0, 1].
1673 Rather, it means that for value 0 VAR should be ~[0, 0]
1674 and for value 1, VAR should be ~[1, 1]. We cannot
1675 represent these ranges.
1677 The only situation in which we can build a valid
1678 anti-range is when LIMIT_VR is a single-valued range
1679 (i.e., LIMIT_VR->MIN == LIMIT_VR->MAX). In that case,
1680 build the anti-range ~[LIMIT_VR->MIN, LIMIT_VR->MAX]. */
1682 && limit_vr->type == VR_RANGE
1683 && compare_values (limit_vr->min, limit_vr->max) == 0)
1685 min = limit_vr->min;
1686 max = limit_vr->max;
1690 /* In any other case, we cannot use LIMIT's range to build a
1691 valid anti-range. */
1695 /* If MIN and MAX cover the whole range for their type, then
1696 just use the original LIMIT. */
1697 if (INTEGRAL_TYPE_P (type)
1698 && vrp_val_is_min (min)
1699 && vrp_val_is_max (max))
1702 set_and_canonicalize_value_range (vr_p, VR_ANTI_RANGE,
1703 min, max, vr_p->equiv);
1705 else if (cond_code == LE_EXPR || cond_code == LT_EXPR)
1707 min = TYPE_MIN_VALUE (type);
1709 if (limit_vr == NULL || limit_vr->type == VR_ANTI_RANGE)
1713 /* If LIMIT_VR is of the form [N1, N2], we need to build the
1714 range [MIN, N2] for LE_EXPR and [MIN, N2 - 1] for
1716 max = limit_vr->max;
1719 /* If the maximum value forces us to be out of bounds, simply punt.
1720 It would be pointless to try and do anything more since this
1721 all should be optimized away above us. */
1722 if ((cond_code == LT_EXPR
1723 && compare_values (max, min) == 0)
1724 || (CONSTANT_CLASS_P (max) && TREE_OVERFLOW (max)))
1725 set_value_range_to_varying (vr_p);
1728 /* For LT_EXPR, we create the range [MIN, MAX - 1]. */
1729 if (cond_code == LT_EXPR)
1731 if (TYPE_PRECISION (TREE_TYPE (max)) == 1
1732 && !TYPE_UNSIGNED (TREE_TYPE (max)))
1733 max = fold_build2 (PLUS_EXPR, TREE_TYPE (max), max,
1734 build_int_cst (TREE_TYPE (max), -1));
1736 max = fold_build2 (MINUS_EXPR, TREE_TYPE (max), max,
1737 build_int_cst (TREE_TYPE (max), 1));
1739 TREE_NO_WARNING (max) = 1;
1742 set_value_range (vr_p, VR_RANGE, min, max, vr_p->equiv);
1745 else if (cond_code == GE_EXPR || cond_code == GT_EXPR)
1747 max = TYPE_MAX_VALUE (type);
1749 if (limit_vr == NULL || limit_vr->type == VR_ANTI_RANGE)
1753 /* If LIMIT_VR is of the form [N1, N2], we need to build the
1754 range [N1, MAX] for GE_EXPR and [N1 + 1, MAX] for
1756 min = limit_vr->min;
1759 /* If the minimum value forces us to be out of bounds, simply punt.
1760 It would be pointless to try and do anything more since this
1761 all should be optimized away above us. */
1762 if ((cond_code == GT_EXPR
1763 && compare_values (min, max) == 0)
1764 || (CONSTANT_CLASS_P (min) && TREE_OVERFLOW (min)))
1765 set_value_range_to_varying (vr_p);
1768 /* For GT_EXPR, we create the range [MIN + 1, MAX]. */
1769 if (cond_code == GT_EXPR)
1771 if (TYPE_PRECISION (TREE_TYPE (min)) == 1
1772 && !TYPE_UNSIGNED (TREE_TYPE (min)))
1773 min = fold_build2 (MINUS_EXPR, TREE_TYPE (min), min,
1774 build_int_cst (TREE_TYPE (min), -1));
1776 min = fold_build2 (PLUS_EXPR, TREE_TYPE (min), min,
1777 build_int_cst (TREE_TYPE (min), 1));
1779 TREE_NO_WARNING (min) = 1;
1782 set_value_range (vr_p, VR_RANGE, min, max, vr_p->equiv);
1788 /* Finally intersect the new range with what we already know about var. */
1789 vrp_intersect_ranges (vr_p, get_value_range (var));
1793 /* Extract range information from SSA name VAR and store it in VR. If
1794 VAR has an interesting range, use it. Otherwise, create the
1795 range [VAR, VAR] and return it. This is useful in situations where
1796 we may have conditionals testing values of VARYING names. For
1803 Even if y_5 is deemed VARYING, we can determine that x_3 > y_5 is
1807 extract_range_from_ssa_name (value_range_t *vr, tree var)
1809 value_range_t *var_vr = get_value_range (var);
1811 if (var_vr->type != VR_UNDEFINED && var_vr->type != VR_VARYING)
1812 copy_value_range (vr, var_vr);
1814 set_value_range (vr, VR_RANGE, var, var, NULL);
1816 add_equivalence (&vr->equiv, var);
1820 /* Wrapper around int_const_binop. If the operation overflows and we
1821 are not using wrapping arithmetic, then adjust the result to be
1822 -INF or +INF depending on CODE, VAL1 and VAL2. This can return
1823 NULL_TREE if we need to use an overflow infinity representation but
1824 the type does not support it. */
1827 vrp_int_const_binop (enum tree_code code, tree val1, tree val2)
1831 res = int_const_binop (code, val1, val2);
1833 /* If we are using unsigned arithmetic, operate symbolically
1834 on -INF and +INF as int_const_binop only handles signed overflow. */
1835 if (TYPE_UNSIGNED (TREE_TYPE (val1)))
1837 int checkz = compare_values (res, val1);
1838 bool overflow = false;
1840 /* Ensure that res = val1 [+*] val2 >= val1
1841 or that res = val1 - val2 <= val1. */
1842 if ((code == PLUS_EXPR
1843 && !(checkz == 1 || checkz == 0))
1844 || (code == MINUS_EXPR
1845 && !(checkz == 0 || checkz == -1)))
1849 /* Checking for multiplication overflow is done by dividing the
1850 output of the multiplication by the first input of the
1851 multiplication. If the result of that division operation is
1852 not equal to the second input of the multiplication, then the
1853 multiplication overflowed. */
1854 else if (code == MULT_EXPR && !integer_zerop (val1))
1856 tree tmp = int_const_binop (TRUNC_DIV_EXPR,
1859 int check = compare_values (tmp, val2);
1867 res = copy_node (res);
1868 TREE_OVERFLOW (res) = 1;
1872 else if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (val1)))
1873 /* If the singed operation wraps then int_const_binop has done
1874 everything we want. */
1876 else if ((TREE_OVERFLOW (res)
1877 && !TREE_OVERFLOW (val1)
1878 && !TREE_OVERFLOW (val2))
1879 || is_overflow_infinity (val1)
1880 || is_overflow_infinity (val2))
1882 /* If the operation overflowed but neither VAL1 nor VAL2 are
1883 overflown, return -INF or +INF depending on the operation
1884 and the combination of signs of the operands. */
1885 int sgn1 = tree_int_cst_sgn (val1);
1886 int sgn2 = tree_int_cst_sgn (val2);
1888 if (needs_overflow_infinity (TREE_TYPE (res))
1889 && !supports_overflow_infinity (TREE_TYPE (res)))
1892 /* We have to punt on adding infinities of different signs,
1893 since we can't tell what the sign of the result should be.
1894 Likewise for subtracting infinities of the same sign. */
1895 if (((code == PLUS_EXPR && sgn1 != sgn2)
1896 || (code == MINUS_EXPR && sgn1 == sgn2))
1897 && is_overflow_infinity (val1)
1898 && is_overflow_infinity (val2))
1901 /* Don't try to handle division or shifting of infinities. */
1902 if ((code == TRUNC_DIV_EXPR
1903 || code == FLOOR_DIV_EXPR
1904 || code == CEIL_DIV_EXPR
1905 || code == EXACT_DIV_EXPR
1906 || code == ROUND_DIV_EXPR
1907 || code == RSHIFT_EXPR)
1908 && (is_overflow_infinity (val1)
1909 || is_overflow_infinity (val2)))
1912 /* Notice that we only need to handle the restricted set of
1913 operations handled by extract_range_from_binary_expr.
1914 Among them, only multiplication, addition and subtraction
1915 can yield overflow without overflown operands because we
1916 are working with integral types only... except in the
1917 case VAL1 = -INF and VAL2 = -1 which overflows to +INF
1918 for division too. */
1920 /* For multiplication, the sign of the overflow is given
1921 by the comparison of the signs of the operands. */
1922 if ((code == MULT_EXPR && sgn1 == sgn2)
1923 /* For addition, the operands must be of the same sign
1924 to yield an overflow. Its sign is therefore that
1925 of one of the operands, for example the first. For
1926 infinite operands X + -INF is negative, not positive. */
1927 || (code == PLUS_EXPR
1929 ? !is_negative_overflow_infinity (val2)
1930 : is_positive_overflow_infinity (val2)))
1931 /* For subtraction, non-infinite operands must be of
1932 different signs to yield an overflow. Its sign is
1933 therefore that of the first operand or the opposite of
1934 that of the second operand. A first operand of 0 counts
1935 as positive here, for the corner case 0 - (-INF), which
1936 overflows, but must yield +INF. For infinite operands 0
1937 - INF is negative, not positive. */
1938 || (code == MINUS_EXPR
1940 ? !is_positive_overflow_infinity (val2)
1941 : is_negative_overflow_infinity (val2)))
1942 /* We only get in here with positive shift count, so the
1943 overflow direction is the same as the sign of val1.
1944 Actually rshift does not overflow at all, but we only
1945 handle the case of shifting overflowed -INF and +INF. */
1946 || (code == RSHIFT_EXPR
1948 /* For division, the only case is -INF / -1 = +INF. */
1949 || code == TRUNC_DIV_EXPR
1950 || code == FLOOR_DIV_EXPR
1951 || code == CEIL_DIV_EXPR
1952 || code == EXACT_DIV_EXPR
1953 || code == ROUND_DIV_EXPR)
1954 return (needs_overflow_infinity (TREE_TYPE (res))
1955 ? positive_overflow_infinity (TREE_TYPE (res))
1956 : TYPE_MAX_VALUE (TREE_TYPE (res)));
1958 return (needs_overflow_infinity (TREE_TYPE (res))
1959 ? negative_overflow_infinity (TREE_TYPE (res))
1960 : TYPE_MIN_VALUE (TREE_TYPE (res)));
1967 /* For range VR compute two double_int bitmasks. In *MAY_BE_NONZERO
1968 bitmask if some bit is unset, it means for all numbers in the range
1969 the bit is 0, otherwise it might be 0 or 1. In *MUST_BE_NONZERO
1970 bitmask if some bit is set, it means for all numbers in the range
1971 the bit is 1, otherwise it might be 0 or 1. */
1974 zero_nonzero_bits_from_vr (value_range_t *vr,
1975 double_int *may_be_nonzero,
1976 double_int *must_be_nonzero)
1978 *may_be_nonzero = double_int_minus_one;
1979 *must_be_nonzero = double_int_zero;
1980 if (!range_int_cst_p (vr)
1981 || TREE_OVERFLOW (vr->min)
1982 || TREE_OVERFLOW (vr->max))
1985 if (range_int_cst_singleton_p (vr))
1987 *may_be_nonzero = tree_to_double_int (vr->min);
1988 *must_be_nonzero = *may_be_nonzero;
1990 else if (tree_int_cst_sgn (vr->min) >= 0
1991 || tree_int_cst_sgn (vr->max) < 0)
1993 double_int dmin = tree_to_double_int (vr->min);
1994 double_int dmax = tree_to_double_int (vr->max);
1995 double_int xor_mask = dmin ^ dmax;
1996 *may_be_nonzero = dmin | dmax;
1997 *must_be_nonzero = dmin & dmax;
1998 if (xor_mask.high != 0)
2000 unsigned HOST_WIDE_INT mask
2001 = ((unsigned HOST_WIDE_INT) 1
2002 << floor_log2 (xor_mask.high)) - 1;
2003 may_be_nonzero->low = ALL_ONES;
2004 may_be_nonzero->high |= mask;
2005 must_be_nonzero->low = 0;
2006 must_be_nonzero->high &= ~mask;
2008 else if (xor_mask.low != 0)
2010 unsigned HOST_WIDE_INT mask
2011 = ((unsigned HOST_WIDE_INT) 1
2012 << floor_log2 (xor_mask.low)) - 1;
2013 may_be_nonzero->low |= mask;
2014 must_be_nonzero->low &= ~mask;
2021 /* Create two value-ranges in *VR0 and *VR1 from the anti-range *AR
2022 so that *VR0 U *VR1 == *AR. Returns true if that is possible,
2023 false otherwise. If *AR can be represented with a single range
2024 *VR1 will be VR_UNDEFINED. */
2027 ranges_from_anti_range (value_range_t *ar,
2028 value_range_t *vr0, value_range_t *vr1)
2030 tree type = TREE_TYPE (ar->min);
2032 vr0->type = VR_UNDEFINED;
2033 vr1->type = VR_UNDEFINED;
2035 if (ar->type != VR_ANTI_RANGE
2036 || TREE_CODE (ar->min) != INTEGER_CST
2037 || TREE_CODE (ar->max) != INTEGER_CST
2038 || !vrp_val_min (type)
2039 || !vrp_val_max (type))
2042 if (!vrp_val_is_min (ar->min))
2044 vr0->type = VR_RANGE;
2045 vr0->min = vrp_val_min (type);
2047 = double_int_to_tree (type,
2048 tree_to_double_int (ar->min) - double_int_one);
2050 if (!vrp_val_is_max (ar->max))
2052 vr1->type = VR_RANGE;
2054 = double_int_to_tree (type,
2055 tree_to_double_int (ar->max) + double_int_one);
2056 vr1->max = vrp_val_max (type);
2058 if (vr0->type == VR_UNDEFINED)
2061 vr1->type = VR_UNDEFINED;
2064 return vr0->type != VR_UNDEFINED;
2067 /* Helper to extract a value-range *VR for a multiplicative operation
2071 extract_range_from_multiplicative_op_1 (value_range_t *vr,
2072 enum tree_code code,
2073 value_range_t *vr0, value_range_t *vr1)
2075 enum value_range_type type;
2082 /* Multiplications, divisions and shifts are a bit tricky to handle,
2083 depending on the mix of signs we have in the two ranges, we
2084 need to operate on different values to get the minimum and
2085 maximum values for the new range. One approach is to figure
2086 out all the variations of range combinations and do the
2089 However, this involves several calls to compare_values and it
2090 is pretty convoluted. It's simpler to do the 4 operations
2091 (MIN0 OP MIN1, MIN0 OP MAX1, MAX0 OP MIN1 and MAX0 OP MAX0 OP
2092 MAX1) and then figure the smallest and largest values to form
2094 gcc_assert (code == MULT_EXPR
2095 || code == TRUNC_DIV_EXPR
2096 || code == FLOOR_DIV_EXPR
2097 || code == CEIL_DIV_EXPR
2098 || code == EXACT_DIV_EXPR
2099 || code == ROUND_DIV_EXPR
2100 || code == RSHIFT_EXPR
2101 || code == LSHIFT_EXPR);
2102 gcc_assert ((vr0->type == VR_RANGE
2103 || (code == MULT_EXPR && vr0->type == VR_ANTI_RANGE))
2104 && vr0->type == vr1->type);
2108 /* Compute the 4 cross operations. */
2110 val[0] = vrp_int_const_binop (code, vr0->min, vr1->min);
2111 if (val[0] == NULL_TREE)
2114 if (vr1->max == vr1->min)
2118 val[1] = vrp_int_const_binop (code, vr0->min, vr1->max);
2119 if (val[1] == NULL_TREE)
2123 if (vr0->max == vr0->min)
2127 val[2] = vrp_int_const_binop (code, vr0->max, vr1->min);
2128 if (val[2] == NULL_TREE)
2132 if (vr0->min == vr0->max || vr1->min == vr1->max)
2136 val[3] = vrp_int_const_binop (code, vr0->max, vr1->max);
2137 if (val[3] == NULL_TREE)
2143 set_value_range_to_varying (vr);
2147 /* Set MIN to the minimum of VAL[i] and MAX to the maximum
2151 for (i = 1; i < 4; i++)
2153 if (!is_gimple_min_invariant (min)
2154 || (TREE_OVERFLOW (min) && !is_overflow_infinity (min))
2155 || !is_gimple_min_invariant (max)
2156 || (TREE_OVERFLOW (max) && !is_overflow_infinity (max)))
2161 if (!is_gimple_min_invariant (val[i])
2162 || (TREE_OVERFLOW (val[i])
2163 && !is_overflow_infinity (val[i])))
2165 /* If we found an overflowed value, set MIN and MAX
2166 to it so that we set the resulting range to
2172 if (compare_values (val[i], min) == -1)
2175 if (compare_values (val[i], max) == 1)
2180 /* If either MIN or MAX overflowed, then set the resulting range to
2181 VARYING. But we do accept an overflow infinity
2183 if (min == NULL_TREE
2184 || !is_gimple_min_invariant (min)
2185 || (TREE_OVERFLOW (min) && !is_overflow_infinity (min))
2187 || !is_gimple_min_invariant (max)
2188 || (TREE_OVERFLOW (max) && !is_overflow_infinity (max)))
2190 set_value_range_to_varying (vr);
2196 2) [-INF, +-INF(OVF)]
2197 3) [+-INF(OVF), +INF]
2198 4) [+-INF(OVF), +-INF(OVF)]
2199 We learn nothing when we have INF and INF(OVF) on both sides.
2200 Note that we do accept [-INF, -INF] and [+INF, +INF] without
2202 if ((vrp_val_is_min (min) || is_overflow_infinity (min))
2203 && (vrp_val_is_max (max) || is_overflow_infinity (max)))
2205 set_value_range_to_varying (vr);
2209 cmp = compare_values (min, max);
2210 if (cmp == -2 || cmp == 1)
2212 /* If the new range has its limits swapped around (MIN > MAX),
2213 then the operation caused one of them to wrap around, mark
2214 the new range VARYING. */
2215 set_value_range_to_varying (vr);
2218 set_value_range (vr, type, min, max, NULL);
2221 /* Some quadruple precision helpers. */
2223 quad_int_cmp (double_int l0, double_int h0,
2224 double_int l1, double_int h1, bool uns)
2226 int c = h0.cmp (h1, uns);
2227 if (c != 0) return c;
2228 return l0.ucmp (l1);
2232 quad_int_pair_sort (double_int *l0, double_int *h0,
2233 double_int *l1, double_int *h1, bool uns)
2235 if (quad_int_cmp (*l0, *h0, *l1, *h1, uns) > 0)
2238 tmp = *l0; *l0 = *l1; *l1 = tmp;
2239 tmp = *h0; *h0 = *h1; *h1 = tmp;
2243 /* Extract range information from a binary operation CODE based on
2244 the ranges of each of its operands, *VR0 and *VR1 with resulting
2245 type EXPR_TYPE. The resulting range is stored in *VR. */
2248 extract_range_from_binary_expr_1 (value_range_t *vr,
2249 enum tree_code code, tree expr_type,
2250 value_range_t *vr0_, value_range_t *vr1_)
2252 value_range_t vr0 = *vr0_, vr1 = *vr1_;
2253 value_range_t vrtem0 = VR_INITIALIZER, vrtem1 = VR_INITIALIZER;
2254 enum value_range_type type;
2255 tree min = NULL_TREE, max = NULL_TREE;
2258 if (!INTEGRAL_TYPE_P (expr_type)
2259 && !POINTER_TYPE_P (expr_type))
2261 set_value_range_to_varying (vr);
2265 /* Not all binary expressions can be applied to ranges in a
2266 meaningful way. Handle only arithmetic operations. */
2267 if (code != PLUS_EXPR
2268 && code != MINUS_EXPR
2269 && code != POINTER_PLUS_EXPR
2270 && code != MULT_EXPR
2271 && code != TRUNC_DIV_EXPR
2272 && code != FLOOR_DIV_EXPR
2273 && code != CEIL_DIV_EXPR
2274 && code != EXACT_DIV_EXPR
2275 && code != ROUND_DIV_EXPR
2276 && code != TRUNC_MOD_EXPR
2277 && code != RSHIFT_EXPR
2278 && code != LSHIFT_EXPR
2281 && code != BIT_AND_EXPR
2282 && code != BIT_IOR_EXPR
2283 && code != BIT_XOR_EXPR)
2285 set_value_range_to_varying (vr);
2289 /* If both ranges are UNDEFINED, so is the result. */
2290 if (vr0.type == VR_UNDEFINED && vr1.type == VR_UNDEFINED)
2292 set_value_range_to_undefined (vr);
2295 /* If one of the ranges is UNDEFINED drop it to VARYING for the following
2296 code. At some point we may want to special-case operations that
2297 have UNDEFINED result for all or some value-ranges of the not UNDEFINED
2299 else if (vr0.type == VR_UNDEFINED)
2300 set_value_range_to_varying (&vr0);
2301 else if (vr1.type == VR_UNDEFINED)
2302 set_value_range_to_varying (&vr1);
2304 /* Now canonicalize anti-ranges to ranges when they are not symbolic
2305 and express ~[] op X as ([]' op X) U ([]'' op X). */
2306 if (vr0.type == VR_ANTI_RANGE
2307 && ranges_from_anti_range (&vr0, &vrtem0, &vrtem1))
2309 extract_range_from_binary_expr_1 (vr, code, expr_type, &vrtem0, vr1_);
2310 if (vrtem1.type != VR_UNDEFINED)
2312 value_range_t vrres = VR_INITIALIZER;
2313 extract_range_from_binary_expr_1 (&vrres, code, expr_type,
2315 vrp_meet (vr, &vrres);
2319 /* Likewise for X op ~[]. */
2320 if (vr1.type == VR_ANTI_RANGE
2321 && ranges_from_anti_range (&vr1, &vrtem0, &vrtem1))
2323 extract_range_from_binary_expr_1 (vr, code, expr_type, vr0_, &vrtem0);
2324 if (vrtem1.type != VR_UNDEFINED)
2326 value_range_t vrres = VR_INITIALIZER;
2327 extract_range_from_binary_expr_1 (&vrres, code, expr_type,
2329 vrp_meet (vr, &vrres);
2334 /* The type of the resulting value range defaults to VR0.TYPE. */
2337 /* Refuse to operate on VARYING ranges, ranges of different kinds
2338 and symbolic ranges. As an exception, we allow BIT_AND_EXPR
2339 because we may be able to derive a useful range even if one of
2340 the operands is VR_VARYING or symbolic range. Similarly for
2341 divisions. TODO, we may be able to derive anti-ranges in
2343 if (code != BIT_AND_EXPR
2344 && code != BIT_IOR_EXPR
2345 && code != TRUNC_DIV_EXPR
2346 && code != FLOOR_DIV_EXPR
2347 && code != CEIL_DIV_EXPR
2348 && code != EXACT_DIV_EXPR
2349 && code != ROUND_DIV_EXPR
2350 && code != TRUNC_MOD_EXPR
2353 && (vr0.type == VR_VARYING
2354 || vr1.type == VR_VARYING
2355 || vr0.type != vr1.type
2356 || symbolic_range_p (&vr0)
2357 || symbolic_range_p (&vr1)))
2359 set_value_range_to_varying (vr);
2363 /* Now evaluate the expression to determine the new range. */
2364 if (POINTER_TYPE_P (expr_type))
2366 if (code == MIN_EXPR || code == MAX_EXPR)
2368 /* For MIN/MAX expressions with pointers, we only care about
2369 nullness, if both are non null, then the result is nonnull.
2370 If both are null, then the result is null. Otherwise they
2372 if (range_is_nonnull (&vr0) && range_is_nonnull (&vr1))
2373 set_value_range_to_nonnull (vr, expr_type);
2374 else if (range_is_null (&vr0) && range_is_null (&vr1))
2375 set_value_range_to_null (vr, expr_type);
2377 set_value_range_to_varying (vr);
2379 else if (code == POINTER_PLUS_EXPR)
2381 /* For pointer types, we are really only interested in asserting
2382 whether the expression evaluates to non-NULL. */
2383 if (range_is_nonnull (&vr0) || range_is_nonnull (&vr1))
2384 set_value_range_to_nonnull (vr, expr_type);
2385 else if (range_is_null (&vr0) && range_is_null (&vr1))
2386 set_value_range_to_null (vr, expr_type);
2388 set_value_range_to_varying (vr);
2390 else if (code == BIT_AND_EXPR)
2392 /* For pointer types, we are really only interested in asserting
2393 whether the expression evaluates to non-NULL. */
2394 if (range_is_nonnull (&vr0) && range_is_nonnull (&vr1))
2395 set_value_range_to_nonnull (vr, expr_type);
2396 else if (range_is_null (&vr0) || range_is_null (&vr1))
2397 set_value_range_to_null (vr, expr_type);
2399 set_value_range_to_varying (vr);
2402 set_value_range_to_varying (vr);
2407 /* For integer ranges, apply the operation to each end of the
2408 range and see what we end up with. */
2409 if (code == PLUS_EXPR || code == MINUS_EXPR)
2411 /* If we have a PLUS_EXPR with two VR_RANGE integer constant
2412 ranges compute the precise range for such case if possible. */
2413 if (range_int_cst_p (&vr0)
2414 && range_int_cst_p (&vr1)
2415 /* We need as many bits as the possibly unsigned inputs. */
2416 && TYPE_PRECISION (expr_type) <= HOST_BITS_PER_DOUBLE_INT)
2418 double_int min0 = tree_to_double_int (vr0.min);
2419 double_int max0 = tree_to_double_int (vr0.max);
2420 double_int min1 = tree_to_double_int (vr1.min);
2421 double_int max1 = tree_to_double_int (vr1.max);
2422 bool uns = TYPE_UNSIGNED (expr_type);
2424 = double_int::min_value (TYPE_PRECISION (expr_type), uns);
2426 = double_int::max_value (TYPE_PRECISION (expr_type), uns);
2427 double_int dmin, dmax;
2431 if (code == PLUS_EXPR)
2436 /* Check for overflow in double_int. */
2437 if (min1.cmp (double_int_zero, uns) != dmin.cmp (min0, uns))
2438 min_ovf = min0.cmp (dmin, uns);
2439 if (max1.cmp (double_int_zero, uns) != dmax.cmp (max0, uns))
2440 max_ovf = max0.cmp (dmax, uns);
2442 else /* if (code == MINUS_EXPR) */
2447 if (double_int_zero.cmp (max1, uns) != dmin.cmp (min0, uns))
2448 min_ovf = min0.cmp (max1, uns);
2449 if (double_int_zero.cmp (min1, uns) != dmax.cmp (max0, uns))
2450 max_ovf = max0.cmp (min1, uns);
2453 /* For non-wrapping arithmetic look at possibly smaller
2454 value-ranges of the type. */
2455 if (!TYPE_OVERFLOW_WRAPS (expr_type))
2457 if (vrp_val_min (expr_type))
2458 type_min = tree_to_double_int (vrp_val_min (expr_type));
2459 if (vrp_val_max (expr_type))
2460 type_max = tree_to_double_int (vrp_val_max (expr_type));
2463 /* Check for type overflow. */
2466 if (dmin.cmp (type_min, uns) == -1)
2468 else if (dmin.cmp (type_max, uns) == 1)
2473 if (dmax.cmp (type_min, uns) == -1)
2475 else if (dmax.cmp (type_max, uns) == 1)
2479 if (TYPE_OVERFLOW_WRAPS (expr_type))
2481 /* If overflow wraps, truncate the values and adjust the
2482 range kind and bounds appropriately. */
2484 = dmin.ext (TYPE_PRECISION (expr_type), uns);
2486 = dmax.ext (TYPE_PRECISION (expr_type), uns);
2487 if (min_ovf == max_ovf)
2489 /* No overflow or both overflow or underflow. The
2490 range kind stays VR_RANGE. */
2491 min = double_int_to_tree (expr_type, tmin);
2492 max = double_int_to_tree (expr_type, tmax);
2494 else if (min_ovf == -1
2497 /* Underflow and overflow, drop to VR_VARYING. */
2498 set_value_range_to_varying (vr);
2503 /* Min underflow or max overflow. The range kind
2504 changes to VR_ANTI_RANGE. */
2505 bool covers = false;
2506 double_int tem = tmin;
2507 gcc_assert ((min_ovf == -1 && max_ovf == 0)
2508 || (max_ovf == 1 && min_ovf == 0));
2509 type = VR_ANTI_RANGE;
2510 tmin = tmax + double_int_one;
2511 if (tmin.cmp (tmax, uns) < 0)
2513 tmax = tem + double_int_minus_one;
2514 if (tmax.cmp (tem, uns) > 0)
2516 /* If the anti-range would cover nothing, drop to varying.
2517 Likewise if the anti-range bounds are outside of the
2519 if (covers || tmin.cmp (tmax, uns) > 0)
2521 set_value_range_to_varying (vr);
2524 min = double_int_to_tree (expr_type, tmin);
2525 max = double_int_to_tree (expr_type, tmax);
2530 /* If overflow does not wrap, saturate to the types min/max
2534 if (needs_overflow_infinity (expr_type)
2535 && supports_overflow_infinity (expr_type))
2536 min = negative_overflow_infinity (expr_type);
2538 min = double_int_to_tree (expr_type, type_min);
2540 else if (min_ovf == 1)
2542 if (needs_overflow_infinity (expr_type)
2543 && supports_overflow_infinity (expr_type))
2544 min = positive_overflow_infinity (expr_type);
2546 min = double_int_to_tree (expr_type, type_max);
2549 min = double_int_to_tree (expr_type, dmin);
2553 if (needs_overflow_infinity (expr_type)
2554 && supports_overflow_infinity (expr_type))
2555 max = negative_overflow_infinity (expr_type);
2557 max = double_int_to_tree (expr_type, type_min);
2559 else if (max_ovf == 1)
2561 if (needs_overflow_infinity (expr_type)
2562 && supports_overflow_infinity (expr_type))
2563 max = positive_overflow_infinity (expr_type);
2565 max = double_int_to_tree (expr_type, type_max);
2568 max = double_int_to_tree (expr_type, dmax);
2570 if (needs_overflow_infinity (expr_type)
2571 && supports_overflow_infinity (expr_type))
2573 if (is_negative_overflow_infinity (vr0.min)
2574 || (code == PLUS_EXPR
2575 ? is_negative_overflow_infinity (vr1.min)
2576 : is_positive_overflow_infinity (vr1.max)))
2577 min = negative_overflow_infinity (expr_type);
2578 if (is_positive_overflow_infinity (vr0.max)
2579 || (code == PLUS_EXPR
2580 ? is_positive_overflow_infinity (vr1.max)
2581 : is_negative_overflow_infinity (vr1.min)))
2582 max = positive_overflow_infinity (expr_type);
2587 /* For other cases, for example if we have a PLUS_EXPR with two
2588 VR_ANTI_RANGEs, drop to VR_VARYING. It would take more effort
2589 to compute a precise range for such a case.
2590 ??? General even mixed range kind operations can be expressed
2591 by for example transforming ~[3, 5] + [1, 2] to range-only
2592 operations and a union primitive:
2593 [-INF, 2] + [1, 2] U [5, +INF] + [1, 2]
2594 [-INF+1, 4] U [6, +INF(OVF)]
2595 though usually the union is not exactly representable with
2596 a single range or anti-range as the above is
2597 [-INF+1, +INF(OVF)] intersected with ~[5, 5]
2598 but one could use a scheme similar to equivalences for this. */
2599 set_value_range_to_varying (vr);
2603 else if (code == MIN_EXPR
2604 || code == MAX_EXPR)
2606 if (vr0.type == VR_RANGE
2607 && !symbolic_range_p (&vr0))
2610 if (vr1.type == VR_RANGE
2611 && !symbolic_range_p (&vr1))
2613 /* For operations that make the resulting range directly
2614 proportional to the original ranges, apply the operation to
2615 the same end of each range. */
2616 min = vrp_int_const_binop (code, vr0.min, vr1.min);
2617 max = vrp_int_const_binop (code, vr0.max, vr1.max);
2619 else if (code == MIN_EXPR)
2621 min = vrp_val_min (expr_type);
2624 else if (code == MAX_EXPR)
2627 max = vrp_val_max (expr_type);
2630 else if (vr1.type == VR_RANGE
2631 && !symbolic_range_p (&vr1))
2634 if (code == MIN_EXPR)
2636 min = vrp_val_min (expr_type);
2639 else if (code == MAX_EXPR)
2642 max = vrp_val_max (expr_type);
2647 set_value_range_to_varying (vr);
2651 else if (code == MULT_EXPR)
2653 /* Fancy code so that with unsigned, [-3,-1]*[-3,-1] does not
2655 if (range_int_cst_p (&vr0)
2656 && range_int_cst_p (&vr1)
2657 && TYPE_OVERFLOW_WRAPS (expr_type))
2659 double_int min0, max0, min1, max1, sizem1, size;
2660 double_int prod0l, prod0h, prod1l, prod1h,
2661 prod2l, prod2h, prod3l, prod3h;
2662 bool uns0, uns1, uns;
2664 sizem1 = double_int::max_value (TYPE_PRECISION (expr_type), true);
2665 size = sizem1 + double_int_one;
2667 min0 = tree_to_double_int (vr0.min);
2668 max0 = tree_to_double_int (vr0.max);
2669 min1 = tree_to_double_int (vr1.min);
2670 max1 = tree_to_double_int (vr1.max);
2672 uns0 = TYPE_UNSIGNED (expr_type);
2675 /* Canonicalize the intervals. */
2676 if (TYPE_UNSIGNED (expr_type))
2678 double_int min2 = size - min0;
2679 if (!min2.is_zero () && min2.cmp (max0, true) < 0)
2687 if (!min2.is_zero () && min2.cmp (max1, true) < 0)
2697 prod0l = min0.wide_mul_with_sign (min1, true, &prod0h, &overflow);
2698 if (!uns0 && min0.is_negative ())
2700 if (!uns1 && min1.is_negative ())
2703 prod1l = min0.wide_mul_with_sign (max1, true, &prod1h, &overflow);
2704 if (!uns0 && min0.is_negative ())
2706 if (!uns1 && max1.is_negative ())
2709 prod2l = max0.wide_mul_with_sign (min1, true, &prod2h, &overflow);
2710 if (!uns0 && max0.is_negative ())
2712 if (!uns1 && min1.is_negative ())
2715 prod3l = max0.wide_mul_with_sign (max1, true, &prod3h, &overflow);
2716 if (!uns0 && max0.is_negative ())
2718 if (!uns1 && max1.is_negative ())
2721 /* Sort the 4 products. */
2722 quad_int_pair_sort (&prod0l, &prod0h, &prod3l, &prod3h, uns);
2723 quad_int_pair_sort (&prod1l, &prod1h, &prod2l, &prod2h, uns);
2724 quad_int_pair_sort (&prod0l, &prod0h, &prod1l, &prod1h, uns);
2725 quad_int_pair_sort (&prod2l, &prod2h, &prod3l, &prod3h, uns);
2728 if (prod0l.is_zero ())
2730 prod1l = double_int_zero;
2738 prod2l = prod3l + prod1l;
2739 prod2h = prod3h + prod1h;
2740 if (prod2l.ult (prod3l))
2741 prod2h += double_int_one; /* carry */
2743 if (!prod2h.is_zero ()
2744 || prod2l.cmp (sizem1, true) >= 0)
2746 /* the range covers all values. */
2747 set_value_range_to_varying (vr);
2751 /* The following should handle the wrapping and selecting
2752 VR_ANTI_RANGE for us. */
2753 min = double_int_to_tree (expr_type, prod0l);
2754 max = double_int_to_tree (expr_type, prod3l);
2755 set_and_canonicalize_value_range (vr, VR_RANGE, min, max, NULL);
2759 /* If we have an unsigned MULT_EXPR with two VR_ANTI_RANGEs,
2760 drop to VR_VARYING. It would take more effort to compute a
2761 precise range for such a case. For example, if we have
2762 op0 == 65536 and op1 == 65536 with their ranges both being
2763 ~[0,0] on a 32-bit machine, we would have op0 * op1 == 0, so
2764 we cannot claim that the product is in ~[0,0]. Note that we
2765 are guaranteed to have vr0.type == vr1.type at this
2767 if (vr0.type == VR_ANTI_RANGE
2768 && !TYPE_OVERFLOW_UNDEFINED (expr_type))
2770 set_value_range_to_varying (vr);
2774 extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1);
2777 else if (code == RSHIFT_EXPR
2778 || code == LSHIFT_EXPR)
2780 /* If we have a RSHIFT_EXPR with any shift values outside [0..prec-1],
2781 then drop to VR_VARYING. Outside of this range we get undefined
2782 behavior from the shift operation. We cannot even trust
2783 SHIFT_COUNT_TRUNCATED at this stage, because that applies to rtl
2784 shifts, and the operation at the tree level may be widened. */
2785 if (range_int_cst_p (&vr1)
2786 && compare_tree_int (vr1.min, 0) >= 0
2787 && compare_tree_int (vr1.max, TYPE_PRECISION (expr_type)) == -1)
2789 if (code == RSHIFT_EXPR)
2791 extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1);
2794 /* We can map lshifts by constants to MULT_EXPR handling. */
2795 else if (code == LSHIFT_EXPR
2796 && range_int_cst_singleton_p (&vr1))
2798 bool saved_flag_wrapv;
2799 value_range_t vr1p = VR_INITIALIZER;
2800 vr1p.type = VR_RANGE;
2802 = double_int_to_tree (expr_type,
2804 .llshift (TREE_INT_CST_LOW (vr1.min),
2805 TYPE_PRECISION (expr_type)));
2806 vr1p.max = vr1p.min;
2807 /* We have to use a wrapping multiply though as signed overflow
2808 on lshifts is implementation defined in C89. */
2809 saved_flag_wrapv = flag_wrapv;
2811 extract_range_from_binary_expr_1 (vr, MULT_EXPR, expr_type,
2813 flag_wrapv = saved_flag_wrapv;
2816 else if (code == LSHIFT_EXPR
2817 && range_int_cst_p (&vr0))
2819 int prec = TYPE_PRECISION (expr_type);
2820 int overflow_pos = prec;
2822 double_int bound, complement, low_bound, high_bound;
2823 bool uns = TYPE_UNSIGNED (expr_type);
2824 bool in_bounds = false;
2829 bound_shift = overflow_pos - TREE_INT_CST_LOW (vr1.max);
2830 /* If bound_shift == HOST_BITS_PER_DOUBLE_INT, the llshift can
2831 overflow. However, for that to happen, vr1.max needs to be
2832 zero, which means vr1 is a singleton range of zero, which
2833 means it should be handled by the previous LSHIFT_EXPR
2835 bound = double_int_one.llshift (bound_shift, prec);
2836 complement = ~(bound - double_int_one);
2840 low_bound = bound.zext (prec);
2841 high_bound = complement.zext (prec);
2842 if (tree_to_double_int (vr0.max).ult (low_bound))
2844 /* [5, 6] << [1, 2] == [10, 24]. */
2845 /* We're shifting out only zeroes, the value increases
2849 else if (high_bound.ult (tree_to_double_int (vr0.min)))
2851 /* [0xffffff00, 0xffffffff] << [1, 2]
2852 == [0xfffffc00, 0xfffffffe]. */
2853 /* We're shifting out only ones, the value decreases
2860 /* [-1, 1] << [1, 2] == [-4, 4]. */
2861 low_bound = complement.sext (prec);
2863 if (tree_to_double_int (vr0.max).slt (high_bound)
2864 && low_bound.slt (tree_to_double_int (vr0.min)))
2866 /* For non-negative numbers, we're shifting out only
2867 zeroes, the value increases monotonically.
2868 For negative numbers, we're shifting out only ones, the
2869 value decreases monotomically. */
2876 extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1);
2881 set_value_range_to_varying (vr);
2884 else if (code == TRUNC_DIV_EXPR
2885 || code == FLOOR_DIV_EXPR
2886 || code == CEIL_DIV_EXPR
2887 || code == EXACT_DIV_EXPR
2888 || code == ROUND_DIV_EXPR)
2890 if (vr0.type != VR_RANGE || symbolic_range_p (&vr0))
2892 /* For division, if op1 has VR_RANGE but op0 does not, something
2893 can be deduced just from that range. Say [min, max] / [4, max]
2894 gives [min / 4, max / 4] range. */
2895 if (vr1.type == VR_RANGE
2896 && !symbolic_range_p (&vr1)
2897 && range_includes_zero_p (vr1.min, vr1.max) == 0)
2899 vr0.type = type = VR_RANGE;
2900 vr0.min = vrp_val_min (expr_type);
2901 vr0.max = vrp_val_max (expr_type);
2905 set_value_range_to_varying (vr);
2910 /* For divisions, if flag_non_call_exceptions is true, we must
2911 not eliminate a division by zero. */
2912 if (cfun->can_throw_non_call_exceptions
2913 && (vr1.type != VR_RANGE
2914 || range_includes_zero_p (vr1.min, vr1.max) != 0))
2916 set_value_range_to_varying (vr);
2920 /* For divisions, if op0 is VR_RANGE, we can deduce a range
2921 even if op1 is VR_VARYING, VR_ANTI_RANGE, symbolic or can
2923 if (vr0.type == VR_RANGE
2924 && (vr1.type != VR_RANGE
2925 || range_includes_zero_p (vr1.min, vr1.max) != 0))
2927 tree zero = build_int_cst (TREE_TYPE (vr0.min), 0);
2932 if (TYPE_UNSIGNED (expr_type)
2933 || value_range_nonnegative_p (&vr1))
2935 /* For unsigned division or when divisor is known
2936 to be non-negative, the range has to cover
2937 all numbers from 0 to max for positive max
2938 and all numbers from min to 0 for negative min. */
2939 cmp = compare_values (vr0.max, zero);
2942 else if (cmp == 0 || cmp == 1)
2946 cmp = compare_values (vr0.min, zero);
2949 else if (cmp == 0 || cmp == -1)
2956 /* Otherwise the range is -max .. max or min .. -min
2957 depending on which bound is bigger in absolute value,
2958 as the division can change the sign. */
2959 abs_extent_range (vr, vr0.min, vr0.max);
2962 if (type == VR_VARYING)
2964 set_value_range_to_varying (vr);
2970 extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1);
2974 else if (code == TRUNC_MOD_EXPR)
2976 if (vr1.type != VR_RANGE
2977 || range_includes_zero_p (vr1.min, vr1.max) != 0
2978 || vrp_val_is_min (vr1.min))
2980 set_value_range_to_varying (vr);
2984 /* Compute MAX <|vr1.min|, |vr1.max|> - 1. */
2985 max = fold_unary_to_constant (ABS_EXPR, expr_type, vr1.min);
2986 if (tree_int_cst_lt (max, vr1.max))
2988 max = int_const_binop (MINUS_EXPR, max, integer_one_node);
2989 /* If the dividend is non-negative the modulus will be
2990 non-negative as well. */
2991 if (TYPE_UNSIGNED (expr_type)
2992 || value_range_nonnegative_p (&vr0))
2993 min = build_int_cst (TREE_TYPE (max), 0);
2995 min = fold_unary_to_constant (NEGATE_EXPR, expr_type, max);
2997 else if (code == BIT_AND_EXPR || code == BIT_IOR_EXPR || code == BIT_XOR_EXPR)
2999 bool int_cst_range0, int_cst_range1;
3000 double_int may_be_nonzero0, may_be_nonzero1;
3001 double_int must_be_nonzero0, must_be_nonzero1;
3003 int_cst_range0 = zero_nonzero_bits_from_vr (&vr0, &may_be_nonzero0,
3005 int_cst_range1 = zero_nonzero_bits_from_vr (&vr1, &may_be_nonzero1,
3009 if (code == BIT_AND_EXPR)
3012 min = double_int_to_tree (expr_type,
3013 must_be_nonzero0 & must_be_nonzero1);
3014 dmax = may_be_nonzero0 & may_be_nonzero1;
3015 /* If both input ranges contain only negative values we can
3016 truncate the result range maximum to the minimum of the
3017 input range maxima. */
3018 if (int_cst_range0 && int_cst_range1
3019 && tree_int_cst_sgn (vr0.max) < 0
3020 && tree_int_cst_sgn (vr1.max) < 0)
3022 dmax = dmax.min (tree_to_double_int (vr0.max),
3023 TYPE_UNSIGNED (expr_type));
3024 dmax = dmax.min (tree_to_double_int (vr1.max),
3025 TYPE_UNSIGNED (expr_type));
3027 /* If either input range contains only non-negative values
3028 we can truncate the result range maximum to the respective
3029 maximum of the input range. */
3030 if (int_cst_range0 && tree_int_cst_sgn (vr0.min) >= 0)
3031 dmax = dmax.min (tree_to_double_int (vr0.max),
3032 TYPE_UNSIGNED (expr_type));
3033 if (int_cst_range1 && tree_int_cst_sgn (vr1.min) >= 0)
3034 dmax = dmax.min (tree_to_double_int (vr1.max),
3035 TYPE_UNSIGNED (expr_type));
3036 max = double_int_to_tree (expr_type, dmax);
3038 else if (code == BIT_IOR_EXPR)
3041 max = double_int_to_tree (expr_type,
3042 may_be_nonzero0 | may_be_nonzero1);
3043 dmin = must_be_nonzero0 | must_be_nonzero1;
3044 /* If the input ranges contain only positive values we can
3045 truncate the minimum of the result range to the maximum
3046 of the input range minima. */
3047 if (int_cst_range0 && int_cst_range1
3048 && tree_int_cst_sgn (vr0.min) >= 0
3049 && tree_int_cst_sgn (vr1.min) >= 0)
3051 dmin = dmin.max (tree_to_double_int (vr0.min),
3052 TYPE_UNSIGNED (expr_type));
3053 dmin = dmin.max (tree_to_double_int (vr1.min),
3054 TYPE_UNSIGNED (expr_type));
3056 /* If either input range contains only negative values
3057 we can truncate the minimum of the result range to the
3058 respective minimum range. */
3059 if (int_cst_range0 && tree_int_cst_sgn (vr0.max) < 0)
3060 dmin = dmin.max (tree_to_double_int (vr0.min),
3061 TYPE_UNSIGNED (expr_type));
3062 if (int_cst_range1 && tree_int_cst_sgn (vr1.max) < 0)
3063 dmin = dmin.max (tree_to_double_int (vr1.min),
3064 TYPE_UNSIGNED (expr_type));
3065 min = double_int_to_tree (expr_type, dmin);
3067 else if (code == BIT_XOR_EXPR)
3069 double_int result_zero_bits, result_one_bits;
3070 result_zero_bits = (must_be_nonzero0 & must_be_nonzero1)
3071 | ~(may_be_nonzero0 | may_be_nonzero1);
3072 result_one_bits = must_be_nonzero0.and_not (may_be_nonzero1)
3073 | must_be_nonzero1.and_not (may_be_nonzero0);
3074 max = double_int_to_tree (expr_type, ~result_zero_bits);
3075 min = double_int_to_tree (expr_type, result_one_bits);
3076 /* If the range has all positive or all negative values the
3077 result is better than VARYING. */
3078 if (tree_int_cst_sgn (min) < 0
3079 || tree_int_cst_sgn (max) >= 0)
3082 max = min = NULL_TREE;
3088 /* If either MIN or MAX overflowed, then set the resulting range to
3089 VARYING. But we do accept an overflow infinity
3091 if (min == NULL_TREE
3092 || !is_gimple_min_invariant (min)
3093 || (TREE_OVERFLOW (min) && !is_overflow_infinity (min))
3095 || !is_gimple_min_invariant (max)
3096 || (TREE_OVERFLOW (max) && !is_overflow_infinity (max)))
3098 set_value_range_to_varying (vr);
3104 2) [-INF, +-INF(OVF)]
3105 3) [+-INF(OVF), +INF]
3106 4) [+-INF(OVF), +-INF(OVF)]
3107 We learn nothing when we have INF and INF(OVF) on both sides.
3108 Note that we do accept [-INF, -INF] and [+INF, +INF] without
3110 if ((vrp_val_is_min (min) || is_overflow_infinity (min))
3111 && (vrp_val_is_max (max) || is_overflow_infinity (max)))
3113 set_value_range_to_varying (vr);
3117 cmp = compare_values (min, max);
3118 if (cmp == -2 || cmp == 1)
3120 /* If the new range has its limits swapped around (MIN > MAX),
3121 then the operation caused one of them to wrap around, mark
3122 the new range VARYING. */
3123 set_value_range_to_varying (vr);
3126 set_value_range (vr, type, min, max, NULL);
3129 /* Extract range information from a binary expression OP0 CODE OP1 based on
3130 the ranges of each of its operands with resulting type EXPR_TYPE.
3131 The resulting range is stored in *VR. */
3134 extract_range_from_binary_expr (value_range_t *vr,
3135 enum tree_code code,
3136 tree expr_type, tree op0, tree op1)
3138 value_range_t vr0 = VR_INITIALIZER;
3139 value_range_t vr1 = VR_INITIALIZER;
3141 /* Get value ranges for each operand. For constant operands, create
3142 a new value range with the operand to simplify processing. */
3143 if (TREE_CODE (op0) == SSA_NAME)
3144 vr0 = *(get_value_range (op0));
3145 else if (is_gimple_min_invariant (op0))
3146 set_value_range_to_value (&vr0, op0, NULL);
3148 set_value_range_to_varying (&vr0);
3150 if (TREE_CODE (op1) == SSA_NAME)
3151 vr1 = *(get_value_range (op1));
3152 else if (is_gimple_min_invariant (op1))
3153 set_value_range_to_value (&vr1, op1, NULL);
3155 set_value_range_to_varying (&vr1);
3157 extract_range_from_binary_expr_1 (vr, code, expr_type, &vr0, &vr1);
3160 /* Extract range information from a unary operation CODE based on
3161 the range of its operand *VR0 with type OP0_TYPE with resulting type TYPE.
3162 The The resulting range is stored in *VR. */
3165 extract_range_from_unary_expr_1 (value_range_t *vr,
3166 enum tree_code code, tree type,
3167 value_range_t *vr0_, tree op0_type)
3169 value_range_t vr0 = *vr0_, vrtem0 = VR_INITIALIZER, vrtem1 = VR_INITIALIZER;
3171 /* VRP only operates on integral and pointer types. */
3172 if (!(INTEGRAL_TYPE_P (op0_type)
3173 || POINTER_TYPE_P (op0_type))
3174 || !(INTEGRAL_TYPE_P (type)
3175 || POINTER_TYPE_P (type)))
3177 set_value_range_to_varying (vr);
3181 /* If VR0 is UNDEFINED, so is the result. */
3182 if (vr0.type == VR_UNDEFINED)
3184 set_value_range_to_undefined (vr);
3188 /* Handle operations that we express in terms of others. */
3189 if (code == PAREN_EXPR)
3191 /* PAREN_EXPR is a simple copy. */
3192 copy_value_range (vr, &vr0);
3195 else if (code == NEGATE_EXPR)
3197 /* -X is simply 0 - X, so re-use existing code that also handles
3198 anti-ranges fine. */
3199 value_range_t zero = VR_INITIALIZER;
3200 set_value_range_to_value (&zero, build_int_cst (type, 0), NULL);
3201 extract_range_from_binary_expr_1 (vr, MINUS_EXPR, type, &zero, &vr0);
3204 else if (code == BIT_NOT_EXPR)
3206 /* ~X is simply -1 - X, so re-use existing code that also handles
3207 anti-ranges fine. */
3208 value_range_t minusone = VR_INITIALIZER;
3209 set_value_range_to_value (&minusone, build_int_cst (type, -1), NULL);
3210 extract_range_from_binary_expr_1 (vr, MINUS_EXPR,
3211 type, &minusone, &vr0);
3215 /* Now canonicalize anti-ranges to ranges when they are not symbolic
3216 and express op ~[] as (op []') U (op []''). */
3217 if (vr0.type == VR_ANTI_RANGE
3218 && ranges_from_anti_range (&vr0, &vrtem0, &vrtem1))
3220 extract_range_from_unary_expr_1 (vr, code, type, &vrtem0, op0_type);
3221 if (vrtem1.type != VR_UNDEFINED)
3223 value_range_t vrres = VR_INITIALIZER;
3224 extract_range_from_unary_expr_1 (&vrres, code, type,
3226 vrp_meet (vr, &vrres);
3231 if (CONVERT_EXPR_CODE_P (code))
3233 tree inner_type = op0_type;
3234 tree outer_type = type;
3236 /* If the expression evaluates to a pointer, we are only interested in
3237 determining if it evaluates to NULL [0, 0] or non-NULL (~[0, 0]). */
3238 if (POINTER_TYPE_P (type))
3240 if (range_is_nonnull (&vr0))
3241 set_value_range_to_nonnull (vr, type);
3242 else if (range_is_null (&vr0))
3243 set_value_range_to_null (vr, type);
3245 set_value_range_to_varying (vr);
3249 /* If VR0 is varying and we increase the type precision, assume
3250 a full range for the following transformation. */
3251 if (vr0.type == VR_VARYING
3252 && INTEGRAL_TYPE_P (inner_type)
3253 && TYPE_PRECISION (inner_type) < TYPE_PRECISION (outer_type))
3255 vr0.type = VR_RANGE;
3256 vr0.min = TYPE_MIN_VALUE (inner_type);
3257 vr0.max = TYPE_MAX_VALUE (inner_type);
3260 /* If VR0 is a constant range or anti-range and the conversion is
3261 not truncating we can convert the min and max values and
3262 canonicalize the resulting range. Otherwise we can do the
3263 conversion if the size of the range is less than what the
3264 precision of the target type can represent and the range is
3265 not an anti-range. */
3266 if ((vr0.type == VR_RANGE
3267 || vr0.type == VR_ANTI_RANGE)
3268 && TREE_CODE (vr0.min) == INTEGER_CST
3269 && TREE_CODE (vr0.max) == INTEGER_CST
3270 && (!is_overflow_infinity (vr0.min)
3271 || (vr0.type == VR_RANGE
3272 && TYPE_PRECISION (outer_type) > TYPE_PRECISION (inner_type)
3273 && needs_overflow_infinity (outer_type)
3274 && supports_overflow_infinity (outer_type)))
3275 && (!is_overflow_infinity (vr0.max)
3276 || (vr0.type == VR_RANGE
3277 && TYPE_PRECISION (outer_type) > TYPE_PRECISION (inner_type)
3278 && needs_overflow_infinity (outer_type)
3279 && supports_overflow_infinity (outer_type)))
3280 && (TYPE_PRECISION (outer_type) >= TYPE_PRECISION (inner_type)
3281 || (vr0.type == VR_RANGE
3282 && integer_zerop (int_const_binop (RSHIFT_EXPR,
3283 int_const_binop (MINUS_EXPR, vr0.max, vr0.min),
3284 size_int (TYPE_PRECISION (outer_type)))))))
3286 tree new_min, new_max;
3287 if (is_overflow_infinity (vr0.min))
3288 new_min = negative_overflow_infinity (outer_type);
3290 new_min = force_fit_type_double (outer_type,
3291 tree_to_double_int (vr0.min),
3293 if (is_overflow_infinity (vr0.max))
3294 new_max = positive_overflow_infinity (outer_type);
3296 new_max = force_fit_type_double (outer_type,
3297 tree_to_double_int (vr0.max),
3299 set_and_canonicalize_value_range (vr, vr0.type,
3300 new_min, new_max, NULL);
3304 set_value_range_to_varying (vr);
3307 else if (code == ABS_EXPR)
3312 /* Pass through vr0 in the easy cases. */
3313 if (TYPE_UNSIGNED (type)
3314 || value_range_nonnegative_p (&vr0))
3316 copy_value_range (vr, &vr0);
3320 /* For the remaining varying or symbolic ranges we can't do anything
3322 if (vr0.type == VR_VARYING
3323 || symbolic_range_p (&vr0))
3325 set_value_range_to_varying (vr);
3329 /* -TYPE_MIN_VALUE = TYPE_MIN_VALUE with flag_wrapv so we can't get a
3331 if (!TYPE_OVERFLOW_UNDEFINED (type)
3332 && ((vr0.type == VR_RANGE
3333 && vrp_val_is_min (vr0.min))
3334 || (vr0.type == VR_ANTI_RANGE
3335 && !vrp_val_is_min (vr0.min))))
3337 set_value_range_to_varying (vr);
3341 /* ABS_EXPR may flip the range around, if the original range
3342 included negative values. */
3343 if (is_overflow_infinity (vr0.min))
3344 min = positive_overflow_infinity (type);
3345 else if (!vrp_val_is_min (vr0.min))
3346 min = fold_unary_to_constant (code, type, vr0.min);
3347 else if (!needs_overflow_infinity (type))
3348 min = TYPE_MAX_VALUE (type);
3349 else if (supports_overflow_infinity (type))
3350 min = positive_overflow_infinity (type);
3353 set_value_range_to_varying (vr);
3357 if (is_overflow_infinity (vr0.max))
3358 max = positive_overflow_infinity (type);
3359 else if (!vrp_val_is_min (vr0.max))
3360 max = fold_unary_to_constant (code, type, vr0.max);
3361 else if (!needs_overflow_infinity (type))
3362 max = TYPE_MAX_VALUE (type);
3363 else if (supports_overflow_infinity (type)
3364 /* We shouldn't generate [+INF, +INF] as set_value_range
3365 doesn't like this and ICEs. */
3366 && !is_positive_overflow_infinity (min))
3367 max = positive_overflow_infinity (type);
3370 set_value_range_to_varying (vr);
3374 cmp = compare_values (min, max);
3376 /* If a VR_ANTI_RANGEs contains zero, then we have
3377 ~[-INF, min(MIN, MAX)]. */
3378 if (vr0.type == VR_ANTI_RANGE)
3380 if (range_includes_zero_p (vr0.min, vr0.max) == 1)
3382 /* Take the lower of the two values. */
3386 /* Create ~[-INF, min (abs(MIN), abs(MAX))]
3387 or ~[-INF + 1, min (abs(MIN), abs(MAX))] when
3388 flag_wrapv is set and the original anti-range doesn't include
3389 TYPE_MIN_VALUE, remember -TYPE_MIN_VALUE = TYPE_MIN_VALUE. */
3390 if (TYPE_OVERFLOW_WRAPS (type))
3392 tree type_min_value = TYPE_MIN_VALUE (type);
3394 min = (vr0.min != type_min_value
3395 ? int_const_binop (PLUS_EXPR, type_min_value,
3401 if (overflow_infinity_range_p (&vr0))
3402 min = negative_overflow_infinity (type);
3404 min = TYPE_MIN_VALUE (type);
3409 /* All else has failed, so create the range [0, INF], even for
3410 flag_wrapv since TYPE_MIN_VALUE is in the original
3412 vr0.type = VR_RANGE;
3413 min = build_int_cst (type, 0);
3414 if (needs_overflow_infinity (type))
3416 if (supports_overflow_infinity (type))
3417 max = positive_overflow_infinity (type);
3420 set_value_range_to_varying (vr);
3425 max = TYPE_MAX_VALUE (type);
3429 /* If the range contains zero then we know that the minimum value in the
3430 range will be zero. */
3431 else if (range_includes_zero_p (vr0.min, vr0.max) == 1)
3435 min = build_int_cst (type, 0);
3439 /* If the range was reversed, swap MIN and MAX. */
3448 cmp = compare_values (min, max);
3449 if (cmp == -2 || cmp == 1)
3451 /* If the new range has its limits swapped around (MIN > MAX),
3452 then the operation caused one of them to wrap around, mark
3453 the new range VARYING. */
3454 set_value_range_to_varying (vr);
3457 set_value_range (vr, vr0.type, min, max, NULL);
3461 /* For unhandled operations fall back to varying. */
3462 set_value_range_to_varying (vr);
3467 /* Extract range information from a unary expression CODE OP0 based on
3468 the range of its operand with resulting type TYPE.
3469 The resulting range is stored in *VR. */
3472 extract_range_from_unary_expr (value_range_t *vr, enum tree_code code,
3473 tree type, tree op0)
3475 value_range_t vr0 = VR_INITIALIZER;
3477 /* Get value ranges for the operand. For constant operands, create
3478 a new value range with the operand to simplify processing. */
3479 if (TREE_CODE (op0) == SSA_NAME)
3480 vr0 = *(get_value_range (op0));
3481 else if (is_gimple_min_invariant (op0))
3482 set_value_range_to_value (&vr0, op0, NULL);
3484 set_value_range_to_varying (&vr0);
3486 extract_range_from_unary_expr_1 (vr, code, type, &vr0, TREE_TYPE (op0));
3490 /* Extract range information from a conditional expression STMT based on
3491 the ranges of each of its operands and the expression code. */
3494 extract_range_from_cond_expr (value_range_t *vr, gimple stmt)
3497 value_range_t vr0 = VR_INITIALIZER;
3498 value_range_t vr1 = VR_INITIALIZER;
3500 /* Get value ranges for each operand. For constant operands, create
3501 a new value range with the operand to simplify processing. */
3502 op0 = gimple_assign_rhs2 (stmt);
3503 if (TREE_CODE (op0) == SSA_NAME)
3504 vr0 = *(get_value_range (op0));
3505 else if (is_gimple_min_invariant (op0))
3506 set_value_range_to_value (&vr0, op0, NULL);
3508 set_value_range_to_varying (&vr0);
3510 op1 = gimple_assign_rhs3 (stmt);
3511 if (TREE_CODE (op1) == SSA_NAME)
3512 vr1 = *(get_value_range (op1));
3513 else if (is_gimple_min_invariant (op1))
3514 set_value_range_to_value (&vr1, op1, NULL);
3516 set_value_range_to_varying (&vr1);
3518 /* The resulting value range is the union of the operand ranges */
3519 copy_value_range (vr, &vr0);
3520 vrp_meet (vr, &vr1);
3524 /* Extract range information from a comparison expression EXPR based
3525 on the range of its operand and the expression code. */
3528 extract_range_from_comparison (value_range_t *vr, enum tree_code code,
3529 tree type, tree op0, tree op1)
3534 val = vrp_evaluate_conditional_warnv_with_ops (code, op0, op1, false, &sop,
3537 /* A disadvantage of using a special infinity as an overflow
3538 representation is that we lose the ability to record overflow
3539 when we don't have an infinity. So we have to ignore a result
3540 which relies on overflow. */
3542 if (val && !is_overflow_infinity (val) && !sop)
3544 /* Since this expression was found on the RHS of an assignment,
3545 its type may be different from _Bool. Convert VAL to EXPR's
3547 val = fold_convert (type, val);
3548 if (is_gimple_min_invariant (val))
3549 set_value_range_to_value (vr, val, vr->equiv);
3551 set_value_range (vr, VR_RANGE, val, val, vr->equiv);
3554 /* The result of a comparison is always true or false. */
3555 set_value_range_to_truthvalue (vr, type);
3558 /* Try to derive a nonnegative or nonzero range out of STMT relying
3559 primarily on generic routines in fold in conjunction with range data.
3560 Store the result in *VR */
3563 extract_range_basic (value_range_t *vr, gimple stmt)
3566 tree type = gimple_expr_type (stmt);
3568 /* If the call is __builtin_constant_p and the argument is a
3569 function parameter resolve it to false. This avoids bogus
3570 array bound warnings.
3571 ??? We could do this as early as inlining is finished. */
3572 if (gimple_call_builtin_p (stmt, BUILT_IN_CONSTANT_P))
3574 tree arg = gimple_call_arg (stmt, 0);
3575 if (TREE_CODE (arg) == SSA_NAME
3576 && SSA_NAME_IS_DEFAULT_DEF (arg)
3577 && TREE_CODE (SSA_NAME_VAR (arg)) == PARM_DECL)
3578 set_value_range_to_null (vr, type);
3580 else if (INTEGRAL_TYPE_P (type)
3581 && gimple_stmt_nonnegative_warnv_p (stmt, &sop))
3582 set_value_range_to_nonnegative (vr, type,
3583 sop || stmt_overflow_infinity (stmt));
3584 else if (vrp_stmt_computes_nonzero (stmt, &sop)
3586 set_value_range_to_nonnull (vr, type);
3588 set_value_range_to_varying (vr);
3592 /* Try to compute a useful range out of assignment STMT and store it
3596 extract_range_from_assignment (value_range_t *vr, gimple stmt)
3598 enum tree_code code = gimple_assign_rhs_code (stmt);
3600 if (code == ASSERT_EXPR)
3601 extract_range_from_assert (vr, gimple_assign_rhs1 (stmt));
3602 else if (code == SSA_NAME)
3603 extract_range_from_ssa_name (vr, gimple_assign_rhs1 (stmt));
3604 else if (TREE_CODE_CLASS (code) == tcc_binary)
3605 extract_range_from_binary_expr (vr, gimple_assign_rhs_code (stmt),
3606 gimple_expr_type (stmt),
3607 gimple_assign_rhs1 (stmt),
3608 gimple_assign_rhs2 (stmt));
3609 else if (TREE_CODE_CLASS (code) == tcc_unary)
3610 extract_range_from_unary_expr (vr, gimple_assign_rhs_code (stmt),
3611 gimple_expr_type (stmt),
3612 gimple_assign_rhs1 (stmt));
3613 else if (code == COND_EXPR)
3614 extract_range_from_cond_expr (vr, stmt);
3615 else if (TREE_CODE_CLASS (code) == tcc_comparison)
3616 extract_range_from_comparison (vr, gimple_assign_rhs_code (stmt),
3617 gimple_expr_type (stmt),
3618 gimple_assign_rhs1 (stmt),
3619 gimple_assign_rhs2 (stmt));
3620 else if (get_gimple_rhs_class (code) == GIMPLE_SINGLE_RHS
3621 && is_gimple_min_invariant (gimple_assign_rhs1 (stmt)))
3622 set_value_range_to_value (vr, gimple_assign_rhs1 (stmt), NULL);
3624 set_value_range_to_varying (vr);
3626 if (vr->type == VR_VARYING)
3627 extract_range_basic (vr, stmt);
3630 /* Given a range VR, a LOOP and a variable VAR, determine whether it
3631 would be profitable to adjust VR using scalar evolution information
3632 for VAR. If so, update VR with the new limits. */
3635 adjust_range_with_scev (value_range_t *vr, struct loop *loop,
3636 gimple stmt, tree var)
3638 tree init, step, chrec, tmin, tmax, min, max, type, tem;
3639 enum ev_direction dir;
3641 /* TODO. Don't adjust anti-ranges. An anti-range may provide
3642 better opportunities than a regular range, but I'm not sure. */
3643 if (vr->type == VR_ANTI_RANGE)
3646 chrec = instantiate_parameters (loop, analyze_scalar_evolution (loop, var));
3648 /* Like in PR19590, scev can return a constant function. */
3649 if (is_gimple_min_invariant (chrec))
3651 set_value_range_to_value (vr, chrec, vr->equiv);
3655 if (TREE_CODE (chrec) != POLYNOMIAL_CHREC)
3658 init = initial_condition_in_loop_num (chrec, loop->num);
3659 tem = op_with_constant_singleton_value_range (init);
3662 step = evolution_part_in_loop_num (chrec, loop->num);
3663 tem = op_with_constant_singleton_value_range (step);
3667 /* If STEP is symbolic, we can't know whether INIT will be the
3668 minimum or maximum value in the range. Also, unless INIT is
3669 a simple expression, compare_values and possibly other functions
3670 in tree-vrp won't be able to handle it. */
3671 if (step == NULL_TREE
3672 || !is_gimple_min_invariant (step)
3673 || !valid_value_p (init))
3676 dir = scev_direction (chrec);
3677 if (/* Do not adjust ranges if we do not know whether the iv increases
3678 or decreases, ... */
3679 dir == EV_DIR_UNKNOWN
3680 /* ... or if it may wrap. */
3681 || scev_probably_wraps_p (init, step, stmt, get_chrec_loop (chrec),
3685 /* We use TYPE_MIN_VALUE and TYPE_MAX_VALUE here instead of
3686 negative_overflow_infinity and positive_overflow_infinity,
3687 because we have concluded that the loop probably does not
3690 type = TREE_TYPE (var);
3691 if (POINTER_TYPE_P (type) || !TYPE_MIN_VALUE (type))
3692 tmin = lower_bound_in_type (type, type);
3694 tmin = TYPE_MIN_VALUE (type);
3695 if (POINTER_TYPE_P (type) || !TYPE_MAX_VALUE (type))
3696 tmax = upper_bound_in_type (type, type);
3698 tmax = TYPE_MAX_VALUE (type);
3700 /* Try to use estimated number of iterations for the loop to constrain the
3701 final value in the evolution. */
3702 if (TREE_CODE (step) == INTEGER_CST
3703 && is_gimple_val (init)
3704 && (TREE_CODE (init) != SSA_NAME
3705 || get_value_range (init)->type == VR_RANGE))
3709 /* We are only entering here for loop header PHI nodes, so using
3710 the number of latch executions is the correct thing to use. */
3711 if (max_loop_iterations (loop, &nit))
3713 value_range_t maxvr = VR_INITIALIZER;
3715 bool unsigned_p = TYPE_UNSIGNED (TREE_TYPE (step));
3716 bool overflow = false;
3718 dtmp = tree_to_double_int (step)
3719 .mul_with_sign (nit, unsigned_p, &overflow);
3720 /* If the multiplication overflowed we can't do a meaningful
3721 adjustment. Likewise if the result doesn't fit in the type
3722 of the induction variable. For a signed type we have to
3723 check whether the result has the expected signedness which
3724 is that of the step as number of iterations is unsigned. */
3726 && double_int_fits_to_tree_p (TREE_TYPE (init), dtmp)
3728 || ((dtmp.high ^ TREE_INT_CST_HIGH (step)) >= 0)))
3730 tem = double_int_to_tree (TREE_TYPE (init), dtmp);
3731 extract_range_from_binary_expr (&maxvr, PLUS_EXPR,
3732 TREE_TYPE (init), init, tem);
3733 /* Likewise if the addition did. */
3734 if (maxvr.type == VR_RANGE)
3743 if (vr->type == VR_VARYING || vr->type == VR_UNDEFINED)
3748 /* For VARYING or UNDEFINED ranges, just about anything we get
3749 from scalar evolutions should be better. */
3751 if (dir == EV_DIR_DECREASES)
3756 /* If we would create an invalid range, then just assume we
3757 know absolutely nothing. This may be over-conservative,
3758 but it's clearly safe, and should happen only in unreachable
3759 parts of code, or for invalid programs. */
3760 if (compare_values (min, max) == 1)
3763 set_value_range (vr, VR_RANGE, min, max, vr->equiv);
3765 else if (vr->type == VR_RANGE)
3770 if (dir == EV_DIR_DECREASES)
3772 /* INIT is the maximum value. If INIT is lower than VR->MAX
3773 but no smaller than VR->MIN, set VR->MAX to INIT. */
3774 if (compare_values (init, max) == -1)
3777 /* According to the loop information, the variable does not
3778 overflow. If we think it does, probably because of an
3779 overflow due to arithmetic on a different INF value,
3781 if (is_negative_overflow_infinity (min)
3782 || compare_values (min, tmin) == -1)
3788 /* If INIT is bigger than VR->MIN, set VR->MIN to INIT. */
3789 if (compare_values (init, min) == 1)
3792 if (is_positive_overflow_infinity (max)
3793 || compare_values (tmax, max) == -1)
3797 /* If we just created an invalid range with the minimum
3798 greater than the maximum, we fail conservatively.
3799 This should happen only in unreachable
3800 parts of code, or for invalid programs. */
3801 if (compare_values (min, max) == 1)
3804 set_value_range (vr, VR_RANGE, min, max, vr->equiv);
3808 /* Return true if VAR may overflow at STMT. This checks any available
3809 loop information to see if we can determine that VAR does not
3813 vrp_var_may_overflow (tree var, gimple stmt)
3816 tree chrec, init, step;
3818 if (current_loops == NULL)
3821 l = loop_containing_stmt (stmt);
3826 chrec = instantiate_parameters (l, analyze_scalar_evolution (l, var));
3827 if (TREE_CODE (chrec) != POLYNOMIAL_CHREC)
3830 init = initial_condition_in_loop_num (chrec, l->num);
3831 step = evolution_part_in_loop_num (chrec, l->num);
3833 if (step == NULL_TREE
3834 || !is_gimple_min_invariant (step)
3835 || !valid_value_p (init))
3838 /* If we get here, we know something useful about VAR based on the
3839 loop information. If it wraps, it may overflow. */
3841 if (scev_probably_wraps_p (init, step, stmt, get_chrec_loop (chrec),
3845 if (dump_file && (dump_flags & TDF_DETAILS) != 0)
3847 print_generic_expr (dump_file, var, 0);
3848 fprintf (dump_file, ": loop information indicates does not overflow\n");
3855 /* Given two numeric value ranges VR0, VR1 and a comparison code COMP:
3857 - Return BOOLEAN_TRUE_NODE if VR0 COMP VR1 always returns true for
3858 all the values in the ranges.
3860 - Return BOOLEAN_FALSE_NODE if the comparison always returns false.
3862 - Return NULL_TREE if it is not always possible to determine the
3863 value of the comparison.
3865 Also set *STRICT_OVERFLOW_P to indicate whether a range with an
3866 overflow infinity was used in the test. */
3870 compare_ranges (enum tree_code comp, value_range_t *vr0, value_range_t *vr1,
3871 bool *strict_overflow_p)
3873 /* VARYING or UNDEFINED ranges cannot be compared. */
3874 if (vr0->type == VR_VARYING
3875 || vr0->type == VR_UNDEFINED
3876 || vr1->type == VR_VARYING
3877 || vr1->type == VR_UNDEFINED)
3880 /* Anti-ranges need to be handled separately. */
3881 if (vr0->type == VR_ANTI_RANGE || vr1->type == VR_ANTI_RANGE)
3883 /* If both are anti-ranges, then we cannot compute any
3885 if (vr0->type == VR_ANTI_RANGE && vr1->type == VR_ANTI_RANGE)
3888 /* These comparisons are never statically computable. */
3895 /* Equality can be computed only between a range and an
3896 anti-range. ~[VAL1, VAL2] == [VAL1, VAL2] is always false. */
3897 if (vr0->type == VR_RANGE)
3899 /* To simplify processing, make VR0 the anti-range. */
3900 value_range_t *tmp = vr0;
3905 gcc_assert (comp == NE_EXPR || comp == EQ_EXPR);
3907 if (compare_values_warnv (vr0->min, vr1->min, strict_overflow_p) == 0
3908 && compare_values_warnv (vr0->max, vr1->max, strict_overflow_p) == 0)
3909 return (comp == NE_EXPR) ? boolean_true_node : boolean_false_node;
3914 if (!usable_range_p (vr0, strict_overflow_p)
3915 || !usable_range_p (vr1, strict_overflow_p))
3918 /* Simplify processing. If COMP is GT_EXPR or GE_EXPR, switch the
3919 operands around and change the comparison code. */
3920 if (comp == GT_EXPR || comp == GE_EXPR)
3923 comp = (comp == GT_EXPR) ? LT_EXPR : LE_EXPR;
3929 if (comp == EQ_EXPR)
3931 /* Equality may only be computed if both ranges represent
3932 exactly one value. */
3933 if (compare_values_warnv (vr0->min, vr0->max, strict_overflow_p) == 0
3934 && compare_values_warnv (vr1->min, vr1->max, strict_overflow_p) == 0)
3936 int cmp_min = compare_values_warnv (vr0->min, vr1->min,
3938 int cmp_max = compare_values_warnv (vr0->max, vr1->max,
3940 if (cmp_min == 0 && cmp_max == 0)
3941 return boolean_true_node;
3942 else if (cmp_min != -2 && cmp_max != -2)
3943 return boolean_false_node;
3945 /* If [V0_MIN, V1_MAX] < [V1_MIN, V1_MAX] then V0 != V1. */
3946 else if (compare_values_warnv (vr0->min, vr1->max,
3947 strict_overflow_p) == 1
3948 || compare_values_warnv (vr1->min, vr0->max,
3949 strict_overflow_p) == 1)
3950 return boolean_false_node;
3954 else if (comp == NE_EXPR)
3958 /* If VR0 is completely to the left or completely to the right
3959 of VR1, they are always different. Notice that we need to
3960 make sure that both comparisons yield similar results to
3961 avoid comparing values that cannot be compared at
3963 cmp1 = compare_values_warnv (vr0->max, vr1->min, strict_overflow_p);
3964 cmp2 = compare_values_warnv (vr0->min, vr1->max, strict_overflow_p);
3965 if ((cmp1 == -1 && cmp2 == -1) || (cmp1 == 1 && cmp2 == 1))
3966 return boolean_true_node;
3968 /* If VR0 and VR1 represent a single value and are identical,
3970 else if (compare_values_warnv (vr0->min, vr0->max,
3971 strict_overflow_p) == 0
3972 && compare_values_warnv (vr1->min, vr1->max,
3973 strict_overflow_p) == 0
3974 && compare_values_warnv (vr0->min, vr1->min,
3975 strict_overflow_p) == 0
3976 && compare_values_warnv (vr0->max, vr1->max,
3977 strict_overflow_p) == 0)
3978 return boolean_false_node;
3980 /* Otherwise, they may or may not be different. */
3984 else if (comp == LT_EXPR || comp == LE_EXPR)
3988 /* If VR0 is to the left of VR1, return true. */
3989 tst = compare_values_warnv (vr0->max, vr1->min, strict_overflow_p);
3990 if ((comp == LT_EXPR && tst == -1)
3991 || (comp == LE_EXPR && (tst == -1 || tst == 0)))
3993 if (overflow_infinity_range_p (vr0)
3994 || overflow_infinity_range_p (vr1))
3995 *strict_overflow_p = true;
3996 return boolean_true_node;
3999 /* If VR0 is to the right of VR1, return false. */
4000 tst = compare_values_warnv (vr0->min, vr1->max, strict_overflow_p);
4001 if ((comp == LT_EXPR && (tst == 0 || tst == 1))
4002 || (comp == LE_EXPR && tst == 1))
4004 if (overflow_infinity_range_p (vr0)
4005 || overflow_infinity_range_p (vr1))
4006 *strict_overflow_p = true;
4007 return boolean_false_node;
4010 /* Otherwise, we don't know. */
4018 /* Given a value range VR, a value VAL and a comparison code COMP, return
4019 BOOLEAN_TRUE_NODE if VR COMP VAL always returns true for all the
4020 values in VR. Return BOOLEAN_FALSE_NODE if the comparison
4021 always returns false. Return NULL_TREE if it is not always
4022 possible to determine the value of the comparison. Also set
4023 *STRICT_OVERFLOW_P to indicate whether a range with an overflow
4024 infinity was used in the test. */
4027 compare_range_with_value (enum tree_code comp, value_range_t *vr, tree val,
4028 bool *strict_overflow_p)
4030 if (vr->type == VR_VARYING || vr->type == VR_UNDEFINED)
4033 /* Anti-ranges need to be handled separately. */
4034 if (vr->type == VR_ANTI_RANGE)
4036 /* For anti-ranges, the only predicates that we can compute at
4037 compile time are equality and inequality. */
4044 /* ~[VAL_1, VAL_2] OP VAL is known if VAL_1 <= VAL <= VAL_2. */
4045 if (value_inside_range (val, vr->min, vr->max) == 1)
4046 return (comp == NE_EXPR) ? boolean_true_node : boolean_false_node;
4051 if (!usable_range_p (vr, strict_overflow_p))
4054 if (comp == EQ_EXPR)
4056 /* EQ_EXPR may only be computed if VR represents exactly
4058 if (compare_values_warnv (vr->min, vr->max, strict_overflow_p) == 0)
4060 int cmp = compare_values_warnv (vr->min, val, strict_overflow_p);
4062 return boolean_true_node;
4063 else if (cmp == -1 || cmp == 1 || cmp == 2)
4064 return boolean_false_node;
4066 else if (compare_values_warnv (val, vr->min, strict_overflow_p) == -1
4067 || compare_values_warnv (vr->max, val, strict_overflow_p) == -1)
4068 return boolean_false_node;
4072 else if (comp == NE_EXPR)
4074 /* If VAL is not inside VR, then they are always different. */
4075 if (compare_values_warnv (vr->max, val, strict_overflow_p) == -1
4076 || compare_values_warnv (vr->min, val, strict_overflow_p) == 1)
4077 return boolean_true_node;
4079 /* If VR represents exactly one value equal to VAL, then return
4081 if (compare_values_warnv (vr->min, vr->max, strict_overflow_p) == 0
4082 && compare_values_warnv (vr->min, val, strict_overflow_p) == 0)
4083 return boolean_false_node;
4085 /* Otherwise, they may or may not be different. */
4088 else if (comp == LT_EXPR || comp == LE_EXPR)
4092 /* If VR is to the left of VAL, return true. */
4093 tst = compare_values_warnv (vr->max, val, strict_overflow_p);
4094 if ((comp == LT_EXPR && tst == -1)
4095 || (comp == LE_EXPR && (tst == -1 || tst == 0)))
4097 if (overflow_infinity_range_p (vr))
4098 *strict_overflow_p = true;
4099 return boolean_true_node;
4102 /* If VR is to the right of VAL, return false. */
4103 tst = compare_values_warnv (vr->min, val, strict_overflow_p);
4104 if ((comp == LT_EXPR && (tst == 0 || tst == 1))
4105 || (comp == LE_EXPR && tst == 1))
4107 if (overflow_infinity_range_p (vr))
4108 *strict_overflow_p = true;
4109 return boolean_false_node;
4112 /* Otherwise, we don't know. */
4115 else if (comp == GT_EXPR || comp == GE_EXPR)
4119 /* If VR is to the right of VAL, return true. */
4120 tst = compare_values_warnv (vr->min, val, strict_overflow_p);
4121 if ((comp == GT_EXPR && tst == 1)
4122 || (comp == GE_EXPR && (tst == 0 || tst == 1)))
4124 if (overflow_infinity_range_p (vr))
4125 *strict_overflow_p = true;
4126 return boolean_true_node;
4129 /* If VR is to the left of VAL, return false. */
4130 tst = compare_values_warnv (vr->max, val, strict_overflow_p);
4131 if ((comp == GT_EXPR && (tst == -1 || tst == 0))
4132 || (comp == GE_EXPR && tst == -1))
4134 if (overflow_infinity_range_p (vr))
4135 *strict_overflow_p = true;
4136 return boolean_false_node;
4139 /* Otherwise, we don't know. */
4147 /* Debugging dumps. */
4149 void dump_value_range (FILE *, value_range_t *);
4150 void debug_value_range (value_range_t *);
4151 void dump_all_value_ranges (FILE *);
4152 void debug_all_value_ranges (void);
4153 void dump_vr_equiv (FILE *, bitmap);
4154 void debug_vr_equiv (bitmap);
4157 /* Dump value range VR to FILE. */
4160 dump_value_range (FILE *file, value_range_t *vr)
4163 fprintf (file, "[]");
4164 else if (vr->type == VR_UNDEFINED)
4165 fprintf (file, "UNDEFINED");
4166 else if (vr->type == VR_RANGE || vr->type == VR_ANTI_RANGE)
4168 tree type = TREE_TYPE (vr->min);
4170 fprintf (file, "%s[", (vr->type == VR_ANTI_RANGE) ? "~" : "");
4172 if (is_negative_overflow_infinity (vr->min))
4173 fprintf (file, "-INF(OVF)");
4174 else if (INTEGRAL_TYPE_P (type)
4175 && !TYPE_UNSIGNED (type)
4176 && vrp_val_is_min (vr->min))
4177 fprintf (file, "-INF");
4179 print_generic_expr (file, vr->min, 0);
4181 fprintf (file, ", ");
4183 if (is_positive_overflow_infinity (vr->max))
4184 fprintf (file, "+INF(OVF)");
4185 else if (INTEGRAL_TYPE_P (type)
4186 && vrp_val_is_max (vr->max))
4187 fprintf (file, "+INF");
4189 print_generic_expr (file, vr->max, 0);
4191 fprintf (file, "]");
4198 fprintf (file, " EQUIVALENCES: { ");
4200 EXECUTE_IF_SET_IN_BITMAP (vr->equiv, 0, i, bi)
4202 print_generic_expr (file, ssa_name (i), 0);
4203 fprintf (file, " ");
4207 fprintf (file, "} (%u elements)", c);
4210 else if (vr->type == VR_VARYING)
4211 fprintf (file, "VARYING");
4213 fprintf (file, "INVALID RANGE");
4217 /* Dump value range VR to stderr. */
4220 debug_value_range (value_range_t *vr)
4222 dump_value_range (stderr, vr);
4223 fprintf (stderr, "\n");
4227 /* Dump value ranges of all SSA_NAMEs to FILE. */
4230 dump_all_value_ranges (FILE *file)
4234 for (i = 0; i < num_vr_values; i++)
4238 print_generic_expr (file, ssa_name (i), 0);
4239 fprintf (file, ": ");
4240 dump_value_range (file, vr_value[i]);
4241 fprintf (file, "\n");
4245 fprintf (file, "\n");
4249 /* Dump all value ranges to stderr. */
4252 debug_all_value_ranges (void)
4254 dump_all_value_ranges (stderr);
4258 /* Given a COND_EXPR COND of the form 'V OP W', and an SSA name V,
4259 create a new SSA name N and return the assertion assignment
4260 'V = ASSERT_EXPR <V, V OP W>'. */
4263 build_assert_expr_for (tree cond, tree v)
4268 gcc_assert (TREE_CODE (v) == SSA_NAME
4269 && COMPARISON_CLASS_P (cond));
4271 a = build2 (ASSERT_EXPR, TREE_TYPE (v), v, cond);
4272 assertion = gimple_build_assign (NULL_TREE, a);
4274 /* The new ASSERT_EXPR, creates a new SSA name that replaces the
4275 operand of the ASSERT_EXPR. Create it so the new name and the old one
4276 are registered in the replacement table so that we can fix the SSA web
4277 after adding all the ASSERT_EXPRs. */
4278 create_new_def_for (v, assertion, NULL);
4284 /* Return false if EXPR is a predicate expression involving floating
4288 fp_predicate (gimple stmt)
4290 GIMPLE_CHECK (stmt, GIMPLE_COND);
4292 return FLOAT_TYPE_P (TREE_TYPE (gimple_cond_lhs (stmt)));
4296 /* If the range of values taken by OP can be inferred after STMT executes,
4297 return the comparison code (COMP_CODE_P) and value (VAL_P) that
4298 describes the inferred range. Return true if a range could be
4302 infer_value_range (gimple stmt, tree op, enum tree_code *comp_code_p, tree *val_p)
4305 *comp_code_p = ERROR_MARK;
4307 /* Do not attempt to infer anything in names that flow through
4309 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op))
4312 /* Similarly, don't infer anything from statements that may throw
4314 if (stmt_could_throw_p (stmt))
4317 /* If STMT is the last statement of a basic block with no
4318 successors, there is no point inferring anything about any of its
4319 operands. We would not be able to find a proper insertion point
4320 for the assertion, anyway. */
4321 if (stmt_ends_bb_p (stmt) && EDGE_COUNT (gimple_bb (stmt)->succs) == 0)
4324 /* We can only assume that a pointer dereference will yield
4325 non-NULL if -fdelete-null-pointer-checks is enabled. */
4326 if (flag_delete_null_pointer_checks
4327 && POINTER_TYPE_P (TREE_TYPE (op))
4328 && gimple_code (stmt) != GIMPLE_ASM)
4330 unsigned num_uses, num_loads, num_stores;
4332 count_uses_and_derefs (op, stmt, &num_uses, &num_loads, &num_stores);
4333 if (num_loads + num_stores > 0)
4335 *val_p = build_int_cst (TREE_TYPE (op), 0);
4336 *comp_code_p = NE_EXPR;
4345 void dump_asserts_for (FILE *, tree);
4346 void debug_asserts_for (tree);
4347 void dump_all_asserts (FILE *);
4348 void debug_all_asserts (void);
4350 /* Dump all the registered assertions for NAME to FILE. */
4353 dump_asserts_for (FILE *file, tree name)
4357 fprintf (file, "Assertions to be inserted for ");
4358 print_generic_expr (file, name, 0);
4359 fprintf (file, "\n");
4361 loc = asserts_for[SSA_NAME_VERSION (name)];
4364 fprintf (file, "\t");
4365 print_gimple_stmt (file, gsi_stmt (loc->si), 0, 0);
4366 fprintf (file, "\n\tBB #%d", loc->bb->index);
4369 fprintf (file, "\n\tEDGE %d->%d", loc->e->src->index,
4370 loc->e->dest->index);
4371 dump_edge_info (file, loc->e, dump_flags, 0);
4373 fprintf (file, "\n\tPREDICATE: ");
4374 print_generic_expr (file, name, 0);
4375 fprintf (file, " %s ", tree_code_name[(int)loc->comp_code]);
4376 print_generic_expr (file, loc->val, 0);
4377 fprintf (file, "\n\n");
4381 fprintf (file, "\n");
4385 /* Dump all the registered assertions for NAME to stderr. */
4388 debug_asserts_for (tree name)
4390 dump_asserts_for (stderr, name);
4394 /* Dump all the registered assertions for all the names to FILE. */
4397 dump_all_asserts (FILE *file)
4402 fprintf (file, "\nASSERT_EXPRs to be inserted\n\n");
4403 EXECUTE_IF_SET_IN_BITMAP (need_assert_for, 0, i, bi)
4404 dump_asserts_for (file, ssa_name (i));
4405 fprintf (file, "\n");
4409 /* Dump all the registered assertions for all the names to stderr. */
4412 debug_all_asserts (void)
4414 dump_all_asserts (stderr);
4418 /* If NAME doesn't have an ASSERT_EXPR registered for asserting
4419 'EXPR COMP_CODE VAL' at a location that dominates block BB or
4420 E->DEST, then register this location as a possible insertion point
4421 for ASSERT_EXPR <NAME, EXPR COMP_CODE VAL>.
4423 BB, E and SI provide the exact insertion point for the new
4424 ASSERT_EXPR. If BB is NULL, then the ASSERT_EXPR is to be inserted
4425 on edge E. Otherwise, if E is NULL, the ASSERT_EXPR is inserted on
4426 BB. If SI points to a COND_EXPR or a SWITCH_EXPR statement, then E
4427 must not be NULL. */
4430 register_new_assert_for (tree name, tree expr,
4431 enum tree_code comp_code,
4435 gimple_stmt_iterator si)
4437 assert_locus_t n, loc, last_loc;
4438 basic_block dest_bb;
4440 gcc_checking_assert (bb == NULL || e == NULL);
4443 gcc_checking_assert (gimple_code (gsi_stmt (si)) != GIMPLE_COND
4444 && gimple_code (gsi_stmt (si)) != GIMPLE_SWITCH);
4446 /* Never build an assert comparing against an integer constant with
4447 TREE_OVERFLOW set. This confuses our undefined overflow warning
4449 if (TREE_CODE (val) == INTEGER_CST
4450 && TREE_OVERFLOW (val))
4451 val = build_int_cst_wide (TREE_TYPE (val),
4452 TREE_INT_CST_LOW (val), TREE_INT_CST_HIGH (val));
4454 /* The new assertion A will be inserted at BB or E. We need to
4455 determine if the new location is dominated by a previously
4456 registered location for A. If we are doing an edge insertion,
4457 assume that A will be inserted at E->DEST. Note that this is not
4460 If E is a critical edge, it will be split. But even if E is
4461 split, the new block will dominate the same set of blocks that
4464 The reverse, however, is not true, blocks dominated by E->DEST
4465 will not be dominated by the new block created to split E. So,
4466 if the insertion location is on a critical edge, we will not use
4467 the new location to move another assertion previously registered
4468 at a block dominated by E->DEST. */
4469 dest_bb = (bb) ? bb : e->dest;
4471 /* If NAME already has an ASSERT_EXPR registered for COMP_CODE and
4472 VAL at a block dominating DEST_BB, then we don't need to insert a new
4473 one. Similarly, if the same assertion already exists at a block
4474 dominated by DEST_BB and the new location is not on a critical
4475 edge, then update the existing location for the assertion (i.e.,
4476 move the assertion up in the dominance tree).
4478 Note, this is implemented as a simple linked list because there
4479 should not be more than a handful of assertions registered per
4480 name. If this becomes a performance problem, a table hashed by
4481 COMP_CODE and VAL could be implemented. */
4482 loc = asserts_for[SSA_NAME_VERSION (name)];
4486 if (loc->comp_code == comp_code
4488 || operand_equal_p (loc->val, val, 0))
4489 && (loc->expr == expr
4490 || operand_equal_p (loc->expr, expr, 0)))
4492 /* If E is not a critical edge and DEST_BB
4493 dominates the existing location for the assertion, move
4494 the assertion up in the dominance tree by updating its
4495 location information. */
4496 if ((e == NULL || !EDGE_CRITICAL_P (e))
4497 && dominated_by_p (CDI_DOMINATORS, loc->bb, dest_bb))
4506 /* Update the last node of the list and move to the next one. */
4511 /* If we didn't find an assertion already registered for
4512 NAME COMP_CODE VAL, add a new one at the end of the list of
4513 assertions associated with NAME. */
4514 n = XNEW (struct assert_locus_d);
4518 n->comp_code = comp_code;
4526 asserts_for[SSA_NAME_VERSION (name)] = n;
4528 bitmap_set_bit (need_assert_for, SSA_NAME_VERSION (name));
4531 /* (COND_OP0 COND_CODE COND_OP1) is a predicate which uses NAME.
4532 Extract a suitable test code and value and store them into *CODE_P and
4533 *VAL_P so the predicate is normalized to NAME *CODE_P *VAL_P.
4535 If no extraction was possible, return FALSE, otherwise return TRUE.
4537 If INVERT is true, then we invert the result stored into *CODE_P. */
4540 extract_code_and_val_from_cond_with_ops (tree name, enum tree_code cond_code,
4541 tree cond_op0, tree cond_op1,
4542 bool invert, enum tree_code *code_p,
4545 enum tree_code comp_code;
4548 /* Otherwise, we have a comparison of the form NAME COMP VAL
4549 or VAL COMP NAME. */
4550 if (name == cond_op1)
4552 /* If the predicate is of the form VAL COMP NAME, flip
4553 COMP around because we need to register NAME as the
4554 first operand in the predicate. */
4555 comp_code = swap_tree_comparison (cond_code);
4560 /* The comparison is of the form NAME COMP VAL, so the
4561 comparison code remains unchanged. */
4562 comp_code = cond_code;
4566 /* Invert the comparison code as necessary. */
4568 comp_code = invert_tree_comparison (comp_code, 0);
4570 /* VRP does not handle float types. */
4571 if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (val)))
4574 /* Do not register always-false predicates.
4575 FIXME: this works around a limitation in fold() when dealing with
4576 enumerations. Given 'enum { N1, N2 } x;', fold will not
4577 fold 'if (x > N2)' to 'if (0)'. */
4578 if ((comp_code == GT_EXPR || comp_code == LT_EXPR)
4579 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
4581 tree min = TYPE_MIN_VALUE (TREE_TYPE (val));
4582 tree max = TYPE_MAX_VALUE (TREE_TYPE (val));
4584 if (comp_code == GT_EXPR
4586 || compare_values (val, max) == 0))
4589 if (comp_code == LT_EXPR
4591 || compare_values (val, min) == 0))
4594 *code_p = comp_code;
4599 /* Find out smallest RES where RES > VAL && (RES & MASK) == RES, if any
4600 (otherwise return VAL). VAL and MASK must be zero-extended for
4601 precision PREC. If SGNBIT is non-zero, first xor VAL with SGNBIT
4602 (to transform signed values into unsigned) and at the end xor
4606 masked_increment (double_int val, double_int mask, double_int sgnbit,
4609 double_int bit = double_int_one, res;
4613 for (i = 0; i < prec; i++, bit += bit)
4616 if ((res & bit).is_zero ())
4618 res = bit - double_int_one;
4619 res = (val + bit).and_not (res);
4622 return res ^ sgnbit;
4624 return val ^ sgnbit;
4627 /* Try to register an edge assertion for SSA name NAME on edge E for
4628 the condition COND contributing to the conditional jump pointed to by BSI.
4629 Invert the condition COND if INVERT is true.
4630 Return true if an assertion for NAME could be registered. */
4633 register_edge_assert_for_2 (tree name, edge e, gimple_stmt_iterator bsi,
4634 enum tree_code cond_code,
4635 tree cond_op0, tree cond_op1, bool invert)
4638 enum tree_code comp_code;
4639 bool retval = false;
4641 if (!extract_code_and_val_from_cond_with_ops (name, cond_code,
4644 invert, &comp_code, &val))
4647 /* Only register an ASSERT_EXPR if NAME was found in the sub-graph
4648 reachable from E. */
4649 if (live_on_edge (e, name)
4650 && !has_single_use (name))
4652 register_new_assert_for (name, name, comp_code, val, NULL, e, bsi);
4656 /* In the case of NAME <= CST and NAME being defined as
4657 NAME = (unsigned) NAME2 + CST2 we can assert NAME2 >= -CST2
4658 and NAME2 <= CST - CST2. We can do the same for NAME > CST.
4659 This catches range and anti-range tests. */
4660 if ((comp_code == LE_EXPR
4661 || comp_code == GT_EXPR)
4662 && TREE_CODE (val) == INTEGER_CST
4663 && TYPE_UNSIGNED (TREE_TYPE (val)))
4665 gimple def_stmt = SSA_NAME_DEF_STMT (name);
4666 tree cst2 = NULL_TREE, name2 = NULL_TREE, name3 = NULL_TREE;
4668 /* Extract CST2 from the (optional) addition. */
4669 if (is_gimple_assign (def_stmt)
4670 && gimple_assign_rhs_code (def_stmt) == PLUS_EXPR)
4672 name2 = gimple_assign_rhs1 (def_stmt);
4673 cst2 = gimple_assign_rhs2 (def_stmt);
4674 if (TREE_CODE (name2) == SSA_NAME
4675 && TREE_CODE (cst2) == INTEGER_CST)
4676 def_stmt = SSA_NAME_DEF_STMT (name2);
4679 /* Extract NAME2 from the (optional) sign-changing cast. */
4680 if (gimple_assign_cast_p (def_stmt))
4682 if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt))
4683 && ! TYPE_UNSIGNED (TREE_TYPE (gimple_assign_rhs1 (def_stmt)))
4684 && (TYPE_PRECISION (gimple_expr_type (def_stmt))
4685 == TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (def_stmt)))))
4686 name3 = gimple_assign_rhs1 (def_stmt);
4689 /* If name3 is used later, create an ASSERT_EXPR for it. */
4690 if (name3 != NULL_TREE
4691 && TREE_CODE (name3) == SSA_NAME
4692 && (cst2 == NULL_TREE
4693 || TREE_CODE (cst2) == INTEGER_CST)
4694 && INTEGRAL_TYPE_P (TREE_TYPE (name3))
4695 && live_on_edge (e, name3)
4696 && !has_single_use (name3))
4700 /* Build an expression for the range test. */
4701 tmp = build1 (NOP_EXPR, TREE_TYPE (name), name3);
4702 if (cst2 != NULL_TREE)
4703 tmp = build2 (PLUS_EXPR, TREE_TYPE (name), tmp, cst2);
4707 fprintf (dump_file, "Adding assert for ");
4708 print_generic_expr (dump_file, name3, 0);
4709 fprintf (dump_file, " from ");
4710 print_generic_expr (dump_file, tmp, 0);
4711 fprintf (dump_file, "\n");
4714 register_new_assert_for (name3, tmp, comp_code, val, NULL, e, bsi);
4719 /* If name2 is used later, create an ASSERT_EXPR for it. */
4720 if (name2 != NULL_TREE
4721 && TREE_CODE (name2) == SSA_NAME
4722 && TREE_CODE (cst2) == INTEGER_CST
4723 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
4724 && live_on_edge (e, name2)
4725 && !has_single_use (name2))
4729 /* Build an expression for the range test. */
4731 if (TREE_TYPE (name) != TREE_TYPE (name2))
4732 tmp = build1 (NOP_EXPR, TREE_TYPE (name), tmp);
4733 if (cst2 != NULL_TREE)
4734 tmp = build2 (PLUS_EXPR, TREE_TYPE (name), tmp, cst2);
4738 fprintf (dump_file, "Adding assert for ");
4739 print_generic_expr (dump_file, name2, 0);
4740 fprintf (dump_file, " from ");
4741 print_generic_expr (dump_file, tmp, 0);
4742 fprintf (dump_file, "\n");
4745 register_new_assert_for (name2, tmp, comp_code, val, NULL, e, bsi);
4751 /* In the case of post-in/decrement tests like if (i++) ... and uses
4752 of the in/decremented value on the edge the extra name we want to
4753 assert for is not on the def chain of the name compared. Instead
4754 it is in the set of use stmts. */
4755 if ((comp_code == NE_EXPR
4756 || comp_code == EQ_EXPR)
4757 && TREE_CODE (val) == INTEGER_CST)
4759 imm_use_iterator ui;
4761 FOR_EACH_IMM_USE_STMT (use_stmt, ui, name)
4763 /* Cut off to use-stmts that are in the predecessor. */
4764 if (gimple_bb (use_stmt) != e->src)
4767 if (!is_gimple_assign (use_stmt))
4770 enum tree_code code = gimple_assign_rhs_code (use_stmt);
4771 if (code != PLUS_EXPR
4772 && code != MINUS_EXPR)
4775 tree cst = gimple_assign_rhs2 (use_stmt);
4776 if (TREE_CODE (cst) != INTEGER_CST)
4779 tree name2 = gimple_assign_lhs (use_stmt);
4780 if (live_on_edge (e, name2))
4782 cst = int_const_binop (code, val, cst);
4783 register_new_assert_for (name2, name2, comp_code, cst,
4790 if (TREE_CODE_CLASS (comp_code) == tcc_comparison
4791 && TREE_CODE (val) == INTEGER_CST)
4793 gimple def_stmt = SSA_NAME_DEF_STMT (name);
4794 tree name2 = NULL_TREE, names[2], cst2 = NULL_TREE;
4795 tree val2 = NULL_TREE;
4796 double_int mask = double_int_zero;
4797 unsigned int prec = TYPE_PRECISION (TREE_TYPE (val));
4798 unsigned int nprec = prec;
4799 enum tree_code rhs_code = ERROR_MARK;
4801 if (is_gimple_assign (def_stmt))
4802 rhs_code = gimple_assign_rhs_code (def_stmt);
4804 /* Add asserts for NAME cmp CST and NAME being defined
4805 as NAME = (int) NAME2. */
4806 if (!TYPE_UNSIGNED (TREE_TYPE (val))
4807 && (comp_code == LE_EXPR || comp_code == LT_EXPR
4808 || comp_code == GT_EXPR || comp_code == GE_EXPR)
4809 && gimple_assign_cast_p (def_stmt))
4811 name2 = gimple_assign_rhs1 (def_stmt);
4812 if (CONVERT_EXPR_CODE_P (rhs_code)
4813 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
4814 && TYPE_UNSIGNED (TREE_TYPE (name2))
4815 && prec == TYPE_PRECISION (TREE_TYPE (name2))
4816 && (comp_code == LE_EXPR || comp_code == GT_EXPR
4817 || !tree_int_cst_equal (val,
4818 TYPE_MIN_VALUE (TREE_TYPE (val))))
4819 && live_on_edge (e, name2)
4820 && !has_single_use (name2))
4823 enum tree_code new_comp_code = comp_code;
4825 cst = fold_convert (TREE_TYPE (name2),
4826 TYPE_MIN_VALUE (TREE_TYPE (val)));
4827 /* Build an expression for the range test. */
4828 tmp = build2 (PLUS_EXPR, TREE_TYPE (name2), name2, cst);
4829 cst = fold_build2 (PLUS_EXPR, TREE_TYPE (name2), cst,
4830 fold_convert (TREE_TYPE (name2), val));
4831 if (comp_code == LT_EXPR || comp_code == GE_EXPR)
4833 new_comp_code = comp_code == LT_EXPR ? LE_EXPR : GT_EXPR;
4834 cst = fold_build2 (MINUS_EXPR, TREE_TYPE (name2), cst,
4835 build_int_cst (TREE_TYPE (name2), 1));
4840 fprintf (dump_file, "Adding assert for ");
4841 print_generic_expr (dump_file, name2, 0);
4842 fprintf (dump_file, " from ");
4843 print_generic_expr (dump_file, tmp, 0);
4844 fprintf (dump_file, "\n");
4847 register_new_assert_for (name2, tmp, new_comp_code, cst, NULL,
4854 /* Add asserts for NAME cmp CST and NAME being defined as
4855 NAME = NAME2 >> CST2.
4857 Extract CST2 from the right shift. */
4858 if (rhs_code == RSHIFT_EXPR)
4860 name2 = gimple_assign_rhs1 (def_stmt);
4861 cst2 = gimple_assign_rhs2 (def_stmt);
4862 if (TREE_CODE (name2) == SSA_NAME
4863 && host_integerp (cst2, 1)
4864 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
4865 && IN_RANGE (tree_low_cst (cst2, 1), 1, prec - 1)
4866 && prec <= HOST_BITS_PER_DOUBLE_INT
4867 && prec == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (val)))
4868 && live_on_edge (e, name2)
4869 && !has_single_use (name2))
4871 mask = double_int::mask (tree_low_cst (cst2, 1));
4872 val2 = fold_binary (LSHIFT_EXPR, TREE_TYPE (val), val, cst2);
4875 if (val2 != NULL_TREE
4876 && TREE_CODE (val2) == INTEGER_CST
4877 && simple_cst_equal (fold_build2 (RSHIFT_EXPR,
4881 enum tree_code new_comp_code = comp_code;
4885 if (comp_code == EQ_EXPR || comp_code == NE_EXPR)
4887 if (!TYPE_UNSIGNED (TREE_TYPE (val)))
4889 tree type = build_nonstandard_integer_type (prec, 1);
4890 tmp = build1 (NOP_EXPR, type, name2);
4891 val2 = fold_convert (type, val2);
4893 tmp = fold_build2 (MINUS_EXPR, TREE_TYPE (tmp), tmp, val2);
4894 new_val = double_int_to_tree (TREE_TYPE (tmp), mask);
4895 new_comp_code = comp_code == EQ_EXPR ? LE_EXPR : GT_EXPR;
4897 else if (comp_code == LT_EXPR || comp_code == GE_EXPR)
4900 = double_int::min_value (prec, TYPE_UNSIGNED (TREE_TYPE (val)));
4902 if (minval == tree_to_double_int (new_val))
4903 new_val = NULL_TREE;
4908 = double_int::max_value (prec, TYPE_UNSIGNED (TREE_TYPE (val)));
4909 mask |= tree_to_double_int (val2);
4911 new_val = NULL_TREE;
4913 new_val = double_int_to_tree (TREE_TYPE (val2), mask);
4920 fprintf (dump_file, "Adding assert for ");
4921 print_generic_expr (dump_file, name2, 0);
4922 fprintf (dump_file, " from ");
4923 print_generic_expr (dump_file, tmp, 0);
4924 fprintf (dump_file, "\n");
4927 register_new_assert_for (name2, tmp, new_comp_code, new_val,
4933 /* Add asserts for NAME cmp CST and NAME being defined as
4934 NAME = NAME2 & CST2.
4936 Extract CST2 from the and.
4939 NAME = (unsigned) NAME2;
4940 casts where NAME's type is unsigned and has smaller precision
4941 than NAME2's type as if it was NAME = NAME2 & MASK. */
4942 names[0] = NULL_TREE;
4943 names[1] = NULL_TREE;
4945 if (rhs_code == BIT_AND_EXPR
4946 || (CONVERT_EXPR_CODE_P (rhs_code)
4947 && TREE_CODE (TREE_TYPE (val)) == INTEGER_TYPE
4948 && TYPE_UNSIGNED (TREE_TYPE (val))
4949 && TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (def_stmt)))
4953 name2 = gimple_assign_rhs1 (def_stmt);
4954 if (rhs_code == BIT_AND_EXPR)
4955 cst2 = gimple_assign_rhs2 (def_stmt);
4958 cst2 = TYPE_MAX_VALUE (TREE_TYPE (val));
4959 nprec = TYPE_PRECISION (TREE_TYPE (name2));
4961 if (TREE_CODE (name2) == SSA_NAME
4962 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
4963 && TREE_CODE (cst2) == INTEGER_CST
4964 && !integer_zerop (cst2)
4965 && nprec <= HOST_BITS_PER_DOUBLE_INT
4967 || TYPE_UNSIGNED (TREE_TYPE (val))))
4969 gimple def_stmt2 = SSA_NAME_DEF_STMT (name2);
4970 if (gimple_assign_cast_p (def_stmt2))
4972 names[1] = gimple_assign_rhs1 (def_stmt2);
4973 if (!CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt2))
4974 || !INTEGRAL_TYPE_P (TREE_TYPE (names[1]))
4975 || (TYPE_PRECISION (TREE_TYPE (name2))
4976 != TYPE_PRECISION (TREE_TYPE (names[1])))
4977 || !live_on_edge (e, names[1])
4978 || has_single_use (names[1]))
4979 names[1] = NULL_TREE;
4981 if (live_on_edge (e, name2)
4982 && !has_single_use (name2))
4986 if (names[0] || names[1])
4988 double_int minv, maxv = double_int_zero, valv, cst2v;
4989 double_int tem, sgnbit;
4990 bool valid_p = false, valn = false, cst2n = false;
4991 enum tree_code ccode = comp_code;
4993 valv = tree_to_double_int (val).zext (nprec);
4994 cst2v = tree_to_double_int (cst2).zext (nprec);
4995 if (!TYPE_UNSIGNED (TREE_TYPE (val)))
4997 valn = valv.sext (nprec).is_negative ();
4998 cst2n = cst2v.sext (nprec).is_negative ();
5000 /* If CST2 doesn't have most significant bit set,
5001 but VAL is negative, we have comparison like
5002 if ((x & 0x123) > -4) (always true). Just give up. */
5006 sgnbit = double_int_one.llshift (nprec - 1, nprec).zext (nprec);
5008 sgnbit = double_int_zero;
5009 minv = valv & cst2v;
5013 /* Minimum unsigned value for equality is VAL & CST2
5014 (should be equal to VAL, otherwise we probably should
5015 have folded the comparison into false) and
5016 maximum unsigned value is VAL | ~CST2. */
5017 maxv = valv | ~cst2v;
5018 maxv = maxv.zext (nprec);
5022 tem = valv | ~cst2v;
5023 tem = tem.zext (nprec);
5024 /* If VAL is 0, handle (X & CST2) != 0 as (X & CST2) > 0U. */
5025 if (valv.is_zero ())
5028 sgnbit = double_int_zero;
5031 /* If (VAL | ~CST2) is all ones, handle it as
5032 (X & CST2) < VAL. */
5033 if (tem == double_int::mask (nprec))
5037 sgnbit = double_int_zero;
5041 && cst2v.sext (nprec).is_negative ())
5043 = double_int_one.llshift (nprec - 1, nprec).zext (nprec);
5044 if (!sgnbit.is_zero ())
5052 if (tem == double_int::mask (nprec - 1))
5058 sgnbit = double_int_zero;
5062 /* Minimum unsigned value for >= if (VAL & CST2) == VAL
5063 is VAL and maximum unsigned value is ~0. For signed
5064 comparison, if CST2 doesn't have most significant bit
5065 set, handle it similarly. If CST2 has MSB set,
5066 the minimum is the same, and maximum is ~0U/2. */
5069 /* If (VAL & CST2) != VAL, X & CST2 can't be equal to
5071 minv = masked_increment (valv, cst2v, sgnbit, nprec);
5075 maxv = double_int::mask (nprec - (cst2n ? 1 : 0));
5080 /* Find out smallest MINV where MINV > VAL
5081 && (MINV & CST2) == MINV, if any. If VAL is signed and
5082 CST2 has MSB set, compute it biased by 1 << (nprec - 1). */
5083 minv = masked_increment (valv, cst2v, sgnbit, nprec);
5086 maxv = double_int::mask (nprec - (cst2n ? 1 : 0));
5090 /* Minimum unsigned value for <= is 0 and maximum
5091 unsigned value is VAL | ~CST2 if (VAL & CST2) == VAL.
5092 Otherwise, find smallest VAL2 where VAL2 > VAL
5093 && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
5095 For signed comparison, if CST2 doesn't have most
5096 significant bit set, handle it similarly. If CST2 has
5097 MSB set, the maximum is the same and minimum is INT_MIN. */
5102 maxv = masked_increment (valv, cst2v, sgnbit, nprec);
5105 maxv -= double_int_one;
5108 maxv = maxv.zext (nprec);
5114 /* Minimum unsigned value for < is 0 and maximum
5115 unsigned value is (VAL-1) | ~CST2 if (VAL & CST2) == VAL.
5116 Otherwise, find smallest VAL2 where VAL2 > VAL
5117 && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
5119 For signed comparison, if CST2 doesn't have most
5120 significant bit set, handle it similarly. If CST2 has
5121 MSB set, the maximum is the same and minimum is INT_MIN. */
5130 maxv = masked_increment (valv, cst2v, sgnbit, nprec);
5134 maxv -= double_int_one;
5136 maxv = maxv.zext (nprec);
5144 && (maxv - minv).zext (nprec) != double_int::mask (nprec))
5146 tree tmp, new_val, type;
5149 for (i = 0; i < 2; i++)
5152 double_int maxv2 = maxv;
5154 type = TREE_TYPE (names[i]);
5155 if (!TYPE_UNSIGNED (type))
5157 type = build_nonstandard_integer_type (nprec, 1);
5158 tmp = build1 (NOP_EXPR, type, names[i]);
5160 if (!minv.is_zero ())
5162 tmp = build2 (PLUS_EXPR, type, tmp,
5163 double_int_to_tree (type, -minv));
5164 maxv2 = maxv - minv;
5166 new_val = double_int_to_tree (type, maxv2);
5170 fprintf (dump_file, "Adding assert for ");
5171 print_generic_expr (dump_file, names[i], 0);
5172 fprintf (dump_file, " from ");
5173 print_generic_expr (dump_file, tmp, 0);
5174 fprintf (dump_file, "\n");
5177 register_new_assert_for (names[i], tmp, LE_EXPR,
5178 new_val, NULL, e, bsi);
5188 /* OP is an operand of a truth value expression which is known to have
5189 a particular value. Register any asserts for OP and for any
5190 operands in OP's defining statement.
5192 If CODE is EQ_EXPR, then we want to register OP is zero (false),
5193 if CODE is NE_EXPR, then we want to register OP is nonzero (true). */
5196 register_edge_assert_for_1 (tree op, enum tree_code code,
5197 edge e, gimple_stmt_iterator bsi)
5199 bool retval = false;
5202 enum tree_code rhs_code;
5204 /* We only care about SSA_NAMEs. */
5205 if (TREE_CODE (op) != SSA_NAME)
5208 /* We know that OP will have a zero or nonzero value. If OP is used
5209 more than once go ahead and register an assert for OP.
5211 The FOUND_IN_SUBGRAPH support is not helpful in this situation as
5212 it will always be set for OP (because OP is used in a COND_EXPR in
5214 if (!has_single_use (op))
5216 val = build_int_cst (TREE_TYPE (op), 0);
5217 register_new_assert_for (op, op, code, val, NULL, e, bsi);
5221 /* Now look at how OP is set. If it's set from a comparison,
5222 a truth operation or some bit operations, then we may be able
5223 to register information about the operands of that assignment. */
5224 op_def = SSA_NAME_DEF_STMT (op);
5225 if (gimple_code (op_def) != GIMPLE_ASSIGN)
5228 rhs_code = gimple_assign_rhs_code (op_def);
5230 if (TREE_CODE_CLASS (rhs_code) == tcc_comparison)
5232 bool invert = (code == EQ_EXPR ? true : false);
5233 tree op0 = gimple_assign_rhs1 (op_def);
5234 tree op1 = gimple_assign_rhs2 (op_def);
5236 if (TREE_CODE (op0) == SSA_NAME)
5237 retval |= register_edge_assert_for_2 (op0, e, bsi, rhs_code, op0, op1,
5239 if (TREE_CODE (op1) == SSA_NAME)
5240 retval |= register_edge_assert_for_2 (op1, e, bsi, rhs_code, op0, op1,
5243 else if ((code == NE_EXPR
5244 && gimple_assign_rhs_code (op_def) == BIT_AND_EXPR)
5246 && gimple_assign_rhs_code (op_def) == BIT_IOR_EXPR))
5248 /* Recurse on each operand. */
5249 tree op0 = gimple_assign_rhs1 (op_def);
5250 tree op1 = gimple_assign_rhs2 (op_def);
5251 if (TREE_CODE (op0) == SSA_NAME
5252 && has_single_use (op0))
5253 retval |= register_edge_assert_for_1 (op0, code, e, bsi);
5254 if (TREE_CODE (op1) == SSA_NAME
5255 && has_single_use (op1))
5256 retval |= register_edge_assert_for_1 (op1, code, e, bsi);
5258 else if (gimple_assign_rhs_code (op_def) == BIT_NOT_EXPR
5259 && TYPE_PRECISION (TREE_TYPE (gimple_assign_lhs (op_def))) == 1)
5261 /* Recurse, flipping CODE. */
5262 code = invert_tree_comparison (code, false);
5263 retval |= register_edge_assert_for_1 (gimple_assign_rhs1 (op_def),
5266 else if (gimple_assign_rhs_code (op_def) == SSA_NAME)
5268 /* Recurse through the copy. */
5269 retval |= register_edge_assert_for_1 (gimple_assign_rhs1 (op_def),
5272 else if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (op_def)))
5274 /* Recurse through the type conversion. */
5275 retval |= register_edge_assert_for_1 (gimple_assign_rhs1 (op_def),
5282 /* Try to register an edge assertion for SSA name NAME on edge E for
5283 the condition COND contributing to the conditional jump pointed to by SI.
5284 Return true if an assertion for NAME could be registered. */
5287 register_edge_assert_for (tree name, edge e, gimple_stmt_iterator si,
5288 enum tree_code cond_code, tree cond_op0,
5292 enum tree_code comp_code;
5293 bool retval = false;
5294 bool is_else_edge = (e->flags & EDGE_FALSE_VALUE) != 0;
5296 /* Do not attempt to infer anything in names that flow through
5298 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name))
5301 if (!extract_code_and_val_from_cond_with_ops (name, cond_code,
5307 /* Register ASSERT_EXPRs for name. */
5308 retval |= register_edge_assert_for_2 (name, e, si, cond_code, cond_op0,
5309 cond_op1, is_else_edge);
5312 /* If COND is effectively an equality test of an SSA_NAME against
5313 the value zero or one, then we may be able to assert values
5314 for SSA_NAMEs which flow into COND. */
5316 /* In the case of NAME == 1 or NAME != 0, for BIT_AND_EXPR defining
5317 statement of NAME we can assert both operands of the BIT_AND_EXPR
5318 have nonzero value. */
5319 if (((comp_code == EQ_EXPR && integer_onep (val))
5320 || (comp_code == NE_EXPR && integer_zerop (val))))
5322 gimple def_stmt = SSA_NAME_DEF_STMT (name);
5324 if (is_gimple_assign (def_stmt)
5325 && gimple_assign_rhs_code (def_stmt) == BIT_AND_EXPR)
5327 tree op0 = gimple_assign_rhs1 (def_stmt);
5328 tree op1 = gimple_assign_rhs2 (def_stmt);
5329 retval |= register_edge_assert_for_1 (op0, NE_EXPR, e, si);
5330 retval |= register_edge_assert_for_1 (op1, NE_EXPR, e, si);
5334 /* In the case of NAME == 0 or NAME != 1, for BIT_IOR_EXPR defining
5335 statement of NAME we can assert both operands of the BIT_IOR_EXPR
5337 if (((comp_code == EQ_EXPR && integer_zerop (val))
5338 || (comp_code == NE_EXPR && integer_onep (val))))
5340 gimple def_stmt = SSA_NAME_DEF_STMT (name);
5342 /* For BIT_IOR_EXPR only if NAME == 0 both operands have
5343 necessarily zero value, or if type-precision is one. */
5344 if (is_gimple_assign (def_stmt)
5345 && (gimple_assign_rhs_code (def_stmt) == BIT_IOR_EXPR
5346 && (TYPE_PRECISION (TREE_TYPE (name)) == 1
5347 || comp_code == EQ_EXPR)))
5349 tree op0 = gimple_assign_rhs1 (def_stmt);
5350 tree op1 = gimple_assign_rhs2 (def_stmt);
5351 retval |= register_edge_assert_for_1 (op0, EQ_EXPR, e, si);
5352 retval |= register_edge_assert_for_1 (op1, EQ_EXPR, e, si);
5360 /* Determine whether the outgoing edges of BB should receive an
5361 ASSERT_EXPR for each of the operands of BB's LAST statement.
5362 The last statement of BB must be a COND_EXPR.
5364 If any of the sub-graphs rooted at BB have an interesting use of
5365 the predicate operands, an assert location node is added to the
5366 list of assertions for the corresponding operands. */
5369 find_conditional_asserts (basic_block bb, gimple last)
5372 gimple_stmt_iterator bsi;
5378 need_assert = false;
5379 bsi = gsi_for_stmt (last);
5381 /* Look for uses of the operands in each of the sub-graphs
5382 rooted at BB. We need to check each of the outgoing edges
5383 separately, so that we know what kind of ASSERT_EXPR to
5385 FOR_EACH_EDGE (e, ei, bb->succs)
5390 /* Register the necessary assertions for each operand in the
5391 conditional predicate. */
5392 FOR_EACH_SSA_TREE_OPERAND (op, last, iter, SSA_OP_USE)
5394 need_assert |= register_edge_assert_for (op, e, bsi,
5395 gimple_cond_code (last),
5396 gimple_cond_lhs (last),
5397 gimple_cond_rhs (last));
5410 /* Compare two case labels sorting first by the destination bb index
5411 and then by the case value. */
5414 compare_case_labels (const void *p1, const void *p2)
5416 const struct case_info *ci1 = (const struct case_info *) p1;
5417 const struct case_info *ci2 = (const struct case_info *) p2;
5418 int idx1 = ci1->bb->index;
5419 int idx2 = ci2->bb->index;
5423 else if (idx1 == idx2)
5425 /* Make sure the default label is first in a group. */
5426 if (!CASE_LOW (ci1->expr))
5428 else if (!CASE_LOW (ci2->expr))
5431 return tree_int_cst_compare (CASE_LOW (ci1->expr),
5432 CASE_LOW (ci2->expr));
5438 /* Determine whether the outgoing edges of BB should receive an
5439 ASSERT_EXPR for each of the operands of BB's LAST statement.
5440 The last statement of BB must be a SWITCH_EXPR.
5442 If any of the sub-graphs rooted at BB have an interesting use of
5443 the predicate operands, an assert location node is added to the
5444 list of assertions for the corresponding operands. */
5447 find_switch_asserts (basic_block bb, gimple last)
5450 gimple_stmt_iterator bsi;
5453 struct case_info *ci;
5454 size_t n = gimple_switch_num_labels (last);
5455 #if GCC_VERSION >= 4000
5458 /* Work around GCC 3.4 bug (PR 37086). */
5459 volatile unsigned int idx;
5462 need_assert = false;
5463 bsi = gsi_for_stmt (last);
5464 op = gimple_switch_index (last);
5465 if (TREE_CODE (op) != SSA_NAME)
5468 /* Build a vector of case labels sorted by destination label. */
5469 ci = XNEWVEC (struct case_info, n);
5470 for (idx = 0; idx < n; ++idx)
5472 ci[idx].expr = gimple_switch_label (last, idx);
5473 ci[idx].bb = label_to_block (CASE_LABEL (ci[idx].expr));
5475 qsort (ci, n, sizeof (struct case_info), compare_case_labels);
5477 for (idx = 0; idx < n; ++idx)
5480 tree cl = ci[idx].expr;
5481 basic_block cbb = ci[idx].bb;
5483 min = CASE_LOW (cl);
5484 max = CASE_HIGH (cl);
5486 /* If there are multiple case labels with the same destination
5487 we need to combine them to a single value range for the edge. */
5488 if (idx + 1 < n && cbb == ci[idx + 1].bb)
5490 /* Skip labels until the last of the group. */
5493 } while (idx < n && cbb == ci[idx].bb);
5496 /* Pick up the maximum of the case label range. */
5497 if (CASE_HIGH (ci[idx].expr))
5498 max = CASE_HIGH (ci[idx].expr);
5500 max = CASE_LOW (ci[idx].expr);
5503 /* Nothing to do if the range includes the default label until we
5504 can register anti-ranges. */
5505 if (min == NULL_TREE)
5508 /* Find the edge to register the assert expr on. */
5509 e = find_edge (bb, cbb);
5511 /* Register the necessary assertions for the operand in the
5513 need_assert |= register_edge_assert_for (op, e, bsi,
5514 max ? GE_EXPR : EQ_EXPR,
5516 fold_convert (TREE_TYPE (op),
5520 need_assert |= register_edge_assert_for (op, e, bsi, LE_EXPR,
5522 fold_convert (TREE_TYPE (op),
5532 /* Traverse all the statements in block BB looking for statements that
5533 may generate useful assertions for the SSA names in their operand.
5534 If a statement produces a useful assertion A for name N_i, then the
5535 list of assertions already generated for N_i is scanned to
5536 determine if A is actually needed.
5538 If N_i already had the assertion A at a location dominating the
5539 current location, then nothing needs to be done. Otherwise, the
5540 new location for A is recorded instead.
5542 1- For every statement S in BB, all the variables used by S are
5543 added to bitmap FOUND_IN_SUBGRAPH.
5545 2- If statement S uses an operand N in a way that exposes a known
5546 value range for N, then if N was not already generated by an
5547 ASSERT_EXPR, create a new assert location for N. For instance,
5548 if N is a pointer and the statement dereferences it, we can
5549 assume that N is not NULL.
5551 3- COND_EXPRs are a special case of #2. We can derive range
5552 information from the predicate but need to insert different
5553 ASSERT_EXPRs for each of the sub-graphs rooted at the
5554 conditional block. If the last statement of BB is a conditional
5555 expression of the form 'X op Y', then
5557 a) Remove X and Y from the set FOUND_IN_SUBGRAPH.
5559 b) If the conditional is the only entry point to the sub-graph
5560 corresponding to the THEN_CLAUSE, recurse into it. On
5561 return, if X and/or Y are marked in FOUND_IN_SUBGRAPH, then
5562 an ASSERT_EXPR is added for the corresponding variable.
5564 c) Repeat step (b) on the ELSE_CLAUSE.
5566 d) Mark X and Y in FOUND_IN_SUBGRAPH.
5575 In this case, an assertion on the THEN clause is useful to
5576 determine that 'a' is always 9 on that edge. However, an assertion
5577 on the ELSE clause would be unnecessary.
5579 4- If BB does not end in a conditional expression, then we recurse
5580 into BB's dominator children.
5582 At the end of the recursive traversal, every SSA name will have a
5583 list of locations where ASSERT_EXPRs should be added. When a new
5584 location for name N is found, it is registered by calling
5585 register_new_assert_for. That function keeps track of all the
5586 registered assertions to prevent adding unnecessary assertions.
5587 For instance, if a pointer P_4 is dereferenced more than once in a
5588 dominator tree, only the location dominating all the dereference of
5589 P_4 will receive an ASSERT_EXPR.
5591 If this function returns true, then it means that there are names
5592 for which we need to generate ASSERT_EXPRs. Those assertions are
5593 inserted by process_assert_insertions. */
5596 find_assert_locations_1 (basic_block bb, sbitmap live)
5598 gimple_stmt_iterator si;
5602 need_assert = false;
5603 last = last_stmt (bb);
5605 /* If BB's last statement is a conditional statement involving integer
5606 operands, determine if we need to add ASSERT_EXPRs. */
5608 && gimple_code (last) == GIMPLE_COND
5609 && !fp_predicate (last)
5610 && !ZERO_SSA_OPERANDS (last, SSA_OP_USE))
5611 need_assert |= find_conditional_asserts (bb, last);
5613 /* If BB's last statement is a switch statement involving integer
5614 operands, determine if we need to add ASSERT_EXPRs. */
5616 && gimple_code (last) == GIMPLE_SWITCH
5617 && !ZERO_SSA_OPERANDS (last, SSA_OP_USE))
5618 need_assert |= find_switch_asserts (bb, last);
5620 /* Traverse all the statements in BB marking used names and looking
5621 for statements that may infer assertions for their used operands. */
5622 for (si = gsi_last_bb (bb); !gsi_end_p (si); gsi_prev (&si))
5628 stmt = gsi_stmt (si);
5630 if (is_gimple_debug (stmt))
5633 /* See if we can derive an assertion for any of STMT's operands. */
5634 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_USE)
5637 enum tree_code comp_code;
5639 /* If op is not live beyond this stmt, do not bother to insert
5641 if (!bitmap_bit_p (live, SSA_NAME_VERSION (op)))
5644 /* If OP is used in such a way that we can infer a value
5645 range for it, and we don't find a previous assertion for
5646 it, create a new assertion location node for OP. */
5647 if (infer_value_range (stmt, op, &comp_code, &value))
5649 /* If we are able to infer a nonzero value range for OP,
5650 then walk backwards through the use-def chain to see if OP
5651 was set via a typecast.
5653 If so, then we can also infer a nonzero value range
5654 for the operand of the NOP_EXPR. */
5655 if (comp_code == NE_EXPR && integer_zerop (value))
5658 gimple def_stmt = SSA_NAME_DEF_STMT (t);
5660 while (is_gimple_assign (def_stmt)
5661 && gimple_assign_rhs_code (def_stmt) == NOP_EXPR
5663 (gimple_assign_rhs1 (def_stmt)) == SSA_NAME
5665 (TREE_TYPE (gimple_assign_rhs1 (def_stmt))))
5667 t = gimple_assign_rhs1 (def_stmt);
5668 def_stmt = SSA_NAME_DEF_STMT (t);
5670 /* Note we want to register the assert for the
5671 operand of the NOP_EXPR after SI, not after the
5673 if (! has_single_use (t))
5675 register_new_assert_for (t, t, comp_code, value,
5682 register_new_assert_for (op, op, comp_code, value, bb, NULL, si);
5688 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_USE)
5689 bitmap_set_bit (live, SSA_NAME_VERSION (op));
5690 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_DEF)
5691 bitmap_clear_bit (live, SSA_NAME_VERSION (op));
5694 /* Traverse all PHI nodes in BB, updating live. */
5695 for (si = gsi_start_phis (bb); !gsi_end_p(si); gsi_next (&si))
5697 use_operand_p arg_p;
5699 gimple phi = gsi_stmt (si);
5700 tree res = gimple_phi_result (phi);
5702 if (virtual_operand_p (res))
5705 FOR_EACH_PHI_ARG (arg_p, phi, i, SSA_OP_USE)
5707 tree arg = USE_FROM_PTR (arg_p);
5708 if (TREE_CODE (arg) == SSA_NAME)
5709 bitmap_set_bit (live, SSA_NAME_VERSION (arg));
5712 bitmap_clear_bit (live, SSA_NAME_VERSION (res));
5718 /* Do an RPO walk over the function computing SSA name liveness
5719 on-the-fly and deciding on assert expressions to insert.
5720 Returns true if there are assert expressions to be inserted. */
5723 find_assert_locations (void)
5725 int *rpo = XNEWVEC (int, last_basic_block);
5726 int *bb_rpo = XNEWVEC (int, last_basic_block);
5727 int *last_rpo = XCNEWVEC (int, last_basic_block);
5731 live = XCNEWVEC (sbitmap, last_basic_block);
5732 rpo_cnt = pre_and_rev_post_order_compute (NULL, rpo, false);
5733 for (i = 0; i < rpo_cnt; ++i)
5736 need_asserts = false;
5737 for (i = rpo_cnt - 1; i >= 0; --i)
5739 basic_block bb = BASIC_BLOCK (rpo[i]);
5745 live[rpo[i]] = sbitmap_alloc (num_ssa_names);
5746 bitmap_clear (live[rpo[i]]);
5749 /* Process BB and update the live information with uses in
5751 need_asserts |= find_assert_locations_1 (bb, live[rpo[i]]);
5753 /* Merge liveness into the predecessor blocks and free it. */
5754 if (!bitmap_empty_p (live[rpo[i]]))
5757 FOR_EACH_EDGE (e, ei, bb->preds)
5759 int pred = e->src->index;
5760 if ((e->flags & EDGE_DFS_BACK) || pred == ENTRY_BLOCK)
5765 live[pred] = sbitmap_alloc (num_ssa_names);
5766 bitmap_clear (live[pred]);
5768 bitmap_ior (live[pred], live[pred], live[rpo[i]]);
5770 if (bb_rpo[pred] < pred_rpo)
5771 pred_rpo = bb_rpo[pred];
5774 /* Record the RPO number of the last visited block that needs
5775 live information from this block. */
5776 last_rpo[rpo[i]] = pred_rpo;
5780 sbitmap_free (live[rpo[i]]);
5781 live[rpo[i]] = NULL;
5784 /* We can free all successors live bitmaps if all their
5785 predecessors have been visited already. */
5786 FOR_EACH_EDGE (e, ei, bb->succs)
5787 if (last_rpo[e->dest->index] == i
5788 && live[e->dest->index])
5790 sbitmap_free (live[e->dest->index]);
5791 live[e->dest->index] = NULL;
5796 XDELETEVEC (bb_rpo);
5797 XDELETEVEC (last_rpo);
5798 for (i = 0; i < last_basic_block; ++i)
5800 sbitmap_free (live[i]);
5803 return need_asserts;
5806 /* Create an ASSERT_EXPR for NAME and insert it in the location
5807 indicated by LOC. Return true if we made any edge insertions. */
5810 process_assert_insertions_for (tree name, assert_locus_t loc)
5812 /* Build the comparison expression NAME_i COMP_CODE VAL. */
5819 /* If we have X <=> X do not insert an assert expr for that. */
5820 if (loc->expr == loc->val)
5823 cond = build2 (loc->comp_code, boolean_type_node, loc->expr, loc->val);
5824 assert_stmt = build_assert_expr_for (cond, name);
5827 /* We have been asked to insert the assertion on an edge. This
5828 is used only by COND_EXPR and SWITCH_EXPR assertions. */
5829 gcc_checking_assert (gimple_code (gsi_stmt (loc->si)) == GIMPLE_COND
5830 || (gimple_code (gsi_stmt (loc->si))
5833 gsi_insert_on_edge (loc->e, assert_stmt);
5837 /* Otherwise, we can insert right after LOC->SI iff the
5838 statement must not be the last statement in the block. */
5839 stmt = gsi_stmt (loc->si);
5840 if (!stmt_ends_bb_p (stmt))
5842 gsi_insert_after (&loc->si, assert_stmt, GSI_SAME_STMT);
5846 /* If STMT must be the last statement in BB, we can only insert new
5847 assertions on the non-abnormal edge out of BB. Note that since
5848 STMT is not control flow, there may only be one non-abnormal edge
5850 FOR_EACH_EDGE (e, ei, loc->bb->succs)
5851 if (!(e->flags & EDGE_ABNORMAL))
5853 gsi_insert_on_edge (e, assert_stmt);
5861 /* Process all the insertions registered for every name N_i registered
5862 in NEED_ASSERT_FOR. The list of assertions to be inserted are
5863 found in ASSERTS_FOR[i]. */
5866 process_assert_insertions (void)
5870 bool update_edges_p = false;
5871 int num_asserts = 0;
5873 if (dump_file && (dump_flags & TDF_DETAILS))
5874 dump_all_asserts (dump_file);
5876 EXECUTE_IF_SET_IN_BITMAP (need_assert_for, 0, i, bi)
5878 assert_locus_t loc = asserts_for[i];
5883 assert_locus_t next = loc->next;
5884 update_edges_p |= process_assert_insertions_for (ssa_name (i), loc);
5892 gsi_commit_edge_inserts ();
5894 statistics_counter_event (cfun, "Number of ASSERT_EXPR expressions inserted",
5899 /* Traverse the flowgraph looking for conditional jumps to insert range
5900 expressions. These range expressions are meant to provide information
5901 to optimizations that need to reason in terms of value ranges. They
5902 will not be expanded into RTL. For instance, given:
5911 this pass will transform the code into:
5917 x = ASSERT_EXPR <x, x < y>
5922 y = ASSERT_EXPR <y, x <= y>
5926 The idea is that once copy and constant propagation have run, other
5927 optimizations will be able to determine what ranges of values can 'x'
5928 take in different paths of the code, simply by checking the reaching
5929 definition of 'x'. */
5932 insert_range_assertions (void)
5934 need_assert_for = BITMAP_ALLOC (NULL);
5935 asserts_for = XCNEWVEC (assert_locus_t, num_ssa_names);
5937 calculate_dominance_info (CDI_DOMINATORS);
5939 if (find_assert_locations ())
5941 process_assert_insertions ();
5942 update_ssa (TODO_update_ssa_no_phi);
5945 if (dump_file && (dump_flags & TDF_DETAILS))
5947 fprintf (dump_file, "\nSSA form after inserting ASSERT_EXPRs\n");
5948 dump_function_to_file (current_function_decl, dump_file, dump_flags);
5952 BITMAP_FREE (need_assert_for);
5955 /* Checks one ARRAY_REF in REF, located at LOCUS. Ignores flexible arrays
5956 and "struct" hacks. If VRP can determine that the
5957 array subscript is a constant, check if it is outside valid
5958 range. If the array subscript is a RANGE, warn if it is
5959 non-overlapping with valid range.
5960 IGNORE_OFF_BY_ONE is true if the ARRAY_REF is inside a ADDR_EXPR. */
5963 check_array_ref (location_t location, tree ref, bool ignore_off_by_one)
5965 value_range_t* vr = NULL;
5966 tree low_sub, up_sub;
5967 tree low_bound, up_bound, up_bound_p1;
5970 if (TREE_NO_WARNING (ref))
5973 low_sub = up_sub = TREE_OPERAND (ref, 1);
5974 up_bound = array_ref_up_bound (ref);
5976 /* Can not check flexible arrays. */
5978 || TREE_CODE (up_bound) != INTEGER_CST)
5981 /* Accesses to trailing arrays via pointers may access storage
5982 beyond the types array bounds. */
5983 base = get_base_address (ref);
5984 if (base && TREE_CODE (base) == MEM_REF)
5986 tree cref, next = NULL_TREE;
5988 if (TREE_CODE (TREE_OPERAND (ref, 0)) != COMPONENT_REF)
5991 cref = TREE_OPERAND (ref, 0);
5992 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (cref, 0))) == RECORD_TYPE)
5993 for (next = DECL_CHAIN (TREE_OPERAND (cref, 1));
5994 next && TREE_CODE (next) != FIELD_DECL;
5995 next = DECL_CHAIN (next))
5998 /* If this is the last field in a struct type or a field in a
5999 union type do not warn. */
6004 low_bound = array_ref_low_bound (ref);
6005 up_bound_p1 = int_const_binop (PLUS_EXPR, up_bound, integer_one_node);
6007 if (TREE_CODE (low_sub) == SSA_NAME)
6009 vr = get_value_range (low_sub);
6010 if (vr->type == VR_RANGE || vr->type == VR_ANTI_RANGE)
6012 low_sub = vr->type == VR_RANGE ? vr->max : vr->min;
6013 up_sub = vr->type == VR_RANGE ? vr->min : vr->max;
6017 if (vr && vr->type == VR_ANTI_RANGE)
6019 if (TREE_CODE (up_sub) == INTEGER_CST
6020 && tree_int_cst_lt (up_bound, up_sub)
6021 && TREE_CODE (low_sub) == INTEGER_CST
6022 && tree_int_cst_lt (low_sub, low_bound))
6024 warning_at (location, OPT_Warray_bounds,
6025 "array subscript is outside array bounds");
6026 TREE_NO_WARNING (ref) = 1;
6029 else if (TREE_CODE (up_sub) == INTEGER_CST
6030 && (ignore_off_by_one
6031 ? (tree_int_cst_lt (up_bound, up_sub)
6032 && !tree_int_cst_equal (up_bound_p1, up_sub))
6033 : (tree_int_cst_lt (up_bound, up_sub)
6034 || tree_int_cst_equal (up_bound_p1, up_sub))))
6036 if (dump_file && (dump_flags & TDF_DETAILS))
6038 fprintf (dump_file, "Array bound warning for ");
6039 dump_generic_expr (MSG_NOTE, TDF_SLIM, ref);
6040 fprintf (dump_file, "\n");
6042 warning_at (location, OPT_Warray_bounds,
6043 "array subscript is above array bounds");
6044 TREE_NO_WARNING (ref) = 1;
6046 else if (TREE_CODE (low_sub) == INTEGER_CST
6047 && tree_int_cst_lt (low_sub, low_bound))
6049 if (dump_file && (dump_flags & TDF_DETAILS))
6051 fprintf (dump_file, "Array bound warning for ");
6052 dump_generic_expr (MSG_NOTE, TDF_SLIM, ref);
6053 fprintf (dump_file, "\n");
6055 warning_at (location, OPT_Warray_bounds,
6056 "array subscript is below array bounds");
6057 TREE_NO_WARNING (ref) = 1;
6061 /* Searches if the expr T, located at LOCATION computes
6062 address of an ARRAY_REF, and call check_array_ref on it. */
6065 search_for_addr_array (tree t, location_t location)
6067 while (TREE_CODE (t) == SSA_NAME)
6069 gimple g = SSA_NAME_DEF_STMT (t);
6071 if (gimple_code (g) != GIMPLE_ASSIGN)
6074 if (get_gimple_rhs_class (gimple_assign_rhs_code (g))
6075 != GIMPLE_SINGLE_RHS)
6078 t = gimple_assign_rhs1 (g);
6082 /* We are only interested in addresses of ARRAY_REF's. */
6083 if (TREE_CODE (t) != ADDR_EXPR)
6086 /* Check each ARRAY_REFs in the reference chain. */
6089 if (TREE_CODE (t) == ARRAY_REF)
6090 check_array_ref (location, t, true /*ignore_off_by_one*/);
6092 t = TREE_OPERAND (t, 0);
6094 while (handled_component_p (t));
6096 if (TREE_CODE (t) == MEM_REF
6097 && TREE_CODE (TREE_OPERAND (t, 0)) == ADDR_EXPR
6098 && !TREE_NO_WARNING (t))
6100 tree tem = TREE_OPERAND (TREE_OPERAND (t, 0), 0);
6101 tree low_bound, up_bound, el_sz;
6103 if (TREE_CODE (TREE_TYPE (tem)) != ARRAY_TYPE
6104 || TREE_CODE (TREE_TYPE (TREE_TYPE (tem))) == ARRAY_TYPE
6105 || !TYPE_DOMAIN (TREE_TYPE (tem)))
6108 low_bound = TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (tem)));
6109 up_bound = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (tem)));
6110 el_sz = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (tem)));
6112 || TREE_CODE (low_bound) != INTEGER_CST
6114 || TREE_CODE (up_bound) != INTEGER_CST
6116 || TREE_CODE (el_sz) != INTEGER_CST)
6119 idx = mem_ref_offset (t);
6120 idx = idx.sdiv (tree_to_double_int (el_sz), TRUNC_DIV_EXPR);
6121 if (idx.slt (double_int_zero))
6123 if (dump_file && (dump_flags & TDF_DETAILS))
6125 fprintf (dump_file, "Array bound warning for ");
6126 dump_generic_expr (MSG_NOTE, TDF_SLIM, t);
6127 fprintf (dump_file, "\n");
6129 warning_at (location, OPT_Warray_bounds,
6130 "array subscript is below array bounds");
6131 TREE_NO_WARNING (t) = 1;
6133 else if (idx.sgt (tree_to_double_int (up_bound)
6134 - tree_to_double_int (low_bound)
6137 if (dump_file && (dump_flags & TDF_DETAILS))
6139 fprintf (dump_file, "Array bound warning for ");
6140 dump_generic_expr (MSG_NOTE, TDF_SLIM, t);
6141 fprintf (dump_file, "\n");
6143 warning_at (location, OPT_Warray_bounds,
6144 "array subscript is above array bounds");
6145 TREE_NO_WARNING (t) = 1;
6150 /* walk_tree() callback that checks if *TP is
6151 an ARRAY_REF inside an ADDR_EXPR (in which an array
6152 subscript one outside the valid range is allowed). Call
6153 check_array_ref for each ARRAY_REF found. The location is
6157 check_array_bounds (tree *tp, int *walk_subtree, void *data)
6160 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
6161 location_t location;
6163 if (EXPR_HAS_LOCATION (t))
6164 location = EXPR_LOCATION (t);
6167 location_t *locp = (location_t *) wi->info;
6171 *walk_subtree = TRUE;
6173 if (TREE_CODE (t) == ARRAY_REF)
6174 check_array_ref (location, t, false /*ignore_off_by_one*/);
6176 if (TREE_CODE (t) == MEM_REF
6177 || (TREE_CODE (t) == RETURN_EXPR && TREE_OPERAND (t, 0)))
6178 search_for_addr_array (TREE_OPERAND (t, 0), location);
6180 if (TREE_CODE (t) == ADDR_EXPR)
6181 *walk_subtree = FALSE;
6186 /* Walk over all statements of all reachable BBs and call check_array_bounds
6190 check_all_array_refs (void)
6193 gimple_stmt_iterator si;
6199 bool executable = false;
6201 /* Skip blocks that were found to be unreachable. */
6202 FOR_EACH_EDGE (e, ei, bb->preds)
6203 executable |= !!(e->flags & EDGE_EXECUTABLE);
6207 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
6209 gimple stmt = gsi_stmt (si);
6210 struct walk_stmt_info wi;
6211 if (!gimple_has_location (stmt))
6214 if (is_gimple_call (stmt))
6217 size_t n = gimple_call_num_args (stmt);
6218 for (i = 0; i < n; i++)
6220 tree arg = gimple_call_arg (stmt, i);
6221 search_for_addr_array (arg, gimple_location (stmt));
6226 memset (&wi, 0, sizeof (wi));
6227 wi.info = CONST_CAST (void *, (const void *)
6228 gimple_location_ptr (stmt));
6230 walk_gimple_op (gsi_stmt (si),
6238 /* Convert range assertion expressions into the implied copies and
6239 copy propagate away the copies. Doing the trivial copy propagation
6240 here avoids the need to run the full copy propagation pass after
6243 FIXME, this will eventually lead to copy propagation removing the
6244 names that had useful range information attached to them. For
6245 instance, if we had the assertion N_i = ASSERT_EXPR <N_j, N_j > 3>,
6246 then N_i will have the range [3, +INF].
6248 However, by converting the assertion into the implied copy
6249 operation N_i = N_j, we will then copy-propagate N_j into the uses
6250 of N_i and lose the range information. We may want to hold on to
6251 ASSERT_EXPRs a little while longer as the ranges could be used in
6252 things like jump threading.
6254 The problem with keeping ASSERT_EXPRs around is that passes after
6255 VRP need to handle them appropriately.
6257 Another approach would be to make the range information a first
6258 class property of the SSA_NAME so that it can be queried from
6259 any pass. This is made somewhat more complex by the need for
6260 multiple ranges to be associated with one SSA_NAME. */
6263 remove_range_assertions (void)
6266 gimple_stmt_iterator si;
6268 /* Note that the BSI iterator bump happens at the bottom of the
6269 loop and no bump is necessary if we're removing the statement
6270 referenced by the current BSI. */
6272 for (si = gsi_start_bb (bb); !gsi_end_p (si);)
6274 gimple stmt = gsi_stmt (si);
6277 if (is_gimple_assign (stmt)
6278 && gimple_assign_rhs_code (stmt) == ASSERT_EXPR)
6280 tree rhs = gimple_assign_rhs1 (stmt);
6282 tree cond = fold (ASSERT_EXPR_COND (rhs));
6283 use_operand_p use_p;
6284 imm_use_iterator iter;
6286 gcc_assert (cond != boolean_false_node);
6288 /* Propagate the RHS into every use of the LHS. */
6289 var = ASSERT_EXPR_VAR (rhs);
6290 FOR_EACH_IMM_USE_STMT (use_stmt, iter,
6291 gimple_assign_lhs (stmt))
6292 FOR_EACH_IMM_USE_ON_STMT (use_p, iter)
6294 SET_USE (use_p, var);
6295 gcc_assert (TREE_CODE (var) == SSA_NAME);
6298 /* And finally, remove the copy, it is not needed. */
6299 gsi_remove (&si, true);
6300 release_defs (stmt);
6308 /* Return true if STMT is interesting for VRP. */
6311 stmt_interesting_for_vrp (gimple stmt)
6313 if (gimple_code (stmt) == GIMPLE_PHI)
6315 tree res = gimple_phi_result (stmt);
6316 return (!virtual_operand_p (res)
6317 && (INTEGRAL_TYPE_P (TREE_TYPE (res))
6318 || POINTER_TYPE_P (TREE_TYPE (res))));
6320 else if (is_gimple_assign (stmt) || is_gimple_call (stmt))
6322 tree lhs = gimple_get_lhs (stmt);
6324 /* In general, assignments with virtual operands are not useful
6325 for deriving ranges, with the obvious exception of calls to
6326 builtin functions. */
6327 if (lhs && TREE_CODE (lhs) == SSA_NAME
6328 && (INTEGRAL_TYPE_P (TREE_TYPE (lhs))
6329 || POINTER_TYPE_P (TREE_TYPE (lhs)))
6330 && ((is_gimple_call (stmt)
6331 && gimple_call_fndecl (stmt) != NULL_TREE
6332 && DECL_BUILT_IN (gimple_call_fndecl (stmt)))
6333 || !gimple_vuse (stmt)))
6336 else if (gimple_code (stmt) == GIMPLE_COND
6337 || gimple_code (stmt) == GIMPLE_SWITCH)
6344 /* Initialize local data structures for VRP. */
6347 vrp_initialize (void)
6351 values_propagated = false;
6352 num_vr_values = num_ssa_names;
6353 vr_value = XCNEWVEC (value_range_t *, num_vr_values);
6354 vr_phi_edge_counts = XCNEWVEC (int, num_ssa_names);
6358 gimple_stmt_iterator si;
6360 for (si = gsi_start_phis (bb); !gsi_end_p (si); gsi_next (&si))
6362 gimple phi = gsi_stmt (si);
6363 if (!stmt_interesting_for_vrp (phi))
6365 tree lhs = PHI_RESULT (phi);
6366 set_value_range_to_varying (get_value_range (lhs));
6367 prop_set_simulate_again (phi, false);
6370 prop_set_simulate_again (phi, true);
6373 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
6375 gimple stmt = gsi_stmt (si);
6377 /* If the statement is a control insn, then we do not
6378 want to avoid simulating the statement once. Failure
6379 to do so means that those edges will never get added. */
6380 if (stmt_ends_bb_p (stmt))
6381 prop_set_simulate_again (stmt, true);
6382 else if (!stmt_interesting_for_vrp (stmt))
6386 FOR_EACH_SSA_TREE_OPERAND (def, stmt, i, SSA_OP_DEF)
6387 set_value_range_to_varying (get_value_range (def));
6388 prop_set_simulate_again (stmt, false);
6391 prop_set_simulate_again (stmt, true);
6396 /* Return the singleton value-range for NAME or NAME. */
6399 vrp_valueize (tree name)
6401 if (TREE_CODE (name) == SSA_NAME)
6403 value_range_t *vr = get_value_range (name);
6404 if (vr->type == VR_RANGE
6405 && (vr->min == vr->max
6406 || operand_equal_p (vr->min, vr->max, 0)))
6412 /* Visit assignment STMT. If it produces an interesting range, record
6413 the SSA name in *OUTPUT_P. */
6415 static enum ssa_prop_result
6416 vrp_visit_assignment_or_call (gimple stmt, tree *output_p)
6420 enum gimple_code code = gimple_code (stmt);
6421 lhs = gimple_get_lhs (stmt);
6423 /* We only keep track of ranges in integral and pointer types. */
6424 if (TREE_CODE (lhs) == SSA_NAME
6425 && ((INTEGRAL_TYPE_P (TREE_TYPE (lhs))
6426 /* It is valid to have NULL MIN/MAX values on a type. See
6427 build_range_type. */
6428 && TYPE_MIN_VALUE (TREE_TYPE (lhs))
6429 && TYPE_MAX_VALUE (TREE_TYPE (lhs)))
6430 || POINTER_TYPE_P (TREE_TYPE (lhs))))
6432 value_range_t new_vr = VR_INITIALIZER;
6434 /* Try folding the statement to a constant first. */
6435 tree tem = gimple_fold_stmt_to_constant (stmt, vrp_valueize);
6436 if (tem && !is_overflow_infinity (tem))
6437 set_value_range (&new_vr, VR_RANGE, tem, tem, NULL);
6438 /* Then dispatch to value-range extracting functions. */
6439 else if (code == GIMPLE_CALL)
6440 extract_range_basic (&new_vr, stmt);
6442 extract_range_from_assignment (&new_vr, stmt);
6444 if (update_value_range (lhs, &new_vr))
6448 if (dump_file && (dump_flags & TDF_DETAILS))
6450 fprintf (dump_file, "Found new range for ");
6451 print_generic_expr (dump_file, lhs, 0);
6452 fprintf (dump_file, ": ");
6453 dump_value_range (dump_file, &new_vr);
6454 fprintf (dump_file, "\n\n");
6457 if (new_vr.type == VR_VARYING)
6458 return SSA_PROP_VARYING;
6460 return SSA_PROP_INTERESTING;
6463 return SSA_PROP_NOT_INTERESTING;
6466 /* Every other statement produces no useful ranges. */
6467 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_DEF)
6468 set_value_range_to_varying (get_value_range (def));
6470 return SSA_PROP_VARYING;
6473 /* Helper that gets the value range of the SSA_NAME with version I
6474 or a symbolic range containing the SSA_NAME only if the value range
6475 is varying or undefined. */
6477 static inline value_range_t
6478 get_vr_for_comparison (int i)
6480 value_range_t vr = *get_value_range (ssa_name (i));
6482 /* If name N_i does not have a valid range, use N_i as its own
6483 range. This allows us to compare against names that may
6484 have N_i in their ranges. */
6485 if (vr.type == VR_VARYING || vr.type == VR_UNDEFINED)
6488 vr.min = ssa_name (i);
6489 vr.max = ssa_name (i);
6495 /* Compare all the value ranges for names equivalent to VAR with VAL
6496 using comparison code COMP. Return the same value returned by
6497 compare_range_with_value, including the setting of
6498 *STRICT_OVERFLOW_P. */
6501 compare_name_with_value (enum tree_code comp, tree var, tree val,
6502 bool *strict_overflow_p)
6508 int used_strict_overflow;
6510 value_range_t equiv_vr;
6512 /* Get the set of equivalences for VAR. */
6513 e = get_value_range (var)->equiv;
6515 /* Start at -1. Set it to 0 if we do a comparison without relying
6516 on overflow, or 1 if all comparisons rely on overflow. */
6517 used_strict_overflow = -1;
6519 /* Compare vars' value range with val. */
6520 equiv_vr = get_vr_for_comparison (SSA_NAME_VERSION (var));
6522 retval = compare_range_with_value (comp, &equiv_vr, val, &sop);
6524 used_strict_overflow = sop ? 1 : 0;
6526 /* If the equiv set is empty we have done all work we need to do. */
6530 && used_strict_overflow > 0)
6531 *strict_overflow_p = true;
6535 EXECUTE_IF_SET_IN_BITMAP (e, 0, i, bi)
6537 equiv_vr = get_vr_for_comparison (i);
6539 t = compare_range_with_value (comp, &equiv_vr, val, &sop);
6542 /* If we get different answers from different members
6543 of the equivalence set this check must be in a dead
6544 code region. Folding it to a trap representation
6545 would be correct here. For now just return don't-know. */
6555 used_strict_overflow = 0;
6556 else if (used_strict_overflow < 0)
6557 used_strict_overflow = 1;
6562 && used_strict_overflow > 0)
6563 *strict_overflow_p = true;
6569 /* Given a comparison code COMP and names N1 and N2, compare all the
6570 ranges equivalent to N1 against all the ranges equivalent to N2
6571 to determine the value of N1 COMP N2. Return the same value
6572 returned by compare_ranges. Set *STRICT_OVERFLOW_P to indicate
6573 whether we relied on an overflow infinity in the comparison. */
6577 compare_names (enum tree_code comp, tree n1, tree n2,
6578 bool *strict_overflow_p)
6582 bitmap_iterator bi1, bi2;
6584 int used_strict_overflow;
6585 static bitmap_obstack *s_obstack = NULL;
6586 static bitmap s_e1 = NULL, s_e2 = NULL;
6588 /* Compare the ranges of every name equivalent to N1 against the
6589 ranges of every name equivalent to N2. */
6590 e1 = get_value_range (n1)->equiv;
6591 e2 = get_value_range (n2)->equiv;
6593 /* Use the fake bitmaps if e1 or e2 are not available. */
6594 if (s_obstack == NULL)
6596 s_obstack = XNEW (bitmap_obstack);
6597 bitmap_obstack_initialize (s_obstack);
6598 s_e1 = BITMAP_ALLOC (s_obstack);
6599 s_e2 = BITMAP_ALLOC (s_obstack);
6606 /* Add N1 and N2 to their own set of equivalences to avoid
6607 duplicating the body of the loop just to check N1 and N2
6609 bitmap_set_bit (e1, SSA_NAME_VERSION (n1));
6610 bitmap_set_bit (e2, SSA_NAME_VERSION (n2));
6612 /* If the equivalence sets have a common intersection, then the two
6613 names can be compared without checking their ranges. */
6614 if (bitmap_intersect_p (e1, e2))
6616 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
6617 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
6619 return (comp == EQ_EXPR || comp == GE_EXPR || comp == LE_EXPR)
6621 : boolean_false_node;
6624 /* Start at -1. Set it to 0 if we do a comparison without relying
6625 on overflow, or 1 if all comparisons rely on overflow. */
6626 used_strict_overflow = -1;
6628 /* Otherwise, compare all the equivalent ranges. First, add N1 and
6629 N2 to their own set of equivalences to avoid duplicating the body
6630 of the loop just to check N1 and N2 ranges. */
6631 EXECUTE_IF_SET_IN_BITMAP (e1, 0, i1, bi1)
6633 value_range_t vr1 = get_vr_for_comparison (i1);
6635 t = retval = NULL_TREE;
6636 EXECUTE_IF_SET_IN_BITMAP (e2, 0, i2, bi2)
6640 value_range_t vr2 = get_vr_for_comparison (i2);
6642 t = compare_ranges (comp, &vr1, &vr2, &sop);
6645 /* If we get different answers from different members
6646 of the equivalence set this check must be in a dead
6647 code region. Folding it to a trap representation
6648 would be correct here. For now just return don't-know. */
6652 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
6653 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
6659 used_strict_overflow = 0;
6660 else if (used_strict_overflow < 0)
6661 used_strict_overflow = 1;
6667 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
6668 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
6669 if (used_strict_overflow > 0)
6670 *strict_overflow_p = true;
6675 /* None of the equivalent ranges are useful in computing this
6677 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
6678 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
6682 /* Helper function for vrp_evaluate_conditional_warnv. */
6685 vrp_evaluate_conditional_warnv_with_ops_using_ranges (enum tree_code code,
6687 bool * strict_overflow_p)
6689 value_range_t *vr0, *vr1;
6691 vr0 = (TREE_CODE (op0) == SSA_NAME) ? get_value_range (op0) : NULL;
6692 vr1 = (TREE_CODE (op1) == SSA_NAME) ? get_value_range (op1) : NULL;
6695 return compare_ranges (code, vr0, vr1, strict_overflow_p);
6696 else if (vr0 && vr1 == NULL)
6697 return compare_range_with_value (code, vr0, op1, strict_overflow_p);
6698 else if (vr0 == NULL && vr1)
6699 return (compare_range_with_value
6700 (swap_tree_comparison (code), vr1, op0, strict_overflow_p));
6704 /* Helper function for vrp_evaluate_conditional_warnv. */
6707 vrp_evaluate_conditional_warnv_with_ops (enum tree_code code, tree op0,
6708 tree op1, bool use_equiv_p,
6709 bool *strict_overflow_p, bool *only_ranges)
6713 *only_ranges = true;
6715 /* We only deal with integral and pointer types. */
6716 if (!INTEGRAL_TYPE_P (TREE_TYPE (op0))
6717 && !POINTER_TYPE_P (TREE_TYPE (op0)))
6723 && (ret = vrp_evaluate_conditional_warnv_with_ops_using_ranges
6724 (code, op0, op1, strict_overflow_p)))
6726 *only_ranges = false;
6727 if (TREE_CODE (op0) == SSA_NAME && TREE_CODE (op1) == SSA_NAME)
6728 return compare_names (code, op0, op1, strict_overflow_p);
6729 else if (TREE_CODE (op0) == SSA_NAME)
6730 return compare_name_with_value (code, op0, op1, strict_overflow_p);
6731 else if (TREE_CODE (op1) == SSA_NAME)
6732 return (compare_name_with_value
6733 (swap_tree_comparison (code), op1, op0, strict_overflow_p));
6736 return vrp_evaluate_conditional_warnv_with_ops_using_ranges (code, op0, op1,
6741 /* Given (CODE OP0 OP1) within STMT, try to simplify it based on value range
6742 information. Return NULL if the conditional can not be evaluated.
6743 The ranges of all the names equivalent with the operands in COND
6744 will be used when trying to compute the value. If the result is
6745 based on undefined signed overflow, issue a warning if
6749 vrp_evaluate_conditional (enum tree_code code, tree op0, tree op1, gimple stmt)
6755 /* Some passes and foldings leak constants with overflow flag set
6756 into the IL. Avoid doing wrong things with these and bail out. */
6757 if ((TREE_CODE (op0) == INTEGER_CST
6758 && TREE_OVERFLOW (op0))
6759 || (TREE_CODE (op1) == INTEGER_CST
6760 && TREE_OVERFLOW (op1)))
6764 ret = vrp_evaluate_conditional_warnv_with_ops (code, op0, op1, true, &sop,
6769 enum warn_strict_overflow_code wc;
6770 const char* warnmsg;
6772 if (is_gimple_min_invariant (ret))
6774 wc = WARN_STRICT_OVERFLOW_CONDITIONAL;
6775 warnmsg = G_("assuming signed overflow does not occur when "
6776 "simplifying conditional to constant");
6780 wc = WARN_STRICT_OVERFLOW_COMPARISON;
6781 warnmsg = G_("assuming signed overflow does not occur when "
6782 "simplifying conditional");
6785 if (issue_strict_overflow_warning (wc))
6787 location_t location;
6789 if (!gimple_has_location (stmt))
6790 location = input_location;
6792 location = gimple_location (stmt);
6793 warning_at (location, OPT_Wstrict_overflow, "%s", warnmsg);
6797 if (warn_type_limits
6798 && ret && only_ranges
6799 && TREE_CODE_CLASS (code) == tcc_comparison
6800 && TREE_CODE (op0) == SSA_NAME)
6802 /* If the comparison is being folded and the operand on the LHS
6803 is being compared against a constant value that is outside of
6804 the natural range of OP0's type, then the predicate will
6805 always fold regardless of the value of OP0. If -Wtype-limits
6806 was specified, emit a warning. */
6807 tree type = TREE_TYPE (op0);
6808 value_range_t *vr0 = get_value_range (op0);
6810 if (vr0->type != VR_VARYING
6811 && INTEGRAL_TYPE_P (type)
6812 && vrp_val_is_min (vr0->min)
6813 && vrp_val_is_max (vr0->max)
6814 && is_gimple_min_invariant (op1))
6816 location_t location;
6818 if (!gimple_has_location (stmt))
6819 location = input_location;
6821 location = gimple_location (stmt);
6823 warning_at (location, OPT_Wtype_limits,
6825 ? G_("comparison always false "
6826 "due to limited range of data type")
6827 : G_("comparison always true "
6828 "due to limited range of data type"));
6836 /* Visit conditional statement STMT. If we can determine which edge
6837 will be taken out of STMT's basic block, record it in
6838 *TAKEN_EDGE_P and return SSA_PROP_INTERESTING. Otherwise, return
6839 SSA_PROP_VARYING. */
6841 static enum ssa_prop_result
6842 vrp_visit_cond_stmt (gimple stmt, edge *taken_edge_p)
6847 *taken_edge_p = NULL;
6849 if (dump_file && (dump_flags & TDF_DETAILS))
6854 fprintf (dump_file, "\nVisiting conditional with predicate: ");
6855 print_gimple_stmt (dump_file, stmt, 0, 0);
6856 fprintf (dump_file, "\nWith known ranges\n");
6858 FOR_EACH_SSA_TREE_OPERAND (use, stmt, i, SSA_OP_USE)
6860 fprintf (dump_file, "\t");
6861 print_generic_expr (dump_file, use, 0);
6862 fprintf (dump_file, ": ");
6863 dump_value_range (dump_file, vr_value[SSA_NAME_VERSION (use)]);
6866 fprintf (dump_file, "\n");
6869 /* Compute the value of the predicate COND by checking the known
6870 ranges of each of its operands.
6872 Note that we cannot evaluate all the equivalent ranges here
6873 because those ranges may not yet be final and with the current
6874 propagation strategy, we cannot determine when the value ranges
6875 of the names in the equivalence set have changed.
6877 For instance, given the following code fragment
6881 i_14 = ASSERT_EXPR <i_5, i_5 != 0>
6885 Assume that on the first visit to i_14, i_5 has the temporary
6886 range [8, 8] because the second argument to the PHI function is
6887 not yet executable. We derive the range ~[0, 0] for i_14 and the
6888 equivalence set { i_5 }. So, when we visit 'if (i_14 == 1)' for
6889 the first time, since i_14 is equivalent to the range [8, 8], we
6890 determine that the predicate is always false.
6892 On the next round of propagation, i_13 is determined to be
6893 VARYING, which causes i_5 to drop down to VARYING. So, another
6894 visit to i_14 is scheduled. In this second visit, we compute the
6895 exact same range and equivalence set for i_14, namely ~[0, 0] and
6896 { i_5 }. But we did not have the previous range for i_5
6897 registered, so vrp_visit_assignment thinks that the range for
6898 i_14 has not changed. Therefore, the predicate 'if (i_14 == 1)'
6899 is not visited again, which stops propagation from visiting
6900 statements in the THEN clause of that if().
6902 To properly fix this we would need to keep the previous range
6903 value for the names in the equivalence set. This way we would've
6904 discovered that from one visit to the other i_5 changed from
6905 range [8, 8] to VR_VARYING.
6907 However, fixing this apparent limitation may not be worth the
6908 additional checking. Testing on several code bases (GCC, DLV,
6909 MICO, TRAMP3D and SPEC2000) showed that doing this results in
6910 4 more predicates folded in SPEC. */
6913 val = vrp_evaluate_conditional_warnv_with_ops (gimple_cond_code (stmt),
6914 gimple_cond_lhs (stmt),
6915 gimple_cond_rhs (stmt),
6920 *taken_edge_p = find_taken_edge (gimple_bb (stmt), val);
6923 if (dump_file && (dump_flags & TDF_DETAILS))
6925 "\nIgnoring predicate evaluation because "
6926 "it assumes that signed overflow is undefined");
6931 if (dump_file && (dump_flags & TDF_DETAILS))
6933 fprintf (dump_file, "\nPredicate evaluates to: ");
6934 if (val == NULL_TREE)
6935 fprintf (dump_file, "DON'T KNOW\n");
6937 print_generic_stmt (dump_file, val, 0);
6940 return (*taken_edge_p) ? SSA_PROP_INTERESTING : SSA_PROP_VARYING;
6943 /* Searches the case label vector VEC for the index *IDX of the CASE_LABEL
6944 that includes the value VAL. The search is restricted to the range
6945 [START_IDX, n - 1] where n is the size of VEC.
6947 If there is a CASE_LABEL for VAL, its index is placed in IDX and true is
6950 If there is no CASE_LABEL for VAL and there is one that is larger than VAL,
6951 it is placed in IDX and false is returned.
6953 If VAL is larger than any CASE_LABEL, n is placed on IDX and false is
6957 find_case_label_index (gimple stmt, size_t start_idx, tree val, size_t *idx)
6959 size_t n = gimple_switch_num_labels (stmt);
6962 /* Find case label for minimum of the value range or the next one.
6963 At each iteration we are searching in [low, high - 1]. */
6965 for (low = start_idx, high = n; high != low; )
6969 /* Note that i != high, so we never ask for n. */
6970 size_t i = (high + low) / 2;
6971 t = gimple_switch_label (stmt, i);
6973 /* Cache the result of comparing CASE_LOW and val. */
6974 cmp = tree_int_cst_compare (CASE_LOW (t), val);
6978 /* Ranges cannot be empty. */
6987 if (CASE_HIGH (t) != NULL
6988 && tree_int_cst_compare (CASE_HIGH (t), val) >= 0)
7000 /* Searches the case label vector VEC for the range of CASE_LABELs that is used
7001 for values between MIN and MAX. The first index is placed in MIN_IDX. The
7002 last index is placed in MAX_IDX. If the range of CASE_LABELs is empty
7003 then MAX_IDX < MIN_IDX.
7004 Returns true if the default label is not needed. */
7007 find_case_label_range (gimple stmt, tree min, tree max, size_t *min_idx,
7011 bool min_take_default = !find_case_label_index (stmt, 1, min, &i);
7012 bool max_take_default = !find_case_label_index (stmt, i, max, &j);
7016 && max_take_default)
7018 /* Only the default case label reached.
7019 Return an empty range. */
7026 bool take_default = min_take_default || max_take_default;
7030 if (max_take_default)
7033 /* If the case label range is continuous, we do not need
7034 the default case label. Verify that. */
7035 high = CASE_LOW (gimple_switch_label (stmt, i));
7036 if (CASE_HIGH (gimple_switch_label (stmt, i)))
7037 high = CASE_HIGH (gimple_switch_label (stmt, i));
7038 for (k = i + 1; k <= j; ++k)
7040 low = CASE_LOW (gimple_switch_label (stmt, k));
7041 if (!integer_onep (int_const_binop (MINUS_EXPR, low, high)))
7043 take_default = true;
7047 if (CASE_HIGH (gimple_switch_label (stmt, k)))
7048 high = CASE_HIGH (gimple_switch_label (stmt, k));
7053 return !take_default;
7057 /* Searches the case label vector VEC for the ranges of CASE_LABELs that are
7058 used in range VR. The indices are placed in MIN_IDX1, MAX_IDX, MIN_IDX2 and
7059 MAX_IDX2. If the ranges of CASE_LABELs are empty then MAX_IDX1 < MIN_IDX1.
7060 Returns true if the default label is not needed. */
7063 find_case_label_ranges (gimple stmt, value_range_t *vr, size_t *min_idx1,
7064 size_t *max_idx1, size_t *min_idx2,
7068 unsigned int n = gimple_switch_num_labels (stmt);
7070 tree case_low, case_high;
7071 tree min = vr->min, max = vr->max;
7073 gcc_checking_assert (vr->type == VR_RANGE || vr->type == VR_ANTI_RANGE);
7075 take_default = !find_case_label_range (stmt, min, max, &i, &j);
7077 /* Set second range to emtpy. */
7081 if (vr->type == VR_RANGE)
7085 return !take_default;
7088 /* Set first range to all case labels. */
7095 /* Make sure all the values of case labels [i , j] are contained in
7096 range [MIN, MAX]. */
7097 case_low = CASE_LOW (gimple_switch_label (stmt, i));
7098 case_high = CASE_HIGH (gimple_switch_label (stmt, j));
7099 if (tree_int_cst_compare (case_low, min) < 0)
7101 if (case_high != NULL_TREE
7102 && tree_int_cst_compare (max, case_high) < 0)
7108 /* If the range spans case labels [i, j], the corresponding anti-range spans
7109 the labels [1, i - 1] and [j + 1, n - 1]. */
7135 /* Visit switch statement STMT. If we can determine which edge
7136 will be taken out of STMT's basic block, record it in
7137 *TAKEN_EDGE_P and return SSA_PROP_INTERESTING. Otherwise, return
7138 SSA_PROP_VARYING. */
7140 static enum ssa_prop_result
7141 vrp_visit_switch_stmt (gimple stmt, edge *taken_edge_p)
7145 size_t i = 0, j = 0, k, l;
7148 *taken_edge_p = NULL;
7149 op = gimple_switch_index (stmt);
7150 if (TREE_CODE (op) != SSA_NAME)
7151 return SSA_PROP_VARYING;
7153 vr = get_value_range (op);
7154 if (dump_file && (dump_flags & TDF_DETAILS))
7156 fprintf (dump_file, "\nVisiting switch expression with operand ");
7157 print_generic_expr (dump_file, op, 0);
7158 fprintf (dump_file, " with known range ");
7159 dump_value_range (dump_file, vr);
7160 fprintf (dump_file, "\n");
7163 if ((vr->type != VR_RANGE
7164 && vr->type != VR_ANTI_RANGE)
7165 || symbolic_range_p (vr))
7166 return SSA_PROP_VARYING;
7168 /* Find the single edge that is taken from the switch expression. */
7169 take_default = !find_case_label_ranges (stmt, vr, &i, &j, &k, &l);
7171 /* Check if the range spans no CASE_LABEL. If so, we only reach the default
7175 gcc_assert (take_default);
7176 val = gimple_switch_default_label (stmt);
7180 /* Check if labels with index i to j and maybe the default label
7181 are all reaching the same label. */
7183 val = gimple_switch_label (stmt, i);
7185 && CASE_LABEL (gimple_switch_default_label (stmt))
7186 != CASE_LABEL (val))
7188 if (dump_file && (dump_flags & TDF_DETAILS))
7189 fprintf (dump_file, " not a single destination for this "
7191 return SSA_PROP_VARYING;
7193 for (++i; i <= j; ++i)
7195 if (CASE_LABEL (gimple_switch_label (stmt, i)) != CASE_LABEL (val))
7197 if (dump_file && (dump_flags & TDF_DETAILS))
7198 fprintf (dump_file, " not a single destination for this "
7200 return SSA_PROP_VARYING;
7205 if (CASE_LABEL (gimple_switch_label (stmt, k)) != CASE_LABEL (val))
7207 if (dump_file && (dump_flags & TDF_DETAILS))
7208 fprintf (dump_file, " not a single destination for this "
7210 return SSA_PROP_VARYING;
7215 *taken_edge_p = find_edge (gimple_bb (stmt),
7216 label_to_block (CASE_LABEL (val)));
7218 if (dump_file && (dump_flags & TDF_DETAILS))
7220 fprintf (dump_file, " will take edge to ");
7221 print_generic_stmt (dump_file, CASE_LABEL (val), 0);
7224 return SSA_PROP_INTERESTING;
7228 /* Evaluate statement STMT. If the statement produces a useful range,
7229 return SSA_PROP_INTERESTING and record the SSA name with the
7230 interesting range into *OUTPUT_P.
7232 If STMT is a conditional branch and we can determine its truth
7233 value, the taken edge is recorded in *TAKEN_EDGE_P.
7235 If STMT produces a varying value, return SSA_PROP_VARYING. */
7237 static enum ssa_prop_result
7238 vrp_visit_stmt (gimple stmt, edge *taken_edge_p, tree *output_p)
7243 if (dump_file && (dump_flags & TDF_DETAILS))
7245 fprintf (dump_file, "\nVisiting statement:\n");
7246 print_gimple_stmt (dump_file, stmt, 0, dump_flags);
7247 fprintf (dump_file, "\n");
7250 if (!stmt_interesting_for_vrp (stmt))
7251 gcc_assert (stmt_ends_bb_p (stmt));
7252 else if (is_gimple_assign (stmt) || is_gimple_call (stmt))
7254 /* In general, assignments with virtual operands are not useful
7255 for deriving ranges, with the obvious exception of calls to
7256 builtin functions. */
7257 if ((is_gimple_call (stmt)
7258 && gimple_call_fndecl (stmt) != NULL_TREE
7259 && DECL_BUILT_IN (gimple_call_fndecl (stmt)))
7260 || !gimple_vuse (stmt))
7261 return vrp_visit_assignment_or_call (stmt, output_p);
7263 else if (gimple_code (stmt) == GIMPLE_COND)
7264 return vrp_visit_cond_stmt (stmt, taken_edge_p);
7265 else if (gimple_code (stmt) == GIMPLE_SWITCH)
7266 return vrp_visit_switch_stmt (stmt, taken_edge_p);
7268 /* All other statements produce nothing of interest for VRP, so mark
7269 their outputs varying and prevent further simulation. */
7270 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_DEF)
7271 set_value_range_to_varying (get_value_range (def));
7273 return SSA_PROP_VARYING;
7276 /* Union the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
7277 { VR1TYPE, VR0MIN, VR0MAX } and store the result
7278 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
7279 possible such range. The resulting range is not canonicalized. */
7282 union_ranges (enum value_range_type *vr0type,
7283 tree *vr0min, tree *vr0max,
7284 enum value_range_type vr1type,
7285 tree vr1min, tree vr1max)
7287 bool mineq = operand_equal_p (*vr0min, vr1min, 0);
7288 bool maxeq = operand_equal_p (*vr0max, vr1max, 0);
7290 /* [] is vr0, () is vr1 in the following classification comments. */
7294 if (*vr0type == vr1type)
7295 /* Nothing to do for equal ranges. */
7297 else if ((*vr0type == VR_RANGE
7298 && vr1type == VR_ANTI_RANGE)
7299 || (*vr0type == VR_ANTI_RANGE
7300 && vr1type == VR_RANGE))
7302 /* For anti-range with range union the result is varying. */
7308 else if (operand_less_p (*vr0max, vr1min) == 1
7309 || operand_less_p (vr1max, *vr0min) == 1)
7311 /* [ ] ( ) or ( ) [ ]
7312 If the ranges have an empty intersection, result of the union
7313 operation is the anti-range or if both are anti-ranges
7315 if (*vr0type == VR_ANTI_RANGE
7316 && vr1type == VR_ANTI_RANGE)
7318 else if (*vr0type == VR_ANTI_RANGE
7319 && vr1type == VR_RANGE)
7321 else if (*vr0type == VR_RANGE
7322 && vr1type == VR_ANTI_RANGE)
7328 else if (*vr0type == VR_RANGE
7329 && vr1type == VR_RANGE)
7331 /* The result is the convex hull of both ranges. */
7332 if (operand_less_p (*vr0max, vr1min) == 1)
7334 /* If the result can be an anti-range, create one. */
7335 if (TREE_CODE (*vr0max) == INTEGER_CST
7336 && TREE_CODE (vr1min) == INTEGER_CST
7337 && vrp_val_is_min (*vr0min)
7338 && vrp_val_is_max (vr1max))
7340 tree min = int_const_binop (PLUS_EXPR,
7341 *vr0max, integer_one_node);
7342 tree max = int_const_binop (MINUS_EXPR,
7343 vr1min, integer_one_node);
7344 if (!operand_less_p (max, min))
7346 *vr0type = VR_ANTI_RANGE;
7358 /* If the result can be an anti-range, create one. */
7359 if (TREE_CODE (vr1max) == INTEGER_CST
7360 && TREE_CODE (*vr0min) == INTEGER_CST
7361 && vrp_val_is_min (vr1min)
7362 && vrp_val_is_max (*vr0max))
7364 tree min = int_const_binop (PLUS_EXPR,
7365 vr1max, integer_one_node);
7366 tree max = int_const_binop (MINUS_EXPR,
7367 *vr0min, integer_one_node);
7368 if (!operand_less_p (max, min))
7370 *vr0type = VR_ANTI_RANGE;
7384 else if ((maxeq || operand_less_p (vr1max, *vr0max) == 1)
7385 && (mineq || operand_less_p (*vr0min, vr1min) == 1))
7387 /* [ ( ) ] or [( ) ] or [ ( )] */
7388 if (*vr0type == VR_RANGE
7389 && vr1type == VR_RANGE)
7391 else if (*vr0type == VR_ANTI_RANGE
7392 && vr1type == VR_ANTI_RANGE)
7398 else if (*vr0type == VR_ANTI_RANGE
7399 && vr1type == VR_RANGE)
7401 /* Arbitrarily choose the right or left gap. */
7402 if (!mineq && TREE_CODE (vr1min) == INTEGER_CST)
7403 *vr0max = int_const_binop (MINUS_EXPR, vr1min, integer_one_node);
7404 else if (!maxeq && TREE_CODE (vr1max) == INTEGER_CST)
7405 *vr0min = int_const_binop (PLUS_EXPR, vr1max, integer_one_node);
7409 else if (*vr0type == VR_RANGE
7410 && vr1type == VR_ANTI_RANGE)
7411 /* The result covers everything. */
7416 else if ((maxeq || operand_less_p (*vr0max, vr1max) == 1)
7417 && (mineq || operand_less_p (vr1min, *vr0min) == 1))
7419 /* ( [ ] ) or ([ ] ) or ( [ ]) */
7420 if (*vr0type == VR_RANGE
7421 && vr1type == VR_RANGE)
7427 else if (*vr0type == VR_ANTI_RANGE
7428 && vr1type == VR_ANTI_RANGE)
7430 else if (*vr0type == VR_RANGE
7431 && vr1type == VR_ANTI_RANGE)
7433 *vr0type = VR_ANTI_RANGE;
7434 if (!mineq && TREE_CODE (*vr0min) == INTEGER_CST)
7436 *vr0max = int_const_binop (MINUS_EXPR, *vr0min, integer_one_node);
7439 else if (!maxeq && TREE_CODE (*vr0max) == INTEGER_CST)
7441 *vr0min = int_const_binop (PLUS_EXPR, *vr0max, integer_one_node);
7447 else if (*vr0type == VR_ANTI_RANGE
7448 && vr1type == VR_RANGE)
7449 /* The result covers everything. */
7454 else if ((operand_less_p (vr1min, *vr0max) == 1
7455 || operand_equal_p (vr1min, *vr0max, 0))
7456 && operand_less_p (*vr0min, vr1min) == 1)
7458 /* [ ( ] ) or [ ]( ) */
7459 if (*vr0type == VR_RANGE
7460 && vr1type == VR_RANGE)
7462 else if (*vr0type == VR_ANTI_RANGE
7463 && vr1type == VR_ANTI_RANGE)
7465 else if (*vr0type == VR_ANTI_RANGE
7466 && vr1type == VR_RANGE)
7468 if (TREE_CODE (vr1min) == INTEGER_CST)
7469 *vr0max = int_const_binop (MINUS_EXPR, vr1min, integer_one_node);
7473 else if (*vr0type == VR_RANGE
7474 && vr1type == VR_ANTI_RANGE)
7476 if (TREE_CODE (*vr0max) == INTEGER_CST)
7479 *vr0min = int_const_binop (PLUS_EXPR, *vr0max, integer_one_node);
7488 else if ((operand_less_p (*vr0min, vr1max) == 1
7489 || operand_equal_p (*vr0min, vr1max, 0))
7490 && operand_less_p (vr1min, *vr0min) == 1)
7492 /* ( [ ) ] or ( )[ ] */
7493 if (*vr0type == VR_RANGE
7494 && vr1type == VR_RANGE)
7496 else if (*vr0type == VR_ANTI_RANGE
7497 && vr1type == VR_ANTI_RANGE)
7499 else if (*vr0type == VR_ANTI_RANGE
7500 && vr1type == VR_RANGE)
7502 if (TREE_CODE (vr1max) == INTEGER_CST)
7503 *vr0min = int_const_binop (PLUS_EXPR, vr1max, integer_one_node);
7507 else if (*vr0type == VR_RANGE
7508 && vr1type == VR_ANTI_RANGE)
7510 if (TREE_CODE (*vr0min) == INTEGER_CST)
7514 *vr0max = int_const_binop (MINUS_EXPR, *vr0min, integer_one_node);
7528 *vr0type = VR_VARYING;
7529 *vr0min = NULL_TREE;
7530 *vr0max = NULL_TREE;
7533 /* Intersect the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
7534 { VR1TYPE, VR0MIN, VR0MAX } and store the result
7535 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
7536 possible such range. The resulting range is not canonicalized. */
7539 intersect_ranges (enum value_range_type *vr0type,
7540 tree *vr0min, tree *vr0max,
7541 enum value_range_type vr1type,
7542 tree vr1min, tree vr1max)
7544 bool mineq = operand_equal_p (*vr0min, vr1min, 0);
7545 bool maxeq = operand_equal_p (*vr0max, vr1max, 0);
7547 /* [] is vr0, () is vr1 in the following classification comments. */
7551 if (*vr0type == vr1type)
7552 /* Nothing to do for equal ranges. */
7554 else if ((*vr0type == VR_RANGE
7555 && vr1type == VR_ANTI_RANGE)
7556 || (*vr0type == VR_ANTI_RANGE
7557 && vr1type == VR_RANGE))
7559 /* For anti-range with range intersection the result is empty. */
7560 *vr0type = VR_UNDEFINED;
7561 *vr0min = NULL_TREE;
7562 *vr0max = NULL_TREE;
7567 else if (operand_less_p (*vr0max, vr1min) == 1
7568 || operand_less_p (vr1max, *vr0min) == 1)
7570 /* [ ] ( ) or ( ) [ ]
7571 If the ranges have an empty intersection, the result of the
7572 intersect operation is the range for intersecting an
7573 anti-range with a range or empty when intersecting two ranges. */
7574 if (*vr0type == VR_RANGE
7575 && vr1type == VR_ANTI_RANGE)
7577 else if (*vr0type == VR_ANTI_RANGE
7578 && vr1type == VR_RANGE)
7584 else if (*vr0type == VR_RANGE
7585 && vr1type == VR_RANGE)
7587 *vr0type = VR_UNDEFINED;
7588 *vr0min = NULL_TREE;
7589 *vr0max = NULL_TREE;
7591 else if (*vr0type == VR_ANTI_RANGE
7592 && vr1type == VR_ANTI_RANGE)
7594 /* If the anti-ranges are adjacent to each other merge them. */
7595 if (TREE_CODE (*vr0max) == INTEGER_CST
7596 && TREE_CODE (vr1min) == INTEGER_CST
7597 && operand_less_p (*vr0max, vr1min) == 1
7598 && integer_onep (int_const_binop (MINUS_EXPR,
7601 else if (TREE_CODE (vr1max) == INTEGER_CST
7602 && TREE_CODE (*vr0min) == INTEGER_CST
7603 && operand_less_p (vr1max, *vr0min) == 1
7604 && integer_onep (int_const_binop (MINUS_EXPR,
7607 /* Else arbitrarily take VR0. */
7610 else if ((maxeq || operand_less_p (vr1max, *vr0max) == 1)
7611 && (mineq || operand_less_p (*vr0min, vr1min) == 1))
7613 /* [ ( ) ] or [( ) ] or [ ( )] */
7614 if (*vr0type == VR_RANGE
7615 && vr1type == VR_RANGE)
7617 /* If both are ranges the result is the inner one. */
7622 else if (*vr0type == VR_RANGE
7623 && vr1type == VR_ANTI_RANGE)
7625 /* Choose the right gap if the left one is empty. */
7628 if (TREE_CODE (vr1max) == INTEGER_CST)
7629 *vr0min = int_const_binop (PLUS_EXPR, vr1max, integer_one_node);
7633 /* Choose the left gap if the right one is empty. */
7636 if (TREE_CODE (vr1min) == INTEGER_CST)
7637 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
7642 /* Choose the anti-range if the range is effectively varying. */
7643 else if (vrp_val_is_min (*vr0min)
7644 && vrp_val_is_max (*vr0max))
7650 /* Else choose the range. */
7652 else if (*vr0type == VR_ANTI_RANGE
7653 && vr1type == VR_ANTI_RANGE)
7654 /* If both are anti-ranges the result is the outer one. */
7656 else if (*vr0type == VR_ANTI_RANGE
7657 && vr1type == VR_RANGE)
7659 /* The intersection is empty. */
7660 *vr0type = VR_UNDEFINED;
7661 *vr0min = NULL_TREE;
7662 *vr0max = NULL_TREE;
7667 else if ((maxeq || operand_less_p (*vr0max, vr1max) == 1)
7668 && (mineq || operand_less_p (vr1min, *vr0min) == 1))
7670 /* ( [ ] ) or ([ ] ) or ( [ ]) */
7671 if (*vr0type == VR_RANGE
7672 && vr1type == VR_RANGE)
7673 /* Choose the inner range. */
7675 else if (*vr0type == VR_ANTI_RANGE
7676 && vr1type == VR_RANGE)
7678 /* Choose the right gap if the left is empty. */
7681 *vr0type = VR_RANGE;
7682 if (TREE_CODE (*vr0max) == INTEGER_CST)
7683 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
7689 /* Choose the left gap if the right is empty. */
7692 *vr0type = VR_RANGE;
7693 if (TREE_CODE (*vr0min) == INTEGER_CST)
7694 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
7700 /* Choose the anti-range if the range is effectively varying. */
7701 else if (vrp_val_is_min (vr1min)
7702 && vrp_val_is_max (vr1max))
7704 /* Else choose the range. */
7712 else if (*vr0type == VR_ANTI_RANGE
7713 && vr1type == VR_ANTI_RANGE)
7715 /* If both are anti-ranges the result is the outer one. */
7720 else if (vr1type == VR_ANTI_RANGE
7721 && *vr0type == VR_RANGE)
7723 /* The intersection is empty. */
7724 *vr0type = VR_UNDEFINED;
7725 *vr0min = NULL_TREE;
7726 *vr0max = NULL_TREE;
7731 else if ((operand_less_p (vr1min, *vr0max) == 1
7732 || operand_equal_p (vr1min, *vr0max, 0))
7733 && operand_less_p (*vr0min, vr1min) == 1)
7735 /* [ ( ] ) or [ ]( ) */
7736 if (*vr0type == VR_ANTI_RANGE
7737 && vr1type == VR_ANTI_RANGE)
7739 else if (*vr0type == VR_RANGE
7740 && vr1type == VR_RANGE)
7742 else if (*vr0type == VR_RANGE
7743 && vr1type == VR_ANTI_RANGE)
7745 if (TREE_CODE (vr1min) == INTEGER_CST)
7746 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
7751 else if (*vr0type == VR_ANTI_RANGE
7752 && vr1type == VR_RANGE)
7754 *vr0type = VR_RANGE;
7755 if (TREE_CODE (*vr0max) == INTEGER_CST)
7756 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
7765 else if ((operand_less_p (*vr0min, vr1max) == 1
7766 || operand_equal_p (*vr0min, vr1max, 0))
7767 && operand_less_p (vr1min, *vr0min) == 1)
7769 /* ( [ ) ] or ( )[ ] */
7770 if (*vr0type == VR_ANTI_RANGE
7771 && vr1type == VR_ANTI_RANGE)
7773 else if (*vr0type == VR_RANGE
7774 && vr1type == VR_RANGE)
7776 else if (*vr0type == VR_RANGE
7777 && vr1type == VR_ANTI_RANGE)
7779 if (TREE_CODE (vr1max) == INTEGER_CST)
7780 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
7785 else if (*vr0type == VR_ANTI_RANGE
7786 && vr1type == VR_RANGE)
7788 *vr0type = VR_RANGE;
7789 if (TREE_CODE (*vr0min) == INTEGER_CST)
7790 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
7800 /* As a fallback simply use { *VRTYPE, *VR0MIN, *VR0MAX } as
7801 result for the intersection. That's always a conservative
7802 correct estimate. */
7808 /* Intersect the two value-ranges *VR0 and *VR1 and store the result
7809 in *VR0. This may not be the smallest possible such range. */
7812 vrp_intersect_ranges_1 (value_range_t *vr0, value_range_t *vr1)
7814 value_range_t saved;
7816 /* If either range is VR_VARYING the other one wins. */
7817 if (vr1->type == VR_VARYING)
7819 if (vr0->type == VR_VARYING)
7821 copy_value_range (vr0, vr1);
7825 /* When either range is VR_UNDEFINED the resulting range is
7826 VR_UNDEFINED, too. */
7827 if (vr0->type == VR_UNDEFINED)
7829 if (vr1->type == VR_UNDEFINED)
7831 set_value_range_to_undefined (vr0);
7835 /* Save the original vr0 so we can return it as conservative intersection
7836 result when our worker turns things to varying. */
7838 intersect_ranges (&vr0->type, &vr0->min, &vr0->max,
7839 vr1->type, vr1->min, vr1->max);
7840 /* Make sure to canonicalize the result though as the inversion of a
7841 VR_RANGE can still be a VR_RANGE. */
7842 set_and_canonicalize_value_range (vr0, vr0->type,
7843 vr0->min, vr0->max, vr0->equiv);
7844 /* If that failed, use the saved original VR0. */
7845 if (vr0->type == VR_VARYING)
7850 /* If the result is VR_UNDEFINED there is no need to mess with
7851 the equivalencies. */
7852 if (vr0->type == VR_UNDEFINED)
7855 /* The resulting set of equivalences for range intersection is the union of
7857 if (vr0->equiv && vr1->equiv && vr0->equiv != vr1->equiv)
7858 bitmap_ior_into (vr0->equiv, vr1->equiv);
7859 else if (vr1->equiv && !vr0->equiv)
7860 bitmap_copy (vr0->equiv, vr1->equiv);
7864 vrp_intersect_ranges (value_range_t *vr0, value_range_t *vr1)
7866 if (dump_file && (dump_flags & TDF_DETAILS))
7868 fprintf (dump_file, "Intersecting\n ");
7869 dump_value_range (dump_file, vr0);
7870 fprintf (dump_file, "\nand\n ");
7871 dump_value_range (dump_file, vr1);
7872 fprintf (dump_file, "\n");
7874 vrp_intersect_ranges_1 (vr0, vr1);
7875 if (dump_file && (dump_flags & TDF_DETAILS))
7877 fprintf (dump_file, "to\n ");
7878 dump_value_range (dump_file, vr0);
7879 fprintf (dump_file, "\n");
7883 /* Meet operation for value ranges. Given two value ranges VR0 and
7884 VR1, store in VR0 a range that contains both VR0 and VR1. This
7885 may not be the smallest possible such range. */
7888 vrp_meet_1 (value_range_t *vr0, value_range_t *vr1)
7890 value_range_t saved;
7892 if (vr0->type == VR_UNDEFINED)
7894 set_value_range (vr0, vr1->type, vr1->min, vr1->max, vr1->equiv);
7898 if (vr1->type == VR_UNDEFINED)
7900 /* VR0 already has the resulting range. */
7904 if (vr0->type == VR_VARYING)
7906 /* Nothing to do. VR0 already has the resulting range. */
7910 if (vr1->type == VR_VARYING)
7912 set_value_range_to_varying (vr0);
7917 union_ranges (&vr0->type, &vr0->min, &vr0->max,
7918 vr1->type, vr1->min, vr1->max);
7919 if (vr0->type == VR_VARYING)
7921 /* Failed to find an efficient meet. Before giving up and setting
7922 the result to VARYING, see if we can at least derive a useful
7923 anti-range. FIXME, all this nonsense about distinguishing
7924 anti-ranges from ranges is necessary because of the odd
7925 semantics of range_includes_zero_p and friends. */
7926 if (((saved.type == VR_RANGE
7927 && range_includes_zero_p (saved.min, saved.max) == 0)
7928 || (saved.type == VR_ANTI_RANGE
7929 && range_includes_zero_p (saved.min, saved.max) == 1))
7930 && ((vr1->type == VR_RANGE
7931 && range_includes_zero_p (vr1->min, vr1->max) == 0)
7932 || (vr1->type == VR_ANTI_RANGE
7933 && range_includes_zero_p (vr1->min, vr1->max) == 1)))
7935 set_value_range_to_nonnull (vr0, TREE_TYPE (saved.min));
7937 /* Since this meet operation did not result from the meeting of
7938 two equivalent names, VR0 cannot have any equivalences. */
7940 bitmap_clear (vr0->equiv);
7944 set_value_range_to_varying (vr0);
7947 set_and_canonicalize_value_range (vr0, vr0->type, vr0->min, vr0->max,
7949 if (vr0->type == VR_VARYING)
7952 /* The resulting set of equivalences is always the intersection of
7954 if (vr0->equiv && vr1->equiv && vr0->equiv != vr1->equiv)
7955 bitmap_and_into (vr0->equiv, vr1->equiv);
7956 else if (vr0->equiv && !vr1->equiv)
7957 bitmap_clear (vr0->equiv);
7961 vrp_meet (value_range_t *vr0, value_range_t *vr1)
7963 if (dump_file && (dump_flags & TDF_DETAILS))
7965 fprintf (dump_file, "Meeting\n ");
7966 dump_value_range (dump_file, vr0);
7967 fprintf (dump_file, "\nand\n ");
7968 dump_value_range (dump_file, vr1);
7969 fprintf (dump_file, "\n");
7971 vrp_meet_1 (vr0, vr1);
7972 if (dump_file && (dump_flags & TDF_DETAILS))
7974 fprintf (dump_file, "to\n ");
7975 dump_value_range (dump_file, vr0);
7976 fprintf (dump_file, "\n");
7981 /* Visit all arguments for PHI node PHI that flow through executable
7982 edges. If a valid value range can be derived from all the incoming
7983 value ranges, set a new range for the LHS of PHI. */
7985 static enum ssa_prop_result
7986 vrp_visit_phi_node (gimple phi)
7989 tree lhs = PHI_RESULT (phi);
7990 value_range_t *lhs_vr = get_value_range (lhs);
7991 value_range_t vr_result = VR_INITIALIZER;
7993 int edges, old_edges;
7996 if (dump_file && (dump_flags & TDF_DETAILS))
7998 fprintf (dump_file, "\nVisiting PHI node: ");
7999 print_gimple_stmt (dump_file, phi, 0, dump_flags);
8003 for (i = 0; i < gimple_phi_num_args (phi); i++)
8005 edge e = gimple_phi_arg_edge (phi, i);
8007 if (dump_file && (dump_flags & TDF_DETAILS))
8010 "\n Argument #%d (%d -> %d %sexecutable)\n",
8011 (int) i, e->src->index, e->dest->index,
8012 (e->flags & EDGE_EXECUTABLE) ? "" : "not ");
8015 if (e->flags & EDGE_EXECUTABLE)
8017 tree arg = PHI_ARG_DEF (phi, i);
8018 value_range_t vr_arg;
8022 if (TREE_CODE (arg) == SSA_NAME)
8024 vr_arg = *(get_value_range (arg));
8025 /* Do not allow equivalences or symbolic ranges to leak in from
8026 backedges. That creates invalid equivalencies.
8027 See PR53465 and PR54767. */
8028 if (e->flags & EDGE_DFS_BACK
8029 && (vr_arg.type == VR_RANGE
8030 || vr_arg.type == VR_ANTI_RANGE))
8032 vr_arg.equiv = NULL;
8033 if (symbolic_range_p (&vr_arg))
8035 vr_arg.type = VR_VARYING;
8036 vr_arg.min = NULL_TREE;
8037 vr_arg.max = NULL_TREE;
8043 if (is_overflow_infinity (arg))
8045 arg = copy_node (arg);
8046 TREE_OVERFLOW (arg) = 0;
8049 vr_arg.type = VR_RANGE;
8052 vr_arg.equiv = NULL;
8055 if (dump_file && (dump_flags & TDF_DETAILS))
8057 fprintf (dump_file, "\t");
8058 print_generic_expr (dump_file, arg, dump_flags);
8059 fprintf (dump_file, "\n\tValue: ");
8060 dump_value_range (dump_file, &vr_arg);
8061 fprintf (dump_file, "\n");
8065 copy_value_range (&vr_result, &vr_arg);
8067 vrp_meet (&vr_result, &vr_arg);
8070 if (vr_result.type == VR_VARYING)
8075 if (vr_result.type == VR_VARYING)
8077 else if (vr_result.type == VR_UNDEFINED)
8080 old_edges = vr_phi_edge_counts[SSA_NAME_VERSION (lhs)];
8081 vr_phi_edge_counts[SSA_NAME_VERSION (lhs)] = edges;
8083 /* To prevent infinite iterations in the algorithm, derive ranges
8084 when the new value is slightly bigger or smaller than the
8085 previous one. We don't do this if we have seen a new executable
8086 edge; this helps us avoid an overflow infinity for conditionals
8087 which are not in a loop. If the old value-range was VR_UNDEFINED
8088 use the updated range and iterate one more time. */
8090 && gimple_phi_num_args (phi) > 1
8091 && edges == old_edges
8092 && lhs_vr->type != VR_UNDEFINED)
8094 int cmp_min = compare_values (lhs_vr->min, vr_result.min);
8095 int cmp_max = compare_values (lhs_vr->max, vr_result.max);
8097 /* For non VR_RANGE or for pointers fall back to varying if
8098 the range changed. */
8099 if ((lhs_vr->type != VR_RANGE || vr_result.type != VR_RANGE
8100 || POINTER_TYPE_P (TREE_TYPE (lhs)))
8101 && (cmp_min != 0 || cmp_max != 0))
8104 /* If the new minimum is smaller or larger than the previous
8105 one, go all the way to -INF. In the first case, to avoid
8106 iterating millions of times to reach -INF, and in the
8107 other case to avoid infinite bouncing between different
8109 if (cmp_min > 0 || cmp_min < 0)
8111 if (!needs_overflow_infinity (TREE_TYPE (vr_result.min))
8112 || !vrp_var_may_overflow (lhs, phi))
8113 vr_result.min = TYPE_MIN_VALUE (TREE_TYPE (vr_result.min));
8114 else if (supports_overflow_infinity (TREE_TYPE (vr_result.min)))
8116 negative_overflow_infinity (TREE_TYPE (vr_result.min));
8119 /* Similarly, if the new maximum is smaller or larger than
8120 the previous one, go all the way to +INF. */
8121 if (cmp_max < 0 || cmp_max > 0)
8123 if (!needs_overflow_infinity (TREE_TYPE (vr_result.max))
8124 || !vrp_var_may_overflow (lhs, phi))
8125 vr_result.max = TYPE_MAX_VALUE (TREE_TYPE (vr_result.max));
8126 else if (supports_overflow_infinity (TREE_TYPE (vr_result.max)))
8128 positive_overflow_infinity (TREE_TYPE (vr_result.max));
8131 /* If we dropped either bound to +-INF then if this is a loop
8132 PHI node SCEV may known more about its value-range. */
8133 if ((cmp_min > 0 || cmp_min < 0
8134 || cmp_max < 0 || cmp_max > 0)
8136 && (l = loop_containing_stmt (phi))
8137 && l->header == gimple_bb (phi))
8138 adjust_range_with_scev (&vr_result, l, phi, lhs);
8140 /* If we will end up with a (-INF, +INF) range, set it to
8141 VARYING. Same if the previous max value was invalid for
8142 the type and we end up with vr_result.min > vr_result.max. */
8143 if ((vrp_val_is_max (vr_result.max)
8144 && vrp_val_is_min (vr_result.min))
8145 || compare_values (vr_result.min,
8150 /* If the new range is different than the previous value, keep
8153 if (update_value_range (lhs, &vr_result))
8155 if (dump_file && (dump_flags & TDF_DETAILS))
8157 fprintf (dump_file, "Found new range for ");
8158 print_generic_expr (dump_file, lhs, 0);
8159 fprintf (dump_file, ": ");
8160 dump_value_range (dump_file, &vr_result);
8161 fprintf (dump_file, "\n\n");
8164 return SSA_PROP_INTERESTING;
8167 /* Nothing changed, don't add outgoing edges. */
8168 return SSA_PROP_NOT_INTERESTING;
8170 /* No match found. Set the LHS to VARYING. */
8172 set_value_range_to_varying (lhs_vr);
8173 return SSA_PROP_VARYING;
8176 /* Simplify boolean operations if the source is known
8177 to be already a boolean. */
8179 simplify_truth_ops_using_ranges (gimple_stmt_iterator *gsi, gimple stmt)
8181 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
8183 bool need_conversion;
8185 /* We handle only !=/== case here. */
8186 gcc_assert (rhs_code == EQ_EXPR || rhs_code == NE_EXPR);
8188 op0 = gimple_assign_rhs1 (stmt);
8189 if (!op_with_boolean_value_range_p (op0))
8192 op1 = gimple_assign_rhs2 (stmt);
8193 if (!op_with_boolean_value_range_p (op1))
8196 /* Reduce number of cases to handle to NE_EXPR. As there is no
8197 BIT_XNOR_EXPR we cannot replace A == B with a single statement. */
8198 if (rhs_code == EQ_EXPR)
8200 if (TREE_CODE (op1) == INTEGER_CST)
8201 op1 = int_const_binop (BIT_XOR_EXPR, op1, integer_one_node);
8206 lhs = gimple_assign_lhs (stmt);
8208 = !useless_type_conversion_p (TREE_TYPE (lhs), TREE_TYPE (op0));
8210 /* Make sure to not sign-extend a 1-bit 1 when converting the result. */
8212 && !TYPE_UNSIGNED (TREE_TYPE (op0))
8213 && TYPE_PRECISION (TREE_TYPE (op0)) == 1
8214 && TYPE_PRECISION (TREE_TYPE (lhs)) > 1)
8217 /* For A != 0 we can substitute A itself. */
8218 if (integer_zerop (op1))
8219 gimple_assign_set_rhs_with_ops (gsi,
8221 ? NOP_EXPR : TREE_CODE (op0),
8223 /* For A != B we substitute A ^ B. Either with conversion. */
8224 else if (need_conversion)
8226 tree tem = make_ssa_name (TREE_TYPE (op0), NULL);
8227 gimple newop = gimple_build_assign_with_ops (BIT_XOR_EXPR, tem, op0, op1);
8228 gsi_insert_before (gsi, newop, GSI_SAME_STMT);
8229 gimple_assign_set_rhs_with_ops (gsi, NOP_EXPR, tem, NULL_TREE);
8233 gimple_assign_set_rhs_with_ops (gsi, BIT_XOR_EXPR, op0, op1);
8234 update_stmt (gsi_stmt (*gsi));
8239 /* Simplify a division or modulo operator to a right shift or
8240 bitwise and if the first operand is unsigned or is greater
8241 than zero and the second operand is an exact power of two. */
8244 simplify_div_or_mod_using_ranges (gimple stmt)
8246 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
8248 tree op0 = gimple_assign_rhs1 (stmt);
8249 tree op1 = gimple_assign_rhs2 (stmt);
8250 value_range_t *vr = get_value_range (gimple_assign_rhs1 (stmt));
8252 if (TYPE_UNSIGNED (TREE_TYPE (op0)))
8254 val = integer_one_node;
8260 val = compare_range_with_value (GE_EXPR, vr, integer_zero_node, &sop);
8264 && integer_onep (val)
8265 && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC))
8267 location_t location;
8269 if (!gimple_has_location (stmt))
8270 location = input_location;
8272 location = gimple_location (stmt);
8273 warning_at (location, OPT_Wstrict_overflow,
8274 "assuming signed overflow does not occur when "
8275 "simplifying %</%> or %<%%%> to %<>>%> or %<&%>");
8279 if (val && integer_onep (val))
8283 if (rhs_code == TRUNC_DIV_EXPR)
8285 t = build_int_cst (integer_type_node, tree_log2 (op1));
8286 gimple_assign_set_rhs_code (stmt, RSHIFT_EXPR);
8287 gimple_assign_set_rhs1 (stmt, op0);
8288 gimple_assign_set_rhs2 (stmt, t);
8292 t = build_int_cst (TREE_TYPE (op1), 1);
8293 t = int_const_binop (MINUS_EXPR, op1, t);
8294 t = fold_convert (TREE_TYPE (op0), t);
8296 gimple_assign_set_rhs_code (stmt, BIT_AND_EXPR);
8297 gimple_assign_set_rhs1 (stmt, op0);
8298 gimple_assign_set_rhs2 (stmt, t);
8308 /* If the operand to an ABS_EXPR is >= 0, then eliminate the
8309 ABS_EXPR. If the operand is <= 0, then simplify the
8310 ABS_EXPR into a NEGATE_EXPR. */
8313 simplify_abs_using_ranges (gimple stmt)
8316 tree op = gimple_assign_rhs1 (stmt);
8317 tree type = TREE_TYPE (op);
8318 value_range_t *vr = get_value_range (op);
8320 if (TYPE_UNSIGNED (type))
8322 val = integer_zero_node;
8328 val = compare_range_with_value (LE_EXPR, vr, integer_zero_node, &sop);
8332 val = compare_range_with_value (GE_EXPR, vr, integer_zero_node,
8337 if (integer_zerop (val))
8338 val = integer_one_node;
8339 else if (integer_onep (val))
8340 val = integer_zero_node;
8345 && (integer_onep (val) || integer_zerop (val)))
8347 if (sop && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC))
8349 location_t location;
8351 if (!gimple_has_location (stmt))
8352 location = input_location;
8354 location = gimple_location (stmt);
8355 warning_at (location, OPT_Wstrict_overflow,
8356 "assuming signed overflow does not occur when "
8357 "simplifying %<abs (X)%> to %<X%> or %<-X%>");
8360 gimple_assign_set_rhs1 (stmt, op);
8361 if (integer_onep (val))
8362 gimple_assign_set_rhs_code (stmt, NEGATE_EXPR);
8364 gimple_assign_set_rhs_code (stmt, SSA_NAME);
8373 /* Optimize away redundant BIT_AND_EXPR and BIT_IOR_EXPR.
8374 If all the bits that are being cleared by & are already
8375 known to be zero from VR, or all the bits that are being
8376 set by | are already known to be one from VR, the bit
8377 operation is redundant. */
8380 simplify_bit_ops_using_ranges (gimple_stmt_iterator *gsi, gimple stmt)
8382 tree op0 = gimple_assign_rhs1 (stmt);
8383 tree op1 = gimple_assign_rhs2 (stmt);
8384 tree op = NULL_TREE;
8385 value_range_t vr0 = VR_INITIALIZER;
8386 value_range_t vr1 = VR_INITIALIZER;
8387 double_int may_be_nonzero0, may_be_nonzero1;
8388 double_int must_be_nonzero0, must_be_nonzero1;
8391 if (TREE_CODE (op0) == SSA_NAME)
8392 vr0 = *(get_value_range (op0));
8393 else if (is_gimple_min_invariant (op0))
8394 set_value_range_to_value (&vr0, op0, NULL);
8398 if (TREE_CODE (op1) == SSA_NAME)
8399 vr1 = *(get_value_range (op1));
8400 else if (is_gimple_min_invariant (op1))
8401 set_value_range_to_value (&vr1, op1, NULL);
8405 if (!zero_nonzero_bits_from_vr (&vr0, &may_be_nonzero0, &must_be_nonzero0))
8407 if (!zero_nonzero_bits_from_vr (&vr1, &may_be_nonzero1, &must_be_nonzero1))
8410 switch (gimple_assign_rhs_code (stmt))
8413 mask = may_be_nonzero0.and_not (must_be_nonzero1);
8414 if (mask.is_zero ())
8419 mask = may_be_nonzero1.and_not (must_be_nonzero0);
8420 if (mask.is_zero ())
8427 mask = may_be_nonzero0.and_not (must_be_nonzero1);
8428 if (mask.is_zero ())
8433 mask = may_be_nonzero1.and_not (must_be_nonzero0);
8434 if (mask.is_zero ())
8444 if (op == NULL_TREE)
8447 gimple_assign_set_rhs_with_ops (gsi, TREE_CODE (op), op, NULL);
8448 update_stmt (gsi_stmt (*gsi));
8452 /* We are comparing trees OP0 and OP1 using COND_CODE. OP0 has
8453 a known value range VR.
8455 If there is one and only one value which will satisfy the
8456 conditional, then return that value. Else return NULL. */
8459 test_for_singularity (enum tree_code cond_code, tree op0,
8460 tree op1, value_range_t *vr)
8465 /* Extract minimum/maximum values which satisfy the
8466 the conditional as it was written. */
8467 if (cond_code == LE_EXPR || cond_code == LT_EXPR)
8469 /* This should not be negative infinity; there is no overflow
8471 min = TYPE_MIN_VALUE (TREE_TYPE (op0));
8474 if (cond_code == LT_EXPR && !is_overflow_infinity (max))
8476 tree one = build_int_cst (TREE_TYPE (op0), 1);
8477 max = fold_build2 (MINUS_EXPR, TREE_TYPE (op0), max, one);
8479 TREE_NO_WARNING (max) = 1;
8482 else if (cond_code == GE_EXPR || cond_code == GT_EXPR)
8484 /* This should not be positive infinity; there is no overflow
8486 max = TYPE_MAX_VALUE (TREE_TYPE (op0));
8489 if (cond_code == GT_EXPR && !is_overflow_infinity (min))
8491 tree one = build_int_cst (TREE_TYPE (op0), 1);
8492 min = fold_build2 (PLUS_EXPR, TREE_TYPE (op0), min, one);
8494 TREE_NO_WARNING (min) = 1;
8498 /* Now refine the minimum and maximum values using any
8499 value range information we have for op0. */
8502 if (compare_values (vr->min, min) == 1)
8504 if (compare_values (vr->max, max) == -1)
8507 /* If the new min/max values have converged to a single value,
8508 then there is only one value which can satisfy the condition,
8509 return that value. */
8510 if (operand_equal_p (min, max, 0) && is_gimple_min_invariant (min))
8516 /* Simplify a conditional using a relational operator to an equality
8517 test if the range information indicates only one value can satisfy
8518 the original conditional. */
8521 simplify_cond_using_ranges (gimple stmt)
8523 tree op0 = gimple_cond_lhs (stmt);
8524 tree op1 = gimple_cond_rhs (stmt);
8525 enum tree_code cond_code = gimple_cond_code (stmt);
8527 if (cond_code != NE_EXPR
8528 && cond_code != EQ_EXPR
8529 && TREE_CODE (op0) == SSA_NAME
8530 && INTEGRAL_TYPE_P (TREE_TYPE (op0))
8531 && is_gimple_min_invariant (op1))
8533 value_range_t *vr = get_value_range (op0);
8535 /* If we have range information for OP0, then we might be
8536 able to simplify this conditional. */
8537 if (vr->type == VR_RANGE)
8539 tree new_tree = test_for_singularity (cond_code, op0, op1, vr);
8545 fprintf (dump_file, "Simplified relational ");
8546 print_gimple_stmt (dump_file, stmt, 0, 0);
8547 fprintf (dump_file, " into ");
8550 gimple_cond_set_code (stmt, EQ_EXPR);
8551 gimple_cond_set_lhs (stmt, op0);
8552 gimple_cond_set_rhs (stmt, new_tree);
8558 print_gimple_stmt (dump_file, stmt, 0, 0);
8559 fprintf (dump_file, "\n");
8565 /* Try again after inverting the condition. We only deal
8566 with integral types here, so no need to worry about
8567 issues with inverting FP comparisons. */
8568 cond_code = invert_tree_comparison (cond_code, false);
8569 new_tree = test_for_singularity (cond_code, op0, op1, vr);
8575 fprintf (dump_file, "Simplified relational ");
8576 print_gimple_stmt (dump_file, stmt, 0, 0);
8577 fprintf (dump_file, " into ");
8580 gimple_cond_set_code (stmt, NE_EXPR);
8581 gimple_cond_set_lhs (stmt, op0);
8582 gimple_cond_set_rhs (stmt, new_tree);
8588 print_gimple_stmt (dump_file, stmt, 0, 0);
8589 fprintf (dump_file, "\n");
8600 /* Simplify a switch statement using the value range of the switch
8604 simplify_switch_using_ranges (gimple stmt)
8606 tree op = gimple_switch_index (stmt);
8611 size_t i = 0, j = 0, n, n2;
8614 size_t k = 1, l = 0;
8616 if (TREE_CODE (op) == SSA_NAME)
8618 vr = get_value_range (op);
8620 /* We can only handle integer ranges. */
8621 if ((vr->type != VR_RANGE
8622 && vr->type != VR_ANTI_RANGE)
8623 || symbolic_range_p (vr))
8626 /* Find case label for min/max of the value range. */
8627 take_default = !find_case_label_ranges (stmt, vr, &i, &j, &k, &l);
8629 else if (TREE_CODE (op) == INTEGER_CST)
8631 take_default = !find_case_label_index (stmt, 1, op, &i);
8645 n = gimple_switch_num_labels (stmt);
8647 /* Bail out if this is just all edges taken. */
8653 /* Build a new vector of taken case labels. */
8654 vec2 = make_tree_vec (j - i + 1 + l - k + 1 + (int)take_default);
8657 /* Add the default edge, if necessary. */
8659 TREE_VEC_ELT (vec2, n2++) = gimple_switch_default_label (stmt);
8661 for (; i <= j; ++i, ++n2)
8662 TREE_VEC_ELT (vec2, n2) = gimple_switch_label (stmt, i);
8664 for (; k <= l; ++k, ++n2)
8665 TREE_VEC_ELT (vec2, n2) = gimple_switch_label (stmt, k);
8667 /* Mark needed edges. */
8668 for (i = 0; i < n2; ++i)
8670 e = find_edge (gimple_bb (stmt),
8671 label_to_block (CASE_LABEL (TREE_VEC_ELT (vec2, i))));
8672 e->aux = (void *)-1;
8675 /* Queue not needed edges for later removal. */
8676 FOR_EACH_EDGE (e, ei, gimple_bb (stmt)->succs)
8678 if (e->aux == (void *)-1)
8684 if (dump_file && (dump_flags & TDF_DETAILS))
8686 fprintf (dump_file, "removing unreachable case label\n");
8688 to_remove_edges.safe_push (e);
8689 e->flags &= ~EDGE_EXECUTABLE;
8692 /* And queue an update for the stmt. */
8695 to_update_switch_stmts.safe_push (su);
8699 /* Simplify an integral conversion from an SSA name in STMT. */
8702 simplify_conversion_using_ranges (gimple stmt)
8704 tree innerop, middleop, finaltype;
8706 value_range_t *innervr;
8707 bool inner_unsigned_p, middle_unsigned_p, final_unsigned_p;
8708 unsigned inner_prec, middle_prec, final_prec;
8709 double_int innermin, innermed, innermax, middlemin, middlemed, middlemax;
8711 finaltype = TREE_TYPE (gimple_assign_lhs (stmt));
8712 if (!INTEGRAL_TYPE_P (finaltype))
8714 middleop = gimple_assign_rhs1 (stmt);
8715 def_stmt = SSA_NAME_DEF_STMT (middleop);
8716 if (!is_gimple_assign (def_stmt)
8717 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt)))
8719 innerop = gimple_assign_rhs1 (def_stmt);
8720 if (TREE_CODE (innerop) != SSA_NAME)
8723 /* Get the value-range of the inner operand. */
8724 innervr = get_value_range (innerop);
8725 if (innervr->type != VR_RANGE
8726 || TREE_CODE (innervr->min) != INTEGER_CST
8727 || TREE_CODE (innervr->max) != INTEGER_CST)
8730 /* Simulate the conversion chain to check if the result is equal if
8731 the middle conversion is removed. */
8732 innermin = tree_to_double_int (innervr->min);
8733 innermax = tree_to_double_int (innervr->max);
8735 inner_prec = TYPE_PRECISION (TREE_TYPE (innerop));
8736 middle_prec = TYPE_PRECISION (TREE_TYPE (middleop));
8737 final_prec = TYPE_PRECISION (finaltype);
8739 /* If the first conversion is not injective, the second must not
8741 if ((innermax - innermin).ugt (double_int::mask (middle_prec))
8742 && middle_prec < final_prec)
8744 /* We also want a medium value so that we can track the effect that
8745 narrowing conversions with sign change have. */
8746 inner_unsigned_p = TYPE_UNSIGNED (TREE_TYPE (innerop));
8747 if (inner_unsigned_p)
8748 innermed = double_int::mask (inner_prec).lrshift (1, inner_prec);
8750 innermed = double_int_zero;
8751 if (innermin.cmp (innermed, inner_unsigned_p) >= 0
8752 || innermed.cmp (innermax, inner_unsigned_p) >= 0)
8753 innermed = innermin;
8755 middle_unsigned_p = TYPE_UNSIGNED (TREE_TYPE (middleop));
8756 middlemin = innermin.ext (middle_prec, middle_unsigned_p);
8757 middlemed = innermed.ext (middle_prec, middle_unsigned_p);
8758 middlemax = innermax.ext (middle_prec, middle_unsigned_p);
8760 /* Require that the final conversion applied to both the original
8761 and the intermediate range produces the same result. */
8762 final_unsigned_p = TYPE_UNSIGNED (finaltype);
8763 if (middlemin.ext (final_prec, final_unsigned_p)
8764 != innermin.ext (final_prec, final_unsigned_p)
8765 || middlemed.ext (final_prec, final_unsigned_p)
8766 != innermed.ext (final_prec, final_unsigned_p)
8767 || middlemax.ext (final_prec, final_unsigned_p)
8768 != innermax.ext (final_prec, final_unsigned_p))
8771 gimple_assign_set_rhs1 (stmt, innerop);
8776 /* Return whether the value range *VR fits in an integer type specified
8777 by PRECISION and UNSIGNED_P. */
8780 range_fits_type_p (value_range_t *vr, unsigned precision, bool unsigned_p)
8783 unsigned src_precision;
8786 /* We can only handle integral and pointer types. */
8787 src_type = TREE_TYPE (vr->min);
8788 if (!INTEGRAL_TYPE_P (src_type)
8789 && !POINTER_TYPE_P (src_type))
8792 /* An extension is fine unless VR is signed and unsigned_p,
8793 and so is an identity transform. */
8794 src_precision = TYPE_PRECISION (TREE_TYPE (vr->min));
8795 if ((src_precision < precision
8796 && !(unsigned_p && !TYPE_UNSIGNED (src_type)))
8797 || (src_precision == precision
8798 && TYPE_UNSIGNED (src_type) == unsigned_p))
8801 /* Now we can only handle ranges with constant bounds. */
8802 if (vr->type != VR_RANGE
8803 || TREE_CODE (vr->min) != INTEGER_CST
8804 || TREE_CODE (vr->max) != INTEGER_CST)
8807 /* For sign changes, the MSB of the double_int has to be clear.
8808 An unsigned value with its MSB set cannot be represented by
8809 a signed double_int, while a negative value cannot be represented
8810 by an unsigned double_int. */
8811 if (TYPE_UNSIGNED (src_type) != unsigned_p
8812 && (TREE_INT_CST_HIGH (vr->min) | TREE_INT_CST_HIGH (vr->max)) < 0)
8815 /* Then we can perform the conversion on both ends and compare
8816 the result for equality. */
8817 tem = tree_to_double_int (vr->min).ext (precision, unsigned_p);
8818 if (tree_to_double_int (vr->min) != tem)
8820 tem = tree_to_double_int (vr->max).ext (precision, unsigned_p);
8821 if (tree_to_double_int (vr->max) != tem)
8827 /* Simplify a conversion from integral SSA name to float in STMT. */
8830 simplify_float_conversion_using_ranges (gimple_stmt_iterator *gsi, gimple stmt)
8832 tree rhs1 = gimple_assign_rhs1 (stmt);
8833 value_range_t *vr = get_value_range (rhs1);
8834 enum machine_mode fltmode = TYPE_MODE (TREE_TYPE (gimple_assign_lhs (stmt)));
8835 enum machine_mode mode;
8839 /* We can only handle constant ranges. */
8840 if (vr->type != VR_RANGE
8841 || TREE_CODE (vr->min) != INTEGER_CST
8842 || TREE_CODE (vr->max) != INTEGER_CST)
8845 /* First check if we can use a signed type in place of an unsigned. */
8846 if (TYPE_UNSIGNED (TREE_TYPE (rhs1))
8847 && (can_float_p (fltmode, TYPE_MODE (TREE_TYPE (rhs1)), 0)
8848 != CODE_FOR_nothing)
8849 && range_fits_type_p (vr, GET_MODE_PRECISION
8850 (TYPE_MODE (TREE_TYPE (rhs1))), 0))
8851 mode = TYPE_MODE (TREE_TYPE (rhs1));
8852 /* If we can do the conversion in the current input mode do nothing. */
8853 else if (can_float_p (fltmode, TYPE_MODE (TREE_TYPE (rhs1)),
8854 TYPE_UNSIGNED (TREE_TYPE (rhs1))) != CODE_FOR_nothing)
8856 /* Otherwise search for a mode we can use, starting from the narrowest
8857 integer mode available. */
8860 mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
8863 /* If we cannot do a signed conversion to float from mode
8864 or if the value-range does not fit in the signed type
8865 try with a wider mode. */
8866 if (can_float_p (fltmode, mode, 0) != CODE_FOR_nothing
8867 && range_fits_type_p (vr, GET_MODE_PRECISION (mode), 0))
8870 mode = GET_MODE_WIDER_MODE (mode);
8871 /* But do not widen the input. Instead leave that to the
8872 optabs expansion code. */
8873 if (GET_MODE_PRECISION (mode) > TYPE_PRECISION (TREE_TYPE (rhs1)))
8876 while (mode != VOIDmode);
8877 if (mode == VOIDmode)
8881 /* It works, insert a truncation or sign-change before the
8882 float conversion. */
8883 tem = make_ssa_name (build_nonstandard_integer_type
8884 (GET_MODE_PRECISION (mode), 0), NULL);
8885 conv = gimple_build_assign_with_ops (NOP_EXPR, tem, rhs1, NULL_TREE);
8886 gsi_insert_before (gsi, conv, GSI_SAME_STMT);
8887 gimple_assign_set_rhs1 (stmt, tem);
8893 /* Simplify STMT using ranges if possible. */
8896 simplify_stmt_using_ranges (gimple_stmt_iterator *gsi)
8898 gimple stmt = gsi_stmt (*gsi);
8899 if (is_gimple_assign (stmt))
8901 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
8902 tree rhs1 = gimple_assign_rhs1 (stmt);
8908 /* Transform EQ_EXPR, NE_EXPR into BIT_XOR_EXPR or identity
8909 if the RHS is zero or one, and the LHS are known to be boolean
8911 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
8912 return simplify_truth_ops_using_ranges (gsi, stmt);
8915 /* Transform TRUNC_DIV_EXPR and TRUNC_MOD_EXPR into RSHIFT_EXPR
8916 and BIT_AND_EXPR respectively if the first operand is greater
8917 than zero and the second operand is an exact power of two. */
8918 case TRUNC_DIV_EXPR:
8919 case TRUNC_MOD_EXPR:
8920 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1))
8921 && integer_pow2p (gimple_assign_rhs2 (stmt)))
8922 return simplify_div_or_mod_using_ranges (stmt);
8925 /* Transform ABS (X) into X or -X as appropriate. */
8927 if (TREE_CODE (rhs1) == SSA_NAME
8928 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
8929 return simplify_abs_using_ranges (stmt);
8934 /* Optimize away BIT_AND_EXPR and BIT_IOR_EXPR
8935 if all the bits being cleared are already cleared or
8936 all the bits being set are already set. */
8937 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
8938 return simplify_bit_ops_using_ranges (gsi, stmt);
8942 if (TREE_CODE (rhs1) == SSA_NAME
8943 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
8944 return simplify_conversion_using_ranges (stmt);
8948 if (TREE_CODE (rhs1) == SSA_NAME
8949 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
8950 return simplify_float_conversion_using_ranges (gsi, stmt);
8957 else if (gimple_code (stmt) == GIMPLE_COND)
8958 return simplify_cond_using_ranges (stmt);
8959 else if (gimple_code (stmt) == GIMPLE_SWITCH)
8960 return simplify_switch_using_ranges (stmt);
8965 /* If the statement pointed by SI has a predicate whose value can be
8966 computed using the value range information computed by VRP, compute
8967 its value and return true. Otherwise, return false. */
8970 fold_predicate_in (gimple_stmt_iterator *si)
8972 bool assignment_p = false;
8974 gimple stmt = gsi_stmt (*si);
8976 if (is_gimple_assign (stmt)
8977 && TREE_CODE_CLASS (gimple_assign_rhs_code (stmt)) == tcc_comparison)
8979 assignment_p = true;
8980 val = vrp_evaluate_conditional (gimple_assign_rhs_code (stmt),
8981 gimple_assign_rhs1 (stmt),
8982 gimple_assign_rhs2 (stmt),
8985 else if (gimple_code (stmt) == GIMPLE_COND)
8986 val = vrp_evaluate_conditional (gimple_cond_code (stmt),
8987 gimple_cond_lhs (stmt),
8988 gimple_cond_rhs (stmt),
8996 val = fold_convert (gimple_expr_type (stmt), val);
9000 fprintf (dump_file, "Folding predicate ");
9001 print_gimple_expr (dump_file, stmt, 0, 0);
9002 fprintf (dump_file, " to ");
9003 print_generic_expr (dump_file, val, 0);
9004 fprintf (dump_file, "\n");
9007 if (is_gimple_assign (stmt))
9008 gimple_assign_set_rhs_from_tree (si, val);
9011 gcc_assert (gimple_code (stmt) == GIMPLE_COND);
9012 if (integer_zerop (val))
9013 gimple_cond_make_false (stmt);
9014 else if (integer_onep (val))
9015 gimple_cond_make_true (stmt);
9026 /* Callback for substitute_and_fold folding the stmt at *SI. */
9029 vrp_fold_stmt (gimple_stmt_iterator *si)
9031 if (fold_predicate_in (si))
9034 return simplify_stmt_using_ranges (si);
9037 /* Stack of dest,src equivalency pairs that need to be restored after
9038 each attempt to thread a block's incoming edge to an outgoing edge.
9040 A NULL entry is used to mark the end of pairs which need to be
9042 static vec<tree> equiv_stack;
9044 /* A trivial wrapper so that we can present the generic jump threading
9045 code with a simple API for simplifying statements. STMT is the
9046 statement we want to simplify, WITHIN_STMT provides the location
9047 for any overflow warnings. */
9050 simplify_stmt_for_jump_threading (gimple stmt, gimple within_stmt)
9052 /* We only use VRP information to simplify conditionals. This is
9053 overly conservative, but it's unclear if doing more would be
9054 worth the compile time cost. */
9055 if (gimple_code (stmt) != GIMPLE_COND)
9058 return vrp_evaluate_conditional (gimple_cond_code (stmt),
9059 gimple_cond_lhs (stmt),
9060 gimple_cond_rhs (stmt), within_stmt);
9063 /* Blocks which have more than one predecessor and more than
9064 one successor present jump threading opportunities, i.e.,
9065 when the block is reached from a specific predecessor, we
9066 may be able to determine which of the outgoing edges will
9067 be traversed. When this optimization applies, we are able
9068 to avoid conditionals at runtime and we may expose secondary
9069 optimization opportunities.
9071 This routine is effectively a driver for the generic jump
9072 threading code. It basically just presents the generic code
9073 with edges that may be suitable for jump threading.
9075 Unlike DOM, we do not iterate VRP if jump threading was successful.
9076 While iterating may expose new opportunities for VRP, it is expected
9077 those opportunities would be very limited and the compile time cost
9078 to expose those opportunities would be significant.
9080 As jump threading opportunities are discovered, they are registered
9081 for later realization. */
9084 identify_jump_threads (void)
9091 /* Ugh. When substituting values earlier in this pass we can
9092 wipe the dominance information. So rebuild the dominator
9093 information as we need it within the jump threading code. */
9094 calculate_dominance_info (CDI_DOMINATORS);
9096 /* We do not allow VRP information to be used for jump threading
9097 across a back edge in the CFG. Otherwise it becomes too
9098 difficult to avoid eliminating loop exit tests. Of course
9099 EDGE_DFS_BACK is not accurate at this time so we have to
9101 mark_dfs_back_edges ();
9103 /* Do not thread across edges we are about to remove. Just marking
9104 them as EDGE_DFS_BACK will do. */
9105 FOR_EACH_VEC_ELT (to_remove_edges, i, e)
9106 e->flags |= EDGE_DFS_BACK;
9108 /* Allocate our unwinder stack to unwind any temporary equivalences
9109 that might be recorded. */
9110 equiv_stack.create (20);
9112 /* To avoid lots of silly node creation, we create a single
9113 conditional and just modify it in-place when attempting to
9115 dummy = gimple_build_cond (EQ_EXPR,
9116 integer_zero_node, integer_zero_node,
9119 /* Walk through all the blocks finding those which present a
9120 potential jump threading opportunity. We could set this up
9121 as a dominator walker and record data during the walk, but
9122 I doubt it's worth the effort for the classes of jump
9123 threading opportunities we are trying to identify at this
9124 point in compilation. */
9129 /* If the generic jump threading code does not find this block
9130 interesting, then there is nothing to do. */
9131 if (! potentially_threadable_block (bb))
9134 /* We only care about blocks ending in a COND_EXPR. While there
9135 may be some value in handling SWITCH_EXPR here, I doubt it's
9136 terribly important. */
9137 last = gsi_stmt (gsi_last_bb (bb));
9139 /* We're basically looking for a switch or any kind of conditional with
9140 integral or pointer type arguments. Note the type of the second
9141 argument will be the same as the first argument, so no need to
9142 check it explicitly. */
9143 if (gimple_code (last) == GIMPLE_SWITCH
9144 || (gimple_code (last) == GIMPLE_COND
9145 && TREE_CODE (gimple_cond_lhs (last)) == SSA_NAME
9146 && (INTEGRAL_TYPE_P (TREE_TYPE (gimple_cond_lhs (last)))
9147 || POINTER_TYPE_P (TREE_TYPE (gimple_cond_lhs (last))))
9148 && (TREE_CODE (gimple_cond_rhs (last)) == SSA_NAME
9149 || is_gimple_min_invariant (gimple_cond_rhs (last)))))
9153 /* We've got a block with multiple predecessors and multiple
9154 successors which also ends in a suitable conditional or
9155 switch statement. For each predecessor, see if we can thread
9156 it to a specific successor. */
9157 FOR_EACH_EDGE (e, ei, bb->preds)
9159 /* Do not thread across back edges or abnormal edges
9161 if (e->flags & (EDGE_DFS_BACK | EDGE_COMPLEX))
9164 thread_across_edge (dummy, e, true, &equiv_stack,
9165 simplify_stmt_for_jump_threading);
9170 /* We do not actually update the CFG or SSA graphs at this point as
9171 ASSERT_EXPRs are still in the IL and cfg cleanup code does not yet
9172 handle ASSERT_EXPRs gracefully. */
9175 /* We identified all the jump threading opportunities earlier, but could
9176 not transform the CFG at that time. This routine transforms the
9177 CFG and arranges for the dominator tree to be rebuilt if necessary.
9179 Note the SSA graph update will occur during the normal TODO
9180 processing by the pass manager. */
9182 finalize_jump_threads (void)
9184 thread_through_all_blocks (false);
9185 equiv_stack.release ();
9189 /* Traverse all the blocks folding conditionals with known ranges. */
9196 values_propagated = true;
9200 fprintf (dump_file, "\nValue ranges after VRP:\n\n");
9201 dump_all_value_ranges (dump_file);
9202 fprintf (dump_file, "\n");
9205 substitute_and_fold (op_with_constant_singleton_value_range,
9206 vrp_fold_stmt, false);
9208 if (warn_array_bounds)
9209 check_all_array_refs ();
9211 /* We must identify jump threading opportunities before we release
9212 the datastructures built by VRP. */
9213 identify_jump_threads ();
9215 /* Free allocated memory. */
9216 for (i = 0; i < num_vr_values; i++)
9219 BITMAP_FREE (vr_value[i]->equiv);
9224 free (vr_phi_edge_counts);
9226 /* So that we can distinguish between VRP data being available
9227 and not available. */
9229 vr_phi_edge_counts = NULL;
9233 /* Main entry point to VRP (Value Range Propagation). This pass is
9234 loosely based on J. R. C. Patterson, ``Accurate Static Branch
9235 Prediction by Value Range Propagation,'' in SIGPLAN Conference on
9236 Programming Language Design and Implementation, pp. 67-78, 1995.
9237 Also available at http://citeseer.ist.psu.edu/patterson95accurate.html
9239 This is essentially an SSA-CCP pass modified to deal with ranges
9240 instead of constants.
9242 While propagating ranges, we may find that two or more SSA name
9243 have equivalent, though distinct ranges. For instance,
9246 2 p_4 = ASSERT_EXPR <p_3, p_3 != 0>
9248 4 p_5 = ASSERT_EXPR <p_4, p_4 == q_2>;
9252 In the code above, pointer p_5 has range [q_2, q_2], but from the
9253 code we can also determine that p_5 cannot be NULL and, if q_2 had
9254 a non-varying range, p_5's range should also be compatible with it.
9256 These equivalences are created by two expressions: ASSERT_EXPR and
9257 copy operations. Since p_5 is an assertion on p_4, and p_4 was the
9258 result of another assertion, then we can use the fact that p_5 and
9259 p_4 are equivalent when evaluating p_5's range.
9261 Together with value ranges, we also propagate these equivalences
9262 between names so that we can take advantage of information from
9263 multiple ranges when doing final replacement. Note that this
9264 equivalency relation is transitive but not symmetric.
9266 In the example above, p_5 is equivalent to p_4, q_2 and p_3, but we
9267 cannot assert that q_2 is equivalent to p_5 because q_2 may be used
9268 in contexts where that assertion does not hold (e.g., in line 6).
9270 TODO, the main difference between this pass and Patterson's is that
9271 we do not propagate edge probabilities. We only compute whether
9272 edges can be taken or not. That is, instead of having a spectrum
9273 of jump probabilities between 0 and 1, we only deal with 0, 1 and
9274 DON'T KNOW. In the future, it may be worthwhile to propagate
9275 probabilities to aid branch prediction. */
9284 loop_optimizer_init (LOOPS_NORMAL | LOOPS_HAVE_RECORDED_EXITS);
9285 rewrite_into_loop_closed_ssa (NULL, TODO_update_ssa);
9288 /* ??? This ends up using stale EDGE_DFS_BACK for liveness computation.
9289 Inserting assertions may split edges which will invalidate
9291 insert_range_assertions ();
9293 to_remove_edges.create (10);
9294 to_update_switch_stmts.create (5);
9295 threadedge_initialize_values ();
9297 /* For visiting PHI nodes we need EDGE_DFS_BACK computed. */
9298 mark_dfs_back_edges ();
9301 ssa_propagate (vrp_visit_stmt, vrp_visit_phi_node);
9304 free_numbers_of_iterations_estimates ();
9306 /* ASSERT_EXPRs must be removed before finalizing jump threads
9307 as finalizing jump threads calls the CFG cleanup code which
9308 does not properly handle ASSERT_EXPRs. */
9309 remove_range_assertions ();
9311 /* If we exposed any new variables, go ahead and put them into
9312 SSA form now, before we handle jump threading. This simplifies
9313 interactions between rewriting of _DECL nodes into SSA form
9314 and rewriting SSA_NAME nodes into SSA form after block
9315 duplication and CFG manipulation. */
9316 update_ssa (TODO_update_ssa);
9318 finalize_jump_threads ();
9320 /* Remove dead edges from SWITCH_EXPR optimization. This leaves the
9321 CFG in a broken state and requires a cfg_cleanup run. */
9322 FOR_EACH_VEC_ELT (to_remove_edges, i, e)
9324 /* Update SWITCH_EXPR case label vector. */
9325 FOR_EACH_VEC_ELT (to_update_switch_stmts, i, su)
9328 size_t n = TREE_VEC_LENGTH (su->vec);
9330 gimple_switch_set_num_labels (su->stmt, n);
9331 for (j = 0; j < n; j++)
9332 gimple_switch_set_label (su->stmt, j, TREE_VEC_ELT (su->vec, j));
9333 /* As we may have replaced the default label with a regular one
9334 make sure to make it a real default label again. This ensures
9335 optimal expansion. */
9336 label = gimple_switch_label (su->stmt, 0);
9337 CASE_LOW (label) = NULL_TREE;
9338 CASE_HIGH (label) = NULL_TREE;
9341 if (to_remove_edges.length () > 0)
9342 free_dominance_info (CDI_DOMINATORS);
9344 to_remove_edges.release ();
9345 to_update_switch_stmts.release ();
9346 threadedge_finalize_values ();
9349 loop_optimizer_finalize ();
9356 return flag_tree_vrp != 0;
9359 struct gimple_opt_pass pass_vrp =
9364 OPTGROUP_NONE, /* optinfo_flags */
9365 gate_vrp, /* gate */
9366 execute_vrp, /* execute */
9369 0, /* static_pass_number */
9370 TV_TREE_VRP, /* tv_id */
9371 PROP_ssa, /* properties_required */
9372 0, /* properties_provided */
9373 0, /* properties_destroyed */
9374 0, /* todo_flags_start */
9379 | TODO_ggc_collect /* todo_flags_finish */