1 /* Support routines for Value Range Propagation (VRP).
2 Copyright (C) 2005-2014 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"
27 #include "stor-layout.h"
29 #include "basic-block.h"
30 #include "tree-ssa-alias.h"
31 #include "internal-fn.h"
32 #include "gimple-fold.h"
34 #include "gimple-expr.h"
37 #include "gimple-iterator.h"
38 #include "gimple-walk.h"
39 #include "gimple-ssa.h"
41 #include "tree-phinodes.h"
42 #include "ssa-iterators.h"
43 #include "stringpool.h"
44 #include "tree-ssanames.h"
45 #include "tree-ssa-loop-manip.h"
46 #include "tree-ssa-loop-niter.h"
47 #include "tree-ssa-loop.h"
48 #include "tree-into-ssa.h"
50 #include "tree-pass.h"
51 #include "tree-dump.h"
52 #include "gimple-pretty-print.h"
53 #include "diagnostic-core.h"
56 #include "tree-scalar-evolution.h"
57 #include "tree-ssa-propagate.h"
58 #include "tree-chrec.h"
59 #include "tree-ssa-threadupdate.h"
62 #include "tree-ssa-threadedge.h"
67 /* Range of values that can be associated with an SSA_NAME after VRP
71 /* Lattice value represented by this range. */
72 enum value_range_type type
;
74 /* Minimum and maximum values represented by this range. These
75 values should be interpreted as follows:
77 - If TYPE is VR_UNDEFINED or VR_VARYING then MIN and MAX must
80 - If TYPE == VR_RANGE then MIN holds the minimum value and
81 MAX holds the maximum value of the range [MIN, MAX].
83 - If TYPE == ANTI_RANGE the variable is known to NOT
84 take any values in the range [MIN, MAX]. */
88 /* Set of SSA names whose value ranges are equivalent to this one.
89 This set is only valid when TYPE is VR_RANGE or VR_ANTI_RANGE. */
93 typedef struct value_range_d value_range_t
;
95 #define VR_INITIALIZER { VR_UNDEFINED, NULL_TREE, NULL_TREE, NULL }
97 /* Set of SSA names found live during the RPO traversal of the function
98 for still active basic-blocks. */
101 /* Return true if the SSA name NAME is live on the edge E. */
104 live_on_edge (edge e
, tree name
)
106 return (live
[e
->dest
->index
]
107 && bitmap_bit_p (live
[e
->dest
->index
], SSA_NAME_VERSION (name
)));
110 /* Local functions. */
111 static int compare_values (tree val1
, tree val2
);
112 static int compare_values_warnv (tree val1
, tree val2
, bool *);
113 static void vrp_meet (value_range_t
*, value_range_t
*);
114 static void vrp_intersect_ranges (value_range_t
*, value_range_t
*);
115 static tree
vrp_evaluate_conditional_warnv_with_ops (enum tree_code
,
116 tree
, tree
, bool, bool *,
119 /* Location information for ASSERT_EXPRs. Each instance of this
120 structure describes an ASSERT_EXPR for an SSA name. Since a single
121 SSA name may have more than one assertion associated with it, these
122 locations are kept in a linked list attached to the corresponding
124 struct assert_locus_d
126 /* Basic block where the assertion would be inserted. */
129 /* Some assertions need to be inserted on an edge (e.g., assertions
130 generated by COND_EXPRs). In those cases, BB will be NULL. */
133 /* Pointer to the statement that generated this assertion. */
134 gimple_stmt_iterator si
;
136 /* Predicate code for the ASSERT_EXPR. Must be COMPARISON_CLASS_P. */
137 enum tree_code comp_code
;
139 /* Value being compared against. */
142 /* Expression to compare. */
145 /* Next node in the linked list. */
146 struct assert_locus_d
*next
;
149 typedef struct assert_locus_d
*assert_locus_t
;
151 /* If bit I is present, it means that SSA name N_i has a list of
152 assertions that should be inserted in the IL. */
153 static bitmap need_assert_for
;
155 /* Array of locations lists where to insert assertions. ASSERTS_FOR[I]
156 holds a list of ASSERT_LOCUS_T nodes that describe where
157 ASSERT_EXPRs for SSA name N_I should be inserted. */
158 static assert_locus_t
*asserts_for
;
160 /* Value range array. After propagation, VR_VALUE[I] holds the range
161 of values that SSA name N_I may take. */
162 static unsigned num_vr_values
;
163 static value_range_t
**vr_value
;
164 static bool values_propagated
;
166 /* For a PHI node which sets SSA name N_I, VR_COUNTS[I] holds the
167 number of executable edges we saw the last time we visited the
169 static int *vr_phi_edge_counts
;
176 static vec
<edge
> to_remove_edges
;
177 static vec
<switch_update
> to_update_switch_stmts
;
180 /* Return the maximum value for TYPE. */
183 vrp_val_max (const_tree type
)
185 if (!INTEGRAL_TYPE_P (type
))
188 return TYPE_MAX_VALUE (type
);
191 /* Return the minimum value for TYPE. */
194 vrp_val_min (const_tree type
)
196 if (!INTEGRAL_TYPE_P (type
))
199 return TYPE_MIN_VALUE (type
);
202 /* Return whether VAL is equal to the maximum value of its type. This
203 will be true for a positive overflow infinity. We can't do a
204 simple equality comparison with TYPE_MAX_VALUE because C typedefs
205 and Ada subtypes can produce types whose TYPE_MAX_VALUE is not ==
206 to the integer constant with the same value in the type. */
209 vrp_val_is_max (const_tree val
)
211 tree type_max
= vrp_val_max (TREE_TYPE (val
));
212 return (val
== type_max
213 || (type_max
!= NULL_TREE
214 && operand_equal_p (val
, type_max
, 0)));
217 /* Return whether VAL is equal to the minimum value of its type. This
218 will be true for a negative overflow infinity. */
221 vrp_val_is_min (const_tree val
)
223 tree type_min
= vrp_val_min (TREE_TYPE (val
));
224 return (val
== type_min
225 || (type_min
!= NULL_TREE
226 && operand_equal_p (val
, type_min
, 0)));
230 /* Return whether TYPE should use an overflow infinity distinct from
231 TYPE_{MIN,MAX}_VALUE. We use an overflow infinity value to
232 represent a signed overflow during VRP computations. An infinity
233 is distinct from a half-range, which will go from some number to
234 TYPE_{MIN,MAX}_VALUE. */
237 needs_overflow_infinity (const_tree type
)
239 return INTEGRAL_TYPE_P (type
) && !TYPE_OVERFLOW_WRAPS (type
);
242 /* Return whether TYPE can support our overflow infinity
243 representation: we use the TREE_OVERFLOW flag, which only exists
244 for constants. If TYPE doesn't support this, we don't optimize
245 cases which would require signed overflow--we drop them to
249 supports_overflow_infinity (const_tree type
)
251 tree min
= vrp_val_min (type
), max
= vrp_val_max (type
);
252 #ifdef ENABLE_CHECKING
253 gcc_assert (needs_overflow_infinity (type
));
255 return (min
!= NULL_TREE
256 && CONSTANT_CLASS_P (min
)
258 && CONSTANT_CLASS_P (max
));
261 /* VAL is the maximum or minimum value of a type. Return a
262 corresponding overflow infinity. */
265 make_overflow_infinity (tree val
)
267 gcc_checking_assert (val
!= NULL_TREE
&& CONSTANT_CLASS_P (val
));
268 val
= copy_node (val
);
269 TREE_OVERFLOW (val
) = 1;
273 /* Return a negative overflow infinity for TYPE. */
276 negative_overflow_infinity (tree type
)
278 gcc_checking_assert (supports_overflow_infinity (type
));
279 return make_overflow_infinity (vrp_val_min (type
));
282 /* Return a positive overflow infinity for TYPE. */
285 positive_overflow_infinity (tree type
)
287 gcc_checking_assert (supports_overflow_infinity (type
));
288 return make_overflow_infinity (vrp_val_max (type
));
291 /* Return whether VAL is a negative overflow infinity. */
294 is_negative_overflow_infinity (const_tree val
)
296 return (needs_overflow_infinity (TREE_TYPE (val
))
297 && CONSTANT_CLASS_P (val
)
298 && TREE_OVERFLOW (val
)
299 && vrp_val_is_min (val
));
302 /* Return whether VAL is a positive overflow infinity. */
305 is_positive_overflow_infinity (const_tree val
)
307 return (needs_overflow_infinity (TREE_TYPE (val
))
308 && CONSTANT_CLASS_P (val
)
309 && TREE_OVERFLOW (val
)
310 && vrp_val_is_max (val
));
313 /* Return whether VAL is a positive or negative overflow infinity. */
316 is_overflow_infinity (const_tree val
)
318 return (needs_overflow_infinity (TREE_TYPE (val
))
319 && CONSTANT_CLASS_P (val
)
320 && TREE_OVERFLOW (val
)
321 && (vrp_val_is_min (val
) || vrp_val_is_max (val
)));
324 /* Return whether STMT has a constant rhs that is_overflow_infinity. */
327 stmt_overflow_infinity (gimple stmt
)
329 if (is_gimple_assign (stmt
)
330 && get_gimple_rhs_class (gimple_assign_rhs_code (stmt
)) ==
332 return is_overflow_infinity (gimple_assign_rhs1 (stmt
));
336 /* If VAL is now an overflow infinity, return VAL. Otherwise, return
337 the same value with TREE_OVERFLOW clear. This can be used to avoid
338 confusing a regular value with an overflow value. */
341 avoid_overflow_infinity (tree val
)
343 if (!is_overflow_infinity (val
))
346 if (vrp_val_is_max (val
))
347 return vrp_val_max (TREE_TYPE (val
));
350 gcc_checking_assert (vrp_val_is_min (val
));
351 return vrp_val_min (TREE_TYPE (val
));
356 /* Return true if ARG is marked with the nonnull attribute in the
357 current function signature. */
360 nonnull_arg_p (const_tree arg
)
362 tree t
, attrs
, fntype
;
363 unsigned HOST_WIDE_INT arg_num
;
365 gcc_assert (TREE_CODE (arg
) == PARM_DECL
&& POINTER_TYPE_P (TREE_TYPE (arg
)));
367 /* The static chain decl is always non null. */
368 if (arg
== cfun
->static_chain_decl
)
371 fntype
= TREE_TYPE (current_function_decl
);
372 for (attrs
= TYPE_ATTRIBUTES (fntype
); attrs
; attrs
= TREE_CHAIN (attrs
))
374 attrs
= lookup_attribute ("nonnull", attrs
);
376 /* If "nonnull" wasn't specified, we know nothing about the argument. */
377 if (attrs
== NULL_TREE
)
380 /* If "nonnull" applies to all the arguments, then ARG is non-null. */
381 if (TREE_VALUE (attrs
) == NULL_TREE
)
384 /* Get the position number for ARG in the function signature. */
385 for (arg_num
= 1, t
= DECL_ARGUMENTS (current_function_decl
);
387 t
= DECL_CHAIN (t
), arg_num
++)
393 gcc_assert (t
== arg
);
395 /* Now see if ARG_NUM is mentioned in the nonnull list. */
396 for (t
= TREE_VALUE (attrs
); t
; t
= TREE_CHAIN (t
))
398 if (compare_tree_int (TREE_VALUE (t
), arg_num
) == 0)
407 /* Set value range VR to VR_UNDEFINED. */
410 set_value_range_to_undefined (value_range_t
*vr
)
412 vr
->type
= VR_UNDEFINED
;
413 vr
->min
= vr
->max
= NULL_TREE
;
415 bitmap_clear (vr
->equiv
);
419 /* Set value range VR to VR_VARYING. */
422 set_value_range_to_varying (value_range_t
*vr
)
424 vr
->type
= VR_VARYING
;
425 vr
->min
= vr
->max
= NULL_TREE
;
427 bitmap_clear (vr
->equiv
);
431 /* Set value range VR to {T, MIN, MAX, EQUIV}. */
434 set_value_range (value_range_t
*vr
, enum value_range_type t
, tree min
,
435 tree max
, bitmap equiv
)
437 #if defined ENABLE_CHECKING
438 /* Check the validity of the range. */
439 if (t
== VR_RANGE
|| t
== VR_ANTI_RANGE
)
443 gcc_assert (min
&& max
);
445 gcc_assert ((!TREE_OVERFLOW_P (min
) || is_overflow_infinity (min
))
446 && (!TREE_OVERFLOW_P (max
) || is_overflow_infinity (max
)));
448 if (INTEGRAL_TYPE_P (TREE_TYPE (min
)) && t
== VR_ANTI_RANGE
)
449 gcc_assert (!vrp_val_is_min (min
) || !vrp_val_is_max (max
));
451 cmp
= compare_values (min
, max
);
452 gcc_assert (cmp
== 0 || cmp
== -1 || cmp
== -2);
454 if (needs_overflow_infinity (TREE_TYPE (min
)))
455 gcc_assert (!is_overflow_infinity (min
)
456 || !is_overflow_infinity (max
));
459 if (t
== VR_UNDEFINED
|| t
== VR_VARYING
)
460 gcc_assert (min
== NULL_TREE
&& max
== NULL_TREE
);
462 if (t
== VR_UNDEFINED
|| t
== VR_VARYING
)
463 gcc_assert (equiv
== NULL
|| bitmap_empty_p (equiv
));
470 /* Since updating the equivalence set involves deep copying the
471 bitmaps, only do it if absolutely necessary. */
472 if (vr
->equiv
== NULL
474 vr
->equiv
= BITMAP_ALLOC (NULL
);
476 if (equiv
!= vr
->equiv
)
478 if (equiv
&& !bitmap_empty_p (equiv
))
479 bitmap_copy (vr
->equiv
, equiv
);
481 bitmap_clear (vr
->equiv
);
486 /* Set value range VR to the canonical form of {T, MIN, MAX, EQUIV}.
487 This means adjusting T, MIN and MAX representing the case of a
488 wrapping range with MAX < MIN covering [MIN, type_max] U [type_min, MAX]
489 as anti-rage ~[MAX+1, MIN-1]. Likewise for wrapping anti-ranges.
490 In corner cases where MAX+1 or MIN-1 wraps this will fall back
492 This routine exists to ease canonicalization in the case where we
493 extract ranges from var + CST op limit. */
496 set_and_canonicalize_value_range (value_range_t
*vr
, enum value_range_type t
,
497 tree min
, tree max
, bitmap equiv
)
499 /* Use the canonical setters for VR_UNDEFINED and VR_VARYING. */
500 if (t
== VR_UNDEFINED
)
502 set_value_range_to_undefined (vr
);
505 else if (t
== VR_VARYING
)
507 set_value_range_to_varying (vr
);
511 /* Nothing to canonicalize for symbolic ranges. */
512 if (TREE_CODE (min
) != INTEGER_CST
513 || TREE_CODE (max
) != INTEGER_CST
)
515 set_value_range (vr
, t
, min
, max
, equiv
);
519 /* Wrong order for min and max, to swap them and the VR type we need
521 if (tree_int_cst_lt (max
, min
))
525 /* For one bit precision if max < min, then the swapped
526 range covers all values, so for VR_RANGE it is varying and
527 for VR_ANTI_RANGE empty range, so drop to varying as well. */
528 if (TYPE_PRECISION (TREE_TYPE (min
)) == 1)
530 set_value_range_to_varying (vr
);
534 one
= build_int_cst (TREE_TYPE (min
), 1);
535 tmp
= int_const_binop (PLUS_EXPR
, max
, one
);
536 max
= int_const_binop (MINUS_EXPR
, min
, one
);
539 /* There's one corner case, if we had [C+1, C] before we now have
540 that again. But this represents an empty value range, so drop
541 to varying in this case. */
542 if (tree_int_cst_lt (max
, min
))
544 set_value_range_to_varying (vr
);
548 t
= t
== VR_RANGE
? VR_ANTI_RANGE
: VR_RANGE
;
551 /* Anti-ranges that can be represented as ranges should be so. */
552 if (t
== VR_ANTI_RANGE
)
554 bool is_min
= vrp_val_is_min (min
);
555 bool is_max
= vrp_val_is_max (max
);
557 if (is_min
&& is_max
)
559 /* We cannot deal with empty ranges, drop to varying.
560 ??? This could be VR_UNDEFINED instead. */
561 set_value_range_to_varying (vr
);
564 else if (TYPE_PRECISION (TREE_TYPE (min
)) == 1
565 && (is_min
|| is_max
))
567 /* Non-empty boolean ranges can always be represented
568 as a singleton range. */
570 min
= max
= vrp_val_max (TREE_TYPE (min
));
572 min
= max
= vrp_val_min (TREE_TYPE (min
));
576 /* As a special exception preserve non-null ranges. */
577 && !(TYPE_UNSIGNED (TREE_TYPE (min
))
578 && integer_zerop (max
)))
580 tree one
= build_int_cst (TREE_TYPE (max
), 1);
581 min
= int_const_binop (PLUS_EXPR
, max
, one
);
582 max
= vrp_val_max (TREE_TYPE (max
));
587 tree one
= build_int_cst (TREE_TYPE (min
), 1);
588 max
= int_const_binop (MINUS_EXPR
, min
, one
);
589 min
= vrp_val_min (TREE_TYPE (min
));
594 /* Drop [-INF(OVF), +INF(OVF)] to varying. */
595 if (needs_overflow_infinity (TREE_TYPE (min
))
596 && is_overflow_infinity (min
)
597 && is_overflow_infinity (max
))
599 set_value_range_to_varying (vr
);
603 set_value_range (vr
, t
, min
, max
, equiv
);
606 /* Copy value range FROM into value range TO. */
609 copy_value_range (value_range_t
*to
, value_range_t
*from
)
611 set_value_range (to
, from
->type
, from
->min
, from
->max
, from
->equiv
);
614 /* Set value range VR to a single value. This function is only called
615 with values we get from statements, and exists to clear the
616 TREE_OVERFLOW flag so that we don't think we have an overflow
617 infinity when we shouldn't. */
620 set_value_range_to_value (value_range_t
*vr
, tree val
, bitmap equiv
)
622 gcc_assert (is_gimple_min_invariant (val
));
623 if (TREE_OVERFLOW_P (val
))
624 val
= drop_tree_overflow (val
);
625 set_value_range (vr
, VR_RANGE
, val
, val
, equiv
);
628 /* Set value range VR to a non-negative range of type TYPE.
629 OVERFLOW_INFINITY indicates whether to use an overflow infinity
630 rather than TYPE_MAX_VALUE; this should be true if we determine
631 that the range is nonnegative based on the assumption that signed
632 overflow does not occur. */
635 set_value_range_to_nonnegative (value_range_t
*vr
, tree type
,
636 bool overflow_infinity
)
640 if (overflow_infinity
&& !supports_overflow_infinity (type
))
642 set_value_range_to_varying (vr
);
646 zero
= build_int_cst (type
, 0);
647 set_value_range (vr
, VR_RANGE
, zero
,
649 ? positive_overflow_infinity (type
)
650 : TYPE_MAX_VALUE (type
)),
654 /* Set value range VR to a non-NULL range of type TYPE. */
657 set_value_range_to_nonnull (value_range_t
*vr
, tree type
)
659 tree zero
= build_int_cst (type
, 0);
660 set_value_range (vr
, VR_ANTI_RANGE
, zero
, zero
, vr
->equiv
);
664 /* Set value range VR to a NULL range of type TYPE. */
667 set_value_range_to_null (value_range_t
*vr
, tree type
)
669 set_value_range_to_value (vr
, build_int_cst (type
, 0), vr
->equiv
);
673 /* Set value range VR to a range of a truthvalue of type TYPE. */
676 set_value_range_to_truthvalue (value_range_t
*vr
, tree type
)
678 if (TYPE_PRECISION (type
) == 1)
679 set_value_range_to_varying (vr
);
681 set_value_range (vr
, VR_RANGE
,
682 build_int_cst (type
, 0), build_int_cst (type
, 1),
687 /* If abs (min) < abs (max), set VR to [-max, max], if
688 abs (min) >= abs (max), set VR to [-min, min]. */
691 abs_extent_range (value_range_t
*vr
, tree min
, tree max
)
695 gcc_assert (TREE_CODE (min
) == INTEGER_CST
);
696 gcc_assert (TREE_CODE (max
) == INTEGER_CST
);
697 gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (min
)));
698 gcc_assert (!TYPE_UNSIGNED (TREE_TYPE (min
)));
699 min
= fold_unary (ABS_EXPR
, TREE_TYPE (min
), min
);
700 max
= fold_unary (ABS_EXPR
, TREE_TYPE (max
), max
);
701 if (TREE_OVERFLOW (min
) || TREE_OVERFLOW (max
))
703 set_value_range_to_varying (vr
);
706 cmp
= compare_values (min
, max
);
708 min
= fold_unary (NEGATE_EXPR
, TREE_TYPE (min
), max
);
709 else if (cmp
== 0 || cmp
== 1)
712 min
= fold_unary (NEGATE_EXPR
, TREE_TYPE (min
), min
);
716 set_value_range_to_varying (vr
);
719 set_and_canonicalize_value_range (vr
, VR_RANGE
, min
, max
, NULL
);
723 /* Return value range information for VAR.
725 If we have no values ranges recorded (ie, VRP is not running), then
726 return NULL. Otherwise create an empty range if none existed for VAR. */
728 static value_range_t
*
729 get_value_range (const_tree var
)
731 static const struct value_range_d vr_const_varying
732 = { VR_VARYING
, NULL_TREE
, NULL_TREE
, NULL
};
735 unsigned ver
= SSA_NAME_VERSION (var
);
737 /* If we have no recorded ranges, then return NULL. */
741 /* If we query the range for a new SSA name return an unmodifiable VARYING.
742 We should get here at most from the substitute-and-fold stage which
743 will never try to change values. */
744 if (ver
>= num_vr_values
)
745 return CONST_CAST (value_range_t
*, &vr_const_varying
);
751 /* After propagation finished do not allocate new value-ranges. */
752 if (values_propagated
)
753 return CONST_CAST (value_range_t
*, &vr_const_varying
);
755 /* Create a default value range. */
756 vr_value
[ver
] = vr
= XCNEW (value_range_t
);
758 /* Defer allocating the equivalence set. */
761 /* If VAR is a default definition of a parameter, the variable can
762 take any value in VAR's type. */
763 if (SSA_NAME_IS_DEFAULT_DEF (var
))
765 sym
= SSA_NAME_VAR (var
);
766 if (TREE_CODE (sym
) == PARM_DECL
)
768 /* Try to use the "nonnull" attribute to create ~[0, 0]
769 anti-ranges for pointers. Note that this is only valid with
770 default definitions of PARM_DECLs. */
771 if (POINTER_TYPE_P (TREE_TYPE (sym
))
772 && nonnull_arg_p (sym
))
773 set_value_range_to_nonnull (vr
, TREE_TYPE (sym
));
775 set_value_range_to_varying (vr
);
777 else if (TREE_CODE (sym
) == RESULT_DECL
778 && DECL_BY_REFERENCE (sym
))
779 set_value_range_to_nonnull (vr
, TREE_TYPE (sym
));
785 /* Return true, if VAL1 and VAL2 are equal values for VRP purposes. */
788 vrp_operand_equal_p (const_tree val1
, const_tree val2
)
792 if (!val1
|| !val2
|| !operand_equal_p (val1
, val2
, 0))
794 if (is_overflow_infinity (val1
))
795 return is_overflow_infinity (val2
);
799 /* Return true, if the bitmaps B1 and B2 are equal. */
802 vrp_bitmap_equal_p (const_bitmap b1
, const_bitmap b2
)
805 || ((!b1
|| bitmap_empty_p (b1
))
806 && (!b2
|| bitmap_empty_p (b2
)))
808 && bitmap_equal_p (b1
, b2
)));
811 /* Update the value range and equivalence set for variable VAR to
812 NEW_VR. Return true if NEW_VR is different from VAR's previous
815 NOTE: This function assumes that NEW_VR is a temporary value range
816 object created for the sole purpose of updating VAR's range. The
817 storage used by the equivalence set from NEW_VR will be freed by
818 this function. Do not call update_value_range when NEW_VR
819 is the range object associated with another SSA name. */
822 update_value_range (const_tree var
, value_range_t
*new_vr
)
824 value_range_t
*old_vr
;
827 /* Update the value range, if necessary. */
828 old_vr
= get_value_range (var
);
829 is_new
= old_vr
->type
!= new_vr
->type
830 || !vrp_operand_equal_p (old_vr
->min
, new_vr
->min
)
831 || !vrp_operand_equal_p (old_vr
->max
, new_vr
->max
)
832 || !vrp_bitmap_equal_p (old_vr
->equiv
, new_vr
->equiv
);
836 /* Do not allow transitions up the lattice. The following
837 is slightly more awkward than just new_vr->type < old_vr->type
838 because VR_RANGE and VR_ANTI_RANGE need to be considered
839 the same. We may not have is_new when transitioning to
840 UNDEFINED or from VARYING. */
841 if (new_vr
->type
== VR_UNDEFINED
842 || old_vr
->type
== VR_VARYING
)
843 set_value_range_to_varying (old_vr
);
845 set_value_range (old_vr
, new_vr
->type
, new_vr
->min
, new_vr
->max
,
849 BITMAP_FREE (new_vr
->equiv
);
855 /* Add VAR and VAR's equivalence set to EQUIV. This is the central
856 point where equivalence processing can be turned on/off. */
859 add_equivalence (bitmap
*equiv
, const_tree var
)
861 unsigned ver
= SSA_NAME_VERSION (var
);
862 value_range_t
*vr
= vr_value
[ver
];
865 *equiv
= BITMAP_ALLOC (NULL
);
866 bitmap_set_bit (*equiv
, ver
);
868 bitmap_ior_into (*equiv
, vr
->equiv
);
872 /* Return true if VR is ~[0, 0]. */
875 range_is_nonnull (value_range_t
*vr
)
877 return vr
->type
== VR_ANTI_RANGE
878 && integer_zerop (vr
->min
)
879 && integer_zerop (vr
->max
);
883 /* Return true if VR is [0, 0]. */
886 range_is_null (value_range_t
*vr
)
888 return vr
->type
== VR_RANGE
889 && integer_zerop (vr
->min
)
890 && integer_zerop (vr
->max
);
893 /* Return true if max and min of VR are INTEGER_CST. It's not necessary
897 range_int_cst_p (value_range_t
*vr
)
899 return (vr
->type
== VR_RANGE
900 && TREE_CODE (vr
->max
) == INTEGER_CST
901 && TREE_CODE (vr
->min
) == INTEGER_CST
);
904 /* Return true if VR is a INTEGER_CST singleton. */
907 range_int_cst_singleton_p (value_range_t
*vr
)
909 return (range_int_cst_p (vr
)
910 && !is_overflow_infinity (vr
->min
)
911 && !is_overflow_infinity (vr
->max
)
912 && tree_int_cst_equal (vr
->min
, vr
->max
));
915 /* Return true if value range VR involves at least one symbol. */
918 symbolic_range_p (value_range_t
*vr
)
920 return (!is_gimple_min_invariant (vr
->min
)
921 || !is_gimple_min_invariant (vr
->max
));
924 /* Return true if value range VR uses an overflow infinity. */
927 overflow_infinity_range_p (value_range_t
*vr
)
929 return (vr
->type
== VR_RANGE
930 && (is_overflow_infinity (vr
->min
)
931 || is_overflow_infinity (vr
->max
)));
934 /* Return false if we can not make a valid comparison based on VR;
935 this will be the case if it uses an overflow infinity and overflow
936 is not undefined (i.e., -fno-strict-overflow is in effect).
937 Otherwise return true, and set *STRICT_OVERFLOW_P to true if VR
938 uses an overflow infinity. */
941 usable_range_p (value_range_t
*vr
, bool *strict_overflow_p
)
943 gcc_assert (vr
->type
== VR_RANGE
);
944 if (is_overflow_infinity (vr
->min
))
946 *strict_overflow_p
= true;
947 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (vr
->min
)))
950 if (is_overflow_infinity (vr
->max
))
952 *strict_overflow_p
= true;
953 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (vr
->max
)))
960 /* Return true if the result of assignment STMT is know to be non-negative.
961 If the return value is based on the assumption that signed overflow is
962 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
963 *STRICT_OVERFLOW_P.*/
966 gimple_assign_nonnegative_warnv_p (gimple stmt
, bool *strict_overflow_p
)
968 enum tree_code code
= gimple_assign_rhs_code (stmt
);
969 switch (get_gimple_rhs_class (code
))
971 case GIMPLE_UNARY_RHS
:
972 return tree_unary_nonnegative_warnv_p (gimple_assign_rhs_code (stmt
),
973 gimple_expr_type (stmt
),
974 gimple_assign_rhs1 (stmt
),
976 case GIMPLE_BINARY_RHS
:
977 return tree_binary_nonnegative_warnv_p (gimple_assign_rhs_code (stmt
),
978 gimple_expr_type (stmt
),
979 gimple_assign_rhs1 (stmt
),
980 gimple_assign_rhs2 (stmt
),
982 case GIMPLE_TERNARY_RHS
:
984 case GIMPLE_SINGLE_RHS
:
985 return tree_single_nonnegative_warnv_p (gimple_assign_rhs1 (stmt
),
987 case GIMPLE_INVALID_RHS
:
994 /* Return true if return value of call STMT is know to be non-negative.
995 If the return value is based on the assumption that signed overflow is
996 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
997 *STRICT_OVERFLOW_P.*/
1000 gimple_call_nonnegative_warnv_p (gimple stmt
, bool *strict_overflow_p
)
1002 tree arg0
= gimple_call_num_args (stmt
) > 0 ?
1003 gimple_call_arg (stmt
, 0) : NULL_TREE
;
1004 tree arg1
= gimple_call_num_args (stmt
) > 1 ?
1005 gimple_call_arg (stmt
, 1) : NULL_TREE
;
1007 return tree_call_nonnegative_warnv_p (gimple_expr_type (stmt
),
1008 gimple_call_fndecl (stmt
),
1014 /* Return true if STMT is know to to compute a non-negative value.
1015 If the return value is based on the assumption that signed overflow is
1016 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1017 *STRICT_OVERFLOW_P.*/
1020 gimple_stmt_nonnegative_warnv_p (gimple stmt
, bool *strict_overflow_p
)
1022 switch (gimple_code (stmt
))
1025 return gimple_assign_nonnegative_warnv_p (stmt
, strict_overflow_p
);
1027 return gimple_call_nonnegative_warnv_p (stmt
, strict_overflow_p
);
1033 /* Return true if the result of assignment STMT is know to be non-zero.
1034 If the return value is based on the assumption that signed overflow is
1035 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1036 *STRICT_OVERFLOW_P.*/
1039 gimple_assign_nonzero_warnv_p (gimple stmt
, bool *strict_overflow_p
)
1041 enum tree_code code
= gimple_assign_rhs_code (stmt
);
1042 switch (get_gimple_rhs_class (code
))
1044 case GIMPLE_UNARY_RHS
:
1045 return tree_unary_nonzero_warnv_p (gimple_assign_rhs_code (stmt
),
1046 gimple_expr_type (stmt
),
1047 gimple_assign_rhs1 (stmt
),
1049 case GIMPLE_BINARY_RHS
:
1050 return tree_binary_nonzero_warnv_p (gimple_assign_rhs_code (stmt
),
1051 gimple_expr_type (stmt
),
1052 gimple_assign_rhs1 (stmt
),
1053 gimple_assign_rhs2 (stmt
),
1055 case GIMPLE_TERNARY_RHS
:
1057 case GIMPLE_SINGLE_RHS
:
1058 return tree_single_nonzero_warnv_p (gimple_assign_rhs1 (stmt
),
1060 case GIMPLE_INVALID_RHS
:
1067 /* Return true if STMT is known to compute a non-zero value.
1068 If the return value is based on the assumption that signed overflow is
1069 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1070 *STRICT_OVERFLOW_P.*/
1073 gimple_stmt_nonzero_warnv_p (gimple stmt
, bool *strict_overflow_p
)
1075 switch (gimple_code (stmt
))
1078 return gimple_assign_nonzero_warnv_p (stmt
, strict_overflow_p
);
1081 tree fndecl
= gimple_call_fndecl (stmt
);
1082 if (!fndecl
) return false;
1083 if (flag_delete_null_pointer_checks
&& !flag_check_new
1084 && DECL_IS_OPERATOR_NEW (fndecl
)
1085 && !TREE_NOTHROW (fndecl
))
1087 if (flag_delete_null_pointer_checks
&&
1088 lookup_attribute ("returns_nonnull",
1089 TYPE_ATTRIBUTES (gimple_call_fntype (stmt
))))
1091 return gimple_alloca_call_p (stmt
);
1098 /* Like tree_expr_nonzero_warnv_p, but this function uses value ranges
1102 vrp_stmt_computes_nonzero (gimple stmt
, bool *strict_overflow_p
)
1104 if (gimple_stmt_nonzero_warnv_p (stmt
, strict_overflow_p
))
1107 /* If we have an expression of the form &X->a, then the expression
1108 is nonnull if X is nonnull. */
1109 if (is_gimple_assign (stmt
)
1110 && gimple_assign_rhs_code (stmt
) == ADDR_EXPR
)
1112 tree expr
= gimple_assign_rhs1 (stmt
);
1113 tree base
= get_base_address (TREE_OPERAND (expr
, 0));
1115 if (base
!= NULL_TREE
1116 && TREE_CODE (base
) == MEM_REF
1117 && TREE_CODE (TREE_OPERAND (base
, 0)) == SSA_NAME
)
1119 value_range_t
*vr
= get_value_range (TREE_OPERAND (base
, 0));
1120 if (range_is_nonnull (vr
))
1128 /* Returns true if EXPR is a valid value (as expected by compare_values) --
1129 a gimple invariant, or SSA_NAME +- CST. */
1132 valid_value_p (tree expr
)
1134 if (TREE_CODE (expr
) == SSA_NAME
)
1137 if (TREE_CODE (expr
) == PLUS_EXPR
1138 || TREE_CODE (expr
) == MINUS_EXPR
)
1139 return (TREE_CODE (TREE_OPERAND (expr
, 0)) == SSA_NAME
1140 && TREE_CODE (TREE_OPERAND (expr
, 1)) == INTEGER_CST
);
1142 return is_gimple_min_invariant (expr
);
1148 -2 if those are incomparable. */
1150 operand_less_p (tree val
, tree val2
)
1152 /* LT is folded faster than GE and others. Inline the common case. */
1153 if (TREE_CODE (val
) == INTEGER_CST
&& TREE_CODE (val2
) == INTEGER_CST
)
1154 return tree_int_cst_lt (val
, val2
);
1159 fold_defer_overflow_warnings ();
1161 tcmp
= fold_binary_to_constant (LT_EXPR
, boolean_type_node
, val
, val2
);
1163 fold_undefer_and_ignore_overflow_warnings ();
1166 || TREE_CODE (tcmp
) != INTEGER_CST
)
1169 if (!integer_zerop (tcmp
))
1173 /* val >= val2, not considering overflow infinity. */
1174 if (is_negative_overflow_infinity (val
))
1175 return is_negative_overflow_infinity (val2
) ? 0 : 1;
1176 else if (is_positive_overflow_infinity (val2
))
1177 return is_positive_overflow_infinity (val
) ? 0 : 1;
1182 /* Compare two values VAL1 and VAL2. Return
1184 -2 if VAL1 and VAL2 cannot be compared at compile-time,
1187 +1 if VAL1 > VAL2, and
1190 This is similar to tree_int_cst_compare but supports pointer values
1191 and values that cannot be compared at compile time.
1193 If STRICT_OVERFLOW_P is not NULL, then set *STRICT_OVERFLOW_P to
1194 true if the return value is only valid if we assume that signed
1195 overflow is undefined. */
1198 compare_values_warnv (tree val1
, tree val2
, bool *strict_overflow_p
)
1203 /* Below we rely on the fact that VAL1 and VAL2 are both pointers or
1205 gcc_assert (POINTER_TYPE_P (TREE_TYPE (val1
))
1206 == POINTER_TYPE_P (TREE_TYPE (val2
)));
1207 /* Convert the two values into the same type. This is needed because
1208 sizetype causes sign extension even for unsigned types. */
1209 val2
= fold_convert (TREE_TYPE (val1
), val2
);
1210 STRIP_USELESS_TYPE_CONVERSION (val2
);
1212 if ((TREE_CODE (val1
) == SSA_NAME
1213 || TREE_CODE (val1
) == PLUS_EXPR
1214 || TREE_CODE (val1
) == MINUS_EXPR
)
1215 && (TREE_CODE (val2
) == SSA_NAME
1216 || TREE_CODE (val2
) == PLUS_EXPR
1217 || TREE_CODE (val2
) == MINUS_EXPR
))
1219 tree n1
, c1
, n2
, c2
;
1220 enum tree_code code1
, code2
;
1222 /* If VAL1 and VAL2 are of the form 'NAME [+-] CST' or 'NAME',
1223 return -1 or +1 accordingly. If VAL1 and VAL2 don't use the
1224 same name, return -2. */
1225 if (TREE_CODE (val1
) == SSA_NAME
)
1233 code1
= TREE_CODE (val1
);
1234 n1
= TREE_OPERAND (val1
, 0);
1235 c1
= TREE_OPERAND (val1
, 1);
1236 if (tree_int_cst_sgn (c1
) == -1)
1238 if (is_negative_overflow_infinity (c1
))
1240 c1
= fold_unary_to_constant (NEGATE_EXPR
, TREE_TYPE (c1
), c1
);
1243 code1
= code1
== MINUS_EXPR
? PLUS_EXPR
: MINUS_EXPR
;
1247 if (TREE_CODE (val2
) == SSA_NAME
)
1255 code2
= TREE_CODE (val2
);
1256 n2
= TREE_OPERAND (val2
, 0);
1257 c2
= TREE_OPERAND (val2
, 1);
1258 if (tree_int_cst_sgn (c2
) == -1)
1260 if (is_negative_overflow_infinity (c2
))
1262 c2
= fold_unary_to_constant (NEGATE_EXPR
, TREE_TYPE (c2
), c2
);
1265 code2
= code2
== MINUS_EXPR
? PLUS_EXPR
: MINUS_EXPR
;
1269 /* Both values must use the same name. */
1273 if (code1
== SSA_NAME
1274 && code2
== SSA_NAME
)
1278 /* If overflow is defined we cannot simplify more. */
1279 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (val1
)))
1282 if (strict_overflow_p
!= NULL
1283 && (code1
== SSA_NAME
|| !TREE_NO_WARNING (val1
))
1284 && (code2
== SSA_NAME
|| !TREE_NO_WARNING (val2
)))
1285 *strict_overflow_p
= true;
1287 if (code1
== SSA_NAME
)
1289 if (code2
== PLUS_EXPR
)
1290 /* NAME < NAME + CST */
1292 else if (code2
== MINUS_EXPR
)
1293 /* NAME > NAME - CST */
1296 else if (code1
== PLUS_EXPR
)
1298 if (code2
== SSA_NAME
)
1299 /* NAME + CST > NAME */
1301 else if (code2
== PLUS_EXPR
)
1302 /* NAME + CST1 > NAME + CST2, if CST1 > CST2 */
1303 return compare_values_warnv (c1
, c2
, strict_overflow_p
);
1304 else if (code2
== MINUS_EXPR
)
1305 /* NAME + CST1 > NAME - CST2 */
1308 else if (code1
== MINUS_EXPR
)
1310 if (code2
== SSA_NAME
)
1311 /* NAME - CST < NAME */
1313 else if (code2
== PLUS_EXPR
)
1314 /* NAME - CST1 < NAME + CST2 */
1316 else if (code2
== MINUS_EXPR
)
1317 /* NAME - CST1 > NAME - CST2, if CST1 < CST2. Notice that
1318 C1 and C2 are swapped in the call to compare_values. */
1319 return compare_values_warnv (c2
, c1
, strict_overflow_p
);
1325 /* We cannot compare non-constants. */
1326 if (!is_gimple_min_invariant (val1
) || !is_gimple_min_invariant (val2
))
1329 if (!POINTER_TYPE_P (TREE_TYPE (val1
)))
1331 /* We cannot compare overflowed values, except for overflow
1333 if (TREE_OVERFLOW (val1
) || TREE_OVERFLOW (val2
))
1335 if (strict_overflow_p
!= NULL
)
1336 *strict_overflow_p
= true;
1337 if (is_negative_overflow_infinity (val1
))
1338 return is_negative_overflow_infinity (val2
) ? 0 : -1;
1339 else if (is_negative_overflow_infinity (val2
))
1341 else if (is_positive_overflow_infinity (val1
))
1342 return is_positive_overflow_infinity (val2
) ? 0 : 1;
1343 else if (is_positive_overflow_infinity (val2
))
1348 return tree_int_cst_compare (val1
, val2
);
1354 /* First see if VAL1 and VAL2 are not the same. */
1355 if (val1
== val2
|| operand_equal_p (val1
, val2
, 0))
1358 /* If VAL1 is a lower address than VAL2, return -1. */
1359 if (operand_less_p (val1
, val2
) == 1)
1362 /* If VAL1 is a higher address than VAL2, return +1. */
1363 if (operand_less_p (val2
, val1
) == 1)
1366 /* If VAL1 is different than VAL2, return +2.
1367 For integer constants we either have already returned -1 or 1
1368 or they are equivalent. We still might succeed in proving
1369 something about non-trivial operands. */
1370 if (TREE_CODE (val1
) != INTEGER_CST
1371 || TREE_CODE (val2
) != INTEGER_CST
)
1373 t
= fold_binary_to_constant (NE_EXPR
, boolean_type_node
, val1
, val2
);
1374 if (t
&& integer_onep (t
))
1382 /* Compare values like compare_values_warnv, but treat comparisons of
1383 nonconstants which rely on undefined overflow as incomparable. */
1386 compare_values (tree val1
, tree val2
)
1392 ret
= compare_values_warnv (val1
, val2
, &sop
);
1394 && (!is_gimple_min_invariant (val1
) || !is_gimple_min_invariant (val2
)))
1400 /* Return 1 if VAL is inside value range MIN <= VAL <= MAX,
1401 0 if VAL is not inside [MIN, MAX],
1402 -2 if we cannot tell either way.
1404 Benchmark compile/20001226-1.c compilation time after changing this
1408 value_inside_range (tree val
, tree min
, tree max
)
1412 cmp1
= operand_less_p (val
, min
);
1418 cmp2
= operand_less_p (max
, val
);
1426 /* Return true if value ranges VR0 and VR1 have a non-empty
1429 Benchmark compile/20001226-1.c compilation time after changing this
1434 value_ranges_intersect_p (value_range_t
*vr0
, value_range_t
*vr1
)
1436 /* The value ranges do not intersect if the maximum of the first range is
1437 less than the minimum of the second range or vice versa.
1438 When those relations are unknown, we can't do any better. */
1439 if (operand_less_p (vr0
->max
, vr1
->min
) != 0)
1441 if (operand_less_p (vr1
->max
, vr0
->min
) != 0)
1447 /* Return 1 if [MIN, MAX] includes the value zero, 0 if it does not
1448 include the value zero, -2 if we cannot tell. */
1451 range_includes_zero_p (tree min
, tree max
)
1453 tree zero
= build_int_cst (TREE_TYPE (min
), 0);
1454 return value_inside_range (zero
, min
, max
);
1457 /* Return true if *VR is know to only contain nonnegative values. */
1460 value_range_nonnegative_p (value_range_t
*vr
)
1462 /* Testing for VR_ANTI_RANGE is not useful here as any anti-range
1463 which would return a useful value should be encoded as a
1465 if (vr
->type
== VR_RANGE
)
1467 int result
= compare_values (vr
->min
, integer_zero_node
);
1468 return (result
== 0 || result
== 1);
1474 /* If *VR has a value rante that is a single constant value return that,
1475 otherwise return NULL_TREE. */
1478 value_range_constant_singleton (value_range_t
*vr
)
1480 if (vr
->type
== VR_RANGE
1481 && operand_equal_p (vr
->min
, vr
->max
, 0)
1482 && is_gimple_min_invariant (vr
->min
))
1488 /* If OP has a value range with a single constant value return that,
1489 otherwise return NULL_TREE. This returns OP itself if OP is a
1493 op_with_constant_singleton_value_range (tree op
)
1495 if (is_gimple_min_invariant (op
))
1498 if (TREE_CODE (op
) != SSA_NAME
)
1501 return value_range_constant_singleton (get_value_range (op
));
1504 /* Return true if op is in a boolean [0, 1] value-range. */
1507 op_with_boolean_value_range_p (tree op
)
1511 if (TYPE_PRECISION (TREE_TYPE (op
)) == 1)
1514 if (integer_zerop (op
)
1515 || integer_onep (op
))
1518 if (TREE_CODE (op
) != SSA_NAME
)
1521 vr
= get_value_range (op
);
1522 return (vr
->type
== VR_RANGE
1523 && integer_zerop (vr
->min
)
1524 && integer_onep (vr
->max
));
1527 /* Extract value range information from an ASSERT_EXPR EXPR and store
1531 extract_range_from_assert (value_range_t
*vr_p
, tree expr
)
1533 tree var
, cond
, limit
, min
, max
, type
;
1534 value_range_t
*limit_vr
;
1535 enum tree_code cond_code
;
1537 var
= ASSERT_EXPR_VAR (expr
);
1538 cond
= ASSERT_EXPR_COND (expr
);
1540 gcc_assert (COMPARISON_CLASS_P (cond
));
1542 /* Find VAR in the ASSERT_EXPR conditional. */
1543 if (var
== TREE_OPERAND (cond
, 0)
1544 || TREE_CODE (TREE_OPERAND (cond
, 0)) == PLUS_EXPR
1545 || TREE_CODE (TREE_OPERAND (cond
, 0)) == NOP_EXPR
)
1547 /* If the predicate is of the form VAR COMP LIMIT, then we just
1548 take LIMIT from the RHS and use the same comparison code. */
1549 cond_code
= TREE_CODE (cond
);
1550 limit
= TREE_OPERAND (cond
, 1);
1551 cond
= TREE_OPERAND (cond
, 0);
1555 /* If the predicate is of the form LIMIT COMP VAR, then we need
1556 to flip around the comparison code to create the proper range
1558 cond_code
= swap_tree_comparison (TREE_CODE (cond
));
1559 limit
= TREE_OPERAND (cond
, 0);
1560 cond
= TREE_OPERAND (cond
, 1);
1563 limit
= avoid_overflow_infinity (limit
);
1565 type
= TREE_TYPE (var
);
1566 gcc_assert (limit
!= var
);
1568 /* For pointer arithmetic, we only keep track of pointer equality
1570 if (POINTER_TYPE_P (type
) && cond_code
!= NE_EXPR
&& cond_code
!= EQ_EXPR
)
1572 set_value_range_to_varying (vr_p
);
1576 /* If LIMIT is another SSA name and LIMIT has a range of its own,
1577 try to use LIMIT's range to avoid creating symbolic ranges
1579 limit_vr
= (TREE_CODE (limit
) == SSA_NAME
) ? get_value_range (limit
) : NULL
;
1581 /* LIMIT's range is only interesting if it has any useful information. */
1583 && (limit_vr
->type
== VR_UNDEFINED
1584 || limit_vr
->type
== VR_VARYING
1585 || symbolic_range_p (limit_vr
)))
1588 /* Initially, the new range has the same set of equivalences of
1589 VAR's range. This will be revised before returning the final
1590 value. Since assertions may be chained via mutually exclusive
1591 predicates, we will need to trim the set of equivalences before
1593 gcc_assert (vr_p
->equiv
== NULL
);
1594 add_equivalence (&vr_p
->equiv
, var
);
1596 /* Extract a new range based on the asserted comparison for VAR and
1597 LIMIT's value range. Notice that if LIMIT has an anti-range, we
1598 will only use it for equality comparisons (EQ_EXPR). For any
1599 other kind of assertion, we cannot derive a range from LIMIT's
1600 anti-range that can be used to describe the new range. For
1601 instance, ASSERT_EXPR <x_2, x_2 <= b_4>. If b_4 is ~[2, 10],
1602 then b_4 takes on the ranges [-INF, 1] and [11, +INF]. There is
1603 no single range for x_2 that could describe LE_EXPR, so we might
1604 as well build the range [b_4, +INF] for it.
1605 One special case we handle is extracting a range from a
1606 range test encoded as (unsigned)var + CST <= limit. */
1607 if (TREE_CODE (cond
) == NOP_EXPR
1608 || TREE_CODE (cond
) == PLUS_EXPR
)
1610 if (TREE_CODE (cond
) == PLUS_EXPR
)
1612 min
= fold_build1 (NEGATE_EXPR
, TREE_TYPE (TREE_OPERAND (cond
, 1)),
1613 TREE_OPERAND (cond
, 1));
1614 max
= int_const_binop (PLUS_EXPR
, limit
, min
);
1615 cond
= TREE_OPERAND (cond
, 0);
1619 min
= build_int_cst (TREE_TYPE (var
), 0);
1623 /* Make sure to not set TREE_OVERFLOW on the final type
1624 conversion. We are willingly interpreting large positive
1625 unsigned values as negative singed values here. */
1626 min
= force_fit_type (TREE_TYPE (var
), wi::to_widest (min
), 0, false);
1627 max
= force_fit_type (TREE_TYPE (var
), wi::to_widest (max
), 0, false);
1629 /* We can transform a max, min range to an anti-range or
1630 vice-versa. Use set_and_canonicalize_value_range which does
1632 if (cond_code
== LE_EXPR
)
1633 set_and_canonicalize_value_range (vr_p
, VR_RANGE
,
1634 min
, max
, vr_p
->equiv
);
1635 else if (cond_code
== GT_EXPR
)
1636 set_and_canonicalize_value_range (vr_p
, VR_ANTI_RANGE
,
1637 min
, max
, vr_p
->equiv
);
1641 else if (cond_code
== EQ_EXPR
)
1643 enum value_range_type range_type
;
1647 range_type
= limit_vr
->type
;
1648 min
= limit_vr
->min
;
1649 max
= limit_vr
->max
;
1653 range_type
= VR_RANGE
;
1658 set_value_range (vr_p
, range_type
, min
, max
, vr_p
->equiv
);
1660 /* When asserting the equality VAR == LIMIT and LIMIT is another
1661 SSA name, the new range will also inherit the equivalence set
1663 if (TREE_CODE (limit
) == SSA_NAME
)
1664 add_equivalence (&vr_p
->equiv
, limit
);
1666 else if (cond_code
== NE_EXPR
)
1668 /* As described above, when LIMIT's range is an anti-range and
1669 this assertion is an inequality (NE_EXPR), then we cannot
1670 derive anything from the anti-range. For instance, if
1671 LIMIT's range was ~[0, 0], the assertion 'VAR != LIMIT' does
1672 not imply that VAR's range is [0, 0]. So, in the case of
1673 anti-ranges, we just assert the inequality using LIMIT and
1676 If LIMIT_VR is a range, we can only use it to build a new
1677 anti-range if LIMIT_VR is a single-valued range. For
1678 instance, if LIMIT_VR is [0, 1], the predicate
1679 VAR != [0, 1] does not mean that VAR's range is ~[0, 1].
1680 Rather, it means that for value 0 VAR should be ~[0, 0]
1681 and for value 1, VAR should be ~[1, 1]. We cannot
1682 represent these ranges.
1684 The only situation in which we can build a valid
1685 anti-range is when LIMIT_VR is a single-valued range
1686 (i.e., LIMIT_VR->MIN == LIMIT_VR->MAX). In that case,
1687 build the anti-range ~[LIMIT_VR->MIN, LIMIT_VR->MAX]. */
1689 && limit_vr
->type
== VR_RANGE
1690 && compare_values (limit_vr
->min
, limit_vr
->max
) == 0)
1692 min
= limit_vr
->min
;
1693 max
= limit_vr
->max
;
1697 /* In any other case, we cannot use LIMIT's range to build a
1698 valid anti-range. */
1702 /* If MIN and MAX cover the whole range for their type, then
1703 just use the original LIMIT. */
1704 if (INTEGRAL_TYPE_P (type
)
1705 && vrp_val_is_min (min
)
1706 && vrp_val_is_max (max
))
1709 set_and_canonicalize_value_range (vr_p
, VR_ANTI_RANGE
,
1710 min
, max
, vr_p
->equiv
);
1712 else if (cond_code
== LE_EXPR
|| cond_code
== LT_EXPR
)
1714 min
= TYPE_MIN_VALUE (type
);
1716 if (limit_vr
== NULL
|| limit_vr
->type
== VR_ANTI_RANGE
)
1720 /* If LIMIT_VR is of the form [N1, N2], we need to build the
1721 range [MIN, N2] for LE_EXPR and [MIN, N2 - 1] for
1723 max
= limit_vr
->max
;
1726 /* If the maximum value forces us to be out of bounds, simply punt.
1727 It would be pointless to try and do anything more since this
1728 all should be optimized away above us. */
1729 if ((cond_code
== LT_EXPR
1730 && compare_values (max
, min
) == 0)
1731 || is_overflow_infinity (max
))
1732 set_value_range_to_varying (vr_p
);
1735 /* For LT_EXPR, we create the range [MIN, MAX - 1]. */
1736 if (cond_code
== LT_EXPR
)
1738 if (TYPE_PRECISION (TREE_TYPE (max
)) == 1
1739 && !TYPE_UNSIGNED (TREE_TYPE (max
)))
1740 max
= fold_build2 (PLUS_EXPR
, TREE_TYPE (max
), max
,
1741 build_int_cst (TREE_TYPE (max
), -1));
1743 max
= fold_build2 (MINUS_EXPR
, TREE_TYPE (max
), max
,
1744 build_int_cst (TREE_TYPE (max
), 1));
1746 TREE_NO_WARNING (max
) = 1;
1749 set_value_range (vr_p
, VR_RANGE
, min
, max
, vr_p
->equiv
);
1752 else if (cond_code
== GE_EXPR
|| cond_code
== GT_EXPR
)
1754 max
= TYPE_MAX_VALUE (type
);
1756 if (limit_vr
== NULL
|| limit_vr
->type
== VR_ANTI_RANGE
)
1760 /* If LIMIT_VR is of the form [N1, N2], we need to build the
1761 range [N1, MAX] for GE_EXPR and [N1 + 1, MAX] for
1763 min
= limit_vr
->min
;
1766 /* If the minimum value forces us to be out of bounds, simply punt.
1767 It would be pointless to try and do anything more since this
1768 all should be optimized away above us. */
1769 if ((cond_code
== GT_EXPR
1770 && compare_values (min
, max
) == 0)
1771 || is_overflow_infinity (min
))
1772 set_value_range_to_varying (vr_p
);
1775 /* For GT_EXPR, we create the range [MIN + 1, MAX]. */
1776 if (cond_code
== GT_EXPR
)
1778 if (TYPE_PRECISION (TREE_TYPE (min
)) == 1
1779 && !TYPE_UNSIGNED (TREE_TYPE (min
)))
1780 min
= fold_build2 (MINUS_EXPR
, TREE_TYPE (min
), min
,
1781 build_int_cst (TREE_TYPE (min
), -1));
1783 min
= fold_build2 (PLUS_EXPR
, TREE_TYPE (min
), min
,
1784 build_int_cst (TREE_TYPE (min
), 1));
1786 TREE_NO_WARNING (min
) = 1;
1789 set_value_range (vr_p
, VR_RANGE
, min
, max
, vr_p
->equiv
);
1795 /* Finally intersect the new range with what we already know about var. */
1796 vrp_intersect_ranges (vr_p
, get_value_range (var
));
1800 /* Extract range information from SSA name VAR and store it in VR. If
1801 VAR has an interesting range, use it. Otherwise, create the
1802 range [VAR, VAR] and return it. This is useful in situations where
1803 we may have conditionals testing values of VARYING names. For
1810 Even if y_5 is deemed VARYING, we can determine that x_3 > y_5 is
1814 extract_range_from_ssa_name (value_range_t
*vr
, tree var
)
1816 value_range_t
*var_vr
= get_value_range (var
);
1818 if (var_vr
->type
!= VR_UNDEFINED
&& var_vr
->type
!= VR_VARYING
)
1819 copy_value_range (vr
, var_vr
);
1821 set_value_range (vr
, VR_RANGE
, var
, var
, NULL
);
1823 add_equivalence (&vr
->equiv
, var
);
1827 /* Wrapper around int_const_binop. If the operation overflows and we
1828 are not using wrapping arithmetic, then adjust the result to be
1829 -INF or +INF depending on CODE, VAL1 and VAL2. This can return
1830 NULL_TREE if we need to use an overflow infinity representation but
1831 the type does not support it. */
1834 vrp_int_const_binop (enum tree_code code
, tree val1
, tree val2
)
1838 res
= int_const_binop (code
, val1
, val2
);
1840 /* If we are using unsigned arithmetic, operate symbolically
1841 on -INF and +INF as int_const_binop only handles signed overflow. */
1842 if (TYPE_UNSIGNED (TREE_TYPE (val1
)))
1844 int checkz
= compare_values (res
, val1
);
1845 bool overflow
= false;
1847 /* Ensure that res = val1 [+*] val2 >= val1
1848 or that res = val1 - val2 <= val1. */
1849 if ((code
== PLUS_EXPR
1850 && !(checkz
== 1 || checkz
== 0))
1851 || (code
== MINUS_EXPR
1852 && !(checkz
== 0 || checkz
== -1)))
1856 /* Checking for multiplication overflow is done by dividing the
1857 output of the multiplication by the first input of the
1858 multiplication. If the result of that division operation is
1859 not equal to the second input of the multiplication, then the
1860 multiplication overflowed. */
1861 else if (code
== MULT_EXPR
&& !integer_zerop (val1
))
1863 tree tmp
= int_const_binop (TRUNC_DIV_EXPR
,
1866 int check
= compare_values (tmp
, val2
);
1874 res
= copy_node (res
);
1875 TREE_OVERFLOW (res
) = 1;
1879 else if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (val1
)))
1880 /* If the singed operation wraps then int_const_binop has done
1881 everything we want. */
1883 /* Signed division of -1/0 overflows and by the time it gets here
1884 returns NULL_TREE. */
1887 else if ((TREE_OVERFLOW (res
)
1888 && !TREE_OVERFLOW (val1
)
1889 && !TREE_OVERFLOW (val2
))
1890 || is_overflow_infinity (val1
)
1891 || is_overflow_infinity (val2
))
1893 /* If the operation overflowed but neither VAL1 nor VAL2 are
1894 overflown, return -INF or +INF depending on the operation
1895 and the combination of signs of the operands. */
1896 int sgn1
= tree_int_cst_sgn (val1
);
1897 int sgn2
= tree_int_cst_sgn (val2
);
1899 if (needs_overflow_infinity (TREE_TYPE (res
))
1900 && !supports_overflow_infinity (TREE_TYPE (res
)))
1903 /* We have to punt on adding infinities of different signs,
1904 since we can't tell what the sign of the result should be.
1905 Likewise for subtracting infinities of the same sign. */
1906 if (((code
== PLUS_EXPR
&& sgn1
!= sgn2
)
1907 || (code
== MINUS_EXPR
&& sgn1
== sgn2
))
1908 && is_overflow_infinity (val1
)
1909 && is_overflow_infinity (val2
))
1912 /* Don't try to handle division or shifting of infinities. */
1913 if ((code
== TRUNC_DIV_EXPR
1914 || code
== FLOOR_DIV_EXPR
1915 || code
== CEIL_DIV_EXPR
1916 || code
== EXACT_DIV_EXPR
1917 || code
== ROUND_DIV_EXPR
1918 || code
== RSHIFT_EXPR
)
1919 && (is_overflow_infinity (val1
)
1920 || is_overflow_infinity (val2
)))
1923 /* Notice that we only need to handle the restricted set of
1924 operations handled by extract_range_from_binary_expr.
1925 Among them, only multiplication, addition and subtraction
1926 can yield overflow without overflown operands because we
1927 are working with integral types only... except in the
1928 case VAL1 = -INF and VAL2 = -1 which overflows to +INF
1929 for division too. */
1931 /* For multiplication, the sign of the overflow is given
1932 by the comparison of the signs of the operands. */
1933 if ((code
== MULT_EXPR
&& sgn1
== sgn2
)
1934 /* For addition, the operands must be of the same sign
1935 to yield an overflow. Its sign is therefore that
1936 of one of the operands, for example the first. For
1937 infinite operands X + -INF is negative, not positive. */
1938 || (code
== PLUS_EXPR
1940 ? !is_negative_overflow_infinity (val2
)
1941 : is_positive_overflow_infinity (val2
)))
1942 /* For subtraction, non-infinite operands must be of
1943 different signs to yield an overflow. Its sign is
1944 therefore that of the first operand or the opposite of
1945 that of the second operand. A first operand of 0 counts
1946 as positive here, for the corner case 0 - (-INF), which
1947 overflows, but must yield +INF. For infinite operands 0
1948 - INF is negative, not positive. */
1949 || (code
== MINUS_EXPR
1951 ? !is_positive_overflow_infinity (val2
)
1952 : is_negative_overflow_infinity (val2
)))
1953 /* We only get in here with positive shift count, so the
1954 overflow direction is the same as the sign of val1.
1955 Actually rshift does not overflow at all, but we only
1956 handle the case of shifting overflowed -INF and +INF. */
1957 || (code
== RSHIFT_EXPR
1959 /* For division, the only case is -INF / -1 = +INF. */
1960 || code
== TRUNC_DIV_EXPR
1961 || code
== FLOOR_DIV_EXPR
1962 || code
== CEIL_DIV_EXPR
1963 || code
== EXACT_DIV_EXPR
1964 || code
== ROUND_DIV_EXPR
)
1965 return (needs_overflow_infinity (TREE_TYPE (res
))
1966 ? positive_overflow_infinity (TREE_TYPE (res
))
1967 : TYPE_MAX_VALUE (TREE_TYPE (res
)));
1969 return (needs_overflow_infinity (TREE_TYPE (res
))
1970 ? negative_overflow_infinity (TREE_TYPE (res
))
1971 : TYPE_MIN_VALUE (TREE_TYPE (res
)));
1978 /* For range VR compute two wide_int bitmasks. In *MAY_BE_NONZERO
1979 bitmask if some bit is unset, it means for all numbers in the range
1980 the bit is 0, otherwise it might be 0 or 1. In *MUST_BE_NONZERO
1981 bitmask if some bit is set, it means for all numbers in the range
1982 the bit is 1, otherwise it might be 0 or 1. */
1985 zero_nonzero_bits_from_vr (const tree expr_type
,
1987 wide_int
*may_be_nonzero
,
1988 wide_int
*must_be_nonzero
)
1990 *may_be_nonzero
= wi::minus_one (TYPE_PRECISION (expr_type
));
1991 *must_be_nonzero
= wi::zero (TYPE_PRECISION (expr_type
));
1992 if (!range_int_cst_p (vr
)
1993 || is_overflow_infinity (vr
->min
)
1994 || is_overflow_infinity (vr
->max
))
1997 if (range_int_cst_singleton_p (vr
))
1999 *may_be_nonzero
= vr
->min
;
2000 *must_be_nonzero
= *may_be_nonzero
;
2002 else if (tree_int_cst_sgn (vr
->min
) >= 0
2003 || tree_int_cst_sgn (vr
->max
) < 0)
2005 wide_int xor_mask
= wi::bit_xor (vr
->min
, vr
->max
);
2006 *may_be_nonzero
= wi::bit_or (vr
->min
, vr
->max
);
2007 *must_be_nonzero
= wi::bit_and (vr
->min
, vr
->max
);
2010 wide_int mask
= wi::mask (wi::floor_log2 (xor_mask
), false,
2011 (*may_be_nonzero
).get_precision ());
2012 *may_be_nonzero
= (*may_be_nonzero
) | mask
;
2013 *must_be_nonzero
= (*must_be_nonzero
).and_not (mask
);
2020 /* Create two value-ranges in *VR0 and *VR1 from the anti-range *AR
2021 so that *VR0 U *VR1 == *AR. Returns true if that is possible,
2022 false otherwise. If *AR can be represented with a single range
2023 *VR1 will be VR_UNDEFINED. */
2026 ranges_from_anti_range (value_range_t
*ar
,
2027 value_range_t
*vr0
, value_range_t
*vr1
)
2029 tree type
= TREE_TYPE (ar
->min
);
2031 vr0
->type
= VR_UNDEFINED
;
2032 vr1
->type
= VR_UNDEFINED
;
2034 if (ar
->type
!= VR_ANTI_RANGE
2035 || TREE_CODE (ar
->min
) != INTEGER_CST
2036 || TREE_CODE (ar
->max
) != INTEGER_CST
2037 || !vrp_val_min (type
)
2038 || !vrp_val_max (type
))
2041 if (!vrp_val_is_min (ar
->min
))
2043 vr0
->type
= VR_RANGE
;
2044 vr0
->min
= vrp_val_min (type
);
2045 vr0
->max
= wide_int_to_tree (type
, wi::sub (ar
->min
, 1));
2047 if (!vrp_val_is_max (ar
->max
))
2049 vr1
->type
= VR_RANGE
;
2050 vr1
->min
= wide_int_to_tree (type
, wi::add (ar
->max
, 1));
2051 vr1
->max
= vrp_val_max (type
);
2053 if (vr0
->type
== VR_UNDEFINED
)
2056 vr1
->type
= VR_UNDEFINED
;
2059 return vr0
->type
!= VR_UNDEFINED
;
2062 /* Helper to extract a value-range *VR for a multiplicative operation
2066 extract_range_from_multiplicative_op_1 (value_range_t
*vr
,
2067 enum tree_code code
,
2068 value_range_t
*vr0
, value_range_t
*vr1
)
2070 enum value_range_type type
;
2077 /* Multiplications, divisions and shifts are a bit tricky to handle,
2078 depending on the mix of signs we have in the two ranges, we
2079 need to operate on different values to get the minimum and
2080 maximum values for the new range. One approach is to figure
2081 out all the variations of range combinations and do the
2084 However, this involves several calls to compare_values and it
2085 is pretty convoluted. It's simpler to do the 4 operations
2086 (MIN0 OP MIN1, MIN0 OP MAX1, MAX0 OP MIN1 and MAX0 OP MAX0 OP
2087 MAX1) and then figure the smallest and largest values to form
2089 gcc_assert (code
== MULT_EXPR
2090 || code
== TRUNC_DIV_EXPR
2091 || code
== FLOOR_DIV_EXPR
2092 || code
== CEIL_DIV_EXPR
2093 || code
== EXACT_DIV_EXPR
2094 || code
== ROUND_DIV_EXPR
2095 || code
== RSHIFT_EXPR
2096 || code
== LSHIFT_EXPR
);
2097 gcc_assert ((vr0
->type
== VR_RANGE
2098 || (code
== MULT_EXPR
&& vr0
->type
== VR_ANTI_RANGE
))
2099 && vr0
->type
== vr1
->type
);
2103 /* Compute the 4 cross operations. */
2105 val
[0] = vrp_int_const_binop (code
, vr0
->min
, vr1
->min
);
2106 if (val
[0] == NULL_TREE
)
2109 if (vr1
->max
== vr1
->min
)
2113 val
[1] = vrp_int_const_binop (code
, vr0
->min
, vr1
->max
);
2114 if (val
[1] == NULL_TREE
)
2118 if (vr0
->max
== vr0
->min
)
2122 val
[2] = vrp_int_const_binop (code
, vr0
->max
, vr1
->min
);
2123 if (val
[2] == NULL_TREE
)
2127 if (vr0
->min
== vr0
->max
|| vr1
->min
== vr1
->max
)
2131 val
[3] = vrp_int_const_binop (code
, vr0
->max
, vr1
->max
);
2132 if (val
[3] == NULL_TREE
)
2138 set_value_range_to_varying (vr
);
2142 /* Set MIN to the minimum of VAL[i] and MAX to the maximum
2146 for (i
= 1; i
< 4; i
++)
2148 if (!is_gimple_min_invariant (min
)
2149 || (TREE_OVERFLOW (min
) && !is_overflow_infinity (min
))
2150 || !is_gimple_min_invariant (max
)
2151 || (TREE_OVERFLOW (max
) && !is_overflow_infinity (max
)))
2156 if (!is_gimple_min_invariant (val
[i
])
2157 || (TREE_OVERFLOW (val
[i
])
2158 && !is_overflow_infinity (val
[i
])))
2160 /* If we found an overflowed value, set MIN and MAX
2161 to it so that we set the resulting range to
2167 if (compare_values (val
[i
], min
) == -1)
2170 if (compare_values (val
[i
], max
) == 1)
2175 /* If either MIN or MAX overflowed, then set the resulting range to
2176 VARYING. But we do accept an overflow infinity
2178 if (min
== NULL_TREE
2179 || !is_gimple_min_invariant (min
)
2180 || (TREE_OVERFLOW (min
) && !is_overflow_infinity (min
))
2182 || !is_gimple_min_invariant (max
)
2183 || (TREE_OVERFLOW (max
) && !is_overflow_infinity (max
)))
2185 set_value_range_to_varying (vr
);
2191 2) [-INF, +-INF(OVF)]
2192 3) [+-INF(OVF), +INF]
2193 4) [+-INF(OVF), +-INF(OVF)]
2194 We learn nothing when we have INF and INF(OVF) on both sides.
2195 Note that we do accept [-INF, -INF] and [+INF, +INF] without
2197 if ((vrp_val_is_min (min
) || is_overflow_infinity (min
))
2198 && (vrp_val_is_max (max
) || is_overflow_infinity (max
)))
2200 set_value_range_to_varying (vr
);
2204 cmp
= compare_values (min
, max
);
2205 if (cmp
== -2 || cmp
== 1)
2207 /* If the new range has its limits swapped around (MIN > MAX),
2208 then the operation caused one of them to wrap around, mark
2209 the new range VARYING. */
2210 set_value_range_to_varying (vr
);
2213 set_value_range (vr
, type
, min
, max
, NULL
);
2216 /* Extract range information from a binary operation CODE based on
2217 the ranges of each of its operands, *VR0 and *VR1 with resulting
2218 type EXPR_TYPE. The resulting range is stored in *VR. */
2221 extract_range_from_binary_expr_1 (value_range_t
*vr
,
2222 enum tree_code code
, tree expr_type
,
2223 value_range_t
*vr0_
, value_range_t
*vr1_
)
2225 value_range_t vr0
= *vr0_
, vr1
= *vr1_
;
2226 value_range_t vrtem0
= VR_INITIALIZER
, vrtem1
= VR_INITIALIZER
;
2227 enum value_range_type type
;
2228 tree min
= NULL_TREE
, max
= NULL_TREE
;
2231 if (!INTEGRAL_TYPE_P (expr_type
)
2232 && !POINTER_TYPE_P (expr_type
))
2234 set_value_range_to_varying (vr
);
2238 /* Not all binary expressions can be applied to ranges in a
2239 meaningful way. Handle only arithmetic operations. */
2240 if (code
!= PLUS_EXPR
2241 && code
!= MINUS_EXPR
2242 && code
!= POINTER_PLUS_EXPR
2243 && code
!= MULT_EXPR
2244 && code
!= TRUNC_DIV_EXPR
2245 && code
!= FLOOR_DIV_EXPR
2246 && code
!= CEIL_DIV_EXPR
2247 && code
!= EXACT_DIV_EXPR
2248 && code
!= ROUND_DIV_EXPR
2249 && code
!= TRUNC_MOD_EXPR
2250 && code
!= RSHIFT_EXPR
2251 && code
!= LSHIFT_EXPR
2254 && code
!= BIT_AND_EXPR
2255 && code
!= BIT_IOR_EXPR
2256 && code
!= BIT_XOR_EXPR
)
2258 set_value_range_to_varying (vr
);
2262 /* If both ranges are UNDEFINED, so is the result. */
2263 if (vr0
.type
== VR_UNDEFINED
&& vr1
.type
== VR_UNDEFINED
)
2265 set_value_range_to_undefined (vr
);
2268 /* If one of the ranges is UNDEFINED drop it to VARYING for the following
2269 code. At some point we may want to special-case operations that
2270 have UNDEFINED result for all or some value-ranges of the not UNDEFINED
2272 else if (vr0
.type
== VR_UNDEFINED
)
2273 set_value_range_to_varying (&vr0
);
2274 else if (vr1
.type
== VR_UNDEFINED
)
2275 set_value_range_to_varying (&vr1
);
2277 /* Now canonicalize anti-ranges to ranges when they are not symbolic
2278 and express ~[] op X as ([]' op X) U ([]'' op X). */
2279 if (vr0
.type
== VR_ANTI_RANGE
2280 && ranges_from_anti_range (&vr0
, &vrtem0
, &vrtem1
))
2282 extract_range_from_binary_expr_1 (vr
, code
, expr_type
, &vrtem0
, vr1_
);
2283 if (vrtem1
.type
!= VR_UNDEFINED
)
2285 value_range_t vrres
= VR_INITIALIZER
;
2286 extract_range_from_binary_expr_1 (&vrres
, code
, expr_type
,
2288 vrp_meet (vr
, &vrres
);
2292 /* Likewise for X op ~[]. */
2293 if (vr1
.type
== VR_ANTI_RANGE
2294 && ranges_from_anti_range (&vr1
, &vrtem0
, &vrtem1
))
2296 extract_range_from_binary_expr_1 (vr
, code
, expr_type
, vr0_
, &vrtem0
);
2297 if (vrtem1
.type
!= VR_UNDEFINED
)
2299 value_range_t vrres
= VR_INITIALIZER
;
2300 extract_range_from_binary_expr_1 (&vrres
, code
, expr_type
,
2302 vrp_meet (vr
, &vrres
);
2307 /* The type of the resulting value range defaults to VR0.TYPE. */
2310 /* Refuse to operate on VARYING ranges, ranges of different kinds
2311 and symbolic ranges. As an exception, we allow BIT_AND_EXPR
2312 because we may be able to derive a useful range even if one of
2313 the operands is VR_VARYING or symbolic range. Similarly for
2314 divisions. TODO, we may be able to derive anti-ranges in
2316 if (code
!= BIT_AND_EXPR
2317 && code
!= BIT_IOR_EXPR
2318 && code
!= TRUNC_DIV_EXPR
2319 && code
!= FLOOR_DIV_EXPR
2320 && code
!= CEIL_DIV_EXPR
2321 && code
!= EXACT_DIV_EXPR
2322 && code
!= ROUND_DIV_EXPR
2323 && code
!= TRUNC_MOD_EXPR
2326 && (vr0
.type
== VR_VARYING
2327 || vr1
.type
== VR_VARYING
2328 || vr0
.type
!= vr1
.type
2329 || symbolic_range_p (&vr0
)
2330 || symbolic_range_p (&vr1
)))
2332 set_value_range_to_varying (vr
);
2336 /* Now evaluate the expression to determine the new range. */
2337 if (POINTER_TYPE_P (expr_type
))
2339 if (code
== MIN_EXPR
|| code
== MAX_EXPR
)
2341 /* For MIN/MAX expressions with pointers, we only care about
2342 nullness, if both are non null, then the result is nonnull.
2343 If both are null, then the result is null. Otherwise they
2345 if (range_is_nonnull (&vr0
) && range_is_nonnull (&vr1
))
2346 set_value_range_to_nonnull (vr
, expr_type
);
2347 else if (range_is_null (&vr0
) && range_is_null (&vr1
))
2348 set_value_range_to_null (vr
, expr_type
);
2350 set_value_range_to_varying (vr
);
2352 else if (code
== POINTER_PLUS_EXPR
)
2354 /* For pointer types, we are really only interested in asserting
2355 whether the expression evaluates to non-NULL. */
2356 if (range_is_nonnull (&vr0
) || range_is_nonnull (&vr1
))
2357 set_value_range_to_nonnull (vr
, expr_type
);
2358 else if (range_is_null (&vr0
) && range_is_null (&vr1
))
2359 set_value_range_to_null (vr
, expr_type
);
2361 set_value_range_to_varying (vr
);
2363 else if (code
== BIT_AND_EXPR
)
2365 /* For pointer types, we are really only interested in asserting
2366 whether the expression evaluates to non-NULL. */
2367 if (range_is_nonnull (&vr0
) && range_is_nonnull (&vr1
))
2368 set_value_range_to_nonnull (vr
, expr_type
);
2369 else if (range_is_null (&vr0
) || range_is_null (&vr1
))
2370 set_value_range_to_null (vr
, expr_type
);
2372 set_value_range_to_varying (vr
);
2375 set_value_range_to_varying (vr
);
2380 /* For integer ranges, apply the operation to each end of the
2381 range and see what we end up with. */
2382 if (code
== PLUS_EXPR
|| code
== MINUS_EXPR
)
2384 /* If we have a PLUS_EXPR with two VR_RANGE integer constant
2385 ranges compute the precise range for such case if possible. */
2386 if (range_int_cst_p (&vr0
)
2387 && range_int_cst_p (&vr1
))
2389 signop sgn
= TYPE_SIGN (expr_type
);
2390 unsigned int prec
= TYPE_PRECISION (expr_type
);
2391 wide_int type_min
= wi::min_value (TYPE_PRECISION (expr_type
), sgn
);
2392 wide_int type_max
= wi::max_value (TYPE_PRECISION (expr_type
), sgn
);
2393 wide_int wmin
, wmax
;
2397 if (code
== PLUS_EXPR
)
2399 wmin
= wi::add (vr0
.min
, vr1
.min
);
2400 wmax
= wi::add (vr0
.max
, vr1
.max
);
2402 /* Check for overflow. */
2403 if (wi::cmp (vr1
.min
, 0, sgn
) != wi::cmp (wmin
, vr0
.min
, sgn
))
2404 min_ovf
= wi::cmp (vr0
.min
, wmin
, sgn
);
2405 if (wi::cmp (vr1
.max
, 0, sgn
) != wi::cmp (wmax
, vr0
.max
, sgn
))
2406 max_ovf
= wi::cmp (vr0
.max
, wmax
, sgn
);
2408 else /* if (code == MINUS_EXPR) */
2410 wmin
= wi::sub (vr0
.min
, vr1
.max
);
2411 wmax
= wi::sub (vr0
.max
, vr1
.min
);
2413 if (wi::cmp (0, vr1
.max
, sgn
) != wi::cmp (wmin
, vr0
.min
, sgn
))
2414 min_ovf
= wi::cmp (vr0
.min
, vr1
.max
, sgn
);
2415 if (wi::cmp (0, vr1
.min
, sgn
) != wi::cmp (wmax
, vr0
.max
, sgn
))
2416 max_ovf
= wi::cmp (vr0
.max
, vr1
.min
, sgn
);
2419 /* For non-wrapping arithmetic look at possibly smaller
2420 value-ranges of the type. */
2421 if (!TYPE_OVERFLOW_WRAPS (expr_type
))
2423 if (vrp_val_min (expr_type
))
2424 type_min
= vrp_val_min (expr_type
);
2425 if (vrp_val_max (expr_type
))
2426 type_max
= vrp_val_max (expr_type
);
2429 /* Check for type overflow. */
2432 if (wi::cmp (wmin
, type_min
, sgn
) == -1)
2434 else if (wi::cmp (wmin
, type_max
, sgn
) == 1)
2439 if (wi::cmp (wmax
, type_min
, sgn
) == -1)
2441 else if (wi::cmp (wmax
, type_max
, sgn
) == 1)
2445 if (TYPE_OVERFLOW_WRAPS (expr_type
))
2447 /* If overflow wraps, truncate the values and adjust the
2448 range kind and bounds appropriately. */
2449 wide_int tmin
= wide_int::from (wmin
, prec
, sgn
);
2450 wide_int tmax
= wide_int::from (wmax
, prec
, sgn
);
2451 if (min_ovf
== max_ovf
)
2453 /* No overflow or both overflow or underflow. The
2454 range kind stays VR_RANGE. */
2455 min
= wide_int_to_tree (expr_type
, tmin
);
2456 max
= wide_int_to_tree (expr_type
, tmax
);
2458 else if (min_ovf
== -1
2461 /* Underflow and overflow, drop to VR_VARYING. */
2462 set_value_range_to_varying (vr
);
2467 /* Min underflow or max overflow. The range kind
2468 changes to VR_ANTI_RANGE. */
2469 bool covers
= false;
2470 wide_int tem
= tmin
;
2471 gcc_assert ((min_ovf
== -1 && max_ovf
== 0)
2472 || (max_ovf
== 1 && min_ovf
== 0));
2473 type
= VR_ANTI_RANGE
;
2475 if (wi::cmp (tmin
, tmax
, sgn
) < 0)
2478 if (wi::cmp (tmax
, tem
, sgn
) > 0)
2480 /* If the anti-range would cover nothing, drop to varying.
2481 Likewise if the anti-range bounds are outside of the
2483 if (covers
|| wi::cmp (tmin
, tmax
, sgn
) > 0)
2485 set_value_range_to_varying (vr
);
2488 min
= wide_int_to_tree (expr_type
, tmin
);
2489 max
= wide_int_to_tree (expr_type
, tmax
);
2494 /* If overflow does not wrap, saturate to the types min/max
2498 if (needs_overflow_infinity (expr_type
)
2499 && supports_overflow_infinity (expr_type
))
2500 min
= negative_overflow_infinity (expr_type
);
2502 min
= wide_int_to_tree (expr_type
, type_min
);
2504 else if (min_ovf
== 1)
2506 if (needs_overflow_infinity (expr_type
)
2507 && supports_overflow_infinity (expr_type
))
2508 min
= positive_overflow_infinity (expr_type
);
2510 min
= wide_int_to_tree (expr_type
, type_max
);
2513 min
= wide_int_to_tree (expr_type
, wmin
);
2517 if (needs_overflow_infinity (expr_type
)
2518 && supports_overflow_infinity (expr_type
))
2519 max
= negative_overflow_infinity (expr_type
);
2521 max
= wide_int_to_tree (expr_type
, type_min
);
2523 else if (max_ovf
== 1)
2525 if (needs_overflow_infinity (expr_type
)
2526 && supports_overflow_infinity (expr_type
))
2527 max
= positive_overflow_infinity (expr_type
);
2529 max
= wide_int_to_tree (expr_type
, type_max
);
2532 max
= wide_int_to_tree (expr_type
, wmax
);
2534 if (needs_overflow_infinity (expr_type
)
2535 && supports_overflow_infinity (expr_type
))
2537 if (is_negative_overflow_infinity (vr0
.min
)
2538 || (code
== PLUS_EXPR
2539 ? is_negative_overflow_infinity (vr1
.min
)
2540 : is_positive_overflow_infinity (vr1
.max
)))
2541 min
= negative_overflow_infinity (expr_type
);
2542 if (is_positive_overflow_infinity (vr0
.max
)
2543 || (code
== PLUS_EXPR
2544 ? is_positive_overflow_infinity (vr1
.max
)
2545 : is_negative_overflow_infinity (vr1
.min
)))
2546 max
= positive_overflow_infinity (expr_type
);
2551 /* For other cases, for example if we have a PLUS_EXPR with two
2552 VR_ANTI_RANGEs, drop to VR_VARYING. It would take more effort
2553 to compute a precise range for such a case.
2554 ??? General even mixed range kind operations can be expressed
2555 by for example transforming ~[3, 5] + [1, 2] to range-only
2556 operations and a union primitive:
2557 [-INF, 2] + [1, 2] U [5, +INF] + [1, 2]
2558 [-INF+1, 4] U [6, +INF(OVF)]
2559 though usually the union is not exactly representable with
2560 a single range or anti-range as the above is
2561 [-INF+1, +INF(OVF)] intersected with ~[5, 5]
2562 but one could use a scheme similar to equivalences for this. */
2563 set_value_range_to_varying (vr
);
2567 else if (code
== MIN_EXPR
2568 || code
== MAX_EXPR
)
2570 if (vr0
.type
== VR_RANGE
2571 && !symbolic_range_p (&vr0
))
2574 if (vr1
.type
== VR_RANGE
2575 && !symbolic_range_p (&vr1
))
2577 /* For operations that make the resulting range directly
2578 proportional to the original ranges, apply the operation to
2579 the same end of each range. */
2580 min
= vrp_int_const_binop (code
, vr0
.min
, vr1
.min
);
2581 max
= vrp_int_const_binop (code
, vr0
.max
, vr1
.max
);
2583 else if (code
== MIN_EXPR
)
2585 min
= vrp_val_min (expr_type
);
2588 else if (code
== MAX_EXPR
)
2591 max
= vrp_val_max (expr_type
);
2594 else if (vr1
.type
== VR_RANGE
2595 && !symbolic_range_p (&vr1
))
2598 if (code
== MIN_EXPR
)
2600 min
= vrp_val_min (expr_type
);
2603 else if (code
== MAX_EXPR
)
2606 max
= vrp_val_max (expr_type
);
2611 set_value_range_to_varying (vr
);
2615 else if (code
== MULT_EXPR
)
2617 /* Fancy code so that with unsigned, [-3,-1]*[-3,-1] does not
2618 drop to varying. This test requires 2*prec bits if both
2619 operands are signed and 2*prec + 2 bits if either is not. */
2621 signop sign
= TYPE_SIGN (expr_type
);
2622 unsigned int prec
= TYPE_PRECISION (expr_type
);
2624 if (range_int_cst_p (&vr0
)
2625 && range_int_cst_p (&vr1
)
2626 && TYPE_OVERFLOW_WRAPS (expr_type
))
2628 typedef FIXED_WIDE_INT (WIDE_INT_MAX_PRECISION
* 2) vrp_int
;
2629 typedef generic_wide_int
2630 <wi::extended_tree
<WIDE_INT_MAX_PRECISION
* 2> > vrp_int_cst
;
2631 vrp_int sizem1
= wi::mask
<vrp_int
> (prec
, false);
2632 vrp_int size
= sizem1
+ 1;
2634 /* Extend the values using the sign of the result to PREC2.
2635 From here on out, everthing is just signed math no matter
2636 what the input types were. */
2637 vrp_int min0
= vrp_int_cst (vr0
.min
);
2638 vrp_int max0
= vrp_int_cst (vr0
.max
);
2639 vrp_int min1
= vrp_int_cst (vr1
.min
);
2640 vrp_int max1
= vrp_int_cst (vr1
.max
);
2641 /* Canonicalize the intervals. */
2642 if (sign
== UNSIGNED
)
2644 if (wi::ltu_p (size
, min0
+ max0
))
2650 if (wi::ltu_p (size
, min1
+ max1
))
2657 vrp_int prod0
= min0
* min1
;
2658 vrp_int prod1
= min0
* max1
;
2659 vrp_int prod2
= max0
* min1
;
2660 vrp_int prod3
= max0
* max1
;
2662 /* Sort the 4 products so that min is in prod0 and max is in
2664 /* min0min1 > max0max1 */
2665 if (wi::gts_p (prod0
, prod3
))
2667 vrp_int tmp
= prod3
;
2672 /* min0max1 > max0min1 */
2673 if (wi::gts_p (prod1
, prod2
))
2675 vrp_int tmp
= prod2
;
2680 if (wi::gts_p (prod0
, prod1
))
2682 vrp_int tmp
= prod1
;
2687 if (wi::gts_p (prod2
, prod3
))
2689 vrp_int tmp
= prod3
;
2694 /* diff = max - min. */
2695 prod2
= prod3
- prod0
;
2696 if (wi::geu_p (prod2
, sizem1
))
2698 /* the range covers all values. */
2699 set_value_range_to_varying (vr
);
2703 /* The following should handle the wrapping and selecting
2704 VR_ANTI_RANGE for us. */
2705 min
= wide_int_to_tree (expr_type
, prod0
);
2706 max
= wide_int_to_tree (expr_type
, prod3
);
2707 set_and_canonicalize_value_range (vr
, VR_RANGE
, min
, max
, NULL
);
2711 /* If we have an unsigned MULT_EXPR with two VR_ANTI_RANGEs,
2712 drop to VR_VARYING. It would take more effort to compute a
2713 precise range for such a case. For example, if we have
2714 op0 == 65536 and op1 == 65536 with their ranges both being
2715 ~[0,0] on a 32-bit machine, we would have op0 * op1 == 0, so
2716 we cannot claim that the product is in ~[0,0]. Note that we
2717 are guaranteed to have vr0.type == vr1.type at this
2719 if (vr0
.type
== VR_ANTI_RANGE
2720 && !TYPE_OVERFLOW_UNDEFINED (expr_type
))
2722 set_value_range_to_varying (vr
);
2726 extract_range_from_multiplicative_op_1 (vr
, code
, &vr0
, &vr1
);
2729 else if (code
== RSHIFT_EXPR
2730 || code
== LSHIFT_EXPR
)
2732 /* If we have a RSHIFT_EXPR with any shift values outside [0..prec-1],
2733 then drop to VR_VARYING. Outside of this range we get undefined
2734 behavior from the shift operation. We cannot even trust
2735 SHIFT_COUNT_TRUNCATED at this stage, because that applies to rtl
2736 shifts, and the operation at the tree level may be widened. */
2737 if (range_int_cst_p (&vr1
)
2738 && compare_tree_int (vr1
.min
, 0) >= 0
2739 && compare_tree_int (vr1
.max
, TYPE_PRECISION (expr_type
)) == -1)
2741 if (code
== RSHIFT_EXPR
)
2743 extract_range_from_multiplicative_op_1 (vr
, code
, &vr0
, &vr1
);
2746 /* We can map lshifts by constants to MULT_EXPR handling. */
2747 else if (code
== LSHIFT_EXPR
2748 && range_int_cst_singleton_p (&vr1
))
2750 bool saved_flag_wrapv
;
2751 value_range_t vr1p
= VR_INITIALIZER
;
2752 vr1p
.type
= VR_RANGE
;
2753 vr1p
.min
= (wide_int_to_tree
2755 wi::set_bit_in_zero (tree_to_shwi (vr1
.min
),
2756 TYPE_PRECISION (expr_type
))));
2757 vr1p
.max
= vr1p
.min
;
2758 /* We have to use a wrapping multiply though as signed overflow
2759 on lshifts is implementation defined in C89. */
2760 saved_flag_wrapv
= flag_wrapv
;
2762 extract_range_from_binary_expr_1 (vr
, MULT_EXPR
, expr_type
,
2764 flag_wrapv
= saved_flag_wrapv
;
2767 else if (code
== LSHIFT_EXPR
2768 && range_int_cst_p (&vr0
))
2770 int prec
= TYPE_PRECISION (expr_type
);
2771 int overflow_pos
= prec
;
2773 wide_int low_bound
, high_bound
;
2774 bool uns
= TYPE_UNSIGNED (expr_type
);
2775 bool in_bounds
= false;
2780 bound_shift
= overflow_pos
- tree_to_shwi (vr1
.max
);
2781 /* If bound_shift == HOST_BITS_PER_WIDE_INT, the llshift can
2782 overflow. However, for that to happen, vr1.max needs to be
2783 zero, which means vr1 is a singleton range of zero, which
2784 means it should be handled by the previous LSHIFT_EXPR
2786 wide_int bound
= wi::set_bit_in_zero (bound_shift
, prec
);
2787 wide_int complement
= ~(bound
- 1);
2792 high_bound
= complement
;
2793 if (wi::ltu_p (vr0
.max
, low_bound
))
2795 /* [5, 6] << [1, 2] == [10, 24]. */
2796 /* We're shifting out only zeroes, the value increases
2800 else if (wi::ltu_p (high_bound
, vr0
.min
))
2802 /* [0xffffff00, 0xffffffff] << [1, 2]
2803 == [0xfffffc00, 0xfffffffe]. */
2804 /* We're shifting out only ones, the value decreases
2811 /* [-1, 1] << [1, 2] == [-4, 4]. */
2812 low_bound
= complement
;
2814 if (wi::lts_p (vr0
.max
, high_bound
)
2815 && wi::lts_p (low_bound
, vr0
.min
))
2817 /* For non-negative numbers, we're shifting out only
2818 zeroes, the value increases monotonically.
2819 For negative numbers, we're shifting out only ones, the
2820 value decreases monotomically. */
2827 extract_range_from_multiplicative_op_1 (vr
, code
, &vr0
, &vr1
);
2832 set_value_range_to_varying (vr
);
2835 else if (code
== TRUNC_DIV_EXPR
2836 || code
== FLOOR_DIV_EXPR
2837 || code
== CEIL_DIV_EXPR
2838 || code
== EXACT_DIV_EXPR
2839 || code
== ROUND_DIV_EXPR
)
2841 if (vr0
.type
!= VR_RANGE
|| symbolic_range_p (&vr0
))
2843 /* For division, if op1 has VR_RANGE but op0 does not, something
2844 can be deduced just from that range. Say [min, max] / [4, max]
2845 gives [min / 4, max / 4] range. */
2846 if (vr1
.type
== VR_RANGE
2847 && !symbolic_range_p (&vr1
)
2848 && range_includes_zero_p (vr1
.min
, vr1
.max
) == 0)
2850 vr0
.type
= type
= VR_RANGE
;
2851 vr0
.min
= vrp_val_min (expr_type
);
2852 vr0
.max
= vrp_val_max (expr_type
);
2856 set_value_range_to_varying (vr
);
2861 /* For divisions, if flag_non_call_exceptions is true, we must
2862 not eliminate a division by zero. */
2863 if (cfun
->can_throw_non_call_exceptions
2864 && (vr1
.type
!= VR_RANGE
2865 || range_includes_zero_p (vr1
.min
, vr1
.max
) != 0))
2867 set_value_range_to_varying (vr
);
2871 /* For divisions, if op0 is VR_RANGE, we can deduce a range
2872 even if op1 is VR_VARYING, VR_ANTI_RANGE, symbolic or can
2874 if (vr0
.type
== VR_RANGE
2875 && (vr1
.type
!= VR_RANGE
2876 || range_includes_zero_p (vr1
.min
, vr1
.max
) != 0))
2878 tree zero
= build_int_cst (TREE_TYPE (vr0
.min
), 0);
2883 if (TYPE_UNSIGNED (expr_type
)
2884 || value_range_nonnegative_p (&vr1
))
2886 /* For unsigned division or when divisor is known
2887 to be non-negative, the range has to cover
2888 all numbers from 0 to max for positive max
2889 and all numbers from min to 0 for negative min. */
2890 cmp
= compare_values (vr0
.max
, zero
);
2893 else if (cmp
== 0 || cmp
== 1)
2897 cmp
= compare_values (vr0
.min
, zero
);
2900 else if (cmp
== 0 || cmp
== -1)
2907 /* Otherwise the range is -max .. max or min .. -min
2908 depending on which bound is bigger in absolute value,
2909 as the division can change the sign. */
2910 abs_extent_range (vr
, vr0
.min
, vr0
.max
);
2913 if (type
== VR_VARYING
)
2915 set_value_range_to_varying (vr
);
2921 extract_range_from_multiplicative_op_1 (vr
, code
, &vr0
, &vr1
);
2925 else if (code
== TRUNC_MOD_EXPR
)
2927 if (vr1
.type
!= VR_RANGE
2928 || range_includes_zero_p (vr1
.min
, vr1
.max
) != 0
2929 || vrp_val_is_min (vr1
.min
))
2931 set_value_range_to_varying (vr
);
2935 /* Compute MAX <|vr1.min|, |vr1.max|> - 1. */
2936 max
= fold_unary_to_constant (ABS_EXPR
, expr_type
, vr1
.min
);
2937 if (tree_int_cst_lt (max
, vr1
.max
))
2939 max
= int_const_binop (MINUS_EXPR
, max
, build_int_cst (TREE_TYPE (max
), 1));
2940 /* If the dividend is non-negative the modulus will be
2941 non-negative as well. */
2942 if (TYPE_UNSIGNED (expr_type
)
2943 || value_range_nonnegative_p (&vr0
))
2944 min
= build_int_cst (TREE_TYPE (max
), 0);
2946 min
= fold_unary_to_constant (NEGATE_EXPR
, expr_type
, max
);
2948 else if (code
== BIT_AND_EXPR
|| code
== BIT_IOR_EXPR
|| code
== BIT_XOR_EXPR
)
2950 bool int_cst_range0
, int_cst_range1
;
2951 wide_int may_be_nonzero0
, may_be_nonzero1
;
2952 wide_int must_be_nonzero0
, must_be_nonzero1
;
2954 int_cst_range0
= zero_nonzero_bits_from_vr (expr_type
, &vr0
,
2957 int_cst_range1
= zero_nonzero_bits_from_vr (expr_type
, &vr1
,
2962 if (code
== BIT_AND_EXPR
)
2964 min
= wide_int_to_tree (expr_type
,
2965 must_be_nonzero0
& must_be_nonzero1
);
2966 wide_int wmax
= may_be_nonzero0
& may_be_nonzero1
;
2967 /* If both input ranges contain only negative values we can
2968 truncate the result range maximum to the minimum of the
2969 input range maxima. */
2970 if (int_cst_range0
&& int_cst_range1
2971 && tree_int_cst_sgn (vr0
.max
) < 0
2972 && tree_int_cst_sgn (vr1
.max
) < 0)
2974 wmax
= wi::min (wmax
, vr0
.max
, TYPE_SIGN (expr_type
));
2975 wmax
= wi::min (wmax
, vr1
.max
, TYPE_SIGN (expr_type
));
2977 /* If either input range contains only non-negative values
2978 we can truncate the result range maximum to the respective
2979 maximum of the input range. */
2980 if (int_cst_range0
&& tree_int_cst_sgn (vr0
.min
) >= 0)
2981 wmax
= wi::min (wmax
, vr0
.max
, TYPE_SIGN (expr_type
));
2982 if (int_cst_range1
&& tree_int_cst_sgn (vr1
.min
) >= 0)
2983 wmax
= wi::min (wmax
, vr1
.max
, TYPE_SIGN (expr_type
));
2984 max
= wide_int_to_tree (expr_type
, wmax
);
2986 else if (code
== BIT_IOR_EXPR
)
2988 max
= wide_int_to_tree (expr_type
,
2989 may_be_nonzero0
| may_be_nonzero1
);
2990 wide_int wmin
= must_be_nonzero0
| must_be_nonzero1
;
2991 /* If the input ranges contain only positive values we can
2992 truncate the minimum of the result range to the maximum
2993 of the input range minima. */
2994 if (int_cst_range0
&& int_cst_range1
2995 && tree_int_cst_sgn (vr0
.min
) >= 0
2996 && tree_int_cst_sgn (vr1
.min
) >= 0)
2998 wmin
= wi::max (wmin
, vr0
.min
, TYPE_SIGN (expr_type
));
2999 wmin
= wi::max (wmin
, vr1
.min
, TYPE_SIGN (expr_type
));
3001 /* If either input range contains only negative values
3002 we can truncate the minimum of the result range to the
3003 respective minimum range. */
3004 if (int_cst_range0
&& tree_int_cst_sgn (vr0
.max
) < 0)
3005 wmin
= wi::max (wmin
, vr0
.min
, TYPE_SIGN (expr_type
));
3006 if (int_cst_range1
&& tree_int_cst_sgn (vr1
.max
) < 0)
3007 wmin
= wi::max (wmin
, vr1
.min
, TYPE_SIGN (expr_type
));
3008 min
= wide_int_to_tree (expr_type
, wmin
);
3010 else if (code
== BIT_XOR_EXPR
)
3012 wide_int result_zero_bits
= ((must_be_nonzero0
& must_be_nonzero1
)
3013 | ~(may_be_nonzero0
| may_be_nonzero1
));
3014 wide_int result_one_bits
3015 = (must_be_nonzero0
.and_not (may_be_nonzero1
)
3016 | must_be_nonzero1
.and_not (may_be_nonzero0
));
3017 max
= wide_int_to_tree (expr_type
, ~result_zero_bits
);
3018 min
= wide_int_to_tree (expr_type
, result_one_bits
);
3019 /* If the range has all positive or all negative values the
3020 result is better than VARYING. */
3021 if (tree_int_cst_sgn (min
) < 0
3022 || tree_int_cst_sgn (max
) >= 0)
3025 max
= min
= NULL_TREE
;
3031 /* If either MIN or MAX overflowed, then set the resulting range to
3032 VARYING. But we do accept an overflow infinity
3034 if (min
== NULL_TREE
3035 || !is_gimple_min_invariant (min
)
3036 || (TREE_OVERFLOW (min
) && !is_overflow_infinity (min
))
3038 || !is_gimple_min_invariant (max
)
3039 || (TREE_OVERFLOW (max
) && !is_overflow_infinity (max
)))
3041 set_value_range_to_varying (vr
);
3047 2) [-INF, +-INF(OVF)]
3048 3) [+-INF(OVF), +INF]
3049 4) [+-INF(OVF), +-INF(OVF)]
3050 We learn nothing when we have INF and INF(OVF) on both sides.
3051 Note that we do accept [-INF, -INF] and [+INF, +INF] without
3053 if ((vrp_val_is_min (min
) || is_overflow_infinity (min
))
3054 && (vrp_val_is_max (max
) || is_overflow_infinity (max
)))
3056 set_value_range_to_varying (vr
);
3060 cmp
= compare_values (min
, max
);
3061 if (cmp
== -2 || cmp
== 1)
3063 /* If the new range has its limits swapped around (MIN > MAX),
3064 then the operation caused one of them to wrap around, mark
3065 the new range VARYING. */
3066 set_value_range_to_varying (vr
);
3069 set_value_range (vr
, type
, min
, max
, NULL
);
3072 /* Extract range information from a binary expression OP0 CODE OP1 based on
3073 the ranges of each of its operands with resulting type EXPR_TYPE.
3074 The resulting range is stored in *VR. */
3077 extract_range_from_binary_expr (value_range_t
*vr
,
3078 enum tree_code code
,
3079 tree expr_type
, tree op0
, tree op1
)
3081 value_range_t vr0
= VR_INITIALIZER
;
3082 value_range_t vr1
= VR_INITIALIZER
;
3084 /* Get value ranges for each operand. For constant operands, create
3085 a new value range with the operand to simplify processing. */
3086 if (TREE_CODE (op0
) == SSA_NAME
)
3087 vr0
= *(get_value_range (op0
));
3088 else if (is_gimple_min_invariant (op0
))
3089 set_value_range_to_value (&vr0
, op0
, NULL
);
3091 set_value_range_to_varying (&vr0
);
3093 if (TREE_CODE (op1
) == SSA_NAME
)
3094 vr1
= *(get_value_range (op1
));
3095 else if (is_gimple_min_invariant (op1
))
3096 set_value_range_to_value (&vr1
, op1
, NULL
);
3098 set_value_range_to_varying (&vr1
);
3100 extract_range_from_binary_expr_1 (vr
, code
, expr_type
, &vr0
, &vr1
);
3103 /* Extract range information from a unary operation CODE based on
3104 the range of its operand *VR0 with type OP0_TYPE with resulting type TYPE.
3105 The The resulting range is stored in *VR. */
3108 extract_range_from_unary_expr_1 (value_range_t
*vr
,
3109 enum tree_code code
, tree type
,
3110 value_range_t
*vr0_
, tree op0_type
)
3112 value_range_t vr0
= *vr0_
, vrtem0
= VR_INITIALIZER
, vrtem1
= VR_INITIALIZER
;
3114 /* VRP only operates on integral and pointer types. */
3115 if (!(INTEGRAL_TYPE_P (op0_type
)
3116 || POINTER_TYPE_P (op0_type
))
3117 || !(INTEGRAL_TYPE_P (type
)
3118 || POINTER_TYPE_P (type
)))
3120 set_value_range_to_varying (vr
);
3124 /* If VR0 is UNDEFINED, so is the result. */
3125 if (vr0
.type
== VR_UNDEFINED
)
3127 set_value_range_to_undefined (vr
);
3131 /* Handle operations that we express in terms of others. */
3132 if (code
== PAREN_EXPR
|| code
== OBJ_TYPE_REF
)
3134 /* PAREN_EXPR and OBJ_TYPE_REF are simple copies. */
3135 copy_value_range (vr
, &vr0
);
3138 else if (code
== NEGATE_EXPR
)
3140 /* -X is simply 0 - X, so re-use existing code that also handles
3141 anti-ranges fine. */
3142 value_range_t zero
= VR_INITIALIZER
;
3143 set_value_range_to_value (&zero
, build_int_cst (type
, 0), NULL
);
3144 extract_range_from_binary_expr_1 (vr
, MINUS_EXPR
, type
, &zero
, &vr0
);
3147 else if (code
== BIT_NOT_EXPR
)
3149 /* ~X is simply -1 - X, so re-use existing code that also handles
3150 anti-ranges fine. */
3151 value_range_t minusone
= VR_INITIALIZER
;
3152 set_value_range_to_value (&minusone
, build_int_cst (type
, -1), NULL
);
3153 extract_range_from_binary_expr_1 (vr
, MINUS_EXPR
,
3154 type
, &minusone
, &vr0
);
3158 /* Now canonicalize anti-ranges to ranges when they are not symbolic
3159 and express op ~[] as (op []') U (op []''). */
3160 if (vr0
.type
== VR_ANTI_RANGE
3161 && ranges_from_anti_range (&vr0
, &vrtem0
, &vrtem1
))
3163 extract_range_from_unary_expr_1 (vr
, code
, type
, &vrtem0
, op0_type
);
3164 if (vrtem1
.type
!= VR_UNDEFINED
)
3166 value_range_t vrres
= VR_INITIALIZER
;
3167 extract_range_from_unary_expr_1 (&vrres
, code
, type
,
3169 vrp_meet (vr
, &vrres
);
3174 if (CONVERT_EXPR_CODE_P (code
))
3176 tree inner_type
= op0_type
;
3177 tree outer_type
= type
;
3179 /* If the expression evaluates to a pointer, we are only interested in
3180 determining if it evaluates to NULL [0, 0] or non-NULL (~[0, 0]). */
3181 if (POINTER_TYPE_P (type
))
3183 if (range_is_nonnull (&vr0
))
3184 set_value_range_to_nonnull (vr
, type
);
3185 else if (range_is_null (&vr0
))
3186 set_value_range_to_null (vr
, type
);
3188 set_value_range_to_varying (vr
);
3192 /* If VR0 is varying and we increase the type precision, assume
3193 a full range for the following transformation. */
3194 if (vr0
.type
== VR_VARYING
3195 && INTEGRAL_TYPE_P (inner_type
)
3196 && TYPE_PRECISION (inner_type
) < TYPE_PRECISION (outer_type
))
3198 vr0
.type
= VR_RANGE
;
3199 vr0
.min
= TYPE_MIN_VALUE (inner_type
);
3200 vr0
.max
= TYPE_MAX_VALUE (inner_type
);
3203 /* If VR0 is a constant range or anti-range and the conversion is
3204 not truncating we can convert the min and max values and
3205 canonicalize the resulting range. Otherwise we can do the
3206 conversion if the size of the range is less than what the
3207 precision of the target type can represent and the range is
3208 not an anti-range. */
3209 if ((vr0
.type
== VR_RANGE
3210 || vr0
.type
== VR_ANTI_RANGE
)
3211 && TREE_CODE (vr0
.min
) == INTEGER_CST
3212 && TREE_CODE (vr0
.max
) == INTEGER_CST
3213 && (!is_overflow_infinity (vr0
.min
)
3214 || (vr0
.type
== VR_RANGE
3215 && TYPE_PRECISION (outer_type
) > TYPE_PRECISION (inner_type
)
3216 && needs_overflow_infinity (outer_type
)
3217 && supports_overflow_infinity (outer_type
)))
3218 && (!is_overflow_infinity (vr0
.max
)
3219 || (vr0
.type
== VR_RANGE
3220 && TYPE_PRECISION (outer_type
) > TYPE_PRECISION (inner_type
)
3221 && needs_overflow_infinity (outer_type
)
3222 && supports_overflow_infinity (outer_type
)))
3223 && (TYPE_PRECISION (outer_type
) >= TYPE_PRECISION (inner_type
)
3224 || (vr0
.type
== VR_RANGE
3225 && integer_zerop (int_const_binop (RSHIFT_EXPR
,
3226 int_const_binop (MINUS_EXPR
, vr0
.max
, vr0
.min
),
3227 size_int (TYPE_PRECISION (outer_type
)))))))
3229 tree new_min
, new_max
;
3230 if (is_overflow_infinity (vr0
.min
))
3231 new_min
= negative_overflow_infinity (outer_type
);
3233 new_min
= force_fit_type (outer_type
, wi::to_widest (vr0
.min
),
3235 if (is_overflow_infinity (vr0
.max
))
3236 new_max
= positive_overflow_infinity (outer_type
);
3238 new_max
= force_fit_type (outer_type
, wi::to_widest (vr0
.max
),
3240 set_and_canonicalize_value_range (vr
, vr0
.type
,
3241 new_min
, new_max
, NULL
);
3245 set_value_range_to_varying (vr
);
3248 else if (code
== ABS_EXPR
)
3253 /* Pass through vr0 in the easy cases. */
3254 if (TYPE_UNSIGNED (type
)
3255 || value_range_nonnegative_p (&vr0
))
3257 copy_value_range (vr
, &vr0
);
3261 /* For the remaining varying or symbolic ranges we can't do anything
3263 if (vr0
.type
== VR_VARYING
3264 || symbolic_range_p (&vr0
))
3266 set_value_range_to_varying (vr
);
3270 /* -TYPE_MIN_VALUE = TYPE_MIN_VALUE with flag_wrapv so we can't get a
3272 if (!TYPE_OVERFLOW_UNDEFINED (type
)
3273 && ((vr0
.type
== VR_RANGE
3274 && vrp_val_is_min (vr0
.min
))
3275 || (vr0
.type
== VR_ANTI_RANGE
3276 && !vrp_val_is_min (vr0
.min
))))
3278 set_value_range_to_varying (vr
);
3282 /* ABS_EXPR may flip the range around, if the original range
3283 included negative values. */
3284 if (is_overflow_infinity (vr0
.min
))
3285 min
= positive_overflow_infinity (type
);
3286 else if (!vrp_val_is_min (vr0
.min
))
3287 min
= fold_unary_to_constant (code
, type
, vr0
.min
);
3288 else if (!needs_overflow_infinity (type
))
3289 min
= TYPE_MAX_VALUE (type
);
3290 else if (supports_overflow_infinity (type
))
3291 min
= positive_overflow_infinity (type
);
3294 set_value_range_to_varying (vr
);
3298 if (is_overflow_infinity (vr0
.max
))
3299 max
= positive_overflow_infinity (type
);
3300 else if (!vrp_val_is_min (vr0
.max
))
3301 max
= fold_unary_to_constant (code
, type
, vr0
.max
);
3302 else if (!needs_overflow_infinity (type
))
3303 max
= TYPE_MAX_VALUE (type
);
3304 else if (supports_overflow_infinity (type
)
3305 /* We shouldn't generate [+INF, +INF] as set_value_range
3306 doesn't like this and ICEs. */
3307 && !is_positive_overflow_infinity (min
))
3308 max
= positive_overflow_infinity (type
);
3311 set_value_range_to_varying (vr
);
3315 cmp
= compare_values (min
, max
);
3317 /* If a VR_ANTI_RANGEs contains zero, then we have
3318 ~[-INF, min(MIN, MAX)]. */
3319 if (vr0
.type
== VR_ANTI_RANGE
)
3321 if (range_includes_zero_p (vr0
.min
, vr0
.max
) == 1)
3323 /* Take the lower of the two values. */
3327 /* Create ~[-INF, min (abs(MIN), abs(MAX))]
3328 or ~[-INF + 1, min (abs(MIN), abs(MAX))] when
3329 flag_wrapv is set and the original anti-range doesn't include
3330 TYPE_MIN_VALUE, remember -TYPE_MIN_VALUE = TYPE_MIN_VALUE. */
3331 if (TYPE_OVERFLOW_WRAPS (type
))
3333 tree type_min_value
= TYPE_MIN_VALUE (type
);
3335 min
= (vr0
.min
!= type_min_value
3336 ? int_const_binop (PLUS_EXPR
, type_min_value
,
3337 build_int_cst (TREE_TYPE (type_min_value
), 1))
3342 if (overflow_infinity_range_p (&vr0
))
3343 min
= negative_overflow_infinity (type
);
3345 min
= TYPE_MIN_VALUE (type
);
3350 /* All else has failed, so create the range [0, INF], even for
3351 flag_wrapv since TYPE_MIN_VALUE is in the original
3353 vr0
.type
= VR_RANGE
;
3354 min
= build_int_cst (type
, 0);
3355 if (needs_overflow_infinity (type
))
3357 if (supports_overflow_infinity (type
))
3358 max
= positive_overflow_infinity (type
);
3361 set_value_range_to_varying (vr
);
3366 max
= TYPE_MAX_VALUE (type
);
3370 /* If the range contains zero then we know that the minimum value in the
3371 range will be zero. */
3372 else if (range_includes_zero_p (vr0
.min
, vr0
.max
) == 1)
3376 min
= build_int_cst (type
, 0);
3380 /* If the range was reversed, swap MIN and MAX. */
3389 cmp
= compare_values (min
, max
);
3390 if (cmp
== -2 || cmp
== 1)
3392 /* If the new range has its limits swapped around (MIN > MAX),
3393 then the operation caused one of them to wrap around, mark
3394 the new range VARYING. */
3395 set_value_range_to_varying (vr
);
3398 set_value_range (vr
, vr0
.type
, min
, max
, NULL
);
3402 /* For unhandled operations fall back to varying. */
3403 set_value_range_to_varying (vr
);
3408 /* Extract range information from a unary expression CODE OP0 based on
3409 the range of its operand with resulting type TYPE.
3410 The resulting range is stored in *VR. */
3413 extract_range_from_unary_expr (value_range_t
*vr
, enum tree_code code
,
3414 tree type
, tree op0
)
3416 value_range_t vr0
= VR_INITIALIZER
;
3418 /* Get value ranges for the operand. For constant operands, create
3419 a new value range with the operand to simplify processing. */
3420 if (TREE_CODE (op0
) == SSA_NAME
)
3421 vr0
= *(get_value_range (op0
));
3422 else if (is_gimple_min_invariant (op0
))
3423 set_value_range_to_value (&vr0
, op0
, NULL
);
3425 set_value_range_to_varying (&vr0
);
3427 extract_range_from_unary_expr_1 (vr
, code
, type
, &vr0
, TREE_TYPE (op0
));
3431 /* Extract range information from a conditional expression STMT based on
3432 the ranges of each of its operands and the expression code. */
3435 extract_range_from_cond_expr (value_range_t
*vr
, gimple stmt
)
3438 value_range_t vr0
= VR_INITIALIZER
;
3439 value_range_t vr1
= VR_INITIALIZER
;
3441 /* Get value ranges for each operand. For constant operands, create
3442 a new value range with the operand to simplify processing. */
3443 op0
= gimple_assign_rhs2 (stmt
);
3444 if (TREE_CODE (op0
) == SSA_NAME
)
3445 vr0
= *(get_value_range (op0
));
3446 else if (is_gimple_min_invariant (op0
))
3447 set_value_range_to_value (&vr0
, op0
, NULL
);
3449 set_value_range_to_varying (&vr0
);
3451 op1
= gimple_assign_rhs3 (stmt
);
3452 if (TREE_CODE (op1
) == SSA_NAME
)
3453 vr1
= *(get_value_range (op1
));
3454 else if (is_gimple_min_invariant (op1
))
3455 set_value_range_to_value (&vr1
, op1
, NULL
);
3457 set_value_range_to_varying (&vr1
);
3459 /* The resulting value range is the union of the operand ranges */
3460 copy_value_range (vr
, &vr0
);
3461 vrp_meet (vr
, &vr1
);
3465 /* Extract range information from a comparison expression EXPR based
3466 on the range of its operand and the expression code. */
3469 extract_range_from_comparison (value_range_t
*vr
, enum tree_code code
,
3470 tree type
, tree op0
, tree op1
)
3475 val
= vrp_evaluate_conditional_warnv_with_ops (code
, op0
, op1
, false, &sop
,
3478 /* A disadvantage of using a special infinity as an overflow
3479 representation is that we lose the ability to record overflow
3480 when we don't have an infinity. So we have to ignore a result
3481 which relies on overflow. */
3483 if (val
&& !is_overflow_infinity (val
) && !sop
)
3485 /* Since this expression was found on the RHS of an assignment,
3486 its type may be different from _Bool. Convert VAL to EXPR's
3488 val
= fold_convert (type
, val
);
3489 if (is_gimple_min_invariant (val
))
3490 set_value_range_to_value (vr
, val
, vr
->equiv
);
3492 set_value_range (vr
, VR_RANGE
, val
, val
, vr
->equiv
);
3495 /* The result of a comparison is always true or false. */
3496 set_value_range_to_truthvalue (vr
, type
);
3499 /* Try to derive a nonnegative or nonzero range out of STMT relying
3500 primarily on generic routines in fold in conjunction with range data.
3501 Store the result in *VR */
3504 extract_range_basic (value_range_t
*vr
, gimple stmt
)
3507 tree type
= gimple_expr_type (stmt
);
3509 if (gimple_call_builtin_p (stmt
, BUILT_IN_NORMAL
))
3511 tree fndecl
= gimple_call_fndecl (stmt
), arg
;
3512 int mini
, maxi
, zerov
= 0, prec
;
3514 switch (DECL_FUNCTION_CODE (fndecl
))
3516 case BUILT_IN_CONSTANT_P
:
3517 /* If the call is __builtin_constant_p and the argument is a
3518 function parameter resolve it to false. This avoids bogus
3519 array bound warnings.
3520 ??? We could do this as early as inlining is finished. */
3521 arg
= gimple_call_arg (stmt
, 0);
3522 if (TREE_CODE (arg
) == SSA_NAME
3523 && SSA_NAME_IS_DEFAULT_DEF (arg
)
3524 && TREE_CODE (SSA_NAME_VAR (arg
)) == PARM_DECL
)
3526 set_value_range_to_null (vr
, type
);
3530 /* Both __builtin_ffs* and __builtin_popcount return
3532 CASE_INT_FN (BUILT_IN_FFS
):
3533 CASE_INT_FN (BUILT_IN_POPCOUNT
):
3534 arg
= gimple_call_arg (stmt
, 0);
3535 prec
= TYPE_PRECISION (TREE_TYPE (arg
));
3538 if (TREE_CODE (arg
) == SSA_NAME
)
3540 value_range_t
*vr0
= get_value_range (arg
);
3541 /* If arg is non-zero, then ffs or popcount
3543 if (((vr0
->type
== VR_RANGE
3544 && integer_nonzerop (vr0
->min
))
3545 || (vr0
->type
== VR_ANTI_RANGE
3546 && integer_zerop (vr0
->min
)))
3547 && !is_overflow_infinity (vr0
->min
))
3549 /* If some high bits are known to be zero,
3550 we can decrease the maximum. */
3551 if (vr0
->type
== VR_RANGE
3552 && TREE_CODE (vr0
->max
) == INTEGER_CST
3553 && !is_overflow_infinity (vr0
->max
))
3554 maxi
= tree_floor_log2 (vr0
->max
) + 1;
3557 /* __builtin_parity* returns [0, 1]. */
3558 CASE_INT_FN (BUILT_IN_PARITY
):
3562 /* __builtin_c[lt]z* return [0, prec-1], except for
3563 when the argument is 0, but that is undefined behavior.
3564 On many targets where the CLZ RTL or optab value is defined
3565 for 0 the value is prec, so include that in the range
3567 CASE_INT_FN (BUILT_IN_CLZ
):
3568 arg
= gimple_call_arg (stmt
, 0);
3569 prec
= TYPE_PRECISION (TREE_TYPE (arg
));
3572 if (optab_handler (clz_optab
, TYPE_MODE (TREE_TYPE (arg
)))
3574 && CLZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (TREE_TYPE (arg
)),
3576 /* Handle only the single common value. */
3578 /* Magic value to give up, unless vr0 proves
3581 if (TREE_CODE (arg
) == SSA_NAME
)
3583 value_range_t
*vr0
= get_value_range (arg
);
3584 /* From clz of VR_RANGE minimum we can compute
3586 if (vr0
->type
== VR_RANGE
3587 && TREE_CODE (vr0
->min
) == INTEGER_CST
3588 && !is_overflow_infinity (vr0
->min
))
3590 maxi
= prec
- 1 - tree_floor_log2 (vr0
->min
);
3594 else if (vr0
->type
== VR_ANTI_RANGE
3595 && integer_zerop (vr0
->min
)
3596 && !is_overflow_infinity (vr0
->min
))
3603 /* From clz of VR_RANGE maximum we can compute
3605 if (vr0
->type
== VR_RANGE
3606 && TREE_CODE (vr0
->max
) == INTEGER_CST
3607 && !is_overflow_infinity (vr0
->max
))
3609 mini
= prec
- 1 - tree_floor_log2 (vr0
->max
);
3617 /* __builtin_ctz* return [0, prec-1], except for
3618 when the argument is 0, but that is undefined behavior.
3619 If there is a ctz optab for this mode and
3620 CTZ_DEFINED_VALUE_AT_ZERO, include that in the range,
3621 otherwise just assume 0 won't be seen. */
3622 CASE_INT_FN (BUILT_IN_CTZ
):
3623 arg
= gimple_call_arg (stmt
, 0);
3624 prec
= TYPE_PRECISION (TREE_TYPE (arg
));
3627 if (optab_handler (ctz_optab
, TYPE_MODE (TREE_TYPE (arg
)))
3629 && CTZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (TREE_TYPE (arg
)),
3632 /* Handle only the two common values. */
3635 else if (zerov
== prec
)
3638 /* Magic value to give up, unless vr0 proves
3642 if (TREE_CODE (arg
) == SSA_NAME
)
3644 value_range_t
*vr0
= get_value_range (arg
);
3645 /* If arg is non-zero, then use [0, prec - 1]. */
3646 if (((vr0
->type
== VR_RANGE
3647 && integer_nonzerop (vr0
->min
))
3648 || (vr0
->type
== VR_ANTI_RANGE
3649 && integer_zerop (vr0
->min
)))
3650 && !is_overflow_infinity (vr0
->min
))
3655 /* If some high bits are known to be zero,
3656 we can decrease the result maximum. */
3657 if (vr0
->type
== VR_RANGE
3658 && TREE_CODE (vr0
->max
) == INTEGER_CST
3659 && !is_overflow_infinity (vr0
->max
))
3661 maxi
= tree_floor_log2 (vr0
->max
);
3662 /* For vr0 [0, 0] give up. */
3670 /* __builtin_clrsb* returns [0, prec-1]. */
3671 CASE_INT_FN (BUILT_IN_CLRSB
):
3672 arg
= gimple_call_arg (stmt
, 0);
3673 prec
= TYPE_PRECISION (TREE_TYPE (arg
));
3678 set_value_range (vr
, VR_RANGE
, build_int_cst (type
, mini
),
3679 build_int_cst (type
, maxi
), NULL
);
3685 else if (is_gimple_call (stmt
)
3686 && gimple_call_internal_p (stmt
))
3688 enum tree_code subcode
= ERROR_MARK
;
3689 switch (gimple_call_internal_fn (stmt
))
3691 case IFN_UBSAN_CHECK_ADD
:
3692 subcode
= PLUS_EXPR
;
3694 case IFN_UBSAN_CHECK_SUB
:
3695 subcode
= MINUS_EXPR
;
3697 case IFN_UBSAN_CHECK_MUL
:
3698 subcode
= MULT_EXPR
;
3703 if (subcode
!= ERROR_MARK
)
3705 bool saved_flag_wrapv
= flag_wrapv
;
3706 /* Pretend the arithmetics is wrapping. If there is
3707 any overflow, we'll complain, but will actually do
3708 wrapping operation. */
3710 extract_range_from_binary_expr (vr
, subcode
, type
,
3711 gimple_call_arg (stmt
, 0),
3712 gimple_call_arg (stmt
, 1));
3713 flag_wrapv
= saved_flag_wrapv
;
3715 /* If for both arguments vrp_valueize returned non-NULL,
3716 this should have been already folded and if not, it
3717 wasn't folded because of overflow. Avoid removing the
3718 UBSAN_CHECK_* calls in that case. */
3719 if (vr
->type
== VR_RANGE
3720 && (vr
->min
== vr
->max
3721 || operand_equal_p (vr
->min
, vr
->max
, 0)))
3722 set_value_range_to_varying (vr
);
3726 if (INTEGRAL_TYPE_P (type
)
3727 && gimple_stmt_nonnegative_warnv_p (stmt
, &sop
))
3728 set_value_range_to_nonnegative (vr
, type
,
3729 sop
|| stmt_overflow_infinity (stmt
));
3730 else if (vrp_stmt_computes_nonzero (stmt
, &sop
)
3732 set_value_range_to_nonnull (vr
, type
);
3734 set_value_range_to_varying (vr
);
3738 /* Try to compute a useful range out of assignment STMT and store it
3742 extract_range_from_assignment (value_range_t
*vr
, gimple stmt
)
3744 enum tree_code code
= gimple_assign_rhs_code (stmt
);
3746 if (code
== ASSERT_EXPR
)
3747 extract_range_from_assert (vr
, gimple_assign_rhs1 (stmt
));
3748 else if (code
== SSA_NAME
)
3749 extract_range_from_ssa_name (vr
, gimple_assign_rhs1 (stmt
));
3750 else if (TREE_CODE_CLASS (code
) == tcc_binary
)
3751 extract_range_from_binary_expr (vr
, gimple_assign_rhs_code (stmt
),
3752 gimple_expr_type (stmt
),
3753 gimple_assign_rhs1 (stmt
),
3754 gimple_assign_rhs2 (stmt
));
3755 else if (TREE_CODE_CLASS (code
) == tcc_unary
)
3756 extract_range_from_unary_expr (vr
, gimple_assign_rhs_code (stmt
),
3757 gimple_expr_type (stmt
),
3758 gimple_assign_rhs1 (stmt
));
3759 else if (code
== COND_EXPR
)
3760 extract_range_from_cond_expr (vr
, stmt
);
3761 else if (TREE_CODE_CLASS (code
) == tcc_comparison
)
3762 extract_range_from_comparison (vr
, gimple_assign_rhs_code (stmt
),
3763 gimple_expr_type (stmt
),
3764 gimple_assign_rhs1 (stmt
),
3765 gimple_assign_rhs2 (stmt
));
3766 else if (get_gimple_rhs_class (code
) == GIMPLE_SINGLE_RHS
3767 && is_gimple_min_invariant (gimple_assign_rhs1 (stmt
)))
3768 set_value_range_to_value (vr
, gimple_assign_rhs1 (stmt
), NULL
);
3770 set_value_range_to_varying (vr
);
3772 if (vr
->type
== VR_VARYING
)
3773 extract_range_basic (vr
, stmt
);
3776 /* Given a range VR, a LOOP and a variable VAR, determine whether it
3777 would be profitable to adjust VR using scalar evolution information
3778 for VAR. If so, update VR with the new limits. */
3781 adjust_range_with_scev (value_range_t
*vr
, struct loop
*loop
,
3782 gimple stmt
, tree var
)
3784 tree init
, step
, chrec
, tmin
, tmax
, min
, max
, type
, tem
;
3785 enum ev_direction dir
;
3787 /* TODO. Don't adjust anti-ranges. An anti-range may provide
3788 better opportunities than a regular range, but I'm not sure. */
3789 if (vr
->type
== VR_ANTI_RANGE
)
3792 chrec
= instantiate_parameters (loop
, analyze_scalar_evolution (loop
, var
));
3794 /* Like in PR19590, scev can return a constant function. */
3795 if (is_gimple_min_invariant (chrec
))
3797 set_value_range_to_value (vr
, chrec
, vr
->equiv
);
3801 if (TREE_CODE (chrec
) != POLYNOMIAL_CHREC
)
3804 init
= initial_condition_in_loop_num (chrec
, loop
->num
);
3805 tem
= op_with_constant_singleton_value_range (init
);
3808 step
= evolution_part_in_loop_num (chrec
, loop
->num
);
3809 tem
= op_with_constant_singleton_value_range (step
);
3813 /* If STEP is symbolic, we can't know whether INIT will be the
3814 minimum or maximum value in the range. Also, unless INIT is
3815 a simple expression, compare_values and possibly other functions
3816 in tree-vrp won't be able to handle it. */
3817 if (step
== NULL_TREE
3818 || !is_gimple_min_invariant (step
)
3819 || !valid_value_p (init
))
3822 dir
= scev_direction (chrec
);
3823 if (/* Do not adjust ranges if we do not know whether the iv increases
3824 or decreases, ... */
3825 dir
== EV_DIR_UNKNOWN
3826 /* ... or if it may wrap. */
3827 || scev_probably_wraps_p (init
, step
, stmt
, get_chrec_loop (chrec
),
3831 /* We use TYPE_MIN_VALUE and TYPE_MAX_VALUE here instead of
3832 negative_overflow_infinity and positive_overflow_infinity,
3833 because we have concluded that the loop probably does not
3836 type
= TREE_TYPE (var
);
3837 if (POINTER_TYPE_P (type
) || !TYPE_MIN_VALUE (type
))
3838 tmin
= lower_bound_in_type (type
, type
);
3840 tmin
= TYPE_MIN_VALUE (type
);
3841 if (POINTER_TYPE_P (type
) || !TYPE_MAX_VALUE (type
))
3842 tmax
= upper_bound_in_type (type
, type
);
3844 tmax
= TYPE_MAX_VALUE (type
);
3846 /* Try to use estimated number of iterations for the loop to constrain the
3847 final value in the evolution. */
3848 if (TREE_CODE (step
) == INTEGER_CST
3849 && is_gimple_val (init
)
3850 && (TREE_CODE (init
) != SSA_NAME
3851 || get_value_range (init
)->type
== VR_RANGE
))
3855 /* We are only entering here for loop header PHI nodes, so using
3856 the number of latch executions is the correct thing to use. */
3857 if (max_loop_iterations (loop
, &nit
))
3859 value_range_t maxvr
= VR_INITIALIZER
;
3860 signop sgn
= TYPE_SIGN (TREE_TYPE (step
));
3863 wide_int wtmp
= wi::mul (wi::to_widest (step
), nit
, sgn
, &overflow
);
3864 /* If the multiplication overflowed we can't do a meaningful
3865 adjustment. Likewise if the result doesn't fit in the type
3866 of the induction variable. For a signed type we have to
3867 check whether the result has the expected signedness which
3868 is that of the step as number of iterations is unsigned. */
3870 && wi::fits_to_tree_p (wtmp
, TREE_TYPE (init
))
3872 || wi::gts_p (wtmp
, 0) == wi::gts_p (step
, 0)))
3874 tem
= wide_int_to_tree (TREE_TYPE (init
), wtmp
);
3875 extract_range_from_binary_expr (&maxvr
, PLUS_EXPR
,
3876 TREE_TYPE (init
), init
, tem
);
3877 /* Likewise if the addition did. */
3878 if (maxvr
.type
== VR_RANGE
)
3887 if (vr
->type
== VR_VARYING
|| vr
->type
== VR_UNDEFINED
)
3892 /* For VARYING or UNDEFINED ranges, just about anything we get
3893 from scalar evolutions should be better. */
3895 if (dir
== EV_DIR_DECREASES
)
3900 /* If we would create an invalid range, then just assume we
3901 know absolutely nothing. This may be over-conservative,
3902 but it's clearly safe, and should happen only in unreachable
3903 parts of code, or for invalid programs. */
3904 if (compare_values (min
, max
) == 1)
3907 set_value_range (vr
, VR_RANGE
, min
, max
, vr
->equiv
);
3909 else if (vr
->type
== VR_RANGE
)
3914 if (dir
== EV_DIR_DECREASES
)
3916 /* INIT is the maximum value. If INIT is lower than VR->MAX
3917 but no smaller than VR->MIN, set VR->MAX to INIT. */
3918 if (compare_values (init
, max
) == -1)
3921 /* According to the loop information, the variable does not
3922 overflow. If we think it does, probably because of an
3923 overflow due to arithmetic on a different INF value,
3925 if (is_negative_overflow_infinity (min
)
3926 || compare_values (min
, tmin
) == -1)
3932 /* If INIT is bigger than VR->MIN, set VR->MIN to INIT. */
3933 if (compare_values (init
, min
) == 1)
3936 if (is_positive_overflow_infinity (max
)
3937 || compare_values (tmax
, max
) == -1)
3941 /* If we just created an invalid range with the minimum
3942 greater than the maximum, we fail conservatively.
3943 This should happen only in unreachable
3944 parts of code, or for invalid programs. */
3945 if (compare_values (min
, max
) == 1)
3948 set_value_range (vr
, VR_RANGE
, min
, max
, vr
->equiv
);
3952 /* Return true if VAR may overflow at STMT. This checks any available
3953 loop information to see if we can determine that VAR does not
3957 vrp_var_may_overflow (tree var
, gimple stmt
)
3960 tree chrec
, init
, step
;
3962 if (current_loops
== NULL
)
3965 l
= loop_containing_stmt (stmt
);
3970 chrec
= instantiate_parameters (l
, analyze_scalar_evolution (l
, var
));
3971 if (TREE_CODE (chrec
) != POLYNOMIAL_CHREC
)
3974 init
= initial_condition_in_loop_num (chrec
, l
->num
);
3975 step
= evolution_part_in_loop_num (chrec
, l
->num
);
3977 if (step
== NULL_TREE
3978 || !is_gimple_min_invariant (step
)
3979 || !valid_value_p (init
))
3982 /* If we get here, we know something useful about VAR based on the
3983 loop information. If it wraps, it may overflow. */
3985 if (scev_probably_wraps_p (init
, step
, stmt
, get_chrec_loop (chrec
),
3989 if (dump_file
&& (dump_flags
& TDF_DETAILS
) != 0)
3991 print_generic_expr (dump_file
, var
, 0);
3992 fprintf (dump_file
, ": loop information indicates does not overflow\n");
3999 /* Given two numeric value ranges VR0, VR1 and a comparison code COMP:
4001 - Return BOOLEAN_TRUE_NODE if VR0 COMP VR1 always returns true for
4002 all the values in the ranges.
4004 - Return BOOLEAN_FALSE_NODE if the comparison always returns false.
4006 - Return NULL_TREE if it is not always possible to determine the
4007 value of the comparison.
4009 Also set *STRICT_OVERFLOW_P to indicate whether a range with an
4010 overflow infinity was used in the test. */
4014 compare_ranges (enum tree_code comp
, value_range_t
*vr0
, value_range_t
*vr1
,
4015 bool *strict_overflow_p
)
4017 /* VARYING or UNDEFINED ranges cannot be compared. */
4018 if (vr0
->type
== VR_VARYING
4019 || vr0
->type
== VR_UNDEFINED
4020 || vr1
->type
== VR_VARYING
4021 || vr1
->type
== VR_UNDEFINED
)
4024 /* Anti-ranges need to be handled separately. */
4025 if (vr0
->type
== VR_ANTI_RANGE
|| vr1
->type
== VR_ANTI_RANGE
)
4027 /* If both are anti-ranges, then we cannot compute any
4029 if (vr0
->type
== VR_ANTI_RANGE
&& vr1
->type
== VR_ANTI_RANGE
)
4032 /* These comparisons are never statically computable. */
4039 /* Equality can be computed only between a range and an
4040 anti-range. ~[VAL1, VAL2] == [VAL1, VAL2] is always false. */
4041 if (vr0
->type
== VR_RANGE
)
4043 /* To simplify processing, make VR0 the anti-range. */
4044 value_range_t
*tmp
= vr0
;
4049 gcc_assert (comp
== NE_EXPR
|| comp
== EQ_EXPR
);
4051 if (compare_values_warnv (vr0
->min
, vr1
->min
, strict_overflow_p
) == 0
4052 && compare_values_warnv (vr0
->max
, vr1
->max
, strict_overflow_p
) == 0)
4053 return (comp
== NE_EXPR
) ? boolean_true_node
: boolean_false_node
;
4058 if (!usable_range_p (vr0
, strict_overflow_p
)
4059 || !usable_range_p (vr1
, strict_overflow_p
))
4062 /* Simplify processing. If COMP is GT_EXPR or GE_EXPR, switch the
4063 operands around and change the comparison code. */
4064 if (comp
== GT_EXPR
|| comp
== GE_EXPR
)
4067 comp
= (comp
== GT_EXPR
) ? LT_EXPR
: LE_EXPR
;
4073 if (comp
== EQ_EXPR
)
4075 /* Equality may only be computed if both ranges represent
4076 exactly one value. */
4077 if (compare_values_warnv (vr0
->min
, vr0
->max
, strict_overflow_p
) == 0
4078 && compare_values_warnv (vr1
->min
, vr1
->max
, strict_overflow_p
) == 0)
4080 int cmp_min
= compare_values_warnv (vr0
->min
, vr1
->min
,
4082 int cmp_max
= compare_values_warnv (vr0
->max
, vr1
->max
,
4084 if (cmp_min
== 0 && cmp_max
== 0)
4085 return boolean_true_node
;
4086 else if (cmp_min
!= -2 && cmp_max
!= -2)
4087 return boolean_false_node
;
4089 /* If [V0_MIN, V1_MAX] < [V1_MIN, V1_MAX] then V0 != V1. */
4090 else if (compare_values_warnv (vr0
->min
, vr1
->max
,
4091 strict_overflow_p
) == 1
4092 || compare_values_warnv (vr1
->min
, vr0
->max
,
4093 strict_overflow_p
) == 1)
4094 return boolean_false_node
;
4098 else if (comp
== NE_EXPR
)
4102 /* If VR0 is completely to the left or completely to the right
4103 of VR1, they are always different. Notice that we need to
4104 make sure that both comparisons yield similar results to
4105 avoid comparing values that cannot be compared at
4107 cmp1
= compare_values_warnv (vr0
->max
, vr1
->min
, strict_overflow_p
);
4108 cmp2
= compare_values_warnv (vr0
->min
, vr1
->max
, strict_overflow_p
);
4109 if ((cmp1
== -1 && cmp2
== -1) || (cmp1
== 1 && cmp2
== 1))
4110 return boolean_true_node
;
4112 /* If VR0 and VR1 represent a single value and are identical,
4114 else if (compare_values_warnv (vr0
->min
, vr0
->max
,
4115 strict_overflow_p
) == 0
4116 && compare_values_warnv (vr1
->min
, vr1
->max
,
4117 strict_overflow_p
) == 0
4118 && compare_values_warnv (vr0
->min
, vr1
->min
,
4119 strict_overflow_p
) == 0
4120 && compare_values_warnv (vr0
->max
, vr1
->max
,
4121 strict_overflow_p
) == 0)
4122 return boolean_false_node
;
4124 /* Otherwise, they may or may not be different. */
4128 else if (comp
== LT_EXPR
|| comp
== LE_EXPR
)
4132 /* If VR0 is to the left of VR1, return true. */
4133 tst
= compare_values_warnv (vr0
->max
, vr1
->min
, strict_overflow_p
);
4134 if ((comp
== LT_EXPR
&& tst
== -1)
4135 || (comp
== LE_EXPR
&& (tst
== -1 || tst
== 0)))
4137 if (overflow_infinity_range_p (vr0
)
4138 || overflow_infinity_range_p (vr1
))
4139 *strict_overflow_p
= true;
4140 return boolean_true_node
;
4143 /* If VR0 is to the right of VR1, return false. */
4144 tst
= compare_values_warnv (vr0
->min
, vr1
->max
, strict_overflow_p
);
4145 if ((comp
== LT_EXPR
&& (tst
== 0 || tst
== 1))
4146 || (comp
== LE_EXPR
&& tst
== 1))
4148 if (overflow_infinity_range_p (vr0
)
4149 || overflow_infinity_range_p (vr1
))
4150 *strict_overflow_p
= true;
4151 return boolean_false_node
;
4154 /* Otherwise, we don't know. */
4162 /* Given a value range VR, a value VAL and a comparison code COMP, return
4163 BOOLEAN_TRUE_NODE if VR COMP VAL always returns true for all the
4164 values in VR. Return BOOLEAN_FALSE_NODE if the comparison
4165 always returns false. Return NULL_TREE if it is not always
4166 possible to determine the value of the comparison. Also set
4167 *STRICT_OVERFLOW_P to indicate whether a range with an overflow
4168 infinity was used in the test. */
4171 compare_range_with_value (enum tree_code comp
, value_range_t
*vr
, tree val
,
4172 bool *strict_overflow_p
)
4174 if (vr
->type
== VR_VARYING
|| vr
->type
== VR_UNDEFINED
)
4177 /* Anti-ranges need to be handled separately. */
4178 if (vr
->type
== VR_ANTI_RANGE
)
4180 /* For anti-ranges, the only predicates that we can compute at
4181 compile time are equality and inequality. */
4188 /* ~[VAL_1, VAL_2] OP VAL is known if VAL_1 <= VAL <= VAL_2. */
4189 if (value_inside_range (val
, vr
->min
, vr
->max
) == 1)
4190 return (comp
== NE_EXPR
) ? boolean_true_node
: boolean_false_node
;
4195 if (!usable_range_p (vr
, strict_overflow_p
))
4198 if (comp
== EQ_EXPR
)
4200 /* EQ_EXPR may only be computed if VR represents exactly
4202 if (compare_values_warnv (vr
->min
, vr
->max
, strict_overflow_p
) == 0)
4204 int cmp
= compare_values_warnv (vr
->min
, val
, strict_overflow_p
);
4206 return boolean_true_node
;
4207 else if (cmp
== -1 || cmp
== 1 || cmp
== 2)
4208 return boolean_false_node
;
4210 else if (compare_values_warnv (val
, vr
->min
, strict_overflow_p
) == -1
4211 || compare_values_warnv (vr
->max
, val
, strict_overflow_p
) == -1)
4212 return boolean_false_node
;
4216 else if (comp
== NE_EXPR
)
4218 /* If VAL is not inside VR, then they are always different. */
4219 if (compare_values_warnv (vr
->max
, val
, strict_overflow_p
) == -1
4220 || compare_values_warnv (vr
->min
, val
, strict_overflow_p
) == 1)
4221 return boolean_true_node
;
4223 /* If VR represents exactly one value equal to VAL, then return
4225 if (compare_values_warnv (vr
->min
, vr
->max
, strict_overflow_p
) == 0
4226 && compare_values_warnv (vr
->min
, val
, strict_overflow_p
) == 0)
4227 return boolean_false_node
;
4229 /* Otherwise, they may or may not be different. */
4232 else if (comp
== LT_EXPR
|| comp
== LE_EXPR
)
4236 /* If VR is to the left of VAL, return true. */
4237 tst
= compare_values_warnv (vr
->max
, val
, strict_overflow_p
);
4238 if ((comp
== LT_EXPR
&& tst
== -1)
4239 || (comp
== LE_EXPR
&& (tst
== -1 || tst
== 0)))
4241 if (overflow_infinity_range_p (vr
))
4242 *strict_overflow_p
= true;
4243 return boolean_true_node
;
4246 /* If VR is to the right of VAL, return false. */
4247 tst
= compare_values_warnv (vr
->min
, val
, strict_overflow_p
);
4248 if ((comp
== LT_EXPR
&& (tst
== 0 || tst
== 1))
4249 || (comp
== LE_EXPR
&& tst
== 1))
4251 if (overflow_infinity_range_p (vr
))
4252 *strict_overflow_p
= true;
4253 return boolean_false_node
;
4256 /* Otherwise, we don't know. */
4259 else if (comp
== GT_EXPR
|| comp
== GE_EXPR
)
4263 /* If VR is to the right of VAL, return true. */
4264 tst
= compare_values_warnv (vr
->min
, val
, strict_overflow_p
);
4265 if ((comp
== GT_EXPR
&& tst
== 1)
4266 || (comp
== GE_EXPR
&& (tst
== 0 || tst
== 1)))
4268 if (overflow_infinity_range_p (vr
))
4269 *strict_overflow_p
= true;
4270 return boolean_true_node
;
4273 /* If VR is to the left of VAL, return false. */
4274 tst
= compare_values_warnv (vr
->max
, val
, strict_overflow_p
);
4275 if ((comp
== GT_EXPR
&& (tst
== -1 || tst
== 0))
4276 || (comp
== GE_EXPR
&& tst
== -1))
4278 if (overflow_infinity_range_p (vr
))
4279 *strict_overflow_p
= true;
4280 return boolean_false_node
;
4283 /* Otherwise, we don't know. */
4291 /* Debugging dumps. */
4293 void dump_value_range (FILE *, value_range_t
*);
4294 void debug_value_range (value_range_t
*);
4295 void dump_all_value_ranges (FILE *);
4296 void debug_all_value_ranges (void);
4297 void dump_vr_equiv (FILE *, bitmap
);
4298 void debug_vr_equiv (bitmap
);
4301 /* Dump value range VR to FILE. */
4304 dump_value_range (FILE *file
, value_range_t
*vr
)
4307 fprintf (file
, "[]");
4308 else if (vr
->type
== VR_UNDEFINED
)
4309 fprintf (file
, "UNDEFINED");
4310 else if (vr
->type
== VR_RANGE
|| vr
->type
== VR_ANTI_RANGE
)
4312 tree type
= TREE_TYPE (vr
->min
);
4314 fprintf (file
, "%s[", (vr
->type
== VR_ANTI_RANGE
) ? "~" : "");
4316 if (is_negative_overflow_infinity (vr
->min
))
4317 fprintf (file
, "-INF(OVF)");
4318 else if (INTEGRAL_TYPE_P (type
)
4319 && !TYPE_UNSIGNED (type
)
4320 && vrp_val_is_min (vr
->min
))
4321 fprintf (file
, "-INF");
4323 print_generic_expr (file
, vr
->min
, 0);
4325 fprintf (file
, ", ");
4327 if (is_positive_overflow_infinity (vr
->max
))
4328 fprintf (file
, "+INF(OVF)");
4329 else if (INTEGRAL_TYPE_P (type
)
4330 && vrp_val_is_max (vr
->max
))
4331 fprintf (file
, "+INF");
4333 print_generic_expr (file
, vr
->max
, 0);
4335 fprintf (file
, "]");
4342 fprintf (file
, " EQUIVALENCES: { ");
4344 EXECUTE_IF_SET_IN_BITMAP (vr
->equiv
, 0, i
, bi
)
4346 print_generic_expr (file
, ssa_name (i
), 0);
4347 fprintf (file
, " ");
4351 fprintf (file
, "} (%u elements)", c
);
4354 else if (vr
->type
== VR_VARYING
)
4355 fprintf (file
, "VARYING");
4357 fprintf (file
, "INVALID RANGE");
4361 /* Dump value range VR to stderr. */
4364 debug_value_range (value_range_t
*vr
)
4366 dump_value_range (stderr
, vr
);
4367 fprintf (stderr
, "\n");
4371 /* Dump value ranges of all SSA_NAMEs to FILE. */
4374 dump_all_value_ranges (FILE *file
)
4378 for (i
= 0; i
< num_vr_values
; i
++)
4382 print_generic_expr (file
, ssa_name (i
), 0);
4383 fprintf (file
, ": ");
4384 dump_value_range (file
, vr_value
[i
]);
4385 fprintf (file
, "\n");
4389 fprintf (file
, "\n");
4393 /* Dump all value ranges to stderr. */
4396 debug_all_value_ranges (void)
4398 dump_all_value_ranges (stderr
);
4402 /* Given a COND_EXPR COND of the form 'V OP W', and an SSA name V,
4403 create a new SSA name N and return the assertion assignment
4404 'V = ASSERT_EXPR <V, V OP W>'. */
4407 build_assert_expr_for (tree cond
, tree v
)
4412 gcc_assert (TREE_CODE (v
) == SSA_NAME
4413 && COMPARISON_CLASS_P (cond
));
4415 a
= build2 (ASSERT_EXPR
, TREE_TYPE (v
), v
, cond
);
4416 assertion
= gimple_build_assign (NULL_TREE
, a
);
4418 /* The new ASSERT_EXPR, creates a new SSA name that replaces the
4419 operand of the ASSERT_EXPR. Create it so the new name and the old one
4420 are registered in the replacement table so that we can fix the SSA web
4421 after adding all the ASSERT_EXPRs. */
4422 create_new_def_for (v
, assertion
, NULL
);
4428 /* Return false if EXPR is a predicate expression involving floating
4432 fp_predicate (gimple stmt
)
4434 GIMPLE_CHECK (stmt
, GIMPLE_COND
);
4436 return FLOAT_TYPE_P (TREE_TYPE (gimple_cond_lhs (stmt
)));
4439 /* If the range of values taken by OP can be inferred after STMT executes,
4440 return the comparison code (COMP_CODE_P) and value (VAL_P) that
4441 describes the inferred range. Return true if a range could be
4445 infer_value_range (gimple stmt
, tree op
, enum tree_code
*comp_code_p
, tree
*val_p
)
4448 *comp_code_p
= ERROR_MARK
;
4450 /* Do not attempt to infer anything in names that flow through
4452 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op
))
4455 /* Similarly, don't infer anything from statements that may throw
4456 exceptions. ??? Relax this requirement? */
4457 if (stmt_could_throw_p (stmt
))
4460 /* If STMT is the last statement of a basic block with no normal
4461 successors, there is no point inferring anything about any of its
4462 operands. We would not be able to find a proper insertion point
4463 for the assertion, anyway. */
4464 if (stmt_ends_bb_p (stmt
))
4469 FOR_EACH_EDGE (e
, ei
, gimple_bb (stmt
)->succs
)
4470 if (!(e
->flags
& EDGE_ABNORMAL
))
4476 if (infer_nonnull_range (stmt
, op
, true, true))
4478 *val_p
= build_int_cst (TREE_TYPE (op
), 0);
4479 *comp_code_p
= NE_EXPR
;
4487 void dump_asserts_for (FILE *, tree
);
4488 void debug_asserts_for (tree
);
4489 void dump_all_asserts (FILE *);
4490 void debug_all_asserts (void);
4492 /* Dump all the registered assertions for NAME to FILE. */
4495 dump_asserts_for (FILE *file
, tree name
)
4499 fprintf (file
, "Assertions to be inserted for ");
4500 print_generic_expr (file
, name
, 0);
4501 fprintf (file
, "\n");
4503 loc
= asserts_for
[SSA_NAME_VERSION (name
)];
4506 fprintf (file
, "\t");
4507 print_gimple_stmt (file
, gsi_stmt (loc
->si
), 0, 0);
4508 fprintf (file
, "\n\tBB #%d", loc
->bb
->index
);
4511 fprintf (file
, "\n\tEDGE %d->%d", loc
->e
->src
->index
,
4512 loc
->e
->dest
->index
);
4513 dump_edge_info (file
, loc
->e
, dump_flags
, 0);
4515 fprintf (file
, "\n\tPREDICATE: ");
4516 print_generic_expr (file
, name
, 0);
4517 fprintf (file
, " %s ", get_tree_code_name (loc
->comp_code
));
4518 print_generic_expr (file
, loc
->val
, 0);
4519 fprintf (file
, "\n\n");
4523 fprintf (file
, "\n");
4527 /* Dump all the registered assertions for NAME to stderr. */
4530 debug_asserts_for (tree name
)
4532 dump_asserts_for (stderr
, name
);
4536 /* Dump all the registered assertions for all the names to FILE. */
4539 dump_all_asserts (FILE *file
)
4544 fprintf (file
, "\nASSERT_EXPRs to be inserted\n\n");
4545 EXECUTE_IF_SET_IN_BITMAP (need_assert_for
, 0, i
, bi
)
4546 dump_asserts_for (file
, ssa_name (i
));
4547 fprintf (file
, "\n");
4551 /* Dump all the registered assertions for all the names to stderr. */
4554 debug_all_asserts (void)
4556 dump_all_asserts (stderr
);
4560 /* If NAME doesn't have an ASSERT_EXPR registered for asserting
4561 'EXPR COMP_CODE VAL' at a location that dominates block BB or
4562 E->DEST, then register this location as a possible insertion point
4563 for ASSERT_EXPR <NAME, EXPR COMP_CODE VAL>.
4565 BB, E and SI provide the exact insertion point for the new
4566 ASSERT_EXPR. If BB is NULL, then the ASSERT_EXPR is to be inserted
4567 on edge E. Otherwise, if E is NULL, the ASSERT_EXPR is inserted on
4568 BB. If SI points to a COND_EXPR or a SWITCH_EXPR statement, then E
4569 must not be NULL. */
4572 register_new_assert_for (tree name
, tree expr
,
4573 enum tree_code comp_code
,
4577 gimple_stmt_iterator si
)
4579 assert_locus_t n
, loc
, last_loc
;
4580 basic_block dest_bb
;
4582 gcc_checking_assert (bb
== NULL
|| e
== NULL
);
4585 gcc_checking_assert (gimple_code (gsi_stmt (si
)) != GIMPLE_COND
4586 && gimple_code (gsi_stmt (si
)) != GIMPLE_SWITCH
);
4588 /* Never build an assert comparing against an integer constant with
4589 TREE_OVERFLOW set. This confuses our undefined overflow warning
4591 if (TREE_OVERFLOW_P (val
))
4592 val
= drop_tree_overflow (val
);
4594 /* The new assertion A will be inserted at BB or E. We need to
4595 determine if the new location is dominated by a previously
4596 registered location for A. If we are doing an edge insertion,
4597 assume that A will be inserted at E->DEST. Note that this is not
4600 If E is a critical edge, it will be split. But even if E is
4601 split, the new block will dominate the same set of blocks that
4604 The reverse, however, is not true, blocks dominated by E->DEST
4605 will not be dominated by the new block created to split E. So,
4606 if the insertion location is on a critical edge, we will not use
4607 the new location to move another assertion previously registered
4608 at a block dominated by E->DEST. */
4609 dest_bb
= (bb
) ? bb
: e
->dest
;
4611 /* If NAME already has an ASSERT_EXPR registered for COMP_CODE and
4612 VAL at a block dominating DEST_BB, then we don't need to insert a new
4613 one. Similarly, if the same assertion already exists at a block
4614 dominated by DEST_BB and the new location is not on a critical
4615 edge, then update the existing location for the assertion (i.e.,
4616 move the assertion up in the dominance tree).
4618 Note, this is implemented as a simple linked list because there
4619 should not be more than a handful of assertions registered per
4620 name. If this becomes a performance problem, a table hashed by
4621 COMP_CODE and VAL could be implemented. */
4622 loc
= asserts_for
[SSA_NAME_VERSION (name
)];
4626 if (loc
->comp_code
== comp_code
4628 || operand_equal_p (loc
->val
, val
, 0))
4629 && (loc
->expr
== expr
4630 || operand_equal_p (loc
->expr
, expr
, 0)))
4632 /* If E is not a critical edge and DEST_BB
4633 dominates the existing location for the assertion, move
4634 the assertion up in the dominance tree by updating its
4635 location information. */
4636 if ((e
== NULL
|| !EDGE_CRITICAL_P (e
))
4637 && dominated_by_p (CDI_DOMINATORS
, loc
->bb
, dest_bb
))
4646 /* Update the last node of the list and move to the next one. */
4651 /* If we didn't find an assertion already registered for
4652 NAME COMP_CODE VAL, add a new one at the end of the list of
4653 assertions associated with NAME. */
4654 n
= XNEW (struct assert_locus_d
);
4658 n
->comp_code
= comp_code
;
4666 asserts_for
[SSA_NAME_VERSION (name
)] = n
;
4668 bitmap_set_bit (need_assert_for
, SSA_NAME_VERSION (name
));
4671 /* (COND_OP0 COND_CODE COND_OP1) is a predicate which uses NAME.
4672 Extract a suitable test code and value and store them into *CODE_P and
4673 *VAL_P so the predicate is normalized to NAME *CODE_P *VAL_P.
4675 If no extraction was possible, return FALSE, otherwise return TRUE.
4677 If INVERT is true, then we invert the result stored into *CODE_P. */
4680 extract_code_and_val_from_cond_with_ops (tree name
, enum tree_code cond_code
,
4681 tree cond_op0
, tree cond_op1
,
4682 bool invert
, enum tree_code
*code_p
,
4685 enum tree_code comp_code
;
4688 /* Otherwise, we have a comparison of the form NAME COMP VAL
4689 or VAL COMP NAME. */
4690 if (name
== cond_op1
)
4692 /* If the predicate is of the form VAL COMP NAME, flip
4693 COMP around because we need to register NAME as the
4694 first operand in the predicate. */
4695 comp_code
= swap_tree_comparison (cond_code
);
4700 /* The comparison is of the form NAME COMP VAL, so the
4701 comparison code remains unchanged. */
4702 comp_code
= cond_code
;
4706 /* Invert the comparison code as necessary. */
4708 comp_code
= invert_tree_comparison (comp_code
, 0);
4710 /* VRP does not handle float types. */
4711 if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (val
)))
4714 /* Do not register always-false predicates.
4715 FIXME: this works around a limitation in fold() when dealing with
4716 enumerations. Given 'enum { N1, N2 } x;', fold will not
4717 fold 'if (x > N2)' to 'if (0)'. */
4718 if ((comp_code
== GT_EXPR
|| comp_code
== LT_EXPR
)
4719 && INTEGRAL_TYPE_P (TREE_TYPE (val
)))
4721 tree min
= TYPE_MIN_VALUE (TREE_TYPE (val
));
4722 tree max
= TYPE_MAX_VALUE (TREE_TYPE (val
));
4724 if (comp_code
== GT_EXPR
4726 || compare_values (val
, max
) == 0))
4729 if (comp_code
== LT_EXPR
4731 || compare_values (val
, min
) == 0))
4734 *code_p
= comp_code
;
4739 /* Find out smallest RES where RES > VAL && (RES & MASK) == RES, if any
4740 (otherwise return VAL). VAL and MASK must be zero-extended for
4741 precision PREC. If SGNBIT is non-zero, first xor VAL with SGNBIT
4742 (to transform signed values into unsigned) and at the end xor
4746 masked_increment (const wide_int
&val_in
, const wide_int
&mask
,
4747 const wide_int
&sgnbit
, unsigned int prec
)
4749 wide_int bit
= wi::one (prec
), res
;
4752 wide_int val
= val_in
^ sgnbit
;
4753 for (i
= 0; i
< prec
; i
++, bit
+= bit
)
4756 if ((res
& bit
) == 0)
4759 res
= (val
+ bit
).and_not (res
);
4761 if (wi::gtu_p (res
, val
))
4762 return res
^ sgnbit
;
4764 return val
^ sgnbit
;
4767 /* Try to register an edge assertion for SSA name NAME on edge E for
4768 the condition COND contributing to the conditional jump pointed to by BSI.
4769 Invert the condition COND if INVERT is true.
4770 Return true if an assertion for NAME could be registered. */
4773 register_edge_assert_for_2 (tree name
, edge e
, gimple_stmt_iterator bsi
,
4774 enum tree_code cond_code
,
4775 tree cond_op0
, tree cond_op1
, bool invert
)
4778 enum tree_code comp_code
;
4779 bool retval
= false;
4781 if (!extract_code_and_val_from_cond_with_ops (name
, cond_code
,
4784 invert
, &comp_code
, &val
))
4787 /* Only register an ASSERT_EXPR if NAME was found in the sub-graph
4788 reachable from E. */
4789 if (live_on_edge (e
, name
)
4790 && !has_single_use (name
))
4792 register_new_assert_for (name
, name
, comp_code
, val
, NULL
, e
, bsi
);
4796 /* In the case of NAME <= CST and NAME being defined as
4797 NAME = (unsigned) NAME2 + CST2 we can assert NAME2 >= -CST2
4798 and NAME2 <= CST - CST2. We can do the same for NAME > CST.
4799 This catches range and anti-range tests. */
4800 if ((comp_code
== LE_EXPR
4801 || comp_code
== GT_EXPR
)
4802 && TREE_CODE (val
) == INTEGER_CST
4803 && TYPE_UNSIGNED (TREE_TYPE (val
)))
4805 gimple def_stmt
= SSA_NAME_DEF_STMT (name
);
4806 tree cst2
= NULL_TREE
, name2
= NULL_TREE
, name3
= NULL_TREE
;
4808 /* Extract CST2 from the (optional) addition. */
4809 if (is_gimple_assign (def_stmt
)
4810 && gimple_assign_rhs_code (def_stmt
) == PLUS_EXPR
)
4812 name2
= gimple_assign_rhs1 (def_stmt
);
4813 cst2
= gimple_assign_rhs2 (def_stmt
);
4814 if (TREE_CODE (name2
) == SSA_NAME
4815 && TREE_CODE (cst2
) == INTEGER_CST
)
4816 def_stmt
= SSA_NAME_DEF_STMT (name2
);
4819 /* Extract NAME2 from the (optional) sign-changing cast. */
4820 if (gimple_assign_cast_p (def_stmt
))
4822 if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt
))
4823 && ! TYPE_UNSIGNED (TREE_TYPE (gimple_assign_rhs1 (def_stmt
)))
4824 && (TYPE_PRECISION (gimple_expr_type (def_stmt
))
4825 == TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (def_stmt
)))))
4826 name3
= gimple_assign_rhs1 (def_stmt
);
4829 /* If name3 is used later, create an ASSERT_EXPR for it. */
4830 if (name3
!= NULL_TREE
4831 && TREE_CODE (name3
) == SSA_NAME
4832 && (cst2
== NULL_TREE
4833 || TREE_CODE (cst2
) == INTEGER_CST
)
4834 && INTEGRAL_TYPE_P (TREE_TYPE (name3
))
4835 && live_on_edge (e
, name3
)
4836 && !has_single_use (name3
))
4840 /* Build an expression for the range test. */
4841 tmp
= build1 (NOP_EXPR
, TREE_TYPE (name
), name3
);
4842 if (cst2
!= NULL_TREE
)
4843 tmp
= build2 (PLUS_EXPR
, TREE_TYPE (name
), tmp
, cst2
);
4847 fprintf (dump_file
, "Adding assert for ");
4848 print_generic_expr (dump_file
, name3
, 0);
4849 fprintf (dump_file
, " from ");
4850 print_generic_expr (dump_file
, tmp
, 0);
4851 fprintf (dump_file
, "\n");
4854 register_new_assert_for (name3
, tmp
, comp_code
, val
, NULL
, e
, bsi
);
4859 /* If name2 is used later, create an ASSERT_EXPR for it. */
4860 if (name2
!= NULL_TREE
4861 && TREE_CODE (name2
) == SSA_NAME
4862 && TREE_CODE (cst2
) == INTEGER_CST
4863 && INTEGRAL_TYPE_P (TREE_TYPE (name2
))
4864 && live_on_edge (e
, name2
)
4865 && !has_single_use (name2
))
4869 /* Build an expression for the range test. */
4871 if (TREE_TYPE (name
) != TREE_TYPE (name2
))
4872 tmp
= build1 (NOP_EXPR
, TREE_TYPE (name
), tmp
);
4873 if (cst2
!= NULL_TREE
)
4874 tmp
= build2 (PLUS_EXPR
, TREE_TYPE (name
), tmp
, cst2
);
4878 fprintf (dump_file
, "Adding assert for ");
4879 print_generic_expr (dump_file
, name2
, 0);
4880 fprintf (dump_file
, " from ");
4881 print_generic_expr (dump_file
, tmp
, 0);
4882 fprintf (dump_file
, "\n");
4885 register_new_assert_for (name2
, tmp
, comp_code
, val
, NULL
, e
, bsi
);
4891 /* In the case of post-in/decrement tests like if (i++) ... and uses
4892 of the in/decremented value on the edge the extra name we want to
4893 assert for is not on the def chain of the name compared. Instead
4894 it is in the set of use stmts. */
4895 if ((comp_code
== NE_EXPR
4896 || comp_code
== EQ_EXPR
)
4897 && TREE_CODE (val
) == INTEGER_CST
)
4899 imm_use_iterator ui
;
4901 FOR_EACH_IMM_USE_STMT (use_stmt
, ui
, name
)
4903 /* Cut off to use-stmts that are in the predecessor. */
4904 if (gimple_bb (use_stmt
) != e
->src
)
4907 if (!is_gimple_assign (use_stmt
))
4910 enum tree_code code
= gimple_assign_rhs_code (use_stmt
);
4911 if (code
!= PLUS_EXPR
4912 && code
!= MINUS_EXPR
)
4915 tree cst
= gimple_assign_rhs2 (use_stmt
);
4916 if (TREE_CODE (cst
) != INTEGER_CST
)
4919 tree name2
= gimple_assign_lhs (use_stmt
);
4920 if (live_on_edge (e
, name2
))
4922 cst
= int_const_binop (code
, val
, cst
);
4923 register_new_assert_for (name2
, name2
, comp_code
, cst
,
4930 if (TREE_CODE_CLASS (comp_code
) == tcc_comparison
4931 && TREE_CODE (val
) == INTEGER_CST
)
4933 gimple def_stmt
= SSA_NAME_DEF_STMT (name
);
4934 tree name2
= NULL_TREE
, names
[2], cst2
= NULL_TREE
;
4935 tree val2
= NULL_TREE
;
4936 unsigned int prec
= TYPE_PRECISION (TREE_TYPE (val
));
4937 wide_int mask
= wi::zero (prec
);
4938 unsigned int nprec
= prec
;
4939 enum tree_code rhs_code
= ERROR_MARK
;
4941 if (is_gimple_assign (def_stmt
))
4942 rhs_code
= gimple_assign_rhs_code (def_stmt
);
4944 /* Add asserts for NAME cmp CST and NAME being defined
4945 as NAME = (int) NAME2. */
4946 if (!TYPE_UNSIGNED (TREE_TYPE (val
))
4947 && (comp_code
== LE_EXPR
|| comp_code
== LT_EXPR
4948 || comp_code
== GT_EXPR
|| comp_code
== GE_EXPR
)
4949 && gimple_assign_cast_p (def_stmt
))
4951 name2
= gimple_assign_rhs1 (def_stmt
);
4952 if (CONVERT_EXPR_CODE_P (rhs_code
)
4953 && INTEGRAL_TYPE_P (TREE_TYPE (name2
))
4954 && TYPE_UNSIGNED (TREE_TYPE (name2
))
4955 && prec
== TYPE_PRECISION (TREE_TYPE (name2
))
4956 && (comp_code
== LE_EXPR
|| comp_code
== GT_EXPR
4957 || !tree_int_cst_equal (val
,
4958 TYPE_MIN_VALUE (TREE_TYPE (val
))))
4959 && live_on_edge (e
, name2
)
4960 && !has_single_use (name2
))
4963 enum tree_code new_comp_code
= comp_code
;
4965 cst
= fold_convert (TREE_TYPE (name2
),
4966 TYPE_MIN_VALUE (TREE_TYPE (val
)));
4967 /* Build an expression for the range test. */
4968 tmp
= build2 (PLUS_EXPR
, TREE_TYPE (name2
), name2
, cst
);
4969 cst
= fold_build2 (PLUS_EXPR
, TREE_TYPE (name2
), cst
,
4970 fold_convert (TREE_TYPE (name2
), val
));
4971 if (comp_code
== LT_EXPR
|| comp_code
== GE_EXPR
)
4973 new_comp_code
= comp_code
== LT_EXPR
? LE_EXPR
: GT_EXPR
;
4974 cst
= fold_build2 (MINUS_EXPR
, TREE_TYPE (name2
), cst
,
4975 build_int_cst (TREE_TYPE (name2
), 1));
4980 fprintf (dump_file
, "Adding assert for ");
4981 print_generic_expr (dump_file
, name2
, 0);
4982 fprintf (dump_file
, " from ");
4983 print_generic_expr (dump_file
, tmp
, 0);
4984 fprintf (dump_file
, "\n");
4987 register_new_assert_for (name2
, tmp
, new_comp_code
, cst
, NULL
,
4994 /* Add asserts for NAME cmp CST and NAME being defined as
4995 NAME = NAME2 >> CST2.
4997 Extract CST2 from the right shift. */
4998 if (rhs_code
== RSHIFT_EXPR
)
5000 name2
= gimple_assign_rhs1 (def_stmt
);
5001 cst2
= gimple_assign_rhs2 (def_stmt
);
5002 if (TREE_CODE (name2
) == SSA_NAME
5003 && tree_fits_uhwi_p (cst2
)
5004 && INTEGRAL_TYPE_P (TREE_TYPE (name2
))
5005 && IN_RANGE (tree_to_uhwi (cst2
), 1, prec
- 1)
5006 && prec
== GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (val
)))
5007 && live_on_edge (e
, name2
)
5008 && !has_single_use (name2
))
5010 mask
= wi::mask (tree_to_uhwi (cst2
), false, prec
);
5011 val2
= fold_binary (LSHIFT_EXPR
, TREE_TYPE (val
), val
, cst2
);
5014 if (val2
!= NULL_TREE
5015 && TREE_CODE (val2
) == INTEGER_CST
5016 && simple_cst_equal (fold_build2 (RSHIFT_EXPR
,
5020 enum tree_code new_comp_code
= comp_code
;
5024 if (comp_code
== EQ_EXPR
|| comp_code
== NE_EXPR
)
5026 if (!TYPE_UNSIGNED (TREE_TYPE (val
)))
5028 tree type
= build_nonstandard_integer_type (prec
, 1);
5029 tmp
= build1 (NOP_EXPR
, type
, name2
);
5030 val2
= fold_convert (type
, val2
);
5032 tmp
= fold_build2 (MINUS_EXPR
, TREE_TYPE (tmp
), tmp
, val2
);
5033 new_val
= wide_int_to_tree (TREE_TYPE (tmp
), mask
);
5034 new_comp_code
= comp_code
== EQ_EXPR
? LE_EXPR
: GT_EXPR
;
5036 else if (comp_code
== LT_EXPR
|| comp_code
== GE_EXPR
)
5039 = wi::min_value (prec
, TYPE_SIGN (TREE_TYPE (val
)));
5041 if (minval
== new_val
)
5042 new_val
= NULL_TREE
;
5047 = wi::max_value (prec
, TYPE_SIGN (TREE_TYPE (val
)));
5050 new_val
= NULL_TREE
;
5052 new_val
= wide_int_to_tree (TREE_TYPE (val2
), mask
);
5059 fprintf (dump_file
, "Adding assert for ");
5060 print_generic_expr (dump_file
, name2
, 0);
5061 fprintf (dump_file
, " from ");
5062 print_generic_expr (dump_file
, tmp
, 0);
5063 fprintf (dump_file
, "\n");
5066 register_new_assert_for (name2
, tmp
, new_comp_code
, new_val
,
5072 /* Add asserts for NAME cmp CST and NAME being defined as
5073 NAME = NAME2 & CST2.
5075 Extract CST2 from the and.
5078 NAME = (unsigned) NAME2;
5079 casts where NAME's type is unsigned and has smaller precision
5080 than NAME2's type as if it was NAME = NAME2 & MASK. */
5081 names
[0] = NULL_TREE
;
5082 names
[1] = NULL_TREE
;
5084 if (rhs_code
== BIT_AND_EXPR
5085 || (CONVERT_EXPR_CODE_P (rhs_code
)
5086 && TREE_CODE (TREE_TYPE (val
)) == INTEGER_TYPE
5087 && TYPE_UNSIGNED (TREE_TYPE (val
))
5088 && TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (def_stmt
)))
5092 name2
= gimple_assign_rhs1 (def_stmt
);
5093 if (rhs_code
== BIT_AND_EXPR
)
5094 cst2
= gimple_assign_rhs2 (def_stmt
);
5097 cst2
= TYPE_MAX_VALUE (TREE_TYPE (val
));
5098 nprec
= TYPE_PRECISION (TREE_TYPE (name2
));
5100 if (TREE_CODE (name2
) == SSA_NAME
5101 && INTEGRAL_TYPE_P (TREE_TYPE (name2
))
5102 && TREE_CODE (cst2
) == INTEGER_CST
5103 && !integer_zerop (cst2
)
5105 || TYPE_UNSIGNED (TREE_TYPE (val
))))
5107 gimple def_stmt2
= SSA_NAME_DEF_STMT (name2
);
5108 if (gimple_assign_cast_p (def_stmt2
))
5110 names
[1] = gimple_assign_rhs1 (def_stmt2
);
5111 if (!CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt2
))
5112 || !INTEGRAL_TYPE_P (TREE_TYPE (names
[1]))
5113 || (TYPE_PRECISION (TREE_TYPE (name2
))
5114 != TYPE_PRECISION (TREE_TYPE (names
[1])))
5115 || !live_on_edge (e
, names
[1])
5116 || has_single_use (names
[1]))
5117 names
[1] = NULL_TREE
;
5119 if (live_on_edge (e
, name2
)
5120 && !has_single_use (name2
))
5124 if (names
[0] || names
[1])
5126 wide_int minv
, maxv
, valv
, cst2v
;
5127 wide_int tem
, sgnbit
;
5128 bool valid_p
= false, valn
= false, cst2n
= false;
5129 enum tree_code ccode
= comp_code
;
5131 valv
= wide_int::from (val
, nprec
, UNSIGNED
);
5132 cst2v
= wide_int::from (cst2
, nprec
, UNSIGNED
);
5133 if (TYPE_SIGN (TREE_TYPE (val
)) == SIGNED
)
5135 valn
= wi::neg_p (wi::sext (valv
, nprec
));
5136 cst2n
= wi::neg_p (wi::sext (cst2v
, nprec
));
5138 /* If CST2 doesn't have most significant bit set,
5139 but VAL is negative, we have comparison like
5140 if ((x & 0x123) > -4) (always true). Just give up. */
5144 sgnbit
= wi::set_bit_in_zero (nprec
- 1, nprec
);
5146 sgnbit
= wi::zero (nprec
);
5147 minv
= valv
& cst2v
;
5151 /* Minimum unsigned value for equality is VAL & CST2
5152 (should be equal to VAL, otherwise we probably should
5153 have folded the comparison into false) and
5154 maximum unsigned value is VAL | ~CST2. */
5155 maxv
= valv
| ~cst2v
;
5156 maxv
= wi::zext (maxv
, nprec
);
5161 tem
= valv
| ~cst2v
;
5162 tem
= wi::zext (tem
, nprec
);
5163 /* If VAL is 0, handle (X & CST2) != 0 as (X & CST2) > 0U. */
5167 sgnbit
= wi::zero (nprec
);
5170 /* If (VAL | ~CST2) is all ones, handle it as
5171 (X & CST2) < VAL. */
5176 sgnbit
= wi::zero (nprec
);
5179 if (!cst2n
&& wi::neg_p (wi::sext (cst2v
, nprec
)))
5180 sgnbit
= wi::set_bit_in_zero (nprec
- 1, nprec
);
5189 if (tem
== wi::mask (nprec
- 1, false, nprec
))
5195 sgnbit
= wi::zero (nprec
);
5200 /* Minimum unsigned value for >= if (VAL & CST2) == VAL
5201 is VAL and maximum unsigned value is ~0. For signed
5202 comparison, if CST2 doesn't have most significant bit
5203 set, handle it similarly. If CST2 has MSB set,
5204 the minimum is the same, and maximum is ~0U/2. */
5207 /* If (VAL & CST2) != VAL, X & CST2 can't be equal to
5209 minv
= masked_increment (valv
, cst2v
, sgnbit
, nprec
);
5213 maxv
= wi::mask (nprec
- (cst2n
? 1 : 0), false, nprec
);
5219 /* Find out smallest MINV where MINV > VAL
5220 && (MINV & CST2) == MINV, if any. If VAL is signed and
5221 CST2 has MSB set, compute it biased by 1 << (nprec - 1). */
5222 minv
= masked_increment (valv
, cst2v
, sgnbit
, nprec
);
5225 maxv
= wi::mask (nprec
- (cst2n
? 1 : 0), false, nprec
);
5230 /* Minimum unsigned value for <= is 0 and maximum
5231 unsigned value is VAL | ~CST2 if (VAL & CST2) == VAL.
5232 Otherwise, find smallest VAL2 where VAL2 > VAL
5233 && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
5235 For signed comparison, if CST2 doesn't have most
5236 significant bit set, handle it similarly. If CST2 has
5237 MSB set, the maximum is the same and minimum is INT_MIN. */
5242 maxv
= masked_increment (valv
, cst2v
, sgnbit
, nprec
);
5248 maxv
= wi::zext (maxv
, nprec
);
5255 /* Minimum unsigned value for < is 0 and maximum
5256 unsigned value is (VAL-1) | ~CST2 if (VAL & CST2) == VAL.
5257 Otherwise, find smallest VAL2 where VAL2 > VAL
5258 && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
5260 For signed comparison, if CST2 doesn't have most
5261 significant bit set, handle it similarly. If CST2 has
5262 MSB set, the maximum is the same and minimum is INT_MIN. */
5271 maxv
= masked_increment (valv
, cst2v
, sgnbit
, nprec
);
5277 maxv
= wi::zext (maxv
, nprec
);
5286 && wi::zext (maxv
- minv
, nprec
) != wi::minus_one (nprec
))
5288 tree tmp
, new_val
, type
;
5291 for (i
= 0; i
< 2; i
++)
5294 wide_int maxv2
= maxv
;
5296 type
= TREE_TYPE (names
[i
]);
5297 if (!TYPE_UNSIGNED (type
))
5299 type
= build_nonstandard_integer_type (nprec
, 1);
5300 tmp
= build1 (NOP_EXPR
, type
, names
[i
]);
5304 tmp
= build2 (PLUS_EXPR
, type
, tmp
,
5305 wide_int_to_tree (type
, -minv
));
5306 maxv2
= maxv
- minv
;
5308 new_val
= wide_int_to_tree (type
, maxv2
);
5312 fprintf (dump_file
, "Adding assert for ");
5313 print_generic_expr (dump_file
, names
[i
], 0);
5314 fprintf (dump_file
, " from ");
5315 print_generic_expr (dump_file
, tmp
, 0);
5316 fprintf (dump_file
, "\n");
5319 register_new_assert_for (names
[i
], tmp
, LE_EXPR
,
5320 new_val
, NULL
, e
, bsi
);
5330 /* OP is an operand of a truth value expression which is known to have
5331 a particular value. Register any asserts for OP and for any
5332 operands in OP's defining statement.
5334 If CODE is EQ_EXPR, then we want to register OP is zero (false),
5335 if CODE is NE_EXPR, then we want to register OP is nonzero (true). */
5338 register_edge_assert_for_1 (tree op
, enum tree_code code
,
5339 edge e
, gimple_stmt_iterator bsi
)
5341 bool retval
= false;
5344 enum tree_code rhs_code
;
5346 /* We only care about SSA_NAMEs. */
5347 if (TREE_CODE (op
) != SSA_NAME
)
5350 /* We know that OP will have a zero or nonzero value. If OP is used
5351 more than once go ahead and register an assert for OP. */
5352 if (live_on_edge (e
, op
)
5353 && !has_single_use (op
))
5355 val
= build_int_cst (TREE_TYPE (op
), 0);
5356 register_new_assert_for (op
, op
, code
, val
, NULL
, e
, bsi
);
5360 /* Now look at how OP is set. If it's set from a comparison,
5361 a truth operation or some bit operations, then we may be able
5362 to register information about the operands of that assignment. */
5363 op_def
= SSA_NAME_DEF_STMT (op
);
5364 if (gimple_code (op_def
) != GIMPLE_ASSIGN
)
5367 rhs_code
= gimple_assign_rhs_code (op_def
);
5369 if (TREE_CODE_CLASS (rhs_code
) == tcc_comparison
)
5371 bool invert
= (code
== EQ_EXPR
? true : false);
5372 tree op0
= gimple_assign_rhs1 (op_def
);
5373 tree op1
= gimple_assign_rhs2 (op_def
);
5375 if (TREE_CODE (op0
) == SSA_NAME
)
5376 retval
|= register_edge_assert_for_2 (op0
, e
, bsi
, rhs_code
, op0
, op1
,
5378 if (TREE_CODE (op1
) == SSA_NAME
)
5379 retval
|= register_edge_assert_for_2 (op1
, e
, bsi
, rhs_code
, op0
, op1
,
5382 else if ((code
== NE_EXPR
5383 && gimple_assign_rhs_code (op_def
) == BIT_AND_EXPR
)
5385 && gimple_assign_rhs_code (op_def
) == BIT_IOR_EXPR
))
5387 /* Recurse on each operand. */
5388 tree op0
= gimple_assign_rhs1 (op_def
);
5389 tree op1
= gimple_assign_rhs2 (op_def
);
5390 if (TREE_CODE (op0
) == SSA_NAME
5391 && has_single_use (op0
))
5392 retval
|= register_edge_assert_for_1 (op0
, code
, e
, bsi
);
5393 if (TREE_CODE (op1
) == SSA_NAME
5394 && has_single_use (op1
))
5395 retval
|= register_edge_assert_for_1 (op1
, code
, e
, bsi
);
5397 else if (gimple_assign_rhs_code (op_def
) == BIT_NOT_EXPR
5398 && TYPE_PRECISION (TREE_TYPE (gimple_assign_lhs (op_def
))) == 1)
5400 /* Recurse, flipping CODE. */
5401 code
= invert_tree_comparison (code
, false);
5402 retval
|= register_edge_assert_for_1 (gimple_assign_rhs1 (op_def
),
5405 else if (gimple_assign_rhs_code (op_def
) == SSA_NAME
)
5407 /* Recurse through the copy. */
5408 retval
|= register_edge_assert_for_1 (gimple_assign_rhs1 (op_def
),
5411 else if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (op_def
)))
5413 /* Recurse through the type conversion, unless it is a narrowing
5414 conversion or conversion from non-integral type. */
5415 tree rhs
= gimple_assign_rhs1 (op_def
);
5416 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs
))
5417 && (TYPE_PRECISION (TREE_TYPE (rhs
))
5418 <= TYPE_PRECISION (TREE_TYPE (op
))))
5419 retval
|= register_edge_assert_for_1 (rhs
, code
, e
, bsi
);
5425 /* Try to register an edge assertion for SSA name NAME on edge E for
5426 the condition COND contributing to the conditional jump pointed to by SI.
5427 Return true if an assertion for NAME could be registered. */
5430 register_edge_assert_for (tree name
, edge e
, gimple_stmt_iterator si
,
5431 enum tree_code cond_code
, tree cond_op0
,
5435 enum tree_code comp_code
;
5436 bool retval
= false;
5437 bool is_else_edge
= (e
->flags
& EDGE_FALSE_VALUE
) != 0;
5439 /* Do not attempt to infer anything in names that flow through
5441 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name
))
5444 if (!extract_code_and_val_from_cond_with_ops (name
, cond_code
,
5450 /* Register ASSERT_EXPRs for name. */
5451 retval
|= register_edge_assert_for_2 (name
, e
, si
, cond_code
, cond_op0
,
5452 cond_op1
, is_else_edge
);
5455 /* If COND is effectively an equality test of an SSA_NAME against
5456 the value zero or one, then we may be able to assert values
5457 for SSA_NAMEs which flow into COND. */
5459 /* In the case of NAME == 1 or NAME != 0, for BIT_AND_EXPR defining
5460 statement of NAME we can assert both operands of the BIT_AND_EXPR
5461 have nonzero value. */
5462 if (((comp_code
== EQ_EXPR
&& integer_onep (val
))
5463 || (comp_code
== NE_EXPR
&& integer_zerop (val
))))
5465 gimple def_stmt
= SSA_NAME_DEF_STMT (name
);
5467 if (is_gimple_assign (def_stmt
)
5468 && gimple_assign_rhs_code (def_stmt
) == BIT_AND_EXPR
)
5470 tree op0
= gimple_assign_rhs1 (def_stmt
);
5471 tree op1
= gimple_assign_rhs2 (def_stmt
);
5472 retval
|= register_edge_assert_for_1 (op0
, NE_EXPR
, e
, si
);
5473 retval
|= register_edge_assert_for_1 (op1
, NE_EXPR
, e
, si
);
5477 /* In the case of NAME == 0 or NAME != 1, for BIT_IOR_EXPR defining
5478 statement of NAME we can assert both operands of the BIT_IOR_EXPR
5480 if (((comp_code
== EQ_EXPR
&& integer_zerop (val
))
5481 || (comp_code
== NE_EXPR
&& integer_onep (val
))))
5483 gimple def_stmt
= SSA_NAME_DEF_STMT (name
);
5485 /* For BIT_IOR_EXPR only if NAME == 0 both operands have
5486 necessarily zero value, or if type-precision is one. */
5487 if (is_gimple_assign (def_stmt
)
5488 && (gimple_assign_rhs_code (def_stmt
) == BIT_IOR_EXPR
5489 && (TYPE_PRECISION (TREE_TYPE (name
)) == 1
5490 || comp_code
== EQ_EXPR
)))
5492 tree op0
= gimple_assign_rhs1 (def_stmt
);
5493 tree op1
= gimple_assign_rhs2 (def_stmt
);
5494 retval
|= register_edge_assert_for_1 (op0
, EQ_EXPR
, e
, si
);
5495 retval
|= register_edge_assert_for_1 (op1
, EQ_EXPR
, e
, si
);
5503 /* Determine whether the outgoing edges of BB should receive an
5504 ASSERT_EXPR for each of the operands of BB's LAST statement.
5505 The last statement of BB must be a COND_EXPR.
5507 If any of the sub-graphs rooted at BB have an interesting use of
5508 the predicate operands, an assert location node is added to the
5509 list of assertions for the corresponding operands. */
5512 find_conditional_asserts (basic_block bb
, gimple last
)
5515 gimple_stmt_iterator bsi
;
5521 need_assert
= false;
5522 bsi
= gsi_for_stmt (last
);
5524 /* Look for uses of the operands in each of the sub-graphs
5525 rooted at BB. We need to check each of the outgoing edges
5526 separately, so that we know what kind of ASSERT_EXPR to
5528 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
5533 /* Register the necessary assertions for each operand in the
5534 conditional predicate. */
5535 FOR_EACH_SSA_TREE_OPERAND (op
, last
, iter
, SSA_OP_USE
)
5537 need_assert
|= register_edge_assert_for (op
, e
, bsi
,
5538 gimple_cond_code (last
),
5539 gimple_cond_lhs (last
),
5540 gimple_cond_rhs (last
));
5553 /* Compare two case labels sorting first by the destination bb index
5554 and then by the case value. */
5557 compare_case_labels (const void *p1
, const void *p2
)
5559 const struct case_info
*ci1
= (const struct case_info
*) p1
;
5560 const struct case_info
*ci2
= (const struct case_info
*) p2
;
5561 int idx1
= ci1
->bb
->index
;
5562 int idx2
= ci2
->bb
->index
;
5566 else if (idx1
== idx2
)
5568 /* Make sure the default label is first in a group. */
5569 if (!CASE_LOW (ci1
->expr
))
5571 else if (!CASE_LOW (ci2
->expr
))
5574 return tree_int_cst_compare (CASE_LOW (ci1
->expr
),
5575 CASE_LOW (ci2
->expr
));
5581 /* Determine whether the outgoing edges of BB should receive an
5582 ASSERT_EXPR for each of the operands of BB's LAST statement.
5583 The last statement of BB must be a SWITCH_EXPR.
5585 If any of the sub-graphs rooted at BB have an interesting use of
5586 the predicate operands, an assert location node is added to the
5587 list of assertions for the corresponding operands. */
5590 find_switch_asserts (basic_block bb
, gimple last
)
5593 gimple_stmt_iterator bsi
;
5596 struct case_info
*ci
;
5597 size_t n
= gimple_switch_num_labels (last
);
5598 #if GCC_VERSION >= 4000
5601 /* Work around GCC 3.4 bug (PR 37086). */
5602 volatile unsigned int idx
;
5605 need_assert
= false;
5606 bsi
= gsi_for_stmt (last
);
5607 op
= gimple_switch_index (last
);
5608 if (TREE_CODE (op
) != SSA_NAME
)
5611 /* Build a vector of case labels sorted by destination label. */
5612 ci
= XNEWVEC (struct case_info
, n
);
5613 for (idx
= 0; idx
< n
; ++idx
)
5615 ci
[idx
].expr
= gimple_switch_label (last
, idx
);
5616 ci
[idx
].bb
= label_to_block (CASE_LABEL (ci
[idx
].expr
));
5618 qsort (ci
, n
, sizeof (struct case_info
), compare_case_labels
);
5620 for (idx
= 0; idx
< n
; ++idx
)
5623 tree cl
= ci
[idx
].expr
;
5624 basic_block cbb
= ci
[idx
].bb
;
5626 min
= CASE_LOW (cl
);
5627 max
= CASE_HIGH (cl
);
5629 /* If there are multiple case labels with the same destination
5630 we need to combine them to a single value range for the edge. */
5631 if (idx
+ 1 < n
&& cbb
== ci
[idx
+ 1].bb
)
5633 /* Skip labels until the last of the group. */
5636 } while (idx
< n
&& cbb
== ci
[idx
].bb
);
5639 /* Pick up the maximum of the case label range. */
5640 if (CASE_HIGH (ci
[idx
].expr
))
5641 max
= CASE_HIGH (ci
[idx
].expr
);
5643 max
= CASE_LOW (ci
[idx
].expr
);
5646 /* Nothing to do if the range includes the default label until we
5647 can register anti-ranges. */
5648 if (min
== NULL_TREE
)
5651 /* Find the edge to register the assert expr on. */
5652 e
= find_edge (bb
, cbb
);
5654 /* Register the necessary assertions for the operand in the
5656 need_assert
|= register_edge_assert_for (op
, e
, bsi
,
5657 max
? GE_EXPR
: EQ_EXPR
,
5659 fold_convert (TREE_TYPE (op
),
5663 need_assert
|= register_edge_assert_for (op
, e
, bsi
, LE_EXPR
,
5665 fold_convert (TREE_TYPE (op
),
5675 /* Traverse all the statements in block BB looking for statements that
5676 may generate useful assertions for the SSA names in their operand.
5677 If a statement produces a useful assertion A for name N_i, then the
5678 list of assertions already generated for N_i is scanned to
5679 determine if A is actually needed.
5681 If N_i already had the assertion A at a location dominating the
5682 current location, then nothing needs to be done. Otherwise, the
5683 new location for A is recorded instead.
5685 1- For every statement S in BB, all the variables used by S are
5686 added to bitmap FOUND_IN_SUBGRAPH.
5688 2- If statement S uses an operand N in a way that exposes a known
5689 value range for N, then if N was not already generated by an
5690 ASSERT_EXPR, create a new assert location for N. For instance,
5691 if N is a pointer and the statement dereferences it, we can
5692 assume that N is not NULL.
5694 3- COND_EXPRs are a special case of #2. We can derive range
5695 information from the predicate but need to insert different
5696 ASSERT_EXPRs for each of the sub-graphs rooted at the
5697 conditional block. If the last statement of BB is a conditional
5698 expression of the form 'X op Y', then
5700 a) Remove X and Y from the set FOUND_IN_SUBGRAPH.
5702 b) If the conditional is the only entry point to the sub-graph
5703 corresponding to the THEN_CLAUSE, recurse into it. On
5704 return, if X and/or Y are marked in FOUND_IN_SUBGRAPH, then
5705 an ASSERT_EXPR is added for the corresponding variable.
5707 c) Repeat step (b) on the ELSE_CLAUSE.
5709 d) Mark X and Y in FOUND_IN_SUBGRAPH.
5718 In this case, an assertion on the THEN clause is useful to
5719 determine that 'a' is always 9 on that edge. However, an assertion
5720 on the ELSE clause would be unnecessary.
5722 4- If BB does not end in a conditional expression, then we recurse
5723 into BB's dominator children.
5725 At the end of the recursive traversal, every SSA name will have a
5726 list of locations where ASSERT_EXPRs should be added. When a new
5727 location for name N is found, it is registered by calling
5728 register_new_assert_for. That function keeps track of all the
5729 registered assertions to prevent adding unnecessary assertions.
5730 For instance, if a pointer P_4 is dereferenced more than once in a
5731 dominator tree, only the location dominating all the dereference of
5732 P_4 will receive an ASSERT_EXPR.
5734 If this function returns true, then it means that there are names
5735 for which we need to generate ASSERT_EXPRs. Those assertions are
5736 inserted by process_assert_insertions. */
5739 find_assert_locations_1 (basic_block bb
, sbitmap live
)
5741 gimple_stmt_iterator si
;
5745 need_assert
= false;
5746 last
= last_stmt (bb
);
5748 /* If BB's last statement is a conditional statement involving integer
5749 operands, determine if we need to add ASSERT_EXPRs. */
5751 && gimple_code (last
) == GIMPLE_COND
5752 && !fp_predicate (last
)
5753 && !ZERO_SSA_OPERANDS (last
, SSA_OP_USE
))
5754 need_assert
|= find_conditional_asserts (bb
, last
);
5756 /* If BB's last statement is a switch statement involving integer
5757 operands, determine if we need to add ASSERT_EXPRs. */
5759 && gimple_code (last
) == GIMPLE_SWITCH
5760 && !ZERO_SSA_OPERANDS (last
, SSA_OP_USE
))
5761 need_assert
|= find_switch_asserts (bb
, last
);
5763 /* Traverse all the statements in BB marking used names and looking
5764 for statements that may infer assertions for their used operands. */
5765 for (si
= gsi_last_bb (bb
); !gsi_end_p (si
); gsi_prev (&si
))
5771 stmt
= gsi_stmt (si
);
5773 if (is_gimple_debug (stmt
))
5776 /* See if we can derive an assertion for any of STMT's operands. */
5777 FOR_EACH_SSA_TREE_OPERAND (op
, stmt
, i
, SSA_OP_USE
)
5780 enum tree_code comp_code
;
5782 /* If op is not live beyond this stmt, do not bother to insert
5784 if (!bitmap_bit_p (live
, SSA_NAME_VERSION (op
)))
5787 /* If OP is used in such a way that we can infer a value
5788 range for it, and we don't find a previous assertion for
5789 it, create a new assertion location node for OP. */
5790 if (infer_value_range (stmt
, op
, &comp_code
, &value
))
5792 /* If we are able to infer a nonzero value range for OP,
5793 then walk backwards through the use-def chain to see if OP
5794 was set via a typecast.
5796 If so, then we can also infer a nonzero value range
5797 for the operand of the NOP_EXPR. */
5798 if (comp_code
== NE_EXPR
&& integer_zerop (value
))
5801 gimple def_stmt
= SSA_NAME_DEF_STMT (t
);
5803 while (is_gimple_assign (def_stmt
)
5804 && gimple_assign_rhs_code (def_stmt
) == NOP_EXPR
5806 (gimple_assign_rhs1 (def_stmt
)) == SSA_NAME
5808 (TREE_TYPE (gimple_assign_rhs1 (def_stmt
))))
5810 t
= gimple_assign_rhs1 (def_stmt
);
5811 def_stmt
= SSA_NAME_DEF_STMT (t
);
5813 /* Note we want to register the assert for the
5814 operand of the NOP_EXPR after SI, not after the
5816 if (! has_single_use (t
))
5818 register_new_assert_for (t
, t
, comp_code
, value
,
5825 register_new_assert_for (op
, op
, comp_code
, value
, bb
, NULL
, si
);
5831 FOR_EACH_SSA_TREE_OPERAND (op
, stmt
, i
, SSA_OP_USE
)
5832 bitmap_set_bit (live
, SSA_NAME_VERSION (op
));
5833 FOR_EACH_SSA_TREE_OPERAND (op
, stmt
, i
, SSA_OP_DEF
)
5834 bitmap_clear_bit (live
, SSA_NAME_VERSION (op
));
5837 /* Traverse all PHI nodes in BB, updating live. */
5838 for (si
= gsi_start_phis (bb
); !gsi_end_p (si
); gsi_next (&si
))
5840 use_operand_p arg_p
;
5842 gimple phi
= gsi_stmt (si
);
5843 tree res
= gimple_phi_result (phi
);
5845 if (virtual_operand_p (res
))
5848 FOR_EACH_PHI_ARG (arg_p
, phi
, i
, SSA_OP_USE
)
5850 tree arg
= USE_FROM_PTR (arg_p
);
5851 if (TREE_CODE (arg
) == SSA_NAME
)
5852 bitmap_set_bit (live
, SSA_NAME_VERSION (arg
));
5855 bitmap_clear_bit (live
, SSA_NAME_VERSION (res
));
5861 /* Do an RPO walk over the function computing SSA name liveness
5862 on-the-fly and deciding on assert expressions to insert.
5863 Returns true if there are assert expressions to be inserted. */
5866 find_assert_locations (void)
5868 int *rpo
= XNEWVEC (int, last_basic_block_for_fn (cfun
));
5869 int *bb_rpo
= XNEWVEC (int, last_basic_block_for_fn (cfun
));
5870 int *last_rpo
= XCNEWVEC (int, last_basic_block_for_fn (cfun
));
5874 live
= XCNEWVEC (sbitmap
, last_basic_block_for_fn (cfun
));
5875 rpo_cnt
= pre_and_rev_post_order_compute (NULL
, rpo
, false);
5876 for (i
= 0; i
< rpo_cnt
; ++i
)
5879 /* Pre-seed loop latch liveness from loop header PHI nodes. Due to
5880 the order we compute liveness and insert asserts we otherwise
5881 fail to insert asserts into the loop latch. */
5883 FOR_EACH_LOOP (loop
, 0)
5885 i
= loop
->latch
->index
;
5886 unsigned int j
= single_succ_edge (loop
->latch
)->dest_idx
;
5887 for (gimple_stmt_iterator gsi
= gsi_start_phis (loop
->header
);
5888 !gsi_end_p (gsi
); gsi_next (&gsi
))
5890 gimple phi
= gsi_stmt (gsi
);
5891 if (virtual_operand_p (gimple_phi_result (phi
)))
5893 tree arg
= gimple_phi_arg_def (phi
, j
);
5894 if (TREE_CODE (arg
) == SSA_NAME
)
5896 if (live
[i
] == NULL
)
5898 live
[i
] = sbitmap_alloc (num_ssa_names
);
5899 bitmap_clear (live
[i
]);
5901 bitmap_set_bit (live
[i
], SSA_NAME_VERSION (arg
));
5906 need_asserts
= false;
5907 for (i
= rpo_cnt
- 1; i
>= 0; --i
)
5909 basic_block bb
= BASIC_BLOCK_FOR_FN (cfun
, rpo
[i
]);
5915 live
[rpo
[i
]] = sbitmap_alloc (num_ssa_names
);
5916 bitmap_clear (live
[rpo
[i
]]);
5919 /* Process BB and update the live information with uses in
5921 need_asserts
|= find_assert_locations_1 (bb
, live
[rpo
[i
]]);
5923 /* Merge liveness into the predecessor blocks and free it. */
5924 if (!bitmap_empty_p (live
[rpo
[i
]]))
5927 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
5929 int pred
= e
->src
->index
;
5930 if ((e
->flags
& EDGE_DFS_BACK
) || pred
== ENTRY_BLOCK
)
5935 live
[pred
] = sbitmap_alloc (num_ssa_names
);
5936 bitmap_clear (live
[pred
]);
5938 bitmap_ior (live
[pred
], live
[pred
], live
[rpo
[i
]]);
5940 if (bb_rpo
[pred
] < pred_rpo
)
5941 pred_rpo
= bb_rpo
[pred
];
5944 /* Record the RPO number of the last visited block that needs
5945 live information from this block. */
5946 last_rpo
[rpo
[i
]] = pred_rpo
;
5950 sbitmap_free (live
[rpo
[i
]]);
5951 live
[rpo
[i
]] = NULL
;
5954 /* We can free all successors live bitmaps if all their
5955 predecessors have been visited already. */
5956 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
5957 if (last_rpo
[e
->dest
->index
] == i
5958 && live
[e
->dest
->index
])
5960 sbitmap_free (live
[e
->dest
->index
]);
5961 live
[e
->dest
->index
] = NULL
;
5966 XDELETEVEC (bb_rpo
);
5967 XDELETEVEC (last_rpo
);
5968 for (i
= 0; i
< last_basic_block_for_fn (cfun
); ++i
)
5970 sbitmap_free (live
[i
]);
5973 return need_asserts
;
5976 /* Create an ASSERT_EXPR for NAME and insert it in the location
5977 indicated by LOC. Return true if we made any edge insertions. */
5980 process_assert_insertions_for (tree name
, assert_locus_t loc
)
5982 /* Build the comparison expression NAME_i COMP_CODE VAL. */
5989 /* If we have X <=> X do not insert an assert expr for that. */
5990 if (loc
->expr
== loc
->val
)
5993 cond
= build2 (loc
->comp_code
, boolean_type_node
, loc
->expr
, loc
->val
);
5994 assert_stmt
= build_assert_expr_for (cond
, name
);
5997 /* We have been asked to insert the assertion on an edge. This
5998 is used only by COND_EXPR and SWITCH_EXPR assertions. */
5999 gcc_checking_assert (gimple_code (gsi_stmt (loc
->si
)) == GIMPLE_COND
6000 || (gimple_code (gsi_stmt (loc
->si
))
6003 gsi_insert_on_edge (loc
->e
, assert_stmt
);
6007 /* Otherwise, we can insert right after LOC->SI iff the
6008 statement must not be the last statement in the block. */
6009 stmt
= gsi_stmt (loc
->si
);
6010 if (!stmt_ends_bb_p (stmt
))
6012 gsi_insert_after (&loc
->si
, assert_stmt
, GSI_SAME_STMT
);
6016 /* If STMT must be the last statement in BB, we can only insert new
6017 assertions on the non-abnormal edge out of BB. Note that since
6018 STMT is not control flow, there may only be one non-abnormal edge
6020 FOR_EACH_EDGE (e
, ei
, loc
->bb
->succs
)
6021 if (!(e
->flags
& EDGE_ABNORMAL
))
6023 gsi_insert_on_edge (e
, assert_stmt
);
6031 /* Process all the insertions registered for every name N_i registered
6032 in NEED_ASSERT_FOR. The list of assertions to be inserted are
6033 found in ASSERTS_FOR[i]. */
6036 process_assert_insertions (void)
6040 bool update_edges_p
= false;
6041 int num_asserts
= 0;
6043 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6044 dump_all_asserts (dump_file
);
6046 EXECUTE_IF_SET_IN_BITMAP (need_assert_for
, 0, i
, bi
)
6048 assert_locus_t loc
= asserts_for
[i
];
6053 assert_locus_t next
= loc
->next
;
6054 update_edges_p
|= process_assert_insertions_for (ssa_name (i
), loc
);
6062 gsi_commit_edge_inserts ();
6064 statistics_counter_event (cfun
, "Number of ASSERT_EXPR expressions inserted",
6069 /* Traverse the flowgraph looking for conditional jumps to insert range
6070 expressions. These range expressions are meant to provide information
6071 to optimizations that need to reason in terms of value ranges. They
6072 will not be expanded into RTL. For instance, given:
6081 this pass will transform the code into:
6087 x = ASSERT_EXPR <x, x < y>
6092 y = ASSERT_EXPR <y, x <= y>
6096 The idea is that once copy and constant propagation have run, other
6097 optimizations will be able to determine what ranges of values can 'x'
6098 take in different paths of the code, simply by checking the reaching
6099 definition of 'x'. */
6102 insert_range_assertions (void)
6104 need_assert_for
= BITMAP_ALLOC (NULL
);
6105 asserts_for
= XCNEWVEC (assert_locus_t
, num_ssa_names
);
6107 calculate_dominance_info (CDI_DOMINATORS
);
6109 if (find_assert_locations ())
6111 process_assert_insertions ();
6112 update_ssa (TODO_update_ssa_no_phi
);
6115 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6117 fprintf (dump_file
, "\nSSA form after inserting ASSERT_EXPRs\n");
6118 dump_function_to_file (current_function_decl
, dump_file
, dump_flags
);
6122 BITMAP_FREE (need_assert_for
);
6125 /* Checks one ARRAY_REF in REF, located at LOCUS. Ignores flexible arrays
6126 and "struct" hacks. If VRP can determine that the
6127 array subscript is a constant, check if it is outside valid
6128 range. If the array subscript is a RANGE, warn if it is
6129 non-overlapping with valid range.
6130 IGNORE_OFF_BY_ONE is true if the ARRAY_REF is inside a ADDR_EXPR. */
6133 check_array_ref (location_t location
, tree ref
, bool ignore_off_by_one
)
6135 value_range_t
* vr
= NULL
;
6136 tree low_sub
, up_sub
;
6137 tree low_bound
, up_bound
, up_bound_p1
;
6140 if (TREE_NO_WARNING (ref
))
6143 low_sub
= up_sub
= TREE_OPERAND (ref
, 1);
6144 up_bound
= array_ref_up_bound (ref
);
6146 /* Can not check flexible arrays. */
6148 || TREE_CODE (up_bound
) != INTEGER_CST
)
6151 /* Accesses to trailing arrays via pointers may access storage
6152 beyond the types array bounds. */
6153 base
= get_base_address (ref
);
6154 if (base
&& TREE_CODE (base
) == MEM_REF
)
6156 tree cref
, next
= NULL_TREE
;
6158 if (TREE_CODE (TREE_OPERAND (ref
, 0)) != COMPONENT_REF
)
6161 cref
= TREE_OPERAND (ref
, 0);
6162 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (cref
, 0))) == RECORD_TYPE
)
6163 for (next
= DECL_CHAIN (TREE_OPERAND (cref
, 1));
6164 next
&& TREE_CODE (next
) != FIELD_DECL
;
6165 next
= DECL_CHAIN (next
))
6168 /* If this is the last field in a struct type or a field in a
6169 union type do not warn. */
6174 low_bound
= array_ref_low_bound (ref
);
6175 up_bound_p1
= int_const_binop (PLUS_EXPR
, up_bound
,
6176 build_int_cst (TREE_TYPE (up_bound
), 1));
6178 if (TREE_CODE (low_sub
) == SSA_NAME
)
6180 vr
= get_value_range (low_sub
);
6181 if (vr
->type
== VR_RANGE
|| vr
->type
== VR_ANTI_RANGE
)
6183 low_sub
= vr
->type
== VR_RANGE
? vr
->max
: vr
->min
;
6184 up_sub
= vr
->type
== VR_RANGE
? vr
->min
: vr
->max
;
6188 if (vr
&& vr
->type
== VR_ANTI_RANGE
)
6190 if (TREE_CODE (up_sub
) == INTEGER_CST
6191 && tree_int_cst_lt (up_bound
, up_sub
)
6192 && TREE_CODE (low_sub
) == INTEGER_CST
6193 && tree_int_cst_lt (low_sub
, low_bound
))
6195 warning_at (location
, OPT_Warray_bounds
,
6196 "array subscript is outside array bounds");
6197 TREE_NO_WARNING (ref
) = 1;
6200 else if (TREE_CODE (up_sub
) == INTEGER_CST
6201 && (ignore_off_by_one
6202 ? (tree_int_cst_lt (up_bound
, up_sub
)
6203 && !tree_int_cst_equal (up_bound_p1
, up_sub
))
6204 : (tree_int_cst_lt (up_bound
, up_sub
)
6205 || tree_int_cst_equal (up_bound_p1
, up_sub
))))
6207 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6209 fprintf (dump_file
, "Array bound warning for ");
6210 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, ref
);
6211 fprintf (dump_file
, "\n");
6213 warning_at (location
, OPT_Warray_bounds
,
6214 "array subscript is above array bounds");
6215 TREE_NO_WARNING (ref
) = 1;
6217 else if (TREE_CODE (low_sub
) == INTEGER_CST
6218 && tree_int_cst_lt (low_sub
, low_bound
))
6220 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6222 fprintf (dump_file
, "Array bound warning for ");
6223 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, ref
);
6224 fprintf (dump_file
, "\n");
6226 warning_at (location
, OPT_Warray_bounds
,
6227 "array subscript is below array bounds");
6228 TREE_NO_WARNING (ref
) = 1;
6232 /* Searches if the expr T, located at LOCATION computes
6233 address of an ARRAY_REF, and call check_array_ref on it. */
6236 search_for_addr_array (tree t
, location_t location
)
6238 while (TREE_CODE (t
) == SSA_NAME
)
6240 gimple g
= SSA_NAME_DEF_STMT (t
);
6242 if (gimple_code (g
) != GIMPLE_ASSIGN
)
6245 if (get_gimple_rhs_class (gimple_assign_rhs_code (g
))
6246 != GIMPLE_SINGLE_RHS
)
6249 t
= gimple_assign_rhs1 (g
);
6253 /* We are only interested in addresses of ARRAY_REF's. */
6254 if (TREE_CODE (t
) != ADDR_EXPR
)
6257 /* Check each ARRAY_REFs in the reference chain. */
6260 if (TREE_CODE (t
) == ARRAY_REF
)
6261 check_array_ref (location
, t
, true /*ignore_off_by_one*/);
6263 t
= TREE_OPERAND (t
, 0);
6265 while (handled_component_p (t
));
6267 if (TREE_CODE (t
) == MEM_REF
6268 && TREE_CODE (TREE_OPERAND (t
, 0)) == ADDR_EXPR
6269 && !TREE_NO_WARNING (t
))
6271 tree tem
= TREE_OPERAND (TREE_OPERAND (t
, 0), 0);
6272 tree low_bound
, up_bound
, el_sz
;
6274 if (TREE_CODE (TREE_TYPE (tem
)) != ARRAY_TYPE
6275 || TREE_CODE (TREE_TYPE (TREE_TYPE (tem
))) == ARRAY_TYPE
6276 || !TYPE_DOMAIN (TREE_TYPE (tem
)))
6279 low_bound
= TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (tem
)));
6280 up_bound
= TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (tem
)));
6281 el_sz
= TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (tem
)));
6283 || TREE_CODE (low_bound
) != INTEGER_CST
6285 || TREE_CODE (up_bound
) != INTEGER_CST
6287 || TREE_CODE (el_sz
) != INTEGER_CST
)
6290 idx
= mem_ref_offset (t
);
6291 idx
= wi::sdiv_trunc (idx
, wi::to_offset (el_sz
));
6292 if (wi::lts_p (idx
, 0))
6294 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6296 fprintf (dump_file
, "Array bound warning for ");
6297 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, t
);
6298 fprintf (dump_file
, "\n");
6300 warning_at (location
, OPT_Warray_bounds
,
6301 "array subscript is below array bounds");
6302 TREE_NO_WARNING (t
) = 1;
6304 else if (wi::gts_p (idx
, (wi::to_offset (up_bound
)
6305 - wi::to_offset (low_bound
) + 1)))
6307 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6309 fprintf (dump_file
, "Array bound warning for ");
6310 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, t
);
6311 fprintf (dump_file
, "\n");
6313 warning_at (location
, OPT_Warray_bounds
,
6314 "array subscript is above array bounds");
6315 TREE_NO_WARNING (t
) = 1;
6320 /* walk_tree() callback that checks if *TP is
6321 an ARRAY_REF inside an ADDR_EXPR (in which an array
6322 subscript one outside the valid range is allowed). Call
6323 check_array_ref for each ARRAY_REF found. The location is
6327 check_array_bounds (tree
*tp
, int *walk_subtree
, void *data
)
6330 struct walk_stmt_info
*wi
= (struct walk_stmt_info
*) data
;
6331 location_t location
;
6333 if (EXPR_HAS_LOCATION (t
))
6334 location
= EXPR_LOCATION (t
);
6337 location_t
*locp
= (location_t
*) wi
->info
;
6341 *walk_subtree
= TRUE
;
6343 if (TREE_CODE (t
) == ARRAY_REF
)
6344 check_array_ref (location
, t
, false /*ignore_off_by_one*/);
6346 if (TREE_CODE (t
) == MEM_REF
6347 || (TREE_CODE (t
) == RETURN_EXPR
&& TREE_OPERAND (t
, 0)))
6348 search_for_addr_array (TREE_OPERAND (t
, 0), location
);
6350 if (TREE_CODE (t
) == ADDR_EXPR
)
6351 *walk_subtree
= FALSE
;
6356 /* Walk over all statements of all reachable BBs and call check_array_bounds
6360 check_all_array_refs (void)
6363 gimple_stmt_iterator si
;
6365 FOR_EACH_BB_FN (bb
, cfun
)
6369 bool executable
= false;
6371 /* Skip blocks that were found to be unreachable. */
6372 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
6373 executable
|= !!(e
->flags
& EDGE_EXECUTABLE
);
6377 for (si
= gsi_start_bb (bb
); !gsi_end_p (si
); gsi_next (&si
))
6379 gimple stmt
= gsi_stmt (si
);
6380 struct walk_stmt_info wi
;
6381 if (!gimple_has_location (stmt
))
6384 if (is_gimple_call (stmt
))
6387 size_t n
= gimple_call_num_args (stmt
);
6388 for (i
= 0; i
< n
; i
++)
6390 tree arg
= gimple_call_arg (stmt
, i
);
6391 search_for_addr_array (arg
, gimple_location (stmt
));
6396 memset (&wi
, 0, sizeof (wi
));
6397 wi
.info
= CONST_CAST (void *, (const void *)
6398 gimple_location_ptr (stmt
));
6400 walk_gimple_op (gsi_stmt (si
),
6408 /* Return true if all imm uses of VAR are either in STMT, or
6409 feed (optionally through a chain of single imm uses) GIMPLE_COND
6410 in basic block COND_BB. */
6413 all_imm_uses_in_stmt_or_feed_cond (tree var
, gimple stmt
, basic_block cond_bb
)
6415 use_operand_p use_p
, use2_p
;
6416 imm_use_iterator iter
;
6418 FOR_EACH_IMM_USE_FAST (use_p
, iter
, var
)
6419 if (USE_STMT (use_p
) != stmt
)
6421 gimple use_stmt
= USE_STMT (use_p
), use_stmt2
;
6422 if (is_gimple_debug (use_stmt
))
6424 while (is_gimple_assign (use_stmt
)
6425 && TREE_CODE (gimple_assign_lhs (use_stmt
)) == SSA_NAME
6426 && single_imm_use (gimple_assign_lhs (use_stmt
),
6427 &use2_p
, &use_stmt2
))
6428 use_stmt
= use_stmt2
;
6429 if (gimple_code (use_stmt
) != GIMPLE_COND
6430 || gimple_bb (use_stmt
) != cond_bb
)
6443 __builtin_unreachable ();
6445 x_5 = ASSERT_EXPR <x_3, ...>;
6446 If x_3 has no other immediate uses (checked by caller),
6447 var is the x_3 var from ASSERT_EXPR, we can clear low 5 bits
6448 from the non-zero bitmask. */
6451 maybe_set_nonzero_bits (basic_block bb
, tree var
)
6453 edge e
= single_pred_edge (bb
);
6454 basic_block cond_bb
= e
->src
;
6455 gimple stmt
= last_stmt (cond_bb
);
6459 || gimple_code (stmt
) != GIMPLE_COND
6460 || gimple_cond_code (stmt
) != ((e
->flags
& EDGE_TRUE_VALUE
)
6461 ? EQ_EXPR
: NE_EXPR
)
6462 || TREE_CODE (gimple_cond_lhs (stmt
)) != SSA_NAME
6463 || !integer_zerop (gimple_cond_rhs (stmt
)))
6466 stmt
= SSA_NAME_DEF_STMT (gimple_cond_lhs (stmt
));
6467 if (!is_gimple_assign (stmt
)
6468 || gimple_assign_rhs_code (stmt
) != BIT_AND_EXPR
6469 || TREE_CODE (gimple_assign_rhs2 (stmt
)) != INTEGER_CST
)
6471 if (gimple_assign_rhs1 (stmt
) != var
)
6475 if (TREE_CODE (gimple_assign_rhs1 (stmt
)) != SSA_NAME
)
6477 stmt2
= SSA_NAME_DEF_STMT (gimple_assign_rhs1 (stmt
));
6478 if (!gimple_assign_cast_p (stmt2
)
6479 || gimple_assign_rhs1 (stmt2
) != var
6480 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (stmt2
))
6481 || (TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (stmt
)))
6482 != TYPE_PRECISION (TREE_TYPE (var
))))
6485 cst
= gimple_assign_rhs2 (stmt
);
6486 set_nonzero_bits (var
, wi::bit_and_not (get_nonzero_bits (var
), cst
));
6489 /* Convert range assertion expressions into the implied copies and
6490 copy propagate away the copies. Doing the trivial copy propagation
6491 here avoids the need to run the full copy propagation pass after
6494 FIXME, this will eventually lead to copy propagation removing the
6495 names that had useful range information attached to them. For
6496 instance, if we had the assertion N_i = ASSERT_EXPR <N_j, N_j > 3>,
6497 then N_i will have the range [3, +INF].
6499 However, by converting the assertion into the implied copy
6500 operation N_i = N_j, we will then copy-propagate N_j into the uses
6501 of N_i and lose the range information. We may want to hold on to
6502 ASSERT_EXPRs a little while longer as the ranges could be used in
6503 things like jump threading.
6505 The problem with keeping ASSERT_EXPRs around is that passes after
6506 VRP need to handle them appropriately.
6508 Another approach would be to make the range information a first
6509 class property of the SSA_NAME so that it can be queried from
6510 any pass. This is made somewhat more complex by the need for
6511 multiple ranges to be associated with one SSA_NAME. */
6514 remove_range_assertions (void)
6517 gimple_stmt_iterator si
;
6518 /* 1 if looking at ASSERT_EXPRs immediately at the beginning of
6519 a basic block preceeded by GIMPLE_COND branching to it and
6520 __builtin_trap, -1 if not yet checked, 0 otherwise. */
6523 /* Note that the BSI iterator bump happens at the bottom of the
6524 loop and no bump is necessary if we're removing the statement
6525 referenced by the current BSI. */
6526 FOR_EACH_BB_FN (bb
, cfun
)
6527 for (si
= gsi_after_labels (bb
), is_unreachable
= -1; !gsi_end_p (si
);)
6529 gimple stmt
= gsi_stmt (si
);
6532 if (is_gimple_assign (stmt
)
6533 && gimple_assign_rhs_code (stmt
) == ASSERT_EXPR
)
6535 tree lhs
= gimple_assign_lhs (stmt
);
6536 tree rhs
= gimple_assign_rhs1 (stmt
);
6538 tree cond
= fold (ASSERT_EXPR_COND (rhs
));
6539 use_operand_p use_p
;
6540 imm_use_iterator iter
;
6542 gcc_assert (cond
!= boolean_false_node
);
6544 var
= ASSERT_EXPR_VAR (rhs
);
6545 gcc_assert (TREE_CODE (var
) == SSA_NAME
);
6547 if (!POINTER_TYPE_P (TREE_TYPE (lhs
))
6548 && SSA_NAME_RANGE_INFO (lhs
))
6550 if (is_unreachable
== -1)
6553 if (single_pred_p (bb
)
6554 && assert_unreachable_fallthru_edge_p
6555 (single_pred_edge (bb
)))
6559 if (x_7 >= 10 && x_7 < 20)
6560 __builtin_unreachable ();
6561 x_8 = ASSERT_EXPR <x_7, ...>;
6562 if the only uses of x_7 are in the ASSERT_EXPR and
6563 in the condition. In that case, we can copy the
6564 range info from x_8 computed in this pass also
6567 && all_imm_uses_in_stmt_or_feed_cond (var
, stmt
,
6570 set_range_info (var
, SSA_NAME_RANGE_TYPE (lhs
),
6571 SSA_NAME_RANGE_INFO (lhs
)->get_min (),
6572 SSA_NAME_RANGE_INFO (lhs
)->get_max ());
6573 maybe_set_nonzero_bits (bb
, var
);
6577 /* Propagate the RHS into every use of the LHS. */
6578 FOR_EACH_IMM_USE_STMT (use_stmt
, iter
, lhs
)
6579 FOR_EACH_IMM_USE_ON_STMT (use_p
, iter
)
6580 SET_USE (use_p
, var
);
6582 /* And finally, remove the copy, it is not needed. */
6583 gsi_remove (&si
, true);
6584 release_defs (stmt
);
6595 /* Return true if STMT is interesting for VRP. */
6598 stmt_interesting_for_vrp (gimple stmt
)
6600 if (gimple_code (stmt
) == GIMPLE_PHI
)
6602 tree res
= gimple_phi_result (stmt
);
6603 return (!virtual_operand_p (res
)
6604 && (INTEGRAL_TYPE_P (TREE_TYPE (res
))
6605 || POINTER_TYPE_P (TREE_TYPE (res
))));
6607 else if (is_gimple_assign (stmt
) || is_gimple_call (stmt
))
6609 tree lhs
= gimple_get_lhs (stmt
);
6611 /* In general, assignments with virtual operands are not useful
6612 for deriving ranges, with the obvious exception of calls to
6613 builtin functions. */
6614 if (lhs
&& TREE_CODE (lhs
) == SSA_NAME
6615 && (INTEGRAL_TYPE_P (TREE_TYPE (lhs
))
6616 || POINTER_TYPE_P (TREE_TYPE (lhs
)))
6617 && (is_gimple_call (stmt
)
6618 || !gimple_vuse (stmt
)))
6621 else if (gimple_code (stmt
) == GIMPLE_COND
6622 || gimple_code (stmt
) == GIMPLE_SWITCH
)
6629 /* Initialize local data structures for VRP. */
6632 vrp_initialize (void)
6636 values_propagated
= false;
6637 num_vr_values
= num_ssa_names
;
6638 vr_value
= XCNEWVEC (value_range_t
*, num_vr_values
);
6639 vr_phi_edge_counts
= XCNEWVEC (int, num_ssa_names
);
6641 FOR_EACH_BB_FN (bb
, cfun
)
6643 gimple_stmt_iterator si
;
6645 for (si
= gsi_start_phis (bb
); !gsi_end_p (si
); gsi_next (&si
))
6647 gimple phi
= gsi_stmt (si
);
6648 if (!stmt_interesting_for_vrp (phi
))
6650 tree lhs
= PHI_RESULT (phi
);
6651 set_value_range_to_varying (get_value_range (lhs
));
6652 prop_set_simulate_again (phi
, false);
6655 prop_set_simulate_again (phi
, true);
6658 for (si
= gsi_start_bb (bb
); !gsi_end_p (si
); gsi_next (&si
))
6660 gimple stmt
= gsi_stmt (si
);
6662 /* If the statement is a control insn, then we do not
6663 want to avoid simulating the statement once. Failure
6664 to do so means that those edges will never get added. */
6665 if (stmt_ends_bb_p (stmt
))
6666 prop_set_simulate_again (stmt
, true);
6667 else if (!stmt_interesting_for_vrp (stmt
))
6671 FOR_EACH_SSA_TREE_OPERAND (def
, stmt
, i
, SSA_OP_DEF
)
6672 set_value_range_to_varying (get_value_range (def
));
6673 prop_set_simulate_again (stmt
, false);
6676 prop_set_simulate_again (stmt
, true);
6681 /* Return the singleton value-range for NAME or NAME. */
6684 vrp_valueize (tree name
)
6686 if (TREE_CODE (name
) == SSA_NAME
)
6688 value_range_t
*vr
= get_value_range (name
);
6689 if (vr
->type
== VR_RANGE
6690 && (vr
->min
== vr
->max
6691 || operand_equal_p (vr
->min
, vr
->max
, 0)))
6697 /* Visit assignment STMT. If it produces an interesting range, record
6698 the SSA name in *OUTPUT_P. */
6700 static enum ssa_prop_result
6701 vrp_visit_assignment_or_call (gimple stmt
, tree
*output_p
)
6705 enum gimple_code code
= gimple_code (stmt
);
6706 lhs
= gimple_get_lhs (stmt
);
6708 /* We only keep track of ranges in integral and pointer types. */
6709 if (TREE_CODE (lhs
) == SSA_NAME
6710 && ((INTEGRAL_TYPE_P (TREE_TYPE (lhs
))
6711 /* It is valid to have NULL MIN/MAX values on a type. See
6712 build_range_type. */
6713 && TYPE_MIN_VALUE (TREE_TYPE (lhs
))
6714 && TYPE_MAX_VALUE (TREE_TYPE (lhs
)))
6715 || POINTER_TYPE_P (TREE_TYPE (lhs
))))
6717 value_range_t new_vr
= VR_INITIALIZER
;
6719 /* Try folding the statement to a constant first. */
6720 tree tem
= gimple_fold_stmt_to_constant (stmt
, vrp_valueize
);
6722 set_value_range_to_value (&new_vr
, tem
, NULL
);
6723 /* Then dispatch to value-range extracting functions. */
6724 else if (code
== GIMPLE_CALL
)
6725 extract_range_basic (&new_vr
, stmt
);
6727 extract_range_from_assignment (&new_vr
, stmt
);
6729 if (update_value_range (lhs
, &new_vr
))
6733 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6735 fprintf (dump_file
, "Found new range for ");
6736 print_generic_expr (dump_file
, lhs
, 0);
6737 fprintf (dump_file
, ": ");
6738 dump_value_range (dump_file
, &new_vr
);
6739 fprintf (dump_file
, "\n\n");
6742 if (new_vr
.type
== VR_VARYING
)
6743 return SSA_PROP_VARYING
;
6745 return SSA_PROP_INTERESTING
;
6748 return SSA_PROP_NOT_INTERESTING
;
6751 /* Every other statement produces no useful ranges. */
6752 FOR_EACH_SSA_TREE_OPERAND (def
, stmt
, iter
, SSA_OP_DEF
)
6753 set_value_range_to_varying (get_value_range (def
));
6755 return SSA_PROP_VARYING
;
6758 /* Helper that gets the value range of the SSA_NAME with version I
6759 or a symbolic range containing the SSA_NAME only if the value range
6760 is varying or undefined. */
6762 static inline value_range_t
6763 get_vr_for_comparison (int i
)
6765 value_range_t vr
= *get_value_range (ssa_name (i
));
6767 /* If name N_i does not have a valid range, use N_i as its own
6768 range. This allows us to compare against names that may
6769 have N_i in their ranges. */
6770 if (vr
.type
== VR_VARYING
|| vr
.type
== VR_UNDEFINED
)
6773 vr
.min
= ssa_name (i
);
6774 vr
.max
= ssa_name (i
);
6780 /* Compare all the value ranges for names equivalent to VAR with VAL
6781 using comparison code COMP. Return the same value returned by
6782 compare_range_with_value, including the setting of
6783 *STRICT_OVERFLOW_P. */
6786 compare_name_with_value (enum tree_code comp
, tree var
, tree val
,
6787 bool *strict_overflow_p
)
6793 int used_strict_overflow
;
6795 value_range_t equiv_vr
;
6797 /* Get the set of equivalences for VAR. */
6798 e
= get_value_range (var
)->equiv
;
6800 /* Start at -1. Set it to 0 if we do a comparison without relying
6801 on overflow, or 1 if all comparisons rely on overflow. */
6802 used_strict_overflow
= -1;
6804 /* Compare vars' value range with val. */
6805 equiv_vr
= get_vr_for_comparison (SSA_NAME_VERSION (var
));
6807 retval
= compare_range_with_value (comp
, &equiv_vr
, val
, &sop
);
6809 used_strict_overflow
= sop
? 1 : 0;
6811 /* If the equiv set is empty we have done all work we need to do. */
6815 && used_strict_overflow
> 0)
6816 *strict_overflow_p
= true;
6820 EXECUTE_IF_SET_IN_BITMAP (e
, 0, i
, bi
)
6822 equiv_vr
= get_vr_for_comparison (i
);
6824 t
= compare_range_with_value (comp
, &equiv_vr
, val
, &sop
);
6827 /* If we get different answers from different members
6828 of the equivalence set this check must be in a dead
6829 code region. Folding it to a trap representation
6830 would be correct here. For now just return don't-know. */
6840 used_strict_overflow
= 0;
6841 else if (used_strict_overflow
< 0)
6842 used_strict_overflow
= 1;
6847 && used_strict_overflow
> 0)
6848 *strict_overflow_p
= true;
6854 /* Given a comparison code COMP and names N1 and N2, compare all the
6855 ranges equivalent to N1 against all the ranges equivalent to N2
6856 to determine the value of N1 COMP N2. Return the same value
6857 returned by compare_ranges. Set *STRICT_OVERFLOW_P to indicate
6858 whether we relied on an overflow infinity in the comparison. */
6862 compare_names (enum tree_code comp
, tree n1
, tree n2
,
6863 bool *strict_overflow_p
)
6867 bitmap_iterator bi1
, bi2
;
6869 int used_strict_overflow
;
6870 static bitmap_obstack
*s_obstack
= NULL
;
6871 static bitmap s_e1
= NULL
, s_e2
= NULL
;
6873 /* Compare the ranges of every name equivalent to N1 against the
6874 ranges of every name equivalent to N2. */
6875 e1
= get_value_range (n1
)->equiv
;
6876 e2
= get_value_range (n2
)->equiv
;
6878 /* Use the fake bitmaps if e1 or e2 are not available. */
6879 if (s_obstack
== NULL
)
6881 s_obstack
= XNEW (bitmap_obstack
);
6882 bitmap_obstack_initialize (s_obstack
);
6883 s_e1
= BITMAP_ALLOC (s_obstack
);
6884 s_e2
= BITMAP_ALLOC (s_obstack
);
6891 /* Add N1 and N2 to their own set of equivalences to avoid
6892 duplicating the body of the loop just to check N1 and N2
6894 bitmap_set_bit (e1
, SSA_NAME_VERSION (n1
));
6895 bitmap_set_bit (e2
, SSA_NAME_VERSION (n2
));
6897 /* If the equivalence sets have a common intersection, then the two
6898 names can be compared without checking their ranges. */
6899 if (bitmap_intersect_p (e1
, e2
))
6901 bitmap_clear_bit (e1
, SSA_NAME_VERSION (n1
));
6902 bitmap_clear_bit (e2
, SSA_NAME_VERSION (n2
));
6904 return (comp
== EQ_EXPR
|| comp
== GE_EXPR
|| comp
== LE_EXPR
)
6906 : boolean_false_node
;
6909 /* Start at -1. Set it to 0 if we do a comparison without relying
6910 on overflow, or 1 if all comparisons rely on overflow. */
6911 used_strict_overflow
= -1;
6913 /* Otherwise, compare all the equivalent ranges. First, add N1 and
6914 N2 to their own set of equivalences to avoid duplicating the body
6915 of the loop just to check N1 and N2 ranges. */
6916 EXECUTE_IF_SET_IN_BITMAP (e1
, 0, i1
, bi1
)
6918 value_range_t vr1
= get_vr_for_comparison (i1
);
6920 t
= retval
= NULL_TREE
;
6921 EXECUTE_IF_SET_IN_BITMAP (e2
, 0, i2
, bi2
)
6925 value_range_t vr2
= get_vr_for_comparison (i2
);
6927 t
= compare_ranges (comp
, &vr1
, &vr2
, &sop
);
6930 /* If we get different answers from different members
6931 of the equivalence set this check must be in a dead
6932 code region. Folding it to a trap representation
6933 would be correct here. For now just return don't-know. */
6937 bitmap_clear_bit (e1
, SSA_NAME_VERSION (n1
));
6938 bitmap_clear_bit (e2
, SSA_NAME_VERSION (n2
));
6944 used_strict_overflow
= 0;
6945 else if (used_strict_overflow
< 0)
6946 used_strict_overflow
= 1;
6952 bitmap_clear_bit (e1
, SSA_NAME_VERSION (n1
));
6953 bitmap_clear_bit (e2
, SSA_NAME_VERSION (n2
));
6954 if (used_strict_overflow
> 0)
6955 *strict_overflow_p
= true;
6960 /* None of the equivalent ranges are useful in computing this
6962 bitmap_clear_bit (e1
, SSA_NAME_VERSION (n1
));
6963 bitmap_clear_bit (e2
, SSA_NAME_VERSION (n2
));
6967 /* Helper function for vrp_evaluate_conditional_warnv. */
6970 vrp_evaluate_conditional_warnv_with_ops_using_ranges (enum tree_code code
,
6972 bool * strict_overflow_p
)
6974 value_range_t
*vr0
, *vr1
;
6976 vr0
= (TREE_CODE (op0
) == SSA_NAME
) ? get_value_range (op0
) : NULL
;
6977 vr1
= (TREE_CODE (op1
) == SSA_NAME
) ? get_value_range (op1
) : NULL
;
6980 return compare_ranges (code
, vr0
, vr1
, strict_overflow_p
);
6981 else if (vr0
&& vr1
== NULL
)
6982 return compare_range_with_value (code
, vr0
, op1
, strict_overflow_p
);
6983 else if (vr0
== NULL
&& vr1
)
6984 return (compare_range_with_value
6985 (swap_tree_comparison (code
), vr1
, op0
, strict_overflow_p
));
6989 /* Helper function for vrp_evaluate_conditional_warnv. */
6992 vrp_evaluate_conditional_warnv_with_ops (enum tree_code code
, tree op0
,
6993 tree op1
, bool use_equiv_p
,
6994 bool *strict_overflow_p
, bool *only_ranges
)
6998 *only_ranges
= true;
7000 /* We only deal with integral and pointer types. */
7001 if (!INTEGRAL_TYPE_P (TREE_TYPE (op0
))
7002 && !POINTER_TYPE_P (TREE_TYPE (op0
)))
7008 && (ret
= vrp_evaluate_conditional_warnv_with_ops_using_ranges
7009 (code
, op0
, op1
, strict_overflow_p
)))
7011 *only_ranges
= false;
7012 if (TREE_CODE (op0
) == SSA_NAME
&& TREE_CODE (op1
) == SSA_NAME
)
7013 return compare_names (code
, op0
, op1
, strict_overflow_p
);
7014 else if (TREE_CODE (op0
) == SSA_NAME
)
7015 return compare_name_with_value (code
, op0
, op1
, strict_overflow_p
);
7016 else if (TREE_CODE (op1
) == SSA_NAME
)
7017 return (compare_name_with_value
7018 (swap_tree_comparison (code
), op1
, op0
, strict_overflow_p
));
7021 return vrp_evaluate_conditional_warnv_with_ops_using_ranges (code
, op0
, op1
,
7026 /* Given (CODE OP0 OP1) within STMT, try to simplify it based on value range
7027 information. Return NULL if the conditional can not be evaluated.
7028 The ranges of all the names equivalent with the operands in COND
7029 will be used when trying to compute the value. If the result is
7030 based on undefined signed overflow, issue a warning if
7034 vrp_evaluate_conditional (enum tree_code code
, tree op0
, tree op1
, gimple stmt
)
7040 /* Some passes and foldings leak constants with overflow flag set
7041 into the IL. Avoid doing wrong things with these and bail out. */
7042 if ((TREE_CODE (op0
) == INTEGER_CST
7043 && TREE_OVERFLOW (op0
))
7044 || (TREE_CODE (op1
) == INTEGER_CST
7045 && TREE_OVERFLOW (op1
)))
7049 ret
= vrp_evaluate_conditional_warnv_with_ops (code
, op0
, op1
, true, &sop
,
7054 enum warn_strict_overflow_code wc
;
7055 const char* warnmsg
;
7057 if (is_gimple_min_invariant (ret
))
7059 wc
= WARN_STRICT_OVERFLOW_CONDITIONAL
;
7060 warnmsg
= G_("assuming signed overflow does not occur when "
7061 "simplifying conditional to constant");
7065 wc
= WARN_STRICT_OVERFLOW_COMPARISON
;
7066 warnmsg
= G_("assuming signed overflow does not occur when "
7067 "simplifying conditional");
7070 if (issue_strict_overflow_warning (wc
))
7072 location_t location
;
7074 if (!gimple_has_location (stmt
))
7075 location
= input_location
;
7077 location
= gimple_location (stmt
);
7078 warning_at (location
, OPT_Wstrict_overflow
, "%s", warnmsg
);
7082 if (warn_type_limits
7083 && ret
&& only_ranges
7084 && TREE_CODE_CLASS (code
) == tcc_comparison
7085 && TREE_CODE (op0
) == SSA_NAME
)
7087 /* If the comparison is being folded and the operand on the LHS
7088 is being compared against a constant value that is outside of
7089 the natural range of OP0's type, then the predicate will
7090 always fold regardless of the value of OP0. If -Wtype-limits
7091 was specified, emit a warning. */
7092 tree type
= TREE_TYPE (op0
);
7093 value_range_t
*vr0
= get_value_range (op0
);
7095 if (vr0
->type
!= VR_VARYING
7096 && INTEGRAL_TYPE_P (type
)
7097 && vrp_val_is_min (vr0
->min
)
7098 && vrp_val_is_max (vr0
->max
)
7099 && is_gimple_min_invariant (op1
))
7101 location_t location
;
7103 if (!gimple_has_location (stmt
))
7104 location
= input_location
;
7106 location
= gimple_location (stmt
);
7108 warning_at (location
, OPT_Wtype_limits
,
7110 ? G_("comparison always false "
7111 "due to limited range of data type")
7112 : G_("comparison always true "
7113 "due to limited range of data type"));
7121 /* Visit conditional statement STMT. If we can determine which edge
7122 will be taken out of STMT's basic block, record it in
7123 *TAKEN_EDGE_P and return SSA_PROP_INTERESTING. Otherwise, return
7124 SSA_PROP_VARYING. */
7126 static enum ssa_prop_result
7127 vrp_visit_cond_stmt (gimple stmt
, edge
*taken_edge_p
)
7132 *taken_edge_p
= NULL
;
7134 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7139 fprintf (dump_file
, "\nVisiting conditional with predicate: ");
7140 print_gimple_stmt (dump_file
, stmt
, 0, 0);
7141 fprintf (dump_file
, "\nWith known ranges\n");
7143 FOR_EACH_SSA_TREE_OPERAND (use
, stmt
, i
, SSA_OP_USE
)
7145 fprintf (dump_file
, "\t");
7146 print_generic_expr (dump_file
, use
, 0);
7147 fprintf (dump_file
, ": ");
7148 dump_value_range (dump_file
, vr_value
[SSA_NAME_VERSION (use
)]);
7151 fprintf (dump_file
, "\n");
7154 /* Compute the value of the predicate COND by checking the known
7155 ranges of each of its operands.
7157 Note that we cannot evaluate all the equivalent ranges here
7158 because those ranges may not yet be final and with the current
7159 propagation strategy, we cannot determine when the value ranges
7160 of the names in the equivalence set have changed.
7162 For instance, given the following code fragment
7166 i_14 = ASSERT_EXPR <i_5, i_5 != 0>
7170 Assume that on the first visit to i_14, i_5 has the temporary
7171 range [8, 8] because the second argument to the PHI function is
7172 not yet executable. We derive the range ~[0, 0] for i_14 and the
7173 equivalence set { i_5 }. So, when we visit 'if (i_14 == 1)' for
7174 the first time, since i_14 is equivalent to the range [8, 8], we
7175 determine that the predicate is always false.
7177 On the next round of propagation, i_13 is determined to be
7178 VARYING, which causes i_5 to drop down to VARYING. So, another
7179 visit to i_14 is scheduled. In this second visit, we compute the
7180 exact same range and equivalence set for i_14, namely ~[0, 0] and
7181 { i_5 }. But we did not have the previous range for i_5
7182 registered, so vrp_visit_assignment thinks that the range for
7183 i_14 has not changed. Therefore, the predicate 'if (i_14 == 1)'
7184 is not visited again, which stops propagation from visiting
7185 statements in the THEN clause of that if().
7187 To properly fix this we would need to keep the previous range
7188 value for the names in the equivalence set. This way we would've
7189 discovered that from one visit to the other i_5 changed from
7190 range [8, 8] to VR_VARYING.
7192 However, fixing this apparent limitation may not be worth the
7193 additional checking. Testing on several code bases (GCC, DLV,
7194 MICO, TRAMP3D and SPEC2000) showed that doing this results in
7195 4 more predicates folded in SPEC. */
7198 val
= vrp_evaluate_conditional_warnv_with_ops (gimple_cond_code (stmt
),
7199 gimple_cond_lhs (stmt
),
7200 gimple_cond_rhs (stmt
),
7205 *taken_edge_p
= find_taken_edge (gimple_bb (stmt
), val
);
7208 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7210 "\nIgnoring predicate evaluation because "
7211 "it assumes that signed overflow is undefined");
7216 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7218 fprintf (dump_file
, "\nPredicate evaluates to: ");
7219 if (val
== NULL_TREE
)
7220 fprintf (dump_file
, "DON'T KNOW\n");
7222 print_generic_stmt (dump_file
, val
, 0);
7225 return (*taken_edge_p
) ? SSA_PROP_INTERESTING
: SSA_PROP_VARYING
;
7228 /* Searches the case label vector VEC for the index *IDX of the CASE_LABEL
7229 that includes the value VAL. The search is restricted to the range
7230 [START_IDX, n - 1] where n is the size of VEC.
7232 If there is a CASE_LABEL for VAL, its index is placed in IDX and true is
7235 If there is no CASE_LABEL for VAL and there is one that is larger than VAL,
7236 it is placed in IDX and false is returned.
7238 If VAL is larger than any CASE_LABEL, n is placed on IDX and false is
7242 find_case_label_index (gimple stmt
, size_t start_idx
, tree val
, size_t *idx
)
7244 size_t n
= gimple_switch_num_labels (stmt
);
7247 /* Find case label for minimum of the value range or the next one.
7248 At each iteration we are searching in [low, high - 1]. */
7250 for (low
= start_idx
, high
= n
; high
!= low
; )
7254 /* Note that i != high, so we never ask for n. */
7255 size_t i
= (high
+ low
) / 2;
7256 t
= gimple_switch_label (stmt
, i
);
7258 /* Cache the result of comparing CASE_LOW and val. */
7259 cmp
= tree_int_cst_compare (CASE_LOW (t
), val
);
7263 /* Ranges cannot be empty. */
7272 if (CASE_HIGH (t
) != NULL
7273 && tree_int_cst_compare (CASE_HIGH (t
), val
) >= 0)
7285 /* Searches the case label vector VEC for the range of CASE_LABELs that is used
7286 for values between MIN and MAX. The first index is placed in MIN_IDX. The
7287 last index is placed in MAX_IDX. If the range of CASE_LABELs is empty
7288 then MAX_IDX < MIN_IDX.
7289 Returns true if the default label is not needed. */
7292 find_case_label_range (gimple stmt
, tree min
, tree max
, size_t *min_idx
,
7296 bool min_take_default
= !find_case_label_index (stmt
, 1, min
, &i
);
7297 bool max_take_default
= !find_case_label_index (stmt
, i
, max
, &j
);
7301 && max_take_default
)
7303 /* Only the default case label reached.
7304 Return an empty range. */
7311 bool take_default
= min_take_default
|| max_take_default
;
7315 if (max_take_default
)
7318 /* If the case label range is continuous, we do not need
7319 the default case label. Verify that. */
7320 high
= CASE_LOW (gimple_switch_label (stmt
, i
));
7321 if (CASE_HIGH (gimple_switch_label (stmt
, i
)))
7322 high
= CASE_HIGH (gimple_switch_label (stmt
, i
));
7323 for (k
= i
+ 1; k
<= j
; ++k
)
7325 low
= CASE_LOW (gimple_switch_label (stmt
, k
));
7326 if (!integer_onep (int_const_binop (MINUS_EXPR
, low
, high
)))
7328 take_default
= true;
7332 if (CASE_HIGH (gimple_switch_label (stmt
, k
)))
7333 high
= CASE_HIGH (gimple_switch_label (stmt
, k
));
7338 return !take_default
;
7342 /* Searches the case label vector VEC for the ranges of CASE_LABELs that are
7343 used in range VR. The indices are placed in MIN_IDX1, MAX_IDX, MIN_IDX2 and
7344 MAX_IDX2. If the ranges of CASE_LABELs are empty then MAX_IDX1 < MIN_IDX1.
7345 Returns true if the default label is not needed. */
7348 find_case_label_ranges (gimple stmt
, value_range_t
*vr
, size_t *min_idx1
,
7349 size_t *max_idx1
, size_t *min_idx2
,
7353 unsigned int n
= gimple_switch_num_labels (stmt
);
7355 tree case_low
, case_high
;
7356 tree min
= vr
->min
, max
= vr
->max
;
7358 gcc_checking_assert (vr
->type
== VR_RANGE
|| vr
->type
== VR_ANTI_RANGE
);
7360 take_default
= !find_case_label_range (stmt
, min
, max
, &i
, &j
);
7362 /* Set second range to emtpy. */
7366 if (vr
->type
== VR_RANGE
)
7370 return !take_default
;
7373 /* Set first range to all case labels. */
7380 /* Make sure all the values of case labels [i , j] are contained in
7381 range [MIN, MAX]. */
7382 case_low
= CASE_LOW (gimple_switch_label (stmt
, i
));
7383 case_high
= CASE_HIGH (gimple_switch_label (stmt
, j
));
7384 if (tree_int_cst_compare (case_low
, min
) < 0)
7386 if (case_high
!= NULL_TREE
7387 && tree_int_cst_compare (max
, case_high
) < 0)
7393 /* If the range spans case labels [i, j], the corresponding anti-range spans
7394 the labels [1, i - 1] and [j + 1, n - 1]. */
7420 /* Visit switch statement STMT. If we can determine which edge
7421 will be taken out of STMT's basic block, record it in
7422 *TAKEN_EDGE_P and return SSA_PROP_INTERESTING. Otherwise, return
7423 SSA_PROP_VARYING. */
7425 static enum ssa_prop_result
7426 vrp_visit_switch_stmt (gimple stmt
, edge
*taken_edge_p
)
7430 size_t i
= 0, j
= 0, k
, l
;
7433 *taken_edge_p
= NULL
;
7434 op
= gimple_switch_index (stmt
);
7435 if (TREE_CODE (op
) != SSA_NAME
)
7436 return SSA_PROP_VARYING
;
7438 vr
= get_value_range (op
);
7439 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7441 fprintf (dump_file
, "\nVisiting switch expression with operand ");
7442 print_generic_expr (dump_file
, op
, 0);
7443 fprintf (dump_file
, " with known range ");
7444 dump_value_range (dump_file
, vr
);
7445 fprintf (dump_file
, "\n");
7448 if ((vr
->type
!= VR_RANGE
7449 && vr
->type
!= VR_ANTI_RANGE
)
7450 || symbolic_range_p (vr
))
7451 return SSA_PROP_VARYING
;
7453 /* Find the single edge that is taken from the switch expression. */
7454 take_default
= !find_case_label_ranges (stmt
, vr
, &i
, &j
, &k
, &l
);
7456 /* Check if the range spans no CASE_LABEL. If so, we only reach the default
7460 gcc_assert (take_default
);
7461 val
= gimple_switch_default_label (stmt
);
7465 /* Check if labels with index i to j and maybe the default label
7466 are all reaching the same label. */
7468 val
= gimple_switch_label (stmt
, i
);
7470 && CASE_LABEL (gimple_switch_default_label (stmt
))
7471 != CASE_LABEL (val
))
7473 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7474 fprintf (dump_file
, " not a single destination for this "
7476 return SSA_PROP_VARYING
;
7478 for (++i
; i
<= j
; ++i
)
7480 if (CASE_LABEL (gimple_switch_label (stmt
, i
)) != CASE_LABEL (val
))
7482 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7483 fprintf (dump_file
, " not a single destination for this "
7485 return SSA_PROP_VARYING
;
7490 if (CASE_LABEL (gimple_switch_label (stmt
, k
)) != CASE_LABEL (val
))
7492 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7493 fprintf (dump_file
, " not a single destination for this "
7495 return SSA_PROP_VARYING
;
7500 *taken_edge_p
= find_edge (gimple_bb (stmt
),
7501 label_to_block (CASE_LABEL (val
)));
7503 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7505 fprintf (dump_file
, " will take edge to ");
7506 print_generic_stmt (dump_file
, CASE_LABEL (val
), 0);
7509 return SSA_PROP_INTERESTING
;
7513 /* Evaluate statement STMT. If the statement produces a useful range,
7514 return SSA_PROP_INTERESTING and record the SSA name with the
7515 interesting range into *OUTPUT_P.
7517 If STMT is a conditional branch and we can determine its truth
7518 value, the taken edge is recorded in *TAKEN_EDGE_P.
7520 If STMT produces a varying value, return SSA_PROP_VARYING. */
7522 static enum ssa_prop_result
7523 vrp_visit_stmt (gimple stmt
, edge
*taken_edge_p
, tree
*output_p
)
7528 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7530 fprintf (dump_file
, "\nVisiting statement:\n");
7531 print_gimple_stmt (dump_file
, stmt
, 0, dump_flags
);
7532 fprintf (dump_file
, "\n");
7535 if (!stmt_interesting_for_vrp (stmt
))
7536 gcc_assert (stmt_ends_bb_p (stmt
));
7537 else if (is_gimple_assign (stmt
) || is_gimple_call (stmt
))
7538 return vrp_visit_assignment_or_call (stmt
, output_p
);
7539 else if (gimple_code (stmt
) == GIMPLE_COND
)
7540 return vrp_visit_cond_stmt (stmt
, taken_edge_p
);
7541 else if (gimple_code (stmt
) == GIMPLE_SWITCH
)
7542 return vrp_visit_switch_stmt (stmt
, taken_edge_p
);
7544 /* All other statements produce nothing of interest for VRP, so mark
7545 their outputs varying and prevent further simulation. */
7546 FOR_EACH_SSA_TREE_OPERAND (def
, stmt
, iter
, SSA_OP_DEF
)
7547 set_value_range_to_varying (get_value_range (def
));
7549 return SSA_PROP_VARYING
;
7552 /* Union the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
7553 { VR1TYPE, VR0MIN, VR0MAX } and store the result
7554 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
7555 possible such range. The resulting range is not canonicalized. */
7558 union_ranges (enum value_range_type
*vr0type
,
7559 tree
*vr0min
, tree
*vr0max
,
7560 enum value_range_type vr1type
,
7561 tree vr1min
, tree vr1max
)
7563 bool mineq
= operand_equal_p (*vr0min
, vr1min
, 0);
7564 bool maxeq
= operand_equal_p (*vr0max
, vr1max
, 0);
7566 /* [] is vr0, () is vr1 in the following classification comments. */
7570 if (*vr0type
== vr1type
)
7571 /* Nothing to do for equal ranges. */
7573 else if ((*vr0type
== VR_RANGE
7574 && vr1type
== VR_ANTI_RANGE
)
7575 || (*vr0type
== VR_ANTI_RANGE
7576 && vr1type
== VR_RANGE
))
7578 /* For anti-range with range union the result is varying. */
7584 else if (operand_less_p (*vr0max
, vr1min
) == 1
7585 || operand_less_p (vr1max
, *vr0min
) == 1)
7587 /* [ ] ( ) or ( ) [ ]
7588 If the ranges have an empty intersection, result of the union
7589 operation is the anti-range or if both are anti-ranges
7591 if (*vr0type
== VR_ANTI_RANGE
7592 && vr1type
== VR_ANTI_RANGE
)
7594 else if (*vr0type
== VR_ANTI_RANGE
7595 && vr1type
== VR_RANGE
)
7597 else if (*vr0type
== VR_RANGE
7598 && vr1type
== VR_ANTI_RANGE
)
7604 else if (*vr0type
== VR_RANGE
7605 && vr1type
== VR_RANGE
)
7607 /* The result is the convex hull of both ranges. */
7608 if (operand_less_p (*vr0max
, vr1min
) == 1)
7610 /* If the result can be an anti-range, create one. */
7611 if (TREE_CODE (*vr0max
) == INTEGER_CST
7612 && TREE_CODE (vr1min
) == INTEGER_CST
7613 && vrp_val_is_min (*vr0min
)
7614 && vrp_val_is_max (vr1max
))
7616 tree min
= int_const_binop (PLUS_EXPR
,
7618 build_int_cst (TREE_TYPE (*vr0max
), 1));
7619 tree max
= int_const_binop (MINUS_EXPR
,
7621 build_int_cst (TREE_TYPE (vr1min
), 1));
7622 if (!operand_less_p (max
, min
))
7624 *vr0type
= VR_ANTI_RANGE
;
7636 /* If the result can be an anti-range, create one. */
7637 if (TREE_CODE (vr1max
) == INTEGER_CST
7638 && TREE_CODE (*vr0min
) == INTEGER_CST
7639 && vrp_val_is_min (vr1min
)
7640 && vrp_val_is_max (*vr0max
))
7642 tree min
= int_const_binop (PLUS_EXPR
,
7644 build_int_cst (TREE_TYPE (vr1max
), 1));
7645 tree max
= int_const_binop (MINUS_EXPR
,
7647 build_int_cst (TREE_TYPE (*vr0min
), 1));
7648 if (!operand_less_p (max
, min
))
7650 *vr0type
= VR_ANTI_RANGE
;
7664 else if ((maxeq
|| operand_less_p (vr1max
, *vr0max
) == 1)
7665 && (mineq
|| operand_less_p (*vr0min
, vr1min
) == 1))
7667 /* [ ( ) ] or [( ) ] or [ ( )] */
7668 if (*vr0type
== VR_RANGE
7669 && vr1type
== VR_RANGE
)
7671 else if (*vr0type
== VR_ANTI_RANGE
7672 && vr1type
== VR_ANTI_RANGE
)
7678 else if (*vr0type
== VR_ANTI_RANGE
7679 && vr1type
== VR_RANGE
)
7681 /* Arbitrarily choose the right or left gap. */
7682 if (!mineq
&& TREE_CODE (vr1min
) == INTEGER_CST
)
7683 *vr0max
= int_const_binop (MINUS_EXPR
, vr1min
,
7684 build_int_cst (TREE_TYPE (vr1min
), 1));
7685 else if (!maxeq
&& TREE_CODE (vr1max
) == INTEGER_CST
)
7686 *vr0min
= int_const_binop (PLUS_EXPR
, vr1max
,
7687 build_int_cst (TREE_TYPE (vr1max
), 1));
7691 else if (*vr0type
== VR_RANGE
7692 && vr1type
== VR_ANTI_RANGE
)
7693 /* The result covers everything. */
7698 else if ((maxeq
|| operand_less_p (*vr0max
, vr1max
) == 1)
7699 && (mineq
|| operand_less_p (vr1min
, *vr0min
) == 1))
7701 /* ( [ ] ) or ([ ] ) or ( [ ]) */
7702 if (*vr0type
== VR_RANGE
7703 && vr1type
== VR_RANGE
)
7709 else if (*vr0type
== VR_ANTI_RANGE
7710 && vr1type
== VR_ANTI_RANGE
)
7712 else if (*vr0type
== VR_RANGE
7713 && vr1type
== VR_ANTI_RANGE
)
7715 *vr0type
= VR_ANTI_RANGE
;
7716 if (!mineq
&& TREE_CODE (*vr0min
) == INTEGER_CST
)
7718 *vr0max
= int_const_binop (MINUS_EXPR
, *vr0min
,
7719 build_int_cst (TREE_TYPE (*vr0min
), 1));
7722 else if (!maxeq
&& TREE_CODE (*vr0max
) == INTEGER_CST
)
7724 *vr0min
= int_const_binop (PLUS_EXPR
, *vr0max
,
7725 build_int_cst (TREE_TYPE (*vr0max
), 1));
7731 else if (*vr0type
== VR_ANTI_RANGE
7732 && vr1type
== VR_RANGE
)
7733 /* The result covers everything. */
7738 else if ((operand_less_p (vr1min
, *vr0max
) == 1
7739 || operand_equal_p (vr1min
, *vr0max
, 0))
7740 && operand_less_p (*vr0min
, vr1min
) == 1
7741 && operand_less_p (*vr0max
, vr1max
) == 1)
7743 /* [ ( ] ) or [ ]( ) */
7744 if (*vr0type
== VR_RANGE
7745 && vr1type
== VR_RANGE
)
7747 else if (*vr0type
== VR_ANTI_RANGE
7748 && vr1type
== VR_ANTI_RANGE
)
7750 else if (*vr0type
== VR_ANTI_RANGE
7751 && vr1type
== VR_RANGE
)
7753 if (TREE_CODE (vr1min
) == INTEGER_CST
)
7754 *vr0max
= int_const_binop (MINUS_EXPR
, vr1min
,
7755 build_int_cst (TREE_TYPE (vr1min
), 1));
7759 else if (*vr0type
== VR_RANGE
7760 && vr1type
== VR_ANTI_RANGE
)
7762 if (TREE_CODE (*vr0max
) == INTEGER_CST
)
7765 *vr0min
= int_const_binop (PLUS_EXPR
, *vr0max
,
7766 build_int_cst (TREE_TYPE (*vr0max
), 1));
7775 else if ((operand_less_p (*vr0min
, vr1max
) == 1
7776 || operand_equal_p (*vr0min
, vr1max
, 0))
7777 && operand_less_p (vr1min
, *vr0min
) == 1
7778 && operand_less_p (vr1max
, *vr0max
) == 1)
7780 /* ( [ ) ] or ( )[ ] */
7781 if (*vr0type
== VR_RANGE
7782 && vr1type
== VR_RANGE
)
7784 else if (*vr0type
== VR_ANTI_RANGE
7785 && vr1type
== VR_ANTI_RANGE
)
7787 else if (*vr0type
== VR_ANTI_RANGE
7788 && vr1type
== VR_RANGE
)
7790 if (TREE_CODE (vr1max
) == INTEGER_CST
)
7791 *vr0min
= int_const_binop (PLUS_EXPR
, vr1max
,
7792 build_int_cst (TREE_TYPE (vr1max
), 1));
7796 else if (*vr0type
== VR_RANGE
7797 && vr1type
== VR_ANTI_RANGE
)
7799 if (TREE_CODE (*vr0min
) == INTEGER_CST
)
7803 *vr0max
= int_const_binop (MINUS_EXPR
, *vr0min
,
7804 build_int_cst (TREE_TYPE (*vr0min
), 1));
7818 *vr0type
= VR_VARYING
;
7819 *vr0min
= NULL_TREE
;
7820 *vr0max
= NULL_TREE
;
7823 /* Intersect the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
7824 { VR1TYPE, VR0MIN, VR0MAX } and store the result
7825 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
7826 possible such range. The resulting range is not canonicalized. */
7829 intersect_ranges (enum value_range_type
*vr0type
,
7830 tree
*vr0min
, tree
*vr0max
,
7831 enum value_range_type vr1type
,
7832 tree vr1min
, tree vr1max
)
7834 bool mineq
= operand_equal_p (*vr0min
, vr1min
, 0);
7835 bool maxeq
= operand_equal_p (*vr0max
, vr1max
, 0);
7837 /* [] is vr0, () is vr1 in the following classification comments. */
7841 if (*vr0type
== vr1type
)
7842 /* Nothing to do for equal ranges. */
7844 else if ((*vr0type
== VR_RANGE
7845 && vr1type
== VR_ANTI_RANGE
)
7846 || (*vr0type
== VR_ANTI_RANGE
7847 && vr1type
== VR_RANGE
))
7849 /* For anti-range with range intersection the result is empty. */
7850 *vr0type
= VR_UNDEFINED
;
7851 *vr0min
= NULL_TREE
;
7852 *vr0max
= NULL_TREE
;
7857 else if (operand_less_p (*vr0max
, vr1min
) == 1
7858 || operand_less_p (vr1max
, *vr0min
) == 1)
7860 /* [ ] ( ) or ( ) [ ]
7861 If the ranges have an empty intersection, the result of the
7862 intersect operation is the range for intersecting an
7863 anti-range with a range or empty when intersecting two ranges. */
7864 if (*vr0type
== VR_RANGE
7865 && vr1type
== VR_ANTI_RANGE
)
7867 else if (*vr0type
== VR_ANTI_RANGE
7868 && vr1type
== VR_RANGE
)
7874 else if (*vr0type
== VR_RANGE
7875 && vr1type
== VR_RANGE
)
7877 *vr0type
= VR_UNDEFINED
;
7878 *vr0min
= NULL_TREE
;
7879 *vr0max
= NULL_TREE
;
7881 else if (*vr0type
== VR_ANTI_RANGE
7882 && vr1type
== VR_ANTI_RANGE
)
7884 /* If the anti-ranges are adjacent to each other merge them. */
7885 if (TREE_CODE (*vr0max
) == INTEGER_CST
7886 && TREE_CODE (vr1min
) == INTEGER_CST
7887 && operand_less_p (*vr0max
, vr1min
) == 1
7888 && integer_onep (int_const_binop (MINUS_EXPR
,
7891 else if (TREE_CODE (vr1max
) == INTEGER_CST
7892 && TREE_CODE (*vr0min
) == INTEGER_CST
7893 && operand_less_p (vr1max
, *vr0min
) == 1
7894 && integer_onep (int_const_binop (MINUS_EXPR
,
7897 /* Else arbitrarily take VR0. */
7900 else if ((maxeq
|| operand_less_p (vr1max
, *vr0max
) == 1)
7901 && (mineq
|| operand_less_p (*vr0min
, vr1min
) == 1))
7903 /* [ ( ) ] or [( ) ] or [ ( )] */
7904 if (*vr0type
== VR_RANGE
7905 && vr1type
== VR_RANGE
)
7907 /* If both are ranges the result is the inner one. */
7912 else if (*vr0type
== VR_RANGE
7913 && vr1type
== VR_ANTI_RANGE
)
7915 /* Choose the right gap if the left one is empty. */
7918 if (TREE_CODE (vr1max
) == INTEGER_CST
)
7919 *vr0min
= int_const_binop (PLUS_EXPR
, vr1max
,
7920 build_int_cst (TREE_TYPE (vr1max
), 1));
7924 /* Choose the left gap if the right one is empty. */
7927 if (TREE_CODE (vr1min
) == INTEGER_CST
)
7928 *vr0max
= int_const_binop (MINUS_EXPR
, vr1min
,
7929 build_int_cst (TREE_TYPE (vr1min
), 1));
7933 /* Choose the anti-range if the range is effectively varying. */
7934 else if (vrp_val_is_min (*vr0min
)
7935 && vrp_val_is_max (*vr0max
))
7941 /* Else choose the range. */
7943 else if (*vr0type
== VR_ANTI_RANGE
7944 && vr1type
== VR_ANTI_RANGE
)
7945 /* If both are anti-ranges the result is the outer one. */
7947 else if (*vr0type
== VR_ANTI_RANGE
7948 && vr1type
== VR_RANGE
)
7950 /* The intersection is empty. */
7951 *vr0type
= VR_UNDEFINED
;
7952 *vr0min
= NULL_TREE
;
7953 *vr0max
= NULL_TREE
;
7958 else if ((maxeq
|| operand_less_p (*vr0max
, vr1max
) == 1)
7959 && (mineq
|| operand_less_p (vr1min
, *vr0min
) == 1))
7961 /* ( [ ] ) or ([ ] ) or ( [ ]) */
7962 if (*vr0type
== VR_RANGE
7963 && vr1type
== VR_RANGE
)
7964 /* Choose the inner range. */
7966 else if (*vr0type
== VR_ANTI_RANGE
7967 && vr1type
== VR_RANGE
)
7969 /* Choose the right gap if the left is empty. */
7972 *vr0type
= VR_RANGE
;
7973 if (TREE_CODE (*vr0max
) == INTEGER_CST
)
7974 *vr0min
= int_const_binop (PLUS_EXPR
, *vr0max
,
7975 build_int_cst (TREE_TYPE (*vr0max
), 1));
7980 /* Choose the left gap if the right is empty. */
7983 *vr0type
= VR_RANGE
;
7984 if (TREE_CODE (*vr0min
) == INTEGER_CST
)
7985 *vr0max
= int_const_binop (MINUS_EXPR
, *vr0min
,
7986 build_int_cst (TREE_TYPE (*vr0min
), 1));
7991 /* Choose the anti-range if the range is effectively varying. */
7992 else if (vrp_val_is_min (vr1min
)
7993 && vrp_val_is_max (vr1max
))
7995 /* Else choose the range. */
8003 else if (*vr0type
== VR_ANTI_RANGE
8004 && vr1type
== VR_ANTI_RANGE
)
8006 /* If both are anti-ranges the result is the outer one. */
8011 else if (vr1type
== VR_ANTI_RANGE
8012 && *vr0type
== VR_RANGE
)
8014 /* The intersection is empty. */
8015 *vr0type
= VR_UNDEFINED
;
8016 *vr0min
= NULL_TREE
;
8017 *vr0max
= NULL_TREE
;
8022 else if ((operand_less_p (vr1min
, *vr0max
) == 1
8023 || operand_equal_p (vr1min
, *vr0max
, 0))
8024 && operand_less_p (*vr0min
, vr1min
) == 1)
8026 /* [ ( ] ) or [ ]( ) */
8027 if (*vr0type
== VR_ANTI_RANGE
8028 && vr1type
== VR_ANTI_RANGE
)
8030 else if (*vr0type
== VR_RANGE
8031 && vr1type
== VR_RANGE
)
8033 else if (*vr0type
== VR_RANGE
8034 && vr1type
== VR_ANTI_RANGE
)
8036 if (TREE_CODE (vr1min
) == INTEGER_CST
)
8037 *vr0max
= int_const_binop (MINUS_EXPR
, vr1min
,
8038 build_int_cst (TREE_TYPE (vr1min
), 1));
8042 else if (*vr0type
== VR_ANTI_RANGE
8043 && vr1type
== VR_RANGE
)
8045 *vr0type
= VR_RANGE
;
8046 if (TREE_CODE (*vr0max
) == INTEGER_CST
)
8047 *vr0min
= int_const_binop (PLUS_EXPR
, *vr0max
,
8048 build_int_cst (TREE_TYPE (*vr0max
), 1));
8056 else if ((operand_less_p (*vr0min
, vr1max
) == 1
8057 || operand_equal_p (*vr0min
, vr1max
, 0))
8058 && operand_less_p (vr1min
, *vr0min
) == 1)
8060 /* ( [ ) ] or ( )[ ] */
8061 if (*vr0type
== VR_ANTI_RANGE
8062 && vr1type
== VR_ANTI_RANGE
)
8064 else if (*vr0type
== VR_RANGE
8065 && vr1type
== VR_RANGE
)
8067 else if (*vr0type
== VR_RANGE
8068 && vr1type
== VR_ANTI_RANGE
)
8070 if (TREE_CODE (vr1max
) == INTEGER_CST
)
8071 *vr0min
= int_const_binop (PLUS_EXPR
, vr1max
,
8072 build_int_cst (TREE_TYPE (vr1max
), 1));
8076 else if (*vr0type
== VR_ANTI_RANGE
8077 && vr1type
== VR_RANGE
)
8079 *vr0type
= VR_RANGE
;
8080 if (TREE_CODE (*vr0min
) == INTEGER_CST
)
8081 *vr0max
= int_const_binop (MINUS_EXPR
, *vr0min
,
8082 build_int_cst (TREE_TYPE (*vr0min
), 1));
8091 /* As a fallback simply use { *VRTYPE, *VR0MIN, *VR0MAX } as
8092 result for the intersection. That's always a conservative
8093 correct estimate. */
8099 /* Intersect the two value-ranges *VR0 and *VR1 and store the result
8100 in *VR0. This may not be the smallest possible such range. */
8103 vrp_intersect_ranges_1 (value_range_t
*vr0
, value_range_t
*vr1
)
8105 value_range_t saved
;
8107 /* If either range is VR_VARYING the other one wins. */
8108 if (vr1
->type
== VR_VARYING
)
8110 if (vr0
->type
== VR_VARYING
)
8112 copy_value_range (vr0
, vr1
);
8116 /* When either range is VR_UNDEFINED the resulting range is
8117 VR_UNDEFINED, too. */
8118 if (vr0
->type
== VR_UNDEFINED
)
8120 if (vr1
->type
== VR_UNDEFINED
)
8122 set_value_range_to_undefined (vr0
);
8126 /* Save the original vr0 so we can return it as conservative intersection
8127 result when our worker turns things to varying. */
8129 intersect_ranges (&vr0
->type
, &vr0
->min
, &vr0
->max
,
8130 vr1
->type
, vr1
->min
, vr1
->max
);
8131 /* Make sure to canonicalize the result though as the inversion of a
8132 VR_RANGE can still be a VR_RANGE. */
8133 set_and_canonicalize_value_range (vr0
, vr0
->type
,
8134 vr0
->min
, vr0
->max
, vr0
->equiv
);
8135 /* If that failed, use the saved original VR0. */
8136 if (vr0
->type
== VR_VARYING
)
8141 /* If the result is VR_UNDEFINED there is no need to mess with
8142 the equivalencies. */
8143 if (vr0
->type
== VR_UNDEFINED
)
8146 /* The resulting set of equivalences for range intersection is the union of
8148 if (vr0
->equiv
&& vr1
->equiv
&& vr0
->equiv
!= vr1
->equiv
)
8149 bitmap_ior_into (vr0
->equiv
, vr1
->equiv
);
8150 else if (vr1
->equiv
&& !vr0
->equiv
)
8151 bitmap_copy (vr0
->equiv
, vr1
->equiv
);
8155 vrp_intersect_ranges (value_range_t
*vr0
, value_range_t
*vr1
)
8157 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8159 fprintf (dump_file
, "Intersecting\n ");
8160 dump_value_range (dump_file
, vr0
);
8161 fprintf (dump_file
, "\nand\n ");
8162 dump_value_range (dump_file
, vr1
);
8163 fprintf (dump_file
, "\n");
8165 vrp_intersect_ranges_1 (vr0
, vr1
);
8166 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8168 fprintf (dump_file
, "to\n ");
8169 dump_value_range (dump_file
, vr0
);
8170 fprintf (dump_file
, "\n");
8174 /* Meet operation for value ranges. Given two value ranges VR0 and
8175 VR1, store in VR0 a range that contains both VR0 and VR1. This
8176 may not be the smallest possible such range. */
8179 vrp_meet_1 (value_range_t
*vr0
, value_range_t
*vr1
)
8181 value_range_t saved
;
8183 if (vr0
->type
== VR_UNDEFINED
)
8185 set_value_range (vr0
, vr1
->type
, vr1
->min
, vr1
->max
, vr1
->equiv
);
8189 if (vr1
->type
== VR_UNDEFINED
)
8191 /* VR0 already has the resulting range. */
8195 if (vr0
->type
== VR_VARYING
)
8197 /* Nothing to do. VR0 already has the resulting range. */
8201 if (vr1
->type
== VR_VARYING
)
8203 set_value_range_to_varying (vr0
);
8208 union_ranges (&vr0
->type
, &vr0
->min
, &vr0
->max
,
8209 vr1
->type
, vr1
->min
, vr1
->max
);
8210 if (vr0
->type
== VR_VARYING
)
8212 /* Failed to find an efficient meet. Before giving up and setting
8213 the result to VARYING, see if we can at least derive a useful
8214 anti-range. FIXME, all this nonsense about distinguishing
8215 anti-ranges from ranges is necessary because of the odd
8216 semantics of range_includes_zero_p and friends. */
8217 if (((saved
.type
== VR_RANGE
8218 && range_includes_zero_p (saved
.min
, saved
.max
) == 0)
8219 || (saved
.type
== VR_ANTI_RANGE
8220 && range_includes_zero_p (saved
.min
, saved
.max
) == 1))
8221 && ((vr1
->type
== VR_RANGE
8222 && range_includes_zero_p (vr1
->min
, vr1
->max
) == 0)
8223 || (vr1
->type
== VR_ANTI_RANGE
8224 && range_includes_zero_p (vr1
->min
, vr1
->max
) == 1)))
8226 set_value_range_to_nonnull (vr0
, TREE_TYPE (saved
.min
));
8228 /* Since this meet operation did not result from the meeting of
8229 two equivalent names, VR0 cannot have any equivalences. */
8231 bitmap_clear (vr0
->equiv
);
8235 set_value_range_to_varying (vr0
);
8238 set_and_canonicalize_value_range (vr0
, vr0
->type
, vr0
->min
, vr0
->max
,
8240 if (vr0
->type
== VR_VARYING
)
8243 /* The resulting set of equivalences is always the intersection of
8245 if (vr0
->equiv
&& vr1
->equiv
&& vr0
->equiv
!= vr1
->equiv
)
8246 bitmap_and_into (vr0
->equiv
, vr1
->equiv
);
8247 else if (vr0
->equiv
&& !vr1
->equiv
)
8248 bitmap_clear (vr0
->equiv
);
8252 vrp_meet (value_range_t
*vr0
, value_range_t
*vr1
)
8254 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8256 fprintf (dump_file
, "Meeting\n ");
8257 dump_value_range (dump_file
, vr0
);
8258 fprintf (dump_file
, "\nand\n ");
8259 dump_value_range (dump_file
, vr1
);
8260 fprintf (dump_file
, "\n");
8262 vrp_meet_1 (vr0
, vr1
);
8263 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8265 fprintf (dump_file
, "to\n ");
8266 dump_value_range (dump_file
, vr0
);
8267 fprintf (dump_file
, "\n");
8272 /* Visit all arguments for PHI node PHI that flow through executable
8273 edges. If a valid value range can be derived from all the incoming
8274 value ranges, set a new range for the LHS of PHI. */
8276 static enum ssa_prop_result
8277 vrp_visit_phi_node (gimple phi
)
8280 tree lhs
= PHI_RESULT (phi
);
8281 value_range_t
*lhs_vr
= get_value_range (lhs
);
8282 value_range_t vr_result
= VR_INITIALIZER
;
8284 int edges
, old_edges
;
8287 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8289 fprintf (dump_file
, "\nVisiting PHI node: ");
8290 print_gimple_stmt (dump_file
, phi
, 0, dump_flags
);
8294 for (i
= 0; i
< gimple_phi_num_args (phi
); i
++)
8296 edge e
= gimple_phi_arg_edge (phi
, i
);
8298 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8301 "\n Argument #%d (%d -> %d %sexecutable)\n",
8302 (int) i
, e
->src
->index
, e
->dest
->index
,
8303 (e
->flags
& EDGE_EXECUTABLE
) ? "" : "not ");
8306 if (e
->flags
& EDGE_EXECUTABLE
)
8308 tree arg
= PHI_ARG_DEF (phi
, i
);
8309 value_range_t vr_arg
;
8313 if (TREE_CODE (arg
) == SSA_NAME
)
8315 vr_arg
= *(get_value_range (arg
));
8316 /* Do not allow equivalences or symbolic ranges to leak in from
8317 backedges. That creates invalid equivalencies.
8318 See PR53465 and PR54767. */
8319 if (e
->flags
& EDGE_DFS_BACK
8320 && (vr_arg
.type
== VR_RANGE
8321 || vr_arg
.type
== VR_ANTI_RANGE
))
8323 vr_arg
.equiv
= NULL
;
8324 if (symbolic_range_p (&vr_arg
))
8326 vr_arg
.type
= VR_VARYING
;
8327 vr_arg
.min
= NULL_TREE
;
8328 vr_arg
.max
= NULL_TREE
;
8334 if (TREE_OVERFLOW_P (arg
))
8335 arg
= drop_tree_overflow (arg
);
8337 vr_arg
.type
= VR_RANGE
;
8340 vr_arg
.equiv
= NULL
;
8343 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8345 fprintf (dump_file
, "\t");
8346 print_generic_expr (dump_file
, arg
, dump_flags
);
8347 fprintf (dump_file
, "\n\tValue: ");
8348 dump_value_range (dump_file
, &vr_arg
);
8349 fprintf (dump_file
, "\n");
8353 copy_value_range (&vr_result
, &vr_arg
);
8355 vrp_meet (&vr_result
, &vr_arg
);
8358 if (vr_result
.type
== VR_VARYING
)
8363 if (vr_result
.type
== VR_VARYING
)
8365 else if (vr_result
.type
== VR_UNDEFINED
)
8368 old_edges
= vr_phi_edge_counts
[SSA_NAME_VERSION (lhs
)];
8369 vr_phi_edge_counts
[SSA_NAME_VERSION (lhs
)] = edges
;
8371 /* To prevent infinite iterations in the algorithm, derive ranges
8372 when the new value is slightly bigger or smaller than the
8373 previous one. We don't do this if we have seen a new executable
8374 edge; this helps us avoid an overflow infinity for conditionals
8375 which are not in a loop. If the old value-range was VR_UNDEFINED
8376 use the updated range and iterate one more time. */
8378 && gimple_phi_num_args (phi
) > 1
8379 && edges
== old_edges
8380 && lhs_vr
->type
!= VR_UNDEFINED
)
8382 int cmp_min
= compare_values (lhs_vr
->min
, vr_result
.min
);
8383 int cmp_max
= compare_values (lhs_vr
->max
, vr_result
.max
);
8385 /* For non VR_RANGE or for pointers fall back to varying if
8386 the range changed. */
8387 if ((lhs_vr
->type
!= VR_RANGE
|| vr_result
.type
!= VR_RANGE
8388 || POINTER_TYPE_P (TREE_TYPE (lhs
)))
8389 && (cmp_min
!= 0 || cmp_max
!= 0))
8392 /* If the new minimum is smaller or larger than the previous
8393 one, go all the way to -INF. In the first case, to avoid
8394 iterating millions of times to reach -INF, and in the
8395 other case to avoid infinite bouncing between different
8397 if (cmp_min
> 0 || cmp_min
< 0)
8399 if (!needs_overflow_infinity (TREE_TYPE (vr_result
.min
))
8400 || !vrp_var_may_overflow (lhs
, phi
))
8401 vr_result
.min
= TYPE_MIN_VALUE (TREE_TYPE (vr_result
.min
));
8402 else if (supports_overflow_infinity (TREE_TYPE (vr_result
.min
)))
8404 negative_overflow_infinity (TREE_TYPE (vr_result
.min
));
8407 /* Similarly, if the new maximum is smaller or larger than
8408 the previous one, go all the way to +INF. */
8409 if (cmp_max
< 0 || cmp_max
> 0)
8411 if (!needs_overflow_infinity (TREE_TYPE (vr_result
.max
))
8412 || !vrp_var_may_overflow (lhs
, phi
))
8413 vr_result
.max
= TYPE_MAX_VALUE (TREE_TYPE (vr_result
.max
));
8414 else if (supports_overflow_infinity (TREE_TYPE (vr_result
.max
)))
8416 positive_overflow_infinity (TREE_TYPE (vr_result
.max
));
8419 /* If we dropped either bound to +-INF then if this is a loop
8420 PHI node SCEV may known more about its value-range. */
8421 if ((cmp_min
> 0 || cmp_min
< 0
8422 || cmp_max
< 0 || cmp_max
> 0)
8424 && (l
= loop_containing_stmt (phi
))
8425 && l
->header
== gimple_bb (phi
))
8426 adjust_range_with_scev (&vr_result
, l
, phi
, lhs
);
8428 /* If we will end up with a (-INF, +INF) range, set it to
8429 VARYING. Same if the previous max value was invalid for
8430 the type and we end up with vr_result.min > vr_result.max. */
8431 if ((vrp_val_is_max (vr_result
.max
)
8432 && vrp_val_is_min (vr_result
.min
))
8433 || compare_values (vr_result
.min
,
8438 /* If the new range is different than the previous value, keep
8441 if (update_value_range (lhs
, &vr_result
))
8443 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8445 fprintf (dump_file
, "Found new range for ");
8446 print_generic_expr (dump_file
, lhs
, 0);
8447 fprintf (dump_file
, ": ");
8448 dump_value_range (dump_file
, &vr_result
);
8449 fprintf (dump_file
, "\n\n");
8452 return SSA_PROP_INTERESTING
;
8455 /* Nothing changed, don't add outgoing edges. */
8456 return SSA_PROP_NOT_INTERESTING
;
8458 /* No match found. Set the LHS to VARYING. */
8460 set_value_range_to_varying (lhs_vr
);
8461 return SSA_PROP_VARYING
;
8464 /* Simplify boolean operations if the source is known
8465 to be already a boolean. */
8467 simplify_truth_ops_using_ranges (gimple_stmt_iterator
*gsi
, gimple stmt
)
8469 enum tree_code rhs_code
= gimple_assign_rhs_code (stmt
);
8471 bool need_conversion
;
8473 /* We handle only !=/== case here. */
8474 gcc_assert (rhs_code
== EQ_EXPR
|| rhs_code
== NE_EXPR
);
8476 op0
= gimple_assign_rhs1 (stmt
);
8477 if (!op_with_boolean_value_range_p (op0
))
8480 op1
= gimple_assign_rhs2 (stmt
);
8481 if (!op_with_boolean_value_range_p (op1
))
8484 /* Reduce number of cases to handle to NE_EXPR. As there is no
8485 BIT_XNOR_EXPR we cannot replace A == B with a single statement. */
8486 if (rhs_code
== EQ_EXPR
)
8488 if (TREE_CODE (op1
) == INTEGER_CST
)
8489 op1
= int_const_binop (BIT_XOR_EXPR
, op1
,
8490 build_int_cst (TREE_TYPE (op1
), 1));
8495 lhs
= gimple_assign_lhs (stmt
);
8497 = !useless_type_conversion_p (TREE_TYPE (lhs
), TREE_TYPE (op0
));
8499 /* Make sure to not sign-extend a 1-bit 1 when converting the result. */
8501 && !TYPE_UNSIGNED (TREE_TYPE (op0
))
8502 && TYPE_PRECISION (TREE_TYPE (op0
)) == 1
8503 && TYPE_PRECISION (TREE_TYPE (lhs
)) > 1)
8506 /* For A != 0 we can substitute A itself. */
8507 if (integer_zerop (op1
))
8508 gimple_assign_set_rhs_with_ops (gsi
,
8510 ? NOP_EXPR
: TREE_CODE (op0
),
8512 /* For A != B we substitute A ^ B. Either with conversion. */
8513 else if (need_conversion
)
8515 tree tem
= make_ssa_name (TREE_TYPE (op0
), NULL
);
8516 gimple newop
= gimple_build_assign_with_ops (BIT_XOR_EXPR
, tem
, op0
, op1
);
8517 gsi_insert_before (gsi
, newop
, GSI_SAME_STMT
);
8518 gimple_assign_set_rhs_with_ops (gsi
, NOP_EXPR
, tem
, NULL_TREE
);
8522 gimple_assign_set_rhs_with_ops (gsi
, BIT_XOR_EXPR
, op0
, op1
);
8523 update_stmt (gsi_stmt (*gsi
));
8528 /* Simplify a division or modulo operator to a right shift or
8529 bitwise and if the first operand is unsigned or is greater
8530 than zero and the second operand is an exact power of two. */
8533 simplify_div_or_mod_using_ranges (gimple stmt
)
8535 enum tree_code rhs_code
= gimple_assign_rhs_code (stmt
);
8537 tree op0
= gimple_assign_rhs1 (stmt
);
8538 tree op1
= gimple_assign_rhs2 (stmt
);
8539 value_range_t
*vr
= get_value_range (gimple_assign_rhs1 (stmt
));
8541 if (TYPE_UNSIGNED (TREE_TYPE (op0
)))
8543 val
= integer_one_node
;
8549 val
= compare_range_with_value (GE_EXPR
, vr
, integer_zero_node
, &sop
);
8553 && integer_onep (val
)
8554 && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC
))
8556 location_t location
;
8558 if (!gimple_has_location (stmt
))
8559 location
= input_location
;
8561 location
= gimple_location (stmt
);
8562 warning_at (location
, OPT_Wstrict_overflow
,
8563 "assuming signed overflow does not occur when "
8564 "simplifying %</%> or %<%%%> to %<>>%> or %<&%>");
8568 if (val
&& integer_onep (val
))
8572 if (rhs_code
== TRUNC_DIV_EXPR
)
8574 t
= build_int_cst (integer_type_node
, tree_log2 (op1
));
8575 gimple_assign_set_rhs_code (stmt
, RSHIFT_EXPR
);
8576 gimple_assign_set_rhs1 (stmt
, op0
);
8577 gimple_assign_set_rhs2 (stmt
, t
);
8581 t
= build_int_cst (TREE_TYPE (op1
), 1);
8582 t
= int_const_binop (MINUS_EXPR
, op1
, t
);
8583 t
= fold_convert (TREE_TYPE (op0
), t
);
8585 gimple_assign_set_rhs_code (stmt
, BIT_AND_EXPR
);
8586 gimple_assign_set_rhs1 (stmt
, op0
);
8587 gimple_assign_set_rhs2 (stmt
, t
);
8597 /* If the operand to an ABS_EXPR is >= 0, then eliminate the
8598 ABS_EXPR. If the operand is <= 0, then simplify the
8599 ABS_EXPR into a NEGATE_EXPR. */
8602 simplify_abs_using_ranges (gimple stmt
)
8605 tree op
= gimple_assign_rhs1 (stmt
);
8606 tree type
= TREE_TYPE (op
);
8607 value_range_t
*vr
= get_value_range (op
);
8609 if (TYPE_UNSIGNED (type
))
8611 val
= integer_zero_node
;
8617 val
= compare_range_with_value (LE_EXPR
, vr
, integer_zero_node
, &sop
);
8621 val
= compare_range_with_value (GE_EXPR
, vr
, integer_zero_node
,
8626 if (integer_zerop (val
))
8627 val
= integer_one_node
;
8628 else if (integer_onep (val
))
8629 val
= integer_zero_node
;
8634 && (integer_onep (val
) || integer_zerop (val
)))
8636 if (sop
&& issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC
))
8638 location_t location
;
8640 if (!gimple_has_location (stmt
))
8641 location
= input_location
;
8643 location
= gimple_location (stmt
);
8644 warning_at (location
, OPT_Wstrict_overflow
,
8645 "assuming signed overflow does not occur when "
8646 "simplifying %<abs (X)%> to %<X%> or %<-X%>");
8649 gimple_assign_set_rhs1 (stmt
, op
);
8650 if (integer_onep (val
))
8651 gimple_assign_set_rhs_code (stmt
, NEGATE_EXPR
);
8653 gimple_assign_set_rhs_code (stmt
, SSA_NAME
);
8662 /* Optimize away redundant BIT_AND_EXPR and BIT_IOR_EXPR.
8663 If all the bits that are being cleared by & are already
8664 known to be zero from VR, or all the bits that are being
8665 set by | are already known to be one from VR, the bit
8666 operation is redundant. */
8669 simplify_bit_ops_using_ranges (gimple_stmt_iterator
*gsi
, gimple stmt
)
8671 tree op0
= gimple_assign_rhs1 (stmt
);
8672 tree op1
= gimple_assign_rhs2 (stmt
);
8673 tree op
= NULL_TREE
;
8674 value_range_t vr0
= VR_INITIALIZER
;
8675 value_range_t vr1
= VR_INITIALIZER
;
8676 wide_int may_be_nonzero0
, may_be_nonzero1
;
8677 wide_int must_be_nonzero0
, must_be_nonzero1
;
8680 if (TREE_CODE (op0
) == SSA_NAME
)
8681 vr0
= *(get_value_range (op0
));
8682 else if (is_gimple_min_invariant (op0
))
8683 set_value_range_to_value (&vr0
, op0
, NULL
);
8687 if (TREE_CODE (op1
) == SSA_NAME
)
8688 vr1
= *(get_value_range (op1
));
8689 else if (is_gimple_min_invariant (op1
))
8690 set_value_range_to_value (&vr1
, op1
, NULL
);
8694 if (!zero_nonzero_bits_from_vr (TREE_TYPE (op0
), &vr0
, &may_be_nonzero0
,
8697 if (!zero_nonzero_bits_from_vr (TREE_TYPE (op1
), &vr1
, &may_be_nonzero1
,
8701 switch (gimple_assign_rhs_code (stmt
))
8704 mask
= may_be_nonzero0
.and_not (must_be_nonzero1
);
8710 mask
= may_be_nonzero1
.and_not (must_be_nonzero0
);
8718 mask
= may_be_nonzero0
.and_not (must_be_nonzero1
);
8724 mask
= may_be_nonzero1
.and_not (must_be_nonzero0
);
8735 if (op
== NULL_TREE
)
8738 gimple_assign_set_rhs_with_ops (gsi
, TREE_CODE (op
), op
, NULL
);
8739 update_stmt (gsi_stmt (*gsi
));
8743 /* We are comparing trees OP0 and OP1 using COND_CODE. OP0 has
8744 a known value range VR.
8746 If there is one and only one value which will satisfy the
8747 conditional, then return that value. Else return NULL. */
8750 test_for_singularity (enum tree_code cond_code
, tree op0
,
8751 tree op1
, value_range_t
*vr
)
8756 /* Extract minimum/maximum values which satisfy the
8757 the conditional as it was written. */
8758 if (cond_code
== LE_EXPR
|| cond_code
== LT_EXPR
)
8760 /* This should not be negative infinity; there is no overflow
8762 min
= TYPE_MIN_VALUE (TREE_TYPE (op0
));
8765 if (cond_code
== LT_EXPR
&& !is_overflow_infinity (max
))
8767 tree one
= build_int_cst (TREE_TYPE (op0
), 1);
8768 max
= fold_build2 (MINUS_EXPR
, TREE_TYPE (op0
), max
, one
);
8770 TREE_NO_WARNING (max
) = 1;
8773 else if (cond_code
== GE_EXPR
|| cond_code
== GT_EXPR
)
8775 /* This should not be positive infinity; there is no overflow
8777 max
= TYPE_MAX_VALUE (TREE_TYPE (op0
));
8780 if (cond_code
== GT_EXPR
&& !is_overflow_infinity (min
))
8782 tree one
= build_int_cst (TREE_TYPE (op0
), 1);
8783 min
= fold_build2 (PLUS_EXPR
, TREE_TYPE (op0
), min
, one
);
8785 TREE_NO_WARNING (min
) = 1;
8789 /* Now refine the minimum and maximum values using any
8790 value range information we have for op0. */
8793 if (compare_values (vr
->min
, min
) == 1)
8795 if (compare_values (vr
->max
, max
) == -1)
8798 /* If the new min/max values have converged to a single value,
8799 then there is only one value which can satisfy the condition,
8800 return that value. */
8801 if (operand_equal_p (min
, max
, 0) && is_gimple_min_invariant (min
))
8807 /* Return whether the value range *VR fits in an integer type specified
8808 by PRECISION and UNSIGNED_P. */
8811 range_fits_type_p (value_range_t
*vr
, unsigned dest_precision
, signop dest_sgn
)
8814 unsigned src_precision
;
8818 /* We can only handle integral and pointer types. */
8819 src_type
= TREE_TYPE (vr
->min
);
8820 if (!INTEGRAL_TYPE_P (src_type
)
8821 && !POINTER_TYPE_P (src_type
))
8824 /* An extension is fine unless VR is SIGNED and dest_sgn is UNSIGNED,
8825 and so is an identity transform. */
8826 src_precision
= TYPE_PRECISION (TREE_TYPE (vr
->min
));
8827 src_sgn
= TYPE_SIGN (src_type
);
8828 if ((src_precision
< dest_precision
8829 && !(dest_sgn
== UNSIGNED
&& src_sgn
== SIGNED
))
8830 || (src_precision
== dest_precision
&& src_sgn
== dest_sgn
))
8833 /* Now we can only handle ranges with constant bounds. */
8834 if (vr
->type
!= VR_RANGE
8835 || TREE_CODE (vr
->min
) != INTEGER_CST
8836 || TREE_CODE (vr
->max
) != INTEGER_CST
)
8839 /* For sign changes, the MSB of the wide_int has to be clear.
8840 An unsigned value with its MSB set cannot be represented by
8841 a signed wide_int, while a negative value cannot be represented
8842 by an unsigned wide_int. */
8843 if (src_sgn
!= dest_sgn
8844 && (wi::lts_p (vr
->min
, 0) || wi::lts_p (vr
->max
, 0)))
8847 /* Then we can perform the conversion on both ends and compare
8848 the result for equality. */
8849 tem
= wi::ext (wi::to_widest (vr
->min
), dest_precision
, dest_sgn
);
8850 if (tem
!= wi::to_widest (vr
->min
))
8852 tem
= wi::ext (wi::to_widest (vr
->max
), dest_precision
, dest_sgn
);
8853 if (tem
!= wi::to_widest (vr
->max
))
8859 /* Simplify a conditional using a relational operator to an equality
8860 test if the range information indicates only one value can satisfy
8861 the original conditional. */
8864 simplify_cond_using_ranges (gimple stmt
)
8866 tree op0
= gimple_cond_lhs (stmt
);
8867 tree op1
= gimple_cond_rhs (stmt
);
8868 enum tree_code cond_code
= gimple_cond_code (stmt
);
8870 if (cond_code
!= NE_EXPR
8871 && cond_code
!= EQ_EXPR
8872 && TREE_CODE (op0
) == SSA_NAME
8873 && INTEGRAL_TYPE_P (TREE_TYPE (op0
))
8874 && is_gimple_min_invariant (op1
))
8876 value_range_t
*vr
= get_value_range (op0
);
8878 /* If we have range information for OP0, then we might be
8879 able to simplify this conditional. */
8880 if (vr
->type
== VR_RANGE
)
8882 tree new_tree
= test_for_singularity (cond_code
, op0
, op1
, vr
);
8888 fprintf (dump_file
, "Simplified relational ");
8889 print_gimple_stmt (dump_file
, stmt
, 0, 0);
8890 fprintf (dump_file
, " into ");
8893 gimple_cond_set_code (stmt
, EQ_EXPR
);
8894 gimple_cond_set_lhs (stmt
, op0
);
8895 gimple_cond_set_rhs (stmt
, new_tree
);
8901 print_gimple_stmt (dump_file
, stmt
, 0, 0);
8902 fprintf (dump_file
, "\n");
8908 /* Try again after inverting the condition. We only deal
8909 with integral types here, so no need to worry about
8910 issues with inverting FP comparisons. */
8911 cond_code
= invert_tree_comparison (cond_code
, false);
8912 new_tree
= test_for_singularity (cond_code
, op0
, op1
, vr
);
8918 fprintf (dump_file
, "Simplified relational ");
8919 print_gimple_stmt (dump_file
, stmt
, 0, 0);
8920 fprintf (dump_file
, " into ");
8923 gimple_cond_set_code (stmt
, NE_EXPR
);
8924 gimple_cond_set_lhs (stmt
, op0
);
8925 gimple_cond_set_rhs (stmt
, new_tree
);
8931 print_gimple_stmt (dump_file
, stmt
, 0, 0);
8932 fprintf (dump_file
, "\n");
8940 /* If we have a comparison of an SSA_NAME (OP0) against a constant,
8941 see if OP0 was set by a type conversion where the source of
8942 the conversion is another SSA_NAME with a range that fits
8943 into the range of OP0's type.
8945 If so, the conversion is redundant as the earlier SSA_NAME can be
8946 used for the comparison directly if we just massage the constant in the
8948 if (TREE_CODE (op0
) == SSA_NAME
8949 && TREE_CODE (op1
) == INTEGER_CST
)
8951 gimple def_stmt
= SSA_NAME_DEF_STMT (op0
);
8954 if (!is_gimple_assign (def_stmt
)
8955 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt
)))
8958 innerop
= gimple_assign_rhs1 (def_stmt
);
8960 if (TREE_CODE (innerop
) == SSA_NAME
8961 && !POINTER_TYPE_P (TREE_TYPE (innerop
)))
8963 value_range_t
*vr
= get_value_range (innerop
);
8965 if (range_int_cst_p (vr
)
8966 && range_fits_type_p (vr
,
8967 TYPE_PRECISION (TREE_TYPE (op0
)),
8968 TYPE_SIGN (TREE_TYPE (op0
)))
8969 && int_fits_type_p (op1
, TREE_TYPE (innerop
))
8970 /* The range must not have overflowed, or if it did overflow
8971 we must not be wrapping/trapping overflow and optimizing
8972 with strict overflow semantics. */
8973 && ((!is_negative_overflow_infinity (vr
->min
)
8974 && !is_positive_overflow_infinity (vr
->max
))
8975 || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (innerop
))))
8977 /* If the range overflowed and the user has asked for warnings
8978 when strict overflow semantics were used to optimize code,
8979 issue an appropriate warning. */
8980 if ((is_negative_overflow_infinity (vr
->min
)
8981 || is_positive_overflow_infinity (vr
->max
))
8982 && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_CONDITIONAL
))
8984 location_t location
;
8986 if (!gimple_has_location (stmt
))
8987 location
= input_location
;
8989 location
= gimple_location (stmt
);
8990 warning_at (location
, OPT_Wstrict_overflow
,
8991 "assuming signed overflow does not occur when "
8992 "simplifying conditional");
8995 tree newconst
= fold_convert (TREE_TYPE (innerop
), op1
);
8996 gimple_cond_set_lhs (stmt
, innerop
);
8997 gimple_cond_set_rhs (stmt
, newconst
);
9006 /* Simplify a switch statement using the value range of the switch
9010 simplify_switch_using_ranges (gimple stmt
)
9012 tree op
= gimple_switch_index (stmt
);
9017 size_t i
= 0, j
= 0, n
, n2
;
9020 size_t k
= 1, l
= 0;
9022 if (TREE_CODE (op
) == SSA_NAME
)
9024 vr
= get_value_range (op
);
9026 /* We can only handle integer ranges. */
9027 if ((vr
->type
!= VR_RANGE
9028 && vr
->type
!= VR_ANTI_RANGE
)
9029 || symbolic_range_p (vr
))
9032 /* Find case label for min/max of the value range. */
9033 take_default
= !find_case_label_ranges (stmt
, vr
, &i
, &j
, &k
, &l
);
9035 else if (TREE_CODE (op
) == INTEGER_CST
)
9037 take_default
= !find_case_label_index (stmt
, 1, op
, &i
);
9051 n
= gimple_switch_num_labels (stmt
);
9053 /* Bail out if this is just all edges taken. */
9059 /* Build a new vector of taken case labels. */
9060 vec2
= make_tree_vec (j
- i
+ 1 + l
- k
+ 1 + (int)take_default
);
9063 /* Add the default edge, if necessary. */
9065 TREE_VEC_ELT (vec2
, n2
++) = gimple_switch_default_label (stmt
);
9067 for (; i
<= j
; ++i
, ++n2
)
9068 TREE_VEC_ELT (vec2
, n2
) = gimple_switch_label (stmt
, i
);
9070 for (; k
<= l
; ++k
, ++n2
)
9071 TREE_VEC_ELT (vec2
, n2
) = gimple_switch_label (stmt
, k
);
9073 /* Mark needed edges. */
9074 for (i
= 0; i
< n2
; ++i
)
9076 e
= find_edge (gimple_bb (stmt
),
9077 label_to_block (CASE_LABEL (TREE_VEC_ELT (vec2
, i
))));
9078 e
->aux
= (void *)-1;
9081 /* Queue not needed edges for later removal. */
9082 FOR_EACH_EDGE (e
, ei
, gimple_bb (stmt
)->succs
)
9084 if (e
->aux
== (void *)-1)
9090 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
9092 fprintf (dump_file
, "removing unreachable case label\n");
9094 to_remove_edges
.safe_push (e
);
9095 e
->flags
&= ~EDGE_EXECUTABLE
;
9098 /* And queue an update for the stmt. */
9101 to_update_switch_stmts
.safe_push (su
);
9105 /* Simplify an integral conversion from an SSA name in STMT. */
9108 simplify_conversion_using_ranges (gimple stmt
)
9110 tree innerop
, middleop
, finaltype
;
9112 value_range_t
*innervr
;
9113 signop inner_sgn
, middle_sgn
, final_sgn
;
9114 unsigned inner_prec
, middle_prec
, final_prec
;
9115 widest_int innermin
, innermed
, innermax
, middlemin
, middlemed
, middlemax
;
9117 finaltype
= TREE_TYPE (gimple_assign_lhs (stmt
));
9118 if (!INTEGRAL_TYPE_P (finaltype
))
9120 middleop
= gimple_assign_rhs1 (stmt
);
9121 def_stmt
= SSA_NAME_DEF_STMT (middleop
);
9122 if (!is_gimple_assign (def_stmt
)
9123 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt
)))
9125 innerop
= gimple_assign_rhs1 (def_stmt
);
9126 if (TREE_CODE (innerop
) != SSA_NAME
9127 || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (innerop
))
9130 /* Get the value-range of the inner operand. */
9131 innervr
= get_value_range (innerop
);
9132 if (innervr
->type
!= VR_RANGE
9133 || TREE_CODE (innervr
->min
) != INTEGER_CST
9134 || TREE_CODE (innervr
->max
) != INTEGER_CST
)
9137 /* Simulate the conversion chain to check if the result is equal if
9138 the middle conversion is removed. */
9139 innermin
= wi::to_widest (innervr
->min
);
9140 innermax
= wi::to_widest (innervr
->max
);
9142 inner_prec
= TYPE_PRECISION (TREE_TYPE (innerop
));
9143 middle_prec
= TYPE_PRECISION (TREE_TYPE (middleop
));
9144 final_prec
= TYPE_PRECISION (finaltype
);
9146 /* If the first conversion is not injective, the second must not
9148 if (wi::gtu_p (innermax
- innermin
,
9149 wi::mask
<widest_int
> (middle_prec
, false))
9150 && middle_prec
< final_prec
)
9152 /* We also want a medium value so that we can track the effect that
9153 narrowing conversions with sign change have. */
9154 inner_sgn
= TYPE_SIGN (TREE_TYPE (innerop
));
9155 if (inner_sgn
== UNSIGNED
)
9156 innermed
= wi::shifted_mask
<widest_int
> (1, inner_prec
- 1, false);
9159 if (wi::cmp (innermin
, innermed
, inner_sgn
) >= 0
9160 || wi::cmp (innermed
, innermax
, inner_sgn
) >= 0)
9161 innermed
= innermin
;
9163 middle_sgn
= TYPE_SIGN (TREE_TYPE (middleop
));
9164 middlemin
= wi::ext (innermin
, middle_prec
, middle_sgn
);
9165 middlemed
= wi::ext (innermed
, middle_prec
, middle_sgn
);
9166 middlemax
= wi::ext (innermax
, middle_prec
, middle_sgn
);
9168 /* Require that the final conversion applied to both the original
9169 and the intermediate range produces the same result. */
9170 final_sgn
= TYPE_SIGN (finaltype
);
9171 if (wi::ext (middlemin
, final_prec
, final_sgn
)
9172 != wi::ext (innermin
, final_prec
, final_sgn
)
9173 || wi::ext (middlemed
, final_prec
, final_sgn
)
9174 != wi::ext (innermed
, final_prec
, final_sgn
)
9175 || wi::ext (middlemax
, final_prec
, final_sgn
)
9176 != wi::ext (innermax
, final_prec
, final_sgn
))
9179 gimple_assign_set_rhs1 (stmt
, innerop
);
9184 /* Simplify a conversion from integral SSA name to float in STMT. */
9187 simplify_float_conversion_using_ranges (gimple_stmt_iterator
*gsi
, gimple stmt
)
9189 tree rhs1
= gimple_assign_rhs1 (stmt
);
9190 value_range_t
*vr
= get_value_range (rhs1
);
9191 enum machine_mode fltmode
= TYPE_MODE (TREE_TYPE (gimple_assign_lhs (stmt
)));
9192 enum machine_mode mode
;
9196 /* We can only handle constant ranges. */
9197 if (vr
->type
!= VR_RANGE
9198 || TREE_CODE (vr
->min
) != INTEGER_CST
9199 || TREE_CODE (vr
->max
) != INTEGER_CST
)
9202 /* First check if we can use a signed type in place of an unsigned. */
9203 if (TYPE_UNSIGNED (TREE_TYPE (rhs1
))
9204 && (can_float_p (fltmode
, TYPE_MODE (TREE_TYPE (rhs1
)), 0)
9205 != CODE_FOR_nothing
)
9206 && range_fits_type_p (vr
, TYPE_PRECISION (TREE_TYPE (rhs1
)), SIGNED
))
9207 mode
= TYPE_MODE (TREE_TYPE (rhs1
));
9208 /* If we can do the conversion in the current input mode do nothing. */
9209 else if (can_float_p (fltmode
, TYPE_MODE (TREE_TYPE (rhs1
)),
9210 TYPE_UNSIGNED (TREE_TYPE (rhs1
))) != CODE_FOR_nothing
)
9212 /* Otherwise search for a mode we can use, starting from the narrowest
9213 integer mode available. */
9216 mode
= GET_CLASS_NARROWEST_MODE (MODE_INT
);
9219 /* If we cannot do a signed conversion to float from mode
9220 or if the value-range does not fit in the signed type
9221 try with a wider mode. */
9222 if (can_float_p (fltmode
, mode
, 0) != CODE_FOR_nothing
9223 && range_fits_type_p (vr
, GET_MODE_PRECISION (mode
), SIGNED
))
9226 mode
= GET_MODE_WIDER_MODE (mode
);
9227 /* But do not widen the input. Instead leave that to the
9228 optabs expansion code. */
9229 if (GET_MODE_PRECISION (mode
) > TYPE_PRECISION (TREE_TYPE (rhs1
)))
9232 while (mode
!= VOIDmode
);
9233 if (mode
== VOIDmode
)
9237 /* It works, insert a truncation or sign-change before the
9238 float conversion. */
9239 tem
= make_ssa_name (build_nonstandard_integer_type
9240 (GET_MODE_PRECISION (mode
), 0), NULL
);
9241 conv
= gimple_build_assign_with_ops (NOP_EXPR
, tem
, rhs1
, NULL_TREE
);
9242 gsi_insert_before (gsi
, conv
, GSI_SAME_STMT
);
9243 gimple_assign_set_rhs1 (stmt
, tem
);
9249 /* Simplify an internal fn call using ranges if possible. */
9252 simplify_internal_call_using_ranges (gimple_stmt_iterator
*gsi
, gimple stmt
)
9254 enum tree_code subcode
;
9255 switch (gimple_call_internal_fn (stmt
))
9257 case IFN_UBSAN_CHECK_ADD
:
9258 subcode
= PLUS_EXPR
;
9260 case IFN_UBSAN_CHECK_SUB
:
9261 subcode
= MINUS_EXPR
;
9263 case IFN_UBSAN_CHECK_MUL
:
9264 subcode
= MULT_EXPR
;
9270 value_range_t vr0
= VR_INITIALIZER
;
9271 value_range_t vr1
= VR_INITIALIZER
;
9272 tree op0
= gimple_call_arg (stmt
, 0);
9273 tree op1
= gimple_call_arg (stmt
, 1);
9275 if (TREE_CODE (op0
) == SSA_NAME
)
9276 vr0
= *get_value_range (op0
);
9277 else if (TREE_CODE (op0
) == INTEGER_CST
)
9278 set_value_range_to_value (&vr0
, op0
, NULL
);
9280 set_value_range_to_varying (&vr0
);
9282 if (TREE_CODE (op1
) == SSA_NAME
)
9283 vr1
= *get_value_range (op1
);
9284 else if (TREE_CODE (op1
) == INTEGER_CST
)
9285 set_value_range_to_value (&vr1
, op1
, NULL
);
9287 set_value_range_to_varying (&vr1
);
9289 if (!range_int_cst_p (&vr0
))
9291 /* If one range is VR_ANTI_RANGE, VR_VARYING etc.,
9292 optimize at least x = y + 0; x = y - 0; x = y * 0;
9293 and x = y * 1; which never overflow. */
9294 if (!range_int_cst_p (&vr1
))
9296 if (tree_int_cst_sgn (vr1
.min
) == -1)
9298 if (compare_tree_int (vr1
.max
, subcode
== MULT_EXPR
) == 1)
9301 else if (!range_int_cst_p (&vr1
))
9303 /* If one range is VR_ANTI_RANGE, VR_VARYING etc.,
9304 optimize at least x = 0 + y; x = 0 * y; and x = 1 * y;
9305 which never overflow. */
9306 if (subcode
== MINUS_EXPR
)
9308 if (!range_int_cst_p (&vr0
))
9310 if (tree_int_cst_sgn (vr0
.min
) == -1)
9312 if (compare_tree_int (vr0
.max
, subcode
== MULT_EXPR
) == 1)
9317 tree r1
= int_const_binop (subcode
, vr0
.min
, vr1
.min
);
9318 tree r2
= int_const_binop (subcode
, vr0
.max
, vr1
.max
);
9319 if (r1
== NULL_TREE
|| TREE_OVERFLOW (r1
)
9320 || r2
== NULL_TREE
|| TREE_OVERFLOW (r2
))
9322 if (subcode
== MULT_EXPR
)
9324 tree r3
= int_const_binop (subcode
, vr0
.min
, vr1
.max
);
9325 tree r4
= int_const_binop (subcode
, vr0
.max
, vr1
.min
);
9326 if (r3
== NULL_TREE
|| TREE_OVERFLOW (r3
)
9327 || r4
== NULL_TREE
|| TREE_OVERFLOW (r4
))
9332 gimple g
= gimple_build_assign_with_ops (subcode
, gimple_call_lhs (stmt
),
9334 gsi_replace (gsi
, g
, false);
9338 /* Simplify STMT using ranges if possible. */
9341 simplify_stmt_using_ranges (gimple_stmt_iterator
*gsi
)
9343 gimple stmt
= gsi_stmt (*gsi
);
9344 if (is_gimple_assign (stmt
))
9346 enum tree_code rhs_code
= gimple_assign_rhs_code (stmt
);
9347 tree rhs1
= gimple_assign_rhs1 (stmt
);
9353 /* Transform EQ_EXPR, NE_EXPR into BIT_XOR_EXPR or identity
9354 if the RHS is zero or one, and the LHS are known to be boolean
9356 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
9357 return simplify_truth_ops_using_ranges (gsi
, stmt
);
9360 /* Transform TRUNC_DIV_EXPR and TRUNC_MOD_EXPR into RSHIFT_EXPR
9361 and BIT_AND_EXPR respectively if the first operand is greater
9362 than zero and the second operand is an exact power of two. */
9363 case TRUNC_DIV_EXPR
:
9364 case TRUNC_MOD_EXPR
:
9365 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1
))
9366 && integer_pow2p (gimple_assign_rhs2 (stmt
)))
9367 return simplify_div_or_mod_using_ranges (stmt
);
9370 /* Transform ABS (X) into X or -X as appropriate. */
9372 if (TREE_CODE (rhs1
) == SSA_NAME
9373 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
9374 return simplify_abs_using_ranges (stmt
);
9379 /* Optimize away BIT_AND_EXPR and BIT_IOR_EXPR
9380 if all the bits being cleared are already cleared or
9381 all the bits being set are already set. */
9382 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
9383 return simplify_bit_ops_using_ranges (gsi
, stmt
);
9387 if (TREE_CODE (rhs1
) == SSA_NAME
9388 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
9389 return simplify_conversion_using_ranges (stmt
);
9393 if (TREE_CODE (rhs1
) == SSA_NAME
9394 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
9395 return simplify_float_conversion_using_ranges (gsi
, stmt
);
9402 else if (gimple_code (stmt
) == GIMPLE_COND
)
9403 return simplify_cond_using_ranges (stmt
);
9404 else if (gimple_code (stmt
) == GIMPLE_SWITCH
)
9405 return simplify_switch_using_ranges (stmt
);
9406 else if (is_gimple_call (stmt
)
9407 && gimple_call_internal_p (stmt
))
9408 return simplify_internal_call_using_ranges (gsi
, stmt
);
9413 /* If the statement pointed by SI has a predicate whose value can be
9414 computed using the value range information computed by VRP, compute
9415 its value and return true. Otherwise, return false. */
9418 fold_predicate_in (gimple_stmt_iterator
*si
)
9420 bool assignment_p
= false;
9422 gimple stmt
= gsi_stmt (*si
);
9424 if (is_gimple_assign (stmt
)
9425 && TREE_CODE_CLASS (gimple_assign_rhs_code (stmt
)) == tcc_comparison
)
9427 assignment_p
= true;
9428 val
= vrp_evaluate_conditional (gimple_assign_rhs_code (stmt
),
9429 gimple_assign_rhs1 (stmt
),
9430 gimple_assign_rhs2 (stmt
),
9433 else if (gimple_code (stmt
) == GIMPLE_COND
)
9434 val
= vrp_evaluate_conditional (gimple_cond_code (stmt
),
9435 gimple_cond_lhs (stmt
),
9436 gimple_cond_rhs (stmt
),
9444 val
= fold_convert (gimple_expr_type (stmt
), val
);
9448 fprintf (dump_file
, "Folding predicate ");
9449 print_gimple_expr (dump_file
, stmt
, 0, 0);
9450 fprintf (dump_file
, " to ");
9451 print_generic_expr (dump_file
, val
, 0);
9452 fprintf (dump_file
, "\n");
9455 if (is_gimple_assign (stmt
))
9456 gimple_assign_set_rhs_from_tree (si
, val
);
9459 gcc_assert (gimple_code (stmt
) == GIMPLE_COND
);
9460 if (integer_zerop (val
))
9461 gimple_cond_make_false (stmt
);
9462 else if (integer_onep (val
))
9463 gimple_cond_make_true (stmt
);
9474 /* Callback for substitute_and_fold folding the stmt at *SI. */
9477 vrp_fold_stmt (gimple_stmt_iterator
*si
)
9479 if (fold_predicate_in (si
))
9482 return simplify_stmt_using_ranges (si
);
9485 /* Stack of dest,src equivalency pairs that need to be restored after
9486 each attempt to thread a block's incoming edge to an outgoing edge.
9488 A NULL entry is used to mark the end of pairs which need to be
9490 static vec
<tree
> equiv_stack
;
9492 /* A trivial wrapper so that we can present the generic jump threading
9493 code with a simple API for simplifying statements. STMT is the
9494 statement we want to simplify, WITHIN_STMT provides the location
9495 for any overflow warnings. */
9498 simplify_stmt_for_jump_threading (gimple stmt
, gimple within_stmt
)
9500 if (gimple_code (stmt
) == GIMPLE_COND
)
9501 return vrp_evaluate_conditional (gimple_cond_code (stmt
),
9502 gimple_cond_lhs (stmt
),
9503 gimple_cond_rhs (stmt
), within_stmt
);
9505 if (gimple_code (stmt
) == GIMPLE_ASSIGN
)
9507 value_range_t new_vr
= VR_INITIALIZER
;
9508 tree lhs
= gimple_assign_lhs (stmt
);
9510 if (TREE_CODE (lhs
) == SSA_NAME
9511 && (INTEGRAL_TYPE_P (TREE_TYPE (lhs
))
9512 || POINTER_TYPE_P (TREE_TYPE (lhs
))))
9514 extract_range_from_assignment (&new_vr
, stmt
);
9515 if (range_int_cst_singleton_p (&new_vr
))
9523 /* Blocks which have more than one predecessor and more than
9524 one successor present jump threading opportunities, i.e.,
9525 when the block is reached from a specific predecessor, we
9526 may be able to determine which of the outgoing edges will
9527 be traversed. When this optimization applies, we are able
9528 to avoid conditionals at runtime and we may expose secondary
9529 optimization opportunities.
9531 This routine is effectively a driver for the generic jump
9532 threading code. It basically just presents the generic code
9533 with edges that may be suitable for jump threading.
9535 Unlike DOM, we do not iterate VRP if jump threading was successful.
9536 While iterating may expose new opportunities for VRP, it is expected
9537 those opportunities would be very limited and the compile time cost
9538 to expose those opportunities would be significant.
9540 As jump threading opportunities are discovered, they are registered
9541 for later realization. */
9544 identify_jump_threads (void)
9551 /* Ugh. When substituting values earlier in this pass we can
9552 wipe the dominance information. So rebuild the dominator
9553 information as we need it within the jump threading code. */
9554 calculate_dominance_info (CDI_DOMINATORS
);
9556 /* We do not allow VRP information to be used for jump threading
9557 across a back edge in the CFG. Otherwise it becomes too
9558 difficult to avoid eliminating loop exit tests. Of course
9559 EDGE_DFS_BACK is not accurate at this time so we have to
9561 mark_dfs_back_edges ();
9563 /* Do not thread across edges we are about to remove. Just marking
9564 them as EDGE_DFS_BACK will do. */
9565 FOR_EACH_VEC_ELT (to_remove_edges
, i
, e
)
9566 e
->flags
|= EDGE_DFS_BACK
;
9568 /* Allocate our unwinder stack to unwind any temporary equivalences
9569 that might be recorded. */
9570 equiv_stack
.create (20);
9572 /* To avoid lots of silly node creation, we create a single
9573 conditional and just modify it in-place when attempting to
9575 dummy
= gimple_build_cond (EQ_EXPR
,
9576 integer_zero_node
, integer_zero_node
,
9579 /* Walk through all the blocks finding those which present a
9580 potential jump threading opportunity. We could set this up
9581 as a dominator walker and record data during the walk, but
9582 I doubt it's worth the effort for the classes of jump
9583 threading opportunities we are trying to identify at this
9584 point in compilation. */
9585 FOR_EACH_BB_FN (bb
, cfun
)
9589 /* If the generic jump threading code does not find this block
9590 interesting, then there is nothing to do. */
9591 if (! potentially_threadable_block (bb
))
9594 /* We only care about blocks ending in a COND_EXPR. While there
9595 may be some value in handling SWITCH_EXPR here, I doubt it's
9596 terribly important. */
9597 last
= gsi_stmt (gsi_last_bb (bb
));
9599 /* We're basically looking for a switch or any kind of conditional with
9600 integral or pointer type arguments. Note the type of the second
9601 argument will be the same as the first argument, so no need to
9602 check it explicitly. */
9603 if (gimple_code (last
) == GIMPLE_SWITCH
9604 || (gimple_code (last
) == GIMPLE_COND
9605 && TREE_CODE (gimple_cond_lhs (last
)) == SSA_NAME
9606 && (INTEGRAL_TYPE_P (TREE_TYPE (gimple_cond_lhs (last
)))
9607 || POINTER_TYPE_P (TREE_TYPE (gimple_cond_lhs (last
))))
9608 && (TREE_CODE (gimple_cond_rhs (last
)) == SSA_NAME
9609 || is_gimple_min_invariant (gimple_cond_rhs (last
)))))
9613 /* We've got a block with multiple predecessors and multiple
9614 successors which also ends in a suitable conditional or
9615 switch statement. For each predecessor, see if we can thread
9616 it to a specific successor. */
9617 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
9619 /* Do not thread across back edges or abnormal edges
9621 if (e
->flags
& (EDGE_DFS_BACK
| EDGE_COMPLEX
))
9624 thread_across_edge (dummy
, e
, true, &equiv_stack
,
9625 simplify_stmt_for_jump_threading
);
9630 /* We do not actually update the CFG or SSA graphs at this point as
9631 ASSERT_EXPRs are still in the IL and cfg cleanup code does not yet
9632 handle ASSERT_EXPRs gracefully. */
9635 /* We identified all the jump threading opportunities earlier, but could
9636 not transform the CFG at that time. This routine transforms the
9637 CFG and arranges for the dominator tree to be rebuilt if necessary.
9639 Note the SSA graph update will occur during the normal TODO
9640 processing by the pass manager. */
9642 finalize_jump_threads (void)
9644 thread_through_all_blocks (false);
9645 equiv_stack
.release ();
9649 /* Traverse all the blocks folding conditionals with known ranges. */
9656 values_propagated
= true;
9660 fprintf (dump_file
, "\nValue ranges after VRP:\n\n");
9661 dump_all_value_ranges (dump_file
);
9662 fprintf (dump_file
, "\n");
9665 substitute_and_fold (op_with_constant_singleton_value_range
,
9666 vrp_fold_stmt
, false);
9668 if (warn_array_bounds
)
9669 check_all_array_refs ();
9671 /* We must identify jump threading opportunities before we release
9672 the datastructures built by VRP. */
9673 identify_jump_threads ();
9675 /* Set value range to non pointer SSA_NAMEs. */
9676 for (i
= 0; i
< num_vr_values
; i
++)
9679 tree name
= ssa_name (i
);
9682 || POINTER_TYPE_P (TREE_TYPE (name
))
9683 || (vr_value
[i
]->type
== VR_VARYING
)
9684 || (vr_value
[i
]->type
== VR_UNDEFINED
))
9687 if ((TREE_CODE (vr_value
[i
]->min
) == INTEGER_CST
)
9688 && (TREE_CODE (vr_value
[i
]->max
) == INTEGER_CST
)
9689 && (vr_value
[i
]->type
== VR_RANGE
9690 || vr_value
[i
]->type
== VR_ANTI_RANGE
))
9691 set_range_info (name
, vr_value
[i
]->type
, vr_value
[i
]->min
,
9695 /* Free allocated memory. */
9696 for (i
= 0; i
< num_vr_values
; i
++)
9699 BITMAP_FREE (vr_value
[i
]->equiv
);
9704 free (vr_phi_edge_counts
);
9706 /* So that we can distinguish between VRP data being available
9707 and not available. */
9709 vr_phi_edge_counts
= NULL
;
9713 /* Main entry point to VRP (Value Range Propagation). This pass is
9714 loosely based on J. R. C. Patterson, ``Accurate Static Branch
9715 Prediction by Value Range Propagation,'' in SIGPLAN Conference on
9716 Programming Language Design and Implementation, pp. 67-78, 1995.
9717 Also available at http://citeseer.ist.psu.edu/patterson95accurate.html
9719 This is essentially an SSA-CCP pass modified to deal with ranges
9720 instead of constants.
9722 While propagating ranges, we may find that two or more SSA name
9723 have equivalent, though distinct ranges. For instance,
9726 2 p_4 = ASSERT_EXPR <p_3, p_3 != 0>
9728 4 p_5 = ASSERT_EXPR <p_4, p_4 == q_2>;
9732 In the code above, pointer p_5 has range [q_2, q_2], but from the
9733 code we can also determine that p_5 cannot be NULL and, if q_2 had
9734 a non-varying range, p_5's range should also be compatible with it.
9736 These equivalences are created by two expressions: ASSERT_EXPR and
9737 copy operations. Since p_5 is an assertion on p_4, and p_4 was the
9738 result of another assertion, then we can use the fact that p_5 and
9739 p_4 are equivalent when evaluating p_5's range.
9741 Together with value ranges, we also propagate these equivalences
9742 between names so that we can take advantage of information from
9743 multiple ranges when doing final replacement. Note that this
9744 equivalency relation is transitive but not symmetric.
9746 In the example above, p_5 is equivalent to p_4, q_2 and p_3, but we
9747 cannot assert that q_2 is equivalent to p_5 because q_2 may be used
9748 in contexts where that assertion does not hold (e.g., in line 6).
9750 TODO, the main difference between this pass and Patterson's is that
9751 we do not propagate edge probabilities. We only compute whether
9752 edges can be taken or not. That is, instead of having a spectrum
9753 of jump probabilities between 0 and 1, we only deal with 0, 1 and
9754 DON'T KNOW. In the future, it may be worthwhile to propagate
9755 probabilities to aid branch prediction. */
9764 loop_optimizer_init (LOOPS_NORMAL
| LOOPS_HAVE_RECORDED_EXITS
);
9765 rewrite_into_loop_closed_ssa (NULL
, TODO_update_ssa
);
9768 /* ??? This ends up using stale EDGE_DFS_BACK for liveness computation.
9769 Inserting assertions may split edges which will invalidate
9771 insert_range_assertions ();
9773 to_remove_edges
.create (10);
9774 to_update_switch_stmts
.create (5);
9775 threadedge_initialize_values ();
9777 /* For visiting PHI nodes we need EDGE_DFS_BACK computed. */
9778 mark_dfs_back_edges ();
9781 ssa_propagate (vrp_visit_stmt
, vrp_visit_phi_node
);
9784 free_numbers_of_iterations_estimates ();
9786 /* ASSERT_EXPRs must be removed before finalizing jump threads
9787 as finalizing jump threads calls the CFG cleanup code which
9788 does not properly handle ASSERT_EXPRs. */
9789 remove_range_assertions ();
9791 /* If we exposed any new variables, go ahead and put them into
9792 SSA form now, before we handle jump threading. This simplifies
9793 interactions between rewriting of _DECL nodes into SSA form
9794 and rewriting SSA_NAME nodes into SSA form after block
9795 duplication and CFG manipulation. */
9796 update_ssa (TODO_update_ssa
);
9798 finalize_jump_threads ();
9800 /* Remove dead edges from SWITCH_EXPR optimization. This leaves the
9801 CFG in a broken state and requires a cfg_cleanup run. */
9802 FOR_EACH_VEC_ELT (to_remove_edges
, i
, e
)
9804 /* Update SWITCH_EXPR case label vector. */
9805 FOR_EACH_VEC_ELT (to_update_switch_stmts
, i
, su
)
9808 size_t n
= TREE_VEC_LENGTH (su
->vec
);
9810 gimple_switch_set_num_labels (su
->stmt
, n
);
9811 for (j
= 0; j
< n
; j
++)
9812 gimple_switch_set_label (su
->stmt
, j
, TREE_VEC_ELT (su
->vec
, j
));
9813 /* As we may have replaced the default label with a regular one
9814 make sure to make it a real default label again. This ensures
9815 optimal expansion. */
9816 label
= gimple_switch_label (su
->stmt
, 0);
9817 CASE_LOW (label
) = NULL_TREE
;
9818 CASE_HIGH (label
) = NULL_TREE
;
9821 if (to_remove_edges
.length () > 0)
9823 free_dominance_info (CDI_DOMINATORS
);
9825 loops_state_set (LOOPS_NEED_FIXUP
);
9828 to_remove_edges
.release ();
9829 to_update_switch_stmts
.release ();
9830 threadedge_finalize_values ();
9833 loop_optimizer_finalize ();
9840 return flag_tree_vrp
!= 0;
9845 const pass_data pass_data_vrp
=
9847 GIMPLE_PASS
, /* type */
9849 OPTGROUP_NONE
, /* optinfo_flags */
9850 true, /* has_gate */
9851 true, /* has_execute */
9852 TV_TREE_VRP
, /* tv_id */
9853 PROP_ssa
, /* properties_required */
9854 0, /* properties_provided */
9855 0, /* properties_destroyed */
9856 0, /* todo_flags_start */
9857 ( TODO_cleanup_cfg
| TODO_update_ssa
9859 | TODO_verify_flow
), /* todo_flags_finish */
9862 class pass_vrp
: public gimple_opt_pass
9865 pass_vrp (gcc::context
*ctxt
)
9866 : gimple_opt_pass (pass_data_vrp
, ctxt
)
9869 /* opt_pass methods: */
9870 opt_pass
* clone () { return new pass_vrp (m_ctxt
); }
9871 bool gate () { return gate_vrp (); }
9872 unsigned int execute () { return execute_vrp (); }
9874 }; // class pass_vrp
9879 make_pass_vrp (gcc::context
*ctxt
)
9881 return new pass_vrp (ctxt
);