1 /* Support routines for Value Range Propagation (VRP).
2 Copyright (C) 2005-2015 Free Software Foundation, Inc.
3 Contributed by Diego Novillo <dnovillo@redhat.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
23 #include "coretypes.h"
29 #include "double-int.h"
36 #include "fold-const.h"
37 #include "stor-layout.h"
40 #include "hard-reg-set.h"
43 #include "dominance.h"
46 #include "basic-block.h"
47 #include "tree-ssa-alias.h"
48 #include "internal-fn.h"
49 #include "gimple-fold.h"
51 #include "gimple-expr.h"
54 #include "gimple-iterator.h"
55 #include "gimple-walk.h"
56 #include "gimple-ssa.h"
58 #include "tree-phinodes.h"
59 #include "ssa-iterators.h"
60 #include "stringpool.h"
61 #include "tree-ssanames.h"
62 #include "tree-ssa-loop-manip.h"
63 #include "tree-ssa-loop-niter.h"
64 #include "tree-ssa-loop.h"
65 #include "tree-into-ssa.h"
67 #include "tree-pass.h"
68 #include "tree-dump.h"
69 #include "gimple-pretty-print.h"
70 #include "diagnostic-core.h"
73 #include "tree-scalar-evolution.h"
74 #include "tree-ssa-propagate.h"
75 #include "tree-chrec.h"
76 #include "tree-ssa-threadupdate.h"
78 #include "insn-codes.h"
80 #include "tree-ssa-threadedge.h"
85 /* Range of values that can be associated with an SSA_NAME after VRP
89 /* Lattice value represented by this range. */
90 enum value_range_type type
;
92 /* Minimum and maximum values represented by this range. These
93 values should be interpreted as follows:
95 - If TYPE is VR_UNDEFINED or VR_VARYING then MIN and MAX must
98 - If TYPE == VR_RANGE then MIN holds the minimum value and
99 MAX holds the maximum value of the range [MIN, MAX].
101 - If TYPE == ANTI_RANGE the variable is known to NOT
102 take any values in the range [MIN, MAX]. */
106 /* Set of SSA names whose value ranges are equivalent to this one.
107 This set is only valid when TYPE is VR_RANGE or VR_ANTI_RANGE. */
111 typedef struct value_range_d value_range_t
;
113 #define VR_INITIALIZER { VR_UNDEFINED, NULL_TREE, NULL_TREE, NULL }
115 /* Set of SSA names found live during the RPO traversal of the function
116 for still active basic-blocks. */
117 static sbitmap
*live
;
119 /* Return true if the SSA name NAME is live on the edge E. */
122 live_on_edge (edge e
, tree name
)
124 return (live
[e
->dest
->index
]
125 && bitmap_bit_p (live
[e
->dest
->index
], SSA_NAME_VERSION (name
)));
128 /* Local functions. */
129 static int compare_values (tree val1
, tree val2
);
130 static int compare_values_warnv (tree val1
, tree val2
, bool *);
131 static void vrp_meet (value_range_t
*, value_range_t
*);
132 static void vrp_intersect_ranges (value_range_t
*, value_range_t
*);
133 static tree
vrp_evaluate_conditional_warnv_with_ops (enum tree_code
,
134 tree
, tree
, bool, bool *,
137 /* Location information for ASSERT_EXPRs. Each instance of this
138 structure describes an ASSERT_EXPR for an SSA name. Since a single
139 SSA name may have more than one assertion associated with it, these
140 locations are kept in a linked list attached to the corresponding
142 struct assert_locus_d
144 /* Basic block where the assertion would be inserted. */
147 /* Some assertions need to be inserted on an edge (e.g., assertions
148 generated by COND_EXPRs). In those cases, BB will be NULL. */
151 /* Pointer to the statement that generated this assertion. */
152 gimple_stmt_iterator si
;
154 /* Predicate code for the ASSERT_EXPR. Must be COMPARISON_CLASS_P. */
155 enum tree_code comp_code
;
157 /* Value being compared against. */
160 /* Expression to compare. */
163 /* Next node in the linked list. */
164 struct assert_locus_d
*next
;
167 typedef struct assert_locus_d
*assert_locus_t
;
169 /* If bit I is present, it means that SSA name N_i has a list of
170 assertions that should be inserted in the IL. */
171 static bitmap need_assert_for
;
173 /* Array of locations lists where to insert assertions. ASSERTS_FOR[I]
174 holds a list of ASSERT_LOCUS_T nodes that describe where
175 ASSERT_EXPRs for SSA name N_I should be inserted. */
176 static assert_locus_t
*asserts_for
;
178 /* Value range array. After propagation, VR_VALUE[I] holds the range
179 of values that SSA name N_I may take. */
180 static unsigned num_vr_values
;
181 static value_range_t
**vr_value
;
182 static bool values_propagated
;
184 /* For a PHI node which sets SSA name N_I, VR_COUNTS[I] holds the
185 number of executable edges we saw the last time we visited the
187 static int *vr_phi_edge_counts
;
194 static vec
<edge
> to_remove_edges
;
195 static vec
<switch_update
> to_update_switch_stmts
;
198 /* Return the maximum value for TYPE. */
201 vrp_val_max (const_tree type
)
203 if (!INTEGRAL_TYPE_P (type
))
206 return TYPE_MAX_VALUE (type
);
209 /* Return the minimum value for TYPE. */
212 vrp_val_min (const_tree type
)
214 if (!INTEGRAL_TYPE_P (type
))
217 return TYPE_MIN_VALUE (type
);
220 /* Return whether VAL is equal to the maximum value of its type. This
221 will be true for a positive overflow infinity. We can't do a
222 simple equality comparison with TYPE_MAX_VALUE because C typedefs
223 and Ada subtypes can produce types whose TYPE_MAX_VALUE is not ==
224 to the integer constant with the same value in the type. */
227 vrp_val_is_max (const_tree val
)
229 tree type_max
= vrp_val_max (TREE_TYPE (val
));
230 return (val
== type_max
231 || (type_max
!= NULL_TREE
232 && operand_equal_p (val
, type_max
, 0)));
235 /* Return whether VAL is equal to the minimum value of its type. This
236 will be true for a negative overflow infinity. */
239 vrp_val_is_min (const_tree val
)
241 tree type_min
= vrp_val_min (TREE_TYPE (val
));
242 return (val
== type_min
243 || (type_min
!= NULL_TREE
244 && operand_equal_p (val
, type_min
, 0)));
248 /* Return whether TYPE should use an overflow infinity distinct from
249 TYPE_{MIN,MAX}_VALUE. We use an overflow infinity value to
250 represent a signed overflow during VRP computations. An infinity
251 is distinct from a half-range, which will go from some number to
252 TYPE_{MIN,MAX}_VALUE. */
255 needs_overflow_infinity (const_tree type
)
257 return INTEGRAL_TYPE_P (type
) && !TYPE_OVERFLOW_WRAPS (type
);
260 /* Return whether TYPE can support our overflow infinity
261 representation: we use the TREE_OVERFLOW flag, which only exists
262 for constants. If TYPE doesn't support this, we don't optimize
263 cases which would require signed overflow--we drop them to
267 supports_overflow_infinity (const_tree type
)
269 tree min
= vrp_val_min (type
), max
= vrp_val_max (type
);
270 #ifdef ENABLE_CHECKING
271 gcc_assert (needs_overflow_infinity (type
));
273 return (min
!= NULL_TREE
274 && CONSTANT_CLASS_P (min
)
276 && CONSTANT_CLASS_P (max
));
279 /* VAL is the maximum or minimum value of a type. Return a
280 corresponding overflow infinity. */
283 make_overflow_infinity (tree val
)
285 gcc_checking_assert (val
!= NULL_TREE
&& CONSTANT_CLASS_P (val
));
286 val
= copy_node (val
);
287 TREE_OVERFLOW (val
) = 1;
291 /* Return a negative overflow infinity for TYPE. */
294 negative_overflow_infinity (tree type
)
296 gcc_checking_assert (supports_overflow_infinity (type
));
297 return make_overflow_infinity (vrp_val_min (type
));
300 /* Return a positive overflow infinity for TYPE. */
303 positive_overflow_infinity (tree type
)
305 gcc_checking_assert (supports_overflow_infinity (type
));
306 return make_overflow_infinity (vrp_val_max (type
));
309 /* Return whether VAL is a negative overflow infinity. */
312 is_negative_overflow_infinity (const_tree val
)
314 return (TREE_OVERFLOW_P (val
)
315 && needs_overflow_infinity (TREE_TYPE (val
))
316 && vrp_val_is_min (val
));
319 /* Return whether VAL is a positive overflow infinity. */
322 is_positive_overflow_infinity (const_tree val
)
324 return (TREE_OVERFLOW_P (val
)
325 && needs_overflow_infinity (TREE_TYPE (val
))
326 && vrp_val_is_max (val
));
329 /* Return whether VAL is a positive or negative overflow infinity. */
332 is_overflow_infinity (const_tree val
)
334 return (TREE_OVERFLOW_P (val
)
335 && needs_overflow_infinity (TREE_TYPE (val
))
336 && (vrp_val_is_min (val
) || vrp_val_is_max (val
)));
339 /* Return whether STMT has a constant rhs that is_overflow_infinity. */
342 stmt_overflow_infinity (gimple stmt
)
344 if (is_gimple_assign (stmt
)
345 && get_gimple_rhs_class (gimple_assign_rhs_code (stmt
)) ==
347 return is_overflow_infinity (gimple_assign_rhs1 (stmt
));
351 /* If VAL is now an overflow infinity, return VAL. Otherwise, return
352 the same value with TREE_OVERFLOW clear. This can be used to avoid
353 confusing a regular value with an overflow value. */
356 avoid_overflow_infinity (tree val
)
358 if (!is_overflow_infinity (val
))
361 if (vrp_val_is_max (val
))
362 return vrp_val_max (TREE_TYPE (val
));
365 gcc_checking_assert (vrp_val_is_min (val
));
366 return vrp_val_min (TREE_TYPE (val
));
371 /* Return true if ARG is marked with the nonnull attribute in the
372 current function signature. */
375 nonnull_arg_p (const_tree arg
)
377 tree t
, attrs
, fntype
;
378 unsigned HOST_WIDE_INT arg_num
;
380 gcc_assert (TREE_CODE (arg
) == PARM_DECL
&& POINTER_TYPE_P (TREE_TYPE (arg
)));
382 /* The static chain decl is always non null. */
383 if (arg
== cfun
->static_chain_decl
)
386 fntype
= TREE_TYPE (current_function_decl
);
387 for (attrs
= TYPE_ATTRIBUTES (fntype
); attrs
; attrs
= TREE_CHAIN (attrs
))
389 attrs
= lookup_attribute ("nonnull", attrs
);
391 /* If "nonnull" wasn't specified, we know nothing about the argument. */
392 if (attrs
== NULL_TREE
)
395 /* If "nonnull" applies to all the arguments, then ARG is non-null. */
396 if (TREE_VALUE (attrs
) == NULL_TREE
)
399 /* Get the position number for ARG in the function signature. */
400 for (arg_num
= 1, t
= DECL_ARGUMENTS (current_function_decl
);
402 t
= DECL_CHAIN (t
), arg_num
++)
408 gcc_assert (t
== arg
);
410 /* Now see if ARG_NUM is mentioned in the nonnull list. */
411 for (t
= TREE_VALUE (attrs
); t
; t
= TREE_CHAIN (t
))
413 if (compare_tree_int (TREE_VALUE (t
), arg_num
) == 0)
422 /* Set value range VR to VR_UNDEFINED. */
425 set_value_range_to_undefined (value_range_t
*vr
)
427 vr
->type
= VR_UNDEFINED
;
428 vr
->min
= vr
->max
= NULL_TREE
;
430 bitmap_clear (vr
->equiv
);
434 /* Set value range VR to VR_VARYING. */
437 set_value_range_to_varying (value_range_t
*vr
)
439 vr
->type
= VR_VARYING
;
440 vr
->min
= vr
->max
= NULL_TREE
;
442 bitmap_clear (vr
->equiv
);
446 /* Set value range VR to {T, MIN, MAX, EQUIV}. */
449 set_value_range (value_range_t
*vr
, enum value_range_type t
, tree min
,
450 tree max
, bitmap equiv
)
452 #if defined ENABLE_CHECKING
453 /* Check the validity of the range. */
454 if (t
== VR_RANGE
|| t
== VR_ANTI_RANGE
)
458 gcc_assert (min
&& max
);
460 gcc_assert ((!TREE_OVERFLOW_P (min
) || is_overflow_infinity (min
))
461 && (!TREE_OVERFLOW_P (max
) || is_overflow_infinity (max
)));
463 if (INTEGRAL_TYPE_P (TREE_TYPE (min
)) && t
== VR_ANTI_RANGE
)
464 gcc_assert (!vrp_val_is_min (min
) || !vrp_val_is_max (max
));
466 cmp
= compare_values (min
, max
);
467 gcc_assert (cmp
== 0 || cmp
== -1 || cmp
== -2);
469 if (needs_overflow_infinity (TREE_TYPE (min
)))
470 gcc_assert (!is_overflow_infinity (min
)
471 || !is_overflow_infinity (max
));
474 if (t
== VR_UNDEFINED
|| t
== VR_VARYING
)
475 gcc_assert (min
== NULL_TREE
&& max
== NULL_TREE
);
477 if (t
== VR_UNDEFINED
|| t
== VR_VARYING
)
478 gcc_assert (equiv
== NULL
|| bitmap_empty_p (equiv
));
485 /* Since updating the equivalence set involves deep copying the
486 bitmaps, only do it if absolutely necessary. */
487 if (vr
->equiv
== NULL
489 vr
->equiv
= BITMAP_ALLOC (NULL
);
491 if (equiv
!= vr
->equiv
)
493 if (equiv
&& !bitmap_empty_p (equiv
))
494 bitmap_copy (vr
->equiv
, equiv
);
496 bitmap_clear (vr
->equiv
);
501 /* Set value range VR to the canonical form of {T, MIN, MAX, EQUIV}.
502 This means adjusting T, MIN and MAX representing the case of a
503 wrapping range with MAX < MIN covering [MIN, type_max] U [type_min, MAX]
504 as anti-rage ~[MAX+1, MIN-1]. Likewise for wrapping anti-ranges.
505 In corner cases where MAX+1 or MIN-1 wraps this will fall back
507 This routine exists to ease canonicalization in the case where we
508 extract ranges from var + CST op limit. */
511 set_and_canonicalize_value_range (value_range_t
*vr
, enum value_range_type t
,
512 tree min
, tree max
, bitmap equiv
)
514 /* Use the canonical setters for VR_UNDEFINED and VR_VARYING. */
515 if (t
== VR_UNDEFINED
)
517 set_value_range_to_undefined (vr
);
520 else if (t
== VR_VARYING
)
522 set_value_range_to_varying (vr
);
526 /* Nothing to canonicalize for symbolic ranges. */
527 if (TREE_CODE (min
) != INTEGER_CST
528 || TREE_CODE (max
) != INTEGER_CST
)
530 set_value_range (vr
, t
, min
, max
, equiv
);
534 /* Wrong order for min and max, to swap them and the VR type we need
536 if (tree_int_cst_lt (max
, min
))
540 /* For one bit precision if max < min, then the swapped
541 range covers all values, so for VR_RANGE it is varying and
542 for VR_ANTI_RANGE empty range, so drop to varying as well. */
543 if (TYPE_PRECISION (TREE_TYPE (min
)) == 1)
545 set_value_range_to_varying (vr
);
549 one
= build_int_cst (TREE_TYPE (min
), 1);
550 tmp
= int_const_binop (PLUS_EXPR
, max
, one
);
551 max
= int_const_binop (MINUS_EXPR
, min
, one
);
554 /* There's one corner case, if we had [C+1, C] before we now have
555 that again. But this represents an empty value range, so drop
556 to varying in this case. */
557 if (tree_int_cst_lt (max
, min
))
559 set_value_range_to_varying (vr
);
563 t
= t
== VR_RANGE
? VR_ANTI_RANGE
: VR_RANGE
;
566 /* Anti-ranges that can be represented as ranges should be so. */
567 if (t
== VR_ANTI_RANGE
)
569 bool is_min
= vrp_val_is_min (min
);
570 bool is_max
= vrp_val_is_max (max
);
572 if (is_min
&& is_max
)
574 /* We cannot deal with empty ranges, drop to varying.
575 ??? This could be VR_UNDEFINED instead. */
576 set_value_range_to_varying (vr
);
579 else if (TYPE_PRECISION (TREE_TYPE (min
)) == 1
580 && (is_min
|| is_max
))
582 /* Non-empty boolean ranges can always be represented
583 as a singleton range. */
585 min
= max
= vrp_val_max (TREE_TYPE (min
));
587 min
= max
= vrp_val_min (TREE_TYPE (min
));
591 /* As a special exception preserve non-null ranges. */
592 && !(TYPE_UNSIGNED (TREE_TYPE (min
))
593 && integer_zerop (max
)))
595 tree one
= build_int_cst (TREE_TYPE (max
), 1);
596 min
= int_const_binop (PLUS_EXPR
, max
, one
);
597 max
= vrp_val_max (TREE_TYPE (max
));
602 tree one
= build_int_cst (TREE_TYPE (min
), 1);
603 max
= int_const_binop (MINUS_EXPR
, min
, one
);
604 min
= vrp_val_min (TREE_TYPE (min
));
609 /* Drop [-INF(OVF), +INF(OVF)] to varying. */
610 if (needs_overflow_infinity (TREE_TYPE (min
))
611 && is_overflow_infinity (min
)
612 && is_overflow_infinity (max
))
614 set_value_range_to_varying (vr
);
618 set_value_range (vr
, t
, min
, max
, equiv
);
621 /* Copy value range FROM into value range TO. */
624 copy_value_range (value_range_t
*to
, value_range_t
*from
)
626 set_value_range (to
, from
->type
, from
->min
, from
->max
, from
->equiv
);
629 /* Set value range VR to a single value. This function is only called
630 with values we get from statements, and exists to clear the
631 TREE_OVERFLOW flag so that we don't think we have an overflow
632 infinity when we shouldn't. */
635 set_value_range_to_value (value_range_t
*vr
, tree val
, bitmap equiv
)
637 gcc_assert (is_gimple_min_invariant (val
));
638 if (TREE_OVERFLOW_P (val
))
639 val
= drop_tree_overflow (val
);
640 set_value_range (vr
, VR_RANGE
, val
, val
, equiv
);
643 /* Set value range VR to a non-negative range of type TYPE.
644 OVERFLOW_INFINITY indicates whether to use an overflow infinity
645 rather than TYPE_MAX_VALUE; this should be true if we determine
646 that the range is nonnegative based on the assumption that signed
647 overflow does not occur. */
650 set_value_range_to_nonnegative (value_range_t
*vr
, tree type
,
651 bool overflow_infinity
)
655 if (overflow_infinity
&& !supports_overflow_infinity (type
))
657 set_value_range_to_varying (vr
);
661 zero
= build_int_cst (type
, 0);
662 set_value_range (vr
, VR_RANGE
, zero
,
664 ? positive_overflow_infinity (type
)
665 : TYPE_MAX_VALUE (type
)),
669 /* Set value range VR to a non-NULL range of type TYPE. */
672 set_value_range_to_nonnull (value_range_t
*vr
, tree type
)
674 tree zero
= build_int_cst (type
, 0);
675 set_value_range (vr
, VR_ANTI_RANGE
, zero
, zero
, vr
->equiv
);
679 /* Set value range VR to a NULL range of type TYPE. */
682 set_value_range_to_null (value_range_t
*vr
, tree type
)
684 set_value_range_to_value (vr
, build_int_cst (type
, 0), vr
->equiv
);
688 /* Set value range VR to a range of a truthvalue of type TYPE. */
691 set_value_range_to_truthvalue (value_range_t
*vr
, tree type
)
693 if (TYPE_PRECISION (type
) == 1)
694 set_value_range_to_varying (vr
);
696 set_value_range (vr
, VR_RANGE
,
697 build_int_cst (type
, 0), build_int_cst (type
, 1),
702 /* If abs (min) < abs (max), set VR to [-max, max], if
703 abs (min) >= abs (max), set VR to [-min, min]. */
706 abs_extent_range (value_range_t
*vr
, tree min
, tree max
)
710 gcc_assert (TREE_CODE (min
) == INTEGER_CST
);
711 gcc_assert (TREE_CODE (max
) == INTEGER_CST
);
712 gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (min
)));
713 gcc_assert (!TYPE_UNSIGNED (TREE_TYPE (min
)));
714 min
= fold_unary (ABS_EXPR
, TREE_TYPE (min
), min
);
715 max
= fold_unary (ABS_EXPR
, TREE_TYPE (max
), max
);
716 if (TREE_OVERFLOW (min
) || TREE_OVERFLOW (max
))
718 set_value_range_to_varying (vr
);
721 cmp
= compare_values (min
, max
);
723 min
= fold_unary (NEGATE_EXPR
, TREE_TYPE (min
), max
);
724 else if (cmp
== 0 || cmp
== 1)
727 min
= fold_unary (NEGATE_EXPR
, TREE_TYPE (min
), min
);
731 set_value_range_to_varying (vr
);
734 set_and_canonicalize_value_range (vr
, VR_RANGE
, min
, max
, NULL
);
738 /* Return value range information for VAR.
740 If we have no values ranges recorded (ie, VRP is not running), then
741 return NULL. Otherwise create an empty range if none existed for VAR. */
743 static value_range_t
*
744 get_value_range (const_tree var
)
746 static const struct value_range_d vr_const_varying
747 = { VR_VARYING
, NULL_TREE
, NULL_TREE
, NULL
};
750 unsigned ver
= SSA_NAME_VERSION (var
);
752 /* If we have no recorded ranges, then return NULL. */
756 /* If we query the range for a new SSA name return an unmodifiable VARYING.
757 We should get here at most from the substitute-and-fold stage which
758 will never try to change values. */
759 if (ver
>= num_vr_values
)
760 return CONST_CAST (value_range_t
*, &vr_const_varying
);
766 /* After propagation finished do not allocate new value-ranges. */
767 if (values_propagated
)
768 return CONST_CAST (value_range_t
*, &vr_const_varying
);
770 /* Create a default value range. */
771 vr_value
[ver
] = vr
= XCNEW (value_range_t
);
773 /* Defer allocating the equivalence set. */
776 /* If VAR is a default definition of a parameter, the variable can
777 take any value in VAR's type. */
778 if (SSA_NAME_IS_DEFAULT_DEF (var
))
780 sym
= SSA_NAME_VAR (var
);
781 if (TREE_CODE (sym
) == PARM_DECL
)
783 /* Try to use the "nonnull" attribute to create ~[0, 0]
784 anti-ranges for pointers. Note that this is only valid with
785 default definitions of PARM_DECLs. */
786 if (POINTER_TYPE_P (TREE_TYPE (sym
))
787 && nonnull_arg_p (sym
))
788 set_value_range_to_nonnull (vr
, TREE_TYPE (sym
));
790 set_value_range_to_varying (vr
);
792 else if (TREE_CODE (sym
) == RESULT_DECL
793 && DECL_BY_REFERENCE (sym
))
794 set_value_range_to_nonnull (vr
, TREE_TYPE (sym
));
800 /* Return true, if VAL1 and VAL2 are equal values for VRP purposes. */
803 vrp_operand_equal_p (const_tree val1
, const_tree val2
)
807 if (!val1
|| !val2
|| !operand_equal_p (val1
, val2
, 0))
809 return is_overflow_infinity (val1
) == is_overflow_infinity (val2
);
812 /* Return true, if the bitmaps B1 and B2 are equal. */
815 vrp_bitmap_equal_p (const_bitmap b1
, const_bitmap b2
)
818 || ((!b1
|| bitmap_empty_p (b1
))
819 && (!b2
|| bitmap_empty_p (b2
)))
821 && bitmap_equal_p (b1
, b2
)));
824 /* Update the value range and equivalence set for variable VAR to
825 NEW_VR. Return true if NEW_VR is different from VAR's previous
828 NOTE: This function assumes that NEW_VR is a temporary value range
829 object created for the sole purpose of updating VAR's range. The
830 storage used by the equivalence set from NEW_VR will be freed by
831 this function. Do not call update_value_range when NEW_VR
832 is the range object associated with another SSA name. */
835 update_value_range (const_tree var
, value_range_t
*new_vr
)
837 value_range_t
*old_vr
;
840 /* Update the value range, if necessary. */
841 old_vr
= get_value_range (var
);
842 is_new
= old_vr
->type
!= new_vr
->type
843 || !vrp_operand_equal_p (old_vr
->min
, new_vr
->min
)
844 || !vrp_operand_equal_p (old_vr
->max
, new_vr
->max
)
845 || !vrp_bitmap_equal_p (old_vr
->equiv
, new_vr
->equiv
);
849 /* Do not allow transitions up the lattice. The following
850 is slightly more awkward than just new_vr->type < old_vr->type
851 because VR_RANGE and VR_ANTI_RANGE need to be considered
852 the same. We may not have is_new when transitioning to
853 UNDEFINED or from VARYING. */
854 if (new_vr
->type
== VR_UNDEFINED
855 || old_vr
->type
== VR_VARYING
)
856 set_value_range_to_varying (old_vr
);
858 set_value_range (old_vr
, new_vr
->type
, new_vr
->min
, new_vr
->max
,
862 BITMAP_FREE (new_vr
->equiv
);
868 /* Add VAR and VAR's equivalence set to EQUIV. This is the central
869 point where equivalence processing can be turned on/off. */
872 add_equivalence (bitmap
*equiv
, const_tree var
)
874 unsigned ver
= SSA_NAME_VERSION (var
);
875 value_range_t
*vr
= vr_value
[ver
];
878 *equiv
= BITMAP_ALLOC (NULL
);
879 bitmap_set_bit (*equiv
, ver
);
881 bitmap_ior_into (*equiv
, vr
->equiv
);
885 /* Return true if VR is ~[0, 0]. */
888 range_is_nonnull (value_range_t
*vr
)
890 return vr
->type
== VR_ANTI_RANGE
891 && integer_zerop (vr
->min
)
892 && integer_zerop (vr
->max
);
896 /* Return true if VR is [0, 0]. */
899 range_is_null (value_range_t
*vr
)
901 return vr
->type
== VR_RANGE
902 && integer_zerop (vr
->min
)
903 && integer_zerop (vr
->max
);
906 /* Return true if max and min of VR are INTEGER_CST. It's not necessary
910 range_int_cst_p (value_range_t
*vr
)
912 return (vr
->type
== VR_RANGE
913 && TREE_CODE (vr
->max
) == INTEGER_CST
914 && TREE_CODE (vr
->min
) == INTEGER_CST
);
917 /* Return true if VR is a INTEGER_CST singleton. */
920 range_int_cst_singleton_p (value_range_t
*vr
)
922 return (range_int_cst_p (vr
)
923 && !is_overflow_infinity (vr
->min
)
924 && !is_overflow_infinity (vr
->max
)
925 && tree_int_cst_equal (vr
->min
, vr
->max
));
928 /* Return true if value range VR involves at least one symbol. */
931 symbolic_range_p (value_range_t
*vr
)
933 return (!is_gimple_min_invariant (vr
->min
)
934 || !is_gimple_min_invariant (vr
->max
));
937 /* Return the single symbol (an SSA_NAME) contained in T if any, or NULL_TREE
938 otherwise. We only handle additive operations and set NEG to true if the
939 symbol is negated and INV to the invariant part, if any. */
942 get_single_symbol (tree t
, bool *neg
, tree
*inv
)
947 if (TREE_CODE (t
) == PLUS_EXPR
948 || TREE_CODE (t
) == POINTER_PLUS_EXPR
949 || TREE_CODE (t
) == MINUS_EXPR
)
951 if (is_gimple_min_invariant (TREE_OPERAND (t
, 0)))
953 neg_
= (TREE_CODE (t
) == MINUS_EXPR
);
954 inv_
= TREE_OPERAND (t
, 0);
955 t
= TREE_OPERAND (t
, 1);
957 else if (is_gimple_min_invariant (TREE_OPERAND (t
, 1)))
960 inv_
= TREE_OPERAND (t
, 1);
961 t
= TREE_OPERAND (t
, 0);
972 if (TREE_CODE (t
) == NEGATE_EXPR
)
974 t
= TREE_OPERAND (t
, 0);
978 if (TREE_CODE (t
) != SSA_NAME
)
986 /* The reverse operation: build a symbolic expression with TYPE
987 from symbol SYM, negated according to NEG, and invariant INV. */
990 build_symbolic_expr (tree type
, tree sym
, bool neg
, tree inv
)
992 const bool pointer_p
= POINTER_TYPE_P (type
);
996 t
= build1 (NEGATE_EXPR
, type
, t
);
998 if (integer_zerop (inv
))
1001 return build2 (pointer_p
? POINTER_PLUS_EXPR
: PLUS_EXPR
, type
, t
, inv
);
1004 /* Return true if value range VR involves exactly one symbol SYM. */
1007 symbolic_range_based_on_p (value_range_t
*vr
, const_tree sym
)
1009 bool neg
, min_has_symbol
, max_has_symbol
;
1012 if (is_gimple_min_invariant (vr
->min
))
1013 min_has_symbol
= false;
1014 else if (get_single_symbol (vr
->min
, &neg
, &inv
) == sym
)
1015 min_has_symbol
= true;
1019 if (is_gimple_min_invariant (vr
->max
))
1020 max_has_symbol
= false;
1021 else if (get_single_symbol (vr
->max
, &neg
, &inv
) == sym
)
1022 max_has_symbol
= true;
1026 return (min_has_symbol
|| max_has_symbol
);
1029 /* Return true if value range VR uses an overflow infinity. */
1032 overflow_infinity_range_p (value_range_t
*vr
)
1034 return (vr
->type
== VR_RANGE
1035 && (is_overflow_infinity (vr
->min
)
1036 || is_overflow_infinity (vr
->max
)));
1039 /* Return false if we can not make a valid comparison based on VR;
1040 this will be the case if it uses an overflow infinity and overflow
1041 is not undefined (i.e., -fno-strict-overflow is in effect).
1042 Otherwise return true, and set *STRICT_OVERFLOW_P to true if VR
1043 uses an overflow infinity. */
1046 usable_range_p (value_range_t
*vr
, bool *strict_overflow_p
)
1048 gcc_assert (vr
->type
== VR_RANGE
);
1049 if (is_overflow_infinity (vr
->min
))
1051 *strict_overflow_p
= true;
1052 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (vr
->min
)))
1055 if (is_overflow_infinity (vr
->max
))
1057 *strict_overflow_p
= true;
1058 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (vr
->max
)))
1065 /* Return true if the result of assignment STMT is know to be non-negative.
1066 If the return value is based on the assumption that signed overflow is
1067 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1068 *STRICT_OVERFLOW_P.*/
1071 gimple_assign_nonnegative_warnv_p (gimple stmt
, bool *strict_overflow_p
)
1073 enum tree_code code
= gimple_assign_rhs_code (stmt
);
1074 switch (get_gimple_rhs_class (code
))
1076 case GIMPLE_UNARY_RHS
:
1077 return tree_unary_nonnegative_warnv_p (gimple_assign_rhs_code (stmt
),
1078 gimple_expr_type (stmt
),
1079 gimple_assign_rhs1 (stmt
),
1081 case GIMPLE_BINARY_RHS
:
1082 return tree_binary_nonnegative_warnv_p (gimple_assign_rhs_code (stmt
),
1083 gimple_expr_type (stmt
),
1084 gimple_assign_rhs1 (stmt
),
1085 gimple_assign_rhs2 (stmt
),
1087 case GIMPLE_TERNARY_RHS
:
1089 case GIMPLE_SINGLE_RHS
:
1090 return tree_single_nonnegative_warnv_p (gimple_assign_rhs1 (stmt
),
1092 case GIMPLE_INVALID_RHS
:
1099 /* Return true if return value of call STMT is know to be non-negative.
1100 If the return value is based on the assumption that signed overflow is
1101 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1102 *STRICT_OVERFLOW_P.*/
1105 gimple_call_nonnegative_warnv_p (gimple stmt
, bool *strict_overflow_p
)
1107 tree arg0
= gimple_call_num_args (stmt
) > 0 ?
1108 gimple_call_arg (stmt
, 0) : NULL_TREE
;
1109 tree arg1
= gimple_call_num_args (stmt
) > 1 ?
1110 gimple_call_arg (stmt
, 1) : NULL_TREE
;
1112 return tree_call_nonnegative_warnv_p (gimple_expr_type (stmt
),
1113 gimple_call_fndecl (stmt
),
1119 /* Return true if STMT is know to to compute a non-negative value.
1120 If the return value is based on the assumption that signed overflow is
1121 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1122 *STRICT_OVERFLOW_P.*/
1125 gimple_stmt_nonnegative_warnv_p (gimple stmt
, bool *strict_overflow_p
)
1127 switch (gimple_code (stmt
))
1130 return gimple_assign_nonnegative_warnv_p (stmt
, strict_overflow_p
);
1132 return gimple_call_nonnegative_warnv_p (stmt
, strict_overflow_p
);
1138 /* Return true if the result of assignment STMT is know to be non-zero.
1139 If the return value is based on the assumption that signed overflow is
1140 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1141 *STRICT_OVERFLOW_P.*/
1144 gimple_assign_nonzero_warnv_p (gimple stmt
, bool *strict_overflow_p
)
1146 enum tree_code code
= gimple_assign_rhs_code (stmt
);
1147 switch (get_gimple_rhs_class (code
))
1149 case GIMPLE_UNARY_RHS
:
1150 return tree_unary_nonzero_warnv_p (gimple_assign_rhs_code (stmt
),
1151 gimple_expr_type (stmt
),
1152 gimple_assign_rhs1 (stmt
),
1154 case GIMPLE_BINARY_RHS
:
1155 return tree_binary_nonzero_warnv_p (gimple_assign_rhs_code (stmt
),
1156 gimple_expr_type (stmt
),
1157 gimple_assign_rhs1 (stmt
),
1158 gimple_assign_rhs2 (stmt
),
1160 case GIMPLE_TERNARY_RHS
:
1162 case GIMPLE_SINGLE_RHS
:
1163 return tree_single_nonzero_warnv_p (gimple_assign_rhs1 (stmt
),
1165 case GIMPLE_INVALID_RHS
:
1172 /* Return true if STMT is known to compute a non-zero value.
1173 If the return value is based on the assumption that signed overflow is
1174 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1175 *STRICT_OVERFLOW_P.*/
1178 gimple_stmt_nonzero_warnv_p (gimple stmt
, bool *strict_overflow_p
)
1180 switch (gimple_code (stmt
))
1183 return gimple_assign_nonzero_warnv_p (stmt
, strict_overflow_p
);
1186 tree fndecl
= gimple_call_fndecl (stmt
);
1187 if (!fndecl
) return false;
1188 if (flag_delete_null_pointer_checks
&& !flag_check_new
1189 && DECL_IS_OPERATOR_NEW (fndecl
)
1190 && !TREE_NOTHROW (fndecl
))
1192 if (flag_delete_null_pointer_checks
&&
1193 lookup_attribute ("returns_nonnull",
1194 TYPE_ATTRIBUTES (gimple_call_fntype (stmt
))))
1196 return gimple_alloca_call_p (stmt
);
1203 /* Like tree_expr_nonzero_warnv_p, but this function uses value ranges
1207 vrp_stmt_computes_nonzero (gimple stmt
, bool *strict_overflow_p
)
1209 if (gimple_stmt_nonzero_warnv_p (stmt
, strict_overflow_p
))
1212 /* If we have an expression of the form &X->a, then the expression
1213 is nonnull if X is nonnull. */
1214 if (is_gimple_assign (stmt
)
1215 && gimple_assign_rhs_code (stmt
) == ADDR_EXPR
)
1217 tree expr
= gimple_assign_rhs1 (stmt
);
1218 tree base
= get_base_address (TREE_OPERAND (expr
, 0));
1220 if (base
!= NULL_TREE
1221 && TREE_CODE (base
) == MEM_REF
1222 && TREE_CODE (TREE_OPERAND (base
, 0)) == SSA_NAME
)
1224 value_range_t
*vr
= get_value_range (TREE_OPERAND (base
, 0));
1225 if (range_is_nonnull (vr
))
1233 /* Returns true if EXPR is a valid value (as expected by compare_values) --
1234 a gimple invariant, or SSA_NAME +- CST. */
1237 valid_value_p (tree expr
)
1239 if (TREE_CODE (expr
) == SSA_NAME
)
1242 if (TREE_CODE (expr
) == PLUS_EXPR
1243 || TREE_CODE (expr
) == MINUS_EXPR
)
1244 return (TREE_CODE (TREE_OPERAND (expr
, 0)) == SSA_NAME
1245 && TREE_CODE (TREE_OPERAND (expr
, 1)) == INTEGER_CST
);
1247 return is_gimple_min_invariant (expr
);
1253 -2 if those are incomparable. */
1255 operand_less_p (tree val
, tree val2
)
1257 /* LT is folded faster than GE and others. Inline the common case. */
1258 if (TREE_CODE (val
) == INTEGER_CST
&& TREE_CODE (val2
) == INTEGER_CST
)
1259 return tree_int_cst_lt (val
, val2
);
1264 fold_defer_overflow_warnings ();
1266 tcmp
= fold_binary_to_constant (LT_EXPR
, boolean_type_node
, val
, val2
);
1268 fold_undefer_and_ignore_overflow_warnings ();
1271 || TREE_CODE (tcmp
) != INTEGER_CST
)
1274 if (!integer_zerop (tcmp
))
1278 /* val >= val2, not considering overflow infinity. */
1279 if (is_negative_overflow_infinity (val
))
1280 return is_negative_overflow_infinity (val2
) ? 0 : 1;
1281 else if (is_positive_overflow_infinity (val2
))
1282 return is_positive_overflow_infinity (val
) ? 0 : 1;
1287 /* Compare two values VAL1 and VAL2. Return
1289 -2 if VAL1 and VAL2 cannot be compared at compile-time,
1292 +1 if VAL1 > VAL2, and
1295 This is similar to tree_int_cst_compare but supports pointer values
1296 and values that cannot be compared at compile time.
1298 If STRICT_OVERFLOW_P is not NULL, then set *STRICT_OVERFLOW_P to
1299 true if the return value is only valid if we assume that signed
1300 overflow is undefined. */
1303 compare_values_warnv (tree val1
, tree val2
, bool *strict_overflow_p
)
1308 /* Below we rely on the fact that VAL1 and VAL2 are both pointers or
1310 gcc_assert (POINTER_TYPE_P (TREE_TYPE (val1
))
1311 == POINTER_TYPE_P (TREE_TYPE (val2
)));
1313 /* Convert the two values into the same type. This is needed because
1314 sizetype causes sign extension even for unsigned types. */
1315 val2
= fold_convert (TREE_TYPE (val1
), val2
);
1316 STRIP_USELESS_TYPE_CONVERSION (val2
);
1318 if ((TREE_CODE (val1
) == SSA_NAME
1319 || (TREE_CODE (val1
) == NEGATE_EXPR
1320 && TREE_CODE (TREE_OPERAND (val1
, 0)) == SSA_NAME
)
1321 || TREE_CODE (val1
) == PLUS_EXPR
1322 || TREE_CODE (val1
) == MINUS_EXPR
)
1323 && (TREE_CODE (val2
) == SSA_NAME
1324 || (TREE_CODE (val2
) == NEGATE_EXPR
1325 && TREE_CODE (TREE_OPERAND (val2
, 0)) == SSA_NAME
)
1326 || TREE_CODE (val2
) == PLUS_EXPR
1327 || TREE_CODE (val2
) == MINUS_EXPR
))
1329 tree n1
, c1
, n2
, c2
;
1330 enum tree_code code1
, code2
;
1332 /* If VAL1 and VAL2 are of the form '[-]NAME [+-] CST' or 'NAME',
1333 return -1 or +1 accordingly. If VAL1 and VAL2 don't use the
1334 same name, return -2. */
1335 if (TREE_CODE (val1
) == SSA_NAME
|| TREE_CODE (val1
) == NEGATE_EXPR
)
1343 code1
= TREE_CODE (val1
);
1344 n1
= TREE_OPERAND (val1
, 0);
1345 c1
= TREE_OPERAND (val1
, 1);
1346 if (tree_int_cst_sgn (c1
) == -1)
1348 if (is_negative_overflow_infinity (c1
))
1350 c1
= fold_unary_to_constant (NEGATE_EXPR
, TREE_TYPE (c1
), c1
);
1353 code1
= code1
== MINUS_EXPR
? PLUS_EXPR
: MINUS_EXPR
;
1357 if (TREE_CODE (val2
) == SSA_NAME
|| TREE_CODE (val2
) == NEGATE_EXPR
)
1365 code2
= TREE_CODE (val2
);
1366 n2
= TREE_OPERAND (val2
, 0);
1367 c2
= TREE_OPERAND (val2
, 1);
1368 if (tree_int_cst_sgn (c2
) == -1)
1370 if (is_negative_overflow_infinity (c2
))
1372 c2
= fold_unary_to_constant (NEGATE_EXPR
, TREE_TYPE (c2
), c2
);
1375 code2
= code2
== MINUS_EXPR
? PLUS_EXPR
: MINUS_EXPR
;
1379 /* Both values must use the same name. */
1380 if (TREE_CODE (n1
) == NEGATE_EXPR
&& TREE_CODE (n2
) == NEGATE_EXPR
)
1382 n1
= TREE_OPERAND (n1
, 0);
1383 n2
= TREE_OPERAND (n2
, 0);
1388 if (code1
== SSA_NAME
&& code2
== SSA_NAME
)
1392 /* If overflow is defined we cannot simplify more. */
1393 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (val1
)))
1396 if (strict_overflow_p
!= NULL
1397 && (code1
== SSA_NAME
|| !TREE_NO_WARNING (val1
))
1398 && (code2
== SSA_NAME
|| !TREE_NO_WARNING (val2
)))
1399 *strict_overflow_p
= true;
1401 if (code1
== SSA_NAME
)
1403 if (code2
== PLUS_EXPR
)
1404 /* NAME < NAME + CST */
1406 else if (code2
== MINUS_EXPR
)
1407 /* NAME > NAME - CST */
1410 else if (code1
== PLUS_EXPR
)
1412 if (code2
== SSA_NAME
)
1413 /* NAME + CST > NAME */
1415 else if (code2
== PLUS_EXPR
)
1416 /* NAME + CST1 > NAME + CST2, if CST1 > CST2 */
1417 return compare_values_warnv (c1
, c2
, strict_overflow_p
);
1418 else if (code2
== MINUS_EXPR
)
1419 /* NAME + CST1 > NAME - CST2 */
1422 else if (code1
== MINUS_EXPR
)
1424 if (code2
== SSA_NAME
)
1425 /* NAME - CST < NAME */
1427 else if (code2
== PLUS_EXPR
)
1428 /* NAME - CST1 < NAME + CST2 */
1430 else if (code2
== MINUS_EXPR
)
1431 /* NAME - CST1 > NAME - CST2, if CST1 < CST2. Notice that
1432 C1 and C2 are swapped in the call to compare_values. */
1433 return compare_values_warnv (c2
, c1
, strict_overflow_p
);
1439 /* We cannot compare non-constants. */
1440 if (!is_gimple_min_invariant (val1
) || !is_gimple_min_invariant (val2
))
1443 if (!POINTER_TYPE_P (TREE_TYPE (val1
)))
1445 /* We cannot compare overflowed values, except for overflow
1447 if (TREE_OVERFLOW (val1
) || TREE_OVERFLOW (val2
))
1449 if (strict_overflow_p
!= NULL
)
1450 *strict_overflow_p
= true;
1451 if (is_negative_overflow_infinity (val1
))
1452 return is_negative_overflow_infinity (val2
) ? 0 : -1;
1453 else if (is_negative_overflow_infinity (val2
))
1455 else if (is_positive_overflow_infinity (val1
))
1456 return is_positive_overflow_infinity (val2
) ? 0 : 1;
1457 else if (is_positive_overflow_infinity (val2
))
1462 return tree_int_cst_compare (val1
, val2
);
1468 /* First see if VAL1 and VAL2 are not the same. */
1469 if (val1
== val2
|| operand_equal_p (val1
, val2
, 0))
1472 /* If VAL1 is a lower address than VAL2, return -1. */
1473 if (operand_less_p (val1
, val2
) == 1)
1476 /* If VAL1 is a higher address than VAL2, return +1. */
1477 if (operand_less_p (val2
, val1
) == 1)
1480 /* If VAL1 is different than VAL2, return +2.
1481 For integer constants we either have already returned -1 or 1
1482 or they are equivalent. We still might succeed in proving
1483 something about non-trivial operands. */
1484 if (TREE_CODE (val1
) != INTEGER_CST
1485 || TREE_CODE (val2
) != INTEGER_CST
)
1487 t
= fold_binary_to_constant (NE_EXPR
, boolean_type_node
, val1
, val2
);
1488 if (t
&& integer_onep (t
))
1496 /* Compare values like compare_values_warnv, but treat comparisons of
1497 nonconstants which rely on undefined overflow as incomparable. */
1500 compare_values (tree val1
, tree val2
)
1506 ret
= compare_values_warnv (val1
, val2
, &sop
);
1508 && (!is_gimple_min_invariant (val1
) || !is_gimple_min_invariant (val2
)))
1514 /* Return 1 if VAL is inside value range MIN <= VAL <= MAX,
1515 0 if VAL is not inside [MIN, MAX],
1516 -2 if we cannot tell either way.
1518 Benchmark compile/20001226-1.c compilation time after changing this
1522 value_inside_range (tree val
, tree min
, tree max
)
1526 cmp1
= operand_less_p (val
, min
);
1532 cmp2
= operand_less_p (max
, val
);
1540 /* Return true if value ranges VR0 and VR1 have a non-empty
1543 Benchmark compile/20001226-1.c compilation time after changing this
1548 value_ranges_intersect_p (value_range_t
*vr0
, value_range_t
*vr1
)
1550 /* The value ranges do not intersect if the maximum of the first range is
1551 less than the minimum of the second range or vice versa.
1552 When those relations are unknown, we can't do any better. */
1553 if (operand_less_p (vr0
->max
, vr1
->min
) != 0)
1555 if (operand_less_p (vr1
->max
, vr0
->min
) != 0)
1561 /* Return 1 if [MIN, MAX] includes the value zero, 0 if it does not
1562 include the value zero, -2 if we cannot tell. */
1565 range_includes_zero_p (tree min
, tree max
)
1567 tree zero
= build_int_cst (TREE_TYPE (min
), 0);
1568 return value_inside_range (zero
, min
, max
);
1571 /* Return true if *VR is know to only contain nonnegative values. */
1574 value_range_nonnegative_p (value_range_t
*vr
)
1576 /* Testing for VR_ANTI_RANGE is not useful here as any anti-range
1577 which would return a useful value should be encoded as a
1579 if (vr
->type
== VR_RANGE
)
1581 int result
= compare_values (vr
->min
, integer_zero_node
);
1582 return (result
== 0 || result
== 1);
1588 /* If *VR has a value rante that is a single constant value return that,
1589 otherwise return NULL_TREE. */
1592 value_range_constant_singleton (value_range_t
*vr
)
1594 if (vr
->type
== VR_RANGE
1595 && operand_equal_p (vr
->min
, vr
->max
, 0)
1596 && is_gimple_min_invariant (vr
->min
))
1602 /* If OP has a value range with a single constant value return that,
1603 otherwise return NULL_TREE. This returns OP itself if OP is a
1607 op_with_constant_singleton_value_range (tree op
)
1609 if (is_gimple_min_invariant (op
))
1612 if (TREE_CODE (op
) != SSA_NAME
)
1615 return value_range_constant_singleton (get_value_range (op
));
1618 /* Return true if op is in a boolean [0, 1] value-range. */
1621 op_with_boolean_value_range_p (tree op
)
1625 if (TYPE_PRECISION (TREE_TYPE (op
)) == 1)
1628 if (integer_zerop (op
)
1629 || integer_onep (op
))
1632 if (TREE_CODE (op
) != SSA_NAME
)
1635 vr
= get_value_range (op
);
1636 return (vr
->type
== VR_RANGE
1637 && integer_zerop (vr
->min
)
1638 && integer_onep (vr
->max
));
1641 /* Extract value range information from an ASSERT_EXPR EXPR and store
1645 extract_range_from_assert (value_range_t
*vr_p
, tree expr
)
1647 tree var
, cond
, limit
, min
, max
, type
;
1648 value_range_t
*limit_vr
;
1649 enum tree_code cond_code
;
1651 var
= ASSERT_EXPR_VAR (expr
);
1652 cond
= ASSERT_EXPR_COND (expr
);
1654 gcc_assert (COMPARISON_CLASS_P (cond
));
1656 /* Find VAR in the ASSERT_EXPR conditional. */
1657 if (var
== TREE_OPERAND (cond
, 0)
1658 || TREE_CODE (TREE_OPERAND (cond
, 0)) == PLUS_EXPR
1659 || TREE_CODE (TREE_OPERAND (cond
, 0)) == NOP_EXPR
)
1661 /* If the predicate is of the form VAR COMP LIMIT, then we just
1662 take LIMIT from the RHS and use the same comparison code. */
1663 cond_code
= TREE_CODE (cond
);
1664 limit
= TREE_OPERAND (cond
, 1);
1665 cond
= TREE_OPERAND (cond
, 0);
1669 /* If the predicate is of the form LIMIT COMP VAR, then we need
1670 to flip around the comparison code to create the proper range
1672 cond_code
= swap_tree_comparison (TREE_CODE (cond
));
1673 limit
= TREE_OPERAND (cond
, 0);
1674 cond
= TREE_OPERAND (cond
, 1);
1677 limit
= avoid_overflow_infinity (limit
);
1679 type
= TREE_TYPE (var
);
1680 gcc_assert (limit
!= var
);
1682 /* For pointer arithmetic, we only keep track of pointer equality
1684 if (POINTER_TYPE_P (type
) && cond_code
!= NE_EXPR
&& cond_code
!= EQ_EXPR
)
1686 set_value_range_to_varying (vr_p
);
1690 /* If LIMIT is another SSA name and LIMIT has a range of its own,
1691 try to use LIMIT's range to avoid creating symbolic ranges
1693 limit_vr
= (TREE_CODE (limit
) == SSA_NAME
) ? get_value_range (limit
) : NULL
;
1695 /* LIMIT's range is only interesting if it has any useful information. */
1697 && (limit_vr
->type
== VR_UNDEFINED
1698 || limit_vr
->type
== VR_VARYING
1699 || symbolic_range_p (limit_vr
)))
1702 /* Initially, the new range has the same set of equivalences of
1703 VAR's range. This will be revised before returning the final
1704 value. Since assertions may be chained via mutually exclusive
1705 predicates, we will need to trim the set of equivalences before
1707 gcc_assert (vr_p
->equiv
== NULL
);
1708 add_equivalence (&vr_p
->equiv
, var
);
1710 /* Extract a new range based on the asserted comparison for VAR and
1711 LIMIT's value range. Notice that if LIMIT has an anti-range, we
1712 will only use it for equality comparisons (EQ_EXPR). For any
1713 other kind of assertion, we cannot derive a range from LIMIT's
1714 anti-range that can be used to describe the new range. For
1715 instance, ASSERT_EXPR <x_2, x_2 <= b_4>. If b_4 is ~[2, 10],
1716 then b_4 takes on the ranges [-INF, 1] and [11, +INF]. There is
1717 no single range for x_2 that could describe LE_EXPR, so we might
1718 as well build the range [b_4, +INF] for it.
1719 One special case we handle is extracting a range from a
1720 range test encoded as (unsigned)var + CST <= limit. */
1721 if (TREE_CODE (cond
) == NOP_EXPR
1722 || TREE_CODE (cond
) == PLUS_EXPR
)
1724 if (TREE_CODE (cond
) == PLUS_EXPR
)
1726 min
= fold_build1 (NEGATE_EXPR
, TREE_TYPE (TREE_OPERAND (cond
, 1)),
1727 TREE_OPERAND (cond
, 1));
1728 max
= int_const_binop (PLUS_EXPR
, limit
, min
);
1729 cond
= TREE_OPERAND (cond
, 0);
1733 min
= build_int_cst (TREE_TYPE (var
), 0);
1737 /* Make sure to not set TREE_OVERFLOW on the final type
1738 conversion. We are willingly interpreting large positive
1739 unsigned values as negative signed values here. */
1740 min
= force_fit_type (TREE_TYPE (var
), wi::to_widest (min
), 0, false);
1741 max
= force_fit_type (TREE_TYPE (var
), wi::to_widest (max
), 0, false);
1743 /* We can transform a max, min range to an anti-range or
1744 vice-versa. Use set_and_canonicalize_value_range which does
1746 if (cond_code
== LE_EXPR
)
1747 set_and_canonicalize_value_range (vr_p
, VR_RANGE
,
1748 min
, max
, vr_p
->equiv
);
1749 else if (cond_code
== GT_EXPR
)
1750 set_and_canonicalize_value_range (vr_p
, VR_ANTI_RANGE
,
1751 min
, max
, vr_p
->equiv
);
1755 else if (cond_code
== EQ_EXPR
)
1757 enum value_range_type range_type
;
1761 range_type
= limit_vr
->type
;
1762 min
= limit_vr
->min
;
1763 max
= limit_vr
->max
;
1767 range_type
= VR_RANGE
;
1772 set_value_range (vr_p
, range_type
, min
, max
, vr_p
->equiv
);
1774 /* When asserting the equality VAR == LIMIT and LIMIT is another
1775 SSA name, the new range will also inherit the equivalence set
1777 if (TREE_CODE (limit
) == SSA_NAME
)
1778 add_equivalence (&vr_p
->equiv
, limit
);
1780 else if (cond_code
== NE_EXPR
)
1782 /* As described above, when LIMIT's range is an anti-range and
1783 this assertion is an inequality (NE_EXPR), then we cannot
1784 derive anything from the anti-range. For instance, if
1785 LIMIT's range was ~[0, 0], the assertion 'VAR != LIMIT' does
1786 not imply that VAR's range is [0, 0]. So, in the case of
1787 anti-ranges, we just assert the inequality using LIMIT and
1790 If LIMIT_VR is a range, we can only use it to build a new
1791 anti-range if LIMIT_VR is a single-valued range. For
1792 instance, if LIMIT_VR is [0, 1], the predicate
1793 VAR != [0, 1] does not mean that VAR's range is ~[0, 1].
1794 Rather, it means that for value 0 VAR should be ~[0, 0]
1795 and for value 1, VAR should be ~[1, 1]. We cannot
1796 represent these ranges.
1798 The only situation in which we can build a valid
1799 anti-range is when LIMIT_VR is a single-valued range
1800 (i.e., LIMIT_VR->MIN == LIMIT_VR->MAX). In that case,
1801 build the anti-range ~[LIMIT_VR->MIN, LIMIT_VR->MAX]. */
1803 && limit_vr
->type
== VR_RANGE
1804 && compare_values (limit_vr
->min
, limit_vr
->max
) == 0)
1806 min
= limit_vr
->min
;
1807 max
= limit_vr
->max
;
1811 /* In any other case, we cannot use LIMIT's range to build a
1812 valid anti-range. */
1816 /* If MIN and MAX cover the whole range for their type, then
1817 just use the original LIMIT. */
1818 if (INTEGRAL_TYPE_P (type
)
1819 && vrp_val_is_min (min
)
1820 && vrp_val_is_max (max
))
1823 set_and_canonicalize_value_range (vr_p
, VR_ANTI_RANGE
,
1824 min
, max
, vr_p
->equiv
);
1826 else if (cond_code
== LE_EXPR
|| cond_code
== LT_EXPR
)
1828 min
= TYPE_MIN_VALUE (type
);
1830 if (limit_vr
== NULL
|| limit_vr
->type
== VR_ANTI_RANGE
)
1834 /* If LIMIT_VR is of the form [N1, N2], we need to build the
1835 range [MIN, N2] for LE_EXPR and [MIN, N2 - 1] for
1837 max
= limit_vr
->max
;
1840 /* If the maximum value forces us to be out of bounds, simply punt.
1841 It would be pointless to try and do anything more since this
1842 all should be optimized away above us. */
1843 if ((cond_code
== LT_EXPR
1844 && compare_values (max
, min
) == 0)
1845 || is_overflow_infinity (max
))
1846 set_value_range_to_varying (vr_p
);
1849 /* For LT_EXPR, we create the range [MIN, MAX - 1]. */
1850 if (cond_code
== LT_EXPR
)
1852 if (TYPE_PRECISION (TREE_TYPE (max
)) == 1
1853 && !TYPE_UNSIGNED (TREE_TYPE (max
)))
1854 max
= fold_build2 (PLUS_EXPR
, TREE_TYPE (max
), max
,
1855 build_int_cst (TREE_TYPE (max
), -1));
1857 max
= fold_build2 (MINUS_EXPR
, TREE_TYPE (max
), max
,
1858 build_int_cst (TREE_TYPE (max
), 1));
1860 TREE_NO_WARNING (max
) = 1;
1863 set_value_range (vr_p
, VR_RANGE
, min
, max
, vr_p
->equiv
);
1866 else if (cond_code
== GE_EXPR
|| cond_code
== GT_EXPR
)
1868 max
= TYPE_MAX_VALUE (type
);
1870 if (limit_vr
== NULL
|| limit_vr
->type
== VR_ANTI_RANGE
)
1874 /* If LIMIT_VR is of the form [N1, N2], we need to build the
1875 range [N1, MAX] for GE_EXPR and [N1 + 1, MAX] for
1877 min
= limit_vr
->min
;
1880 /* If the minimum value forces us to be out of bounds, simply punt.
1881 It would be pointless to try and do anything more since this
1882 all should be optimized away above us. */
1883 if ((cond_code
== GT_EXPR
1884 && compare_values (min
, max
) == 0)
1885 || is_overflow_infinity (min
))
1886 set_value_range_to_varying (vr_p
);
1889 /* For GT_EXPR, we create the range [MIN + 1, MAX]. */
1890 if (cond_code
== GT_EXPR
)
1892 if (TYPE_PRECISION (TREE_TYPE (min
)) == 1
1893 && !TYPE_UNSIGNED (TREE_TYPE (min
)))
1894 min
= fold_build2 (MINUS_EXPR
, TREE_TYPE (min
), min
,
1895 build_int_cst (TREE_TYPE (min
), -1));
1897 min
= fold_build2 (PLUS_EXPR
, TREE_TYPE (min
), min
,
1898 build_int_cst (TREE_TYPE (min
), 1));
1900 TREE_NO_WARNING (min
) = 1;
1903 set_value_range (vr_p
, VR_RANGE
, min
, max
, vr_p
->equiv
);
1909 /* Finally intersect the new range with what we already know about var. */
1910 vrp_intersect_ranges (vr_p
, get_value_range (var
));
1914 /* Extract range information from SSA name VAR and store it in VR. If
1915 VAR has an interesting range, use it. Otherwise, create the
1916 range [VAR, VAR] and return it. This is useful in situations where
1917 we may have conditionals testing values of VARYING names. For
1924 Even if y_5 is deemed VARYING, we can determine that x_3 > y_5 is
1928 extract_range_from_ssa_name (value_range_t
*vr
, tree var
)
1930 value_range_t
*var_vr
= get_value_range (var
);
1932 if (var_vr
->type
!= VR_VARYING
)
1933 copy_value_range (vr
, var_vr
);
1935 set_value_range (vr
, VR_RANGE
, var
, var
, NULL
);
1937 add_equivalence (&vr
->equiv
, var
);
1941 /* Wrapper around int_const_binop. If the operation overflows and we
1942 are not using wrapping arithmetic, then adjust the result to be
1943 -INF or +INF depending on CODE, VAL1 and VAL2. This can return
1944 NULL_TREE if we need to use an overflow infinity representation but
1945 the type does not support it. */
1948 vrp_int_const_binop (enum tree_code code
, tree val1
, tree val2
)
1952 res
= int_const_binop (code
, val1
, val2
);
1954 /* If we are using unsigned arithmetic, operate symbolically
1955 on -INF and +INF as int_const_binop only handles signed overflow. */
1956 if (TYPE_UNSIGNED (TREE_TYPE (val1
)))
1958 int checkz
= compare_values (res
, val1
);
1959 bool overflow
= false;
1961 /* Ensure that res = val1 [+*] val2 >= val1
1962 or that res = val1 - val2 <= val1. */
1963 if ((code
== PLUS_EXPR
1964 && !(checkz
== 1 || checkz
== 0))
1965 || (code
== MINUS_EXPR
1966 && !(checkz
== 0 || checkz
== -1)))
1970 /* Checking for multiplication overflow is done by dividing the
1971 output of the multiplication by the first input of the
1972 multiplication. If the result of that division operation is
1973 not equal to the second input of the multiplication, then the
1974 multiplication overflowed. */
1975 else if (code
== MULT_EXPR
&& !integer_zerop (val1
))
1977 tree tmp
= int_const_binop (TRUNC_DIV_EXPR
,
1980 int check
= compare_values (tmp
, val2
);
1988 res
= copy_node (res
);
1989 TREE_OVERFLOW (res
) = 1;
1993 else if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (val1
)))
1994 /* If the singed operation wraps then int_const_binop has done
1995 everything we want. */
1997 /* Signed division of -1/0 overflows and by the time it gets here
1998 returns NULL_TREE. */
2001 else if ((TREE_OVERFLOW (res
)
2002 && !TREE_OVERFLOW (val1
)
2003 && !TREE_OVERFLOW (val2
))
2004 || is_overflow_infinity (val1
)
2005 || is_overflow_infinity (val2
))
2007 /* If the operation overflowed but neither VAL1 nor VAL2 are
2008 overflown, return -INF or +INF depending on the operation
2009 and the combination of signs of the operands. */
2010 int sgn1
= tree_int_cst_sgn (val1
);
2011 int sgn2
= tree_int_cst_sgn (val2
);
2013 if (needs_overflow_infinity (TREE_TYPE (res
))
2014 && !supports_overflow_infinity (TREE_TYPE (res
)))
2017 /* We have to punt on adding infinities of different signs,
2018 since we can't tell what the sign of the result should be.
2019 Likewise for subtracting infinities of the same sign. */
2020 if (((code
== PLUS_EXPR
&& sgn1
!= sgn2
)
2021 || (code
== MINUS_EXPR
&& sgn1
== sgn2
))
2022 && is_overflow_infinity (val1
)
2023 && is_overflow_infinity (val2
))
2026 /* Don't try to handle division or shifting of infinities. */
2027 if ((code
== TRUNC_DIV_EXPR
2028 || code
== FLOOR_DIV_EXPR
2029 || code
== CEIL_DIV_EXPR
2030 || code
== EXACT_DIV_EXPR
2031 || code
== ROUND_DIV_EXPR
2032 || code
== RSHIFT_EXPR
)
2033 && (is_overflow_infinity (val1
)
2034 || is_overflow_infinity (val2
)))
2037 /* Notice that we only need to handle the restricted set of
2038 operations handled by extract_range_from_binary_expr.
2039 Among them, only multiplication, addition and subtraction
2040 can yield overflow without overflown operands because we
2041 are working with integral types only... except in the
2042 case VAL1 = -INF and VAL2 = -1 which overflows to +INF
2043 for division too. */
2045 /* For multiplication, the sign of the overflow is given
2046 by the comparison of the signs of the operands. */
2047 if ((code
== MULT_EXPR
&& sgn1
== sgn2
)
2048 /* For addition, the operands must be of the same sign
2049 to yield an overflow. Its sign is therefore that
2050 of one of the operands, for example the first. For
2051 infinite operands X + -INF is negative, not positive. */
2052 || (code
== PLUS_EXPR
2054 ? !is_negative_overflow_infinity (val2
)
2055 : is_positive_overflow_infinity (val2
)))
2056 /* For subtraction, non-infinite operands must be of
2057 different signs to yield an overflow. Its sign is
2058 therefore that of the first operand or the opposite of
2059 that of the second operand. A first operand of 0 counts
2060 as positive here, for the corner case 0 - (-INF), which
2061 overflows, but must yield +INF. For infinite operands 0
2062 - INF is negative, not positive. */
2063 || (code
== MINUS_EXPR
2065 ? !is_positive_overflow_infinity (val2
)
2066 : is_negative_overflow_infinity (val2
)))
2067 /* We only get in here with positive shift count, so the
2068 overflow direction is the same as the sign of val1.
2069 Actually rshift does not overflow at all, but we only
2070 handle the case of shifting overflowed -INF and +INF. */
2071 || (code
== RSHIFT_EXPR
2073 /* For division, the only case is -INF / -1 = +INF. */
2074 || code
== TRUNC_DIV_EXPR
2075 || code
== FLOOR_DIV_EXPR
2076 || code
== CEIL_DIV_EXPR
2077 || code
== EXACT_DIV_EXPR
2078 || code
== ROUND_DIV_EXPR
)
2079 return (needs_overflow_infinity (TREE_TYPE (res
))
2080 ? positive_overflow_infinity (TREE_TYPE (res
))
2081 : TYPE_MAX_VALUE (TREE_TYPE (res
)));
2083 return (needs_overflow_infinity (TREE_TYPE (res
))
2084 ? negative_overflow_infinity (TREE_TYPE (res
))
2085 : TYPE_MIN_VALUE (TREE_TYPE (res
)));
2092 /* For range VR compute two wide_int bitmasks. In *MAY_BE_NONZERO
2093 bitmask if some bit is unset, it means for all numbers in the range
2094 the bit is 0, otherwise it might be 0 or 1. In *MUST_BE_NONZERO
2095 bitmask if some bit is set, it means for all numbers in the range
2096 the bit is 1, otherwise it might be 0 or 1. */
2099 zero_nonzero_bits_from_vr (const tree expr_type
,
2101 wide_int
*may_be_nonzero
,
2102 wide_int
*must_be_nonzero
)
2104 *may_be_nonzero
= wi::minus_one (TYPE_PRECISION (expr_type
));
2105 *must_be_nonzero
= wi::zero (TYPE_PRECISION (expr_type
));
2106 if (!range_int_cst_p (vr
)
2107 || is_overflow_infinity (vr
->min
)
2108 || is_overflow_infinity (vr
->max
))
2111 if (range_int_cst_singleton_p (vr
))
2113 *may_be_nonzero
= vr
->min
;
2114 *must_be_nonzero
= *may_be_nonzero
;
2116 else if (tree_int_cst_sgn (vr
->min
) >= 0
2117 || tree_int_cst_sgn (vr
->max
) < 0)
2119 wide_int xor_mask
= wi::bit_xor (vr
->min
, vr
->max
);
2120 *may_be_nonzero
= wi::bit_or (vr
->min
, vr
->max
);
2121 *must_be_nonzero
= wi::bit_and (vr
->min
, vr
->max
);
2124 wide_int mask
= wi::mask (wi::floor_log2 (xor_mask
), false,
2125 may_be_nonzero
->get_precision ());
2126 *may_be_nonzero
= *may_be_nonzero
| mask
;
2127 *must_be_nonzero
= must_be_nonzero
->and_not (mask
);
2134 /* Create two value-ranges in *VR0 and *VR1 from the anti-range *AR
2135 so that *VR0 U *VR1 == *AR. Returns true if that is possible,
2136 false otherwise. If *AR can be represented with a single range
2137 *VR1 will be VR_UNDEFINED. */
2140 ranges_from_anti_range (value_range_t
*ar
,
2141 value_range_t
*vr0
, value_range_t
*vr1
)
2143 tree type
= TREE_TYPE (ar
->min
);
2145 vr0
->type
= VR_UNDEFINED
;
2146 vr1
->type
= VR_UNDEFINED
;
2148 if (ar
->type
!= VR_ANTI_RANGE
2149 || TREE_CODE (ar
->min
) != INTEGER_CST
2150 || TREE_CODE (ar
->max
) != INTEGER_CST
2151 || !vrp_val_min (type
)
2152 || !vrp_val_max (type
))
2155 if (!vrp_val_is_min (ar
->min
))
2157 vr0
->type
= VR_RANGE
;
2158 vr0
->min
= vrp_val_min (type
);
2159 vr0
->max
= wide_int_to_tree (type
, wi::sub (ar
->min
, 1));
2161 if (!vrp_val_is_max (ar
->max
))
2163 vr1
->type
= VR_RANGE
;
2164 vr1
->min
= wide_int_to_tree (type
, wi::add (ar
->max
, 1));
2165 vr1
->max
= vrp_val_max (type
);
2167 if (vr0
->type
== VR_UNDEFINED
)
2170 vr1
->type
= VR_UNDEFINED
;
2173 return vr0
->type
!= VR_UNDEFINED
;
2176 /* Helper to extract a value-range *VR for a multiplicative operation
2180 extract_range_from_multiplicative_op_1 (value_range_t
*vr
,
2181 enum tree_code code
,
2182 value_range_t
*vr0
, value_range_t
*vr1
)
2184 enum value_range_type type
;
2191 /* Multiplications, divisions and shifts are a bit tricky to handle,
2192 depending on the mix of signs we have in the two ranges, we
2193 need to operate on different values to get the minimum and
2194 maximum values for the new range. One approach is to figure
2195 out all the variations of range combinations and do the
2198 However, this involves several calls to compare_values and it
2199 is pretty convoluted. It's simpler to do the 4 operations
2200 (MIN0 OP MIN1, MIN0 OP MAX1, MAX0 OP MIN1 and MAX0 OP MAX0 OP
2201 MAX1) and then figure the smallest and largest values to form
2203 gcc_assert (code
== MULT_EXPR
2204 || code
== TRUNC_DIV_EXPR
2205 || code
== FLOOR_DIV_EXPR
2206 || code
== CEIL_DIV_EXPR
2207 || code
== EXACT_DIV_EXPR
2208 || code
== ROUND_DIV_EXPR
2209 || code
== RSHIFT_EXPR
2210 || code
== LSHIFT_EXPR
);
2211 gcc_assert ((vr0
->type
== VR_RANGE
2212 || (code
== MULT_EXPR
&& vr0
->type
== VR_ANTI_RANGE
))
2213 && vr0
->type
== vr1
->type
);
2217 /* Compute the 4 cross operations. */
2219 val
[0] = vrp_int_const_binop (code
, vr0
->min
, vr1
->min
);
2220 if (val
[0] == NULL_TREE
)
2223 if (vr1
->max
== vr1
->min
)
2227 val
[1] = vrp_int_const_binop (code
, vr0
->min
, vr1
->max
);
2228 if (val
[1] == NULL_TREE
)
2232 if (vr0
->max
== vr0
->min
)
2236 val
[2] = vrp_int_const_binop (code
, vr0
->max
, vr1
->min
);
2237 if (val
[2] == NULL_TREE
)
2241 if (vr0
->min
== vr0
->max
|| vr1
->min
== vr1
->max
)
2245 val
[3] = vrp_int_const_binop (code
, vr0
->max
, vr1
->max
);
2246 if (val
[3] == NULL_TREE
)
2252 set_value_range_to_varying (vr
);
2256 /* Set MIN to the minimum of VAL[i] and MAX to the maximum
2260 for (i
= 1; i
< 4; i
++)
2262 if (!is_gimple_min_invariant (min
)
2263 || (TREE_OVERFLOW (min
) && !is_overflow_infinity (min
))
2264 || !is_gimple_min_invariant (max
)
2265 || (TREE_OVERFLOW (max
) && !is_overflow_infinity (max
)))
2270 if (!is_gimple_min_invariant (val
[i
])
2271 || (TREE_OVERFLOW (val
[i
])
2272 && !is_overflow_infinity (val
[i
])))
2274 /* If we found an overflowed value, set MIN and MAX
2275 to it so that we set the resulting range to
2281 if (compare_values (val
[i
], min
) == -1)
2284 if (compare_values (val
[i
], max
) == 1)
2289 /* If either MIN or MAX overflowed, then set the resulting range to
2290 VARYING. But we do accept an overflow infinity
2292 if (min
== NULL_TREE
2293 || !is_gimple_min_invariant (min
)
2294 || (TREE_OVERFLOW (min
) && !is_overflow_infinity (min
))
2296 || !is_gimple_min_invariant (max
)
2297 || (TREE_OVERFLOW (max
) && !is_overflow_infinity (max
)))
2299 set_value_range_to_varying (vr
);
2305 2) [-INF, +-INF(OVF)]
2306 3) [+-INF(OVF), +INF]
2307 4) [+-INF(OVF), +-INF(OVF)]
2308 We learn nothing when we have INF and INF(OVF) on both sides.
2309 Note that we do accept [-INF, -INF] and [+INF, +INF] without
2311 if ((vrp_val_is_min (min
) || is_overflow_infinity (min
))
2312 && (vrp_val_is_max (max
) || is_overflow_infinity (max
)))
2314 set_value_range_to_varying (vr
);
2318 cmp
= compare_values (min
, max
);
2319 if (cmp
== -2 || cmp
== 1)
2321 /* If the new range has its limits swapped around (MIN > MAX),
2322 then the operation caused one of them to wrap around, mark
2323 the new range VARYING. */
2324 set_value_range_to_varying (vr
);
2327 set_value_range (vr
, type
, min
, max
, NULL
);
2330 /* Extract range information from a binary operation CODE based on
2331 the ranges of each of its operands *VR0 and *VR1 with resulting
2332 type EXPR_TYPE. The resulting range is stored in *VR. */
2335 extract_range_from_binary_expr_1 (value_range_t
*vr
,
2336 enum tree_code code
, tree expr_type
,
2337 value_range_t
*vr0_
, value_range_t
*vr1_
)
2339 value_range_t vr0
= *vr0_
, vr1
= *vr1_
;
2340 value_range_t vrtem0
= VR_INITIALIZER
, vrtem1
= VR_INITIALIZER
;
2341 enum value_range_type type
;
2342 tree min
= NULL_TREE
, max
= NULL_TREE
;
2345 if (!INTEGRAL_TYPE_P (expr_type
)
2346 && !POINTER_TYPE_P (expr_type
))
2348 set_value_range_to_varying (vr
);
2352 /* Not all binary expressions can be applied to ranges in a
2353 meaningful way. Handle only arithmetic operations. */
2354 if (code
!= PLUS_EXPR
2355 && code
!= MINUS_EXPR
2356 && code
!= POINTER_PLUS_EXPR
2357 && code
!= MULT_EXPR
2358 && code
!= TRUNC_DIV_EXPR
2359 && code
!= FLOOR_DIV_EXPR
2360 && code
!= CEIL_DIV_EXPR
2361 && code
!= EXACT_DIV_EXPR
2362 && code
!= ROUND_DIV_EXPR
2363 && code
!= TRUNC_MOD_EXPR
2364 && code
!= RSHIFT_EXPR
2365 && code
!= LSHIFT_EXPR
2368 && code
!= BIT_AND_EXPR
2369 && code
!= BIT_IOR_EXPR
2370 && code
!= BIT_XOR_EXPR
)
2372 set_value_range_to_varying (vr
);
2376 /* If both ranges are UNDEFINED, so is the result. */
2377 if (vr0
.type
== VR_UNDEFINED
&& vr1
.type
== VR_UNDEFINED
)
2379 set_value_range_to_undefined (vr
);
2382 /* If one of the ranges is UNDEFINED drop it to VARYING for the following
2383 code. At some point we may want to special-case operations that
2384 have UNDEFINED result for all or some value-ranges of the not UNDEFINED
2386 else if (vr0
.type
== VR_UNDEFINED
)
2387 set_value_range_to_varying (&vr0
);
2388 else if (vr1
.type
== VR_UNDEFINED
)
2389 set_value_range_to_varying (&vr1
);
2391 /* Now canonicalize anti-ranges to ranges when they are not symbolic
2392 and express ~[] op X as ([]' op X) U ([]'' op X). */
2393 if (vr0
.type
== VR_ANTI_RANGE
2394 && ranges_from_anti_range (&vr0
, &vrtem0
, &vrtem1
))
2396 extract_range_from_binary_expr_1 (vr
, code
, expr_type
, &vrtem0
, vr1_
);
2397 if (vrtem1
.type
!= VR_UNDEFINED
)
2399 value_range_t vrres
= VR_INITIALIZER
;
2400 extract_range_from_binary_expr_1 (&vrres
, code
, expr_type
,
2402 vrp_meet (vr
, &vrres
);
2406 /* Likewise for X op ~[]. */
2407 if (vr1
.type
== VR_ANTI_RANGE
2408 && ranges_from_anti_range (&vr1
, &vrtem0
, &vrtem1
))
2410 extract_range_from_binary_expr_1 (vr
, code
, expr_type
, vr0_
, &vrtem0
);
2411 if (vrtem1
.type
!= VR_UNDEFINED
)
2413 value_range_t vrres
= VR_INITIALIZER
;
2414 extract_range_from_binary_expr_1 (&vrres
, code
, expr_type
,
2416 vrp_meet (vr
, &vrres
);
2421 /* The type of the resulting value range defaults to VR0.TYPE. */
2424 /* Refuse to operate on VARYING ranges, ranges of different kinds
2425 and symbolic ranges. As an exception, we allow BIT_{AND,IOR}
2426 because we may be able to derive a useful range even if one of
2427 the operands is VR_VARYING or symbolic range. Similarly for
2428 divisions, MIN/MAX and PLUS/MINUS.
2430 TODO, we may be able to derive anti-ranges in some cases. */
2431 if (code
!= BIT_AND_EXPR
2432 && code
!= BIT_IOR_EXPR
2433 && code
!= TRUNC_DIV_EXPR
2434 && code
!= FLOOR_DIV_EXPR
2435 && code
!= CEIL_DIV_EXPR
2436 && code
!= EXACT_DIV_EXPR
2437 && code
!= ROUND_DIV_EXPR
2438 && code
!= TRUNC_MOD_EXPR
2441 && code
!= PLUS_EXPR
2442 && code
!= MINUS_EXPR
2443 && code
!= RSHIFT_EXPR
2444 && (vr0
.type
== VR_VARYING
2445 || vr1
.type
== VR_VARYING
2446 || vr0
.type
!= vr1
.type
2447 || symbolic_range_p (&vr0
)
2448 || symbolic_range_p (&vr1
)))
2450 set_value_range_to_varying (vr
);
2454 /* Now evaluate the expression to determine the new range. */
2455 if (POINTER_TYPE_P (expr_type
))
2457 if (code
== MIN_EXPR
|| code
== MAX_EXPR
)
2459 /* For MIN/MAX expressions with pointers, we only care about
2460 nullness, if both are non null, then the result is nonnull.
2461 If both are null, then the result is null. Otherwise they
2463 if (range_is_nonnull (&vr0
) && range_is_nonnull (&vr1
))
2464 set_value_range_to_nonnull (vr
, expr_type
);
2465 else if (range_is_null (&vr0
) && range_is_null (&vr1
))
2466 set_value_range_to_null (vr
, expr_type
);
2468 set_value_range_to_varying (vr
);
2470 else if (code
== POINTER_PLUS_EXPR
)
2472 /* For pointer types, we are really only interested in asserting
2473 whether the expression evaluates to non-NULL. */
2474 if (range_is_nonnull (&vr0
) || range_is_nonnull (&vr1
))
2475 set_value_range_to_nonnull (vr
, expr_type
);
2476 else if (range_is_null (&vr0
) && range_is_null (&vr1
))
2477 set_value_range_to_null (vr
, expr_type
);
2479 set_value_range_to_varying (vr
);
2481 else if (code
== BIT_AND_EXPR
)
2483 /* For pointer types, we are really only interested in asserting
2484 whether the expression evaluates to non-NULL. */
2485 if (range_is_nonnull (&vr0
) && range_is_nonnull (&vr1
))
2486 set_value_range_to_nonnull (vr
, expr_type
);
2487 else if (range_is_null (&vr0
) || range_is_null (&vr1
))
2488 set_value_range_to_null (vr
, expr_type
);
2490 set_value_range_to_varying (vr
);
2493 set_value_range_to_varying (vr
);
2498 /* For integer ranges, apply the operation to each end of the
2499 range and see what we end up with. */
2500 if (code
== PLUS_EXPR
|| code
== MINUS_EXPR
)
2502 const bool minus_p
= (code
== MINUS_EXPR
);
2503 tree min_op0
= vr0
.min
;
2504 tree min_op1
= minus_p
? vr1
.max
: vr1
.min
;
2505 tree max_op0
= vr0
.max
;
2506 tree max_op1
= minus_p
? vr1
.min
: vr1
.max
;
2507 tree sym_min_op0
= NULL_TREE
;
2508 tree sym_min_op1
= NULL_TREE
;
2509 tree sym_max_op0
= NULL_TREE
;
2510 tree sym_max_op1
= NULL_TREE
;
2511 bool neg_min_op0
, neg_min_op1
, neg_max_op0
, neg_max_op1
;
2513 /* If we have a PLUS or MINUS with two VR_RANGEs, either constant or
2514 single-symbolic ranges, try to compute the precise resulting range,
2515 but only if we know that this resulting range will also be constant
2516 or single-symbolic. */
2517 if (vr0
.type
== VR_RANGE
&& vr1
.type
== VR_RANGE
2518 && (TREE_CODE (min_op0
) == INTEGER_CST
2520 = get_single_symbol (min_op0
, &neg_min_op0
, &min_op0
)))
2521 && (TREE_CODE (min_op1
) == INTEGER_CST
2523 = get_single_symbol (min_op1
, &neg_min_op1
, &min_op1
)))
2524 && (!(sym_min_op0
&& sym_min_op1
)
2525 || (sym_min_op0
== sym_min_op1
2526 && neg_min_op0
== (minus_p
? neg_min_op1
: !neg_min_op1
)))
2527 && (TREE_CODE (max_op0
) == INTEGER_CST
2529 = get_single_symbol (max_op0
, &neg_max_op0
, &max_op0
)))
2530 && (TREE_CODE (max_op1
) == INTEGER_CST
2532 = get_single_symbol (max_op1
, &neg_max_op1
, &max_op1
)))
2533 && (!(sym_max_op0
&& sym_max_op1
)
2534 || (sym_max_op0
== sym_max_op1
2535 && neg_max_op0
== (minus_p
? neg_max_op1
: !neg_max_op1
))))
2537 const signop sgn
= TYPE_SIGN (expr_type
);
2538 const unsigned int prec
= TYPE_PRECISION (expr_type
);
2539 wide_int type_min
, type_max
, wmin
, wmax
;
2543 /* Get the lower and upper bounds of the type. */
2544 if (TYPE_OVERFLOW_WRAPS (expr_type
))
2546 type_min
= wi::min_value (prec
, sgn
);
2547 type_max
= wi::max_value (prec
, sgn
);
2551 type_min
= vrp_val_min (expr_type
);
2552 type_max
= vrp_val_max (expr_type
);
2555 /* Combine the lower bounds, if any. */
2556 if (min_op0
&& min_op1
)
2560 wmin
= wi::sub (min_op0
, min_op1
);
2562 /* Check for overflow. */
2563 if (wi::cmp (0, min_op1
, sgn
)
2564 != wi::cmp (wmin
, min_op0
, sgn
))
2565 min_ovf
= wi::cmp (min_op0
, min_op1
, sgn
);
2569 wmin
= wi::add (min_op0
, min_op1
);
2571 /* Check for overflow. */
2572 if (wi::cmp (min_op1
, 0, sgn
)
2573 != wi::cmp (wmin
, min_op0
, sgn
))
2574 min_ovf
= wi::cmp (min_op0
, wmin
, sgn
);
2580 wmin
= minus_p
? wi::neg (min_op1
) : min_op1
;
2582 wmin
= wi::shwi (0, prec
);
2584 /* Combine the upper bounds, if any. */
2585 if (max_op0
&& max_op1
)
2589 wmax
= wi::sub (max_op0
, max_op1
);
2591 /* Check for overflow. */
2592 if (wi::cmp (0, max_op1
, sgn
)
2593 != wi::cmp (wmax
, max_op0
, sgn
))
2594 max_ovf
= wi::cmp (max_op0
, max_op1
, sgn
);
2598 wmax
= wi::add (max_op0
, max_op1
);
2600 if (wi::cmp (max_op1
, 0, sgn
)
2601 != wi::cmp (wmax
, max_op0
, sgn
))
2602 max_ovf
= wi::cmp (max_op0
, wmax
, sgn
);
2608 wmax
= minus_p
? wi::neg (max_op1
) : max_op1
;
2610 wmax
= wi::shwi (0, prec
);
2612 /* Check for type overflow. */
2615 if (wi::cmp (wmin
, type_min
, sgn
) == -1)
2617 else if (wi::cmp (wmin
, type_max
, sgn
) == 1)
2622 if (wi::cmp (wmax
, type_min
, sgn
) == -1)
2624 else if (wi::cmp (wmax
, type_max
, sgn
) == 1)
2628 /* If we have overflow for the constant part and the resulting
2629 range will be symbolic, drop to VR_VARYING. */
2630 if ((min_ovf
&& sym_min_op0
!= sym_min_op1
)
2631 || (max_ovf
&& sym_max_op0
!= sym_max_op1
))
2633 set_value_range_to_varying (vr
);
2637 if (TYPE_OVERFLOW_WRAPS (expr_type
))
2639 /* If overflow wraps, truncate the values and adjust the
2640 range kind and bounds appropriately. */
2641 wide_int tmin
= wide_int::from (wmin
, prec
, sgn
);
2642 wide_int tmax
= wide_int::from (wmax
, prec
, sgn
);
2643 if (min_ovf
== max_ovf
)
2645 /* No overflow or both overflow or underflow. The
2646 range kind stays VR_RANGE. */
2647 min
= wide_int_to_tree (expr_type
, tmin
);
2648 max
= wide_int_to_tree (expr_type
, tmax
);
2650 else if (min_ovf
== -1 && max_ovf
== 1)
2652 /* Underflow and overflow, drop to VR_VARYING. */
2653 set_value_range_to_varying (vr
);
2658 /* Min underflow or max overflow. The range kind
2659 changes to VR_ANTI_RANGE. */
2660 bool covers
= false;
2661 wide_int tem
= tmin
;
2662 gcc_assert ((min_ovf
== -1 && max_ovf
== 0)
2663 || (max_ovf
== 1 && min_ovf
== 0));
2664 type
= VR_ANTI_RANGE
;
2666 if (wi::cmp (tmin
, tmax
, sgn
) < 0)
2669 if (wi::cmp (tmax
, tem
, sgn
) > 0)
2671 /* If the anti-range would cover nothing, drop to varying.
2672 Likewise if the anti-range bounds are outside of the
2674 if (covers
|| wi::cmp (tmin
, tmax
, sgn
) > 0)
2676 set_value_range_to_varying (vr
);
2679 min
= wide_int_to_tree (expr_type
, tmin
);
2680 max
= wide_int_to_tree (expr_type
, tmax
);
2685 /* If overflow does not wrap, saturate to the types min/max
2689 if (needs_overflow_infinity (expr_type
)
2690 && supports_overflow_infinity (expr_type
))
2691 min
= negative_overflow_infinity (expr_type
);
2693 min
= wide_int_to_tree (expr_type
, type_min
);
2695 else if (min_ovf
== 1)
2697 if (needs_overflow_infinity (expr_type
)
2698 && supports_overflow_infinity (expr_type
))
2699 min
= positive_overflow_infinity (expr_type
);
2701 min
= wide_int_to_tree (expr_type
, type_max
);
2704 min
= wide_int_to_tree (expr_type
, wmin
);
2708 if (needs_overflow_infinity (expr_type
)
2709 && supports_overflow_infinity (expr_type
))
2710 max
= negative_overflow_infinity (expr_type
);
2712 max
= wide_int_to_tree (expr_type
, type_min
);
2714 else if (max_ovf
== 1)
2716 if (needs_overflow_infinity (expr_type
)
2717 && supports_overflow_infinity (expr_type
))
2718 max
= positive_overflow_infinity (expr_type
);
2720 max
= wide_int_to_tree (expr_type
, type_max
);
2723 max
= wide_int_to_tree (expr_type
, wmax
);
2726 if (needs_overflow_infinity (expr_type
)
2727 && supports_overflow_infinity (expr_type
))
2729 if ((min_op0
&& is_negative_overflow_infinity (min_op0
))
2732 ? is_positive_overflow_infinity (min_op1
)
2733 : is_negative_overflow_infinity (min_op1
))))
2734 min
= negative_overflow_infinity (expr_type
);
2735 if ((max_op0
&& is_positive_overflow_infinity (max_op0
))
2738 ? is_negative_overflow_infinity (max_op1
)
2739 : is_positive_overflow_infinity (max_op1
))))
2740 max
= positive_overflow_infinity (expr_type
);
2743 /* If the result lower bound is constant, we're done;
2744 otherwise, build the symbolic lower bound. */
2745 if (sym_min_op0
== sym_min_op1
)
2747 else if (sym_min_op0
)
2748 min
= build_symbolic_expr (expr_type
, sym_min_op0
,
2750 else if (sym_min_op1
)
2751 min
= build_symbolic_expr (expr_type
, sym_min_op1
,
2752 neg_min_op1
^ minus_p
, min
);
2754 /* Likewise for the upper bound. */
2755 if (sym_max_op0
== sym_max_op1
)
2757 else if (sym_max_op0
)
2758 max
= build_symbolic_expr (expr_type
, sym_max_op0
,
2760 else if (sym_max_op1
)
2761 max
= build_symbolic_expr (expr_type
, sym_max_op1
,
2762 neg_max_op1
^ minus_p
, max
);
2766 /* For other cases, for example if we have a PLUS_EXPR with two
2767 VR_ANTI_RANGEs, drop to VR_VARYING. It would take more effort
2768 to compute a precise range for such a case.
2769 ??? General even mixed range kind operations can be expressed
2770 by for example transforming ~[3, 5] + [1, 2] to range-only
2771 operations and a union primitive:
2772 [-INF, 2] + [1, 2] U [5, +INF] + [1, 2]
2773 [-INF+1, 4] U [6, +INF(OVF)]
2774 though usually the union is not exactly representable with
2775 a single range or anti-range as the above is
2776 [-INF+1, +INF(OVF)] intersected with ~[5, 5]
2777 but one could use a scheme similar to equivalences for this. */
2778 set_value_range_to_varying (vr
);
2782 else if (code
== MIN_EXPR
2783 || code
== MAX_EXPR
)
2785 if (vr0
.type
== VR_RANGE
2786 && !symbolic_range_p (&vr0
))
2789 if (vr1
.type
== VR_RANGE
2790 && !symbolic_range_p (&vr1
))
2792 /* For operations that make the resulting range directly
2793 proportional to the original ranges, apply the operation to
2794 the same end of each range. */
2795 min
= vrp_int_const_binop (code
, vr0
.min
, vr1
.min
);
2796 max
= vrp_int_const_binop (code
, vr0
.max
, vr1
.max
);
2798 else if (code
== MIN_EXPR
)
2800 min
= vrp_val_min (expr_type
);
2803 else if (code
== MAX_EXPR
)
2806 max
= vrp_val_max (expr_type
);
2809 else if (vr1
.type
== VR_RANGE
2810 && !symbolic_range_p (&vr1
))
2813 if (code
== MIN_EXPR
)
2815 min
= vrp_val_min (expr_type
);
2818 else if (code
== MAX_EXPR
)
2821 max
= vrp_val_max (expr_type
);
2826 set_value_range_to_varying (vr
);
2830 else if (code
== MULT_EXPR
)
2832 /* Fancy code so that with unsigned, [-3,-1]*[-3,-1] does not
2833 drop to varying. This test requires 2*prec bits if both
2834 operands are signed and 2*prec + 2 bits if either is not. */
2836 signop sign
= TYPE_SIGN (expr_type
);
2837 unsigned int prec
= TYPE_PRECISION (expr_type
);
2839 if (range_int_cst_p (&vr0
)
2840 && range_int_cst_p (&vr1
)
2841 && TYPE_OVERFLOW_WRAPS (expr_type
))
2843 typedef FIXED_WIDE_INT (WIDE_INT_MAX_PRECISION
* 2) vrp_int
;
2844 typedef generic_wide_int
2845 <wi::extended_tree
<WIDE_INT_MAX_PRECISION
* 2> > vrp_int_cst
;
2846 vrp_int sizem1
= wi::mask
<vrp_int
> (prec
, false);
2847 vrp_int size
= sizem1
+ 1;
2849 /* Extend the values using the sign of the result to PREC2.
2850 From here on out, everthing is just signed math no matter
2851 what the input types were. */
2852 vrp_int min0
= vrp_int_cst (vr0
.min
);
2853 vrp_int max0
= vrp_int_cst (vr0
.max
);
2854 vrp_int min1
= vrp_int_cst (vr1
.min
);
2855 vrp_int max1
= vrp_int_cst (vr1
.max
);
2856 /* Canonicalize the intervals. */
2857 if (sign
== UNSIGNED
)
2859 if (wi::ltu_p (size
, min0
+ max0
))
2865 if (wi::ltu_p (size
, min1
+ max1
))
2872 vrp_int prod0
= min0
* min1
;
2873 vrp_int prod1
= min0
* max1
;
2874 vrp_int prod2
= max0
* min1
;
2875 vrp_int prod3
= max0
* max1
;
2877 /* Sort the 4 products so that min is in prod0 and max is in
2879 /* min0min1 > max0max1 */
2880 if (wi::gts_p (prod0
, prod3
))
2882 vrp_int tmp
= prod3
;
2887 /* min0max1 > max0min1 */
2888 if (wi::gts_p (prod1
, prod2
))
2890 vrp_int tmp
= prod2
;
2895 if (wi::gts_p (prod0
, prod1
))
2897 vrp_int tmp
= prod1
;
2902 if (wi::gts_p (prod2
, prod3
))
2904 vrp_int tmp
= prod3
;
2909 /* diff = max - min. */
2910 prod2
= prod3
- prod0
;
2911 if (wi::geu_p (prod2
, sizem1
))
2913 /* the range covers all values. */
2914 set_value_range_to_varying (vr
);
2918 /* The following should handle the wrapping and selecting
2919 VR_ANTI_RANGE for us. */
2920 min
= wide_int_to_tree (expr_type
, prod0
);
2921 max
= wide_int_to_tree (expr_type
, prod3
);
2922 set_and_canonicalize_value_range (vr
, VR_RANGE
, min
, max
, NULL
);
2926 /* If we have an unsigned MULT_EXPR with two VR_ANTI_RANGEs,
2927 drop to VR_VARYING. It would take more effort to compute a
2928 precise range for such a case. For example, if we have
2929 op0 == 65536 and op1 == 65536 with their ranges both being
2930 ~[0,0] on a 32-bit machine, we would have op0 * op1 == 0, so
2931 we cannot claim that the product is in ~[0,0]. Note that we
2932 are guaranteed to have vr0.type == vr1.type at this
2934 if (vr0
.type
== VR_ANTI_RANGE
2935 && !TYPE_OVERFLOW_UNDEFINED (expr_type
))
2937 set_value_range_to_varying (vr
);
2941 extract_range_from_multiplicative_op_1 (vr
, code
, &vr0
, &vr1
);
2944 else if (code
== RSHIFT_EXPR
2945 || code
== LSHIFT_EXPR
)
2947 /* If we have a RSHIFT_EXPR with any shift values outside [0..prec-1],
2948 then drop to VR_VARYING. Outside of this range we get undefined
2949 behavior from the shift operation. We cannot even trust
2950 SHIFT_COUNT_TRUNCATED at this stage, because that applies to rtl
2951 shifts, and the operation at the tree level may be widened. */
2952 if (range_int_cst_p (&vr1
)
2953 && compare_tree_int (vr1
.min
, 0) >= 0
2954 && compare_tree_int (vr1
.max
, TYPE_PRECISION (expr_type
)) == -1)
2956 if (code
== RSHIFT_EXPR
)
2958 /* Even if vr0 is VARYING or otherwise not usable, we can derive
2959 useful ranges just from the shift count. E.g.
2960 x >> 63 for signed 64-bit x is always [-1, 0]. */
2961 if (vr0
.type
!= VR_RANGE
|| symbolic_range_p (&vr0
))
2963 vr0
.type
= type
= VR_RANGE
;
2964 vr0
.min
= vrp_val_min (expr_type
);
2965 vr0
.max
= vrp_val_max (expr_type
);
2967 extract_range_from_multiplicative_op_1 (vr
, code
, &vr0
, &vr1
);
2970 /* We can map lshifts by constants to MULT_EXPR handling. */
2971 else if (code
== LSHIFT_EXPR
2972 && range_int_cst_singleton_p (&vr1
))
2974 bool saved_flag_wrapv
;
2975 value_range_t vr1p
= VR_INITIALIZER
;
2976 vr1p
.type
= VR_RANGE
;
2977 vr1p
.min
= (wide_int_to_tree
2979 wi::set_bit_in_zero (tree_to_shwi (vr1
.min
),
2980 TYPE_PRECISION (expr_type
))));
2981 vr1p
.max
= vr1p
.min
;
2982 /* We have to use a wrapping multiply though as signed overflow
2983 on lshifts is implementation defined in C89. */
2984 saved_flag_wrapv
= flag_wrapv
;
2986 extract_range_from_binary_expr_1 (vr
, MULT_EXPR
, expr_type
,
2988 flag_wrapv
= saved_flag_wrapv
;
2991 else if (code
== LSHIFT_EXPR
2992 && range_int_cst_p (&vr0
))
2994 int prec
= TYPE_PRECISION (expr_type
);
2995 int overflow_pos
= prec
;
2997 wide_int low_bound
, high_bound
;
2998 bool uns
= TYPE_UNSIGNED (expr_type
);
2999 bool in_bounds
= false;
3004 bound_shift
= overflow_pos
- tree_to_shwi (vr1
.max
);
3005 /* If bound_shift == HOST_BITS_PER_WIDE_INT, the llshift can
3006 overflow. However, for that to happen, vr1.max needs to be
3007 zero, which means vr1 is a singleton range of zero, which
3008 means it should be handled by the previous LSHIFT_EXPR
3010 wide_int bound
= wi::set_bit_in_zero (bound_shift
, prec
);
3011 wide_int complement
= ~(bound
- 1);
3016 high_bound
= complement
;
3017 if (wi::ltu_p (vr0
.max
, low_bound
))
3019 /* [5, 6] << [1, 2] == [10, 24]. */
3020 /* We're shifting out only zeroes, the value increases
3024 else if (wi::ltu_p (high_bound
, vr0
.min
))
3026 /* [0xffffff00, 0xffffffff] << [1, 2]
3027 == [0xfffffc00, 0xfffffffe]. */
3028 /* We're shifting out only ones, the value decreases
3035 /* [-1, 1] << [1, 2] == [-4, 4]. */
3036 low_bound
= complement
;
3038 if (wi::lts_p (vr0
.max
, high_bound
)
3039 && wi::lts_p (low_bound
, vr0
.min
))
3041 /* For non-negative numbers, we're shifting out only
3042 zeroes, the value increases monotonically.
3043 For negative numbers, we're shifting out only ones, the
3044 value decreases monotomically. */
3051 extract_range_from_multiplicative_op_1 (vr
, code
, &vr0
, &vr1
);
3056 set_value_range_to_varying (vr
);
3059 else if (code
== TRUNC_DIV_EXPR
3060 || code
== FLOOR_DIV_EXPR
3061 || code
== CEIL_DIV_EXPR
3062 || code
== EXACT_DIV_EXPR
3063 || code
== ROUND_DIV_EXPR
)
3065 if (vr0
.type
!= VR_RANGE
|| symbolic_range_p (&vr0
))
3067 /* For division, if op1 has VR_RANGE but op0 does not, something
3068 can be deduced just from that range. Say [min, max] / [4, max]
3069 gives [min / 4, max / 4] range. */
3070 if (vr1
.type
== VR_RANGE
3071 && !symbolic_range_p (&vr1
)
3072 && range_includes_zero_p (vr1
.min
, vr1
.max
) == 0)
3074 vr0
.type
= type
= VR_RANGE
;
3075 vr0
.min
= vrp_val_min (expr_type
);
3076 vr0
.max
= vrp_val_max (expr_type
);
3080 set_value_range_to_varying (vr
);
3085 /* For divisions, if flag_non_call_exceptions is true, we must
3086 not eliminate a division by zero. */
3087 if (cfun
->can_throw_non_call_exceptions
3088 && (vr1
.type
!= VR_RANGE
3089 || range_includes_zero_p (vr1
.min
, vr1
.max
) != 0))
3091 set_value_range_to_varying (vr
);
3095 /* For divisions, if op0 is VR_RANGE, we can deduce a range
3096 even if op1 is VR_VARYING, VR_ANTI_RANGE, symbolic or can
3098 if (vr0
.type
== VR_RANGE
3099 && (vr1
.type
!= VR_RANGE
3100 || range_includes_zero_p (vr1
.min
, vr1
.max
) != 0))
3102 tree zero
= build_int_cst (TREE_TYPE (vr0
.min
), 0);
3107 if (TYPE_UNSIGNED (expr_type
)
3108 || value_range_nonnegative_p (&vr1
))
3110 /* For unsigned division or when divisor is known
3111 to be non-negative, the range has to cover
3112 all numbers from 0 to max for positive max
3113 and all numbers from min to 0 for negative min. */
3114 cmp
= compare_values (vr0
.max
, zero
);
3117 else if (cmp
== 0 || cmp
== 1)
3121 cmp
= compare_values (vr0
.min
, zero
);
3124 else if (cmp
== 0 || cmp
== -1)
3131 /* Otherwise the range is -max .. max or min .. -min
3132 depending on which bound is bigger in absolute value,
3133 as the division can change the sign. */
3134 abs_extent_range (vr
, vr0
.min
, vr0
.max
);
3137 if (type
== VR_VARYING
)
3139 set_value_range_to_varying (vr
);
3145 extract_range_from_multiplicative_op_1 (vr
, code
, &vr0
, &vr1
);
3149 else if (code
== TRUNC_MOD_EXPR
)
3151 if (vr1
.type
!= VR_RANGE
3152 || range_includes_zero_p (vr1
.min
, vr1
.max
) != 0
3153 || vrp_val_is_min (vr1
.min
))
3155 set_value_range_to_varying (vr
);
3159 /* Compute MAX <|vr1.min|, |vr1.max|> - 1. */
3160 max
= fold_unary_to_constant (ABS_EXPR
, expr_type
, vr1
.min
);
3161 if (tree_int_cst_lt (max
, vr1
.max
))
3163 max
= int_const_binop (MINUS_EXPR
, max
, build_int_cst (TREE_TYPE (max
), 1));
3164 /* If the dividend is non-negative the modulus will be
3165 non-negative as well. */
3166 if (TYPE_UNSIGNED (expr_type
)
3167 || value_range_nonnegative_p (&vr0
))
3168 min
= build_int_cst (TREE_TYPE (max
), 0);
3170 min
= fold_unary_to_constant (NEGATE_EXPR
, expr_type
, max
);
3172 else if (code
== BIT_AND_EXPR
|| code
== BIT_IOR_EXPR
|| code
== BIT_XOR_EXPR
)
3174 bool int_cst_range0
, int_cst_range1
;
3175 wide_int may_be_nonzero0
, may_be_nonzero1
;
3176 wide_int must_be_nonzero0
, must_be_nonzero1
;
3178 int_cst_range0
= zero_nonzero_bits_from_vr (expr_type
, &vr0
,
3181 int_cst_range1
= zero_nonzero_bits_from_vr (expr_type
, &vr1
,
3186 if (code
== BIT_AND_EXPR
)
3188 min
= wide_int_to_tree (expr_type
,
3189 must_be_nonzero0
& must_be_nonzero1
);
3190 wide_int wmax
= may_be_nonzero0
& may_be_nonzero1
;
3191 /* If both input ranges contain only negative values we can
3192 truncate the result range maximum to the minimum of the
3193 input range maxima. */
3194 if (int_cst_range0
&& int_cst_range1
3195 && tree_int_cst_sgn (vr0
.max
) < 0
3196 && tree_int_cst_sgn (vr1
.max
) < 0)
3198 wmax
= wi::min (wmax
, vr0
.max
, TYPE_SIGN (expr_type
));
3199 wmax
= wi::min (wmax
, vr1
.max
, TYPE_SIGN (expr_type
));
3201 /* If either input range contains only non-negative values
3202 we can truncate the result range maximum to the respective
3203 maximum of the input range. */
3204 if (int_cst_range0
&& tree_int_cst_sgn (vr0
.min
) >= 0)
3205 wmax
= wi::min (wmax
, vr0
.max
, TYPE_SIGN (expr_type
));
3206 if (int_cst_range1
&& tree_int_cst_sgn (vr1
.min
) >= 0)
3207 wmax
= wi::min (wmax
, vr1
.max
, TYPE_SIGN (expr_type
));
3208 max
= wide_int_to_tree (expr_type
, wmax
);
3210 else if (code
== BIT_IOR_EXPR
)
3212 max
= wide_int_to_tree (expr_type
,
3213 may_be_nonzero0
| may_be_nonzero1
);
3214 wide_int wmin
= must_be_nonzero0
| must_be_nonzero1
;
3215 /* If the input ranges contain only positive values we can
3216 truncate the minimum of the result range to the maximum
3217 of the input range minima. */
3218 if (int_cst_range0
&& int_cst_range1
3219 && tree_int_cst_sgn (vr0
.min
) >= 0
3220 && tree_int_cst_sgn (vr1
.min
) >= 0)
3222 wmin
= wi::max (wmin
, vr0
.min
, TYPE_SIGN (expr_type
));
3223 wmin
= wi::max (wmin
, vr1
.min
, TYPE_SIGN (expr_type
));
3225 /* If either input range contains only negative values
3226 we can truncate the minimum of the result range to the
3227 respective minimum range. */
3228 if (int_cst_range0
&& tree_int_cst_sgn (vr0
.max
) < 0)
3229 wmin
= wi::max (wmin
, vr0
.min
, TYPE_SIGN (expr_type
));
3230 if (int_cst_range1
&& tree_int_cst_sgn (vr1
.max
) < 0)
3231 wmin
= wi::max (wmin
, vr1
.min
, TYPE_SIGN (expr_type
));
3232 min
= wide_int_to_tree (expr_type
, wmin
);
3234 else if (code
== BIT_XOR_EXPR
)
3236 wide_int result_zero_bits
= ((must_be_nonzero0
& must_be_nonzero1
)
3237 | ~(may_be_nonzero0
| may_be_nonzero1
));
3238 wide_int result_one_bits
3239 = (must_be_nonzero0
.and_not (may_be_nonzero1
)
3240 | must_be_nonzero1
.and_not (may_be_nonzero0
));
3241 max
= wide_int_to_tree (expr_type
, ~result_zero_bits
);
3242 min
= wide_int_to_tree (expr_type
, result_one_bits
);
3243 /* If the range has all positive or all negative values the
3244 result is better than VARYING. */
3245 if (tree_int_cst_sgn (min
) < 0
3246 || tree_int_cst_sgn (max
) >= 0)
3249 max
= min
= NULL_TREE
;
3255 /* If either MIN or MAX overflowed, then set the resulting range to
3256 VARYING. But we do accept an overflow infinity representation. */
3257 if (min
== NULL_TREE
3258 || (TREE_OVERFLOW_P (min
) && !is_overflow_infinity (min
))
3260 || (TREE_OVERFLOW_P (max
) && !is_overflow_infinity (max
)))
3262 set_value_range_to_varying (vr
);
3268 2) [-INF, +-INF(OVF)]
3269 3) [+-INF(OVF), +INF]
3270 4) [+-INF(OVF), +-INF(OVF)]
3271 We learn nothing when we have INF and INF(OVF) on both sides.
3272 Note that we do accept [-INF, -INF] and [+INF, +INF] without
3274 if ((vrp_val_is_min (min
) || is_overflow_infinity (min
))
3275 && (vrp_val_is_max (max
) || is_overflow_infinity (max
)))
3277 set_value_range_to_varying (vr
);
3281 cmp
= compare_values (min
, max
);
3282 if (cmp
== -2 || cmp
== 1)
3284 /* If the new range has its limits swapped around (MIN > MAX),
3285 then the operation caused one of them to wrap around, mark
3286 the new range VARYING. */
3287 set_value_range_to_varying (vr
);
3290 set_value_range (vr
, type
, min
, max
, NULL
);
3293 /* Extract range information from a binary expression OP0 CODE OP1 based on
3294 the ranges of each of its operands with resulting type EXPR_TYPE.
3295 The resulting range is stored in *VR. */
3298 extract_range_from_binary_expr (value_range_t
*vr
,
3299 enum tree_code code
,
3300 tree expr_type
, tree op0
, tree op1
)
3302 value_range_t vr0
= VR_INITIALIZER
;
3303 value_range_t vr1
= VR_INITIALIZER
;
3305 /* Get value ranges for each operand. For constant operands, create
3306 a new value range with the operand to simplify processing. */
3307 if (TREE_CODE (op0
) == SSA_NAME
)
3308 vr0
= *(get_value_range (op0
));
3309 else if (is_gimple_min_invariant (op0
))
3310 set_value_range_to_value (&vr0
, op0
, NULL
);
3312 set_value_range_to_varying (&vr0
);
3314 if (TREE_CODE (op1
) == SSA_NAME
)
3315 vr1
= *(get_value_range (op1
));
3316 else if (is_gimple_min_invariant (op1
))
3317 set_value_range_to_value (&vr1
, op1
, NULL
);
3319 set_value_range_to_varying (&vr1
);
3321 extract_range_from_binary_expr_1 (vr
, code
, expr_type
, &vr0
, &vr1
);
3323 /* Try harder for PLUS and MINUS if the range of one operand is symbolic
3324 and based on the other operand, for example if it was deduced from a
3325 symbolic comparison. When a bound of the range of the first operand
3326 is invariant, we set the corresponding bound of the new range to INF
3327 in order to avoid recursing on the range of the second operand. */
3328 if (vr
->type
== VR_VARYING
3329 && (code
== PLUS_EXPR
|| code
== MINUS_EXPR
)
3330 && TREE_CODE (op1
) == SSA_NAME
3331 && vr0
.type
== VR_RANGE
3332 && symbolic_range_based_on_p (&vr0
, op1
))
3334 const bool minus_p
= (code
== MINUS_EXPR
);
3335 value_range_t n_vr1
= VR_INITIALIZER
;
3337 /* Try with VR0 and [-INF, OP1]. */
3338 if (is_gimple_min_invariant (minus_p
? vr0
.max
: vr0
.min
))
3339 set_value_range (&n_vr1
, VR_RANGE
, vrp_val_min (expr_type
), op1
, NULL
);
3341 /* Try with VR0 and [OP1, +INF]. */
3342 else if (is_gimple_min_invariant (minus_p
? vr0
.min
: vr0
.max
))
3343 set_value_range (&n_vr1
, VR_RANGE
, op1
, vrp_val_max (expr_type
), NULL
);
3345 /* Try with VR0 and [OP1, OP1]. */
3347 set_value_range (&n_vr1
, VR_RANGE
, op1
, op1
, NULL
);
3349 extract_range_from_binary_expr_1 (vr
, code
, expr_type
, &vr0
, &n_vr1
);
3352 if (vr
->type
== VR_VARYING
3353 && (code
== PLUS_EXPR
|| code
== MINUS_EXPR
)
3354 && TREE_CODE (op0
) == SSA_NAME
3355 && vr1
.type
== VR_RANGE
3356 && symbolic_range_based_on_p (&vr1
, op0
))
3358 const bool minus_p
= (code
== MINUS_EXPR
);
3359 value_range_t n_vr0
= VR_INITIALIZER
;
3361 /* Try with [-INF, OP0] and VR1. */
3362 if (is_gimple_min_invariant (minus_p
? vr1
.max
: vr1
.min
))
3363 set_value_range (&n_vr0
, VR_RANGE
, vrp_val_min (expr_type
), op0
, NULL
);
3365 /* Try with [OP0, +INF] and VR1. */
3366 else if (is_gimple_min_invariant (minus_p
? vr1
.min
: vr1
.max
))
3367 set_value_range (&n_vr0
, VR_RANGE
, op0
, vrp_val_max (expr_type
), NULL
);
3369 /* Try with [OP0, OP0] and VR1. */
3371 set_value_range (&n_vr0
, VR_RANGE
, op0
, op0
, NULL
);
3373 extract_range_from_binary_expr_1 (vr
, code
, expr_type
, &n_vr0
, &vr1
);
3377 /* Extract range information from a unary operation CODE based on
3378 the range of its operand *VR0 with type OP0_TYPE with resulting type TYPE.
3379 The The resulting range is stored in *VR. */
3382 extract_range_from_unary_expr_1 (value_range_t
*vr
,
3383 enum tree_code code
, tree type
,
3384 value_range_t
*vr0_
, tree op0_type
)
3386 value_range_t vr0
= *vr0_
, vrtem0
= VR_INITIALIZER
, vrtem1
= VR_INITIALIZER
;
3388 /* VRP only operates on integral and pointer types. */
3389 if (!(INTEGRAL_TYPE_P (op0_type
)
3390 || POINTER_TYPE_P (op0_type
))
3391 || !(INTEGRAL_TYPE_P (type
)
3392 || POINTER_TYPE_P (type
)))
3394 set_value_range_to_varying (vr
);
3398 /* If VR0 is UNDEFINED, so is the result. */
3399 if (vr0
.type
== VR_UNDEFINED
)
3401 set_value_range_to_undefined (vr
);
3405 /* Handle operations that we express in terms of others. */
3406 if (code
== PAREN_EXPR
|| code
== OBJ_TYPE_REF
)
3408 /* PAREN_EXPR and OBJ_TYPE_REF are simple copies. */
3409 copy_value_range (vr
, &vr0
);
3412 else if (code
== NEGATE_EXPR
)
3414 /* -X is simply 0 - X, so re-use existing code that also handles
3415 anti-ranges fine. */
3416 value_range_t zero
= VR_INITIALIZER
;
3417 set_value_range_to_value (&zero
, build_int_cst (type
, 0), NULL
);
3418 extract_range_from_binary_expr_1 (vr
, MINUS_EXPR
, type
, &zero
, &vr0
);
3421 else if (code
== BIT_NOT_EXPR
)
3423 /* ~X is simply -1 - X, so re-use existing code that also handles
3424 anti-ranges fine. */
3425 value_range_t minusone
= VR_INITIALIZER
;
3426 set_value_range_to_value (&minusone
, build_int_cst (type
, -1), NULL
);
3427 extract_range_from_binary_expr_1 (vr
, MINUS_EXPR
,
3428 type
, &minusone
, &vr0
);
3432 /* Now canonicalize anti-ranges to ranges when they are not symbolic
3433 and express op ~[] as (op []') U (op []''). */
3434 if (vr0
.type
== VR_ANTI_RANGE
3435 && ranges_from_anti_range (&vr0
, &vrtem0
, &vrtem1
))
3437 extract_range_from_unary_expr_1 (vr
, code
, type
, &vrtem0
, op0_type
);
3438 if (vrtem1
.type
!= VR_UNDEFINED
)
3440 value_range_t vrres
= VR_INITIALIZER
;
3441 extract_range_from_unary_expr_1 (&vrres
, code
, type
,
3443 vrp_meet (vr
, &vrres
);
3448 if (CONVERT_EXPR_CODE_P (code
))
3450 tree inner_type
= op0_type
;
3451 tree outer_type
= type
;
3453 /* If the expression evaluates to a pointer, we are only interested in
3454 determining if it evaluates to NULL [0, 0] or non-NULL (~[0, 0]). */
3455 if (POINTER_TYPE_P (type
))
3457 if (range_is_nonnull (&vr0
))
3458 set_value_range_to_nonnull (vr
, type
);
3459 else if (range_is_null (&vr0
))
3460 set_value_range_to_null (vr
, type
);
3462 set_value_range_to_varying (vr
);
3466 /* If VR0 is varying and we increase the type precision, assume
3467 a full range for the following transformation. */
3468 if (vr0
.type
== VR_VARYING
3469 && INTEGRAL_TYPE_P (inner_type
)
3470 && TYPE_PRECISION (inner_type
) < TYPE_PRECISION (outer_type
))
3472 vr0
.type
= VR_RANGE
;
3473 vr0
.min
= TYPE_MIN_VALUE (inner_type
);
3474 vr0
.max
= TYPE_MAX_VALUE (inner_type
);
3477 /* If VR0 is a constant range or anti-range and the conversion is
3478 not truncating we can convert the min and max values and
3479 canonicalize the resulting range. Otherwise we can do the
3480 conversion if the size of the range is less than what the
3481 precision of the target type can represent and the range is
3482 not an anti-range. */
3483 if ((vr0
.type
== VR_RANGE
3484 || vr0
.type
== VR_ANTI_RANGE
)
3485 && TREE_CODE (vr0
.min
) == INTEGER_CST
3486 && TREE_CODE (vr0
.max
) == INTEGER_CST
3487 && (!is_overflow_infinity (vr0
.min
)
3488 || (vr0
.type
== VR_RANGE
3489 && TYPE_PRECISION (outer_type
) > TYPE_PRECISION (inner_type
)
3490 && needs_overflow_infinity (outer_type
)
3491 && supports_overflow_infinity (outer_type
)))
3492 && (!is_overflow_infinity (vr0
.max
)
3493 || (vr0
.type
== VR_RANGE
3494 && TYPE_PRECISION (outer_type
) > TYPE_PRECISION (inner_type
)
3495 && needs_overflow_infinity (outer_type
)
3496 && supports_overflow_infinity (outer_type
)))
3497 && (TYPE_PRECISION (outer_type
) >= TYPE_PRECISION (inner_type
)
3498 || (vr0
.type
== VR_RANGE
3499 && integer_zerop (int_const_binop (RSHIFT_EXPR
,
3500 int_const_binop (MINUS_EXPR
, vr0
.max
, vr0
.min
),
3501 size_int (TYPE_PRECISION (outer_type
)))))))
3503 tree new_min
, new_max
;
3504 if (is_overflow_infinity (vr0
.min
))
3505 new_min
= negative_overflow_infinity (outer_type
);
3507 new_min
= force_fit_type (outer_type
, wi::to_widest (vr0
.min
),
3509 if (is_overflow_infinity (vr0
.max
))
3510 new_max
= positive_overflow_infinity (outer_type
);
3512 new_max
= force_fit_type (outer_type
, wi::to_widest (vr0
.max
),
3514 set_and_canonicalize_value_range (vr
, vr0
.type
,
3515 new_min
, new_max
, NULL
);
3519 set_value_range_to_varying (vr
);
3522 else if (code
== ABS_EXPR
)
3527 /* Pass through vr0 in the easy cases. */
3528 if (TYPE_UNSIGNED (type
)
3529 || value_range_nonnegative_p (&vr0
))
3531 copy_value_range (vr
, &vr0
);
3535 /* For the remaining varying or symbolic ranges we can't do anything
3537 if (vr0
.type
== VR_VARYING
3538 || symbolic_range_p (&vr0
))
3540 set_value_range_to_varying (vr
);
3544 /* -TYPE_MIN_VALUE = TYPE_MIN_VALUE with flag_wrapv so we can't get a
3546 if (!TYPE_OVERFLOW_UNDEFINED (type
)
3547 && ((vr0
.type
== VR_RANGE
3548 && vrp_val_is_min (vr0
.min
))
3549 || (vr0
.type
== VR_ANTI_RANGE
3550 && !vrp_val_is_min (vr0
.min
))))
3552 set_value_range_to_varying (vr
);
3556 /* ABS_EXPR may flip the range around, if the original range
3557 included negative values. */
3558 if (is_overflow_infinity (vr0
.min
))
3559 min
= positive_overflow_infinity (type
);
3560 else if (!vrp_val_is_min (vr0
.min
))
3561 min
= fold_unary_to_constant (code
, type
, vr0
.min
);
3562 else if (!needs_overflow_infinity (type
))
3563 min
= TYPE_MAX_VALUE (type
);
3564 else if (supports_overflow_infinity (type
))
3565 min
= positive_overflow_infinity (type
);
3568 set_value_range_to_varying (vr
);
3572 if (is_overflow_infinity (vr0
.max
))
3573 max
= positive_overflow_infinity (type
);
3574 else if (!vrp_val_is_min (vr0
.max
))
3575 max
= fold_unary_to_constant (code
, type
, vr0
.max
);
3576 else if (!needs_overflow_infinity (type
))
3577 max
= TYPE_MAX_VALUE (type
);
3578 else if (supports_overflow_infinity (type
)
3579 /* We shouldn't generate [+INF, +INF] as set_value_range
3580 doesn't like this and ICEs. */
3581 && !is_positive_overflow_infinity (min
))
3582 max
= positive_overflow_infinity (type
);
3585 set_value_range_to_varying (vr
);
3589 cmp
= compare_values (min
, max
);
3591 /* If a VR_ANTI_RANGEs contains zero, then we have
3592 ~[-INF, min(MIN, MAX)]. */
3593 if (vr0
.type
== VR_ANTI_RANGE
)
3595 if (range_includes_zero_p (vr0
.min
, vr0
.max
) == 1)
3597 /* Take the lower of the two values. */
3601 /* Create ~[-INF, min (abs(MIN), abs(MAX))]
3602 or ~[-INF + 1, min (abs(MIN), abs(MAX))] when
3603 flag_wrapv is set and the original anti-range doesn't include
3604 TYPE_MIN_VALUE, remember -TYPE_MIN_VALUE = TYPE_MIN_VALUE. */
3605 if (TYPE_OVERFLOW_WRAPS (type
))
3607 tree type_min_value
= TYPE_MIN_VALUE (type
);
3609 min
= (vr0
.min
!= type_min_value
3610 ? int_const_binop (PLUS_EXPR
, type_min_value
,
3611 build_int_cst (TREE_TYPE (type_min_value
), 1))
3616 if (overflow_infinity_range_p (&vr0
))
3617 min
= negative_overflow_infinity (type
);
3619 min
= TYPE_MIN_VALUE (type
);
3624 /* All else has failed, so create the range [0, INF], even for
3625 flag_wrapv since TYPE_MIN_VALUE is in the original
3627 vr0
.type
= VR_RANGE
;
3628 min
= build_int_cst (type
, 0);
3629 if (needs_overflow_infinity (type
))
3631 if (supports_overflow_infinity (type
))
3632 max
= positive_overflow_infinity (type
);
3635 set_value_range_to_varying (vr
);
3640 max
= TYPE_MAX_VALUE (type
);
3644 /* If the range contains zero then we know that the minimum value in the
3645 range will be zero. */
3646 else if (range_includes_zero_p (vr0
.min
, vr0
.max
) == 1)
3650 min
= build_int_cst (type
, 0);
3654 /* If the range was reversed, swap MIN and MAX. */
3663 cmp
= compare_values (min
, max
);
3664 if (cmp
== -2 || cmp
== 1)
3666 /* If the new range has its limits swapped around (MIN > MAX),
3667 then the operation caused one of them to wrap around, mark
3668 the new range VARYING. */
3669 set_value_range_to_varying (vr
);
3672 set_value_range (vr
, vr0
.type
, min
, max
, NULL
);
3676 /* For unhandled operations fall back to varying. */
3677 set_value_range_to_varying (vr
);
3682 /* Extract range information from a unary expression CODE OP0 based on
3683 the range of its operand with resulting type TYPE.
3684 The resulting range is stored in *VR. */
3687 extract_range_from_unary_expr (value_range_t
*vr
, enum tree_code code
,
3688 tree type
, tree op0
)
3690 value_range_t vr0
= VR_INITIALIZER
;
3692 /* Get value ranges for the operand. For constant operands, create
3693 a new value range with the operand to simplify processing. */
3694 if (TREE_CODE (op0
) == SSA_NAME
)
3695 vr0
= *(get_value_range (op0
));
3696 else if (is_gimple_min_invariant (op0
))
3697 set_value_range_to_value (&vr0
, op0
, NULL
);
3699 set_value_range_to_varying (&vr0
);
3701 extract_range_from_unary_expr_1 (vr
, code
, type
, &vr0
, TREE_TYPE (op0
));
3705 /* Extract range information from a conditional expression STMT based on
3706 the ranges of each of its operands and the expression code. */
3709 extract_range_from_cond_expr (value_range_t
*vr
, gassign
*stmt
)
3712 value_range_t vr0
= VR_INITIALIZER
;
3713 value_range_t vr1
= VR_INITIALIZER
;
3715 /* Get value ranges for each operand. For constant operands, create
3716 a new value range with the operand to simplify processing. */
3717 op0
= gimple_assign_rhs2 (stmt
);
3718 if (TREE_CODE (op0
) == SSA_NAME
)
3719 vr0
= *(get_value_range (op0
));
3720 else if (is_gimple_min_invariant (op0
))
3721 set_value_range_to_value (&vr0
, op0
, NULL
);
3723 set_value_range_to_varying (&vr0
);
3725 op1
= gimple_assign_rhs3 (stmt
);
3726 if (TREE_CODE (op1
) == SSA_NAME
)
3727 vr1
= *(get_value_range (op1
));
3728 else if (is_gimple_min_invariant (op1
))
3729 set_value_range_to_value (&vr1
, op1
, NULL
);
3731 set_value_range_to_varying (&vr1
);
3733 /* The resulting value range is the union of the operand ranges */
3734 copy_value_range (vr
, &vr0
);
3735 vrp_meet (vr
, &vr1
);
3739 /* Extract range information from a comparison expression EXPR based
3740 on the range of its operand and the expression code. */
3743 extract_range_from_comparison (value_range_t
*vr
, enum tree_code code
,
3744 tree type
, tree op0
, tree op1
)
3749 val
= vrp_evaluate_conditional_warnv_with_ops (code
, op0
, op1
, false, &sop
,
3752 /* A disadvantage of using a special infinity as an overflow
3753 representation is that we lose the ability to record overflow
3754 when we don't have an infinity. So we have to ignore a result
3755 which relies on overflow. */
3757 if (val
&& !is_overflow_infinity (val
) && !sop
)
3759 /* Since this expression was found on the RHS of an assignment,
3760 its type may be different from _Bool. Convert VAL to EXPR's
3762 val
= fold_convert (type
, val
);
3763 if (is_gimple_min_invariant (val
))
3764 set_value_range_to_value (vr
, val
, vr
->equiv
);
3766 set_value_range (vr
, VR_RANGE
, val
, val
, vr
->equiv
);
3769 /* The result of a comparison is always true or false. */
3770 set_value_range_to_truthvalue (vr
, type
);
3773 /* Helper function for simplify_internal_call_using_ranges and
3774 extract_range_basic. Return true if OP0 SUBCODE OP1 for
3775 SUBCODE {PLUS,MINUS,MULT}_EXPR is known to never overflow or
3776 always overflow. Set *OVF to true if it is known to always
3780 check_for_binary_op_overflow (enum tree_code subcode
, tree type
,
3781 tree op0
, tree op1
, bool *ovf
)
3783 value_range_t vr0
= VR_INITIALIZER
;
3784 value_range_t vr1
= VR_INITIALIZER
;
3785 if (TREE_CODE (op0
) == SSA_NAME
)
3786 vr0
= *get_value_range (op0
);
3787 else if (TREE_CODE (op0
) == INTEGER_CST
)
3788 set_value_range_to_value (&vr0
, op0
, NULL
);
3790 set_value_range_to_varying (&vr0
);
3792 if (TREE_CODE (op1
) == SSA_NAME
)
3793 vr1
= *get_value_range (op1
);
3794 else if (TREE_CODE (op1
) == INTEGER_CST
)
3795 set_value_range_to_value (&vr1
, op1
, NULL
);
3797 set_value_range_to_varying (&vr1
);
3799 if (!range_int_cst_p (&vr0
)
3800 || TREE_OVERFLOW (vr0
.min
)
3801 || TREE_OVERFLOW (vr0
.max
))
3803 vr0
.min
= vrp_val_min (TREE_TYPE (op0
));
3804 vr0
.max
= vrp_val_max (TREE_TYPE (op0
));
3806 if (!range_int_cst_p (&vr1
)
3807 || TREE_OVERFLOW (vr1
.min
)
3808 || TREE_OVERFLOW (vr1
.max
))
3810 vr1
.min
= vrp_val_min (TREE_TYPE (op1
));
3811 vr1
.max
= vrp_val_max (TREE_TYPE (op1
));
3813 *ovf
= arith_overflowed_p (subcode
, type
, vr0
.min
,
3814 subcode
== MINUS_EXPR
? vr1
.max
: vr1
.min
);
3815 if (arith_overflowed_p (subcode
, type
, vr0
.max
,
3816 subcode
== MINUS_EXPR
? vr1
.min
: vr1
.max
) != *ovf
)
3818 if (subcode
== MULT_EXPR
)
3820 if (arith_overflowed_p (subcode
, type
, vr0
.min
, vr1
.max
) != *ovf
3821 || arith_overflowed_p (subcode
, type
, vr0
.max
, vr1
.min
) != *ovf
)
3826 /* So far we found that there is an overflow on the boundaries.
3827 That doesn't prove that there is an overflow even for all values
3828 in between the boundaries. For that compute widest_int range
3829 of the result and see if it doesn't overlap the range of
3831 widest_int wmin
, wmax
;
3834 w
[0] = wi::to_widest (vr0
.min
);
3835 w
[1] = wi::to_widest (vr0
.max
);
3836 w
[2] = wi::to_widest (vr1
.min
);
3837 w
[3] = wi::to_widest (vr1
.max
);
3838 for (i
= 0; i
< 4; i
++)
3844 wt
= wi::add (w
[i
& 1], w
[2 + (i
& 2) / 2]);
3847 wt
= wi::sub (w
[i
& 1], w
[2 + (i
& 2) / 2]);
3850 wt
= wi::mul (w
[i
& 1], w
[2 + (i
& 2) / 2]);
3862 wmin
= wi::smin (wmin
, wt
);
3863 wmax
= wi::smax (wmax
, wt
);
3866 /* The result of op0 CODE op1 is known to be in range
3868 widest_int wtmin
= wi::to_widest (vrp_val_min (type
));
3869 widest_int wtmax
= wi::to_widest (vrp_val_max (type
));
3870 /* If all values in [wmin, wmax] are smaller than
3871 [wtmin, wtmax] or all are larger than [wtmin, wtmax],
3872 the arithmetic operation will always overflow. */
3873 if (wi::lts_p (wmax
, wtmin
) || wi::gts_p (wmin
, wtmax
))
3880 /* Try to derive a nonnegative or nonzero range out of STMT relying
3881 primarily on generic routines in fold in conjunction with range data.
3882 Store the result in *VR */
3885 extract_range_basic (value_range_t
*vr
, gimple stmt
)
3888 tree type
= gimple_expr_type (stmt
);
3890 if (gimple_call_builtin_p (stmt
, BUILT_IN_NORMAL
))
3892 tree fndecl
= gimple_call_fndecl (stmt
), arg
;
3893 int mini
, maxi
, zerov
= 0, prec
;
3895 switch (DECL_FUNCTION_CODE (fndecl
))
3897 case BUILT_IN_CONSTANT_P
:
3898 /* If the call is __builtin_constant_p and the argument is a
3899 function parameter resolve it to false. This avoids bogus
3900 array bound warnings.
3901 ??? We could do this as early as inlining is finished. */
3902 arg
= gimple_call_arg (stmt
, 0);
3903 if (TREE_CODE (arg
) == SSA_NAME
3904 && SSA_NAME_IS_DEFAULT_DEF (arg
)
3905 && TREE_CODE (SSA_NAME_VAR (arg
)) == PARM_DECL
)
3907 set_value_range_to_null (vr
, type
);
3911 /* Both __builtin_ffs* and __builtin_popcount return
3913 CASE_INT_FN (BUILT_IN_FFS
):
3914 CASE_INT_FN (BUILT_IN_POPCOUNT
):
3915 arg
= gimple_call_arg (stmt
, 0);
3916 prec
= TYPE_PRECISION (TREE_TYPE (arg
));
3919 if (TREE_CODE (arg
) == SSA_NAME
)
3921 value_range_t
*vr0
= get_value_range (arg
);
3922 /* If arg is non-zero, then ffs or popcount
3924 if (((vr0
->type
== VR_RANGE
3925 && range_includes_zero_p (vr0
->min
, vr0
->max
) == 0)
3926 || (vr0
->type
== VR_ANTI_RANGE
3927 && range_includes_zero_p (vr0
->min
, vr0
->max
) == 1))
3928 && !is_overflow_infinity (vr0
->min
)
3929 && !is_overflow_infinity (vr0
->max
))
3931 /* If some high bits are known to be zero,
3932 we can decrease the maximum. */
3933 if (vr0
->type
== VR_RANGE
3934 && TREE_CODE (vr0
->max
) == INTEGER_CST
3935 && !operand_less_p (vr0
->min
,
3936 build_zero_cst (TREE_TYPE (vr0
->min
)))
3937 && !is_overflow_infinity (vr0
->max
))
3938 maxi
= tree_floor_log2 (vr0
->max
) + 1;
3941 /* __builtin_parity* returns [0, 1]. */
3942 CASE_INT_FN (BUILT_IN_PARITY
):
3946 /* __builtin_c[lt]z* return [0, prec-1], except for
3947 when the argument is 0, but that is undefined behavior.
3948 On many targets where the CLZ RTL or optab value is defined
3949 for 0 the value is prec, so include that in the range
3951 CASE_INT_FN (BUILT_IN_CLZ
):
3952 arg
= gimple_call_arg (stmt
, 0);
3953 prec
= TYPE_PRECISION (TREE_TYPE (arg
));
3956 if (optab_handler (clz_optab
, TYPE_MODE (TREE_TYPE (arg
)))
3958 && CLZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (TREE_TYPE (arg
)),
3960 /* Handle only the single common value. */
3962 /* Magic value to give up, unless vr0 proves
3965 if (TREE_CODE (arg
) == SSA_NAME
)
3967 value_range_t
*vr0
= get_value_range (arg
);
3968 /* From clz of VR_RANGE minimum we can compute
3970 if (vr0
->type
== VR_RANGE
3971 && TREE_CODE (vr0
->min
) == INTEGER_CST
3972 && !is_overflow_infinity (vr0
->min
))
3974 maxi
= prec
- 1 - tree_floor_log2 (vr0
->min
);
3978 else if (vr0
->type
== VR_ANTI_RANGE
3979 && integer_zerop (vr0
->min
)
3980 && !is_overflow_infinity (vr0
->min
))
3987 /* From clz of VR_RANGE maximum we can compute
3989 if (vr0
->type
== VR_RANGE
3990 && TREE_CODE (vr0
->max
) == INTEGER_CST
3991 && !is_overflow_infinity (vr0
->max
))
3993 mini
= prec
- 1 - tree_floor_log2 (vr0
->max
);
4001 /* __builtin_ctz* return [0, prec-1], except for
4002 when the argument is 0, but that is undefined behavior.
4003 If there is a ctz optab for this mode and
4004 CTZ_DEFINED_VALUE_AT_ZERO, include that in the range,
4005 otherwise just assume 0 won't be seen. */
4006 CASE_INT_FN (BUILT_IN_CTZ
):
4007 arg
= gimple_call_arg (stmt
, 0);
4008 prec
= TYPE_PRECISION (TREE_TYPE (arg
));
4011 if (optab_handler (ctz_optab
, TYPE_MODE (TREE_TYPE (arg
)))
4013 && CTZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (TREE_TYPE (arg
)),
4016 /* Handle only the two common values. */
4019 else if (zerov
== prec
)
4022 /* Magic value to give up, unless vr0 proves
4026 if (TREE_CODE (arg
) == SSA_NAME
)
4028 value_range_t
*vr0
= get_value_range (arg
);
4029 /* If arg is non-zero, then use [0, prec - 1]. */
4030 if (((vr0
->type
== VR_RANGE
4031 && integer_nonzerop (vr0
->min
))
4032 || (vr0
->type
== VR_ANTI_RANGE
4033 && integer_zerop (vr0
->min
)))
4034 && !is_overflow_infinity (vr0
->min
))
4039 /* If some high bits are known to be zero,
4040 we can decrease the result maximum. */
4041 if (vr0
->type
== VR_RANGE
4042 && TREE_CODE (vr0
->max
) == INTEGER_CST
4043 && !is_overflow_infinity (vr0
->max
))
4045 maxi
= tree_floor_log2 (vr0
->max
);
4046 /* For vr0 [0, 0] give up. */
4054 /* __builtin_clrsb* returns [0, prec-1]. */
4055 CASE_INT_FN (BUILT_IN_CLRSB
):
4056 arg
= gimple_call_arg (stmt
, 0);
4057 prec
= TYPE_PRECISION (TREE_TYPE (arg
));
4062 set_value_range (vr
, VR_RANGE
, build_int_cst (type
, mini
),
4063 build_int_cst (type
, maxi
), NULL
);
4069 else if (is_gimple_call (stmt
) && gimple_call_internal_p (stmt
))
4071 enum tree_code subcode
= ERROR_MARK
;
4072 switch (gimple_call_internal_fn (stmt
))
4074 case IFN_UBSAN_CHECK_ADD
:
4075 subcode
= PLUS_EXPR
;
4077 case IFN_UBSAN_CHECK_SUB
:
4078 subcode
= MINUS_EXPR
;
4080 case IFN_UBSAN_CHECK_MUL
:
4081 subcode
= MULT_EXPR
;
4086 if (subcode
!= ERROR_MARK
)
4088 bool saved_flag_wrapv
= flag_wrapv
;
4089 /* Pretend the arithmetics is wrapping. If there is
4090 any overflow, we'll complain, but will actually do
4091 wrapping operation. */
4093 extract_range_from_binary_expr (vr
, subcode
, type
,
4094 gimple_call_arg (stmt
, 0),
4095 gimple_call_arg (stmt
, 1));
4096 flag_wrapv
= saved_flag_wrapv
;
4098 /* If for both arguments vrp_valueize returned non-NULL,
4099 this should have been already folded and if not, it
4100 wasn't folded because of overflow. Avoid removing the
4101 UBSAN_CHECK_* calls in that case. */
4102 if (vr
->type
== VR_RANGE
4103 && (vr
->min
== vr
->max
4104 || operand_equal_p (vr
->min
, vr
->max
, 0)))
4105 set_value_range_to_varying (vr
);
4109 /* Handle extraction of the two results (result of arithmetics and
4110 a flag whether arithmetics overflowed) from {ADD,SUB,MUL}_OVERFLOW
4111 internal function. */
4112 else if (is_gimple_assign (stmt
)
4113 && (gimple_assign_rhs_code (stmt
) == REALPART_EXPR
4114 || gimple_assign_rhs_code (stmt
) == IMAGPART_EXPR
)
4115 && INTEGRAL_TYPE_P (type
))
4117 enum tree_code code
= gimple_assign_rhs_code (stmt
);
4118 tree op
= gimple_assign_rhs1 (stmt
);
4119 if (TREE_CODE (op
) == code
&& TREE_CODE (TREE_OPERAND (op
, 0)) == SSA_NAME
)
4121 gimple g
= SSA_NAME_DEF_STMT (TREE_OPERAND (op
, 0));
4122 if (is_gimple_call (g
) && gimple_call_internal_p (g
))
4124 enum tree_code subcode
= ERROR_MARK
;
4125 switch (gimple_call_internal_fn (g
))
4127 case IFN_ADD_OVERFLOW
:
4128 subcode
= PLUS_EXPR
;
4130 case IFN_SUB_OVERFLOW
:
4131 subcode
= MINUS_EXPR
;
4133 case IFN_MUL_OVERFLOW
:
4134 subcode
= MULT_EXPR
;
4139 if (subcode
!= ERROR_MARK
)
4141 tree op0
= gimple_call_arg (g
, 0);
4142 tree op1
= gimple_call_arg (g
, 1);
4143 if (code
== IMAGPART_EXPR
)
4146 if (check_for_binary_op_overflow (subcode
, type
,
4148 set_value_range_to_value (vr
,
4149 build_int_cst (type
, ovf
),
4152 set_value_range (vr
, VR_RANGE
, build_int_cst (type
, 0),
4153 build_int_cst (type
, 1), NULL
);
4155 else if (types_compatible_p (type
, TREE_TYPE (op0
))
4156 && types_compatible_p (type
, TREE_TYPE (op1
)))
4158 bool saved_flag_wrapv
= flag_wrapv
;
4159 /* Pretend the arithmetics is wrapping. If there is
4160 any overflow, IMAGPART_EXPR will be set. */
4162 extract_range_from_binary_expr (vr
, subcode
, type
,
4164 flag_wrapv
= saved_flag_wrapv
;
4168 value_range_t vr0
= VR_INITIALIZER
;
4169 value_range_t vr1
= VR_INITIALIZER
;
4170 bool saved_flag_wrapv
= flag_wrapv
;
4171 /* Pretend the arithmetics is wrapping. If there is
4172 any overflow, IMAGPART_EXPR will be set. */
4174 extract_range_from_unary_expr (&vr0
, NOP_EXPR
,
4176 extract_range_from_unary_expr (&vr1
, NOP_EXPR
,
4178 extract_range_from_binary_expr_1 (vr
, subcode
, type
,
4180 flag_wrapv
= saved_flag_wrapv
;
4187 if (INTEGRAL_TYPE_P (type
)
4188 && gimple_stmt_nonnegative_warnv_p (stmt
, &sop
))
4189 set_value_range_to_nonnegative (vr
, type
,
4190 sop
|| stmt_overflow_infinity (stmt
));
4191 else if (vrp_stmt_computes_nonzero (stmt
, &sop
)
4193 set_value_range_to_nonnull (vr
, type
);
4195 set_value_range_to_varying (vr
);
4199 /* Try to compute a useful range out of assignment STMT and store it
4203 extract_range_from_assignment (value_range_t
*vr
, gassign
*stmt
)
4205 enum tree_code code
= gimple_assign_rhs_code (stmt
);
4207 if (code
== ASSERT_EXPR
)
4208 extract_range_from_assert (vr
, gimple_assign_rhs1 (stmt
));
4209 else if (code
== SSA_NAME
)
4210 extract_range_from_ssa_name (vr
, gimple_assign_rhs1 (stmt
));
4211 else if (TREE_CODE_CLASS (code
) == tcc_binary
)
4212 extract_range_from_binary_expr (vr
, gimple_assign_rhs_code (stmt
),
4213 gimple_expr_type (stmt
),
4214 gimple_assign_rhs1 (stmt
),
4215 gimple_assign_rhs2 (stmt
));
4216 else if (TREE_CODE_CLASS (code
) == tcc_unary
)
4217 extract_range_from_unary_expr (vr
, gimple_assign_rhs_code (stmt
),
4218 gimple_expr_type (stmt
),
4219 gimple_assign_rhs1 (stmt
));
4220 else if (code
== COND_EXPR
)
4221 extract_range_from_cond_expr (vr
, stmt
);
4222 else if (TREE_CODE_CLASS (code
) == tcc_comparison
)
4223 extract_range_from_comparison (vr
, gimple_assign_rhs_code (stmt
),
4224 gimple_expr_type (stmt
),
4225 gimple_assign_rhs1 (stmt
),
4226 gimple_assign_rhs2 (stmt
));
4227 else if (get_gimple_rhs_class (code
) == GIMPLE_SINGLE_RHS
4228 && is_gimple_min_invariant (gimple_assign_rhs1 (stmt
)))
4229 set_value_range_to_value (vr
, gimple_assign_rhs1 (stmt
), NULL
);
4231 set_value_range_to_varying (vr
);
4233 if (vr
->type
== VR_VARYING
)
4234 extract_range_basic (vr
, stmt
);
4237 /* Given a range VR, a LOOP and a variable VAR, determine whether it
4238 would be profitable to adjust VR using scalar evolution information
4239 for VAR. If so, update VR with the new limits. */
4242 adjust_range_with_scev (value_range_t
*vr
, struct loop
*loop
,
4243 gimple stmt
, tree var
)
4245 tree init
, step
, chrec
, tmin
, tmax
, min
, max
, type
, tem
;
4246 enum ev_direction dir
;
4248 /* TODO. Don't adjust anti-ranges. An anti-range may provide
4249 better opportunities than a regular range, but I'm not sure. */
4250 if (vr
->type
== VR_ANTI_RANGE
)
4253 chrec
= instantiate_parameters (loop
, analyze_scalar_evolution (loop
, var
));
4255 /* Like in PR19590, scev can return a constant function. */
4256 if (is_gimple_min_invariant (chrec
))
4258 set_value_range_to_value (vr
, chrec
, vr
->equiv
);
4262 if (TREE_CODE (chrec
) != POLYNOMIAL_CHREC
)
4265 init
= initial_condition_in_loop_num (chrec
, loop
->num
);
4266 tem
= op_with_constant_singleton_value_range (init
);
4269 step
= evolution_part_in_loop_num (chrec
, loop
->num
);
4270 tem
= op_with_constant_singleton_value_range (step
);
4274 /* If STEP is symbolic, we can't know whether INIT will be the
4275 minimum or maximum value in the range. Also, unless INIT is
4276 a simple expression, compare_values and possibly other functions
4277 in tree-vrp won't be able to handle it. */
4278 if (step
== NULL_TREE
4279 || !is_gimple_min_invariant (step
)
4280 || !valid_value_p (init
))
4283 dir
= scev_direction (chrec
);
4284 if (/* Do not adjust ranges if we do not know whether the iv increases
4285 or decreases, ... */
4286 dir
== EV_DIR_UNKNOWN
4287 /* ... or if it may wrap. */
4288 || scev_probably_wraps_p (init
, step
, stmt
, get_chrec_loop (chrec
),
4292 /* We use TYPE_MIN_VALUE and TYPE_MAX_VALUE here instead of
4293 negative_overflow_infinity and positive_overflow_infinity,
4294 because we have concluded that the loop probably does not
4297 type
= TREE_TYPE (var
);
4298 if (POINTER_TYPE_P (type
) || !TYPE_MIN_VALUE (type
))
4299 tmin
= lower_bound_in_type (type
, type
);
4301 tmin
= TYPE_MIN_VALUE (type
);
4302 if (POINTER_TYPE_P (type
) || !TYPE_MAX_VALUE (type
))
4303 tmax
= upper_bound_in_type (type
, type
);
4305 tmax
= TYPE_MAX_VALUE (type
);
4307 /* Try to use estimated number of iterations for the loop to constrain the
4308 final value in the evolution. */
4309 if (TREE_CODE (step
) == INTEGER_CST
4310 && is_gimple_val (init
)
4311 && (TREE_CODE (init
) != SSA_NAME
4312 || get_value_range (init
)->type
== VR_RANGE
))
4316 /* We are only entering here for loop header PHI nodes, so using
4317 the number of latch executions is the correct thing to use. */
4318 if (max_loop_iterations (loop
, &nit
))
4320 value_range_t maxvr
= VR_INITIALIZER
;
4321 signop sgn
= TYPE_SIGN (TREE_TYPE (step
));
4324 widest_int wtmp
= wi::mul (wi::to_widest (step
), nit
, sgn
,
4326 /* If the multiplication overflowed we can't do a meaningful
4327 adjustment. Likewise if the result doesn't fit in the type
4328 of the induction variable. For a signed type we have to
4329 check whether the result has the expected signedness which
4330 is that of the step as number of iterations is unsigned. */
4332 && wi::fits_to_tree_p (wtmp
, TREE_TYPE (init
))
4334 || wi::gts_p (wtmp
, 0) == wi::gts_p (step
, 0)))
4336 tem
= wide_int_to_tree (TREE_TYPE (init
), wtmp
);
4337 extract_range_from_binary_expr (&maxvr
, PLUS_EXPR
,
4338 TREE_TYPE (init
), init
, tem
);
4339 /* Likewise if the addition did. */
4340 if (maxvr
.type
== VR_RANGE
)
4349 if (vr
->type
== VR_VARYING
|| vr
->type
== VR_UNDEFINED
)
4354 /* For VARYING or UNDEFINED ranges, just about anything we get
4355 from scalar evolutions should be better. */
4357 if (dir
== EV_DIR_DECREASES
)
4362 else if (vr
->type
== VR_RANGE
)
4367 if (dir
== EV_DIR_DECREASES
)
4369 /* INIT is the maximum value. If INIT is lower than VR->MAX
4370 but no smaller than VR->MIN, set VR->MAX to INIT. */
4371 if (compare_values (init
, max
) == -1)
4374 /* According to the loop information, the variable does not
4375 overflow. If we think it does, probably because of an
4376 overflow due to arithmetic on a different INF value,
4378 if (is_negative_overflow_infinity (min
)
4379 || compare_values (min
, tmin
) == -1)
4385 /* If INIT is bigger than VR->MIN, set VR->MIN to INIT. */
4386 if (compare_values (init
, min
) == 1)
4389 if (is_positive_overflow_infinity (max
)
4390 || compare_values (tmax
, max
) == -1)
4397 /* If we just created an invalid range with the minimum
4398 greater than the maximum, we fail conservatively.
4399 This should happen only in unreachable
4400 parts of code, or for invalid programs. */
4401 if (compare_values (min
, max
) == 1
4402 || (is_negative_overflow_infinity (min
)
4403 && is_positive_overflow_infinity (max
)))
4406 set_value_range (vr
, VR_RANGE
, min
, max
, vr
->equiv
);
4410 /* Given two numeric value ranges VR0, VR1 and a comparison code COMP:
4412 - Return BOOLEAN_TRUE_NODE if VR0 COMP VR1 always returns true for
4413 all the values in the ranges.
4415 - Return BOOLEAN_FALSE_NODE if the comparison always returns false.
4417 - Return NULL_TREE if it is not always possible to determine the
4418 value of the comparison.
4420 Also set *STRICT_OVERFLOW_P to indicate whether a range with an
4421 overflow infinity was used in the test. */
4425 compare_ranges (enum tree_code comp
, value_range_t
*vr0
, value_range_t
*vr1
,
4426 bool *strict_overflow_p
)
4428 /* VARYING or UNDEFINED ranges cannot be compared. */
4429 if (vr0
->type
== VR_VARYING
4430 || vr0
->type
== VR_UNDEFINED
4431 || vr1
->type
== VR_VARYING
4432 || vr1
->type
== VR_UNDEFINED
)
4435 /* Anti-ranges need to be handled separately. */
4436 if (vr0
->type
== VR_ANTI_RANGE
|| vr1
->type
== VR_ANTI_RANGE
)
4438 /* If both are anti-ranges, then we cannot compute any
4440 if (vr0
->type
== VR_ANTI_RANGE
&& vr1
->type
== VR_ANTI_RANGE
)
4443 /* These comparisons are never statically computable. */
4450 /* Equality can be computed only between a range and an
4451 anti-range. ~[VAL1, VAL2] == [VAL1, VAL2] is always false. */
4452 if (vr0
->type
== VR_RANGE
)
4454 /* To simplify processing, make VR0 the anti-range. */
4455 value_range_t
*tmp
= vr0
;
4460 gcc_assert (comp
== NE_EXPR
|| comp
== EQ_EXPR
);
4462 if (compare_values_warnv (vr0
->min
, vr1
->min
, strict_overflow_p
) == 0
4463 && compare_values_warnv (vr0
->max
, vr1
->max
, strict_overflow_p
) == 0)
4464 return (comp
== NE_EXPR
) ? boolean_true_node
: boolean_false_node
;
4469 if (!usable_range_p (vr0
, strict_overflow_p
)
4470 || !usable_range_p (vr1
, strict_overflow_p
))
4473 /* Simplify processing. If COMP is GT_EXPR or GE_EXPR, switch the
4474 operands around and change the comparison code. */
4475 if (comp
== GT_EXPR
|| comp
== GE_EXPR
)
4478 comp
= (comp
== GT_EXPR
) ? LT_EXPR
: LE_EXPR
;
4484 if (comp
== EQ_EXPR
)
4486 /* Equality may only be computed if both ranges represent
4487 exactly one value. */
4488 if (compare_values_warnv (vr0
->min
, vr0
->max
, strict_overflow_p
) == 0
4489 && compare_values_warnv (vr1
->min
, vr1
->max
, strict_overflow_p
) == 0)
4491 int cmp_min
= compare_values_warnv (vr0
->min
, vr1
->min
,
4493 int cmp_max
= compare_values_warnv (vr0
->max
, vr1
->max
,
4495 if (cmp_min
== 0 && cmp_max
== 0)
4496 return boolean_true_node
;
4497 else if (cmp_min
!= -2 && cmp_max
!= -2)
4498 return boolean_false_node
;
4500 /* If [V0_MIN, V1_MAX] < [V1_MIN, V1_MAX] then V0 != V1. */
4501 else if (compare_values_warnv (vr0
->min
, vr1
->max
,
4502 strict_overflow_p
) == 1
4503 || compare_values_warnv (vr1
->min
, vr0
->max
,
4504 strict_overflow_p
) == 1)
4505 return boolean_false_node
;
4509 else if (comp
== NE_EXPR
)
4513 /* If VR0 is completely to the left or completely to the right
4514 of VR1, they are always different. Notice that we need to
4515 make sure that both comparisons yield similar results to
4516 avoid comparing values that cannot be compared at
4518 cmp1
= compare_values_warnv (vr0
->max
, vr1
->min
, strict_overflow_p
);
4519 cmp2
= compare_values_warnv (vr0
->min
, vr1
->max
, strict_overflow_p
);
4520 if ((cmp1
== -1 && cmp2
== -1) || (cmp1
== 1 && cmp2
== 1))
4521 return boolean_true_node
;
4523 /* If VR0 and VR1 represent a single value and are identical,
4525 else if (compare_values_warnv (vr0
->min
, vr0
->max
,
4526 strict_overflow_p
) == 0
4527 && compare_values_warnv (vr1
->min
, vr1
->max
,
4528 strict_overflow_p
) == 0
4529 && compare_values_warnv (vr0
->min
, vr1
->min
,
4530 strict_overflow_p
) == 0
4531 && compare_values_warnv (vr0
->max
, vr1
->max
,
4532 strict_overflow_p
) == 0)
4533 return boolean_false_node
;
4535 /* Otherwise, they may or may not be different. */
4539 else if (comp
== LT_EXPR
|| comp
== LE_EXPR
)
4543 /* If VR0 is to the left of VR1, return true. */
4544 tst
= compare_values_warnv (vr0
->max
, vr1
->min
, strict_overflow_p
);
4545 if ((comp
== LT_EXPR
&& tst
== -1)
4546 || (comp
== LE_EXPR
&& (tst
== -1 || tst
== 0)))
4548 if (overflow_infinity_range_p (vr0
)
4549 || overflow_infinity_range_p (vr1
))
4550 *strict_overflow_p
= true;
4551 return boolean_true_node
;
4554 /* If VR0 is to the right of VR1, return false. */
4555 tst
= compare_values_warnv (vr0
->min
, vr1
->max
, strict_overflow_p
);
4556 if ((comp
== LT_EXPR
&& (tst
== 0 || tst
== 1))
4557 || (comp
== LE_EXPR
&& tst
== 1))
4559 if (overflow_infinity_range_p (vr0
)
4560 || overflow_infinity_range_p (vr1
))
4561 *strict_overflow_p
= true;
4562 return boolean_false_node
;
4565 /* Otherwise, we don't know. */
4573 /* Given a value range VR, a value VAL and a comparison code COMP, return
4574 BOOLEAN_TRUE_NODE if VR COMP VAL always returns true for all the
4575 values in VR. Return BOOLEAN_FALSE_NODE if the comparison
4576 always returns false. Return NULL_TREE if it is not always
4577 possible to determine the value of the comparison. Also set
4578 *STRICT_OVERFLOW_P to indicate whether a range with an overflow
4579 infinity was used in the test. */
4582 compare_range_with_value (enum tree_code comp
, value_range_t
*vr
, tree val
,
4583 bool *strict_overflow_p
)
4585 if (vr
->type
== VR_VARYING
|| vr
->type
== VR_UNDEFINED
)
4588 /* Anti-ranges need to be handled separately. */
4589 if (vr
->type
== VR_ANTI_RANGE
)
4591 /* For anti-ranges, the only predicates that we can compute at
4592 compile time are equality and inequality. */
4599 /* ~[VAL_1, VAL_2] OP VAL is known if VAL_1 <= VAL <= VAL_2. */
4600 if (value_inside_range (val
, vr
->min
, vr
->max
) == 1)
4601 return (comp
== NE_EXPR
) ? boolean_true_node
: boolean_false_node
;
4606 if (!usable_range_p (vr
, strict_overflow_p
))
4609 if (comp
== EQ_EXPR
)
4611 /* EQ_EXPR may only be computed if VR represents exactly
4613 if (compare_values_warnv (vr
->min
, vr
->max
, strict_overflow_p
) == 0)
4615 int cmp
= compare_values_warnv (vr
->min
, val
, strict_overflow_p
);
4617 return boolean_true_node
;
4618 else if (cmp
== -1 || cmp
== 1 || cmp
== 2)
4619 return boolean_false_node
;
4621 else if (compare_values_warnv (val
, vr
->min
, strict_overflow_p
) == -1
4622 || compare_values_warnv (vr
->max
, val
, strict_overflow_p
) == -1)
4623 return boolean_false_node
;
4627 else if (comp
== NE_EXPR
)
4629 /* If VAL is not inside VR, then they are always different. */
4630 if (compare_values_warnv (vr
->max
, val
, strict_overflow_p
) == -1
4631 || compare_values_warnv (vr
->min
, val
, strict_overflow_p
) == 1)
4632 return boolean_true_node
;
4634 /* If VR represents exactly one value equal to VAL, then return
4636 if (compare_values_warnv (vr
->min
, vr
->max
, strict_overflow_p
) == 0
4637 && compare_values_warnv (vr
->min
, val
, strict_overflow_p
) == 0)
4638 return boolean_false_node
;
4640 /* Otherwise, they may or may not be different. */
4643 else if (comp
== LT_EXPR
|| comp
== LE_EXPR
)
4647 /* If VR is to the left of VAL, return true. */
4648 tst
= compare_values_warnv (vr
->max
, val
, strict_overflow_p
);
4649 if ((comp
== LT_EXPR
&& tst
== -1)
4650 || (comp
== LE_EXPR
&& (tst
== -1 || tst
== 0)))
4652 if (overflow_infinity_range_p (vr
))
4653 *strict_overflow_p
= true;
4654 return boolean_true_node
;
4657 /* If VR is to the right of VAL, return false. */
4658 tst
= compare_values_warnv (vr
->min
, val
, strict_overflow_p
);
4659 if ((comp
== LT_EXPR
&& (tst
== 0 || tst
== 1))
4660 || (comp
== LE_EXPR
&& tst
== 1))
4662 if (overflow_infinity_range_p (vr
))
4663 *strict_overflow_p
= true;
4664 return boolean_false_node
;
4667 /* Otherwise, we don't know. */
4670 else if (comp
== GT_EXPR
|| comp
== GE_EXPR
)
4674 /* If VR is to the right of VAL, return true. */
4675 tst
= compare_values_warnv (vr
->min
, val
, strict_overflow_p
);
4676 if ((comp
== GT_EXPR
&& tst
== 1)
4677 || (comp
== GE_EXPR
&& (tst
== 0 || tst
== 1)))
4679 if (overflow_infinity_range_p (vr
))
4680 *strict_overflow_p
= true;
4681 return boolean_true_node
;
4684 /* If VR is to the left of VAL, return false. */
4685 tst
= compare_values_warnv (vr
->max
, val
, strict_overflow_p
);
4686 if ((comp
== GT_EXPR
&& (tst
== -1 || tst
== 0))
4687 || (comp
== GE_EXPR
&& tst
== -1))
4689 if (overflow_infinity_range_p (vr
))
4690 *strict_overflow_p
= true;
4691 return boolean_false_node
;
4694 /* Otherwise, we don't know. */
4702 /* Debugging dumps. */
4704 void dump_value_range (FILE *, value_range_t
*);
4705 void debug_value_range (value_range_t
*);
4706 void dump_all_value_ranges (FILE *);
4707 void debug_all_value_ranges (void);
4708 void dump_vr_equiv (FILE *, bitmap
);
4709 void debug_vr_equiv (bitmap
);
4712 /* Dump value range VR to FILE. */
4715 dump_value_range (FILE *file
, value_range_t
*vr
)
4718 fprintf (file
, "[]");
4719 else if (vr
->type
== VR_UNDEFINED
)
4720 fprintf (file
, "UNDEFINED");
4721 else if (vr
->type
== VR_RANGE
|| vr
->type
== VR_ANTI_RANGE
)
4723 tree type
= TREE_TYPE (vr
->min
);
4725 fprintf (file
, "%s[", (vr
->type
== VR_ANTI_RANGE
) ? "~" : "");
4727 if (is_negative_overflow_infinity (vr
->min
))
4728 fprintf (file
, "-INF(OVF)");
4729 else if (INTEGRAL_TYPE_P (type
)
4730 && !TYPE_UNSIGNED (type
)
4731 && vrp_val_is_min (vr
->min
))
4732 fprintf (file
, "-INF");
4734 print_generic_expr (file
, vr
->min
, 0);
4736 fprintf (file
, ", ");
4738 if (is_positive_overflow_infinity (vr
->max
))
4739 fprintf (file
, "+INF(OVF)");
4740 else if (INTEGRAL_TYPE_P (type
)
4741 && vrp_val_is_max (vr
->max
))
4742 fprintf (file
, "+INF");
4744 print_generic_expr (file
, vr
->max
, 0);
4746 fprintf (file
, "]");
4753 fprintf (file
, " EQUIVALENCES: { ");
4755 EXECUTE_IF_SET_IN_BITMAP (vr
->equiv
, 0, i
, bi
)
4757 print_generic_expr (file
, ssa_name (i
), 0);
4758 fprintf (file
, " ");
4762 fprintf (file
, "} (%u elements)", c
);
4765 else if (vr
->type
== VR_VARYING
)
4766 fprintf (file
, "VARYING");
4768 fprintf (file
, "INVALID RANGE");
4772 /* Dump value range VR to stderr. */
4775 debug_value_range (value_range_t
*vr
)
4777 dump_value_range (stderr
, vr
);
4778 fprintf (stderr
, "\n");
4782 /* Dump value ranges of all SSA_NAMEs to FILE. */
4785 dump_all_value_ranges (FILE *file
)
4789 for (i
= 0; i
< num_vr_values
; i
++)
4793 print_generic_expr (file
, ssa_name (i
), 0);
4794 fprintf (file
, ": ");
4795 dump_value_range (file
, vr_value
[i
]);
4796 fprintf (file
, "\n");
4800 fprintf (file
, "\n");
4804 /* Dump all value ranges to stderr. */
4807 debug_all_value_ranges (void)
4809 dump_all_value_ranges (stderr
);
4813 /* Given a COND_EXPR COND of the form 'V OP W', and an SSA name V,
4814 create a new SSA name N and return the assertion assignment
4815 'N = ASSERT_EXPR <V, V OP W>'. */
4818 build_assert_expr_for (tree cond
, tree v
)
4823 gcc_assert (TREE_CODE (v
) == SSA_NAME
4824 && COMPARISON_CLASS_P (cond
));
4826 a
= build2 (ASSERT_EXPR
, TREE_TYPE (v
), v
, cond
);
4827 assertion
= gimple_build_assign (NULL_TREE
, a
);
4829 /* The new ASSERT_EXPR, creates a new SSA name that replaces the
4830 operand of the ASSERT_EXPR. Create it so the new name and the old one
4831 are registered in the replacement table so that we can fix the SSA web
4832 after adding all the ASSERT_EXPRs. */
4833 create_new_def_for (v
, assertion
, NULL
);
4839 /* Return false if EXPR is a predicate expression involving floating
4843 fp_predicate (gimple stmt
)
4845 GIMPLE_CHECK (stmt
, GIMPLE_COND
);
4847 return FLOAT_TYPE_P (TREE_TYPE (gimple_cond_lhs (stmt
)));
4850 /* If the range of values taken by OP can be inferred after STMT executes,
4851 return the comparison code (COMP_CODE_P) and value (VAL_P) that
4852 describes the inferred range. Return true if a range could be
4856 infer_value_range (gimple stmt
, tree op
, enum tree_code
*comp_code_p
, tree
*val_p
)
4859 *comp_code_p
= ERROR_MARK
;
4861 /* Do not attempt to infer anything in names that flow through
4863 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op
))
4866 /* Similarly, don't infer anything from statements that may throw
4867 exceptions. ??? Relax this requirement? */
4868 if (stmt_could_throw_p (stmt
))
4871 /* If STMT is the last statement of a basic block with no normal
4872 successors, there is no point inferring anything about any of its
4873 operands. We would not be able to find a proper insertion point
4874 for the assertion, anyway. */
4875 if (stmt_ends_bb_p (stmt
))
4880 FOR_EACH_EDGE (e
, ei
, gimple_bb (stmt
)->succs
)
4881 if (!(e
->flags
& EDGE_ABNORMAL
))
4887 if (infer_nonnull_range (stmt
, op
, true, true))
4889 *val_p
= build_int_cst (TREE_TYPE (op
), 0);
4890 *comp_code_p
= NE_EXPR
;
4898 void dump_asserts_for (FILE *, tree
);
4899 void debug_asserts_for (tree
);
4900 void dump_all_asserts (FILE *);
4901 void debug_all_asserts (void);
4903 /* Dump all the registered assertions for NAME to FILE. */
4906 dump_asserts_for (FILE *file
, tree name
)
4910 fprintf (file
, "Assertions to be inserted for ");
4911 print_generic_expr (file
, name
, 0);
4912 fprintf (file
, "\n");
4914 loc
= asserts_for
[SSA_NAME_VERSION (name
)];
4917 fprintf (file
, "\t");
4918 print_gimple_stmt (file
, gsi_stmt (loc
->si
), 0, 0);
4919 fprintf (file
, "\n\tBB #%d", loc
->bb
->index
);
4922 fprintf (file
, "\n\tEDGE %d->%d", loc
->e
->src
->index
,
4923 loc
->e
->dest
->index
);
4924 dump_edge_info (file
, loc
->e
, dump_flags
, 0);
4926 fprintf (file
, "\n\tPREDICATE: ");
4927 print_generic_expr (file
, name
, 0);
4928 fprintf (file
, " %s ", get_tree_code_name (loc
->comp_code
));
4929 print_generic_expr (file
, loc
->val
, 0);
4930 fprintf (file
, "\n\n");
4934 fprintf (file
, "\n");
4938 /* Dump all the registered assertions for NAME to stderr. */
4941 debug_asserts_for (tree name
)
4943 dump_asserts_for (stderr
, name
);
4947 /* Dump all the registered assertions for all the names to FILE. */
4950 dump_all_asserts (FILE *file
)
4955 fprintf (file
, "\nASSERT_EXPRs to be inserted\n\n");
4956 EXECUTE_IF_SET_IN_BITMAP (need_assert_for
, 0, i
, bi
)
4957 dump_asserts_for (file
, ssa_name (i
));
4958 fprintf (file
, "\n");
4962 /* Dump all the registered assertions for all the names to stderr. */
4965 debug_all_asserts (void)
4967 dump_all_asserts (stderr
);
4971 /* If NAME doesn't have an ASSERT_EXPR registered for asserting
4972 'EXPR COMP_CODE VAL' at a location that dominates block BB or
4973 E->DEST, then register this location as a possible insertion point
4974 for ASSERT_EXPR <NAME, EXPR COMP_CODE VAL>.
4976 BB, E and SI provide the exact insertion point for the new
4977 ASSERT_EXPR. If BB is NULL, then the ASSERT_EXPR is to be inserted
4978 on edge E. Otherwise, if E is NULL, the ASSERT_EXPR is inserted on
4979 BB. If SI points to a COND_EXPR or a SWITCH_EXPR statement, then E
4980 must not be NULL. */
4983 register_new_assert_for (tree name
, tree expr
,
4984 enum tree_code comp_code
,
4988 gimple_stmt_iterator si
)
4990 assert_locus_t n
, loc
, last_loc
;
4991 basic_block dest_bb
;
4993 gcc_checking_assert (bb
== NULL
|| e
== NULL
);
4996 gcc_checking_assert (gimple_code (gsi_stmt (si
)) != GIMPLE_COND
4997 && gimple_code (gsi_stmt (si
)) != GIMPLE_SWITCH
);
4999 /* Never build an assert comparing against an integer constant with
5000 TREE_OVERFLOW set. This confuses our undefined overflow warning
5002 if (TREE_OVERFLOW_P (val
))
5003 val
= drop_tree_overflow (val
);
5005 /* The new assertion A will be inserted at BB or E. We need to
5006 determine if the new location is dominated by a previously
5007 registered location for A. If we are doing an edge insertion,
5008 assume that A will be inserted at E->DEST. Note that this is not
5011 If E is a critical edge, it will be split. But even if E is
5012 split, the new block will dominate the same set of blocks that
5015 The reverse, however, is not true, blocks dominated by E->DEST
5016 will not be dominated by the new block created to split E. So,
5017 if the insertion location is on a critical edge, we will not use
5018 the new location to move another assertion previously registered
5019 at a block dominated by E->DEST. */
5020 dest_bb
= (bb
) ? bb
: e
->dest
;
5022 /* If NAME already has an ASSERT_EXPR registered for COMP_CODE and
5023 VAL at a block dominating DEST_BB, then we don't need to insert a new
5024 one. Similarly, if the same assertion already exists at a block
5025 dominated by DEST_BB and the new location is not on a critical
5026 edge, then update the existing location for the assertion (i.e.,
5027 move the assertion up in the dominance tree).
5029 Note, this is implemented as a simple linked list because there
5030 should not be more than a handful of assertions registered per
5031 name. If this becomes a performance problem, a table hashed by
5032 COMP_CODE and VAL could be implemented. */
5033 loc
= asserts_for
[SSA_NAME_VERSION (name
)];
5037 if (loc
->comp_code
== comp_code
5039 || operand_equal_p (loc
->val
, val
, 0))
5040 && (loc
->expr
== expr
5041 || operand_equal_p (loc
->expr
, expr
, 0)))
5043 /* If E is not a critical edge and DEST_BB
5044 dominates the existing location for the assertion, move
5045 the assertion up in the dominance tree by updating its
5046 location information. */
5047 if ((e
== NULL
|| !EDGE_CRITICAL_P (e
))
5048 && dominated_by_p (CDI_DOMINATORS
, loc
->bb
, dest_bb
))
5057 /* Update the last node of the list and move to the next one. */
5062 /* If we didn't find an assertion already registered for
5063 NAME COMP_CODE VAL, add a new one at the end of the list of
5064 assertions associated with NAME. */
5065 n
= XNEW (struct assert_locus_d
);
5069 n
->comp_code
= comp_code
;
5077 asserts_for
[SSA_NAME_VERSION (name
)] = n
;
5079 bitmap_set_bit (need_assert_for
, SSA_NAME_VERSION (name
));
5082 /* (COND_OP0 COND_CODE COND_OP1) is a predicate which uses NAME.
5083 Extract a suitable test code and value and store them into *CODE_P and
5084 *VAL_P so the predicate is normalized to NAME *CODE_P *VAL_P.
5086 If no extraction was possible, return FALSE, otherwise return TRUE.
5088 If INVERT is true, then we invert the result stored into *CODE_P. */
5091 extract_code_and_val_from_cond_with_ops (tree name
, enum tree_code cond_code
,
5092 tree cond_op0
, tree cond_op1
,
5093 bool invert
, enum tree_code
*code_p
,
5096 enum tree_code comp_code
;
5099 /* Otherwise, we have a comparison of the form NAME COMP VAL
5100 or VAL COMP NAME. */
5101 if (name
== cond_op1
)
5103 /* If the predicate is of the form VAL COMP NAME, flip
5104 COMP around because we need to register NAME as the
5105 first operand in the predicate. */
5106 comp_code
= swap_tree_comparison (cond_code
);
5111 /* The comparison is of the form NAME COMP VAL, so the
5112 comparison code remains unchanged. */
5113 comp_code
= cond_code
;
5117 /* Invert the comparison code as necessary. */
5119 comp_code
= invert_tree_comparison (comp_code
, 0);
5121 /* VRP does not handle float types. */
5122 if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (val
)))
5125 /* Do not register always-false predicates.
5126 FIXME: this works around a limitation in fold() when dealing with
5127 enumerations. Given 'enum { N1, N2 } x;', fold will not
5128 fold 'if (x > N2)' to 'if (0)'. */
5129 if ((comp_code
== GT_EXPR
|| comp_code
== LT_EXPR
)
5130 && INTEGRAL_TYPE_P (TREE_TYPE (val
)))
5132 tree min
= TYPE_MIN_VALUE (TREE_TYPE (val
));
5133 tree max
= TYPE_MAX_VALUE (TREE_TYPE (val
));
5135 if (comp_code
== GT_EXPR
5137 || compare_values (val
, max
) == 0))
5140 if (comp_code
== LT_EXPR
5142 || compare_values (val
, min
) == 0))
5145 *code_p
= comp_code
;
5150 /* Find out smallest RES where RES > VAL && (RES & MASK) == RES, if any
5151 (otherwise return VAL). VAL and MASK must be zero-extended for
5152 precision PREC. If SGNBIT is non-zero, first xor VAL with SGNBIT
5153 (to transform signed values into unsigned) and at the end xor
5157 masked_increment (const wide_int
&val_in
, const wide_int
&mask
,
5158 const wide_int
&sgnbit
, unsigned int prec
)
5160 wide_int bit
= wi::one (prec
), res
;
5163 wide_int val
= val_in
^ sgnbit
;
5164 for (i
= 0; i
< prec
; i
++, bit
+= bit
)
5167 if ((res
& bit
) == 0)
5170 res
= (val
+ bit
).and_not (res
);
5172 if (wi::gtu_p (res
, val
))
5173 return res
^ sgnbit
;
5175 return val
^ sgnbit
;
5178 /* Try to register an edge assertion for SSA name NAME on edge E for
5179 the condition COND contributing to the conditional jump pointed to by BSI.
5180 Invert the condition COND if INVERT is true. */
5183 register_edge_assert_for_2 (tree name
, edge e
, gimple_stmt_iterator bsi
,
5184 enum tree_code cond_code
,
5185 tree cond_op0
, tree cond_op1
, bool invert
)
5188 enum tree_code comp_code
;
5190 if (!extract_code_and_val_from_cond_with_ops (name
, cond_code
,
5193 invert
, &comp_code
, &val
))
5196 /* Only register an ASSERT_EXPR if NAME was found in the sub-graph
5197 reachable from E. */
5198 if (live_on_edge (e
, name
)
5199 && !has_single_use (name
))
5200 register_new_assert_for (name
, name
, comp_code
, val
, NULL
, e
, bsi
);
5202 /* In the case of NAME <= CST and NAME being defined as
5203 NAME = (unsigned) NAME2 + CST2 we can assert NAME2 >= -CST2
5204 and NAME2 <= CST - CST2. We can do the same for NAME > CST.
5205 This catches range and anti-range tests. */
5206 if ((comp_code
== LE_EXPR
5207 || comp_code
== GT_EXPR
)
5208 && TREE_CODE (val
) == INTEGER_CST
5209 && TYPE_UNSIGNED (TREE_TYPE (val
)))
5211 gimple def_stmt
= SSA_NAME_DEF_STMT (name
);
5212 tree cst2
= NULL_TREE
, name2
= NULL_TREE
, name3
= NULL_TREE
;
5214 /* Extract CST2 from the (optional) addition. */
5215 if (is_gimple_assign (def_stmt
)
5216 && gimple_assign_rhs_code (def_stmt
) == PLUS_EXPR
)
5218 name2
= gimple_assign_rhs1 (def_stmt
);
5219 cst2
= gimple_assign_rhs2 (def_stmt
);
5220 if (TREE_CODE (name2
) == SSA_NAME
5221 && TREE_CODE (cst2
) == INTEGER_CST
)
5222 def_stmt
= SSA_NAME_DEF_STMT (name2
);
5225 /* Extract NAME2 from the (optional) sign-changing cast. */
5226 if (gimple_assign_cast_p (def_stmt
))
5228 if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt
))
5229 && ! TYPE_UNSIGNED (TREE_TYPE (gimple_assign_rhs1 (def_stmt
)))
5230 && (TYPE_PRECISION (gimple_expr_type (def_stmt
))
5231 == TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (def_stmt
)))))
5232 name3
= gimple_assign_rhs1 (def_stmt
);
5235 /* If name3 is used later, create an ASSERT_EXPR for it. */
5236 if (name3
!= NULL_TREE
5237 && TREE_CODE (name3
) == SSA_NAME
5238 && (cst2
== NULL_TREE
5239 || TREE_CODE (cst2
) == INTEGER_CST
)
5240 && INTEGRAL_TYPE_P (TREE_TYPE (name3
))
5241 && live_on_edge (e
, name3
)
5242 && !has_single_use (name3
))
5246 /* Build an expression for the range test. */
5247 tmp
= build1 (NOP_EXPR
, TREE_TYPE (name
), name3
);
5248 if (cst2
!= NULL_TREE
)
5249 tmp
= build2 (PLUS_EXPR
, TREE_TYPE (name
), tmp
, cst2
);
5253 fprintf (dump_file
, "Adding assert for ");
5254 print_generic_expr (dump_file
, name3
, 0);
5255 fprintf (dump_file
, " from ");
5256 print_generic_expr (dump_file
, tmp
, 0);
5257 fprintf (dump_file
, "\n");
5260 register_new_assert_for (name3
, tmp
, comp_code
, val
, NULL
, e
, bsi
);
5263 /* If name2 is used later, create an ASSERT_EXPR for it. */
5264 if (name2
!= NULL_TREE
5265 && TREE_CODE (name2
) == SSA_NAME
5266 && TREE_CODE (cst2
) == INTEGER_CST
5267 && INTEGRAL_TYPE_P (TREE_TYPE (name2
))
5268 && live_on_edge (e
, name2
)
5269 && !has_single_use (name2
))
5273 /* Build an expression for the range test. */
5275 if (TREE_TYPE (name
) != TREE_TYPE (name2
))
5276 tmp
= build1 (NOP_EXPR
, TREE_TYPE (name
), tmp
);
5277 if (cst2
!= NULL_TREE
)
5278 tmp
= build2 (PLUS_EXPR
, TREE_TYPE (name
), tmp
, cst2
);
5282 fprintf (dump_file
, "Adding assert for ");
5283 print_generic_expr (dump_file
, name2
, 0);
5284 fprintf (dump_file
, " from ");
5285 print_generic_expr (dump_file
, tmp
, 0);
5286 fprintf (dump_file
, "\n");
5289 register_new_assert_for (name2
, tmp
, comp_code
, val
, NULL
, e
, bsi
);
5293 /* In the case of post-in/decrement tests like if (i++) ... and uses
5294 of the in/decremented value on the edge the extra name we want to
5295 assert for is not on the def chain of the name compared. Instead
5296 it is in the set of use stmts. */
5297 if ((comp_code
== NE_EXPR
5298 || comp_code
== EQ_EXPR
)
5299 && TREE_CODE (val
) == INTEGER_CST
)
5301 imm_use_iterator ui
;
5303 FOR_EACH_IMM_USE_STMT (use_stmt
, ui
, name
)
5305 /* Cut off to use-stmts that are in the predecessor. */
5306 if (gimple_bb (use_stmt
) != e
->src
)
5309 if (!is_gimple_assign (use_stmt
))
5312 enum tree_code code
= gimple_assign_rhs_code (use_stmt
);
5313 if (code
!= PLUS_EXPR
5314 && code
!= MINUS_EXPR
)
5317 tree cst
= gimple_assign_rhs2 (use_stmt
);
5318 if (TREE_CODE (cst
) != INTEGER_CST
)
5321 tree name2
= gimple_assign_lhs (use_stmt
);
5322 if (live_on_edge (e
, name2
))
5324 cst
= int_const_binop (code
, val
, cst
);
5325 register_new_assert_for (name2
, name2
, comp_code
, cst
,
5331 if (TREE_CODE_CLASS (comp_code
) == tcc_comparison
5332 && TREE_CODE (val
) == INTEGER_CST
)
5334 gimple def_stmt
= SSA_NAME_DEF_STMT (name
);
5335 tree name2
= NULL_TREE
, names
[2], cst2
= NULL_TREE
;
5336 tree val2
= NULL_TREE
;
5337 unsigned int prec
= TYPE_PRECISION (TREE_TYPE (val
));
5338 wide_int mask
= wi::zero (prec
);
5339 unsigned int nprec
= prec
;
5340 enum tree_code rhs_code
= ERROR_MARK
;
5342 if (is_gimple_assign (def_stmt
))
5343 rhs_code
= gimple_assign_rhs_code (def_stmt
);
5345 /* Add asserts for NAME cmp CST and NAME being defined
5346 as NAME = (int) NAME2. */
5347 if (!TYPE_UNSIGNED (TREE_TYPE (val
))
5348 && (comp_code
== LE_EXPR
|| comp_code
== LT_EXPR
5349 || comp_code
== GT_EXPR
|| comp_code
== GE_EXPR
)
5350 && gimple_assign_cast_p (def_stmt
))
5352 name2
= gimple_assign_rhs1 (def_stmt
);
5353 if (CONVERT_EXPR_CODE_P (rhs_code
)
5354 && INTEGRAL_TYPE_P (TREE_TYPE (name2
))
5355 && TYPE_UNSIGNED (TREE_TYPE (name2
))
5356 && prec
== TYPE_PRECISION (TREE_TYPE (name2
))
5357 && (comp_code
== LE_EXPR
|| comp_code
== GT_EXPR
5358 || !tree_int_cst_equal (val
,
5359 TYPE_MIN_VALUE (TREE_TYPE (val
))))
5360 && live_on_edge (e
, name2
)
5361 && !has_single_use (name2
))
5364 enum tree_code new_comp_code
= comp_code
;
5366 cst
= fold_convert (TREE_TYPE (name2
),
5367 TYPE_MIN_VALUE (TREE_TYPE (val
)));
5368 /* Build an expression for the range test. */
5369 tmp
= build2 (PLUS_EXPR
, TREE_TYPE (name2
), name2
, cst
);
5370 cst
= fold_build2 (PLUS_EXPR
, TREE_TYPE (name2
), cst
,
5371 fold_convert (TREE_TYPE (name2
), val
));
5372 if (comp_code
== LT_EXPR
|| comp_code
== GE_EXPR
)
5374 new_comp_code
= comp_code
== LT_EXPR
? LE_EXPR
: GT_EXPR
;
5375 cst
= fold_build2 (MINUS_EXPR
, TREE_TYPE (name2
), cst
,
5376 build_int_cst (TREE_TYPE (name2
), 1));
5381 fprintf (dump_file
, "Adding assert for ");
5382 print_generic_expr (dump_file
, name2
, 0);
5383 fprintf (dump_file
, " from ");
5384 print_generic_expr (dump_file
, tmp
, 0);
5385 fprintf (dump_file
, "\n");
5388 register_new_assert_for (name2
, tmp
, new_comp_code
, cst
, NULL
,
5393 /* Add asserts for NAME cmp CST and NAME being defined as
5394 NAME = NAME2 >> CST2.
5396 Extract CST2 from the right shift. */
5397 if (rhs_code
== RSHIFT_EXPR
)
5399 name2
= gimple_assign_rhs1 (def_stmt
);
5400 cst2
= gimple_assign_rhs2 (def_stmt
);
5401 if (TREE_CODE (name2
) == SSA_NAME
5402 && tree_fits_uhwi_p (cst2
)
5403 && INTEGRAL_TYPE_P (TREE_TYPE (name2
))
5404 && IN_RANGE (tree_to_uhwi (cst2
), 1, prec
- 1)
5405 && prec
== GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (val
)))
5406 && live_on_edge (e
, name2
)
5407 && !has_single_use (name2
))
5409 mask
= wi::mask (tree_to_uhwi (cst2
), false, prec
);
5410 val2
= fold_binary (LSHIFT_EXPR
, TREE_TYPE (val
), val
, cst2
);
5413 if (val2
!= NULL_TREE
5414 && TREE_CODE (val2
) == INTEGER_CST
5415 && simple_cst_equal (fold_build2 (RSHIFT_EXPR
,
5419 enum tree_code new_comp_code
= comp_code
;
5423 if (comp_code
== EQ_EXPR
|| comp_code
== NE_EXPR
)
5425 if (!TYPE_UNSIGNED (TREE_TYPE (val
)))
5427 tree type
= build_nonstandard_integer_type (prec
, 1);
5428 tmp
= build1 (NOP_EXPR
, type
, name2
);
5429 val2
= fold_convert (type
, val2
);
5431 tmp
= fold_build2 (MINUS_EXPR
, TREE_TYPE (tmp
), tmp
, val2
);
5432 new_val
= wide_int_to_tree (TREE_TYPE (tmp
), mask
);
5433 new_comp_code
= comp_code
== EQ_EXPR
? LE_EXPR
: GT_EXPR
;
5435 else if (comp_code
== LT_EXPR
|| comp_code
== GE_EXPR
)
5438 = wi::min_value (prec
, TYPE_SIGN (TREE_TYPE (val
)));
5440 if (minval
== new_val
)
5441 new_val
= NULL_TREE
;
5446 = wi::max_value (prec
, TYPE_SIGN (TREE_TYPE (val
)));
5449 new_val
= NULL_TREE
;
5451 new_val
= wide_int_to_tree (TREE_TYPE (val2
), mask
);
5458 fprintf (dump_file
, "Adding assert for ");
5459 print_generic_expr (dump_file
, name2
, 0);
5460 fprintf (dump_file
, " from ");
5461 print_generic_expr (dump_file
, tmp
, 0);
5462 fprintf (dump_file
, "\n");
5465 register_new_assert_for (name2
, tmp
, new_comp_code
, new_val
,
5470 /* Add asserts for NAME cmp CST and NAME being defined as
5471 NAME = NAME2 & CST2.
5473 Extract CST2 from the and.
5476 NAME = (unsigned) NAME2;
5477 casts where NAME's type is unsigned and has smaller precision
5478 than NAME2's type as if it was NAME = NAME2 & MASK. */
5479 names
[0] = NULL_TREE
;
5480 names
[1] = NULL_TREE
;
5482 if (rhs_code
== BIT_AND_EXPR
5483 || (CONVERT_EXPR_CODE_P (rhs_code
)
5484 && TREE_CODE (TREE_TYPE (val
)) == INTEGER_TYPE
5485 && TYPE_UNSIGNED (TREE_TYPE (val
))
5486 && TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (def_stmt
)))
5489 name2
= gimple_assign_rhs1 (def_stmt
);
5490 if (rhs_code
== BIT_AND_EXPR
)
5491 cst2
= gimple_assign_rhs2 (def_stmt
);
5494 cst2
= TYPE_MAX_VALUE (TREE_TYPE (val
));
5495 nprec
= TYPE_PRECISION (TREE_TYPE (name2
));
5497 if (TREE_CODE (name2
) == SSA_NAME
5498 && INTEGRAL_TYPE_P (TREE_TYPE (name2
))
5499 && TREE_CODE (cst2
) == INTEGER_CST
5500 && !integer_zerop (cst2
)
5502 || TYPE_UNSIGNED (TREE_TYPE (val
))))
5504 gimple def_stmt2
= SSA_NAME_DEF_STMT (name2
);
5505 if (gimple_assign_cast_p (def_stmt2
))
5507 names
[1] = gimple_assign_rhs1 (def_stmt2
);
5508 if (!CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt2
))
5509 || !INTEGRAL_TYPE_P (TREE_TYPE (names
[1]))
5510 || (TYPE_PRECISION (TREE_TYPE (name2
))
5511 != TYPE_PRECISION (TREE_TYPE (names
[1])))
5512 || !live_on_edge (e
, names
[1])
5513 || has_single_use (names
[1]))
5514 names
[1] = NULL_TREE
;
5516 if (live_on_edge (e
, name2
)
5517 && !has_single_use (name2
))
5521 if (names
[0] || names
[1])
5523 wide_int minv
, maxv
, valv
, cst2v
;
5524 wide_int tem
, sgnbit
;
5525 bool valid_p
= false, valn
, cst2n
;
5526 enum tree_code ccode
= comp_code
;
5528 valv
= wide_int::from (val
, nprec
, UNSIGNED
);
5529 cst2v
= wide_int::from (cst2
, nprec
, UNSIGNED
);
5530 valn
= wi::neg_p (valv
, TYPE_SIGN (TREE_TYPE (val
)));
5531 cst2n
= wi::neg_p (cst2v
, TYPE_SIGN (TREE_TYPE (val
)));
5532 /* If CST2 doesn't have most significant bit set,
5533 but VAL is negative, we have comparison like
5534 if ((x & 0x123) > -4) (always true). Just give up. */
5538 sgnbit
= wi::set_bit_in_zero (nprec
- 1, nprec
);
5540 sgnbit
= wi::zero (nprec
);
5541 minv
= valv
& cst2v
;
5545 /* Minimum unsigned value for equality is VAL & CST2
5546 (should be equal to VAL, otherwise we probably should
5547 have folded the comparison into false) and
5548 maximum unsigned value is VAL | ~CST2. */
5549 maxv
= valv
| ~cst2v
;
5554 tem
= valv
| ~cst2v
;
5555 /* If VAL is 0, handle (X & CST2) != 0 as (X & CST2) > 0U. */
5559 sgnbit
= wi::zero (nprec
);
5562 /* If (VAL | ~CST2) is all ones, handle it as
5563 (X & CST2) < VAL. */
5568 sgnbit
= wi::zero (nprec
);
5571 if (!cst2n
&& wi::neg_p (cst2v
))
5572 sgnbit
= wi::set_bit_in_zero (nprec
- 1, nprec
);
5581 if (tem
== wi::mask (nprec
- 1, false, nprec
))
5587 sgnbit
= wi::zero (nprec
);
5592 /* Minimum unsigned value for >= if (VAL & CST2) == VAL
5593 is VAL and maximum unsigned value is ~0. For signed
5594 comparison, if CST2 doesn't have most significant bit
5595 set, handle it similarly. If CST2 has MSB set,
5596 the minimum is the same, and maximum is ~0U/2. */
5599 /* If (VAL & CST2) != VAL, X & CST2 can't be equal to
5601 minv
= masked_increment (valv
, cst2v
, sgnbit
, nprec
);
5605 maxv
= wi::mask (nprec
- (cst2n
? 1 : 0), false, nprec
);
5611 /* Find out smallest MINV where MINV > VAL
5612 && (MINV & CST2) == MINV, if any. If VAL is signed and
5613 CST2 has MSB set, compute it biased by 1 << (nprec - 1). */
5614 minv
= masked_increment (valv
, cst2v
, sgnbit
, nprec
);
5617 maxv
= wi::mask (nprec
- (cst2n
? 1 : 0), false, nprec
);
5622 /* Minimum unsigned value for <= is 0 and maximum
5623 unsigned value is VAL | ~CST2 if (VAL & CST2) == VAL.
5624 Otherwise, find smallest VAL2 where VAL2 > VAL
5625 && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
5627 For signed comparison, if CST2 doesn't have most
5628 significant bit set, handle it similarly. If CST2 has
5629 MSB set, the maximum is the same and minimum is INT_MIN. */
5634 maxv
= masked_increment (valv
, cst2v
, sgnbit
, nprec
);
5646 /* Minimum unsigned value for < is 0 and maximum
5647 unsigned value is (VAL-1) | ~CST2 if (VAL & CST2) == VAL.
5648 Otherwise, find smallest VAL2 where VAL2 > VAL
5649 && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
5651 For signed comparison, if CST2 doesn't have most
5652 significant bit set, handle it similarly. If CST2 has
5653 MSB set, the maximum is the same and minimum is INT_MIN. */
5662 maxv
= masked_increment (valv
, cst2v
, sgnbit
, nprec
);
5676 && (maxv
- minv
) != -1)
5678 tree tmp
, new_val
, type
;
5681 for (i
= 0; i
< 2; i
++)
5684 wide_int maxv2
= maxv
;
5686 type
= TREE_TYPE (names
[i
]);
5687 if (!TYPE_UNSIGNED (type
))
5689 type
= build_nonstandard_integer_type (nprec
, 1);
5690 tmp
= build1 (NOP_EXPR
, type
, names
[i
]);
5694 tmp
= build2 (PLUS_EXPR
, type
, tmp
,
5695 wide_int_to_tree (type
, -minv
));
5696 maxv2
= maxv
- minv
;
5698 new_val
= wide_int_to_tree (type
, maxv2
);
5702 fprintf (dump_file
, "Adding assert for ");
5703 print_generic_expr (dump_file
, names
[i
], 0);
5704 fprintf (dump_file
, " from ");
5705 print_generic_expr (dump_file
, tmp
, 0);
5706 fprintf (dump_file
, "\n");
5709 register_new_assert_for (names
[i
], tmp
, LE_EXPR
,
5710 new_val
, NULL
, e
, bsi
);
5717 /* OP is an operand of a truth value expression which is known to have
5718 a particular value. Register any asserts for OP and for any
5719 operands in OP's defining statement.
5721 If CODE is EQ_EXPR, then we want to register OP is zero (false),
5722 if CODE is NE_EXPR, then we want to register OP is nonzero (true). */
5725 register_edge_assert_for_1 (tree op
, enum tree_code code
,
5726 edge e
, gimple_stmt_iterator bsi
)
5730 enum tree_code rhs_code
;
5732 /* We only care about SSA_NAMEs. */
5733 if (TREE_CODE (op
) != SSA_NAME
)
5736 /* We know that OP will have a zero or nonzero value. If OP is used
5737 more than once go ahead and register an assert for OP. */
5738 if (live_on_edge (e
, op
)
5739 && !has_single_use (op
))
5741 val
= build_int_cst (TREE_TYPE (op
), 0);
5742 register_new_assert_for (op
, op
, code
, val
, NULL
, e
, bsi
);
5745 /* Now look at how OP is set. If it's set from a comparison,
5746 a truth operation or some bit operations, then we may be able
5747 to register information about the operands of that assignment. */
5748 op_def
= SSA_NAME_DEF_STMT (op
);
5749 if (gimple_code (op_def
) != GIMPLE_ASSIGN
)
5752 rhs_code
= gimple_assign_rhs_code (op_def
);
5754 if (TREE_CODE_CLASS (rhs_code
) == tcc_comparison
)
5756 bool invert
= (code
== EQ_EXPR
? true : false);
5757 tree op0
= gimple_assign_rhs1 (op_def
);
5758 tree op1
= gimple_assign_rhs2 (op_def
);
5760 if (TREE_CODE (op0
) == SSA_NAME
)
5761 register_edge_assert_for_2 (op0
, e
, bsi
, rhs_code
, op0
, op1
, invert
);
5762 if (TREE_CODE (op1
) == SSA_NAME
)
5763 register_edge_assert_for_2 (op1
, e
, bsi
, rhs_code
, op0
, op1
, invert
);
5765 else if ((code
== NE_EXPR
5766 && gimple_assign_rhs_code (op_def
) == BIT_AND_EXPR
)
5768 && gimple_assign_rhs_code (op_def
) == BIT_IOR_EXPR
))
5770 /* Recurse on each operand. */
5771 tree op0
= gimple_assign_rhs1 (op_def
);
5772 tree op1
= gimple_assign_rhs2 (op_def
);
5773 if (TREE_CODE (op0
) == SSA_NAME
5774 && has_single_use (op0
))
5775 register_edge_assert_for_1 (op0
, code
, e
, bsi
);
5776 if (TREE_CODE (op1
) == SSA_NAME
5777 && has_single_use (op1
))
5778 register_edge_assert_for_1 (op1
, code
, e
, bsi
);
5780 else if (gimple_assign_rhs_code (op_def
) == BIT_NOT_EXPR
5781 && TYPE_PRECISION (TREE_TYPE (gimple_assign_lhs (op_def
))) == 1)
5783 /* Recurse, flipping CODE. */
5784 code
= invert_tree_comparison (code
, false);
5785 register_edge_assert_for_1 (gimple_assign_rhs1 (op_def
), code
, e
, bsi
);
5787 else if (gimple_assign_rhs_code (op_def
) == SSA_NAME
)
5789 /* Recurse through the copy. */
5790 register_edge_assert_for_1 (gimple_assign_rhs1 (op_def
), code
, e
, bsi
);
5792 else if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (op_def
)))
5794 /* Recurse through the type conversion, unless it is a narrowing
5795 conversion or conversion from non-integral type. */
5796 tree rhs
= gimple_assign_rhs1 (op_def
);
5797 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs
))
5798 && (TYPE_PRECISION (TREE_TYPE (rhs
))
5799 <= TYPE_PRECISION (TREE_TYPE (op
))))
5800 register_edge_assert_for_1 (rhs
, code
, e
, bsi
);
5804 /* Try to register an edge assertion for SSA name NAME on edge E for
5805 the condition COND contributing to the conditional jump pointed to by
5809 register_edge_assert_for (tree name
, edge e
, gimple_stmt_iterator si
,
5810 enum tree_code cond_code
, tree cond_op0
,
5814 enum tree_code comp_code
;
5815 bool is_else_edge
= (e
->flags
& EDGE_FALSE_VALUE
) != 0;
5817 /* Do not attempt to infer anything in names that flow through
5819 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name
))
5822 if (!extract_code_and_val_from_cond_with_ops (name
, cond_code
,
5828 /* Register ASSERT_EXPRs for name. */
5829 register_edge_assert_for_2 (name
, e
, si
, cond_code
, cond_op0
,
5830 cond_op1
, is_else_edge
);
5833 /* If COND is effectively an equality test of an SSA_NAME against
5834 the value zero or one, then we may be able to assert values
5835 for SSA_NAMEs which flow into COND. */
5837 /* In the case of NAME == 1 or NAME != 0, for BIT_AND_EXPR defining
5838 statement of NAME we can assert both operands of the BIT_AND_EXPR
5839 have nonzero value. */
5840 if (((comp_code
== EQ_EXPR
&& integer_onep (val
))
5841 || (comp_code
== NE_EXPR
&& integer_zerop (val
))))
5843 gimple def_stmt
= SSA_NAME_DEF_STMT (name
);
5845 if (is_gimple_assign (def_stmt
)
5846 && gimple_assign_rhs_code (def_stmt
) == BIT_AND_EXPR
)
5848 tree op0
= gimple_assign_rhs1 (def_stmt
);
5849 tree op1
= gimple_assign_rhs2 (def_stmt
);
5850 register_edge_assert_for_1 (op0
, NE_EXPR
, e
, si
);
5851 register_edge_assert_for_1 (op1
, NE_EXPR
, e
, si
);
5855 /* In the case of NAME == 0 or NAME != 1, for BIT_IOR_EXPR defining
5856 statement of NAME we can assert both operands of the BIT_IOR_EXPR
5858 if (((comp_code
== EQ_EXPR
&& integer_zerop (val
))
5859 || (comp_code
== NE_EXPR
&& integer_onep (val
))))
5861 gimple def_stmt
= SSA_NAME_DEF_STMT (name
);
5863 /* For BIT_IOR_EXPR only if NAME == 0 both operands have
5864 necessarily zero value, or if type-precision is one. */
5865 if (is_gimple_assign (def_stmt
)
5866 && (gimple_assign_rhs_code (def_stmt
) == BIT_IOR_EXPR
5867 && (TYPE_PRECISION (TREE_TYPE (name
)) == 1
5868 || comp_code
== EQ_EXPR
)))
5870 tree op0
= gimple_assign_rhs1 (def_stmt
);
5871 tree op1
= gimple_assign_rhs2 (def_stmt
);
5872 register_edge_assert_for_1 (op0
, EQ_EXPR
, e
, si
);
5873 register_edge_assert_for_1 (op1
, EQ_EXPR
, e
, si
);
5879 /* Determine whether the outgoing edges of BB should receive an
5880 ASSERT_EXPR for each of the operands of BB's LAST statement.
5881 The last statement of BB must be a COND_EXPR.
5883 If any of the sub-graphs rooted at BB have an interesting use of
5884 the predicate operands, an assert location node is added to the
5885 list of assertions for the corresponding operands. */
5888 find_conditional_asserts (basic_block bb
, gcond
*last
)
5890 gimple_stmt_iterator bsi
;
5896 bsi
= gsi_for_stmt (last
);
5898 /* Look for uses of the operands in each of the sub-graphs
5899 rooted at BB. We need to check each of the outgoing edges
5900 separately, so that we know what kind of ASSERT_EXPR to
5902 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
5907 /* Register the necessary assertions for each operand in the
5908 conditional predicate. */
5909 FOR_EACH_SSA_TREE_OPERAND (op
, last
, iter
, SSA_OP_USE
)
5910 register_edge_assert_for (op
, e
, bsi
,
5911 gimple_cond_code (last
),
5912 gimple_cond_lhs (last
),
5913 gimple_cond_rhs (last
));
5923 /* Compare two case labels sorting first by the destination bb index
5924 and then by the case value. */
5927 compare_case_labels (const void *p1
, const void *p2
)
5929 const struct case_info
*ci1
= (const struct case_info
*) p1
;
5930 const struct case_info
*ci2
= (const struct case_info
*) p2
;
5931 int idx1
= ci1
->bb
->index
;
5932 int idx2
= ci2
->bb
->index
;
5936 else if (idx1
== idx2
)
5938 /* Make sure the default label is first in a group. */
5939 if (!CASE_LOW (ci1
->expr
))
5941 else if (!CASE_LOW (ci2
->expr
))
5944 return tree_int_cst_compare (CASE_LOW (ci1
->expr
),
5945 CASE_LOW (ci2
->expr
));
5951 /* Determine whether the outgoing edges of BB should receive an
5952 ASSERT_EXPR for each of the operands of BB's LAST statement.
5953 The last statement of BB must be a SWITCH_EXPR.
5955 If any of the sub-graphs rooted at BB have an interesting use of
5956 the predicate operands, an assert location node is added to the
5957 list of assertions for the corresponding operands. */
5960 find_switch_asserts (basic_block bb
, gswitch
*last
)
5962 gimple_stmt_iterator bsi
;
5965 struct case_info
*ci
;
5966 size_t n
= gimple_switch_num_labels (last
);
5967 #if GCC_VERSION >= 4000
5970 /* Work around GCC 3.4 bug (PR 37086). */
5971 volatile unsigned int idx
;
5974 bsi
= gsi_for_stmt (last
);
5975 op
= gimple_switch_index (last
);
5976 if (TREE_CODE (op
) != SSA_NAME
)
5979 /* Build a vector of case labels sorted by destination label. */
5980 ci
= XNEWVEC (struct case_info
, n
);
5981 for (idx
= 0; idx
< n
; ++idx
)
5983 ci
[idx
].expr
= gimple_switch_label (last
, idx
);
5984 ci
[idx
].bb
= label_to_block (CASE_LABEL (ci
[idx
].expr
));
5986 qsort (ci
, n
, sizeof (struct case_info
), compare_case_labels
);
5988 for (idx
= 0; idx
< n
; ++idx
)
5991 tree cl
= ci
[idx
].expr
;
5992 basic_block cbb
= ci
[idx
].bb
;
5994 min
= CASE_LOW (cl
);
5995 max
= CASE_HIGH (cl
);
5997 /* If there are multiple case labels with the same destination
5998 we need to combine them to a single value range for the edge. */
5999 if (idx
+ 1 < n
&& cbb
== ci
[idx
+ 1].bb
)
6001 /* Skip labels until the last of the group. */
6004 } while (idx
< n
&& cbb
== ci
[idx
].bb
);
6007 /* Pick up the maximum of the case label range. */
6008 if (CASE_HIGH (ci
[idx
].expr
))
6009 max
= CASE_HIGH (ci
[idx
].expr
);
6011 max
= CASE_LOW (ci
[idx
].expr
);
6014 /* Nothing to do if the range includes the default label until we
6015 can register anti-ranges. */
6016 if (min
== NULL_TREE
)
6019 /* Find the edge to register the assert expr on. */
6020 e
= find_edge (bb
, cbb
);
6022 /* Register the necessary assertions for the operand in the
6024 register_edge_assert_for (op
, e
, bsi
,
6025 max
? GE_EXPR
: EQ_EXPR
,
6026 op
, fold_convert (TREE_TYPE (op
), min
));
6028 register_edge_assert_for (op
, e
, bsi
, LE_EXPR
, op
,
6029 fold_convert (TREE_TYPE (op
), max
));
6036 /* Traverse all the statements in block BB looking for statements that
6037 may generate useful assertions for the SSA names in their operand.
6038 If a statement produces a useful assertion A for name N_i, then the
6039 list of assertions already generated for N_i is scanned to
6040 determine if A is actually needed.
6042 If N_i already had the assertion A at a location dominating the
6043 current location, then nothing needs to be done. Otherwise, the
6044 new location for A is recorded instead.
6046 1- For every statement S in BB, all the variables used by S are
6047 added to bitmap FOUND_IN_SUBGRAPH.
6049 2- If statement S uses an operand N in a way that exposes a known
6050 value range for N, then if N was not already generated by an
6051 ASSERT_EXPR, create a new assert location for N. For instance,
6052 if N is a pointer and the statement dereferences it, we can
6053 assume that N is not NULL.
6055 3- COND_EXPRs are a special case of #2. We can derive range
6056 information from the predicate but need to insert different
6057 ASSERT_EXPRs for each of the sub-graphs rooted at the
6058 conditional block. If the last statement of BB is a conditional
6059 expression of the form 'X op Y', then
6061 a) Remove X and Y from the set FOUND_IN_SUBGRAPH.
6063 b) If the conditional is the only entry point to the sub-graph
6064 corresponding to the THEN_CLAUSE, recurse into it. On
6065 return, if X and/or Y are marked in FOUND_IN_SUBGRAPH, then
6066 an ASSERT_EXPR is added for the corresponding variable.
6068 c) Repeat step (b) on the ELSE_CLAUSE.
6070 d) Mark X and Y in FOUND_IN_SUBGRAPH.
6079 In this case, an assertion on the THEN clause is useful to
6080 determine that 'a' is always 9 on that edge. However, an assertion
6081 on the ELSE clause would be unnecessary.
6083 4- If BB does not end in a conditional expression, then we recurse
6084 into BB's dominator children.
6086 At the end of the recursive traversal, every SSA name will have a
6087 list of locations where ASSERT_EXPRs should be added. When a new
6088 location for name N is found, it is registered by calling
6089 register_new_assert_for. That function keeps track of all the
6090 registered assertions to prevent adding unnecessary assertions.
6091 For instance, if a pointer P_4 is dereferenced more than once in a
6092 dominator tree, only the location dominating all the dereference of
6093 P_4 will receive an ASSERT_EXPR. */
6096 find_assert_locations_1 (basic_block bb
, sbitmap live
)
6100 last
= last_stmt (bb
);
6102 /* If BB's last statement is a conditional statement involving integer
6103 operands, determine if we need to add ASSERT_EXPRs. */
6105 && gimple_code (last
) == GIMPLE_COND
6106 && !fp_predicate (last
)
6107 && !ZERO_SSA_OPERANDS (last
, SSA_OP_USE
))
6108 find_conditional_asserts (bb
, as_a
<gcond
*> (last
));
6110 /* If BB's last statement is a switch statement involving integer
6111 operands, determine if we need to add ASSERT_EXPRs. */
6113 && gimple_code (last
) == GIMPLE_SWITCH
6114 && !ZERO_SSA_OPERANDS (last
, SSA_OP_USE
))
6115 find_switch_asserts (bb
, as_a
<gswitch
*> (last
));
6117 /* Traverse all the statements in BB marking used names and looking
6118 for statements that may infer assertions for their used operands. */
6119 for (gimple_stmt_iterator si
= gsi_last_bb (bb
); !gsi_end_p (si
);
6126 stmt
= gsi_stmt (si
);
6128 if (is_gimple_debug (stmt
))
6131 /* See if we can derive an assertion for any of STMT's operands. */
6132 FOR_EACH_SSA_TREE_OPERAND (op
, stmt
, i
, SSA_OP_USE
)
6135 enum tree_code comp_code
;
6137 /* If op is not live beyond this stmt, do not bother to insert
6139 if (!bitmap_bit_p (live
, SSA_NAME_VERSION (op
)))
6142 /* If OP is used in such a way that we can infer a value
6143 range for it, and we don't find a previous assertion for
6144 it, create a new assertion location node for OP. */
6145 if (infer_value_range (stmt
, op
, &comp_code
, &value
))
6147 /* If we are able to infer a nonzero value range for OP,
6148 then walk backwards through the use-def chain to see if OP
6149 was set via a typecast.
6151 If so, then we can also infer a nonzero value range
6152 for the operand of the NOP_EXPR. */
6153 if (comp_code
== NE_EXPR
&& integer_zerop (value
))
6156 gimple def_stmt
= SSA_NAME_DEF_STMT (t
);
6158 while (is_gimple_assign (def_stmt
)
6159 && CONVERT_EXPR_CODE_P
6160 (gimple_assign_rhs_code (def_stmt
))
6162 (gimple_assign_rhs1 (def_stmt
)) == SSA_NAME
6164 (TREE_TYPE (gimple_assign_rhs1 (def_stmt
))))
6166 t
= gimple_assign_rhs1 (def_stmt
);
6167 def_stmt
= SSA_NAME_DEF_STMT (t
);
6169 /* Note we want to register the assert for the
6170 operand of the NOP_EXPR after SI, not after the
6172 if (! has_single_use (t
))
6173 register_new_assert_for (t
, t
, comp_code
, value
,
6178 register_new_assert_for (op
, op
, comp_code
, value
, bb
, NULL
, si
);
6183 FOR_EACH_SSA_TREE_OPERAND (op
, stmt
, i
, SSA_OP_USE
)
6184 bitmap_set_bit (live
, SSA_NAME_VERSION (op
));
6185 FOR_EACH_SSA_TREE_OPERAND (op
, stmt
, i
, SSA_OP_DEF
)
6186 bitmap_clear_bit (live
, SSA_NAME_VERSION (op
));
6189 /* Traverse all PHI nodes in BB, updating live. */
6190 for (gphi_iterator si
= gsi_start_phis (bb
); !gsi_end_p (si
);
6193 use_operand_p arg_p
;
6195 gphi
*phi
= si
.phi ();
6196 tree res
= gimple_phi_result (phi
);
6198 if (virtual_operand_p (res
))
6201 FOR_EACH_PHI_ARG (arg_p
, phi
, i
, SSA_OP_USE
)
6203 tree arg
= USE_FROM_PTR (arg_p
);
6204 if (TREE_CODE (arg
) == SSA_NAME
)
6205 bitmap_set_bit (live
, SSA_NAME_VERSION (arg
));
6208 bitmap_clear_bit (live
, SSA_NAME_VERSION (res
));
6212 /* Do an RPO walk over the function computing SSA name liveness
6213 on-the-fly and deciding on assert expressions to insert. */
6216 find_assert_locations (void)
6218 int *rpo
= XNEWVEC (int, last_basic_block_for_fn (cfun
));
6219 int *bb_rpo
= XNEWVEC (int, last_basic_block_for_fn (cfun
));
6220 int *last_rpo
= XCNEWVEC (int, last_basic_block_for_fn (cfun
));
6223 live
= XCNEWVEC (sbitmap
, last_basic_block_for_fn (cfun
));
6224 rpo_cnt
= pre_and_rev_post_order_compute (NULL
, rpo
, false);
6225 for (i
= 0; i
< rpo_cnt
; ++i
)
6228 /* Pre-seed loop latch liveness from loop header PHI nodes. Due to
6229 the order we compute liveness and insert asserts we otherwise
6230 fail to insert asserts into the loop latch. */
6232 FOR_EACH_LOOP (loop
, 0)
6234 i
= loop
->latch
->index
;
6235 unsigned int j
= single_succ_edge (loop
->latch
)->dest_idx
;
6236 for (gphi_iterator gsi
= gsi_start_phis (loop
->header
);
6237 !gsi_end_p (gsi
); gsi_next (&gsi
))
6239 gphi
*phi
= gsi
.phi ();
6240 if (virtual_operand_p (gimple_phi_result (phi
)))
6242 tree arg
= gimple_phi_arg_def (phi
, j
);
6243 if (TREE_CODE (arg
) == SSA_NAME
)
6245 if (live
[i
] == NULL
)
6247 live
[i
] = sbitmap_alloc (num_ssa_names
);
6248 bitmap_clear (live
[i
]);
6250 bitmap_set_bit (live
[i
], SSA_NAME_VERSION (arg
));
6255 for (i
= rpo_cnt
- 1; i
>= 0; --i
)
6257 basic_block bb
= BASIC_BLOCK_FOR_FN (cfun
, rpo
[i
]);
6263 live
[rpo
[i
]] = sbitmap_alloc (num_ssa_names
);
6264 bitmap_clear (live
[rpo
[i
]]);
6267 /* Process BB and update the live information with uses in
6269 find_assert_locations_1 (bb
, live
[rpo
[i
]]);
6271 /* Merge liveness into the predecessor blocks and free it. */
6272 if (!bitmap_empty_p (live
[rpo
[i
]]))
6275 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
6277 int pred
= e
->src
->index
;
6278 if ((e
->flags
& EDGE_DFS_BACK
) || pred
== ENTRY_BLOCK
)
6283 live
[pred
] = sbitmap_alloc (num_ssa_names
);
6284 bitmap_clear (live
[pred
]);
6286 bitmap_ior (live
[pred
], live
[pred
], live
[rpo
[i
]]);
6288 if (bb_rpo
[pred
] < pred_rpo
)
6289 pred_rpo
= bb_rpo
[pred
];
6292 /* Record the RPO number of the last visited block that needs
6293 live information from this block. */
6294 last_rpo
[rpo
[i
]] = pred_rpo
;
6298 sbitmap_free (live
[rpo
[i
]]);
6299 live
[rpo
[i
]] = NULL
;
6302 /* We can free all successors live bitmaps if all their
6303 predecessors have been visited already. */
6304 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
6305 if (last_rpo
[e
->dest
->index
] == i
6306 && live
[e
->dest
->index
])
6308 sbitmap_free (live
[e
->dest
->index
]);
6309 live
[e
->dest
->index
] = NULL
;
6314 XDELETEVEC (bb_rpo
);
6315 XDELETEVEC (last_rpo
);
6316 for (i
= 0; i
< last_basic_block_for_fn (cfun
); ++i
)
6318 sbitmap_free (live
[i
]);
6322 /* Create an ASSERT_EXPR for NAME and insert it in the location
6323 indicated by LOC. Return true if we made any edge insertions. */
6326 process_assert_insertions_for (tree name
, assert_locus_t loc
)
6328 /* Build the comparison expression NAME_i COMP_CODE VAL. */
6335 /* If we have X <=> X do not insert an assert expr for that. */
6336 if (loc
->expr
== loc
->val
)
6339 cond
= build2 (loc
->comp_code
, boolean_type_node
, loc
->expr
, loc
->val
);
6340 assert_stmt
= build_assert_expr_for (cond
, name
);
6343 /* We have been asked to insert the assertion on an edge. This
6344 is used only by COND_EXPR and SWITCH_EXPR assertions. */
6345 gcc_checking_assert (gimple_code (gsi_stmt (loc
->si
)) == GIMPLE_COND
6346 || (gimple_code (gsi_stmt (loc
->si
))
6349 gsi_insert_on_edge (loc
->e
, assert_stmt
);
6353 /* Otherwise, we can insert right after LOC->SI iff the
6354 statement must not be the last statement in the block. */
6355 stmt
= gsi_stmt (loc
->si
);
6356 if (!stmt_ends_bb_p (stmt
))
6358 gsi_insert_after (&loc
->si
, assert_stmt
, GSI_SAME_STMT
);
6362 /* If STMT must be the last statement in BB, we can only insert new
6363 assertions on the non-abnormal edge out of BB. Note that since
6364 STMT is not control flow, there may only be one non-abnormal edge
6366 FOR_EACH_EDGE (e
, ei
, loc
->bb
->succs
)
6367 if (!(e
->flags
& EDGE_ABNORMAL
))
6369 gsi_insert_on_edge (e
, assert_stmt
);
6377 /* Process all the insertions registered for every name N_i registered
6378 in NEED_ASSERT_FOR. The list of assertions to be inserted are
6379 found in ASSERTS_FOR[i]. */
6382 process_assert_insertions (void)
6386 bool update_edges_p
= false;
6387 int num_asserts
= 0;
6389 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6390 dump_all_asserts (dump_file
);
6392 EXECUTE_IF_SET_IN_BITMAP (need_assert_for
, 0, i
, bi
)
6394 assert_locus_t loc
= asserts_for
[i
];
6399 assert_locus_t next
= loc
->next
;
6400 update_edges_p
|= process_assert_insertions_for (ssa_name (i
), loc
);
6408 gsi_commit_edge_inserts ();
6410 statistics_counter_event (cfun
, "Number of ASSERT_EXPR expressions inserted",
6415 /* Traverse the flowgraph looking for conditional jumps to insert range
6416 expressions. These range expressions are meant to provide information
6417 to optimizations that need to reason in terms of value ranges. They
6418 will not be expanded into RTL. For instance, given:
6427 this pass will transform the code into:
6433 x = ASSERT_EXPR <x, x < y>
6438 y = ASSERT_EXPR <y, x >= y>
6442 The idea is that once copy and constant propagation have run, other
6443 optimizations will be able to determine what ranges of values can 'x'
6444 take in different paths of the code, simply by checking the reaching
6445 definition of 'x'. */
6448 insert_range_assertions (void)
6450 need_assert_for
= BITMAP_ALLOC (NULL
);
6451 asserts_for
= XCNEWVEC (assert_locus_t
, num_ssa_names
);
6453 calculate_dominance_info (CDI_DOMINATORS
);
6455 find_assert_locations ();
6456 if (!bitmap_empty_p (need_assert_for
))
6458 process_assert_insertions ();
6459 update_ssa (TODO_update_ssa_no_phi
);
6462 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6464 fprintf (dump_file
, "\nSSA form after inserting ASSERT_EXPRs\n");
6465 dump_function_to_file (current_function_decl
, dump_file
, dump_flags
);
6469 BITMAP_FREE (need_assert_for
);
6472 /* Checks one ARRAY_REF in REF, located at LOCUS. Ignores flexible arrays
6473 and "struct" hacks. If VRP can determine that the
6474 array subscript is a constant, check if it is outside valid
6475 range. If the array subscript is a RANGE, warn if it is
6476 non-overlapping with valid range.
6477 IGNORE_OFF_BY_ONE is true if the ARRAY_REF is inside a ADDR_EXPR. */
6480 check_array_ref (location_t location
, tree ref
, bool ignore_off_by_one
)
6482 value_range_t
* vr
= NULL
;
6483 tree low_sub
, up_sub
;
6484 tree low_bound
, up_bound
, up_bound_p1
;
6487 if (TREE_NO_WARNING (ref
))
6490 low_sub
= up_sub
= TREE_OPERAND (ref
, 1);
6491 up_bound
= array_ref_up_bound (ref
);
6493 /* Can not check flexible arrays. */
6495 || TREE_CODE (up_bound
) != INTEGER_CST
)
6498 /* Accesses to trailing arrays via pointers may access storage
6499 beyond the types array bounds. */
6500 base
= get_base_address (ref
);
6501 if ((warn_array_bounds
< 2)
6502 && base
&& TREE_CODE (base
) == MEM_REF
)
6504 tree cref
, next
= NULL_TREE
;
6506 if (TREE_CODE (TREE_OPERAND (ref
, 0)) != COMPONENT_REF
)
6509 cref
= TREE_OPERAND (ref
, 0);
6510 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (cref
, 0))) == RECORD_TYPE
)
6511 for (next
= DECL_CHAIN (TREE_OPERAND (cref
, 1));
6512 next
&& TREE_CODE (next
) != FIELD_DECL
;
6513 next
= DECL_CHAIN (next
))
6516 /* If this is the last field in a struct type or a field in a
6517 union type do not warn. */
6522 low_bound
= array_ref_low_bound (ref
);
6523 up_bound_p1
= int_const_binop (PLUS_EXPR
, up_bound
,
6524 build_int_cst (TREE_TYPE (up_bound
), 1));
6526 if (TREE_CODE (low_sub
) == SSA_NAME
)
6528 vr
= get_value_range (low_sub
);
6529 if (vr
->type
== VR_RANGE
|| vr
->type
== VR_ANTI_RANGE
)
6531 low_sub
= vr
->type
== VR_RANGE
? vr
->max
: vr
->min
;
6532 up_sub
= vr
->type
== VR_RANGE
? vr
->min
: vr
->max
;
6536 if (vr
&& vr
->type
== VR_ANTI_RANGE
)
6538 if (TREE_CODE (up_sub
) == INTEGER_CST
6539 && tree_int_cst_lt (up_bound
, up_sub
)
6540 && TREE_CODE (low_sub
) == INTEGER_CST
6541 && tree_int_cst_lt (low_sub
, low_bound
))
6543 warning_at (location
, OPT_Warray_bounds
,
6544 "array subscript is outside array bounds");
6545 TREE_NO_WARNING (ref
) = 1;
6548 else if (TREE_CODE (up_sub
) == INTEGER_CST
6549 && (ignore_off_by_one
6550 ? (tree_int_cst_lt (up_bound
, up_sub
)
6551 && !tree_int_cst_equal (up_bound_p1
, up_sub
))
6552 : (tree_int_cst_lt (up_bound
, up_sub
)
6553 || tree_int_cst_equal (up_bound_p1
, up_sub
))))
6555 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6557 fprintf (dump_file
, "Array bound warning for ");
6558 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, ref
);
6559 fprintf (dump_file
, "\n");
6561 warning_at (location
, OPT_Warray_bounds
,
6562 "array subscript is above array bounds");
6563 TREE_NO_WARNING (ref
) = 1;
6565 else if (TREE_CODE (low_sub
) == INTEGER_CST
6566 && tree_int_cst_lt (low_sub
, low_bound
))
6568 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6570 fprintf (dump_file
, "Array bound warning for ");
6571 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, ref
);
6572 fprintf (dump_file
, "\n");
6574 warning_at (location
, OPT_Warray_bounds
,
6575 "array subscript is below array bounds");
6576 TREE_NO_WARNING (ref
) = 1;
6580 /* Searches if the expr T, located at LOCATION computes
6581 address of an ARRAY_REF, and call check_array_ref on it. */
6584 search_for_addr_array (tree t
, location_t location
)
6586 while (TREE_CODE (t
) == SSA_NAME
)
6588 gimple g
= SSA_NAME_DEF_STMT (t
);
6590 if (gimple_code (g
) != GIMPLE_ASSIGN
)
6593 if (get_gimple_rhs_class (gimple_assign_rhs_code (g
))
6594 != GIMPLE_SINGLE_RHS
)
6597 t
= gimple_assign_rhs1 (g
);
6601 /* We are only interested in addresses of ARRAY_REF's. */
6602 if (TREE_CODE (t
) != ADDR_EXPR
)
6605 /* Check each ARRAY_REFs in the reference chain. */
6608 if (TREE_CODE (t
) == ARRAY_REF
)
6609 check_array_ref (location
, t
, true /*ignore_off_by_one*/);
6611 t
= TREE_OPERAND (t
, 0);
6613 while (handled_component_p (t
));
6615 if (TREE_CODE (t
) == MEM_REF
6616 && TREE_CODE (TREE_OPERAND (t
, 0)) == ADDR_EXPR
6617 && !TREE_NO_WARNING (t
))
6619 tree tem
= TREE_OPERAND (TREE_OPERAND (t
, 0), 0);
6620 tree low_bound
, up_bound
, el_sz
;
6622 if (TREE_CODE (TREE_TYPE (tem
)) != ARRAY_TYPE
6623 || TREE_CODE (TREE_TYPE (TREE_TYPE (tem
))) == ARRAY_TYPE
6624 || !TYPE_DOMAIN (TREE_TYPE (tem
)))
6627 low_bound
= TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (tem
)));
6628 up_bound
= TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (tem
)));
6629 el_sz
= TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (tem
)));
6631 || TREE_CODE (low_bound
) != INTEGER_CST
6633 || TREE_CODE (up_bound
) != INTEGER_CST
6635 || TREE_CODE (el_sz
) != INTEGER_CST
)
6638 idx
= mem_ref_offset (t
);
6639 idx
= wi::sdiv_trunc (idx
, wi::to_offset (el_sz
));
6640 if (wi::lts_p (idx
, 0))
6642 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6644 fprintf (dump_file
, "Array bound warning for ");
6645 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, t
);
6646 fprintf (dump_file
, "\n");
6648 warning_at (location
, OPT_Warray_bounds
,
6649 "array subscript is below array bounds");
6650 TREE_NO_WARNING (t
) = 1;
6652 else if (wi::gts_p (idx
, (wi::to_offset (up_bound
)
6653 - wi::to_offset (low_bound
) + 1)))
6655 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6657 fprintf (dump_file
, "Array bound warning for ");
6658 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, t
);
6659 fprintf (dump_file
, "\n");
6661 warning_at (location
, OPT_Warray_bounds
,
6662 "array subscript is above array bounds");
6663 TREE_NO_WARNING (t
) = 1;
6668 /* walk_tree() callback that checks if *TP is
6669 an ARRAY_REF inside an ADDR_EXPR (in which an array
6670 subscript one outside the valid range is allowed). Call
6671 check_array_ref for each ARRAY_REF found. The location is
6675 check_array_bounds (tree
*tp
, int *walk_subtree
, void *data
)
6678 struct walk_stmt_info
*wi
= (struct walk_stmt_info
*) data
;
6679 location_t location
;
6681 if (EXPR_HAS_LOCATION (t
))
6682 location
= EXPR_LOCATION (t
);
6685 location_t
*locp
= (location_t
*) wi
->info
;
6689 *walk_subtree
= TRUE
;
6691 if (TREE_CODE (t
) == ARRAY_REF
)
6692 check_array_ref (location
, t
, false /*ignore_off_by_one*/);
6694 if (TREE_CODE (t
) == MEM_REF
6695 || (TREE_CODE (t
) == RETURN_EXPR
&& TREE_OPERAND (t
, 0)))
6696 search_for_addr_array (TREE_OPERAND (t
, 0), location
);
6698 if (TREE_CODE (t
) == ADDR_EXPR
)
6699 *walk_subtree
= FALSE
;
6704 /* Walk over all statements of all reachable BBs and call check_array_bounds
6708 check_all_array_refs (void)
6711 gimple_stmt_iterator si
;
6713 FOR_EACH_BB_FN (bb
, cfun
)
6717 bool executable
= false;
6719 /* Skip blocks that were found to be unreachable. */
6720 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
6721 executable
|= !!(e
->flags
& EDGE_EXECUTABLE
);
6725 for (si
= gsi_start_bb (bb
); !gsi_end_p (si
); gsi_next (&si
))
6727 gimple stmt
= gsi_stmt (si
);
6728 struct walk_stmt_info wi
;
6729 if (!gimple_has_location (stmt
))
6732 if (is_gimple_call (stmt
))
6735 size_t n
= gimple_call_num_args (stmt
);
6736 for (i
= 0; i
< n
; i
++)
6738 tree arg
= gimple_call_arg (stmt
, i
);
6739 search_for_addr_array (arg
, gimple_location (stmt
));
6744 memset (&wi
, 0, sizeof (wi
));
6745 wi
.info
= CONST_CAST (void *, (const void *)
6746 gimple_location_ptr (stmt
));
6748 walk_gimple_op (gsi_stmt (si
),
6756 /* Return true if all imm uses of VAR are either in STMT, or
6757 feed (optionally through a chain of single imm uses) GIMPLE_COND
6758 in basic block COND_BB. */
6761 all_imm_uses_in_stmt_or_feed_cond (tree var
, gimple stmt
, basic_block cond_bb
)
6763 use_operand_p use_p
, use2_p
;
6764 imm_use_iterator iter
;
6766 FOR_EACH_IMM_USE_FAST (use_p
, iter
, var
)
6767 if (USE_STMT (use_p
) != stmt
)
6769 gimple use_stmt
= USE_STMT (use_p
), use_stmt2
;
6770 if (is_gimple_debug (use_stmt
))
6772 while (is_gimple_assign (use_stmt
)
6773 && TREE_CODE (gimple_assign_lhs (use_stmt
)) == SSA_NAME
6774 && single_imm_use (gimple_assign_lhs (use_stmt
),
6775 &use2_p
, &use_stmt2
))
6776 use_stmt
= use_stmt2
;
6777 if (gimple_code (use_stmt
) != GIMPLE_COND
6778 || gimple_bb (use_stmt
) != cond_bb
)
6791 __builtin_unreachable ();
6793 x_5 = ASSERT_EXPR <x_3, ...>;
6794 If x_3 has no other immediate uses (checked by caller),
6795 var is the x_3 var from ASSERT_EXPR, we can clear low 5 bits
6796 from the non-zero bitmask. */
6799 maybe_set_nonzero_bits (basic_block bb
, tree var
)
6801 edge e
= single_pred_edge (bb
);
6802 basic_block cond_bb
= e
->src
;
6803 gimple stmt
= last_stmt (cond_bb
);
6807 || gimple_code (stmt
) != GIMPLE_COND
6808 || gimple_cond_code (stmt
) != ((e
->flags
& EDGE_TRUE_VALUE
)
6809 ? EQ_EXPR
: NE_EXPR
)
6810 || TREE_CODE (gimple_cond_lhs (stmt
)) != SSA_NAME
6811 || !integer_zerop (gimple_cond_rhs (stmt
)))
6814 stmt
= SSA_NAME_DEF_STMT (gimple_cond_lhs (stmt
));
6815 if (!is_gimple_assign (stmt
)
6816 || gimple_assign_rhs_code (stmt
) != BIT_AND_EXPR
6817 || TREE_CODE (gimple_assign_rhs2 (stmt
)) != INTEGER_CST
)
6819 if (gimple_assign_rhs1 (stmt
) != var
)
6823 if (TREE_CODE (gimple_assign_rhs1 (stmt
)) != SSA_NAME
)
6825 stmt2
= SSA_NAME_DEF_STMT (gimple_assign_rhs1 (stmt
));
6826 if (!gimple_assign_cast_p (stmt2
)
6827 || gimple_assign_rhs1 (stmt2
) != var
6828 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (stmt2
))
6829 || (TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (stmt
)))
6830 != TYPE_PRECISION (TREE_TYPE (var
))))
6833 cst
= gimple_assign_rhs2 (stmt
);
6834 set_nonzero_bits (var
, wi::bit_and_not (get_nonzero_bits (var
), cst
));
6837 /* Convert range assertion expressions into the implied copies and
6838 copy propagate away the copies. Doing the trivial copy propagation
6839 here avoids the need to run the full copy propagation pass after
6842 FIXME, this will eventually lead to copy propagation removing the
6843 names that had useful range information attached to them. For
6844 instance, if we had the assertion N_i = ASSERT_EXPR <N_j, N_j > 3>,
6845 then N_i will have the range [3, +INF].
6847 However, by converting the assertion into the implied copy
6848 operation N_i = N_j, we will then copy-propagate N_j into the uses
6849 of N_i and lose the range information. We may want to hold on to
6850 ASSERT_EXPRs a little while longer as the ranges could be used in
6851 things like jump threading.
6853 The problem with keeping ASSERT_EXPRs around is that passes after
6854 VRP need to handle them appropriately.
6856 Another approach would be to make the range information a first
6857 class property of the SSA_NAME so that it can be queried from
6858 any pass. This is made somewhat more complex by the need for
6859 multiple ranges to be associated with one SSA_NAME. */
6862 remove_range_assertions (void)
6865 gimple_stmt_iterator si
;
6866 /* 1 if looking at ASSERT_EXPRs immediately at the beginning of
6867 a basic block preceeded by GIMPLE_COND branching to it and
6868 __builtin_trap, -1 if not yet checked, 0 otherwise. */
6871 /* Note that the BSI iterator bump happens at the bottom of the
6872 loop and no bump is necessary if we're removing the statement
6873 referenced by the current BSI. */
6874 FOR_EACH_BB_FN (bb
, cfun
)
6875 for (si
= gsi_after_labels (bb
), is_unreachable
= -1; !gsi_end_p (si
);)
6877 gimple stmt
= gsi_stmt (si
);
6880 if (is_gimple_assign (stmt
)
6881 && gimple_assign_rhs_code (stmt
) == ASSERT_EXPR
)
6883 tree lhs
= gimple_assign_lhs (stmt
);
6884 tree rhs
= gimple_assign_rhs1 (stmt
);
6886 tree cond
= fold (ASSERT_EXPR_COND (rhs
));
6887 use_operand_p use_p
;
6888 imm_use_iterator iter
;
6890 gcc_assert (cond
!= boolean_false_node
);
6892 var
= ASSERT_EXPR_VAR (rhs
);
6893 gcc_assert (TREE_CODE (var
) == SSA_NAME
);
6895 if (!POINTER_TYPE_P (TREE_TYPE (lhs
))
6896 && SSA_NAME_RANGE_INFO (lhs
))
6898 if (is_unreachable
== -1)
6901 if (single_pred_p (bb
)
6902 && assert_unreachable_fallthru_edge_p
6903 (single_pred_edge (bb
)))
6907 if (x_7 >= 10 && x_7 < 20)
6908 __builtin_unreachable ();
6909 x_8 = ASSERT_EXPR <x_7, ...>;
6910 if the only uses of x_7 are in the ASSERT_EXPR and
6911 in the condition. In that case, we can copy the
6912 range info from x_8 computed in this pass also
6915 && all_imm_uses_in_stmt_or_feed_cond (var
, stmt
,
6918 set_range_info (var
, SSA_NAME_RANGE_TYPE (lhs
),
6919 SSA_NAME_RANGE_INFO (lhs
)->get_min (),
6920 SSA_NAME_RANGE_INFO (lhs
)->get_max ());
6921 maybe_set_nonzero_bits (bb
, var
);
6925 /* Propagate the RHS into every use of the LHS. */
6926 FOR_EACH_IMM_USE_STMT (use_stmt
, iter
, lhs
)
6927 FOR_EACH_IMM_USE_ON_STMT (use_p
, iter
)
6928 SET_USE (use_p
, var
);
6930 /* And finally, remove the copy, it is not needed. */
6931 gsi_remove (&si
, true);
6932 release_defs (stmt
);
6936 if (!is_gimple_debug (gsi_stmt (si
)))
6944 /* Return true if STMT is interesting for VRP. */
6947 stmt_interesting_for_vrp (gimple stmt
)
6949 if (gimple_code (stmt
) == GIMPLE_PHI
)
6951 tree res
= gimple_phi_result (stmt
);
6952 return (!virtual_operand_p (res
)
6953 && (INTEGRAL_TYPE_P (TREE_TYPE (res
))
6954 || POINTER_TYPE_P (TREE_TYPE (res
))));
6956 else if (is_gimple_assign (stmt
) || is_gimple_call (stmt
))
6958 tree lhs
= gimple_get_lhs (stmt
);
6960 /* In general, assignments with virtual operands are not useful
6961 for deriving ranges, with the obvious exception of calls to
6962 builtin functions. */
6963 if (lhs
&& TREE_CODE (lhs
) == SSA_NAME
6964 && (INTEGRAL_TYPE_P (TREE_TYPE (lhs
))
6965 || POINTER_TYPE_P (TREE_TYPE (lhs
)))
6966 && (is_gimple_call (stmt
)
6967 || !gimple_vuse (stmt
)))
6969 else if (is_gimple_call (stmt
) && gimple_call_internal_p (stmt
))
6970 switch (gimple_call_internal_fn (stmt
))
6972 case IFN_ADD_OVERFLOW
:
6973 case IFN_SUB_OVERFLOW
:
6974 case IFN_MUL_OVERFLOW
:
6975 /* These internal calls return _Complex integer type,
6976 but are interesting to VRP nevertheless. */
6977 if (lhs
&& TREE_CODE (lhs
) == SSA_NAME
)
6984 else if (gimple_code (stmt
) == GIMPLE_COND
6985 || gimple_code (stmt
) == GIMPLE_SWITCH
)
6992 /* Initialize local data structures for VRP. */
6995 vrp_initialize (void)
6999 values_propagated
= false;
7000 num_vr_values
= num_ssa_names
;
7001 vr_value
= XCNEWVEC (value_range_t
*, num_vr_values
);
7002 vr_phi_edge_counts
= XCNEWVEC (int, num_ssa_names
);
7004 FOR_EACH_BB_FN (bb
, cfun
)
7006 for (gphi_iterator si
= gsi_start_phis (bb
); !gsi_end_p (si
);
7009 gphi
*phi
= si
.phi ();
7010 if (!stmt_interesting_for_vrp (phi
))
7012 tree lhs
= PHI_RESULT (phi
);
7013 set_value_range_to_varying (get_value_range (lhs
));
7014 prop_set_simulate_again (phi
, false);
7017 prop_set_simulate_again (phi
, true);
7020 for (gimple_stmt_iterator si
= gsi_start_bb (bb
); !gsi_end_p (si
);
7023 gimple stmt
= gsi_stmt (si
);
7025 /* If the statement is a control insn, then we do not
7026 want to avoid simulating the statement once. Failure
7027 to do so means that those edges will never get added. */
7028 if (stmt_ends_bb_p (stmt
))
7029 prop_set_simulate_again (stmt
, true);
7030 else if (!stmt_interesting_for_vrp (stmt
))
7034 FOR_EACH_SSA_TREE_OPERAND (def
, stmt
, i
, SSA_OP_DEF
)
7035 set_value_range_to_varying (get_value_range (def
));
7036 prop_set_simulate_again (stmt
, false);
7039 prop_set_simulate_again (stmt
, true);
7044 /* Return the singleton value-range for NAME or NAME. */
7047 vrp_valueize (tree name
)
7049 if (TREE_CODE (name
) == SSA_NAME
)
7051 value_range_t
*vr
= get_value_range (name
);
7052 if (vr
->type
== VR_RANGE
7053 && (vr
->min
== vr
->max
7054 || operand_equal_p (vr
->min
, vr
->max
, 0)))
7060 /* Return the singleton value-range for NAME if that is a constant
7061 but signal to not follow SSA edges. */
7064 vrp_valueize_1 (tree name
)
7066 if (TREE_CODE (name
) == SSA_NAME
)
7068 value_range_t
*vr
= get_value_range (name
);
7069 if (range_int_cst_singleton_p (vr
))
7071 /* If the definition may be simulated again we cannot follow
7072 this SSA edge as the SSA propagator does not necessarily
7073 re-visit the use. */
7074 gimple def_stmt
= SSA_NAME_DEF_STMT (name
);
7075 if (prop_simulate_again_p (def_stmt
))
7081 /* Visit assignment STMT. If it produces an interesting range, record
7082 the SSA name in *OUTPUT_P. */
7084 static enum ssa_prop_result
7085 vrp_visit_assignment_or_call (gimple stmt
, tree
*output_p
)
7089 enum gimple_code code
= gimple_code (stmt
);
7090 lhs
= gimple_get_lhs (stmt
);
7092 /* We only keep track of ranges in integral and pointer types. */
7093 if (TREE_CODE (lhs
) == SSA_NAME
7094 && ((INTEGRAL_TYPE_P (TREE_TYPE (lhs
))
7095 /* It is valid to have NULL MIN/MAX values on a type. See
7096 build_range_type. */
7097 && TYPE_MIN_VALUE (TREE_TYPE (lhs
))
7098 && TYPE_MAX_VALUE (TREE_TYPE (lhs
)))
7099 || POINTER_TYPE_P (TREE_TYPE (lhs
))))
7101 value_range_t new_vr
= VR_INITIALIZER
;
7103 /* Try folding the statement to a constant first. */
7104 tree tem
= gimple_fold_stmt_to_constant_1 (stmt
, vrp_valueize
,
7106 if (tem
&& is_gimple_min_invariant (tem
))
7107 set_value_range_to_value (&new_vr
, tem
, NULL
);
7108 /* Then dispatch to value-range extracting functions. */
7109 else if (code
== GIMPLE_CALL
)
7110 extract_range_basic (&new_vr
, stmt
);
7112 extract_range_from_assignment (&new_vr
, as_a
<gassign
*> (stmt
));
7114 if (update_value_range (lhs
, &new_vr
))
7118 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7120 fprintf (dump_file
, "Found new range for ");
7121 print_generic_expr (dump_file
, lhs
, 0);
7122 fprintf (dump_file
, ": ");
7123 dump_value_range (dump_file
, &new_vr
);
7124 fprintf (dump_file
, "\n");
7127 if (new_vr
.type
== VR_VARYING
)
7128 return SSA_PROP_VARYING
;
7130 return SSA_PROP_INTERESTING
;
7133 return SSA_PROP_NOT_INTERESTING
;
7135 else if (is_gimple_call (stmt
) && gimple_call_internal_p (stmt
))
7136 switch (gimple_call_internal_fn (stmt
))
7138 case IFN_ADD_OVERFLOW
:
7139 case IFN_SUB_OVERFLOW
:
7140 case IFN_MUL_OVERFLOW
:
7141 /* These internal calls return _Complex integer type,
7142 which VRP does not track, but the immediate uses
7143 thereof might be interesting. */
7144 if (lhs
&& TREE_CODE (lhs
) == SSA_NAME
)
7146 imm_use_iterator iter
;
7147 use_operand_p use_p
;
7148 enum ssa_prop_result res
= SSA_PROP_VARYING
;
7150 set_value_range_to_varying (get_value_range (lhs
));
7152 FOR_EACH_IMM_USE_FAST (use_p
, iter
, lhs
)
7154 gimple use_stmt
= USE_STMT (use_p
);
7155 if (!is_gimple_assign (use_stmt
))
7157 enum tree_code rhs_code
= gimple_assign_rhs_code (use_stmt
);
7158 if (rhs_code
!= REALPART_EXPR
&& rhs_code
!= IMAGPART_EXPR
)
7160 tree rhs1
= gimple_assign_rhs1 (use_stmt
);
7161 tree use_lhs
= gimple_assign_lhs (use_stmt
);
7162 if (TREE_CODE (rhs1
) != rhs_code
7163 || TREE_OPERAND (rhs1
, 0) != lhs
7164 || TREE_CODE (use_lhs
) != SSA_NAME
7165 || !stmt_interesting_for_vrp (use_stmt
)
7166 || (!INTEGRAL_TYPE_P (TREE_TYPE (use_lhs
))
7167 || !TYPE_MIN_VALUE (TREE_TYPE (use_lhs
))
7168 || !TYPE_MAX_VALUE (TREE_TYPE (use_lhs
))))
7171 /* If there is a change in the value range for any of the
7172 REALPART_EXPR/IMAGPART_EXPR immediate uses, return
7173 SSA_PROP_INTERESTING. If there are any REALPART_EXPR
7174 or IMAGPART_EXPR immediate uses, but none of them have
7175 a change in their value ranges, return
7176 SSA_PROP_NOT_INTERESTING. If there are no
7177 {REAL,IMAG}PART_EXPR uses at all,
7178 return SSA_PROP_VARYING. */
7179 value_range_t new_vr
= VR_INITIALIZER
;
7180 extract_range_basic (&new_vr
, use_stmt
);
7181 value_range_t
*old_vr
= get_value_range (use_lhs
);
7182 if (old_vr
->type
!= new_vr
.type
7183 || !vrp_operand_equal_p (old_vr
->min
, new_vr
.min
)
7184 || !vrp_operand_equal_p (old_vr
->max
, new_vr
.max
)
7185 || !vrp_bitmap_equal_p (old_vr
->equiv
, new_vr
.equiv
))
7186 res
= SSA_PROP_INTERESTING
;
7188 res
= SSA_PROP_NOT_INTERESTING
;
7189 BITMAP_FREE (new_vr
.equiv
);
7190 if (res
== SSA_PROP_INTERESTING
)
7204 /* Every other statement produces no useful ranges. */
7205 FOR_EACH_SSA_TREE_OPERAND (def
, stmt
, iter
, SSA_OP_DEF
)
7206 set_value_range_to_varying (get_value_range (def
));
7208 return SSA_PROP_VARYING
;
7211 /* Helper that gets the value range of the SSA_NAME with version I
7212 or a symbolic range containing the SSA_NAME only if the value range
7213 is varying or undefined. */
7215 static inline value_range_t
7216 get_vr_for_comparison (int i
)
7218 value_range_t vr
= *get_value_range (ssa_name (i
));
7220 /* If name N_i does not have a valid range, use N_i as its own
7221 range. This allows us to compare against names that may
7222 have N_i in their ranges. */
7223 if (vr
.type
== VR_VARYING
|| vr
.type
== VR_UNDEFINED
)
7226 vr
.min
= ssa_name (i
);
7227 vr
.max
= ssa_name (i
);
7233 /* Compare all the value ranges for names equivalent to VAR with VAL
7234 using comparison code COMP. Return the same value returned by
7235 compare_range_with_value, including the setting of
7236 *STRICT_OVERFLOW_P. */
7239 compare_name_with_value (enum tree_code comp
, tree var
, tree val
,
7240 bool *strict_overflow_p
)
7246 int used_strict_overflow
;
7248 value_range_t equiv_vr
;
7250 /* Get the set of equivalences for VAR. */
7251 e
= get_value_range (var
)->equiv
;
7253 /* Start at -1. Set it to 0 if we do a comparison without relying
7254 on overflow, or 1 if all comparisons rely on overflow. */
7255 used_strict_overflow
= -1;
7257 /* Compare vars' value range with val. */
7258 equiv_vr
= get_vr_for_comparison (SSA_NAME_VERSION (var
));
7260 retval
= compare_range_with_value (comp
, &equiv_vr
, val
, &sop
);
7262 used_strict_overflow
= sop
? 1 : 0;
7264 /* If the equiv set is empty we have done all work we need to do. */
7268 && used_strict_overflow
> 0)
7269 *strict_overflow_p
= true;
7273 EXECUTE_IF_SET_IN_BITMAP (e
, 0, i
, bi
)
7275 equiv_vr
= get_vr_for_comparison (i
);
7277 t
= compare_range_with_value (comp
, &equiv_vr
, val
, &sop
);
7280 /* If we get different answers from different members
7281 of the equivalence set this check must be in a dead
7282 code region. Folding it to a trap representation
7283 would be correct here. For now just return don't-know. */
7293 used_strict_overflow
= 0;
7294 else if (used_strict_overflow
< 0)
7295 used_strict_overflow
= 1;
7300 && used_strict_overflow
> 0)
7301 *strict_overflow_p
= true;
7307 /* Given a comparison code COMP and names N1 and N2, compare all the
7308 ranges equivalent to N1 against all the ranges equivalent to N2
7309 to determine the value of N1 COMP N2. Return the same value
7310 returned by compare_ranges. Set *STRICT_OVERFLOW_P to indicate
7311 whether we relied on an overflow infinity in the comparison. */
7315 compare_names (enum tree_code comp
, tree n1
, tree n2
,
7316 bool *strict_overflow_p
)
7320 bitmap_iterator bi1
, bi2
;
7322 int used_strict_overflow
;
7323 static bitmap_obstack
*s_obstack
= NULL
;
7324 static bitmap s_e1
= NULL
, s_e2
= NULL
;
7326 /* Compare the ranges of every name equivalent to N1 against the
7327 ranges of every name equivalent to N2. */
7328 e1
= get_value_range (n1
)->equiv
;
7329 e2
= get_value_range (n2
)->equiv
;
7331 /* Use the fake bitmaps if e1 or e2 are not available. */
7332 if (s_obstack
== NULL
)
7334 s_obstack
= XNEW (bitmap_obstack
);
7335 bitmap_obstack_initialize (s_obstack
);
7336 s_e1
= BITMAP_ALLOC (s_obstack
);
7337 s_e2
= BITMAP_ALLOC (s_obstack
);
7344 /* Add N1 and N2 to their own set of equivalences to avoid
7345 duplicating the body of the loop just to check N1 and N2
7347 bitmap_set_bit (e1
, SSA_NAME_VERSION (n1
));
7348 bitmap_set_bit (e2
, SSA_NAME_VERSION (n2
));
7350 /* If the equivalence sets have a common intersection, then the two
7351 names can be compared without checking their ranges. */
7352 if (bitmap_intersect_p (e1
, e2
))
7354 bitmap_clear_bit (e1
, SSA_NAME_VERSION (n1
));
7355 bitmap_clear_bit (e2
, SSA_NAME_VERSION (n2
));
7357 return (comp
== EQ_EXPR
|| comp
== GE_EXPR
|| comp
== LE_EXPR
)
7359 : boolean_false_node
;
7362 /* Start at -1. Set it to 0 if we do a comparison without relying
7363 on overflow, or 1 if all comparisons rely on overflow. */
7364 used_strict_overflow
= -1;
7366 /* Otherwise, compare all the equivalent ranges. First, add N1 and
7367 N2 to their own set of equivalences to avoid duplicating the body
7368 of the loop just to check N1 and N2 ranges. */
7369 EXECUTE_IF_SET_IN_BITMAP (e1
, 0, i1
, bi1
)
7371 value_range_t vr1
= get_vr_for_comparison (i1
);
7373 t
= retval
= NULL_TREE
;
7374 EXECUTE_IF_SET_IN_BITMAP (e2
, 0, i2
, bi2
)
7378 value_range_t vr2
= get_vr_for_comparison (i2
);
7380 t
= compare_ranges (comp
, &vr1
, &vr2
, &sop
);
7383 /* If we get different answers from different members
7384 of the equivalence set this check must be in a dead
7385 code region. Folding it to a trap representation
7386 would be correct here. For now just return don't-know. */
7390 bitmap_clear_bit (e1
, SSA_NAME_VERSION (n1
));
7391 bitmap_clear_bit (e2
, SSA_NAME_VERSION (n2
));
7397 used_strict_overflow
= 0;
7398 else if (used_strict_overflow
< 0)
7399 used_strict_overflow
= 1;
7405 bitmap_clear_bit (e1
, SSA_NAME_VERSION (n1
));
7406 bitmap_clear_bit (e2
, SSA_NAME_VERSION (n2
));
7407 if (used_strict_overflow
> 0)
7408 *strict_overflow_p
= true;
7413 /* None of the equivalent ranges are useful in computing this
7415 bitmap_clear_bit (e1
, SSA_NAME_VERSION (n1
));
7416 bitmap_clear_bit (e2
, SSA_NAME_VERSION (n2
));
7420 /* Helper function for vrp_evaluate_conditional_warnv. */
7423 vrp_evaluate_conditional_warnv_with_ops_using_ranges (enum tree_code code
,
7425 bool * strict_overflow_p
)
7427 value_range_t
*vr0
, *vr1
;
7429 vr0
= (TREE_CODE (op0
) == SSA_NAME
) ? get_value_range (op0
) : NULL
;
7430 vr1
= (TREE_CODE (op1
) == SSA_NAME
) ? get_value_range (op1
) : NULL
;
7432 tree res
= NULL_TREE
;
7434 res
= compare_ranges (code
, vr0
, vr1
, strict_overflow_p
);
7436 res
= compare_range_with_value (code
, vr0
, op1
, strict_overflow_p
);
7438 res
= (compare_range_with_value
7439 (swap_tree_comparison (code
), vr1
, op0
, strict_overflow_p
));
7443 /* Helper function for vrp_evaluate_conditional_warnv. */
7446 vrp_evaluate_conditional_warnv_with_ops (enum tree_code code
, tree op0
,
7447 tree op1
, bool use_equiv_p
,
7448 bool *strict_overflow_p
, bool *only_ranges
)
7452 *only_ranges
= true;
7454 /* We only deal with integral and pointer types. */
7455 if (!INTEGRAL_TYPE_P (TREE_TYPE (op0
))
7456 && !POINTER_TYPE_P (TREE_TYPE (op0
)))
7462 && (ret
= vrp_evaluate_conditional_warnv_with_ops_using_ranges
7463 (code
, op0
, op1
, strict_overflow_p
)))
7465 *only_ranges
= false;
7466 if (TREE_CODE (op0
) == SSA_NAME
&& TREE_CODE (op1
) == SSA_NAME
)
7467 return compare_names (code
, op0
, op1
, strict_overflow_p
);
7468 else if (TREE_CODE (op0
) == SSA_NAME
)
7469 return compare_name_with_value (code
, op0
, op1
, strict_overflow_p
);
7470 else if (TREE_CODE (op1
) == SSA_NAME
)
7471 return (compare_name_with_value
7472 (swap_tree_comparison (code
), op1
, op0
, strict_overflow_p
));
7475 return vrp_evaluate_conditional_warnv_with_ops_using_ranges (code
, op0
, op1
,
7480 /* Given (CODE OP0 OP1) within STMT, try to simplify it based on value range
7481 information. Return NULL if the conditional can not be evaluated.
7482 The ranges of all the names equivalent with the operands in COND
7483 will be used when trying to compute the value. If the result is
7484 based on undefined signed overflow, issue a warning if
7488 vrp_evaluate_conditional (enum tree_code code
, tree op0
, tree op1
, gimple stmt
)
7494 /* Some passes and foldings leak constants with overflow flag set
7495 into the IL. Avoid doing wrong things with these and bail out. */
7496 if ((TREE_CODE (op0
) == INTEGER_CST
7497 && TREE_OVERFLOW (op0
))
7498 || (TREE_CODE (op1
) == INTEGER_CST
7499 && TREE_OVERFLOW (op1
)))
7503 ret
= vrp_evaluate_conditional_warnv_with_ops (code
, op0
, op1
, true, &sop
,
7508 enum warn_strict_overflow_code wc
;
7509 const char* warnmsg
;
7511 if (is_gimple_min_invariant (ret
))
7513 wc
= WARN_STRICT_OVERFLOW_CONDITIONAL
;
7514 warnmsg
= G_("assuming signed overflow does not occur when "
7515 "simplifying conditional to constant");
7519 wc
= WARN_STRICT_OVERFLOW_COMPARISON
;
7520 warnmsg
= G_("assuming signed overflow does not occur when "
7521 "simplifying conditional");
7524 if (issue_strict_overflow_warning (wc
))
7526 location_t location
;
7528 if (!gimple_has_location (stmt
))
7529 location
= input_location
;
7531 location
= gimple_location (stmt
);
7532 warning_at (location
, OPT_Wstrict_overflow
, "%s", warnmsg
);
7536 if (warn_type_limits
7537 && ret
&& only_ranges
7538 && TREE_CODE_CLASS (code
) == tcc_comparison
7539 && TREE_CODE (op0
) == SSA_NAME
)
7541 /* If the comparison is being folded and the operand on the LHS
7542 is being compared against a constant value that is outside of
7543 the natural range of OP0's type, then the predicate will
7544 always fold regardless of the value of OP0. If -Wtype-limits
7545 was specified, emit a warning. */
7546 tree type
= TREE_TYPE (op0
);
7547 value_range_t
*vr0
= get_value_range (op0
);
7549 if (vr0
->type
== VR_RANGE
7550 && INTEGRAL_TYPE_P (type
)
7551 && vrp_val_is_min (vr0
->min
)
7552 && vrp_val_is_max (vr0
->max
)
7553 && is_gimple_min_invariant (op1
))
7555 location_t location
;
7557 if (!gimple_has_location (stmt
))
7558 location
= input_location
;
7560 location
= gimple_location (stmt
);
7562 warning_at (location
, OPT_Wtype_limits
,
7564 ? G_("comparison always false "
7565 "due to limited range of data type")
7566 : G_("comparison always true "
7567 "due to limited range of data type"));
7575 /* Visit conditional statement STMT. If we can determine which edge
7576 will be taken out of STMT's basic block, record it in
7577 *TAKEN_EDGE_P and return SSA_PROP_INTERESTING. Otherwise, return
7578 SSA_PROP_VARYING. */
7580 static enum ssa_prop_result
7581 vrp_visit_cond_stmt (gcond
*stmt
, edge
*taken_edge_p
)
7586 *taken_edge_p
= NULL
;
7588 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7593 fprintf (dump_file
, "\nVisiting conditional with predicate: ");
7594 print_gimple_stmt (dump_file
, stmt
, 0, 0);
7595 fprintf (dump_file
, "\nWith known ranges\n");
7597 FOR_EACH_SSA_TREE_OPERAND (use
, stmt
, i
, SSA_OP_USE
)
7599 fprintf (dump_file
, "\t");
7600 print_generic_expr (dump_file
, use
, 0);
7601 fprintf (dump_file
, ": ");
7602 dump_value_range (dump_file
, vr_value
[SSA_NAME_VERSION (use
)]);
7605 fprintf (dump_file
, "\n");
7608 /* Compute the value of the predicate COND by checking the known
7609 ranges of each of its operands.
7611 Note that we cannot evaluate all the equivalent ranges here
7612 because those ranges may not yet be final and with the current
7613 propagation strategy, we cannot determine when the value ranges
7614 of the names in the equivalence set have changed.
7616 For instance, given the following code fragment
7620 i_14 = ASSERT_EXPR <i_5, i_5 != 0>
7624 Assume that on the first visit to i_14, i_5 has the temporary
7625 range [8, 8] because the second argument to the PHI function is
7626 not yet executable. We derive the range ~[0, 0] for i_14 and the
7627 equivalence set { i_5 }. So, when we visit 'if (i_14 == 1)' for
7628 the first time, since i_14 is equivalent to the range [8, 8], we
7629 determine that the predicate is always false.
7631 On the next round of propagation, i_13 is determined to be
7632 VARYING, which causes i_5 to drop down to VARYING. So, another
7633 visit to i_14 is scheduled. In this second visit, we compute the
7634 exact same range and equivalence set for i_14, namely ~[0, 0] and
7635 { i_5 }. But we did not have the previous range for i_5
7636 registered, so vrp_visit_assignment thinks that the range for
7637 i_14 has not changed. Therefore, the predicate 'if (i_14 == 1)'
7638 is not visited again, which stops propagation from visiting
7639 statements in the THEN clause of that if().
7641 To properly fix this we would need to keep the previous range
7642 value for the names in the equivalence set. This way we would've
7643 discovered that from one visit to the other i_5 changed from
7644 range [8, 8] to VR_VARYING.
7646 However, fixing this apparent limitation may not be worth the
7647 additional checking. Testing on several code bases (GCC, DLV,
7648 MICO, TRAMP3D and SPEC2000) showed that doing this results in
7649 4 more predicates folded in SPEC. */
7652 val
= vrp_evaluate_conditional_warnv_with_ops (gimple_cond_code (stmt
),
7653 gimple_cond_lhs (stmt
),
7654 gimple_cond_rhs (stmt
),
7659 *taken_edge_p
= find_taken_edge (gimple_bb (stmt
), val
);
7662 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7664 "\nIgnoring predicate evaluation because "
7665 "it assumes that signed overflow is undefined");
7670 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7672 fprintf (dump_file
, "\nPredicate evaluates to: ");
7673 if (val
== NULL_TREE
)
7674 fprintf (dump_file
, "DON'T KNOW\n");
7676 print_generic_stmt (dump_file
, val
, 0);
7679 return (*taken_edge_p
) ? SSA_PROP_INTERESTING
: SSA_PROP_VARYING
;
7682 /* Searches the case label vector VEC for the index *IDX of the CASE_LABEL
7683 that includes the value VAL. The search is restricted to the range
7684 [START_IDX, n - 1] where n is the size of VEC.
7686 If there is a CASE_LABEL for VAL, its index is placed in IDX and true is
7689 If there is no CASE_LABEL for VAL and there is one that is larger than VAL,
7690 it is placed in IDX and false is returned.
7692 If VAL is larger than any CASE_LABEL, n is placed on IDX and false is
7696 find_case_label_index (gswitch
*stmt
, size_t start_idx
, tree val
, size_t *idx
)
7698 size_t n
= gimple_switch_num_labels (stmt
);
7701 /* Find case label for minimum of the value range or the next one.
7702 At each iteration we are searching in [low, high - 1]. */
7704 for (low
= start_idx
, high
= n
; high
!= low
; )
7708 /* Note that i != high, so we never ask for n. */
7709 size_t i
= (high
+ low
) / 2;
7710 t
= gimple_switch_label (stmt
, i
);
7712 /* Cache the result of comparing CASE_LOW and val. */
7713 cmp
= tree_int_cst_compare (CASE_LOW (t
), val
);
7717 /* Ranges cannot be empty. */
7726 if (CASE_HIGH (t
) != NULL
7727 && tree_int_cst_compare (CASE_HIGH (t
), val
) >= 0)
7739 /* Searches the case label vector VEC for the range of CASE_LABELs that is used
7740 for values between MIN and MAX. The first index is placed in MIN_IDX. The
7741 last index is placed in MAX_IDX. If the range of CASE_LABELs is empty
7742 then MAX_IDX < MIN_IDX.
7743 Returns true if the default label is not needed. */
7746 find_case_label_range (gswitch
*stmt
, tree min
, tree max
, size_t *min_idx
,
7750 bool min_take_default
= !find_case_label_index (stmt
, 1, min
, &i
);
7751 bool max_take_default
= !find_case_label_index (stmt
, i
, max
, &j
);
7755 && max_take_default
)
7757 /* Only the default case label reached.
7758 Return an empty range. */
7765 bool take_default
= min_take_default
|| max_take_default
;
7769 if (max_take_default
)
7772 /* If the case label range is continuous, we do not need
7773 the default case label. Verify that. */
7774 high
= CASE_LOW (gimple_switch_label (stmt
, i
));
7775 if (CASE_HIGH (gimple_switch_label (stmt
, i
)))
7776 high
= CASE_HIGH (gimple_switch_label (stmt
, i
));
7777 for (k
= i
+ 1; k
<= j
; ++k
)
7779 low
= CASE_LOW (gimple_switch_label (stmt
, k
));
7780 if (!integer_onep (int_const_binop (MINUS_EXPR
, low
, high
)))
7782 take_default
= true;
7786 if (CASE_HIGH (gimple_switch_label (stmt
, k
)))
7787 high
= CASE_HIGH (gimple_switch_label (stmt
, k
));
7792 return !take_default
;
7796 /* Searches the case label vector VEC for the ranges of CASE_LABELs that are
7797 used in range VR. The indices are placed in MIN_IDX1, MAX_IDX, MIN_IDX2 and
7798 MAX_IDX2. If the ranges of CASE_LABELs are empty then MAX_IDX1 < MIN_IDX1.
7799 Returns true if the default label is not needed. */
7802 find_case_label_ranges (gswitch
*stmt
, value_range_t
*vr
, size_t *min_idx1
,
7803 size_t *max_idx1
, size_t *min_idx2
,
7807 unsigned int n
= gimple_switch_num_labels (stmt
);
7809 tree case_low
, case_high
;
7810 tree min
= vr
->min
, max
= vr
->max
;
7812 gcc_checking_assert (vr
->type
== VR_RANGE
|| vr
->type
== VR_ANTI_RANGE
);
7814 take_default
= !find_case_label_range (stmt
, min
, max
, &i
, &j
);
7816 /* Set second range to emtpy. */
7820 if (vr
->type
== VR_RANGE
)
7824 return !take_default
;
7827 /* Set first range to all case labels. */
7834 /* Make sure all the values of case labels [i , j] are contained in
7835 range [MIN, MAX]. */
7836 case_low
= CASE_LOW (gimple_switch_label (stmt
, i
));
7837 case_high
= CASE_HIGH (gimple_switch_label (stmt
, j
));
7838 if (tree_int_cst_compare (case_low
, min
) < 0)
7840 if (case_high
!= NULL_TREE
7841 && tree_int_cst_compare (max
, case_high
) < 0)
7847 /* If the range spans case labels [i, j], the corresponding anti-range spans
7848 the labels [1, i - 1] and [j + 1, n - 1]. */
7874 /* Visit switch statement STMT. If we can determine which edge
7875 will be taken out of STMT's basic block, record it in
7876 *TAKEN_EDGE_P and return SSA_PROP_INTERESTING. Otherwise, return
7877 SSA_PROP_VARYING. */
7879 static enum ssa_prop_result
7880 vrp_visit_switch_stmt (gswitch
*stmt
, edge
*taken_edge_p
)
7884 size_t i
= 0, j
= 0, k
, l
;
7887 *taken_edge_p
= NULL
;
7888 op
= gimple_switch_index (stmt
);
7889 if (TREE_CODE (op
) != SSA_NAME
)
7890 return SSA_PROP_VARYING
;
7892 vr
= get_value_range (op
);
7893 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7895 fprintf (dump_file
, "\nVisiting switch expression with operand ");
7896 print_generic_expr (dump_file
, op
, 0);
7897 fprintf (dump_file
, " with known range ");
7898 dump_value_range (dump_file
, vr
);
7899 fprintf (dump_file
, "\n");
7902 if ((vr
->type
!= VR_RANGE
7903 && vr
->type
!= VR_ANTI_RANGE
)
7904 || symbolic_range_p (vr
))
7905 return SSA_PROP_VARYING
;
7907 /* Find the single edge that is taken from the switch expression. */
7908 take_default
= !find_case_label_ranges (stmt
, vr
, &i
, &j
, &k
, &l
);
7910 /* Check if the range spans no CASE_LABEL. If so, we only reach the default
7914 gcc_assert (take_default
);
7915 val
= gimple_switch_default_label (stmt
);
7919 /* Check if labels with index i to j and maybe the default label
7920 are all reaching the same label. */
7922 val
= gimple_switch_label (stmt
, i
);
7924 && CASE_LABEL (gimple_switch_default_label (stmt
))
7925 != CASE_LABEL (val
))
7927 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7928 fprintf (dump_file
, " not a single destination for this "
7930 return SSA_PROP_VARYING
;
7932 for (++i
; i
<= j
; ++i
)
7934 if (CASE_LABEL (gimple_switch_label (stmt
, i
)) != CASE_LABEL (val
))
7936 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7937 fprintf (dump_file
, " not a single destination for this "
7939 return SSA_PROP_VARYING
;
7944 if (CASE_LABEL (gimple_switch_label (stmt
, k
)) != CASE_LABEL (val
))
7946 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7947 fprintf (dump_file
, " not a single destination for this "
7949 return SSA_PROP_VARYING
;
7954 *taken_edge_p
= find_edge (gimple_bb (stmt
),
7955 label_to_block (CASE_LABEL (val
)));
7957 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7959 fprintf (dump_file
, " will take edge to ");
7960 print_generic_stmt (dump_file
, CASE_LABEL (val
), 0);
7963 return SSA_PROP_INTERESTING
;
7967 /* Evaluate statement STMT. If the statement produces a useful range,
7968 return SSA_PROP_INTERESTING and record the SSA name with the
7969 interesting range into *OUTPUT_P.
7971 If STMT is a conditional branch and we can determine its truth
7972 value, the taken edge is recorded in *TAKEN_EDGE_P.
7974 If STMT produces a varying value, return SSA_PROP_VARYING. */
7976 static enum ssa_prop_result
7977 vrp_visit_stmt (gimple stmt
, edge
*taken_edge_p
, tree
*output_p
)
7982 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7984 fprintf (dump_file
, "\nVisiting statement:\n");
7985 print_gimple_stmt (dump_file
, stmt
, 0, dump_flags
);
7988 if (!stmt_interesting_for_vrp (stmt
))
7989 gcc_assert (stmt_ends_bb_p (stmt
));
7990 else if (is_gimple_assign (stmt
) || is_gimple_call (stmt
))
7991 return vrp_visit_assignment_or_call (stmt
, output_p
);
7992 else if (gimple_code (stmt
) == GIMPLE_COND
)
7993 return vrp_visit_cond_stmt (as_a
<gcond
*> (stmt
), taken_edge_p
);
7994 else if (gimple_code (stmt
) == GIMPLE_SWITCH
)
7995 return vrp_visit_switch_stmt (as_a
<gswitch
*> (stmt
), taken_edge_p
);
7997 /* All other statements produce nothing of interest for VRP, so mark
7998 their outputs varying and prevent further simulation. */
7999 FOR_EACH_SSA_TREE_OPERAND (def
, stmt
, iter
, SSA_OP_DEF
)
8000 set_value_range_to_varying (get_value_range (def
));
8002 return SSA_PROP_VARYING
;
8005 /* Union the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
8006 { VR1TYPE, VR0MIN, VR0MAX } and store the result
8007 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
8008 possible such range. The resulting range is not canonicalized. */
8011 union_ranges (enum value_range_type
*vr0type
,
8012 tree
*vr0min
, tree
*vr0max
,
8013 enum value_range_type vr1type
,
8014 tree vr1min
, tree vr1max
)
8016 bool mineq
= operand_equal_p (*vr0min
, vr1min
, 0);
8017 bool maxeq
= operand_equal_p (*vr0max
, vr1max
, 0);
8019 /* [] is vr0, () is vr1 in the following classification comments. */
8023 if (*vr0type
== vr1type
)
8024 /* Nothing to do for equal ranges. */
8026 else if ((*vr0type
== VR_RANGE
8027 && vr1type
== VR_ANTI_RANGE
)
8028 || (*vr0type
== VR_ANTI_RANGE
8029 && vr1type
== VR_RANGE
))
8031 /* For anti-range with range union the result is varying. */
8037 else if (operand_less_p (*vr0max
, vr1min
) == 1
8038 || operand_less_p (vr1max
, *vr0min
) == 1)
8040 /* [ ] ( ) or ( ) [ ]
8041 If the ranges have an empty intersection, result of the union
8042 operation is the anti-range or if both are anti-ranges
8044 if (*vr0type
== VR_ANTI_RANGE
8045 && vr1type
== VR_ANTI_RANGE
)
8047 else if (*vr0type
== VR_ANTI_RANGE
8048 && vr1type
== VR_RANGE
)
8050 else if (*vr0type
== VR_RANGE
8051 && vr1type
== VR_ANTI_RANGE
)
8057 else if (*vr0type
== VR_RANGE
8058 && vr1type
== VR_RANGE
)
8060 /* The result is the convex hull of both ranges. */
8061 if (operand_less_p (*vr0max
, vr1min
) == 1)
8063 /* If the result can be an anti-range, create one. */
8064 if (TREE_CODE (*vr0max
) == INTEGER_CST
8065 && TREE_CODE (vr1min
) == INTEGER_CST
8066 && vrp_val_is_min (*vr0min
)
8067 && vrp_val_is_max (vr1max
))
8069 tree min
= int_const_binop (PLUS_EXPR
,
8071 build_int_cst (TREE_TYPE (*vr0max
), 1));
8072 tree max
= int_const_binop (MINUS_EXPR
,
8074 build_int_cst (TREE_TYPE (vr1min
), 1));
8075 if (!operand_less_p (max
, min
))
8077 *vr0type
= VR_ANTI_RANGE
;
8089 /* If the result can be an anti-range, create one. */
8090 if (TREE_CODE (vr1max
) == INTEGER_CST
8091 && TREE_CODE (*vr0min
) == INTEGER_CST
8092 && vrp_val_is_min (vr1min
)
8093 && vrp_val_is_max (*vr0max
))
8095 tree min
= int_const_binop (PLUS_EXPR
,
8097 build_int_cst (TREE_TYPE (vr1max
), 1));
8098 tree max
= int_const_binop (MINUS_EXPR
,
8100 build_int_cst (TREE_TYPE (*vr0min
), 1));
8101 if (!operand_less_p (max
, min
))
8103 *vr0type
= VR_ANTI_RANGE
;
8117 else if ((maxeq
|| operand_less_p (vr1max
, *vr0max
) == 1)
8118 && (mineq
|| operand_less_p (*vr0min
, vr1min
) == 1))
8120 /* [ ( ) ] or [( ) ] or [ ( )] */
8121 if (*vr0type
== VR_RANGE
8122 && vr1type
== VR_RANGE
)
8124 else if (*vr0type
== VR_ANTI_RANGE
8125 && vr1type
== VR_ANTI_RANGE
)
8131 else if (*vr0type
== VR_ANTI_RANGE
8132 && vr1type
== VR_RANGE
)
8134 /* Arbitrarily choose the right or left gap. */
8135 if (!mineq
&& TREE_CODE (vr1min
) == INTEGER_CST
)
8136 *vr0max
= int_const_binop (MINUS_EXPR
, vr1min
,
8137 build_int_cst (TREE_TYPE (vr1min
), 1));
8138 else if (!maxeq
&& TREE_CODE (vr1max
) == INTEGER_CST
)
8139 *vr0min
= int_const_binop (PLUS_EXPR
, vr1max
,
8140 build_int_cst (TREE_TYPE (vr1max
), 1));
8144 else if (*vr0type
== VR_RANGE
8145 && vr1type
== VR_ANTI_RANGE
)
8146 /* The result covers everything. */
8151 else if ((maxeq
|| operand_less_p (*vr0max
, vr1max
) == 1)
8152 && (mineq
|| operand_less_p (vr1min
, *vr0min
) == 1))
8154 /* ( [ ] ) or ([ ] ) or ( [ ]) */
8155 if (*vr0type
== VR_RANGE
8156 && vr1type
== VR_RANGE
)
8162 else if (*vr0type
== VR_ANTI_RANGE
8163 && vr1type
== VR_ANTI_RANGE
)
8165 else if (*vr0type
== VR_RANGE
8166 && vr1type
== VR_ANTI_RANGE
)
8168 *vr0type
= VR_ANTI_RANGE
;
8169 if (!mineq
&& TREE_CODE (*vr0min
) == INTEGER_CST
)
8171 *vr0max
= int_const_binop (MINUS_EXPR
, *vr0min
,
8172 build_int_cst (TREE_TYPE (*vr0min
), 1));
8175 else if (!maxeq
&& TREE_CODE (*vr0max
) == INTEGER_CST
)
8177 *vr0min
= int_const_binop (PLUS_EXPR
, *vr0max
,
8178 build_int_cst (TREE_TYPE (*vr0max
), 1));
8184 else if (*vr0type
== VR_ANTI_RANGE
8185 && vr1type
== VR_RANGE
)
8186 /* The result covers everything. */
8191 else if ((operand_less_p (vr1min
, *vr0max
) == 1
8192 || operand_equal_p (vr1min
, *vr0max
, 0))
8193 && operand_less_p (*vr0min
, vr1min
) == 1
8194 && operand_less_p (*vr0max
, vr1max
) == 1)
8196 /* [ ( ] ) or [ ]( ) */
8197 if (*vr0type
== VR_RANGE
8198 && vr1type
== VR_RANGE
)
8200 else if (*vr0type
== VR_ANTI_RANGE
8201 && vr1type
== VR_ANTI_RANGE
)
8203 else if (*vr0type
== VR_ANTI_RANGE
8204 && vr1type
== VR_RANGE
)
8206 if (TREE_CODE (vr1min
) == INTEGER_CST
)
8207 *vr0max
= int_const_binop (MINUS_EXPR
, vr1min
,
8208 build_int_cst (TREE_TYPE (vr1min
), 1));
8212 else if (*vr0type
== VR_RANGE
8213 && vr1type
== VR_ANTI_RANGE
)
8215 if (TREE_CODE (*vr0max
) == INTEGER_CST
)
8218 *vr0min
= int_const_binop (PLUS_EXPR
, *vr0max
,
8219 build_int_cst (TREE_TYPE (*vr0max
), 1));
8228 else if ((operand_less_p (*vr0min
, vr1max
) == 1
8229 || operand_equal_p (*vr0min
, vr1max
, 0))
8230 && operand_less_p (vr1min
, *vr0min
) == 1
8231 && operand_less_p (vr1max
, *vr0max
) == 1)
8233 /* ( [ ) ] or ( )[ ] */
8234 if (*vr0type
== VR_RANGE
8235 && vr1type
== VR_RANGE
)
8237 else if (*vr0type
== VR_ANTI_RANGE
8238 && vr1type
== VR_ANTI_RANGE
)
8240 else if (*vr0type
== VR_ANTI_RANGE
8241 && vr1type
== VR_RANGE
)
8243 if (TREE_CODE (vr1max
) == INTEGER_CST
)
8244 *vr0min
= int_const_binop (PLUS_EXPR
, vr1max
,
8245 build_int_cst (TREE_TYPE (vr1max
), 1));
8249 else if (*vr0type
== VR_RANGE
8250 && vr1type
== VR_ANTI_RANGE
)
8252 if (TREE_CODE (*vr0min
) == INTEGER_CST
)
8256 *vr0max
= int_const_binop (MINUS_EXPR
, *vr0min
,
8257 build_int_cst (TREE_TYPE (*vr0min
), 1));
8271 *vr0type
= VR_VARYING
;
8272 *vr0min
= NULL_TREE
;
8273 *vr0max
= NULL_TREE
;
8276 /* Intersect the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
8277 { VR1TYPE, VR0MIN, VR0MAX } and store the result
8278 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
8279 possible such range. The resulting range is not canonicalized. */
8282 intersect_ranges (enum value_range_type
*vr0type
,
8283 tree
*vr0min
, tree
*vr0max
,
8284 enum value_range_type vr1type
,
8285 tree vr1min
, tree vr1max
)
8287 bool mineq
= operand_equal_p (*vr0min
, vr1min
, 0);
8288 bool maxeq
= operand_equal_p (*vr0max
, vr1max
, 0);
8290 /* [] is vr0, () is vr1 in the following classification comments. */
8294 if (*vr0type
== vr1type
)
8295 /* Nothing to do for equal ranges. */
8297 else if ((*vr0type
== VR_RANGE
8298 && vr1type
== VR_ANTI_RANGE
)
8299 || (*vr0type
== VR_ANTI_RANGE
8300 && vr1type
== VR_RANGE
))
8302 /* For anti-range with range intersection the result is empty. */
8303 *vr0type
= VR_UNDEFINED
;
8304 *vr0min
= NULL_TREE
;
8305 *vr0max
= NULL_TREE
;
8310 else if (operand_less_p (*vr0max
, vr1min
) == 1
8311 || operand_less_p (vr1max
, *vr0min
) == 1)
8313 /* [ ] ( ) or ( ) [ ]
8314 If the ranges have an empty intersection, the result of the
8315 intersect operation is the range for intersecting an
8316 anti-range with a range or empty when intersecting two ranges. */
8317 if (*vr0type
== VR_RANGE
8318 && vr1type
== VR_ANTI_RANGE
)
8320 else if (*vr0type
== VR_ANTI_RANGE
8321 && vr1type
== VR_RANGE
)
8327 else if (*vr0type
== VR_RANGE
8328 && vr1type
== VR_RANGE
)
8330 *vr0type
= VR_UNDEFINED
;
8331 *vr0min
= NULL_TREE
;
8332 *vr0max
= NULL_TREE
;
8334 else if (*vr0type
== VR_ANTI_RANGE
8335 && vr1type
== VR_ANTI_RANGE
)
8337 /* If the anti-ranges are adjacent to each other merge them. */
8338 if (TREE_CODE (*vr0max
) == INTEGER_CST
8339 && TREE_CODE (vr1min
) == INTEGER_CST
8340 && operand_less_p (*vr0max
, vr1min
) == 1
8341 && integer_onep (int_const_binop (MINUS_EXPR
,
8344 else if (TREE_CODE (vr1max
) == INTEGER_CST
8345 && TREE_CODE (*vr0min
) == INTEGER_CST
8346 && operand_less_p (vr1max
, *vr0min
) == 1
8347 && integer_onep (int_const_binop (MINUS_EXPR
,
8350 /* Else arbitrarily take VR0. */
8353 else if ((maxeq
|| operand_less_p (vr1max
, *vr0max
) == 1)
8354 && (mineq
|| operand_less_p (*vr0min
, vr1min
) == 1))
8356 /* [ ( ) ] or [( ) ] or [ ( )] */
8357 if (*vr0type
== VR_RANGE
8358 && vr1type
== VR_RANGE
)
8360 /* If both are ranges the result is the inner one. */
8365 else if (*vr0type
== VR_RANGE
8366 && vr1type
== VR_ANTI_RANGE
)
8368 /* Choose the right gap if the left one is empty. */
8371 if (TREE_CODE (vr1max
) == INTEGER_CST
)
8372 *vr0min
= int_const_binop (PLUS_EXPR
, vr1max
,
8373 build_int_cst (TREE_TYPE (vr1max
), 1));
8377 /* Choose the left gap if the right one is empty. */
8380 if (TREE_CODE (vr1min
) == INTEGER_CST
)
8381 *vr0max
= int_const_binop (MINUS_EXPR
, vr1min
,
8382 build_int_cst (TREE_TYPE (vr1min
), 1));
8386 /* Choose the anti-range if the range is effectively varying. */
8387 else if (vrp_val_is_min (*vr0min
)
8388 && vrp_val_is_max (*vr0max
))
8394 /* Else choose the range. */
8396 else if (*vr0type
== VR_ANTI_RANGE
8397 && vr1type
== VR_ANTI_RANGE
)
8398 /* If both are anti-ranges the result is the outer one. */
8400 else if (*vr0type
== VR_ANTI_RANGE
8401 && vr1type
== VR_RANGE
)
8403 /* The intersection is empty. */
8404 *vr0type
= VR_UNDEFINED
;
8405 *vr0min
= NULL_TREE
;
8406 *vr0max
= NULL_TREE
;
8411 else if ((maxeq
|| operand_less_p (*vr0max
, vr1max
) == 1)
8412 && (mineq
|| operand_less_p (vr1min
, *vr0min
) == 1))
8414 /* ( [ ] ) or ([ ] ) or ( [ ]) */
8415 if (*vr0type
== VR_RANGE
8416 && vr1type
== VR_RANGE
)
8417 /* Choose the inner range. */
8419 else if (*vr0type
== VR_ANTI_RANGE
8420 && vr1type
== VR_RANGE
)
8422 /* Choose the right gap if the left is empty. */
8425 *vr0type
= VR_RANGE
;
8426 if (TREE_CODE (*vr0max
) == INTEGER_CST
)
8427 *vr0min
= int_const_binop (PLUS_EXPR
, *vr0max
,
8428 build_int_cst (TREE_TYPE (*vr0max
), 1));
8433 /* Choose the left gap if the right is empty. */
8436 *vr0type
= VR_RANGE
;
8437 if (TREE_CODE (*vr0min
) == INTEGER_CST
)
8438 *vr0max
= int_const_binop (MINUS_EXPR
, *vr0min
,
8439 build_int_cst (TREE_TYPE (*vr0min
), 1));
8444 /* Choose the anti-range if the range is effectively varying. */
8445 else if (vrp_val_is_min (vr1min
)
8446 && vrp_val_is_max (vr1max
))
8448 /* Else choose the range. */
8456 else if (*vr0type
== VR_ANTI_RANGE
8457 && vr1type
== VR_ANTI_RANGE
)
8459 /* If both are anti-ranges the result is the outer one. */
8464 else if (vr1type
== VR_ANTI_RANGE
8465 && *vr0type
== VR_RANGE
)
8467 /* The intersection is empty. */
8468 *vr0type
= VR_UNDEFINED
;
8469 *vr0min
= NULL_TREE
;
8470 *vr0max
= NULL_TREE
;
8475 else if ((operand_less_p (vr1min
, *vr0max
) == 1
8476 || operand_equal_p (vr1min
, *vr0max
, 0))
8477 && operand_less_p (*vr0min
, vr1min
) == 1)
8479 /* [ ( ] ) or [ ]( ) */
8480 if (*vr0type
== VR_ANTI_RANGE
8481 && vr1type
== VR_ANTI_RANGE
)
8483 else if (*vr0type
== VR_RANGE
8484 && vr1type
== VR_RANGE
)
8486 else if (*vr0type
== VR_RANGE
8487 && vr1type
== VR_ANTI_RANGE
)
8489 if (TREE_CODE (vr1min
) == INTEGER_CST
)
8490 *vr0max
= int_const_binop (MINUS_EXPR
, vr1min
,
8491 build_int_cst (TREE_TYPE (vr1min
), 1));
8495 else if (*vr0type
== VR_ANTI_RANGE
8496 && vr1type
== VR_RANGE
)
8498 *vr0type
= VR_RANGE
;
8499 if (TREE_CODE (*vr0max
) == INTEGER_CST
)
8500 *vr0min
= int_const_binop (PLUS_EXPR
, *vr0max
,
8501 build_int_cst (TREE_TYPE (*vr0max
), 1));
8509 else if ((operand_less_p (*vr0min
, vr1max
) == 1
8510 || operand_equal_p (*vr0min
, vr1max
, 0))
8511 && operand_less_p (vr1min
, *vr0min
) == 1)
8513 /* ( [ ) ] or ( )[ ] */
8514 if (*vr0type
== VR_ANTI_RANGE
8515 && vr1type
== VR_ANTI_RANGE
)
8517 else if (*vr0type
== VR_RANGE
8518 && vr1type
== VR_RANGE
)
8520 else if (*vr0type
== VR_RANGE
8521 && vr1type
== VR_ANTI_RANGE
)
8523 if (TREE_CODE (vr1max
) == INTEGER_CST
)
8524 *vr0min
= int_const_binop (PLUS_EXPR
, vr1max
,
8525 build_int_cst (TREE_TYPE (vr1max
), 1));
8529 else if (*vr0type
== VR_ANTI_RANGE
8530 && vr1type
== VR_RANGE
)
8532 *vr0type
= VR_RANGE
;
8533 if (TREE_CODE (*vr0min
) == INTEGER_CST
)
8534 *vr0max
= int_const_binop (MINUS_EXPR
, *vr0min
,
8535 build_int_cst (TREE_TYPE (*vr0min
), 1));
8544 /* As a fallback simply use { *VRTYPE, *VR0MIN, *VR0MAX } as
8545 result for the intersection. That's always a conservative
8546 correct estimate. */
8552 /* Intersect the two value-ranges *VR0 and *VR1 and store the result
8553 in *VR0. This may not be the smallest possible such range. */
8556 vrp_intersect_ranges_1 (value_range_t
*vr0
, value_range_t
*vr1
)
8558 value_range_t saved
;
8560 /* If either range is VR_VARYING the other one wins. */
8561 if (vr1
->type
== VR_VARYING
)
8563 if (vr0
->type
== VR_VARYING
)
8565 copy_value_range (vr0
, vr1
);
8569 /* When either range is VR_UNDEFINED the resulting range is
8570 VR_UNDEFINED, too. */
8571 if (vr0
->type
== VR_UNDEFINED
)
8573 if (vr1
->type
== VR_UNDEFINED
)
8575 set_value_range_to_undefined (vr0
);
8579 /* Save the original vr0 so we can return it as conservative intersection
8580 result when our worker turns things to varying. */
8582 intersect_ranges (&vr0
->type
, &vr0
->min
, &vr0
->max
,
8583 vr1
->type
, vr1
->min
, vr1
->max
);
8584 /* Make sure to canonicalize the result though as the inversion of a
8585 VR_RANGE can still be a VR_RANGE. */
8586 set_and_canonicalize_value_range (vr0
, vr0
->type
,
8587 vr0
->min
, vr0
->max
, vr0
->equiv
);
8588 /* If that failed, use the saved original VR0. */
8589 if (vr0
->type
== VR_VARYING
)
8594 /* If the result is VR_UNDEFINED there is no need to mess with
8595 the equivalencies. */
8596 if (vr0
->type
== VR_UNDEFINED
)
8599 /* The resulting set of equivalences for range intersection is the union of
8601 if (vr0
->equiv
&& vr1
->equiv
&& vr0
->equiv
!= vr1
->equiv
)
8602 bitmap_ior_into (vr0
->equiv
, vr1
->equiv
);
8603 else if (vr1
->equiv
&& !vr0
->equiv
)
8604 bitmap_copy (vr0
->equiv
, vr1
->equiv
);
8608 vrp_intersect_ranges (value_range_t
*vr0
, value_range_t
*vr1
)
8610 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8612 fprintf (dump_file
, "Intersecting\n ");
8613 dump_value_range (dump_file
, vr0
);
8614 fprintf (dump_file
, "\nand\n ");
8615 dump_value_range (dump_file
, vr1
);
8616 fprintf (dump_file
, "\n");
8618 vrp_intersect_ranges_1 (vr0
, vr1
);
8619 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8621 fprintf (dump_file
, "to\n ");
8622 dump_value_range (dump_file
, vr0
);
8623 fprintf (dump_file
, "\n");
8627 /* Meet operation for value ranges. Given two value ranges VR0 and
8628 VR1, store in VR0 a range that contains both VR0 and VR1. This
8629 may not be the smallest possible such range. */
8632 vrp_meet_1 (value_range_t
*vr0
, value_range_t
*vr1
)
8634 value_range_t saved
;
8636 if (vr0
->type
== VR_UNDEFINED
)
8638 set_value_range (vr0
, vr1
->type
, vr1
->min
, vr1
->max
, vr1
->equiv
);
8642 if (vr1
->type
== VR_UNDEFINED
)
8644 /* VR0 already has the resulting range. */
8648 if (vr0
->type
== VR_VARYING
)
8650 /* Nothing to do. VR0 already has the resulting range. */
8654 if (vr1
->type
== VR_VARYING
)
8656 set_value_range_to_varying (vr0
);
8661 union_ranges (&vr0
->type
, &vr0
->min
, &vr0
->max
,
8662 vr1
->type
, vr1
->min
, vr1
->max
);
8663 if (vr0
->type
== VR_VARYING
)
8665 /* Failed to find an efficient meet. Before giving up and setting
8666 the result to VARYING, see if we can at least derive a useful
8667 anti-range. FIXME, all this nonsense about distinguishing
8668 anti-ranges from ranges is necessary because of the odd
8669 semantics of range_includes_zero_p and friends. */
8670 if (((saved
.type
== VR_RANGE
8671 && range_includes_zero_p (saved
.min
, saved
.max
) == 0)
8672 || (saved
.type
== VR_ANTI_RANGE
8673 && range_includes_zero_p (saved
.min
, saved
.max
) == 1))
8674 && ((vr1
->type
== VR_RANGE
8675 && range_includes_zero_p (vr1
->min
, vr1
->max
) == 0)
8676 || (vr1
->type
== VR_ANTI_RANGE
8677 && range_includes_zero_p (vr1
->min
, vr1
->max
) == 1)))
8679 set_value_range_to_nonnull (vr0
, TREE_TYPE (saved
.min
));
8681 /* Since this meet operation did not result from the meeting of
8682 two equivalent names, VR0 cannot have any equivalences. */
8684 bitmap_clear (vr0
->equiv
);
8688 set_value_range_to_varying (vr0
);
8691 set_and_canonicalize_value_range (vr0
, vr0
->type
, vr0
->min
, vr0
->max
,
8693 if (vr0
->type
== VR_VARYING
)
8696 /* The resulting set of equivalences is always the intersection of
8698 if (vr0
->equiv
&& vr1
->equiv
&& vr0
->equiv
!= vr1
->equiv
)
8699 bitmap_and_into (vr0
->equiv
, vr1
->equiv
);
8700 else if (vr0
->equiv
&& !vr1
->equiv
)
8701 bitmap_clear (vr0
->equiv
);
8705 vrp_meet (value_range_t
*vr0
, value_range_t
*vr1
)
8707 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8709 fprintf (dump_file
, "Meeting\n ");
8710 dump_value_range (dump_file
, vr0
);
8711 fprintf (dump_file
, "\nand\n ");
8712 dump_value_range (dump_file
, vr1
);
8713 fprintf (dump_file
, "\n");
8715 vrp_meet_1 (vr0
, vr1
);
8716 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8718 fprintf (dump_file
, "to\n ");
8719 dump_value_range (dump_file
, vr0
);
8720 fprintf (dump_file
, "\n");
8725 /* Visit all arguments for PHI node PHI that flow through executable
8726 edges. If a valid value range can be derived from all the incoming
8727 value ranges, set a new range for the LHS of PHI. */
8729 static enum ssa_prop_result
8730 vrp_visit_phi_node (gphi
*phi
)
8733 tree lhs
= PHI_RESULT (phi
);
8734 value_range_t
*lhs_vr
= get_value_range (lhs
);
8735 value_range_t vr_result
= VR_INITIALIZER
;
8737 int edges
, old_edges
;
8740 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8742 fprintf (dump_file
, "\nVisiting PHI node: ");
8743 print_gimple_stmt (dump_file
, phi
, 0, dump_flags
);
8747 for (i
= 0; i
< gimple_phi_num_args (phi
); i
++)
8749 edge e
= gimple_phi_arg_edge (phi
, i
);
8751 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8754 " Argument #%d (%d -> %d %sexecutable)\n",
8755 (int) i
, e
->src
->index
, e
->dest
->index
,
8756 (e
->flags
& EDGE_EXECUTABLE
) ? "" : "not ");
8759 if (e
->flags
& EDGE_EXECUTABLE
)
8761 tree arg
= PHI_ARG_DEF (phi
, i
);
8762 value_range_t vr_arg
;
8766 if (TREE_CODE (arg
) == SSA_NAME
)
8768 vr_arg
= *(get_value_range (arg
));
8769 /* Do not allow equivalences or symbolic ranges to leak in from
8770 backedges. That creates invalid equivalencies.
8771 See PR53465 and PR54767. */
8772 if (e
->flags
& EDGE_DFS_BACK
)
8774 if (vr_arg
.type
== VR_RANGE
8775 || vr_arg
.type
== VR_ANTI_RANGE
)
8777 vr_arg
.equiv
= NULL
;
8778 if (symbolic_range_p (&vr_arg
))
8780 vr_arg
.type
= VR_VARYING
;
8781 vr_arg
.min
= NULL_TREE
;
8782 vr_arg
.max
= NULL_TREE
;
8788 /* If the non-backedge arguments range is VR_VARYING then
8789 we can still try recording a simple equivalence. */
8790 if (vr_arg
.type
== VR_VARYING
)
8792 vr_arg
.type
= VR_RANGE
;
8795 vr_arg
.equiv
= NULL
;
8801 if (TREE_OVERFLOW_P (arg
))
8802 arg
= drop_tree_overflow (arg
);
8804 vr_arg
.type
= VR_RANGE
;
8807 vr_arg
.equiv
= NULL
;
8810 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8812 fprintf (dump_file
, "\t");
8813 print_generic_expr (dump_file
, arg
, dump_flags
);
8814 fprintf (dump_file
, ": ");
8815 dump_value_range (dump_file
, &vr_arg
);
8816 fprintf (dump_file
, "\n");
8820 copy_value_range (&vr_result
, &vr_arg
);
8822 vrp_meet (&vr_result
, &vr_arg
);
8825 if (vr_result
.type
== VR_VARYING
)
8830 if (vr_result
.type
== VR_VARYING
)
8832 else if (vr_result
.type
== VR_UNDEFINED
)
8835 old_edges
= vr_phi_edge_counts
[SSA_NAME_VERSION (lhs
)];
8836 vr_phi_edge_counts
[SSA_NAME_VERSION (lhs
)] = edges
;
8838 /* To prevent infinite iterations in the algorithm, derive ranges
8839 when the new value is slightly bigger or smaller than the
8840 previous one. We don't do this if we have seen a new executable
8841 edge; this helps us avoid an overflow infinity for conditionals
8842 which are not in a loop. If the old value-range was VR_UNDEFINED
8843 use the updated range and iterate one more time. */
8845 && gimple_phi_num_args (phi
) > 1
8846 && edges
== old_edges
8847 && lhs_vr
->type
!= VR_UNDEFINED
)
8849 /* Compare old and new ranges, fall back to varying if the
8850 values are not comparable. */
8851 int cmp_min
= compare_values (lhs_vr
->min
, vr_result
.min
);
8854 int cmp_max
= compare_values (lhs_vr
->max
, vr_result
.max
);
8858 /* For non VR_RANGE or for pointers fall back to varying if
8859 the range changed. */
8860 if ((lhs_vr
->type
!= VR_RANGE
|| vr_result
.type
!= VR_RANGE
8861 || POINTER_TYPE_P (TREE_TYPE (lhs
)))
8862 && (cmp_min
!= 0 || cmp_max
!= 0))
8865 /* If the new minimum is larger than than the previous one
8866 retain the old value. If the new minimum value is smaller
8867 than the previous one and not -INF go all the way to -INF + 1.
8868 In the first case, to avoid infinite bouncing between different
8869 minimums, and in the other case to avoid iterating millions of
8870 times to reach -INF. Going to -INF + 1 also lets the following
8871 iteration compute whether there will be any overflow, at the
8872 expense of one additional iteration. */
8874 vr_result
.min
= lhs_vr
->min
;
8875 else if (cmp_min
> 0
8876 && !vrp_val_is_min (vr_result
.min
))
8878 = int_const_binop (PLUS_EXPR
,
8879 vrp_val_min (TREE_TYPE (vr_result
.min
)),
8880 build_int_cst (TREE_TYPE (vr_result
.min
), 1));
8882 /* Similarly for the maximum value. */
8884 vr_result
.max
= lhs_vr
->max
;
8885 else if (cmp_max
< 0
8886 && !vrp_val_is_max (vr_result
.max
))
8888 = int_const_binop (MINUS_EXPR
,
8889 vrp_val_max (TREE_TYPE (vr_result
.min
)),
8890 build_int_cst (TREE_TYPE (vr_result
.min
), 1));
8892 /* If we dropped either bound to +-INF then if this is a loop
8893 PHI node SCEV may known more about its value-range. */
8894 if ((cmp_min
> 0 || cmp_min
< 0
8895 || cmp_max
< 0 || cmp_max
> 0)
8896 && (l
= loop_containing_stmt (phi
))
8897 && l
->header
== gimple_bb (phi
))
8898 adjust_range_with_scev (&vr_result
, l
, phi
, lhs
);
8900 /* If we will end up with a (-INF, +INF) range, set it to
8901 VARYING. Same if the previous max value was invalid for
8902 the type and we end up with vr_result.min > vr_result.max. */
8903 if ((vrp_val_is_max (vr_result
.max
)
8904 && vrp_val_is_min (vr_result
.min
))
8905 || compare_values (vr_result
.min
,
8910 /* If the new range is different than the previous value, keep
8913 if (update_value_range (lhs
, &vr_result
))
8915 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8917 fprintf (dump_file
, "Found new range for ");
8918 print_generic_expr (dump_file
, lhs
, 0);
8919 fprintf (dump_file
, ": ");
8920 dump_value_range (dump_file
, &vr_result
);
8921 fprintf (dump_file
, "\n");
8924 return SSA_PROP_INTERESTING
;
8927 /* Nothing changed, don't add outgoing edges. */
8928 return SSA_PROP_NOT_INTERESTING
;
8930 /* No match found. Set the LHS to VARYING. */
8932 set_value_range_to_varying (lhs_vr
);
8933 return SSA_PROP_VARYING
;
8936 /* Simplify boolean operations if the source is known
8937 to be already a boolean. */
8939 simplify_truth_ops_using_ranges (gimple_stmt_iterator
*gsi
, gimple stmt
)
8941 enum tree_code rhs_code
= gimple_assign_rhs_code (stmt
);
8943 bool need_conversion
;
8945 /* We handle only !=/== case here. */
8946 gcc_assert (rhs_code
== EQ_EXPR
|| rhs_code
== NE_EXPR
);
8948 op0
= gimple_assign_rhs1 (stmt
);
8949 if (!op_with_boolean_value_range_p (op0
))
8952 op1
= gimple_assign_rhs2 (stmt
);
8953 if (!op_with_boolean_value_range_p (op1
))
8956 /* Reduce number of cases to handle to NE_EXPR. As there is no
8957 BIT_XNOR_EXPR we cannot replace A == B with a single statement. */
8958 if (rhs_code
== EQ_EXPR
)
8960 if (TREE_CODE (op1
) == INTEGER_CST
)
8961 op1
= int_const_binop (BIT_XOR_EXPR
, op1
,
8962 build_int_cst (TREE_TYPE (op1
), 1));
8967 lhs
= gimple_assign_lhs (stmt
);
8969 = !useless_type_conversion_p (TREE_TYPE (lhs
), TREE_TYPE (op0
));
8971 /* Make sure to not sign-extend a 1-bit 1 when converting the result. */
8973 && !TYPE_UNSIGNED (TREE_TYPE (op0
))
8974 && TYPE_PRECISION (TREE_TYPE (op0
)) == 1
8975 && TYPE_PRECISION (TREE_TYPE (lhs
)) > 1)
8978 /* For A != 0 we can substitute A itself. */
8979 if (integer_zerop (op1
))
8980 gimple_assign_set_rhs_with_ops (gsi
,
8982 ? NOP_EXPR
: TREE_CODE (op0
), op0
);
8983 /* For A != B we substitute A ^ B. Either with conversion. */
8984 else if (need_conversion
)
8986 tree tem
= make_ssa_name (TREE_TYPE (op0
));
8988 = gimple_build_assign (tem
, BIT_XOR_EXPR
, op0
, op1
);
8989 gsi_insert_before (gsi
, newop
, GSI_SAME_STMT
);
8990 gimple_assign_set_rhs_with_ops (gsi
, NOP_EXPR
, tem
);
8994 gimple_assign_set_rhs_with_ops (gsi
, BIT_XOR_EXPR
, op0
, op1
);
8995 update_stmt (gsi_stmt (*gsi
));
9000 /* Simplify a division or modulo operator to a right shift or
9001 bitwise and if the first operand is unsigned or is greater
9002 than zero and the second operand is an exact power of two.
9003 For TRUNC_MOD_EXPR op0 % op1 with constant op1, optimize it
9004 into just op0 if op0's range is known to be a subset of
9005 [-op1 + 1, op1 - 1] for signed and [0, op1 - 1] for unsigned
9009 simplify_div_or_mod_using_ranges (gimple stmt
)
9011 enum tree_code rhs_code
= gimple_assign_rhs_code (stmt
);
9013 tree op0
= gimple_assign_rhs1 (stmt
);
9014 tree op1
= gimple_assign_rhs2 (stmt
);
9015 value_range_t
*vr
= get_value_range (op0
);
9017 if (rhs_code
== TRUNC_MOD_EXPR
9018 && TREE_CODE (op1
) == INTEGER_CST
9019 && tree_int_cst_sgn (op1
) == 1
9020 && range_int_cst_p (vr
)
9021 && tree_int_cst_lt (vr
->max
, op1
))
9023 if (TYPE_UNSIGNED (TREE_TYPE (op0
))
9024 || tree_int_cst_sgn (vr
->min
) >= 0
9025 || tree_int_cst_lt (fold_unary (NEGATE_EXPR
, TREE_TYPE (op1
), op1
),
9028 /* If op0 already has the range op0 % op1 has,
9029 then TRUNC_MOD_EXPR won't change anything. */
9030 gimple_stmt_iterator gsi
= gsi_for_stmt (stmt
);
9031 gimple_assign_set_rhs_from_tree (&gsi
, op0
);
9037 if (!integer_pow2p (op1
))
9040 if (TYPE_UNSIGNED (TREE_TYPE (op0
)))
9042 val
= integer_one_node
;
9048 val
= compare_range_with_value (GE_EXPR
, vr
, integer_zero_node
, &sop
);
9052 && integer_onep (val
)
9053 && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC
))
9055 location_t location
;
9057 if (!gimple_has_location (stmt
))
9058 location
= input_location
;
9060 location
= gimple_location (stmt
);
9061 warning_at (location
, OPT_Wstrict_overflow
,
9062 "assuming signed overflow does not occur when "
9063 "simplifying %</%> or %<%%%> to %<>>%> or %<&%>");
9067 if (val
&& integer_onep (val
))
9071 if (rhs_code
== TRUNC_DIV_EXPR
)
9073 t
= build_int_cst (integer_type_node
, tree_log2 (op1
));
9074 gimple_assign_set_rhs_code (stmt
, RSHIFT_EXPR
);
9075 gimple_assign_set_rhs1 (stmt
, op0
);
9076 gimple_assign_set_rhs2 (stmt
, t
);
9080 t
= build_int_cst (TREE_TYPE (op1
), 1);
9081 t
= int_const_binop (MINUS_EXPR
, op1
, t
);
9082 t
= fold_convert (TREE_TYPE (op0
), t
);
9084 gimple_assign_set_rhs_code (stmt
, BIT_AND_EXPR
);
9085 gimple_assign_set_rhs1 (stmt
, op0
);
9086 gimple_assign_set_rhs2 (stmt
, t
);
9096 /* If the operand to an ABS_EXPR is >= 0, then eliminate the
9097 ABS_EXPR. If the operand is <= 0, then simplify the
9098 ABS_EXPR into a NEGATE_EXPR. */
9101 simplify_abs_using_ranges (gimple stmt
)
9104 tree op
= gimple_assign_rhs1 (stmt
);
9105 tree type
= TREE_TYPE (op
);
9106 value_range_t
*vr
= get_value_range (op
);
9108 if (TYPE_UNSIGNED (type
))
9110 val
= integer_zero_node
;
9116 val
= compare_range_with_value (LE_EXPR
, vr
, integer_zero_node
, &sop
);
9120 val
= compare_range_with_value (GE_EXPR
, vr
, integer_zero_node
,
9125 if (integer_zerop (val
))
9126 val
= integer_one_node
;
9127 else if (integer_onep (val
))
9128 val
= integer_zero_node
;
9133 && (integer_onep (val
) || integer_zerop (val
)))
9135 if (sop
&& issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC
))
9137 location_t location
;
9139 if (!gimple_has_location (stmt
))
9140 location
= input_location
;
9142 location
= gimple_location (stmt
);
9143 warning_at (location
, OPT_Wstrict_overflow
,
9144 "assuming signed overflow does not occur when "
9145 "simplifying %<abs (X)%> to %<X%> or %<-X%>");
9148 gimple_assign_set_rhs1 (stmt
, op
);
9149 if (integer_onep (val
))
9150 gimple_assign_set_rhs_code (stmt
, NEGATE_EXPR
);
9152 gimple_assign_set_rhs_code (stmt
, SSA_NAME
);
9161 /* Optimize away redundant BIT_AND_EXPR and BIT_IOR_EXPR.
9162 If all the bits that are being cleared by & are already
9163 known to be zero from VR, or all the bits that are being
9164 set by | are already known to be one from VR, the bit
9165 operation is redundant. */
9168 simplify_bit_ops_using_ranges (gimple_stmt_iterator
*gsi
, gimple stmt
)
9170 tree op0
= gimple_assign_rhs1 (stmt
);
9171 tree op1
= gimple_assign_rhs2 (stmt
);
9172 tree op
= NULL_TREE
;
9173 value_range_t vr0
= VR_INITIALIZER
;
9174 value_range_t vr1
= VR_INITIALIZER
;
9175 wide_int may_be_nonzero0
, may_be_nonzero1
;
9176 wide_int must_be_nonzero0
, must_be_nonzero1
;
9179 if (TREE_CODE (op0
) == SSA_NAME
)
9180 vr0
= *(get_value_range (op0
));
9181 else if (is_gimple_min_invariant (op0
))
9182 set_value_range_to_value (&vr0
, op0
, NULL
);
9186 if (TREE_CODE (op1
) == SSA_NAME
)
9187 vr1
= *(get_value_range (op1
));
9188 else if (is_gimple_min_invariant (op1
))
9189 set_value_range_to_value (&vr1
, op1
, NULL
);
9193 if (!zero_nonzero_bits_from_vr (TREE_TYPE (op0
), &vr0
, &may_be_nonzero0
,
9196 if (!zero_nonzero_bits_from_vr (TREE_TYPE (op1
), &vr1
, &may_be_nonzero1
,
9200 switch (gimple_assign_rhs_code (stmt
))
9203 mask
= may_be_nonzero0
.and_not (must_be_nonzero1
);
9209 mask
= may_be_nonzero1
.and_not (must_be_nonzero0
);
9217 mask
= may_be_nonzero0
.and_not (must_be_nonzero1
);
9223 mask
= may_be_nonzero1
.and_not (must_be_nonzero0
);
9234 if (op
== NULL_TREE
)
9237 gimple_assign_set_rhs_with_ops (gsi
, TREE_CODE (op
), op
);
9238 update_stmt (gsi_stmt (*gsi
));
9242 /* We are comparing trees OP0 and OP1 using COND_CODE. OP0 has
9243 a known value range VR.
9245 If there is one and only one value which will satisfy the
9246 conditional, then return that value. Else return NULL.
9248 If signed overflow must be undefined for the value to satisfy
9249 the conditional, then set *STRICT_OVERFLOW_P to true. */
9252 test_for_singularity (enum tree_code cond_code
, tree op0
,
9253 tree op1
, value_range_t
*vr
,
9254 bool *strict_overflow_p
)
9259 /* Extract minimum/maximum values which satisfy the
9260 the conditional as it was written. */
9261 if (cond_code
== LE_EXPR
|| cond_code
== LT_EXPR
)
9263 /* This should not be negative infinity; there is no overflow
9265 min
= TYPE_MIN_VALUE (TREE_TYPE (op0
));
9268 if (cond_code
== LT_EXPR
&& !is_overflow_infinity (max
))
9270 tree one
= build_int_cst (TREE_TYPE (op0
), 1);
9271 max
= fold_build2 (MINUS_EXPR
, TREE_TYPE (op0
), max
, one
);
9273 TREE_NO_WARNING (max
) = 1;
9276 else if (cond_code
== GE_EXPR
|| cond_code
== GT_EXPR
)
9278 /* This should not be positive infinity; there is no overflow
9280 max
= TYPE_MAX_VALUE (TREE_TYPE (op0
));
9283 if (cond_code
== GT_EXPR
&& !is_overflow_infinity (min
))
9285 tree one
= build_int_cst (TREE_TYPE (op0
), 1);
9286 min
= fold_build2 (PLUS_EXPR
, TREE_TYPE (op0
), min
, one
);
9288 TREE_NO_WARNING (min
) = 1;
9292 /* Now refine the minimum and maximum values using any
9293 value range information we have for op0. */
9296 if (compare_values (vr
->min
, min
) == 1)
9298 if (compare_values (vr
->max
, max
) == -1)
9301 /* If the new min/max values have converged to a single value,
9302 then there is only one value which can satisfy the condition,
9303 return that value. */
9304 if (operand_equal_p (min
, max
, 0) && is_gimple_min_invariant (min
))
9306 if ((cond_code
== LE_EXPR
|| cond_code
== LT_EXPR
)
9307 && is_overflow_infinity (vr
->max
))
9308 *strict_overflow_p
= true;
9309 if ((cond_code
== GE_EXPR
|| cond_code
== GT_EXPR
)
9310 && is_overflow_infinity (vr
->min
))
9311 *strict_overflow_p
= true;
9319 /* Return whether the value range *VR fits in an integer type specified
9320 by PRECISION and UNSIGNED_P. */
9323 range_fits_type_p (value_range_t
*vr
, unsigned dest_precision
, signop dest_sgn
)
9326 unsigned src_precision
;
9330 /* We can only handle integral and pointer types. */
9331 src_type
= TREE_TYPE (vr
->min
);
9332 if (!INTEGRAL_TYPE_P (src_type
)
9333 && !POINTER_TYPE_P (src_type
))
9336 /* An extension is fine unless VR is SIGNED and dest_sgn is UNSIGNED,
9337 and so is an identity transform. */
9338 src_precision
= TYPE_PRECISION (TREE_TYPE (vr
->min
));
9339 src_sgn
= TYPE_SIGN (src_type
);
9340 if ((src_precision
< dest_precision
9341 && !(dest_sgn
== UNSIGNED
&& src_sgn
== SIGNED
))
9342 || (src_precision
== dest_precision
&& src_sgn
== dest_sgn
))
9345 /* Now we can only handle ranges with constant bounds. */
9346 if (vr
->type
!= VR_RANGE
9347 || TREE_CODE (vr
->min
) != INTEGER_CST
9348 || TREE_CODE (vr
->max
) != INTEGER_CST
)
9351 /* For sign changes, the MSB of the wide_int has to be clear.
9352 An unsigned value with its MSB set cannot be represented by
9353 a signed wide_int, while a negative value cannot be represented
9354 by an unsigned wide_int. */
9355 if (src_sgn
!= dest_sgn
9356 && (wi::lts_p (vr
->min
, 0) || wi::lts_p (vr
->max
, 0)))
9359 /* Then we can perform the conversion on both ends and compare
9360 the result for equality. */
9361 tem
= wi::ext (wi::to_widest (vr
->min
), dest_precision
, dest_sgn
);
9362 if (tem
!= wi::to_widest (vr
->min
))
9364 tem
= wi::ext (wi::to_widest (vr
->max
), dest_precision
, dest_sgn
);
9365 if (tem
!= wi::to_widest (vr
->max
))
9371 /* Simplify a conditional using a relational operator to an equality
9372 test if the range information indicates only one value can satisfy
9373 the original conditional. */
9376 simplify_cond_using_ranges (gcond
*stmt
)
9378 tree op0
= gimple_cond_lhs (stmt
);
9379 tree op1
= gimple_cond_rhs (stmt
);
9380 enum tree_code cond_code
= gimple_cond_code (stmt
);
9382 if (cond_code
!= NE_EXPR
9383 && cond_code
!= EQ_EXPR
9384 && TREE_CODE (op0
) == SSA_NAME
9385 && INTEGRAL_TYPE_P (TREE_TYPE (op0
))
9386 && is_gimple_min_invariant (op1
))
9388 value_range_t
*vr
= get_value_range (op0
);
9390 /* If we have range information for OP0, then we might be
9391 able to simplify this conditional. */
9392 if (vr
->type
== VR_RANGE
)
9394 enum warn_strict_overflow_code wc
= WARN_STRICT_OVERFLOW_COMPARISON
;
9396 tree new_tree
= test_for_singularity (cond_code
, op0
, op1
, vr
, &sop
);
9399 && (!sop
|| TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0
))))
9403 fprintf (dump_file
, "Simplified relational ");
9404 print_gimple_stmt (dump_file
, stmt
, 0, 0);
9405 fprintf (dump_file
, " into ");
9408 gimple_cond_set_code (stmt
, EQ_EXPR
);
9409 gimple_cond_set_lhs (stmt
, op0
);
9410 gimple_cond_set_rhs (stmt
, new_tree
);
9416 print_gimple_stmt (dump_file
, stmt
, 0, 0);
9417 fprintf (dump_file
, "\n");
9420 if (sop
&& issue_strict_overflow_warning (wc
))
9422 location_t location
= input_location
;
9423 if (gimple_has_location (stmt
))
9424 location
= gimple_location (stmt
);
9426 warning_at (location
, OPT_Wstrict_overflow
,
9427 "assuming signed overflow does not occur when "
9428 "simplifying conditional");
9434 /* Try again after inverting the condition. We only deal
9435 with integral types here, so no need to worry about
9436 issues with inverting FP comparisons. */
9438 new_tree
= test_for_singularity
9439 (invert_tree_comparison (cond_code
, false),
9440 op0
, op1
, vr
, &sop
);
9443 && (!sop
|| TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0
))))
9447 fprintf (dump_file
, "Simplified relational ");
9448 print_gimple_stmt (dump_file
, stmt
, 0, 0);
9449 fprintf (dump_file
, " into ");
9452 gimple_cond_set_code (stmt
, NE_EXPR
);
9453 gimple_cond_set_lhs (stmt
, op0
);
9454 gimple_cond_set_rhs (stmt
, new_tree
);
9460 print_gimple_stmt (dump_file
, stmt
, 0, 0);
9461 fprintf (dump_file
, "\n");
9464 if (sop
&& issue_strict_overflow_warning (wc
))
9466 location_t location
= input_location
;
9467 if (gimple_has_location (stmt
))
9468 location
= gimple_location (stmt
);
9470 warning_at (location
, OPT_Wstrict_overflow
,
9471 "assuming signed overflow does not occur when "
9472 "simplifying conditional");
9480 /* If we have a comparison of an SSA_NAME (OP0) against a constant,
9481 see if OP0 was set by a type conversion where the source of
9482 the conversion is another SSA_NAME with a range that fits
9483 into the range of OP0's type.
9485 If so, the conversion is redundant as the earlier SSA_NAME can be
9486 used for the comparison directly if we just massage the constant in the
9488 if (TREE_CODE (op0
) == SSA_NAME
9489 && TREE_CODE (op1
) == INTEGER_CST
)
9491 gimple def_stmt
= SSA_NAME_DEF_STMT (op0
);
9494 if (!is_gimple_assign (def_stmt
)
9495 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt
)))
9498 innerop
= gimple_assign_rhs1 (def_stmt
);
9500 if (TREE_CODE (innerop
) == SSA_NAME
9501 && !POINTER_TYPE_P (TREE_TYPE (innerop
)))
9503 value_range_t
*vr
= get_value_range (innerop
);
9505 if (range_int_cst_p (vr
)
9506 && range_fits_type_p (vr
,
9507 TYPE_PRECISION (TREE_TYPE (op0
)),
9508 TYPE_SIGN (TREE_TYPE (op0
)))
9509 && int_fits_type_p (op1
, TREE_TYPE (innerop
))
9510 /* The range must not have overflowed, or if it did overflow
9511 we must not be wrapping/trapping overflow and optimizing
9512 with strict overflow semantics. */
9513 && ((!is_negative_overflow_infinity (vr
->min
)
9514 && !is_positive_overflow_infinity (vr
->max
))
9515 || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (innerop
))))
9517 /* If the range overflowed and the user has asked for warnings
9518 when strict overflow semantics were used to optimize code,
9519 issue an appropriate warning. */
9520 if (cond_code
!= EQ_EXPR
&& cond_code
!= NE_EXPR
9521 && (is_negative_overflow_infinity (vr
->min
)
9522 || is_positive_overflow_infinity (vr
->max
))
9523 && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_CONDITIONAL
))
9525 location_t location
;
9527 if (!gimple_has_location (stmt
))
9528 location
= input_location
;
9530 location
= gimple_location (stmt
);
9531 warning_at (location
, OPT_Wstrict_overflow
,
9532 "assuming signed overflow does not occur when "
9533 "simplifying conditional");
9536 tree newconst
= fold_convert (TREE_TYPE (innerop
), op1
);
9537 gimple_cond_set_lhs (stmt
, innerop
);
9538 gimple_cond_set_rhs (stmt
, newconst
);
9547 /* Simplify a switch statement using the value range of the switch
9551 simplify_switch_using_ranges (gswitch
*stmt
)
9553 tree op
= gimple_switch_index (stmt
);
9558 size_t i
= 0, j
= 0, n
, n2
;
9561 size_t k
= 1, l
= 0;
9563 if (TREE_CODE (op
) == SSA_NAME
)
9565 vr
= get_value_range (op
);
9567 /* We can only handle integer ranges. */
9568 if ((vr
->type
!= VR_RANGE
9569 && vr
->type
!= VR_ANTI_RANGE
)
9570 || symbolic_range_p (vr
))
9573 /* Find case label for min/max of the value range. */
9574 take_default
= !find_case_label_ranges (stmt
, vr
, &i
, &j
, &k
, &l
);
9576 else if (TREE_CODE (op
) == INTEGER_CST
)
9578 take_default
= !find_case_label_index (stmt
, 1, op
, &i
);
9592 n
= gimple_switch_num_labels (stmt
);
9594 /* Bail out if this is just all edges taken. */
9600 /* Build a new vector of taken case labels. */
9601 vec2
= make_tree_vec (j
- i
+ 1 + l
- k
+ 1 + (int)take_default
);
9604 /* Add the default edge, if necessary. */
9606 TREE_VEC_ELT (vec2
, n2
++) = gimple_switch_default_label (stmt
);
9608 for (; i
<= j
; ++i
, ++n2
)
9609 TREE_VEC_ELT (vec2
, n2
) = gimple_switch_label (stmt
, i
);
9611 for (; k
<= l
; ++k
, ++n2
)
9612 TREE_VEC_ELT (vec2
, n2
) = gimple_switch_label (stmt
, k
);
9614 /* Mark needed edges. */
9615 for (i
= 0; i
< n2
; ++i
)
9617 e
= find_edge (gimple_bb (stmt
),
9618 label_to_block (CASE_LABEL (TREE_VEC_ELT (vec2
, i
))));
9619 e
->aux
= (void *)-1;
9622 /* Queue not needed edges for later removal. */
9623 FOR_EACH_EDGE (e
, ei
, gimple_bb (stmt
)->succs
)
9625 if (e
->aux
== (void *)-1)
9631 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
9633 fprintf (dump_file
, "removing unreachable case label\n");
9635 to_remove_edges
.safe_push (e
);
9636 e
->flags
&= ~EDGE_EXECUTABLE
;
9639 /* And queue an update for the stmt. */
9642 to_update_switch_stmts
.safe_push (su
);
9646 /* Simplify an integral conversion from an SSA name in STMT. */
9649 simplify_conversion_using_ranges (gimple stmt
)
9651 tree innerop
, middleop
, finaltype
;
9653 value_range_t
*innervr
;
9654 signop inner_sgn
, middle_sgn
, final_sgn
;
9655 unsigned inner_prec
, middle_prec
, final_prec
;
9656 widest_int innermin
, innermed
, innermax
, middlemin
, middlemed
, middlemax
;
9658 finaltype
= TREE_TYPE (gimple_assign_lhs (stmt
));
9659 if (!INTEGRAL_TYPE_P (finaltype
))
9661 middleop
= gimple_assign_rhs1 (stmt
);
9662 def_stmt
= SSA_NAME_DEF_STMT (middleop
);
9663 if (!is_gimple_assign (def_stmt
)
9664 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt
)))
9666 innerop
= gimple_assign_rhs1 (def_stmt
);
9667 if (TREE_CODE (innerop
) != SSA_NAME
9668 || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (innerop
))
9671 /* Get the value-range of the inner operand. */
9672 innervr
= get_value_range (innerop
);
9673 if (innervr
->type
!= VR_RANGE
9674 || TREE_CODE (innervr
->min
) != INTEGER_CST
9675 || TREE_CODE (innervr
->max
) != INTEGER_CST
)
9678 /* Simulate the conversion chain to check if the result is equal if
9679 the middle conversion is removed. */
9680 innermin
= wi::to_widest (innervr
->min
);
9681 innermax
= wi::to_widest (innervr
->max
);
9683 inner_prec
= TYPE_PRECISION (TREE_TYPE (innerop
));
9684 middle_prec
= TYPE_PRECISION (TREE_TYPE (middleop
));
9685 final_prec
= TYPE_PRECISION (finaltype
);
9687 /* If the first conversion is not injective, the second must not
9689 if (wi::gtu_p (innermax
- innermin
,
9690 wi::mask
<widest_int
> (middle_prec
, false))
9691 && middle_prec
< final_prec
)
9693 /* We also want a medium value so that we can track the effect that
9694 narrowing conversions with sign change have. */
9695 inner_sgn
= TYPE_SIGN (TREE_TYPE (innerop
));
9696 if (inner_sgn
== UNSIGNED
)
9697 innermed
= wi::shifted_mask
<widest_int
> (1, inner_prec
- 1, false);
9700 if (wi::cmp (innermin
, innermed
, inner_sgn
) >= 0
9701 || wi::cmp (innermed
, innermax
, inner_sgn
) >= 0)
9702 innermed
= innermin
;
9704 middle_sgn
= TYPE_SIGN (TREE_TYPE (middleop
));
9705 middlemin
= wi::ext (innermin
, middle_prec
, middle_sgn
);
9706 middlemed
= wi::ext (innermed
, middle_prec
, middle_sgn
);
9707 middlemax
= wi::ext (innermax
, middle_prec
, middle_sgn
);
9709 /* Require that the final conversion applied to both the original
9710 and the intermediate range produces the same result. */
9711 final_sgn
= TYPE_SIGN (finaltype
);
9712 if (wi::ext (middlemin
, final_prec
, final_sgn
)
9713 != wi::ext (innermin
, final_prec
, final_sgn
)
9714 || wi::ext (middlemed
, final_prec
, final_sgn
)
9715 != wi::ext (innermed
, final_prec
, final_sgn
)
9716 || wi::ext (middlemax
, final_prec
, final_sgn
)
9717 != wi::ext (innermax
, final_prec
, final_sgn
))
9720 gimple_assign_set_rhs1 (stmt
, innerop
);
9725 /* Simplify a conversion from integral SSA name to float in STMT. */
9728 simplify_float_conversion_using_ranges (gimple_stmt_iterator
*gsi
, gimple stmt
)
9730 tree rhs1
= gimple_assign_rhs1 (stmt
);
9731 value_range_t
*vr
= get_value_range (rhs1
);
9732 machine_mode fltmode
= TYPE_MODE (TREE_TYPE (gimple_assign_lhs (stmt
)));
9737 /* We can only handle constant ranges. */
9738 if (vr
->type
!= VR_RANGE
9739 || TREE_CODE (vr
->min
) != INTEGER_CST
9740 || TREE_CODE (vr
->max
) != INTEGER_CST
)
9743 /* First check if we can use a signed type in place of an unsigned. */
9744 if (TYPE_UNSIGNED (TREE_TYPE (rhs1
))
9745 && (can_float_p (fltmode
, TYPE_MODE (TREE_TYPE (rhs1
)), 0)
9746 != CODE_FOR_nothing
)
9747 && range_fits_type_p (vr
, TYPE_PRECISION (TREE_TYPE (rhs1
)), SIGNED
))
9748 mode
= TYPE_MODE (TREE_TYPE (rhs1
));
9749 /* If we can do the conversion in the current input mode do nothing. */
9750 else if (can_float_p (fltmode
, TYPE_MODE (TREE_TYPE (rhs1
)),
9751 TYPE_UNSIGNED (TREE_TYPE (rhs1
))) != CODE_FOR_nothing
)
9753 /* Otherwise search for a mode we can use, starting from the narrowest
9754 integer mode available. */
9757 mode
= GET_CLASS_NARROWEST_MODE (MODE_INT
);
9760 /* If we cannot do a signed conversion to float from mode
9761 or if the value-range does not fit in the signed type
9762 try with a wider mode. */
9763 if (can_float_p (fltmode
, mode
, 0) != CODE_FOR_nothing
9764 && range_fits_type_p (vr
, GET_MODE_PRECISION (mode
), SIGNED
))
9767 mode
= GET_MODE_WIDER_MODE (mode
);
9768 /* But do not widen the input. Instead leave that to the
9769 optabs expansion code. */
9770 if (GET_MODE_PRECISION (mode
) > TYPE_PRECISION (TREE_TYPE (rhs1
)))
9773 while (mode
!= VOIDmode
);
9774 if (mode
== VOIDmode
)
9778 /* It works, insert a truncation or sign-change before the
9779 float conversion. */
9780 tem
= make_ssa_name (build_nonstandard_integer_type
9781 (GET_MODE_PRECISION (mode
), 0));
9782 conv
= gimple_build_assign (tem
, NOP_EXPR
, rhs1
);
9783 gsi_insert_before (gsi
, conv
, GSI_SAME_STMT
);
9784 gimple_assign_set_rhs1 (stmt
, tem
);
9790 /* Simplify an internal fn call using ranges if possible. */
9793 simplify_internal_call_using_ranges (gimple_stmt_iterator
*gsi
, gimple stmt
)
9795 enum tree_code subcode
;
9796 bool is_ubsan
= false;
9798 switch (gimple_call_internal_fn (stmt
))
9800 case IFN_UBSAN_CHECK_ADD
:
9801 subcode
= PLUS_EXPR
;
9804 case IFN_UBSAN_CHECK_SUB
:
9805 subcode
= MINUS_EXPR
;
9808 case IFN_UBSAN_CHECK_MUL
:
9809 subcode
= MULT_EXPR
;
9812 case IFN_ADD_OVERFLOW
:
9813 subcode
= PLUS_EXPR
;
9815 case IFN_SUB_OVERFLOW
:
9816 subcode
= MINUS_EXPR
;
9818 case IFN_MUL_OVERFLOW
:
9819 subcode
= MULT_EXPR
;
9825 tree op0
= gimple_call_arg (stmt
, 0);
9826 tree op1
= gimple_call_arg (stmt
, 1);
9829 type
= TREE_TYPE (op0
);
9830 else if (gimple_call_lhs (stmt
) == NULL_TREE
)
9833 type
= TREE_TYPE (TREE_TYPE (gimple_call_lhs (stmt
)));
9834 if (!check_for_binary_op_overflow (subcode
, type
, op0
, op1
, &ovf
)
9835 || (is_ubsan
&& ovf
))
9839 location_t loc
= gimple_location (stmt
);
9841 g
= gimple_build_assign (gimple_call_lhs (stmt
), subcode
, op0
, op1
);
9844 int prec
= TYPE_PRECISION (type
);
9847 || !useless_type_conversion_p (type
, TREE_TYPE (op0
))
9848 || !useless_type_conversion_p (type
, TREE_TYPE (op1
)))
9849 utype
= build_nonstandard_integer_type (prec
, 1);
9850 if (TREE_CODE (op0
) == INTEGER_CST
)
9851 op0
= fold_convert (utype
, op0
);
9852 else if (!useless_type_conversion_p (utype
, TREE_TYPE (op0
)))
9854 g
= gimple_build_assign (make_ssa_name (utype
), NOP_EXPR
, op0
);
9855 gimple_set_location (g
, loc
);
9856 gsi_insert_before (gsi
, g
, GSI_SAME_STMT
);
9857 op0
= gimple_assign_lhs (g
);
9859 if (TREE_CODE (op1
) == INTEGER_CST
)
9860 op1
= fold_convert (utype
, op1
);
9861 else if (!useless_type_conversion_p (utype
, TREE_TYPE (op1
)))
9863 g
= gimple_build_assign (make_ssa_name (utype
), NOP_EXPR
, op1
);
9864 gimple_set_location (g
, loc
);
9865 gsi_insert_before (gsi
, g
, GSI_SAME_STMT
);
9866 op1
= gimple_assign_lhs (g
);
9868 g
= gimple_build_assign (make_ssa_name (utype
), subcode
, op0
, op1
);
9869 gimple_set_location (g
, loc
);
9870 gsi_insert_before (gsi
, g
, GSI_SAME_STMT
);
9873 g
= gimple_build_assign (make_ssa_name (type
), NOP_EXPR
,
9874 gimple_assign_lhs (g
));
9875 gimple_set_location (g
, loc
);
9876 gsi_insert_before (gsi
, g
, GSI_SAME_STMT
);
9878 g
= gimple_build_assign (gimple_call_lhs (stmt
), COMPLEX_EXPR
,
9879 gimple_assign_lhs (g
),
9880 build_int_cst (type
, ovf
));
9882 gimple_set_location (g
, loc
);
9883 gsi_replace (gsi
, g
, false);
9887 /* Simplify STMT using ranges if possible. */
9890 simplify_stmt_using_ranges (gimple_stmt_iterator
*gsi
)
9892 gimple stmt
= gsi_stmt (*gsi
);
9893 if (is_gimple_assign (stmt
))
9895 enum tree_code rhs_code
= gimple_assign_rhs_code (stmt
);
9896 tree rhs1
= gimple_assign_rhs1 (stmt
);
9902 /* Transform EQ_EXPR, NE_EXPR into BIT_XOR_EXPR or identity
9903 if the RHS is zero or one, and the LHS are known to be boolean
9905 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
9906 return simplify_truth_ops_using_ranges (gsi
, stmt
);
9909 /* Transform TRUNC_DIV_EXPR and TRUNC_MOD_EXPR into RSHIFT_EXPR
9910 and BIT_AND_EXPR respectively if the first operand is greater
9911 than zero and the second operand is an exact power of two.
9912 Also optimize TRUNC_MOD_EXPR away if the second operand is
9913 constant and the first operand already has the right value
9915 case TRUNC_DIV_EXPR
:
9916 case TRUNC_MOD_EXPR
:
9917 if (TREE_CODE (rhs1
) == SSA_NAME
9918 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
9919 return simplify_div_or_mod_using_ranges (stmt
);
9922 /* Transform ABS (X) into X or -X as appropriate. */
9924 if (TREE_CODE (rhs1
) == SSA_NAME
9925 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
9926 return simplify_abs_using_ranges (stmt
);
9931 /* Optimize away BIT_AND_EXPR and BIT_IOR_EXPR
9932 if all the bits being cleared are already cleared or
9933 all the bits being set are already set. */
9934 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
9935 return simplify_bit_ops_using_ranges (gsi
, stmt
);
9939 if (TREE_CODE (rhs1
) == SSA_NAME
9940 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
9941 return simplify_conversion_using_ranges (stmt
);
9945 if (TREE_CODE (rhs1
) == SSA_NAME
9946 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
9947 return simplify_float_conversion_using_ranges (gsi
, stmt
);
9954 else if (gimple_code (stmt
) == GIMPLE_COND
)
9955 return simplify_cond_using_ranges (as_a
<gcond
*> (stmt
));
9956 else if (gimple_code (stmt
) == GIMPLE_SWITCH
)
9957 return simplify_switch_using_ranges (as_a
<gswitch
*> (stmt
));
9958 else if (is_gimple_call (stmt
)
9959 && gimple_call_internal_p (stmt
))
9960 return simplify_internal_call_using_ranges (gsi
, stmt
);
9965 /* If the statement pointed by SI has a predicate whose value can be
9966 computed using the value range information computed by VRP, compute
9967 its value and return true. Otherwise, return false. */
9970 fold_predicate_in (gimple_stmt_iterator
*si
)
9972 bool assignment_p
= false;
9974 gimple stmt
= gsi_stmt (*si
);
9976 if (is_gimple_assign (stmt
)
9977 && TREE_CODE_CLASS (gimple_assign_rhs_code (stmt
)) == tcc_comparison
)
9979 assignment_p
= true;
9980 val
= vrp_evaluate_conditional (gimple_assign_rhs_code (stmt
),
9981 gimple_assign_rhs1 (stmt
),
9982 gimple_assign_rhs2 (stmt
),
9985 else if (gcond
*cond_stmt
= dyn_cast
<gcond
*> (stmt
))
9986 val
= vrp_evaluate_conditional (gimple_cond_code (cond_stmt
),
9987 gimple_cond_lhs (cond_stmt
),
9988 gimple_cond_rhs (cond_stmt
),
9996 val
= fold_convert (gimple_expr_type (stmt
), val
);
10000 fprintf (dump_file
, "Folding predicate ");
10001 print_gimple_expr (dump_file
, stmt
, 0, 0);
10002 fprintf (dump_file
, " to ");
10003 print_generic_expr (dump_file
, val
, 0);
10004 fprintf (dump_file
, "\n");
10007 if (is_gimple_assign (stmt
))
10008 gimple_assign_set_rhs_from_tree (si
, val
);
10011 gcc_assert (gimple_code (stmt
) == GIMPLE_COND
);
10012 gcond
*cond_stmt
= as_a
<gcond
*> (stmt
);
10013 if (integer_zerop (val
))
10014 gimple_cond_make_false (cond_stmt
);
10015 else if (integer_onep (val
))
10016 gimple_cond_make_true (cond_stmt
);
10018 gcc_unreachable ();
10027 /* Callback for substitute_and_fold folding the stmt at *SI. */
10030 vrp_fold_stmt (gimple_stmt_iterator
*si
)
10032 if (fold_predicate_in (si
))
10035 return simplify_stmt_using_ranges (si
);
10038 /* Stack of dest,src equivalency pairs that need to be restored after
10039 each attempt to thread a block's incoming edge to an outgoing edge.
10041 A NULL entry is used to mark the end of pairs which need to be
10043 static vec
<tree
> equiv_stack
;
10045 /* A trivial wrapper so that we can present the generic jump threading
10046 code with a simple API for simplifying statements. STMT is the
10047 statement we want to simplify, WITHIN_STMT provides the location
10048 for any overflow warnings. */
10051 simplify_stmt_for_jump_threading (gimple stmt
, gimple within_stmt
)
10053 if (gcond
*cond_stmt
= dyn_cast
<gcond
*> (stmt
))
10054 return vrp_evaluate_conditional (gimple_cond_code (cond_stmt
),
10055 gimple_cond_lhs (cond_stmt
),
10056 gimple_cond_rhs (cond_stmt
),
10059 if (gassign
*assign_stmt
= dyn_cast
<gassign
*> (stmt
))
10061 value_range_t new_vr
= VR_INITIALIZER
;
10062 tree lhs
= gimple_assign_lhs (assign_stmt
);
10064 if (TREE_CODE (lhs
) == SSA_NAME
10065 && (INTEGRAL_TYPE_P (TREE_TYPE (lhs
))
10066 || POINTER_TYPE_P (TREE_TYPE (lhs
))))
10068 extract_range_from_assignment (&new_vr
, assign_stmt
);
10069 if (range_int_cst_singleton_p (&new_vr
))
10077 /* Blocks which have more than one predecessor and more than
10078 one successor present jump threading opportunities, i.e.,
10079 when the block is reached from a specific predecessor, we
10080 may be able to determine which of the outgoing edges will
10081 be traversed. When this optimization applies, we are able
10082 to avoid conditionals at runtime and we may expose secondary
10083 optimization opportunities.
10085 This routine is effectively a driver for the generic jump
10086 threading code. It basically just presents the generic code
10087 with edges that may be suitable for jump threading.
10089 Unlike DOM, we do not iterate VRP if jump threading was successful.
10090 While iterating may expose new opportunities for VRP, it is expected
10091 those opportunities would be very limited and the compile time cost
10092 to expose those opportunities would be significant.
10094 As jump threading opportunities are discovered, they are registered
10095 for later realization. */
10098 identify_jump_threads (void)
10105 /* Ugh. When substituting values earlier in this pass we can
10106 wipe the dominance information. So rebuild the dominator
10107 information as we need it within the jump threading code. */
10108 calculate_dominance_info (CDI_DOMINATORS
);
10110 /* We do not allow VRP information to be used for jump threading
10111 across a back edge in the CFG. Otherwise it becomes too
10112 difficult to avoid eliminating loop exit tests. Of course
10113 EDGE_DFS_BACK is not accurate at this time so we have to
10115 mark_dfs_back_edges ();
10117 /* Do not thread across edges we are about to remove. Just marking
10118 them as EDGE_DFS_BACK will do. */
10119 FOR_EACH_VEC_ELT (to_remove_edges
, i
, e
)
10120 e
->flags
|= EDGE_DFS_BACK
;
10122 /* Allocate our unwinder stack to unwind any temporary equivalences
10123 that might be recorded. */
10124 equiv_stack
.create (20);
10126 /* To avoid lots of silly node creation, we create a single
10127 conditional and just modify it in-place when attempting to
10129 dummy
= gimple_build_cond (EQ_EXPR
,
10130 integer_zero_node
, integer_zero_node
,
10133 /* Walk through all the blocks finding those which present a
10134 potential jump threading opportunity. We could set this up
10135 as a dominator walker and record data during the walk, but
10136 I doubt it's worth the effort for the classes of jump
10137 threading opportunities we are trying to identify at this
10138 point in compilation. */
10139 FOR_EACH_BB_FN (bb
, cfun
)
10143 /* If the generic jump threading code does not find this block
10144 interesting, then there is nothing to do. */
10145 if (! potentially_threadable_block (bb
))
10148 /* We only care about blocks ending in a COND_EXPR. While there
10149 may be some value in handling SWITCH_EXPR here, I doubt it's
10150 terribly important. */
10151 last
= gsi_stmt (gsi_last_bb (bb
));
10153 /* We're basically looking for a switch or any kind of conditional with
10154 integral or pointer type arguments. Note the type of the second
10155 argument will be the same as the first argument, so no need to
10156 check it explicitly. */
10157 if (gimple_code (last
) == GIMPLE_SWITCH
10158 || (gimple_code (last
) == GIMPLE_COND
10159 && TREE_CODE (gimple_cond_lhs (last
)) == SSA_NAME
10160 && (INTEGRAL_TYPE_P (TREE_TYPE (gimple_cond_lhs (last
)))
10161 || POINTER_TYPE_P (TREE_TYPE (gimple_cond_lhs (last
))))
10162 && (TREE_CODE (gimple_cond_rhs (last
)) == SSA_NAME
10163 || is_gimple_min_invariant (gimple_cond_rhs (last
)))))
10167 /* We've got a block with multiple predecessors and multiple
10168 successors which also ends in a suitable conditional or
10169 switch statement. For each predecessor, see if we can thread
10170 it to a specific successor. */
10171 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
10173 /* Do not thread across back edges or abnormal edges
10175 if (e
->flags
& (EDGE_DFS_BACK
| EDGE_COMPLEX
))
10178 thread_across_edge (dummy
, e
, true, &equiv_stack
,
10179 simplify_stmt_for_jump_threading
);
10184 /* We do not actually update the CFG or SSA graphs at this point as
10185 ASSERT_EXPRs are still in the IL and cfg cleanup code does not yet
10186 handle ASSERT_EXPRs gracefully. */
10189 /* We identified all the jump threading opportunities earlier, but could
10190 not transform the CFG at that time. This routine transforms the
10191 CFG and arranges for the dominator tree to be rebuilt if necessary.
10193 Note the SSA graph update will occur during the normal TODO
10194 processing by the pass manager. */
10196 finalize_jump_threads (void)
10198 thread_through_all_blocks (false);
10199 equiv_stack
.release ();
10203 /* Traverse all the blocks folding conditionals with known ranges. */
10206 vrp_finalize (void)
10210 values_propagated
= true;
10214 fprintf (dump_file
, "\nValue ranges after VRP:\n\n");
10215 dump_all_value_ranges (dump_file
);
10216 fprintf (dump_file
, "\n");
10219 substitute_and_fold (op_with_constant_singleton_value_range
,
10220 vrp_fold_stmt
, false);
10222 if (warn_array_bounds
)
10223 check_all_array_refs ();
10225 /* We must identify jump threading opportunities before we release
10226 the datastructures built by VRP. */
10227 identify_jump_threads ();
10229 /* Set value range to non pointer SSA_NAMEs. */
10230 for (i
= 0; i
< num_vr_values
; i
++)
10233 tree name
= ssa_name (i
);
10236 || POINTER_TYPE_P (TREE_TYPE (name
))
10237 || (vr_value
[i
]->type
== VR_VARYING
)
10238 || (vr_value
[i
]->type
== VR_UNDEFINED
))
10241 if ((TREE_CODE (vr_value
[i
]->min
) == INTEGER_CST
)
10242 && (TREE_CODE (vr_value
[i
]->max
) == INTEGER_CST
)
10243 && (vr_value
[i
]->type
== VR_RANGE
10244 || vr_value
[i
]->type
== VR_ANTI_RANGE
))
10245 set_range_info (name
, vr_value
[i
]->type
, vr_value
[i
]->min
,
10249 /* Free allocated memory. */
10250 for (i
= 0; i
< num_vr_values
; i
++)
10253 BITMAP_FREE (vr_value
[i
]->equiv
);
10254 free (vr_value
[i
]);
10258 free (vr_phi_edge_counts
);
10260 /* So that we can distinguish between VRP data being available
10261 and not available. */
10263 vr_phi_edge_counts
= NULL
;
10267 /* Main entry point to VRP (Value Range Propagation). This pass is
10268 loosely based on J. R. C. Patterson, ``Accurate Static Branch
10269 Prediction by Value Range Propagation,'' in SIGPLAN Conference on
10270 Programming Language Design and Implementation, pp. 67-78, 1995.
10271 Also available at http://citeseer.ist.psu.edu/patterson95accurate.html
10273 This is essentially an SSA-CCP pass modified to deal with ranges
10274 instead of constants.
10276 While propagating ranges, we may find that two or more SSA name
10277 have equivalent, though distinct ranges. For instance,
10280 2 p_4 = ASSERT_EXPR <p_3, p_3 != 0>
10282 4 p_5 = ASSERT_EXPR <p_4, p_4 == q_2>;
10286 In the code above, pointer p_5 has range [q_2, q_2], but from the
10287 code we can also determine that p_5 cannot be NULL and, if q_2 had
10288 a non-varying range, p_5's range should also be compatible with it.
10290 These equivalences are created by two expressions: ASSERT_EXPR and
10291 copy operations. Since p_5 is an assertion on p_4, and p_4 was the
10292 result of another assertion, then we can use the fact that p_5 and
10293 p_4 are equivalent when evaluating p_5's range.
10295 Together with value ranges, we also propagate these equivalences
10296 between names so that we can take advantage of information from
10297 multiple ranges when doing final replacement. Note that this
10298 equivalency relation is transitive but not symmetric.
10300 In the example above, p_5 is equivalent to p_4, q_2 and p_3, but we
10301 cannot assert that q_2 is equivalent to p_5 because q_2 may be used
10302 in contexts where that assertion does not hold (e.g., in line 6).
10304 TODO, the main difference between this pass and Patterson's is that
10305 we do not propagate edge probabilities. We only compute whether
10306 edges can be taken or not. That is, instead of having a spectrum
10307 of jump probabilities between 0 and 1, we only deal with 0, 1 and
10308 DON'T KNOW. In the future, it may be worthwhile to propagate
10309 probabilities to aid branch prediction. */
10311 static unsigned int
10318 loop_optimizer_init (LOOPS_NORMAL
| LOOPS_HAVE_RECORDED_EXITS
);
10319 rewrite_into_loop_closed_ssa (NULL
, TODO_update_ssa
);
10320 scev_initialize ();
10322 /* ??? This ends up using stale EDGE_DFS_BACK for liveness computation.
10323 Inserting assertions may split edges which will invalidate
10325 insert_range_assertions ();
10327 to_remove_edges
.create (10);
10328 to_update_switch_stmts
.create (5);
10329 threadedge_initialize_values ();
10331 /* For visiting PHI nodes we need EDGE_DFS_BACK computed. */
10332 mark_dfs_back_edges ();
10335 ssa_propagate (vrp_visit_stmt
, vrp_visit_phi_node
);
10338 free_numbers_of_iterations_estimates ();
10340 /* ASSERT_EXPRs must be removed before finalizing jump threads
10341 as finalizing jump threads calls the CFG cleanup code which
10342 does not properly handle ASSERT_EXPRs. */
10343 remove_range_assertions ();
10345 /* If we exposed any new variables, go ahead and put them into
10346 SSA form now, before we handle jump threading. This simplifies
10347 interactions between rewriting of _DECL nodes into SSA form
10348 and rewriting SSA_NAME nodes into SSA form after block
10349 duplication and CFG manipulation. */
10350 update_ssa (TODO_update_ssa
);
10352 finalize_jump_threads ();
10354 /* Remove dead edges from SWITCH_EXPR optimization. This leaves the
10355 CFG in a broken state and requires a cfg_cleanup run. */
10356 FOR_EACH_VEC_ELT (to_remove_edges
, i
, e
)
10358 /* Update SWITCH_EXPR case label vector. */
10359 FOR_EACH_VEC_ELT (to_update_switch_stmts
, i
, su
)
10362 size_t n
= TREE_VEC_LENGTH (su
->vec
);
10364 gimple_switch_set_num_labels (su
->stmt
, n
);
10365 for (j
= 0; j
< n
; j
++)
10366 gimple_switch_set_label (su
->stmt
, j
, TREE_VEC_ELT (su
->vec
, j
));
10367 /* As we may have replaced the default label with a regular one
10368 make sure to make it a real default label again. This ensures
10369 optimal expansion. */
10370 label
= gimple_switch_label (su
->stmt
, 0);
10371 CASE_LOW (label
) = NULL_TREE
;
10372 CASE_HIGH (label
) = NULL_TREE
;
10375 if (to_remove_edges
.length () > 0)
10377 free_dominance_info (CDI_DOMINATORS
);
10378 loops_state_set (LOOPS_NEED_FIXUP
);
10381 to_remove_edges
.release ();
10382 to_update_switch_stmts
.release ();
10383 threadedge_finalize_values ();
10386 loop_optimizer_finalize ();
10392 const pass_data pass_data_vrp
=
10394 GIMPLE_PASS
, /* type */
10396 OPTGROUP_NONE
, /* optinfo_flags */
10397 TV_TREE_VRP
, /* tv_id */
10398 PROP_ssa
, /* properties_required */
10399 0, /* properties_provided */
10400 0, /* properties_destroyed */
10401 0, /* todo_flags_start */
10402 ( TODO_cleanup_cfg
| TODO_update_ssa
), /* todo_flags_finish */
10405 class pass_vrp
: public gimple_opt_pass
10408 pass_vrp (gcc::context
*ctxt
)
10409 : gimple_opt_pass (pass_data_vrp
, ctxt
)
10412 /* opt_pass methods: */
10413 opt_pass
* clone () { return new pass_vrp (m_ctxt
); }
10414 virtual bool gate (function
*) { return flag_tree_vrp
!= 0; }
10415 virtual unsigned int execute (function
*) { return execute_vrp (); }
10417 }; // class pass_vrp
10419 } // anon namespace
10422 make_pass_vrp (gcc::context
*ctxt
)
10424 return new pass_vrp (ctxt
);