1 /* Support routines for Value Range Propagation (VRP).
2 Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
3 Free Software Foundation, Inc.
4 Contributed by Diego Novillo <dnovillo@redhat.com>.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
24 #include "coretypes.h"
29 #include "basic-block.h"
30 #include "tree-flow.h"
31 #include "tree-pass.h"
32 #include "tree-dump.h"
34 #include "tree-pretty-print.h"
35 #include "gimple-pretty-print.h"
36 #include "diagnostic-core.h"
39 #include "tree-scalar-evolution.h"
40 #include "tree-ssa-propagate.h"
41 #include "tree-chrec.h"
42 #include "gimple-fold.h"
47 /* Type of value ranges. See value_range_d for a description of these
49 enum value_range_type
{ VR_UNDEFINED
, VR_RANGE
, VR_ANTI_RANGE
, VR_VARYING
};
51 /* Range of values that can be associated with an SSA_NAME after VRP
55 /* Lattice value represented by this range. */
56 enum value_range_type type
;
58 /* Minimum and maximum values represented by this range. These
59 values should be interpreted as follows:
61 - If TYPE is VR_UNDEFINED or VR_VARYING then MIN and MAX must
64 - If TYPE == VR_RANGE then MIN holds the minimum value and
65 MAX holds the maximum value of the range [MIN, MAX].
67 - If TYPE == ANTI_RANGE the variable is known to NOT
68 take any values in the range [MIN, MAX]. */
72 /* Set of SSA names whose value ranges are equivalent to this one.
73 This set is only valid when TYPE is VR_RANGE or VR_ANTI_RANGE. */
77 typedef struct value_range_d value_range_t
;
79 #define VR_INITIALIZER { VR_UNDEFINED, NULL_TREE, NULL_TREE, NULL }
81 /* Set of SSA names found live during the RPO traversal of the function
82 for still active basic-blocks. */
85 /* Return true if the SSA name NAME is live on the edge E. */
88 live_on_edge (edge e
, tree name
)
90 return (live
[e
->dest
->index
]
91 && TEST_BIT (live
[e
->dest
->index
], SSA_NAME_VERSION (name
)));
94 /* Local functions. */
95 static int compare_values (tree val1
, tree val2
);
96 static int compare_values_warnv (tree val1
, tree val2
, bool *);
97 static void vrp_meet (value_range_t
*, value_range_t
*);
98 static void vrp_intersect_ranges (value_range_t
*, value_range_t
*);
99 static tree
vrp_evaluate_conditional_warnv_with_ops (enum tree_code
,
100 tree
, tree
, bool, bool *,
103 /* Location information for ASSERT_EXPRs. Each instance of this
104 structure describes an ASSERT_EXPR for an SSA name. Since a single
105 SSA name may have more than one assertion associated with it, these
106 locations are kept in a linked list attached to the corresponding
108 struct assert_locus_d
110 /* Basic block where the assertion would be inserted. */
113 /* Some assertions need to be inserted on an edge (e.g., assertions
114 generated by COND_EXPRs). In those cases, BB will be NULL. */
117 /* Pointer to the statement that generated this assertion. */
118 gimple_stmt_iterator si
;
120 /* Predicate code for the ASSERT_EXPR. Must be COMPARISON_CLASS_P. */
121 enum tree_code comp_code
;
123 /* Value being compared against. */
126 /* Expression to compare. */
129 /* Next node in the linked list. */
130 struct assert_locus_d
*next
;
133 typedef struct assert_locus_d
*assert_locus_t
;
135 /* If bit I is present, it means that SSA name N_i has a list of
136 assertions that should be inserted in the IL. */
137 static bitmap need_assert_for
;
139 /* Array of locations lists where to insert assertions. ASSERTS_FOR[I]
140 holds a list of ASSERT_LOCUS_T nodes that describe where
141 ASSERT_EXPRs for SSA name N_I should be inserted. */
142 static assert_locus_t
*asserts_for
;
144 /* Value range array. After propagation, VR_VALUE[I] holds the range
145 of values that SSA name N_I may take. */
146 static unsigned num_vr_values
;
147 static value_range_t
**vr_value
;
148 static bool values_propagated
;
150 /* For a PHI node which sets SSA name N_I, VR_COUNTS[I] holds the
151 number of executable edges we saw the last time we visited the
153 static int *vr_phi_edge_counts
;
160 static VEC (edge
, heap
) *to_remove_edges
;
161 DEF_VEC_O(switch_update
);
162 DEF_VEC_ALLOC_O(switch_update
, heap
);
163 static VEC (switch_update
, heap
) *to_update_switch_stmts
;
166 /* Return the maximum value for TYPE. */
169 vrp_val_max (const_tree type
)
171 if (!INTEGRAL_TYPE_P (type
))
174 return TYPE_MAX_VALUE (type
);
177 /* Return the minimum value for TYPE. */
180 vrp_val_min (const_tree type
)
182 if (!INTEGRAL_TYPE_P (type
))
185 return TYPE_MIN_VALUE (type
);
188 /* Return whether VAL is equal to the maximum value of its type. This
189 will be true for a positive overflow infinity. We can't do a
190 simple equality comparison with TYPE_MAX_VALUE because C typedefs
191 and Ada subtypes can produce types whose TYPE_MAX_VALUE is not ==
192 to the integer constant with the same value in the type. */
195 vrp_val_is_max (const_tree val
)
197 tree type_max
= vrp_val_max (TREE_TYPE (val
));
198 return (val
== type_max
199 || (type_max
!= NULL_TREE
200 && operand_equal_p (val
, type_max
, 0)));
203 /* Return whether VAL is equal to the minimum value of its type. This
204 will be true for a negative overflow infinity. */
207 vrp_val_is_min (const_tree val
)
209 tree type_min
= vrp_val_min (TREE_TYPE (val
));
210 return (val
== type_min
211 || (type_min
!= NULL_TREE
212 && operand_equal_p (val
, type_min
, 0)));
216 /* Return whether TYPE should use an overflow infinity distinct from
217 TYPE_{MIN,MAX}_VALUE. We use an overflow infinity value to
218 represent a signed overflow during VRP computations. An infinity
219 is distinct from a half-range, which will go from some number to
220 TYPE_{MIN,MAX}_VALUE. */
223 needs_overflow_infinity (const_tree type
)
225 return INTEGRAL_TYPE_P (type
) && !TYPE_OVERFLOW_WRAPS (type
);
228 /* Return whether TYPE can support our overflow infinity
229 representation: we use the TREE_OVERFLOW flag, which only exists
230 for constants. If TYPE doesn't support this, we don't optimize
231 cases which would require signed overflow--we drop them to
235 supports_overflow_infinity (const_tree type
)
237 tree min
= vrp_val_min (type
), max
= vrp_val_max (type
);
238 #ifdef ENABLE_CHECKING
239 gcc_assert (needs_overflow_infinity (type
));
241 return (min
!= NULL_TREE
242 && CONSTANT_CLASS_P (min
)
244 && CONSTANT_CLASS_P (max
));
247 /* VAL is the maximum or minimum value of a type. Return a
248 corresponding overflow infinity. */
251 make_overflow_infinity (tree val
)
253 gcc_checking_assert (val
!= NULL_TREE
&& CONSTANT_CLASS_P (val
));
254 val
= copy_node (val
);
255 TREE_OVERFLOW (val
) = 1;
259 /* Return a negative overflow infinity for TYPE. */
262 negative_overflow_infinity (tree type
)
264 gcc_checking_assert (supports_overflow_infinity (type
));
265 return make_overflow_infinity (vrp_val_min (type
));
268 /* Return a positive overflow infinity for TYPE. */
271 positive_overflow_infinity (tree type
)
273 gcc_checking_assert (supports_overflow_infinity (type
));
274 return make_overflow_infinity (vrp_val_max (type
));
277 /* Return whether VAL is a negative overflow infinity. */
280 is_negative_overflow_infinity (const_tree val
)
282 return (needs_overflow_infinity (TREE_TYPE (val
))
283 && CONSTANT_CLASS_P (val
)
284 && TREE_OVERFLOW (val
)
285 && vrp_val_is_min (val
));
288 /* Return whether VAL is a positive overflow infinity. */
291 is_positive_overflow_infinity (const_tree val
)
293 return (needs_overflow_infinity (TREE_TYPE (val
))
294 && CONSTANT_CLASS_P (val
)
295 && TREE_OVERFLOW (val
)
296 && vrp_val_is_max (val
));
299 /* Return whether VAL is a positive or negative overflow infinity. */
302 is_overflow_infinity (const_tree val
)
304 return (needs_overflow_infinity (TREE_TYPE (val
))
305 && CONSTANT_CLASS_P (val
)
306 && TREE_OVERFLOW (val
)
307 && (vrp_val_is_min (val
) || vrp_val_is_max (val
)));
310 /* Return whether STMT has a constant rhs that is_overflow_infinity. */
313 stmt_overflow_infinity (gimple stmt
)
315 if (is_gimple_assign (stmt
)
316 && get_gimple_rhs_class (gimple_assign_rhs_code (stmt
)) ==
318 return is_overflow_infinity (gimple_assign_rhs1 (stmt
));
322 /* If VAL is now an overflow infinity, return VAL. Otherwise, return
323 the same value with TREE_OVERFLOW clear. This can be used to avoid
324 confusing a regular value with an overflow value. */
327 avoid_overflow_infinity (tree val
)
329 if (!is_overflow_infinity (val
))
332 if (vrp_val_is_max (val
))
333 return vrp_val_max (TREE_TYPE (val
));
336 gcc_checking_assert (vrp_val_is_min (val
));
337 return vrp_val_min (TREE_TYPE (val
));
342 /* Return true if ARG is marked with the nonnull attribute in the
343 current function signature. */
346 nonnull_arg_p (const_tree arg
)
348 tree t
, attrs
, fntype
;
349 unsigned HOST_WIDE_INT arg_num
;
351 gcc_assert (TREE_CODE (arg
) == PARM_DECL
&& POINTER_TYPE_P (TREE_TYPE (arg
)));
353 /* The static chain decl is always non null. */
354 if (arg
== cfun
->static_chain_decl
)
357 fntype
= TREE_TYPE (current_function_decl
);
358 attrs
= lookup_attribute ("nonnull", TYPE_ATTRIBUTES (fntype
));
360 /* If "nonnull" wasn't specified, we know nothing about the argument. */
361 if (attrs
== NULL_TREE
)
364 /* If "nonnull" applies to all the arguments, then ARG is non-null. */
365 if (TREE_VALUE (attrs
) == NULL_TREE
)
368 /* Get the position number for ARG in the function signature. */
369 for (arg_num
= 1, t
= DECL_ARGUMENTS (current_function_decl
);
371 t
= DECL_CHAIN (t
), arg_num
++)
377 gcc_assert (t
== arg
);
379 /* Now see if ARG_NUM is mentioned in the nonnull list. */
380 for (t
= TREE_VALUE (attrs
); t
; t
= TREE_CHAIN (t
))
382 if (compare_tree_int (TREE_VALUE (t
), arg_num
) == 0)
390 /* Set value range VR to VR_UNDEFINED. */
393 set_value_range_to_undefined (value_range_t
*vr
)
395 vr
->type
= VR_UNDEFINED
;
396 vr
->min
= vr
->max
= NULL_TREE
;
398 bitmap_clear (vr
->equiv
);
402 /* Set value range VR to VR_VARYING. */
405 set_value_range_to_varying (value_range_t
*vr
)
407 vr
->type
= VR_VARYING
;
408 vr
->min
= vr
->max
= NULL_TREE
;
410 bitmap_clear (vr
->equiv
);
414 /* Set value range VR to {T, MIN, MAX, EQUIV}. */
417 set_value_range (value_range_t
*vr
, enum value_range_type t
, tree min
,
418 tree max
, bitmap equiv
)
420 #if defined ENABLE_CHECKING
421 /* Check the validity of the range. */
422 if (t
== VR_RANGE
|| t
== VR_ANTI_RANGE
)
426 gcc_assert (min
&& max
);
428 if (INTEGRAL_TYPE_P (TREE_TYPE (min
)) && t
== VR_ANTI_RANGE
)
429 gcc_assert (!vrp_val_is_min (min
) || !vrp_val_is_max (max
));
431 cmp
= compare_values (min
, max
);
432 gcc_assert (cmp
== 0 || cmp
== -1 || cmp
== -2);
434 if (needs_overflow_infinity (TREE_TYPE (min
)))
435 gcc_assert (!is_overflow_infinity (min
)
436 || !is_overflow_infinity (max
));
439 if (t
== VR_UNDEFINED
|| t
== VR_VARYING
)
440 gcc_assert (min
== NULL_TREE
&& max
== NULL_TREE
);
442 if (t
== VR_UNDEFINED
|| t
== VR_VARYING
)
443 gcc_assert (equiv
== NULL
|| bitmap_empty_p (equiv
));
450 /* Since updating the equivalence set involves deep copying the
451 bitmaps, only do it if absolutely necessary. */
452 if (vr
->equiv
== NULL
454 vr
->equiv
= BITMAP_ALLOC (NULL
);
456 if (equiv
!= vr
->equiv
)
458 if (equiv
&& !bitmap_empty_p (equiv
))
459 bitmap_copy (vr
->equiv
, equiv
);
461 bitmap_clear (vr
->equiv
);
466 /* Set value range VR to the canonical form of {T, MIN, MAX, EQUIV}.
467 This means adjusting T, MIN and MAX representing the case of a
468 wrapping range with MAX < MIN covering [MIN, type_max] U [type_min, MAX]
469 as anti-rage ~[MAX+1, MIN-1]. Likewise for wrapping anti-ranges.
470 In corner cases where MAX+1 or MIN-1 wraps this will fall back
472 This routine exists to ease canonicalization in the case where we
473 extract ranges from var + CST op limit. */
476 set_and_canonicalize_value_range (value_range_t
*vr
, enum value_range_type t
,
477 tree min
, tree max
, bitmap equiv
)
479 /* Use the canonical setters for VR_UNDEFINED and VR_VARYING. */
480 if (t
== VR_UNDEFINED
)
482 set_value_range_to_undefined (vr
);
485 else if (t
== VR_VARYING
)
487 set_value_range_to_varying (vr
);
491 /* Nothing to canonicalize for symbolic ranges. */
492 if (TREE_CODE (min
) != INTEGER_CST
493 || TREE_CODE (max
) != INTEGER_CST
)
495 set_value_range (vr
, t
, min
, max
, equiv
);
499 /* Wrong order for min and max, to swap them and the VR type we need
501 if (tree_int_cst_lt (max
, min
))
503 tree one
= build_int_cst (TREE_TYPE (min
), 1);
504 tree tmp
= int_const_binop (PLUS_EXPR
, max
, one
);
505 max
= int_const_binop (MINUS_EXPR
, min
, one
);
508 /* There's one corner case, if we had [C+1, C] before we now have
509 that again. But this represents an empty value range, so drop
510 to varying in this case. */
511 if (tree_int_cst_lt (max
, min
))
513 set_value_range_to_varying (vr
);
517 t
= t
== VR_RANGE
? VR_ANTI_RANGE
: VR_RANGE
;
520 /* Anti-ranges that can be represented as ranges should be so. */
521 if (t
== VR_ANTI_RANGE
)
523 bool is_min
= vrp_val_is_min (min
);
524 bool is_max
= vrp_val_is_max (max
);
526 if (is_min
&& is_max
)
528 /* We cannot deal with empty ranges, drop to varying.
529 ??? This could be VR_UNDEFINED instead. */
530 set_value_range_to_varying (vr
);
534 /* As a special exception preserve non-null ranges. */
535 && !(TYPE_UNSIGNED (TREE_TYPE (min
))
536 && integer_zerop (max
)))
538 tree one
= build_int_cst (TREE_TYPE (max
), 1);
539 min
= int_const_binop (PLUS_EXPR
, max
, one
);
540 max
= vrp_val_max (TREE_TYPE (max
));
545 tree one
= build_int_cst (TREE_TYPE (min
), 1);
546 max
= int_const_binop (MINUS_EXPR
, min
, one
);
547 min
= vrp_val_min (TREE_TYPE (min
));
552 /* Drop [-INF(OVF), +INF(OVF)] to varying. */
553 if (needs_overflow_infinity (TREE_TYPE (min
))
554 && is_overflow_infinity (min
)
555 && is_overflow_infinity (max
))
557 set_value_range_to_varying (vr
);
561 set_value_range (vr
, t
, min
, max
, equiv
);
564 /* Copy value range FROM into value range TO. */
567 copy_value_range (value_range_t
*to
, value_range_t
*from
)
569 set_value_range (to
, from
->type
, from
->min
, from
->max
, from
->equiv
);
572 /* Set value range VR to a single value. This function is only called
573 with values we get from statements, and exists to clear the
574 TREE_OVERFLOW flag so that we don't think we have an overflow
575 infinity when we shouldn't. */
578 set_value_range_to_value (value_range_t
*vr
, tree val
, bitmap equiv
)
580 gcc_assert (is_gimple_min_invariant (val
));
581 val
= avoid_overflow_infinity (val
);
582 set_value_range (vr
, VR_RANGE
, val
, val
, equiv
);
585 /* Set value range VR to a non-negative range of type TYPE.
586 OVERFLOW_INFINITY indicates whether to use an overflow infinity
587 rather than TYPE_MAX_VALUE; this should be true if we determine
588 that the range is nonnegative based on the assumption that signed
589 overflow does not occur. */
592 set_value_range_to_nonnegative (value_range_t
*vr
, tree type
,
593 bool overflow_infinity
)
597 if (overflow_infinity
&& !supports_overflow_infinity (type
))
599 set_value_range_to_varying (vr
);
603 zero
= build_int_cst (type
, 0);
604 set_value_range (vr
, VR_RANGE
, zero
,
606 ? positive_overflow_infinity (type
)
607 : TYPE_MAX_VALUE (type
)),
611 /* Set value range VR to a non-NULL range of type TYPE. */
614 set_value_range_to_nonnull (value_range_t
*vr
, tree type
)
616 tree zero
= build_int_cst (type
, 0);
617 set_value_range (vr
, VR_ANTI_RANGE
, zero
, zero
, vr
->equiv
);
621 /* Set value range VR to a NULL range of type TYPE. */
624 set_value_range_to_null (value_range_t
*vr
, tree type
)
626 set_value_range_to_value (vr
, build_int_cst (type
, 0), vr
->equiv
);
630 /* Set value range VR to a range of a truthvalue of type TYPE. */
633 set_value_range_to_truthvalue (value_range_t
*vr
, tree type
)
635 if (TYPE_PRECISION (type
) == 1)
636 set_value_range_to_varying (vr
);
638 set_value_range (vr
, VR_RANGE
,
639 build_int_cst (type
, 0), build_int_cst (type
, 1),
644 /* If abs (min) < abs (max), set VR to [-max, max], if
645 abs (min) >= abs (max), set VR to [-min, min]. */
648 abs_extent_range (value_range_t
*vr
, tree min
, tree max
)
652 gcc_assert (TREE_CODE (min
) == INTEGER_CST
);
653 gcc_assert (TREE_CODE (max
) == INTEGER_CST
);
654 gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (min
)));
655 gcc_assert (!TYPE_UNSIGNED (TREE_TYPE (min
)));
656 min
= fold_unary (ABS_EXPR
, TREE_TYPE (min
), min
);
657 max
= fold_unary (ABS_EXPR
, TREE_TYPE (max
), max
);
658 if (TREE_OVERFLOW (min
) || TREE_OVERFLOW (max
))
660 set_value_range_to_varying (vr
);
663 cmp
= compare_values (min
, max
);
665 min
= fold_unary (NEGATE_EXPR
, TREE_TYPE (min
), max
);
666 else if (cmp
== 0 || cmp
== 1)
669 min
= fold_unary (NEGATE_EXPR
, TREE_TYPE (min
), min
);
673 set_value_range_to_varying (vr
);
676 set_and_canonicalize_value_range (vr
, VR_RANGE
, min
, max
, NULL
);
680 /* Return value range information for VAR.
682 If we have no values ranges recorded (ie, VRP is not running), then
683 return NULL. Otherwise create an empty range if none existed for VAR. */
685 static value_range_t
*
686 get_value_range (const_tree var
)
688 static const struct value_range_d vr_const_varying
689 = { VR_VARYING
, NULL_TREE
, NULL_TREE
, NULL
};
692 unsigned ver
= SSA_NAME_VERSION (var
);
694 /* If we have no recorded ranges, then return NULL. */
698 /* If we query the range for a new SSA name return an unmodifiable VARYING.
699 We should get here at most from the substitute-and-fold stage which
700 will never try to change values. */
701 if (ver
>= num_vr_values
)
702 return CONST_CAST (value_range_t
*, &vr_const_varying
);
708 /* After propagation finished do not allocate new value-ranges. */
709 if (values_propagated
)
710 return CONST_CAST (value_range_t
*, &vr_const_varying
);
712 /* Create a default value range. */
713 vr_value
[ver
] = vr
= XCNEW (value_range_t
);
715 /* Defer allocating the equivalence set. */
718 /* If VAR is a default definition of a parameter, the variable can
719 take any value in VAR's type. */
720 sym
= SSA_NAME_VAR (var
);
721 if (SSA_NAME_IS_DEFAULT_DEF (var
))
723 if (TREE_CODE (sym
) == PARM_DECL
)
725 /* Try to use the "nonnull" attribute to create ~[0, 0]
726 anti-ranges for pointers. Note that this is only valid with
727 default definitions of PARM_DECLs. */
728 if (POINTER_TYPE_P (TREE_TYPE (sym
))
729 && nonnull_arg_p (sym
))
730 set_value_range_to_nonnull (vr
, TREE_TYPE (sym
));
732 set_value_range_to_varying (vr
);
734 else if (TREE_CODE (sym
) == RESULT_DECL
735 && DECL_BY_REFERENCE (sym
))
736 set_value_range_to_nonnull (vr
, TREE_TYPE (sym
));
742 /* Return true, if VAL1 and VAL2 are equal values for VRP purposes. */
745 vrp_operand_equal_p (const_tree val1
, const_tree val2
)
749 if (!val1
|| !val2
|| !operand_equal_p (val1
, val2
, 0))
751 if (is_overflow_infinity (val1
))
752 return is_overflow_infinity (val2
);
756 /* Return true, if the bitmaps B1 and B2 are equal. */
759 vrp_bitmap_equal_p (const_bitmap b1
, const_bitmap b2
)
762 || ((!b1
|| bitmap_empty_p (b1
))
763 && (!b2
|| bitmap_empty_p (b2
)))
765 && bitmap_equal_p (b1
, b2
)));
768 /* Update the value range and equivalence set for variable VAR to
769 NEW_VR. Return true if NEW_VR is different from VAR's previous
772 NOTE: This function assumes that NEW_VR is a temporary value range
773 object created for the sole purpose of updating VAR's range. The
774 storage used by the equivalence set from NEW_VR will be freed by
775 this function. Do not call update_value_range when NEW_VR
776 is the range object associated with another SSA name. */
779 update_value_range (const_tree var
, value_range_t
*new_vr
)
781 value_range_t
*old_vr
;
784 /* Update the value range, if necessary. */
785 old_vr
= get_value_range (var
);
786 is_new
= old_vr
->type
!= new_vr
->type
787 || !vrp_operand_equal_p (old_vr
->min
, new_vr
->min
)
788 || !vrp_operand_equal_p (old_vr
->max
, new_vr
->max
)
789 || !vrp_bitmap_equal_p (old_vr
->equiv
, new_vr
->equiv
);
792 set_value_range (old_vr
, new_vr
->type
, new_vr
->min
, new_vr
->max
,
795 BITMAP_FREE (new_vr
->equiv
);
801 /* Add VAR and VAR's equivalence set to EQUIV. This is the central
802 point where equivalence processing can be turned on/off. */
805 add_equivalence (bitmap
*equiv
, const_tree var
)
807 unsigned ver
= SSA_NAME_VERSION (var
);
808 value_range_t
*vr
= vr_value
[ver
];
811 *equiv
= BITMAP_ALLOC (NULL
);
812 bitmap_set_bit (*equiv
, ver
);
814 bitmap_ior_into (*equiv
, vr
->equiv
);
818 /* Return true if VR is ~[0, 0]. */
821 range_is_nonnull (value_range_t
*vr
)
823 return vr
->type
== VR_ANTI_RANGE
824 && integer_zerop (vr
->min
)
825 && integer_zerop (vr
->max
);
829 /* Return true if VR is [0, 0]. */
832 range_is_null (value_range_t
*vr
)
834 return vr
->type
== VR_RANGE
835 && integer_zerop (vr
->min
)
836 && integer_zerop (vr
->max
);
839 /* Return true if max and min of VR are INTEGER_CST. It's not necessary
843 range_int_cst_p (value_range_t
*vr
)
845 return (vr
->type
== VR_RANGE
846 && TREE_CODE (vr
->max
) == INTEGER_CST
847 && TREE_CODE (vr
->min
) == INTEGER_CST
);
850 /* Return true if VR is a INTEGER_CST singleton. */
853 range_int_cst_singleton_p (value_range_t
*vr
)
855 return (range_int_cst_p (vr
)
856 && !TREE_OVERFLOW (vr
->min
)
857 && !TREE_OVERFLOW (vr
->max
)
858 && tree_int_cst_equal (vr
->min
, vr
->max
));
861 /* Return true if value range VR involves at least one symbol. */
864 symbolic_range_p (value_range_t
*vr
)
866 return (!is_gimple_min_invariant (vr
->min
)
867 || !is_gimple_min_invariant (vr
->max
));
870 /* Return true if value range VR uses an overflow infinity. */
873 overflow_infinity_range_p (value_range_t
*vr
)
875 return (vr
->type
== VR_RANGE
876 && (is_overflow_infinity (vr
->min
)
877 || is_overflow_infinity (vr
->max
)));
880 /* Return false if we can not make a valid comparison based on VR;
881 this will be the case if it uses an overflow infinity and overflow
882 is not undefined (i.e., -fno-strict-overflow is in effect).
883 Otherwise return true, and set *STRICT_OVERFLOW_P to true if VR
884 uses an overflow infinity. */
887 usable_range_p (value_range_t
*vr
, bool *strict_overflow_p
)
889 gcc_assert (vr
->type
== VR_RANGE
);
890 if (is_overflow_infinity (vr
->min
))
892 *strict_overflow_p
= true;
893 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (vr
->min
)))
896 if (is_overflow_infinity (vr
->max
))
898 *strict_overflow_p
= true;
899 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (vr
->max
)))
906 /* Return true if the result of assignment STMT is know to be non-negative.
907 If the return value is based on the assumption that signed overflow is
908 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
909 *STRICT_OVERFLOW_P.*/
912 gimple_assign_nonnegative_warnv_p (gimple stmt
, bool *strict_overflow_p
)
914 enum tree_code code
= gimple_assign_rhs_code (stmt
);
915 switch (get_gimple_rhs_class (code
))
917 case GIMPLE_UNARY_RHS
:
918 return tree_unary_nonnegative_warnv_p (gimple_assign_rhs_code (stmt
),
919 gimple_expr_type (stmt
),
920 gimple_assign_rhs1 (stmt
),
922 case GIMPLE_BINARY_RHS
:
923 return tree_binary_nonnegative_warnv_p (gimple_assign_rhs_code (stmt
),
924 gimple_expr_type (stmt
),
925 gimple_assign_rhs1 (stmt
),
926 gimple_assign_rhs2 (stmt
),
928 case GIMPLE_TERNARY_RHS
:
930 case GIMPLE_SINGLE_RHS
:
931 return tree_single_nonnegative_warnv_p (gimple_assign_rhs1 (stmt
),
933 case GIMPLE_INVALID_RHS
:
940 /* Return true if return value of call STMT is know to be non-negative.
941 If the return value is based on the assumption that signed overflow is
942 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
943 *STRICT_OVERFLOW_P.*/
946 gimple_call_nonnegative_warnv_p (gimple stmt
, bool *strict_overflow_p
)
948 tree arg0
= gimple_call_num_args (stmt
) > 0 ?
949 gimple_call_arg (stmt
, 0) : NULL_TREE
;
950 tree arg1
= gimple_call_num_args (stmt
) > 1 ?
951 gimple_call_arg (stmt
, 1) : NULL_TREE
;
953 return tree_call_nonnegative_warnv_p (gimple_expr_type (stmt
),
954 gimple_call_fndecl (stmt
),
960 /* Return true if STMT is know to to compute a non-negative value.
961 If the return value is based on the assumption that signed overflow is
962 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
963 *STRICT_OVERFLOW_P.*/
966 gimple_stmt_nonnegative_warnv_p (gimple stmt
, bool *strict_overflow_p
)
968 switch (gimple_code (stmt
))
971 return gimple_assign_nonnegative_warnv_p (stmt
, strict_overflow_p
);
973 return gimple_call_nonnegative_warnv_p (stmt
, strict_overflow_p
);
979 /* Return true if the result of assignment STMT is know to be non-zero.
980 If the return value is based on the assumption that signed overflow is
981 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
982 *STRICT_OVERFLOW_P.*/
985 gimple_assign_nonzero_warnv_p (gimple stmt
, bool *strict_overflow_p
)
987 enum tree_code code
= gimple_assign_rhs_code (stmt
);
988 switch (get_gimple_rhs_class (code
))
990 case GIMPLE_UNARY_RHS
:
991 return tree_unary_nonzero_warnv_p (gimple_assign_rhs_code (stmt
),
992 gimple_expr_type (stmt
),
993 gimple_assign_rhs1 (stmt
),
995 case GIMPLE_BINARY_RHS
:
996 return tree_binary_nonzero_warnv_p (gimple_assign_rhs_code (stmt
),
997 gimple_expr_type (stmt
),
998 gimple_assign_rhs1 (stmt
),
999 gimple_assign_rhs2 (stmt
),
1001 case GIMPLE_TERNARY_RHS
:
1003 case GIMPLE_SINGLE_RHS
:
1004 return tree_single_nonzero_warnv_p (gimple_assign_rhs1 (stmt
),
1006 case GIMPLE_INVALID_RHS
:
1013 /* Return true if STMT is know to to compute a non-zero value.
1014 If the return value is based on the assumption that signed overflow is
1015 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1016 *STRICT_OVERFLOW_P.*/
1019 gimple_stmt_nonzero_warnv_p (gimple stmt
, bool *strict_overflow_p
)
1021 switch (gimple_code (stmt
))
1024 return gimple_assign_nonzero_warnv_p (stmt
, strict_overflow_p
);
1026 return gimple_alloca_call_p (stmt
);
1032 /* Like tree_expr_nonzero_warnv_p, but this function uses value ranges
1036 vrp_stmt_computes_nonzero (gimple stmt
, bool *strict_overflow_p
)
1038 if (gimple_stmt_nonzero_warnv_p (stmt
, strict_overflow_p
))
1041 /* If we have an expression of the form &X->a, then the expression
1042 is nonnull if X is nonnull. */
1043 if (is_gimple_assign (stmt
)
1044 && gimple_assign_rhs_code (stmt
) == ADDR_EXPR
)
1046 tree expr
= gimple_assign_rhs1 (stmt
);
1047 tree base
= get_base_address (TREE_OPERAND (expr
, 0));
1049 if (base
!= NULL_TREE
1050 && TREE_CODE (base
) == MEM_REF
1051 && TREE_CODE (TREE_OPERAND (base
, 0)) == SSA_NAME
)
1053 value_range_t
*vr
= get_value_range (TREE_OPERAND (base
, 0));
1054 if (range_is_nonnull (vr
))
1062 /* Returns true if EXPR is a valid value (as expected by compare_values) --
1063 a gimple invariant, or SSA_NAME +- CST. */
1066 valid_value_p (tree expr
)
1068 if (TREE_CODE (expr
) == SSA_NAME
)
1071 if (TREE_CODE (expr
) == PLUS_EXPR
1072 || TREE_CODE (expr
) == MINUS_EXPR
)
1073 return (TREE_CODE (TREE_OPERAND (expr
, 0)) == SSA_NAME
1074 && TREE_CODE (TREE_OPERAND (expr
, 1)) == INTEGER_CST
);
1076 return is_gimple_min_invariant (expr
);
1082 -2 if those are incomparable. */
1084 operand_less_p (tree val
, tree val2
)
1086 /* LT is folded faster than GE and others. Inline the common case. */
1087 if (TREE_CODE (val
) == INTEGER_CST
&& TREE_CODE (val2
) == INTEGER_CST
)
1089 if (TYPE_UNSIGNED (TREE_TYPE (val
)))
1090 return INT_CST_LT_UNSIGNED (val
, val2
);
1093 if (INT_CST_LT (val
, val2
))
1101 fold_defer_overflow_warnings ();
1103 tcmp
= fold_binary_to_constant (LT_EXPR
, boolean_type_node
, val
, val2
);
1105 fold_undefer_and_ignore_overflow_warnings ();
1108 || TREE_CODE (tcmp
) != INTEGER_CST
)
1111 if (!integer_zerop (tcmp
))
1115 /* val >= val2, not considering overflow infinity. */
1116 if (is_negative_overflow_infinity (val
))
1117 return is_negative_overflow_infinity (val2
) ? 0 : 1;
1118 else if (is_positive_overflow_infinity (val2
))
1119 return is_positive_overflow_infinity (val
) ? 0 : 1;
1124 /* Compare two values VAL1 and VAL2. Return
1126 -2 if VAL1 and VAL2 cannot be compared at compile-time,
1129 +1 if VAL1 > VAL2, and
1132 This is similar to tree_int_cst_compare but supports pointer values
1133 and values that cannot be compared at compile time.
1135 If STRICT_OVERFLOW_P is not NULL, then set *STRICT_OVERFLOW_P to
1136 true if the return value is only valid if we assume that signed
1137 overflow is undefined. */
1140 compare_values_warnv (tree val1
, tree val2
, bool *strict_overflow_p
)
1145 /* Below we rely on the fact that VAL1 and VAL2 are both pointers or
1147 gcc_assert (POINTER_TYPE_P (TREE_TYPE (val1
))
1148 == POINTER_TYPE_P (TREE_TYPE (val2
)));
1149 /* Convert the two values into the same type. This is needed because
1150 sizetype causes sign extension even for unsigned types. */
1151 val2
= fold_convert (TREE_TYPE (val1
), val2
);
1152 STRIP_USELESS_TYPE_CONVERSION (val2
);
1154 if ((TREE_CODE (val1
) == SSA_NAME
1155 || TREE_CODE (val1
) == PLUS_EXPR
1156 || TREE_CODE (val1
) == MINUS_EXPR
)
1157 && (TREE_CODE (val2
) == SSA_NAME
1158 || TREE_CODE (val2
) == PLUS_EXPR
1159 || TREE_CODE (val2
) == MINUS_EXPR
))
1161 tree n1
, c1
, n2
, c2
;
1162 enum tree_code code1
, code2
;
1164 /* If VAL1 and VAL2 are of the form 'NAME [+-] CST' or 'NAME',
1165 return -1 or +1 accordingly. If VAL1 and VAL2 don't use the
1166 same name, return -2. */
1167 if (TREE_CODE (val1
) == SSA_NAME
)
1175 code1
= TREE_CODE (val1
);
1176 n1
= TREE_OPERAND (val1
, 0);
1177 c1
= TREE_OPERAND (val1
, 1);
1178 if (tree_int_cst_sgn (c1
) == -1)
1180 if (is_negative_overflow_infinity (c1
))
1182 c1
= fold_unary_to_constant (NEGATE_EXPR
, TREE_TYPE (c1
), c1
);
1185 code1
= code1
== MINUS_EXPR
? PLUS_EXPR
: MINUS_EXPR
;
1189 if (TREE_CODE (val2
) == SSA_NAME
)
1197 code2
= TREE_CODE (val2
);
1198 n2
= TREE_OPERAND (val2
, 0);
1199 c2
= TREE_OPERAND (val2
, 1);
1200 if (tree_int_cst_sgn (c2
) == -1)
1202 if (is_negative_overflow_infinity (c2
))
1204 c2
= fold_unary_to_constant (NEGATE_EXPR
, TREE_TYPE (c2
), c2
);
1207 code2
= code2
== MINUS_EXPR
? PLUS_EXPR
: MINUS_EXPR
;
1211 /* Both values must use the same name. */
1215 if (code1
== SSA_NAME
1216 && code2
== SSA_NAME
)
1220 /* If overflow is defined we cannot simplify more. */
1221 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (val1
)))
1224 if (strict_overflow_p
!= NULL
1225 && (code1
== SSA_NAME
|| !TREE_NO_WARNING (val1
))
1226 && (code2
== SSA_NAME
|| !TREE_NO_WARNING (val2
)))
1227 *strict_overflow_p
= true;
1229 if (code1
== SSA_NAME
)
1231 if (code2
== PLUS_EXPR
)
1232 /* NAME < NAME + CST */
1234 else if (code2
== MINUS_EXPR
)
1235 /* NAME > NAME - CST */
1238 else if (code1
== PLUS_EXPR
)
1240 if (code2
== SSA_NAME
)
1241 /* NAME + CST > NAME */
1243 else if (code2
== PLUS_EXPR
)
1244 /* NAME + CST1 > NAME + CST2, if CST1 > CST2 */
1245 return compare_values_warnv (c1
, c2
, strict_overflow_p
);
1246 else if (code2
== MINUS_EXPR
)
1247 /* NAME + CST1 > NAME - CST2 */
1250 else if (code1
== MINUS_EXPR
)
1252 if (code2
== SSA_NAME
)
1253 /* NAME - CST < NAME */
1255 else if (code2
== PLUS_EXPR
)
1256 /* NAME - CST1 < NAME + CST2 */
1258 else if (code2
== MINUS_EXPR
)
1259 /* NAME - CST1 > NAME - CST2, if CST1 < CST2. Notice that
1260 C1 and C2 are swapped in the call to compare_values. */
1261 return compare_values_warnv (c2
, c1
, strict_overflow_p
);
1267 /* We cannot compare non-constants. */
1268 if (!is_gimple_min_invariant (val1
) || !is_gimple_min_invariant (val2
))
1271 if (!POINTER_TYPE_P (TREE_TYPE (val1
)))
1273 /* We cannot compare overflowed values, except for overflow
1275 if (TREE_OVERFLOW (val1
) || TREE_OVERFLOW (val2
))
1277 if (strict_overflow_p
!= NULL
)
1278 *strict_overflow_p
= true;
1279 if (is_negative_overflow_infinity (val1
))
1280 return is_negative_overflow_infinity (val2
) ? 0 : -1;
1281 else if (is_negative_overflow_infinity (val2
))
1283 else if (is_positive_overflow_infinity (val1
))
1284 return is_positive_overflow_infinity (val2
) ? 0 : 1;
1285 else if (is_positive_overflow_infinity (val2
))
1290 return tree_int_cst_compare (val1
, val2
);
1296 /* First see if VAL1 and VAL2 are not the same. */
1297 if (val1
== val2
|| operand_equal_p (val1
, val2
, 0))
1300 /* If VAL1 is a lower address than VAL2, return -1. */
1301 if (operand_less_p (val1
, val2
) == 1)
1304 /* If VAL1 is a higher address than VAL2, return +1. */
1305 if (operand_less_p (val2
, val1
) == 1)
1308 /* If VAL1 is different than VAL2, return +2.
1309 For integer constants we either have already returned -1 or 1
1310 or they are equivalent. We still might succeed in proving
1311 something about non-trivial operands. */
1312 if (TREE_CODE (val1
) != INTEGER_CST
1313 || TREE_CODE (val2
) != INTEGER_CST
)
1315 t
= fold_binary_to_constant (NE_EXPR
, boolean_type_node
, val1
, val2
);
1316 if (t
&& integer_onep (t
))
1324 /* Compare values like compare_values_warnv, but treat comparisons of
1325 nonconstants which rely on undefined overflow as incomparable. */
1328 compare_values (tree val1
, tree val2
)
1334 ret
= compare_values_warnv (val1
, val2
, &sop
);
1336 && (!is_gimple_min_invariant (val1
) || !is_gimple_min_invariant (val2
)))
1342 /* Return 1 if VAL is inside value range VR (VR->MIN <= VAL <= VR->MAX),
1343 0 if VAL is not inside VR,
1344 -2 if we cannot tell either way.
1346 FIXME, the current semantics of this functions are a bit quirky
1347 when taken in the context of VRP. In here we do not care
1348 about VR's type. If VR is the anti-range ~[3, 5] the call
1349 value_inside_range (4, VR) will return 1.
1351 This is counter-intuitive in a strict sense, but the callers
1352 currently expect this. They are calling the function
1353 merely to determine whether VR->MIN <= VAL <= VR->MAX. The
1354 callers are applying the VR_RANGE/VR_ANTI_RANGE semantics
1357 This also applies to value_ranges_intersect_p and
1358 range_includes_zero_p. The semantics of VR_RANGE and
1359 VR_ANTI_RANGE should be encoded here, but that also means
1360 adapting the users of these functions to the new semantics.
1362 Benchmark compile/20001226-1.c compilation time after changing this
1366 value_inside_range (tree val
, value_range_t
* vr
)
1370 cmp1
= operand_less_p (val
, vr
->min
);
1376 cmp2
= operand_less_p (vr
->max
, val
);
1384 /* Return true if value ranges VR0 and VR1 have a non-empty
1387 Benchmark compile/20001226-1.c compilation time after changing this
1392 value_ranges_intersect_p (value_range_t
*vr0
, value_range_t
*vr1
)
1394 /* The value ranges do not intersect if the maximum of the first range is
1395 less than the minimum of the second range or vice versa.
1396 When those relations are unknown, we can't do any better. */
1397 if (operand_less_p (vr0
->max
, vr1
->min
) != 0)
1399 if (operand_less_p (vr1
->max
, vr0
->min
) != 0)
1405 /* Return true if VR includes the value zero, false otherwise. FIXME,
1406 currently this will return false for an anti-range like ~[-4, 3].
1407 This will be wrong when the semantics of value_inside_range are
1408 modified (currently the users of this function expect these
1412 range_includes_zero_p (value_range_t
*vr
)
1416 gcc_assert (vr
->type
!= VR_UNDEFINED
1417 && vr
->type
!= VR_VARYING
1418 && !symbolic_range_p (vr
));
1420 zero
= build_int_cst (TREE_TYPE (vr
->min
), 0);
1421 return (value_inside_range (zero
, vr
) == 1);
1424 /* Return true if *VR is know to only contain nonnegative values. */
1427 value_range_nonnegative_p (value_range_t
*vr
)
1429 /* Testing for VR_ANTI_RANGE is not useful here as any anti-range
1430 which would return a useful value should be encoded as a
1432 if (vr
->type
== VR_RANGE
)
1434 int result
= compare_values (vr
->min
, integer_zero_node
);
1435 return (result
== 0 || result
== 1);
1441 /* Return true if T, an SSA_NAME, is known to be nonnegative. Return
1442 false otherwise or if no value range information is available. */
1445 ssa_name_nonnegative_p (const_tree t
)
1447 value_range_t
*vr
= get_value_range (t
);
1449 if (INTEGRAL_TYPE_P (t
)
1450 && TYPE_UNSIGNED (t
))
1456 return value_range_nonnegative_p (vr
);
1459 /* If *VR has a value rante that is a single constant value return that,
1460 otherwise return NULL_TREE. */
1463 value_range_constant_singleton (value_range_t
*vr
)
1465 if (vr
->type
== VR_RANGE
1466 && operand_equal_p (vr
->min
, vr
->max
, 0)
1467 && is_gimple_min_invariant (vr
->min
))
1473 /* If OP has a value range with a single constant value return that,
1474 otherwise return NULL_TREE. This returns OP itself if OP is a
1478 op_with_constant_singleton_value_range (tree op
)
1480 if (is_gimple_min_invariant (op
))
1483 if (TREE_CODE (op
) != SSA_NAME
)
1486 return value_range_constant_singleton (get_value_range (op
));
1489 /* Return true if op is in a boolean [0, 1] value-range. */
1492 op_with_boolean_value_range_p (tree op
)
1496 if (TYPE_PRECISION (TREE_TYPE (op
)) == 1)
1499 if (integer_zerop (op
)
1500 || integer_onep (op
))
1503 if (TREE_CODE (op
) != SSA_NAME
)
1506 vr
= get_value_range (op
);
1507 return (vr
->type
== VR_RANGE
1508 && integer_zerop (vr
->min
)
1509 && integer_onep (vr
->max
));
1512 /* Extract value range information from an ASSERT_EXPR EXPR and store
1516 extract_range_from_assert (value_range_t
*vr_p
, tree expr
)
1518 tree var
, cond
, limit
, min
, max
, type
;
1519 value_range_t
*limit_vr
;
1520 enum tree_code cond_code
;
1522 var
= ASSERT_EXPR_VAR (expr
);
1523 cond
= ASSERT_EXPR_COND (expr
);
1525 gcc_assert (COMPARISON_CLASS_P (cond
));
1527 /* Find VAR in the ASSERT_EXPR conditional. */
1528 if (var
== TREE_OPERAND (cond
, 0)
1529 || TREE_CODE (TREE_OPERAND (cond
, 0)) == PLUS_EXPR
1530 || TREE_CODE (TREE_OPERAND (cond
, 0)) == NOP_EXPR
)
1532 /* If the predicate is of the form VAR COMP LIMIT, then we just
1533 take LIMIT from the RHS and use the same comparison code. */
1534 cond_code
= TREE_CODE (cond
);
1535 limit
= TREE_OPERAND (cond
, 1);
1536 cond
= TREE_OPERAND (cond
, 0);
1540 /* If the predicate is of the form LIMIT COMP VAR, then we need
1541 to flip around the comparison code to create the proper range
1543 cond_code
= swap_tree_comparison (TREE_CODE (cond
));
1544 limit
= TREE_OPERAND (cond
, 0);
1545 cond
= TREE_OPERAND (cond
, 1);
1548 limit
= avoid_overflow_infinity (limit
);
1550 type
= TREE_TYPE (var
);
1551 gcc_assert (limit
!= var
);
1553 /* For pointer arithmetic, we only keep track of pointer equality
1555 if (POINTER_TYPE_P (type
) && cond_code
!= NE_EXPR
&& cond_code
!= EQ_EXPR
)
1557 set_value_range_to_varying (vr_p
);
1561 /* If LIMIT is another SSA name and LIMIT has a range of its own,
1562 try to use LIMIT's range to avoid creating symbolic ranges
1564 limit_vr
= (TREE_CODE (limit
) == SSA_NAME
) ? get_value_range (limit
) : NULL
;
1566 /* LIMIT's range is only interesting if it has any useful information. */
1568 && (limit_vr
->type
== VR_UNDEFINED
1569 || limit_vr
->type
== VR_VARYING
1570 || symbolic_range_p (limit_vr
)))
1573 /* Initially, the new range has the same set of equivalences of
1574 VAR's range. This will be revised before returning the final
1575 value. Since assertions may be chained via mutually exclusive
1576 predicates, we will need to trim the set of equivalences before
1578 gcc_assert (vr_p
->equiv
== NULL
);
1579 add_equivalence (&vr_p
->equiv
, var
);
1581 /* Extract a new range based on the asserted comparison for VAR and
1582 LIMIT's value range. Notice that if LIMIT has an anti-range, we
1583 will only use it for equality comparisons (EQ_EXPR). For any
1584 other kind of assertion, we cannot derive a range from LIMIT's
1585 anti-range that can be used to describe the new range. For
1586 instance, ASSERT_EXPR <x_2, x_2 <= b_4>. If b_4 is ~[2, 10],
1587 then b_4 takes on the ranges [-INF, 1] and [11, +INF]. There is
1588 no single range for x_2 that could describe LE_EXPR, so we might
1589 as well build the range [b_4, +INF] for it.
1590 One special case we handle is extracting a range from a
1591 range test encoded as (unsigned)var + CST <= limit. */
1592 if (TREE_CODE (cond
) == NOP_EXPR
1593 || TREE_CODE (cond
) == PLUS_EXPR
)
1595 if (TREE_CODE (cond
) == PLUS_EXPR
)
1597 min
= fold_build1 (NEGATE_EXPR
, TREE_TYPE (TREE_OPERAND (cond
, 1)),
1598 TREE_OPERAND (cond
, 1));
1599 max
= int_const_binop (PLUS_EXPR
, limit
, min
);
1600 cond
= TREE_OPERAND (cond
, 0);
1604 min
= build_int_cst (TREE_TYPE (var
), 0);
1608 /* Make sure to not set TREE_OVERFLOW on the final type
1609 conversion. We are willingly interpreting large positive
1610 unsigned values as negative singed values here. */
1611 min
= force_fit_type_double (TREE_TYPE (var
), tree_to_double_int (min
),
1613 max
= force_fit_type_double (TREE_TYPE (var
), tree_to_double_int (max
),
1616 /* We can transform a max, min range to an anti-range or
1617 vice-versa. Use set_and_canonicalize_value_range which does
1619 if (cond_code
== LE_EXPR
)
1620 set_and_canonicalize_value_range (vr_p
, VR_RANGE
,
1621 min
, max
, vr_p
->equiv
);
1622 else if (cond_code
== GT_EXPR
)
1623 set_and_canonicalize_value_range (vr_p
, VR_ANTI_RANGE
,
1624 min
, max
, vr_p
->equiv
);
1628 else if (cond_code
== EQ_EXPR
)
1630 enum value_range_type range_type
;
1634 range_type
= limit_vr
->type
;
1635 min
= limit_vr
->min
;
1636 max
= limit_vr
->max
;
1640 range_type
= VR_RANGE
;
1645 set_value_range (vr_p
, range_type
, min
, max
, vr_p
->equiv
);
1647 /* When asserting the equality VAR == LIMIT and LIMIT is another
1648 SSA name, the new range will also inherit the equivalence set
1650 if (TREE_CODE (limit
) == SSA_NAME
)
1651 add_equivalence (&vr_p
->equiv
, limit
);
1653 else if (cond_code
== NE_EXPR
)
1655 /* As described above, when LIMIT's range is an anti-range and
1656 this assertion is an inequality (NE_EXPR), then we cannot
1657 derive anything from the anti-range. For instance, if
1658 LIMIT's range was ~[0, 0], the assertion 'VAR != LIMIT' does
1659 not imply that VAR's range is [0, 0]. So, in the case of
1660 anti-ranges, we just assert the inequality using LIMIT and
1663 If LIMIT_VR is a range, we can only use it to build a new
1664 anti-range if LIMIT_VR is a single-valued range. For
1665 instance, if LIMIT_VR is [0, 1], the predicate
1666 VAR != [0, 1] does not mean that VAR's range is ~[0, 1].
1667 Rather, it means that for value 0 VAR should be ~[0, 0]
1668 and for value 1, VAR should be ~[1, 1]. We cannot
1669 represent these ranges.
1671 The only situation in which we can build a valid
1672 anti-range is when LIMIT_VR is a single-valued range
1673 (i.e., LIMIT_VR->MIN == LIMIT_VR->MAX). In that case,
1674 build the anti-range ~[LIMIT_VR->MIN, LIMIT_VR->MAX]. */
1676 && limit_vr
->type
== VR_RANGE
1677 && compare_values (limit_vr
->min
, limit_vr
->max
) == 0)
1679 min
= limit_vr
->min
;
1680 max
= limit_vr
->max
;
1684 /* In any other case, we cannot use LIMIT's range to build a
1685 valid anti-range. */
1689 /* If MIN and MAX cover the whole range for their type, then
1690 just use the original LIMIT. */
1691 if (INTEGRAL_TYPE_P (type
)
1692 && vrp_val_is_min (min
)
1693 && vrp_val_is_max (max
))
1696 set_value_range (vr_p
, VR_ANTI_RANGE
, min
, max
, vr_p
->equiv
);
1698 else if (cond_code
== LE_EXPR
|| cond_code
== LT_EXPR
)
1700 min
= TYPE_MIN_VALUE (type
);
1702 if (limit_vr
== NULL
|| limit_vr
->type
== VR_ANTI_RANGE
)
1706 /* If LIMIT_VR is of the form [N1, N2], we need to build the
1707 range [MIN, N2] for LE_EXPR and [MIN, N2 - 1] for
1709 max
= limit_vr
->max
;
1712 /* If the maximum value forces us to be out of bounds, simply punt.
1713 It would be pointless to try and do anything more since this
1714 all should be optimized away above us. */
1715 if ((cond_code
== LT_EXPR
1716 && compare_values (max
, min
) == 0)
1717 || (CONSTANT_CLASS_P (max
) && TREE_OVERFLOW (max
)))
1718 set_value_range_to_varying (vr_p
);
1721 /* For LT_EXPR, we create the range [MIN, MAX - 1]. */
1722 if (cond_code
== LT_EXPR
)
1724 if (TYPE_PRECISION (TREE_TYPE (max
)) == 1
1725 && !TYPE_UNSIGNED (TREE_TYPE (max
)))
1726 max
= fold_build2 (PLUS_EXPR
, TREE_TYPE (max
), max
,
1727 build_int_cst (TREE_TYPE (max
), -1));
1729 max
= fold_build2 (MINUS_EXPR
, TREE_TYPE (max
), max
,
1730 build_int_cst (TREE_TYPE (max
), 1));
1732 TREE_NO_WARNING (max
) = 1;
1735 set_value_range (vr_p
, VR_RANGE
, min
, max
, vr_p
->equiv
);
1738 else if (cond_code
== GE_EXPR
|| cond_code
== GT_EXPR
)
1740 max
= TYPE_MAX_VALUE (type
);
1742 if (limit_vr
== NULL
|| limit_vr
->type
== VR_ANTI_RANGE
)
1746 /* If LIMIT_VR is of the form [N1, N2], we need to build the
1747 range [N1, MAX] for GE_EXPR and [N1 + 1, MAX] for
1749 min
= limit_vr
->min
;
1752 /* If the minimum value forces us to be out of bounds, simply punt.
1753 It would be pointless to try and do anything more since this
1754 all should be optimized away above us. */
1755 if ((cond_code
== GT_EXPR
1756 && compare_values (min
, max
) == 0)
1757 || (CONSTANT_CLASS_P (min
) && TREE_OVERFLOW (min
)))
1758 set_value_range_to_varying (vr_p
);
1761 /* For GT_EXPR, we create the range [MIN + 1, MAX]. */
1762 if (cond_code
== GT_EXPR
)
1764 if (TYPE_PRECISION (TREE_TYPE (min
)) == 1
1765 && !TYPE_UNSIGNED (TREE_TYPE (min
)))
1766 min
= fold_build2 (MINUS_EXPR
, TREE_TYPE (min
), min
,
1767 build_int_cst (TREE_TYPE (min
), -1));
1769 min
= fold_build2 (PLUS_EXPR
, TREE_TYPE (min
), min
,
1770 build_int_cst (TREE_TYPE (min
), 1));
1772 TREE_NO_WARNING (min
) = 1;
1775 set_value_range (vr_p
, VR_RANGE
, min
, max
, vr_p
->equiv
);
1781 /* Finally intersect the new range with what we already know about var. */
1782 vrp_intersect_ranges (vr_p
, get_value_range (var
));
1786 /* Extract range information from SSA name VAR and store it in VR. If
1787 VAR has an interesting range, use it. Otherwise, create the
1788 range [VAR, VAR] and return it. This is useful in situations where
1789 we may have conditionals testing values of VARYING names. For
1796 Even if y_5 is deemed VARYING, we can determine that x_3 > y_5 is
1800 extract_range_from_ssa_name (value_range_t
*vr
, tree var
)
1802 value_range_t
*var_vr
= get_value_range (var
);
1804 if (var_vr
->type
!= VR_UNDEFINED
&& var_vr
->type
!= VR_VARYING
)
1805 copy_value_range (vr
, var_vr
);
1807 set_value_range (vr
, VR_RANGE
, var
, var
, NULL
);
1809 add_equivalence (&vr
->equiv
, var
);
1813 /* Wrapper around int_const_binop. If the operation overflows and we
1814 are not using wrapping arithmetic, then adjust the result to be
1815 -INF or +INF depending on CODE, VAL1 and VAL2. This can return
1816 NULL_TREE if we need to use an overflow infinity representation but
1817 the type does not support it. */
1820 vrp_int_const_binop (enum tree_code code
, tree val1
, tree val2
)
1824 res
= int_const_binop (code
, val1
, val2
);
1826 /* If we are using unsigned arithmetic, operate symbolically
1827 on -INF and +INF as int_const_binop only handles signed overflow. */
1828 if (TYPE_UNSIGNED (TREE_TYPE (val1
)))
1830 int checkz
= compare_values (res
, val1
);
1831 bool overflow
= false;
1833 /* Ensure that res = val1 [+*] val2 >= val1
1834 or that res = val1 - val2 <= val1. */
1835 if ((code
== PLUS_EXPR
1836 && !(checkz
== 1 || checkz
== 0))
1837 || (code
== MINUS_EXPR
1838 && !(checkz
== 0 || checkz
== -1)))
1842 /* Checking for multiplication overflow is done by dividing the
1843 output of the multiplication by the first input of the
1844 multiplication. If the result of that division operation is
1845 not equal to the second input of the multiplication, then the
1846 multiplication overflowed. */
1847 else if (code
== MULT_EXPR
&& !integer_zerop (val1
))
1849 tree tmp
= int_const_binop (TRUNC_DIV_EXPR
,
1852 int check
= compare_values (tmp
, val2
);
1860 res
= copy_node (res
);
1861 TREE_OVERFLOW (res
) = 1;
1865 else if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (val1
)))
1866 /* If the singed operation wraps then int_const_binop has done
1867 everything we want. */
1869 else if ((TREE_OVERFLOW (res
)
1870 && !TREE_OVERFLOW (val1
)
1871 && !TREE_OVERFLOW (val2
))
1872 || is_overflow_infinity (val1
)
1873 || is_overflow_infinity (val2
))
1875 /* If the operation overflowed but neither VAL1 nor VAL2 are
1876 overflown, return -INF or +INF depending on the operation
1877 and the combination of signs of the operands. */
1878 int sgn1
= tree_int_cst_sgn (val1
);
1879 int sgn2
= tree_int_cst_sgn (val2
);
1881 if (needs_overflow_infinity (TREE_TYPE (res
))
1882 && !supports_overflow_infinity (TREE_TYPE (res
)))
1885 /* We have to punt on adding infinities of different signs,
1886 since we can't tell what the sign of the result should be.
1887 Likewise for subtracting infinities of the same sign. */
1888 if (((code
== PLUS_EXPR
&& sgn1
!= sgn2
)
1889 || (code
== MINUS_EXPR
&& sgn1
== sgn2
))
1890 && is_overflow_infinity (val1
)
1891 && is_overflow_infinity (val2
))
1894 /* Don't try to handle division or shifting of infinities. */
1895 if ((code
== TRUNC_DIV_EXPR
1896 || code
== FLOOR_DIV_EXPR
1897 || code
== CEIL_DIV_EXPR
1898 || code
== EXACT_DIV_EXPR
1899 || code
== ROUND_DIV_EXPR
1900 || code
== RSHIFT_EXPR
)
1901 && (is_overflow_infinity (val1
)
1902 || is_overflow_infinity (val2
)))
1905 /* Notice that we only need to handle the restricted set of
1906 operations handled by extract_range_from_binary_expr.
1907 Among them, only multiplication, addition and subtraction
1908 can yield overflow without overflown operands because we
1909 are working with integral types only... except in the
1910 case VAL1 = -INF and VAL2 = -1 which overflows to +INF
1911 for division too. */
1913 /* For multiplication, the sign of the overflow is given
1914 by the comparison of the signs of the operands. */
1915 if ((code
== MULT_EXPR
&& sgn1
== sgn2
)
1916 /* For addition, the operands must be of the same sign
1917 to yield an overflow. Its sign is therefore that
1918 of one of the operands, for example the first. For
1919 infinite operands X + -INF is negative, not positive. */
1920 || (code
== PLUS_EXPR
1922 ? !is_negative_overflow_infinity (val2
)
1923 : is_positive_overflow_infinity (val2
)))
1924 /* For subtraction, non-infinite operands must be of
1925 different signs to yield an overflow. Its sign is
1926 therefore that of the first operand or the opposite of
1927 that of the second operand. A first operand of 0 counts
1928 as positive here, for the corner case 0 - (-INF), which
1929 overflows, but must yield +INF. For infinite operands 0
1930 - INF is negative, not positive. */
1931 || (code
== MINUS_EXPR
1933 ? !is_positive_overflow_infinity (val2
)
1934 : is_negative_overflow_infinity (val2
)))
1935 /* We only get in here with positive shift count, so the
1936 overflow direction is the same as the sign of val1.
1937 Actually rshift does not overflow at all, but we only
1938 handle the case of shifting overflowed -INF and +INF. */
1939 || (code
== RSHIFT_EXPR
1941 /* For division, the only case is -INF / -1 = +INF. */
1942 || code
== TRUNC_DIV_EXPR
1943 || code
== FLOOR_DIV_EXPR
1944 || code
== CEIL_DIV_EXPR
1945 || code
== EXACT_DIV_EXPR
1946 || code
== ROUND_DIV_EXPR
)
1947 return (needs_overflow_infinity (TREE_TYPE (res
))
1948 ? positive_overflow_infinity (TREE_TYPE (res
))
1949 : TYPE_MAX_VALUE (TREE_TYPE (res
)));
1951 return (needs_overflow_infinity (TREE_TYPE (res
))
1952 ? negative_overflow_infinity (TREE_TYPE (res
))
1953 : TYPE_MIN_VALUE (TREE_TYPE (res
)));
1960 /* For range VR compute two double_int bitmasks. In *MAY_BE_NONZERO
1961 bitmask if some bit is unset, it means for all numbers in the range
1962 the bit is 0, otherwise it might be 0 or 1. In *MUST_BE_NONZERO
1963 bitmask if some bit is set, it means for all numbers in the range
1964 the bit is 1, otherwise it might be 0 or 1. */
1967 zero_nonzero_bits_from_vr (value_range_t
*vr
,
1968 double_int
*may_be_nonzero
,
1969 double_int
*must_be_nonzero
)
1971 *may_be_nonzero
= double_int_minus_one
;
1972 *must_be_nonzero
= double_int_zero
;
1973 if (!range_int_cst_p (vr
)
1974 || TREE_OVERFLOW (vr
->min
)
1975 || TREE_OVERFLOW (vr
->max
))
1978 if (range_int_cst_singleton_p (vr
))
1980 *may_be_nonzero
= tree_to_double_int (vr
->min
);
1981 *must_be_nonzero
= *may_be_nonzero
;
1983 else if (tree_int_cst_sgn (vr
->min
) >= 0
1984 || tree_int_cst_sgn (vr
->max
) < 0)
1986 double_int dmin
= tree_to_double_int (vr
->min
);
1987 double_int dmax
= tree_to_double_int (vr
->max
);
1988 double_int xor_mask
= double_int_xor (dmin
, dmax
);
1989 *may_be_nonzero
= double_int_ior (dmin
, dmax
);
1990 *must_be_nonzero
= double_int_and (dmin
, dmax
);
1991 if (xor_mask
.high
!= 0)
1993 unsigned HOST_WIDE_INT mask
1994 = ((unsigned HOST_WIDE_INT
) 1
1995 << floor_log2 (xor_mask
.high
)) - 1;
1996 may_be_nonzero
->low
= ALL_ONES
;
1997 may_be_nonzero
->high
|= mask
;
1998 must_be_nonzero
->low
= 0;
1999 must_be_nonzero
->high
&= ~mask
;
2001 else if (xor_mask
.low
!= 0)
2003 unsigned HOST_WIDE_INT mask
2004 = ((unsigned HOST_WIDE_INT
) 1
2005 << floor_log2 (xor_mask
.low
)) - 1;
2006 may_be_nonzero
->low
|= mask
;
2007 must_be_nonzero
->low
&= ~mask
;
2014 /* Create two value-ranges in *VR0 and *VR1 from the anti-range *AR
2015 so that *VR0 U *VR1 == *AR. Returns true if that is possible,
2016 false otherwise. If *AR can be represented with a single range
2017 *VR1 will be VR_UNDEFINED. */
2020 ranges_from_anti_range (value_range_t
*ar
,
2021 value_range_t
*vr0
, value_range_t
*vr1
)
2023 tree type
= TREE_TYPE (ar
->min
);
2025 vr0
->type
= VR_UNDEFINED
;
2026 vr1
->type
= VR_UNDEFINED
;
2028 if (ar
->type
!= VR_ANTI_RANGE
2029 || TREE_CODE (ar
->min
) != INTEGER_CST
2030 || TREE_CODE (ar
->max
) != INTEGER_CST
2031 || !vrp_val_min (type
)
2032 || !vrp_val_max (type
))
2035 if (!vrp_val_is_min (ar
->min
))
2037 vr0
->type
= VR_RANGE
;
2038 vr0
->min
= vrp_val_min (type
);
2040 = double_int_to_tree (type
,
2041 double_int_sub (tree_to_double_int (ar
->min
),
2044 if (!vrp_val_is_max (ar
->max
))
2046 vr1
->type
= VR_RANGE
;
2048 = double_int_to_tree (type
,
2049 double_int_add (tree_to_double_int (ar
->max
),
2051 vr1
->max
= vrp_val_max (type
);
2053 if (vr0
->type
== VR_UNDEFINED
)
2056 vr1
->type
= VR_UNDEFINED
;
2059 return vr0
->type
!= VR_UNDEFINED
;
2062 /* Helper to extract a value-range *VR for a multiplicative operation
2066 extract_range_from_multiplicative_op_1 (value_range_t
*vr
,
2067 enum tree_code code
,
2068 value_range_t
*vr0
, value_range_t
*vr1
)
2070 enum value_range_type type
;
2077 /* Multiplications, divisions and shifts are a bit tricky to handle,
2078 depending on the mix of signs we have in the two ranges, we
2079 need to operate on different values to get the minimum and
2080 maximum values for the new range. One approach is to figure
2081 out all the variations of range combinations and do the
2084 However, this involves several calls to compare_values and it
2085 is pretty convoluted. It's simpler to do the 4 operations
2086 (MIN0 OP MIN1, MIN0 OP MAX1, MAX0 OP MIN1 and MAX0 OP MAX0 OP
2087 MAX1) and then figure the smallest and largest values to form
2089 gcc_assert (code
== MULT_EXPR
2090 || code
== TRUNC_DIV_EXPR
2091 || code
== FLOOR_DIV_EXPR
2092 || code
== CEIL_DIV_EXPR
2093 || code
== EXACT_DIV_EXPR
2094 || code
== ROUND_DIV_EXPR
2095 || code
== RSHIFT_EXPR
);
2096 gcc_assert ((vr0
->type
== VR_RANGE
2097 || (code
== MULT_EXPR
&& vr0
->type
== VR_ANTI_RANGE
))
2098 && vr0
->type
== vr1
->type
);
2102 /* Compute the 4 cross operations. */
2104 val
[0] = vrp_int_const_binop (code
, vr0
->min
, vr1
->min
);
2105 if (val
[0] == NULL_TREE
)
2108 if (vr1
->max
== vr1
->min
)
2112 val
[1] = vrp_int_const_binop (code
, vr0
->min
, vr1
->max
);
2113 if (val
[1] == NULL_TREE
)
2117 if (vr0
->max
== vr0
->min
)
2121 val
[2] = vrp_int_const_binop (code
, vr0
->max
, vr1
->min
);
2122 if (val
[2] == NULL_TREE
)
2126 if (vr0
->min
== vr0
->max
|| vr1
->min
== vr1
->max
)
2130 val
[3] = vrp_int_const_binop (code
, vr0
->max
, vr1
->max
);
2131 if (val
[3] == NULL_TREE
)
2137 set_value_range_to_varying (vr
);
2141 /* Set MIN to the minimum of VAL[i] and MAX to the maximum
2145 for (i
= 1; i
< 4; i
++)
2147 if (!is_gimple_min_invariant (min
)
2148 || (TREE_OVERFLOW (min
) && !is_overflow_infinity (min
))
2149 || !is_gimple_min_invariant (max
)
2150 || (TREE_OVERFLOW (max
) && !is_overflow_infinity (max
)))
2155 if (!is_gimple_min_invariant (val
[i
])
2156 || (TREE_OVERFLOW (val
[i
])
2157 && !is_overflow_infinity (val
[i
])))
2159 /* If we found an overflowed value, set MIN and MAX
2160 to it so that we set the resulting range to
2166 if (compare_values (val
[i
], min
) == -1)
2169 if (compare_values (val
[i
], max
) == 1)
2174 /* If either MIN or MAX overflowed, then set the resulting range to
2175 VARYING. But we do accept an overflow infinity
2177 if (min
== NULL_TREE
2178 || !is_gimple_min_invariant (min
)
2179 || (TREE_OVERFLOW (min
) && !is_overflow_infinity (min
))
2181 || !is_gimple_min_invariant (max
)
2182 || (TREE_OVERFLOW (max
) && !is_overflow_infinity (max
)))
2184 set_value_range_to_varying (vr
);
2190 2) [-INF, +-INF(OVF)]
2191 3) [+-INF(OVF), +INF]
2192 4) [+-INF(OVF), +-INF(OVF)]
2193 We learn nothing when we have INF and INF(OVF) on both sides.
2194 Note that we do accept [-INF, -INF] and [+INF, +INF] without
2196 if ((vrp_val_is_min (min
) || is_overflow_infinity (min
))
2197 && (vrp_val_is_max (max
) || is_overflow_infinity (max
)))
2199 set_value_range_to_varying (vr
);
2203 cmp
= compare_values (min
, max
);
2204 if (cmp
== -2 || cmp
== 1)
2206 /* If the new range has its limits swapped around (MIN > MAX),
2207 then the operation caused one of them to wrap around, mark
2208 the new range VARYING. */
2209 set_value_range_to_varying (vr
);
2212 set_value_range (vr
, type
, min
, max
, NULL
);
2215 /* Extract range information from a binary operation CODE based on
2216 the ranges of each of its operands, *VR0 and *VR1 with resulting
2217 type EXPR_TYPE. The resulting range is stored in *VR. */
2220 extract_range_from_binary_expr_1 (value_range_t
*vr
,
2221 enum tree_code code
, tree expr_type
,
2222 value_range_t
*vr0_
, value_range_t
*vr1_
)
2224 value_range_t vr0
= *vr0_
, vr1
= *vr1_
;
2225 value_range_t vrtem0
= VR_INITIALIZER
, vrtem1
= VR_INITIALIZER
;
2226 enum value_range_type type
;
2227 tree min
= NULL_TREE
, max
= NULL_TREE
;
2230 if (!INTEGRAL_TYPE_P (expr_type
)
2231 && !POINTER_TYPE_P (expr_type
))
2233 set_value_range_to_varying (vr
);
2237 /* Not all binary expressions can be applied to ranges in a
2238 meaningful way. Handle only arithmetic operations. */
2239 if (code
!= PLUS_EXPR
2240 && code
!= MINUS_EXPR
2241 && code
!= POINTER_PLUS_EXPR
2242 && code
!= MULT_EXPR
2243 && code
!= TRUNC_DIV_EXPR
2244 && code
!= FLOOR_DIV_EXPR
2245 && code
!= CEIL_DIV_EXPR
2246 && code
!= EXACT_DIV_EXPR
2247 && code
!= ROUND_DIV_EXPR
2248 && code
!= TRUNC_MOD_EXPR
2249 && code
!= RSHIFT_EXPR
2250 && code
!= LSHIFT_EXPR
2253 && code
!= BIT_AND_EXPR
2254 && code
!= BIT_IOR_EXPR
2255 && code
!= BIT_XOR_EXPR
)
2257 set_value_range_to_varying (vr
);
2261 /* If both ranges are UNDEFINED, so is the result. */
2262 if (vr0
.type
== VR_UNDEFINED
&& vr1
.type
== VR_UNDEFINED
)
2264 set_value_range_to_undefined (vr
);
2267 /* If one of the ranges is UNDEFINED drop it to VARYING for the following
2268 code. At some point we may want to special-case operations that
2269 have UNDEFINED result for all or some value-ranges of the not UNDEFINED
2271 else if (vr0
.type
== VR_UNDEFINED
)
2272 set_value_range_to_varying (&vr0
);
2273 else if (vr1
.type
== VR_UNDEFINED
)
2274 set_value_range_to_varying (&vr1
);
2276 /* Now canonicalize anti-ranges to ranges when they are not symbolic
2277 and express ~[] op X as ([]' op X) U ([]'' op X). */
2278 if (vr0
.type
== VR_ANTI_RANGE
2279 && ranges_from_anti_range (&vr0
, &vrtem0
, &vrtem1
))
2281 extract_range_from_binary_expr_1 (vr
, code
, expr_type
, &vrtem0
, vr1_
);
2282 if (vrtem1
.type
!= VR_UNDEFINED
)
2284 value_range_t vrres
= VR_INITIALIZER
;
2285 extract_range_from_binary_expr_1 (&vrres
, code
, expr_type
,
2287 vrp_meet (vr
, &vrres
);
2291 /* Likewise for X op ~[]. */
2292 if (vr1
.type
== VR_ANTI_RANGE
2293 && ranges_from_anti_range (&vr1
, &vrtem0
, &vrtem1
))
2295 extract_range_from_binary_expr_1 (vr
, code
, expr_type
, vr0_
, &vrtem0
);
2296 if (vrtem1
.type
!= VR_UNDEFINED
)
2298 value_range_t vrres
= VR_INITIALIZER
;
2299 extract_range_from_binary_expr_1 (&vrres
, code
, expr_type
,
2301 vrp_meet (vr
, &vrres
);
2306 /* The type of the resulting value range defaults to VR0.TYPE. */
2309 /* Refuse to operate on VARYING ranges, ranges of different kinds
2310 and symbolic ranges. As an exception, we allow BIT_AND_EXPR
2311 because we may be able to derive a useful range even if one of
2312 the operands is VR_VARYING or symbolic range. Similarly for
2313 divisions. TODO, we may be able to derive anti-ranges in
2315 if (code
!= BIT_AND_EXPR
2316 && code
!= BIT_IOR_EXPR
2317 && code
!= TRUNC_DIV_EXPR
2318 && code
!= FLOOR_DIV_EXPR
2319 && code
!= CEIL_DIV_EXPR
2320 && code
!= EXACT_DIV_EXPR
2321 && code
!= ROUND_DIV_EXPR
2322 && code
!= TRUNC_MOD_EXPR
2323 && (vr0
.type
== VR_VARYING
2324 || vr1
.type
== VR_VARYING
2325 || vr0
.type
!= vr1
.type
2326 || symbolic_range_p (&vr0
)
2327 || symbolic_range_p (&vr1
)))
2329 set_value_range_to_varying (vr
);
2333 /* Now evaluate the expression to determine the new range. */
2334 if (POINTER_TYPE_P (expr_type
))
2336 if (code
== MIN_EXPR
|| code
== MAX_EXPR
)
2338 /* For MIN/MAX expressions with pointers, we only care about
2339 nullness, if both are non null, then the result is nonnull.
2340 If both are null, then the result is null. Otherwise they
2342 if (range_is_nonnull (&vr0
) && range_is_nonnull (&vr1
))
2343 set_value_range_to_nonnull (vr
, expr_type
);
2344 else if (range_is_null (&vr0
) && range_is_null (&vr1
))
2345 set_value_range_to_null (vr
, expr_type
);
2347 set_value_range_to_varying (vr
);
2349 else if (code
== POINTER_PLUS_EXPR
)
2351 /* For pointer types, we are really only interested in asserting
2352 whether the expression evaluates to non-NULL. */
2353 if (range_is_nonnull (&vr0
) || range_is_nonnull (&vr1
))
2354 set_value_range_to_nonnull (vr
, expr_type
);
2355 else if (range_is_null (&vr0
) && range_is_null (&vr1
))
2356 set_value_range_to_null (vr
, expr_type
);
2358 set_value_range_to_varying (vr
);
2360 else if (code
== BIT_AND_EXPR
)
2362 /* For pointer types, we are really only interested in asserting
2363 whether the expression evaluates to non-NULL. */
2364 if (range_is_nonnull (&vr0
) && range_is_nonnull (&vr1
))
2365 set_value_range_to_nonnull (vr
, expr_type
);
2366 else if (range_is_null (&vr0
) || range_is_null (&vr1
))
2367 set_value_range_to_null (vr
, expr_type
);
2369 set_value_range_to_varying (vr
);
2372 set_value_range_to_varying (vr
);
2377 /* For integer ranges, apply the operation to each end of the
2378 range and see what we end up with. */
2379 if (code
== PLUS_EXPR
)
2381 /* If we have a PLUS_EXPR with two VR_RANGE integer constant
2382 ranges compute the precise range for such case if possible. */
2383 if (range_int_cst_p (&vr0
)
2384 && range_int_cst_p (&vr1
)
2385 /* We attempt to do infinite precision signed integer arithmetic,
2386 thus we need two more bits than the possibly unsigned inputs. */
2387 && TYPE_PRECISION (expr_type
) < HOST_BITS_PER_DOUBLE_INT
- 1)
2389 double_int min0
= tree_to_double_int (vr0
.min
);
2390 double_int max0
= tree_to_double_int (vr0
.max
);
2391 double_int min1
= tree_to_double_int (vr1
.min
);
2392 double_int max1
= tree_to_double_int (vr1
.max
);
2393 bool uns
= TYPE_UNSIGNED (expr_type
);
2395 = double_int_min_value (TYPE_PRECISION (expr_type
), uns
);
2397 = double_int_max_value (TYPE_PRECISION (expr_type
), uns
);
2398 double_int dmin
, dmax
;
2400 dmin
= double_int_add (min0
, min1
);
2401 dmax
= double_int_add (max0
, max1
);
2403 if (TYPE_OVERFLOW_WRAPS (expr_type
))
2405 /* If overflow wraps, truncate the values and adjust the
2406 range kind and bounds appropriately. */
2408 = double_int_ext (dmin
, TYPE_PRECISION (expr_type
), uns
);
2410 = double_int_ext (dmax
, TYPE_PRECISION (expr_type
), uns
);
2411 gcc_assert (double_int_scmp (dmin
, dmax
) <= 0);
2412 if ((double_int_scmp (dmin
, type_min
) == -1
2413 && double_int_scmp (dmax
, type_min
) == -1)
2414 || (double_int_scmp (dmin
, type_max
) == 1
2415 && double_int_scmp (dmax
, type_max
) == 1)
2416 || (double_int_scmp (type_min
, dmin
) <= 0
2417 && double_int_scmp (dmax
, type_max
) <= 0))
2419 /* No overflow or both overflow or underflow. The
2420 range kind stays VR_RANGE. */
2421 min
= double_int_to_tree (expr_type
, tmin
);
2422 max
= double_int_to_tree (expr_type
, tmax
);
2424 else if (double_int_scmp (dmin
, type_min
) == -1
2425 && double_int_scmp (dmax
, type_max
) == 1)
2427 /* Underflow and overflow, drop to VR_VARYING. */
2428 set_value_range_to_varying (vr
);
2433 /* Min underflow or max overflow. The range kind
2434 changes to VR_ANTI_RANGE. */
2435 double_int tem
= tmin
;
2436 gcc_assert ((double_int_scmp (dmin
, type_min
) == -1
2437 && double_int_scmp (dmax
, type_min
) >= 0
2438 && double_int_scmp (dmax
, type_max
) <= 0)
2439 || (double_int_scmp (dmax
, type_max
) == 1
2440 && double_int_scmp (dmin
, type_min
) >= 0
2441 && double_int_scmp (dmin
, type_max
) <= 0));
2442 type
= VR_ANTI_RANGE
;
2443 tmin
= double_int_add (tmax
, double_int_one
);
2444 tmax
= double_int_add (tem
, double_int_minus_one
);
2445 /* If the anti-range would cover nothing, drop to varying.
2446 Likewise if the anti-range bounds are outside of the
2448 if (double_int_cmp (tmin
, tmax
, uns
) > 0
2449 || double_int_cmp (tmin
, type_min
, uns
) < 0
2450 || double_int_cmp (tmax
, type_max
, uns
) > 0)
2452 set_value_range_to_varying (vr
);
2455 min
= double_int_to_tree (expr_type
, tmin
);
2456 max
= double_int_to_tree (expr_type
, tmax
);
2461 /* For non-wrapping arithmetic look at possibly smaller
2462 value-ranges of the type. */
2463 if (vrp_val_min (expr_type
))
2464 type_min
= tree_to_double_int (vrp_val_min (expr_type
));
2465 if (vrp_val_max (expr_type
))
2466 type_max
= tree_to_double_int (vrp_val_max (expr_type
));
2468 /* If overflow does not wrap, saturate to the types min/max
2470 if (double_int_scmp (dmin
, type_min
) == -1)
2472 if (needs_overflow_infinity (expr_type
)
2473 && supports_overflow_infinity (expr_type
))
2474 min
= negative_overflow_infinity (expr_type
);
2476 min
= double_int_to_tree (expr_type
, type_min
);
2478 else if (double_int_scmp (dmin
, type_max
) == 1)
2480 if (needs_overflow_infinity (expr_type
)
2481 && supports_overflow_infinity (expr_type
))
2482 min
= positive_overflow_infinity (expr_type
);
2484 min
= double_int_to_tree (expr_type
, type_max
);
2487 min
= double_int_to_tree (expr_type
, dmin
);
2489 if (double_int_scmp (dmax
, type_min
) == -1)
2491 if (needs_overflow_infinity (expr_type
)
2492 && supports_overflow_infinity (expr_type
))
2493 max
= negative_overflow_infinity (expr_type
);
2495 max
= double_int_to_tree (expr_type
, type_min
);
2497 else if (double_int_scmp (dmax
, type_max
) == 1)
2499 if (needs_overflow_infinity (expr_type
)
2500 && supports_overflow_infinity (expr_type
))
2501 max
= positive_overflow_infinity (expr_type
);
2503 max
= double_int_to_tree (expr_type
, type_max
);
2506 max
= double_int_to_tree (expr_type
, dmax
);
2508 if (needs_overflow_infinity (expr_type
)
2509 && supports_overflow_infinity (expr_type
))
2511 if (is_negative_overflow_infinity (vr0
.min
)
2512 || is_negative_overflow_infinity (vr1
.min
))
2513 min
= negative_overflow_infinity (expr_type
);
2514 if (is_positive_overflow_infinity (vr0
.max
)
2515 || is_positive_overflow_infinity (vr1
.max
))
2516 max
= positive_overflow_infinity (expr_type
);
2521 /* For other cases, for example if we have a PLUS_EXPR with two
2522 VR_ANTI_RANGEs, drop to VR_VARYING. It would take more effort
2523 to compute a precise range for such a case.
2524 ??? General even mixed range kind operations can be expressed
2525 by for example transforming ~[3, 5] + [1, 2] to range-only
2526 operations and a union primitive:
2527 [-INF, 2] + [1, 2] U [5, +INF] + [1, 2]
2528 [-INF+1, 4] U [6, +INF(OVF)]
2529 though usually the union is not exactly representable with
2530 a single range or anti-range as the above is
2531 [-INF+1, +INF(OVF)] intersected with ~[5, 5]
2532 but one could use a scheme similar to equivalences for this. */
2533 set_value_range_to_varying (vr
);
2537 else if (code
== MIN_EXPR
2538 || code
== MAX_EXPR
)
2540 if (vr0
.type
== VR_ANTI_RANGE
)
2542 /* For MIN_EXPR and MAX_EXPR with two VR_ANTI_RANGEs,
2543 the resulting VR_ANTI_RANGE is the same - intersection
2544 of the two ranges. */
2545 min
= vrp_int_const_binop (MAX_EXPR
, vr0
.min
, vr1
.min
);
2546 max
= vrp_int_const_binop (MIN_EXPR
, vr0
.max
, vr1
.max
);
2550 /* For operations that make the resulting range directly
2551 proportional to the original ranges, apply the operation to
2552 the same end of each range. */
2553 min
= vrp_int_const_binop (code
, vr0
.min
, vr1
.min
);
2554 max
= vrp_int_const_binop (code
, vr0
.max
, vr1
.max
);
2557 else if (code
== MULT_EXPR
)
2559 /* If we have an unsigned MULT_EXPR with two VR_ANTI_RANGEs,
2560 drop to VR_VARYING. It would take more effort to compute a
2561 precise range for such a case. For example, if we have
2562 op0 == 65536 and op1 == 65536 with their ranges both being
2563 ~[0,0] on a 32-bit machine, we would have op0 * op1 == 0, so
2564 we cannot claim that the product is in ~[0,0]. Note that we
2565 are guaranteed to have vr0.type == vr1.type at this
2567 if (vr0
.type
== VR_ANTI_RANGE
2568 && !TYPE_OVERFLOW_UNDEFINED (expr_type
))
2570 set_value_range_to_varying (vr
);
2574 extract_range_from_multiplicative_op_1 (vr
, code
, &vr0
, &vr1
);
2577 else if (code
== RSHIFT_EXPR
)
2579 /* If we have a RSHIFT_EXPR with any shift values outside [0..prec-1],
2580 then drop to VR_VARYING. Outside of this range we get undefined
2581 behavior from the shift operation. We cannot even trust
2582 SHIFT_COUNT_TRUNCATED at this stage, because that applies to rtl
2583 shifts, and the operation at the tree level may be widened. */
2584 if (vr1
.type
!= VR_RANGE
2585 || !value_range_nonnegative_p (&vr1
)
2586 || TREE_CODE (vr1
.max
) != INTEGER_CST
2587 || compare_tree_int (vr1
.max
, TYPE_PRECISION (expr_type
) - 1) == 1)
2589 set_value_range_to_varying (vr
);
2593 extract_range_from_multiplicative_op_1 (vr
, code
, &vr0
, &vr1
);
2596 else if (code
== LSHIFT_EXPR
)
2598 /* If we have a LSHIFT_EXPR with any shift values outside [0..prec-1],
2599 then drop to VR_VARYING. Outside of this range we get undefined
2600 behavior from the shift operation. We cannot even trust
2601 SHIFT_COUNT_TRUNCATED at this stage, because that applies to rtl
2602 shifts, and the operation at the tree level may be widened. */
2603 if (vr1
.type
!= VR_RANGE
2604 || !value_range_nonnegative_p (&vr1
)
2605 || TREE_CODE (vr1
.max
) != INTEGER_CST
2606 || compare_tree_int (vr1
.max
, TYPE_PRECISION (expr_type
) - 1) == 1)
2608 set_value_range_to_varying (vr
);
2612 /* We can map shifts by constants to MULT_EXPR handling. */
2613 if (range_int_cst_singleton_p (&vr1
))
2615 value_range_t vr1p
= VR_INITIALIZER
;
2616 vr1p
.type
= VR_RANGE
;
2618 = double_int_to_tree (expr_type
,
2619 double_int_lshift (double_int_one
,
2620 TREE_INT_CST_LOW (vr1
.min
),
2621 TYPE_PRECISION (expr_type
),
2623 vr1p
.max
= vr1p
.min
;
2624 extract_range_from_multiplicative_op_1 (vr
, MULT_EXPR
, &vr0
, &vr1p
);
2628 set_value_range_to_varying (vr
);
2631 else if (code
== TRUNC_DIV_EXPR
2632 || code
== FLOOR_DIV_EXPR
2633 || code
== CEIL_DIV_EXPR
2634 || code
== EXACT_DIV_EXPR
2635 || code
== ROUND_DIV_EXPR
)
2637 if (vr0
.type
!= VR_RANGE
|| symbolic_range_p (&vr0
))
2639 /* For division, if op1 has VR_RANGE but op0 does not, something
2640 can be deduced just from that range. Say [min, max] / [4, max]
2641 gives [min / 4, max / 4] range. */
2642 if (vr1
.type
== VR_RANGE
2643 && !symbolic_range_p (&vr1
)
2644 && !range_includes_zero_p (&vr1
))
2646 vr0
.type
= type
= VR_RANGE
;
2647 vr0
.min
= vrp_val_min (expr_type
);
2648 vr0
.max
= vrp_val_max (expr_type
);
2652 set_value_range_to_varying (vr
);
2657 /* For divisions, if flag_non_call_exceptions is true, we must
2658 not eliminate a division by zero. */
2659 if (cfun
->can_throw_non_call_exceptions
2660 && (vr1
.type
!= VR_RANGE
2661 || symbolic_range_p (&vr1
)
2662 || range_includes_zero_p (&vr1
)))
2664 set_value_range_to_varying (vr
);
2668 /* For divisions, if op0 is VR_RANGE, we can deduce a range
2669 even if op1 is VR_VARYING, VR_ANTI_RANGE, symbolic or can
2671 if (vr0
.type
== VR_RANGE
2672 && (vr1
.type
!= VR_RANGE
2673 || symbolic_range_p (&vr1
)
2674 || range_includes_zero_p (&vr1
)))
2676 tree zero
= build_int_cst (TREE_TYPE (vr0
.min
), 0);
2681 if (TYPE_UNSIGNED (expr_type
)
2682 || value_range_nonnegative_p (&vr1
))
2684 /* For unsigned division or when divisor is known
2685 to be non-negative, the range has to cover
2686 all numbers from 0 to max for positive max
2687 and all numbers from min to 0 for negative min. */
2688 cmp
= compare_values (vr0
.max
, zero
);
2691 else if (cmp
== 0 || cmp
== 1)
2695 cmp
= compare_values (vr0
.min
, zero
);
2698 else if (cmp
== 0 || cmp
== -1)
2705 /* Otherwise the range is -max .. max or min .. -min
2706 depending on which bound is bigger in absolute value,
2707 as the division can change the sign. */
2708 abs_extent_range (vr
, vr0
.min
, vr0
.max
);
2711 if (type
== VR_VARYING
)
2713 set_value_range_to_varying (vr
);
2719 extract_range_from_multiplicative_op_1 (vr
, code
, &vr0
, &vr1
);
2723 else if (code
== TRUNC_MOD_EXPR
)
2725 if (vr1
.type
!= VR_RANGE
2726 || symbolic_range_p (&vr1
)
2727 || range_includes_zero_p (&vr1
)
2728 || vrp_val_is_min (vr1
.min
))
2730 set_value_range_to_varying (vr
);
2734 /* Compute MAX <|vr1.min|, |vr1.max|> - 1. */
2735 max
= fold_unary_to_constant (ABS_EXPR
, expr_type
, vr1
.min
);
2736 if (tree_int_cst_lt (max
, vr1
.max
))
2738 max
= int_const_binop (MINUS_EXPR
, max
, integer_one_node
);
2739 /* If the dividend is non-negative the modulus will be
2740 non-negative as well. */
2741 if (TYPE_UNSIGNED (expr_type
)
2742 || value_range_nonnegative_p (&vr0
))
2743 min
= build_int_cst (TREE_TYPE (max
), 0);
2745 min
= fold_unary_to_constant (NEGATE_EXPR
, expr_type
, max
);
2747 else if (code
== MINUS_EXPR
)
2749 /* If we have a MINUS_EXPR with two VR_ANTI_RANGEs, drop to
2750 VR_VARYING. It would take more effort to compute a precise
2751 range for such a case. For example, if we have op0 == 1 and
2752 op1 == 1 with their ranges both being ~[0,0], we would have
2753 op0 - op1 == 0, so we cannot claim that the difference is in
2754 ~[0,0]. Note that we are guaranteed to have
2755 vr0.type == vr1.type at this point. */
2756 if (vr0
.type
== VR_ANTI_RANGE
)
2758 set_value_range_to_varying (vr
);
2762 /* For MINUS_EXPR, apply the operation to the opposite ends of
2764 min
= vrp_int_const_binop (code
, vr0
.min
, vr1
.max
);
2765 max
= vrp_int_const_binop (code
, vr0
.max
, vr1
.min
);
2767 else if (code
== BIT_AND_EXPR
|| code
== BIT_IOR_EXPR
|| code
== BIT_XOR_EXPR
)
2769 bool int_cst_range0
, int_cst_range1
;
2770 double_int may_be_nonzero0
, may_be_nonzero1
;
2771 double_int must_be_nonzero0
, must_be_nonzero1
;
2773 int_cst_range0
= zero_nonzero_bits_from_vr (&vr0
, &may_be_nonzero0
,
2775 int_cst_range1
= zero_nonzero_bits_from_vr (&vr1
, &may_be_nonzero1
,
2779 if (code
== BIT_AND_EXPR
)
2782 min
= double_int_to_tree (expr_type
,
2783 double_int_and (must_be_nonzero0
,
2785 dmax
= double_int_and (may_be_nonzero0
, may_be_nonzero1
);
2786 /* If both input ranges contain only negative values we can
2787 truncate the result range maximum to the minimum of the
2788 input range maxima. */
2789 if (int_cst_range0
&& int_cst_range1
2790 && tree_int_cst_sgn (vr0
.max
) < 0
2791 && tree_int_cst_sgn (vr1
.max
) < 0)
2793 dmax
= double_int_min (dmax
, tree_to_double_int (vr0
.max
),
2794 TYPE_UNSIGNED (expr_type
));
2795 dmax
= double_int_min (dmax
, tree_to_double_int (vr1
.max
),
2796 TYPE_UNSIGNED (expr_type
));
2798 /* If either input range contains only non-negative values
2799 we can truncate the result range maximum to the respective
2800 maximum of the input range. */
2801 if (int_cst_range0
&& tree_int_cst_sgn (vr0
.min
) >= 0)
2802 dmax
= double_int_min (dmax
, tree_to_double_int (vr0
.max
),
2803 TYPE_UNSIGNED (expr_type
));
2804 if (int_cst_range1
&& tree_int_cst_sgn (vr1
.min
) >= 0)
2805 dmax
= double_int_min (dmax
, tree_to_double_int (vr1
.max
),
2806 TYPE_UNSIGNED (expr_type
));
2807 max
= double_int_to_tree (expr_type
, dmax
);
2809 else if (code
== BIT_IOR_EXPR
)
2812 max
= double_int_to_tree (expr_type
,
2813 double_int_ior (may_be_nonzero0
,
2815 dmin
= double_int_ior (must_be_nonzero0
, must_be_nonzero1
);
2816 /* If the input ranges contain only positive values we can
2817 truncate the minimum of the result range to the maximum
2818 of the input range minima. */
2819 if (int_cst_range0
&& int_cst_range1
2820 && tree_int_cst_sgn (vr0
.min
) >= 0
2821 && tree_int_cst_sgn (vr1
.min
) >= 0)
2823 dmin
= double_int_max (dmin
, tree_to_double_int (vr0
.min
),
2824 TYPE_UNSIGNED (expr_type
));
2825 dmin
= double_int_max (dmin
, tree_to_double_int (vr1
.min
),
2826 TYPE_UNSIGNED (expr_type
));
2828 /* If either input range contains only negative values
2829 we can truncate the minimum of the result range to the
2830 respective minimum range. */
2831 if (int_cst_range0
&& tree_int_cst_sgn (vr0
.max
) < 0)
2832 dmin
= double_int_max (dmin
, tree_to_double_int (vr0
.min
),
2833 TYPE_UNSIGNED (expr_type
));
2834 if (int_cst_range1
&& tree_int_cst_sgn (vr1
.max
) < 0)
2835 dmin
= double_int_max (dmin
, tree_to_double_int (vr1
.min
),
2836 TYPE_UNSIGNED (expr_type
));
2837 min
= double_int_to_tree (expr_type
, dmin
);
2839 else if (code
== BIT_XOR_EXPR
)
2841 double_int result_zero_bits
, result_one_bits
;
2843 = double_int_ior (double_int_and (must_be_nonzero0
,
2846 (double_int_ior (may_be_nonzero0
,
2849 = double_int_ior (double_int_and
2851 double_int_not (may_be_nonzero1
)),
2854 double_int_not (may_be_nonzero0
)));
2855 max
= double_int_to_tree (expr_type
,
2856 double_int_not (result_zero_bits
));
2857 min
= double_int_to_tree (expr_type
, result_one_bits
);
2858 /* If the range has all positive or all negative values the
2859 result is better than VARYING. */
2860 if (tree_int_cst_sgn (min
) < 0
2861 || tree_int_cst_sgn (max
) >= 0)
2864 max
= min
= NULL_TREE
;
2870 /* If either MIN or MAX overflowed, then set the resulting range to
2871 VARYING. But we do accept an overflow infinity
2873 if (min
== NULL_TREE
2874 || !is_gimple_min_invariant (min
)
2875 || (TREE_OVERFLOW (min
) && !is_overflow_infinity (min
))
2877 || !is_gimple_min_invariant (max
)
2878 || (TREE_OVERFLOW (max
) && !is_overflow_infinity (max
)))
2880 set_value_range_to_varying (vr
);
2886 2) [-INF, +-INF(OVF)]
2887 3) [+-INF(OVF), +INF]
2888 4) [+-INF(OVF), +-INF(OVF)]
2889 We learn nothing when we have INF and INF(OVF) on both sides.
2890 Note that we do accept [-INF, -INF] and [+INF, +INF] without
2892 if ((vrp_val_is_min (min
) || is_overflow_infinity (min
))
2893 && (vrp_val_is_max (max
) || is_overflow_infinity (max
)))
2895 set_value_range_to_varying (vr
);
2899 cmp
= compare_values (min
, max
);
2900 if (cmp
== -2 || cmp
== 1)
2902 /* If the new range has its limits swapped around (MIN > MAX),
2903 then the operation caused one of them to wrap around, mark
2904 the new range VARYING. */
2905 set_value_range_to_varying (vr
);
2908 set_value_range (vr
, type
, min
, max
, NULL
);
2911 /* Extract range information from a binary expression OP0 CODE OP1 based on
2912 the ranges of each of its operands with resulting type EXPR_TYPE.
2913 The resulting range is stored in *VR. */
2916 extract_range_from_binary_expr (value_range_t
*vr
,
2917 enum tree_code code
,
2918 tree expr_type
, tree op0
, tree op1
)
2920 value_range_t vr0
= VR_INITIALIZER
;
2921 value_range_t vr1
= VR_INITIALIZER
;
2923 /* Get value ranges for each operand. For constant operands, create
2924 a new value range with the operand to simplify processing. */
2925 if (TREE_CODE (op0
) == SSA_NAME
)
2926 vr0
= *(get_value_range (op0
));
2927 else if (is_gimple_min_invariant (op0
))
2928 set_value_range_to_value (&vr0
, op0
, NULL
);
2930 set_value_range_to_varying (&vr0
);
2932 if (TREE_CODE (op1
) == SSA_NAME
)
2933 vr1
= *(get_value_range (op1
));
2934 else if (is_gimple_min_invariant (op1
))
2935 set_value_range_to_value (&vr1
, op1
, NULL
);
2937 set_value_range_to_varying (&vr1
);
2939 extract_range_from_binary_expr_1 (vr
, code
, expr_type
, &vr0
, &vr1
);
2942 /* Extract range information from a unary operation CODE based on
2943 the range of its operand *VR0 with type OP0_TYPE with resulting type TYPE.
2944 The The resulting range is stored in *VR. */
2947 extract_range_from_unary_expr_1 (value_range_t
*vr
,
2948 enum tree_code code
, tree type
,
2949 value_range_t
*vr0_
, tree op0_type
)
2951 value_range_t vr0
= *vr0_
, vrtem0
= VR_INITIALIZER
, vrtem1
= VR_INITIALIZER
;
2953 /* VRP only operates on integral and pointer types. */
2954 if (!(INTEGRAL_TYPE_P (op0_type
)
2955 || POINTER_TYPE_P (op0_type
))
2956 || !(INTEGRAL_TYPE_P (type
)
2957 || POINTER_TYPE_P (type
)))
2959 set_value_range_to_varying (vr
);
2963 /* If VR0 is UNDEFINED, so is the result. */
2964 if (vr0
.type
== VR_UNDEFINED
)
2966 set_value_range_to_undefined (vr
);
2970 /* Handle operations that we express in terms of others. */
2971 if (code
== PAREN_EXPR
)
2973 /* PAREN_EXPR is a simple copy. */
2974 copy_value_range (vr
, &vr0
);
2977 else if (code
== NEGATE_EXPR
)
2979 /* -X is simply 0 - X, so re-use existing code that also handles
2980 anti-ranges fine. */
2981 value_range_t zero
= VR_INITIALIZER
;
2982 set_value_range_to_value (&zero
, build_int_cst (type
, 0), NULL
);
2983 extract_range_from_binary_expr_1 (vr
, MINUS_EXPR
, type
, &zero
, &vr0
);
2986 else if (code
== BIT_NOT_EXPR
)
2988 /* ~X is simply -1 - X, so re-use existing code that also handles
2989 anti-ranges fine. */
2990 value_range_t minusone
= VR_INITIALIZER
;
2991 set_value_range_to_value (&minusone
, build_int_cst (type
, -1), NULL
);
2992 extract_range_from_binary_expr_1 (vr
, MINUS_EXPR
,
2993 type
, &minusone
, &vr0
);
2997 /* Now canonicalize anti-ranges to ranges when they are not symbolic
2998 and express op ~[] as (op []') U (op []''). */
2999 if (vr0
.type
== VR_ANTI_RANGE
3000 && ranges_from_anti_range (&vr0
, &vrtem0
, &vrtem1
))
3002 extract_range_from_unary_expr_1 (vr
, code
, type
, &vrtem0
, op0_type
);
3003 if (vrtem1
.type
!= VR_UNDEFINED
)
3005 value_range_t vrres
= VR_INITIALIZER
;
3006 extract_range_from_unary_expr_1 (&vrres
, code
, type
,
3008 vrp_meet (vr
, &vrres
);
3013 if (CONVERT_EXPR_CODE_P (code
))
3015 tree inner_type
= op0_type
;
3016 tree outer_type
= type
;
3018 /* If the expression evaluates to a pointer, we are only interested in
3019 determining if it evaluates to NULL [0, 0] or non-NULL (~[0, 0]). */
3020 if (POINTER_TYPE_P (type
))
3022 if (range_is_nonnull (&vr0
))
3023 set_value_range_to_nonnull (vr
, type
);
3024 else if (range_is_null (&vr0
))
3025 set_value_range_to_null (vr
, type
);
3027 set_value_range_to_varying (vr
);
3031 /* If VR0 is varying and we increase the type precision, assume
3032 a full range for the following transformation. */
3033 if (vr0
.type
== VR_VARYING
3034 && INTEGRAL_TYPE_P (inner_type
)
3035 && TYPE_PRECISION (inner_type
) < TYPE_PRECISION (outer_type
))
3037 vr0
.type
= VR_RANGE
;
3038 vr0
.min
= TYPE_MIN_VALUE (inner_type
);
3039 vr0
.max
= TYPE_MAX_VALUE (inner_type
);
3042 /* If VR0 is a constant range or anti-range and the conversion is
3043 not truncating we can convert the min and max values and
3044 canonicalize the resulting range. Otherwise we can do the
3045 conversion if the size of the range is less than what the
3046 precision of the target type can represent and the range is
3047 not an anti-range. */
3048 if ((vr0
.type
== VR_RANGE
3049 || vr0
.type
== VR_ANTI_RANGE
)
3050 && TREE_CODE (vr0
.min
) == INTEGER_CST
3051 && TREE_CODE (vr0
.max
) == INTEGER_CST
3052 && (!is_overflow_infinity (vr0
.min
)
3053 || (vr0
.type
== VR_RANGE
3054 && TYPE_PRECISION (outer_type
) > TYPE_PRECISION (inner_type
)
3055 && needs_overflow_infinity (outer_type
)
3056 && supports_overflow_infinity (outer_type
)))
3057 && (!is_overflow_infinity (vr0
.max
)
3058 || (vr0
.type
== VR_RANGE
3059 && TYPE_PRECISION (outer_type
) > TYPE_PRECISION (inner_type
)
3060 && needs_overflow_infinity (outer_type
)
3061 && supports_overflow_infinity (outer_type
)))
3062 && (TYPE_PRECISION (outer_type
) >= TYPE_PRECISION (inner_type
)
3063 || (vr0
.type
== VR_RANGE
3064 && integer_zerop (int_const_binop (RSHIFT_EXPR
,
3065 int_const_binop (MINUS_EXPR
, vr0
.max
, vr0
.min
),
3066 size_int (TYPE_PRECISION (outer_type
)))))))
3068 tree new_min
, new_max
;
3069 if (is_overflow_infinity (vr0
.min
))
3070 new_min
= negative_overflow_infinity (outer_type
);
3072 new_min
= force_fit_type_double (outer_type
,
3073 tree_to_double_int (vr0
.min
),
3075 if (is_overflow_infinity (vr0
.max
))
3076 new_max
= positive_overflow_infinity (outer_type
);
3078 new_max
= force_fit_type_double (outer_type
,
3079 tree_to_double_int (vr0
.max
),
3081 set_and_canonicalize_value_range (vr
, vr0
.type
,
3082 new_min
, new_max
, NULL
);
3086 set_value_range_to_varying (vr
);
3089 else if (code
== ABS_EXPR
)
3094 /* Pass through vr0 in the easy cases. */
3095 if (TYPE_UNSIGNED (type
)
3096 || value_range_nonnegative_p (&vr0
))
3098 copy_value_range (vr
, &vr0
);
3102 /* For the remaining varying or symbolic ranges we can't do anything
3104 if (vr0
.type
== VR_VARYING
3105 || symbolic_range_p (&vr0
))
3107 set_value_range_to_varying (vr
);
3111 /* -TYPE_MIN_VALUE = TYPE_MIN_VALUE with flag_wrapv so we can't get a
3113 if (!TYPE_OVERFLOW_UNDEFINED (type
)
3114 && ((vr0
.type
== VR_RANGE
3115 && vrp_val_is_min (vr0
.min
))
3116 || (vr0
.type
== VR_ANTI_RANGE
3117 && !vrp_val_is_min (vr0
.min
))))
3119 set_value_range_to_varying (vr
);
3123 /* ABS_EXPR may flip the range around, if the original range
3124 included negative values. */
3125 if (is_overflow_infinity (vr0
.min
))
3126 min
= positive_overflow_infinity (type
);
3127 else if (!vrp_val_is_min (vr0
.min
))
3128 min
= fold_unary_to_constant (code
, type
, vr0
.min
);
3129 else if (!needs_overflow_infinity (type
))
3130 min
= TYPE_MAX_VALUE (type
);
3131 else if (supports_overflow_infinity (type
))
3132 min
= positive_overflow_infinity (type
);
3135 set_value_range_to_varying (vr
);
3139 if (is_overflow_infinity (vr0
.max
))
3140 max
= positive_overflow_infinity (type
);
3141 else if (!vrp_val_is_min (vr0
.max
))
3142 max
= fold_unary_to_constant (code
, type
, vr0
.max
);
3143 else if (!needs_overflow_infinity (type
))
3144 max
= TYPE_MAX_VALUE (type
);
3145 else if (supports_overflow_infinity (type
)
3146 /* We shouldn't generate [+INF, +INF] as set_value_range
3147 doesn't like this and ICEs. */
3148 && !is_positive_overflow_infinity (min
))
3149 max
= positive_overflow_infinity (type
);
3152 set_value_range_to_varying (vr
);
3156 cmp
= compare_values (min
, max
);
3158 /* If a VR_ANTI_RANGEs contains zero, then we have
3159 ~[-INF, min(MIN, MAX)]. */
3160 if (vr0
.type
== VR_ANTI_RANGE
)
3162 if (range_includes_zero_p (&vr0
))
3164 /* Take the lower of the two values. */
3168 /* Create ~[-INF, min (abs(MIN), abs(MAX))]
3169 or ~[-INF + 1, min (abs(MIN), abs(MAX))] when
3170 flag_wrapv is set and the original anti-range doesn't include
3171 TYPE_MIN_VALUE, remember -TYPE_MIN_VALUE = TYPE_MIN_VALUE. */
3172 if (TYPE_OVERFLOW_WRAPS (type
))
3174 tree type_min_value
= TYPE_MIN_VALUE (type
);
3176 min
= (vr0
.min
!= type_min_value
3177 ? int_const_binop (PLUS_EXPR
, type_min_value
,
3183 if (overflow_infinity_range_p (&vr0
))
3184 min
= negative_overflow_infinity (type
);
3186 min
= TYPE_MIN_VALUE (type
);
3191 /* All else has failed, so create the range [0, INF], even for
3192 flag_wrapv since TYPE_MIN_VALUE is in the original
3194 vr0
.type
= VR_RANGE
;
3195 min
= build_int_cst (type
, 0);
3196 if (needs_overflow_infinity (type
))
3198 if (supports_overflow_infinity (type
))
3199 max
= positive_overflow_infinity (type
);
3202 set_value_range_to_varying (vr
);
3207 max
= TYPE_MAX_VALUE (type
);
3211 /* If the range contains zero then we know that the minimum value in the
3212 range will be zero. */
3213 else if (range_includes_zero_p (&vr0
))
3217 min
= build_int_cst (type
, 0);
3221 /* If the range was reversed, swap MIN and MAX. */
3230 cmp
= compare_values (min
, max
);
3231 if (cmp
== -2 || cmp
== 1)
3233 /* If the new range has its limits swapped around (MIN > MAX),
3234 then the operation caused one of them to wrap around, mark
3235 the new range VARYING. */
3236 set_value_range_to_varying (vr
);
3239 set_value_range (vr
, vr0
.type
, min
, max
, NULL
);
3243 /* For unhandled operations fall back to varying. */
3244 set_value_range_to_varying (vr
);
3249 /* Extract range information from a unary expression CODE OP0 based on
3250 the range of its operand with resulting type TYPE.
3251 The resulting range is stored in *VR. */
3254 extract_range_from_unary_expr (value_range_t
*vr
, enum tree_code code
,
3255 tree type
, tree op0
)
3257 value_range_t vr0
= VR_INITIALIZER
;
3259 /* Get value ranges for the operand. For constant operands, create
3260 a new value range with the operand to simplify processing. */
3261 if (TREE_CODE (op0
) == SSA_NAME
)
3262 vr0
= *(get_value_range (op0
));
3263 else if (is_gimple_min_invariant (op0
))
3264 set_value_range_to_value (&vr0
, op0
, NULL
);
3266 set_value_range_to_varying (&vr0
);
3268 extract_range_from_unary_expr_1 (vr
, code
, type
, &vr0
, TREE_TYPE (op0
));
3272 /* Extract range information from a conditional expression STMT based on
3273 the ranges of each of its operands and the expression code. */
3276 extract_range_from_cond_expr (value_range_t
*vr
, gimple stmt
)
3279 value_range_t vr0
= VR_INITIALIZER
;
3280 value_range_t vr1
= VR_INITIALIZER
;
3282 /* Get value ranges for each operand. For constant operands, create
3283 a new value range with the operand to simplify processing. */
3284 op0
= gimple_assign_rhs2 (stmt
);
3285 if (TREE_CODE (op0
) == SSA_NAME
)
3286 vr0
= *(get_value_range (op0
));
3287 else if (is_gimple_min_invariant (op0
))
3288 set_value_range_to_value (&vr0
, op0
, NULL
);
3290 set_value_range_to_varying (&vr0
);
3292 op1
= gimple_assign_rhs3 (stmt
);
3293 if (TREE_CODE (op1
) == SSA_NAME
)
3294 vr1
= *(get_value_range (op1
));
3295 else if (is_gimple_min_invariant (op1
))
3296 set_value_range_to_value (&vr1
, op1
, NULL
);
3298 set_value_range_to_varying (&vr1
);
3300 /* The resulting value range is the union of the operand ranges */
3301 copy_value_range (vr
, &vr0
);
3302 vrp_meet (vr
, &vr1
);
3306 /* Extract range information from a comparison expression EXPR based
3307 on the range of its operand and the expression code. */
3310 extract_range_from_comparison (value_range_t
*vr
, enum tree_code code
,
3311 tree type
, tree op0
, tree op1
)
3316 val
= vrp_evaluate_conditional_warnv_with_ops (code
, op0
, op1
, false, &sop
,
3319 /* A disadvantage of using a special infinity as an overflow
3320 representation is that we lose the ability to record overflow
3321 when we don't have an infinity. So we have to ignore a result
3322 which relies on overflow. */
3324 if (val
&& !is_overflow_infinity (val
) && !sop
)
3326 /* Since this expression was found on the RHS of an assignment,
3327 its type may be different from _Bool. Convert VAL to EXPR's
3329 val
= fold_convert (type
, val
);
3330 if (is_gimple_min_invariant (val
))
3331 set_value_range_to_value (vr
, val
, vr
->equiv
);
3333 set_value_range (vr
, VR_RANGE
, val
, val
, vr
->equiv
);
3336 /* The result of a comparison is always true or false. */
3337 set_value_range_to_truthvalue (vr
, type
);
3340 /* Try to derive a nonnegative or nonzero range out of STMT relying
3341 primarily on generic routines in fold in conjunction with range data.
3342 Store the result in *VR */
3345 extract_range_basic (value_range_t
*vr
, gimple stmt
)
3348 tree type
= gimple_expr_type (stmt
);
3350 if (INTEGRAL_TYPE_P (type
)
3351 && gimple_stmt_nonnegative_warnv_p (stmt
, &sop
))
3352 set_value_range_to_nonnegative (vr
, type
,
3353 sop
|| stmt_overflow_infinity (stmt
));
3354 else if (vrp_stmt_computes_nonzero (stmt
, &sop
)
3356 set_value_range_to_nonnull (vr
, type
);
3358 set_value_range_to_varying (vr
);
3362 /* Try to compute a useful range out of assignment STMT and store it
3366 extract_range_from_assignment (value_range_t
*vr
, gimple stmt
)
3368 enum tree_code code
= gimple_assign_rhs_code (stmt
);
3370 if (code
== ASSERT_EXPR
)
3371 extract_range_from_assert (vr
, gimple_assign_rhs1 (stmt
));
3372 else if (code
== SSA_NAME
)
3373 extract_range_from_ssa_name (vr
, gimple_assign_rhs1 (stmt
));
3374 else if (TREE_CODE_CLASS (code
) == tcc_binary
)
3375 extract_range_from_binary_expr (vr
, gimple_assign_rhs_code (stmt
),
3376 gimple_expr_type (stmt
),
3377 gimple_assign_rhs1 (stmt
),
3378 gimple_assign_rhs2 (stmt
));
3379 else if (TREE_CODE_CLASS (code
) == tcc_unary
)
3380 extract_range_from_unary_expr (vr
, gimple_assign_rhs_code (stmt
),
3381 gimple_expr_type (stmt
),
3382 gimple_assign_rhs1 (stmt
));
3383 else if (code
== COND_EXPR
)
3384 extract_range_from_cond_expr (vr
, stmt
);
3385 else if (TREE_CODE_CLASS (code
) == tcc_comparison
)
3386 extract_range_from_comparison (vr
, gimple_assign_rhs_code (stmt
),
3387 gimple_expr_type (stmt
),
3388 gimple_assign_rhs1 (stmt
),
3389 gimple_assign_rhs2 (stmt
));
3390 else if (get_gimple_rhs_class (code
) == GIMPLE_SINGLE_RHS
3391 && is_gimple_min_invariant (gimple_assign_rhs1 (stmt
)))
3392 set_value_range_to_value (vr
, gimple_assign_rhs1 (stmt
), NULL
);
3394 set_value_range_to_varying (vr
);
3396 if (vr
->type
== VR_VARYING
)
3397 extract_range_basic (vr
, stmt
);
3400 /* Given a range VR, a LOOP and a variable VAR, determine whether it
3401 would be profitable to adjust VR using scalar evolution information
3402 for VAR. If so, update VR with the new limits. */
3405 adjust_range_with_scev (value_range_t
*vr
, struct loop
*loop
,
3406 gimple stmt
, tree var
)
3408 tree init
, step
, chrec
, tmin
, tmax
, min
, max
, type
, tem
;
3409 enum ev_direction dir
;
3411 /* TODO. Don't adjust anti-ranges. An anti-range may provide
3412 better opportunities than a regular range, but I'm not sure. */
3413 if (vr
->type
== VR_ANTI_RANGE
)
3416 chrec
= instantiate_parameters (loop
, analyze_scalar_evolution (loop
, var
));
3418 /* Like in PR19590, scev can return a constant function. */
3419 if (is_gimple_min_invariant (chrec
))
3421 set_value_range_to_value (vr
, chrec
, vr
->equiv
);
3425 if (TREE_CODE (chrec
) != POLYNOMIAL_CHREC
)
3428 init
= initial_condition_in_loop_num (chrec
, loop
->num
);
3429 tem
= op_with_constant_singleton_value_range (init
);
3432 step
= evolution_part_in_loop_num (chrec
, loop
->num
);
3433 tem
= op_with_constant_singleton_value_range (step
);
3437 /* If STEP is symbolic, we can't know whether INIT will be the
3438 minimum or maximum value in the range. Also, unless INIT is
3439 a simple expression, compare_values and possibly other functions
3440 in tree-vrp won't be able to handle it. */
3441 if (step
== NULL_TREE
3442 || !is_gimple_min_invariant (step
)
3443 || !valid_value_p (init
))
3446 dir
= scev_direction (chrec
);
3447 if (/* Do not adjust ranges if we do not know whether the iv increases
3448 or decreases, ... */
3449 dir
== EV_DIR_UNKNOWN
3450 /* ... or if it may wrap. */
3451 || scev_probably_wraps_p (init
, step
, stmt
, get_chrec_loop (chrec
),
3455 /* We use TYPE_MIN_VALUE and TYPE_MAX_VALUE here instead of
3456 negative_overflow_infinity and positive_overflow_infinity,
3457 because we have concluded that the loop probably does not
3460 type
= TREE_TYPE (var
);
3461 if (POINTER_TYPE_P (type
) || !TYPE_MIN_VALUE (type
))
3462 tmin
= lower_bound_in_type (type
, type
);
3464 tmin
= TYPE_MIN_VALUE (type
);
3465 if (POINTER_TYPE_P (type
) || !TYPE_MAX_VALUE (type
))
3466 tmax
= upper_bound_in_type (type
, type
);
3468 tmax
= TYPE_MAX_VALUE (type
);
3470 /* Try to use estimated number of iterations for the loop to constrain the
3471 final value in the evolution. */
3472 if (TREE_CODE (step
) == INTEGER_CST
3473 && is_gimple_val (init
)
3474 && (TREE_CODE (init
) != SSA_NAME
3475 || get_value_range (init
)->type
== VR_RANGE
))
3479 /* We are only entering here for loop header PHI nodes, so using
3480 the number of latch executions is the correct thing to use. */
3481 if (max_loop_iterations (loop
, &nit
))
3483 value_range_t maxvr
= VR_INITIALIZER
;
3485 bool unsigned_p
= TYPE_UNSIGNED (TREE_TYPE (step
));
3488 dtmp
= double_int_mul_with_sign (tree_to_double_int (step
), nit
,
3489 unsigned_p
, &overflow
);
3490 /* If the multiplication overflowed we can't do a meaningful
3491 adjustment. Likewise if the result doesn't fit in the type
3492 of the induction variable. For a signed type we have to
3493 check whether the result has the expected signedness which
3494 is that of the step as number of iterations is unsigned. */
3496 && double_int_fits_to_tree_p (TREE_TYPE (init
), dtmp
)
3498 || ((dtmp
.high
^ TREE_INT_CST_HIGH (step
)) >= 0)))
3500 tem
= double_int_to_tree (TREE_TYPE (init
), dtmp
);
3501 extract_range_from_binary_expr (&maxvr
, PLUS_EXPR
,
3502 TREE_TYPE (init
), init
, tem
);
3503 /* Likewise if the addition did. */
3504 if (maxvr
.type
== VR_RANGE
)
3513 if (vr
->type
== VR_VARYING
|| vr
->type
== VR_UNDEFINED
)
3518 /* For VARYING or UNDEFINED ranges, just about anything we get
3519 from scalar evolutions should be better. */
3521 if (dir
== EV_DIR_DECREASES
)
3526 /* If we would create an invalid range, then just assume we
3527 know absolutely nothing. This may be over-conservative,
3528 but it's clearly safe, and should happen only in unreachable
3529 parts of code, or for invalid programs. */
3530 if (compare_values (min
, max
) == 1)
3533 set_value_range (vr
, VR_RANGE
, min
, max
, vr
->equiv
);
3535 else if (vr
->type
== VR_RANGE
)
3540 if (dir
== EV_DIR_DECREASES
)
3542 /* INIT is the maximum value. If INIT is lower than VR->MAX
3543 but no smaller than VR->MIN, set VR->MAX to INIT. */
3544 if (compare_values (init
, max
) == -1)
3547 /* According to the loop information, the variable does not
3548 overflow. If we think it does, probably because of an
3549 overflow due to arithmetic on a different INF value,
3551 if (is_negative_overflow_infinity (min
)
3552 || compare_values (min
, tmin
) == -1)
3558 /* If INIT is bigger than VR->MIN, set VR->MIN to INIT. */
3559 if (compare_values (init
, min
) == 1)
3562 if (is_positive_overflow_infinity (max
)
3563 || compare_values (tmax
, max
) == -1)
3567 /* If we just created an invalid range with the minimum
3568 greater than the maximum, we fail conservatively.
3569 This should happen only in unreachable
3570 parts of code, or for invalid programs. */
3571 if (compare_values (min
, max
) == 1)
3574 set_value_range (vr
, VR_RANGE
, min
, max
, vr
->equiv
);
3578 /* Return true if VAR may overflow at STMT. This checks any available
3579 loop information to see if we can determine that VAR does not
3583 vrp_var_may_overflow (tree var
, gimple stmt
)
3586 tree chrec
, init
, step
;
3588 if (current_loops
== NULL
)
3591 l
= loop_containing_stmt (stmt
);
3596 chrec
= instantiate_parameters (l
, analyze_scalar_evolution (l
, var
));
3597 if (TREE_CODE (chrec
) != POLYNOMIAL_CHREC
)
3600 init
= initial_condition_in_loop_num (chrec
, l
->num
);
3601 step
= evolution_part_in_loop_num (chrec
, l
->num
);
3603 if (step
== NULL_TREE
3604 || !is_gimple_min_invariant (step
)
3605 || !valid_value_p (init
))
3608 /* If we get here, we know something useful about VAR based on the
3609 loop information. If it wraps, it may overflow. */
3611 if (scev_probably_wraps_p (init
, step
, stmt
, get_chrec_loop (chrec
),
3615 if (dump_file
&& (dump_flags
& TDF_DETAILS
) != 0)
3617 print_generic_expr (dump_file
, var
, 0);
3618 fprintf (dump_file
, ": loop information indicates does not overflow\n");
3625 /* Given two numeric value ranges VR0, VR1 and a comparison code COMP:
3627 - Return BOOLEAN_TRUE_NODE if VR0 COMP VR1 always returns true for
3628 all the values in the ranges.
3630 - Return BOOLEAN_FALSE_NODE if the comparison always returns false.
3632 - Return NULL_TREE if it is not always possible to determine the
3633 value of the comparison.
3635 Also set *STRICT_OVERFLOW_P to indicate whether a range with an
3636 overflow infinity was used in the test. */
3640 compare_ranges (enum tree_code comp
, value_range_t
*vr0
, value_range_t
*vr1
,
3641 bool *strict_overflow_p
)
3643 /* VARYING or UNDEFINED ranges cannot be compared. */
3644 if (vr0
->type
== VR_VARYING
3645 || vr0
->type
== VR_UNDEFINED
3646 || vr1
->type
== VR_VARYING
3647 || vr1
->type
== VR_UNDEFINED
)
3650 /* Anti-ranges need to be handled separately. */
3651 if (vr0
->type
== VR_ANTI_RANGE
|| vr1
->type
== VR_ANTI_RANGE
)
3653 /* If both are anti-ranges, then we cannot compute any
3655 if (vr0
->type
== VR_ANTI_RANGE
&& vr1
->type
== VR_ANTI_RANGE
)
3658 /* These comparisons are never statically computable. */
3665 /* Equality can be computed only between a range and an
3666 anti-range. ~[VAL1, VAL2] == [VAL1, VAL2] is always false. */
3667 if (vr0
->type
== VR_RANGE
)
3669 /* To simplify processing, make VR0 the anti-range. */
3670 value_range_t
*tmp
= vr0
;
3675 gcc_assert (comp
== NE_EXPR
|| comp
== EQ_EXPR
);
3677 if (compare_values_warnv (vr0
->min
, vr1
->min
, strict_overflow_p
) == 0
3678 && compare_values_warnv (vr0
->max
, vr1
->max
, strict_overflow_p
) == 0)
3679 return (comp
== NE_EXPR
) ? boolean_true_node
: boolean_false_node
;
3684 if (!usable_range_p (vr0
, strict_overflow_p
)
3685 || !usable_range_p (vr1
, strict_overflow_p
))
3688 /* Simplify processing. If COMP is GT_EXPR or GE_EXPR, switch the
3689 operands around and change the comparison code. */
3690 if (comp
== GT_EXPR
|| comp
== GE_EXPR
)
3693 comp
= (comp
== GT_EXPR
) ? LT_EXPR
: LE_EXPR
;
3699 if (comp
== EQ_EXPR
)
3701 /* Equality may only be computed if both ranges represent
3702 exactly one value. */
3703 if (compare_values_warnv (vr0
->min
, vr0
->max
, strict_overflow_p
) == 0
3704 && compare_values_warnv (vr1
->min
, vr1
->max
, strict_overflow_p
) == 0)
3706 int cmp_min
= compare_values_warnv (vr0
->min
, vr1
->min
,
3708 int cmp_max
= compare_values_warnv (vr0
->max
, vr1
->max
,
3710 if (cmp_min
== 0 && cmp_max
== 0)
3711 return boolean_true_node
;
3712 else if (cmp_min
!= -2 && cmp_max
!= -2)
3713 return boolean_false_node
;
3715 /* If [V0_MIN, V1_MAX] < [V1_MIN, V1_MAX] then V0 != V1. */
3716 else if (compare_values_warnv (vr0
->min
, vr1
->max
,
3717 strict_overflow_p
) == 1
3718 || compare_values_warnv (vr1
->min
, vr0
->max
,
3719 strict_overflow_p
) == 1)
3720 return boolean_false_node
;
3724 else if (comp
== NE_EXPR
)
3728 /* If VR0 is completely to the left or completely to the right
3729 of VR1, they are always different. Notice that we need to
3730 make sure that both comparisons yield similar results to
3731 avoid comparing values that cannot be compared at
3733 cmp1
= compare_values_warnv (vr0
->max
, vr1
->min
, strict_overflow_p
);
3734 cmp2
= compare_values_warnv (vr0
->min
, vr1
->max
, strict_overflow_p
);
3735 if ((cmp1
== -1 && cmp2
== -1) || (cmp1
== 1 && cmp2
== 1))
3736 return boolean_true_node
;
3738 /* If VR0 and VR1 represent a single value and are identical,
3740 else if (compare_values_warnv (vr0
->min
, vr0
->max
,
3741 strict_overflow_p
) == 0
3742 && compare_values_warnv (vr1
->min
, vr1
->max
,
3743 strict_overflow_p
) == 0
3744 && compare_values_warnv (vr0
->min
, vr1
->min
,
3745 strict_overflow_p
) == 0
3746 && compare_values_warnv (vr0
->max
, vr1
->max
,
3747 strict_overflow_p
) == 0)
3748 return boolean_false_node
;
3750 /* Otherwise, they may or may not be different. */
3754 else if (comp
== LT_EXPR
|| comp
== LE_EXPR
)
3758 /* If VR0 is to the left of VR1, return true. */
3759 tst
= compare_values_warnv (vr0
->max
, vr1
->min
, strict_overflow_p
);
3760 if ((comp
== LT_EXPR
&& tst
== -1)
3761 || (comp
== LE_EXPR
&& (tst
== -1 || tst
== 0)))
3763 if (overflow_infinity_range_p (vr0
)
3764 || overflow_infinity_range_p (vr1
))
3765 *strict_overflow_p
= true;
3766 return boolean_true_node
;
3769 /* If VR0 is to the right of VR1, return false. */
3770 tst
= compare_values_warnv (vr0
->min
, vr1
->max
, strict_overflow_p
);
3771 if ((comp
== LT_EXPR
&& (tst
== 0 || tst
== 1))
3772 || (comp
== LE_EXPR
&& tst
== 1))
3774 if (overflow_infinity_range_p (vr0
)
3775 || overflow_infinity_range_p (vr1
))
3776 *strict_overflow_p
= true;
3777 return boolean_false_node
;
3780 /* Otherwise, we don't know. */
3788 /* Given a value range VR, a value VAL and a comparison code COMP, return
3789 BOOLEAN_TRUE_NODE if VR COMP VAL always returns true for all the
3790 values in VR. Return BOOLEAN_FALSE_NODE if the comparison
3791 always returns false. Return NULL_TREE if it is not always
3792 possible to determine the value of the comparison. Also set
3793 *STRICT_OVERFLOW_P to indicate whether a range with an overflow
3794 infinity was used in the test. */
3797 compare_range_with_value (enum tree_code comp
, value_range_t
*vr
, tree val
,
3798 bool *strict_overflow_p
)
3800 if (vr
->type
== VR_VARYING
|| vr
->type
== VR_UNDEFINED
)
3803 /* Anti-ranges need to be handled separately. */
3804 if (vr
->type
== VR_ANTI_RANGE
)
3806 /* For anti-ranges, the only predicates that we can compute at
3807 compile time are equality and inequality. */
3814 /* ~[VAL_1, VAL_2] OP VAL is known if VAL_1 <= VAL <= VAL_2. */
3815 if (value_inside_range (val
, vr
) == 1)
3816 return (comp
== NE_EXPR
) ? boolean_true_node
: boolean_false_node
;
3821 if (!usable_range_p (vr
, strict_overflow_p
))
3824 if (comp
== EQ_EXPR
)
3826 /* EQ_EXPR may only be computed if VR represents exactly
3828 if (compare_values_warnv (vr
->min
, vr
->max
, strict_overflow_p
) == 0)
3830 int cmp
= compare_values_warnv (vr
->min
, val
, strict_overflow_p
);
3832 return boolean_true_node
;
3833 else if (cmp
== -1 || cmp
== 1 || cmp
== 2)
3834 return boolean_false_node
;
3836 else if (compare_values_warnv (val
, vr
->min
, strict_overflow_p
) == -1
3837 || compare_values_warnv (vr
->max
, val
, strict_overflow_p
) == -1)
3838 return boolean_false_node
;
3842 else if (comp
== NE_EXPR
)
3844 /* If VAL is not inside VR, then they are always different. */
3845 if (compare_values_warnv (vr
->max
, val
, strict_overflow_p
) == -1
3846 || compare_values_warnv (vr
->min
, val
, strict_overflow_p
) == 1)
3847 return boolean_true_node
;
3849 /* If VR represents exactly one value equal to VAL, then return
3851 if (compare_values_warnv (vr
->min
, vr
->max
, strict_overflow_p
) == 0
3852 && compare_values_warnv (vr
->min
, val
, strict_overflow_p
) == 0)
3853 return boolean_false_node
;
3855 /* Otherwise, they may or may not be different. */
3858 else if (comp
== LT_EXPR
|| comp
== LE_EXPR
)
3862 /* If VR is to the left of VAL, return true. */
3863 tst
= compare_values_warnv (vr
->max
, val
, strict_overflow_p
);
3864 if ((comp
== LT_EXPR
&& tst
== -1)
3865 || (comp
== LE_EXPR
&& (tst
== -1 || tst
== 0)))
3867 if (overflow_infinity_range_p (vr
))
3868 *strict_overflow_p
= true;
3869 return boolean_true_node
;
3872 /* If VR is to the right of VAL, return false. */
3873 tst
= compare_values_warnv (vr
->min
, val
, strict_overflow_p
);
3874 if ((comp
== LT_EXPR
&& (tst
== 0 || tst
== 1))
3875 || (comp
== LE_EXPR
&& tst
== 1))
3877 if (overflow_infinity_range_p (vr
))
3878 *strict_overflow_p
= true;
3879 return boolean_false_node
;
3882 /* Otherwise, we don't know. */
3885 else if (comp
== GT_EXPR
|| comp
== GE_EXPR
)
3889 /* If VR is to the right of VAL, return true. */
3890 tst
= compare_values_warnv (vr
->min
, val
, strict_overflow_p
);
3891 if ((comp
== GT_EXPR
&& tst
== 1)
3892 || (comp
== GE_EXPR
&& (tst
== 0 || tst
== 1)))
3894 if (overflow_infinity_range_p (vr
))
3895 *strict_overflow_p
= true;
3896 return boolean_true_node
;
3899 /* If VR is to the left of VAL, return false. */
3900 tst
= compare_values_warnv (vr
->max
, val
, strict_overflow_p
);
3901 if ((comp
== GT_EXPR
&& (tst
== -1 || tst
== 0))
3902 || (comp
== GE_EXPR
&& tst
== -1))
3904 if (overflow_infinity_range_p (vr
))
3905 *strict_overflow_p
= true;
3906 return boolean_false_node
;
3909 /* Otherwise, we don't know. */
3917 /* Debugging dumps. */
3919 void dump_value_range (FILE *, value_range_t
*);
3920 void debug_value_range (value_range_t
*);
3921 void dump_all_value_ranges (FILE *);
3922 void debug_all_value_ranges (void);
3923 void dump_vr_equiv (FILE *, bitmap
);
3924 void debug_vr_equiv (bitmap
);
3927 /* Dump value range VR to FILE. */
3930 dump_value_range (FILE *file
, value_range_t
*vr
)
3933 fprintf (file
, "[]");
3934 else if (vr
->type
== VR_UNDEFINED
)
3935 fprintf (file
, "UNDEFINED");
3936 else if (vr
->type
== VR_RANGE
|| vr
->type
== VR_ANTI_RANGE
)
3938 tree type
= TREE_TYPE (vr
->min
);
3940 fprintf (file
, "%s[", (vr
->type
== VR_ANTI_RANGE
) ? "~" : "");
3942 if (is_negative_overflow_infinity (vr
->min
))
3943 fprintf (file
, "-INF(OVF)");
3944 else if (INTEGRAL_TYPE_P (type
)
3945 && !TYPE_UNSIGNED (type
)
3946 && vrp_val_is_min (vr
->min
))
3947 fprintf (file
, "-INF");
3949 print_generic_expr (file
, vr
->min
, 0);
3951 fprintf (file
, ", ");
3953 if (is_positive_overflow_infinity (vr
->max
))
3954 fprintf (file
, "+INF(OVF)");
3955 else if (INTEGRAL_TYPE_P (type
)
3956 && vrp_val_is_max (vr
->max
))
3957 fprintf (file
, "+INF");
3959 print_generic_expr (file
, vr
->max
, 0);
3961 fprintf (file
, "]");
3968 fprintf (file
, " EQUIVALENCES: { ");
3970 EXECUTE_IF_SET_IN_BITMAP (vr
->equiv
, 0, i
, bi
)
3972 print_generic_expr (file
, ssa_name (i
), 0);
3973 fprintf (file
, " ");
3977 fprintf (file
, "} (%u elements)", c
);
3980 else if (vr
->type
== VR_VARYING
)
3981 fprintf (file
, "VARYING");
3983 fprintf (file
, "INVALID RANGE");
3987 /* Dump value range VR to stderr. */
3990 debug_value_range (value_range_t
*vr
)
3992 dump_value_range (stderr
, vr
);
3993 fprintf (stderr
, "\n");
3997 /* Dump value ranges of all SSA_NAMEs to FILE. */
4000 dump_all_value_ranges (FILE *file
)
4004 for (i
= 0; i
< num_vr_values
; i
++)
4008 print_generic_expr (file
, ssa_name (i
), 0);
4009 fprintf (file
, ": ");
4010 dump_value_range (file
, vr_value
[i
]);
4011 fprintf (file
, "\n");
4015 fprintf (file
, "\n");
4019 /* Dump all value ranges to stderr. */
4022 debug_all_value_ranges (void)
4024 dump_all_value_ranges (stderr
);
4028 /* Given a COND_EXPR COND of the form 'V OP W', and an SSA name V,
4029 create a new SSA name N and return the assertion assignment
4030 'V = ASSERT_EXPR <V, V OP W>'. */
4033 build_assert_expr_for (tree cond
, tree v
)
4038 gcc_assert (TREE_CODE (v
) == SSA_NAME
);
4039 n
= duplicate_ssa_name (v
, NULL
);
4041 if (COMPARISON_CLASS_P (cond
))
4043 tree a
= build2 (ASSERT_EXPR
, TREE_TYPE (v
), v
, cond
);
4044 assertion
= gimple_build_assign (n
, a
);
4046 else if (TREE_CODE (cond
) == SSA_NAME
)
4048 /* Given V, build the assignment N = true. */
4049 gcc_assert (v
== cond
);
4050 assertion
= gimple_build_assign (n
, boolean_true_node
);
4055 SSA_NAME_DEF_STMT (n
) = assertion
;
4057 /* The new ASSERT_EXPR, creates a new SSA name that replaces the
4058 operand of the ASSERT_EXPR. Register the new name and the old one
4059 in the replacement table so that we can fix the SSA web after
4060 adding all the ASSERT_EXPRs. */
4061 register_new_name_mapping (n
, v
);
4067 /* Return false if EXPR is a predicate expression involving floating
4071 fp_predicate (gimple stmt
)
4073 GIMPLE_CHECK (stmt
, GIMPLE_COND
);
4075 return FLOAT_TYPE_P (TREE_TYPE (gimple_cond_lhs (stmt
)));
4079 /* If the range of values taken by OP can be inferred after STMT executes,
4080 return the comparison code (COMP_CODE_P) and value (VAL_P) that
4081 describes the inferred range. Return true if a range could be
4085 infer_value_range (gimple stmt
, tree op
, enum tree_code
*comp_code_p
, tree
*val_p
)
4088 *comp_code_p
= ERROR_MARK
;
4090 /* Do not attempt to infer anything in names that flow through
4092 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op
))
4095 /* Similarly, don't infer anything from statements that may throw
4097 if (stmt_could_throw_p (stmt
))
4100 /* If STMT is the last statement of a basic block with no
4101 successors, there is no point inferring anything about any of its
4102 operands. We would not be able to find a proper insertion point
4103 for the assertion, anyway. */
4104 if (stmt_ends_bb_p (stmt
) && EDGE_COUNT (gimple_bb (stmt
)->succs
) == 0)
4107 /* We can only assume that a pointer dereference will yield
4108 non-NULL if -fdelete-null-pointer-checks is enabled. */
4109 if (flag_delete_null_pointer_checks
4110 && POINTER_TYPE_P (TREE_TYPE (op
))
4111 && gimple_code (stmt
) != GIMPLE_ASM
)
4113 unsigned num_uses
, num_loads
, num_stores
;
4115 count_uses_and_derefs (op
, stmt
, &num_uses
, &num_loads
, &num_stores
);
4116 if (num_loads
+ num_stores
> 0)
4118 *val_p
= build_int_cst (TREE_TYPE (op
), 0);
4119 *comp_code_p
= NE_EXPR
;
4128 void dump_asserts_for (FILE *, tree
);
4129 void debug_asserts_for (tree
);
4130 void dump_all_asserts (FILE *);
4131 void debug_all_asserts (void);
4133 /* Dump all the registered assertions for NAME to FILE. */
4136 dump_asserts_for (FILE *file
, tree name
)
4140 fprintf (file
, "Assertions to be inserted for ");
4141 print_generic_expr (file
, name
, 0);
4142 fprintf (file
, "\n");
4144 loc
= asserts_for
[SSA_NAME_VERSION (name
)];
4147 fprintf (file
, "\t");
4148 print_gimple_stmt (file
, gsi_stmt (loc
->si
), 0, 0);
4149 fprintf (file
, "\n\tBB #%d", loc
->bb
->index
);
4152 fprintf (file
, "\n\tEDGE %d->%d", loc
->e
->src
->index
,
4153 loc
->e
->dest
->index
);
4154 dump_edge_info (file
, loc
->e
, 0);
4156 fprintf (file
, "\n\tPREDICATE: ");
4157 print_generic_expr (file
, name
, 0);
4158 fprintf (file
, " %s ", tree_code_name
[(int)loc
->comp_code
]);
4159 print_generic_expr (file
, loc
->val
, 0);
4160 fprintf (file
, "\n\n");
4164 fprintf (file
, "\n");
4168 /* Dump all the registered assertions for NAME to stderr. */
4171 debug_asserts_for (tree name
)
4173 dump_asserts_for (stderr
, name
);
4177 /* Dump all the registered assertions for all the names to FILE. */
4180 dump_all_asserts (FILE *file
)
4185 fprintf (file
, "\nASSERT_EXPRs to be inserted\n\n");
4186 EXECUTE_IF_SET_IN_BITMAP (need_assert_for
, 0, i
, bi
)
4187 dump_asserts_for (file
, ssa_name (i
));
4188 fprintf (file
, "\n");
4192 /* Dump all the registered assertions for all the names to stderr. */
4195 debug_all_asserts (void)
4197 dump_all_asserts (stderr
);
4201 /* If NAME doesn't have an ASSERT_EXPR registered for asserting
4202 'EXPR COMP_CODE VAL' at a location that dominates block BB or
4203 E->DEST, then register this location as a possible insertion point
4204 for ASSERT_EXPR <NAME, EXPR COMP_CODE VAL>.
4206 BB, E and SI provide the exact insertion point for the new
4207 ASSERT_EXPR. If BB is NULL, then the ASSERT_EXPR is to be inserted
4208 on edge E. Otherwise, if E is NULL, the ASSERT_EXPR is inserted on
4209 BB. If SI points to a COND_EXPR or a SWITCH_EXPR statement, then E
4210 must not be NULL. */
4213 register_new_assert_for (tree name
, tree expr
,
4214 enum tree_code comp_code
,
4218 gimple_stmt_iterator si
)
4220 assert_locus_t n
, loc
, last_loc
;
4221 basic_block dest_bb
;
4223 gcc_checking_assert (bb
== NULL
|| e
== NULL
);
4226 gcc_checking_assert (gimple_code (gsi_stmt (si
)) != GIMPLE_COND
4227 && gimple_code (gsi_stmt (si
)) != GIMPLE_SWITCH
);
4229 /* Never build an assert comparing against an integer constant with
4230 TREE_OVERFLOW set. This confuses our undefined overflow warning
4232 if (TREE_CODE (val
) == INTEGER_CST
4233 && TREE_OVERFLOW (val
))
4234 val
= build_int_cst_wide (TREE_TYPE (val
),
4235 TREE_INT_CST_LOW (val
), TREE_INT_CST_HIGH (val
));
4237 /* The new assertion A will be inserted at BB or E. We need to
4238 determine if the new location is dominated by a previously
4239 registered location for A. If we are doing an edge insertion,
4240 assume that A will be inserted at E->DEST. Note that this is not
4243 If E is a critical edge, it will be split. But even if E is
4244 split, the new block will dominate the same set of blocks that
4247 The reverse, however, is not true, blocks dominated by E->DEST
4248 will not be dominated by the new block created to split E. So,
4249 if the insertion location is on a critical edge, we will not use
4250 the new location to move another assertion previously registered
4251 at a block dominated by E->DEST. */
4252 dest_bb
= (bb
) ? bb
: e
->dest
;
4254 /* If NAME already has an ASSERT_EXPR registered for COMP_CODE and
4255 VAL at a block dominating DEST_BB, then we don't need to insert a new
4256 one. Similarly, if the same assertion already exists at a block
4257 dominated by DEST_BB and the new location is not on a critical
4258 edge, then update the existing location for the assertion (i.e.,
4259 move the assertion up in the dominance tree).
4261 Note, this is implemented as a simple linked list because there
4262 should not be more than a handful of assertions registered per
4263 name. If this becomes a performance problem, a table hashed by
4264 COMP_CODE and VAL could be implemented. */
4265 loc
= asserts_for
[SSA_NAME_VERSION (name
)];
4269 if (loc
->comp_code
== comp_code
4271 || operand_equal_p (loc
->val
, val
, 0))
4272 && (loc
->expr
== expr
4273 || operand_equal_p (loc
->expr
, expr
, 0)))
4275 /* If the assertion NAME COMP_CODE VAL has already been
4276 registered at a basic block that dominates DEST_BB, then
4277 we don't need to insert the same assertion again. Note
4278 that we don't check strict dominance here to avoid
4279 replicating the same assertion inside the same basic
4280 block more than once (e.g., when a pointer is
4281 dereferenced several times inside a block).
4283 An exception to this rule are edge insertions. If the
4284 new assertion is to be inserted on edge E, then it will
4285 dominate all the other insertions that we may want to
4286 insert in DEST_BB. So, if we are doing an edge
4287 insertion, don't do this dominance check. */
4289 && dominated_by_p (CDI_DOMINATORS
, dest_bb
, loc
->bb
))
4292 /* Otherwise, if E is not a critical edge and DEST_BB
4293 dominates the existing location for the assertion, move
4294 the assertion up in the dominance tree by updating its
4295 location information. */
4296 if ((e
== NULL
|| !EDGE_CRITICAL_P (e
))
4297 && dominated_by_p (CDI_DOMINATORS
, loc
->bb
, dest_bb
))
4306 /* Update the last node of the list and move to the next one. */
4311 /* If we didn't find an assertion already registered for
4312 NAME COMP_CODE VAL, add a new one at the end of the list of
4313 assertions associated with NAME. */
4314 n
= XNEW (struct assert_locus_d
);
4318 n
->comp_code
= comp_code
;
4326 asserts_for
[SSA_NAME_VERSION (name
)] = n
;
4328 bitmap_set_bit (need_assert_for
, SSA_NAME_VERSION (name
));
4331 /* (COND_OP0 COND_CODE COND_OP1) is a predicate which uses NAME.
4332 Extract a suitable test code and value and store them into *CODE_P and
4333 *VAL_P so the predicate is normalized to NAME *CODE_P *VAL_P.
4335 If no extraction was possible, return FALSE, otherwise return TRUE.
4337 If INVERT is true, then we invert the result stored into *CODE_P. */
4340 extract_code_and_val_from_cond_with_ops (tree name
, enum tree_code cond_code
,
4341 tree cond_op0
, tree cond_op1
,
4342 bool invert
, enum tree_code
*code_p
,
4345 enum tree_code comp_code
;
4348 /* Otherwise, we have a comparison of the form NAME COMP VAL
4349 or VAL COMP NAME. */
4350 if (name
== cond_op1
)
4352 /* If the predicate is of the form VAL COMP NAME, flip
4353 COMP around because we need to register NAME as the
4354 first operand in the predicate. */
4355 comp_code
= swap_tree_comparison (cond_code
);
4360 /* The comparison is of the form NAME COMP VAL, so the
4361 comparison code remains unchanged. */
4362 comp_code
= cond_code
;
4366 /* Invert the comparison code as necessary. */
4368 comp_code
= invert_tree_comparison (comp_code
, 0);
4370 /* VRP does not handle float types. */
4371 if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (val
)))
4374 /* Do not register always-false predicates.
4375 FIXME: this works around a limitation in fold() when dealing with
4376 enumerations. Given 'enum { N1, N2 } x;', fold will not
4377 fold 'if (x > N2)' to 'if (0)'. */
4378 if ((comp_code
== GT_EXPR
|| comp_code
== LT_EXPR
)
4379 && INTEGRAL_TYPE_P (TREE_TYPE (val
)))
4381 tree min
= TYPE_MIN_VALUE (TREE_TYPE (val
));
4382 tree max
= TYPE_MAX_VALUE (TREE_TYPE (val
));
4384 if (comp_code
== GT_EXPR
4386 || compare_values (val
, max
) == 0))
4389 if (comp_code
== LT_EXPR
4391 || compare_values (val
, min
) == 0))
4394 *code_p
= comp_code
;
4399 /* Find out smallest RES where RES > VAL && (RES & MASK) == RES, if any
4400 (otherwise return VAL). VAL and MASK must be zero-extended for
4401 precision PREC. If SGNBIT is non-zero, first xor VAL with SGNBIT
4402 (to transform signed values into unsigned) and at the end xor
4406 masked_increment (double_int val
, double_int mask
, double_int sgnbit
,
4409 double_int bit
= double_int_one
, res
;
4412 val
= double_int_xor (val
, sgnbit
);
4413 for (i
= 0; i
< prec
; i
++, bit
= double_int_add (bit
, bit
))
4416 if (double_int_zero_p (double_int_and (res
, bit
)))
4418 res
= double_int_sub (bit
, double_int_one
);
4419 res
= double_int_and_not (double_int_add (val
, bit
), res
);
4420 res
= double_int_and (res
, mask
);
4421 if (double_int_ucmp (res
, val
) > 0)
4422 return double_int_xor (res
, sgnbit
);
4424 return double_int_xor (val
, sgnbit
);
4427 /* Try to register an edge assertion for SSA name NAME on edge E for
4428 the condition COND contributing to the conditional jump pointed to by BSI.
4429 Invert the condition COND if INVERT is true.
4430 Return true if an assertion for NAME could be registered. */
4433 register_edge_assert_for_2 (tree name
, edge e
, gimple_stmt_iterator bsi
,
4434 enum tree_code cond_code
,
4435 tree cond_op0
, tree cond_op1
, bool invert
)
4438 enum tree_code comp_code
;
4439 bool retval
= false;
4441 if (!extract_code_and_val_from_cond_with_ops (name
, cond_code
,
4444 invert
, &comp_code
, &val
))
4447 /* Only register an ASSERT_EXPR if NAME was found in the sub-graph
4448 reachable from E. */
4449 if (live_on_edge (e
, name
)
4450 && !has_single_use (name
))
4452 register_new_assert_for (name
, name
, comp_code
, val
, NULL
, e
, bsi
);
4456 /* In the case of NAME <= CST and NAME being defined as
4457 NAME = (unsigned) NAME2 + CST2 we can assert NAME2 >= -CST2
4458 and NAME2 <= CST - CST2. We can do the same for NAME > CST.
4459 This catches range and anti-range tests. */
4460 if ((comp_code
== LE_EXPR
4461 || comp_code
== GT_EXPR
)
4462 && TREE_CODE (val
) == INTEGER_CST
4463 && TYPE_UNSIGNED (TREE_TYPE (val
)))
4465 gimple def_stmt
= SSA_NAME_DEF_STMT (name
);
4466 tree cst2
= NULL_TREE
, name2
= NULL_TREE
, name3
= NULL_TREE
;
4468 /* Extract CST2 from the (optional) addition. */
4469 if (is_gimple_assign (def_stmt
)
4470 && gimple_assign_rhs_code (def_stmt
) == PLUS_EXPR
)
4472 name2
= gimple_assign_rhs1 (def_stmt
);
4473 cst2
= gimple_assign_rhs2 (def_stmt
);
4474 if (TREE_CODE (name2
) == SSA_NAME
4475 && TREE_CODE (cst2
) == INTEGER_CST
)
4476 def_stmt
= SSA_NAME_DEF_STMT (name2
);
4479 /* Extract NAME2 from the (optional) sign-changing cast. */
4480 if (gimple_assign_cast_p (def_stmt
))
4482 if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt
))
4483 && ! TYPE_UNSIGNED (TREE_TYPE (gimple_assign_rhs1 (def_stmt
)))
4484 && (TYPE_PRECISION (gimple_expr_type (def_stmt
))
4485 == TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (def_stmt
)))))
4486 name3
= gimple_assign_rhs1 (def_stmt
);
4489 /* If name3 is used later, create an ASSERT_EXPR for it. */
4490 if (name3
!= NULL_TREE
4491 && TREE_CODE (name3
) == SSA_NAME
4492 && (cst2
== NULL_TREE
4493 || TREE_CODE (cst2
) == INTEGER_CST
)
4494 && INTEGRAL_TYPE_P (TREE_TYPE (name3
))
4495 && live_on_edge (e
, name3
)
4496 && !has_single_use (name3
))
4500 /* Build an expression for the range test. */
4501 tmp
= build1 (NOP_EXPR
, TREE_TYPE (name
), name3
);
4502 if (cst2
!= NULL_TREE
)
4503 tmp
= build2 (PLUS_EXPR
, TREE_TYPE (name
), tmp
, cst2
);
4507 fprintf (dump_file
, "Adding assert for ");
4508 print_generic_expr (dump_file
, name3
, 0);
4509 fprintf (dump_file
, " from ");
4510 print_generic_expr (dump_file
, tmp
, 0);
4511 fprintf (dump_file
, "\n");
4514 register_new_assert_for (name3
, tmp
, comp_code
, val
, NULL
, e
, bsi
);
4519 /* If name2 is used later, create an ASSERT_EXPR for it. */
4520 if (name2
!= NULL_TREE
4521 && TREE_CODE (name2
) == SSA_NAME
4522 && TREE_CODE (cst2
) == INTEGER_CST
4523 && INTEGRAL_TYPE_P (TREE_TYPE (name2
))
4524 && live_on_edge (e
, name2
)
4525 && !has_single_use (name2
))
4529 /* Build an expression for the range test. */
4531 if (TREE_TYPE (name
) != TREE_TYPE (name2
))
4532 tmp
= build1 (NOP_EXPR
, TREE_TYPE (name
), tmp
);
4533 if (cst2
!= NULL_TREE
)
4534 tmp
= build2 (PLUS_EXPR
, TREE_TYPE (name
), tmp
, cst2
);
4538 fprintf (dump_file
, "Adding assert for ");
4539 print_generic_expr (dump_file
, name2
, 0);
4540 fprintf (dump_file
, " from ");
4541 print_generic_expr (dump_file
, tmp
, 0);
4542 fprintf (dump_file
, "\n");
4545 register_new_assert_for (name2
, tmp
, comp_code
, val
, NULL
, e
, bsi
);
4551 if (TREE_CODE_CLASS (comp_code
) == tcc_comparison
4552 && TREE_CODE (val
) == INTEGER_CST
)
4554 gimple def_stmt
= SSA_NAME_DEF_STMT (name
);
4555 tree name2
= NULL_TREE
, names
[2], cst2
= NULL_TREE
;
4556 tree val2
= NULL_TREE
;
4557 double_int mask
= double_int_zero
;
4558 unsigned int prec
= TYPE_PRECISION (TREE_TYPE (val
));
4560 /* Add asserts for NAME cmp CST and NAME being defined
4561 as NAME = (int) NAME2. */
4562 if (!TYPE_UNSIGNED (TREE_TYPE (val
))
4563 && (comp_code
== LE_EXPR
|| comp_code
== LT_EXPR
4564 || comp_code
== GT_EXPR
|| comp_code
== GE_EXPR
)
4565 && gimple_assign_cast_p (def_stmt
))
4567 name2
= gimple_assign_rhs1 (def_stmt
);
4568 if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt
))
4569 && INTEGRAL_TYPE_P (TREE_TYPE (name2
))
4570 && TYPE_UNSIGNED (TREE_TYPE (name2
))
4571 && prec
== TYPE_PRECISION (TREE_TYPE (name2
))
4572 && (comp_code
== LE_EXPR
|| comp_code
== GT_EXPR
4573 || !tree_int_cst_equal (val
,
4574 TYPE_MIN_VALUE (TREE_TYPE (val
))))
4575 && live_on_edge (e
, name2
)
4576 && !has_single_use (name2
))
4579 enum tree_code new_comp_code
= comp_code
;
4581 cst
= fold_convert (TREE_TYPE (name2
),
4582 TYPE_MIN_VALUE (TREE_TYPE (val
)));
4583 /* Build an expression for the range test. */
4584 tmp
= build2 (PLUS_EXPR
, TREE_TYPE (name2
), name2
, cst
);
4585 cst
= fold_build2 (PLUS_EXPR
, TREE_TYPE (name2
), cst
,
4586 fold_convert (TREE_TYPE (name2
), val
));
4587 if (comp_code
== LT_EXPR
|| comp_code
== GE_EXPR
)
4589 new_comp_code
= comp_code
== LT_EXPR
? LE_EXPR
: GT_EXPR
;
4590 cst
= fold_build2 (MINUS_EXPR
, TREE_TYPE (name2
), cst
,
4591 build_int_cst (TREE_TYPE (name2
), 1));
4596 fprintf (dump_file
, "Adding assert for ");
4597 print_generic_expr (dump_file
, name2
, 0);
4598 fprintf (dump_file
, " from ");
4599 print_generic_expr (dump_file
, tmp
, 0);
4600 fprintf (dump_file
, "\n");
4603 register_new_assert_for (name2
, tmp
, new_comp_code
, cst
, NULL
,
4610 /* Add asserts for NAME cmp CST and NAME being defined as
4611 NAME = NAME2 >> CST2.
4613 Extract CST2 from the right shift. */
4614 if (is_gimple_assign (def_stmt
)
4615 && gimple_assign_rhs_code (def_stmt
) == RSHIFT_EXPR
)
4617 name2
= gimple_assign_rhs1 (def_stmt
);
4618 cst2
= gimple_assign_rhs2 (def_stmt
);
4619 if (TREE_CODE (name2
) == SSA_NAME
4620 && host_integerp (cst2
, 1)
4621 && INTEGRAL_TYPE_P (TREE_TYPE (name2
))
4622 && IN_RANGE (tree_low_cst (cst2
, 1), 1, prec
- 1)
4623 && prec
<= HOST_BITS_PER_DOUBLE_INT
4624 && prec
== GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (val
)))
4625 && live_on_edge (e
, name2
)
4626 && !has_single_use (name2
))
4628 mask
= double_int_mask (tree_low_cst (cst2
, 1));
4629 val2
= fold_binary (LSHIFT_EXPR
, TREE_TYPE (val
), val
, cst2
);
4632 if (val2
!= NULL_TREE
4633 && TREE_CODE (val2
) == INTEGER_CST
4634 && simple_cst_equal (fold_build2 (RSHIFT_EXPR
,
4638 enum tree_code new_comp_code
= comp_code
;
4642 if (comp_code
== EQ_EXPR
|| comp_code
== NE_EXPR
)
4644 if (!TYPE_UNSIGNED (TREE_TYPE (val
)))
4646 tree type
= build_nonstandard_integer_type (prec
, 1);
4647 tmp
= build1 (NOP_EXPR
, type
, name2
);
4648 val2
= fold_convert (type
, val2
);
4650 tmp
= fold_build2 (MINUS_EXPR
, TREE_TYPE (tmp
), tmp
, val2
);
4651 new_val
= double_int_to_tree (TREE_TYPE (tmp
), mask
);
4652 new_comp_code
= comp_code
== EQ_EXPR
? LE_EXPR
: GT_EXPR
;
4654 else if (comp_code
== LT_EXPR
|| comp_code
== GE_EXPR
)
4659 = double_int_max_value (prec
, TYPE_UNSIGNED (TREE_TYPE (val
)));
4660 mask
= double_int_ior (tree_to_double_int (val2
), mask
);
4661 if (double_int_equal_p (mask
, maxval
))
4662 new_val
= NULL_TREE
;
4664 new_val
= double_int_to_tree (TREE_TYPE (val2
), mask
);
4671 fprintf (dump_file
, "Adding assert for ");
4672 print_generic_expr (dump_file
, name2
, 0);
4673 fprintf (dump_file
, " from ");
4674 print_generic_expr (dump_file
, tmp
, 0);
4675 fprintf (dump_file
, "\n");
4678 register_new_assert_for (name2
, tmp
, new_comp_code
, new_val
,
4684 /* Add asserts for NAME cmp CST and NAME being defined as
4685 NAME = NAME2 & CST2.
4687 Extract CST2 from the and. */
4688 names
[0] = NULL_TREE
;
4689 names
[1] = NULL_TREE
;
4691 if (is_gimple_assign (def_stmt
)
4692 && gimple_assign_rhs_code (def_stmt
) == BIT_AND_EXPR
)
4694 name2
= gimple_assign_rhs1 (def_stmt
);
4695 cst2
= gimple_assign_rhs2 (def_stmt
);
4696 if (TREE_CODE (name2
) == SSA_NAME
4697 && INTEGRAL_TYPE_P (TREE_TYPE (name2
))
4698 && TREE_CODE (cst2
) == INTEGER_CST
4699 && !integer_zerop (cst2
)
4700 && prec
<= HOST_BITS_PER_DOUBLE_INT
4702 || TYPE_UNSIGNED (TREE_TYPE (val
))))
4704 gimple def_stmt2
= SSA_NAME_DEF_STMT (name2
);
4705 if (gimple_assign_cast_p (def_stmt2
))
4707 names
[1] = gimple_assign_rhs1 (def_stmt2
);
4708 if (!CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt2
))
4709 || !INTEGRAL_TYPE_P (TREE_TYPE (names
[1]))
4710 || (TYPE_PRECISION (TREE_TYPE (name2
))
4711 != TYPE_PRECISION (TREE_TYPE (names
[1])))
4712 || !live_on_edge (e
, names
[1])
4713 || has_single_use (names
[1]))
4714 names
[1] = NULL_TREE
;
4716 if (live_on_edge (e
, name2
)
4717 && !has_single_use (name2
))
4721 if (names
[0] || names
[1])
4723 double_int minv
, maxv
= double_int_zero
, valv
, cst2v
;
4724 double_int tem
, sgnbit
;
4725 bool valid_p
= false, valn
= false, cst2n
= false;
4726 enum tree_code ccode
= comp_code
;
4728 valv
= double_int_zext (tree_to_double_int (val
), prec
);
4729 cst2v
= double_int_zext (tree_to_double_int (cst2
), prec
);
4730 if (!TYPE_UNSIGNED (TREE_TYPE (val
)))
4732 valn
= double_int_negative_p (double_int_sext (valv
, prec
));
4733 cst2n
= double_int_negative_p (double_int_sext (cst2v
, prec
));
4735 /* If CST2 doesn't have most significant bit set,
4736 but VAL is negative, we have comparison like
4737 if ((x & 0x123) > -4) (always true). Just give up. */
4741 sgnbit
= double_int_zext (double_int_lshift (double_int_one
,
4745 sgnbit
= double_int_zero
;
4746 minv
= double_int_and (valv
, cst2v
);
4750 /* Minimum unsigned value for equality is VAL & CST2
4751 (should be equal to VAL, otherwise we probably should
4752 have folded the comparison into false) and
4753 maximum unsigned value is VAL | ~CST2. */
4754 maxv
= double_int_ior (valv
, double_int_not (cst2v
));
4755 maxv
= double_int_zext (maxv
, prec
);
4759 tem
= double_int_ior (valv
, double_int_not (cst2v
));
4760 tem
= double_int_zext (tem
, prec
);
4761 /* If VAL is 0, handle (X & CST2) != 0 as (X & CST2) > 0U. */
4762 if (double_int_zero_p (valv
))
4765 sgnbit
= double_int_zero
;
4768 /* If (VAL | ~CST2) is all ones, handle it as
4769 (X & CST2) < VAL. */
4770 if (double_int_equal_p (tem
, double_int_mask (prec
)))
4774 sgnbit
= double_int_zero
;
4778 && double_int_negative_p (double_int_sext (cst2v
, prec
)))
4779 sgnbit
= double_int_zext (double_int_lshift (double_int_one
,
4782 if (!double_int_zero_p (sgnbit
))
4784 if (double_int_equal_p (valv
, sgnbit
))
4790 if (double_int_equal_p (tem
, double_int_mask (prec
- 1)))
4796 sgnbit
= double_int_zero
;
4800 /* Minimum unsigned value for >= if (VAL & CST2) == VAL
4801 is VAL and maximum unsigned value is ~0. For signed
4802 comparison, if CST2 doesn't have most significant bit
4803 set, handle it similarly. If CST2 has MSB set,
4804 the minimum is the same, and maximum is ~0U/2. */
4805 if (!double_int_equal_p (minv
, valv
))
4807 /* If (VAL & CST2) != VAL, X & CST2 can't be equal to
4809 minv
= masked_increment (valv
, cst2v
, sgnbit
, prec
);
4810 if (double_int_equal_p (minv
, valv
))
4813 maxv
= double_int_mask (prec
- (cst2n
? 1 : 0));
4818 /* Find out smallest MINV where MINV > VAL
4819 && (MINV & CST2) == MINV, if any. If VAL is signed and
4820 CST2 has MSB set, compute it biased by 1 << (prec - 1). */
4821 minv
= masked_increment (valv
, cst2v
, sgnbit
, prec
);
4822 if (double_int_equal_p (minv
, valv
))
4824 maxv
= double_int_mask (prec
- (cst2n
? 1 : 0));
4828 /* Minimum unsigned value for <= is 0 and maximum
4829 unsigned value is VAL | ~CST2 if (VAL & CST2) == VAL.
4830 Otherwise, find smallest VAL2 where VAL2 > VAL
4831 && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
4833 For signed comparison, if CST2 doesn't have most
4834 significant bit set, handle it similarly. If CST2 has
4835 MSB set, the maximum is the same and minimum is INT_MIN. */
4836 if (double_int_equal_p (minv
, valv
))
4840 maxv
= masked_increment (valv
, cst2v
, sgnbit
, prec
);
4841 if (double_int_equal_p (maxv
, valv
))
4843 maxv
= double_int_sub (maxv
, double_int_one
);
4845 maxv
= double_int_ior (maxv
, double_int_not (cst2v
));
4846 maxv
= double_int_zext (maxv
, prec
);
4852 /* Minimum unsigned value for < is 0 and maximum
4853 unsigned value is (VAL-1) | ~CST2 if (VAL & CST2) == VAL.
4854 Otherwise, find smallest VAL2 where VAL2 > VAL
4855 && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
4857 For signed comparison, if CST2 doesn't have most
4858 significant bit set, handle it similarly. If CST2 has
4859 MSB set, the maximum is the same and minimum is INT_MIN. */
4860 if (double_int_equal_p (minv
, valv
))
4862 if (double_int_equal_p (valv
, sgnbit
))
4868 maxv
= masked_increment (valv
, cst2v
, sgnbit
, prec
);
4869 if (double_int_equal_p (maxv
, valv
))
4872 maxv
= double_int_sub (maxv
, double_int_one
);
4873 maxv
= double_int_ior (maxv
, double_int_not (cst2v
));
4874 maxv
= double_int_zext (maxv
, prec
);
4882 && !double_int_equal_p (double_int_zext (double_int_sub (maxv
,
4885 double_int_mask (prec
)))
4887 tree tmp
, new_val
, type
;
4890 for (i
= 0; i
< 2; i
++)
4893 double_int maxv2
= maxv
;
4895 type
= TREE_TYPE (names
[i
]);
4896 if (!TYPE_UNSIGNED (type
))
4898 type
= build_nonstandard_integer_type (prec
, 1);
4899 tmp
= build1 (NOP_EXPR
, type
, names
[i
]);
4901 if (!double_int_zero_p (minv
))
4903 tmp
= build2 (PLUS_EXPR
, type
, tmp
,
4904 double_int_to_tree (type
,
4905 double_int_neg (minv
)));
4906 maxv2
= double_int_sub (maxv
, minv
);
4908 new_val
= double_int_to_tree (type
, maxv2
);
4912 fprintf (dump_file
, "Adding assert for ");
4913 print_generic_expr (dump_file
, names
[i
], 0);
4914 fprintf (dump_file
, " from ");
4915 print_generic_expr (dump_file
, tmp
, 0);
4916 fprintf (dump_file
, "\n");
4919 register_new_assert_for (names
[i
], tmp
, LE_EXPR
,
4920 new_val
, NULL
, e
, bsi
);
4930 /* OP is an operand of a truth value expression which is known to have
4931 a particular value. Register any asserts for OP and for any
4932 operands in OP's defining statement.
4934 If CODE is EQ_EXPR, then we want to register OP is zero (false),
4935 if CODE is NE_EXPR, then we want to register OP is nonzero (true). */
4938 register_edge_assert_for_1 (tree op
, enum tree_code code
,
4939 edge e
, gimple_stmt_iterator bsi
)
4941 bool retval
= false;
4944 enum tree_code rhs_code
;
4946 /* We only care about SSA_NAMEs. */
4947 if (TREE_CODE (op
) != SSA_NAME
)
4950 /* We know that OP will have a zero or nonzero value. If OP is used
4951 more than once go ahead and register an assert for OP.
4953 The FOUND_IN_SUBGRAPH support is not helpful in this situation as
4954 it will always be set for OP (because OP is used in a COND_EXPR in
4956 if (!has_single_use (op
))
4958 val
= build_int_cst (TREE_TYPE (op
), 0);
4959 register_new_assert_for (op
, op
, code
, val
, NULL
, e
, bsi
);
4963 /* Now look at how OP is set. If it's set from a comparison,
4964 a truth operation or some bit operations, then we may be able
4965 to register information about the operands of that assignment. */
4966 op_def
= SSA_NAME_DEF_STMT (op
);
4967 if (gimple_code (op_def
) != GIMPLE_ASSIGN
)
4970 rhs_code
= gimple_assign_rhs_code (op_def
);
4972 if (TREE_CODE_CLASS (rhs_code
) == tcc_comparison
)
4974 bool invert
= (code
== EQ_EXPR
? true : false);
4975 tree op0
= gimple_assign_rhs1 (op_def
);
4976 tree op1
= gimple_assign_rhs2 (op_def
);
4978 if (TREE_CODE (op0
) == SSA_NAME
)
4979 retval
|= register_edge_assert_for_2 (op0
, e
, bsi
, rhs_code
, op0
, op1
,
4981 if (TREE_CODE (op1
) == SSA_NAME
)
4982 retval
|= register_edge_assert_for_2 (op1
, e
, bsi
, rhs_code
, op0
, op1
,
4985 else if ((code
== NE_EXPR
4986 && gimple_assign_rhs_code (op_def
) == BIT_AND_EXPR
)
4988 && gimple_assign_rhs_code (op_def
) == BIT_IOR_EXPR
))
4990 /* Recurse on each operand. */
4991 retval
|= register_edge_assert_for_1 (gimple_assign_rhs1 (op_def
),
4993 retval
|= register_edge_assert_for_1 (gimple_assign_rhs2 (op_def
),
4996 else if (gimple_assign_rhs_code (op_def
) == BIT_NOT_EXPR
4997 && TYPE_PRECISION (TREE_TYPE (gimple_assign_lhs (op_def
))) == 1)
4999 /* Recurse, flipping CODE. */
5000 code
= invert_tree_comparison (code
, false);
5001 retval
|= register_edge_assert_for_1 (gimple_assign_rhs1 (op_def
),
5004 else if (gimple_assign_rhs_code (op_def
) == SSA_NAME
)
5006 /* Recurse through the copy. */
5007 retval
|= register_edge_assert_for_1 (gimple_assign_rhs1 (op_def
),
5010 else if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (op_def
)))
5012 /* Recurse through the type conversion. */
5013 retval
|= register_edge_assert_for_1 (gimple_assign_rhs1 (op_def
),
5020 /* Try to register an edge assertion for SSA name NAME on edge E for
5021 the condition COND contributing to the conditional jump pointed to by SI.
5022 Return true if an assertion for NAME could be registered. */
5025 register_edge_assert_for (tree name
, edge e
, gimple_stmt_iterator si
,
5026 enum tree_code cond_code
, tree cond_op0
,
5030 enum tree_code comp_code
;
5031 bool retval
= false;
5032 bool is_else_edge
= (e
->flags
& EDGE_FALSE_VALUE
) != 0;
5034 /* Do not attempt to infer anything in names that flow through
5036 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name
))
5039 if (!extract_code_and_val_from_cond_with_ops (name
, cond_code
,
5045 /* Register ASSERT_EXPRs for name. */
5046 retval
|= register_edge_assert_for_2 (name
, e
, si
, cond_code
, cond_op0
,
5047 cond_op1
, is_else_edge
);
5050 /* If COND is effectively an equality test of an SSA_NAME against
5051 the value zero or one, then we may be able to assert values
5052 for SSA_NAMEs which flow into COND. */
5054 /* In the case of NAME == 1 or NAME != 0, for BIT_AND_EXPR defining
5055 statement of NAME we can assert both operands of the BIT_AND_EXPR
5056 have nonzero value. */
5057 if (((comp_code
== EQ_EXPR
&& integer_onep (val
))
5058 || (comp_code
== NE_EXPR
&& integer_zerop (val
))))
5060 gimple def_stmt
= SSA_NAME_DEF_STMT (name
);
5062 if (is_gimple_assign (def_stmt
)
5063 && gimple_assign_rhs_code (def_stmt
) == BIT_AND_EXPR
)
5065 tree op0
= gimple_assign_rhs1 (def_stmt
);
5066 tree op1
= gimple_assign_rhs2 (def_stmt
);
5067 retval
|= register_edge_assert_for_1 (op0
, NE_EXPR
, e
, si
);
5068 retval
|= register_edge_assert_for_1 (op1
, NE_EXPR
, e
, si
);
5072 /* In the case of NAME == 0 or NAME != 1, for BIT_IOR_EXPR defining
5073 statement of NAME we can assert both operands of the BIT_IOR_EXPR
5075 if (((comp_code
== EQ_EXPR
&& integer_zerop (val
))
5076 || (comp_code
== NE_EXPR
&& integer_onep (val
))))
5078 gimple def_stmt
= SSA_NAME_DEF_STMT (name
);
5080 /* For BIT_IOR_EXPR only if NAME == 0 both operands have
5081 necessarily zero value, or if type-precision is one. */
5082 if (is_gimple_assign (def_stmt
)
5083 && (gimple_assign_rhs_code (def_stmt
) == BIT_IOR_EXPR
5084 && (TYPE_PRECISION (TREE_TYPE (name
)) == 1
5085 || comp_code
== EQ_EXPR
)))
5087 tree op0
= gimple_assign_rhs1 (def_stmt
);
5088 tree op1
= gimple_assign_rhs2 (def_stmt
);
5089 retval
|= register_edge_assert_for_1 (op0
, EQ_EXPR
, e
, si
);
5090 retval
|= register_edge_assert_for_1 (op1
, EQ_EXPR
, e
, si
);
5098 /* Determine whether the outgoing edges of BB should receive an
5099 ASSERT_EXPR for each of the operands of BB's LAST statement.
5100 The last statement of BB must be a COND_EXPR.
5102 If any of the sub-graphs rooted at BB have an interesting use of
5103 the predicate operands, an assert location node is added to the
5104 list of assertions for the corresponding operands. */
5107 find_conditional_asserts (basic_block bb
, gimple last
)
5110 gimple_stmt_iterator bsi
;
5116 need_assert
= false;
5117 bsi
= gsi_for_stmt (last
);
5119 /* Look for uses of the operands in each of the sub-graphs
5120 rooted at BB. We need to check each of the outgoing edges
5121 separately, so that we know what kind of ASSERT_EXPR to
5123 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
5128 /* Register the necessary assertions for each operand in the
5129 conditional predicate. */
5130 FOR_EACH_SSA_TREE_OPERAND (op
, last
, iter
, SSA_OP_USE
)
5132 need_assert
|= register_edge_assert_for (op
, e
, bsi
,
5133 gimple_cond_code (last
),
5134 gimple_cond_lhs (last
),
5135 gimple_cond_rhs (last
));
5148 /* Compare two case labels sorting first by the destination bb index
5149 and then by the case value. */
5152 compare_case_labels (const void *p1
, const void *p2
)
5154 const struct case_info
*ci1
= (const struct case_info
*) p1
;
5155 const struct case_info
*ci2
= (const struct case_info
*) p2
;
5156 int idx1
= ci1
->bb
->index
;
5157 int idx2
= ci2
->bb
->index
;
5161 else if (idx1
== idx2
)
5163 /* Make sure the default label is first in a group. */
5164 if (!CASE_LOW (ci1
->expr
))
5166 else if (!CASE_LOW (ci2
->expr
))
5169 return tree_int_cst_compare (CASE_LOW (ci1
->expr
),
5170 CASE_LOW (ci2
->expr
));
5176 /* Determine whether the outgoing edges of BB should receive an
5177 ASSERT_EXPR for each of the operands of BB's LAST statement.
5178 The last statement of BB must be a SWITCH_EXPR.
5180 If any of the sub-graphs rooted at BB have an interesting use of
5181 the predicate operands, an assert location node is added to the
5182 list of assertions for the corresponding operands. */
5185 find_switch_asserts (basic_block bb
, gimple last
)
5188 gimple_stmt_iterator bsi
;
5191 struct case_info
*ci
;
5192 size_t n
= gimple_switch_num_labels (last
);
5193 #if GCC_VERSION >= 4000
5196 /* Work around GCC 3.4 bug (PR 37086). */
5197 volatile unsigned int idx
;
5200 need_assert
= false;
5201 bsi
= gsi_for_stmt (last
);
5202 op
= gimple_switch_index (last
);
5203 if (TREE_CODE (op
) != SSA_NAME
)
5206 /* Build a vector of case labels sorted by destination label. */
5207 ci
= XNEWVEC (struct case_info
, n
);
5208 for (idx
= 0; idx
< n
; ++idx
)
5210 ci
[idx
].expr
= gimple_switch_label (last
, idx
);
5211 ci
[idx
].bb
= label_to_block (CASE_LABEL (ci
[idx
].expr
));
5213 qsort (ci
, n
, sizeof (struct case_info
), compare_case_labels
);
5215 for (idx
= 0; idx
< n
; ++idx
)
5218 tree cl
= ci
[idx
].expr
;
5219 basic_block cbb
= ci
[idx
].bb
;
5221 min
= CASE_LOW (cl
);
5222 max
= CASE_HIGH (cl
);
5224 /* If there are multiple case labels with the same destination
5225 we need to combine them to a single value range for the edge. */
5226 if (idx
+ 1 < n
&& cbb
== ci
[idx
+ 1].bb
)
5228 /* Skip labels until the last of the group. */
5231 } while (idx
< n
&& cbb
== ci
[idx
].bb
);
5234 /* Pick up the maximum of the case label range. */
5235 if (CASE_HIGH (ci
[idx
].expr
))
5236 max
= CASE_HIGH (ci
[idx
].expr
);
5238 max
= CASE_LOW (ci
[idx
].expr
);
5241 /* Nothing to do if the range includes the default label until we
5242 can register anti-ranges. */
5243 if (min
== NULL_TREE
)
5246 /* Find the edge to register the assert expr on. */
5247 e
= find_edge (bb
, cbb
);
5249 /* Register the necessary assertions for the operand in the
5251 need_assert
|= register_edge_assert_for (op
, e
, bsi
,
5252 max
? GE_EXPR
: EQ_EXPR
,
5254 fold_convert (TREE_TYPE (op
),
5258 need_assert
|= register_edge_assert_for (op
, e
, bsi
, LE_EXPR
,
5260 fold_convert (TREE_TYPE (op
),
5270 /* Traverse all the statements in block BB looking for statements that
5271 may generate useful assertions for the SSA names in their operand.
5272 If a statement produces a useful assertion A for name N_i, then the
5273 list of assertions already generated for N_i is scanned to
5274 determine if A is actually needed.
5276 If N_i already had the assertion A at a location dominating the
5277 current location, then nothing needs to be done. Otherwise, the
5278 new location for A is recorded instead.
5280 1- For every statement S in BB, all the variables used by S are
5281 added to bitmap FOUND_IN_SUBGRAPH.
5283 2- If statement S uses an operand N in a way that exposes a known
5284 value range for N, then if N was not already generated by an
5285 ASSERT_EXPR, create a new assert location for N. For instance,
5286 if N is a pointer and the statement dereferences it, we can
5287 assume that N is not NULL.
5289 3- COND_EXPRs are a special case of #2. We can derive range
5290 information from the predicate but need to insert different
5291 ASSERT_EXPRs for each of the sub-graphs rooted at the
5292 conditional block. If the last statement of BB is a conditional
5293 expression of the form 'X op Y', then
5295 a) Remove X and Y from the set FOUND_IN_SUBGRAPH.
5297 b) If the conditional is the only entry point to the sub-graph
5298 corresponding to the THEN_CLAUSE, recurse into it. On
5299 return, if X and/or Y are marked in FOUND_IN_SUBGRAPH, then
5300 an ASSERT_EXPR is added for the corresponding variable.
5302 c) Repeat step (b) on the ELSE_CLAUSE.
5304 d) Mark X and Y in FOUND_IN_SUBGRAPH.
5313 In this case, an assertion on the THEN clause is useful to
5314 determine that 'a' is always 9 on that edge. However, an assertion
5315 on the ELSE clause would be unnecessary.
5317 4- If BB does not end in a conditional expression, then we recurse
5318 into BB's dominator children.
5320 At the end of the recursive traversal, every SSA name will have a
5321 list of locations where ASSERT_EXPRs should be added. When a new
5322 location for name N is found, it is registered by calling
5323 register_new_assert_for. That function keeps track of all the
5324 registered assertions to prevent adding unnecessary assertions.
5325 For instance, if a pointer P_4 is dereferenced more than once in a
5326 dominator tree, only the location dominating all the dereference of
5327 P_4 will receive an ASSERT_EXPR.
5329 If this function returns true, then it means that there are names
5330 for which we need to generate ASSERT_EXPRs. Those assertions are
5331 inserted by process_assert_insertions. */
5334 find_assert_locations_1 (basic_block bb
, sbitmap live
)
5336 gimple_stmt_iterator si
;
5341 need_assert
= false;
5342 last
= last_stmt (bb
);
5344 /* If BB's last statement is a conditional statement involving integer
5345 operands, determine if we need to add ASSERT_EXPRs. */
5347 && gimple_code (last
) == GIMPLE_COND
5348 && !fp_predicate (last
)
5349 && !ZERO_SSA_OPERANDS (last
, SSA_OP_USE
))
5350 need_assert
|= find_conditional_asserts (bb
, last
);
5352 /* If BB's last statement is a switch statement involving integer
5353 operands, determine if we need to add ASSERT_EXPRs. */
5355 && gimple_code (last
) == GIMPLE_SWITCH
5356 && !ZERO_SSA_OPERANDS (last
, SSA_OP_USE
))
5357 need_assert
|= find_switch_asserts (bb
, last
);
5359 /* Traverse all the statements in BB marking used names and looking
5360 for statements that may infer assertions for their used operands. */
5361 for (si
= gsi_start_bb (bb
); !gsi_end_p (si
); gsi_next (&si
))
5367 stmt
= gsi_stmt (si
);
5369 if (is_gimple_debug (stmt
))
5372 /* See if we can derive an assertion for any of STMT's operands. */
5373 FOR_EACH_SSA_TREE_OPERAND (op
, stmt
, i
, SSA_OP_USE
)
5376 enum tree_code comp_code
;
5378 /* Mark OP in our live bitmap. */
5379 SET_BIT (live
, SSA_NAME_VERSION (op
));
5381 /* If OP is used in such a way that we can infer a value
5382 range for it, and we don't find a previous assertion for
5383 it, create a new assertion location node for OP. */
5384 if (infer_value_range (stmt
, op
, &comp_code
, &value
))
5386 /* If we are able to infer a nonzero value range for OP,
5387 then walk backwards through the use-def chain to see if OP
5388 was set via a typecast.
5390 If so, then we can also infer a nonzero value range
5391 for the operand of the NOP_EXPR. */
5392 if (comp_code
== NE_EXPR
&& integer_zerop (value
))
5395 gimple def_stmt
= SSA_NAME_DEF_STMT (t
);
5397 while (is_gimple_assign (def_stmt
)
5398 && gimple_assign_rhs_code (def_stmt
) == NOP_EXPR
5400 (gimple_assign_rhs1 (def_stmt
)) == SSA_NAME
5402 (TREE_TYPE (gimple_assign_rhs1 (def_stmt
))))
5404 t
= gimple_assign_rhs1 (def_stmt
);
5405 def_stmt
= SSA_NAME_DEF_STMT (t
);
5407 /* Note we want to register the assert for the
5408 operand of the NOP_EXPR after SI, not after the
5410 if (! has_single_use (t
))
5412 register_new_assert_for (t
, t
, comp_code
, value
,
5419 /* If OP is used only once, namely in this STMT, don't
5420 bother creating an ASSERT_EXPR for it. Such an
5421 ASSERT_EXPR would do nothing but increase compile time. */
5422 if (!has_single_use (op
))
5424 register_new_assert_for (op
, op
, comp_code
, value
,
5432 /* Traverse all PHI nodes in BB marking used operands. */
5433 for (si
= gsi_start_phis (bb
); !gsi_end_p(si
); gsi_next (&si
))
5435 use_operand_p arg_p
;
5437 phi
= gsi_stmt (si
);
5439 FOR_EACH_PHI_ARG (arg_p
, phi
, i
, SSA_OP_USE
)
5441 tree arg
= USE_FROM_PTR (arg_p
);
5442 if (TREE_CODE (arg
) == SSA_NAME
)
5443 SET_BIT (live
, SSA_NAME_VERSION (arg
));
5450 /* Do an RPO walk over the function computing SSA name liveness
5451 on-the-fly and deciding on assert expressions to insert.
5452 Returns true if there are assert expressions to be inserted. */
5455 find_assert_locations (void)
5457 int *rpo
= XCNEWVEC (int, last_basic_block
+ NUM_FIXED_BLOCKS
);
5458 int *bb_rpo
= XCNEWVEC (int, last_basic_block
+ NUM_FIXED_BLOCKS
);
5459 int *last_rpo
= XCNEWVEC (int, last_basic_block
+ NUM_FIXED_BLOCKS
);
5463 live
= XCNEWVEC (sbitmap
, last_basic_block
+ NUM_FIXED_BLOCKS
);
5464 rpo_cnt
= pre_and_rev_post_order_compute (NULL
, rpo
, false);
5465 for (i
= 0; i
< rpo_cnt
; ++i
)
5468 need_asserts
= false;
5469 for (i
= rpo_cnt
-1; i
>= 0; --i
)
5471 basic_block bb
= BASIC_BLOCK (rpo
[i
]);
5477 live
[rpo
[i
]] = sbitmap_alloc (num_ssa_names
);
5478 sbitmap_zero (live
[rpo
[i
]]);
5481 /* Process BB and update the live information with uses in
5483 need_asserts
|= find_assert_locations_1 (bb
, live
[rpo
[i
]]);
5485 /* Merge liveness into the predecessor blocks and free it. */
5486 if (!sbitmap_empty_p (live
[rpo
[i
]]))
5489 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
5491 int pred
= e
->src
->index
;
5492 if (e
->flags
& EDGE_DFS_BACK
)
5497 live
[pred
] = sbitmap_alloc (num_ssa_names
);
5498 sbitmap_zero (live
[pred
]);
5500 sbitmap_a_or_b (live
[pred
], live
[pred
], live
[rpo
[i
]]);
5502 if (bb_rpo
[pred
] < pred_rpo
)
5503 pred_rpo
= bb_rpo
[pred
];
5506 /* Record the RPO number of the last visited block that needs
5507 live information from this block. */
5508 last_rpo
[rpo
[i
]] = pred_rpo
;
5512 sbitmap_free (live
[rpo
[i
]]);
5513 live
[rpo
[i
]] = NULL
;
5516 /* We can free all successors live bitmaps if all their
5517 predecessors have been visited already. */
5518 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
5519 if (last_rpo
[e
->dest
->index
] == i
5520 && live
[e
->dest
->index
])
5522 sbitmap_free (live
[e
->dest
->index
]);
5523 live
[e
->dest
->index
] = NULL
;
5528 XDELETEVEC (bb_rpo
);
5529 XDELETEVEC (last_rpo
);
5530 for (i
= 0; i
< last_basic_block
+ NUM_FIXED_BLOCKS
; ++i
)
5532 sbitmap_free (live
[i
]);
5535 return need_asserts
;
5538 /* Create an ASSERT_EXPR for NAME and insert it in the location
5539 indicated by LOC. Return true if we made any edge insertions. */
5542 process_assert_insertions_for (tree name
, assert_locus_t loc
)
5544 /* Build the comparison expression NAME_i COMP_CODE VAL. */
5551 /* If we have X <=> X do not insert an assert expr for that. */
5552 if (loc
->expr
== loc
->val
)
5555 cond
= build2 (loc
->comp_code
, boolean_type_node
, loc
->expr
, loc
->val
);
5556 assert_stmt
= build_assert_expr_for (cond
, name
);
5559 /* We have been asked to insert the assertion on an edge. This
5560 is used only by COND_EXPR and SWITCH_EXPR assertions. */
5561 gcc_checking_assert (gimple_code (gsi_stmt (loc
->si
)) == GIMPLE_COND
5562 || (gimple_code (gsi_stmt (loc
->si
))
5565 gsi_insert_on_edge (loc
->e
, assert_stmt
);
5569 /* Otherwise, we can insert right after LOC->SI iff the
5570 statement must not be the last statement in the block. */
5571 stmt
= gsi_stmt (loc
->si
);
5572 if (!stmt_ends_bb_p (stmt
))
5574 gsi_insert_after (&loc
->si
, assert_stmt
, GSI_SAME_STMT
);
5578 /* If STMT must be the last statement in BB, we can only insert new
5579 assertions on the non-abnormal edge out of BB. Note that since
5580 STMT is not control flow, there may only be one non-abnormal edge
5582 FOR_EACH_EDGE (e
, ei
, loc
->bb
->succs
)
5583 if (!(e
->flags
& EDGE_ABNORMAL
))
5585 gsi_insert_on_edge (e
, assert_stmt
);
5593 /* Process all the insertions registered for every name N_i registered
5594 in NEED_ASSERT_FOR. The list of assertions to be inserted are
5595 found in ASSERTS_FOR[i]. */
5598 process_assert_insertions (void)
5602 bool update_edges_p
= false;
5603 int num_asserts
= 0;
5605 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
5606 dump_all_asserts (dump_file
);
5608 EXECUTE_IF_SET_IN_BITMAP (need_assert_for
, 0, i
, bi
)
5610 assert_locus_t loc
= asserts_for
[i
];
5615 assert_locus_t next
= loc
->next
;
5616 update_edges_p
|= process_assert_insertions_for (ssa_name (i
), loc
);
5624 gsi_commit_edge_inserts ();
5626 statistics_counter_event (cfun
, "Number of ASSERT_EXPR expressions inserted",
5631 /* Traverse the flowgraph looking for conditional jumps to insert range
5632 expressions. These range expressions are meant to provide information
5633 to optimizations that need to reason in terms of value ranges. They
5634 will not be expanded into RTL. For instance, given:
5643 this pass will transform the code into:
5649 x = ASSERT_EXPR <x, x < y>
5654 y = ASSERT_EXPR <y, x <= y>
5658 The idea is that once copy and constant propagation have run, other
5659 optimizations will be able to determine what ranges of values can 'x'
5660 take in different paths of the code, simply by checking the reaching
5661 definition of 'x'. */
5664 insert_range_assertions (void)
5666 need_assert_for
= BITMAP_ALLOC (NULL
);
5667 asserts_for
= XCNEWVEC (assert_locus_t
, num_ssa_names
);
5669 calculate_dominance_info (CDI_DOMINATORS
);
5671 if (find_assert_locations ())
5673 process_assert_insertions ();
5674 update_ssa (TODO_update_ssa_no_phi
);
5677 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
5679 fprintf (dump_file
, "\nSSA form after inserting ASSERT_EXPRs\n");
5680 dump_function_to_file (current_function_decl
, dump_file
, dump_flags
);
5684 BITMAP_FREE (need_assert_for
);
5687 /* Checks one ARRAY_REF in REF, located at LOCUS. Ignores flexible arrays
5688 and "struct" hacks. If VRP can determine that the
5689 array subscript is a constant, check if it is outside valid
5690 range. If the array subscript is a RANGE, warn if it is
5691 non-overlapping with valid range.
5692 IGNORE_OFF_BY_ONE is true if the ARRAY_REF is inside a ADDR_EXPR. */
5695 check_array_ref (location_t location
, tree ref
, bool ignore_off_by_one
)
5697 value_range_t
* vr
= NULL
;
5698 tree low_sub
, up_sub
;
5699 tree low_bound
, up_bound
, up_bound_p1
;
5702 if (TREE_NO_WARNING (ref
))
5705 low_sub
= up_sub
= TREE_OPERAND (ref
, 1);
5706 up_bound
= array_ref_up_bound (ref
);
5708 /* Can not check flexible arrays. */
5710 || TREE_CODE (up_bound
) != INTEGER_CST
)
5713 /* Accesses to trailing arrays via pointers may access storage
5714 beyond the types array bounds. */
5715 base
= get_base_address (ref
);
5716 if (base
&& TREE_CODE (base
) == MEM_REF
)
5718 tree cref
, next
= NULL_TREE
;
5720 if (TREE_CODE (TREE_OPERAND (ref
, 0)) != COMPONENT_REF
)
5723 cref
= TREE_OPERAND (ref
, 0);
5724 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (cref
, 0))) == RECORD_TYPE
)
5725 for (next
= DECL_CHAIN (TREE_OPERAND (cref
, 1));
5726 next
&& TREE_CODE (next
) != FIELD_DECL
;
5727 next
= DECL_CHAIN (next
))
5730 /* If this is the last field in a struct type or a field in a
5731 union type do not warn. */
5736 low_bound
= array_ref_low_bound (ref
);
5737 up_bound_p1
= int_const_binop (PLUS_EXPR
, up_bound
, integer_one_node
);
5739 if (TREE_CODE (low_sub
) == SSA_NAME
)
5741 vr
= get_value_range (low_sub
);
5742 if (vr
->type
== VR_RANGE
|| vr
->type
== VR_ANTI_RANGE
)
5744 low_sub
= vr
->type
== VR_RANGE
? vr
->max
: vr
->min
;
5745 up_sub
= vr
->type
== VR_RANGE
? vr
->min
: vr
->max
;
5749 if (vr
&& vr
->type
== VR_ANTI_RANGE
)
5751 if (TREE_CODE (up_sub
) == INTEGER_CST
5752 && tree_int_cst_lt (up_bound
, up_sub
)
5753 && TREE_CODE (low_sub
) == INTEGER_CST
5754 && tree_int_cst_lt (low_sub
, low_bound
))
5756 warning_at (location
, OPT_Warray_bounds
,
5757 "array subscript is outside array bounds");
5758 TREE_NO_WARNING (ref
) = 1;
5761 else if (TREE_CODE (up_sub
) == INTEGER_CST
5762 && (ignore_off_by_one
5763 ? (tree_int_cst_lt (up_bound
, up_sub
)
5764 && !tree_int_cst_equal (up_bound_p1
, up_sub
))
5765 : (tree_int_cst_lt (up_bound
, up_sub
)
5766 || tree_int_cst_equal (up_bound_p1
, up_sub
))))
5768 warning_at (location
, OPT_Warray_bounds
,
5769 "array subscript is above array bounds");
5770 TREE_NO_WARNING (ref
) = 1;
5772 else if (TREE_CODE (low_sub
) == INTEGER_CST
5773 && tree_int_cst_lt (low_sub
, low_bound
))
5775 warning_at (location
, OPT_Warray_bounds
,
5776 "array subscript is below array bounds");
5777 TREE_NO_WARNING (ref
) = 1;
5781 /* Searches if the expr T, located at LOCATION computes
5782 address of an ARRAY_REF, and call check_array_ref on it. */
5785 search_for_addr_array (tree t
, location_t location
)
5787 while (TREE_CODE (t
) == SSA_NAME
)
5789 gimple g
= SSA_NAME_DEF_STMT (t
);
5791 if (gimple_code (g
) != GIMPLE_ASSIGN
)
5794 if (get_gimple_rhs_class (gimple_assign_rhs_code (g
))
5795 != GIMPLE_SINGLE_RHS
)
5798 t
= gimple_assign_rhs1 (g
);
5802 /* We are only interested in addresses of ARRAY_REF's. */
5803 if (TREE_CODE (t
) != ADDR_EXPR
)
5806 /* Check each ARRAY_REFs in the reference chain. */
5809 if (TREE_CODE (t
) == ARRAY_REF
)
5810 check_array_ref (location
, t
, true /*ignore_off_by_one*/);
5812 t
= TREE_OPERAND (t
, 0);
5814 while (handled_component_p (t
));
5816 if (TREE_CODE (t
) == MEM_REF
5817 && TREE_CODE (TREE_OPERAND (t
, 0)) == ADDR_EXPR
5818 && !TREE_NO_WARNING (t
))
5820 tree tem
= TREE_OPERAND (TREE_OPERAND (t
, 0), 0);
5821 tree low_bound
, up_bound
, el_sz
;
5823 if (TREE_CODE (TREE_TYPE (tem
)) != ARRAY_TYPE
5824 || TREE_CODE (TREE_TYPE (TREE_TYPE (tem
))) == ARRAY_TYPE
5825 || !TYPE_DOMAIN (TREE_TYPE (tem
)))
5828 low_bound
= TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (tem
)));
5829 up_bound
= TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (tem
)));
5830 el_sz
= TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (tem
)));
5832 || TREE_CODE (low_bound
) != INTEGER_CST
5834 || TREE_CODE (up_bound
) != INTEGER_CST
5836 || TREE_CODE (el_sz
) != INTEGER_CST
)
5839 idx
= mem_ref_offset (t
);
5840 idx
= double_int_sdiv (idx
, tree_to_double_int (el_sz
), TRUNC_DIV_EXPR
);
5841 if (double_int_scmp (idx
, double_int_zero
) < 0)
5843 warning_at (location
, OPT_Warray_bounds
,
5844 "array subscript is below array bounds");
5845 TREE_NO_WARNING (t
) = 1;
5847 else if (double_int_scmp (idx
,
5850 (tree_to_double_int (up_bound
),
5852 (tree_to_double_int (low_bound
))),
5853 double_int_one
)) > 0)
5855 warning_at (location
, OPT_Warray_bounds
,
5856 "array subscript is above array bounds");
5857 TREE_NO_WARNING (t
) = 1;
5862 /* walk_tree() callback that checks if *TP is
5863 an ARRAY_REF inside an ADDR_EXPR (in which an array
5864 subscript one outside the valid range is allowed). Call
5865 check_array_ref for each ARRAY_REF found. The location is
5869 check_array_bounds (tree
*tp
, int *walk_subtree
, void *data
)
5872 struct walk_stmt_info
*wi
= (struct walk_stmt_info
*) data
;
5873 location_t location
;
5875 if (EXPR_HAS_LOCATION (t
))
5876 location
= EXPR_LOCATION (t
);
5879 location_t
*locp
= (location_t
*) wi
->info
;
5883 *walk_subtree
= TRUE
;
5885 if (TREE_CODE (t
) == ARRAY_REF
)
5886 check_array_ref (location
, t
, false /*ignore_off_by_one*/);
5888 if (TREE_CODE (t
) == MEM_REF
5889 || (TREE_CODE (t
) == RETURN_EXPR
&& TREE_OPERAND (t
, 0)))
5890 search_for_addr_array (TREE_OPERAND (t
, 0), location
);
5892 if (TREE_CODE (t
) == ADDR_EXPR
)
5893 *walk_subtree
= FALSE
;
5898 /* Walk over all statements of all reachable BBs and call check_array_bounds
5902 check_all_array_refs (void)
5905 gimple_stmt_iterator si
;
5911 bool executable
= false;
5913 /* Skip blocks that were found to be unreachable. */
5914 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
5915 executable
|= !!(e
->flags
& EDGE_EXECUTABLE
);
5919 for (si
= gsi_start_bb (bb
); !gsi_end_p (si
); gsi_next (&si
))
5921 gimple stmt
= gsi_stmt (si
);
5922 struct walk_stmt_info wi
;
5923 if (!gimple_has_location (stmt
))
5926 if (is_gimple_call (stmt
))
5929 size_t n
= gimple_call_num_args (stmt
);
5930 for (i
= 0; i
< n
; i
++)
5932 tree arg
= gimple_call_arg (stmt
, i
);
5933 search_for_addr_array (arg
, gimple_location (stmt
));
5938 memset (&wi
, 0, sizeof (wi
));
5939 wi
.info
= CONST_CAST (void *, (const void *)
5940 gimple_location_ptr (stmt
));
5942 walk_gimple_op (gsi_stmt (si
),
5950 /* Convert range assertion expressions into the implied copies and
5951 copy propagate away the copies. Doing the trivial copy propagation
5952 here avoids the need to run the full copy propagation pass after
5955 FIXME, this will eventually lead to copy propagation removing the
5956 names that had useful range information attached to them. For
5957 instance, if we had the assertion N_i = ASSERT_EXPR <N_j, N_j > 3>,
5958 then N_i will have the range [3, +INF].
5960 However, by converting the assertion into the implied copy
5961 operation N_i = N_j, we will then copy-propagate N_j into the uses
5962 of N_i and lose the range information. We may want to hold on to
5963 ASSERT_EXPRs a little while longer as the ranges could be used in
5964 things like jump threading.
5966 The problem with keeping ASSERT_EXPRs around is that passes after
5967 VRP need to handle them appropriately.
5969 Another approach would be to make the range information a first
5970 class property of the SSA_NAME so that it can be queried from
5971 any pass. This is made somewhat more complex by the need for
5972 multiple ranges to be associated with one SSA_NAME. */
5975 remove_range_assertions (void)
5978 gimple_stmt_iterator si
;
5980 /* Note that the BSI iterator bump happens at the bottom of the
5981 loop and no bump is necessary if we're removing the statement
5982 referenced by the current BSI. */
5984 for (si
= gsi_start_bb (bb
); !gsi_end_p (si
);)
5986 gimple stmt
= gsi_stmt (si
);
5989 if (is_gimple_assign (stmt
)
5990 && gimple_assign_rhs_code (stmt
) == ASSERT_EXPR
)
5992 tree rhs
= gimple_assign_rhs1 (stmt
);
5994 tree cond
= fold (ASSERT_EXPR_COND (rhs
));
5995 use_operand_p use_p
;
5996 imm_use_iterator iter
;
5998 gcc_assert (cond
!= boolean_false_node
);
6000 /* Propagate the RHS into every use of the LHS. */
6001 var
= ASSERT_EXPR_VAR (rhs
);
6002 FOR_EACH_IMM_USE_STMT (use_stmt
, iter
,
6003 gimple_assign_lhs (stmt
))
6004 FOR_EACH_IMM_USE_ON_STMT (use_p
, iter
)
6006 SET_USE (use_p
, var
);
6007 gcc_assert (TREE_CODE (var
) == SSA_NAME
);
6010 /* And finally, remove the copy, it is not needed. */
6011 gsi_remove (&si
, true);
6012 release_defs (stmt
);
6020 /* Return true if STMT is interesting for VRP. */
6023 stmt_interesting_for_vrp (gimple stmt
)
6025 if (gimple_code (stmt
) == GIMPLE_PHI
6026 && is_gimple_reg (gimple_phi_result (stmt
))
6027 && (INTEGRAL_TYPE_P (TREE_TYPE (gimple_phi_result (stmt
)))
6028 || POINTER_TYPE_P (TREE_TYPE (gimple_phi_result (stmt
)))))
6030 else if (is_gimple_assign (stmt
) || is_gimple_call (stmt
))
6032 tree lhs
= gimple_get_lhs (stmt
);
6034 /* In general, assignments with virtual operands are not useful
6035 for deriving ranges, with the obvious exception of calls to
6036 builtin functions. */
6037 if (lhs
&& TREE_CODE (lhs
) == SSA_NAME
6038 && (INTEGRAL_TYPE_P (TREE_TYPE (lhs
))
6039 || POINTER_TYPE_P (TREE_TYPE (lhs
)))
6040 && ((is_gimple_call (stmt
)
6041 && gimple_call_fndecl (stmt
) != NULL_TREE
6042 && DECL_BUILT_IN (gimple_call_fndecl (stmt
)))
6043 || !gimple_vuse (stmt
)))
6046 else if (gimple_code (stmt
) == GIMPLE_COND
6047 || gimple_code (stmt
) == GIMPLE_SWITCH
)
6054 /* Initialize local data structures for VRP. */
6057 vrp_initialize (void)
6061 values_propagated
= false;
6062 num_vr_values
= num_ssa_names
;
6063 vr_value
= XCNEWVEC (value_range_t
*, num_vr_values
);
6064 vr_phi_edge_counts
= XCNEWVEC (int, num_ssa_names
);
6068 gimple_stmt_iterator si
;
6070 for (si
= gsi_start_phis (bb
); !gsi_end_p (si
); gsi_next (&si
))
6072 gimple phi
= gsi_stmt (si
);
6073 if (!stmt_interesting_for_vrp (phi
))
6075 tree lhs
= PHI_RESULT (phi
);
6076 set_value_range_to_varying (get_value_range (lhs
));
6077 prop_set_simulate_again (phi
, false);
6080 prop_set_simulate_again (phi
, true);
6083 for (si
= gsi_start_bb (bb
); !gsi_end_p (si
); gsi_next (&si
))
6085 gimple stmt
= gsi_stmt (si
);
6087 /* If the statement is a control insn, then we do not
6088 want to avoid simulating the statement once. Failure
6089 to do so means that those edges will never get added. */
6090 if (stmt_ends_bb_p (stmt
))
6091 prop_set_simulate_again (stmt
, true);
6092 else if (!stmt_interesting_for_vrp (stmt
))
6096 FOR_EACH_SSA_TREE_OPERAND (def
, stmt
, i
, SSA_OP_DEF
)
6097 set_value_range_to_varying (get_value_range (def
));
6098 prop_set_simulate_again (stmt
, false);
6101 prop_set_simulate_again (stmt
, true);
6106 /* Return the singleton value-range for NAME or NAME. */
6109 vrp_valueize (tree name
)
6111 if (TREE_CODE (name
) == SSA_NAME
)
6113 value_range_t
*vr
= get_value_range (name
);
6114 if (vr
->type
== VR_RANGE
6115 && (vr
->min
== vr
->max
6116 || operand_equal_p (vr
->min
, vr
->max
, 0)))
6122 /* Visit assignment STMT. If it produces an interesting range, record
6123 the SSA name in *OUTPUT_P. */
6125 static enum ssa_prop_result
6126 vrp_visit_assignment_or_call (gimple stmt
, tree
*output_p
)
6130 enum gimple_code code
= gimple_code (stmt
);
6131 lhs
= gimple_get_lhs (stmt
);
6133 /* We only keep track of ranges in integral and pointer types. */
6134 if (TREE_CODE (lhs
) == SSA_NAME
6135 && ((INTEGRAL_TYPE_P (TREE_TYPE (lhs
))
6136 /* It is valid to have NULL MIN/MAX values on a type. See
6137 build_range_type. */
6138 && TYPE_MIN_VALUE (TREE_TYPE (lhs
))
6139 && TYPE_MAX_VALUE (TREE_TYPE (lhs
)))
6140 || POINTER_TYPE_P (TREE_TYPE (lhs
))))
6142 value_range_t new_vr
= VR_INITIALIZER
;
6144 /* Try folding the statement to a constant first. */
6145 tree tem
= gimple_fold_stmt_to_constant (stmt
, vrp_valueize
);
6146 if (tem
&& !is_overflow_infinity (tem
))
6147 set_value_range (&new_vr
, VR_RANGE
, tem
, tem
, NULL
);
6148 /* Then dispatch to value-range extracting functions. */
6149 else if (code
== GIMPLE_CALL
)
6150 extract_range_basic (&new_vr
, stmt
);
6152 extract_range_from_assignment (&new_vr
, stmt
);
6154 if (update_value_range (lhs
, &new_vr
))
6158 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6160 fprintf (dump_file
, "Found new range for ");
6161 print_generic_expr (dump_file
, lhs
, 0);
6162 fprintf (dump_file
, ": ");
6163 dump_value_range (dump_file
, &new_vr
);
6164 fprintf (dump_file
, "\n\n");
6167 if (new_vr
.type
== VR_VARYING
)
6168 return SSA_PROP_VARYING
;
6170 return SSA_PROP_INTERESTING
;
6173 return SSA_PROP_NOT_INTERESTING
;
6176 /* Every other statement produces no useful ranges. */
6177 FOR_EACH_SSA_TREE_OPERAND (def
, stmt
, iter
, SSA_OP_DEF
)
6178 set_value_range_to_varying (get_value_range (def
));
6180 return SSA_PROP_VARYING
;
6183 /* Helper that gets the value range of the SSA_NAME with version I
6184 or a symbolic range containing the SSA_NAME only if the value range
6185 is varying or undefined. */
6187 static inline value_range_t
6188 get_vr_for_comparison (int i
)
6190 value_range_t vr
= *get_value_range (ssa_name (i
));
6192 /* If name N_i does not have a valid range, use N_i as its own
6193 range. This allows us to compare against names that may
6194 have N_i in their ranges. */
6195 if (vr
.type
== VR_VARYING
|| vr
.type
== VR_UNDEFINED
)
6198 vr
.min
= ssa_name (i
);
6199 vr
.max
= ssa_name (i
);
6205 /* Compare all the value ranges for names equivalent to VAR with VAL
6206 using comparison code COMP. Return the same value returned by
6207 compare_range_with_value, including the setting of
6208 *STRICT_OVERFLOW_P. */
6211 compare_name_with_value (enum tree_code comp
, tree var
, tree val
,
6212 bool *strict_overflow_p
)
6218 int used_strict_overflow
;
6220 value_range_t equiv_vr
;
6222 /* Get the set of equivalences for VAR. */
6223 e
= get_value_range (var
)->equiv
;
6225 /* Start at -1. Set it to 0 if we do a comparison without relying
6226 on overflow, or 1 if all comparisons rely on overflow. */
6227 used_strict_overflow
= -1;
6229 /* Compare vars' value range with val. */
6230 equiv_vr
= get_vr_for_comparison (SSA_NAME_VERSION (var
));
6232 retval
= compare_range_with_value (comp
, &equiv_vr
, val
, &sop
);
6234 used_strict_overflow
= sop
? 1 : 0;
6236 /* If the equiv set is empty we have done all work we need to do. */
6240 && used_strict_overflow
> 0)
6241 *strict_overflow_p
= true;
6245 EXECUTE_IF_SET_IN_BITMAP (e
, 0, i
, bi
)
6247 equiv_vr
= get_vr_for_comparison (i
);
6249 t
= compare_range_with_value (comp
, &equiv_vr
, val
, &sop
);
6252 /* If we get different answers from different members
6253 of the equivalence set this check must be in a dead
6254 code region. Folding it to a trap representation
6255 would be correct here. For now just return don't-know. */
6265 used_strict_overflow
= 0;
6266 else if (used_strict_overflow
< 0)
6267 used_strict_overflow
= 1;
6272 && used_strict_overflow
> 0)
6273 *strict_overflow_p
= true;
6279 /* Given a comparison code COMP and names N1 and N2, compare all the
6280 ranges equivalent to N1 against all the ranges equivalent to N2
6281 to determine the value of N1 COMP N2. Return the same value
6282 returned by compare_ranges. Set *STRICT_OVERFLOW_P to indicate
6283 whether we relied on an overflow infinity in the comparison. */
6287 compare_names (enum tree_code comp
, tree n1
, tree n2
,
6288 bool *strict_overflow_p
)
6292 bitmap_iterator bi1
, bi2
;
6294 int used_strict_overflow
;
6295 static bitmap_obstack
*s_obstack
= NULL
;
6296 static bitmap s_e1
= NULL
, s_e2
= NULL
;
6298 /* Compare the ranges of every name equivalent to N1 against the
6299 ranges of every name equivalent to N2. */
6300 e1
= get_value_range (n1
)->equiv
;
6301 e2
= get_value_range (n2
)->equiv
;
6303 /* Use the fake bitmaps if e1 or e2 are not available. */
6304 if (s_obstack
== NULL
)
6306 s_obstack
= XNEW (bitmap_obstack
);
6307 bitmap_obstack_initialize (s_obstack
);
6308 s_e1
= BITMAP_ALLOC (s_obstack
);
6309 s_e2
= BITMAP_ALLOC (s_obstack
);
6316 /* Add N1 and N2 to their own set of equivalences to avoid
6317 duplicating the body of the loop just to check N1 and N2
6319 bitmap_set_bit (e1
, SSA_NAME_VERSION (n1
));
6320 bitmap_set_bit (e2
, SSA_NAME_VERSION (n2
));
6322 /* If the equivalence sets have a common intersection, then the two
6323 names can be compared without checking their ranges. */
6324 if (bitmap_intersect_p (e1
, e2
))
6326 bitmap_clear_bit (e1
, SSA_NAME_VERSION (n1
));
6327 bitmap_clear_bit (e2
, SSA_NAME_VERSION (n2
));
6329 return (comp
== EQ_EXPR
|| comp
== GE_EXPR
|| comp
== LE_EXPR
)
6331 : boolean_false_node
;
6334 /* Start at -1. Set it to 0 if we do a comparison without relying
6335 on overflow, or 1 if all comparisons rely on overflow. */
6336 used_strict_overflow
= -1;
6338 /* Otherwise, compare all the equivalent ranges. First, add N1 and
6339 N2 to their own set of equivalences to avoid duplicating the body
6340 of the loop just to check N1 and N2 ranges. */
6341 EXECUTE_IF_SET_IN_BITMAP (e1
, 0, i1
, bi1
)
6343 value_range_t vr1
= get_vr_for_comparison (i1
);
6345 t
= retval
= NULL_TREE
;
6346 EXECUTE_IF_SET_IN_BITMAP (e2
, 0, i2
, bi2
)
6350 value_range_t vr2
= get_vr_for_comparison (i2
);
6352 t
= compare_ranges (comp
, &vr1
, &vr2
, &sop
);
6355 /* If we get different answers from different members
6356 of the equivalence set this check must be in a dead
6357 code region. Folding it to a trap representation
6358 would be correct here. For now just return don't-know. */
6362 bitmap_clear_bit (e1
, SSA_NAME_VERSION (n1
));
6363 bitmap_clear_bit (e2
, SSA_NAME_VERSION (n2
));
6369 used_strict_overflow
= 0;
6370 else if (used_strict_overflow
< 0)
6371 used_strict_overflow
= 1;
6377 bitmap_clear_bit (e1
, SSA_NAME_VERSION (n1
));
6378 bitmap_clear_bit (e2
, SSA_NAME_VERSION (n2
));
6379 if (used_strict_overflow
> 0)
6380 *strict_overflow_p
= true;
6385 /* None of the equivalent ranges are useful in computing this
6387 bitmap_clear_bit (e1
, SSA_NAME_VERSION (n1
));
6388 bitmap_clear_bit (e2
, SSA_NAME_VERSION (n2
));
6392 /* Helper function for vrp_evaluate_conditional_warnv. */
6395 vrp_evaluate_conditional_warnv_with_ops_using_ranges (enum tree_code code
,
6397 bool * strict_overflow_p
)
6399 value_range_t
*vr0
, *vr1
;
6401 vr0
= (TREE_CODE (op0
) == SSA_NAME
) ? get_value_range (op0
) : NULL
;
6402 vr1
= (TREE_CODE (op1
) == SSA_NAME
) ? get_value_range (op1
) : NULL
;
6405 return compare_ranges (code
, vr0
, vr1
, strict_overflow_p
);
6406 else if (vr0
&& vr1
== NULL
)
6407 return compare_range_with_value (code
, vr0
, op1
, strict_overflow_p
);
6408 else if (vr0
== NULL
&& vr1
)
6409 return (compare_range_with_value
6410 (swap_tree_comparison (code
), vr1
, op0
, strict_overflow_p
));
6414 /* Helper function for vrp_evaluate_conditional_warnv. */
6417 vrp_evaluate_conditional_warnv_with_ops (enum tree_code code
, tree op0
,
6418 tree op1
, bool use_equiv_p
,
6419 bool *strict_overflow_p
, bool *only_ranges
)
6423 *only_ranges
= true;
6425 /* We only deal with integral and pointer types. */
6426 if (!INTEGRAL_TYPE_P (TREE_TYPE (op0
))
6427 && !POINTER_TYPE_P (TREE_TYPE (op0
)))
6433 && (ret
= vrp_evaluate_conditional_warnv_with_ops_using_ranges
6434 (code
, op0
, op1
, strict_overflow_p
)))
6436 *only_ranges
= false;
6437 if (TREE_CODE (op0
) == SSA_NAME
&& TREE_CODE (op1
) == SSA_NAME
)
6438 return compare_names (code
, op0
, op1
, strict_overflow_p
);
6439 else if (TREE_CODE (op0
) == SSA_NAME
)
6440 return compare_name_with_value (code
, op0
, op1
, strict_overflow_p
);
6441 else if (TREE_CODE (op1
) == SSA_NAME
)
6442 return (compare_name_with_value
6443 (swap_tree_comparison (code
), op1
, op0
, strict_overflow_p
));
6446 return vrp_evaluate_conditional_warnv_with_ops_using_ranges (code
, op0
, op1
,
6451 /* Given (CODE OP0 OP1) within STMT, try to simplify it based on value range
6452 information. Return NULL if the conditional can not be evaluated.
6453 The ranges of all the names equivalent with the operands in COND
6454 will be used when trying to compute the value. If the result is
6455 based on undefined signed overflow, issue a warning if
6459 vrp_evaluate_conditional (enum tree_code code
, tree op0
, tree op1
, gimple stmt
)
6465 /* Some passes and foldings leak constants with overflow flag set
6466 into the IL. Avoid doing wrong things with these and bail out. */
6467 if ((TREE_CODE (op0
) == INTEGER_CST
6468 && TREE_OVERFLOW (op0
))
6469 || (TREE_CODE (op1
) == INTEGER_CST
6470 && TREE_OVERFLOW (op1
)))
6474 ret
= vrp_evaluate_conditional_warnv_with_ops (code
, op0
, op1
, true, &sop
,
6479 enum warn_strict_overflow_code wc
;
6480 const char* warnmsg
;
6482 if (is_gimple_min_invariant (ret
))
6484 wc
= WARN_STRICT_OVERFLOW_CONDITIONAL
;
6485 warnmsg
= G_("assuming signed overflow does not occur when "
6486 "simplifying conditional to constant");
6490 wc
= WARN_STRICT_OVERFLOW_COMPARISON
;
6491 warnmsg
= G_("assuming signed overflow does not occur when "
6492 "simplifying conditional");
6495 if (issue_strict_overflow_warning (wc
))
6497 location_t location
;
6499 if (!gimple_has_location (stmt
))
6500 location
= input_location
;
6502 location
= gimple_location (stmt
);
6503 warning_at (location
, OPT_Wstrict_overflow
, "%s", warnmsg
);
6507 if (warn_type_limits
6508 && ret
&& only_ranges
6509 && TREE_CODE_CLASS (code
) == tcc_comparison
6510 && TREE_CODE (op0
) == SSA_NAME
)
6512 /* If the comparison is being folded and the operand on the LHS
6513 is being compared against a constant value that is outside of
6514 the natural range of OP0's type, then the predicate will
6515 always fold regardless of the value of OP0. If -Wtype-limits
6516 was specified, emit a warning. */
6517 tree type
= TREE_TYPE (op0
);
6518 value_range_t
*vr0
= get_value_range (op0
);
6520 if (vr0
->type
!= VR_VARYING
6521 && INTEGRAL_TYPE_P (type
)
6522 && vrp_val_is_min (vr0
->min
)
6523 && vrp_val_is_max (vr0
->max
)
6524 && is_gimple_min_invariant (op1
))
6526 location_t location
;
6528 if (!gimple_has_location (stmt
))
6529 location
= input_location
;
6531 location
= gimple_location (stmt
);
6533 warning_at (location
, OPT_Wtype_limits
,
6535 ? G_("comparison always false "
6536 "due to limited range of data type")
6537 : G_("comparison always true "
6538 "due to limited range of data type"));
6546 /* Visit conditional statement STMT. If we can determine which edge
6547 will be taken out of STMT's basic block, record it in
6548 *TAKEN_EDGE_P and return SSA_PROP_INTERESTING. Otherwise, return
6549 SSA_PROP_VARYING. */
6551 static enum ssa_prop_result
6552 vrp_visit_cond_stmt (gimple stmt
, edge
*taken_edge_p
)
6557 *taken_edge_p
= NULL
;
6559 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6564 fprintf (dump_file
, "\nVisiting conditional with predicate: ");
6565 print_gimple_stmt (dump_file
, stmt
, 0, 0);
6566 fprintf (dump_file
, "\nWith known ranges\n");
6568 FOR_EACH_SSA_TREE_OPERAND (use
, stmt
, i
, SSA_OP_USE
)
6570 fprintf (dump_file
, "\t");
6571 print_generic_expr (dump_file
, use
, 0);
6572 fprintf (dump_file
, ": ");
6573 dump_value_range (dump_file
, vr_value
[SSA_NAME_VERSION (use
)]);
6576 fprintf (dump_file
, "\n");
6579 /* Compute the value of the predicate COND by checking the known
6580 ranges of each of its operands.
6582 Note that we cannot evaluate all the equivalent ranges here
6583 because those ranges may not yet be final and with the current
6584 propagation strategy, we cannot determine when the value ranges
6585 of the names in the equivalence set have changed.
6587 For instance, given the following code fragment
6591 i_14 = ASSERT_EXPR <i_5, i_5 != 0>
6595 Assume that on the first visit to i_14, i_5 has the temporary
6596 range [8, 8] because the second argument to the PHI function is
6597 not yet executable. We derive the range ~[0, 0] for i_14 and the
6598 equivalence set { i_5 }. So, when we visit 'if (i_14 == 1)' for
6599 the first time, since i_14 is equivalent to the range [8, 8], we
6600 determine that the predicate is always false.
6602 On the next round of propagation, i_13 is determined to be
6603 VARYING, which causes i_5 to drop down to VARYING. So, another
6604 visit to i_14 is scheduled. In this second visit, we compute the
6605 exact same range and equivalence set for i_14, namely ~[0, 0] and
6606 { i_5 }. But we did not have the previous range for i_5
6607 registered, so vrp_visit_assignment thinks that the range for
6608 i_14 has not changed. Therefore, the predicate 'if (i_14 == 1)'
6609 is not visited again, which stops propagation from visiting
6610 statements in the THEN clause of that if().
6612 To properly fix this we would need to keep the previous range
6613 value for the names in the equivalence set. This way we would've
6614 discovered that from one visit to the other i_5 changed from
6615 range [8, 8] to VR_VARYING.
6617 However, fixing this apparent limitation may not be worth the
6618 additional checking. Testing on several code bases (GCC, DLV,
6619 MICO, TRAMP3D and SPEC2000) showed that doing this results in
6620 4 more predicates folded in SPEC. */
6623 val
= vrp_evaluate_conditional_warnv_with_ops (gimple_cond_code (stmt
),
6624 gimple_cond_lhs (stmt
),
6625 gimple_cond_rhs (stmt
),
6630 *taken_edge_p
= find_taken_edge (gimple_bb (stmt
), val
);
6633 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6635 "\nIgnoring predicate evaluation because "
6636 "it assumes that signed overflow is undefined");
6641 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6643 fprintf (dump_file
, "\nPredicate evaluates to: ");
6644 if (val
== NULL_TREE
)
6645 fprintf (dump_file
, "DON'T KNOW\n");
6647 print_generic_stmt (dump_file
, val
, 0);
6650 return (*taken_edge_p
) ? SSA_PROP_INTERESTING
: SSA_PROP_VARYING
;
6653 /* Searches the case label vector VEC for the index *IDX of the CASE_LABEL
6654 that includes the value VAL. The search is restricted to the range
6655 [START_IDX, n - 1] where n is the size of VEC.
6657 If there is a CASE_LABEL for VAL, its index is placed in IDX and true is
6660 If there is no CASE_LABEL for VAL and there is one that is larger than VAL,
6661 it is placed in IDX and false is returned.
6663 If VAL is larger than any CASE_LABEL, n is placed on IDX and false is
6667 find_case_label_index (gimple stmt
, size_t start_idx
, tree val
, size_t *idx
)
6669 size_t n
= gimple_switch_num_labels (stmt
);
6672 /* Find case label for minimum of the value range or the next one.
6673 At each iteration we are searching in [low, high - 1]. */
6675 for (low
= start_idx
, high
= n
; high
!= low
; )
6679 /* Note that i != high, so we never ask for n. */
6680 size_t i
= (high
+ low
) / 2;
6681 t
= gimple_switch_label (stmt
, i
);
6683 /* Cache the result of comparing CASE_LOW and val. */
6684 cmp
= tree_int_cst_compare (CASE_LOW (t
), val
);
6688 /* Ranges cannot be empty. */
6697 if (CASE_HIGH (t
) != NULL
6698 && tree_int_cst_compare (CASE_HIGH (t
), val
) >= 0)
6710 /* Searches the case label vector VEC for the range of CASE_LABELs that is used
6711 for values between MIN and MAX. The first index is placed in MIN_IDX. The
6712 last index is placed in MAX_IDX. If the range of CASE_LABELs is empty
6713 then MAX_IDX < MIN_IDX.
6714 Returns true if the default label is not needed. */
6717 find_case_label_range (gimple stmt
, tree min
, tree max
, size_t *min_idx
,
6721 bool min_take_default
= !find_case_label_index (stmt
, 1, min
, &i
);
6722 bool max_take_default
= !find_case_label_index (stmt
, i
, max
, &j
);
6726 && max_take_default
)
6728 /* Only the default case label reached.
6729 Return an empty range. */
6736 bool take_default
= min_take_default
|| max_take_default
;
6740 if (max_take_default
)
6743 /* If the case label range is continuous, we do not need
6744 the default case label. Verify that. */
6745 high
= CASE_LOW (gimple_switch_label (stmt
, i
));
6746 if (CASE_HIGH (gimple_switch_label (stmt
, i
)))
6747 high
= CASE_HIGH (gimple_switch_label (stmt
, i
));
6748 for (k
= i
+ 1; k
<= j
; ++k
)
6750 low
= CASE_LOW (gimple_switch_label (stmt
, k
));
6751 if (!integer_onep (int_const_binop (MINUS_EXPR
, low
, high
)))
6753 take_default
= true;
6757 if (CASE_HIGH (gimple_switch_label (stmt
, k
)))
6758 high
= CASE_HIGH (gimple_switch_label (stmt
, k
));
6763 return !take_default
;
6767 /* Visit switch statement STMT. If we can determine which edge
6768 will be taken out of STMT's basic block, record it in
6769 *TAKEN_EDGE_P and return SSA_PROP_INTERESTING. Otherwise, return
6770 SSA_PROP_VARYING. */
6772 static enum ssa_prop_result
6773 vrp_visit_switch_stmt (gimple stmt
, edge
*taken_edge_p
)
6777 size_t i
= 0, j
= 0;
6780 *taken_edge_p
= NULL
;
6781 op
= gimple_switch_index (stmt
);
6782 if (TREE_CODE (op
) != SSA_NAME
)
6783 return SSA_PROP_VARYING
;
6785 vr
= get_value_range (op
);
6786 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6788 fprintf (dump_file
, "\nVisiting switch expression with operand ");
6789 print_generic_expr (dump_file
, op
, 0);
6790 fprintf (dump_file
, " with known range ");
6791 dump_value_range (dump_file
, vr
);
6792 fprintf (dump_file
, "\n");
6795 if (vr
->type
!= VR_RANGE
6796 || symbolic_range_p (vr
))
6797 return SSA_PROP_VARYING
;
6799 /* Find the single edge that is taken from the switch expression. */
6800 take_default
= !find_case_label_range (stmt
, vr
->min
, vr
->max
, &i
, &j
);
6802 /* Check if the range spans no CASE_LABEL. If so, we only reach the default
6806 gcc_assert (take_default
);
6807 val
= gimple_switch_default_label (stmt
);
6811 /* Check if labels with index i to j and maybe the default label
6812 are all reaching the same label. */
6814 val
= gimple_switch_label (stmt
, i
);
6816 && CASE_LABEL (gimple_switch_default_label (stmt
))
6817 != CASE_LABEL (val
))
6819 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6820 fprintf (dump_file
, " not a single destination for this "
6822 return SSA_PROP_VARYING
;
6824 for (++i
; i
<= j
; ++i
)
6826 if (CASE_LABEL (gimple_switch_label (stmt
, i
)) != CASE_LABEL (val
))
6828 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6829 fprintf (dump_file
, " not a single destination for this "
6831 return SSA_PROP_VARYING
;
6836 *taken_edge_p
= find_edge (gimple_bb (stmt
),
6837 label_to_block (CASE_LABEL (val
)));
6839 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6841 fprintf (dump_file
, " will take edge to ");
6842 print_generic_stmt (dump_file
, CASE_LABEL (val
), 0);
6845 return SSA_PROP_INTERESTING
;
6849 /* Evaluate statement STMT. If the statement produces a useful range,
6850 return SSA_PROP_INTERESTING and record the SSA name with the
6851 interesting range into *OUTPUT_P.
6853 If STMT is a conditional branch and we can determine its truth
6854 value, the taken edge is recorded in *TAKEN_EDGE_P.
6856 If STMT produces a varying value, return SSA_PROP_VARYING. */
6858 static enum ssa_prop_result
6859 vrp_visit_stmt (gimple stmt
, edge
*taken_edge_p
, tree
*output_p
)
6864 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6866 fprintf (dump_file
, "\nVisiting statement:\n");
6867 print_gimple_stmt (dump_file
, stmt
, 0, dump_flags
);
6868 fprintf (dump_file
, "\n");
6871 if (!stmt_interesting_for_vrp (stmt
))
6872 gcc_assert (stmt_ends_bb_p (stmt
));
6873 else if (is_gimple_assign (stmt
) || is_gimple_call (stmt
))
6875 /* In general, assignments with virtual operands are not useful
6876 for deriving ranges, with the obvious exception of calls to
6877 builtin functions. */
6878 if ((is_gimple_call (stmt
)
6879 && gimple_call_fndecl (stmt
) != NULL_TREE
6880 && DECL_BUILT_IN (gimple_call_fndecl (stmt
)))
6881 || !gimple_vuse (stmt
))
6882 return vrp_visit_assignment_or_call (stmt
, output_p
);
6884 else if (gimple_code (stmt
) == GIMPLE_COND
)
6885 return vrp_visit_cond_stmt (stmt
, taken_edge_p
);
6886 else if (gimple_code (stmt
) == GIMPLE_SWITCH
)
6887 return vrp_visit_switch_stmt (stmt
, taken_edge_p
);
6889 /* All other statements produce nothing of interest for VRP, so mark
6890 their outputs varying and prevent further simulation. */
6891 FOR_EACH_SSA_TREE_OPERAND (def
, stmt
, iter
, SSA_OP_DEF
)
6892 set_value_range_to_varying (get_value_range (def
));
6894 return SSA_PROP_VARYING
;
6897 /* Union the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
6898 { VR1TYPE, VR0MIN, VR0MAX } and store the result
6899 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
6900 possible such range. The resulting range is not canonicalized. */
6903 union_ranges (enum value_range_type
*vr0type
,
6904 tree
*vr0min
, tree
*vr0max
,
6905 enum value_range_type vr1type
,
6906 tree vr1min
, tree vr1max
)
6908 bool mineq
= operand_equal_p (*vr0min
, vr1min
, 0);
6909 bool maxeq
= operand_equal_p (*vr0max
, vr1max
, 0);
6911 /* [] is vr0, () is vr1 in the following classification comments. */
6915 if (*vr0type
== vr1type
)
6916 /* Nothing to do for equal ranges. */
6918 else if ((*vr0type
== VR_RANGE
6919 && vr1type
== VR_ANTI_RANGE
)
6920 || (*vr0type
== VR_ANTI_RANGE
6921 && vr1type
== VR_RANGE
))
6923 /* For anti-range with range union the result is varying. */
6929 else if (operand_less_p (*vr0max
, vr1min
) == 1
6930 || operand_less_p (vr1max
, *vr0min
) == 1)
6932 /* [ ] ( ) or ( ) [ ]
6933 If the ranges have an empty intersection, result of the union
6934 operation is the anti-range or if both are anti-ranges
6936 if (*vr0type
== VR_ANTI_RANGE
6937 && vr1type
== VR_ANTI_RANGE
)
6939 else if (*vr0type
== VR_ANTI_RANGE
6940 && vr1type
== VR_RANGE
)
6942 else if (*vr0type
== VR_RANGE
6943 && vr1type
== VR_ANTI_RANGE
)
6949 else if (*vr0type
== VR_RANGE
6950 && vr1type
== VR_RANGE
)
6952 /* The result is the convex hull of both ranges. */
6953 if (operand_less_p (*vr0max
, vr1min
) == 1)
6955 /* If the result can be an anti-range, create one. */
6956 if (TREE_CODE (*vr0max
) == INTEGER_CST
6957 && TREE_CODE (vr1min
) == INTEGER_CST
6958 && vrp_val_is_min (*vr0min
)
6959 && vrp_val_is_max (vr1max
))
6961 tree min
= int_const_binop (PLUS_EXPR
,
6962 *vr0max
, integer_one_node
);
6963 tree max
= int_const_binop (MINUS_EXPR
,
6964 vr1min
, integer_one_node
);
6965 if (!operand_less_p (max
, min
))
6967 *vr0type
= VR_ANTI_RANGE
;
6979 /* If the result can be an anti-range, create one. */
6980 if (TREE_CODE (vr1max
) == INTEGER_CST
6981 && TREE_CODE (*vr0min
) == INTEGER_CST
6982 && vrp_val_is_min (vr1min
)
6983 && vrp_val_is_max (*vr0max
))
6985 tree min
= int_const_binop (PLUS_EXPR
,
6986 vr1max
, integer_one_node
);
6987 tree max
= int_const_binop (MINUS_EXPR
,
6988 *vr0min
, integer_one_node
);
6989 if (!operand_less_p (max
, min
))
6991 *vr0type
= VR_ANTI_RANGE
;
7005 else if ((maxeq
|| operand_less_p (vr1max
, *vr0max
) == 1)
7006 && (mineq
|| operand_less_p (*vr0min
, vr1min
) == 1))
7008 /* [ ( ) ] or [( ) ] or [ ( )] */
7009 if (*vr0type
== VR_RANGE
7010 && vr1type
== VR_RANGE
)
7012 else if (*vr0type
== VR_ANTI_RANGE
7013 && vr1type
== VR_ANTI_RANGE
)
7019 else if (*vr0type
== VR_ANTI_RANGE
7020 && vr1type
== VR_RANGE
)
7022 /* Arbitrarily choose the right or left gap. */
7023 if (!mineq
&& TREE_CODE (vr1min
) == INTEGER_CST
)
7024 *vr0max
= int_const_binop (MINUS_EXPR
, vr1min
, integer_one_node
);
7025 else if (!maxeq
&& TREE_CODE (vr1max
) == INTEGER_CST
)
7026 *vr0min
= int_const_binop (PLUS_EXPR
, vr1max
, integer_one_node
);
7030 else if (*vr0type
== VR_RANGE
7031 && vr1type
== VR_ANTI_RANGE
)
7032 /* The result covers everything. */
7037 else if ((maxeq
|| operand_less_p (*vr0max
, vr1max
) == 1)
7038 && (mineq
|| operand_less_p (vr1min
, *vr0min
) == 1))
7040 /* ( [ ] ) or ([ ] ) or ( [ ]) */
7041 if (*vr0type
== VR_RANGE
7042 && vr1type
== VR_RANGE
)
7048 else if (*vr0type
== VR_ANTI_RANGE
7049 && vr1type
== VR_ANTI_RANGE
)
7051 else if (*vr0type
== VR_RANGE
7052 && vr1type
== VR_ANTI_RANGE
)
7054 *vr0type
= VR_ANTI_RANGE
;
7055 if (!mineq
&& TREE_CODE (*vr0min
) == INTEGER_CST
)
7057 *vr0max
= int_const_binop (MINUS_EXPR
, *vr0min
, integer_one_node
);
7060 else if (!maxeq
&& TREE_CODE (*vr0max
) == INTEGER_CST
)
7062 *vr0min
= int_const_binop (PLUS_EXPR
, *vr0max
, integer_one_node
);
7068 else if (*vr0type
== VR_ANTI_RANGE
7069 && vr1type
== VR_RANGE
)
7070 /* The result covers everything. */
7075 else if ((operand_less_p (vr1min
, *vr0max
) == 1
7076 || operand_equal_p (vr1min
, *vr0max
, 0))
7077 && operand_less_p (*vr0min
, vr1min
) == 1)
7079 /* [ ( ] ) or [ ]( ) */
7080 if (*vr0type
== VR_RANGE
7081 && vr1type
== VR_RANGE
)
7083 else if (*vr0type
== VR_ANTI_RANGE
7084 && vr1type
== VR_ANTI_RANGE
)
7086 else if (*vr0type
== VR_ANTI_RANGE
7087 && vr1type
== VR_RANGE
)
7089 if (TREE_CODE (vr1min
) == INTEGER_CST
)
7090 *vr0max
= int_const_binop (MINUS_EXPR
, vr1min
, integer_one_node
);
7094 else if (*vr0type
== VR_RANGE
7095 && vr1type
== VR_ANTI_RANGE
)
7097 if (TREE_CODE (*vr0max
) == INTEGER_CST
)
7100 *vr0min
= int_const_binop (PLUS_EXPR
, *vr0max
, integer_one_node
);
7109 else if ((operand_less_p (*vr0min
, vr1max
) == 1
7110 || operand_equal_p (*vr0min
, vr1max
, 0))
7111 && operand_less_p (vr1min
, *vr0min
) == 1)
7113 /* ( [ ) ] or ( )[ ] */
7114 if (*vr0type
== VR_RANGE
7115 && vr1type
== VR_RANGE
)
7117 else if (*vr0type
== VR_ANTI_RANGE
7118 && vr1type
== VR_ANTI_RANGE
)
7120 else if (*vr0type
== VR_ANTI_RANGE
7121 && vr1type
== VR_RANGE
)
7123 if (TREE_CODE (vr1max
) == INTEGER_CST
)
7124 *vr0min
= int_const_binop (PLUS_EXPR
, vr1max
, integer_one_node
);
7128 else if (*vr0type
== VR_RANGE
7129 && vr1type
== VR_ANTI_RANGE
)
7131 if (TREE_CODE (*vr0min
) == INTEGER_CST
)
7135 *vr0max
= int_const_binop (MINUS_EXPR
, *vr0min
, integer_one_node
);
7149 *vr0type
= VR_VARYING
;
7150 *vr0min
= NULL_TREE
;
7151 *vr0max
= NULL_TREE
;
7154 /* Intersect the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
7155 { VR1TYPE, VR0MIN, VR0MAX } and store the result
7156 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
7157 possible such range. The resulting range is not canonicalized. */
7160 intersect_ranges (enum value_range_type
*vr0type
,
7161 tree
*vr0min
, tree
*vr0max
,
7162 enum value_range_type vr1type
,
7163 tree vr1min
, tree vr1max
)
7165 bool mineq
= operand_equal_p (*vr0min
, vr1min
, 0);
7166 bool maxeq
= operand_equal_p (*vr0max
, vr1max
, 0);
7168 /* [] is vr0, () is vr1 in the following classification comments. */
7172 if (*vr0type
== vr1type
)
7173 /* Nothing to do for equal ranges. */
7175 else if ((*vr0type
== VR_RANGE
7176 && vr1type
== VR_ANTI_RANGE
)
7177 || (*vr0type
== VR_ANTI_RANGE
7178 && vr1type
== VR_RANGE
))
7180 /* For anti-range with range intersection the result is empty. */
7181 *vr0type
= VR_UNDEFINED
;
7182 *vr0min
= NULL_TREE
;
7183 *vr0max
= NULL_TREE
;
7188 else if (operand_less_p (*vr0max
, vr1min
) == 1
7189 || operand_less_p (vr1max
, *vr0min
) == 1)
7191 /* [ ] ( ) or ( ) [ ]
7192 If the ranges have an empty intersection, the result of the
7193 intersect operation is the range for intersecting an
7194 anti-range with a range or empty when intersecting two ranges. */
7195 if (*vr0type
== VR_RANGE
7196 && vr1type
== VR_ANTI_RANGE
)
7198 else if (*vr0type
== VR_ANTI_RANGE
7199 && vr1type
== VR_RANGE
)
7205 else if (*vr0type
== VR_RANGE
7206 && vr1type
== VR_RANGE
)
7208 *vr0type
= VR_UNDEFINED
;
7209 *vr0min
= NULL_TREE
;
7210 *vr0max
= NULL_TREE
;
7212 else if (*vr0type
== VR_ANTI_RANGE
7213 && vr1type
== VR_ANTI_RANGE
)
7215 /* If the anti-ranges are adjacent to each other merge them. */
7216 if (TREE_CODE (*vr0max
) == INTEGER_CST
7217 && TREE_CODE (vr1min
) == INTEGER_CST
7218 && operand_less_p (*vr0max
, vr1min
) == 1
7219 && integer_onep (int_const_binop (MINUS_EXPR
,
7222 else if (TREE_CODE (vr1max
) == INTEGER_CST
7223 && TREE_CODE (*vr0min
) == INTEGER_CST
7224 && operand_less_p (vr1max
, *vr0min
) == 1
7225 && integer_onep (int_const_binop (MINUS_EXPR
,
7228 /* Else arbitrarily take VR0. */
7231 else if ((maxeq
|| operand_less_p (vr1max
, *vr0max
) == 1)
7232 && (mineq
|| operand_less_p (*vr0min
, vr1min
) == 1))
7234 /* [ ( ) ] or [( ) ] or [ ( )] */
7235 if (*vr0type
== VR_RANGE
7236 && vr1type
== VR_RANGE
)
7238 /* If both are ranges the result is the inner one. */
7243 else if (*vr0type
== VR_RANGE
7244 && vr1type
== VR_ANTI_RANGE
)
7246 /* Choose the right gap if the left one is empty. */
7249 if (TREE_CODE (vr1max
) == INTEGER_CST
)
7250 *vr0min
= int_const_binop (PLUS_EXPR
, vr1max
, integer_one_node
);
7254 /* Choose the left gap if the right one is empty. */
7257 if (TREE_CODE (vr1min
) == INTEGER_CST
)
7258 *vr0max
= int_const_binop (MINUS_EXPR
, vr1min
,
7263 /* Choose the anti-range if the range is effectively varying. */
7264 else if (vrp_val_is_min (*vr0min
)
7265 && vrp_val_is_max (*vr0max
))
7271 /* Else choose the range. */
7273 else if (*vr0type
== VR_ANTI_RANGE
7274 && vr1type
== VR_ANTI_RANGE
)
7275 /* If both are anti-ranges the result is the outer one. */
7277 else if (*vr0type
== VR_ANTI_RANGE
7278 && vr1type
== VR_RANGE
)
7280 /* The intersection is empty. */
7281 *vr0type
= VR_UNDEFINED
;
7282 *vr0min
= NULL_TREE
;
7283 *vr0max
= NULL_TREE
;
7288 else if ((maxeq
|| operand_less_p (*vr0max
, vr1max
) == 1)
7289 && (mineq
|| operand_less_p (vr1min
, *vr0min
) == 1))
7291 /* ( [ ] ) or ([ ] ) or ( [ ]) */
7292 if (*vr0type
== VR_RANGE
7293 && vr1type
== VR_RANGE
)
7294 /* Choose the inner range. */
7296 else if (*vr0type
== VR_ANTI_RANGE
7297 && vr1type
== VR_RANGE
)
7299 /* Choose the right gap if the left is empty. */
7302 *vr0type
= VR_RANGE
;
7303 if (TREE_CODE (*vr0max
) == INTEGER_CST
)
7304 *vr0min
= int_const_binop (PLUS_EXPR
, *vr0max
,
7310 /* Choose the left gap if the right is empty. */
7313 *vr0type
= VR_RANGE
;
7314 if (TREE_CODE (*vr0min
) == INTEGER_CST
)
7315 *vr0max
= int_const_binop (MINUS_EXPR
, *vr0min
,
7321 /* Choose the anti-range if the range is effectively varying. */
7322 else if (vrp_val_is_min (vr1min
)
7323 && vrp_val_is_max (vr1max
))
7325 /* Else choose the range. */
7333 else if (*vr0type
== VR_ANTI_RANGE
7334 && vr1type
== VR_ANTI_RANGE
)
7336 /* If both are anti-ranges the result is the outer one. */
7341 else if (vr1type
== VR_ANTI_RANGE
7342 && *vr0type
== VR_RANGE
)
7344 /* The intersection is empty. */
7345 *vr0type
= VR_UNDEFINED
;
7346 *vr0min
= NULL_TREE
;
7347 *vr0max
= NULL_TREE
;
7352 else if ((operand_less_p (vr1min
, *vr0max
) == 1
7353 || operand_equal_p (vr1min
, *vr0max
, 0))
7354 && operand_less_p (*vr0min
, vr1min
) == 1)
7356 /* [ ( ] ) or [ ]( ) */
7357 if (*vr0type
== VR_ANTI_RANGE
7358 && vr1type
== VR_ANTI_RANGE
)
7360 else if (*vr0type
== VR_RANGE
7361 && vr1type
== VR_RANGE
)
7363 else if (*vr0type
== VR_RANGE
7364 && vr1type
== VR_ANTI_RANGE
)
7366 if (TREE_CODE (vr1min
) == INTEGER_CST
)
7367 *vr0max
= int_const_binop (MINUS_EXPR
, vr1min
,
7372 else if (*vr0type
== VR_ANTI_RANGE
7373 && vr1type
== VR_RANGE
)
7375 *vr0type
= VR_RANGE
;
7376 if (TREE_CODE (*vr0max
) == INTEGER_CST
)
7377 *vr0min
= int_const_binop (PLUS_EXPR
, *vr0max
,
7386 else if ((operand_less_p (*vr0min
, vr1max
) == 1
7387 || operand_equal_p (*vr0min
, vr1max
, 0))
7388 && operand_less_p (vr1min
, *vr0min
) == 1)
7390 /* ( [ ) ] or ( )[ ] */
7391 if (*vr0type
== VR_ANTI_RANGE
7392 && vr1type
== VR_ANTI_RANGE
)
7394 else if (*vr0type
== VR_RANGE
7395 && vr1type
== VR_RANGE
)
7397 else if (*vr0type
== VR_RANGE
7398 && vr1type
== VR_ANTI_RANGE
)
7400 if (TREE_CODE (vr1max
) == INTEGER_CST
)
7401 *vr0min
= int_const_binop (PLUS_EXPR
, vr1max
,
7406 else if (*vr0type
== VR_ANTI_RANGE
7407 && vr1type
== VR_RANGE
)
7409 *vr0type
= VR_RANGE
;
7410 if (TREE_CODE (*vr0min
) == INTEGER_CST
)
7411 *vr0max
= int_const_binop (MINUS_EXPR
, *vr0min
,
7421 /* As a fallback simply use { *VRTYPE, *VR0MIN, *VR0MAX } as
7422 result for the intersection. That's always a conservative
7423 correct estimate. */
7429 /* Intersect the two value-ranges *VR0 and *VR1 and store the result
7430 in *VR0. This may not be the smallest possible such range. */
7433 vrp_intersect_ranges_1 (value_range_t
*vr0
, value_range_t
*vr1
)
7435 value_range_t saved
;
7437 /* If either range is VR_VARYING the other one wins. */
7438 if (vr1
->type
== VR_VARYING
)
7440 if (vr0
->type
== VR_VARYING
)
7442 copy_value_range (vr0
, vr1
);
7446 /* When either range is VR_UNDEFINED the resulting range is
7447 VR_UNDEFINED, too. */
7448 if (vr0
->type
== VR_UNDEFINED
)
7450 if (vr1
->type
== VR_UNDEFINED
)
7452 set_value_range_to_undefined (vr0
);
7456 /* Save the original vr0 so we can return it as conservative intersection
7457 result when our worker turns things to varying. */
7459 intersect_ranges (&vr0
->type
, &vr0
->min
, &vr0
->max
,
7460 vr1
->type
, vr1
->min
, vr1
->max
);
7461 /* Make sure to canonicalize the result though as the inversion of a
7462 VR_RANGE can still be a VR_RANGE. */
7463 set_and_canonicalize_value_range (vr0
, vr0
->type
,
7464 vr0
->min
, vr0
->max
, vr0
->equiv
);
7465 /* If that failed, use the saved original VR0. */
7466 if (vr0
->type
== VR_VARYING
)
7471 /* If the result is VR_UNDEFINED there is no need to mess with
7472 the equivalencies. */
7473 if (vr0
->type
== VR_UNDEFINED
)
7476 /* The resulting set of equivalences for range intersection is the union of
7478 if (vr0
->equiv
&& vr1
->equiv
&& vr0
->equiv
!= vr1
->equiv
)
7479 bitmap_ior_into (vr0
->equiv
, vr1
->equiv
);
7480 else if (vr1
->equiv
&& !vr0
->equiv
)
7481 bitmap_copy (vr0
->equiv
, vr1
->equiv
);
7485 vrp_intersect_ranges (value_range_t
*vr0
, value_range_t
*vr1
)
7487 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7489 fprintf (dump_file
, "Intersecting\n ");
7490 dump_value_range (dump_file
, vr0
);
7491 fprintf (dump_file
, "\nand\n ");
7492 dump_value_range (dump_file
, vr1
);
7493 fprintf (dump_file
, "\n");
7495 vrp_intersect_ranges_1 (vr0
, vr1
);
7496 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7498 fprintf (dump_file
, "to\n ");
7499 dump_value_range (dump_file
, vr0
);
7500 fprintf (dump_file
, "\n");
7504 /* Meet operation for value ranges. Given two value ranges VR0 and
7505 VR1, store in VR0 a range that contains both VR0 and VR1. This
7506 may not be the smallest possible such range. */
7509 vrp_meet_1 (value_range_t
*vr0
, value_range_t
*vr1
)
7511 value_range_t saved
;
7513 if (vr0
->type
== VR_UNDEFINED
)
7515 /* Drop equivalences. See PR53465. */
7516 set_value_range (vr0
, vr1
->type
, vr1
->min
, vr1
->max
, NULL
);
7520 if (vr1
->type
== VR_UNDEFINED
)
7522 /* VR0 already has the resulting range, just drop equivalences.
7525 bitmap_clear (vr0
->equiv
);
7529 if (vr0
->type
== VR_VARYING
)
7531 /* Nothing to do. VR0 already has the resulting range. */
7535 if (vr1
->type
== VR_VARYING
)
7537 set_value_range_to_varying (vr0
);
7542 union_ranges (&vr0
->type
, &vr0
->min
, &vr0
->max
,
7543 vr1
->type
, vr1
->min
, vr1
->max
);
7544 if (vr0
->type
== VR_VARYING
)
7546 /* Failed to find an efficient meet. Before giving up and setting
7547 the result to VARYING, see if we can at least derive a useful
7548 anti-range. FIXME, all this nonsense about distinguishing
7549 anti-ranges from ranges is necessary because of the odd
7550 semantics of range_includes_zero_p and friends. */
7551 if (!symbolic_range_p (&saved
)
7552 && ((saved
.type
== VR_RANGE
&& !range_includes_zero_p (&saved
))
7553 || (saved
.type
== VR_ANTI_RANGE
&& range_includes_zero_p (&saved
)))
7554 && !symbolic_range_p (vr1
)
7555 && ((vr1
->type
== VR_RANGE
&& !range_includes_zero_p (vr1
))
7556 || (vr1
->type
== VR_ANTI_RANGE
&& range_includes_zero_p (vr1
))))
7558 set_value_range_to_nonnull (vr0
, TREE_TYPE (saved
.min
));
7560 /* Since this meet operation did not result from the meeting of
7561 two equivalent names, VR0 cannot have any equivalences. */
7563 bitmap_clear (vr0
->equiv
);
7567 set_value_range_to_varying (vr0
);
7570 set_and_canonicalize_value_range (vr0
, vr0
->type
, vr0
->min
, vr0
->max
,
7572 if (vr0
->type
== VR_VARYING
)
7575 /* The resulting set of equivalences is always the intersection of
7577 if (vr0
->equiv
&& vr1
->equiv
&& vr0
->equiv
!= vr1
->equiv
)
7578 bitmap_and_into (vr0
->equiv
, vr1
->equiv
);
7579 else if (vr0
->equiv
&& !vr1
->equiv
)
7580 bitmap_clear (vr0
->equiv
);
7584 vrp_meet (value_range_t
*vr0
, value_range_t
*vr1
)
7586 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7588 fprintf (dump_file
, "Meeting\n ");
7589 dump_value_range (dump_file
, vr0
);
7590 fprintf (dump_file
, "\nand\n ");
7591 dump_value_range (dump_file
, vr1
);
7592 fprintf (dump_file
, "\n");
7594 vrp_meet_1 (vr0
, vr1
);
7595 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7597 fprintf (dump_file
, "to\n ");
7598 dump_value_range (dump_file
, vr0
);
7599 fprintf (dump_file
, "\n");
7604 /* Visit all arguments for PHI node PHI that flow through executable
7605 edges. If a valid value range can be derived from all the incoming
7606 value ranges, set a new range for the LHS of PHI. */
7608 static enum ssa_prop_result
7609 vrp_visit_phi_node (gimple phi
)
7612 tree lhs
= PHI_RESULT (phi
);
7613 value_range_t
*lhs_vr
= get_value_range (lhs
);
7614 value_range_t vr_result
= VR_INITIALIZER
;
7616 int edges
, old_edges
;
7619 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7621 fprintf (dump_file
, "\nVisiting PHI node: ");
7622 print_gimple_stmt (dump_file
, phi
, 0, dump_flags
);
7626 for (i
= 0; i
< gimple_phi_num_args (phi
); i
++)
7628 edge e
= gimple_phi_arg_edge (phi
, i
);
7630 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7633 "\n Argument #%d (%d -> %d %sexecutable)\n",
7634 (int) i
, e
->src
->index
, e
->dest
->index
,
7635 (e
->flags
& EDGE_EXECUTABLE
) ? "" : "not ");
7638 if (e
->flags
& EDGE_EXECUTABLE
)
7640 tree arg
= PHI_ARG_DEF (phi
, i
);
7641 value_range_t vr_arg
;
7645 if (TREE_CODE (arg
) == SSA_NAME
)
7647 vr_arg
= *(get_value_range (arg
));
7651 if (is_overflow_infinity (arg
))
7653 arg
= copy_node (arg
);
7654 TREE_OVERFLOW (arg
) = 0;
7657 vr_arg
.type
= VR_RANGE
;
7660 vr_arg
.equiv
= NULL
;
7663 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7665 fprintf (dump_file
, "\t");
7666 print_generic_expr (dump_file
, arg
, dump_flags
);
7667 fprintf (dump_file
, "\n\tValue: ");
7668 dump_value_range (dump_file
, &vr_arg
);
7669 fprintf (dump_file
, "\n");
7673 copy_value_range (&vr_result
, &vr_arg
);
7675 vrp_meet (&vr_result
, &vr_arg
);
7678 if (vr_result
.type
== VR_VARYING
)
7683 if (vr_result
.type
== VR_VARYING
)
7685 else if (vr_result
.type
== VR_UNDEFINED
)
7688 old_edges
= vr_phi_edge_counts
[SSA_NAME_VERSION (lhs
)];
7689 vr_phi_edge_counts
[SSA_NAME_VERSION (lhs
)] = edges
;
7691 /* To prevent infinite iterations in the algorithm, derive ranges
7692 when the new value is slightly bigger or smaller than the
7693 previous one. We don't do this if we have seen a new executable
7694 edge; this helps us avoid an overflow infinity for conditionals
7695 which are not in a loop. */
7697 && gimple_phi_num_args (phi
) > 1
7698 && edges
== old_edges
)
7700 int cmp_min
= compare_values (lhs_vr
->min
, vr_result
.min
);
7701 int cmp_max
= compare_values (lhs_vr
->max
, vr_result
.max
);
7703 /* For non VR_RANGE or for pointers fall back to varying if
7704 the range changed. */
7705 if ((lhs_vr
->type
!= VR_RANGE
|| vr_result
.type
!= VR_RANGE
7706 || POINTER_TYPE_P (TREE_TYPE (lhs
)))
7707 && (cmp_min
!= 0 || cmp_max
!= 0))
7710 /* If the new minimum is smaller or larger than the previous
7711 one, go all the way to -INF. In the first case, to avoid
7712 iterating millions of times to reach -INF, and in the
7713 other case to avoid infinite bouncing between different
7715 if (cmp_min
> 0 || cmp_min
< 0)
7717 if (!needs_overflow_infinity (TREE_TYPE (vr_result
.min
))
7718 || !vrp_var_may_overflow (lhs
, phi
))
7719 vr_result
.min
= TYPE_MIN_VALUE (TREE_TYPE (vr_result
.min
));
7720 else if (supports_overflow_infinity (TREE_TYPE (vr_result
.min
)))
7722 negative_overflow_infinity (TREE_TYPE (vr_result
.min
));
7725 /* Similarly, if the new maximum is smaller or larger than
7726 the previous one, go all the way to +INF. */
7727 if (cmp_max
< 0 || cmp_max
> 0)
7729 if (!needs_overflow_infinity (TREE_TYPE (vr_result
.max
))
7730 || !vrp_var_may_overflow (lhs
, phi
))
7731 vr_result
.max
= TYPE_MAX_VALUE (TREE_TYPE (vr_result
.max
));
7732 else if (supports_overflow_infinity (TREE_TYPE (vr_result
.max
)))
7734 positive_overflow_infinity (TREE_TYPE (vr_result
.max
));
7737 /* If we dropped either bound to +-INF then if this is a loop
7738 PHI node SCEV may known more about its value-range. */
7739 if ((cmp_min
> 0 || cmp_min
< 0
7740 || cmp_max
< 0 || cmp_max
> 0)
7742 && (l
= loop_containing_stmt (phi
))
7743 && l
->header
== gimple_bb (phi
))
7744 adjust_range_with_scev (&vr_result
, l
, phi
, lhs
);
7746 /* If we will end up with a (-INF, +INF) range, set it to
7747 VARYING. Same if the previous max value was invalid for
7748 the type and we end up with vr_result.min > vr_result.max. */
7749 if ((vrp_val_is_max (vr_result
.max
)
7750 && vrp_val_is_min (vr_result
.min
))
7751 || compare_values (vr_result
.min
,
7756 /* If the new range is different than the previous value, keep
7759 if (update_value_range (lhs
, &vr_result
))
7761 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7763 fprintf (dump_file
, "Found new range for ");
7764 print_generic_expr (dump_file
, lhs
, 0);
7765 fprintf (dump_file
, ": ");
7766 dump_value_range (dump_file
, &vr_result
);
7767 fprintf (dump_file
, "\n\n");
7770 return SSA_PROP_INTERESTING
;
7773 /* Nothing changed, don't add outgoing edges. */
7774 return SSA_PROP_NOT_INTERESTING
;
7776 /* No match found. Set the LHS to VARYING. */
7778 set_value_range_to_varying (lhs_vr
);
7779 return SSA_PROP_VARYING
;
7782 /* Simplify boolean operations if the source is known
7783 to be already a boolean. */
7785 simplify_truth_ops_using_ranges (gimple_stmt_iterator
*gsi
, gimple stmt
)
7787 enum tree_code rhs_code
= gimple_assign_rhs_code (stmt
);
7789 bool need_conversion
;
7791 /* We handle only !=/== case here. */
7792 gcc_assert (rhs_code
== EQ_EXPR
|| rhs_code
== NE_EXPR
);
7794 op0
= gimple_assign_rhs1 (stmt
);
7795 if (!op_with_boolean_value_range_p (op0
))
7798 op1
= gimple_assign_rhs2 (stmt
);
7799 if (!op_with_boolean_value_range_p (op1
))
7802 /* Reduce number of cases to handle to NE_EXPR. As there is no
7803 BIT_XNOR_EXPR we cannot replace A == B with a single statement. */
7804 if (rhs_code
== EQ_EXPR
)
7806 if (TREE_CODE (op1
) == INTEGER_CST
)
7807 op1
= int_const_binop (BIT_XOR_EXPR
, op1
, integer_one_node
);
7812 lhs
= gimple_assign_lhs (stmt
);
7814 = !useless_type_conversion_p (TREE_TYPE (lhs
), TREE_TYPE (op0
));
7816 /* Make sure to not sign-extend a 1-bit 1 when converting the result. */
7818 && !TYPE_UNSIGNED (TREE_TYPE (op0
))
7819 && TYPE_PRECISION (TREE_TYPE (op0
)) == 1
7820 && TYPE_PRECISION (TREE_TYPE (lhs
)) > 1)
7823 /* For A != 0 we can substitute A itself. */
7824 if (integer_zerop (op1
))
7825 gimple_assign_set_rhs_with_ops (gsi
,
7827 ? NOP_EXPR
: TREE_CODE (op0
),
7829 /* For A != B we substitute A ^ B. Either with conversion. */
7830 else if (need_conversion
)
7833 tree tem
= create_tmp_reg (TREE_TYPE (op0
), NULL
);
7834 newop
= gimple_build_assign_with_ops (BIT_XOR_EXPR
, tem
, op0
, op1
);
7835 tem
= make_ssa_name (tem
, newop
);
7836 gimple_assign_set_lhs (newop
, tem
);
7837 gsi_insert_before (gsi
, newop
, GSI_SAME_STMT
);
7838 update_stmt (newop
);
7839 gimple_assign_set_rhs_with_ops (gsi
, NOP_EXPR
, tem
, NULL_TREE
);
7843 gimple_assign_set_rhs_with_ops (gsi
, BIT_XOR_EXPR
, op0
, op1
);
7844 update_stmt (gsi_stmt (*gsi
));
7849 /* Simplify a division or modulo operator to a right shift or
7850 bitwise and if the first operand is unsigned or is greater
7851 than zero and the second operand is an exact power of two. */
7854 simplify_div_or_mod_using_ranges (gimple stmt
)
7856 enum tree_code rhs_code
= gimple_assign_rhs_code (stmt
);
7858 tree op0
= gimple_assign_rhs1 (stmt
);
7859 tree op1
= gimple_assign_rhs2 (stmt
);
7860 value_range_t
*vr
= get_value_range (gimple_assign_rhs1 (stmt
));
7862 if (TYPE_UNSIGNED (TREE_TYPE (op0
)))
7864 val
= integer_one_node
;
7870 val
= compare_range_with_value (GE_EXPR
, vr
, integer_zero_node
, &sop
);
7874 && integer_onep (val
)
7875 && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC
))
7877 location_t location
;
7879 if (!gimple_has_location (stmt
))
7880 location
= input_location
;
7882 location
= gimple_location (stmt
);
7883 warning_at (location
, OPT_Wstrict_overflow
,
7884 "assuming signed overflow does not occur when "
7885 "simplifying %</%> or %<%%%> to %<>>%> or %<&%>");
7889 if (val
&& integer_onep (val
))
7893 if (rhs_code
== TRUNC_DIV_EXPR
)
7895 t
= build_int_cst (integer_type_node
, tree_log2 (op1
));
7896 gimple_assign_set_rhs_code (stmt
, RSHIFT_EXPR
);
7897 gimple_assign_set_rhs1 (stmt
, op0
);
7898 gimple_assign_set_rhs2 (stmt
, t
);
7902 t
= build_int_cst (TREE_TYPE (op1
), 1);
7903 t
= int_const_binop (MINUS_EXPR
, op1
, t
);
7904 t
= fold_convert (TREE_TYPE (op0
), t
);
7906 gimple_assign_set_rhs_code (stmt
, BIT_AND_EXPR
);
7907 gimple_assign_set_rhs1 (stmt
, op0
);
7908 gimple_assign_set_rhs2 (stmt
, t
);
7918 /* If the operand to an ABS_EXPR is >= 0, then eliminate the
7919 ABS_EXPR. If the operand is <= 0, then simplify the
7920 ABS_EXPR into a NEGATE_EXPR. */
7923 simplify_abs_using_ranges (gimple stmt
)
7926 tree op
= gimple_assign_rhs1 (stmt
);
7927 tree type
= TREE_TYPE (op
);
7928 value_range_t
*vr
= get_value_range (op
);
7930 if (TYPE_UNSIGNED (type
))
7932 val
= integer_zero_node
;
7938 val
= compare_range_with_value (LE_EXPR
, vr
, integer_zero_node
, &sop
);
7942 val
= compare_range_with_value (GE_EXPR
, vr
, integer_zero_node
,
7947 if (integer_zerop (val
))
7948 val
= integer_one_node
;
7949 else if (integer_onep (val
))
7950 val
= integer_zero_node
;
7955 && (integer_onep (val
) || integer_zerop (val
)))
7957 if (sop
&& issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC
))
7959 location_t location
;
7961 if (!gimple_has_location (stmt
))
7962 location
= input_location
;
7964 location
= gimple_location (stmt
);
7965 warning_at (location
, OPT_Wstrict_overflow
,
7966 "assuming signed overflow does not occur when "
7967 "simplifying %<abs (X)%> to %<X%> or %<-X%>");
7970 gimple_assign_set_rhs1 (stmt
, op
);
7971 if (integer_onep (val
))
7972 gimple_assign_set_rhs_code (stmt
, NEGATE_EXPR
);
7974 gimple_assign_set_rhs_code (stmt
, SSA_NAME
);
7983 /* Optimize away redundant BIT_AND_EXPR and BIT_IOR_EXPR.
7984 If all the bits that are being cleared by & are already
7985 known to be zero from VR, or all the bits that are being
7986 set by | are already known to be one from VR, the bit
7987 operation is redundant. */
7990 simplify_bit_ops_using_ranges (gimple_stmt_iterator
*gsi
, gimple stmt
)
7992 tree op0
= gimple_assign_rhs1 (stmt
);
7993 tree op1
= gimple_assign_rhs2 (stmt
);
7994 tree op
= NULL_TREE
;
7995 value_range_t vr0
= VR_INITIALIZER
;
7996 value_range_t vr1
= VR_INITIALIZER
;
7997 double_int may_be_nonzero0
, may_be_nonzero1
;
7998 double_int must_be_nonzero0
, must_be_nonzero1
;
8001 if (TREE_CODE (op0
) == SSA_NAME
)
8002 vr0
= *(get_value_range (op0
));
8003 else if (is_gimple_min_invariant (op0
))
8004 set_value_range_to_value (&vr0
, op0
, NULL
);
8008 if (TREE_CODE (op1
) == SSA_NAME
)
8009 vr1
= *(get_value_range (op1
));
8010 else if (is_gimple_min_invariant (op1
))
8011 set_value_range_to_value (&vr1
, op1
, NULL
);
8015 if (!zero_nonzero_bits_from_vr (&vr0
, &may_be_nonzero0
, &must_be_nonzero0
))
8017 if (!zero_nonzero_bits_from_vr (&vr1
, &may_be_nonzero1
, &must_be_nonzero1
))
8020 switch (gimple_assign_rhs_code (stmt
))
8023 mask
= double_int_and_not (may_be_nonzero0
, must_be_nonzero1
);
8024 if (double_int_zero_p (mask
))
8029 mask
= double_int_and_not (may_be_nonzero1
, must_be_nonzero0
);
8030 if (double_int_zero_p (mask
))
8037 mask
= double_int_and_not (may_be_nonzero0
, must_be_nonzero1
);
8038 if (double_int_zero_p (mask
))
8043 mask
= double_int_and_not (may_be_nonzero1
, must_be_nonzero0
);
8044 if (double_int_zero_p (mask
))
8054 if (op
== NULL_TREE
)
8057 gimple_assign_set_rhs_with_ops (gsi
, TREE_CODE (op
), op
, NULL
);
8058 update_stmt (gsi_stmt (*gsi
));
8062 /* We are comparing trees OP0 and OP1 using COND_CODE. OP0 has
8063 a known value range VR.
8065 If there is one and only one value which will satisfy the
8066 conditional, then return that value. Else return NULL. */
8069 test_for_singularity (enum tree_code cond_code
, tree op0
,
8070 tree op1
, value_range_t
*vr
)
8075 /* Extract minimum/maximum values which satisfy the
8076 the conditional as it was written. */
8077 if (cond_code
== LE_EXPR
|| cond_code
== LT_EXPR
)
8079 /* This should not be negative infinity; there is no overflow
8081 min
= TYPE_MIN_VALUE (TREE_TYPE (op0
));
8084 if (cond_code
== LT_EXPR
&& !is_overflow_infinity (max
))
8086 tree one
= build_int_cst (TREE_TYPE (op0
), 1);
8087 max
= fold_build2 (MINUS_EXPR
, TREE_TYPE (op0
), max
, one
);
8089 TREE_NO_WARNING (max
) = 1;
8092 else if (cond_code
== GE_EXPR
|| cond_code
== GT_EXPR
)
8094 /* This should not be positive infinity; there is no overflow
8096 max
= TYPE_MAX_VALUE (TREE_TYPE (op0
));
8099 if (cond_code
== GT_EXPR
&& !is_overflow_infinity (min
))
8101 tree one
= build_int_cst (TREE_TYPE (op0
), 1);
8102 min
= fold_build2 (PLUS_EXPR
, TREE_TYPE (op0
), min
, one
);
8104 TREE_NO_WARNING (min
) = 1;
8108 /* Now refine the minimum and maximum values using any
8109 value range information we have for op0. */
8112 if (compare_values (vr
->min
, min
) == 1)
8114 if (compare_values (vr
->max
, max
) == -1)
8117 /* If the new min/max values have converged to a single value,
8118 then there is only one value which can satisfy the condition,
8119 return that value. */
8120 if (operand_equal_p (min
, max
, 0) && is_gimple_min_invariant (min
))
8126 /* Simplify a conditional using a relational operator to an equality
8127 test if the range information indicates only one value can satisfy
8128 the original conditional. */
8131 simplify_cond_using_ranges (gimple stmt
)
8133 tree op0
= gimple_cond_lhs (stmt
);
8134 tree op1
= gimple_cond_rhs (stmt
);
8135 enum tree_code cond_code
= gimple_cond_code (stmt
);
8137 if (cond_code
!= NE_EXPR
8138 && cond_code
!= EQ_EXPR
8139 && TREE_CODE (op0
) == SSA_NAME
8140 && INTEGRAL_TYPE_P (TREE_TYPE (op0
))
8141 && is_gimple_min_invariant (op1
))
8143 value_range_t
*vr
= get_value_range (op0
);
8145 /* If we have range information for OP0, then we might be
8146 able to simplify this conditional. */
8147 if (vr
->type
== VR_RANGE
)
8149 tree new_tree
= test_for_singularity (cond_code
, op0
, op1
, vr
);
8155 fprintf (dump_file
, "Simplified relational ");
8156 print_gimple_stmt (dump_file
, stmt
, 0, 0);
8157 fprintf (dump_file
, " into ");
8160 gimple_cond_set_code (stmt
, EQ_EXPR
);
8161 gimple_cond_set_lhs (stmt
, op0
);
8162 gimple_cond_set_rhs (stmt
, new_tree
);
8168 print_gimple_stmt (dump_file
, stmt
, 0, 0);
8169 fprintf (dump_file
, "\n");
8175 /* Try again after inverting the condition. We only deal
8176 with integral types here, so no need to worry about
8177 issues with inverting FP comparisons. */
8178 cond_code
= invert_tree_comparison (cond_code
, false);
8179 new_tree
= test_for_singularity (cond_code
, op0
, op1
, vr
);
8185 fprintf (dump_file
, "Simplified relational ");
8186 print_gimple_stmt (dump_file
, stmt
, 0, 0);
8187 fprintf (dump_file
, " into ");
8190 gimple_cond_set_code (stmt
, NE_EXPR
);
8191 gimple_cond_set_lhs (stmt
, op0
);
8192 gimple_cond_set_rhs (stmt
, new_tree
);
8198 print_gimple_stmt (dump_file
, stmt
, 0, 0);
8199 fprintf (dump_file
, "\n");
8210 /* Simplify a switch statement using the value range of the switch
8214 simplify_switch_using_ranges (gimple stmt
)
8216 tree op
= gimple_switch_index (stmt
);
8221 size_t i
= 0, j
= 0, n
, n2
;
8225 if (TREE_CODE (op
) == SSA_NAME
)
8227 vr
= get_value_range (op
);
8229 /* We can only handle integer ranges. */
8230 if (vr
->type
!= VR_RANGE
8231 || symbolic_range_p (vr
))
8234 /* Find case label for min/max of the value range. */
8235 take_default
= !find_case_label_range (stmt
, vr
->min
, vr
->max
, &i
, &j
);
8237 else if (TREE_CODE (op
) == INTEGER_CST
)
8239 take_default
= !find_case_label_index (stmt
, 1, op
, &i
);
8253 n
= gimple_switch_num_labels (stmt
);
8255 /* Bail out if this is just all edges taken. */
8261 /* Build a new vector of taken case labels. */
8262 vec2
= make_tree_vec (j
- i
+ 1 + (int)take_default
);
8265 /* Add the default edge, if necessary. */
8267 TREE_VEC_ELT (vec2
, n2
++) = gimple_switch_default_label (stmt
);
8269 for (; i
<= j
; ++i
, ++n2
)
8270 TREE_VEC_ELT (vec2
, n2
) = gimple_switch_label (stmt
, i
);
8272 /* Mark needed edges. */
8273 for (i
= 0; i
< n2
; ++i
)
8275 e
= find_edge (gimple_bb (stmt
),
8276 label_to_block (CASE_LABEL (TREE_VEC_ELT (vec2
, i
))));
8277 e
->aux
= (void *)-1;
8280 /* Queue not needed edges for later removal. */
8281 FOR_EACH_EDGE (e
, ei
, gimple_bb (stmt
)->succs
)
8283 if (e
->aux
== (void *)-1)
8289 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8291 fprintf (dump_file
, "removing unreachable case label\n");
8293 VEC_safe_push (edge
, heap
, to_remove_edges
, e
);
8294 e
->flags
&= ~EDGE_EXECUTABLE
;
8297 /* And queue an update for the stmt. */
8300 VEC_safe_push (switch_update
, heap
, to_update_switch_stmts
, &su
);
8304 /* Simplify an integral conversion from an SSA name in STMT. */
8307 simplify_conversion_using_ranges (gimple stmt
)
8309 tree innerop
, middleop
, finaltype
;
8311 value_range_t
*innervr
;
8312 bool inner_unsigned_p
, middle_unsigned_p
, final_unsigned_p
;
8313 unsigned inner_prec
, middle_prec
, final_prec
;
8314 double_int innermin
, innermed
, innermax
, middlemin
, middlemed
, middlemax
;
8316 finaltype
= TREE_TYPE (gimple_assign_lhs (stmt
));
8317 if (!INTEGRAL_TYPE_P (finaltype
))
8319 middleop
= gimple_assign_rhs1 (stmt
);
8320 def_stmt
= SSA_NAME_DEF_STMT (middleop
);
8321 if (!is_gimple_assign (def_stmt
)
8322 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt
)))
8324 innerop
= gimple_assign_rhs1 (def_stmt
);
8325 if (TREE_CODE (innerop
) != SSA_NAME
)
8328 /* Get the value-range of the inner operand. */
8329 innervr
= get_value_range (innerop
);
8330 if (innervr
->type
!= VR_RANGE
8331 || TREE_CODE (innervr
->min
) != INTEGER_CST
8332 || TREE_CODE (innervr
->max
) != INTEGER_CST
)
8335 /* Simulate the conversion chain to check if the result is equal if
8336 the middle conversion is removed. */
8337 innermin
= tree_to_double_int (innervr
->min
);
8338 innermax
= tree_to_double_int (innervr
->max
);
8340 inner_prec
= TYPE_PRECISION (TREE_TYPE (innerop
));
8341 middle_prec
= TYPE_PRECISION (TREE_TYPE (middleop
));
8342 final_prec
= TYPE_PRECISION (finaltype
);
8344 /* If the first conversion is not injective, the second must not
8346 if (double_int_cmp (double_int_sub (innermax
, innermin
),
8347 double_int_mask (middle_prec
), true) > 0
8348 && middle_prec
< final_prec
)
8350 /* We also want a medium value so that we can track the effect that
8351 narrowing conversions with sign change have. */
8352 inner_unsigned_p
= TYPE_UNSIGNED (TREE_TYPE (innerop
));
8353 if (inner_unsigned_p
)
8354 innermed
= double_int_rshift (double_int_mask (inner_prec
),
8355 1, inner_prec
, false);
8357 innermed
= double_int_zero
;
8358 if (double_int_cmp (innermin
, innermed
, inner_unsigned_p
) >= 0
8359 || double_int_cmp (innermed
, innermax
, inner_unsigned_p
) >= 0)
8360 innermed
= innermin
;
8362 middle_unsigned_p
= TYPE_UNSIGNED (TREE_TYPE (middleop
));
8363 middlemin
= double_int_ext (innermin
, middle_prec
, middle_unsigned_p
);
8364 middlemed
= double_int_ext (innermed
, middle_prec
, middle_unsigned_p
);
8365 middlemax
= double_int_ext (innermax
, middle_prec
, middle_unsigned_p
);
8367 /* Require that the final conversion applied to both the original
8368 and the intermediate range produces the same result. */
8369 final_unsigned_p
= TYPE_UNSIGNED (finaltype
);
8370 if (!double_int_equal_p (double_int_ext (middlemin
,
8371 final_prec
, final_unsigned_p
),
8372 double_int_ext (innermin
,
8373 final_prec
, final_unsigned_p
))
8374 || !double_int_equal_p (double_int_ext (middlemed
,
8375 final_prec
, final_unsigned_p
),
8376 double_int_ext (innermed
,
8377 final_prec
, final_unsigned_p
))
8378 || !double_int_equal_p (double_int_ext (middlemax
,
8379 final_prec
, final_unsigned_p
),
8380 double_int_ext (innermax
,
8381 final_prec
, final_unsigned_p
)))
8384 gimple_assign_set_rhs1 (stmt
, innerop
);
8389 /* Return whether the value range *VR fits in an integer type specified
8390 by PRECISION and UNSIGNED_P. */
8393 range_fits_type_p (value_range_t
*vr
, unsigned precision
, bool unsigned_p
)
8396 unsigned src_precision
;
8399 /* We can only handle integral and pointer types. */
8400 src_type
= TREE_TYPE (vr
->min
);
8401 if (!INTEGRAL_TYPE_P (src_type
)
8402 && !POINTER_TYPE_P (src_type
))
8405 /* An extension is always fine, so is an identity transform. */
8406 src_precision
= TYPE_PRECISION (TREE_TYPE (vr
->min
));
8407 if (src_precision
< precision
8408 || (src_precision
== precision
8409 && TYPE_UNSIGNED (src_type
) == unsigned_p
))
8412 /* Now we can only handle ranges with constant bounds. */
8413 if (vr
->type
!= VR_RANGE
8414 || TREE_CODE (vr
->min
) != INTEGER_CST
8415 || TREE_CODE (vr
->max
) != INTEGER_CST
)
8418 /* For precision-preserving sign-changes the MSB of the double-int
8420 if (src_precision
== precision
8421 && (TREE_INT_CST_HIGH (vr
->min
) | TREE_INT_CST_HIGH (vr
->max
)) < 0)
8424 /* Then we can perform the conversion on both ends and compare
8425 the result for equality. */
8426 tem
= double_int_ext (tree_to_double_int (vr
->min
), precision
, unsigned_p
);
8427 if (!double_int_equal_p (tree_to_double_int (vr
->min
), tem
))
8429 tem
= double_int_ext (tree_to_double_int (vr
->max
), precision
, unsigned_p
);
8430 if (!double_int_equal_p (tree_to_double_int (vr
->max
), tem
))
8436 /* Simplify a conversion from integral SSA name to float in STMT. */
8439 simplify_float_conversion_using_ranges (gimple_stmt_iterator
*gsi
, gimple stmt
)
8441 tree rhs1
= gimple_assign_rhs1 (stmt
);
8442 value_range_t
*vr
= get_value_range (rhs1
);
8443 enum machine_mode fltmode
= TYPE_MODE (TREE_TYPE (gimple_assign_lhs (stmt
)));
8444 enum machine_mode mode
;
8448 /* We can only handle constant ranges. */
8449 if (vr
->type
!= VR_RANGE
8450 || TREE_CODE (vr
->min
) != INTEGER_CST
8451 || TREE_CODE (vr
->max
) != INTEGER_CST
)
8454 /* First check if we can use a signed type in place of an unsigned. */
8455 if (TYPE_UNSIGNED (TREE_TYPE (rhs1
))
8456 && (can_float_p (fltmode
, TYPE_MODE (TREE_TYPE (rhs1
)), 0)
8457 != CODE_FOR_nothing
)
8458 && range_fits_type_p (vr
, GET_MODE_PRECISION
8459 (TYPE_MODE (TREE_TYPE (rhs1
))), 0))
8460 mode
= TYPE_MODE (TREE_TYPE (rhs1
));
8461 /* If we can do the conversion in the current input mode do nothing. */
8462 else if (can_float_p (fltmode
, TYPE_MODE (TREE_TYPE (rhs1
)),
8463 TYPE_UNSIGNED (TREE_TYPE (rhs1
))))
8465 /* Otherwise search for a mode we can use, starting from the narrowest
8466 integer mode available. */
8469 mode
= GET_CLASS_NARROWEST_MODE (MODE_INT
);
8472 /* If we cannot do a signed conversion to float from mode
8473 or if the value-range does not fit in the signed type
8474 try with a wider mode. */
8475 if (can_float_p (fltmode
, mode
, 0) != CODE_FOR_nothing
8476 && range_fits_type_p (vr
, GET_MODE_PRECISION (mode
), 0))
8479 mode
= GET_MODE_WIDER_MODE (mode
);
8480 /* But do not widen the input. Instead leave that to the
8481 optabs expansion code. */
8482 if (GET_MODE_PRECISION (mode
) > TYPE_PRECISION (TREE_TYPE (rhs1
)))
8485 while (mode
!= VOIDmode
);
8486 if (mode
== VOIDmode
)
8490 /* It works, insert a truncation or sign-change before the
8491 float conversion. */
8492 tem
= create_tmp_var (build_nonstandard_integer_type
8493 (GET_MODE_PRECISION (mode
), 0), NULL
);
8494 conv
= gimple_build_assign_with_ops (NOP_EXPR
, tem
, rhs1
, NULL_TREE
);
8495 tem
= make_ssa_name (tem
, conv
);
8496 gimple_assign_set_lhs (conv
, tem
);
8497 gsi_insert_before (gsi
, conv
, GSI_SAME_STMT
);
8498 gimple_assign_set_rhs1 (stmt
, tem
);
8504 /* Simplify STMT using ranges if possible. */
8507 simplify_stmt_using_ranges (gimple_stmt_iterator
*gsi
)
8509 gimple stmt
= gsi_stmt (*gsi
);
8510 if (is_gimple_assign (stmt
))
8512 enum tree_code rhs_code
= gimple_assign_rhs_code (stmt
);
8513 tree rhs1
= gimple_assign_rhs1 (stmt
);
8519 /* Transform EQ_EXPR, NE_EXPR into BIT_XOR_EXPR or identity
8520 if the RHS is zero or one, and the LHS are known to be boolean
8522 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
8523 return simplify_truth_ops_using_ranges (gsi
, stmt
);
8526 /* Transform TRUNC_DIV_EXPR and TRUNC_MOD_EXPR into RSHIFT_EXPR
8527 and BIT_AND_EXPR respectively if the first operand is greater
8528 than zero and the second operand is an exact power of two. */
8529 case TRUNC_DIV_EXPR
:
8530 case TRUNC_MOD_EXPR
:
8531 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1
))
8532 && integer_pow2p (gimple_assign_rhs2 (stmt
)))
8533 return simplify_div_or_mod_using_ranges (stmt
);
8536 /* Transform ABS (X) into X or -X as appropriate. */
8538 if (TREE_CODE (rhs1
) == SSA_NAME
8539 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
8540 return simplify_abs_using_ranges (stmt
);
8545 /* Optimize away BIT_AND_EXPR and BIT_IOR_EXPR
8546 if all the bits being cleared are already cleared or
8547 all the bits being set are already set. */
8548 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
8549 return simplify_bit_ops_using_ranges (gsi
, stmt
);
8553 if (TREE_CODE (rhs1
) == SSA_NAME
8554 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
8555 return simplify_conversion_using_ranges (stmt
);
8559 if (TREE_CODE (rhs1
) == SSA_NAME
8560 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
8561 return simplify_float_conversion_using_ranges (gsi
, stmt
);
8568 else if (gimple_code (stmt
) == GIMPLE_COND
)
8569 return simplify_cond_using_ranges (stmt
);
8570 else if (gimple_code (stmt
) == GIMPLE_SWITCH
)
8571 return simplify_switch_using_ranges (stmt
);
8576 /* If the statement pointed by SI has a predicate whose value can be
8577 computed using the value range information computed by VRP, compute
8578 its value and return true. Otherwise, return false. */
8581 fold_predicate_in (gimple_stmt_iterator
*si
)
8583 bool assignment_p
= false;
8585 gimple stmt
= gsi_stmt (*si
);
8587 if (is_gimple_assign (stmt
)
8588 && TREE_CODE_CLASS (gimple_assign_rhs_code (stmt
)) == tcc_comparison
)
8590 assignment_p
= true;
8591 val
= vrp_evaluate_conditional (gimple_assign_rhs_code (stmt
),
8592 gimple_assign_rhs1 (stmt
),
8593 gimple_assign_rhs2 (stmt
),
8596 else if (gimple_code (stmt
) == GIMPLE_COND
)
8597 val
= vrp_evaluate_conditional (gimple_cond_code (stmt
),
8598 gimple_cond_lhs (stmt
),
8599 gimple_cond_rhs (stmt
),
8607 val
= fold_convert (gimple_expr_type (stmt
), val
);
8611 fprintf (dump_file
, "Folding predicate ");
8612 print_gimple_expr (dump_file
, stmt
, 0, 0);
8613 fprintf (dump_file
, " to ");
8614 print_generic_expr (dump_file
, val
, 0);
8615 fprintf (dump_file
, "\n");
8618 if (is_gimple_assign (stmt
))
8619 gimple_assign_set_rhs_from_tree (si
, val
);
8622 gcc_assert (gimple_code (stmt
) == GIMPLE_COND
);
8623 if (integer_zerop (val
))
8624 gimple_cond_make_false (stmt
);
8625 else if (integer_onep (val
))
8626 gimple_cond_make_true (stmt
);
8637 /* Callback for substitute_and_fold folding the stmt at *SI. */
8640 vrp_fold_stmt (gimple_stmt_iterator
*si
)
8642 if (fold_predicate_in (si
))
8645 return simplify_stmt_using_ranges (si
);
8648 /* Stack of dest,src equivalency pairs that need to be restored after
8649 each attempt to thread a block's incoming edge to an outgoing edge.
8651 A NULL entry is used to mark the end of pairs which need to be
8653 static VEC(tree
,heap
) *stack
;
8655 /* A trivial wrapper so that we can present the generic jump threading
8656 code with a simple API for simplifying statements. STMT is the
8657 statement we want to simplify, WITHIN_STMT provides the location
8658 for any overflow warnings. */
8661 simplify_stmt_for_jump_threading (gimple stmt
, gimple within_stmt
)
8663 /* We only use VRP information to simplify conditionals. This is
8664 overly conservative, but it's unclear if doing more would be
8665 worth the compile time cost. */
8666 if (gimple_code (stmt
) != GIMPLE_COND
)
8669 return vrp_evaluate_conditional (gimple_cond_code (stmt
),
8670 gimple_cond_lhs (stmt
),
8671 gimple_cond_rhs (stmt
), within_stmt
);
8674 /* Blocks which have more than one predecessor and more than
8675 one successor present jump threading opportunities, i.e.,
8676 when the block is reached from a specific predecessor, we
8677 may be able to determine which of the outgoing edges will
8678 be traversed. When this optimization applies, we are able
8679 to avoid conditionals at runtime and we may expose secondary
8680 optimization opportunities.
8682 This routine is effectively a driver for the generic jump
8683 threading code. It basically just presents the generic code
8684 with edges that may be suitable for jump threading.
8686 Unlike DOM, we do not iterate VRP if jump threading was successful.
8687 While iterating may expose new opportunities for VRP, it is expected
8688 those opportunities would be very limited and the compile time cost
8689 to expose those opportunities would be significant.
8691 As jump threading opportunities are discovered, they are registered
8692 for later realization. */
8695 identify_jump_threads (void)
8702 /* Ugh. When substituting values earlier in this pass we can
8703 wipe the dominance information. So rebuild the dominator
8704 information as we need it within the jump threading code. */
8705 calculate_dominance_info (CDI_DOMINATORS
);
8707 /* We do not allow VRP information to be used for jump threading
8708 across a back edge in the CFG. Otherwise it becomes too
8709 difficult to avoid eliminating loop exit tests. Of course
8710 EDGE_DFS_BACK is not accurate at this time so we have to
8712 mark_dfs_back_edges ();
8714 /* Do not thread across edges we are about to remove. Just marking
8715 them as EDGE_DFS_BACK will do. */
8716 FOR_EACH_VEC_ELT (edge
, to_remove_edges
, i
, e
)
8717 e
->flags
|= EDGE_DFS_BACK
;
8719 /* Allocate our unwinder stack to unwind any temporary equivalences
8720 that might be recorded. */
8721 stack
= VEC_alloc (tree
, heap
, 20);
8723 /* To avoid lots of silly node creation, we create a single
8724 conditional and just modify it in-place when attempting to
8726 dummy
= gimple_build_cond (EQ_EXPR
,
8727 integer_zero_node
, integer_zero_node
,
8730 /* Walk through all the blocks finding those which present a
8731 potential jump threading opportunity. We could set this up
8732 as a dominator walker and record data during the walk, but
8733 I doubt it's worth the effort for the classes of jump
8734 threading opportunities we are trying to identify at this
8735 point in compilation. */
8740 /* If the generic jump threading code does not find this block
8741 interesting, then there is nothing to do. */
8742 if (! potentially_threadable_block (bb
))
8745 /* We only care about blocks ending in a COND_EXPR. While there
8746 may be some value in handling SWITCH_EXPR here, I doubt it's
8747 terribly important. */
8748 last
= gsi_stmt (gsi_last_bb (bb
));
8750 /* We're basically looking for a switch or any kind of conditional with
8751 integral or pointer type arguments. Note the type of the second
8752 argument will be the same as the first argument, so no need to
8753 check it explicitly. */
8754 if (gimple_code (last
) == GIMPLE_SWITCH
8755 || (gimple_code (last
) == GIMPLE_COND
8756 && TREE_CODE (gimple_cond_lhs (last
)) == SSA_NAME
8757 && (INTEGRAL_TYPE_P (TREE_TYPE (gimple_cond_lhs (last
)))
8758 || POINTER_TYPE_P (TREE_TYPE (gimple_cond_lhs (last
))))
8759 && (TREE_CODE (gimple_cond_rhs (last
)) == SSA_NAME
8760 || is_gimple_min_invariant (gimple_cond_rhs (last
)))))
8764 /* We've got a block with multiple predecessors and multiple
8765 successors which also ends in a suitable conditional or
8766 switch statement. For each predecessor, see if we can thread
8767 it to a specific successor. */
8768 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
8770 /* Do not thread across back edges or abnormal edges
8772 if (e
->flags
& (EDGE_DFS_BACK
| EDGE_COMPLEX
))
8775 thread_across_edge (dummy
, e
, true, &stack
,
8776 simplify_stmt_for_jump_threading
);
8781 /* We do not actually update the CFG or SSA graphs at this point as
8782 ASSERT_EXPRs are still in the IL and cfg cleanup code does not yet
8783 handle ASSERT_EXPRs gracefully. */
8786 /* We identified all the jump threading opportunities earlier, but could
8787 not transform the CFG at that time. This routine transforms the
8788 CFG and arranges for the dominator tree to be rebuilt if necessary.
8790 Note the SSA graph update will occur during the normal TODO
8791 processing by the pass manager. */
8793 finalize_jump_threads (void)
8795 thread_through_all_blocks (false);
8796 VEC_free (tree
, heap
, stack
);
8800 /* Traverse all the blocks folding conditionals with known ranges. */
8807 values_propagated
= true;
8811 fprintf (dump_file
, "\nValue ranges after VRP:\n\n");
8812 dump_all_value_ranges (dump_file
);
8813 fprintf (dump_file
, "\n");
8816 substitute_and_fold (op_with_constant_singleton_value_range
,
8817 vrp_fold_stmt
, false);
8819 if (warn_array_bounds
)
8820 check_all_array_refs ();
8822 /* We must identify jump threading opportunities before we release
8823 the datastructures built by VRP. */
8824 identify_jump_threads ();
8826 /* Free allocated memory. */
8827 for (i
= 0; i
< num_vr_values
; i
++)
8830 BITMAP_FREE (vr_value
[i
]->equiv
);
8835 free (vr_phi_edge_counts
);
8837 /* So that we can distinguish between VRP data being available
8838 and not available. */
8840 vr_phi_edge_counts
= NULL
;
8844 /* Main entry point to VRP (Value Range Propagation). This pass is
8845 loosely based on J. R. C. Patterson, ``Accurate Static Branch
8846 Prediction by Value Range Propagation,'' in SIGPLAN Conference on
8847 Programming Language Design and Implementation, pp. 67-78, 1995.
8848 Also available at http://citeseer.ist.psu.edu/patterson95accurate.html
8850 This is essentially an SSA-CCP pass modified to deal with ranges
8851 instead of constants.
8853 While propagating ranges, we may find that two or more SSA name
8854 have equivalent, though distinct ranges. For instance,
8857 2 p_4 = ASSERT_EXPR <p_3, p_3 != 0>
8859 4 p_5 = ASSERT_EXPR <p_4, p_4 == q_2>;
8863 In the code above, pointer p_5 has range [q_2, q_2], but from the
8864 code we can also determine that p_5 cannot be NULL and, if q_2 had
8865 a non-varying range, p_5's range should also be compatible with it.
8867 These equivalences are created by two expressions: ASSERT_EXPR and
8868 copy operations. Since p_5 is an assertion on p_4, and p_4 was the
8869 result of another assertion, then we can use the fact that p_5 and
8870 p_4 are equivalent when evaluating p_5's range.
8872 Together with value ranges, we also propagate these equivalences
8873 between names so that we can take advantage of information from
8874 multiple ranges when doing final replacement. Note that this
8875 equivalency relation is transitive but not symmetric.
8877 In the example above, p_5 is equivalent to p_4, q_2 and p_3, but we
8878 cannot assert that q_2 is equivalent to p_5 because q_2 may be used
8879 in contexts where that assertion does not hold (e.g., in line 6).
8881 TODO, the main difference between this pass and Patterson's is that
8882 we do not propagate edge probabilities. We only compute whether
8883 edges can be taken or not. That is, instead of having a spectrum
8884 of jump probabilities between 0 and 1, we only deal with 0, 1 and
8885 DON'T KNOW. In the future, it may be worthwhile to propagate
8886 probabilities to aid branch prediction. */
8895 loop_optimizer_init (LOOPS_NORMAL
| LOOPS_HAVE_RECORDED_EXITS
);
8896 rewrite_into_loop_closed_ssa (NULL
, TODO_update_ssa
);
8899 insert_range_assertions ();
8901 to_remove_edges
= VEC_alloc (edge
, heap
, 10);
8902 to_update_switch_stmts
= VEC_alloc (switch_update
, heap
, 5);
8903 threadedge_initialize_values ();
8906 ssa_propagate (vrp_visit_stmt
, vrp_visit_phi_node
);
8909 free_numbers_of_iterations_estimates ();
8911 /* ASSERT_EXPRs must be removed before finalizing jump threads
8912 as finalizing jump threads calls the CFG cleanup code which
8913 does not properly handle ASSERT_EXPRs. */
8914 remove_range_assertions ();
8916 /* If we exposed any new variables, go ahead and put them into
8917 SSA form now, before we handle jump threading. This simplifies
8918 interactions between rewriting of _DECL nodes into SSA form
8919 and rewriting SSA_NAME nodes into SSA form after block
8920 duplication and CFG manipulation. */
8921 update_ssa (TODO_update_ssa
);
8923 finalize_jump_threads ();
8925 /* Remove dead edges from SWITCH_EXPR optimization. This leaves the
8926 CFG in a broken state and requires a cfg_cleanup run. */
8927 FOR_EACH_VEC_ELT (edge
, to_remove_edges
, i
, e
)
8929 /* Update SWITCH_EXPR case label vector. */
8930 FOR_EACH_VEC_ELT (switch_update
, to_update_switch_stmts
, i
, su
)
8933 size_t n
= TREE_VEC_LENGTH (su
->vec
);
8935 gimple_switch_set_num_labels (su
->stmt
, n
);
8936 for (j
= 0; j
< n
; j
++)
8937 gimple_switch_set_label (su
->stmt
, j
, TREE_VEC_ELT (su
->vec
, j
));
8938 /* As we may have replaced the default label with a regular one
8939 make sure to make it a real default label again. This ensures
8940 optimal expansion. */
8941 label
= gimple_switch_default_label (su
->stmt
);
8942 CASE_LOW (label
) = NULL_TREE
;
8943 CASE_HIGH (label
) = NULL_TREE
;
8946 if (VEC_length (edge
, to_remove_edges
) > 0)
8947 free_dominance_info (CDI_DOMINATORS
);
8949 VEC_free (edge
, heap
, to_remove_edges
);
8950 VEC_free (switch_update
, heap
, to_update_switch_stmts
);
8951 threadedge_finalize_values ();
8954 loop_optimizer_finalize ();
8961 return flag_tree_vrp
!= 0;
8964 struct gimple_opt_pass pass_vrp
=
8969 gate_vrp
, /* gate */
8970 execute_vrp
, /* execute */
8973 0, /* static_pass_number */
8974 TV_TREE_VRP
, /* tv_id */
8975 PROP_ssa
, /* properties_required */
8976 0, /* properties_provided */
8977 0, /* properties_destroyed */
8978 0, /* todo_flags_start */
8983 | TODO_ggc_collect
/* todo_flags_finish */