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
))
504 tree one
= build_int_cst (TREE_TYPE (min
), 1);
505 tree tmp
= int_const_binop (PLUS_EXPR
, max
, one
);
506 max
= int_const_binop (MINUS_EXPR
, min
, one
);
509 /* There's one corner case, if we had [C+1, C] before we now have
510 that again. But this represents an empty value range, so drop
511 to varying in this case. */
512 if (tree_int_cst_lt (max
, min
))
514 set_value_range_to_varying (vr
);
518 t
= t
== VR_RANGE
? VR_ANTI_RANGE
: VR_RANGE
;
521 /* Anti-ranges that can be represented as ranges should be so. */
522 if (t
== VR_ANTI_RANGE
)
524 bool is_min
= vrp_val_is_min (min
);
525 bool is_max
= vrp_val_is_max (max
);
527 if (is_min
&& is_max
)
529 /* We cannot deal with empty ranges, drop to varying.
530 ??? This could be VR_UNDEFINED instead. */
531 set_value_range_to_varying (vr
);
535 /* As a special exception preserve non-null ranges. */
536 && !(TYPE_UNSIGNED (TREE_TYPE (min
))
537 && integer_zerop (max
)))
539 tree one
= build_int_cst (TREE_TYPE (max
), 1);
540 min
= int_const_binop (PLUS_EXPR
, max
, one
);
541 max
= vrp_val_max (TREE_TYPE (max
));
546 tree one
= build_int_cst (TREE_TYPE (min
), 1);
547 max
= int_const_binop (MINUS_EXPR
, min
, one
);
548 min
= vrp_val_min (TREE_TYPE (min
));
553 /* Drop [-INF(OVF), +INF(OVF)] to varying. */
554 if (needs_overflow_infinity (TREE_TYPE (min
))
555 && is_overflow_infinity (min
)
556 && is_overflow_infinity (max
))
558 set_value_range_to_varying (vr
);
562 set_value_range (vr
, t
, min
, max
, equiv
);
565 /* Copy value range FROM into value range TO. */
568 copy_value_range (value_range_t
*to
, value_range_t
*from
)
570 set_value_range (to
, from
->type
, from
->min
, from
->max
, from
->equiv
);
573 /* Set value range VR to a single value. This function is only called
574 with values we get from statements, and exists to clear the
575 TREE_OVERFLOW flag so that we don't think we have an overflow
576 infinity when we shouldn't. */
579 set_value_range_to_value (value_range_t
*vr
, tree val
, bitmap equiv
)
581 gcc_assert (is_gimple_min_invariant (val
));
582 val
= avoid_overflow_infinity (val
);
583 set_value_range (vr
, VR_RANGE
, val
, val
, equiv
);
586 /* Set value range VR to a non-negative range of type TYPE.
587 OVERFLOW_INFINITY indicates whether to use an overflow infinity
588 rather than TYPE_MAX_VALUE; this should be true if we determine
589 that the range is nonnegative based on the assumption that signed
590 overflow does not occur. */
593 set_value_range_to_nonnegative (value_range_t
*vr
, tree type
,
594 bool overflow_infinity
)
598 if (overflow_infinity
&& !supports_overflow_infinity (type
))
600 set_value_range_to_varying (vr
);
604 zero
= build_int_cst (type
, 0);
605 set_value_range (vr
, VR_RANGE
, zero
,
607 ? positive_overflow_infinity (type
)
608 : TYPE_MAX_VALUE (type
)),
612 /* Set value range VR to a non-NULL range of type TYPE. */
615 set_value_range_to_nonnull (value_range_t
*vr
, tree type
)
617 tree zero
= build_int_cst (type
, 0);
618 set_value_range (vr
, VR_ANTI_RANGE
, zero
, zero
, vr
->equiv
);
622 /* Set value range VR to a NULL range of type TYPE. */
625 set_value_range_to_null (value_range_t
*vr
, tree type
)
627 set_value_range_to_value (vr
, build_int_cst (type
, 0), vr
->equiv
);
631 /* Set value range VR to a range of a truthvalue of type TYPE. */
634 set_value_range_to_truthvalue (value_range_t
*vr
, tree type
)
636 if (TYPE_PRECISION (type
) == 1)
637 set_value_range_to_varying (vr
);
639 set_value_range (vr
, VR_RANGE
,
640 build_int_cst (type
, 0), build_int_cst (type
, 1),
645 /* If abs (min) < abs (max), set VR to [-max, max], if
646 abs (min) >= abs (max), set VR to [-min, min]. */
649 abs_extent_range (value_range_t
*vr
, tree min
, tree max
)
653 gcc_assert (TREE_CODE (min
) == INTEGER_CST
);
654 gcc_assert (TREE_CODE (max
) == INTEGER_CST
);
655 gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (min
)));
656 gcc_assert (!TYPE_UNSIGNED (TREE_TYPE (min
)));
657 min
= fold_unary (ABS_EXPR
, TREE_TYPE (min
), min
);
658 max
= fold_unary (ABS_EXPR
, TREE_TYPE (max
), max
);
659 if (TREE_OVERFLOW (min
) || TREE_OVERFLOW (max
))
661 set_value_range_to_varying (vr
);
664 cmp
= compare_values (min
, max
);
666 min
= fold_unary (NEGATE_EXPR
, TREE_TYPE (min
), max
);
667 else if (cmp
== 0 || cmp
== 1)
670 min
= fold_unary (NEGATE_EXPR
, TREE_TYPE (min
), min
);
674 set_value_range_to_varying (vr
);
677 set_and_canonicalize_value_range (vr
, VR_RANGE
, min
, max
, NULL
);
681 /* Return value range information for VAR.
683 If we have no values ranges recorded (ie, VRP is not running), then
684 return NULL. Otherwise create an empty range if none existed for VAR. */
686 static value_range_t
*
687 get_value_range (const_tree var
)
689 static const struct value_range_d vr_const_varying
690 = { VR_VARYING
, NULL_TREE
, NULL_TREE
, NULL
};
693 unsigned ver
= SSA_NAME_VERSION (var
);
695 /* If we have no recorded ranges, then return NULL. */
699 /* If we query the range for a new SSA name return an unmodifiable VARYING.
700 We should get here at most from the substitute-and-fold stage which
701 will never try to change values. */
702 if (ver
>= num_vr_values
)
703 return CONST_CAST (value_range_t
*, &vr_const_varying
);
709 /* After propagation finished do not allocate new value-ranges. */
710 if (values_propagated
)
711 return CONST_CAST (value_range_t
*, &vr_const_varying
);
713 /* Create a default value range. */
714 vr_value
[ver
] = vr
= XCNEW (value_range_t
);
716 /* Defer allocating the equivalence set. */
719 /* If VAR is a default definition of a parameter, the variable can
720 take any value in VAR's type. */
721 if (SSA_NAME_IS_DEFAULT_DEF (var
))
723 sym
= SSA_NAME_VAR (var
);
724 if (TREE_CODE (sym
) == PARM_DECL
)
726 /* Try to use the "nonnull" attribute to create ~[0, 0]
727 anti-ranges for pointers. Note that this is only valid with
728 default definitions of PARM_DECLs. */
729 if (POINTER_TYPE_P (TREE_TYPE (sym
))
730 && nonnull_arg_p (sym
))
731 set_value_range_to_nonnull (vr
, TREE_TYPE (sym
));
733 set_value_range_to_varying (vr
);
735 else if (TREE_CODE (sym
) == RESULT_DECL
736 && DECL_BY_REFERENCE (sym
))
737 set_value_range_to_nonnull (vr
, TREE_TYPE (sym
));
743 /* Return true, if VAL1 and VAL2 are equal values for VRP purposes. */
746 vrp_operand_equal_p (const_tree val1
, const_tree val2
)
750 if (!val1
|| !val2
|| !operand_equal_p (val1
, val2
, 0))
752 if (is_overflow_infinity (val1
))
753 return is_overflow_infinity (val2
);
757 /* Return true, if the bitmaps B1 and B2 are equal. */
760 vrp_bitmap_equal_p (const_bitmap b1
, const_bitmap b2
)
763 || ((!b1
|| bitmap_empty_p (b1
))
764 && (!b2
|| bitmap_empty_p (b2
)))
766 && bitmap_equal_p (b1
, b2
)));
769 /* Update the value range and equivalence set for variable VAR to
770 NEW_VR. Return true if NEW_VR is different from VAR's previous
773 NOTE: This function assumes that NEW_VR is a temporary value range
774 object created for the sole purpose of updating VAR's range. The
775 storage used by the equivalence set from NEW_VR will be freed by
776 this function. Do not call update_value_range when NEW_VR
777 is the range object associated with another SSA name. */
780 update_value_range (const_tree var
, value_range_t
*new_vr
)
782 value_range_t
*old_vr
;
785 /* Update the value range, if necessary. */
786 old_vr
= get_value_range (var
);
787 is_new
= old_vr
->type
!= new_vr
->type
788 || !vrp_operand_equal_p (old_vr
->min
, new_vr
->min
)
789 || !vrp_operand_equal_p (old_vr
->max
, new_vr
->max
)
790 || !vrp_bitmap_equal_p (old_vr
->equiv
, new_vr
->equiv
);
793 set_value_range (old_vr
, new_vr
->type
, new_vr
->min
, new_vr
->max
,
796 BITMAP_FREE (new_vr
->equiv
);
802 /* Add VAR and VAR's equivalence set to EQUIV. This is the central
803 point where equivalence processing can be turned on/off. */
806 add_equivalence (bitmap
*equiv
, const_tree var
)
808 unsigned ver
= SSA_NAME_VERSION (var
);
809 value_range_t
*vr
= vr_value
[ver
];
812 *equiv
= BITMAP_ALLOC (NULL
);
813 bitmap_set_bit (*equiv
, ver
);
815 bitmap_ior_into (*equiv
, vr
->equiv
);
819 /* Return true if VR is ~[0, 0]. */
822 range_is_nonnull (value_range_t
*vr
)
824 return vr
->type
== VR_ANTI_RANGE
825 && integer_zerop (vr
->min
)
826 && integer_zerop (vr
->max
);
830 /* Return true if VR is [0, 0]. */
833 range_is_null (value_range_t
*vr
)
835 return vr
->type
== VR_RANGE
836 && integer_zerop (vr
->min
)
837 && integer_zerop (vr
->max
);
840 /* Return true if max and min of VR are INTEGER_CST. It's not necessary
844 range_int_cst_p (value_range_t
*vr
)
846 return (vr
->type
== VR_RANGE
847 && TREE_CODE (vr
->max
) == INTEGER_CST
848 && TREE_CODE (vr
->min
) == INTEGER_CST
);
851 /* Return true if VR is a INTEGER_CST singleton. */
854 range_int_cst_singleton_p (value_range_t
*vr
)
856 return (range_int_cst_p (vr
)
857 && !TREE_OVERFLOW (vr
->min
)
858 && !TREE_OVERFLOW (vr
->max
)
859 && tree_int_cst_equal (vr
->min
, vr
->max
));
862 /* Return true if value range VR involves at least one symbol. */
865 symbolic_range_p (value_range_t
*vr
)
867 return (!is_gimple_min_invariant (vr
->min
)
868 || !is_gimple_min_invariant (vr
->max
));
871 /* Return true if value range VR uses an overflow infinity. */
874 overflow_infinity_range_p (value_range_t
*vr
)
876 return (vr
->type
== VR_RANGE
877 && (is_overflow_infinity (vr
->min
)
878 || is_overflow_infinity (vr
->max
)));
881 /* Return false if we can not make a valid comparison based on VR;
882 this will be the case if it uses an overflow infinity and overflow
883 is not undefined (i.e., -fno-strict-overflow is in effect).
884 Otherwise return true, and set *STRICT_OVERFLOW_P to true if VR
885 uses an overflow infinity. */
888 usable_range_p (value_range_t
*vr
, bool *strict_overflow_p
)
890 gcc_assert (vr
->type
== VR_RANGE
);
891 if (is_overflow_infinity (vr
->min
))
893 *strict_overflow_p
= true;
894 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (vr
->min
)))
897 if (is_overflow_infinity (vr
->max
))
899 *strict_overflow_p
= true;
900 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (vr
->max
)))
907 /* Return true if the result of assignment STMT is know to be non-negative.
908 If the return value is based on the assumption that signed overflow is
909 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
910 *STRICT_OVERFLOW_P.*/
913 gimple_assign_nonnegative_warnv_p (gimple stmt
, bool *strict_overflow_p
)
915 enum tree_code code
= gimple_assign_rhs_code (stmt
);
916 switch (get_gimple_rhs_class (code
))
918 case GIMPLE_UNARY_RHS
:
919 return tree_unary_nonnegative_warnv_p (gimple_assign_rhs_code (stmt
),
920 gimple_expr_type (stmt
),
921 gimple_assign_rhs1 (stmt
),
923 case GIMPLE_BINARY_RHS
:
924 return tree_binary_nonnegative_warnv_p (gimple_assign_rhs_code (stmt
),
925 gimple_expr_type (stmt
),
926 gimple_assign_rhs1 (stmt
),
927 gimple_assign_rhs2 (stmt
),
929 case GIMPLE_TERNARY_RHS
:
931 case GIMPLE_SINGLE_RHS
:
932 return tree_single_nonnegative_warnv_p (gimple_assign_rhs1 (stmt
),
934 case GIMPLE_INVALID_RHS
:
941 /* Return true if return value of call STMT is know to be non-negative.
942 If the return value is based on the assumption that signed overflow is
943 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
944 *STRICT_OVERFLOW_P.*/
947 gimple_call_nonnegative_warnv_p (gimple stmt
, bool *strict_overflow_p
)
949 tree arg0
= gimple_call_num_args (stmt
) > 0 ?
950 gimple_call_arg (stmt
, 0) : NULL_TREE
;
951 tree arg1
= gimple_call_num_args (stmt
) > 1 ?
952 gimple_call_arg (stmt
, 1) : NULL_TREE
;
954 return tree_call_nonnegative_warnv_p (gimple_expr_type (stmt
),
955 gimple_call_fndecl (stmt
),
961 /* Return true if STMT is know to to compute a non-negative value.
962 If the return value is based on the assumption that signed overflow is
963 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
964 *STRICT_OVERFLOW_P.*/
967 gimple_stmt_nonnegative_warnv_p (gimple stmt
, bool *strict_overflow_p
)
969 switch (gimple_code (stmt
))
972 return gimple_assign_nonnegative_warnv_p (stmt
, strict_overflow_p
);
974 return gimple_call_nonnegative_warnv_p (stmt
, strict_overflow_p
);
980 /* Return true if the result of assignment STMT is know to be non-zero.
981 If the return value is based on the assumption that signed overflow is
982 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
983 *STRICT_OVERFLOW_P.*/
986 gimple_assign_nonzero_warnv_p (gimple stmt
, bool *strict_overflow_p
)
988 enum tree_code code
= gimple_assign_rhs_code (stmt
);
989 switch (get_gimple_rhs_class (code
))
991 case GIMPLE_UNARY_RHS
:
992 return tree_unary_nonzero_warnv_p (gimple_assign_rhs_code (stmt
),
993 gimple_expr_type (stmt
),
994 gimple_assign_rhs1 (stmt
),
996 case GIMPLE_BINARY_RHS
:
997 return tree_binary_nonzero_warnv_p (gimple_assign_rhs_code (stmt
),
998 gimple_expr_type (stmt
),
999 gimple_assign_rhs1 (stmt
),
1000 gimple_assign_rhs2 (stmt
),
1002 case GIMPLE_TERNARY_RHS
:
1004 case GIMPLE_SINGLE_RHS
:
1005 return tree_single_nonzero_warnv_p (gimple_assign_rhs1 (stmt
),
1007 case GIMPLE_INVALID_RHS
:
1014 /* Return true if STMT is know to to compute a non-zero value.
1015 If the return value is based on the assumption that signed overflow is
1016 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1017 *STRICT_OVERFLOW_P.*/
1020 gimple_stmt_nonzero_warnv_p (gimple stmt
, bool *strict_overflow_p
)
1022 switch (gimple_code (stmt
))
1025 return gimple_assign_nonzero_warnv_p (stmt
, strict_overflow_p
);
1027 return gimple_alloca_call_p (stmt
);
1033 /* Like tree_expr_nonzero_warnv_p, but this function uses value ranges
1037 vrp_stmt_computes_nonzero (gimple stmt
, bool *strict_overflow_p
)
1039 if (gimple_stmt_nonzero_warnv_p (stmt
, strict_overflow_p
))
1042 /* If we have an expression of the form &X->a, then the expression
1043 is nonnull if X is nonnull. */
1044 if (is_gimple_assign (stmt
)
1045 && gimple_assign_rhs_code (stmt
) == ADDR_EXPR
)
1047 tree expr
= gimple_assign_rhs1 (stmt
);
1048 tree base
= get_base_address (TREE_OPERAND (expr
, 0));
1050 if (base
!= NULL_TREE
1051 && TREE_CODE (base
) == MEM_REF
1052 && TREE_CODE (TREE_OPERAND (base
, 0)) == SSA_NAME
)
1054 value_range_t
*vr
= get_value_range (TREE_OPERAND (base
, 0));
1055 if (range_is_nonnull (vr
))
1063 /* Returns true if EXPR is a valid value (as expected by compare_values) --
1064 a gimple invariant, or SSA_NAME +- CST. */
1067 valid_value_p (tree expr
)
1069 if (TREE_CODE (expr
) == SSA_NAME
)
1072 if (TREE_CODE (expr
) == PLUS_EXPR
1073 || TREE_CODE (expr
) == MINUS_EXPR
)
1074 return (TREE_CODE (TREE_OPERAND (expr
, 0)) == SSA_NAME
1075 && TREE_CODE (TREE_OPERAND (expr
, 1)) == INTEGER_CST
);
1077 return is_gimple_min_invariant (expr
);
1083 -2 if those are incomparable. */
1085 operand_less_p (tree val
, tree val2
)
1087 /* LT is folded faster than GE and others. Inline the common case. */
1088 if (TREE_CODE (val
) == INTEGER_CST
&& TREE_CODE (val2
) == INTEGER_CST
)
1090 if (TYPE_UNSIGNED (TREE_TYPE (val
)))
1091 return INT_CST_LT_UNSIGNED (val
, val2
);
1094 if (INT_CST_LT (val
, val2
))
1102 fold_defer_overflow_warnings ();
1104 tcmp
= fold_binary_to_constant (LT_EXPR
, boolean_type_node
, val
, val2
);
1106 fold_undefer_and_ignore_overflow_warnings ();
1109 || TREE_CODE (tcmp
) != INTEGER_CST
)
1112 if (!integer_zerop (tcmp
))
1116 /* val >= val2, not considering overflow infinity. */
1117 if (is_negative_overflow_infinity (val
))
1118 return is_negative_overflow_infinity (val2
) ? 0 : 1;
1119 else if (is_positive_overflow_infinity (val2
))
1120 return is_positive_overflow_infinity (val
) ? 0 : 1;
1125 /* Compare two values VAL1 and VAL2. Return
1127 -2 if VAL1 and VAL2 cannot be compared at compile-time,
1130 +1 if VAL1 > VAL2, and
1133 This is similar to tree_int_cst_compare but supports pointer values
1134 and values that cannot be compared at compile time.
1136 If STRICT_OVERFLOW_P is not NULL, then set *STRICT_OVERFLOW_P to
1137 true if the return value is only valid if we assume that signed
1138 overflow is undefined. */
1141 compare_values_warnv (tree val1
, tree val2
, bool *strict_overflow_p
)
1146 /* Below we rely on the fact that VAL1 and VAL2 are both pointers or
1148 gcc_assert (POINTER_TYPE_P (TREE_TYPE (val1
))
1149 == POINTER_TYPE_P (TREE_TYPE (val2
)));
1150 /* Convert the two values into the same type. This is needed because
1151 sizetype causes sign extension even for unsigned types. */
1152 val2
= fold_convert (TREE_TYPE (val1
), val2
);
1153 STRIP_USELESS_TYPE_CONVERSION (val2
);
1155 if ((TREE_CODE (val1
) == SSA_NAME
1156 || TREE_CODE (val1
) == PLUS_EXPR
1157 || TREE_CODE (val1
) == MINUS_EXPR
)
1158 && (TREE_CODE (val2
) == SSA_NAME
1159 || TREE_CODE (val2
) == PLUS_EXPR
1160 || TREE_CODE (val2
) == MINUS_EXPR
))
1162 tree n1
, c1
, n2
, c2
;
1163 enum tree_code code1
, code2
;
1165 /* If VAL1 and VAL2 are of the form 'NAME [+-] CST' or 'NAME',
1166 return -1 or +1 accordingly. If VAL1 and VAL2 don't use the
1167 same name, return -2. */
1168 if (TREE_CODE (val1
) == SSA_NAME
)
1176 code1
= TREE_CODE (val1
);
1177 n1
= TREE_OPERAND (val1
, 0);
1178 c1
= TREE_OPERAND (val1
, 1);
1179 if (tree_int_cst_sgn (c1
) == -1)
1181 if (is_negative_overflow_infinity (c1
))
1183 c1
= fold_unary_to_constant (NEGATE_EXPR
, TREE_TYPE (c1
), c1
);
1186 code1
= code1
== MINUS_EXPR
? PLUS_EXPR
: MINUS_EXPR
;
1190 if (TREE_CODE (val2
) == SSA_NAME
)
1198 code2
= TREE_CODE (val2
);
1199 n2
= TREE_OPERAND (val2
, 0);
1200 c2
= TREE_OPERAND (val2
, 1);
1201 if (tree_int_cst_sgn (c2
) == -1)
1203 if (is_negative_overflow_infinity (c2
))
1205 c2
= fold_unary_to_constant (NEGATE_EXPR
, TREE_TYPE (c2
), c2
);
1208 code2
= code2
== MINUS_EXPR
? PLUS_EXPR
: MINUS_EXPR
;
1212 /* Both values must use the same name. */
1216 if (code1
== SSA_NAME
1217 && code2
== SSA_NAME
)
1221 /* If overflow is defined we cannot simplify more. */
1222 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (val1
)))
1225 if (strict_overflow_p
!= NULL
1226 && (code1
== SSA_NAME
|| !TREE_NO_WARNING (val1
))
1227 && (code2
== SSA_NAME
|| !TREE_NO_WARNING (val2
)))
1228 *strict_overflow_p
= true;
1230 if (code1
== SSA_NAME
)
1232 if (code2
== PLUS_EXPR
)
1233 /* NAME < NAME + CST */
1235 else if (code2
== MINUS_EXPR
)
1236 /* NAME > NAME - CST */
1239 else if (code1
== PLUS_EXPR
)
1241 if (code2
== SSA_NAME
)
1242 /* NAME + CST > NAME */
1244 else if (code2
== PLUS_EXPR
)
1245 /* NAME + CST1 > NAME + CST2, if CST1 > CST2 */
1246 return compare_values_warnv (c1
, c2
, strict_overflow_p
);
1247 else if (code2
== MINUS_EXPR
)
1248 /* NAME + CST1 > NAME - CST2 */
1251 else if (code1
== MINUS_EXPR
)
1253 if (code2
== SSA_NAME
)
1254 /* NAME - CST < NAME */
1256 else if (code2
== PLUS_EXPR
)
1257 /* NAME - CST1 < NAME + CST2 */
1259 else if (code2
== MINUS_EXPR
)
1260 /* NAME - CST1 > NAME - CST2, if CST1 < CST2. Notice that
1261 C1 and C2 are swapped in the call to compare_values. */
1262 return compare_values_warnv (c2
, c1
, strict_overflow_p
);
1268 /* We cannot compare non-constants. */
1269 if (!is_gimple_min_invariant (val1
) || !is_gimple_min_invariant (val2
))
1272 if (!POINTER_TYPE_P (TREE_TYPE (val1
)))
1274 /* We cannot compare overflowed values, except for overflow
1276 if (TREE_OVERFLOW (val1
) || TREE_OVERFLOW (val2
))
1278 if (strict_overflow_p
!= NULL
)
1279 *strict_overflow_p
= true;
1280 if (is_negative_overflow_infinity (val1
))
1281 return is_negative_overflow_infinity (val2
) ? 0 : -1;
1282 else if (is_negative_overflow_infinity (val2
))
1284 else if (is_positive_overflow_infinity (val1
))
1285 return is_positive_overflow_infinity (val2
) ? 0 : 1;
1286 else if (is_positive_overflow_infinity (val2
))
1291 return tree_int_cst_compare (val1
, val2
);
1297 /* First see if VAL1 and VAL2 are not the same. */
1298 if (val1
== val2
|| operand_equal_p (val1
, val2
, 0))
1301 /* If VAL1 is a lower address than VAL2, return -1. */
1302 if (operand_less_p (val1
, val2
) == 1)
1305 /* If VAL1 is a higher address than VAL2, return +1. */
1306 if (operand_less_p (val2
, val1
) == 1)
1309 /* If VAL1 is different than VAL2, return +2.
1310 For integer constants we either have already returned -1 or 1
1311 or they are equivalent. We still might succeed in proving
1312 something about non-trivial operands. */
1313 if (TREE_CODE (val1
) != INTEGER_CST
1314 || TREE_CODE (val2
) != INTEGER_CST
)
1316 t
= fold_binary_to_constant (NE_EXPR
, boolean_type_node
, val1
, val2
);
1317 if (t
&& integer_onep (t
))
1325 /* Compare values like compare_values_warnv, but treat comparisons of
1326 nonconstants which rely on undefined overflow as incomparable. */
1329 compare_values (tree val1
, tree val2
)
1335 ret
= compare_values_warnv (val1
, val2
, &sop
);
1337 && (!is_gimple_min_invariant (val1
) || !is_gimple_min_invariant (val2
)))
1343 /* Return 1 if VAL is inside value range MIN <= VAL <= MAX,
1344 0 if VAL is not inside [MIN, MAX],
1345 -2 if we cannot tell either way.
1347 Benchmark compile/20001226-1.c compilation time after changing this
1351 value_inside_range (tree val
, tree min
, tree max
)
1355 cmp1
= operand_less_p (val
, min
);
1361 cmp2
= operand_less_p (max
, val
);
1369 /* Return true if value ranges VR0 and VR1 have a non-empty
1372 Benchmark compile/20001226-1.c compilation time after changing this
1377 value_ranges_intersect_p (value_range_t
*vr0
, value_range_t
*vr1
)
1379 /* The value ranges do not intersect if the maximum of the first range is
1380 less than the minimum of the second range or vice versa.
1381 When those relations are unknown, we can't do any better. */
1382 if (operand_less_p (vr0
->max
, vr1
->min
) != 0)
1384 if (operand_less_p (vr1
->max
, vr0
->min
) != 0)
1390 /* Return 1 if [MIN, MAX] includes the value zero, 0 if it does not
1391 include the value zero, -2 if we cannot tell. */
1394 range_includes_zero_p (tree min
, tree max
)
1396 tree zero
= build_int_cst (TREE_TYPE (min
), 0);
1397 return value_inside_range (zero
, min
, max
);
1400 /* Return true if *VR is know to only contain nonnegative values. */
1403 value_range_nonnegative_p (value_range_t
*vr
)
1405 /* Testing for VR_ANTI_RANGE is not useful here as any anti-range
1406 which would return a useful value should be encoded as a
1408 if (vr
->type
== VR_RANGE
)
1410 int result
= compare_values (vr
->min
, integer_zero_node
);
1411 return (result
== 0 || result
== 1);
1417 /* Return true if T, an SSA_NAME, is known to be nonnegative. Return
1418 false otherwise or if no value range information is available. */
1421 ssa_name_nonnegative_p (const_tree t
)
1423 value_range_t
*vr
= get_value_range (t
);
1425 if (INTEGRAL_TYPE_P (t
)
1426 && TYPE_UNSIGNED (t
))
1432 return value_range_nonnegative_p (vr
);
1435 /* If *VR has a value rante that is a single constant value return that,
1436 otherwise return NULL_TREE. */
1439 value_range_constant_singleton (value_range_t
*vr
)
1441 if (vr
->type
== VR_RANGE
1442 && operand_equal_p (vr
->min
, vr
->max
, 0)
1443 && is_gimple_min_invariant (vr
->min
))
1449 /* If OP has a value range with a single constant value return that,
1450 otherwise return NULL_TREE. This returns OP itself if OP is a
1454 op_with_constant_singleton_value_range (tree op
)
1456 if (is_gimple_min_invariant (op
))
1459 if (TREE_CODE (op
) != SSA_NAME
)
1462 return value_range_constant_singleton (get_value_range (op
));
1465 /* Return true if op is in a boolean [0, 1] value-range. */
1468 op_with_boolean_value_range_p (tree op
)
1472 if (TYPE_PRECISION (TREE_TYPE (op
)) == 1)
1475 if (integer_zerop (op
)
1476 || integer_onep (op
))
1479 if (TREE_CODE (op
) != SSA_NAME
)
1482 vr
= get_value_range (op
);
1483 return (vr
->type
== VR_RANGE
1484 && integer_zerop (vr
->min
)
1485 && integer_onep (vr
->max
));
1488 /* Extract value range information from an ASSERT_EXPR EXPR and store
1492 extract_range_from_assert (value_range_t
*vr_p
, tree expr
)
1494 tree var
, cond
, limit
, min
, max
, type
;
1495 value_range_t
*limit_vr
;
1496 enum tree_code cond_code
;
1498 var
= ASSERT_EXPR_VAR (expr
);
1499 cond
= ASSERT_EXPR_COND (expr
);
1501 gcc_assert (COMPARISON_CLASS_P (cond
));
1503 /* Find VAR in the ASSERT_EXPR conditional. */
1504 if (var
== TREE_OPERAND (cond
, 0)
1505 || TREE_CODE (TREE_OPERAND (cond
, 0)) == PLUS_EXPR
1506 || TREE_CODE (TREE_OPERAND (cond
, 0)) == NOP_EXPR
)
1508 /* If the predicate is of the form VAR COMP LIMIT, then we just
1509 take LIMIT from the RHS and use the same comparison code. */
1510 cond_code
= TREE_CODE (cond
);
1511 limit
= TREE_OPERAND (cond
, 1);
1512 cond
= TREE_OPERAND (cond
, 0);
1516 /* If the predicate is of the form LIMIT COMP VAR, then we need
1517 to flip around the comparison code to create the proper range
1519 cond_code
= swap_tree_comparison (TREE_CODE (cond
));
1520 limit
= TREE_OPERAND (cond
, 0);
1521 cond
= TREE_OPERAND (cond
, 1);
1524 limit
= avoid_overflow_infinity (limit
);
1526 type
= TREE_TYPE (var
);
1527 gcc_assert (limit
!= var
);
1529 /* For pointer arithmetic, we only keep track of pointer equality
1531 if (POINTER_TYPE_P (type
) && cond_code
!= NE_EXPR
&& cond_code
!= EQ_EXPR
)
1533 set_value_range_to_varying (vr_p
);
1537 /* If LIMIT is another SSA name and LIMIT has a range of its own,
1538 try to use LIMIT's range to avoid creating symbolic ranges
1540 limit_vr
= (TREE_CODE (limit
) == SSA_NAME
) ? get_value_range (limit
) : NULL
;
1542 /* LIMIT's range is only interesting if it has any useful information. */
1544 && (limit_vr
->type
== VR_UNDEFINED
1545 || limit_vr
->type
== VR_VARYING
1546 || symbolic_range_p (limit_vr
)))
1549 /* Initially, the new range has the same set of equivalences of
1550 VAR's range. This will be revised before returning the final
1551 value. Since assertions may be chained via mutually exclusive
1552 predicates, we will need to trim the set of equivalences before
1554 gcc_assert (vr_p
->equiv
== NULL
);
1555 add_equivalence (&vr_p
->equiv
, var
);
1557 /* Extract a new range based on the asserted comparison for VAR and
1558 LIMIT's value range. Notice that if LIMIT has an anti-range, we
1559 will only use it for equality comparisons (EQ_EXPR). For any
1560 other kind of assertion, we cannot derive a range from LIMIT's
1561 anti-range that can be used to describe the new range. For
1562 instance, ASSERT_EXPR <x_2, x_2 <= b_4>. If b_4 is ~[2, 10],
1563 then b_4 takes on the ranges [-INF, 1] and [11, +INF]. There is
1564 no single range for x_2 that could describe LE_EXPR, so we might
1565 as well build the range [b_4, +INF] for it.
1566 One special case we handle is extracting a range from a
1567 range test encoded as (unsigned)var + CST <= limit. */
1568 if (TREE_CODE (cond
) == NOP_EXPR
1569 || TREE_CODE (cond
) == PLUS_EXPR
)
1571 if (TREE_CODE (cond
) == PLUS_EXPR
)
1573 min
= fold_build1 (NEGATE_EXPR
, TREE_TYPE (TREE_OPERAND (cond
, 1)),
1574 TREE_OPERAND (cond
, 1));
1575 max
= int_const_binop (PLUS_EXPR
, limit
, min
);
1576 cond
= TREE_OPERAND (cond
, 0);
1580 min
= build_int_cst (TREE_TYPE (var
), 0);
1584 /* Make sure to not set TREE_OVERFLOW on the final type
1585 conversion. We are willingly interpreting large positive
1586 unsigned values as negative singed values here. */
1587 min
= force_fit_type_double (TREE_TYPE (var
), tree_to_double_int (min
),
1589 max
= force_fit_type_double (TREE_TYPE (var
), tree_to_double_int (max
),
1592 /* We can transform a max, min range to an anti-range or
1593 vice-versa. Use set_and_canonicalize_value_range which does
1595 if (cond_code
== LE_EXPR
)
1596 set_and_canonicalize_value_range (vr_p
, VR_RANGE
,
1597 min
, max
, vr_p
->equiv
);
1598 else if (cond_code
== GT_EXPR
)
1599 set_and_canonicalize_value_range (vr_p
, VR_ANTI_RANGE
,
1600 min
, max
, vr_p
->equiv
);
1604 else if (cond_code
== EQ_EXPR
)
1606 enum value_range_type range_type
;
1610 range_type
= limit_vr
->type
;
1611 min
= limit_vr
->min
;
1612 max
= limit_vr
->max
;
1616 range_type
= VR_RANGE
;
1621 set_value_range (vr_p
, range_type
, min
, max
, vr_p
->equiv
);
1623 /* When asserting the equality VAR == LIMIT and LIMIT is another
1624 SSA name, the new range will also inherit the equivalence set
1626 if (TREE_CODE (limit
) == SSA_NAME
)
1627 add_equivalence (&vr_p
->equiv
, limit
);
1629 else if (cond_code
== NE_EXPR
)
1631 /* As described above, when LIMIT's range is an anti-range and
1632 this assertion is an inequality (NE_EXPR), then we cannot
1633 derive anything from the anti-range. For instance, if
1634 LIMIT's range was ~[0, 0], the assertion 'VAR != LIMIT' does
1635 not imply that VAR's range is [0, 0]. So, in the case of
1636 anti-ranges, we just assert the inequality using LIMIT and
1639 If LIMIT_VR is a range, we can only use it to build a new
1640 anti-range if LIMIT_VR is a single-valued range. For
1641 instance, if LIMIT_VR is [0, 1], the predicate
1642 VAR != [0, 1] does not mean that VAR's range is ~[0, 1].
1643 Rather, it means that for value 0 VAR should be ~[0, 0]
1644 and for value 1, VAR should be ~[1, 1]. We cannot
1645 represent these ranges.
1647 The only situation in which we can build a valid
1648 anti-range is when LIMIT_VR is a single-valued range
1649 (i.e., LIMIT_VR->MIN == LIMIT_VR->MAX). In that case,
1650 build the anti-range ~[LIMIT_VR->MIN, LIMIT_VR->MAX]. */
1652 && limit_vr
->type
== VR_RANGE
1653 && compare_values (limit_vr
->min
, limit_vr
->max
) == 0)
1655 min
= limit_vr
->min
;
1656 max
= limit_vr
->max
;
1660 /* In any other case, we cannot use LIMIT's range to build a
1661 valid anti-range. */
1665 /* If MIN and MAX cover the whole range for their type, then
1666 just use the original LIMIT. */
1667 if (INTEGRAL_TYPE_P (type
)
1668 && vrp_val_is_min (min
)
1669 && vrp_val_is_max (max
))
1672 set_value_range (vr_p
, VR_ANTI_RANGE
, min
, max
, vr_p
->equiv
);
1674 else if (cond_code
== LE_EXPR
|| cond_code
== LT_EXPR
)
1676 min
= TYPE_MIN_VALUE (type
);
1678 if (limit_vr
== NULL
|| limit_vr
->type
== VR_ANTI_RANGE
)
1682 /* If LIMIT_VR is of the form [N1, N2], we need to build the
1683 range [MIN, N2] for LE_EXPR and [MIN, N2 - 1] for
1685 max
= limit_vr
->max
;
1688 /* If the maximum value forces us to be out of bounds, simply punt.
1689 It would be pointless to try and do anything more since this
1690 all should be optimized away above us. */
1691 if ((cond_code
== LT_EXPR
1692 && compare_values (max
, min
) == 0)
1693 || (CONSTANT_CLASS_P (max
) && TREE_OVERFLOW (max
)))
1694 set_value_range_to_varying (vr_p
);
1697 /* For LT_EXPR, we create the range [MIN, MAX - 1]. */
1698 if (cond_code
== LT_EXPR
)
1700 if (TYPE_PRECISION (TREE_TYPE (max
)) == 1
1701 && !TYPE_UNSIGNED (TREE_TYPE (max
)))
1702 max
= fold_build2 (PLUS_EXPR
, TREE_TYPE (max
), max
,
1703 build_int_cst (TREE_TYPE (max
), -1));
1705 max
= fold_build2 (MINUS_EXPR
, TREE_TYPE (max
), max
,
1706 build_int_cst (TREE_TYPE (max
), 1));
1708 TREE_NO_WARNING (max
) = 1;
1711 set_value_range (vr_p
, VR_RANGE
, min
, max
, vr_p
->equiv
);
1714 else if (cond_code
== GE_EXPR
|| cond_code
== GT_EXPR
)
1716 max
= TYPE_MAX_VALUE (type
);
1718 if (limit_vr
== NULL
|| limit_vr
->type
== VR_ANTI_RANGE
)
1722 /* If LIMIT_VR is of the form [N1, N2], we need to build the
1723 range [N1, MAX] for GE_EXPR and [N1 + 1, MAX] for
1725 min
= limit_vr
->min
;
1728 /* If the minimum value forces us to be out of bounds, simply punt.
1729 It would be pointless to try and do anything more since this
1730 all should be optimized away above us. */
1731 if ((cond_code
== GT_EXPR
1732 && compare_values (min
, max
) == 0)
1733 || (CONSTANT_CLASS_P (min
) && TREE_OVERFLOW (min
)))
1734 set_value_range_to_varying (vr_p
);
1737 /* For GT_EXPR, we create the range [MIN + 1, MAX]. */
1738 if (cond_code
== GT_EXPR
)
1740 if (TYPE_PRECISION (TREE_TYPE (min
)) == 1
1741 && !TYPE_UNSIGNED (TREE_TYPE (min
)))
1742 min
= fold_build2 (MINUS_EXPR
, TREE_TYPE (min
), min
,
1743 build_int_cst (TREE_TYPE (min
), -1));
1745 min
= fold_build2 (PLUS_EXPR
, TREE_TYPE (min
), min
,
1746 build_int_cst (TREE_TYPE (min
), 1));
1748 TREE_NO_WARNING (min
) = 1;
1751 set_value_range (vr_p
, VR_RANGE
, min
, max
, vr_p
->equiv
);
1757 /* Finally intersect the new range with what we already know about var. */
1758 vrp_intersect_ranges (vr_p
, get_value_range (var
));
1762 /* Extract range information from SSA name VAR and store it in VR. If
1763 VAR has an interesting range, use it. Otherwise, create the
1764 range [VAR, VAR] and return it. This is useful in situations where
1765 we may have conditionals testing values of VARYING names. For
1772 Even if y_5 is deemed VARYING, we can determine that x_3 > y_5 is
1776 extract_range_from_ssa_name (value_range_t
*vr
, tree var
)
1778 value_range_t
*var_vr
= get_value_range (var
);
1780 if (var_vr
->type
!= VR_UNDEFINED
&& var_vr
->type
!= VR_VARYING
)
1781 copy_value_range (vr
, var_vr
);
1783 set_value_range (vr
, VR_RANGE
, var
, var
, NULL
);
1785 add_equivalence (&vr
->equiv
, var
);
1789 /* Wrapper around int_const_binop. If the operation overflows and we
1790 are not using wrapping arithmetic, then adjust the result to be
1791 -INF or +INF depending on CODE, VAL1 and VAL2. This can return
1792 NULL_TREE if we need to use an overflow infinity representation but
1793 the type does not support it. */
1796 vrp_int_const_binop (enum tree_code code
, tree val1
, tree val2
)
1800 res
= int_const_binop (code
, val1
, val2
);
1802 /* If we are using unsigned arithmetic, operate symbolically
1803 on -INF and +INF as int_const_binop only handles signed overflow. */
1804 if (TYPE_UNSIGNED (TREE_TYPE (val1
)))
1806 int checkz
= compare_values (res
, val1
);
1807 bool overflow
= false;
1809 /* Ensure that res = val1 [+*] val2 >= val1
1810 or that res = val1 - val2 <= val1. */
1811 if ((code
== PLUS_EXPR
1812 && !(checkz
== 1 || checkz
== 0))
1813 || (code
== MINUS_EXPR
1814 && !(checkz
== 0 || checkz
== -1)))
1818 /* Checking for multiplication overflow is done by dividing the
1819 output of the multiplication by the first input of the
1820 multiplication. If the result of that division operation is
1821 not equal to the second input of the multiplication, then the
1822 multiplication overflowed. */
1823 else if (code
== MULT_EXPR
&& !integer_zerop (val1
))
1825 tree tmp
= int_const_binop (TRUNC_DIV_EXPR
,
1828 int check
= compare_values (tmp
, val2
);
1836 res
= copy_node (res
);
1837 TREE_OVERFLOW (res
) = 1;
1841 else if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (val1
)))
1842 /* If the singed operation wraps then int_const_binop has done
1843 everything we want. */
1845 else if ((TREE_OVERFLOW (res
)
1846 && !TREE_OVERFLOW (val1
)
1847 && !TREE_OVERFLOW (val2
))
1848 || is_overflow_infinity (val1
)
1849 || is_overflow_infinity (val2
))
1851 /* If the operation overflowed but neither VAL1 nor VAL2 are
1852 overflown, return -INF or +INF depending on the operation
1853 and the combination of signs of the operands. */
1854 int sgn1
= tree_int_cst_sgn (val1
);
1855 int sgn2
= tree_int_cst_sgn (val2
);
1857 if (needs_overflow_infinity (TREE_TYPE (res
))
1858 && !supports_overflow_infinity (TREE_TYPE (res
)))
1861 /* We have to punt on adding infinities of different signs,
1862 since we can't tell what the sign of the result should be.
1863 Likewise for subtracting infinities of the same sign. */
1864 if (((code
== PLUS_EXPR
&& sgn1
!= sgn2
)
1865 || (code
== MINUS_EXPR
&& sgn1
== sgn2
))
1866 && is_overflow_infinity (val1
)
1867 && is_overflow_infinity (val2
))
1870 /* Don't try to handle division or shifting of infinities. */
1871 if ((code
== TRUNC_DIV_EXPR
1872 || code
== FLOOR_DIV_EXPR
1873 || code
== CEIL_DIV_EXPR
1874 || code
== EXACT_DIV_EXPR
1875 || code
== ROUND_DIV_EXPR
1876 || code
== RSHIFT_EXPR
)
1877 && (is_overflow_infinity (val1
)
1878 || is_overflow_infinity (val2
)))
1881 /* Notice that we only need to handle the restricted set of
1882 operations handled by extract_range_from_binary_expr.
1883 Among them, only multiplication, addition and subtraction
1884 can yield overflow without overflown operands because we
1885 are working with integral types only... except in the
1886 case VAL1 = -INF and VAL2 = -1 which overflows to +INF
1887 for division too. */
1889 /* For multiplication, the sign of the overflow is given
1890 by the comparison of the signs of the operands. */
1891 if ((code
== MULT_EXPR
&& sgn1
== sgn2
)
1892 /* For addition, the operands must be of the same sign
1893 to yield an overflow. Its sign is therefore that
1894 of one of the operands, for example the first. For
1895 infinite operands X + -INF is negative, not positive. */
1896 || (code
== PLUS_EXPR
1898 ? !is_negative_overflow_infinity (val2
)
1899 : is_positive_overflow_infinity (val2
)))
1900 /* For subtraction, non-infinite operands must be of
1901 different signs to yield an overflow. Its sign is
1902 therefore that of the first operand or the opposite of
1903 that of the second operand. A first operand of 0 counts
1904 as positive here, for the corner case 0 - (-INF), which
1905 overflows, but must yield +INF. For infinite operands 0
1906 - INF is negative, not positive. */
1907 || (code
== MINUS_EXPR
1909 ? !is_positive_overflow_infinity (val2
)
1910 : is_negative_overflow_infinity (val2
)))
1911 /* We only get in here with positive shift count, so the
1912 overflow direction is the same as the sign of val1.
1913 Actually rshift does not overflow at all, but we only
1914 handle the case of shifting overflowed -INF and +INF. */
1915 || (code
== RSHIFT_EXPR
1917 /* For division, the only case is -INF / -1 = +INF. */
1918 || code
== TRUNC_DIV_EXPR
1919 || code
== FLOOR_DIV_EXPR
1920 || code
== CEIL_DIV_EXPR
1921 || code
== EXACT_DIV_EXPR
1922 || code
== ROUND_DIV_EXPR
)
1923 return (needs_overflow_infinity (TREE_TYPE (res
))
1924 ? positive_overflow_infinity (TREE_TYPE (res
))
1925 : TYPE_MAX_VALUE (TREE_TYPE (res
)));
1927 return (needs_overflow_infinity (TREE_TYPE (res
))
1928 ? negative_overflow_infinity (TREE_TYPE (res
))
1929 : TYPE_MIN_VALUE (TREE_TYPE (res
)));
1936 /* For range VR compute two double_int bitmasks. In *MAY_BE_NONZERO
1937 bitmask if some bit is unset, it means for all numbers in the range
1938 the bit is 0, otherwise it might be 0 or 1. In *MUST_BE_NONZERO
1939 bitmask if some bit is set, it means for all numbers in the range
1940 the bit is 1, otherwise it might be 0 or 1. */
1943 zero_nonzero_bits_from_vr (value_range_t
*vr
,
1944 double_int
*may_be_nonzero
,
1945 double_int
*must_be_nonzero
)
1947 *may_be_nonzero
= double_int_minus_one
;
1948 *must_be_nonzero
= double_int_zero
;
1949 if (!range_int_cst_p (vr
)
1950 || TREE_OVERFLOW (vr
->min
)
1951 || TREE_OVERFLOW (vr
->max
))
1954 if (range_int_cst_singleton_p (vr
))
1956 *may_be_nonzero
= tree_to_double_int (vr
->min
);
1957 *must_be_nonzero
= *may_be_nonzero
;
1959 else if (tree_int_cst_sgn (vr
->min
) >= 0
1960 || tree_int_cst_sgn (vr
->max
) < 0)
1962 double_int dmin
= tree_to_double_int (vr
->min
);
1963 double_int dmax
= tree_to_double_int (vr
->max
);
1964 double_int xor_mask
= double_int_xor (dmin
, dmax
);
1965 *may_be_nonzero
= double_int_ior (dmin
, dmax
);
1966 *must_be_nonzero
= double_int_and (dmin
, dmax
);
1967 if (xor_mask
.high
!= 0)
1969 unsigned HOST_WIDE_INT mask
1970 = ((unsigned HOST_WIDE_INT
) 1
1971 << floor_log2 (xor_mask
.high
)) - 1;
1972 may_be_nonzero
->low
= ALL_ONES
;
1973 may_be_nonzero
->high
|= mask
;
1974 must_be_nonzero
->low
= 0;
1975 must_be_nonzero
->high
&= ~mask
;
1977 else if (xor_mask
.low
!= 0)
1979 unsigned HOST_WIDE_INT mask
1980 = ((unsigned HOST_WIDE_INT
) 1
1981 << floor_log2 (xor_mask
.low
)) - 1;
1982 may_be_nonzero
->low
|= mask
;
1983 must_be_nonzero
->low
&= ~mask
;
1990 /* Create two value-ranges in *VR0 and *VR1 from the anti-range *AR
1991 so that *VR0 U *VR1 == *AR. Returns true if that is possible,
1992 false otherwise. If *AR can be represented with a single range
1993 *VR1 will be VR_UNDEFINED. */
1996 ranges_from_anti_range (value_range_t
*ar
,
1997 value_range_t
*vr0
, value_range_t
*vr1
)
1999 tree type
= TREE_TYPE (ar
->min
);
2001 vr0
->type
= VR_UNDEFINED
;
2002 vr1
->type
= VR_UNDEFINED
;
2004 if (ar
->type
!= VR_ANTI_RANGE
2005 || TREE_CODE (ar
->min
) != INTEGER_CST
2006 || TREE_CODE (ar
->max
) != INTEGER_CST
2007 || !vrp_val_min (type
)
2008 || !vrp_val_max (type
))
2011 if (!vrp_val_is_min (ar
->min
))
2013 vr0
->type
= VR_RANGE
;
2014 vr0
->min
= vrp_val_min (type
);
2016 = double_int_to_tree (type
,
2017 double_int_sub (tree_to_double_int (ar
->min
),
2020 if (!vrp_val_is_max (ar
->max
))
2022 vr1
->type
= VR_RANGE
;
2024 = double_int_to_tree (type
,
2025 double_int_add (tree_to_double_int (ar
->max
),
2027 vr1
->max
= vrp_val_max (type
);
2029 if (vr0
->type
== VR_UNDEFINED
)
2032 vr1
->type
= VR_UNDEFINED
;
2035 return vr0
->type
!= VR_UNDEFINED
;
2038 /* Helper to extract a value-range *VR for a multiplicative operation
2042 extract_range_from_multiplicative_op_1 (value_range_t
*vr
,
2043 enum tree_code code
,
2044 value_range_t
*vr0
, value_range_t
*vr1
)
2046 enum value_range_type type
;
2053 /* Multiplications, divisions and shifts are a bit tricky to handle,
2054 depending on the mix of signs we have in the two ranges, we
2055 need to operate on different values to get the minimum and
2056 maximum values for the new range. One approach is to figure
2057 out all the variations of range combinations and do the
2060 However, this involves several calls to compare_values and it
2061 is pretty convoluted. It's simpler to do the 4 operations
2062 (MIN0 OP MIN1, MIN0 OP MAX1, MAX0 OP MIN1 and MAX0 OP MAX0 OP
2063 MAX1) and then figure the smallest and largest values to form
2065 gcc_assert (code
== MULT_EXPR
2066 || code
== TRUNC_DIV_EXPR
2067 || code
== FLOOR_DIV_EXPR
2068 || code
== CEIL_DIV_EXPR
2069 || code
== EXACT_DIV_EXPR
2070 || code
== ROUND_DIV_EXPR
2071 || code
== RSHIFT_EXPR
);
2072 gcc_assert ((vr0
->type
== VR_RANGE
2073 || (code
== MULT_EXPR
&& vr0
->type
== VR_ANTI_RANGE
))
2074 && vr0
->type
== vr1
->type
);
2078 /* Compute the 4 cross operations. */
2080 val
[0] = vrp_int_const_binop (code
, vr0
->min
, vr1
->min
);
2081 if (val
[0] == NULL_TREE
)
2084 if (vr1
->max
== vr1
->min
)
2088 val
[1] = vrp_int_const_binop (code
, vr0
->min
, vr1
->max
);
2089 if (val
[1] == NULL_TREE
)
2093 if (vr0
->max
== vr0
->min
)
2097 val
[2] = vrp_int_const_binop (code
, vr0
->max
, vr1
->min
);
2098 if (val
[2] == NULL_TREE
)
2102 if (vr0
->min
== vr0
->max
|| vr1
->min
== vr1
->max
)
2106 val
[3] = vrp_int_const_binop (code
, vr0
->max
, vr1
->max
);
2107 if (val
[3] == NULL_TREE
)
2113 set_value_range_to_varying (vr
);
2117 /* Set MIN to the minimum of VAL[i] and MAX to the maximum
2121 for (i
= 1; i
< 4; i
++)
2123 if (!is_gimple_min_invariant (min
)
2124 || (TREE_OVERFLOW (min
) && !is_overflow_infinity (min
))
2125 || !is_gimple_min_invariant (max
)
2126 || (TREE_OVERFLOW (max
) && !is_overflow_infinity (max
)))
2131 if (!is_gimple_min_invariant (val
[i
])
2132 || (TREE_OVERFLOW (val
[i
])
2133 && !is_overflow_infinity (val
[i
])))
2135 /* If we found an overflowed value, set MIN and MAX
2136 to it so that we set the resulting range to
2142 if (compare_values (val
[i
], min
) == -1)
2145 if (compare_values (val
[i
], max
) == 1)
2150 /* If either MIN or MAX overflowed, then set the resulting range to
2151 VARYING. But we do accept an overflow infinity
2153 if (min
== NULL_TREE
2154 || !is_gimple_min_invariant (min
)
2155 || (TREE_OVERFLOW (min
) && !is_overflow_infinity (min
))
2157 || !is_gimple_min_invariant (max
)
2158 || (TREE_OVERFLOW (max
) && !is_overflow_infinity (max
)))
2160 set_value_range_to_varying (vr
);
2166 2) [-INF, +-INF(OVF)]
2167 3) [+-INF(OVF), +INF]
2168 4) [+-INF(OVF), +-INF(OVF)]
2169 We learn nothing when we have INF and INF(OVF) on both sides.
2170 Note that we do accept [-INF, -INF] and [+INF, +INF] without
2172 if ((vrp_val_is_min (min
) || is_overflow_infinity (min
))
2173 && (vrp_val_is_max (max
) || is_overflow_infinity (max
)))
2175 set_value_range_to_varying (vr
);
2179 cmp
= compare_values (min
, max
);
2180 if (cmp
== -2 || cmp
== 1)
2182 /* If the new range has its limits swapped around (MIN > MAX),
2183 then the operation caused one of them to wrap around, mark
2184 the new range VARYING. */
2185 set_value_range_to_varying (vr
);
2188 set_value_range (vr
, type
, min
, max
, NULL
);
2191 /* Some quadruple precision helpers. */
2193 quad_int_cmp (double_int l0
, double_int h0
,
2194 double_int l1
, double_int h1
, bool uns
)
2196 int c
= double_int_cmp (h0
, h1
, uns
);
2197 if (c
!= 0) return c
;
2198 return double_int_ucmp (l0
, l1
);
2202 quad_int_pair_sort (double_int
*l0
, double_int
*h0
,
2203 double_int
*l1
, double_int
*h1
, bool uns
)
2205 if (quad_int_cmp (*l0
, *h0
, *l1
, *h1
, uns
) > 0)
2208 tmp
= *l0
; *l0
= *l1
; *l1
= tmp
;
2209 tmp
= *h0
; *h0
= *h1
; *h1
= tmp
;
2213 /* Extract range information from a binary operation CODE based on
2214 the ranges of each of its operands, *VR0 and *VR1 with resulting
2215 type EXPR_TYPE. The resulting range is stored in *VR. */
2218 extract_range_from_binary_expr_1 (value_range_t
*vr
,
2219 enum tree_code code
, tree expr_type
,
2220 value_range_t
*vr0_
, value_range_t
*vr1_
)
2222 value_range_t vr0
= *vr0_
, vr1
= *vr1_
;
2223 value_range_t vrtem0
= VR_INITIALIZER
, vrtem1
= VR_INITIALIZER
;
2224 enum value_range_type type
;
2225 tree min
= NULL_TREE
, max
= NULL_TREE
;
2228 if (!INTEGRAL_TYPE_P (expr_type
)
2229 && !POINTER_TYPE_P (expr_type
))
2231 set_value_range_to_varying (vr
);
2235 /* Not all binary expressions can be applied to ranges in a
2236 meaningful way. Handle only arithmetic operations. */
2237 if (code
!= PLUS_EXPR
2238 && code
!= MINUS_EXPR
2239 && code
!= POINTER_PLUS_EXPR
2240 && code
!= MULT_EXPR
2241 && code
!= TRUNC_DIV_EXPR
2242 && code
!= FLOOR_DIV_EXPR
2243 && code
!= CEIL_DIV_EXPR
2244 && code
!= EXACT_DIV_EXPR
2245 && code
!= ROUND_DIV_EXPR
2246 && code
!= TRUNC_MOD_EXPR
2247 && code
!= RSHIFT_EXPR
2248 && code
!= LSHIFT_EXPR
2251 && code
!= BIT_AND_EXPR
2252 && code
!= BIT_IOR_EXPR
2253 && code
!= BIT_XOR_EXPR
)
2255 set_value_range_to_varying (vr
);
2259 /* If both ranges are UNDEFINED, so is the result. */
2260 if (vr0
.type
== VR_UNDEFINED
&& vr1
.type
== VR_UNDEFINED
)
2262 set_value_range_to_undefined (vr
);
2265 /* If one of the ranges is UNDEFINED drop it to VARYING for the following
2266 code. At some point we may want to special-case operations that
2267 have UNDEFINED result for all or some value-ranges of the not UNDEFINED
2269 else if (vr0
.type
== VR_UNDEFINED
)
2270 set_value_range_to_varying (&vr0
);
2271 else if (vr1
.type
== VR_UNDEFINED
)
2272 set_value_range_to_varying (&vr1
);
2274 /* Now canonicalize anti-ranges to ranges when they are not symbolic
2275 and express ~[] op X as ([]' op X) U ([]'' op X). */
2276 if (vr0
.type
== VR_ANTI_RANGE
2277 && ranges_from_anti_range (&vr0
, &vrtem0
, &vrtem1
))
2279 extract_range_from_binary_expr_1 (vr
, code
, expr_type
, &vrtem0
, vr1_
);
2280 if (vrtem1
.type
!= VR_UNDEFINED
)
2282 value_range_t vrres
= VR_INITIALIZER
;
2283 extract_range_from_binary_expr_1 (&vrres
, code
, expr_type
,
2285 vrp_meet (vr
, &vrres
);
2289 /* Likewise for X op ~[]. */
2290 if (vr1
.type
== VR_ANTI_RANGE
2291 && ranges_from_anti_range (&vr1
, &vrtem0
, &vrtem1
))
2293 extract_range_from_binary_expr_1 (vr
, code
, expr_type
, vr0_
, &vrtem0
);
2294 if (vrtem1
.type
!= VR_UNDEFINED
)
2296 value_range_t vrres
= VR_INITIALIZER
;
2297 extract_range_from_binary_expr_1 (&vrres
, code
, expr_type
,
2299 vrp_meet (vr
, &vrres
);
2304 /* The type of the resulting value range defaults to VR0.TYPE. */
2307 /* Refuse to operate on VARYING ranges, ranges of different kinds
2308 and symbolic ranges. As an exception, we allow BIT_AND_EXPR
2309 because we may be able to derive a useful range even if one of
2310 the operands is VR_VARYING or symbolic range. Similarly for
2311 divisions. TODO, we may be able to derive anti-ranges in
2313 if (code
!= BIT_AND_EXPR
2314 && code
!= BIT_IOR_EXPR
2315 && code
!= TRUNC_DIV_EXPR
2316 && code
!= FLOOR_DIV_EXPR
2317 && code
!= CEIL_DIV_EXPR
2318 && code
!= EXACT_DIV_EXPR
2319 && code
!= ROUND_DIV_EXPR
2320 && code
!= TRUNC_MOD_EXPR
2321 && (vr0
.type
== VR_VARYING
2322 || vr1
.type
== VR_VARYING
2323 || vr0
.type
!= vr1
.type
2324 || symbolic_range_p (&vr0
)
2325 || symbolic_range_p (&vr1
)))
2327 set_value_range_to_varying (vr
);
2331 /* Now evaluate the expression to determine the new range. */
2332 if (POINTER_TYPE_P (expr_type
))
2334 if (code
== MIN_EXPR
|| code
== MAX_EXPR
)
2336 /* For MIN/MAX expressions with pointers, we only care about
2337 nullness, if both are non null, then the result is nonnull.
2338 If both are null, then the result is null. Otherwise they
2340 if (range_is_nonnull (&vr0
) && range_is_nonnull (&vr1
))
2341 set_value_range_to_nonnull (vr
, expr_type
);
2342 else if (range_is_null (&vr0
) && range_is_null (&vr1
))
2343 set_value_range_to_null (vr
, expr_type
);
2345 set_value_range_to_varying (vr
);
2347 else if (code
== POINTER_PLUS_EXPR
)
2349 /* For pointer types, we are really only interested in asserting
2350 whether the expression evaluates to non-NULL. */
2351 if (range_is_nonnull (&vr0
) || range_is_nonnull (&vr1
))
2352 set_value_range_to_nonnull (vr
, expr_type
);
2353 else if (range_is_null (&vr0
) && range_is_null (&vr1
))
2354 set_value_range_to_null (vr
, expr_type
);
2356 set_value_range_to_varying (vr
);
2358 else if (code
== BIT_AND_EXPR
)
2360 /* For pointer types, we are really only interested in asserting
2361 whether the expression evaluates to non-NULL. */
2362 if (range_is_nonnull (&vr0
) && range_is_nonnull (&vr1
))
2363 set_value_range_to_nonnull (vr
, expr_type
);
2364 else if (range_is_null (&vr0
) || range_is_null (&vr1
))
2365 set_value_range_to_null (vr
, expr_type
);
2367 set_value_range_to_varying (vr
);
2370 set_value_range_to_varying (vr
);
2375 /* For integer ranges, apply the operation to each end of the
2376 range and see what we end up with. */
2377 if (code
== PLUS_EXPR
|| code
== MINUS_EXPR
)
2379 /* If we have a PLUS_EXPR with two VR_RANGE integer constant
2380 ranges compute the precise range for such case if possible. */
2381 if (range_int_cst_p (&vr0
)
2382 && range_int_cst_p (&vr1
)
2383 /* We need as many bits as the possibly unsigned inputs. */
2384 && TYPE_PRECISION (expr_type
) <= HOST_BITS_PER_DOUBLE_INT
)
2386 double_int min0
= tree_to_double_int (vr0
.min
);
2387 double_int max0
= tree_to_double_int (vr0
.max
);
2388 double_int min1
= tree_to_double_int (vr1
.min
);
2389 double_int max1
= tree_to_double_int (vr1
.max
);
2390 bool uns
= TYPE_UNSIGNED (expr_type
);
2392 = double_int_min_value (TYPE_PRECISION (expr_type
), uns
);
2394 = double_int_max_value (TYPE_PRECISION (expr_type
), uns
);
2395 double_int dmin
, dmax
;
2399 if (code
== PLUS_EXPR
)
2401 dmin
= double_int_add (min0
, min1
);
2402 dmax
= double_int_add (max0
, max1
);
2404 /* Check for overflow in double_int. */
2405 if (double_int_cmp (min1
, double_int_zero
, uns
)
2406 != double_int_cmp (dmin
, min0
, uns
))
2407 min_ovf
= double_int_cmp (min0
, dmin
, uns
);
2408 if (double_int_cmp (max1
, double_int_zero
, uns
)
2409 != double_int_cmp (dmax
, max0
, uns
))
2410 max_ovf
= double_int_cmp (max0
, dmax
, uns
);
2412 else /* if (code == MINUS_EXPR) */
2414 dmin
= double_int_sub (min0
, max1
);
2415 dmax
= double_int_sub (max0
, min1
);
2417 if (double_int_cmp (double_int_zero
, max1
, uns
)
2418 != double_int_cmp (dmin
, min0
, uns
))
2419 min_ovf
= double_int_cmp (min0
, max1
, uns
);
2420 if (double_int_cmp (double_int_zero
, min1
, uns
)
2421 != double_int_cmp (dmax
, max0
, uns
))
2422 max_ovf
= double_int_cmp (max0
, min1
, uns
);
2425 /* For non-wrapping arithmetic look at possibly smaller
2426 value-ranges of the type. */
2427 if (!TYPE_OVERFLOW_WRAPS (expr_type
))
2429 if (vrp_val_min (expr_type
))
2430 type_min
= tree_to_double_int (vrp_val_min (expr_type
));
2431 if (vrp_val_max (expr_type
))
2432 type_max
= tree_to_double_int (vrp_val_max (expr_type
));
2435 /* Check for type overflow. */
2438 if (double_int_cmp (dmin
, type_min
, uns
) == -1)
2440 else if (double_int_cmp (dmin
, type_max
, uns
) == 1)
2445 if (double_int_cmp (dmax
, type_min
, uns
) == -1)
2447 else if (double_int_cmp (dmax
, type_max
, uns
) == 1)
2451 if (TYPE_OVERFLOW_WRAPS (expr_type
))
2453 /* If overflow wraps, truncate the values and adjust the
2454 range kind and bounds appropriately. */
2456 = double_int_ext (dmin
, TYPE_PRECISION (expr_type
), uns
);
2458 = double_int_ext (dmax
, TYPE_PRECISION (expr_type
), uns
);
2459 if (min_ovf
== max_ovf
)
2461 /* No overflow or both overflow or underflow. The
2462 range kind stays VR_RANGE. */
2463 min
= double_int_to_tree (expr_type
, tmin
);
2464 max
= double_int_to_tree (expr_type
, tmax
);
2466 else if (min_ovf
== -1
2469 /* Underflow and overflow, drop to VR_VARYING. */
2470 set_value_range_to_varying (vr
);
2475 /* Min underflow or max overflow. The range kind
2476 changes to VR_ANTI_RANGE. */
2477 bool covers
= false;
2478 double_int tem
= tmin
;
2479 gcc_assert ((min_ovf
== -1 && max_ovf
== 0)
2480 || (max_ovf
== 1 && min_ovf
== 0));
2481 type
= VR_ANTI_RANGE
;
2482 tmin
= double_int_add (tmax
, double_int_one
);
2483 if (double_int_cmp (tmin
, tmax
, uns
) < 0)
2485 tmax
= double_int_add (tem
, double_int_minus_one
);
2486 if (double_int_cmp (tmax
, tem
, uns
) > 0)
2488 /* If the anti-range would cover nothing, drop to varying.
2489 Likewise if the anti-range bounds are outside of the
2491 if (covers
|| double_int_cmp (tmin
, tmax
, uns
) > 0)
2493 set_value_range_to_varying (vr
);
2496 min
= double_int_to_tree (expr_type
, tmin
);
2497 max
= double_int_to_tree (expr_type
, tmax
);
2502 /* If overflow does not wrap, saturate to the types min/max
2506 if (needs_overflow_infinity (expr_type
)
2507 && supports_overflow_infinity (expr_type
))
2508 min
= negative_overflow_infinity (expr_type
);
2510 min
= double_int_to_tree (expr_type
, type_min
);
2512 else if (min_ovf
== 1)
2514 if (needs_overflow_infinity (expr_type
)
2515 && supports_overflow_infinity (expr_type
))
2516 min
= positive_overflow_infinity (expr_type
);
2518 min
= double_int_to_tree (expr_type
, type_max
);
2521 min
= double_int_to_tree (expr_type
, dmin
);
2525 if (needs_overflow_infinity (expr_type
)
2526 && supports_overflow_infinity (expr_type
))
2527 max
= negative_overflow_infinity (expr_type
);
2529 max
= double_int_to_tree (expr_type
, type_min
);
2531 else if (max_ovf
== 1)
2533 if (needs_overflow_infinity (expr_type
)
2534 && supports_overflow_infinity (expr_type
))
2535 max
= positive_overflow_infinity (expr_type
);
2537 max
= double_int_to_tree (expr_type
, type_max
);
2540 max
= double_int_to_tree (expr_type
, dmax
);
2542 if (needs_overflow_infinity (expr_type
)
2543 && supports_overflow_infinity (expr_type
))
2545 if (is_negative_overflow_infinity (vr0
.min
)
2546 || (code
== PLUS_EXPR
2547 ? is_negative_overflow_infinity (vr1
.min
)
2548 : is_positive_overflow_infinity (vr1
.max
)))
2549 min
= negative_overflow_infinity (expr_type
);
2550 if (is_positive_overflow_infinity (vr0
.max
)
2551 || (code
== PLUS_EXPR
2552 ? is_positive_overflow_infinity (vr1
.max
)
2553 : is_negative_overflow_infinity (vr1
.min
)))
2554 max
= positive_overflow_infinity (expr_type
);
2559 /* For other cases, for example if we have a PLUS_EXPR with two
2560 VR_ANTI_RANGEs, drop to VR_VARYING. It would take more effort
2561 to compute a precise range for such a case.
2562 ??? General even mixed range kind operations can be expressed
2563 by for example transforming ~[3, 5] + [1, 2] to range-only
2564 operations and a union primitive:
2565 [-INF, 2] + [1, 2] U [5, +INF] + [1, 2]
2566 [-INF+1, 4] U [6, +INF(OVF)]
2567 though usually the union is not exactly representable with
2568 a single range or anti-range as the above is
2569 [-INF+1, +INF(OVF)] intersected with ~[5, 5]
2570 but one could use a scheme similar to equivalences for this. */
2571 set_value_range_to_varying (vr
);
2575 else if (code
== MIN_EXPR
2576 || code
== MAX_EXPR
)
2578 if (vr0
.type
== VR_ANTI_RANGE
)
2580 /* For MIN_EXPR and MAX_EXPR with two VR_ANTI_RANGEs,
2581 the resulting VR_ANTI_RANGE is the same - intersection
2582 of the two ranges. */
2583 min
= vrp_int_const_binop (MAX_EXPR
, vr0
.min
, vr1
.min
);
2584 max
= vrp_int_const_binop (MIN_EXPR
, vr0
.max
, vr1
.max
);
2588 /* For operations that make the resulting range directly
2589 proportional to the original ranges, apply the operation to
2590 the same end of each range. */
2591 min
= vrp_int_const_binop (code
, vr0
.min
, vr1
.min
);
2592 max
= vrp_int_const_binop (code
, vr0
.max
, vr1
.max
);
2595 else if (code
== MULT_EXPR
)
2597 /* Fancy code so that with unsigned, [-3,-1]*[-3,-1] does not
2599 if (range_int_cst_p (&vr0
)
2600 && range_int_cst_p (&vr1
)
2601 && TYPE_OVERFLOW_WRAPS (expr_type
))
2603 double_int min0
, max0
, min1
, max1
, sizem1
, size
;
2604 double_int prod0l
, prod0h
, prod1l
, prod1h
,
2605 prod2l
, prod2h
, prod3l
, prod3h
;
2606 bool uns0
, uns1
, uns
;
2608 sizem1
= double_int_max_value (TYPE_PRECISION (expr_type
), true);
2609 size
= double_int_add (sizem1
, double_int_one
);
2611 min0
= tree_to_double_int (vr0
.min
);
2612 max0
= tree_to_double_int (vr0
.max
);
2613 min1
= tree_to_double_int (vr1
.min
);
2614 max1
= tree_to_double_int (vr1
.max
);
2616 uns0
= TYPE_UNSIGNED (expr_type
);
2619 /* Canonicalize the intervals. */
2620 if (TYPE_UNSIGNED (expr_type
))
2622 double_int min2
= double_int_sub (size
, min0
);
2623 if (double_int_cmp (min2
, max0
, true) < 0)
2625 min0
= double_int_neg (min2
);
2626 max0
= double_int_sub (max0
, size
);
2630 min2
= double_int_sub (size
, min1
);
2631 if (double_int_cmp (min2
, max1
, true) < 0)
2633 min1
= double_int_neg (min2
);
2634 max1
= double_int_sub (max1
, size
);
2640 mul_double_wide_with_sign (min0
.low
, min0
.high
,
2641 min1
.low
, min1
.high
,
2642 &prod0l
.low
, &prod0l
.high
,
2643 &prod0h
.low
, &prod0h
.high
, true);
2644 if (!uns0
&& double_int_negative_p (min0
))
2645 prod0h
= double_int_sub (prod0h
, min1
);
2646 if (!uns1
&& double_int_negative_p (min1
))
2647 prod0h
= double_int_sub (prod0h
, min0
);
2649 mul_double_wide_with_sign (min0
.low
, min0
.high
,
2650 max1
.low
, max1
.high
,
2651 &prod1l
.low
, &prod1l
.high
,
2652 &prod1h
.low
, &prod1h
.high
, true);
2653 if (!uns0
&& double_int_negative_p (min0
))
2654 prod1h
= double_int_sub (prod1h
, max1
);
2655 if (!uns1
&& double_int_negative_p (max1
))
2656 prod1h
= double_int_sub (prod1h
, min0
);
2658 mul_double_wide_with_sign (max0
.low
, max0
.high
,
2659 min1
.low
, min1
.high
,
2660 &prod2l
.low
, &prod2l
.high
,
2661 &prod2h
.low
, &prod2h
.high
, true);
2662 if (!uns0
&& double_int_negative_p (max0
))
2663 prod2h
= double_int_sub (prod2h
, min1
);
2664 if (!uns1
&& double_int_negative_p (min1
))
2665 prod2h
= double_int_sub (prod2h
, max0
);
2667 mul_double_wide_with_sign (max0
.low
, max0
.high
,
2668 max1
.low
, max1
.high
,
2669 &prod3l
.low
, &prod3l
.high
,
2670 &prod3h
.low
, &prod3h
.high
, true);
2671 if (!uns0
&& double_int_negative_p (max0
))
2672 prod3h
= double_int_sub (prod3h
, max1
);
2673 if (!uns1
&& double_int_negative_p (max1
))
2674 prod3h
= double_int_sub (prod3h
, max0
);
2676 /* Sort the 4 products. */
2677 quad_int_pair_sort (&prod0l
, &prod0h
, &prod3l
, &prod3h
, uns
);
2678 quad_int_pair_sort (&prod1l
, &prod1h
, &prod2l
, &prod2h
, uns
);
2679 quad_int_pair_sort (&prod0l
, &prod0h
, &prod1l
, &prod1h
, uns
);
2680 quad_int_pair_sort (&prod2l
, &prod2h
, &prod3l
, &prod3h
, uns
);
2683 if (double_int_zero_p (prod0l
))
2685 prod1l
= double_int_zero
;
2686 prod1h
= double_int_neg (prod0h
);
2690 prod1l
= double_int_neg (prod0l
);
2691 prod1h
= double_int_not (prod0h
);
2693 prod2l
= double_int_add (prod3l
, prod1l
);
2694 prod2h
= double_int_add (prod3h
, prod1h
);
2695 if (double_int_ucmp (prod2l
, prod3l
) < 0)
2696 prod2h
= double_int_add (prod2h
, double_int_one
); /* carry */
2698 if (!double_int_zero_p (prod2h
)
2699 || double_int_cmp (prod2l
, sizem1
, true) >= 0)
2701 /* the range covers all values. */
2702 set_value_range_to_varying (vr
);
2706 /* The following should handle the wrapping and selecting
2707 VR_ANTI_RANGE for us. */
2708 min
= double_int_to_tree (expr_type
, prod0l
);
2709 max
= double_int_to_tree (expr_type
, prod3l
);
2710 set_and_canonicalize_value_range (vr
, VR_RANGE
, min
, max
, NULL
);
2714 /* If we have an unsigned MULT_EXPR with two VR_ANTI_RANGEs,
2715 drop to VR_VARYING. It would take more effort to compute a
2716 precise range for such a case. For example, if we have
2717 op0 == 65536 and op1 == 65536 with their ranges both being
2718 ~[0,0] on a 32-bit machine, we would have op0 * op1 == 0, so
2719 we cannot claim that the product is in ~[0,0]. Note that we
2720 are guaranteed to have vr0.type == vr1.type at this
2722 if (vr0
.type
== VR_ANTI_RANGE
2723 && !TYPE_OVERFLOW_UNDEFINED (expr_type
))
2725 set_value_range_to_varying (vr
);
2729 extract_range_from_multiplicative_op_1 (vr
, code
, &vr0
, &vr1
);
2732 else if (code
== RSHIFT_EXPR
2733 || code
== LSHIFT_EXPR
)
2735 /* If we have a RSHIFT_EXPR with any shift values outside [0..prec-1],
2736 then drop to VR_VARYING. Outside of this range we get undefined
2737 behavior from the shift operation. We cannot even trust
2738 SHIFT_COUNT_TRUNCATED at this stage, because that applies to rtl
2739 shifts, and the operation at the tree level may be widened. */
2740 if (range_int_cst_p (&vr1
)
2741 && compare_tree_int (vr1
.min
, 0) >= 0
2742 && compare_tree_int (vr1
.max
, TYPE_PRECISION (expr_type
)) == -1)
2744 if (code
== RSHIFT_EXPR
)
2746 extract_range_from_multiplicative_op_1 (vr
, code
, &vr0
, &vr1
);
2749 /* We can map lshifts by constants to MULT_EXPR handling. */
2750 else if (code
== LSHIFT_EXPR
2751 && range_int_cst_singleton_p (&vr1
))
2753 bool saved_flag_wrapv
;
2754 value_range_t vr1p
= VR_INITIALIZER
;
2755 vr1p
.type
= VR_RANGE
;
2757 = double_int_to_tree (expr_type
,
2760 TREE_INT_CST_LOW (vr1
.min
),
2761 TYPE_PRECISION (expr_type
),
2763 vr1p
.max
= vr1p
.min
;
2764 /* We have to use a wrapping multiply though as signed overflow
2765 on lshifts is implementation defined in C89. */
2766 saved_flag_wrapv
= flag_wrapv
;
2768 extract_range_from_binary_expr_1 (vr
, MULT_EXPR
, expr_type
,
2770 flag_wrapv
= saved_flag_wrapv
;
2774 set_value_range_to_varying (vr
);
2777 else if (code
== TRUNC_DIV_EXPR
2778 || code
== FLOOR_DIV_EXPR
2779 || code
== CEIL_DIV_EXPR
2780 || code
== EXACT_DIV_EXPR
2781 || code
== ROUND_DIV_EXPR
)
2783 if (vr0
.type
!= VR_RANGE
|| symbolic_range_p (&vr0
))
2785 /* For division, if op1 has VR_RANGE but op0 does not, something
2786 can be deduced just from that range. Say [min, max] / [4, max]
2787 gives [min / 4, max / 4] range. */
2788 if (vr1
.type
== VR_RANGE
2789 && !symbolic_range_p (&vr1
)
2790 && range_includes_zero_p (vr1
.min
, vr1
.max
) == 0)
2792 vr0
.type
= type
= VR_RANGE
;
2793 vr0
.min
= vrp_val_min (expr_type
);
2794 vr0
.max
= vrp_val_max (expr_type
);
2798 set_value_range_to_varying (vr
);
2803 /* For divisions, if flag_non_call_exceptions is true, we must
2804 not eliminate a division by zero. */
2805 if (cfun
->can_throw_non_call_exceptions
2806 && (vr1
.type
!= VR_RANGE
2807 || range_includes_zero_p (vr1
.min
, vr1
.max
) != 0))
2809 set_value_range_to_varying (vr
);
2813 /* For divisions, if op0 is VR_RANGE, we can deduce a range
2814 even if op1 is VR_VARYING, VR_ANTI_RANGE, symbolic or can
2816 if (vr0
.type
== VR_RANGE
2817 && (vr1
.type
!= VR_RANGE
2818 || range_includes_zero_p (vr1
.min
, vr1
.max
) != 0))
2820 tree zero
= build_int_cst (TREE_TYPE (vr0
.min
), 0);
2825 if (TYPE_UNSIGNED (expr_type
)
2826 || value_range_nonnegative_p (&vr1
))
2828 /* For unsigned division or when divisor is known
2829 to be non-negative, the range has to cover
2830 all numbers from 0 to max for positive max
2831 and all numbers from min to 0 for negative min. */
2832 cmp
= compare_values (vr0
.max
, zero
);
2835 else if (cmp
== 0 || cmp
== 1)
2839 cmp
= compare_values (vr0
.min
, zero
);
2842 else if (cmp
== 0 || cmp
== -1)
2849 /* Otherwise the range is -max .. max or min .. -min
2850 depending on which bound is bigger in absolute value,
2851 as the division can change the sign. */
2852 abs_extent_range (vr
, vr0
.min
, vr0
.max
);
2855 if (type
== VR_VARYING
)
2857 set_value_range_to_varying (vr
);
2863 extract_range_from_multiplicative_op_1 (vr
, code
, &vr0
, &vr1
);
2867 else if (code
== TRUNC_MOD_EXPR
)
2869 if (vr1
.type
!= VR_RANGE
2870 || range_includes_zero_p (vr1
.min
, vr1
.max
) != 0
2871 || vrp_val_is_min (vr1
.min
))
2873 set_value_range_to_varying (vr
);
2877 /* Compute MAX <|vr1.min|, |vr1.max|> - 1. */
2878 max
= fold_unary_to_constant (ABS_EXPR
, expr_type
, vr1
.min
);
2879 if (tree_int_cst_lt (max
, vr1
.max
))
2881 max
= int_const_binop (MINUS_EXPR
, max
, integer_one_node
);
2882 /* If the dividend is non-negative the modulus will be
2883 non-negative as well. */
2884 if (TYPE_UNSIGNED (expr_type
)
2885 || value_range_nonnegative_p (&vr0
))
2886 min
= build_int_cst (TREE_TYPE (max
), 0);
2888 min
= fold_unary_to_constant (NEGATE_EXPR
, expr_type
, max
);
2890 else if (code
== BIT_AND_EXPR
|| code
== BIT_IOR_EXPR
|| code
== BIT_XOR_EXPR
)
2892 bool int_cst_range0
, int_cst_range1
;
2893 double_int may_be_nonzero0
, may_be_nonzero1
;
2894 double_int must_be_nonzero0
, must_be_nonzero1
;
2896 int_cst_range0
= zero_nonzero_bits_from_vr (&vr0
, &may_be_nonzero0
,
2898 int_cst_range1
= zero_nonzero_bits_from_vr (&vr1
, &may_be_nonzero1
,
2902 if (code
== BIT_AND_EXPR
)
2905 min
= double_int_to_tree (expr_type
,
2906 double_int_and (must_be_nonzero0
,
2908 dmax
= double_int_and (may_be_nonzero0
, may_be_nonzero1
);
2909 /* If both input ranges contain only negative values we can
2910 truncate the result range maximum to the minimum of the
2911 input range maxima. */
2912 if (int_cst_range0
&& int_cst_range1
2913 && tree_int_cst_sgn (vr0
.max
) < 0
2914 && tree_int_cst_sgn (vr1
.max
) < 0)
2916 dmax
= double_int_min (dmax
, tree_to_double_int (vr0
.max
),
2917 TYPE_UNSIGNED (expr_type
));
2918 dmax
= double_int_min (dmax
, tree_to_double_int (vr1
.max
),
2919 TYPE_UNSIGNED (expr_type
));
2921 /* If either input range contains only non-negative values
2922 we can truncate the result range maximum to the respective
2923 maximum of the input range. */
2924 if (int_cst_range0
&& tree_int_cst_sgn (vr0
.min
) >= 0)
2925 dmax
= double_int_min (dmax
, tree_to_double_int (vr0
.max
),
2926 TYPE_UNSIGNED (expr_type
));
2927 if (int_cst_range1
&& tree_int_cst_sgn (vr1
.min
) >= 0)
2928 dmax
= double_int_min (dmax
, tree_to_double_int (vr1
.max
),
2929 TYPE_UNSIGNED (expr_type
));
2930 max
= double_int_to_tree (expr_type
, dmax
);
2932 else if (code
== BIT_IOR_EXPR
)
2935 max
= double_int_to_tree (expr_type
,
2936 double_int_ior (may_be_nonzero0
,
2938 dmin
= double_int_ior (must_be_nonzero0
, must_be_nonzero1
);
2939 /* If the input ranges contain only positive values we can
2940 truncate the minimum of the result range to the maximum
2941 of the input range minima. */
2942 if (int_cst_range0
&& int_cst_range1
2943 && tree_int_cst_sgn (vr0
.min
) >= 0
2944 && tree_int_cst_sgn (vr1
.min
) >= 0)
2946 dmin
= double_int_max (dmin
, tree_to_double_int (vr0
.min
),
2947 TYPE_UNSIGNED (expr_type
));
2948 dmin
= double_int_max (dmin
, tree_to_double_int (vr1
.min
),
2949 TYPE_UNSIGNED (expr_type
));
2951 /* If either input range contains only negative values
2952 we can truncate the minimum of the result range to the
2953 respective minimum range. */
2954 if (int_cst_range0
&& tree_int_cst_sgn (vr0
.max
) < 0)
2955 dmin
= double_int_max (dmin
, tree_to_double_int (vr0
.min
),
2956 TYPE_UNSIGNED (expr_type
));
2957 if (int_cst_range1
&& tree_int_cst_sgn (vr1
.max
) < 0)
2958 dmin
= double_int_max (dmin
, tree_to_double_int (vr1
.min
),
2959 TYPE_UNSIGNED (expr_type
));
2960 min
= double_int_to_tree (expr_type
, dmin
);
2962 else if (code
== BIT_XOR_EXPR
)
2964 double_int result_zero_bits
, result_one_bits
;
2966 = double_int_ior (double_int_and (must_be_nonzero0
,
2969 (double_int_ior (may_be_nonzero0
,
2972 = double_int_ior (double_int_and
2974 double_int_not (may_be_nonzero1
)),
2977 double_int_not (may_be_nonzero0
)));
2978 max
= double_int_to_tree (expr_type
,
2979 double_int_not (result_zero_bits
));
2980 min
= double_int_to_tree (expr_type
, result_one_bits
);
2981 /* If the range has all positive or all negative values the
2982 result is better than VARYING. */
2983 if (tree_int_cst_sgn (min
) < 0
2984 || tree_int_cst_sgn (max
) >= 0)
2987 max
= min
= NULL_TREE
;
2993 /* If either MIN or MAX overflowed, then set the resulting range to
2994 VARYING. But we do accept an overflow infinity
2996 if (min
== NULL_TREE
2997 || !is_gimple_min_invariant (min
)
2998 || (TREE_OVERFLOW (min
) && !is_overflow_infinity (min
))
3000 || !is_gimple_min_invariant (max
)
3001 || (TREE_OVERFLOW (max
) && !is_overflow_infinity (max
)))
3003 set_value_range_to_varying (vr
);
3009 2) [-INF, +-INF(OVF)]
3010 3) [+-INF(OVF), +INF]
3011 4) [+-INF(OVF), +-INF(OVF)]
3012 We learn nothing when we have INF and INF(OVF) on both sides.
3013 Note that we do accept [-INF, -INF] and [+INF, +INF] without
3015 if ((vrp_val_is_min (min
) || is_overflow_infinity (min
))
3016 && (vrp_val_is_max (max
) || is_overflow_infinity (max
)))
3018 set_value_range_to_varying (vr
);
3022 cmp
= compare_values (min
, max
);
3023 if (cmp
== -2 || cmp
== 1)
3025 /* If the new range has its limits swapped around (MIN > MAX),
3026 then the operation caused one of them to wrap around, mark
3027 the new range VARYING. */
3028 set_value_range_to_varying (vr
);
3031 set_value_range (vr
, type
, min
, max
, NULL
);
3034 /* Extract range information from a binary expression OP0 CODE OP1 based on
3035 the ranges of each of its operands with resulting type EXPR_TYPE.
3036 The resulting range is stored in *VR. */
3039 extract_range_from_binary_expr (value_range_t
*vr
,
3040 enum tree_code code
,
3041 tree expr_type
, tree op0
, tree op1
)
3043 value_range_t vr0
= VR_INITIALIZER
;
3044 value_range_t vr1
= VR_INITIALIZER
;
3046 /* Get value ranges for each operand. For constant operands, create
3047 a new value range with the operand to simplify processing. */
3048 if (TREE_CODE (op0
) == SSA_NAME
)
3049 vr0
= *(get_value_range (op0
));
3050 else if (is_gimple_min_invariant (op0
))
3051 set_value_range_to_value (&vr0
, op0
, NULL
);
3053 set_value_range_to_varying (&vr0
);
3055 if (TREE_CODE (op1
) == SSA_NAME
)
3056 vr1
= *(get_value_range (op1
));
3057 else if (is_gimple_min_invariant (op1
))
3058 set_value_range_to_value (&vr1
, op1
, NULL
);
3060 set_value_range_to_varying (&vr1
);
3062 extract_range_from_binary_expr_1 (vr
, code
, expr_type
, &vr0
, &vr1
);
3065 /* Extract range information from a unary operation CODE based on
3066 the range of its operand *VR0 with type OP0_TYPE with resulting type TYPE.
3067 The The resulting range is stored in *VR. */
3070 extract_range_from_unary_expr_1 (value_range_t
*vr
,
3071 enum tree_code code
, tree type
,
3072 value_range_t
*vr0_
, tree op0_type
)
3074 value_range_t vr0
= *vr0_
, vrtem0
= VR_INITIALIZER
, vrtem1
= VR_INITIALIZER
;
3076 /* VRP only operates on integral and pointer types. */
3077 if (!(INTEGRAL_TYPE_P (op0_type
)
3078 || POINTER_TYPE_P (op0_type
))
3079 || !(INTEGRAL_TYPE_P (type
)
3080 || POINTER_TYPE_P (type
)))
3082 set_value_range_to_varying (vr
);
3086 /* If VR0 is UNDEFINED, so is the result. */
3087 if (vr0
.type
== VR_UNDEFINED
)
3089 set_value_range_to_undefined (vr
);
3093 /* Handle operations that we express in terms of others. */
3094 if (code
== PAREN_EXPR
)
3096 /* PAREN_EXPR is a simple copy. */
3097 copy_value_range (vr
, &vr0
);
3100 else if (code
== NEGATE_EXPR
)
3102 /* -X is simply 0 - X, so re-use existing code that also handles
3103 anti-ranges fine. */
3104 value_range_t zero
= VR_INITIALIZER
;
3105 set_value_range_to_value (&zero
, build_int_cst (type
, 0), NULL
);
3106 extract_range_from_binary_expr_1 (vr
, MINUS_EXPR
, type
, &zero
, &vr0
);
3109 else if (code
== BIT_NOT_EXPR
)
3111 /* ~X is simply -1 - X, so re-use existing code that also handles
3112 anti-ranges fine. */
3113 value_range_t minusone
= VR_INITIALIZER
;
3114 set_value_range_to_value (&minusone
, build_int_cst (type
, -1), NULL
);
3115 extract_range_from_binary_expr_1 (vr
, MINUS_EXPR
,
3116 type
, &minusone
, &vr0
);
3120 /* Now canonicalize anti-ranges to ranges when they are not symbolic
3121 and express op ~[] as (op []') U (op []''). */
3122 if (vr0
.type
== VR_ANTI_RANGE
3123 && ranges_from_anti_range (&vr0
, &vrtem0
, &vrtem1
))
3125 extract_range_from_unary_expr_1 (vr
, code
, type
, &vrtem0
, op0_type
);
3126 if (vrtem1
.type
!= VR_UNDEFINED
)
3128 value_range_t vrres
= VR_INITIALIZER
;
3129 extract_range_from_unary_expr_1 (&vrres
, code
, type
,
3131 vrp_meet (vr
, &vrres
);
3136 if (CONVERT_EXPR_CODE_P (code
))
3138 tree inner_type
= op0_type
;
3139 tree outer_type
= type
;
3141 /* If the expression evaluates to a pointer, we are only interested in
3142 determining if it evaluates to NULL [0, 0] or non-NULL (~[0, 0]). */
3143 if (POINTER_TYPE_P (type
))
3145 if (range_is_nonnull (&vr0
))
3146 set_value_range_to_nonnull (vr
, type
);
3147 else if (range_is_null (&vr0
))
3148 set_value_range_to_null (vr
, type
);
3150 set_value_range_to_varying (vr
);
3154 /* If VR0 is varying and we increase the type precision, assume
3155 a full range for the following transformation. */
3156 if (vr0
.type
== VR_VARYING
3157 && INTEGRAL_TYPE_P (inner_type
)
3158 && TYPE_PRECISION (inner_type
) < TYPE_PRECISION (outer_type
))
3160 vr0
.type
= VR_RANGE
;
3161 vr0
.min
= TYPE_MIN_VALUE (inner_type
);
3162 vr0
.max
= TYPE_MAX_VALUE (inner_type
);
3165 /* If VR0 is a constant range or anti-range and the conversion is
3166 not truncating we can convert the min and max values and
3167 canonicalize the resulting range. Otherwise we can do the
3168 conversion if the size of the range is less than what the
3169 precision of the target type can represent and the range is
3170 not an anti-range. */
3171 if ((vr0
.type
== VR_RANGE
3172 || vr0
.type
== VR_ANTI_RANGE
)
3173 && TREE_CODE (vr0
.min
) == INTEGER_CST
3174 && TREE_CODE (vr0
.max
) == INTEGER_CST
3175 && (!is_overflow_infinity (vr0
.min
)
3176 || (vr0
.type
== VR_RANGE
3177 && TYPE_PRECISION (outer_type
) > TYPE_PRECISION (inner_type
)
3178 && needs_overflow_infinity (outer_type
)
3179 && supports_overflow_infinity (outer_type
)))
3180 && (!is_overflow_infinity (vr0
.max
)
3181 || (vr0
.type
== VR_RANGE
3182 && TYPE_PRECISION (outer_type
) > TYPE_PRECISION (inner_type
)
3183 && needs_overflow_infinity (outer_type
)
3184 && supports_overflow_infinity (outer_type
)))
3185 && (TYPE_PRECISION (outer_type
) >= TYPE_PRECISION (inner_type
)
3186 || (vr0
.type
== VR_RANGE
3187 && integer_zerop (int_const_binop (RSHIFT_EXPR
,
3188 int_const_binop (MINUS_EXPR
, vr0
.max
, vr0
.min
),
3189 size_int (TYPE_PRECISION (outer_type
)))))))
3191 tree new_min
, new_max
;
3192 if (is_overflow_infinity (vr0
.min
))
3193 new_min
= negative_overflow_infinity (outer_type
);
3195 new_min
= force_fit_type_double (outer_type
,
3196 tree_to_double_int (vr0
.min
),
3198 if (is_overflow_infinity (vr0
.max
))
3199 new_max
= positive_overflow_infinity (outer_type
);
3201 new_max
= force_fit_type_double (outer_type
,
3202 tree_to_double_int (vr0
.max
),
3204 set_and_canonicalize_value_range (vr
, vr0
.type
,
3205 new_min
, new_max
, NULL
);
3209 set_value_range_to_varying (vr
);
3212 else if (code
== ABS_EXPR
)
3217 /* Pass through vr0 in the easy cases. */
3218 if (TYPE_UNSIGNED (type
)
3219 || value_range_nonnegative_p (&vr0
))
3221 copy_value_range (vr
, &vr0
);
3225 /* For the remaining varying or symbolic ranges we can't do anything
3227 if (vr0
.type
== VR_VARYING
3228 || symbolic_range_p (&vr0
))
3230 set_value_range_to_varying (vr
);
3234 /* -TYPE_MIN_VALUE = TYPE_MIN_VALUE with flag_wrapv so we can't get a
3236 if (!TYPE_OVERFLOW_UNDEFINED (type
)
3237 && ((vr0
.type
== VR_RANGE
3238 && vrp_val_is_min (vr0
.min
))
3239 || (vr0
.type
== VR_ANTI_RANGE
3240 && !vrp_val_is_min (vr0
.min
))))
3242 set_value_range_to_varying (vr
);
3246 /* ABS_EXPR may flip the range around, if the original range
3247 included negative values. */
3248 if (is_overflow_infinity (vr0
.min
))
3249 min
= positive_overflow_infinity (type
);
3250 else if (!vrp_val_is_min (vr0
.min
))
3251 min
= fold_unary_to_constant (code
, type
, vr0
.min
);
3252 else if (!needs_overflow_infinity (type
))
3253 min
= TYPE_MAX_VALUE (type
);
3254 else if (supports_overflow_infinity (type
))
3255 min
= positive_overflow_infinity (type
);
3258 set_value_range_to_varying (vr
);
3262 if (is_overflow_infinity (vr0
.max
))
3263 max
= positive_overflow_infinity (type
);
3264 else if (!vrp_val_is_min (vr0
.max
))
3265 max
= fold_unary_to_constant (code
, type
, vr0
.max
);
3266 else if (!needs_overflow_infinity (type
))
3267 max
= TYPE_MAX_VALUE (type
);
3268 else if (supports_overflow_infinity (type
)
3269 /* We shouldn't generate [+INF, +INF] as set_value_range
3270 doesn't like this and ICEs. */
3271 && !is_positive_overflow_infinity (min
))
3272 max
= positive_overflow_infinity (type
);
3275 set_value_range_to_varying (vr
);
3279 cmp
= compare_values (min
, max
);
3281 /* If a VR_ANTI_RANGEs contains zero, then we have
3282 ~[-INF, min(MIN, MAX)]. */
3283 if (vr0
.type
== VR_ANTI_RANGE
)
3285 if (range_includes_zero_p (vr0
.min
, vr0
.max
) == 1)
3287 /* Take the lower of the two values. */
3291 /* Create ~[-INF, min (abs(MIN), abs(MAX))]
3292 or ~[-INF + 1, min (abs(MIN), abs(MAX))] when
3293 flag_wrapv is set and the original anti-range doesn't include
3294 TYPE_MIN_VALUE, remember -TYPE_MIN_VALUE = TYPE_MIN_VALUE. */
3295 if (TYPE_OVERFLOW_WRAPS (type
))
3297 tree type_min_value
= TYPE_MIN_VALUE (type
);
3299 min
= (vr0
.min
!= type_min_value
3300 ? int_const_binop (PLUS_EXPR
, type_min_value
,
3306 if (overflow_infinity_range_p (&vr0
))
3307 min
= negative_overflow_infinity (type
);
3309 min
= TYPE_MIN_VALUE (type
);
3314 /* All else has failed, so create the range [0, INF], even for
3315 flag_wrapv since TYPE_MIN_VALUE is in the original
3317 vr0
.type
= VR_RANGE
;
3318 min
= build_int_cst (type
, 0);
3319 if (needs_overflow_infinity (type
))
3321 if (supports_overflow_infinity (type
))
3322 max
= positive_overflow_infinity (type
);
3325 set_value_range_to_varying (vr
);
3330 max
= TYPE_MAX_VALUE (type
);
3334 /* If the range contains zero then we know that the minimum value in the
3335 range will be zero. */
3336 else if (range_includes_zero_p (vr0
.min
, vr0
.max
) == 1)
3340 min
= build_int_cst (type
, 0);
3344 /* If the range was reversed, swap MIN and MAX. */
3353 cmp
= compare_values (min
, max
);
3354 if (cmp
== -2 || cmp
== 1)
3356 /* If the new range has its limits swapped around (MIN > MAX),
3357 then the operation caused one of them to wrap around, mark
3358 the new range VARYING. */
3359 set_value_range_to_varying (vr
);
3362 set_value_range (vr
, vr0
.type
, min
, max
, NULL
);
3366 /* For unhandled operations fall back to varying. */
3367 set_value_range_to_varying (vr
);
3372 /* Extract range information from a unary expression CODE OP0 based on
3373 the range of its operand with resulting type TYPE.
3374 The resulting range is stored in *VR. */
3377 extract_range_from_unary_expr (value_range_t
*vr
, enum tree_code code
,
3378 tree type
, tree op0
)
3380 value_range_t vr0
= VR_INITIALIZER
;
3382 /* Get value ranges for the operand. For constant operands, create
3383 a new value range with the operand to simplify processing. */
3384 if (TREE_CODE (op0
) == SSA_NAME
)
3385 vr0
= *(get_value_range (op0
));
3386 else if (is_gimple_min_invariant (op0
))
3387 set_value_range_to_value (&vr0
, op0
, NULL
);
3389 set_value_range_to_varying (&vr0
);
3391 extract_range_from_unary_expr_1 (vr
, code
, type
, &vr0
, TREE_TYPE (op0
));
3395 /* Extract range information from a conditional expression STMT based on
3396 the ranges of each of its operands and the expression code. */
3399 extract_range_from_cond_expr (value_range_t
*vr
, gimple stmt
)
3402 value_range_t vr0
= VR_INITIALIZER
;
3403 value_range_t vr1
= VR_INITIALIZER
;
3405 /* Get value ranges for each operand. For constant operands, create
3406 a new value range with the operand to simplify processing. */
3407 op0
= gimple_assign_rhs2 (stmt
);
3408 if (TREE_CODE (op0
) == SSA_NAME
)
3409 vr0
= *(get_value_range (op0
));
3410 else if (is_gimple_min_invariant (op0
))
3411 set_value_range_to_value (&vr0
, op0
, NULL
);
3413 set_value_range_to_varying (&vr0
);
3415 op1
= gimple_assign_rhs3 (stmt
);
3416 if (TREE_CODE (op1
) == SSA_NAME
)
3417 vr1
= *(get_value_range (op1
));
3418 else if (is_gimple_min_invariant (op1
))
3419 set_value_range_to_value (&vr1
, op1
, NULL
);
3421 set_value_range_to_varying (&vr1
);
3423 /* The resulting value range is the union of the operand ranges */
3424 copy_value_range (vr
, &vr0
);
3425 vrp_meet (vr
, &vr1
);
3429 /* Extract range information from a comparison expression EXPR based
3430 on the range of its operand and the expression code. */
3433 extract_range_from_comparison (value_range_t
*vr
, enum tree_code code
,
3434 tree type
, tree op0
, tree op1
)
3439 val
= vrp_evaluate_conditional_warnv_with_ops (code
, op0
, op1
, false, &sop
,
3442 /* A disadvantage of using a special infinity as an overflow
3443 representation is that we lose the ability to record overflow
3444 when we don't have an infinity. So we have to ignore a result
3445 which relies on overflow. */
3447 if (val
&& !is_overflow_infinity (val
) && !sop
)
3449 /* Since this expression was found on the RHS of an assignment,
3450 its type may be different from _Bool. Convert VAL to EXPR's
3452 val
= fold_convert (type
, val
);
3453 if (is_gimple_min_invariant (val
))
3454 set_value_range_to_value (vr
, val
, vr
->equiv
);
3456 set_value_range (vr
, VR_RANGE
, val
, val
, vr
->equiv
);
3459 /* The result of a comparison is always true or false. */
3460 set_value_range_to_truthvalue (vr
, type
);
3463 /* Try to derive a nonnegative or nonzero range out of STMT relying
3464 primarily on generic routines in fold in conjunction with range data.
3465 Store the result in *VR */
3468 extract_range_basic (value_range_t
*vr
, gimple stmt
)
3471 tree type
= gimple_expr_type (stmt
);
3473 if (INTEGRAL_TYPE_P (type
)
3474 && gimple_stmt_nonnegative_warnv_p (stmt
, &sop
))
3475 set_value_range_to_nonnegative (vr
, type
,
3476 sop
|| stmt_overflow_infinity (stmt
));
3477 else if (vrp_stmt_computes_nonzero (stmt
, &sop
)
3479 set_value_range_to_nonnull (vr
, type
);
3481 set_value_range_to_varying (vr
);
3485 /* Try to compute a useful range out of assignment STMT and store it
3489 extract_range_from_assignment (value_range_t
*vr
, gimple stmt
)
3491 enum tree_code code
= gimple_assign_rhs_code (stmt
);
3493 if (code
== ASSERT_EXPR
)
3494 extract_range_from_assert (vr
, gimple_assign_rhs1 (stmt
));
3495 else if (code
== SSA_NAME
)
3496 extract_range_from_ssa_name (vr
, gimple_assign_rhs1 (stmt
));
3497 else if (TREE_CODE_CLASS (code
) == tcc_binary
)
3498 extract_range_from_binary_expr (vr
, gimple_assign_rhs_code (stmt
),
3499 gimple_expr_type (stmt
),
3500 gimple_assign_rhs1 (stmt
),
3501 gimple_assign_rhs2 (stmt
));
3502 else if (TREE_CODE_CLASS (code
) == tcc_unary
)
3503 extract_range_from_unary_expr (vr
, gimple_assign_rhs_code (stmt
),
3504 gimple_expr_type (stmt
),
3505 gimple_assign_rhs1 (stmt
));
3506 else if (code
== COND_EXPR
)
3507 extract_range_from_cond_expr (vr
, stmt
);
3508 else if (TREE_CODE_CLASS (code
) == tcc_comparison
)
3509 extract_range_from_comparison (vr
, gimple_assign_rhs_code (stmt
),
3510 gimple_expr_type (stmt
),
3511 gimple_assign_rhs1 (stmt
),
3512 gimple_assign_rhs2 (stmt
));
3513 else if (get_gimple_rhs_class (code
) == GIMPLE_SINGLE_RHS
3514 && is_gimple_min_invariant (gimple_assign_rhs1 (stmt
)))
3515 set_value_range_to_value (vr
, gimple_assign_rhs1 (stmt
), NULL
);
3517 set_value_range_to_varying (vr
);
3519 if (vr
->type
== VR_VARYING
)
3520 extract_range_basic (vr
, stmt
);
3523 /* Given a range VR, a LOOP and a variable VAR, determine whether it
3524 would be profitable to adjust VR using scalar evolution information
3525 for VAR. If so, update VR with the new limits. */
3528 adjust_range_with_scev (value_range_t
*vr
, struct loop
*loop
,
3529 gimple stmt
, tree var
)
3531 tree init
, step
, chrec
, tmin
, tmax
, min
, max
, type
, tem
;
3532 enum ev_direction dir
;
3534 /* TODO. Don't adjust anti-ranges. An anti-range may provide
3535 better opportunities than a regular range, but I'm not sure. */
3536 if (vr
->type
== VR_ANTI_RANGE
)
3539 chrec
= instantiate_parameters (loop
, analyze_scalar_evolution (loop
, var
));
3541 /* Like in PR19590, scev can return a constant function. */
3542 if (is_gimple_min_invariant (chrec
))
3544 set_value_range_to_value (vr
, chrec
, vr
->equiv
);
3548 if (TREE_CODE (chrec
) != POLYNOMIAL_CHREC
)
3551 init
= initial_condition_in_loop_num (chrec
, loop
->num
);
3552 tem
= op_with_constant_singleton_value_range (init
);
3555 step
= evolution_part_in_loop_num (chrec
, loop
->num
);
3556 tem
= op_with_constant_singleton_value_range (step
);
3560 /* If STEP is symbolic, we can't know whether INIT will be the
3561 minimum or maximum value in the range. Also, unless INIT is
3562 a simple expression, compare_values and possibly other functions
3563 in tree-vrp won't be able to handle it. */
3564 if (step
== NULL_TREE
3565 || !is_gimple_min_invariant (step
)
3566 || !valid_value_p (init
))
3569 dir
= scev_direction (chrec
);
3570 if (/* Do not adjust ranges if we do not know whether the iv increases
3571 or decreases, ... */
3572 dir
== EV_DIR_UNKNOWN
3573 /* ... or if it may wrap. */
3574 || scev_probably_wraps_p (init
, step
, stmt
, get_chrec_loop (chrec
),
3578 /* We use TYPE_MIN_VALUE and TYPE_MAX_VALUE here instead of
3579 negative_overflow_infinity and positive_overflow_infinity,
3580 because we have concluded that the loop probably does not
3583 type
= TREE_TYPE (var
);
3584 if (POINTER_TYPE_P (type
) || !TYPE_MIN_VALUE (type
))
3585 tmin
= lower_bound_in_type (type
, type
);
3587 tmin
= TYPE_MIN_VALUE (type
);
3588 if (POINTER_TYPE_P (type
) || !TYPE_MAX_VALUE (type
))
3589 tmax
= upper_bound_in_type (type
, type
);
3591 tmax
= TYPE_MAX_VALUE (type
);
3593 /* Try to use estimated number of iterations for the loop to constrain the
3594 final value in the evolution. */
3595 if (TREE_CODE (step
) == INTEGER_CST
3596 && is_gimple_val (init
)
3597 && (TREE_CODE (init
) != SSA_NAME
3598 || get_value_range (init
)->type
== VR_RANGE
))
3602 /* We are only entering here for loop header PHI nodes, so using
3603 the number of latch executions is the correct thing to use. */
3604 if (max_loop_iterations (loop
, &nit
))
3606 value_range_t maxvr
= VR_INITIALIZER
;
3608 bool unsigned_p
= TYPE_UNSIGNED (TREE_TYPE (step
));
3611 dtmp
= double_int_mul_with_sign (tree_to_double_int (step
), nit
,
3612 unsigned_p
, &overflow
);
3613 /* If the multiplication overflowed we can't do a meaningful
3614 adjustment. Likewise if the result doesn't fit in the type
3615 of the induction variable. For a signed type we have to
3616 check whether the result has the expected signedness which
3617 is that of the step as number of iterations is unsigned. */
3619 && double_int_fits_to_tree_p (TREE_TYPE (init
), dtmp
)
3621 || ((dtmp
.high
^ TREE_INT_CST_HIGH (step
)) >= 0)))
3623 tem
= double_int_to_tree (TREE_TYPE (init
), dtmp
);
3624 extract_range_from_binary_expr (&maxvr
, PLUS_EXPR
,
3625 TREE_TYPE (init
), init
, tem
);
3626 /* Likewise if the addition did. */
3627 if (maxvr
.type
== VR_RANGE
)
3636 if (vr
->type
== VR_VARYING
|| vr
->type
== VR_UNDEFINED
)
3641 /* For VARYING or UNDEFINED ranges, just about anything we get
3642 from scalar evolutions should be better. */
3644 if (dir
== EV_DIR_DECREASES
)
3649 /* If we would create an invalid range, then just assume we
3650 know absolutely nothing. This may be over-conservative,
3651 but it's clearly safe, and should happen only in unreachable
3652 parts of code, or for invalid programs. */
3653 if (compare_values (min
, max
) == 1)
3656 set_value_range (vr
, VR_RANGE
, min
, max
, vr
->equiv
);
3658 else if (vr
->type
== VR_RANGE
)
3663 if (dir
== EV_DIR_DECREASES
)
3665 /* INIT is the maximum value. If INIT is lower than VR->MAX
3666 but no smaller than VR->MIN, set VR->MAX to INIT. */
3667 if (compare_values (init
, max
) == -1)
3670 /* According to the loop information, the variable does not
3671 overflow. If we think it does, probably because of an
3672 overflow due to arithmetic on a different INF value,
3674 if (is_negative_overflow_infinity (min
)
3675 || compare_values (min
, tmin
) == -1)
3681 /* If INIT is bigger than VR->MIN, set VR->MIN to INIT. */
3682 if (compare_values (init
, min
) == 1)
3685 if (is_positive_overflow_infinity (max
)
3686 || compare_values (tmax
, max
) == -1)
3690 /* If we just created an invalid range with the minimum
3691 greater than the maximum, we fail conservatively.
3692 This should happen only in unreachable
3693 parts of code, or for invalid programs. */
3694 if (compare_values (min
, max
) == 1)
3697 set_value_range (vr
, VR_RANGE
, min
, max
, vr
->equiv
);
3701 /* Return true if VAR may overflow at STMT. This checks any available
3702 loop information to see if we can determine that VAR does not
3706 vrp_var_may_overflow (tree var
, gimple stmt
)
3709 tree chrec
, init
, step
;
3711 if (current_loops
== NULL
)
3714 l
= loop_containing_stmt (stmt
);
3719 chrec
= instantiate_parameters (l
, analyze_scalar_evolution (l
, var
));
3720 if (TREE_CODE (chrec
) != POLYNOMIAL_CHREC
)
3723 init
= initial_condition_in_loop_num (chrec
, l
->num
);
3724 step
= evolution_part_in_loop_num (chrec
, l
->num
);
3726 if (step
== NULL_TREE
3727 || !is_gimple_min_invariant (step
)
3728 || !valid_value_p (init
))
3731 /* If we get here, we know something useful about VAR based on the
3732 loop information. If it wraps, it may overflow. */
3734 if (scev_probably_wraps_p (init
, step
, stmt
, get_chrec_loop (chrec
),
3738 if (dump_file
&& (dump_flags
& TDF_DETAILS
) != 0)
3740 print_generic_expr (dump_file
, var
, 0);
3741 fprintf (dump_file
, ": loop information indicates does not overflow\n");
3748 /* Given two numeric value ranges VR0, VR1 and a comparison code COMP:
3750 - Return BOOLEAN_TRUE_NODE if VR0 COMP VR1 always returns true for
3751 all the values in the ranges.
3753 - Return BOOLEAN_FALSE_NODE if the comparison always returns false.
3755 - Return NULL_TREE if it is not always possible to determine the
3756 value of the comparison.
3758 Also set *STRICT_OVERFLOW_P to indicate whether a range with an
3759 overflow infinity was used in the test. */
3763 compare_ranges (enum tree_code comp
, value_range_t
*vr0
, value_range_t
*vr1
,
3764 bool *strict_overflow_p
)
3766 /* VARYING or UNDEFINED ranges cannot be compared. */
3767 if (vr0
->type
== VR_VARYING
3768 || vr0
->type
== VR_UNDEFINED
3769 || vr1
->type
== VR_VARYING
3770 || vr1
->type
== VR_UNDEFINED
)
3773 /* Anti-ranges need to be handled separately. */
3774 if (vr0
->type
== VR_ANTI_RANGE
|| vr1
->type
== VR_ANTI_RANGE
)
3776 /* If both are anti-ranges, then we cannot compute any
3778 if (vr0
->type
== VR_ANTI_RANGE
&& vr1
->type
== VR_ANTI_RANGE
)
3781 /* These comparisons are never statically computable. */
3788 /* Equality can be computed only between a range and an
3789 anti-range. ~[VAL1, VAL2] == [VAL1, VAL2] is always false. */
3790 if (vr0
->type
== VR_RANGE
)
3792 /* To simplify processing, make VR0 the anti-range. */
3793 value_range_t
*tmp
= vr0
;
3798 gcc_assert (comp
== NE_EXPR
|| comp
== EQ_EXPR
);
3800 if (compare_values_warnv (vr0
->min
, vr1
->min
, strict_overflow_p
) == 0
3801 && compare_values_warnv (vr0
->max
, vr1
->max
, strict_overflow_p
) == 0)
3802 return (comp
== NE_EXPR
) ? boolean_true_node
: boolean_false_node
;
3807 if (!usable_range_p (vr0
, strict_overflow_p
)
3808 || !usable_range_p (vr1
, strict_overflow_p
))
3811 /* Simplify processing. If COMP is GT_EXPR or GE_EXPR, switch the
3812 operands around and change the comparison code. */
3813 if (comp
== GT_EXPR
|| comp
== GE_EXPR
)
3816 comp
= (comp
== GT_EXPR
) ? LT_EXPR
: LE_EXPR
;
3822 if (comp
== EQ_EXPR
)
3824 /* Equality may only be computed if both ranges represent
3825 exactly one value. */
3826 if (compare_values_warnv (vr0
->min
, vr0
->max
, strict_overflow_p
) == 0
3827 && compare_values_warnv (vr1
->min
, vr1
->max
, strict_overflow_p
) == 0)
3829 int cmp_min
= compare_values_warnv (vr0
->min
, vr1
->min
,
3831 int cmp_max
= compare_values_warnv (vr0
->max
, vr1
->max
,
3833 if (cmp_min
== 0 && cmp_max
== 0)
3834 return boolean_true_node
;
3835 else if (cmp_min
!= -2 && cmp_max
!= -2)
3836 return boolean_false_node
;
3838 /* If [V0_MIN, V1_MAX] < [V1_MIN, V1_MAX] then V0 != V1. */
3839 else if (compare_values_warnv (vr0
->min
, vr1
->max
,
3840 strict_overflow_p
) == 1
3841 || compare_values_warnv (vr1
->min
, vr0
->max
,
3842 strict_overflow_p
) == 1)
3843 return boolean_false_node
;
3847 else if (comp
== NE_EXPR
)
3851 /* If VR0 is completely to the left or completely to the right
3852 of VR1, they are always different. Notice that we need to
3853 make sure that both comparisons yield similar results to
3854 avoid comparing values that cannot be compared at
3856 cmp1
= compare_values_warnv (vr0
->max
, vr1
->min
, strict_overflow_p
);
3857 cmp2
= compare_values_warnv (vr0
->min
, vr1
->max
, strict_overflow_p
);
3858 if ((cmp1
== -1 && cmp2
== -1) || (cmp1
== 1 && cmp2
== 1))
3859 return boolean_true_node
;
3861 /* If VR0 and VR1 represent a single value and are identical,
3863 else if (compare_values_warnv (vr0
->min
, vr0
->max
,
3864 strict_overflow_p
) == 0
3865 && compare_values_warnv (vr1
->min
, vr1
->max
,
3866 strict_overflow_p
) == 0
3867 && compare_values_warnv (vr0
->min
, vr1
->min
,
3868 strict_overflow_p
) == 0
3869 && compare_values_warnv (vr0
->max
, vr1
->max
,
3870 strict_overflow_p
) == 0)
3871 return boolean_false_node
;
3873 /* Otherwise, they may or may not be different. */
3877 else if (comp
== LT_EXPR
|| comp
== LE_EXPR
)
3881 /* If VR0 is to the left of VR1, return true. */
3882 tst
= compare_values_warnv (vr0
->max
, vr1
->min
, strict_overflow_p
);
3883 if ((comp
== LT_EXPR
&& tst
== -1)
3884 || (comp
== LE_EXPR
&& (tst
== -1 || tst
== 0)))
3886 if (overflow_infinity_range_p (vr0
)
3887 || overflow_infinity_range_p (vr1
))
3888 *strict_overflow_p
= true;
3889 return boolean_true_node
;
3892 /* If VR0 is to the right of VR1, return false. */
3893 tst
= compare_values_warnv (vr0
->min
, vr1
->max
, strict_overflow_p
);
3894 if ((comp
== LT_EXPR
&& (tst
== 0 || tst
== 1))
3895 || (comp
== LE_EXPR
&& tst
== 1))
3897 if (overflow_infinity_range_p (vr0
)
3898 || overflow_infinity_range_p (vr1
))
3899 *strict_overflow_p
= true;
3900 return boolean_false_node
;
3903 /* Otherwise, we don't know. */
3911 /* Given a value range VR, a value VAL and a comparison code COMP, return
3912 BOOLEAN_TRUE_NODE if VR COMP VAL always returns true for all the
3913 values in VR. Return BOOLEAN_FALSE_NODE if the comparison
3914 always returns false. Return NULL_TREE if it is not always
3915 possible to determine the value of the comparison. Also set
3916 *STRICT_OVERFLOW_P to indicate whether a range with an overflow
3917 infinity was used in the test. */
3920 compare_range_with_value (enum tree_code comp
, value_range_t
*vr
, tree val
,
3921 bool *strict_overflow_p
)
3923 if (vr
->type
== VR_VARYING
|| vr
->type
== VR_UNDEFINED
)
3926 /* Anti-ranges need to be handled separately. */
3927 if (vr
->type
== VR_ANTI_RANGE
)
3929 /* For anti-ranges, the only predicates that we can compute at
3930 compile time are equality and inequality. */
3937 /* ~[VAL_1, VAL_2] OP VAL is known if VAL_1 <= VAL <= VAL_2. */
3938 if (value_inside_range (val
, vr
->min
, vr
->max
) == 1)
3939 return (comp
== NE_EXPR
) ? boolean_true_node
: boolean_false_node
;
3944 if (!usable_range_p (vr
, strict_overflow_p
))
3947 if (comp
== EQ_EXPR
)
3949 /* EQ_EXPR may only be computed if VR represents exactly
3951 if (compare_values_warnv (vr
->min
, vr
->max
, strict_overflow_p
) == 0)
3953 int cmp
= compare_values_warnv (vr
->min
, val
, strict_overflow_p
);
3955 return boolean_true_node
;
3956 else if (cmp
== -1 || cmp
== 1 || cmp
== 2)
3957 return boolean_false_node
;
3959 else if (compare_values_warnv (val
, vr
->min
, strict_overflow_p
) == -1
3960 || compare_values_warnv (vr
->max
, val
, strict_overflow_p
) == -1)
3961 return boolean_false_node
;
3965 else if (comp
== NE_EXPR
)
3967 /* If VAL is not inside VR, then they are always different. */
3968 if (compare_values_warnv (vr
->max
, val
, strict_overflow_p
) == -1
3969 || compare_values_warnv (vr
->min
, val
, strict_overflow_p
) == 1)
3970 return boolean_true_node
;
3972 /* If VR represents exactly one value equal to VAL, then return
3974 if (compare_values_warnv (vr
->min
, vr
->max
, strict_overflow_p
) == 0
3975 && compare_values_warnv (vr
->min
, val
, strict_overflow_p
) == 0)
3976 return boolean_false_node
;
3978 /* Otherwise, they may or may not be different. */
3981 else if (comp
== LT_EXPR
|| comp
== LE_EXPR
)
3985 /* If VR is to the left of VAL, return true. */
3986 tst
= compare_values_warnv (vr
->max
, val
, strict_overflow_p
);
3987 if ((comp
== LT_EXPR
&& tst
== -1)
3988 || (comp
== LE_EXPR
&& (tst
== -1 || tst
== 0)))
3990 if (overflow_infinity_range_p (vr
))
3991 *strict_overflow_p
= true;
3992 return boolean_true_node
;
3995 /* If VR is to the right of VAL, return false. */
3996 tst
= compare_values_warnv (vr
->min
, val
, strict_overflow_p
);
3997 if ((comp
== LT_EXPR
&& (tst
== 0 || tst
== 1))
3998 || (comp
== LE_EXPR
&& tst
== 1))
4000 if (overflow_infinity_range_p (vr
))
4001 *strict_overflow_p
= true;
4002 return boolean_false_node
;
4005 /* Otherwise, we don't know. */
4008 else if (comp
== GT_EXPR
|| comp
== GE_EXPR
)
4012 /* If VR is to the right of VAL, return true. */
4013 tst
= compare_values_warnv (vr
->min
, val
, strict_overflow_p
);
4014 if ((comp
== GT_EXPR
&& tst
== 1)
4015 || (comp
== GE_EXPR
&& (tst
== 0 || tst
== 1)))
4017 if (overflow_infinity_range_p (vr
))
4018 *strict_overflow_p
= true;
4019 return boolean_true_node
;
4022 /* If VR is to the left of VAL, return false. */
4023 tst
= compare_values_warnv (vr
->max
, val
, strict_overflow_p
);
4024 if ((comp
== GT_EXPR
&& (tst
== -1 || tst
== 0))
4025 || (comp
== GE_EXPR
&& tst
== -1))
4027 if (overflow_infinity_range_p (vr
))
4028 *strict_overflow_p
= true;
4029 return boolean_false_node
;
4032 /* Otherwise, we don't know. */
4040 /* Debugging dumps. */
4042 void dump_value_range (FILE *, value_range_t
*);
4043 void debug_value_range (value_range_t
*);
4044 void dump_all_value_ranges (FILE *);
4045 void debug_all_value_ranges (void);
4046 void dump_vr_equiv (FILE *, bitmap
);
4047 void debug_vr_equiv (bitmap
);
4050 /* Dump value range VR to FILE. */
4053 dump_value_range (FILE *file
, value_range_t
*vr
)
4056 fprintf (file
, "[]");
4057 else if (vr
->type
== VR_UNDEFINED
)
4058 fprintf (file
, "UNDEFINED");
4059 else if (vr
->type
== VR_RANGE
|| vr
->type
== VR_ANTI_RANGE
)
4061 tree type
= TREE_TYPE (vr
->min
);
4063 fprintf (file
, "%s[", (vr
->type
== VR_ANTI_RANGE
) ? "~" : "");
4065 if (is_negative_overflow_infinity (vr
->min
))
4066 fprintf (file
, "-INF(OVF)");
4067 else if (INTEGRAL_TYPE_P (type
)
4068 && !TYPE_UNSIGNED (type
)
4069 && vrp_val_is_min (vr
->min
))
4070 fprintf (file
, "-INF");
4072 print_generic_expr (file
, vr
->min
, 0);
4074 fprintf (file
, ", ");
4076 if (is_positive_overflow_infinity (vr
->max
))
4077 fprintf (file
, "+INF(OVF)");
4078 else if (INTEGRAL_TYPE_P (type
)
4079 && vrp_val_is_max (vr
->max
))
4080 fprintf (file
, "+INF");
4082 print_generic_expr (file
, vr
->max
, 0);
4084 fprintf (file
, "]");
4091 fprintf (file
, " EQUIVALENCES: { ");
4093 EXECUTE_IF_SET_IN_BITMAP (vr
->equiv
, 0, i
, bi
)
4095 print_generic_expr (file
, ssa_name (i
), 0);
4096 fprintf (file
, " ");
4100 fprintf (file
, "} (%u elements)", c
);
4103 else if (vr
->type
== VR_VARYING
)
4104 fprintf (file
, "VARYING");
4106 fprintf (file
, "INVALID RANGE");
4110 /* Dump value range VR to stderr. */
4113 debug_value_range (value_range_t
*vr
)
4115 dump_value_range (stderr
, vr
);
4116 fprintf (stderr
, "\n");
4120 /* Dump value ranges of all SSA_NAMEs to FILE. */
4123 dump_all_value_ranges (FILE *file
)
4127 for (i
= 0; i
< num_vr_values
; i
++)
4131 print_generic_expr (file
, ssa_name (i
), 0);
4132 fprintf (file
, ": ");
4133 dump_value_range (file
, vr_value
[i
]);
4134 fprintf (file
, "\n");
4138 fprintf (file
, "\n");
4142 /* Dump all value ranges to stderr. */
4145 debug_all_value_ranges (void)
4147 dump_all_value_ranges (stderr
);
4151 /* Given a COND_EXPR COND of the form 'V OP W', and an SSA name V,
4152 create a new SSA name N and return the assertion assignment
4153 'V = ASSERT_EXPR <V, V OP W>'. */
4156 build_assert_expr_for (tree cond
, tree v
)
4161 gcc_assert (TREE_CODE (v
) == SSA_NAME
4162 && COMPARISON_CLASS_P (cond
));
4164 a
= build2 (ASSERT_EXPR
, TREE_TYPE (v
), v
, cond
);
4165 assertion
= gimple_build_assign (NULL_TREE
, a
);
4167 /* The new ASSERT_EXPR, creates a new SSA name that replaces the
4168 operand of the ASSERT_EXPR. Create it so the new name and the old one
4169 are registered in the replacement table so that we can fix the SSA web
4170 after adding all the ASSERT_EXPRs. */
4171 create_new_def_for (v
, assertion
, NULL
);
4177 /* Return false if EXPR is a predicate expression involving floating
4181 fp_predicate (gimple stmt
)
4183 GIMPLE_CHECK (stmt
, GIMPLE_COND
);
4185 return FLOAT_TYPE_P (TREE_TYPE (gimple_cond_lhs (stmt
)));
4189 /* If the range of values taken by OP can be inferred after STMT executes,
4190 return the comparison code (COMP_CODE_P) and value (VAL_P) that
4191 describes the inferred range. Return true if a range could be
4195 infer_value_range (gimple stmt
, tree op
, enum tree_code
*comp_code_p
, tree
*val_p
)
4198 *comp_code_p
= ERROR_MARK
;
4200 /* Do not attempt to infer anything in names that flow through
4202 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op
))
4205 /* Similarly, don't infer anything from statements that may throw
4207 if (stmt_could_throw_p (stmt
))
4210 /* If STMT is the last statement of a basic block with no
4211 successors, there is no point inferring anything about any of its
4212 operands. We would not be able to find a proper insertion point
4213 for the assertion, anyway. */
4214 if (stmt_ends_bb_p (stmt
) && EDGE_COUNT (gimple_bb (stmt
)->succs
) == 0)
4217 /* We can only assume that a pointer dereference will yield
4218 non-NULL if -fdelete-null-pointer-checks is enabled. */
4219 if (flag_delete_null_pointer_checks
4220 && POINTER_TYPE_P (TREE_TYPE (op
))
4221 && gimple_code (stmt
) != GIMPLE_ASM
)
4223 unsigned num_uses
, num_loads
, num_stores
;
4225 count_uses_and_derefs (op
, stmt
, &num_uses
, &num_loads
, &num_stores
);
4226 if (num_loads
+ num_stores
> 0)
4228 *val_p
= build_int_cst (TREE_TYPE (op
), 0);
4229 *comp_code_p
= NE_EXPR
;
4238 void dump_asserts_for (FILE *, tree
);
4239 void debug_asserts_for (tree
);
4240 void dump_all_asserts (FILE *);
4241 void debug_all_asserts (void);
4243 /* Dump all the registered assertions for NAME to FILE. */
4246 dump_asserts_for (FILE *file
, tree name
)
4250 fprintf (file
, "Assertions to be inserted for ");
4251 print_generic_expr (file
, name
, 0);
4252 fprintf (file
, "\n");
4254 loc
= asserts_for
[SSA_NAME_VERSION (name
)];
4257 fprintf (file
, "\t");
4258 print_gimple_stmt (file
, gsi_stmt (loc
->si
), 0, 0);
4259 fprintf (file
, "\n\tBB #%d", loc
->bb
->index
);
4262 fprintf (file
, "\n\tEDGE %d->%d", loc
->e
->src
->index
,
4263 loc
->e
->dest
->index
);
4264 dump_edge_info (file
, loc
->e
, dump_flags
, 0);
4266 fprintf (file
, "\n\tPREDICATE: ");
4267 print_generic_expr (file
, name
, 0);
4268 fprintf (file
, " %s ", tree_code_name
[(int)loc
->comp_code
]);
4269 print_generic_expr (file
, loc
->val
, 0);
4270 fprintf (file
, "\n\n");
4274 fprintf (file
, "\n");
4278 /* Dump all the registered assertions for NAME to stderr. */
4281 debug_asserts_for (tree name
)
4283 dump_asserts_for (stderr
, name
);
4287 /* Dump all the registered assertions for all the names to FILE. */
4290 dump_all_asserts (FILE *file
)
4295 fprintf (file
, "\nASSERT_EXPRs to be inserted\n\n");
4296 EXECUTE_IF_SET_IN_BITMAP (need_assert_for
, 0, i
, bi
)
4297 dump_asserts_for (file
, ssa_name (i
));
4298 fprintf (file
, "\n");
4302 /* Dump all the registered assertions for all the names to stderr. */
4305 debug_all_asserts (void)
4307 dump_all_asserts (stderr
);
4311 /* If NAME doesn't have an ASSERT_EXPR registered for asserting
4312 'EXPR COMP_CODE VAL' at a location that dominates block BB or
4313 E->DEST, then register this location as a possible insertion point
4314 for ASSERT_EXPR <NAME, EXPR COMP_CODE VAL>.
4316 BB, E and SI provide the exact insertion point for the new
4317 ASSERT_EXPR. If BB is NULL, then the ASSERT_EXPR is to be inserted
4318 on edge E. Otherwise, if E is NULL, the ASSERT_EXPR is inserted on
4319 BB. If SI points to a COND_EXPR or a SWITCH_EXPR statement, then E
4320 must not be NULL. */
4323 register_new_assert_for (tree name
, tree expr
,
4324 enum tree_code comp_code
,
4328 gimple_stmt_iterator si
)
4330 assert_locus_t n
, loc
, last_loc
;
4331 basic_block dest_bb
;
4333 gcc_checking_assert (bb
== NULL
|| e
== NULL
);
4336 gcc_checking_assert (gimple_code (gsi_stmt (si
)) != GIMPLE_COND
4337 && gimple_code (gsi_stmt (si
)) != GIMPLE_SWITCH
);
4339 /* Never build an assert comparing against an integer constant with
4340 TREE_OVERFLOW set. This confuses our undefined overflow warning
4342 if (TREE_CODE (val
) == INTEGER_CST
4343 && TREE_OVERFLOW (val
))
4344 val
= build_int_cst_wide (TREE_TYPE (val
),
4345 TREE_INT_CST_LOW (val
), TREE_INT_CST_HIGH (val
));
4347 /* The new assertion A will be inserted at BB or E. We need to
4348 determine if the new location is dominated by a previously
4349 registered location for A. If we are doing an edge insertion,
4350 assume that A will be inserted at E->DEST. Note that this is not
4353 If E is a critical edge, it will be split. But even if E is
4354 split, the new block will dominate the same set of blocks that
4357 The reverse, however, is not true, blocks dominated by E->DEST
4358 will not be dominated by the new block created to split E. So,
4359 if the insertion location is on a critical edge, we will not use
4360 the new location to move another assertion previously registered
4361 at a block dominated by E->DEST. */
4362 dest_bb
= (bb
) ? bb
: e
->dest
;
4364 /* If NAME already has an ASSERT_EXPR registered for COMP_CODE and
4365 VAL at a block dominating DEST_BB, then we don't need to insert a new
4366 one. Similarly, if the same assertion already exists at a block
4367 dominated by DEST_BB and the new location is not on a critical
4368 edge, then update the existing location for the assertion (i.e.,
4369 move the assertion up in the dominance tree).
4371 Note, this is implemented as a simple linked list because there
4372 should not be more than a handful of assertions registered per
4373 name. If this becomes a performance problem, a table hashed by
4374 COMP_CODE and VAL could be implemented. */
4375 loc
= asserts_for
[SSA_NAME_VERSION (name
)];
4379 if (loc
->comp_code
== comp_code
4381 || operand_equal_p (loc
->val
, val
, 0))
4382 && (loc
->expr
== expr
4383 || operand_equal_p (loc
->expr
, expr
, 0)))
4385 /* If the assertion NAME COMP_CODE VAL has already been
4386 registered at a basic block that dominates DEST_BB, then
4387 we don't need to insert the same assertion again. Note
4388 that we don't check strict dominance here to avoid
4389 replicating the same assertion inside the same basic
4390 block more than once (e.g., when a pointer is
4391 dereferenced several times inside a block).
4393 An exception to this rule are edge insertions. If the
4394 new assertion is to be inserted on edge E, then it will
4395 dominate all the other insertions that we may want to
4396 insert in DEST_BB. So, if we are doing an edge
4397 insertion, don't do this dominance check. */
4399 && dominated_by_p (CDI_DOMINATORS
, dest_bb
, loc
->bb
))
4402 /* Otherwise, if E is not a critical edge and DEST_BB
4403 dominates the existing location for the assertion, move
4404 the assertion up in the dominance tree by updating its
4405 location information. */
4406 if ((e
== NULL
|| !EDGE_CRITICAL_P (e
))
4407 && dominated_by_p (CDI_DOMINATORS
, loc
->bb
, dest_bb
))
4416 /* Update the last node of the list and move to the next one. */
4421 /* If we didn't find an assertion already registered for
4422 NAME COMP_CODE VAL, add a new one at the end of the list of
4423 assertions associated with NAME. */
4424 n
= XNEW (struct assert_locus_d
);
4428 n
->comp_code
= comp_code
;
4436 asserts_for
[SSA_NAME_VERSION (name
)] = n
;
4438 bitmap_set_bit (need_assert_for
, SSA_NAME_VERSION (name
));
4441 /* (COND_OP0 COND_CODE COND_OP1) is a predicate which uses NAME.
4442 Extract a suitable test code and value and store them into *CODE_P and
4443 *VAL_P so the predicate is normalized to NAME *CODE_P *VAL_P.
4445 If no extraction was possible, return FALSE, otherwise return TRUE.
4447 If INVERT is true, then we invert the result stored into *CODE_P. */
4450 extract_code_and_val_from_cond_with_ops (tree name
, enum tree_code cond_code
,
4451 tree cond_op0
, tree cond_op1
,
4452 bool invert
, enum tree_code
*code_p
,
4455 enum tree_code comp_code
;
4458 /* Otherwise, we have a comparison of the form NAME COMP VAL
4459 or VAL COMP NAME. */
4460 if (name
== cond_op1
)
4462 /* If the predicate is of the form VAL COMP NAME, flip
4463 COMP around because we need to register NAME as the
4464 first operand in the predicate. */
4465 comp_code
= swap_tree_comparison (cond_code
);
4470 /* The comparison is of the form NAME COMP VAL, so the
4471 comparison code remains unchanged. */
4472 comp_code
= cond_code
;
4476 /* Invert the comparison code as necessary. */
4478 comp_code
= invert_tree_comparison (comp_code
, 0);
4480 /* VRP does not handle float types. */
4481 if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (val
)))
4484 /* Do not register always-false predicates.
4485 FIXME: this works around a limitation in fold() when dealing with
4486 enumerations. Given 'enum { N1, N2 } x;', fold will not
4487 fold 'if (x > N2)' to 'if (0)'. */
4488 if ((comp_code
== GT_EXPR
|| comp_code
== LT_EXPR
)
4489 && INTEGRAL_TYPE_P (TREE_TYPE (val
)))
4491 tree min
= TYPE_MIN_VALUE (TREE_TYPE (val
));
4492 tree max
= TYPE_MAX_VALUE (TREE_TYPE (val
));
4494 if (comp_code
== GT_EXPR
4496 || compare_values (val
, max
) == 0))
4499 if (comp_code
== LT_EXPR
4501 || compare_values (val
, min
) == 0))
4504 *code_p
= comp_code
;
4509 /* Find out smallest RES where RES > VAL && (RES & MASK) == RES, if any
4510 (otherwise return VAL). VAL and MASK must be zero-extended for
4511 precision PREC. If SGNBIT is non-zero, first xor VAL with SGNBIT
4512 (to transform signed values into unsigned) and at the end xor
4516 masked_increment (double_int val
, double_int mask
, double_int sgnbit
,
4519 double_int bit
= double_int_one
, res
;
4522 val
= double_int_xor (val
, sgnbit
);
4523 for (i
= 0; i
< prec
; i
++, bit
= double_int_add (bit
, bit
))
4526 if (double_int_zero_p (double_int_and (res
, bit
)))
4528 res
= double_int_sub (bit
, double_int_one
);
4529 res
= double_int_and_not (double_int_add (val
, bit
), res
);
4530 res
= double_int_and (res
, mask
);
4531 if (double_int_ucmp (res
, val
) > 0)
4532 return double_int_xor (res
, sgnbit
);
4534 return double_int_xor (val
, sgnbit
);
4537 /* Try to register an edge assertion for SSA name NAME on edge E for
4538 the condition COND contributing to the conditional jump pointed to by BSI.
4539 Invert the condition COND if INVERT is true.
4540 Return true if an assertion for NAME could be registered. */
4543 register_edge_assert_for_2 (tree name
, edge e
, gimple_stmt_iterator bsi
,
4544 enum tree_code cond_code
,
4545 tree cond_op0
, tree cond_op1
, bool invert
)
4548 enum tree_code comp_code
;
4549 bool retval
= false;
4551 if (!extract_code_and_val_from_cond_with_ops (name
, cond_code
,
4554 invert
, &comp_code
, &val
))
4557 /* Only register an ASSERT_EXPR if NAME was found in the sub-graph
4558 reachable from E. */
4559 if (live_on_edge (e
, name
)
4560 && !has_single_use (name
))
4562 register_new_assert_for (name
, name
, comp_code
, val
, NULL
, e
, bsi
);
4566 /* In the case of NAME <= CST and NAME being defined as
4567 NAME = (unsigned) NAME2 + CST2 we can assert NAME2 >= -CST2
4568 and NAME2 <= CST - CST2. We can do the same for NAME > CST.
4569 This catches range and anti-range tests. */
4570 if ((comp_code
== LE_EXPR
4571 || comp_code
== GT_EXPR
)
4572 && TREE_CODE (val
) == INTEGER_CST
4573 && TYPE_UNSIGNED (TREE_TYPE (val
)))
4575 gimple def_stmt
= SSA_NAME_DEF_STMT (name
);
4576 tree cst2
= NULL_TREE
, name2
= NULL_TREE
, name3
= NULL_TREE
;
4578 /* Extract CST2 from the (optional) addition. */
4579 if (is_gimple_assign (def_stmt
)
4580 && gimple_assign_rhs_code (def_stmt
) == PLUS_EXPR
)
4582 name2
= gimple_assign_rhs1 (def_stmt
);
4583 cst2
= gimple_assign_rhs2 (def_stmt
);
4584 if (TREE_CODE (name2
) == SSA_NAME
4585 && TREE_CODE (cst2
) == INTEGER_CST
)
4586 def_stmt
= SSA_NAME_DEF_STMT (name2
);
4589 /* Extract NAME2 from the (optional) sign-changing cast. */
4590 if (gimple_assign_cast_p (def_stmt
))
4592 if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt
))
4593 && ! TYPE_UNSIGNED (TREE_TYPE (gimple_assign_rhs1 (def_stmt
)))
4594 && (TYPE_PRECISION (gimple_expr_type (def_stmt
))
4595 == TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (def_stmt
)))))
4596 name3
= gimple_assign_rhs1 (def_stmt
);
4599 /* If name3 is used later, create an ASSERT_EXPR for it. */
4600 if (name3
!= NULL_TREE
4601 && TREE_CODE (name3
) == SSA_NAME
4602 && (cst2
== NULL_TREE
4603 || TREE_CODE (cst2
) == INTEGER_CST
)
4604 && INTEGRAL_TYPE_P (TREE_TYPE (name3
))
4605 && live_on_edge (e
, name3
)
4606 && !has_single_use (name3
))
4610 /* Build an expression for the range test. */
4611 tmp
= build1 (NOP_EXPR
, TREE_TYPE (name
), name3
);
4612 if (cst2
!= NULL_TREE
)
4613 tmp
= build2 (PLUS_EXPR
, TREE_TYPE (name
), tmp
, cst2
);
4617 fprintf (dump_file
, "Adding assert for ");
4618 print_generic_expr (dump_file
, name3
, 0);
4619 fprintf (dump_file
, " from ");
4620 print_generic_expr (dump_file
, tmp
, 0);
4621 fprintf (dump_file
, "\n");
4624 register_new_assert_for (name3
, tmp
, comp_code
, val
, NULL
, e
, bsi
);
4629 /* If name2 is used later, create an ASSERT_EXPR for it. */
4630 if (name2
!= NULL_TREE
4631 && TREE_CODE (name2
) == SSA_NAME
4632 && TREE_CODE (cst2
) == INTEGER_CST
4633 && INTEGRAL_TYPE_P (TREE_TYPE (name2
))
4634 && live_on_edge (e
, name2
)
4635 && !has_single_use (name2
))
4639 /* Build an expression for the range test. */
4641 if (TREE_TYPE (name
) != TREE_TYPE (name2
))
4642 tmp
= build1 (NOP_EXPR
, TREE_TYPE (name
), tmp
);
4643 if (cst2
!= NULL_TREE
)
4644 tmp
= build2 (PLUS_EXPR
, TREE_TYPE (name
), tmp
, cst2
);
4648 fprintf (dump_file
, "Adding assert for ");
4649 print_generic_expr (dump_file
, name2
, 0);
4650 fprintf (dump_file
, " from ");
4651 print_generic_expr (dump_file
, tmp
, 0);
4652 fprintf (dump_file
, "\n");
4655 register_new_assert_for (name2
, tmp
, comp_code
, val
, NULL
, e
, bsi
);
4661 if (TREE_CODE_CLASS (comp_code
) == tcc_comparison
4662 && TREE_CODE (val
) == INTEGER_CST
)
4664 gimple def_stmt
= SSA_NAME_DEF_STMT (name
);
4665 tree name2
= NULL_TREE
, names
[2], cst2
= NULL_TREE
;
4666 tree val2
= NULL_TREE
;
4667 double_int mask
= double_int_zero
;
4668 unsigned int prec
= TYPE_PRECISION (TREE_TYPE (val
));
4670 /* Add asserts for NAME cmp CST and NAME being defined
4671 as NAME = (int) NAME2. */
4672 if (!TYPE_UNSIGNED (TREE_TYPE (val
))
4673 && (comp_code
== LE_EXPR
|| comp_code
== LT_EXPR
4674 || comp_code
== GT_EXPR
|| comp_code
== GE_EXPR
)
4675 && gimple_assign_cast_p (def_stmt
))
4677 name2
= gimple_assign_rhs1 (def_stmt
);
4678 if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt
))
4679 && INTEGRAL_TYPE_P (TREE_TYPE (name2
))
4680 && TYPE_UNSIGNED (TREE_TYPE (name2
))
4681 && prec
== TYPE_PRECISION (TREE_TYPE (name2
))
4682 && (comp_code
== LE_EXPR
|| comp_code
== GT_EXPR
4683 || !tree_int_cst_equal (val
,
4684 TYPE_MIN_VALUE (TREE_TYPE (val
))))
4685 && live_on_edge (e
, name2
)
4686 && !has_single_use (name2
))
4689 enum tree_code new_comp_code
= comp_code
;
4691 cst
= fold_convert (TREE_TYPE (name2
),
4692 TYPE_MIN_VALUE (TREE_TYPE (val
)));
4693 /* Build an expression for the range test. */
4694 tmp
= build2 (PLUS_EXPR
, TREE_TYPE (name2
), name2
, cst
);
4695 cst
= fold_build2 (PLUS_EXPR
, TREE_TYPE (name2
), cst
,
4696 fold_convert (TREE_TYPE (name2
), val
));
4697 if (comp_code
== LT_EXPR
|| comp_code
== GE_EXPR
)
4699 new_comp_code
= comp_code
== LT_EXPR
? LE_EXPR
: GT_EXPR
;
4700 cst
= fold_build2 (MINUS_EXPR
, TREE_TYPE (name2
), cst
,
4701 build_int_cst (TREE_TYPE (name2
), 1));
4706 fprintf (dump_file
, "Adding assert for ");
4707 print_generic_expr (dump_file
, name2
, 0);
4708 fprintf (dump_file
, " from ");
4709 print_generic_expr (dump_file
, tmp
, 0);
4710 fprintf (dump_file
, "\n");
4713 register_new_assert_for (name2
, tmp
, new_comp_code
, cst
, NULL
,
4720 /* Add asserts for NAME cmp CST and NAME being defined as
4721 NAME = NAME2 >> CST2.
4723 Extract CST2 from the right shift. */
4724 if (is_gimple_assign (def_stmt
)
4725 && gimple_assign_rhs_code (def_stmt
) == RSHIFT_EXPR
)
4727 name2
= gimple_assign_rhs1 (def_stmt
);
4728 cst2
= gimple_assign_rhs2 (def_stmt
);
4729 if (TREE_CODE (name2
) == SSA_NAME
4730 && host_integerp (cst2
, 1)
4731 && INTEGRAL_TYPE_P (TREE_TYPE (name2
))
4732 && IN_RANGE (tree_low_cst (cst2
, 1), 1, prec
- 1)
4733 && prec
<= HOST_BITS_PER_DOUBLE_INT
4734 && prec
== GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (val
)))
4735 && live_on_edge (e
, name2
)
4736 && !has_single_use (name2
))
4738 mask
= double_int_mask (tree_low_cst (cst2
, 1));
4739 val2
= fold_binary (LSHIFT_EXPR
, TREE_TYPE (val
), val
, cst2
);
4742 if (val2
!= NULL_TREE
4743 && TREE_CODE (val2
) == INTEGER_CST
4744 && simple_cst_equal (fold_build2 (RSHIFT_EXPR
,
4748 enum tree_code new_comp_code
= comp_code
;
4752 if (comp_code
== EQ_EXPR
|| comp_code
== NE_EXPR
)
4754 if (!TYPE_UNSIGNED (TREE_TYPE (val
)))
4756 tree type
= build_nonstandard_integer_type (prec
, 1);
4757 tmp
= build1 (NOP_EXPR
, type
, name2
);
4758 val2
= fold_convert (type
, val2
);
4760 tmp
= fold_build2 (MINUS_EXPR
, TREE_TYPE (tmp
), tmp
, val2
);
4761 new_val
= double_int_to_tree (TREE_TYPE (tmp
), mask
);
4762 new_comp_code
= comp_code
== EQ_EXPR
? LE_EXPR
: GT_EXPR
;
4764 else if (comp_code
== LT_EXPR
|| comp_code
== GE_EXPR
)
4769 = double_int_max_value (prec
, TYPE_UNSIGNED (TREE_TYPE (val
)));
4770 mask
= double_int_ior (tree_to_double_int (val2
), mask
);
4771 if (double_int_equal_p (mask
, maxval
))
4772 new_val
= NULL_TREE
;
4774 new_val
= double_int_to_tree (TREE_TYPE (val2
), mask
);
4781 fprintf (dump_file
, "Adding assert for ");
4782 print_generic_expr (dump_file
, name2
, 0);
4783 fprintf (dump_file
, " from ");
4784 print_generic_expr (dump_file
, tmp
, 0);
4785 fprintf (dump_file
, "\n");
4788 register_new_assert_for (name2
, tmp
, new_comp_code
, new_val
,
4794 /* Add asserts for NAME cmp CST and NAME being defined as
4795 NAME = NAME2 & CST2.
4797 Extract CST2 from the and. */
4798 names
[0] = NULL_TREE
;
4799 names
[1] = NULL_TREE
;
4801 if (is_gimple_assign (def_stmt
)
4802 && gimple_assign_rhs_code (def_stmt
) == BIT_AND_EXPR
)
4804 name2
= gimple_assign_rhs1 (def_stmt
);
4805 cst2
= gimple_assign_rhs2 (def_stmt
);
4806 if (TREE_CODE (name2
) == SSA_NAME
4807 && INTEGRAL_TYPE_P (TREE_TYPE (name2
))
4808 && TREE_CODE (cst2
) == INTEGER_CST
4809 && !integer_zerop (cst2
)
4810 && prec
<= HOST_BITS_PER_DOUBLE_INT
4812 || TYPE_UNSIGNED (TREE_TYPE (val
))))
4814 gimple def_stmt2
= SSA_NAME_DEF_STMT (name2
);
4815 if (gimple_assign_cast_p (def_stmt2
))
4817 names
[1] = gimple_assign_rhs1 (def_stmt2
);
4818 if (!CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt2
))
4819 || !INTEGRAL_TYPE_P (TREE_TYPE (names
[1]))
4820 || (TYPE_PRECISION (TREE_TYPE (name2
))
4821 != TYPE_PRECISION (TREE_TYPE (names
[1])))
4822 || !live_on_edge (e
, names
[1])
4823 || has_single_use (names
[1]))
4824 names
[1] = NULL_TREE
;
4826 if (live_on_edge (e
, name2
)
4827 && !has_single_use (name2
))
4831 if (names
[0] || names
[1])
4833 double_int minv
, maxv
= double_int_zero
, valv
, cst2v
;
4834 double_int tem
, sgnbit
;
4835 bool valid_p
= false, valn
= false, cst2n
= false;
4836 enum tree_code ccode
= comp_code
;
4838 valv
= double_int_zext (tree_to_double_int (val
), prec
);
4839 cst2v
= double_int_zext (tree_to_double_int (cst2
), prec
);
4840 if (!TYPE_UNSIGNED (TREE_TYPE (val
)))
4842 valn
= double_int_negative_p (double_int_sext (valv
, prec
));
4843 cst2n
= double_int_negative_p (double_int_sext (cst2v
, prec
));
4845 /* If CST2 doesn't have most significant bit set,
4846 but VAL is negative, we have comparison like
4847 if ((x & 0x123) > -4) (always true). Just give up. */
4851 sgnbit
= double_int_zext (double_int_lshift (double_int_one
,
4855 sgnbit
= double_int_zero
;
4856 minv
= double_int_and (valv
, cst2v
);
4860 /* Minimum unsigned value for equality is VAL & CST2
4861 (should be equal to VAL, otherwise we probably should
4862 have folded the comparison into false) and
4863 maximum unsigned value is VAL | ~CST2. */
4864 maxv
= double_int_ior (valv
, double_int_not (cst2v
));
4865 maxv
= double_int_zext (maxv
, prec
);
4869 tem
= double_int_ior (valv
, double_int_not (cst2v
));
4870 tem
= double_int_zext (tem
, prec
);
4871 /* If VAL is 0, handle (X & CST2) != 0 as (X & CST2) > 0U. */
4872 if (double_int_zero_p (valv
))
4875 sgnbit
= double_int_zero
;
4878 /* If (VAL | ~CST2) is all ones, handle it as
4879 (X & CST2) < VAL. */
4880 if (double_int_equal_p (tem
, double_int_mask (prec
)))
4884 sgnbit
= double_int_zero
;
4888 && double_int_negative_p (double_int_sext (cst2v
, prec
)))
4889 sgnbit
= double_int_zext (double_int_lshift (double_int_one
,
4892 if (!double_int_zero_p (sgnbit
))
4894 if (double_int_equal_p (valv
, sgnbit
))
4900 if (double_int_equal_p (tem
, double_int_mask (prec
- 1)))
4906 sgnbit
= double_int_zero
;
4910 /* Minimum unsigned value for >= if (VAL & CST2) == VAL
4911 is VAL and maximum unsigned value is ~0. For signed
4912 comparison, if CST2 doesn't have most significant bit
4913 set, handle it similarly. If CST2 has MSB set,
4914 the minimum is the same, and maximum is ~0U/2. */
4915 if (!double_int_equal_p (minv
, valv
))
4917 /* If (VAL & CST2) != VAL, X & CST2 can't be equal to
4919 minv
= masked_increment (valv
, cst2v
, sgnbit
, prec
);
4920 if (double_int_equal_p (minv
, valv
))
4923 maxv
= double_int_mask (prec
- (cst2n
? 1 : 0));
4928 /* Find out smallest MINV where MINV > VAL
4929 && (MINV & CST2) == MINV, if any. If VAL is signed and
4930 CST2 has MSB set, compute it biased by 1 << (prec - 1). */
4931 minv
= masked_increment (valv
, cst2v
, sgnbit
, prec
);
4932 if (double_int_equal_p (minv
, valv
))
4934 maxv
= double_int_mask (prec
- (cst2n
? 1 : 0));
4938 /* Minimum unsigned value for <= is 0 and maximum
4939 unsigned value is VAL | ~CST2 if (VAL & CST2) == VAL.
4940 Otherwise, find smallest VAL2 where VAL2 > VAL
4941 && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
4943 For signed comparison, if CST2 doesn't have most
4944 significant bit set, handle it similarly. If CST2 has
4945 MSB set, the maximum is the same and minimum is INT_MIN. */
4946 if (double_int_equal_p (minv
, valv
))
4950 maxv
= masked_increment (valv
, cst2v
, sgnbit
, prec
);
4951 if (double_int_equal_p (maxv
, valv
))
4953 maxv
= double_int_sub (maxv
, double_int_one
);
4955 maxv
= double_int_ior (maxv
, double_int_not (cst2v
));
4956 maxv
= double_int_zext (maxv
, prec
);
4962 /* Minimum unsigned value for < is 0 and maximum
4963 unsigned value is (VAL-1) | ~CST2 if (VAL & CST2) == VAL.
4964 Otherwise, find smallest VAL2 where VAL2 > VAL
4965 && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
4967 For signed comparison, if CST2 doesn't have most
4968 significant bit set, handle it similarly. If CST2 has
4969 MSB set, the maximum is the same and minimum is INT_MIN. */
4970 if (double_int_equal_p (minv
, valv
))
4972 if (double_int_equal_p (valv
, sgnbit
))
4978 maxv
= masked_increment (valv
, cst2v
, sgnbit
, prec
);
4979 if (double_int_equal_p (maxv
, valv
))
4982 maxv
= double_int_sub (maxv
, double_int_one
);
4983 maxv
= double_int_ior (maxv
, double_int_not (cst2v
));
4984 maxv
= double_int_zext (maxv
, prec
);
4992 && !double_int_equal_p (double_int_zext (double_int_sub (maxv
,
4995 double_int_mask (prec
)))
4997 tree tmp
, new_val
, type
;
5000 for (i
= 0; i
< 2; i
++)
5003 double_int maxv2
= maxv
;
5005 type
= TREE_TYPE (names
[i
]);
5006 if (!TYPE_UNSIGNED (type
))
5008 type
= build_nonstandard_integer_type (prec
, 1);
5009 tmp
= build1 (NOP_EXPR
, type
, names
[i
]);
5011 if (!double_int_zero_p (minv
))
5013 tmp
= build2 (PLUS_EXPR
, type
, tmp
,
5014 double_int_to_tree (type
,
5015 double_int_neg (minv
)));
5016 maxv2
= double_int_sub (maxv
, minv
);
5018 new_val
= double_int_to_tree (type
, maxv2
);
5022 fprintf (dump_file
, "Adding assert for ");
5023 print_generic_expr (dump_file
, names
[i
], 0);
5024 fprintf (dump_file
, " from ");
5025 print_generic_expr (dump_file
, tmp
, 0);
5026 fprintf (dump_file
, "\n");
5029 register_new_assert_for (names
[i
], tmp
, LE_EXPR
,
5030 new_val
, NULL
, e
, bsi
);
5040 /* OP is an operand of a truth value expression which is known to have
5041 a particular value. Register any asserts for OP and for any
5042 operands in OP's defining statement.
5044 If CODE is EQ_EXPR, then we want to register OP is zero (false),
5045 if CODE is NE_EXPR, then we want to register OP is nonzero (true). */
5048 register_edge_assert_for_1 (tree op
, enum tree_code code
,
5049 edge e
, gimple_stmt_iterator bsi
)
5051 bool retval
= false;
5054 enum tree_code rhs_code
;
5056 /* We only care about SSA_NAMEs. */
5057 if (TREE_CODE (op
) != SSA_NAME
)
5060 /* We know that OP will have a zero or nonzero value. If OP is used
5061 more than once go ahead and register an assert for OP.
5063 The FOUND_IN_SUBGRAPH support is not helpful in this situation as
5064 it will always be set for OP (because OP is used in a COND_EXPR in
5066 if (!has_single_use (op
))
5068 val
= build_int_cst (TREE_TYPE (op
), 0);
5069 register_new_assert_for (op
, op
, code
, val
, NULL
, e
, bsi
);
5073 /* Now look at how OP is set. If it's set from a comparison,
5074 a truth operation or some bit operations, then we may be able
5075 to register information about the operands of that assignment. */
5076 op_def
= SSA_NAME_DEF_STMT (op
);
5077 if (gimple_code (op_def
) != GIMPLE_ASSIGN
)
5080 rhs_code
= gimple_assign_rhs_code (op_def
);
5082 if (TREE_CODE_CLASS (rhs_code
) == tcc_comparison
)
5084 bool invert
= (code
== EQ_EXPR
? true : false);
5085 tree op0
= gimple_assign_rhs1 (op_def
);
5086 tree op1
= gimple_assign_rhs2 (op_def
);
5088 if (TREE_CODE (op0
) == SSA_NAME
)
5089 retval
|= register_edge_assert_for_2 (op0
, e
, bsi
, rhs_code
, op0
, op1
,
5091 if (TREE_CODE (op1
) == SSA_NAME
)
5092 retval
|= register_edge_assert_for_2 (op1
, e
, bsi
, rhs_code
, op0
, op1
,
5095 else if ((code
== NE_EXPR
5096 && gimple_assign_rhs_code (op_def
) == BIT_AND_EXPR
)
5098 && gimple_assign_rhs_code (op_def
) == BIT_IOR_EXPR
))
5100 /* Recurse on each operand. */
5101 retval
|= register_edge_assert_for_1 (gimple_assign_rhs1 (op_def
),
5103 retval
|= register_edge_assert_for_1 (gimple_assign_rhs2 (op_def
),
5106 else if (gimple_assign_rhs_code (op_def
) == BIT_NOT_EXPR
5107 && TYPE_PRECISION (TREE_TYPE (gimple_assign_lhs (op_def
))) == 1)
5109 /* Recurse, flipping CODE. */
5110 code
= invert_tree_comparison (code
, false);
5111 retval
|= register_edge_assert_for_1 (gimple_assign_rhs1 (op_def
),
5114 else if (gimple_assign_rhs_code (op_def
) == SSA_NAME
)
5116 /* Recurse through the copy. */
5117 retval
|= register_edge_assert_for_1 (gimple_assign_rhs1 (op_def
),
5120 else if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (op_def
)))
5122 /* Recurse through the type conversion. */
5123 retval
|= register_edge_assert_for_1 (gimple_assign_rhs1 (op_def
),
5130 /* Try to register an edge assertion for SSA name NAME on edge E for
5131 the condition COND contributing to the conditional jump pointed to by SI.
5132 Return true if an assertion for NAME could be registered. */
5135 register_edge_assert_for (tree name
, edge e
, gimple_stmt_iterator si
,
5136 enum tree_code cond_code
, tree cond_op0
,
5140 enum tree_code comp_code
;
5141 bool retval
= false;
5142 bool is_else_edge
= (e
->flags
& EDGE_FALSE_VALUE
) != 0;
5144 /* Do not attempt to infer anything in names that flow through
5146 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name
))
5149 if (!extract_code_and_val_from_cond_with_ops (name
, cond_code
,
5155 /* Register ASSERT_EXPRs for name. */
5156 retval
|= register_edge_assert_for_2 (name
, e
, si
, cond_code
, cond_op0
,
5157 cond_op1
, is_else_edge
);
5160 /* If COND is effectively an equality test of an SSA_NAME against
5161 the value zero or one, then we may be able to assert values
5162 for SSA_NAMEs which flow into COND. */
5164 /* In the case of NAME == 1 or NAME != 0, for BIT_AND_EXPR defining
5165 statement of NAME we can assert both operands of the BIT_AND_EXPR
5166 have nonzero value. */
5167 if (((comp_code
== EQ_EXPR
&& integer_onep (val
))
5168 || (comp_code
== NE_EXPR
&& integer_zerop (val
))))
5170 gimple def_stmt
= SSA_NAME_DEF_STMT (name
);
5172 if (is_gimple_assign (def_stmt
)
5173 && gimple_assign_rhs_code (def_stmt
) == BIT_AND_EXPR
)
5175 tree op0
= gimple_assign_rhs1 (def_stmt
);
5176 tree op1
= gimple_assign_rhs2 (def_stmt
);
5177 retval
|= register_edge_assert_for_1 (op0
, NE_EXPR
, e
, si
);
5178 retval
|= register_edge_assert_for_1 (op1
, NE_EXPR
, e
, si
);
5182 /* In the case of NAME == 0 or NAME != 1, for BIT_IOR_EXPR defining
5183 statement of NAME we can assert both operands of the BIT_IOR_EXPR
5185 if (((comp_code
== EQ_EXPR
&& integer_zerop (val
))
5186 || (comp_code
== NE_EXPR
&& integer_onep (val
))))
5188 gimple def_stmt
= SSA_NAME_DEF_STMT (name
);
5190 /* For BIT_IOR_EXPR only if NAME == 0 both operands have
5191 necessarily zero value, or if type-precision is one. */
5192 if (is_gimple_assign (def_stmt
)
5193 && (gimple_assign_rhs_code (def_stmt
) == BIT_IOR_EXPR
5194 && (TYPE_PRECISION (TREE_TYPE (name
)) == 1
5195 || comp_code
== EQ_EXPR
)))
5197 tree op0
= gimple_assign_rhs1 (def_stmt
);
5198 tree op1
= gimple_assign_rhs2 (def_stmt
);
5199 retval
|= register_edge_assert_for_1 (op0
, EQ_EXPR
, e
, si
);
5200 retval
|= register_edge_assert_for_1 (op1
, EQ_EXPR
, e
, si
);
5208 /* Determine whether the outgoing edges of BB should receive an
5209 ASSERT_EXPR for each of the operands of BB's LAST statement.
5210 The last statement of BB must be a COND_EXPR.
5212 If any of the sub-graphs rooted at BB have an interesting use of
5213 the predicate operands, an assert location node is added to the
5214 list of assertions for the corresponding operands. */
5217 find_conditional_asserts (basic_block bb
, gimple last
)
5220 gimple_stmt_iterator bsi
;
5226 need_assert
= false;
5227 bsi
= gsi_for_stmt (last
);
5229 /* Look for uses of the operands in each of the sub-graphs
5230 rooted at BB. We need to check each of the outgoing edges
5231 separately, so that we know what kind of ASSERT_EXPR to
5233 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
5238 /* Register the necessary assertions for each operand in the
5239 conditional predicate. */
5240 FOR_EACH_SSA_TREE_OPERAND (op
, last
, iter
, SSA_OP_USE
)
5242 need_assert
|= register_edge_assert_for (op
, e
, bsi
,
5243 gimple_cond_code (last
),
5244 gimple_cond_lhs (last
),
5245 gimple_cond_rhs (last
));
5258 /* Compare two case labels sorting first by the destination bb index
5259 and then by the case value. */
5262 compare_case_labels (const void *p1
, const void *p2
)
5264 const struct case_info
*ci1
= (const struct case_info
*) p1
;
5265 const struct case_info
*ci2
= (const struct case_info
*) p2
;
5266 int idx1
= ci1
->bb
->index
;
5267 int idx2
= ci2
->bb
->index
;
5271 else if (idx1
== idx2
)
5273 /* Make sure the default label is first in a group. */
5274 if (!CASE_LOW (ci1
->expr
))
5276 else if (!CASE_LOW (ci2
->expr
))
5279 return tree_int_cst_compare (CASE_LOW (ci1
->expr
),
5280 CASE_LOW (ci2
->expr
));
5286 /* Determine whether the outgoing edges of BB should receive an
5287 ASSERT_EXPR for each of the operands of BB's LAST statement.
5288 The last statement of BB must be a SWITCH_EXPR.
5290 If any of the sub-graphs rooted at BB have an interesting use of
5291 the predicate operands, an assert location node is added to the
5292 list of assertions for the corresponding operands. */
5295 find_switch_asserts (basic_block bb
, gimple last
)
5298 gimple_stmt_iterator bsi
;
5301 struct case_info
*ci
;
5302 size_t n
= gimple_switch_num_labels (last
);
5303 #if GCC_VERSION >= 4000
5306 /* Work around GCC 3.4 bug (PR 37086). */
5307 volatile unsigned int idx
;
5310 need_assert
= false;
5311 bsi
= gsi_for_stmt (last
);
5312 op
= gimple_switch_index (last
);
5313 if (TREE_CODE (op
) != SSA_NAME
)
5316 /* Build a vector of case labels sorted by destination label. */
5317 ci
= XNEWVEC (struct case_info
, n
);
5318 for (idx
= 0; idx
< n
; ++idx
)
5320 ci
[idx
].expr
= gimple_switch_label (last
, idx
);
5321 ci
[idx
].bb
= label_to_block (CASE_LABEL (ci
[idx
].expr
));
5323 qsort (ci
, n
, sizeof (struct case_info
), compare_case_labels
);
5325 for (idx
= 0; idx
< n
; ++idx
)
5328 tree cl
= ci
[idx
].expr
;
5329 basic_block cbb
= ci
[idx
].bb
;
5331 min
= CASE_LOW (cl
);
5332 max
= CASE_HIGH (cl
);
5334 /* If there are multiple case labels with the same destination
5335 we need to combine them to a single value range for the edge. */
5336 if (idx
+ 1 < n
&& cbb
== ci
[idx
+ 1].bb
)
5338 /* Skip labels until the last of the group. */
5341 } while (idx
< n
&& cbb
== ci
[idx
].bb
);
5344 /* Pick up the maximum of the case label range. */
5345 if (CASE_HIGH (ci
[idx
].expr
))
5346 max
= CASE_HIGH (ci
[idx
].expr
);
5348 max
= CASE_LOW (ci
[idx
].expr
);
5351 /* Nothing to do if the range includes the default label until we
5352 can register anti-ranges. */
5353 if (min
== NULL_TREE
)
5356 /* Find the edge to register the assert expr on. */
5357 e
= find_edge (bb
, cbb
);
5359 /* Register the necessary assertions for the operand in the
5361 need_assert
|= register_edge_assert_for (op
, e
, bsi
,
5362 max
? GE_EXPR
: EQ_EXPR
,
5364 fold_convert (TREE_TYPE (op
),
5368 need_assert
|= register_edge_assert_for (op
, e
, bsi
, LE_EXPR
,
5370 fold_convert (TREE_TYPE (op
),
5380 /* Traverse all the statements in block BB looking for statements that
5381 may generate useful assertions for the SSA names in their operand.
5382 If a statement produces a useful assertion A for name N_i, then the
5383 list of assertions already generated for N_i is scanned to
5384 determine if A is actually needed.
5386 If N_i already had the assertion A at a location dominating the
5387 current location, then nothing needs to be done. Otherwise, the
5388 new location for A is recorded instead.
5390 1- For every statement S in BB, all the variables used by S are
5391 added to bitmap FOUND_IN_SUBGRAPH.
5393 2- If statement S uses an operand N in a way that exposes a known
5394 value range for N, then if N was not already generated by an
5395 ASSERT_EXPR, create a new assert location for N. For instance,
5396 if N is a pointer and the statement dereferences it, we can
5397 assume that N is not NULL.
5399 3- COND_EXPRs are a special case of #2. We can derive range
5400 information from the predicate but need to insert different
5401 ASSERT_EXPRs for each of the sub-graphs rooted at the
5402 conditional block. If the last statement of BB is a conditional
5403 expression of the form 'X op Y', then
5405 a) Remove X and Y from the set FOUND_IN_SUBGRAPH.
5407 b) If the conditional is the only entry point to the sub-graph
5408 corresponding to the THEN_CLAUSE, recurse into it. On
5409 return, if X and/or Y are marked in FOUND_IN_SUBGRAPH, then
5410 an ASSERT_EXPR is added for the corresponding variable.
5412 c) Repeat step (b) on the ELSE_CLAUSE.
5414 d) Mark X and Y in FOUND_IN_SUBGRAPH.
5423 In this case, an assertion on the THEN clause is useful to
5424 determine that 'a' is always 9 on that edge. However, an assertion
5425 on the ELSE clause would be unnecessary.
5427 4- If BB does not end in a conditional expression, then we recurse
5428 into BB's dominator children.
5430 At the end of the recursive traversal, every SSA name will have a
5431 list of locations where ASSERT_EXPRs should be added. When a new
5432 location for name N is found, it is registered by calling
5433 register_new_assert_for. That function keeps track of all the
5434 registered assertions to prevent adding unnecessary assertions.
5435 For instance, if a pointer P_4 is dereferenced more than once in a
5436 dominator tree, only the location dominating all the dereference of
5437 P_4 will receive an ASSERT_EXPR.
5439 If this function returns true, then it means that there are names
5440 for which we need to generate ASSERT_EXPRs. Those assertions are
5441 inserted by process_assert_insertions. */
5444 find_assert_locations_1 (basic_block bb
, sbitmap live
)
5446 gimple_stmt_iterator si
;
5451 need_assert
= false;
5452 last
= last_stmt (bb
);
5454 /* If BB's last statement is a conditional statement involving integer
5455 operands, determine if we need to add ASSERT_EXPRs. */
5457 && gimple_code (last
) == GIMPLE_COND
5458 && !fp_predicate (last
)
5459 && !ZERO_SSA_OPERANDS (last
, SSA_OP_USE
))
5460 need_assert
|= find_conditional_asserts (bb
, last
);
5462 /* If BB's last statement is a switch statement involving integer
5463 operands, determine if we need to add ASSERT_EXPRs. */
5465 && gimple_code (last
) == GIMPLE_SWITCH
5466 && !ZERO_SSA_OPERANDS (last
, SSA_OP_USE
))
5467 need_assert
|= find_switch_asserts (bb
, last
);
5469 /* Traverse all the statements in BB marking used names and looking
5470 for statements that may infer assertions for their used operands. */
5471 for (si
= gsi_start_bb (bb
); !gsi_end_p (si
); gsi_next (&si
))
5477 stmt
= gsi_stmt (si
);
5479 if (is_gimple_debug (stmt
))
5482 /* See if we can derive an assertion for any of STMT's operands. */
5483 FOR_EACH_SSA_TREE_OPERAND (op
, stmt
, i
, SSA_OP_USE
)
5486 enum tree_code comp_code
;
5488 /* Mark OP in our live bitmap. */
5489 SET_BIT (live
, SSA_NAME_VERSION (op
));
5491 /* If OP is used in such a way that we can infer a value
5492 range for it, and we don't find a previous assertion for
5493 it, create a new assertion location node for OP. */
5494 if (infer_value_range (stmt
, op
, &comp_code
, &value
))
5496 /* If we are able to infer a nonzero value range for OP,
5497 then walk backwards through the use-def chain to see if OP
5498 was set via a typecast.
5500 If so, then we can also infer a nonzero value range
5501 for the operand of the NOP_EXPR. */
5502 if (comp_code
== NE_EXPR
&& integer_zerop (value
))
5505 gimple def_stmt
= SSA_NAME_DEF_STMT (t
);
5507 while (is_gimple_assign (def_stmt
)
5508 && gimple_assign_rhs_code (def_stmt
) == NOP_EXPR
5510 (gimple_assign_rhs1 (def_stmt
)) == SSA_NAME
5512 (TREE_TYPE (gimple_assign_rhs1 (def_stmt
))))
5514 t
= gimple_assign_rhs1 (def_stmt
);
5515 def_stmt
= SSA_NAME_DEF_STMT (t
);
5517 /* Note we want to register the assert for the
5518 operand of the NOP_EXPR after SI, not after the
5520 if (! has_single_use (t
))
5522 register_new_assert_for (t
, t
, comp_code
, value
,
5529 /* If OP is used only once, namely in this STMT, don't
5530 bother creating an ASSERT_EXPR for it. Such an
5531 ASSERT_EXPR would do nothing but increase compile time. */
5532 if (!has_single_use (op
))
5534 register_new_assert_for (op
, op
, comp_code
, value
,
5542 /* Traverse all PHI nodes in BB marking used operands. */
5543 for (si
= gsi_start_phis (bb
); !gsi_end_p(si
); gsi_next (&si
))
5545 use_operand_p arg_p
;
5547 phi
= gsi_stmt (si
);
5549 FOR_EACH_PHI_ARG (arg_p
, phi
, i
, SSA_OP_USE
)
5551 tree arg
= USE_FROM_PTR (arg_p
);
5552 if (TREE_CODE (arg
) == SSA_NAME
)
5553 SET_BIT (live
, SSA_NAME_VERSION (arg
));
5560 /* Do an RPO walk over the function computing SSA name liveness
5561 on-the-fly and deciding on assert expressions to insert.
5562 Returns true if there are assert expressions to be inserted. */
5565 find_assert_locations (void)
5567 int *rpo
= XNEWVEC (int, last_basic_block
);
5568 int *bb_rpo
= XNEWVEC (int, last_basic_block
);
5569 int *last_rpo
= XCNEWVEC (int, last_basic_block
);
5573 live
= XCNEWVEC (sbitmap
, last_basic_block
);
5574 rpo_cnt
= pre_and_rev_post_order_compute (NULL
, rpo
, false);
5575 for (i
= 0; i
< rpo_cnt
; ++i
)
5578 need_asserts
= false;
5579 for (i
= rpo_cnt
- 1; i
>= 0; --i
)
5581 basic_block bb
= BASIC_BLOCK (rpo
[i
]);
5587 live
[rpo
[i
]] = sbitmap_alloc (num_ssa_names
);
5588 sbitmap_zero (live
[rpo
[i
]]);
5591 /* Process BB and update the live information with uses in
5593 need_asserts
|= find_assert_locations_1 (bb
, live
[rpo
[i
]]);
5595 /* Merge liveness into the predecessor blocks and free it. */
5596 if (!sbitmap_empty_p (live
[rpo
[i
]]))
5599 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
5601 int pred
= e
->src
->index
;
5602 if ((e
->flags
& EDGE_DFS_BACK
) || pred
== ENTRY_BLOCK
)
5607 live
[pred
] = sbitmap_alloc (num_ssa_names
);
5608 sbitmap_zero (live
[pred
]);
5610 sbitmap_a_or_b (live
[pred
], live
[pred
], live
[rpo
[i
]]);
5612 if (bb_rpo
[pred
] < pred_rpo
)
5613 pred_rpo
= bb_rpo
[pred
];
5616 /* Record the RPO number of the last visited block that needs
5617 live information from this block. */
5618 last_rpo
[rpo
[i
]] = pred_rpo
;
5622 sbitmap_free (live
[rpo
[i
]]);
5623 live
[rpo
[i
]] = NULL
;
5626 /* We can free all successors live bitmaps if all their
5627 predecessors have been visited already. */
5628 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
5629 if (last_rpo
[e
->dest
->index
] == i
5630 && live
[e
->dest
->index
])
5632 sbitmap_free (live
[e
->dest
->index
]);
5633 live
[e
->dest
->index
] = NULL
;
5638 XDELETEVEC (bb_rpo
);
5639 XDELETEVEC (last_rpo
);
5640 for (i
= 0; i
< last_basic_block
; ++i
)
5642 sbitmap_free (live
[i
]);
5645 return need_asserts
;
5648 /* Create an ASSERT_EXPR for NAME and insert it in the location
5649 indicated by LOC. Return true if we made any edge insertions. */
5652 process_assert_insertions_for (tree name
, assert_locus_t loc
)
5654 /* Build the comparison expression NAME_i COMP_CODE VAL. */
5661 /* If we have X <=> X do not insert an assert expr for that. */
5662 if (loc
->expr
== loc
->val
)
5665 cond
= build2 (loc
->comp_code
, boolean_type_node
, loc
->expr
, loc
->val
);
5666 assert_stmt
= build_assert_expr_for (cond
, name
);
5669 /* We have been asked to insert the assertion on an edge. This
5670 is used only by COND_EXPR and SWITCH_EXPR assertions. */
5671 gcc_checking_assert (gimple_code (gsi_stmt (loc
->si
)) == GIMPLE_COND
5672 || (gimple_code (gsi_stmt (loc
->si
))
5675 gsi_insert_on_edge (loc
->e
, assert_stmt
);
5679 /* Otherwise, we can insert right after LOC->SI iff the
5680 statement must not be the last statement in the block. */
5681 stmt
= gsi_stmt (loc
->si
);
5682 if (!stmt_ends_bb_p (stmt
))
5684 gsi_insert_after (&loc
->si
, assert_stmt
, GSI_SAME_STMT
);
5688 /* If STMT must be the last statement in BB, we can only insert new
5689 assertions on the non-abnormal edge out of BB. Note that since
5690 STMT is not control flow, there may only be one non-abnormal edge
5692 FOR_EACH_EDGE (e
, ei
, loc
->bb
->succs
)
5693 if (!(e
->flags
& EDGE_ABNORMAL
))
5695 gsi_insert_on_edge (e
, assert_stmt
);
5703 /* Process all the insertions registered for every name N_i registered
5704 in NEED_ASSERT_FOR. The list of assertions to be inserted are
5705 found in ASSERTS_FOR[i]. */
5708 process_assert_insertions (void)
5712 bool update_edges_p
= false;
5713 int num_asserts
= 0;
5715 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
5716 dump_all_asserts (dump_file
);
5718 EXECUTE_IF_SET_IN_BITMAP (need_assert_for
, 0, i
, bi
)
5720 assert_locus_t loc
= asserts_for
[i
];
5725 assert_locus_t next
= loc
->next
;
5726 update_edges_p
|= process_assert_insertions_for (ssa_name (i
), loc
);
5734 gsi_commit_edge_inserts ();
5736 statistics_counter_event (cfun
, "Number of ASSERT_EXPR expressions inserted",
5741 /* Traverse the flowgraph looking for conditional jumps to insert range
5742 expressions. These range expressions are meant to provide information
5743 to optimizations that need to reason in terms of value ranges. They
5744 will not be expanded into RTL. For instance, given:
5753 this pass will transform the code into:
5759 x = ASSERT_EXPR <x, x < y>
5764 y = ASSERT_EXPR <y, x <= y>
5768 The idea is that once copy and constant propagation have run, other
5769 optimizations will be able to determine what ranges of values can 'x'
5770 take in different paths of the code, simply by checking the reaching
5771 definition of 'x'. */
5774 insert_range_assertions (void)
5776 need_assert_for
= BITMAP_ALLOC (NULL
);
5777 asserts_for
= XCNEWVEC (assert_locus_t
, num_ssa_names
);
5779 calculate_dominance_info (CDI_DOMINATORS
);
5781 if (find_assert_locations ())
5783 process_assert_insertions ();
5784 update_ssa (TODO_update_ssa_no_phi
);
5787 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
5789 fprintf (dump_file
, "\nSSA form after inserting ASSERT_EXPRs\n");
5790 dump_function_to_file (current_function_decl
, dump_file
, dump_flags
);
5794 BITMAP_FREE (need_assert_for
);
5797 /* Checks one ARRAY_REF in REF, located at LOCUS. Ignores flexible arrays
5798 and "struct" hacks. If VRP can determine that the
5799 array subscript is a constant, check if it is outside valid
5800 range. If the array subscript is a RANGE, warn if it is
5801 non-overlapping with valid range.
5802 IGNORE_OFF_BY_ONE is true if the ARRAY_REF is inside a ADDR_EXPR. */
5805 check_array_ref (location_t location
, tree ref
, bool ignore_off_by_one
)
5807 value_range_t
* vr
= NULL
;
5808 tree low_sub
, up_sub
;
5809 tree low_bound
, up_bound
, up_bound_p1
;
5812 if (TREE_NO_WARNING (ref
))
5815 low_sub
= up_sub
= TREE_OPERAND (ref
, 1);
5816 up_bound
= array_ref_up_bound (ref
);
5818 /* Can not check flexible arrays. */
5820 || TREE_CODE (up_bound
) != INTEGER_CST
)
5823 /* Accesses to trailing arrays via pointers may access storage
5824 beyond the types array bounds. */
5825 base
= get_base_address (ref
);
5826 if (base
&& TREE_CODE (base
) == MEM_REF
)
5828 tree cref
, next
= NULL_TREE
;
5830 if (TREE_CODE (TREE_OPERAND (ref
, 0)) != COMPONENT_REF
)
5833 cref
= TREE_OPERAND (ref
, 0);
5834 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (cref
, 0))) == RECORD_TYPE
)
5835 for (next
= DECL_CHAIN (TREE_OPERAND (cref
, 1));
5836 next
&& TREE_CODE (next
) != FIELD_DECL
;
5837 next
= DECL_CHAIN (next
))
5840 /* If this is the last field in a struct type or a field in a
5841 union type do not warn. */
5846 low_bound
= array_ref_low_bound (ref
);
5847 up_bound_p1
= int_const_binop (PLUS_EXPR
, up_bound
, integer_one_node
);
5849 if (TREE_CODE (low_sub
) == SSA_NAME
)
5851 vr
= get_value_range (low_sub
);
5852 if (vr
->type
== VR_RANGE
|| vr
->type
== VR_ANTI_RANGE
)
5854 low_sub
= vr
->type
== VR_RANGE
? vr
->max
: vr
->min
;
5855 up_sub
= vr
->type
== VR_RANGE
? vr
->min
: vr
->max
;
5859 if (vr
&& vr
->type
== VR_ANTI_RANGE
)
5861 if (TREE_CODE (up_sub
) == INTEGER_CST
5862 && tree_int_cst_lt (up_bound
, up_sub
)
5863 && TREE_CODE (low_sub
) == INTEGER_CST
5864 && tree_int_cst_lt (low_sub
, low_bound
))
5866 warning_at (location
, OPT_Warray_bounds
,
5867 "array subscript is outside array bounds");
5868 TREE_NO_WARNING (ref
) = 1;
5871 else if (TREE_CODE (up_sub
) == INTEGER_CST
5872 && (ignore_off_by_one
5873 ? (tree_int_cst_lt (up_bound
, up_sub
)
5874 && !tree_int_cst_equal (up_bound_p1
, up_sub
))
5875 : (tree_int_cst_lt (up_bound
, up_sub
)
5876 || tree_int_cst_equal (up_bound_p1
, up_sub
))))
5878 warning_at (location
, OPT_Warray_bounds
,
5879 "array subscript is above array bounds");
5880 TREE_NO_WARNING (ref
) = 1;
5882 else if (TREE_CODE (low_sub
) == INTEGER_CST
5883 && tree_int_cst_lt (low_sub
, low_bound
))
5885 warning_at (location
, OPT_Warray_bounds
,
5886 "array subscript is below array bounds");
5887 TREE_NO_WARNING (ref
) = 1;
5891 /* Searches if the expr T, located at LOCATION computes
5892 address of an ARRAY_REF, and call check_array_ref on it. */
5895 search_for_addr_array (tree t
, location_t location
)
5897 while (TREE_CODE (t
) == SSA_NAME
)
5899 gimple g
= SSA_NAME_DEF_STMT (t
);
5901 if (gimple_code (g
) != GIMPLE_ASSIGN
)
5904 if (get_gimple_rhs_class (gimple_assign_rhs_code (g
))
5905 != GIMPLE_SINGLE_RHS
)
5908 t
= gimple_assign_rhs1 (g
);
5912 /* We are only interested in addresses of ARRAY_REF's. */
5913 if (TREE_CODE (t
) != ADDR_EXPR
)
5916 /* Check each ARRAY_REFs in the reference chain. */
5919 if (TREE_CODE (t
) == ARRAY_REF
)
5920 check_array_ref (location
, t
, true /*ignore_off_by_one*/);
5922 t
= TREE_OPERAND (t
, 0);
5924 while (handled_component_p (t
));
5926 if (TREE_CODE (t
) == MEM_REF
5927 && TREE_CODE (TREE_OPERAND (t
, 0)) == ADDR_EXPR
5928 && !TREE_NO_WARNING (t
))
5930 tree tem
= TREE_OPERAND (TREE_OPERAND (t
, 0), 0);
5931 tree low_bound
, up_bound
, el_sz
;
5933 if (TREE_CODE (TREE_TYPE (tem
)) != ARRAY_TYPE
5934 || TREE_CODE (TREE_TYPE (TREE_TYPE (tem
))) == ARRAY_TYPE
5935 || !TYPE_DOMAIN (TREE_TYPE (tem
)))
5938 low_bound
= TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (tem
)));
5939 up_bound
= TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (tem
)));
5940 el_sz
= TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (tem
)));
5942 || TREE_CODE (low_bound
) != INTEGER_CST
5944 || TREE_CODE (up_bound
) != INTEGER_CST
5946 || TREE_CODE (el_sz
) != INTEGER_CST
)
5949 idx
= mem_ref_offset (t
);
5950 idx
= double_int_sdiv (idx
, tree_to_double_int (el_sz
), TRUNC_DIV_EXPR
);
5951 if (double_int_scmp (idx
, double_int_zero
) < 0)
5953 warning_at (location
, OPT_Warray_bounds
,
5954 "array subscript is below array bounds");
5955 TREE_NO_WARNING (t
) = 1;
5957 else if (double_int_scmp (idx
,
5960 (tree_to_double_int (up_bound
),
5962 (tree_to_double_int (low_bound
))),
5963 double_int_one
)) > 0)
5965 warning_at (location
, OPT_Warray_bounds
,
5966 "array subscript is above array bounds");
5967 TREE_NO_WARNING (t
) = 1;
5972 /* walk_tree() callback that checks if *TP is
5973 an ARRAY_REF inside an ADDR_EXPR (in which an array
5974 subscript one outside the valid range is allowed). Call
5975 check_array_ref for each ARRAY_REF found. The location is
5979 check_array_bounds (tree
*tp
, int *walk_subtree
, void *data
)
5982 struct walk_stmt_info
*wi
= (struct walk_stmt_info
*) data
;
5983 location_t location
;
5985 if (EXPR_HAS_LOCATION (t
))
5986 location
= EXPR_LOCATION (t
);
5989 location_t
*locp
= (location_t
*) wi
->info
;
5993 *walk_subtree
= TRUE
;
5995 if (TREE_CODE (t
) == ARRAY_REF
)
5996 check_array_ref (location
, t
, false /*ignore_off_by_one*/);
5998 if (TREE_CODE (t
) == MEM_REF
5999 || (TREE_CODE (t
) == RETURN_EXPR
&& TREE_OPERAND (t
, 0)))
6000 search_for_addr_array (TREE_OPERAND (t
, 0), location
);
6002 if (TREE_CODE (t
) == ADDR_EXPR
)
6003 *walk_subtree
= FALSE
;
6008 /* Walk over all statements of all reachable BBs and call check_array_bounds
6012 check_all_array_refs (void)
6015 gimple_stmt_iterator si
;
6021 bool executable
= false;
6023 /* Skip blocks that were found to be unreachable. */
6024 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
6025 executable
|= !!(e
->flags
& EDGE_EXECUTABLE
);
6029 for (si
= gsi_start_bb (bb
); !gsi_end_p (si
); gsi_next (&si
))
6031 gimple stmt
= gsi_stmt (si
);
6032 struct walk_stmt_info wi
;
6033 if (!gimple_has_location (stmt
))
6036 if (is_gimple_call (stmt
))
6039 size_t n
= gimple_call_num_args (stmt
);
6040 for (i
= 0; i
< n
; i
++)
6042 tree arg
= gimple_call_arg (stmt
, i
);
6043 search_for_addr_array (arg
, gimple_location (stmt
));
6048 memset (&wi
, 0, sizeof (wi
));
6049 wi
.info
= CONST_CAST (void *, (const void *)
6050 gimple_location_ptr (stmt
));
6052 walk_gimple_op (gsi_stmt (si
),
6060 /* Convert range assertion expressions into the implied copies and
6061 copy propagate away the copies. Doing the trivial copy propagation
6062 here avoids the need to run the full copy propagation pass after
6065 FIXME, this will eventually lead to copy propagation removing the
6066 names that had useful range information attached to them. For
6067 instance, if we had the assertion N_i = ASSERT_EXPR <N_j, N_j > 3>,
6068 then N_i will have the range [3, +INF].
6070 However, by converting the assertion into the implied copy
6071 operation N_i = N_j, we will then copy-propagate N_j into the uses
6072 of N_i and lose the range information. We may want to hold on to
6073 ASSERT_EXPRs a little while longer as the ranges could be used in
6074 things like jump threading.
6076 The problem with keeping ASSERT_EXPRs around is that passes after
6077 VRP need to handle them appropriately.
6079 Another approach would be to make the range information a first
6080 class property of the SSA_NAME so that it can be queried from
6081 any pass. This is made somewhat more complex by the need for
6082 multiple ranges to be associated with one SSA_NAME. */
6085 remove_range_assertions (void)
6088 gimple_stmt_iterator si
;
6090 /* Note that the BSI iterator bump happens at the bottom of the
6091 loop and no bump is necessary if we're removing the statement
6092 referenced by the current BSI. */
6094 for (si
= gsi_start_bb (bb
); !gsi_end_p (si
);)
6096 gimple stmt
= gsi_stmt (si
);
6099 if (is_gimple_assign (stmt
)
6100 && gimple_assign_rhs_code (stmt
) == ASSERT_EXPR
)
6102 tree rhs
= gimple_assign_rhs1 (stmt
);
6104 tree cond
= fold (ASSERT_EXPR_COND (rhs
));
6105 use_operand_p use_p
;
6106 imm_use_iterator iter
;
6108 gcc_assert (cond
!= boolean_false_node
);
6110 /* Propagate the RHS into every use of the LHS. */
6111 var
= ASSERT_EXPR_VAR (rhs
);
6112 FOR_EACH_IMM_USE_STMT (use_stmt
, iter
,
6113 gimple_assign_lhs (stmt
))
6114 FOR_EACH_IMM_USE_ON_STMT (use_p
, iter
)
6116 SET_USE (use_p
, var
);
6117 gcc_assert (TREE_CODE (var
) == SSA_NAME
);
6120 /* And finally, remove the copy, it is not needed. */
6121 gsi_remove (&si
, true);
6122 release_defs (stmt
);
6130 /* Return true if STMT is interesting for VRP. */
6133 stmt_interesting_for_vrp (gimple stmt
)
6135 if (gimple_code (stmt
) == GIMPLE_PHI
)
6137 tree res
= gimple_phi_result (stmt
);
6138 return (!virtual_operand_p (res
)
6139 && (INTEGRAL_TYPE_P (TREE_TYPE (res
))
6140 || POINTER_TYPE_P (TREE_TYPE (res
))));
6142 else if (is_gimple_assign (stmt
) || is_gimple_call (stmt
))
6144 tree lhs
= gimple_get_lhs (stmt
);
6146 /* In general, assignments with virtual operands are not useful
6147 for deriving ranges, with the obvious exception of calls to
6148 builtin functions. */
6149 if (lhs
&& TREE_CODE (lhs
) == SSA_NAME
6150 && (INTEGRAL_TYPE_P (TREE_TYPE (lhs
))
6151 || POINTER_TYPE_P (TREE_TYPE (lhs
)))
6152 && ((is_gimple_call (stmt
)
6153 && gimple_call_fndecl (stmt
) != NULL_TREE
6154 && DECL_BUILT_IN (gimple_call_fndecl (stmt
)))
6155 || !gimple_vuse (stmt
)))
6158 else if (gimple_code (stmt
) == GIMPLE_COND
6159 || gimple_code (stmt
) == GIMPLE_SWITCH
)
6166 /* Initialize local data structures for VRP. */
6169 vrp_initialize (void)
6173 values_propagated
= false;
6174 num_vr_values
= num_ssa_names
;
6175 vr_value
= XCNEWVEC (value_range_t
*, num_vr_values
);
6176 vr_phi_edge_counts
= XCNEWVEC (int, num_ssa_names
);
6180 gimple_stmt_iterator si
;
6182 for (si
= gsi_start_phis (bb
); !gsi_end_p (si
); gsi_next (&si
))
6184 gimple phi
= gsi_stmt (si
);
6185 if (!stmt_interesting_for_vrp (phi
))
6187 tree lhs
= PHI_RESULT (phi
);
6188 set_value_range_to_varying (get_value_range (lhs
));
6189 prop_set_simulate_again (phi
, false);
6192 prop_set_simulate_again (phi
, true);
6195 for (si
= gsi_start_bb (bb
); !gsi_end_p (si
); gsi_next (&si
))
6197 gimple stmt
= gsi_stmt (si
);
6199 /* If the statement is a control insn, then we do not
6200 want to avoid simulating the statement once. Failure
6201 to do so means that those edges will never get added. */
6202 if (stmt_ends_bb_p (stmt
))
6203 prop_set_simulate_again (stmt
, true);
6204 else if (!stmt_interesting_for_vrp (stmt
))
6208 FOR_EACH_SSA_TREE_OPERAND (def
, stmt
, i
, SSA_OP_DEF
)
6209 set_value_range_to_varying (get_value_range (def
));
6210 prop_set_simulate_again (stmt
, false);
6213 prop_set_simulate_again (stmt
, true);
6218 /* Return the singleton value-range for NAME or NAME. */
6221 vrp_valueize (tree name
)
6223 if (TREE_CODE (name
) == SSA_NAME
)
6225 value_range_t
*vr
= get_value_range (name
);
6226 if (vr
->type
== VR_RANGE
6227 && (vr
->min
== vr
->max
6228 || operand_equal_p (vr
->min
, vr
->max
, 0)))
6234 /* Visit assignment STMT. If it produces an interesting range, record
6235 the SSA name in *OUTPUT_P. */
6237 static enum ssa_prop_result
6238 vrp_visit_assignment_or_call (gimple stmt
, tree
*output_p
)
6242 enum gimple_code code
= gimple_code (stmt
);
6243 lhs
= gimple_get_lhs (stmt
);
6245 /* We only keep track of ranges in integral and pointer types. */
6246 if (TREE_CODE (lhs
) == SSA_NAME
6247 && ((INTEGRAL_TYPE_P (TREE_TYPE (lhs
))
6248 /* It is valid to have NULL MIN/MAX values on a type. See
6249 build_range_type. */
6250 && TYPE_MIN_VALUE (TREE_TYPE (lhs
))
6251 && TYPE_MAX_VALUE (TREE_TYPE (lhs
)))
6252 || POINTER_TYPE_P (TREE_TYPE (lhs
))))
6254 value_range_t new_vr
= VR_INITIALIZER
;
6256 /* Try folding the statement to a constant first. */
6257 tree tem
= gimple_fold_stmt_to_constant (stmt
, vrp_valueize
);
6258 if (tem
&& !is_overflow_infinity (tem
))
6259 set_value_range (&new_vr
, VR_RANGE
, tem
, tem
, NULL
);
6260 /* Then dispatch to value-range extracting functions. */
6261 else if (code
== GIMPLE_CALL
)
6262 extract_range_basic (&new_vr
, stmt
);
6264 extract_range_from_assignment (&new_vr
, stmt
);
6266 if (update_value_range (lhs
, &new_vr
))
6270 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6272 fprintf (dump_file
, "Found new range for ");
6273 print_generic_expr (dump_file
, lhs
, 0);
6274 fprintf (dump_file
, ": ");
6275 dump_value_range (dump_file
, &new_vr
);
6276 fprintf (dump_file
, "\n\n");
6279 if (new_vr
.type
== VR_VARYING
)
6280 return SSA_PROP_VARYING
;
6282 return SSA_PROP_INTERESTING
;
6285 return SSA_PROP_NOT_INTERESTING
;
6288 /* Every other statement produces no useful ranges. */
6289 FOR_EACH_SSA_TREE_OPERAND (def
, stmt
, iter
, SSA_OP_DEF
)
6290 set_value_range_to_varying (get_value_range (def
));
6292 return SSA_PROP_VARYING
;
6295 /* Helper that gets the value range of the SSA_NAME with version I
6296 or a symbolic range containing the SSA_NAME only if the value range
6297 is varying or undefined. */
6299 static inline value_range_t
6300 get_vr_for_comparison (int i
)
6302 value_range_t vr
= *get_value_range (ssa_name (i
));
6304 /* If name N_i does not have a valid range, use N_i as its own
6305 range. This allows us to compare against names that may
6306 have N_i in their ranges. */
6307 if (vr
.type
== VR_VARYING
|| vr
.type
== VR_UNDEFINED
)
6310 vr
.min
= ssa_name (i
);
6311 vr
.max
= ssa_name (i
);
6317 /* Compare all the value ranges for names equivalent to VAR with VAL
6318 using comparison code COMP. Return the same value returned by
6319 compare_range_with_value, including the setting of
6320 *STRICT_OVERFLOW_P. */
6323 compare_name_with_value (enum tree_code comp
, tree var
, tree val
,
6324 bool *strict_overflow_p
)
6330 int used_strict_overflow
;
6332 value_range_t equiv_vr
;
6334 /* Get the set of equivalences for VAR. */
6335 e
= get_value_range (var
)->equiv
;
6337 /* Start at -1. Set it to 0 if we do a comparison without relying
6338 on overflow, or 1 if all comparisons rely on overflow. */
6339 used_strict_overflow
= -1;
6341 /* Compare vars' value range with val. */
6342 equiv_vr
= get_vr_for_comparison (SSA_NAME_VERSION (var
));
6344 retval
= compare_range_with_value (comp
, &equiv_vr
, val
, &sop
);
6346 used_strict_overflow
= sop
? 1 : 0;
6348 /* If the equiv set is empty we have done all work we need to do. */
6352 && used_strict_overflow
> 0)
6353 *strict_overflow_p
= true;
6357 EXECUTE_IF_SET_IN_BITMAP (e
, 0, i
, bi
)
6359 equiv_vr
= get_vr_for_comparison (i
);
6361 t
= compare_range_with_value (comp
, &equiv_vr
, val
, &sop
);
6364 /* If we get different answers from different members
6365 of the equivalence set this check must be in a dead
6366 code region. Folding it to a trap representation
6367 would be correct here. For now just return don't-know. */
6377 used_strict_overflow
= 0;
6378 else if (used_strict_overflow
< 0)
6379 used_strict_overflow
= 1;
6384 && used_strict_overflow
> 0)
6385 *strict_overflow_p
= true;
6391 /* Given a comparison code COMP and names N1 and N2, compare all the
6392 ranges equivalent to N1 against all the ranges equivalent to N2
6393 to determine the value of N1 COMP N2. Return the same value
6394 returned by compare_ranges. Set *STRICT_OVERFLOW_P to indicate
6395 whether we relied on an overflow infinity in the comparison. */
6399 compare_names (enum tree_code comp
, tree n1
, tree n2
,
6400 bool *strict_overflow_p
)
6404 bitmap_iterator bi1
, bi2
;
6406 int used_strict_overflow
;
6407 static bitmap_obstack
*s_obstack
= NULL
;
6408 static bitmap s_e1
= NULL
, s_e2
= NULL
;
6410 /* Compare the ranges of every name equivalent to N1 against the
6411 ranges of every name equivalent to N2. */
6412 e1
= get_value_range (n1
)->equiv
;
6413 e2
= get_value_range (n2
)->equiv
;
6415 /* Use the fake bitmaps if e1 or e2 are not available. */
6416 if (s_obstack
== NULL
)
6418 s_obstack
= XNEW (bitmap_obstack
);
6419 bitmap_obstack_initialize (s_obstack
);
6420 s_e1
= BITMAP_ALLOC (s_obstack
);
6421 s_e2
= BITMAP_ALLOC (s_obstack
);
6428 /* Add N1 and N2 to their own set of equivalences to avoid
6429 duplicating the body of the loop just to check N1 and N2
6431 bitmap_set_bit (e1
, SSA_NAME_VERSION (n1
));
6432 bitmap_set_bit (e2
, SSA_NAME_VERSION (n2
));
6434 /* If the equivalence sets have a common intersection, then the two
6435 names can be compared without checking their ranges. */
6436 if (bitmap_intersect_p (e1
, e2
))
6438 bitmap_clear_bit (e1
, SSA_NAME_VERSION (n1
));
6439 bitmap_clear_bit (e2
, SSA_NAME_VERSION (n2
));
6441 return (comp
== EQ_EXPR
|| comp
== GE_EXPR
|| comp
== LE_EXPR
)
6443 : boolean_false_node
;
6446 /* Start at -1. Set it to 0 if we do a comparison without relying
6447 on overflow, or 1 if all comparisons rely on overflow. */
6448 used_strict_overflow
= -1;
6450 /* Otherwise, compare all the equivalent ranges. First, add N1 and
6451 N2 to their own set of equivalences to avoid duplicating the body
6452 of the loop just to check N1 and N2 ranges. */
6453 EXECUTE_IF_SET_IN_BITMAP (e1
, 0, i1
, bi1
)
6455 value_range_t vr1
= get_vr_for_comparison (i1
);
6457 t
= retval
= NULL_TREE
;
6458 EXECUTE_IF_SET_IN_BITMAP (e2
, 0, i2
, bi2
)
6462 value_range_t vr2
= get_vr_for_comparison (i2
);
6464 t
= compare_ranges (comp
, &vr1
, &vr2
, &sop
);
6467 /* If we get different answers from different members
6468 of the equivalence set this check must be in a dead
6469 code region. Folding it to a trap representation
6470 would be correct here. For now just return don't-know. */
6474 bitmap_clear_bit (e1
, SSA_NAME_VERSION (n1
));
6475 bitmap_clear_bit (e2
, SSA_NAME_VERSION (n2
));
6481 used_strict_overflow
= 0;
6482 else if (used_strict_overflow
< 0)
6483 used_strict_overflow
= 1;
6489 bitmap_clear_bit (e1
, SSA_NAME_VERSION (n1
));
6490 bitmap_clear_bit (e2
, SSA_NAME_VERSION (n2
));
6491 if (used_strict_overflow
> 0)
6492 *strict_overflow_p
= true;
6497 /* None of the equivalent ranges are useful in computing this
6499 bitmap_clear_bit (e1
, SSA_NAME_VERSION (n1
));
6500 bitmap_clear_bit (e2
, SSA_NAME_VERSION (n2
));
6504 /* Helper function for vrp_evaluate_conditional_warnv. */
6507 vrp_evaluate_conditional_warnv_with_ops_using_ranges (enum tree_code code
,
6509 bool * strict_overflow_p
)
6511 value_range_t
*vr0
, *vr1
;
6513 vr0
= (TREE_CODE (op0
) == SSA_NAME
) ? get_value_range (op0
) : NULL
;
6514 vr1
= (TREE_CODE (op1
) == SSA_NAME
) ? get_value_range (op1
) : NULL
;
6517 return compare_ranges (code
, vr0
, vr1
, strict_overflow_p
);
6518 else if (vr0
&& vr1
== NULL
)
6519 return compare_range_with_value (code
, vr0
, op1
, strict_overflow_p
);
6520 else if (vr0
== NULL
&& vr1
)
6521 return (compare_range_with_value
6522 (swap_tree_comparison (code
), vr1
, op0
, strict_overflow_p
));
6526 /* Helper function for vrp_evaluate_conditional_warnv. */
6529 vrp_evaluate_conditional_warnv_with_ops (enum tree_code code
, tree op0
,
6530 tree op1
, bool use_equiv_p
,
6531 bool *strict_overflow_p
, bool *only_ranges
)
6535 *only_ranges
= true;
6537 /* We only deal with integral and pointer types. */
6538 if (!INTEGRAL_TYPE_P (TREE_TYPE (op0
))
6539 && !POINTER_TYPE_P (TREE_TYPE (op0
)))
6545 && (ret
= vrp_evaluate_conditional_warnv_with_ops_using_ranges
6546 (code
, op0
, op1
, strict_overflow_p
)))
6548 *only_ranges
= false;
6549 if (TREE_CODE (op0
) == SSA_NAME
&& TREE_CODE (op1
) == SSA_NAME
)
6550 return compare_names (code
, op0
, op1
, strict_overflow_p
);
6551 else if (TREE_CODE (op0
) == SSA_NAME
)
6552 return compare_name_with_value (code
, op0
, op1
, strict_overflow_p
);
6553 else if (TREE_CODE (op1
) == SSA_NAME
)
6554 return (compare_name_with_value
6555 (swap_tree_comparison (code
), op1
, op0
, strict_overflow_p
));
6558 return vrp_evaluate_conditional_warnv_with_ops_using_ranges (code
, op0
, op1
,
6563 /* Given (CODE OP0 OP1) within STMT, try to simplify it based on value range
6564 information. Return NULL if the conditional can not be evaluated.
6565 The ranges of all the names equivalent with the operands in COND
6566 will be used when trying to compute the value. If the result is
6567 based on undefined signed overflow, issue a warning if
6571 vrp_evaluate_conditional (enum tree_code code
, tree op0
, tree op1
, gimple stmt
)
6577 /* Some passes and foldings leak constants with overflow flag set
6578 into the IL. Avoid doing wrong things with these and bail out. */
6579 if ((TREE_CODE (op0
) == INTEGER_CST
6580 && TREE_OVERFLOW (op0
))
6581 || (TREE_CODE (op1
) == INTEGER_CST
6582 && TREE_OVERFLOW (op1
)))
6586 ret
= vrp_evaluate_conditional_warnv_with_ops (code
, op0
, op1
, true, &sop
,
6591 enum warn_strict_overflow_code wc
;
6592 const char* warnmsg
;
6594 if (is_gimple_min_invariant (ret
))
6596 wc
= WARN_STRICT_OVERFLOW_CONDITIONAL
;
6597 warnmsg
= G_("assuming signed overflow does not occur when "
6598 "simplifying conditional to constant");
6602 wc
= WARN_STRICT_OVERFLOW_COMPARISON
;
6603 warnmsg
= G_("assuming signed overflow does not occur when "
6604 "simplifying conditional");
6607 if (issue_strict_overflow_warning (wc
))
6609 location_t location
;
6611 if (!gimple_has_location (stmt
))
6612 location
= input_location
;
6614 location
= gimple_location (stmt
);
6615 warning_at (location
, OPT_Wstrict_overflow
, "%s", warnmsg
);
6619 if (warn_type_limits
6620 && ret
&& only_ranges
6621 && TREE_CODE_CLASS (code
) == tcc_comparison
6622 && TREE_CODE (op0
) == SSA_NAME
)
6624 /* If the comparison is being folded and the operand on the LHS
6625 is being compared against a constant value that is outside of
6626 the natural range of OP0's type, then the predicate will
6627 always fold regardless of the value of OP0. If -Wtype-limits
6628 was specified, emit a warning. */
6629 tree type
= TREE_TYPE (op0
);
6630 value_range_t
*vr0
= get_value_range (op0
);
6632 if (vr0
->type
!= VR_VARYING
6633 && INTEGRAL_TYPE_P (type
)
6634 && vrp_val_is_min (vr0
->min
)
6635 && vrp_val_is_max (vr0
->max
)
6636 && is_gimple_min_invariant (op1
))
6638 location_t location
;
6640 if (!gimple_has_location (stmt
))
6641 location
= input_location
;
6643 location
= gimple_location (stmt
);
6645 warning_at (location
, OPT_Wtype_limits
,
6647 ? G_("comparison always false "
6648 "due to limited range of data type")
6649 : G_("comparison always true "
6650 "due to limited range of data type"));
6658 /* Visit conditional statement STMT. If we can determine which edge
6659 will be taken out of STMT's basic block, record it in
6660 *TAKEN_EDGE_P and return SSA_PROP_INTERESTING. Otherwise, return
6661 SSA_PROP_VARYING. */
6663 static enum ssa_prop_result
6664 vrp_visit_cond_stmt (gimple stmt
, edge
*taken_edge_p
)
6669 *taken_edge_p
= NULL
;
6671 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6676 fprintf (dump_file
, "\nVisiting conditional with predicate: ");
6677 print_gimple_stmt (dump_file
, stmt
, 0, 0);
6678 fprintf (dump_file
, "\nWith known ranges\n");
6680 FOR_EACH_SSA_TREE_OPERAND (use
, stmt
, i
, SSA_OP_USE
)
6682 fprintf (dump_file
, "\t");
6683 print_generic_expr (dump_file
, use
, 0);
6684 fprintf (dump_file
, ": ");
6685 dump_value_range (dump_file
, vr_value
[SSA_NAME_VERSION (use
)]);
6688 fprintf (dump_file
, "\n");
6691 /* Compute the value of the predicate COND by checking the known
6692 ranges of each of its operands.
6694 Note that we cannot evaluate all the equivalent ranges here
6695 because those ranges may not yet be final and with the current
6696 propagation strategy, we cannot determine when the value ranges
6697 of the names in the equivalence set have changed.
6699 For instance, given the following code fragment
6703 i_14 = ASSERT_EXPR <i_5, i_5 != 0>
6707 Assume that on the first visit to i_14, i_5 has the temporary
6708 range [8, 8] because the second argument to the PHI function is
6709 not yet executable. We derive the range ~[0, 0] for i_14 and the
6710 equivalence set { i_5 }. So, when we visit 'if (i_14 == 1)' for
6711 the first time, since i_14 is equivalent to the range [8, 8], we
6712 determine that the predicate is always false.
6714 On the next round of propagation, i_13 is determined to be
6715 VARYING, which causes i_5 to drop down to VARYING. So, another
6716 visit to i_14 is scheduled. In this second visit, we compute the
6717 exact same range and equivalence set for i_14, namely ~[0, 0] and
6718 { i_5 }. But we did not have the previous range for i_5
6719 registered, so vrp_visit_assignment thinks that the range for
6720 i_14 has not changed. Therefore, the predicate 'if (i_14 == 1)'
6721 is not visited again, which stops propagation from visiting
6722 statements in the THEN clause of that if().
6724 To properly fix this we would need to keep the previous range
6725 value for the names in the equivalence set. This way we would've
6726 discovered that from one visit to the other i_5 changed from
6727 range [8, 8] to VR_VARYING.
6729 However, fixing this apparent limitation may not be worth the
6730 additional checking. Testing on several code bases (GCC, DLV,
6731 MICO, TRAMP3D and SPEC2000) showed that doing this results in
6732 4 more predicates folded in SPEC. */
6735 val
= vrp_evaluate_conditional_warnv_with_ops (gimple_cond_code (stmt
),
6736 gimple_cond_lhs (stmt
),
6737 gimple_cond_rhs (stmt
),
6742 *taken_edge_p
= find_taken_edge (gimple_bb (stmt
), val
);
6745 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6747 "\nIgnoring predicate evaluation because "
6748 "it assumes that signed overflow is undefined");
6753 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6755 fprintf (dump_file
, "\nPredicate evaluates to: ");
6756 if (val
== NULL_TREE
)
6757 fprintf (dump_file
, "DON'T KNOW\n");
6759 print_generic_stmt (dump_file
, val
, 0);
6762 return (*taken_edge_p
) ? SSA_PROP_INTERESTING
: SSA_PROP_VARYING
;
6765 /* Searches the case label vector VEC for the index *IDX of the CASE_LABEL
6766 that includes the value VAL. The search is restricted to the range
6767 [START_IDX, n - 1] where n is the size of VEC.
6769 If there is a CASE_LABEL for VAL, its index is placed in IDX and true is
6772 If there is no CASE_LABEL for VAL and there is one that is larger than VAL,
6773 it is placed in IDX and false is returned.
6775 If VAL is larger than any CASE_LABEL, n is placed on IDX and false is
6779 find_case_label_index (gimple stmt
, size_t start_idx
, tree val
, size_t *idx
)
6781 size_t n
= gimple_switch_num_labels (stmt
);
6784 /* Find case label for minimum of the value range or the next one.
6785 At each iteration we are searching in [low, high - 1]. */
6787 for (low
= start_idx
, high
= n
; high
!= low
; )
6791 /* Note that i != high, so we never ask for n. */
6792 size_t i
= (high
+ low
) / 2;
6793 t
= gimple_switch_label (stmt
, i
);
6795 /* Cache the result of comparing CASE_LOW and val. */
6796 cmp
= tree_int_cst_compare (CASE_LOW (t
), val
);
6800 /* Ranges cannot be empty. */
6809 if (CASE_HIGH (t
) != NULL
6810 && tree_int_cst_compare (CASE_HIGH (t
), val
) >= 0)
6822 /* Searches the case label vector VEC for the range of CASE_LABELs that is used
6823 for values between MIN and MAX. The first index is placed in MIN_IDX. The
6824 last index is placed in MAX_IDX. If the range of CASE_LABELs is empty
6825 then MAX_IDX < MIN_IDX.
6826 Returns true if the default label is not needed. */
6829 find_case_label_range (gimple stmt
, tree min
, tree max
, size_t *min_idx
,
6833 bool min_take_default
= !find_case_label_index (stmt
, 1, min
, &i
);
6834 bool max_take_default
= !find_case_label_index (stmt
, i
, max
, &j
);
6838 && max_take_default
)
6840 /* Only the default case label reached.
6841 Return an empty range. */
6848 bool take_default
= min_take_default
|| max_take_default
;
6852 if (max_take_default
)
6855 /* If the case label range is continuous, we do not need
6856 the default case label. Verify that. */
6857 high
= CASE_LOW (gimple_switch_label (stmt
, i
));
6858 if (CASE_HIGH (gimple_switch_label (stmt
, i
)))
6859 high
= CASE_HIGH (gimple_switch_label (stmt
, i
));
6860 for (k
= i
+ 1; k
<= j
; ++k
)
6862 low
= CASE_LOW (gimple_switch_label (stmt
, k
));
6863 if (!integer_onep (int_const_binop (MINUS_EXPR
, low
, high
)))
6865 take_default
= true;
6869 if (CASE_HIGH (gimple_switch_label (stmt
, k
)))
6870 high
= CASE_HIGH (gimple_switch_label (stmt
, k
));
6875 return !take_default
;
6879 /* Searches the case label vector VEC for the ranges of CASE_LABELs that are
6880 used in range VR. The indices are placed in MIN_IDX1, MAX_IDX, MIN_IDX2 and
6881 MAX_IDX2. If the ranges of CASE_LABELs are empty then MAX_IDX1 < MIN_IDX1.
6882 Returns true if the default label is not needed. */
6885 find_case_label_ranges (gimple stmt
, value_range_t
*vr
, size_t *min_idx1
,
6886 size_t *max_idx1
, size_t *min_idx2
,
6890 unsigned int n
= gimple_switch_num_labels (stmt
);
6892 tree case_low
, case_high
;
6893 tree min
= vr
->min
, max
= vr
->max
;
6895 gcc_checking_assert (vr
->type
== VR_RANGE
|| vr
->type
== VR_ANTI_RANGE
);
6897 take_default
= !find_case_label_range (stmt
, min
, max
, &i
, &j
);
6899 /* Set second range to emtpy. */
6903 if (vr
->type
== VR_RANGE
)
6907 return !take_default
;
6910 /* Set first range to all case labels. */
6917 /* Make sure all the values of case labels [i , j] are contained in
6918 range [MIN, MAX]. */
6919 case_low
= CASE_LOW (gimple_switch_label (stmt
, i
));
6920 case_high
= CASE_HIGH (gimple_switch_label (stmt
, j
));
6921 if (tree_int_cst_compare (case_low
, min
) < 0)
6923 if (case_high
!= NULL_TREE
6924 && tree_int_cst_compare (max
, case_high
) < 0)
6930 /* If the range spans case labels [i, j], the corresponding anti-range spans
6931 the labels [1, i - 1] and [j + 1, n - 1]. */
6957 /* Visit switch statement STMT. If we can determine which edge
6958 will be taken out of STMT's basic block, record it in
6959 *TAKEN_EDGE_P and return SSA_PROP_INTERESTING. Otherwise, return
6960 SSA_PROP_VARYING. */
6962 static enum ssa_prop_result
6963 vrp_visit_switch_stmt (gimple stmt
, edge
*taken_edge_p
)
6967 size_t i
= 0, j
= 0, k
, l
;
6970 *taken_edge_p
= NULL
;
6971 op
= gimple_switch_index (stmt
);
6972 if (TREE_CODE (op
) != SSA_NAME
)
6973 return SSA_PROP_VARYING
;
6975 vr
= get_value_range (op
);
6976 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6978 fprintf (dump_file
, "\nVisiting switch expression with operand ");
6979 print_generic_expr (dump_file
, op
, 0);
6980 fprintf (dump_file
, " with known range ");
6981 dump_value_range (dump_file
, vr
);
6982 fprintf (dump_file
, "\n");
6985 if ((vr
->type
!= VR_RANGE
6986 && vr
->type
!= VR_ANTI_RANGE
)
6987 || symbolic_range_p (vr
))
6988 return SSA_PROP_VARYING
;
6990 /* Find the single edge that is taken from the switch expression. */
6991 take_default
= !find_case_label_ranges (stmt
, vr
, &i
, &j
, &k
, &l
);
6993 /* Check if the range spans no CASE_LABEL. If so, we only reach the default
6997 gcc_assert (take_default
);
6998 val
= gimple_switch_default_label (stmt
);
7002 /* Check if labels with index i to j and maybe the default label
7003 are all reaching the same label. */
7005 val
= gimple_switch_label (stmt
, i
);
7007 && CASE_LABEL (gimple_switch_default_label (stmt
))
7008 != CASE_LABEL (val
))
7010 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7011 fprintf (dump_file
, " not a single destination for this "
7013 return SSA_PROP_VARYING
;
7015 for (++i
; i
<= j
; ++i
)
7017 if (CASE_LABEL (gimple_switch_label (stmt
, i
)) != CASE_LABEL (val
))
7019 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7020 fprintf (dump_file
, " not a single destination for this "
7022 return SSA_PROP_VARYING
;
7027 if (CASE_LABEL (gimple_switch_label (stmt
, k
)) != CASE_LABEL (val
))
7029 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7030 fprintf (dump_file
, " not a single destination for this "
7032 return SSA_PROP_VARYING
;
7037 *taken_edge_p
= find_edge (gimple_bb (stmt
),
7038 label_to_block (CASE_LABEL (val
)));
7040 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7042 fprintf (dump_file
, " will take edge to ");
7043 print_generic_stmt (dump_file
, CASE_LABEL (val
), 0);
7046 return SSA_PROP_INTERESTING
;
7050 /* Evaluate statement STMT. If the statement produces a useful range,
7051 return SSA_PROP_INTERESTING and record the SSA name with the
7052 interesting range into *OUTPUT_P.
7054 If STMT is a conditional branch and we can determine its truth
7055 value, the taken edge is recorded in *TAKEN_EDGE_P.
7057 If STMT produces a varying value, return SSA_PROP_VARYING. */
7059 static enum ssa_prop_result
7060 vrp_visit_stmt (gimple stmt
, edge
*taken_edge_p
, tree
*output_p
)
7065 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7067 fprintf (dump_file
, "\nVisiting statement:\n");
7068 print_gimple_stmt (dump_file
, stmt
, 0, dump_flags
);
7069 fprintf (dump_file
, "\n");
7072 if (!stmt_interesting_for_vrp (stmt
))
7073 gcc_assert (stmt_ends_bb_p (stmt
));
7074 else if (is_gimple_assign (stmt
) || is_gimple_call (stmt
))
7076 /* In general, assignments with virtual operands are not useful
7077 for deriving ranges, with the obvious exception of calls to
7078 builtin functions. */
7079 if ((is_gimple_call (stmt
)
7080 && gimple_call_fndecl (stmt
) != NULL_TREE
7081 && DECL_BUILT_IN (gimple_call_fndecl (stmt
)))
7082 || !gimple_vuse (stmt
))
7083 return vrp_visit_assignment_or_call (stmt
, output_p
);
7085 else if (gimple_code (stmt
) == GIMPLE_COND
)
7086 return vrp_visit_cond_stmt (stmt
, taken_edge_p
);
7087 else if (gimple_code (stmt
) == GIMPLE_SWITCH
)
7088 return vrp_visit_switch_stmt (stmt
, taken_edge_p
);
7090 /* All other statements produce nothing of interest for VRP, so mark
7091 their outputs varying and prevent further simulation. */
7092 FOR_EACH_SSA_TREE_OPERAND (def
, stmt
, iter
, SSA_OP_DEF
)
7093 set_value_range_to_varying (get_value_range (def
));
7095 return SSA_PROP_VARYING
;
7098 /* Union the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
7099 { VR1TYPE, VR0MIN, VR0MAX } and store the result
7100 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
7101 possible such range. The resulting range is not canonicalized. */
7104 union_ranges (enum value_range_type
*vr0type
,
7105 tree
*vr0min
, tree
*vr0max
,
7106 enum value_range_type vr1type
,
7107 tree vr1min
, tree vr1max
)
7109 bool mineq
= operand_equal_p (*vr0min
, vr1min
, 0);
7110 bool maxeq
= operand_equal_p (*vr0max
, vr1max
, 0);
7112 /* [] is vr0, () is vr1 in the following classification comments. */
7116 if (*vr0type
== vr1type
)
7117 /* Nothing to do for equal ranges. */
7119 else if ((*vr0type
== VR_RANGE
7120 && vr1type
== VR_ANTI_RANGE
)
7121 || (*vr0type
== VR_ANTI_RANGE
7122 && vr1type
== VR_RANGE
))
7124 /* For anti-range with range union the result is varying. */
7130 else if (operand_less_p (*vr0max
, vr1min
) == 1
7131 || operand_less_p (vr1max
, *vr0min
) == 1)
7133 /* [ ] ( ) or ( ) [ ]
7134 If the ranges have an empty intersection, result of the union
7135 operation is the anti-range or if both are anti-ranges
7137 if (*vr0type
== VR_ANTI_RANGE
7138 && vr1type
== VR_ANTI_RANGE
)
7140 else if (*vr0type
== VR_ANTI_RANGE
7141 && vr1type
== VR_RANGE
)
7143 else if (*vr0type
== VR_RANGE
7144 && vr1type
== VR_ANTI_RANGE
)
7150 else if (*vr0type
== VR_RANGE
7151 && vr1type
== VR_RANGE
)
7153 /* The result is the convex hull of both ranges. */
7154 if (operand_less_p (*vr0max
, vr1min
) == 1)
7156 /* If the result can be an anti-range, create one. */
7157 if (TREE_CODE (*vr0max
) == INTEGER_CST
7158 && TREE_CODE (vr1min
) == INTEGER_CST
7159 && vrp_val_is_min (*vr0min
)
7160 && vrp_val_is_max (vr1max
))
7162 tree min
= int_const_binop (PLUS_EXPR
,
7163 *vr0max
, integer_one_node
);
7164 tree max
= int_const_binop (MINUS_EXPR
,
7165 vr1min
, integer_one_node
);
7166 if (!operand_less_p (max
, min
))
7168 *vr0type
= VR_ANTI_RANGE
;
7180 /* If the result can be an anti-range, create one. */
7181 if (TREE_CODE (vr1max
) == INTEGER_CST
7182 && TREE_CODE (*vr0min
) == INTEGER_CST
7183 && vrp_val_is_min (vr1min
)
7184 && vrp_val_is_max (*vr0max
))
7186 tree min
= int_const_binop (PLUS_EXPR
,
7187 vr1max
, integer_one_node
);
7188 tree max
= int_const_binop (MINUS_EXPR
,
7189 *vr0min
, integer_one_node
);
7190 if (!operand_less_p (max
, min
))
7192 *vr0type
= VR_ANTI_RANGE
;
7206 else if ((maxeq
|| operand_less_p (vr1max
, *vr0max
) == 1)
7207 && (mineq
|| operand_less_p (*vr0min
, vr1min
) == 1))
7209 /* [ ( ) ] or [( ) ] or [ ( )] */
7210 if (*vr0type
== VR_RANGE
7211 && vr1type
== VR_RANGE
)
7213 else if (*vr0type
== VR_ANTI_RANGE
7214 && vr1type
== VR_ANTI_RANGE
)
7220 else if (*vr0type
== VR_ANTI_RANGE
7221 && vr1type
== VR_RANGE
)
7223 /* Arbitrarily choose the right or left gap. */
7224 if (!mineq
&& TREE_CODE (vr1min
) == INTEGER_CST
)
7225 *vr0max
= int_const_binop (MINUS_EXPR
, vr1min
, integer_one_node
);
7226 else if (!maxeq
&& TREE_CODE (vr1max
) == INTEGER_CST
)
7227 *vr0min
= int_const_binop (PLUS_EXPR
, vr1max
, integer_one_node
);
7231 else if (*vr0type
== VR_RANGE
7232 && vr1type
== VR_ANTI_RANGE
)
7233 /* The result covers everything. */
7238 else if ((maxeq
|| operand_less_p (*vr0max
, vr1max
) == 1)
7239 && (mineq
|| operand_less_p (vr1min
, *vr0min
) == 1))
7241 /* ( [ ] ) or ([ ] ) or ( [ ]) */
7242 if (*vr0type
== VR_RANGE
7243 && vr1type
== VR_RANGE
)
7249 else if (*vr0type
== VR_ANTI_RANGE
7250 && vr1type
== VR_ANTI_RANGE
)
7252 else if (*vr0type
== VR_RANGE
7253 && vr1type
== VR_ANTI_RANGE
)
7255 *vr0type
= VR_ANTI_RANGE
;
7256 if (!mineq
&& TREE_CODE (*vr0min
) == INTEGER_CST
)
7258 *vr0max
= int_const_binop (MINUS_EXPR
, *vr0min
, integer_one_node
);
7261 else if (!maxeq
&& TREE_CODE (*vr0max
) == INTEGER_CST
)
7263 *vr0min
= int_const_binop (PLUS_EXPR
, *vr0max
, integer_one_node
);
7269 else if (*vr0type
== VR_ANTI_RANGE
7270 && vr1type
== VR_RANGE
)
7271 /* The result covers everything. */
7276 else if ((operand_less_p (vr1min
, *vr0max
) == 1
7277 || operand_equal_p (vr1min
, *vr0max
, 0))
7278 && operand_less_p (*vr0min
, vr1min
) == 1)
7280 /* [ ( ] ) or [ ]( ) */
7281 if (*vr0type
== VR_RANGE
7282 && vr1type
== VR_RANGE
)
7284 else if (*vr0type
== VR_ANTI_RANGE
7285 && vr1type
== VR_ANTI_RANGE
)
7287 else if (*vr0type
== VR_ANTI_RANGE
7288 && vr1type
== VR_RANGE
)
7290 if (TREE_CODE (vr1min
) == INTEGER_CST
)
7291 *vr0max
= int_const_binop (MINUS_EXPR
, vr1min
, integer_one_node
);
7295 else if (*vr0type
== VR_RANGE
7296 && vr1type
== VR_ANTI_RANGE
)
7298 if (TREE_CODE (*vr0max
) == INTEGER_CST
)
7301 *vr0min
= int_const_binop (PLUS_EXPR
, *vr0max
, integer_one_node
);
7310 else if ((operand_less_p (*vr0min
, vr1max
) == 1
7311 || operand_equal_p (*vr0min
, vr1max
, 0))
7312 && operand_less_p (vr1min
, *vr0min
) == 1)
7314 /* ( [ ) ] or ( )[ ] */
7315 if (*vr0type
== VR_RANGE
7316 && vr1type
== VR_RANGE
)
7318 else if (*vr0type
== VR_ANTI_RANGE
7319 && vr1type
== VR_ANTI_RANGE
)
7321 else if (*vr0type
== VR_ANTI_RANGE
7322 && vr1type
== VR_RANGE
)
7324 if (TREE_CODE (vr1max
) == INTEGER_CST
)
7325 *vr0min
= int_const_binop (PLUS_EXPR
, vr1max
, integer_one_node
);
7329 else if (*vr0type
== VR_RANGE
7330 && vr1type
== VR_ANTI_RANGE
)
7332 if (TREE_CODE (*vr0min
) == INTEGER_CST
)
7336 *vr0max
= int_const_binop (MINUS_EXPR
, *vr0min
, integer_one_node
);
7350 *vr0type
= VR_VARYING
;
7351 *vr0min
= NULL_TREE
;
7352 *vr0max
= NULL_TREE
;
7355 /* Intersect the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
7356 { VR1TYPE, VR0MIN, VR0MAX } and store the result
7357 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
7358 possible such range. The resulting range is not canonicalized. */
7361 intersect_ranges (enum value_range_type
*vr0type
,
7362 tree
*vr0min
, tree
*vr0max
,
7363 enum value_range_type vr1type
,
7364 tree vr1min
, tree vr1max
)
7366 bool mineq
= operand_equal_p (*vr0min
, vr1min
, 0);
7367 bool maxeq
= operand_equal_p (*vr0max
, vr1max
, 0);
7369 /* [] is vr0, () is vr1 in the following classification comments. */
7373 if (*vr0type
== vr1type
)
7374 /* Nothing to do for equal ranges. */
7376 else if ((*vr0type
== VR_RANGE
7377 && vr1type
== VR_ANTI_RANGE
)
7378 || (*vr0type
== VR_ANTI_RANGE
7379 && vr1type
== VR_RANGE
))
7381 /* For anti-range with range intersection the result is empty. */
7382 *vr0type
= VR_UNDEFINED
;
7383 *vr0min
= NULL_TREE
;
7384 *vr0max
= NULL_TREE
;
7389 else if (operand_less_p (*vr0max
, vr1min
) == 1
7390 || operand_less_p (vr1max
, *vr0min
) == 1)
7392 /* [ ] ( ) or ( ) [ ]
7393 If the ranges have an empty intersection, the result of the
7394 intersect operation is the range for intersecting an
7395 anti-range with a range or empty when intersecting two ranges. */
7396 if (*vr0type
== VR_RANGE
7397 && vr1type
== VR_ANTI_RANGE
)
7399 else if (*vr0type
== VR_ANTI_RANGE
7400 && vr1type
== VR_RANGE
)
7406 else if (*vr0type
== VR_RANGE
7407 && vr1type
== VR_RANGE
)
7409 *vr0type
= VR_UNDEFINED
;
7410 *vr0min
= NULL_TREE
;
7411 *vr0max
= NULL_TREE
;
7413 else if (*vr0type
== VR_ANTI_RANGE
7414 && vr1type
== VR_ANTI_RANGE
)
7416 /* If the anti-ranges are adjacent to each other merge them. */
7417 if (TREE_CODE (*vr0max
) == INTEGER_CST
7418 && TREE_CODE (vr1min
) == INTEGER_CST
7419 && operand_less_p (*vr0max
, vr1min
) == 1
7420 && integer_onep (int_const_binop (MINUS_EXPR
,
7423 else if (TREE_CODE (vr1max
) == INTEGER_CST
7424 && TREE_CODE (*vr0min
) == INTEGER_CST
7425 && operand_less_p (vr1max
, *vr0min
) == 1
7426 && integer_onep (int_const_binop (MINUS_EXPR
,
7429 /* Else arbitrarily take VR0. */
7432 else if ((maxeq
|| operand_less_p (vr1max
, *vr0max
) == 1)
7433 && (mineq
|| operand_less_p (*vr0min
, vr1min
) == 1))
7435 /* [ ( ) ] or [( ) ] or [ ( )] */
7436 if (*vr0type
== VR_RANGE
7437 && vr1type
== VR_RANGE
)
7439 /* If both are ranges the result is the inner one. */
7444 else if (*vr0type
== VR_RANGE
7445 && vr1type
== VR_ANTI_RANGE
)
7447 /* Choose the right gap if the left one is empty. */
7450 if (TREE_CODE (vr1max
) == INTEGER_CST
)
7451 *vr0min
= int_const_binop (PLUS_EXPR
, vr1max
, integer_one_node
);
7455 /* Choose the left gap if the right one is empty. */
7458 if (TREE_CODE (vr1min
) == INTEGER_CST
)
7459 *vr0max
= int_const_binop (MINUS_EXPR
, vr1min
,
7464 /* Choose the anti-range if the range is effectively varying. */
7465 else if (vrp_val_is_min (*vr0min
)
7466 && vrp_val_is_max (*vr0max
))
7472 /* Else choose the range. */
7474 else if (*vr0type
== VR_ANTI_RANGE
7475 && vr1type
== VR_ANTI_RANGE
)
7476 /* If both are anti-ranges the result is the outer one. */
7478 else if (*vr0type
== VR_ANTI_RANGE
7479 && vr1type
== VR_RANGE
)
7481 /* The intersection is empty. */
7482 *vr0type
= VR_UNDEFINED
;
7483 *vr0min
= NULL_TREE
;
7484 *vr0max
= NULL_TREE
;
7489 else if ((maxeq
|| operand_less_p (*vr0max
, vr1max
) == 1)
7490 && (mineq
|| operand_less_p (vr1min
, *vr0min
) == 1))
7492 /* ( [ ] ) or ([ ] ) or ( [ ]) */
7493 if (*vr0type
== VR_RANGE
7494 && vr1type
== VR_RANGE
)
7495 /* Choose the inner range. */
7497 else if (*vr0type
== VR_ANTI_RANGE
7498 && vr1type
== VR_RANGE
)
7500 /* Choose the right gap if the left is empty. */
7503 *vr0type
= VR_RANGE
;
7504 if (TREE_CODE (*vr0max
) == INTEGER_CST
)
7505 *vr0min
= int_const_binop (PLUS_EXPR
, *vr0max
,
7511 /* Choose the left gap if the right is empty. */
7514 *vr0type
= VR_RANGE
;
7515 if (TREE_CODE (*vr0min
) == INTEGER_CST
)
7516 *vr0max
= int_const_binop (MINUS_EXPR
, *vr0min
,
7522 /* Choose the anti-range if the range is effectively varying. */
7523 else if (vrp_val_is_min (vr1min
)
7524 && vrp_val_is_max (vr1max
))
7526 /* Else choose the range. */
7534 else if (*vr0type
== VR_ANTI_RANGE
7535 && vr1type
== VR_ANTI_RANGE
)
7537 /* If both are anti-ranges the result is the outer one. */
7542 else if (vr1type
== VR_ANTI_RANGE
7543 && *vr0type
== VR_RANGE
)
7545 /* The intersection is empty. */
7546 *vr0type
= VR_UNDEFINED
;
7547 *vr0min
= NULL_TREE
;
7548 *vr0max
= NULL_TREE
;
7553 else if ((operand_less_p (vr1min
, *vr0max
) == 1
7554 || operand_equal_p (vr1min
, *vr0max
, 0))
7555 && operand_less_p (*vr0min
, vr1min
) == 1)
7557 /* [ ( ] ) or [ ]( ) */
7558 if (*vr0type
== VR_ANTI_RANGE
7559 && vr1type
== VR_ANTI_RANGE
)
7561 else if (*vr0type
== VR_RANGE
7562 && vr1type
== VR_RANGE
)
7564 else if (*vr0type
== VR_RANGE
7565 && vr1type
== VR_ANTI_RANGE
)
7567 if (TREE_CODE (vr1min
) == INTEGER_CST
)
7568 *vr0max
= int_const_binop (MINUS_EXPR
, vr1min
,
7573 else if (*vr0type
== VR_ANTI_RANGE
7574 && vr1type
== VR_RANGE
)
7576 *vr0type
= VR_RANGE
;
7577 if (TREE_CODE (*vr0max
) == INTEGER_CST
)
7578 *vr0min
= int_const_binop (PLUS_EXPR
, *vr0max
,
7587 else if ((operand_less_p (*vr0min
, vr1max
) == 1
7588 || operand_equal_p (*vr0min
, vr1max
, 0))
7589 && operand_less_p (vr1min
, *vr0min
) == 1)
7591 /* ( [ ) ] or ( )[ ] */
7592 if (*vr0type
== VR_ANTI_RANGE
7593 && vr1type
== VR_ANTI_RANGE
)
7595 else if (*vr0type
== VR_RANGE
7596 && vr1type
== VR_RANGE
)
7598 else if (*vr0type
== VR_RANGE
7599 && vr1type
== VR_ANTI_RANGE
)
7601 if (TREE_CODE (vr1max
) == INTEGER_CST
)
7602 *vr0min
= int_const_binop (PLUS_EXPR
, vr1max
,
7607 else if (*vr0type
== VR_ANTI_RANGE
7608 && vr1type
== VR_RANGE
)
7610 *vr0type
= VR_RANGE
;
7611 if (TREE_CODE (*vr0min
) == INTEGER_CST
)
7612 *vr0max
= int_const_binop (MINUS_EXPR
, *vr0min
,
7622 /* As a fallback simply use { *VRTYPE, *VR0MIN, *VR0MAX } as
7623 result for the intersection. That's always a conservative
7624 correct estimate. */
7630 /* Intersect the two value-ranges *VR0 and *VR1 and store the result
7631 in *VR0. This may not be the smallest possible such range. */
7634 vrp_intersect_ranges_1 (value_range_t
*vr0
, value_range_t
*vr1
)
7636 value_range_t saved
;
7638 /* If either range is VR_VARYING the other one wins. */
7639 if (vr1
->type
== VR_VARYING
)
7641 if (vr0
->type
== VR_VARYING
)
7643 copy_value_range (vr0
, vr1
);
7647 /* When either range is VR_UNDEFINED the resulting range is
7648 VR_UNDEFINED, too. */
7649 if (vr0
->type
== VR_UNDEFINED
)
7651 if (vr1
->type
== VR_UNDEFINED
)
7653 set_value_range_to_undefined (vr0
);
7657 /* Save the original vr0 so we can return it as conservative intersection
7658 result when our worker turns things to varying. */
7660 intersect_ranges (&vr0
->type
, &vr0
->min
, &vr0
->max
,
7661 vr1
->type
, vr1
->min
, vr1
->max
);
7662 /* Make sure to canonicalize the result though as the inversion of a
7663 VR_RANGE can still be a VR_RANGE. */
7664 set_and_canonicalize_value_range (vr0
, vr0
->type
,
7665 vr0
->min
, vr0
->max
, vr0
->equiv
);
7666 /* If that failed, use the saved original VR0. */
7667 if (vr0
->type
== VR_VARYING
)
7672 /* If the result is VR_UNDEFINED there is no need to mess with
7673 the equivalencies. */
7674 if (vr0
->type
== VR_UNDEFINED
)
7677 /* The resulting set of equivalences for range intersection is the union of
7679 if (vr0
->equiv
&& vr1
->equiv
&& vr0
->equiv
!= vr1
->equiv
)
7680 bitmap_ior_into (vr0
->equiv
, vr1
->equiv
);
7681 else if (vr1
->equiv
&& !vr0
->equiv
)
7682 bitmap_copy (vr0
->equiv
, vr1
->equiv
);
7686 vrp_intersect_ranges (value_range_t
*vr0
, value_range_t
*vr1
)
7688 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7690 fprintf (dump_file
, "Intersecting\n ");
7691 dump_value_range (dump_file
, vr0
);
7692 fprintf (dump_file
, "\nand\n ");
7693 dump_value_range (dump_file
, vr1
);
7694 fprintf (dump_file
, "\n");
7696 vrp_intersect_ranges_1 (vr0
, vr1
);
7697 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7699 fprintf (dump_file
, "to\n ");
7700 dump_value_range (dump_file
, vr0
);
7701 fprintf (dump_file
, "\n");
7705 /* Meet operation for value ranges. Given two value ranges VR0 and
7706 VR1, store in VR0 a range that contains both VR0 and VR1. This
7707 may not be the smallest possible such range. */
7710 vrp_meet_1 (value_range_t
*vr0
, value_range_t
*vr1
)
7712 value_range_t saved
;
7714 if (vr0
->type
== VR_UNDEFINED
)
7716 /* Drop equivalences. See PR53465. */
7717 set_value_range (vr0
, vr1
->type
, vr1
->min
, vr1
->max
, NULL
);
7721 if (vr1
->type
== VR_UNDEFINED
)
7723 /* VR0 already has the resulting range, just drop equivalences.
7726 bitmap_clear (vr0
->equiv
);
7730 if (vr0
->type
== VR_VARYING
)
7732 /* Nothing to do. VR0 already has the resulting range. */
7736 if (vr1
->type
== VR_VARYING
)
7738 set_value_range_to_varying (vr0
);
7743 union_ranges (&vr0
->type
, &vr0
->min
, &vr0
->max
,
7744 vr1
->type
, vr1
->min
, vr1
->max
);
7745 if (vr0
->type
== VR_VARYING
)
7747 /* Failed to find an efficient meet. Before giving up and setting
7748 the result to VARYING, see if we can at least derive a useful
7749 anti-range. FIXME, all this nonsense about distinguishing
7750 anti-ranges from ranges is necessary because of the odd
7751 semantics of range_includes_zero_p and friends. */
7752 if (((saved
.type
== VR_RANGE
7753 && range_includes_zero_p (saved
.min
, saved
.max
) == 0)
7754 || (saved
.type
== VR_ANTI_RANGE
7755 && range_includes_zero_p (saved
.min
, saved
.max
) == 1))
7756 && ((vr1
->type
== VR_RANGE
7757 && range_includes_zero_p (vr1
->min
, vr1
->max
) == 0)
7758 || (vr1
->type
== VR_ANTI_RANGE
7759 && range_includes_zero_p (vr1
->min
, vr1
->max
) == 1)))
7761 set_value_range_to_nonnull (vr0
, TREE_TYPE (saved
.min
));
7763 /* Since this meet operation did not result from the meeting of
7764 two equivalent names, VR0 cannot have any equivalences. */
7766 bitmap_clear (vr0
->equiv
);
7770 set_value_range_to_varying (vr0
);
7773 set_and_canonicalize_value_range (vr0
, vr0
->type
, vr0
->min
, vr0
->max
,
7775 if (vr0
->type
== VR_VARYING
)
7778 /* The resulting set of equivalences is always the intersection of
7780 if (vr0
->equiv
&& vr1
->equiv
&& vr0
->equiv
!= vr1
->equiv
)
7781 bitmap_and_into (vr0
->equiv
, vr1
->equiv
);
7782 else if (vr0
->equiv
&& !vr1
->equiv
)
7783 bitmap_clear (vr0
->equiv
);
7787 vrp_meet (value_range_t
*vr0
, value_range_t
*vr1
)
7789 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7791 fprintf (dump_file
, "Meeting\n ");
7792 dump_value_range (dump_file
, vr0
);
7793 fprintf (dump_file
, "\nand\n ");
7794 dump_value_range (dump_file
, vr1
);
7795 fprintf (dump_file
, "\n");
7797 vrp_meet_1 (vr0
, vr1
);
7798 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7800 fprintf (dump_file
, "to\n ");
7801 dump_value_range (dump_file
, vr0
);
7802 fprintf (dump_file
, "\n");
7807 /* Visit all arguments for PHI node PHI that flow through executable
7808 edges. If a valid value range can be derived from all the incoming
7809 value ranges, set a new range for the LHS of PHI. */
7811 static enum ssa_prop_result
7812 vrp_visit_phi_node (gimple phi
)
7815 tree lhs
= PHI_RESULT (phi
);
7816 value_range_t
*lhs_vr
= get_value_range (lhs
);
7817 value_range_t vr_result
= VR_INITIALIZER
;
7819 int edges
, old_edges
;
7822 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7824 fprintf (dump_file
, "\nVisiting PHI node: ");
7825 print_gimple_stmt (dump_file
, phi
, 0, dump_flags
);
7829 for (i
= 0; i
< gimple_phi_num_args (phi
); i
++)
7831 edge e
= gimple_phi_arg_edge (phi
, i
);
7833 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7836 "\n Argument #%d (%d -> %d %sexecutable)\n",
7837 (int) i
, e
->src
->index
, e
->dest
->index
,
7838 (e
->flags
& EDGE_EXECUTABLE
) ? "" : "not ");
7841 if (e
->flags
& EDGE_EXECUTABLE
)
7843 tree arg
= PHI_ARG_DEF (phi
, i
);
7844 value_range_t vr_arg
;
7848 if (TREE_CODE (arg
) == SSA_NAME
)
7850 vr_arg
= *(get_value_range (arg
));
7854 if (is_overflow_infinity (arg
))
7856 arg
= copy_node (arg
);
7857 TREE_OVERFLOW (arg
) = 0;
7860 vr_arg
.type
= VR_RANGE
;
7863 vr_arg
.equiv
= NULL
;
7866 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7868 fprintf (dump_file
, "\t");
7869 print_generic_expr (dump_file
, arg
, dump_flags
);
7870 fprintf (dump_file
, "\n\tValue: ");
7871 dump_value_range (dump_file
, &vr_arg
);
7872 fprintf (dump_file
, "\n");
7876 copy_value_range (&vr_result
, &vr_arg
);
7878 vrp_meet (&vr_result
, &vr_arg
);
7881 if (vr_result
.type
== VR_VARYING
)
7886 if (vr_result
.type
== VR_VARYING
)
7888 else if (vr_result
.type
== VR_UNDEFINED
)
7891 old_edges
= vr_phi_edge_counts
[SSA_NAME_VERSION (lhs
)];
7892 vr_phi_edge_counts
[SSA_NAME_VERSION (lhs
)] = edges
;
7894 /* To prevent infinite iterations in the algorithm, derive ranges
7895 when the new value is slightly bigger or smaller than the
7896 previous one. We don't do this if we have seen a new executable
7897 edge; this helps us avoid an overflow infinity for conditionals
7898 which are not in a loop. If the old value-range was VR_UNDEFINED
7899 use the updated range and iterate one more time. */
7901 && gimple_phi_num_args (phi
) > 1
7902 && edges
== old_edges
7903 && lhs_vr
->type
!= VR_UNDEFINED
)
7905 int cmp_min
= compare_values (lhs_vr
->min
, vr_result
.min
);
7906 int cmp_max
= compare_values (lhs_vr
->max
, vr_result
.max
);
7908 /* For non VR_RANGE or for pointers fall back to varying if
7909 the range changed. */
7910 if ((lhs_vr
->type
!= VR_RANGE
|| vr_result
.type
!= VR_RANGE
7911 || POINTER_TYPE_P (TREE_TYPE (lhs
)))
7912 && (cmp_min
!= 0 || cmp_max
!= 0))
7915 /* If the new minimum is smaller or larger than the previous
7916 one, go all the way to -INF. In the first case, to avoid
7917 iterating millions of times to reach -INF, and in the
7918 other case to avoid infinite bouncing between different
7920 if (cmp_min
> 0 || cmp_min
< 0)
7922 if (!needs_overflow_infinity (TREE_TYPE (vr_result
.min
))
7923 || !vrp_var_may_overflow (lhs
, phi
))
7924 vr_result
.min
= TYPE_MIN_VALUE (TREE_TYPE (vr_result
.min
));
7925 else if (supports_overflow_infinity (TREE_TYPE (vr_result
.min
)))
7927 negative_overflow_infinity (TREE_TYPE (vr_result
.min
));
7930 /* Similarly, if the new maximum is smaller or larger than
7931 the previous one, go all the way to +INF. */
7932 if (cmp_max
< 0 || cmp_max
> 0)
7934 if (!needs_overflow_infinity (TREE_TYPE (vr_result
.max
))
7935 || !vrp_var_may_overflow (lhs
, phi
))
7936 vr_result
.max
= TYPE_MAX_VALUE (TREE_TYPE (vr_result
.max
));
7937 else if (supports_overflow_infinity (TREE_TYPE (vr_result
.max
)))
7939 positive_overflow_infinity (TREE_TYPE (vr_result
.max
));
7942 /* If we dropped either bound to +-INF then if this is a loop
7943 PHI node SCEV may known more about its value-range. */
7944 if ((cmp_min
> 0 || cmp_min
< 0
7945 || cmp_max
< 0 || cmp_max
> 0)
7947 && (l
= loop_containing_stmt (phi
))
7948 && l
->header
== gimple_bb (phi
))
7949 adjust_range_with_scev (&vr_result
, l
, phi
, lhs
);
7951 /* If we will end up with a (-INF, +INF) range, set it to
7952 VARYING. Same if the previous max value was invalid for
7953 the type and we end up with vr_result.min > vr_result.max. */
7954 if ((vrp_val_is_max (vr_result
.max
)
7955 && vrp_val_is_min (vr_result
.min
))
7956 || compare_values (vr_result
.min
,
7961 /* If the new range is different than the previous value, keep
7964 if (update_value_range (lhs
, &vr_result
))
7966 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7968 fprintf (dump_file
, "Found new range for ");
7969 print_generic_expr (dump_file
, lhs
, 0);
7970 fprintf (dump_file
, ": ");
7971 dump_value_range (dump_file
, &vr_result
);
7972 fprintf (dump_file
, "\n\n");
7975 return SSA_PROP_INTERESTING
;
7978 /* Nothing changed, don't add outgoing edges. */
7979 return SSA_PROP_NOT_INTERESTING
;
7981 /* No match found. Set the LHS to VARYING. */
7983 set_value_range_to_varying (lhs_vr
);
7984 return SSA_PROP_VARYING
;
7987 /* Simplify boolean operations if the source is known
7988 to be already a boolean. */
7990 simplify_truth_ops_using_ranges (gimple_stmt_iterator
*gsi
, gimple stmt
)
7992 enum tree_code rhs_code
= gimple_assign_rhs_code (stmt
);
7994 bool need_conversion
;
7996 /* We handle only !=/== case here. */
7997 gcc_assert (rhs_code
== EQ_EXPR
|| rhs_code
== NE_EXPR
);
7999 op0
= gimple_assign_rhs1 (stmt
);
8000 if (!op_with_boolean_value_range_p (op0
))
8003 op1
= gimple_assign_rhs2 (stmt
);
8004 if (!op_with_boolean_value_range_p (op1
))
8007 /* Reduce number of cases to handle to NE_EXPR. As there is no
8008 BIT_XNOR_EXPR we cannot replace A == B with a single statement. */
8009 if (rhs_code
== EQ_EXPR
)
8011 if (TREE_CODE (op1
) == INTEGER_CST
)
8012 op1
= int_const_binop (BIT_XOR_EXPR
, op1
, integer_one_node
);
8017 lhs
= gimple_assign_lhs (stmt
);
8019 = !useless_type_conversion_p (TREE_TYPE (lhs
), TREE_TYPE (op0
));
8021 /* Make sure to not sign-extend a 1-bit 1 when converting the result. */
8023 && !TYPE_UNSIGNED (TREE_TYPE (op0
))
8024 && TYPE_PRECISION (TREE_TYPE (op0
)) == 1
8025 && TYPE_PRECISION (TREE_TYPE (lhs
)) > 1)
8028 /* For A != 0 we can substitute A itself. */
8029 if (integer_zerop (op1
))
8030 gimple_assign_set_rhs_with_ops (gsi
,
8032 ? NOP_EXPR
: TREE_CODE (op0
),
8034 /* For A != B we substitute A ^ B. Either with conversion. */
8035 else if (need_conversion
)
8037 tree tem
= make_ssa_name (TREE_TYPE (op0
), NULL
);
8038 gimple newop
= gimple_build_assign_with_ops (BIT_XOR_EXPR
, tem
, op0
, op1
);
8039 gsi_insert_before (gsi
, newop
, GSI_SAME_STMT
);
8040 gimple_assign_set_rhs_with_ops (gsi
, NOP_EXPR
, tem
, NULL_TREE
);
8044 gimple_assign_set_rhs_with_ops (gsi
, BIT_XOR_EXPR
, op0
, op1
);
8045 update_stmt (gsi_stmt (*gsi
));
8050 /* Simplify a division or modulo operator to a right shift or
8051 bitwise and if the first operand is unsigned or is greater
8052 than zero and the second operand is an exact power of two. */
8055 simplify_div_or_mod_using_ranges (gimple stmt
)
8057 enum tree_code rhs_code
= gimple_assign_rhs_code (stmt
);
8059 tree op0
= gimple_assign_rhs1 (stmt
);
8060 tree op1
= gimple_assign_rhs2 (stmt
);
8061 value_range_t
*vr
= get_value_range (gimple_assign_rhs1 (stmt
));
8063 if (TYPE_UNSIGNED (TREE_TYPE (op0
)))
8065 val
= integer_one_node
;
8071 val
= compare_range_with_value (GE_EXPR
, vr
, integer_zero_node
, &sop
);
8075 && integer_onep (val
)
8076 && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC
))
8078 location_t location
;
8080 if (!gimple_has_location (stmt
))
8081 location
= input_location
;
8083 location
= gimple_location (stmt
);
8084 warning_at (location
, OPT_Wstrict_overflow
,
8085 "assuming signed overflow does not occur when "
8086 "simplifying %</%> or %<%%%> to %<>>%> or %<&%>");
8090 if (val
&& integer_onep (val
))
8094 if (rhs_code
== TRUNC_DIV_EXPR
)
8096 t
= build_int_cst (integer_type_node
, tree_log2 (op1
));
8097 gimple_assign_set_rhs_code (stmt
, RSHIFT_EXPR
);
8098 gimple_assign_set_rhs1 (stmt
, op0
);
8099 gimple_assign_set_rhs2 (stmt
, t
);
8103 t
= build_int_cst (TREE_TYPE (op1
), 1);
8104 t
= int_const_binop (MINUS_EXPR
, op1
, t
);
8105 t
= fold_convert (TREE_TYPE (op0
), t
);
8107 gimple_assign_set_rhs_code (stmt
, BIT_AND_EXPR
);
8108 gimple_assign_set_rhs1 (stmt
, op0
);
8109 gimple_assign_set_rhs2 (stmt
, t
);
8119 /* If the operand to an ABS_EXPR is >= 0, then eliminate the
8120 ABS_EXPR. If the operand is <= 0, then simplify the
8121 ABS_EXPR into a NEGATE_EXPR. */
8124 simplify_abs_using_ranges (gimple stmt
)
8127 tree op
= gimple_assign_rhs1 (stmt
);
8128 tree type
= TREE_TYPE (op
);
8129 value_range_t
*vr
= get_value_range (op
);
8131 if (TYPE_UNSIGNED (type
))
8133 val
= integer_zero_node
;
8139 val
= compare_range_with_value (LE_EXPR
, vr
, integer_zero_node
, &sop
);
8143 val
= compare_range_with_value (GE_EXPR
, vr
, integer_zero_node
,
8148 if (integer_zerop (val
))
8149 val
= integer_one_node
;
8150 else if (integer_onep (val
))
8151 val
= integer_zero_node
;
8156 && (integer_onep (val
) || integer_zerop (val
)))
8158 if (sop
&& issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC
))
8160 location_t location
;
8162 if (!gimple_has_location (stmt
))
8163 location
= input_location
;
8165 location
= gimple_location (stmt
);
8166 warning_at (location
, OPT_Wstrict_overflow
,
8167 "assuming signed overflow does not occur when "
8168 "simplifying %<abs (X)%> to %<X%> or %<-X%>");
8171 gimple_assign_set_rhs1 (stmt
, op
);
8172 if (integer_onep (val
))
8173 gimple_assign_set_rhs_code (stmt
, NEGATE_EXPR
);
8175 gimple_assign_set_rhs_code (stmt
, SSA_NAME
);
8184 /* Optimize away redundant BIT_AND_EXPR and BIT_IOR_EXPR.
8185 If all the bits that are being cleared by & are already
8186 known to be zero from VR, or all the bits that are being
8187 set by | are already known to be one from VR, the bit
8188 operation is redundant. */
8191 simplify_bit_ops_using_ranges (gimple_stmt_iterator
*gsi
, gimple stmt
)
8193 tree op0
= gimple_assign_rhs1 (stmt
);
8194 tree op1
= gimple_assign_rhs2 (stmt
);
8195 tree op
= NULL_TREE
;
8196 value_range_t vr0
= VR_INITIALIZER
;
8197 value_range_t vr1
= VR_INITIALIZER
;
8198 double_int may_be_nonzero0
, may_be_nonzero1
;
8199 double_int must_be_nonzero0
, must_be_nonzero1
;
8202 if (TREE_CODE (op0
) == SSA_NAME
)
8203 vr0
= *(get_value_range (op0
));
8204 else if (is_gimple_min_invariant (op0
))
8205 set_value_range_to_value (&vr0
, op0
, NULL
);
8209 if (TREE_CODE (op1
) == SSA_NAME
)
8210 vr1
= *(get_value_range (op1
));
8211 else if (is_gimple_min_invariant (op1
))
8212 set_value_range_to_value (&vr1
, op1
, NULL
);
8216 if (!zero_nonzero_bits_from_vr (&vr0
, &may_be_nonzero0
, &must_be_nonzero0
))
8218 if (!zero_nonzero_bits_from_vr (&vr1
, &may_be_nonzero1
, &must_be_nonzero1
))
8221 switch (gimple_assign_rhs_code (stmt
))
8224 mask
= double_int_and_not (may_be_nonzero0
, must_be_nonzero1
);
8225 if (double_int_zero_p (mask
))
8230 mask
= double_int_and_not (may_be_nonzero1
, must_be_nonzero0
);
8231 if (double_int_zero_p (mask
))
8238 mask
= double_int_and_not (may_be_nonzero0
, must_be_nonzero1
);
8239 if (double_int_zero_p (mask
))
8244 mask
= double_int_and_not (may_be_nonzero1
, must_be_nonzero0
);
8245 if (double_int_zero_p (mask
))
8255 if (op
== NULL_TREE
)
8258 gimple_assign_set_rhs_with_ops (gsi
, TREE_CODE (op
), op
, NULL
);
8259 update_stmt (gsi_stmt (*gsi
));
8263 /* We are comparing trees OP0 and OP1 using COND_CODE. OP0 has
8264 a known value range VR.
8266 If there is one and only one value which will satisfy the
8267 conditional, then return that value. Else return NULL. */
8270 test_for_singularity (enum tree_code cond_code
, tree op0
,
8271 tree op1
, value_range_t
*vr
)
8276 /* Extract minimum/maximum values which satisfy the
8277 the conditional as it was written. */
8278 if (cond_code
== LE_EXPR
|| cond_code
== LT_EXPR
)
8280 /* This should not be negative infinity; there is no overflow
8282 min
= TYPE_MIN_VALUE (TREE_TYPE (op0
));
8285 if (cond_code
== LT_EXPR
&& !is_overflow_infinity (max
))
8287 tree one
= build_int_cst (TREE_TYPE (op0
), 1);
8288 max
= fold_build2 (MINUS_EXPR
, TREE_TYPE (op0
), max
, one
);
8290 TREE_NO_WARNING (max
) = 1;
8293 else if (cond_code
== GE_EXPR
|| cond_code
== GT_EXPR
)
8295 /* This should not be positive infinity; there is no overflow
8297 max
= TYPE_MAX_VALUE (TREE_TYPE (op0
));
8300 if (cond_code
== GT_EXPR
&& !is_overflow_infinity (min
))
8302 tree one
= build_int_cst (TREE_TYPE (op0
), 1);
8303 min
= fold_build2 (PLUS_EXPR
, TREE_TYPE (op0
), min
, one
);
8305 TREE_NO_WARNING (min
) = 1;
8309 /* Now refine the minimum and maximum values using any
8310 value range information we have for op0. */
8313 if (compare_values (vr
->min
, min
) == 1)
8315 if (compare_values (vr
->max
, max
) == -1)
8318 /* If the new min/max values have converged to a single value,
8319 then there is only one value which can satisfy the condition,
8320 return that value. */
8321 if (operand_equal_p (min
, max
, 0) && is_gimple_min_invariant (min
))
8327 /* Simplify a conditional using a relational operator to an equality
8328 test if the range information indicates only one value can satisfy
8329 the original conditional. */
8332 simplify_cond_using_ranges (gimple stmt
)
8334 tree op0
= gimple_cond_lhs (stmt
);
8335 tree op1
= gimple_cond_rhs (stmt
);
8336 enum tree_code cond_code
= gimple_cond_code (stmt
);
8338 if (cond_code
!= NE_EXPR
8339 && cond_code
!= EQ_EXPR
8340 && TREE_CODE (op0
) == SSA_NAME
8341 && INTEGRAL_TYPE_P (TREE_TYPE (op0
))
8342 && is_gimple_min_invariant (op1
))
8344 value_range_t
*vr
= get_value_range (op0
);
8346 /* If we have range information for OP0, then we might be
8347 able to simplify this conditional. */
8348 if (vr
->type
== VR_RANGE
)
8350 tree new_tree
= test_for_singularity (cond_code
, op0
, op1
, vr
);
8356 fprintf (dump_file
, "Simplified relational ");
8357 print_gimple_stmt (dump_file
, stmt
, 0, 0);
8358 fprintf (dump_file
, " into ");
8361 gimple_cond_set_code (stmt
, EQ_EXPR
);
8362 gimple_cond_set_lhs (stmt
, op0
);
8363 gimple_cond_set_rhs (stmt
, new_tree
);
8369 print_gimple_stmt (dump_file
, stmt
, 0, 0);
8370 fprintf (dump_file
, "\n");
8376 /* Try again after inverting the condition. We only deal
8377 with integral types here, so no need to worry about
8378 issues with inverting FP comparisons. */
8379 cond_code
= invert_tree_comparison (cond_code
, false);
8380 new_tree
= test_for_singularity (cond_code
, op0
, op1
, vr
);
8386 fprintf (dump_file
, "Simplified relational ");
8387 print_gimple_stmt (dump_file
, stmt
, 0, 0);
8388 fprintf (dump_file
, " into ");
8391 gimple_cond_set_code (stmt
, NE_EXPR
);
8392 gimple_cond_set_lhs (stmt
, op0
);
8393 gimple_cond_set_rhs (stmt
, new_tree
);
8399 print_gimple_stmt (dump_file
, stmt
, 0, 0);
8400 fprintf (dump_file
, "\n");
8411 /* Simplify a switch statement using the value range of the switch
8415 simplify_switch_using_ranges (gimple stmt
)
8417 tree op
= gimple_switch_index (stmt
);
8422 size_t i
= 0, j
= 0, n
, n2
;
8425 size_t k
= 1, l
= 0;
8427 if (TREE_CODE (op
) == SSA_NAME
)
8429 vr
= get_value_range (op
);
8431 /* We can only handle integer ranges. */
8432 if ((vr
->type
!= VR_RANGE
8433 && vr
->type
!= VR_ANTI_RANGE
)
8434 || symbolic_range_p (vr
))
8437 /* Find case label for min/max of the value range. */
8438 take_default
= !find_case_label_ranges (stmt
, vr
, &i
, &j
, &k
, &l
);
8440 else if (TREE_CODE (op
) == INTEGER_CST
)
8442 take_default
= !find_case_label_index (stmt
, 1, op
, &i
);
8456 n
= gimple_switch_num_labels (stmt
);
8458 /* Bail out if this is just all edges taken. */
8464 /* Build a new vector of taken case labels. */
8465 vec2
= make_tree_vec (j
- i
+ 1 + l
- k
+ 1 + (int)take_default
);
8468 /* Add the default edge, if necessary. */
8470 TREE_VEC_ELT (vec2
, n2
++) = gimple_switch_default_label (stmt
);
8472 for (; i
<= j
; ++i
, ++n2
)
8473 TREE_VEC_ELT (vec2
, n2
) = gimple_switch_label (stmt
, i
);
8475 for (; k
<= l
; ++k
, ++n2
)
8476 TREE_VEC_ELT (vec2
, n2
) = gimple_switch_label (stmt
, k
);
8478 /* Mark needed edges. */
8479 for (i
= 0; i
< n2
; ++i
)
8481 e
= find_edge (gimple_bb (stmt
),
8482 label_to_block (CASE_LABEL (TREE_VEC_ELT (vec2
, i
))));
8483 e
->aux
= (void *)-1;
8486 /* Queue not needed edges for later removal. */
8487 FOR_EACH_EDGE (e
, ei
, gimple_bb (stmt
)->succs
)
8489 if (e
->aux
== (void *)-1)
8495 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8497 fprintf (dump_file
, "removing unreachable case label\n");
8499 VEC_safe_push (edge
, heap
, to_remove_edges
, e
);
8500 e
->flags
&= ~EDGE_EXECUTABLE
;
8503 /* And queue an update for the stmt. */
8506 VEC_safe_push (switch_update
, heap
, to_update_switch_stmts
, &su
);
8510 /* Simplify an integral conversion from an SSA name in STMT. */
8513 simplify_conversion_using_ranges (gimple stmt
)
8515 tree innerop
, middleop
, finaltype
;
8517 value_range_t
*innervr
;
8518 bool inner_unsigned_p
, middle_unsigned_p
, final_unsigned_p
;
8519 unsigned inner_prec
, middle_prec
, final_prec
;
8520 double_int innermin
, innermed
, innermax
, middlemin
, middlemed
, middlemax
;
8522 finaltype
= TREE_TYPE (gimple_assign_lhs (stmt
));
8523 if (!INTEGRAL_TYPE_P (finaltype
))
8525 middleop
= gimple_assign_rhs1 (stmt
);
8526 def_stmt
= SSA_NAME_DEF_STMT (middleop
);
8527 if (!is_gimple_assign (def_stmt
)
8528 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt
)))
8530 innerop
= gimple_assign_rhs1 (def_stmt
);
8531 if (TREE_CODE (innerop
) != SSA_NAME
)
8534 /* Get the value-range of the inner operand. */
8535 innervr
= get_value_range (innerop
);
8536 if (innervr
->type
!= VR_RANGE
8537 || TREE_CODE (innervr
->min
) != INTEGER_CST
8538 || TREE_CODE (innervr
->max
) != INTEGER_CST
)
8541 /* Simulate the conversion chain to check if the result is equal if
8542 the middle conversion is removed. */
8543 innermin
= tree_to_double_int (innervr
->min
);
8544 innermax
= tree_to_double_int (innervr
->max
);
8546 inner_prec
= TYPE_PRECISION (TREE_TYPE (innerop
));
8547 middle_prec
= TYPE_PRECISION (TREE_TYPE (middleop
));
8548 final_prec
= TYPE_PRECISION (finaltype
);
8550 /* If the first conversion is not injective, the second must not
8552 if (double_int_cmp (double_int_sub (innermax
, innermin
),
8553 double_int_mask (middle_prec
), true) > 0
8554 && middle_prec
< final_prec
)
8556 /* We also want a medium value so that we can track the effect that
8557 narrowing conversions with sign change have. */
8558 inner_unsigned_p
= TYPE_UNSIGNED (TREE_TYPE (innerop
));
8559 if (inner_unsigned_p
)
8560 innermed
= double_int_rshift (double_int_mask (inner_prec
),
8561 1, inner_prec
, false);
8563 innermed
= double_int_zero
;
8564 if (double_int_cmp (innermin
, innermed
, inner_unsigned_p
) >= 0
8565 || double_int_cmp (innermed
, innermax
, inner_unsigned_p
) >= 0)
8566 innermed
= innermin
;
8568 middle_unsigned_p
= TYPE_UNSIGNED (TREE_TYPE (middleop
));
8569 middlemin
= double_int_ext (innermin
, middle_prec
, middle_unsigned_p
);
8570 middlemed
= double_int_ext (innermed
, middle_prec
, middle_unsigned_p
);
8571 middlemax
= double_int_ext (innermax
, middle_prec
, middle_unsigned_p
);
8573 /* Require that the final conversion applied to both the original
8574 and the intermediate range produces the same result. */
8575 final_unsigned_p
= TYPE_UNSIGNED (finaltype
);
8576 if (!double_int_equal_p (double_int_ext (middlemin
,
8577 final_prec
, final_unsigned_p
),
8578 double_int_ext (innermin
,
8579 final_prec
, final_unsigned_p
))
8580 || !double_int_equal_p (double_int_ext (middlemed
,
8581 final_prec
, final_unsigned_p
),
8582 double_int_ext (innermed
,
8583 final_prec
, final_unsigned_p
))
8584 || !double_int_equal_p (double_int_ext (middlemax
,
8585 final_prec
, final_unsigned_p
),
8586 double_int_ext (innermax
,
8587 final_prec
, final_unsigned_p
)))
8590 gimple_assign_set_rhs1 (stmt
, innerop
);
8595 /* Return whether the value range *VR fits in an integer type specified
8596 by PRECISION and UNSIGNED_P. */
8599 range_fits_type_p (value_range_t
*vr
, unsigned precision
, bool unsigned_p
)
8602 unsigned src_precision
;
8605 /* We can only handle integral and pointer types. */
8606 src_type
= TREE_TYPE (vr
->min
);
8607 if (!INTEGRAL_TYPE_P (src_type
)
8608 && !POINTER_TYPE_P (src_type
))
8611 /* An extension is always fine, so is an identity transform. */
8612 src_precision
= TYPE_PRECISION (TREE_TYPE (vr
->min
));
8613 if (src_precision
< precision
8614 || (src_precision
== precision
8615 && TYPE_UNSIGNED (src_type
) == unsigned_p
))
8618 /* Now we can only handle ranges with constant bounds. */
8619 if (vr
->type
!= VR_RANGE
8620 || TREE_CODE (vr
->min
) != INTEGER_CST
8621 || TREE_CODE (vr
->max
) != INTEGER_CST
)
8624 /* For precision-preserving sign-changes the MSB of the double-int
8626 if (src_precision
== precision
8627 && (TREE_INT_CST_HIGH (vr
->min
) | TREE_INT_CST_HIGH (vr
->max
)) < 0)
8630 /* Then we can perform the conversion on both ends and compare
8631 the result for equality. */
8632 tem
= double_int_ext (tree_to_double_int (vr
->min
), precision
, unsigned_p
);
8633 if (!double_int_equal_p (tree_to_double_int (vr
->min
), tem
))
8635 tem
= double_int_ext (tree_to_double_int (vr
->max
), precision
, unsigned_p
);
8636 if (!double_int_equal_p (tree_to_double_int (vr
->max
), tem
))
8642 /* Simplify a conversion from integral SSA name to float in STMT. */
8645 simplify_float_conversion_using_ranges (gimple_stmt_iterator
*gsi
, gimple stmt
)
8647 tree rhs1
= gimple_assign_rhs1 (stmt
);
8648 value_range_t
*vr
= get_value_range (rhs1
);
8649 enum machine_mode fltmode
= TYPE_MODE (TREE_TYPE (gimple_assign_lhs (stmt
)));
8650 enum machine_mode mode
;
8654 /* We can only handle constant ranges. */
8655 if (vr
->type
!= VR_RANGE
8656 || TREE_CODE (vr
->min
) != INTEGER_CST
8657 || TREE_CODE (vr
->max
) != INTEGER_CST
)
8660 /* First check if we can use a signed type in place of an unsigned. */
8661 if (TYPE_UNSIGNED (TREE_TYPE (rhs1
))
8662 && (can_float_p (fltmode
, TYPE_MODE (TREE_TYPE (rhs1
)), 0)
8663 != CODE_FOR_nothing
)
8664 && range_fits_type_p (vr
, GET_MODE_PRECISION
8665 (TYPE_MODE (TREE_TYPE (rhs1
))), 0))
8666 mode
= TYPE_MODE (TREE_TYPE (rhs1
));
8667 /* If we can do the conversion in the current input mode do nothing. */
8668 else if (can_float_p (fltmode
, TYPE_MODE (TREE_TYPE (rhs1
)),
8669 TYPE_UNSIGNED (TREE_TYPE (rhs1
))))
8671 /* Otherwise search for a mode we can use, starting from the narrowest
8672 integer mode available. */
8675 mode
= GET_CLASS_NARROWEST_MODE (MODE_INT
);
8678 /* If we cannot do a signed conversion to float from mode
8679 or if the value-range does not fit in the signed type
8680 try with a wider mode. */
8681 if (can_float_p (fltmode
, mode
, 0) != CODE_FOR_nothing
8682 && range_fits_type_p (vr
, GET_MODE_PRECISION (mode
), 0))
8685 mode
= GET_MODE_WIDER_MODE (mode
);
8686 /* But do not widen the input. Instead leave that to the
8687 optabs expansion code. */
8688 if (GET_MODE_PRECISION (mode
) > TYPE_PRECISION (TREE_TYPE (rhs1
)))
8691 while (mode
!= VOIDmode
);
8692 if (mode
== VOIDmode
)
8696 /* It works, insert a truncation or sign-change before the
8697 float conversion. */
8698 tem
= make_ssa_name (build_nonstandard_integer_type
8699 (GET_MODE_PRECISION (mode
), 0), NULL
);
8700 conv
= gimple_build_assign_with_ops (NOP_EXPR
, tem
, rhs1
, NULL_TREE
);
8701 gsi_insert_before (gsi
, conv
, GSI_SAME_STMT
);
8702 gimple_assign_set_rhs1 (stmt
, tem
);
8708 /* Simplify STMT using ranges if possible. */
8711 simplify_stmt_using_ranges (gimple_stmt_iterator
*gsi
)
8713 gimple stmt
= gsi_stmt (*gsi
);
8714 if (is_gimple_assign (stmt
))
8716 enum tree_code rhs_code
= gimple_assign_rhs_code (stmt
);
8717 tree rhs1
= gimple_assign_rhs1 (stmt
);
8723 /* Transform EQ_EXPR, NE_EXPR into BIT_XOR_EXPR or identity
8724 if the RHS is zero or one, and the LHS are known to be boolean
8726 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
8727 return simplify_truth_ops_using_ranges (gsi
, stmt
);
8730 /* Transform TRUNC_DIV_EXPR and TRUNC_MOD_EXPR into RSHIFT_EXPR
8731 and BIT_AND_EXPR respectively if the first operand is greater
8732 than zero and the second operand is an exact power of two. */
8733 case TRUNC_DIV_EXPR
:
8734 case TRUNC_MOD_EXPR
:
8735 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1
))
8736 && integer_pow2p (gimple_assign_rhs2 (stmt
)))
8737 return simplify_div_or_mod_using_ranges (stmt
);
8740 /* Transform ABS (X) into X or -X as appropriate. */
8742 if (TREE_CODE (rhs1
) == SSA_NAME
8743 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
8744 return simplify_abs_using_ranges (stmt
);
8749 /* Optimize away BIT_AND_EXPR and BIT_IOR_EXPR
8750 if all the bits being cleared are already cleared or
8751 all the bits being set are already set. */
8752 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
8753 return simplify_bit_ops_using_ranges (gsi
, stmt
);
8757 if (TREE_CODE (rhs1
) == SSA_NAME
8758 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
8759 return simplify_conversion_using_ranges (stmt
);
8763 if (TREE_CODE (rhs1
) == SSA_NAME
8764 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
8765 return simplify_float_conversion_using_ranges (gsi
, stmt
);
8772 else if (gimple_code (stmt
) == GIMPLE_COND
)
8773 return simplify_cond_using_ranges (stmt
);
8774 else if (gimple_code (stmt
) == GIMPLE_SWITCH
)
8775 return simplify_switch_using_ranges (stmt
);
8780 /* If the statement pointed by SI has a predicate whose value can be
8781 computed using the value range information computed by VRP, compute
8782 its value and return true. Otherwise, return false. */
8785 fold_predicate_in (gimple_stmt_iterator
*si
)
8787 bool assignment_p
= false;
8789 gimple stmt
= gsi_stmt (*si
);
8791 if (is_gimple_assign (stmt
)
8792 && TREE_CODE_CLASS (gimple_assign_rhs_code (stmt
)) == tcc_comparison
)
8794 assignment_p
= true;
8795 val
= vrp_evaluate_conditional (gimple_assign_rhs_code (stmt
),
8796 gimple_assign_rhs1 (stmt
),
8797 gimple_assign_rhs2 (stmt
),
8800 else if (gimple_code (stmt
) == GIMPLE_COND
)
8801 val
= vrp_evaluate_conditional (gimple_cond_code (stmt
),
8802 gimple_cond_lhs (stmt
),
8803 gimple_cond_rhs (stmt
),
8811 val
= fold_convert (gimple_expr_type (stmt
), val
);
8815 fprintf (dump_file
, "Folding predicate ");
8816 print_gimple_expr (dump_file
, stmt
, 0, 0);
8817 fprintf (dump_file
, " to ");
8818 print_generic_expr (dump_file
, val
, 0);
8819 fprintf (dump_file
, "\n");
8822 if (is_gimple_assign (stmt
))
8823 gimple_assign_set_rhs_from_tree (si
, val
);
8826 gcc_assert (gimple_code (stmt
) == GIMPLE_COND
);
8827 if (integer_zerop (val
))
8828 gimple_cond_make_false (stmt
);
8829 else if (integer_onep (val
))
8830 gimple_cond_make_true (stmt
);
8841 /* Callback for substitute_and_fold folding the stmt at *SI. */
8844 vrp_fold_stmt (gimple_stmt_iterator
*si
)
8846 if (fold_predicate_in (si
))
8849 return simplify_stmt_using_ranges (si
);
8852 /* Stack of dest,src equivalency pairs that need to be restored after
8853 each attempt to thread a block's incoming edge to an outgoing edge.
8855 A NULL entry is used to mark the end of pairs which need to be
8857 static VEC(tree
,heap
) *equiv_stack
;
8859 /* A trivial wrapper so that we can present the generic jump threading
8860 code with a simple API for simplifying statements. STMT is the
8861 statement we want to simplify, WITHIN_STMT provides the location
8862 for any overflow warnings. */
8865 simplify_stmt_for_jump_threading (gimple stmt
, gimple within_stmt
)
8867 /* We only use VRP information to simplify conditionals. This is
8868 overly conservative, but it's unclear if doing more would be
8869 worth the compile time cost. */
8870 if (gimple_code (stmt
) != GIMPLE_COND
)
8873 return vrp_evaluate_conditional (gimple_cond_code (stmt
),
8874 gimple_cond_lhs (stmt
),
8875 gimple_cond_rhs (stmt
), within_stmt
);
8878 /* Blocks which have more than one predecessor and more than
8879 one successor present jump threading opportunities, i.e.,
8880 when the block is reached from a specific predecessor, we
8881 may be able to determine which of the outgoing edges will
8882 be traversed. When this optimization applies, we are able
8883 to avoid conditionals at runtime and we may expose secondary
8884 optimization opportunities.
8886 This routine is effectively a driver for the generic jump
8887 threading code. It basically just presents the generic code
8888 with edges that may be suitable for jump threading.
8890 Unlike DOM, we do not iterate VRP if jump threading was successful.
8891 While iterating may expose new opportunities for VRP, it is expected
8892 those opportunities would be very limited and the compile time cost
8893 to expose those opportunities would be significant.
8895 As jump threading opportunities are discovered, they are registered
8896 for later realization. */
8899 identify_jump_threads (void)
8906 /* Ugh. When substituting values earlier in this pass we can
8907 wipe the dominance information. So rebuild the dominator
8908 information as we need it within the jump threading code. */
8909 calculate_dominance_info (CDI_DOMINATORS
);
8911 /* We do not allow VRP information to be used for jump threading
8912 across a back edge in the CFG. Otherwise it becomes too
8913 difficult to avoid eliminating loop exit tests. Of course
8914 EDGE_DFS_BACK is not accurate at this time so we have to
8916 mark_dfs_back_edges ();
8918 /* Do not thread across edges we are about to remove. Just marking
8919 them as EDGE_DFS_BACK will do. */
8920 FOR_EACH_VEC_ELT (edge
, to_remove_edges
, i
, e
)
8921 e
->flags
|= EDGE_DFS_BACK
;
8923 /* Allocate our unwinder stack to unwind any temporary equivalences
8924 that might be recorded. */
8925 equiv_stack
= VEC_alloc (tree
, heap
, 20);
8927 /* To avoid lots of silly node creation, we create a single
8928 conditional and just modify it in-place when attempting to
8930 dummy
= gimple_build_cond (EQ_EXPR
,
8931 integer_zero_node
, integer_zero_node
,
8934 /* Walk through all the blocks finding those which present a
8935 potential jump threading opportunity. We could set this up
8936 as a dominator walker and record data during the walk, but
8937 I doubt it's worth the effort for the classes of jump
8938 threading opportunities we are trying to identify at this
8939 point in compilation. */
8944 /* If the generic jump threading code does not find this block
8945 interesting, then there is nothing to do. */
8946 if (! potentially_threadable_block (bb
))
8949 /* We only care about blocks ending in a COND_EXPR. While there
8950 may be some value in handling SWITCH_EXPR here, I doubt it's
8951 terribly important. */
8952 last
= gsi_stmt (gsi_last_bb (bb
));
8954 /* We're basically looking for a switch or any kind of conditional with
8955 integral or pointer type arguments. Note the type of the second
8956 argument will be the same as the first argument, so no need to
8957 check it explicitly. */
8958 if (gimple_code (last
) == GIMPLE_SWITCH
8959 || (gimple_code (last
) == GIMPLE_COND
8960 && TREE_CODE (gimple_cond_lhs (last
)) == SSA_NAME
8961 && (INTEGRAL_TYPE_P (TREE_TYPE (gimple_cond_lhs (last
)))
8962 || POINTER_TYPE_P (TREE_TYPE (gimple_cond_lhs (last
))))
8963 && (TREE_CODE (gimple_cond_rhs (last
)) == SSA_NAME
8964 || is_gimple_min_invariant (gimple_cond_rhs (last
)))))
8968 /* We've got a block with multiple predecessors and multiple
8969 successors which also ends in a suitable conditional or
8970 switch statement. For each predecessor, see if we can thread
8971 it to a specific successor. */
8972 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
8974 /* Do not thread across back edges or abnormal edges
8976 if (e
->flags
& (EDGE_DFS_BACK
| EDGE_COMPLEX
))
8979 thread_across_edge (dummy
, e
, true, &equiv_stack
,
8980 simplify_stmt_for_jump_threading
);
8985 /* We do not actually update the CFG or SSA graphs at this point as
8986 ASSERT_EXPRs are still in the IL and cfg cleanup code does not yet
8987 handle ASSERT_EXPRs gracefully. */
8990 /* We identified all the jump threading opportunities earlier, but could
8991 not transform the CFG at that time. This routine transforms the
8992 CFG and arranges for the dominator tree to be rebuilt if necessary.
8994 Note the SSA graph update will occur during the normal TODO
8995 processing by the pass manager. */
8997 finalize_jump_threads (void)
8999 thread_through_all_blocks (false);
9000 VEC_free (tree
, heap
, equiv_stack
);
9004 /* Traverse all the blocks folding conditionals with known ranges. */
9011 values_propagated
= true;
9015 fprintf (dump_file
, "\nValue ranges after VRP:\n\n");
9016 dump_all_value_ranges (dump_file
);
9017 fprintf (dump_file
, "\n");
9020 substitute_and_fold (op_with_constant_singleton_value_range
,
9021 vrp_fold_stmt
, false);
9023 if (warn_array_bounds
)
9024 check_all_array_refs ();
9026 /* We must identify jump threading opportunities before we release
9027 the datastructures built by VRP. */
9028 identify_jump_threads ();
9030 /* Free allocated memory. */
9031 for (i
= 0; i
< num_vr_values
; i
++)
9034 BITMAP_FREE (vr_value
[i
]->equiv
);
9039 free (vr_phi_edge_counts
);
9041 /* So that we can distinguish between VRP data being available
9042 and not available. */
9044 vr_phi_edge_counts
= NULL
;
9048 /* Main entry point to VRP (Value Range Propagation). This pass is
9049 loosely based on J. R. C. Patterson, ``Accurate Static Branch
9050 Prediction by Value Range Propagation,'' in SIGPLAN Conference on
9051 Programming Language Design and Implementation, pp. 67-78, 1995.
9052 Also available at http://citeseer.ist.psu.edu/patterson95accurate.html
9054 This is essentially an SSA-CCP pass modified to deal with ranges
9055 instead of constants.
9057 While propagating ranges, we may find that two or more SSA name
9058 have equivalent, though distinct ranges. For instance,
9061 2 p_4 = ASSERT_EXPR <p_3, p_3 != 0>
9063 4 p_5 = ASSERT_EXPR <p_4, p_4 == q_2>;
9067 In the code above, pointer p_5 has range [q_2, q_2], but from the
9068 code we can also determine that p_5 cannot be NULL and, if q_2 had
9069 a non-varying range, p_5's range should also be compatible with it.
9071 These equivalences are created by two expressions: ASSERT_EXPR and
9072 copy operations. Since p_5 is an assertion on p_4, and p_4 was the
9073 result of another assertion, then we can use the fact that p_5 and
9074 p_4 are equivalent when evaluating p_5's range.
9076 Together with value ranges, we also propagate these equivalences
9077 between names so that we can take advantage of information from
9078 multiple ranges when doing final replacement. Note that this
9079 equivalency relation is transitive but not symmetric.
9081 In the example above, p_5 is equivalent to p_4, q_2 and p_3, but we
9082 cannot assert that q_2 is equivalent to p_5 because q_2 may be used
9083 in contexts where that assertion does not hold (e.g., in line 6).
9085 TODO, the main difference between this pass and Patterson's is that
9086 we do not propagate edge probabilities. We only compute whether
9087 edges can be taken or not. That is, instead of having a spectrum
9088 of jump probabilities between 0 and 1, we only deal with 0, 1 and
9089 DON'T KNOW. In the future, it may be worthwhile to propagate
9090 probabilities to aid branch prediction. */
9099 loop_optimizer_init (LOOPS_NORMAL
| LOOPS_HAVE_RECORDED_EXITS
);
9100 rewrite_into_loop_closed_ssa (NULL
, TODO_update_ssa
);
9103 insert_range_assertions ();
9105 to_remove_edges
= VEC_alloc (edge
, heap
, 10);
9106 to_update_switch_stmts
= VEC_alloc (switch_update
, heap
, 5);
9107 threadedge_initialize_values ();
9110 ssa_propagate (vrp_visit_stmt
, vrp_visit_phi_node
);
9113 free_numbers_of_iterations_estimates ();
9115 /* ASSERT_EXPRs must be removed before finalizing jump threads
9116 as finalizing jump threads calls the CFG cleanup code which
9117 does not properly handle ASSERT_EXPRs. */
9118 remove_range_assertions ();
9120 /* If we exposed any new variables, go ahead and put them into
9121 SSA form now, before we handle jump threading. This simplifies
9122 interactions between rewriting of _DECL nodes into SSA form
9123 and rewriting SSA_NAME nodes into SSA form after block
9124 duplication and CFG manipulation. */
9125 update_ssa (TODO_update_ssa
);
9127 finalize_jump_threads ();
9129 /* Remove dead edges from SWITCH_EXPR optimization. This leaves the
9130 CFG in a broken state and requires a cfg_cleanup run. */
9131 FOR_EACH_VEC_ELT (edge
, to_remove_edges
, i
, e
)
9133 /* Update SWITCH_EXPR case label vector. */
9134 FOR_EACH_VEC_ELT (switch_update
, to_update_switch_stmts
, i
, su
)
9137 size_t n
= TREE_VEC_LENGTH (su
->vec
);
9139 gimple_switch_set_num_labels (su
->stmt
, n
);
9140 for (j
= 0; j
< n
; j
++)
9141 gimple_switch_set_label (su
->stmt
, j
, TREE_VEC_ELT (su
->vec
, j
));
9142 /* As we may have replaced the default label with a regular one
9143 make sure to make it a real default label again. This ensures
9144 optimal expansion. */
9145 label
= gimple_switch_label (su
->stmt
, 0);
9146 CASE_LOW (label
) = NULL_TREE
;
9147 CASE_HIGH (label
) = NULL_TREE
;
9150 if (VEC_length (edge
, to_remove_edges
) > 0)
9151 free_dominance_info (CDI_DOMINATORS
);
9153 VEC_free (edge
, heap
, to_remove_edges
);
9154 VEC_free (switch_update
, heap
, to_update_switch_stmts
);
9155 threadedge_finalize_values ();
9158 loop_optimizer_finalize ();
9165 return flag_tree_vrp
!= 0;
9168 struct gimple_opt_pass pass_vrp
=
9173 gate_vrp
, /* gate */
9174 execute_vrp
, /* execute */
9177 0, /* static_pass_number */
9178 TV_TREE_VRP
, /* tv_id */
9179 PROP_ssa
, /* properties_required */
9180 0, /* properties_provided */
9181 0, /* properties_destroyed */
9182 0, /* todo_flags_start */
9187 | TODO_ggc_collect
/* todo_flags_finish */