1 /* Support routines for Value Range Propagation (VRP).
2 Copyright (C) 2005-2015 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"
32 #include "fold-const.h"
33 #include "stor-layout.h"
36 #include "internal-fn.h"
37 #include "gimple-fold.h"
39 #include "gimple-iterator.h"
40 #include "gimple-walk.h"
42 #include "tree-ssa-loop-manip.h"
43 #include "tree-ssa-loop-niter.h"
44 #include "tree-ssa-loop.h"
45 #include "tree-into-ssa.h"
47 #include "tree-pass.h"
48 #include "tree-dump.h"
49 #include "gimple-pretty-print.h"
50 #include "diagnostic-core.h"
53 #include "tree-scalar-evolution.h"
54 #include "tree-ssa-propagate.h"
55 #include "tree-chrec.h"
56 #include "tree-ssa-threadupdate.h"
57 #include "insn-codes.h"
58 #include "optabs-tree.h"
59 #include "tree-ssa-scopedtables.h"
60 #include "tree-ssa-threadedge.h"
64 /* Range of values that can be associated with an SSA_NAME after VRP
68 /* Lattice value represented by this range. */
69 enum value_range_type type
;
71 /* Minimum and maximum values represented by this range. These
72 values should be interpreted as follows:
74 - If TYPE is VR_UNDEFINED or VR_VARYING then MIN and MAX must
77 - If TYPE == VR_RANGE then MIN holds the minimum value and
78 MAX holds the maximum value of the range [MIN, MAX].
80 - If TYPE == ANTI_RANGE the variable is known to NOT
81 take any values in the range [MIN, MAX]. */
85 /* Set of SSA names whose value ranges are equivalent to this one.
86 This set is only valid when TYPE is VR_RANGE or VR_ANTI_RANGE. */
90 #define VR_INITIALIZER { VR_UNDEFINED, NULL_TREE, NULL_TREE, NULL }
92 /* Set of SSA names found live during the RPO traversal of the function
93 for still active basic-blocks. */
96 /* Return true if the SSA name NAME is live on the edge E. */
99 live_on_edge (edge e
, tree name
)
101 return (live
[e
->dest
->index
]
102 && bitmap_bit_p (live
[e
->dest
->index
], SSA_NAME_VERSION (name
)));
105 /* Local functions. */
106 static int compare_values (tree val1
, tree val2
);
107 static int compare_values_warnv (tree val1
, tree val2
, bool *);
108 static void vrp_meet (value_range
*, value_range
*);
109 static void vrp_intersect_ranges (value_range
*, value_range
*);
110 static tree
vrp_evaluate_conditional_warnv_with_ops (enum tree_code
,
111 tree
, tree
, bool, bool *,
114 /* Location information for ASSERT_EXPRs. Each instance of this
115 structure describes an ASSERT_EXPR for an SSA name. Since a single
116 SSA name may have more than one assertion associated with it, these
117 locations are kept in a linked list attached to the corresponding
121 /* Basic block where the assertion would be inserted. */
124 /* Some assertions need to be inserted on an edge (e.g., assertions
125 generated by COND_EXPRs). In those cases, BB will be NULL. */
128 /* Pointer to the statement that generated this assertion. */
129 gimple_stmt_iterator si
;
131 /* Predicate code for the ASSERT_EXPR. Must be COMPARISON_CLASS_P. */
132 enum tree_code comp_code
;
134 /* Value being compared against. */
137 /* Expression to compare. */
140 /* Next node in the linked list. */
144 /* If bit I is present, it means that SSA name N_i has a list of
145 assertions that should be inserted in the IL. */
146 static bitmap need_assert_for
;
148 /* Array of locations lists where to insert assertions. ASSERTS_FOR[I]
149 holds a list of ASSERT_LOCUS_T nodes that describe where
150 ASSERT_EXPRs for SSA name N_I should be inserted. */
151 static assert_locus
**asserts_for
;
153 /* Value range array. After propagation, VR_VALUE[I] holds the range
154 of values that SSA name N_I may take. */
155 static unsigned num_vr_values
;
156 static value_range
**vr_value
;
157 static bool values_propagated
;
159 /* For a PHI node which sets SSA name N_I, VR_COUNTS[I] holds the
160 number of executable edges we saw the last time we visited the
162 static int *vr_phi_edge_counts
;
164 struct switch_update
{
169 static vec
<edge
> to_remove_edges
;
170 static vec
<switch_update
> to_update_switch_stmts
;
173 /* Return the maximum value for TYPE. */
176 vrp_val_max (const_tree type
)
178 if (!INTEGRAL_TYPE_P (type
))
181 return TYPE_MAX_VALUE (type
);
184 /* Return the minimum value for TYPE. */
187 vrp_val_min (const_tree type
)
189 if (!INTEGRAL_TYPE_P (type
))
192 return TYPE_MIN_VALUE (type
);
195 /* Return whether VAL is equal to the maximum value of its type. This
196 will be true for a positive overflow infinity. We can't do a
197 simple equality comparison with TYPE_MAX_VALUE because C typedefs
198 and Ada subtypes can produce types whose TYPE_MAX_VALUE is not ==
199 to the integer constant with the same value in the type. */
202 vrp_val_is_max (const_tree val
)
204 tree type_max
= vrp_val_max (TREE_TYPE (val
));
205 return (val
== type_max
206 || (type_max
!= NULL_TREE
207 && operand_equal_p (val
, type_max
, 0)));
210 /* Return whether VAL is equal to the minimum value of its type. This
211 will be true for a negative overflow infinity. */
214 vrp_val_is_min (const_tree val
)
216 tree type_min
= vrp_val_min (TREE_TYPE (val
));
217 return (val
== type_min
218 || (type_min
!= NULL_TREE
219 && operand_equal_p (val
, type_min
, 0)));
223 /* Return whether TYPE should use an overflow infinity distinct from
224 TYPE_{MIN,MAX}_VALUE. We use an overflow infinity value to
225 represent a signed overflow during VRP computations. An infinity
226 is distinct from a half-range, which will go from some number to
227 TYPE_{MIN,MAX}_VALUE. */
230 needs_overflow_infinity (const_tree type
)
232 return INTEGRAL_TYPE_P (type
) && !TYPE_OVERFLOW_WRAPS (type
);
235 /* Return whether TYPE can support our overflow infinity
236 representation: we use the TREE_OVERFLOW flag, which only exists
237 for constants. If TYPE doesn't support this, we don't optimize
238 cases which would require signed overflow--we drop them to
242 supports_overflow_infinity (const_tree type
)
244 tree min
= vrp_val_min (type
), max
= vrp_val_max (type
);
245 #ifdef ENABLE_CHECKING
246 gcc_assert (needs_overflow_infinity (type
));
248 return (min
!= NULL_TREE
249 && CONSTANT_CLASS_P (min
)
251 && CONSTANT_CLASS_P (max
));
254 /* VAL is the maximum or minimum value of a type. Return a
255 corresponding overflow infinity. */
258 make_overflow_infinity (tree val
)
260 gcc_checking_assert (val
!= NULL_TREE
&& CONSTANT_CLASS_P (val
));
261 val
= copy_node (val
);
262 TREE_OVERFLOW (val
) = 1;
266 /* Return a negative overflow infinity for TYPE. */
269 negative_overflow_infinity (tree type
)
271 gcc_checking_assert (supports_overflow_infinity (type
));
272 return make_overflow_infinity (vrp_val_min (type
));
275 /* Return a positive overflow infinity for TYPE. */
278 positive_overflow_infinity (tree type
)
280 gcc_checking_assert (supports_overflow_infinity (type
));
281 return make_overflow_infinity (vrp_val_max (type
));
284 /* Return whether VAL is a negative overflow infinity. */
287 is_negative_overflow_infinity (const_tree val
)
289 return (TREE_OVERFLOW_P (val
)
290 && needs_overflow_infinity (TREE_TYPE (val
))
291 && vrp_val_is_min (val
));
294 /* Return whether VAL is a positive overflow infinity. */
297 is_positive_overflow_infinity (const_tree val
)
299 return (TREE_OVERFLOW_P (val
)
300 && needs_overflow_infinity (TREE_TYPE (val
))
301 && vrp_val_is_max (val
));
304 /* Return whether VAL is a positive or negative overflow infinity. */
307 is_overflow_infinity (const_tree val
)
309 return (TREE_OVERFLOW_P (val
)
310 && needs_overflow_infinity (TREE_TYPE (val
))
311 && (vrp_val_is_min (val
) || vrp_val_is_max (val
)));
314 /* Return whether STMT has a constant rhs that is_overflow_infinity. */
317 stmt_overflow_infinity (gimple
*stmt
)
319 if (is_gimple_assign (stmt
)
320 && get_gimple_rhs_class (gimple_assign_rhs_code (stmt
)) ==
322 return is_overflow_infinity (gimple_assign_rhs1 (stmt
));
326 /* If VAL is now an overflow infinity, return VAL. Otherwise, return
327 the same value with TREE_OVERFLOW clear. This can be used to avoid
328 confusing a regular value with an overflow value. */
331 avoid_overflow_infinity (tree val
)
333 if (!is_overflow_infinity (val
))
336 if (vrp_val_is_max (val
))
337 return vrp_val_max (TREE_TYPE (val
));
340 gcc_checking_assert (vrp_val_is_min (val
));
341 return vrp_val_min (TREE_TYPE (val
));
346 /* Set value range VR to VR_UNDEFINED. */
349 set_value_range_to_undefined (value_range
*vr
)
351 vr
->type
= VR_UNDEFINED
;
352 vr
->min
= vr
->max
= NULL_TREE
;
354 bitmap_clear (vr
->equiv
);
358 /* Set value range VR to VR_VARYING. */
361 set_value_range_to_varying (value_range
*vr
)
363 vr
->type
= VR_VARYING
;
364 vr
->min
= vr
->max
= NULL_TREE
;
366 bitmap_clear (vr
->equiv
);
370 /* Set value range VR to {T, MIN, MAX, EQUIV}. */
373 set_value_range (value_range
*vr
, enum value_range_type t
, tree min
,
374 tree max
, bitmap equiv
)
376 #if defined ENABLE_CHECKING
377 /* Check the validity of the range. */
378 if (t
== VR_RANGE
|| t
== VR_ANTI_RANGE
)
382 gcc_assert (min
&& max
);
384 gcc_assert ((!TREE_OVERFLOW_P (min
) || is_overflow_infinity (min
))
385 && (!TREE_OVERFLOW_P (max
) || is_overflow_infinity (max
)));
387 if (INTEGRAL_TYPE_P (TREE_TYPE (min
)) && t
== VR_ANTI_RANGE
)
388 gcc_assert (!vrp_val_is_min (min
) || !vrp_val_is_max (max
));
390 cmp
= compare_values (min
, max
);
391 gcc_assert (cmp
== 0 || cmp
== -1 || cmp
== -2);
393 if (needs_overflow_infinity (TREE_TYPE (min
)))
394 gcc_assert (!is_overflow_infinity (min
)
395 || !is_overflow_infinity (max
));
398 if (t
== VR_UNDEFINED
|| t
== VR_VARYING
)
399 gcc_assert (min
== NULL_TREE
&& max
== NULL_TREE
);
401 if (t
== VR_UNDEFINED
|| t
== VR_VARYING
)
402 gcc_assert (equiv
== NULL
|| bitmap_empty_p (equiv
));
409 /* Since updating the equivalence set involves deep copying the
410 bitmaps, only do it if absolutely necessary. */
411 if (vr
->equiv
== NULL
413 vr
->equiv
= BITMAP_ALLOC (NULL
);
415 if (equiv
!= vr
->equiv
)
417 if (equiv
&& !bitmap_empty_p (equiv
))
418 bitmap_copy (vr
->equiv
, equiv
);
420 bitmap_clear (vr
->equiv
);
425 /* Set value range VR to the canonical form of {T, MIN, MAX, EQUIV}.
426 This means adjusting T, MIN and MAX representing the case of a
427 wrapping range with MAX < MIN covering [MIN, type_max] U [type_min, MAX]
428 as anti-rage ~[MAX+1, MIN-1]. Likewise for wrapping anti-ranges.
429 In corner cases where MAX+1 or MIN-1 wraps this will fall back
431 This routine exists to ease canonicalization in the case where we
432 extract ranges from var + CST op limit. */
435 set_and_canonicalize_value_range (value_range
*vr
, enum value_range_type t
,
436 tree min
, tree max
, bitmap equiv
)
438 /* Use the canonical setters for VR_UNDEFINED and VR_VARYING. */
439 if (t
== VR_UNDEFINED
)
441 set_value_range_to_undefined (vr
);
444 else if (t
== VR_VARYING
)
446 set_value_range_to_varying (vr
);
450 /* Nothing to canonicalize for symbolic ranges. */
451 if (TREE_CODE (min
) != INTEGER_CST
452 || TREE_CODE (max
) != INTEGER_CST
)
454 set_value_range (vr
, t
, min
, max
, equiv
);
458 /* Wrong order for min and max, to swap them and the VR type we need
460 if (tree_int_cst_lt (max
, min
))
464 /* For one bit precision if max < min, then the swapped
465 range covers all values, so for VR_RANGE it is varying and
466 for VR_ANTI_RANGE empty range, so drop to varying as well. */
467 if (TYPE_PRECISION (TREE_TYPE (min
)) == 1)
469 set_value_range_to_varying (vr
);
473 one
= build_int_cst (TREE_TYPE (min
), 1);
474 tmp
= int_const_binop (PLUS_EXPR
, max
, one
);
475 max
= int_const_binop (MINUS_EXPR
, min
, one
);
478 /* There's one corner case, if we had [C+1, C] before we now have
479 that again. But this represents an empty value range, so drop
480 to varying in this case. */
481 if (tree_int_cst_lt (max
, min
))
483 set_value_range_to_varying (vr
);
487 t
= t
== VR_RANGE
? VR_ANTI_RANGE
: VR_RANGE
;
490 /* Anti-ranges that can be represented as ranges should be so. */
491 if (t
== VR_ANTI_RANGE
)
493 bool is_min
= vrp_val_is_min (min
);
494 bool is_max
= vrp_val_is_max (max
);
496 if (is_min
&& is_max
)
498 /* We cannot deal with empty ranges, drop to varying.
499 ??? This could be VR_UNDEFINED instead. */
500 set_value_range_to_varying (vr
);
503 else if (TYPE_PRECISION (TREE_TYPE (min
)) == 1
504 && (is_min
|| is_max
))
506 /* Non-empty boolean ranges can always be represented
507 as a singleton range. */
509 min
= max
= vrp_val_max (TREE_TYPE (min
));
511 min
= max
= vrp_val_min (TREE_TYPE (min
));
515 /* As a special exception preserve non-null ranges. */
516 && !(TYPE_UNSIGNED (TREE_TYPE (min
))
517 && integer_zerop (max
)))
519 tree one
= build_int_cst (TREE_TYPE (max
), 1);
520 min
= int_const_binop (PLUS_EXPR
, max
, one
);
521 max
= vrp_val_max (TREE_TYPE (max
));
526 tree one
= build_int_cst (TREE_TYPE (min
), 1);
527 max
= int_const_binop (MINUS_EXPR
, min
, one
);
528 min
= vrp_val_min (TREE_TYPE (min
));
533 /* Drop [-INF(OVF), +INF(OVF)] to varying. */
534 if (needs_overflow_infinity (TREE_TYPE (min
))
535 && is_overflow_infinity (min
)
536 && is_overflow_infinity (max
))
538 set_value_range_to_varying (vr
);
542 set_value_range (vr
, t
, min
, max
, equiv
);
545 /* Copy value range FROM into value range TO. */
548 copy_value_range (value_range
*to
, value_range
*from
)
550 set_value_range (to
, from
->type
, from
->min
, from
->max
, from
->equiv
);
553 /* Set value range VR to a single value. This function is only called
554 with values we get from statements, and exists to clear the
555 TREE_OVERFLOW flag so that we don't think we have an overflow
556 infinity when we shouldn't. */
559 set_value_range_to_value (value_range
*vr
, tree val
, bitmap equiv
)
561 gcc_assert (is_gimple_min_invariant (val
));
562 if (TREE_OVERFLOW_P (val
))
563 val
= drop_tree_overflow (val
);
564 set_value_range (vr
, VR_RANGE
, val
, val
, equiv
);
567 /* Set value range VR to a non-negative range of type TYPE.
568 OVERFLOW_INFINITY indicates whether to use an overflow infinity
569 rather than TYPE_MAX_VALUE; this should be true if we determine
570 that the range is nonnegative based on the assumption that signed
571 overflow does not occur. */
574 set_value_range_to_nonnegative (value_range
*vr
, tree type
,
575 bool overflow_infinity
)
579 if (overflow_infinity
&& !supports_overflow_infinity (type
))
581 set_value_range_to_varying (vr
);
585 zero
= build_int_cst (type
, 0);
586 set_value_range (vr
, VR_RANGE
, zero
,
588 ? positive_overflow_infinity (type
)
589 : TYPE_MAX_VALUE (type
)),
593 /* Set value range VR to a non-NULL range of type TYPE. */
596 set_value_range_to_nonnull (value_range
*vr
, tree type
)
598 tree zero
= build_int_cst (type
, 0);
599 set_value_range (vr
, VR_ANTI_RANGE
, zero
, zero
, vr
->equiv
);
603 /* Set value range VR to a NULL range of type TYPE. */
606 set_value_range_to_null (value_range
*vr
, tree type
)
608 set_value_range_to_value (vr
, build_int_cst (type
, 0), vr
->equiv
);
612 /* Set value range VR to a range of a truthvalue of type TYPE. */
615 set_value_range_to_truthvalue (value_range
*vr
, tree type
)
617 if (TYPE_PRECISION (type
) == 1)
618 set_value_range_to_varying (vr
);
620 set_value_range (vr
, VR_RANGE
,
621 build_int_cst (type
, 0), build_int_cst (type
, 1),
626 /* If abs (min) < abs (max), set VR to [-max, max], if
627 abs (min) >= abs (max), set VR to [-min, min]. */
630 abs_extent_range (value_range
*vr
, tree min
, tree max
)
634 gcc_assert (TREE_CODE (min
) == INTEGER_CST
);
635 gcc_assert (TREE_CODE (max
) == INTEGER_CST
);
636 gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (min
)));
637 gcc_assert (!TYPE_UNSIGNED (TREE_TYPE (min
)));
638 min
= fold_unary (ABS_EXPR
, TREE_TYPE (min
), min
);
639 max
= fold_unary (ABS_EXPR
, TREE_TYPE (max
), max
);
640 if (TREE_OVERFLOW (min
) || TREE_OVERFLOW (max
))
642 set_value_range_to_varying (vr
);
645 cmp
= compare_values (min
, max
);
647 min
= fold_unary (NEGATE_EXPR
, TREE_TYPE (min
), max
);
648 else if (cmp
== 0 || cmp
== 1)
651 min
= fold_unary (NEGATE_EXPR
, TREE_TYPE (min
), min
);
655 set_value_range_to_varying (vr
);
658 set_and_canonicalize_value_range (vr
, VR_RANGE
, min
, max
, NULL
);
662 /* Return value range information for VAR.
664 If we have no values ranges recorded (ie, VRP is not running), then
665 return NULL. Otherwise create an empty range if none existed for VAR. */
668 get_value_range (const_tree var
)
670 static const value_range vr_const_varying
671 = { VR_VARYING
, NULL_TREE
, NULL_TREE
, NULL
};
674 unsigned ver
= SSA_NAME_VERSION (var
);
676 /* If we have no recorded ranges, then return NULL. */
680 /* If we query the range for a new SSA name return an unmodifiable VARYING.
681 We should get here at most from the substitute-and-fold stage which
682 will never try to change values. */
683 if (ver
>= num_vr_values
)
684 return CONST_CAST (value_range
*, &vr_const_varying
);
690 /* After propagation finished do not allocate new value-ranges. */
691 if (values_propagated
)
692 return CONST_CAST (value_range
*, &vr_const_varying
);
694 /* Create a default value range. */
695 vr_value
[ver
] = vr
= XCNEW (value_range
);
697 /* Defer allocating the equivalence set. */
700 /* If VAR is a default definition of a parameter, the variable can
701 take any value in VAR's type. */
702 if (SSA_NAME_IS_DEFAULT_DEF (var
))
704 sym
= SSA_NAME_VAR (var
);
705 if (TREE_CODE (sym
) == PARM_DECL
)
707 /* Try to use the "nonnull" attribute to create ~[0, 0]
708 anti-ranges for pointers. Note that this is only valid with
709 default definitions of PARM_DECLs. */
710 if (POINTER_TYPE_P (TREE_TYPE (sym
))
711 && nonnull_arg_p (sym
))
712 set_value_range_to_nonnull (vr
, TREE_TYPE (sym
));
714 set_value_range_to_varying (vr
);
716 else if (TREE_CODE (sym
) == RESULT_DECL
717 && DECL_BY_REFERENCE (sym
))
718 set_value_range_to_nonnull (vr
, TREE_TYPE (sym
));
724 /* Return true, if VAL1 and VAL2 are equal values for VRP purposes. */
727 vrp_operand_equal_p (const_tree val1
, const_tree val2
)
731 if (!val1
|| !val2
|| !operand_equal_p (val1
, val2
, 0))
733 return is_overflow_infinity (val1
) == is_overflow_infinity (val2
);
736 /* Return true, if the bitmaps B1 and B2 are equal. */
739 vrp_bitmap_equal_p (const_bitmap b1
, const_bitmap b2
)
742 || ((!b1
|| bitmap_empty_p (b1
))
743 && (!b2
|| bitmap_empty_p (b2
)))
745 && bitmap_equal_p (b1
, b2
)));
748 /* Update the value range and equivalence set for variable VAR to
749 NEW_VR. Return true if NEW_VR is different from VAR's previous
752 NOTE: This function assumes that NEW_VR is a temporary value range
753 object created for the sole purpose of updating VAR's range. The
754 storage used by the equivalence set from NEW_VR will be freed by
755 this function. Do not call update_value_range when NEW_VR
756 is the range object associated with another SSA name. */
759 update_value_range (const_tree var
, value_range
*new_vr
)
764 /* If there is a value-range on the SSA name from earlier analysis
766 if (INTEGRAL_TYPE_P (TREE_TYPE (var
)))
769 value_range_type rtype
= get_range_info (var
, &min
, &max
);
770 if (rtype
== VR_RANGE
|| rtype
== VR_ANTI_RANGE
)
774 nr
.min
= wide_int_to_tree (TREE_TYPE (var
), min
);
775 nr
.max
= wide_int_to_tree (TREE_TYPE (var
), max
);
777 vrp_intersect_ranges (new_vr
, &nr
);
781 /* Update the value range, if necessary. */
782 old_vr
= get_value_range (var
);
783 is_new
= old_vr
->type
!= new_vr
->type
784 || !vrp_operand_equal_p (old_vr
->min
, new_vr
->min
)
785 || !vrp_operand_equal_p (old_vr
->max
, new_vr
->max
)
786 || !vrp_bitmap_equal_p (old_vr
->equiv
, new_vr
->equiv
);
790 /* Do not allow transitions up the lattice. The following
791 is slightly more awkward than just new_vr->type < old_vr->type
792 because VR_RANGE and VR_ANTI_RANGE need to be considered
793 the same. We may not have is_new when transitioning to
794 UNDEFINED. If old_vr->type is VARYING, we shouldn't be
796 if (new_vr
->type
== VR_UNDEFINED
)
798 BITMAP_FREE (new_vr
->equiv
);
799 set_value_range_to_varying (old_vr
);
800 set_value_range_to_varying (new_vr
);
804 set_value_range (old_vr
, new_vr
->type
, new_vr
->min
, new_vr
->max
,
808 BITMAP_FREE (new_vr
->equiv
);
814 /* Add VAR and VAR's equivalence set to EQUIV. This is the central
815 point where equivalence processing can be turned on/off. */
818 add_equivalence (bitmap
*equiv
, const_tree var
)
820 unsigned ver
= SSA_NAME_VERSION (var
);
821 value_range
*vr
= vr_value
[ver
];
824 *equiv
= BITMAP_ALLOC (NULL
);
825 bitmap_set_bit (*equiv
, ver
);
827 bitmap_ior_into (*equiv
, vr
->equiv
);
831 /* Return true if VR is ~[0, 0]. */
834 range_is_nonnull (value_range
*vr
)
836 return vr
->type
== VR_ANTI_RANGE
837 && integer_zerop (vr
->min
)
838 && integer_zerop (vr
->max
);
842 /* Return true if VR is [0, 0]. */
845 range_is_null (value_range
*vr
)
847 return vr
->type
== VR_RANGE
848 && integer_zerop (vr
->min
)
849 && integer_zerop (vr
->max
);
852 /* Return true if max and min of VR are INTEGER_CST. It's not necessary
856 range_int_cst_p (value_range
*vr
)
858 return (vr
->type
== VR_RANGE
859 && TREE_CODE (vr
->max
) == INTEGER_CST
860 && TREE_CODE (vr
->min
) == INTEGER_CST
);
863 /* Return true if VR is a INTEGER_CST singleton. */
866 range_int_cst_singleton_p (value_range
*vr
)
868 return (range_int_cst_p (vr
)
869 && !is_overflow_infinity (vr
->min
)
870 && !is_overflow_infinity (vr
->max
)
871 && tree_int_cst_equal (vr
->min
, vr
->max
));
874 /* Return true if value range VR involves at least one symbol. */
877 symbolic_range_p (value_range
*vr
)
879 return (!is_gimple_min_invariant (vr
->min
)
880 || !is_gimple_min_invariant (vr
->max
));
883 /* Return the single symbol (an SSA_NAME) contained in T if any, or NULL_TREE
884 otherwise. We only handle additive operations and set NEG to true if the
885 symbol is negated and INV to the invariant part, if any. */
888 get_single_symbol (tree t
, bool *neg
, tree
*inv
)
893 if (TREE_CODE (t
) == PLUS_EXPR
894 || TREE_CODE (t
) == POINTER_PLUS_EXPR
895 || TREE_CODE (t
) == MINUS_EXPR
)
897 if (is_gimple_min_invariant (TREE_OPERAND (t
, 0)))
899 neg_
= (TREE_CODE (t
) == MINUS_EXPR
);
900 inv_
= TREE_OPERAND (t
, 0);
901 t
= TREE_OPERAND (t
, 1);
903 else if (is_gimple_min_invariant (TREE_OPERAND (t
, 1)))
906 inv_
= TREE_OPERAND (t
, 1);
907 t
= TREE_OPERAND (t
, 0);
918 if (TREE_CODE (t
) == NEGATE_EXPR
)
920 t
= TREE_OPERAND (t
, 0);
924 if (TREE_CODE (t
) != SSA_NAME
)
932 /* The reverse operation: build a symbolic expression with TYPE
933 from symbol SYM, negated according to NEG, and invariant INV. */
936 build_symbolic_expr (tree type
, tree sym
, bool neg
, tree inv
)
938 const bool pointer_p
= POINTER_TYPE_P (type
);
942 t
= build1 (NEGATE_EXPR
, type
, t
);
944 if (integer_zerop (inv
))
947 return build2 (pointer_p
? POINTER_PLUS_EXPR
: PLUS_EXPR
, type
, t
, inv
);
950 /* Return true if value range VR involves exactly one symbol SYM. */
953 symbolic_range_based_on_p (value_range
*vr
, const_tree sym
)
955 bool neg
, min_has_symbol
, max_has_symbol
;
958 if (is_gimple_min_invariant (vr
->min
))
959 min_has_symbol
= false;
960 else if (get_single_symbol (vr
->min
, &neg
, &inv
) == sym
)
961 min_has_symbol
= true;
965 if (is_gimple_min_invariant (vr
->max
))
966 max_has_symbol
= false;
967 else if (get_single_symbol (vr
->max
, &neg
, &inv
) == sym
)
968 max_has_symbol
= true;
972 return (min_has_symbol
|| max_has_symbol
);
975 /* Return true if value range VR uses an overflow infinity. */
978 overflow_infinity_range_p (value_range
*vr
)
980 return (vr
->type
== VR_RANGE
981 && (is_overflow_infinity (vr
->min
)
982 || is_overflow_infinity (vr
->max
)));
985 /* Return false if we can not make a valid comparison based on VR;
986 this will be the case if it uses an overflow infinity and overflow
987 is not undefined (i.e., -fno-strict-overflow is in effect).
988 Otherwise return true, and set *STRICT_OVERFLOW_P to true if VR
989 uses an overflow infinity. */
992 usable_range_p (value_range
*vr
, bool *strict_overflow_p
)
994 gcc_assert (vr
->type
== VR_RANGE
);
995 if (is_overflow_infinity (vr
->min
))
997 *strict_overflow_p
= true;
998 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (vr
->min
)))
1001 if (is_overflow_infinity (vr
->max
))
1003 *strict_overflow_p
= true;
1004 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (vr
->max
)))
1010 /* Return true if the result of assignment STMT is know to be non-zero.
1011 If the return value is based on the assumption that signed overflow is
1012 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1013 *STRICT_OVERFLOW_P.*/
1016 gimple_assign_nonzero_warnv_p (gimple
*stmt
, bool *strict_overflow_p
)
1018 enum tree_code code
= gimple_assign_rhs_code (stmt
);
1019 switch (get_gimple_rhs_class (code
))
1021 case GIMPLE_UNARY_RHS
:
1022 return tree_unary_nonzero_warnv_p (gimple_assign_rhs_code (stmt
),
1023 gimple_expr_type (stmt
),
1024 gimple_assign_rhs1 (stmt
),
1026 case GIMPLE_BINARY_RHS
:
1027 return tree_binary_nonzero_warnv_p (gimple_assign_rhs_code (stmt
),
1028 gimple_expr_type (stmt
),
1029 gimple_assign_rhs1 (stmt
),
1030 gimple_assign_rhs2 (stmt
),
1032 case GIMPLE_TERNARY_RHS
:
1034 case GIMPLE_SINGLE_RHS
:
1035 return tree_single_nonzero_warnv_p (gimple_assign_rhs1 (stmt
),
1037 case GIMPLE_INVALID_RHS
:
1044 /* Return true if STMT is known to compute a non-zero value.
1045 If the return value is based on the assumption that signed overflow is
1046 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1047 *STRICT_OVERFLOW_P.*/
1050 gimple_stmt_nonzero_warnv_p (gimple
*stmt
, bool *strict_overflow_p
)
1052 switch (gimple_code (stmt
))
1055 return gimple_assign_nonzero_warnv_p (stmt
, strict_overflow_p
);
1058 tree fndecl
= gimple_call_fndecl (stmt
);
1059 if (!fndecl
) return false;
1060 if (flag_delete_null_pointer_checks
&& !flag_check_new
1061 && DECL_IS_OPERATOR_NEW (fndecl
)
1062 && !TREE_NOTHROW (fndecl
))
1064 /* References are always non-NULL. */
1065 if (flag_delete_null_pointer_checks
1066 && TREE_CODE (TREE_TYPE (fndecl
)) == REFERENCE_TYPE
)
1068 if (flag_delete_null_pointer_checks
&&
1069 lookup_attribute ("returns_nonnull",
1070 TYPE_ATTRIBUTES (gimple_call_fntype (stmt
))))
1072 return gimple_alloca_call_p (stmt
);
1079 /* Like tree_expr_nonzero_warnv_p, but this function uses value ranges
1083 vrp_stmt_computes_nonzero (gimple
*stmt
, bool *strict_overflow_p
)
1085 if (gimple_stmt_nonzero_warnv_p (stmt
, strict_overflow_p
))
1088 /* If we have an expression of the form &X->a, then the expression
1089 is nonnull if X is nonnull. */
1090 if (is_gimple_assign (stmt
)
1091 && gimple_assign_rhs_code (stmt
) == ADDR_EXPR
)
1093 tree expr
= gimple_assign_rhs1 (stmt
);
1094 tree base
= get_base_address (TREE_OPERAND (expr
, 0));
1096 if (base
!= NULL_TREE
1097 && TREE_CODE (base
) == MEM_REF
1098 && TREE_CODE (TREE_OPERAND (base
, 0)) == SSA_NAME
)
1100 value_range
*vr
= get_value_range (TREE_OPERAND (base
, 0));
1101 if (range_is_nonnull (vr
))
1109 /* Returns true if EXPR is a valid value (as expected by compare_values) --
1110 a gimple invariant, or SSA_NAME +- CST. */
1113 valid_value_p (tree expr
)
1115 if (TREE_CODE (expr
) == SSA_NAME
)
1118 if (TREE_CODE (expr
) == PLUS_EXPR
1119 || TREE_CODE (expr
) == MINUS_EXPR
)
1120 return (TREE_CODE (TREE_OPERAND (expr
, 0)) == SSA_NAME
1121 && TREE_CODE (TREE_OPERAND (expr
, 1)) == INTEGER_CST
);
1123 return is_gimple_min_invariant (expr
);
1129 -2 if those are incomparable. */
1131 operand_less_p (tree val
, tree val2
)
1133 /* LT is folded faster than GE and others. Inline the common case. */
1134 if (TREE_CODE (val
) == INTEGER_CST
&& TREE_CODE (val2
) == INTEGER_CST
)
1135 return tree_int_cst_lt (val
, val2
);
1140 fold_defer_overflow_warnings ();
1142 tcmp
= fold_binary_to_constant (LT_EXPR
, boolean_type_node
, val
, val2
);
1144 fold_undefer_and_ignore_overflow_warnings ();
1147 || TREE_CODE (tcmp
) != INTEGER_CST
)
1150 if (!integer_zerop (tcmp
))
1154 /* val >= val2, not considering overflow infinity. */
1155 if (is_negative_overflow_infinity (val
))
1156 return is_negative_overflow_infinity (val2
) ? 0 : 1;
1157 else if (is_positive_overflow_infinity (val2
))
1158 return is_positive_overflow_infinity (val
) ? 0 : 1;
1163 /* Compare two values VAL1 and VAL2. Return
1165 -2 if VAL1 and VAL2 cannot be compared at compile-time,
1168 +1 if VAL1 > VAL2, and
1171 This is similar to tree_int_cst_compare but supports pointer values
1172 and values that cannot be compared at compile time.
1174 If STRICT_OVERFLOW_P is not NULL, then set *STRICT_OVERFLOW_P to
1175 true if the return value is only valid if we assume that signed
1176 overflow is undefined. */
1179 compare_values_warnv (tree val1
, tree val2
, bool *strict_overflow_p
)
1184 /* Below we rely on the fact that VAL1 and VAL2 are both pointers or
1186 gcc_assert (POINTER_TYPE_P (TREE_TYPE (val1
))
1187 == POINTER_TYPE_P (TREE_TYPE (val2
)));
1189 /* Convert the two values into the same type. This is needed because
1190 sizetype causes sign extension even for unsigned types. */
1191 val2
= fold_convert (TREE_TYPE (val1
), val2
);
1192 STRIP_USELESS_TYPE_CONVERSION (val2
);
1194 if ((TREE_CODE (val1
) == SSA_NAME
1195 || (TREE_CODE (val1
) == NEGATE_EXPR
1196 && TREE_CODE (TREE_OPERAND (val1
, 0)) == SSA_NAME
)
1197 || TREE_CODE (val1
) == PLUS_EXPR
1198 || TREE_CODE (val1
) == MINUS_EXPR
)
1199 && (TREE_CODE (val2
) == SSA_NAME
1200 || (TREE_CODE (val2
) == NEGATE_EXPR
1201 && TREE_CODE (TREE_OPERAND (val2
, 0)) == SSA_NAME
)
1202 || TREE_CODE (val2
) == PLUS_EXPR
1203 || TREE_CODE (val2
) == MINUS_EXPR
))
1205 tree n1
, c1
, n2
, c2
;
1206 enum tree_code code1
, code2
;
1208 /* If VAL1 and VAL2 are of the form '[-]NAME [+-] CST' or 'NAME',
1209 return -1 or +1 accordingly. If VAL1 and VAL2 don't use the
1210 same name, return -2. */
1211 if (TREE_CODE (val1
) == SSA_NAME
|| TREE_CODE (val1
) == NEGATE_EXPR
)
1219 code1
= TREE_CODE (val1
);
1220 n1
= TREE_OPERAND (val1
, 0);
1221 c1
= TREE_OPERAND (val1
, 1);
1222 if (tree_int_cst_sgn (c1
) == -1)
1224 if (is_negative_overflow_infinity (c1
))
1226 c1
= fold_unary_to_constant (NEGATE_EXPR
, TREE_TYPE (c1
), c1
);
1229 code1
= code1
== MINUS_EXPR
? PLUS_EXPR
: MINUS_EXPR
;
1233 if (TREE_CODE (val2
) == SSA_NAME
|| TREE_CODE (val2
) == NEGATE_EXPR
)
1241 code2
= TREE_CODE (val2
);
1242 n2
= TREE_OPERAND (val2
, 0);
1243 c2
= TREE_OPERAND (val2
, 1);
1244 if (tree_int_cst_sgn (c2
) == -1)
1246 if (is_negative_overflow_infinity (c2
))
1248 c2
= fold_unary_to_constant (NEGATE_EXPR
, TREE_TYPE (c2
), c2
);
1251 code2
= code2
== MINUS_EXPR
? PLUS_EXPR
: MINUS_EXPR
;
1255 /* Both values must use the same name. */
1256 if (TREE_CODE (n1
) == NEGATE_EXPR
&& TREE_CODE (n2
) == NEGATE_EXPR
)
1258 n1
= TREE_OPERAND (n1
, 0);
1259 n2
= TREE_OPERAND (n2
, 0);
1264 if (code1
== SSA_NAME
&& code2
== SSA_NAME
)
1268 /* If overflow is defined we cannot simplify more. */
1269 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (val1
)))
1272 if (strict_overflow_p
!= NULL
1273 && (code1
== SSA_NAME
|| !TREE_NO_WARNING (val1
))
1274 && (code2
== SSA_NAME
|| !TREE_NO_WARNING (val2
)))
1275 *strict_overflow_p
= true;
1277 if (code1
== SSA_NAME
)
1279 if (code2
== PLUS_EXPR
)
1280 /* NAME < NAME + CST */
1282 else if (code2
== MINUS_EXPR
)
1283 /* NAME > NAME - CST */
1286 else if (code1
== PLUS_EXPR
)
1288 if (code2
== SSA_NAME
)
1289 /* NAME + CST > NAME */
1291 else if (code2
== PLUS_EXPR
)
1292 /* NAME + CST1 > NAME + CST2, if CST1 > CST2 */
1293 return compare_values_warnv (c1
, c2
, strict_overflow_p
);
1294 else if (code2
== MINUS_EXPR
)
1295 /* NAME + CST1 > NAME - CST2 */
1298 else if (code1
== MINUS_EXPR
)
1300 if (code2
== SSA_NAME
)
1301 /* NAME - CST < NAME */
1303 else if (code2
== PLUS_EXPR
)
1304 /* NAME - CST1 < NAME + CST2 */
1306 else if (code2
== MINUS_EXPR
)
1307 /* NAME - CST1 > NAME - CST2, if CST1 < CST2. Notice that
1308 C1 and C2 are swapped in the call to compare_values. */
1309 return compare_values_warnv (c2
, c1
, strict_overflow_p
);
1315 /* We cannot compare non-constants. */
1316 if (!is_gimple_min_invariant (val1
) || !is_gimple_min_invariant (val2
))
1319 if (!POINTER_TYPE_P (TREE_TYPE (val1
)))
1321 /* We cannot compare overflowed values, except for overflow
1323 if (TREE_OVERFLOW (val1
) || TREE_OVERFLOW (val2
))
1325 if (strict_overflow_p
!= NULL
)
1326 *strict_overflow_p
= true;
1327 if (is_negative_overflow_infinity (val1
))
1328 return is_negative_overflow_infinity (val2
) ? 0 : -1;
1329 else if (is_negative_overflow_infinity (val2
))
1331 else if (is_positive_overflow_infinity (val1
))
1332 return is_positive_overflow_infinity (val2
) ? 0 : 1;
1333 else if (is_positive_overflow_infinity (val2
))
1338 return tree_int_cst_compare (val1
, val2
);
1344 /* First see if VAL1 and VAL2 are not the same. */
1345 if (val1
== val2
|| operand_equal_p (val1
, val2
, 0))
1348 /* If VAL1 is a lower address than VAL2, return -1. */
1349 if (operand_less_p (val1
, val2
) == 1)
1352 /* If VAL1 is a higher address than VAL2, return +1. */
1353 if (operand_less_p (val2
, val1
) == 1)
1356 /* If VAL1 is different than VAL2, return +2.
1357 For integer constants we either have already returned -1 or 1
1358 or they are equivalent. We still might succeed in proving
1359 something about non-trivial operands. */
1360 if (TREE_CODE (val1
) != INTEGER_CST
1361 || TREE_CODE (val2
) != INTEGER_CST
)
1363 t
= fold_binary_to_constant (NE_EXPR
, boolean_type_node
, val1
, val2
);
1364 if (t
&& integer_onep (t
))
1372 /* Compare values like compare_values_warnv, but treat comparisons of
1373 nonconstants which rely on undefined overflow as incomparable. */
1376 compare_values (tree val1
, tree val2
)
1382 ret
= compare_values_warnv (val1
, val2
, &sop
);
1384 && (!is_gimple_min_invariant (val1
) || !is_gimple_min_invariant (val2
)))
1390 /* Return 1 if VAL is inside value range MIN <= VAL <= MAX,
1391 0 if VAL is not inside [MIN, MAX],
1392 -2 if we cannot tell either way.
1394 Benchmark compile/20001226-1.c compilation time after changing this
1398 value_inside_range (tree val
, tree min
, tree max
)
1402 cmp1
= operand_less_p (val
, min
);
1408 cmp2
= operand_less_p (max
, val
);
1416 /* Return true if value ranges VR0 and VR1 have a non-empty
1419 Benchmark compile/20001226-1.c compilation time after changing this
1424 value_ranges_intersect_p (value_range
*vr0
, value_range
*vr1
)
1426 /* The value ranges do not intersect if the maximum of the first range is
1427 less than the minimum of the second range or vice versa.
1428 When those relations are unknown, we can't do any better. */
1429 if (operand_less_p (vr0
->max
, vr1
->min
) != 0)
1431 if (operand_less_p (vr1
->max
, vr0
->min
) != 0)
1437 /* Return 1 if [MIN, MAX] includes the value zero, 0 if it does not
1438 include the value zero, -2 if we cannot tell. */
1441 range_includes_zero_p (tree min
, tree max
)
1443 tree zero
= build_int_cst (TREE_TYPE (min
), 0);
1444 return value_inside_range (zero
, min
, max
);
1447 /* Return true if *VR is know to only contain nonnegative values. */
1450 value_range_nonnegative_p (value_range
*vr
)
1452 /* Testing for VR_ANTI_RANGE is not useful here as any anti-range
1453 which would return a useful value should be encoded as a
1455 if (vr
->type
== VR_RANGE
)
1457 int result
= compare_values (vr
->min
, integer_zero_node
);
1458 return (result
== 0 || result
== 1);
1464 /* If *VR has a value rante that is a single constant value return that,
1465 otherwise return NULL_TREE. */
1468 value_range_constant_singleton (value_range
*vr
)
1470 if (vr
->type
== VR_RANGE
1471 && operand_equal_p (vr
->min
, vr
->max
, 0)
1472 && is_gimple_min_invariant (vr
->min
))
1478 /* If OP has a value range with a single constant value return that,
1479 otherwise return NULL_TREE. This returns OP itself if OP is a
1483 op_with_constant_singleton_value_range (tree op
)
1485 if (is_gimple_min_invariant (op
))
1488 if (TREE_CODE (op
) != SSA_NAME
)
1491 return value_range_constant_singleton (get_value_range (op
));
1494 /* Return true if op is in a boolean [0, 1] value-range. */
1497 op_with_boolean_value_range_p (tree op
)
1501 if (TYPE_PRECISION (TREE_TYPE (op
)) == 1)
1504 if (integer_zerop (op
)
1505 || integer_onep (op
))
1508 if (TREE_CODE (op
) != SSA_NAME
)
1511 vr
= get_value_range (op
);
1512 return (vr
->type
== VR_RANGE
1513 && integer_zerop (vr
->min
)
1514 && integer_onep (vr
->max
));
1517 /* Extract value range information from an ASSERT_EXPR EXPR and store
1521 extract_range_from_assert (value_range
*vr_p
, tree expr
)
1523 tree var
, cond
, limit
, min
, max
, type
;
1524 value_range
*limit_vr
;
1525 enum tree_code cond_code
;
1527 var
= ASSERT_EXPR_VAR (expr
);
1528 cond
= ASSERT_EXPR_COND (expr
);
1530 gcc_assert (COMPARISON_CLASS_P (cond
));
1532 /* Find VAR in the ASSERT_EXPR conditional. */
1533 if (var
== TREE_OPERAND (cond
, 0)
1534 || TREE_CODE (TREE_OPERAND (cond
, 0)) == PLUS_EXPR
1535 || TREE_CODE (TREE_OPERAND (cond
, 0)) == NOP_EXPR
)
1537 /* If the predicate is of the form VAR COMP LIMIT, then we just
1538 take LIMIT from the RHS and use the same comparison code. */
1539 cond_code
= TREE_CODE (cond
);
1540 limit
= TREE_OPERAND (cond
, 1);
1541 cond
= TREE_OPERAND (cond
, 0);
1545 /* If the predicate is of the form LIMIT COMP VAR, then we need
1546 to flip around the comparison code to create the proper range
1548 cond_code
= swap_tree_comparison (TREE_CODE (cond
));
1549 limit
= TREE_OPERAND (cond
, 0);
1550 cond
= TREE_OPERAND (cond
, 1);
1553 limit
= avoid_overflow_infinity (limit
);
1555 type
= TREE_TYPE (var
);
1556 gcc_assert (limit
!= var
);
1558 /* For pointer arithmetic, we only keep track of pointer equality
1560 if (POINTER_TYPE_P (type
) && cond_code
!= NE_EXPR
&& cond_code
!= EQ_EXPR
)
1562 set_value_range_to_varying (vr_p
);
1566 /* If LIMIT is another SSA name and LIMIT has a range of its own,
1567 try to use LIMIT's range to avoid creating symbolic ranges
1569 limit_vr
= (TREE_CODE (limit
) == SSA_NAME
) ? get_value_range (limit
) : NULL
;
1571 /* LIMIT's range is only interesting if it has any useful information. */
1573 && (limit_vr
->type
== VR_UNDEFINED
1574 || limit_vr
->type
== VR_VARYING
1575 || symbolic_range_p (limit_vr
)))
1578 /* Initially, the new range has the same set of equivalences of
1579 VAR's range. This will be revised before returning the final
1580 value. Since assertions may be chained via mutually exclusive
1581 predicates, we will need to trim the set of equivalences before
1583 gcc_assert (vr_p
->equiv
== NULL
);
1584 add_equivalence (&vr_p
->equiv
, var
);
1586 /* Extract a new range based on the asserted comparison for VAR and
1587 LIMIT's value range. Notice that if LIMIT has an anti-range, we
1588 will only use it for equality comparisons (EQ_EXPR). For any
1589 other kind of assertion, we cannot derive a range from LIMIT's
1590 anti-range that can be used to describe the new range. For
1591 instance, ASSERT_EXPR <x_2, x_2 <= b_4>. If b_4 is ~[2, 10],
1592 then b_4 takes on the ranges [-INF, 1] and [11, +INF]. There is
1593 no single range for x_2 that could describe LE_EXPR, so we might
1594 as well build the range [b_4, +INF] for it.
1595 One special case we handle is extracting a range from a
1596 range test encoded as (unsigned)var + CST <= limit. */
1597 if (TREE_CODE (cond
) == NOP_EXPR
1598 || TREE_CODE (cond
) == PLUS_EXPR
)
1600 if (TREE_CODE (cond
) == PLUS_EXPR
)
1602 min
= fold_build1 (NEGATE_EXPR
, TREE_TYPE (TREE_OPERAND (cond
, 1)),
1603 TREE_OPERAND (cond
, 1));
1604 max
= int_const_binop (PLUS_EXPR
, limit
, min
);
1605 cond
= TREE_OPERAND (cond
, 0);
1609 min
= build_int_cst (TREE_TYPE (var
), 0);
1613 /* Make sure to not set TREE_OVERFLOW on the final type
1614 conversion. We are willingly interpreting large positive
1615 unsigned values as negative signed values here. */
1616 min
= force_fit_type (TREE_TYPE (var
), wi::to_widest (min
), 0, false);
1617 max
= force_fit_type (TREE_TYPE (var
), wi::to_widest (max
), 0, false);
1619 /* We can transform a max, min range to an anti-range or
1620 vice-versa. Use set_and_canonicalize_value_range which does
1622 if (cond_code
== LE_EXPR
)
1623 set_and_canonicalize_value_range (vr_p
, VR_RANGE
,
1624 min
, max
, vr_p
->equiv
);
1625 else if (cond_code
== GT_EXPR
)
1626 set_and_canonicalize_value_range (vr_p
, VR_ANTI_RANGE
,
1627 min
, max
, vr_p
->equiv
);
1631 else if (cond_code
== EQ_EXPR
)
1633 enum value_range_type range_type
;
1637 range_type
= limit_vr
->type
;
1638 min
= limit_vr
->min
;
1639 max
= limit_vr
->max
;
1643 range_type
= VR_RANGE
;
1648 set_value_range (vr_p
, range_type
, min
, max
, vr_p
->equiv
);
1650 /* When asserting the equality VAR == LIMIT and LIMIT is another
1651 SSA name, the new range will also inherit the equivalence set
1653 if (TREE_CODE (limit
) == SSA_NAME
)
1654 add_equivalence (&vr_p
->equiv
, limit
);
1656 else if (cond_code
== NE_EXPR
)
1658 /* As described above, when LIMIT's range is an anti-range and
1659 this assertion is an inequality (NE_EXPR), then we cannot
1660 derive anything from the anti-range. For instance, if
1661 LIMIT's range was ~[0, 0], the assertion 'VAR != LIMIT' does
1662 not imply that VAR's range is [0, 0]. So, in the case of
1663 anti-ranges, we just assert the inequality using LIMIT and
1666 If LIMIT_VR is a range, we can only use it to build a new
1667 anti-range if LIMIT_VR is a single-valued range. For
1668 instance, if LIMIT_VR is [0, 1], the predicate
1669 VAR != [0, 1] does not mean that VAR's range is ~[0, 1].
1670 Rather, it means that for value 0 VAR should be ~[0, 0]
1671 and for value 1, VAR should be ~[1, 1]. We cannot
1672 represent these ranges.
1674 The only situation in which we can build a valid
1675 anti-range is when LIMIT_VR is a single-valued range
1676 (i.e., LIMIT_VR->MIN == LIMIT_VR->MAX). In that case,
1677 build the anti-range ~[LIMIT_VR->MIN, LIMIT_VR->MAX]. */
1679 && limit_vr
->type
== VR_RANGE
1680 && compare_values (limit_vr
->min
, limit_vr
->max
) == 0)
1682 min
= limit_vr
->min
;
1683 max
= limit_vr
->max
;
1687 /* In any other case, we cannot use LIMIT's range to build a
1688 valid anti-range. */
1692 /* If MIN and MAX cover the whole range for their type, then
1693 just use the original LIMIT. */
1694 if (INTEGRAL_TYPE_P (type
)
1695 && vrp_val_is_min (min
)
1696 && vrp_val_is_max (max
))
1699 set_and_canonicalize_value_range (vr_p
, VR_ANTI_RANGE
,
1700 min
, max
, vr_p
->equiv
);
1702 else if (cond_code
== LE_EXPR
|| cond_code
== LT_EXPR
)
1704 min
= TYPE_MIN_VALUE (type
);
1706 if (limit_vr
== NULL
|| limit_vr
->type
== VR_ANTI_RANGE
)
1710 /* If LIMIT_VR is of the form [N1, N2], we need to build the
1711 range [MIN, N2] for LE_EXPR and [MIN, N2 - 1] for
1713 max
= limit_vr
->max
;
1716 /* If the maximum value forces us to be out of bounds, simply punt.
1717 It would be pointless to try and do anything more since this
1718 all should be optimized away above us. */
1719 if ((cond_code
== LT_EXPR
1720 && compare_values (max
, min
) == 0)
1721 || is_overflow_infinity (max
))
1722 set_value_range_to_varying (vr_p
);
1725 /* For LT_EXPR, we create the range [MIN, MAX - 1]. */
1726 if (cond_code
== LT_EXPR
)
1728 if (TYPE_PRECISION (TREE_TYPE (max
)) == 1
1729 && !TYPE_UNSIGNED (TREE_TYPE (max
)))
1730 max
= fold_build2 (PLUS_EXPR
, TREE_TYPE (max
), max
,
1731 build_int_cst (TREE_TYPE (max
), -1));
1733 max
= fold_build2 (MINUS_EXPR
, TREE_TYPE (max
), max
,
1734 build_int_cst (TREE_TYPE (max
), 1));
1736 TREE_NO_WARNING (max
) = 1;
1739 set_value_range (vr_p
, VR_RANGE
, min
, max
, vr_p
->equiv
);
1742 else if (cond_code
== GE_EXPR
|| cond_code
== GT_EXPR
)
1744 max
= TYPE_MAX_VALUE (type
);
1746 if (limit_vr
== NULL
|| limit_vr
->type
== VR_ANTI_RANGE
)
1750 /* If LIMIT_VR is of the form [N1, N2], we need to build the
1751 range [N1, MAX] for GE_EXPR and [N1 + 1, MAX] for
1753 min
= limit_vr
->min
;
1756 /* If the minimum value forces us to be out of bounds, simply punt.
1757 It would be pointless to try and do anything more since this
1758 all should be optimized away above us. */
1759 if ((cond_code
== GT_EXPR
1760 && compare_values (min
, max
) == 0)
1761 || is_overflow_infinity (min
))
1762 set_value_range_to_varying (vr_p
);
1765 /* For GT_EXPR, we create the range [MIN + 1, MAX]. */
1766 if (cond_code
== GT_EXPR
)
1768 if (TYPE_PRECISION (TREE_TYPE (min
)) == 1
1769 && !TYPE_UNSIGNED (TREE_TYPE (min
)))
1770 min
= fold_build2 (MINUS_EXPR
, TREE_TYPE (min
), min
,
1771 build_int_cst (TREE_TYPE (min
), -1));
1773 min
= fold_build2 (PLUS_EXPR
, TREE_TYPE (min
), min
,
1774 build_int_cst (TREE_TYPE (min
), 1));
1776 TREE_NO_WARNING (min
) = 1;
1779 set_value_range (vr_p
, VR_RANGE
, min
, max
, vr_p
->equiv
);
1785 /* Finally intersect the new range with what we already know about var. */
1786 vrp_intersect_ranges (vr_p
, get_value_range (var
));
1790 /* Extract range information from SSA name VAR and store it in VR. If
1791 VAR has an interesting range, use it. Otherwise, create the
1792 range [VAR, VAR] and return it. This is useful in situations where
1793 we may have conditionals testing values of VARYING names. For
1800 Even if y_5 is deemed VARYING, we can determine that x_3 > y_5 is
1804 extract_range_from_ssa_name (value_range
*vr
, tree var
)
1806 value_range
*var_vr
= get_value_range (var
);
1808 if (var_vr
->type
!= VR_VARYING
)
1809 copy_value_range (vr
, var_vr
);
1811 set_value_range (vr
, VR_RANGE
, var
, var
, NULL
);
1813 add_equivalence (&vr
->equiv
, var
);
1817 /* Wrapper around int_const_binop. If the operation overflows and we
1818 are not using wrapping arithmetic, then adjust the result to be
1819 -INF or +INF depending on CODE, VAL1 and VAL2. This can return
1820 NULL_TREE if we need to use an overflow infinity representation but
1821 the type does not support it. */
1824 vrp_int_const_binop (enum tree_code code
, tree val1
, tree val2
)
1828 res
= int_const_binop (code
, val1
, val2
);
1830 /* If we are using unsigned arithmetic, operate symbolically
1831 on -INF and +INF as int_const_binop only handles signed overflow. */
1832 if (TYPE_UNSIGNED (TREE_TYPE (val1
)))
1834 int checkz
= compare_values (res
, val1
);
1835 bool overflow
= false;
1837 /* Ensure that res = val1 [+*] val2 >= val1
1838 or that res = val1 - val2 <= val1. */
1839 if ((code
== PLUS_EXPR
1840 && !(checkz
== 1 || checkz
== 0))
1841 || (code
== MINUS_EXPR
1842 && !(checkz
== 0 || checkz
== -1)))
1846 /* Checking for multiplication overflow is done by dividing the
1847 output of the multiplication by the first input of the
1848 multiplication. If the result of that division operation is
1849 not equal to the second input of the multiplication, then the
1850 multiplication overflowed. */
1851 else if (code
== MULT_EXPR
&& !integer_zerop (val1
))
1853 tree tmp
= int_const_binop (TRUNC_DIV_EXPR
,
1856 int check
= compare_values (tmp
, val2
);
1864 res
= copy_node (res
);
1865 TREE_OVERFLOW (res
) = 1;
1869 else if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (val1
)))
1870 /* If the singed operation wraps then int_const_binop has done
1871 everything we want. */
1873 /* Signed division of -1/0 overflows and by the time it gets here
1874 returns NULL_TREE. */
1877 else if ((TREE_OVERFLOW (res
)
1878 && !TREE_OVERFLOW (val1
)
1879 && !TREE_OVERFLOW (val2
))
1880 || is_overflow_infinity (val1
)
1881 || is_overflow_infinity (val2
))
1883 /* If the operation overflowed but neither VAL1 nor VAL2 are
1884 overflown, return -INF or +INF depending on the operation
1885 and the combination of signs of the operands. */
1886 int sgn1
= tree_int_cst_sgn (val1
);
1887 int sgn2
= tree_int_cst_sgn (val2
);
1889 if (needs_overflow_infinity (TREE_TYPE (res
))
1890 && !supports_overflow_infinity (TREE_TYPE (res
)))
1893 /* We have to punt on adding infinities of different signs,
1894 since we can't tell what the sign of the result should be.
1895 Likewise for subtracting infinities of the same sign. */
1896 if (((code
== PLUS_EXPR
&& sgn1
!= sgn2
)
1897 || (code
== MINUS_EXPR
&& sgn1
== sgn2
))
1898 && is_overflow_infinity (val1
)
1899 && is_overflow_infinity (val2
))
1902 /* Don't try to handle division or shifting of infinities. */
1903 if ((code
== TRUNC_DIV_EXPR
1904 || code
== FLOOR_DIV_EXPR
1905 || code
== CEIL_DIV_EXPR
1906 || code
== EXACT_DIV_EXPR
1907 || code
== ROUND_DIV_EXPR
1908 || code
== RSHIFT_EXPR
)
1909 && (is_overflow_infinity (val1
)
1910 || is_overflow_infinity (val2
)))
1913 /* Notice that we only need to handle the restricted set of
1914 operations handled by extract_range_from_binary_expr.
1915 Among them, only multiplication, addition and subtraction
1916 can yield overflow without overflown operands because we
1917 are working with integral types only... except in the
1918 case VAL1 = -INF and VAL2 = -1 which overflows to +INF
1919 for division too. */
1921 /* For multiplication, the sign of the overflow is given
1922 by the comparison of the signs of the operands. */
1923 if ((code
== MULT_EXPR
&& sgn1
== sgn2
)
1924 /* For addition, the operands must be of the same sign
1925 to yield an overflow. Its sign is therefore that
1926 of one of the operands, for example the first. For
1927 infinite operands X + -INF is negative, not positive. */
1928 || (code
== PLUS_EXPR
1930 ? !is_negative_overflow_infinity (val2
)
1931 : is_positive_overflow_infinity (val2
)))
1932 /* For subtraction, non-infinite operands must be of
1933 different signs to yield an overflow. Its sign is
1934 therefore that of the first operand or the opposite of
1935 that of the second operand. A first operand of 0 counts
1936 as positive here, for the corner case 0 - (-INF), which
1937 overflows, but must yield +INF. For infinite operands 0
1938 - INF is negative, not positive. */
1939 || (code
== MINUS_EXPR
1941 ? !is_positive_overflow_infinity (val2
)
1942 : is_negative_overflow_infinity (val2
)))
1943 /* We only get in here with positive shift count, so the
1944 overflow direction is the same as the sign of val1.
1945 Actually rshift does not overflow at all, but we only
1946 handle the case of shifting overflowed -INF and +INF. */
1947 || (code
== RSHIFT_EXPR
1949 /* For division, the only case is -INF / -1 = +INF. */
1950 || code
== TRUNC_DIV_EXPR
1951 || code
== FLOOR_DIV_EXPR
1952 || code
== CEIL_DIV_EXPR
1953 || code
== EXACT_DIV_EXPR
1954 || code
== ROUND_DIV_EXPR
)
1955 return (needs_overflow_infinity (TREE_TYPE (res
))
1956 ? positive_overflow_infinity (TREE_TYPE (res
))
1957 : TYPE_MAX_VALUE (TREE_TYPE (res
)));
1959 return (needs_overflow_infinity (TREE_TYPE (res
))
1960 ? negative_overflow_infinity (TREE_TYPE (res
))
1961 : TYPE_MIN_VALUE (TREE_TYPE (res
)));
1968 /* For range VR compute two wide_int bitmasks. In *MAY_BE_NONZERO
1969 bitmask if some bit is unset, it means for all numbers in the range
1970 the bit is 0, otherwise it might be 0 or 1. In *MUST_BE_NONZERO
1971 bitmask if some bit is set, it means for all numbers in the range
1972 the bit is 1, otherwise it might be 0 or 1. */
1975 zero_nonzero_bits_from_vr (const tree expr_type
,
1977 wide_int
*may_be_nonzero
,
1978 wide_int
*must_be_nonzero
)
1980 *may_be_nonzero
= wi::minus_one (TYPE_PRECISION (expr_type
));
1981 *must_be_nonzero
= wi::zero (TYPE_PRECISION (expr_type
));
1982 if (!range_int_cst_p (vr
)
1983 || is_overflow_infinity (vr
->min
)
1984 || is_overflow_infinity (vr
->max
))
1987 if (range_int_cst_singleton_p (vr
))
1989 *may_be_nonzero
= vr
->min
;
1990 *must_be_nonzero
= *may_be_nonzero
;
1992 else if (tree_int_cst_sgn (vr
->min
) >= 0
1993 || tree_int_cst_sgn (vr
->max
) < 0)
1995 wide_int xor_mask
= wi::bit_xor (vr
->min
, vr
->max
);
1996 *may_be_nonzero
= wi::bit_or (vr
->min
, vr
->max
);
1997 *must_be_nonzero
= wi::bit_and (vr
->min
, vr
->max
);
2000 wide_int mask
= wi::mask (wi::floor_log2 (xor_mask
), false,
2001 may_be_nonzero
->get_precision ());
2002 *may_be_nonzero
= *may_be_nonzero
| mask
;
2003 *must_be_nonzero
= must_be_nonzero
->and_not (mask
);
2010 /* Create two value-ranges in *VR0 and *VR1 from the anti-range *AR
2011 so that *VR0 U *VR1 == *AR. Returns true if that is possible,
2012 false otherwise. If *AR can be represented with a single range
2013 *VR1 will be VR_UNDEFINED. */
2016 ranges_from_anti_range (value_range
*ar
,
2017 value_range
*vr0
, value_range
*vr1
)
2019 tree type
= TREE_TYPE (ar
->min
);
2021 vr0
->type
= VR_UNDEFINED
;
2022 vr1
->type
= VR_UNDEFINED
;
2024 if (ar
->type
!= VR_ANTI_RANGE
2025 || TREE_CODE (ar
->min
) != INTEGER_CST
2026 || TREE_CODE (ar
->max
) != INTEGER_CST
2027 || !vrp_val_min (type
)
2028 || !vrp_val_max (type
))
2031 if (!vrp_val_is_min (ar
->min
))
2033 vr0
->type
= VR_RANGE
;
2034 vr0
->min
= vrp_val_min (type
);
2035 vr0
->max
= wide_int_to_tree (type
, wi::sub (ar
->min
, 1));
2037 if (!vrp_val_is_max (ar
->max
))
2039 vr1
->type
= VR_RANGE
;
2040 vr1
->min
= wide_int_to_tree (type
, wi::add (ar
->max
, 1));
2041 vr1
->max
= vrp_val_max (type
);
2043 if (vr0
->type
== VR_UNDEFINED
)
2046 vr1
->type
= VR_UNDEFINED
;
2049 return vr0
->type
!= VR_UNDEFINED
;
2052 /* Helper to extract a value-range *VR for a multiplicative operation
2056 extract_range_from_multiplicative_op_1 (value_range
*vr
,
2057 enum tree_code code
,
2058 value_range
*vr0
, value_range
*vr1
)
2060 enum value_range_type type
;
2067 /* Multiplications, divisions and shifts are a bit tricky to handle,
2068 depending on the mix of signs we have in the two ranges, we
2069 need to operate on different values to get the minimum and
2070 maximum values for the new range. One approach is to figure
2071 out all the variations of range combinations and do the
2074 However, this involves several calls to compare_values and it
2075 is pretty convoluted. It's simpler to do the 4 operations
2076 (MIN0 OP MIN1, MIN0 OP MAX1, MAX0 OP MIN1 and MAX0 OP MAX0 OP
2077 MAX1) and then figure the smallest and largest values to form
2079 gcc_assert (code
== MULT_EXPR
2080 || code
== TRUNC_DIV_EXPR
2081 || code
== FLOOR_DIV_EXPR
2082 || code
== CEIL_DIV_EXPR
2083 || code
== EXACT_DIV_EXPR
2084 || code
== ROUND_DIV_EXPR
2085 || code
== RSHIFT_EXPR
2086 || code
== LSHIFT_EXPR
);
2087 gcc_assert ((vr0
->type
== VR_RANGE
2088 || (code
== MULT_EXPR
&& vr0
->type
== VR_ANTI_RANGE
))
2089 && vr0
->type
== vr1
->type
);
2093 /* Compute the 4 cross operations. */
2095 val
[0] = vrp_int_const_binop (code
, vr0
->min
, vr1
->min
);
2096 if (val
[0] == NULL_TREE
)
2099 if (vr1
->max
== vr1
->min
)
2103 val
[1] = vrp_int_const_binop (code
, vr0
->min
, vr1
->max
);
2104 if (val
[1] == NULL_TREE
)
2108 if (vr0
->max
== vr0
->min
)
2112 val
[2] = vrp_int_const_binop (code
, vr0
->max
, vr1
->min
);
2113 if (val
[2] == NULL_TREE
)
2117 if (vr0
->min
== vr0
->max
|| vr1
->min
== vr1
->max
)
2121 val
[3] = vrp_int_const_binop (code
, vr0
->max
, vr1
->max
);
2122 if (val
[3] == NULL_TREE
)
2128 set_value_range_to_varying (vr
);
2132 /* Set MIN to the minimum of VAL[i] and MAX to the maximum
2136 for (i
= 1; i
< 4; i
++)
2138 if (!is_gimple_min_invariant (min
)
2139 || (TREE_OVERFLOW (min
) && !is_overflow_infinity (min
))
2140 || !is_gimple_min_invariant (max
)
2141 || (TREE_OVERFLOW (max
) && !is_overflow_infinity (max
)))
2146 if (!is_gimple_min_invariant (val
[i
])
2147 || (TREE_OVERFLOW (val
[i
])
2148 && !is_overflow_infinity (val
[i
])))
2150 /* If we found an overflowed value, set MIN and MAX
2151 to it so that we set the resulting range to
2157 if (compare_values (val
[i
], min
) == -1)
2160 if (compare_values (val
[i
], max
) == 1)
2165 /* If either MIN or MAX overflowed, then set the resulting range to
2166 VARYING. But we do accept an overflow infinity
2168 if (min
== NULL_TREE
2169 || !is_gimple_min_invariant (min
)
2170 || (TREE_OVERFLOW (min
) && !is_overflow_infinity (min
))
2172 || !is_gimple_min_invariant (max
)
2173 || (TREE_OVERFLOW (max
) && !is_overflow_infinity (max
)))
2175 set_value_range_to_varying (vr
);
2181 2) [-INF, +-INF(OVF)]
2182 3) [+-INF(OVF), +INF]
2183 4) [+-INF(OVF), +-INF(OVF)]
2184 We learn nothing when we have INF and INF(OVF) on both sides.
2185 Note that we do accept [-INF, -INF] and [+INF, +INF] without
2187 if ((vrp_val_is_min (min
) || is_overflow_infinity (min
))
2188 && (vrp_val_is_max (max
) || is_overflow_infinity (max
)))
2190 set_value_range_to_varying (vr
);
2194 cmp
= compare_values (min
, max
);
2195 if (cmp
== -2 || cmp
== 1)
2197 /* If the new range has its limits swapped around (MIN > MAX),
2198 then the operation caused one of them to wrap around, mark
2199 the new range VARYING. */
2200 set_value_range_to_varying (vr
);
2203 set_value_range (vr
, type
, min
, max
, NULL
);
2206 /* Extract range information from a binary operation CODE based on
2207 the ranges of each of its operands *VR0 and *VR1 with resulting
2208 type EXPR_TYPE. The resulting range is stored in *VR. */
2211 extract_range_from_binary_expr_1 (value_range
*vr
,
2212 enum tree_code code
, tree expr_type
,
2213 value_range
*vr0_
, value_range
*vr1_
)
2215 value_range vr0
= *vr0_
, vr1
= *vr1_
;
2216 value_range vrtem0
= VR_INITIALIZER
, vrtem1
= VR_INITIALIZER
;
2217 enum value_range_type type
;
2218 tree min
= NULL_TREE
, max
= NULL_TREE
;
2221 if (!INTEGRAL_TYPE_P (expr_type
)
2222 && !POINTER_TYPE_P (expr_type
))
2224 set_value_range_to_varying (vr
);
2228 /* Not all binary expressions can be applied to ranges in a
2229 meaningful way. Handle only arithmetic operations. */
2230 if (code
!= PLUS_EXPR
2231 && code
!= MINUS_EXPR
2232 && code
!= POINTER_PLUS_EXPR
2233 && code
!= MULT_EXPR
2234 && code
!= TRUNC_DIV_EXPR
2235 && code
!= FLOOR_DIV_EXPR
2236 && code
!= CEIL_DIV_EXPR
2237 && code
!= EXACT_DIV_EXPR
2238 && code
!= ROUND_DIV_EXPR
2239 && code
!= TRUNC_MOD_EXPR
2240 && code
!= RSHIFT_EXPR
2241 && code
!= LSHIFT_EXPR
2244 && code
!= BIT_AND_EXPR
2245 && code
!= BIT_IOR_EXPR
2246 && code
!= BIT_XOR_EXPR
)
2248 set_value_range_to_varying (vr
);
2252 /* If both ranges are UNDEFINED, so is the result. */
2253 if (vr0
.type
== VR_UNDEFINED
&& vr1
.type
== VR_UNDEFINED
)
2255 set_value_range_to_undefined (vr
);
2258 /* If one of the ranges is UNDEFINED drop it to VARYING for the following
2259 code. At some point we may want to special-case operations that
2260 have UNDEFINED result for all or some value-ranges of the not UNDEFINED
2262 else if (vr0
.type
== VR_UNDEFINED
)
2263 set_value_range_to_varying (&vr0
);
2264 else if (vr1
.type
== VR_UNDEFINED
)
2265 set_value_range_to_varying (&vr1
);
2267 /* Now canonicalize anti-ranges to ranges when they are not symbolic
2268 and express ~[] op X as ([]' op X) U ([]'' op X). */
2269 if (vr0
.type
== VR_ANTI_RANGE
2270 && ranges_from_anti_range (&vr0
, &vrtem0
, &vrtem1
))
2272 extract_range_from_binary_expr_1 (vr
, code
, expr_type
, &vrtem0
, vr1_
);
2273 if (vrtem1
.type
!= VR_UNDEFINED
)
2275 value_range vrres
= VR_INITIALIZER
;
2276 extract_range_from_binary_expr_1 (&vrres
, code
, expr_type
,
2278 vrp_meet (vr
, &vrres
);
2282 /* Likewise for X op ~[]. */
2283 if (vr1
.type
== VR_ANTI_RANGE
2284 && ranges_from_anti_range (&vr1
, &vrtem0
, &vrtem1
))
2286 extract_range_from_binary_expr_1 (vr
, code
, expr_type
, vr0_
, &vrtem0
);
2287 if (vrtem1
.type
!= VR_UNDEFINED
)
2289 value_range vrres
= VR_INITIALIZER
;
2290 extract_range_from_binary_expr_1 (&vrres
, code
, expr_type
,
2292 vrp_meet (vr
, &vrres
);
2297 /* The type of the resulting value range defaults to VR0.TYPE. */
2300 /* Refuse to operate on VARYING ranges, ranges of different kinds
2301 and symbolic ranges. As an exception, we allow BIT_{AND,IOR}
2302 because we may be able to derive a useful range even if one of
2303 the operands is VR_VARYING or symbolic range. Similarly for
2304 divisions, MIN/MAX and PLUS/MINUS.
2306 TODO, we may be able to derive anti-ranges in some cases. */
2307 if (code
!= BIT_AND_EXPR
2308 && code
!= BIT_IOR_EXPR
2309 && code
!= TRUNC_DIV_EXPR
2310 && code
!= FLOOR_DIV_EXPR
2311 && code
!= CEIL_DIV_EXPR
2312 && code
!= EXACT_DIV_EXPR
2313 && code
!= ROUND_DIV_EXPR
2314 && code
!= TRUNC_MOD_EXPR
2317 && code
!= PLUS_EXPR
2318 && code
!= MINUS_EXPR
2319 && code
!= RSHIFT_EXPR
2320 && (vr0
.type
== VR_VARYING
2321 || vr1
.type
== VR_VARYING
2322 || vr0
.type
!= vr1
.type
2323 || symbolic_range_p (&vr0
)
2324 || symbolic_range_p (&vr1
)))
2326 set_value_range_to_varying (vr
);
2330 /* Now evaluate the expression to determine the new range. */
2331 if (POINTER_TYPE_P (expr_type
))
2333 if (code
== MIN_EXPR
|| code
== MAX_EXPR
)
2335 /* For MIN/MAX expressions with pointers, we only care about
2336 nullness, if both are non null, then the result is nonnull.
2337 If both are null, then the result is null. Otherwise they
2339 if (range_is_nonnull (&vr0
) && range_is_nonnull (&vr1
))
2340 set_value_range_to_nonnull (vr
, expr_type
);
2341 else if (range_is_null (&vr0
) && range_is_null (&vr1
))
2342 set_value_range_to_null (vr
, expr_type
);
2344 set_value_range_to_varying (vr
);
2346 else if (code
== POINTER_PLUS_EXPR
)
2348 /* For pointer types, we are really only interested in asserting
2349 whether the expression evaluates to non-NULL. */
2350 if (range_is_nonnull (&vr0
) || range_is_nonnull (&vr1
))
2351 set_value_range_to_nonnull (vr
, expr_type
);
2352 else if (range_is_null (&vr0
) && range_is_null (&vr1
))
2353 set_value_range_to_null (vr
, expr_type
);
2355 set_value_range_to_varying (vr
);
2357 else if (code
== BIT_AND_EXPR
)
2359 /* For pointer types, we are really only interested in asserting
2360 whether the expression evaluates to non-NULL. */
2361 if (range_is_nonnull (&vr0
) && range_is_nonnull (&vr1
))
2362 set_value_range_to_nonnull (vr
, expr_type
);
2363 else if (range_is_null (&vr0
) || range_is_null (&vr1
))
2364 set_value_range_to_null (vr
, expr_type
);
2366 set_value_range_to_varying (vr
);
2369 set_value_range_to_varying (vr
);
2374 /* For integer ranges, apply the operation to each end of the
2375 range and see what we end up with. */
2376 if (code
== PLUS_EXPR
|| code
== MINUS_EXPR
)
2378 const bool minus_p
= (code
== MINUS_EXPR
);
2379 tree min_op0
= vr0
.min
;
2380 tree min_op1
= minus_p
? vr1
.max
: vr1
.min
;
2381 tree max_op0
= vr0
.max
;
2382 tree max_op1
= minus_p
? vr1
.min
: vr1
.max
;
2383 tree sym_min_op0
= NULL_TREE
;
2384 tree sym_min_op1
= NULL_TREE
;
2385 tree sym_max_op0
= NULL_TREE
;
2386 tree sym_max_op1
= NULL_TREE
;
2387 bool neg_min_op0
, neg_min_op1
, neg_max_op0
, neg_max_op1
;
2389 /* If we have a PLUS or MINUS with two VR_RANGEs, either constant or
2390 single-symbolic ranges, try to compute the precise resulting range,
2391 but only if we know that this resulting range will also be constant
2392 or single-symbolic. */
2393 if (vr0
.type
== VR_RANGE
&& vr1
.type
== VR_RANGE
2394 && (TREE_CODE (min_op0
) == INTEGER_CST
2396 = get_single_symbol (min_op0
, &neg_min_op0
, &min_op0
)))
2397 && (TREE_CODE (min_op1
) == INTEGER_CST
2399 = get_single_symbol (min_op1
, &neg_min_op1
, &min_op1
)))
2400 && (!(sym_min_op0
&& sym_min_op1
)
2401 || (sym_min_op0
== sym_min_op1
2402 && neg_min_op0
== (minus_p
? neg_min_op1
: !neg_min_op1
)))
2403 && (TREE_CODE (max_op0
) == INTEGER_CST
2405 = get_single_symbol (max_op0
, &neg_max_op0
, &max_op0
)))
2406 && (TREE_CODE (max_op1
) == INTEGER_CST
2408 = get_single_symbol (max_op1
, &neg_max_op1
, &max_op1
)))
2409 && (!(sym_max_op0
&& sym_max_op1
)
2410 || (sym_max_op0
== sym_max_op1
2411 && neg_max_op0
== (minus_p
? neg_max_op1
: !neg_max_op1
))))
2413 const signop sgn
= TYPE_SIGN (expr_type
);
2414 const unsigned int prec
= TYPE_PRECISION (expr_type
);
2415 wide_int type_min
, type_max
, wmin
, wmax
;
2419 /* Get the lower and upper bounds of the type. */
2420 if (TYPE_OVERFLOW_WRAPS (expr_type
))
2422 type_min
= wi::min_value (prec
, sgn
);
2423 type_max
= wi::max_value (prec
, sgn
);
2427 type_min
= vrp_val_min (expr_type
);
2428 type_max
= vrp_val_max (expr_type
);
2431 /* Combine the lower bounds, if any. */
2432 if (min_op0
&& min_op1
)
2436 wmin
= wi::sub (min_op0
, min_op1
);
2438 /* Check for overflow. */
2439 if (wi::cmp (0, min_op1
, sgn
)
2440 != wi::cmp (wmin
, min_op0
, sgn
))
2441 min_ovf
= wi::cmp (min_op0
, min_op1
, sgn
);
2445 wmin
= wi::add (min_op0
, min_op1
);
2447 /* Check for overflow. */
2448 if (wi::cmp (min_op1
, 0, sgn
)
2449 != wi::cmp (wmin
, min_op0
, sgn
))
2450 min_ovf
= wi::cmp (min_op0
, wmin
, sgn
);
2456 wmin
= minus_p
? wi::neg (min_op1
) : min_op1
;
2458 wmin
= wi::shwi (0, prec
);
2460 /* Combine the upper bounds, if any. */
2461 if (max_op0
&& max_op1
)
2465 wmax
= wi::sub (max_op0
, max_op1
);
2467 /* Check for overflow. */
2468 if (wi::cmp (0, max_op1
, sgn
)
2469 != wi::cmp (wmax
, max_op0
, sgn
))
2470 max_ovf
= wi::cmp (max_op0
, max_op1
, sgn
);
2474 wmax
= wi::add (max_op0
, max_op1
);
2476 if (wi::cmp (max_op1
, 0, sgn
)
2477 != wi::cmp (wmax
, max_op0
, sgn
))
2478 max_ovf
= wi::cmp (max_op0
, wmax
, sgn
);
2484 wmax
= minus_p
? wi::neg (max_op1
) : max_op1
;
2486 wmax
= wi::shwi (0, prec
);
2488 /* Check for type overflow. */
2491 if (wi::cmp (wmin
, type_min
, sgn
) == -1)
2493 else if (wi::cmp (wmin
, type_max
, sgn
) == 1)
2498 if (wi::cmp (wmax
, type_min
, sgn
) == -1)
2500 else if (wi::cmp (wmax
, type_max
, sgn
) == 1)
2504 /* If we have overflow for the constant part and the resulting
2505 range will be symbolic, drop to VR_VARYING. */
2506 if ((min_ovf
&& sym_min_op0
!= sym_min_op1
)
2507 || (max_ovf
&& sym_max_op0
!= sym_max_op1
))
2509 set_value_range_to_varying (vr
);
2513 if (TYPE_OVERFLOW_WRAPS (expr_type
))
2515 /* If overflow wraps, truncate the values and adjust the
2516 range kind and bounds appropriately. */
2517 wide_int tmin
= wide_int::from (wmin
, prec
, sgn
);
2518 wide_int tmax
= wide_int::from (wmax
, prec
, sgn
);
2519 if (min_ovf
== max_ovf
)
2521 /* No overflow or both overflow or underflow. The
2522 range kind stays VR_RANGE. */
2523 min
= wide_int_to_tree (expr_type
, tmin
);
2524 max
= wide_int_to_tree (expr_type
, tmax
);
2526 else if (min_ovf
== -1 && max_ovf
== 1)
2528 /* Underflow and overflow, drop to VR_VARYING. */
2529 set_value_range_to_varying (vr
);
2534 /* Min underflow or max overflow. The range kind
2535 changes to VR_ANTI_RANGE. */
2536 bool covers
= false;
2537 wide_int tem
= tmin
;
2538 gcc_assert ((min_ovf
== -1 && max_ovf
== 0)
2539 || (max_ovf
== 1 && min_ovf
== 0));
2540 type
= VR_ANTI_RANGE
;
2542 if (wi::cmp (tmin
, tmax
, sgn
) < 0)
2545 if (wi::cmp (tmax
, tem
, sgn
) > 0)
2547 /* If the anti-range would cover nothing, drop to varying.
2548 Likewise if the anti-range bounds are outside of the
2550 if (covers
|| wi::cmp (tmin
, tmax
, sgn
) > 0)
2552 set_value_range_to_varying (vr
);
2555 min
= wide_int_to_tree (expr_type
, tmin
);
2556 max
= wide_int_to_tree (expr_type
, tmax
);
2561 /* If overflow does not wrap, saturate to the types min/max
2565 if (needs_overflow_infinity (expr_type
)
2566 && supports_overflow_infinity (expr_type
))
2567 min
= negative_overflow_infinity (expr_type
);
2569 min
= wide_int_to_tree (expr_type
, type_min
);
2571 else if (min_ovf
== 1)
2573 if (needs_overflow_infinity (expr_type
)
2574 && supports_overflow_infinity (expr_type
))
2575 min
= positive_overflow_infinity (expr_type
);
2577 min
= wide_int_to_tree (expr_type
, type_max
);
2580 min
= wide_int_to_tree (expr_type
, wmin
);
2584 if (needs_overflow_infinity (expr_type
)
2585 && supports_overflow_infinity (expr_type
))
2586 max
= negative_overflow_infinity (expr_type
);
2588 max
= wide_int_to_tree (expr_type
, type_min
);
2590 else if (max_ovf
== 1)
2592 if (needs_overflow_infinity (expr_type
)
2593 && supports_overflow_infinity (expr_type
))
2594 max
= positive_overflow_infinity (expr_type
);
2596 max
= wide_int_to_tree (expr_type
, type_max
);
2599 max
= wide_int_to_tree (expr_type
, wmax
);
2602 if (needs_overflow_infinity (expr_type
)
2603 && supports_overflow_infinity (expr_type
))
2605 if ((min_op0
&& is_negative_overflow_infinity (min_op0
))
2608 ? is_positive_overflow_infinity (min_op1
)
2609 : is_negative_overflow_infinity (min_op1
))))
2610 min
= negative_overflow_infinity (expr_type
);
2611 if ((max_op0
&& is_positive_overflow_infinity (max_op0
))
2614 ? is_negative_overflow_infinity (max_op1
)
2615 : is_positive_overflow_infinity (max_op1
))))
2616 max
= positive_overflow_infinity (expr_type
);
2619 /* If the result lower bound is constant, we're done;
2620 otherwise, build the symbolic lower bound. */
2621 if (sym_min_op0
== sym_min_op1
)
2623 else if (sym_min_op0
)
2624 min
= build_symbolic_expr (expr_type
, sym_min_op0
,
2626 else if (sym_min_op1
)
2627 min
= build_symbolic_expr (expr_type
, sym_min_op1
,
2628 neg_min_op1
^ minus_p
, min
);
2630 /* Likewise for the upper bound. */
2631 if (sym_max_op0
== sym_max_op1
)
2633 else if (sym_max_op0
)
2634 max
= build_symbolic_expr (expr_type
, sym_max_op0
,
2636 else if (sym_max_op1
)
2637 max
= build_symbolic_expr (expr_type
, sym_max_op1
,
2638 neg_max_op1
^ minus_p
, max
);
2642 /* For other cases, for example if we have a PLUS_EXPR with two
2643 VR_ANTI_RANGEs, drop to VR_VARYING. It would take more effort
2644 to compute a precise range for such a case.
2645 ??? General even mixed range kind operations can be expressed
2646 by for example transforming ~[3, 5] + [1, 2] to range-only
2647 operations and a union primitive:
2648 [-INF, 2] + [1, 2] U [5, +INF] + [1, 2]
2649 [-INF+1, 4] U [6, +INF(OVF)]
2650 though usually the union is not exactly representable with
2651 a single range or anti-range as the above is
2652 [-INF+1, +INF(OVF)] intersected with ~[5, 5]
2653 but one could use a scheme similar to equivalences for this. */
2654 set_value_range_to_varying (vr
);
2658 else if (code
== MIN_EXPR
2659 || code
== MAX_EXPR
)
2661 if (vr0
.type
== VR_RANGE
2662 && !symbolic_range_p (&vr0
))
2665 if (vr1
.type
== VR_RANGE
2666 && !symbolic_range_p (&vr1
))
2668 /* For operations that make the resulting range directly
2669 proportional to the original ranges, apply the operation to
2670 the same end of each range. */
2671 min
= vrp_int_const_binop (code
, vr0
.min
, vr1
.min
);
2672 max
= vrp_int_const_binop (code
, vr0
.max
, vr1
.max
);
2674 else if (code
== MIN_EXPR
)
2676 min
= vrp_val_min (expr_type
);
2679 else if (code
== MAX_EXPR
)
2682 max
= vrp_val_max (expr_type
);
2685 else if (vr1
.type
== VR_RANGE
2686 && !symbolic_range_p (&vr1
))
2689 if (code
== MIN_EXPR
)
2691 min
= vrp_val_min (expr_type
);
2694 else if (code
== MAX_EXPR
)
2697 max
= vrp_val_max (expr_type
);
2702 set_value_range_to_varying (vr
);
2706 else if (code
== MULT_EXPR
)
2708 /* Fancy code so that with unsigned, [-3,-1]*[-3,-1] does not
2709 drop to varying. This test requires 2*prec bits if both
2710 operands are signed and 2*prec + 2 bits if either is not. */
2712 signop sign
= TYPE_SIGN (expr_type
);
2713 unsigned int prec
= TYPE_PRECISION (expr_type
);
2715 if (range_int_cst_p (&vr0
)
2716 && range_int_cst_p (&vr1
)
2717 && TYPE_OVERFLOW_WRAPS (expr_type
))
2719 typedef FIXED_WIDE_INT (WIDE_INT_MAX_PRECISION
* 2) vrp_int
;
2720 typedef generic_wide_int
2721 <wi::extended_tree
<WIDE_INT_MAX_PRECISION
* 2> > vrp_int_cst
;
2722 vrp_int sizem1
= wi::mask
<vrp_int
> (prec
, false);
2723 vrp_int size
= sizem1
+ 1;
2725 /* Extend the values using the sign of the result to PREC2.
2726 From here on out, everthing is just signed math no matter
2727 what the input types were. */
2728 vrp_int min0
= vrp_int_cst (vr0
.min
);
2729 vrp_int max0
= vrp_int_cst (vr0
.max
);
2730 vrp_int min1
= vrp_int_cst (vr1
.min
);
2731 vrp_int max1
= vrp_int_cst (vr1
.max
);
2732 /* Canonicalize the intervals. */
2733 if (sign
== UNSIGNED
)
2735 if (wi::ltu_p (size
, min0
+ max0
))
2741 if (wi::ltu_p (size
, min1
+ max1
))
2748 vrp_int prod0
= min0
* min1
;
2749 vrp_int prod1
= min0
* max1
;
2750 vrp_int prod2
= max0
* min1
;
2751 vrp_int prod3
= max0
* max1
;
2753 /* Sort the 4 products so that min is in prod0 and max is in
2755 /* min0min1 > max0max1 */
2756 if (wi::gts_p (prod0
, prod3
))
2757 std::swap (prod0
, prod3
);
2759 /* min0max1 > max0min1 */
2760 if (wi::gts_p (prod1
, prod2
))
2761 std::swap (prod1
, prod2
);
2763 if (wi::gts_p (prod0
, prod1
))
2764 std::swap (prod0
, prod1
);
2766 if (wi::gts_p (prod2
, prod3
))
2767 std::swap (prod2
, prod3
);
2769 /* diff = max - min. */
2770 prod2
= prod3
- prod0
;
2771 if (wi::geu_p (prod2
, sizem1
))
2773 /* the range covers all values. */
2774 set_value_range_to_varying (vr
);
2778 /* The following should handle the wrapping and selecting
2779 VR_ANTI_RANGE for us. */
2780 min
= wide_int_to_tree (expr_type
, prod0
);
2781 max
= wide_int_to_tree (expr_type
, prod3
);
2782 set_and_canonicalize_value_range (vr
, VR_RANGE
, min
, max
, NULL
);
2786 /* If we have an unsigned MULT_EXPR with two VR_ANTI_RANGEs,
2787 drop to VR_VARYING. It would take more effort to compute a
2788 precise range for such a case. For example, if we have
2789 op0 == 65536 and op1 == 65536 with their ranges both being
2790 ~[0,0] on a 32-bit machine, we would have op0 * op1 == 0, so
2791 we cannot claim that the product is in ~[0,0]. Note that we
2792 are guaranteed to have vr0.type == vr1.type at this
2794 if (vr0
.type
== VR_ANTI_RANGE
2795 && !TYPE_OVERFLOW_UNDEFINED (expr_type
))
2797 set_value_range_to_varying (vr
);
2801 extract_range_from_multiplicative_op_1 (vr
, code
, &vr0
, &vr1
);
2804 else if (code
== RSHIFT_EXPR
2805 || code
== LSHIFT_EXPR
)
2807 /* If we have a RSHIFT_EXPR with any shift values outside [0..prec-1],
2808 then drop to VR_VARYING. Outside of this range we get undefined
2809 behavior from the shift operation. We cannot even trust
2810 SHIFT_COUNT_TRUNCATED at this stage, because that applies to rtl
2811 shifts, and the operation at the tree level may be widened. */
2812 if (range_int_cst_p (&vr1
)
2813 && compare_tree_int (vr1
.min
, 0) >= 0
2814 && compare_tree_int (vr1
.max
, TYPE_PRECISION (expr_type
)) == -1)
2816 if (code
== RSHIFT_EXPR
)
2818 /* Even if vr0 is VARYING or otherwise not usable, we can derive
2819 useful ranges just from the shift count. E.g.
2820 x >> 63 for signed 64-bit x is always [-1, 0]. */
2821 if (vr0
.type
!= VR_RANGE
|| symbolic_range_p (&vr0
))
2823 vr0
.type
= type
= VR_RANGE
;
2824 vr0
.min
= vrp_val_min (expr_type
);
2825 vr0
.max
= vrp_val_max (expr_type
);
2827 extract_range_from_multiplicative_op_1 (vr
, code
, &vr0
, &vr1
);
2830 /* We can map lshifts by constants to MULT_EXPR handling. */
2831 else if (code
== LSHIFT_EXPR
2832 && range_int_cst_singleton_p (&vr1
))
2834 bool saved_flag_wrapv
;
2835 value_range vr1p
= VR_INITIALIZER
;
2836 vr1p
.type
= VR_RANGE
;
2837 vr1p
.min
= (wide_int_to_tree
2839 wi::set_bit_in_zero (tree_to_shwi (vr1
.min
),
2840 TYPE_PRECISION (expr_type
))));
2841 vr1p
.max
= vr1p
.min
;
2842 /* We have to use a wrapping multiply though as signed overflow
2843 on lshifts is implementation defined in C89. */
2844 saved_flag_wrapv
= flag_wrapv
;
2846 extract_range_from_binary_expr_1 (vr
, MULT_EXPR
, expr_type
,
2848 flag_wrapv
= saved_flag_wrapv
;
2851 else if (code
== LSHIFT_EXPR
2852 && range_int_cst_p (&vr0
))
2854 int prec
= TYPE_PRECISION (expr_type
);
2855 int overflow_pos
= prec
;
2857 wide_int low_bound
, high_bound
;
2858 bool uns
= TYPE_UNSIGNED (expr_type
);
2859 bool in_bounds
= false;
2864 bound_shift
= overflow_pos
- tree_to_shwi (vr1
.max
);
2865 /* If bound_shift == HOST_BITS_PER_WIDE_INT, the llshift can
2866 overflow. However, for that to happen, vr1.max needs to be
2867 zero, which means vr1 is a singleton range of zero, which
2868 means it should be handled by the previous LSHIFT_EXPR
2870 wide_int bound
= wi::set_bit_in_zero (bound_shift
, prec
);
2871 wide_int complement
= ~(bound
- 1);
2876 high_bound
= complement
;
2877 if (wi::ltu_p (vr0
.max
, low_bound
))
2879 /* [5, 6] << [1, 2] == [10, 24]. */
2880 /* We're shifting out only zeroes, the value increases
2884 else if (wi::ltu_p (high_bound
, vr0
.min
))
2886 /* [0xffffff00, 0xffffffff] << [1, 2]
2887 == [0xfffffc00, 0xfffffffe]. */
2888 /* We're shifting out only ones, the value decreases
2895 /* [-1, 1] << [1, 2] == [-4, 4]. */
2896 low_bound
= complement
;
2898 if (wi::lts_p (vr0
.max
, high_bound
)
2899 && wi::lts_p (low_bound
, vr0
.min
))
2901 /* For non-negative numbers, we're shifting out only
2902 zeroes, the value increases monotonically.
2903 For negative numbers, we're shifting out only ones, the
2904 value decreases monotomically. */
2911 extract_range_from_multiplicative_op_1 (vr
, code
, &vr0
, &vr1
);
2916 set_value_range_to_varying (vr
);
2919 else if (code
== TRUNC_DIV_EXPR
2920 || code
== FLOOR_DIV_EXPR
2921 || code
== CEIL_DIV_EXPR
2922 || code
== EXACT_DIV_EXPR
2923 || code
== ROUND_DIV_EXPR
)
2925 if (vr0
.type
!= VR_RANGE
|| symbolic_range_p (&vr0
))
2927 /* For division, if op1 has VR_RANGE but op0 does not, something
2928 can be deduced just from that range. Say [min, max] / [4, max]
2929 gives [min / 4, max / 4] range. */
2930 if (vr1
.type
== VR_RANGE
2931 && !symbolic_range_p (&vr1
)
2932 && range_includes_zero_p (vr1
.min
, vr1
.max
) == 0)
2934 vr0
.type
= type
= VR_RANGE
;
2935 vr0
.min
= vrp_val_min (expr_type
);
2936 vr0
.max
= vrp_val_max (expr_type
);
2940 set_value_range_to_varying (vr
);
2945 /* For divisions, if flag_non_call_exceptions is true, we must
2946 not eliminate a division by zero. */
2947 if (cfun
->can_throw_non_call_exceptions
2948 && (vr1
.type
!= VR_RANGE
2949 || range_includes_zero_p (vr1
.min
, vr1
.max
) != 0))
2951 set_value_range_to_varying (vr
);
2955 /* For divisions, if op0 is VR_RANGE, we can deduce a range
2956 even if op1 is VR_VARYING, VR_ANTI_RANGE, symbolic or can
2958 if (vr0
.type
== VR_RANGE
2959 && (vr1
.type
!= VR_RANGE
2960 || range_includes_zero_p (vr1
.min
, vr1
.max
) != 0))
2962 tree zero
= build_int_cst (TREE_TYPE (vr0
.min
), 0);
2967 if (TYPE_UNSIGNED (expr_type
)
2968 || value_range_nonnegative_p (&vr1
))
2970 /* For unsigned division or when divisor is known
2971 to be non-negative, the range has to cover
2972 all numbers from 0 to max for positive max
2973 and all numbers from min to 0 for negative min. */
2974 cmp
= compare_values (vr0
.max
, zero
);
2977 /* When vr0.max < 0, vr1.min != 0 and value
2978 ranges for dividend and divisor are available. */
2979 if (vr1
.type
== VR_RANGE
2980 && !symbolic_range_p (&vr0
)
2981 && !symbolic_range_p (&vr1
)
2982 && !compare_values (vr1
.min
, zero
))
2983 max
= int_const_binop (code
, vr0
.max
, vr1
.min
);
2987 else if (cmp
== 0 || cmp
== 1)
2991 cmp
= compare_values (vr0
.min
, zero
);
2994 /* For unsigned division when value ranges for dividend
2995 and divisor are available. */
2996 if (vr1
.type
== VR_RANGE
2997 && !symbolic_range_p (&vr0
)
2998 && !symbolic_range_p (&vr1
))
2999 min
= int_const_binop (code
, vr0
.min
, vr1
.max
);
3003 else if (cmp
== 0 || cmp
== -1)
3010 /* Otherwise the range is -max .. max or min .. -min
3011 depending on which bound is bigger in absolute value,
3012 as the division can change the sign. */
3013 abs_extent_range (vr
, vr0
.min
, vr0
.max
);
3016 if (type
== VR_VARYING
)
3018 set_value_range_to_varying (vr
);
3024 extract_range_from_multiplicative_op_1 (vr
, code
, &vr0
, &vr1
);
3028 else if (code
== TRUNC_MOD_EXPR
)
3030 if (range_is_null (&vr1
))
3032 set_value_range_to_undefined (vr
);
3035 /* ABS (A % B) < ABS (B) and either
3036 0 <= A % B <= A or A <= A % B <= 0. */
3038 signop sgn
= TYPE_SIGN (expr_type
);
3039 unsigned int prec
= TYPE_PRECISION (expr_type
);
3040 wide_int wmin
, wmax
, tmp
;
3041 wide_int zero
= wi::zero (prec
);
3042 wide_int one
= wi::one (prec
);
3043 if (vr1
.type
== VR_RANGE
&& !symbolic_range_p (&vr1
))
3045 wmax
= wi::sub (vr1
.max
, one
);
3048 tmp
= wi::sub (wi::minus_one (prec
), vr1
.min
);
3049 wmax
= wi::smax (wmax
, tmp
);
3054 wmax
= wi::max_value (prec
, sgn
);
3055 /* X % INT_MIN may be INT_MAX. */
3056 if (sgn
== UNSIGNED
)
3060 if (sgn
== UNSIGNED
)
3065 if (vr0
.type
== VR_RANGE
&& TREE_CODE (vr0
.min
) == INTEGER_CST
)
3068 if (wi::gts_p (tmp
, zero
))
3070 wmin
= wi::smax (wmin
, tmp
);
3074 if (vr0
.type
== VR_RANGE
&& TREE_CODE (vr0
.max
) == INTEGER_CST
)
3077 if (sgn
== SIGNED
&& wi::neg_p (tmp
))
3079 wmax
= wi::min (wmax
, tmp
, sgn
);
3082 min
= wide_int_to_tree (expr_type
, wmin
);
3083 max
= wide_int_to_tree (expr_type
, wmax
);
3085 else if (code
== BIT_AND_EXPR
|| code
== BIT_IOR_EXPR
|| code
== BIT_XOR_EXPR
)
3087 bool int_cst_range0
, int_cst_range1
;
3088 wide_int may_be_nonzero0
, may_be_nonzero1
;
3089 wide_int must_be_nonzero0
, must_be_nonzero1
;
3091 int_cst_range0
= zero_nonzero_bits_from_vr (expr_type
, &vr0
,
3094 int_cst_range1
= zero_nonzero_bits_from_vr (expr_type
, &vr1
,
3099 if (code
== BIT_AND_EXPR
)
3101 min
= wide_int_to_tree (expr_type
,
3102 must_be_nonzero0
& must_be_nonzero1
);
3103 wide_int wmax
= may_be_nonzero0
& may_be_nonzero1
;
3104 /* If both input ranges contain only negative values we can
3105 truncate the result range maximum to the minimum of the
3106 input range maxima. */
3107 if (int_cst_range0
&& int_cst_range1
3108 && tree_int_cst_sgn (vr0
.max
) < 0
3109 && tree_int_cst_sgn (vr1
.max
) < 0)
3111 wmax
= wi::min (wmax
, vr0
.max
, TYPE_SIGN (expr_type
));
3112 wmax
= wi::min (wmax
, vr1
.max
, TYPE_SIGN (expr_type
));
3114 /* If either input range contains only non-negative values
3115 we can truncate the result range maximum to the respective
3116 maximum of the input range. */
3117 if (int_cst_range0
&& tree_int_cst_sgn (vr0
.min
) >= 0)
3118 wmax
= wi::min (wmax
, vr0
.max
, TYPE_SIGN (expr_type
));
3119 if (int_cst_range1
&& tree_int_cst_sgn (vr1
.min
) >= 0)
3120 wmax
= wi::min (wmax
, vr1
.max
, TYPE_SIGN (expr_type
));
3121 max
= wide_int_to_tree (expr_type
, wmax
);
3123 else if (code
== BIT_IOR_EXPR
)
3125 max
= wide_int_to_tree (expr_type
,
3126 may_be_nonzero0
| may_be_nonzero1
);
3127 wide_int wmin
= must_be_nonzero0
| must_be_nonzero1
;
3128 /* If the input ranges contain only positive values we can
3129 truncate the minimum of the result range to the maximum
3130 of the input range minima. */
3131 if (int_cst_range0
&& int_cst_range1
3132 && tree_int_cst_sgn (vr0
.min
) >= 0
3133 && tree_int_cst_sgn (vr1
.min
) >= 0)
3135 wmin
= wi::max (wmin
, vr0
.min
, TYPE_SIGN (expr_type
));
3136 wmin
= wi::max (wmin
, vr1
.min
, TYPE_SIGN (expr_type
));
3138 /* If either input range contains only negative values
3139 we can truncate the minimum of the result range to the
3140 respective minimum range. */
3141 if (int_cst_range0
&& tree_int_cst_sgn (vr0
.max
) < 0)
3142 wmin
= wi::max (wmin
, vr0
.min
, TYPE_SIGN (expr_type
));
3143 if (int_cst_range1
&& tree_int_cst_sgn (vr1
.max
) < 0)
3144 wmin
= wi::max (wmin
, vr1
.min
, TYPE_SIGN (expr_type
));
3145 min
= wide_int_to_tree (expr_type
, wmin
);
3147 else if (code
== BIT_XOR_EXPR
)
3149 wide_int result_zero_bits
= ((must_be_nonzero0
& must_be_nonzero1
)
3150 | ~(may_be_nonzero0
| may_be_nonzero1
));
3151 wide_int result_one_bits
3152 = (must_be_nonzero0
.and_not (may_be_nonzero1
)
3153 | must_be_nonzero1
.and_not (may_be_nonzero0
));
3154 max
= wide_int_to_tree (expr_type
, ~result_zero_bits
);
3155 min
= wide_int_to_tree (expr_type
, result_one_bits
);
3156 /* If the range has all positive or all negative values the
3157 result is better than VARYING. */
3158 if (tree_int_cst_sgn (min
) < 0
3159 || tree_int_cst_sgn (max
) >= 0)
3162 max
= min
= NULL_TREE
;
3168 /* If either MIN or MAX overflowed, then set the resulting range to
3169 VARYING. But we do accept an overflow infinity representation. */
3170 if (min
== NULL_TREE
3171 || (TREE_OVERFLOW_P (min
) && !is_overflow_infinity (min
))
3173 || (TREE_OVERFLOW_P (max
) && !is_overflow_infinity (max
)))
3175 set_value_range_to_varying (vr
);
3181 2) [-INF, +-INF(OVF)]
3182 3) [+-INF(OVF), +INF]
3183 4) [+-INF(OVF), +-INF(OVF)]
3184 We learn nothing when we have INF and INF(OVF) on both sides.
3185 Note that we do accept [-INF, -INF] and [+INF, +INF] without
3187 if ((vrp_val_is_min (min
) || is_overflow_infinity (min
))
3188 && (vrp_val_is_max (max
) || is_overflow_infinity (max
)))
3190 set_value_range_to_varying (vr
);
3194 cmp
= compare_values (min
, max
);
3195 if (cmp
== -2 || cmp
== 1)
3197 /* If the new range has its limits swapped around (MIN > MAX),
3198 then the operation caused one of them to wrap around, mark
3199 the new range VARYING. */
3200 set_value_range_to_varying (vr
);
3203 set_value_range (vr
, type
, min
, max
, NULL
);
3206 /* Extract range information from a binary expression OP0 CODE OP1 based on
3207 the ranges of each of its operands with resulting type EXPR_TYPE.
3208 The resulting range is stored in *VR. */
3211 extract_range_from_binary_expr (value_range
*vr
,
3212 enum tree_code code
,
3213 tree expr_type
, tree op0
, tree op1
)
3215 value_range vr0
= VR_INITIALIZER
;
3216 value_range vr1
= VR_INITIALIZER
;
3218 /* Get value ranges for each operand. For constant operands, create
3219 a new value range with the operand to simplify processing. */
3220 if (TREE_CODE (op0
) == SSA_NAME
)
3221 vr0
= *(get_value_range (op0
));
3222 else if (is_gimple_min_invariant (op0
))
3223 set_value_range_to_value (&vr0
, op0
, NULL
);
3225 set_value_range_to_varying (&vr0
);
3227 if (TREE_CODE (op1
) == SSA_NAME
)
3228 vr1
= *(get_value_range (op1
));
3229 else if (is_gimple_min_invariant (op1
))
3230 set_value_range_to_value (&vr1
, op1
, NULL
);
3232 set_value_range_to_varying (&vr1
);
3234 extract_range_from_binary_expr_1 (vr
, code
, expr_type
, &vr0
, &vr1
);
3236 /* Try harder for PLUS and MINUS if the range of one operand is symbolic
3237 and based on the other operand, for example if it was deduced from a
3238 symbolic comparison. When a bound of the range of the first operand
3239 is invariant, we set the corresponding bound of the new range to INF
3240 in order to avoid recursing on the range of the second operand. */
3241 if (vr
->type
== VR_VARYING
3242 && (code
== PLUS_EXPR
|| code
== MINUS_EXPR
)
3243 && TREE_CODE (op1
) == SSA_NAME
3244 && vr0
.type
== VR_RANGE
3245 && symbolic_range_based_on_p (&vr0
, op1
))
3247 const bool minus_p
= (code
== MINUS_EXPR
);
3248 value_range n_vr1
= VR_INITIALIZER
;
3250 /* Try with VR0 and [-INF, OP1]. */
3251 if (is_gimple_min_invariant (minus_p
? vr0
.max
: vr0
.min
))
3252 set_value_range (&n_vr1
, VR_RANGE
, vrp_val_min (expr_type
), op1
, NULL
);
3254 /* Try with VR0 and [OP1, +INF]. */
3255 else if (is_gimple_min_invariant (minus_p
? vr0
.min
: vr0
.max
))
3256 set_value_range (&n_vr1
, VR_RANGE
, op1
, vrp_val_max (expr_type
), NULL
);
3258 /* Try with VR0 and [OP1, OP1]. */
3260 set_value_range (&n_vr1
, VR_RANGE
, op1
, op1
, NULL
);
3262 extract_range_from_binary_expr_1 (vr
, code
, expr_type
, &vr0
, &n_vr1
);
3265 if (vr
->type
== VR_VARYING
3266 && (code
== PLUS_EXPR
|| code
== MINUS_EXPR
)
3267 && TREE_CODE (op0
) == SSA_NAME
3268 && vr1
.type
== VR_RANGE
3269 && symbolic_range_based_on_p (&vr1
, op0
))
3271 const bool minus_p
= (code
== MINUS_EXPR
);
3272 value_range n_vr0
= VR_INITIALIZER
;
3274 /* Try with [-INF, OP0] and VR1. */
3275 if (is_gimple_min_invariant (minus_p
? vr1
.max
: vr1
.min
))
3276 set_value_range (&n_vr0
, VR_RANGE
, vrp_val_min (expr_type
), op0
, NULL
);
3278 /* Try with [OP0, +INF] and VR1. */
3279 else if (is_gimple_min_invariant (minus_p
? vr1
.min
: vr1
.max
))
3280 set_value_range (&n_vr0
, VR_RANGE
, op0
, vrp_val_max (expr_type
), NULL
);
3282 /* Try with [OP0, OP0] and VR1. */
3284 set_value_range (&n_vr0
, VR_RANGE
, op0
, op0
, NULL
);
3286 extract_range_from_binary_expr_1 (vr
, code
, expr_type
, &n_vr0
, &vr1
);
3290 /* Extract range information from a unary operation CODE based on
3291 the range of its operand *VR0 with type OP0_TYPE with resulting type TYPE.
3292 The resulting range is stored in *VR. */
3295 extract_range_from_unary_expr_1 (value_range
*vr
,
3296 enum tree_code code
, tree type
,
3297 value_range
*vr0_
, tree op0_type
)
3299 value_range vr0
= *vr0_
, vrtem0
= VR_INITIALIZER
, vrtem1
= VR_INITIALIZER
;
3301 /* VRP only operates on integral and pointer types. */
3302 if (!(INTEGRAL_TYPE_P (op0_type
)
3303 || POINTER_TYPE_P (op0_type
))
3304 || !(INTEGRAL_TYPE_P (type
)
3305 || POINTER_TYPE_P (type
)))
3307 set_value_range_to_varying (vr
);
3311 /* If VR0 is UNDEFINED, so is the result. */
3312 if (vr0
.type
== VR_UNDEFINED
)
3314 set_value_range_to_undefined (vr
);
3318 /* Handle operations that we express in terms of others. */
3319 if (code
== PAREN_EXPR
|| code
== OBJ_TYPE_REF
)
3321 /* PAREN_EXPR and OBJ_TYPE_REF are simple copies. */
3322 copy_value_range (vr
, &vr0
);
3325 else if (code
== NEGATE_EXPR
)
3327 /* -X is simply 0 - X, so re-use existing code that also handles
3328 anti-ranges fine. */
3329 value_range zero
= VR_INITIALIZER
;
3330 set_value_range_to_value (&zero
, build_int_cst (type
, 0), NULL
);
3331 extract_range_from_binary_expr_1 (vr
, MINUS_EXPR
, type
, &zero
, &vr0
);
3334 else if (code
== BIT_NOT_EXPR
)
3336 /* ~X is simply -1 - X, so re-use existing code that also handles
3337 anti-ranges fine. */
3338 value_range minusone
= VR_INITIALIZER
;
3339 set_value_range_to_value (&minusone
, build_int_cst (type
, -1), NULL
);
3340 extract_range_from_binary_expr_1 (vr
, MINUS_EXPR
,
3341 type
, &minusone
, &vr0
);
3345 /* Now canonicalize anti-ranges to ranges when they are not symbolic
3346 and express op ~[] as (op []') U (op []''). */
3347 if (vr0
.type
== VR_ANTI_RANGE
3348 && ranges_from_anti_range (&vr0
, &vrtem0
, &vrtem1
))
3350 extract_range_from_unary_expr_1 (vr
, code
, type
, &vrtem0
, op0_type
);
3351 if (vrtem1
.type
!= VR_UNDEFINED
)
3353 value_range vrres
= VR_INITIALIZER
;
3354 extract_range_from_unary_expr_1 (&vrres
, code
, type
,
3356 vrp_meet (vr
, &vrres
);
3361 if (CONVERT_EXPR_CODE_P (code
))
3363 tree inner_type
= op0_type
;
3364 tree outer_type
= type
;
3366 /* If the expression evaluates to a pointer, we are only interested in
3367 determining if it evaluates to NULL [0, 0] or non-NULL (~[0, 0]). */
3368 if (POINTER_TYPE_P (type
))
3370 if (range_is_nonnull (&vr0
))
3371 set_value_range_to_nonnull (vr
, type
);
3372 else if (range_is_null (&vr0
))
3373 set_value_range_to_null (vr
, type
);
3375 set_value_range_to_varying (vr
);
3379 /* If VR0 is varying and we increase the type precision, assume
3380 a full range for the following transformation. */
3381 if (vr0
.type
== VR_VARYING
3382 && INTEGRAL_TYPE_P (inner_type
)
3383 && TYPE_PRECISION (inner_type
) < TYPE_PRECISION (outer_type
))
3385 vr0
.type
= VR_RANGE
;
3386 vr0
.min
= TYPE_MIN_VALUE (inner_type
);
3387 vr0
.max
= TYPE_MAX_VALUE (inner_type
);
3390 /* If VR0 is a constant range or anti-range and the conversion is
3391 not truncating we can convert the min and max values and
3392 canonicalize the resulting range. Otherwise we can do the
3393 conversion if the size of the range is less than what the
3394 precision of the target type can represent and the range is
3395 not an anti-range. */
3396 if ((vr0
.type
== VR_RANGE
3397 || vr0
.type
== VR_ANTI_RANGE
)
3398 && TREE_CODE (vr0
.min
) == INTEGER_CST
3399 && TREE_CODE (vr0
.max
) == INTEGER_CST
3400 && (!is_overflow_infinity (vr0
.min
)
3401 || (vr0
.type
== VR_RANGE
3402 && TYPE_PRECISION (outer_type
) > TYPE_PRECISION (inner_type
)
3403 && needs_overflow_infinity (outer_type
)
3404 && supports_overflow_infinity (outer_type
)))
3405 && (!is_overflow_infinity (vr0
.max
)
3406 || (vr0
.type
== VR_RANGE
3407 && TYPE_PRECISION (outer_type
) > TYPE_PRECISION (inner_type
)
3408 && needs_overflow_infinity (outer_type
)
3409 && supports_overflow_infinity (outer_type
)))
3410 && (TYPE_PRECISION (outer_type
) >= TYPE_PRECISION (inner_type
)
3411 || (vr0
.type
== VR_RANGE
3412 && integer_zerop (int_const_binop (RSHIFT_EXPR
,
3413 int_const_binop (MINUS_EXPR
, vr0
.max
, vr0
.min
),
3414 size_int (TYPE_PRECISION (outer_type
)))))))
3416 tree new_min
, new_max
;
3417 if (is_overflow_infinity (vr0
.min
))
3418 new_min
= negative_overflow_infinity (outer_type
);
3420 new_min
= force_fit_type (outer_type
, wi::to_widest (vr0
.min
),
3422 if (is_overflow_infinity (vr0
.max
))
3423 new_max
= positive_overflow_infinity (outer_type
);
3425 new_max
= force_fit_type (outer_type
, wi::to_widest (vr0
.max
),
3427 set_and_canonicalize_value_range (vr
, vr0
.type
,
3428 new_min
, new_max
, NULL
);
3432 set_value_range_to_varying (vr
);
3435 else if (code
== ABS_EXPR
)
3440 /* Pass through vr0 in the easy cases. */
3441 if (TYPE_UNSIGNED (type
)
3442 || value_range_nonnegative_p (&vr0
))
3444 copy_value_range (vr
, &vr0
);
3448 /* For the remaining varying or symbolic ranges we can't do anything
3450 if (vr0
.type
== VR_VARYING
3451 || symbolic_range_p (&vr0
))
3453 set_value_range_to_varying (vr
);
3457 /* -TYPE_MIN_VALUE = TYPE_MIN_VALUE with flag_wrapv so we can't get a
3459 if (!TYPE_OVERFLOW_UNDEFINED (type
)
3460 && ((vr0
.type
== VR_RANGE
3461 && vrp_val_is_min (vr0
.min
))
3462 || (vr0
.type
== VR_ANTI_RANGE
3463 && !vrp_val_is_min (vr0
.min
))))
3465 set_value_range_to_varying (vr
);
3469 /* ABS_EXPR may flip the range around, if the original range
3470 included negative values. */
3471 if (is_overflow_infinity (vr0
.min
))
3472 min
= positive_overflow_infinity (type
);
3473 else if (!vrp_val_is_min (vr0
.min
))
3474 min
= fold_unary_to_constant (code
, type
, vr0
.min
);
3475 else if (!needs_overflow_infinity (type
))
3476 min
= TYPE_MAX_VALUE (type
);
3477 else if (supports_overflow_infinity (type
))
3478 min
= positive_overflow_infinity (type
);
3481 set_value_range_to_varying (vr
);
3485 if (is_overflow_infinity (vr0
.max
))
3486 max
= positive_overflow_infinity (type
);
3487 else if (!vrp_val_is_min (vr0
.max
))
3488 max
= fold_unary_to_constant (code
, type
, vr0
.max
);
3489 else if (!needs_overflow_infinity (type
))
3490 max
= TYPE_MAX_VALUE (type
);
3491 else if (supports_overflow_infinity (type
)
3492 /* We shouldn't generate [+INF, +INF] as set_value_range
3493 doesn't like this and ICEs. */
3494 && !is_positive_overflow_infinity (min
))
3495 max
= positive_overflow_infinity (type
);
3498 set_value_range_to_varying (vr
);
3502 cmp
= compare_values (min
, max
);
3504 /* If a VR_ANTI_RANGEs contains zero, then we have
3505 ~[-INF, min(MIN, MAX)]. */
3506 if (vr0
.type
== VR_ANTI_RANGE
)
3508 if (range_includes_zero_p (vr0
.min
, vr0
.max
) == 1)
3510 /* Take the lower of the two values. */
3514 /* Create ~[-INF, min (abs(MIN), abs(MAX))]
3515 or ~[-INF + 1, min (abs(MIN), abs(MAX))] when
3516 flag_wrapv is set and the original anti-range doesn't include
3517 TYPE_MIN_VALUE, remember -TYPE_MIN_VALUE = TYPE_MIN_VALUE. */
3518 if (TYPE_OVERFLOW_WRAPS (type
))
3520 tree type_min_value
= TYPE_MIN_VALUE (type
);
3522 min
= (vr0
.min
!= type_min_value
3523 ? int_const_binop (PLUS_EXPR
, type_min_value
,
3524 build_int_cst (TREE_TYPE (type_min_value
), 1))
3529 if (overflow_infinity_range_p (&vr0
))
3530 min
= negative_overflow_infinity (type
);
3532 min
= TYPE_MIN_VALUE (type
);
3537 /* All else has failed, so create the range [0, INF], even for
3538 flag_wrapv since TYPE_MIN_VALUE is in the original
3540 vr0
.type
= VR_RANGE
;
3541 min
= build_int_cst (type
, 0);
3542 if (needs_overflow_infinity (type
))
3544 if (supports_overflow_infinity (type
))
3545 max
= positive_overflow_infinity (type
);
3548 set_value_range_to_varying (vr
);
3553 max
= TYPE_MAX_VALUE (type
);
3557 /* If the range contains zero then we know that the minimum value in the
3558 range will be zero. */
3559 else if (range_includes_zero_p (vr0
.min
, vr0
.max
) == 1)
3563 min
= build_int_cst (type
, 0);
3567 /* If the range was reversed, swap MIN and MAX. */
3569 std::swap (min
, max
);
3572 cmp
= compare_values (min
, max
);
3573 if (cmp
== -2 || cmp
== 1)
3575 /* If the new range has its limits swapped around (MIN > MAX),
3576 then the operation caused one of them to wrap around, mark
3577 the new range VARYING. */
3578 set_value_range_to_varying (vr
);
3581 set_value_range (vr
, vr0
.type
, min
, max
, NULL
);
3585 /* For unhandled operations fall back to varying. */
3586 set_value_range_to_varying (vr
);
3591 /* Extract range information from a unary expression CODE OP0 based on
3592 the range of its operand with resulting type TYPE.
3593 The resulting range is stored in *VR. */
3596 extract_range_from_unary_expr (value_range
*vr
, enum tree_code code
,
3597 tree type
, tree op0
)
3599 value_range vr0
= VR_INITIALIZER
;
3601 /* Get value ranges for the operand. For constant operands, create
3602 a new value range with the operand to simplify processing. */
3603 if (TREE_CODE (op0
) == SSA_NAME
)
3604 vr0
= *(get_value_range (op0
));
3605 else if (is_gimple_min_invariant (op0
))
3606 set_value_range_to_value (&vr0
, op0
, NULL
);
3608 set_value_range_to_varying (&vr0
);
3610 extract_range_from_unary_expr_1 (vr
, code
, type
, &vr0
, TREE_TYPE (op0
));
3614 /* Extract range information from a conditional expression STMT based on
3615 the ranges of each of its operands and the expression code. */
3618 extract_range_from_cond_expr (value_range
*vr
, gassign
*stmt
)
3621 value_range vr0
= VR_INITIALIZER
;
3622 value_range vr1
= VR_INITIALIZER
;
3624 /* Get value ranges for each operand. For constant operands, create
3625 a new value range with the operand to simplify processing. */
3626 op0
= gimple_assign_rhs2 (stmt
);
3627 if (TREE_CODE (op0
) == SSA_NAME
)
3628 vr0
= *(get_value_range (op0
));
3629 else if (is_gimple_min_invariant (op0
))
3630 set_value_range_to_value (&vr0
, op0
, NULL
);
3632 set_value_range_to_varying (&vr0
);
3634 op1
= gimple_assign_rhs3 (stmt
);
3635 if (TREE_CODE (op1
) == SSA_NAME
)
3636 vr1
= *(get_value_range (op1
));
3637 else if (is_gimple_min_invariant (op1
))
3638 set_value_range_to_value (&vr1
, op1
, NULL
);
3640 set_value_range_to_varying (&vr1
);
3642 /* The resulting value range is the union of the operand ranges */
3643 copy_value_range (vr
, &vr0
);
3644 vrp_meet (vr
, &vr1
);
3648 /* Extract range information from a comparison expression EXPR based
3649 on the range of its operand and the expression code. */
3652 extract_range_from_comparison (value_range
*vr
, enum tree_code code
,
3653 tree type
, tree op0
, tree op1
)
3658 val
= vrp_evaluate_conditional_warnv_with_ops (code
, op0
, op1
, false, &sop
,
3661 /* A disadvantage of using a special infinity as an overflow
3662 representation is that we lose the ability to record overflow
3663 when we don't have an infinity. So we have to ignore a result
3664 which relies on overflow. */
3666 if (val
&& !is_overflow_infinity (val
) && !sop
)
3668 /* Since this expression was found on the RHS of an assignment,
3669 its type may be different from _Bool. Convert VAL to EXPR's
3671 val
= fold_convert (type
, val
);
3672 if (is_gimple_min_invariant (val
))
3673 set_value_range_to_value (vr
, val
, vr
->equiv
);
3675 set_value_range (vr
, VR_RANGE
, val
, val
, vr
->equiv
);
3678 /* The result of a comparison is always true or false. */
3679 set_value_range_to_truthvalue (vr
, type
);
3682 /* Helper function for simplify_internal_call_using_ranges and
3683 extract_range_basic. Return true if OP0 SUBCODE OP1 for
3684 SUBCODE {PLUS,MINUS,MULT}_EXPR is known to never overflow or
3685 always overflow. Set *OVF to true if it is known to always
3689 check_for_binary_op_overflow (enum tree_code subcode
, tree type
,
3690 tree op0
, tree op1
, bool *ovf
)
3692 value_range vr0
= VR_INITIALIZER
;
3693 value_range vr1
= VR_INITIALIZER
;
3694 if (TREE_CODE (op0
) == SSA_NAME
)
3695 vr0
= *get_value_range (op0
);
3696 else if (TREE_CODE (op0
) == INTEGER_CST
)
3697 set_value_range_to_value (&vr0
, op0
, NULL
);
3699 set_value_range_to_varying (&vr0
);
3701 if (TREE_CODE (op1
) == SSA_NAME
)
3702 vr1
= *get_value_range (op1
);
3703 else if (TREE_CODE (op1
) == INTEGER_CST
)
3704 set_value_range_to_value (&vr1
, op1
, NULL
);
3706 set_value_range_to_varying (&vr1
);
3708 if (!range_int_cst_p (&vr0
)
3709 || TREE_OVERFLOW (vr0
.min
)
3710 || TREE_OVERFLOW (vr0
.max
))
3712 vr0
.min
= vrp_val_min (TREE_TYPE (op0
));
3713 vr0
.max
= vrp_val_max (TREE_TYPE (op0
));
3715 if (!range_int_cst_p (&vr1
)
3716 || TREE_OVERFLOW (vr1
.min
)
3717 || TREE_OVERFLOW (vr1
.max
))
3719 vr1
.min
= vrp_val_min (TREE_TYPE (op1
));
3720 vr1
.max
= vrp_val_max (TREE_TYPE (op1
));
3722 *ovf
= arith_overflowed_p (subcode
, type
, vr0
.min
,
3723 subcode
== MINUS_EXPR
? vr1
.max
: vr1
.min
);
3724 if (arith_overflowed_p (subcode
, type
, vr0
.max
,
3725 subcode
== MINUS_EXPR
? vr1
.min
: vr1
.max
) != *ovf
)
3727 if (subcode
== MULT_EXPR
)
3729 if (arith_overflowed_p (subcode
, type
, vr0
.min
, vr1
.max
) != *ovf
3730 || arith_overflowed_p (subcode
, type
, vr0
.max
, vr1
.min
) != *ovf
)
3735 /* So far we found that there is an overflow on the boundaries.
3736 That doesn't prove that there is an overflow even for all values
3737 in between the boundaries. For that compute widest_int range
3738 of the result and see if it doesn't overlap the range of
3740 widest_int wmin
, wmax
;
3743 w
[0] = wi::to_widest (vr0
.min
);
3744 w
[1] = wi::to_widest (vr0
.max
);
3745 w
[2] = wi::to_widest (vr1
.min
);
3746 w
[3] = wi::to_widest (vr1
.max
);
3747 for (i
= 0; i
< 4; i
++)
3753 wt
= wi::add (w
[i
& 1], w
[2 + (i
& 2) / 2]);
3756 wt
= wi::sub (w
[i
& 1], w
[2 + (i
& 2) / 2]);
3759 wt
= wi::mul (w
[i
& 1], w
[2 + (i
& 2) / 2]);
3771 wmin
= wi::smin (wmin
, wt
);
3772 wmax
= wi::smax (wmax
, wt
);
3775 /* The result of op0 CODE op1 is known to be in range
3777 widest_int wtmin
= wi::to_widest (vrp_val_min (type
));
3778 widest_int wtmax
= wi::to_widest (vrp_val_max (type
));
3779 /* If all values in [wmin, wmax] are smaller than
3780 [wtmin, wtmax] or all are larger than [wtmin, wtmax],
3781 the arithmetic operation will always overflow. */
3782 if (wi::lts_p (wmax
, wtmin
) || wi::gts_p (wmin
, wtmax
))
3789 /* Try to derive a nonnegative or nonzero range out of STMT relying
3790 primarily on generic routines in fold in conjunction with range data.
3791 Store the result in *VR */
3794 extract_range_basic (value_range
*vr
, gimple
*stmt
)
3797 tree type
= gimple_expr_type (stmt
);
3799 if (gimple_call_builtin_p (stmt
, BUILT_IN_NORMAL
))
3801 tree fndecl
= gimple_call_fndecl (stmt
), arg
;
3802 int mini
, maxi
, zerov
= 0, prec
;
3804 switch (DECL_FUNCTION_CODE (fndecl
))
3806 case BUILT_IN_CONSTANT_P
:
3807 /* If the call is __builtin_constant_p and the argument is a
3808 function parameter resolve it to false. This avoids bogus
3809 array bound warnings.
3810 ??? We could do this as early as inlining is finished. */
3811 arg
= gimple_call_arg (stmt
, 0);
3812 if (TREE_CODE (arg
) == SSA_NAME
3813 && SSA_NAME_IS_DEFAULT_DEF (arg
)
3814 && TREE_CODE (SSA_NAME_VAR (arg
)) == PARM_DECL
)
3816 set_value_range_to_null (vr
, type
);
3820 /* Both __builtin_ffs* and __builtin_popcount return
3822 CASE_INT_FN (BUILT_IN_FFS
):
3823 CASE_INT_FN (BUILT_IN_POPCOUNT
):
3824 arg
= gimple_call_arg (stmt
, 0);
3825 prec
= TYPE_PRECISION (TREE_TYPE (arg
));
3828 if (TREE_CODE (arg
) == SSA_NAME
)
3830 value_range
*vr0
= get_value_range (arg
);
3831 /* If arg is non-zero, then ffs or popcount
3833 if (((vr0
->type
== VR_RANGE
3834 && range_includes_zero_p (vr0
->min
, vr0
->max
) == 0)
3835 || (vr0
->type
== VR_ANTI_RANGE
3836 && range_includes_zero_p (vr0
->min
, vr0
->max
) == 1))
3837 && !is_overflow_infinity (vr0
->min
)
3838 && !is_overflow_infinity (vr0
->max
))
3840 /* If some high bits are known to be zero,
3841 we can decrease the maximum. */
3842 if (vr0
->type
== VR_RANGE
3843 && TREE_CODE (vr0
->max
) == INTEGER_CST
3844 && !operand_less_p (vr0
->min
,
3845 build_zero_cst (TREE_TYPE (vr0
->min
)))
3846 && !is_overflow_infinity (vr0
->max
))
3847 maxi
= tree_floor_log2 (vr0
->max
) + 1;
3850 /* __builtin_parity* returns [0, 1]. */
3851 CASE_INT_FN (BUILT_IN_PARITY
):
3855 /* __builtin_c[lt]z* return [0, prec-1], except for
3856 when the argument is 0, but that is undefined behavior.
3857 On many targets where the CLZ RTL or optab value is defined
3858 for 0 the value is prec, so include that in the range
3860 CASE_INT_FN (BUILT_IN_CLZ
):
3861 arg
= gimple_call_arg (stmt
, 0);
3862 prec
= TYPE_PRECISION (TREE_TYPE (arg
));
3865 if (optab_handler (clz_optab
, TYPE_MODE (TREE_TYPE (arg
)))
3867 && CLZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (TREE_TYPE (arg
)),
3869 /* Handle only the single common value. */
3871 /* Magic value to give up, unless vr0 proves
3874 if (TREE_CODE (arg
) == SSA_NAME
)
3876 value_range
*vr0
= get_value_range (arg
);
3877 /* From clz of VR_RANGE minimum we can compute
3879 if (vr0
->type
== VR_RANGE
3880 && TREE_CODE (vr0
->min
) == INTEGER_CST
3881 && !is_overflow_infinity (vr0
->min
))
3883 maxi
= prec
- 1 - tree_floor_log2 (vr0
->min
);
3887 else if (vr0
->type
== VR_ANTI_RANGE
3888 && integer_zerop (vr0
->min
)
3889 && !is_overflow_infinity (vr0
->min
))
3896 /* From clz of VR_RANGE maximum we can compute
3898 if (vr0
->type
== VR_RANGE
3899 && TREE_CODE (vr0
->max
) == INTEGER_CST
3900 && !is_overflow_infinity (vr0
->max
))
3902 mini
= prec
- 1 - tree_floor_log2 (vr0
->max
);
3910 /* __builtin_ctz* return [0, prec-1], except for
3911 when the argument is 0, but that is undefined behavior.
3912 If there is a ctz optab for this mode and
3913 CTZ_DEFINED_VALUE_AT_ZERO, include that in the range,
3914 otherwise just assume 0 won't be seen. */
3915 CASE_INT_FN (BUILT_IN_CTZ
):
3916 arg
= gimple_call_arg (stmt
, 0);
3917 prec
= TYPE_PRECISION (TREE_TYPE (arg
));
3920 if (optab_handler (ctz_optab
, TYPE_MODE (TREE_TYPE (arg
)))
3922 && CTZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (TREE_TYPE (arg
)),
3925 /* Handle only the two common values. */
3928 else if (zerov
== prec
)
3931 /* Magic value to give up, unless vr0 proves
3935 if (TREE_CODE (arg
) == SSA_NAME
)
3937 value_range
*vr0
= get_value_range (arg
);
3938 /* If arg is non-zero, then use [0, prec - 1]. */
3939 if (((vr0
->type
== VR_RANGE
3940 && integer_nonzerop (vr0
->min
))
3941 || (vr0
->type
== VR_ANTI_RANGE
3942 && integer_zerop (vr0
->min
)))
3943 && !is_overflow_infinity (vr0
->min
))
3948 /* If some high bits are known to be zero,
3949 we can decrease the result maximum. */
3950 if (vr0
->type
== VR_RANGE
3951 && TREE_CODE (vr0
->max
) == INTEGER_CST
3952 && !is_overflow_infinity (vr0
->max
))
3954 maxi
= tree_floor_log2 (vr0
->max
);
3955 /* For vr0 [0, 0] give up. */
3963 /* __builtin_clrsb* returns [0, prec-1]. */
3964 CASE_INT_FN (BUILT_IN_CLRSB
):
3965 arg
= gimple_call_arg (stmt
, 0);
3966 prec
= TYPE_PRECISION (TREE_TYPE (arg
));
3971 set_value_range (vr
, VR_RANGE
, build_int_cst (type
, mini
),
3972 build_int_cst (type
, maxi
), NULL
);
3978 else if (is_gimple_call (stmt
) && gimple_call_internal_p (stmt
))
3980 enum tree_code subcode
= ERROR_MARK
;
3981 switch (gimple_call_internal_fn (stmt
))
3983 case IFN_UBSAN_CHECK_ADD
:
3984 subcode
= PLUS_EXPR
;
3986 case IFN_UBSAN_CHECK_SUB
:
3987 subcode
= MINUS_EXPR
;
3989 case IFN_UBSAN_CHECK_MUL
:
3990 subcode
= MULT_EXPR
;
3995 if (subcode
!= ERROR_MARK
)
3997 bool saved_flag_wrapv
= flag_wrapv
;
3998 /* Pretend the arithmetics is wrapping. If there is
3999 any overflow, we'll complain, but will actually do
4000 wrapping operation. */
4002 extract_range_from_binary_expr (vr
, subcode
, type
,
4003 gimple_call_arg (stmt
, 0),
4004 gimple_call_arg (stmt
, 1));
4005 flag_wrapv
= saved_flag_wrapv
;
4007 /* If for both arguments vrp_valueize returned non-NULL,
4008 this should have been already folded and if not, it
4009 wasn't folded because of overflow. Avoid removing the
4010 UBSAN_CHECK_* calls in that case. */
4011 if (vr
->type
== VR_RANGE
4012 && (vr
->min
== vr
->max
4013 || operand_equal_p (vr
->min
, vr
->max
, 0)))
4014 set_value_range_to_varying (vr
);
4018 /* Handle extraction of the two results (result of arithmetics and
4019 a flag whether arithmetics overflowed) from {ADD,SUB,MUL}_OVERFLOW
4020 internal function. */
4021 else if (is_gimple_assign (stmt
)
4022 && (gimple_assign_rhs_code (stmt
) == REALPART_EXPR
4023 || gimple_assign_rhs_code (stmt
) == IMAGPART_EXPR
)
4024 && INTEGRAL_TYPE_P (type
))
4026 enum tree_code code
= gimple_assign_rhs_code (stmt
);
4027 tree op
= gimple_assign_rhs1 (stmt
);
4028 if (TREE_CODE (op
) == code
&& TREE_CODE (TREE_OPERAND (op
, 0)) == SSA_NAME
)
4030 gimple
*g
= SSA_NAME_DEF_STMT (TREE_OPERAND (op
, 0));
4031 if (is_gimple_call (g
) && gimple_call_internal_p (g
))
4033 enum tree_code subcode
= ERROR_MARK
;
4034 switch (gimple_call_internal_fn (g
))
4036 case IFN_ADD_OVERFLOW
:
4037 subcode
= PLUS_EXPR
;
4039 case IFN_SUB_OVERFLOW
:
4040 subcode
= MINUS_EXPR
;
4042 case IFN_MUL_OVERFLOW
:
4043 subcode
= MULT_EXPR
;
4048 if (subcode
!= ERROR_MARK
)
4050 tree op0
= gimple_call_arg (g
, 0);
4051 tree op1
= gimple_call_arg (g
, 1);
4052 if (code
== IMAGPART_EXPR
)
4055 if (check_for_binary_op_overflow (subcode
, type
,
4057 set_value_range_to_value (vr
,
4058 build_int_cst (type
, ovf
),
4061 set_value_range (vr
, VR_RANGE
, build_int_cst (type
, 0),
4062 build_int_cst (type
, 1), NULL
);
4064 else if (types_compatible_p (type
, TREE_TYPE (op0
))
4065 && types_compatible_p (type
, TREE_TYPE (op1
)))
4067 bool saved_flag_wrapv
= flag_wrapv
;
4068 /* Pretend the arithmetics is wrapping. If there is
4069 any overflow, IMAGPART_EXPR will be set. */
4071 extract_range_from_binary_expr (vr
, subcode
, type
,
4073 flag_wrapv
= saved_flag_wrapv
;
4077 value_range vr0
= VR_INITIALIZER
;
4078 value_range vr1
= VR_INITIALIZER
;
4079 bool saved_flag_wrapv
= flag_wrapv
;
4080 /* Pretend the arithmetics is wrapping. If there is
4081 any overflow, IMAGPART_EXPR will be set. */
4083 extract_range_from_unary_expr (&vr0
, NOP_EXPR
,
4085 extract_range_from_unary_expr (&vr1
, NOP_EXPR
,
4087 extract_range_from_binary_expr_1 (vr
, subcode
, type
,
4089 flag_wrapv
= saved_flag_wrapv
;
4096 if (INTEGRAL_TYPE_P (type
)
4097 && gimple_stmt_nonnegative_warnv_p (stmt
, &sop
))
4098 set_value_range_to_nonnegative (vr
, type
,
4099 sop
|| stmt_overflow_infinity (stmt
));
4100 else if (vrp_stmt_computes_nonzero (stmt
, &sop
)
4102 set_value_range_to_nonnull (vr
, type
);
4104 set_value_range_to_varying (vr
);
4108 /* Try to compute a useful range out of assignment STMT and store it
4112 extract_range_from_assignment (value_range
*vr
, gassign
*stmt
)
4114 enum tree_code code
= gimple_assign_rhs_code (stmt
);
4116 if (code
== ASSERT_EXPR
)
4117 extract_range_from_assert (vr
, gimple_assign_rhs1 (stmt
));
4118 else if (code
== SSA_NAME
)
4119 extract_range_from_ssa_name (vr
, gimple_assign_rhs1 (stmt
));
4120 else if (TREE_CODE_CLASS (code
) == tcc_binary
)
4121 extract_range_from_binary_expr (vr
, gimple_assign_rhs_code (stmt
),
4122 gimple_expr_type (stmt
),
4123 gimple_assign_rhs1 (stmt
),
4124 gimple_assign_rhs2 (stmt
));
4125 else if (TREE_CODE_CLASS (code
) == tcc_unary
)
4126 extract_range_from_unary_expr (vr
, gimple_assign_rhs_code (stmt
),
4127 gimple_expr_type (stmt
),
4128 gimple_assign_rhs1 (stmt
));
4129 else if (code
== COND_EXPR
)
4130 extract_range_from_cond_expr (vr
, stmt
);
4131 else if (TREE_CODE_CLASS (code
) == tcc_comparison
)
4132 extract_range_from_comparison (vr
, gimple_assign_rhs_code (stmt
),
4133 gimple_expr_type (stmt
),
4134 gimple_assign_rhs1 (stmt
),
4135 gimple_assign_rhs2 (stmt
));
4136 else if (get_gimple_rhs_class (code
) == GIMPLE_SINGLE_RHS
4137 && is_gimple_min_invariant (gimple_assign_rhs1 (stmt
)))
4138 set_value_range_to_value (vr
, gimple_assign_rhs1 (stmt
), NULL
);
4140 set_value_range_to_varying (vr
);
4142 if (vr
->type
== VR_VARYING
)
4143 extract_range_basic (vr
, stmt
);
4146 /* Given a range VR, a LOOP and a variable VAR, determine whether it
4147 would be profitable to adjust VR using scalar evolution information
4148 for VAR. If so, update VR with the new limits. */
4151 adjust_range_with_scev (value_range
*vr
, struct loop
*loop
,
4152 gimple
*stmt
, tree var
)
4154 tree init
, step
, chrec
, tmin
, tmax
, min
, max
, type
, tem
;
4155 enum ev_direction dir
;
4157 /* TODO. Don't adjust anti-ranges. An anti-range may provide
4158 better opportunities than a regular range, but I'm not sure. */
4159 if (vr
->type
== VR_ANTI_RANGE
)
4162 chrec
= instantiate_parameters (loop
, analyze_scalar_evolution (loop
, var
));
4164 /* Like in PR19590, scev can return a constant function. */
4165 if (is_gimple_min_invariant (chrec
))
4167 set_value_range_to_value (vr
, chrec
, vr
->equiv
);
4171 if (TREE_CODE (chrec
) != POLYNOMIAL_CHREC
)
4174 init
= initial_condition_in_loop_num (chrec
, loop
->num
);
4175 tem
= op_with_constant_singleton_value_range (init
);
4178 step
= evolution_part_in_loop_num (chrec
, loop
->num
);
4179 tem
= op_with_constant_singleton_value_range (step
);
4183 /* If STEP is symbolic, we can't know whether INIT will be the
4184 minimum or maximum value in the range. Also, unless INIT is
4185 a simple expression, compare_values and possibly other functions
4186 in tree-vrp won't be able to handle it. */
4187 if (step
== NULL_TREE
4188 || !is_gimple_min_invariant (step
)
4189 || !valid_value_p (init
))
4192 dir
= scev_direction (chrec
);
4193 if (/* Do not adjust ranges if we do not know whether the iv increases
4194 or decreases, ... */
4195 dir
== EV_DIR_UNKNOWN
4196 /* ... or if it may wrap. */
4197 || scev_probably_wraps_p (init
, step
, stmt
, get_chrec_loop (chrec
),
4201 /* We use TYPE_MIN_VALUE and TYPE_MAX_VALUE here instead of
4202 negative_overflow_infinity and positive_overflow_infinity,
4203 because we have concluded that the loop probably does not
4206 type
= TREE_TYPE (var
);
4207 if (POINTER_TYPE_P (type
) || !TYPE_MIN_VALUE (type
))
4208 tmin
= lower_bound_in_type (type
, type
);
4210 tmin
= TYPE_MIN_VALUE (type
);
4211 if (POINTER_TYPE_P (type
) || !TYPE_MAX_VALUE (type
))
4212 tmax
= upper_bound_in_type (type
, type
);
4214 tmax
= TYPE_MAX_VALUE (type
);
4216 /* Try to use estimated number of iterations for the loop to constrain the
4217 final value in the evolution. */
4218 if (TREE_CODE (step
) == INTEGER_CST
4219 && is_gimple_val (init
)
4220 && (TREE_CODE (init
) != SSA_NAME
4221 || get_value_range (init
)->type
== VR_RANGE
))
4225 /* We are only entering here for loop header PHI nodes, so using
4226 the number of latch executions is the correct thing to use. */
4227 if (max_loop_iterations (loop
, &nit
))
4229 value_range maxvr
= VR_INITIALIZER
;
4230 signop sgn
= TYPE_SIGN (TREE_TYPE (step
));
4233 widest_int wtmp
= wi::mul (wi::to_widest (step
), nit
, sgn
,
4235 /* If the multiplication overflowed we can't do a meaningful
4236 adjustment. Likewise if the result doesn't fit in the type
4237 of the induction variable. For a signed type we have to
4238 check whether the result has the expected signedness which
4239 is that of the step as number of iterations is unsigned. */
4241 && wi::fits_to_tree_p (wtmp
, TREE_TYPE (init
))
4243 || wi::gts_p (wtmp
, 0) == wi::gts_p (step
, 0)))
4245 tem
= wide_int_to_tree (TREE_TYPE (init
), wtmp
);
4246 extract_range_from_binary_expr (&maxvr
, PLUS_EXPR
,
4247 TREE_TYPE (init
), init
, tem
);
4248 /* Likewise if the addition did. */
4249 if (maxvr
.type
== VR_RANGE
)
4258 if (vr
->type
== VR_VARYING
|| vr
->type
== VR_UNDEFINED
)
4263 /* For VARYING or UNDEFINED ranges, just about anything we get
4264 from scalar evolutions should be better. */
4266 if (dir
== EV_DIR_DECREASES
)
4271 else if (vr
->type
== VR_RANGE
)
4276 if (dir
== EV_DIR_DECREASES
)
4278 /* INIT is the maximum value. If INIT is lower than VR->MAX
4279 but no smaller than VR->MIN, set VR->MAX to INIT. */
4280 if (compare_values (init
, max
) == -1)
4283 /* According to the loop information, the variable does not
4284 overflow. If we think it does, probably because of an
4285 overflow due to arithmetic on a different INF value,
4287 if (is_negative_overflow_infinity (min
)
4288 || compare_values (min
, tmin
) == -1)
4294 /* If INIT is bigger than VR->MIN, set VR->MIN to INIT. */
4295 if (compare_values (init
, min
) == 1)
4298 if (is_positive_overflow_infinity (max
)
4299 || compare_values (tmax
, max
) == -1)
4306 /* If we just created an invalid range with the minimum
4307 greater than the maximum, we fail conservatively.
4308 This should happen only in unreachable
4309 parts of code, or for invalid programs. */
4310 if (compare_values (min
, max
) == 1
4311 || (is_negative_overflow_infinity (min
)
4312 && is_positive_overflow_infinity (max
)))
4315 set_value_range (vr
, VR_RANGE
, min
, max
, vr
->equiv
);
4319 /* Given two numeric value ranges VR0, VR1 and a comparison code COMP:
4321 - Return BOOLEAN_TRUE_NODE if VR0 COMP VR1 always returns true for
4322 all the values in the ranges.
4324 - Return BOOLEAN_FALSE_NODE if the comparison always returns false.
4326 - Return NULL_TREE if it is not always possible to determine the
4327 value of the comparison.
4329 Also set *STRICT_OVERFLOW_P to indicate whether a range with an
4330 overflow infinity was used in the test. */
4334 compare_ranges (enum tree_code comp
, value_range
*vr0
, value_range
*vr1
,
4335 bool *strict_overflow_p
)
4337 /* VARYING or UNDEFINED ranges cannot be compared. */
4338 if (vr0
->type
== VR_VARYING
4339 || vr0
->type
== VR_UNDEFINED
4340 || vr1
->type
== VR_VARYING
4341 || vr1
->type
== VR_UNDEFINED
)
4344 /* Anti-ranges need to be handled separately. */
4345 if (vr0
->type
== VR_ANTI_RANGE
|| vr1
->type
== VR_ANTI_RANGE
)
4347 /* If both are anti-ranges, then we cannot compute any
4349 if (vr0
->type
== VR_ANTI_RANGE
&& vr1
->type
== VR_ANTI_RANGE
)
4352 /* These comparisons are never statically computable. */
4359 /* Equality can be computed only between a range and an
4360 anti-range. ~[VAL1, VAL2] == [VAL1, VAL2] is always false. */
4361 if (vr0
->type
== VR_RANGE
)
4363 /* To simplify processing, make VR0 the anti-range. */
4364 value_range
*tmp
= vr0
;
4369 gcc_assert (comp
== NE_EXPR
|| comp
== EQ_EXPR
);
4371 if (compare_values_warnv (vr0
->min
, vr1
->min
, strict_overflow_p
) == 0
4372 && compare_values_warnv (vr0
->max
, vr1
->max
, strict_overflow_p
) == 0)
4373 return (comp
== NE_EXPR
) ? boolean_true_node
: boolean_false_node
;
4378 if (!usable_range_p (vr0
, strict_overflow_p
)
4379 || !usable_range_p (vr1
, strict_overflow_p
))
4382 /* Simplify processing. If COMP is GT_EXPR or GE_EXPR, switch the
4383 operands around and change the comparison code. */
4384 if (comp
== GT_EXPR
|| comp
== GE_EXPR
)
4386 comp
= (comp
== GT_EXPR
) ? LT_EXPR
: LE_EXPR
;
4387 std::swap (vr0
, vr1
);
4390 if (comp
== EQ_EXPR
)
4392 /* Equality may only be computed if both ranges represent
4393 exactly one value. */
4394 if (compare_values_warnv (vr0
->min
, vr0
->max
, strict_overflow_p
) == 0
4395 && compare_values_warnv (vr1
->min
, vr1
->max
, strict_overflow_p
) == 0)
4397 int cmp_min
= compare_values_warnv (vr0
->min
, vr1
->min
,
4399 int cmp_max
= compare_values_warnv (vr0
->max
, vr1
->max
,
4401 if (cmp_min
== 0 && cmp_max
== 0)
4402 return boolean_true_node
;
4403 else if (cmp_min
!= -2 && cmp_max
!= -2)
4404 return boolean_false_node
;
4406 /* If [V0_MIN, V1_MAX] < [V1_MIN, V1_MAX] then V0 != V1. */
4407 else if (compare_values_warnv (vr0
->min
, vr1
->max
,
4408 strict_overflow_p
) == 1
4409 || compare_values_warnv (vr1
->min
, vr0
->max
,
4410 strict_overflow_p
) == 1)
4411 return boolean_false_node
;
4415 else if (comp
== NE_EXPR
)
4419 /* If VR0 is completely to the left or completely to the right
4420 of VR1, they are always different. Notice that we need to
4421 make sure that both comparisons yield similar results to
4422 avoid comparing values that cannot be compared at
4424 cmp1
= compare_values_warnv (vr0
->max
, vr1
->min
, strict_overflow_p
);
4425 cmp2
= compare_values_warnv (vr0
->min
, vr1
->max
, strict_overflow_p
);
4426 if ((cmp1
== -1 && cmp2
== -1) || (cmp1
== 1 && cmp2
== 1))
4427 return boolean_true_node
;
4429 /* If VR0 and VR1 represent a single value and are identical,
4431 else if (compare_values_warnv (vr0
->min
, vr0
->max
,
4432 strict_overflow_p
) == 0
4433 && compare_values_warnv (vr1
->min
, vr1
->max
,
4434 strict_overflow_p
) == 0
4435 && compare_values_warnv (vr0
->min
, vr1
->min
,
4436 strict_overflow_p
) == 0
4437 && compare_values_warnv (vr0
->max
, vr1
->max
,
4438 strict_overflow_p
) == 0)
4439 return boolean_false_node
;
4441 /* Otherwise, they may or may not be different. */
4445 else if (comp
== LT_EXPR
|| comp
== LE_EXPR
)
4449 /* If VR0 is to the left of VR1, return true. */
4450 tst
= compare_values_warnv (vr0
->max
, vr1
->min
, strict_overflow_p
);
4451 if ((comp
== LT_EXPR
&& tst
== -1)
4452 || (comp
== LE_EXPR
&& (tst
== -1 || tst
== 0)))
4454 if (overflow_infinity_range_p (vr0
)
4455 || overflow_infinity_range_p (vr1
))
4456 *strict_overflow_p
= true;
4457 return boolean_true_node
;
4460 /* If VR0 is to the right of VR1, return false. */
4461 tst
= compare_values_warnv (vr0
->min
, vr1
->max
, strict_overflow_p
);
4462 if ((comp
== LT_EXPR
&& (tst
== 0 || tst
== 1))
4463 || (comp
== LE_EXPR
&& tst
== 1))
4465 if (overflow_infinity_range_p (vr0
)
4466 || overflow_infinity_range_p (vr1
))
4467 *strict_overflow_p
= true;
4468 return boolean_false_node
;
4471 /* Otherwise, we don't know. */
4479 /* Given a value range VR, a value VAL and a comparison code COMP, return
4480 BOOLEAN_TRUE_NODE if VR COMP VAL always returns true for all the
4481 values in VR. Return BOOLEAN_FALSE_NODE if the comparison
4482 always returns false. Return NULL_TREE if it is not always
4483 possible to determine the value of the comparison. Also set
4484 *STRICT_OVERFLOW_P to indicate whether a range with an overflow
4485 infinity was used in the test. */
4488 compare_range_with_value (enum tree_code comp
, value_range
*vr
, tree val
,
4489 bool *strict_overflow_p
)
4491 if (vr
->type
== VR_VARYING
|| vr
->type
== VR_UNDEFINED
)
4494 /* Anti-ranges need to be handled separately. */
4495 if (vr
->type
== VR_ANTI_RANGE
)
4497 /* For anti-ranges, the only predicates that we can compute at
4498 compile time are equality and inequality. */
4505 /* ~[VAL_1, VAL_2] OP VAL is known if VAL_1 <= VAL <= VAL_2. */
4506 if (value_inside_range (val
, vr
->min
, vr
->max
) == 1)
4507 return (comp
== NE_EXPR
) ? boolean_true_node
: boolean_false_node
;
4512 if (!usable_range_p (vr
, strict_overflow_p
))
4515 if (comp
== EQ_EXPR
)
4517 /* EQ_EXPR may only be computed if VR represents exactly
4519 if (compare_values_warnv (vr
->min
, vr
->max
, strict_overflow_p
) == 0)
4521 int cmp
= compare_values_warnv (vr
->min
, val
, strict_overflow_p
);
4523 return boolean_true_node
;
4524 else if (cmp
== -1 || cmp
== 1 || cmp
== 2)
4525 return boolean_false_node
;
4527 else if (compare_values_warnv (val
, vr
->min
, strict_overflow_p
) == -1
4528 || compare_values_warnv (vr
->max
, val
, strict_overflow_p
) == -1)
4529 return boolean_false_node
;
4533 else if (comp
== NE_EXPR
)
4535 /* If VAL is not inside VR, then they are always different. */
4536 if (compare_values_warnv (vr
->max
, val
, strict_overflow_p
) == -1
4537 || compare_values_warnv (vr
->min
, val
, strict_overflow_p
) == 1)
4538 return boolean_true_node
;
4540 /* If VR represents exactly one value equal to VAL, then return
4542 if (compare_values_warnv (vr
->min
, vr
->max
, strict_overflow_p
) == 0
4543 && compare_values_warnv (vr
->min
, val
, strict_overflow_p
) == 0)
4544 return boolean_false_node
;
4546 /* Otherwise, they may or may not be different. */
4549 else if (comp
== LT_EXPR
|| comp
== LE_EXPR
)
4553 /* If VR is to the left of VAL, return true. */
4554 tst
= compare_values_warnv (vr
->max
, val
, strict_overflow_p
);
4555 if ((comp
== LT_EXPR
&& tst
== -1)
4556 || (comp
== LE_EXPR
&& (tst
== -1 || tst
== 0)))
4558 if (overflow_infinity_range_p (vr
))
4559 *strict_overflow_p
= true;
4560 return boolean_true_node
;
4563 /* If VR is to the right of VAL, return false. */
4564 tst
= compare_values_warnv (vr
->min
, val
, strict_overflow_p
);
4565 if ((comp
== LT_EXPR
&& (tst
== 0 || tst
== 1))
4566 || (comp
== LE_EXPR
&& tst
== 1))
4568 if (overflow_infinity_range_p (vr
))
4569 *strict_overflow_p
= true;
4570 return boolean_false_node
;
4573 /* Otherwise, we don't know. */
4576 else if (comp
== GT_EXPR
|| comp
== GE_EXPR
)
4580 /* If VR is to the right of VAL, return true. */
4581 tst
= compare_values_warnv (vr
->min
, val
, strict_overflow_p
);
4582 if ((comp
== GT_EXPR
&& tst
== 1)
4583 || (comp
== GE_EXPR
&& (tst
== 0 || tst
== 1)))
4585 if (overflow_infinity_range_p (vr
))
4586 *strict_overflow_p
= true;
4587 return boolean_true_node
;
4590 /* If VR is to the left of VAL, return false. */
4591 tst
= compare_values_warnv (vr
->max
, val
, strict_overflow_p
);
4592 if ((comp
== GT_EXPR
&& (tst
== -1 || tst
== 0))
4593 || (comp
== GE_EXPR
&& tst
== -1))
4595 if (overflow_infinity_range_p (vr
))
4596 *strict_overflow_p
= true;
4597 return boolean_false_node
;
4600 /* Otherwise, we don't know. */
4608 /* Debugging dumps. */
4610 void dump_value_range (FILE *, value_range
*);
4611 void debug_value_range (value_range
*);
4612 void dump_all_value_ranges (FILE *);
4613 void debug_all_value_ranges (void);
4614 void dump_vr_equiv (FILE *, bitmap
);
4615 void debug_vr_equiv (bitmap
);
4618 /* Dump value range VR to FILE. */
4621 dump_value_range (FILE *file
, value_range
*vr
)
4624 fprintf (file
, "[]");
4625 else if (vr
->type
== VR_UNDEFINED
)
4626 fprintf (file
, "UNDEFINED");
4627 else if (vr
->type
== VR_RANGE
|| vr
->type
== VR_ANTI_RANGE
)
4629 tree type
= TREE_TYPE (vr
->min
);
4631 fprintf (file
, "%s[", (vr
->type
== VR_ANTI_RANGE
) ? "~" : "");
4633 if (is_negative_overflow_infinity (vr
->min
))
4634 fprintf (file
, "-INF(OVF)");
4635 else if (INTEGRAL_TYPE_P (type
)
4636 && !TYPE_UNSIGNED (type
)
4637 && vrp_val_is_min (vr
->min
))
4638 fprintf (file
, "-INF");
4640 print_generic_expr (file
, vr
->min
, 0);
4642 fprintf (file
, ", ");
4644 if (is_positive_overflow_infinity (vr
->max
))
4645 fprintf (file
, "+INF(OVF)");
4646 else if (INTEGRAL_TYPE_P (type
)
4647 && vrp_val_is_max (vr
->max
))
4648 fprintf (file
, "+INF");
4650 print_generic_expr (file
, vr
->max
, 0);
4652 fprintf (file
, "]");
4659 fprintf (file
, " EQUIVALENCES: { ");
4661 EXECUTE_IF_SET_IN_BITMAP (vr
->equiv
, 0, i
, bi
)
4663 print_generic_expr (file
, ssa_name (i
), 0);
4664 fprintf (file
, " ");
4668 fprintf (file
, "} (%u elements)", c
);
4671 else if (vr
->type
== VR_VARYING
)
4672 fprintf (file
, "VARYING");
4674 fprintf (file
, "INVALID RANGE");
4678 /* Dump value range VR to stderr. */
4681 debug_value_range (value_range
*vr
)
4683 dump_value_range (stderr
, vr
);
4684 fprintf (stderr
, "\n");
4688 /* Dump value ranges of all SSA_NAMEs to FILE. */
4691 dump_all_value_ranges (FILE *file
)
4695 for (i
= 0; i
< num_vr_values
; i
++)
4699 print_generic_expr (file
, ssa_name (i
), 0);
4700 fprintf (file
, ": ");
4701 dump_value_range (file
, vr_value
[i
]);
4702 fprintf (file
, "\n");
4706 fprintf (file
, "\n");
4710 /* Dump all value ranges to stderr. */
4713 debug_all_value_ranges (void)
4715 dump_all_value_ranges (stderr
);
4719 /* Given a COND_EXPR COND of the form 'V OP W', and an SSA name V,
4720 create a new SSA name N and return the assertion assignment
4721 'N = ASSERT_EXPR <V, V OP W>'. */
4724 build_assert_expr_for (tree cond
, tree v
)
4729 gcc_assert (TREE_CODE (v
) == SSA_NAME
4730 && COMPARISON_CLASS_P (cond
));
4732 a
= build2 (ASSERT_EXPR
, TREE_TYPE (v
), v
, cond
);
4733 assertion
= gimple_build_assign (NULL_TREE
, a
);
4735 /* The new ASSERT_EXPR, creates a new SSA name that replaces the
4736 operand of the ASSERT_EXPR. Create it so the new name and the old one
4737 are registered in the replacement table so that we can fix the SSA web
4738 after adding all the ASSERT_EXPRs. */
4739 create_new_def_for (v
, assertion
, NULL
);
4745 /* Return false if EXPR is a predicate expression involving floating
4749 fp_predicate (gimple
*stmt
)
4751 GIMPLE_CHECK (stmt
, GIMPLE_COND
);
4753 return FLOAT_TYPE_P (TREE_TYPE (gimple_cond_lhs (stmt
)));
4756 /* If the range of values taken by OP can be inferred after STMT executes,
4757 return the comparison code (COMP_CODE_P) and value (VAL_P) that
4758 describes the inferred range. Return true if a range could be
4762 infer_value_range (gimple
*stmt
, tree op
, tree_code
*comp_code_p
, tree
*val_p
)
4765 *comp_code_p
= ERROR_MARK
;
4767 /* Do not attempt to infer anything in names that flow through
4769 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op
))
4772 /* Similarly, don't infer anything from statements that may throw
4773 exceptions. ??? Relax this requirement? */
4774 if (stmt_could_throw_p (stmt
))
4777 /* If STMT is the last statement of a basic block with no normal
4778 successors, there is no point inferring anything about any of its
4779 operands. We would not be able to find a proper insertion point
4780 for the assertion, anyway. */
4781 if (stmt_ends_bb_p (stmt
))
4786 FOR_EACH_EDGE (e
, ei
, gimple_bb (stmt
)->succs
)
4787 if (!(e
->flags
& EDGE_ABNORMAL
))
4793 if (infer_nonnull_range (stmt
, op
))
4795 *val_p
= build_int_cst (TREE_TYPE (op
), 0);
4796 *comp_code_p
= NE_EXPR
;
4804 void dump_asserts_for (FILE *, tree
);
4805 void debug_asserts_for (tree
);
4806 void dump_all_asserts (FILE *);
4807 void debug_all_asserts (void);
4809 /* Dump all the registered assertions for NAME to FILE. */
4812 dump_asserts_for (FILE *file
, tree name
)
4816 fprintf (file
, "Assertions to be inserted for ");
4817 print_generic_expr (file
, name
, 0);
4818 fprintf (file
, "\n");
4820 loc
= asserts_for
[SSA_NAME_VERSION (name
)];
4823 fprintf (file
, "\t");
4824 print_gimple_stmt (file
, gsi_stmt (loc
->si
), 0, 0);
4825 fprintf (file
, "\n\tBB #%d", loc
->bb
->index
);
4828 fprintf (file
, "\n\tEDGE %d->%d", loc
->e
->src
->index
,
4829 loc
->e
->dest
->index
);
4830 dump_edge_info (file
, loc
->e
, dump_flags
, 0);
4832 fprintf (file
, "\n\tPREDICATE: ");
4833 print_generic_expr (file
, name
, 0);
4834 fprintf (file
, " %s ", get_tree_code_name (loc
->comp_code
));
4835 print_generic_expr (file
, loc
->val
, 0);
4836 fprintf (file
, "\n\n");
4840 fprintf (file
, "\n");
4844 /* Dump all the registered assertions for NAME to stderr. */
4847 debug_asserts_for (tree name
)
4849 dump_asserts_for (stderr
, name
);
4853 /* Dump all the registered assertions for all the names to FILE. */
4856 dump_all_asserts (FILE *file
)
4861 fprintf (file
, "\nASSERT_EXPRs to be inserted\n\n");
4862 EXECUTE_IF_SET_IN_BITMAP (need_assert_for
, 0, i
, bi
)
4863 dump_asserts_for (file
, ssa_name (i
));
4864 fprintf (file
, "\n");
4868 /* Dump all the registered assertions for all the names to stderr. */
4871 debug_all_asserts (void)
4873 dump_all_asserts (stderr
);
4877 /* If NAME doesn't have an ASSERT_EXPR registered for asserting
4878 'EXPR COMP_CODE VAL' at a location that dominates block BB or
4879 E->DEST, then register this location as a possible insertion point
4880 for ASSERT_EXPR <NAME, EXPR COMP_CODE VAL>.
4882 BB, E and SI provide the exact insertion point for the new
4883 ASSERT_EXPR. If BB is NULL, then the ASSERT_EXPR is to be inserted
4884 on edge E. Otherwise, if E is NULL, the ASSERT_EXPR is inserted on
4885 BB. If SI points to a COND_EXPR or a SWITCH_EXPR statement, then E
4886 must not be NULL. */
4889 register_new_assert_for (tree name
, tree expr
,
4890 enum tree_code comp_code
,
4894 gimple_stmt_iterator si
)
4896 assert_locus
*n
, *loc
, *last_loc
;
4897 basic_block dest_bb
;
4899 gcc_checking_assert (bb
== NULL
|| e
== NULL
);
4902 gcc_checking_assert (gimple_code (gsi_stmt (si
)) != GIMPLE_COND
4903 && gimple_code (gsi_stmt (si
)) != GIMPLE_SWITCH
);
4905 /* Never build an assert comparing against an integer constant with
4906 TREE_OVERFLOW set. This confuses our undefined overflow warning
4908 if (TREE_OVERFLOW_P (val
))
4909 val
= drop_tree_overflow (val
);
4911 /* The new assertion A will be inserted at BB or E. We need to
4912 determine if the new location is dominated by a previously
4913 registered location for A. If we are doing an edge insertion,
4914 assume that A will be inserted at E->DEST. Note that this is not
4917 If E is a critical edge, it will be split. But even if E is
4918 split, the new block will dominate the same set of blocks that
4921 The reverse, however, is not true, blocks dominated by E->DEST
4922 will not be dominated by the new block created to split E. So,
4923 if the insertion location is on a critical edge, we will not use
4924 the new location to move another assertion previously registered
4925 at a block dominated by E->DEST. */
4926 dest_bb
= (bb
) ? bb
: e
->dest
;
4928 /* If NAME already has an ASSERT_EXPR registered for COMP_CODE and
4929 VAL at a block dominating DEST_BB, then we don't need to insert a new
4930 one. Similarly, if the same assertion already exists at a block
4931 dominated by DEST_BB and the new location is not on a critical
4932 edge, then update the existing location for the assertion (i.e.,
4933 move the assertion up in the dominance tree).
4935 Note, this is implemented as a simple linked list because there
4936 should not be more than a handful of assertions registered per
4937 name. If this becomes a performance problem, a table hashed by
4938 COMP_CODE and VAL could be implemented. */
4939 loc
= asserts_for
[SSA_NAME_VERSION (name
)];
4943 if (loc
->comp_code
== comp_code
4945 || operand_equal_p (loc
->val
, val
, 0))
4946 && (loc
->expr
== expr
4947 || operand_equal_p (loc
->expr
, expr
, 0)))
4949 /* If E is not a critical edge and DEST_BB
4950 dominates the existing location for the assertion, move
4951 the assertion up in the dominance tree by updating its
4952 location information. */
4953 if ((e
== NULL
|| !EDGE_CRITICAL_P (e
))
4954 && dominated_by_p (CDI_DOMINATORS
, loc
->bb
, dest_bb
))
4963 /* Update the last node of the list and move to the next one. */
4968 /* If we didn't find an assertion already registered for
4969 NAME COMP_CODE VAL, add a new one at the end of the list of
4970 assertions associated with NAME. */
4971 n
= XNEW (struct assert_locus
);
4975 n
->comp_code
= comp_code
;
4983 asserts_for
[SSA_NAME_VERSION (name
)] = n
;
4985 bitmap_set_bit (need_assert_for
, SSA_NAME_VERSION (name
));
4988 /* (COND_OP0 COND_CODE COND_OP1) is a predicate which uses NAME.
4989 Extract a suitable test code and value and store them into *CODE_P and
4990 *VAL_P so the predicate is normalized to NAME *CODE_P *VAL_P.
4992 If no extraction was possible, return FALSE, otherwise return TRUE.
4994 If INVERT is true, then we invert the result stored into *CODE_P. */
4997 extract_code_and_val_from_cond_with_ops (tree name
, enum tree_code cond_code
,
4998 tree cond_op0
, tree cond_op1
,
4999 bool invert
, enum tree_code
*code_p
,
5002 enum tree_code comp_code
;
5005 /* Otherwise, we have a comparison of the form NAME COMP VAL
5006 or VAL COMP NAME. */
5007 if (name
== cond_op1
)
5009 /* If the predicate is of the form VAL COMP NAME, flip
5010 COMP around because we need to register NAME as the
5011 first operand in the predicate. */
5012 comp_code
= swap_tree_comparison (cond_code
);
5017 /* The comparison is of the form NAME COMP VAL, so the
5018 comparison code remains unchanged. */
5019 comp_code
= cond_code
;
5023 /* Invert the comparison code as necessary. */
5025 comp_code
= invert_tree_comparison (comp_code
, 0);
5027 /* VRP does not handle float types. */
5028 if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (val
)))
5031 /* Do not register always-false predicates.
5032 FIXME: this works around a limitation in fold() when dealing with
5033 enumerations. Given 'enum { N1, N2 } x;', fold will not
5034 fold 'if (x > N2)' to 'if (0)'. */
5035 if ((comp_code
== GT_EXPR
|| comp_code
== LT_EXPR
)
5036 && INTEGRAL_TYPE_P (TREE_TYPE (val
)))
5038 tree min
= TYPE_MIN_VALUE (TREE_TYPE (val
));
5039 tree max
= TYPE_MAX_VALUE (TREE_TYPE (val
));
5041 if (comp_code
== GT_EXPR
5043 || compare_values (val
, max
) == 0))
5046 if (comp_code
== LT_EXPR
5048 || compare_values (val
, min
) == 0))
5051 *code_p
= comp_code
;
5056 /* Find out smallest RES where RES > VAL && (RES & MASK) == RES, if any
5057 (otherwise return VAL). VAL and MASK must be zero-extended for
5058 precision PREC. If SGNBIT is non-zero, first xor VAL with SGNBIT
5059 (to transform signed values into unsigned) and at the end xor
5063 masked_increment (const wide_int
&val_in
, const wide_int
&mask
,
5064 const wide_int
&sgnbit
, unsigned int prec
)
5066 wide_int bit
= wi::one (prec
), res
;
5069 wide_int val
= val_in
^ sgnbit
;
5070 for (i
= 0; i
< prec
; i
++, bit
+= bit
)
5073 if ((res
& bit
) == 0)
5076 res
= (val
+ bit
).and_not (res
);
5078 if (wi::gtu_p (res
, val
))
5079 return res
^ sgnbit
;
5081 return val
^ sgnbit
;
5084 /* Try to register an edge assertion for SSA name NAME on edge E for
5085 the condition COND contributing to the conditional jump pointed to by BSI.
5086 Invert the condition COND if INVERT is true. */
5089 register_edge_assert_for_2 (tree name
, edge e
, gimple_stmt_iterator bsi
,
5090 enum tree_code cond_code
,
5091 tree cond_op0
, tree cond_op1
, bool invert
)
5094 enum tree_code comp_code
;
5096 if (!extract_code_and_val_from_cond_with_ops (name
, cond_code
,
5099 invert
, &comp_code
, &val
))
5102 /* Only register an ASSERT_EXPR if NAME was found in the sub-graph
5103 reachable from E. */
5104 if (live_on_edge (e
, name
)
5105 && !has_single_use (name
))
5106 register_new_assert_for (name
, name
, comp_code
, val
, NULL
, e
, bsi
);
5108 /* In the case of NAME <= CST and NAME being defined as
5109 NAME = (unsigned) NAME2 + CST2 we can assert NAME2 >= -CST2
5110 and NAME2 <= CST - CST2. We can do the same for NAME > CST.
5111 This catches range and anti-range tests. */
5112 if ((comp_code
== LE_EXPR
5113 || comp_code
== GT_EXPR
)
5114 && TREE_CODE (val
) == INTEGER_CST
5115 && TYPE_UNSIGNED (TREE_TYPE (val
)))
5117 gimple
*def_stmt
= SSA_NAME_DEF_STMT (name
);
5118 tree cst2
= NULL_TREE
, name2
= NULL_TREE
, name3
= NULL_TREE
;
5120 /* Extract CST2 from the (optional) addition. */
5121 if (is_gimple_assign (def_stmt
)
5122 && gimple_assign_rhs_code (def_stmt
) == PLUS_EXPR
)
5124 name2
= gimple_assign_rhs1 (def_stmt
);
5125 cst2
= gimple_assign_rhs2 (def_stmt
);
5126 if (TREE_CODE (name2
) == SSA_NAME
5127 && TREE_CODE (cst2
) == INTEGER_CST
)
5128 def_stmt
= SSA_NAME_DEF_STMT (name2
);
5131 /* Extract NAME2 from the (optional) sign-changing cast. */
5132 if (gimple_assign_cast_p (def_stmt
))
5134 if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt
))
5135 && ! TYPE_UNSIGNED (TREE_TYPE (gimple_assign_rhs1 (def_stmt
)))
5136 && (TYPE_PRECISION (gimple_expr_type (def_stmt
))
5137 == TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (def_stmt
)))))
5138 name3
= gimple_assign_rhs1 (def_stmt
);
5141 /* If name3 is used later, create an ASSERT_EXPR for it. */
5142 if (name3
!= NULL_TREE
5143 && TREE_CODE (name3
) == SSA_NAME
5144 && (cst2
== NULL_TREE
5145 || TREE_CODE (cst2
) == INTEGER_CST
)
5146 && INTEGRAL_TYPE_P (TREE_TYPE (name3
))
5147 && live_on_edge (e
, name3
)
5148 && !has_single_use (name3
))
5152 /* Build an expression for the range test. */
5153 tmp
= build1 (NOP_EXPR
, TREE_TYPE (name
), name3
);
5154 if (cst2
!= NULL_TREE
)
5155 tmp
= build2 (PLUS_EXPR
, TREE_TYPE (name
), tmp
, cst2
);
5159 fprintf (dump_file
, "Adding assert for ");
5160 print_generic_expr (dump_file
, name3
, 0);
5161 fprintf (dump_file
, " from ");
5162 print_generic_expr (dump_file
, tmp
, 0);
5163 fprintf (dump_file
, "\n");
5166 register_new_assert_for (name3
, tmp
, comp_code
, val
, NULL
, e
, bsi
);
5169 /* If name2 is used later, create an ASSERT_EXPR for it. */
5170 if (name2
!= NULL_TREE
5171 && TREE_CODE (name2
) == SSA_NAME
5172 && TREE_CODE (cst2
) == INTEGER_CST
5173 && INTEGRAL_TYPE_P (TREE_TYPE (name2
))
5174 && live_on_edge (e
, name2
)
5175 && !has_single_use (name2
))
5179 /* Build an expression for the range test. */
5181 if (TREE_TYPE (name
) != TREE_TYPE (name2
))
5182 tmp
= build1 (NOP_EXPR
, TREE_TYPE (name
), tmp
);
5183 if (cst2
!= NULL_TREE
)
5184 tmp
= build2 (PLUS_EXPR
, TREE_TYPE (name
), tmp
, cst2
);
5188 fprintf (dump_file
, "Adding assert for ");
5189 print_generic_expr (dump_file
, name2
, 0);
5190 fprintf (dump_file
, " from ");
5191 print_generic_expr (dump_file
, tmp
, 0);
5192 fprintf (dump_file
, "\n");
5195 register_new_assert_for (name2
, tmp
, comp_code
, val
, NULL
, e
, bsi
);
5199 /* In the case of post-in/decrement tests like if (i++) ... and uses
5200 of the in/decremented value on the edge the extra name we want to
5201 assert for is not on the def chain of the name compared. Instead
5202 it is in the set of use stmts.
5203 Similar cases happen for conversions that were simplified through
5204 fold_{sign_changed,widened}_comparison. */
5205 if ((comp_code
== NE_EXPR
5206 || comp_code
== EQ_EXPR
)
5207 && TREE_CODE (val
) == INTEGER_CST
)
5209 imm_use_iterator ui
;
5211 FOR_EACH_IMM_USE_STMT (use_stmt
, ui
, name
)
5213 if (!is_gimple_assign (use_stmt
))
5216 /* Cut off to use-stmts that are dominating the predecessor. */
5217 if (!dominated_by_p (CDI_DOMINATORS
, e
->src
, gimple_bb (use_stmt
)))
5220 tree name2
= gimple_assign_lhs (use_stmt
);
5221 if (TREE_CODE (name2
) != SSA_NAME
5222 || !live_on_edge (e
, name2
))
5225 enum tree_code code
= gimple_assign_rhs_code (use_stmt
);
5227 if (code
== PLUS_EXPR
5228 || code
== MINUS_EXPR
)
5230 cst
= gimple_assign_rhs2 (use_stmt
);
5231 if (TREE_CODE (cst
) != INTEGER_CST
)
5233 cst
= int_const_binop (code
, val
, cst
);
5235 else if (CONVERT_EXPR_CODE_P (code
))
5237 /* For truncating conversions we cannot record
5239 if (comp_code
== NE_EXPR
5240 && (TYPE_PRECISION (TREE_TYPE (name2
))
5241 < TYPE_PRECISION (TREE_TYPE (name
))))
5243 cst
= fold_convert (TREE_TYPE (name2
), val
);
5248 if (TREE_OVERFLOW_P (cst
))
5249 cst
= drop_tree_overflow (cst
);
5250 register_new_assert_for (name2
, name2
, comp_code
, cst
,
5255 if (TREE_CODE_CLASS (comp_code
) == tcc_comparison
5256 && TREE_CODE (val
) == INTEGER_CST
)
5258 gimple
*def_stmt
= SSA_NAME_DEF_STMT (name
);
5259 tree name2
= NULL_TREE
, names
[2], cst2
= NULL_TREE
;
5260 tree val2
= NULL_TREE
;
5261 unsigned int prec
= TYPE_PRECISION (TREE_TYPE (val
));
5262 wide_int mask
= wi::zero (prec
);
5263 unsigned int nprec
= prec
;
5264 enum tree_code rhs_code
= ERROR_MARK
;
5266 if (is_gimple_assign (def_stmt
))
5267 rhs_code
= gimple_assign_rhs_code (def_stmt
);
5269 /* Add asserts for NAME cmp CST and NAME being defined
5270 as NAME = (int) NAME2. */
5271 if (!TYPE_UNSIGNED (TREE_TYPE (val
))
5272 && (comp_code
== LE_EXPR
|| comp_code
== LT_EXPR
5273 || comp_code
== GT_EXPR
|| comp_code
== GE_EXPR
)
5274 && gimple_assign_cast_p (def_stmt
))
5276 name2
= gimple_assign_rhs1 (def_stmt
);
5277 if (CONVERT_EXPR_CODE_P (rhs_code
)
5278 && INTEGRAL_TYPE_P (TREE_TYPE (name2
))
5279 && TYPE_UNSIGNED (TREE_TYPE (name2
))
5280 && prec
== TYPE_PRECISION (TREE_TYPE (name2
))
5281 && (comp_code
== LE_EXPR
|| comp_code
== GT_EXPR
5282 || !tree_int_cst_equal (val
,
5283 TYPE_MIN_VALUE (TREE_TYPE (val
))))
5284 && live_on_edge (e
, name2
)
5285 && !has_single_use (name2
))
5288 enum tree_code new_comp_code
= comp_code
;
5290 cst
= fold_convert (TREE_TYPE (name2
),
5291 TYPE_MIN_VALUE (TREE_TYPE (val
)));
5292 /* Build an expression for the range test. */
5293 tmp
= build2 (PLUS_EXPR
, TREE_TYPE (name2
), name2
, cst
);
5294 cst
= fold_build2 (PLUS_EXPR
, TREE_TYPE (name2
), cst
,
5295 fold_convert (TREE_TYPE (name2
), val
));
5296 if (comp_code
== LT_EXPR
|| comp_code
== GE_EXPR
)
5298 new_comp_code
= comp_code
== LT_EXPR
? LE_EXPR
: GT_EXPR
;
5299 cst
= fold_build2 (MINUS_EXPR
, TREE_TYPE (name2
), cst
,
5300 build_int_cst (TREE_TYPE (name2
), 1));
5305 fprintf (dump_file
, "Adding assert for ");
5306 print_generic_expr (dump_file
, name2
, 0);
5307 fprintf (dump_file
, " from ");
5308 print_generic_expr (dump_file
, tmp
, 0);
5309 fprintf (dump_file
, "\n");
5312 register_new_assert_for (name2
, tmp
, new_comp_code
, cst
, NULL
,
5317 /* Add asserts for NAME cmp CST and NAME being defined as
5318 NAME = NAME2 >> CST2.
5320 Extract CST2 from the right shift. */
5321 if (rhs_code
== RSHIFT_EXPR
)
5323 name2
= gimple_assign_rhs1 (def_stmt
);
5324 cst2
= gimple_assign_rhs2 (def_stmt
);
5325 if (TREE_CODE (name2
) == SSA_NAME
5326 && tree_fits_uhwi_p (cst2
)
5327 && INTEGRAL_TYPE_P (TREE_TYPE (name2
))
5328 && IN_RANGE (tree_to_uhwi (cst2
), 1, prec
- 1)
5329 && prec
== GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (val
)))
5330 && live_on_edge (e
, name2
)
5331 && !has_single_use (name2
))
5333 mask
= wi::mask (tree_to_uhwi (cst2
), false, prec
);
5334 val2
= fold_binary (LSHIFT_EXPR
, TREE_TYPE (val
), val
, cst2
);
5337 if (val2
!= NULL_TREE
5338 && TREE_CODE (val2
) == INTEGER_CST
5339 && simple_cst_equal (fold_build2 (RSHIFT_EXPR
,
5343 enum tree_code new_comp_code
= comp_code
;
5347 if (comp_code
== EQ_EXPR
|| comp_code
== NE_EXPR
)
5349 if (!TYPE_UNSIGNED (TREE_TYPE (val
)))
5351 tree type
= build_nonstandard_integer_type (prec
, 1);
5352 tmp
= build1 (NOP_EXPR
, type
, name2
);
5353 val2
= fold_convert (type
, val2
);
5355 tmp
= fold_build2 (MINUS_EXPR
, TREE_TYPE (tmp
), tmp
, val2
);
5356 new_val
= wide_int_to_tree (TREE_TYPE (tmp
), mask
);
5357 new_comp_code
= comp_code
== EQ_EXPR
? LE_EXPR
: GT_EXPR
;
5359 else if (comp_code
== LT_EXPR
|| comp_code
== GE_EXPR
)
5362 = wi::min_value (prec
, TYPE_SIGN (TREE_TYPE (val
)));
5364 if (minval
== new_val
)
5365 new_val
= NULL_TREE
;
5370 = wi::max_value (prec
, TYPE_SIGN (TREE_TYPE (val
)));
5373 new_val
= NULL_TREE
;
5375 new_val
= wide_int_to_tree (TREE_TYPE (val2
), mask
);
5382 fprintf (dump_file
, "Adding assert for ");
5383 print_generic_expr (dump_file
, name2
, 0);
5384 fprintf (dump_file
, " from ");
5385 print_generic_expr (dump_file
, tmp
, 0);
5386 fprintf (dump_file
, "\n");
5389 register_new_assert_for (name2
, tmp
, new_comp_code
, new_val
,
5394 /* Add asserts for NAME cmp CST and NAME being defined as
5395 NAME = NAME2 & CST2.
5397 Extract CST2 from the and.
5400 NAME = (unsigned) NAME2;
5401 casts where NAME's type is unsigned and has smaller precision
5402 than NAME2's type as if it was NAME = NAME2 & MASK. */
5403 names
[0] = NULL_TREE
;
5404 names
[1] = NULL_TREE
;
5406 if (rhs_code
== BIT_AND_EXPR
5407 || (CONVERT_EXPR_CODE_P (rhs_code
)
5408 && TREE_CODE (TREE_TYPE (val
)) == INTEGER_TYPE
5409 && TYPE_UNSIGNED (TREE_TYPE (val
))
5410 && TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (def_stmt
)))
5413 name2
= gimple_assign_rhs1 (def_stmt
);
5414 if (rhs_code
== BIT_AND_EXPR
)
5415 cst2
= gimple_assign_rhs2 (def_stmt
);
5418 cst2
= TYPE_MAX_VALUE (TREE_TYPE (val
));
5419 nprec
= TYPE_PRECISION (TREE_TYPE (name2
));
5421 if (TREE_CODE (name2
) == SSA_NAME
5422 && INTEGRAL_TYPE_P (TREE_TYPE (name2
))
5423 && TREE_CODE (cst2
) == INTEGER_CST
5424 && !integer_zerop (cst2
)
5426 || TYPE_UNSIGNED (TREE_TYPE (val
))))
5428 gimple
*def_stmt2
= SSA_NAME_DEF_STMT (name2
);
5429 if (gimple_assign_cast_p (def_stmt2
))
5431 names
[1] = gimple_assign_rhs1 (def_stmt2
);
5432 if (!CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt2
))
5433 || !INTEGRAL_TYPE_P (TREE_TYPE (names
[1]))
5434 || (TYPE_PRECISION (TREE_TYPE (name2
))
5435 != TYPE_PRECISION (TREE_TYPE (names
[1])))
5436 || !live_on_edge (e
, names
[1])
5437 || has_single_use (names
[1]))
5438 names
[1] = NULL_TREE
;
5440 if (live_on_edge (e
, name2
)
5441 && !has_single_use (name2
))
5445 if (names
[0] || names
[1])
5447 wide_int minv
, maxv
, valv
, cst2v
;
5448 wide_int tem
, sgnbit
;
5449 bool valid_p
= false, valn
, cst2n
;
5450 enum tree_code ccode
= comp_code
;
5452 valv
= wide_int::from (val
, nprec
, UNSIGNED
);
5453 cst2v
= wide_int::from (cst2
, nprec
, UNSIGNED
);
5454 valn
= wi::neg_p (valv
, TYPE_SIGN (TREE_TYPE (val
)));
5455 cst2n
= wi::neg_p (cst2v
, TYPE_SIGN (TREE_TYPE (val
)));
5456 /* If CST2 doesn't have most significant bit set,
5457 but VAL is negative, we have comparison like
5458 if ((x & 0x123) > -4) (always true). Just give up. */
5462 sgnbit
= wi::set_bit_in_zero (nprec
- 1, nprec
);
5464 sgnbit
= wi::zero (nprec
);
5465 minv
= valv
& cst2v
;
5469 /* Minimum unsigned value for equality is VAL & CST2
5470 (should be equal to VAL, otherwise we probably should
5471 have folded the comparison into false) and
5472 maximum unsigned value is VAL | ~CST2. */
5473 maxv
= valv
| ~cst2v
;
5478 tem
= valv
| ~cst2v
;
5479 /* If VAL is 0, handle (X & CST2) != 0 as (X & CST2) > 0U. */
5483 sgnbit
= wi::zero (nprec
);
5486 /* If (VAL | ~CST2) is all ones, handle it as
5487 (X & CST2) < VAL. */
5492 sgnbit
= wi::zero (nprec
);
5495 if (!cst2n
&& wi::neg_p (cst2v
))
5496 sgnbit
= wi::set_bit_in_zero (nprec
- 1, nprec
);
5505 if (tem
== wi::mask (nprec
- 1, false, nprec
))
5511 sgnbit
= wi::zero (nprec
);
5516 /* Minimum unsigned value for >= if (VAL & CST2) == VAL
5517 is VAL and maximum unsigned value is ~0. For signed
5518 comparison, if CST2 doesn't have most significant bit
5519 set, handle it similarly. If CST2 has MSB set,
5520 the minimum is the same, and maximum is ~0U/2. */
5523 /* If (VAL & CST2) != VAL, X & CST2 can't be equal to
5525 minv
= masked_increment (valv
, cst2v
, sgnbit
, nprec
);
5529 maxv
= wi::mask (nprec
- (cst2n
? 1 : 0), false, nprec
);
5535 /* Find out smallest MINV where MINV > VAL
5536 && (MINV & CST2) == MINV, if any. If VAL is signed and
5537 CST2 has MSB set, compute it biased by 1 << (nprec - 1). */
5538 minv
= masked_increment (valv
, cst2v
, sgnbit
, nprec
);
5541 maxv
= wi::mask (nprec
- (cst2n
? 1 : 0), false, nprec
);
5546 /* Minimum unsigned value for <= is 0 and maximum
5547 unsigned value is VAL | ~CST2 if (VAL & CST2) == VAL.
5548 Otherwise, find smallest VAL2 where VAL2 > VAL
5549 && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
5551 For signed comparison, if CST2 doesn't have most
5552 significant bit set, handle it similarly. If CST2 has
5553 MSB set, the maximum is the same and minimum is INT_MIN. */
5558 maxv
= masked_increment (valv
, cst2v
, sgnbit
, nprec
);
5570 /* Minimum unsigned value for < is 0 and maximum
5571 unsigned value is (VAL-1) | ~CST2 if (VAL & CST2) == VAL.
5572 Otherwise, find smallest VAL2 where VAL2 > VAL
5573 && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
5575 For signed comparison, if CST2 doesn't have most
5576 significant bit set, handle it similarly. If CST2 has
5577 MSB set, the maximum is the same and minimum is INT_MIN. */
5586 maxv
= masked_increment (valv
, cst2v
, sgnbit
, nprec
);
5600 && (maxv
- minv
) != -1)
5602 tree tmp
, new_val
, type
;
5605 for (i
= 0; i
< 2; i
++)
5608 wide_int maxv2
= maxv
;
5610 type
= TREE_TYPE (names
[i
]);
5611 if (!TYPE_UNSIGNED (type
))
5613 type
= build_nonstandard_integer_type (nprec
, 1);
5614 tmp
= build1 (NOP_EXPR
, type
, names
[i
]);
5618 tmp
= build2 (PLUS_EXPR
, type
, tmp
,
5619 wide_int_to_tree (type
, -minv
));
5620 maxv2
= maxv
- minv
;
5622 new_val
= wide_int_to_tree (type
, maxv2
);
5626 fprintf (dump_file
, "Adding assert for ");
5627 print_generic_expr (dump_file
, names
[i
], 0);
5628 fprintf (dump_file
, " from ");
5629 print_generic_expr (dump_file
, tmp
, 0);
5630 fprintf (dump_file
, "\n");
5633 register_new_assert_for (names
[i
], tmp
, LE_EXPR
,
5634 new_val
, NULL
, e
, bsi
);
5641 /* OP is an operand of a truth value expression which is known to have
5642 a particular value. Register any asserts for OP and for any
5643 operands in OP's defining statement.
5645 If CODE is EQ_EXPR, then we want to register OP is zero (false),
5646 if CODE is NE_EXPR, then we want to register OP is nonzero (true). */
5649 register_edge_assert_for_1 (tree op
, enum tree_code code
,
5650 edge e
, gimple_stmt_iterator bsi
)
5654 enum tree_code rhs_code
;
5656 /* We only care about SSA_NAMEs. */
5657 if (TREE_CODE (op
) != SSA_NAME
)
5660 /* We know that OP will have a zero or nonzero value. If OP is used
5661 more than once go ahead and register an assert for OP. */
5662 if (live_on_edge (e
, op
)
5663 && !has_single_use (op
))
5665 val
= build_int_cst (TREE_TYPE (op
), 0);
5666 register_new_assert_for (op
, op
, code
, val
, NULL
, e
, bsi
);
5669 /* Now look at how OP is set. If it's set from a comparison,
5670 a truth operation or some bit operations, then we may be able
5671 to register information about the operands of that assignment. */
5672 op_def
= SSA_NAME_DEF_STMT (op
);
5673 if (gimple_code (op_def
) != GIMPLE_ASSIGN
)
5676 rhs_code
= gimple_assign_rhs_code (op_def
);
5678 if (TREE_CODE_CLASS (rhs_code
) == tcc_comparison
)
5680 bool invert
= (code
== EQ_EXPR
? true : false);
5681 tree op0
= gimple_assign_rhs1 (op_def
);
5682 tree op1
= gimple_assign_rhs2 (op_def
);
5684 if (TREE_CODE (op0
) == SSA_NAME
)
5685 register_edge_assert_for_2 (op0
, e
, bsi
, rhs_code
, op0
, op1
, invert
);
5686 if (TREE_CODE (op1
) == SSA_NAME
)
5687 register_edge_assert_for_2 (op1
, e
, bsi
, rhs_code
, op0
, op1
, invert
);
5689 else if ((code
== NE_EXPR
5690 && gimple_assign_rhs_code (op_def
) == BIT_AND_EXPR
)
5692 && gimple_assign_rhs_code (op_def
) == BIT_IOR_EXPR
))
5694 /* Recurse on each operand. */
5695 tree op0
= gimple_assign_rhs1 (op_def
);
5696 tree op1
= gimple_assign_rhs2 (op_def
);
5697 if (TREE_CODE (op0
) == SSA_NAME
5698 && has_single_use (op0
))
5699 register_edge_assert_for_1 (op0
, code
, e
, bsi
);
5700 if (TREE_CODE (op1
) == SSA_NAME
5701 && has_single_use (op1
))
5702 register_edge_assert_for_1 (op1
, code
, e
, bsi
);
5704 else if (gimple_assign_rhs_code (op_def
) == BIT_NOT_EXPR
5705 && TYPE_PRECISION (TREE_TYPE (gimple_assign_lhs (op_def
))) == 1)
5707 /* Recurse, flipping CODE. */
5708 code
= invert_tree_comparison (code
, false);
5709 register_edge_assert_for_1 (gimple_assign_rhs1 (op_def
), code
, e
, bsi
);
5711 else if (gimple_assign_rhs_code (op_def
) == SSA_NAME
)
5713 /* Recurse through the copy. */
5714 register_edge_assert_for_1 (gimple_assign_rhs1 (op_def
), code
, e
, bsi
);
5716 else if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (op_def
)))
5718 /* Recurse through the type conversion, unless it is a narrowing
5719 conversion or conversion from non-integral type. */
5720 tree rhs
= gimple_assign_rhs1 (op_def
);
5721 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs
))
5722 && (TYPE_PRECISION (TREE_TYPE (rhs
))
5723 <= TYPE_PRECISION (TREE_TYPE (op
))))
5724 register_edge_assert_for_1 (rhs
, code
, e
, bsi
);
5728 /* Try to register an edge assertion for SSA name NAME on edge E for
5729 the condition COND contributing to the conditional jump pointed to by
5733 register_edge_assert_for (tree name
, edge e
, gimple_stmt_iterator si
,
5734 enum tree_code cond_code
, tree cond_op0
,
5738 enum tree_code comp_code
;
5739 bool is_else_edge
= (e
->flags
& EDGE_FALSE_VALUE
) != 0;
5741 /* Do not attempt to infer anything in names that flow through
5743 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name
))
5746 if (!extract_code_and_val_from_cond_with_ops (name
, cond_code
,
5752 /* Register ASSERT_EXPRs for name. */
5753 register_edge_assert_for_2 (name
, e
, si
, cond_code
, cond_op0
,
5754 cond_op1
, is_else_edge
);
5757 /* If COND is effectively an equality test of an SSA_NAME against
5758 the value zero or one, then we may be able to assert values
5759 for SSA_NAMEs which flow into COND. */
5761 /* In the case of NAME == 1 or NAME != 0, for BIT_AND_EXPR defining
5762 statement of NAME we can assert both operands of the BIT_AND_EXPR
5763 have nonzero value. */
5764 if (((comp_code
== EQ_EXPR
&& integer_onep (val
))
5765 || (comp_code
== NE_EXPR
&& integer_zerop (val
))))
5767 gimple
*def_stmt
= SSA_NAME_DEF_STMT (name
);
5769 if (is_gimple_assign (def_stmt
)
5770 && gimple_assign_rhs_code (def_stmt
) == BIT_AND_EXPR
)
5772 tree op0
= gimple_assign_rhs1 (def_stmt
);
5773 tree op1
= gimple_assign_rhs2 (def_stmt
);
5774 register_edge_assert_for_1 (op0
, NE_EXPR
, e
, si
);
5775 register_edge_assert_for_1 (op1
, NE_EXPR
, e
, si
);
5779 /* In the case of NAME == 0 or NAME != 1, for BIT_IOR_EXPR defining
5780 statement of NAME we can assert both operands of the BIT_IOR_EXPR
5782 if (((comp_code
== EQ_EXPR
&& integer_zerop (val
))
5783 || (comp_code
== NE_EXPR
&& integer_onep (val
))))
5785 gimple
*def_stmt
= SSA_NAME_DEF_STMT (name
);
5787 /* For BIT_IOR_EXPR only if NAME == 0 both operands have
5788 necessarily zero value, or if type-precision is one. */
5789 if (is_gimple_assign (def_stmt
)
5790 && (gimple_assign_rhs_code (def_stmt
) == BIT_IOR_EXPR
5791 && (TYPE_PRECISION (TREE_TYPE (name
)) == 1
5792 || comp_code
== EQ_EXPR
)))
5794 tree op0
= gimple_assign_rhs1 (def_stmt
);
5795 tree op1
= gimple_assign_rhs2 (def_stmt
);
5796 register_edge_assert_for_1 (op0
, EQ_EXPR
, e
, si
);
5797 register_edge_assert_for_1 (op1
, EQ_EXPR
, e
, si
);
5803 /* Determine whether the outgoing edges of BB should receive an
5804 ASSERT_EXPR for each of the operands of BB's LAST statement.
5805 The last statement of BB must be a COND_EXPR.
5807 If any of the sub-graphs rooted at BB have an interesting use of
5808 the predicate operands, an assert location node is added to the
5809 list of assertions for the corresponding operands. */
5812 find_conditional_asserts (basic_block bb
, gcond
*last
)
5814 gimple_stmt_iterator bsi
;
5820 bsi
= gsi_for_stmt (last
);
5822 /* Look for uses of the operands in each of the sub-graphs
5823 rooted at BB. We need to check each of the outgoing edges
5824 separately, so that we know what kind of ASSERT_EXPR to
5826 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
5831 /* Register the necessary assertions for each operand in the
5832 conditional predicate. */
5833 FOR_EACH_SSA_TREE_OPERAND (op
, last
, iter
, SSA_OP_USE
)
5834 register_edge_assert_for (op
, e
, bsi
,
5835 gimple_cond_code (last
),
5836 gimple_cond_lhs (last
),
5837 gimple_cond_rhs (last
));
5847 /* Compare two case labels sorting first by the destination bb index
5848 and then by the case value. */
5851 compare_case_labels (const void *p1
, const void *p2
)
5853 const struct case_info
*ci1
= (const struct case_info
*) p1
;
5854 const struct case_info
*ci2
= (const struct case_info
*) p2
;
5855 int idx1
= ci1
->bb
->index
;
5856 int idx2
= ci2
->bb
->index
;
5860 else if (idx1
== idx2
)
5862 /* Make sure the default label is first in a group. */
5863 if (!CASE_LOW (ci1
->expr
))
5865 else if (!CASE_LOW (ci2
->expr
))
5868 return tree_int_cst_compare (CASE_LOW (ci1
->expr
),
5869 CASE_LOW (ci2
->expr
));
5875 /* Determine whether the outgoing edges of BB should receive an
5876 ASSERT_EXPR for each of the operands of BB's LAST statement.
5877 The last statement of BB must be a SWITCH_EXPR.
5879 If any of the sub-graphs rooted at BB have an interesting use of
5880 the predicate operands, an assert location node is added to the
5881 list of assertions for the corresponding operands. */
5884 find_switch_asserts (basic_block bb
, gswitch
*last
)
5886 gimple_stmt_iterator bsi
;
5889 struct case_info
*ci
;
5890 size_t n
= gimple_switch_num_labels (last
);
5891 #if GCC_VERSION >= 4000
5894 /* Work around GCC 3.4 bug (PR 37086). */
5895 volatile unsigned int idx
;
5898 bsi
= gsi_for_stmt (last
);
5899 op
= gimple_switch_index (last
);
5900 if (TREE_CODE (op
) != SSA_NAME
)
5903 /* Build a vector of case labels sorted by destination label. */
5904 ci
= XNEWVEC (struct case_info
, n
);
5905 for (idx
= 0; idx
< n
; ++idx
)
5907 ci
[idx
].expr
= gimple_switch_label (last
, idx
);
5908 ci
[idx
].bb
= label_to_block (CASE_LABEL (ci
[idx
].expr
));
5910 qsort (ci
, n
, sizeof (struct case_info
), compare_case_labels
);
5912 for (idx
= 0; idx
< n
; ++idx
)
5915 tree cl
= ci
[idx
].expr
;
5916 basic_block cbb
= ci
[idx
].bb
;
5918 min
= CASE_LOW (cl
);
5919 max
= CASE_HIGH (cl
);
5921 /* If there are multiple case labels with the same destination
5922 we need to combine them to a single value range for the edge. */
5923 if (idx
+ 1 < n
&& cbb
== ci
[idx
+ 1].bb
)
5925 /* Skip labels until the last of the group. */
5928 } while (idx
< n
&& cbb
== ci
[idx
].bb
);
5931 /* Pick up the maximum of the case label range. */
5932 if (CASE_HIGH (ci
[idx
].expr
))
5933 max
= CASE_HIGH (ci
[idx
].expr
);
5935 max
= CASE_LOW (ci
[idx
].expr
);
5938 /* Nothing to do if the range includes the default label until we
5939 can register anti-ranges. */
5940 if (min
== NULL_TREE
)
5943 /* Find the edge to register the assert expr on. */
5944 e
= find_edge (bb
, cbb
);
5946 /* Register the necessary assertions for the operand in the
5948 register_edge_assert_for (op
, e
, bsi
,
5949 max
? GE_EXPR
: EQ_EXPR
,
5950 op
, fold_convert (TREE_TYPE (op
), min
));
5952 register_edge_assert_for (op
, e
, bsi
, LE_EXPR
, op
,
5953 fold_convert (TREE_TYPE (op
), max
));
5960 /* Traverse all the statements in block BB looking for statements that
5961 may generate useful assertions for the SSA names in their operand.
5962 If a statement produces a useful assertion A for name N_i, then the
5963 list of assertions already generated for N_i is scanned to
5964 determine if A is actually needed.
5966 If N_i already had the assertion A at a location dominating the
5967 current location, then nothing needs to be done. Otherwise, the
5968 new location for A is recorded instead.
5970 1- For every statement S in BB, all the variables used by S are
5971 added to bitmap FOUND_IN_SUBGRAPH.
5973 2- If statement S uses an operand N in a way that exposes a known
5974 value range for N, then if N was not already generated by an
5975 ASSERT_EXPR, create a new assert location for N. For instance,
5976 if N is a pointer and the statement dereferences it, we can
5977 assume that N is not NULL.
5979 3- COND_EXPRs are a special case of #2. We can derive range
5980 information from the predicate but need to insert different
5981 ASSERT_EXPRs for each of the sub-graphs rooted at the
5982 conditional block. If the last statement of BB is a conditional
5983 expression of the form 'X op Y', then
5985 a) Remove X and Y from the set FOUND_IN_SUBGRAPH.
5987 b) If the conditional is the only entry point to the sub-graph
5988 corresponding to the THEN_CLAUSE, recurse into it. On
5989 return, if X and/or Y are marked in FOUND_IN_SUBGRAPH, then
5990 an ASSERT_EXPR is added for the corresponding variable.
5992 c) Repeat step (b) on the ELSE_CLAUSE.
5994 d) Mark X and Y in FOUND_IN_SUBGRAPH.
6003 In this case, an assertion on the THEN clause is useful to
6004 determine that 'a' is always 9 on that edge. However, an assertion
6005 on the ELSE clause would be unnecessary.
6007 4- If BB does not end in a conditional expression, then we recurse
6008 into BB's dominator children.
6010 At the end of the recursive traversal, every SSA name will have a
6011 list of locations where ASSERT_EXPRs should be added. When a new
6012 location for name N is found, it is registered by calling
6013 register_new_assert_for. That function keeps track of all the
6014 registered assertions to prevent adding unnecessary assertions.
6015 For instance, if a pointer P_4 is dereferenced more than once in a
6016 dominator tree, only the location dominating all the dereference of
6017 P_4 will receive an ASSERT_EXPR. */
6020 find_assert_locations_1 (basic_block bb
, sbitmap live
)
6024 last
= last_stmt (bb
);
6026 /* If BB's last statement is a conditional statement involving integer
6027 operands, determine if we need to add ASSERT_EXPRs. */
6029 && gimple_code (last
) == GIMPLE_COND
6030 && !fp_predicate (last
)
6031 && !ZERO_SSA_OPERANDS (last
, SSA_OP_USE
))
6032 find_conditional_asserts (bb
, as_a
<gcond
*> (last
));
6034 /* If BB's last statement is a switch statement involving integer
6035 operands, determine if we need to add ASSERT_EXPRs. */
6037 && gimple_code (last
) == GIMPLE_SWITCH
6038 && !ZERO_SSA_OPERANDS (last
, SSA_OP_USE
))
6039 find_switch_asserts (bb
, as_a
<gswitch
*> (last
));
6041 /* Traverse all the statements in BB marking used names and looking
6042 for statements that may infer assertions for their used operands. */
6043 for (gimple_stmt_iterator si
= gsi_last_bb (bb
); !gsi_end_p (si
);
6050 stmt
= gsi_stmt (si
);
6052 if (is_gimple_debug (stmt
))
6055 /* See if we can derive an assertion for any of STMT's operands. */
6056 FOR_EACH_SSA_TREE_OPERAND (op
, stmt
, i
, SSA_OP_USE
)
6059 enum tree_code comp_code
;
6061 /* If op is not live beyond this stmt, do not bother to insert
6063 if (!bitmap_bit_p (live
, SSA_NAME_VERSION (op
)))
6066 /* If OP is used in such a way that we can infer a value
6067 range for it, and we don't find a previous assertion for
6068 it, create a new assertion location node for OP. */
6069 if (infer_value_range (stmt
, op
, &comp_code
, &value
))
6071 /* If we are able to infer a nonzero value range for OP,
6072 then walk backwards through the use-def chain to see if OP
6073 was set via a typecast.
6075 If so, then we can also infer a nonzero value range
6076 for the operand of the NOP_EXPR. */
6077 if (comp_code
== NE_EXPR
&& integer_zerop (value
))
6080 gimple
*def_stmt
= SSA_NAME_DEF_STMT (t
);
6082 while (is_gimple_assign (def_stmt
)
6083 && CONVERT_EXPR_CODE_P
6084 (gimple_assign_rhs_code (def_stmt
))
6086 (gimple_assign_rhs1 (def_stmt
)) == SSA_NAME
6088 (TREE_TYPE (gimple_assign_rhs1 (def_stmt
))))
6090 t
= gimple_assign_rhs1 (def_stmt
);
6091 def_stmt
= SSA_NAME_DEF_STMT (t
);
6093 /* Note we want to register the assert for the
6094 operand of the NOP_EXPR after SI, not after the
6096 if (! has_single_use (t
))
6097 register_new_assert_for (t
, t
, comp_code
, value
,
6102 register_new_assert_for (op
, op
, comp_code
, value
, bb
, NULL
, si
);
6107 FOR_EACH_SSA_TREE_OPERAND (op
, stmt
, i
, SSA_OP_USE
)
6108 bitmap_set_bit (live
, SSA_NAME_VERSION (op
));
6109 FOR_EACH_SSA_TREE_OPERAND (op
, stmt
, i
, SSA_OP_DEF
)
6110 bitmap_clear_bit (live
, SSA_NAME_VERSION (op
));
6113 /* Traverse all PHI nodes in BB, updating live. */
6114 for (gphi_iterator si
= gsi_start_phis (bb
); !gsi_end_p (si
);
6117 use_operand_p arg_p
;
6119 gphi
*phi
= si
.phi ();
6120 tree res
= gimple_phi_result (phi
);
6122 if (virtual_operand_p (res
))
6125 FOR_EACH_PHI_ARG (arg_p
, phi
, i
, SSA_OP_USE
)
6127 tree arg
= USE_FROM_PTR (arg_p
);
6128 if (TREE_CODE (arg
) == SSA_NAME
)
6129 bitmap_set_bit (live
, SSA_NAME_VERSION (arg
));
6132 bitmap_clear_bit (live
, SSA_NAME_VERSION (res
));
6136 /* Do an RPO walk over the function computing SSA name liveness
6137 on-the-fly and deciding on assert expressions to insert. */
6140 find_assert_locations (void)
6142 int *rpo
= XNEWVEC (int, last_basic_block_for_fn (cfun
));
6143 int *bb_rpo
= XNEWVEC (int, last_basic_block_for_fn (cfun
));
6144 int *last_rpo
= XCNEWVEC (int, last_basic_block_for_fn (cfun
));
6147 live
= XCNEWVEC (sbitmap
, last_basic_block_for_fn (cfun
));
6148 rpo_cnt
= pre_and_rev_post_order_compute (NULL
, rpo
, false);
6149 for (i
= 0; i
< rpo_cnt
; ++i
)
6152 /* Pre-seed loop latch liveness from loop header PHI nodes. Due to
6153 the order we compute liveness and insert asserts we otherwise
6154 fail to insert asserts into the loop latch. */
6156 FOR_EACH_LOOP (loop
, 0)
6158 i
= loop
->latch
->index
;
6159 unsigned int j
= single_succ_edge (loop
->latch
)->dest_idx
;
6160 for (gphi_iterator gsi
= gsi_start_phis (loop
->header
);
6161 !gsi_end_p (gsi
); gsi_next (&gsi
))
6163 gphi
*phi
= gsi
.phi ();
6164 if (virtual_operand_p (gimple_phi_result (phi
)))
6166 tree arg
= gimple_phi_arg_def (phi
, j
);
6167 if (TREE_CODE (arg
) == SSA_NAME
)
6169 if (live
[i
] == NULL
)
6171 live
[i
] = sbitmap_alloc (num_ssa_names
);
6172 bitmap_clear (live
[i
]);
6174 bitmap_set_bit (live
[i
], SSA_NAME_VERSION (arg
));
6179 for (i
= rpo_cnt
- 1; i
>= 0; --i
)
6181 basic_block bb
= BASIC_BLOCK_FOR_FN (cfun
, rpo
[i
]);
6187 live
[rpo
[i
]] = sbitmap_alloc (num_ssa_names
);
6188 bitmap_clear (live
[rpo
[i
]]);
6191 /* Process BB and update the live information with uses in
6193 find_assert_locations_1 (bb
, live
[rpo
[i
]]);
6195 /* Merge liveness into the predecessor blocks and free it. */
6196 if (!bitmap_empty_p (live
[rpo
[i
]]))
6199 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
6201 int pred
= e
->src
->index
;
6202 if ((e
->flags
& EDGE_DFS_BACK
) || pred
== ENTRY_BLOCK
)
6207 live
[pred
] = sbitmap_alloc (num_ssa_names
);
6208 bitmap_clear (live
[pred
]);
6210 bitmap_ior (live
[pred
], live
[pred
], live
[rpo
[i
]]);
6212 if (bb_rpo
[pred
] < pred_rpo
)
6213 pred_rpo
= bb_rpo
[pred
];
6216 /* Record the RPO number of the last visited block that needs
6217 live information from this block. */
6218 last_rpo
[rpo
[i
]] = pred_rpo
;
6222 sbitmap_free (live
[rpo
[i
]]);
6223 live
[rpo
[i
]] = NULL
;
6226 /* We can free all successors live bitmaps if all their
6227 predecessors have been visited already. */
6228 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
6229 if (last_rpo
[e
->dest
->index
] == i
6230 && live
[e
->dest
->index
])
6232 sbitmap_free (live
[e
->dest
->index
]);
6233 live
[e
->dest
->index
] = NULL
;
6238 XDELETEVEC (bb_rpo
);
6239 XDELETEVEC (last_rpo
);
6240 for (i
= 0; i
< last_basic_block_for_fn (cfun
); ++i
)
6242 sbitmap_free (live
[i
]);
6246 /* Create an ASSERT_EXPR for NAME and insert it in the location
6247 indicated by LOC. Return true if we made any edge insertions. */
6250 process_assert_insertions_for (tree name
, assert_locus
*loc
)
6252 /* Build the comparison expression NAME_i COMP_CODE VAL. */
6255 gimple
*assert_stmt
;
6259 /* If we have X <=> X do not insert an assert expr for that. */
6260 if (loc
->expr
== loc
->val
)
6263 cond
= build2 (loc
->comp_code
, boolean_type_node
, loc
->expr
, loc
->val
);
6264 assert_stmt
= build_assert_expr_for (cond
, name
);
6267 /* We have been asked to insert the assertion on an edge. This
6268 is used only by COND_EXPR and SWITCH_EXPR assertions. */
6269 gcc_checking_assert (gimple_code (gsi_stmt (loc
->si
)) == GIMPLE_COND
6270 || (gimple_code (gsi_stmt (loc
->si
))
6273 gsi_insert_on_edge (loc
->e
, assert_stmt
);
6277 /* Otherwise, we can insert right after LOC->SI iff the
6278 statement must not be the last statement in the block. */
6279 stmt
= gsi_stmt (loc
->si
);
6280 if (!stmt_ends_bb_p (stmt
))
6282 gsi_insert_after (&loc
->si
, assert_stmt
, GSI_SAME_STMT
);
6286 /* If STMT must be the last statement in BB, we can only insert new
6287 assertions on the non-abnormal edge out of BB. Note that since
6288 STMT is not control flow, there may only be one non-abnormal edge
6290 FOR_EACH_EDGE (e
, ei
, loc
->bb
->succs
)
6291 if (!(e
->flags
& EDGE_ABNORMAL
))
6293 gsi_insert_on_edge (e
, assert_stmt
);
6301 /* Process all the insertions registered for every name N_i registered
6302 in NEED_ASSERT_FOR. The list of assertions to be inserted are
6303 found in ASSERTS_FOR[i]. */
6306 process_assert_insertions (void)
6310 bool update_edges_p
= false;
6311 int num_asserts
= 0;
6313 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6314 dump_all_asserts (dump_file
);
6316 EXECUTE_IF_SET_IN_BITMAP (need_assert_for
, 0, i
, bi
)
6318 assert_locus
*loc
= asserts_for
[i
];
6323 assert_locus
*next
= loc
->next
;
6324 update_edges_p
|= process_assert_insertions_for (ssa_name (i
), loc
);
6332 gsi_commit_edge_inserts ();
6334 statistics_counter_event (cfun
, "Number of ASSERT_EXPR expressions inserted",
6339 /* Traverse the flowgraph looking for conditional jumps to insert range
6340 expressions. These range expressions are meant to provide information
6341 to optimizations that need to reason in terms of value ranges. They
6342 will not be expanded into RTL. For instance, given:
6351 this pass will transform the code into:
6357 x = ASSERT_EXPR <x, x < y>
6362 y = ASSERT_EXPR <y, x >= y>
6366 The idea is that once copy and constant propagation have run, other
6367 optimizations will be able to determine what ranges of values can 'x'
6368 take in different paths of the code, simply by checking the reaching
6369 definition of 'x'. */
6372 insert_range_assertions (void)
6374 need_assert_for
= BITMAP_ALLOC (NULL
);
6375 asserts_for
= XCNEWVEC (assert_locus
*, num_ssa_names
);
6377 calculate_dominance_info (CDI_DOMINATORS
);
6379 find_assert_locations ();
6380 if (!bitmap_empty_p (need_assert_for
))
6382 process_assert_insertions ();
6383 update_ssa (TODO_update_ssa_no_phi
);
6386 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6388 fprintf (dump_file
, "\nSSA form after inserting ASSERT_EXPRs\n");
6389 dump_function_to_file (current_function_decl
, dump_file
, dump_flags
);
6393 BITMAP_FREE (need_assert_for
);
6396 /* Checks one ARRAY_REF in REF, located at LOCUS. Ignores flexible arrays
6397 and "struct" hacks. If VRP can determine that the
6398 array subscript is a constant, check if it is outside valid
6399 range. If the array subscript is a RANGE, warn if it is
6400 non-overlapping with valid range.
6401 IGNORE_OFF_BY_ONE is true if the ARRAY_REF is inside a ADDR_EXPR. */
6404 check_array_ref (location_t location
, tree ref
, bool ignore_off_by_one
)
6406 value_range
*vr
= NULL
;
6407 tree low_sub
, up_sub
;
6408 tree low_bound
, up_bound
, up_bound_p1
;
6411 if (TREE_NO_WARNING (ref
))
6414 low_sub
= up_sub
= TREE_OPERAND (ref
, 1);
6415 up_bound
= array_ref_up_bound (ref
);
6417 /* Can not check flexible arrays. */
6419 || TREE_CODE (up_bound
) != INTEGER_CST
)
6422 /* Accesses to trailing arrays via pointers may access storage
6423 beyond the types array bounds. */
6424 base
= get_base_address (ref
);
6425 if ((warn_array_bounds
< 2)
6426 && base
&& TREE_CODE (base
) == MEM_REF
)
6428 tree cref
, next
= NULL_TREE
;
6430 if (TREE_CODE (TREE_OPERAND (ref
, 0)) != COMPONENT_REF
)
6433 cref
= TREE_OPERAND (ref
, 0);
6434 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (cref
, 0))) == RECORD_TYPE
)
6435 for (next
= DECL_CHAIN (TREE_OPERAND (cref
, 1));
6436 next
&& TREE_CODE (next
) != FIELD_DECL
;
6437 next
= DECL_CHAIN (next
))
6440 /* If this is the last field in a struct type or a field in a
6441 union type do not warn. */
6446 low_bound
= array_ref_low_bound (ref
);
6447 up_bound_p1
= int_const_binop (PLUS_EXPR
, up_bound
,
6448 build_int_cst (TREE_TYPE (up_bound
), 1));
6451 if (tree_int_cst_equal (low_bound
, up_bound_p1
))
6453 warning_at (location
, OPT_Warray_bounds
,
6454 "array subscript is above array bounds");
6455 TREE_NO_WARNING (ref
) = 1;
6458 if (TREE_CODE (low_sub
) == SSA_NAME
)
6460 vr
= get_value_range (low_sub
);
6461 if (vr
->type
== VR_RANGE
|| vr
->type
== VR_ANTI_RANGE
)
6463 low_sub
= vr
->type
== VR_RANGE
? vr
->max
: vr
->min
;
6464 up_sub
= vr
->type
== VR_RANGE
? vr
->min
: vr
->max
;
6468 if (vr
&& vr
->type
== VR_ANTI_RANGE
)
6470 if (TREE_CODE (up_sub
) == INTEGER_CST
6471 && (ignore_off_by_one
6472 ? tree_int_cst_lt (up_bound
, up_sub
)
6473 : tree_int_cst_le (up_bound
, up_sub
))
6474 && TREE_CODE (low_sub
) == INTEGER_CST
6475 && tree_int_cst_le (low_sub
, low_bound
))
6477 warning_at (location
, OPT_Warray_bounds
,
6478 "array subscript is outside array bounds");
6479 TREE_NO_WARNING (ref
) = 1;
6482 else if (TREE_CODE (up_sub
) == INTEGER_CST
6483 && (ignore_off_by_one
6484 ? !tree_int_cst_le (up_sub
, up_bound_p1
)
6485 : !tree_int_cst_le (up_sub
, up_bound
)))
6487 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6489 fprintf (dump_file
, "Array bound warning for ");
6490 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, ref
);
6491 fprintf (dump_file
, "\n");
6493 warning_at (location
, OPT_Warray_bounds
,
6494 "array subscript is above array bounds");
6495 TREE_NO_WARNING (ref
) = 1;
6497 else if (TREE_CODE (low_sub
) == INTEGER_CST
6498 && tree_int_cst_lt (low_sub
, low_bound
))
6500 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6502 fprintf (dump_file
, "Array bound warning for ");
6503 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, ref
);
6504 fprintf (dump_file
, "\n");
6506 warning_at (location
, OPT_Warray_bounds
,
6507 "array subscript is below array bounds");
6508 TREE_NO_WARNING (ref
) = 1;
6512 /* Searches if the expr T, located at LOCATION computes
6513 address of an ARRAY_REF, and call check_array_ref on it. */
6516 search_for_addr_array (tree t
, location_t location
)
6518 /* Check each ARRAY_REFs in the reference chain. */
6521 if (TREE_CODE (t
) == ARRAY_REF
)
6522 check_array_ref (location
, t
, true /*ignore_off_by_one*/);
6524 t
= TREE_OPERAND (t
, 0);
6526 while (handled_component_p (t
));
6528 if (TREE_CODE (t
) == MEM_REF
6529 && TREE_CODE (TREE_OPERAND (t
, 0)) == ADDR_EXPR
6530 && !TREE_NO_WARNING (t
))
6532 tree tem
= TREE_OPERAND (TREE_OPERAND (t
, 0), 0);
6533 tree low_bound
, up_bound
, el_sz
;
6535 if (TREE_CODE (TREE_TYPE (tem
)) != ARRAY_TYPE
6536 || TREE_CODE (TREE_TYPE (TREE_TYPE (tem
))) == ARRAY_TYPE
6537 || !TYPE_DOMAIN (TREE_TYPE (tem
)))
6540 low_bound
= TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (tem
)));
6541 up_bound
= TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (tem
)));
6542 el_sz
= TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (tem
)));
6544 || TREE_CODE (low_bound
) != INTEGER_CST
6546 || TREE_CODE (up_bound
) != INTEGER_CST
6548 || TREE_CODE (el_sz
) != INTEGER_CST
)
6551 idx
= mem_ref_offset (t
);
6552 idx
= wi::sdiv_trunc (idx
, wi::to_offset (el_sz
));
6553 if (wi::lts_p (idx
, 0))
6555 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6557 fprintf (dump_file
, "Array bound warning for ");
6558 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, t
);
6559 fprintf (dump_file
, "\n");
6561 warning_at (location
, OPT_Warray_bounds
,
6562 "array subscript is below array bounds");
6563 TREE_NO_WARNING (t
) = 1;
6565 else if (wi::gts_p (idx
, (wi::to_offset (up_bound
)
6566 - wi::to_offset (low_bound
) + 1)))
6568 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6570 fprintf (dump_file
, "Array bound warning for ");
6571 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, t
);
6572 fprintf (dump_file
, "\n");
6574 warning_at (location
, OPT_Warray_bounds
,
6575 "array subscript is above array bounds");
6576 TREE_NO_WARNING (t
) = 1;
6581 /* walk_tree() callback that checks if *TP is
6582 an ARRAY_REF inside an ADDR_EXPR (in which an array
6583 subscript one outside the valid range is allowed). Call
6584 check_array_ref for each ARRAY_REF found. The location is
6588 check_array_bounds (tree
*tp
, int *walk_subtree
, void *data
)
6591 struct walk_stmt_info
*wi
= (struct walk_stmt_info
*) data
;
6592 location_t location
;
6594 if (EXPR_HAS_LOCATION (t
))
6595 location
= EXPR_LOCATION (t
);
6598 location_t
*locp
= (location_t
*) wi
->info
;
6602 *walk_subtree
= TRUE
;
6604 if (TREE_CODE (t
) == ARRAY_REF
)
6605 check_array_ref (location
, t
, false /*ignore_off_by_one*/);
6607 else if (TREE_CODE (t
) == ADDR_EXPR
)
6609 search_for_addr_array (t
, location
);
6610 *walk_subtree
= FALSE
;
6616 /* Walk over all statements of all reachable BBs and call check_array_bounds
6620 check_all_array_refs (void)
6623 gimple_stmt_iterator si
;
6625 FOR_EACH_BB_FN (bb
, cfun
)
6629 bool executable
= false;
6631 /* Skip blocks that were found to be unreachable. */
6632 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
6633 executable
|= !!(e
->flags
& EDGE_EXECUTABLE
);
6637 for (si
= gsi_start_bb (bb
); !gsi_end_p (si
); gsi_next (&si
))
6639 gimple
*stmt
= gsi_stmt (si
);
6640 struct walk_stmt_info wi
;
6641 if (!gimple_has_location (stmt
)
6642 || is_gimple_debug (stmt
))
6645 memset (&wi
, 0, sizeof (wi
));
6647 location_t loc
= gimple_location (stmt
);
6650 walk_gimple_op (gsi_stmt (si
),
6657 /* Return true if all imm uses of VAR are either in STMT, or
6658 feed (optionally through a chain of single imm uses) GIMPLE_COND
6659 in basic block COND_BB. */
6662 all_imm_uses_in_stmt_or_feed_cond (tree var
, gimple
*stmt
, basic_block cond_bb
)
6664 use_operand_p use_p
, use2_p
;
6665 imm_use_iterator iter
;
6667 FOR_EACH_IMM_USE_FAST (use_p
, iter
, var
)
6668 if (USE_STMT (use_p
) != stmt
)
6670 gimple
*use_stmt
= USE_STMT (use_p
), *use_stmt2
;
6671 if (is_gimple_debug (use_stmt
))
6673 while (is_gimple_assign (use_stmt
)
6674 && TREE_CODE (gimple_assign_lhs (use_stmt
)) == SSA_NAME
6675 && single_imm_use (gimple_assign_lhs (use_stmt
),
6676 &use2_p
, &use_stmt2
))
6677 use_stmt
= use_stmt2
;
6678 if (gimple_code (use_stmt
) != GIMPLE_COND
6679 || gimple_bb (use_stmt
) != cond_bb
)
6692 __builtin_unreachable ();
6694 x_5 = ASSERT_EXPR <x_3, ...>;
6695 If x_3 has no other immediate uses (checked by caller),
6696 var is the x_3 var from ASSERT_EXPR, we can clear low 5 bits
6697 from the non-zero bitmask. */
6700 maybe_set_nonzero_bits (basic_block bb
, tree var
)
6702 edge e
= single_pred_edge (bb
);
6703 basic_block cond_bb
= e
->src
;
6704 gimple
*stmt
= last_stmt (cond_bb
);
6708 || gimple_code (stmt
) != GIMPLE_COND
6709 || gimple_cond_code (stmt
) != ((e
->flags
& EDGE_TRUE_VALUE
)
6710 ? EQ_EXPR
: NE_EXPR
)
6711 || TREE_CODE (gimple_cond_lhs (stmt
)) != SSA_NAME
6712 || !integer_zerop (gimple_cond_rhs (stmt
)))
6715 stmt
= SSA_NAME_DEF_STMT (gimple_cond_lhs (stmt
));
6716 if (!is_gimple_assign (stmt
)
6717 || gimple_assign_rhs_code (stmt
) != BIT_AND_EXPR
6718 || TREE_CODE (gimple_assign_rhs2 (stmt
)) != INTEGER_CST
)
6720 if (gimple_assign_rhs1 (stmt
) != var
)
6724 if (TREE_CODE (gimple_assign_rhs1 (stmt
)) != SSA_NAME
)
6726 stmt2
= SSA_NAME_DEF_STMT (gimple_assign_rhs1 (stmt
));
6727 if (!gimple_assign_cast_p (stmt2
)
6728 || gimple_assign_rhs1 (stmt2
) != var
6729 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (stmt2
))
6730 || (TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (stmt
)))
6731 != TYPE_PRECISION (TREE_TYPE (var
))))
6734 cst
= gimple_assign_rhs2 (stmt
);
6735 set_nonzero_bits (var
, wi::bit_and_not (get_nonzero_bits (var
), cst
));
6738 /* Convert range assertion expressions into the implied copies and
6739 copy propagate away the copies. Doing the trivial copy propagation
6740 here avoids the need to run the full copy propagation pass after
6743 FIXME, this will eventually lead to copy propagation removing the
6744 names that had useful range information attached to them. For
6745 instance, if we had the assertion N_i = ASSERT_EXPR <N_j, N_j > 3>,
6746 then N_i will have the range [3, +INF].
6748 However, by converting the assertion into the implied copy
6749 operation N_i = N_j, we will then copy-propagate N_j into the uses
6750 of N_i and lose the range information. We may want to hold on to
6751 ASSERT_EXPRs a little while longer as the ranges could be used in
6752 things like jump threading.
6754 The problem with keeping ASSERT_EXPRs around is that passes after
6755 VRP need to handle them appropriately.
6757 Another approach would be to make the range information a first
6758 class property of the SSA_NAME so that it can be queried from
6759 any pass. This is made somewhat more complex by the need for
6760 multiple ranges to be associated with one SSA_NAME. */
6763 remove_range_assertions (void)
6766 gimple_stmt_iterator si
;
6767 /* 1 if looking at ASSERT_EXPRs immediately at the beginning of
6768 a basic block preceeded by GIMPLE_COND branching to it and
6769 __builtin_trap, -1 if not yet checked, 0 otherwise. */
6772 /* Note that the BSI iterator bump happens at the bottom of the
6773 loop and no bump is necessary if we're removing the statement
6774 referenced by the current BSI. */
6775 FOR_EACH_BB_FN (bb
, cfun
)
6776 for (si
= gsi_after_labels (bb
), is_unreachable
= -1; !gsi_end_p (si
);)
6778 gimple
*stmt
= gsi_stmt (si
);
6781 if (is_gimple_assign (stmt
)
6782 && gimple_assign_rhs_code (stmt
) == ASSERT_EXPR
)
6784 tree lhs
= gimple_assign_lhs (stmt
);
6785 tree rhs
= gimple_assign_rhs1 (stmt
);
6787 use_operand_p use_p
;
6788 imm_use_iterator iter
;
6790 var
= ASSERT_EXPR_VAR (rhs
);
6791 gcc_assert (TREE_CODE (var
) == SSA_NAME
);
6793 if (!POINTER_TYPE_P (TREE_TYPE (lhs
))
6794 && SSA_NAME_RANGE_INFO (lhs
))
6796 if (is_unreachable
== -1)
6799 if (single_pred_p (bb
)
6800 && assert_unreachable_fallthru_edge_p
6801 (single_pred_edge (bb
)))
6805 if (x_7 >= 10 && x_7 < 20)
6806 __builtin_unreachable ();
6807 x_8 = ASSERT_EXPR <x_7, ...>;
6808 if the only uses of x_7 are in the ASSERT_EXPR and
6809 in the condition. In that case, we can copy the
6810 range info from x_8 computed in this pass also
6813 && all_imm_uses_in_stmt_or_feed_cond (var
, stmt
,
6816 set_range_info (var
, SSA_NAME_RANGE_TYPE (lhs
),
6817 SSA_NAME_RANGE_INFO (lhs
)->get_min (),
6818 SSA_NAME_RANGE_INFO (lhs
)->get_max ());
6819 maybe_set_nonzero_bits (bb
, var
);
6823 /* Propagate the RHS into every use of the LHS. */
6824 FOR_EACH_IMM_USE_STMT (use_stmt
, iter
, lhs
)
6825 FOR_EACH_IMM_USE_ON_STMT (use_p
, iter
)
6826 SET_USE (use_p
, var
);
6828 /* And finally, remove the copy, it is not needed. */
6829 gsi_remove (&si
, true);
6830 release_defs (stmt
);
6834 if (!is_gimple_debug (gsi_stmt (si
)))
6842 /* Return true if STMT is interesting for VRP. */
6845 stmt_interesting_for_vrp (gimple
*stmt
)
6847 if (gimple_code (stmt
) == GIMPLE_PHI
)
6849 tree res
= gimple_phi_result (stmt
);
6850 return (!virtual_operand_p (res
)
6851 && (INTEGRAL_TYPE_P (TREE_TYPE (res
))
6852 || POINTER_TYPE_P (TREE_TYPE (res
))));
6854 else if (is_gimple_assign (stmt
) || is_gimple_call (stmt
))
6856 tree lhs
= gimple_get_lhs (stmt
);
6858 /* In general, assignments with virtual operands are not useful
6859 for deriving ranges, with the obvious exception of calls to
6860 builtin functions. */
6861 if (lhs
&& TREE_CODE (lhs
) == SSA_NAME
6862 && (INTEGRAL_TYPE_P (TREE_TYPE (lhs
))
6863 || POINTER_TYPE_P (TREE_TYPE (lhs
)))
6864 && (is_gimple_call (stmt
)
6865 || !gimple_vuse (stmt
)))
6867 else if (is_gimple_call (stmt
) && gimple_call_internal_p (stmt
))
6868 switch (gimple_call_internal_fn (stmt
))
6870 case IFN_ADD_OVERFLOW
:
6871 case IFN_SUB_OVERFLOW
:
6872 case IFN_MUL_OVERFLOW
:
6873 /* These internal calls return _Complex integer type,
6874 but are interesting to VRP nevertheless. */
6875 if (lhs
&& TREE_CODE (lhs
) == SSA_NAME
)
6882 else if (gimple_code (stmt
) == GIMPLE_COND
6883 || gimple_code (stmt
) == GIMPLE_SWITCH
)
6890 /* Initialize local data structures for VRP. */
6893 vrp_initialize (void)
6897 values_propagated
= false;
6898 num_vr_values
= num_ssa_names
;
6899 vr_value
= XCNEWVEC (value_range
*, num_vr_values
);
6900 vr_phi_edge_counts
= XCNEWVEC (int, num_ssa_names
);
6902 FOR_EACH_BB_FN (bb
, cfun
)
6904 for (gphi_iterator si
= gsi_start_phis (bb
); !gsi_end_p (si
);
6907 gphi
*phi
= si
.phi ();
6908 if (!stmt_interesting_for_vrp (phi
))
6910 tree lhs
= PHI_RESULT (phi
);
6911 set_value_range_to_varying (get_value_range (lhs
));
6912 prop_set_simulate_again (phi
, false);
6915 prop_set_simulate_again (phi
, true);
6918 for (gimple_stmt_iterator si
= gsi_start_bb (bb
); !gsi_end_p (si
);
6921 gimple
*stmt
= gsi_stmt (si
);
6923 /* If the statement is a control insn, then we do not
6924 want to avoid simulating the statement once. Failure
6925 to do so means that those edges will never get added. */
6926 if (stmt_ends_bb_p (stmt
))
6927 prop_set_simulate_again (stmt
, true);
6928 else if (!stmt_interesting_for_vrp (stmt
))
6932 FOR_EACH_SSA_TREE_OPERAND (def
, stmt
, i
, SSA_OP_DEF
)
6933 set_value_range_to_varying (get_value_range (def
));
6934 prop_set_simulate_again (stmt
, false);
6937 prop_set_simulate_again (stmt
, true);
6942 /* Return the singleton value-range for NAME or NAME. */
6945 vrp_valueize (tree name
)
6947 if (TREE_CODE (name
) == SSA_NAME
)
6949 value_range
*vr
= get_value_range (name
);
6950 if (vr
->type
== VR_RANGE
6951 && (vr
->min
== vr
->max
6952 || operand_equal_p (vr
->min
, vr
->max
, 0)))
6958 /* Return the singleton value-range for NAME if that is a constant
6959 but signal to not follow SSA edges. */
6962 vrp_valueize_1 (tree name
)
6964 if (TREE_CODE (name
) == SSA_NAME
)
6966 /* If the definition may be simulated again we cannot follow
6967 this SSA edge as the SSA propagator does not necessarily
6968 re-visit the use. */
6969 gimple
*def_stmt
= SSA_NAME_DEF_STMT (name
);
6970 if (!gimple_nop_p (def_stmt
)
6971 && prop_simulate_again_p (def_stmt
))
6973 value_range
*vr
= get_value_range (name
);
6974 if (range_int_cst_singleton_p (vr
))
6980 /* Visit assignment STMT. If it produces an interesting range, record
6981 the SSA name in *OUTPUT_P. */
6983 static enum ssa_prop_result
6984 vrp_visit_assignment_or_call (gimple
*stmt
, tree
*output_p
)
6988 enum gimple_code code
= gimple_code (stmt
);
6989 lhs
= gimple_get_lhs (stmt
);
6991 /* We only keep track of ranges in integral and pointer types. */
6992 if (TREE_CODE (lhs
) == SSA_NAME
6993 && ((INTEGRAL_TYPE_P (TREE_TYPE (lhs
))
6994 /* It is valid to have NULL MIN/MAX values on a type. See
6995 build_range_type. */
6996 && TYPE_MIN_VALUE (TREE_TYPE (lhs
))
6997 && TYPE_MAX_VALUE (TREE_TYPE (lhs
)))
6998 || POINTER_TYPE_P (TREE_TYPE (lhs
))))
7000 value_range new_vr
= VR_INITIALIZER
;
7002 /* Try folding the statement to a constant first. */
7003 tree tem
= gimple_fold_stmt_to_constant_1 (stmt
, vrp_valueize
,
7005 if (tem
&& is_gimple_min_invariant (tem
))
7006 set_value_range_to_value (&new_vr
, tem
, NULL
);
7007 /* Then dispatch to value-range extracting functions. */
7008 else if (code
== GIMPLE_CALL
)
7009 extract_range_basic (&new_vr
, stmt
);
7011 extract_range_from_assignment (&new_vr
, as_a
<gassign
*> (stmt
));
7013 if (update_value_range (lhs
, &new_vr
))
7017 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7019 fprintf (dump_file
, "Found new range for ");
7020 print_generic_expr (dump_file
, lhs
, 0);
7021 fprintf (dump_file
, ": ");
7022 dump_value_range (dump_file
, &new_vr
);
7023 fprintf (dump_file
, "\n");
7026 if (new_vr
.type
== VR_VARYING
)
7027 return SSA_PROP_VARYING
;
7029 return SSA_PROP_INTERESTING
;
7032 return SSA_PROP_NOT_INTERESTING
;
7034 else if (is_gimple_call (stmt
) && gimple_call_internal_p (stmt
))
7035 switch (gimple_call_internal_fn (stmt
))
7037 case IFN_ADD_OVERFLOW
:
7038 case IFN_SUB_OVERFLOW
:
7039 case IFN_MUL_OVERFLOW
:
7040 /* These internal calls return _Complex integer type,
7041 which VRP does not track, but the immediate uses
7042 thereof might be interesting. */
7043 if (lhs
&& TREE_CODE (lhs
) == SSA_NAME
)
7045 imm_use_iterator iter
;
7046 use_operand_p use_p
;
7047 enum ssa_prop_result res
= SSA_PROP_VARYING
;
7049 set_value_range_to_varying (get_value_range (lhs
));
7051 FOR_EACH_IMM_USE_FAST (use_p
, iter
, lhs
)
7053 gimple
*use_stmt
= USE_STMT (use_p
);
7054 if (!is_gimple_assign (use_stmt
))
7056 enum tree_code rhs_code
= gimple_assign_rhs_code (use_stmt
);
7057 if (rhs_code
!= REALPART_EXPR
&& rhs_code
!= IMAGPART_EXPR
)
7059 tree rhs1
= gimple_assign_rhs1 (use_stmt
);
7060 tree use_lhs
= gimple_assign_lhs (use_stmt
);
7061 if (TREE_CODE (rhs1
) != rhs_code
7062 || TREE_OPERAND (rhs1
, 0) != lhs
7063 || TREE_CODE (use_lhs
) != SSA_NAME
7064 || !stmt_interesting_for_vrp (use_stmt
)
7065 || (!INTEGRAL_TYPE_P (TREE_TYPE (use_lhs
))
7066 || !TYPE_MIN_VALUE (TREE_TYPE (use_lhs
))
7067 || !TYPE_MAX_VALUE (TREE_TYPE (use_lhs
))))
7070 /* If there is a change in the value range for any of the
7071 REALPART_EXPR/IMAGPART_EXPR immediate uses, return
7072 SSA_PROP_INTERESTING. If there are any REALPART_EXPR
7073 or IMAGPART_EXPR immediate uses, but none of them have
7074 a change in their value ranges, return
7075 SSA_PROP_NOT_INTERESTING. If there are no
7076 {REAL,IMAG}PART_EXPR uses at all,
7077 return SSA_PROP_VARYING. */
7078 value_range new_vr
= VR_INITIALIZER
;
7079 extract_range_basic (&new_vr
, use_stmt
);
7080 value_range
*old_vr
= get_value_range (use_lhs
);
7081 if (old_vr
->type
!= new_vr
.type
7082 || !vrp_operand_equal_p (old_vr
->min
, new_vr
.min
)
7083 || !vrp_operand_equal_p (old_vr
->max
, new_vr
.max
)
7084 || !vrp_bitmap_equal_p (old_vr
->equiv
, new_vr
.equiv
))
7085 res
= SSA_PROP_INTERESTING
;
7087 res
= SSA_PROP_NOT_INTERESTING
;
7088 BITMAP_FREE (new_vr
.equiv
);
7089 if (res
== SSA_PROP_INTERESTING
)
7103 /* Every other statement produces no useful ranges. */
7104 FOR_EACH_SSA_TREE_OPERAND (def
, stmt
, iter
, SSA_OP_DEF
)
7105 set_value_range_to_varying (get_value_range (def
));
7107 return SSA_PROP_VARYING
;
7110 /* Helper that gets the value range of the SSA_NAME with version I
7111 or a symbolic range containing the SSA_NAME only if the value range
7112 is varying or undefined. */
7114 static inline value_range
7115 get_vr_for_comparison (int i
)
7117 value_range vr
= *get_value_range (ssa_name (i
));
7119 /* If name N_i does not have a valid range, use N_i as its own
7120 range. This allows us to compare against names that may
7121 have N_i in their ranges. */
7122 if (vr
.type
== VR_VARYING
|| vr
.type
== VR_UNDEFINED
)
7125 vr
.min
= ssa_name (i
);
7126 vr
.max
= ssa_name (i
);
7132 /* Compare all the value ranges for names equivalent to VAR with VAL
7133 using comparison code COMP. Return the same value returned by
7134 compare_range_with_value, including the setting of
7135 *STRICT_OVERFLOW_P. */
7138 compare_name_with_value (enum tree_code comp
, tree var
, tree val
,
7139 bool *strict_overflow_p
)
7145 int used_strict_overflow
;
7147 value_range equiv_vr
;
7149 /* Get the set of equivalences for VAR. */
7150 e
= get_value_range (var
)->equiv
;
7152 /* Start at -1. Set it to 0 if we do a comparison without relying
7153 on overflow, or 1 if all comparisons rely on overflow. */
7154 used_strict_overflow
= -1;
7156 /* Compare vars' value range with val. */
7157 equiv_vr
= get_vr_for_comparison (SSA_NAME_VERSION (var
));
7159 retval
= compare_range_with_value (comp
, &equiv_vr
, val
, &sop
);
7161 used_strict_overflow
= sop
? 1 : 0;
7163 /* If the equiv set is empty we have done all work we need to do. */
7167 && used_strict_overflow
> 0)
7168 *strict_overflow_p
= true;
7172 EXECUTE_IF_SET_IN_BITMAP (e
, 0, i
, bi
)
7174 equiv_vr
= get_vr_for_comparison (i
);
7176 t
= compare_range_with_value (comp
, &equiv_vr
, val
, &sop
);
7179 /* If we get different answers from different members
7180 of the equivalence set this check must be in a dead
7181 code region. Folding it to a trap representation
7182 would be correct here. For now just return don't-know. */
7192 used_strict_overflow
= 0;
7193 else if (used_strict_overflow
< 0)
7194 used_strict_overflow
= 1;
7199 && used_strict_overflow
> 0)
7200 *strict_overflow_p
= true;
7206 /* Given a comparison code COMP and names N1 and N2, compare all the
7207 ranges equivalent to N1 against all the ranges equivalent to N2
7208 to determine the value of N1 COMP N2. Return the same value
7209 returned by compare_ranges. Set *STRICT_OVERFLOW_P to indicate
7210 whether we relied on an overflow infinity in the comparison. */
7214 compare_names (enum tree_code comp
, tree n1
, tree n2
,
7215 bool *strict_overflow_p
)
7219 bitmap_iterator bi1
, bi2
;
7221 int used_strict_overflow
;
7222 static bitmap_obstack
*s_obstack
= NULL
;
7223 static bitmap s_e1
= NULL
, s_e2
= NULL
;
7225 /* Compare the ranges of every name equivalent to N1 against the
7226 ranges of every name equivalent to N2. */
7227 e1
= get_value_range (n1
)->equiv
;
7228 e2
= get_value_range (n2
)->equiv
;
7230 /* Use the fake bitmaps if e1 or e2 are not available. */
7231 if (s_obstack
== NULL
)
7233 s_obstack
= XNEW (bitmap_obstack
);
7234 bitmap_obstack_initialize (s_obstack
);
7235 s_e1
= BITMAP_ALLOC (s_obstack
);
7236 s_e2
= BITMAP_ALLOC (s_obstack
);
7243 /* Add N1 and N2 to their own set of equivalences to avoid
7244 duplicating the body of the loop just to check N1 and N2
7246 bitmap_set_bit (e1
, SSA_NAME_VERSION (n1
));
7247 bitmap_set_bit (e2
, SSA_NAME_VERSION (n2
));
7249 /* If the equivalence sets have a common intersection, then the two
7250 names can be compared without checking their ranges. */
7251 if (bitmap_intersect_p (e1
, e2
))
7253 bitmap_clear_bit (e1
, SSA_NAME_VERSION (n1
));
7254 bitmap_clear_bit (e2
, SSA_NAME_VERSION (n2
));
7256 return (comp
== EQ_EXPR
|| comp
== GE_EXPR
|| comp
== LE_EXPR
)
7258 : boolean_false_node
;
7261 /* Start at -1. Set it to 0 if we do a comparison without relying
7262 on overflow, or 1 if all comparisons rely on overflow. */
7263 used_strict_overflow
= -1;
7265 /* Otherwise, compare all the equivalent ranges. First, add N1 and
7266 N2 to their own set of equivalences to avoid duplicating the body
7267 of the loop just to check N1 and N2 ranges. */
7268 EXECUTE_IF_SET_IN_BITMAP (e1
, 0, i1
, bi1
)
7270 value_range vr1
= get_vr_for_comparison (i1
);
7272 t
= retval
= NULL_TREE
;
7273 EXECUTE_IF_SET_IN_BITMAP (e2
, 0, i2
, bi2
)
7277 value_range vr2
= get_vr_for_comparison (i2
);
7279 t
= compare_ranges (comp
, &vr1
, &vr2
, &sop
);
7282 /* If we get different answers from different members
7283 of the equivalence set this check must be in a dead
7284 code region. Folding it to a trap representation
7285 would be correct here. For now just return don't-know. */
7289 bitmap_clear_bit (e1
, SSA_NAME_VERSION (n1
));
7290 bitmap_clear_bit (e2
, SSA_NAME_VERSION (n2
));
7296 used_strict_overflow
= 0;
7297 else if (used_strict_overflow
< 0)
7298 used_strict_overflow
= 1;
7304 bitmap_clear_bit (e1
, SSA_NAME_VERSION (n1
));
7305 bitmap_clear_bit (e2
, SSA_NAME_VERSION (n2
));
7306 if (used_strict_overflow
> 0)
7307 *strict_overflow_p
= true;
7312 /* None of the equivalent ranges are useful in computing this
7314 bitmap_clear_bit (e1
, SSA_NAME_VERSION (n1
));
7315 bitmap_clear_bit (e2
, SSA_NAME_VERSION (n2
));
7319 /* Helper function for vrp_evaluate_conditional_warnv & other
7323 vrp_evaluate_conditional_warnv_with_ops_using_ranges (enum tree_code code
,
7325 bool * strict_overflow_p
)
7327 value_range
*vr0
, *vr1
;
7329 vr0
= (TREE_CODE (op0
) == SSA_NAME
) ? get_value_range (op0
) : NULL
;
7330 vr1
= (TREE_CODE (op1
) == SSA_NAME
) ? get_value_range (op1
) : NULL
;
7332 tree res
= NULL_TREE
;
7334 res
= compare_ranges (code
, vr0
, vr1
, strict_overflow_p
);
7336 res
= compare_range_with_value (code
, vr0
, op1
, strict_overflow_p
);
7338 res
= (compare_range_with_value
7339 (swap_tree_comparison (code
), vr1
, op0
, strict_overflow_p
));
7343 /* Helper function for vrp_evaluate_conditional_warnv. */
7346 vrp_evaluate_conditional_warnv_with_ops (enum tree_code code
, tree op0
,
7347 tree op1
, bool use_equiv_p
,
7348 bool *strict_overflow_p
, bool *only_ranges
)
7352 *only_ranges
= true;
7354 /* We only deal with integral and pointer types. */
7355 if (!INTEGRAL_TYPE_P (TREE_TYPE (op0
))
7356 && !POINTER_TYPE_P (TREE_TYPE (op0
)))
7362 && (ret
= vrp_evaluate_conditional_warnv_with_ops_using_ranges
7363 (code
, op0
, op1
, strict_overflow_p
)))
7365 *only_ranges
= false;
7366 if (TREE_CODE (op0
) == SSA_NAME
&& TREE_CODE (op1
) == SSA_NAME
)
7367 return compare_names (code
, op0
, op1
, strict_overflow_p
);
7368 else if (TREE_CODE (op0
) == SSA_NAME
)
7369 return compare_name_with_value (code
, op0
, op1
, strict_overflow_p
);
7370 else if (TREE_CODE (op1
) == SSA_NAME
)
7371 return (compare_name_with_value
7372 (swap_tree_comparison (code
), op1
, op0
, strict_overflow_p
));
7375 return vrp_evaluate_conditional_warnv_with_ops_using_ranges (code
, op0
, op1
,
7380 /* Given (CODE OP0 OP1) within STMT, try to simplify it based on value range
7381 information. Return NULL if the conditional can not be evaluated.
7382 The ranges of all the names equivalent with the operands in COND
7383 will be used when trying to compute the value. If the result is
7384 based on undefined signed overflow, issue a warning if
7388 vrp_evaluate_conditional (tree_code code
, tree op0
, tree op1
, gimple
*stmt
)
7394 /* Some passes and foldings leak constants with overflow flag set
7395 into the IL. Avoid doing wrong things with these and bail out. */
7396 if ((TREE_CODE (op0
) == INTEGER_CST
7397 && TREE_OVERFLOW (op0
))
7398 || (TREE_CODE (op1
) == INTEGER_CST
7399 && TREE_OVERFLOW (op1
)))
7403 ret
= vrp_evaluate_conditional_warnv_with_ops (code
, op0
, op1
, true, &sop
,
7408 enum warn_strict_overflow_code wc
;
7409 const char* warnmsg
;
7411 if (is_gimple_min_invariant (ret
))
7413 wc
= WARN_STRICT_OVERFLOW_CONDITIONAL
;
7414 warnmsg
= G_("assuming signed overflow does not occur when "
7415 "simplifying conditional to constant");
7419 wc
= WARN_STRICT_OVERFLOW_COMPARISON
;
7420 warnmsg
= G_("assuming signed overflow does not occur when "
7421 "simplifying conditional");
7424 if (issue_strict_overflow_warning (wc
))
7426 location_t location
;
7428 if (!gimple_has_location (stmt
))
7429 location
= input_location
;
7431 location
= gimple_location (stmt
);
7432 warning_at (location
, OPT_Wstrict_overflow
, "%s", warnmsg
);
7436 if (warn_type_limits
7437 && ret
&& only_ranges
7438 && TREE_CODE_CLASS (code
) == tcc_comparison
7439 && TREE_CODE (op0
) == SSA_NAME
)
7441 /* If the comparison is being folded and the operand on the LHS
7442 is being compared against a constant value that is outside of
7443 the natural range of OP0's type, then the predicate will
7444 always fold regardless of the value of OP0. If -Wtype-limits
7445 was specified, emit a warning. */
7446 tree type
= TREE_TYPE (op0
);
7447 value_range
*vr0
= get_value_range (op0
);
7449 if (vr0
->type
== VR_RANGE
7450 && INTEGRAL_TYPE_P (type
)
7451 && vrp_val_is_min (vr0
->min
)
7452 && vrp_val_is_max (vr0
->max
)
7453 && is_gimple_min_invariant (op1
))
7455 location_t location
;
7457 if (!gimple_has_location (stmt
))
7458 location
= input_location
;
7460 location
= gimple_location (stmt
);
7462 warning_at (location
, OPT_Wtype_limits
,
7464 ? G_("comparison always false "
7465 "due to limited range of data type")
7466 : G_("comparison always true "
7467 "due to limited range of data type"));
7475 /* Visit conditional statement STMT. If we can determine which edge
7476 will be taken out of STMT's basic block, record it in
7477 *TAKEN_EDGE_P and return SSA_PROP_INTERESTING. Otherwise, return
7478 SSA_PROP_VARYING. */
7480 static enum ssa_prop_result
7481 vrp_visit_cond_stmt (gcond
*stmt
, edge
*taken_edge_p
)
7486 *taken_edge_p
= NULL
;
7488 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7493 fprintf (dump_file
, "\nVisiting conditional with predicate: ");
7494 print_gimple_stmt (dump_file
, stmt
, 0, 0);
7495 fprintf (dump_file
, "\nWith known ranges\n");
7497 FOR_EACH_SSA_TREE_OPERAND (use
, stmt
, i
, SSA_OP_USE
)
7499 fprintf (dump_file
, "\t");
7500 print_generic_expr (dump_file
, use
, 0);
7501 fprintf (dump_file
, ": ");
7502 dump_value_range (dump_file
, vr_value
[SSA_NAME_VERSION (use
)]);
7505 fprintf (dump_file
, "\n");
7508 /* Compute the value of the predicate COND by checking the known
7509 ranges of each of its operands.
7511 Note that we cannot evaluate all the equivalent ranges here
7512 because those ranges may not yet be final and with the current
7513 propagation strategy, we cannot determine when the value ranges
7514 of the names in the equivalence set have changed.
7516 For instance, given the following code fragment
7520 i_14 = ASSERT_EXPR <i_5, i_5 != 0>
7524 Assume that on the first visit to i_14, i_5 has the temporary
7525 range [8, 8] because the second argument to the PHI function is
7526 not yet executable. We derive the range ~[0, 0] for i_14 and the
7527 equivalence set { i_5 }. So, when we visit 'if (i_14 == 1)' for
7528 the first time, since i_14 is equivalent to the range [8, 8], we
7529 determine that the predicate is always false.
7531 On the next round of propagation, i_13 is determined to be
7532 VARYING, which causes i_5 to drop down to VARYING. So, another
7533 visit to i_14 is scheduled. In this second visit, we compute the
7534 exact same range and equivalence set for i_14, namely ~[0, 0] and
7535 { i_5 }. But we did not have the previous range for i_5
7536 registered, so vrp_visit_assignment thinks that the range for
7537 i_14 has not changed. Therefore, the predicate 'if (i_14 == 1)'
7538 is not visited again, which stops propagation from visiting
7539 statements in the THEN clause of that if().
7541 To properly fix this we would need to keep the previous range
7542 value for the names in the equivalence set. This way we would've
7543 discovered that from one visit to the other i_5 changed from
7544 range [8, 8] to VR_VARYING.
7546 However, fixing this apparent limitation may not be worth the
7547 additional checking. Testing on several code bases (GCC, DLV,
7548 MICO, TRAMP3D and SPEC2000) showed that doing this results in
7549 4 more predicates folded in SPEC. */
7552 val
= vrp_evaluate_conditional_warnv_with_ops (gimple_cond_code (stmt
),
7553 gimple_cond_lhs (stmt
),
7554 gimple_cond_rhs (stmt
),
7559 *taken_edge_p
= find_taken_edge (gimple_bb (stmt
), val
);
7562 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7564 "\nIgnoring predicate evaluation because "
7565 "it assumes that signed overflow is undefined");
7570 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7572 fprintf (dump_file
, "\nPredicate evaluates to: ");
7573 if (val
== NULL_TREE
)
7574 fprintf (dump_file
, "DON'T KNOW\n");
7576 print_generic_stmt (dump_file
, val
, 0);
7579 return (*taken_edge_p
) ? SSA_PROP_INTERESTING
: SSA_PROP_VARYING
;
7582 /* Searches the case label vector VEC for the index *IDX of the CASE_LABEL
7583 that includes the value VAL. The search is restricted to the range
7584 [START_IDX, n - 1] where n is the size of VEC.
7586 If there is a CASE_LABEL for VAL, its index is placed in IDX and true is
7589 If there is no CASE_LABEL for VAL and there is one that is larger than VAL,
7590 it is placed in IDX and false is returned.
7592 If VAL is larger than any CASE_LABEL, n is placed on IDX and false is
7596 find_case_label_index (gswitch
*stmt
, size_t start_idx
, tree val
, size_t *idx
)
7598 size_t n
= gimple_switch_num_labels (stmt
);
7601 /* Find case label for minimum of the value range or the next one.
7602 At each iteration we are searching in [low, high - 1]. */
7604 for (low
= start_idx
, high
= n
; high
!= low
; )
7608 /* Note that i != high, so we never ask for n. */
7609 size_t i
= (high
+ low
) / 2;
7610 t
= gimple_switch_label (stmt
, i
);
7612 /* Cache the result of comparing CASE_LOW and val. */
7613 cmp
= tree_int_cst_compare (CASE_LOW (t
), val
);
7617 /* Ranges cannot be empty. */
7626 if (CASE_HIGH (t
) != NULL
7627 && tree_int_cst_compare (CASE_HIGH (t
), val
) >= 0)
7639 /* Searches the case label vector VEC for the range of CASE_LABELs that is used
7640 for values between MIN and MAX. The first index is placed in MIN_IDX. The
7641 last index is placed in MAX_IDX. If the range of CASE_LABELs is empty
7642 then MAX_IDX < MIN_IDX.
7643 Returns true if the default label is not needed. */
7646 find_case_label_range (gswitch
*stmt
, tree min
, tree max
, size_t *min_idx
,
7650 bool min_take_default
= !find_case_label_index (stmt
, 1, min
, &i
);
7651 bool max_take_default
= !find_case_label_index (stmt
, i
, max
, &j
);
7655 && max_take_default
)
7657 /* Only the default case label reached.
7658 Return an empty range. */
7665 bool take_default
= min_take_default
|| max_take_default
;
7669 if (max_take_default
)
7672 /* If the case label range is continuous, we do not need
7673 the default case label. Verify that. */
7674 high
= CASE_LOW (gimple_switch_label (stmt
, i
));
7675 if (CASE_HIGH (gimple_switch_label (stmt
, i
)))
7676 high
= CASE_HIGH (gimple_switch_label (stmt
, i
));
7677 for (k
= i
+ 1; k
<= j
; ++k
)
7679 low
= CASE_LOW (gimple_switch_label (stmt
, k
));
7680 if (!integer_onep (int_const_binop (MINUS_EXPR
, low
, high
)))
7682 take_default
= true;
7686 if (CASE_HIGH (gimple_switch_label (stmt
, k
)))
7687 high
= CASE_HIGH (gimple_switch_label (stmt
, k
));
7692 return !take_default
;
7696 /* Searches the case label vector VEC for the ranges of CASE_LABELs that are
7697 used in range VR. The indices are placed in MIN_IDX1, MAX_IDX, MIN_IDX2 and
7698 MAX_IDX2. If the ranges of CASE_LABELs are empty then MAX_IDX1 < MIN_IDX1.
7699 Returns true if the default label is not needed. */
7702 find_case_label_ranges (gswitch
*stmt
, value_range
*vr
, size_t *min_idx1
,
7703 size_t *max_idx1
, size_t *min_idx2
,
7707 unsigned int n
= gimple_switch_num_labels (stmt
);
7709 tree case_low
, case_high
;
7710 tree min
= vr
->min
, max
= vr
->max
;
7712 gcc_checking_assert (vr
->type
== VR_RANGE
|| vr
->type
== VR_ANTI_RANGE
);
7714 take_default
= !find_case_label_range (stmt
, min
, max
, &i
, &j
);
7716 /* Set second range to emtpy. */
7720 if (vr
->type
== VR_RANGE
)
7724 return !take_default
;
7727 /* Set first range to all case labels. */
7734 /* Make sure all the values of case labels [i , j] are contained in
7735 range [MIN, MAX]. */
7736 case_low
= CASE_LOW (gimple_switch_label (stmt
, i
));
7737 case_high
= CASE_HIGH (gimple_switch_label (stmt
, j
));
7738 if (tree_int_cst_compare (case_low
, min
) < 0)
7740 if (case_high
!= NULL_TREE
7741 && tree_int_cst_compare (max
, case_high
) < 0)
7747 /* If the range spans case labels [i, j], the corresponding anti-range spans
7748 the labels [1, i - 1] and [j + 1, n - 1]. */
7774 /* Visit switch statement STMT. If we can determine which edge
7775 will be taken out of STMT's basic block, record it in
7776 *TAKEN_EDGE_P and return SSA_PROP_INTERESTING. Otherwise, return
7777 SSA_PROP_VARYING. */
7779 static enum ssa_prop_result
7780 vrp_visit_switch_stmt (gswitch
*stmt
, edge
*taken_edge_p
)
7784 size_t i
= 0, j
= 0, k
, l
;
7787 *taken_edge_p
= NULL
;
7788 op
= gimple_switch_index (stmt
);
7789 if (TREE_CODE (op
) != SSA_NAME
)
7790 return SSA_PROP_VARYING
;
7792 vr
= get_value_range (op
);
7793 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7795 fprintf (dump_file
, "\nVisiting switch expression with operand ");
7796 print_generic_expr (dump_file
, op
, 0);
7797 fprintf (dump_file
, " with known range ");
7798 dump_value_range (dump_file
, vr
);
7799 fprintf (dump_file
, "\n");
7802 if ((vr
->type
!= VR_RANGE
7803 && vr
->type
!= VR_ANTI_RANGE
)
7804 || symbolic_range_p (vr
))
7805 return SSA_PROP_VARYING
;
7807 /* Find the single edge that is taken from the switch expression. */
7808 take_default
= !find_case_label_ranges (stmt
, vr
, &i
, &j
, &k
, &l
);
7810 /* Check if the range spans no CASE_LABEL. If so, we only reach the default
7814 gcc_assert (take_default
);
7815 val
= gimple_switch_default_label (stmt
);
7819 /* Check if labels with index i to j and maybe the default label
7820 are all reaching the same label. */
7822 val
= gimple_switch_label (stmt
, i
);
7824 && CASE_LABEL (gimple_switch_default_label (stmt
))
7825 != CASE_LABEL (val
))
7827 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7828 fprintf (dump_file
, " not a single destination for this "
7830 return SSA_PROP_VARYING
;
7832 for (++i
; i
<= j
; ++i
)
7834 if (CASE_LABEL (gimple_switch_label (stmt
, i
)) != CASE_LABEL (val
))
7836 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7837 fprintf (dump_file
, " not a single destination for this "
7839 return SSA_PROP_VARYING
;
7844 if (CASE_LABEL (gimple_switch_label (stmt
, k
)) != CASE_LABEL (val
))
7846 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7847 fprintf (dump_file
, " not a single destination for this "
7849 return SSA_PROP_VARYING
;
7854 *taken_edge_p
= find_edge (gimple_bb (stmt
),
7855 label_to_block (CASE_LABEL (val
)));
7857 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7859 fprintf (dump_file
, " will take edge to ");
7860 print_generic_stmt (dump_file
, CASE_LABEL (val
), 0);
7863 return SSA_PROP_INTERESTING
;
7867 /* Evaluate statement STMT. If the statement produces a useful range,
7868 return SSA_PROP_INTERESTING and record the SSA name with the
7869 interesting range into *OUTPUT_P.
7871 If STMT is a conditional branch and we can determine its truth
7872 value, the taken edge is recorded in *TAKEN_EDGE_P.
7874 If STMT produces a varying value, return SSA_PROP_VARYING. */
7876 static enum ssa_prop_result
7877 vrp_visit_stmt (gimple
*stmt
, edge
*taken_edge_p
, tree
*output_p
)
7882 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7884 fprintf (dump_file
, "\nVisiting statement:\n");
7885 print_gimple_stmt (dump_file
, stmt
, 0, dump_flags
);
7888 if (!stmt_interesting_for_vrp (stmt
))
7889 gcc_assert (stmt_ends_bb_p (stmt
));
7890 else if (is_gimple_assign (stmt
) || is_gimple_call (stmt
))
7891 return vrp_visit_assignment_or_call (stmt
, output_p
);
7892 else if (gimple_code (stmt
) == GIMPLE_COND
)
7893 return vrp_visit_cond_stmt (as_a
<gcond
*> (stmt
), taken_edge_p
);
7894 else if (gimple_code (stmt
) == GIMPLE_SWITCH
)
7895 return vrp_visit_switch_stmt (as_a
<gswitch
*> (stmt
), taken_edge_p
);
7897 /* All other statements produce nothing of interest for VRP, so mark
7898 their outputs varying and prevent further simulation. */
7899 FOR_EACH_SSA_TREE_OPERAND (def
, stmt
, iter
, SSA_OP_DEF
)
7900 set_value_range_to_varying (get_value_range (def
));
7902 return SSA_PROP_VARYING
;
7905 /* Union the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
7906 { VR1TYPE, VR0MIN, VR0MAX } and store the result
7907 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
7908 possible such range. The resulting range is not canonicalized. */
7911 union_ranges (enum value_range_type
*vr0type
,
7912 tree
*vr0min
, tree
*vr0max
,
7913 enum value_range_type vr1type
,
7914 tree vr1min
, tree vr1max
)
7916 bool mineq
= operand_equal_p (*vr0min
, vr1min
, 0);
7917 bool maxeq
= operand_equal_p (*vr0max
, vr1max
, 0);
7919 /* [] is vr0, () is vr1 in the following classification comments. */
7923 if (*vr0type
== vr1type
)
7924 /* Nothing to do for equal ranges. */
7926 else if ((*vr0type
== VR_RANGE
7927 && vr1type
== VR_ANTI_RANGE
)
7928 || (*vr0type
== VR_ANTI_RANGE
7929 && vr1type
== VR_RANGE
))
7931 /* For anti-range with range union the result is varying. */
7937 else if (operand_less_p (*vr0max
, vr1min
) == 1
7938 || operand_less_p (vr1max
, *vr0min
) == 1)
7940 /* [ ] ( ) or ( ) [ ]
7941 If the ranges have an empty intersection, result of the union
7942 operation is the anti-range or if both are anti-ranges
7944 if (*vr0type
== VR_ANTI_RANGE
7945 && vr1type
== VR_ANTI_RANGE
)
7947 else if (*vr0type
== VR_ANTI_RANGE
7948 && vr1type
== VR_RANGE
)
7950 else if (*vr0type
== VR_RANGE
7951 && vr1type
== VR_ANTI_RANGE
)
7957 else if (*vr0type
== VR_RANGE
7958 && vr1type
== VR_RANGE
)
7960 /* The result is the convex hull of both ranges. */
7961 if (operand_less_p (*vr0max
, vr1min
) == 1)
7963 /* If the result can be an anti-range, create one. */
7964 if (TREE_CODE (*vr0max
) == INTEGER_CST
7965 && TREE_CODE (vr1min
) == INTEGER_CST
7966 && vrp_val_is_min (*vr0min
)
7967 && vrp_val_is_max (vr1max
))
7969 tree min
= int_const_binop (PLUS_EXPR
,
7971 build_int_cst (TREE_TYPE (*vr0max
), 1));
7972 tree max
= int_const_binop (MINUS_EXPR
,
7974 build_int_cst (TREE_TYPE (vr1min
), 1));
7975 if (!operand_less_p (max
, min
))
7977 *vr0type
= VR_ANTI_RANGE
;
7989 /* If the result can be an anti-range, create one. */
7990 if (TREE_CODE (vr1max
) == INTEGER_CST
7991 && TREE_CODE (*vr0min
) == INTEGER_CST
7992 && vrp_val_is_min (vr1min
)
7993 && vrp_val_is_max (*vr0max
))
7995 tree min
= int_const_binop (PLUS_EXPR
,
7997 build_int_cst (TREE_TYPE (vr1max
), 1));
7998 tree max
= int_const_binop (MINUS_EXPR
,
8000 build_int_cst (TREE_TYPE (*vr0min
), 1));
8001 if (!operand_less_p (max
, min
))
8003 *vr0type
= VR_ANTI_RANGE
;
8017 else if ((maxeq
|| operand_less_p (vr1max
, *vr0max
) == 1)
8018 && (mineq
|| operand_less_p (*vr0min
, vr1min
) == 1))
8020 /* [ ( ) ] or [( ) ] or [ ( )] */
8021 if (*vr0type
== VR_RANGE
8022 && vr1type
== VR_RANGE
)
8024 else if (*vr0type
== VR_ANTI_RANGE
8025 && vr1type
== VR_ANTI_RANGE
)
8031 else if (*vr0type
== VR_ANTI_RANGE
8032 && vr1type
== VR_RANGE
)
8034 /* Arbitrarily choose the right or left gap. */
8035 if (!mineq
&& TREE_CODE (vr1min
) == INTEGER_CST
)
8036 *vr0max
= int_const_binop (MINUS_EXPR
, vr1min
,
8037 build_int_cst (TREE_TYPE (vr1min
), 1));
8038 else if (!maxeq
&& TREE_CODE (vr1max
) == INTEGER_CST
)
8039 *vr0min
= int_const_binop (PLUS_EXPR
, vr1max
,
8040 build_int_cst (TREE_TYPE (vr1max
), 1));
8044 else if (*vr0type
== VR_RANGE
8045 && vr1type
== VR_ANTI_RANGE
)
8046 /* The result covers everything. */
8051 else if ((maxeq
|| operand_less_p (*vr0max
, vr1max
) == 1)
8052 && (mineq
|| operand_less_p (vr1min
, *vr0min
) == 1))
8054 /* ( [ ] ) or ([ ] ) or ( [ ]) */
8055 if (*vr0type
== VR_RANGE
8056 && vr1type
== VR_RANGE
)
8062 else if (*vr0type
== VR_ANTI_RANGE
8063 && vr1type
== VR_ANTI_RANGE
)
8065 else if (*vr0type
== VR_RANGE
8066 && vr1type
== VR_ANTI_RANGE
)
8068 *vr0type
= VR_ANTI_RANGE
;
8069 if (!mineq
&& TREE_CODE (*vr0min
) == INTEGER_CST
)
8071 *vr0max
= int_const_binop (MINUS_EXPR
, *vr0min
,
8072 build_int_cst (TREE_TYPE (*vr0min
), 1));
8075 else if (!maxeq
&& TREE_CODE (*vr0max
) == INTEGER_CST
)
8077 *vr0min
= int_const_binop (PLUS_EXPR
, *vr0max
,
8078 build_int_cst (TREE_TYPE (*vr0max
), 1));
8084 else if (*vr0type
== VR_ANTI_RANGE
8085 && vr1type
== VR_RANGE
)
8086 /* The result covers everything. */
8091 else if ((operand_less_p (vr1min
, *vr0max
) == 1
8092 || operand_equal_p (vr1min
, *vr0max
, 0))
8093 && operand_less_p (*vr0min
, vr1min
) == 1
8094 && operand_less_p (*vr0max
, vr1max
) == 1)
8096 /* [ ( ] ) or [ ]( ) */
8097 if (*vr0type
== VR_RANGE
8098 && vr1type
== VR_RANGE
)
8100 else if (*vr0type
== VR_ANTI_RANGE
8101 && vr1type
== VR_ANTI_RANGE
)
8103 else if (*vr0type
== VR_ANTI_RANGE
8104 && vr1type
== VR_RANGE
)
8106 if (TREE_CODE (vr1min
) == INTEGER_CST
)
8107 *vr0max
= int_const_binop (MINUS_EXPR
, vr1min
,
8108 build_int_cst (TREE_TYPE (vr1min
), 1));
8112 else if (*vr0type
== VR_RANGE
8113 && vr1type
== VR_ANTI_RANGE
)
8115 if (TREE_CODE (*vr0max
) == INTEGER_CST
)
8118 *vr0min
= int_const_binop (PLUS_EXPR
, *vr0max
,
8119 build_int_cst (TREE_TYPE (*vr0max
), 1));
8128 else if ((operand_less_p (*vr0min
, vr1max
) == 1
8129 || operand_equal_p (*vr0min
, vr1max
, 0))
8130 && operand_less_p (vr1min
, *vr0min
) == 1
8131 && operand_less_p (vr1max
, *vr0max
) == 1)
8133 /* ( [ ) ] or ( )[ ] */
8134 if (*vr0type
== VR_RANGE
8135 && vr1type
== VR_RANGE
)
8137 else if (*vr0type
== VR_ANTI_RANGE
8138 && vr1type
== VR_ANTI_RANGE
)
8140 else if (*vr0type
== VR_ANTI_RANGE
8141 && vr1type
== VR_RANGE
)
8143 if (TREE_CODE (vr1max
) == INTEGER_CST
)
8144 *vr0min
= int_const_binop (PLUS_EXPR
, vr1max
,
8145 build_int_cst (TREE_TYPE (vr1max
), 1));
8149 else if (*vr0type
== VR_RANGE
8150 && vr1type
== VR_ANTI_RANGE
)
8152 if (TREE_CODE (*vr0min
) == INTEGER_CST
)
8156 *vr0max
= int_const_binop (MINUS_EXPR
, *vr0min
,
8157 build_int_cst (TREE_TYPE (*vr0min
), 1));
8171 *vr0type
= VR_VARYING
;
8172 *vr0min
= NULL_TREE
;
8173 *vr0max
= NULL_TREE
;
8176 /* Intersect the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
8177 { VR1TYPE, VR0MIN, VR0MAX } and store the result
8178 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
8179 possible such range. The resulting range is not canonicalized. */
8182 intersect_ranges (enum value_range_type
*vr0type
,
8183 tree
*vr0min
, tree
*vr0max
,
8184 enum value_range_type vr1type
,
8185 tree vr1min
, tree vr1max
)
8187 bool mineq
= operand_equal_p (*vr0min
, vr1min
, 0);
8188 bool maxeq
= operand_equal_p (*vr0max
, vr1max
, 0);
8190 /* [] is vr0, () is vr1 in the following classification comments. */
8194 if (*vr0type
== vr1type
)
8195 /* Nothing to do for equal ranges. */
8197 else if ((*vr0type
== VR_RANGE
8198 && vr1type
== VR_ANTI_RANGE
)
8199 || (*vr0type
== VR_ANTI_RANGE
8200 && vr1type
== VR_RANGE
))
8202 /* For anti-range with range intersection the result is empty. */
8203 *vr0type
= VR_UNDEFINED
;
8204 *vr0min
= NULL_TREE
;
8205 *vr0max
= NULL_TREE
;
8210 else if (operand_less_p (*vr0max
, vr1min
) == 1
8211 || operand_less_p (vr1max
, *vr0min
) == 1)
8213 /* [ ] ( ) or ( ) [ ]
8214 If the ranges have an empty intersection, the result of the
8215 intersect operation is the range for intersecting an
8216 anti-range with a range or empty when intersecting two ranges. */
8217 if (*vr0type
== VR_RANGE
8218 && vr1type
== VR_ANTI_RANGE
)
8220 else if (*vr0type
== VR_ANTI_RANGE
8221 && vr1type
== VR_RANGE
)
8227 else if (*vr0type
== VR_RANGE
8228 && vr1type
== VR_RANGE
)
8230 *vr0type
= VR_UNDEFINED
;
8231 *vr0min
= NULL_TREE
;
8232 *vr0max
= NULL_TREE
;
8234 else if (*vr0type
== VR_ANTI_RANGE
8235 && vr1type
== VR_ANTI_RANGE
)
8237 /* If the anti-ranges are adjacent to each other merge them. */
8238 if (TREE_CODE (*vr0max
) == INTEGER_CST
8239 && TREE_CODE (vr1min
) == INTEGER_CST
8240 && operand_less_p (*vr0max
, vr1min
) == 1
8241 && integer_onep (int_const_binop (MINUS_EXPR
,
8244 else if (TREE_CODE (vr1max
) == INTEGER_CST
8245 && TREE_CODE (*vr0min
) == INTEGER_CST
8246 && operand_less_p (vr1max
, *vr0min
) == 1
8247 && integer_onep (int_const_binop (MINUS_EXPR
,
8250 /* Else arbitrarily take VR0. */
8253 else if ((maxeq
|| operand_less_p (vr1max
, *vr0max
) == 1)
8254 && (mineq
|| operand_less_p (*vr0min
, vr1min
) == 1))
8256 /* [ ( ) ] or [( ) ] or [ ( )] */
8257 if (*vr0type
== VR_RANGE
8258 && vr1type
== VR_RANGE
)
8260 /* If both are ranges the result is the inner one. */
8265 else if (*vr0type
== VR_RANGE
8266 && vr1type
== VR_ANTI_RANGE
)
8268 /* Choose the right gap if the left one is empty. */
8271 if (TREE_CODE (vr1max
) == INTEGER_CST
)
8272 *vr0min
= int_const_binop (PLUS_EXPR
, vr1max
,
8273 build_int_cst (TREE_TYPE (vr1max
), 1));
8277 /* Choose the left gap if the right one is empty. */
8280 if (TREE_CODE (vr1min
) == INTEGER_CST
)
8281 *vr0max
= int_const_binop (MINUS_EXPR
, vr1min
,
8282 build_int_cst (TREE_TYPE (vr1min
), 1));
8286 /* Choose the anti-range if the range is effectively varying. */
8287 else if (vrp_val_is_min (*vr0min
)
8288 && vrp_val_is_max (*vr0max
))
8294 /* Else choose the range. */
8296 else if (*vr0type
== VR_ANTI_RANGE
8297 && vr1type
== VR_ANTI_RANGE
)
8298 /* If both are anti-ranges the result is the outer one. */
8300 else if (*vr0type
== VR_ANTI_RANGE
8301 && vr1type
== VR_RANGE
)
8303 /* The intersection is empty. */
8304 *vr0type
= VR_UNDEFINED
;
8305 *vr0min
= NULL_TREE
;
8306 *vr0max
= NULL_TREE
;
8311 else if ((maxeq
|| operand_less_p (*vr0max
, vr1max
) == 1)
8312 && (mineq
|| operand_less_p (vr1min
, *vr0min
) == 1))
8314 /* ( [ ] ) or ([ ] ) or ( [ ]) */
8315 if (*vr0type
== VR_RANGE
8316 && vr1type
== VR_RANGE
)
8317 /* Choose the inner range. */
8319 else if (*vr0type
== VR_ANTI_RANGE
8320 && vr1type
== VR_RANGE
)
8322 /* Choose the right gap if the left is empty. */
8325 *vr0type
= VR_RANGE
;
8326 if (TREE_CODE (*vr0max
) == INTEGER_CST
)
8327 *vr0min
= int_const_binop (PLUS_EXPR
, *vr0max
,
8328 build_int_cst (TREE_TYPE (*vr0max
), 1));
8333 /* Choose the left gap if the right is empty. */
8336 *vr0type
= VR_RANGE
;
8337 if (TREE_CODE (*vr0min
) == INTEGER_CST
)
8338 *vr0max
= int_const_binop (MINUS_EXPR
, *vr0min
,
8339 build_int_cst (TREE_TYPE (*vr0min
), 1));
8344 /* Choose the anti-range if the range is effectively varying. */
8345 else if (vrp_val_is_min (vr1min
)
8346 && vrp_val_is_max (vr1max
))
8348 /* Else choose the range. */
8356 else if (*vr0type
== VR_ANTI_RANGE
8357 && vr1type
== VR_ANTI_RANGE
)
8359 /* If both are anti-ranges the result is the outer one. */
8364 else if (vr1type
== VR_ANTI_RANGE
8365 && *vr0type
== VR_RANGE
)
8367 /* The intersection is empty. */
8368 *vr0type
= VR_UNDEFINED
;
8369 *vr0min
= NULL_TREE
;
8370 *vr0max
= NULL_TREE
;
8375 else if ((operand_less_p (vr1min
, *vr0max
) == 1
8376 || operand_equal_p (vr1min
, *vr0max
, 0))
8377 && operand_less_p (*vr0min
, vr1min
) == 1)
8379 /* [ ( ] ) or [ ]( ) */
8380 if (*vr0type
== VR_ANTI_RANGE
8381 && vr1type
== VR_ANTI_RANGE
)
8383 else if (*vr0type
== VR_RANGE
8384 && vr1type
== VR_RANGE
)
8386 else if (*vr0type
== VR_RANGE
8387 && vr1type
== VR_ANTI_RANGE
)
8389 if (TREE_CODE (vr1min
) == INTEGER_CST
)
8390 *vr0max
= int_const_binop (MINUS_EXPR
, vr1min
,
8391 build_int_cst (TREE_TYPE (vr1min
), 1));
8395 else if (*vr0type
== VR_ANTI_RANGE
8396 && vr1type
== VR_RANGE
)
8398 *vr0type
= VR_RANGE
;
8399 if (TREE_CODE (*vr0max
) == INTEGER_CST
)
8400 *vr0min
= int_const_binop (PLUS_EXPR
, *vr0max
,
8401 build_int_cst (TREE_TYPE (*vr0max
), 1));
8409 else if ((operand_less_p (*vr0min
, vr1max
) == 1
8410 || operand_equal_p (*vr0min
, vr1max
, 0))
8411 && operand_less_p (vr1min
, *vr0min
) == 1)
8413 /* ( [ ) ] or ( )[ ] */
8414 if (*vr0type
== VR_ANTI_RANGE
8415 && vr1type
== VR_ANTI_RANGE
)
8417 else if (*vr0type
== VR_RANGE
8418 && vr1type
== VR_RANGE
)
8420 else if (*vr0type
== VR_RANGE
8421 && vr1type
== VR_ANTI_RANGE
)
8423 if (TREE_CODE (vr1max
) == INTEGER_CST
)
8424 *vr0min
= int_const_binop (PLUS_EXPR
, vr1max
,
8425 build_int_cst (TREE_TYPE (vr1max
), 1));
8429 else if (*vr0type
== VR_ANTI_RANGE
8430 && vr1type
== VR_RANGE
)
8432 *vr0type
= VR_RANGE
;
8433 if (TREE_CODE (*vr0min
) == INTEGER_CST
)
8434 *vr0max
= int_const_binop (MINUS_EXPR
, *vr0min
,
8435 build_int_cst (TREE_TYPE (*vr0min
), 1));
8444 /* As a fallback simply use { *VRTYPE, *VR0MIN, *VR0MAX } as
8445 result for the intersection. That's always a conservative
8446 correct estimate. */
8452 /* Intersect the two value-ranges *VR0 and *VR1 and store the result
8453 in *VR0. This may not be the smallest possible such range. */
8456 vrp_intersect_ranges_1 (value_range
*vr0
, value_range
*vr1
)
8460 /* If either range is VR_VARYING the other one wins. */
8461 if (vr1
->type
== VR_VARYING
)
8463 if (vr0
->type
== VR_VARYING
)
8465 copy_value_range (vr0
, vr1
);
8469 /* When either range is VR_UNDEFINED the resulting range is
8470 VR_UNDEFINED, too. */
8471 if (vr0
->type
== VR_UNDEFINED
)
8473 if (vr1
->type
== VR_UNDEFINED
)
8475 set_value_range_to_undefined (vr0
);
8479 /* Save the original vr0 so we can return it as conservative intersection
8480 result when our worker turns things to varying. */
8482 intersect_ranges (&vr0
->type
, &vr0
->min
, &vr0
->max
,
8483 vr1
->type
, vr1
->min
, vr1
->max
);
8484 /* Make sure to canonicalize the result though as the inversion of a
8485 VR_RANGE can still be a VR_RANGE. */
8486 set_and_canonicalize_value_range (vr0
, vr0
->type
,
8487 vr0
->min
, vr0
->max
, vr0
->equiv
);
8488 /* If that failed, use the saved original VR0. */
8489 if (vr0
->type
== VR_VARYING
)
8494 /* If the result is VR_UNDEFINED there is no need to mess with
8495 the equivalencies. */
8496 if (vr0
->type
== VR_UNDEFINED
)
8499 /* The resulting set of equivalences for range intersection is the union of
8501 if (vr0
->equiv
&& vr1
->equiv
&& vr0
->equiv
!= vr1
->equiv
)
8502 bitmap_ior_into (vr0
->equiv
, vr1
->equiv
);
8503 else if (vr1
->equiv
&& !vr0
->equiv
)
8504 bitmap_copy (vr0
->equiv
, vr1
->equiv
);
8508 vrp_intersect_ranges (value_range
*vr0
, value_range
*vr1
)
8510 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8512 fprintf (dump_file
, "Intersecting\n ");
8513 dump_value_range (dump_file
, vr0
);
8514 fprintf (dump_file
, "\nand\n ");
8515 dump_value_range (dump_file
, vr1
);
8516 fprintf (dump_file
, "\n");
8518 vrp_intersect_ranges_1 (vr0
, vr1
);
8519 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8521 fprintf (dump_file
, "to\n ");
8522 dump_value_range (dump_file
, vr0
);
8523 fprintf (dump_file
, "\n");
8527 /* Meet operation for value ranges. Given two value ranges VR0 and
8528 VR1, store in VR0 a range that contains both VR0 and VR1. This
8529 may not be the smallest possible such range. */
8532 vrp_meet_1 (value_range
*vr0
, value_range
*vr1
)
8536 if (vr0
->type
== VR_UNDEFINED
)
8538 set_value_range (vr0
, vr1
->type
, vr1
->min
, vr1
->max
, vr1
->equiv
);
8542 if (vr1
->type
== VR_UNDEFINED
)
8544 /* VR0 already has the resulting range. */
8548 if (vr0
->type
== VR_VARYING
)
8550 /* Nothing to do. VR0 already has the resulting range. */
8554 if (vr1
->type
== VR_VARYING
)
8556 set_value_range_to_varying (vr0
);
8561 union_ranges (&vr0
->type
, &vr0
->min
, &vr0
->max
,
8562 vr1
->type
, vr1
->min
, vr1
->max
);
8563 if (vr0
->type
== VR_VARYING
)
8565 /* Failed to find an efficient meet. Before giving up and setting
8566 the result to VARYING, see if we can at least derive a useful
8567 anti-range. FIXME, all this nonsense about distinguishing
8568 anti-ranges from ranges is necessary because of the odd
8569 semantics of range_includes_zero_p and friends. */
8570 if (((saved
.type
== VR_RANGE
8571 && range_includes_zero_p (saved
.min
, saved
.max
) == 0)
8572 || (saved
.type
== VR_ANTI_RANGE
8573 && range_includes_zero_p (saved
.min
, saved
.max
) == 1))
8574 && ((vr1
->type
== VR_RANGE
8575 && range_includes_zero_p (vr1
->min
, vr1
->max
) == 0)
8576 || (vr1
->type
== VR_ANTI_RANGE
8577 && range_includes_zero_p (vr1
->min
, vr1
->max
) == 1)))
8579 set_value_range_to_nonnull (vr0
, TREE_TYPE (saved
.min
));
8581 /* Since this meet operation did not result from the meeting of
8582 two equivalent names, VR0 cannot have any equivalences. */
8584 bitmap_clear (vr0
->equiv
);
8588 set_value_range_to_varying (vr0
);
8591 set_and_canonicalize_value_range (vr0
, vr0
->type
, vr0
->min
, vr0
->max
,
8593 if (vr0
->type
== VR_VARYING
)
8596 /* The resulting set of equivalences is always the intersection of
8598 if (vr0
->equiv
&& vr1
->equiv
&& vr0
->equiv
!= vr1
->equiv
)
8599 bitmap_and_into (vr0
->equiv
, vr1
->equiv
);
8600 else if (vr0
->equiv
&& !vr1
->equiv
)
8601 bitmap_clear (vr0
->equiv
);
8605 vrp_meet (value_range
*vr0
, value_range
*vr1
)
8607 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8609 fprintf (dump_file
, "Meeting\n ");
8610 dump_value_range (dump_file
, vr0
);
8611 fprintf (dump_file
, "\nand\n ");
8612 dump_value_range (dump_file
, vr1
);
8613 fprintf (dump_file
, "\n");
8615 vrp_meet_1 (vr0
, vr1
);
8616 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8618 fprintf (dump_file
, "to\n ");
8619 dump_value_range (dump_file
, vr0
);
8620 fprintf (dump_file
, "\n");
8625 /* Visit all arguments for PHI node PHI that flow through executable
8626 edges. If a valid value range can be derived from all the incoming
8627 value ranges, set a new range for the LHS of PHI. */
8629 static enum ssa_prop_result
8630 vrp_visit_phi_node (gphi
*phi
)
8633 tree lhs
= PHI_RESULT (phi
);
8634 value_range
*lhs_vr
= get_value_range (lhs
);
8635 value_range vr_result
= VR_INITIALIZER
;
8637 int edges
, old_edges
;
8640 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8642 fprintf (dump_file
, "\nVisiting PHI node: ");
8643 print_gimple_stmt (dump_file
, phi
, 0, dump_flags
);
8647 for (i
= 0; i
< gimple_phi_num_args (phi
); i
++)
8649 edge e
= gimple_phi_arg_edge (phi
, i
);
8651 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8654 " Argument #%d (%d -> %d %sexecutable)\n",
8655 (int) i
, e
->src
->index
, e
->dest
->index
,
8656 (e
->flags
& EDGE_EXECUTABLE
) ? "" : "not ");
8659 if (e
->flags
& EDGE_EXECUTABLE
)
8661 tree arg
= PHI_ARG_DEF (phi
, i
);
8666 if (TREE_CODE (arg
) == SSA_NAME
)
8668 vr_arg
= *(get_value_range (arg
));
8669 /* Do not allow equivalences or symbolic ranges to leak in from
8670 backedges. That creates invalid equivalencies.
8671 See PR53465 and PR54767. */
8672 if (e
->flags
& EDGE_DFS_BACK
)
8674 if (vr_arg
.type
== VR_RANGE
8675 || vr_arg
.type
== VR_ANTI_RANGE
)
8677 vr_arg
.equiv
= NULL
;
8678 if (symbolic_range_p (&vr_arg
))
8680 vr_arg
.type
= VR_VARYING
;
8681 vr_arg
.min
= NULL_TREE
;
8682 vr_arg
.max
= NULL_TREE
;
8688 /* If the non-backedge arguments range is VR_VARYING then
8689 we can still try recording a simple equivalence. */
8690 if (vr_arg
.type
== VR_VARYING
)
8692 vr_arg
.type
= VR_RANGE
;
8695 vr_arg
.equiv
= NULL
;
8701 if (TREE_OVERFLOW_P (arg
))
8702 arg
= drop_tree_overflow (arg
);
8704 vr_arg
.type
= VR_RANGE
;
8707 vr_arg
.equiv
= NULL
;
8710 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8712 fprintf (dump_file
, "\t");
8713 print_generic_expr (dump_file
, arg
, dump_flags
);
8714 fprintf (dump_file
, ": ");
8715 dump_value_range (dump_file
, &vr_arg
);
8716 fprintf (dump_file
, "\n");
8720 copy_value_range (&vr_result
, &vr_arg
);
8722 vrp_meet (&vr_result
, &vr_arg
);
8725 if (vr_result
.type
== VR_VARYING
)
8730 if (vr_result
.type
== VR_VARYING
)
8732 else if (vr_result
.type
== VR_UNDEFINED
)
8735 old_edges
= vr_phi_edge_counts
[SSA_NAME_VERSION (lhs
)];
8736 vr_phi_edge_counts
[SSA_NAME_VERSION (lhs
)] = edges
;
8738 /* To prevent infinite iterations in the algorithm, derive ranges
8739 when the new value is slightly bigger or smaller than the
8740 previous one. We don't do this if we have seen a new executable
8741 edge; this helps us avoid an overflow infinity for conditionals
8742 which are not in a loop. If the old value-range was VR_UNDEFINED
8743 use the updated range and iterate one more time. */
8745 && gimple_phi_num_args (phi
) > 1
8746 && edges
== old_edges
8747 && lhs_vr
->type
!= VR_UNDEFINED
)
8749 /* Compare old and new ranges, fall back to varying if the
8750 values are not comparable. */
8751 int cmp_min
= compare_values (lhs_vr
->min
, vr_result
.min
);
8754 int cmp_max
= compare_values (lhs_vr
->max
, vr_result
.max
);
8758 /* For non VR_RANGE or for pointers fall back to varying if
8759 the range changed. */
8760 if ((lhs_vr
->type
!= VR_RANGE
|| vr_result
.type
!= VR_RANGE
8761 || POINTER_TYPE_P (TREE_TYPE (lhs
)))
8762 && (cmp_min
!= 0 || cmp_max
!= 0))
8765 /* If the new minimum is larger than the previous one
8766 retain the old value. If the new minimum value is smaller
8767 than the previous one and not -INF go all the way to -INF + 1.
8768 In the first case, to avoid infinite bouncing between different
8769 minimums, and in the other case to avoid iterating millions of
8770 times to reach -INF. Going to -INF + 1 also lets the following
8771 iteration compute whether there will be any overflow, at the
8772 expense of one additional iteration. */
8774 vr_result
.min
= lhs_vr
->min
;
8775 else if (cmp_min
> 0
8776 && !vrp_val_is_min (vr_result
.min
))
8778 = int_const_binop (PLUS_EXPR
,
8779 vrp_val_min (TREE_TYPE (vr_result
.min
)),
8780 build_int_cst (TREE_TYPE (vr_result
.min
), 1));
8782 /* Similarly for the maximum value. */
8784 vr_result
.max
= lhs_vr
->max
;
8785 else if (cmp_max
< 0
8786 && !vrp_val_is_max (vr_result
.max
))
8788 = int_const_binop (MINUS_EXPR
,
8789 vrp_val_max (TREE_TYPE (vr_result
.min
)),
8790 build_int_cst (TREE_TYPE (vr_result
.min
), 1));
8792 /* If we dropped either bound to +-INF then if this is a loop
8793 PHI node SCEV may known more about its value-range. */
8794 if ((cmp_min
> 0 || cmp_min
< 0
8795 || cmp_max
< 0 || cmp_max
> 0)
8796 && (l
= loop_containing_stmt (phi
))
8797 && l
->header
== gimple_bb (phi
))
8798 adjust_range_with_scev (&vr_result
, l
, phi
, lhs
);
8800 /* If we will end up with a (-INF, +INF) range, set it to
8801 VARYING. Same if the previous max value was invalid for
8802 the type and we end up with vr_result.min > vr_result.max. */
8803 if ((vrp_val_is_max (vr_result
.max
)
8804 && vrp_val_is_min (vr_result
.min
))
8805 || compare_values (vr_result
.min
,
8810 /* If the new range is different than the previous value, keep
8813 if (update_value_range (lhs
, &vr_result
))
8815 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8817 fprintf (dump_file
, "Found new range for ");
8818 print_generic_expr (dump_file
, lhs
, 0);
8819 fprintf (dump_file
, ": ");
8820 dump_value_range (dump_file
, &vr_result
);
8821 fprintf (dump_file
, "\n");
8824 if (vr_result
.type
== VR_VARYING
)
8825 return SSA_PROP_VARYING
;
8827 return SSA_PROP_INTERESTING
;
8830 /* Nothing changed, don't add outgoing edges. */
8831 return SSA_PROP_NOT_INTERESTING
;
8833 /* No match found. Set the LHS to VARYING. */
8835 set_value_range_to_varying (lhs_vr
);
8836 return SSA_PROP_VARYING
;
8839 /* Simplify boolean operations if the source is known
8840 to be already a boolean. */
8842 simplify_truth_ops_using_ranges (gimple_stmt_iterator
*gsi
, gimple
*stmt
)
8844 enum tree_code rhs_code
= gimple_assign_rhs_code (stmt
);
8846 bool need_conversion
;
8848 /* We handle only !=/== case here. */
8849 gcc_assert (rhs_code
== EQ_EXPR
|| rhs_code
== NE_EXPR
);
8851 op0
= gimple_assign_rhs1 (stmt
);
8852 if (!op_with_boolean_value_range_p (op0
))
8855 op1
= gimple_assign_rhs2 (stmt
);
8856 if (!op_with_boolean_value_range_p (op1
))
8859 /* Reduce number of cases to handle to NE_EXPR. As there is no
8860 BIT_XNOR_EXPR we cannot replace A == B with a single statement. */
8861 if (rhs_code
== EQ_EXPR
)
8863 if (TREE_CODE (op1
) == INTEGER_CST
)
8864 op1
= int_const_binop (BIT_XOR_EXPR
, op1
,
8865 build_int_cst (TREE_TYPE (op1
), 1));
8870 lhs
= gimple_assign_lhs (stmt
);
8872 = !useless_type_conversion_p (TREE_TYPE (lhs
), TREE_TYPE (op0
));
8874 /* Make sure to not sign-extend a 1-bit 1 when converting the result. */
8876 && !TYPE_UNSIGNED (TREE_TYPE (op0
))
8877 && TYPE_PRECISION (TREE_TYPE (op0
)) == 1
8878 && TYPE_PRECISION (TREE_TYPE (lhs
)) > 1)
8881 /* For A != 0 we can substitute A itself. */
8882 if (integer_zerop (op1
))
8883 gimple_assign_set_rhs_with_ops (gsi
,
8885 ? NOP_EXPR
: TREE_CODE (op0
), op0
);
8886 /* For A != B we substitute A ^ B. Either with conversion. */
8887 else if (need_conversion
)
8889 tree tem
= make_ssa_name (TREE_TYPE (op0
));
8891 = gimple_build_assign (tem
, BIT_XOR_EXPR
, op0
, op1
);
8892 gsi_insert_before (gsi
, newop
, GSI_SAME_STMT
);
8893 gimple_assign_set_rhs_with_ops (gsi
, NOP_EXPR
, tem
);
8897 gimple_assign_set_rhs_with_ops (gsi
, BIT_XOR_EXPR
, op0
, op1
);
8898 update_stmt (gsi_stmt (*gsi
));
8903 /* Simplify a division or modulo operator to a right shift or
8904 bitwise and if the first operand is unsigned or is greater
8905 than zero and the second operand is an exact power of two.
8906 For TRUNC_MOD_EXPR op0 % op1 with constant op1, optimize it
8907 into just op0 if op0's range is known to be a subset of
8908 [-op1 + 1, op1 - 1] for signed and [0, op1 - 1] for unsigned
8912 simplify_div_or_mod_using_ranges (gimple
*stmt
)
8914 enum tree_code rhs_code
= gimple_assign_rhs_code (stmt
);
8916 tree op0
= gimple_assign_rhs1 (stmt
);
8917 tree op1
= gimple_assign_rhs2 (stmt
);
8918 value_range
*vr
= get_value_range (op0
);
8920 if (rhs_code
== TRUNC_MOD_EXPR
8921 && TREE_CODE (op1
) == INTEGER_CST
8922 && tree_int_cst_sgn (op1
) == 1
8923 && range_int_cst_p (vr
)
8924 && tree_int_cst_lt (vr
->max
, op1
))
8926 if (TYPE_UNSIGNED (TREE_TYPE (op0
))
8927 || tree_int_cst_sgn (vr
->min
) >= 0
8928 || tree_int_cst_lt (fold_unary (NEGATE_EXPR
, TREE_TYPE (op1
), op1
),
8931 /* If op0 already has the range op0 % op1 has,
8932 then TRUNC_MOD_EXPR won't change anything. */
8933 gimple_stmt_iterator gsi
= gsi_for_stmt (stmt
);
8934 gimple_assign_set_rhs_from_tree (&gsi
, op0
);
8940 if (!integer_pow2p (op1
))
8943 if (TYPE_UNSIGNED (TREE_TYPE (op0
)))
8945 val
= integer_one_node
;
8951 val
= compare_range_with_value (GE_EXPR
, vr
, integer_zero_node
, &sop
);
8955 && integer_onep (val
)
8956 && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC
))
8958 location_t location
;
8960 if (!gimple_has_location (stmt
))
8961 location
= input_location
;
8963 location
= gimple_location (stmt
);
8964 warning_at (location
, OPT_Wstrict_overflow
,
8965 "assuming signed overflow does not occur when "
8966 "simplifying %</%> or %<%%%> to %<>>%> or %<&%>");
8970 if (val
&& integer_onep (val
))
8974 if (rhs_code
== TRUNC_DIV_EXPR
)
8976 t
= build_int_cst (integer_type_node
, tree_log2 (op1
));
8977 gimple_assign_set_rhs_code (stmt
, RSHIFT_EXPR
);
8978 gimple_assign_set_rhs1 (stmt
, op0
);
8979 gimple_assign_set_rhs2 (stmt
, t
);
8983 t
= build_int_cst (TREE_TYPE (op1
), 1);
8984 t
= int_const_binop (MINUS_EXPR
, op1
, t
);
8985 t
= fold_convert (TREE_TYPE (op0
), t
);
8987 gimple_assign_set_rhs_code (stmt
, BIT_AND_EXPR
);
8988 gimple_assign_set_rhs1 (stmt
, op0
);
8989 gimple_assign_set_rhs2 (stmt
, t
);
8999 /* Simplify a min or max if the ranges of the two operands are
9000 disjoint. Return true if we do simplify. */
9003 simplify_min_or_max_using_ranges (gimple
*stmt
)
9005 tree op0
= gimple_assign_rhs1 (stmt
);
9006 tree op1
= gimple_assign_rhs2 (stmt
);
9010 val
= (vrp_evaluate_conditional_warnv_with_ops_using_ranges
9011 (LE_EXPR
, op0
, op1
, &sop
));
9015 val
= (vrp_evaluate_conditional_warnv_with_ops_using_ranges
9016 (LT_EXPR
, op0
, op1
, &sop
));
9021 if (sop
&& issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC
))
9023 location_t location
;
9025 if (!gimple_has_location (stmt
))
9026 location
= input_location
;
9028 location
= gimple_location (stmt
);
9029 warning_at (location
, OPT_Wstrict_overflow
,
9030 "assuming signed overflow does not occur when "
9031 "simplifying %<min/max (X,Y)%> to %<X%> or %<Y%>");
9034 /* VAL == TRUE -> OP0 < or <= op1
9035 VAL == FALSE -> OP0 > or >= op1. */
9036 tree res
= ((gimple_assign_rhs_code (stmt
) == MAX_EXPR
)
9037 == integer_zerop (val
)) ? op0
: op1
;
9038 gimple_stmt_iterator gsi
= gsi_for_stmt (stmt
);
9039 gimple_assign_set_rhs_from_tree (&gsi
, res
);
9047 /* If the operand to an ABS_EXPR is >= 0, then eliminate the
9048 ABS_EXPR. If the operand is <= 0, then simplify the
9049 ABS_EXPR into a NEGATE_EXPR. */
9052 simplify_abs_using_ranges (gimple
*stmt
)
9054 tree op
= gimple_assign_rhs1 (stmt
);
9055 value_range
*vr
= get_value_range (op
);
9062 val
= compare_range_with_value (LE_EXPR
, vr
, integer_zero_node
, &sop
);
9065 /* The range is neither <= 0 nor > 0. Now see if it is
9066 either < 0 or >= 0. */
9068 val
= compare_range_with_value (LT_EXPR
, vr
, integer_zero_node
,
9074 if (sop
&& issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC
))
9076 location_t location
;
9078 if (!gimple_has_location (stmt
))
9079 location
= input_location
;
9081 location
= gimple_location (stmt
);
9082 warning_at (location
, OPT_Wstrict_overflow
,
9083 "assuming signed overflow does not occur when "
9084 "simplifying %<abs (X)%> to %<X%> or %<-X%>");
9087 gimple_assign_set_rhs1 (stmt
, op
);
9088 if (integer_zerop (val
))
9089 gimple_assign_set_rhs_code (stmt
, SSA_NAME
);
9091 gimple_assign_set_rhs_code (stmt
, NEGATE_EXPR
);
9100 /* Optimize away redundant BIT_AND_EXPR and BIT_IOR_EXPR.
9101 If all the bits that are being cleared by & are already
9102 known to be zero from VR, or all the bits that are being
9103 set by | are already known to be one from VR, the bit
9104 operation is redundant. */
9107 simplify_bit_ops_using_ranges (gimple_stmt_iterator
*gsi
, gimple
*stmt
)
9109 tree op0
= gimple_assign_rhs1 (stmt
);
9110 tree op1
= gimple_assign_rhs2 (stmt
);
9111 tree op
= NULL_TREE
;
9112 value_range vr0
= VR_INITIALIZER
;
9113 value_range vr1
= VR_INITIALIZER
;
9114 wide_int may_be_nonzero0
, may_be_nonzero1
;
9115 wide_int must_be_nonzero0
, must_be_nonzero1
;
9118 if (TREE_CODE (op0
) == SSA_NAME
)
9119 vr0
= *(get_value_range (op0
));
9120 else if (is_gimple_min_invariant (op0
))
9121 set_value_range_to_value (&vr0
, op0
, NULL
);
9125 if (TREE_CODE (op1
) == SSA_NAME
)
9126 vr1
= *(get_value_range (op1
));
9127 else if (is_gimple_min_invariant (op1
))
9128 set_value_range_to_value (&vr1
, op1
, NULL
);
9132 if (!zero_nonzero_bits_from_vr (TREE_TYPE (op0
), &vr0
, &may_be_nonzero0
,
9135 if (!zero_nonzero_bits_from_vr (TREE_TYPE (op1
), &vr1
, &may_be_nonzero1
,
9139 switch (gimple_assign_rhs_code (stmt
))
9142 mask
= may_be_nonzero0
.and_not (must_be_nonzero1
);
9148 mask
= may_be_nonzero1
.and_not (must_be_nonzero0
);
9156 mask
= may_be_nonzero0
.and_not (must_be_nonzero1
);
9162 mask
= may_be_nonzero1
.and_not (must_be_nonzero0
);
9173 if (op
== NULL_TREE
)
9176 gimple_assign_set_rhs_with_ops (gsi
, TREE_CODE (op
), op
);
9177 update_stmt (gsi_stmt (*gsi
));
9181 /* We are comparing trees OP0 and OP1 using COND_CODE. OP0 has
9182 a known value range VR.
9184 If there is one and only one value which will satisfy the
9185 conditional, then return that value. Else return NULL.
9187 If signed overflow must be undefined for the value to satisfy
9188 the conditional, then set *STRICT_OVERFLOW_P to true. */
9191 test_for_singularity (enum tree_code cond_code
, tree op0
,
9192 tree op1
, value_range
*vr
,
9193 bool *strict_overflow_p
)
9198 /* Extract minimum/maximum values which satisfy the
9199 the conditional as it was written. */
9200 if (cond_code
== LE_EXPR
|| cond_code
== LT_EXPR
)
9202 /* This should not be negative infinity; there is no overflow
9204 min
= TYPE_MIN_VALUE (TREE_TYPE (op0
));
9207 if (cond_code
== LT_EXPR
&& !is_overflow_infinity (max
))
9209 tree one
= build_int_cst (TREE_TYPE (op0
), 1);
9210 max
= fold_build2 (MINUS_EXPR
, TREE_TYPE (op0
), max
, one
);
9212 TREE_NO_WARNING (max
) = 1;
9215 else if (cond_code
== GE_EXPR
|| cond_code
== GT_EXPR
)
9217 /* This should not be positive infinity; there is no overflow
9219 max
= TYPE_MAX_VALUE (TREE_TYPE (op0
));
9222 if (cond_code
== GT_EXPR
&& !is_overflow_infinity (min
))
9224 tree one
= build_int_cst (TREE_TYPE (op0
), 1);
9225 min
= fold_build2 (PLUS_EXPR
, TREE_TYPE (op0
), min
, one
);
9227 TREE_NO_WARNING (min
) = 1;
9231 /* Now refine the minimum and maximum values using any
9232 value range information we have for op0. */
9235 if (compare_values (vr
->min
, min
) == 1)
9237 if (compare_values (vr
->max
, max
) == -1)
9240 /* If the new min/max values have converged to a single value,
9241 then there is only one value which can satisfy the condition,
9242 return that value. */
9243 if (operand_equal_p (min
, max
, 0) && is_gimple_min_invariant (min
))
9245 if ((cond_code
== LE_EXPR
|| cond_code
== LT_EXPR
)
9246 && is_overflow_infinity (vr
->max
))
9247 *strict_overflow_p
= true;
9248 if ((cond_code
== GE_EXPR
|| cond_code
== GT_EXPR
)
9249 && is_overflow_infinity (vr
->min
))
9250 *strict_overflow_p
= true;
9258 /* Return whether the value range *VR fits in an integer type specified
9259 by PRECISION and UNSIGNED_P. */
9262 range_fits_type_p (value_range
*vr
, unsigned dest_precision
, signop dest_sgn
)
9265 unsigned src_precision
;
9269 /* We can only handle integral and pointer types. */
9270 src_type
= TREE_TYPE (vr
->min
);
9271 if (!INTEGRAL_TYPE_P (src_type
)
9272 && !POINTER_TYPE_P (src_type
))
9275 /* An extension is fine unless VR is SIGNED and dest_sgn is UNSIGNED,
9276 and so is an identity transform. */
9277 src_precision
= TYPE_PRECISION (TREE_TYPE (vr
->min
));
9278 src_sgn
= TYPE_SIGN (src_type
);
9279 if ((src_precision
< dest_precision
9280 && !(dest_sgn
== UNSIGNED
&& src_sgn
== SIGNED
))
9281 || (src_precision
== dest_precision
&& src_sgn
== dest_sgn
))
9284 /* Now we can only handle ranges with constant bounds. */
9285 if (vr
->type
!= VR_RANGE
9286 || TREE_CODE (vr
->min
) != INTEGER_CST
9287 || TREE_CODE (vr
->max
) != INTEGER_CST
)
9290 /* For sign changes, the MSB of the wide_int has to be clear.
9291 An unsigned value with its MSB set cannot be represented by
9292 a signed wide_int, while a negative value cannot be represented
9293 by an unsigned wide_int. */
9294 if (src_sgn
!= dest_sgn
9295 && (wi::lts_p (vr
->min
, 0) || wi::lts_p (vr
->max
, 0)))
9298 /* Then we can perform the conversion on both ends and compare
9299 the result for equality. */
9300 tem
= wi::ext (wi::to_widest (vr
->min
), dest_precision
, dest_sgn
);
9301 if (tem
!= wi::to_widest (vr
->min
))
9303 tem
= wi::ext (wi::to_widest (vr
->max
), dest_precision
, dest_sgn
);
9304 if (tem
!= wi::to_widest (vr
->max
))
9310 /* Simplify a conditional using a relational operator to an equality
9311 test if the range information indicates only one value can satisfy
9312 the original conditional. */
9315 simplify_cond_using_ranges (gcond
*stmt
)
9317 tree op0
= gimple_cond_lhs (stmt
);
9318 tree op1
= gimple_cond_rhs (stmt
);
9319 enum tree_code cond_code
= gimple_cond_code (stmt
);
9321 if (cond_code
!= NE_EXPR
9322 && cond_code
!= EQ_EXPR
9323 && TREE_CODE (op0
) == SSA_NAME
9324 && INTEGRAL_TYPE_P (TREE_TYPE (op0
))
9325 && is_gimple_min_invariant (op1
))
9327 value_range
*vr
= get_value_range (op0
);
9329 /* If we have range information for OP0, then we might be
9330 able to simplify this conditional. */
9331 if (vr
->type
== VR_RANGE
)
9333 enum warn_strict_overflow_code wc
= WARN_STRICT_OVERFLOW_COMPARISON
;
9335 tree new_tree
= test_for_singularity (cond_code
, op0
, op1
, vr
, &sop
);
9338 && (!sop
|| TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0
))))
9342 fprintf (dump_file
, "Simplified relational ");
9343 print_gimple_stmt (dump_file
, stmt
, 0, 0);
9344 fprintf (dump_file
, " into ");
9347 gimple_cond_set_code (stmt
, EQ_EXPR
);
9348 gimple_cond_set_lhs (stmt
, op0
);
9349 gimple_cond_set_rhs (stmt
, new_tree
);
9355 print_gimple_stmt (dump_file
, stmt
, 0, 0);
9356 fprintf (dump_file
, "\n");
9359 if (sop
&& issue_strict_overflow_warning (wc
))
9361 location_t location
= input_location
;
9362 if (gimple_has_location (stmt
))
9363 location
= gimple_location (stmt
);
9365 warning_at (location
, OPT_Wstrict_overflow
,
9366 "assuming signed overflow does not occur when "
9367 "simplifying conditional");
9373 /* Try again after inverting the condition. We only deal
9374 with integral types here, so no need to worry about
9375 issues with inverting FP comparisons. */
9377 new_tree
= test_for_singularity
9378 (invert_tree_comparison (cond_code
, false),
9379 op0
, op1
, vr
, &sop
);
9382 && (!sop
|| TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0
))))
9386 fprintf (dump_file
, "Simplified relational ");
9387 print_gimple_stmt (dump_file
, stmt
, 0, 0);
9388 fprintf (dump_file
, " into ");
9391 gimple_cond_set_code (stmt
, NE_EXPR
);
9392 gimple_cond_set_lhs (stmt
, op0
);
9393 gimple_cond_set_rhs (stmt
, new_tree
);
9399 print_gimple_stmt (dump_file
, stmt
, 0, 0);
9400 fprintf (dump_file
, "\n");
9403 if (sop
&& issue_strict_overflow_warning (wc
))
9405 location_t location
= input_location
;
9406 if (gimple_has_location (stmt
))
9407 location
= gimple_location (stmt
);
9409 warning_at (location
, OPT_Wstrict_overflow
,
9410 "assuming signed overflow does not occur when "
9411 "simplifying conditional");
9419 /* If we have a comparison of an SSA_NAME (OP0) against a constant,
9420 see if OP0 was set by a type conversion where the source of
9421 the conversion is another SSA_NAME with a range that fits
9422 into the range of OP0's type.
9424 If so, the conversion is redundant as the earlier SSA_NAME can be
9425 used for the comparison directly if we just massage the constant in the
9427 if (TREE_CODE (op0
) == SSA_NAME
9428 && TREE_CODE (op1
) == INTEGER_CST
)
9430 gimple
*def_stmt
= SSA_NAME_DEF_STMT (op0
);
9433 if (!is_gimple_assign (def_stmt
)
9434 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt
)))
9437 innerop
= gimple_assign_rhs1 (def_stmt
);
9439 if (TREE_CODE (innerop
) == SSA_NAME
9440 && !POINTER_TYPE_P (TREE_TYPE (innerop
)))
9442 value_range
*vr
= get_value_range (innerop
);
9444 if (range_int_cst_p (vr
)
9445 && range_fits_type_p (vr
,
9446 TYPE_PRECISION (TREE_TYPE (op0
)),
9447 TYPE_SIGN (TREE_TYPE (op0
)))
9448 && int_fits_type_p (op1
, TREE_TYPE (innerop
))
9449 /* The range must not have overflowed, or if it did overflow
9450 we must not be wrapping/trapping overflow and optimizing
9451 with strict overflow semantics. */
9452 && ((!is_negative_overflow_infinity (vr
->min
)
9453 && !is_positive_overflow_infinity (vr
->max
))
9454 || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (innerop
))))
9456 /* If the range overflowed and the user has asked for warnings
9457 when strict overflow semantics were used to optimize code,
9458 issue an appropriate warning. */
9459 if (cond_code
!= EQ_EXPR
&& cond_code
!= NE_EXPR
9460 && (is_negative_overflow_infinity (vr
->min
)
9461 || is_positive_overflow_infinity (vr
->max
))
9462 && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_CONDITIONAL
))
9464 location_t location
;
9466 if (!gimple_has_location (stmt
))
9467 location
= input_location
;
9469 location
= gimple_location (stmt
);
9470 warning_at (location
, OPT_Wstrict_overflow
,
9471 "assuming signed overflow does not occur when "
9472 "simplifying conditional");
9475 tree newconst
= fold_convert (TREE_TYPE (innerop
), op1
);
9476 gimple_cond_set_lhs (stmt
, innerop
);
9477 gimple_cond_set_rhs (stmt
, newconst
);
9486 /* Simplify a switch statement using the value range of the switch
9490 simplify_switch_using_ranges (gswitch
*stmt
)
9492 tree op
= gimple_switch_index (stmt
);
9497 size_t i
= 0, j
= 0, n
, n2
;
9500 size_t k
= 1, l
= 0;
9502 if (TREE_CODE (op
) == SSA_NAME
)
9504 vr
= get_value_range (op
);
9506 /* We can only handle integer ranges. */
9507 if ((vr
->type
!= VR_RANGE
9508 && vr
->type
!= VR_ANTI_RANGE
)
9509 || symbolic_range_p (vr
))
9512 /* Find case label for min/max of the value range. */
9513 take_default
= !find_case_label_ranges (stmt
, vr
, &i
, &j
, &k
, &l
);
9515 else if (TREE_CODE (op
) == INTEGER_CST
)
9517 take_default
= !find_case_label_index (stmt
, 1, op
, &i
);
9531 n
= gimple_switch_num_labels (stmt
);
9533 /* Bail out if this is just all edges taken. */
9539 /* Build a new vector of taken case labels. */
9540 vec2
= make_tree_vec (j
- i
+ 1 + l
- k
+ 1 + (int)take_default
);
9543 /* Add the default edge, if necessary. */
9545 TREE_VEC_ELT (vec2
, n2
++) = gimple_switch_default_label (stmt
);
9547 for (; i
<= j
; ++i
, ++n2
)
9548 TREE_VEC_ELT (vec2
, n2
) = gimple_switch_label (stmt
, i
);
9550 for (; k
<= l
; ++k
, ++n2
)
9551 TREE_VEC_ELT (vec2
, n2
) = gimple_switch_label (stmt
, k
);
9553 /* Mark needed edges. */
9554 for (i
= 0; i
< n2
; ++i
)
9556 e
= find_edge (gimple_bb (stmt
),
9557 label_to_block (CASE_LABEL (TREE_VEC_ELT (vec2
, i
))));
9558 e
->aux
= (void *)-1;
9561 /* Queue not needed edges for later removal. */
9562 FOR_EACH_EDGE (e
, ei
, gimple_bb (stmt
)->succs
)
9564 if (e
->aux
== (void *)-1)
9570 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
9572 fprintf (dump_file
, "removing unreachable case label\n");
9574 to_remove_edges
.safe_push (e
);
9575 e
->flags
&= ~EDGE_EXECUTABLE
;
9578 /* And queue an update for the stmt. */
9581 to_update_switch_stmts
.safe_push (su
);
9585 /* Simplify an integral conversion from an SSA name in STMT. */
9588 simplify_conversion_using_ranges (gimple
*stmt
)
9590 tree innerop
, middleop
, finaltype
;
9592 value_range
*innervr
;
9593 signop inner_sgn
, middle_sgn
, final_sgn
;
9594 unsigned inner_prec
, middle_prec
, final_prec
;
9595 widest_int innermin
, innermed
, innermax
, middlemin
, middlemed
, middlemax
;
9597 finaltype
= TREE_TYPE (gimple_assign_lhs (stmt
));
9598 if (!INTEGRAL_TYPE_P (finaltype
))
9600 middleop
= gimple_assign_rhs1 (stmt
);
9601 def_stmt
= SSA_NAME_DEF_STMT (middleop
);
9602 if (!is_gimple_assign (def_stmt
)
9603 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt
)))
9605 innerop
= gimple_assign_rhs1 (def_stmt
);
9606 if (TREE_CODE (innerop
) != SSA_NAME
9607 || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (innerop
))
9610 /* Get the value-range of the inner operand. */
9611 innervr
= get_value_range (innerop
);
9612 if (innervr
->type
!= VR_RANGE
9613 || TREE_CODE (innervr
->min
) != INTEGER_CST
9614 || TREE_CODE (innervr
->max
) != INTEGER_CST
)
9617 /* Simulate the conversion chain to check if the result is equal if
9618 the middle conversion is removed. */
9619 innermin
= wi::to_widest (innervr
->min
);
9620 innermax
= wi::to_widest (innervr
->max
);
9622 inner_prec
= TYPE_PRECISION (TREE_TYPE (innerop
));
9623 middle_prec
= TYPE_PRECISION (TREE_TYPE (middleop
));
9624 final_prec
= TYPE_PRECISION (finaltype
);
9626 /* If the first conversion is not injective, the second must not
9628 if (wi::gtu_p (innermax
- innermin
,
9629 wi::mask
<widest_int
> (middle_prec
, false))
9630 && middle_prec
< final_prec
)
9632 /* We also want a medium value so that we can track the effect that
9633 narrowing conversions with sign change have. */
9634 inner_sgn
= TYPE_SIGN (TREE_TYPE (innerop
));
9635 if (inner_sgn
== UNSIGNED
)
9636 innermed
= wi::shifted_mask
<widest_int
> (1, inner_prec
- 1, false);
9639 if (wi::cmp (innermin
, innermed
, inner_sgn
) >= 0
9640 || wi::cmp (innermed
, innermax
, inner_sgn
) >= 0)
9641 innermed
= innermin
;
9643 middle_sgn
= TYPE_SIGN (TREE_TYPE (middleop
));
9644 middlemin
= wi::ext (innermin
, middle_prec
, middle_sgn
);
9645 middlemed
= wi::ext (innermed
, middle_prec
, middle_sgn
);
9646 middlemax
= wi::ext (innermax
, middle_prec
, middle_sgn
);
9648 /* Require that the final conversion applied to both the original
9649 and the intermediate range produces the same result. */
9650 final_sgn
= TYPE_SIGN (finaltype
);
9651 if (wi::ext (middlemin
, final_prec
, final_sgn
)
9652 != wi::ext (innermin
, final_prec
, final_sgn
)
9653 || wi::ext (middlemed
, final_prec
, final_sgn
)
9654 != wi::ext (innermed
, final_prec
, final_sgn
)
9655 || wi::ext (middlemax
, final_prec
, final_sgn
)
9656 != wi::ext (innermax
, final_prec
, final_sgn
))
9659 gimple_assign_set_rhs1 (stmt
, innerop
);
9664 /* Simplify a conversion from integral SSA name to float in STMT. */
9667 simplify_float_conversion_using_ranges (gimple_stmt_iterator
*gsi
,
9670 tree rhs1
= gimple_assign_rhs1 (stmt
);
9671 value_range
*vr
= get_value_range (rhs1
);
9672 machine_mode fltmode
= TYPE_MODE (TREE_TYPE (gimple_assign_lhs (stmt
)));
9677 /* We can only handle constant ranges. */
9678 if (vr
->type
!= VR_RANGE
9679 || TREE_CODE (vr
->min
) != INTEGER_CST
9680 || TREE_CODE (vr
->max
) != INTEGER_CST
)
9683 /* First check if we can use a signed type in place of an unsigned. */
9684 if (TYPE_UNSIGNED (TREE_TYPE (rhs1
))
9685 && (can_float_p (fltmode
, TYPE_MODE (TREE_TYPE (rhs1
)), 0)
9686 != CODE_FOR_nothing
)
9687 && range_fits_type_p (vr
, TYPE_PRECISION (TREE_TYPE (rhs1
)), SIGNED
))
9688 mode
= TYPE_MODE (TREE_TYPE (rhs1
));
9689 /* If we can do the conversion in the current input mode do nothing. */
9690 else if (can_float_p (fltmode
, TYPE_MODE (TREE_TYPE (rhs1
)),
9691 TYPE_UNSIGNED (TREE_TYPE (rhs1
))) != CODE_FOR_nothing
)
9693 /* Otherwise search for a mode we can use, starting from the narrowest
9694 integer mode available. */
9697 mode
= GET_CLASS_NARROWEST_MODE (MODE_INT
);
9700 /* If we cannot do a signed conversion to float from mode
9701 or if the value-range does not fit in the signed type
9702 try with a wider mode. */
9703 if (can_float_p (fltmode
, mode
, 0) != CODE_FOR_nothing
9704 && range_fits_type_p (vr
, GET_MODE_PRECISION (mode
), SIGNED
))
9707 mode
= GET_MODE_WIDER_MODE (mode
);
9708 /* But do not widen the input. Instead leave that to the
9709 optabs expansion code. */
9710 if (GET_MODE_PRECISION (mode
) > TYPE_PRECISION (TREE_TYPE (rhs1
)))
9713 while (mode
!= VOIDmode
);
9714 if (mode
== VOIDmode
)
9718 /* It works, insert a truncation or sign-change before the
9719 float conversion. */
9720 tem
= make_ssa_name (build_nonstandard_integer_type
9721 (GET_MODE_PRECISION (mode
), 0));
9722 conv
= gimple_build_assign (tem
, NOP_EXPR
, rhs1
);
9723 gsi_insert_before (gsi
, conv
, GSI_SAME_STMT
);
9724 gimple_assign_set_rhs1 (stmt
, tem
);
9730 /* Simplify an internal fn call using ranges if possible. */
9733 simplify_internal_call_using_ranges (gimple_stmt_iterator
*gsi
, gimple
*stmt
)
9735 enum tree_code subcode
;
9736 bool is_ubsan
= false;
9738 switch (gimple_call_internal_fn (stmt
))
9740 case IFN_UBSAN_CHECK_ADD
:
9741 subcode
= PLUS_EXPR
;
9744 case IFN_UBSAN_CHECK_SUB
:
9745 subcode
= MINUS_EXPR
;
9748 case IFN_UBSAN_CHECK_MUL
:
9749 subcode
= MULT_EXPR
;
9752 case IFN_ADD_OVERFLOW
:
9753 subcode
= PLUS_EXPR
;
9755 case IFN_SUB_OVERFLOW
:
9756 subcode
= MINUS_EXPR
;
9758 case IFN_MUL_OVERFLOW
:
9759 subcode
= MULT_EXPR
;
9765 tree op0
= gimple_call_arg (stmt
, 0);
9766 tree op1
= gimple_call_arg (stmt
, 1);
9769 type
= TREE_TYPE (op0
);
9770 else if (gimple_call_lhs (stmt
) == NULL_TREE
)
9773 type
= TREE_TYPE (TREE_TYPE (gimple_call_lhs (stmt
)));
9774 if (!check_for_binary_op_overflow (subcode
, type
, op0
, op1
, &ovf
)
9775 || (is_ubsan
&& ovf
))
9779 location_t loc
= gimple_location (stmt
);
9781 g
= gimple_build_assign (gimple_call_lhs (stmt
), subcode
, op0
, op1
);
9784 int prec
= TYPE_PRECISION (type
);
9787 || !useless_type_conversion_p (type
, TREE_TYPE (op0
))
9788 || !useless_type_conversion_p (type
, TREE_TYPE (op1
)))
9789 utype
= build_nonstandard_integer_type (prec
, 1);
9790 if (TREE_CODE (op0
) == INTEGER_CST
)
9791 op0
= fold_convert (utype
, op0
);
9792 else if (!useless_type_conversion_p (utype
, TREE_TYPE (op0
)))
9794 g
= gimple_build_assign (make_ssa_name (utype
), NOP_EXPR
, op0
);
9795 gimple_set_location (g
, loc
);
9796 gsi_insert_before (gsi
, g
, GSI_SAME_STMT
);
9797 op0
= gimple_assign_lhs (g
);
9799 if (TREE_CODE (op1
) == INTEGER_CST
)
9800 op1
= fold_convert (utype
, op1
);
9801 else if (!useless_type_conversion_p (utype
, TREE_TYPE (op1
)))
9803 g
= gimple_build_assign (make_ssa_name (utype
), NOP_EXPR
, op1
);
9804 gimple_set_location (g
, loc
);
9805 gsi_insert_before (gsi
, g
, GSI_SAME_STMT
);
9806 op1
= gimple_assign_lhs (g
);
9808 g
= gimple_build_assign (make_ssa_name (utype
), subcode
, op0
, op1
);
9809 gimple_set_location (g
, loc
);
9810 gsi_insert_before (gsi
, g
, GSI_SAME_STMT
);
9813 g
= gimple_build_assign (make_ssa_name (type
), NOP_EXPR
,
9814 gimple_assign_lhs (g
));
9815 gimple_set_location (g
, loc
);
9816 gsi_insert_before (gsi
, g
, GSI_SAME_STMT
);
9818 g
= gimple_build_assign (gimple_call_lhs (stmt
), COMPLEX_EXPR
,
9819 gimple_assign_lhs (g
),
9820 build_int_cst (type
, ovf
));
9822 gimple_set_location (g
, loc
);
9823 gsi_replace (gsi
, g
, false);
9827 /* Simplify STMT using ranges if possible. */
9830 simplify_stmt_using_ranges (gimple_stmt_iterator
*gsi
)
9832 gimple
*stmt
= gsi_stmt (*gsi
);
9833 if (is_gimple_assign (stmt
))
9835 enum tree_code rhs_code
= gimple_assign_rhs_code (stmt
);
9836 tree rhs1
= gimple_assign_rhs1 (stmt
);
9842 /* Transform EQ_EXPR, NE_EXPR into BIT_XOR_EXPR or identity
9843 if the RHS is zero or one, and the LHS are known to be boolean
9845 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
9846 return simplify_truth_ops_using_ranges (gsi
, stmt
);
9849 /* Transform TRUNC_DIV_EXPR and TRUNC_MOD_EXPR into RSHIFT_EXPR
9850 and BIT_AND_EXPR respectively if the first operand is greater
9851 than zero and the second operand is an exact power of two.
9852 Also optimize TRUNC_MOD_EXPR away if the second operand is
9853 constant and the first operand already has the right value
9855 case TRUNC_DIV_EXPR
:
9856 case TRUNC_MOD_EXPR
:
9857 if (TREE_CODE (rhs1
) == SSA_NAME
9858 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
9859 return simplify_div_or_mod_using_ranges (stmt
);
9862 /* Transform ABS (X) into X or -X as appropriate. */
9864 if (TREE_CODE (rhs1
) == SSA_NAME
9865 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
9866 return simplify_abs_using_ranges (stmt
);
9871 /* Optimize away BIT_AND_EXPR and BIT_IOR_EXPR
9872 if all the bits being cleared are already cleared or
9873 all the bits being set are already set. */
9874 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
9875 return simplify_bit_ops_using_ranges (gsi
, stmt
);
9879 if (TREE_CODE (rhs1
) == SSA_NAME
9880 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
9881 return simplify_conversion_using_ranges (stmt
);
9885 if (TREE_CODE (rhs1
) == SSA_NAME
9886 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
9887 return simplify_float_conversion_using_ranges (gsi
, stmt
);
9892 return simplify_min_or_max_using_ranges (stmt
);
9899 else if (gimple_code (stmt
) == GIMPLE_COND
)
9900 return simplify_cond_using_ranges (as_a
<gcond
*> (stmt
));
9901 else if (gimple_code (stmt
) == GIMPLE_SWITCH
)
9902 return simplify_switch_using_ranges (as_a
<gswitch
*> (stmt
));
9903 else if (is_gimple_call (stmt
)
9904 && gimple_call_internal_p (stmt
))
9905 return simplify_internal_call_using_ranges (gsi
, stmt
);
9910 /* If the statement pointed by SI has a predicate whose value can be
9911 computed using the value range information computed by VRP, compute
9912 its value and return true. Otherwise, return false. */
9915 fold_predicate_in (gimple_stmt_iterator
*si
)
9917 bool assignment_p
= false;
9919 gimple
*stmt
= gsi_stmt (*si
);
9921 if (is_gimple_assign (stmt
)
9922 && TREE_CODE_CLASS (gimple_assign_rhs_code (stmt
)) == tcc_comparison
)
9924 assignment_p
= true;
9925 val
= vrp_evaluate_conditional (gimple_assign_rhs_code (stmt
),
9926 gimple_assign_rhs1 (stmt
),
9927 gimple_assign_rhs2 (stmt
),
9930 else if (gcond
*cond_stmt
= dyn_cast
<gcond
*> (stmt
))
9931 val
= vrp_evaluate_conditional (gimple_cond_code (cond_stmt
),
9932 gimple_cond_lhs (cond_stmt
),
9933 gimple_cond_rhs (cond_stmt
),
9941 val
= fold_convert (gimple_expr_type (stmt
), val
);
9945 fprintf (dump_file
, "Folding predicate ");
9946 print_gimple_expr (dump_file
, stmt
, 0, 0);
9947 fprintf (dump_file
, " to ");
9948 print_generic_expr (dump_file
, val
, 0);
9949 fprintf (dump_file
, "\n");
9952 if (is_gimple_assign (stmt
))
9953 gimple_assign_set_rhs_from_tree (si
, val
);
9956 gcc_assert (gimple_code (stmt
) == GIMPLE_COND
);
9957 gcond
*cond_stmt
= as_a
<gcond
*> (stmt
);
9958 if (integer_zerop (val
))
9959 gimple_cond_make_false (cond_stmt
);
9960 else if (integer_onep (val
))
9961 gimple_cond_make_true (cond_stmt
);
9972 /* Callback for substitute_and_fold folding the stmt at *SI. */
9975 vrp_fold_stmt (gimple_stmt_iterator
*si
)
9977 if (fold_predicate_in (si
))
9980 return simplify_stmt_using_ranges (si
);
9983 /* Unwindable const/copy equivalences. */
9984 const_and_copies
*equiv_stack
;
9986 /* A trivial wrapper so that we can present the generic jump threading
9987 code with a simple API for simplifying statements. STMT is the
9988 statement we want to simplify, WITHIN_STMT provides the location
9989 for any overflow warnings. */
9992 simplify_stmt_for_jump_threading (gimple
*stmt
, gimple
*within_stmt
,
9993 class avail_exprs_stack
*avail_exprs_stack ATTRIBUTE_UNUSED
)
9995 if (gcond
*cond_stmt
= dyn_cast
<gcond
*> (stmt
))
9996 return vrp_evaluate_conditional (gimple_cond_code (cond_stmt
),
9997 gimple_cond_lhs (cond_stmt
),
9998 gimple_cond_rhs (cond_stmt
),
10001 if (gassign
*assign_stmt
= dyn_cast
<gassign
*> (stmt
))
10003 value_range new_vr
= VR_INITIALIZER
;
10004 tree lhs
= gimple_assign_lhs (assign_stmt
);
10006 if (TREE_CODE (lhs
) == SSA_NAME
10007 && (INTEGRAL_TYPE_P (TREE_TYPE (lhs
))
10008 || POINTER_TYPE_P (TREE_TYPE (lhs
))))
10010 extract_range_from_assignment (&new_vr
, assign_stmt
);
10011 if (range_int_cst_singleton_p (&new_vr
))
10019 /* Blocks which have more than one predecessor and more than
10020 one successor present jump threading opportunities, i.e.,
10021 when the block is reached from a specific predecessor, we
10022 may be able to determine which of the outgoing edges will
10023 be traversed. When this optimization applies, we are able
10024 to avoid conditionals at runtime and we may expose secondary
10025 optimization opportunities.
10027 This routine is effectively a driver for the generic jump
10028 threading code. It basically just presents the generic code
10029 with edges that may be suitable for jump threading.
10031 Unlike DOM, we do not iterate VRP if jump threading was successful.
10032 While iterating may expose new opportunities for VRP, it is expected
10033 those opportunities would be very limited and the compile time cost
10034 to expose those opportunities would be significant.
10036 As jump threading opportunities are discovered, they are registered
10037 for later realization. */
10040 identify_jump_threads (void)
10047 /* Ugh. When substituting values earlier in this pass we can
10048 wipe the dominance information. So rebuild the dominator
10049 information as we need it within the jump threading code. */
10050 calculate_dominance_info (CDI_DOMINATORS
);
10052 /* We do not allow VRP information to be used for jump threading
10053 across a back edge in the CFG. Otherwise it becomes too
10054 difficult to avoid eliminating loop exit tests. Of course
10055 EDGE_DFS_BACK is not accurate at this time so we have to
10057 mark_dfs_back_edges ();
10059 /* Do not thread across edges we are about to remove. Just marking
10060 them as EDGE_DFS_BACK will do. */
10061 FOR_EACH_VEC_ELT (to_remove_edges
, i
, e
)
10062 e
->flags
|= EDGE_DFS_BACK
;
10064 /* Allocate our unwinder stack to unwind any temporary equivalences
10065 that might be recorded. */
10066 equiv_stack
= new const_and_copies ();
10068 /* To avoid lots of silly node creation, we create a single
10069 conditional and just modify it in-place when attempting to
10071 dummy
= gimple_build_cond (EQ_EXPR
,
10072 integer_zero_node
, integer_zero_node
,
10075 /* Walk through all the blocks finding those which present a
10076 potential jump threading opportunity. We could set this up
10077 as a dominator walker and record data during the walk, but
10078 I doubt it's worth the effort for the classes of jump
10079 threading opportunities we are trying to identify at this
10080 point in compilation. */
10081 FOR_EACH_BB_FN (bb
, cfun
)
10085 /* If the generic jump threading code does not find this block
10086 interesting, then there is nothing to do. */
10087 if (! potentially_threadable_block (bb
))
10090 last
= last_stmt (bb
);
10092 /* We're basically looking for a switch or any kind of conditional with
10093 integral or pointer type arguments. Note the type of the second
10094 argument will be the same as the first argument, so no need to
10095 check it explicitly.
10097 We also handle the case where there are no statements in the
10098 block. This come up with forwarder blocks that are not
10099 optimized away because they lead to a loop header. But we do
10100 want to thread through them as we can sometimes thread to the
10101 loop exit which is obviously profitable. */
10103 || gimple_code (last
) == GIMPLE_SWITCH
10104 || (gimple_code (last
) == GIMPLE_COND
10105 && TREE_CODE (gimple_cond_lhs (last
)) == SSA_NAME
10106 && (INTEGRAL_TYPE_P (TREE_TYPE (gimple_cond_lhs (last
)))
10107 || POINTER_TYPE_P (TREE_TYPE (gimple_cond_lhs (last
))))
10108 && (TREE_CODE (gimple_cond_rhs (last
)) == SSA_NAME
10109 || is_gimple_min_invariant (gimple_cond_rhs (last
)))))
10113 /* We've got a block with multiple predecessors and multiple
10114 successors which also ends in a suitable conditional or
10115 switch statement. For each predecessor, see if we can thread
10116 it to a specific successor. */
10117 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
10119 /* Do not thread across back edges or abnormal edges
10121 if (e
->flags
& (EDGE_DFS_BACK
| EDGE_COMPLEX
))
10124 thread_across_edge (dummy
, e
, true, equiv_stack
, NULL
,
10125 simplify_stmt_for_jump_threading
);
10130 /* We do not actually update the CFG or SSA graphs at this point as
10131 ASSERT_EXPRs are still in the IL and cfg cleanup code does not yet
10132 handle ASSERT_EXPRs gracefully. */
10135 /* We identified all the jump threading opportunities earlier, but could
10136 not transform the CFG at that time. This routine transforms the
10137 CFG and arranges for the dominator tree to be rebuilt if necessary.
10139 Note the SSA graph update will occur during the normal TODO
10140 processing by the pass manager. */
10142 finalize_jump_threads (void)
10144 thread_through_all_blocks (false);
10145 delete equiv_stack
;
10149 /* Traverse all the blocks folding conditionals with known ranges. */
10152 vrp_finalize (void)
10156 values_propagated
= true;
10160 fprintf (dump_file
, "\nValue ranges after VRP:\n\n");
10161 dump_all_value_ranges (dump_file
);
10162 fprintf (dump_file
, "\n");
10165 substitute_and_fold (op_with_constant_singleton_value_range
,
10166 vrp_fold_stmt
, false);
10168 if (warn_array_bounds
&& first_pass_instance
)
10169 check_all_array_refs ();
10171 /* We must identify jump threading opportunities before we release
10172 the datastructures built by VRP. */
10173 identify_jump_threads ();
10175 /* Set value range to non pointer SSA_NAMEs. */
10176 for (i
= 0; i
< num_vr_values
; i
++)
10179 tree name
= ssa_name (i
);
10182 || POINTER_TYPE_P (TREE_TYPE (name
))
10183 || (vr_value
[i
]->type
== VR_VARYING
)
10184 || (vr_value
[i
]->type
== VR_UNDEFINED
))
10187 if ((TREE_CODE (vr_value
[i
]->min
) == INTEGER_CST
)
10188 && (TREE_CODE (vr_value
[i
]->max
) == INTEGER_CST
)
10189 && (vr_value
[i
]->type
== VR_RANGE
10190 || vr_value
[i
]->type
== VR_ANTI_RANGE
))
10191 set_range_info (name
, vr_value
[i
]->type
, vr_value
[i
]->min
,
10195 /* Free allocated memory. */
10196 for (i
= 0; i
< num_vr_values
; i
++)
10199 BITMAP_FREE (vr_value
[i
]->equiv
);
10200 free (vr_value
[i
]);
10204 free (vr_phi_edge_counts
);
10206 /* So that we can distinguish between VRP data being available
10207 and not available. */
10209 vr_phi_edge_counts
= NULL
;
10213 /* Main entry point to VRP (Value Range Propagation). This pass is
10214 loosely based on J. R. C. Patterson, ``Accurate Static Branch
10215 Prediction by Value Range Propagation,'' in SIGPLAN Conference on
10216 Programming Language Design and Implementation, pp. 67-78, 1995.
10217 Also available at http://citeseer.ist.psu.edu/patterson95accurate.html
10219 This is essentially an SSA-CCP pass modified to deal with ranges
10220 instead of constants.
10222 While propagating ranges, we may find that two or more SSA name
10223 have equivalent, though distinct ranges. For instance,
10226 2 p_4 = ASSERT_EXPR <p_3, p_3 != 0>
10228 4 p_5 = ASSERT_EXPR <p_4, p_4 == q_2>;
10232 In the code above, pointer p_5 has range [q_2, q_2], but from the
10233 code we can also determine that p_5 cannot be NULL and, if q_2 had
10234 a non-varying range, p_5's range should also be compatible with it.
10236 These equivalences are created by two expressions: ASSERT_EXPR and
10237 copy operations. Since p_5 is an assertion on p_4, and p_4 was the
10238 result of another assertion, then we can use the fact that p_5 and
10239 p_4 are equivalent when evaluating p_5's range.
10241 Together with value ranges, we also propagate these equivalences
10242 between names so that we can take advantage of information from
10243 multiple ranges when doing final replacement. Note that this
10244 equivalency relation is transitive but not symmetric.
10246 In the example above, p_5 is equivalent to p_4, q_2 and p_3, but we
10247 cannot assert that q_2 is equivalent to p_5 because q_2 may be used
10248 in contexts where that assertion does not hold (e.g., in line 6).
10250 TODO, the main difference between this pass and Patterson's is that
10251 we do not propagate edge probabilities. We only compute whether
10252 edges can be taken or not. That is, instead of having a spectrum
10253 of jump probabilities between 0 and 1, we only deal with 0, 1 and
10254 DON'T KNOW. In the future, it may be worthwhile to propagate
10255 probabilities to aid branch prediction. */
10257 static unsigned int
10264 loop_optimizer_init (LOOPS_NORMAL
| LOOPS_HAVE_RECORDED_EXITS
);
10265 rewrite_into_loop_closed_ssa (NULL
, TODO_update_ssa
);
10266 scev_initialize ();
10268 /* ??? This ends up using stale EDGE_DFS_BACK for liveness computation.
10269 Inserting assertions may split edges which will invalidate
10271 insert_range_assertions ();
10273 to_remove_edges
.create (10);
10274 to_update_switch_stmts
.create (5);
10275 threadedge_initialize_values ();
10277 /* For visiting PHI nodes we need EDGE_DFS_BACK computed. */
10278 mark_dfs_back_edges ();
10281 ssa_propagate (vrp_visit_stmt
, vrp_visit_phi_node
);
10284 free_numbers_of_iterations_estimates ();
10286 /* ASSERT_EXPRs must be removed before finalizing jump threads
10287 as finalizing jump threads calls the CFG cleanup code which
10288 does not properly handle ASSERT_EXPRs. */
10289 remove_range_assertions ();
10291 /* If we exposed any new variables, go ahead and put them into
10292 SSA form now, before we handle jump threading. This simplifies
10293 interactions between rewriting of _DECL nodes into SSA form
10294 and rewriting SSA_NAME nodes into SSA form after block
10295 duplication and CFG manipulation. */
10296 update_ssa (TODO_update_ssa
);
10298 finalize_jump_threads ();
10300 /* Remove dead edges from SWITCH_EXPR optimization. This leaves the
10301 CFG in a broken state and requires a cfg_cleanup run. */
10302 FOR_EACH_VEC_ELT (to_remove_edges
, i
, e
)
10304 /* Update SWITCH_EXPR case label vector. */
10305 FOR_EACH_VEC_ELT (to_update_switch_stmts
, i
, su
)
10308 size_t n
= TREE_VEC_LENGTH (su
->vec
);
10310 gimple_switch_set_num_labels (su
->stmt
, n
);
10311 for (j
= 0; j
< n
; j
++)
10312 gimple_switch_set_label (su
->stmt
, j
, TREE_VEC_ELT (su
->vec
, j
));
10313 /* As we may have replaced the default label with a regular one
10314 make sure to make it a real default label again. This ensures
10315 optimal expansion. */
10316 label
= gimple_switch_label (su
->stmt
, 0);
10317 CASE_LOW (label
) = NULL_TREE
;
10318 CASE_HIGH (label
) = NULL_TREE
;
10321 if (to_remove_edges
.length () > 0)
10323 free_dominance_info (CDI_DOMINATORS
);
10324 loops_state_set (LOOPS_NEED_FIXUP
);
10327 to_remove_edges
.release ();
10328 to_update_switch_stmts
.release ();
10329 threadedge_finalize_values ();
10332 loop_optimizer_finalize ();
10338 const pass_data pass_data_vrp
=
10340 GIMPLE_PASS
, /* type */
10342 OPTGROUP_NONE
, /* optinfo_flags */
10343 TV_TREE_VRP
, /* tv_id */
10344 PROP_ssa
, /* properties_required */
10345 0, /* properties_provided */
10346 0, /* properties_destroyed */
10347 0, /* todo_flags_start */
10348 ( TODO_cleanup_cfg
| TODO_update_ssa
), /* todo_flags_finish */
10351 class pass_vrp
: public gimple_opt_pass
10354 pass_vrp (gcc::context
*ctxt
)
10355 : gimple_opt_pass (pass_data_vrp
, ctxt
)
10358 /* opt_pass methods: */
10359 opt_pass
* clone () { return new pass_vrp (m_ctxt
); }
10360 virtual bool gate (function
*) { return flag_tree_vrp
!= 0; }
10361 virtual unsigned int execute (function
*) { return execute_vrp (); }
10363 }; // class pass_vrp
10365 } // anon namespace
10368 make_pass_vrp (gcc::context
*ctxt
)
10370 return new pass_vrp (ctxt
);