1 /* Support routines for Value Range Propagation (VRP).
2 Copyright (C) 2005-2016 Free Software Foundation, Inc.
3 Contributed by Diego Novillo <dnovillo@redhat.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
23 #include "coretypes.h"
25 #include "insn-codes.h"
30 #include "tree-pass.h"
32 #include "optabs-tree.h"
33 #include "gimple-pretty-print.h"
34 #include "diagnostic-core.h"
36 #include "fold-const.h"
37 #include "stor-layout.h"
40 #include "gimple-fold.h"
42 #include "gimple-iterator.h"
43 #include "gimple-walk.h"
45 #include "tree-ssa-loop-manip.h"
46 #include "tree-ssa-loop-niter.h"
47 #include "tree-ssa-loop.h"
48 #include "tree-into-ssa.h"
52 #include "tree-scalar-evolution.h"
53 #include "tree-ssa-propagate.h"
54 #include "tree-chrec.h"
55 #include "tree-ssa-threadupdate.h"
56 #include "tree-ssa-scopedtables.h"
57 #include "tree-ssa-threadedge.h"
60 #include "case-cfn-macros.h"
62 /* Range of values that can be associated with an SSA_NAME after VRP
66 /* Lattice value represented by this range. */
67 enum value_range_type type
;
69 /* Minimum and maximum values represented by this range. These
70 values should be interpreted as follows:
72 - If TYPE is VR_UNDEFINED or VR_VARYING then MIN and MAX must
75 - If TYPE == VR_RANGE then MIN holds the minimum value and
76 MAX holds the maximum value of the range [MIN, MAX].
78 - If TYPE == ANTI_RANGE the variable is known to NOT
79 take any values in the range [MIN, MAX]. */
83 /* Set of SSA names whose value ranges are equivalent to this one.
84 This set is only valid when TYPE is VR_RANGE or VR_ANTI_RANGE. */
88 #define VR_INITIALIZER { VR_UNDEFINED, NULL_TREE, NULL_TREE, NULL }
90 /* Set of SSA names found live during the RPO traversal of the function
91 for still active basic-blocks. */
94 /* Return true if the SSA name NAME is live on the edge E. */
97 live_on_edge (edge e
, tree name
)
99 return (live
[e
->dest
->index
]
100 && bitmap_bit_p (live
[e
->dest
->index
], SSA_NAME_VERSION (name
)));
103 /* Local functions. */
104 static int compare_values (tree val1
, tree val2
);
105 static int compare_values_warnv (tree val1
, tree val2
, bool *);
106 static void vrp_meet (value_range
*, value_range
*);
107 static void vrp_intersect_ranges (value_range
*, value_range
*);
108 static tree
vrp_evaluate_conditional_warnv_with_ops (enum tree_code
,
109 tree
, tree
, bool, bool *,
112 /* Location information for ASSERT_EXPRs. Each instance of this
113 structure describes an ASSERT_EXPR for an SSA name. Since a single
114 SSA name may have more than one assertion associated with it, these
115 locations are kept in a linked list attached to the corresponding
119 /* Basic block where the assertion would be inserted. */
122 /* Some assertions need to be inserted on an edge (e.g., assertions
123 generated by COND_EXPRs). In those cases, BB will be NULL. */
126 /* Pointer to the statement that generated this assertion. */
127 gimple_stmt_iterator si
;
129 /* Predicate code for the ASSERT_EXPR. Must be COMPARISON_CLASS_P. */
130 enum tree_code comp_code
;
132 /* Value being compared against. */
135 /* Expression to compare. */
138 /* Next node in the linked list. */
142 /* If bit I is present, it means that SSA name N_i has a list of
143 assertions that should be inserted in the IL. */
144 static bitmap need_assert_for
;
146 /* Array of locations lists where to insert assertions. ASSERTS_FOR[I]
147 holds a list of ASSERT_LOCUS_T nodes that describe where
148 ASSERT_EXPRs for SSA name N_I should be inserted. */
149 static assert_locus
**asserts_for
;
151 /* Value range array. After propagation, VR_VALUE[I] holds the range
152 of values that SSA name N_I may take. */
153 static unsigned num_vr_values
;
154 static value_range
**vr_value
;
155 static bool values_propagated
;
157 /* For a PHI node which sets SSA name N_I, VR_COUNTS[I] holds the
158 number of executable edges we saw the last time we visited the
160 static int *vr_phi_edge_counts
;
162 struct switch_update
{
167 static vec
<edge
> to_remove_edges
;
168 static vec
<switch_update
> to_update_switch_stmts
;
171 /* Return the maximum value for TYPE. */
174 vrp_val_max (const_tree type
)
176 if (!INTEGRAL_TYPE_P (type
))
179 return TYPE_MAX_VALUE (type
);
182 /* Return the minimum value for TYPE. */
185 vrp_val_min (const_tree type
)
187 if (!INTEGRAL_TYPE_P (type
))
190 return TYPE_MIN_VALUE (type
);
193 /* Return whether VAL is equal to the maximum value of its type. This
194 will be true for a positive overflow infinity. We can't do a
195 simple equality comparison with TYPE_MAX_VALUE because C typedefs
196 and Ada subtypes can produce types whose TYPE_MAX_VALUE is not ==
197 to the integer constant with the same value in the type. */
200 vrp_val_is_max (const_tree val
)
202 tree type_max
= vrp_val_max (TREE_TYPE (val
));
203 return (val
== type_max
204 || (type_max
!= NULL_TREE
205 && operand_equal_p (val
, type_max
, 0)));
208 /* Return whether VAL is equal to the minimum value of its type. This
209 will be true for a negative overflow infinity. */
212 vrp_val_is_min (const_tree val
)
214 tree type_min
= vrp_val_min (TREE_TYPE (val
));
215 return (val
== type_min
216 || (type_min
!= NULL_TREE
217 && operand_equal_p (val
, type_min
, 0)));
221 /* Return whether TYPE should use an overflow infinity distinct from
222 TYPE_{MIN,MAX}_VALUE. We use an overflow infinity value to
223 represent a signed overflow during VRP computations. An infinity
224 is distinct from a half-range, which will go from some number to
225 TYPE_{MIN,MAX}_VALUE. */
228 needs_overflow_infinity (const_tree type
)
230 return INTEGRAL_TYPE_P (type
) && !TYPE_OVERFLOW_WRAPS (type
);
233 /* Return whether TYPE can support our overflow infinity
234 representation: we use the TREE_OVERFLOW flag, which only exists
235 for constants. If TYPE doesn't support this, we don't optimize
236 cases which would require signed overflow--we drop them to
240 supports_overflow_infinity (const_tree type
)
242 tree min
= vrp_val_min (type
), max
= vrp_val_max (type
);
243 gcc_checking_assert (needs_overflow_infinity (type
));
244 return (min
!= NULL_TREE
245 && CONSTANT_CLASS_P (min
)
247 && CONSTANT_CLASS_P (max
));
250 /* VAL is the maximum or minimum value of a type. Return a
251 corresponding overflow infinity. */
254 make_overflow_infinity (tree val
)
256 gcc_checking_assert (val
!= NULL_TREE
&& CONSTANT_CLASS_P (val
));
257 val
= copy_node (val
);
258 TREE_OVERFLOW (val
) = 1;
262 /* Return a negative overflow infinity for TYPE. */
265 negative_overflow_infinity (tree type
)
267 gcc_checking_assert (supports_overflow_infinity (type
));
268 return make_overflow_infinity (vrp_val_min (type
));
271 /* Return a positive overflow infinity for TYPE. */
274 positive_overflow_infinity (tree type
)
276 gcc_checking_assert (supports_overflow_infinity (type
));
277 return make_overflow_infinity (vrp_val_max (type
));
280 /* Return whether VAL is a negative overflow infinity. */
283 is_negative_overflow_infinity (const_tree val
)
285 return (TREE_OVERFLOW_P (val
)
286 && needs_overflow_infinity (TREE_TYPE (val
))
287 && vrp_val_is_min (val
));
290 /* Return whether VAL is a positive overflow infinity. */
293 is_positive_overflow_infinity (const_tree val
)
295 return (TREE_OVERFLOW_P (val
)
296 && needs_overflow_infinity (TREE_TYPE (val
))
297 && vrp_val_is_max (val
));
300 /* Return whether VAL is a positive or negative overflow infinity. */
303 is_overflow_infinity (const_tree val
)
305 return (TREE_OVERFLOW_P (val
)
306 && needs_overflow_infinity (TREE_TYPE (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 /* Set value range VR to VR_UNDEFINED. */
345 set_value_range_to_undefined (value_range
*vr
)
347 vr
->type
= VR_UNDEFINED
;
348 vr
->min
= vr
->max
= NULL_TREE
;
350 bitmap_clear (vr
->equiv
);
354 /* Set value range VR to VR_VARYING. */
357 set_value_range_to_varying (value_range
*vr
)
359 vr
->type
= VR_VARYING
;
360 vr
->min
= vr
->max
= NULL_TREE
;
362 bitmap_clear (vr
->equiv
);
366 /* Set value range VR to {T, MIN, MAX, EQUIV}. */
369 set_value_range (value_range
*vr
, enum value_range_type t
, tree min
,
370 tree max
, bitmap equiv
)
372 /* Check the validity of the range. */
374 && (t
== VR_RANGE
|| t
== VR_ANTI_RANGE
))
378 gcc_assert (min
&& max
);
380 gcc_assert ((!TREE_OVERFLOW_P (min
) || is_overflow_infinity (min
))
381 && (!TREE_OVERFLOW_P (max
) || is_overflow_infinity (max
)));
383 if (INTEGRAL_TYPE_P (TREE_TYPE (min
)) && t
== VR_ANTI_RANGE
)
384 gcc_assert (!vrp_val_is_min (min
) || !vrp_val_is_max (max
));
386 cmp
= compare_values (min
, max
);
387 gcc_assert (cmp
== 0 || cmp
== -1 || cmp
== -2);
389 if (needs_overflow_infinity (TREE_TYPE (min
)))
390 gcc_assert (!is_overflow_infinity (min
)
391 || !is_overflow_infinity (max
));
395 && (t
== VR_UNDEFINED
|| t
== VR_VARYING
))
397 gcc_assert (min
== NULL_TREE
&& max
== NULL_TREE
);
398 gcc_assert (equiv
== NULL
|| bitmap_empty_p (equiv
));
405 /* Since updating the equivalence set involves deep copying the
406 bitmaps, only do it if absolutely necessary. */
407 if (vr
->equiv
== NULL
409 vr
->equiv
= BITMAP_ALLOC (NULL
);
411 if (equiv
!= vr
->equiv
)
413 if (equiv
&& !bitmap_empty_p (equiv
))
414 bitmap_copy (vr
->equiv
, equiv
);
416 bitmap_clear (vr
->equiv
);
421 /* Set value range VR to the canonical form of {T, MIN, MAX, EQUIV}.
422 This means adjusting T, MIN and MAX representing the case of a
423 wrapping range with MAX < MIN covering [MIN, type_max] U [type_min, MAX]
424 as anti-rage ~[MAX+1, MIN-1]. Likewise for wrapping anti-ranges.
425 In corner cases where MAX+1 or MIN-1 wraps this will fall back
427 This routine exists to ease canonicalization in the case where we
428 extract ranges from var + CST op limit. */
431 set_and_canonicalize_value_range (value_range
*vr
, enum value_range_type t
,
432 tree min
, tree max
, bitmap equiv
)
434 /* Use the canonical setters for VR_UNDEFINED and VR_VARYING. */
435 if (t
== VR_UNDEFINED
)
437 set_value_range_to_undefined (vr
);
440 else if (t
== VR_VARYING
)
442 set_value_range_to_varying (vr
);
446 /* Nothing to canonicalize for symbolic ranges. */
447 if (TREE_CODE (min
) != INTEGER_CST
448 || TREE_CODE (max
) != INTEGER_CST
)
450 set_value_range (vr
, t
, min
, max
, equiv
);
454 /* Wrong order for min and max, to swap them and the VR type we need
456 if (tree_int_cst_lt (max
, min
))
460 /* For one bit precision if max < min, then the swapped
461 range covers all values, so for VR_RANGE it is varying and
462 for VR_ANTI_RANGE empty range, so drop to varying as well. */
463 if (TYPE_PRECISION (TREE_TYPE (min
)) == 1)
465 set_value_range_to_varying (vr
);
469 one
= build_int_cst (TREE_TYPE (min
), 1);
470 tmp
= int_const_binop (PLUS_EXPR
, max
, one
);
471 max
= int_const_binop (MINUS_EXPR
, min
, one
);
474 /* There's one corner case, if we had [C+1, C] before we now have
475 that again. But this represents an empty value range, so drop
476 to varying in this case. */
477 if (tree_int_cst_lt (max
, min
))
479 set_value_range_to_varying (vr
);
483 t
= t
== VR_RANGE
? VR_ANTI_RANGE
: VR_RANGE
;
486 /* Anti-ranges that can be represented as ranges should be so. */
487 if (t
== VR_ANTI_RANGE
)
489 bool is_min
= vrp_val_is_min (min
);
490 bool is_max
= vrp_val_is_max (max
);
492 if (is_min
&& is_max
)
494 /* We cannot deal with empty ranges, drop to varying.
495 ??? This could be VR_UNDEFINED instead. */
496 set_value_range_to_varying (vr
);
499 else if (TYPE_PRECISION (TREE_TYPE (min
)) == 1
500 && (is_min
|| is_max
))
502 /* Non-empty boolean ranges can always be represented
503 as a singleton range. */
505 min
= max
= vrp_val_max (TREE_TYPE (min
));
507 min
= max
= vrp_val_min (TREE_TYPE (min
));
511 /* As a special exception preserve non-null ranges. */
512 && !(TYPE_UNSIGNED (TREE_TYPE (min
))
513 && integer_zerop (max
)))
515 tree one
= build_int_cst (TREE_TYPE (max
), 1);
516 min
= int_const_binop (PLUS_EXPR
, max
, one
);
517 max
= vrp_val_max (TREE_TYPE (max
));
522 tree one
= build_int_cst (TREE_TYPE (min
), 1);
523 max
= int_const_binop (MINUS_EXPR
, min
, one
);
524 min
= vrp_val_min (TREE_TYPE (min
));
529 /* Drop [-INF(OVF), +INF(OVF)] to varying. */
530 if (needs_overflow_infinity (TREE_TYPE (min
))
531 && is_overflow_infinity (min
)
532 && is_overflow_infinity (max
))
534 set_value_range_to_varying (vr
);
538 set_value_range (vr
, t
, min
, max
, equiv
);
541 /* Copy value range FROM into value range TO. */
544 copy_value_range (value_range
*to
, value_range
*from
)
546 set_value_range (to
, from
->type
, from
->min
, from
->max
, from
->equiv
);
549 /* Set value range VR to a single value. This function is only called
550 with values we get from statements, and exists to clear the
551 TREE_OVERFLOW flag so that we don't think we have an overflow
552 infinity when we shouldn't. */
555 set_value_range_to_value (value_range
*vr
, tree val
, bitmap equiv
)
557 gcc_assert (is_gimple_min_invariant (val
));
558 if (TREE_OVERFLOW_P (val
))
559 val
= drop_tree_overflow (val
);
560 set_value_range (vr
, VR_RANGE
, val
, val
, equiv
);
563 /* Set value range VR to a non-negative range of type TYPE.
564 OVERFLOW_INFINITY indicates whether to use an overflow infinity
565 rather than TYPE_MAX_VALUE; this should be true if we determine
566 that the range is nonnegative based on the assumption that signed
567 overflow does not occur. */
570 set_value_range_to_nonnegative (value_range
*vr
, tree type
,
571 bool overflow_infinity
)
575 if (overflow_infinity
&& !supports_overflow_infinity (type
))
577 set_value_range_to_varying (vr
);
581 zero
= build_int_cst (type
, 0);
582 set_value_range (vr
, VR_RANGE
, zero
,
584 ? positive_overflow_infinity (type
)
585 : TYPE_MAX_VALUE (type
)),
589 /* Set value range VR to a non-NULL range of type TYPE. */
592 set_value_range_to_nonnull (value_range
*vr
, tree type
)
594 tree zero
= build_int_cst (type
, 0);
595 set_value_range (vr
, VR_ANTI_RANGE
, zero
, zero
, vr
->equiv
);
599 /* Set value range VR to a NULL range of type TYPE. */
602 set_value_range_to_null (value_range
*vr
, tree type
)
604 set_value_range_to_value (vr
, build_int_cst (type
, 0), vr
->equiv
);
608 /* Set value range VR to a range of a truthvalue of type TYPE. */
611 set_value_range_to_truthvalue (value_range
*vr
, tree type
)
613 if (TYPE_PRECISION (type
) == 1)
614 set_value_range_to_varying (vr
);
616 set_value_range (vr
, VR_RANGE
,
617 build_int_cst (type
, 0), build_int_cst (type
, 1),
622 /* If abs (min) < abs (max), set VR to [-max, max], if
623 abs (min) >= abs (max), set VR to [-min, min]. */
626 abs_extent_range (value_range
*vr
, tree min
, tree max
)
630 gcc_assert (TREE_CODE (min
) == INTEGER_CST
);
631 gcc_assert (TREE_CODE (max
) == INTEGER_CST
);
632 gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (min
)));
633 gcc_assert (!TYPE_UNSIGNED (TREE_TYPE (min
)));
634 min
= fold_unary (ABS_EXPR
, TREE_TYPE (min
), min
);
635 max
= fold_unary (ABS_EXPR
, TREE_TYPE (max
), max
);
636 if (TREE_OVERFLOW (min
) || TREE_OVERFLOW (max
))
638 set_value_range_to_varying (vr
);
641 cmp
= compare_values (min
, max
);
643 min
= fold_unary (NEGATE_EXPR
, TREE_TYPE (min
), max
);
644 else if (cmp
== 0 || cmp
== 1)
647 min
= fold_unary (NEGATE_EXPR
, TREE_TYPE (min
), min
);
651 set_value_range_to_varying (vr
);
654 set_and_canonicalize_value_range (vr
, VR_RANGE
, min
, max
, NULL
);
658 /* Return value range information for VAR.
660 If we have no values ranges recorded (ie, VRP is not running), then
661 return NULL. Otherwise create an empty range if none existed for VAR. */
664 get_value_range (const_tree var
)
666 static const value_range vr_const_varying
667 = { VR_VARYING
, NULL_TREE
, NULL_TREE
, NULL
};
670 unsigned ver
= SSA_NAME_VERSION (var
);
672 /* If we have no recorded ranges, then return NULL. */
676 /* If we query the range for a new SSA name return an unmodifiable VARYING.
677 We should get here at most from the substitute-and-fold stage which
678 will never try to change values. */
679 if (ver
>= num_vr_values
)
680 return CONST_CAST (value_range
*, &vr_const_varying
);
686 /* After propagation finished do not allocate new value-ranges. */
687 if (values_propagated
)
688 return CONST_CAST (value_range
*, &vr_const_varying
);
690 /* Create a default value range. */
691 vr_value
[ver
] = vr
= XCNEW (value_range
);
693 /* Defer allocating the equivalence set. */
696 /* If VAR is a default definition of a parameter, the variable can
697 take any value in VAR's type. */
698 if (SSA_NAME_IS_DEFAULT_DEF (var
))
700 sym
= SSA_NAME_VAR (var
);
701 if (TREE_CODE (sym
) == PARM_DECL
)
703 /* Try to use the "nonnull" attribute to create ~[0, 0]
704 anti-ranges for pointers. Note that this is only valid with
705 default definitions of PARM_DECLs. */
706 if (POINTER_TYPE_P (TREE_TYPE (sym
))
707 && nonnull_arg_p (sym
))
708 set_value_range_to_nonnull (vr
, TREE_TYPE (sym
));
710 set_value_range_to_varying (vr
);
712 else if (TREE_CODE (sym
) == RESULT_DECL
713 && DECL_BY_REFERENCE (sym
))
714 set_value_range_to_nonnull (vr
, TREE_TYPE (sym
));
720 /* Set value-ranges of all SSA names defined by STMT to varying. */
723 set_defs_to_varying (gimple
*stmt
)
727 FOR_EACH_SSA_TREE_OPERAND (def
, stmt
, i
, SSA_OP_DEF
)
729 value_range
*vr
= get_value_range (def
);
730 /* Avoid writing to vr_const_varying get_value_range may return. */
731 if (vr
->type
!= VR_VARYING
)
732 set_value_range_to_varying (vr
);
737 /* Return true, if VAL1 and VAL2 are equal values for VRP purposes. */
740 vrp_operand_equal_p (const_tree val1
, const_tree val2
)
744 if (!val1
|| !val2
|| !operand_equal_p (val1
, val2
, 0))
746 return is_overflow_infinity (val1
) == is_overflow_infinity (val2
);
749 /* Return true, if the bitmaps B1 and B2 are equal. */
752 vrp_bitmap_equal_p (const_bitmap b1
, const_bitmap b2
)
755 || ((!b1
|| bitmap_empty_p (b1
))
756 && (!b2
|| bitmap_empty_p (b2
)))
758 && bitmap_equal_p (b1
, b2
)));
761 /* Update the value range and equivalence set for variable VAR to
762 NEW_VR. Return true if NEW_VR is different from VAR's previous
765 NOTE: This function assumes that NEW_VR is a temporary value range
766 object created for the sole purpose of updating VAR's range. The
767 storage used by the equivalence set from NEW_VR will be freed by
768 this function. Do not call update_value_range when NEW_VR
769 is the range object associated with another SSA name. */
772 update_value_range (const_tree var
, value_range
*new_vr
)
777 /* If there is a value-range on the SSA name from earlier analysis
779 if (INTEGRAL_TYPE_P (TREE_TYPE (var
)))
782 value_range_type rtype
= get_range_info (var
, &min
, &max
);
783 if (rtype
== VR_RANGE
|| rtype
== VR_ANTI_RANGE
)
787 /* Range info on SSA names doesn't carry overflow information
788 so make sure to preserve the overflow bit on the lattice. */
789 if (new_vr
->type
== VR_RANGE
790 && is_negative_overflow_infinity (new_vr
->min
)
791 && wi::eq_p (new_vr
->min
, min
))
792 nr
.min
= new_vr
->min
;
794 nr
.min
= wide_int_to_tree (TREE_TYPE (var
), min
);
795 if (new_vr
->type
== VR_RANGE
796 && is_positive_overflow_infinity (new_vr
->max
)
797 && wi::eq_p (new_vr
->max
, max
))
798 nr
.max
= new_vr
->max
;
800 nr
.max
= wide_int_to_tree (TREE_TYPE (var
), max
);
802 vrp_intersect_ranges (new_vr
, &nr
);
806 /* Update the value range, if necessary. */
807 old_vr
= get_value_range (var
);
808 is_new
= old_vr
->type
!= new_vr
->type
809 || !vrp_operand_equal_p (old_vr
->min
, new_vr
->min
)
810 || !vrp_operand_equal_p (old_vr
->max
, new_vr
->max
)
811 || !vrp_bitmap_equal_p (old_vr
->equiv
, new_vr
->equiv
);
815 /* Do not allow transitions up the lattice. The following
816 is slightly more awkward than just new_vr->type < old_vr->type
817 because VR_RANGE and VR_ANTI_RANGE need to be considered
818 the same. We may not have is_new when transitioning to
819 UNDEFINED. If old_vr->type is VARYING, we shouldn't be
821 if (new_vr
->type
== VR_UNDEFINED
)
823 BITMAP_FREE (new_vr
->equiv
);
824 set_value_range_to_varying (old_vr
);
825 set_value_range_to_varying (new_vr
);
829 set_value_range (old_vr
, new_vr
->type
, new_vr
->min
, new_vr
->max
,
833 BITMAP_FREE (new_vr
->equiv
);
839 /* Add VAR and VAR's equivalence set to EQUIV. This is the central
840 point where equivalence processing can be turned on/off. */
843 add_equivalence (bitmap
*equiv
, const_tree var
)
845 unsigned ver
= SSA_NAME_VERSION (var
);
846 value_range
*vr
= vr_value
[ver
];
849 *equiv
= BITMAP_ALLOC (NULL
);
850 bitmap_set_bit (*equiv
, ver
);
852 bitmap_ior_into (*equiv
, vr
->equiv
);
856 /* Return true if VR is ~[0, 0]. */
859 range_is_nonnull (value_range
*vr
)
861 return vr
->type
== VR_ANTI_RANGE
862 && integer_zerop (vr
->min
)
863 && integer_zerop (vr
->max
);
867 /* Return true if VR is [0, 0]. */
870 range_is_null (value_range
*vr
)
872 return vr
->type
== VR_RANGE
873 && integer_zerop (vr
->min
)
874 && integer_zerop (vr
->max
);
877 /* Return true if max and min of VR are INTEGER_CST. It's not necessary
881 range_int_cst_p (value_range
*vr
)
883 return (vr
->type
== VR_RANGE
884 && TREE_CODE (vr
->max
) == INTEGER_CST
885 && TREE_CODE (vr
->min
) == INTEGER_CST
);
888 /* Return true if VR is a INTEGER_CST singleton. */
891 range_int_cst_singleton_p (value_range
*vr
)
893 return (range_int_cst_p (vr
)
894 && !is_overflow_infinity (vr
->min
)
895 && !is_overflow_infinity (vr
->max
)
896 && tree_int_cst_equal (vr
->min
, vr
->max
));
899 /* Return true if value range VR involves at least one symbol. */
902 symbolic_range_p (value_range
*vr
)
904 return (!is_gimple_min_invariant (vr
->min
)
905 || !is_gimple_min_invariant (vr
->max
));
908 /* Return the single symbol (an SSA_NAME) contained in T if any, or NULL_TREE
909 otherwise. We only handle additive operations and set NEG to true if the
910 symbol is negated and INV to the invariant part, if any. */
913 get_single_symbol (tree t
, bool *neg
, tree
*inv
)
918 if (TREE_CODE (t
) == PLUS_EXPR
919 || TREE_CODE (t
) == POINTER_PLUS_EXPR
920 || TREE_CODE (t
) == MINUS_EXPR
)
922 if (is_gimple_min_invariant (TREE_OPERAND (t
, 0)))
924 neg_
= (TREE_CODE (t
) == MINUS_EXPR
);
925 inv_
= TREE_OPERAND (t
, 0);
926 t
= TREE_OPERAND (t
, 1);
928 else if (is_gimple_min_invariant (TREE_OPERAND (t
, 1)))
931 inv_
= TREE_OPERAND (t
, 1);
932 t
= TREE_OPERAND (t
, 0);
943 if (TREE_CODE (t
) == NEGATE_EXPR
)
945 t
= TREE_OPERAND (t
, 0);
949 if (TREE_CODE (t
) != SSA_NAME
)
957 /* The reverse operation: build a symbolic expression with TYPE
958 from symbol SYM, negated according to NEG, and invariant INV. */
961 build_symbolic_expr (tree type
, tree sym
, bool neg
, tree inv
)
963 const bool pointer_p
= POINTER_TYPE_P (type
);
967 t
= build1 (NEGATE_EXPR
, type
, t
);
969 if (integer_zerop (inv
))
972 return build2 (pointer_p
? POINTER_PLUS_EXPR
: PLUS_EXPR
, type
, t
, inv
);
975 /* Return true if value range VR involves exactly one symbol SYM. */
978 symbolic_range_based_on_p (value_range
*vr
, const_tree sym
)
980 bool neg
, min_has_symbol
, max_has_symbol
;
983 if (is_gimple_min_invariant (vr
->min
))
984 min_has_symbol
= false;
985 else if (get_single_symbol (vr
->min
, &neg
, &inv
) == sym
)
986 min_has_symbol
= true;
990 if (is_gimple_min_invariant (vr
->max
))
991 max_has_symbol
= false;
992 else if (get_single_symbol (vr
->max
, &neg
, &inv
) == sym
)
993 max_has_symbol
= true;
997 return (min_has_symbol
|| max_has_symbol
);
1000 /* Return true if value range VR uses an overflow infinity. */
1003 overflow_infinity_range_p (value_range
*vr
)
1005 return (vr
->type
== VR_RANGE
1006 && (is_overflow_infinity (vr
->min
)
1007 || is_overflow_infinity (vr
->max
)));
1010 /* Return false if we can not make a valid comparison based on VR;
1011 this will be the case if it uses an overflow infinity and overflow
1012 is not undefined (i.e., -fno-strict-overflow is in effect).
1013 Otherwise return true, and set *STRICT_OVERFLOW_P to true if VR
1014 uses an overflow infinity. */
1017 usable_range_p (value_range
*vr
, bool *strict_overflow_p
)
1019 gcc_assert (vr
->type
== VR_RANGE
);
1020 if (is_overflow_infinity (vr
->min
))
1022 *strict_overflow_p
= true;
1023 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (vr
->min
)))
1026 if (is_overflow_infinity (vr
->max
))
1028 *strict_overflow_p
= true;
1029 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (vr
->max
)))
1035 /* Return true if the result of assignment STMT is know to be non-zero.
1036 If the return value is based on the assumption that signed overflow is
1037 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1038 *STRICT_OVERFLOW_P.*/
1041 gimple_assign_nonzero_warnv_p (gimple
*stmt
, bool *strict_overflow_p
)
1043 enum tree_code code
= gimple_assign_rhs_code (stmt
);
1044 switch (get_gimple_rhs_class (code
))
1046 case GIMPLE_UNARY_RHS
:
1047 return tree_unary_nonzero_warnv_p (gimple_assign_rhs_code (stmt
),
1048 gimple_expr_type (stmt
),
1049 gimple_assign_rhs1 (stmt
),
1051 case GIMPLE_BINARY_RHS
:
1052 return tree_binary_nonzero_warnv_p (gimple_assign_rhs_code (stmt
),
1053 gimple_expr_type (stmt
),
1054 gimple_assign_rhs1 (stmt
),
1055 gimple_assign_rhs2 (stmt
),
1057 case GIMPLE_TERNARY_RHS
:
1059 case GIMPLE_SINGLE_RHS
:
1060 return tree_single_nonzero_warnv_p (gimple_assign_rhs1 (stmt
),
1062 case GIMPLE_INVALID_RHS
:
1069 /* Return true if STMT is known to compute a non-zero value.
1070 If the return value is based on the assumption that signed overflow is
1071 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1072 *STRICT_OVERFLOW_P.*/
1075 gimple_stmt_nonzero_warnv_p (gimple
*stmt
, bool *strict_overflow_p
)
1077 switch (gimple_code (stmt
))
1080 return gimple_assign_nonzero_warnv_p (stmt
, strict_overflow_p
);
1083 tree fndecl
= gimple_call_fndecl (stmt
);
1084 if (!fndecl
) return false;
1085 if (flag_delete_null_pointer_checks
&& !flag_check_new
1086 && DECL_IS_OPERATOR_NEW (fndecl
)
1087 && !TREE_NOTHROW (fndecl
))
1089 /* References are always non-NULL. */
1090 if (flag_delete_null_pointer_checks
1091 && TREE_CODE (TREE_TYPE (fndecl
)) == REFERENCE_TYPE
)
1093 if (flag_delete_null_pointer_checks
&&
1094 lookup_attribute ("returns_nonnull",
1095 TYPE_ATTRIBUTES (gimple_call_fntype (stmt
))))
1097 return gimple_alloca_call_p (stmt
);
1104 /* Like tree_expr_nonzero_warnv_p, but this function uses value ranges
1108 vrp_stmt_computes_nonzero (gimple
*stmt
, bool *strict_overflow_p
)
1110 if (gimple_stmt_nonzero_warnv_p (stmt
, strict_overflow_p
))
1113 /* If we have an expression of the form &X->a, then the expression
1114 is nonnull if X is nonnull. */
1115 if (is_gimple_assign (stmt
)
1116 && gimple_assign_rhs_code (stmt
) == ADDR_EXPR
)
1118 tree expr
= gimple_assign_rhs1 (stmt
);
1119 tree base
= get_base_address (TREE_OPERAND (expr
, 0));
1121 if (base
!= NULL_TREE
1122 && TREE_CODE (base
) == MEM_REF
1123 && TREE_CODE (TREE_OPERAND (base
, 0)) == SSA_NAME
)
1125 value_range
*vr
= get_value_range (TREE_OPERAND (base
, 0));
1126 if (range_is_nonnull (vr
))
1134 /* Returns true if EXPR is a valid value (as expected by compare_values) --
1135 a gimple invariant, or SSA_NAME +- CST. */
1138 valid_value_p (tree expr
)
1140 if (TREE_CODE (expr
) == SSA_NAME
)
1143 if (TREE_CODE (expr
) == PLUS_EXPR
1144 || TREE_CODE (expr
) == MINUS_EXPR
)
1145 return (TREE_CODE (TREE_OPERAND (expr
, 0)) == SSA_NAME
1146 && TREE_CODE (TREE_OPERAND (expr
, 1)) == INTEGER_CST
);
1148 return is_gimple_min_invariant (expr
);
1154 -2 if those are incomparable. */
1156 operand_less_p (tree val
, tree val2
)
1158 /* LT is folded faster than GE and others. Inline the common case. */
1159 if (TREE_CODE (val
) == INTEGER_CST
&& TREE_CODE (val2
) == INTEGER_CST
)
1161 if (! is_positive_overflow_infinity (val2
))
1162 return tree_int_cst_lt (val
, val2
);
1168 fold_defer_overflow_warnings ();
1170 tcmp
= fold_binary_to_constant (LT_EXPR
, boolean_type_node
, val
, val2
);
1172 fold_undefer_and_ignore_overflow_warnings ();
1175 || TREE_CODE (tcmp
) != INTEGER_CST
)
1178 if (!integer_zerop (tcmp
))
1182 /* val >= val2, not considering overflow infinity. */
1183 if (is_negative_overflow_infinity (val
))
1184 return is_negative_overflow_infinity (val2
) ? 0 : 1;
1185 else if (is_positive_overflow_infinity (val2
))
1186 return is_positive_overflow_infinity (val
) ? 0 : 1;
1191 /* Compare two values VAL1 and VAL2. Return
1193 -2 if VAL1 and VAL2 cannot be compared at compile-time,
1196 +1 if VAL1 > VAL2, and
1199 This is similar to tree_int_cst_compare but supports pointer values
1200 and values that cannot be compared at compile time.
1202 If STRICT_OVERFLOW_P is not NULL, then set *STRICT_OVERFLOW_P to
1203 true if the return value is only valid if we assume that signed
1204 overflow is undefined. */
1207 compare_values_warnv (tree val1
, tree val2
, bool *strict_overflow_p
)
1212 /* Below we rely on the fact that VAL1 and VAL2 are both pointers or
1214 gcc_assert (POINTER_TYPE_P (TREE_TYPE (val1
))
1215 == POINTER_TYPE_P (TREE_TYPE (val2
)));
1217 /* Convert the two values into the same type. This is needed because
1218 sizetype causes sign extension even for unsigned types. */
1219 val2
= fold_convert (TREE_TYPE (val1
), val2
);
1220 STRIP_USELESS_TYPE_CONVERSION (val2
);
1222 if ((TREE_CODE (val1
) == SSA_NAME
1223 || (TREE_CODE (val1
) == NEGATE_EXPR
1224 && TREE_CODE (TREE_OPERAND (val1
, 0)) == SSA_NAME
)
1225 || TREE_CODE (val1
) == PLUS_EXPR
1226 || TREE_CODE (val1
) == MINUS_EXPR
)
1227 && (TREE_CODE (val2
) == SSA_NAME
1228 || (TREE_CODE (val2
) == NEGATE_EXPR
1229 && TREE_CODE (TREE_OPERAND (val2
, 0)) == SSA_NAME
)
1230 || TREE_CODE (val2
) == PLUS_EXPR
1231 || TREE_CODE (val2
) == MINUS_EXPR
))
1233 tree n1
, c1
, n2
, c2
;
1234 enum tree_code code1
, code2
;
1236 /* If VAL1 and VAL2 are of the form '[-]NAME [+-] CST' or 'NAME',
1237 return -1 or +1 accordingly. If VAL1 and VAL2 don't use the
1238 same name, return -2. */
1239 if (TREE_CODE (val1
) == SSA_NAME
|| TREE_CODE (val1
) == NEGATE_EXPR
)
1247 code1
= TREE_CODE (val1
);
1248 n1
= TREE_OPERAND (val1
, 0);
1249 c1
= TREE_OPERAND (val1
, 1);
1250 if (tree_int_cst_sgn (c1
) == -1)
1252 if (is_negative_overflow_infinity (c1
))
1254 c1
= fold_unary_to_constant (NEGATE_EXPR
, TREE_TYPE (c1
), c1
);
1257 code1
= code1
== MINUS_EXPR
? PLUS_EXPR
: MINUS_EXPR
;
1261 if (TREE_CODE (val2
) == SSA_NAME
|| TREE_CODE (val2
) == NEGATE_EXPR
)
1269 code2
= TREE_CODE (val2
);
1270 n2
= TREE_OPERAND (val2
, 0);
1271 c2
= TREE_OPERAND (val2
, 1);
1272 if (tree_int_cst_sgn (c2
) == -1)
1274 if (is_negative_overflow_infinity (c2
))
1276 c2
= fold_unary_to_constant (NEGATE_EXPR
, TREE_TYPE (c2
), c2
);
1279 code2
= code2
== MINUS_EXPR
? PLUS_EXPR
: MINUS_EXPR
;
1283 /* Both values must use the same name. */
1284 if (TREE_CODE (n1
) == NEGATE_EXPR
&& TREE_CODE (n2
) == NEGATE_EXPR
)
1286 n1
= TREE_OPERAND (n1
, 0);
1287 n2
= TREE_OPERAND (n2
, 0);
1292 if (code1
== SSA_NAME
&& code2
== SSA_NAME
)
1296 /* If overflow is defined we cannot simplify more. */
1297 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (val1
)))
1300 if (strict_overflow_p
!= NULL
1301 && (code1
== SSA_NAME
|| !TREE_NO_WARNING (val1
))
1302 && (code2
== SSA_NAME
|| !TREE_NO_WARNING (val2
)))
1303 *strict_overflow_p
= true;
1305 if (code1
== SSA_NAME
)
1307 if (code2
== PLUS_EXPR
)
1308 /* NAME < NAME + CST */
1310 else if (code2
== MINUS_EXPR
)
1311 /* NAME > NAME - CST */
1314 else if (code1
== PLUS_EXPR
)
1316 if (code2
== SSA_NAME
)
1317 /* NAME + CST > NAME */
1319 else if (code2
== PLUS_EXPR
)
1320 /* NAME + CST1 > NAME + CST2, if CST1 > CST2 */
1321 return compare_values_warnv (c1
, c2
, strict_overflow_p
);
1322 else if (code2
== MINUS_EXPR
)
1323 /* NAME + CST1 > NAME - CST2 */
1326 else if (code1
== MINUS_EXPR
)
1328 if (code2
== SSA_NAME
)
1329 /* NAME - CST < NAME */
1331 else if (code2
== PLUS_EXPR
)
1332 /* NAME - CST1 < NAME + CST2 */
1334 else if (code2
== MINUS_EXPR
)
1335 /* NAME - CST1 > NAME - CST2, if CST1 < CST2. Notice that
1336 C1 and C2 are swapped in the call to compare_values. */
1337 return compare_values_warnv (c2
, c1
, strict_overflow_p
);
1343 /* We cannot compare non-constants. */
1344 if (!is_gimple_min_invariant (val1
) || !is_gimple_min_invariant (val2
))
1347 if (!POINTER_TYPE_P (TREE_TYPE (val1
)))
1349 /* We cannot compare overflowed values, except for overflow
1351 if (TREE_OVERFLOW (val1
) || TREE_OVERFLOW (val2
))
1353 if (strict_overflow_p
!= NULL
)
1354 *strict_overflow_p
= true;
1355 if (is_negative_overflow_infinity (val1
))
1356 return is_negative_overflow_infinity (val2
) ? 0 : -1;
1357 else if (is_negative_overflow_infinity (val2
))
1359 else if (is_positive_overflow_infinity (val1
))
1360 return is_positive_overflow_infinity (val2
) ? 0 : 1;
1361 else if (is_positive_overflow_infinity (val2
))
1366 return tree_int_cst_compare (val1
, val2
);
1372 /* First see if VAL1 and VAL2 are not the same. */
1373 if (val1
== val2
|| operand_equal_p (val1
, val2
, 0))
1376 /* If VAL1 is a lower address than VAL2, return -1. */
1377 if (operand_less_p (val1
, val2
) == 1)
1380 /* If VAL1 is a higher address than VAL2, return +1. */
1381 if (operand_less_p (val2
, val1
) == 1)
1384 /* If VAL1 is different than VAL2, return +2.
1385 For integer constants we either have already returned -1 or 1
1386 or they are equivalent. We still might succeed in proving
1387 something about non-trivial operands. */
1388 if (TREE_CODE (val1
) != INTEGER_CST
1389 || TREE_CODE (val2
) != INTEGER_CST
)
1391 t
= fold_binary_to_constant (NE_EXPR
, boolean_type_node
, val1
, val2
);
1392 if (t
&& integer_onep (t
))
1400 /* Compare values like compare_values_warnv, but treat comparisons of
1401 nonconstants which rely on undefined overflow as incomparable. */
1404 compare_values (tree val1
, tree val2
)
1410 ret
= compare_values_warnv (val1
, val2
, &sop
);
1412 && (!is_gimple_min_invariant (val1
) || !is_gimple_min_invariant (val2
)))
1418 /* Return 1 if VAL is inside value range MIN <= VAL <= MAX,
1419 0 if VAL is not inside [MIN, MAX],
1420 -2 if we cannot tell either way.
1422 Benchmark compile/20001226-1.c compilation time after changing this
1426 value_inside_range (tree val
, tree min
, tree max
)
1430 cmp1
= operand_less_p (val
, min
);
1436 cmp2
= operand_less_p (max
, val
);
1444 /* Return true if value ranges VR0 and VR1 have a non-empty
1447 Benchmark compile/20001226-1.c compilation time after changing this
1452 value_ranges_intersect_p (value_range
*vr0
, value_range
*vr1
)
1454 /* The value ranges do not intersect if the maximum of the first range is
1455 less than the minimum of the second range or vice versa.
1456 When those relations are unknown, we can't do any better. */
1457 if (operand_less_p (vr0
->max
, vr1
->min
) != 0)
1459 if (operand_less_p (vr1
->max
, vr0
->min
) != 0)
1465 /* Return 1 if [MIN, MAX] includes the value zero, 0 if it does not
1466 include the value zero, -2 if we cannot tell. */
1469 range_includes_zero_p (tree min
, tree max
)
1471 tree zero
= build_int_cst (TREE_TYPE (min
), 0);
1472 return value_inside_range (zero
, min
, max
);
1475 /* Return true if *VR is know to only contain nonnegative values. */
1478 value_range_nonnegative_p (value_range
*vr
)
1480 /* Testing for VR_ANTI_RANGE is not useful here as any anti-range
1481 which would return a useful value should be encoded as a
1483 if (vr
->type
== VR_RANGE
)
1485 int result
= compare_values (vr
->min
, integer_zero_node
);
1486 return (result
== 0 || result
== 1);
1492 /* If *VR has a value rante that is a single constant value return that,
1493 otherwise return NULL_TREE. */
1496 value_range_constant_singleton (value_range
*vr
)
1498 if (vr
->type
== VR_RANGE
1499 && vrp_operand_equal_p (vr
->min
, vr
->max
)
1500 && is_gimple_min_invariant (vr
->min
))
1506 /* If OP has a value range with a single constant value return that,
1507 otherwise return NULL_TREE. This returns OP itself if OP is a
1511 op_with_constant_singleton_value_range (tree op
)
1513 if (is_gimple_min_invariant (op
))
1516 if (TREE_CODE (op
) != SSA_NAME
)
1519 return value_range_constant_singleton (get_value_range (op
));
1522 /* Return true if op is in a boolean [0, 1] value-range. */
1525 op_with_boolean_value_range_p (tree op
)
1529 if (TYPE_PRECISION (TREE_TYPE (op
)) == 1)
1532 if (integer_zerop (op
)
1533 || integer_onep (op
))
1536 if (TREE_CODE (op
) != SSA_NAME
)
1539 vr
= get_value_range (op
);
1540 return (vr
->type
== VR_RANGE
1541 && integer_zerop (vr
->min
)
1542 && integer_onep (vr
->max
));
1545 /* Extract value range information from an ASSERT_EXPR EXPR and store
1549 extract_range_from_assert (value_range
*vr_p
, tree expr
)
1551 tree var
, cond
, limit
, min
, max
, type
;
1552 value_range
*limit_vr
;
1553 enum tree_code cond_code
;
1555 var
= ASSERT_EXPR_VAR (expr
);
1556 cond
= ASSERT_EXPR_COND (expr
);
1558 gcc_assert (COMPARISON_CLASS_P (cond
));
1560 /* Find VAR in the ASSERT_EXPR conditional. */
1561 if (var
== TREE_OPERAND (cond
, 0)
1562 || TREE_CODE (TREE_OPERAND (cond
, 0)) == PLUS_EXPR
1563 || TREE_CODE (TREE_OPERAND (cond
, 0)) == NOP_EXPR
)
1565 /* If the predicate is of the form VAR COMP LIMIT, then we just
1566 take LIMIT from the RHS and use the same comparison code. */
1567 cond_code
= TREE_CODE (cond
);
1568 limit
= TREE_OPERAND (cond
, 1);
1569 cond
= TREE_OPERAND (cond
, 0);
1573 /* If the predicate is of the form LIMIT COMP VAR, then we need
1574 to flip around the comparison code to create the proper range
1576 cond_code
= swap_tree_comparison (TREE_CODE (cond
));
1577 limit
= TREE_OPERAND (cond
, 0);
1578 cond
= TREE_OPERAND (cond
, 1);
1581 limit
= avoid_overflow_infinity (limit
);
1583 type
= TREE_TYPE (var
);
1584 gcc_assert (limit
!= var
);
1586 /* For pointer arithmetic, we only keep track of pointer equality
1588 if (POINTER_TYPE_P (type
) && cond_code
!= NE_EXPR
&& cond_code
!= EQ_EXPR
)
1590 set_value_range_to_varying (vr_p
);
1594 /* If LIMIT is another SSA name and LIMIT has a range of its own,
1595 try to use LIMIT's range to avoid creating symbolic ranges
1597 limit_vr
= (TREE_CODE (limit
) == SSA_NAME
) ? get_value_range (limit
) : NULL
;
1599 /* LIMIT's range is only interesting if it has any useful information. */
1601 && (limit_vr
->type
== VR_UNDEFINED
1602 || limit_vr
->type
== VR_VARYING
1603 || symbolic_range_p (limit_vr
)))
1606 /* Initially, the new range has the same set of equivalences of
1607 VAR's range. This will be revised before returning the final
1608 value. Since assertions may be chained via mutually exclusive
1609 predicates, we will need to trim the set of equivalences before
1611 gcc_assert (vr_p
->equiv
== NULL
);
1612 add_equivalence (&vr_p
->equiv
, var
);
1614 /* Extract a new range based on the asserted comparison for VAR and
1615 LIMIT's value range. Notice that if LIMIT has an anti-range, we
1616 will only use it for equality comparisons (EQ_EXPR). For any
1617 other kind of assertion, we cannot derive a range from LIMIT's
1618 anti-range that can be used to describe the new range. For
1619 instance, ASSERT_EXPR <x_2, x_2 <= b_4>. If b_4 is ~[2, 10],
1620 then b_4 takes on the ranges [-INF, 1] and [11, +INF]. There is
1621 no single range for x_2 that could describe LE_EXPR, so we might
1622 as well build the range [b_4, +INF] for it.
1623 One special case we handle is extracting a range from a
1624 range test encoded as (unsigned)var + CST <= limit. */
1625 if (TREE_CODE (cond
) == NOP_EXPR
1626 || TREE_CODE (cond
) == PLUS_EXPR
)
1628 if (TREE_CODE (cond
) == PLUS_EXPR
)
1630 min
= fold_build1 (NEGATE_EXPR
, TREE_TYPE (TREE_OPERAND (cond
, 1)),
1631 TREE_OPERAND (cond
, 1));
1632 max
= int_const_binop (PLUS_EXPR
, limit
, min
);
1633 cond
= TREE_OPERAND (cond
, 0);
1637 min
= build_int_cst (TREE_TYPE (var
), 0);
1641 /* Make sure to not set TREE_OVERFLOW on the final type
1642 conversion. We are willingly interpreting large positive
1643 unsigned values as negative signed values here. */
1644 min
= force_fit_type (TREE_TYPE (var
), wi::to_widest (min
), 0, false);
1645 max
= force_fit_type (TREE_TYPE (var
), wi::to_widest (max
), 0, false);
1647 /* We can transform a max, min range to an anti-range or
1648 vice-versa. Use set_and_canonicalize_value_range which does
1650 if (cond_code
== LE_EXPR
)
1651 set_and_canonicalize_value_range (vr_p
, VR_RANGE
,
1652 min
, max
, vr_p
->equiv
);
1653 else if (cond_code
== GT_EXPR
)
1654 set_and_canonicalize_value_range (vr_p
, VR_ANTI_RANGE
,
1655 min
, max
, vr_p
->equiv
);
1659 else if (cond_code
== EQ_EXPR
)
1661 enum value_range_type range_type
;
1665 range_type
= limit_vr
->type
;
1666 min
= limit_vr
->min
;
1667 max
= limit_vr
->max
;
1671 range_type
= VR_RANGE
;
1676 set_value_range (vr_p
, range_type
, min
, max
, vr_p
->equiv
);
1678 /* When asserting the equality VAR == LIMIT and LIMIT is another
1679 SSA name, the new range will also inherit the equivalence set
1681 if (TREE_CODE (limit
) == SSA_NAME
)
1682 add_equivalence (&vr_p
->equiv
, limit
);
1684 else if (cond_code
== NE_EXPR
)
1686 /* As described above, when LIMIT's range is an anti-range and
1687 this assertion is an inequality (NE_EXPR), then we cannot
1688 derive anything from the anti-range. For instance, if
1689 LIMIT's range was ~[0, 0], the assertion 'VAR != LIMIT' does
1690 not imply that VAR's range is [0, 0]. So, in the case of
1691 anti-ranges, we just assert the inequality using LIMIT and
1694 If LIMIT_VR is a range, we can only use it to build a new
1695 anti-range if LIMIT_VR is a single-valued range. For
1696 instance, if LIMIT_VR is [0, 1], the predicate
1697 VAR != [0, 1] does not mean that VAR's range is ~[0, 1].
1698 Rather, it means that for value 0 VAR should be ~[0, 0]
1699 and for value 1, VAR should be ~[1, 1]. We cannot
1700 represent these ranges.
1702 The only situation in which we can build a valid
1703 anti-range is when LIMIT_VR is a single-valued range
1704 (i.e., LIMIT_VR->MIN == LIMIT_VR->MAX). In that case,
1705 build the anti-range ~[LIMIT_VR->MIN, LIMIT_VR->MAX]. */
1707 && limit_vr
->type
== VR_RANGE
1708 && compare_values (limit_vr
->min
, limit_vr
->max
) == 0)
1710 min
= limit_vr
->min
;
1711 max
= limit_vr
->max
;
1715 /* In any other case, we cannot use LIMIT's range to build a
1716 valid anti-range. */
1720 /* If MIN and MAX cover the whole range for their type, then
1721 just use the original LIMIT. */
1722 if (INTEGRAL_TYPE_P (type
)
1723 && vrp_val_is_min (min
)
1724 && vrp_val_is_max (max
))
1727 set_and_canonicalize_value_range (vr_p
, VR_ANTI_RANGE
,
1728 min
, max
, vr_p
->equiv
);
1730 else if (cond_code
== LE_EXPR
|| cond_code
== LT_EXPR
)
1732 min
= TYPE_MIN_VALUE (type
);
1734 if (limit_vr
== NULL
|| limit_vr
->type
== VR_ANTI_RANGE
)
1738 /* If LIMIT_VR is of the form [N1, N2], we need to build the
1739 range [MIN, N2] for LE_EXPR and [MIN, N2 - 1] for
1741 max
= limit_vr
->max
;
1744 /* If the maximum value forces us to be out of bounds, simply punt.
1745 It would be pointless to try and do anything more since this
1746 all should be optimized away above us. */
1747 if ((cond_code
== LT_EXPR
1748 && compare_values (max
, min
) == 0)
1749 || is_overflow_infinity (max
))
1750 set_value_range_to_varying (vr_p
);
1753 /* For LT_EXPR, we create the range [MIN, MAX - 1]. */
1754 if (cond_code
== LT_EXPR
)
1756 if (TYPE_PRECISION (TREE_TYPE (max
)) == 1
1757 && !TYPE_UNSIGNED (TREE_TYPE (max
)))
1758 max
= fold_build2 (PLUS_EXPR
, TREE_TYPE (max
), max
,
1759 build_int_cst (TREE_TYPE (max
), -1));
1761 max
= fold_build2 (MINUS_EXPR
, TREE_TYPE (max
), max
,
1762 build_int_cst (TREE_TYPE (max
), 1));
1764 TREE_NO_WARNING (max
) = 1;
1767 set_value_range (vr_p
, VR_RANGE
, min
, max
, vr_p
->equiv
);
1770 else if (cond_code
== GE_EXPR
|| cond_code
== GT_EXPR
)
1772 max
= TYPE_MAX_VALUE (type
);
1774 if (limit_vr
== NULL
|| limit_vr
->type
== VR_ANTI_RANGE
)
1778 /* If LIMIT_VR is of the form [N1, N2], we need to build the
1779 range [N1, MAX] for GE_EXPR and [N1 + 1, MAX] for
1781 min
= limit_vr
->min
;
1784 /* If the minimum value forces us to be out of bounds, simply punt.
1785 It would be pointless to try and do anything more since this
1786 all should be optimized away above us. */
1787 if ((cond_code
== GT_EXPR
1788 && compare_values (min
, max
) == 0)
1789 || is_overflow_infinity (min
))
1790 set_value_range_to_varying (vr_p
);
1793 /* For GT_EXPR, we create the range [MIN + 1, MAX]. */
1794 if (cond_code
== GT_EXPR
)
1796 if (TYPE_PRECISION (TREE_TYPE (min
)) == 1
1797 && !TYPE_UNSIGNED (TREE_TYPE (min
)))
1798 min
= fold_build2 (MINUS_EXPR
, TREE_TYPE (min
), min
,
1799 build_int_cst (TREE_TYPE (min
), -1));
1801 min
= fold_build2 (PLUS_EXPR
, TREE_TYPE (min
), min
,
1802 build_int_cst (TREE_TYPE (min
), 1));
1804 TREE_NO_WARNING (min
) = 1;
1807 set_value_range (vr_p
, VR_RANGE
, min
, max
, vr_p
->equiv
);
1813 /* Finally intersect the new range with what we already know about var. */
1814 vrp_intersect_ranges (vr_p
, get_value_range (var
));
1818 /* Extract range information from SSA name VAR and store it in VR. If
1819 VAR has an interesting range, use it. Otherwise, create the
1820 range [VAR, VAR] and return it. This is useful in situations where
1821 we may have conditionals testing values of VARYING names. For
1828 Even if y_5 is deemed VARYING, we can determine that x_3 > y_5 is
1832 extract_range_from_ssa_name (value_range
*vr
, tree var
)
1834 value_range
*var_vr
= get_value_range (var
);
1836 if (var_vr
->type
!= VR_VARYING
)
1837 copy_value_range (vr
, var_vr
);
1839 set_value_range (vr
, VR_RANGE
, var
, var
, NULL
);
1841 add_equivalence (&vr
->equiv
, var
);
1845 /* Wrapper around int_const_binop. If the operation overflows and we
1846 are not using wrapping arithmetic, then adjust the result to be
1847 -INF or +INF depending on CODE, VAL1 and VAL2. This can return
1848 NULL_TREE if we need to use an overflow infinity representation but
1849 the type does not support it. */
1852 vrp_int_const_binop (enum tree_code code
, tree val1
, tree val2
)
1856 res
= int_const_binop (code
, val1
, val2
);
1858 /* If we are using unsigned arithmetic, operate symbolically
1859 on -INF and +INF as int_const_binop only handles signed overflow. */
1860 if (TYPE_UNSIGNED (TREE_TYPE (val1
)))
1862 int checkz
= compare_values (res
, val1
);
1863 bool overflow
= false;
1865 /* Ensure that res = val1 [+*] val2 >= val1
1866 or that res = val1 - val2 <= val1. */
1867 if ((code
== PLUS_EXPR
1868 && !(checkz
== 1 || checkz
== 0))
1869 || (code
== MINUS_EXPR
1870 && !(checkz
== 0 || checkz
== -1)))
1874 /* Checking for multiplication overflow is done by dividing the
1875 output of the multiplication by the first input of the
1876 multiplication. If the result of that division operation is
1877 not equal to the second input of the multiplication, then the
1878 multiplication overflowed. */
1879 else if (code
== MULT_EXPR
&& !integer_zerop (val1
))
1881 tree tmp
= int_const_binop (TRUNC_DIV_EXPR
,
1884 int check
= compare_values (tmp
, val2
);
1892 res
= copy_node (res
);
1893 TREE_OVERFLOW (res
) = 1;
1897 else if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (val1
)))
1898 /* If the singed operation wraps then int_const_binop has done
1899 everything we want. */
1901 /* Signed division of -1/0 overflows and by the time it gets here
1902 returns NULL_TREE. */
1905 else if ((TREE_OVERFLOW (res
)
1906 && !TREE_OVERFLOW (val1
)
1907 && !TREE_OVERFLOW (val2
))
1908 || is_overflow_infinity (val1
)
1909 || is_overflow_infinity (val2
))
1911 /* If the operation overflowed but neither VAL1 nor VAL2 are
1912 overflown, return -INF or +INF depending on the operation
1913 and the combination of signs of the operands. */
1914 int sgn1
= tree_int_cst_sgn (val1
);
1915 int sgn2
= tree_int_cst_sgn (val2
);
1917 if (needs_overflow_infinity (TREE_TYPE (res
))
1918 && !supports_overflow_infinity (TREE_TYPE (res
)))
1921 /* We have to punt on adding infinities of different signs,
1922 since we can't tell what the sign of the result should be.
1923 Likewise for subtracting infinities of the same sign. */
1924 if (((code
== PLUS_EXPR
&& sgn1
!= sgn2
)
1925 || (code
== MINUS_EXPR
&& sgn1
== sgn2
))
1926 && is_overflow_infinity (val1
)
1927 && is_overflow_infinity (val2
))
1930 /* Don't try to handle division or shifting of infinities. */
1931 if ((code
== TRUNC_DIV_EXPR
1932 || code
== FLOOR_DIV_EXPR
1933 || code
== CEIL_DIV_EXPR
1934 || code
== EXACT_DIV_EXPR
1935 || code
== ROUND_DIV_EXPR
1936 || code
== RSHIFT_EXPR
)
1937 && (is_overflow_infinity (val1
)
1938 || is_overflow_infinity (val2
)))
1941 /* Notice that we only need to handle the restricted set of
1942 operations handled by extract_range_from_binary_expr.
1943 Among them, only multiplication, addition and subtraction
1944 can yield overflow without overflown operands because we
1945 are working with integral types only... except in the
1946 case VAL1 = -INF and VAL2 = -1 which overflows to +INF
1947 for division too. */
1949 /* For multiplication, the sign of the overflow is given
1950 by the comparison of the signs of the operands. */
1951 if ((code
== MULT_EXPR
&& sgn1
== sgn2
)
1952 /* For addition, the operands must be of the same sign
1953 to yield an overflow. Its sign is therefore that
1954 of one of the operands, for example the first. For
1955 infinite operands X + -INF is negative, not positive. */
1956 || (code
== PLUS_EXPR
1958 ? !is_negative_overflow_infinity (val2
)
1959 : is_positive_overflow_infinity (val2
)))
1960 /* For subtraction, non-infinite operands must be of
1961 different signs to yield an overflow. Its sign is
1962 therefore that of the first operand or the opposite of
1963 that of the second operand. A first operand of 0 counts
1964 as positive here, for the corner case 0 - (-INF), which
1965 overflows, but must yield +INF. For infinite operands 0
1966 - INF is negative, not positive. */
1967 || (code
== MINUS_EXPR
1969 ? !is_positive_overflow_infinity (val2
)
1970 : is_negative_overflow_infinity (val2
)))
1971 /* We only get in here with positive shift count, so the
1972 overflow direction is the same as the sign of val1.
1973 Actually rshift does not overflow at all, but we only
1974 handle the case of shifting overflowed -INF and +INF. */
1975 || (code
== RSHIFT_EXPR
1977 /* For division, the only case is -INF / -1 = +INF. */
1978 || code
== TRUNC_DIV_EXPR
1979 || code
== FLOOR_DIV_EXPR
1980 || code
== CEIL_DIV_EXPR
1981 || code
== EXACT_DIV_EXPR
1982 || code
== ROUND_DIV_EXPR
)
1983 return (needs_overflow_infinity (TREE_TYPE (res
))
1984 ? positive_overflow_infinity (TREE_TYPE (res
))
1985 : TYPE_MAX_VALUE (TREE_TYPE (res
)));
1987 return (needs_overflow_infinity (TREE_TYPE (res
))
1988 ? negative_overflow_infinity (TREE_TYPE (res
))
1989 : TYPE_MIN_VALUE (TREE_TYPE (res
)));
1996 /* For range VR compute two wide_int bitmasks. In *MAY_BE_NONZERO
1997 bitmask if some bit is unset, it means for all numbers in the range
1998 the bit is 0, otherwise it might be 0 or 1. In *MUST_BE_NONZERO
1999 bitmask if some bit is set, it means for all numbers in the range
2000 the bit is 1, otherwise it might be 0 or 1. */
2003 zero_nonzero_bits_from_vr (const tree expr_type
,
2005 wide_int
*may_be_nonzero
,
2006 wide_int
*must_be_nonzero
)
2008 *may_be_nonzero
= wi::minus_one (TYPE_PRECISION (expr_type
));
2009 *must_be_nonzero
= wi::zero (TYPE_PRECISION (expr_type
));
2010 if (!range_int_cst_p (vr
)
2011 || is_overflow_infinity (vr
->min
)
2012 || is_overflow_infinity (vr
->max
))
2015 if (range_int_cst_singleton_p (vr
))
2017 *may_be_nonzero
= vr
->min
;
2018 *must_be_nonzero
= *may_be_nonzero
;
2020 else if (tree_int_cst_sgn (vr
->min
) >= 0
2021 || tree_int_cst_sgn (vr
->max
) < 0)
2023 wide_int xor_mask
= wi::bit_xor (vr
->min
, vr
->max
);
2024 *may_be_nonzero
= wi::bit_or (vr
->min
, vr
->max
);
2025 *must_be_nonzero
= wi::bit_and (vr
->min
, vr
->max
);
2028 wide_int mask
= wi::mask (wi::floor_log2 (xor_mask
), false,
2029 may_be_nonzero
->get_precision ());
2030 *may_be_nonzero
= *may_be_nonzero
| mask
;
2031 *must_be_nonzero
= must_be_nonzero
->and_not (mask
);
2038 /* Create two value-ranges in *VR0 and *VR1 from the anti-range *AR
2039 so that *VR0 U *VR1 == *AR. Returns true if that is possible,
2040 false otherwise. If *AR can be represented with a single range
2041 *VR1 will be VR_UNDEFINED. */
2044 ranges_from_anti_range (value_range
*ar
,
2045 value_range
*vr0
, value_range
*vr1
)
2047 tree type
= TREE_TYPE (ar
->min
);
2049 vr0
->type
= VR_UNDEFINED
;
2050 vr1
->type
= VR_UNDEFINED
;
2052 if (ar
->type
!= VR_ANTI_RANGE
2053 || TREE_CODE (ar
->min
) != INTEGER_CST
2054 || TREE_CODE (ar
->max
) != INTEGER_CST
2055 || !vrp_val_min (type
)
2056 || !vrp_val_max (type
))
2059 if (!vrp_val_is_min (ar
->min
))
2061 vr0
->type
= VR_RANGE
;
2062 vr0
->min
= vrp_val_min (type
);
2063 vr0
->max
= wide_int_to_tree (type
, wi::sub (ar
->min
, 1));
2065 if (!vrp_val_is_max (ar
->max
))
2067 vr1
->type
= VR_RANGE
;
2068 vr1
->min
= wide_int_to_tree (type
, wi::add (ar
->max
, 1));
2069 vr1
->max
= vrp_val_max (type
);
2071 if (vr0
->type
== VR_UNDEFINED
)
2074 vr1
->type
= VR_UNDEFINED
;
2077 return vr0
->type
!= VR_UNDEFINED
;
2080 /* Helper to extract a value-range *VR for a multiplicative operation
2084 extract_range_from_multiplicative_op_1 (value_range
*vr
,
2085 enum tree_code code
,
2086 value_range
*vr0
, value_range
*vr1
)
2088 enum value_range_type type
;
2095 /* Multiplications, divisions and shifts are a bit tricky to handle,
2096 depending on the mix of signs we have in the two ranges, we
2097 need to operate on different values to get the minimum and
2098 maximum values for the new range. One approach is to figure
2099 out all the variations of range combinations and do the
2102 However, this involves several calls to compare_values and it
2103 is pretty convoluted. It's simpler to do the 4 operations
2104 (MIN0 OP MIN1, MIN0 OP MAX1, MAX0 OP MIN1 and MAX0 OP MAX0 OP
2105 MAX1) and then figure the smallest and largest values to form
2107 gcc_assert (code
== MULT_EXPR
2108 || code
== TRUNC_DIV_EXPR
2109 || code
== FLOOR_DIV_EXPR
2110 || code
== CEIL_DIV_EXPR
2111 || code
== EXACT_DIV_EXPR
2112 || code
== ROUND_DIV_EXPR
2113 || code
== RSHIFT_EXPR
2114 || code
== LSHIFT_EXPR
);
2115 gcc_assert ((vr0
->type
== VR_RANGE
2116 || (code
== MULT_EXPR
&& vr0
->type
== VR_ANTI_RANGE
))
2117 && vr0
->type
== vr1
->type
);
2121 /* Compute the 4 cross operations. */
2123 val
[0] = vrp_int_const_binop (code
, vr0
->min
, vr1
->min
);
2124 if (val
[0] == NULL_TREE
)
2127 if (vr1
->max
== vr1
->min
)
2131 val
[1] = vrp_int_const_binop (code
, vr0
->min
, vr1
->max
);
2132 if (val
[1] == NULL_TREE
)
2136 if (vr0
->max
== vr0
->min
)
2140 val
[2] = vrp_int_const_binop (code
, vr0
->max
, vr1
->min
);
2141 if (val
[2] == NULL_TREE
)
2145 if (vr0
->min
== vr0
->max
|| vr1
->min
== vr1
->max
)
2149 val
[3] = vrp_int_const_binop (code
, vr0
->max
, vr1
->max
);
2150 if (val
[3] == NULL_TREE
)
2156 set_value_range_to_varying (vr
);
2160 /* Set MIN to the minimum of VAL[i] and MAX to the maximum
2164 for (i
= 1; i
< 4; i
++)
2166 if (!is_gimple_min_invariant (min
)
2167 || (TREE_OVERFLOW (min
) && !is_overflow_infinity (min
))
2168 || !is_gimple_min_invariant (max
)
2169 || (TREE_OVERFLOW (max
) && !is_overflow_infinity (max
)))
2174 if (!is_gimple_min_invariant (val
[i
])
2175 || (TREE_OVERFLOW (val
[i
])
2176 && !is_overflow_infinity (val
[i
])))
2178 /* If we found an overflowed value, set MIN and MAX
2179 to it so that we set the resulting range to
2185 if (compare_values (val
[i
], min
) == -1)
2188 if (compare_values (val
[i
], max
) == 1)
2193 /* If either MIN or MAX overflowed, then set the resulting range to
2194 VARYING. But we do accept an overflow infinity
2196 if (min
== NULL_TREE
2197 || !is_gimple_min_invariant (min
)
2198 || (TREE_OVERFLOW (min
) && !is_overflow_infinity (min
))
2200 || !is_gimple_min_invariant (max
)
2201 || (TREE_OVERFLOW (max
) && !is_overflow_infinity (max
)))
2203 set_value_range_to_varying (vr
);
2209 2) [-INF, +-INF(OVF)]
2210 3) [+-INF(OVF), +INF]
2211 4) [+-INF(OVF), +-INF(OVF)]
2212 We learn nothing when we have INF and INF(OVF) on both sides.
2213 Note that we do accept [-INF, -INF] and [+INF, +INF] without
2215 if ((vrp_val_is_min (min
) || is_overflow_infinity (min
))
2216 && (vrp_val_is_max (max
) || is_overflow_infinity (max
)))
2218 set_value_range_to_varying (vr
);
2222 cmp
= compare_values (min
, max
);
2223 if (cmp
== -2 || cmp
== 1)
2225 /* If the new range has its limits swapped around (MIN > MAX),
2226 then the operation caused one of them to wrap around, mark
2227 the new range VARYING. */
2228 set_value_range_to_varying (vr
);
2231 set_value_range (vr
, type
, min
, max
, NULL
);
2234 /* Extract range information from a binary operation CODE based on
2235 the ranges of each of its operands *VR0 and *VR1 with resulting
2236 type EXPR_TYPE. The resulting range is stored in *VR. */
2239 extract_range_from_binary_expr_1 (value_range
*vr
,
2240 enum tree_code code
, tree expr_type
,
2241 value_range
*vr0_
, value_range
*vr1_
)
2243 value_range vr0
= *vr0_
, vr1
= *vr1_
;
2244 value_range vrtem0
= VR_INITIALIZER
, vrtem1
= VR_INITIALIZER
;
2245 enum value_range_type type
;
2246 tree min
= NULL_TREE
, max
= NULL_TREE
;
2249 if (!INTEGRAL_TYPE_P (expr_type
)
2250 && !POINTER_TYPE_P (expr_type
))
2252 set_value_range_to_varying (vr
);
2256 /* Not all binary expressions can be applied to ranges in a
2257 meaningful way. Handle only arithmetic operations. */
2258 if (code
!= PLUS_EXPR
2259 && code
!= MINUS_EXPR
2260 && code
!= POINTER_PLUS_EXPR
2261 && code
!= MULT_EXPR
2262 && code
!= TRUNC_DIV_EXPR
2263 && code
!= FLOOR_DIV_EXPR
2264 && code
!= CEIL_DIV_EXPR
2265 && code
!= EXACT_DIV_EXPR
2266 && code
!= ROUND_DIV_EXPR
2267 && code
!= TRUNC_MOD_EXPR
2268 && code
!= RSHIFT_EXPR
2269 && code
!= LSHIFT_EXPR
2272 && code
!= BIT_AND_EXPR
2273 && code
!= BIT_IOR_EXPR
2274 && code
!= BIT_XOR_EXPR
)
2276 set_value_range_to_varying (vr
);
2280 /* If both ranges are UNDEFINED, so is the result. */
2281 if (vr0
.type
== VR_UNDEFINED
&& vr1
.type
== VR_UNDEFINED
)
2283 set_value_range_to_undefined (vr
);
2286 /* If one of the ranges is UNDEFINED drop it to VARYING for the following
2287 code. At some point we may want to special-case operations that
2288 have UNDEFINED result for all or some value-ranges of the not UNDEFINED
2290 else if (vr0
.type
== VR_UNDEFINED
)
2291 set_value_range_to_varying (&vr0
);
2292 else if (vr1
.type
== VR_UNDEFINED
)
2293 set_value_range_to_varying (&vr1
);
2295 /* Now canonicalize anti-ranges to ranges when they are not symbolic
2296 and express ~[] op X as ([]' op X) U ([]'' op X). */
2297 if (vr0
.type
== VR_ANTI_RANGE
2298 && ranges_from_anti_range (&vr0
, &vrtem0
, &vrtem1
))
2300 extract_range_from_binary_expr_1 (vr
, code
, expr_type
, &vrtem0
, vr1_
);
2301 if (vrtem1
.type
!= VR_UNDEFINED
)
2303 value_range vrres
= VR_INITIALIZER
;
2304 extract_range_from_binary_expr_1 (&vrres
, code
, expr_type
,
2306 vrp_meet (vr
, &vrres
);
2310 /* Likewise for X op ~[]. */
2311 if (vr1
.type
== VR_ANTI_RANGE
2312 && ranges_from_anti_range (&vr1
, &vrtem0
, &vrtem1
))
2314 extract_range_from_binary_expr_1 (vr
, code
, expr_type
, vr0_
, &vrtem0
);
2315 if (vrtem1
.type
!= VR_UNDEFINED
)
2317 value_range vrres
= VR_INITIALIZER
;
2318 extract_range_from_binary_expr_1 (&vrres
, code
, expr_type
,
2320 vrp_meet (vr
, &vrres
);
2325 /* The type of the resulting value range defaults to VR0.TYPE. */
2328 /* Refuse to operate on VARYING ranges, ranges of different kinds
2329 and symbolic ranges. As an exception, we allow BIT_{AND,IOR}
2330 because we may be able to derive a useful range even if one of
2331 the operands is VR_VARYING or symbolic range. Similarly for
2332 divisions, MIN/MAX and PLUS/MINUS.
2334 TODO, we may be able to derive anti-ranges in some cases. */
2335 if (code
!= BIT_AND_EXPR
2336 && code
!= BIT_IOR_EXPR
2337 && code
!= TRUNC_DIV_EXPR
2338 && code
!= FLOOR_DIV_EXPR
2339 && code
!= CEIL_DIV_EXPR
2340 && code
!= EXACT_DIV_EXPR
2341 && code
!= ROUND_DIV_EXPR
2342 && code
!= TRUNC_MOD_EXPR
2345 && code
!= PLUS_EXPR
2346 && code
!= MINUS_EXPR
2347 && code
!= RSHIFT_EXPR
2348 && (vr0
.type
== VR_VARYING
2349 || vr1
.type
== VR_VARYING
2350 || vr0
.type
!= vr1
.type
2351 || symbolic_range_p (&vr0
)
2352 || symbolic_range_p (&vr1
)))
2354 set_value_range_to_varying (vr
);
2358 /* Now evaluate the expression to determine the new range. */
2359 if (POINTER_TYPE_P (expr_type
))
2361 if (code
== MIN_EXPR
|| code
== MAX_EXPR
)
2363 /* For MIN/MAX expressions with pointers, we only care about
2364 nullness, if both are non null, then the result is nonnull.
2365 If both are null, then the result is null. Otherwise they
2367 if (range_is_nonnull (&vr0
) && range_is_nonnull (&vr1
))
2368 set_value_range_to_nonnull (vr
, expr_type
);
2369 else if (range_is_null (&vr0
) && range_is_null (&vr1
))
2370 set_value_range_to_null (vr
, expr_type
);
2372 set_value_range_to_varying (vr
);
2374 else if (code
== POINTER_PLUS_EXPR
)
2376 /* For pointer types, we are really only interested in asserting
2377 whether the expression evaluates to non-NULL. */
2378 if (range_is_nonnull (&vr0
) || range_is_nonnull (&vr1
))
2379 set_value_range_to_nonnull (vr
, expr_type
);
2380 else if (range_is_null (&vr0
) && range_is_null (&vr1
))
2381 set_value_range_to_null (vr
, expr_type
);
2383 set_value_range_to_varying (vr
);
2385 else if (code
== BIT_AND_EXPR
)
2387 /* For pointer types, we are really only interested in asserting
2388 whether the expression evaluates to non-NULL. */
2389 if (range_is_nonnull (&vr0
) && range_is_nonnull (&vr1
))
2390 set_value_range_to_nonnull (vr
, expr_type
);
2391 else if (range_is_null (&vr0
) || range_is_null (&vr1
))
2392 set_value_range_to_null (vr
, expr_type
);
2394 set_value_range_to_varying (vr
);
2397 set_value_range_to_varying (vr
);
2402 /* For integer ranges, apply the operation to each end of the
2403 range and see what we end up with. */
2404 if (code
== PLUS_EXPR
|| code
== MINUS_EXPR
)
2406 const bool minus_p
= (code
== MINUS_EXPR
);
2407 tree min_op0
= vr0
.min
;
2408 tree min_op1
= minus_p
? vr1
.max
: vr1
.min
;
2409 tree max_op0
= vr0
.max
;
2410 tree max_op1
= minus_p
? vr1
.min
: vr1
.max
;
2411 tree sym_min_op0
= NULL_TREE
;
2412 tree sym_min_op1
= NULL_TREE
;
2413 tree sym_max_op0
= NULL_TREE
;
2414 tree sym_max_op1
= NULL_TREE
;
2415 bool neg_min_op0
, neg_min_op1
, neg_max_op0
, neg_max_op1
;
2417 /* If we have a PLUS or MINUS with two VR_RANGEs, either constant or
2418 single-symbolic ranges, try to compute the precise resulting range,
2419 but only if we know that this resulting range will also be constant
2420 or single-symbolic. */
2421 if (vr0
.type
== VR_RANGE
&& vr1
.type
== VR_RANGE
2422 && (TREE_CODE (min_op0
) == INTEGER_CST
2424 = get_single_symbol (min_op0
, &neg_min_op0
, &min_op0
)))
2425 && (TREE_CODE (min_op1
) == INTEGER_CST
2427 = get_single_symbol (min_op1
, &neg_min_op1
, &min_op1
)))
2428 && (!(sym_min_op0
&& sym_min_op1
)
2429 || (sym_min_op0
== sym_min_op1
2430 && neg_min_op0
== (minus_p
? neg_min_op1
: !neg_min_op1
)))
2431 && (TREE_CODE (max_op0
) == INTEGER_CST
2433 = get_single_symbol (max_op0
, &neg_max_op0
, &max_op0
)))
2434 && (TREE_CODE (max_op1
) == INTEGER_CST
2436 = get_single_symbol (max_op1
, &neg_max_op1
, &max_op1
)))
2437 && (!(sym_max_op0
&& sym_max_op1
)
2438 || (sym_max_op0
== sym_max_op1
2439 && neg_max_op0
== (minus_p
? neg_max_op1
: !neg_max_op1
))))
2441 const signop sgn
= TYPE_SIGN (expr_type
);
2442 const unsigned int prec
= TYPE_PRECISION (expr_type
);
2443 wide_int type_min
, type_max
, wmin
, wmax
;
2447 /* Get the lower and upper bounds of the type. */
2448 if (TYPE_OVERFLOW_WRAPS (expr_type
))
2450 type_min
= wi::min_value (prec
, sgn
);
2451 type_max
= wi::max_value (prec
, sgn
);
2455 type_min
= vrp_val_min (expr_type
);
2456 type_max
= vrp_val_max (expr_type
);
2459 /* Combine the lower bounds, if any. */
2460 if (min_op0
&& min_op1
)
2464 wmin
= wi::sub (min_op0
, min_op1
);
2466 /* Check for overflow. */
2467 if (wi::cmp (0, min_op1
, sgn
)
2468 != wi::cmp (wmin
, min_op0
, sgn
))
2469 min_ovf
= wi::cmp (min_op0
, min_op1
, sgn
);
2473 wmin
= wi::add (min_op0
, min_op1
);
2475 /* Check for overflow. */
2476 if (wi::cmp (min_op1
, 0, sgn
)
2477 != wi::cmp (wmin
, min_op0
, sgn
))
2478 min_ovf
= wi::cmp (min_op0
, wmin
, sgn
);
2484 wmin
= minus_p
? wi::neg (min_op1
) : min_op1
;
2486 wmin
= wi::shwi (0, prec
);
2488 /* Combine the upper bounds, if any. */
2489 if (max_op0
&& max_op1
)
2493 wmax
= wi::sub (max_op0
, max_op1
);
2495 /* Check for overflow. */
2496 if (wi::cmp (0, max_op1
, sgn
)
2497 != wi::cmp (wmax
, max_op0
, sgn
))
2498 max_ovf
= wi::cmp (max_op0
, max_op1
, sgn
);
2502 wmax
= wi::add (max_op0
, max_op1
);
2504 if (wi::cmp (max_op1
, 0, sgn
)
2505 != wi::cmp (wmax
, max_op0
, sgn
))
2506 max_ovf
= wi::cmp (max_op0
, wmax
, sgn
);
2512 wmax
= minus_p
? wi::neg (max_op1
) : max_op1
;
2514 wmax
= wi::shwi (0, prec
);
2516 /* Check for type overflow. */
2519 if (wi::cmp (wmin
, type_min
, sgn
) == -1)
2521 else if (wi::cmp (wmin
, type_max
, sgn
) == 1)
2526 if (wi::cmp (wmax
, type_min
, sgn
) == -1)
2528 else if (wi::cmp (wmax
, type_max
, sgn
) == 1)
2532 /* If we have overflow for the constant part and the resulting
2533 range will be symbolic, drop to VR_VARYING. */
2534 if ((min_ovf
&& sym_min_op0
!= sym_min_op1
)
2535 || (max_ovf
&& sym_max_op0
!= sym_max_op1
))
2537 set_value_range_to_varying (vr
);
2541 if (TYPE_OVERFLOW_WRAPS (expr_type
))
2543 /* If overflow wraps, truncate the values and adjust the
2544 range kind and bounds appropriately. */
2545 wide_int tmin
= wide_int::from (wmin
, prec
, sgn
);
2546 wide_int tmax
= wide_int::from (wmax
, prec
, sgn
);
2547 if (min_ovf
== max_ovf
)
2549 /* No overflow or both overflow or underflow. The
2550 range kind stays VR_RANGE. */
2551 min
= wide_int_to_tree (expr_type
, tmin
);
2552 max
= wide_int_to_tree (expr_type
, tmax
);
2554 else if ((min_ovf
== -1 && max_ovf
== 0)
2555 || (max_ovf
== 1 && min_ovf
== 0))
2557 /* Min underflow or max overflow. The range kind
2558 changes to VR_ANTI_RANGE. */
2559 bool covers
= false;
2560 wide_int tem
= tmin
;
2561 type
= VR_ANTI_RANGE
;
2563 if (wi::cmp (tmin
, tmax
, sgn
) < 0)
2566 if (wi::cmp (tmax
, tem
, sgn
) > 0)
2568 /* If the anti-range would cover nothing, drop to varying.
2569 Likewise if the anti-range bounds are outside of the
2571 if (covers
|| wi::cmp (tmin
, tmax
, sgn
) > 0)
2573 set_value_range_to_varying (vr
);
2576 min
= wide_int_to_tree (expr_type
, tmin
);
2577 max
= wide_int_to_tree (expr_type
, tmax
);
2581 /* Other underflow and/or overflow, drop to VR_VARYING. */
2582 set_value_range_to_varying (vr
);
2588 /* If overflow does not wrap, saturate to the types min/max
2592 if (needs_overflow_infinity (expr_type
)
2593 && supports_overflow_infinity (expr_type
))
2594 min
= negative_overflow_infinity (expr_type
);
2596 min
= wide_int_to_tree (expr_type
, type_min
);
2598 else if (min_ovf
== 1)
2600 if (needs_overflow_infinity (expr_type
)
2601 && supports_overflow_infinity (expr_type
))
2602 min
= positive_overflow_infinity (expr_type
);
2604 min
= wide_int_to_tree (expr_type
, type_max
);
2607 min
= wide_int_to_tree (expr_type
, wmin
);
2611 if (needs_overflow_infinity (expr_type
)
2612 && supports_overflow_infinity (expr_type
))
2613 max
= negative_overflow_infinity (expr_type
);
2615 max
= wide_int_to_tree (expr_type
, type_min
);
2617 else if (max_ovf
== 1)
2619 if (needs_overflow_infinity (expr_type
)
2620 && supports_overflow_infinity (expr_type
))
2621 max
= positive_overflow_infinity (expr_type
);
2623 max
= wide_int_to_tree (expr_type
, type_max
);
2626 max
= wide_int_to_tree (expr_type
, wmax
);
2629 if (needs_overflow_infinity (expr_type
)
2630 && supports_overflow_infinity (expr_type
))
2632 if ((min_op0
&& is_negative_overflow_infinity (min_op0
))
2635 ? is_positive_overflow_infinity (min_op1
)
2636 : is_negative_overflow_infinity (min_op1
))))
2637 min
= negative_overflow_infinity (expr_type
);
2638 if ((max_op0
&& is_positive_overflow_infinity (max_op0
))
2641 ? is_negative_overflow_infinity (max_op1
)
2642 : is_positive_overflow_infinity (max_op1
))))
2643 max
= positive_overflow_infinity (expr_type
);
2646 /* If the result lower bound is constant, we're done;
2647 otherwise, build the symbolic lower bound. */
2648 if (sym_min_op0
== sym_min_op1
)
2650 else if (sym_min_op0
)
2651 min
= build_symbolic_expr (expr_type
, sym_min_op0
,
2653 else if (sym_min_op1
)
2654 min
= build_symbolic_expr (expr_type
, sym_min_op1
,
2655 neg_min_op1
^ minus_p
, min
);
2657 /* Likewise for the upper bound. */
2658 if (sym_max_op0
== sym_max_op1
)
2660 else if (sym_max_op0
)
2661 max
= build_symbolic_expr (expr_type
, sym_max_op0
,
2663 else if (sym_max_op1
)
2664 max
= build_symbolic_expr (expr_type
, sym_max_op1
,
2665 neg_max_op1
^ minus_p
, max
);
2669 /* For other cases, for example if we have a PLUS_EXPR with two
2670 VR_ANTI_RANGEs, drop to VR_VARYING. It would take more effort
2671 to compute a precise range for such a case.
2672 ??? General even mixed range kind operations can be expressed
2673 by for example transforming ~[3, 5] + [1, 2] to range-only
2674 operations and a union primitive:
2675 [-INF, 2] + [1, 2] U [5, +INF] + [1, 2]
2676 [-INF+1, 4] U [6, +INF(OVF)]
2677 though usually the union is not exactly representable with
2678 a single range or anti-range as the above is
2679 [-INF+1, +INF(OVF)] intersected with ~[5, 5]
2680 but one could use a scheme similar to equivalences for this. */
2681 set_value_range_to_varying (vr
);
2685 else if (code
== MIN_EXPR
2686 || code
== MAX_EXPR
)
2688 if (vr0
.type
== VR_RANGE
2689 && !symbolic_range_p (&vr0
))
2692 if (vr1
.type
== VR_RANGE
2693 && !symbolic_range_p (&vr1
))
2695 /* For operations that make the resulting range directly
2696 proportional to the original ranges, apply the operation to
2697 the same end of each range. */
2698 min
= vrp_int_const_binop (code
, vr0
.min
, vr1
.min
);
2699 max
= vrp_int_const_binop (code
, vr0
.max
, vr1
.max
);
2701 else if (code
== MIN_EXPR
)
2703 min
= vrp_val_min (expr_type
);
2706 else if (code
== MAX_EXPR
)
2709 max
= vrp_val_max (expr_type
);
2712 else if (vr1
.type
== VR_RANGE
2713 && !symbolic_range_p (&vr1
))
2716 if (code
== MIN_EXPR
)
2718 min
= vrp_val_min (expr_type
);
2721 else if (code
== MAX_EXPR
)
2724 max
= vrp_val_max (expr_type
);
2729 set_value_range_to_varying (vr
);
2733 else if (code
== MULT_EXPR
)
2735 /* Fancy code so that with unsigned, [-3,-1]*[-3,-1] does not
2736 drop to varying. This test requires 2*prec bits if both
2737 operands are signed and 2*prec + 2 bits if either is not. */
2739 signop sign
= TYPE_SIGN (expr_type
);
2740 unsigned int prec
= TYPE_PRECISION (expr_type
);
2742 if (range_int_cst_p (&vr0
)
2743 && range_int_cst_p (&vr1
)
2744 && TYPE_OVERFLOW_WRAPS (expr_type
))
2746 typedef FIXED_WIDE_INT (WIDE_INT_MAX_PRECISION
* 2) vrp_int
;
2747 typedef generic_wide_int
2748 <wi::extended_tree
<WIDE_INT_MAX_PRECISION
* 2> > vrp_int_cst
;
2749 vrp_int sizem1
= wi::mask
<vrp_int
> (prec
, false);
2750 vrp_int size
= sizem1
+ 1;
2752 /* Extend the values using the sign of the result to PREC2.
2753 From here on out, everthing is just signed math no matter
2754 what the input types were. */
2755 vrp_int min0
= vrp_int_cst (vr0
.min
);
2756 vrp_int max0
= vrp_int_cst (vr0
.max
);
2757 vrp_int min1
= vrp_int_cst (vr1
.min
);
2758 vrp_int max1
= vrp_int_cst (vr1
.max
);
2759 /* Canonicalize the intervals. */
2760 if (sign
== UNSIGNED
)
2762 if (wi::ltu_p (size
, min0
+ max0
))
2768 if (wi::ltu_p (size
, min1
+ max1
))
2775 vrp_int prod0
= min0
* min1
;
2776 vrp_int prod1
= min0
* max1
;
2777 vrp_int prod2
= max0
* min1
;
2778 vrp_int prod3
= max0
* max1
;
2780 /* Sort the 4 products so that min is in prod0 and max is in
2782 /* min0min1 > max0max1 */
2783 if (wi::gts_p (prod0
, prod3
))
2784 std::swap (prod0
, prod3
);
2786 /* min0max1 > max0min1 */
2787 if (wi::gts_p (prod1
, prod2
))
2788 std::swap (prod1
, prod2
);
2790 if (wi::gts_p (prod0
, prod1
))
2791 std::swap (prod0
, prod1
);
2793 if (wi::gts_p (prod2
, prod3
))
2794 std::swap (prod2
, prod3
);
2796 /* diff = max - min. */
2797 prod2
= prod3
- prod0
;
2798 if (wi::geu_p (prod2
, sizem1
))
2800 /* the range covers all values. */
2801 set_value_range_to_varying (vr
);
2805 /* The following should handle the wrapping and selecting
2806 VR_ANTI_RANGE for us. */
2807 min
= wide_int_to_tree (expr_type
, prod0
);
2808 max
= wide_int_to_tree (expr_type
, prod3
);
2809 set_and_canonicalize_value_range (vr
, VR_RANGE
, min
, max
, NULL
);
2813 /* If we have an unsigned MULT_EXPR with two VR_ANTI_RANGEs,
2814 drop to VR_VARYING. It would take more effort to compute a
2815 precise range for such a case. For example, if we have
2816 op0 == 65536 and op1 == 65536 with their ranges both being
2817 ~[0,0] on a 32-bit machine, we would have op0 * op1 == 0, so
2818 we cannot claim that the product is in ~[0,0]. Note that we
2819 are guaranteed to have vr0.type == vr1.type at this
2821 if (vr0
.type
== VR_ANTI_RANGE
2822 && !TYPE_OVERFLOW_UNDEFINED (expr_type
))
2824 set_value_range_to_varying (vr
);
2828 extract_range_from_multiplicative_op_1 (vr
, code
, &vr0
, &vr1
);
2831 else if (code
== RSHIFT_EXPR
2832 || code
== LSHIFT_EXPR
)
2834 /* If we have a RSHIFT_EXPR with any shift values outside [0..prec-1],
2835 then drop to VR_VARYING. Outside of this range we get undefined
2836 behavior from the shift operation. We cannot even trust
2837 SHIFT_COUNT_TRUNCATED at this stage, because that applies to rtl
2838 shifts, and the operation at the tree level may be widened. */
2839 if (range_int_cst_p (&vr1
)
2840 && compare_tree_int (vr1
.min
, 0) >= 0
2841 && compare_tree_int (vr1
.max
, TYPE_PRECISION (expr_type
)) == -1)
2843 if (code
== RSHIFT_EXPR
)
2845 /* Even if vr0 is VARYING or otherwise not usable, we can derive
2846 useful ranges just from the shift count. E.g.
2847 x >> 63 for signed 64-bit x is always [-1, 0]. */
2848 if (vr0
.type
!= VR_RANGE
|| symbolic_range_p (&vr0
))
2850 vr0
.type
= type
= VR_RANGE
;
2851 vr0
.min
= vrp_val_min (expr_type
);
2852 vr0
.max
= vrp_val_max (expr_type
);
2854 extract_range_from_multiplicative_op_1 (vr
, code
, &vr0
, &vr1
);
2857 /* We can map lshifts by constants to MULT_EXPR handling. */
2858 else if (code
== LSHIFT_EXPR
2859 && range_int_cst_singleton_p (&vr1
))
2861 bool saved_flag_wrapv
;
2862 value_range vr1p
= VR_INITIALIZER
;
2863 vr1p
.type
= VR_RANGE
;
2864 vr1p
.min
= (wide_int_to_tree
2866 wi::set_bit_in_zero (tree_to_shwi (vr1
.min
),
2867 TYPE_PRECISION (expr_type
))));
2868 vr1p
.max
= vr1p
.min
;
2869 /* We have to use a wrapping multiply though as signed overflow
2870 on lshifts is implementation defined in C89. */
2871 saved_flag_wrapv
= flag_wrapv
;
2873 extract_range_from_binary_expr_1 (vr
, MULT_EXPR
, expr_type
,
2875 flag_wrapv
= saved_flag_wrapv
;
2878 else if (code
== LSHIFT_EXPR
2879 && range_int_cst_p (&vr0
))
2881 int prec
= TYPE_PRECISION (expr_type
);
2882 int overflow_pos
= prec
;
2884 wide_int low_bound
, high_bound
;
2885 bool uns
= TYPE_UNSIGNED (expr_type
);
2886 bool in_bounds
= false;
2891 bound_shift
= overflow_pos
- tree_to_shwi (vr1
.max
);
2892 /* If bound_shift == HOST_BITS_PER_WIDE_INT, the llshift can
2893 overflow. However, for that to happen, vr1.max needs to be
2894 zero, which means vr1 is a singleton range of zero, which
2895 means it should be handled by the previous LSHIFT_EXPR
2897 wide_int bound
= wi::set_bit_in_zero (bound_shift
, prec
);
2898 wide_int complement
= ~(bound
- 1);
2903 high_bound
= complement
;
2904 if (wi::ltu_p (vr0
.max
, low_bound
))
2906 /* [5, 6] << [1, 2] == [10, 24]. */
2907 /* We're shifting out only zeroes, the value increases
2911 else if (wi::ltu_p (high_bound
, vr0
.min
))
2913 /* [0xffffff00, 0xffffffff] << [1, 2]
2914 == [0xfffffc00, 0xfffffffe]. */
2915 /* We're shifting out only ones, the value decreases
2922 /* [-1, 1] << [1, 2] == [-4, 4]. */
2923 low_bound
= complement
;
2925 if (wi::lts_p (vr0
.max
, high_bound
)
2926 && wi::lts_p (low_bound
, vr0
.min
))
2928 /* For non-negative numbers, we're shifting out only
2929 zeroes, the value increases monotonically.
2930 For negative numbers, we're shifting out only ones, the
2931 value decreases monotomically. */
2938 extract_range_from_multiplicative_op_1 (vr
, code
, &vr0
, &vr1
);
2943 set_value_range_to_varying (vr
);
2946 else if (code
== TRUNC_DIV_EXPR
2947 || code
== FLOOR_DIV_EXPR
2948 || code
== CEIL_DIV_EXPR
2949 || code
== EXACT_DIV_EXPR
2950 || code
== ROUND_DIV_EXPR
)
2952 if (vr0
.type
!= VR_RANGE
|| symbolic_range_p (&vr0
))
2954 /* For division, if op1 has VR_RANGE but op0 does not, something
2955 can be deduced just from that range. Say [min, max] / [4, max]
2956 gives [min / 4, max / 4] range. */
2957 if (vr1
.type
== VR_RANGE
2958 && !symbolic_range_p (&vr1
)
2959 && range_includes_zero_p (vr1
.min
, vr1
.max
) == 0)
2961 vr0
.type
= type
= VR_RANGE
;
2962 vr0
.min
= vrp_val_min (expr_type
);
2963 vr0
.max
= vrp_val_max (expr_type
);
2967 set_value_range_to_varying (vr
);
2972 /* For divisions, if flag_non_call_exceptions is true, we must
2973 not eliminate a division by zero. */
2974 if (cfun
->can_throw_non_call_exceptions
2975 && (vr1
.type
!= VR_RANGE
2976 || range_includes_zero_p (vr1
.min
, vr1
.max
) != 0))
2978 set_value_range_to_varying (vr
);
2982 /* For divisions, if op0 is VR_RANGE, we can deduce a range
2983 even if op1 is VR_VARYING, VR_ANTI_RANGE, symbolic or can
2985 if (vr0
.type
== VR_RANGE
2986 && (vr1
.type
!= VR_RANGE
2987 || range_includes_zero_p (vr1
.min
, vr1
.max
) != 0))
2989 tree zero
= build_int_cst (TREE_TYPE (vr0
.min
), 0);
2994 if (TYPE_UNSIGNED (expr_type
)
2995 || value_range_nonnegative_p (&vr1
))
2997 /* For unsigned division or when divisor is known
2998 to be non-negative, the range has to cover
2999 all numbers from 0 to max for positive max
3000 and all numbers from min to 0 for negative min. */
3001 cmp
= compare_values (vr0
.max
, zero
);
3004 /* When vr0.max < 0, vr1.min != 0 and value
3005 ranges for dividend and divisor are available. */
3006 if (vr1
.type
== VR_RANGE
3007 && !symbolic_range_p (&vr0
)
3008 && !symbolic_range_p (&vr1
)
3009 && compare_values (vr1
.min
, zero
) != 0)
3010 max
= int_const_binop (code
, vr0
.max
, vr1
.min
);
3014 else if (cmp
== 0 || cmp
== 1)
3018 cmp
= compare_values (vr0
.min
, zero
);
3021 /* For unsigned division when value ranges for dividend
3022 and divisor are available. */
3023 if (vr1
.type
== VR_RANGE
3024 && !symbolic_range_p (&vr0
)
3025 && !symbolic_range_p (&vr1
)
3026 && compare_values (vr1
.max
, zero
) != 0)
3027 min
= int_const_binop (code
, vr0
.min
, vr1
.max
);
3031 else if (cmp
== 0 || cmp
== -1)
3038 /* Otherwise the range is -max .. max or min .. -min
3039 depending on which bound is bigger in absolute value,
3040 as the division can change the sign. */
3041 abs_extent_range (vr
, vr0
.min
, vr0
.max
);
3044 if (type
== VR_VARYING
)
3046 set_value_range_to_varying (vr
);
3050 else if (!symbolic_range_p (&vr0
) && !symbolic_range_p (&vr1
))
3052 extract_range_from_multiplicative_op_1 (vr
, code
, &vr0
, &vr1
);
3056 else if (code
== TRUNC_MOD_EXPR
)
3058 if (range_is_null (&vr1
))
3060 set_value_range_to_undefined (vr
);
3063 /* ABS (A % B) < ABS (B) and either
3064 0 <= A % B <= A or A <= A % B <= 0. */
3066 signop sgn
= TYPE_SIGN (expr_type
);
3067 unsigned int prec
= TYPE_PRECISION (expr_type
);
3068 wide_int wmin
, wmax
, tmp
;
3069 wide_int zero
= wi::zero (prec
);
3070 wide_int one
= wi::one (prec
);
3071 if (vr1
.type
== VR_RANGE
&& !symbolic_range_p (&vr1
))
3073 wmax
= wi::sub (vr1
.max
, one
);
3076 tmp
= wi::sub (wi::minus_one (prec
), vr1
.min
);
3077 wmax
= wi::smax (wmax
, tmp
);
3082 wmax
= wi::max_value (prec
, sgn
);
3083 /* X % INT_MIN may be INT_MAX. */
3084 if (sgn
== UNSIGNED
)
3088 if (sgn
== UNSIGNED
)
3093 if (vr0
.type
== VR_RANGE
&& TREE_CODE (vr0
.min
) == INTEGER_CST
)
3096 if (wi::gts_p (tmp
, zero
))
3098 wmin
= wi::smax (wmin
, tmp
);
3102 if (vr0
.type
== VR_RANGE
&& TREE_CODE (vr0
.max
) == INTEGER_CST
)
3105 if (sgn
== SIGNED
&& wi::neg_p (tmp
))
3107 wmax
= wi::min (wmax
, tmp
, sgn
);
3110 min
= wide_int_to_tree (expr_type
, wmin
);
3111 max
= wide_int_to_tree (expr_type
, wmax
);
3113 else if (code
== BIT_AND_EXPR
|| code
== BIT_IOR_EXPR
|| code
== BIT_XOR_EXPR
)
3115 bool int_cst_range0
, int_cst_range1
;
3116 wide_int may_be_nonzero0
, may_be_nonzero1
;
3117 wide_int must_be_nonzero0
, must_be_nonzero1
;
3119 int_cst_range0
= zero_nonzero_bits_from_vr (expr_type
, &vr0
,
3122 int_cst_range1
= zero_nonzero_bits_from_vr (expr_type
, &vr1
,
3127 if (code
== BIT_AND_EXPR
)
3129 min
= wide_int_to_tree (expr_type
,
3130 must_be_nonzero0
& must_be_nonzero1
);
3131 wide_int wmax
= may_be_nonzero0
& may_be_nonzero1
;
3132 /* If both input ranges contain only negative values we can
3133 truncate the result range maximum to the minimum of the
3134 input range maxima. */
3135 if (int_cst_range0
&& int_cst_range1
3136 && tree_int_cst_sgn (vr0
.max
) < 0
3137 && tree_int_cst_sgn (vr1
.max
) < 0)
3139 wmax
= wi::min (wmax
, vr0
.max
, TYPE_SIGN (expr_type
));
3140 wmax
= wi::min (wmax
, vr1
.max
, TYPE_SIGN (expr_type
));
3142 /* If either input range contains only non-negative values
3143 we can truncate the result range maximum to the respective
3144 maximum of the input range. */
3145 if (int_cst_range0
&& tree_int_cst_sgn (vr0
.min
) >= 0)
3146 wmax
= wi::min (wmax
, vr0
.max
, TYPE_SIGN (expr_type
));
3147 if (int_cst_range1
&& tree_int_cst_sgn (vr1
.min
) >= 0)
3148 wmax
= wi::min (wmax
, vr1
.max
, TYPE_SIGN (expr_type
));
3149 max
= wide_int_to_tree (expr_type
, wmax
);
3151 else if (code
== BIT_IOR_EXPR
)
3153 max
= wide_int_to_tree (expr_type
,
3154 may_be_nonzero0
| may_be_nonzero1
);
3155 wide_int wmin
= must_be_nonzero0
| must_be_nonzero1
;
3156 /* If the input ranges contain only positive values we can
3157 truncate the minimum of the result range to the maximum
3158 of the input range minima. */
3159 if (int_cst_range0
&& int_cst_range1
3160 && tree_int_cst_sgn (vr0
.min
) >= 0
3161 && tree_int_cst_sgn (vr1
.min
) >= 0)
3163 wmin
= wi::max (wmin
, vr0
.min
, TYPE_SIGN (expr_type
));
3164 wmin
= wi::max (wmin
, vr1
.min
, TYPE_SIGN (expr_type
));
3166 /* If either input range contains only negative values
3167 we can truncate the minimum of the result range to the
3168 respective minimum range. */
3169 if (int_cst_range0
&& tree_int_cst_sgn (vr0
.max
) < 0)
3170 wmin
= wi::max (wmin
, vr0
.min
, TYPE_SIGN (expr_type
));
3171 if (int_cst_range1
&& tree_int_cst_sgn (vr1
.max
) < 0)
3172 wmin
= wi::max (wmin
, vr1
.min
, TYPE_SIGN (expr_type
));
3173 min
= wide_int_to_tree (expr_type
, wmin
);
3175 else if (code
== BIT_XOR_EXPR
)
3177 wide_int result_zero_bits
= ((must_be_nonzero0
& must_be_nonzero1
)
3178 | ~(may_be_nonzero0
| may_be_nonzero1
));
3179 wide_int result_one_bits
3180 = (must_be_nonzero0
.and_not (may_be_nonzero1
)
3181 | must_be_nonzero1
.and_not (may_be_nonzero0
));
3182 max
= wide_int_to_tree (expr_type
, ~result_zero_bits
);
3183 min
= wide_int_to_tree (expr_type
, result_one_bits
);
3184 /* If the range has all positive or all negative values the
3185 result is better than VARYING. */
3186 if (tree_int_cst_sgn (min
) < 0
3187 || tree_int_cst_sgn (max
) >= 0)
3190 max
= min
= NULL_TREE
;
3196 /* If either MIN or MAX overflowed, then set the resulting range to
3197 VARYING. But we do accept an overflow infinity representation. */
3198 if (min
== NULL_TREE
3199 || (TREE_OVERFLOW_P (min
) && !is_overflow_infinity (min
))
3201 || (TREE_OVERFLOW_P (max
) && !is_overflow_infinity (max
)))
3203 set_value_range_to_varying (vr
);
3209 2) [-INF, +-INF(OVF)]
3210 3) [+-INF(OVF), +INF]
3211 4) [+-INF(OVF), +-INF(OVF)]
3212 We learn nothing when we have INF and INF(OVF) on both sides.
3213 Note that we do accept [-INF, -INF] and [+INF, +INF] without
3215 if ((vrp_val_is_min (min
) || is_overflow_infinity (min
))
3216 && (vrp_val_is_max (max
) || is_overflow_infinity (max
)))
3218 set_value_range_to_varying (vr
);
3222 cmp
= compare_values (min
, max
);
3223 if (cmp
== -2 || cmp
== 1)
3225 /* If the new range has its limits swapped around (MIN > MAX),
3226 then the operation caused one of them to wrap around, mark
3227 the new range VARYING. */
3228 set_value_range_to_varying (vr
);
3231 set_value_range (vr
, type
, min
, max
, NULL
);
3234 /* Extract range information from a binary expression OP0 CODE OP1 based on
3235 the ranges of each of its operands with resulting type EXPR_TYPE.
3236 The resulting range is stored in *VR. */
3239 extract_range_from_binary_expr (value_range
*vr
,
3240 enum tree_code code
,
3241 tree expr_type
, tree op0
, tree op1
)
3243 value_range vr0
= VR_INITIALIZER
;
3244 value_range vr1
= VR_INITIALIZER
;
3246 /* Get value ranges for each operand. For constant operands, create
3247 a new value range with the operand to simplify processing. */
3248 if (TREE_CODE (op0
) == SSA_NAME
)
3249 vr0
= *(get_value_range (op0
));
3250 else if (is_gimple_min_invariant (op0
))
3251 set_value_range_to_value (&vr0
, op0
, NULL
);
3253 set_value_range_to_varying (&vr0
);
3255 if (TREE_CODE (op1
) == SSA_NAME
)
3256 vr1
= *(get_value_range (op1
));
3257 else if (is_gimple_min_invariant (op1
))
3258 set_value_range_to_value (&vr1
, op1
, NULL
);
3260 set_value_range_to_varying (&vr1
);
3262 extract_range_from_binary_expr_1 (vr
, code
, expr_type
, &vr0
, &vr1
);
3264 /* Try harder for PLUS and MINUS if the range of one operand is symbolic
3265 and based on the other operand, for example if it was deduced from a
3266 symbolic comparison. When a bound of the range of the first operand
3267 is invariant, we set the corresponding bound of the new range to INF
3268 in order to avoid recursing on the range of the second operand. */
3269 if (vr
->type
== VR_VARYING
3270 && (code
== PLUS_EXPR
|| code
== MINUS_EXPR
)
3271 && TREE_CODE (op1
) == SSA_NAME
3272 && vr0
.type
== VR_RANGE
3273 && symbolic_range_based_on_p (&vr0
, op1
))
3275 const bool minus_p
= (code
== MINUS_EXPR
);
3276 value_range n_vr1
= VR_INITIALIZER
;
3278 /* Try with VR0 and [-INF, OP1]. */
3279 if (is_gimple_min_invariant (minus_p
? vr0
.max
: vr0
.min
))
3280 set_value_range (&n_vr1
, VR_RANGE
, vrp_val_min (expr_type
), op1
, NULL
);
3282 /* Try with VR0 and [OP1, +INF]. */
3283 else if (is_gimple_min_invariant (minus_p
? vr0
.min
: vr0
.max
))
3284 set_value_range (&n_vr1
, VR_RANGE
, op1
, vrp_val_max (expr_type
), NULL
);
3286 /* Try with VR0 and [OP1, OP1]. */
3288 set_value_range (&n_vr1
, VR_RANGE
, op1
, op1
, NULL
);
3290 extract_range_from_binary_expr_1 (vr
, code
, expr_type
, &vr0
, &n_vr1
);
3293 if (vr
->type
== VR_VARYING
3294 && (code
== PLUS_EXPR
|| code
== MINUS_EXPR
)
3295 && TREE_CODE (op0
) == SSA_NAME
3296 && vr1
.type
== VR_RANGE
3297 && symbolic_range_based_on_p (&vr1
, op0
))
3299 const bool minus_p
= (code
== MINUS_EXPR
);
3300 value_range n_vr0
= VR_INITIALIZER
;
3302 /* Try with [-INF, OP0] and VR1. */
3303 if (is_gimple_min_invariant (minus_p
? vr1
.max
: vr1
.min
))
3304 set_value_range (&n_vr0
, VR_RANGE
, vrp_val_min (expr_type
), op0
, NULL
);
3306 /* Try with [OP0, +INF] and VR1. */
3307 else if (is_gimple_min_invariant (minus_p
? vr1
.min
: vr1
.max
))
3308 set_value_range (&n_vr0
, VR_RANGE
, op0
, vrp_val_max (expr_type
), NULL
);
3310 /* Try with [OP0, OP0] and VR1. */
3312 set_value_range (&n_vr0
, VR_RANGE
, op0
, op0
, NULL
);
3314 extract_range_from_binary_expr_1 (vr
, code
, expr_type
, &n_vr0
, &vr1
);
3318 /* Extract range information from a unary operation CODE based on
3319 the range of its operand *VR0 with type OP0_TYPE with resulting type TYPE.
3320 The resulting range is stored in *VR. */
3323 extract_range_from_unary_expr_1 (value_range
*vr
,
3324 enum tree_code code
, tree type
,
3325 value_range
*vr0_
, tree op0_type
)
3327 value_range vr0
= *vr0_
, vrtem0
= VR_INITIALIZER
, vrtem1
= VR_INITIALIZER
;
3329 /* VRP only operates on integral and pointer types. */
3330 if (!(INTEGRAL_TYPE_P (op0_type
)
3331 || POINTER_TYPE_P (op0_type
))
3332 || !(INTEGRAL_TYPE_P (type
)
3333 || POINTER_TYPE_P (type
)))
3335 set_value_range_to_varying (vr
);
3339 /* If VR0 is UNDEFINED, so is the result. */
3340 if (vr0
.type
== VR_UNDEFINED
)
3342 set_value_range_to_undefined (vr
);
3346 /* Handle operations that we express in terms of others. */
3347 if (code
== PAREN_EXPR
|| code
== OBJ_TYPE_REF
)
3349 /* PAREN_EXPR and OBJ_TYPE_REF are simple copies. */
3350 copy_value_range (vr
, &vr0
);
3353 else if (code
== NEGATE_EXPR
)
3355 /* -X is simply 0 - X, so re-use existing code that also handles
3356 anti-ranges fine. */
3357 value_range zero
= VR_INITIALIZER
;
3358 set_value_range_to_value (&zero
, build_int_cst (type
, 0), NULL
);
3359 extract_range_from_binary_expr_1 (vr
, MINUS_EXPR
, type
, &zero
, &vr0
);
3362 else if (code
== BIT_NOT_EXPR
)
3364 /* ~X is simply -1 - X, so re-use existing code that also handles
3365 anti-ranges fine. */
3366 value_range minusone
= VR_INITIALIZER
;
3367 set_value_range_to_value (&minusone
, build_int_cst (type
, -1), NULL
);
3368 extract_range_from_binary_expr_1 (vr
, MINUS_EXPR
,
3369 type
, &minusone
, &vr0
);
3373 /* Now canonicalize anti-ranges to ranges when they are not symbolic
3374 and express op ~[] as (op []') U (op []''). */
3375 if (vr0
.type
== VR_ANTI_RANGE
3376 && ranges_from_anti_range (&vr0
, &vrtem0
, &vrtem1
))
3378 extract_range_from_unary_expr_1 (vr
, code
, type
, &vrtem0
, op0_type
);
3379 if (vrtem1
.type
!= VR_UNDEFINED
)
3381 value_range vrres
= VR_INITIALIZER
;
3382 extract_range_from_unary_expr_1 (&vrres
, code
, type
,
3384 vrp_meet (vr
, &vrres
);
3389 if (CONVERT_EXPR_CODE_P (code
))
3391 tree inner_type
= op0_type
;
3392 tree outer_type
= type
;
3394 /* If the expression evaluates to a pointer, we are only interested in
3395 determining if it evaluates to NULL [0, 0] or non-NULL (~[0, 0]). */
3396 if (POINTER_TYPE_P (type
))
3398 if (range_is_nonnull (&vr0
))
3399 set_value_range_to_nonnull (vr
, type
);
3400 else if (range_is_null (&vr0
))
3401 set_value_range_to_null (vr
, type
);
3403 set_value_range_to_varying (vr
);
3407 /* If VR0 is varying and we increase the type precision, assume
3408 a full range for the following transformation. */
3409 if (vr0
.type
== VR_VARYING
3410 && INTEGRAL_TYPE_P (inner_type
)
3411 && TYPE_PRECISION (inner_type
) < TYPE_PRECISION (outer_type
))
3413 vr0
.type
= VR_RANGE
;
3414 vr0
.min
= TYPE_MIN_VALUE (inner_type
);
3415 vr0
.max
= TYPE_MAX_VALUE (inner_type
);
3418 /* If VR0 is a constant range or anti-range and the conversion is
3419 not truncating we can convert the min and max values and
3420 canonicalize the resulting range. Otherwise we can do the
3421 conversion if the size of the range is less than what the
3422 precision of the target type can represent and the range is
3423 not an anti-range. */
3424 if ((vr0
.type
== VR_RANGE
3425 || vr0
.type
== VR_ANTI_RANGE
)
3426 && TREE_CODE (vr0
.min
) == INTEGER_CST
3427 && TREE_CODE (vr0
.max
) == INTEGER_CST
3428 && (!is_overflow_infinity (vr0
.min
)
3429 || (vr0
.type
== VR_RANGE
3430 && TYPE_PRECISION (outer_type
) > TYPE_PRECISION (inner_type
)
3431 && needs_overflow_infinity (outer_type
)
3432 && supports_overflow_infinity (outer_type
)))
3433 && (!is_overflow_infinity (vr0
.max
)
3434 || (vr0
.type
== VR_RANGE
3435 && TYPE_PRECISION (outer_type
) > TYPE_PRECISION (inner_type
)
3436 && needs_overflow_infinity (outer_type
)
3437 && supports_overflow_infinity (outer_type
)))
3438 && (TYPE_PRECISION (outer_type
) >= TYPE_PRECISION (inner_type
)
3439 || (vr0
.type
== VR_RANGE
3440 && integer_zerop (int_const_binop (RSHIFT_EXPR
,
3441 int_const_binop (MINUS_EXPR
, vr0
.max
, vr0
.min
),
3442 size_int (TYPE_PRECISION (outer_type
)))))))
3444 tree new_min
, new_max
;
3445 if (is_overflow_infinity (vr0
.min
))
3446 new_min
= negative_overflow_infinity (outer_type
);
3448 new_min
= force_fit_type (outer_type
, wi::to_widest (vr0
.min
),
3450 if (is_overflow_infinity (vr0
.max
))
3451 new_max
= positive_overflow_infinity (outer_type
);
3453 new_max
= force_fit_type (outer_type
, wi::to_widest (vr0
.max
),
3455 set_and_canonicalize_value_range (vr
, vr0
.type
,
3456 new_min
, new_max
, NULL
);
3460 set_value_range_to_varying (vr
);
3463 else if (code
== ABS_EXPR
)
3468 /* Pass through vr0 in the easy cases. */
3469 if (TYPE_UNSIGNED (type
)
3470 || value_range_nonnegative_p (&vr0
))
3472 copy_value_range (vr
, &vr0
);
3476 /* For the remaining varying or symbolic ranges we can't do anything
3478 if (vr0
.type
== VR_VARYING
3479 || symbolic_range_p (&vr0
))
3481 set_value_range_to_varying (vr
);
3485 /* -TYPE_MIN_VALUE = TYPE_MIN_VALUE with flag_wrapv so we can't get a
3487 if (!TYPE_OVERFLOW_UNDEFINED (type
)
3488 && ((vr0
.type
== VR_RANGE
3489 && vrp_val_is_min (vr0
.min
))
3490 || (vr0
.type
== VR_ANTI_RANGE
3491 && !vrp_val_is_min (vr0
.min
))))
3493 set_value_range_to_varying (vr
);
3497 /* ABS_EXPR may flip the range around, if the original range
3498 included negative values. */
3499 if (is_overflow_infinity (vr0
.min
))
3500 min
= positive_overflow_infinity (type
);
3501 else if (!vrp_val_is_min (vr0
.min
))
3502 min
= fold_unary_to_constant (code
, type
, vr0
.min
);
3503 else if (!needs_overflow_infinity (type
))
3504 min
= TYPE_MAX_VALUE (type
);
3505 else if (supports_overflow_infinity (type
))
3506 min
= positive_overflow_infinity (type
);
3509 set_value_range_to_varying (vr
);
3513 if (is_overflow_infinity (vr0
.max
))
3514 max
= positive_overflow_infinity (type
);
3515 else if (!vrp_val_is_min (vr0
.max
))
3516 max
= fold_unary_to_constant (code
, type
, vr0
.max
);
3517 else if (!needs_overflow_infinity (type
))
3518 max
= TYPE_MAX_VALUE (type
);
3519 else if (supports_overflow_infinity (type
)
3520 /* We shouldn't generate [+INF, +INF] as set_value_range
3521 doesn't like this and ICEs. */
3522 && !is_positive_overflow_infinity (min
))
3523 max
= positive_overflow_infinity (type
);
3526 set_value_range_to_varying (vr
);
3530 cmp
= compare_values (min
, max
);
3532 /* If a VR_ANTI_RANGEs contains zero, then we have
3533 ~[-INF, min(MIN, MAX)]. */
3534 if (vr0
.type
== VR_ANTI_RANGE
)
3536 if (range_includes_zero_p (vr0
.min
, vr0
.max
) == 1)
3538 /* Take the lower of the two values. */
3542 /* Create ~[-INF, min (abs(MIN), abs(MAX))]
3543 or ~[-INF + 1, min (abs(MIN), abs(MAX))] when
3544 flag_wrapv is set and the original anti-range doesn't include
3545 TYPE_MIN_VALUE, remember -TYPE_MIN_VALUE = TYPE_MIN_VALUE. */
3546 if (TYPE_OVERFLOW_WRAPS (type
))
3548 tree type_min_value
= TYPE_MIN_VALUE (type
);
3550 min
= (vr0
.min
!= type_min_value
3551 ? int_const_binop (PLUS_EXPR
, type_min_value
,
3552 build_int_cst (TREE_TYPE (type_min_value
), 1))
3557 if (overflow_infinity_range_p (&vr0
))
3558 min
= negative_overflow_infinity (type
);
3560 min
= TYPE_MIN_VALUE (type
);
3565 /* All else has failed, so create the range [0, INF], even for
3566 flag_wrapv since TYPE_MIN_VALUE is in the original
3568 vr0
.type
= VR_RANGE
;
3569 min
= build_int_cst (type
, 0);
3570 if (needs_overflow_infinity (type
))
3572 if (supports_overflow_infinity (type
))
3573 max
= positive_overflow_infinity (type
);
3576 set_value_range_to_varying (vr
);
3581 max
= TYPE_MAX_VALUE (type
);
3585 /* If the range contains zero then we know that the minimum value in the
3586 range will be zero. */
3587 else if (range_includes_zero_p (vr0
.min
, vr0
.max
) == 1)
3591 min
= build_int_cst (type
, 0);
3595 /* If the range was reversed, swap MIN and MAX. */
3597 std::swap (min
, max
);
3600 cmp
= compare_values (min
, max
);
3601 if (cmp
== -2 || cmp
== 1)
3603 /* If the new range has its limits swapped around (MIN > MAX),
3604 then the operation caused one of them to wrap around, mark
3605 the new range VARYING. */
3606 set_value_range_to_varying (vr
);
3609 set_value_range (vr
, vr0
.type
, min
, max
, NULL
);
3613 /* For unhandled operations fall back to varying. */
3614 set_value_range_to_varying (vr
);
3619 /* Extract range information from a unary expression CODE OP0 based on
3620 the range of its operand with resulting type TYPE.
3621 The resulting range is stored in *VR. */
3624 extract_range_from_unary_expr (value_range
*vr
, enum tree_code code
,
3625 tree type
, tree op0
)
3627 value_range vr0
= VR_INITIALIZER
;
3629 /* Get value ranges for the operand. For constant operands, create
3630 a new value range with the operand to simplify processing. */
3631 if (TREE_CODE (op0
) == SSA_NAME
)
3632 vr0
= *(get_value_range (op0
));
3633 else if (is_gimple_min_invariant (op0
))
3634 set_value_range_to_value (&vr0
, op0
, NULL
);
3636 set_value_range_to_varying (&vr0
);
3638 extract_range_from_unary_expr_1 (vr
, code
, type
, &vr0
, TREE_TYPE (op0
));
3642 /* Extract range information from a conditional expression STMT based on
3643 the ranges of each of its operands and the expression code. */
3646 extract_range_from_cond_expr (value_range
*vr
, gassign
*stmt
)
3649 value_range vr0
= VR_INITIALIZER
;
3650 value_range vr1
= VR_INITIALIZER
;
3652 /* Get value ranges for each operand. For constant operands, create
3653 a new value range with the operand to simplify processing. */
3654 op0
= gimple_assign_rhs2 (stmt
);
3655 if (TREE_CODE (op0
) == SSA_NAME
)
3656 vr0
= *(get_value_range (op0
));
3657 else if (is_gimple_min_invariant (op0
))
3658 set_value_range_to_value (&vr0
, op0
, NULL
);
3660 set_value_range_to_varying (&vr0
);
3662 op1
= gimple_assign_rhs3 (stmt
);
3663 if (TREE_CODE (op1
) == SSA_NAME
)
3664 vr1
= *(get_value_range (op1
));
3665 else if (is_gimple_min_invariant (op1
))
3666 set_value_range_to_value (&vr1
, op1
, NULL
);
3668 set_value_range_to_varying (&vr1
);
3670 /* The resulting value range is the union of the operand ranges */
3671 copy_value_range (vr
, &vr0
);
3672 vrp_meet (vr
, &vr1
);
3676 /* Extract range information from a comparison expression EXPR based
3677 on the range of its operand and the expression code. */
3680 extract_range_from_comparison (value_range
*vr
, enum tree_code code
,
3681 tree type
, tree op0
, tree op1
)
3686 val
= vrp_evaluate_conditional_warnv_with_ops (code
, op0
, op1
, false, &sop
,
3689 /* A disadvantage of using a special infinity as an overflow
3690 representation is that we lose the ability to record overflow
3691 when we don't have an infinity. So we have to ignore a result
3692 which relies on overflow. */
3694 if (val
&& !is_overflow_infinity (val
) && !sop
)
3696 /* Since this expression was found on the RHS of an assignment,
3697 its type may be different from _Bool. Convert VAL to EXPR's
3699 val
= fold_convert (type
, val
);
3700 if (is_gimple_min_invariant (val
))
3701 set_value_range_to_value (vr
, val
, vr
->equiv
);
3703 set_value_range (vr
, VR_RANGE
, val
, val
, vr
->equiv
);
3706 /* The result of a comparison is always true or false. */
3707 set_value_range_to_truthvalue (vr
, type
);
3710 /* Helper function for simplify_internal_call_using_ranges and
3711 extract_range_basic. Return true if OP0 SUBCODE OP1 for
3712 SUBCODE {PLUS,MINUS,MULT}_EXPR is known to never overflow or
3713 always overflow. Set *OVF to true if it is known to always
3717 check_for_binary_op_overflow (enum tree_code subcode
, tree type
,
3718 tree op0
, tree op1
, bool *ovf
)
3720 value_range vr0
= VR_INITIALIZER
;
3721 value_range vr1
= VR_INITIALIZER
;
3722 if (TREE_CODE (op0
) == SSA_NAME
)
3723 vr0
= *get_value_range (op0
);
3724 else if (TREE_CODE (op0
) == INTEGER_CST
)
3725 set_value_range_to_value (&vr0
, op0
, NULL
);
3727 set_value_range_to_varying (&vr0
);
3729 if (TREE_CODE (op1
) == SSA_NAME
)
3730 vr1
= *get_value_range (op1
);
3731 else if (TREE_CODE (op1
) == INTEGER_CST
)
3732 set_value_range_to_value (&vr1
, op1
, NULL
);
3734 set_value_range_to_varying (&vr1
);
3736 if (!range_int_cst_p (&vr0
)
3737 || TREE_OVERFLOW (vr0
.min
)
3738 || TREE_OVERFLOW (vr0
.max
))
3740 vr0
.min
= vrp_val_min (TREE_TYPE (op0
));
3741 vr0
.max
= vrp_val_max (TREE_TYPE (op0
));
3743 if (!range_int_cst_p (&vr1
)
3744 || TREE_OVERFLOW (vr1
.min
)
3745 || TREE_OVERFLOW (vr1
.max
))
3747 vr1
.min
= vrp_val_min (TREE_TYPE (op1
));
3748 vr1
.max
= vrp_val_max (TREE_TYPE (op1
));
3750 *ovf
= arith_overflowed_p (subcode
, type
, vr0
.min
,
3751 subcode
== MINUS_EXPR
? vr1
.max
: vr1
.min
);
3752 if (arith_overflowed_p (subcode
, type
, vr0
.max
,
3753 subcode
== MINUS_EXPR
? vr1
.min
: vr1
.max
) != *ovf
)
3755 if (subcode
== MULT_EXPR
)
3757 if (arith_overflowed_p (subcode
, type
, vr0
.min
, vr1
.max
) != *ovf
3758 || arith_overflowed_p (subcode
, type
, vr0
.max
, vr1
.min
) != *ovf
)
3763 /* So far we found that there is an overflow on the boundaries.
3764 That doesn't prove that there is an overflow even for all values
3765 in between the boundaries. For that compute widest_int range
3766 of the result and see if it doesn't overlap the range of
3768 widest_int wmin
, wmax
;
3771 w
[0] = wi::to_widest (vr0
.min
);
3772 w
[1] = wi::to_widest (vr0
.max
);
3773 w
[2] = wi::to_widest (vr1
.min
);
3774 w
[3] = wi::to_widest (vr1
.max
);
3775 for (i
= 0; i
< 4; i
++)
3781 wt
= wi::add (w
[i
& 1], w
[2 + (i
& 2) / 2]);
3784 wt
= wi::sub (w
[i
& 1], w
[2 + (i
& 2) / 2]);
3787 wt
= wi::mul (w
[i
& 1], w
[2 + (i
& 2) / 2]);
3799 wmin
= wi::smin (wmin
, wt
);
3800 wmax
= wi::smax (wmax
, wt
);
3803 /* The result of op0 CODE op1 is known to be in range
3805 widest_int wtmin
= wi::to_widest (vrp_val_min (type
));
3806 widest_int wtmax
= wi::to_widest (vrp_val_max (type
));
3807 /* If all values in [wmin, wmax] are smaller than
3808 [wtmin, wtmax] or all are larger than [wtmin, wtmax],
3809 the arithmetic operation will always overflow. */
3810 if (wi::lts_p (wmax
, wtmin
) || wi::gts_p (wmin
, wtmax
))
3817 /* Try to derive a nonnegative or nonzero range out of STMT relying
3818 primarily on generic routines in fold in conjunction with range data.
3819 Store the result in *VR */
3822 extract_range_basic (value_range
*vr
, gimple
*stmt
)
3825 tree type
= gimple_expr_type (stmt
);
3827 if (is_gimple_call (stmt
))
3830 int mini
, maxi
, zerov
= 0, prec
;
3831 enum tree_code subcode
= ERROR_MARK
;
3832 combined_fn cfn
= gimple_call_combined_fn (stmt
);
3836 case CFN_BUILT_IN_CONSTANT_P
:
3837 /* If the call is __builtin_constant_p and the argument is a
3838 function parameter resolve it to false. This avoids bogus
3839 array bound warnings.
3840 ??? We could do this as early as inlining is finished. */
3841 arg
= gimple_call_arg (stmt
, 0);
3842 if (TREE_CODE (arg
) == SSA_NAME
3843 && SSA_NAME_IS_DEFAULT_DEF (arg
)
3844 && TREE_CODE (SSA_NAME_VAR (arg
)) == PARM_DECL
)
3846 set_value_range_to_null (vr
, type
);
3850 /* Both __builtin_ffs* and __builtin_popcount return
3854 arg
= gimple_call_arg (stmt
, 0);
3855 prec
= TYPE_PRECISION (TREE_TYPE (arg
));
3858 if (TREE_CODE (arg
) == SSA_NAME
)
3860 value_range
*vr0
= get_value_range (arg
);
3861 /* If arg is non-zero, then ffs or popcount
3863 if (((vr0
->type
== VR_RANGE
3864 && range_includes_zero_p (vr0
->min
, vr0
->max
) == 0)
3865 || (vr0
->type
== VR_ANTI_RANGE
3866 && range_includes_zero_p (vr0
->min
, vr0
->max
) == 1))
3867 && !is_overflow_infinity (vr0
->min
)
3868 && !is_overflow_infinity (vr0
->max
))
3870 /* If some high bits are known to be zero,
3871 we can decrease the maximum. */
3872 if (vr0
->type
== VR_RANGE
3873 && TREE_CODE (vr0
->max
) == INTEGER_CST
3874 && !operand_less_p (vr0
->min
,
3875 build_zero_cst (TREE_TYPE (vr0
->min
)))
3876 && !is_overflow_infinity (vr0
->max
))
3877 maxi
= tree_floor_log2 (vr0
->max
) + 1;
3880 /* __builtin_parity* returns [0, 1]. */
3885 /* __builtin_c[lt]z* return [0, prec-1], except for
3886 when the argument is 0, but that is undefined behavior.
3887 On many targets where the CLZ RTL or optab value is defined
3888 for 0 the value is prec, so include that in the range
3891 arg
= gimple_call_arg (stmt
, 0);
3892 prec
= TYPE_PRECISION (TREE_TYPE (arg
));
3895 if (optab_handler (clz_optab
, TYPE_MODE (TREE_TYPE (arg
)))
3897 && CLZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (TREE_TYPE (arg
)),
3899 /* Handle only the single common value. */
3901 /* Magic value to give up, unless vr0 proves
3904 if (TREE_CODE (arg
) == SSA_NAME
)
3906 value_range
*vr0
= get_value_range (arg
);
3907 /* From clz of VR_RANGE minimum we can compute
3909 if (vr0
->type
== VR_RANGE
3910 && TREE_CODE (vr0
->min
) == INTEGER_CST
3911 && !is_overflow_infinity (vr0
->min
))
3913 maxi
= prec
- 1 - tree_floor_log2 (vr0
->min
);
3917 else if (vr0
->type
== VR_ANTI_RANGE
3918 && integer_zerop (vr0
->min
)
3919 && !is_overflow_infinity (vr0
->min
))
3926 /* From clz of VR_RANGE maximum we can compute
3928 if (vr0
->type
== VR_RANGE
3929 && TREE_CODE (vr0
->max
) == INTEGER_CST
3930 && !is_overflow_infinity (vr0
->max
))
3932 mini
= prec
- 1 - tree_floor_log2 (vr0
->max
);
3940 /* __builtin_ctz* return [0, prec-1], except for
3941 when the argument is 0, but that is undefined behavior.
3942 If there is a ctz optab for this mode and
3943 CTZ_DEFINED_VALUE_AT_ZERO, include that in the range,
3944 otherwise just assume 0 won't be seen. */
3946 arg
= gimple_call_arg (stmt
, 0);
3947 prec
= TYPE_PRECISION (TREE_TYPE (arg
));
3950 if (optab_handler (ctz_optab
, TYPE_MODE (TREE_TYPE (arg
)))
3952 && CTZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (TREE_TYPE (arg
)),
3955 /* Handle only the two common values. */
3958 else if (zerov
== prec
)
3961 /* Magic value to give up, unless vr0 proves
3965 if (TREE_CODE (arg
) == SSA_NAME
)
3967 value_range
*vr0
= get_value_range (arg
);
3968 /* If arg is non-zero, then use [0, prec - 1]. */
3969 if (((vr0
->type
== VR_RANGE
3970 && integer_nonzerop (vr0
->min
))
3971 || (vr0
->type
== VR_ANTI_RANGE
3972 && integer_zerop (vr0
->min
)))
3973 && !is_overflow_infinity (vr0
->min
))
3978 /* If some high bits are known to be zero,
3979 we can decrease the result maximum. */
3980 if (vr0
->type
== VR_RANGE
3981 && TREE_CODE (vr0
->max
) == INTEGER_CST
3982 && !is_overflow_infinity (vr0
->max
))
3984 maxi
= tree_floor_log2 (vr0
->max
);
3985 /* For vr0 [0, 0] give up. */
3993 /* __builtin_clrsb* returns [0, prec-1]. */
3995 arg
= gimple_call_arg (stmt
, 0);
3996 prec
= TYPE_PRECISION (TREE_TYPE (arg
));
4001 set_value_range (vr
, VR_RANGE
, build_int_cst (type
, mini
),
4002 build_int_cst (type
, maxi
), NULL
);
4004 case CFN_UBSAN_CHECK_ADD
:
4005 subcode
= PLUS_EXPR
;
4007 case CFN_UBSAN_CHECK_SUB
:
4008 subcode
= MINUS_EXPR
;
4010 case CFN_UBSAN_CHECK_MUL
:
4011 subcode
= MULT_EXPR
;
4013 case CFN_GOACC_DIM_SIZE
:
4014 case CFN_GOACC_DIM_POS
:
4015 /* Optimizing these two internal functions helps the loop
4016 optimizer eliminate outer comparisons. Size is [1,N]
4017 and pos is [0,N-1]. */
4019 bool is_pos
= cfn
== CFN_GOACC_DIM_POS
;
4020 int axis
= get_oacc_ifn_dim_arg (stmt
);
4021 int size
= get_oacc_fn_dim_size (current_function_decl
, axis
);
4024 /* If it's dynamic, the backend might know a hardware
4026 size
= targetm
.goacc
.dim_limit (axis
);
4028 tree type
= TREE_TYPE (gimple_call_lhs (stmt
));
4029 set_value_range (vr
, VR_RANGE
,
4030 build_int_cst (type
, is_pos
? 0 : 1),
4031 size
? build_int_cst (type
, size
- is_pos
)
4032 : vrp_val_max (type
), NULL
);
4038 if (subcode
!= ERROR_MARK
)
4040 bool saved_flag_wrapv
= flag_wrapv
;
4041 /* Pretend the arithmetics is wrapping. If there is
4042 any overflow, we'll complain, but will actually do
4043 wrapping operation. */
4045 extract_range_from_binary_expr (vr
, subcode
, type
,
4046 gimple_call_arg (stmt
, 0),
4047 gimple_call_arg (stmt
, 1));
4048 flag_wrapv
= saved_flag_wrapv
;
4050 /* If for both arguments vrp_valueize returned non-NULL,
4051 this should have been already folded and if not, it
4052 wasn't folded because of overflow. Avoid removing the
4053 UBSAN_CHECK_* calls in that case. */
4054 if (vr
->type
== VR_RANGE
4055 && (vr
->min
== vr
->max
4056 || operand_equal_p (vr
->min
, vr
->max
, 0)))
4057 set_value_range_to_varying (vr
);
4061 /* Handle extraction of the two results (result of arithmetics and
4062 a flag whether arithmetics overflowed) from {ADD,SUB,MUL}_OVERFLOW
4063 internal function. */
4064 else if (is_gimple_assign (stmt
)
4065 && (gimple_assign_rhs_code (stmt
) == REALPART_EXPR
4066 || gimple_assign_rhs_code (stmt
) == IMAGPART_EXPR
)
4067 && INTEGRAL_TYPE_P (type
))
4069 enum tree_code code
= gimple_assign_rhs_code (stmt
);
4070 tree op
= gimple_assign_rhs1 (stmt
);
4071 if (TREE_CODE (op
) == code
&& TREE_CODE (TREE_OPERAND (op
, 0)) == SSA_NAME
)
4073 gimple
*g
= SSA_NAME_DEF_STMT (TREE_OPERAND (op
, 0));
4074 if (is_gimple_call (g
) && gimple_call_internal_p (g
))
4076 enum tree_code subcode
= ERROR_MARK
;
4077 switch (gimple_call_internal_fn (g
))
4079 case IFN_ADD_OVERFLOW
:
4080 subcode
= PLUS_EXPR
;
4082 case IFN_SUB_OVERFLOW
:
4083 subcode
= MINUS_EXPR
;
4085 case IFN_MUL_OVERFLOW
:
4086 subcode
= MULT_EXPR
;
4091 if (subcode
!= ERROR_MARK
)
4093 tree op0
= gimple_call_arg (g
, 0);
4094 tree op1
= gimple_call_arg (g
, 1);
4095 if (code
== IMAGPART_EXPR
)
4098 if (check_for_binary_op_overflow (subcode
, type
,
4100 set_value_range_to_value (vr
,
4101 build_int_cst (type
, ovf
),
4104 set_value_range (vr
, VR_RANGE
, build_int_cst (type
, 0),
4105 build_int_cst (type
, 1), NULL
);
4107 else if (types_compatible_p (type
, TREE_TYPE (op0
))
4108 && types_compatible_p (type
, TREE_TYPE (op1
)))
4110 bool saved_flag_wrapv
= flag_wrapv
;
4111 /* Pretend the arithmetics is wrapping. If there is
4112 any overflow, IMAGPART_EXPR will be set. */
4114 extract_range_from_binary_expr (vr
, subcode
, type
,
4116 flag_wrapv
= saved_flag_wrapv
;
4120 value_range vr0
= VR_INITIALIZER
;
4121 value_range vr1
= VR_INITIALIZER
;
4122 bool saved_flag_wrapv
= flag_wrapv
;
4123 /* Pretend the arithmetics is wrapping. If there is
4124 any overflow, IMAGPART_EXPR will be set. */
4126 extract_range_from_unary_expr (&vr0
, NOP_EXPR
,
4128 extract_range_from_unary_expr (&vr1
, NOP_EXPR
,
4130 extract_range_from_binary_expr_1 (vr
, subcode
, type
,
4132 flag_wrapv
= saved_flag_wrapv
;
4139 if (INTEGRAL_TYPE_P (type
)
4140 && gimple_stmt_nonnegative_warnv_p (stmt
, &sop
))
4141 set_value_range_to_nonnegative (vr
, type
,
4142 sop
|| stmt_overflow_infinity (stmt
));
4143 else if (vrp_stmt_computes_nonzero (stmt
, &sop
)
4145 set_value_range_to_nonnull (vr
, type
);
4147 set_value_range_to_varying (vr
);
4151 /* Try to compute a useful range out of assignment STMT and store it
4155 extract_range_from_assignment (value_range
*vr
, gassign
*stmt
)
4157 enum tree_code code
= gimple_assign_rhs_code (stmt
);
4159 if (code
== ASSERT_EXPR
)
4160 extract_range_from_assert (vr
, gimple_assign_rhs1 (stmt
));
4161 else if (code
== SSA_NAME
)
4162 extract_range_from_ssa_name (vr
, gimple_assign_rhs1 (stmt
));
4163 else if (TREE_CODE_CLASS (code
) == tcc_binary
)
4164 extract_range_from_binary_expr (vr
, gimple_assign_rhs_code (stmt
),
4165 gimple_expr_type (stmt
),
4166 gimple_assign_rhs1 (stmt
),
4167 gimple_assign_rhs2 (stmt
));
4168 else if (TREE_CODE_CLASS (code
) == tcc_unary
)
4169 extract_range_from_unary_expr (vr
, gimple_assign_rhs_code (stmt
),
4170 gimple_expr_type (stmt
),
4171 gimple_assign_rhs1 (stmt
));
4172 else if (code
== COND_EXPR
)
4173 extract_range_from_cond_expr (vr
, stmt
);
4174 else if (TREE_CODE_CLASS (code
) == tcc_comparison
)
4175 extract_range_from_comparison (vr
, gimple_assign_rhs_code (stmt
),
4176 gimple_expr_type (stmt
),
4177 gimple_assign_rhs1 (stmt
),
4178 gimple_assign_rhs2 (stmt
));
4179 else if (get_gimple_rhs_class (code
) == GIMPLE_SINGLE_RHS
4180 && is_gimple_min_invariant (gimple_assign_rhs1 (stmt
)))
4181 set_value_range_to_value (vr
, gimple_assign_rhs1 (stmt
), NULL
);
4183 set_value_range_to_varying (vr
);
4185 if (vr
->type
== VR_VARYING
)
4186 extract_range_basic (vr
, stmt
);
4189 /* Given a range VR, a LOOP and a variable VAR, determine whether it
4190 would be profitable to adjust VR using scalar evolution information
4191 for VAR. If so, update VR with the new limits. */
4194 adjust_range_with_scev (value_range
*vr
, struct loop
*loop
,
4195 gimple
*stmt
, tree var
)
4197 tree init
, step
, chrec
, tmin
, tmax
, min
, max
, type
, tem
;
4198 enum ev_direction dir
;
4200 /* TODO. Don't adjust anti-ranges. An anti-range may provide
4201 better opportunities than a regular range, but I'm not sure. */
4202 if (vr
->type
== VR_ANTI_RANGE
)
4205 chrec
= instantiate_parameters (loop
, analyze_scalar_evolution (loop
, var
));
4207 /* Like in PR19590, scev can return a constant function. */
4208 if (is_gimple_min_invariant (chrec
))
4210 set_value_range_to_value (vr
, chrec
, vr
->equiv
);
4214 if (TREE_CODE (chrec
) != POLYNOMIAL_CHREC
)
4217 init
= initial_condition_in_loop_num (chrec
, loop
->num
);
4218 tem
= op_with_constant_singleton_value_range (init
);
4221 step
= evolution_part_in_loop_num (chrec
, loop
->num
);
4222 tem
= op_with_constant_singleton_value_range (step
);
4226 /* If STEP is symbolic, we can't know whether INIT will be the
4227 minimum or maximum value in the range. Also, unless INIT is
4228 a simple expression, compare_values and possibly other functions
4229 in tree-vrp won't be able to handle it. */
4230 if (step
== NULL_TREE
4231 || !is_gimple_min_invariant (step
)
4232 || !valid_value_p (init
))
4235 dir
= scev_direction (chrec
);
4236 if (/* Do not adjust ranges if we do not know whether the iv increases
4237 or decreases, ... */
4238 dir
== EV_DIR_UNKNOWN
4239 /* ... or if it may wrap. */
4240 || scev_probably_wraps_p (init
, step
, stmt
, get_chrec_loop (chrec
),
4244 /* We use TYPE_MIN_VALUE and TYPE_MAX_VALUE here instead of
4245 negative_overflow_infinity and positive_overflow_infinity,
4246 because we have concluded that the loop probably does not
4249 type
= TREE_TYPE (var
);
4250 if (POINTER_TYPE_P (type
) || !TYPE_MIN_VALUE (type
))
4251 tmin
= lower_bound_in_type (type
, type
);
4253 tmin
= TYPE_MIN_VALUE (type
);
4254 if (POINTER_TYPE_P (type
) || !TYPE_MAX_VALUE (type
))
4255 tmax
= upper_bound_in_type (type
, type
);
4257 tmax
= TYPE_MAX_VALUE (type
);
4259 /* Try to use estimated number of iterations for the loop to constrain the
4260 final value in the evolution. */
4261 if (TREE_CODE (step
) == INTEGER_CST
4262 && is_gimple_val (init
)
4263 && (TREE_CODE (init
) != SSA_NAME
4264 || get_value_range (init
)->type
== VR_RANGE
))
4268 /* We are only entering here for loop header PHI nodes, so using
4269 the number of latch executions is the correct thing to use. */
4270 if (max_loop_iterations (loop
, &nit
))
4272 value_range maxvr
= VR_INITIALIZER
;
4273 signop sgn
= TYPE_SIGN (TREE_TYPE (step
));
4276 widest_int wtmp
= wi::mul (wi::to_widest (step
), nit
, sgn
,
4278 /* If the multiplication overflowed we can't do a meaningful
4279 adjustment. Likewise if the result doesn't fit in the type
4280 of the induction variable. For a signed type we have to
4281 check whether the result has the expected signedness which
4282 is that of the step as number of iterations is unsigned. */
4284 && wi::fits_to_tree_p (wtmp
, TREE_TYPE (init
))
4286 || wi::gts_p (wtmp
, 0) == wi::gts_p (step
, 0)))
4288 tem
= wide_int_to_tree (TREE_TYPE (init
), wtmp
);
4289 extract_range_from_binary_expr (&maxvr
, PLUS_EXPR
,
4290 TREE_TYPE (init
), init
, tem
);
4291 /* Likewise if the addition did. */
4292 if (maxvr
.type
== VR_RANGE
)
4294 value_range initvr
= VR_INITIALIZER
;
4296 if (TREE_CODE (init
) == SSA_NAME
)
4297 initvr
= *(get_value_range (init
));
4298 else if (is_gimple_min_invariant (init
))
4299 set_value_range_to_value (&initvr
, init
, NULL
);
4303 /* Check if init + nit * step overflows. Though we checked
4304 scev {init, step}_loop doesn't wrap, it is not enough
4305 because the loop may exit immediately. Overflow could
4306 happen in the plus expression in this case. */
4307 if ((dir
== EV_DIR_DECREASES
4308 && (is_negative_overflow_infinity (maxvr
.min
)
4309 || compare_values (maxvr
.min
, initvr
.min
) != -1))
4310 || (dir
== EV_DIR_GROWS
4311 && (is_positive_overflow_infinity (maxvr
.max
)
4312 || compare_values (maxvr
.max
, initvr
.max
) != 1)))
4322 if (vr
->type
== VR_VARYING
|| vr
->type
== VR_UNDEFINED
)
4327 /* For VARYING or UNDEFINED ranges, just about anything we get
4328 from scalar evolutions should be better. */
4330 if (dir
== EV_DIR_DECREASES
)
4335 else if (vr
->type
== VR_RANGE
)
4340 if (dir
== EV_DIR_DECREASES
)
4342 /* INIT is the maximum value. If INIT is lower than VR->MAX
4343 but no smaller than VR->MIN, set VR->MAX to INIT. */
4344 if (compare_values (init
, max
) == -1)
4347 /* According to the loop information, the variable does not
4348 overflow. If we think it does, probably because of an
4349 overflow due to arithmetic on a different INF value,
4351 if (is_negative_overflow_infinity (min
)
4352 || compare_values (min
, tmin
) == -1)
4358 /* If INIT is bigger than VR->MIN, set VR->MIN to INIT. */
4359 if (compare_values (init
, min
) == 1)
4362 if (is_positive_overflow_infinity (max
)
4363 || compare_values (tmax
, max
) == -1)
4370 /* If we just created an invalid range with the minimum
4371 greater than the maximum, we fail conservatively.
4372 This should happen only in unreachable
4373 parts of code, or for invalid programs. */
4374 if (compare_values (min
, max
) == 1
4375 || (is_negative_overflow_infinity (min
)
4376 && is_positive_overflow_infinity (max
)))
4379 /* Even for valid range info, sometimes overflow flag will leak in.
4380 As GIMPLE IL should have no constants with TREE_OVERFLOW set, we
4381 drop them except for +-overflow_infinity which still need special
4382 handling in vrp pass. */
4383 if (TREE_OVERFLOW_P (min
)
4384 && ! is_negative_overflow_infinity (min
))
4385 min
= drop_tree_overflow (min
);
4386 if (TREE_OVERFLOW_P (max
)
4387 && ! is_positive_overflow_infinity (max
))
4388 max
= drop_tree_overflow (max
);
4390 set_value_range (vr
, VR_RANGE
, min
, max
, vr
->equiv
);
4394 /* Given two numeric value ranges VR0, VR1 and a comparison code COMP:
4396 - Return BOOLEAN_TRUE_NODE if VR0 COMP VR1 always returns true for
4397 all the values in the ranges.
4399 - Return BOOLEAN_FALSE_NODE if the comparison always returns false.
4401 - Return NULL_TREE if it is not always possible to determine the
4402 value of the comparison.
4404 Also set *STRICT_OVERFLOW_P to indicate whether a range with an
4405 overflow infinity was used in the test. */
4409 compare_ranges (enum tree_code comp
, value_range
*vr0
, value_range
*vr1
,
4410 bool *strict_overflow_p
)
4412 /* VARYING or UNDEFINED ranges cannot be compared. */
4413 if (vr0
->type
== VR_VARYING
4414 || vr0
->type
== VR_UNDEFINED
4415 || vr1
->type
== VR_VARYING
4416 || vr1
->type
== VR_UNDEFINED
)
4419 /* Anti-ranges need to be handled separately. */
4420 if (vr0
->type
== VR_ANTI_RANGE
|| vr1
->type
== VR_ANTI_RANGE
)
4422 /* If both are anti-ranges, then we cannot compute any
4424 if (vr0
->type
== VR_ANTI_RANGE
&& vr1
->type
== VR_ANTI_RANGE
)
4427 /* These comparisons are never statically computable. */
4434 /* Equality can be computed only between a range and an
4435 anti-range. ~[VAL1, VAL2] == [VAL1, VAL2] is always false. */
4436 if (vr0
->type
== VR_RANGE
)
4438 /* To simplify processing, make VR0 the anti-range. */
4439 value_range
*tmp
= vr0
;
4444 gcc_assert (comp
== NE_EXPR
|| comp
== EQ_EXPR
);
4446 if (compare_values_warnv (vr0
->min
, vr1
->min
, strict_overflow_p
) == 0
4447 && compare_values_warnv (vr0
->max
, vr1
->max
, strict_overflow_p
) == 0)
4448 return (comp
== NE_EXPR
) ? boolean_true_node
: boolean_false_node
;
4453 if (!usable_range_p (vr0
, strict_overflow_p
)
4454 || !usable_range_p (vr1
, strict_overflow_p
))
4457 /* Simplify processing. If COMP is GT_EXPR or GE_EXPR, switch the
4458 operands around and change the comparison code. */
4459 if (comp
== GT_EXPR
|| comp
== GE_EXPR
)
4461 comp
= (comp
== GT_EXPR
) ? LT_EXPR
: LE_EXPR
;
4462 std::swap (vr0
, vr1
);
4465 if (comp
== EQ_EXPR
)
4467 /* Equality may only be computed if both ranges represent
4468 exactly one value. */
4469 if (compare_values_warnv (vr0
->min
, vr0
->max
, strict_overflow_p
) == 0
4470 && compare_values_warnv (vr1
->min
, vr1
->max
, strict_overflow_p
) == 0)
4472 int cmp_min
= compare_values_warnv (vr0
->min
, vr1
->min
,
4474 int cmp_max
= compare_values_warnv (vr0
->max
, vr1
->max
,
4476 if (cmp_min
== 0 && cmp_max
== 0)
4477 return boolean_true_node
;
4478 else if (cmp_min
!= -2 && cmp_max
!= -2)
4479 return boolean_false_node
;
4481 /* If [V0_MIN, V1_MAX] < [V1_MIN, V1_MAX] then V0 != V1. */
4482 else if (compare_values_warnv (vr0
->min
, vr1
->max
,
4483 strict_overflow_p
) == 1
4484 || compare_values_warnv (vr1
->min
, vr0
->max
,
4485 strict_overflow_p
) == 1)
4486 return boolean_false_node
;
4490 else if (comp
== NE_EXPR
)
4494 /* If VR0 is completely to the left or completely to the right
4495 of VR1, they are always different. Notice that we need to
4496 make sure that both comparisons yield similar results to
4497 avoid comparing values that cannot be compared at
4499 cmp1
= compare_values_warnv (vr0
->max
, vr1
->min
, strict_overflow_p
);
4500 cmp2
= compare_values_warnv (vr0
->min
, vr1
->max
, strict_overflow_p
);
4501 if ((cmp1
== -1 && cmp2
== -1) || (cmp1
== 1 && cmp2
== 1))
4502 return boolean_true_node
;
4504 /* If VR0 and VR1 represent a single value and are identical,
4506 else if (compare_values_warnv (vr0
->min
, vr0
->max
,
4507 strict_overflow_p
) == 0
4508 && compare_values_warnv (vr1
->min
, vr1
->max
,
4509 strict_overflow_p
) == 0
4510 && compare_values_warnv (vr0
->min
, vr1
->min
,
4511 strict_overflow_p
) == 0
4512 && compare_values_warnv (vr0
->max
, vr1
->max
,
4513 strict_overflow_p
) == 0)
4514 return boolean_false_node
;
4516 /* Otherwise, they may or may not be different. */
4520 else if (comp
== LT_EXPR
|| comp
== LE_EXPR
)
4524 /* If VR0 is to the left of VR1, return true. */
4525 tst
= compare_values_warnv (vr0
->max
, vr1
->min
, strict_overflow_p
);
4526 if ((comp
== LT_EXPR
&& tst
== -1)
4527 || (comp
== LE_EXPR
&& (tst
== -1 || tst
== 0)))
4529 if (overflow_infinity_range_p (vr0
)
4530 || overflow_infinity_range_p (vr1
))
4531 *strict_overflow_p
= true;
4532 return boolean_true_node
;
4535 /* If VR0 is to the right of VR1, return false. */
4536 tst
= compare_values_warnv (vr0
->min
, vr1
->max
, strict_overflow_p
);
4537 if ((comp
== LT_EXPR
&& (tst
== 0 || tst
== 1))
4538 || (comp
== LE_EXPR
&& tst
== 1))
4540 if (overflow_infinity_range_p (vr0
)
4541 || overflow_infinity_range_p (vr1
))
4542 *strict_overflow_p
= true;
4543 return boolean_false_node
;
4546 /* Otherwise, we don't know. */
4554 /* Given a value range VR, a value VAL and a comparison code COMP, return
4555 BOOLEAN_TRUE_NODE if VR COMP VAL always returns true for all the
4556 values in VR. Return BOOLEAN_FALSE_NODE if the comparison
4557 always returns false. Return NULL_TREE if it is not always
4558 possible to determine the value of the comparison. Also set
4559 *STRICT_OVERFLOW_P to indicate whether a range with an overflow
4560 infinity was used in the test. */
4563 compare_range_with_value (enum tree_code comp
, value_range
*vr
, tree val
,
4564 bool *strict_overflow_p
)
4566 if (vr
->type
== VR_VARYING
|| vr
->type
== VR_UNDEFINED
)
4569 /* Anti-ranges need to be handled separately. */
4570 if (vr
->type
== VR_ANTI_RANGE
)
4572 /* For anti-ranges, the only predicates that we can compute at
4573 compile time are equality and inequality. */
4580 /* ~[VAL_1, VAL_2] OP VAL is known if VAL_1 <= VAL <= VAL_2. */
4581 if (value_inside_range (val
, vr
->min
, vr
->max
) == 1)
4582 return (comp
== NE_EXPR
) ? boolean_true_node
: boolean_false_node
;
4587 if (!usable_range_p (vr
, strict_overflow_p
))
4590 if (comp
== EQ_EXPR
)
4592 /* EQ_EXPR may only be computed if VR represents exactly
4594 if (compare_values_warnv (vr
->min
, vr
->max
, strict_overflow_p
) == 0)
4596 int cmp
= compare_values_warnv (vr
->min
, val
, strict_overflow_p
);
4598 return boolean_true_node
;
4599 else if (cmp
== -1 || cmp
== 1 || cmp
== 2)
4600 return boolean_false_node
;
4602 else if (compare_values_warnv (val
, vr
->min
, strict_overflow_p
) == -1
4603 || compare_values_warnv (vr
->max
, val
, strict_overflow_p
) == -1)
4604 return boolean_false_node
;
4608 else if (comp
== NE_EXPR
)
4610 /* If VAL is not inside VR, then they are always different. */
4611 if (compare_values_warnv (vr
->max
, val
, strict_overflow_p
) == -1
4612 || compare_values_warnv (vr
->min
, val
, strict_overflow_p
) == 1)
4613 return boolean_true_node
;
4615 /* If VR represents exactly one value equal to VAL, then return
4617 if (compare_values_warnv (vr
->min
, vr
->max
, strict_overflow_p
) == 0
4618 && compare_values_warnv (vr
->min
, val
, strict_overflow_p
) == 0)
4619 return boolean_false_node
;
4621 /* Otherwise, they may or may not be different. */
4624 else if (comp
== LT_EXPR
|| comp
== LE_EXPR
)
4628 /* If VR is to the left of VAL, return true. */
4629 tst
= compare_values_warnv (vr
->max
, val
, strict_overflow_p
);
4630 if ((comp
== LT_EXPR
&& tst
== -1)
4631 || (comp
== LE_EXPR
&& (tst
== -1 || tst
== 0)))
4633 if (overflow_infinity_range_p (vr
))
4634 *strict_overflow_p
= true;
4635 return boolean_true_node
;
4638 /* If VR is to the right of VAL, return false. */
4639 tst
= compare_values_warnv (vr
->min
, val
, strict_overflow_p
);
4640 if ((comp
== LT_EXPR
&& (tst
== 0 || tst
== 1))
4641 || (comp
== LE_EXPR
&& tst
== 1))
4643 if (overflow_infinity_range_p (vr
))
4644 *strict_overflow_p
= true;
4645 return boolean_false_node
;
4648 /* Otherwise, we don't know. */
4651 else if (comp
== GT_EXPR
|| comp
== GE_EXPR
)
4655 /* If VR is to the right of VAL, return true. */
4656 tst
= compare_values_warnv (vr
->min
, val
, strict_overflow_p
);
4657 if ((comp
== GT_EXPR
&& tst
== 1)
4658 || (comp
== GE_EXPR
&& (tst
== 0 || tst
== 1)))
4660 if (overflow_infinity_range_p (vr
))
4661 *strict_overflow_p
= true;
4662 return boolean_true_node
;
4665 /* If VR is to the left of VAL, return false. */
4666 tst
= compare_values_warnv (vr
->max
, val
, strict_overflow_p
);
4667 if ((comp
== GT_EXPR
&& (tst
== -1 || tst
== 0))
4668 || (comp
== GE_EXPR
&& tst
== -1))
4670 if (overflow_infinity_range_p (vr
))
4671 *strict_overflow_p
= true;
4672 return boolean_false_node
;
4675 /* Otherwise, we don't know. */
4683 /* Debugging dumps. */
4685 void dump_value_range (FILE *, value_range
*);
4686 void debug_value_range (value_range
*);
4687 void dump_all_value_ranges (FILE *);
4688 void debug_all_value_ranges (void);
4689 void dump_vr_equiv (FILE *, bitmap
);
4690 void debug_vr_equiv (bitmap
);
4693 /* Dump value range VR to FILE. */
4696 dump_value_range (FILE *file
, value_range
*vr
)
4699 fprintf (file
, "[]");
4700 else if (vr
->type
== VR_UNDEFINED
)
4701 fprintf (file
, "UNDEFINED");
4702 else if (vr
->type
== VR_RANGE
|| vr
->type
== VR_ANTI_RANGE
)
4704 tree type
= TREE_TYPE (vr
->min
);
4706 fprintf (file
, "%s[", (vr
->type
== VR_ANTI_RANGE
) ? "~" : "");
4708 if (is_negative_overflow_infinity (vr
->min
))
4709 fprintf (file
, "-INF(OVF)");
4710 else if (INTEGRAL_TYPE_P (type
)
4711 && !TYPE_UNSIGNED (type
)
4712 && vrp_val_is_min (vr
->min
))
4713 fprintf (file
, "-INF");
4715 print_generic_expr (file
, vr
->min
, 0);
4717 fprintf (file
, ", ");
4719 if (is_positive_overflow_infinity (vr
->max
))
4720 fprintf (file
, "+INF(OVF)");
4721 else if (INTEGRAL_TYPE_P (type
)
4722 && vrp_val_is_max (vr
->max
))
4723 fprintf (file
, "+INF");
4725 print_generic_expr (file
, vr
->max
, 0);
4727 fprintf (file
, "]");
4734 fprintf (file
, " EQUIVALENCES: { ");
4736 EXECUTE_IF_SET_IN_BITMAP (vr
->equiv
, 0, i
, bi
)
4738 print_generic_expr (file
, ssa_name (i
), 0);
4739 fprintf (file
, " ");
4743 fprintf (file
, "} (%u elements)", c
);
4746 else if (vr
->type
== VR_VARYING
)
4747 fprintf (file
, "VARYING");
4749 fprintf (file
, "INVALID RANGE");
4753 /* Dump value range VR to stderr. */
4756 debug_value_range (value_range
*vr
)
4758 dump_value_range (stderr
, vr
);
4759 fprintf (stderr
, "\n");
4763 /* Dump value ranges of all SSA_NAMEs to FILE. */
4766 dump_all_value_ranges (FILE *file
)
4770 for (i
= 0; i
< num_vr_values
; i
++)
4774 print_generic_expr (file
, ssa_name (i
), 0);
4775 fprintf (file
, ": ");
4776 dump_value_range (file
, vr_value
[i
]);
4777 fprintf (file
, "\n");
4781 fprintf (file
, "\n");
4785 /* Dump all value ranges to stderr. */
4788 debug_all_value_ranges (void)
4790 dump_all_value_ranges (stderr
);
4794 /* Given a COND_EXPR COND of the form 'V OP W', and an SSA name V,
4795 create a new SSA name N and return the assertion assignment
4796 'N = ASSERT_EXPR <V, V OP W>'. */
4799 build_assert_expr_for (tree cond
, tree v
)
4804 gcc_assert (TREE_CODE (v
) == SSA_NAME
4805 && COMPARISON_CLASS_P (cond
));
4807 a
= build2 (ASSERT_EXPR
, TREE_TYPE (v
), v
, cond
);
4808 assertion
= gimple_build_assign (NULL_TREE
, a
);
4810 /* The new ASSERT_EXPR, creates a new SSA name that replaces the
4811 operand of the ASSERT_EXPR. Create it so the new name and the old one
4812 are registered in the replacement table so that we can fix the SSA web
4813 after adding all the ASSERT_EXPRs. */
4814 create_new_def_for (v
, assertion
, NULL
);
4820 /* Return false if EXPR is a predicate expression involving floating
4824 fp_predicate (gimple
*stmt
)
4826 GIMPLE_CHECK (stmt
, GIMPLE_COND
);
4828 return FLOAT_TYPE_P (TREE_TYPE (gimple_cond_lhs (stmt
)));
4831 /* If the range of values taken by OP can be inferred after STMT executes,
4832 return the comparison code (COMP_CODE_P) and value (VAL_P) that
4833 describes the inferred range. Return true if a range could be
4837 infer_value_range (gimple
*stmt
, tree op
, tree_code
*comp_code_p
, tree
*val_p
)
4840 *comp_code_p
= ERROR_MARK
;
4842 /* Do not attempt to infer anything in names that flow through
4844 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op
))
4847 /* Similarly, don't infer anything from statements that may throw
4848 exceptions. ??? Relax this requirement? */
4849 if (stmt_could_throw_p (stmt
))
4852 /* If STMT is the last statement of a basic block with no normal
4853 successors, there is no point inferring anything about any of its
4854 operands. We would not be able to find a proper insertion point
4855 for the assertion, anyway. */
4856 if (stmt_ends_bb_p (stmt
))
4861 FOR_EACH_EDGE (e
, ei
, gimple_bb (stmt
)->succs
)
4862 if (!(e
->flags
& EDGE_ABNORMAL
))
4868 if (infer_nonnull_range (stmt
, op
))
4870 *val_p
= build_int_cst (TREE_TYPE (op
), 0);
4871 *comp_code_p
= NE_EXPR
;
4879 void dump_asserts_for (FILE *, tree
);
4880 void debug_asserts_for (tree
);
4881 void dump_all_asserts (FILE *);
4882 void debug_all_asserts (void);
4884 /* Dump all the registered assertions for NAME to FILE. */
4887 dump_asserts_for (FILE *file
, tree name
)
4891 fprintf (file
, "Assertions to be inserted for ");
4892 print_generic_expr (file
, name
, 0);
4893 fprintf (file
, "\n");
4895 loc
= asserts_for
[SSA_NAME_VERSION (name
)];
4898 fprintf (file
, "\t");
4899 print_gimple_stmt (file
, gsi_stmt (loc
->si
), 0, 0);
4900 fprintf (file
, "\n\tBB #%d", loc
->bb
->index
);
4903 fprintf (file
, "\n\tEDGE %d->%d", loc
->e
->src
->index
,
4904 loc
->e
->dest
->index
);
4905 dump_edge_info (file
, loc
->e
, dump_flags
, 0);
4907 fprintf (file
, "\n\tPREDICATE: ");
4908 print_generic_expr (file
, name
, 0);
4909 fprintf (file
, " %s ", get_tree_code_name (loc
->comp_code
));
4910 print_generic_expr (file
, loc
->val
, 0);
4911 fprintf (file
, "\n\n");
4915 fprintf (file
, "\n");
4919 /* Dump all the registered assertions for NAME to stderr. */
4922 debug_asserts_for (tree name
)
4924 dump_asserts_for (stderr
, name
);
4928 /* Dump all the registered assertions for all the names to FILE. */
4931 dump_all_asserts (FILE *file
)
4936 fprintf (file
, "\nASSERT_EXPRs to be inserted\n\n");
4937 EXECUTE_IF_SET_IN_BITMAP (need_assert_for
, 0, i
, bi
)
4938 dump_asserts_for (file
, ssa_name (i
));
4939 fprintf (file
, "\n");
4943 /* Dump all the registered assertions for all the names to stderr. */
4946 debug_all_asserts (void)
4948 dump_all_asserts (stderr
);
4952 /* If NAME doesn't have an ASSERT_EXPR registered for asserting
4953 'EXPR COMP_CODE VAL' at a location that dominates block BB or
4954 E->DEST, then register this location as a possible insertion point
4955 for ASSERT_EXPR <NAME, EXPR COMP_CODE VAL>.
4957 BB, E and SI provide the exact insertion point for the new
4958 ASSERT_EXPR. If BB is NULL, then the ASSERT_EXPR is to be inserted
4959 on edge E. Otherwise, if E is NULL, the ASSERT_EXPR is inserted on
4960 BB. If SI points to a COND_EXPR or a SWITCH_EXPR statement, then E
4961 must not be NULL. */
4964 register_new_assert_for (tree name
, tree expr
,
4965 enum tree_code comp_code
,
4969 gimple_stmt_iterator si
)
4971 assert_locus
*n
, *loc
, *last_loc
;
4972 basic_block dest_bb
;
4974 gcc_checking_assert (bb
== NULL
|| e
== NULL
);
4977 gcc_checking_assert (gimple_code (gsi_stmt (si
)) != GIMPLE_COND
4978 && gimple_code (gsi_stmt (si
)) != GIMPLE_SWITCH
);
4980 /* Never build an assert comparing against an integer constant with
4981 TREE_OVERFLOW set. This confuses our undefined overflow warning
4983 if (TREE_OVERFLOW_P (val
))
4984 val
= drop_tree_overflow (val
);
4986 /* The new assertion A will be inserted at BB or E. We need to
4987 determine if the new location is dominated by a previously
4988 registered location for A. If we are doing an edge insertion,
4989 assume that A will be inserted at E->DEST. Note that this is not
4992 If E is a critical edge, it will be split. But even if E is
4993 split, the new block will dominate the same set of blocks that
4996 The reverse, however, is not true, blocks dominated by E->DEST
4997 will not be dominated by the new block created to split E. So,
4998 if the insertion location is on a critical edge, we will not use
4999 the new location to move another assertion previously registered
5000 at a block dominated by E->DEST. */
5001 dest_bb
= (bb
) ? bb
: e
->dest
;
5003 /* If NAME already has an ASSERT_EXPR registered for COMP_CODE and
5004 VAL at a block dominating DEST_BB, then we don't need to insert a new
5005 one. Similarly, if the same assertion already exists at a block
5006 dominated by DEST_BB and the new location is not on a critical
5007 edge, then update the existing location for the assertion (i.e.,
5008 move the assertion up in the dominance tree).
5010 Note, this is implemented as a simple linked list because there
5011 should not be more than a handful of assertions registered per
5012 name. If this becomes a performance problem, a table hashed by
5013 COMP_CODE and VAL could be implemented. */
5014 loc
= asserts_for
[SSA_NAME_VERSION (name
)];
5018 if (loc
->comp_code
== comp_code
5020 || operand_equal_p (loc
->val
, val
, 0))
5021 && (loc
->expr
== expr
5022 || operand_equal_p (loc
->expr
, expr
, 0)))
5024 /* If E is not a critical edge and DEST_BB
5025 dominates the existing location for the assertion, move
5026 the assertion up in the dominance tree by updating its
5027 location information. */
5028 if ((e
== NULL
|| !EDGE_CRITICAL_P (e
))
5029 && dominated_by_p (CDI_DOMINATORS
, loc
->bb
, dest_bb
))
5038 /* Update the last node of the list and move to the next one. */
5043 /* If we didn't find an assertion already registered for
5044 NAME COMP_CODE VAL, add a new one at the end of the list of
5045 assertions associated with NAME. */
5046 n
= XNEW (struct assert_locus
);
5050 n
->comp_code
= comp_code
;
5058 asserts_for
[SSA_NAME_VERSION (name
)] = n
;
5060 bitmap_set_bit (need_assert_for
, SSA_NAME_VERSION (name
));
5063 /* (COND_OP0 COND_CODE COND_OP1) is a predicate which uses NAME.
5064 Extract a suitable test code and value and store them into *CODE_P and
5065 *VAL_P so the predicate is normalized to NAME *CODE_P *VAL_P.
5067 If no extraction was possible, return FALSE, otherwise return TRUE.
5069 If INVERT is true, then we invert the result stored into *CODE_P. */
5072 extract_code_and_val_from_cond_with_ops (tree name
, enum tree_code cond_code
,
5073 tree cond_op0
, tree cond_op1
,
5074 bool invert
, enum tree_code
*code_p
,
5077 enum tree_code comp_code
;
5080 /* Otherwise, we have a comparison of the form NAME COMP VAL
5081 or VAL COMP NAME. */
5082 if (name
== cond_op1
)
5084 /* If the predicate is of the form VAL COMP NAME, flip
5085 COMP around because we need to register NAME as the
5086 first operand in the predicate. */
5087 comp_code
= swap_tree_comparison (cond_code
);
5092 /* The comparison is of the form NAME COMP VAL, so the
5093 comparison code remains unchanged. */
5094 comp_code
= cond_code
;
5098 /* Invert the comparison code as necessary. */
5100 comp_code
= invert_tree_comparison (comp_code
, 0);
5102 /* VRP only handles integral and pointer types. */
5103 if (! INTEGRAL_TYPE_P (TREE_TYPE (val
))
5104 && ! POINTER_TYPE_P (TREE_TYPE (val
)))
5107 /* Do not register always-false predicates.
5108 FIXME: this works around a limitation in fold() when dealing with
5109 enumerations. Given 'enum { N1, N2 } x;', fold will not
5110 fold 'if (x > N2)' to 'if (0)'. */
5111 if ((comp_code
== GT_EXPR
|| comp_code
== LT_EXPR
)
5112 && INTEGRAL_TYPE_P (TREE_TYPE (val
)))
5114 tree min
= TYPE_MIN_VALUE (TREE_TYPE (val
));
5115 tree max
= TYPE_MAX_VALUE (TREE_TYPE (val
));
5117 if (comp_code
== GT_EXPR
5119 || compare_values (val
, max
) == 0))
5122 if (comp_code
== LT_EXPR
5124 || compare_values (val
, min
) == 0))
5127 *code_p
= comp_code
;
5132 /* Find out smallest RES where RES > VAL && (RES & MASK) == RES, if any
5133 (otherwise return VAL). VAL and MASK must be zero-extended for
5134 precision PREC. If SGNBIT is non-zero, first xor VAL with SGNBIT
5135 (to transform signed values into unsigned) and at the end xor
5139 masked_increment (const wide_int
&val_in
, const wide_int
&mask
,
5140 const wide_int
&sgnbit
, unsigned int prec
)
5142 wide_int bit
= wi::one (prec
), res
;
5145 wide_int val
= val_in
^ sgnbit
;
5146 for (i
= 0; i
< prec
; i
++, bit
+= bit
)
5149 if ((res
& bit
) == 0)
5152 res
= (val
+ bit
).and_not (res
);
5154 if (wi::gtu_p (res
, val
))
5155 return res
^ sgnbit
;
5157 return val
^ sgnbit
;
5160 /* Try to register an edge assertion for SSA name NAME on edge E for
5161 the condition COND contributing to the conditional jump pointed to by BSI.
5162 Invert the condition COND if INVERT is true. */
5165 register_edge_assert_for_2 (tree name
, edge e
, gimple_stmt_iterator bsi
,
5166 enum tree_code cond_code
,
5167 tree cond_op0
, tree cond_op1
, bool invert
)
5170 enum tree_code comp_code
;
5172 if (!extract_code_and_val_from_cond_with_ops (name
, cond_code
,
5175 invert
, &comp_code
, &val
))
5178 /* Only register an ASSERT_EXPR if NAME was found in the sub-graph
5179 reachable from E. */
5180 if (live_on_edge (e
, name
)
5181 && !has_single_use (name
))
5182 register_new_assert_for (name
, name
, comp_code
, val
, NULL
, e
, bsi
);
5184 /* In the case of NAME <= CST and NAME being defined as
5185 NAME = (unsigned) NAME2 + CST2 we can assert NAME2 >= -CST2
5186 and NAME2 <= CST - CST2. We can do the same for NAME > CST.
5187 This catches range and anti-range tests. */
5188 if ((comp_code
== LE_EXPR
5189 || comp_code
== GT_EXPR
)
5190 && TREE_CODE (val
) == INTEGER_CST
5191 && TYPE_UNSIGNED (TREE_TYPE (val
)))
5193 gimple
*def_stmt
= SSA_NAME_DEF_STMT (name
);
5194 tree cst2
= NULL_TREE
, name2
= NULL_TREE
, name3
= NULL_TREE
;
5196 /* Extract CST2 from the (optional) addition. */
5197 if (is_gimple_assign (def_stmt
)
5198 && gimple_assign_rhs_code (def_stmt
) == PLUS_EXPR
)
5200 name2
= gimple_assign_rhs1 (def_stmt
);
5201 cst2
= gimple_assign_rhs2 (def_stmt
);
5202 if (TREE_CODE (name2
) == SSA_NAME
5203 && TREE_CODE (cst2
) == INTEGER_CST
)
5204 def_stmt
= SSA_NAME_DEF_STMT (name2
);
5207 /* Extract NAME2 from the (optional) sign-changing cast. */
5208 if (gimple_assign_cast_p (def_stmt
))
5210 if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt
))
5211 && ! TYPE_UNSIGNED (TREE_TYPE (gimple_assign_rhs1 (def_stmt
)))
5212 && (TYPE_PRECISION (gimple_expr_type (def_stmt
))
5213 == TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (def_stmt
)))))
5214 name3
= gimple_assign_rhs1 (def_stmt
);
5217 /* If name3 is used later, create an ASSERT_EXPR for it. */
5218 if (name3
!= NULL_TREE
5219 && TREE_CODE (name3
) == SSA_NAME
5220 && (cst2
== NULL_TREE
5221 || TREE_CODE (cst2
) == INTEGER_CST
)
5222 && INTEGRAL_TYPE_P (TREE_TYPE (name3
))
5223 && live_on_edge (e
, name3
)
5224 && !has_single_use (name3
))
5228 /* Build an expression for the range test. */
5229 tmp
= build1 (NOP_EXPR
, TREE_TYPE (name
), name3
);
5230 if (cst2
!= NULL_TREE
)
5231 tmp
= build2 (PLUS_EXPR
, TREE_TYPE (name
), tmp
, cst2
);
5235 fprintf (dump_file
, "Adding assert for ");
5236 print_generic_expr (dump_file
, name3
, 0);
5237 fprintf (dump_file
, " from ");
5238 print_generic_expr (dump_file
, tmp
, 0);
5239 fprintf (dump_file
, "\n");
5242 register_new_assert_for (name3
, tmp
, comp_code
, val
, NULL
, e
, bsi
);
5245 /* If name2 is used later, create an ASSERT_EXPR for it. */
5246 if (name2
!= NULL_TREE
5247 && TREE_CODE (name2
) == SSA_NAME
5248 && TREE_CODE (cst2
) == INTEGER_CST
5249 && INTEGRAL_TYPE_P (TREE_TYPE (name2
))
5250 && live_on_edge (e
, name2
)
5251 && !has_single_use (name2
))
5255 /* Build an expression for the range test. */
5257 if (TREE_TYPE (name
) != TREE_TYPE (name2
))
5258 tmp
= build1 (NOP_EXPR
, TREE_TYPE (name
), tmp
);
5259 if (cst2
!= NULL_TREE
)
5260 tmp
= build2 (PLUS_EXPR
, TREE_TYPE (name
), tmp
, cst2
);
5264 fprintf (dump_file
, "Adding assert for ");
5265 print_generic_expr (dump_file
, name2
, 0);
5266 fprintf (dump_file
, " from ");
5267 print_generic_expr (dump_file
, tmp
, 0);
5268 fprintf (dump_file
, "\n");
5271 register_new_assert_for (name2
, tmp
, comp_code
, val
, NULL
, e
, bsi
);
5275 /* In the case of post-in/decrement tests like if (i++) ... and uses
5276 of the in/decremented value on the edge the extra name we want to
5277 assert for is not on the def chain of the name compared. Instead
5278 it is in the set of use stmts.
5279 Similar cases happen for conversions that were simplified through
5280 fold_{sign_changed,widened}_comparison. */
5281 if ((comp_code
== NE_EXPR
5282 || comp_code
== EQ_EXPR
)
5283 && TREE_CODE (val
) == INTEGER_CST
)
5285 imm_use_iterator ui
;
5287 FOR_EACH_IMM_USE_STMT (use_stmt
, ui
, name
)
5289 if (!is_gimple_assign (use_stmt
))
5292 /* Cut off to use-stmts that are dominating the predecessor. */
5293 if (!dominated_by_p (CDI_DOMINATORS
, e
->src
, gimple_bb (use_stmt
)))
5296 tree name2
= gimple_assign_lhs (use_stmt
);
5297 if (TREE_CODE (name2
) != SSA_NAME
5298 || !live_on_edge (e
, name2
))
5301 enum tree_code code
= gimple_assign_rhs_code (use_stmt
);
5303 if (code
== PLUS_EXPR
5304 || code
== MINUS_EXPR
)
5306 cst
= gimple_assign_rhs2 (use_stmt
);
5307 if (TREE_CODE (cst
) != INTEGER_CST
)
5309 cst
= int_const_binop (code
, val
, cst
);
5311 else if (CONVERT_EXPR_CODE_P (code
))
5313 /* For truncating conversions we cannot record
5315 if (comp_code
== NE_EXPR
5316 && (TYPE_PRECISION (TREE_TYPE (name2
))
5317 < TYPE_PRECISION (TREE_TYPE (name
))))
5319 cst
= fold_convert (TREE_TYPE (name2
), val
);
5324 if (TREE_OVERFLOW_P (cst
))
5325 cst
= drop_tree_overflow (cst
);
5326 register_new_assert_for (name2
, name2
, comp_code
, cst
,
5331 if (TREE_CODE_CLASS (comp_code
) == tcc_comparison
5332 && TREE_CODE (val
) == INTEGER_CST
)
5334 gimple
*def_stmt
= SSA_NAME_DEF_STMT (name
);
5335 tree name2
= NULL_TREE
, names
[2], cst2
= NULL_TREE
;
5336 tree val2
= NULL_TREE
;
5337 unsigned int prec
= TYPE_PRECISION (TREE_TYPE (val
));
5338 wide_int mask
= wi::zero (prec
);
5339 unsigned int nprec
= prec
;
5340 enum tree_code rhs_code
= ERROR_MARK
;
5342 if (is_gimple_assign (def_stmt
))
5343 rhs_code
= gimple_assign_rhs_code (def_stmt
);
5345 /* In the case of NAME != CST1 where NAME = A +- CST2 we can
5346 assert that A != CST1 -+ CST2. */
5347 if ((comp_code
== EQ_EXPR
|| comp_code
== NE_EXPR
)
5348 && (rhs_code
== PLUS_EXPR
|| rhs_code
== MINUS_EXPR
))
5350 tree op0
= gimple_assign_rhs1 (def_stmt
);
5351 tree op1
= gimple_assign_rhs2 (def_stmt
);
5352 if (TREE_CODE (op0
) == SSA_NAME
5353 && TREE_CODE (op1
) == INTEGER_CST
5354 && live_on_edge (e
, op0
)
5355 && !has_single_use (op0
))
5357 enum tree_code reverse_op
= (rhs_code
== PLUS_EXPR
5358 ? MINUS_EXPR
: PLUS_EXPR
);
5359 op1
= int_const_binop (reverse_op
, val
, op1
);
5360 if (TREE_OVERFLOW (op1
))
5361 op1
= drop_tree_overflow (op1
);
5362 register_new_assert_for (op0
, op0
, comp_code
, op1
, NULL
, e
, bsi
);
5366 /* Add asserts for NAME cmp CST and NAME being defined
5367 as NAME = (int) NAME2. */
5368 if (!TYPE_UNSIGNED (TREE_TYPE (val
))
5369 && (comp_code
== LE_EXPR
|| comp_code
== LT_EXPR
5370 || comp_code
== GT_EXPR
|| comp_code
== GE_EXPR
)
5371 && gimple_assign_cast_p (def_stmt
))
5373 name2
= gimple_assign_rhs1 (def_stmt
);
5374 if (CONVERT_EXPR_CODE_P (rhs_code
)
5375 && INTEGRAL_TYPE_P (TREE_TYPE (name2
))
5376 && TYPE_UNSIGNED (TREE_TYPE (name2
))
5377 && prec
== TYPE_PRECISION (TREE_TYPE (name2
))
5378 && (comp_code
== LE_EXPR
|| comp_code
== GT_EXPR
5379 || !tree_int_cst_equal (val
,
5380 TYPE_MIN_VALUE (TREE_TYPE (val
))))
5381 && live_on_edge (e
, name2
)
5382 && !has_single_use (name2
))
5385 enum tree_code new_comp_code
= comp_code
;
5387 cst
= fold_convert (TREE_TYPE (name2
),
5388 TYPE_MIN_VALUE (TREE_TYPE (val
)));
5389 /* Build an expression for the range test. */
5390 tmp
= build2 (PLUS_EXPR
, TREE_TYPE (name2
), name2
, cst
);
5391 cst
= fold_build2 (PLUS_EXPR
, TREE_TYPE (name2
), cst
,
5392 fold_convert (TREE_TYPE (name2
), val
));
5393 if (comp_code
== LT_EXPR
|| comp_code
== GE_EXPR
)
5395 new_comp_code
= comp_code
== LT_EXPR
? LE_EXPR
: GT_EXPR
;
5396 cst
= fold_build2 (MINUS_EXPR
, TREE_TYPE (name2
), cst
,
5397 build_int_cst (TREE_TYPE (name2
), 1));
5402 fprintf (dump_file
, "Adding assert for ");
5403 print_generic_expr (dump_file
, name2
, 0);
5404 fprintf (dump_file
, " from ");
5405 print_generic_expr (dump_file
, tmp
, 0);
5406 fprintf (dump_file
, "\n");
5409 register_new_assert_for (name2
, tmp
, new_comp_code
, cst
, NULL
,
5414 /* Add asserts for NAME cmp CST and NAME being defined as
5415 NAME = NAME2 >> CST2.
5417 Extract CST2 from the right shift. */
5418 if (rhs_code
== RSHIFT_EXPR
)
5420 name2
= gimple_assign_rhs1 (def_stmt
);
5421 cst2
= gimple_assign_rhs2 (def_stmt
);
5422 if (TREE_CODE (name2
) == SSA_NAME
5423 && tree_fits_uhwi_p (cst2
)
5424 && INTEGRAL_TYPE_P (TREE_TYPE (name2
))
5425 && IN_RANGE (tree_to_uhwi (cst2
), 1, prec
- 1)
5426 && prec
== GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (val
)))
5427 && live_on_edge (e
, name2
)
5428 && !has_single_use (name2
))
5430 mask
= wi::mask (tree_to_uhwi (cst2
), false, prec
);
5431 val2
= fold_binary (LSHIFT_EXPR
, TREE_TYPE (val
), val
, cst2
);
5434 if (val2
!= NULL_TREE
5435 && TREE_CODE (val2
) == INTEGER_CST
5436 && simple_cst_equal (fold_build2 (RSHIFT_EXPR
,
5440 enum tree_code new_comp_code
= comp_code
;
5444 if (comp_code
== EQ_EXPR
|| comp_code
== NE_EXPR
)
5446 if (!TYPE_UNSIGNED (TREE_TYPE (val
)))
5448 tree type
= build_nonstandard_integer_type (prec
, 1);
5449 tmp
= build1 (NOP_EXPR
, type
, name2
);
5450 val2
= fold_convert (type
, val2
);
5452 tmp
= fold_build2 (MINUS_EXPR
, TREE_TYPE (tmp
), tmp
, val2
);
5453 new_val
= wide_int_to_tree (TREE_TYPE (tmp
), mask
);
5454 new_comp_code
= comp_code
== EQ_EXPR
? LE_EXPR
: GT_EXPR
;
5456 else if (comp_code
== LT_EXPR
|| comp_code
== GE_EXPR
)
5459 = wi::min_value (prec
, TYPE_SIGN (TREE_TYPE (val
)));
5461 if (minval
== new_val
)
5462 new_val
= NULL_TREE
;
5467 = wi::max_value (prec
, TYPE_SIGN (TREE_TYPE (val
)));
5470 new_val
= NULL_TREE
;
5472 new_val
= wide_int_to_tree (TREE_TYPE (val2
), mask
);
5479 fprintf (dump_file
, "Adding assert for ");
5480 print_generic_expr (dump_file
, name2
, 0);
5481 fprintf (dump_file
, " from ");
5482 print_generic_expr (dump_file
, tmp
, 0);
5483 fprintf (dump_file
, "\n");
5486 register_new_assert_for (name2
, tmp
, new_comp_code
, new_val
,
5491 /* Add asserts for NAME cmp CST and NAME being defined as
5492 NAME = NAME2 & CST2.
5494 Extract CST2 from the and.
5497 NAME = (unsigned) NAME2;
5498 casts where NAME's type is unsigned and has smaller precision
5499 than NAME2's type as if it was NAME = NAME2 & MASK. */
5500 names
[0] = NULL_TREE
;
5501 names
[1] = NULL_TREE
;
5503 if (rhs_code
== BIT_AND_EXPR
5504 || (CONVERT_EXPR_CODE_P (rhs_code
)
5505 && INTEGRAL_TYPE_P (TREE_TYPE (val
))
5506 && TYPE_UNSIGNED (TREE_TYPE (val
))
5507 && TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (def_stmt
)))
5510 name2
= gimple_assign_rhs1 (def_stmt
);
5511 if (rhs_code
== BIT_AND_EXPR
)
5512 cst2
= gimple_assign_rhs2 (def_stmt
);
5515 cst2
= TYPE_MAX_VALUE (TREE_TYPE (val
));
5516 nprec
= TYPE_PRECISION (TREE_TYPE (name2
));
5518 if (TREE_CODE (name2
) == SSA_NAME
5519 && INTEGRAL_TYPE_P (TREE_TYPE (name2
))
5520 && TREE_CODE (cst2
) == INTEGER_CST
5521 && !integer_zerop (cst2
)
5523 || TYPE_UNSIGNED (TREE_TYPE (val
))))
5525 gimple
*def_stmt2
= SSA_NAME_DEF_STMT (name2
);
5526 if (gimple_assign_cast_p (def_stmt2
))
5528 names
[1] = gimple_assign_rhs1 (def_stmt2
);
5529 if (!CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt2
))
5530 || !INTEGRAL_TYPE_P (TREE_TYPE (names
[1]))
5531 || (TYPE_PRECISION (TREE_TYPE (name2
))
5532 != TYPE_PRECISION (TREE_TYPE (names
[1])))
5533 || !live_on_edge (e
, names
[1])
5534 || has_single_use (names
[1]))
5535 names
[1] = NULL_TREE
;
5537 if (live_on_edge (e
, name2
)
5538 && !has_single_use (name2
))
5542 if (names
[0] || names
[1])
5544 wide_int minv
, maxv
, valv
, cst2v
;
5545 wide_int tem
, sgnbit
;
5546 bool valid_p
= false, valn
, cst2n
;
5547 enum tree_code ccode
= comp_code
;
5549 valv
= wide_int::from (val
, nprec
, UNSIGNED
);
5550 cst2v
= wide_int::from (cst2
, nprec
, UNSIGNED
);
5551 valn
= wi::neg_p (valv
, TYPE_SIGN (TREE_TYPE (val
)));
5552 cst2n
= wi::neg_p (cst2v
, TYPE_SIGN (TREE_TYPE (val
)));
5553 /* If CST2 doesn't have most significant bit set,
5554 but VAL is negative, we have comparison like
5555 if ((x & 0x123) > -4) (always true). Just give up. */
5559 sgnbit
= wi::set_bit_in_zero (nprec
- 1, nprec
);
5561 sgnbit
= wi::zero (nprec
);
5562 minv
= valv
& cst2v
;
5566 /* Minimum unsigned value for equality is VAL & CST2
5567 (should be equal to VAL, otherwise we probably should
5568 have folded the comparison into false) and
5569 maximum unsigned value is VAL | ~CST2. */
5570 maxv
= valv
| ~cst2v
;
5575 tem
= valv
| ~cst2v
;
5576 /* If VAL is 0, handle (X & CST2) != 0 as (X & CST2) > 0U. */
5580 sgnbit
= wi::zero (nprec
);
5583 /* If (VAL | ~CST2) is all ones, handle it as
5584 (X & CST2) < VAL. */
5589 sgnbit
= wi::zero (nprec
);
5592 if (!cst2n
&& wi::neg_p (cst2v
))
5593 sgnbit
= wi::set_bit_in_zero (nprec
- 1, nprec
);
5602 if (tem
== wi::mask (nprec
- 1, false, nprec
))
5608 sgnbit
= wi::zero (nprec
);
5613 /* Minimum unsigned value for >= if (VAL & CST2) == VAL
5614 is VAL and maximum unsigned value is ~0. For signed
5615 comparison, if CST2 doesn't have most significant bit
5616 set, handle it similarly. If CST2 has MSB set,
5617 the minimum is the same, and maximum is ~0U/2. */
5620 /* If (VAL & CST2) != VAL, X & CST2 can't be equal to
5622 minv
= masked_increment (valv
, cst2v
, sgnbit
, nprec
);
5626 maxv
= wi::mask (nprec
- (cst2n
? 1 : 0), false, nprec
);
5632 /* Find out smallest MINV where MINV > VAL
5633 && (MINV & CST2) == MINV, if any. If VAL is signed and
5634 CST2 has MSB set, compute it biased by 1 << (nprec - 1). */
5635 minv
= masked_increment (valv
, cst2v
, sgnbit
, nprec
);
5638 maxv
= wi::mask (nprec
- (cst2n
? 1 : 0), false, nprec
);
5643 /* Minimum unsigned value for <= is 0 and maximum
5644 unsigned value is VAL | ~CST2 if (VAL & CST2) == VAL.
5645 Otherwise, find smallest VAL2 where VAL2 > VAL
5646 && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
5648 For signed comparison, if CST2 doesn't have most
5649 significant bit set, handle it similarly. If CST2 has
5650 MSB set, the maximum is the same and minimum is INT_MIN. */
5655 maxv
= masked_increment (valv
, cst2v
, sgnbit
, nprec
);
5667 /* Minimum unsigned value for < is 0 and maximum
5668 unsigned value is (VAL-1) | ~CST2 if (VAL & CST2) == VAL.
5669 Otherwise, find smallest VAL2 where VAL2 > VAL
5670 && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
5672 For signed comparison, if CST2 doesn't have most
5673 significant bit set, handle it similarly. If CST2 has
5674 MSB set, the maximum is the same and minimum is INT_MIN. */
5683 maxv
= masked_increment (valv
, cst2v
, sgnbit
, nprec
);
5697 && (maxv
- minv
) != -1)
5699 tree tmp
, new_val
, type
;
5702 for (i
= 0; i
< 2; i
++)
5705 wide_int maxv2
= maxv
;
5707 type
= TREE_TYPE (names
[i
]);
5708 if (!TYPE_UNSIGNED (type
))
5710 type
= build_nonstandard_integer_type (nprec
, 1);
5711 tmp
= build1 (NOP_EXPR
, type
, names
[i
]);
5715 tmp
= build2 (PLUS_EXPR
, type
, tmp
,
5716 wide_int_to_tree (type
, -minv
));
5717 maxv2
= maxv
- minv
;
5719 new_val
= wide_int_to_tree (type
, maxv2
);
5723 fprintf (dump_file
, "Adding assert for ");
5724 print_generic_expr (dump_file
, names
[i
], 0);
5725 fprintf (dump_file
, " from ");
5726 print_generic_expr (dump_file
, tmp
, 0);
5727 fprintf (dump_file
, "\n");
5730 register_new_assert_for (names
[i
], tmp
, LE_EXPR
,
5731 new_val
, NULL
, e
, bsi
);
5738 /* OP is an operand of a truth value expression which is known to have
5739 a particular value. Register any asserts for OP and for any
5740 operands in OP's defining statement.
5742 If CODE is EQ_EXPR, then we want to register OP is zero (false),
5743 if CODE is NE_EXPR, then we want to register OP is nonzero (true). */
5746 register_edge_assert_for_1 (tree op
, enum tree_code code
,
5747 edge e
, gimple_stmt_iterator bsi
)
5751 enum tree_code rhs_code
;
5753 /* We only care about SSA_NAMEs. */
5754 if (TREE_CODE (op
) != SSA_NAME
)
5757 /* We know that OP will have a zero or nonzero value. If OP is used
5758 more than once go ahead and register an assert for OP. */
5759 if (live_on_edge (e
, op
)
5760 && !has_single_use (op
))
5762 val
= build_int_cst (TREE_TYPE (op
), 0);
5763 register_new_assert_for (op
, op
, code
, val
, NULL
, e
, bsi
);
5766 /* Now look at how OP is set. If it's set from a comparison,
5767 a truth operation or some bit operations, then we may be able
5768 to register information about the operands of that assignment. */
5769 op_def
= SSA_NAME_DEF_STMT (op
);
5770 if (gimple_code (op_def
) != GIMPLE_ASSIGN
)
5773 rhs_code
= gimple_assign_rhs_code (op_def
);
5775 if (TREE_CODE_CLASS (rhs_code
) == tcc_comparison
)
5777 bool invert
= (code
== EQ_EXPR
? true : false);
5778 tree op0
= gimple_assign_rhs1 (op_def
);
5779 tree op1
= gimple_assign_rhs2 (op_def
);
5781 if (TREE_CODE (op0
) == SSA_NAME
)
5782 register_edge_assert_for_2 (op0
, e
, bsi
, rhs_code
, op0
, op1
, invert
);
5783 if (TREE_CODE (op1
) == SSA_NAME
)
5784 register_edge_assert_for_2 (op1
, e
, bsi
, rhs_code
, op0
, op1
, invert
);
5786 else if ((code
== NE_EXPR
5787 && gimple_assign_rhs_code (op_def
) == BIT_AND_EXPR
)
5789 && gimple_assign_rhs_code (op_def
) == BIT_IOR_EXPR
))
5791 /* Recurse on each operand. */
5792 tree op0
= gimple_assign_rhs1 (op_def
);
5793 tree op1
= gimple_assign_rhs2 (op_def
);
5794 if (TREE_CODE (op0
) == SSA_NAME
5795 && has_single_use (op0
))
5796 register_edge_assert_for_1 (op0
, code
, e
, bsi
);
5797 if (TREE_CODE (op1
) == SSA_NAME
5798 && has_single_use (op1
))
5799 register_edge_assert_for_1 (op1
, code
, e
, bsi
);
5801 else if (gimple_assign_rhs_code (op_def
) == BIT_NOT_EXPR
5802 && TYPE_PRECISION (TREE_TYPE (gimple_assign_lhs (op_def
))) == 1)
5804 /* Recurse, flipping CODE. */
5805 code
= invert_tree_comparison (code
, false);
5806 register_edge_assert_for_1 (gimple_assign_rhs1 (op_def
), code
, e
, bsi
);
5808 else if (gimple_assign_rhs_code (op_def
) == SSA_NAME
)
5810 /* Recurse through the copy. */
5811 register_edge_assert_for_1 (gimple_assign_rhs1 (op_def
), code
, e
, bsi
);
5813 else if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (op_def
)))
5815 /* Recurse through the type conversion, unless it is a narrowing
5816 conversion or conversion from non-integral type. */
5817 tree rhs
= gimple_assign_rhs1 (op_def
);
5818 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs
))
5819 && (TYPE_PRECISION (TREE_TYPE (rhs
))
5820 <= TYPE_PRECISION (TREE_TYPE (op
))))
5821 register_edge_assert_for_1 (rhs
, code
, e
, bsi
);
5825 /* Try to register an edge assertion for SSA name NAME on edge E for
5826 the condition COND contributing to the conditional jump pointed to by
5830 register_edge_assert_for (tree name
, edge e
, gimple_stmt_iterator si
,
5831 enum tree_code cond_code
, tree cond_op0
,
5835 enum tree_code comp_code
;
5836 bool is_else_edge
= (e
->flags
& EDGE_FALSE_VALUE
) != 0;
5838 /* Do not attempt to infer anything in names that flow through
5840 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name
))
5843 if (!extract_code_and_val_from_cond_with_ops (name
, cond_code
,
5849 /* Register ASSERT_EXPRs for name. */
5850 register_edge_assert_for_2 (name
, e
, si
, cond_code
, cond_op0
,
5851 cond_op1
, is_else_edge
);
5854 /* If COND is effectively an equality test of an SSA_NAME against
5855 the value zero or one, then we may be able to assert values
5856 for SSA_NAMEs which flow into COND. */
5858 /* In the case of NAME == 1 or NAME != 0, for BIT_AND_EXPR defining
5859 statement of NAME we can assert both operands of the BIT_AND_EXPR
5860 have nonzero value. */
5861 if (((comp_code
== EQ_EXPR
&& integer_onep (val
))
5862 || (comp_code
== NE_EXPR
&& integer_zerop (val
))))
5864 gimple
*def_stmt
= SSA_NAME_DEF_STMT (name
);
5866 if (is_gimple_assign (def_stmt
)
5867 && gimple_assign_rhs_code (def_stmt
) == BIT_AND_EXPR
)
5869 tree op0
= gimple_assign_rhs1 (def_stmt
);
5870 tree op1
= gimple_assign_rhs2 (def_stmt
);
5871 register_edge_assert_for_1 (op0
, NE_EXPR
, e
, si
);
5872 register_edge_assert_for_1 (op1
, NE_EXPR
, e
, si
);
5876 /* In the case of NAME == 0 or NAME != 1, for BIT_IOR_EXPR defining
5877 statement of NAME we can assert both operands of the BIT_IOR_EXPR
5879 if (((comp_code
== EQ_EXPR
&& integer_zerop (val
))
5880 || (comp_code
== NE_EXPR
&& integer_onep (val
))))
5882 gimple
*def_stmt
= SSA_NAME_DEF_STMT (name
);
5884 /* For BIT_IOR_EXPR only if NAME == 0 both operands have
5885 necessarily zero value, or if type-precision is one. */
5886 if (is_gimple_assign (def_stmt
)
5887 && (gimple_assign_rhs_code (def_stmt
) == BIT_IOR_EXPR
5888 && (TYPE_PRECISION (TREE_TYPE (name
)) == 1
5889 || comp_code
== EQ_EXPR
)))
5891 tree op0
= gimple_assign_rhs1 (def_stmt
);
5892 tree op1
= gimple_assign_rhs2 (def_stmt
);
5893 register_edge_assert_for_1 (op0
, EQ_EXPR
, e
, si
);
5894 register_edge_assert_for_1 (op1
, EQ_EXPR
, e
, si
);
5900 /* Determine whether the outgoing edges of BB should receive an
5901 ASSERT_EXPR for each of the operands of BB's LAST statement.
5902 The last statement of BB must be a COND_EXPR.
5904 If any of the sub-graphs rooted at BB have an interesting use of
5905 the predicate operands, an assert location node is added to the
5906 list of assertions for the corresponding operands. */
5909 find_conditional_asserts (basic_block bb
, gcond
*last
)
5911 gimple_stmt_iterator bsi
;
5917 bsi
= gsi_for_stmt (last
);
5919 /* Look for uses of the operands in each of the sub-graphs
5920 rooted at BB. We need to check each of the outgoing edges
5921 separately, so that we know what kind of ASSERT_EXPR to
5923 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
5928 /* Register the necessary assertions for each operand in the
5929 conditional predicate. */
5930 FOR_EACH_SSA_TREE_OPERAND (op
, last
, iter
, SSA_OP_USE
)
5931 register_edge_assert_for (op
, e
, bsi
,
5932 gimple_cond_code (last
),
5933 gimple_cond_lhs (last
),
5934 gimple_cond_rhs (last
));
5944 /* Compare two case labels sorting first by the destination bb index
5945 and then by the case value. */
5948 compare_case_labels (const void *p1
, const void *p2
)
5950 const struct case_info
*ci1
= (const struct case_info
*) p1
;
5951 const struct case_info
*ci2
= (const struct case_info
*) p2
;
5952 int idx1
= ci1
->bb
->index
;
5953 int idx2
= ci2
->bb
->index
;
5957 else if (idx1
== idx2
)
5959 /* Make sure the default label is first in a group. */
5960 if (!CASE_LOW (ci1
->expr
))
5962 else if (!CASE_LOW (ci2
->expr
))
5965 return tree_int_cst_compare (CASE_LOW (ci1
->expr
),
5966 CASE_LOW (ci2
->expr
));
5972 /* Determine whether the outgoing edges of BB should receive an
5973 ASSERT_EXPR for each of the operands of BB's LAST statement.
5974 The last statement of BB must be a SWITCH_EXPR.
5976 If any of the sub-graphs rooted at BB have an interesting use of
5977 the predicate operands, an assert location node is added to the
5978 list of assertions for the corresponding operands. */
5981 find_switch_asserts (basic_block bb
, gswitch
*last
)
5983 gimple_stmt_iterator bsi
;
5986 struct case_info
*ci
;
5987 size_t n
= gimple_switch_num_labels (last
);
5988 #if GCC_VERSION >= 4000
5991 /* Work around GCC 3.4 bug (PR 37086). */
5992 volatile unsigned int idx
;
5995 bsi
= gsi_for_stmt (last
);
5996 op
= gimple_switch_index (last
);
5997 if (TREE_CODE (op
) != SSA_NAME
)
6000 /* Build a vector of case labels sorted by destination label. */
6001 ci
= XNEWVEC (struct case_info
, n
);
6002 for (idx
= 0; idx
< n
; ++idx
)
6004 ci
[idx
].expr
= gimple_switch_label (last
, idx
);
6005 ci
[idx
].bb
= label_to_block (CASE_LABEL (ci
[idx
].expr
));
6007 qsort (ci
, n
, sizeof (struct case_info
), compare_case_labels
);
6009 for (idx
= 0; idx
< n
; ++idx
)
6012 tree cl
= ci
[idx
].expr
;
6013 basic_block cbb
= ci
[idx
].bb
;
6015 min
= CASE_LOW (cl
);
6016 max
= CASE_HIGH (cl
);
6018 /* If there are multiple case labels with the same destination
6019 we need to combine them to a single value range for the edge. */
6020 if (idx
+ 1 < n
&& cbb
== ci
[idx
+ 1].bb
)
6022 /* Skip labels until the last of the group. */
6025 } while (idx
< n
&& cbb
== ci
[idx
].bb
);
6028 /* Pick up the maximum of the case label range. */
6029 if (CASE_HIGH (ci
[idx
].expr
))
6030 max
= CASE_HIGH (ci
[idx
].expr
);
6032 max
= CASE_LOW (ci
[idx
].expr
);
6035 /* Nothing to do if the range includes the default label until we
6036 can register anti-ranges. */
6037 if (min
== NULL_TREE
)
6040 /* Find the edge to register the assert expr on. */
6041 e
= find_edge (bb
, cbb
);
6043 /* Register the necessary assertions for the operand in the
6045 register_edge_assert_for (op
, e
, bsi
,
6046 max
? GE_EXPR
: EQ_EXPR
,
6047 op
, fold_convert (TREE_TYPE (op
), min
));
6049 register_edge_assert_for (op
, e
, bsi
, LE_EXPR
, op
,
6050 fold_convert (TREE_TYPE (op
), max
));
6057 /* Traverse all the statements in block BB looking for statements that
6058 may generate useful assertions for the SSA names in their operand.
6059 If a statement produces a useful assertion A for name N_i, then the
6060 list of assertions already generated for N_i is scanned to
6061 determine if A is actually needed.
6063 If N_i already had the assertion A at a location dominating the
6064 current location, then nothing needs to be done. Otherwise, the
6065 new location for A is recorded instead.
6067 1- For every statement S in BB, all the variables used by S are
6068 added to bitmap FOUND_IN_SUBGRAPH.
6070 2- If statement S uses an operand N in a way that exposes a known
6071 value range for N, then if N was not already generated by an
6072 ASSERT_EXPR, create a new assert location for N. For instance,
6073 if N is a pointer and the statement dereferences it, we can
6074 assume that N is not NULL.
6076 3- COND_EXPRs are a special case of #2. We can derive range
6077 information from the predicate but need to insert different
6078 ASSERT_EXPRs for each of the sub-graphs rooted at the
6079 conditional block. If the last statement of BB is a conditional
6080 expression of the form 'X op Y', then
6082 a) Remove X and Y from the set FOUND_IN_SUBGRAPH.
6084 b) If the conditional is the only entry point to the sub-graph
6085 corresponding to the THEN_CLAUSE, recurse into it. On
6086 return, if X and/or Y are marked in FOUND_IN_SUBGRAPH, then
6087 an ASSERT_EXPR is added for the corresponding variable.
6089 c) Repeat step (b) on the ELSE_CLAUSE.
6091 d) Mark X and Y in FOUND_IN_SUBGRAPH.
6100 In this case, an assertion on the THEN clause is useful to
6101 determine that 'a' is always 9 on that edge. However, an assertion
6102 on the ELSE clause would be unnecessary.
6104 4- If BB does not end in a conditional expression, then we recurse
6105 into BB's dominator children.
6107 At the end of the recursive traversal, every SSA name will have a
6108 list of locations where ASSERT_EXPRs should be added. When a new
6109 location for name N is found, it is registered by calling
6110 register_new_assert_for. That function keeps track of all the
6111 registered assertions to prevent adding unnecessary assertions.
6112 For instance, if a pointer P_4 is dereferenced more than once in a
6113 dominator tree, only the location dominating all the dereference of
6114 P_4 will receive an ASSERT_EXPR. */
6117 find_assert_locations_1 (basic_block bb
, sbitmap live
)
6121 last
= last_stmt (bb
);
6123 /* If BB's last statement is a conditional statement involving integer
6124 operands, determine if we need to add ASSERT_EXPRs. */
6126 && gimple_code (last
) == GIMPLE_COND
6127 && !fp_predicate (last
)
6128 && !ZERO_SSA_OPERANDS (last
, SSA_OP_USE
))
6129 find_conditional_asserts (bb
, as_a
<gcond
*> (last
));
6131 /* If BB's last statement is a switch statement involving integer
6132 operands, determine if we need to add ASSERT_EXPRs. */
6134 && gimple_code (last
) == GIMPLE_SWITCH
6135 && !ZERO_SSA_OPERANDS (last
, SSA_OP_USE
))
6136 find_switch_asserts (bb
, as_a
<gswitch
*> (last
));
6138 /* Traverse all the statements in BB marking used names and looking
6139 for statements that may infer assertions for their used operands. */
6140 for (gimple_stmt_iterator si
= gsi_last_bb (bb
); !gsi_end_p (si
);
6147 stmt
= gsi_stmt (si
);
6149 if (is_gimple_debug (stmt
))
6152 /* See if we can derive an assertion for any of STMT's operands. */
6153 FOR_EACH_SSA_TREE_OPERAND (op
, stmt
, i
, SSA_OP_USE
)
6156 enum tree_code comp_code
;
6158 /* If op is not live beyond this stmt, do not bother to insert
6160 if (!bitmap_bit_p (live
, SSA_NAME_VERSION (op
)))
6163 /* If OP is used in such a way that we can infer a value
6164 range for it, and we don't find a previous assertion for
6165 it, create a new assertion location node for OP. */
6166 if (infer_value_range (stmt
, op
, &comp_code
, &value
))
6168 /* If we are able to infer a nonzero value range for OP,
6169 then walk backwards through the use-def chain to see if OP
6170 was set via a typecast.
6172 If so, then we can also infer a nonzero value range
6173 for the operand of the NOP_EXPR. */
6174 if (comp_code
== NE_EXPR
&& integer_zerop (value
))
6177 gimple
*def_stmt
= SSA_NAME_DEF_STMT (t
);
6179 while (is_gimple_assign (def_stmt
)
6180 && CONVERT_EXPR_CODE_P
6181 (gimple_assign_rhs_code (def_stmt
))
6183 (gimple_assign_rhs1 (def_stmt
)) == SSA_NAME
6185 (TREE_TYPE (gimple_assign_rhs1 (def_stmt
))))
6187 t
= gimple_assign_rhs1 (def_stmt
);
6188 def_stmt
= SSA_NAME_DEF_STMT (t
);
6190 /* Note we want to register the assert for the
6191 operand of the NOP_EXPR after SI, not after the
6193 if (! has_single_use (t
))
6194 register_new_assert_for (t
, t
, comp_code
, value
,
6199 register_new_assert_for (op
, op
, comp_code
, value
, bb
, NULL
, si
);
6204 FOR_EACH_SSA_TREE_OPERAND (op
, stmt
, i
, SSA_OP_USE
)
6205 bitmap_set_bit (live
, SSA_NAME_VERSION (op
));
6206 FOR_EACH_SSA_TREE_OPERAND (op
, stmt
, i
, SSA_OP_DEF
)
6207 bitmap_clear_bit (live
, SSA_NAME_VERSION (op
));
6210 /* Traverse all PHI nodes in BB, updating live. */
6211 for (gphi_iterator si
= gsi_start_phis (bb
); !gsi_end_p (si
);
6214 use_operand_p arg_p
;
6216 gphi
*phi
= si
.phi ();
6217 tree res
= gimple_phi_result (phi
);
6219 if (virtual_operand_p (res
))
6222 FOR_EACH_PHI_ARG (arg_p
, phi
, i
, SSA_OP_USE
)
6224 tree arg
= USE_FROM_PTR (arg_p
);
6225 if (TREE_CODE (arg
) == SSA_NAME
)
6226 bitmap_set_bit (live
, SSA_NAME_VERSION (arg
));
6229 bitmap_clear_bit (live
, SSA_NAME_VERSION (res
));
6233 /* Do an RPO walk over the function computing SSA name liveness
6234 on-the-fly and deciding on assert expressions to insert. */
6237 find_assert_locations (void)
6239 int *rpo
= XNEWVEC (int, last_basic_block_for_fn (cfun
));
6240 int *bb_rpo
= XNEWVEC (int, last_basic_block_for_fn (cfun
));
6241 int *last_rpo
= XCNEWVEC (int, last_basic_block_for_fn (cfun
));
6244 live
= XCNEWVEC (sbitmap
, last_basic_block_for_fn (cfun
));
6245 rpo_cnt
= pre_and_rev_post_order_compute (NULL
, rpo
, false);
6246 for (i
= 0; i
< rpo_cnt
; ++i
)
6249 /* Pre-seed loop latch liveness from loop header PHI nodes. Due to
6250 the order we compute liveness and insert asserts we otherwise
6251 fail to insert asserts into the loop latch. */
6253 FOR_EACH_LOOP (loop
, 0)
6255 i
= loop
->latch
->index
;
6256 unsigned int j
= single_succ_edge (loop
->latch
)->dest_idx
;
6257 for (gphi_iterator gsi
= gsi_start_phis (loop
->header
);
6258 !gsi_end_p (gsi
); gsi_next (&gsi
))
6260 gphi
*phi
= gsi
.phi ();
6261 if (virtual_operand_p (gimple_phi_result (phi
)))
6263 tree arg
= gimple_phi_arg_def (phi
, j
);
6264 if (TREE_CODE (arg
) == SSA_NAME
)
6266 if (live
[i
] == NULL
)
6268 live
[i
] = sbitmap_alloc (num_ssa_names
);
6269 bitmap_clear (live
[i
]);
6271 bitmap_set_bit (live
[i
], SSA_NAME_VERSION (arg
));
6276 for (i
= rpo_cnt
- 1; i
>= 0; --i
)
6278 basic_block bb
= BASIC_BLOCK_FOR_FN (cfun
, rpo
[i
]);
6284 live
[rpo
[i
]] = sbitmap_alloc (num_ssa_names
);
6285 bitmap_clear (live
[rpo
[i
]]);
6288 /* Process BB and update the live information with uses in
6290 find_assert_locations_1 (bb
, live
[rpo
[i
]]);
6292 /* Merge liveness into the predecessor blocks and free it. */
6293 if (!bitmap_empty_p (live
[rpo
[i
]]))
6296 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
6298 int pred
= e
->src
->index
;
6299 if ((e
->flags
& EDGE_DFS_BACK
) || pred
== ENTRY_BLOCK
)
6304 live
[pred
] = sbitmap_alloc (num_ssa_names
);
6305 bitmap_clear (live
[pred
]);
6307 bitmap_ior (live
[pred
], live
[pred
], live
[rpo
[i
]]);
6309 if (bb_rpo
[pred
] < pred_rpo
)
6310 pred_rpo
= bb_rpo
[pred
];
6313 /* Record the RPO number of the last visited block that needs
6314 live information from this block. */
6315 last_rpo
[rpo
[i
]] = pred_rpo
;
6319 sbitmap_free (live
[rpo
[i
]]);
6320 live
[rpo
[i
]] = NULL
;
6323 /* We can free all successors live bitmaps if all their
6324 predecessors have been visited already. */
6325 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
6326 if (last_rpo
[e
->dest
->index
] == i
6327 && live
[e
->dest
->index
])
6329 sbitmap_free (live
[e
->dest
->index
]);
6330 live
[e
->dest
->index
] = NULL
;
6335 XDELETEVEC (bb_rpo
);
6336 XDELETEVEC (last_rpo
);
6337 for (i
= 0; i
< last_basic_block_for_fn (cfun
); ++i
)
6339 sbitmap_free (live
[i
]);
6343 /* Create an ASSERT_EXPR for NAME and insert it in the location
6344 indicated by LOC. Return true if we made any edge insertions. */
6347 process_assert_insertions_for (tree name
, assert_locus
*loc
)
6349 /* Build the comparison expression NAME_i COMP_CODE VAL. */
6352 gimple
*assert_stmt
;
6356 /* If we have X <=> X do not insert an assert expr for that. */
6357 if (loc
->expr
== loc
->val
)
6360 cond
= build2 (loc
->comp_code
, boolean_type_node
, loc
->expr
, loc
->val
);
6361 assert_stmt
= build_assert_expr_for (cond
, name
);
6364 /* We have been asked to insert the assertion on an edge. This
6365 is used only by COND_EXPR and SWITCH_EXPR assertions. */
6366 gcc_checking_assert (gimple_code (gsi_stmt (loc
->si
)) == GIMPLE_COND
6367 || (gimple_code (gsi_stmt (loc
->si
))
6370 gsi_insert_on_edge (loc
->e
, assert_stmt
);
6374 /* Otherwise, we can insert right after LOC->SI iff the
6375 statement must not be the last statement in the block. */
6376 stmt
= gsi_stmt (loc
->si
);
6377 if (!stmt_ends_bb_p (stmt
))
6379 gsi_insert_after (&loc
->si
, assert_stmt
, GSI_SAME_STMT
);
6383 /* If STMT must be the last statement in BB, we can only insert new
6384 assertions on the non-abnormal edge out of BB. Note that since
6385 STMT is not control flow, there may only be one non-abnormal edge
6387 FOR_EACH_EDGE (e
, ei
, loc
->bb
->succs
)
6388 if (!(e
->flags
& EDGE_ABNORMAL
))
6390 gsi_insert_on_edge (e
, assert_stmt
);
6398 /* Process all the insertions registered for every name N_i registered
6399 in NEED_ASSERT_FOR. The list of assertions to be inserted are
6400 found in ASSERTS_FOR[i]. */
6403 process_assert_insertions (void)
6407 bool update_edges_p
= false;
6408 int num_asserts
= 0;
6410 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6411 dump_all_asserts (dump_file
);
6413 EXECUTE_IF_SET_IN_BITMAP (need_assert_for
, 0, i
, bi
)
6415 assert_locus
*loc
= asserts_for
[i
];
6420 assert_locus
*next
= loc
->next
;
6421 update_edges_p
|= process_assert_insertions_for (ssa_name (i
), loc
);
6429 gsi_commit_edge_inserts ();
6431 statistics_counter_event (cfun
, "Number of ASSERT_EXPR expressions inserted",
6436 /* Traverse the flowgraph looking for conditional jumps to insert range
6437 expressions. These range expressions are meant to provide information
6438 to optimizations that need to reason in terms of value ranges. They
6439 will not be expanded into RTL. For instance, given:
6448 this pass will transform the code into:
6454 x = ASSERT_EXPR <x, x < y>
6459 y = ASSERT_EXPR <y, x >= y>
6463 The idea is that once copy and constant propagation have run, other
6464 optimizations will be able to determine what ranges of values can 'x'
6465 take in different paths of the code, simply by checking the reaching
6466 definition of 'x'. */
6469 insert_range_assertions (void)
6471 need_assert_for
= BITMAP_ALLOC (NULL
);
6472 asserts_for
= XCNEWVEC (assert_locus
*, num_ssa_names
);
6474 calculate_dominance_info (CDI_DOMINATORS
);
6476 find_assert_locations ();
6477 if (!bitmap_empty_p (need_assert_for
))
6479 process_assert_insertions ();
6480 update_ssa (TODO_update_ssa_no_phi
);
6483 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6485 fprintf (dump_file
, "\nSSA form after inserting ASSERT_EXPRs\n");
6486 dump_function_to_file (current_function_decl
, dump_file
, dump_flags
);
6490 BITMAP_FREE (need_assert_for
);
6493 /* Checks one ARRAY_REF in REF, located at LOCUS. Ignores flexible arrays
6494 and "struct" hacks. If VRP can determine that the
6495 array subscript is a constant, check if it is outside valid
6496 range. If the array subscript is a RANGE, warn if it is
6497 non-overlapping with valid range.
6498 IGNORE_OFF_BY_ONE is true if the ARRAY_REF is inside a ADDR_EXPR. */
6501 check_array_ref (location_t location
, tree ref
, bool ignore_off_by_one
)
6503 value_range
*vr
= NULL
;
6504 tree low_sub
, up_sub
;
6505 tree low_bound
, up_bound
, up_bound_p1
;
6507 if (TREE_NO_WARNING (ref
))
6510 low_sub
= up_sub
= TREE_OPERAND (ref
, 1);
6511 up_bound
= array_ref_up_bound (ref
);
6513 /* Can not check flexible arrays. */
6515 || TREE_CODE (up_bound
) != INTEGER_CST
)
6518 /* Accesses to trailing arrays via pointers may access storage
6519 beyond the types array bounds. */
6520 if (warn_array_bounds
< 2
6521 && array_at_struct_end_p (ref
))
6524 low_bound
= array_ref_low_bound (ref
);
6525 up_bound_p1
= int_const_binop (PLUS_EXPR
, up_bound
,
6526 build_int_cst (TREE_TYPE (up_bound
), 1));
6529 if (tree_int_cst_equal (low_bound
, up_bound_p1
))
6531 warning_at (location
, OPT_Warray_bounds
,
6532 "array subscript is above array bounds");
6533 TREE_NO_WARNING (ref
) = 1;
6536 if (TREE_CODE (low_sub
) == SSA_NAME
)
6538 vr
= get_value_range (low_sub
);
6539 if (vr
->type
== VR_RANGE
|| vr
->type
== VR_ANTI_RANGE
)
6541 low_sub
= vr
->type
== VR_RANGE
? vr
->max
: vr
->min
;
6542 up_sub
= vr
->type
== VR_RANGE
? vr
->min
: vr
->max
;
6546 if (vr
&& vr
->type
== VR_ANTI_RANGE
)
6548 if (TREE_CODE (up_sub
) == INTEGER_CST
6549 && (ignore_off_by_one
6550 ? tree_int_cst_lt (up_bound
, up_sub
)
6551 : tree_int_cst_le (up_bound
, up_sub
))
6552 && TREE_CODE (low_sub
) == INTEGER_CST
6553 && tree_int_cst_le (low_sub
, low_bound
))
6555 warning_at (location
, OPT_Warray_bounds
,
6556 "array subscript is outside array bounds");
6557 TREE_NO_WARNING (ref
) = 1;
6560 else if (TREE_CODE (up_sub
) == INTEGER_CST
6561 && (ignore_off_by_one
6562 ? !tree_int_cst_le (up_sub
, up_bound_p1
)
6563 : !tree_int_cst_le (up_sub
, up_bound
)))
6565 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6567 fprintf (dump_file
, "Array bound warning for ");
6568 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, ref
);
6569 fprintf (dump_file
, "\n");
6571 warning_at (location
, OPT_Warray_bounds
,
6572 "array subscript is above array bounds");
6573 TREE_NO_WARNING (ref
) = 1;
6575 else if (TREE_CODE (low_sub
) == INTEGER_CST
6576 && tree_int_cst_lt (low_sub
, low_bound
))
6578 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6580 fprintf (dump_file
, "Array bound warning for ");
6581 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, ref
);
6582 fprintf (dump_file
, "\n");
6584 warning_at (location
, OPT_Warray_bounds
,
6585 "array subscript is below array bounds");
6586 TREE_NO_WARNING (ref
) = 1;
6590 /* Searches if the expr T, located at LOCATION computes
6591 address of an ARRAY_REF, and call check_array_ref on it. */
6594 search_for_addr_array (tree t
, location_t location
)
6596 /* Check each ARRAY_REFs in the reference chain. */
6599 if (TREE_CODE (t
) == ARRAY_REF
)
6600 check_array_ref (location
, t
, true /*ignore_off_by_one*/);
6602 t
= TREE_OPERAND (t
, 0);
6604 while (handled_component_p (t
));
6606 if (TREE_CODE (t
) == MEM_REF
6607 && TREE_CODE (TREE_OPERAND (t
, 0)) == ADDR_EXPR
6608 && !TREE_NO_WARNING (t
))
6610 tree tem
= TREE_OPERAND (TREE_OPERAND (t
, 0), 0);
6611 tree low_bound
, up_bound
, el_sz
;
6613 if (TREE_CODE (TREE_TYPE (tem
)) != ARRAY_TYPE
6614 || TREE_CODE (TREE_TYPE (TREE_TYPE (tem
))) == ARRAY_TYPE
6615 || !TYPE_DOMAIN (TREE_TYPE (tem
)))
6618 low_bound
= TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (tem
)));
6619 up_bound
= TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (tem
)));
6620 el_sz
= TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (tem
)));
6622 || TREE_CODE (low_bound
) != INTEGER_CST
6624 || TREE_CODE (up_bound
) != INTEGER_CST
6626 || TREE_CODE (el_sz
) != INTEGER_CST
)
6629 idx
= mem_ref_offset (t
);
6630 idx
= wi::sdiv_trunc (idx
, wi::to_offset (el_sz
));
6631 if (wi::lts_p (idx
, 0))
6633 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6635 fprintf (dump_file
, "Array bound warning for ");
6636 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, t
);
6637 fprintf (dump_file
, "\n");
6639 warning_at (location
, OPT_Warray_bounds
,
6640 "array subscript is below array bounds");
6641 TREE_NO_WARNING (t
) = 1;
6643 else if (wi::gts_p (idx
, (wi::to_offset (up_bound
)
6644 - wi::to_offset (low_bound
) + 1)))
6646 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6648 fprintf (dump_file
, "Array bound warning for ");
6649 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, t
);
6650 fprintf (dump_file
, "\n");
6652 warning_at (location
, OPT_Warray_bounds
,
6653 "array subscript is above array bounds");
6654 TREE_NO_WARNING (t
) = 1;
6659 /* walk_tree() callback that checks if *TP is
6660 an ARRAY_REF inside an ADDR_EXPR (in which an array
6661 subscript one outside the valid range is allowed). Call
6662 check_array_ref for each ARRAY_REF found. The location is
6666 check_array_bounds (tree
*tp
, int *walk_subtree
, void *data
)
6669 struct walk_stmt_info
*wi
= (struct walk_stmt_info
*) data
;
6670 location_t location
;
6672 if (EXPR_HAS_LOCATION (t
))
6673 location
= EXPR_LOCATION (t
);
6676 location_t
*locp
= (location_t
*) wi
->info
;
6680 *walk_subtree
= TRUE
;
6682 if (TREE_CODE (t
) == ARRAY_REF
)
6683 check_array_ref (location
, t
, false /*ignore_off_by_one*/);
6685 else if (TREE_CODE (t
) == ADDR_EXPR
)
6687 search_for_addr_array (t
, location
);
6688 *walk_subtree
= FALSE
;
6694 /* Walk over all statements of all reachable BBs and call check_array_bounds
6698 check_all_array_refs (void)
6701 gimple_stmt_iterator si
;
6703 FOR_EACH_BB_FN (bb
, cfun
)
6707 bool executable
= false;
6709 /* Skip blocks that were found to be unreachable. */
6710 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
6711 executable
|= !!(e
->flags
& EDGE_EXECUTABLE
);
6715 for (si
= gsi_start_bb (bb
); !gsi_end_p (si
); gsi_next (&si
))
6717 gimple
*stmt
= gsi_stmt (si
);
6718 struct walk_stmt_info wi
;
6719 if (!gimple_has_location (stmt
)
6720 || is_gimple_debug (stmt
))
6723 memset (&wi
, 0, sizeof (wi
));
6725 location_t loc
= gimple_location (stmt
);
6728 walk_gimple_op (gsi_stmt (si
),
6735 /* Return true if all imm uses of VAR are either in STMT, or
6736 feed (optionally through a chain of single imm uses) GIMPLE_COND
6737 in basic block COND_BB. */
6740 all_imm_uses_in_stmt_or_feed_cond (tree var
, gimple
*stmt
, basic_block cond_bb
)
6742 use_operand_p use_p
, use2_p
;
6743 imm_use_iterator iter
;
6745 FOR_EACH_IMM_USE_FAST (use_p
, iter
, var
)
6746 if (USE_STMT (use_p
) != stmt
)
6748 gimple
*use_stmt
= USE_STMT (use_p
), *use_stmt2
;
6749 if (is_gimple_debug (use_stmt
))
6751 while (is_gimple_assign (use_stmt
)
6752 && TREE_CODE (gimple_assign_lhs (use_stmt
)) == SSA_NAME
6753 && single_imm_use (gimple_assign_lhs (use_stmt
),
6754 &use2_p
, &use_stmt2
))
6755 use_stmt
= use_stmt2
;
6756 if (gimple_code (use_stmt
) != GIMPLE_COND
6757 || gimple_bb (use_stmt
) != cond_bb
)
6770 __builtin_unreachable ();
6772 x_5 = ASSERT_EXPR <x_3, ...>;
6773 If x_3 has no other immediate uses (checked by caller),
6774 var is the x_3 var from ASSERT_EXPR, we can clear low 5 bits
6775 from the non-zero bitmask. */
6778 maybe_set_nonzero_bits (basic_block bb
, tree var
)
6780 edge e
= single_pred_edge (bb
);
6781 basic_block cond_bb
= e
->src
;
6782 gimple
*stmt
= last_stmt (cond_bb
);
6786 || gimple_code (stmt
) != GIMPLE_COND
6787 || gimple_cond_code (stmt
) != ((e
->flags
& EDGE_TRUE_VALUE
)
6788 ? EQ_EXPR
: NE_EXPR
)
6789 || TREE_CODE (gimple_cond_lhs (stmt
)) != SSA_NAME
6790 || !integer_zerop (gimple_cond_rhs (stmt
)))
6793 stmt
= SSA_NAME_DEF_STMT (gimple_cond_lhs (stmt
));
6794 if (!is_gimple_assign (stmt
)
6795 || gimple_assign_rhs_code (stmt
) != BIT_AND_EXPR
6796 || TREE_CODE (gimple_assign_rhs2 (stmt
)) != INTEGER_CST
)
6798 if (gimple_assign_rhs1 (stmt
) != var
)
6802 if (TREE_CODE (gimple_assign_rhs1 (stmt
)) != SSA_NAME
)
6804 stmt2
= SSA_NAME_DEF_STMT (gimple_assign_rhs1 (stmt
));
6805 if (!gimple_assign_cast_p (stmt2
)
6806 || gimple_assign_rhs1 (stmt2
) != var
6807 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (stmt2
))
6808 || (TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (stmt
)))
6809 != TYPE_PRECISION (TREE_TYPE (var
))))
6812 cst
= gimple_assign_rhs2 (stmt
);
6813 set_nonzero_bits (var
, wi::bit_and_not (get_nonzero_bits (var
), cst
));
6816 /* Convert range assertion expressions into the implied copies and
6817 copy propagate away the copies. Doing the trivial copy propagation
6818 here avoids the need to run the full copy propagation pass after
6821 FIXME, this will eventually lead to copy propagation removing the
6822 names that had useful range information attached to them. For
6823 instance, if we had the assertion N_i = ASSERT_EXPR <N_j, N_j > 3>,
6824 then N_i will have the range [3, +INF].
6826 However, by converting the assertion into the implied copy
6827 operation N_i = N_j, we will then copy-propagate N_j into the uses
6828 of N_i and lose the range information. We may want to hold on to
6829 ASSERT_EXPRs a little while longer as the ranges could be used in
6830 things like jump threading.
6832 The problem with keeping ASSERT_EXPRs around is that passes after
6833 VRP need to handle them appropriately.
6835 Another approach would be to make the range information a first
6836 class property of the SSA_NAME so that it can be queried from
6837 any pass. This is made somewhat more complex by the need for
6838 multiple ranges to be associated with one SSA_NAME. */
6841 remove_range_assertions (void)
6844 gimple_stmt_iterator si
;
6845 /* 1 if looking at ASSERT_EXPRs immediately at the beginning of
6846 a basic block preceeded by GIMPLE_COND branching to it and
6847 __builtin_trap, -1 if not yet checked, 0 otherwise. */
6850 /* Note that the BSI iterator bump happens at the bottom of the
6851 loop and no bump is necessary if we're removing the statement
6852 referenced by the current BSI. */
6853 FOR_EACH_BB_FN (bb
, cfun
)
6854 for (si
= gsi_after_labels (bb
), is_unreachable
= -1; !gsi_end_p (si
);)
6856 gimple
*stmt
= gsi_stmt (si
);
6859 if (is_gimple_assign (stmt
)
6860 && gimple_assign_rhs_code (stmt
) == ASSERT_EXPR
)
6862 tree lhs
= gimple_assign_lhs (stmt
);
6863 tree rhs
= gimple_assign_rhs1 (stmt
);
6865 use_operand_p use_p
;
6866 imm_use_iterator iter
;
6868 var
= ASSERT_EXPR_VAR (rhs
);
6869 gcc_assert (TREE_CODE (var
) == SSA_NAME
);
6871 if (!POINTER_TYPE_P (TREE_TYPE (lhs
))
6872 && SSA_NAME_RANGE_INFO (lhs
))
6874 if (is_unreachable
== -1)
6877 if (single_pred_p (bb
)
6878 && assert_unreachable_fallthru_edge_p
6879 (single_pred_edge (bb
)))
6883 if (x_7 >= 10 && x_7 < 20)
6884 __builtin_unreachable ();
6885 x_8 = ASSERT_EXPR <x_7, ...>;
6886 if the only uses of x_7 are in the ASSERT_EXPR and
6887 in the condition. In that case, we can copy the
6888 range info from x_8 computed in this pass also
6891 && all_imm_uses_in_stmt_or_feed_cond (var
, stmt
,
6894 set_range_info (var
, SSA_NAME_RANGE_TYPE (lhs
),
6895 SSA_NAME_RANGE_INFO (lhs
)->get_min (),
6896 SSA_NAME_RANGE_INFO (lhs
)->get_max ());
6897 maybe_set_nonzero_bits (bb
, var
);
6901 /* Propagate the RHS into every use of the LHS. */
6902 FOR_EACH_IMM_USE_STMT (use_stmt
, iter
, lhs
)
6903 FOR_EACH_IMM_USE_ON_STMT (use_p
, iter
)
6904 SET_USE (use_p
, var
);
6906 /* And finally, remove the copy, it is not needed. */
6907 gsi_remove (&si
, true);
6908 release_defs (stmt
);
6912 if (!is_gimple_debug (gsi_stmt (si
)))
6920 /* Return true if STMT is interesting for VRP. */
6923 stmt_interesting_for_vrp (gimple
*stmt
)
6925 if (gimple_code (stmt
) == GIMPLE_PHI
)
6927 tree res
= gimple_phi_result (stmt
);
6928 return (!virtual_operand_p (res
)
6929 && (INTEGRAL_TYPE_P (TREE_TYPE (res
))
6930 || POINTER_TYPE_P (TREE_TYPE (res
))));
6932 else if (is_gimple_assign (stmt
) || is_gimple_call (stmt
))
6934 tree lhs
= gimple_get_lhs (stmt
);
6936 /* In general, assignments with virtual operands are not useful
6937 for deriving ranges, with the obvious exception of calls to
6938 builtin functions. */
6939 if (lhs
&& TREE_CODE (lhs
) == SSA_NAME
6940 && (INTEGRAL_TYPE_P (TREE_TYPE (lhs
))
6941 || POINTER_TYPE_P (TREE_TYPE (lhs
)))
6942 && (is_gimple_call (stmt
)
6943 || !gimple_vuse (stmt
)))
6945 else if (is_gimple_call (stmt
) && gimple_call_internal_p (stmt
))
6946 switch (gimple_call_internal_fn (stmt
))
6948 case IFN_ADD_OVERFLOW
:
6949 case IFN_SUB_OVERFLOW
:
6950 case IFN_MUL_OVERFLOW
:
6951 /* These internal calls return _Complex integer type,
6952 but are interesting to VRP nevertheless. */
6953 if (lhs
&& TREE_CODE (lhs
) == SSA_NAME
)
6960 else if (gimple_code (stmt
) == GIMPLE_COND
6961 || gimple_code (stmt
) == GIMPLE_SWITCH
)
6968 /* Initialize local data structures for VRP. */
6971 vrp_initialize (void)
6975 values_propagated
= false;
6976 num_vr_values
= num_ssa_names
;
6977 vr_value
= XCNEWVEC (value_range
*, num_vr_values
);
6978 vr_phi_edge_counts
= XCNEWVEC (int, num_ssa_names
);
6980 FOR_EACH_BB_FN (bb
, cfun
)
6982 for (gphi_iterator si
= gsi_start_phis (bb
); !gsi_end_p (si
);
6985 gphi
*phi
= si
.phi ();
6986 if (!stmt_interesting_for_vrp (phi
))
6988 tree lhs
= PHI_RESULT (phi
);
6989 set_value_range_to_varying (get_value_range (lhs
));
6990 prop_set_simulate_again (phi
, false);
6993 prop_set_simulate_again (phi
, true);
6996 for (gimple_stmt_iterator si
= gsi_start_bb (bb
); !gsi_end_p (si
);
6999 gimple
*stmt
= gsi_stmt (si
);
7001 /* If the statement is a control insn, then we do not
7002 want to avoid simulating the statement once. Failure
7003 to do so means that those edges will never get added. */
7004 if (stmt_ends_bb_p (stmt
))
7005 prop_set_simulate_again (stmt
, true);
7006 else if (!stmt_interesting_for_vrp (stmt
))
7008 set_defs_to_varying (stmt
);
7009 prop_set_simulate_again (stmt
, false);
7012 prop_set_simulate_again (stmt
, true);
7017 /* Return the singleton value-range for NAME or NAME. */
7020 vrp_valueize (tree name
)
7022 if (TREE_CODE (name
) == SSA_NAME
)
7024 value_range
*vr
= get_value_range (name
);
7025 if (vr
->type
== VR_RANGE
7026 && vrp_operand_equal_p (vr
->min
, vr
->max
))
7032 /* Return the singleton value-range for NAME if that is a constant
7033 but signal to not follow SSA edges. */
7036 vrp_valueize_1 (tree name
)
7038 if (TREE_CODE (name
) == SSA_NAME
)
7040 /* If the definition may be simulated again we cannot follow
7041 this SSA edge as the SSA propagator does not necessarily
7042 re-visit the use. */
7043 gimple
*def_stmt
= SSA_NAME_DEF_STMT (name
);
7044 if (!gimple_nop_p (def_stmt
)
7045 && prop_simulate_again_p (def_stmt
))
7047 value_range
*vr
= get_value_range (name
);
7048 if (range_int_cst_singleton_p (vr
))
7054 /* Visit assignment STMT. If it produces an interesting range, record
7055 the SSA name in *OUTPUT_P. */
7057 static enum ssa_prop_result
7058 vrp_visit_assignment_or_call (gimple
*stmt
, tree
*output_p
)
7062 enum gimple_code code
= gimple_code (stmt
);
7063 lhs
= gimple_get_lhs (stmt
);
7065 /* We only keep track of ranges in integral and pointer types. */
7066 if (TREE_CODE (lhs
) == SSA_NAME
7067 && ((INTEGRAL_TYPE_P (TREE_TYPE (lhs
))
7068 /* It is valid to have NULL MIN/MAX values on a type. See
7069 build_range_type. */
7070 && TYPE_MIN_VALUE (TREE_TYPE (lhs
))
7071 && TYPE_MAX_VALUE (TREE_TYPE (lhs
)))
7072 || POINTER_TYPE_P (TREE_TYPE (lhs
))))
7074 value_range new_vr
= VR_INITIALIZER
;
7076 /* Try folding the statement to a constant first. */
7077 tree tem
= gimple_fold_stmt_to_constant_1 (stmt
, vrp_valueize
,
7079 if (tem
&& is_gimple_min_invariant (tem
))
7080 set_value_range_to_value (&new_vr
, tem
, NULL
);
7081 /* Then dispatch to value-range extracting functions. */
7082 else if (code
== GIMPLE_CALL
)
7083 extract_range_basic (&new_vr
, stmt
);
7085 extract_range_from_assignment (&new_vr
, as_a
<gassign
*> (stmt
));
7087 if (update_value_range (lhs
, &new_vr
))
7091 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7093 fprintf (dump_file
, "Found new range for ");
7094 print_generic_expr (dump_file
, lhs
, 0);
7095 fprintf (dump_file
, ": ");
7096 dump_value_range (dump_file
, &new_vr
);
7097 fprintf (dump_file
, "\n");
7100 if (new_vr
.type
== VR_VARYING
)
7101 return SSA_PROP_VARYING
;
7103 return SSA_PROP_INTERESTING
;
7106 return SSA_PROP_NOT_INTERESTING
;
7108 else if (is_gimple_call (stmt
) && gimple_call_internal_p (stmt
))
7109 switch (gimple_call_internal_fn (stmt
))
7111 case IFN_ADD_OVERFLOW
:
7112 case IFN_SUB_OVERFLOW
:
7113 case IFN_MUL_OVERFLOW
:
7114 /* These internal calls return _Complex integer type,
7115 which VRP does not track, but the immediate uses
7116 thereof might be interesting. */
7117 if (lhs
&& TREE_CODE (lhs
) == SSA_NAME
)
7119 imm_use_iterator iter
;
7120 use_operand_p use_p
;
7121 enum ssa_prop_result res
= SSA_PROP_VARYING
;
7123 set_value_range_to_varying (get_value_range (lhs
));
7125 FOR_EACH_IMM_USE_FAST (use_p
, iter
, lhs
)
7127 gimple
*use_stmt
= USE_STMT (use_p
);
7128 if (!is_gimple_assign (use_stmt
))
7130 enum tree_code rhs_code
= gimple_assign_rhs_code (use_stmt
);
7131 if (rhs_code
!= REALPART_EXPR
&& rhs_code
!= IMAGPART_EXPR
)
7133 tree rhs1
= gimple_assign_rhs1 (use_stmt
);
7134 tree use_lhs
= gimple_assign_lhs (use_stmt
);
7135 if (TREE_CODE (rhs1
) != rhs_code
7136 || TREE_OPERAND (rhs1
, 0) != lhs
7137 || TREE_CODE (use_lhs
) != SSA_NAME
7138 || !stmt_interesting_for_vrp (use_stmt
)
7139 || (!INTEGRAL_TYPE_P (TREE_TYPE (use_lhs
))
7140 || !TYPE_MIN_VALUE (TREE_TYPE (use_lhs
))
7141 || !TYPE_MAX_VALUE (TREE_TYPE (use_lhs
))))
7144 /* If there is a change in the value range for any of the
7145 REALPART_EXPR/IMAGPART_EXPR immediate uses, return
7146 SSA_PROP_INTERESTING. If there are any REALPART_EXPR
7147 or IMAGPART_EXPR immediate uses, but none of them have
7148 a change in their value ranges, return
7149 SSA_PROP_NOT_INTERESTING. If there are no
7150 {REAL,IMAG}PART_EXPR uses at all,
7151 return SSA_PROP_VARYING. */
7152 value_range new_vr
= VR_INITIALIZER
;
7153 extract_range_basic (&new_vr
, use_stmt
);
7154 value_range
*old_vr
= get_value_range (use_lhs
);
7155 if (old_vr
->type
!= new_vr
.type
7156 || !vrp_operand_equal_p (old_vr
->min
, new_vr
.min
)
7157 || !vrp_operand_equal_p (old_vr
->max
, new_vr
.max
)
7158 || !vrp_bitmap_equal_p (old_vr
->equiv
, new_vr
.equiv
))
7159 res
= SSA_PROP_INTERESTING
;
7161 res
= SSA_PROP_NOT_INTERESTING
;
7162 BITMAP_FREE (new_vr
.equiv
);
7163 if (res
== SSA_PROP_INTERESTING
)
7177 /* Every other statement produces no useful ranges. */
7178 FOR_EACH_SSA_TREE_OPERAND (def
, stmt
, iter
, SSA_OP_DEF
)
7179 set_value_range_to_varying (get_value_range (def
));
7181 return SSA_PROP_VARYING
;
7184 /* Helper that gets the value range of the SSA_NAME with version I
7185 or a symbolic range containing the SSA_NAME only if the value range
7186 is varying or undefined. */
7188 static inline value_range
7189 get_vr_for_comparison (int i
)
7191 value_range vr
= *get_value_range (ssa_name (i
));
7193 /* If name N_i does not have a valid range, use N_i as its own
7194 range. This allows us to compare against names that may
7195 have N_i in their ranges. */
7196 if (vr
.type
== VR_VARYING
|| vr
.type
== VR_UNDEFINED
)
7199 vr
.min
= ssa_name (i
);
7200 vr
.max
= ssa_name (i
);
7206 /* Compare all the value ranges for names equivalent to VAR with VAL
7207 using comparison code COMP. Return the same value returned by
7208 compare_range_with_value, including the setting of
7209 *STRICT_OVERFLOW_P. */
7212 compare_name_with_value (enum tree_code comp
, tree var
, tree val
,
7213 bool *strict_overflow_p
, bool use_equiv_p
)
7219 int used_strict_overflow
;
7221 value_range equiv_vr
;
7223 /* Get the set of equivalences for VAR. */
7224 e
= get_value_range (var
)->equiv
;
7226 /* Start at -1. Set it to 0 if we do a comparison without relying
7227 on overflow, or 1 if all comparisons rely on overflow. */
7228 used_strict_overflow
= -1;
7230 /* Compare vars' value range with val. */
7231 equiv_vr
= get_vr_for_comparison (SSA_NAME_VERSION (var
));
7233 retval
= compare_range_with_value (comp
, &equiv_vr
, val
, &sop
);
7235 used_strict_overflow
= sop
? 1 : 0;
7237 /* If the equiv set is empty we have done all work we need to do. */
7241 && used_strict_overflow
> 0)
7242 *strict_overflow_p
= true;
7246 EXECUTE_IF_SET_IN_BITMAP (e
, 0, i
, bi
)
7249 && ! SSA_NAME_IS_DEFAULT_DEF (ssa_name (i
))
7250 && prop_simulate_again_p (SSA_NAME_DEF_STMT (ssa_name (i
))))
7253 equiv_vr
= get_vr_for_comparison (i
);
7255 t
= compare_range_with_value (comp
, &equiv_vr
, val
, &sop
);
7258 /* If we get different answers from different members
7259 of the equivalence set this check must be in a dead
7260 code region. Folding it to a trap representation
7261 would be correct here. For now just return don't-know. */
7271 used_strict_overflow
= 0;
7272 else if (used_strict_overflow
< 0)
7273 used_strict_overflow
= 1;
7278 && used_strict_overflow
> 0)
7279 *strict_overflow_p
= true;
7285 /* Given a comparison code COMP and names N1 and N2, compare all the
7286 ranges equivalent to N1 against all the ranges equivalent to N2
7287 to determine the value of N1 COMP N2. Return the same value
7288 returned by compare_ranges. Set *STRICT_OVERFLOW_P to indicate
7289 whether we relied on an overflow infinity in the comparison. */
7293 compare_names (enum tree_code comp
, tree n1
, tree n2
,
7294 bool *strict_overflow_p
)
7298 bitmap_iterator bi1
, bi2
;
7300 int used_strict_overflow
;
7301 static bitmap_obstack
*s_obstack
= NULL
;
7302 static bitmap s_e1
= NULL
, s_e2
= NULL
;
7304 /* Compare the ranges of every name equivalent to N1 against the
7305 ranges of every name equivalent to N2. */
7306 e1
= get_value_range (n1
)->equiv
;
7307 e2
= get_value_range (n2
)->equiv
;
7309 /* Use the fake bitmaps if e1 or e2 are not available. */
7310 if (s_obstack
== NULL
)
7312 s_obstack
= XNEW (bitmap_obstack
);
7313 bitmap_obstack_initialize (s_obstack
);
7314 s_e1
= BITMAP_ALLOC (s_obstack
);
7315 s_e2
= BITMAP_ALLOC (s_obstack
);
7322 /* Add N1 and N2 to their own set of equivalences to avoid
7323 duplicating the body of the loop just to check N1 and N2
7325 bitmap_set_bit (e1
, SSA_NAME_VERSION (n1
));
7326 bitmap_set_bit (e2
, SSA_NAME_VERSION (n2
));
7328 /* If the equivalence sets have a common intersection, then the two
7329 names can be compared without checking their ranges. */
7330 if (bitmap_intersect_p (e1
, e2
))
7332 bitmap_clear_bit (e1
, SSA_NAME_VERSION (n1
));
7333 bitmap_clear_bit (e2
, SSA_NAME_VERSION (n2
));
7335 return (comp
== EQ_EXPR
|| comp
== GE_EXPR
|| comp
== LE_EXPR
)
7337 : boolean_false_node
;
7340 /* Start at -1. Set it to 0 if we do a comparison without relying
7341 on overflow, or 1 if all comparisons rely on overflow. */
7342 used_strict_overflow
= -1;
7344 /* Otherwise, compare all the equivalent ranges. First, add N1 and
7345 N2 to their own set of equivalences to avoid duplicating the body
7346 of the loop just to check N1 and N2 ranges. */
7347 EXECUTE_IF_SET_IN_BITMAP (e1
, 0, i1
, bi1
)
7349 value_range vr1
= get_vr_for_comparison (i1
);
7351 t
= retval
= NULL_TREE
;
7352 EXECUTE_IF_SET_IN_BITMAP (e2
, 0, i2
, bi2
)
7356 value_range vr2
= get_vr_for_comparison (i2
);
7358 t
= compare_ranges (comp
, &vr1
, &vr2
, &sop
);
7361 /* If we get different answers from different members
7362 of the equivalence set this check must be in a dead
7363 code region. Folding it to a trap representation
7364 would be correct here. For now just return don't-know. */
7368 bitmap_clear_bit (e1
, SSA_NAME_VERSION (n1
));
7369 bitmap_clear_bit (e2
, SSA_NAME_VERSION (n2
));
7375 used_strict_overflow
= 0;
7376 else if (used_strict_overflow
< 0)
7377 used_strict_overflow
= 1;
7383 bitmap_clear_bit (e1
, SSA_NAME_VERSION (n1
));
7384 bitmap_clear_bit (e2
, SSA_NAME_VERSION (n2
));
7385 if (used_strict_overflow
> 0)
7386 *strict_overflow_p
= true;
7391 /* None of the equivalent ranges are useful in computing this
7393 bitmap_clear_bit (e1
, SSA_NAME_VERSION (n1
));
7394 bitmap_clear_bit (e2
, SSA_NAME_VERSION (n2
));
7398 /* Helper function for vrp_evaluate_conditional_warnv & other
7402 vrp_evaluate_conditional_warnv_with_ops_using_ranges (enum tree_code code
,
7404 bool * strict_overflow_p
)
7406 value_range
*vr0
, *vr1
;
7408 vr0
= (TREE_CODE (op0
) == SSA_NAME
) ? get_value_range (op0
) : NULL
;
7409 vr1
= (TREE_CODE (op1
) == SSA_NAME
) ? get_value_range (op1
) : NULL
;
7411 tree res
= NULL_TREE
;
7413 res
= compare_ranges (code
, vr0
, vr1
, strict_overflow_p
);
7415 res
= compare_range_with_value (code
, vr0
, op1
, strict_overflow_p
);
7417 res
= (compare_range_with_value
7418 (swap_tree_comparison (code
), vr1
, op0
, strict_overflow_p
));
7422 /* Helper function for vrp_evaluate_conditional_warnv. */
7425 vrp_evaluate_conditional_warnv_with_ops (enum tree_code code
, tree op0
,
7426 tree op1
, bool use_equiv_p
,
7427 bool *strict_overflow_p
, bool *only_ranges
)
7431 *only_ranges
= true;
7433 /* We only deal with integral and pointer types. */
7434 if (!INTEGRAL_TYPE_P (TREE_TYPE (op0
))
7435 && !POINTER_TYPE_P (TREE_TYPE (op0
)))
7438 if ((ret
= vrp_evaluate_conditional_warnv_with_ops_using_ranges
7439 (code
, op0
, op1
, strict_overflow_p
)))
7442 *only_ranges
= false;
7443 /* Do not use compare_names during propagation, it's quadratic. */
7444 if (TREE_CODE (op0
) == SSA_NAME
&& TREE_CODE (op1
) == SSA_NAME
7446 return compare_names (code
, op0
, op1
, strict_overflow_p
);
7447 else if (TREE_CODE (op0
) == SSA_NAME
)
7448 return compare_name_with_value (code
, op0
, op1
,
7449 strict_overflow_p
, use_equiv_p
);
7450 else if (TREE_CODE (op1
) == SSA_NAME
)
7451 return compare_name_with_value (swap_tree_comparison (code
), op1
, op0
,
7452 strict_overflow_p
, use_equiv_p
);
7456 /* Given (CODE OP0 OP1) within STMT, try to simplify it based on value range
7457 information. Return NULL if the conditional can not be evaluated.
7458 The ranges of all the names equivalent with the operands in COND
7459 will be used when trying to compute the value. If the result is
7460 based on undefined signed overflow, issue a warning if
7464 vrp_evaluate_conditional (tree_code code
, tree op0
, tree op1
, gimple
*stmt
)
7470 /* Some passes and foldings leak constants with overflow flag set
7471 into the IL. Avoid doing wrong things with these and bail out. */
7472 if ((TREE_CODE (op0
) == INTEGER_CST
7473 && TREE_OVERFLOW (op0
))
7474 || (TREE_CODE (op1
) == INTEGER_CST
7475 && TREE_OVERFLOW (op1
)))
7479 ret
= vrp_evaluate_conditional_warnv_with_ops (code
, op0
, op1
, true, &sop
,
7484 enum warn_strict_overflow_code wc
;
7485 const char* warnmsg
;
7487 if (is_gimple_min_invariant (ret
))
7489 wc
= WARN_STRICT_OVERFLOW_CONDITIONAL
;
7490 warnmsg
= G_("assuming signed overflow does not occur when "
7491 "simplifying conditional to constant");
7495 wc
= WARN_STRICT_OVERFLOW_COMPARISON
;
7496 warnmsg
= G_("assuming signed overflow does not occur when "
7497 "simplifying conditional");
7500 if (issue_strict_overflow_warning (wc
))
7502 location_t location
;
7504 if (!gimple_has_location (stmt
))
7505 location
= input_location
;
7507 location
= gimple_location (stmt
);
7508 warning_at (location
, OPT_Wstrict_overflow
, "%s", warnmsg
);
7512 if (warn_type_limits
7513 && ret
&& only_ranges
7514 && TREE_CODE_CLASS (code
) == tcc_comparison
7515 && TREE_CODE (op0
) == SSA_NAME
)
7517 /* If the comparison is being folded and the operand on the LHS
7518 is being compared against a constant value that is outside of
7519 the natural range of OP0's type, then the predicate will
7520 always fold regardless of the value of OP0. If -Wtype-limits
7521 was specified, emit a warning. */
7522 tree type
= TREE_TYPE (op0
);
7523 value_range
*vr0
= get_value_range (op0
);
7525 if (vr0
->type
== VR_RANGE
7526 && INTEGRAL_TYPE_P (type
)
7527 && vrp_val_is_min (vr0
->min
)
7528 && vrp_val_is_max (vr0
->max
)
7529 && is_gimple_min_invariant (op1
))
7531 location_t location
;
7533 if (!gimple_has_location (stmt
))
7534 location
= input_location
;
7536 location
= gimple_location (stmt
);
7538 warning_at (location
, OPT_Wtype_limits
,
7540 ? G_("comparison always false "
7541 "due to limited range of data type")
7542 : G_("comparison always true "
7543 "due to limited range of data type"));
7551 /* Visit conditional statement STMT. If we can determine which edge
7552 will be taken out of STMT's basic block, record it in
7553 *TAKEN_EDGE_P and return SSA_PROP_INTERESTING. Otherwise, return
7554 SSA_PROP_VARYING. */
7556 static enum ssa_prop_result
7557 vrp_visit_cond_stmt (gcond
*stmt
, edge
*taken_edge_p
)
7562 *taken_edge_p
= NULL
;
7564 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7569 fprintf (dump_file
, "\nVisiting conditional with predicate: ");
7570 print_gimple_stmt (dump_file
, stmt
, 0, 0);
7571 fprintf (dump_file
, "\nWith known ranges\n");
7573 FOR_EACH_SSA_TREE_OPERAND (use
, stmt
, i
, SSA_OP_USE
)
7575 fprintf (dump_file
, "\t");
7576 print_generic_expr (dump_file
, use
, 0);
7577 fprintf (dump_file
, ": ");
7578 dump_value_range (dump_file
, vr_value
[SSA_NAME_VERSION (use
)]);
7581 fprintf (dump_file
, "\n");
7584 /* Compute the value of the predicate COND by checking the known
7585 ranges of each of its operands.
7587 Note that we cannot evaluate all the equivalent ranges here
7588 because those ranges may not yet be final and with the current
7589 propagation strategy, we cannot determine when the value ranges
7590 of the names in the equivalence set have changed.
7592 For instance, given the following code fragment
7596 i_14 = ASSERT_EXPR <i_5, i_5 != 0>
7600 Assume that on the first visit to i_14, i_5 has the temporary
7601 range [8, 8] because the second argument to the PHI function is
7602 not yet executable. We derive the range ~[0, 0] for i_14 and the
7603 equivalence set { i_5 }. So, when we visit 'if (i_14 == 1)' for
7604 the first time, since i_14 is equivalent to the range [8, 8], we
7605 determine that the predicate is always false.
7607 On the next round of propagation, i_13 is determined to be
7608 VARYING, which causes i_5 to drop down to VARYING. So, another
7609 visit to i_14 is scheduled. In this second visit, we compute the
7610 exact same range and equivalence set for i_14, namely ~[0, 0] and
7611 { i_5 }. But we did not have the previous range for i_5
7612 registered, so vrp_visit_assignment thinks that the range for
7613 i_14 has not changed. Therefore, the predicate 'if (i_14 == 1)'
7614 is not visited again, which stops propagation from visiting
7615 statements in the THEN clause of that if().
7617 To properly fix this we would need to keep the previous range
7618 value for the names in the equivalence set. This way we would've
7619 discovered that from one visit to the other i_5 changed from
7620 range [8, 8] to VR_VARYING.
7622 However, fixing this apparent limitation may not be worth the
7623 additional checking. Testing on several code bases (GCC, DLV,
7624 MICO, TRAMP3D and SPEC2000) showed that doing this results in
7625 4 more predicates folded in SPEC. */
7628 val
= vrp_evaluate_conditional_warnv_with_ops (gimple_cond_code (stmt
),
7629 gimple_cond_lhs (stmt
),
7630 gimple_cond_rhs (stmt
),
7635 *taken_edge_p
= find_taken_edge (gimple_bb (stmt
), val
);
7638 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7640 "\nIgnoring predicate evaluation because "
7641 "it assumes that signed overflow is undefined");
7646 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7648 fprintf (dump_file
, "\nPredicate evaluates to: ");
7649 if (val
== NULL_TREE
)
7650 fprintf (dump_file
, "DON'T KNOW\n");
7652 print_generic_stmt (dump_file
, val
, 0);
7655 return (*taken_edge_p
) ? SSA_PROP_INTERESTING
: SSA_PROP_VARYING
;
7658 /* Searches the case label vector VEC for the index *IDX of the CASE_LABEL
7659 that includes the value VAL. The search is restricted to the range
7660 [START_IDX, n - 1] where n is the size of VEC.
7662 If there is a CASE_LABEL for VAL, its index is placed in IDX and true is
7665 If there is no CASE_LABEL for VAL and there is one that is larger than VAL,
7666 it is placed in IDX and false is returned.
7668 If VAL is larger than any CASE_LABEL, n is placed on IDX and false is
7672 find_case_label_index (gswitch
*stmt
, size_t start_idx
, tree val
, size_t *idx
)
7674 size_t n
= gimple_switch_num_labels (stmt
);
7677 /* Find case label for minimum of the value range or the next one.
7678 At each iteration we are searching in [low, high - 1]. */
7680 for (low
= start_idx
, high
= n
; high
!= low
; )
7684 /* Note that i != high, so we never ask for n. */
7685 size_t i
= (high
+ low
) / 2;
7686 t
= gimple_switch_label (stmt
, i
);
7688 /* Cache the result of comparing CASE_LOW and val. */
7689 cmp
= tree_int_cst_compare (CASE_LOW (t
), val
);
7693 /* Ranges cannot be empty. */
7702 if (CASE_HIGH (t
) != NULL
7703 && tree_int_cst_compare (CASE_HIGH (t
), val
) >= 0)
7715 /* Searches the case label vector VEC for the range of CASE_LABELs that is used
7716 for values between MIN and MAX. The first index is placed in MIN_IDX. The
7717 last index is placed in MAX_IDX. If the range of CASE_LABELs is empty
7718 then MAX_IDX < MIN_IDX.
7719 Returns true if the default label is not needed. */
7722 find_case_label_range (gswitch
*stmt
, tree min
, tree max
, size_t *min_idx
,
7726 bool min_take_default
= !find_case_label_index (stmt
, 1, min
, &i
);
7727 bool max_take_default
= !find_case_label_index (stmt
, i
, max
, &j
);
7731 && max_take_default
)
7733 /* Only the default case label reached.
7734 Return an empty range. */
7741 bool take_default
= min_take_default
|| max_take_default
;
7745 if (max_take_default
)
7748 /* If the case label range is continuous, we do not need
7749 the default case label. Verify that. */
7750 high
= CASE_LOW (gimple_switch_label (stmt
, i
));
7751 if (CASE_HIGH (gimple_switch_label (stmt
, i
)))
7752 high
= CASE_HIGH (gimple_switch_label (stmt
, i
));
7753 for (k
= i
+ 1; k
<= j
; ++k
)
7755 low
= CASE_LOW (gimple_switch_label (stmt
, k
));
7756 if (!integer_onep (int_const_binop (MINUS_EXPR
, low
, high
)))
7758 take_default
= true;
7762 if (CASE_HIGH (gimple_switch_label (stmt
, k
)))
7763 high
= CASE_HIGH (gimple_switch_label (stmt
, k
));
7768 return !take_default
;
7772 /* Searches the case label vector VEC for the ranges of CASE_LABELs that are
7773 used in range VR. The indices are placed in MIN_IDX1, MAX_IDX, MIN_IDX2 and
7774 MAX_IDX2. If the ranges of CASE_LABELs are empty then MAX_IDX1 < MIN_IDX1.
7775 Returns true if the default label is not needed. */
7778 find_case_label_ranges (gswitch
*stmt
, value_range
*vr
, size_t *min_idx1
,
7779 size_t *max_idx1
, size_t *min_idx2
,
7783 unsigned int n
= gimple_switch_num_labels (stmt
);
7785 tree case_low
, case_high
;
7786 tree min
= vr
->min
, max
= vr
->max
;
7788 gcc_checking_assert (vr
->type
== VR_RANGE
|| vr
->type
== VR_ANTI_RANGE
);
7790 take_default
= !find_case_label_range (stmt
, min
, max
, &i
, &j
);
7792 /* Set second range to emtpy. */
7796 if (vr
->type
== VR_RANGE
)
7800 return !take_default
;
7803 /* Set first range to all case labels. */
7810 /* Make sure all the values of case labels [i , j] are contained in
7811 range [MIN, MAX]. */
7812 case_low
= CASE_LOW (gimple_switch_label (stmt
, i
));
7813 case_high
= CASE_HIGH (gimple_switch_label (stmt
, j
));
7814 if (tree_int_cst_compare (case_low
, min
) < 0)
7816 if (case_high
!= NULL_TREE
7817 && tree_int_cst_compare (max
, case_high
) < 0)
7823 /* If the range spans case labels [i, j], the corresponding anti-range spans
7824 the labels [1, i - 1] and [j + 1, n - 1]. */
7850 /* Visit switch statement STMT. If we can determine which edge
7851 will be taken out of STMT's basic block, record it in
7852 *TAKEN_EDGE_P and return SSA_PROP_INTERESTING. Otherwise, return
7853 SSA_PROP_VARYING. */
7855 static enum ssa_prop_result
7856 vrp_visit_switch_stmt (gswitch
*stmt
, edge
*taken_edge_p
)
7860 size_t i
= 0, j
= 0, k
, l
;
7863 *taken_edge_p
= NULL
;
7864 op
= gimple_switch_index (stmt
);
7865 if (TREE_CODE (op
) != SSA_NAME
)
7866 return SSA_PROP_VARYING
;
7868 vr
= get_value_range (op
);
7869 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7871 fprintf (dump_file
, "\nVisiting switch expression with operand ");
7872 print_generic_expr (dump_file
, op
, 0);
7873 fprintf (dump_file
, " with known range ");
7874 dump_value_range (dump_file
, vr
);
7875 fprintf (dump_file
, "\n");
7878 if ((vr
->type
!= VR_RANGE
7879 && vr
->type
!= VR_ANTI_RANGE
)
7880 || symbolic_range_p (vr
))
7881 return SSA_PROP_VARYING
;
7883 /* Find the single edge that is taken from the switch expression. */
7884 take_default
= !find_case_label_ranges (stmt
, vr
, &i
, &j
, &k
, &l
);
7886 /* Check if the range spans no CASE_LABEL. If so, we only reach the default
7890 gcc_assert (take_default
);
7891 val
= gimple_switch_default_label (stmt
);
7895 /* Check if labels with index i to j and maybe the default label
7896 are all reaching the same label. */
7898 val
= gimple_switch_label (stmt
, i
);
7900 && CASE_LABEL (gimple_switch_default_label (stmt
))
7901 != CASE_LABEL (val
))
7903 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7904 fprintf (dump_file
, " not a single destination for this "
7906 return SSA_PROP_VARYING
;
7908 for (++i
; i
<= j
; ++i
)
7910 if (CASE_LABEL (gimple_switch_label (stmt
, i
)) != CASE_LABEL (val
))
7912 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7913 fprintf (dump_file
, " not a single destination for this "
7915 return SSA_PROP_VARYING
;
7920 if (CASE_LABEL (gimple_switch_label (stmt
, k
)) != CASE_LABEL (val
))
7922 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7923 fprintf (dump_file
, " not a single destination for this "
7925 return SSA_PROP_VARYING
;
7930 *taken_edge_p
= find_edge (gimple_bb (stmt
),
7931 label_to_block (CASE_LABEL (val
)));
7933 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7935 fprintf (dump_file
, " will take edge to ");
7936 print_generic_stmt (dump_file
, CASE_LABEL (val
), 0);
7939 return SSA_PROP_INTERESTING
;
7943 /* Evaluate statement STMT. If the statement produces a useful range,
7944 return SSA_PROP_INTERESTING and record the SSA name with the
7945 interesting range into *OUTPUT_P.
7947 If STMT is a conditional branch and we can determine its truth
7948 value, the taken edge is recorded in *TAKEN_EDGE_P.
7950 If STMT produces a varying value, return SSA_PROP_VARYING. */
7952 static enum ssa_prop_result
7953 vrp_visit_stmt (gimple
*stmt
, edge
*taken_edge_p
, tree
*output_p
)
7955 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7957 fprintf (dump_file
, "\nVisiting statement:\n");
7958 print_gimple_stmt (dump_file
, stmt
, 0, dump_flags
);
7961 if (!stmt_interesting_for_vrp (stmt
))
7962 gcc_assert (stmt_ends_bb_p (stmt
));
7963 else if (is_gimple_assign (stmt
) || is_gimple_call (stmt
))
7964 return vrp_visit_assignment_or_call (stmt
, output_p
);
7965 else if (gimple_code (stmt
) == GIMPLE_COND
)
7966 return vrp_visit_cond_stmt (as_a
<gcond
*> (stmt
), taken_edge_p
);
7967 else if (gimple_code (stmt
) == GIMPLE_SWITCH
)
7968 return vrp_visit_switch_stmt (as_a
<gswitch
*> (stmt
), taken_edge_p
);
7970 /* All other statements produce nothing of interest for VRP, so mark
7971 their outputs varying and prevent further simulation. */
7972 set_defs_to_varying (stmt
);
7974 return SSA_PROP_VARYING
;
7977 /* Union the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
7978 { VR1TYPE, VR0MIN, VR0MAX } and store the result
7979 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
7980 possible such range. The resulting range is not canonicalized. */
7983 union_ranges (enum value_range_type
*vr0type
,
7984 tree
*vr0min
, tree
*vr0max
,
7985 enum value_range_type vr1type
,
7986 tree vr1min
, tree vr1max
)
7988 bool mineq
= vrp_operand_equal_p (*vr0min
, vr1min
);
7989 bool maxeq
= vrp_operand_equal_p (*vr0max
, vr1max
);
7991 /* [] is vr0, () is vr1 in the following classification comments. */
7995 if (*vr0type
== vr1type
)
7996 /* Nothing to do for equal ranges. */
7998 else if ((*vr0type
== VR_RANGE
7999 && vr1type
== VR_ANTI_RANGE
)
8000 || (*vr0type
== VR_ANTI_RANGE
8001 && vr1type
== VR_RANGE
))
8003 /* For anti-range with range union the result is varying. */
8009 else if (operand_less_p (*vr0max
, vr1min
) == 1
8010 || operand_less_p (vr1max
, *vr0min
) == 1)
8012 /* [ ] ( ) or ( ) [ ]
8013 If the ranges have an empty intersection, result of the union
8014 operation is the anti-range or if both are anti-ranges
8016 if (*vr0type
== VR_ANTI_RANGE
8017 && vr1type
== VR_ANTI_RANGE
)
8019 else if (*vr0type
== VR_ANTI_RANGE
8020 && vr1type
== VR_RANGE
)
8022 else if (*vr0type
== VR_RANGE
8023 && vr1type
== VR_ANTI_RANGE
)
8029 else if (*vr0type
== VR_RANGE
8030 && vr1type
== VR_RANGE
)
8032 /* The result is the convex hull of both ranges. */
8033 if (operand_less_p (*vr0max
, vr1min
) == 1)
8035 /* If the result can be an anti-range, create one. */
8036 if (TREE_CODE (*vr0max
) == INTEGER_CST
8037 && TREE_CODE (vr1min
) == INTEGER_CST
8038 && vrp_val_is_min (*vr0min
)
8039 && vrp_val_is_max (vr1max
))
8041 tree min
= int_const_binop (PLUS_EXPR
,
8043 build_int_cst (TREE_TYPE (*vr0max
), 1));
8044 tree max
= int_const_binop (MINUS_EXPR
,
8046 build_int_cst (TREE_TYPE (vr1min
), 1));
8047 if (!operand_less_p (max
, min
))
8049 *vr0type
= VR_ANTI_RANGE
;
8061 /* If the result can be an anti-range, create one. */
8062 if (TREE_CODE (vr1max
) == INTEGER_CST
8063 && TREE_CODE (*vr0min
) == INTEGER_CST
8064 && vrp_val_is_min (vr1min
)
8065 && vrp_val_is_max (*vr0max
))
8067 tree min
= int_const_binop (PLUS_EXPR
,
8069 build_int_cst (TREE_TYPE (vr1max
), 1));
8070 tree max
= int_const_binop (MINUS_EXPR
,
8072 build_int_cst (TREE_TYPE (*vr0min
), 1));
8073 if (!operand_less_p (max
, min
))
8075 *vr0type
= VR_ANTI_RANGE
;
8089 else if ((maxeq
|| operand_less_p (vr1max
, *vr0max
) == 1)
8090 && (mineq
|| operand_less_p (*vr0min
, vr1min
) == 1))
8092 /* [ ( ) ] or [( ) ] or [ ( )] */
8093 if (*vr0type
== VR_RANGE
8094 && vr1type
== VR_RANGE
)
8096 else if (*vr0type
== VR_ANTI_RANGE
8097 && vr1type
== VR_ANTI_RANGE
)
8103 else if (*vr0type
== VR_ANTI_RANGE
8104 && vr1type
== VR_RANGE
)
8106 /* Arbitrarily choose the right or left gap. */
8107 if (!mineq
&& TREE_CODE (vr1min
) == INTEGER_CST
)
8108 *vr0max
= int_const_binop (MINUS_EXPR
, vr1min
,
8109 build_int_cst (TREE_TYPE (vr1min
), 1));
8110 else if (!maxeq
&& TREE_CODE (vr1max
) == INTEGER_CST
)
8111 *vr0min
= int_const_binop (PLUS_EXPR
, vr1max
,
8112 build_int_cst (TREE_TYPE (vr1max
), 1));
8116 else if (*vr0type
== VR_RANGE
8117 && vr1type
== VR_ANTI_RANGE
)
8118 /* The result covers everything. */
8123 else if ((maxeq
|| operand_less_p (*vr0max
, vr1max
) == 1)
8124 && (mineq
|| operand_less_p (vr1min
, *vr0min
) == 1))
8126 /* ( [ ] ) or ([ ] ) or ( [ ]) */
8127 if (*vr0type
== VR_RANGE
8128 && vr1type
== VR_RANGE
)
8134 else if (*vr0type
== VR_ANTI_RANGE
8135 && vr1type
== VR_ANTI_RANGE
)
8137 else if (*vr0type
== VR_RANGE
8138 && vr1type
== VR_ANTI_RANGE
)
8140 *vr0type
= VR_ANTI_RANGE
;
8141 if (!mineq
&& TREE_CODE (*vr0min
) == INTEGER_CST
)
8143 *vr0max
= int_const_binop (MINUS_EXPR
, *vr0min
,
8144 build_int_cst (TREE_TYPE (*vr0min
), 1));
8147 else if (!maxeq
&& TREE_CODE (*vr0max
) == INTEGER_CST
)
8149 *vr0min
= int_const_binop (PLUS_EXPR
, *vr0max
,
8150 build_int_cst (TREE_TYPE (*vr0max
), 1));
8156 else if (*vr0type
== VR_ANTI_RANGE
8157 && vr1type
== VR_RANGE
)
8158 /* The result covers everything. */
8163 else if ((operand_less_p (vr1min
, *vr0max
) == 1
8164 || operand_equal_p (vr1min
, *vr0max
, 0))
8165 && operand_less_p (*vr0min
, vr1min
) == 1
8166 && operand_less_p (*vr0max
, vr1max
) == 1)
8168 /* [ ( ] ) or [ ]( ) */
8169 if (*vr0type
== VR_RANGE
8170 && vr1type
== VR_RANGE
)
8172 else if (*vr0type
== VR_ANTI_RANGE
8173 && vr1type
== VR_ANTI_RANGE
)
8175 else if (*vr0type
== VR_ANTI_RANGE
8176 && vr1type
== VR_RANGE
)
8178 if (TREE_CODE (vr1min
) == INTEGER_CST
)
8179 *vr0max
= int_const_binop (MINUS_EXPR
, vr1min
,
8180 build_int_cst (TREE_TYPE (vr1min
), 1));
8184 else if (*vr0type
== VR_RANGE
8185 && vr1type
== VR_ANTI_RANGE
)
8187 if (TREE_CODE (*vr0max
) == INTEGER_CST
)
8190 *vr0min
= int_const_binop (PLUS_EXPR
, *vr0max
,
8191 build_int_cst (TREE_TYPE (*vr0max
), 1));
8200 else if ((operand_less_p (*vr0min
, vr1max
) == 1
8201 || operand_equal_p (*vr0min
, vr1max
, 0))
8202 && operand_less_p (vr1min
, *vr0min
) == 1
8203 && operand_less_p (vr1max
, *vr0max
) == 1)
8205 /* ( [ ) ] or ( )[ ] */
8206 if (*vr0type
== VR_RANGE
8207 && vr1type
== VR_RANGE
)
8209 else if (*vr0type
== VR_ANTI_RANGE
8210 && vr1type
== VR_ANTI_RANGE
)
8212 else if (*vr0type
== VR_ANTI_RANGE
8213 && vr1type
== VR_RANGE
)
8215 if (TREE_CODE (vr1max
) == INTEGER_CST
)
8216 *vr0min
= int_const_binop (PLUS_EXPR
, vr1max
,
8217 build_int_cst (TREE_TYPE (vr1max
), 1));
8221 else if (*vr0type
== VR_RANGE
8222 && vr1type
== VR_ANTI_RANGE
)
8224 if (TREE_CODE (*vr0min
) == INTEGER_CST
)
8228 *vr0max
= int_const_binop (MINUS_EXPR
, *vr0min
,
8229 build_int_cst (TREE_TYPE (*vr0min
), 1));
8243 *vr0type
= VR_VARYING
;
8244 *vr0min
= NULL_TREE
;
8245 *vr0max
= NULL_TREE
;
8248 /* Intersect the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
8249 { VR1TYPE, VR0MIN, VR0MAX } and store the result
8250 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
8251 possible such range. The resulting range is not canonicalized. */
8254 intersect_ranges (enum value_range_type
*vr0type
,
8255 tree
*vr0min
, tree
*vr0max
,
8256 enum value_range_type vr1type
,
8257 tree vr1min
, tree vr1max
)
8259 bool mineq
= vrp_operand_equal_p (*vr0min
, vr1min
);
8260 bool maxeq
= vrp_operand_equal_p (*vr0max
, vr1max
);
8262 /* [] is vr0, () is vr1 in the following classification comments. */
8266 if (*vr0type
== vr1type
)
8267 /* Nothing to do for equal ranges. */
8269 else if ((*vr0type
== VR_RANGE
8270 && vr1type
== VR_ANTI_RANGE
)
8271 || (*vr0type
== VR_ANTI_RANGE
8272 && vr1type
== VR_RANGE
))
8274 /* For anti-range with range intersection the result is empty. */
8275 *vr0type
= VR_UNDEFINED
;
8276 *vr0min
= NULL_TREE
;
8277 *vr0max
= NULL_TREE
;
8282 else if (operand_less_p (*vr0max
, vr1min
) == 1
8283 || operand_less_p (vr1max
, *vr0min
) == 1)
8285 /* [ ] ( ) or ( ) [ ]
8286 If the ranges have an empty intersection, the result of the
8287 intersect operation is the range for intersecting an
8288 anti-range with a range or empty when intersecting two ranges. */
8289 if (*vr0type
== VR_RANGE
8290 && vr1type
== VR_ANTI_RANGE
)
8292 else if (*vr0type
== VR_ANTI_RANGE
8293 && vr1type
== VR_RANGE
)
8299 else if (*vr0type
== VR_RANGE
8300 && vr1type
== VR_RANGE
)
8302 *vr0type
= VR_UNDEFINED
;
8303 *vr0min
= NULL_TREE
;
8304 *vr0max
= NULL_TREE
;
8306 else if (*vr0type
== VR_ANTI_RANGE
8307 && vr1type
== VR_ANTI_RANGE
)
8309 /* If the anti-ranges are adjacent to each other merge them. */
8310 if (TREE_CODE (*vr0max
) == INTEGER_CST
8311 && TREE_CODE (vr1min
) == INTEGER_CST
8312 && operand_less_p (*vr0max
, vr1min
) == 1
8313 && integer_onep (int_const_binop (MINUS_EXPR
,
8316 else if (TREE_CODE (vr1max
) == INTEGER_CST
8317 && TREE_CODE (*vr0min
) == INTEGER_CST
8318 && operand_less_p (vr1max
, *vr0min
) == 1
8319 && integer_onep (int_const_binop (MINUS_EXPR
,
8322 /* Else arbitrarily take VR0. */
8325 else if ((maxeq
|| operand_less_p (vr1max
, *vr0max
) == 1)
8326 && (mineq
|| operand_less_p (*vr0min
, vr1min
) == 1))
8328 /* [ ( ) ] or [( ) ] or [ ( )] */
8329 if (*vr0type
== VR_RANGE
8330 && vr1type
== VR_RANGE
)
8332 /* If both are ranges the result is the inner one. */
8337 else if (*vr0type
== VR_RANGE
8338 && vr1type
== VR_ANTI_RANGE
)
8340 /* Choose the right gap if the left one is empty. */
8343 if (TREE_CODE (vr1max
) == INTEGER_CST
)
8344 *vr0min
= int_const_binop (PLUS_EXPR
, vr1max
,
8345 build_int_cst (TREE_TYPE (vr1max
), 1));
8349 /* Choose the left gap if the right one is empty. */
8352 if (TREE_CODE (vr1min
) == INTEGER_CST
)
8353 *vr0max
= int_const_binop (MINUS_EXPR
, vr1min
,
8354 build_int_cst (TREE_TYPE (vr1min
), 1));
8358 /* Choose the anti-range if the range is effectively varying. */
8359 else if (vrp_val_is_min (*vr0min
)
8360 && vrp_val_is_max (*vr0max
))
8366 /* Else choose the range. */
8368 else if (*vr0type
== VR_ANTI_RANGE
8369 && vr1type
== VR_ANTI_RANGE
)
8370 /* If both are anti-ranges the result is the outer one. */
8372 else if (*vr0type
== VR_ANTI_RANGE
8373 && vr1type
== VR_RANGE
)
8375 /* The intersection is empty. */
8376 *vr0type
= VR_UNDEFINED
;
8377 *vr0min
= NULL_TREE
;
8378 *vr0max
= NULL_TREE
;
8383 else if ((maxeq
|| operand_less_p (*vr0max
, vr1max
) == 1)
8384 && (mineq
|| operand_less_p (vr1min
, *vr0min
) == 1))
8386 /* ( [ ] ) or ([ ] ) or ( [ ]) */
8387 if (*vr0type
== VR_RANGE
8388 && vr1type
== VR_RANGE
)
8389 /* Choose the inner range. */
8391 else if (*vr0type
== VR_ANTI_RANGE
8392 && vr1type
== VR_RANGE
)
8394 /* Choose the right gap if the left is empty. */
8397 *vr0type
= VR_RANGE
;
8398 if (TREE_CODE (*vr0max
) == INTEGER_CST
)
8399 *vr0min
= int_const_binop (PLUS_EXPR
, *vr0max
,
8400 build_int_cst (TREE_TYPE (*vr0max
), 1));
8405 /* Choose the left gap if the right is empty. */
8408 *vr0type
= VR_RANGE
;
8409 if (TREE_CODE (*vr0min
) == INTEGER_CST
)
8410 *vr0max
= int_const_binop (MINUS_EXPR
, *vr0min
,
8411 build_int_cst (TREE_TYPE (*vr0min
), 1));
8416 /* Choose the anti-range if the range is effectively varying. */
8417 else if (vrp_val_is_min (vr1min
)
8418 && vrp_val_is_max (vr1max
))
8420 /* Else choose the range. */
8428 else if (*vr0type
== VR_ANTI_RANGE
8429 && vr1type
== VR_ANTI_RANGE
)
8431 /* If both are anti-ranges the result is the outer one. */
8436 else if (vr1type
== VR_ANTI_RANGE
8437 && *vr0type
== VR_RANGE
)
8439 /* The intersection is empty. */
8440 *vr0type
= VR_UNDEFINED
;
8441 *vr0min
= NULL_TREE
;
8442 *vr0max
= NULL_TREE
;
8447 else if ((operand_less_p (vr1min
, *vr0max
) == 1
8448 || operand_equal_p (vr1min
, *vr0max
, 0))
8449 && operand_less_p (*vr0min
, vr1min
) == 1)
8451 /* [ ( ] ) or [ ]( ) */
8452 if (*vr0type
== VR_ANTI_RANGE
8453 && vr1type
== VR_ANTI_RANGE
)
8455 else if (*vr0type
== VR_RANGE
8456 && vr1type
== VR_RANGE
)
8458 else if (*vr0type
== VR_RANGE
8459 && vr1type
== VR_ANTI_RANGE
)
8461 if (TREE_CODE (vr1min
) == INTEGER_CST
)
8462 *vr0max
= int_const_binop (MINUS_EXPR
, vr1min
,
8463 build_int_cst (TREE_TYPE (vr1min
), 1));
8467 else if (*vr0type
== VR_ANTI_RANGE
8468 && vr1type
== VR_RANGE
)
8470 *vr0type
= VR_RANGE
;
8471 if (TREE_CODE (*vr0max
) == INTEGER_CST
)
8472 *vr0min
= int_const_binop (PLUS_EXPR
, *vr0max
,
8473 build_int_cst (TREE_TYPE (*vr0max
), 1));
8481 else if ((operand_less_p (*vr0min
, vr1max
) == 1
8482 || operand_equal_p (*vr0min
, vr1max
, 0))
8483 && operand_less_p (vr1min
, *vr0min
) == 1)
8485 /* ( [ ) ] or ( )[ ] */
8486 if (*vr0type
== VR_ANTI_RANGE
8487 && vr1type
== VR_ANTI_RANGE
)
8489 else if (*vr0type
== VR_RANGE
8490 && vr1type
== VR_RANGE
)
8492 else if (*vr0type
== VR_RANGE
8493 && vr1type
== VR_ANTI_RANGE
)
8495 if (TREE_CODE (vr1max
) == INTEGER_CST
)
8496 *vr0min
= int_const_binop (PLUS_EXPR
, vr1max
,
8497 build_int_cst (TREE_TYPE (vr1max
), 1));
8501 else if (*vr0type
== VR_ANTI_RANGE
8502 && vr1type
== VR_RANGE
)
8504 *vr0type
= VR_RANGE
;
8505 if (TREE_CODE (*vr0min
) == INTEGER_CST
)
8506 *vr0max
= int_const_binop (MINUS_EXPR
, *vr0min
,
8507 build_int_cst (TREE_TYPE (*vr0min
), 1));
8516 /* As a fallback simply use { *VRTYPE, *VR0MIN, *VR0MAX } as
8517 result for the intersection. That's always a conservative
8518 correct estimate. */
8524 /* Intersect the two value-ranges *VR0 and *VR1 and store the result
8525 in *VR0. This may not be the smallest possible such range. */
8528 vrp_intersect_ranges_1 (value_range
*vr0
, value_range
*vr1
)
8532 /* If either range is VR_VARYING the other one wins. */
8533 if (vr1
->type
== VR_VARYING
)
8535 if (vr0
->type
== VR_VARYING
)
8537 copy_value_range (vr0
, vr1
);
8541 /* When either range is VR_UNDEFINED the resulting range is
8542 VR_UNDEFINED, too. */
8543 if (vr0
->type
== VR_UNDEFINED
)
8545 if (vr1
->type
== VR_UNDEFINED
)
8547 set_value_range_to_undefined (vr0
);
8551 /* Save the original vr0 so we can return it as conservative intersection
8552 result when our worker turns things to varying. */
8554 intersect_ranges (&vr0
->type
, &vr0
->min
, &vr0
->max
,
8555 vr1
->type
, vr1
->min
, vr1
->max
);
8556 /* Make sure to canonicalize the result though as the inversion of a
8557 VR_RANGE can still be a VR_RANGE. */
8558 set_and_canonicalize_value_range (vr0
, vr0
->type
,
8559 vr0
->min
, vr0
->max
, vr0
->equiv
);
8560 /* If that failed, use the saved original VR0. */
8561 if (vr0
->type
== VR_VARYING
)
8566 /* If the result is VR_UNDEFINED there is no need to mess with
8567 the equivalencies. */
8568 if (vr0
->type
== VR_UNDEFINED
)
8571 /* The resulting set of equivalences for range intersection is the union of
8573 if (vr0
->equiv
&& vr1
->equiv
&& vr0
->equiv
!= vr1
->equiv
)
8574 bitmap_ior_into (vr0
->equiv
, vr1
->equiv
);
8575 else if (vr1
->equiv
&& !vr0
->equiv
)
8577 vr0
->equiv
= BITMAP_ALLOC (NULL
);
8578 bitmap_copy (vr0
->equiv
, vr1
->equiv
);
8583 vrp_intersect_ranges (value_range
*vr0
, value_range
*vr1
)
8585 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8587 fprintf (dump_file
, "Intersecting\n ");
8588 dump_value_range (dump_file
, vr0
);
8589 fprintf (dump_file
, "\nand\n ");
8590 dump_value_range (dump_file
, vr1
);
8591 fprintf (dump_file
, "\n");
8593 vrp_intersect_ranges_1 (vr0
, vr1
);
8594 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8596 fprintf (dump_file
, "to\n ");
8597 dump_value_range (dump_file
, vr0
);
8598 fprintf (dump_file
, "\n");
8602 /* Meet operation for value ranges. Given two value ranges VR0 and
8603 VR1, store in VR0 a range that contains both VR0 and VR1. This
8604 may not be the smallest possible such range. */
8607 vrp_meet_1 (value_range
*vr0
, value_range
*vr1
)
8611 if (vr0
->type
== VR_UNDEFINED
)
8613 set_value_range (vr0
, vr1
->type
, vr1
->min
, vr1
->max
, vr1
->equiv
);
8617 if (vr1
->type
== VR_UNDEFINED
)
8619 /* VR0 already has the resulting range. */
8623 if (vr0
->type
== VR_VARYING
)
8625 /* Nothing to do. VR0 already has the resulting range. */
8629 if (vr1
->type
== VR_VARYING
)
8631 set_value_range_to_varying (vr0
);
8636 union_ranges (&vr0
->type
, &vr0
->min
, &vr0
->max
,
8637 vr1
->type
, vr1
->min
, vr1
->max
);
8638 if (vr0
->type
== VR_VARYING
)
8640 /* Failed to find an efficient meet. Before giving up and setting
8641 the result to VARYING, see if we can at least derive a useful
8642 anti-range. FIXME, all this nonsense about distinguishing
8643 anti-ranges from ranges is necessary because of the odd
8644 semantics of range_includes_zero_p and friends. */
8645 if (((saved
.type
== VR_RANGE
8646 && range_includes_zero_p (saved
.min
, saved
.max
) == 0)
8647 || (saved
.type
== VR_ANTI_RANGE
8648 && range_includes_zero_p (saved
.min
, saved
.max
) == 1))
8649 && ((vr1
->type
== VR_RANGE
8650 && range_includes_zero_p (vr1
->min
, vr1
->max
) == 0)
8651 || (vr1
->type
== VR_ANTI_RANGE
8652 && range_includes_zero_p (vr1
->min
, vr1
->max
) == 1)))
8654 set_value_range_to_nonnull (vr0
, TREE_TYPE (saved
.min
));
8656 /* Since this meet operation did not result from the meeting of
8657 two equivalent names, VR0 cannot have any equivalences. */
8659 bitmap_clear (vr0
->equiv
);
8663 set_value_range_to_varying (vr0
);
8666 set_and_canonicalize_value_range (vr0
, vr0
->type
, vr0
->min
, vr0
->max
,
8668 if (vr0
->type
== VR_VARYING
)
8671 /* The resulting set of equivalences is always the intersection of
8673 if (vr0
->equiv
&& vr1
->equiv
&& vr0
->equiv
!= vr1
->equiv
)
8674 bitmap_and_into (vr0
->equiv
, vr1
->equiv
);
8675 else if (vr0
->equiv
&& !vr1
->equiv
)
8676 bitmap_clear (vr0
->equiv
);
8680 vrp_meet (value_range
*vr0
, value_range
*vr1
)
8682 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8684 fprintf (dump_file
, "Meeting\n ");
8685 dump_value_range (dump_file
, vr0
);
8686 fprintf (dump_file
, "\nand\n ");
8687 dump_value_range (dump_file
, vr1
);
8688 fprintf (dump_file
, "\n");
8690 vrp_meet_1 (vr0
, vr1
);
8691 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8693 fprintf (dump_file
, "to\n ");
8694 dump_value_range (dump_file
, vr0
);
8695 fprintf (dump_file
, "\n");
8700 /* Visit all arguments for PHI node PHI that flow through executable
8701 edges. If a valid value range can be derived from all the incoming
8702 value ranges, set a new range for the LHS of PHI. */
8704 static enum ssa_prop_result
8705 vrp_visit_phi_node (gphi
*phi
)
8708 tree lhs
= PHI_RESULT (phi
);
8709 value_range
*lhs_vr
= get_value_range (lhs
);
8710 value_range vr_result
= VR_INITIALIZER
;
8712 int edges
, old_edges
;
8715 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8717 fprintf (dump_file
, "\nVisiting PHI node: ");
8718 print_gimple_stmt (dump_file
, phi
, 0, dump_flags
);
8722 for (i
= 0; i
< gimple_phi_num_args (phi
); i
++)
8724 edge e
= gimple_phi_arg_edge (phi
, i
);
8726 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8729 " Argument #%d (%d -> %d %sexecutable)\n",
8730 (int) i
, e
->src
->index
, e
->dest
->index
,
8731 (e
->flags
& EDGE_EXECUTABLE
) ? "" : "not ");
8734 if (e
->flags
& EDGE_EXECUTABLE
)
8736 tree arg
= PHI_ARG_DEF (phi
, i
);
8741 if (TREE_CODE (arg
) == SSA_NAME
)
8743 vr_arg
= *(get_value_range (arg
));
8744 /* Do not allow equivalences or symbolic ranges to leak in from
8745 backedges. That creates invalid equivalencies.
8746 See PR53465 and PR54767. */
8747 if (e
->flags
& EDGE_DFS_BACK
)
8749 if (vr_arg
.type
== VR_RANGE
8750 || vr_arg
.type
== VR_ANTI_RANGE
)
8752 vr_arg
.equiv
= NULL
;
8753 if (symbolic_range_p (&vr_arg
))
8755 vr_arg
.type
= VR_VARYING
;
8756 vr_arg
.min
= NULL_TREE
;
8757 vr_arg
.max
= NULL_TREE
;
8763 /* If the non-backedge arguments range is VR_VARYING then
8764 we can still try recording a simple equivalence. */
8765 if (vr_arg
.type
== VR_VARYING
)
8767 vr_arg
.type
= VR_RANGE
;
8770 vr_arg
.equiv
= NULL
;
8776 if (TREE_OVERFLOW_P (arg
))
8777 arg
= drop_tree_overflow (arg
);
8779 vr_arg
.type
= VR_RANGE
;
8782 vr_arg
.equiv
= NULL
;
8785 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8787 fprintf (dump_file
, "\t");
8788 print_generic_expr (dump_file
, arg
, dump_flags
);
8789 fprintf (dump_file
, ": ");
8790 dump_value_range (dump_file
, &vr_arg
);
8791 fprintf (dump_file
, "\n");
8795 copy_value_range (&vr_result
, &vr_arg
);
8797 vrp_meet (&vr_result
, &vr_arg
);
8800 if (vr_result
.type
== VR_VARYING
)
8805 if (vr_result
.type
== VR_VARYING
)
8807 else if (vr_result
.type
== VR_UNDEFINED
)
8810 old_edges
= vr_phi_edge_counts
[SSA_NAME_VERSION (lhs
)];
8811 vr_phi_edge_counts
[SSA_NAME_VERSION (lhs
)] = edges
;
8813 /* To prevent infinite iterations in the algorithm, derive ranges
8814 when the new value is slightly bigger or smaller than the
8815 previous one. We don't do this if we have seen a new executable
8816 edge; this helps us avoid an overflow infinity for conditionals
8817 which are not in a loop. If the old value-range was VR_UNDEFINED
8818 use the updated range and iterate one more time. */
8820 && gimple_phi_num_args (phi
) > 1
8821 && edges
== old_edges
8822 && lhs_vr
->type
!= VR_UNDEFINED
)
8824 /* Compare old and new ranges, fall back to varying if the
8825 values are not comparable. */
8826 int cmp_min
= compare_values (lhs_vr
->min
, vr_result
.min
);
8829 int cmp_max
= compare_values (lhs_vr
->max
, vr_result
.max
);
8833 /* For non VR_RANGE or for pointers fall back to varying if
8834 the range changed. */
8835 if ((lhs_vr
->type
!= VR_RANGE
|| vr_result
.type
!= VR_RANGE
8836 || POINTER_TYPE_P (TREE_TYPE (lhs
)))
8837 && (cmp_min
!= 0 || cmp_max
!= 0))
8840 /* If the new minimum is larger than the previous one
8841 retain the old value. If the new minimum value is smaller
8842 than the previous one and not -INF go all the way to -INF + 1.
8843 In the first case, to avoid infinite bouncing between different
8844 minimums, and in the other case to avoid iterating millions of
8845 times to reach -INF. Going to -INF + 1 also lets the following
8846 iteration compute whether there will be any overflow, at the
8847 expense of one additional iteration. */
8849 vr_result
.min
= lhs_vr
->min
;
8850 else if (cmp_min
> 0
8851 && !vrp_val_is_min (vr_result
.min
))
8853 = int_const_binop (PLUS_EXPR
,
8854 vrp_val_min (TREE_TYPE (vr_result
.min
)),
8855 build_int_cst (TREE_TYPE (vr_result
.min
), 1));
8857 /* Similarly for the maximum value. */
8859 vr_result
.max
= lhs_vr
->max
;
8860 else if (cmp_max
< 0
8861 && !vrp_val_is_max (vr_result
.max
))
8863 = int_const_binop (MINUS_EXPR
,
8864 vrp_val_max (TREE_TYPE (vr_result
.min
)),
8865 build_int_cst (TREE_TYPE (vr_result
.min
), 1));
8867 /* If we dropped either bound to +-INF then if this is a loop
8868 PHI node SCEV may known more about its value-range. */
8869 if (cmp_min
> 0 || cmp_min
< 0
8870 || cmp_max
< 0 || cmp_max
> 0)
8873 goto infinite_check
;
8876 /* If the new range is different than the previous value, keep
8879 if (update_value_range (lhs
, &vr_result
))
8881 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8883 fprintf (dump_file
, "Found new range for ");
8884 print_generic_expr (dump_file
, lhs
, 0);
8885 fprintf (dump_file
, ": ");
8886 dump_value_range (dump_file
, &vr_result
);
8887 fprintf (dump_file
, "\n");
8890 if (vr_result
.type
== VR_VARYING
)
8891 return SSA_PROP_VARYING
;
8893 return SSA_PROP_INTERESTING
;
8896 /* Nothing changed, don't add outgoing edges. */
8897 return SSA_PROP_NOT_INTERESTING
;
8900 set_value_range_to_varying (&vr_result
);
8903 /* If this is a loop PHI node SCEV may known more about its value-range.
8904 scev_check can be reached from two paths, one is a fall through from above
8905 "varying" label, the other is direct goto from code block which tries to
8906 avoid infinite simulation. */
8907 if ((l
= loop_containing_stmt (phi
))
8908 && l
->header
== gimple_bb (phi
))
8909 adjust_range_with_scev (&vr_result
, l
, phi
, lhs
);
8912 /* If we will end up with a (-INF, +INF) range, set it to
8913 VARYING. Same if the previous max value was invalid for
8914 the type and we end up with vr_result.min > vr_result.max. */
8915 if ((vr_result
.type
== VR_RANGE
|| vr_result
.type
== VR_ANTI_RANGE
)
8916 && !((vrp_val_is_max (vr_result
.max
) && vrp_val_is_min (vr_result
.min
))
8917 || compare_values (vr_result
.min
, vr_result
.max
) > 0))
8920 /* No match found. Set the LHS to VARYING. */
8921 set_value_range_to_varying (lhs_vr
);
8922 return SSA_PROP_VARYING
;
8925 /* Simplify boolean operations if the source is known
8926 to be already a boolean. */
8928 simplify_truth_ops_using_ranges (gimple_stmt_iterator
*gsi
, gimple
*stmt
)
8930 enum tree_code rhs_code
= gimple_assign_rhs_code (stmt
);
8932 bool need_conversion
;
8934 /* We handle only !=/== case here. */
8935 gcc_assert (rhs_code
== EQ_EXPR
|| rhs_code
== NE_EXPR
);
8937 op0
= gimple_assign_rhs1 (stmt
);
8938 if (!op_with_boolean_value_range_p (op0
))
8941 op1
= gimple_assign_rhs2 (stmt
);
8942 if (!op_with_boolean_value_range_p (op1
))
8945 /* Reduce number of cases to handle to NE_EXPR. As there is no
8946 BIT_XNOR_EXPR we cannot replace A == B with a single statement. */
8947 if (rhs_code
== EQ_EXPR
)
8949 if (TREE_CODE (op1
) == INTEGER_CST
)
8950 op1
= int_const_binop (BIT_XOR_EXPR
, op1
,
8951 build_int_cst (TREE_TYPE (op1
), 1));
8956 lhs
= gimple_assign_lhs (stmt
);
8958 = !useless_type_conversion_p (TREE_TYPE (lhs
), TREE_TYPE (op0
));
8960 /* Make sure to not sign-extend a 1-bit 1 when converting the result. */
8962 && !TYPE_UNSIGNED (TREE_TYPE (op0
))
8963 && TYPE_PRECISION (TREE_TYPE (op0
)) == 1
8964 && TYPE_PRECISION (TREE_TYPE (lhs
)) > 1)
8967 /* For A != 0 we can substitute A itself. */
8968 if (integer_zerop (op1
))
8969 gimple_assign_set_rhs_with_ops (gsi
,
8971 ? NOP_EXPR
: TREE_CODE (op0
), op0
);
8972 /* For A != B we substitute A ^ B. Either with conversion. */
8973 else if (need_conversion
)
8975 tree tem
= make_ssa_name (TREE_TYPE (op0
));
8977 = gimple_build_assign (tem
, BIT_XOR_EXPR
, op0
, op1
);
8978 gsi_insert_before (gsi
, newop
, GSI_SAME_STMT
);
8979 gimple_assign_set_rhs_with_ops (gsi
, NOP_EXPR
, tem
);
8983 gimple_assign_set_rhs_with_ops (gsi
, BIT_XOR_EXPR
, op0
, op1
);
8984 update_stmt (gsi_stmt (*gsi
));
8989 /* Simplify a division or modulo operator to a right shift or
8990 bitwise and if the first operand is unsigned or is greater
8991 than zero and the second operand is an exact power of two.
8992 For TRUNC_MOD_EXPR op0 % op1 with constant op1, optimize it
8993 into just op0 if op0's range is known to be a subset of
8994 [-op1 + 1, op1 - 1] for signed and [0, op1 - 1] for unsigned
8998 simplify_div_or_mod_using_ranges (gimple_stmt_iterator
*gsi
, gimple
*stmt
)
9000 enum tree_code rhs_code
= gimple_assign_rhs_code (stmt
);
9002 tree op0
= gimple_assign_rhs1 (stmt
);
9003 tree op1
= gimple_assign_rhs2 (stmt
);
9004 value_range
*vr
= get_value_range (op0
);
9006 if (rhs_code
== TRUNC_MOD_EXPR
9007 && TREE_CODE (op1
) == INTEGER_CST
9008 && tree_int_cst_sgn (op1
) == 1
9009 && range_int_cst_p (vr
)
9010 && tree_int_cst_lt (vr
->max
, op1
))
9012 if (TYPE_UNSIGNED (TREE_TYPE (op0
))
9013 || tree_int_cst_sgn (vr
->min
) >= 0
9014 || tree_int_cst_lt (fold_unary (NEGATE_EXPR
, TREE_TYPE (op1
), op1
),
9017 /* If op0 already has the range op0 % op1 has,
9018 then TRUNC_MOD_EXPR won't change anything. */
9019 gimple_stmt_iterator gsi
= gsi_for_stmt (stmt
);
9020 gimple_assign_set_rhs_from_tree (&gsi
, op0
);
9026 if (!integer_pow2p (op1
))
9028 /* X % -Y can be only optimized into X % Y either if
9029 X is not INT_MIN, or Y is not -1. Fold it now, as after
9030 remove_range_assertions the range info might be not available
9032 if (rhs_code
== TRUNC_MOD_EXPR
9033 && fold_stmt (gsi
, follow_single_use_edges
))
9038 if (TYPE_UNSIGNED (TREE_TYPE (op0
)))
9039 val
= integer_one_node
;
9044 val
= compare_range_with_value (GE_EXPR
, vr
, integer_zero_node
, &sop
);
9048 && integer_onep (val
)
9049 && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC
))
9051 location_t location
;
9053 if (!gimple_has_location (stmt
))
9054 location
= input_location
;
9056 location
= gimple_location (stmt
);
9057 warning_at (location
, OPT_Wstrict_overflow
,
9058 "assuming signed overflow does not occur when "
9059 "simplifying %</%> or %<%%%> to %<>>%> or %<&%>");
9063 if (val
&& integer_onep (val
))
9067 if (rhs_code
== TRUNC_DIV_EXPR
)
9069 t
= build_int_cst (integer_type_node
, tree_log2 (op1
));
9070 gimple_assign_set_rhs_code (stmt
, RSHIFT_EXPR
);
9071 gimple_assign_set_rhs1 (stmt
, op0
);
9072 gimple_assign_set_rhs2 (stmt
, t
);
9076 t
= build_int_cst (TREE_TYPE (op1
), 1);
9077 t
= int_const_binop (MINUS_EXPR
, op1
, t
);
9078 t
= fold_convert (TREE_TYPE (op0
), t
);
9080 gimple_assign_set_rhs_code (stmt
, BIT_AND_EXPR
);
9081 gimple_assign_set_rhs1 (stmt
, op0
);
9082 gimple_assign_set_rhs2 (stmt
, t
);
9092 /* Simplify a min or max if the ranges of the two operands are
9093 disjoint. Return true if we do simplify. */
9096 simplify_min_or_max_using_ranges (gimple
*stmt
)
9098 tree op0
= gimple_assign_rhs1 (stmt
);
9099 tree op1
= gimple_assign_rhs2 (stmt
);
9103 val
= (vrp_evaluate_conditional_warnv_with_ops_using_ranges
9104 (LE_EXPR
, op0
, op1
, &sop
));
9108 val
= (vrp_evaluate_conditional_warnv_with_ops_using_ranges
9109 (LT_EXPR
, op0
, op1
, &sop
));
9114 if (sop
&& issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC
))
9116 location_t location
;
9118 if (!gimple_has_location (stmt
))
9119 location
= input_location
;
9121 location
= gimple_location (stmt
);
9122 warning_at (location
, OPT_Wstrict_overflow
,
9123 "assuming signed overflow does not occur when "
9124 "simplifying %<min/max (X,Y)%> to %<X%> or %<Y%>");
9127 /* VAL == TRUE -> OP0 < or <= op1
9128 VAL == FALSE -> OP0 > or >= op1. */
9129 tree res
= ((gimple_assign_rhs_code (stmt
) == MAX_EXPR
)
9130 == integer_zerop (val
)) ? op0
: op1
;
9131 gimple_stmt_iterator gsi
= gsi_for_stmt (stmt
);
9132 gimple_assign_set_rhs_from_tree (&gsi
, res
);
9140 /* If the operand to an ABS_EXPR is >= 0, then eliminate the
9141 ABS_EXPR. If the operand is <= 0, then simplify the
9142 ABS_EXPR into a NEGATE_EXPR. */
9145 simplify_abs_using_ranges (gimple
*stmt
)
9147 tree op
= gimple_assign_rhs1 (stmt
);
9148 value_range
*vr
= get_value_range (op
);
9155 val
= compare_range_with_value (LE_EXPR
, vr
, integer_zero_node
, &sop
);
9158 /* The range is neither <= 0 nor > 0. Now see if it is
9159 either < 0 or >= 0. */
9161 val
= compare_range_with_value (LT_EXPR
, vr
, integer_zero_node
,
9167 if (sop
&& issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC
))
9169 location_t location
;
9171 if (!gimple_has_location (stmt
))
9172 location
= input_location
;
9174 location
= gimple_location (stmt
);
9175 warning_at (location
, OPT_Wstrict_overflow
,
9176 "assuming signed overflow does not occur when "
9177 "simplifying %<abs (X)%> to %<X%> or %<-X%>");
9180 gimple_assign_set_rhs1 (stmt
, op
);
9181 if (integer_zerop (val
))
9182 gimple_assign_set_rhs_code (stmt
, SSA_NAME
);
9184 gimple_assign_set_rhs_code (stmt
, NEGATE_EXPR
);
9193 /* Optimize away redundant BIT_AND_EXPR and BIT_IOR_EXPR.
9194 If all the bits that are being cleared by & are already
9195 known to be zero from VR, or all the bits that are being
9196 set by | are already known to be one from VR, the bit
9197 operation is redundant. */
9200 simplify_bit_ops_using_ranges (gimple_stmt_iterator
*gsi
, gimple
*stmt
)
9202 tree op0
= gimple_assign_rhs1 (stmt
);
9203 tree op1
= gimple_assign_rhs2 (stmt
);
9204 tree op
= NULL_TREE
;
9205 value_range vr0
= VR_INITIALIZER
;
9206 value_range vr1
= VR_INITIALIZER
;
9207 wide_int may_be_nonzero0
, may_be_nonzero1
;
9208 wide_int must_be_nonzero0
, must_be_nonzero1
;
9211 if (TREE_CODE (op0
) == SSA_NAME
)
9212 vr0
= *(get_value_range (op0
));
9213 else if (is_gimple_min_invariant (op0
))
9214 set_value_range_to_value (&vr0
, op0
, NULL
);
9218 if (TREE_CODE (op1
) == SSA_NAME
)
9219 vr1
= *(get_value_range (op1
));
9220 else if (is_gimple_min_invariant (op1
))
9221 set_value_range_to_value (&vr1
, op1
, NULL
);
9225 if (!zero_nonzero_bits_from_vr (TREE_TYPE (op0
), &vr0
, &may_be_nonzero0
,
9228 if (!zero_nonzero_bits_from_vr (TREE_TYPE (op1
), &vr1
, &may_be_nonzero1
,
9232 switch (gimple_assign_rhs_code (stmt
))
9235 mask
= may_be_nonzero0
.and_not (must_be_nonzero1
);
9241 mask
= may_be_nonzero1
.and_not (must_be_nonzero0
);
9249 mask
= may_be_nonzero0
.and_not (must_be_nonzero1
);
9255 mask
= may_be_nonzero1
.and_not (must_be_nonzero0
);
9266 if (op
== NULL_TREE
)
9269 gimple_assign_set_rhs_with_ops (gsi
, TREE_CODE (op
), op
);
9270 update_stmt (gsi_stmt (*gsi
));
9274 /* We are comparing trees OP0 and OP1 using COND_CODE. OP0 has
9275 a known value range VR.
9277 If there is one and only one value which will satisfy the
9278 conditional, then return that value. Else return NULL.
9280 If signed overflow must be undefined for the value to satisfy
9281 the conditional, then set *STRICT_OVERFLOW_P to true. */
9284 test_for_singularity (enum tree_code cond_code
, tree op0
,
9285 tree op1
, value_range
*vr
,
9286 bool *strict_overflow_p
)
9291 /* Extract minimum/maximum values which satisfy the conditional as it was
9293 if (cond_code
== LE_EXPR
|| cond_code
== LT_EXPR
)
9295 /* This should not be negative infinity; there is no overflow
9297 min
= TYPE_MIN_VALUE (TREE_TYPE (op0
));
9300 if (cond_code
== LT_EXPR
&& !is_overflow_infinity (max
))
9302 tree one
= build_int_cst (TREE_TYPE (op0
), 1);
9303 max
= fold_build2 (MINUS_EXPR
, TREE_TYPE (op0
), max
, one
);
9305 TREE_NO_WARNING (max
) = 1;
9308 else if (cond_code
== GE_EXPR
|| cond_code
== GT_EXPR
)
9310 /* This should not be positive infinity; there is no overflow
9312 max
= TYPE_MAX_VALUE (TREE_TYPE (op0
));
9315 if (cond_code
== GT_EXPR
&& !is_overflow_infinity (min
))
9317 tree one
= build_int_cst (TREE_TYPE (op0
), 1);
9318 min
= fold_build2 (PLUS_EXPR
, TREE_TYPE (op0
), min
, one
);
9320 TREE_NO_WARNING (min
) = 1;
9324 /* Now refine the minimum and maximum values using any
9325 value range information we have for op0. */
9328 if (compare_values (vr
->min
, min
) == 1)
9330 if (compare_values (vr
->max
, max
) == -1)
9333 /* If the new min/max values have converged to a single value,
9334 then there is only one value which can satisfy the condition,
9335 return that value. */
9336 if (operand_equal_p (min
, max
, 0) && is_gimple_min_invariant (min
))
9338 if ((cond_code
== LE_EXPR
|| cond_code
== LT_EXPR
)
9339 && is_overflow_infinity (vr
->max
))
9340 *strict_overflow_p
= true;
9341 if ((cond_code
== GE_EXPR
|| cond_code
== GT_EXPR
)
9342 && is_overflow_infinity (vr
->min
))
9343 *strict_overflow_p
= true;
9351 /* Return whether the value range *VR fits in an integer type specified
9352 by PRECISION and UNSIGNED_P. */
9355 range_fits_type_p (value_range
*vr
, unsigned dest_precision
, signop dest_sgn
)
9358 unsigned src_precision
;
9362 /* We can only handle integral and pointer types. */
9363 src_type
= TREE_TYPE (vr
->min
);
9364 if (!INTEGRAL_TYPE_P (src_type
)
9365 && !POINTER_TYPE_P (src_type
))
9368 /* An extension is fine unless VR is SIGNED and dest_sgn is UNSIGNED,
9369 and so is an identity transform. */
9370 src_precision
= TYPE_PRECISION (TREE_TYPE (vr
->min
));
9371 src_sgn
= TYPE_SIGN (src_type
);
9372 if ((src_precision
< dest_precision
9373 && !(dest_sgn
== UNSIGNED
&& src_sgn
== SIGNED
))
9374 || (src_precision
== dest_precision
&& src_sgn
== dest_sgn
))
9377 /* Now we can only handle ranges with constant bounds. */
9378 if (vr
->type
!= VR_RANGE
9379 || TREE_CODE (vr
->min
) != INTEGER_CST
9380 || TREE_CODE (vr
->max
) != INTEGER_CST
)
9383 /* For sign changes, the MSB of the wide_int has to be clear.
9384 An unsigned value with its MSB set cannot be represented by
9385 a signed wide_int, while a negative value cannot be represented
9386 by an unsigned wide_int. */
9387 if (src_sgn
!= dest_sgn
9388 && (wi::lts_p (vr
->min
, 0) || wi::lts_p (vr
->max
, 0)))
9391 /* Then we can perform the conversion on both ends and compare
9392 the result for equality. */
9393 tem
= wi::ext (wi::to_widest (vr
->min
), dest_precision
, dest_sgn
);
9394 if (tem
!= wi::to_widest (vr
->min
))
9396 tem
= wi::ext (wi::to_widest (vr
->max
), dest_precision
, dest_sgn
);
9397 if (tem
!= wi::to_widest (vr
->max
))
9403 /* Simplify a conditional using a relational operator to an equality
9404 test if the range information indicates only one value can satisfy
9405 the original conditional. */
9408 simplify_cond_using_ranges (gcond
*stmt
)
9410 tree op0
= gimple_cond_lhs (stmt
);
9411 tree op1
= gimple_cond_rhs (stmt
);
9412 enum tree_code cond_code
= gimple_cond_code (stmt
);
9414 if (cond_code
!= NE_EXPR
9415 && cond_code
!= EQ_EXPR
9416 && TREE_CODE (op0
) == SSA_NAME
9417 && INTEGRAL_TYPE_P (TREE_TYPE (op0
))
9418 && is_gimple_min_invariant (op1
))
9420 value_range
*vr
= get_value_range (op0
);
9422 /* If we have range information for OP0, then we might be
9423 able to simplify this conditional. */
9424 if (vr
->type
== VR_RANGE
)
9426 enum warn_strict_overflow_code wc
= WARN_STRICT_OVERFLOW_COMPARISON
;
9428 tree new_tree
= test_for_singularity (cond_code
, op0
, op1
, vr
, &sop
);
9431 && (!sop
|| TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0
))))
9435 fprintf (dump_file
, "Simplified relational ");
9436 print_gimple_stmt (dump_file
, stmt
, 0, 0);
9437 fprintf (dump_file
, " into ");
9440 gimple_cond_set_code (stmt
, EQ_EXPR
);
9441 gimple_cond_set_lhs (stmt
, op0
);
9442 gimple_cond_set_rhs (stmt
, new_tree
);
9448 print_gimple_stmt (dump_file
, stmt
, 0, 0);
9449 fprintf (dump_file
, "\n");
9452 if (sop
&& issue_strict_overflow_warning (wc
))
9454 location_t location
= input_location
;
9455 if (gimple_has_location (stmt
))
9456 location
= gimple_location (stmt
);
9458 warning_at (location
, OPT_Wstrict_overflow
,
9459 "assuming signed overflow does not occur when "
9460 "simplifying conditional");
9466 /* Try again after inverting the condition. We only deal
9467 with integral types here, so no need to worry about
9468 issues with inverting FP comparisons. */
9470 new_tree
= test_for_singularity
9471 (invert_tree_comparison (cond_code
, false),
9472 op0
, op1
, vr
, &sop
);
9475 && (!sop
|| TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0
))))
9479 fprintf (dump_file
, "Simplified relational ");
9480 print_gimple_stmt (dump_file
, stmt
, 0, 0);
9481 fprintf (dump_file
, " into ");
9484 gimple_cond_set_code (stmt
, NE_EXPR
);
9485 gimple_cond_set_lhs (stmt
, op0
);
9486 gimple_cond_set_rhs (stmt
, new_tree
);
9492 print_gimple_stmt (dump_file
, stmt
, 0, 0);
9493 fprintf (dump_file
, "\n");
9496 if (sop
&& issue_strict_overflow_warning (wc
))
9498 location_t location
= input_location
;
9499 if (gimple_has_location (stmt
))
9500 location
= gimple_location (stmt
);
9502 warning_at (location
, OPT_Wstrict_overflow
,
9503 "assuming signed overflow does not occur when "
9504 "simplifying conditional");
9512 /* If we have a comparison of an SSA_NAME (OP0) against a constant,
9513 see if OP0 was set by a type conversion where the source of
9514 the conversion is another SSA_NAME with a range that fits
9515 into the range of OP0's type.
9517 If so, the conversion is redundant as the earlier SSA_NAME can be
9518 used for the comparison directly if we just massage the constant in the
9520 if (TREE_CODE (op0
) == SSA_NAME
9521 && TREE_CODE (op1
) == INTEGER_CST
)
9523 gimple
*def_stmt
= SSA_NAME_DEF_STMT (op0
);
9526 if (!is_gimple_assign (def_stmt
)
9527 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt
)))
9530 innerop
= gimple_assign_rhs1 (def_stmt
);
9532 if (TREE_CODE (innerop
) == SSA_NAME
9533 && !POINTER_TYPE_P (TREE_TYPE (innerop
))
9534 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (innerop
)
9535 && desired_pro_or_demotion_p (TREE_TYPE (innerop
), TREE_TYPE (op0
)))
9537 value_range
*vr
= get_value_range (innerop
);
9539 if (range_int_cst_p (vr
)
9540 && range_fits_type_p (vr
,
9541 TYPE_PRECISION (TREE_TYPE (op0
)),
9542 TYPE_SIGN (TREE_TYPE (op0
)))
9543 && int_fits_type_p (op1
, TREE_TYPE (innerop
))
9544 /* The range must not have overflowed, or if it did overflow
9545 we must not be wrapping/trapping overflow and optimizing
9546 with strict overflow semantics. */
9547 && ((!is_negative_overflow_infinity (vr
->min
)
9548 && !is_positive_overflow_infinity (vr
->max
))
9549 || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (innerop
))))
9551 /* If the range overflowed and the user has asked for warnings
9552 when strict overflow semantics were used to optimize code,
9553 issue an appropriate warning. */
9554 if (cond_code
!= EQ_EXPR
&& cond_code
!= NE_EXPR
9555 && (is_negative_overflow_infinity (vr
->min
)
9556 || is_positive_overflow_infinity (vr
->max
))
9557 && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_CONDITIONAL
))
9559 location_t location
;
9561 if (!gimple_has_location (stmt
))
9562 location
= input_location
;
9564 location
= gimple_location (stmt
);
9565 warning_at (location
, OPT_Wstrict_overflow
,
9566 "assuming signed overflow does not occur when "
9567 "simplifying conditional");
9570 tree newconst
= fold_convert (TREE_TYPE (innerop
), op1
);
9571 gimple_cond_set_lhs (stmt
, innerop
);
9572 gimple_cond_set_rhs (stmt
, newconst
);
9581 /* Simplify a switch statement using the value range of the switch
9585 simplify_switch_using_ranges (gswitch
*stmt
)
9587 tree op
= gimple_switch_index (stmt
);
9592 size_t i
= 0, j
= 0, n
, n2
;
9595 size_t k
= 1, l
= 0;
9597 if (TREE_CODE (op
) == SSA_NAME
)
9599 vr
= get_value_range (op
);
9601 /* We can only handle integer ranges. */
9602 if ((vr
->type
!= VR_RANGE
9603 && vr
->type
!= VR_ANTI_RANGE
)
9604 || symbolic_range_p (vr
))
9607 /* Find case label for min/max of the value range. */
9608 take_default
= !find_case_label_ranges (stmt
, vr
, &i
, &j
, &k
, &l
);
9610 else if (TREE_CODE (op
) == INTEGER_CST
)
9612 take_default
= !find_case_label_index (stmt
, 1, op
, &i
);
9626 n
= gimple_switch_num_labels (stmt
);
9628 /* Bail out if this is just all edges taken. */
9634 /* Build a new vector of taken case labels. */
9635 vec2
= make_tree_vec (j
- i
+ 1 + l
- k
+ 1 + (int)take_default
);
9638 /* Add the default edge, if necessary. */
9640 TREE_VEC_ELT (vec2
, n2
++) = gimple_switch_default_label (stmt
);
9642 for (; i
<= j
; ++i
, ++n2
)
9643 TREE_VEC_ELT (vec2
, n2
) = gimple_switch_label (stmt
, i
);
9645 for (; k
<= l
; ++k
, ++n2
)
9646 TREE_VEC_ELT (vec2
, n2
) = gimple_switch_label (stmt
, k
);
9648 /* Mark needed edges. */
9649 for (i
= 0; i
< n2
; ++i
)
9651 e
= find_edge (gimple_bb (stmt
),
9652 label_to_block (CASE_LABEL (TREE_VEC_ELT (vec2
, i
))));
9653 e
->aux
= (void *)-1;
9656 /* Queue not needed edges for later removal. */
9657 FOR_EACH_EDGE (e
, ei
, gimple_bb (stmt
)->succs
)
9659 if (e
->aux
== (void *)-1)
9665 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
9667 fprintf (dump_file
, "removing unreachable case label\n");
9669 to_remove_edges
.safe_push (e
);
9670 e
->flags
&= ~EDGE_EXECUTABLE
;
9673 /* And queue an update for the stmt. */
9676 to_update_switch_stmts
.safe_push (su
);
9680 /* Simplify an integral conversion from an SSA name in STMT. */
9683 simplify_conversion_using_ranges (gimple
*stmt
)
9685 tree innerop
, middleop
, finaltype
;
9687 value_range
*innervr
;
9688 signop inner_sgn
, middle_sgn
, final_sgn
;
9689 unsigned inner_prec
, middle_prec
, final_prec
;
9690 widest_int innermin
, innermed
, innermax
, middlemin
, middlemed
, middlemax
;
9692 finaltype
= TREE_TYPE (gimple_assign_lhs (stmt
));
9693 if (!INTEGRAL_TYPE_P (finaltype
))
9695 middleop
= gimple_assign_rhs1 (stmt
);
9696 def_stmt
= SSA_NAME_DEF_STMT (middleop
);
9697 if (!is_gimple_assign (def_stmt
)
9698 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt
)))
9700 innerop
= gimple_assign_rhs1 (def_stmt
);
9701 if (TREE_CODE (innerop
) != SSA_NAME
9702 || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (innerop
))
9705 /* Get the value-range of the inner operand. */
9706 innervr
= get_value_range (innerop
);
9707 if (innervr
->type
!= VR_RANGE
9708 || TREE_CODE (innervr
->min
) != INTEGER_CST
9709 || TREE_CODE (innervr
->max
) != INTEGER_CST
)
9712 /* Simulate the conversion chain to check if the result is equal if
9713 the middle conversion is removed. */
9714 innermin
= wi::to_widest (innervr
->min
);
9715 innermax
= wi::to_widest (innervr
->max
);
9717 inner_prec
= TYPE_PRECISION (TREE_TYPE (innerop
));
9718 middle_prec
= TYPE_PRECISION (TREE_TYPE (middleop
));
9719 final_prec
= TYPE_PRECISION (finaltype
);
9721 /* If the first conversion is not injective, the second must not
9723 if (wi::gtu_p (innermax
- innermin
,
9724 wi::mask
<widest_int
> (middle_prec
, false))
9725 && middle_prec
< final_prec
)
9727 /* We also want a medium value so that we can track the effect that
9728 narrowing conversions with sign change have. */
9729 inner_sgn
= TYPE_SIGN (TREE_TYPE (innerop
));
9730 if (inner_sgn
== UNSIGNED
)
9731 innermed
= wi::shifted_mask
<widest_int
> (1, inner_prec
- 1, false);
9734 if (wi::cmp (innermin
, innermed
, inner_sgn
) >= 0
9735 || wi::cmp (innermed
, innermax
, inner_sgn
) >= 0)
9736 innermed
= innermin
;
9738 middle_sgn
= TYPE_SIGN (TREE_TYPE (middleop
));
9739 middlemin
= wi::ext (innermin
, middle_prec
, middle_sgn
);
9740 middlemed
= wi::ext (innermed
, middle_prec
, middle_sgn
);
9741 middlemax
= wi::ext (innermax
, middle_prec
, middle_sgn
);
9743 /* Require that the final conversion applied to both the original
9744 and the intermediate range produces the same result. */
9745 final_sgn
= TYPE_SIGN (finaltype
);
9746 if (wi::ext (middlemin
, final_prec
, final_sgn
)
9747 != wi::ext (innermin
, final_prec
, final_sgn
)
9748 || wi::ext (middlemed
, final_prec
, final_sgn
)
9749 != wi::ext (innermed
, final_prec
, final_sgn
)
9750 || wi::ext (middlemax
, final_prec
, final_sgn
)
9751 != wi::ext (innermax
, final_prec
, final_sgn
))
9754 gimple_assign_set_rhs1 (stmt
, innerop
);
9759 /* Simplify a conversion from integral SSA name to float in STMT. */
9762 simplify_float_conversion_using_ranges (gimple_stmt_iterator
*gsi
,
9765 tree rhs1
= gimple_assign_rhs1 (stmt
);
9766 value_range
*vr
= get_value_range (rhs1
);
9767 machine_mode fltmode
= TYPE_MODE (TREE_TYPE (gimple_assign_lhs (stmt
)));
9772 /* We can only handle constant ranges. */
9773 if (vr
->type
!= VR_RANGE
9774 || TREE_CODE (vr
->min
) != INTEGER_CST
9775 || TREE_CODE (vr
->max
) != INTEGER_CST
)
9778 /* First check if we can use a signed type in place of an unsigned. */
9779 if (TYPE_UNSIGNED (TREE_TYPE (rhs1
))
9780 && (can_float_p (fltmode
, TYPE_MODE (TREE_TYPE (rhs1
)), 0)
9781 != CODE_FOR_nothing
)
9782 && range_fits_type_p (vr
, TYPE_PRECISION (TREE_TYPE (rhs1
)), SIGNED
))
9783 mode
= TYPE_MODE (TREE_TYPE (rhs1
));
9784 /* If we can do the conversion in the current input mode do nothing. */
9785 else if (can_float_p (fltmode
, TYPE_MODE (TREE_TYPE (rhs1
)),
9786 TYPE_UNSIGNED (TREE_TYPE (rhs1
))) != CODE_FOR_nothing
)
9788 /* Otherwise search for a mode we can use, starting from the narrowest
9789 integer mode available. */
9792 mode
= GET_CLASS_NARROWEST_MODE (MODE_INT
);
9795 /* If we cannot do a signed conversion to float from mode
9796 or if the value-range does not fit in the signed type
9797 try with a wider mode. */
9798 if (can_float_p (fltmode
, mode
, 0) != CODE_FOR_nothing
9799 && range_fits_type_p (vr
, GET_MODE_PRECISION (mode
), SIGNED
))
9802 mode
= GET_MODE_WIDER_MODE (mode
);
9803 /* But do not widen the input. Instead leave that to the
9804 optabs expansion code. */
9805 if (GET_MODE_PRECISION (mode
) > TYPE_PRECISION (TREE_TYPE (rhs1
)))
9808 while (mode
!= VOIDmode
);
9809 if (mode
== VOIDmode
)
9813 /* It works, insert a truncation or sign-change before the
9814 float conversion. */
9815 tem
= make_ssa_name (build_nonstandard_integer_type
9816 (GET_MODE_PRECISION (mode
), 0));
9817 conv
= gimple_build_assign (tem
, NOP_EXPR
, rhs1
);
9818 gsi_insert_before (gsi
, conv
, GSI_SAME_STMT
);
9819 gimple_assign_set_rhs1 (stmt
, tem
);
9825 /* Simplify an internal fn call using ranges if possible. */
9828 simplify_internal_call_using_ranges (gimple_stmt_iterator
*gsi
, gimple
*stmt
)
9830 enum tree_code subcode
;
9831 bool is_ubsan
= false;
9833 switch (gimple_call_internal_fn (stmt
))
9835 case IFN_UBSAN_CHECK_ADD
:
9836 subcode
= PLUS_EXPR
;
9839 case IFN_UBSAN_CHECK_SUB
:
9840 subcode
= MINUS_EXPR
;
9843 case IFN_UBSAN_CHECK_MUL
:
9844 subcode
= MULT_EXPR
;
9847 case IFN_ADD_OVERFLOW
:
9848 subcode
= PLUS_EXPR
;
9850 case IFN_SUB_OVERFLOW
:
9851 subcode
= MINUS_EXPR
;
9853 case IFN_MUL_OVERFLOW
:
9854 subcode
= MULT_EXPR
;
9860 tree op0
= gimple_call_arg (stmt
, 0);
9861 tree op1
= gimple_call_arg (stmt
, 1);
9864 type
= TREE_TYPE (op0
);
9865 else if (gimple_call_lhs (stmt
) == NULL_TREE
)
9868 type
= TREE_TYPE (TREE_TYPE (gimple_call_lhs (stmt
)));
9869 if (!check_for_binary_op_overflow (subcode
, type
, op0
, op1
, &ovf
)
9870 || (is_ubsan
&& ovf
))
9874 location_t loc
= gimple_location (stmt
);
9876 g
= gimple_build_assign (gimple_call_lhs (stmt
), subcode
, op0
, op1
);
9879 int prec
= TYPE_PRECISION (type
);
9882 || !useless_type_conversion_p (type
, TREE_TYPE (op0
))
9883 || !useless_type_conversion_p (type
, TREE_TYPE (op1
)))
9884 utype
= build_nonstandard_integer_type (prec
, 1);
9885 if (TREE_CODE (op0
) == INTEGER_CST
)
9886 op0
= fold_convert (utype
, op0
);
9887 else if (!useless_type_conversion_p (utype
, TREE_TYPE (op0
)))
9889 g
= gimple_build_assign (make_ssa_name (utype
), NOP_EXPR
, op0
);
9890 gimple_set_location (g
, loc
);
9891 gsi_insert_before (gsi
, g
, GSI_SAME_STMT
);
9892 op0
= gimple_assign_lhs (g
);
9894 if (TREE_CODE (op1
) == INTEGER_CST
)
9895 op1
= fold_convert (utype
, op1
);
9896 else if (!useless_type_conversion_p (utype
, TREE_TYPE (op1
)))
9898 g
= gimple_build_assign (make_ssa_name (utype
), NOP_EXPR
, op1
);
9899 gimple_set_location (g
, loc
);
9900 gsi_insert_before (gsi
, g
, GSI_SAME_STMT
);
9901 op1
= gimple_assign_lhs (g
);
9903 g
= gimple_build_assign (make_ssa_name (utype
), subcode
, op0
, op1
);
9904 gimple_set_location (g
, loc
);
9905 gsi_insert_before (gsi
, g
, GSI_SAME_STMT
);
9908 g
= gimple_build_assign (make_ssa_name (type
), NOP_EXPR
,
9909 gimple_assign_lhs (g
));
9910 gimple_set_location (g
, loc
);
9911 gsi_insert_before (gsi
, g
, GSI_SAME_STMT
);
9913 g
= gimple_build_assign (gimple_call_lhs (stmt
), COMPLEX_EXPR
,
9914 gimple_assign_lhs (g
),
9915 build_int_cst (type
, ovf
));
9917 gimple_set_location (g
, loc
);
9918 gsi_replace (gsi
, g
, false);
9922 /* Simplify STMT using ranges if possible. */
9925 simplify_stmt_using_ranges (gimple_stmt_iterator
*gsi
)
9927 gimple
*stmt
= gsi_stmt (*gsi
);
9928 if (is_gimple_assign (stmt
))
9930 enum tree_code rhs_code
= gimple_assign_rhs_code (stmt
);
9931 tree rhs1
= gimple_assign_rhs1 (stmt
);
9937 /* Transform EQ_EXPR, NE_EXPR into BIT_XOR_EXPR or identity
9938 if the RHS is zero or one, and the LHS are known to be boolean
9940 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
9941 return simplify_truth_ops_using_ranges (gsi
, stmt
);
9944 /* Transform TRUNC_DIV_EXPR and TRUNC_MOD_EXPR into RSHIFT_EXPR
9945 and BIT_AND_EXPR respectively if the first operand is greater
9946 than zero and the second operand is an exact power of two.
9947 Also optimize TRUNC_MOD_EXPR away if the second operand is
9948 constant and the first operand already has the right value
9950 case TRUNC_DIV_EXPR
:
9951 case TRUNC_MOD_EXPR
:
9952 if (TREE_CODE (rhs1
) == SSA_NAME
9953 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
9954 return simplify_div_or_mod_using_ranges (gsi
, stmt
);
9957 /* Transform ABS (X) into X or -X as appropriate. */
9959 if (TREE_CODE (rhs1
) == SSA_NAME
9960 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
9961 return simplify_abs_using_ranges (stmt
);
9966 /* Optimize away BIT_AND_EXPR and BIT_IOR_EXPR
9967 if all the bits being cleared are already cleared or
9968 all the bits being set are already set. */
9969 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
9970 return simplify_bit_ops_using_ranges (gsi
, stmt
);
9974 if (TREE_CODE (rhs1
) == SSA_NAME
9975 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
9976 return simplify_conversion_using_ranges (stmt
);
9980 if (TREE_CODE (rhs1
) == SSA_NAME
9981 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
9982 return simplify_float_conversion_using_ranges (gsi
, stmt
);
9987 return simplify_min_or_max_using_ranges (stmt
);
9994 else if (gimple_code (stmt
) == GIMPLE_COND
)
9995 return simplify_cond_using_ranges (as_a
<gcond
*> (stmt
));
9996 else if (gimple_code (stmt
) == GIMPLE_SWITCH
)
9997 return simplify_switch_using_ranges (as_a
<gswitch
*> (stmt
));
9998 else if (is_gimple_call (stmt
)
9999 && gimple_call_internal_p (stmt
))
10000 return simplify_internal_call_using_ranges (gsi
, stmt
);
10005 /* If the statement pointed by SI has a predicate whose value can be
10006 computed using the value range information computed by VRP, compute
10007 its value and return true. Otherwise, return false. */
10010 fold_predicate_in (gimple_stmt_iterator
*si
)
10012 bool assignment_p
= false;
10014 gimple
*stmt
= gsi_stmt (*si
);
10016 if (is_gimple_assign (stmt
)
10017 && TREE_CODE_CLASS (gimple_assign_rhs_code (stmt
)) == tcc_comparison
)
10019 assignment_p
= true;
10020 val
= vrp_evaluate_conditional (gimple_assign_rhs_code (stmt
),
10021 gimple_assign_rhs1 (stmt
),
10022 gimple_assign_rhs2 (stmt
),
10025 else if (gcond
*cond_stmt
= dyn_cast
<gcond
*> (stmt
))
10026 val
= vrp_evaluate_conditional (gimple_cond_code (cond_stmt
),
10027 gimple_cond_lhs (cond_stmt
),
10028 gimple_cond_rhs (cond_stmt
),
10036 val
= fold_convert (gimple_expr_type (stmt
), val
);
10040 fprintf (dump_file
, "Folding predicate ");
10041 print_gimple_expr (dump_file
, stmt
, 0, 0);
10042 fprintf (dump_file
, " to ");
10043 print_generic_expr (dump_file
, val
, 0);
10044 fprintf (dump_file
, "\n");
10047 if (is_gimple_assign (stmt
))
10048 gimple_assign_set_rhs_from_tree (si
, val
);
10051 gcc_assert (gimple_code (stmt
) == GIMPLE_COND
);
10052 gcond
*cond_stmt
= as_a
<gcond
*> (stmt
);
10053 if (integer_zerop (val
))
10054 gimple_cond_make_false (cond_stmt
);
10055 else if (integer_onep (val
))
10056 gimple_cond_make_true (cond_stmt
);
10058 gcc_unreachable ();
10067 /* Callback for substitute_and_fold folding the stmt at *SI. */
10070 vrp_fold_stmt (gimple_stmt_iterator
*si
)
10072 if (fold_predicate_in (si
))
10075 return simplify_stmt_using_ranges (si
);
10078 /* Unwindable const/copy equivalences. */
10079 const_and_copies
*equiv_stack
;
10081 /* A trivial wrapper so that we can present the generic jump threading
10082 code with a simple API for simplifying statements. STMT is the
10083 statement we want to simplify, WITHIN_STMT provides the location
10084 for any overflow warnings. */
10087 simplify_stmt_for_jump_threading (gimple
*stmt
, gimple
*within_stmt
,
10088 class avail_exprs_stack
*avail_exprs_stack ATTRIBUTE_UNUSED
)
10090 if (gcond
*cond_stmt
= dyn_cast
<gcond
*> (stmt
))
10091 return vrp_evaluate_conditional (gimple_cond_code (cond_stmt
),
10092 gimple_cond_lhs (cond_stmt
),
10093 gimple_cond_rhs (cond_stmt
),
10096 if (gassign
*assign_stmt
= dyn_cast
<gassign
*> (stmt
))
10098 value_range new_vr
= VR_INITIALIZER
;
10099 tree lhs
= gimple_assign_lhs (assign_stmt
);
10101 if (TREE_CODE (lhs
) == SSA_NAME
10102 && (INTEGRAL_TYPE_P (TREE_TYPE (lhs
))
10103 || POINTER_TYPE_P (TREE_TYPE (lhs
))))
10105 extract_range_from_assignment (&new_vr
, assign_stmt
);
10106 if (range_int_cst_singleton_p (&new_vr
))
10114 /* Blocks which have more than one predecessor and more than
10115 one successor present jump threading opportunities, i.e.,
10116 when the block is reached from a specific predecessor, we
10117 may be able to determine which of the outgoing edges will
10118 be traversed. When this optimization applies, we are able
10119 to avoid conditionals at runtime and we may expose secondary
10120 optimization opportunities.
10122 This routine is effectively a driver for the generic jump
10123 threading code. It basically just presents the generic code
10124 with edges that may be suitable for jump threading.
10126 Unlike DOM, we do not iterate VRP if jump threading was successful.
10127 While iterating may expose new opportunities for VRP, it is expected
10128 those opportunities would be very limited and the compile time cost
10129 to expose those opportunities would be significant.
10131 As jump threading opportunities are discovered, they are registered
10132 for later realization. */
10135 identify_jump_threads (void)
10142 /* Ugh. When substituting values earlier in this pass we can
10143 wipe the dominance information. So rebuild the dominator
10144 information as we need it within the jump threading code. */
10145 calculate_dominance_info (CDI_DOMINATORS
);
10147 /* We do not allow VRP information to be used for jump threading
10148 across a back edge in the CFG. Otherwise it becomes too
10149 difficult to avoid eliminating loop exit tests. Of course
10150 EDGE_DFS_BACK is not accurate at this time so we have to
10152 mark_dfs_back_edges ();
10154 /* Do not thread across edges we are about to remove. Just marking
10155 them as EDGE_IGNORE will do. */
10156 FOR_EACH_VEC_ELT (to_remove_edges
, i
, e
)
10157 e
->flags
|= EDGE_IGNORE
;
10159 /* Allocate our unwinder stack to unwind any temporary equivalences
10160 that might be recorded. */
10161 equiv_stack
= new const_and_copies ();
10163 /* To avoid lots of silly node creation, we create a single
10164 conditional and just modify it in-place when attempting to
10166 dummy
= gimple_build_cond (EQ_EXPR
,
10167 integer_zero_node
, integer_zero_node
,
10170 /* Walk through all the blocks finding those which present a
10171 potential jump threading opportunity. We could set this up
10172 as a dominator walker and record data during the walk, but
10173 I doubt it's worth the effort for the classes of jump
10174 threading opportunities we are trying to identify at this
10175 point in compilation. */
10176 FOR_EACH_BB_FN (bb
, cfun
)
10180 /* If the generic jump threading code does not find this block
10181 interesting, then there is nothing to do. */
10182 if (! potentially_threadable_block (bb
))
10185 last
= last_stmt (bb
);
10187 /* We're basically looking for a switch or any kind of conditional with
10188 integral or pointer type arguments. Note the type of the second
10189 argument will be the same as the first argument, so no need to
10190 check it explicitly.
10192 We also handle the case where there are no statements in the
10193 block. This come up with forwarder blocks that are not
10194 optimized away because they lead to a loop header. But we do
10195 want to thread through them as we can sometimes thread to the
10196 loop exit which is obviously profitable. */
10198 || gimple_code (last
) == GIMPLE_SWITCH
10199 || (gimple_code (last
) == GIMPLE_COND
10200 && TREE_CODE (gimple_cond_lhs (last
)) == SSA_NAME
10201 && (INTEGRAL_TYPE_P (TREE_TYPE (gimple_cond_lhs (last
)))
10202 || POINTER_TYPE_P (TREE_TYPE (gimple_cond_lhs (last
))))
10203 && (TREE_CODE (gimple_cond_rhs (last
)) == SSA_NAME
10204 || is_gimple_min_invariant (gimple_cond_rhs (last
)))))
10208 /* We've got a block with multiple predecessors and multiple
10209 successors which also ends in a suitable conditional or
10210 switch statement. For each predecessor, see if we can thread
10211 it to a specific successor. */
10212 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
10214 /* Do not thread across edges marked to ignoreor abnormal
10215 edges in the CFG. */
10216 if (e
->flags
& (EDGE_IGNORE
| EDGE_COMPLEX
))
10219 thread_across_edge (dummy
, e
, true, equiv_stack
, NULL
,
10220 simplify_stmt_for_jump_threading
);
10225 /* Clear EDGE_IGNORE. */
10226 FOR_EACH_VEC_ELT (to_remove_edges
, i
, e
)
10227 e
->flags
&= ~EDGE_IGNORE
;
10229 /* We do not actually update the CFG or SSA graphs at this point as
10230 ASSERT_EXPRs are still in the IL and cfg cleanup code does not yet
10231 handle ASSERT_EXPRs gracefully. */
10234 /* We identified all the jump threading opportunities earlier, but could
10235 not transform the CFG at that time. This routine transforms the
10236 CFG and arranges for the dominator tree to be rebuilt if necessary.
10238 Note the SSA graph update will occur during the normal TODO
10239 processing by the pass manager. */
10241 finalize_jump_threads (void)
10243 thread_through_all_blocks (false);
10244 delete equiv_stack
;
10248 /* Traverse all the blocks folding conditionals with known ranges. */
10251 vrp_finalize (bool warn_array_bounds_p
)
10255 values_propagated
= true;
10259 fprintf (dump_file
, "\nValue ranges after VRP:\n\n");
10260 dump_all_value_ranges (dump_file
);
10261 fprintf (dump_file
, "\n");
10264 /* Set value range to non pointer SSA_NAMEs. */
10265 for (i
= 0; i
< num_vr_values
; i
++)
10268 tree name
= ssa_name (i
);
10271 || POINTER_TYPE_P (TREE_TYPE (name
))
10272 || (vr_value
[i
]->type
== VR_VARYING
)
10273 || (vr_value
[i
]->type
== VR_UNDEFINED
))
10276 if ((TREE_CODE (vr_value
[i
]->min
) == INTEGER_CST
)
10277 && (TREE_CODE (vr_value
[i
]->max
) == INTEGER_CST
)
10278 && (vr_value
[i
]->type
== VR_RANGE
10279 || vr_value
[i
]->type
== VR_ANTI_RANGE
))
10280 set_range_info (name
, vr_value
[i
]->type
, vr_value
[i
]->min
,
10284 substitute_and_fold (op_with_constant_singleton_value_range
,
10285 vrp_fold_stmt
, false);
10287 if (warn_array_bounds
&& warn_array_bounds_p
)
10288 check_all_array_refs ();
10290 /* We must identify jump threading opportunities before we release
10291 the datastructures built by VRP. */
10292 identify_jump_threads ();
10294 /* Free allocated memory. */
10295 for (i
= 0; i
< num_vr_values
; i
++)
10298 BITMAP_FREE (vr_value
[i
]->equiv
);
10299 free (vr_value
[i
]);
10303 free (vr_phi_edge_counts
);
10305 /* So that we can distinguish between VRP data being available
10306 and not available. */
10308 vr_phi_edge_counts
= NULL
;
10312 /* Main entry point to VRP (Value Range Propagation). This pass is
10313 loosely based on J. R. C. Patterson, ``Accurate Static Branch
10314 Prediction by Value Range Propagation,'' in SIGPLAN Conference on
10315 Programming Language Design and Implementation, pp. 67-78, 1995.
10316 Also available at http://citeseer.ist.psu.edu/patterson95accurate.html
10318 This is essentially an SSA-CCP pass modified to deal with ranges
10319 instead of constants.
10321 While propagating ranges, we may find that two or more SSA name
10322 have equivalent, though distinct ranges. For instance,
10325 2 p_4 = ASSERT_EXPR <p_3, p_3 != 0>
10327 4 p_5 = ASSERT_EXPR <p_4, p_4 == q_2>;
10331 In the code above, pointer p_5 has range [q_2, q_2], but from the
10332 code we can also determine that p_5 cannot be NULL and, if q_2 had
10333 a non-varying range, p_5's range should also be compatible with it.
10335 These equivalences are created by two expressions: ASSERT_EXPR and
10336 copy operations. Since p_5 is an assertion on p_4, and p_4 was the
10337 result of another assertion, then we can use the fact that p_5 and
10338 p_4 are equivalent when evaluating p_5's range.
10340 Together with value ranges, we also propagate these equivalences
10341 between names so that we can take advantage of information from
10342 multiple ranges when doing final replacement. Note that this
10343 equivalency relation is transitive but not symmetric.
10345 In the example above, p_5 is equivalent to p_4, q_2 and p_3, but we
10346 cannot assert that q_2 is equivalent to p_5 because q_2 may be used
10347 in contexts where that assertion does not hold (e.g., in line 6).
10349 TODO, the main difference between this pass and Patterson's is that
10350 we do not propagate edge probabilities. We only compute whether
10351 edges can be taken or not. That is, instead of having a spectrum
10352 of jump probabilities between 0 and 1, we only deal with 0, 1 and
10353 DON'T KNOW. In the future, it may be worthwhile to propagate
10354 probabilities to aid branch prediction. */
10356 static unsigned int
10357 execute_vrp (bool warn_array_bounds_p
)
10363 loop_optimizer_init (LOOPS_NORMAL
| LOOPS_HAVE_RECORDED_EXITS
);
10364 rewrite_into_loop_closed_ssa (NULL
, TODO_update_ssa
);
10365 scev_initialize ();
10367 /* ??? This ends up using stale EDGE_DFS_BACK for liveness computation.
10368 Inserting assertions may split edges which will invalidate
10370 insert_range_assertions ();
10372 to_remove_edges
.create (10);
10373 to_update_switch_stmts
.create (5);
10374 threadedge_initialize_values ();
10376 /* For visiting PHI nodes we need EDGE_DFS_BACK computed. */
10377 mark_dfs_back_edges ();
10380 ssa_propagate (vrp_visit_stmt
, vrp_visit_phi_node
);
10381 vrp_finalize (warn_array_bounds_p
);
10383 free_numbers_of_iterations_estimates (cfun
);
10385 /* ASSERT_EXPRs must be removed before finalizing jump threads
10386 as finalizing jump threads calls the CFG cleanup code which
10387 does not properly handle ASSERT_EXPRs. */
10388 remove_range_assertions ();
10390 /* If we exposed any new variables, go ahead and put them into
10391 SSA form now, before we handle jump threading. This simplifies
10392 interactions between rewriting of _DECL nodes into SSA form
10393 and rewriting SSA_NAME nodes into SSA form after block
10394 duplication and CFG manipulation. */
10395 update_ssa (TODO_update_ssa
);
10397 finalize_jump_threads ();
10399 /* Remove dead edges from SWITCH_EXPR optimization. This leaves the
10400 CFG in a broken state and requires a cfg_cleanup run. */
10401 FOR_EACH_VEC_ELT (to_remove_edges
, i
, e
)
10403 /* Update SWITCH_EXPR case label vector. */
10404 FOR_EACH_VEC_ELT (to_update_switch_stmts
, i
, su
)
10407 size_t n
= TREE_VEC_LENGTH (su
->vec
);
10409 gimple_switch_set_num_labels (su
->stmt
, n
);
10410 for (j
= 0; j
< n
; j
++)
10411 gimple_switch_set_label (su
->stmt
, j
, TREE_VEC_ELT (su
->vec
, j
));
10412 /* As we may have replaced the default label with a regular one
10413 make sure to make it a real default label again. This ensures
10414 optimal expansion. */
10415 label
= gimple_switch_label (su
->stmt
, 0);
10416 CASE_LOW (label
) = NULL_TREE
;
10417 CASE_HIGH (label
) = NULL_TREE
;
10420 if (to_remove_edges
.length () > 0)
10422 free_dominance_info (CDI_DOMINATORS
);
10423 loops_state_set (LOOPS_NEED_FIXUP
);
10426 to_remove_edges
.release ();
10427 to_update_switch_stmts
.release ();
10428 threadedge_finalize_values ();
10431 loop_optimizer_finalize ();
10437 const pass_data pass_data_vrp
=
10439 GIMPLE_PASS
, /* type */
10441 OPTGROUP_NONE
, /* optinfo_flags */
10442 TV_TREE_VRP
, /* tv_id */
10443 PROP_ssa
, /* properties_required */
10444 0, /* properties_provided */
10445 0, /* properties_destroyed */
10446 0, /* todo_flags_start */
10447 ( TODO_cleanup_cfg
| TODO_update_ssa
), /* todo_flags_finish */
10450 class pass_vrp
: public gimple_opt_pass
10453 pass_vrp (gcc::context
*ctxt
)
10454 : gimple_opt_pass (pass_data_vrp
, ctxt
), warn_array_bounds_p (false)
10457 /* opt_pass methods: */
10458 opt_pass
* clone () { return new pass_vrp (m_ctxt
); }
10459 void set_pass_param (unsigned int n
, bool param
)
10461 gcc_assert (n
== 0);
10462 warn_array_bounds_p
= param
;
10464 virtual bool gate (function
*) { return flag_tree_vrp
!= 0; }
10465 virtual unsigned int execute (function
*)
10466 { return execute_vrp (warn_array_bounds_p
); }
10469 bool warn_array_bounds_p
;
10470 }; // class pass_vrp
10472 } // anon namespace
10475 make_pass_vrp (gcc::context
*ctxt
)
10477 return new pass_vrp (ctxt
);