1 /* Support routines for Value Range Propagation (VRP).
2 Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
3 Free Software Foundation, Inc.
4 Contributed by Diego Novillo <dnovillo@redhat.com>.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
24 #include "coretypes.h"
29 #include "basic-block.h"
30 #include "tree-flow.h"
31 #include "tree-pass.h"
32 #include "tree-dump.h"
33 #include "gimple-pretty-print.h"
34 #include "diagnostic-core.h"
37 #include "tree-scalar-evolution.h"
38 #include "tree-ssa-propagate.h"
39 #include "tree-chrec.h"
40 #include "gimple-fold.h"
45 /* Type of value ranges. See value_range_d for a description of these
47 enum value_range_type
{ VR_UNDEFINED
, VR_RANGE
, VR_ANTI_RANGE
, VR_VARYING
};
49 /* Range of values that can be associated with an SSA_NAME after VRP
53 /* Lattice value represented by this range. */
54 enum value_range_type type
;
56 /* Minimum and maximum values represented by this range. These
57 values should be interpreted as follows:
59 - If TYPE is VR_UNDEFINED or VR_VARYING then MIN and MAX must
62 - If TYPE == VR_RANGE then MIN holds the minimum value and
63 MAX holds the maximum value of the range [MIN, MAX].
65 - If TYPE == ANTI_RANGE the variable is known to NOT
66 take any values in the range [MIN, MAX]. */
70 /* Set of SSA names whose value ranges are equivalent to this one.
71 This set is only valid when TYPE is VR_RANGE or VR_ANTI_RANGE. */
75 typedef struct value_range_d value_range_t
;
77 #define VR_INITIALIZER { VR_UNDEFINED, NULL_TREE, NULL_TREE, NULL }
79 /* Set of SSA names found live during the RPO traversal of the function
80 for still active basic-blocks. */
83 /* Return true if the SSA name NAME is live on the edge E. */
86 live_on_edge (edge e
, tree name
)
88 return (live
[e
->dest
->index
]
89 && TEST_BIT (live
[e
->dest
->index
], SSA_NAME_VERSION (name
)));
92 /* Local functions. */
93 static int compare_values (tree val1
, tree val2
);
94 static int compare_values_warnv (tree val1
, tree val2
, bool *);
95 static void vrp_meet (value_range_t
*, value_range_t
*);
96 static void vrp_intersect_ranges (value_range_t
*, value_range_t
*);
97 static tree
vrp_evaluate_conditional_warnv_with_ops (enum tree_code
,
98 tree
, tree
, bool, bool *,
101 /* Location information for ASSERT_EXPRs. Each instance of this
102 structure describes an ASSERT_EXPR for an SSA name. Since a single
103 SSA name may have more than one assertion associated with it, these
104 locations are kept in a linked list attached to the corresponding
106 struct assert_locus_d
108 /* Basic block where the assertion would be inserted. */
111 /* Some assertions need to be inserted on an edge (e.g., assertions
112 generated by COND_EXPRs). In those cases, BB will be NULL. */
115 /* Pointer to the statement that generated this assertion. */
116 gimple_stmt_iterator si
;
118 /* Predicate code for the ASSERT_EXPR. Must be COMPARISON_CLASS_P. */
119 enum tree_code comp_code
;
121 /* Value being compared against. */
124 /* Expression to compare. */
127 /* Next node in the linked list. */
128 struct assert_locus_d
*next
;
131 typedef struct assert_locus_d
*assert_locus_t
;
133 /* If bit I is present, it means that SSA name N_i has a list of
134 assertions that should be inserted in the IL. */
135 static bitmap need_assert_for
;
137 /* Array of locations lists where to insert assertions. ASSERTS_FOR[I]
138 holds a list of ASSERT_LOCUS_T nodes that describe where
139 ASSERT_EXPRs for SSA name N_I should be inserted. */
140 static assert_locus_t
*asserts_for
;
142 /* Value range array. After propagation, VR_VALUE[I] holds the range
143 of values that SSA name N_I may take. */
144 static unsigned num_vr_values
;
145 static value_range_t
**vr_value
;
146 static bool values_propagated
;
148 /* For a PHI node which sets SSA name N_I, VR_COUNTS[I] holds the
149 number of executable edges we saw the last time we visited the
151 static int *vr_phi_edge_counts
;
158 static VEC (edge
, heap
) *to_remove_edges
;
159 DEF_VEC_O(switch_update
);
160 DEF_VEC_ALLOC_O(switch_update
, heap
);
161 static VEC (switch_update
, heap
) *to_update_switch_stmts
;
164 /* Return the maximum value for TYPE. */
167 vrp_val_max (const_tree type
)
169 if (!INTEGRAL_TYPE_P (type
))
172 return TYPE_MAX_VALUE (type
);
175 /* Return the minimum value for TYPE. */
178 vrp_val_min (const_tree type
)
180 if (!INTEGRAL_TYPE_P (type
))
183 return TYPE_MIN_VALUE (type
);
186 /* Return whether VAL is equal to the maximum value of its type. This
187 will be true for a positive overflow infinity. We can't do a
188 simple equality comparison with TYPE_MAX_VALUE because C typedefs
189 and Ada subtypes can produce types whose TYPE_MAX_VALUE is not ==
190 to the integer constant with the same value in the type. */
193 vrp_val_is_max (const_tree val
)
195 tree type_max
= vrp_val_max (TREE_TYPE (val
));
196 return (val
== type_max
197 || (type_max
!= NULL_TREE
198 && operand_equal_p (val
, type_max
, 0)));
201 /* Return whether VAL is equal to the minimum value of its type. This
202 will be true for a negative overflow infinity. */
205 vrp_val_is_min (const_tree val
)
207 tree type_min
= vrp_val_min (TREE_TYPE (val
));
208 return (val
== type_min
209 || (type_min
!= NULL_TREE
210 && operand_equal_p (val
, type_min
, 0)));
214 /* Return whether TYPE should use an overflow infinity distinct from
215 TYPE_{MIN,MAX}_VALUE. We use an overflow infinity value to
216 represent a signed overflow during VRP computations. An infinity
217 is distinct from a half-range, which will go from some number to
218 TYPE_{MIN,MAX}_VALUE. */
221 needs_overflow_infinity (const_tree type
)
223 return INTEGRAL_TYPE_P (type
) && !TYPE_OVERFLOW_WRAPS (type
);
226 /* Return whether TYPE can support our overflow infinity
227 representation: we use the TREE_OVERFLOW flag, which only exists
228 for constants. If TYPE doesn't support this, we don't optimize
229 cases which would require signed overflow--we drop them to
233 supports_overflow_infinity (const_tree type
)
235 tree min
= vrp_val_min (type
), max
= vrp_val_max (type
);
236 #ifdef ENABLE_CHECKING
237 gcc_assert (needs_overflow_infinity (type
));
239 return (min
!= NULL_TREE
240 && CONSTANT_CLASS_P (min
)
242 && CONSTANT_CLASS_P (max
));
245 /* VAL is the maximum or minimum value of a type. Return a
246 corresponding overflow infinity. */
249 make_overflow_infinity (tree val
)
251 gcc_checking_assert (val
!= NULL_TREE
&& CONSTANT_CLASS_P (val
));
252 val
= copy_node (val
);
253 TREE_OVERFLOW (val
) = 1;
257 /* Return a negative overflow infinity for TYPE. */
260 negative_overflow_infinity (tree type
)
262 gcc_checking_assert (supports_overflow_infinity (type
));
263 return make_overflow_infinity (vrp_val_min (type
));
266 /* Return a positive overflow infinity for TYPE. */
269 positive_overflow_infinity (tree type
)
271 gcc_checking_assert (supports_overflow_infinity (type
));
272 return make_overflow_infinity (vrp_val_max (type
));
275 /* Return whether VAL is a negative overflow infinity. */
278 is_negative_overflow_infinity (const_tree val
)
280 return (needs_overflow_infinity (TREE_TYPE (val
))
281 && CONSTANT_CLASS_P (val
)
282 && TREE_OVERFLOW (val
)
283 && vrp_val_is_min (val
));
286 /* Return whether VAL is a positive overflow infinity. */
289 is_positive_overflow_infinity (const_tree val
)
291 return (needs_overflow_infinity (TREE_TYPE (val
))
292 && CONSTANT_CLASS_P (val
)
293 && TREE_OVERFLOW (val
)
294 && vrp_val_is_max (val
));
297 /* Return whether VAL is a positive or negative overflow infinity. */
300 is_overflow_infinity (const_tree val
)
302 return (needs_overflow_infinity (TREE_TYPE (val
))
303 && CONSTANT_CLASS_P (val
)
304 && TREE_OVERFLOW (val
)
305 && (vrp_val_is_min (val
) || vrp_val_is_max (val
)));
308 /* Return whether STMT has a constant rhs that is_overflow_infinity. */
311 stmt_overflow_infinity (gimple stmt
)
313 if (is_gimple_assign (stmt
)
314 && get_gimple_rhs_class (gimple_assign_rhs_code (stmt
)) ==
316 return is_overflow_infinity (gimple_assign_rhs1 (stmt
));
320 /* If VAL is now an overflow infinity, return VAL. Otherwise, return
321 the same value with TREE_OVERFLOW clear. This can be used to avoid
322 confusing a regular value with an overflow value. */
325 avoid_overflow_infinity (tree val
)
327 if (!is_overflow_infinity (val
))
330 if (vrp_val_is_max (val
))
331 return vrp_val_max (TREE_TYPE (val
));
334 gcc_checking_assert (vrp_val_is_min (val
));
335 return vrp_val_min (TREE_TYPE (val
));
340 /* Return true if ARG is marked with the nonnull attribute in the
341 current function signature. */
344 nonnull_arg_p (const_tree arg
)
346 tree t
, attrs
, fntype
;
347 unsigned HOST_WIDE_INT arg_num
;
349 gcc_assert (TREE_CODE (arg
) == PARM_DECL
&& POINTER_TYPE_P (TREE_TYPE (arg
)));
351 /* The static chain decl is always non null. */
352 if (arg
== cfun
->static_chain_decl
)
355 fntype
= TREE_TYPE (current_function_decl
);
356 for (attrs
= TYPE_ATTRIBUTES (fntype
); attrs
; attrs
= TREE_CHAIN (attrs
))
358 attrs
= lookup_attribute ("nonnull", attrs
);
360 /* If "nonnull" wasn't specified, we know nothing about the argument. */
361 if (attrs
== NULL_TREE
)
364 /* If "nonnull" applies to all the arguments, then ARG is non-null. */
365 if (TREE_VALUE (attrs
) == NULL_TREE
)
368 /* Get the position number for ARG in the function signature. */
369 for (arg_num
= 1, t
= DECL_ARGUMENTS (current_function_decl
);
371 t
= DECL_CHAIN (t
), arg_num
++)
377 gcc_assert (t
== arg
);
379 /* Now see if ARG_NUM is mentioned in the nonnull list. */
380 for (t
= TREE_VALUE (attrs
); t
; t
= TREE_CHAIN (t
))
382 if (compare_tree_int (TREE_VALUE (t
), arg_num
) == 0)
391 /* Set value range VR to VR_UNDEFINED. */
394 set_value_range_to_undefined (value_range_t
*vr
)
396 vr
->type
= VR_UNDEFINED
;
397 vr
->min
= vr
->max
= NULL_TREE
;
399 bitmap_clear (vr
->equiv
);
403 /* Set value range VR to VR_VARYING. */
406 set_value_range_to_varying (value_range_t
*vr
)
408 vr
->type
= VR_VARYING
;
409 vr
->min
= vr
->max
= NULL_TREE
;
411 bitmap_clear (vr
->equiv
);
415 /* Set value range VR to {T, MIN, MAX, EQUIV}. */
418 set_value_range (value_range_t
*vr
, enum value_range_type t
, tree min
,
419 tree max
, bitmap equiv
)
421 #if defined ENABLE_CHECKING
422 /* Check the validity of the range. */
423 if (t
== VR_RANGE
|| t
== VR_ANTI_RANGE
)
427 gcc_assert (min
&& max
);
429 if (INTEGRAL_TYPE_P (TREE_TYPE (min
)) && t
== VR_ANTI_RANGE
)
430 gcc_assert (!vrp_val_is_min (min
) || !vrp_val_is_max (max
));
432 cmp
= compare_values (min
, max
);
433 gcc_assert (cmp
== 0 || cmp
== -1 || cmp
== -2);
435 if (needs_overflow_infinity (TREE_TYPE (min
)))
436 gcc_assert (!is_overflow_infinity (min
)
437 || !is_overflow_infinity (max
));
440 if (t
== VR_UNDEFINED
|| t
== VR_VARYING
)
441 gcc_assert (min
== NULL_TREE
&& max
== NULL_TREE
);
443 if (t
== VR_UNDEFINED
|| t
== VR_VARYING
)
444 gcc_assert (equiv
== NULL
|| bitmap_empty_p (equiv
));
451 /* Since updating the equivalence set involves deep copying the
452 bitmaps, only do it if absolutely necessary. */
453 if (vr
->equiv
== NULL
455 vr
->equiv
= BITMAP_ALLOC (NULL
);
457 if (equiv
!= vr
->equiv
)
459 if (equiv
&& !bitmap_empty_p (equiv
))
460 bitmap_copy (vr
->equiv
, equiv
);
462 bitmap_clear (vr
->equiv
);
467 /* Set value range VR to the canonical form of {T, MIN, MAX, EQUIV}.
468 This means adjusting T, MIN and MAX representing the case of a
469 wrapping range with MAX < MIN covering [MIN, type_max] U [type_min, MAX]
470 as anti-rage ~[MAX+1, MIN-1]. Likewise for wrapping anti-ranges.
471 In corner cases where MAX+1 or MIN-1 wraps this will fall back
473 This routine exists to ease canonicalization in the case where we
474 extract ranges from var + CST op limit. */
477 set_and_canonicalize_value_range (value_range_t
*vr
, enum value_range_type t
,
478 tree min
, tree max
, bitmap equiv
)
480 /* Use the canonical setters for VR_UNDEFINED and VR_VARYING. */
481 if (t
== VR_UNDEFINED
)
483 set_value_range_to_undefined (vr
);
486 else if (t
== VR_VARYING
)
488 set_value_range_to_varying (vr
);
492 /* Nothing to canonicalize for symbolic ranges. */
493 if (TREE_CODE (min
) != INTEGER_CST
494 || TREE_CODE (max
) != INTEGER_CST
)
496 set_value_range (vr
, t
, min
, max
, equiv
);
500 /* Wrong order for min and max, to swap them and the VR type we need
502 if (tree_int_cst_lt (max
, min
))
506 /* For one bit precision if max < min, then the swapped
507 range covers all values, so for VR_RANGE it is varying and
508 for VR_ANTI_RANGE empty range, so drop to varying as well. */
509 if (TYPE_PRECISION (TREE_TYPE (min
)) == 1)
511 set_value_range_to_varying (vr
);
515 one
= build_int_cst (TREE_TYPE (min
), 1);
516 tmp
= int_const_binop (PLUS_EXPR
, max
, one
);
517 max
= int_const_binop (MINUS_EXPR
, min
, one
);
520 /* There's one corner case, if we had [C+1, C] before we now have
521 that again. But this represents an empty value range, so drop
522 to varying in this case. */
523 if (tree_int_cst_lt (max
, min
))
525 set_value_range_to_varying (vr
);
529 t
= t
== VR_RANGE
? VR_ANTI_RANGE
: VR_RANGE
;
532 /* Anti-ranges that can be represented as ranges should be so. */
533 if (t
== VR_ANTI_RANGE
)
535 bool is_min
= vrp_val_is_min (min
);
536 bool is_max
= vrp_val_is_max (max
);
538 if (is_min
&& is_max
)
540 /* We cannot deal with empty ranges, drop to varying.
541 ??? This could be VR_UNDEFINED instead. */
542 set_value_range_to_varying (vr
);
545 else if (TYPE_PRECISION (TREE_TYPE (min
)) == 1
546 && !TYPE_UNSIGNED (TREE_TYPE (min
))
547 && (is_min
|| is_max
))
549 /* For signed 1-bit precision, one is not in-range and
550 thus adding/subtracting it would result in overflows. */
551 if (operand_equal_p (min
, max
, 0))
553 min
= max
= is_min
? vrp_val_max (TREE_TYPE (min
))
554 : vrp_val_min (TREE_TYPE (min
));
559 set_value_range_to_varying (vr
);
564 /* As a special exception preserve non-null ranges. */
565 && !(TYPE_UNSIGNED (TREE_TYPE (min
))
566 && integer_zerop (max
)))
568 tree one
= build_int_cst (TREE_TYPE (max
), 1);
569 min
= int_const_binop (PLUS_EXPR
, max
, one
);
570 max
= vrp_val_max (TREE_TYPE (max
));
575 tree one
= build_int_cst (TREE_TYPE (min
), 1);
576 max
= int_const_binop (MINUS_EXPR
, min
, one
);
577 min
= vrp_val_min (TREE_TYPE (min
));
582 /* Drop [-INF(OVF), +INF(OVF)] to varying. */
583 if (needs_overflow_infinity (TREE_TYPE (min
))
584 && is_overflow_infinity (min
)
585 && is_overflow_infinity (max
))
587 set_value_range_to_varying (vr
);
591 set_value_range (vr
, t
, min
, max
, equiv
);
594 /* Copy value range FROM into value range TO. */
597 copy_value_range (value_range_t
*to
, value_range_t
*from
)
599 set_value_range (to
, from
->type
, from
->min
, from
->max
, from
->equiv
);
602 /* Set value range VR to a single value. This function is only called
603 with values we get from statements, and exists to clear the
604 TREE_OVERFLOW flag so that we don't think we have an overflow
605 infinity when we shouldn't. */
608 set_value_range_to_value (value_range_t
*vr
, tree val
, bitmap equiv
)
610 gcc_assert (is_gimple_min_invariant (val
));
611 val
= avoid_overflow_infinity (val
);
612 set_value_range (vr
, VR_RANGE
, val
, val
, equiv
);
615 /* Set value range VR to a non-negative range of type TYPE.
616 OVERFLOW_INFINITY indicates whether to use an overflow infinity
617 rather than TYPE_MAX_VALUE; this should be true if we determine
618 that the range is nonnegative based on the assumption that signed
619 overflow does not occur. */
622 set_value_range_to_nonnegative (value_range_t
*vr
, tree type
,
623 bool overflow_infinity
)
627 if (overflow_infinity
&& !supports_overflow_infinity (type
))
629 set_value_range_to_varying (vr
);
633 zero
= build_int_cst (type
, 0);
634 set_value_range (vr
, VR_RANGE
, zero
,
636 ? positive_overflow_infinity (type
)
637 : TYPE_MAX_VALUE (type
)),
641 /* Set value range VR to a non-NULL range of type TYPE. */
644 set_value_range_to_nonnull (value_range_t
*vr
, tree type
)
646 tree zero
= build_int_cst (type
, 0);
647 set_value_range (vr
, VR_ANTI_RANGE
, zero
, zero
, vr
->equiv
);
651 /* Set value range VR to a NULL range of type TYPE. */
654 set_value_range_to_null (value_range_t
*vr
, tree type
)
656 set_value_range_to_value (vr
, build_int_cst (type
, 0), vr
->equiv
);
660 /* Set value range VR to a range of a truthvalue of type TYPE. */
663 set_value_range_to_truthvalue (value_range_t
*vr
, tree type
)
665 if (TYPE_PRECISION (type
) == 1)
666 set_value_range_to_varying (vr
);
668 set_value_range (vr
, VR_RANGE
,
669 build_int_cst (type
, 0), build_int_cst (type
, 1),
674 /* If abs (min) < abs (max), set VR to [-max, max], if
675 abs (min) >= abs (max), set VR to [-min, min]. */
678 abs_extent_range (value_range_t
*vr
, tree min
, tree max
)
682 gcc_assert (TREE_CODE (min
) == INTEGER_CST
);
683 gcc_assert (TREE_CODE (max
) == INTEGER_CST
);
684 gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (min
)));
685 gcc_assert (!TYPE_UNSIGNED (TREE_TYPE (min
)));
686 min
= fold_unary (ABS_EXPR
, TREE_TYPE (min
), min
);
687 max
= fold_unary (ABS_EXPR
, TREE_TYPE (max
), max
);
688 if (TREE_OVERFLOW (min
) || TREE_OVERFLOW (max
))
690 set_value_range_to_varying (vr
);
693 cmp
= compare_values (min
, max
);
695 min
= fold_unary (NEGATE_EXPR
, TREE_TYPE (min
), max
);
696 else if (cmp
== 0 || cmp
== 1)
699 min
= fold_unary (NEGATE_EXPR
, TREE_TYPE (min
), min
);
703 set_value_range_to_varying (vr
);
706 set_and_canonicalize_value_range (vr
, VR_RANGE
, min
, max
, NULL
);
710 /* Return value range information for VAR.
712 If we have no values ranges recorded (ie, VRP is not running), then
713 return NULL. Otherwise create an empty range if none existed for VAR. */
715 static value_range_t
*
716 get_value_range (const_tree var
)
718 static const struct value_range_d vr_const_varying
719 = { VR_VARYING
, NULL_TREE
, NULL_TREE
, NULL
};
722 unsigned ver
= SSA_NAME_VERSION (var
);
724 /* If we have no recorded ranges, then return NULL. */
728 /* If we query the range for a new SSA name return an unmodifiable VARYING.
729 We should get here at most from the substitute-and-fold stage which
730 will never try to change values. */
731 if (ver
>= num_vr_values
)
732 return CONST_CAST (value_range_t
*, &vr_const_varying
);
738 /* After propagation finished do not allocate new value-ranges. */
739 if (values_propagated
)
740 return CONST_CAST (value_range_t
*, &vr_const_varying
);
742 /* Create a default value range. */
743 vr_value
[ver
] = vr
= XCNEW (value_range_t
);
745 /* Defer allocating the equivalence set. */
748 /* If VAR is a default definition of a parameter, the variable can
749 take any value in VAR's type. */
750 if (SSA_NAME_IS_DEFAULT_DEF (var
))
752 sym
= SSA_NAME_VAR (var
);
753 if (TREE_CODE (sym
) == PARM_DECL
)
755 /* Try to use the "nonnull" attribute to create ~[0, 0]
756 anti-ranges for pointers. Note that this is only valid with
757 default definitions of PARM_DECLs. */
758 if (POINTER_TYPE_P (TREE_TYPE (sym
))
759 && nonnull_arg_p (sym
))
760 set_value_range_to_nonnull (vr
, TREE_TYPE (sym
));
762 set_value_range_to_varying (vr
);
764 else if (TREE_CODE (sym
) == RESULT_DECL
765 && DECL_BY_REFERENCE (sym
))
766 set_value_range_to_nonnull (vr
, TREE_TYPE (sym
));
772 /* Return true, if VAL1 and VAL2 are equal values for VRP purposes. */
775 vrp_operand_equal_p (const_tree val1
, const_tree val2
)
779 if (!val1
|| !val2
|| !operand_equal_p (val1
, val2
, 0))
781 if (is_overflow_infinity (val1
))
782 return is_overflow_infinity (val2
);
786 /* Return true, if the bitmaps B1 and B2 are equal. */
789 vrp_bitmap_equal_p (const_bitmap b1
, const_bitmap b2
)
792 || ((!b1
|| bitmap_empty_p (b1
))
793 && (!b2
|| bitmap_empty_p (b2
)))
795 && bitmap_equal_p (b1
, b2
)));
798 /* Update the value range and equivalence set for variable VAR to
799 NEW_VR. Return true if NEW_VR is different from VAR's previous
802 NOTE: This function assumes that NEW_VR is a temporary value range
803 object created for the sole purpose of updating VAR's range. The
804 storage used by the equivalence set from NEW_VR will be freed by
805 this function. Do not call update_value_range when NEW_VR
806 is the range object associated with another SSA name. */
809 update_value_range (const_tree var
, value_range_t
*new_vr
)
811 value_range_t
*old_vr
;
814 /* Update the value range, if necessary. */
815 old_vr
= get_value_range (var
);
816 is_new
= old_vr
->type
!= new_vr
->type
817 || !vrp_operand_equal_p (old_vr
->min
, new_vr
->min
)
818 || !vrp_operand_equal_p (old_vr
->max
, new_vr
->max
)
819 || !vrp_bitmap_equal_p (old_vr
->equiv
, new_vr
->equiv
);
822 set_value_range (old_vr
, new_vr
->type
, new_vr
->min
, new_vr
->max
,
825 BITMAP_FREE (new_vr
->equiv
);
831 /* Add VAR and VAR's equivalence set to EQUIV. This is the central
832 point where equivalence processing can be turned on/off. */
835 add_equivalence (bitmap
*equiv
, const_tree var
)
837 unsigned ver
= SSA_NAME_VERSION (var
);
838 value_range_t
*vr
= vr_value
[ver
];
841 *equiv
= BITMAP_ALLOC (NULL
);
842 bitmap_set_bit (*equiv
, ver
);
844 bitmap_ior_into (*equiv
, vr
->equiv
);
848 /* Return true if VR is ~[0, 0]. */
851 range_is_nonnull (value_range_t
*vr
)
853 return vr
->type
== VR_ANTI_RANGE
854 && integer_zerop (vr
->min
)
855 && integer_zerop (vr
->max
);
859 /* Return true if VR is [0, 0]. */
862 range_is_null (value_range_t
*vr
)
864 return vr
->type
== VR_RANGE
865 && integer_zerop (vr
->min
)
866 && integer_zerop (vr
->max
);
869 /* Return true if max and min of VR are INTEGER_CST. It's not necessary
873 range_int_cst_p (value_range_t
*vr
)
875 return (vr
->type
== VR_RANGE
876 && TREE_CODE (vr
->max
) == INTEGER_CST
877 && TREE_CODE (vr
->min
) == INTEGER_CST
);
880 /* Return true if VR is a INTEGER_CST singleton. */
883 range_int_cst_singleton_p (value_range_t
*vr
)
885 return (range_int_cst_p (vr
)
886 && !TREE_OVERFLOW (vr
->min
)
887 && !TREE_OVERFLOW (vr
->max
)
888 && tree_int_cst_equal (vr
->min
, vr
->max
));
891 /* Return true if value range VR involves at least one symbol. */
894 symbolic_range_p (value_range_t
*vr
)
896 return (!is_gimple_min_invariant (vr
->min
)
897 || !is_gimple_min_invariant (vr
->max
));
900 /* Return true if value range VR uses an overflow infinity. */
903 overflow_infinity_range_p (value_range_t
*vr
)
905 return (vr
->type
== VR_RANGE
906 && (is_overflow_infinity (vr
->min
)
907 || is_overflow_infinity (vr
->max
)));
910 /* Return false if we can not make a valid comparison based on VR;
911 this will be the case if it uses an overflow infinity and overflow
912 is not undefined (i.e., -fno-strict-overflow is in effect).
913 Otherwise return true, and set *STRICT_OVERFLOW_P to true if VR
914 uses an overflow infinity. */
917 usable_range_p (value_range_t
*vr
, bool *strict_overflow_p
)
919 gcc_assert (vr
->type
== VR_RANGE
);
920 if (is_overflow_infinity (vr
->min
))
922 *strict_overflow_p
= true;
923 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (vr
->min
)))
926 if (is_overflow_infinity (vr
->max
))
928 *strict_overflow_p
= true;
929 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (vr
->max
)))
936 /* Return true if the result of assignment STMT is know to be non-negative.
937 If the return value is based on the assumption that signed overflow is
938 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
939 *STRICT_OVERFLOW_P.*/
942 gimple_assign_nonnegative_warnv_p (gimple stmt
, bool *strict_overflow_p
)
944 enum tree_code code
= gimple_assign_rhs_code (stmt
);
945 switch (get_gimple_rhs_class (code
))
947 case GIMPLE_UNARY_RHS
:
948 return tree_unary_nonnegative_warnv_p (gimple_assign_rhs_code (stmt
),
949 gimple_expr_type (stmt
),
950 gimple_assign_rhs1 (stmt
),
952 case GIMPLE_BINARY_RHS
:
953 return tree_binary_nonnegative_warnv_p (gimple_assign_rhs_code (stmt
),
954 gimple_expr_type (stmt
),
955 gimple_assign_rhs1 (stmt
),
956 gimple_assign_rhs2 (stmt
),
958 case GIMPLE_TERNARY_RHS
:
960 case GIMPLE_SINGLE_RHS
:
961 return tree_single_nonnegative_warnv_p (gimple_assign_rhs1 (stmt
),
963 case GIMPLE_INVALID_RHS
:
970 /* Return true if return value of call STMT is know to be non-negative.
971 If the return value is based on the assumption that signed overflow is
972 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
973 *STRICT_OVERFLOW_P.*/
976 gimple_call_nonnegative_warnv_p (gimple stmt
, bool *strict_overflow_p
)
978 tree arg0
= gimple_call_num_args (stmt
) > 0 ?
979 gimple_call_arg (stmt
, 0) : NULL_TREE
;
980 tree arg1
= gimple_call_num_args (stmt
) > 1 ?
981 gimple_call_arg (stmt
, 1) : NULL_TREE
;
983 return tree_call_nonnegative_warnv_p (gimple_expr_type (stmt
),
984 gimple_call_fndecl (stmt
),
990 /* Return true if STMT is know to to compute a non-negative value.
991 If the return value is based on the assumption that signed overflow is
992 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
993 *STRICT_OVERFLOW_P.*/
996 gimple_stmt_nonnegative_warnv_p (gimple stmt
, bool *strict_overflow_p
)
998 switch (gimple_code (stmt
))
1001 return gimple_assign_nonnegative_warnv_p (stmt
, strict_overflow_p
);
1003 return gimple_call_nonnegative_warnv_p (stmt
, strict_overflow_p
);
1009 /* Return true if the result of assignment STMT is know to be non-zero.
1010 If the return value is based on the assumption that signed overflow is
1011 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1012 *STRICT_OVERFLOW_P.*/
1015 gimple_assign_nonzero_warnv_p (gimple stmt
, bool *strict_overflow_p
)
1017 enum tree_code code
= gimple_assign_rhs_code (stmt
);
1018 switch (get_gimple_rhs_class (code
))
1020 case GIMPLE_UNARY_RHS
:
1021 return tree_unary_nonzero_warnv_p (gimple_assign_rhs_code (stmt
),
1022 gimple_expr_type (stmt
),
1023 gimple_assign_rhs1 (stmt
),
1025 case GIMPLE_BINARY_RHS
:
1026 return tree_binary_nonzero_warnv_p (gimple_assign_rhs_code (stmt
),
1027 gimple_expr_type (stmt
),
1028 gimple_assign_rhs1 (stmt
),
1029 gimple_assign_rhs2 (stmt
),
1031 case GIMPLE_TERNARY_RHS
:
1033 case GIMPLE_SINGLE_RHS
:
1034 return tree_single_nonzero_warnv_p (gimple_assign_rhs1 (stmt
),
1036 case GIMPLE_INVALID_RHS
:
1043 /* Return true if STMT is know to to compute a non-zero value.
1044 If the return value is based on the assumption that signed overflow is
1045 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1046 *STRICT_OVERFLOW_P.*/
1049 gimple_stmt_nonzero_warnv_p (gimple stmt
, bool *strict_overflow_p
)
1051 switch (gimple_code (stmt
))
1054 return gimple_assign_nonzero_warnv_p (stmt
, strict_overflow_p
);
1056 return gimple_alloca_call_p (stmt
);
1062 /* Like tree_expr_nonzero_warnv_p, but this function uses value ranges
1066 vrp_stmt_computes_nonzero (gimple stmt
, bool *strict_overflow_p
)
1068 if (gimple_stmt_nonzero_warnv_p (stmt
, strict_overflow_p
))
1071 /* If we have an expression of the form &X->a, then the expression
1072 is nonnull if X is nonnull. */
1073 if (is_gimple_assign (stmt
)
1074 && gimple_assign_rhs_code (stmt
) == ADDR_EXPR
)
1076 tree expr
= gimple_assign_rhs1 (stmt
);
1077 tree base
= get_base_address (TREE_OPERAND (expr
, 0));
1079 if (base
!= NULL_TREE
1080 && TREE_CODE (base
) == MEM_REF
1081 && TREE_CODE (TREE_OPERAND (base
, 0)) == SSA_NAME
)
1083 value_range_t
*vr
= get_value_range (TREE_OPERAND (base
, 0));
1084 if (range_is_nonnull (vr
))
1092 /* Returns true if EXPR is a valid value (as expected by compare_values) --
1093 a gimple invariant, or SSA_NAME +- CST. */
1096 valid_value_p (tree expr
)
1098 if (TREE_CODE (expr
) == SSA_NAME
)
1101 if (TREE_CODE (expr
) == PLUS_EXPR
1102 || TREE_CODE (expr
) == MINUS_EXPR
)
1103 return (TREE_CODE (TREE_OPERAND (expr
, 0)) == SSA_NAME
1104 && TREE_CODE (TREE_OPERAND (expr
, 1)) == INTEGER_CST
);
1106 return is_gimple_min_invariant (expr
);
1112 -2 if those are incomparable. */
1114 operand_less_p (tree val
, tree val2
)
1116 /* LT is folded faster than GE and others. Inline the common case. */
1117 if (TREE_CODE (val
) == INTEGER_CST
&& TREE_CODE (val2
) == INTEGER_CST
)
1119 if (TYPE_UNSIGNED (TREE_TYPE (val
)))
1120 return INT_CST_LT_UNSIGNED (val
, val2
);
1123 if (INT_CST_LT (val
, val2
))
1131 fold_defer_overflow_warnings ();
1133 tcmp
= fold_binary_to_constant (LT_EXPR
, boolean_type_node
, val
, val2
);
1135 fold_undefer_and_ignore_overflow_warnings ();
1138 || TREE_CODE (tcmp
) != INTEGER_CST
)
1141 if (!integer_zerop (tcmp
))
1145 /* val >= val2, not considering overflow infinity. */
1146 if (is_negative_overflow_infinity (val
))
1147 return is_negative_overflow_infinity (val2
) ? 0 : 1;
1148 else if (is_positive_overflow_infinity (val2
))
1149 return is_positive_overflow_infinity (val
) ? 0 : 1;
1154 /* Compare two values VAL1 and VAL2. Return
1156 -2 if VAL1 and VAL2 cannot be compared at compile-time,
1159 +1 if VAL1 > VAL2, and
1162 This is similar to tree_int_cst_compare but supports pointer values
1163 and values that cannot be compared at compile time.
1165 If STRICT_OVERFLOW_P is not NULL, then set *STRICT_OVERFLOW_P to
1166 true if the return value is only valid if we assume that signed
1167 overflow is undefined. */
1170 compare_values_warnv (tree val1
, tree val2
, bool *strict_overflow_p
)
1175 /* Below we rely on the fact that VAL1 and VAL2 are both pointers or
1177 gcc_assert (POINTER_TYPE_P (TREE_TYPE (val1
))
1178 == POINTER_TYPE_P (TREE_TYPE (val2
)));
1179 /* Convert the two values into the same type. This is needed because
1180 sizetype causes sign extension even for unsigned types. */
1181 val2
= fold_convert (TREE_TYPE (val1
), val2
);
1182 STRIP_USELESS_TYPE_CONVERSION (val2
);
1184 if ((TREE_CODE (val1
) == SSA_NAME
1185 || TREE_CODE (val1
) == PLUS_EXPR
1186 || TREE_CODE (val1
) == MINUS_EXPR
)
1187 && (TREE_CODE (val2
) == SSA_NAME
1188 || TREE_CODE (val2
) == PLUS_EXPR
1189 || TREE_CODE (val2
) == MINUS_EXPR
))
1191 tree n1
, c1
, n2
, c2
;
1192 enum tree_code code1
, code2
;
1194 /* If VAL1 and VAL2 are of the form 'NAME [+-] CST' or 'NAME',
1195 return -1 or +1 accordingly. If VAL1 and VAL2 don't use the
1196 same name, return -2. */
1197 if (TREE_CODE (val1
) == SSA_NAME
)
1205 code1
= TREE_CODE (val1
);
1206 n1
= TREE_OPERAND (val1
, 0);
1207 c1
= TREE_OPERAND (val1
, 1);
1208 if (tree_int_cst_sgn (c1
) == -1)
1210 if (is_negative_overflow_infinity (c1
))
1212 c1
= fold_unary_to_constant (NEGATE_EXPR
, TREE_TYPE (c1
), c1
);
1215 code1
= code1
== MINUS_EXPR
? PLUS_EXPR
: MINUS_EXPR
;
1219 if (TREE_CODE (val2
) == SSA_NAME
)
1227 code2
= TREE_CODE (val2
);
1228 n2
= TREE_OPERAND (val2
, 0);
1229 c2
= TREE_OPERAND (val2
, 1);
1230 if (tree_int_cst_sgn (c2
) == -1)
1232 if (is_negative_overflow_infinity (c2
))
1234 c2
= fold_unary_to_constant (NEGATE_EXPR
, TREE_TYPE (c2
), c2
);
1237 code2
= code2
== MINUS_EXPR
? PLUS_EXPR
: MINUS_EXPR
;
1241 /* Both values must use the same name. */
1245 if (code1
== SSA_NAME
1246 && code2
== SSA_NAME
)
1250 /* If overflow is defined we cannot simplify more. */
1251 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (val1
)))
1254 if (strict_overflow_p
!= NULL
1255 && (code1
== SSA_NAME
|| !TREE_NO_WARNING (val1
))
1256 && (code2
== SSA_NAME
|| !TREE_NO_WARNING (val2
)))
1257 *strict_overflow_p
= true;
1259 if (code1
== SSA_NAME
)
1261 if (code2
== PLUS_EXPR
)
1262 /* NAME < NAME + CST */
1264 else if (code2
== MINUS_EXPR
)
1265 /* NAME > NAME - CST */
1268 else if (code1
== PLUS_EXPR
)
1270 if (code2
== SSA_NAME
)
1271 /* NAME + CST > NAME */
1273 else if (code2
== PLUS_EXPR
)
1274 /* NAME + CST1 > NAME + CST2, if CST1 > CST2 */
1275 return compare_values_warnv (c1
, c2
, strict_overflow_p
);
1276 else if (code2
== MINUS_EXPR
)
1277 /* NAME + CST1 > NAME - CST2 */
1280 else if (code1
== MINUS_EXPR
)
1282 if (code2
== SSA_NAME
)
1283 /* NAME - CST < NAME */
1285 else if (code2
== PLUS_EXPR
)
1286 /* NAME - CST1 < NAME + CST2 */
1288 else if (code2
== MINUS_EXPR
)
1289 /* NAME - CST1 > NAME - CST2, if CST1 < CST2. Notice that
1290 C1 and C2 are swapped in the call to compare_values. */
1291 return compare_values_warnv (c2
, c1
, strict_overflow_p
);
1297 /* We cannot compare non-constants. */
1298 if (!is_gimple_min_invariant (val1
) || !is_gimple_min_invariant (val2
))
1301 if (!POINTER_TYPE_P (TREE_TYPE (val1
)))
1303 /* We cannot compare overflowed values, except for overflow
1305 if (TREE_OVERFLOW (val1
) || TREE_OVERFLOW (val2
))
1307 if (strict_overflow_p
!= NULL
)
1308 *strict_overflow_p
= true;
1309 if (is_negative_overflow_infinity (val1
))
1310 return is_negative_overflow_infinity (val2
) ? 0 : -1;
1311 else if (is_negative_overflow_infinity (val2
))
1313 else if (is_positive_overflow_infinity (val1
))
1314 return is_positive_overflow_infinity (val2
) ? 0 : 1;
1315 else if (is_positive_overflow_infinity (val2
))
1320 return tree_int_cst_compare (val1
, val2
);
1326 /* First see if VAL1 and VAL2 are not the same. */
1327 if (val1
== val2
|| operand_equal_p (val1
, val2
, 0))
1330 /* If VAL1 is a lower address than VAL2, return -1. */
1331 if (operand_less_p (val1
, val2
) == 1)
1334 /* If VAL1 is a higher address than VAL2, return +1. */
1335 if (operand_less_p (val2
, val1
) == 1)
1338 /* If VAL1 is different than VAL2, return +2.
1339 For integer constants we either have already returned -1 or 1
1340 or they are equivalent. We still might succeed in proving
1341 something about non-trivial operands. */
1342 if (TREE_CODE (val1
) != INTEGER_CST
1343 || TREE_CODE (val2
) != INTEGER_CST
)
1345 t
= fold_binary_to_constant (NE_EXPR
, boolean_type_node
, val1
, val2
);
1346 if (t
&& integer_onep (t
))
1354 /* Compare values like compare_values_warnv, but treat comparisons of
1355 nonconstants which rely on undefined overflow as incomparable. */
1358 compare_values (tree val1
, tree val2
)
1364 ret
= compare_values_warnv (val1
, val2
, &sop
);
1366 && (!is_gimple_min_invariant (val1
) || !is_gimple_min_invariant (val2
)))
1372 /* Return 1 if VAL is inside value range MIN <= VAL <= MAX,
1373 0 if VAL is not inside [MIN, MAX],
1374 -2 if we cannot tell either way.
1376 Benchmark compile/20001226-1.c compilation time after changing this
1380 value_inside_range (tree val
, tree min
, tree max
)
1384 cmp1
= operand_less_p (val
, min
);
1390 cmp2
= operand_less_p (max
, val
);
1398 /* Return true if value ranges VR0 and VR1 have a non-empty
1401 Benchmark compile/20001226-1.c compilation time after changing this
1406 value_ranges_intersect_p (value_range_t
*vr0
, value_range_t
*vr1
)
1408 /* The value ranges do not intersect if the maximum of the first range is
1409 less than the minimum of the second range or vice versa.
1410 When those relations are unknown, we can't do any better. */
1411 if (operand_less_p (vr0
->max
, vr1
->min
) != 0)
1413 if (operand_less_p (vr1
->max
, vr0
->min
) != 0)
1419 /* Return 1 if [MIN, MAX] includes the value zero, 0 if it does not
1420 include the value zero, -2 if we cannot tell. */
1423 range_includes_zero_p (tree min
, tree max
)
1425 tree zero
= build_int_cst (TREE_TYPE (min
), 0);
1426 return value_inside_range (zero
, min
, max
);
1429 /* Return true if *VR is know to only contain nonnegative values. */
1432 value_range_nonnegative_p (value_range_t
*vr
)
1434 /* Testing for VR_ANTI_RANGE is not useful here as any anti-range
1435 which would return a useful value should be encoded as a
1437 if (vr
->type
== VR_RANGE
)
1439 int result
= compare_values (vr
->min
, integer_zero_node
);
1440 return (result
== 0 || result
== 1);
1446 /* Return true if T, an SSA_NAME, is known to be nonnegative. Return
1447 false otherwise or if no value range information is available. */
1450 ssa_name_nonnegative_p (const_tree t
)
1452 value_range_t
*vr
= get_value_range (t
);
1454 if (INTEGRAL_TYPE_P (t
)
1455 && TYPE_UNSIGNED (t
))
1461 return value_range_nonnegative_p (vr
);
1464 /* If *VR has a value rante that is a single constant value return that,
1465 otherwise return NULL_TREE. */
1468 value_range_constant_singleton (value_range_t
*vr
)
1470 if (vr
->type
== VR_RANGE
1471 && operand_equal_p (vr
->min
, vr
->max
, 0)
1472 && is_gimple_min_invariant (vr
->min
))
1478 /* If OP has a value range with a single constant value return that,
1479 otherwise return NULL_TREE. This returns OP itself if OP is a
1483 op_with_constant_singleton_value_range (tree op
)
1485 if (is_gimple_min_invariant (op
))
1488 if (TREE_CODE (op
) != SSA_NAME
)
1491 return value_range_constant_singleton (get_value_range (op
));
1494 /* Return true if op is in a boolean [0, 1] value-range. */
1497 op_with_boolean_value_range_p (tree op
)
1501 if (TYPE_PRECISION (TREE_TYPE (op
)) == 1)
1504 if (integer_zerop (op
)
1505 || integer_onep (op
))
1508 if (TREE_CODE (op
) != SSA_NAME
)
1511 vr
= get_value_range (op
);
1512 return (vr
->type
== VR_RANGE
1513 && integer_zerop (vr
->min
)
1514 && integer_onep (vr
->max
));
1517 /* Extract value range information from an ASSERT_EXPR EXPR and store
1521 extract_range_from_assert (value_range_t
*vr_p
, tree expr
)
1523 tree var
, cond
, limit
, min
, max
, type
;
1524 value_range_t
*limit_vr
;
1525 enum tree_code cond_code
;
1527 var
= ASSERT_EXPR_VAR (expr
);
1528 cond
= ASSERT_EXPR_COND (expr
);
1530 gcc_assert (COMPARISON_CLASS_P (cond
));
1532 /* Find VAR in the ASSERT_EXPR conditional. */
1533 if (var
== TREE_OPERAND (cond
, 0)
1534 || TREE_CODE (TREE_OPERAND (cond
, 0)) == PLUS_EXPR
1535 || TREE_CODE (TREE_OPERAND (cond
, 0)) == NOP_EXPR
)
1537 /* If the predicate is of the form VAR COMP LIMIT, then we just
1538 take LIMIT from the RHS and use the same comparison code. */
1539 cond_code
= TREE_CODE (cond
);
1540 limit
= TREE_OPERAND (cond
, 1);
1541 cond
= TREE_OPERAND (cond
, 0);
1545 /* If the predicate is of the form LIMIT COMP VAR, then we need
1546 to flip around the comparison code to create the proper range
1548 cond_code
= swap_tree_comparison (TREE_CODE (cond
));
1549 limit
= TREE_OPERAND (cond
, 0);
1550 cond
= TREE_OPERAND (cond
, 1);
1553 limit
= avoid_overflow_infinity (limit
);
1555 type
= TREE_TYPE (var
);
1556 gcc_assert (limit
!= var
);
1558 /* For pointer arithmetic, we only keep track of pointer equality
1560 if (POINTER_TYPE_P (type
) && cond_code
!= NE_EXPR
&& cond_code
!= EQ_EXPR
)
1562 set_value_range_to_varying (vr_p
);
1566 /* If LIMIT is another SSA name and LIMIT has a range of its own,
1567 try to use LIMIT's range to avoid creating symbolic ranges
1569 limit_vr
= (TREE_CODE (limit
) == SSA_NAME
) ? get_value_range (limit
) : NULL
;
1571 /* LIMIT's range is only interesting if it has any useful information. */
1573 && (limit_vr
->type
== VR_UNDEFINED
1574 || limit_vr
->type
== VR_VARYING
1575 || symbolic_range_p (limit_vr
)))
1578 /* Initially, the new range has the same set of equivalences of
1579 VAR's range. This will be revised before returning the final
1580 value. Since assertions may be chained via mutually exclusive
1581 predicates, we will need to trim the set of equivalences before
1583 gcc_assert (vr_p
->equiv
== NULL
);
1584 add_equivalence (&vr_p
->equiv
, var
);
1586 /* Extract a new range based on the asserted comparison for VAR and
1587 LIMIT's value range. Notice that if LIMIT has an anti-range, we
1588 will only use it for equality comparisons (EQ_EXPR). For any
1589 other kind of assertion, we cannot derive a range from LIMIT's
1590 anti-range that can be used to describe the new range. For
1591 instance, ASSERT_EXPR <x_2, x_2 <= b_4>. If b_4 is ~[2, 10],
1592 then b_4 takes on the ranges [-INF, 1] and [11, +INF]. There is
1593 no single range for x_2 that could describe LE_EXPR, so we might
1594 as well build the range [b_4, +INF] for it.
1595 One special case we handle is extracting a range from a
1596 range test encoded as (unsigned)var + CST <= limit. */
1597 if (TREE_CODE (cond
) == NOP_EXPR
1598 || TREE_CODE (cond
) == PLUS_EXPR
)
1600 if (TREE_CODE (cond
) == PLUS_EXPR
)
1602 min
= fold_build1 (NEGATE_EXPR
, TREE_TYPE (TREE_OPERAND (cond
, 1)),
1603 TREE_OPERAND (cond
, 1));
1604 max
= int_const_binop (PLUS_EXPR
, limit
, min
);
1605 cond
= TREE_OPERAND (cond
, 0);
1609 min
= build_int_cst (TREE_TYPE (var
), 0);
1613 /* Make sure to not set TREE_OVERFLOW on the final type
1614 conversion. We are willingly interpreting large positive
1615 unsigned values as negative singed values here. */
1616 min
= force_fit_type_double (TREE_TYPE (var
), tree_to_double_int (min
),
1618 max
= force_fit_type_double (TREE_TYPE (var
), tree_to_double_int (max
),
1621 /* We can transform a max, min range to an anti-range or
1622 vice-versa. Use set_and_canonicalize_value_range which does
1624 if (cond_code
== LE_EXPR
)
1625 set_and_canonicalize_value_range (vr_p
, VR_RANGE
,
1626 min
, max
, vr_p
->equiv
);
1627 else if (cond_code
== GT_EXPR
)
1628 set_and_canonicalize_value_range (vr_p
, VR_ANTI_RANGE
,
1629 min
, max
, vr_p
->equiv
);
1633 else if (cond_code
== EQ_EXPR
)
1635 enum value_range_type range_type
;
1639 range_type
= limit_vr
->type
;
1640 min
= limit_vr
->min
;
1641 max
= limit_vr
->max
;
1645 range_type
= VR_RANGE
;
1650 set_value_range (vr_p
, range_type
, min
, max
, vr_p
->equiv
);
1652 /* When asserting the equality VAR == LIMIT and LIMIT is another
1653 SSA name, the new range will also inherit the equivalence set
1655 if (TREE_CODE (limit
) == SSA_NAME
)
1656 add_equivalence (&vr_p
->equiv
, limit
);
1658 else if (cond_code
== NE_EXPR
)
1660 /* As described above, when LIMIT's range is an anti-range and
1661 this assertion is an inequality (NE_EXPR), then we cannot
1662 derive anything from the anti-range. For instance, if
1663 LIMIT's range was ~[0, 0], the assertion 'VAR != LIMIT' does
1664 not imply that VAR's range is [0, 0]. So, in the case of
1665 anti-ranges, we just assert the inequality using LIMIT and
1668 If LIMIT_VR is a range, we can only use it to build a new
1669 anti-range if LIMIT_VR is a single-valued range. For
1670 instance, if LIMIT_VR is [0, 1], the predicate
1671 VAR != [0, 1] does not mean that VAR's range is ~[0, 1].
1672 Rather, it means that for value 0 VAR should be ~[0, 0]
1673 and for value 1, VAR should be ~[1, 1]. We cannot
1674 represent these ranges.
1676 The only situation in which we can build a valid
1677 anti-range is when LIMIT_VR is a single-valued range
1678 (i.e., LIMIT_VR->MIN == LIMIT_VR->MAX). In that case,
1679 build the anti-range ~[LIMIT_VR->MIN, LIMIT_VR->MAX]. */
1681 && limit_vr
->type
== VR_RANGE
1682 && compare_values (limit_vr
->min
, limit_vr
->max
) == 0)
1684 min
= limit_vr
->min
;
1685 max
= limit_vr
->max
;
1689 /* In any other case, we cannot use LIMIT's range to build a
1690 valid anti-range. */
1694 /* If MIN and MAX cover the whole range for their type, then
1695 just use the original LIMIT. */
1696 if (INTEGRAL_TYPE_P (type
)
1697 && vrp_val_is_min (min
)
1698 && vrp_val_is_max (max
))
1701 set_value_range (vr_p
, VR_ANTI_RANGE
, min
, max
, vr_p
->equiv
);
1703 else if (cond_code
== LE_EXPR
|| cond_code
== LT_EXPR
)
1705 min
= TYPE_MIN_VALUE (type
);
1707 if (limit_vr
== NULL
|| limit_vr
->type
== VR_ANTI_RANGE
)
1711 /* If LIMIT_VR is of the form [N1, N2], we need to build the
1712 range [MIN, N2] for LE_EXPR and [MIN, N2 - 1] for
1714 max
= limit_vr
->max
;
1717 /* If the maximum value forces us to be out of bounds, simply punt.
1718 It would be pointless to try and do anything more since this
1719 all should be optimized away above us. */
1720 if ((cond_code
== LT_EXPR
1721 && compare_values (max
, min
) == 0)
1722 || (CONSTANT_CLASS_P (max
) && TREE_OVERFLOW (max
)))
1723 set_value_range_to_varying (vr_p
);
1726 /* For LT_EXPR, we create the range [MIN, MAX - 1]. */
1727 if (cond_code
== LT_EXPR
)
1729 if (TYPE_PRECISION (TREE_TYPE (max
)) == 1
1730 && !TYPE_UNSIGNED (TREE_TYPE (max
)))
1731 max
= fold_build2 (PLUS_EXPR
, TREE_TYPE (max
), max
,
1732 build_int_cst (TREE_TYPE (max
), -1));
1734 max
= fold_build2 (MINUS_EXPR
, TREE_TYPE (max
), max
,
1735 build_int_cst (TREE_TYPE (max
), 1));
1737 TREE_NO_WARNING (max
) = 1;
1740 set_value_range (vr_p
, VR_RANGE
, min
, max
, vr_p
->equiv
);
1743 else if (cond_code
== GE_EXPR
|| cond_code
== GT_EXPR
)
1745 max
= TYPE_MAX_VALUE (type
);
1747 if (limit_vr
== NULL
|| limit_vr
->type
== VR_ANTI_RANGE
)
1751 /* If LIMIT_VR is of the form [N1, N2], we need to build the
1752 range [N1, MAX] for GE_EXPR and [N1 + 1, MAX] for
1754 min
= limit_vr
->min
;
1757 /* If the minimum value forces us to be out of bounds, simply punt.
1758 It would be pointless to try and do anything more since this
1759 all should be optimized away above us. */
1760 if ((cond_code
== GT_EXPR
1761 && compare_values (min
, max
) == 0)
1762 || (CONSTANT_CLASS_P (min
) && TREE_OVERFLOW (min
)))
1763 set_value_range_to_varying (vr_p
);
1766 /* For GT_EXPR, we create the range [MIN + 1, MAX]. */
1767 if (cond_code
== GT_EXPR
)
1769 if (TYPE_PRECISION (TREE_TYPE (min
)) == 1
1770 && !TYPE_UNSIGNED (TREE_TYPE (min
)))
1771 min
= fold_build2 (MINUS_EXPR
, TREE_TYPE (min
), min
,
1772 build_int_cst (TREE_TYPE (min
), -1));
1774 min
= fold_build2 (PLUS_EXPR
, TREE_TYPE (min
), min
,
1775 build_int_cst (TREE_TYPE (min
), 1));
1777 TREE_NO_WARNING (min
) = 1;
1780 set_value_range (vr_p
, VR_RANGE
, min
, max
, vr_p
->equiv
);
1786 /* Finally intersect the new range with what we already know about var. */
1787 vrp_intersect_ranges (vr_p
, get_value_range (var
));
1791 /* Extract range information from SSA name VAR and store it in VR. If
1792 VAR has an interesting range, use it. Otherwise, create the
1793 range [VAR, VAR] and return it. This is useful in situations where
1794 we may have conditionals testing values of VARYING names. For
1801 Even if y_5 is deemed VARYING, we can determine that x_3 > y_5 is
1805 extract_range_from_ssa_name (value_range_t
*vr
, tree var
)
1807 value_range_t
*var_vr
= get_value_range (var
);
1809 if (var_vr
->type
!= VR_UNDEFINED
&& var_vr
->type
!= VR_VARYING
)
1810 copy_value_range (vr
, var_vr
);
1812 set_value_range (vr
, VR_RANGE
, var
, var
, NULL
);
1814 add_equivalence (&vr
->equiv
, var
);
1818 /* Wrapper around int_const_binop. If the operation overflows and we
1819 are not using wrapping arithmetic, then adjust the result to be
1820 -INF or +INF depending on CODE, VAL1 and VAL2. This can return
1821 NULL_TREE if we need to use an overflow infinity representation but
1822 the type does not support it. */
1825 vrp_int_const_binop (enum tree_code code
, tree val1
, tree val2
)
1829 res
= int_const_binop (code
, val1
, val2
);
1831 /* If we are using unsigned arithmetic, operate symbolically
1832 on -INF and +INF as int_const_binop only handles signed overflow. */
1833 if (TYPE_UNSIGNED (TREE_TYPE (val1
)))
1835 int checkz
= compare_values (res
, val1
);
1836 bool overflow
= false;
1838 /* Ensure that res = val1 [+*] val2 >= val1
1839 or that res = val1 - val2 <= val1. */
1840 if ((code
== PLUS_EXPR
1841 && !(checkz
== 1 || checkz
== 0))
1842 || (code
== MINUS_EXPR
1843 && !(checkz
== 0 || checkz
== -1)))
1847 /* Checking for multiplication overflow is done by dividing the
1848 output of the multiplication by the first input of the
1849 multiplication. If the result of that division operation is
1850 not equal to the second input of the multiplication, then the
1851 multiplication overflowed. */
1852 else if (code
== MULT_EXPR
&& !integer_zerop (val1
))
1854 tree tmp
= int_const_binop (TRUNC_DIV_EXPR
,
1857 int check
= compare_values (tmp
, val2
);
1865 res
= copy_node (res
);
1866 TREE_OVERFLOW (res
) = 1;
1870 else if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (val1
)))
1871 /* If the singed operation wraps then int_const_binop has done
1872 everything we want. */
1874 else if ((TREE_OVERFLOW (res
)
1875 && !TREE_OVERFLOW (val1
)
1876 && !TREE_OVERFLOW (val2
))
1877 || is_overflow_infinity (val1
)
1878 || is_overflow_infinity (val2
))
1880 /* If the operation overflowed but neither VAL1 nor VAL2 are
1881 overflown, return -INF or +INF depending on the operation
1882 and the combination of signs of the operands. */
1883 int sgn1
= tree_int_cst_sgn (val1
);
1884 int sgn2
= tree_int_cst_sgn (val2
);
1886 if (needs_overflow_infinity (TREE_TYPE (res
))
1887 && !supports_overflow_infinity (TREE_TYPE (res
)))
1890 /* We have to punt on adding infinities of different signs,
1891 since we can't tell what the sign of the result should be.
1892 Likewise for subtracting infinities of the same sign. */
1893 if (((code
== PLUS_EXPR
&& sgn1
!= sgn2
)
1894 || (code
== MINUS_EXPR
&& sgn1
== sgn2
))
1895 && is_overflow_infinity (val1
)
1896 && is_overflow_infinity (val2
))
1899 /* Don't try to handle division or shifting of infinities. */
1900 if ((code
== TRUNC_DIV_EXPR
1901 || code
== FLOOR_DIV_EXPR
1902 || code
== CEIL_DIV_EXPR
1903 || code
== EXACT_DIV_EXPR
1904 || code
== ROUND_DIV_EXPR
1905 || code
== RSHIFT_EXPR
)
1906 && (is_overflow_infinity (val1
)
1907 || is_overflow_infinity (val2
)))
1910 /* Notice that we only need to handle the restricted set of
1911 operations handled by extract_range_from_binary_expr.
1912 Among them, only multiplication, addition and subtraction
1913 can yield overflow without overflown operands because we
1914 are working with integral types only... except in the
1915 case VAL1 = -INF and VAL2 = -1 which overflows to +INF
1916 for division too. */
1918 /* For multiplication, the sign of the overflow is given
1919 by the comparison of the signs of the operands. */
1920 if ((code
== MULT_EXPR
&& sgn1
== sgn2
)
1921 /* For addition, the operands must be of the same sign
1922 to yield an overflow. Its sign is therefore that
1923 of one of the operands, for example the first. For
1924 infinite operands X + -INF is negative, not positive. */
1925 || (code
== PLUS_EXPR
1927 ? !is_negative_overflow_infinity (val2
)
1928 : is_positive_overflow_infinity (val2
)))
1929 /* For subtraction, non-infinite operands must be of
1930 different signs to yield an overflow. Its sign is
1931 therefore that of the first operand or the opposite of
1932 that of the second operand. A first operand of 0 counts
1933 as positive here, for the corner case 0 - (-INF), which
1934 overflows, but must yield +INF. For infinite operands 0
1935 - INF is negative, not positive. */
1936 || (code
== MINUS_EXPR
1938 ? !is_positive_overflow_infinity (val2
)
1939 : is_negative_overflow_infinity (val2
)))
1940 /* We only get in here with positive shift count, so the
1941 overflow direction is the same as the sign of val1.
1942 Actually rshift does not overflow at all, but we only
1943 handle the case of shifting overflowed -INF and +INF. */
1944 || (code
== RSHIFT_EXPR
1946 /* For division, the only case is -INF / -1 = +INF. */
1947 || code
== TRUNC_DIV_EXPR
1948 || code
== FLOOR_DIV_EXPR
1949 || code
== CEIL_DIV_EXPR
1950 || code
== EXACT_DIV_EXPR
1951 || code
== ROUND_DIV_EXPR
)
1952 return (needs_overflow_infinity (TREE_TYPE (res
))
1953 ? positive_overflow_infinity (TREE_TYPE (res
))
1954 : TYPE_MAX_VALUE (TREE_TYPE (res
)));
1956 return (needs_overflow_infinity (TREE_TYPE (res
))
1957 ? negative_overflow_infinity (TREE_TYPE (res
))
1958 : TYPE_MIN_VALUE (TREE_TYPE (res
)));
1965 /* For range VR compute two double_int bitmasks. In *MAY_BE_NONZERO
1966 bitmask if some bit is unset, it means for all numbers in the range
1967 the bit is 0, otherwise it might be 0 or 1. In *MUST_BE_NONZERO
1968 bitmask if some bit is set, it means for all numbers in the range
1969 the bit is 1, otherwise it might be 0 or 1. */
1972 zero_nonzero_bits_from_vr (value_range_t
*vr
,
1973 double_int
*may_be_nonzero
,
1974 double_int
*must_be_nonzero
)
1976 *may_be_nonzero
= double_int_minus_one
;
1977 *must_be_nonzero
= double_int_zero
;
1978 if (!range_int_cst_p (vr
)
1979 || TREE_OVERFLOW (vr
->min
)
1980 || TREE_OVERFLOW (vr
->max
))
1983 if (range_int_cst_singleton_p (vr
))
1985 *may_be_nonzero
= tree_to_double_int (vr
->min
);
1986 *must_be_nonzero
= *may_be_nonzero
;
1988 else if (tree_int_cst_sgn (vr
->min
) >= 0
1989 || tree_int_cst_sgn (vr
->max
) < 0)
1991 double_int dmin
= tree_to_double_int (vr
->min
);
1992 double_int dmax
= tree_to_double_int (vr
->max
);
1993 double_int xor_mask
= dmin
^ dmax
;
1994 *may_be_nonzero
= dmin
| dmax
;
1995 *must_be_nonzero
= dmin
& dmax
;
1996 if (xor_mask
.high
!= 0)
1998 unsigned HOST_WIDE_INT mask
1999 = ((unsigned HOST_WIDE_INT
) 1
2000 << floor_log2 (xor_mask
.high
)) - 1;
2001 may_be_nonzero
->low
= ALL_ONES
;
2002 may_be_nonzero
->high
|= mask
;
2003 must_be_nonzero
->low
= 0;
2004 must_be_nonzero
->high
&= ~mask
;
2006 else if (xor_mask
.low
!= 0)
2008 unsigned HOST_WIDE_INT mask
2009 = ((unsigned HOST_WIDE_INT
) 1
2010 << floor_log2 (xor_mask
.low
)) - 1;
2011 may_be_nonzero
->low
|= mask
;
2012 must_be_nonzero
->low
&= ~mask
;
2019 /* Create two value-ranges in *VR0 and *VR1 from the anti-range *AR
2020 so that *VR0 U *VR1 == *AR. Returns true if that is possible,
2021 false otherwise. If *AR can be represented with a single range
2022 *VR1 will be VR_UNDEFINED. */
2025 ranges_from_anti_range (value_range_t
*ar
,
2026 value_range_t
*vr0
, value_range_t
*vr1
)
2028 tree type
= TREE_TYPE (ar
->min
);
2030 vr0
->type
= VR_UNDEFINED
;
2031 vr1
->type
= VR_UNDEFINED
;
2033 if (ar
->type
!= VR_ANTI_RANGE
2034 || TREE_CODE (ar
->min
) != INTEGER_CST
2035 || TREE_CODE (ar
->max
) != INTEGER_CST
2036 || !vrp_val_min (type
)
2037 || !vrp_val_max (type
))
2040 if (!vrp_val_is_min (ar
->min
))
2042 vr0
->type
= VR_RANGE
;
2043 vr0
->min
= vrp_val_min (type
);
2045 = double_int_to_tree (type
,
2046 tree_to_double_int (ar
->min
) - double_int_one
);
2048 if (!vrp_val_is_max (ar
->max
))
2050 vr1
->type
= VR_RANGE
;
2052 = double_int_to_tree (type
,
2053 tree_to_double_int (ar
->max
) + double_int_one
);
2054 vr1
->max
= vrp_val_max (type
);
2056 if (vr0
->type
== VR_UNDEFINED
)
2059 vr1
->type
= VR_UNDEFINED
;
2062 return vr0
->type
!= VR_UNDEFINED
;
2065 /* Helper to extract a value-range *VR for a multiplicative operation
2069 extract_range_from_multiplicative_op_1 (value_range_t
*vr
,
2070 enum tree_code code
,
2071 value_range_t
*vr0
, value_range_t
*vr1
)
2073 enum value_range_type type
;
2080 /* Multiplications, divisions and shifts are a bit tricky to handle,
2081 depending on the mix of signs we have in the two ranges, we
2082 need to operate on different values to get the minimum and
2083 maximum values for the new range. One approach is to figure
2084 out all the variations of range combinations and do the
2087 However, this involves several calls to compare_values and it
2088 is pretty convoluted. It's simpler to do the 4 operations
2089 (MIN0 OP MIN1, MIN0 OP MAX1, MAX0 OP MIN1 and MAX0 OP MAX0 OP
2090 MAX1) and then figure the smallest and largest values to form
2092 gcc_assert (code
== MULT_EXPR
2093 || code
== TRUNC_DIV_EXPR
2094 || code
== FLOOR_DIV_EXPR
2095 || code
== CEIL_DIV_EXPR
2096 || code
== EXACT_DIV_EXPR
2097 || code
== ROUND_DIV_EXPR
2098 || code
== RSHIFT_EXPR
2099 || code
== LSHIFT_EXPR
);
2100 gcc_assert ((vr0
->type
== VR_RANGE
2101 || (code
== MULT_EXPR
&& vr0
->type
== VR_ANTI_RANGE
))
2102 && vr0
->type
== vr1
->type
);
2106 /* Compute the 4 cross operations. */
2108 val
[0] = vrp_int_const_binop (code
, vr0
->min
, vr1
->min
);
2109 if (val
[0] == NULL_TREE
)
2112 if (vr1
->max
== vr1
->min
)
2116 val
[1] = vrp_int_const_binop (code
, vr0
->min
, vr1
->max
);
2117 if (val
[1] == NULL_TREE
)
2121 if (vr0
->max
== vr0
->min
)
2125 val
[2] = vrp_int_const_binop (code
, vr0
->max
, vr1
->min
);
2126 if (val
[2] == NULL_TREE
)
2130 if (vr0
->min
== vr0
->max
|| vr1
->min
== vr1
->max
)
2134 val
[3] = vrp_int_const_binop (code
, vr0
->max
, vr1
->max
);
2135 if (val
[3] == NULL_TREE
)
2141 set_value_range_to_varying (vr
);
2145 /* Set MIN to the minimum of VAL[i] and MAX to the maximum
2149 for (i
= 1; i
< 4; i
++)
2151 if (!is_gimple_min_invariant (min
)
2152 || (TREE_OVERFLOW (min
) && !is_overflow_infinity (min
))
2153 || !is_gimple_min_invariant (max
)
2154 || (TREE_OVERFLOW (max
) && !is_overflow_infinity (max
)))
2159 if (!is_gimple_min_invariant (val
[i
])
2160 || (TREE_OVERFLOW (val
[i
])
2161 && !is_overflow_infinity (val
[i
])))
2163 /* If we found an overflowed value, set MIN and MAX
2164 to it so that we set the resulting range to
2170 if (compare_values (val
[i
], min
) == -1)
2173 if (compare_values (val
[i
], max
) == 1)
2178 /* If either MIN or MAX overflowed, then set the resulting range to
2179 VARYING. But we do accept an overflow infinity
2181 if (min
== NULL_TREE
2182 || !is_gimple_min_invariant (min
)
2183 || (TREE_OVERFLOW (min
) && !is_overflow_infinity (min
))
2185 || !is_gimple_min_invariant (max
)
2186 || (TREE_OVERFLOW (max
) && !is_overflow_infinity (max
)))
2188 set_value_range_to_varying (vr
);
2194 2) [-INF, +-INF(OVF)]
2195 3) [+-INF(OVF), +INF]
2196 4) [+-INF(OVF), +-INF(OVF)]
2197 We learn nothing when we have INF and INF(OVF) on both sides.
2198 Note that we do accept [-INF, -INF] and [+INF, +INF] without
2200 if ((vrp_val_is_min (min
) || is_overflow_infinity (min
))
2201 && (vrp_val_is_max (max
) || is_overflow_infinity (max
)))
2203 set_value_range_to_varying (vr
);
2207 cmp
= compare_values (min
, max
);
2208 if (cmp
== -2 || cmp
== 1)
2210 /* If the new range has its limits swapped around (MIN > MAX),
2211 then the operation caused one of them to wrap around, mark
2212 the new range VARYING. */
2213 set_value_range_to_varying (vr
);
2216 set_value_range (vr
, type
, min
, max
, NULL
);
2219 /* Some quadruple precision helpers. */
2221 quad_int_cmp (double_int l0
, double_int h0
,
2222 double_int l1
, double_int h1
, bool uns
)
2224 int c
= h0
.cmp (h1
, uns
);
2225 if (c
!= 0) return c
;
2226 return l0
.ucmp (l1
);
2230 quad_int_pair_sort (double_int
*l0
, double_int
*h0
,
2231 double_int
*l1
, double_int
*h1
, bool uns
)
2233 if (quad_int_cmp (*l0
, *h0
, *l1
, *h1
, uns
) > 0)
2236 tmp
= *l0
; *l0
= *l1
; *l1
= tmp
;
2237 tmp
= *h0
; *h0
= *h1
; *h1
= tmp
;
2241 /* Extract range information from a binary operation CODE based on
2242 the ranges of each of its operands, *VR0 and *VR1 with resulting
2243 type EXPR_TYPE. The resulting range is stored in *VR. */
2246 extract_range_from_binary_expr_1 (value_range_t
*vr
,
2247 enum tree_code code
, tree expr_type
,
2248 value_range_t
*vr0_
, value_range_t
*vr1_
)
2250 value_range_t vr0
= *vr0_
, vr1
= *vr1_
;
2251 value_range_t vrtem0
= VR_INITIALIZER
, vrtem1
= VR_INITIALIZER
;
2252 enum value_range_type type
;
2253 tree min
= NULL_TREE
, max
= NULL_TREE
;
2256 if (!INTEGRAL_TYPE_P (expr_type
)
2257 && !POINTER_TYPE_P (expr_type
))
2259 set_value_range_to_varying (vr
);
2263 /* Not all binary expressions can be applied to ranges in a
2264 meaningful way. Handle only arithmetic operations. */
2265 if (code
!= PLUS_EXPR
2266 && code
!= MINUS_EXPR
2267 && code
!= POINTER_PLUS_EXPR
2268 && code
!= MULT_EXPR
2269 && code
!= TRUNC_DIV_EXPR
2270 && code
!= FLOOR_DIV_EXPR
2271 && code
!= CEIL_DIV_EXPR
2272 && code
!= EXACT_DIV_EXPR
2273 && code
!= ROUND_DIV_EXPR
2274 && code
!= TRUNC_MOD_EXPR
2275 && code
!= RSHIFT_EXPR
2276 && code
!= LSHIFT_EXPR
2279 && code
!= BIT_AND_EXPR
2280 && code
!= BIT_IOR_EXPR
2281 && code
!= BIT_XOR_EXPR
)
2283 set_value_range_to_varying (vr
);
2287 /* If both ranges are UNDEFINED, so is the result. */
2288 if (vr0
.type
== VR_UNDEFINED
&& vr1
.type
== VR_UNDEFINED
)
2290 set_value_range_to_undefined (vr
);
2293 /* If one of the ranges is UNDEFINED drop it to VARYING for the following
2294 code. At some point we may want to special-case operations that
2295 have UNDEFINED result for all or some value-ranges of the not UNDEFINED
2297 else if (vr0
.type
== VR_UNDEFINED
)
2298 set_value_range_to_varying (&vr0
);
2299 else if (vr1
.type
== VR_UNDEFINED
)
2300 set_value_range_to_varying (&vr1
);
2302 /* Now canonicalize anti-ranges to ranges when they are not symbolic
2303 and express ~[] op X as ([]' op X) U ([]'' op X). */
2304 if (vr0
.type
== VR_ANTI_RANGE
2305 && ranges_from_anti_range (&vr0
, &vrtem0
, &vrtem1
))
2307 extract_range_from_binary_expr_1 (vr
, code
, expr_type
, &vrtem0
, vr1_
);
2308 if (vrtem1
.type
!= VR_UNDEFINED
)
2310 value_range_t vrres
= VR_INITIALIZER
;
2311 extract_range_from_binary_expr_1 (&vrres
, code
, expr_type
,
2313 vrp_meet (vr
, &vrres
);
2317 /* Likewise for X op ~[]. */
2318 if (vr1
.type
== VR_ANTI_RANGE
2319 && ranges_from_anti_range (&vr1
, &vrtem0
, &vrtem1
))
2321 extract_range_from_binary_expr_1 (vr
, code
, expr_type
, vr0_
, &vrtem0
);
2322 if (vrtem1
.type
!= VR_UNDEFINED
)
2324 value_range_t vrres
= VR_INITIALIZER
;
2325 extract_range_from_binary_expr_1 (&vrres
, code
, expr_type
,
2327 vrp_meet (vr
, &vrres
);
2332 /* The type of the resulting value range defaults to VR0.TYPE. */
2335 /* Refuse to operate on VARYING ranges, ranges of different kinds
2336 and symbolic ranges. As an exception, we allow BIT_AND_EXPR
2337 because we may be able to derive a useful range even if one of
2338 the operands is VR_VARYING or symbolic range. Similarly for
2339 divisions. TODO, we may be able to derive anti-ranges in
2341 if (code
!= BIT_AND_EXPR
2342 && code
!= BIT_IOR_EXPR
2343 && code
!= TRUNC_DIV_EXPR
2344 && code
!= FLOOR_DIV_EXPR
2345 && code
!= CEIL_DIV_EXPR
2346 && code
!= EXACT_DIV_EXPR
2347 && code
!= ROUND_DIV_EXPR
2348 && code
!= TRUNC_MOD_EXPR
2349 && (vr0
.type
== VR_VARYING
2350 || vr1
.type
== VR_VARYING
2351 || vr0
.type
!= vr1
.type
2352 || symbolic_range_p (&vr0
)
2353 || symbolic_range_p (&vr1
)))
2355 set_value_range_to_varying (vr
);
2359 /* Now evaluate the expression to determine the new range. */
2360 if (POINTER_TYPE_P (expr_type
))
2362 if (code
== MIN_EXPR
|| code
== MAX_EXPR
)
2364 /* For MIN/MAX expressions with pointers, we only care about
2365 nullness, if both are non null, then the result is nonnull.
2366 If both are null, then the result is null. Otherwise they
2368 if (range_is_nonnull (&vr0
) && range_is_nonnull (&vr1
))
2369 set_value_range_to_nonnull (vr
, expr_type
);
2370 else if (range_is_null (&vr0
) && range_is_null (&vr1
))
2371 set_value_range_to_null (vr
, expr_type
);
2373 set_value_range_to_varying (vr
);
2375 else if (code
== POINTER_PLUS_EXPR
)
2377 /* For pointer types, we are really only interested in asserting
2378 whether the expression evaluates to non-NULL. */
2379 if (range_is_nonnull (&vr0
) || range_is_nonnull (&vr1
))
2380 set_value_range_to_nonnull (vr
, expr_type
);
2381 else if (range_is_null (&vr0
) && range_is_null (&vr1
))
2382 set_value_range_to_null (vr
, expr_type
);
2384 set_value_range_to_varying (vr
);
2386 else if (code
== BIT_AND_EXPR
)
2388 /* For pointer types, we are really only interested in asserting
2389 whether the expression evaluates to non-NULL. */
2390 if (range_is_nonnull (&vr0
) && range_is_nonnull (&vr1
))
2391 set_value_range_to_nonnull (vr
, expr_type
);
2392 else if (range_is_null (&vr0
) || range_is_null (&vr1
))
2393 set_value_range_to_null (vr
, expr_type
);
2395 set_value_range_to_varying (vr
);
2398 set_value_range_to_varying (vr
);
2403 /* For integer ranges, apply the operation to each end of the
2404 range and see what we end up with. */
2405 if (code
== PLUS_EXPR
|| code
== MINUS_EXPR
)
2407 /* If we have a PLUS_EXPR with two VR_RANGE integer constant
2408 ranges compute the precise range for such case if possible. */
2409 if (range_int_cst_p (&vr0
)
2410 && range_int_cst_p (&vr1
)
2411 /* We need as many bits as the possibly unsigned inputs. */
2412 && TYPE_PRECISION (expr_type
) <= HOST_BITS_PER_DOUBLE_INT
)
2414 double_int min0
= tree_to_double_int (vr0
.min
);
2415 double_int max0
= tree_to_double_int (vr0
.max
);
2416 double_int min1
= tree_to_double_int (vr1
.min
);
2417 double_int max1
= tree_to_double_int (vr1
.max
);
2418 bool uns
= TYPE_UNSIGNED (expr_type
);
2420 = double_int::min_value (TYPE_PRECISION (expr_type
), uns
);
2422 = double_int::max_value (TYPE_PRECISION (expr_type
), uns
);
2423 double_int dmin
, dmax
;
2427 if (code
== PLUS_EXPR
)
2432 /* Check for overflow in double_int. */
2433 if (min1
.cmp (double_int_zero
, uns
) != dmin
.cmp (min0
, uns
))
2434 min_ovf
= min0
.cmp (dmin
, uns
);
2435 if (max1
.cmp (double_int_zero
, uns
) != dmax
.cmp (max0
, uns
))
2436 max_ovf
= max0
.cmp (dmax
, uns
);
2438 else /* if (code == MINUS_EXPR) */
2443 if (double_int_zero
.cmp (max1
, uns
) != dmin
.cmp (min0
, uns
))
2444 min_ovf
= min0
.cmp (max1
, uns
);
2445 if (double_int_zero
.cmp (min1
, uns
) != dmax
.cmp (max0
, uns
))
2446 max_ovf
= max0
.cmp (min1
, uns
);
2449 /* For non-wrapping arithmetic look at possibly smaller
2450 value-ranges of the type. */
2451 if (!TYPE_OVERFLOW_WRAPS (expr_type
))
2453 if (vrp_val_min (expr_type
))
2454 type_min
= tree_to_double_int (vrp_val_min (expr_type
));
2455 if (vrp_val_max (expr_type
))
2456 type_max
= tree_to_double_int (vrp_val_max (expr_type
));
2459 /* Check for type overflow. */
2462 if (dmin
.cmp (type_min
, uns
) == -1)
2464 else if (dmin
.cmp (type_max
, uns
) == 1)
2469 if (dmax
.cmp (type_min
, uns
) == -1)
2471 else if (dmax
.cmp (type_max
, uns
) == 1)
2475 if (TYPE_OVERFLOW_WRAPS (expr_type
))
2477 /* If overflow wraps, truncate the values and adjust the
2478 range kind and bounds appropriately. */
2480 = dmin
.ext (TYPE_PRECISION (expr_type
), uns
);
2482 = dmax
.ext (TYPE_PRECISION (expr_type
), uns
);
2483 if (min_ovf
== max_ovf
)
2485 /* No overflow or both overflow or underflow. The
2486 range kind stays VR_RANGE. */
2487 min
= double_int_to_tree (expr_type
, tmin
);
2488 max
= double_int_to_tree (expr_type
, tmax
);
2490 else if (min_ovf
== -1
2493 /* Underflow and overflow, drop to VR_VARYING. */
2494 set_value_range_to_varying (vr
);
2499 /* Min underflow or max overflow. The range kind
2500 changes to VR_ANTI_RANGE. */
2501 bool covers
= false;
2502 double_int tem
= tmin
;
2503 gcc_assert ((min_ovf
== -1 && max_ovf
== 0)
2504 || (max_ovf
== 1 && min_ovf
== 0));
2505 type
= VR_ANTI_RANGE
;
2506 tmin
= tmax
+ double_int_one
;
2507 if (tmin
.cmp (tmax
, uns
) < 0)
2509 tmax
= tem
+ double_int_minus_one
;
2510 if (tmax
.cmp (tem
, uns
) > 0)
2512 /* If the anti-range would cover nothing, drop to varying.
2513 Likewise if the anti-range bounds are outside of the
2515 if (covers
|| tmin
.cmp (tmax
, uns
) > 0)
2517 set_value_range_to_varying (vr
);
2520 min
= double_int_to_tree (expr_type
, tmin
);
2521 max
= double_int_to_tree (expr_type
, tmax
);
2526 /* If overflow does not wrap, saturate to the types min/max
2530 if (needs_overflow_infinity (expr_type
)
2531 && supports_overflow_infinity (expr_type
))
2532 min
= negative_overflow_infinity (expr_type
);
2534 min
= double_int_to_tree (expr_type
, type_min
);
2536 else if (min_ovf
== 1)
2538 if (needs_overflow_infinity (expr_type
)
2539 && supports_overflow_infinity (expr_type
))
2540 min
= positive_overflow_infinity (expr_type
);
2542 min
= double_int_to_tree (expr_type
, type_max
);
2545 min
= double_int_to_tree (expr_type
, dmin
);
2549 if (needs_overflow_infinity (expr_type
)
2550 && supports_overflow_infinity (expr_type
))
2551 max
= negative_overflow_infinity (expr_type
);
2553 max
= double_int_to_tree (expr_type
, type_min
);
2555 else if (max_ovf
== 1)
2557 if (needs_overflow_infinity (expr_type
)
2558 && supports_overflow_infinity (expr_type
))
2559 max
= positive_overflow_infinity (expr_type
);
2561 max
= double_int_to_tree (expr_type
, type_max
);
2564 max
= double_int_to_tree (expr_type
, dmax
);
2566 if (needs_overflow_infinity (expr_type
)
2567 && supports_overflow_infinity (expr_type
))
2569 if (is_negative_overflow_infinity (vr0
.min
)
2570 || (code
== PLUS_EXPR
2571 ? is_negative_overflow_infinity (vr1
.min
)
2572 : is_positive_overflow_infinity (vr1
.max
)))
2573 min
= negative_overflow_infinity (expr_type
);
2574 if (is_positive_overflow_infinity (vr0
.max
)
2575 || (code
== PLUS_EXPR
2576 ? is_positive_overflow_infinity (vr1
.max
)
2577 : is_negative_overflow_infinity (vr1
.min
)))
2578 max
= positive_overflow_infinity (expr_type
);
2583 /* For other cases, for example if we have a PLUS_EXPR with two
2584 VR_ANTI_RANGEs, drop to VR_VARYING. It would take more effort
2585 to compute a precise range for such a case.
2586 ??? General even mixed range kind operations can be expressed
2587 by for example transforming ~[3, 5] + [1, 2] to range-only
2588 operations and a union primitive:
2589 [-INF, 2] + [1, 2] U [5, +INF] + [1, 2]
2590 [-INF+1, 4] U [6, +INF(OVF)]
2591 though usually the union is not exactly representable with
2592 a single range or anti-range as the above is
2593 [-INF+1, +INF(OVF)] intersected with ~[5, 5]
2594 but one could use a scheme similar to equivalences for this. */
2595 set_value_range_to_varying (vr
);
2599 else if (code
== MIN_EXPR
2600 || code
== MAX_EXPR
)
2602 if (vr0
.type
== VR_ANTI_RANGE
)
2604 /* For MIN_EXPR and MAX_EXPR with two VR_ANTI_RANGEs,
2605 the resulting VR_ANTI_RANGE is the same - intersection
2606 of the two ranges. */
2607 min
= vrp_int_const_binop (MAX_EXPR
, vr0
.min
, vr1
.min
);
2608 max
= vrp_int_const_binop (MIN_EXPR
, vr0
.max
, vr1
.max
);
2612 /* For operations that make the resulting range directly
2613 proportional to the original ranges, apply the operation to
2614 the same end of each range. */
2615 min
= vrp_int_const_binop (code
, vr0
.min
, vr1
.min
);
2616 max
= vrp_int_const_binop (code
, vr0
.max
, vr1
.max
);
2619 else if (code
== MULT_EXPR
)
2621 /* Fancy code so that with unsigned, [-3,-1]*[-3,-1] does not
2623 if (range_int_cst_p (&vr0
)
2624 && range_int_cst_p (&vr1
)
2625 && TYPE_OVERFLOW_WRAPS (expr_type
))
2627 double_int min0
, max0
, min1
, max1
, sizem1
, size
;
2628 double_int prod0l
, prod0h
, prod1l
, prod1h
,
2629 prod2l
, prod2h
, prod3l
, prod3h
;
2630 bool uns0
, uns1
, uns
;
2632 sizem1
= double_int::max_value (TYPE_PRECISION (expr_type
), true);
2633 size
= sizem1
+ double_int_one
;
2635 min0
= tree_to_double_int (vr0
.min
);
2636 max0
= tree_to_double_int (vr0
.max
);
2637 min1
= tree_to_double_int (vr1
.min
);
2638 max1
= tree_to_double_int (vr1
.max
);
2640 uns0
= TYPE_UNSIGNED (expr_type
);
2643 /* Canonicalize the intervals. */
2644 if (TYPE_UNSIGNED (expr_type
))
2646 double_int min2
= size
- min0
;
2647 if (min2
.cmp (max0
, true) < 0)
2655 if (min2
.cmp (max1
, true) < 0)
2665 prod0l
= min0
.wide_mul_with_sign (min1
, true, &prod0h
, &overflow
);
2666 if (!uns0
&& min0
.is_negative ())
2668 if (!uns1
&& min1
.is_negative ())
2671 prod1l
= min0
.wide_mul_with_sign (max1
, true, &prod1h
, &overflow
);
2672 if (!uns0
&& min0
.is_negative ())
2674 if (!uns1
&& max1
.is_negative ())
2677 prod2l
= max0
.wide_mul_with_sign (min1
, true, &prod2h
, &overflow
);
2678 if (!uns0
&& max0
.is_negative ())
2680 if (!uns1
&& min1
.is_negative ())
2683 prod3l
= max0
.wide_mul_with_sign (max1
, true, &prod3h
, &overflow
);
2684 if (!uns0
&& max0
.is_negative ())
2686 if (!uns1
&& max1
.is_negative ())
2689 /* Sort the 4 products. */
2690 quad_int_pair_sort (&prod0l
, &prod0h
, &prod3l
, &prod3h
, uns
);
2691 quad_int_pair_sort (&prod1l
, &prod1h
, &prod2l
, &prod2h
, uns
);
2692 quad_int_pair_sort (&prod0l
, &prod0h
, &prod1l
, &prod1h
, uns
);
2693 quad_int_pair_sort (&prod2l
, &prod2h
, &prod3l
, &prod3h
, uns
);
2696 if (prod0l
.is_zero ())
2698 prod1l
= double_int_zero
;
2706 prod2l
= prod3l
+ prod1l
;
2707 prod2h
= prod3h
+ prod1h
;
2708 if (prod2l
.ult (prod3l
))
2709 prod2h
+= double_int_one
; /* carry */
2711 if (!prod2h
.is_zero ()
2712 || prod2l
.cmp (sizem1
, true) >= 0)
2714 /* the range covers all values. */
2715 set_value_range_to_varying (vr
);
2719 /* The following should handle the wrapping and selecting
2720 VR_ANTI_RANGE for us. */
2721 min
= double_int_to_tree (expr_type
, prod0l
);
2722 max
= double_int_to_tree (expr_type
, prod3l
);
2723 set_and_canonicalize_value_range (vr
, VR_RANGE
, min
, max
, NULL
);
2727 /* If we have an unsigned MULT_EXPR with two VR_ANTI_RANGEs,
2728 drop to VR_VARYING. It would take more effort to compute a
2729 precise range for such a case. For example, if we have
2730 op0 == 65536 and op1 == 65536 with their ranges both being
2731 ~[0,0] on a 32-bit machine, we would have op0 * op1 == 0, so
2732 we cannot claim that the product is in ~[0,0]. Note that we
2733 are guaranteed to have vr0.type == vr1.type at this
2735 if (vr0
.type
== VR_ANTI_RANGE
2736 && !TYPE_OVERFLOW_UNDEFINED (expr_type
))
2738 set_value_range_to_varying (vr
);
2742 extract_range_from_multiplicative_op_1 (vr
, code
, &vr0
, &vr1
);
2745 else if (code
== RSHIFT_EXPR
2746 || code
== LSHIFT_EXPR
)
2748 /* If we have a RSHIFT_EXPR with any shift values outside [0..prec-1],
2749 then drop to VR_VARYING. Outside of this range we get undefined
2750 behavior from the shift operation. We cannot even trust
2751 SHIFT_COUNT_TRUNCATED at this stage, because that applies to rtl
2752 shifts, and the operation at the tree level may be widened. */
2753 if (range_int_cst_p (&vr1
)
2754 && compare_tree_int (vr1
.min
, 0) >= 0
2755 && compare_tree_int (vr1
.max
, TYPE_PRECISION (expr_type
)) == -1)
2757 if (code
== RSHIFT_EXPR
)
2759 extract_range_from_multiplicative_op_1 (vr
, code
, &vr0
, &vr1
);
2762 /* We can map lshifts by constants to MULT_EXPR handling. */
2763 else if (code
== LSHIFT_EXPR
2764 && range_int_cst_singleton_p (&vr1
))
2766 bool saved_flag_wrapv
;
2767 value_range_t vr1p
= VR_INITIALIZER
;
2768 vr1p
.type
= VR_RANGE
;
2770 = double_int_to_tree (expr_type
,
2772 .llshift (TREE_INT_CST_LOW (vr1
.min
),
2773 TYPE_PRECISION (expr_type
)));
2774 vr1p
.max
= vr1p
.min
;
2775 /* We have to use a wrapping multiply though as signed overflow
2776 on lshifts is implementation defined in C89. */
2777 saved_flag_wrapv
= flag_wrapv
;
2779 extract_range_from_binary_expr_1 (vr
, MULT_EXPR
, expr_type
,
2781 flag_wrapv
= saved_flag_wrapv
;
2784 else if (code
== LSHIFT_EXPR
2785 && range_int_cst_p (&vr0
))
2787 int prec
= TYPE_PRECISION (expr_type
);
2788 int overflow_pos
= prec
;
2790 double_int bound
, complement
, low_bound
, high_bound
;
2791 bool uns
= TYPE_UNSIGNED (expr_type
);
2792 bool in_bounds
= false;
2797 bound_shift
= overflow_pos
- TREE_INT_CST_LOW (vr1
.max
);
2798 /* If bound_shift == HOST_BITS_PER_DOUBLE_INT, the llshift can
2799 overflow. However, for that to happen, vr1.max needs to be
2800 zero, which means vr1 is a singleton range of zero, which
2801 means it should be handled by the previous LSHIFT_EXPR
2803 bound
= double_int_one
.llshift (bound_shift
, prec
);
2804 complement
= ~(bound
- double_int_one
);
2809 high_bound
= complement
.zext (prec
);
2810 if (tree_to_double_int (vr0
.max
).ult (low_bound
))
2812 /* [5, 6] << [1, 2] == [10, 24]. */
2813 /* We're shifting out only zeroes, the value increases
2817 else if (high_bound
.ult (tree_to_double_int (vr0
.min
)))
2819 /* [0xffffff00, 0xffffffff] << [1, 2]
2820 == [0xfffffc00, 0xfffffffe]. */
2821 /* We're shifting out only ones, the value decreases
2828 /* [-1, 1] << [1, 2] == [-4, 4]. */
2829 low_bound
= complement
.sext (prec
);
2831 if (tree_to_double_int (vr0
.max
).slt (high_bound
)
2832 && low_bound
.slt (tree_to_double_int (vr0
.min
)))
2834 /* For non-negative numbers, we're shifting out only
2835 zeroes, the value increases monotonically.
2836 For negative numbers, we're shifting out only ones, the
2837 value decreases monotomically. */
2844 extract_range_from_multiplicative_op_1 (vr
, code
, &vr0
, &vr1
);
2849 set_value_range_to_varying (vr
);
2852 else if (code
== TRUNC_DIV_EXPR
2853 || code
== FLOOR_DIV_EXPR
2854 || code
== CEIL_DIV_EXPR
2855 || code
== EXACT_DIV_EXPR
2856 || code
== ROUND_DIV_EXPR
)
2858 if (vr0
.type
!= VR_RANGE
|| symbolic_range_p (&vr0
))
2860 /* For division, if op1 has VR_RANGE but op0 does not, something
2861 can be deduced just from that range. Say [min, max] / [4, max]
2862 gives [min / 4, max / 4] range. */
2863 if (vr1
.type
== VR_RANGE
2864 && !symbolic_range_p (&vr1
)
2865 && range_includes_zero_p (vr1
.min
, vr1
.max
) == 0)
2867 vr0
.type
= type
= VR_RANGE
;
2868 vr0
.min
= vrp_val_min (expr_type
);
2869 vr0
.max
= vrp_val_max (expr_type
);
2873 set_value_range_to_varying (vr
);
2878 /* For divisions, if flag_non_call_exceptions is true, we must
2879 not eliminate a division by zero. */
2880 if (cfun
->can_throw_non_call_exceptions
2881 && (vr1
.type
!= VR_RANGE
2882 || range_includes_zero_p (vr1
.min
, vr1
.max
) != 0))
2884 set_value_range_to_varying (vr
);
2888 /* For divisions, if op0 is VR_RANGE, we can deduce a range
2889 even if op1 is VR_VARYING, VR_ANTI_RANGE, symbolic or can
2891 if (vr0
.type
== VR_RANGE
2892 && (vr1
.type
!= VR_RANGE
2893 || range_includes_zero_p (vr1
.min
, vr1
.max
) != 0))
2895 tree zero
= build_int_cst (TREE_TYPE (vr0
.min
), 0);
2900 if (TYPE_UNSIGNED (expr_type
)
2901 || value_range_nonnegative_p (&vr1
))
2903 /* For unsigned division or when divisor is known
2904 to be non-negative, the range has to cover
2905 all numbers from 0 to max for positive max
2906 and all numbers from min to 0 for negative min. */
2907 cmp
= compare_values (vr0
.max
, zero
);
2910 else if (cmp
== 0 || cmp
== 1)
2914 cmp
= compare_values (vr0
.min
, zero
);
2917 else if (cmp
== 0 || cmp
== -1)
2924 /* Otherwise the range is -max .. max or min .. -min
2925 depending on which bound is bigger in absolute value,
2926 as the division can change the sign. */
2927 abs_extent_range (vr
, vr0
.min
, vr0
.max
);
2930 if (type
== VR_VARYING
)
2932 set_value_range_to_varying (vr
);
2938 extract_range_from_multiplicative_op_1 (vr
, code
, &vr0
, &vr1
);
2942 else if (code
== TRUNC_MOD_EXPR
)
2944 if (vr1
.type
!= VR_RANGE
2945 || range_includes_zero_p (vr1
.min
, vr1
.max
) != 0
2946 || vrp_val_is_min (vr1
.min
))
2948 set_value_range_to_varying (vr
);
2952 /* Compute MAX <|vr1.min|, |vr1.max|> - 1. */
2953 max
= fold_unary_to_constant (ABS_EXPR
, expr_type
, vr1
.min
);
2954 if (tree_int_cst_lt (max
, vr1
.max
))
2956 max
= int_const_binop (MINUS_EXPR
, max
, integer_one_node
);
2957 /* If the dividend is non-negative the modulus will be
2958 non-negative as well. */
2959 if (TYPE_UNSIGNED (expr_type
)
2960 || value_range_nonnegative_p (&vr0
))
2961 min
= build_int_cst (TREE_TYPE (max
), 0);
2963 min
= fold_unary_to_constant (NEGATE_EXPR
, expr_type
, max
);
2965 else if (code
== BIT_AND_EXPR
|| code
== BIT_IOR_EXPR
|| code
== BIT_XOR_EXPR
)
2967 bool int_cst_range0
, int_cst_range1
;
2968 double_int may_be_nonzero0
, may_be_nonzero1
;
2969 double_int must_be_nonzero0
, must_be_nonzero1
;
2971 int_cst_range0
= zero_nonzero_bits_from_vr (&vr0
, &may_be_nonzero0
,
2973 int_cst_range1
= zero_nonzero_bits_from_vr (&vr1
, &may_be_nonzero1
,
2977 if (code
== BIT_AND_EXPR
)
2980 min
= double_int_to_tree (expr_type
,
2981 must_be_nonzero0
& must_be_nonzero1
);
2982 dmax
= may_be_nonzero0
& may_be_nonzero1
;
2983 /* If both input ranges contain only negative values we can
2984 truncate the result range maximum to the minimum of the
2985 input range maxima. */
2986 if (int_cst_range0
&& int_cst_range1
2987 && tree_int_cst_sgn (vr0
.max
) < 0
2988 && tree_int_cst_sgn (vr1
.max
) < 0)
2990 dmax
= dmax
.min (tree_to_double_int (vr0
.max
),
2991 TYPE_UNSIGNED (expr_type
));
2992 dmax
= dmax
.min (tree_to_double_int (vr1
.max
),
2993 TYPE_UNSIGNED (expr_type
));
2995 /* If either input range contains only non-negative values
2996 we can truncate the result range maximum to the respective
2997 maximum of the input range. */
2998 if (int_cst_range0
&& tree_int_cst_sgn (vr0
.min
) >= 0)
2999 dmax
= dmax
.min (tree_to_double_int (vr0
.max
),
3000 TYPE_UNSIGNED (expr_type
));
3001 if (int_cst_range1
&& tree_int_cst_sgn (vr1
.min
) >= 0)
3002 dmax
= dmax
.min (tree_to_double_int (vr1
.max
),
3003 TYPE_UNSIGNED (expr_type
));
3004 max
= double_int_to_tree (expr_type
, dmax
);
3006 else if (code
== BIT_IOR_EXPR
)
3009 max
= double_int_to_tree (expr_type
,
3010 may_be_nonzero0
| may_be_nonzero1
);
3011 dmin
= must_be_nonzero0
| must_be_nonzero1
;
3012 /* If the input ranges contain only positive values we can
3013 truncate the minimum of the result range to the maximum
3014 of the input range minima. */
3015 if (int_cst_range0
&& int_cst_range1
3016 && tree_int_cst_sgn (vr0
.min
) >= 0
3017 && tree_int_cst_sgn (vr1
.min
) >= 0)
3019 dmin
= dmin
.max (tree_to_double_int (vr0
.min
),
3020 TYPE_UNSIGNED (expr_type
));
3021 dmin
= dmin
.max (tree_to_double_int (vr1
.min
),
3022 TYPE_UNSIGNED (expr_type
));
3024 /* If either input range contains only negative values
3025 we can truncate the minimum of the result range to the
3026 respective minimum range. */
3027 if (int_cst_range0
&& tree_int_cst_sgn (vr0
.max
) < 0)
3028 dmin
= dmin
.max (tree_to_double_int (vr0
.min
),
3029 TYPE_UNSIGNED (expr_type
));
3030 if (int_cst_range1
&& tree_int_cst_sgn (vr1
.max
) < 0)
3031 dmin
= dmin
.max (tree_to_double_int (vr1
.min
),
3032 TYPE_UNSIGNED (expr_type
));
3033 min
= double_int_to_tree (expr_type
, dmin
);
3035 else if (code
== BIT_XOR_EXPR
)
3037 double_int result_zero_bits
, result_one_bits
;
3038 result_zero_bits
= (must_be_nonzero0
& must_be_nonzero1
)
3039 | ~(may_be_nonzero0
| may_be_nonzero1
);
3040 result_one_bits
= must_be_nonzero0
.and_not (may_be_nonzero1
)
3041 | must_be_nonzero1
.and_not (may_be_nonzero0
);
3042 max
= double_int_to_tree (expr_type
, ~result_zero_bits
);
3043 min
= double_int_to_tree (expr_type
, result_one_bits
);
3044 /* If the range has all positive or all negative values the
3045 result is better than VARYING. */
3046 if (tree_int_cst_sgn (min
) < 0
3047 || tree_int_cst_sgn (max
) >= 0)
3050 max
= min
= NULL_TREE
;
3056 /* If either MIN or MAX overflowed, then set the resulting range to
3057 VARYING. But we do accept an overflow infinity
3059 if (min
== NULL_TREE
3060 || !is_gimple_min_invariant (min
)
3061 || (TREE_OVERFLOW (min
) && !is_overflow_infinity (min
))
3063 || !is_gimple_min_invariant (max
)
3064 || (TREE_OVERFLOW (max
) && !is_overflow_infinity (max
)))
3066 set_value_range_to_varying (vr
);
3072 2) [-INF, +-INF(OVF)]
3073 3) [+-INF(OVF), +INF]
3074 4) [+-INF(OVF), +-INF(OVF)]
3075 We learn nothing when we have INF and INF(OVF) on both sides.
3076 Note that we do accept [-INF, -INF] and [+INF, +INF] without
3078 if ((vrp_val_is_min (min
) || is_overflow_infinity (min
))
3079 && (vrp_val_is_max (max
) || is_overflow_infinity (max
)))
3081 set_value_range_to_varying (vr
);
3085 cmp
= compare_values (min
, max
);
3086 if (cmp
== -2 || cmp
== 1)
3088 /* If the new range has its limits swapped around (MIN > MAX),
3089 then the operation caused one of them to wrap around, mark
3090 the new range VARYING. */
3091 set_value_range_to_varying (vr
);
3094 set_value_range (vr
, type
, min
, max
, NULL
);
3097 /* Extract range information from a binary expression OP0 CODE OP1 based on
3098 the ranges of each of its operands with resulting type EXPR_TYPE.
3099 The resulting range is stored in *VR. */
3102 extract_range_from_binary_expr (value_range_t
*vr
,
3103 enum tree_code code
,
3104 tree expr_type
, tree op0
, tree op1
)
3106 value_range_t vr0
= VR_INITIALIZER
;
3107 value_range_t vr1
= VR_INITIALIZER
;
3109 /* Get value ranges for each operand. For constant operands, create
3110 a new value range with the operand to simplify processing. */
3111 if (TREE_CODE (op0
) == SSA_NAME
)
3112 vr0
= *(get_value_range (op0
));
3113 else if (is_gimple_min_invariant (op0
))
3114 set_value_range_to_value (&vr0
, op0
, NULL
);
3116 set_value_range_to_varying (&vr0
);
3118 if (TREE_CODE (op1
) == SSA_NAME
)
3119 vr1
= *(get_value_range (op1
));
3120 else if (is_gimple_min_invariant (op1
))
3121 set_value_range_to_value (&vr1
, op1
, NULL
);
3123 set_value_range_to_varying (&vr1
);
3125 extract_range_from_binary_expr_1 (vr
, code
, expr_type
, &vr0
, &vr1
);
3128 /* Extract range information from a unary operation CODE based on
3129 the range of its operand *VR0 with type OP0_TYPE with resulting type TYPE.
3130 The The resulting range is stored in *VR. */
3133 extract_range_from_unary_expr_1 (value_range_t
*vr
,
3134 enum tree_code code
, tree type
,
3135 value_range_t
*vr0_
, tree op0_type
)
3137 value_range_t vr0
= *vr0_
, vrtem0
= VR_INITIALIZER
, vrtem1
= VR_INITIALIZER
;
3139 /* VRP only operates on integral and pointer types. */
3140 if (!(INTEGRAL_TYPE_P (op0_type
)
3141 || POINTER_TYPE_P (op0_type
))
3142 || !(INTEGRAL_TYPE_P (type
)
3143 || POINTER_TYPE_P (type
)))
3145 set_value_range_to_varying (vr
);
3149 /* If VR0 is UNDEFINED, so is the result. */
3150 if (vr0
.type
== VR_UNDEFINED
)
3152 set_value_range_to_undefined (vr
);
3156 /* Handle operations that we express in terms of others. */
3157 if (code
== PAREN_EXPR
)
3159 /* PAREN_EXPR is a simple copy. */
3160 copy_value_range (vr
, &vr0
);
3163 else if (code
== NEGATE_EXPR
)
3165 /* -X is simply 0 - X, so re-use existing code that also handles
3166 anti-ranges fine. */
3167 value_range_t zero
= VR_INITIALIZER
;
3168 set_value_range_to_value (&zero
, build_int_cst (type
, 0), NULL
);
3169 extract_range_from_binary_expr_1 (vr
, MINUS_EXPR
, type
, &zero
, &vr0
);
3172 else if (code
== BIT_NOT_EXPR
)
3174 /* ~X is simply -1 - X, so re-use existing code that also handles
3175 anti-ranges fine. */
3176 value_range_t minusone
= VR_INITIALIZER
;
3177 set_value_range_to_value (&minusone
, build_int_cst (type
, -1), NULL
);
3178 extract_range_from_binary_expr_1 (vr
, MINUS_EXPR
,
3179 type
, &minusone
, &vr0
);
3183 /* Now canonicalize anti-ranges to ranges when they are not symbolic
3184 and express op ~[] as (op []') U (op []''). */
3185 if (vr0
.type
== VR_ANTI_RANGE
3186 && ranges_from_anti_range (&vr0
, &vrtem0
, &vrtem1
))
3188 extract_range_from_unary_expr_1 (vr
, code
, type
, &vrtem0
, op0_type
);
3189 if (vrtem1
.type
!= VR_UNDEFINED
)
3191 value_range_t vrres
= VR_INITIALIZER
;
3192 extract_range_from_unary_expr_1 (&vrres
, code
, type
,
3194 vrp_meet (vr
, &vrres
);
3199 if (CONVERT_EXPR_CODE_P (code
))
3201 tree inner_type
= op0_type
;
3202 tree outer_type
= type
;
3204 /* If the expression evaluates to a pointer, we are only interested in
3205 determining if it evaluates to NULL [0, 0] or non-NULL (~[0, 0]). */
3206 if (POINTER_TYPE_P (type
))
3208 if (range_is_nonnull (&vr0
))
3209 set_value_range_to_nonnull (vr
, type
);
3210 else if (range_is_null (&vr0
))
3211 set_value_range_to_null (vr
, type
);
3213 set_value_range_to_varying (vr
);
3217 /* If VR0 is varying and we increase the type precision, assume
3218 a full range for the following transformation. */
3219 if (vr0
.type
== VR_VARYING
3220 && INTEGRAL_TYPE_P (inner_type
)
3221 && TYPE_PRECISION (inner_type
) < TYPE_PRECISION (outer_type
))
3223 vr0
.type
= VR_RANGE
;
3224 vr0
.min
= TYPE_MIN_VALUE (inner_type
);
3225 vr0
.max
= TYPE_MAX_VALUE (inner_type
);
3228 /* If VR0 is a constant range or anti-range and the conversion is
3229 not truncating we can convert the min and max values and
3230 canonicalize the resulting range. Otherwise we can do the
3231 conversion if the size of the range is less than what the
3232 precision of the target type can represent and the range is
3233 not an anti-range. */
3234 if ((vr0
.type
== VR_RANGE
3235 || vr0
.type
== VR_ANTI_RANGE
)
3236 && TREE_CODE (vr0
.min
) == INTEGER_CST
3237 && TREE_CODE (vr0
.max
) == INTEGER_CST
3238 && (!is_overflow_infinity (vr0
.min
)
3239 || (vr0
.type
== VR_RANGE
3240 && TYPE_PRECISION (outer_type
) > TYPE_PRECISION (inner_type
)
3241 && needs_overflow_infinity (outer_type
)
3242 && supports_overflow_infinity (outer_type
)))
3243 && (!is_overflow_infinity (vr0
.max
)
3244 || (vr0
.type
== VR_RANGE
3245 && TYPE_PRECISION (outer_type
) > TYPE_PRECISION (inner_type
)
3246 && needs_overflow_infinity (outer_type
)
3247 && supports_overflow_infinity (outer_type
)))
3248 && (TYPE_PRECISION (outer_type
) >= TYPE_PRECISION (inner_type
)
3249 || (vr0
.type
== VR_RANGE
3250 && integer_zerop (int_const_binop (RSHIFT_EXPR
,
3251 int_const_binop (MINUS_EXPR
, vr0
.max
, vr0
.min
),
3252 size_int (TYPE_PRECISION (outer_type
)))))))
3254 tree new_min
, new_max
;
3255 if (is_overflow_infinity (vr0
.min
))
3256 new_min
= negative_overflow_infinity (outer_type
);
3258 new_min
= force_fit_type_double (outer_type
,
3259 tree_to_double_int (vr0
.min
),
3261 if (is_overflow_infinity (vr0
.max
))
3262 new_max
= positive_overflow_infinity (outer_type
);
3264 new_max
= force_fit_type_double (outer_type
,
3265 tree_to_double_int (vr0
.max
),
3267 set_and_canonicalize_value_range (vr
, vr0
.type
,
3268 new_min
, new_max
, NULL
);
3272 set_value_range_to_varying (vr
);
3275 else if (code
== ABS_EXPR
)
3280 /* Pass through vr0 in the easy cases. */
3281 if (TYPE_UNSIGNED (type
)
3282 || value_range_nonnegative_p (&vr0
))
3284 copy_value_range (vr
, &vr0
);
3288 /* For the remaining varying or symbolic ranges we can't do anything
3290 if (vr0
.type
== VR_VARYING
3291 || symbolic_range_p (&vr0
))
3293 set_value_range_to_varying (vr
);
3297 /* -TYPE_MIN_VALUE = TYPE_MIN_VALUE with flag_wrapv so we can't get a
3299 if (!TYPE_OVERFLOW_UNDEFINED (type
)
3300 && ((vr0
.type
== VR_RANGE
3301 && vrp_val_is_min (vr0
.min
))
3302 || (vr0
.type
== VR_ANTI_RANGE
3303 && !vrp_val_is_min (vr0
.min
))))
3305 set_value_range_to_varying (vr
);
3309 /* ABS_EXPR may flip the range around, if the original range
3310 included negative values. */
3311 if (is_overflow_infinity (vr0
.min
))
3312 min
= positive_overflow_infinity (type
);
3313 else if (!vrp_val_is_min (vr0
.min
))
3314 min
= fold_unary_to_constant (code
, type
, vr0
.min
);
3315 else if (!needs_overflow_infinity (type
))
3316 min
= TYPE_MAX_VALUE (type
);
3317 else if (supports_overflow_infinity (type
))
3318 min
= positive_overflow_infinity (type
);
3321 set_value_range_to_varying (vr
);
3325 if (is_overflow_infinity (vr0
.max
))
3326 max
= positive_overflow_infinity (type
);
3327 else if (!vrp_val_is_min (vr0
.max
))
3328 max
= fold_unary_to_constant (code
, type
, vr0
.max
);
3329 else if (!needs_overflow_infinity (type
))
3330 max
= TYPE_MAX_VALUE (type
);
3331 else if (supports_overflow_infinity (type
)
3332 /* We shouldn't generate [+INF, +INF] as set_value_range
3333 doesn't like this and ICEs. */
3334 && !is_positive_overflow_infinity (min
))
3335 max
= positive_overflow_infinity (type
);
3338 set_value_range_to_varying (vr
);
3342 cmp
= compare_values (min
, max
);
3344 /* If a VR_ANTI_RANGEs contains zero, then we have
3345 ~[-INF, min(MIN, MAX)]. */
3346 if (vr0
.type
== VR_ANTI_RANGE
)
3348 if (range_includes_zero_p (vr0
.min
, vr0
.max
) == 1)
3350 /* Take the lower of the two values. */
3354 /* Create ~[-INF, min (abs(MIN), abs(MAX))]
3355 or ~[-INF + 1, min (abs(MIN), abs(MAX))] when
3356 flag_wrapv is set and the original anti-range doesn't include
3357 TYPE_MIN_VALUE, remember -TYPE_MIN_VALUE = TYPE_MIN_VALUE. */
3358 if (TYPE_OVERFLOW_WRAPS (type
))
3360 tree type_min_value
= TYPE_MIN_VALUE (type
);
3362 min
= (vr0
.min
!= type_min_value
3363 ? int_const_binop (PLUS_EXPR
, type_min_value
,
3369 if (overflow_infinity_range_p (&vr0
))
3370 min
= negative_overflow_infinity (type
);
3372 min
= TYPE_MIN_VALUE (type
);
3377 /* All else has failed, so create the range [0, INF], even for
3378 flag_wrapv since TYPE_MIN_VALUE is in the original
3380 vr0
.type
= VR_RANGE
;
3381 min
= build_int_cst (type
, 0);
3382 if (needs_overflow_infinity (type
))
3384 if (supports_overflow_infinity (type
))
3385 max
= positive_overflow_infinity (type
);
3388 set_value_range_to_varying (vr
);
3393 max
= TYPE_MAX_VALUE (type
);
3397 /* If the range contains zero then we know that the minimum value in the
3398 range will be zero. */
3399 else if (range_includes_zero_p (vr0
.min
, vr0
.max
) == 1)
3403 min
= build_int_cst (type
, 0);
3407 /* If the range was reversed, swap MIN and MAX. */
3416 cmp
= compare_values (min
, max
);
3417 if (cmp
== -2 || cmp
== 1)
3419 /* If the new range has its limits swapped around (MIN > MAX),
3420 then the operation caused one of them to wrap around, mark
3421 the new range VARYING. */
3422 set_value_range_to_varying (vr
);
3425 set_value_range (vr
, vr0
.type
, min
, max
, NULL
);
3429 /* For unhandled operations fall back to varying. */
3430 set_value_range_to_varying (vr
);
3435 /* Extract range information from a unary expression CODE OP0 based on
3436 the range of its operand with resulting type TYPE.
3437 The resulting range is stored in *VR. */
3440 extract_range_from_unary_expr (value_range_t
*vr
, enum tree_code code
,
3441 tree type
, tree op0
)
3443 value_range_t vr0
= VR_INITIALIZER
;
3445 /* Get value ranges for the operand. For constant operands, create
3446 a new value range with the operand to simplify processing. */
3447 if (TREE_CODE (op0
) == SSA_NAME
)
3448 vr0
= *(get_value_range (op0
));
3449 else if (is_gimple_min_invariant (op0
))
3450 set_value_range_to_value (&vr0
, op0
, NULL
);
3452 set_value_range_to_varying (&vr0
);
3454 extract_range_from_unary_expr_1 (vr
, code
, type
, &vr0
, TREE_TYPE (op0
));
3458 /* Extract range information from a conditional expression STMT based on
3459 the ranges of each of its operands and the expression code. */
3462 extract_range_from_cond_expr (value_range_t
*vr
, gimple stmt
)
3465 value_range_t vr0
= VR_INITIALIZER
;
3466 value_range_t vr1
= VR_INITIALIZER
;
3468 /* Get value ranges for each operand. For constant operands, create
3469 a new value range with the operand to simplify processing. */
3470 op0
= gimple_assign_rhs2 (stmt
);
3471 if (TREE_CODE (op0
) == SSA_NAME
)
3472 vr0
= *(get_value_range (op0
));
3473 else if (is_gimple_min_invariant (op0
))
3474 set_value_range_to_value (&vr0
, op0
, NULL
);
3476 set_value_range_to_varying (&vr0
);
3478 op1
= gimple_assign_rhs3 (stmt
);
3479 if (TREE_CODE (op1
) == SSA_NAME
)
3480 vr1
= *(get_value_range (op1
));
3481 else if (is_gimple_min_invariant (op1
))
3482 set_value_range_to_value (&vr1
, op1
, NULL
);
3484 set_value_range_to_varying (&vr1
);
3486 /* The resulting value range is the union of the operand ranges */
3487 copy_value_range (vr
, &vr0
);
3488 vrp_meet (vr
, &vr1
);
3492 /* Extract range information from a comparison expression EXPR based
3493 on the range of its operand and the expression code. */
3496 extract_range_from_comparison (value_range_t
*vr
, enum tree_code code
,
3497 tree type
, tree op0
, tree op1
)
3502 val
= vrp_evaluate_conditional_warnv_with_ops (code
, op0
, op1
, false, &sop
,
3505 /* A disadvantage of using a special infinity as an overflow
3506 representation is that we lose the ability to record overflow
3507 when we don't have an infinity. So we have to ignore a result
3508 which relies on overflow. */
3510 if (val
&& !is_overflow_infinity (val
) && !sop
)
3512 /* Since this expression was found on the RHS of an assignment,
3513 its type may be different from _Bool. Convert VAL to EXPR's
3515 val
= fold_convert (type
, val
);
3516 if (is_gimple_min_invariant (val
))
3517 set_value_range_to_value (vr
, val
, vr
->equiv
);
3519 set_value_range (vr
, VR_RANGE
, val
, val
, vr
->equiv
);
3522 /* The result of a comparison is always true or false. */
3523 set_value_range_to_truthvalue (vr
, type
);
3526 /* Try to derive a nonnegative or nonzero range out of STMT relying
3527 primarily on generic routines in fold in conjunction with range data.
3528 Store the result in *VR */
3531 extract_range_basic (value_range_t
*vr
, gimple stmt
)
3534 tree type
= gimple_expr_type (stmt
);
3536 if (INTEGRAL_TYPE_P (type
)
3537 && gimple_stmt_nonnegative_warnv_p (stmt
, &sop
))
3538 set_value_range_to_nonnegative (vr
, type
,
3539 sop
|| stmt_overflow_infinity (stmt
));
3540 else if (vrp_stmt_computes_nonzero (stmt
, &sop
)
3542 set_value_range_to_nonnull (vr
, type
);
3544 set_value_range_to_varying (vr
);
3548 /* Try to compute a useful range out of assignment STMT and store it
3552 extract_range_from_assignment (value_range_t
*vr
, gimple stmt
)
3554 enum tree_code code
= gimple_assign_rhs_code (stmt
);
3556 if (code
== ASSERT_EXPR
)
3557 extract_range_from_assert (vr
, gimple_assign_rhs1 (stmt
));
3558 else if (code
== SSA_NAME
)
3559 extract_range_from_ssa_name (vr
, gimple_assign_rhs1 (stmt
));
3560 else if (TREE_CODE_CLASS (code
) == tcc_binary
)
3561 extract_range_from_binary_expr (vr
, gimple_assign_rhs_code (stmt
),
3562 gimple_expr_type (stmt
),
3563 gimple_assign_rhs1 (stmt
),
3564 gimple_assign_rhs2 (stmt
));
3565 else if (TREE_CODE_CLASS (code
) == tcc_unary
)
3566 extract_range_from_unary_expr (vr
, gimple_assign_rhs_code (stmt
),
3567 gimple_expr_type (stmt
),
3568 gimple_assign_rhs1 (stmt
));
3569 else if (code
== COND_EXPR
)
3570 extract_range_from_cond_expr (vr
, stmt
);
3571 else if (TREE_CODE_CLASS (code
) == tcc_comparison
)
3572 extract_range_from_comparison (vr
, gimple_assign_rhs_code (stmt
),
3573 gimple_expr_type (stmt
),
3574 gimple_assign_rhs1 (stmt
),
3575 gimple_assign_rhs2 (stmt
));
3576 else if (get_gimple_rhs_class (code
) == GIMPLE_SINGLE_RHS
3577 && is_gimple_min_invariant (gimple_assign_rhs1 (stmt
)))
3578 set_value_range_to_value (vr
, gimple_assign_rhs1 (stmt
), NULL
);
3580 set_value_range_to_varying (vr
);
3582 if (vr
->type
== VR_VARYING
)
3583 extract_range_basic (vr
, stmt
);
3586 /* Given a range VR, a LOOP and a variable VAR, determine whether it
3587 would be profitable to adjust VR using scalar evolution information
3588 for VAR. If so, update VR with the new limits. */
3591 adjust_range_with_scev (value_range_t
*vr
, struct loop
*loop
,
3592 gimple stmt
, tree var
)
3594 tree init
, step
, chrec
, tmin
, tmax
, min
, max
, type
, tem
;
3595 enum ev_direction dir
;
3597 /* TODO. Don't adjust anti-ranges. An anti-range may provide
3598 better opportunities than a regular range, but I'm not sure. */
3599 if (vr
->type
== VR_ANTI_RANGE
)
3602 chrec
= instantiate_parameters (loop
, analyze_scalar_evolution (loop
, var
));
3604 /* Like in PR19590, scev can return a constant function. */
3605 if (is_gimple_min_invariant (chrec
))
3607 set_value_range_to_value (vr
, chrec
, vr
->equiv
);
3611 if (TREE_CODE (chrec
) != POLYNOMIAL_CHREC
)
3614 init
= initial_condition_in_loop_num (chrec
, loop
->num
);
3615 tem
= op_with_constant_singleton_value_range (init
);
3618 step
= evolution_part_in_loop_num (chrec
, loop
->num
);
3619 tem
= op_with_constant_singleton_value_range (step
);
3623 /* If STEP is symbolic, we can't know whether INIT will be the
3624 minimum or maximum value in the range. Also, unless INIT is
3625 a simple expression, compare_values and possibly other functions
3626 in tree-vrp won't be able to handle it. */
3627 if (step
== NULL_TREE
3628 || !is_gimple_min_invariant (step
)
3629 || !valid_value_p (init
))
3632 dir
= scev_direction (chrec
);
3633 if (/* Do not adjust ranges if we do not know whether the iv increases
3634 or decreases, ... */
3635 dir
== EV_DIR_UNKNOWN
3636 /* ... or if it may wrap. */
3637 || scev_probably_wraps_p (init
, step
, stmt
, get_chrec_loop (chrec
),
3641 /* We use TYPE_MIN_VALUE and TYPE_MAX_VALUE here instead of
3642 negative_overflow_infinity and positive_overflow_infinity,
3643 because we have concluded that the loop probably does not
3646 type
= TREE_TYPE (var
);
3647 if (POINTER_TYPE_P (type
) || !TYPE_MIN_VALUE (type
))
3648 tmin
= lower_bound_in_type (type
, type
);
3650 tmin
= TYPE_MIN_VALUE (type
);
3651 if (POINTER_TYPE_P (type
) || !TYPE_MAX_VALUE (type
))
3652 tmax
= upper_bound_in_type (type
, type
);
3654 tmax
= TYPE_MAX_VALUE (type
);
3656 /* Try to use estimated number of iterations for the loop to constrain the
3657 final value in the evolution. */
3658 if (TREE_CODE (step
) == INTEGER_CST
3659 && is_gimple_val (init
)
3660 && (TREE_CODE (init
) != SSA_NAME
3661 || get_value_range (init
)->type
== VR_RANGE
))
3665 /* We are only entering here for loop header PHI nodes, so using
3666 the number of latch executions is the correct thing to use. */
3667 if (max_loop_iterations (loop
, &nit
))
3669 value_range_t maxvr
= VR_INITIALIZER
;
3671 bool unsigned_p
= TYPE_UNSIGNED (TREE_TYPE (step
));
3672 bool overflow
= false;
3674 dtmp
= tree_to_double_int (step
)
3675 .mul_with_sign (nit
, unsigned_p
, &overflow
);
3676 /* If the multiplication overflowed we can't do a meaningful
3677 adjustment. Likewise if the result doesn't fit in the type
3678 of the induction variable. For a signed type we have to
3679 check whether the result has the expected signedness which
3680 is that of the step as number of iterations is unsigned. */
3682 && double_int_fits_to_tree_p (TREE_TYPE (init
), dtmp
)
3684 || ((dtmp
.high
^ TREE_INT_CST_HIGH (step
)) >= 0)))
3686 tem
= double_int_to_tree (TREE_TYPE (init
), dtmp
);
3687 extract_range_from_binary_expr (&maxvr
, PLUS_EXPR
,
3688 TREE_TYPE (init
), init
, tem
);
3689 /* Likewise if the addition did. */
3690 if (maxvr
.type
== VR_RANGE
)
3699 if (vr
->type
== VR_VARYING
|| vr
->type
== VR_UNDEFINED
)
3704 /* For VARYING or UNDEFINED ranges, just about anything we get
3705 from scalar evolutions should be better. */
3707 if (dir
== EV_DIR_DECREASES
)
3712 /* If we would create an invalid range, then just assume we
3713 know absolutely nothing. This may be over-conservative,
3714 but it's clearly safe, and should happen only in unreachable
3715 parts of code, or for invalid programs. */
3716 if (compare_values (min
, max
) == 1)
3719 set_value_range (vr
, VR_RANGE
, min
, max
, vr
->equiv
);
3721 else if (vr
->type
== VR_RANGE
)
3726 if (dir
== EV_DIR_DECREASES
)
3728 /* INIT is the maximum value. If INIT is lower than VR->MAX
3729 but no smaller than VR->MIN, set VR->MAX to INIT. */
3730 if (compare_values (init
, max
) == -1)
3733 /* According to the loop information, the variable does not
3734 overflow. If we think it does, probably because of an
3735 overflow due to arithmetic on a different INF value,
3737 if (is_negative_overflow_infinity (min
)
3738 || compare_values (min
, tmin
) == -1)
3744 /* If INIT is bigger than VR->MIN, set VR->MIN to INIT. */
3745 if (compare_values (init
, min
) == 1)
3748 if (is_positive_overflow_infinity (max
)
3749 || compare_values (tmax
, max
) == -1)
3753 /* If we just created an invalid range with the minimum
3754 greater than the maximum, we fail conservatively.
3755 This should happen only in unreachable
3756 parts of code, or for invalid programs. */
3757 if (compare_values (min
, max
) == 1)
3760 set_value_range (vr
, VR_RANGE
, min
, max
, vr
->equiv
);
3764 /* Return true if VAR may overflow at STMT. This checks any available
3765 loop information to see if we can determine that VAR does not
3769 vrp_var_may_overflow (tree var
, gimple stmt
)
3772 tree chrec
, init
, step
;
3774 if (current_loops
== NULL
)
3777 l
= loop_containing_stmt (stmt
);
3782 chrec
= instantiate_parameters (l
, analyze_scalar_evolution (l
, var
));
3783 if (TREE_CODE (chrec
) != POLYNOMIAL_CHREC
)
3786 init
= initial_condition_in_loop_num (chrec
, l
->num
);
3787 step
= evolution_part_in_loop_num (chrec
, l
->num
);
3789 if (step
== NULL_TREE
3790 || !is_gimple_min_invariant (step
)
3791 || !valid_value_p (init
))
3794 /* If we get here, we know something useful about VAR based on the
3795 loop information. If it wraps, it may overflow. */
3797 if (scev_probably_wraps_p (init
, step
, stmt
, get_chrec_loop (chrec
),
3801 if (dump_file
&& (dump_flags
& TDF_DETAILS
) != 0)
3803 print_generic_expr (dump_file
, var
, 0);
3804 fprintf (dump_file
, ": loop information indicates does not overflow\n");
3811 /* Given two numeric value ranges VR0, VR1 and a comparison code COMP:
3813 - Return BOOLEAN_TRUE_NODE if VR0 COMP VR1 always returns true for
3814 all the values in the ranges.
3816 - Return BOOLEAN_FALSE_NODE if the comparison always returns false.
3818 - Return NULL_TREE if it is not always possible to determine the
3819 value of the comparison.
3821 Also set *STRICT_OVERFLOW_P to indicate whether a range with an
3822 overflow infinity was used in the test. */
3826 compare_ranges (enum tree_code comp
, value_range_t
*vr0
, value_range_t
*vr1
,
3827 bool *strict_overflow_p
)
3829 /* VARYING or UNDEFINED ranges cannot be compared. */
3830 if (vr0
->type
== VR_VARYING
3831 || vr0
->type
== VR_UNDEFINED
3832 || vr1
->type
== VR_VARYING
3833 || vr1
->type
== VR_UNDEFINED
)
3836 /* Anti-ranges need to be handled separately. */
3837 if (vr0
->type
== VR_ANTI_RANGE
|| vr1
->type
== VR_ANTI_RANGE
)
3839 /* If both are anti-ranges, then we cannot compute any
3841 if (vr0
->type
== VR_ANTI_RANGE
&& vr1
->type
== VR_ANTI_RANGE
)
3844 /* These comparisons are never statically computable. */
3851 /* Equality can be computed only between a range and an
3852 anti-range. ~[VAL1, VAL2] == [VAL1, VAL2] is always false. */
3853 if (vr0
->type
== VR_RANGE
)
3855 /* To simplify processing, make VR0 the anti-range. */
3856 value_range_t
*tmp
= vr0
;
3861 gcc_assert (comp
== NE_EXPR
|| comp
== EQ_EXPR
);
3863 if (compare_values_warnv (vr0
->min
, vr1
->min
, strict_overflow_p
) == 0
3864 && compare_values_warnv (vr0
->max
, vr1
->max
, strict_overflow_p
) == 0)
3865 return (comp
== NE_EXPR
) ? boolean_true_node
: boolean_false_node
;
3870 if (!usable_range_p (vr0
, strict_overflow_p
)
3871 || !usable_range_p (vr1
, strict_overflow_p
))
3874 /* Simplify processing. If COMP is GT_EXPR or GE_EXPR, switch the
3875 operands around and change the comparison code. */
3876 if (comp
== GT_EXPR
|| comp
== GE_EXPR
)
3879 comp
= (comp
== GT_EXPR
) ? LT_EXPR
: LE_EXPR
;
3885 if (comp
== EQ_EXPR
)
3887 /* Equality may only be computed if both ranges represent
3888 exactly one value. */
3889 if (compare_values_warnv (vr0
->min
, vr0
->max
, strict_overflow_p
) == 0
3890 && compare_values_warnv (vr1
->min
, vr1
->max
, strict_overflow_p
) == 0)
3892 int cmp_min
= compare_values_warnv (vr0
->min
, vr1
->min
,
3894 int cmp_max
= compare_values_warnv (vr0
->max
, vr1
->max
,
3896 if (cmp_min
== 0 && cmp_max
== 0)
3897 return boolean_true_node
;
3898 else if (cmp_min
!= -2 && cmp_max
!= -2)
3899 return boolean_false_node
;
3901 /* If [V0_MIN, V1_MAX] < [V1_MIN, V1_MAX] then V0 != V1. */
3902 else if (compare_values_warnv (vr0
->min
, vr1
->max
,
3903 strict_overflow_p
) == 1
3904 || compare_values_warnv (vr1
->min
, vr0
->max
,
3905 strict_overflow_p
) == 1)
3906 return boolean_false_node
;
3910 else if (comp
== NE_EXPR
)
3914 /* If VR0 is completely to the left or completely to the right
3915 of VR1, they are always different. Notice that we need to
3916 make sure that both comparisons yield similar results to
3917 avoid comparing values that cannot be compared at
3919 cmp1
= compare_values_warnv (vr0
->max
, vr1
->min
, strict_overflow_p
);
3920 cmp2
= compare_values_warnv (vr0
->min
, vr1
->max
, strict_overflow_p
);
3921 if ((cmp1
== -1 && cmp2
== -1) || (cmp1
== 1 && cmp2
== 1))
3922 return boolean_true_node
;
3924 /* If VR0 and VR1 represent a single value and are identical,
3926 else if (compare_values_warnv (vr0
->min
, vr0
->max
,
3927 strict_overflow_p
) == 0
3928 && compare_values_warnv (vr1
->min
, vr1
->max
,
3929 strict_overflow_p
) == 0
3930 && compare_values_warnv (vr0
->min
, vr1
->min
,
3931 strict_overflow_p
) == 0
3932 && compare_values_warnv (vr0
->max
, vr1
->max
,
3933 strict_overflow_p
) == 0)
3934 return boolean_false_node
;
3936 /* Otherwise, they may or may not be different. */
3940 else if (comp
== LT_EXPR
|| comp
== LE_EXPR
)
3944 /* If VR0 is to the left of VR1, return true. */
3945 tst
= compare_values_warnv (vr0
->max
, vr1
->min
, strict_overflow_p
);
3946 if ((comp
== LT_EXPR
&& tst
== -1)
3947 || (comp
== LE_EXPR
&& (tst
== -1 || tst
== 0)))
3949 if (overflow_infinity_range_p (vr0
)
3950 || overflow_infinity_range_p (vr1
))
3951 *strict_overflow_p
= true;
3952 return boolean_true_node
;
3955 /* If VR0 is to the right of VR1, return false. */
3956 tst
= compare_values_warnv (vr0
->min
, vr1
->max
, strict_overflow_p
);
3957 if ((comp
== LT_EXPR
&& (tst
== 0 || tst
== 1))
3958 || (comp
== LE_EXPR
&& tst
== 1))
3960 if (overflow_infinity_range_p (vr0
)
3961 || overflow_infinity_range_p (vr1
))
3962 *strict_overflow_p
= true;
3963 return boolean_false_node
;
3966 /* Otherwise, we don't know. */
3974 /* Given a value range VR, a value VAL and a comparison code COMP, return
3975 BOOLEAN_TRUE_NODE if VR COMP VAL always returns true for all the
3976 values in VR. Return BOOLEAN_FALSE_NODE if the comparison
3977 always returns false. Return NULL_TREE if it is not always
3978 possible to determine the value of the comparison. Also set
3979 *STRICT_OVERFLOW_P to indicate whether a range with an overflow
3980 infinity was used in the test. */
3983 compare_range_with_value (enum tree_code comp
, value_range_t
*vr
, tree val
,
3984 bool *strict_overflow_p
)
3986 if (vr
->type
== VR_VARYING
|| vr
->type
== VR_UNDEFINED
)
3989 /* Anti-ranges need to be handled separately. */
3990 if (vr
->type
== VR_ANTI_RANGE
)
3992 /* For anti-ranges, the only predicates that we can compute at
3993 compile time are equality and inequality. */
4000 /* ~[VAL_1, VAL_2] OP VAL is known if VAL_1 <= VAL <= VAL_2. */
4001 if (value_inside_range (val
, vr
->min
, vr
->max
) == 1)
4002 return (comp
== NE_EXPR
) ? boolean_true_node
: boolean_false_node
;
4007 if (!usable_range_p (vr
, strict_overflow_p
))
4010 if (comp
== EQ_EXPR
)
4012 /* EQ_EXPR may only be computed if VR represents exactly
4014 if (compare_values_warnv (vr
->min
, vr
->max
, strict_overflow_p
) == 0)
4016 int cmp
= compare_values_warnv (vr
->min
, val
, strict_overflow_p
);
4018 return boolean_true_node
;
4019 else if (cmp
== -1 || cmp
== 1 || cmp
== 2)
4020 return boolean_false_node
;
4022 else if (compare_values_warnv (val
, vr
->min
, strict_overflow_p
) == -1
4023 || compare_values_warnv (vr
->max
, val
, strict_overflow_p
) == -1)
4024 return boolean_false_node
;
4028 else if (comp
== NE_EXPR
)
4030 /* If VAL is not inside VR, then they are always different. */
4031 if (compare_values_warnv (vr
->max
, val
, strict_overflow_p
) == -1
4032 || compare_values_warnv (vr
->min
, val
, strict_overflow_p
) == 1)
4033 return boolean_true_node
;
4035 /* If VR represents exactly one value equal to VAL, then return
4037 if (compare_values_warnv (vr
->min
, vr
->max
, strict_overflow_p
) == 0
4038 && compare_values_warnv (vr
->min
, val
, strict_overflow_p
) == 0)
4039 return boolean_false_node
;
4041 /* Otherwise, they may or may not be different. */
4044 else if (comp
== LT_EXPR
|| comp
== LE_EXPR
)
4048 /* If VR is to the left of VAL, return true. */
4049 tst
= compare_values_warnv (vr
->max
, val
, strict_overflow_p
);
4050 if ((comp
== LT_EXPR
&& tst
== -1)
4051 || (comp
== LE_EXPR
&& (tst
== -1 || tst
== 0)))
4053 if (overflow_infinity_range_p (vr
))
4054 *strict_overflow_p
= true;
4055 return boolean_true_node
;
4058 /* If VR is to the right of VAL, return false. */
4059 tst
= compare_values_warnv (vr
->min
, val
, strict_overflow_p
);
4060 if ((comp
== LT_EXPR
&& (tst
== 0 || tst
== 1))
4061 || (comp
== LE_EXPR
&& tst
== 1))
4063 if (overflow_infinity_range_p (vr
))
4064 *strict_overflow_p
= true;
4065 return boolean_false_node
;
4068 /* Otherwise, we don't know. */
4071 else if (comp
== GT_EXPR
|| comp
== GE_EXPR
)
4075 /* If VR is to the right of VAL, return true. */
4076 tst
= compare_values_warnv (vr
->min
, val
, strict_overflow_p
);
4077 if ((comp
== GT_EXPR
&& tst
== 1)
4078 || (comp
== GE_EXPR
&& (tst
== 0 || tst
== 1)))
4080 if (overflow_infinity_range_p (vr
))
4081 *strict_overflow_p
= true;
4082 return boolean_true_node
;
4085 /* If VR is to the left of VAL, return false. */
4086 tst
= compare_values_warnv (vr
->max
, val
, strict_overflow_p
);
4087 if ((comp
== GT_EXPR
&& (tst
== -1 || tst
== 0))
4088 || (comp
== GE_EXPR
&& tst
== -1))
4090 if (overflow_infinity_range_p (vr
))
4091 *strict_overflow_p
= true;
4092 return boolean_false_node
;
4095 /* Otherwise, we don't know. */
4103 /* Debugging dumps. */
4105 void dump_value_range (FILE *, value_range_t
*);
4106 void debug_value_range (value_range_t
*);
4107 void dump_all_value_ranges (FILE *);
4108 void debug_all_value_ranges (void);
4109 void dump_vr_equiv (FILE *, bitmap
);
4110 void debug_vr_equiv (bitmap
);
4113 /* Dump value range VR to FILE. */
4116 dump_value_range (FILE *file
, value_range_t
*vr
)
4119 fprintf (file
, "[]");
4120 else if (vr
->type
== VR_UNDEFINED
)
4121 fprintf (file
, "UNDEFINED");
4122 else if (vr
->type
== VR_RANGE
|| vr
->type
== VR_ANTI_RANGE
)
4124 tree type
= TREE_TYPE (vr
->min
);
4126 fprintf (file
, "%s[", (vr
->type
== VR_ANTI_RANGE
) ? "~" : "");
4128 if (is_negative_overflow_infinity (vr
->min
))
4129 fprintf (file
, "-INF(OVF)");
4130 else if (INTEGRAL_TYPE_P (type
)
4131 && !TYPE_UNSIGNED (type
)
4132 && vrp_val_is_min (vr
->min
))
4133 fprintf (file
, "-INF");
4135 print_generic_expr (file
, vr
->min
, 0);
4137 fprintf (file
, ", ");
4139 if (is_positive_overflow_infinity (vr
->max
))
4140 fprintf (file
, "+INF(OVF)");
4141 else if (INTEGRAL_TYPE_P (type
)
4142 && vrp_val_is_max (vr
->max
))
4143 fprintf (file
, "+INF");
4145 print_generic_expr (file
, vr
->max
, 0);
4147 fprintf (file
, "]");
4154 fprintf (file
, " EQUIVALENCES: { ");
4156 EXECUTE_IF_SET_IN_BITMAP (vr
->equiv
, 0, i
, bi
)
4158 print_generic_expr (file
, ssa_name (i
), 0);
4159 fprintf (file
, " ");
4163 fprintf (file
, "} (%u elements)", c
);
4166 else if (vr
->type
== VR_VARYING
)
4167 fprintf (file
, "VARYING");
4169 fprintf (file
, "INVALID RANGE");
4173 /* Dump value range VR to stderr. */
4176 debug_value_range (value_range_t
*vr
)
4178 dump_value_range (stderr
, vr
);
4179 fprintf (stderr
, "\n");
4183 /* Dump value ranges of all SSA_NAMEs to FILE. */
4186 dump_all_value_ranges (FILE *file
)
4190 for (i
= 0; i
< num_vr_values
; i
++)
4194 print_generic_expr (file
, ssa_name (i
), 0);
4195 fprintf (file
, ": ");
4196 dump_value_range (file
, vr_value
[i
]);
4197 fprintf (file
, "\n");
4201 fprintf (file
, "\n");
4205 /* Dump all value ranges to stderr. */
4208 debug_all_value_ranges (void)
4210 dump_all_value_ranges (stderr
);
4214 /* Given a COND_EXPR COND of the form 'V OP W', and an SSA name V,
4215 create a new SSA name N and return the assertion assignment
4216 'V = ASSERT_EXPR <V, V OP W>'. */
4219 build_assert_expr_for (tree cond
, tree v
)
4224 gcc_assert (TREE_CODE (v
) == SSA_NAME
4225 && COMPARISON_CLASS_P (cond
));
4227 a
= build2 (ASSERT_EXPR
, TREE_TYPE (v
), v
, cond
);
4228 assertion
= gimple_build_assign (NULL_TREE
, a
);
4230 /* The new ASSERT_EXPR, creates a new SSA name that replaces the
4231 operand of the ASSERT_EXPR. Create it so the new name and the old one
4232 are registered in the replacement table so that we can fix the SSA web
4233 after adding all the ASSERT_EXPRs. */
4234 create_new_def_for (v
, assertion
, NULL
);
4240 /* Return false if EXPR is a predicate expression involving floating
4244 fp_predicate (gimple stmt
)
4246 GIMPLE_CHECK (stmt
, GIMPLE_COND
);
4248 return FLOAT_TYPE_P (TREE_TYPE (gimple_cond_lhs (stmt
)));
4252 /* If the range of values taken by OP can be inferred after STMT executes,
4253 return the comparison code (COMP_CODE_P) and value (VAL_P) that
4254 describes the inferred range. Return true if a range could be
4258 infer_value_range (gimple stmt
, tree op
, enum tree_code
*comp_code_p
, tree
*val_p
)
4261 *comp_code_p
= ERROR_MARK
;
4263 /* Do not attempt to infer anything in names that flow through
4265 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op
))
4268 /* Similarly, don't infer anything from statements that may throw
4270 if (stmt_could_throw_p (stmt
))
4273 /* If STMT is the last statement of a basic block with no
4274 successors, there is no point inferring anything about any of its
4275 operands. We would not be able to find a proper insertion point
4276 for the assertion, anyway. */
4277 if (stmt_ends_bb_p (stmt
) && EDGE_COUNT (gimple_bb (stmt
)->succs
) == 0)
4280 /* We can only assume that a pointer dereference will yield
4281 non-NULL if -fdelete-null-pointer-checks is enabled. */
4282 if (flag_delete_null_pointer_checks
4283 && POINTER_TYPE_P (TREE_TYPE (op
))
4284 && gimple_code (stmt
) != GIMPLE_ASM
)
4286 unsigned num_uses
, num_loads
, num_stores
;
4288 count_uses_and_derefs (op
, stmt
, &num_uses
, &num_loads
, &num_stores
);
4289 if (num_loads
+ num_stores
> 0)
4291 *val_p
= build_int_cst (TREE_TYPE (op
), 0);
4292 *comp_code_p
= NE_EXPR
;
4301 void dump_asserts_for (FILE *, tree
);
4302 void debug_asserts_for (tree
);
4303 void dump_all_asserts (FILE *);
4304 void debug_all_asserts (void);
4306 /* Dump all the registered assertions for NAME to FILE. */
4309 dump_asserts_for (FILE *file
, tree name
)
4313 fprintf (file
, "Assertions to be inserted for ");
4314 print_generic_expr (file
, name
, 0);
4315 fprintf (file
, "\n");
4317 loc
= asserts_for
[SSA_NAME_VERSION (name
)];
4320 fprintf (file
, "\t");
4321 print_gimple_stmt (file
, gsi_stmt (loc
->si
), 0, 0);
4322 fprintf (file
, "\n\tBB #%d", loc
->bb
->index
);
4325 fprintf (file
, "\n\tEDGE %d->%d", loc
->e
->src
->index
,
4326 loc
->e
->dest
->index
);
4327 dump_edge_info (file
, loc
->e
, dump_flags
, 0);
4329 fprintf (file
, "\n\tPREDICATE: ");
4330 print_generic_expr (file
, name
, 0);
4331 fprintf (file
, " %s ", tree_code_name
[(int)loc
->comp_code
]);
4332 print_generic_expr (file
, loc
->val
, 0);
4333 fprintf (file
, "\n\n");
4337 fprintf (file
, "\n");
4341 /* Dump all the registered assertions for NAME to stderr. */
4344 debug_asserts_for (tree name
)
4346 dump_asserts_for (stderr
, name
);
4350 /* Dump all the registered assertions for all the names to FILE. */
4353 dump_all_asserts (FILE *file
)
4358 fprintf (file
, "\nASSERT_EXPRs to be inserted\n\n");
4359 EXECUTE_IF_SET_IN_BITMAP (need_assert_for
, 0, i
, bi
)
4360 dump_asserts_for (file
, ssa_name (i
));
4361 fprintf (file
, "\n");
4365 /* Dump all the registered assertions for all the names to stderr. */
4368 debug_all_asserts (void)
4370 dump_all_asserts (stderr
);
4374 /* If NAME doesn't have an ASSERT_EXPR registered for asserting
4375 'EXPR COMP_CODE VAL' at a location that dominates block BB or
4376 E->DEST, then register this location as a possible insertion point
4377 for ASSERT_EXPR <NAME, EXPR COMP_CODE VAL>.
4379 BB, E and SI provide the exact insertion point for the new
4380 ASSERT_EXPR. If BB is NULL, then the ASSERT_EXPR is to be inserted
4381 on edge E. Otherwise, if E is NULL, the ASSERT_EXPR is inserted on
4382 BB. If SI points to a COND_EXPR or a SWITCH_EXPR statement, then E
4383 must not be NULL. */
4386 register_new_assert_for (tree name
, tree expr
,
4387 enum tree_code comp_code
,
4391 gimple_stmt_iterator si
)
4393 assert_locus_t n
, loc
, last_loc
;
4394 basic_block dest_bb
;
4396 gcc_checking_assert (bb
== NULL
|| e
== NULL
);
4399 gcc_checking_assert (gimple_code (gsi_stmt (si
)) != GIMPLE_COND
4400 && gimple_code (gsi_stmt (si
)) != GIMPLE_SWITCH
);
4402 /* Never build an assert comparing against an integer constant with
4403 TREE_OVERFLOW set. This confuses our undefined overflow warning
4405 if (TREE_CODE (val
) == INTEGER_CST
4406 && TREE_OVERFLOW (val
))
4407 val
= build_int_cst_wide (TREE_TYPE (val
),
4408 TREE_INT_CST_LOW (val
), TREE_INT_CST_HIGH (val
));
4410 /* The new assertion A will be inserted at BB or E. We need to
4411 determine if the new location is dominated by a previously
4412 registered location for A. If we are doing an edge insertion,
4413 assume that A will be inserted at E->DEST. Note that this is not
4416 If E is a critical edge, it will be split. But even if E is
4417 split, the new block will dominate the same set of blocks that
4420 The reverse, however, is not true, blocks dominated by E->DEST
4421 will not be dominated by the new block created to split E. So,
4422 if the insertion location is on a critical edge, we will not use
4423 the new location to move another assertion previously registered
4424 at a block dominated by E->DEST. */
4425 dest_bb
= (bb
) ? bb
: e
->dest
;
4427 /* If NAME already has an ASSERT_EXPR registered for COMP_CODE and
4428 VAL at a block dominating DEST_BB, then we don't need to insert a new
4429 one. Similarly, if the same assertion already exists at a block
4430 dominated by DEST_BB and the new location is not on a critical
4431 edge, then update the existing location for the assertion (i.e.,
4432 move the assertion up in the dominance tree).
4434 Note, this is implemented as a simple linked list because there
4435 should not be more than a handful of assertions registered per
4436 name. If this becomes a performance problem, a table hashed by
4437 COMP_CODE and VAL could be implemented. */
4438 loc
= asserts_for
[SSA_NAME_VERSION (name
)];
4442 if (loc
->comp_code
== comp_code
4444 || operand_equal_p (loc
->val
, val
, 0))
4445 && (loc
->expr
== expr
4446 || operand_equal_p (loc
->expr
, expr
, 0)))
4448 /* If E is not a critical edge and DEST_BB
4449 dominates the existing location for the assertion, move
4450 the assertion up in the dominance tree by updating its
4451 location information. */
4452 if ((e
== NULL
|| !EDGE_CRITICAL_P (e
))
4453 && dominated_by_p (CDI_DOMINATORS
, loc
->bb
, dest_bb
))
4462 /* Update the last node of the list and move to the next one. */
4467 /* If we didn't find an assertion already registered for
4468 NAME COMP_CODE VAL, add a new one at the end of the list of
4469 assertions associated with NAME. */
4470 n
= XNEW (struct assert_locus_d
);
4474 n
->comp_code
= comp_code
;
4482 asserts_for
[SSA_NAME_VERSION (name
)] = n
;
4484 bitmap_set_bit (need_assert_for
, SSA_NAME_VERSION (name
));
4487 /* (COND_OP0 COND_CODE COND_OP1) is a predicate which uses NAME.
4488 Extract a suitable test code and value and store them into *CODE_P and
4489 *VAL_P so the predicate is normalized to NAME *CODE_P *VAL_P.
4491 If no extraction was possible, return FALSE, otherwise return TRUE.
4493 If INVERT is true, then we invert the result stored into *CODE_P. */
4496 extract_code_and_val_from_cond_with_ops (tree name
, enum tree_code cond_code
,
4497 tree cond_op0
, tree cond_op1
,
4498 bool invert
, enum tree_code
*code_p
,
4501 enum tree_code comp_code
;
4504 /* Otherwise, we have a comparison of the form NAME COMP VAL
4505 or VAL COMP NAME. */
4506 if (name
== cond_op1
)
4508 /* If the predicate is of the form VAL COMP NAME, flip
4509 COMP around because we need to register NAME as the
4510 first operand in the predicate. */
4511 comp_code
= swap_tree_comparison (cond_code
);
4516 /* The comparison is of the form NAME COMP VAL, so the
4517 comparison code remains unchanged. */
4518 comp_code
= cond_code
;
4522 /* Invert the comparison code as necessary. */
4524 comp_code
= invert_tree_comparison (comp_code
, 0);
4526 /* VRP does not handle float types. */
4527 if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (val
)))
4530 /* Do not register always-false predicates.
4531 FIXME: this works around a limitation in fold() when dealing with
4532 enumerations. Given 'enum { N1, N2 } x;', fold will not
4533 fold 'if (x > N2)' to 'if (0)'. */
4534 if ((comp_code
== GT_EXPR
|| comp_code
== LT_EXPR
)
4535 && INTEGRAL_TYPE_P (TREE_TYPE (val
)))
4537 tree min
= TYPE_MIN_VALUE (TREE_TYPE (val
));
4538 tree max
= TYPE_MAX_VALUE (TREE_TYPE (val
));
4540 if (comp_code
== GT_EXPR
4542 || compare_values (val
, max
) == 0))
4545 if (comp_code
== LT_EXPR
4547 || compare_values (val
, min
) == 0))
4550 *code_p
= comp_code
;
4555 /* Find out smallest RES where RES > VAL && (RES & MASK) == RES, if any
4556 (otherwise return VAL). VAL and MASK must be zero-extended for
4557 precision PREC. If SGNBIT is non-zero, first xor VAL with SGNBIT
4558 (to transform signed values into unsigned) and at the end xor
4562 masked_increment (double_int val
, double_int mask
, double_int sgnbit
,
4565 double_int bit
= double_int_one
, res
;
4569 for (i
= 0; i
< prec
; i
++, bit
+= bit
)
4572 if ((res
& bit
).is_zero ())
4574 res
= bit
- double_int_one
;
4575 res
= (val
+ bit
).and_not (res
);
4578 return res
^ sgnbit
;
4580 return val
^ sgnbit
;
4583 /* Try to register an edge assertion for SSA name NAME on edge E for
4584 the condition COND contributing to the conditional jump pointed to by BSI.
4585 Invert the condition COND if INVERT is true.
4586 Return true if an assertion for NAME could be registered. */
4589 register_edge_assert_for_2 (tree name
, edge e
, gimple_stmt_iterator bsi
,
4590 enum tree_code cond_code
,
4591 tree cond_op0
, tree cond_op1
, bool invert
)
4594 enum tree_code comp_code
;
4595 bool retval
= false;
4597 if (!extract_code_and_val_from_cond_with_ops (name
, cond_code
,
4600 invert
, &comp_code
, &val
))
4603 /* Only register an ASSERT_EXPR if NAME was found in the sub-graph
4604 reachable from E. */
4605 if (live_on_edge (e
, name
)
4606 && !has_single_use (name
))
4608 register_new_assert_for (name
, name
, comp_code
, val
, NULL
, e
, bsi
);
4612 /* In the case of NAME <= CST and NAME being defined as
4613 NAME = (unsigned) NAME2 + CST2 we can assert NAME2 >= -CST2
4614 and NAME2 <= CST - CST2. We can do the same for NAME > CST.
4615 This catches range and anti-range tests. */
4616 if ((comp_code
== LE_EXPR
4617 || comp_code
== GT_EXPR
)
4618 && TREE_CODE (val
) == INTEGER_CST
4619 && TYPE_UNSIGNED (TREE_TYPE (val
)))
4621 gimple def_stmt
= SSA_NAME_DEF_STMT (name
);
4622 tree cst2
= NULL_TREE
, name2
= NULL_TREE
, name3
= NULL_TREE
;
4624 /* Extract CST2 from the (optional) addition. */
4625 if (is_gimple_assign (def_stmt
)
4626 && gimple_assign_rhs_code (def_stmt
) == PLUS_EXPR
)
4628 name2
= gimple_assign_rhs1 (def_stmt
);
4629 cst2
= gimple_assign_rhs2 (def_stmt
);
4630 if (TREE_CODE (name2
) == SSA_NAME
4631 && TREE_CODE (cst2
) == INTEGER_CST
)
4632 def_stmt
= SSA_NAME_DEF_STMT (name2
);
4635 /* Extract NAME2 from the (optional) sign-changing cast. */
4636 if (gimple_assign_cast_p (def_stmt
))
4638 if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt
))
4639 && ! TYPE_UNSIGNED (TREE_TYPE (gimple_assign_rhs1 (def_stmt
)))
4640 && (TYPE_PRECISION (gimple_expr_type (def_stmt
))
4641 == TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (def_stmt
)))))
4642 name3
= gimple_assign_rhs1 (def_stmt
);
4645 /* If name3 is used later, create an ASSERT_EXPR for it. */
4646 if (name3
!= NULL_TREE
4647 && TREE_CODE (name3
) == SSA_NAME
4648 && (cst2
== NULL_TREE
4649 || TREE_CODE (cst2
) == INTEGER_CST
)
4650 && INTEGRAL_TYPE_P (TREE_TYPE (name3
))
4651 && live_on_edge (e
, name3
)
4652 && !has_single_use (name3
))
4656 /* Build an expression for the range test. */
4657 tmp
= build1 (NOP_EXPR
, TREE_TYPE (name
), name3
);
4658 if (cst2
!= NULL_TREE
)
4659 tmp
= build2 (PLUS_EXPR
, TREE_TYPE (name
), tmp
, cst2
);
4663 fprintf (dump_file
, "Adding assert for ");
4664 print_generic_expr (dump_file
, name3
, 0);
4665 fprintf (dump_file
, " from ");
4666 print_generic_expr (dump_file
, tmp
, 0);
4667 fprintf (dump_file
, "\n");
4670 register_new_assert_for (name3
, tmp
, comp_code
, val
, NULL
, e
, bsi
);
4675 /* If name2 is used later, create an ASSERT_EXPR for it. */
4676 if (name2
!= NULL_TREE
4677 && TREE_CODE (name2
) == SSA_NAME
4678 && TREE_CODE (cst2
) == INTEGER_CST
4679 && INTEGRAL_TYPE_P (TREE_TYPE (name2
))
4680 && live_on_edge (e
, name2
)
4681 && !has_single_use (name2
))
4685 /* Build an expression for the range test. */
4687 if (TREE_TYPE (name
) != TREE_TYPE (name2
))
4688 tmp
= build1 (NOP_EXPR
, TREE_TYPE (name
), tmp
);
4689 if (cst2
!= NULL_TREE
)
4690 tmp
= build2 (PLUS_EXPR
, TREE_TYPE (name
), tmp
, cst2
);
4694 fprintf (dump_file
, "Adding assert for ");
4695 print_generic_expr (dump_file
, name2
, 0);
4696 fprintf (dump_file
, " from ");
4697 print_generic_expr (dump_file
, tmp
, 0);
4698 fprintf (dump_file
, "\n");
4701 register_new_assert_for (name2
, tmp
, comp_code
, val
, NULL
, e
, bsi
);
4707 if (TREE_CODE_CLASS (comp_code
) == tcc_comparison
4708 && TREE_CODE (val
) == INTEGER_CST
)
4710 gimple def_stmt
= SSA_NAME_DEF_STMT (name
);
4711 tree name2
= NULL_TREE
, names
[2], cst2
= NULL_TREE
;
4712 tree val2
= NULL_TREE
;
4713 double_int mask
= double_int_zero
;
4714 unsigned int prec
= TYPE_PRECISION (TREE_TYPE (val
));
4715 unsigned int nprec
= prec
;
4716 enum tree_code rhs_code
= ERROR_MARK
;
4718 if (is_gimple_assign (def_stmt
))
4719 rhs_code
= gimple_assign_rhs_code (def_stmt
);
4721 /* Add asserts for NAME cmp CST and NAME being defined
4722 as NAME = (int) NAME2. */
4723 if (!TYPE_UNSIGNED (TREE_TYPE (val
))
4724 && (comp_code
== LE_EXPR
|| comp_code
== LT_EXPR
4725 || comp_code
== GT_EXPR
|| comp_code
== GE_EXPR
)
4726 && gimple_assign_cast_p (def_stmt
))
4728 name2
= gimple_assign_rhs1 (def_stmt
);
4729 if (CONVERT_EXPR_CODE_P (rhs_code
)
4730 && INTEGRAL_TYPE_P (TREE_TYPE (name2
))
4731 && TYPE_UNSIGNED (TREE_TYPE (name2
))
4732 && prec
== TYPE_PRECISION (TREE_TYPE (name2
))
4733 && (comp_code
== LE_EXPR
|| comp_code
== GT_EXPR
4734 || !tree_int_cst_equal (val
,
4735 TYPE_MIN_VALUE (TREE_TYPE (val
))))
4736 && live_on_edge (e
, name2
)
4737 && !has_single_use (name2
))
4740 enum tree_code new_comp_code
= comp_code
;
4742 cst
= fold_convert (TREE_TYPE (name2
),
4743 TYPE_MIN_VALUE (TREE_TYPE (val
)));
4744 /* Build an expression for the range test. */
4745 tmp
= build2 (PLUS_EXPR
, TREE_TYPE (name2
), name2
, cst
);
4746 cst
= fold_build2 (PLUS_EXPR
, TREE_TYPE (name2
), cst
,
4747 fold_convert (TREE_TYPE (name2
), val
));
4748 if (comp_code
== LT_EXPR
|| comp_code
== GE_EXPR
)
4750 new_comp_code
= comp_code
== LT_EXPR
? LE_EXPR
: GT_EXPR
;
4751 cst
= fold_build2 (MINUS_EXPR
, TREE_TYPE (name2
), cst
,
4752 build_int_cst (TREE_TYPE (name2
), 1));
4757 fprintf (dump_file
, "Adding assert for ");
4758 print_generic_expr (dump_file
, name2
, 0);
4759 fprintf (dump_file
, " from ");
4760 print_generic_expr (dump_file
, tmp
, 0);
4761 fprintf (dump_file
, "\n");
4764 register_new_assert_for (name2
, tmp
, new_comp_code
, cst
, NULL
,
4771 /* Add asserts for NAME cmp CST and NAME being defined as
4772 NAME = NAME2 >> CST2.
4774 Extract CST2 from the right shift. */
4775 if (rhs_code
== RSHIFT_EXPR
)
4777 name2
= gimple_assign_rhs1 (def_stmt
);
4778 cst2
= gimple_assign_rhs2 (def_stmt
);
4779 if (TREE_CODE (name2
) == SSA_NAME
4780 && host_integerp (cst2
, 1)
4781 && INTEGRAL_TYPE_P (TREE_TYPE (name2
))
4782 && IN_RANGE (tree_low_cst (cst2
, 1), 1, prec
- 1)
4783 && prec
<= HOST_BITS_PER_DOUBLE_INT
4784 && prec
== GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (val
)))
4785 && live_on_edge (e
, name2
)
4786 && !has_single_use (name2
))
4788 mask
= double_int::mask (tree_low_cst (cst2
, 1));
4789 val2
= fold_binary (LSHIFT_EXPR
, TREE_TYPE (val
), val
, cst2
);
4792 if (val2
!= NULL_TREE
4793 && TREE_CODE (val2
) == INTEGER_CST
4794 && simple_cst_equal (fold_build2 (RSHIFT_EXPR
,
4798 enum tree_code new_comp_code
= comp_code
;
4802 if (comp_code
== EQ_EXPR
|| comp_code
== NE_EXPR
)
4804 if (!TYPE_UNSIGNED (TREE_TYPE (val
)))
4806 tree type
= build_nonstandard_integer_type (prec
, 1);
4807 tmp
= build1 (NOP_EXPR
, type
, name2
);
4808 val2
= fold_convert (type
, val2
);
4810 tmp
= fold_build2 (MINUS_EXPR
, TREE_TYPE (tmp
), tmp
, val2
);
4811 new_val
= double_int_to_tree (TREE_TYPE (tmp
), mask
);
4812 new_comp_code
= comp_code
== EQ_EXPR
? LE_EXPR
: GT_EXPR
;
4814 else if (comp_code
== LT_EXPR
|| comp_code
== GE_EXPR
)
4819 = double_int::max_value (prec
, TYPE_UNSIGNED (TREE_TYPE (val
)));
4820 mask
|= tree_to_double_int (val2
);
4822 new_val
= NULL_TREE
;
4824 new_val
= double_int_to_tree (TREE_TYPE (val2
), mask
);
4831 fprintf (dump_file
, "Adding assert for ");
4832 print_generic_expr (dump_file
, name2
, 0);
4833 fprintf (dump_file
, " from ");
4834 print_generic_expr (dump_file
, tmp
, 0);
4835 fprintf (dump_file
, "\n");
4838 register_new_assert_for (name2
, tmp
, new_comp_code
, new_val
,
4844 /* Add asserts for NAME cmp CST and NAME being defined as
4845 NAME = NAME2 & CST2.
4847 Extract CST2 from the and.
4850 NAME = (unsigned) NAME2;
4851 casts where NAME's type is unsigned and has smaller precision
4852 than NAME2's type as if it was NAME = NAME2 & MASK. */
4853 names
[0] = NULL_TREE
;
4854 names
[1] = NULL_TREE
;
4856 if (rhs_code
== BIT_AND_EXPR
4857 || (CONVERT_EXPR_CODE_P (rhs_code
)
4858 && TREE_CODE (TREE_TYPE (val
)) == INTEGER_TYPE
4859 && TYPE_UNSIGNED (TREE_TYPE (val
))
4860 && TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (def_stmt
)))
4864 name2
= gimple_assign_rhs1 (def_stmt
);
4865 if (rhs_code
== BIT_AND_EXPR
)
4866 cst2
= gimple_assign_rhs2 (def_stmt
);
4869 cst2
= TYPE_MAX_VALUE (TREE_TYPE (val
));
4870 nprec
= TYPE_PRECISION (TREE_TYPE (name2
));
4872 if (TREE_CODE (name2
) == SSA_NAME
4873 && INTEGRAL_TYPE_P (TREE_TYPE (name2
))
4874 && TREE_CODE (cst2
) == INTEGER_CST
4875 && !integer_zerop (cst2
)
4876 && nprec
<= HOST_BITS_PER_DOUBLE_INT
4878 || TYPE_UNSIGNED (TREE_TYPE (val
))))
4880 gimple def_stmt2
= SSA_NAME_DEF_STMT (name2
);
4881 if (gimple_assign_cast_p (def_stmt2
))
4883 names
[1] = gimple_assign_rhs1 (def_stmt2
);
4884 if (!CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt2
))
4885 || !INTEGRAL_TYPE_P (TREE_TYPE (names
[1]))
4886 || (TYPE_PRECISION (TREE_TYPE (name2
))
4887 != TYPE_PRECISION (TREE_TYPE (names
[1])))
4888 || !live_on_edge (e
, names
[1])
4889 || has_single_use (names
[1]))
4890 names
[1] = NULL_TREE
;
4892 if (live_on_edge (e
, name2
)
4893 && !has_single_use (name2
))
4897 if (names
[0] || names
[1])
4899 double_int minv
, maxv
= double_int_zero
, valv
, cst2v
;
4900 double_int tem
, sgnbit
;
4901 bool valid_p
= false, valn
= false, cst2n
= false;
4902 enum tree_code ccode
= comp_code
;
4904 valv
= tree_to_double_int (val
).zext (nprec
);
4905 cst2v
= tree_to_double_int (cst2
).zext (nprec
);
4906 if (!TYPE_UNSIGNED (TREE_TYPE (val
)))
4908 valn
= valv
.sext (nprec
).is_negative ();
4909 cst2n
= cst2v
.sext (nprec
).is_negative ();
4911 /* If CST2 doesn't have most significant bit set,
4912 but VAL is negative, we have comparison like
4913 if ((x & 0x123) > -4) (always true). Just give up. */
4917 sgnbit
= double_int_one
.llshift (nprec
- 1, nprec
).zext (nprec
);
4919 sgnbit
= double_int_zero
;
4920 minv
= valv
& cst2v
;
4924 /* Minimum unsigned value for equality is VAL & CST2
4925 (should be equal to VAL, otherwise we probably should
4926 have folded the comparison into false) and
4927 maximum unsigned value is VAL | ~CST2. */
4928 maxv
= valv
| ~cst2v
;
4929 maxv
= maxv
.zext (nprec
);
4933 tem
= valv
| ~cst2v
;
4934 tem
= tem
.zext (nprec
);
4935 /* If VAL is 0, handle (X & CST2) != 0 as (X & CST2) > 0U. */
4936 if (valv
.is_zero ())
4939 sgnbit
= double_int_zero
;
4942 /* If (VAL | ~CST2) is all ones, handle it as
4943 (X & CST2) < VAL. */
4944 if (tem
== double_int::mask (nprec
))
4948 sgnbit
= double_int_zero
;
4952 && cst2v
.sext (nprec
).is_negative ())
4954 = double_int_one
.llshift (nprec
- 1, nprec
).zext (nprec
);
4955 if (!sgnbit
.is_zero ())
4963 if (tem
== double_int::mask (nprec
- 1))
4969 sgnbit
= double_int_zero
;
4973 /* Minimum unsigned value for >= if (VAL & CST2) == VAL
4974 is VAL and maximum unsigned value is ~0. For signed
4975 comparison, if CST2 doesn't have most significant bit
4976 set, handle it similarly. If CST2 has MSB set,
4977 the minimum is the same, and maximum is ~0U/2. */
4980 /* If (VAL & CST2) != VAL, X & CST2 can't be equal to
4982 minv
= masked_increment (valv
, cst2v
, sgnbit
, nprec
);
4986 maxv
= double_int::mask (nprec
- (cst2n
? 1 : 0));
4991 /* Find out smallest MINV where MINV > VAL
4992 && (MINV & CST2) == MINV, if any. If VAL is signed and
4993 CST2 has MSB set, compute it biased by 1 << (nprec - 1). */
4994 minv
= masked_increment (valv
, cst2v
, sgnbit
, nprec
);
4997 maxv
= double_int::mask (nprec
- (cst2n
? 1 : 0));
5001 /* Minimum unsigned value for <= is 0 and maximum
5002 unsigned value is VAL | ~CST2 if (VAL & CST2) == VAL.
5003 Otherwise, find smallest VAL2 where VAL2 > VAL
5004 && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
5006 For signed comparison, if CST2 doesn't have most
5007 significant bit set, handle it similarly. If CST2 has
5008 MSB set, the maximum is the same and minimum is INT_MIN. */
5013 maxv
= masked_increment (valv
, cst2v
, sgnbit
, nprec
);
5016 maxv
-= double_int_one
;
5019 maxv
= maxv
.zext (nprec
);
5025 /* Minimum unsigned value for < is 0 and maximum
5026 unsigned value is (VAL-1) | ~CST2 if (VAL & CST2) == VAL.
5027 Otherwise, find smallest VAL2 where VAL2 > VAL
5028 && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
5030 For signed comparison, if CST2 doesn't have most
5031 significant bit set, handle it similarly. If CST2 has
5032 MSB set, the maximum is the same and minimum is INT_MIN. */
5041 maxv
= masked_increment (valv
, cst2v
, sgnbit
, nprec
);
5045 maxv
-= double_int_one
;
5047 maxv
= maxv
.zext (nprec
);
5055 && (maxv
- minv
).zext (nprec
) != double_int::mask (nprec
))
5057 tree tmp
, new_val
, type
;
5060 for (i
= 0; i
< 2; i
++)
5063 double_int maxv2
= maxv
;
5065 type
= TREE_TYPE (names
[i
]);
5066 if (!TYPE_UNSIGNED (type
))
5068 type
= build_nonstandard_integer_type (nprec
, 1);
5069 tmp
= build1 (NOP_EXPR
, type
, names
[i
]);
5071 if (!minv
.is_zero ())
5073 tmp
= build2 (PLUS_EXPR
, type
, tmp
,
5074 double_int_to_tree (type
, -minv
));
5075 maxv2
= maxv
- minv
;
5077 new_val
= double_int_to_tree (type
, maxv2
);
5081 fprintf (dump_file
, "Adding assert for ");
5082 print_generic_expr (dump_file
, names
[i
], 0);
5083 fprintf (dump_file
, " from ");
5084 print_generic_expr (dump_file
, tmp
, 0);
5085 fprintf (dump_file
, "\n");
5088 register_new_assert_for (names
[i
], tmp
, LE_EXPR
,
5089 new_val
, NULL
, e
, bsi
);
5099 /* OP is an operand of a truth value expression which is known to have
5100 a particular value. Register any asserts for OP and for any
5101 operands in OP's defining statement.
5103 If CODE is EQ_EXPR, then we want to register OP is zero (false),
5104 if CODE is NE_EXPR, then we want to register OP is nonzero (true). */
5107 register_edge_assert_for_1 (tree op
, enum tree_code code
,
5108 edge e
, gimple_stmt_iterator bsi
)
5110 bool retval
= false;
5113 enum tree_code rhs_code
;
5115 /* We only care about SSA_NAMEs. */
5116 if (TREE_CODE (op
) != SSA_NAME
)
5119 /* We know that OP will have a zero or nonzero value. If OP is used
5120 more than once go ahead and register an assert for OP.
5122 The FOUND_IN_SUBGRAPH support is not helpful in this situation as
5123 it will always be set for OP (because OP is used in a COND_EXPR in
5125 if (!has_single_use (op
))
5127 val
= build_int_cst (TREE_TYPE (op
), 0);
5128 register_new_assert_for (op
, op
, code
, val
, NULL
, e
, bsi
);
5132 /* Now look at how OP is set. If it's set from a comparison,
5133 a truth operation or some bit operations, then we may be able
5134 to register information about the operands of that assignment. */
5135 op_def
= SSA_NAME_DEF_STMT (op
);
5136 if (gimple_code (op_def
) != GIMPLE_ASSIGN
)
5139 rhs_code
= gimple_assign_rhs_code (op_def
);
5141 if (TREE_CODE_CLASS (rhs_code
) == tcc_comparison
)
5143 bool invert
= (code
== EQ_EXPR
? true : false);
5144 tree op0
= gimple_assign_rhs1 (op_def
);
5145 tree op1
= gimple_assign_rhs2 (op_def
);
5147 if (TREE_CODE (op0
) == SSA_NAME
)
5148 retval
|= register_edge_assert_for_2 (op0
, e
, bsi
, rhs_code
, op0
, op1
,
5150 if (TREE_CODE (op1
) == SSA_NAME
)
5151 retval
|= register_edge_assert_for_2 (op1
, e
, bsi
, rhs_code
, op0
, op1
,
5154 else if ((code
== NE_EXPR
5155 && gimple_assign_rhs_code (op_def
) == BIT_AND_EXPR
)
5157 && gimple_assign_rhs_code (op_def
) == BIT_IOR_EXPR
))
5159 /* Recurse on each operand. */
5160 retval
|= register_edge_assert_for_1 (gimple_assign_rhs1 (op_def
),
5162 retval
|= register_edge_assert_for_1 (gimple_assign_rhs2 (op_def
),
5165 else if (gimple_assign_rhs_code (op_def
) == BIT_NOT_EXPR
5166 && TYPE_PRECISION (TREE_TYPE (gimple_assign_lhs (op_def
))) == 1)
5168 /* Recurse, flipping CODE. */
5169 code
= invert_tree_comparison (code
, false);
5170 retval
|= register_edge_assert_for_1 (gimple_assign_rhs1 (op_def
),
5173 else if (gimple_assign_rhs_code (op_def
) == SSA_NAME
)
5175 /* Recurse through the copy. */
5176 retval
|= register_edge_assert_for_1 (gimple_assign_rhs1 (op_def
),
5179 else if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (op_def
)))
5181 /* Recurse through the type conversion. */
5182 retval
|= register_edge_assert_for_1 (gimple_assign_rhs1 (op_def
),
5189 /* Try to register an edge assertion for SSA name NAME on edge E for
5190 the condition COND contributing to the conditional jump pointed to by SI.
5191 Return true if an assertion for NAME could be registered. */
5194 register_edge_assert_for (tree name
, edge e
, gimple_stmt_iterator si
,
5195 enum tree_code cond_code
, tree cond_op0
,
5199 enum tree_code comp_code
;
5200 bool retval
= false;
5201 bool is_else_edge
= (e
->flags
& EDGE_FALSE_VALUE
) != 0;
5203 /* Do not attempt to infer anything in names that flow through
5205 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name
))
5208 if (!extract_code_and_val_from_cond_with_ops (name
, cond_code
,
5214 /* Register ASSERT_EXPRs for name. */
5215 retval
|= register_edge_assert_for_2 (name
, e
, si
, cond_code
, cond_op0
,
5216 cond_op1
, is_else_edge
);
5219 /* If COND is effectively an equality test of an SSA_NAME against
5220 the value zero or one, then we may be able to assert values
5221 for SSA_NAMEs which flow into COND. */
5223 /* In the case of NAME == 1 or NAME != 0, for BIT_AND_EXPR defining
5224 statement of NAME we can assert both operands of the BIT_AND_EXPR
5225 have nonzero value. */
5226 if (((comp_code
== EQ_EXPR
&& integer_onep (val
))
5227 || (comp_code
== NE_EXPR
&& integer_zerop (val
))))
5229 gimple def_stmt
= SSA_NAME_DEF_STMT (name
);
5231 if (is_gimple_assign (def_stmt
)
5232 && gimple_assign_rhs_code (def_stmt
) == BIT_AND_EXPR
)
5234 tree op0
= gimple_assign_rhs1 (def_stmt
);
5235 tree op1
= gimple_assign_rhs2 (def_stmt
);
5236 retval
|= register_edge_assert_for_1 (op0
, NE_EXPR
, e
, si
);
5237 retval
|= register_edge_assert_for_1 (op1
, NE_EXPR
, e
, si
);
5241 /* In the case of NAME == 0 or NAME != 1, for BIT_IOR_EXPR defining
5242 statement of NAME we can assert both operands of the BIT_IOR_EXPR
5244 if (((comp_code
== EQ_EXPR
&& integer_zerop (val
))
5245 || (comp_code
== NE_EXPR
&& integer_onep (val
))))
5247 gimple def_stmt
= SSA_NAME_DEF_STMT (name
);
5249 /* For BIT_IOR_EXPR only if NAME == 0 both operands have
5250 necessarily zero value, or if type-precision is one. */
5251 if (is_gimple_assign (def_stmt
)
5252 && (gimple_assign_rhs_code (def_stmt
) == BIT_IOR_EXPR
5253 && (TYPE_PRECISION (TREE_TYPE (name
)) == 1
5254 || comp_code
== EQ_EXPR
)))
5256 tree op0
= gimple_assign_rhs1 (def_stmt
);
5257 tree op1
= gimple_assign_rhs2 (def_stmt
);
5258 retval
|= register_edge_assert_for_1 (op0
, EQ_EXPR
, e
, si
);
5259 retval
|= register_edge_assert_for_1 (op1
, EQ_EXPR
, e
, si
);
5267 /* Determine whether the outgoing edges of BB should receive an
5268 ASSERT_EXPR for each of the operands of BB's LAST statement.
5269 The last statement of BB must be a COND_EXPR.
5271 If any of the sub-graphs rooted at BB have an interesting use of
5272 the predicate operands, an assert location node is added to the
5273 list of assertions for the corresponding operands. */
5276 find_conditional_asserts (basic_block bb
, gimple last
)
5279 gimple_stmt_iterator bsi
;
5285 need_assert
= false;
5286 bsi
= gsi_for_stmt (last
);
5288 /* Look for uses of the operands in each of the sub-graphs
5289 rooted at BB. We need to check each of the outgoing edges
5290 separately, so that we know what kind of ASSERT_EXPR to
5292 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
5297 /* Register the necessary assertions for each operand in the
5298 conditional predicate. */
5299 FOR_EACH_SSA_TREE_OPERAND (op
, last
, iter
, SSA_OP_USE
)
5301 need_assert
|= register_edge_assert_for (op
, e
, bsi
,
5302 gimple_cond_code (last
),
5303 gimple_cond_lhs (last
),
5304 gimple_cond_rhs (last
));
5317 /* Compare two case labels sorting first by the destination bb index
5318 and then by the case value. */
5321 compare_case_labels (const void *p1
, const void *p2
)
5323 const struct case_info
*ci1
= (const struct case_info
*) p1
;
5324 const struct case_info
*ci2
= (const struct case_info
*) p2
;
5325 int idx1
= ci1
->bb
->index
;
5326 int idx2
= ci2
->bb
->index
;
5330 else if (idx1
== idx2
)
5332 /* Make sure the default label is first in a group. */
5333 if (!CASE_LOW (ci1
->expr
))
5335 else if (!CASE_LOW (ci2
->expr
))
5338 return tree_int_cst_compare (CASE_LOW (ci1
->expr
),
5339 CASE_LOW (ci2
->expr
));
5345 /* Determine whether the outgoing edges of BB should receive an
5346 ASSERT_EXPR for each of the operands of BB's LAST statement.
5347 The last statement of BB must be a SWITCH_EXPR.
5349 If any of the sub-graphs rooted at BB have an interesting use of
5350 the predicate operands, an assert location node is added to the
5351 list of assertions for the corresponding operands. */
5354 find_switch_asserts (basic_block bb
, gimple last
)
5357 gimple_stmt_iterator bsi
;
5360 struct case_info
*ci
;
5361 size_t n
= gimple_switch_num_labels (last
);
5362 #if GCC_VERSION >= 4000
5365 /* Work around GCC 3.4 bug (PR 37086). */
5366 volatile unsigned int idx
;
5369 need_assert
= false;
5370 bsi
= gsi_for_stmt (last
);
5371 op
= gimple_switch_index (last
);
5372 if (TREE_CODE (op
) != SSA_NAME
)
5375 /* Build a vector of case labels sorted by destination label. */
5376 ci
= XNEWVEC (struct case_info
, n
);
5377 for (idx
= 0; idx
< n
; ++idx
)
5379 ci
[idx
].expr
= gimple_switch_label (last
, idx
);
5380 ci
[idx
].bb
= label_to_block (CASE_LABEL (ci
[idx
].expr
));
5382 qsort (ci
, n
, sizeof (struct case_info
), compare_case_labels
);
5384 for (idx
= 0; idx
< n
; ++idx
)
5387 tree cl
= ci
[idx
].expr
;
5388 basic_block cbb
= ci
[idx
].bb
;
5390 min
= CASE_LOW (cl
);
5391 max
= CASE_HIGH (cl
);
5393 /* If there are multiple case labels with the same destination
5394 we need to combine them to a single value range for the edge. */
5395 if (idx
+ 1 < n
&& cbb
== ci
[idx
+ 1].bb
)
5397 /* Skip labels until the last of the group. */
5400 } while (idx
< n
&& cbb
== ci
[idx
].bb
);
5403 /* Pick up the maximum of the case label range. */
5404 if (CASE_HIGH (ci
[idx
].expr
))
5405 max
= CASE_HIGH (ci
[idx
].expr
);
5407 max
= CASE_LOW (ci
[idx
].expr
);
5410 /* Nothing to do if the range includes the default label until we
5411 can register anti-ranges. */
5412 if (min
== NULL_TREE
)
5415 /* Find the edge to register the assert expr on. */
5416 e
= find_edge (bb
, cbb
);
5418 /* Register the necessary assertions for the operand in the
5420 need_assert
|= register_edge_assert_for (op
, e
, bsi
,
5421 max
? GE_EXPR
: EQ_EXPR
,
5423 fold_convert (TREE_TYPE (op
),
5427 need_assert
|= register_edge_assert_for (op
, e
, bsi
, LE_EXPR
,
5429 fold_convert (TREE_TYPE (op
),
5439 /* Traverse all the statements in block BB looking for statements that
5440 may generate useful assertions for the SSA names in their operand.
5441 If a statement produces a useful assertion A for name N_i, then the
5442 list of assertions already generated for N_i is scanned to
5443 determine if A is actually needed.
5445 If N_i already had the assertion A at a location dominating the
5446 current location, then nothing needs to be done. Otherwise, the
5447 new location for A is recorded instead.
5449 1- For every statement S in BB, all the variables used by S are
5450 added to bitmap FOUND_IN_SUBGRAPH.
5452 2- If statement S uses an operand N in a way that exposes a known
5453 value range for N, then if N was not already generated by an
5454 ASSERT_EXPR, create a new assert location for N. For instance,
5455 if N is a pointer and the statement dereferences it, we can
5456 assume that N is not NULL.
5458 3- COND_EXPRs are a special case of #2. We can derive range
5459 information from the predicate but need to insert different
5460 ASSERT_EXPRs for each of the sub-graphs rooted at the
5461 conditional block. If the last statement of BB is a conditional
5462 expression of the form 'X op Y', then
5464 a) Remove X and Y from the set FOUND_IN_SUBGRAPH.
5466 b) If the conditional is the only entry point to the sub-graph
5467 corresponding to the THEN_CLAUSE, recurse into it. On
5468 return, if X and/or Y are marked in FOUND_IN_SUBGRAPH, then
5469 an ASSERT_EXPR is added for the corresponding variable.
5471 c) Repeat step (b) on the ELSE_CLAUSE.
5473 d) Mark X and Y in FOUND_IN_SUBGRAPH.
5482 In this case, an assertion on the THEN clause is useful to
5483 determine that 'a' is always 9 on that edge. However, an assertion
5484 on the ELSE clause would be unnecessary.
5486 4- If BB does not end in a conditional expression, then we recurse
5487 into BB's dominator children.
5489 At the end of the recursive traversal, every SSA name will have a
5490 list of locations where ASSERT_EXPRs should be added. When a new
5491 location for name N is found, it is registered by calling
5492 register_new_assert_for. That function keeps track of all the
5493 registered assertions to prevent adding unnecessary assertions.
5494 For instance, if a pointer P_4 is dereferenced more than once in a
5495 dominator tree, only the location dominating all the dereference of
5496 P_4 will receive an ASSERT_EXPR.
5498 If this function returns true, then it means that there are names
5499 for which we need to generate ASSERT_EXPRs. Those assertions are
5500 inserted by process_assert_insertions. */
5503 find_assert_locations_1 (basic_block bb
, sbitmap live
)
5505 gimple_stmt_iterator si
;
5509 need_assert
= false;
5510 last
= last_stmt (bb
);
5512 /* If BB's last statement is a conditional statement involving integer
5513 operands, determine if we need to add ASSERT_EXPRs. */
5515 && gimple_code (last
) == GIMPLE_COND
5516 && !fp_predicate (last
)
5517 && !ZERO_SSA_OPERANDS (last
, SSA_OP_USE
))
5518 need_assert
|= find_conditional_asserts (bb
, last
);
5520 /* If BB's last statement is a switch statement involving integer
5521 operands, determine if we need to add ASSERT_EXPRs. */
5523 && gimple_code (last
) == GIMPLE_SWITCH
5524 && !ZERO_SSA_OPERANDS (last
, SSA_OP_USE
))
5525 need_assert
|= find_switch_asserts (bb
, last
);
5527 /* Traverse all the statements in BB marking used names and looking
5528 for statements that may infer assertions for their used operands. */
5529 for (si
= gsi_last_bb (bb
); !gsi_end_p (si
); gsi_prev (&si
))
5535 stmt
= gsi_stmt (si
);
5537 if (is_gimple_debug (stmt
))
5540 /* See if we can derive an assertion for any of STMT's operands. */
5541 FOR_EACH_SSA_TREE_OPERAND (op
, stmt
, i
, SSA_OP_USE
)
5544 enum tree_code comp_code
;
5546 /* If op is not live beyond this stmt, do not bother to insert
5548 if (!TEST_BIT (live
, SSA_NAME_VERSION (op
)))
5551 /* If OP is used in such a way that we can infer a value
5552 range for it, and we don't find a previous assertion for
5553 it, create a new assertion location node for OP. */
5554 if (infer_value_range (stmt
, op
, &comp_code
, &value
))
5556 /* If we are able to infer a nonzero value range for OP,
5557 then walk backwards through the use-def chain to see if OP
5558 was set via a typecast.
5560 If so, then we can also infer a nonzero value range
5561 for the operand of the NOP_EXPR. */
5562 if (comp_code
== NE_EXPR
&& integer_zerop (value
))
5565 gimple def_stmt
= SSA_NAME_DEF_STMT (t
);
5567 while (is_gimple_assign (def_stmt
)
5568 && gimple_assign_rhs_code (def_stmt
) == NOP_EXPR
5570 (gimple_assign_rhs1 (def_stmt
)) == SSA_NAME
5572 (TREE_TYPE (gimple_assign_rhs1 (def_stmt
))))
5574 t
= gimple_assign_rhs1 (def_stmt
);
5575 def_stmt
= SSA_NAME_DEF_STMT (t
);
5577 /* Note we want to register the assert for the
5578 operand of the NOP_EXPR after SI, not after the
5580 if (! has_single_use (t
))
5582 register_new_assert_for (t
, t
, comp_code
, value
,
5589 register_new_assert_for (op
, op
, comp_code
, value
, bb
, NULL
, si
);
5595 FOR_EACH_SSA_TREE_OPERAND (op
, stmt
, i
, SSA_OP_USE
)
5596 SET_BIT (live
, SSA_NAME_VERSION (op
));
5597 FOR_EACH_SSA_TREE_OPERAND (op
, stmt
, i
, SSA_OP_DEF
)
5598 RESET_BIT (live
, SSA_NAME_VERSION (op
));
5601 /* Traverse all PHI nodes in BB, updating live. */
5602 for (si
= gsi_start_phis (bb
); !gsi_end_p(si
); gsi_next (&si
))
5604 use_operand_p arg_p
;
5606 gimple phi
= gsi_stmt (si
);
5607 tree res
= gimple_phi_result (phi
);
5609 if (virtual_operand_p (res
))
5612 FOR_EACH_PHI_ARG (arg_p
, phi
, i
, SSA_OP_USE
)
5614 tree arg
= USE_FROM_PTR (arg_p
);
5615 if (TREE_CODE (arg
) == SSA_NAME
)
5616 SET_BIT (live
, SSA_NAME_VERSION (arg
));
5619 RESET_BIT (live
, SSA_NAME_VERSION (res
));
5625 /* Do an RPO walk over the function computing SSA name liveness
5626 on-the-fly and deciding on assert expressions to insert.
5627 Returns true if there are assert expressions to be inserted. */
5630 find_assert_locations (void)
5632 int *rpo
= XNEWVEC (int, last_basic_block
);
5633 int *bb_rpo
= XNEWVEC (int, last_basic_block
);
5634 int *last_rpo
= XCNEWVEC (int, last_basic_block
);
5638 live
= XCNEWVEC (sbitmap
, last_basic_block
);
5639 rpo_cnt
= pre_and_rev_post_order_compute (NULL
, rpo
, false);
5640 for (i
= 0; i
< rpo_cnt
; ++i
)
5643 need_asserts
= false;
5644 for (i
= rpo_cnt
- 1; i
>= 0; --i
)
5646 basic_block bb
= BASIC_BLOCK (rpo
[i
]);
5652 live
[rpo
[i
]] = sbitmap_alloc (num_ssa_names
);
5653 sbitmap_zero (live
[rpo
[i
]]);
5656 /* Process BB and update the live information with uses in
5658 need_asserts
|= find_assert_locations_1 (bb
, live
[rpo
[i
]]);
5660 /* Merge liveness into the predecessor blocks and free it. */
5661 if (!sbitmap_empty_p (live
[rpo
[i
]]))
5664 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
5666 int pred
= e
->src
->index
;
5667 if ((e
->flags
& EDGE_DFS_BACK
) || pred
== ENTRY_BLOCK
)
5672 live
[pred
] = sbitmap_alloc (num_ssa_names
);
5673 sbitmap_zero (live
[pred
]);
5675 sbitmap_a_or_b (live
[pred
], live
[pred
], live
[rpo
[i
]]);
5677 if (bb_rpo
[pred
] < pred_rpo
)
5678 pred_rpo
= bb_rpo
[pred
];
5681 /* Record the RPO number of the last visited block that needs
5682 live information from this block. */
5683 last_rpo
[rpo
[i
]] = pred_rpo
;
5687 sbitmap_free (live
[rpo
[i
]]);
5688 live
[rpo
[i
]] = NULL
;
5691 /* We can free all successors live bitmaps if all their
5692 predecessors have been visited already. */
5693 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
5694 if (last_rpo
[e
->dest
->index
] == i
5695 && live
[e
->dest
->index
])
5697 sbitmap_free (live
[e
->dest
->index
]);
5698 live
[e
->dest
->index
] = NULL
;
5703 XDELETEVEC (bb_rpo
);
5704 XDELETEVEC (last_rpo
);
5705 for (i
= 0; i
< last_basic_block
; ++i
)
5707 sbitmap_free (live
[i
]);
5710 return need_asserts
;
5713 /* Create an ASSERT_EXPR for NAME and insert it in the location
5714 indicated by LOC. Return true if we made any edge insertions. */
5717 process_assert_insertions_for (tree name
, assert_locus_t loc
)
5719 /* Build the comparison expression NAME_i COMP_CODE VAL. */
5726 /* If we have X <=> X do not insert an assert expr for that. */
5727 if (loc
->expr
== loc
->val
)
5730 cond
= build2 (loc
->comp_code
, boolean_type_node
, loc
->expr
, loc
->val
);
5731 assert_stmt
= build_assert_expr_for (cond
, name
);
5734 /* We have been asked to insert the assertion on an edge. This
5735 is used only by COND_EXPR and SWITCH_EXPR assertions. */
5736 gcc_checking_assert (gimple_code (gsi_stmt (loc
->si
)) == GIMPLE_COND
5737 || (gimple_code (gsi_stmt (loc
->si
))
5740 gsi_insert_on_edge (loc
->e
, assert_stmt
);
5744 /* Otherwise, we can insert right after LOC->SI iff the
5745 statement must not be the last statement in the block. */
5746 stmt
= gsi_stmt (loc
->si
);
5747 if (!stmt_ends_bb_p (stmt
))
5749 gsi_insert_after (&loc
->si
, assert_stmt
, GSI_SAME_STMT
);
5753 /* If STMT must be the last statement in BB, we can only insert new
5754 assertions on the non-abnormal edge out of BB. Note that since
5755 STMT is not control flow, there may only be one non-abnormal edge
5757 FOR_EACH_EDGE (e
, ei
, loc
->bb
->succs
)
5758 if (!(e
->flags
& EDGE_ABNORMAL
))
5760 gsi_insert_on_edge (e
, assert_stmt
);
5768 /* Process all the insertions registered for every name N_i registered
5769 in NEED_ASSERT_FOR. The list of assertions to be inserted are
5770 found in ASSERTS_FOR[i]. */
5773 process_assert_insertions (void)
5777 bool update_edges_p
= false;
5778 int num_asserts
= 0;
5780 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
5781 dump_all_asserts (dump_file
);
5783 EXECUTE_IF_SET_IN_BITMAP (need_assert_for
, 0, i
, bi
)
5785 assert_locus_t loc
= asserts_for
[i
];
5790 assert_locus_t next
= loc
->next
;
5791 update_edges_p
|= process_assert_insertions_for (ssa_name (i
), loc
);
5799 gsi_commit_edge_inserts ();
5801 statistics_counter_event (cfun
, "Number of ASSERT_EXPR expressions inserted",
5806 /* Traverse the flowgraph looking for conditional jumps to insert range
5807 expressions. These range expressions are meant to provide information
5808 to optimizations that need to reason in terms of value ranges. They
5809 will not be expanded into RTL. For instance, given:
5818 this pass will transform the code into:
5824 x = ASSERT_EXPR <x, x < y>
5829 y = ASSERT_EXPR <y, x <= y>
5833 The idea is that once copy and constant propagation have run, other
5834 optimizations will be able to determine what ranges of values can 'x'
5835 take in different paths of the code, simply by checking the reaching
5836 definition of 'x'. */
5839 insert_range_assertions (void)
5841 need_assert_for
= BITMAP_ALLOC (NULL
);
5842 asserts_for
= XCNEWVEC (assert_locus_t
, num_ssa_names
);
5844 calculate_dominance_info (CDI_DOMINATORS
);
5846 if (find_assert_locations ())
5848 process_assert_insertions ();
5849 update_ssa (TODO_update_ssa_no_phi
);
5852 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
5854 fprintf (dump_file
, "\nSSA form after inserting ASSERT_EXPRs\n");
5855 dump_function_to_file (current_function_decl
, dump_file
, dump_flags
);
5859 BITMAP_FREE (need_assert_for
);
5862 /* Checks one ARRAY_REF in REF, located at LOCUS. Ignores flexible arrays
5863 and "struct" hacks. If VRP can determine that the
5864 array subscript is a constant, check if it is outside valid
5865 range. If the array subscript is a RANGE, warn if it is
5866 non-overlapping with valid range.
5867 IGNORE_OFF_BY_ONE is true if the ARRAY_REF is inside a ADDR_EXPR. */
5870 check_array_ref (location_t location
, tree ref
, bool ignore_off_by_one
)
5872 value_range_t
* vr
= NULL
;
5873 tree low_sub
, up_sub
;
5874 tree low_bound
, up_bound
, up_bound_p1
;
5877 if (TREE_NO_WARNING (ref
))
5880 low_sub
= up_sub
= TREE_OPERAND (ref
, 1);
5881 up_bound
= array_ref_up_bound (ref
);
5883 /* Can not check flexible arrays. */
5885 || TREE_CODE (up_bound
) != INTEGER_CST
)
5888 /* Accesses to trailing arrays via pointers may access storage
5889 beyond the types array bounds. */
5890 base
= get_base_address (ref
);
5891 if (base
&& TREE_CODE (base
) == MEM_REF
)
5893 tree cref
, next
= NULL_TREE
;
5895 if (TREE_CODE (TREE_OPERAND (ref
, 0)) != COMPONENT_REF
)
5898 cref
= TREE_OPERAND (ref
, 0);
5899 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (cref
, 0))) == RECORD_TYPE
)
5900 for (next
= DECL_CHAIN (TREE_OPERAND (cref
, 1));
5901 next
&& TREE_CODE (next
) != FIELD_DECL
;
5902 next
= DECL_CHAIN (next
))
5905 /* If this is the last field in a struct type or a field in a
5906 union type do not warn. */
5911 low_bound
= array_ref_low_bound (ref
);
5912 up_bound_p1
= int_const_binop (PLUS_EXPR
, up_bound
, integer_one_node
);
5914 if (TREE_CODE (low_sub
) == SSA_NAME
)
5916 vr
= get_value_range (low_sub
);
5917 if (vr
->type
== VR_RANGE
|| vr
->type
== VR_ANTI_RANGE
)
5919 low_sub
= vr
->type
== VR_RANGE
? vr
->max
: vr
->min
;
5920 up_sub
= vr
->type
== VR_RANGE
? vr
->min
: vr
->max
;
5924 if (vr
&& vr
->type
== VR_ANTI_RANGE
)
5926 if (TREE_CODE (up_sub
) == INTEGER_CST
5927 && tree_int_cst_lt (up_bound
, up_sub
)
5928 && TREE_CODE (low_sub
) == INTEGER_CST
5929 && tree_int_cst_lt (low_sub
, low_bound
))
5931 warning_at (location
, OPT_Warray_bounds
,
5932 "array subscript is outside array bounds");
5933 TREE_NO_WARNING (ref
) = 1;
5936 else if (TREE_CODE (up_sub
) == INTEGER_CST
5937 && (ignore_off_by_one
5938 ? (tree_int_cst_lt (up_bound
, up_sub
)
5939 && !tree_int_cst_equal (up_bound_p1
, up_sub
))
5940 : (tree_int_cst_lt (up_bound
, up_sub
)
5941 || tree_int_cst_equal (up_bound_p1
, up_sub
))))
5943 warning_at (location
, OPT_Warray_bounds
,
5944 "array subscript is above array bounds");
5945 TREE_NO_WARNING (ref
) = 1;
5947 else if (TREE_CODE (low_sub
) == INTEGER_CST
5948 && tree_int_cst_lt (low_sub
, low_bound
))
5950 warning_at (location
, OPT_Warray_bounds
,
5951 "array subscript is below array bounds");
5952 TREE_NO_WARNING (ref
) = 1;
5956 /* Searches if the expr T, located at LOCATION computes
5957 address of an ARRAY_REF, and call check_array_ref on it. */
5960 search_for_addr_array (tree t
, location_t location
)
5962 while (TREE_CODE (t
) == SSA_NAME
)
5964 gimple g
= SSA_NAME_DEF_STMT (t
);
5966 if (gimple_code (g
) != GIMPLE_ASSIGN
)
5969 if (get_gimple_rhs_class (gimple_assign_rhs_code (g
))
5970 != GIMPLE_SINGLE_RHS
)
5973 t
= gimple_assign_rhs1 (g
);
5977 /* We are only interested in addresses of ARRAY_REF's. */
5978 if (TREE_CODE (t
) != ADDR_EXPR
)
5981 /* Check each ARRAY_REFs in the reference chain. */
5984 if (TREE_CODE (t
) == ARRAY_REF
)
5985 check_array_ref (location
, t
, true /*ignore_off_by_one*/);
5987 t
= TREE_OPERAND (t
, 0);
5989 while (handled_component_p (t
));
5991 if (TREE_CODE (t
) == MEM_REF
5992 && TREE_CODE (TREE_OPERAND (t
, 0)) == ADDR_EXPR
5993 && !TREE_NO_WARNING (t
))
5995 tree tem
= TREE_OPERAND (TREE_OPERAND (t
, 0), 0);
5996 tree low_bound
, up_bound
, el_sz
;
5998 if (TREE_CODE (TREE_TYPE (tem
)) != ARRAY_TYPE
5999 || TREE_CODE (TREE_TYPE (TREE_TYPE (tem
))) == ARRAY_TYPE
6000 || !TYPE_DOMAIN (TREE_TYPE (tem
)))
6003 low_bound
= TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (tem
)));
6004 up_bound
= TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (tem
)));
6005 el_sz
= TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (tem
)));
6007 || TREE_CODE (low_bound
) != INTEGER_CST
6009 || TREE_CODE (up_bound
) != INTEGER_CST
6011 || TREE_CODE (el_sz
) != INTEGER_CST
)
6014 idx
= mem_ref_offset (t
);
6015 idx
= idx
.sdiv (tree_to_double_int (el_sz
), TRUNC_DIV_EXPR
);
6016 if (idx
.slt (double_int_zero
))
6018 warning_at (location
, OPT_Warray_bounds
,
6019 "array subscript is below array bounds");
6020 TREE_NO_WARNING (t
) = 1;
6022 else if (idx
.sgt (tree_to_double_int (up_bound
)
6023 - tree_to_double_int (low_bound
)
6026 warning_at (location
, OPT_Warray_bounds
,
6027 "array subscript is above array bounds");
6028 TREE_NO_WARNING (t
) = 1;
6033 /* walk_tree() callback that checks if *TP is
6034 an ARRAY_REF inside an ADDR_EXPR (in which an array
6035 subscript one outside the valid range is allowed). Call
6036 check_array_ref for each ARRAY_REF found. The location is
6040 check_array_bounds (tree
*tp
, int *walk_subtree
, void *data
)
6043 struct walk_stmt_info
*wi
= (struct walk_stmt_info
*) data
;
6044 location_t location
;
6046 if (EXPR_HAS_LOCATION (t
))
6047 location
= EXPR_LOCATION (t
);
6050 location_t
*locp
= (location_t
*) wi
->info
;
6054 *walk_subtree
= TRUE
;
6056 if (TREE_CODE (t
) == ARRAY_REF
)
6057 check_array_ref (location
, t
, false /*ignore_off_by_one*/);
6059 if (TREE_CODE (t
) == MEM_REF
6060 || (TREE_CODE (t
) == RETURN_EXPR
&& TREE_OPERAND (t
, 0)))
6061 search_for_addr_array (TREE_OPERAND (t
, 0), location
);
6063 if (TREE_CODE (t
) == ADDR_EXPR
)
6064 *walk_subtree
= FALSE
;
6069 /* Walk over all statements of all reachable BBs and call check_array_bounds
6073 check_all_array_refs (void)
6076 gimple_stmt_iterator si
;
6082 bool executable
= false;
6084 /* Skip blocks that were found to be unreachable. */
6085 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
6086 executable
|= !!(e
->flags
& EDGE_EXECUTABLE
);
6090 for (si
= gsi_start_bb (bb
); !gsi_end_p (si
); gsi_next (&si
))
6092 gimple stmt
= gsi_stmt (si
);
6093 struct walk_stmt_info wi
;
6094 if (!gimple_has_location (stmt
))
6097 if (is_gimple_call (stmt
))
6100 size_t n
= gimple_call_num_args (stmt
);
6101 for (i
= 0; i
< n
; i
++)
6103 tree arg
= gimple_call_arg (stmt
, i
);
6104 search_for_addr_array (arg
, gimple_location (stmt
));
6109 memset (&wi
, 0, sizeof (wi
));
6110 wi
.info
= CONST_CAST (void *, (const void *)
6111 gimple_location_ptr (stmt
));
6113 walk_gimple_op (gsi_stmt (si
),
6121 /* Convert range assertion expressions into the implied copies and
6122 copy propagate away the copies. Doing the trivial copy propagation
6123 here avoids the need to run the full copy propagation pass after
6126 FIXME, this will eventually lead to copy propagation removing the
6127 names that had useful range information attached to them. For
6128 instance, if we had the assertion N_i = ASSERT_EXPR <N_j, N_j > 3>,
6129 then N_i will have the range [3, +INF].
6131 However, by converting the assertion into the implied copy
6132 operation N_i = N_j, we will then copy-propagate N_j into the uses
6133 of N_i and lose the range information. We may want to hold on to
6134 ASSERT_EXPRs a little while longer as the ranges could be used in
6135 things like jump threading.
6137 The problem with keeping ASSERT_EXPRs around is that passes after
6138 VRP need to handle them appropriately.
6140 Another approach would be to make the range information a first
6141 class property of the SSA_NAME so that it can be queried from
6142 any pass. This is made somewhat more complex by the need for
6143 multiple ranges to be associated with one SSA_NAME. */
6146 remove_range_assertions (void)
6149 gimple_stmt_iterator si
;
6151 /* Note that the BSI iterator bump happens at the bottom of the
6152 loop and no bump is necessary if we're removing the statement
6153 referenced by the current BSI. */
6155 for (si
= gsi_start_bb (bb
); !gsi_end_p (si
);)
6157 gimple stmt
= gsi_stmt (si
);
6160 if (is_gimple_assign (stmt
)
6161 && gimple_assign_rhs_code (stmt
) == ASSERT_EXPR
)
6163 tree rhs
= gimple_assign_rhs1 (stmt
);
6165 tree cond
= fold (ASSERT_EXPR_COND (rhs
));
6166 use_operand_p use_p
;
6167 imm_use_iterator iter
;
6169 gcc_assert (cond
!= boolean_false_node
);
6171 /* Propagate the RHS into every use of the LHS. */
6172 var
= ASSERT_EXPR_VAR (rhs
);
6173 FOR_EACH_IMM_USE_STMT (use_stmt
, iter
,
6174 gimple_assign_lhs (stmt
))
6175 FOR_EACH_IMM_USE_ON_STMT (use_p
, iter
)
6177 SET_USE (use_p
, var
);
6178 gcc_assert (TREE_CODE (var
) == SSA_NAME
);
6181 /* And finally, remove the copy, it is not needed. */
6182 gsi_remove (&si
, true);
6183 release_defs (stmt
);
6191 /* Return true if STMT is interesting for VRP. */
6194 stmt_interesting_for_vrp (gimple stmt
)
6196 if (gimple_code (stmt
) == GIMPLE_PHI
)
6198 tree res
= gimple_phi_result (stmt
);
6199 return (!virtual_operand_p (res
)
6200 && (INTEGRAL_TYPE_P (TREE_TYPE (res
))
6201 || POINTER_TYPE_P (TREE_TYPE (res
))));
6203 else if (is_gimple_assign (stmt
) || is_gimple_call (stmt
))
6205 tree lhs
= gimple_get_lhs (stmt
);
6207 /* In general, assignments with virtual operands are not useful
6208 for deriving ranges, with the obvious exception of calls to
6209 builtin functions. */
6210 if (lhs
&& TREE_CODE (lhs
) == SSA_NAME
6211 && (INTEGRAL_TYPE_P (TREE_TYPE (lhs
))
6212 || POINTER_TYPE_P (TREE_TYPE (lhs
)))
6213 && ((is_gimple_call (stmt
)
6214 && gimple_call_fndecl (stmt
) != NULL_TREE
6215 && DECL_BUILT_IN (gimple_call_fndecl (stmt
)))
6216 || !gimple_vuse (stmt
)))
6219 else if (gimple_code (stmt
) == GIMPLE_COND
6220 || gimple_code (stmt
) == GIMPLE_SWITCH
)
6227 /* Initialize local data structures for VRP. */
6230 vrp_initialize (void)
6234 values_propagated
= false;
6235 num_vr_values
= num_ssa_names
;
6236 vr_value
= XCNEWVEC (value_range_t
*, num_vr_values
);
6237 vr_phi_edge_counts
= XCNEWVEC (int, num_ssa_names
);
6241 gimple_stmt_iterator si
;
6243 for (si
= gsi_start_phis (bb
); !gsi_end_p (si
); gsi_next (&si
))
6245 gimple phi
= gsi_stmt (si
);
6246 if (!stmt_interesting_for_vrp (phi
))
6248 tree lhs
= PHI_RESULT (phi
);
6249 set_value_range_to_varying (get_value_range (lhs
));
6250 prop_set_simulate_again (phi
, false);
6253 prop_set_simulate_again (phi
, true);
6256 for (si
= gsi_start_bb (bb
); !gsi_end_p (si
); gsi_next (&si
))
6258 gimple stmt
= gsi_stmt (si
);
6260 /* If the statement is a control insn, then we do not
6261 want to avoid simulating the statement once. Failure
6262 to do so means that those edges will never get added. */
6263 if (stmt_ends_bb_p (stmt
))
6264 prop_set_simulate_again (stmt
, true);
6265 else if (!stmt_interesting_for_vrp (stmt
))
6269 FOR_EACH_SSA_TREE_OPERAND (def
, stmt
, i
, SSA_OP_DEF
)
6270 set_value_range_to_varying (get_value_range (def
));
6271 prop_set_simulate_again (stmt
, false);
6274 prop_set_simulate_again (stmt
, true);
6279 /* Return the singleton value-range for NAME or NAME. */
6282 vrp_valueize (tree name
)
6284 if (TREE_CODE (name
) == SSA_NAME
)
6286 value_range_t
*vr
= get_value_range (name
);
6287 if (vr
->type
== VR_RANGE
6288 && (vr
->min
== vr
->max
6289 || operand_equal_p (vr
->min
, vr
->max
, 0)))
6295 /* Visit assignment STMT. If it produces an interesting range, record
6296 the SSA name in *OUTPUT_P. */
6298 static enum ssa_prop_result
6299 vrp_visit_assignment_or_call (gimple stmt
, tree
*output_p
)
6303 enum gimple_code code
= gimple_code (stmt
);
6304 lhs
= gimple_get_lhs (stmt
);
6306 /* We only keep track of ranges in integral and pointer types. */
6307 if (TREE_CODE (lhs
) == SSA_NAME
6308 && ((INTEGRAL_TYPE_P (TREE_TYPE (lhs
))
6309 /* It is valid to have NULL MIN/MAX values on a type. See
6310 build_range_type. */
6311 && TYPE_MIN_VALUE (TREE_TYPE (lhs
))
6312 && TYPE_MAX_VALUE (TREE_TYPE (lhs
)))
6313 || POINTER_TYPE_P (TREE_TYPE (lhs
))))
6315 value_range_t new_vr
= VR_INITIALIZER
;
6317 /* Try folding the statement to a constant first. */
6318 tree tem
= gimple_fold_stmt_to_constant (stmt
, vrp_valueize
);
6319 if (tem
&& !is_overflow_infinity (tem
))
6320 set_value_range (&new_vr
, VR_RANGE
, tem
, tem
, NULL
);
6321 /* Then dispatch to value-range extracting functions. */
6322 else if (code
== GIMPLE_CALL
)
6323 extract_range_basic (&new_vr
, stmt
);
6325 extract_range_from_assignment (&new_vr
, stmt
);
6327 if (update_value_range (lhs
, &new_vr
))
6331 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6333 fprintf (dump_file
, "Found new range for ");
6334 print_generic_expr (dump_file
, lhs
, 0);
6335 fprintf (dump_file
, ": ");
6336 dump_value_range (dump_file
, &new_vr
);
6337 fprintf (dump_file
, "\n\n");
6340 if (new_vr
.type
== VR_VARYING
)
6341 return SSA_PROP_VARYING
;
6343 return SSA_PROP_INTERESTING
;
6346 return SSA_PROP_NOT_INTERESTING
;
6349 /* Every other statement produces no useful ranges. */
6350 FOR_EACH_SSA_TREE_OPERAND (def
, stmt
, iter
, SSA_OP_DEF
)
6351 set_value_range_to_varying (get_value_range (def
));
6353 return SSA_PROP_VARYING
;
6356 /* Helper that gets the value range of the SSA_NAME with version I
6357 or a symbolic range containing the SSA_NAME only if the value range
6358 is varying or undefined. */
6360 static inline value_range_t
6361 get_vr_for_comparison (int i
)
6363 value_range_t vr
= *get_value_range (ssa_name (i
));
6365 /* If name N_i does not have a valid range, use N_i as its own
6366 range. This allows us to compare against names that may
6367 have N_i in their ranges. */
6368 if (vr
.type
== VR_VARYING
|| vr
.type
== VR_UNDEFINED
)
6371 vr
.min
= ssa_name (i
);
6372 vr
.max
= ssa_name (i
);
6378 /* Compare all the value ranges for names equivalent to VAR with VAL
6379 using comparison code COMP. Return the same value returned by
6380 compare_range_with_value, including the setting of
6381 *STRICT_OVERFLOW_P. */
6384 compare_name_with_value (enum tree_code comp
, tree var
, tree val
,
6385 bool *strict_overflow_p
)
6391 int used_strict_overflow
;
6393 value_range_t equiv_vr
;
6395 /* Get the set of equivalences for VAR. */
6396 e
= get_value_range (var
)->equiv
;
6398 /* Start at -1. Set it to 0 if we do a comparison without relying
6399 on overflow, or 1 if all comparisons rely on overflow. */
6400 used_strict_overflow
= -1;
6402 /* Compare vars' value range with val. */
6403 equiv_vr
= get_vr_for_comparison (SSA_NAME_VERSION (var
));
6405 retval
= compare_range_with_value (comp
, &equiv_vr
, val
, &sop
);
6407 used_strict_overflow
= sop
? 1 : 0;
6409 /* If the equiv set is empty we have done all work we need to do. */
6413 && used_strict_overflow
> 0)
6414 *strict_overflow_p
= true;
6418 EXECUTE_IF_SET_IN_BITMAP (e
, 0, i
, bi
)
6420 equiv_vr
= get_vr_for_comparison (i
);
6422 t
= compare_range_with_value (comp
, &equiv_vr
, val
, &sop
);
6425 /* If we get different answers from different members
6426 of the equivalence set this check must be in a dead
6427 code region. Folding it to a trap representation
6428 would be correct here. For now just return don't-know. */
6438 used_strict_overflow
= 0;
6439 else if (used_strict_overflow
< 0)
6440 used_strict_overflow
= 1;
6445 && used_strict_overflow
> 0)
6446 *strict_overflow_p
= true;
6452 /* Given a comparison code COMP and names N1 and N2, compare all the
6453 ranges equivalent to N1 against all the ranges equivalent to N2
6454 to determine the value of N1 COMP N2. Return the same value
6455 returned by compare_ranges. Set *STRICT_OVERFLOW_P to indicate
6456 whether we relied on an overflow infinity in the comparison. */
6460 compare_names (enum tree_code comp
, tree n1
, tree n2
,
6461 bool *strict_overflow_p
)
6465 bitmap_iterator bi1
, bi2
;
6467 int used_strict_overflow
;
6468 static bitmap_obstack
*s_obstack
= NULL
;
6469 static bitmap s_e1
= NULL
, s_e2
= NULL
;
6471 /* Compare the ranges of every name equivalent to N1 against the
6472 ranges of every name equivalent to N2. */
6473 e1
= get_value_range (n1
)->equiv
;
6474 e2
= get_value_range (n2
)->equiv
;
6476 /* Use the fake bitmaps if e1 or e2 are not available. */
6477 if (s_obstack
== NULL
)
6479 s_obstack
= XNEW (bitmap_obstack
);
6480 bitmap_obstack_initialize (s_obstack
);
6481 s_e1
= BITMAP_ALLOC (s_obstack
);
6482 s_e2
= BITMAP_ALLOC (s_obstack
);
6489 /* Add N1 and N2 to their own set of equivalences to avoid
6490 duplicating the body of the loop just to check N1 and N2
6492 bitmap_set_bit (e1
, SSA_NAME_VERSION (n1
));
6493 bitmap_set_bit (e2
, SSA_NAME_VERSION (n2
));
6495 /* If the equivalence sets have a common intersection, then the two
6496 names can be compared without checking their ranges. */
6497 if (bitmap_intersect_p (e1
, e2
))
6499 bitmap_clear_bit (e1
, SSA_NAME_VERSION (n1
));
6500 bitmap_clear_bit (e2
, SSA_NAME_VERSION (n2
));
6502 return (comp
== EQ_EXPR
|| comp
== GE_EXPR
|| comp
== LE_EXPR
)
6504 : boolean_false_node
;
6507 /* Start at -1. Set it to 0 if we do a comparison without relying
6508 on overflow, or 1 if all comparisons rely on overflow. */
6509 used_strict_overflow
= -1;
6511 /* Otherwise, compare all the equivalent ranges. First, add N1 and
6512 N2 to their own set of equivalences to avoid duplicating the body
6513 of the loop just to check N1 and N2 ranges. */
6514 EXECUTE_IF_SET_IN_BITMAP (e1
, 0, i1
, bi1
)
6516 value_range_t vr1
= get_vr_for_comparison (i1
);
6518 t
= retval
= NULL_TREE
;
6519 EXECUTE_IF_SET_IN_BITMAP (e2
, 0, i2
, bi2
)
6523 value_range_t vr2
= get_vr_for_comparison (i2
);
6525 t
= compare_ranges (comp
, &vr1
, &vr2
, &sop
);
6528 /* If we get different answers from different members
6529 of the equivalence set this check must be in a dead
6530 code region. Folding it to a trap representation
6531 would be correct here. For now just return don't-know. */
6535 bitmap_clear_bit (e1
, SSA_NAME_VERSION (n1
));
6536 bitmap_clear_bit (e2
, SSA_NAME_VERSION (n2
));
6542 used_strict_overflow
= 0;
6543 else if (used_strict_overflow
< 0)
6544 used_strict_overflow
= 1;
6550 bitmap_clear_bit (e1
, SSA_NAME_VERSION (n1
));
6551 bitmap_clear_bit (e2
, SSA_NAME_VERSION (n2
));
6552 if (used_strict_overflow
> 0)
6553 *strict_overflow_p
= true;
6558 /* None of the equivalent ranges are useful in computing this
6560 bitmap_clear_bit (e1
, SSA_NAME_VERSION (n1
));
6561 bitmap_clear_bit (e2
, SSA_NAME_VERSION (n2
));
6565 /* Helper function for vrp_evaluate_conditional_warnv. */
6568 vrp_evaluate_conditional_warnv_with_ops_using_ranges (enum tree_code code
,
6570 bool * strict_overflow_p
)
6572 value_range_t
*vr0
, *vr1
;
6574 vr0
= (TREE_CODE (op0
) == SSA_NAME
) ? get_value_range (op0
) : NULL
;
6575 vr1
= (TREE_CODE (op1
) == SSA_NAME
) ? get_value_range (op1
) : NULL
;
6578 return compare_ranges (code
, vr0
, vr1
, strict_overflow_p
);
6579 else if (vr0
&& vr1
== NULL
)
6580 return compare_range_with_value (code
, vr0
, op1
, strict_overflow_p
);
6581 else if (vr0
== NULL
&& vr1
)
6582 return (compare_range_with_value
6583 (swap_tree_comparison (code
), vr1
, op0
, strict_overflow_p
));
6587 /* Helper function for vrp_evaluate_conditional_warnv. */
6590 vrp_evaluate_conditional_warnv_with_ops (enum tree_code code
, tree op0
,
6591 tree op1
, bool use_equiv_p
,
6592 bool *strict_overflow_p
, bool *only_ranges
)
6596 *only_ranges
= true;
6598 /* We only deal with integral and pointer types. */
6599 if (!INTEGRAL_TYPE_P (TREE_TYPE (op0
))
6600 && !POINTER_TYPE_P (TREE_TYPE (op0
)))
6606 && (ret
= vrp_evaluate_conditional_warnv_with_ops_using_ranges
6607 (code
, op0
, op1
, strict_overflow_p
)))
6609 *only_ranges
= false;
6610 if (TREE_CODE (op0
) == SSA_NAME
&& TREE_CODE (op1
) == SSA_NAME
)
6611 return compare_names (code
, op0
, op1
, strict_overflow_p
);
6612 else if (TREE_CODE (op0
) == SSA_NAME
)
6613 return compare_name_with_value (code
, op0
, op1
, strict_overflow_p
);
6614 else if (TREE_CODE (op1
) == SSA_NAME
)
6615 return (compare_name_with_value
6616 (swap_tree_comparison (code
), op1
, op0
, strict_overflow_p
));
6619 return vrp_evaluate_conditional_warnv_with_ops_using_ranges (code
, op0
, op1
,
6624 /* Given (CODE OP0 OP1) within STMT, try to simplify it based on value range
6625 information. Return NULL if the conditional can not be evaluated.
6626 The ranges of all the names equivalent with the operands in COND
6627 will be used when trying to compute the value. If the result is
6628 based on undefined signed overflow, issue a warning if
6632 vrp_evaluate_conditional (enum tree_code code
, tree op0
, tree op1
, gimple stmt
)
6638 /* Some passes and foldings leak constants with overflow flag set
6639 into the IL. Avoid doing wrong things with these and bail out. */
6640 if ((TREE_CODE (op0
) == INTEGER_CST
6641 && TREE_OVERFLOW (op0
))
6642 || (TREE_CODE (op1
) == INTEGER_CST
6643 && TREE_OVERFLOW (op1
)))
6647 ret
= vrp_evaluate_conditional_warnv_with_ops (code
, op0
, op1
, true, &sop
,
6652 enum warn_strict_overflow_code wc
;
6653 const char* warnmsg
;
6655 if (is_gimple_min_invariant (ret
))
6657 wc
= WARN_STRICT_OVERFLOW_CONDITIONAL
;
6658 warnmsg
= G_("assuming signed overflow does not occur when "
6659 "simplifying conditional to constant");
6663 wc
= WARN_STRICT_OVERFLOW_COMPARISON
;
6664 warnmsg
= G_("assuming signed overflow does not occur when "
6665 "simplifying conditional");
6668 if (issue_strict_overflow_warning (wc
))
6670 location_t location
;
6672 if (!gimple_has_location (stmt
))
6673 location
= input_location
;
6675 location
= gimple_location (stmt
);
6676 warning_at (location
, OPT_Wstrict_overflow
, "%s", warnmsg
);
6680 if (warn_type_limits
6681 && ret
&& only_ranges
6682 && TREE_CODE_CLASS (code
) == tcc_comparison
6683 && TREE_CODE (op0
) == SSA_NAME
)
6685 /* If the comparison is being folded and the operand on the LHS
6686 is being compared against a constant value that is outside of
6687 the natural range of OP0's type, then the predicate will
6688 always fold regardless of the value of OP0. If -Wtype-limits
6689 was specified, emit a warning. */
6690 tree type
= TREE_TYPE (op0
);
6691 value_range_t
*vr0
= get_value_range (op0
);
6693 if (vr0
->type
!= VR_VARYING
6694 && INTEGRAL_TYPE_P (type
)
6695 && vrp_val_is_min (vr0
->min
)
6696 && vrp_val_is_max (vr0
->max
)
6697 && is_gimple_min_invariant (op1
))
6699 location_t location
;
6701 if (!gimple_has_location (stmt
))
6702 location
= input_location
;
6704 location
= gimple_location (stmt
);
6706 warning_at (location
, OPT_Wtype_limits
,
6708 ? G_("comparison always false "
6709 "due to limited range of data type")
6710 : G_("comparison always true "
6711 "due to limited range of data type"));
6719 /* Visit conditional statement STMT. If we can determine which edge
6720 will be taken out of STMT's basic block, record it in
6721 *TAKEN_EDGE_P and return SSA_PROP_INTERESTING. Otherwise, return
6722 SSA_PROP_VARYING. */
6724 static enum ssa_prop_result
6725 vrp_visit_cond_stmt (gimple stmt
, edge
*taken_edge_p
)
6730 *taken_edge_p
= NULL
;
6732 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6737 fprintf (dump_file
, "\nVisiting conditional with predicate: ");
6738 print_gimple_stmt (dump_file
, stmt
, 0, 0);
6739 fprintf (dump_file
, "\nWith known ranges\n");
6741 FOR_EACH_SSA_TREE_OPERAND (use
, stmt
, i
, SSA_OP_USE
)
6743 fprintf (dump_file
, "\t");
6744 print_generic_expr (dump_file
, use
, 0);
6745 fprintf (dump_file
, ": ");
6746 dump_value_range (dump_file
, vr_value
[SSA_NAME_VERSION (use
)]);
6749 fprintf (dump_file
, "\n");
6752 /* Compute the value of the predicate COND by checking the known
6753 ranges of each of its operands.
6755 Note that we cannot evaluate all the equivalent ranges here
6756 because those ranges may not yet be final and with the current
6757 propagation strategy, we cannot determine when the value ranges
6758 of the names in the equivalence set have changed.
6760 For instance, given the following code fragment
6764 i_14 = ASSERT_EXPR <i_5, i_5 != 0>
6768 Assume that on the first visit to i_14, i_5 has the temporary
6769 range [8, 8] because the second argument to the PHI function is
6770 not yet executable. We derive the range ~[0, 0] for i_14 and the
6771 equivalence set { i_5 }. So, when we visit 'if (i_14 == 1)' for
6772 the first time, since i_14 is equivalent to the range [8, 8], we
6773 determine that the predicate is always false.
6775 On the next round of propagation, i_13 is determined to be
6776 VARYING, which causes i_5 to drop down to VARYING. So, another
6777 visit to i_14 is scheduled. In this second visit, we compute the
6778 exact same range and equivalence set for i_14, namely ~[0, 0] and
6779 { i_5 }. But we did not have the previous range for i_5
6780 registered, so vrp_visit_assignment thinks that the range for
6781 i_14 has not changed. Therefore, the predicate 'if (i_14 == 1)'
6782 is not visited again, which stops propagation from visiting
6783 statements in the THEN clause of that if().
6785 To properly fix this we would need to keep the previous range
6786 value for the names in the equivalence set. This way we would've
6787 discovered that from one visit to the other i_5 changed from
6788 range [8, 8] to VR_VARYING.
6790 However, fixing this apparent limitation may not be worth the
6791 additional checking. Testing on several code bases (GCC, DLV,
6792 MICO, TRAMP3D and SPEC2000) showed that doing this results in
6793 4 more predicates folded in SPEC. */
6796 val
= vrp_evaluate_conditional_warnv_with_ops (gimple_cond_code (stmt
),
6797 gimple_cond_lhs (stmt
),
6798 gimple_cond_rhs (stmt
),
6803 *taken_edge_p
= find_taken_edge (gimple_bb (stmt
), val
);
6806 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6808 "\nIgnoring predicate evaluation because "
6809 "it assumes that signed overflow is undefined");
6814 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6816 fprintf (dump_file
, "\nPredicate evaluates to: ");
6817 if (val
== NULL_TREE
)
6818 fprintf (dump_file
, "DON'T KNOW\n");
6820 print_generic_stmt (dump_file
, val
, 0);
6823 return (*taken_edge_p
) ? SSA_PROP_INTERESTING
: SSA_PROP_VARYING
;
6826 /* Searches the case label vector VEC for the index *IDX of the CASE_LABEL
6827 that includes the value VAL. The search is restricted to the range
6828 [START_IDX, n - 1] where n is the size of VEC.
6830 If there is a CASE_LABEL for VAL, its index is placed in IDX and true is
6833 If there is no CASE_LABEL for VAL and there is one that is larger than VAL,
6834 it is placed in IDX and false is returned.
6836 If VAL is larger than any CASE_LABEL, n is placed on IDX and false is
6840 find_case_label_index (gimple stmt
, size_t start_idx
, tree val
, size_t *idx
)
6842 size_t n
= gimple_switch_num_labels (stmt
);
6845 /* Find case label for minimum of the value range or the next one.
6846 At each iteration we are searching in [low, high - 1]. */
6848 for (low
= start_idx
, high
= n
; high
!= low
; )
6852 /* Note that i != high, so we never ask for n. */
6853 size_t i
= (high
+ low
) / 2;
6854 t
= gimple_switch_label (stmt
, i
);
6856 /* Cache the result of comparing CASE_LOW and val. */
6857 cmp
= tree_int_cst_compare (CASE_LOW (t
), val
);
6861 /* Ranges cannot be empty. */
6870 if (CASE_HIGH (t
) != NULL
6871 && tree_int_cst_compare (CASE_HIGH (t
), val
) >= 0)
6883 /* Searches the case label vector VEC for the range of CASE_LABELs that is used
6884 for values between MIN and MAX. The first index is placed in MIN_IDX. The
6885 last index is placed in MAX_IDX. If the range of CASE_LABELs is empty
6886 then MAX_IDX < MIN_IDX.
6887 Returns true if the default label is not needed. */
6890 find_case_label_range (gimple stmt
, tree min
, tree max
, size_t *min_idx
,
6894 bool min_take_default
= !find_case_label_index (stmt
, 1, min
, &i
);
6895 bool max_take_default
= !find_case_label_index (stmt
, i
, max
, &j
);
6899 && max_take_default
)
6901 /* Only the default case label reached.
6902 Return an empty range. */
6909 bool take_default
= min_take_default
|| max_take_default
;
6913 if (max_take_default
)
6916 /* If the case label range is continuous, we do not need
6917 the default case label. Verify that. */
6918 high
= CASE_LOW (gimple_switch_label (stmt
, i
));
6919 if (CASE_HIGH (gimple_switch_label (stmt
, i
)))
6920 high
= CASE_HIGH (gimple_switch_label (stmt
, i
));
6921 for (k
= i
+ 1; k
<= j
; ++k
)
6923 low
= CASE_LOW (gimple_switch_label (stmt
, k
));
6924 if (!integer_onep (int_const_binop (MINUS_EXPR
, low
, high
)))
6926 take_default
= true;
6930 if (CASE_HIGH (gimple_switch_label (stmt
, k
)))
6931 high
= CASE_HIGH (gimple_switch_label (stmt
, k
));
6936 return !take_default
;
6940 /* Searches the case label vector VEC for the ranges of CASE_LABELs that are
6941 used in range VR. The indices are placed in MIN_IDX1, MAX_IDX, MIN_IDX2 and
6942 MAX_IDX2. If the ranges of CASE_LABELs are empty then MAX_IDX1 < MIN_IDX1.
6943 Returns true if the default label is not needed. */
6946 find_case_label_ranges (gimple stmt
, value_range_t
*vr
, size_t *min_idx1
,
6947 size_t *max_idx1
, size_t *min_idx2
,
6951 unsigned int n
= gimple_switch_num_labels (stmt
);
6953 tree case_low
, case_high
;
6954 tree min
= vr
->min
, max
= vr
->max
;
6956 gcc_checking_assert (vr
->type
== VR_RANGE
|| vr
->type
== VR_ANTI_RANGE
);
6958 take_default
= !find_case_label_range (stmt
, min
, max
, &i
, &j
);
6960 /* Set second range to emtpy. */
6964 if (vr
->type
== VR_RANGE
)
6968 return !take_default
;
6971 /* Set first range to all case labels. */
6978 /* Make sure all the values of case labels [i , j] are contained in
6979 range [MIN, MAX]. */
6980 case_low
= CASE_LOW (gimple_switch_label (stmt
, i
));
6981 case_high
= CASE_HIGH (gimple_switch_label (stmt
, j
));
6982 if (tree_int_cst_compare (case_low
, min
) < 0)
6984 if (case_high
!= NULL_TREE
6985 && tree_int_cst_compare (max
, case_high
) < 0)
6991 /* If the range spans case labels [i, j], the corresponding anti-range spans
6992 the labels [1, i - 1] and [j + 1, n - 1]. */
7018 /* Visit switch statement STMT. If we can determine which edge
7019 will be taken out of STMT's basic block, record it in
7020 *TAKEN_EDGE_P and return SSA_PROP_INTERESTING. Otherwise, return
7021 SSA_PROP_VARYING. */
7023 static enum ssa_prop_result
7024 vrp_visit_switch_stmt (gimple stmt
, edge
*taken_edge_p
)
7028 size_t i
= 0, j
= 0, k
, l
;
7031 *taken_edge_p
= NULL
;
7032 op
= gimple_switch_index (stmt
);
7033 if (TREE_CODE (op
) != SSA_NAME
)
7034 return SSA_PROP_VARYING
;
7036 vr
= get_value_range (op
);
7037 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7039 fprintf (dump_file
, "\nVisiting switch expression with operand ");
7040 print_generic_expr (dump_file
, op
, 0);
7041 fprintf (dump_file
, " with known range ");
7042 dump_value_range (dump_file
, vr
);
7043 fprintf (dump_file
, "\n");
7046 if ((vr
->type
!= VR_RANGE
7047 && vr
->type
!= VR_ANTI_RANGE
)
7048 || symbolic_range_p (vr
))
7049 return SSA_PROP_VARYING
;
7051 /* Find the single edge that is taken from the switch expression. */
7052 take_default
= !find_case_label_ranges (stmt
, vr
, &i
, &j
, &k
, &l
);
7054 /* Check if the range spans no CASE_LABEL. If so, we only reach the default
7058 gcc_assert (take_default
);
7059 val
= gimple_switch_default_label (stmt
);
7063 /* Check if labels with index i to j and maybe the default label
7064 are all reaching the same label. */
7066 val
= gimple_switch_label (stmt
, i
);
7068 && CASE_LABEL (gimple_switch_default_label (stmt
))
7069 != CASE_LABEL (val
))
7071 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7072 fprintf (dump_file
, " not a single destination for this "
7074 return SSA_PROP_VARYING
;
7076 for (++i
; i
<= j
; ++i
)
7078 if (CASE_LABEL (gimple_switch_label (stmt
, i
)) != CASE_LABEL (val
))
7080 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7081 fprintf (dump_file
, " not a single destination for this "
7083 return SSA_PROP_VARYING
;
7088 if (CASE_LABEL (gimple_switch_label (stmt
, k
)) != CASE_LABEL (val
))
7090 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7091 fprintf (dump_file
, " not a single destination for this "
7093 return SSA_PROP_VARYING
;
7098 *taken_edge_p
= find_edge (gimple_bb (stmt
),
7099 label_to_block (CASE_LABEL (val
)));
7101 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7103 fprintf (dump_file
, " will take edge to ");
7104 print_generic_stmt (dump_file
, CASE_LABEL (val
), 0);
7107 return SSA_PROP_INTERESTING
;
7111 /* Evaluate statement STMT. If the statement produces a useful range,
7112 return SSA_PROP_INTERESTING and record the SSA name with the
7113 interesting range into *OUTPUT_P.
7115 If STMT is a conditional branch and we can determine its truth
7116 value, the taken edge is recorded in *TAKEN_EDGE_P.
7118 If STMT produces a varying value, return SSA_PROP_VARYING. */
7120 static enum ssa_prop_result
7121 vrp_visit_stmt (gimple stmt
, edge
*taken_edge_p
, tree
*output_p
)
7126 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7128 fprintf (dump_file
, "\nVisiting statement:\n");
7129 print_gimple_stmt (dump_file
, stmt
, 0, dump_flags
);
7130 fprintf (dump_file
, "\n");
7133 if (!stmt_interesting_for_vrp (stmt
))
7134 gcc_assert (stmt_ends_bb_p (stmt
));
7135 else if (is_gimple_assign (stmt
) || is_gimple_call (stmt
))
7137 /* In general, assignments with virtual operands are not useful
7138 for deriving ranges, with the obvious exception of calls to
7139 builtin functions. */
7140 if ((is_gimple_call (stmt
)
7141 && gimple_call_fndecl (stmt
) != NULL_TREE
7142 && DECL_BUILT_IN (gimple_call_fndecl (stmt
)))
7143 || !gimple_vuse (stmt
))
7144 return vrp_visit_assignment_or_call (stmt
, output_p
);
7146 else if (gimple_code (stmt
) == GIMPLE_COND
)
7147 return vrp_visit_cond_stmt (stmt
, taken_edge_p
);
7148 else if (gimple_code (stmt
) == GIMPLE_SWITCH
)
7149 return vrp_visit_switch_stmt (stmt
, taken_edge_p
);
7151 /* All other statements produce nothing of interest for VRP, so mark
7152 their outputs varying and prevent further simulation. */
7153 FOR_EACH_SSA_TREE_OPERAND (def
, stmt
, iter
, SSA_OP_DEF
)
7154 set_value_range_to_varying (get_value_range (def
));
7156 return SSA_PROP_VARYING
;
7159 /* Union the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
7160 { VR1TYPE, VR0MIN, VR0MAX } and store the result
7161 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
7162 possible such range. The resulting range is not canonicalized. */
7165 union_ranges (enum value_range_type
*vr0type
,
7166 tree
*vr0min
, tree
*vr0max
,
7167 enum value_range_type vr1type
,
7168 tree vr1min
, tree vr1max
)
7170 bool mineq
= operand_equal_p (*vr0min
, vr1min
, 0);
7171 bool maxeq
= operand_equal_p (*vr0max
, vr1max
, 0);
7173 /* [] is vr0, () is vr1 in the following classification comments. */
7177 if (*vr0type
== vr1type
)
7178 /* Nothing to do for equal ranges. */
7180 else if ((*vr0type
== VR_RANGE
7181 && vr1type
== VR_ANTI_RANGE
)
7182 || (*vr0type
== VR_ANTI_RANGE
7183 && vr1type
== VR_RANGE
))
7185 /* For anti-range with range union the result is varying. */
7191 else if (operand_less_p (*vr0max
, vr1min
) == 1
7192 || operand_less_p (vr1max
, *vr0min
) == 1)
7194 /* [ ] ( ) or ( ) [ ]
7195 If the ranges have an empty intersection, result of the union
7196 operation is the anti-range or if both are anti-ranges
7198 if (*vr0type
== VR_ANTI_RANGE
7199 && vr1type
== VR_ANTI_RANGE
)
7201 else if (*vr0type
== VR_ANTI_RANGE
7202 && vr1type
== VR_RANGE
)
7204 else if (*vr0type
== VR_RANGE
7205 && vr1type
== VR_ANTI_RANGE
)
7211 else if (*vr0type
== VR_RANGE
7212 && vr1type
== VR_RANGE
)
7214 /* The result is the convex hull of both ranges. */
7215 if (operand_less_p (*vr0max
, vr1min
) == 1)
7217 /* If the result can be an anti-range, create one. */
7218 if (TREE_CODE (*vr0max
) == INTEGER_CST
7219 && TREE_CODE (vr1min
) == INTEGER_CST
7220 && vrp_val_is_min (*vr0min
)
7221 && vrp_val_is_max (vr1max
))
7223 tree min
= int_const_binop (PLUS_EXPR
,
7224 *vr0max
, integer_one_node
);
7225 tree max
= int_const_binop (MINUS_EXPR
,
7226 vr1min
, integer_one_node
);
7227 if (!operand_less_p (max
, min
))
7229 *vr0type
= VR_ANTI_RANGE
;
7241 /* If the result can be an anti-range, create one. */
7242 if (TREE_CODE (vr1max
) == INTEGER_CST
7243 && TREE_CODE (*vr0min
) == INTEGER_CST
7244 && vrp_val_is_min (vr1min
)
7245 && vrp_val_is_max (*vr0max
))
7247 tree min
= int_const_binop (PLUS_EXPR
,
7248 vr1max
, integer_one_node
);
7249 tree max
= int_const_binop (MINUS_EXPR
,
7250 *vr0min
, integer_one_node
);
7251 if (!operand_less_p (max
, min
))
7253 *vr0type
= VR_ANTI_RANGE
;
7267 else if ((maxeq
|| operand_less_p (vr1max
, *vr0max
) == 1)
7268 && (mineq
|| operand_less_p (*vr0min
, vr1min
) == 1))
7270 /* [ ( ) ] or [( ) ] or [ ( )] */
7271 if (*vr0type
== VR_RANGE
7272 && vr1type
== VR_RANGE
)
7274 else if (*vr0type
== VR_ANTI_RANGE
7275 && vr1type
== VR_ANTI_RANGE
)
7281 else if (*vr0type
== VR_ANTI_RANGE
7282 && vr1type
== VR_RANGE
)
7284 /* Arbitrarily choose the right or left gap. */
7285 if (!mineq
&& TREE_CODE (vr1min
) == INTEGER_CST
)
7286 *vr0max
= int_const_binop (MINUS_EXPR
, vr1min
, integer_one_node
);
7287 else if (!maxeq
&& TREE_CODE (vr1max
) == INTEGER_CST
)
7288 *vr0min
= int_const_binop (PLUS_EXPR
, vr1max
, integer_one_node
);
7292 else if (*vr0type
== VR_RANGE
7293 && vr1type
== VR_ANTI_RANGE
)
7294 /* The result covers everything. */
7299 else if ((maxeq
|| operand_less_p (*vr0max
, vr1max
) == 1)
7300 && (mineq
|| operand_less_p (vr1min
, *vr0min
) == 1))
7302 /* ( [ ] ) or ([ ] ) or ( [ ]) */
7303 if (*vr0type
== VR_RANGE
7304 && vr1type
== VR_RANGE
)
7310 else if (*vr0type
== VR_ANTI_RANGE
7311 && vr1type
== VR_ANTI_RANGE
)
7313 else if (*vr0type
== VR_RANGE
7314 && vr1type
== VR_ANTI_RANGE
)
7316 *vr0type
= VR_ANTI_RANGE
;
7317 if (!mineq
&& TREE_CODE (*vr0min
) == INTEGER_CST
)
7319 *vr0max
= int_const_binop (MINUS_EXPR
, *vr0min
, integer_one_node
);
7322 else if (!maxeq
&& TREE_CODE (*vr0max
) == INTEGER_CST
)
7324 *vr0min
= int_const_binop (PLUS_EXPR
, *vr0max
, integer_one_node
);
7330 else if (*vr0type
== VR_ANTI_RANGE
7331 && vr1type
== VR_RANGE
)
7332 /* The result covers everything. */
7337 else if ((operand_less_p (vr1min
, *vr0max
) == 1
7338 || operand_equal_p (vr1min
, *vr0max
, 0))
7339 && operand_less_p (*vr0min
, vr1min
) == 1)
7341 /* [ ( ] ) or [ ]( ) */
7342 if (*vr0type
== VR_RANGE
7343 && vr1type
== VR_RANGE
)
7345 else if (*vr0type
== VR_ANTI_RANGE
7346 && vr1type
== VR_ANTI_RANGE
)
7348 else if (*vr0type
== VR_ANTI_RANGE
7349 && vr1type
== VR_RANGE
)
7351 if (TREE_CODE (vr1min
) == INTEGER_CST
)
7352 *vr0max
= int_const_binop (MINUS_EXPR
, vr1min
, integer_one_node
);
7356 else if (*vr0type
== VR_RANGE
7357 && vr1type
== VR_ANTI_RANGE
)
7359 if (TREE_CODE (*vr0max
) == INTEGER_CST
)
7362 *vr0min
= int_const_binop (PLUS_EXPR
, *vr0max
, integer_one_node
);
7371 else if ((operand_less_p (*vr0min
, vr1max
) == 1
7372 || operand_equal_p (*vr0min
, vr1max
, 0))
7373 && operand_less_p (vr1min
, *vr0min
) == 1)
7375 /* ( [ ) ] or ( )[ ] */
7376 if (*vr0type
== VR_RANGE
7377 && vr1type
== VR_RANGE
)
7379 else if (*vr0type
== VR_ANTI_RANGE
7380 && vr1type
== VR_ANTI_RANGE
)
7382 else if (*vr0type
== VR_ANTI_RANGE
7383 && vr1type
== VR_RANGE
)
7385 if (TREE_CODE (vr1max
) == INTEGER_CST
)
7386 *vr0min
= int_const_binop (PLUS_EXPR
, vr1max
, integer_one_node
);
7390 else if (*vr0type
== VR_RANGE
7391 && vr1type
== VR_ANTI_RANGE
)
7393 if (TREE_CODE (*vr0min
) == INTEGER_CST
)
7397 *vr0max
= int_const_binop (MINUS_EXPR
, *vr0min
, integer_one_node
);
7411 *vr0type
= VR_VARYING
;
7412 *vr0min
= NULL_TREE
;
7413 *vr0max
= NULL_TREE
;
7416 /* Intersect the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
7417 { VR1TYPE, VR0MIN, VR0MAX } and store the result
7418 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
7419 possible such range. The resulting range is not canonicalized. */
7422 intersect_ranges (enum value_range_type
*vr0type
,
7423 tree
*vr0min
, tree
*vr0max
,
7424 enum value_range_type vr1type
,
7425 tree vr1min
, tree vr1max
)
7427 bool mineq
= operand_equal_p (*vr0min
, vr1min
, 0);
7428 bool maxeq
= operand_equal_p (*vr0max
, vr1max
, 0);
7430 /* [] is vr0, () is vr1 in the following classification comments. */
7434 if (*vr0type
== vr1type
)
7435 /* Nothing to do for equal ranges. */
7437 else if ((*vr0type
== VR_RANGE
7438 && vr1type
== VR_ANTI_RANGE
)
7439 || (*vr0type
== VR_ANTI_RANGE
7440 && vr1type
== VR_RANGE
))
7442 /* For anti-range with range intersection the result is empty. */
7443 *vr0type
= VR_UNDEFINED
;
7444 *vr0min
= NULL_TREE
;
7445 *vr0max
= NULL_TREE
;
7450 else if (operand_less_p (*vr0max
, vr1min
) == 1
7451 || operand_less_p (vr1max
, *vr0min
) == 1)
7453 /* [ ] ( ) or ( ) [ ]
7454 If the ranges have an empty intersection, the result of the
7455 intersect operation is the range for intersecting an
7456 anti-range with a range or empty when intersecting two ranges. */
7457 if (*vr0type
== VR_RANGE
7458 && vr1type
== VR_ANTI_RANGE
)
7460 else if (*vr0type
== VR_ANTI_RANGE
7461 && vr1type
== VR_RANGE
)
7467 else if (*vr0type
== VR_RANGE
7468 && vr1type
== VR_RANGE
)
7470 *vr0type
= VR_UNDEFINED
;
7471 *vr0min
= NULL_TREE
;
7472 *vr0max
= NULL_TREE
;
7474 else if (*vr0type
== VR_ANTI_RANGE
7475 && vr1type
== VR_ANTI_RANGE
)
7477 /* If the anti-ranges are adjacent to each other merge them. */
7478 if (TREE_CODE (*vr0max
) == INTEGER_CST
7479 && TREE_CODE (vr1min
) == INTEGER_CST
7480 && operand_less_p (*vr0max
, vr1min
) == 1
7481 && integer_onep (int_const_binop (MINUS_EXPR
,
7484 else if (TREE_CODE (vr1max
) == INTEGER_CST
7485 && TREE_CODE (*vr0min
) == INTEGER_CST
7486 && operand_less_p (vr1max
, *vr0min
) == 1
7487 && integer_onep (int_const_binop (MINUS_EXPR
,
7490 /* Else arbitrarily take VR0. */
7493 else if ((maxeq
|| operand_less_p (vr1max
, *vr0max
) == 1)
7494 && (mineq
|| operand_less_p (*vr0min
, vr1min
) == 1))
7496 /* [ ( ) ] or [( ) ] or [ ( )] */
7497 if (*vr0type
== VR_RANGE
7498 && vr1type
== VR_RANGE
)
7500 /* If both are ranges the result is the inner one. */
7505 else if (*vr0type
== VR_RANGE
7506 && vr1type
== VR_ANTI_RANGE
)
7508 /* Choose the right gap if the left one is empty. */
7511 if (TREE_CODE (vr1max
) == INTEGER_CST
)
7512 *vr0min
= int_const_binop (PLUS_EXPR
, vr1max
, integer_one_node
);
7516 /* Choose the left gap if the right one is empty. */
7519 if (TREE_CODE (vr1min
) == INTEGER_CST
)
7520 *vr0max
= int_const_binop (MINUS_EXPR
, vr1min
,
7525 /* Choose the anti-range if the range is effectively varying. */
7526 else if (vrp_val_is_min (*vr0min
)
7527 && vrp_val_is_max (*vr0max
))
7533 /* Else choose the range. */
7535 else if (*vr0type
== VR_ANTI_RANGE
7536 && vr1type
== VR_ANTI_RANGE
)
7537 /* If both are anti-ranges the result is the outer one. */
7539 else if (*vr0type
== VR_ANTI_RANGE
7540 && vr1type
== VR_RANGE
)
7542 /* The intersection is empty. */
7543 *vr0type
= VR_UNDEFINED
;
7544 *vr0min
= NULL_TREE
;
7545 *vr0max
= NULL_TREE
;
7550 else if ((maxeq
|| operand_less_p (*vr0max
, vr1max
) == 1)
7551 && (mineq
|| operand_less_p (vr1min
, *vr0min
) == 1))
7553 /* ( [ ] ) or ([ ] ) or ( [ ]) */
7554 if (*vr0type
== VR_RANGE
7555 && vr1type
== VR_RANGE
)
7556 /* Choose the inner range. */
7558 else if (*vr0type
== VR_ANTI_RANGE
7559 && vr1type
== VR_RANGE
)
7561 /* Choose the right gap if the left is empty. */
7564 *vr0type
= VR_RANGE
;
7565 if (TREE_CODE (*vr0max
) == INTEGER_CST
)
7566 *vr0min
= int_const_binop (PLUS_EXPR
, *vr0max
,
7572 /* Choose the left gap if the right is empty. */
7575 *vr0type
= VR_RANGE
;
7576 if (TREE_CODE (*vr0min
) == INTEGER_CST
)
7577 *vr0max
= int_const_binop (MINUS_EXPR
, *vr0min
,
7583 /* Choose the anti-range if the range is effectively varying. */
7584 else if (vrp_val_is_min (vr1min
)
7585 && vrp_val_is_max (vr1max
))
7587 /* Else choose the range. */
7595 else if (*vr0type
== VR_ANTI_RANGE
7596 && vr1type
== VR_ANTI_RANGE
)
7598 /* If both are anti-ranges the result is the outer one. */
7603 else if (vr1type
== VR_ANTI_RANGE
7604 && *vr0type
== VR_RANGE
)
7606 /* The intersection is empty. */
7607 *vr0type
= VR_UNDEFINED
;
7608 *vr0min
= NULL_TREE
;
7609 *vr0max
= NULL_TREE
;
7614 else if ((operand_less_p (vr1min
, *vr0max
) == 1
7615 || operand_equal_p (vr1min
, *vr0max
, 0))
7616 && operand_less_p (*vr0min
, vr1min
) == 1)
7618 /* [ ( ] ) or [ ]( ) */
7619 if (*vr0type
== VR_ANTI_RANGE
7620 && vr1type
== VR_ANTI_RANGE
)
7622 else if (*vr0type
== VR_RANGE
7623 && vr1type
== VR_RANGE
)
7625 else if (*vr0type
== VR_RANGE
7626 && vr1type
== VR_ANTI_RANGE
)
7628 if (TREE_CODE (vr1min
) == INTEGER_CST
)
7629 *vr0max
= int_const_binop (MINUS_EXPR
, vr1min
,
7634 else if (*vr0type
== VR_ANTI_RANGE
7635 && vr1type
== VR_RANGE
)
7637 *vr0type
= VR_RANGE
;
7638 if (TREE_CODE (*vr0max
) == INTEGER_CST
)
7639 *vr0min
= int_const_binop (PLUS_EXPR
, *vr0max
,
7648 else if ((operand_less_p (*vr0min
, vr1max
) == 1
7649 || operand_equal_p (*vr0min
, vr1max
, 0))
7650 && operand_less_p (vr1min
, *vr0min
) == 1)
7652 /* ( [ ) ] or ( )[ ] */
7653 if (*vr0type
== VR_ANTI_RANGE
7654 && vr1type
== VR_ANTI_RANGE
)
7656 else if (*vr0type
== VR_RANGE
7657 && vr1type
== VR_RANGE
)
7659 else if (*vr0type
== VR_RANGE
7660 && vr1type
== VR_ANTI_RANGE
)
7662 if (TREE_CODE (vr1max
) == INTEGER_CST
)
7663 *vr0min
= int_const_binop (PLUS_EXPR
, vr1max
,
7668 else if (*vr0type
== VR_ANTI_RANGE
7669 && vr1type
== VR_RANGE
)
7671 *vr0type
= VR_RANGE
;
7672 if (TREE_CODE (*vr0min
) == INTEGER_CST
)
7673 *vr0max
= int_const_binop (MINUS_EXPR
, *vr0min
,
7683 /* As a fallback simply use { *VRTYPE, *VR0MIN, *VR0MAX } as
7684 result for the intersection. That's always a conservative
7685 correct estimate. */
7691 /* Intersect the two value-ranges *VR0 and *VR1 and store the result
7692 in *VR0. This may not be the smallest possible such range. */
7695 vrp_intersect_ranges_1 (value_range_t
*vr0
, value_range_t
*vr1
)
7697 value_range_t saved
;
7699 /* If either range is VR_VARYING the other one wins. */
7700 if (vr1
->type
== VR_VARYING
)
7702 if (vr0
->type
== VR_VARYING
)
7704 copy_value_range (vr0
, vr1
);
7708 /* When either range is VR_UNDEFINED the resulting range is
7709 VR_UNDEFINED, too. */
7710 if (vr0
->type
== VR_UNDEFINED
)
7712 if (vr1
->type
== VR_UNDEFINED
)
7714 set_value_range_to_undefined (vr0
);
7718 /* Save the original vr0 so we can return it as conservative intersection
7719 result when our worker turns things to varying. */
7721 intersect_ranges (&vr0
->type
, &vr0
->min
, &vr0
->max
,
7722 vr1
->type
, vr1
->min
, vr1
->max
);
7723 /* Make sure to canonicalize the result though as the inversion of a
7724 VR_RANGE can still be a VR_RANGE. */
7725 set_and_canonicalize_value_range (vr0
, vr0
->type
,
7726 vr0
->min
, vr0
->max
, vr0
->equiv
);
7727 /* If that failed, use the saved original VR0. */
7728 if (vr0
->type
== VR_VARYING
)
7733 /* If the result is VR_UNDEFINED there is no need to mess with
7734 the equivalencies. */
7735 if (vr0
->type
== VR_UNDEFINED
)
7738 /* The resulting set of equivalences for range intersection is the union of
7740 if (vr0
->equiv
&& vr1
->equiv
&& vr0
->equiv
!= vr1
->equiv
)
7741 bitmap_ior_into (vr0
->equiv
, vr1
->equiv
);
7742 else if (vr1
->equiv
&& !vr0
->equiv
)
7743 bitmap_copy (vr0
->equiv
, vr1
->equiv
);
7747 vrp_intersect_ranges (value_range_t
*vr0
, value_range_t
*vr1
)
7749 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7751 fprintf (dump_file
, "Intersecting\n ");
7752 dump_value_range (dump_file
, vr0
);
7753 fprintf (dump_file
, "\nand\n ");
7754 dump_value_range (dump_file
, vr1
);
7755 fprintf (dump_file
, "\n");
7757 vrp_intersect_ranges_1 (vr0
, vr1
);
7758 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7760 fprintf (dump_file
, "to\n ");
7761 dump_value_range (dump_file
, vr0
);
7762 fprintf (dump_file
, "\n");
7766 /* Meet operation for value ranges. Given two value ranges VR0 and
7767 VR1, store in VR0 a range that contains both VR0 and VR1. This
7768 may not be the smallest possible such range. */
7771 vrp_meet_1 (value_range_t
*vr0
, value_range_t
*vr1
)
7773 value_range_t saved
;
7775 if (vr0
->type
== VR_UNDEFINED
)
7777 /* Drop equivalences. See PR53465. */
7778 set_value_range (vr0
, vr1
->type
, vr1
->min
, vr1
->max
, NULL
);
7782 if (vr1
->type
== VR_UNDEFINED
)
7784 /* VR0 already has the resulting range, just drop equivalences.
7787 bitmap_clear (vr0
->equiv
);
7791 if (vr0
->type
== VR_VARYING
)
7793 /* Nothing to do. VR0 already has the resulting range. */
7797 if (vr1
->type
== VR_VARYING
)
7799 set_value_range_to_varying (vr0
);
7804 union_ranges (&vr0
->type
, &vr0
->min
, &vr0
->max
,
7805 vr1
->type
, vr1
->min
, vr1
->max
);
7806 if (vr0
->type
== VR_VARYING
)
7808 /* Failed to find an efficient meet. Before giving up and setting
7809 the result to VARYING, see if we can at least derive a useful
7810 anti-range. FIXME, all this nonsense about distinguishing
7811 anti-ranges from ranges is necessary because of the odd
7812 semantics of range_includes_zero_p and friends. */
7813 if (((saved
.type
== VR_RANGE
7814 && range_includes_zero_p (saved
.min
, saved
.max
) == 0)
7815 || (saved
.type
== VR_ANTI_RANGE
7816 && range_includes_zero_p (saved
.min
, saved
.max
) == 1))
7817 && ((vr1
->type
== VR_RANGE
7818 && range_includes_zero_p (vr1
->min
, vr1
->max
) == 0)
7819 || (vr1
->type
== VR_ANTI_RANGE
7820 && range_includes_zero_p (vr1
->min
, vr1
->max
) == 1)))
7822 set_value_range_to_nonnull (vr0
, TREE_TYPE (saved
.min
));
7824 /* Since this meet operation did not result from the meeting of
7825 two equivalent names, VR0 cannot have any equivalences. */
7827 bitmap_clear (vr0
->equiv
);
7831 set_value_range_to_varying (vr0
);
7834 set_and_canonicalize_value_range (vr0
, vr0
->type
, vr0
->min
, vr0
->max
,
7836 if (vr0
->type
== VR_VARYING
)
7839 /* The resulting set of equivalences is always the intersection of
7841 if (vr0
->equiv
&& vr1
->equiv
&& vr0
->equiv
!= vr1
->equiv
)
7842 bitmap_and_into (vr0
->equiv
, vr1
->equiv
);
7843 else if (vr0
->equiv
&& !vr1
->equiv
)
7844 bitmap_clear (vr0
->equiv
);
7848 vrp_meet (value_range_t
*vr0
, value_range_t
*vr1
)
7850 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7852 fprintf (dump_file
, "Meeting\n ");
7853 dump_value_range (dump_file
, vr0
);
7854 fprintf (dump_file
, "\nand\n ");
7855 dump_value_range (dump_file
, vr1
);
7856 fprintf (dump_file
, "\n");
7858 vrp_meet_1 (vr0
, vr1
);
7859 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7861 fprintf (dump_file
, "to\n ");
7862 dump_value_range (dump_file
, vr0
);
7863 fprintf (dump_file
, "\n");
7868 /* Visit all arguments for PHI node PHI that flow through executable
7869 edges. If a valid value range can be derived from all the incoming
7870 value ranges, set a new range for the LHS of PHI. */
7872 static enum ssa_prop_result
7873 vrp_visit_phi_node (gimple phi
)
7876 tree lhs
= PHI_RESULT (phi
);
7877 value_range_t
*lhs_vr
= get_value_range (lhs
);
7878 value_range_t vr_result
= VR_INITIALIZER
;
7880 int edges
, old_edges
;
7883 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7885 fprintf (dump_file
, "\nVisiting PHI node: ");
7886 print_gimple_stmt (dump_file
, phi
, 0, dump_flags
);
7890 for (i
= 0; i
< gimple_phi_num_args (phi
); i
++)
7892 edge e
= gimple_phi_arg_edge (phi
, i
);
7894 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7897 "\n Argument #%d (%d -> %d %sexecutable)\n",
7898 (int) i
, e
->src
->index
, e
->dest
->index
,
7899 (e
->flags
& EDGE_EXECUTABLE
) ? "" : "not ");
7902 if (e
->flags
& EDGE_EXECUTABLE
)
7904 tree arg
= PHI_ARG_DEF (phi
, i
);
7905 value_range_t vr_arg
;
7909 if (TREE_CODE (arg
) == SSA_NAME
)
7911 vr_arg
= *(get_value_range (arg
));
7915 if (is_overflow_infinity (arg
))
7917 arg
= copy_node (arg
);
7918 TREE_OVERFLOW (arg
) = 0;
7921 vr_arg
.type
= VR_RANGE
;
7924 vr_arg
.equiv
= NULL
;
7927 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7929 fprintf (dump_file
, "\t");
7930 print_generic_expr (dump_file
, arg
, dump_flags
);
7931 fprintf (dump_file
, "\n\tValue: ");
7932 dump_value_range (dump_file
, &vr_arg
);
7933 fprintf (dump_file
, "\n");
7937 copy_value_range (&vr_result
, &vr_arg
);
7939 vrp_meet (&vr_result
, &vr_arg
);
7942 if (vr_result
.type
== VR_VARYING
)
7947 if (vr_result
.type
== VR_VARYING
)
7949 else if (vr_result
.type
== VR_UNDEFINED
)
7952 old_edges
= vr_phi_edge_counts
[SSA_NAME_VERSION (lhs
)];
7953 vr_phi_edge_counts
[SSA_NAME_VERSION (lhs
)] = edges
;
7955 /* To prevent infinite iterations in the algorithm, derive ranges
7956 when the new value is slightly bigger or smaller than the
7957 previous one. We don't do this if we have seen a new executable
7958 edge; this helps us avoid an overflow infinity for conditionals
7959 which are not in a loop. If the old value-range was VR_UNDEFINED
7960 use the updated range and iterate one more time. */
7962 && gimple_phi_num_args (phi
) > 1
7963 && edges
== old_edges
7964 && lhs_vr
->type
!= VR_UNDEFINED
)
7966 int cmp_min
= compare_values (lhs_vr
->min
, vr_result
.min
);
7967 int cmp_max
= compare_values (lhs_vr
->max
, vr_result
.max
);
7969 /* For non VR_RANGE or for pointers fall back to varying if
7970 the range changed. */
7971 if ((lhs_vr
->type
!= VR_RANGE
|| vr_result
.type
!= VR_RANGE
7972 || POINTER_TYPE_P (TREE_TYPE (lhs
)))
7973 && (cmp_min
!= 0 || cmp_max
!= 0))
7976 /* If the new minimum is smaller or larger than the previous
7977 one, go all the way to -INF. In the first case, to avoid
7978 iterating millions of times to reach -INF, and in the
7979 other case to avoid infinite bouncing between different
7981 if (cmp_min
> 0 || cmp_min
< 0)
7983 if (!needs_overflow_infinity (TREE_TYPE (vr_result
.min
))
7984 || !vrp_var_may_overflow (lhs
, phi
))
7985 vr_result
.min
= TYPE_MIN_VALUE (TREE_TYPE (vr_result
.min
));
7986 else if (supports_overflow_infinity (TREE_TYPE (vr_result
.min
)))
7988 negative_overflow_infinity (TREE_TYPE (vr_result
.min
));
7991 /* Similarly, if the new maximum is smaller or larger than
7992 the previous one, go all the way to +INF. */
7993 if (cmp_max
< 0 || cmp_max
> 0)
7995 if (!needs_overflow_infinity (TREE_TYPE (vr_result
.max
))
7996 || !vrp_var_may_overflow (lhs
, phi
))
7997 vr_result
.max
= TYPE_MAX_VALUE (TREE_TYPE (vr_result
.max
));
7998 else if (supports_overflow_infinity (TREE_TYPE (vr_result
.max
)))
8000 positive_overflow_infinity (TREE_TYPE (vr_result
.max
));
8003 /* If we dropped either bound to +-INF then if this is a loop
8004 PHI node SCEV may known more about its value-range. */
8005 if ((cmp_min
> 0 || cmp_min
< 0
8006 || cmp_max
< 0 || cmp_max
> 0)
8008 && (l
= loop_containing_stmt (phi
))
8009 && l
->header
== gimple_bb (phi
))
8010 adjust_range_with_scev (&vr_result
, l
, phi
, lhs
);
8012 /* If we will end up with a (-INF, +INF) range, set it to
8013 VARYING. Same if the previous max value was invalid for
8014 the type and we end up with vr_result.min > vr_result.max. */
8015 if ((vrp_val_is_max (vr_result
.max
)
8016 && vrp_val_is_min (vr_result
.min
))
8017 || compare_values (vr_result
.min
,
8022 /* If the new range is different than the previous value, keep
8025 if (update_value_range (lhs
, &vr_result
))
8027 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8029 fprintf (dump_file
, "Found new range for ");
8030 print_generic_expr (dump_file
, lhs
, 0);
8031 fprintf (dump_file
, ": ");
8032 dump_value_range (dump_file
, &vr_result
);
8033 fprintf (dump_file
, "\n\n");
8036 return SSA_PROP_INTERESTING
;
8039 /* Nothing changed, don't add outgoing edges. */
8040 return SSA_PROP_NOT_INTERESTING
;
8042 /* No match found. Set the LHS to VARYING. */
8044 set_value_range_to_varying (lhs_vr
);
8045 return SSA_PROP_VARYING
;
8048 /* Simplify boolean operations if the source is known
8049 to be already a boolean. */
8051 simplify_truth_ops_using_ranges (gimple_stmt_iterator
*gsi
, gimple stmt
)
8053 enum tree_code rhs_code
= gimple_assign_rhs_code (stmt
);
8055 bool need_conversion
;
8057 /* We handle only !=/== case here. */
8058 gcc_assert (rhs_code
== EQ_EXPR
|| rhs_code
== NE_EXPR
);
8060 op0
= gimple_assign_rhs1 (stmt
);
8061 if (!op_with_boolean_value_range_p (op0
))
8064 op1
= gimple_assign_rhs2 (stmt
);
8065 if (!op_with_boolean_value_range_p (op1
))
8068 /* Reduce number of cases to handle to NE_EXPR. As there is no
8069 BIT_XNOR_EXPR we cannot replace A == B with a single statement. */
8070 if (rhs_code
== EQ_EXPR
)
8072 if (TREE_CODE (op1
) == INTEGER_CST
)
8073 op1
= int_const_binop (BIT_XOR_EXPR
, op1
, integer_one_node
);
8078 lhs
= gimple_assign_lhs (stmt
);
8080 = !useless_type_conversion_p (TREE_TYPE (lhs
), TREE_TYPE (op0
));
8082 /* Make sure to not sign-extend a 1-bit 1 when converting the result. */
8084 && !TYPE_UNSIGNED (TREE_TYPE (op0
))
8085 && TYPE_PRECISION (TREE_TYPE (op0
)) == 1
8086 && TYPE_PRECISION (TREE_TYPE (lhs
)) > 1)
8089 /* For A != 0 we can substitute A itself. */
8090 if (integer_zerop (op1
))
8091 gimple_assign_set_rhs_with_ops (gsi
,
8093 ? NOP_EXPR
: TREE_CODE (op0
),
8095 /* For A != B we substitute A ^ B. Either with conversion. */
8096 else if (need_conversion
)
8098 tree tem
= make_ssa_name (TREE_TYPE (op0
), NULL
);
8099 gimple newop
= gimple_build_assign_with_ops (BIT_XOR_EXPR
, tem
, op0
, op1
);
8100 gsi_insert_before (gsi
, newop
, GSI_SAME_STMT
);
8101 gimple_assign_set_rhs_with_ops (gsi
, NOP_EXPR
, tem
, NULL_TREE
);
8105 gimple_assign_set_rhs_with_ops (gsi
, BIT_XOR_EXPR
, op0
, op1
);
8106 update_stmt (gsi_stmt (*gsi
));
8111 /* Simplify a division or modulo operator to a right shift or
8112 bitwise and if the first operand is unsigned or is greater
8113 than zero and the second operand is an exact power of two. */
8116 simplify_div_or_mod_using_ranges (gimple stmt
)
8118 enum tree_code rhs_code
= gimple_assign_rhs_code (stmt
);
8120 tree op0
= gimple_assign_rhs1 (stmt
);
8121 tree op1
= gimple_assign_rhs2 (stmt
);
8122 value_range_t
*vr
= get_value_range (gimple_assign_rhs1 (stmt
));
8124 if (TYPE_UNSIGNED (TREE_TYPE (op0
)))
8126 val
= integer_one_node
;
8132 val
= compare_range_with_value (GE_EXPR
, vr
, integer_zero_node
, &sop
);
8136 && integer_onep (val
)
8137 && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC
))
8139 location_t location
;
8141 if (!gimple_has_location (stmt
))
8142 location
= input_location
;
8144 location
= gimple_location (stmt
);
8145 warning_at (location
, OPT_Wstrict_overflow
,
8146 "assuming signed overflow does not occur when "
8147 "simplifying %</%> or %<%%%> to %<>>%> or %<&%>");
8151 if (val
&& integer_onep (val
))
8155 if (rhs_code
== TRUNC_DIV_EXPR
)
8157 t
= build_int_cst (integer_type_node
, tree_log2 (op1
));
8158 gimple_assign_set_rhs_code (stmt
, RSHIFT_EXPR
);
8159 gimple_assign_set_rhs1 (stmt
, op0
);
8160 gimple_assign_set_rhs2 (stmt
, t
);
8164 t
= build_int_cst (TREE_TYPE (op1
), 1);
8165 t
= int_const_binop (MINUS_EXPR
, op1
, t
);
8166 t
= fold_convert (TREE_TYPE (op0
), t
);
8168 gimple_assign_set_rhs_code (stmt
, BIT_AND_EXPR
);
8169 gimple_assign_set_rhs1 (stmt
, op0
);
8170 gimple_assign_set_rhs2 (stmt
, t
);
8180 /* If the operand to an ABS_EXPR is >= 0, then eliminate the
8181 ABS_EXPR. If the operand is <= 0, then simplify the
8182 ABS_EXPR into a NEGATE_EXPR. */
8185 simplify_abs_using_ranges (gimple stmt
)
8188 tree op
= gimple_assign_rhs1 (stmt
);
8189 tree type
= TREE_TYPE (op
);
8190 value_range_t
*vr
= get_value_range (op
);
8192 if (TYPE_UNSIGNED (type
))
8194 val
= integer_zero_node
;
8200 val
= compare_range_with_value (LE_EXPR
, vr
, integer_zero_node
, &sop
);
8204 val
= compare_range_with_value (GE_EXPR
, vr
, integer_zero_node
,
8209 if (integer_zerop (val
))
8210 val
= integer_one_node
;
8211 else if (integer_onep (val
))
8212 val
= integer_zero_node
;
8217 && (integer_onep (val
) || integer_zerop (val
)))
8219 if (sop
&& issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC
))
8221 location_t location
;
8223 if (!gimple_has_location (stmt
))
8224 location
= input_location
;
8226 location
= gimple_location (stmt
);
8227 warning_at (location
, OPT_Wstrict_overflow
,
8228 "assuming signed overflow does not occur when "
8229 "simplifying %<abs (X)%> to %<X%> or %<-X%>");
8232 gimple_assign_set_rhs1 (stmt
, op
);
8233 if (integer_onep (val
))
8234 gimple_assign_set_rhs_code (stmt
, NEGATE_EXPR
);
8236 gimple_assign_set_rhs_code (stmt
, SSA_NAME
);
8245 /* Optimize away redundant BIT_AND_EXPR and BIT_IOR_EXPR.
8246 If all the bits that are being cleared by & are already
8247 known to be zero from VR, or all the bits that are being
8248 set by | are already known to be one from VR, the bit
8249 operation is redundant. */
8252 simplify_bit_ops_using_ranges (gimple_stmt_iterator
*gsi
, gimple stmt
)
8254 tree op0
= gimple_assign_rhs1 (stmt
);
8255 tree op1
= gimple_assign_rhs2 (stmt
);
8256 tree op
= NULL_TREE
;
8257 value_range_t vr0
= VR_INITIALIZER
;
8258 value_range_t vr1
= VR_INITIALIZER
;
8259 double_int may_be_nonzero0
, may_be_nonzero1
;
8260 double_int must_be_nonzero0
, must_be_nonzero1
;
8263 if (TREE_CODE (op0
) == SSA_NAME
)
8264 vr0
= *(get_value_range (op0
));
8265 else if (is_gimple_min_invariant (op0
))
8266 set_value_range_to_value (&vr0
, op0
, NULL
);
8270 if (TREE_CODE (op1
) == SSA_NAME
)
8271 vr1
= *(get_value_range (op1
));
8272 else if (is_gimple_min_invariant (op1
))
8273 set_value_range_to_value (&vr1
, op1
, NULL
);
8277 if (!zero_nonzero_bits_from_vr (&vr0
, &may_be_nonzero0
, &must_be_nonzero0
))
8279 if (!zero_nonzero_bits_from_vr (&vr1
, &may_be_nonzero1
, &must_be_nonzero1
))
8282 switch (gimple_assign_rhs_code (stmt
))
8285 mask
= may_be_nonzero0
.and_not (must_be_nonzero1
);
8286 if (mask
.is_zero ())
8291 mask
= may_be_nonzero1
.and_not (must_be_nonzero0
);
8292 if (mask
.is_zero ())
8299 mask
= may_be_nonzero0
.and_not (must_be_nonzero1
);
8300 if (mask
.is_zero ())
8305 mask
= may_be_nonzero1
.and_not (must_be_nonzero0
);
8306 if (mask
.is_zero ())
8316 if (op
== NULL_TREE
)
8319 gimple_assign_set_rhs_with_ops (gsi
, TREE_CODE (op
), op
, NULL
);
8320 update_stmt (gsi_stmt (*gsi
));
8324 /* We are comparing trees OP0 and OP1 using COND_CODE. OP0 has
8325 a known value range VR.
8327 If there is one and only one value which will satisfy the
8328 conditional, then return that value. Else return NULL. */
8331 test_for_singularity (enum tree_code cond_code
, tree op0
,
8332 tree op1
, value_range_t
*vr
)
8337 /* Extract minimum/maximum values which satisfy the
8338 the conditional as it was written. */
8339 if (cond_code
== LE_EXPR
|| cond_code
== LT_EXPR
)
8341 /* This should not be negative infinity; there is no overflow
8343 min
= TYPE_MIN_VALUE (TREE_TYPE (op0
));
8346 if (cond_code
== LT_EXPR
&& !is_overflow_infinity (max
))
8348 tree one
= build_int_cst (TREE_TYPE (op0
), 1);
8349 max
= fold_build2 (MINUS_EXPR
, TREE_TYPE (op0
), max
, one
);
8351 TREE_NO_WARNING (max
) = 1;
8354 else if (cond_code
== GE_EXPR
|| cond_code
== GT_EXPR
)
8356 /* This should not be positive infinity; there is no overflow
8358 max
= TYPE_MAX_VALUE (TREE_TYPE (op0
));
8361 if (cond_code
== GT_EXPR
&& !is_overflow_infinity (min
))
8363 tree one
= build_int_cst (TREE_TYPE (op0
), 1);
8364 min
= fold_build2 (PLUS_EXPR
, TREE_TYPE (op0
), min
, one
);
8366 TREE_NO_WARNING (min
) = 1;
8370 /* Now refine the minimum and maximum values using any
8371 value range information we have for op0. */
8374 if (compare_values (vr
->min
, min
) == 1)
8376 if (compare_values (vr
->max
, max
) == -1)
8379 /* If the new min/max values have converged to a single value,
8380 then there is only one value which can satisfy the condition,
8381 return that value. */
8382 if (operand_equal_p (min
, max
, 0) && is_gimple_min_invariant (min
))
8388 /* Simplify a conditional using a relational operator to an equality
8389 test if the range information indicates only one value can satisfy
8390 the original conditional. */
8393 simplify_cond_using_ranges (gimple stmt
)
8395 tree op0
= gimple_cond_lhs (stmt
);
8396 tree op1
= gimple_cond_rhs (stmt
);
8397 enum tree_code cond_code
= gimple_cond_code (stmt
);
8399 if (cond_code
!= NE_EXPR
8400 && cond_code
!= EQ_EXPR
8401 && TREE_CODE (op0
) == SSA_NAME
8402 && INTEGRAL_TYPE_P (TREE_TYPE (op0
))
8403 && is_gimple_min_invariant (op1
))
8405 value_range_t
*vr
= get_value_range (op0
);
8407 /* If we have range information for OP0, then we might be
8408 able to simplify this conditional. */
8409 if (vr
->type
== VR_RANGE
)
8411 tree new_tree
= test_for_singularity (cond_code
, op0
, op1
, vr
);
8417 fprintf (dump_file
, "Simplified relational ");
8418 print_gimple_stmt (dump_file
, stmt
, 0, 0);
8419 fprintf (dump_file
, " into ");
8422 gimple_cond_set_code (stmt
, EQ_EXPR
);
8423 gimple_cond_set_lhs (stmt
, op0
);
8424 gimple_cond_set_rhs (stmt
, new_tree
);
8430 print_gimple_stmt (dump_file
, stmt
, 0, 0);
8431 fprintf (dump_file
, "\n");
8437 /* Try again after inverting the condition. We only deal
8438 with integral types here, so no need to worry about
8439 issues with inverting FP comparisons. */
8440 cond_code
= invert_tree_comparison (cond_code
, false);
8441 new_tree
= test_for_singularity (cond_code
, op0
, op1
, vr
);
8447 fprintf (dump_file
, "Simplified relational ");
8448 print_gimple_stmt (dump_file
, stmt
, 0, 0);
8449 fprintf (dump_file
, " into ");
8452 gimple_cond_set_code (stmt
, NE_EXPR
);
8453 gimple_cond_set_lhs (stmt
, op0
);
8454 gimple_cond_set_rhs (stmt
, new_tree
);
8460 print_gimple_stmt (dump_file
, stmt
, 0, 0);
8461 fprintf (dump_file
, "\n");
8472 /* Simplify a switch statement using the value range of the switch
8476 simplify_switch_using_ranges (gimple stmt
)
8478 tree op
= gimple_switch_index (stmt
);
8483 size_t i
= 0, j
= 0, n
, n2
;
8486 size_t k
= 1, l
= 0;
8488 if (TREE_CODE (op
) == SSA_NAME
)
8490 vr
= get_value_range (op
);
8492 /* We can only handle integer ranges. */
8493 if ((vr
->type
!= VR_RANGE
8494 && vr
->type
!= VR_ANTI_RANGE
)
8495 || symbolic_range_p (vr
))
8498 /* Find case label for min/max of the value range. */
8499 take_default
= !find_case_label_ranges (stmt
, vr
, &i
, &j
, &k
, &l
);
8501 else if (TREE_CODE (op
) == INTEGER_CST
)
8503 take_default
= !find_case_label_index (stmt
, 1, op
, &i
);
8517 n
= gimple_switch_num_labels (stmt
);
8519 /* Bail out if this is just all edges taken. */
8525 /* Build a new vector of taken case labels. */
8526 vec2
= make_tree_vec (j
- i
+ 1 + l
- k
+ 1 + (int)take_default
);
8529 /* Add the default edge, if necessary. */
8531 TREE_VEC_ELT (vec2
, n2
++) = gimple_switch_default_label (stmt
);
8533 for (; i
<= j
; ++i
, ++n2
)
8534 TREE_VEC_ELT (vec2
, n2
) = gimple_switch_label (stmt
, i
);
8536 for (; k
<= l
; ++k
, ++n2
)
8537 TREE_VEC_ELT (vec2
, n2
) = gimple_switch_label (stmt
, k
);
8539 /* Mark needed edges. */
8540 for (i
= 0; i
< n2
; ++i
)
8542 e
= find_edge (gimple_bb (stmt
),
8543 label_to_block (CASE_LABEL (TREE_VEC_ELT (vec2
, i
))));
8544 e
->aux
= (void *)-1;
8547 /* Queue not needed edges for later removal. */
8548 FOR_EACH_EDGE (e
, ei
, gimple_bb (stmt
)->succs
)
8550 if (e
->aux
== (void *)-1)
8556 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8558 fprintf (dump_file
, "removing unreachable case label\n");
8560 VEC_safe_push (edge
, heap
, to_remove_edges
, e
);
8561 e
->flags
&= ~EDGE_EXECUTABLE
;
8564 /* And queue an update for the stmt. */
8567 VEC_safe_push (switch_update
, heap
, to_update_switch_stmts
, su
);
8571 /* Simplify an integral conversion from an SSA name in STMT. */
8574 simplify_conversion_using_ranges (gimple stmt
)
8576 tree innerop
, middleop
, finaltype
;
8578 value_range_t
*innervr
;
8579 bool inner_unsigned_p
, middle_unsigned_p
, final_unsigned_p
;
8580 unsigned inner_prec
, middle_prec
, final_prec
;
8581 double_int innermin
, innermed
, innermax
, middlemin
, middlemed
, middlemax
;
8583 finaltype
= TREE_TYPE (gimple_assign_lhs (stmt
));
8584 if (!INTEGRAL_TYPE_P (finaltype
))
8586 middleop
= gimple_assign_rhs1 (stmt
);
8587 def_stmt
= SSA_NAME_DEF_STMT (middleop
);
8588 if (!is_gimple_assign (def_stmt
)
8589 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt
)))
8591 innerop
= gimple_assign_rhs1 (def_stmt
);
8592 if (TREE_CODE (innerop
) != SSA_NAME
)
8595 /* Get the value-range of the inner operand. */
8596 innervr
= get_value_range (innerop
);
8597 if (innervr
->type
!= VR_RANGE
8598 || TREE_CODE (innervr
->min
) != INTEGER_CST
8599 || TREE_CODE (innervr
->max
) != INTEGER_CST
)
8602 /* Simulate the conversion chain to check if the result is equal if
8603 the middle conversion is removed. */
8604 innermin
= tree_to_double_int (innervr
->min
);
8605 innermax
= tree_to_double_int (innervr
->max
);
8607 inner_prec
= TYPE_PRECISION (TREE_TYPE (innerop
));
8608 middle_prec
= TYPE_PRECISION (TREE_TYPE (middleop
));
8609 final_prec
= TYPE_PRECISION (finaltype
);
8611 /* If the first conversion is not injective, the second must not
8613 if ((innermax
- innermin
).ugt (double_int::mask (middle_prec
))
8614 && middle_prec
< final_prec
)
8616 /* We also want a medium value so that we can track the effect that
8617 narrowing conversions with sign change have. */
8618 inner_unsigned_p
= TYPE_UNSIGNED (TREE_TYPE (innerop
));
8619 if (inner_unsigned_p
)
8620 innermed
= double_int::mask (inner_prec
).lrshift (1, inner_prec
);
8622 innermed
= double_int_zero
;
8623 if (innermin
.cmp (innermed
, inner_unsigned_p
) >= 0
8624 || innermed
.cmp (innermax
, inner_unsigned_p
) >= 0)
8625 innermed
= innermin
;
8627 middle_unsigned_p
= TYPE_UNSIGNED (TREE_TYPE (middleop
));
8628 middlemin
= innermin
.ext (middle_prec
, middle_unsigned_p
);
8629 middlemed
= innermed
.ext (middle_prec
, middle_unsigned_p
);
8630 middlemax
= innermax
.ext (middle_prec
, middle_unsigned_p
);
8632 /* Require that the final conversion applied to both the original
8633 and the intermediate range produces the same result. */
8634 final_unsigned_p
= TYPE_UNSIGNED (finaltype
);
8635 if (middlemin
.ext (final_prec
, final_unsigned_p
)
8636 != innermin
.ext (final_prec
, final_unsigned_p
)
8637 || middlemed
.ext (final_prec
, final_unsigned_p
)
8638 != innermed
.ext (final_prec
, final_unsigned_p
)
8639 || middlemax
.ext (final_prec
, final_unsigned_p
)
8640 != innermax
.ext (final_prec
, final_unsigned_p
))
8643 gimple_assign_set_rhs1 (stmt
, innerop
);
8648 /* Return whether the value range *VR fits in an integer type specified
8649 by PRECISION and UNSIGNED_P. */
8652 range_fits_type_p (value_range_t
*vr
, unsigned precision
, bool unsigned_p
)
8655 unsigned src_precision
;
8658 /* We can only handle integral and pointer types. */
8659 src_type
= TREE_TYPE (vr
->min
);
8660 if (!INTEGRAL_TYPE_P (src_type
)
8661 && !POINTER_TYPE_P (src_type
))
8664 /* An extension is always fine, so is an identity transform. */
8665 src_precision
= TYPE_PRECISION (TREE_TYPE (vr
->min
));
8666 if (src_precision
< precision
8667 || (src_precision
== precision
8668 && TYPE_UNSIGNED (src_type
) == unsigned_p
))
8671 /* Now we can only handle ranges with constant bounds. */
8672 if (vr
->type
!= VR_RANGE
8673 || TREE_CODE (vr
->min
) != INTEGER_CST
8674 || TREE_CODE (vr
->max
) != INTEGER_CST
)
8677 /* For precision-preserving sign-changes the MSB of the double-int
8679 if (src_precision
== precision
8680 && (TREE_INT_CST_HIGH (vr
->min
) | TREE_INT_CST_HIGH (vr
->max
)) < 0)
8683 /* Then we can perform the conversion on both ends and compare
8684 the result for equality. */
8685 tem
= tree_to_double_int (vr
->min
).ext (precision
, unsigned_p
);
8686 if (tree_to_double_int (vr
->min
) != tem
)
8688 tem
= tree_to_double_int (vr
->max
).ext (precision
, unsigned_p
);
8689 if (tree_to_double_int (vr
->max
) != tem
)
8695 /* Simplify a conversion from integral SSA name to float in STMT. */
8698 simplify_float_conversion_using_ranges (gimple_stmt_iterator
*gsi
, gimple stmt
)
8700 tree rhs1
= gimple_assign_rhs1 (stmt
);
8701 value_range_t
*vr
= get_value_range (rhs1
);
8702 enum machine_mode fltmode
= TYPE_MODE (TREE_TYPE (gimple_assign_lhs (stmt
)));
8703 enum machine_mode mode
;
8707 /* We can only handle constant ranges. */
8708 if (vr
->type
!= VR_RANGE
8709 || TREE_CODE (vr
->min
) != INTEGER_CST
8710 || TREE_CODE (vr
->max
) != INTEGER_CST
)
8713 /* First check if we can use a signed type in place of an unsigned. */
8714 if (TYPE_UNSIGNED (TREE_TYPE (rhs1
))
8715 && (can_float_p (fltmode
, TYPE_MODE (TREE_TYPE (rhs1
)), 0)
8716 != CODE_FOR_nothing
)
8717 && range_fits_type_p (vr
, GET_MODE_PRECISION
8718 (TYPE_MODE (TREE_TYPE (rhs1
))), 0))
8719 mode
= TYPE_MODE (TREE_TYPE (rhs1
));
8720 /* If we can do the conversion in the current input mode do nothing. */
8721 else if (can_float_p (fltmode
, TYPE_MODE (TREE_TYPE (rhs1
)),
8722 TYPE_UNSIGNED (TREE_TYPE (rhs1
))))
8724 /* Otherwise search for a mode we can use, starting from the narrowest
8725 integer mode available. */
8728 mode
= GET_CLASS_NARROWEST_MODE (MODE_INT
);
8731 /* If we cannot do a signed conversion to float from mode
8732 or if the value-range does not fit in the signed type
8733 try with a wider mode. */
8734 if (can_float_p (fltmode
, mode
, 0) != CODE_FOR_nothing
8735 && range_fits_type_p (vr
, GET_MODE_PRECISION (mode
), 0))
8738 mode
= GET_MODE_WIDER_MODE (mode
);
8739 /* But do not widen the input. Instead leave that to the
8740 optabs expansion code. */
8741 if (GET_MODE_PRECISION (mode
) > TYPE_PRECISION (TREE_TYPE (rhs1
)))
8744 while (mode
!= VOIDmode
);
8745 if (mode
== VOIDmode
)
8749 /* It works, insert a truncation or sign-change before the
8750 float conversion. */
8751 tem
= make_ssa_name (build_nonstandard_integer_type
8752 (GET_MODE_PRECISION (mode
), 0), NULL
);
8753 conv
= gimple_build_assign_with_ops (NOP_EXPR
, tem
, rhs1
, NULL_TREE
);
8754 gsi_insert_before (gsi
, conv
, GSI_SAME_STMT
);
8755 gimple_assign_set_rhs1 (stmt
, tem
);
8761 /* Simplify STMT using ranges if possible. */
8764 simplify_stmt_using_ranges (gimple_stmt_iterator
*gsi
)
8766 gimple stmt
= gsi_stmt (*gsi
);
8767 if (is_gimple_assign (stmt
))
8769 enum tree_code rhs_code
= gimple_assign_rhs_code (stmt
);
8770 tree rhs1
= gimple_assign_rhs1 (stmt
);
8776 /* Transform EQ_EXPR, NE_EXPR into BIT_XOR_EXPR or identity
8777 if the RHS is zero or one, and the LHS are known to be boolean
8779 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
8780 return simplify_truth_ops_using_ranges (gsi
, stmt
);
8783 /* Transform TRUNC_DIV_EXPR and TRUNC_MOD_EXPR into RSHIFT_EXPR
8784 and BIT_AND_EXPR respectively if the first operand is greater
8785 than zero and the second operand is an exact power of two. */
8786 case TRUNC_DIV_EXPR
:
8787 case TRUNC_MOD_EXPR
:
8788 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1
))
8789 && integer_pow2p (gimple_assign_rhs2 (stmt
)))
8790 return simplify_div_or_mod_using_ranges (stmt
);
8793 /* Transform ABS (X) into X or -X as appropriate. */
8795 if (TREE_CODE (rhs1
) == SSA_NAME
8796 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
8797 return simplify_abs_using_ranges (stmt
);
8802 /* Optimize away BIT_AND_EXPR and BIT_IOR_EXPR
8803 if all the bits being cleared are already cleared or
8804 all the bits being set are already set. */
8805 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
8806 return simplify_bit_ops_using_ranges (gsi
, stmt
);
8810 if (TREE_CODE (rhs1
) == SSA_NAME
8811 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
8812 return simplify_conversion_using_ranges (stmt
);
8816 if (TREE_CODE (rhs1
) == SSA_NAME
8817 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
8818 return simplify_float_conversion_using_ranges (gsi
, stmt
);
8825 else if (gimple_code (stmt
) == GIMPLE_COND
)
8826 return simplify_cond_using_ranges (stmt
);
8827 else if (gimple_code (stmt
) == GIMPLE_SWITCH
)
8828 return simplify_switch_using_ranges (stmt
);
8833 /* If the statement pointed by SI has a predicate whose value can be
8834 computed using the value range information computed by VRP, compute
8835 its value and return true. Otherwise, return false. */
8838 fold_predicate_in (gimple_stmt_iterator
*si
)
8840 bool assignment_p
= false;
8842 gimple stmt
= gsi_stmt (*si
);
8844 if (is_gimple_assign (stmt
)
8845 && TREE_CODE_CLASS (gimple_assign_rhs_code (stmt
)) == tcc_comparison
)
8847 assignment_p
= true;
8848 val
= vrp_evaluate_conditional (gimple_assign_rhs_code (stmt
),
8849 gimple_assign_rhs1 (stmt
),
8850 gimple_assign_rhs2 (stmt
),
8853 else if (gimple_code (stmt
) == GIMPLE_COND
)
8854 val
= vrp_evaluate_conditional (gimple_cond_code (stmt
),
8855 gimple_cond_lhs (stmt
),
8856 gimple_cond_rhs (stmt
),
8864 val
= fold_convert (gimple_expr_type (stmt
), val
);
8868 fprintf (dump_file
, "Folding predicate ");
8869 print_gimple_expr (dump_file
, stmt
, 0, 0);
8870 fprintf (dump_file
, " to ");
8871 print_generic_expr (dump_file
, val
, 0);
8872 fprintf (dump_file
, "\n");
8875 if (is_gimple_assign (stmt
))
8876 gimple_assign_set_rhs_from_tree (si
, val
);
8879 gcc_assert (gimple_code (stmt
) == GIMPLE_COND
);
8880 if (integer_zerop (val
))
8881 gimple_cond_make_false (stmt
);
8882 else if (integer_onep (val
))
8883 gimple_cond_make_true (stmt
);
8894 /* Callback for substitute_and_fold folding the stmt at *SI. */
8897 vrp_fold_stmt (gimple_stmt_iterator
*si
)
8899 if (fold_predicate_in (si
))
8902 return simplify_stmt_using_ranges (si
);
8905 /* Stack of dest,src equivalency pairs that need to be restored after
8906 each attempt to thread a block's incoming edge to an outgoing edge.
8908 A NULL entry is used to mark the end of pairs which need to be
8910 static VEC(tree
,heap
) *equiv_stack
;
8912 /* A trivial wrapper so that we can present the generic jump threading
8913 code with a simple API for simplifying statements. STMT is the
8914 statement we want to simplify, WITHIN_STMT provides the location
8915 for any overflow warnings. */
8918 simplify_stmt_for_jump_threading (gimple stmt
, gimple within_stmt
)
8920 /* We only use VRP information to simplify conditionals. This is
8921 overly conservative, but it's unclear if doing more would be
8922 worth the compile time cost. */
8923 if (gimple_code (stmt
) != GIMPLE_COND
)
8926 return vrp_evaluate_conditional (gimple_cond_code (stmt
),
8927 gimple_cond_lhs (stmt
),
8928 gimple_cond_rhs (stmt
), within_stmt
);
8931 /* Blocks which have more than one predecessor and more than
8932 one successor present jump threading opportunities, i.e.,
8933 when the block is reached from a specific predecessor, we
8934 may be able to determine which of the outgoing edges will
8935 be traversed. When this optimization applies, we are able
8936 to avoid conditionals at runtime and we may expose secondary
8937 optimization opportunities.
8939 This routine is effectively a driver for the generic jump
8940 threading code. It basically just presents the generic code
8941 with edges that may be suitable for jump threading.
8943 Unlike DOM, we do not iterate VRP if jump threading was successful.
8944 While iterating may expose new opportunities for VRP, it is expected
8945 those opportunities would be very limited and the compile time cost
8946 to expose those opportunities would be significant.
8948 As jump threading opportunities are discovered, they are registered
8949 for later realization. */
8952 identify_jump_threads (void)
8959 /* Ugh. When substituting values earlier in this pass we can
8960 wipe the dominance information. So rebuild the dominator
8961 information as we need it within the jump threading code. */
8962 calculate_dominance_info (CDI_DOMINATORS
);
8964 /* We do not allow VRP information to be used for jump threading
8965 across a back edge in the CFG. Otherwise it becomes too
8966 difficult to avoid eliminating loop exit tests. Of course
8967 EDGE_DFS_BACK is not accurate at this time so we have to
8969 mark_dfs_back_edges ();
8971 /* Do not thread across edges we are about to remove. Just marking
8972 them as EDGE_DFS_BACK will do. */
8973 FOR_EACH_VEC_ELT (edge
, to_remove_edges
, i
, e
)
8974 e
->flags
|= EDGE_DFS_BACK
;
8976 /* Allocate our unwinder stack to unwind any temporary equivalences
8977 that might be recorded. */
8978 equiv_stack
= VEC_alloc (tree
, heap
, 20);
8980 /* To avoid lots of silly node creation, we create a single
8981 conditional and just modify it in-place when attempting to
8983 dummy
= gimple_build_cond (EQ_EXPR
,
8984 integer_zero_node
, integer_zero_node
,
8987 /* Walk through all the blocks finding those which present a
8988 potential jump threading opportunity. We could set this up
8989 as a dominator walker and record data during the walk, but
8990 I doubt it's worth the effort for the classes of jump
8991 threading opportunities we are trying to identify at this
8992 point in compilation. */
8997 /* If the generic jump threading code does not find this block
8998 interesting, then there is nothing to do. */
8999 if (! potentially_threadable_block (bb
))
9002 /* We only care about blocks ending in a COND_EXPR. While there
9003 may be some value in handling SWITCH_EXPR here, I doubt it's
9004 terribly important. */
9005 last
= gsi_stmt (gsi_last_bb (bb
));
9007 /* We're basically looking for a switch or any kind of conditional with
9008 integral or pointer type arguments. Note the type of the second
9009 argument will be the same as the first argument, so no need to
9010 check it explicitly. */
9011 if (gimple_code (last
) == GIMPLE_SWITCH
9012 || (gimple_code (last
) == GIMPLE_COND
9013 && TREE_CODE (gimple_cond_lhs (last
)) == SSA_NAME
9014 && (INTEGRAL_TYPE_P (TREE_TYPE (gimple_cond_lhs (last
)))
9015 || POINTER_TYPE_P (TREE_TYPE (gimple_cond_lhs (last
))))
9016 && (TREE_CODE (gimple_cond_rhs (last
)) == SSA_NAME
9017 || is_gimple_min_invariant (gimple_cond_rhs (last
)))))
9021 /* We've got a block with multiple predecessors and multiple
9022 successors which also ends in a suitable conditional or
9023 switch statement. For each predecessor, see if we can thread
9024 it to a specific successor. */
9025 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
9027 /* Do not thread across back edges or abnormal edges
9029 if (e
->flags
& (EDGE_DFS_BACK
| EDGE_COMPLEX
))
9032 thread_across_edge (dummy
, e
, true, &equiv_stack
,
9033 simplify_stmt_for_jump_threading
);
9038 /* We do not actually update the CFG or SSA graphs at this point as
9039 ASSERT_EXPRs are still in the IL and cfg cleanup code does not yet
9040 handle ASSERT_EXPRs gracefully. */
9043 /* We identified all the jump threading opportunities earlier, but could
9044 not transform the CFG at that time. This routine transforms the
9045 CFG and arranges for the dominator tree to be rebuilt if necessary.
9047 Note the SSA graph update will occur during the normal TODO
9048 processing by the pass manager. */
9050 finalize_jump_threads (void)
9052 thread_through_all_blocks (false);
9053 VEC_free (tree
, heap
, equiv_stack
);
9057 /* Traverse all the blocks folding conditionals with known ranges. */
9064 values_propagated
= true;
9068 fprintf (dump_file
, "\nValue ranges after VRP:\n\n");
9069 dump_all_value_ranges (dump_file
);
9070 fprintf (dump_file
, "\n");
9073 substitute_and_fold (op_with_constant_singleton_value_range
,
9074 vrp_fold_stmt
, false);
9076 if (warn_array_bounds
)
9077 check_all_array_refs ();
9079 /* We must identify jump threading opportunities before we release
9080 the datastructures built by VRP. */
9081 identify_jump_threads ();
9083 /* Free allocated memory. */
9084 for (i
= 0; i
< num_vr_values
; i
++)
9087 BITMAP_FREE (vr_value
[i
]->equiv
);
9092 free (vr_phi_edge_counts
);
9094 /* So that we can distinguish between VRP data being available
9095 and not available. */
9097 vr_phi_edge_counts
= NULL
;
9101 /* Main entry point to VRP (Value Range Propagation). This pass is
9102 loosely based on J. R. C. Patterson, ``Accurate Static Branch
9103 Prediction by Value Range Propagation,'' in SIGPLAN Conference on
9104 Programming Language Design and Implementation, pp. 67-78, 1995.
9105 Also available at http://citeseer.ist.psu.edu/patterson95accurate.html
9107 This is essentially an SSA-CCP pass modified to deal with ranges
9108 instead of constants.
9110 While propagating ranges, we may find that two or more SSA name
9111 have equivalent, though distinct ranges. For instance,
9114 2 p_4 = ASSERT_EXPR <p_3, p_3 != 0>
9116 4 p_5 = ASSERT_EXPR <p_4, p_4 == q_2>;
9120 In the code above, pointer p_5 has range [q_2, q_2], but from the
9121 code we can also determine that p_5 cannot be NULL and, if q_2 had
9122 a non-varying range, p_5's range should also be compatible with it.
9124 These equivalences are created by two expressions: ASSERT_EXPR and
9125 copy operations. Since p_5 is an assertion on p_4, and p_4 was the
9126 result of another assertion, then we can use the fact that p_5 and
9127 p_4 are equivalent when evaluating p_5's range.
9129 Together with value ranges, we also propagate these equivalences
9130 between names so that we can take advantage of information from
9131 multiple ranges when doing final replacement. Note that this
9132 equivalency relation is transitive but not symmetric.
9134 In the example above, p_5 is equivalent to p_4, q_2 and p_3, but we
9135 cannot assert that q_2 is equivalent to p_5 because q_2 may be used
9136 in contexts where that assertion does not hold (e.g., in line 6).
9138 TODO, the main difference between this pass and Patterson's is that
9139 we do not propagate edge probabilities. We only compute whether
9140 edges can be taken or not. That is, instead of having a spectrum
9141 of jump probabilities between 0 and 1, we only deal with 0, 1 and
9142 DON'T KNOW. In the future, it may be worthwhile to propagate
9143 probabilities to aid branch prediction. */
9152 loop_optimizer_init (LOOPS_NORMAL
| LOOPS_HAVE_RECORDED_EXITS
);
9153 rewrite_into_loop_closed_ssa (NULL
, TODO_update_ssa
);
9156 insert_range_assertions ();
9158 to_remove_edges
= VEC_alloc (edge
, heap
, 10);
9159 to_update_switch_stmts
= VEC_alloc (switch_update
, heap
, 5);
9160 threadedge_initialize_values ();
9163 ssa_propagate (vrp_visit_stmt
, vrp_visit_phi_node
);
9166 free_numbers_of_iterations_estimates ();
9168 /* ASSERT_EXPRs must be removed before finalizing jump threads
9169 as finalizing jump threads calls the CFG cleanup code which
9170 does not properly handle ASSERT_EXPRs. */
9171 remove_range_assertions ();
9173 /* If we exposed any new variables, go ahead and put them into
9174 SSA form now, before we handle jump threading. This simplifies
9175 interactions between rewriting of _DECL nodes into SSA form
9176 and rewriting SSA_NAME nodes into SSA form after block
9177 duplication and CFG manipulation. */
9178 update_ssa (TODO_update_ssa
);
9180 finalize_jump_threads ();
9182 /* Remove dead edges from SWITCH_EXPR optimization. This leaves the
9183 CFG in a broken state and requires a cfg_cleanup run. */
9184 FOR_EACH_VEC_ELT (edge
, to_remove_edges
, i
, e
)
9186 /* Update SWITCH_EXPR case label vector. */
9187 FOR_EACH_VEC_ELT (switch_update
, to_update_switch_stmts
, i
, su
)
9190 size_t n
= TREE_VEC_LENGTH (su
->vec
);
9192 gimple_switch_set_num_labels (su
->stmt
, n
);
9193 for (j
= 0; j
< n
; j
++)
9194 gimple_switch_set_label (su
->stmt
, j
, TREE_VEC_ELT (su
->vec
, j
));
9195 /* As we may have replaced the default label with a regular one
9196 make sure to make it a real default label again. This ensures
9197 optimal expansion. */
9198 label
= gimple_switch_label (su
->stmt
, 0);
9199 CASE_LOW (label
) = NULL_TREE
;
9200 CASE_HIGH (label
) = NULL_TREE
;
9203 if (VEC_length (edge
, to_remove_edges
) > 0)
9204 free_dominance_info (CDI_DOMINATORS
);
9206 VEC_free (edge
, heap
, to_remove_edges
);
9207 VEC_free (switch_update
, heap
, to_update_switch_stmts
);
9208 threadedge_finalize_values ();
9211 loop_optimizer_finalize ();
9218 return flag_tree_vrp
!= 0;
9221 struct gimple_opt_pass pass_vrp
=
9226 gate_vrp
, /* gate */
9227 execute_vrp
, /* execute */
9230 0, /* static_pass_number */
9231 TV_TREE_VRP
, /* tv_id */
9232 PROP_ssa
, /* properties_required */
9233 0, /* properties_provided */
9234 0, /* properties_destroyed */
9235 0, /* todo_flags_start */
9240 | TODO_ggc_collect
/* todo_flags_finish */