1 /* Support routines for Value Range Propagation (VRP).
2 Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013
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 && bitmap_bit_p (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
> to_remove_edges
;
159 static vec
<switch_update
> to_update_switch_stmts
;
162 /* Return the maximum value for TYPE. */
165 vrp_val_max (const_tree type
)
167 if (!INTEGRAL_TYPE_P (type
))
170 return TYPE_MAX_VALUE (type
);
173 /* Return the minimum value for TYPE. */
176 vrp_val_min (const_tree type
)
178 if (!INTEGRAL_TYPE_P (type
))
181 return TYPE_MIN_VALUE (type
);
184 /* Return whether VAL is equal to the maximum value of its type. This
185 will be true for a positive overflow infinity. We can't do a
186 simple equality comparison with TYPE_MAX_VALUE because C typedefs
187 and Ada subtypes can produce types whose TYPE_MAX_VALUE is not ==
188 to the integer constant with the same value in the type. */
191 vrp_val_is_max (const_tree val
)
193 tree type_max
= vrp_val_max (TREE_TYPE (val
));
194 return (val
== type_max
195 || (type_max
!= NULL_TREE
196 && operand_equal_p (val
, type_max
, 0)));
199 /* Return whether VAL is equal to the minimum value of its type. This
200 will be true for a negative overflow infinity. */
203 vrp_val_is_min (const_tree val
)
205 tree type_min
= vrp_val_min (TREE_TYPE (val
));
206 return (val
== type_min
207 || (type_min
!= NULL_TREE
208 && operand_equal_p (val
, type_min
, 0)));
212 /* Return whether TYPE should use an overflow infinity distinct from
213 TYPE_{MIN,MAX}_VALUE. We use an overflow infinity value to
214 represent a signed overflow during VRP computations. An infinity
215 is distinct from a half-range, which will go from some number to
216 TYPE_{MIN,MAX}_VALUE. */
219 needs_overflow_infinity (const_tree type
)
221 return INTEGRAL_TYPE_P (type
) && !TYPE_OVERFLOW_WRAPS (type
);
224 /* Return whether TYPE can support our overflow infinity
225 representation: we use the TREE_OVERFLOW flag, which only exists
226 for constants. If TYPE doesn't support this, we don't optimize
227 cases which would require signed overflow--we drop them to
231 supports_overflow_infinity (const_tree type
)
233 tree min
= vrp_val_min (type
), max
= vrp_val_max (type
);
234 #ifdef ENABLE_CHECKING
235 gcc_assert (needs_overflow_infinity (type
));
237 return (min
!= NULL_TREE
238 && CONSTANT_CLASS_P (min
)
240 && CONSTANT_CLASS_P (max
));
243 /* VAL is the maximum or minimum value of a type. Return a
244 corresponding overflow infinity. */
247 make_overflow_infinity (tree val
)
249 gcc_checking_assert (val
!= NULL_TREE
&& CONSTANT_CLASS_P (val
));
250 val
= copy_node (val
);
251 TREE_OVERFLOW (val
) = 1;
255 /* Return a negative overflow infinity for TYPE. */
258 negative_overflow_infinity (tree type
)
260 gcc_checking_assert (supports_overflow_infinity (type
));
261 return make_overflow_infinity (vrp_val_min (type
));
264 /* Return a positive overflow infinity for TYPE. */
267 positive_overflow_infinity (tree type
)
269 gcc_checking_assert (supports_overflow_infinity (type
));
270 return make_overflow_infinity (vrp_val_max (type
));
273 /* Return whether VAL is a negative overflow infinity. */
276 is_negative_overflow_infinity (const_tree val
)
278 return (needs_overflow_infinity (TREE_TYPE (val
))
279 && CONSTANT_CLASS_P (val
)
280 && TREE_OVERFLOW (val
)
281 && vrp_val_is_min (val
));
284 /* Return whether VAL is a positive overflow infinity. */
287 is_positive_overflow_infinity (const_tree val
)
289 return (needs_overflow_infinity (TREE_TYPE (val
))
290 && CONSTANT_CLASS_P (val
)
291 && TREE_OVERFLOW (val
)
292 && vrp_val_is_max (val
));
295 /* Return whether VAL is a positive or negative overflow infinity. */
298 is_overflow_infinity (const_tree val
)
300 return (needs_overflow_infinity (TREE_TYPE (val
))
301 && CONSTANT_CLASS_P (val
)
302 && TREE_OVERFLOW (val
)
303 && (vrp_val_is_min (val
) || vrp_val_is_max (val
)));
306 /* Return whether STMT has a constant rhs that is_overflow_infinity. */
309 stmt_overflow_infinity (gimple stmt
)
311 if (is_gimple_assign (stmt
)
312 && get_gimple_rhs_class (gimple_assign_rhs_code (stmt
)) ==
314 return is_overflow_infinity (gimple_assign_rhs1 (stmt
));
318 /* If VAL is now an overflow infinity, return VAL. Otherwise, return
319 the same value with TREE_OVERFLOW clear. This can be used to avoid
320 confusing a regular value with an overflow value. */
323 avoid_overflow_infinity (tree val
)
325 if (!is_overflow_infinity (val
))
328 if (vrp_val_is_max (val
))
329 return vrp_val_max (TREE_TYPE (val
));
332 gcc_checking_assert (vrp_val_is_min (val
));
333 return vrp_val_min (TREE_TYPE (val
));
338 /* Return true if ARG is marked with the nonnull attribute in the
339 current function signature. */
342 nonnull_arg_p (const_tree arg
)
344 tree t
, attrs
, fntype
;
345 unsigned HOST_WIDE_INT arg_num
;
347 gcc_assert (TREE_CODE (arg
) == PARM_DECL
&& POINTER_TYPE_P (TREE_TYPE (arg
)));
349 /* The static chain decl is always non null. */
350 if (arg
== cfun
->static_chain_decl
)
353 fntype
= TREE_TYPE (current_function_decl
);
354 for (attrs
= TYPE_ATTRIBUTES (fntype
); attrs
; attrs
= TREE_CHAIN (attrs
))
356 attrs
= lookup_attribute ("nonnull", attrs
);
358 /* If "nonnull" wasn't specified, we know nothing about the argument. */
359 if (attrs
== NULL_TREE
)
362 /* If "nonnull" applies to all the arguments, then ARG is non-null. */
363 if (TREE_VALUE (attrs
) == NULL_TREE
)
366 /* Get the position number for ARG in the function signature. */
367 for (arg_num
= 1, t
= DECL_ARGUMENTS (current_function_decl
);
369 t
= DECL_CHAIN (t
), arg_num
++)
375 gcc_assert (t
== arg
);
377 /* Now see if ARG_NUM is mentioned in the nonnull list. */
378 for (t
= TREE_VALUE (attrs
); t
; t
= TREE_CHAIN (t
))
380 if (compare_tree_int (TREE_VALUE (t
), arg_num
) == 0)
389 /* Set value range VR to VR_UNDEFINED. */
392 set_value_range_to_undefined (value_range_t
*vr
)
394 vr
->type
= VR_UNDEFINED
;
395 vr
->min
= vr
->max
= NULL_TREE
;
397 bitmap_clear (vr
->equiv
);
401 /* Set value range VR to VR_VARYING. */
404 set_value_range_to_varying (value_range_t
*vr
)
406 vr
->type
= VR_VARYING
;
407 vr
->min
= vr
->max
= NULL_TREE
;
409 bitmap_clear (vr
->equiv
);
413 /* Set value range VR to {T, MIN, MAX, EQUIV}. */
416 set_value_range (value_range_t
*vr
, enum value_range_type t
, tree min
,
417 tree max
, bitmap equiv
)
419 #if defined ENABLE_CHECKING
420 /* Check the validity of the range. */
421 if (t
== VR_RANGE
|| t
== VR_ANTI_RANGE
)
425 gcc_assert (min
&& max
);
427 if (INTEGRAL_TYPE_P (TREE_TYPE (min
)) && t
== VR_ANTI_RANGE
)
428 gcc_assert (!vrp_val_is_min (min
) || !vrp_val_is_max (max
));
430 cmp
= compare_values (min
, max
);
431 gcc_assert (cmp
== 0 || cmp
== -1 || cmp
== -2);
433 if (needs_overflow_infinity (TREE_TYPE (min
)))
434 gcc_assert (!is_overflow_infinity (min
)
435 || !is_overflow_infinity (max
));
438 if (t
== VR_UNDEFINED
|| t
== VR_VARYING
)
439 gcc_assert (min
== NULL_TREE
&& max
== NULL_TREE
);
441 if (t
== VR_UNDEFINED
|| t
== VR_VARYING
)
442 gcc_assert (equiv
== NULL
|| bitmap_empty_p (equiv
));
449 /* Since updating the equivalence set involves deep copying the
450 bitmaps, only do it if absolutely necessary. */
451 if (vr
->equiv
== NULL
453 vr
->equiv
= BITMAP_ALLOC (NULL
);
455 if (equiv
!= vr
->equiv
)
457 if (equiv
&& !bitmap_empty_p (equiv
))
458 bitmap_copy (vr
->equiv
, equiv
);
460 bitmap_clear (vr
->equiv
);
465 /* Set value range VR to the canonical form of {T, MIN, MAX, EQUIV}.
466 This means adjusting T, MIN and MAX representing the case of a
467 wrapping range with MAX < MIN covering [MIN, type_max] U [type_min, MAX]
468 as anti-rage ~[MAX+1, MIN-1]. Likewise for wrapping anti-ranges.
469 In corner cases where MAX+1 or MIN-1 wraps this will fall back
471 This routine exists to ease canonicalization in the case where we
472 extract ranges from var + CST op limit. */
475 set_and_canonicalize_value_range (value_range_t
*vr
, enum value_range_type t
,
476 tree min
, tree max
, bitmap equiv
)
478 /* Use the canonical setters for VR_UNDEFINED and VR_VARYING. */
479 if (t
== VR_UNDEFINED
)
481 set_value_range_to_undefined (vr
);
484 else if (t
== VR_VARYING
)
486 set_value_range_to_varying (vr
);
490 /* Nothing to canonicalize for symbolic ranges. */
491 if (TREE_CODE (min
) != INTEGER_CST
492 || TREE_CODE (max
) != INTEGER_CST
)
494 set_value_range (vr
, t
, min
, max
, equiv
);
498 /* Wrong order for min and max, to swap them and the VR type we need
500 if (tree_int_cst_lt (max
, min
))
504 /* For one bit precision if max < min, then the swapped
505 range covers all values, so for VR_RANGE it is varying and
506 for VR_ANTI_RANGE empty range, so drop to varying as well. */
507 if (TYPE_PRECISION (TREE_TYPE (min
)) == 1)
509 set_value_range_to_varying (vr
);
513 one
= build_int_cst (TREE_TYPE (min
), 1);
514 tmp
= int_const_binop (PLUS_EXPR
, max
, one
);
515 max
= int_const_binop (MINUS_EXPR
, min
, one
);
518 /* There's one corner case, if we had [C+1, C] before we now have
519 that again. But this represents an empty value range, so drop
520 to varying in this case. */
521 if (tree_int_cst_lt (max
, min
))
523 set_value_range_to_varying (vr
);
527 t
= t
== VR_RANGE
? VR_ANTI_RANGE
: VR_RANGE
;
530 /* Anti-ranges that can be represented as ranges should be so. */
531 if (t
== VR_ANTI_RANGE
)
533 bool is_min
= vrp_val_is_min (min
);
534 bool is_max
= vrp_val_is_max (max
);
536 if (is_min
&& is_max
)
538 /* We cannot deal with empty ranges, drop to varying.
539 ??? This could be VR_UNDEFINED instead. */
540 set_value_range_to_varying (vr
);
543 else if (TYPE_PRECISION (TREE_TYPE (min
)) == 1
544 && (is_min
|| is_max
))
546 /* Non-empty boolean ranges can always be represented
547 as a singleton range. */
549 min
= max
= vrp_val_max (TREE_TYPE (min
));
551 min
= max
= vrp_val_min (TREE_TYPE (min
));
555 /* As a special exception preserve non-null ranges. */
556 && !(TYPE_UNSIGNED (TREE_TYPE (min
))
557 && integer_zerop (max
)))
559 tree one
= build_int_cst (TREE_TYPE (max
), 1);
560 min
= int_const_binop (PLUS_EXPR
, max
, one
);
561 max
= vrp_val_max (TREE_TYPE (max
));
566 tree one
= build_int_cst (TREE_TYPE (min
), 1);
567 max
= int_const_binop (MINUS_EXPR
, min
, one
);
568 min
= vrp_val_min (TREE_TYPE (min
));
573 /* Drop [-INF(OVF), +INF(OVF)] to varying. */
574 if (needs_overflow_infinity (TREE_TYPE (min
))
575 && is_overflow_infinity (min
)
576 && is_overflow_infinity (max
))
578 set_value_range_to_varying (vr
);
582 set_value_range (vr
, t
, min
, max
, equiv
);
585 /* Copy value range FROM into value range TO. */
588 copy_value_range (value_range_t
*to
, value_range_t
*from
)
590 set_value_range (to
, from
->type
, from
->min
, from
->max
, from
->equiv
);
593 /* Set value range VR to a single value. This function is only called
594 with values we get from statements, and exists to clear the
595 TREE_OVERFLOW flag so that we don't think we have an overflow
596 infinity when we shouldn't. */
599 set_value_range_to_value (value_range_t
*vr
, tree val
, bitmap equiv
)
601 gcc_assert (is_gimple_min_invariant (val
));
602 val
= avoid_overflow_infinity (val
);
603 set_value_range (vr
, VR_RANGE
, val
, val
, equiv
);
606 /* Set value range VR to a non-negative range of type TYPE.
607 OVERFLOW_INFINITY indicates whether to use an overflow infinity
608 rather than TYPE_MAX_VALUE; this should be true if we determine
609 that the range is nonnegative based on the assumption that signed
610 overflow does not occur. */
613 set_value_range_to_nonnegative (value_range_t
*vr
, tree type
,
614 bool overflow_infinity
)
618 if (overflow_infinity
&& !supports_overflow_infinity (type
))
620 set_value_range_to_varying (vr
);
624 zero
= build_int_cst (type
, 0);
625 set_value_range (vr
, VR_RANGE
, zero
,
627 ? positive_overflow_infinity (type
)
628 : TYPE_MAX_VALUE (type
)),
632 /* Set value range VR to a non-NULL range of type TYPE. */
635 set_value_range_to_nonnull (value_range_t
*vr
, tree type
)
637 tree zero
= build_int_cst (type
, 0);
638 set_value_range (vr
, VR_ANTI_RANGE
, zero
, zero
, vr
->equiv
);
642 /* Set value range VR to a NULL range of type TYPE. */
645 set_value_range_to_null (value_range_t
*vr
, tree type
)
647 set_value_range_to_value (vr
, build_int_cst (type
, 0), vr
->equiv
);
651 /* Set value range VR to a range of a truthvalue of type TYPE. */
654 set_value_range_to_truthvalue (value_range_t
*vr
, tree type
)
656 if (TYPE_PRECISION (type
) == 1)
657 set_value_range_to_varying (vr
);
659 set_value_range (vr
, VR_RANGE
,
660 build_int_cst (type
, 0), build_int_cst (type
, 1),
665 /* If abs (min) < abs (max), set VR to [-max, max], if
666 abs (min) >= abs (max), set VR to [-min, min]. */
669 abs_extent_range (value_range_t
*vr
, tree min
, tree max
)
673 gcc_assert (TREE_CODE (min
) == INTEGER_CST
);
674 gcc_assert (TREE_CODE (max
) == INTEGER_CST
);
675 gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (min
)));
676 gcc_assert (!TYPE_UNSIGNED (TREE_TYPE (min
)));
677 min
= fold_unary (ABS_EXPR
, TREE_TYPE (min
), min
);
678 max
= fold_unary (ABS_EXPR
, TREE_TYPE (max
), max
);
679 if (TREE_OVERFLOW (min
) || TREE_OVERFLOW (max
))
681 set_value_range_to_varying (vr
);
684 cmp
= compare_values (min
, max
);
686 min
= fold_unary (NEGATE_EXPR
, TREE_TYPE (min
), max
);
687 else if (cmp
== 0 || cmp
== 1)
690 min
= fold_unary (NEGATE_EXPR
, TREE_TYPE (min
), min
);
694 set_value_range_to_varying (vr
);
697 set_and_canonicalize_value_range (vr
, VR_RANGE
, min
, max
, NULL
);
701 /* Return value range information for VAR.
703 If we have no values ranges recorded (ie, VRP is not running), then
704 return NULL. Otherwise create an empty range if none existed for VAR. */
706 static value_range_t
*
707 get_value_range (const_tree var
)
709 static const struct value_range_d vr_const_varying
710 = { VR_VARYING
, NULL_TREE
, NULL_TREE
, NULL
};
713 unsigned ver
= SSA_NAME_VERSION (var
);
715 /* If we have no recorded ranges, then return NULL. */
719 /* If we query the range for a new SSA name return an unmodifiable VARYING.
720 We should get here at most from the substitute-and-fold stage which
721 will never try to change values. */
722 if (ver
>= num_vr_values
)
723 return CONST_CAST (value_range_t
*, &vr_const_varying
);
729 /* After propagation finished do not allocate new value-ranges. */
730 if (values_propagated
)
731 return CONST_CAST (value_range_t
*, &vr_const_varying
);
733 /* Create a default value range. */
734 vr_value
[ver
] = vr
= XCNEW (value_range_t
);
736 /* Defer allocating the equivalence set. */
739 /* If VAR is a default definition of a parameter, the variable can
740 take any value in VAR's type. */
741 if (SSA_NAME_IS_DEFAULT_DEF (var
))
743 sym
= SSA_NAME_VAR (var
);
744 if (TREE_CODE (sym
) == PARM_DECL
)
746 /* Try to use the "nonnull" attribute to create ~[0, 0]
747 anti-ranges for pointers. Note that this is only valid with
748 default definitions of PARM_DECLs. */
749 if (POINTER_TYPE_P (TREE_TYPE (sym
))
750 && nonnull_arg_p (sym
))
751 set_value_range_to_nonnull (vr
, TREE_TYPE (sym
));
753 set_value_range_to_varying (vr
);
755 else if (TREE_CODE (sym
) == RESULT_DECL
756 && DECL_BY_REFERENCE (sym
))
757 set_value_range_to_nonnull (vr
, TREE_TYPE (sym
));
763 /* Return true, if VAL1 and VAL2 are equal values for VRP purposes. */
766 vrp_operand_equal_p (const_tree val1
, const_tree val2
)
770 if (!val1
|| !val2
|| !operand_equal_p (val1
, val2
, 0))
772 if (is_overflow_infinity (val1
))
773 return is_overflow_infinity (val2
);
777 /* Return true, if the bitmaps B1 and B2 are equal. */
780 vrp_bitmap_equal_p (const_bitmap b1
, const_bitmap b2
)
783 || ((!b1
|| bitmap_empty_p (b1
))
784 && (!b2
|| bitmap_empty_p (b2
)))
786 && bitmap_equal_p (b1
, b2
)));
789 /* Update the value range and equivalence set for variable VAR to
790 NEW_VR. Return true if NEW_VR is different from VAR's previous
793 NOTE: This function assumes that NEW_VR is a temporary value range
794 object created for the sole purpose of updating VAR's range. The
795 storage used by the equivalence set from NEW_VR will be freed by
796 this function. Do not call update_value_range when NEW_VR
797 is the range object associated with another SSA name. */
800 update_value_range (const_tree var
, value_range_t
*new_vr
)
802 value_range_t
*old_vr
;
805 /* Update the value range, if necessary. */
806 old_vr
= get_value_range (var
);
807 is_new
= old_vr
->type
!= new_vr
->type
808 || !vrp_operand_equal_p (old_vr
->min
, new_vr
->min
)
809 || !vrp_operand_equal_p (old_vr
->max
, new_vr
->max
)
810 || !vrp_bitmap_equal_p (old_vr
->equiv
, new_vr
->equiv
);
814 /* Do not allow transitions up the lattice. The following
815 is slightly more awkward than just new_vr->type < old_vr->type
816 because VR_RANGE and VR_ANTI_RANGE need to be considered
817 the same. We may not have is_new when transitioning to
818 UNDEFINED or from VARYING. */
819 if (new_vr
->type
== VR_UNDEFINED
820 || old_vr
->type
== VR_VARYING
)
821 set_value_range_to_varying (old_vr
);
823 set_value_range (old_vr
, new_vr
->type
, new_vr
->min
, new_vr
->max
,
827 BITMAP_FREE (new_vr
->equiv
);
833 /* Add VAR and VAR's equivalence set to EQUIV. This is the central
834 point where equivalence processing can be turned on/off. */
837 add_equivalence (bitmap
*equiv
, const_tree var
)
839 unsigned ver
= SSA_NAME_VERSION (var
);
840 value_range_t
*vr
= vr_value
[ver
];
843 *equiv
= BITMAP_ALLOC (NULL
);
844 bitmap_set_bit (*equiv
, ver
);
846 bitmap_ior_into (*equiv
, vr
->equiv
);
850 /* Return true if VR is ~[0, 0]. */
853 range_is_nonnull (value_range_t
*vr
)
855 return vr
->type
== VR_ANTI_RANGE
856 && integer_zerop (vr
->min
)
857 && integer_zerop (vr
->max
);
861 /* Return true if VR is [0, 0]. */
864 range_is_null (value_range_t
*vr
)
866 return vr
->type
== VR_RANGE
867 && integer_zerop (vr
->min
)
868 && integer_zerop (vr
->max
);
871 /* Return true if max and min of VR are INTEGER_CST. It's not necessary
875 range_int_cst_p (value_range_t
*vr
)
877 return (vr
->type
== VR_RANGE
878 && TREE_CODE (vr
->max
) == INTEGER_CST
879 && TREE_CODE (vr
->min
) == INTEGER_CST
);
882 /* Return true if VR is a INTEGER_CST singleton. */
885 range_int_cst_singleton_p (value_range_t
*vr
)
887 return (range_int_cst_p (vr
)
888 && !TREE_OVERFLOW (vr
->min
)
889 && !TREE_OVERFLOW (vr
->max
)
890 && tree_int_cst_equal (vr
->min
, vr
->max
));
893 /* Return true if value range VR involves at least one symbol. */
896 symbolic_range_p (value_range_t
*vr
)
898 return (!is_gimple_min_invariant (vr
->min
)
899 || !is_gimple_min_invariant (vr
->max
));
902 /* Return true if value range VR uses an overflow infinity. */
905 overflow_infinity_range_p (value_range_t
*vr
)
907 return (vr
->type
== VR_RANGE
908 && (is_overflow_infinity (vr
->min
)
909 || is_overflow_infinity (vr
->max
)));
912 /* Return false if we can not make a valid comparison based on VR;
913 this will be the case if it uses an overflow infinity and overflow
914 is not undefined (i.e., -fno-strict-overflow is in effect).
915 Otherwise return true, and set *STRICT_OVERFLOW_P to true if VR
916 uses an overflow infinity. */
919 usable_range_p (value_range_t
*vr
, bool *strict_overflow_p
)
921 gcc_assert (vr
->type
== VR_RANGE
);
922 if (is_overflow_infinity (vr
->min
))
924 *strict_overflow_p
= true;
925 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (vr
->min
)))
928 if (is_overflow_infinity (vr
->max
))
930 *strict_overflow_p
= true;
931 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (vr
->max
)))
938 /* Return true if the result of assignment STMT is know to be non-negative.
939 If the return value is based on the assumption that signed overflow is
940 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
941 *STRICT_OVERFLOW_P.*/
944 gimple_assign_nonnegative_warnv_p (gimple stmt
, bool *strict_overflow_p
)
946 enum tree_code code
= gimple_assign_rhs_code (stmt
);
947 switch (get_gimple_rhs_class (code
))
949 case GIMPLE_UNARY_RHS
:
950 return tree_unary_nonnegative_warnv_p (gimple_assign_rhs_code (stmt
),
951 gimple_expr_type (stmt
),
952 gimple_assign_rhs1 (stmt
),
954 case GIMPLE_BINARY_RHS
:
955 return tree_binary_nonnegative_warnv_p (gimple_assign_rhs_code (stmt
),
956 gimple_expr_type (stmt
),
957 gimple_assign_rhs1 (stmt
),
958 gimple_assign_rhs2 (stmt
),
960 case GIMPLE_TERNARY_RHS
:
962 case GIMPLE_SINGLE_RHS
:
963 return tree_single_nonnegative_warnv_p (gimple_assign_rhs1 (stmt
),
965 case GIMPLE_INVALID_RHS
:
972 /* Return true if return value of call STMT is know to be non-negative.
973 If the return value is based on the assumption that signed overflow is
974 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
975 *STRICT_OVERFLOW_P.*/
978 gimple_call_nonnegative_warnv_p (gimple stmt
, bool *strict_overflow_p
)
980 tree arg0
= gimple_call_num_args (stmt
) > 0 ?
981 gimple_call_arg (stmt
, 0) : NULL_TREE
;
982 tree arg1
= gimple_call_num_args (stmt
) > 1 ?
983 gimple_call_arg (stmt
, 1) : NULL_TREE
;
985 return tree_call_nonnegative_warnv_p (gimple_expr_type (stmt
),
986 gimple_call_fndecl (stmt
),
992 /* Return true if STMT is know to to compute a non-negative value.
993 If the return value is based on the assumption that signed overflow is
994 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
995 *STRICT_OVERFLOW_P.*/
998 gimple_stmt_nonnegative_warnv_p (gimple stmt
, bool *strict_overflow_p
)
1000 switch (gimple_code (stmt
))
1003 return gimple_assign_nonnegative_warnv_p (stmt
, strict_overflow_p
);
1005 return gimple_call_nonnegative_warnv_p (stmt
, strict_overflow_p
);
1011 /* Return true if the result of assignment STMT is know to be non-zero.
1012 If the return value is based on the assumption that signed overflow is
1013 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1014 *STRICT_OVERFLOW_P.*/
1017 gimple_assign_nonzero_warnv_p (gimple stmt
, bool *strict_overflow_p
)
1019 enum tree_code code
= gimple_assign_rhs_code (stmt
);
1020 switch (get_gimple_rhs_class (code
))
1022 case GIMPLE_UNARY_RHS
:
1023 return tree_unary_nonzero_warnv_p (gimple_assign_rhs_code (stmt
),
1024 gimple_expr_type (stmt
),
1025 gimple_assign_rhs1 (stmt
),
1027 case GIMPLE_BINARY_RHS
:
1028 return tree_binary_nonzero_warnv_p (gimple_assign_rhs_code (stmt
),
1029 gimple_expr_type (stmt
),
1030 gimple_assign_rhs1 (stmt
),
1031 gimple_assign_rhs2 (stmt
),
1033 case GIMPLE_TERNARY_RHS
:
1035 case GIMPLE_SINGLE_RHS
:
1036 return tree_single_nonzero_warnv_p (gimple_assign_rhs1 (stmt
),
1038 case GIMPLE_INVALID_RHS
:
1045 /* Return true if STMT is know to to compute a non-zero value.
1046 If the return value is based on the assumption that signed overflow is
1047 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1048 *STRICT_OVERFLOW_P.*/
1051 gimple_stmt_nonzero_warnv_p (gimple stmt
, bool *strict_overflow_p
)
1053 switch (gimple_code (stmt
))
1056 return gimple_assign_nonzero_warnv_p (stmt
, strict_overflow_p
);
1058 return gimple_alloca_call_p (stmt
);
1064 /* Like tree_expr_nonzero_warnv_p, but this function uses value ranges
1068 vrp_stmt_computes_nonzero (gimple stmt
, bool *strict_overflow_p
)
1070 if (gimple_stmt_nonzero_warnv_p (stmt
, strict_overflow_p
))
1073 /* If we have an expression of the form &X->a, then the expression
1074 is nonnull if X is nonnull. */
1075 if (is_gimple_assign (stmt
)
1076 && gimple_assign_rhs_code (stmt
) == ADDR_EXPR
)
1078 tree expr
= gimple_assign_rhs1 (stmt
);
1079 tree base
= get_base_address (TREE_OPERAND (expr
, 0));
1081 if (base
!= NULL_TREE
1082 && TREE_CODE (base
) == MEM_REF
1083 && TREE_CODE (TREE_OPERAND (base
, 0)) == SSA_NAME
)
1085 value_range_t
*vr
= get_value_range (TREE_OPERAND (base
, 0));
1086 if (range_is_nonnull (vr
))
1094 /* Returns true if EXPR is a valid value (as expected by compare_values) --
1095 a gimple invariant, or SSA_NAME +- CST. */
1098 valid_value_p (tree expr
)
1100 if (TREE_CODE (expr
) == SSA_NAME
)
1103 if (TREE_CODE (expr
) == PLUS_EXPR
1104 || TREE_CODE (expr
) == MINUS_EXPR
)
1105 return (TREE_CODE (TREE_OPERAND (expr
, 0)) == SSA_NAME
1106 && TREE_CODE (TREE_OPERAND (expr
, 1)) == INTEGER_CST
);
1108 return is_gimple_min_invariant (expr
);
1114 -2 if those are incomparable. */
1116 operand_less_p (tree val
, tree val2
)
1118 /* LT is folded faster than GE and others. Inline the common case. */
1119 if (TREE_CODE (val
) == INTEGER_CST
&& TREE_CODE (val2
) == INTEGER_CST
)
1121 if (TYPE_UNSIGNED (TREE_TYPE (val
)))
1122 return INT_CST_LT_UNSIGNED (val
, val2
);
1125 if (INT_CST_LT (val
, val2
))
1133 fold_defer_overflow_warnings ();
1135 tcmp
= fold_binary_to_constant (LT_EXPR
, boolean_type_node
, val
, val2
);
1137 fold_undefer_and_ignore_overflow_warnings ();
1140 || TREE_CODE (tcmp
) != INTEGER_CST
)
1143 if (!integer_zerop (tcmp
))
1147 /* val >= val2, not considering overflow infinity. */
1148 if (is_negative_overflow_infinity (val
))
1149 return is_negative_overflow_infinity (val2
) ? 0 : 1;
1150 else if (is_positive_overflow_infinity (val2
))
1151 return is_positive_overflow_infinity (val
) ? 0 : 1;
1156 /* Compare two values VAL1 and VAL2. Return
1158 -2 if VAL1 and VAL2 cannot be compared at compile-time,
1161 +1 if VAL1 > VAL2, and
1164 This is similar to tree_int_cst_compare but supports pointer values
1165 and values that cannot be compared at compile time.
1167 If STRICT_OVERFLOW_P is not NULL, then set *STRICT_OVERFLOW_P to
1168 true if the return value is only valid if we assume that signed
1169 overflow is undefined. */
1172 compare_values_warnv (tree val1
, tree val2
, bool *strict_overflow_p
)
1177 /* Below we rely on the fact that VAL1 and VAL2 are both pointers or
1179 gcc_assert (POINTER_TYPE_P (TREE_TYPE (val1
))
1180 == POINTER_TYPE_P (TREE_TYPE (val2
)));
1181 /* Convert the two values into the same type. This is needed because
1182 sizetype causes sign extension even for unsigned types. */
1183 val2
= fold_convert (TREE_TYPE (val1
), val2
);
1184 STRIP_USELESS_TYPE_CONVERSION (val2
);
1186 if ((TREE_CODE (val1
) == SSA_NAME
1187 || TREE_CODE (val1
) == PLUS_EXPR
1188 || TREE_CODE (val1
) == MINUS_EXPR
)
1189 && (TREE_CODE (val2
) == SSA_NAME
1190 || TREE_CODE (val2
) == PLUS_EXPR
1191 || TREE_CODE (val2
) == MINUS_EXPR
))
1193 tree n1
, c1
, n2
, c2
;
1194 enum tree_code code1
, code2
;
1196 /* If VAL1 and VAL2 are of the form 'NAME [+-] CST' or 'NAME',
1197 return -1 or +1 accordingly. If VAL1 and VAL2 don't use the
1198 same name, return -2. */
1199 if (TREE_CODE (val1
) == SSA_NAME
)
1207 code1
= TREE_CODE (val1
);
1208 n1
= TREE_OPERAND (val1
, 0);
1209 c1
= TREE_OPERAND (val1
, 1);
1210 if (tree_int_cst_sgn (c1
) == -1)
1212 if (is_negative_overflow_infinity (c1
))
1214 c1
= fold_unary_to_constant (NEGATE_EXPR
, TREE_TYPE (c1
), c1
);
1217 code1
= code1
== MINUS_EXPR
? PLUS_EXPR
: MINUS_EXPR
;
1221 if (TREE_CODE (val2
) == SSA_NAME
)
1229 code2
= TREE_CODE (val2
);
1230 n2
= TREE_OPERAND (val2
, 0);
1231 c2
= TREE_OPERAND (val2
, 1);
1232 if (tree_int_cst_sgn (c2
) == -1)
1234 if (is_negative_overflow_infinity (c2
))
1236 c2
= fold_unary_to_constant (NEGATE_EXPR
, TREE_TYPE (c2
), c2
);
1239 code2
= code2
== MINUS_EXPR
? PLUS_EXPR
: MINUS_EXPR
;
1243 /* Both values must use the same name. */
1247 if (code1
== SSA_NAME
1248 && code2
== SSA_NAME
)
1252 /* If overflow is defined we cannot simplify more. */
1253 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (val1
)))
1256 if (strict_overflow_p
!= NULL
1257 && (code1
== SSA_NAME
|| !TREE_NO_WARNING (val1
))
1258 && (code2
== SSA_NAME
|| !TREE_NO_WARNING (val2
)))
1259 *strict_overflow_p
= true;
1261 if (code1
== SSA_NAME
)
1263 if (code2
== PLUS_EXPR
)
1264 /* NAME < NAME + CST */
1266 else if (code2
== MINUS_EXPR
)
1267 /* NAME > NAME - CST */
1270 else if (code1
== PLUS_EXPR
)
1272 if (code2
== SSA_NAME
)
1273 /* NAME + CST > NAME */
1275 else if (code2
== PLUS_EXPR
)
1276 /* NAME + CST1 > NAME + CST2, if CST1 > CST2 */
1277 return compare_values_warnv (c1
, c2
, strict_overflow_p
);
1278 else if (code2
== MINUS_EXPR
)
1279 /* NAME + CST1 > NAME - CST2 */
1282 else if (code1
== MINUS_EXPR
)
1284 if (code2
== SSA_NAME
)
1285 /* NAME - CST < NAME */
1287 else if (code2
== PLUS_EXPR
)
1288 /* NAME - CST1 < NAME + CST2 */
1290 else if (code2
== MINUS_EXPR
)
1291 /* NAME - CST1 > NAME - CST2, if CST1 < CST2. Notice that
1292 C1 and C2 are swapped in the call to compare_values. */
1293 return compare_values_warnv (c2
, c1
, strict_overflow_p
);
1299 /* We cannot compare non-constants. */
1300 if (!is_gimple_min_invariant (val1
) || !is_gimple_min_invariant (val2
))
1303 if (!POINTER_TYPE_P (TREE_TYPE (val1
)))
1305 /* We cannot compare overflowed values, except for overflow
1307 if (TREE_OVERFLOW (val1
) || TREE_OVERFLOW (val2
))
1309 if (strict_overflow_p
!= NULL
)
1310 *strict_overflow_p
= true;
1311 if (is_negative_overflow_infinity (val1
))
1312 return is_negative_overflow_infinity (val2
) ? 0 : -1;
1313 else if (is_negative_overflow_infinity (val2
))
1315 else if (is_positive_overflow_infinity (val1
))
1316 return is_positive_overflow_infinity (val2
) ? 0 : 1;
1317 else if (is_positive_overflow_infinity (val2
))
1322 return tree_int_cst_compare (val1
, val2
);
1328 /* First see if VAL1 and VAL2 are not the same. */
1329 if (val1
== val2
|| operand_equal_p (val1
, val2
, 0))
1332 /* If VAL1 is a lower address than VAL2, return -1. */
1333 if (operand_less_p (val1
, val2
) == 1)
1336 /* If VAL1 is a higher address than VAL2, return +1. */
1337 if (operand_less_p (val2
, val1
) == 1)
1340 /* If VAL1 is different than VAL2, return +2.
1341 For integer constants we either have already returned -1 or 1
1342 or they are equivalent. We still might succeed in proving
1343 something about non-trivial operands. */
1344 if (TREE_CODE (val1
) != INTEGER_CST
1345 || TREE_CODE (val2
) != INTEGER_CST
)
1347 t
= fold_binary_to_constant (NE_EXPR
, boolean_type_node
, val1
, val2
);
1348 if (t
&& integer_onep (t
))
1356 /* Compare values like compare_values_warnv, but treat comparisons of
1357 nonconstants which rely on undefined overflow as incomparable. */
1360 compare_values (tree val1
, tree val2
)
1366 ret
= compare_values_warnv (val1
, val2
, &sop
);
1368 && (!is_gimple_min_invariant (val1
) || !is_gimple_min_invariant (val2
)))
1374 /* Return 1 if VAL is inside value range MIN <= VAL <= MAX,
1375 0 if VAL is not inside [MIN, MAX],
1376 -2 if we cannot tell either way.
1378 Benchmark compile/20001226-1.c compilation time after changing this
1382 value_inside_range (tree val
, tree min
, tree max
)
1386 cmp1
= operand_less_p (val
, min
);
1392 cmp2
= operand_less_p (max
, val
);
1400 /* Return true if value ranges VR0 and VR1 have a non-empty
1403 Benchmark compile/20001226-1.c compilation time after changing this
1408 value_ranges_intersect_p (value_range_t
*vr0
, value_range_t
*vr1
)
1410 /* The value ranges do not intersect if the maximum of the first range is
1411 less than the minimum of the second range or vice versa.
1412 When those relations are unknown, we can't do any better. */
1413 if (operand_less_p (vr0
->max
, vr1
->min
) != 0)
1415 if (operand_less_p (vr1
->max
, vr0
->min
) != 0)
1421 /* Return 1 if [MIN, MAX] includes the value zero, 0 if it does not
1422 include the value zero, -2 if we cannot tell. */
1425 range_includes_zero_p (tree min
, tree max
)
1427 tree zero
= build_int_cst (TREE_TYPE (min
), 0);
1428 return value_inside_range (zero
, min
, max
);
1431 /* Return true if *VR is know to only contain nonnegative values. */
1434 value_range_nonnegative_p (value_range_t
*vr
)
1436 /* Testing for VR_ANTI_RANGE is not useful here as any anti-range
1437 which would return a useful value should be encoded as a
1439 if (vr
->type
== VR_RANGE
)
1441 int result
= compare_values (vr
->min
, integer_zero_node
);
1442 return (result
== 0 || result
== 1);
1448 /* Return true if T, an SSA_NAME, is known to be nonnegative. Return
1449 false otherwise or if no value range information is available. */
1452 ssa_name_nonnegative_p (const_tree t
)
1454 value_range_t
*vr
= get_value_range (t
);
1456 if (INTEGRAL_TYPE_P (t
)
1457 && TYPE_UNSIGNED (t
))
1463 return value_range_nonnegative_p (vr
);
1466 /* If *VR has a value rante that is a single constant value return that,
1467 otherwise return NULL_TREE. */
1470 value_range_constant_singleton (value_range_t
*vr
)
1472 if (vr
->type
== VR_RANGE
1473 && operand_equal_p (vr
->min
, vr
->max
, 0)
1474 && is_gimple_min_invariant (vr
->min
))
1480 /* If OP has a value range with a single constant value return that,
1481 otherwise return NULL_TREE. This returns OP itself if OP is a
1485 op_with_constant_singleton_value_range (tree op
)
1487 if (is_gimple_min_invariant (op
))
1490 if (TREE_CODE (op
) != SSA_NAME
)
1493 return value_range_constant_singleton (get_value_range (op
));
1496 /* Return true if op is in a boolean [0, 1] value-range. */
1499 op_with_boolean_value_range_p (tree op
)
1503 if (TYPE_PRECISION (TREE_TYPE (op
)) == 1)
1506 if (integer_zerop (op
)
1507 || integer_onep (op
))
1510 if (TREE_CODE (op
) != SSA_NAME
)
1513 vr
= get_value_range (op
);
1514 return (vr
->type
== VR_RANGE
1515 && integer_zerop (vr
->min
)
1516 && integer_onep (vr
->max
));
1519 /* Extract value range information from an ASSERT_EXPR EXPR and store
1523 extract_range_from_assert (value_range_t
*vr_p
, tree expr
)
1525 tree var
, cond
, limit
, min
, max
, type
;
1526 value_range_t
*limit_vr
;
1527 enum tree_code cond_code
;
1529 var
= ASSERT_EXPR_VAR (expr
);
1530 cond
= ASSERT_EXPR_COND (expr
);
1532 gcc_assert (COMPARISON_CLASS_P (cond
));
1534 /* Find VAR in the ASSERT_EXPR conditional. */
1535 if (var
== TREE_OPERAND (cond
, 0)
1536 || TREE_CODE (TREE_OPERAND (cond
, 0)) == PLUS_EXPR
1537 || TREE_CODE (TREE_OPERAND (cond
, 0)) == NOP_EXPR
)
1539 /* If the predicate is of the form VAR COMP LIMIT, then we just
1540 take LIMIT from the RHS and use the same comparison code. */
1541 cond_code
= TREE_CODE (cond
);
1542 limit
= TREE_OPERAND (cond
, 1);
1543 cond
= TREE_OPERAND (cond
, 0);
1547 /* If the predicate is of the form LIMIT COMP VAR, then we need
1548 to flip around the comparison code to create the proper range
1550 cond_code
= swap_tree_comparison (TREE_CODE (cond
));
1551 limit
= TREE_OPERAND (cond
, 0);
1552 cond
= TREE_OPERAND (cond
, 1);
1555 limit
= avoid_overflow_infinity (limit
);
1557 type
= TREE_TYPE (var
);
1558 gcc_assert (limit
!= var
);
1560 /* For pointer arithmetic, we only keep track of pointer equality
1562 if (POINTER_TYPE_P (type
) && cond_code
!= NE_EXPR
&& cond_code
!= EQ_EXPR
)
1564 set_value_range_to_varying (vr_p
);
1568 /* If LIMIT is another SSA name and LIMIT has a range of its own,
1569 try to use LIMIT's range to avoid creating symbolic ranges
1571 limit_vr
= (TREE_CODE (limit
) == SSA_NAME
) ? get_value_range (limit
) : NULL
;
1573 /* LIMIT's range is only interesting if it has any useful information. */
1575 && (limit_vr
->type
== VR_UNDEFINED
1576 || limit_vr
->type
== VR_VARYING
1577 || symbolic_range_p (limit_vr
)))
1580 /* Initially, the new range has the same set of equivalences of
1581 VAR's range. This will be revised before returning the final
1582 value. Since assertions may be chained via mutually exclusive
1583 predicates, we will need to trim the set of equivalences before
1585 gcc_assert (vr_p
->equiv
== NULL
);
1586 add_equivalence (&vr_p
->equiv
, var
);
1588 /* Extract a new range based on the asserted comparison for VAR and
1589 LIMIT's value range. Notice that if LIMIT has an anti-range, we
1590 will only use it for equality comparisons (EQ_EXPR). For any
1591 other kind of assertion, we cannot derive a range from LIMIT's
1592 anti-range that can be used to describe the new range. For
1593 instance, ASSERT_EXPR <x_2, x_2 <= b_4>. If b_4 is ~[2, 10],
1594 then b_4 takes on the ranges [-INF, 1] and [11, +INF]. There is
1595 no single range for x_2 that could describe LE_EXPR, so we might
1596 as well build the range [b_4, +INF] for it.
1597 One special case we handle is extracting a range from a
1598 range test encoded as (unsigned)var + CST <= limit. */
1599 if (TREE_CODE (cond
) == NOP_EXPR
1600 || TREE_CODE (cond
) == PLUS_EXPR
)
1602 if (TREE_CODE (cond
) == PLUS_EXPR
)
1604 min
= fold_build1 (NEGATE_EXPR
, TREE_TYPE (TREE_OPERAND (cond
, 1)),
1605 TREE_OPERAND (cond
, 1));
1606 max
= int_const_binop (PLUS_EXPR
, limit
, min
);
1607 cond
= TREE_OPERAND (cond
, 0);
1611 min
= build_int_cst (TREE_TYPE (var
), 0);
1615 /* Make sure to not set TREE_OVERFLOW on the final type
1616 conversion. We are willingly interpreting large positive
1617 unsigned values as negative singed values here. */
1618 min
= force_fit_type_double (TREE_TYPE (var
), tree_to_double_int (min
),
1620 max
= force_fit_type_double (TREE_TYPE (var
), tree_to_double_int (max
),
1623 /* We can transform a max, min range to an anti-range or
1624 vice-versa. Use set_and_canonicalize_value_range which does
1626 if (cond_code
== LE_EXPR
)
1627 set_and_canonicalize_value_range (vr_p
, VR_RANGE
,
1628 min
, max
, vr_p
->equiv
);
1629 else if (cond_code
== GT_EXPR
)
1630 set_and_canonicalize_value_range (vr_p
, VR_ANTI_RANGE
,
1631 min
, max
, vr_p
->equiv
);
1635 else if (cond_code
== EQ_EXPR
)
1637 enum value_range_type range_type
;
1641 range_type
= limit_vr
->type
;
1642 min
= limit_vr
->min
;
1643 max
= limit_vr
->max
;
1647 range_type
= VR_RANGE
;
1652 set_value_range (vr_p
, range_type
, min
, max
, vr_p
->equiv
);
1654 /* When asserting the equality VAR == LIMIT and LIMIT is another
1655 SSA name, the new range will also inherit the equivalence set
1657 if (TREE_CODE (limit
) == SSA_NAME
)
1658 add_equivalence (&vr_p
->equiv
, limit
);
1660 else if (cond_code
== NE_EXPR
)
1662 /* As described above, when LIMIT's range is an anti-range and
1663 this assertion is an inequality (NE_EXPR), then we cannot
1664 derive anything from the anti-range. For instance, if
1665 LIMIT's range was ~[0, 0], the assertion 'VAR != LIMIT' does
1666 not imply that VAR's range is [0, 0]. So, in the case of
1667 anti-ranges, we just assert the inequality using LIMIT and
1670 If LIMIT_VR is a range, we can only use it to build a new
1671 anti-range if LIMIT_VR is a single-valued range. For
1672 instance, if LIMIT_VR is [0, 1], the predicate
1673 VAR != [0, 1] does not mean that VAR's range is ~[0, 1].
1674 Rather, it means that for value 0 VAR should be ~[0, 0]
1675 and for value 1, VAR should be ~[1, 1]. We cannot
1676 represent these ranges.
1678 The only situation in which we can build a valid
1679 anti-range is when LIMIT_VR is a single-valued range
1680 (i.e., LIMIT_VR->MIN == LIMIT_VR->MAX). In that case,
1681 build the anti-range ~[LIMIT_VR->MIN, LIMIT_VR->MAX]. */
1683 && limit_vr
->type
== VR_RANGE
1684 && compare_values (limit_vr
->min
, limit_vr
->max
) == 0)
1686 min
= limit_vr
->min
;
1687 max
= limit_vr
->max
;
1691 /* In any other case, we cannot use LIMIT's range to build a
1692 valid anti-range. */
1696 /* If MIN and MAX cover the whole range for their type, then
1697 just use the original LIMIT. */
1698 if (INTEGRAL_TYPE_P (type
)
1699 && vrp_val_is_min (min
)
1700 && vrp_val_is_max (max
))
1703 set_and_canonicalize_value_range (vr_p
, VR_ANTI_RANGE
,
1704 min
, max
, vr_p
->equiv
);
1706 else if (cond_code
== LE_EXPR
|| cond_code
== LT_EXPR
)
1708 min
= TYPE_MIN_VALUE (type
);
1710 if (limit_vr
== NULL
|| limit_vr
->type
== VR_ANTI_RANGE
)
1714 /* If LIMIT_VR is of the form [N1, N2], we need to build the
1715 range [MIN, N2] for LE_EXPR and [MIN, N2 - 1] for
1717 max
= limit_vr
->max
;
1720 /* If the maximum value forces us to be out of bounds, simply punt.
1721 It would be pointless to try and do anything more since this
1722 all should be optimized away above us. */
1723 if ((cond_code
== LT_EXPR
1724 && compare_values (max
, min
) == 0)
1725 || (CONSTANT_CLASS_P (max
) && TREE_OVERFLOW (max
)))
1726 set_value_range_to_varying (vr_p
);
1729 /* For LT_EXPR, we create the range [MIN, MAX - 1]. */
1730 if (cond_code
== LT_EXPR
)
1732 if (TYPE_PRECISION (TREE_TYPE (max
)) == 1
1733 && !TYPE_UNSIGNED (TREE_TYPE (max
)))
1734 max
= fold_build2 (PLUS_EXPR
, TREE_TYPE (max
), max
,
1735 build_int_cst (TREE_TYPE (max
), -1));
1737 max
= fold_build2 (MINUS_EXPR
, TREE_TYPE (max
), max
,
1738 build_int_cst (TREE_TYPE (max
), 1));
1740 TREE_NO_WARNING (max
) = 1;
1743 set_value_range (vr_p
, VR_RANGE
, min
, max
, vr_p
->equiv
);
1746 else if (cond_code
== GE_EXPR
|| cond_code
== GT_EXPR
)
1748 max
= TYPE_MAX_VALUE (type
);
1750 if (limit_vr
== NULL
|| limit_vr
->type
== VR_ANTI_RANGE
)
1754 /* If LIMIT_VR is of the form [N1, N2], we need to build the
1755 range [N1, MAX] for GE_EXPR and [N1 + 1, MAX] for
1757 min
= limit_vr
->min
;
1760 /* If the minimum value forces us to be out of bounds, simply punt.
1761 It would be pointless to try and do anything more since this
1762 all should be optimized away above us. */
1763 if ((cond_code
== GT_EXPR
1764 && compare_values (min
, max
) == 0)
1765 || (CONSTANT_CLASS_P (min
) && TREE_OVERFLOW (min
)))
1766 set_value_range_to_varying (vr_p
);
1769 /* For GT_EXPR, we create the range [MIN + 1, MAX]. */
1770 if (cond_code
== GT_EXPR
)
1772 if (TYPE_PRECISION (TREE_TYPE (min
)) == 1
1773 && !TYPE_UNSIGNED (TREE_TYPE (min
)))
1774 min
= fold_build2 (MINUS_EXPR
, TREE_TYPE (min
), min
,
1775 build_int_cst (TREE_TYPE (min
), -1));
1777 min
= fold_build2 (PLUS_EXPR
, TREE_TYPE (min
), min
,
1778 build_int_cst (TREE_TYPE (min
), 1));
1780 TREE_NO_WARNING (min
) = 1;
1783 set_value_range (vr_p
, VR_RANGE
, min
, max
, vr_p
->equiv
);
1789 /* Finally intersect the new range with what we already know about var. */
1790 vrp_intersect_ranges (vr_p
, get_value_range (var
));
1794 /* Extract range information from SSA name VAR and store it in VR. If
1795 VAR has an interesting range, use it. Otherwise, create the
1796 range [VAR, VAR] and return it. This is useful in situations where
1797 we may have conditionals testing values of VARYING names. For
1804 Even if y_5 is deemed VARYING, we can determine that x_3 > y_5 is
1808 extract_range_from_ssa_name (value_range_t
*vr
, tree var
)
1810 value_range_t
*var_vr
= get_value_range (var
);
1812 if (var_vr
->type
!= VR_UNDEFINED
&& var_vr
->type
!= VR_VARYING
)
1813 copy_value_range (vr
, var_vr
);
1815 set_value_range (vr
, VR_RANGE
, var
, var
, NULL
);
1817 add_equivalence (&vr
->equiv
, var
);
1821 /* Wrapper around int_const_binop. If the operation overflows and we
1822 are not using wrapping arithmetic, then adjust the result to be
1823 -INF or +INF depending on CODE, VAL1 and VAL2. This can return
1824 NULL_TREE if we need to use an overflow infinity representation but
1825 the type does not support it. */
1828 vrp_int_const_binop (enum tree_code code
, tree val1
, tree val2
)
1832 res
= int_const_binop (code
, val1
, val2
);
1834 /* If we are using unsigned arithmetic, operate symbolically
1835 on -INF and +INF as int_const_binop only handles signed overflow. */
1836 if (TYPE_UNSIGNED (TREE_TYPE (val1
)))
1838 int checkz
= compare_values (res
, val1
);
1839 bool overflow
= false;
1841 /* Ensure that res = val1 [+*] val2 >= val1
1842 or that res = val1 - val2 <= val1. */
1843 if ((code
== PLUS_EXPR
1844 && !(checkz
== 1 || checkz
== 0))
1845 || (code
== MINUS_EXPR
1846 && !(checkz
== 0 || checkz
== -1)))
1850 /* Checking for multiplication overflow is done by dividing the
1851 output of the multiplication by the first input of the
1852 multiplication. If the result of that division operation is
1853 not equal to the second input of the multiplication, then the
1854 multiplication overflowed. */
1855 else if (code
== MULT_EXPR
&& !integer_zerop (val1
))
1857 tree tmp
= int_const_binop (TRUNC_DIV_EXPR
,
1860 int check
= compare_values (tmp
, val2
);
1868 res
= copy_node (res
);
1869 TREE_OVERFLOW (res
) = 1;
1873 else if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (val1
)))
1874 /* If the singed operation wraps then int_const_binop has done
1875 everything we want. */
1877 else if ((TREE_OVERFLOW (res
)
1878 && !TREE_OVERFLOW (val1
)
1879 && !TREE_OVERFLOW (val2
))
1880 || is_overflow_infinity (val1
)
1881 || is_overflow_infinity (val2
))
1883 /* If the operation overflowed but neither VAL1 nor VAL2 are
1884 overflown, return -INF or +INF depending on the operation
1885 and the combination of signs of the operands. */
1886 int sgn1
= tree_int_cst_sgn (val1
);
1887 int sgn2
= tree_int_cst_sgn (val2
);
1889 if (needs_overflow_infinity (TREE_TYPE (res
))
1890 && !supports_overflow_infinity (TREE_TYPE (res
)))
1893 /* We have to punt on adding infinities of different signs,
1894 since we can't tell what the sign of the result should be.
1895 Likewise for subtracting infinities of the same sign. */
1896 if (((code
== PLUS_EXPR
&& sgn1
!= sgn2
)
1897 || (code
== MINUS_EXPR
&& sgn1
== sgn2
))
1898 && is_overflow_infinity (val1
)
1899 && is_overflow_infinity (val2
))
1902 /* Don't try to handle division or shifting of infinities. */
1903 if ((code
== TRUNC_DIV_EXPR
1904 || code
== FLOOR_DIV_EXPR
1905 || code
== CEIL_DIV_EXPR
1906 || code
== EXACT_DIV_EXPR
1907 || code
== ROUND_DIV_EXPR
1908 || code
== RSHIFT_EXPR
)
1909 && (is_overflow_infinity (val1
)
1910 || is_overflow_infinity (val2
)))
1913 /* Notice that we only need to handle the restricted set of
1914 operations handled by extract_range_from_binary_expr.
1915 Among them, only multiplication, addition and subtraction
1916 can yield overflow without overflown operands because we
1917 are working with integral types only... except in the
1918 case VAL1 = -INF and VAL2 = -1 which overflows to +INF
1919 for division too. */
1921 /* For multiplication, the sign of the overflow is given
1922 by the comparison of the signs of the operands. */
1923 if ((code
== MULT_EXPR
&& sgn1
== sgn2
)
1924 /* For addition, the operands must be of the same sign
1925 to yield an overflow. Its sign is therefore that
1926 of one of the operands, for example the first. For
1927 infinite operands X + -INF is negative, not positive. */
1928 || (code
== PLUS_EXPR
1930 ? !is_negative_overflow_infinity (val2
)
1931 : is_positive_overflow_infinity (val2
)))
1932 /* For subtraction, non-infinite operands must be of
1933 different signs to yield an overflow. Its sign is
1934 therefore that of the first operand or the opposite of
1935 that of the second operand. A first operand of 0 counts
1936 as positive here, for the corner case 0 - (-INF), which
1937 overflows, but must yield +INF. For infinite operands 0
1938 - INF is negative, not positive. */
1939 || (code
== MINUS_EXPR
1941 ? !is_positive_overflow_infinity (val2
)
1942 : is_negative_overflow_infinity (val2
)))
1943 /* We only get in here with positive shift count, so the
1944 overflow direction is the same as the sign of val1.
1945 Actually rshift does not overflow at all, but we only
1946 handle the case of shifting overflowed -INF and +INF. */
1947 || (code
== RSHIFT_EXPR
1949 /* For division, the only case is -INF / -1 = +INF. */
1950 || code
== TRUNC_DIV_EXPR
1951 || code
== FLOOR_DIV_EXPR
1952 || code
== CEIL_DIV_EXPR
1953 || code
== EXACT_DIV_EXPR
1954 || code
== ROUND_DIV_EXPR
)
1955 return (needs_overflow_infinity (TREE_TYPE (res
))
1956 ? positive_overflow_infinity (TREE_TYPE (res
))
1957 : TYPE_MAX_VALUE (TREE_TYPE (res
)));
1959 return (needs_overflow_infinity (TREE_TYPE (res
))
1960 ? negative_overflow_infinity (TREE_TYPE (res
))
1961 : TYPE_MIN_VALUE (TREE_TYPE (res
)));
1968 /* For range VR compute two double_int bitmasks. In *MAY_BE_NONZERO
1969 bitmask if some bit is unset, it means for all numbers in the range
1970 the bit is 0, otherwise it might be 0 or 1. In *MUST_BE_NONZERO
1971 bitmask if some bit is set, it means for all numbers in the range
1972 the bit is 1, otherwise it might be 0 or 1. */
1975 zero_nonzero_bits_from_vr (value_range_t
*vr
,
1976 double_int
*may_be_nonzero
,
1977 double_int
*must_be_nonzero
)
1979 *may_be_nonzero
= double_int_minus_one
;
1980 *must_be_nonzero
= double_int_zero
;
1981 if (!range_int_cst_p (vr
)
1982 || TREE_OVERFLOW (vr
->min
)
1983 || TREE_OVERFLOW (vr
->max
))
1986 if (range_int_cst_singleton_p (vr
))
1988 *may_be_nonzero
= tree_to_double_int (vr
->min
);
1989 *must_be_nonzero
= *may_be_nonzero
;
1991 else if (tree_int_cst_sgn (vr
->min
) >= 0
1992 || tree_int_cst_sgn (vr
->max
) < 0)
1994 double_int dmin
= tree_to_double_int (vr
->min
);
1995 double_int dmax
= tree_to_double_int (vr
->max
);
1996 double_int xor_mask
= dmin
^ dmax
;
1997 *may_be_nonzero
= dmin
| dmax
;
1998 *must_be_nonzero
= dmin
& dmax
;
1999 if (xor_mask
.high
!= 0)
2001 unsigned HOST_WIDE_INT mask
2002 = ((unsigned HOST_WIDE_INT
) 1
2003 << floor_log2 (xor_mask
.high
)) - 1;
2004 may_be_nonzero
->low
= ALL_ONES
;
2005 may_be_nonzero
->high
|= mask
;
2006 must_be_nonzero
->low
= 0;
2007 must_be_nonzero
->high
&= ~mask
;
2009 else if (xor_mask
.low
!= 0)
2011 unsigned HOST_WIDE_INT mask
2012 = ((unsigned HOST_WIDE_INT
) 1
2013 << floor_log2 (xor_mask
.low
)) - 1;
2014 may_be_nonzero
->low
|= mask
;
2015 must_be_nonzero
->low
&= ~mask
;
2022 /* Create two value-ranges in *VR0 and *VR1 from the anti-range *AR
2023 so that *VR0 U *VR1 == *AR. Returns true if that is possible,
2024 false otherwise. If *AR can be represented with a single range
2025 *VR1 will be VR_UNDEFINED. */
2028 ranges_from_anti_range (value_range_t
*ar
,
2029 value_range_t
*vr0
, value_range_t
*vr1
)
2031 tree type
= TREE_TYPE (ar
->min
);
2033 vr0
->type
= VR_UNDEFINED
;
2034 vr1
->type
= VR_UNDEFINED
;
2036 if (ar
->type
!= VR_ANTI_RANGE
2037 || TREE_CODE (ar
->min
) != INTEGER_CST
2038 || TREE_CODE (ar
->max
) != INTEGER_CST
2039 || !vrp_val_min (type
)
2040 || !vrp_val_max (type
))
2043 if (!vrp_val_is_min (ar
->min
))
2045 vr0
->type
= VR_RANGE
;
2046 vr0
->min
= vrp_val_min (type
);
2048 = double_int_to_tree (type
,
2049 tree_to_double_int (ar
->min
) - double_int_one
);
2051 if (!vrp_val_is_max (ar
->max
))
2053 vr1
->type
= VR_RANGE
;
2055 = double_int_to_tree (type
,
2056 tree_to_double_int (ar
->max
) + double_int_one
);
2057 vr1
->max
= vrp_val_max (type
);
2059 if (vr0
->type
== VR_UNDEFINED
)
2062 vr1
->type
= VR_UNDEFINED
;
2065 return vr0
->type
!= VR_UNDEFINED
;
2068 /* Helper to extract a value-range *VR for a multiplicative operation
2072 extract_range_from_multiplicative_op_1 (value_range_t
*vr
,
2073 enum tree_code code
,
2074 value_range_t
*vr0
, value_range_t
*vr1
)
2076 enum value_range_type type
;
2083 /* Multiplications, divisions and shifts are a bit tricky to handle,
2084 depending on the mix of signs we have in the two ranges, we
2085 need to operate on different values to get the minimum and
2086 maximum values for the new range. One approach is to figure
2087 out all the variations of range combinations and do the
2090 However, this involves several calls to compare_values and it
2091 is pretty convoluted. It's simpler to do the 4 operations
2092 (MIN0 OP MIN1, MIN0 OP MAX1, MAX0 OP MIN1 and MAX0 OP MAX0 OP
2093 MAX1) and then figure the smallest and largest values to form
2095 gcc_assert (code
== MULT_EXPR
2096 || code
== TRUNC_DIV_EXPR
2097 || code
== FLOOR_DIV_EXPR
2098 || code
== CEIL_DIV_EXPR
2099 || code
== EXACT_DIV_EXPR
2100 || code
== ROUND_DIV_EXPR
2101 || code
== RSHIFT_EXPR
2102 || code
== LSHIFT_EXPR
);
2103 gcc_assert ((vr0
->type
== VR_RANGE
2104 || (code
== MULT_EXPR
&& vr0
->type
== VR_ANTI_RANGE
))
2105 && vr0
->type
== vr1
->type
);
2109 /* Compute the 4 cross operations. */
2111 val
[0] = vrp_int_const_binop (code
, vr0
->min
, vr1
->min
);
2112 if (val
[0] == NULL_TREE
)
2115 if (vr1
->max
== vr1
->min
)
2119 val
[1] = vrp_int_const_binop (code
, vr0
->min
, vr1
->max
);
2120 if (val
[1] == NULL_TREE
)
2124 if (vr0
->max
== vr0
->min
)
2128 val
[2] = vrp_int_const_binop (code
, vr0
->max
, vr1
->min
);
2129 if (val
[2] == NULL_TREE
)
2133 if (vr0
->min
== vr0
->max
|| vr1
->min
== vr1
->max
)
2137 val
[3] = vrp_int_const_binop (code
, vr0
->max
, vr1
->max
);
2138 if (val
[3] == NULL_TREE
)
2144 set_value_range_to_varying (vr
);
2148 /* Set MIN to the minimum of VAL[i] and MAX to the maximum
2152 for (i
= 1; i
< 4; i
++)
2154 if (!is_gimple_min_invariant (min
)
2155 || (TREE_OVERFLOW (min
) && !is_overflow_infinity (min
))
2156 || !is_gimple_min_invariant (max
)
2157 || (TREE_OVERFLOW (max
) && !is_overflow_infinity (max
)))
2162 if (!is_gimple_min_invariant (val
[i
])
2163 || (TREE_OVERFLOW (val
[i
])
2164 && !is_overflow_infinity (val
[i
])))
2166 /* If we found an overflowed value, set MIN and MAX
2167 to it so that we set the resulting range to
2173 if (compare_values (val
[i
], min
) == -1)
2176 if (compare_values (val
[i
], max
) == 1)
2181 /* If either MIN or MAX overflowed, then set the resulting range to
2182 VARYING. But we do accept an overflow infinity
2184 if (min
== NULL_TREE
2185 || !is_gimple_min_invariant (min
)
2186 || (TREE_OVERFLOW (min
) && !is_overflow_infinity (min
))
2188 || !is_gimple_min_invariant (max
)
2189 || (TREE_OVERFLOW (max
) && !is_overflow_infinity (max
)))
2191 set_value_range_to_varying (vr
);
2197 2) [-INF, +-INF(OVF)]
2198 3) [+-INF(OVF), +INF]
2199 4) [+-INF(OVF), +-INF(OVF)]
2200 We learn nothing when we have INF and INF(OVF) on both sides.
2201 Note that we do accept [-INF, -INF] and [+INF, +INF] without
2203 if ((vrp_val_is_min (min
) || is_overflow_infinity (min
))
2204 && (vrp_val_is_max (max
) || is_overflow_infinity (max
)))
2206 set_value_range_to_varying (vr
);
2210 cmp
= compare_values (min
, max
);
2211 if (cmp
== -2 || cmp
== 1)
2213 /* If the new range has its limits swapped around (MIN > MAX),
2214 then the operation caused one of them to wrap around, mark
2215 the new range VARYING. */
2216 set_value_range_to_varying (vr
);
2219 set_value_range (vr
, type
, min
, max
, NULL
);
2222 /* Some quadruple precision helpers. */
2224 quad_int_cmp (double_int l0
, double_int h0
,
2225 double_int l1
, double_int h1
, bool uns
)
2227 int c
= h0
.cmp (h1
, uns
);
2228 if (c
!= 0) return c
;
2229 return l0
.ucmp (l1
);
2233 quad_int_pair_sort (double_int
*l0
, double_int
*h0
,
2234 double_int
*l1
, double_int
*h1
, bool uns
)
2236 if (quad_int_cmp (*l0
, *h0
, *l1
, *h1
, uns
) > 0)
2239 tmp
= *l0
; *l0
= *l1
; *l1
= tmp
;
2240 tmp
= *h0
; *h0
= *h1
; *h1
= tmp
;
2244 /* Extract range information from a binary operation CODE based on
2245 the ranges of each of its operands, *VR0 and *VR1 with resulting
2246 type EXPR_TYPE. The resulting range is stored in *VR. */
2249 extract_range_from_binary_expr_1 (value_range_t
*vr
,
2250 enum tree_code code
, tree expr_type
,
2251 value_range_t
*vr0_
, value_range_t
*vr1_
)
2253 value_range_t vr0
= *vr0_
, vr1
= *vr1_
;
2254 value_range_t vrtem0
= VR_INITIALIZER
, vrtem1
= VR_INITIALIZER
;
2255 enum value_range_type type
;
2256 tree min
= NULL_TREE
, max
= NULL_TREE
;
2259 if (!INTEGRAL_TYPE_P (expr_type
)
2260 && !POINTER_TYPE_P (expr_type
))
2262 set_value_range_to_varying (vr
);
2266 /* Not all binary expressions can be applied to ranges in a
2267 meaningful way. Handle only arithmetic operations. */
2268 if (code
!= PLUS_EXPR
2269 && code
!= MINUS_EXPR
2270 && code
!= POINTER_PLUS_EXPR
2271 && code
!= MULT_EXPR
2272 && code
!= TRUNC_DIV_EXPR
2273 && code
!= FLOOR_DIV_EXPR
2274 && code
!= CEIL_DIV_EXPR
2275 && code
!= EXACT_DIV_EXPR
2276 && code
!= ROUND_DIV_EXPR
2277 && code
!= TRUNC_MOD_EXPR
2278 && code
!= RSHIFT_EXPR
2279 && code
!= LSHIFT_EXPR
2282 && code
!= BIT_AND_EXPR
2283 && code
!= BIT_IOR_EXPR
2284 && code
!= BIT_XOR_EXPR
)
2286 set_value_range_to_varying (vr
);
2290 /* If both ranges are UNDEFINED, so is the result. */
2291 if (vr0
.type
== VR_UNDEFINED
&& vr1
.type
== VR_UNDEFINED
)
2293 set_value_range_to_undefined (vr
);
2296 /* If one of the ranges is UNDEFINED drop it to VARYING for the following
2297 code. At some point we may want to special-case operations that
2298 have UNDEFINED result for all or some value-ranges of the not UNDEFINED
2300 else if (vr0
.type
== VR_UNDEFINED
)
2301 set_value_range_to_varying (&vr0
);
2302 else if (vr1
.type
== VR_UNDEFINED
)
2303 set_value_range_to_varying (&vr1
);
2305 /* Now canonicalize anti-ranges to ranges when they are not symbolic
2306 and express ~[] op X as ([]' op X) U ([]'' op X). */
2307 if (vr0
.type
== VR_ANTI_RANGE
2308 && ranges_from_anti_range (&vr0
, &vrtem0
, &vrtem1
))
2310 extract_range_from_binary_expr_1 (vr
, code
, expr_type
, &vrtem0
, vr1_
);
2311 if (vrtem1
.type
!= VR_UNDEFINED
)
2313 value_range_t vrres
= VR_INITIALIZER
;
2314 extract_range_from_binary_expr_1 (&vrres
, code
, expr_type
,
2316 vrp_meet (vr
, &vrres
);
2320 /* Likewise for X op ~[]. */
2321 if (vr1
.type
== VR_ANTI_RANGE
2322 && ranges_from_anti_range (&vr1
, &vrtem0
, &vrtem1
))
2324 extract_range_from_binary_expr_1 (vr
, code
, expr_type
, vr0_
, &vrtem0
);
2325 if (vrtem1
.type
!= VR_UNDEFINED
)
2327 value_range_t vrres
= VR_INITIALIZER
;
2328 extract_range_from_binary_expr_1 (&vrres
, code
, expr_type
,
2330 vrp_meet (vr
, &vrres
);
2335 /* The type of the resulting value range defaults to VR0.TYPE. */
2338 /* Refuse to operate on VARYING ranges, ranges of different kinds
2339 and symbolic ranges. As an exception, we allow BIT_AND_EXPR
2340 because we may be able to derive a useful range even if one of
2341 the operands is VR_VARYING or symbolic range. Similarly for
2342 divisions. TODO, we may be able to derive anti-ranges in
2344 if (code
!= BIT_AND_EXPR
2345 && code
!= BIT_IOR_EXPR
2346 && code
!= TRUNC_DIV_EXPR
2347 && code
!= FLOOR_DIV_EXPR
2348 && code
!= CEIL_DIV_EXPR
2349 && code
!= EXACT_DIV_EXPR
2350 && code
!= ROUND_DIV_EXPR
2351 && code
!= TRUNC_MOD_EXPR
2354 && (vr0
.type
== VR_VARYING
2355 || vr1
.type
== VR_VARYING
2356 || vr0
.type
!= vr1
.type
2357 || symbolic_range_p (&vr0
)
2358 || symbolic_range_p (&vr1
)))
2360 set_value_range_to_varying (vr
);
2364 /* Now evaluate the expression to determine the new range. */
2365 if (POINTER_TYPE_P (expr_type
))
2367 if (code
== MIN_EXPR
|| code
== MAX_EXPR
)
2369 /* For MIN/MAX expressions with pointers, we only care about
2370 nullness, if both are non null, then the result is nonnull.
2371 If both are null, then the result is null. Otherwise they
2373 if (range_is_nonnull (&vr0
) && range_is_nonnull (&vr1
))
2374 set_value_range_to_nonnull (vr
, expr_type
);
2375 else if (range_is_null (&vr0
) && range_is_null (&vr1
))
2376 set_value_range_to_null (vr
, expr_type
);
2378 set_value_range_to_varying (vr
);
2380 else if (code
== POINTER_PLUS_EXPR
)
2382 /* For pointer types, we are really only interested in asserting
2383 whether the expression evaluates to non-NULL. */
2384 if (range_is_nonnull (&vr0
) || range_is_nonnull (&vr1
))
2385 set_value_range_to_nonnull (vr
, expr_type
);
2386 else if (range_is_null (&vr0
) && range_is_null (&vr1
))
2387 set_value_range_to_null (vr
, expr_type
);
2389 set_value_range_to_varying (vr
);
2391 else if (code
== BIT_AND_EXPR
)
2393 /* For pointer types, we are really only interested in asserting
2394 whether the expression evaluates to non-NULL. */
2395 if (range_is_nonnull (&vr0
) && range_is_nonnull (&vr1
))
2396 set_value_range_to_nonnull (vr
, expr_type
);
2397 else if (range_is_null (&vr0
) || range_is_null (&vr1
))
2398 set_value_range_to_null (vr
, expr_type
);
2400 set_value_range_to_varying (vr
);
2403 set_value_range_to_varying (vr
);
2408 /* For integer ranges, apply the operation to each end of the
2409 range and see what we end up with. */
2410 if (code
== PLUS_EXPR
|| code
== MINUS_EXPR
)
2412 /* If we have a PLUS_EXPR with two VR_RANGE integer constant
2413 ranges compute the precise range for such case if possible. */
2414 if (range_int_cst_p (&vr0
)
2415 && range_int_cst_p (&vr1
)
2416 /* We need as many bits as the possibly unsigned inputs. */
2417 && TYPE_PRECISION (expr_type
) <= HOST_BITS_PER_DOUBLE_INT
)
2419 double_int min0
= tree_to_double_int (vr0
.min
);
2420 double_int max0
= tree_to_double_int (vr0
.max
);
2421 double_int min1
= tree_to_double_int (vr1
.min
);
2422 double_int max1
= tree_to_double_int (vr1
.max
);
2423 bool uns
= TYPE_UNSIGNED (expr_type
);
2425 = double_int::min_value (TYPE_PRECISION (expr_type
), uns
);
2427 = double_int::max_value (TYPE_PRECISION (expr_type
), uns
);
2428 double_int dmin
, dmax
;
2432 if (code
== PLUS_EXPR
)
2437 /* Check for overflow in double_int. */
2438 if (min1
.cmp (double_int_zero
, uns
) != dmin
.cmp (min0
, uns
))
2439 min_ovf
= min0
.cmp (dmin
, uns
);
2440 if (max1
.cmp (double_int_zero
, uns
) != dmax
.cmp (max0
, uns
))
2441 max_ovf
= max0
.cmp (dmax
, uns
);
2443 else /* if (code == MINUS_EXPR) */
2448 if (double_int_zero
.cmp (max1
, uns
) != dmin
.cmp (min0
, uns
))
2449 min_ovf
= min0
.cmp (max1
, uns
);
2450 if (double_int_zero
.cmp (min1
, uns
) != dmax
.cmp (max0
, uns
))
2451 max_ovf
= max0
.cmp (min1
, uns
);
2454 /* For non-wrapping arithmetic look at possibly smaller
2455 value-ranges of the type. */
2456 if (!TYPE_OVERFLOW_WRAPS (expr_type
))
2458 if (vrp_val_min (expr_type
))
2459 type_min
= tree_to_double_int (vrp_val_min (expr_type
));
2460 if (vrp_val_max (expr_type
))
2461 type_max
= tree_to_double_int (vrp_val_max (expr_type
));
2464 /* Check for type overflow. */
2467 if (dmin
.cmp (type_min
, uns
) == -1)
2469 else if (dmin
.cmp (type_max
, uns
) == 1)
2474 if (dmax
.cmp (type_min
, uns
) == -1)
2476 else if (dmax
.cmp (type_max
, uns
) == 1)
2480 if (TYPE_OVERFLOW_WRAPS (expr_type
))
2482 /* If overflow wraps, truncate the values and adjust the
2483 range kind and bounds appropriately. */
2485 = dmin
.ext (TYPE_PRECISION (expr_type
), uns
);
2487 = dmax
.ext (TYPE_PRECISION (expr_type
), uns
);
2488 if (min_ovf
== max_ovf
)
2490 /* No overflow or both overflow or underflow. The
2491 range kind stays VR_RANGE. */
2492 min
= double_int_to_tree (expr_type
, tmin
);
2493 max
= double_int_to_tree (expr_type
, tmax
);
2495 else if (min_ovf
== -1
2498 /* Underflow and overflow, drop to VR_VARYING. */
2499 set_value_range_to_varying (vr
);
2504 /* Min underflow or max overflow. The range kind
2505 changes to VR_ANTI_RANGE. */
2506 bool covers
= false;
2507 double_int tem
= tmin
;
2508 gcc_assert ((min_ovf
== -1 && max_ovf
== 0)
2509 || (max_ovf
== 1 && min_ovf
== 0));
2510 type
= VR_ANTI_RANGE
;
2511 tmin
= tmax
+ double_int_one
;
2512 if (tmin
.cmp (tmax
, uns
) < 0)
2514 tmax
= tem
+ double_int_minus_one
;
2515 if (tmax
.cmp (tem
, uns
) > 0)
2517 /* If the anti-range would cover nothing, drop to varying.
2518 Likewise if the anti-range bounds are outside of the
2520 if (covers
|| tmin
.cmp (tmax
, uns
) > 0)
2522 set_value_range_to_varying (vr
);
2525 min
= double_int_to_tree (expr_type
, tmin
);
2526 max
= double_int_to_tree (expr_type
, tmax
);
2531 /* If overflow does not wrap, saturate to the types min/max
2535 if (needs_overflow_infinity (expr_type
)
2536 && supports_overflow_infinity (expr_type
))
2537 min
= negative_overflow_infinity (expr_type
);
2539 min
= double_int_to_tree (expr_type
, type_min
);
2541 else if (min_ovf
== 1)
2543 if (needs_overflow_infinity (expr_type
)
2544 && supports_overflow_infinity (expr_type
))
2545 min
= positive_overflow_infinity (expr_type
);
2547 min
= double_int_to_tree (expr_type
, type_max
);
2550 min
= double_int_to_tree (expr_type
, dmin
);
2554 if (needs_overflow_infinity (expr_type
)
2555 && supports_overflow_infinity (expr_type
))
2556 max
= negative_overflow_infinity (expr_type
);
2558 max
= double_int_to_tree (expr_type
, type_min
);
2560 else if (max_ovf
== 1)
2562 if (needs_overflow_infinity (expr_type
)
2563 && supports_overflow_infinity (expr_type
))
2564 max
= positive_overflow_infinity (expr_type
);
2566 max
= double_int_to_tree (expr_type
, type_max
);
2569 max
= double_int_to_tree (expr_type
, dmax
);
2571 if (needs_overflow_infinity (expr_type
)
2572 && supports_overflow_infinity (expr_type
))
2574 if (is_negative_overflow_infinity (vr0
.min
)
2575 || (code
== PLUS_EXPR
2576 ? is_negative_overflow_infinity (vr1
.min
)
2577 : is_positive_overflow_infinity (vr1
.max
)))
2578 min
= negative_overflow_infinity (expr_type
);
2579 if (is_positive_overflow_infinity (vr0
.max
)
2580 || (code
== PLUS_EXPR
2581 ? is_positive_overflow_infinity (vr1
.max
)
2582 : is_negative_overflow_infinity (vr1
.min
)))
2583 max
= positive_overflow_infinity (expr_type
);
2588 /* For other cases, for example if we have a PLUS_EXPR with two
2589 VR_ANTI_RANGEs, drop to VR_VARYING. It would take more effort
2590 to compute a precise range for such a case.
2591 ??? General even mixed range kind operations can be expressed
2592 by for example transforming ~[3, 5] + [1, 2] to range-only
2593 operations and a union primitive:
2594 [-INF, 2] + [1, 2] U [5, +INF] + [1, 2]
2595 [-INF+1, 4] U [6, +INF(OVF)]
2596 though usually the union is not exactly representable with
2597 a single range or anti-range as the above is
2598 [-INF+1, +INF(OVF)] intersected with ~[5, 5]
2599 but one could use a scheme similar to equivalences for this. */
2600 set_value_range_to_varying (vr
);
2604 else if (code
== MIN_EXPR
2605 || code
== MAX_EXPR
)
2607 if (vr0
.type
== VR_RANGE
2608 && !symbolic_range_p (&vr0
))
2611 if (vr1
.type
== VR_RANGE
2612 && !symbolic_range_p (&vr1
))
2614 /* For operations that make the resulting range directly
2615 proportional to the original ranges, apply the operation to
2616 the same end of each range. */
2617 min
= vrp_int_const_binop (code
, vr0
.min
, vr1
.min
);
2618 max
= vrp_int_const_binop (code
, vr0
.max
, vr1
.max
);
2620 else if (code
== MIN_EXPR
)
2622 min
= vrp_val_min (expr_type
);
2625 else if (code
== MAX_EXPR
)
2628 max
= vrp_val_max (expr_type
);
2631 else if (vr1
.type
== VR_RANGE
2632 && !symbolic_range_p (&vr1
))
2635 if (code
== MIN_EXPR
)
2637 min
= vrp_val_min (expr_type
);
2640 else if (code
== MAX_EXPR
)
2643 max
= vrp_val_max (expr_type
);
2648 set_value_range_to_varying (vr
);
2652 else if (code
== MULT_EXPR
)
2654 /* Fancy code so that with unsigned, [-3,-1]*[-3,-1] does not
2656 if (range_int_cst_p (&vr0
)
2657 && range_int_cst_p (&vr1
)
2658 && TYPE_OVERFLOW_WRAPS (expr_type
))
2660 double_int min0
, max0
, min1
, max1
, sizem1
, size
;
2661 double_int prod0l
, prod0h
, prod1l
, prod1h
,
2662 prod2l
, prod2h
, prod3l
, prod3h
;
2663 bool uns0
, uns1
, uns
;
2665 sizem1
= double_int::max_value (TYPE_PRECISION (expr_type
), true);
2666 size
= sizem1
+ double_int_one
;
2668 min0
= tree_to_double_int (vr0
.min
);
2669 max0
= tree_to_double_int (vr0
.max
);
2670 min1
= tree_to_double_int (vr1
.min
);
2671 max1
= tree_to_double_int (vr1
.max
);
2673 uns0
= TYPE_UNSIGNED (expr_type
);
2676 /* Canonicalize the intervals. */
2677 if (TYPE_UNSIGNED (expr_type
))
2679 double_int min2
= size
- min0
;
2680 if (!min2
.is_zero () && min2
.cmp (max0
, true) < 0)
2688 if (!min2
.is_zero () && min2
.cmp (max1
, true) < 0)
2698 prod0l
= min0
.wide_mul_with_sign (min1
, true, &prod0h
, &overflow
);
2699 if (!uns0
&& min0
.is_negative ())
2701 if (!uns1
&& min1
.is_negative ())
2704 prod1l
= min0
.wide_mul_with_sign (max1
, true, &prod1h
, &overflow
);
2705 if (!uns0
&& min0
.is_negative ())
2707 if (!uns1
&& max1
.is_negative ())
2710 prod2l
= max0
.wide_mul_with_sign (min1
, true, &prod2h
, &overflow
);
2711 if (!uns0
&& max0
.is_negative ())
2713 if (!uns1
&& min1
.is_negative ())
2716 prod3l
= max0
.wide_mul_with_sign (max1
, true, &prod3h
, &overflow
);
2717 if (!uns0
&& max0
.is_negative ())
2719 if (!uns1
&& max1
.is_negative ())
2722 /* Sort the 4 products. */
2723 quad_int_pair_sort (&prod0l
, &prod0h
, &prod3l
, &prod3h
, uns
);
2724 quad_int_pair_sort (&prod1l
, &prod1h
, &prod2l
, &prod2h
, uns
);
2725 quad_int_pair_sort (&prod0l
, &prod0h
, &prod1l
, &prod1h
, uns
);
2726 quad_int_pair_sort (&prod2l
, &prod2h
, &prod3l
, &prod3h
, uns
);
2729 if (prod0l
.is_zero ())
2731 prod1l
= double_int_zero
;
2739 prod2l
= prod3l
+ prod1l
;
2740 prod2h
= prod3h
+ prod1h
;
2741 if (prod2l
.ult (prod3l
))
2742 prod2h
+= double_int_one
; /* carry */
2744 if (!prod2h
.is_zero ()
2745 || prod2l
.cmp (sizem1
, true) >= 0)
2747 /* the range covers all values. */
2748 set_value_range_to_varying (vr
);
2752 /* The following should handle the wrapping and selecting
2753 VR_ANTI_RANGE for us. */
2754 min
= double_int_to_tree (expr_type
, prod0l
);
2755 max
= double_int_to_tree (expr_type
, prod3l
);
2756 set_and_canonicalize_value_range (vr
, VR_RANGE
, min
, max
, NULL
);
2760 /* If we have an unsigned MULT_EXPR with two VR_ANTI_RANGEs,
2761 drop to VR_VARYING. It would take more effort to compute a
2762 precise range for such a case. For example, if we have
2763 op0 == 65536 and op1 == 65536 with their ranges both being
2764 ~[0,0] on a 32-bit machine, we would have op0 * op1 == 0, so
2765 we cannot claim that the product is in ~[0,0]. Note that we
2766 are guaranteed to have vr0.type == vr1.type at this
2768 if (vr0
.type
== VR_ANTI_RANGE
2769 && !TYPE_OVERFLOW_UNDEFINED (expr_type
))
2771 set_value_range_to_varying (vr
);
2775 extract_range_from_multiplicative_op_1 (vr
, code
, &vr0
, &vr1
);
2778 else if (code
== RSHIFT_EXPR
2779 || code
== LSHIFT_EXPR
)
2781 /* If we have a RSHIFT_EXPR with any shift values outside [0..prec-1],
2782 then drop to VR_VARYING. Outside of this range we get undefined
2783 behavior from the shift operation. We cannot even trust
2784 SHIFT_COUNT_TRUNCATED at this stage, because that applies to rtl
2785 shifts, and the operation at the tree level may be widened. */
2786 if (range_int_cst_p (&vr1
)
2787 && compare_tree_int (vr1
.min
, 0) >= 0
2788 && compare_tree_int (vr1
.max
, TYPE_PRECISION (expr_type
)) == -1)
2790 if (code
== RSHIFT_EXPR
)
2792 extract_range_from_multiplicative_op_1 (vr
, code
, &vr0
, &vr1
);
2795 /* We can map lshifts by constants to MULT_EXPR handling. */
2796 else if (code
== LSHIFT_EXPR
2797 && range_int_cst_singleton_p (&vr1
))
2799 bool saved_flag_wrapv
;
2800 value_range_t vr1p
= VR_INITIALIZER
;
2801 vr1p
.type
= VR_RANGE
;
2803 = double_int_to_tree (expr_type
,
2805 .llshift (TREE_INT_CST_LOW (vr1
.min
),
2806 TYPE_PRECISION (expr_type
)));
2807 vr1p
.max
= vr1p
.min
;
2808 /* We have to use a wrapping multiply though as signed overflow
2809 on lshifts is implementation defined in C89. */
2810 saved_flag_wrapv
= flag_wrapv
;
2812 extract_range_from_binary_expr_1 (vr
, MULT_EXPR
, expr_type
,
2814 flag_wrapv
= saved_flag_wrapv
;
2817 else if (code
== LSHIFT_EXPR
2818 && range_int_cst_p (&vr0
))
2820 int prec
= TYPE_PRECISION (expr_type
);
2821 int overflow_pos
= prec
;
2823 double_int bound
, complement
, low_bound
, high_bound
;
2824 bool uns
= TYPE_UNSIGNED (expr_type
);
2825 bool in_bounds
= false;
2830 bound_shift
= overflow_pos
- TREE_INT_CST_LOW (vr1
.max
);
2831 /* If bound_shift == HOST_BITS_PER_DOUBLE_INT, the llshift can
2832 overflow. However, for that to happen, vr1.max needs to be
2833 zero, which means vr1 is a singleton range of zero, which
2834 means it should be handled by the previous LSHIFT_EXPR
2836 bound
= double_int_one
.llshift (bound_shift
, prec
);
2837 complement
= ~(bound
- double_int_one
);
2842 high_bound
= complement
.zext (prec
);
2843 if (tree_to_double_int (vr0
.max
).ult (low_bound
))
2845 /* [5, 6] << [1, 2] == [10, 24]. */
2846 /* We're shifting out only zeroes, the value increases
2850 else if (high_bound
.ult (tree_to_double_int (vr0
.min
)))
2852 /* [0xffffff00, 0xffffffff] << [1, 2]
2853 == [0xfffffc00, 0xfffffffe]. */
2854 /* We're shifting out only ones, the value decreases
2861 /* [-1, 1] << [1, 2] == [-4, 4]. */
2862 low_bound
= complement
.sext (prec
);
2864 if (tree_to_double_int (vr0
.max
).slt (high_bound
)
2865 && low_bound
.slt (tree_to_double_int (vr0
.min
)))
2867 /* For non-negative numbers, we're shifting out only
2868 zeroes, the value increases monotonically.
2869 For negative numbers, we're shifting out only ones, the
2870 value decreases monotomically. */
2877 extract_range_from_multiplicative_op_1 (vr
, code
, &vr0
, &vr1
);
2882 set_value_range_to_varying (vr
);
2885 else if (code
== TRUNC_DIV_EXPR
2886 || code
== FLOOR_DIV_EXPR
2887 || code
== CEIL_DIV_EXPR
2888 || code
== EXACT_DIV_EXPR
2889 || code
== ROUND_DIV_EXPR
)
2891 if (vr0
.type
!= VR_RANGE
|| symbolic_range_p (&vr0
))
2893 /* For division, if op1 has VR_RANGE but op0 does not, something
2894 can be deduced just from that range. Say [min, max] / [4, max]
2895 gives [min / 4, max / 4] range. */
2896 if (vr1
.type
== VR_RANGE
2897 && !symbolic_range_p (&vr1
)
2898 && range_includes_zero_p (vr1
.min
, vr1
.max
) == 0)
2900 vr0
.type
= type
= VR_RANGE
;
2901 vr0
.min
= vrp_val_min (expr_type
);
2902 vr0
.max
= vrp_val_max (expr_type
);
2906 set_value_range_to_varying (vr
);
2911 /* For divisions, if flag_non_call_exceptions is true, we must
2912 not eliminate a division by zero. */
2913 if (cfun
->can_throw_non_call_exceptions
2914 && (vr1
.type
!= VR_RANGE
2915 || range_includes_zero_p (vr1
.min
, vr1
.max
) != 0))
2917 set_value_range_to_varying (vr
);
2921 /* For divisions, if op0 is VR_RANGE, we can deduce a range
2922 even if op1 is VR_VARYING, VR_ANTI_RANGE, symbolic or can
2924 if (vr0
.type
== VR_RANGE
2925 && (vr1
.type
!= VR_RANGE
2926 || range_includes_zero_p (vr1
.min
, vr1
.max
) != 0))
2928 tree zero
= build_int_cst (TREE_TYPE (vr0
.min
), 0);
2933 if (TYPE_UNSIGNED (expr_type
)
2934 || value_range_nonnegative_p (&vr1
))
2936 /* For unsigned division or when divisor is known
2937 to be non-negative, the range has to cover
2938 all numbers from 0 to max for positive max
2939 and all numbers from min to 0 for negative min. */
2940 cmp
= compare_values (vr0
.max
, zero
);
2943 else if (cmp
== 0 || cmp
== 1)
2947 cmp
= compare_values (vr0
.min
, zero
);
2950 else if (cmp
== 0 || cmp
== -1)
2957 /* Otherwise the range is -max .. max or min .. -min
2958 depending on which bound is bigger in absolute value,
2959 as the division can change the sign. */
2960 abs_extent_range (vr
, vr0
.min
, vr0
.max
);
2963 if (type
== VR_VARYING
)
2965 set_value_range_to_varying (vr
);
2971 extract_range_from_multiplicative_op_1 (vr
, code
, &vr0
, &vr1
);
2975 else if (code
== TRUNC_MOD_EXPR
)
2977 if (vr1
.type
!= VR_RANGE
2978 || range_includes_zero_p (vr1
.min
, vr1
.max
) != 0
2979 || vrp_val_is_min (vr1
.min
))
2981 set_value_range_to_varying (vr
);
2985 /* Compute MAX <|vr1.min|, |vr1.max|> - 1. */
2986 max
= fold_unary_to_constant (ABS_EXPR
, expr_type
, vr1
.min
);
2987 if (tree_int_cst_lt (max
, vr1
.max
))
2989 max
= int_const_binop (MINUS_EXPR
, max
, integer_one_node
);
2990 /* If the dividend is non-negative the modulus will be
2991 non-negative as well. */
2992 if (TYPE_UNSIGNED (expr_type
)
2993 || value_range_nonnegative_p (&vr0
))
2994 min
= build_int_cst (TREE_TYPE (max
), 0);
2996 min
= fold_unary_to_constant (NEGATE_EXPR
, expr_type
, max
);
2998 else if (code
== BIT_AND_EXPR
|| code
== BIT_IOR_EXPR
|| code
== BIT_XOR_EXPR
)
3000 bool int_cst_range0
, int_cst_range1
;
3001 double_int may_be_nonzero0
, may_be_nonzero1
;
3002 double_int must_be_nonzero0
, must_be_nonzero1
;
3004 int_cst_range0
= zero_nonzero_bits_from_vr (&vr0
, &may_be_nonzero0
,
3006 int_cst_range1
= zero_nonzero_bits_from_vr (&vr1
, &may_be_nonzero1
,
3010 if (code
== BIT_AND_EXPR
)
3013 min
= double_int_to_tree (expr_type
,
3014 must_be_nonzero0
& must_be_nonzero1
);
3015 dmax
= may_be_nonzero0
& may_be_nonzero1
;
3016 /* If both input ranges contain only negative values we can
3017 truncate the result range maximum to the minimum of the
3018 input range maxima. */
3019 if (int_cst_range0
&& int_cst_range1
3020 && tree_int_cst_sgn (vr0
.max
) < 0
3021 && tree_int_cst_sgn (vr1
.max
) < 0)
3023 dmax
= dmax
.min (tree_to_double_int (vr0
.max
),
3024 TYPE_UNSIGNED (expr_type
));
3025 dmax
= dmax
.min (tree_to_double_int (vr1
.max
),
3026 TYPE_UNSIGNED (expr_type
));
3028 /* If either input range contains only non-negative values
3029 we can truncate the result range maximum to the respective
3030 maximum of the input range. */
3031 if (int_cst_range0
&& tree_int_cst_sgn (vr0
.min
) >= 0)
3032 dmax
= dmax
.min (tree_to_double_int (vr0
.max
),
3033 TYPE_UNSIGNED (expr_type
));
3034 if (int_cst_range1
&& tree_int_cst_sgn (vr1
.min
) >= 0)
3035 dmax
= dmax
.min (tree_to_double_int (vr1
.max
),
3036 TYPE_UNSIGNED (expr_type
));
3037 max
= double_int_to_tree (expr_type
, dmax
);
3039 else if (code
== BIT_IOR_EXPR
)
3042 max
= double_int_to_tree (expr_type
,
3043 may_be_nonzero0
| may_be_nonzero1
);
3044 dmin
= must_be_nonzero0
| must_be_nonzero1
;
3045 /* If the input ranges contain only positive values we can
3046 truncate the minimum of the result range to the maximum
3047 of the input range minima. */
3048 if (int_cst_range0
&& int_cst_range1
3049 && tree_int_cst_sgn (vr0
.min
) >= 0
3050 && tree_int_cst_sgn (vr1
.min
) >= 0)
3052 dmin
= dmin
.max (tree_to_double_int (vr0
.min
),
3053 TYPE_UNSIGNED (expr_type
));
3054 dmin
= dmin
.max (tree_to_double_int (vr1
.min
),
3055 TYPE_UNSIGNED (expr_type
));
3057 /* If either input range contains only negative values
3058 we can truncate the minimum of the result range to the
3059 respective minimum range. */
3060 if (int_cst_range0
&& tree_int_cst_sgn (vr0
.max
) < 0)
3061 dmin
= dmin
.max (tree_to_double_int (vr0
.min
),
3062 TYPE_UNSIGNED (expr_type
));
3063 if (int_cst_range1
&& tree_int_cst_sgn (vr1
.max
) < 0)
3064 dmin
= dmin
.max (tree_to_double_int (vr1
.min
),
3065 TYPE_UNSIGNED (expr_type
));
3066 min
= double_int_to_tree (expr_type
, dmin
);
3068 else if (code
== BIT_XOR_EXPR
)
3070 double_int result_zero_bits
, result_one_bits
;
3071 result_zero_bits
= (must_be_nonzero0
& must_be_nonzero1
)
3072 | ~(may_be_nonzero0
| may_be_nonzero1
);
3073 result_one_bits
= must_be_nonzero0
.and_not (may_be_nonzero1
)
3074 | must_be_nonzero1
.and_not (may_be_nonzero0
);
3075 max
= double_int_to_tree (expr_type
, ~result_zero_bits
);
3076 min
= double_int_to_tree (expr_type
, result_one_bits
);
3077 /* If the range has all positive or all negative values the
3078 result is better than VARYING. */
3079 if (tree_int_cst_sgn (min
) < 0
3080 || tree_int_cst_sgn (max
) >= 0)
3083 max
= min
= NULL_TREE
;
3089 /* If either MIN or MAX overflowed, then set the resulting range to
3090 VARYING. But we do accept an overflow infinity
3092 if (min
== NULL_TREE
3093 || !is_gimple_min_invariant (min
)
3094 || (TREE_OVERFLOW (min
) && !is_overflow_infinity (min
))
3096 || !is_gimple_min_invariant (max
)
3097 || (TREE_OVERFLOW (max
) && !is_overflow_infinity (max
)))
3099 set_value_range_to_varying (vr
);
3105 2) [-INF, +-INF(OVF)]
3106 3) [+-INF(OVF), +INF]
3107 4) [+-INF(OVF), +-INF(OVF)]
3108 We learn nothing when we have INF and INF(OVF) on both sides.
3109 Note that we do accept [-INF, -INF] and [+INF, +INF] without
3111 if ((vrp_val_is_min (min
) || is_overflow_infinity (min
))
3112 && (vrp_val_is_max (max
) || is_overflow_infinity (max
)))
3114 set_value_range_to_varying (vr
);
3118 cmp
= compare_values (min
, max
);
3119 if (cmp
== -2 || cmp
== 1)
3121 /* If the new range has its limits swapped around (MIN > MAX),
3122 then the operation caused one of them to wrap around, mark
3123 the new range VARYING. */
3124 set_value_range_to_varying (vr
);
3127 set_value_range (vr
, type
, min
, max
, NULL
);
3130 /* Extract range information from a binary expression OP0 CODE OP1 based on
3131 the ranges of each of its operands with resulting type EXPR_TYPE.
3132 The resulting range is stored in *VR. */
3135 extract_range_from_binary_expr (value_range_t
*vr
,
3136 enum tree_code code
,
3137 tree expr_type
, tree op0
, tree op1
)
3139 value_range_t vr0
= VR_INITIALIZER
;
3140 value_range_t vr1
= VR_INITIALIZER
;
3142 /* Get value ranges for each operand. For constant operands, create
3143 a new value range with the operand to simplify processing. */
3144 if (TREE_CODE (op0
) == SSA_NAME
)
3145 vr0
= *(get_value_range (op0
));
3146 else if (is_gimple_min_invariant (op0
))
3147 set_value_range_to_value (&vr0
, op0
, NULL
);
3149 set_value_range_to_varying (&vr0
);
3151 if (TREE_CODE (op1
) == SSA_NAME
)
3152 vr1
= *(get_value_range (op1
));
3153 else if (is_gimple_min_invariant (op1
))
3154 set_value_range_to_value (&vr1
, op1
, NULL
);
3156 set_value_range_to_varying (&vr1
);
3158 extract_range_from_binary_expr_1 (vr
, code
, expr_type
, &vr0
, &vr1
);
3161 /* Extract range information from a unary operation CODE based on
3162 the range of its operand *VR0 with type OP0_TYPE with resulting type TYPE.
3163 The The resulting range is stored in *VR. */
3166 extract_range_from_unary_expr_1 (value_range_t
*vr
,
3167 enum tree_code code
, tree type
,
3168 value_range_t
*vr0_
, tree op0_type
)
3170 value_range_t vr0
= *vr0_
, vrtem0
= VR_INITIALIZER
, vrtem1
= VR_INITIALIZER
;
3172 /* VRP only operates on integral and pointer types. */
3173 if (!(INTEGRAL_TYPE_P (op0_type
)
3174 || POINTER_TYPE_P (op0_type
))
3175 || !(INTEGRAL_TYPE_P (type
)
3176 || POINTER_TYPE_P (type
)))
3178 set_value_range_to_varying (vr
);
3182 /* If VR0 is UNDEFINED, so is the result. */
3183 if (vr0
.type
== VR_UNDEFINED
)
3185 set_value_range_to_undefined (vr
);
3189 /* Handle operations that we express in terms of others. */
3190 if (code
== PAREN_EXPR
)
3192 /* PAREN_EXPR is a simple copy. */
3193 copy_value_range (vr
, &vr0
);
3196 else if (code
== NEGATE_EXPR
)
3198 /* -X is simply 0 - X, so re-use existing code that also handles
3199 anti-ranges fine. */
3200 value_range_t zero
= VR_INITIALIZER
;
3201 set_value_range_to_value (&zero
, build_int_cst (type
, 0), NULL
);
3202 extract_range_from_binary_expr_1 (vr
, MINUS_EXPR
, type
, &zero
, &vr0
);
3205 else if (code
== BIT_NOT_EXPR
)
3207 /* ~X is simply -1 - X, so re-use existing code that also handles
3208 anti-ranges fine. */
3209 value_range_t minusone
= VR_INITIALIZER
;
3210 set_value_range_to_value (&minusone
, build_int_cst (type
, -1), NULL
);
3211 extract_range_from_binary_expr_1 (vr
, MINUS_EXPR
,
3212 type
, &minusone
, &vr0
);
3216 /* Now canonicalize anti-ranges to ranges when they are not symbolic
3217 and express op ~[] as (op []') U (op []''). */
3218 if (vr0
.type
== VR_ANTI_RANGE
3219 && ranges_from_anti_range (&vr0
, &vrtem0
, &vrtem1
))
3221 extract_range_from_unary_expr_1 (vr
, code
, type
, &vrtem0
, op0_type
);
3222 if (vrtem1
.type
!= VR_UNDEFINED
)
3224 value_range_t vrres
= VR_INITIALIZER
;
3225 extract_range_from_unary_expr_1 (&vrres
, code
, type
,
3227 vrp_meet (vr
, &vrres
);
3232 if (CONVERT_EXPR_CODE_P (code
))
3234 tree inner_type
= op0_type
;
3235 tree outer_type
= type
;
3237 /* If the expression evaluates to a pointer, we are only interested in
3238 determining if it evaluates to NULL [0, 0] or non-NULL (~[0, 0]). */
3239 if (POINTER_TYPE_P (type
))
3241 if (range_is_nonnull (&vr0
))
3242 set_value_range_to_nonnull (vr
, type
);
3243 else if (range_is_null (&vr0
))
3244 set_value_range_to_null (vr
, type
);
3246 set_value_range_to_varying (vr
);
3250 /* If VR0 is varying and we increase the type precision, assume
3251 a full range for the following transformation. */
3252 if (vr0
.type
== VR_VARYING
3253 && INTEGRAL_TYPE_P (inner_type
)
3254 && TYPE_PRECISION (inner_type
) < TYPE_PRECISION (outer_type
))
3256 vr0
.type
= VR_RANGE
;
3257 vr0
.min
= TYPE_MIN_VALUE (inner_type
);
3258 vr0
.max
= TYPE_MAX_VALUE (inner_type
);
3261 /* If VR0 is a constant range or anti-range and the conversion is
3262 not truncating we can convert the min and max values and
3263 canonicalize the resulting range. Otherwise we can do the
3264 conversion if the size of the range is less than what the
3265 precision of the target type can represent and the range is
3266 not an anti-range. */
3267 if ((vr0
.type
== VR_RANGE
3268 || vr0
.type
== VR_ANTI_RANGE
)
3269 && TREE_CODE (vr0
.min
) == INTEGER_CST
3270 && TREE_CODE (vr0
.max
) == INTEGER_CST
3271 && (!is_overflow_infinity (vr0
.min
)
3272 || (vr0
.type
== VR_RANGE
3273 && TYPE_PRECISION (outer_type
) > TYPE_PRECISION (inner_type
)
3274 && needs_overflow_infinity (outer_type
)
3275 && supports_overflow_infinity (outer_type
)))
3276 && (!is_overflow_infinity (vr0
.max
)
3277 || (vr0
.type
== VR_RANGE
3278 && TYPE_PRECISION (outer_type
) > TYPE_PRECISION (inner_type
)
3279 && needs_overflow_infinity (outer_type
)
3280 && supports_overflow_infinity (outer_type
)))
3281 && (TYPE_PRECISION (outer_type
) >= TYPE_PRECISION (inner_type
)
3282 || (vr0
.type
== VR_RANGE
3283 && integer_zerop (int_const_binop (RSHIFT_EXPR
,
3284 int_const_binop (MINUS_EXPR
, vr0
.max
, vr0
.min
),
3285 size_int (TYPE_PRECISION (outer_type
)))))))
3287 tree new_min
, new_max
;
3288 if (is_overflow_infinity (vr0
.min
))
3289 new_min
= negative_overflow_infinity (outer_type
);
3291 new_min
= force_fit_type_double (outer_type
,
3292 tree_to_double_int (vr0
.min
),
3294 if (is_overflow_infinity (vr0
.max
))
3295 new_max
= positive_overflow_infinity (outer_type
);
3297 new_max
= force_fit_type_double (outer_type
,
3298 tree_to_double_int (vr0
.max
),
3300 set_and_canonicalize_value_range (vr
, vr0
.type
,
3301 new_min
, new_max
, NULL
);
3305 set_value_range_to_varying (vr
);
3308 else if (code
== ABS_EXPR
)
3313 /* Pass through vr0 in the easy cases. */
3314 if (TYPE_UNSIGNED (type
)
3315 || value_range_nonnegative_p (&vr0
))
3317 copy_value_range (vr
, &vr0
);
3321 /* For the remaining varying or symbolic ranges we can't do anything
3323 if (vr0
.type
== VR_VARYING
3324 || symbolic_range_p (&vr0
))
3326 set_value_range_to_varying (vr
);
3330 /* -TYPE_MIN_VALUE = TYPE_MIN_VALUE with flag_wrapv so we can't get a
3332 if (!TYPE_OVERFLOW_UNDEFINED (type
)
3333 && ((vr0
.type
== VR_RANGE
3334 && vrp_val_is_min (vr0
.min
))
3335 || (vr0
.type
== VR_ANTI_RANGE
3336 && !vrp_val_is_min (vr0
.min
))))
3338 set_value_range_to_varying (vr
);
3342 /* ABS_EXPR may flip the range around, if the original range
3343 included negative values. */
3344 if (is_overflow_infinity (vr0
.min
))
3345 min
= positive_overflow_infinity (type
);
3346 else if (!vrp_val_is_min (vr0
.min
))
3347 min
= fold_unary_to_constant (code
, type
, vr0
.min
);
3348 else if (!needs_overflow_infinity (type
))
3349 min
= TYPE_MAX_VALUE (type
);
3350 else if (supports_overflow_infinity (type
))
3351 min
= positive_overflow_infinity (type
);
3354 set_value_range_to_varying (vr
);
3358 if (is_overflow_infinity (vr0
.max
))
3359 max
= positive_overflow_infinity (type
);
3360 else if (!vrp_val_is_min (vr0
.max
))
3361 max
= fold_unary_to_constant (code
, type
, vr0
.max
);
3362 else if (!needs_overflow_infinity (type
))
3363 max
= TYPE_MAX_VALUE (type
);
3364 else if (supports_overflow_infinity (type
)
3365 /* We shouldn't generate [+INF, +INF] as set_value_range
3366 doesn't like this and ICEs. */
3367 && !is_positive_overflow_infinity (min
))
3368 max
= positive_overflow_infinity (type
);
3371 set_value_range_to_varying (vr
);
3375 cmp
= compare_values (min
, max
);
3377 /* If a VR_ANTI_RANGEs contains zero, then we have
3378 ~[-INF, min(MIN, MAX)]. */
3379 if (vr0
.type
== VR_ANTI_RANGE
)
3381 if (range_includes_zero_p (vr0
.min
, vr0
.max
) == 1)
3383 /* Take the lower of the two values. */
3387 /* Create ~[-INF, min (abs(MIN), abs(MAX))]
3388 or ~[-INF + 1, min (abs(MIN), abs(MAX))] when
3389 flag_wrapv is set and the original anti-range doesn't include
3390 TYPE_MIN_VALUE, remember -TYPE_MIN_VALUE = TYPE_MIN_VALUE. */
3391 if (TYPE_OVERFLOW_WRAPS (type
))
3393 tree type_min_value
= TYPE_MIN_VALUE (type
);
3395 min
= (vr0
.min
!= type_min_value
3396 ? int_const_binop (PLUS_EXPR
, type_min_value
,
3402 if (overflow_infinity_range_p (&vr0
))
3403 min
= negative_overflow_infinity (type
);
3405 min
= TYPE_MIN_VALUE (type
);
3410 /* All else has failed, so create the range [0, INF], even for
3411 flag_wrapv since TYPE_MIN_VALUE is in the original
3413 vr0
.type
= VR_RANGE
;
3414 min
= build_int_cst (type
, 0);
3415 if (needs_overflow_infinity (type
))
3417 if (supports_overflow_infinity (type
))
3418 max
= positive_overflow_infinity (type
);
3421 set_value_range_to_varying (vr
);
3426 max
= TYPE_MAX_VALUE (type
);
3430 /* If the range contains zero then we know that the minimum value in the
3431 range will be zero. */
3432 else if (range_includes_zero_p (vr0
.min
, vr0
.max
) == 1)
3436 min
= build_int_cst (type
, 0);
3440 /* If the range was reversed, swap MIN and MAX. */
3449 cmp
= compare_values (min
, max
);
3450 if (cmp
== -2 || cmp
== 1)
3452 /* If the new range has its limits swapped around (MIN > MAX),
3453 then the operation caused one of them to wrap around, mark
3454 the new range VARYING. */
3455 set_value_range_to_varying (vr
);
3458 set_value_range (vr
, vr0
.type
, min
, max
, NULL
);
3462 /* For unhandled operations fall back to varying. */
3463 set_value_range_to_varying (vr
);
3468 /* Extract range information from a unary expression CODE OP0 based on
3469 the range of its operand with resulting type TYPE.
3470 The resulting range is stored in *VR. */
3473 extract_range_from_unary_expr (value_range_t
*vr
, enum tree_code code
,
3474 tree type
, tree op0
)
3476 value_range_t vr0
= VR_INITIALIZER
;
3478 /* Get value ranges for the operand. For constant operands, create
3479 a new value range with the operand to simplify processing. */
3480 if (TREE_CODE (op0
) == SSA_NAME
)
3481 vr0
= *(get_value_range (op0
));
3482 else if (is_gimple_min_invariant (op0
))
3483 set_value_range_to_value (&vr0
, op0
, NULL
);
3485 set_value_range_to_varying (&vr0
);
3487 extract_range_from_unary_expr_1 (vr
, code
, type
, &vr0
, TREE_TYPE (op0
));
3491 /* Extract range information from a conditional expression STMT based on
3492 the ranges of each of its operands and the expression code. */
3495 extract_range_from_cond_expr (value_range_t
*vr
, gimple stmt
)
3498 value_range_t vr0
= VR_INITIALIZER
;
3499 value_range_t vr1
= VR_INITIALIZER
;
3501 /* Get value ranges for each operand. For constant operands, create
3502 a new value range with the operand to simplify processing. */
3503 op0
= gimple_assign_rhs2 (stmt
);
3504 if (TREE_CODE (op0
) == SSA_NAME
)
3505 vr0
= *(get_value_range (op0
));
3506 else if (is_gimple_min_invariant (op0
))
3507 set_value_range_to_value (&vr0
, op0
, NULL
);
3509 set_value_range_to_varying (&vr0
);
3511 op1
= gimple_assign_rhs3 (stmt
);
3512 if (TREE_CODE (op1
) == SSA_NAME
)
3513 vr1
= *(get_value_range (op1
));
3514 else if (is_gimple_min_invariant (op1
))
3515 set_value_range_to_value (&vr1
, op1
, NULL
);
3517 set_value_range_to_varying (&vr1
);
3519 /* The resulting value range is the union of the operand ranges */
3520 copy_value_range (vr
, &vr0
);
3521 vrp_meet (vr
, &vr1
);
3525 /* Extract range information from a comparison expression EXPR based
3526 on the range of its operand and the expression code. */
3529 extract_range_from_comparison (value_range_t
*vr
, enum tree_code code
,
3530 tree type
, tree op0
, tree op1
)
3535 val
= vrp_evaluate_conditional_warnv_with_ops (code
, op0
, op1
, false, &sop
,
3538 /* A disadvantage of using a special infinity as an overflow
3539 representation is that we lose the ability to record overflow
3540 when we don't have an infinity. So we have to ignore a result
3541 which relies on overflow. */
3543 if (val
&& !is_overflow_infinity (val
) && !sop
)
3545 /* Since this expression was found on the RHS of an assignment,
3546 its type may be different from _Bool. Convert VAL to EXPR's
3548 val
= fold_convert (type
, val
);
3549 if (is_gimple_min_invariant (val
))
3550 set_value_range_to_value (vr
, val
, vr
->equiv
);
3552 set_value_range (vr
, VR_RANGE
, val
, val
, vr
->equiv
);
3555 /* The result of a comparison is always true or false. */
3556 set_value_range_to_truthvalue (vr
, type
);
3559 /* Try to derive a nonnegative or nonzero range out of STMT relying
3560 primarily on generic routines in fold in conjunction with range data.
3561 Store the result in *VR */
3564 extract_range_basic (value_range_t
*vr
, gimple stmt
)
3567 tree type
= gimple_expr_type (stmt
);
3569 if (INTEGRAL_TYPE_P (type
)
3570 && gimple_stmt_nonnegative_warnv_p (stmt
, &sop
))
3571 set_value_range_to_nonnegative (vr
, type
,
3572 sop
|| stmt_overflow_infinity (stmt
));
3573 else if (vrp_stmt_computes_nonzero (stmt
, &sop
)
3575 set_value_range_to_nonnull (vr
, type
);
3577 set_value_range_to_varying (vr
);
3581 /* Try to compute a useful range out of assignment STMT and store it
3585 extract_range_from_assignment (value_range_t
*vr
, gimple stmt
)
3587 enum tree_code code
= gimple_assign_rhs_code (stmt
);
3589 if (code
== ASSERT_EXPR
)
3590 extract_range_from_assert (vr
, gimple_assign_rhs1 (stmt
));
3591 else if (code
== SSA_NAME
)
3592 extract_range_from_ssa_name (vr
, gimple_assign_rhs1 (stmt
));
3593 else if (TREE_CODE_CLASS (code
) == tcc_binary
)
3594 extract_range_from_binary_expr (vr
, gimple_assign_rhs_code (stmt
),
3595 gimple_expr_type (stmt
),
3596 gimple_assign_rhs1 (stmt
),
3597 gimple_assign_rhs2 (stmt
));
3598 else if (TREE_CODE_CLASS (code
) == tcc_unary
)
3599 extract_range_from_unary_expr (vr
, gimple_assign_rhs_code (stmt
),
3600 gimple_expr_type (stmt
),
3601 gimple_assign_rhs1 (stmt
));
3602 else if (code
== COND_EXPR
)
3603 extract_range_from_cond_expr (vr
, stmt
);
3604 else if (TREE_CODE_CLASS (code
) == tcc_comparison
)
3605 extract_range_from_comparison (vr
, gimple_assign_rhs_code (stmt
),
3606 gimple_expr_type (stmt
),
3607 gimple_assign_rhs1 (stmt
),
3608 gimple_assign_rhs2 (stmt
));
3609 else if (get_gimple_rhs_class (code
) == GIMPLE_SINGLE_RHS
3610 && is_gimple_min_invariant (gimple_assign_rhs1 (stmt
)))
3611 set_value_range_to_value (vr
, gimple_assign_rhs1 (stmt
), NULL
);
3613 set_value_range_to_varying (vr
);
3615 if (vr
->type
== VR_VARYING
)
3616 extract_range_basic (vr
, stmt
);
3619 /* Given a range VR, a LOOP and a variable VAR, determine whether it
3620 would be profitable to adjust VR using scalar evolution information
3621 for VAR. If so, update VR with the new limits. */
3624 adjust_range_with_scev (value_range_t
*vr
, struct loop
*loop
,
3625 gimple stmt
, tree var
)
3627 tree init
, step
, chrec
, tmin
, tmax
, min
, max
, type
, tem
;
3628 enum ev_direction dir
;
3630 /* TODO. Don't adjust anti-ranges. An anti-range may provide
3631 better opportunities than a regular range, but I'm not sure. */
3632 if (vr
->type
== VR_ANTI_RANGE
)
3635 chrec
= instantiate_parameters (loop
, analyze_scalar_evolution (loop
, var
));
3637 /* Like in PR19590, scev can return a constant function. */
3638 if (is_gimple_min_invariant (chrec
))
3640 set_value_range_to_value (vr
, chrec
, vr
->equiv
);
3644 if (TREE_CODE (chrec
) != POLYNOMIAL_CHREC
)
3647 init
= initial_condition_in_loop_num (chrec
, loop
->num
);
3648 tem
= op_with_constant_singleton_value_range (init
);
3651 step
= evolution_part_in_loop_num (chrec
, loop
->num
);
3652 tem
= op_with_constant_singleton_value_range (step
);
3656 /* If STEP is symbolic, we can't know whether INIT will be the
3657 minimum or maximum value in the range. Also, unless INIT is
3658 a simple expression, compare_values and possibly other functions
3659 in tree-vrp won't be able to handle it. */
3660 if (step
== NULL_TREE
3661 || !is_gimple_min_invariant (step
)
3662 || !valid_value_p (init
))
3665 dir
= scev_direction (chrec
);
3666 if (/* Do not adjust ranges if we do not know whether the iv increases
3667 or decreases, ... */
3668 dir
== EV_DIR_UNKNOWN
3669 /* ... or if it may wrap. */
3670 || scev_probably_wraps_p (init
, step
, stmt
, get_chrec_loop (chrec
),
3674 /* We use TYPE_MIN_VALUE and TYPE_MAX_VALUE here instead of
3675 negative_overflow_infinity and positive_overflow_infinity,
3676 because we have concluded that the loop probably does not
3679 type
= TREE_TYPE (var
);
3680 if (POINTER_TYPE_P (type
) || !TYPE_MIN_VALUE (type
))
3681 tmin
= lower_bound_in_type (type
, type
);
3683 tmin
= TYPE_MIN_VALUE (type
);
3684 if (POINTER_TYPE_P (type
) || !TYPE_MAX_VALUE (type
))
3685 tmax
= upper_bound_in_type (type
, type
);
3687 tmax
= TYPE_MAX_VALUE (type
);
3689 /* Try to use estimated number of iterations for the loop to constrain the
3690 final value in the evolution. */
3691 if (TREE_CODE (step
) == INTEGER_CST
3692 && is_gimple_val (init
)
3693 && (TREE_CODE (init
) != SSA_NAME
3694 || get_value_range (init
)->type
== VR_RANGE
))
3698 /* We are only entering here for loop header PHI nodes, so using
3699 the number of latch executions is the correct thing to use. */
3700 if (max_loop_iterations (loop
, &nit
))
3702 value_range_t maxvr
= VR_INITIALIZER
;
3704 bool unsigned_p
= TYPE_UNSIGNED (TREE_TYPE (step
));
3705 bool overflow
= false;
3707 dtmp
= tree_to_double_int (step
)
3708 .mul_with_sign (nit
, unsigned_p
, &overflow
);
3709 /* If the multiplication overflowed we can't do a meaningful
3710 adjustment. Likewise if the result doesn't fit in the type
3711 of the induction variable. For a signed type we have to
3712 check whether the result has the expected signedness which
3713 is that of the step as number of iterations is unsigned. */
3715 && double_int_fits_to_tree_p (TREE_TYPE (init
), dtmp
)
3717 || ((dtmp
.high
^ TREE_INT_CST_HIGH (step
)) >= 0)))
3719 tem
= double_int_to_tree (TREE_TYPE (init
), dtmp
);
3720 extract_range_from_binary_expr (&maxvr
, PLUS_EXPR
,
3721 TREE_TYPE (init
), init
, tem
);
3722 /* Likewise if the addition did. */
3723 if (maxvr
.type
== VR_RANGE
)
3732 if (vr
->type
== VR_VARYING
|| vr
->type
== VR_UNDEFINED
)
3737 /* For VARYING or UNDEFINED ranges, just about anything we get
3738 from scalar evolutions should be better. */
3740 if (dir
== EV_DIR_DECREASES
)
3745 /* If we would create an invalid range, then just assume we
3746 know absolutely nothing. This may be over-conservative,
3747 but it's clearly safe, and should happen only in unreachable
3748 parts of code, or for invalid programs. */
3749 if (compare_values (min
, max
) == 1)
3752 set_value_range (vr
, VR_RANGE
, min
, max
, vr
->equiv
);
3754 else if (vr
->type
== VR_RANGE
)
3759 if (dir
== EV_DIR_DECREASES
)
3761 /* INIT is the maximum value. If INIT is lower than VR->MAX
3762 but no smaller than VR->MIN, set VR->MAX to INIT. */
3763 if (compare_values (init
, max
) == -1)
3766 /* According to the loop information, the variable does not
3767 overflow. If we think it does, probably because of an
3768 overflow due to arithmetic on a different INF value,
3770 if (is_negative_overflow_infinity (min
)
3771 || compare_values (min
, tmin
) == -1)
3777 /* If INIT is bigger than VR->MIN, set VR->MIN to INIT. */
3778 if (compare_values (init
, min
) == 1)
3781 if (is_positive_overflow_infinity (max
)
3782 || compare_values (tmax
, max
) == -1)
3786 /* If we just created an invalid range with the minimum
3787 greater than the maximum, we fail conservatively.
3788 This should happen only in unreachable
3789 parts of code, or for invalid programs. */
3790 if (compare_values (min
, max
) == 1)
3793 set_value_range (vr
, VR_RANGE
, min
, max
, vr
->equiv
);
3797 /* Return true if VAR may overflow at STMT. This checks any available
3798 loop information to see if we can determine that VAR does not
3802 vrp_var_may_overflow (tree var
, gimple stmt
)
3805 tree chrec
, init
, step
;
3807 if (current_loops
== NULL
)
3810 l
= loop_containing_stmt (stmt
);
3815 chrec
= instantiate_parameters (l
, analyze_scalar_evolution (l
, var
));
3816 if (TREE_CODE (chrec
) != POLYNOMIAL_CHREC
)
3819 init
= initial_condition_in_loop_num (chrec
, l
->num
);
3820 step
= evolution_part_in_loop_num (chrec
, l
->num
);
3822 if (step
== NULL_TREE
3823 || !is_gimple_min_invariant (step
)
3824 || !valid_value_p (init
))
3827 /* If we get here, we know something useful about VAR based on the
3828 loop information. If it wraps, it may overflow. */
3830 if (scev_probably_wraps_p (init
, step
, stmt
, get_chrec_loop (chrec
),
3834 if (dump_file
&& (dump_flags
& TDF_DETAILS
) != 0)
3836 print_generic_expr (dump_file
, var
, 0);
3837 fprintf (dump_file
, ": loop information indicates does not overflow\n");
3844 /* Given two numeric value ranges VR0, VR1 and a comparison code COMP:
3846 - Return BOOLEAN_TRUE_NODE if VR0 COMP VR1 always returns true for
3847 all the values in the ranges.
3849 - Return BOOLEAN_FALSE_NODE if the comparison always returns false.
3851 - Return NULL_TREE if it is not always possible to determine the
3852 value of the comparison.
3854 Also set *STRICT_OVERFLOW_P to indicate whether a range with an
3855 overflow infinity was used in the test. */
3859 compare_ranges (enum tree_code comp
, value_range_t
*vr0
, value_range_t
*vr1
,
3860 bool *strict_overflow_p
)
3862 /* VARYING or UNDEFINED ranges cannot be compared. */
3863 if (vr0
->type
== VR_VARYING
3864 || vr0
->type
== VR_UNDEFINED
3865 || vr1
->type
== VR_VARYING
3866 || vr1
->type
== VR_UNDEFINED
)
3869 /* Anti-ranges need to be handled separately. */
3870 if (vr0
->type
== VR_ANTI_RANGE
|| vr1
->type
== VR_ANTI_RANGE
)
3872 /* If both are anti-ranges, then we cannot compute any
3874 if (vr0
->type
== VR_ANTI_RANGE
&& vr1
->type
== VR_ANTI_RANGE
)
3877 /* These comparisons are never statically computable. */
3884 /* Equality can be computed only between a range and an
3885 anti-range. ~[VAL1, VAL2] == [VAL1, VAL2] is always false. */
3886 if (vr0
->type
== VR_RANGE
)
3888 /* To simplify processing, make VR0 the anti-range. */
3889 value_range_t
*tmp
= vr0
;
3894 gcc_assert (comp
== NE_EXPR
|| comp
== EQ_EXPR
);
3896 if (compare_values_warnv (vr0
->min
, vr1
->min
, strict_overflow_p
) == 0
3897 && compare_values_warnv (vr0
->max
, vr1
->max
, strict_overflow_p
) == 0)
3898 return (comp
== NE_EXPR
) ? boolean_true_node
: boolean_false_node
;
3903 if (!usable_range_p (vr0
, strict_overflow_p
)
3904 || !usable_range_p (vr1
, strict_overflow_p
))
3907 /* Simplify processing. If COMP is GT_EXPR or GE_EXPR, switch the
3908 operands around and change the comparison code. */
3909 if (comp
== GT_EXPR
|| comp
== GE_EXPR
)
3912 comp
= (comp
== GT_EXPR
) ? LT_EXPR
: LE_EXPR
;
3918 if (comp
== EQ_EXPR
)
3920 /* Equality may only be computed if both ranges represent
3921 exactly one value. */
3922 if (compare_values_warnv (vr0
->min
, vr0
->max
, strict_overflow_p
) == 0
3923 && compare_values_warnv (vr1
->min
, vr1
->max
, strict_overflow_p
) == 0)
3925 int cmp_min
= compare_values_warnv (vr0
->min
, vr1
->min
,
3927 int cmp_max
= compare_values_warnv (vr0
->max
, vr1
->max
,
3929 if (cmp_min
== 0 && cmp_max
== 0)
3930 return boolean_true_node
;
3931 else if (cmp_min
!= -2 && cmp_max
!= -2)
3932 return boolean_false_node
;
3934 /* If [V0_MIN, V1_MAX] < [V1_MIN, V1_MAX] then V0 != V1. */
3935 else if (compare_values_warnv (vr0
->min
, vr1
->max
,
3936 strict_overflow_p
) == 1
3937 || compare_values_warnv (vr1
->min
, vr0
->max
,
3938 strict_overflow_p
) == 1)
3939 return boolean_false_node
;
3943 else if (comp
== NE_EXPR
)
3947 /* If VR0 is completely to the left or completely to the right
3948 of VR1, they are always different. Notice that we need to
3949 make sure that both comparisons yield similar results to
3950 avoid comparing values that cannot be compared at
3952 cmp1
= compare_values_warnv (vr0
->max
, vr1
->min
, strict_overflow_p
);
3953 cmp2
= compare_values_warnv (vr0
->min
, vr1
->max
, strict_overflow_p
);
3954 if ((cmp1
== -1 && cmp2
== -1) || (cmp1
== 1 && cmp2
== 1))
3955 return boolean_true_node
;
3957 /* If VR0 and VR1 represent a single value and are identical,
3959 else if (compare_values_warnv (vr0
->min
, vr0
->max
,
3960 strict_overflow_p
) == 0
3961 && compare_values_warnv (vr1
->min
, vr1
->max
,
3962 strict_overflow_p
) == 0
3963 && compare_values_warnv (vr0
->min
, vr1
->min
,
3964 strict_overflow_p
) == 0
3965 && compare_values_warnv (vr0
->max
, vr1
->max
,
3966 strict_overflow_p
) == 0)
3967 return boolean_false_node
;
3969 /* Otherwise, they may or may not be different. */
3973 else if (comp
== LT_EXPR
|| comp
== LE_EXPR
)
3977 /* If VR0 is to the left of VR1, return true. */
3978 tst
= compare_values_warnv (vr0
->max
, vr1
->min
, strict_overflow_p
);
3979 if ((comp
== LT_EXPR
&& tst
== -1)
3980 || (comp
== LE_EXPR
&& (tst
== -1 || tst
== 0)))
3982 if (overflow_infinity_range_p (vr0
)
3983 || overflow_infinity_range_p (vr1
))
3984 *strict_overflow_p
= true;
3985 return boolean_true_node
;
3988 /* If VR0 is to the right of VR1, return false. */
3989 tst
= compare_values_warnv (vr0
->min
, vr1
->max
, strict_overflow_p
);
3990 if ((comp
== LT_EXPR
&& (tst
== 0 || tst
== 1))
3991 || (comp
== LE_EXPR
&& tst
== 1))
3993 if (overflow_infinity_range_p (vr0
)
3994 || overflow_infinity_range_p (vr1
))
3995 *strict_overflow_p
= true;
3996 return boolean_false_node
;
3999 /* Otherwise, we don't know. */
4007 /* Given a value range VR, a value VAL and a comparison code COMP, return
4008 BOOLEAN_TRUE_NODE if VR COMP VAL always returns true for all the
4009 values in VR. Return BOOLEAN_FALSE_NODE if the comparison
4010 always returns false. Return NULL_TREE if it is not always
4011 possible to determine the value of the comparison. Also set
4012 *STRICT_OVERFLOW_P to indicate whether a range with an overflow
4013 infinity was used in the test. */
4016 compare_range_with_value (enum tree_code comp
, value_range_t
*vr
, tree val
,
4017 bool *strict_overflow_p
)
4019 if (vr
->type
== VR_VARYING
|| vr
->type
== VR_UNDEFINED
)
4022 /* Anti-ranges need to be handled separately. */
4023 if (vr
->type
== VR_ANTI_RANGE
)
4025 /* For anti-ranges, the only predicates that we can compute at
4026 compile time are equality and inequality. */
4033 /* ~[VAL_1, VAL_2] OP VAL is known if VAL_1 <= VAL <= VAL_2. */
4034 if (value_inside_range (val
, vr
->min
, vr
->max
) == 1)
4035 return (comp
== NE_EXPR
) ? boolean_true_node
: boolean_false_node
;
4040 if (!usable_range_p (vr
, strict_overflow_p
))
4043 if (comp
== EQ_EXPR
)
4045 /* EQ_EXPR may only be computed if VR represents exactly
4047 if (compare_values_warnv (vr
->min
, vr
->max
, strict_overflow_p
) == 0)
4049 int cmp
= compare_values_warnv (vr
->min
, val
, strict_overflow_p
);
4051 return boolean_true_node
;
4052 else if (cmp
== -1 || cmp
== 1 || cmp
== 2)
4053 return boolean_false_node
;
4055 else if (compare_values_warnv (val
, vr
->min
, strict_overflow_p
) == -1
4056 || compare_values_warnv (vr
->max
, val
, strict_overflow_p
) == -1)
4057 return boolean_false_node
;
4061 else if (comp
== NE_EXPR
)
4063 /* If VAL is not inside VR, then they are always different. */
4064 if (compare_values_warnv (vr
->max
, val
, strict_overflow_p
) == -1
4065 || compare_values_warnv (vr
->min
, val
, strict_overflow_p
) == 1)
4066 return boolean_true_node
;
4068 /* If VR represents exactly one value equal to VAL, then return
4070 if (compare_values_warnv (vr
->min
, vr
->max
, strict_overflow_p
) == 0
4071 && compare_values_warnv (vr
->min
, val
, strict_overflow_p
) == 0)
4072 return boolean_false_node
;
4074 /* Otherwise, they may or may not be different. */
4077 else if (comp
== LT_EXPR
|| comp
== LE_EXPR
)
4081 /* If VR is to the left of VAL, return true. */
4082 tst
= compare_values_warnv (vr
->max
, val
, strict_overflow_p
);
4083 if ((comp
== LT_EXPR
&& tst
== -1)
4084 || (comp
== LE_EXPR
&& (tst
== -1 || tst
== 0)))
4086 if (overflow_infinity_range_p (vr
))
4087 *strict_overflow_p
= true;
4088 return boolean_true_node
;
4091 /* If VR is to the right of VAL, return false. */
4092 tst
= compare_values_warnv (vr
->min
, val
, strict_overflow_p
);
4093 if ((comp
== LT_EXPR
&& (tst
== 0 || tst
== 1))
4094 || (comp
== LE_EXPR
&& tst
== 1))
4096 if (overflow_infinity_range_p (vr
))
4097 *strict_overflow_p
= true;
4098 return boolean_false_node
;
4101 /* Otherwise, we don't know. */
4104 else if (comp
== GT_EXPR
|| comp
== GE_EXPR
)
4108 /* If VR is to the right of VAL, return true. */
4109 tst
= compare_values_warnv (vr
->min
, val
, strict_overflow_p
);
4110 if ((comp
== GT_EXPR
&& tst
== 1)
4111 || (comp
== GE_EXPR
&& (tst
== 0 || tst
== 1)))
4113 if (overflow_infinity_range_p (vr
))
4114 *strict_overflow_p
= true;
4115 return boolean_true_node
;
4118 /* If VR is to the left of VAL, return false. */
4119 tst
= compare_values_warnv (vr
->max
, val
, strict_overflow_p
);
4120 if ((comp
== GT_EXPR
&& (tst
== -1 || tst
== 0))
4121 || (comp
== GE_EXPR
&& tst
== -1))
4123 if (overflow_infinity_range_p (vr
))
4124 *strict_overflow_p
= true;
4125 return boolean_false_node
;
4128 /* Otherwise, we don't know. */
4136 /* Debugging dumps. */
4138 void dump_value_range (FILE *, value_range_t
*);
4139 void debug_value_range (value_range_t
*);
4140 void dump_all_value_ranges (FILE *);
4141 void debug_all_value_ranges (void);
4142 void dump_vr_equiv (FILE *, bitmap
);
4143 void debug_vr_equiv (bitmap
);
4146 /* Dump value range VR to FILE. */
4149 dump_value_range (FILE *file
, value_range_t
*vr
)
4152 fprintf (file
, "[]");
4153 else if (vr
->type
== VR_UNDEFINED
)
4154 fprintf (file
, "UNDEFINED");
4155 else if (vr
->type
== VR_RANGE
|| vr
->type
== VR_ANTI_RANGE
)
4157 tree type
= TREE_TYPE (vr
->min
);
4159 fprintf (file
, "%s[", (vr
->type
== VR_ANTI_RANGE
) ? "~" : "");
4161 if (is_negative_overflow_infinity (vr
->min
))
4162 fprintf (file
, "-INF(OVF)");
4163 else if (INTEGRAL_TYPE_P (type
)
4164 && !TYPE_UNSIGNED (type
)
4165 && vrp_val_is_min (vr
->min
))
4166 fprintf (file
, "-INF");
4168 print_generic_expr (file
, vr
->min
, 0);
4170 fprintf (file
, ", ");
4172 if (is_positive_overflow_infinity (vr
->max
))
4173 fprintf (file
, "+INF(OVF)");
4174 else if (INTEGRAL_TYPE_P (type
)
4175 && vrp_val_is_max (vr
->max
))
4176 fprintf (file
, "+INF");
4178 print_generic_expr (file
, vr
->max
, 0);
4180 fprintf (file
, "]");
4187 fprintf (file
, " EQUIVALENCES: { ");
4189 EXECUTE_IF_SET_IN_BITMAP (vr
->equiv
, 0, i
, bi
)
4191 print_generic_expr (file
, ssa_name (i
), 0);
4192 fprintf (file
, " ");
4196 fprintf (file
, "} (%u elements)", c
);
4199 else if (vr
->type
== VR_VARYING
)
4200 fprintf (file
, "VARYING");
4202 fprintf (file
, "INVALID RANGE");
4206 /* Dump value range VR to stderr. */
4209 debug_value_range (value_range_t
*vr
)
4211 dump_value_range (stderr
, vr
);
4212 fprintf (stderr
, "\n");
4216 /* Dump value ranges of all SSA_NAMEs to FILE. */
4219 dump_all_value_ranges (FILE *file
)
4223 for (i
= 0; i
< num_vr_values
; i
++)
4227 print_generic_expr (file
, ssa_name (i
), 0);
4228 fprintf (file
, ": ");
4229 dump_value_range (file
, vr_value
[i
]);
4230 fprintf (file
, "\n");
4234 fprintf (file
, "\n");
4238 /* Dump all value ranges to stderr. */
4241 debug_all_value_ranges (void)
4243 dump_all_value_ranges (stderr
);
4247 /* Given a COND_EXPR COND of the form 'V OP W', and an SSA name V,
4248 create a new SSA name N and return the assertion assignment
4249 'V = ASSERT_EXPR <V, V OP W>'. */
4252 build_assert_expr_for (tree cond
, tree v
)
4257 gcc_assert (TREE_CODE (v
) == SSA_NAME
4258 && COMPARISON_CLASS_P (cond
));
4260 a
= build2 (ASSERT_EXPR
, TREE_TYPE (v
), v
, cond
);
4261 assertion
= gimple_build_assign (NULL_TREE
, a
);
4263 /* The new ASSERT_EXPR, creates a new SSA name that replaces the
4264 operand of the ASSERT_EXPR. Create it so the new name and the old one
4265 are registered in the replacement table so that we can fix the SSA web
4266 after adding all the ASSERT_EXPRs. */
4267 create_new_def_for (v
, assertion
, NULL
);
4273 /* Return false if EXPR is a predicate expression involving floating
4277 fp_predicate (gimple stmt
)
4279 GIMPLE_CHECK (stmt
, GIMPLE_COND
);
4281 return FLOAT_TYPE_P (TREE_TYPE (gimple_cond_lhs (stmt
)));
4285 /* If the range of values taken by OP can be inferred after STMT executes,
4286 return the comparison code (COMP_CODE_P) and value (VAL_P) that
4287 describes the inferred range. Return true if a range could be
4291 infer_value_range (gimple stmt
, tree op
, enum tree_code
*comp_code_p
, tree
*val_p
)
4294 *comp_code_p
= ERROR_MARK
;
4296 /* Do not attempt to infer anything in names that flow through
4298 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op
))
4301 /* Similarly, don't infer anything from statements that may throw
4303 if (stmt_could_throw_p (stmt
))
4306 /* If STMT is the last statement of a basic block with no
4307 successors, there is no point inferring anything about any of its
4308 operands. We would not be able to find a proper insertion point
4309 for the assertion, anyway. */
4310 if (stmt_ends_bb_p (stmt
) && EDGE_COUNT (gimple_bb (stmt
)->succs
) == 0)
4313 /* We can only assume that a pointer dereference will yield
4314 non-NULL if -fdelete-null-pointer-checks is enabled. */
4315 if (flag_delete_null_pointer_checks
4316 && POINTER_TYPE_P (TREE_TYPE (op
))
4317 && gimple_code (stmt
) != GIMPLE_ASM
)
4319 unsigned num_uses
, num_loads
, num_stores
;
4321 count_uses_and_derefs (op
, stmt
, &num_uses
, &num_loads
, &num_stores
);
4322 if (num_loads
+ num_stores
> 0)
4324 *val_p
= build_int_cst (TREE_TYPE (op
), 0);
4325 *comp_code_p
= NE_EXPR
;
4334 void dump_asserts_for (FILE *, tree
);
4335 void debug_asserts_for (tree
);
4336 void dump_all_asserts (FILE *);
4337 void debug_all_asserts (void);
4339 /* Dump all the registered assertions for NAME to FILE. */
4342 dump_asserts_for (FILE *file
, tree name
)
4346 fprintf (file
, "Assertions to be inserted for ");
4347 print_generic_expr (file
, name
, 0);
4348 fprintf (file
, "\n");
4350 loc
= asserts_for
[SSA_NAME_VERSION (name
)];
4353 fprintf (file
, "\t");
4354 print_gimple_stmt (file
, gsi_stmt (loc
->si
), 0, 0);
4355 fprintf (file
, "\n\tBB #%d", loc
->bb
->index
);
4358 fprintf (file
, "\n\tEDGE %d->%d", loc
->e
->src
->index
,
4359 loc
->e
->dest
->index
);
4360 dump_edge_info (file
, loc
->e
, dump_flags
, 0);
4362 fprintf (file
, "\n\tPREDICATE: ");
4363 print_generic_expr (file
, name
, 0);
4364 fprintf (file
, " %s ", tree_code_name
[(int)loc
->comp_code
]);
4365 print_generic_expr (file
, loc
->val
, 0);
4366 fprintf (file
, "\n\n");
4370 fprintf (file
, "\n");
4374 /* Dump all the registered assertions for NAME to stderr. */
4377 debug_asserts_for (tree name
)
4379 dump_asserts_for (stderr
, name
);
4383 /* Dump all the registered assertions for all the names to FILE. */
4386 dump_all_asserts (FILE *file
)
4391 fprintf (file
, "\nASSERT_EXPRs to be inserted\n\n");
4392 EXECUTE_IF_SET_IN_BITMAP (need_assert_for
, 0, i
, bi
)
4393 dump_asserts_for (file
, ssa_name (i
));
4394 fprintf (file
, "\n");
4398 /* Dump all the registered assertions for all the names to stderr. */
4401 debug_all_asserts (void)
4403 dump_all_asserts (stderr
);
4407 /* If NAME doesn't have an ASSERT_EXPR registered for asserting
4408 'EXPR COMP_CODE VAL' at a location that dominates block BB or
4409 E->DEST, then register this location as a possible insertion point
4410 for ASSERT_EXPR <NAME, EXPR COMP_CODE VAL>.
4412 BB, E and SI provide the exact insertion point for the new
4413 ASSERT_EXPR. If BB is NULL, then the ASSERT_EXPR is to be inserted
4414 on edge E. Otherwise, if E is NULL, the ASSERT_EXPR is inserted on
4415 BB. If SI points to a COND_EXPR or a SWITCH_EXPR statement, then E
4416 must not be NULL. */
4419 register_new_assert_for (tree name
, tree expr
,
4420 enum tree_code comp_code
,
4424 gimple_stmt_iterator si
)
4426 assert_locus_t n
, loc
, last_loc
;
4427 basic_block dest_bb
;
4429 gcc_checking_assert (bb
== NULL
|| e
== NULL
);
4432 gcc_checking_assert (gimple_code (gsi_stmt (si
)) != GIMPLE_COND
4433 && gimple_code (gsi_stmt (si
)) != GIMPLE_SWITCH
);
4435 /* Never build an assert comparing against an integer constant with
4436 TREE_OVERFLOW set. This confuses our undefined overflow warning
4438 if (TREE_CODE (val
) == INTEGER_CST
4439 && TREE_OVERFLOW (val
))
4440 val
= build_int_cst_wide (TREE_TYPE (val
),
4441 TREE_INT_CST_LOW (val
), TREE_INT_CST_HIGH (val
));
4443 /* The new assertion A will be inserted at BB or E. We need to
4444 determine if the new location is dominated by a previously
4445 registered location for A. If we are doing an edge insertion,
4446 assume that A will be inserted at E->DEST. Note that this is not
4449 If E is a critical edge, it will be split. But even if E is
4450 split, the new block will dominate the same set of blocks that
4453 The reverse, however, is not true, blocks dominated by E->DEST
4454 will not be dominated by the new block created to split E. So,
4455 if the insertion location is on a critical edge, we will not use
4456 the new location to move another assertion previously registered
4457 at a block dominated by E->DEST. */
4458 dest_bb
= (bb
) ? bb
: e
->dest
;
4460 /* If NAME already has an ASSERT_EXPR registered for COMP_CODE and
4461 VAL at a block dominating DEST_BB, then we don't need to insert a new
4462 one. Similarly, if the same assertion already exists at a block
4463 dominated by DEST_BB and the new location is not on a critical
4464 edge, then update the existing location for the assertion (i.e.,
4465 move the assertion up in the dominance tree).
4467 Note, this is implemented as a simple linked list because there
4468 should not be more than a handful of assertions registered per
4469 name. If this becomes a performance problem, a table hashed by
4470 COMP_CODE and VAL could be implemented. */
4471 loc
= asserts_for
[SSA_NAME_VERSION (name
)];
4475 if (loc
->comp_code
== comp_code
4477 || operand_equal_p (loc
->val
, val
, 0))
4478 && (loc
->expr
== expr
4479 || operand_equal_p (loc
->expr
, expr
, 0)))
4481 /* If E is not a critical edge and DEST_BB
4482 dominates the existing location for the assertion, move
4483 the assertion up in the dominance tree by updating its
4484 location information. */
4485 if ((e
== NULL
|| !EDGE_CRITICAL_P (e
))
4486 && dominated_by_p (CDI_DOMINATORS
, loc
->bb
, dest_bb
))
4495 /* Update the last node of the list and move to the next one. */
4500 /* If we didn't find an assertion already registered for
4501 NAME COMP_CODE VAL, add a new one at the end of the list of
4502 assertions associated with NAME. */
4503 n
= XNEW (struct assert_locus_d
);
4507 n
->comp_code
= comp_code
;
4515 asserts_for
[SSA_NAME_VERSION (name
)] = n
;
4517 bitmap_set_bit (need_assert_for
, SSA_NAME_VERSION (name
));
4520 /* (COND_OP0 COND_CODE COND_OP1) is a predicate which uses NAME.
4521 Extract a suitable test code and value and store them into *CODE_P and
4522 *VAL_P so the predicate is normalized to NAME *CODE_P *VAL_P.
4524 If no extraction was possible, return FALSE, otherwise return TRUE.
4526 If INVERT is true, then we invert the result stored into *CODE_P. */
4529 extract_code_and_val_from_cond_with_ops (tree name
, enum tree_code cond_code
,
4530 tree cond_op0
, tree cond_op1
,
4531 bool invert
, enum tree_code
*code_p
,
4534 enum tree_code comp_code
;
4537 /* Otherwise, we have a comparison of the form NAME COMP VAL
4538 or VAL COMP NAME. */
4539 if (name
== cond_op1
)
4541 /* If the predicate is of the form VAL COMP NAME, flip
4542 COMP around because we need to register NAME as the
4543 first operand in the predicate. */
4544 comp_code
= swap_tree_comparison (cond_code
);
4549 /* The comparison is of the form NAME COMP VAL, so the
4550 comparison code remains unchanged. */
4551 comp_code
= cond_code
;
4555 /* Invert the comparison code as necessary. */
4557 comp_code
= invert_tree_comparison (comp_code
, 0);
4559 /* VRP does not handle float types. */
4560 if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (val
)))
4563 /* Do not register always-false predicates.
4564 FIXME: this works around a limitation in fold() when dealing with
4565 enumerations. Given 'enum { N1, N2 } x;', fold will not
4566 fold 'if (x > N2)' to 'if (0)'. */
4567 if ((comp_code
== GT_EXPR
|| comp_code
== LT_EXPR
)
4568 && INTEGRAL_TYPE_P (TREE_TYPE (val
)))
4570 tree min
= TYPE_MIN_VALUE (TREE_TYPE (val
));
4571 tree max
= TYPE_MAX_VALUE (TREE_TYPE (val
));
4573 if (comp_code
== GT_EXPR
4575 || compare_values (val
, max
) == 0))
4578 if (comp_code
== LT_EXPR
4580 || compare_values (val
, min
) == 0))
4583 *code_p
= comp_code
;
4588 /* Find out smallest RES where RES > VAL && (RES & MASK) == RES, if any
4589 (otherwise return VAL). VAL and MASK must be zero-extended for
4590 precision PREC. If SGNBIT is non-zero, first xor VAL with SGNBIT
4591 (to transform signed values into unsigned) and at the end xor
4595 masked_increment (double_int val
, double_int mask
, double_int sgnbit
,
4598 double_int bit
= double_int_one
, res
;
4602 for (i
= 0; i
< prec
; i
++, bit
+= bit
)
4605 if ((res
& bit
).is_zero ())
4607 res
= bit
- double_int_one
;
4608 res
= (val
+ bit
).and_not (res
);
4611 return res
^ sgnbit
;
4613 return val
^ sgnbit
;
4616 /* Try to register an edge assertion for SSA name NAME on edge E for
4617 the condition COND contributing to the conditional jump pointed to by BSI.
4618 Invert the condition COND if INVERT is true.
4619 Return true if an assertion for NAME could be registered. */
4622 register_edge_assert_for_2 (tree name
, edge e
, gimple_stmt_iterator bsi
,
4623 enum tree_code cond_code
,
4624 tree cond_op0
, tree cond_op1
, bool invert
)
4627 enum tree_code comp_code
;
4628 bool retval
= false;
4630 if (!extract_code_and_val_from_cond_with_ops (name
, cond_code
,
4633 invert
, &comp_code
, &val
))
4636 /* Only register an ASSERT_EXPR if NAME was found in the sub-graph
4637 reachable from E. */
4638 if (live_on_edge (e
, name
)
4639 && !has_single_use (name
))
4641 register_new_assert_for (name
, name
, comp_code
, val
, NULL
, e
, bsi
);
4645 /* In the case of NAME <= CST and NAME being defined as
4646 NAME = (unsigned) NAME2 + CST2 we can assert NAME2 >= -CST2
4647 and NAME2 <= CST - CST2. We can do the same for NAME > CST.
4648 This catches range and anti-range tests. */
4649 if ((comp_code
== LE_EXPR
4650 || comp_code
== GT_EXPR
)
4651 && TREE_CODE (val
) == INTEGER_CST
4652 && TYPE_UNSIGNED (TREE_TYPE (val
)))
4654 gimple def_stmt
= SSA_NAME_DEF_STMT (name
);
4655 tree cst2
= NULL_TREE
, name2
= NULL_TREE
, name3
= NULL_TREE
;
4657 /* Extract CST2 from the (optional) addition. */
4658 if (is_gimple_assign (def_stmt
)
4659 && gimple_assign_rhs_code (def_stmt
) == PLUS_EXPR
)
4661 name2
= gimple_assign_rhs1 (def_stmt
);
4662 cst2
= gimple_assign_rhs2 (def_stmt
);
4663 if (TREE_CODE (name2
) == SSA_NAME
4664 && TREE_CODE (cst2
) == INTEGER_CST
)
4665 def_stmt
= SSA_NAME_DEF_STMT (name2
);
4668 /* Extract NAME2 from the (optional) sign-changing cast. */
4669 if (gimple_assign_cast_p (def_stmt
))
4671 if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt
))
4672 && ! TYPE_UNSIGNED (TREE_TYPE (gimple_assign_rhs1 (def_stmt
)))
4673 && (TYPE_PRECISION (gimple_expr_type (def_stmt
))
4674 == TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (def_stmt
)))))
4675 name3
= gimple_assign_rhs1 (def_stmt
);
4678 /* If name3 is used later, create an ASSERT_EXPR for it. */
4679 if (name3
!= NULL_TREE
4680 && TREE_CODE (name3
) == SSA_NAME
4681 && (cst2
== NULL_TREE
4682 || TREE_CODE (cst2
) == INTEGER_CST
)
4683 && INTEGRAL_TYPE_P (TREE_TYPE (name3
))
4684 && live_on_edge (e
, name3
)
4685 && !has_single_use (name3
))
4689 /* Build an expression for the range test. */
4690 tmp
= build1 (NOP_EXPR
, TREE_TYPE (name
), name3
);
4691 if (cst2
!= NULL_TREE
)
4692 tmp
= build2 (PLUS_EXPR
, TREE_TYPE (name
), tmp
, cst2
);
4696 fprintf (dump_file
, "Adding assert for ");
4697 print_generic_expr (dump_file
, name3
, 0);
4698 fprintf (dump_file
, " from ");
4699 print_generic_expr (dump_file
, tmp
, 0);
4700 fprintf (dump_file
, "\n");
4703 register_new_assert_for (name3
, tmp
, comp_code
, val
, NULL
, e
, bsi
);
4708 /* If name2 is used later, create an ASSERT_EXPR for it. */
4709 if (name2
!= NULL_TREE
4710 && TREE_CODE (name2
) == SSA_NAME
4711 && TREE_CODE (cst2
) == INTEGER_CST
4712 && INTEGRAL_TYPE_P (TREE_TYPE (name2
))
4713 && live_on_edge (e
, name2
)
4714 && !has_single_use (name2
))
4718 /* Build an expression for the range test. */
4720 if (TREE_TYPE (name
) != TREE_TYPE (name2
))
4721 tmp
= build1 (NOP_EXPR
, TREE_TYPE (name
), tmp
);
4722 if (cst2
!= NULL_TREE
)
4723 tmp
= build2 (PLUS_EXPR
, TREE_TYPE (name
), tmp
, cst2
);
4727 fprintf (dump_file
, "Adding assert for ");
4728 print_generic_expr (dump_file
, name2
, 0);
4729 fprintf (dump_file
, " from ");
4730 print_generic_expr (dump_file
, tmp
, 0);
4731 fprintf (dump_file
, "\n");
4734 register_new_assert_for (name2
, tmp
, comp_code
, val
, NULL
, e
, bsi
);
4740 /* In the case of post-in/decrement tests like if (i++) ... and uses
4741 of the in/decremented value on the edge the extra name we want to
4742 assert for is not on the def chain of the name compared. Instead
4743 it is in the set of use stmts. */
4744 if ((comp_code
== NE_EXPR
4745 || comp_code
== EQ_EXPR
)
4746 && TREE_CODE (val
) == INTEGER_CST
)
4748 imm_use_iterator ui
;
4750 FOR_EACH_IMM_USE_STMT (use_stmt
, ui
, name
)
4752 /* Cut off to use-stmts that are in the predecessor. */
4753 if (gimple_bb (use_stmt
) != e
->src
)
4756 if (!is_gimple_assign (use_stmt
))
4759 enum tree_code code
= gimple_assign_rhs_code (use_stmt
);
4760 if (code
!= PLUS_EXPR
4761 && code
!= MINUS_EXPR
)
4764 tree cst
= gimple_assign_rhs2 (use_stmt
);
4765 if (TREE_CODE (cst
) != INTEGER_CST
)
4768 tree name2
= gimple_assign_lhs (use_stmt
);
4769 if (live_on_edge (e
, name2
))
4771 cst
= int_const_binop (code
, val
, cst
);
4772 register_new_assert_for (name2
, name2
, comp_code
, cst
,
4779 if (TREE_CODE_CLASS (comp_code
) == tcc_comparison
4780 && TREE_CODE (val
) == INTEGER_CST
)
4782 gimple def_stmt
= SSA_NAME_DEF_STMT (name
);
4783 tree name2
= NULL_TREE
, names
[2], cst2
= NULL_TREE
;
4784 tree val2
= NULL_TREE
;
4785 double_int mask
= double_int_zero
;
4786 unsigned int prec
= TYPE_PRECISION (TREE_TYPE (val
));
4787 unsigned int nprec
= prec
;
4788 enum tree_code rhs_code
= ERROR_MARK
;
4790 if (is_gimple_assign (def_stmt
))
4791 rhs_code
= gimple_assign_rhs_code (def_stmt
);
4793 /* Add asserts for NAME cmp CST and NAME being defined
4794 as NAME = (int) NAME2. */
4795 if (!TYPE_UNSIGNED (TREE_TYPE (val
))
4796 && (comp_code
== LE_EXPR
|| comp_code
== LT_EXPR
4797 || comp_code
== GT_EXPR
|| comp_code
== GE_EXPR
)
4798 && gimple_assign_cast_p (def_stmt
))
4800 name2
= gimple_assign_rhs1 (def_stmt
);
4801 if (CONVERT_EXPR_CODE_P (rhs_code
)
4802 && INTEGRAL_TYPE_P (TREE_TYPE (name2
))
4803 && TYPE_UNSIGNED (TREE_TYPE (name2
))
4804 && prec
== TYPE_PRECISION (TREE_TYPE (name2
))
4805 && (comp_code
== LE_EXPR
|| comp_code
== GT_EXPR
4806 || !tree_int_cst_equal (val
,
4807 TYPE_MIN_VALUE (TREE_TYPE (val
))))
4808 && live_on_edge (e
, name2
)
4809 && !has_single_use (name2
))
4812 enum tree_code new_comp_code
= comp_code
;
4814 cst
= fold_convert (TREE_TYPE (name2
),
4815 TYPE_MIN_VALUE (TREE_TYPE (val
)));
4816 /* Build an expression for the range test. */
4817 tmp
= build2 (PLUS_EXPR
, TREE_TYPE (name2
), name2
, cst
);
4818 cst
= fold_build2 (PLUS_EXPR
, TREE_TYPE (name2
), cst
,
4819 fold_convert (TREE_TYPE (name2
), val
));
4820 if (comp_code
== LT_EXPR
|| comp_code
== GE_EXPR
)
4822 new_comp_code
= comp_code
== LT_EXPR
? LE_EXPR
: GT_EXPR
;
4823 cst
= fold_build2 (MINUS_EXPR
, TREE_TYPE (name2
), cst
,
4824 build_int_cst (TREE_TYPE (name2
), 1));
4829 fprintf (dump_file
, "Adding assert for ");
4830 print_generic_expr (dump_file
, name2
, 0);
4831 fprintf (dump_file
, " from ");
4832 print_generic_expr (dump_file
, tmp
, 0);
4833 fprintf (dump_file
, "\n");
4836 register_new_assert_for (name2
, tmp
, new_comp_code
, cst
, NULL
,
4843 /* Add asserts for NAME cmp CST and NAME being defined as
4844 NAME = NAME2 >> CST2.
4846 Extract CST2 from the right shift. */
4847 if (rhs_code
== RSHIFT_EXPR
)
4849 name2
= gimple_assign_rhs1 (def_stmt
);
4850 cst2
= gimple_assign_rhs2 (def_stmt
);
4851 if (TREE_CODE (name2
) == SSA_NAME
4852 && host_integerp (cst2
, 1)
4853 && INTEGRAL_TYPE_P (TREE_TYPE (name2
))
4854 && IN_RANGE (tree_low_cst (cst2
, 1), 1, prec
- 1)
4855 && prec
<= HOST_BITS_PER_DOUBLE_INT
4856 && prec
== GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (val
)))
4857 && live_on_edge (e
, name2
)
4858 && !has_single_use (name2
))
4860 mask
= double_int::mask (tree_low_cst (cst2
, 1));
4861 val2
= fold_binary (LSHIFT_EXPR
, TREE_TYPE (val
), val
, cst2
);
4864 if (val2
!= NULL_TREE
4865 && TREE_CODE (val2
) == INTEGER_CST
4866 && simple_cst_equal (fold_build2 (RSHIFT_EXPR
,
4870 enum tree_code new_comp_code
= comp_code
;
4874 if (comp_code
== EQ_EXPR
|| comp_code
== NE_EXPR
)
4876 if (!TYPE_UNSIGNED (TREE_TYPE (val
)))
4878 tree type
= build_nonstandard_integer_type (prec
, 1);
4879 tmp
= build1 (NOP_EXPR
, type
, name2
);
4880 val2
= fold_convert (type
, val2
);
4882 tmp
= fold_build2 (MINUS_EXPR
, TREE_TYPE (tmp
), tmp
, val2
);
4883 new_val
= double_int_to_tree (TREE_TYPE (tmp
), mask
);
4884 new_comp_code
= comp_code
== EQ_EXPR
? LE_EXPR
: GT_EXPR
;
4886 else if (comp_code
== LT_EXPR
|| comp_code
== GE_EXPR
)
4891 = double_int::max_value (prec
, TYPE_UNSIGNED (TREE_TYPE (val
)));
4892 mask
|= tree_to_double_int (val2
);
4894 new_val
= NULL_TREE
;
4896 new_val
= double_int_to_tree (TREE_TYPE (val2
), mask
);
4903 fprintf (dump_file
, "Adding assert for ");
4904 print_generic_expr (dump_file
, name2
, 0);
4905 fprintf (dump_file
, " from ");
4906 print_generic_expr (dump_file
, tmp
, 0);
4907 fprintf (dump_file
, "\n");
4910 register_new_assert_for (name2
, tmp
, new_comp_code
, new_val
,
4916 /* Add asserts for NAME cmp CST and NAME being defined as
4917 NAME = NAME2 & CST2.
4919 Extract CST2 from the and.
4922 NAME = (unsigned) NAME2;
4923 casts where NAME's type is unsigned and has smaller precision
4924 than NAME2's type as if it was NAME = NAME2 & MASK. */
4925 names
[0] = NULL_TREE
;
4926 names
[1] = NULL_TREE
;
4928 if (rhs_code
== BIT_AND_EXPR
4929 || (CONVERT_EXPR_CODE_P (rhs_code
)
4930 && TREE_CODE (TREE_TYPE (val
)) == INTEGER_TYPE
4931 && TYPE_UNSIGNED (TREE_TYPE (val
))
4932 && TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (def_stmt
)))
4936 name2
= gimple_assign_rhs1 (def_stmt
);
4937 if (rhs_code
== BIT_AND_EXPR
)
4938 cst2
= gimple_assign_rhs2 (def_stmt
);
4941 cst2
= TYPE_MAX_VALUE (TREE_TYPE (val
));
4942 nprec
= TYPE_PRECISION (TREE_TYPE (name2
));
4944 if (TREE_CODE (name2
) == SSA_NAME
4945 && INTEGRAL_TYPE_P (TREE_TYPE (name2
))
4946 && TREE_CODE (cst2
) == INTEGER_CST
4947 && !integer_zerop (cst2
)
4948 && nprec
<= HOST_BITS_PER_DOUBLE_INT
4950 || TYPE_UNSIGNED (TREE_TYPE (val
))))
4952 gimple def_stmt2
= SSA_NAME_DEF_STMT (name2
);
4953 if (gimple_assign_cast_p (def_stmt2
))
4955 names
[1] = gimple_assign_rhs1 (def_stmt2
);
4956 if (!CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt2
))
4957 || !INTEGRAL_TYPE_P (TREE_TYPE (names
[1]))
4958 || (TYPE_PRECISION (TREE_TYPE (name2
))
4959 != TYPE_PRECISION (TREE_TYPE (names
[1])))
4960 || !live_on_edge (e
, names
[1])
4961 || has_single_use (names
[1]))
4962 names
[1] = NULL_TREE
;
4964 if (live_on_edge (e
, name2
)
4965 && !has_single_use (name2
))
4969 if (names
[0] || names
[1])
4971 double_int minv
, maxv
= double_int_zero
, valv
, cst2v
;
4972 double_int tem
, sgnbit
;
4973 bool valid_p
= false, valn
= false, cst2n
= false;
4974 enum tree_code ccode
= comp_code
;
4976 valv
= tree_to_double_int (val
).zext (nprec
);
4977 cst2v
= tree_to_double_int (cst2
).zext (nprec
);
4978 if (!TYPE_UNSIGNED (TREE_TYPE (val
)))
4980 valn
= valv
.sext (nprec
).is_negative ();
4981 cst2n
= cst2v
.sext (nprec
).is_negative ();
4983 /* If CST2 doesn't have most significant bit set,
4984 but VAL is negative, we have comparison like
4985 if ((x & 0x123) > -4) (always true). Just give up. */
4989 sgnbit
= double_int_one
.llshift (nprec
- 1, nprec
).zext (nprec
);
4991 sgnbit
= double_int_zero
;
4992 minv
= valv
& cst2v
;
4996 /* Minimum unsigned value for equality is VAL & CST2
4997 (should be equal to VAL, otherwise we probably should
4998 have folded the comparison into false) and
4999 maximum unsigned value is VAL | ~CST2. */
5000 maxv
= valv
| ~cst2v
;
5001 maxv
= maxv
.zext (nprec
);
5005 tem
= valv
| ~cst2v
;
5006 tem
= tem
.zext (nprec
);
5007 /* If VAL is 0, handle (X & CST2) != 0 as (X & CST2) > 0U. */
5008 if (valv
.is_zero ())
5011 sgnbit
= double_int_zero
;
5014 /* If (VAL | ~CST2) is all ones, handle it as
5015 (X & CST2) < VAL. */
5016 if (tem
== double_int::mask (nprec
))
5020 sgnbit
= double_int_zero
;
5024 && cst2v
.sext (nprec
).is_negative ())
5026 = double_int_one
.llshift (nprec
- 1, nprec
).zext (nprec
);
5027 if (!sgnbit
.is_zero ())
5035 if (tem
== double_int::mask (nprec
- 1))
5041 sgnbit
= double_int_zero
;
5045 /* Minimum unsigned value for >= if (VAL & CST2) == VAL
5046 is VAL and maximum unsigned value is ~0. For signed
5047 comparison, if CST2 doesn't have most significant bit
5048 set, handle it similarly. If CST2 has MSB set,
5049 the minimum is the same, and maximum is ~0U/2. */
5052 /* If (VAL & CST2) != VAL, X & CST2 can't be equal to
5054 minv
= masked_increment (valv
, cst2v
, sgnbit
, nprec
);
5058 maxv
= double_int::mask (nprec
- (cst2n
? 1 : 0));
5063 /* Find out smallest MINV where MINV > VAL
5064 && (MINV & CST2) == MINV, if any. If VAL is signed and
5065 CST2 has MSB set, compute it biased by 1 << (nprec - 1). */
5066 minv
= masked_increment (valv
, cst2v
, sgnbit
, nprec
);
5069 maxv
= double_int::mask (nprec
- (cst2n
? 1 : 0));
5073 /* Minimum unsigned value for <= is 0 and maximum
5074 unsigned value is VAL | ~CST2 if (VAL & CST2) == VAL.
5075 Otherwise, find smallest VAL2 where VAL2 > VAL
5076 && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
5078 For signed comparison, if CST2 doesn't have most
5079 significant bit set, handle it similarly. If CST2 has
5080 MSB set, the maximum is the same and minimum is INT_MIN. */
5085 maxv
= masked_increment (valv
, cst2v
, sgnbit
, nprec
);
5088 maxv
-= double_int_one
;
5091 maxv
= maxv
.zext (nprec
);
5097 /* Minimum unsigned value for < is 0 and maximum
5098 unsigned value is (VAL-1) | ~CST2 if (VAL & CST2) == VAL.
5099 Otherwise, find smallest VAL2 where VAL2 > VAL
5100 && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
5102 For signed comparison, if CST2 doesn't have most
5103 significant bit set, handle it similarly. If CST2 has
5104 MSB set, the maximum is the same and minimum is INT_MIN. */
5113 maxv
= masked_increment (valv
, cst2v
, sgnbit
, nprec
);
5117 maxv
-= double_int_one
;
5119 maxv
= maxv
.zext (nprec
);
5127 && (maxv
- minv
).zext (nprec
) != double_int::mask (nprec
))
5129 tree tmp
, new_val
, type
;
5132 for (i
= 0; i
< 2; i
++)
5135 double_int maxv2
= maxv
;
5137 type
= TREE_TYPE (names
[i
]);
5138 if (!TYPE_UNSIGNED (type
))
5140 type
= build_nonstandard_integer_type (nprec
, 1);
5141 tmp
= build1 (NOP_EXPR
, type
, names
[i
]);
5143 if (!minv
.is_zero ())
5145 tmp
= build2 (PLUS_EXPR
, type
, tmp
,
5146 double_int_to_tree (type
, -minv
));
5147 maxv2
= maxv
- minv
;
5149 new_val
= double_int_to_tree (type
, maxv2
);
5153 fprintf (dump_file
, "Adding assert for ");
5154 print_generic_expr (dump_file
, names
[i
], 0);
5155 fprintf (dump_file
, " from ");
5156 print_generic_expr (dump_file
, tmp
, 0);
5157 fprintf (dump_file
, "\n");
5160 register_new_assert_for (names
[i
], tmp
, LE_EXPR
,
5161 new_val
, NULL
, e
, bsi
);
5171 /* OP is an operand of a truth value expression which is known to have
5172 a particular value. Register any asserts for OP and for any
5173 operands in OP's defining statement.
5175 If CODE is EQ_EXPR, then we want to register OP is zero (false),
5176 if CODE is NE_EXPR, then we want to register OP is nonzero (true). */
5179 register_edge_assert_for_1 (tree op
, enum tree_code code
,
5180 edge e
, gimple_stmt_iterator bsi
)
5182 bool retval
= false;
5185 enum tree_code rhs_code
;
5187 /* We only care about SSA_NAMEs. */
5188 if (TREE_CODE (op
) != SSA_NAME
)
5191 /* We know that OP will have a zero or nonzero value. If OP is used
5192 more than once go ahead and register an assert for OP.
5194 The FOUND_IN_SUBGRAPH support is not helpful in this situation as
5195 it will always be set for OP (because OP is used in a COND_EXPR in
5197 if (!has_single_use (op
))
5199 val
= build_int_cst (TREE_TYPE (op
), 0);
5200 register_new_assert_for (op
, op
, code
, val
, NULL
, e
, bsi
);
5204 /* Now look at how OP is set. If it's set from a comparison,
5205 a truth operation or some bit operations, then we may be able
5206 to register information about the operands of that assignment. */
5207 op_def
= SSA_NAME_DEF_STMT (op
);
5208 if (gimple_code (op_def
) != GIMPLE_ASSIGN
)
5211 rhs_code
= gimple_assign_rhs_code (op_def
);
5213 if (TREE_CODE_CLASS (rhs_code
) == tcc_comparison
)
5215 bool invert
= (code
== EQ_EXPR
? true : false);
5216 tree op0
= gimple_assign_rhs1 (op_def
);
5217 tree op1
= gimple_assign_rhs2 (op_def
);
5219 if (TREE_CODE (op0
) == SSA_NAME
)
5220 retval
|= register_edge_assert_for_2 (op0
, e
, bsi
, rhs_code
, op0
, op1
,
5222 if (TREE_CODE (op1
) == SSA_NAME
)
5223 retval
|= register_edge_assert_for_2 (op1
, e
, bsi
, rhs_code
, op0
, op1
,
5226 else if ((code
== NE_EXPR
5227 && gimple_assign_rhs_code (op_def
) == BIT_AND_EXPR
)
5229 && gimple_assign_rhs_code (op_def
) == BIT_IOR_EXPR
))
5231 /* Recurse on each operand. */
5232 retval
|= register_edge_assert_for_1 (gimple_assign_rhs1 (op_def
),
5234 retval
|= register_edge_assert_for_1 (gimple_assign_rhs2 (op_def
),
5237 else if (gimple_assign_rhs_code (op_def
) == BIT_NOT_EXPR
5238 && TYPE_PRECISION (TREE_TYPE (gimple_assign_lhs (op_def
))) == 1)
5240 /* Recurse, flipping CODE. */
5241 code
= invert_tree_comparison (code
, false);
5242 retval
|= register_edge_assert_for_1 (gimple_assign_rhs1 (op_def
),
5245 else if (gimple_assign_rhs_code (op_def
) == SSA_NAME
)
5247 /* Recurse through the copy. */
5248 retval
|= register_edge_assert_for_1 (gimple_assign_rhs1 (op_def
),
5251 else if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (op_def
)))
5253 /* Recurse through the type conversion. */
5254 retval
|= register_edge_assert_for_1 (gimple_assign_rhs1 (op_def
),
5261 /* Try to register an edge assertion for SSA name NAME on edge E for
5262 the condition COND contributing to the conditional jump pointed to by SI.
5263 Return true if an assertion for NAME could be registered. */
5266 register_edge_assert_for (tree name
, edge e
, gimple_stmt_iterator si
,
5267 enum tree_code cond_code
, tree cond_op0
,
5271 enum tree_code comp_code
;
5272 bool retval
= false;
5273 bool is_else_edge
= (e
->flags
& EDGE_FALSE_VALUE
) != 0;
5275 /* Do not attempt to infer anything in names that flow through
5277 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name
))
5280 if (!extract_code_and_val_from_cond_with_ops (name
, cond_code
,
5286 /* Register ASSERT_EXPRs for name. */
5287 retval
|= register_edge_assert_for_2 (name
, e
, si
, cond_code
, cond_op0
,
5288 cond_op1
, is_else_edge
);
5291 /* If COND is effectively an equality test of an SSA_NAME against
5292 the value zero or one, then we may be able to assert values
5293 for SSA_NAMEs which flow into COND. */
5295 /* In the case of NAME == 1 or NAME != 0, for BIT_AND_EXPR defining
5296 statement of NAME we can assert both operands of the BIT_AND_EXPR
5297 have nonzero value. */
5298 if (((comp_code
== EQ_EXPR
&& integer_onep (val
))
5299 || (comp_code
== NE_EXPR
&& integer_zerop (val
))))
5301 gimple def_stmt
= SSA_NAME_DEF_STMT (name
);
5303 if (is_gimple_assign (def_stmt
)
5304 && gimple_assign_rhs_code (def_stmt
) == BIT_AND_EXPR
)
5306 tree op0
= gimple_assign_rhs1 (def_stmt
);
5307 tree op1
= gimple_assign_rhs2 (def_stmt
);
5308 retval
|= register_edge_assert_for_1 (op0
, NE_EXPR
, e
, si
);
5309 retval
|= register_edge_assert_for_1 (op1
, NE_EXPR
, e
, si
);
5313 /* In the case of NAME == 0 or NAME != 1, for BIT_IOR_EXPR defining
5314 statement of NAME we can assert both operands of the BIT_IOR_EXPR
5316 if (((comp_code
== EQ_EXPR
&& integer_zerop (val
))
5317 || (comp_code
== NE_EXPR
&& integer_onep (val
))))
5319 gimple def_stmt
= SSA_NAME_DEF_STMT (name
);
5321 /* For BIT_IOR_EXPR only if NAME == 0 both operands have
5322 necessarily zero value, or if type-precision is one. */
5323 if (is_gimple_assign (def_stmt
)
5324 && (gimple_assign_rhs_code (def_stmt
) == BIT_IOR_EXPR
5325 && (TYPE_PRECISION (TREE_TYPE (name
)) == 1
5326 || comp_code
== EQ_EXPR
)))
5328 tree op0
= gimple_assign_rhs1 (def_stmt
);
5329 tree op1
= gimple_assign_rhs2 (def_stmt
);
5330 retval
|= register_edge_assert_for_1 (op0
, EQ_EXPR
, e
, si
);
5331 retval
|= register_edge_assert_for_1 (op1
, EQ_EXPR
, e
, si
);
5339 /* Determine whether the outgoing edges of BB should receive an
5340 ASSERT_EXPR for each of the operands of BB's LAST statement.
5341 The last statement of BB must be a COND_EXPR.
5343 If any of the sub-graphs rooted at BB have an interesting use of
5344 the predicate operands, an assert location node is added to the
5345 list of assertions for the corresponding operands. */
5348 find_conditional_asserts (basic_block bb
, gimple last
)
5351 gimple_stmt_iterator bsi
;
5357 need_assert
= false;
5358 bsi
= gsi_for_stmt (last
);
5360 /* Look for uses of the operands in each of the sub-graphs
5361 rooted at BB. We need to check each of the outgoing edges
5362 separately, so that we know what kind of ASSERT_EXPR to
5364 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
5369 /* Register the necessary assertions for each operand in the
5370 conditional predicate. */
5371 FOR_EACH_SSA_TREE_OPERAND (op
, last
, iter
, SSA_OP_USE
)
5373 need_assert
|= register_edge_assert_for (op
, e
, bsi
,
5374 gimple_cond_code (last
),
5375 gimple_cond_lhs (last
),
5376 gimple_cond_rhs (last
));
5389 /* Compare two case labels sorting first by the destination bb index
5390 and then by the case value. */
5393 compare_case_labels (const void *p1
, const void *p2
)
5395 const struct case_info
*ci1
= (const struct case_info
*) p1
;
5396 const struct case_info
*ci2
= (const struct case_info
*) p2
;
5397 int idx1
= ci1
->bb
->index
;
5398 int idx2
= ci2
->bb
->index
;
5402 else if (idx1
== idx2
)
5404 /* Make sure the default label is first in a group. */
5405 if (!CASE_LOW (ci1
->expr
))
5407 else if (!CASE_LOW (ci2
->expr
))
5410 return tree_int_cst_compare (CASE_LOW (ci1
->expr
),
5411 CASE_LOW (ci2
->expr
));
5417 /* Determine whether the outgoing edges of BB should receive an
5418 ASSERT_EXPR for each of the operands of BB's LAST statement.
5419 The last statement of BB must be a SWITCH_EXPR.
5421 If any of the sub-graphs rooted at BB have an interesting use of
5422 the predicate operands, an assert location node is added to the
5423 list of assertions for the corresponding operands. */
5426 find_switch_asserts (basic_block bb
, gimple last
)
5429 gimple_stmt_iterator bsi
;
5432 struct case_info
*ci
;
5433 size_t n
= gimple_switch_num_labels (last
);
5434 #if GCC_VERSION >= 4000
5437 /* Work around GCC 3.4 bug (PR 37086). */
5438 volatile unsigned int idx
;
5441 need_assert
= false;
5442 bsi
= gsi_for_stmt (last
);
5443 op
= gimple_switch_index (last
);
5444 if (TREE_CODE (op
) != SSA_NAME
)
5447 /* Build a vector of case labels sorted by destination label. */
5448 ci
= XNEWVEC (struct case_info
, n
);
5449 for (idx
= 0; idx
< n
; ++idx
)
5451 ci
[idx
].expr
= gimple_switch_label (last
, idx
);
5452 ci
[idx
].bb
= label_to_block (CASE_LABEL (ci
[idx
].expr
));
5454 qsort (ci
, n
, sizeof (struct case_info
), compare_case_labels
);
5456 for (idx
= 0; idx
< n
; ++idx
)
5459 tree cl
= ci
[idx
].expr
;
5460 basic_block cbb
= ci
[idx
].bb
;
5462 min
= CASE_LOW (cl
);
5463 max
= CASE_HIGH (cl
);
5465 /* If there are multiple case labels with the same destination
5466 we need to combine them to a single value range for the edge. */
5467 if (idx
+ 1 < n
&& cbb
== ci
[idx
+ 1].bb
)
5469 /* Skip labels until the last of the group. */
5472 } while (idx
< n
&& cbb
== ci
[idx
].bb
);
5475 /* Pick up the maximum of the case label range. */
5476 if (CASE_HIGH (ci
[idx
].expr
))
5477 max
= CASE_HIGH (ci
[idx
].expr
);
5479 max
= CASE_LOW (ci
[idx
].expr
);
5482 /* Nothing to do if the range includes the default label until we
5483 can register anti-ranges. */
5484 if (min
== NULL_TREE
)
5487 /* Find the edge to register the assert expr on. */
5488 e
= find_edge (bb
, cbb
);
5490 /* Register the necessary assertions for the operand in the
5492 need_assert
|= register_edge_assert_for (op
, e
, bsi
,
5493 max
? GE_EXPR
: EQ_EXPR
,
5495 fold_convert (TREE_TYPE (op
),
5499 need_assert
|= register_edge_assert_for (op
, e
, bsi
, LE_EXPR
,
5501 fold_convert (TREE_TYPE (op
),
5511 /* Traverse all the statements in block BB looking for statements that
5512 may generate useful assertions for the SSA names in their operand.
5513 If a statement produces a useful assertion A for name N_i, then the
5514 list of assertions already generated for N_i is scanned to
5515 determine if A is actually needed.
5517 If N_i already had the assertion A at a location dominating the
5518 current location, then nothing needs to be done. Otherwise, the
5519 new location for A is recorded instead.
5521 1- For every statement S in BB, all the variables used by S are
5522 added to bitmap FOUND_IN_SUBGRAPH.
5524 2- If statement S uses an operand N in a way that exposes a known
5525 value range for N, then if N was not already generated by an
5526 ASSERT_EXPR, create a new assert location for N. For instance,
5527 if N is a pointer and the statement dereferences it, we can
5528 assume that N is not NULL.
5530 3- COND_EXPRs are a special case of #2. We can derive range
5531 information from the predicate but need to insert different
5532 ASSERT_EXPRs for each of the sub-graphs rooted at the
5533 conditional block. If the last statement of BB is a conditional
5534 expression of the form 'X op Y', then
5536 a) Remove X and Y from the set FOUND_IN_SUBGRAPH.
5538 b) If the conditional is the only entry point to the sub-graph
5539 corresponding to the THEN_CLAUSE, recurse into it. On
5540 return, if X and/or Y are marked in FOUND_IN_SUBGRAPH, then
5541 an ASSERT_EXPR is added for the corresponding variable.
5543 c) Repeat step (b) on the ELSE_CLAUSE.
5545 d) Mark X and Y in FOUND_IN_SUBGRAPH.
5554 In this case, an assertion on the THEN clause is useful to
5555 determine that 'a' is always 9 on that edge. However, an assertion
5556 on the ELSE clause would be unnecessary.
5558 4- If BB does not end in a conditional expression, then we recurse
5559 into BB's dominator children.
5561 At the end of the recursive traversal, every SSA name will have a
5562 list of locations where ASSERT_EXPRs should be added. When a new
5563 location for name N is found, it is registered by calling
5564 register_new_assert_for. That function keeps track of all the
5565 registered assertions to prevent adding unnecessary assertions.
5566 For instance, if a pointer P_4 is dereferenced more than once in a
5567 dominator tree, only the location dominating all the dereference of
5568 P_4 will receive an ASSERT_EXPR.
5570 If this function returns true, then it means that there are names
5571 for which we need to generate ASSERT_EXPRs. Those assertions are
5572 inserted by process_assert_insertions. */
5575 find_assert_locations_1 (basic_block bb
, sbitmap live
)
5577 gimple_stmt_iterator si
;
5581 need_assert
= false;
5582 last
= last_stmt (bb
);
5584 /* If BB's last statement is a conditional statement involving integer
5585 operands, determine if we need to add ASSERT_EXPRs. */
5587 && gimple_code (last
) == GIMPLE_COND
5588 && !fp_predicate (last
)
5589 && !ZERO_SSA_OPERANDS (last
, SSA_OP_USE
))
5590 need_assert
|= find_conditional_asserts (bb
, last
);
5592 /* If BB's last statement is a switch statement involving integer
5593 operands, determine if we need to add ASSERT_EXPRs. */
5595 && gimple_code (last
) == GIMPLE_SWITCH
5596 && !ZERO_SSA_OPERANDS (last
, SSA_OP_USE
))
5597 need_assert
|= find_switch_asserts (bb
, last
);
5599 /* Traverse all the statements in BB marking used names and looking
5600 for statements that may infer assertions for their used operands. */
5601 for (si
= gsi_last_bb (bb
); !gsi_end_p (si
); gsi_prev (&si
))
5607 stmt
= gsi_stmt (si
);
5609 if (is_gimple_debug (stmt
))
5612 /* See if we can derive an assertion for any of STMT's operands. */
5613 FOR_EACH_SSA_TREE_OPERAND (op
, stmt
, i
, SSA_OP_USE
)
5616 enum tree_code comp_code
;
5618 /* If op is not live beyond this stmt, do not bother to insert
5620 if (!bitmap_bit_p (live
, SSA_NAME_VERSION (op
)))
5623 /* If OP is used in such a way that we can infer a value
5624 range for it, and we don't find a previous assertion for
5625 it, create a new assertion location node for OP. */
5626 if (infer_value_range (stmt
, op
, &comp_code
, &value
))
5628 /* If we are able to infer a nonzero value range for OP,
5629 then walk backwards through the use-def chain to see if OP
5630 was set via a typecast.
5632 If so, then we can also infer a nonzero value range
5633 for the operand of the NOP_EXPR. */
5634 if (comp_code
== NE_EXPR
&& integer_zerop (value
))
5637 gimple def_stmt
= SSA_NAME_DEF_STMT (t
);
5639 while (is_gimple_assign (def_stmt
)
5640 && gimple_assign_rhs_code (def_stmt
) == NOP_EXPR
5642 (gimple_assign_rhs1 (def_stmt
)) == SSA_NAME
5644 (TREE_TYPE (gimple_assign_rhs1 (def_stmt
))))
5646 t
= gimple_assign_rhs1 (def_stmt
);
5647 def_stmt
= SSA_NAME_DEF_STMT (t
);
5649 /* Note we want to register the assert for the
5650 operand of the NOP_EXPR after SI, not after the
5652 if (! has_single_use (t
))
5654 register_new_assert_for (t
, t
, comp_code
, value
,
5661 register_new_assert_for (op
, op
, comp_code
, value
, bb
, NULL
, si
);
5667 FOR_EACH_SSA_TREE_OPERAND (op
, stmt
, i
, SSA_OP_USE
)
5668 bitmap_set_bit (live
, SSA_NAME_VERSION (op
));
5669 FOR_EACH_SSA_TREE_OPERAND (op
, stmt
, i
, SSA_OP_DEF
)
5670 bitmap_clear_bit (live
, SSA_NAME_VERSION (op
));
5673 /* Traverse all PHI nodes in BB, updating live. */
5674 for (si
= gsi_start_phis (bb
); !gsi_end_p(si
); gsi_next (&si
))
5676 use_operand_p arg_p
;
5678 gimple phi
= gsi_stmt (si
);
5679 tree res
= gimple_phi_result (phi
);
5681 if (virtual_operand_p (res
))
5684 FOR_EACH_PHI_ARG (arg_p
, phi
, i
, SSA_OP_USE
)
5686 tree arg
= USE_FROM_PTR (arg_p
);
5687 if (TREE_CODE (arg
) == SSA_NAME
)
5688 bitmap_set_bit (live
, SSA_NAME_VERSION (arg
));
5691 bitmap_clear_bit (live
, SSA_NAME_VERSION (res
));
5697 /* Do an RPO walk over the function computing SSA name liveness
5698 on-the-fly and deciding on assert expressions to insert.
5699 Returns true if there are assert expressions to be inserted. */
5702 find_assert_locations (void)
5704 int *rpo
= XNEWVEC (int, last_basic_block
);
5705 int *bb_rpo
= XNEWVEC (int, last_basic_block
);
5706 int *last_rpo
= XCNEWVEC (int, last_basic_block
);
5710 live
= XCNEWVEC (sbitmap
, last_basic_block
);
5711 rpo_cnt
= pre_and_rev_post_order_compute (NULL
, rpo
, false);
5712 for (i
= 0; i
< rpo_cnt
; ++i
)
5715 need_asserts
= false;
5716 for (i
= rpo_cnt
- 1; i
>= 0; --i
)
5718 basic_block bb
= BASIC_BLOCK (rpo
[i
]);
5724 live
[rpo
[i
]] = sbitmap_alloc (num_ssa_names
);
5725 bitmap_clear (live
[rpo
[i
]]);
5728 /* Process BB and update the live information with uses in
5730 need_asserts
|= find_assert_locations_1 (bb
, live
[rpo
[i
]]);
5732 /* Merge liveness into the predecessor blocks and free it. */
5733 if (!bitmap_empty_p (live
[rpo
[i
]]))
5736 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
5738 int pred
= e
->src
->index
;
5739 if ((e
->flags
& EDGE_DFS_BACK
) || pred
== ENTRY_BLOCK
)
5744 live
[pred
] = sbitmap_alloc (num_ssa_names
);
5745 bitmap_clear (live
[pred
]);
5747 bitmap_ior (live
[pred
], live
[pred
], live
[rpo
[i
]]);
5749 if (bb_rpo
[pred
] < pred_rpo
)
5750 pred_rpo
= bb_rpo
[pred
];
5753 /* Record the RPO number of the last visited block that needs
5754 live information from this block. */
5755 last_rpo
[rpo
[i
]] = pred_rpo
;
5759 sbitmap_free (live
[rpo
[i
]]);
5760 live
[rpo
[i
]] = NULL
;
5763 /* We can free all successors live bitmaps if all their
5764 predecessors have been visited already. */
5765 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
5766 if (last_rpo
[e
->dest
->index
] == i
5767 && live
[e
->dest
->index
])
5769 sbitmap_free (live
[e
->dest
->index
]);
5770 live
[e
->dest
->index
] = NULL
;
5775 XDELETEVEC (bb_rpo
);
5776 XDELETEVEC (last_rpo
);
5777 for (i
= 0; i
< last_basic_block
; ++i
)
5779 sbitmap_free (live
[i
]);
5782 return need_asserts
;
5785 /* Create an ASSERT_EXPR for NAME and insert it in the location
5786 indicated by LOC. Return true if we made any edge insertions. */
5789 process_assert_insertions_for (tree name
, assert_locus_t loc
)
5791 /* Build the comparison expression NAME_i COMP_CODE VAL. */
5798 /* If we have X <=> X do not insert an assert expr for that. */
5799 if (loc
->expr
== loc
->val
)
5802 cond
= build2 (loc
->comp_code
, boolean_type_node
, loc
->expr
, loc
->val
);
5803 assert_stmt
= build_assert_expr_for (cond
, name
);
5806 /* We have been asked to insert the assertion on an edge. This
5807 is used only by COND_EXPR and SWITCH_EXPR assertions. */
5808 gcc_checking_assert (gimple_code (gsi_stmt (loc
->si
)) == GIMPLE_COND
5809 || (gimple_code (gsi_stmt (loc
->si
))
5812 gsi_insert_on_edge (loc
->e
, assert_stmt
);
5816 /* Otherwise, we can insert right after LOC->SI iff the
5817 statement must not be the last statement in the block. */
5818 stmt
= gsi_stmt (loc
->si
);
5819 if (!stmt_ends_bb_p (stmt
))
5821 gsi_insert_after (&loc
->si
, assert_stmt
, GSI_SAME_STMT
);
5825 /* If STMT must be the last statement in BB, we can only insert new
5826 assertions on the non-abnormal edge out of BB. Note that since
5827 STMT is not control flow, there may only be one non-abnormal edge
5829 FOR_EACH_EDGE (e
, ei
, loc
->bb
->succs
)
5830 if (!(e
->flags
& EDGE_ABNORMAL
))
5832 gsi_insert_on_edge (e
, assert_stmt
);
5840 /* Process all the insertions registered for every name N_i registered
5841 in NEED_ASSERT_FOR. The list of assertions to be inserted are
5842 found in ASSERTS_FOR[i]. */
5845 process_assert_insertions (void)
5849 bool update_edges_p
= false;
5850 int num_asserts
= 0;
5852 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
5853 dump_all_asserts (dump_file
);
5855 EXECUTE_IF_SET_IN_BITMAP (need_assert_for
, 0, i
, bi
)
5857 assert_locus_t loc
= asserts_for
[i
];
5862 assert_locus_t next
= loc
->next
;
5863 update_edges_p
|= process_assert_insertions_for (ssa_name (i
), loc
);
5871 gsi_commit_edge_inserts ();
5873 statistics_counter_event (cfun
, "Number of ASSERT_EXPR expressions inserted",
5878 /* Traverse the flowgraph looking for conditional jumps to insert range
5879 expressions. These range expressions are meant to provide information
5880 to optimizations that need to reason in terms of value ranges. They
5881 will not be expanded into RTL. For instance, given:
5890 this pass will transform the code into:
5896 x = ASSERT_EXPR <x, x < y>
5901 y = ASSERT_EXPR <y, x <= y>
5905 The idea is that once copy and constant propagation have run, other
5906 optimizations will be able to determine what ranges of values can 'x'
5907 take in different paths of the code, simply by checking the reaching
5908 definition of 'x'. */
5911 insert_range_assertions (void)
5913 need_assert_for
= BITMAP_ALLOC (NULL
);
5914 asserts_for
= XCNEWVEC (assert_locus_t
, num_ssa_names
);
5916 calculate_dominance_info (CDI_DOMINATORS
);
5918 if (find_assert_locations ())
5920 process_assert_insertions ();
5921 update_ssa (TODO_update_ssa_no_phi
);
5924 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
5926 fprintf (dump_file
, "\nSSA form after inserting ASSERT_EXPRs\n");
5927 dump_function_to_file (current_function_decl
, dump_file
, dump_flags
);
5931 BITMAP_FREE (need_assert_for
);
5934 /* Checks one ARRAY_REF in REF, located at LOCUS. Ignores flexible arrays
5935 and "struct" hacks. If VRP can determine that the
5936 array subscript is a constant, check if it is outside valid
5937 range. If the array subscript is a RANGE, warn if it is
5938 non-overlapping with valid range.
5939 IGNORE_OFF_BY_ONE is true if the ARRAY_REF is inside a ADDR_EXPR. */
5942 check_array_ref (location_t location
, tree ref
, bool ignore_off_by_one
)
5944 value_range_t
* vr
= NULL
;
5945 tree low_sub
, up_sub
;
5946 tree low_bound
, up_bound
, up_bound_p1
;
5949 if (TREE_NO_WARNING (ref
))
5952 low_sub
= up_sub
= TREE_OPERAND (ref
, 1);
5953 up_bound
= array_ref_up_bound (ref
);
5955 /* Can not check flexible arrays. */
5957 || TREE_CODE (up_bound
) != INTEGER_CST
)
5960 /* Accesses to trailing arrays via pointers may access storage
5961 beyond the types array bounds. */
5962 base
= get_base_address (ref
);
5963 if (base
&& TREE_CODE (base
) == MEM_REF
)
5965 tree cref
, next
= NULL_TREE
;
5967 if (TREE_CODE (TREE_OPERAND (ref
, 0)) != COMPONENT_REF
)
5970 cref
= TREE_OPERAND (ref
, 0);
5971 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (cref
, 0))) == RECORD_TYPE
)
5972 for (next
= DECL_CHAIN (TREE_OPERAND (cref
, 1));
5973 next
&& TREE_CODE (next
) != FIELD_DECL
;
5974 next
= DECL_CHAIN (next
))
5977 /* If this is the last field in a struct type or a field in a
5978 union type do not warn. */
5983 low_bound
= array_ref_low_bound (ref
);
5984 up_bound_p1
= int_const_binop (PLUS_EXPR
, up_bound
, integer_one_node
);
5986 if (TREE_CODE (low_sub
) == SSA_NAME
)
5988 vr
= get_value_range (low_sub
);
5989 if (vr
->type
== VR_RANGE
|| vr
->type
== VR_ANTI_RANGE
)
5991 low_sub
= vr
->type
== VR_RANGE
? vr
->max
: vr
->min
;
5992 up_sub
= vr
->type
== VR_RANGE
? vr
->min
: vr
->max
;
5996 if (vr
&& vr
->type
== VR_ANTI_RANGE
)
5998 if (TREE_CODE (up_sub
) == INTEGER_CST
5999 && tree_int_cst_lt (up_bound
, up_sub
)
6000 && TREE_CODE (low_sub
) == INTEGER_CST
6001 && tree_int_cst_lt (low_sub
, low_bound
))
6003 warning_at (location
, OPT_Warray_bounds
,
6004 "array subscript is outside array bounds");
6005 TREE_NO_WARNING (ref
) = 1;
6008 else if (TREE_CODE (up_sub
) == INTEGER_CST
6009 && (ignore_off_by_one
6010 ? (tree_int_cst_lt (up_bound
, up_sub
)
6011 && !tree_int_cst_equal (up_bound_p1
, up_sub
))
6012 : (tree_int_cst_lt (up_bound
, up_sub
)
6013 || tree_int_cst_equal (up_bound_p1
, up_sub
))))
6015 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6017 fprintf (dump_file
, "Array bound warning for ");
6018 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, ref
);
6020 warning_at (location
, OPT_Warray_bounds
,
6021 "array subscript is above array bounds");
6022 TREE_NO_WARNING (ref
) = 1;
6024 else if (TREE_CODE (low_sub
) == INTEGER_CST
6025 && tree_int_cst_lt (low_sub
, low_bound
))
6027 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6029 fprintf (dump_file
, "Array bound warning for ");
6030 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, ref
);
6032 warning_at (location
, OPT_Warray_bounds
,
6033 "array subscript is below array bounds");
6034 TREE_NO_WARNING (ref
) = 1;
6038 /* Searches if the expr T, located at LOCATION computes
6039 address of an ARRAY_REF, and call check_array_ref on it. */
6042 search_for_addr_array (tree t
, location_t location
)
6044 while (TREE_CODE (t
) == SSA_NAME
)
6046 gimple g
= SSA_NAME_DEF_STMT (t
);
6048 if (gimple_code (g
) != GIMPLE_ASSIGN
)
6051 if (get_gimple_rhs_class (gimple_assign_rhs_code (g
))
6052 != GIMPLE_SINGLE_RHS
)
6055 t
= gimple_assign_rhs1 (g
);
6059 /* We are only interested in addresses of ARRAY_REF's. */
6060 if (TREE_CODE (t
) != ADDR_EXPR
)
6063 /* Check each ARRAY_REFs in the reference chain. */
6066 if (TREE_CODE (t
) == ARRAY_REF
)
6067 check_array_ref (location
, t
, true /*ignore_off_by_one*/);
6069 t
= TREE_OPERAND (t
, 0);
6071 while (handled_component_p (t
));
6073 if (TREE_CODE (t
) == MEM_REF
6074 && TREE_CODE (TREE_OPERAND (t
, 0)) == ADDR_EXPR
6075 && !TREE_NO_WARNING (t
))
6077 tree tem
= TREE_OPERAND (TREE_OPERAND (t
, 0), 0);
6078 tree low_bound
, up_bound
, el_sz
;
6080 if (TREE_CODE (TREE_TYPE (tem
)) != ARRAY_TYPE
6081 || TREE_CODE (TREE_TYPE (TREE_TYPE (tem
))) == ARRAY_TYPE
6082 || !TYPE_DOMAIN (TREE_TYPE (tem
)))
6085 low_bound
= TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (tem
)));
6086 up_bound
= TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (tem
)));
6087 el_sz
= TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (tem
)));
6089 || TREE_CODE (low_bound
) != INTEGER_CST
6091 || TREE_CODE (up_bound
) != INTEGER_CST
6093 || TREE_CODE (el_sz
) != INTEGER_CST
)
6096 idx
= mem_ref_offset (t
);
6097 idx
= idx
.sdiv (tree_to_double_int (el_sz
), TRUNC_DIV_EXPR
);
6098 if (idx
.slt (double_int_zero
))
6100 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6102 fprintf (dump_file
, "Array bound warning for ");
6103 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, t
);
6105 warning_at (location
, OPT_Warray_bounds
,
6106 "array subscript is below array bounds");
6107 TREE_NO_WARNING (t
) = 1;
6109 else if (idx
.sgt (tree_to_double_int (up_bound
)
6110 - tree_to_double_int (low_bound
)
6113 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6115 fprintf (dump_file
, "Array bound warning for ");
6116 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, t
);
6118 warning_at (location
, OPT_Warray_bounds
,
6119 "array subscript is above array bounds");
6120 TREE_NO_WARNING (t
) = 1;
6125 /* walk_tree() callback that checks if *TP is
6126 an ARRAY_REF inside an ADDR_EXPR (in which an array
6127 subscript one outside the valid range is allowed). Call
6128 check_array_ref for each ARRAY_REF found. The location is
6132 check_array_bounds (tree
*tp
, int *walk_subtree
, void *data
)
6135 struct walk_stmt_info
*wi
= (struct walk_stmt_info
*) data
;
6136 location_t location
;
6138 if (EXPR_HAS_LOCATION (t
))
6139 location
= EXPR_LOCATION (t
);
6142 location_t
*locp
= (location_t
*) wi
->info
;
6146 *walk_subtree
= TRUE
;
6148 if (TREE_CODE (t
) == ARRAY_REF
)
6149 check_array_ref (location
, t
, false /*ignore_off_by_one*/);
6151 if (TREE_CODE (t
) == MEM_REF
6152 || (TREE_CODE (t
) == RETURN_EXPR
&& TREE_OPERAND (t
, 0)))
6153 search_for_addr_array (TREE_OPERAND (t
, 0), location
);
6155 if (TREE_CODE (t
) == ADDR_EXPR
)
6156 *walk_subtree
= FALSE
;
6161 /* Walk over all statements of all reachable BBs and call check_array_bounds
6165 check_all_array_refs (void)
6168 gimple_stmt_iterator si
;
6174 bool executable
= false;
6176 /* Skip blocks that were found to be unreachable. */
6177 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
6178 executable
|= !!(e
->flags
& EDGE_EXECUTABLE
);
6182 for (si
= gsi_start_bb (bb
); !gsi_end_p (si
); gsi_next (&si
))
6184 gimple stmt
= gsi_stmt (si
);
6185 struct walk_stmt_info wi
;
6186 if (!gimple_has_location (stmt
))
6189 if (is_gimple_call (stmt
))
6192 size_t n
= gimple_call_num_args (stmt
);
6193 for (i
= 0; i
< n
; i
++)
6195 tree arg
= gimple_call_arg (stmt
, i
);
6196 search_for_addr_array (arg
, gimple_location (stmt
));
6201 memset (&wi
, 0, sizeof (wi
));
6202 wi
.info
= CONST_CAST (void *, (const void *)
6203 gimple_location_ptr (stmt
));
6205 walk_gimple_op (gsi_stmt (si
),
6213 /* Convert range assertion expressions into the implied copies and
6214 copy propagate away the copies. Doing the trivial copy propagation
6215 here avoids the need to run the full copy propagation pass after
6218 FIXME, this will eventually lead to copy propagation removing the
6219 names that had useful range information attached to them. For
6220 instance, if we had the assertion N_i = ASSERT_EXPR <N_j, N_j > 3>,
6221 then N_i will have the range [3, +INF].
6223 However, by converting the assertion into the implied copy
6224 operation N_i = N_j, we will then copy-propagate N_j into the uses
6225 of N_i and lose the range information. We may want to hold on to
6226 ASSERT_EXPRs a little while longer as the ranges could be used in
6227 things like jump threading.
6229 The problem with keeping ASSERT_EXPRs around is that passes after
6230 VRP need to handle them appropriately.
6232 Another approach would be to make the range information a first
6233 class property of the SSA_NAME so that it can be queried from
6234 any pass. This is made somewhat more complex by the need for
6235 multiple ranges to be associated with one SSA_NAME. */
6238 remove_range_assertions (void)
6241 gimple_stmt_iterator si
;
6243 /* Note that the BSI iterator bump happens at the bottom of the
6244 loop and no bump is necessary if we're removing the statement
6245 referenced by the current BSI. */
6247 for (si
= gsi_start_bb (bb
); !gsi_end_p (si
);)
6249 gimple stmt
= gsi_stmt (si
);
6252 if (is_gimple_assign (stmt
)
6253 && gimple_assign_rhs_code (stmt
) == ASSERT_EXPR
)
6255 tree rhs
= gimple_assign_rhs1 (stmt
);
6257 tree cond
= fold (ASSERT_EXPR_COND (rhs
));
6258 use_operand_p use_p
;
6259 imm_use_iterator iter
;
6261 gcc_assert (cond
!= boolean_false_node
);
6263 /* Propagate the RHS into every use of the LHS. */
6264 var
= ASSERT_EXPR_VAR (rhs
);
6265 FOR_EACH_IMM_USE_STMT (use_stmt
, iter
,
6266 gimple_assign_lhs (stmt
))
6267 FOR_EACH_IMM_USE_ON_STMT (use_p
, iter
)
6269 SET_USE (use_p
, var
);
6270 gcc_assert (TREE_CODE (var
) == SSA_NAME
);
6273 /* And finally, remove the copy, it is not needed. */
6274 gsi_remove (&si
, true);
6275 release_defs (stmt
);
6283 /* Return true if STMT is interesting for VRP. */
6286 stmt_interesting_for_vrp (gimple stmt
)
6288 if (gimple_code (stmt
) == GIMPLE_PHI
)
6290 tree res
= gimple_phi_result (stmt
);
6291 return (!virtual_operand_p (res
)
6292 && (INTEGRAL_TYPE_P (TREE_TYPE (res
))
6293 || POINTER_TYPE_P (TREE_TYPE (res
))));
6295 else if (is_gimple_assign (stmt
) || is_gimple_call (stmt
))
6297 tree lhs
= gimple_get_lhs (stmt
);
6299 /* In general, assignments with virtual operands are not useful
6300 for deriving ranges, with the obvious exception of calls to
6301 builtin functions. */
6302 if (lhs
&& TREE_CODE (lhs
) == SSA_NAME
6303 && (INTEGRAL_TYPE_P (TREE_TYPE (lhs
))
6304 || POINTER_TYPE_P (TREE_TYPE (lhs
)))
6305 && ((is_gimple_call (stmt
)
6306 && gimple_call_fndecl (stmt
) != NULL_TREE
6307 && DECL_BUILT_IN (gimple_call_fndecl (stmt
)))
6308 || !gimple_vuse (stmt
)))
6311 else if (gimple_code (stmt
) == GIMPLE_COND
6312 || gimple_code (stmt
) == GIMPLE_SWITCH
)
6319 /* Initialize local data structures for VRP. */
6322 vrp_initialize (void)
6326 values_propagated
= false;
6327 num_vr_values
= num_ssa_names
;
6328 vr_value
= XCNEWVEC (value_range_t
*, num_vr_values
);
6329 vr_phi_edge_counts
= XCNEWVEC (int, num_ssa_names
);
6333 gimple_stmt_iterator si
;
6335 for (si
= gsi_start_phis (bb
); !gsi_end_p (si
); gsi_next (&si
))
6337 gimple phi
= gsi_stmt (si
);
6338 if (!stmt_interesting_for_vrp (phi
))
6340 tree lhs
= PHI_RESULT (phi
);
6341 set_value_range_to_varying (get_value_range (lhs
));
6342 prop_set_simulate_again (phi
, false);
6345 prop_set_simulate_again (phi
, true);
6348 for (si
= gsi_start_bb (bb
); !gsi_end_p (si
); gsi_next (&si
))
6350 gimple stmt
= gsi_stmt (si
);
6352 /* If the statement is a control insn, then we do not
6353 want to avoid simulating the statement once. Failure
6354 to do so means that those edges will never get added. */
6355 if (stmt_ends_bb_p (stmt
))
6356 prop_set_simulate_again (stmt
, true);
6357 else if (!stmt_interesting_for_vrp (stmt
))
6361 FOR_EACH_SSA_TREE_OPERAND (def
, stmt
, i
, SSA_OP_DEF
)
6362 set_value_range_to_varying (get_value_range (def
));
6363 prop_set_simulate_again (stmt
, false);
6366 prop_set_simulate_again (stmt
, true);
6371 /* Return the singleton value-range for NAME or NAME. */
6374 vrp_valueize (tree name
)
6376 if (TREE_CODE (name
) == SSA_NAME
)
6378 value_range_t
*vr
= get_value_range (name
);
6379 if (vr
->type
== VR_RANGE
6380 && (vr
->min
== vr
->max
6381 || operand_equal_p (vr
->min
, vr
->max
, 0)))
6387 /* Visit assignment STMT. If it produces an interesting range, record
6388 the SSA name in *OUTPUT_P. */
6390 static enum ssa_prop_result
6391 vrp_visit_assignment_or_call (gimple stmt
, tree
*output_p
)
6395 enum gimple_code code
= gimple_code (stmt
);
6396 lhs
= gimple_get_lhs (stmt
);
6398 /* We only keep track of ranges in integral and pointer types. */
6399 if (TREE_CODE (lhs
) == SSA_NAME
6400 && ((INTEGRAL_TYPE_P (TREE_TYPE (lhs
))
6401 /* It is valid to have NULL MIN/MAX values on a type. See
6402 build_range_type. */
6403 && TYPE_MIN_VALUE (TREE_TYPE (lhs
))
6404 && TYPE_MAX_VALUE (TREE_TYPE (lhs
)))
6405 || POINTER_TYPE_P (TREE_TYPE (lhs
))))
6407 value_range_t new_vr
= VR_INITIALIZER
;
6409 /* Try folding the statement to a constant first. */
6410 tree tem
= gimple_fold_stmt_to_constant (stmt
, vrp_valueize
);
6411 if (tem
&& !is_overflow_infinity (tem
))
6412 set_value_range (&new_vr
, VR_RANGE
, tem
, tem
, NULL
);
6413 /* Then dispatch to value-range extracting functions. */
6414 else if (code
== GIMPLE_CALL
)
6415 extract_range_basic (&new_vr
, stmt
);
6417 extract_range_from_assignment (&new_vr
, stmt
);
6419 if (update_value_range (lhs
, &new_vr
))
6423 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6425 fprintf (dump_file
, "Found new range for ");
6426 print_generic_expr (dump_file
, lhs
, 0);
6427 fprintf (dump_file
, ": ");
6428 dump_value_range (dump_file
, &new_vr
);
6429 fprintf (dump_file
, "\n\n");
6432 if (new_vr
.type
== VR_VARYING
)
6433 return SSA_PROP_VARYING
;
6435 return SSA_PROP_INTERESTING
;
6438 return SSA_PROP_NOT_INTERESTING
;
6441 /* Every other statement produces no useful ranges. */
6442 FOR_EACH_SSA_TREE_OPERAND (def
, stmt
, iter
, SSA_OP_DEF
)
6443 set_value_range_to_varying (get_value_range (def
));
6445 return SSA_PROP_VARYING
;
6448 /* Helper that gets the value range of the SSA_NAME with version I
6449 or a symbolic range containing the SSA_NAME only if the value range
6450 is varying or undefined. */
6452 static inline value_range_t
6453 get_vr_for_comparison (int i
)
6455 value_range_t vr
= *get_value_range (ssa_name (i
));
6457 /* If name N_i does not have a valid range, use N_i as its own
6458 range. This allows us to compare against names that may
6459 have N_i in their ranges. */
6460 if (vr
.type
== VR_VARYING
|| vr
.type
== VR_UNDEFINED
)
6463 vr
.min
= ssa_name (i
);
6464 vr
.max
= ssa_name (i
);
6470 /* Compare all the value ranges for names equivalent to VAR with VAL
6471 using comparison code COMP. Return the same value returned by
6472 compare_range_with_value, including the setting of
6473 *STRICT_OVERFLOW_P. */
6476 compare_name_with_value (enum tree_code comp
, tree var
, tree val
,
6477 bool *strict_overflow_p
)
6483 int used_strict_overflow
;
6485 value_range_t equiv_vr
;
6487 /* Get the set of equivalences for VAR. */
6488 e
= get_value_range (var
)->equiv
;
6490 /* Start at -1. Set it to 0 if we do a comparison without relying
6491 on overflow, or 1 if all comparisons rely on overflow. */
6492 used_strict_overflow
= -1;
6494 /* Compare vars' value range with val. */
6495 equiv_vr
= get_vr_for_comparison (SSA_NAME_VERSION (var
));
6497 retval
= compare_range_with_value (comp
, &equiv_vr
, val
, &sop
);
6499 used_strict_overflow
= sop
? 1 : 0;
6501 /* If the equiv set is empty we have done all work we need to do. */
6505 && used_strict_overflow
> 0)
6506 *strict_overflow_p
= true;
6510 EXECUTE_IF_SET_IN_BITMAP (e
, 0, i
, bi
)
6512 equiv_vr
= get_vr_for_comparison (i
);
6514 t
= compare_range_with_value (comp
, &equiv_vr
, val
, &sop
);
6517 /* If we get different answers from different members
6518 of the equivalence set this check must be in a dead
6519 code region. Folding it to a trap representation
6520 would be correct here. For now just return don't-know. */
6530 used_strict_overflow
= 0;
6531 else if (used_strict_overflow
< 0)
6532 used_strict_overflow
= 1;
6537 && used_strict_overflow
> 0)
6538 *strict_overflow_p
= true;
6544 /* Given a comparison code COMP and names N1 and N2, compare all the
6545 ranges equivalent to N1 against all the ranges equivalent to N2
6546 to determine the value of N1 COMP N2. Return the same value
6547 returned by compare_ranges. Set *STRICT_OVERFLOW_P to indicate
6548 whether we relied on an overflow infinity in the comparison. */
6552 compare_names (enum tree_code comp
, tree n1
, tree n2
,
6553 bool *strict_overflow_p
)
6557 bitmap_iterator bi1
, bi2
;
6559 int used_strict_overflow
;
6560 static bitmap_obstack
*s_obstack
= NULL
;
6561 static bitmap s_e1
= NULL
, s_e2
= NULL
;
6563 /* Compare the ranges of every name equivalent to N1 against the
6564 ranges of every name equivalent to N2. */
6565 e1
= get_value_range (n1
)->equiv
;
6566 e2
= get_value_range (n2
)->equiv
;
6568 /* Use the fake bitmaps if e1 or e2 are not available. */
6569 if (s_obstack
== NULL
)
6571 s_obstack
= XNEW (bitmap_obstack
);
6572 bitmap_obstack_initialize (s_obstack
);
6573 s_e1
= BITMAP_ALLOC (s_obstack
);
6574 s_e2
= BITMAP_ALLOC (s_obstack
);
6581 /* Add N1 and N2 to their own set of equivalences to avoid
6582 duplicating the body of the loop just to check N1 and N2
6584 bitmap_set_bit (e1
, SSA_NAME_VERSION (n1
));
6585 bitmap_set_bit (e2
, SSA_NAME_VERSION (n2
));
6587 /* If the equivalence sets have a common intersection, then the two
6588 names can be compared without checking their ranges. */
6589 if (bitmap_intersect_p (e1
, e2
))
6591 bitmap_clear_bit (e1
, SSA_NAME_VERSION (n1
));
6592 bitmap_clear_bit (e2
, SSA_NAME_VERSION (n2
));
6594 return (comp
== EQ_EXPR
|| comp
== GE_EXPR
|| comp
== LE_EXPR
)
6596 : boolean_false_node
;
6599 /* Start at -1. Set it to 0 if we do a comparison without relying
6600 on overflow, or 1 if all comparisons rely on overflow. */
6601 used_strict_overflow
= -1;
6603 /* Otherwise, compare all the equivalent ranges. First, add N1 and
6604 N2 to their own set of equivalences to avoid duplicating the body
6605 of the loop just to check N1 and N2 ranges. */
6606 EXECUTE_IF_SET_IN_BITMAP (e1
, 0, i1
, bi1
)
6608 value_range_t vr1
= get_vr_for_comparison (i1
);
6610 t
= retval
= NULL_TREE
;
6611 EXECUTE_IF_SET_IN_BITMAP (e2
, 0, i2
, bi2
)
6615 value_range_t vr2
= get_vr_for_comparison (i2
);
6617 t
= compare_ranges (comp
, &vr1
, &vr2
, &sop
);
6620 /* If we get different answers from different members
6621 of the equivalence set this check must be in a dead
6622 code region. Folding it to a trap representation
6623 would be correct here. For now just return don't-know. */
6627 bitmap_clear_bit (e1
, SSA_NAME_VERSION (n1
));
6628 bitmap_clear_bit (e2
, SSA_NAME_VERSION (n2
));
6634 used_strict_overflow
= 0;
6635 else if (used_strict_overflow
< 0)
6636 used_strict_overflow
= 1;
6642 bitmap_clear_bit (e1
, SSA_NAME_VERSION (n1
));
6643 bitmap_clear_bit (e2
, SSA_NAME_VERSION (n2
));
6644 if (used_strict_overflow
> 0)
6645 *strict_overflow_p
= true;
6650 /* None of the equivalent ranges are useful in computing this
6652 bitmap_clear_bit (e1
, SSA_NAME_VERSION (n1
));
6653 bitmap_clear_bit (e2
, SSA_NAME_VERSION (n2
));
6657 /* Helper function for vrp_evaluate_conditional_warnv. */
6660 vrp_evaluate_conditional_warnv_with_ops_using_ranges (enum tree_code code
,
6662 bool * strict_overflow_p
)
6664 value_range_t
*vr0
, *vr1
;
6666 vr0
= (TREE_CODE (op0
) == SSA_NAME
) ? get_value_range (op0
) : NULL
;
6667 vr1
= (TREE_CODE (op1
) == SSA_NAME
) ? get_value_range (op1
) : NULL
;
6670 return compare_ranges (code
, vr0
, vr1
, strict_overflow_p
);
6671 else if (vr0
&& vr1
== NULL
)
6672 return compare_range_with_value (code
, vr0
, op1
, strict_overflow_p
);
6673 else if (vr0
== NULL
&& vr1
)
6674 return (compare_range_with_value
6675 (swap_tree_comparison (code
), vr1
, op0
, strict_overflow_p
));
6679 /* Helper function for vrp_evaluate_conditional_warnv. */
6682 vrp_evaluate_conditional_warnv_with_ops (enum tree_code code
, tree op0
,
6683 tree op1
, bool use_equiv_p
,
6684 bool *strict_overflow_p
, bool *only_ranges
)
6688 *only_ranges
= true;
6690 /* We only deal with integral and pointer types. */
6691 if (!INTEGRAL_TYPE_P (TREE_TYPE (op0
))
6692 && !POINTER_TYPE_P (TREE_TYPE (op0
)))
6698 && (ret
= vrp_evaluate_conditional_warnv_with_ops_using_ranges
6699 (code
, op0
, op1
, strict_overflow_p
)))
6701 *only_ranges
= false;
6702 if (TREE_CODE (op0
) == SSA_NAME
&& TREE_CODE (op1
) == SSA_NAME
)
6703 return compare_names (code
, op0
, op1
, strict_overflow_p
);
6704 else if (TREE_CODE (op0
) == SSA_NAME
)
6705 return compare_name_with_value (code
, op0
, op1
, strict_overflow_p
);
6706 else if (TREE_CODE (op1
) == SSA_NAME
)
6707 return (compare_name_with_value
6708 (swap_tree_comparison (code
), op1
, op0
, strict_overflow_p
));
6711 return vrp_evaluate_conditional_warnv_with_ops_using_ranges (code
, op0
, op1
,
6716 /* Given (CODE OP0 OP1) within STMT, try to simplify it based on value range
6717 information. Return NULL if the conditional can not be evaluated.
6718 The ranges of all the names equivalent with the operands in COND
6719 will be used when trying to compute the value. If the result is
6720 based on undefined signed overflow, issue a warning if
6724 vrp_evaluate_conditional (enum tree_code code
, tree op0
, tree op1
, gimple stmt
)
6730 /* Some passes and foldings leak constants with overflow flag set
6731 into the IL. Avoid doing wrong things with these and bail out. */
6732 if ((TREE_CODE (op0
) == INTEGER_CST
6733 && TREE_OVERFLOW (op0
))
6734 || (TREE_CODE (op1
) == INTEGER_CST
6735 && TREE_OVERFLOW (op1
)))
6739 ret
= vrp_evaluate_conditional_warnv_with_ops (code
, op0
, op1
, true, &sop
,
6744 enum warn_strict_overflow_code wc
;
6745 const char* warnmsg
;
6747 if (is_gimple_min_invariant (ret
))
6749 wc
= WARN_STRICT_OVERFLOW_CONDITIONAL
;
6750 warnmsg
= G_("assuming signed overflow does not occur when "
6751 "simplifying conditional to constant");
6755 wc
= WARN_STRICT_OVERFLOW_COMPARISON
;
6756 warnmsg
= G_("assuming signed overflow does not occur when "
6757 "simplifying conditional");
6760 if (issue_strict_overflow_warning (wc
))
6762 location_t location
;
6764 if (!gimple_has_location (stmt
))
6765 location
= input_location
;
6767 location
= gimple_location (stmt
);
6768 warning_at (location
, OPT_Wstrict_overflow
, "%s", warnmsg
);
6772 if (warn_type_limits
6773 && ret
&& only_ranges
6774 && TREE_CODE_CLASS (code
) == tcc_comparison
6775 && TREE_CODE (op0
) == SSA_NAME
)
6777 /* If the comparison is being folded and the operand on the LHS
6778 is being compared against a constant value that is outside of
6779 the natural range of OP0's type, then the predicate will
6780 always fold regardless of the value of OP0. If -Wtype-limits
6781 was specified, emit a warning. */
6782 tree type
= TREE_TYPE (op0
);
6783 value_range_t
*vr0
= get_value_range (op0
);
6785 if (vr0
->type
!= VR_VARYING
6786 && INTEGRAL_TYPE_P (type
)
6787 && vrp_val_is_min (vr0
->min
)
6788 && vrp_val_is_max (vr0
->max
)
6789 && is_gimple_min_invariant (op1
))
6791 location_t location
;
6793 if (!gimple_has_location (stmt
))
6794 location
= input_location
;
6796 location
= gimple_location (stmt
);
6798 warning_at (location
, OPT_Wtype_limits
,
6800 ? G_("comparison always false "
6801 "due to limited range of data type")
6802 : G_("comparison always true "
6803 "due to limited range of data type"));
6811 /* Visit conditional statement STMT. If we can determine which edge
6812 will be taken out of STMT's basic block, record it in
6813 *TAKEN_EDGE_P and return SSA_PROP_INTERESTING. Otherwise, return
6814 SSA_PROP_VARYING. */
6816 static enum ssa_prop_result
6817 vrp_visit_cond_stmt (gimple stmt
, edge
*taken_edge_p
)
6822 *taken_edge_p
= NULL
;
6824 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6829 fprintf (dump_file
, "\nVisiting conditional with predicate: ");
6830 print_gimple_stmt (dump_file
, stmt
, 0, 0);
6831 fprintf (dump_file
, "\nWith known ranges\n");
6833 FOR_EACH_SSA_TREE_OPERAND (use
, stmt
, i
, SSA_OP_USE
)
6835 fprintf (dump_file
, "\t");
6836 print_generic_expr (dump_file
, use
, 0);
6837 fprintf (dump_file
, ": ");
6838 dump_value_range (dump_file
, vr_value
[SSA_NAME_VERSION (use
)]);
6841 fprintf (dump_file
, "\n");
6844 /* Compute the value of the predicate COND by checking the known
6845 ranges of each of its operands.
6847 Note that we cannot evaluate all the equivalent ranges here
6848 because those ranges may not yet be final and with the current
6849 propagation strategy, we cannot determine when the value ranges
6850 of the names in the equivalence set have changed.
6852 For instance, given the following code fragment
6856 i_14 = ASSERT_EXPR <i_5, i_5 != 0>
6860 Assume that on the first visit to i_14, i_5 has the temporary
6861 range [8, 8] because the second argument to the PHI function is
6862 not yet executable. We derive the range ~[0, 0] for i_14 and the
6863 equivalence set { i_5 }. So, when we visit 'if (i_14 == 1)' for
6864 the first time, since i_14 is equivalent to the range [8, 8], we
6865 determine that the predicate is always false.
6867 On the next round of propagation, i_13 is determined to be
6868 VARYING, which causes i_5 to drop down to VARYING. So, another
6869 visit to i_14 is scheduled. In this second visit, we compute the
6870 exact same range and equivalence set for i_14, namely ~[0, 0] and
6871 { i_5 }. But we did not have the previous range for i_5
6872 registered, so vrp_visit_assignment thinks that the range for
6873 i_14 has not changed. Therefore, the predicate 'if (i_14 == 1)'
6874 is not visited again, which stops propagation from visiting
6875 statements in the THEN clause of that if().
6877 To properly fix this we would need to keep the previous range
6878 value for the names in the equivalence set. This way we would've
6879 discovered that from one visit to the other i_5 changed from
6880 range [8, 8] to VR_VARYING.
6882 However, fixing this apparent limitation may not be worth the
6883 additional checking. Testing on several code bases (GCC, DLV,
6884 MICO, TRAMP3D and SPEC2000) showed that doing this results in
6885 4 more predicates folded in SPEC. */
6888 val
= vrp_evaluate_conditional_warnv_with_ops (gimple_cond_code (stmt
),
6889 gimple_cond_lhs (stmt
),
6890 gimple_cond_rhs (stmt
),
6895 *taken_edge_p
= find_taken_edge (gimple_bb (stmt
), val
);
6898 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6900 "\nIgnoring predicate evaluation because "
6901 "it assumes that signed overflow is undefined");
6906 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6908 fprintf (dump_file
, "\nPredicate evaluates to: ");
6909 if (val
== NULL_TREE
)
6910 fprintf (dump_file
, "DON'T KNOW\n");
6912 print_generic_stmt (dump_file
, val
, 0);
6915 return (*taken_edge_p
) ? SSA_PROP_INTERESTING
: SSA_PROP_VARYING
;
6918 /* Searches the case label vector VEC for the index *IDX of the CASE_LABEL
6919 that includes the value VAL. The search is restricted to the range
6920 [START_IDX, n - 1] where n is the size of VEC.
6922 If there is a CASE_LABEL for VAL, its index is placed in IDX and true is
6925 If there is no CASE_LABEL for VAL and there is one that is larger than VAL,
6926 it is placed in IDX and false is returned.
6928 If VAL is larger than any CASE_LABEL, n is placed on IDX and false is
6932 find_case_label_index (gimple stmt
, size_t start_idx
, tree val
, size_t *idx
)
6934 size_t n
= gimple_switch_num_labels (stmt
);
6937 /* Find case label for minimum of the value range or the next one.
6938 At each iteration we are searching in [low, high - 1]. */
6940 for (low
= start_idx
, high
= n
; high
!= low
; )
6944 /* Note that i != high, so we never ask for n. */
6945 size_t i
= (high
+ low
) / 2;
6946 t
= gimple_switch_label (stmt
, i
);
6948 /* Cache the result of comparing CASE_LOW and val. */
6949 cmp
= tree_int_cst_compare (CASE_LOW (t
), val
);
6953 /* Ranges cannot be empty. */
6962 if (CASE_HIGH (t
) != NULL
6963 && tree_int_cst_compare (CASE_HIGH (t
), val
) >= 0)
6975 /* Searches the case label vector VEC for the range of CASE_LABELs that is used
6976 for values between MIN and MAX. The first index is placed in MIN_IDX. The
6977 last index is placed in MAX_IDX. If the range of CASE_LABELs is empty
6978 then MAX_IDX < MIN_IDX.
6979 Returns true if the default label is not needed. */
6982 find_case_label_range (gimple stmt
, tree min
, tree max
, size_t *min_idx
,
6986 bool min_take_default
= !find_case_label_index (stmt
, 1, min
, &i
);
6987 bool max_take_default
= !find_case_label_index (stmt
, i
, max
, &j
);
6991 && max_take_default
)
6993 /* Only the default case label reached.
6994 Return an empty range. */
7001 bool take_default
= min_take_default
|| max_take_default
;
7005 if (max_take_default
)
7008 /* If the case label range is continuous, we do not need
7009 the default case label. Verify that. */
7010 high
= CASE_LOW (gimple_switch_label (stmt
, i
));
7011 if (CASE_HIGH (gimple_switch_label (stmt
, i
)))
7012 high
= CASE_HIGH (gimple_switch_label (stmt
, i
));
7013 for (k
= i
+ 1; k
<= j
; ++k
)
7015 low
= CASE_LOW (gimple_switch_label (stmt
, k
));
7016 if (!integer_onep (int_const_binop (MINUS_EXPR
, low
, high
)))
7018 take_default
= true;
7022 if (CASE_HIGH (gimple_switch_label (stmt
, k
)))
7023 high
= CASE_HIGH (gimple_switch_label (stmt
, k
));
7028 return !take_default
;
7032 /* Searches the case label vector VEC for the ranges of CASE_LABELs that are
7033 used in range VR. The indices are placed in MIN_IDX1, MAX_IDX, MIN_IDX2 and
7034 MAX_IDX2. If the ranges of CASE_LABELs are empty then MAX_IDX1 < MIN_IDX1.
7035 Returns true if the default label is not needed. */
7038 find_case_label_ranges (gimple stmt
, value_range_t
*vr
, size_t *min_idx1
,
7039 size_t *max_idx1
, size_t *min_idx2
,
7043 unsigned int n
= gimple_switch_num_labels (stmt
);
7045 tree case_low
, case_high
;
7046 tree min
= vr
->min
, max
= vr
->max
;
7048 gcc_checking_assert (vr
->type
== VR_RANGE
|| vr
->type
== VR_ANTI_RANGE
);
7050 take_default
= !find_case_label_range (stmt
, min
, max
, &i
, &j
);
7052 /* Set second range to emtpy. */
7056 if (vr
->type
== VR_RANGE
)
7060 return !take_default
;
7063 /* Set first range to all case labels. */
7070 /* Make sure all the values of case labels [i , j] are contained in
7071 range [MIN, MAX]. */
7072 case_low
= CASE_LOW (gimple_switch_label (stmt
, i
));
7073 case_high
= CASE_HIGH (gimple_switch_label (stmt
, j
));
7074 if (tree_int_cst_compare (case_low
, min
) < 0)
7076 if (case_high
!= NULL_TREE
7077 && tree_int_cst_compare (max
, case_high
) < 0)
7083 /* If the range spans case labels [i, j], the corresponding anti-range spans
7084 the labels [1, i - 1] and [j + 1, n - 1]. */
7110 /* Visit switch statement STMT. If we can determine which edge
7111 will be taken out of STMT's basic block, record it in
7112 *TAKEN_EDGE_P and return SSA_PROP_INTERESTING. Otherwise, return
7113 SSA_PROP_VARYING. */
7115 static enum ssa_prop_result
7116 vrp_visit_switch_stmt (gimple stmt
, edge
*taken_edge_p
)
7120 size_t i
= 0, j
= 0, k
, l
;
7123 *taken_edge_p
= NULL
;
7124 op
= gimple_switch_index (stmt
);
7125 if (TREE_CODE (op
) != SSA_NAME
)
7126 return SSA_PROP_VARYING
;
7128 vr
= get_value_range (op
);
7129 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7131 fprintf (dump_file
, "\nVisiting switch expression with operand ");
7132 print_generic_expr (dump_file
, op
, 0);
7133 fprintf (dump_file
, " with known range ");
7134 dump_value_range (dump_file
, vr
);
7135 fprintf (dump_file
, "\n");
7138 if ((vr
->type
!= VR_RANGE
7139 && vr
->type
!= VR_ANTI_RANGE
)
7140 || symbolic_range_p (vr
))
7141 return SSA_PROP_VARYING
;
7143 /* Find the single edge that is taken from the switch expression. */
7144 take_default
= !find_case_label_ranges (stmt
, vr
, &i
, &j
, &k
, &l
);
7146 /* Check if the range spans no CASE_LABEL. If so, we only reach the default
7150 gcc_assert (take_default
);
7151 val
= gimple_switch_default_label (stmt
);
7155 /* Check if labels with index i to j and maybe the default label
7156 are all reaching the same label. */
7158 val
= gimple_switch_label (stmt
, i
);
7160 && CASE_LABEL (gimple_switch_default_label (stmt
))
7161 != CASE_LABEL (val
))
7163 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7164 fprintf (dump_file
, " not a single destination for this "
7166 return SSA_PROP_VARYING
;
7168 for (++i
; i
<= j
; ++i
)
7170 if (CASE_LABEL (gimple_switch_label (stmt
, i
)) != CASE_LABEL (val
))
7172 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7173 fprintf (dump_file
, " not a single destination for this "
7175 return SSA_PROP_VARYING
;
7180 if (CASE_LABEL (gimple_switch_label (stmt
, k
)) != CASE_LABEL (val
))
7182 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7183 fprintf (dump_file
, " not a single destination for this "
7185 return SSA_PROP_VARYING
;
7190 *taken_edge_p
= find_edge (gimple_bb (stmt
),
7191 label_to_block (CASE_LABEL (val
)));
7193 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7195 fprintf (dump_file
, " will take edge to ");
7196 print_generic_stmt (dump_file
, CASE_LABEL (val
), 0);
7199 return SSA_PROP_INTERESTING
;
7203 /* Evaluate statement STMT. If the statement produces a useful range,
7204 return SSA_PROP_INTERESTING and record the SSA name with the
7205 interesting range into *OUTPUT_P.
7207 If STMT is a conditional branch and we can determine its truth
7208 value, the taken edge is recorded in *TAKEN_EDGE_P.
7210 If STMT produces a varying value, return SSA_PROP_VARYING. */
7212 static enum ssa_prop_result
7213 vrp_visit_stmt (gimple stmt
, edge
*taken_edge_p
, tree
*output_p
)
7218 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7220 fprintf (dump_file
, "\nVisiting statement:\n");
7221 print_gimple_stmt (dump_file
, stmt
, 0, dump_flags
);
7222 fprintf (dump_file
, "\n");
7225 if (!stmt_interesting_for_vrp (stmt
))
7226 gcc_assert (stmt_ends_bb_p (stmt
));
7227 else if (is_gimple_assign (stmt
) || is_gimple_call (stmt
))
7229 /* In general, assignments with virtual operands are not useful
7230 for deriving ranges, with the obvious exception of calls to
7231 builtin functions. */
7232 if ((is_gimple_call (stmt
)
7233 && gimple_call_fndecl (stmt
) != NULL_TREE
7234 && DECL_BUILT_IN (gimple_call_fndecl (stmt
)))
7235 || !gimple_vuse (stmt
))
7236 return vrp_visit_assignment_or_call (stmt
, output_p
);
7238 else if (gimple_code (stmt
) == GIMPLE_COND
)
7239 return vrp_visit_cond_stmt (stmt
, taken_edge_p
);
7240 else if (gimple_code (stmt
) == GIMPLE_SWITCH
)
7241 return vrp_visit_switch_stmt (stmt
, taken_edge_p
);
7243 /* All other statements produce nothing of interest for VRP, so mark
7244 their outputs varying and prevent further simulation. */
7245 FOR_EACH_SSA_TREE_OPERAND (def
, stmt
, iter
, SSA_OP_DEF
)
7246 set_value_range_to_varying (get_value_range (def
));
7248 return SSA_PROP_VARYING
;
7251 /* Union the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
7252 { VR1TYPE, VR0MIN, VR0MAX } and store the result
7253 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
7254 possible such range. The resulting range is not canonicalized. */
7257 union_ranges (enum value_range_type
*vr0type
,
7258 tree
*vr0min
, tree
*vr0max
,
7259 enum value_range_type vr1type
,
7260 tree vr1min
, tree vr1max
)
7262 bool mineq
= operand_equal_p (*vr0min
, vr1min
, 0);
7263 bool maxeq
= operand_equal_p (*vr0max
, vr1max
, 0);
7265 /* [] is vr0, () is vr1 in the following classification comments. */
7269 if (*vr0type
== vr1type
)
7270 /* Nothing to do for equal ranges. */
7272 else if ((*vr0type
== VR_RANGE
7273 && vr1type
== VR_ANTI_RANGE
)
7274 || (*vr0type
== VR_ANTI_RANGE
7275 && vr1type
== VR_RANGE
))
7277 /* For anti-range with range union the result is varying. */
7283 else if (operand_less_p (*vr0max
, vr1min
) == 1
7284 || operand_less_p (vr1max
, *vr0min
) == 1)
7286 /* [ ] ( ) or ( ) [ ]
7287 If the ranges have an empty intersection, result of the union
7288 operation is the anti-range or if both are anti-ranges
7290 if (*vr0type
== VR_ANTI_RANGE
7291 && vr1type
== VR_ANTI_RANGE
)
7293 else if (*vr0type
== VR_ANTI_RANGE
7294 && vr1type
== VR_RANGE
)
7296 else if (*vr0type
== VR_RANGE
7297 && vr1type
== VR_ANTI_RANGE
)
7303 else if (*vr0type
== VR_RANGE
7304 && vr1type
== VR_RANGE
)
7306 /* The result is the convex hull of both ranges. */
7307 if (operand_less_p (*vr0max
, vr1min
) == 1)
7309 /* If the result can be an anti-range, create one. */
7310 if (TREE_CODE (*vr0max
) == INTEGER_CST
7311 && TREE_CODE (vr1min
) == INTEGER_CST
7312 && vrp_val_is_min (*vr0min
)
7313 && vrp_val_is_max (vr1max
))
7315 tree min
= int_const_binop (PLUS_EXPR
,
7316 *vr0max
, integer_one_node
);
7317 tree max
= int_const_binop (MINUS_EXPR
,
7318 vr1min
, integer_one_node
);
7319 if (!operand_less_p (max
, min
))
7321 *vr0type
= VR_ANTI_RANGE
;
7333 /* If the result can be an anti-range, create one. */
7334 if (TREE_CODE (vr1max
) == INTEGER_CST
7335 && TREE_CODE (*vr0min
) == INTEGER_CST
7336 && vrp_val_is_min (vr1min
)
7337 && vrp_val_is_max (*vr0max
))
7339 tree min
= int_const_binop (PLUS_EXPR
,
7340 vr1max
, integer_one_node
);
7341 tree max
= int_const_binop (MINUS_EXPR
,
7342 *vr0min
, integer_one_node
);
7343 if (!operand_less_p (max
, min
))
7345 *vr0type
= VR_ANTI_RANGE
;
7359 else if ((maxeq
|| operand_less_p (vr1max
, *vr0max
) == 1)
7360 && (mineq
|| operand_less_p (*vr0min
, vr1min
) == 1))
7362 /* [ ( ) ] or [( ) ] or [ ( )] */
7363 if (*vr0type
== VR_RANGE
7364 && vr1type
== VR_RANGE
)
7366 else if (*vr0type
== VR_ANTI_RANGE
7367 && vr1type
== VR_ANTI_RANGE
)
7373 else if (*vr0type
== VR_ANTI_RANGE
7374 && vr1type
== VR_RANGE
)
7376 /* Arbitrarily choose the right or left gap. */
7377 if (!mineq
&& TREE_CODE (vr1min
) == INTEGER_CST
)
7378 *vr0max
= int_const_binop (MINUS_EXPR
, vr1min
, integer_one_node
);
7379 else if (!maxeq
&& TREE_CODE (vr1max
) == INTEGER_CST
)
7380 *vr0min
= int_const_binop (PLUS_EXPR
, vr1max
, integer_one_node
);
7384 else if (*vr0type
== VR_RANGE
7385 && vr1type
== VR_ANTI_RANGE
)
7386 /* The result covers everything. */
7391 else if ((maxeq
|| operand_less_p (*vr0max
, vr1max
) == 1)
7392 && (mineq
|| operand_less_p (vr1min
, *vr0min
) == 1))
7394 /* ( [ ] ) or ([ ] ) or ( [ ]) */
7395 if (*vr0type
== VR_RANGE
7396 && vr1type
== VR_RANGE
)
7402 else if (*vr0type
== VR_ANTI_RANGE
7403 && vr1type
== VR_ANTI_RANGE
)
7405 else if (*vr0type
== VR_RANGE
7406 && vr1type
== VR_ANTI_RANGE
)
7408 *vr0type
= VR_ANTI_RANGE
;
7409 if (!mineq
&& TREE_CODE (*vr0min
) == INTEGER_CST
)
7411 *vr0max
= int_const_binop (MINUS_EXPR
, *vr0min
, integer_one_node
);
7414 else if (!maxeq
&& TREE_CODE (*vr0max
) == INTEGER_CST
)
7416 *vr0min
= int_const_binop (PLUS_EXPR
, *vr0max
, integer_one_node
);
7422 else if (*vr0type
== VR_ANTI_RANGE
7423 && vr1type
== VR_RANGE
)
7424 /* The result covers everything. */
7429 else if ((operand_less_p (vr1min
, *vr0max
) == 1
7430 || operand_equal_p (vr1min
, *vr0max
, 0))
7431 && operand_less_p (*vr0min
, vr1min
) == 1)
7433 /* [ ( ] ) or [ ]( ) */
7434 if (*vr0type
== VR_RANGE
7435 && vr1type
== VR_RANGE
)
7437 else if (*vr0type
== VR_ANTI_RANGE
7438 && vr1type
== VR_ANTI_RANGE
)
7440 else if (*vr0type
== VR_ANTI_RANGE
7441 && vr1type
== VR_RANGE
)
7443 if (TREE_CODE (vr1min
) == INTEGER_CST
)
7444 *vr0max
= int_const_binop (MINUS_EXPR
, vr1min
, integer_one_node
);
7448 else if (*vr0type
== VR_RANGE
7449 && vr1type
== VR_ANTI_RANGE
)
7451 if (TREE_CODE (*vr0max
) == INTEGER_CST
)
7454 *vr0min
= int_const_binop (PLUS_EXPR
, *vr0max
, integer_one_node
);
7463 else if ((operand_less_p (*vr0min
, vr1max
) == 1
7464 || operand_equal_p (*vr0min
, vr1max
, 0))
7465 && operand_less_p (vr1min
, *vr0min
) == 1)
7467 /* ( [ ) ] or ( )[ ] */
7468 if (*vr0type
== VR_RANGE
7469 && vr1type
== VR_RANGE
)
7471 else if (*vr0type
== VR_ANTI_RANGE
7472 && vr1type
== VR_ANTI_RANGE
)
7474 else if (*vr0type
== VR_ANTI_RANGE
7475 && vr1type
== VR_RANGE
)
7477 if (TREE_CODE (vr1max
) == INTEGER_CST
)
7478 *vr0min
= int_const_binop (PLUS_EXPR
, vr1max
, integer_one_node
);
7482 else if (*vr0type
== VR_RANGE
7483 && vr1type
== VR_ANTI_RANGE
)
7485 if (TREE_CODE (*vr0min
) == INTEGER_CST
)
7489 *vr0max
= int_const_binop (MINUS_EXPR
, *vr0min
, integer_one_node
);
7503 *vr0type
= VR_VARYING
;
7504 *vr0min
= NULL_TREE
;
7505 *vr0max
= NULL_TREE
;
7508 /* Intersect the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
7509 { VR1TYPE, VR0MIN, VR0MAX } and store the result
7510 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
7511 possible such range. The resulting range is not canonicalized. */
7514 intersect_ranges (enum value_range_type
*vr0type
,
7515 tree
*vr0min
, tree
*vr0max
,
7516 enum value_range_type vr1type
,
7517 tree vr1min
, tree vr1max
)
7519 bool mineq
= operand_equal_p (*vr0min
, vr1min
, 0);
7520 bool maxeq
= operand_equal_p (*vr0max
, vr1max
, 0);
7522 /* [] is vr0, () is vr1 in the following classification comments. */
7526 if (*vr0type
== vr1type
)
7527 /* Nothing to do for equal ranges. */
7529 else if ((*vr0type
== VR_RANGE
7530 && vr1type
== VR_ANTI_RANGE
)
7531 || (*vr0type
== VR_ANTI_RANGE
7532 && vr1type
== VR_RANGE
))
7534 /* For anti-range with range intersection the result is empty. */
7535 *vr0type
= VR_UNDEFINED
;
7536 *vr0min
= NULL_TREE
;
7537 *vr0max
= NULL_TREE
;
7542 else if (operand_less_p (*vr0max
, vr1min
) == 1
7543 || operand_less_p (vr1max
, *vr0min
) == 1)
7545 /* [ ] ( ) or ( ) [ ]
7546 If the ranges have an empty intersection, the result of the
7547 intersect operation is the range for intersecting an
7548 anti-range with a range or empty when intersecting two ranges. */
7549 if (*vr0type
== VR_RANGE
7550 && vr1type
== VR_ANTI_RANGE
)
7552 else if (*vr0type
== VR_ANTI_RANGE
7553 && vr1type
== VR_RANGE
)
7559 else if (*vr0type
== VR_RANGE
7560 && vr1type
== VR_RANGE
)
7562 *vr0type
= VR_UNDEFINED
;
7563 *vr0min
= NULL_TREE
;
7564 *vr0max
= NULL_TREE
;
7566 else if (*vr0type
== VR_ANTI_RANGE
7567 && vr1type
== VR_ANTI_RANGE
)
7569 /* If the anti-ranges are adjacent to each other merge them. */
7570 if (TREE_CODE (*vr0max
) == INTEGER_CST
7571 && TREE_CODE (vr1min
) == INTEGER_CST
7572 && operand_less_p (*vr0max
, vr1min
) == 1
7573 && integer_onep (int_const_binop (MINUS_EXPR
,
7576 else if (TREE_CODE (vr1max
) == INTEGER_CST
7577 && TREE_CODE (*vr0min
) == INTEGER_CST
7578 && operand_less_p (vr1max
, *vr0min
) == 1
7579 && integer_onep (int_const_binop (MINUS_EXPR
,
7582 /* Else arbitrarily take VR0. */
7585 else if ((maxeq
|| operand_less_p (vr1max
, *vr0max
) == 1)
7586 && (mineq
|| operand_less_p (*vr0min
, vr1min
) == 1))
7588 /* [ ( ) ] or [( ) ] or [ ( )] */
7589 if (*vr0type
== VR_RANGE
7590 && vr1type
== VR_RANGE
)
7592 /* If both are ranges the result is the inner one. */
7597 else if (*vr0type
== VR_RANGE
7598 && vr1type
== VR_ANTI_RANGE
)
7600 /* Choose the right gap if the left one is empty. */
7603 if (TREE_CODE (vr1max
) == INTEGER_CST
)
7604 *vr0min
= int_const_binop (PLUS_EXPR
, vr1max
, integer_one_node
);
7608 /* Choose the left gap if the right one is empty. */
7611 if (TREE_CODE (vr1min
) == INTEGER_CST
)
7612 *vr0max
= int_const_binop (MINUS_EXPR
, vr1min
,
7617 /* Choose the anti-range if the range is effectively varying. */
7618 else if (vrp_val_is_min (*vr0min
)
7619 && vrp_val_is_max (*vr0max
))
7625 /* Else choose the range. */
7627 else if (*vr0type
== VR_ANTI_RANGE
7628 && vr1type
== VR_ANTI_RANGE
)
7629 /* If both are anti-ranges the result is the outer one. */
7631 else if (*vr0type
== VR_ANTI_RANGE
7632 && vr1type
== VR_RANGE
)
7634 /* The intersection is empty. */
7635 *vr0type
= VR_UNDEFINED
;
7636 *vr0min
= NULL_TREE
;
7637 *vr0max
= NULL_TREE
;
7642 else if ((maxeq
|| operand_less_p (*vr0max
, vr1max
) == 1)
7643 && (mineq
|| operand_less_p (vr1min
, *vr0min
) == 1))
7645 /* ( [ ] ) or ([ ] ) or ( [ ]) */
7646 if (*vr0type
== VR_RANGE
7647 && vr1type
== VR_RANGE
)
7648 /* Choose the inner range. */
7650 else if (*vr0type
== VR_ANTI_RANGE
7651 && vr1type
== VR_RANGE
)
7653 /* Choose the right gap if the left is empty. */
7656 *vr0type
= VR_RANGE
;
7657 if (TREE_CODE (*vr0max
) == INTEGER_CST
)
7658 *vr0min
= int_const_binop (PLUS_EXPR
, *vr0max
,
7664 /* Choose the left gap if the right is empty. */
7667 *vr0type
= VR_RANGE
;
7668 if (TREE_CODE (*vr0min
) == INTEGER_CST
)
7669 *vr0max
= int_const_binop (MINUS_EXPR
, *vr0min
,
7675 /* Choose the anti-range if the range is effectively varying. */
7676 else if (vrp_val_is_min (vr1min
)
7677 && vrp_val_is_max (vr1max
))
7679 /* Else choose the range. */
7687 else if (*vr0type
== VR_ANTI_RANGE
7688 && vr1type
== VR_ANTI_RANGE
)
7690 /* If both are anti-ranges the result is the outer one. */
7695 else if (vr1type
== VR_ANTI_RANGE
7696 && *vr0type
== VR_RANGE
)
7698 /* The intersection is empty. */
7699 *vr0type
= VR_UNDEFINED
;
7700 *vr0min
= NULL_TREE
;
7701 *vr0max
= NULL_TREE
;
7706 else if ((operand_less_p (vr1min
, *vr0max
) == 1
7707 || operand_equal_p (vr1min
, *vr0max
, 0))
7708 && operand_less_p (*vr0min
, vr1min
) == 1)
7710 /* [ ( ] ) or [ ]( ) */
7711 if (*vr0type
== VR_ANTI_RANGE
7712 && vr1type
== VR_ANTI_RANGE
)
7714 else if (*vr0type
== VR_RANGE
7715 && vr1type
== VR_RANGE
)
7717 else if (*vr0type
== VR_RANGE
7718 && vr1type
== VR_ANTI_RANGE
)
7720 if (TREE_CODE (vr1min
) == INTEGER_CST
)
7721 *vr0max
= int_const_binop (MINUS_EXPR
, vr1min
,
7726 else if (*vr0type
== VR_ANTI_RANGE
7727 && vr1type
== VR_RANGE
)
7729 *vr0type
= VR_RANGE
;
7730 if (TREE_CODE (*vr0max
) == INTEGER_CST
)
7731 *vr0min
= int_const_binop (PLUS_EXPR
, *vr0max
,
7740 else if ((operand_less_p (*vr0min
, vr1max
) == 1
7741 || operand_equal_p (*vr0min
, vr1max
, 0))
7742 && operand_less_p (vr1min
, *vr0min
) == 1)
7744 /* ( [ ) ] or ( )[ ] */
7745 if (*vr0type
== VR_ANTI_RANGE
7746 && vr1type
== VR_ANTI_RANGE
)
7748 else if (*vr0type
== VR_RANGE
7749 && vr1type
== VR_RANGE
)
7751 else if (*vr0type
== VR_RANGE
7752 && vr1type
== VR_ANTI_RANGE
)
7754 if (TREE_CODE (vr1max
) == INTEGER_CST
)
7755 *vr0min
= int_const_binop (PLUS_EXPR
, vr1max
,
7760 else if (*vr0type
== VR_ANTI_RANGE
7761 && vr1type
== VR_RANGE
)
7763 *vr0type
= VR_RANGE
;
7764 if (TREE_CODE (*vr0min
) == INTEGER_CST
)
7765 *vr0max
= int_const_binop (MINUS_EXPR
, *vr0min
,
7775 /* As a fallback simply use { *VRTYPE, *VR0MIN, *VR0MAX } as
7776 result for the intersection. That's always a conservative
7777 correct estimate. */
7783 /* Intersect the two value-ranges *VR0 and *VR1 and store the result
7784 in *VR0. This may not be the smallest possible such range. */
7787 vrp_intersect_ranges_1 (value_range_t
*vr0
, value_range_t
*vr1
)
7789 value_range_t saved
;
7791 /* If either range is VR_VARYING the other one wins. */
7792 if (vr1
->type
== VR_VARYING
)
7794 if (vr0
->type
== VR_VARYING
)
7796 copy_value_range (vr0
, vr1
);
7800 /* When either range is VR_UNDEFINED the resulting range is
7801 VR_UNDEFINED, too. */
7802 if (vr0
->type
== VR_UNDEFINED
)
7804 if (vr1
->type
== VR_UNDEFINED
)
7806 set_value_range_to_undefined (vr0
);
7810 /* Save the original vr0 so we can return it as conservative intersection
7811 result when our worker turns things to varying. */
7813 intersect_ranges (&vr0
->type
, &vr0
->min
, &vr0
->max
,
7814 vr1
->type
, vr1
->min
, vr1
->max
);
7815 /* Make sure to canonicalize the result though as the inversion of a
7816 VR_RANGE can still be a VR_RANGE. */
7817 set_and_canonicalize_value_range (vr0
, vr0
->type
,
7818 vr0
->min
, vr0
->max
, vr0
->equiv
);
7819 /* If that failed, use the saved original VR0. */
7820 if (vr0
->type
== VR_VARYING
)
7825 /* If the result is VR_UNDEFINED there is no need to mess with
7826 the equivalencies. */
7827 if (vr0
->type
== VR_UNDEFINED
)
7830 /* The resulting set of equivalences for range intersection is the union of
7832 if (vr0
->equiv
&& vr1
->equiv
&& vr0
->equiv
!= vr1
->equiv
)
7833 bitmap_ior_into (vr0
->equiv
, vr1
->equiv
);
7834 else if (vr1
->equiv
&& !vr0
->equiv
)
7835 bitmap_copy (vr0
->equiv
, vr1
->equiv
);
7839 vrp_intersect_ranges (value_range_t
*vr0
, value_range_t
*vr1
)
7841 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7843 fprintf (dump_file
, "Intersecting\n ");
7844 dump_value_range (dump_file
, vr0
);
7845 fprintf (dump_file
, "\nand\n ");
7846 dump_value_range (dump_file
, vr1
);
7847 fprintf (dump_file
, "\n");
7849 vrp_intersect_ranges_1 (vr0
, vr1
);
7850 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7852 fprintf (dump_file
, "to\n ");
7853 dump_value_range (dump_file
, vr0
);
7854 fprintf (dump_file
, "\n");
7858 /* Meet operation for value ranges. Given two value ranges VR0 and
7859 VR1, store in VR0 a range that contains both VR0 and VR1. This
7860 may not be the smallest possible such range. */
7863 vrp_meet_1 (value_range_t
*vr0
, value_range_t
*vr1
)
7865 value_range_t saved
;
7867 if (vr0
->type
== VR_UNDEFINED
)
7869 /* Drop equivalences. See PR53465. */
7870 set_value_range (vr0
, vr1
->type
, vr1
->min
, vr1
->max
, NULL
);
7874 if (vr1
->type
== VR_UNDEFINED
)
7876 /* VR0 already has the resulting range, just drop equivalences.
7879 bitmap_clear (vr0
->equiv
);
7883 if (vr0
->type
== VR_VARYING
)
7885 /* Nothing to do. VR0 already has the resulting range. */
7889 if (vr1
->type
== VR_VARYING
)
7891 set_value_range_to_varying (vr0
);
7896 union_ranges (&vr0
->type
, &vr0
->min
, &vr0
->max
,
7897 vr1
->type
, vr1
->min
, vr1
->max
);
7898 if (vr0
->type
== VR_VARYING
)
7900 /* Failed to find an efficient meet. Before giving up and setting
7901 the result to VARYING, see if we can at least derive a useful
7902 anti-range. FIXME, all this nonsense about distinguishing
7903 anti-ranges from ranges is necessary because of the odd
7904 semantics of range_includes_zero_p and friends. */
7905 if (((saved
.type
== VR_RANGE
7906 && range_includes_zero_p (saved
.min
, saved
.max
) == 0)
7907 || (saved
.type
== VR_ANTI_RANGE
7908 && range_includes_zero_p (saved
.min
, saved
.max
) == 1))
7909 && ((vr1
->type
== VR_RANGE
7910 && range_includes_zero_p (vr1
->min
, vr1
->max
) == 0)
7911 || (vr1
->type
== VR_ANTI_RANGE
7912 && range_includes_zero_p (vr1
->min
, vr1
->max
) == 1)))
7914 set_value_range_to_nonnull (vr0
, TREE_TYPE (saved
.min
));
7916 /* Since this meet operation did not result from the meeting of
7917 two equivalent names, VR0 cannot have any equivalences. */
7919 bitmap_clear (vr0
->equiv
);
7923 set_value_range_to_varying (vr0
);
7926 set_and_canonicalize_value_range (vr0
, vr0
->type
, vr0
->min
, vr0
->max
,
7928 if (vr0
->type
== VR_VARYING
)
7931 /* The resulting set of equivalences is always the intersection of
7933 if (vr0
->equiv
&& vr1
->equiv
&& vr0
->equiv
!= vr1
->equiv
)
7934 bitmap_and_into (vr0
->equiv
, vr1
->equiv
);
7935 else if (vr0
->equiv
&& !vr1
->equiv
)
7936 bitmap_clear (vr0
->equiv
);
7940 vrp_meet (value_range_t
*vr0
, value_range_t
*vr1
)
7942 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7944 fprintf (dump_file
, "Meeting\n ");
7945 dump_value_range (dump_file
, vr0
);
7946 fprintf (dump_file
, "\nand\n ");
7947 dump_value_range (dump_file
, vr1
);
7948 fprintf (dump_file
, "\n");
7950 vrp_meet_1 (vr0
, vr1
);
7951 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7953 fprintf (dump_file
, "to\n ");
7954 dump_value_range (dump_file
, vr0
);
7955 fprintf (dump_file
, "\n");
7960 /* Visit all arguments for PHI node PHI that flow through executable
7961 edges. If a valid value range can be derived from all the incoming
7962 value ranges, set a new range for the LHS of PHI. */
7964 static enum ssa_prop_result
7965 vrp_visit_phi_node (gimple phi
)
7968 tree lhs
= PHI_RESULT (phi
);
7969 value_range_t
*lhs_vr
= get_value_range (lhs
);
7970 value_range_t vr_result
= VR_INITIALIZER
;
7972 int edges
, old_edges
;
7975 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7977 fprintf (dump_file
, "\nVisiting PHI node: ");
7978 print_gimple_stmt (dump_file
, phi
, 0, dump_flags
);
7982 for (i
= 0; i
< gimple_phi_num_args (phi
); i
++)
7984 edge e
= gimple_phi_arg_edge (phi
, i
);
7986 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7989 "\n Argument #%d (%d -> %d %sexecutable)\n",
7990 (int) i
, e
->src
->index
, e
->dest
->index
,
7991 (e
->flags
& EDGE_EXECUTABLE
) ? "" : "not ");
7994 if (e
->flags
& EDGE_EXECUTABLE
)
7996 tree arg
= PHI_ARG_DEF (phi
, i
);
7997 value_range_t vr_arg
;
8001 if (TREE_CODE (arg
) == SSA_NAME
)
8003 vr_arg
= *(get_value_range (arg
));
8007 if (is_overflow_infinity (arg
))
8009 arg
= copy_node (arg
);
8010 TREE_OVERFLOW (arg
) = 0;
8013 vr_arg
.type
= VR_RANGE
;
8016 vr_arg
.equiv
= NULL
;
8019 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8021 fprintf (dump_file
, "\t");
8022 print_generic_expr (dump_file
, arg
, dump_flags
);
8023 fprintf (dump_file
, "\n\tValue: ");
8024 dump_value_range (dump_file
, &vr_arg
);
8025 fprintf (dump_file
, "\n");
8029 copy_value_range (&vr_result
, &vr_arg
);
8031 vrp_meet (&vr_result
, &vr_arg
);
8034 if (vr_result
.type
== VR_VARYING
)
8039 if (vr_result
.type
== VR_VARYING
)
8041 else if (vr_result
.type
== VR_UNDEFINED
)
8044 old_edges
= vr_phi_edge_counts
[SSA_NAME_VERSION (lhs
)];
8045 vr_phi_edge_counts
[SSA_NAME_VERSION (lhs
)] = edges
;
8047 /* To prevent infinite iterations in the algorithm, derive ranges
8048 when the new value is slightly bigger or smaller than the
8049 previous one. We don't do this if we have seen a new executable
8050 edge; this helps us avoid an overflow infinity for conditionals
8051 which are not in a loop. If the old value-range was VR_UNDEFINED
8052 use the updated range and iterate one more time. */
8054 && gimple_phi_num_args (phi
) > 1
8055 && edges
== old_edges
8056 && lhs_vr
->type
!= VR_UNDEFINED
)
8058 int cmp_min
= compare_values (lhs_vr
->min
, vr_result
.min
);
8059 int cmp_max
= compare_values (lhs_vr
->max
, vr_result
.max
);
8061 /* For non VR_RANGE or for pointers fall back to varying if
8062 the range changed. */
8063 if ((lhs_vr
->type
!= VR_RANGE
|| vr_result
.type
!= VR_RANGE
8064 || POINTER_TYPE_P (TREE_TYPE (lhs
)))
8065 && (cmp_min
!= 0 || cmp_max
!= 0))
8068 /* If the new minimum is smaller or larger than the previous
8069 one, go all the way to -INF. In the first case, to avoid
8070 iterating millions of times to reach -INF, and in the
8071 other case to avoid infinite bouncing between different
8073 if (cmp_min
> 0 || cmp_min
< 0)
8075 if (!needs_overflow_infinity (TREE_TYPE (vr_result
.min
))
8076 || !vrp_var_may_overflow (lhs
, phi
))
8077 vr_result
.min
= TYPE_MIN_VALUE (TREE_TYPE (vr_result
.min
));
8078 else if (supports_overflow_infinity (TREE_TYPE (vr_result
.min
)))
8080 negative_overflow_infinity (TREE_TYPE (vr_result
.min
));
8083 /* Similarly, if the new maximum is smaller or larger than
8084 the previous one, go all the way to +INF. */
8085 if (cmp_max
< 0 || cmp_max
> 0)
8087 if (!needs_overflow_infinity (TREE_TYPE (vr_result
.max
))
8088 || !vrp_var_may_overflow (lhs
, phi
))
8089 vr_result
.max
= TYPE_MAX_VALUE (TREE_TYPE (vr_result
.max
));
8090 else if (supports_overflow_infinity (TREE_TYPE (vr_result
.max
)))
8092 positive_overflow_infinity (TREE_TYPE (vr_result
.max
));
8095 /* If we dropped either bound to +-INF then if this is a loop
8096 PHI node SCEV may known more about its value-range. */
8097 if ((cmp_min
> 0 || cmp_min
< 0
8098 || cmp_max
< 0 || cmp_max
> 0)
8100 && (l
= loop_containing_stmt (phi
))
8101 && l
->header
== gimple_bb (phi
))
8102 adjust_range_with_scev (&vr_result
, l
, phi
, lhs
);
8104 /* If we will end up with a (-INF, +INF) range, set it to
8105 VARYING. Same if the previous max value was invalid for
8106 the type and we end up with vr_result.min > vr_result.max. */
8107 if ((vrp_val_is_max (vr_result
.max
)
8108 && vrp_val_is_min (vr_result
.min
))
8109 || compare_values (vr_result
.min
,
8114 /* If the new range is different than the previous value, keep
8117 if (update_value_range (lhs
, &vr_result
))
8119 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8121 fprintf (dump_file
, "Found new range for ");
8122 print_generic_expr (dump_file
, lhs
, 0);
8123 fprintf (dump_file
, ": ");
8124 dump_value_range (dump_file
, &vr_result
);
8125 fprintf (dump_file
, "\n\n");
8128 return SSA_PROP_INTERESTING
;
8131 /* Nothing changed, don't add outgoing edges. */
8132 return SSA_PROP_NOT_INTERESTING
;
8134 /* No match found. Set the LHS to VARYING. */
8136 set_value_range_to_varying (lhs_vr
);
8137 return SSA_PROP_VARYING
;
8140 /* Simplify boolean operations if the source is known
8141 to be already a boolean. */
8143 simplify_truth_ops_using_ranges (gimple_stmt_iterator
*gsi
, gimple stmt
)
8145 enum tree_code rhs_code
= gimple_assign_rhs_code (stmt
);
8147 bool need_conversion
;
8149 /* We handle only !=/== case here. */
8150 gcc_assert (rhs_code
== EQ_EXPR
|| rhs_code
== NE_EXPR
);
8152 op0
= gimple_assign_rhs1 (stmt
);
8153 if (!op_with_boolean_value_range_p (op0
))
8156 op1
= gimple_assign_rhs2 (stmt
);
8157 if (!op_with_boolean_value_range_p (op1
))
8160 /* Reduce number of cases to handle to NE_EXPR. As there is no
8161 BIT_XNOR_EXPR we cannot replace A == B with a single statement. */
8162 if (rhs_code
== EQ_EXPR
)
8164 if (TREE_CODE (op1
) == INTEGER_CST
)
8165 op1
= int_const_binop (BIT_XOR_EXPR
, op1
, integer_one_node
);
8170 lhs
= gimple_assign_lhs (stmt
);
8172 = !useless_type_conversion_p (TREE_TYPE (lhs
), TREE_TYPE (op0
));
8174 /* Make sure to not sign-extend a 1-bit 1 when converting the result. */
8176 && !TYPE_UNSIGNED (TREE_TYPE (op0
))
8177 && TYPE_PRECISION (TREE_TYPE (op0
)) == 1
8178 && TYPE_PRECISION (TREE_TYPE (lhs
)) > 1)
8181 /* For A != 0 we can substitute A itself. */
8182 if (integer_zerop (op1
))
8183 gimple_assign_set_rhs_with_ops (gsi
,
8185 ? NOP_EXPR
: TREE_CODE (op0
),
8187 /* For A != B we substitute A ^ B. Either with conversion. */
8188 else if (need_conversion
)
8190 tree tem
= make_ssa_name (TREE_TYPE (op0
), NULL
);
8191 gimple newop
= gimple_build_assign_with_ops (BIT_XOR_EXPR
, tem
, op0
, op1
);
8192 gsi_insert_before (gsi
, newop
, GSI_SAME_STMT
);
8193 gimple_assign_set_rhs_with_ops (gsi
, NOP_EXPR
, tem
, NULL_TREE
);
8197 gimple_assign_set_rhs_with_ops (gsi
, BIT_XOR_EXPR
, op0
, op1
);
8198 update_stmt (gsi_stmt (*gsi
));
8203 /* Simplify a division or modulo operator to a right shift or
8204 bitwise and if the first operand is unsigned or is greater
8205 than zero and the second operand is an exact power of two. */
8208 simplify_div_or_mod_using_ranges (gimple stmt
)
8210 enum tree_code rhs_code
= gimple_assign_rhs_code (stmt
);
8212 tree op0
= gimple_assign_rhs1 (stmt
);
8213 tree op1
= gimple_assign_rhs2 (stmt
);
8214 value_range_t
*vr
= get_value_range (gimple_assign_rhs1 (stmt
));
8216 if (TYPE_UNSIGNED (TREE_TYPE (op0
)))
8218 val
= integer_one_node
;
8224 val
= compare_range_with_value (GE_EXPR
, vr
, integer_zero_node
, &sop
);
8228 && integer_onep (val
)
8229 && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC
))
8231 location_t location
;
8233 if (!gimple_has_location (stmt
))
8234 location
= input_location
;
8236 location
= gimple_location (stmt
);
8237 warning_at (location
, OPT_Wstrict_overflow
,
8238 "assuming signed overflow does not occur when "
8239 "simplifying %</%> or %<%%%> to %<>>%> or %<&%>");
8243 if (val
&& integer_onep (val
))
8247 if (rhs_code
== TRUNC_DIV_EXPR
)
8249 t
= build_int_cst (integer_type_node
, tree_log2 (op1
));
8250 gimple_assign_set_rhs_code (stmt
, RSHIFT_EXPR
);
8251 gimple_assign_set_rhs1 (stmt
, op0
);
8252 gimple_assign_set_rhs2 (stmt
, t
);
8256 t
= build_int_cst (TREE_TYPE (op1
), 1);
8257 t
= int_const_binop (MINUS_EXPR
, op1
, t
);
8258 t
= fold_convert (TREE_TYPE (op0
), t
);
8260 gimple_assign_set_rhs_code (stmt
, BIT_AND_EXPR
);
8261 gimple_assign_set_rhs1 (stmt
, op0
);
8262 gimple_assign_set_rhs2 (stmt
, t
);
8272 /* If the operand to an ABS_EXPR is >= 0, then eliminate the
8273 ABS_EXPR. If the operand is <= 0, then simplify the
8274 ABS_EXPR into a NEGATE_EXPR. */
8277 simplify_abs_using_ranges (gimple stmt
)
8280 tree op
= gimple_assign_rhs1 (stmt
);
8281 tree type
= TREE_TYPE (op
);
8282 value_range_t
*vr
= get_value_range (op
);
8284 if (TYPE_UNSIGNED (type
))
8286 val
= integer_zero_node
;
8292 val
= compare_range_with_value (LE_EXPR
, vr
, integer_zero_node
, &sop
);
8296 val
= compare_range_with_value (GE_EXPR
, vr
, integer_zero_node
,
8301 if (integer_zerop (val
))
8302 val
= integer_one_node
;
8303 else if (integer_onep (val
))
8304 val
= integer_zero_node
;
8309 && (integer_onep (val
) || integer_zerop (val
)))
8311 if (sop
&& issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC
))
8313 location_t location
;
8315 if (!gimple_has_location (stmt
))
8316 location
= input_location
;
8318 location
= gimple_location (stmt
);
8319 warning_at (location
, OPT_Wstrict_overflow
,
8320 "assuming signed overflow does not occur when "
8321 "simplifying %<abs (X)%> to %<X%> or %<-X%>");
8324 gimple_assign_set_rhs1 (stmt
, op
);
8325 if (integer_onep (val
))
8326 gimple_assign_set_rhs_code (stmt
, NEGATE_EXPR
);
8328 gimple_assign_set_rhs_code (stmt
, SSA_NAME
);
8337 /* Optimize away redundant BIT_AND_EXPR and BIT_IOR_EXPR.
8338 If all the bits that are being cleared by & are already
8339 known to be zero from VR, or all the bits that are being
8340 set by | are already known to be one from VR, the bit
8341 operation is redundant. */
8344 simplify_bit_ops_using_ranges (gimple_stmt_iterator
*gsi
, gimple stmt
)
8346 tree op0
= gimple_assign_rhs1 (stmt
);
8347 tree op1
= gimple_assign_rhs2 (stmt
);
8348 tree op
= NULL_TREE
;
8349 value_range_t vr0
= VR_INITIALIZER
;
8350 value_range_t vr1
= VR_INITIALIZER
;
8351 double_int may_be_nonzero0
, may_be_nonzero1
;
8352 double_int must_be_nonzero0
, must_be_nonzero1
;
8355 if (TREE_CODE (op0
) == SSA_NAME
)
8356 vr0
= *(get_value_range (op0
));
8357 else if (is_gimple_min_invariant (op0
))
8358 set_value_range_to_value (&vr0
, op0
, NULL
);
8362 if (TREE_CODE (op1
) == SSA_NAME
)
8363 vr1
= *(get_value_range (op1
));
8364 else if (is_gimple_min_invariant (op1
))
8365 set_value_range_to_value (&vr1
, op1
, NULL
);
8369 if (!zero_nonzero_bits_from_vr (&vr0
, &may_be_nonzero0
, &must_be_nonzero0
))
8371 if (!zero_nonzero_bits_from_vr (&vr1
, &may_be_nonzero1
, &must_be_nonzero1
))
8374 switch (gimple_assign_rhs_code (stmt
))
8377 mask
= may_be_nonzero0
.and_not (must_be_nonzero1
);
8378 if (mask
.is_zero ())
8383 mask
= may_be_nonzero1
.and_not (must_be_nonzero0
);
8384 if (mask
.is_zero ())
8391 mask
= may_be_nonzero0
.and_not (must_be_nonzero1
);
8392 if (mask
.is_zero ())
8397 mask
= may_be_nonzero1
.and_not (must_be_nonzero0
);
8398 if (mask
.is_zero ())
8408 if (op
== NULL_TREE
)
8411 gimple_assign_set_rhs_with_ops (gsi
, TREE_CODE (op
), op
, NULL
);
8412 update_stmt (gsi_stmt (*gsi
));
8416 /* We are comparing trees OP0 and OP1 using COND_CODE. OP0 has
8417 a known value range VR.
8419 If there is one and only one value which will satisfy the
8420 conditional, then return that value. Else return NULL. */
8423 test_for_singularity (enum tree_code cond_code
, tree op0
,
8424 tree op1
, value_range_t
*vr
)
8429 /* Extract minimum/maximum values which satisfy the
8430 the conditional as it was written. */
8431 if (cond_code
== LE_EXPR
|| cond_code
== LT_EXPR
)
8433 /* This should not be negative infinity; there is no overflow
8435 min
= TYPE_MIN_VALUE (TREE_TYPE (op0
));
8438 if (cond_code
== LT_EXPR
&& !is_overflow_infinity (max
))
8440 tree one
= build_int_cst (TREE_TYPE (op0
), 1);
8441 max
= fold_build2 (MINUS_EXPR
, TREE_TYPE (op0
), max
, one
);
8443 TREE_NO_WARNING (max
) = 1;
8446 else if (cond_code
== GE_EXPR
|| cond_code
== GT_EXPR
)
8448 /* This should not be positive infinity; there is no overflow
8450 max
= TYPE_MAX_VALUE (TREE_TYPE (op0
));
8453 if (cond_code
== GT_EXPR
&& !is_overflow_infinity (min
))
8455 tree one
= build_int_cst (TREE_TYPE (op0
), 1);
8456 min
= fold_build2 (PLUS_EXPR
, TREE_TYPE (op0
), min
, one
);
8458 TREE_NO_WARNING (min
) = 1;
8462 /* Now refine the minimum and maximum values using any
8463 value range information we have for op0. */
8466 if (compare_values (vr
->min
, min
) == 1)
8468 if (compare_values (vr
->max
, max
) == -1)
8471 /* If the new min/max values have converged to a single value,
8472 then there is only one value which can satisfy the condition,
8473 return that value. */
8474 if (operand_equal_p (min
, max
, 0) && is_gimple_min_invariant (min
))
8480 /* Simplify a conditional using a relational operator to an equality
8481 test if the range information indicates only one value can satisfy
8482 the original conditional. */
8485 simplify_cond_using_ranges (gimple stmt
)
8487 tree op0
= gimple_cond_lhs (stmt
);
8488 tree op1
= gimple_cond_rhs (stmt
);
8489 enum tree_code cond_code
= gimple_cond_code (stmt
);
8491 if (cond_code
!= NE_EXPR
8492 && cond_code
!= EQ_EXPR
8493 && TREE_CODE (op0
) == SSA_NAME
8494 && INTEGRAL_TYPE_P (TREE_TYPE (op0
))
8495 && is_gimple_min_invariant (op1
))
8497 value_range_t
*vr
= get_value_range (op0
);
8499 /* If we have range information for OP0, then we might be
8500 able to simplify this conditional. */
8501 if (vr
->type
== VR_RANGE
)
8503 tree new_tree
= test_for_singularity (cond_code
, op0
, op1
, vr
);
8509 fprintf (dump_file
, "Simplified relational ");
8510 print_gimple_stmt (dump_file
, stmt
, 0, 0);
8511 fprintf (dump_file
, " into ");
8514 gimple_cond_set_code (stmt
, EQ_EXPR
);
8515 gimple_cond_set_lhs (stmt
, op0
);
8516 gimple_cond_set_rhs (stmt
, new_tree
);
8522 print_gimple_stmt (dump_file
, stmt
, 0, 0);
8523 fprintf (dump_file
, "\n");
8529 /* Try again after inverting the condition. We only deal
8530 with integral types here, so no need to worry about
8531 issues with inverting FP comparisons. */
8532 cond_code
= invert_tree_comparison (cond_code
, false);
8533 new_tree
= test_for_singularity (cond_code
, op0
, op1
, vr
);
8539 fprintf (dump_file
, "Simplified relational ");
8540 print_gimple_stmt (dump_file
, stmt
, 0, 0);
8541 fprintf (dump_file
, " into ");
8544 gimple_cond_set_code (stmt
, NE_EXPR
);
8545 gimple_cond_set_lhs (stmt
, op0
);
8546 gimple_cond_set_rhs (stmt
, new_tree
);
8552 print_gimple_stmt (dump_file
, stmt
, 0, 0);
8553 fprintf (dump_file
, "\n");
8564 /* Simplify a switch statement using the value range of the switch
8568 simplify_switch_using_ranges (gimple stmt
)
8570 tree op
= gimple_switch_index (stmt
);
8575 size_t i
= 0, j
= 0, n
, n2
;
8578 size_t k
= 1, l
= 0;
8580 if (TREE_CODE (op
) == SSA_NAME
)
8582 vr
= get_value_range (op
);
8584 /* We can only handle integer ranges. */
8585 if ((vr
->type
!= VR_RANGE
8586 && vr
->type
!= VR_ANTI_RANGE
)
8587 || symbolic_range_p (vr
))
8590 /* Find case label for min/max of the value range. */
8591 take_default
= !find_case_label_ranges (stmt
, vr
, &i
, &j
, &k
, &l
);
8593 else if (TREE_CODE (op
) == INTEGER_CST
)
8595 take_default
= !find_case_label_index (stmt
, 1, op
, &i
);
8609 n
= gimple_switch_num_labels (stmt
);
8611 /* Bail out if this is just all edges taken. */
8617 /* Build a new vector of taken case labels. */
8618 vec2
= make_tree_vec (j
- i
+ 1 + l
- k
+ 1 + (int)take_default
);
8621 /* Add the default edge, if necessary. */
8623 TREE_VEC_ELT (vec2
, n2
++) = gimple_switch_default_label (stmt
);
8625 for (; i
<= j
; ++i
, ++n2
)
8626 TREE_VEC_ELT (vec2
, n2
) = gimple_switch_label (stmt
, i
);
8628 for (; k
<= l
; ++k
, ++n2
)
8629 TREE_VEC_ELT (vec2
, n2
) = gimple_switch_label (stmt
, k
);
8631 /* Mark needed edges. */
8632 for (i
= 0; i
< n2
; ++i
)
8634 e
= find_edge (gimple_bb (stmt
),
8635 label_to_block (CASE_LABEL (TREE_VEC_ELT (vec2
, i
))));
8636 e
->aux
= (void *)-1;
8639 /* Queue not needed edges for later removal. */
8640 FOR_EACH_EDGE (e
, ei
, gimple_bb (stmt
)->succs
)
8642 if (e
->aux
== (void *)-1)
8648 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8650 fprintf (dump_file
, "removing unreachable case label\n");
8652 to_remove_edges
.safe_push (e
);
8653 e
->flags
&= ~EDGE_EXECUTABLE
;
8656 /* And queue an update for the stmt. */
8659 to_update_switch_stmts
.safe_push (su
);
8663 /* Simplify an integral conversion from an SSA name in STMT. */
8666 simplify_conversion_using_ranges (gimple stmt
)
8668 tree innerop
, middleop
, finaltype
;
8670 value_range_t
*innervr
;
8671 bool inner_unsigned_p
, middle_unsigned_p
, final_unsigned_p
;
8672 unsigned inner_prec
, middle_prec
, final_prec
;
8673 double_int innermin
, innermed
, innermax
, middlemin
, middlemed
, middlemax
;
8675 finaltype
= TREE_TYPE (gimple_assign_lhs (stmt
));
8676 if (!INTEGRAL_TYPE_P (finaltype
))
8678 middleop
= gimple_assign_rhs1 (stmt
);
8679 def_stmt
= SSA_NAME_DEF_STMT (middleop
);
8680 if (!is_gimple_assign (def_stmt
)
8681 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt
)))
8683 innerop
= gimple_assign_rhs1 (def_stmt
);
8684 if (TREE_CODE (innerop
) != SSA_NAME
)
8687 /* Get the value-range of the inner operand. */
8688 innervr
= get_value_range (innerop
);
8689 if (innervr
->type
!= VR_RANGE
8690 || TREE_CODE (innervr
->min
) != INTEGER_CST
8691 || TREE_CODE (innervr
->max
) != INTEGER_CST
)
8694 /* Simulate the conversion chain to check if the result is equal if
8695 the middle conversion is removed. */
8696 innermin
= tree_to_double_int (innervr
->min
);
8697 innermax
= tree_to_double_int (innervr
->max
);
8699 inner_prec
= TYPE_PRECISION (TREE_TYPE (innerop
));
8700 middle_prec
= TYPE_PRECISION (TREE_TYPE (middleop
));
8701 final_prec
= TYPE_PRECISION (finaltype
);
8703 /* If the first conversion is not injective, the second must not
8705 if ((innermax
- innermin
).ugt (double_int::mask (middle_prec
))
8706 && middle_prec
< final_prec
)
8708 /* We also want a medium value so that we can track the effect that
8709 narrowing conversions with sign change have. */
8710 inner_unsigned_p
= TYPE_UNSIGNED (TREE_TYPE (innerop
));
8711 if (inner_unsigned_p
)
8712 innermed
= double_int::mask (inner_prec
).lrshift (1, inner_prec
);
8714 innermed
= double_int_zero
;
8715 if (innermin
.cmp (innermed
, inner_unsigned_p
) >= 0
8716 || innermed
.cmp (innermax
, inner_unsigned_p
) >= 0)
8717 innermed
= innermin
;
8719 middle_unsigned_p
= TYPE_UNSIGNED (TREE_TYPE (middleop
));
8720 middlemin
= innermin
.ext (middle_prec
, middle_unsigned_p
);
8721 middlemed
= innermed
.ext (middle_prec
, middle_unsigned_p
);
8722 middlemax
= innermax
.ext (middle_prec
, middle_unsigned_p
);
8724 /* Require that the final conversion applied to both the original
8725 and the intermediate range produces the same result. */
8726 final_unsigned_p
= TYPE_UNSIGNED (finaltype
);
8727 if (middlemin
.ext (final_prec
, final_unsigned_p
)
8728 != innermin
.ext (final_prec
, final_unsigned_p
)
8729 || middlemed
.ext (final_prec
, final_unsigned_p
)
8730 != innermed
.ext (final_prec
, final_unsigned_p
)
8731 || middlemax
.ext (final_prec
, final_unsigned_p
)
8732 != innermax
.ext (final_prec
, final_unsigned_p
))
8735 gimple_assign_set_rhs1 (stmt
, innerop
);
8740 /* Return whether the value range *VR fits in an integer type specified
8741 by PRECISION and UNSIGNED_P. */
8744 range_fits_type_p (value_range_t
*vr
, unsigned precision
, bool unsigned_p
)
8747 unsigned src_precision
;
8750 /* We can only handle integral and pointer types. */
8751 src_type
= TREE_TYPE (vr
->min
);
8752 if (!INTEGRAL_TYPE_P (src_type
)
8753 && !POINTER_TYPE_P (src_type
))
8756 /* An extension is fine unless VR is signed and unsigned_p,
8757 and so is an identity transform. */
8758 src_precision
= TYPE_PRECISION (TREE_TYPE (vr
->min
));
8759 if ((src_precision
< precision
8760 && !(unsigned_p
&& !TYPE_UNSIGNED (src_type
)))
8761 || (src_precision
== precision
8762 && TYPE_UNSIGNED (src_type
) == unsigned_p
))
8765 /* Now we can only handle ranges with constant bounds. */
8766 if (vr
->type
!= VR_RANGE
8767 || TREE_CODE (vr
->min
) != INTEGER_CST
8768 || TREE_CODE (vr
->max
) != INTEGER_CST
)
8771 /* For sign changes, the MSB of the double_int has to be clear.
8772 An unsigned value with its MSB set cannot be represented by
8773 a signed double_int, while a negative value cannot be represented
8774 by an unsigned double_int. */
8775 if (TYPE_UNSIGNED (src_type
) != unsigned_p
8776 && (TREE_INT_CST_HIGH (vr
->min
) | TREE_INT_CST_HIGH (vr
->max
)) < 0)
8779 /* Then we can perform the conversion on both ends and compare
8780 the result for equality. */
8781 tem
= tree_to_double_int (vr
->min
).ext (precision
, unsigned_p
);
8782 if (tree_to_double_int (vr
->min
) != tem
)
8784 tem
= tree_to_double_int (vr
->max
).ext (precision
, unsigned_p
);
8785 if (tree_to_double_int (vr
->max
) != tem
)
8791 /* Simplify a conversion from integral SSA name to float in STMT. */
8794 simplify_float_conversion_using_ranges (gimple_stmt_iterator
*gsi
, gimple stmt
)
8796 tree rhs1
= gimple_assign_rhs1 (stmt
);
8797 value_range_t
*vr
= get_value_range (rhs1
);
8798 enum machine_mode fltmode
= TYPE_MODE (TREE_TYPE (gimple_assign_lhs (stmt
)));
8799 enum machine_mode mode
;
8803 /* We can only handle constant ranges. */
8804 if (vr
->type
!= VR_RANGE
8805 || TREE_CODE (vr
->min
) != INTEGER_CST
8806 || TREE_CODE (vr
->max
) != INTEGER_CST
)
8809 /* First check if we can use a signed type in place of an unsigned. */
8810 if (TYPE_UNSIGNED (TREE_TYPE (rhs1
))
8811 && (can_float_p (fltmode
, TYPE_MODE (TREE_TYPE (rhs1
)), 0)
8812 != CODE_FOR_nothing
)
8813 && range_fits_type_p (vr
, GET_MODE_PRECISION
8814 (TYPE_MODE (TREE_TYPE (rhs1
))), 0))
8815 mode
= TYPE_MODE (TREE_TYPE (rhs1
));
8816 /* If we can do the conversion in the current input mode do nothing. */
8817 else if (can_float_p (fltmode
, TYPE_MODE (TREE_TYPE (rhs1
)),
8818 TYPE_UNSIGNED (TREE_TYPE (rhs1
))) != CODE_FOR_nothing
)
8820 /* Otherwise search for a mode we can use, starting from the narrowest
8821 integer mode available. */
8824 mode
= GET_CLASS_NARROWEST_MODE (MODE_INT
);
8827 /* If we cannot do a signed conversion to float from mode
8828 or if the value-range does not fit in the signed type
8829 try with a wider mode. */
8830 if (can_float_p (fltmode
, mode
, 0) != CODE_FOR_nothing
8831 && range_fits_type_p (vr
, GET_MODE_PRECISION (mode
), 0))
8834 mode
= GET_MODE_WIDER_MODE (mode
);
8835 /* But do not widen the input. Instead leave that to the
8836 optabs expansion code. */
8837 if (GET_MODE_PRECISION (mode
) > TYPE_PRECISION (TREE_TYPE (rhs1
)))
8840 while (mode
!= VOIDmode
);
8841 if (mode
== VOIDmode
)
8845 /* It works, insert a truncation or sign-change before the
8846 float conversion. */
8847 tem
= make_ssa_name (build_nonstandard_integer_type
8848 (GET_MODE_PRECISION (mode
), 0), NULL
);
8849 conv
= gimple_build_assign_with_ops (NOP_EXPR
, tem
, rhs1
, NULL_TREE
);
8850 gsi_insert_before (gsi
, conv
, GSI_SAME_STMT
);
8851 gimple_assign_set_rhs1 (stmt
, tem
);
8857 /* Simplify STMT using ranges if possible. */
8860 simplify_stmt_using_ranges (gimple_stmt_iterator
*gsi
)
8862 gimple stmt
= gsi_stmt (*gsi
);
8863 if (is_gimple_assign (stmt
))
8865 enum tree_code rhs_code
= gimple_assign_rhs_code (stmt
);
8866 tree rhs1
= gimple_assign_rhs1 (stmt
);
8872 /* Transform EQ_EXPR, NE_EXPR into BIT_XOR_EXPR or identity
8873 if the RHS is zero or one, and the LHS are known to be boolean
8875 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
8876 return simplify_truth_ops_using_ranges (gsi
, stmt
);
8879 /* Transform TRUNC_DIV_EXPR and TRUNC_MOD_EXPR into RSHIFT_EXPR
8880 and BIT_AND_EXPR respectively if the first operand is greater
8881 than zero and the second operand is an exact power of two. */
8882 case TRUNC_DIV_EXPR
:
8883 case TRUNC_MOD_EXPR
:
8884 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1
))
8885 && integer_pow2p (gimple_assign_rhs2 (stmt
)))
8886 return simplify_div_or_mod_using_ranges (stmt
);
8889 /* Transform ABS (X) into X or -X as appropriate. */
8891 if (TREE_CODE (rhs1
) == SSA_NAME
8892 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
8893 return simplify_abs_using_ranges (stmt
);
8898 /* Optimize away BIT_AND_EXPR and BIT_IOR_EXPR
8899 if all the bits being cleared are already cleared or
8900 all the bits being set are already set. */
8901 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
8902 return simplify_bit_ops_using_ranges (gsi
, stmt
);
8906 if (TREE_CODE (rhs1
) == SSA_NAME
8907 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
8908 return simplify_conversion_using_ranges (stmt
);
8912 if (TREE_CODE (rhs1
) == SSA_NAME
8913 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
8914 return simplify_float_conversion_using_ranges (gsi
, stmt
);
8921 else if (gimple_code (stmt
) == GIMPLE_COND
)
8922 return simplify_cond_using_ranges (stmt
);
8923 else if (gimple_code (stmt
) == GIMPLE_SWITCH
)
8924 return simplify_switch_using_ranges (stmt
);
8929 /* If the statement pointed by SI has a predicate whose value can be
8930 computed using the value range information computed by VRP, compute
8931 its value and return true. Otherwise, return false. */
8934 fold_predicate_in (gimple_stmt_iterator
*si
)
8936 bool assignment_p
= false;
8938 gimple stmt
= gsi_stmt (*si
);
8940 if (is_gimple_assign (stmt
)
8941 && TREE_CODE_CLASS (gimple_assign_rhs_code (stmt
)) == tcc_comparison
)
8943 assignment_p
= true;
8944 val
= vrp_evaluate_conditional (gimple_assign_rhs_code (stmt
),
8945 gimple_assign_rhs1 (stmt
),
8946 gimple_assign_rhs2 (stmt
),
8949 else if (gimple_code (stmt
) == GIMPLE_COND
)
8950 val
= vrp_evaluate_conditional (gimple_cond_code (stmt
),
8951 gimple_cond_lhs (stmt
),
8952 gimple_cond_rhs (stmt
),
8960 val
= fold_convert (gimple_expr_type (stmt
), val
);
8964 fprintf (dump_file
, "Folding predicate ");
8965 print_gimple_expr (dump_file
, stmt
, 0, 0);
8966 fprintf (dump_file
, " to ");
8967 print_generic_expr (dump_file
, val
, 0);
8968 fprintf (dump_file
, "\n");
8971 if (is_gimple_assign (stmt
))
8972 gimple_assign_set_rhs_from_tree (si
, val
);
8975 gcc_assert (gimple_code (stmt
) == GIMPLE_COND
);
8976 if (integer_zerop (val
))
8977 gimple_cond_make_false (stmt
);
8978 else if (integer_onep (val
))
8979 gimple_cond_make_true (stmt
);
8990 /* Callback for substitute_and_fold folding the stmt at *SI. */
8993 vrp_fold_stmt (gimple_stmt_iterator
*si
)
8995 if (fold_predicate_in (si
))
8998 return simplify_stmt_using_ranges (si
);
9001 /* Stack of dest,src equivalency pairs that need to be restored after
9002 each attempt to thread a block's incoming edge to an outgoing edge.
9004 A NULL entry is used to mark the end of pairs which need to be
9006 static vec
<tree
> equiv_stack
;
9008 /* A trivial wrapper so that we can present the generic jump threading
9009 code with a simple API for simplifying statements. STMT is the
9010 statement we want to simplify, WITHIN_STMT provides the location
9011 for any overflow warnings. */
9014 simplify_stmt_for_jump_threading (gimple stmt
, gimple within_stmt
)
9016 /* We only use VRP information to simplify conditionals. This is
9017 overly conservative, but it's unclear if doing more would be
9018 worth the compile time cost. */
9019 if (gimple_code (stmt
) != GIMPLE_COND
)
9022 return vrp_evaluate_conditional (gimple_cond_code (stmt
),
9023 gimple_cond_lhs (stmt
),
9024 gimple_cond_rhs (stmt
), within_stmt
);
9027 /* Blocks which have more than one predecessor and more than
9028 one successor present jump threading opportunities, i.e.,
9029 when the block is reached from a specific predecessor, we
9030 may be able to determine which of the outgoing edges will
9031 be traversed. When this optimization applies, we are able
9032 to avoid conditionals at runtime and we may expose secondary
9033 optimization opportunities.
9035 This routine is effectively a driver for the generic jump
9036 threading code. It basically just presents the generic code
9037 with edges that may be suitable for jump threading.
9039 Unlike DOM, we do not iterate VRP if jump threading was successful.
9040 While iterating may expose new opportunities for VRP, it is expected
9041 those opportunities would be very limited and the compile time cost
9042 to expose those opportunities would be significant.
9044 As jump threading opportunities are discovered, they are registered
9045 for later realization. */
9048 identify_jump_threads (void)
9055 /* Ugh. When substituting values earlier in this pass we can
9056 wipe the dominance information. So rebuild the dominator
9057 information as we need it within the jump threading code. */
9058 calculate_dominance_info (CDI_DOMINATORS
);
9060 /* We do not allow VRP information to be used for jump threading
9061 across a back edge in the CFG. Otherwise it becomes too
9062 difficult to avoid eliminating loop exit tests. Of course
9063 EDGE_DFS_BACK is not accurate at this time so we have to
9065 mark_dfs_back_edges ();
9067 /* Do not thread across edges we are about to remove. Just marking
9068 them as EDGE_DFS_BACK will do. */
9069 FOR_EACH_VEC_ELT (to_remove_edges
, i
, e
)
9070 e
->flags
|= EDGE_DFS_BACK
;
9072 /* Allocate our unwinder stack to unwind any temporary equivalences
9073 that might be recorded. */
9074 equiv_stack
.create (20);
9076 /* To avoid lots of silly node creation, we create a single
9077 conditional and just modify it in-place when attempting to
9079 dummy
= gimple_build_cond (EQ_EXPR
,
9080 integer_zero_node
, integer_zero_node
,
9083 /* Walk through all the blocks finding those which present a
9084 potential jump threading opportunity. We could set this up
9085 as a dominator walker and record data during the walk, but
9086 I doubt it's worth the effort for the classes of jump
9087 threading opportunities we are trying to identify at this
9088 point in compilation. */
9093 /* If the generic jump threading code does not find this block
9094 interesting, then there is nothing to do. */
9095 if (! potentially_threadable_block (bb
))
9098 /* We only care about blocks ending in a COND_EXPR. While there
9099 may be some value in handling SWITCH_EXPR here, I doubt it's
9100 terribly important. */
9101 last
= gsi_stmt (gsi_last_bb (bb
));
9103 /* We're basically looking for a switch or any kind of conditional with
9104 integral or pointer type arguments. Note the type of the second
9105 argument will be the same as the first argument, so no need to
9106 check it explicitly. */
9107 if (gimple_code (last
) == GIMPLE_SWITCH
9108 || (gimple_code (last
) == GIMPLE_COND
9109 && TREE_CODE (gimple_cond_lhs (last
)) == SSA_NAME
9110 && (INTEGRAL_TYPE_P (TREE_TYPE (gimple_cond_lhs (last
)))
9111 || POINTER_TYPE_P (TREE_TYPE (gimple_cond_lhs (last
))))
9112 && (TREE_CODE (gimple_cond_rhs (last
)) == SSA_NAME
9113 || is_gimple_min_invariant (gimple_cond_rhs (last
)))))
9117 /* We've got a block with multiple predecessors and multiple
9118 successors which also ends in a suitable conditional or
9119 switch statement. For each predecessor, see if we can thread
9120 it to a specific successor. */
9121 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
9123 /* Do not thread across back edges or abnormal edges
9125 if (e
->flags
& (EDGE_DFS_BACK
| EDGE_COMPLEX
))
9128 thread_across_edge (dummy
, e
, true, &equiv_stack
,
9129 simplify_stmt_for_jump_threading
);
9134 /* We do not actually update the CFG or SSA graphs at this point as
9135 ASSERT_EXPRs are still in the IL and cfg cleanup code does not yet
9136 handle ASSERT_EXPRs gracefully. */
9139 /* We identified all the jump threading opportunities earlier, but could
9140 not transform the CFG at that time. This routine transforms the
9141 CFG and arranges for the dominator tree to be rebuilt if necessary.
9143 Note the SSA graph update will occur during the normal TODO
9144 processing by the pass manager. */
9146 finalize_jump_threads (void)
9148 thread_through_all_blocks (false);
9149 equiv_stack
.release ();
9153 /* Traverse all the blocks folding conditionals with known ranges. */
9160 values_propagated
= true;
9164 fprintf (dump_file
, "\nValue ranges after VRP:\n\n");
9165 dump_all_value_ranges (dump_file
);
9166 fprintf (dump_file
, "\n");
9169 substitute_and_fold (op_with_constant_singleton_value_range
,
9170 vrp_fold_stmt
, false);
9172 if (warn_array_bounds
)
9173 check_all_array_refs ();
9175 /* We must identify jump threading opportunities before we release
9176 the datastructures built by VRP. */
9177 identify_jump_threads ();
9179 /* Free allocated memory. */
9180 for (i
= 0; i
< num_vr_values
; i
++)
9183 BITMAP_FREE (vr_value
[i
]->equiv
);
9188 free (vr_phi_edge_counts
);
9190 /* So that we can distinguish between VRP data being available
9191 and not available. */
9193 vr_phi_edge_counts
= NULL
;
9197 /* Main entry point to VRP (Value Range Propagation). This pass is
9198 loosely based on J. R. C. Patterson, ``Accurate Static Branch
9199 Prediction by Value Range Propagation,'' in SIGPLAN Conference on
9200 Programming Language Design and Implementation, pp. 67-78, 1995.
9201 Also available at http://citeseer.ist.psu.edu/patterson95accurate.html
9203 This is essentially an SSA-CCP pass modified to deal with ranges
9204 instead of constants.
9206 While propagating ranges, we may find that two or more SSA name
9207 have equivalent, though distinct ranges. For instance,
9210 2 p_4 = ASSERT_EXPR <p_3, p_3 != 0>
9212 4 p_5 = ASSERT_EXPR <p_4, p_4 == q_2>;
9216 In the code above, pointer p_5 has range [q_2, q_2], but from the
9217 code we can also determine that p_5 cannot be NULL and, if q_2 had
9218 a non-varying range, p_5's range should also be compatible with it.
9220 These equivalences are created by two expressions: ASSERT_EXPR and
9221 copy operations. Since p_5 is an assertion on p_4, and p_4 was the
9222 result of another assertion, then we can use the fact that p_5 and
9223 p_4 are equivalent when evaluating p_5's range.
9225 Together with value ranges, we also propagate these equivalences
9226 between names so that we can take advantage of information from
9227 multiple ranges when doing final replacement. Note that this
9228 equivalency relation is transitive but not symmetric.
9230 In the example above, p_5 is equivalent to p_4, q_2 and p_3, but we
9231 cannot assert that q_2 is equivalent to p_5 because q_2 may be used
9232 in contexts where that assertion does not hold (e.g., in line 6).
9234 TODO, the main difference between this pass and Patterson's is that
9235 we do not propagate edge probabilities. We only compute whether
9236 edges can be taken or not. That is, instead of having a spectrum
9237 of jump probabilities between 0 and 1, we only deal with 0, 1 and
9238 DON'T KNOW. In the future, it may be worthwhile to propagate
9239 probabilities to aid branch prediction. */
9248 loop_optimizer_init (LOOPS_NORMAL
| LOOPS_HAVE_RECORDED_EXITS
);
9249 rewrite_into_loop_closed_ssa (NULL
, TODO_update_ssa
);
9252 insert_range_assertions ();
9254 to_remove_edges
.create (10);
9255 to_update_switch_stmts
.create (5);
9256 threadedge_initialize_values ();
9259 ssa_propagate (vrp_visit_stmt
, vrp_visit_phi_node
);
9262 free_numbers_of_iterations_estimates ();
9264 /* ASSERT_EXPRs must be removed before finalizing jump threads
9265 as finalizing jump threads calls the CFG cleanup code which
9266 does not properly handle ASSERT_EXPRs. */
9267 remove_range_assertions ();
9269 /* If we exposed any new variables, go ahead and put them into
9270 SSA form now, before we handle jump threading. This simplifies
9271 interactions between rewriting of _DECL nodes into SSA form
9272 and rewriting SSA_NAME nodes into SSA form after block
9273 duplication and CFG manipulation. */
9274 update_ssa (TODO_update_ssa
);
9276 finalize_jump_threads ();
9278 /* Remove dead edges from SWITCH_EXPR optimization. This leaves the
9279 CFG in a broken state and requires a cfg_cleanup run. */
9280 FOR_EACH_VEC_ELT (to_remove_edges
, i
, e
)
9282 /* Update SWITCH_EXPR case label vector. */
9283 FOR_EACH_VEC_ELT (to_update_switch_stmts
, i
, su
)
9286 size_t n
= TREE_VEC_LENGTH (su
->vec
);
9288 gimple_switch_set_num_labels (su
->stmt
, n
);
9289 for (j
= 0; j
< n
; j
++)
9290 gimple_switch_set_label (su
->stmt
, j
, TREE_VEC_ELT (su
->vec
, j
));
9291 /* As we may have replaced the default label with a regular one
9292 make sure to make it a real default label again. This ensures
9293 optimal expansion. */
9294 label
= gimple_switch_label (su
->stmt
, 0);
9295 CASE_LOW (label
) = NULL_TREE
;
9296 CASE_HIGH (label
) = NULL_TREE
;
9299 if (to_remove_edges
.length () > 0)
9300 free_dominance_info (CDI_DOMINATORS
);
9302 to_remove_edges
.release ();
9303 to_update_switch_stmts
.release ();
9304 threadedge_finalize_values ();
9307 loop_optimizer_finalize ();
9314 return flag_tree_vrp
!= 0;
9317 struct gimple_opt_pass pass_vrp
=
9322 OPTGROUP_NONE
, /* optinfo_flags */
9323 gate_vrp
, /* gate */
9324 execute_vrp
, /* execute */
9327 0, /* static_pass_number */
9328 TV_TREE_VRP
, /* tv_id */
9329 PROP_ssa
, /* properties_required */
9330 0, /* properties_provided */
9331 0, /* properties_destroyed */
9332 0, /* todo_flags_start */
9337 | TODO_ggc_collect
/* todo_flags_finish */