1 /* Support routines for Value Range Propagation (VRP).
2 Copyright (C) 2005-2013 Free Software Foundation, Inc.
3 Contributed by Diego Novillo <dnovillo@redhat.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
23 #include "coretypes.h"
27 #include "stor-layout.h"
29 #include "basic-block.h"
30 #include "tree-ssa-alias.h"
31 #include "internal-fn.h"
32 #include "gimple-fold.h"
34 #include "gimple-expr.h"
37 #include "gimple-iterator.h"
38 #include "gimple-walk.h"
39 #include "gimple-ssa.h"
41 #include "tree-phinodes.h"
42 #include "ssa-iterators.h"
43 #include "stringpool.h"
44 #include "tree-ssanames.h"
45 #include "tree-ssa-loop-manip.h"
46 #include "tree-ssa-loop-niter.h"
47 #include "tree-ssa-loop.h"
48 #include "tree-into-ssa.h"
50 #include "tree-pass.h"
51 #include "tree-dump.h"
52 #include "gimple-pretty-print.h"
53 #include "diagnostic-core.h"
56 #include "tree-scalar-evolution.h"
57 #include "tree-ssa-propagate.h"
58 #include "tree-chrec.h"
59 #include "tree-ssa-threadupdate.h"
62 #include "tree-ssa-threadedge.h"
66 /* Range of values that can be associated with an SSA_NAME after VRP
70 /* Lattice value represented by this range. */
71 enum value_range_type type
;
73 /* Minimum and maximum values represented by this range. These
74 values should be interpreted as follows:
76 - If TYPE is VR_UNDEFINED or VR_VARYING then MIN and MAX must
79 - If TYPE == VR_RANGE then MIN holds the minimum value and
80 MAX holds the maximum value of the range [MIN, MAX].
82 - If TYPE == ANTI_RANGE the variable is known to NOT
83 take any values in the range [MIN, MAX]. */
87 /* Set of SSA names whose value ranges are equivalent to this one.
88 This set is only valid when TYPE is VR_RANGE or VR_ANTI_RANGE. */
92 typedef struct value_range_d value_range_t
;
94 #define VR_INITIALIZER { VR_UNDEFINED, NULL_TREE, NULL_TREE, NULL }
96 /* Set of SSA names found live during the RPO traversal of the function
97 for still active basic-blocks. */
100 /* Return true if the SSA name NAME is live on the edge E. */
103 live_on_edge (edge e
, tree name
)
105 return (live
[e
->dest
->index
]
106 && bitmap_bit_p (live
[e
->dest
->index
], SSA_NAME_VERSION (name
)));
109 /* Local functions. */
110 static int compare_values (tree val1
, tree val2
);
111 static int compare_values_warnv (tree val1
, tree val2
, bool *);
112 static void vrp_meet (value_range_t
*, value_range_t
*);
113 static void vrp_intersect_ranges (value_range_t
*, value_range_t
*);
114 static tree
vrp_evaluate_conditional_warnv_with_ops (enum tree_code
,
115 tree
, tree
, bool, bool *,
118 /* Location information for ASSERT_EXPRs. Each instance of this
119 structure describes an ASSERT_EXPR for an SSA name. Since a single
120 SSA name may have more than one assertion associated with it, these
121 locations are kept in a linked list attached to the corresponding
123 struct assert_locus_d
125 /* Basic block where the assertion would be inserted. */
128 /* Some assertions need to be inserted on an edge (e.g., assertions
129 generated by COND_EXPRs). In those cases, BB will be NULL. */
132 /* Pointer to the statement that generated this assertion. */
133 gimple_stmt_iterator si
;
135 /* Predicate code for the ASSERT_EXPR. Must be COMPARISON_CLASS_P. */
136 enum tree_code comp_code
;
138 /* Value being compared against. */
141 /* Expression to compare. */
144 /* Next node in the linked list. */
145 struct assert_locus_d
*next
;
148 typedef struct assert_locus_d
*assert_locus_t
;
150 /* If bit I is present, it means that SSA name N_i has a list of
151 assertions that should be inserted in the IL. */
152 static bitmap need_assert_for
;
154 /* Array of locations lists where to insert assertions. ASSERTS_FOR[I]
155 holds a list of ASSERT_LOCUS_T nodes that describe where
156 ASSERT_EXPRs for SSA name N_I should be inserted. */
157 static assert_locus_t
*asserts_for
;
159 /* Value range array. After propagation, VR_VALUE[I] holds the range
160 of values that SSA name N_I may take. */
161 static unsigned num_vr_values
;
162 static value_range_t
**vr_value
;
163 static bool values_propagated
;
165 /* For a PHI node which sets SSA name N_I, VR_COUNTS[I] holds the
166 number of executable edges we saw the last time we visited the
168 static int *vr_phi_edge_counts
;
175 static vec
<edge
> to_remove_edges
;
176 static vec
<switch_update
> to_update_switch_stmts
;
179 /* Return the maximum value for TYPE. */
182 vrp_val_max (const_tree type
)
184 if (!INTEGRAL_TYPE_P (type
))
187 return TYPE_MAX_VALUE (type
);
190 /* Return the minimum value for TYPE. */
193 vrp_val_min (const_tree type
)
195 if (!INTEGRAL_TYPE_P (type
))
198 return TYPE_MIN_VALUE (type
);
201 /* Return whether VAL is equal to the maximum value of its type. This
202 will be true for a positive overflow infinity. We can't do a
203 simple equality comparison with TYPE_MAX_VALUE because C typedefs
204 and Ada subtypes can produce types whose TYPE_MAX_VALUE is not ==
205 to the integer constant with the same value in the type. */
208 vrp_val_is_max (const_tree val
)
210 tree type_max
= vrp_val_max (TREE_TYPE (val
));
211 return (val
== type_max
212 || (type_max
!= NULL_TREE
213 && operand_equal_p (val
, type_max
, 0)));
216 /* Return whether VAL is equal to the minimum value of its type. This
217 will be true for a negative overflow infinity. */
220 vrp_val_is_min (const_tree val
)
222 tree type_min
= vrp_val_min (TREE_TYPE (val
));
223 return (val
== type_min
224 || (type_min
!= NULL_TREE
225 && operand_equal_p (val
, type_min
, 0)));
229 /* Return whether TYPE should use an overflow infinity distinct from
230 TYPE_{MIN,MAX}_VALUE. We use an overflow infinity value to
231 represent a signed overflow during VRP computations. An infinity
232 is distinct from a half-range, which will go from some number to
233 TYPE_{MIN,MAX}_VALUE. */
236 needs_overflow_infinity (const_tree type
)
238 return INTEGRAL_TYPE_P (type
) && !TYPE_OVERFLOW_WRAPS (type
);
241 /* Return whether TYPE can support our overflow infinity
242 representation: we use the TREE_OVERFLOW flag, which only exists
243 for constants. If TYPE doesn't support this, we don't optimize
244 cases which would require signed overflow--we drop them to
248 supports_overflow_infinity (const_tree type
)
250 tree min
= vrp_val_min (type
), max
= vrp_val_max (type
);
251 #ifdef ENABLE_CHECKING
252 gcc_assert (needs_overflow_infinity (type
));
254 return (min
!= NULL_TREE
255 && CONSTANT_CLASS_P (min
)
257 && CONSTANT_CLASS_P (max
));
260 /* VAL is the maximum or minimum value of a type. Return a
261 corresponding overflow infinity. */
264 make_overflow_infinity (tree val
)
266 gcc_checking_assert (val
!= NULL_TREE
&& CONSTANT_CLASS_P (val
));
267 val
= copy_node (val
);
268 TREE_OVERFLOW (val
) = 1;
272 /* Return a negative overflow infinity for TYPE. */
275 negative_overflow_infinity (tree type
)
277 gcc_checking_assert (supports_overflow_infinity (type
));
278 return make_overflow_infinity (vrp_val_min (type
));
281 /* Return a positive overflow infinity for TYPE. */
284 positive_overflow_infinity (tree type
)
286 gcc_checking_assert (supports_overflow_infinity (type
));
287 return make_overflow_infinity (vrp_val_max (type
));
290 /* Return whether VAL is a negative overflow infinity. */
293 is_negative_overflow_infinity (const_tree val
)
295 return (needs_overflow_infinity (TREE_TYPE (val
))
296 && CONSTANT_CLASS_P (val
)
297 && TREE_OVERFLOW (val
)
298 && vrp_val_is_min (val
));
301 /* Return whether VAL is a positive overflow infinity. */
304 is_positive_overflow_infinity (const_tree val
)
306 return (needs_overflow_infinity (TREE_TYPE (val
))
307 && CONSTANT_CLASS_P (val
)
308 && TREE_OVERFLOW (val
)
309 && vrp_val_is_max (val
));
312 /* Return whether VAL is a positive or negative overflow infinity. */
315 is_overflow_infinity (const_tree val
)
317 return (needs_overflow_infinity (TREE_TYPE (val
))
318 && CONSTANT_CLASS_P (val
)
319 && TREE_OVERFLOW (val
)
320 && (vrp_val_is_min (val
) || vrp_val_is_max (val
)));
323 /* Return whether STMT has a constant rhs that is_overflow_infinity. */
326 stmt_overflow_infinity (gimple stmt
)
328 if (is_gimple_assign (stmt
)
329 && get_gimple_rhs_class (gimple_assign_rhs_code (stmt
)) ==
331 return is_overflow_infinity (gimple_assign_rhs1 (stmt
));
335 /* If VAL is now an overflow infinity, return VAL. Otherwise, return
336 the same value with TREE_OVERFLOW clear. This can be used to avoid
337 confusing a regular value with an overflow value. */
340 avoid_overflow_infinity (tree val
)
342 if (!is_overflow_infinity (val
))
345 if (vrp_val_is_max (val
))
346 return vrp_val_max (TREE_TYPE (val
));
349 gcc_checking_assert (vrp_val_is_min (val
));
350 return vrp_val_min (TREE_TYPE (val
));
355 /* Return true if ARG is marked with the nonnull attribute in the
356 current function signature. */
359 nonnull_arg_p (const_tree arg
)
361 tree t
, attrs
, fntype
;
362 unsigned HOST_WIDE_INT arg_num
;
364 gcc_assert (TREE_CODE (arg
) == PARM_DECL
&& POINTER_TYPE_P (TREE_TYPE (arg
)));
366 /* The static chain decl is always non null. */
367 if (arg
== cfun
->static_chain_decl
)
370 fntype
= TREE_TYPE (current_function_decl
);
371 for (attrs
= TYPE_ATTRIBUTES (fntype
); attrs
; attrs
= TREE_CHAIN (attrs
))
373 attrs
= lookup_attribute ("nonnull", attrs
);
375 /* If "nonnull" wasn't specified, we know nothing about the argument. */
376 if (attrs
== NULL_TREE
)
379 /* If "nonnull" applies to all the arguments, then ARG is non-null. */
380 if (TREE_VALUE (attrs
) == NULL_TREE
)
383 /* Get the position number for ARG in the function signature. */
384 for (arg_num
= 1, t
= DECL_ARGUMENTS (current_function_decl
);
386 t
= DECL_CHAIN (t
), arg_num
++)
392 gcc_assert (t
== arg
);
394 /* Now see if ARG_NUM is mentioned in the nonnull list. */
395 for (t
= TREE_VALUE (attrs
); t
; t
= TREE_CHAIN (t
))
397 if (compare_tree_int (TREE_VALUE (t
), arg_num
) == 0)
406 /* Set value range VR to VR_UNDEFINED. */
409 set_value_range_to_undefined (value_range_t
*vr
)
411 vr
->type
= VR_UNDEFINED
;
412 vr
->min
= vr
->max
= NULL_TREE
;
414 bitmap_clear (vr
->equiv
);
418 /* Set value range VR to VR_VARYING. */
421 set_value_range_to_varying (value_range_t
*vr
)
423 vr
->type
= VR_VARYING
;
424 vr
->min
= vr
->max
= NULL_TREE
;
426 bitmap_clear (vr
->equiv
);
430 /* Set value range VR to {T, MIN, MAX, EQUIV}. */
433 set_value_range (value_range_t
*vr
, enum value_range_type t
, tree min
,
434 tree max
, bitmap equiv
)
436 #if defined ENABLE_CHECKING
437 /* Check the validity of the range. */
438 if (t
== VR_RANGE
|| t
== VR_ANTI_RANGE
)
442 gcc_assert (min
&& max
);
444 gcc_assert ((!TREE_OVERFLOW_P (min
) || is_overflow_infinity (min
))
445 && (!TREE_OVERFLOW_P (max
) || is_overflow_infinity (max
)));
447 if (INTEGRAL_TYPE_P (TREE_TYPE (min
)) && t
== VR_ANTI_RANGE
)
448 gcc_assert (!vrp_val_is_min (min
) || !vrp_val_is_max (max
));
450 cmp
= compare_values (min
, max
);
451 gcc_assert (cmp
== 0 || cmp
== -1 || cmp
== -2);
453 if (needs_overflow_infinity (TREE_TYPE (min
)))
454 gcc_assert (!is_overflow_infinity (min
)
455 || !is_overflow_infinity (max
));
458 if (t
== VR_UNDEFINED
|| t
== VR_VARYING
)
459 gcc_assert (min
== NULL_TREE
&& max
== NULL_TREE
);
461 if (t
== VR_UNDEFINED
|| t
== VR_VARYING
)
462 gcc_assert (equiv
== NULL
|| bitmap_empty_p (equiv
));
469 /* Since updating the equivalence set involves deep copying the
470 bitmaps, only do it if absolutely necessary. */
471 if (vr
->equiv
== NULL
473 vr
->equiv
= BITMAP_ALLOC (NULL
);
475 if (equiv
!= vr
->equiv
)
477 if (equiv
&& !bitmap_empty_p (equiv
))
478 bitmap_copy (vr
->equiv
, equiv
);
480 bitmap_clear (vr
->equiv
);
485 /* Set value range VR to the canonical form of {T, MIN, MAX, EQUIV}.
486 This means adjusting T, MIN and MAX representing the case of a
487 wrapping range with MAX < MIN covering [MIN, type_max] U [type_min, MAX]
488 as anti-rage ~[MAX+1, MIN-1]. Likewise for wrapping anti-ranges.
489 In corner cases where MAX+1 or MIN-1 wraps this will fall back
491 This routine exists to ease canonicalization in the case where we
492 extract ranges from var + CST op limit. */
495 set_and_canonicalize_value_range (value_range_t
*vr
, enum value_range_type t
,
496 tree min
, tree max
, bitmap equiv
)
498 /* Use the canonical setters for VR_UNDEFINED and VR_VARYING. */
499 if (t
== VR_UNDEFINED
)
501 set_value_range_to_undefined (vr
);
504 else if (t
== VR_VARYING
)
506 set_value_range_to_varying (vr
);
510 /* Nothing to canonicalize for symbolic ranges. */
511 if (TREE_CODE (min
) != INTEGER_CST
512 || TREE_CODE (max
) != INTEGER_CST
)
514 set_value_range (vr
, t
, min
, max
, equiv
);
518 /* Wrong order for min and max, to swap them and the VR type we need
520 if (tree_int_cst_lt (max
, min
))
524 /* For one bit precision if max < min, then the swapped
525 range covers all values, so for VR_RANGE it is varying and
526 for VR_ANTI_RANGE empty range, so drop to varying as well. */
527 if (TYPE_PRECISION (TREE_TYPE (min
)) == 1)
529 set_value_range_to_varying (vr
);
533 one
= build_int_cst (TREE_TYPE (min
), 1);
534 tmp
= int_const_binop (PLUS_EXPR
, max
, one
);
535 max
= int_const_binop (MINUS_EXPR
, min
, one
);
538 /* There's one corner case, if we had [C+1, C] before we now have
539 that again. But this represents an empty value range, so drop
540 to varying in this case. */
541 if (tree_int_cst_lt (max
, min
))
543 set_value_range_to_varying (vr
);
547 t
= t
== VR_RANGE
? VR_ANTI_RANGE
: VR_RANGE
;
550 /* Anti-ranges that can be represented as ranges should be so. */
551 if (t
== VR_ANTI_RANGE
)
553 bool is_min
= vrp_val_is_min (min
);
554 bool is_max
= vrp_val_is_max (max
);
556 if (is_min
&& is_max
)
558 /* We cannot deal with empty ranges, drop to varying.
559 ??? This could be VR_UNDEFINED instead. */
560 set_value_range_to_varying (vr
);
563 else if (TYPE_PRECISION (TREE_TYPE (min
)) == 1
564 && (is_min
|| is_max
))
566 /* Non-empty boolean ranges can always be represented
567 as a singleton range. */
569 min
= max
= vrp_val_max (TREE_TYPE (min
));
571 min
= max
= vrp_val_min (TREE_TYPE (min
));
575 /* As a special exception preserve non-null ranges. */
576 && !(TYPE_UNSIGNED (TREE_TYPE (min
))
577 && integer_zerop (max
)))
579 tree one
= build_int_cst (TREE_TYPE (max
), 1);
580 min
= int_const_binop (PLUS_EXPR
, max
, one
);
581 max
= vrp_val_max (TREE_TYPE (max
));
586 tree one
= build_int_cst (TREE_TYPE (min
), 1);
587 max
= int_const_binop (MINUS_EXPR
, min
, one
);
588 min
= vrp_val_min (TREE_TYPE (min
));
593 /* Drop [-INF(OVF), +INF(OVF)] to varying. */
594 if (needs_overflow_infinity (TREE_TYPE (min
))
595 && is_overflow_infinity (min
)
596 && is_overflow_infinity (max
))
598 set_value_range_to_varying (vr
);
602 set_value_range (vr
, t
, min
, max
, equiv
);
605 /* Copy value range FROM into value range TO. */
608 copy_value_range (value_range_t
*to
, value_range_t
*from
)
610 set_value_range (to
, from
->type
, from
->min
, from
->max
, from
->equiv
);
613 /* Set value range VR to a single value. This function is only called
614 with values we get from statements, and exists to clear the
615 TREE_OVERFLOW flag so that we don't think we have an overflow
616 infinity when we shouldn't. */
619 set_value_range_to_value (value_range_t
*vr
, tree val
, bitmap equiv
)
621 gcc_assert (is_gimple_min_invariant (val
));
622 if (TREE_OVERFLOW_P (val
))
623 val
= drop_tree_overflow (val
);
624 set_value_range (vr
, VR_RANGE
, val
, val
, equiv
);
627 /* Set value range VR to a non-negative range of type TYPE.
628 OVERFLOW_INFINITY indicates whether to use an overflow infinity
629 rather than TYPE_MAX_VALUE; this should be true if we determine
630 that the range is nonnegative based on the assumption that signed
631 overflow does not occur. */
634 set_value_range_to_nonnegative (value_range_t
*vr
, tree type
,
635 bool overflow_infinity
)
639 if (overflow_infinity
&& !supports_overflow_infinity (type
))
641 set_value_range_to_varying (vr
);
645 zero
= build_int_cst (type
, 0);
646 set_value_range (vr
, VR_RANGE
, zero
,
648 ? positive_overflow_infinity (type
)
649 : TYPE_MAX_VALUE (type
)),
653 /* Set value range VR to a non-NULL range of type TYPE. */
656 set_value_range_to_nonnull (value_range_t
*vr
, tree type
)
658 tree zero
= build_int_cst (type
, 0);
659 set_value_range (vr
, VR_ANTI_RANGE
, zero
, zero
, vr
->equiv
);
663 /* Set value range VR to a NULL range of type TYPE. */
666 set_value_range_to_null (value_range_t
*vr
, tree type
)
668 set_value_range_to_value (vr
, build_int_cst (type
, 0), vr
->equiv
);
672 /* Set value range VR to a range of a truthvalue of type TYPE. */
675 set_value_range_to_truthvalue (value_range_t
*vr
, tree type
)
677 if (TYPE_PRECISION (type
) == 1)
678 set_value_range_to_varying (vr
);
680 set_value_range (vr
, VR_RANGE
,
681 build_int_cst (type
, 0), build_int_cst (type
, 1),
686 /* If abs (min) < abs (max), set VR to [-max, max], if
687 abs (min) >= abs (max), set VR to [-min, min]. */
690 abs_extent_range (value_range_t
*vr
, tree min
, tree max
)
694 gcc_assert (TREE_CODE (min
) == INTEGER_CST
);
695 gcc_assert (TREE_CODE (max
) == INTEGER_CST
);
696 gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (min
)));
697 gcc_assert (!TYPE_UNSIGNED (TREE_TYPE (min
)));
698 min
= fold_unary (ABS_EXPR
, TREE_TYPE (min
), min
);
699 max
= fold_unary (ABS_EXPR
, TREE_TYPE (max
), max
);
700 if (TREE_OVERFLOW (min
) || TREE_OVERFLOW (max
))
702 set_value_range_to_varying (vr
);
705 cmp
= compare_values (min
, max
);
707 min
= fold_unary (NEGATE_EXPR
, TREE_TYPE (min
), max
);
708 else if (cmp
== 0 || cmp
== 1)
711 min
= fold_unary (NEGATE_EXPR
, TREE_TYPE (min
), min
);
715 set_value_range_to_varying (vr
);
718 set_and_canonicalize_value_range (vr
, VR_RANGE
, min
, max
, NULL
);
722 /* Return value range information for VAR.
724 If we have no values ranges recorded (ie, VRP is not running), then
725 return NULL. Otherwise create an empty range if none existed for VAR. */
727 static value_range_t
*
728 get_value_range (const_tree var
)
730 static const struct value_range_d vr_const_varying
731 = { VR_VARYING
, NULL_TREE
, NULL_TREE
, NULL
};
734 unsigned ver
= SSA_NAME_VERSION (var
);
736 /* If we have no recorded ranges, then return NULL. */
740 /* If we query the range for a new SSA name return an unmodifiable VARYING.
741 We should get here at most from the substitute-and-fold stage which
742 will never try to change values. */
743 if (ver
>= num_vr_values
)
744 return CONST_CAST (value_range_t
*, &vr_const_varying
);
750 /* After propagation finished do not allocate new value-ranges. */
751 if (values_propagated
)
752 return CONST_CAST (value_range_t
*, &vr_const_varying
);
754 /* Create a default value range. */
755 vr_value
[ver
] = vr
= XCNEW (value_range_t
);
757 /* Defer allocating the equivalence set. */
760 /* If VAR is a default definition of a parameter, the variable can
761 take any value in VAR's type. */
762 if (SSA_NAME_IS_DEFAULT_DEF (var
))
764 sym
= SSA_NAME_VAR (var
);
765 if (TREE_CODE (sym
) == PARM_DECL
)
767 /* Try to use the "nonnull" attribute to create ~[0, 0]
768 anti-ranges for pointers. Note that this is only valid with
769 default definitions of PARM_DECLs. */
770 if (POINTER_TYPE_P (TREE_TYPE (sym
))
771 && nonnull_arg_p (sym
))
772 set_value_range_to_nonnull (vr
, TREE_TYPE (sym
));
774 set_value_range_to_varying (vr
);
776 else if (TREE_CODE (sym
) == RESULT_DECL
777 && DECL_BY_REFERENCE (sym
))
778 set_value_range_to_nonnull (vr
, TREE_TYPE (sym
));
784 /* Return true, if VAL1 and VAL2 are equal values for VRP purposes. */
787 vrp_operand_equal_p (const_tree val1
, const_tree val2
)
791 if (!val1
|| !val2
|| !operand_equal_p (val1
, val2
, 0))
793 if (is_overflow_infinity (val1
))
794 return is_overflow_infinity (val2
);
798 /* Return true, if the bitmaps B1 and B2 are equal. */
801 vrp_bitmap_equal_p (const_bitmap b1
, const_bitmap b2
)
804 || ((!b1
|| bitmap_empty_p (b1
))
805 && (!b2
|| bitmap_empty_p (b2
)))
807 && bitmap_equal_p (b1
, b2
)));
810 /* Update the value range and equivalence set for variable VAR to
811 NEW_VR. Return true if NEW_VR is different from VAR's previous
814 NOTE: This function assumes that NEW_VR is a temporary value range
815 object created for the sole purpose of updating VAR's range. The
816 storage used by the equivalence set from NEW_VR will be freed by
817 this function. Do not call update_value_range when NEW_VR
818 is the range object associated with another SSA name. */
821 update_value_range (const_tree var
, value_range_t
*new_vr
)
823 value_range_t
*old_vr
;
826 /* Update the value range, if necessary. */
827 old_vr
= get_value_range (var
);
828 is_new
= old_vr
->type
!= new_vr
->type
829 || !vrp_operand_equal_p (old_vr
->min
, new_vr
->min
)
830 || !vrp_operand_equal_p (old_vr
->max
, new_vr
->max
)
831 || !vrp_bitmap_equal_p (old_vr
->equiv
, new_vr
->equiv
);
835 /* Do not allow transitions up the lattice. The following
836 is slightly more awkward than just new_vr->type < old_vr->type
837 because VR_RANGE and VR_ANTI_RANGE need to be considered
838 the same. We may not have is_new when transitioning to
839 UNDEFINED or from VARYING. */
840 if (new_vr
->type
== VR_UNDEFINED
841 || old_vr
->type
== VR_VARYING
)
842 set_value_range_to_varying (old_vr
);
844 set_value_range (old_vr
, new_vr
->type
, new_vr
->min
, new_vr
->max
,
848 BITMAP_FREE (new_vr
->equiv
);
854 /* Add VAR and VAR's equivalence set to EQUIV. This is the central
855 point where equivalence processing can be turned on/off. */
858 add_equivalence (bitmap
*equiv
, const_tree var
)
860 unsigned ver
= SSA_NAME_VERSION (var
);
861 value_range_t
*vr
= vr_value
[ver
];
864 *equiv
= BITMAP_ALLOC (NULL
);
865 bitmap_set_bit (*equiv
, ver
);
867 bitmap_ior_into (*equiv
, vr
->equiv
);
871 /* Return true if VR is ~[0, 0]. */
874 range_is_nonnull (value_range_t
*vr
)
876 return vr
->type
== VR_ANTI_RANGE
877 && integer_zerop (vr
->min
)
878 && integer_zerop (vr
->max
);
882 /* Return true if VR is [0, 0]. */
885 range_is_null (value_range_t
*vr
)
887 return vr
->type
== VR_RANGE
888 && integer_zerop (vr
->min
)
889 && integer_zerop (vr
->max
);
892 /* Return true if max and min of VR are INTEGER_CST. It's not necessary
896 range_int_cst_p (value_range_t
*vr
)
898 return (vr
->type
== VR_RANGE
899 && TREE_CODE (vr
->max
) == INTEGER_CST
900 && TREE_CODE (vr
->min
) == INTEGER_CST
);
903 /* Return true if VR is a INTEGER_CST singleton. */
906 range_int_cst_singleton_p (value_range_t
*vr
)
908 return (range_int_cst_p (vr
)
909 && !is_overflow_infinity (vr
->min
)
910 && !is_overflow_infinity (vr
->max
)
911 && tree_int_cst_equal (vr
->min
, vr
->max
));
914 /* Return true if value range VR involves at least one symbol. */
917 symbolic_range_p (value_range_t
*vr
)
919 return (!is_gimple_min_invariant (vr
->min
)
920 || !is_gimple_min_invariant (vr
->max
));
923 /* Return true if value range VR uses an overflow infinity. */
926 overflow_infinity_range_p (value_range_t
*vr
)
928 return (vr
->type
== VR_RANGE
929 && (is_overflow_infinity (vr
->min
)
930 || is_overflow_infinity (vr
->max
)));
933 /* Return false if we can not make a valid comparison based on VR;
934 this will be the case if it uses an overflow infinity and overflow
935 is not undefined (i.e., -fno-strict-overflow is in effect).
936 Otherwise return true, and set *STRICT_OVERFLOW_P to true if VR
937 uses an overflow infinity. */
940 usable_range_p (value_range_t
*vr
, bool *strict_overflow_p
)
942 gcc_assert (vr
->type
== VR_RANGE
);
943 if (is_overflow_infinity (vr
->min
))
945 *strict_overflow_p
= true;
946 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (vr
->min
)))
949 if (is_overflow_infinity (vr
->max
))
951 *strict_overflow_p
= true;
952 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (vr
->max
)))
959 /* Return true if the result of assignment STMT is know to be non-negative.
960 If the return value is based on the assumption that signed overflow is
961 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
962 *STRICT_OVERFLOW_P.*/
965 gimple_assign_nonnegative_warnv_p (gimple stmt
, bool *strict_overflow_p
)
967 enum tree_code code
= gimple_assign_rhs_code (stmt
);
968 switch (get_gimple_rhs_class (code
))
970 case GIMPLE_UNARY_RHS
:
971 return tree_unary_nonnegative_warnv_p (gimple_assign_rhs_code (stmt
),
972 gimple_expr_type (stmt
),
973 gimple_assign_rhs1 (stmt
),
975 case GIMPLE_BINARY_RHS
:
976 return tree_binary_nonnegative_warnv_p (gimple_assign_rhs_code (stmt
),
977 gimple_expr_type (stmt
),
978 gimple_assign_rhs1 (stmt
),
979 gimple_assign_rhs2 (stmt
),
981 case GIMPLE_TERNARY_RHS
:
983 case GIMPLE_SINGLE_RHS
:
984 return tree_single_nonnegative_warnv_p (gimple_assign_rhs1 (stmt
),
986 case GIMPLE_INVALID_RHS
:
993 /* Return true if return value of call STMT is know to be non-negative.
994 If the return value is based on the assumption that signed overflow is
995 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
996 *STRICT_OVERFLOW_P.*/
999 gimple_call_nonnegative_warnv_p (gimple stmt
, bool *strict_overflow_p
)
1001 tree arg0
= gimple_call_num_args (stmt
) > 0 ?
1002 gimple_call_arg (stmt
, 0) : NULL_TREE
;
1003 tree arg1
= gimple_call_num_args (stmt
) > 1 ?
1004 gimple_call_arg (stmt
, 1) : NULL_TREE
;
1006 return tree_call_nonnegative_warnv_p (gimple_expr_type (stmt
),
1007 gimple_call_fndecl (stmt
),
1013 /* Return true if STMT is know to to compute a non-negative value.
1014 If the return value is based on the assumption that signed overflow is
1015 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1016 *STRICT_OVERFLOW_P.*/
1019 gimple_stmt_nonnegative_warnv_p (gimple stmt
, bool *strict_overflow_p
)
1021 switch (gimple_code (stmt
))
1024 return gimple_assign_nonnegative_warnv_p (stmt
, strict_overflow_p
);
1026 return gimple_call_nonnegative_warnv_p (stmt
, strict_overflow_p
);
1032 /* Return true if the result of assignment STMT is know to be non-zero.
1033 If the return value is based on the assumption that signed overflow is
1034 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1035 *STRICT_OVERFLOW_P.*/
1038 gimple_assign_nonzero_warnv_p (gimple stmt
, bool *strict_overflow_p
)
1040 enum tree_code code
= gimple_assign_rhs_code (stmt
);
1041 switch (get_gimple_rhs_class (code
))
1043 case GIMPLE_UNARY_RHS
:
1044 return tree_unary_nonzero_warnv_p (gimple_assign_rhs_code (stmt
),
1045 gimple_expr_type (stmt
),
1046 gimple_assign_rhs1 (stmt
),
1048 case GIMPLE_BINARY_RHS
:
1049 return tree_binary_nonzero_warnv_p (gimple_assign_rhs_code (stmt
),
1050 gimple_expr_type (stmt
),
1051 gimple_assign_rhs1 (stmt
),
1052 gimple_assign_rhs2 (stmt
),
1054 case GIMPLE_TERNARY_RHS
:
1056 case GIMPLE_SINGLE_RHS
:
1057 return tree_single_nonzero_warnv_p (gimple_assign_rhs1 (stmt
),
1059 case GIMPLE_INVALID_RHS
:
1066 /* Return true if STMT is known to compute a non-zero value.
1067 If the return value is based on the assumption that signed overflow is
1068 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1069 *STRICT_OVERFLOW_P.*/
1072 gimple_stmt_nonzero_warnv_p (gimple stmt
, bool *strict_overflow_p
)
1074 switch (gimple_code (stmt
))
1077 return gimple_assign_nonzero_warnv_p (stmt
, strict_overflow_p
);
1080 tree fndecl
= gimple_call_fndecl (stmt
);
1081 if (!fndecl
) return false;
1082 if (flag_delete_null_pointer_checks
&& !flag_check_new
1083 && DECL_IS_OPERATOR_NEW (fndecl
)
1084 && !TREE_NOTHROW (fndecl
))
1086 if (flag_delete_null_pointer_checks
&&
1087 lookup_attribute ("returns_nonnull",
1088 TYPE_ATTRIBUTES (gimple_call_fntype (stmt
))))
1090 return gimple_alloca_call_p (stmt
);
1097 /* Like tree_expr_nonzero_warnv_p, but this function uses value ranges
1101 vrp_stmt_computes_nonzero (gimple stmt
, bool *strict_overflow_p
)
1103 if (gimple_stmt_nonzero_warnv_p (stmt
, strict_overflow_p
))
1106 /* If we have an expression of the form &X->a, then the expression
1107 is nonnull if X is nonnull. */
1108 if (is_gimple_assign (stmt
)
1109 && gimple_assign_rhs_code (stmt
) == ADDR_EXPR
)
1111 tree expr
= gimple_assign_rhs1 (stmt
);
1112 tree base
= get_base_address (TREE_OPERAND (expr
, 0));
1114 if (base
!= NULL_TREE
1115 && TREE_CODE (base
) == MEM_REF
1116 && TREE_CODE (TREE_OPERAND (base
, 0)) == SSA_NAME
)
1118 value_range_t
*vr
= get_value_range (TREE_OPERAND (base
, 0));
1119 if (range_is_nonnull (vr
))
1127 /* Returns true if EXPR is a valid value (as expected by compare_values) --
1128 a gimple invariant, or SSA_NAME +- CST. */
1131 valid_value_p (tree expr
)
1133 if (TREE_CODE (expr
) == SSA_NAME
)
1136 if (TREE_CODE (expr
) == PLUS_EXPR
1137 || TREE_CODE (expr
) == MINUS_EXPR
)
1138 return (TREE_CODE (TREE_OPERAND (expr
, 0)) == SSA_NAME
1139 && TREE_CODE (TREE_OPERAND (expr
, 1)) == INTEGER_CST
);
1141 return is_gimple_min_invariant (expr
);
1147 -2 if those are incomparable. */
1149 operand_less_p (tree val
, tree val2
)
1151 /* LT is folded faster than GE and others. Inline the common case. */
1152 if (TREE_CODE (val
) == INTEGER_CST
&& TREE_CODE (val2
) == INTEGER_CST
)
1154 if (TYPE_UNSIGNED (TREE_TYPE (val
)))
1155 return INT_CST_LT_UNSIGNED (val
, val2
);
1158 if (INT_CST_LT (val
, val2
))
1166 fold_defer_overflow_warnings ();
1168 tcmp
= fold_binary_to_constant (LT_EXPR
, boolean_type_node
, val
, val2
);
1170 fold_undefer_and_ignore_overflow_warnings ();
1173 || TREE_CODE (tcmp
) != INTEGER_CST
)
1176 if (!integer_zerop (tcmp
))
1180 /* val >= val2, not considering overflow infinity. */
1181 if (is_negative_overflow_infinity (val
))
1182 return is_negative_overflow_infinity (val2
) ? 0 : 1;
1183 else if (is_positive_overflow_infinity (val2
))
1184 return is_positive_overflow_infinity (val
) ? 0 : 1;
1189 /* Compare two values VAL1 and VAL2. Return
1191 -2 if VAL1 and VAL2 cannot be compared at compile-time,
1194 +1 if VAL1 > VAL2, and
1197 This is similar to tree_int_cst_compare but supports pointer values
1198 and values that cannot be compared at compile time.
1200 If STRICT_OVERFLOW_P is not NULL, then set *STRICT_OVERFLOW_P to
1201 true if the return value is only valid if we assume that signed
1202 overflow is undefined. */
1205 compare_values_warnv (tree val1
, tree val2
, bool *strict_overflow_p
)
1210 /* Below we rely on the fact that VAL1 and VAL2 are both pointers or
1212 gcc_assert (POINTER_TYPE_P (TREE_TYPE (val1
))
1213 == POINTER_TYPE_P (TREE_TYPE (val2
)));
1214 /* Convert the two values into the same type. This is needed because
1215 sizetype causes sign extension even for unsigned types. */
1216 val2
= fold_convert (TREE_TYPE (val1
), val2
);
1217 STRIP_USELESS_TYPE_CONVERSION (val2
);
1219 if ((TREE_CODE (val1
) == SSA_NAME
1220 || TREE_CODE (val1
) == PLUS_EXPR
1221 || TREE_CODE (val1
) == MINUS_EXPR
)
1222 && (TREE_CODE (val2
) == SSA_NAME
1223 || TREE_CODE (val2
) == PLUS_EXPR
1224 || TREE_CODE (val2
) == MINUS_EXPR
))
1226 tree n1
, c1
, n2
, c2
;
1227 enum tree_code code1
, code2
;
1229 /* If VAL1 and VAL2 are of the form 'NAME [+-] CST' or 'NAME',
1230 return -1 or +1 accordingly. If VAL1 and VAL2 don't use the
1231 same name, return -2. */
1232 if (TREE_CODE (val1
) == SSA_NAME
)
1240 code1
= TREE_CODE (val1
);
1241 n1
= TREE_OPERAND (val1
, 0);
1242 c1
= TREE_OPERAND (val1
, 1);
1243 if (tree_int_cst_sgn (c1
) == -1)
1245 if (is_negative_overflow_infinity (c1
))
1247 c1
= fold_unary_to_constant (NEGATE_EXPR
, TREE_TYPE (c1
), c1
);
1250 code1
= code1
== MINUS_EXPR
? PLUS_EXPR
: MINUS_EXPR
;
1254 if (TREE_CODE (val2
) == SSA_NAME
)
1262 code2
= TREE_CODE (val2
);
1263 n2
= TREE_OPERAND (val2
, 0);
1264 c2
= TREE_OPERAND (val2
, 1);
1265 if (tree_int_cst_sgn (c2
) == -1)
1267 if (is_negative_overflow_infinity (c2
))
1269 c2
= fold_unary_to_constant (NEGATE_EXPR
, TREE_TYPE (c2
), c2
);
1272 code2
= code2
== MINUS_EXPR
? PLUS_EXPR
: MINUS_EXPR
;
1276 /* Both values must use the same name. */
1280 if (code1
== SSA_NAME
1281 && code2
== SSA_NAME
)
1285 /* If overflow is defined we cannot simplify more. */
1286 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (val1
)))
1289 if (strict_overflow_p
!= NULL
1290 && (code1
== SSA_NAME
|| !TREE_NO_WARNING (val1
))
1291 && (code2
== SSA_NAME
|| !TREE_NO_WARNING (val2
)))
1292 *strict_overflow_p
= true;
1294 if (code1
== SSA_NAME
)
1296 if (code2
== PLUS_EXPR
)
1297 /* NAME < NAME + CST */
1299 else if (code2
== MINUS_EXPR
)
1300 /* NAME > NAME - CST */
1303 else if (code1
== PLUS_EXPR
)
1305 if (code2
== SSA_NAME
)
1306 /* NAME + CST > NAME */
1308 else if (code2
== PLUS_EXPR
)
1309 /* NAME + CST1 > NAME + CST2, if CST1 > CST2 */
1310 return compare_values_warnv (c1
, c2
, strict_overflow_p
);
1311 else if (code2
== MINUS_EXPR
)
1312 /* NAME + CST1 > NAME - CST2 */
1315 else if (code1
== MINUS_EXPR
)
1317 if (code2
== SSA_NAME
)
1318 /* NAME - CST < NAME */
1320 else if (code2
== PLUS_EXPR
)
1321 /* NAME - CST1 < NAME + CST2 */
1323 else if (code2
== MINUS_EXPR
)
1324 /* NAME - CST1 > NAME - CST2, if CST1 < CST2. Notice that
1325 C1 and C2 are swapped in the call to compare_values. */
1326 return compare_values_warnv (c2
, c1
, strict_overflow_p
);
1332 /* We cannot compare non-constants. */
1333 if (!is_gimple_min_invariant (val1
) || !is_gimple_min_invariant (val2
))
1336 if (!POINTER_TYPE_P (TREE_TYPE (val1
)))
1338 /* We cannot compare overflowed values, except for overflow
1340 if (TREE_OVERFLOW (val1
) || TREE_OVERFLOW (val2
))
1342 if (strict_overflow_p
!= NULL
)
1343 *strict_overflow_p
= true;
1344 if (is_negative_overflow_infinity (val1
))
1345 return is_negative_overflow_infinity (val2
) ? 0 : -1;
1346 else if (is_negative_overflow_infinity (val2
))
1348 else if (is_positive_overflow_infinity (val1
))
1349 return is_positive_overflow_infinity (val2
) ? 0 : 1;
1350 else if (is_positive_overflow_infinity (val2
))
1355 return tree_int_cst_compare (val1
, val2
);
1361 /* First see if VAL1 and VAL2 are not the same. */
1362 if (val1
== val2
|| operand_equal_p (val1
, val2
, 0))
1365 /* If VAL1 is a lower address than VAL2, return -1. */
1366 if (operand_less_p (val1
, val2
) == 1)
1369 /* If VAL1 is a higher address than VAL2, return +1. */
1370 if (operand_less_p (val2
, val1
) == 1)
1373 /* If VAL1 is different than VAL2, return +2.
1374 For integer constants we either have already returned -1 or 1
1375 or they are equivalent. We still might succeed in proving
1376 something about non-trivial operands. */
1377 if (TREE_CODE (val1
) != INTEGER_CST
1378 || TREE_CODE (val2
) != INTEGER_CST
)
1380 t
= fold_binary_to_constant (NE_EXPR
, boolean_type_node
, val1
, val2
);
1381 if (t
&& integer_onep (t
))
1389 /* Compare values like compare_values_warnv, but treat comparisons of
1390 nonconstants which rely on undefined overflow as incomparable. */
1393 compare_values (tree val1
, tree val2
)
1399 ret
= compare_values_warnv (val1
, val2
, &sop
);
1401 && (!is_gimple_min_invariant (val1
) || !is_gimple_min_invariant (val2
)))
1407 /* Return 1 if VAL is inside value range MIN <= VAL <= MAX,
1408 0 if VAL is not inside [MIN, MAX],
1409 -2 if we cannot tell either way.
1411 Benchmark compile/20001226-1.c compilation time after changing this
1415 value_inside_range (tree val
, tree min
, tree max
)
1419 cmp1
= operand_less_p (val
, min
);
1425 cmp2
= operand_less_p (max
, val
);
1433 /* Return true if value ranges VR0 and VR1 have a non-empty
1436 Benchmark compile/20001226-1.c compilation time after changing this
1441 value_ranges_intersect_p (value_range_t
*vr0
, value_range_t
*vr1
)
1443 /* The value ranges do not intersect if the maximum of the first range is
1444 less than the minimum of the second range or vice versa.
1445 When those relations are unknown, we can't do any better. */
1446 if (operand_less_p (vr0
->max
, vr1
->min
) != 0)
1448 if (operand_less_p (vr1
->max
, vr0
->min
) != 0)
1454 /* Return 1 if [MIN, MAX] includes the value zero, 0 if it does not
1455 include the value zero, -2 if we cannot tell. */
1458 range_includes_zero_p (tree min
, tree max
)
1460 tree zero
= build_int_cst (TREE_TYPE (min
), 0);
1461 return value_inside_range (zero
, min
, max
);
1464 /* Return true if *VR is know to only contain nonnegative values. */
1467 value_range_nonnegative_p (value_range_t
*vr
)
1469 /* Testing for VR_ANTI_RANGE is not useful here as any anti-range
1470 which would return a useful value should be encoded as a
1472 if (vr
->type
== VR_RANGE
)
1474 int result
= compare_values (vr
->min
, integer_zero_node
);
1475 return (result
== 0 || result
== 1);
1481 /* If *VR has a value rante that is a single constant value return that,
1482 otherwise return NULL_TREE. */
1485 value_range_constant_singleton (value_range_t
*vr
)
1487 if (vr
->type
== VR_RANGE
1488 && operand_equal_p (vr
->min
, vr
->max
, 0)
1489 && is_gimple_min_invariant (vr
->min
))
1495 /* If OP has a value range with a single constant value return that,
1496 otherwise return NULL_TREE. This returns OP itself if OP is a
1500 op_with_constant_singleton_value_range (tree op
)
1502 if (is_gimple_min_invariant (op
))
1505 if (TREE_CODE (op
) != SSA_NAME
)
1508 return value_range_constant_singleton (get_value_range (op
));
1511 /* Return true if op is in a boolean [0, 1] value-range. */
1514 op_with_boolean_value_range_p (tree op
)
1518 if (TYPE_PRECISION (TREE_TYPE (op
)) == 1)
1521 if (integer_zerop (op
)
1522 || integer_onep (op
))
1525 if (TREE_CODE (op
) != SSA_NAME
)
1528 vr
= get_value_range (op
);
1529 return (vr
->type
== VR_RANGE
1530 && integer_zerop (vr
->min
)
1531 && integer_onep (vr
->max
));
1534 /* Extract value range information from an ASSERT_EXPR EXPR and store
1538 extract_range_from_assert (value_range_t
*vr_p
, tree expr
)
1540 tree var
, cond
, limit
, min
, max
, type
;
1541 value_range_t
*limit_vr
;
1542 enum tree_code cond_code
;
1544 var
= ASSERT_EXPR_VAR (expr
);
1545 cond
= ASSERT_EXPR_COND (expr
);
1547 gcc_assert (COMPARISON_CLASS_P (cond
));
1549 /* Find VAR in the ASSERT_EXPR conditional. */
1550 if (var
== TREE_OPERAND (cond
, 0)
1551 || TREE_CODE (TREE_OPERAND (cond
, 0)) == PLUS_EXPR
1552 || TREE_CODE (TREE_OPERAND (cond
, 0)) == NOP_EXPR
)
1554 /* If the predicate is of the form VAR COMP LIMIT, then we just
1555 take LIMIT from the RHS and use the same comparison code. */
1556 cond_code
= TREE_CODE (cond
);
1557 limit
= TREE_OPERAND (cond
, 1);
1558 cond
= TREE_OPERAND (cond
, 0);
1562 /* If the predicate is of the form LIMIT COMP VAR, then we need
1563 to flip around the comparison code to create the proper range
1565 cond_code
= swap_tree_comparison (TREE_CODE (cond
));
1566 limit
= TREE_OPERAND (cond
, 0);
1567 cond
= TREE_OPERAND (cond
, 1);
1570 limit
= avoid_overflow_infinity (limit
);
1572 type
= TREE_TYPE (var
);
1573 gcc_assert (limit
!= var
);
1575 /* For pointer arithmetic, we only keep track of pointer equality
1577 if (POINTER_TYPE_P (type
) && cond_code
!= NE_EXPR
&& cond_code
!= EQ_EXPR
)
1579 set_value_range_to_varying (vr_p
);
1583 /* If LIMIT is another SSA name and LIMIT has a range of its own,
1584 try to use LIMIT's range to avoid creating symbolic ranges
1586 limit_vr
= (TREE_CODE (limit
) == SSA_NAME
) ? get_value_range (limit
) : NULL
;
1588 /* LIMIT's range is only interesting if it has any useful information. */
1590 && (limit_vr
->type
== VR_UNDEFINED
1591 || limit_vr
->type
== VR_VARYING
1592 || symbolic_range_p (limit_vr
)))
1595 /* Initially, the new range has the same set of equivalences of
1596 VAR's range. This will be revised before returning the final
1597 value. Since assertions may be chained via mutually exclusive
1598 predicates, we will need to trim the set of equivalences before
1600 gcc_assert (vr_p
->equiv
== NULL
);
1601 add_equivalence (&vr_p
->equiv
, var
);
1603 /* Extract a new range based on the asserted comparison for VAR and
1604 LIMIT's value range. Notice that if LIMIT has an anti-range, we
1605 will only use it for equality comparisons (EQ_EXPR). For any
1606 other kind of assertion, we cannot derive a range from LIMIT's
1607 anti-range that can be used to describe the new range. For
1608 instance, ASSERT_EXPR <x_2, x_2 <= b_4>. If b_4 is ~[2, 10],
1609 then b_4 takes on the ranges [-INF, 1] and [11, +INF]. There is
1610 no single range for x_2 that could describe LE_EXPR, so we might
1611 as well build the range [b_4, +INF] for it.
1612 One special case we handle is extracting a range from a
1613 range test encoded as (unsigned)var + CST <= limit. */
1614 if (TREE_CODE (cond
) == NOP_EXPR
1615 || TREE_CODE (cond
) == PLUS_EXPR
)
1617 if (TREE_CODE (cond
) == PLUS_EXPR
)
1619 min
= fold_build1 (NEGATE_EXPR
, TREE_TYPE (TREE_OPERAND (cond
, 1)),
1620 TREE_OPERAND (cond
, 1));
1621 max
= int_const_binop (PLUS_EXPR
, limit
, min
);
1622 cond
= TREE_OPERAND (cond
, 0);
1626 min
= build_int_cst (TREE_TYPE (var
), 0);
1630 /* Make sure to not set TREE_OVERFLOW on the final type
1631 conversion. We are willingly interpreting large positive
1632 unsigned values as negative singed values here. */
1633 min
= force_fit_type_double (TREE_TYPE (var
), tree_to_double_int (min
),
1635 max
= force_fit_type_double (TREE_TYPE (var
), tree_to_double_int (max
),
1638 /* We can transform a max, min range to an anti-range or
1639 vice-versa. Use set_and_canonicalize_value_range which does
1641 if (cond_code
== LE_EXPR
)
1642 set_and_canonicalize_value_range (vr_p
, VR_RANGE
,
1643 min
, max
, vr_p
->equiv
);
1644 else if (cond_code
== GT_EXPR
)
1645 set_and_canonicalize_value_range (vr_p
, VR_ANTI_RANGE
,
1646 min
, max
, vr_p
->equiv
);
1650 else if (cond_code
== EQ_EXPR
)
1652 enum value_range_type range_type
;
1656 range_type
= limit_vr
->type
;
1657 min
= limit_vr
->min
;
1658 max
= limit_vr
->max
;
1662 range_type
= VR_RANGE
;
1667 set_value_range (vr_p
, range_type
, min
, max
, vr_p
->equiv
);
1669 /* When asserting the equality VAR == LIMIT and LIMIT is another
1670 SSA name, the new range will also inherit the equivalence set
1672 if (TREE_CODE (limit
) == SSA_NAME
)
1673 add_equivalence (&vr_p
->equiv
, limit
);
1675 else if (cond_code
== NE_EXPR
)
1677 /* As described above, when LIMIT's range is an anti-range and
1678 this assertion is an inequality (NE_EXPR), then we cannot
1679 derive anything from the anti-range. For instance, if
1680 LIMIT's range was ~[0, 0], the assertion 'VAR != LIMIT' does
1681 not imply that VAR's range is [0, 0]. So, in the case of
1682 anti-ranges, we just assert the inequality using LIMIT and
1685 If LIMIT_VR is a range, we can only use it to build a new
1686 anti-range if LIMIT_VR is a single-valued range. For
1687 instance, if LIMIT_VR is [0, 1], the predicate
1688 VAR != [0, 1] does not mean that VAR's range is ~[0, 1].
1689 Rather, it means that for value 0 VAR should be ~[0, 0]
1690 and for value 1, VAR should be ~[1, 1]. We cannot
1691 represent these ranges.
1693 The only situation in which we can build a valid
1694 anti-range is when LIMIT_VR is a single-valued range
1695 (i.e., LIMIT_VR->MIN == LIMIT_VR->MAX). In that case,
1696 build the anti-range ~[LIMIT_VR->MIN, LIMIT_VR->MAX]. */
1698 && limit_vr
->type
== VR_RANGE
1699 && compare_values (limit_vr
->min
, limit_vr
->max
) == 0)
1701 min
= limit_vr
->min
;
1702 max
= limit_vr
->max
;
1706 /* In any other case, we cannot use LIMIT's range to build a
1707 valid anti-range. */
1711 /* If MIN and MAX cover the whole range for their type, then
1712 just use the original LIMIT. */
1713 if (INTEGRAL_TYPE_P (type
)
1714 && vrp_val_is_min (min
)
1715 && vrp_val_is_max (max
))
1718 set_and_canonicalize_value_range (vr_p
, VR_ANTI_RANGE
,
1719 min
, max
, vr_p
->equiv
);
1721 else if (cond_code
== LE_EXPR
|| cond_code
== LT_EXPR
)
1723 min
= TYPE_MIN_VALUE (type
);
1725 if (limit_vr
== NULL
|| limit_vr
->type
== VR_ANTI_RANGE
)
1729 /* If LIMIT_VR is of the form [N1, N2], we need to build the
1730 range [MIN, N2] for LE_EXPR and [MIN, N2 - 1] for
1732 max
= limit_vr
->max
;
1735 /* If the maximum value forces us to be out of bounds, simply punt.
1736 It would be pointless to try and do anything more since this
1737 all should be optimized away above us. */
1738 if ((cond_code
== LT_EXPR
1739 && compare_values (max
, min
) == 0)
1740 || is_overflow_infinity (max
))
1741 set_value_range_to_varying (vr_p
);
1744 /* For LT_EXPR, we create the range [MIN, MAX - 1]. */
1745 if (cond_code
== LT_EXPR
)
1747 if (TYPE_PRECISION (TREE_TYPE (max
)) == 1
1748 && !TYPE_UNSIGNED (TREE_TYPE (max
)))
1749 max
= fold_build2 (PLUS_EXPR
, TREE_TYPE (max
), max
,
1750 build_int_cst (TREE_TYPE (max
), -1));
1752 max
= fold_build2 (MINUS_EXPR
, TREE_TYPE (max
), max
,
1753 build_int_cst (TREE_TYPE (max
), 1));
1755 TREE_NO_WARNING (max
) = 1;
1758 set_value_range (vr_p
, VR_RANGE
, min
, max
, vr_p
->equiv
);
1761 else if (cond_code
== GE_EXPR
|| cond_code
== GT_EXPR
)
1763 max
= TYPE_MAX_VALUE (type
);
1765 if (limit_vr
== NULL
|| limit_vr
->type
== VR_ANTI_RANGE
)
1769 /* If LIMIT_VR is of the form [N1, N2], we need to build the
1770 range [N1, MAX] for GE_EXPR and [N1 + 1, MAX] for
1772 min
= limit_vr
->min
;
1775 /* If the minimum value forces us to be out of bounds, simply punt.
1776 It would be pointless to try and do anything more since this
1777 all should be optimized away above us. */
1778 if ((cond_code
== GT_EXPR
1779 && compare_values (min
, max
) == 0)
1780 || is_overflow_infinity (min
))
1781 set_value_range_to_varying (vr_p
);
1784 /* For GT_EXPR, we create the range [MIN + 1, MAX]. */
1785 if (cond_code
== GT_EXPR
)
1787 if (TYPE_PRECISION (TREE_TYPE (min
)) == 1
1788 && !TYPE_UNSIGNED (TREE_TYPE (min
)))
1789 min
= fold_build2 (MINUS_EXPR
, TREE_TYPE (min
), min
,
1790 build_int_cst (TREE_TYPE (min
), -1));
1792 min
= fold_build2 (PLUS_EXPR
, TREE_TYPE (min
), min
,
1793 build_int_cst (TREE_TYPE (min
), 1));
1795 TREE_NO_WARNING (min
) = 1;
1798 set_value_range (vr_p
, VR_RANGE
, min
, max
, vr_p
->equiv
);
1804 /* Finally intersect the new range with what we already know about var. */
1805 vrp_intersect_ranges (vr_p
, get_value_range (var
));
1809 /* Extract range information from SSA name VAR and store it in VR. If
1810 VAR has an interesting range, use it. Otherwise, create the
1811 range [VAR, VAR] and return it. This is useful in situations where
1812 we may have conditionals testing values of VARYING names. For
1819 Even if y_5 is deemed VARYING, we can determine that x_3 > y_5 is
1823 extract_range_from_ssa_name (value_range_t
*vr
, tree var
)
1825 value_range_t
*var_vr
= get_value_range (var
);
1827 if (var_vr
->type
!= VR_UNDEFINED
&& var_vr
->type
!= VR_VARYING
)
1828 copy_value_range (vr
, var_vr
);
1830 set_value_range (vr
, VR_RANGE
, var
, var
, NULL
);
1832 add_equivalence (&vr
->equiv
, var
);
1836 /* Wrapper around int_const_binop. If the operation overflows and we
1837 are not using wrapping arithmetic, then adjust the result to be
1838 -INF or +INF depending on CODE, VAL1 and VAL2. This can return
1839 NULL_TREE if we need to use an overflow infinity representation but
1840 the type does not support it. */
1843 vrp_int_const_binop (enum tree_code code
, tree val1
, tree val2
)
1847 res
= int_const_binop (code
, val1
, val2
);
1849 /* If we are using unsigned arithmetic, operate symbolically
1850 on -INF and +INF as int_const_binop only handles signed overflow. */
1851 if (TYPE_UNSIGNED (TREE_TYPE (val1
)))
1853 int checkz
= compare_values (res
, val1
);
1854 bool overflow
= false;
1856 /* Ensure that res = val1 [+*] val2 >= val1
1857 or that res = val1 - val2 <= val1. */
1858 if ((code
== PLUS_EXPR
1859 && !(checkz
== 1 || checkz
== 0))
1860 || (code
== MINUS_EXPR
1861 && !(checkz
== 0 || checkz
== -1)))
1865 /* Checking for multiplication overflow is done by dividing the
1866 output of the multiplication by the first input of the
1867 multiplication. If the result of that division operation is
1868 not equal to the second input of the multiplication, then the
1869 multiplication overflowed. */
1870 else if (code
== MULT_EXPR
&& !integer_zerop (val1
))
1872 tree tmp
= int_const_binop (TRUNC_DIV_EXPR
,
1875 int check
= compare_values (tmp
, val2
);
1883 res
= copy_node (res
);
1884 TREE_OVERFLOW (res
) = 1;
1888 else if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (val1
)))
1889 /* If the singed operation wraps then int_const_binop has done
1890 everything we want. */
1892 else if ((TREE_OVERFLOW (res
)
1893 && !TREE_OVERFLOW (val1
)
1894 && !TREE_OVERFLOW (val2
))
1895 || is_overflow_infinity (val1
)
1896 || is_overflow_infinity (val2
))
1898 /* If the operation overflowed but neither VAL1 nor VAL2 are
1899 overflown, return -INF or +INF depending on the operation
1900 and the combination of signs of the operands. */
1901 int sgn1
= tree_int_cst_sgn (val1
);
1902 int sgn2
= tree_int_cst_sgn (val2
);
1904 if (needs_overflow_infinity (TREE_TYPE (res
))
1905 && !supports_overflow_infinity (TREE_TYPE (res
)))
1908 /* We have to punt on adding infinities of different signs,
1909 since we can't tell what the sign of the result should be.
1910 Likewise for subtracting infinities of the same sign. */
1911 if (((code
== PLUS_EXPR
&& sgn1
!= sgn2
)
1912 || (code
== MINUS_EXPR
&& sgn1
== sgn2
))
1913 && is_overflow_infinity (val1
)
1914 && is_overflow_infinity (val2
))
1917 /* Don't try to handle division or shifting of infinities. */
1918 if ((code
== TRUNC_DIV_EXPR
1919 || code
== FLOOR_DIV_EXPR
1920 || code
== CEIL_DIV_EXPR
1921 || code
== EXACT_DIV_EXPR
1922 || code
== ROUND_DIV_EXPR
1923 || code
== RSHIFT_EXPR
)
1924 && (is_overflow_infinity (val1
)
1925 || is_overflow_infinity (val2
)))
1928 /* Notice that we only need to handle the restricted set of
1929 operations handled by extract_range_from_binary_expr.
1930 Among them, only multiplication, addition and subtraction
1931 can yield overflow without overflown operands because we
1932 are working with integral types only... except in the
1933 case VAL1 = -INF and VAL2 = -1 which overflows to +INF
1934 for division too. */
1936 /* For multiplication, the sign of the overflow is given
1937 by the comparison of the signs of the operands. */
1938 if ((code
== MULT_EXPR
&& sgn1
== sgn2
)
1939 /* For addition, the operands must be of the same sign
1940 to yield an overflow. Its sign is therefore that
1941 of one of the operands, for example the first. For
1942 infinite operands X + -INF is negative, not positive. */
1943 || (code
== PLUS_EXPR
1945 ? !is_negative_overflow_infinity (val2
)
1946 : is_positive_overflow_infinity (val2
)))
1947 /* For subtraction, non-infinite operands must be of
1948 different signs to yield an overflow. Its sign is
1949 therefore that of the first operand or the opposite of
1950 that of the second operand. A first operand of 0 counts
1951 as positive here, for the corner case 0 - (-INF), which
1952 overflows, but must yield +INF. For infinite operands 0
1953 - INF is negative, not positive. */
1954 || (code
== MINUS_EXPR
1956 ? !is_positive_overflow_infinity (val2
)
1957 : is_negative_overflow_infinity (val2
)))
1958 /* We only get in here with positive shift count, so the
1959 overflow direction is the same as the sign of val1.
1960 Actually rshift does not overflow at all, but we only
1961 handle the case of shifting overflowed -INF and +INF. */
1962 || (code
== RSHIFT_EXPR
1964 /* For division, the only case is -INF / -1 = +INF. */
1965 || code
== TRUNC_DIV_EXPR
1966 || code
== FLOOR_DIV_EXPR
1967 || code
== CEIL_DIV_EXPR
1968 || code
== EXACT_DIV_EXPR
1969 || code
== ROUND_DIV_EXPR
)
1970 return (needs_overflow_infinity (TREE_TYPE (res
))
1971 ? positive_overflow_infinity (TREE_TYPE (res
))
1972 : TYPE_MAX_VALUE (TREE_TYPE (res
)));
1974 return (needs_overflow_infinity (TREE_TYPE (res
))
1975 ? negative_overflow_infinity (TREE_TYPE (res
))
1976 : TYPE_MIN_VALUE (TREE_TYPE (res
)));
1983 /* For range VR compute two double_int bitmasks. In *MAY_BE_NONZERO
1984 bitmask if some bit is unset, it means for all numbers in the range
1985 the bit is 0, otherwise it might be 0 or 1. In *MUST_BE_NONZERO
1986 bitmask if some bit is set, it means for all numbers in the range
1987 the bit is 1, otherwise it might be 0 or 1. */
1990 zero_nonzero_bits_from_vr (value_range_t
*vr
,
1991 double_int
*may_be_nonzero
,
1992 double_int
*must_be_nonzero
)
1994 *may_be_nonzero
= double_int_minus_one
;
1995 *must_be_nonzero
= double_int_zero
;
1996 if (!range_int_cst_p (vr
)
1997 || is_overflow_infinity (vr
->min
)
1998 || is_overflow_infinity (vr
->max
))
2001 if (range_int_cst_singleton_p (vr
))
2003 *may_be_nonzero
= tree_to_double_int (vr
->min
);
2004 *must_be_nonzero
= *may_be_nonzero
;
2006 else if (tree_int_cst_sgn (vr
->min
) >= 0
2007 || tree_int_cst_sgn (vr
->max
) < 0)
2009 double_int dmin
= tree_to_double_int (vr
->min
);
2010 double_int dmax
= tree_to_double_int (vr
->max
);
2011 double_int xor_mask
= dmin
^ dmax
;
2012 *may_be_nonzero
= dmin
| dmax
;
2013 *must_be_nonzero
= dmin
& dmax
;
2014 if (xor_mask
.high
!= 0)
2016 unsigned HOST_WIDE_INT mask
2017 = ((unsigned HOST_WIDE_INT
) 1
2018 << floor_log2 (xor_mask
.high
)) - 1;
2019 may_be_nonzero
->low
= ALL_ONES
;
2020 may_be_nonzero
->high
|= mask
;
2021 must_be_nonzero
->low
= 0;
2022 must_be_nonzero
->high
&= ~mask
;
2024 else if (xor_mask
.low
!= 0)
2026 unsigned HOST_WIDE_INT mask
2027 = ((unsigned HOST_WIDE_INT
) 1
2028 << floor_log2 (xor_mask
.low
)) - 1;
2029 may_be_nonzero
->low
|= mask
;
2030 must_be_nonzero
->low
&= ~mask
;
2037 /* Create two value-ranges in *VR0 and *VR1 from the anti-range *AR
2038 so that *VR0 U *VR1 == *AR. Returns true if that is possible,
2039 false otherwise. If *AR can be represented with a single range
2040 *VR1 will be VR_UNDEFINED. */
2043 ranges_from_anti_range (value_range_t
*ar
,
2044 value_range_t
*vr0
, value_range_t
*vr1
)
2046 tree type
= TREE_TYPE (ar
->min
);
2048 vr0
->type
= VR_UNDEFINED
;
2049 vr1
->type
= VR_UNDEFINED
;
2051 if (ar
->type
!= VR_ANTI_RANGE
2052 || TREE_CODE (ar
->min
) != INTEGER_CST
2053 || TREE_CODE (ar
->max
) != INTEGER_CST
2054 || !vrp_val_min (type
)
2055 || !vrp_val_max (type
))
2058 if (!vrp_val_is_min (ar
->min
))
2060 vr0
->type
= VR_RANGE
;
2061 vr0
->min
= vrp_val_min (type
);
2063 = double_int_to_tree (type
,
2064 tree_to_double_int (ar
->min
) - double_int_one
);
2066 if (!vrp_val_is_max (ar
->max
))
2068 vr1
->type
= VR_RANGE
;
2070 = double_int_to_tree (type
,
2071 tree_to_double_int (ar
->max
) + double_int_one
);
2072 vr1
->max
= vrp_val_max (type
);
2074 if (vr0
->type
== VR_UNDEFINED
)
2077 vr1
->type
= VR_UNDEFINED
;
2080 return vr0
->type
!= VR_UNDEFINED
;
2083 /* Helper to extract a value-range *VR for a multiplicative operation
2087 extract_range_from_multiplicative_op_1 (value_range_t
*vr
,
2088 enum tree_code code
,
2089 value_range_t
*vr0
, value_range_t
*vr1
)
2091 enum value_range_type type
;
2098 /* Multiplications, divisions and shifts are a bit tricky to handle,
2099 depending on the mix of signs we have in the two ranges, we
2100 need to operate on different values to get the minimum and
2101 maximum values for the new range. One approach is to figure
2102 out all the variations of range combinations and do the
2105 However, this involves several calls to compare_values and it
2106 is pretty convoluted. It's simpler to do the 4 operations
2107 (MIN0 OP MIN1, MIN0 OP MAX1, MAX0 OP MIN1 and MAX0 OP MAX0 OP
2108 MAX1) and then figure the smallest and largest values to form
2110 gcc_assert (code
== MULT_EXPR
2111 || code
== TRUNC_DIV_EXPR
2112 || code
== FLOOR_DIV_EXPR
2113 || code
== CEIL_DIV_EXPR
2114 || code
== EXACT_DIV_EXPR
2115 || code
== ROUND_DIV_EXPR
2116 || code
== RSHIFT_EXPR
2117 || code
== LSHIFT_EXPR
);
2118 gcc_assert ((vr0
->type
== VR_RANGE
2119 || (code
== MULT_EXPR
&& vr0
->type
== VR_ANTI_RANGE
))
2120 && vr0
->type
== vr1
->type
);
2124 /* Compute the 4 cross operations. */
2126 val
[0] = vrp_int_const_binop (code
, vr0
->min
, vr1
->min
);
2127 if (val
[0] == NULL_TREE
)
2130 if (vr1
->max
== vr1
->min
)
2134 val
[1] = vrp_int_const_binop (code
, vr0
->min
, vr1
->max
);
2135 if (val
[1] == NULL_TREE
)
2139 if (vr0
->max
== vr0
->min
)
2143 val
[2] = vrp_int_const_binop (code
, vr0
->max
, vr1
->min
);
2144 if (val
[2] == NULL_TREE
)
2148 if (vr0
->min
== vr0
->max
|| vr1
->min
== vr1
->max
)
2152 val
[3] = vrp_int_const_binop (code
, vr0
->max
, vr1
->max
);
2153 if (val
[3] == NULL_TREE
)
2159 set_value_range_to_varying (vr
);
2163 /* Set MIN to the minimum of VAL[i] and MAX to the maximum
2167 for (i
= 1; i
< 4; i
++)
2169 if (!is_gimple_min_invariant (min
)
2170 || (TREE_OVERFLOW (min
) && !is_overflow_infinity (min
))
2171 || !is_gimple_min_invariant (max
)
2172 || (TREE_OVERFLOW (max
) && !is_overflow_infinity (max
)))
2177 if (!is_gimple_min_invariant (val
[i
])
2178 || (TREE_OVERFLOW (val
[i
])
2179 && !is_overflow_infinity (val
[i
])))
2181 /* If we found an overflowed value, set MIN and MAX
2182 to it so that we set the resulting range to
2188 if (compare_values (val
[i
], min
) == -1)
2191 if (compare_values (val
[i
], max
) == 1)
2196 /* If either MIN or MAX overflowed, then set the resulting range to
2197 VARYING. But we do accept an overflow infinity
2199 if (min
== NULL_TREE
2200 || !is_gimple_min_invariant (min
)
2201 || (TREE_OVERFLOW (min
) && !is_overflow_infinity (min
))
2203 || !is_gimple_min_invariant (max
)
2204 || (TREE_OVERFLOW (max
) && !is_overflow_infinity (max
)))
2206 set_value_range_to_varying (vr
);
2212 2) [-INF, +-INF(OVF)]
2213 3) [+-INF(OVF), +INF]
2214 4) [+-INF(OVF), +-INF(OVF)]
2215 We learn nothing when we have INF and INF(OVF) on both sides.
2216 Note that we do accept [-INF, -INF] and [+INF, +INF] without
2218 if ((vrp_val_is_min (min
) || is_overflow_infinity (min
))
2219 && (vrp_val_is_max (max
) || is_overflow_infinity (max
)))
2221 set_value_range_to_varying (vr
);
2225 cmp
= compare_values (min
, max
);
2226 if (cmp
== -2 || cmp
== 1)
2228 /* If the new range has its limits swapped around (MIN > MAX),
2229 then the operation caused one of them to wrap around, mark
2230 the new range VARYING. */
2231 set_value_range_to_varying (vr
);
2234 set_value_range (vr
, type
, min
, max
, NULL
);
2237 /* Some quadruple precision helpers. */
2239 quad_int_cmp (double_int l0
, double_int h0
,
2240 double_int l1
, double_int h1
, bool uns
)
2242 int c
= h0
.cmp (h1
, uns
);
2243 if (c
!= 0) return c
;
2244 return l0
.ucmp (l1
);
2248 quad_int_pair_sort (double_int
*l0
, double_int
*h0
,
2249 double_int
*l1
, double_int
*h1
, bool uns
)
2251 if (quad_int_cmp (*l0
, *h0
, *l1
, *h1
, uns
) > 0)
2254 tmp
= *l0
; *l0
= *l1
; *l1
= tmp
;
2255 tmp
= *h0
; *h0
= *h1
; *h1
= tmp
;
2259 /* Extract range information from a binary operation CODE based on
2260 the ranges of each of its operands, *VR0 and *VR1 with resulting
2261 type EXPR_TYPE. The resulting range is stored in *VR. */
2264 extract_range_from_binary_expr_1 (value_range_t
*vr
,
2265 enum tree_code code
, tree expr_type
,
2266 value_range_t
*vr0_
, value_range_t
*vr1_
)
2268 value_range_t vr0
= *vr0_
, vr1
= *vr1_
;
2269 value_range_t vrtem0
= VR_INITIALIZER
, vrtem1
= VR_INITIALIZER
;
2270 enum value_range_type type
;
2271 tree min
= NULL_TREE
, max
= NULL_TREE
;
2274 if (!INTEGRAL_TYPE_P (expr_type
)
2275 && !POINTER_TYPE_P (expr_type
))
2277 set_value_range_to_varying (vr
);
2281 /* Not all binary expressions can be applied to ranges in a
2282 meaningful way. Handle only arithmetic operations. */
2283 if (code
!= PLUS_EXPR
2284 && code
!= MINUS_EXPR
2285 && code
!= POINTER_PLUS_EXPR
2286 && code
!= MULT_EXPR
2287 && code
!= TRUNC_DIV_EXPR
2288 && code
!= FLOOR_DIV_EXPR
2289 && code
!= CEIL_DIV_EXPR
2290 && code
!= EXACT_DIV_EXPR
2291 && code
!= ROUND_DIV_EXPR
2292 && code
!= TRUNC_MOD_EXPR
2293 && code
!= RSHIFT_EXPR
2294 && code
!= LSHIFT_EXPR
2297 && code
!= BIT_AND_EXPR
2298 && code
!= BIT_IOR_EXPR
2299 && code
!= BIT_XOR_EXPR
)
2301 set_value_range_to_varying (vr
);
2305 /* If both ranges are UNDEFINED, so is the result. */
2306 if (vr0
.type
== VR_UNDEFINED
&& vr1
.type
== VR_UNDEFINED
)
2308 set_value_range_to_undefined (vr
);
2311 /* If one of the ranges is UNDEFINED drop it to VARYING for the following
2312 code. At some point we may want to special-case operations that
2313 have UNDEFINED result for all or some value-ranges of the not UNDEFINED
2315 else if (vr0
.type
== VR_UNDEFINED
)
2316 set_value_range_to_varying (&vr0
);
2317 else if (vr1
.type
== VR_UNDEFINED
)
2318 set_value_range_to_varying (&vr1
);
2320 /* Now canonicalize anti-ranges to ranges when they are not symbolic
2321 and express ~[] op X as ([]' op X) U ([]'' op X). */
2322 if (vr0
.type
== VR_ANTI_RANGE
2323 && ranges_from_anti_range (&vr0
, &vrtem0
, &vrtem1
))
2325 extract_range_from_binary_expr_1 (vr
, code
, expr_type
, &vrtem0
, vr1_
);
2326 if (vrtem1
.type
!= VR_UNDEFINED
)
2328 value_range_t vrres
= VR_INITIALIZER
;
2329 extract_range_from_binary_expr_1 (&vrres
, code
, expr_type
,
2331 vrp_meet (vr
, &vrres
);
2335 /* Likewise for X op ~[]. */
2336 if (vr1
.type
== VR_ANTI_RANGE
2337 && ranges_from_anti_range (&vr1
, &vrtem0
, &vrtem1
))
2339 extract_range_from_binary_expr_1 (vr
, code
, expr_type
, vr0_
, &vrtem0
);
2340 if (vrtem1
.type
!= VR_UNDEFINED
)
2342 value_range_t vrres
= VR_INITIALIZER
;
2343 extract_range_from_binary_expr_1 (&vrres
, code
, expr_type
,
2345 vrp_meet (vr
, &vrres
);
2350 /* The type of the resulting value range defaults to VR0.TYPE. */
2353 /* Refuse to operate on VARYING ranges, ranges of different kinds
2354 and symbolic ranges. As an exception, we allow BIT_AND_EXPR
2355 because we may be able to derive a useful range even if one of
2356 the operands is VR_VARYING or symbolic range. Similarly for
2357 divisions. TODO, we may be able to derive anti-ranges in
2359 if (code
!= BIT_AND_EXPR
2360 && code
!= BIT_IOR_EXPR
2361 && code
!= TRUNC_DIV_EXPR
2362 && code
!= FLOOR_DIV_EXPR
2363 && code
!= CEIL_DIV_EXPR
2364 && code
!= EXACT_DIV_EXPR
2365 && code
!= ROUND_DIV_EXPR
2366 && code
!= TRUNC_MOD_EXPR
2369 && (vr0
.type
== VR_VARYING
2370 || vr1
.type
== VR_VARYING
2371 || vr0
.type
!= vr1
.type
2372 || symbolic_range_p (&vr0
)
2373 || symbolic_range_p (&vr1
)))
2375 set_value_range_to_varying (vr
);
2379 /* Now evaluate the expression to determine the new range. */
2380 if (POINTER_TYPE_P (expr_type
))
2382 if (code
== MIN_EXPR
|| code
== MAX_EXPR
)
2384 /* For MIN/MAX expressions with pointers, we only care about
2385 nullness, if both are non null, then the result is nonnull.
2386 If both are null, then the result is null. Otherwise they
2388 if (range_is_nonnull (&vr0
) && range_is_nonnull (&vr1
))
2389 set_value_range_to_nonnull (vr
, expr_type
);
2390 else if (range_is_null (&vr0
) && range_is_null (&vr1
))
2391 set_value_range_to_null (vr
, expr_type
);
2393 set_value_range_to_varying (vr
);
2395 else if (code
== POINTER_PLUS_EXPR
)
2397 /* For pointer types, we are really only interested in asserting
2398 whether the expression evaluates to non-NULL. */
2399 if (range_is_nonnull (&vr0
) || range_is_nonnull (&vr1
))
2400 set_value_range_to_nonnull (vr
, expr_type
);
2401 else if (range_is_null (&vr0
) && range_is_null (&vr1
))
2402 set_value_range_to_null (vr
, expr_type
);
2404 set_value_range_to_varying (vr
);
2406 else if (code
== BIT_AND_EXPR
)
2408 /* For pointer types, we are really only interested in asserting
2409 whether the expression evaluates to non-NULL. */
2410 if (range_is_nonnull (&vr0
) && range_is_nonnull (&vr1
))
2411 set_value_range_to_nonnull (vr
, expr_type
);
2412 else if (range_is_null (&vr0
) || range_is_null (&vr1
))
2413 set_value_range_to_null (vr
, expr_type
);
2415 set_value_range_to_varying (vr
);
2418 set_value_range_to_varying (vr
);
2423 /* For integer ranges, apply the operation to each end of the
2424 range and see what we end up with. */
2425 if (code
== PLUS_EXPR
|| code
== MINUS_EXPR
)
2427 /* If we have a PLUS_EXPR with two VR_RANGE integer constant
2428 ranges compute the precise range for such case if possible. */
2429 if (range_int_cst_p (&vr0
)
2430 && range_int_cst_p (&vr1
)
2431 /* We need as many bits as the possibly unsigned inputs. */
2432 && TYPE_PRECISION (expr_type
) <= HOST_BITS_PER_DOUBLE_INT
)
2434 double_int min0
= tree_to_double_int (vr0
.min
);
2435 double_int max0
= tree_to_double_int (vr0
.max
);
2436 double_int min1
= tree_to_double_int (vr1
.min
);
2437 double_int max1
= tree_to_double_int (vr1
.max
);
2438 bool uns
= TYPE_UNSIGNED (expr_type
);
2440 = double_int::min_value (TYPE_PRECISION (expr_type
), uns
);
2442 = double_int::max_value (TYPE_PRECISION (expr_type
), uns
);
2443 double_int dmin
, dmax
;
2447 if (code
== PLUS_EXPR
)
2452 /* Check for overflow in double_int. */
2453 if (min1
.cmp (double_int_zero
, uns
) != dmin
.cmp (min0
, uns
))
2454 min_ovf
= min0
.cmp (dmin
, uns
);
2455 if (max1
.cmp (double_int_zero
, uns
) != dmax
.cmp (max0
, uns
))
2456 max_ovf
= max0
.cmp (dmax
, uns
);
2458 else /* if (code == MINUS_EXPR) */
2463 if (double_int_zero
.cmp (max1
, uns
) != dmin
.cmp (min0
, uns
))
2464 min_ovf
= min0
.cmp (max1
, uns
);
2465 if (double_int_zero
.cmp (min1
, uns
) != dmax
.cmp (max0
, uns
))
2466 max_ovf
= max0
.cmp (min1
, uns
);
2469 /* For non-wrapping arithmetic look at possibly smaller
2470 value-ranges of the type. */
2471 if (!TYPE_OVERFLOW_WRAPS (expr_type
))
2473 if (vrp_val_min (expr_type
))
2474 type_min
= tree_to_double_int (vrp_val_min (expr_type
));
2475 if (vrp_val_max (expr_type
))
2476 type_max
= tree_to_double_int (vrp_val_max (expr_type
));
2479 /* Check for type overflow. */
2482 if (dmin
.cmp (type_min
, uns
) == -1)
2484 else if (dmin
.cmp (type_max
, uns
) == 1)
2489 if (dmax
.cmp (type_min
, uns
) == -1)
2491 else if (dmax
.cmp (type_max
, uns
) == 1)
2495 if (TYPE_OVERFLOW_WRAPS (expr_type
))
2497 /* If overflow wraps, truncate the values and adjust the
2498 range kind and bounds appropriately. */
2500 = dmin
.ext (TYPE_PRECISION (expr_type
), uns
);
2502 = dmax
.ext (TYPE_PRECISION (expr_type
), uns
);
2503 if (min_ovf
== max_ovf
)
2505 /* No overflow or both overflow or underflow. The
2506 range kind stays VR_RANGE. */
2507 min
= double_int_to_tree (expr_type
, tmin
);
2508 max
= double_int_to_tree (expr_type
, tmax
);
2510 else if (min_ovf
== -1
2513 /* Underflow and overflow, drop to VR_VARYING. */
2514 set_value_range_to_varying (vr
);
2519 /* Min underflow or max overflow. The range kind
2520 changes to VR_ANTI_RANGE. */
2521 bool covers
= false;
2522 double_int tem
= tmin
;
2523 gcc_assert ((min_ovf
== -1 && max_ovf
== 0)
2524 || (max_ovf
== 1 && min_ovf
== 0));
2525 type
= VR_ANTI_RANGE
;
2526 tmin
= tmax
+ double_int_one
;
2527 if (tmin
.cmp (tmax
, uns
) < 0)
2529 tmax
= tem
+ double_int_minus_one
;
2530 if (tmax
.cmp (tem
, uns
) > 0)
2532 /* If the anti-range would cover nothing, drop to varying.
2533 Likewise if the anti-range bounds are outside of the
2535 if (covers
|| tmin
.cmp (tmax
, uns
) > 0)
2537 set_value_range_to_varying (vr
);
2540 min
= double_int_to_tree (expr_type
, tmin
);
2541 max
= double_int_to_tree (expr_type
, tmax
);
2546 /* If overflow does not wrap, saturate to the types min/max
2550 if (needs_overflow_infinity (expr_type
)
2551 && supports_overflow_infinity (expr_type
))
2552 min
= negative_overflow_infinity (expr_type
);
2554 min
= double_int_to_tree (expr_type
, type_min
);
2556 else if (min_ovf
== 1)
2558 if (needs_overflow_infinity (expr_type
)
2559 && supports_overflow_infinity (expr_type
))
2560 min
= positive_overflow_infinity (expr_type
);
2562 min
= double_int_to_tree (expr_type
, type_max
);
2565 min
= double_int_to_tree (expr_type
, dmin
);
2569 if (needs_overflow_infinity (expr_type
)
2570 && supports_overflow_infinity (expr_type
))
2571 max
= negative_overflow_infinity (expr_type
);
2573 max
= double_int_to_tree (expr_type
, type_min
);
2575 else if (max_ovf
== 1)
2577 if (needs_overflow_infinity (expr_type
)
2578 && supports_overflow_infinity (expr_type
))
2579 max
= positive_overflow_infinity (expr_type
);
2581 max
= double_int_to_tree (expr_type
, type_max
);
2584 max
= double_int_to_tree (expr_type
, dmax
);
2586 if (needs_overflow_infinity (expr_type
)
2587 && supports_overflow_infinity (expr_type
))
2589 if (is_negative_overflow_infinity (vr0
.min
)
2590 || (code
== PLUS_EXPR
2591 ? is_negative_overflow_infinity (vr1
.min
)
2592 : is_positive_overflow_infinity (vr1
.max
)))
2593 min
= negative_overflow_infinity (expr_type
);
2594 if (is_positive_overflow_infinity (vr0
.max
)
2595 || (code
== PLUS_EXPR
2596 ? is_positive_overflow_infinity (vr1
.max
)
2597 : is_negative_overflow_infinity (vr1
.min
)))
2598 max
= positive_overflow_infinity (expr_type
);
2603 /* For other cases, for example if we have a PLUS_EXPR with two
2604 VR_ANTI_RANGEs, drop to VR_VARYING. It would take more effort
2605 to compute a precise range for such a case.
2606 ??? General even mixed range kind operations can be expressed
2607 by for example transforming ~[3, 5] + [1, 2] to range-only
2608 operations and a union primitive:
2609 [-INF, 2] + [1, 2] U [5, +INF] + [1, 2]
2610 [-INF+1, 4] U [6, +INF(OVF)]
2611 though usually the union is not exactly representable with
2612 a single range or anti-range as the above is
2613 [-INF+1, +INF(OVF)] intersected with ~[5, 5]
2614 but one could use a scheme similar to equivalences for this. */
2615 set_value_range_to_varying (vr
);
2619 else if (code
== MIN_EXPR
2620 || code
== MAX_EXPR
)
2622 if (vr0
.type
== VR_RANGE
2623 && !symbolic_range_p (&vr0
))
2626 if (vr1
.type
== VR_RANGE
2627 && !symbolic_range_p (&vr1
))
2629 /* For operations that make the resulting range directly
2630 proportional to the original ranges, apply the operation to
2631 the same end of each range. */
2632 min
= vrp_int_const_binop (code
, vr0
.min
, vr1
.min
);
2633 max
= vrp_int_const_binop (code
, vr0
.max
, vr1
.max
);
2635 else if (code
== MIN_EXPR
)
2637 min
= vrp_val_min (expr_type
);
2640 else if (code
== MAX_EXPR
)
2643 max
= vrp_val_max (expr_type
);
2646 else if (vr1
.type
== VR_RANGE
2647 && !symbolic_range_p (&vr1
))
2650 if (code
== MIN_EXPR
)
2652 min
= vrp_val_min (expr_type
);
2655 else if (code
== MAX_EXPR
)
2658 max
= vrp_val_max (expr_type
);
2663 set_value_range_to_varying (vr
);
2667 else if (code
== MULT_EXPR
)
2669 /* Fancy code so that with unsigned, [-3,-1]*[-3,-1] does not
2671 if (range_int_cst_p (&vr0
)
2672 && range_int_cst_p (&vr1
)
2673 && TYPE_OVERFLOW_WRAPS (expr_type
))
2675 double_int min0
, max0
, min1
, max1
, sizem1
, size
;
2676 double_int prod0l
, prod0h
, prod1l
, prod1h
,
2677 prod2l
, prod2h
, prod3l
, prod3h
;
2678 bool uns0
, uns1
, uns
;
2680 sizem1
= double_int::max_value (TYPE_PRECISION (expr_type
), true);
2681 size
= sizem1
+ double_int_one
;
2683 min0
= tree_to_double_int (vr0
.min
);
2684 max0
= tree_to_double_int (vr0
.max
);
2685 min1
= tree_to_double_int (vr1
.min
);
2686 max1
= tree_to_double_int (vr1
.max
);
2688 uns0
= TYPE_UNSIGNED (expr_type
);
2691 /* Canonicalize the intervals. */
2692 if (TYPE_UNSIGNED (expr_type
))
2694 double_int min2
= size
- min0
;
2695 if (!min2
.is_zero () && min2
.cmp (max0
, true) < 0)
2703 if (!min2
.is_zero () && min2
.cmp (max1
, true) < 0)
2713 prod0l
= min0
.wide_mul_with_sign (min1
, true, &prod0h
, &overflow
);
2714 if (!uns0
&& min0
.is_negative ())
2716 if (!uns1
&& min1
.is_negative ())
2719 prod1l
= min0
.wide_mul_with_sign (max1
, true, &prod1h
, &overflow
);
2720 if (!uns0
&& min0
.is_negative ())
2722 if (!uns1
&& max1
.is_negative ())
2725 prod2l
= max0
.wide_mul_with_sign (min1
, true, &prod2h
, &overflow
);
2726 if (!uns0
&& max0
.is_negative ())
2728 if (!uns1
&& min1
.is_negative ())
2731 prod3l
= max0
.wide_mul_with_sign (max1
, true, &prod3h
, &overflow
);
2732 if (!uns0
&& max0
.is_negative ())
2734 if (!uns1
&& max1
.is_negative ())
2737 /* Sort the 4 products. */
2738 quad_int_pair_sort (&prod0l
, &prod0h
, &prod3l
, &prod3h
, uns
);
2739 quad_int_pair_sort (&prod1l
, &prod1h
, &prod2l
, &prod2h
, uns
);
2740 quad_int_pair_sort (&prod0l
, &prod0h
, &prod1l
, &prod1h
, uns
);
2741 quad_int_pair_sort (&prod2l
, &prod2h
, &prod3l
, &prod3h
, uns
);
2744 if (prod0l
.is_zero ())
2746 prod1l
= double_int_zero
;
2754 prod2l
= prod3l
+ prod1l
;
2755 prod2h
= prod3h
+ prod1h
;
2756 if (prod2l
.ult (prod3l
))
2757 prod2h
+= double_int_one
; /* carry */
2759 if (!prod2h
.is_zero ()
2760 || prod2l
.cmp (sizem1
, true) >= 0)
2762 /* the range covers all values. */
2763 set_value_range_to_varying (vr
);
2767 /* The following should handle the wrapping and selecting
2768 VR_ANTI_RANGE for us. */
2769 min
= double_int_to_tree (expr_type
, prod0l
);
2770 max
= double_int_to_tree (expr_type
, prod3l
);
2771 set_and_canonicalize_value_range (vr
, VR_RANGE
, min
, max
, NULL
);
2775 /* If we have an unsigned MULT_EXPR with two VR_ANTI_RANGEs,
2776 drop to VR_VARYING. It would take more effort to compute a
2777 precise range for such a case. For example, if we have
2778 op0 == 65536 and op1 == 65536 with their ranges both being
2779 ~[0,0] on a 32-bit machine, we would have op0 * op1 == 0, so
2780 we cannot claim that the product is in ~[0,0]. Note that we
2781 are guaranteed to have vr0.type == vr1.type at this
2783 if (vr0
.type
== VR_ANTI_RANGE
2784 && !TYPE_OVERFLOW_UNDEFINED (expr_type
))
2786 set_value_range_to_varying (vr
);
2790 extract_range_from_multiplicative_op_1 (vr
, code
, &vr0
, &vr1
);
2793 else if (code
== RSHIFT_EXPR
2794 || code
== LSHIFT_EXPR
)
2796 /* If we have a RSHIFT_EXPR with any shift values outside [0..prec-1],
2797 then drop to VR_VARYING. Outside of this range we get undefined
2798 behavior from the shift operation. We cannot even trust
2799 SHIFT_COUNT_TRUNCATED at this stage, because that applies to rtl
2800 shifts, and the operation at the tree level may be widened. */
2801 if (range_int_cst_p (&vr1
)
2802 && compare_tree_int (vr1
.min
, 0) >= 0
2803 && compare_tree_int (vr1
.max
, TYPE_PRECISION (expr_type
)) == -1)
2805 if (code
== RSHIFT_EXPR
)
2807 extract_range_from_multiplicative_op_1 (vr
, code
, &vr0
, &vr1
);
2810 /* We can map lshifts by constants to MULT_EXPR handling. */
2811 else if (code
== LSHIFT_EXPR
2812 && range_int_cst_singleton_p (&vr1
))
2814 bool saved_flag_wrapv
;
2815 value_range_t vr1p
= VR_INITIALIZER
;
2816 vr1p
.type
= VR_RANGE
;
2818 = double_int_to_tree (expr_type
,
2820 .llshift (TREE_INT_CST_LOW (vr1
.min
),
2821 TYPE_PRECISION (expr_type
)));
2822 vr1p
.max
= vr1p
.min
;
2823 /* We have to use a wrapping multiply though as signed overflow
2824 on lshifts is implementation defined in C89. */
2825 saved_flag_wrapv
= flag_wrapv
;
2827 extract_range_from_binary_expr_1 (vr
, MULT_EXPR
, expr_type
,
2829 flag_wrapv
= saved_flag_wrapv
;
2832 else if (code
== LSHIFT_EXPR
2833 && range_int_cst_p (&vr0
))
2835 int prec
= TYPE_PRECISION (expr_type
);
2836 int overflow_pos
= prec
;
2838 double_int bound
, complement
, low_bound
, high_bound
;
2839 bool uns
= TYPE_UNSIGNED (expr_type
);
2840 bool in_bounds
= false;
2845 bound_shift
= overflow_pos
- TREE_INT_CST_LOW (vr1
.max
);
2846 /* If bound_shift == HOST_BITS_PER_DOUBLE_INT, the llshift can
2847 overflow. However, for that to happen, vr1.max needs to be
2848 zero, which means vr1 is a singleton range of zero, which
2849 means it should be handled by the previous LSHIFT_EXPR
2851 bound
= double_int_one
.llshift (bound_shift
, prec
);
2852 complement
= ~(bound
- double_int_one
);
2856 low_bound
= bound
.zext (prec
);
2857 high_bound
= complement
.zext (prec
);
2858 if (tree_to_double_int (vr0
.max
).ult (low_bound
))
2860 /* [5, 6] << [1, 2] == [10, 24]. */
2861 /* We're shifting out only zeroes, the value increases
2865 else if (high_bound
.ult (tree_to_double_int (vr0
.min
)))
2867 /* [0xffffff00, 0xffffffff] << [1, 2]
2868 == [0xfffffc00, 0xfffffffe]. */
2869 /* We're shifting out only ones, the value decreases
2876 /* [-1, 1] << [1, 2] == [-4, 4]. */
2877 low_bound
= complement
.sext (prec
);
2879 if (tree_to_double_int (vr0
.max
).slt (high_bound
)
2880 && low_bound
.slt (tree_to_double_int (vr0
.min
)))
2882 /* For non-negative numbers, we're shifting out only
2883 zeroes, the value increases monotonically.
2884 For negative numbers, we're shifting out only ones, the
2885 value decreases monotomically. */
2892 extract_range_from_multiplicative_op_1 (vr
, code
, &vr0
, &vr1
);
2897 set_value_range_to_varying (vr
);
2900 else if (code
== TRUNC_DIV_EXPR
2901 || code
== FLOOR_DIV_EXPR
2902 || code
== CEIL_DIV_EXPR
2903 || code
== EXACT_DIV_EXPR
2904 || code
== ROUND_DIV_EXPR
)
2906 if (vr0
.type
!= VR_RANGE
|| symbolic_range_p (&vr0
))
2908 /* For division, if op1 has VR_RANGE but op0 does not, something
2909 can be deduced just from that range. Say [min, max] / [4, max]
2910 gives [min / 4, max / 4] range. */
2911 if (vr1
.type
== VR_RANGE
2912 && !symbolic_range_p (&vr1
)
2913 && range_includes_zero_p (vr1
.min
, vr1
.max
) == 0)
2915 vr0
.type
= type
= VR_RANGE
;
2916 vr0
.min
= vrp_val_min (expr_type
);
2917 vr0
.max
= vrp_val_max (expr_type
);
2921 set_value_range_to_varying (vr
);
2926 /* For divisions, if flag_non_call_exceptions is true, we must
2927 not eliminate a division by zero. */
2928 if (cfun
->can_throw_non_call_exceptions
2929 && (vr1
.type
!= VR_RANGE
2930 || range_includes_zero_p (vr1
.min
, vr1
.max
) != 0))
2932 set_value_range_to_varying (vr
);
2936 /* For divisions, if op0 is VR_RANGE, we can deduce a range
2937 even if op1 is VR_VARYING, VR_ANTI_RANGE, symbolic or can
2939 if (vr0
.type
== VR_RANGE
2940 && (vr1
.type
!= VR_RANGE
2941 || range_includes_zero_p (vr1
.min
, vr1
.max
) != 0))
2943 tree zero
= build_int_cst (TREE_TYPE (vr0
.min
), 0);
2948 if (TYPE_UNSIGNED (expr_type
)
2949 || value_range_nonnegative_p (&vr1
))
2951 /* For unsigned division or when divisor is known
2952 to be non-negative, the range has to cover
2953 all numbers from 0 to max for positive max
2954 and all numbers from min to 0 for negative min. */
2955 cmp
= compare_values (vr0
.max
, zero
);
2958 else if (cmp
== 0 || cmp
== 1)
2962 cmp
= compare_values (vr0
.min
, zero
);
2965 else if (cmp
== 0 || cmp
== -1)
2972 /* Otherwise the range is -max .. max or min .. -min
2973 depending on which bound is bigger in absolute value,
2974 as the division can change the sign. */
2975 abs_extent_range (vr
, vr0
.min
, vr0
.max
);
2978 if (type
== VR_VARYING
)
2980 set_value_range_to_varying (vr
);
2986 extract_range_from_multiplicative_op_1 (vr
, code
, &vr0
, &vr1
);
2990 else if (code
== TRUNC_MOD_EXPR
)
2992 if (vr1
.type
!= VR_RANGE
2993 || range_includes_zero_p (vr1
.min
, vr1
.max
) != 0
2994 || vrp_val_is_min (vr1
.min
))
2996 set_value_range_to_varying (vr
);
3000 /* Compute MAX <|vr1.min|, |vr1.max|> - 1. */
3001 max
= fold_unary_to_constant (ABS_EXPR
, expr_type
, vr1
.min
);
3002 if (tree_int_cst_lt (max
, vr1
.max
))
3004 max
= int_const_binop (MINUS_EXPR
, max
, integer_one_node
);
3005 /* If the dividend is non-negative the modulus will be
3006 non-negative as well. */
3007 if (TYPE_UNSIGNED (expr_type
)
3008 || value_range_nonnegative_p (&vr0
))
3009 min
= build_int_cst (TREE_TYPE (max
), 0);
3011 min
= fold_unary_to_constant (NEGATE_EXPR
, expr_type
, max
);
3013 else if (code
== BIT_AND_EXPR
|| code
== BIT_IOR_EXPR
|| code
== BIT_XOR_EXPR
)
3015 bool int_cst_range0
, int_cst_range1
;
3016 double_int may_be_nonzero0
, may_be_nonzero1
;
3017 double_int must_be_nonzero0
, must_be_nonzero1
;
3019 int_cst_range0
= zero_nonzero_bits_from_vr (&vr0
, &may_be_nonzero0
,
3021 int_cst_range1
= zero_nonzero_bits_from_vr (&vr1
, &may_be_nonzero1
,
3025 if (code
== BIT_AND_EXPR
)
3028 min
= double_int_to_tree (expr_type
,
3029 must_be_nonzero0
& must_be_nonzero1
);
3030 dmax
= may_be_nonzero0
& may_be_nonzero1
;
3031 /* If both input ranges contain only negative values we can
3032 truncate the result range maximum to the minimum of the
3033 input range maxima. */
3034 if (int_cst_range0
&& int_cst_range1
3035 && tree_int_cst_sgn (vr0
.max
) < 0
3036 && tree_int_cst_sgn (vr1
.max
) < 0)
3038 dmax
= dmax
.min (tree_to_double_int (vr0
.max
),
3039 TYPE_UNSIGNED (expr_type
));
3040 dmax
= dmax
.min (tree_to_double_int (vr1
.max
),
3041 TYPE_UNSIGNED (expr_type
));
3043 /* If either input range contains only non-negative values
3044 we can truncate the result range maximum to the respective
3045 maximum of the input range. */
3046 if (int_cst_range0
&& tree_int_cst_sgn (vr0
.min
) >= 0)
3047 dmax
= dmax
.min (tree_to_double_int (vr0
.max
),
3048 TYPE_UNSIGNED (expr_type
));
3049 if (int_cst_range1
&& tree_int_cst_sgn (vr1
.min
) >= 0)
3050 dmax
= dmax
.min (tree_to_double_int (vr1
.max
),
3051 TYPE_UNSIGNED (expr_type
));
3052 max
= double_int_to_tree (expr_type
, dmax
);
3054 else if (code
== BIT_IOR_EXPR
)
3057 max
= double_int_to_tree (expr_type
,
3058 may_be_nonzero0
| may_be_nonzero1
);
3059 dmin
= must_be_nonzero0
| must_be_nonzero1
;
3060 /* If the input ranges contain only positive values we can
3061 truncate the minimum of the result range to the maximum
3062 of the input range minima. */
3063 if (int_cst_range0
&& int_cst_range1
3064 && tree_int_cst_sgn (vr0
.min
) >= 0
3065 && tree_int_cst_sgn (vr1
.min
) >= 0)
3067 dmin
= dmin
.max (tree_to_double_int (vr0
.min
),
3068 TYPE_UNSIGNED (expr_type
));
3069 dmin
= dmin
.max (tree_to_double_int (vr1
.min
),
3070 TYPE_UNSIGNED (expr_type
));
3072 /* If either input range contains only negative values
3073 we can truncate the minimum of the result range to the
3074 respective minimum range. */
3075 if (int_cst_range0
&& tree_int_cst_sgn (vr0
.max
) < 0)
3076 dmin
= dmin
.max (tree_to_double_int (vr0
.min
),
3077 TYPE_UNSIGNED (expr_type
));
3078 if (int_cst_range1
&& tree_int_cst_sgn (vr1
.max
) < 0)
3079 dmin
= dmin
.max (tree_to_double_int (vr1
.min
),
3080 TYPE_UNSIGNED (expr_type
));
3081 min
= double_int_to_tree (expr_type
, dmin
);
3083 else if (code
== BIT_XOR_EXPR
)
3085 double_int result_zero_bits
, result_one_bits
;
3086 result_zero_bits
= (must_be_nonzero0
& must_be_nonzero1
)
3087 | ~(may_be_nonzero0
| may_be_nonzero1
);
3088 result_one_bits
= must_be_nonzero0
.and_not (may_be_nonzero1
)
3089 | must_be_nonzero1
.and_not (may_be_nonzero0
);
3090 max
= double_int_to_tree (expr_type
, ~result_zero_bits
);
3091 min
= double_int_to_tree (expr_type
, result_one_bits
);
3092 /* If the range has all positive or all negative values the
3093 result is better than VARYING. */
3094 if (tree_int_cst_sgn (min
) < 0
3095 || tree_int_cst_sgn (max
) >= 0)
3098 max
= min
= NULL_TREE
;
3104 /* If either MIN or MAX overflowed, then set the resulting range to
3105 VARYING. But we do accept an overflow infinity
3107 if (min
== NULL_TREE
3108 || !is_gimple_min_invariant (min
)
3109 || (TREE_OVERFLOW (min
) && !is_overflow_infinity (min
))
3111 || !is_gimple_min_invariant (max
)
3112 || (TREE_OVERFLOW (max
) && !is_overflow_infinity (max
)))
3114 set_value_range_to_varying (vr
);
3120 2) [-INF, +-INF(OVF)]
3121 3) [+-INF(OVF), +INF]
3122 4) [+-INF(OVF), +-INF(OVF)]
3123 We learn nothing when we have INF and INF(OVF) on both sides.
3124 Note that we do accept [-INF, -INF] and [+INF, +INF] without
3126 if ((vrp_val_is_min (min
) || is_overflow_infinity (min
))
3127 && (vrp_val_is_max (max
) || is_overflow_infinity (max
)))
3129 set_value_range_to_varying (vr
);
3133 cmp
= compare_values (min
, max
);
3134 if (cmp
== -2 || cmp
== 1)
3136 /* If the new range has its limits swapped around (MIN > MAX),
3137 then the operation caused one of them to wrap around, mark
3138 the new range VARYING. */
3139 set_value_range_to_varying (vr
);
3142 set_value_range (vr
, type
, min
, max
, NULL
);
3145 /* Extract range information from a binary expression OP0 CODE OP1 based on
3146 the ranges of each of its operands with resulting type EXPR_TYPE.
3147 The resulting range is stored in *VR. */
3150 extract_range_from_binary_expr (value_range_t
*vr
,
3151 enum tree_code code
,
3152 tree expr_type
, tree op0
, tree op1
)
3154 value_range_t vr0
= VR_INITIALIZER
;
3155 value_range_t vr1
= VR_INITIALIZER
;
3157 /* Get value ranges for each operand. For constant operands, create
3158 a new value range with the operand to simplify processing. */
3159 if (TREE_CODE (op0
) == SSA_NAME
)
3160 vr0
= *(get_value_range (op0
));
3161 else if (is_gimple_min_invariant (op0
))
3162 set_value_range_to_value (&vr0
, op0
, NULL
);
3164 set_value_range_to_varying (&vr0
);
3166 if (TREE_CODE (op1
) == SSA_NAME
)
3167 vr1
= *(get_value_range (op1
));
3168 else if (is_gimple_min_invariant (op1
))
3169 set_value_range_to_value (&vr1
, op1
, NULL
);
3171 set_value_range_to_varying (&vr1
);
3173 extract_range_from_binary_expr_1 (vr
, code
, expr_type
, &vr0
, &vr1
);
3176 /* Extract range information from a unary operation CODE based on
3177 the range of its operand *VR0 with type OP0_TYPE with resulting type TYPE.
3178 The The resulting range is stored in *VR. */
3181 extract_range_from_unary_expr_1 (value_range_t
*vr
,
3182 enum tree_code code
, tree type
,
3183 value_range_t
*vr0_
, tree op0_type
)
3185 value_range_t vr0
= *vr0_
, vrtem0
= VR_INITIALIZER
, vrtem1
= VR_INITIALIZER
;
3187 /* VRP only operates on integral and pointer types. */
3188 if (!(INTEGRAL_TYPE_P (op0_type
)
3189 || POINTER_TYPE_P (op0_type
))
3190 || !(INTEGRAL_TYPE_P (type
)
3191 || POINTER_TYPE_P (type
)))
3193 set_value_range_to_varying (vr
);
3197 /* If VR0 is UNDEFINED, so is the result. */
3198 if (vr0
.type
== VR_UNDEFINED
)
3200 set_value_range_to_undefined (vr
);
3204 /* Handle operations that we express in terms of others. */
3205 if (code
== PAREN_EXPR
)
3207 /* PAREN_EXPR is a simple copy. */
3208 copy_value_range (vr
, &vr0
);
3211 else if (code
== NEGATE_EXPR
)
3213 /* -X is simply 0 - X, so re-use existing code that also handles
3214 anti-ranges fine. */
3215 value_range_t zero
= VR_INITIALIZER
;
3216 set_value_range_to_value (&zero
, build_int_cst (type
, 0), NULL
);
3217 extract_range_from_binary_expr_1 (vr
, MINUS_EXPR
, type
, &zero
, &vr0
);
3220 else if (code
== BIT_NOT_EXPR
)
3222 /* ~X is simply -1 - X, so re-use existing code that also handles
3223 anti-ranges fine. */
3224 value_range_t minusone
= VR_INITIALIZER
;
3225 set_value_range_to_value (&minusone
, build_int_cst (type
, -1), NULL
);
3226 extract_range_from_binary_expr_1 (vr
, MINUS_EXPR
,
3227 type
, &minusone
, &vr0
);
3231 /* Now canonicalize anti-ranges to ranges when they are not symbolic
3232 and express op ~[] as (op []') U (op []''). */
3233 if (vr0
.type
== VR_ANTI_RANGE
3234 && ranges_from_anti_range (&vr0
, &vrtem0
, &vrtem1
))
3236 extract_range_from_unary_expr_1 (vr
, code
, type
, &vrtem0
, op0_type
);
3237 if (vrtem1
.type
!= VR_UNDEFINED
)
3239 value_range_t vrres
= VR_INITIALIZER
;
3240 extract_range_from_unary_expr_1 (&vrres
, code
, type
,
3242 vrp_meet (vr
, &vrres
);
3247 if (CONVERT_EXPR_CODE_P (code
))
3249 tree inner_type
= op0_type
;
3250 tree outer_type
= type
;
3252 /* If the expression evaluates to a pointer, we are only interested in
3253 determining if it evaluates to NULL [0, 0] or non-NULL (~[0, 0]). */
3254 if (POINTER_TYPE_P (type
))
3256 if (range_is_nonnull (&vr0
))
3257 set_value_range_to_nonnull (vr
, type
);
3258 else if (range_is_null (&vr0
))
3259 set_value_range_to_null (vr
, type
);
3261 set_value_range_to_varying (vr
);
3265 /* If VR0 is varying and we increase the type precision, assume
3266 a full range for the following transformation. */
3267 if (vr0
.type
== VR_VARYING
3268 && INTEGRAL_TYPE_P (inner_type
)
3269 && TYPE_PRECISION (inner_type
) < TYPE_PRECISION (outer_type
))
3271 vr0
.type
= VR_RANGE
;
3272 vr0
.min
= TYPE_MIN_VALUE (inner_type
);
3273 vr0
.max
= TYPE_MAX_VALUE (inner_type
);
3276 /* If VR0 is a constant range or anti-range and the conversion is
3277 not truncating we can convert the min and max values and
3278 canonicalize the resulting range. Otherwise we can do the
3279 conversion if the size of the range is less than what the
3280 precision of the target type can represent and the range is
3281 not an anti-range. */
3282 if ((vr0
.type
== VR_RANGE
3283 || vr0
.type
== VR_ANTI_RANGE
)
3284 && TREE_CODE (vr0
.min
) == INTEGER_CST
3285 && TREE_CODE (vr0
.max
) == INTEGER_CST
3286 && (!is_overflow_infinity (vr0
.min
)
3287 || (vr0
.type
== VR_RANGE
3288 && TYPE_PRECISION (outer_type
) > TYPE_PRECISION (inner_type
)
3289 && needs_overflow_infinity (outer_type
)
3290 && supports_overflow_infinity (outer_type
)))
3291 && (!is_overflow_infinity (vr0
.max
)
3292 || (vr0
.type
== VR_RANGE
3293 && TYPE_PRECISION (outer_type
) > TYPE_PRECISION (inner_type
)
3294 && needs_overflow_infinity (outer_type
)
3295 && supports_overflow_infinity (outer_type
)))
3296 && (TYPE_PRECISION (outer_type
) >= TYPE_PRECISION (inner_type
)
3297 || (vr0
.type
== VR_RANGE
3298 && integer_zerop (int_const_binop (RSHIFT_EXPR
,
3299 int_const_binop (MINUS_EXPR
, vr0
.max
, vr0
.min
),
3300 size_int (TYPE_PRECISION (outer_type
)))))))
3302 tree new_min
, new_max
;
3303 if (is_overflow_infinity (vr0
.min
))
3304 new_min
= negative_overflow_infinity (outer_type
);
3306 new_min
= force_fit_type_double (outer_type
,
3307 tree_to_double_int (vr0
.min
),
3309 if (is_overflow_infinity (vr0
.max
))
3310 new_max
= positive_overflow_infinity (outer_type
);
3312 new_max
= force_fit_type_double (outer_type
,
3313 tree_to_double_int (vr0
.max
),
3315 set_and_canonicalize_value_range (vr
, vr0
.type
,
3316 new_min
, new_max
, NULL
);
3320 set_value_range_to_varying (vr
);
3323 else if (code
== ABS_EXPR
)
3328 /* Pass through vr0 in the easy cases. */
3329 if (TYPE_UNSIGNED (type
)
3330 || value_range_nonnegative_p (&vr0
))
3332 copy_value_range (vr
, &vr0
);
3336 /* For the remaining varying or symbolic ranges we can't do anything
3338 if (vr0
.type
== VR_VARYING
3339 || symbolic_range_p (&vr0
))
3341 set_value_range_to_varying (vr
);
3345 /* -TYPE_MIN_VALUE = TYPE_MIN_VALUE with flag_wrapv so we can't get a
3347 if (!TYPE_OVERFLOW_UNDEFINED (type
)
3348 && ((vr0
.type
== VR_RANGE
3349 && vrp_val_is_min (vr0
.min
))
3350 || (vr0
.type
== VR_ANTI_RANGE
3351 && !vrp_val_is_min (vr0
.min
))))
3353 set_value_range_to_varying (vr
);
3357 /* ABS_EXPR may flip the range around, if the original range
3358 included negative values. */
3359 if (is_overflow_infinity (vr0
.min
))
3360 min
= positive_overflow_infinity (type
);
3361 else if (!vrp_val_is_min (vr0
.min
))
3362 min
= fold_unary_to_constant (code
, type
, vr0
.min
);
3363 else if (!needs_overflow_infinity (type
))
3364 min
= TYPE_MAX_VALUE (type
);
3365 else if (supports_overflow_infinity (type
))
3366 min
= positive_overflow_infinity (type
);
3369 set_value_range_to_varying (vr
);
3373 if (is_overflow_infinity (vr0
.max
))
3374 max
= positive_overflow_infinity (type
);
3375 else if (!vrp_val_is_min (vr0
.max
))
3376 max
= fold_unary_to_constant (code
, type
, vr0
.max
);
3377 else if (!needs_overflow_infinity (type
))
3378 max
= TYPE_MAX_VALUE (type
);
3379 else if (supports_overflow_infinity (type
)
3380 /* We shouldn't generate [+INF, +INF] as set_value_range
3381 doesn't like this and ICEs. */
3382 && !is_positive_overflow_infinity (min
))
3383 max
= positive_overflow_infinity (type
);
3386 set_value_range_to_varying (vr
);
3390 cmp
= compare_values (min
, max
);
3392 /* If a VR_ANTI_RANGEs contains zero, then we have
3393 ~[-INF, min(MIN, MAX)]. */
3394 if (vr0
.type
== VR_ANTI_RANGE
)
3396 if (range_includes_zero_p (vr0
.min
, vr0
.max
) == 1)
3398 /* Take the lower of the two values. */
3402 /* Create ~[-INF, min (abs(MIN), abs(MAX))]
3403 or ~[-INF + 1, min (abs(MIN), abs(MAX))] when
3404 flag_wrapv is set and the original anti-range doesn't include
3405 TYPE_MIN_VALUE, remember -TYPE_MIN_VALUE = TYPE_MIN_VALUE. */
3406 if (TYPE_OVERFLOW_WRAPS (type
))
3408 tree type_min_value
= TYPE_MIN_VALUE (type
);
3410 min
= (vr0
.min
!= type_min_value
3411 ? int_const_binop (PLUS_EXPR
, type_min_value
,
3417 if (overflow_infinity_range_p (&vr0
))
3418 min
= negative_overflow_infinity (type
);
3420 min
= TYPE_MIN_VALUE (type
);
3425 /* All else has failed, so create the range [0, INF], even for
3426 flag_wrapv since TYPE_MIN_VALUE is in the original
3428 vr0
.type
= VR_RANGE
;
3429 min
= build_int_cst (type
, 0);
3430 if (needs_overflow_infinity (type
))
3432 if (supports_overflow_infinity (type
))
3433 max
= positive_overflow_infinity (type
);
3436 set_value_range_to_varying (vr
);
3441 max
= TYPE_MAX_VALUE (type
);
3445 /* If the range contains zero then we know that the minimum value in the
3446 range will be zero. */
3447 else if (range_includes_zero_p (vr0
.min
, vr0
.max
) == 1)
3451 min
= build_int_cst (type
, 0);
3455 /* If the range was reversed, swap MIN and MAX. */
3464 cmp
= compare_values (min
, max
);
3465 if (cmp
== -2 || cmp
== 1)
3467 /* If the new range has its limits swapped around (MIN > MAX),
3468 then the operation caused one of them to wrap around, mark
3469 the new range VARYING. */
3470 set_value_range_to_varying (vr
);
3473 set_value_range (vr
, vr0
.type
, min
, max
, NULL
);
3477 /* For unhandled operations fall back to varying. */
3478 set_value_range_to_varying (vr
);
3483 /* Extract range information from a unary expression CODE OP0 based on
3484 the range of its operand with resulting type TYPE.
3485 The resulting range is stored in *VR. */
3488 extract_range_from_unary_expr (value_range_t
*vr
, enum tree_code code
,
3489 tree type
, tree op0
)
3491 value_range_t vr0
= VR_INITIALIZER
;
3493 /* Get value ranges for the operand. For constant operands, create
3494 a new value range with the operand to simplify processing. */
3495 if (TREE_CODE (op0
) == SSA_NAME
)
3496 vr0
= *(get_value_range (op0
));
3497 else if (is_gimple_min_invariant (op0
))
3498 set_value_range_to_value (&vr0
, op0
, NULL
);
3500 set_value_range_to_varying (&vr0
);
3502 extract_range_from_unary_expr_1 (vr
, code
, type
, &vr0
, TREE_TYPE (op0
));
3506 /* Extract range information from a conditional expression STMT based on
3507 the ranges of each of its operands and the expression code. */
3510 extract_range_from_cond_expr (value_range_t
*vr
, gimple stmt
)
3513 value_range_t vr0
= VR_INITIALIZER
;
3514 value_range_t vr1
= VR_INITIALIZER
;
3516 /* Get value ranges for each operand. For constant operands, create
3517 a new value range with the operand to simplify processing. */
3518 op0
= gimple_assign_rhs2 (stmt
);
3519 if (TREE_CODE (op0
) == SSA_NAME
)
3520 vr0
= *(get_value_range (op0
));
3521 else if (is_gimple_min_invariant (op0
))
3522 set_value_range_to_value (&vr0
, op0
, NULL
);
3524 set_value_range_to_varying (&vr0
);
3526 op1
= gimple_assign_rhs3 (stmt
);
3527 if (TREE_CODE (op1
) == SSA_NAME
)
3528 vr1
= *(get_value_range (op1
));
3529 else if (is_gimple_min_invariant (op1
))
3530 set_value_range_to_value (&vr1
, op1
, NULL
);
3532 set_value_range_to_varying (&vr1
);
3534 /* The resulting value range is the union of the operand ranges */
3535 copy_value_range (vr
, &vr0
);
3536 vrp_meet (vr
, &vr1
);
3540 /* Extract range information from a comparison expression EXPR based
3541 on the range of its operand and the expression code. */
3544 extract_range_from_comparison (value_range_t
*vr
, enum tree_code code
,
3545 tree type
, tree op0
, tree op1
)
3550 val
= vrp_evaluate_conditional_warnv_with_ops (code
, op0
, op1
, false, &sop
,
3553 /* A disadvantage of using a special infinity as an overflow
3554 representation is that we lose the ability to record overflow
3555 when we don't have an infinity. So we have to ignore a result
3556 which relies on overflow. */
3558 if (val
&& !is_overflow_infinity (val
) && !sop
)
3560 /* Since this expression was found on the RHS of an assignment,
3561 its type may be different from _Bool. Convert VAL to EXPR's
3563 val
= fold_convert (type
, val
);
3564 if (is_gimple_min_invariant (val
))
3565 set_value_range_to_value (vr
, val
, vr
->equiv
);
3567 set_value_range (vr
, VR_RANGE
, val
, val
, vr
->equiv
);
3570 /* The result of a comparison is always true or false. */
3571 set_value_range_to_truthvalue (vr
, type
);
3574 /* Try to derive a nonnegative or nonzero range out of STMT relying
3575 primarily on generic routines in fold in conjunction with range data.
3576 Store the result in *VR */
3579 extract_range_basic (value_range_t
*vr
, gimple stmt
)
3582 tree type
= gimple_expr_type (stmt
);
3584 if (gimple_call_builtin_p (stmt
, BUILT_IN_NORMAL
))
3586 tree fndecl
= gimple_call_fndecl (stmt
), arg
;
3587 int mini
, maxi
, zerov
= 0, prec
;
3589 switch (DECL_FUNCTION_CODE (fndecl
))
3591 case BUILT_IN_CONSTANT_P
:
3592 /* If the call is __builtin_constant_p and the argument is a
3593 function parameter resolve it to false. This avoids bogus
3594 array bound warnings.
3595 ??? We could do this as early as inlining is finished. */
3596 arg
= gimple_call_arg (stmt
, 0);
3597 if (TREE_CODE (arg
) == SSA_NAME
3598 && SSA_NAME_IS_DEFAULT_DEF (arg
)
3599 && TREE_CODE (SSA_NAME_VAR (arg
)) == PARM_DECL
)
3601 set_value_range_to_null (vr
, type
);
3605 /* Both __builtin_ffs* and __builtin_popcount return
3607 CASE_INT_FN (BUILT_IN_FFS
):
3608 CASE_INT_FN (BUILT_IN_POPCOUNT
):
3609 arg
= gimple_call_arg (stmt
, 0);
3610 prec
= TYPE_PRECISION (TREE_TYPE (arg
));
3613 if (TREE_CODE (arg
) == SSA_NAME
)
3615 value_range_t
*vr0
= get_value_range (arg
);
3616 /* If arg is non-zero, then ffs or popcount
3618 if (((vr0
->type
== VR_RANGE
3619 && integer_nonzerop (vr0
->min
))
3620 || (vr0
->type
== VR_ANTI_RANGE
3621 && integer_zerop (vr0
->min
)))
3622 && !is_overflow_infinity (vr0
->min
))
3624 /* If some high bits are known to be zero,
3625 we can decrease the maximum. */
3626 if (vr0
->type
== VR_RANGE
3627 && TREE_CODE (vr0
->max
) == INTEGER_CST
3628 && !is_overflow_infinity (vr0
->max
))
3629 maxi
= tree_floor_log2 (vr0
->max
) + 1;
3632 /* __builtin_parity* returns [0, 1]. */
3633 CASE_INT_FN (BUILT_IN_PARITY
):
3637 /* __builtin_c[lt]z* return [0, prec-1], except for
3638 when the argument is 0, but that is undefined behavior.
3639 On many targets where the CLZ RTL or optab value is defined
3640 for 0 the value is prec, so include that in the range
3642 CASE_INT_FN (BUILT_IN_CLZ
):
3643 arg
= gimple_call_arg (stmt
, 0);
3644 prec
= TYPE_PRECISION (TREE_TYPE (arg
));
3647 if (optab_handler (clz_optab
, TYPE_MODE (TREE_TYPE (arg
)))
3649 && CLZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (TREE_TYPE (arg
)),
3651 /* Handle only the single common value. */
3653 /* Magic value to give up, unless vr0 proves
3656 if (TREE_CODE (arg
) == SSA_NAME
)
3658 value_range_t
*vr0
= get_value_range (arg
);
3659 /* From clz of VR_RANGE minimum we can compute
3661 if (vr0
->type
== VR_RANGE
3662 && TREE_CODE (vr0
->min
) == INTEGER_CST
3663 && !is_overflow_infinity (vr0
->min
))
3665 maxi
= prec
- 1 - tree_floor_log2 (vr0
->min
);
3669 else if (vr0
->type
== VR_ANTI_RANGE
3670 && integer_zerop (vr0
->min
)
3671 && !is_overflow_infinity (vr0
->min
))
3678 /* From clz of VR_RANGE maximum we can compute
3680 if (vr0
->type
== VR_RANGE
3681 && TREE_CODE (vr0
->max
) == INTEGER_CST
3682 && !is_overflow_infinity (vr0
->max
))
3684 mini
= prec
- 1 - tree_floor_log2 (vr0
->max
);
3692 /* __builtin_ctz* return [0, prec-1], except for
3693 when the argument is 0, but that is undefined behavior.
3694 If there is a ctz optab for this mode and
3695 CTZ_DEFINED_VALUE_AT_ZERO, include that in the range,
3696 otherwise just assume 0 won't be seen. */
3697 CASE_INT_FN (BUILT_IN_CTZ
):
3698 arg
= gimple_call_arg (stmt
, 0);
3699 prec
= TYPE_PRECISION (TREE_TYPE (arg
));
3702 if (optab_handler (ctz_optab
, TYPE_MODE (TREE_TYPE (arg
)))
3704 && CTZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (TREE_TYPE (arg
)),
3707 /* Handle only the two common values. */
3710 else if (zerov
== prec
)
3713 /* Magic value to give up, unless vr0 proves
3717 if (TREE_CODE (arg
) == SSA_NAME
)
3719 value_range_t
*vr0
= get_value_range (arg
);
3720 /* If arg is non-zero, then use [0, prec - 1]. */
3721 if (((vr0
->type
== VR_RANGE
3722 && integer_nonzerop (vr0
->min
))
3723 || (vr0
->type
== VR_ANTI_RANGE
3724 && integer_zerop (vr0
->min
)))
3725 && !is_overflow_infinity (vr0
->min
))
3730 /* If some high bits are known to be zero,
3731 we can decrease the result maximum. */
3732 if (vr0
->type
== VR_RANGE
3733 && TREE_CODE (vr0
->max
) == INTEGER_CST
3734 && !is_overflow_infinity (vr0
->max
))
3736 maxi
= tree_floor_log2 (vr0
->max
);
3737 /* For vr0 [0, 0] give up. */
3745 /* __builtin_clrsb* returns [0, prec-1]. */
3746 CASE_INT_FN (BUILT_IN_CLRSB
):
3747 arg
= gimple_call_arg (stmt
, 0);
3748 prec
= TYPE_PRECISION (TREE_TYPE (arg
));
3753 set_value_range (vr
, VR_RANGE
, build_int_cst (type
, mini
),
3754 build_int_cst (type
, maxi
), NULL
);
3760 else if (is_gimple_call (stmt
)
3761 && gimple_call_internal_p (stmt
))
3763 enum tree_code subcode
= ERROR_MARK
;
3764 switch (gimple_call_internal_fn (stmt
))
3766 case IFN_UBSAN_CHECK_ADD
:
3767 subcode
= PLUS_EXPR
;
3769 case IFN_UBSAN_CHECK_SUB
:
3770 subcode
= MINUS_EXPR
;
3772 case IFN_UBSAN_CHECK_MUL
:
3773 subcode
= MULT_EXPR
;
3778 if (subcode
!= ERROR_MARK
)
3780 bool saved_flag_wrapv
= flag_wrapv
;
3781 /* Pretend the arithmetics is wrapping. If there is
3782 any overflow, we'll complain, but will actually do
3783 wrapping operation. */
3785 extract_range_from_binary_expr (vr
, subcode
, type
,
3786 gimple_call_arg (stmt
, 0),
3787 gimple_call_arg (stmt
, 1));
3788 flag_wrapv
= saved_flag_wrapv
;
3790 /* If for both arguments vrp_valueize returned non-NULL,
3791 this should have been already folded and if not, it
3792 wasn't folded because of overflow. Avoid removing the
3793 UBSAN_CHECK_* calls in that case. */
3794 if (vr
->type
== VR_RANGE
3795 && (vr
->min
== vr
->max
3796 || operand_equal_p (vr
->min
, vr
->max
, 0)))
3797 set_value_range_to_varying (vr
);
3801 if (INTEGRAL_TYPE_P (type
)
3802 && gimple_stmt_nonnegative_warnv_p (stmt
, &sop
))
3803 set_value_range_to_nonnegative (vr
, type
,
3804 sop
|| stmt_overflow_infinity (stmt
));
3805 else if (vrp_stmt_computes_nonzero (stmt
, &sop
)
3807 set_value_range_to_nonnull (vr
, type
);
3809 set_value_range_to_varying (vr
);
3813 /* Try to compute a useful range out of assignment STMT and store it
3817 extract_range_from_assignment (value_range_t
*vr
, gimple stmt
)
3819 enum tree_code code
= gimple_assign_rhs_code (stmt
);
3821 if (code
== ASSERT_EXPR
)
3822 extract_range_from_assert (vr
, gimple_assign_rhs1 (stmt
));
3823 else if (code
== SSA_NAME
)
3824 extract_range_from_ssa_name (vr
, gimple_assign_rhs1 (stmt
));
3825 else if (TREE_CODE_CLASS (code
) == tcc_binary
)
3826 extract_range_from_binary_expr (vr
, gimple_assign_rhs_code (stmt
),
3827 gimple_expr_type (stmt
),
3828 gimple_assign_rhs1 (stmt
),
3829 gimple_assign_rhs2 (stmt
));
3830 else if (TREE_CODE_CLASS (code
) == tcc_unary
)
3831 extract_range_from_unary_expr (vr
, gimple_assign_rhs_code (stmt
),
3832 gimple_expr_type (stmt
),
3833 gimple_assign_rhs1 (stmt
));
3834 else if (code
== COND_EXPR
)
3835 extract_range_from_cond_expr (vr
, stmt
);
3836 else if (TREE_CODE_CLASS (code
) == tcc_comparison
)
3837 extract_range_from_comparison (vr
, gimple_assign_rhs_code (stmt
),
3838 gimple_expr_type (stmt
),
3839 gimple_assign_rhs1 (stmt
),
3840 gimple_assign_rhs2 (stmt
));
3841 else if (get_gimple_rhs_class (code
) == GIMPLE_SINGLE_RHS
3842 && is_gimple_min_invariant (gimple_assign_rhs1 (stmt
)))
3843 set_value_range_to_value (vr
, gimple_assign_rhs1 (stmt
), NULL
);
3845 set_value_range_to_varying (vr
);
3847 if (vr
->type
== VR_VARYING
)
3848 extract_range_basic (vr
, stmt
);
3851 /* Given a range VR, a LOOP and a variable VAR, determine whether it
3852 would be profitable to adjust VR using scalar evolution information
3853 for VAR. If so, update VR with the new limits. */
3856 adjust_range_with_scev (value_range_t
*vr
, struct loop
*loop
,
3857 gimple stmt
, tree var
)
3859 tree init
, step
, chrec
, tmin
, tmax
, min
, max
, type
, tem
;
3860 enum ev_direction dir
;
3862 /* TODO. Don't adjust anti-ranges. An anti-range may provide
3863 better opportunities than a regular range, but I'm not sure. */
3864 if (vr
->type
== VR_ANTI_RANGE
)
3867 chrec
= instantiate_parameters (loop
, analyze_scalar_evolution (loop
, var
));
3869 /* Like in PR19590, scev can return a constant function. */
3870 if (is_gimple_min_invariant (chrec
))
3872 set_value_range_to_value (vr
, chrec
, vr
->equiv
);
3876 if (TREE_CODE (chrec
) != POLYNOMIAL_CHREC
)
3879 init
= initial_condition_in_loop_num (chrec
, loop
->num
);
3880 tem
= op_with_constant_singleton_value_range (init
);
3883 step
= evolution_part_in_loop_num (chrec
, loop
->num
);
3884 tem
= op_with_constant_singleton_value_range (step
);
3888 /* If STEP is symbolic, we can't know whether INIT will be the
3889 minimum or maximum value in the range. Also, unless INIT is
3890 a simple expression, compare_values and possibly other functions
3891 in tree-vrp won't be able to handle it. */
3892 if (step
== NULL_TREE
3893 || !is_gimple_min_invariant (step
)
3894 || !valid_value_p (init
))
3897 dir
= scev_direction (chrec
);
3898 if (/* Do not adjust ranges if we do not know whether the iv increases
3899 or decreases, ... */
3900 dir
== EV_DIR_UNKNOWN
3901 /* ... or if it may wrap. */
3902 || scev_probably_wraps_p (init
, step
, stmt
, get_chrec_loop (chrec
),
3906 /* We use TYPE_MIN_VALUE and TYPE_MAX_VALUE here instead of
3907 negative_overflow_infinity and positive_overflow_infinity,
3908 because we have concluded that the loop probably does not
3911 type
= TREE_TYPE (var
);
3912 if (POINTER_TYPE_P (type
) || !TYPE_MIN_VALUE (type
))
3913 tmin
= lower_bound_in_type (type
, type
);
3915 tmin
= TYPE_MIN_VALUE (type
);
3916 if (POINTER_TYPE_P (type
) || !TYPE_MAX_VALUE (type
))
3917 tmax
= upper_bound_in_type (type
, type
);
3919 tmax
= TYPE_MAX_VALUE (type
);
3921 /* Try to use estimated number of iterations for the loop to constrain the
3922 final value in the evolution. */
3923 if (TREE_CODE (step
) == INTEGER_CST
3924 && is_gimple_val (init
)
3925 && (TREE_CODE (init
) != SSA_NAME
3926 || get_value_range (init
)->type
== VR_RANGE
))
3930 /* We are only entering here for loop header PHI nodes, so using
3931 the number of latch executions is the correct thing to use. */
3932 if (max_loop_iterations (loop
, &nit
))
3934 value_range_t maxvr
= VR_INITIALIZER
;
3936 bool unsigned_p
= TYPE_UNSIGNED (TREE_TYPE (step
));
3937 bool overflow
= false;
3939 dtmp
= tree_to_double_int (step
)
3940 .mul_with_sign (nit
, unsigned_p
, &overflow
);
3941 /* If the multiplication overflowed we can't do a meaningful
3942 adjustment. Likewise if the result doesn't fit in the type
3943 of the induction variable. For a signed type we have to
3944 check whether the result has the expected signedness which
3945 is that of the step as number of iterations is unsigned. */
3947 && double_int_fits_to_tree_p (TREE_TYPE (init
), dtmp
)
3949 || ((dtmp
.high
^ TREE_INT_CST_HIGH (step
)) >= 0)))
3951 tem
= double_int_to_tree (TREE_TYPE (init
), dtmp
);
3952 extract_range_from_binary_expr (&maxvr
, PLUS_EXPR
,
3953 TREE_TYPE (init
), init
, tem
);
3954 /* Likewise if the addition did. */
3955 if (maxvr
.type
== VR_RANGE
)
3964 if (vr
->type
== VR_VARYING
|| vr
->type
== VR_UNDEFINED
)
3969 /* For VARYING or UNDEFINED ranges, just about anything we get
3970 from scalar evolutions should be better. */
3972 if (dir
== EV_DIR_DECREASES
)
3977 /* If we would create an invalid range, then just assume we
3978 know absolutely nothing. This may be over-conservative,
3979 but it's clearly safe, and should happen only in unreachable
3980 parts of code, or for invalid programs. */
3981 if (compare_values (min
, max
) == 1)
3984 set_value_range (vr
, VR_RANGE
, min
, max
, vr
->equiv
);
3986 else if (vr
->type
== VR_RANGE
)
3991 if (dir
== EV_DIR_DECREASES
)
3993 /* INIT is the maximum value. If INIT is lower than VR->MAX
3994 but no smaller than VR->MIN, set VR->MAX to INIT. */
3995 if (compare_values (init
, max
) == -1)
3998 /* According to the loop information, the variable does not
3999 overflow. If we think it does, probably because of an
4000 overflow due to arithmetic on a different INF value,
4002 if (is_negative_overflow_infinity (min
)
4003 || compare_values (min
, tmin
) == -1)
4009 /* If INIT is bigger than VR->MIN, set VR->MIN to INIT. */
4010 if (compare_values (init
, min
) == 1)
4013 if (is_positive_overflow_infinity (max
)
4014 || compare_values (tmax
, max
) == -1)
4018 /* If we just created an invalid range with the minimum
4019 greater than the maximum, we fail conservatively.
4020 This should happen only in unreachable
4021 parts of code, or for invalid programs. */
4022 if (compare_values (min
, max
) == 1)
4025 set_value_range (vr
, VR_RANGE
, min
, max
, vr
->equiv
);
4029 /* Return true if VAR may overflow at STMT. This checks any available
4030 loop information to see if we can determine that VAR does not
4034 vrp_var_may_overflow (tree var
, gimple stmt
)
4037 tree chrec
, init
, step
;
4039 if (current_loops
== NULL
)
4042 l
= loop_containing_stmt (stmt
);
4047 chrec
= instantiate_parameters (l
, analyze_scalar_evolution (l
, var
));
4048 if (TREE_CODE (chrec
) != POLYNOMIAL_CHREC
)
4051 init
= initial_condition_in_loop_num (chrec
, l
->num
);
4052 step
= evolution_part_in_loop_num (chrec
, l
->num
);
4054 if (step
== NULL_TREE
4055 || !is_gimple_min_invariant (step
)
4056 || !valid_value_p (init
))
4059 /* If we get here, we know something useful about VAR based on the
4060 loop information. If it wraps, it may overflow. */
4062 if (scev_probably_wraps_p (init
, step
, stmt
, get_chrec_loop (chrec
),
4066 if (dump_file
&& (dump_flags
& TDF_DETAILS
) != 0)
4068 print_generic_expr (dump_file
, var
, 0);
4069 fprintf (dump_file
, ": loop information indicates does not overflow\n");
4076 /* Given two numeric value ranges VR0, VR1 and a comparison code COMP:
4078 - Return BOOLEAN_TRUE_NODE if VR0 COMP VR1 always returns true for
4079 all the values in the ranges.
4081 - Return BOOLEAN_FALSE_NODE if the comparison always returns false.
4083 - Return NULL_TREE if it is not always possible to determine the
4084 value of the comparison.
4086 Also set *STRICT_OVERFLOW_P to indicate whether a range with an
4087 overflow infinity was used in the test. */
4091 compare_ranges (enum tree_code comp
, value_range_t
*vr0
, value_range_t
*vr1
,
4092 bool *strict_overflow_p
)
4094 /* VARYING or UNDEFINED ranges cannot be compared. */
4095 if (vr0
->type
== VR_VARYING
4096 || vr0
->type
== VR_UNDEFINED
4097 || vr1
->type
== VR_VARYING
4098 || vr1
->type
== VR_UNDEFINED
)
4101 /* Anti-ranges need to be handled separately. */
4102 if (vr0
->type
== VR_ANTI_RANGE
|| vr1
->type
== VR_ANTI_RANGE
)
4104 /* If both are anti-ranges, then we cannot compute any
4106 if (vr0
->type
== VR_ANTI_RANGE
&& vr1
->type
== VR_ANTI_RANGE
)
4109 /* These comparisons are never statically computable. */
4116 /* Equality can be computed only between a range and an
4117 anti-range. ~[VAL1, VAL2] == [VAL1, VAL2] is always false. */
4118 if (vr0
->type
== VR_RANGE
)
4120 /* To simplify processing, make VR0 the anti-range. */
4121 value_range_t
*tmp
= vr0
;
4126 gcc_assert (comp
== NE_EXPR
|| comp
== EQ_EXPR
);
4128 if (compare_values_warnv (vr0
->min
, vr1
->min
, strict_overflow_p
) == 0
4129 && compare_values_warnv (vr0
->max
, vr1
->max
, strict_overflow_p
) == 0)
4130 return (comp
== NE_EXPR
) ? boolean_true_node
: boolean_false_node
;
4135 if (!usable_range_p (vr0
, strict_overflow_p
)
4136 || !usable_range_p (vr1
, strict_overflow_p
))
4139 /* Simplify processing. If COMP is GT_EXPR or GE_EXPR, switch the
4140 operands around and change the comparison code. */
4141 if (comp
== GT_EXPR
|| comp
== GE_EXPR
)
4144 comp
= (comp
== GT_EXPR
) ? LT_EXPR
: LE_EXPR
;
4150 if (comp
== EQ_EXPR
)
4152 /* Equality may only be computed if both ranges represent
4153 exactly one value. */
4154 if (compare_values_warnv (vr0
->min
, vr0
->max
, strict_overflow_p
) == 0
4155 && compare_values_warnv (vr1
->min
, vr1
->max
, strict_overflow_p
) == 0)
4157 int cmp_min
= compare_values_warnv (vr0
->min
, vr1
->min
,
4159 int cmp_max
= compare_values_warnv (vr0
->max
, vr1
->max
,
4161 if (cmp_min
== 0 && cmp_max
== 0)
4162 return boolean_true_node
;
4163 else if (cmp_min
!= -2 && cmp_max
!= -2)
4164 return boolean_false_node
;
4166 /* If [V0_MIN, V1_MAX] < [V1_MIN, V1_MAX] then V0 != V1. */
4167 else if (compare_values_warnv (vr0
->min
, vr1
->max
,
4168 strict_overflow_p
) == 1
4169 || compare_values_warnv (vr1
->min
, vr0
->max
,
4170 strict_overflow_p
) == 1)
4171 return boolean_false_node
;
4175 else if (comp
== NE_EXPR
)
4179 /* If VR0 is completely to the left or completely to the right
4180 of VR1, they are always different. Notice that we need to
4181 make sure that both comparisons yield similar results to
4182 avoid comparing values that cannot be compared at
4184 cmp1
= compare_values_warnv (vr0
->max
, vr1
->min
, strict_overflow_p
);
4185 cmp2
= compare_values_warnv (vr0
->min
, vr1
->max
, strict_overflow_p
);
4186 if ((cmp1
== -1 && cmp2
== -1) || (cmp1
== 1 && cmp2
== 1))
4187 return boolean_true_node
;
4189 /* If VR0 and VR1 represent a single value and are identical,
4191 else if (compare_values_warnv (vr0
->min
, vr0
->max
,
4192 strict_overflow_p
) == 0
4193 && compare_values_warnv (vr1
->min
, vr1
->max
,
4194 strict_overflow_p
) == 0
4195 && compare_values_warnv (vr0
->min
, vr1
->min
,
4196 strict_overflow_p
) == 0
4197 && compare_values_warnv (vr0
->max
, vr1
->max
,
4198 strict_overflow_p
) == 0)
4199 return boolean_false_node
;
4201 /* Otherwise, they may or may not be different. */
4205 else if (comp
== LT_EXPR
|| comp
== LE_EXPR
)
4209 /* If VR0 is to the left of VR1, return true. */
4210 tst
= compare_values_warnv (vr0
->max
, vr1
->min
, strict_overflow_p
);
4211 if ((comp
== LT_EXPR
&& tst
== -1)
4212 || (comp
== LE_EXPR
&& (tst
== -1 || tst
== 0)))
4214 if (overflow_infinity_range_p (vr0
)
4215 || overflow_infinity_range_p (vr1
))
4216 *strict_overflow_p
= true;
4217 return boolean_true_node
;
4220 /* If VR0 is to the right of VR1, return false. */
4221 tst
= compare_values_warnv (vr0
->min
, vr1
->max
, strict_overflow_p
);
4222 if ((comp
== LT_EXPR
&& (tst
== 0 || tst
== 1))
4223 || (comp
== LE_EXPR
&& tst
== 1))
4225 if (overflow_infinity_range_p (vr0
)
4226 || overflow_infinity_range_p (vr1
))
4227 *strict_overflow_p
= true;
4228 return boolean_false_node
;
4231 /* Otherwise, we don't know. */
4239 /* Given a value range VR, a value VAL and a comparison code COMP, return
4240 BOOLEAN_TRUE_NODE if VR COMP VAL always returns true for all the
4241 values in VR. Return BOOLEAN_FALSE_NODE if the comparison
4242 always returns false. Return NULL_TREE if it is not always
4243 possible to determine the value of the comparison. Also set
4244 *STRICT_OVERFLOW_P to indicate whether a range with an overflow
4245 infinity was used in the test. */
4248 compare_range_with_value (enum tree_code comp
, value_range_t
*vr
, tree val
,
4249 bool *strict_overflow_p
)
4251 if (vr
->type
== VR_VARYING
|| vr
->type
== VR_UNDEFINED
)
4254 /* Anti-ranges need to be handled separately. */
4255 if (vr
->type
== VR_ANTI_RANGE
)
4257 /* For anti-ranges, the only predicates that we can compute at
4258 compile time are equality and inequality. */
4265 /* ~[VAL_1, VAL_2] OP VAL is known if VAL_1 <= VAL <= VAL_2. */
4266 if (value_inside_range (val
, vr
->min
, vr
->max
) == 1)
4267 return (comp
== NE_EXPR
) ? boolean_true_node
: boolean_false_node
;
4272 if (!usable_range_p (vr
, strict_overflow_p
))
4275 if (comp
== EQ_EXPR
)
4277 /* EQ_EXPR may only be computed if VR represents exactly
4279 if (compare_values_warnv (vr
->min
, vr
->max
, strict_overflow_p
) == 0)
4281 int cmp
= compare_values_warnv (vr
->min
, val
, strict_overflow_p
);
4283 return boolean_true_node
;
4284 else if (cmp
== -1 || cmp
== 1 || cmp
== 2)
4285 return boolean_false_node
;
4287 else if (compare_values_warnv (val
, vr
->min
, strict_overflow_p
) == -1
4288 || compare_values_warnv (vr
->max
, val
, strict_overflow_p
) == -1)
4289 return boolean_false_node
;
4293 else if (comp
== NE_EXPR
)
4295 /* If VAL is not inside VR, then they are always different. */
4296 if (compare_values_warnv (vr
->max
, val
, strict_overflow_p
) == -1
4297 || compare_values_warnv (vr
->min
, val
, strict_overflow_p
) == 1)
4298 return boolean_true_node
;
4300 /* If VR represents exactly one value equal to VAL, then return
4302 if (compare_values_warnv (vr
->min
, vr
->max
, strict_overflow_p
) == 0
4303 && compare_values_warnv (vr
->min
, val
, strict_overflow_p
) == 0)
4304 return boolean_false_node
;
4306 /* Otherwise, they may or may not be different. */
4309 else if (comp
== LT_EXPR
|| comp
== LE_EXPR
)
4313 /* If VR is to the left of VAL, return true. */
4314 tst
= compare_values_warnv (vr
->max
, val
, strict_overflow_p
);
4315 if ((comp
== LT_EXPR
&& tst
== -1)
4316 || (comp
== LE_EXPR
&& (tst
== -1 || tst
== 0)))
4318 if (overflow_infinity_range_p (vr
))
4319 *strict_overflow_p
= true;
4320 return boolean_true_node
;
4323 /* If VR is to the right of VAL, return false. */
4324 tst
= compare_values_warnv (vr
->min
, val
, strict_overflow_p
);
4325 if ((comp
== LT_EXPR
&& (tst
== 0 || tst
== 1))
4326 || (comp
== LE_EXPR
&& tst
== 1))
4328 if (overflow_infinity_range_p (vr
))
4329 *strict_overflow_p
= true;
4330 return boolean_false_node
;
4333 /* Otherwise, we don't know. */
4336 else if (comp
== GT_EXPR
|| comp
== GE_EXPR
)
4340 /* If VR is to the right of VAL, return true. */
4341 tst
= compare_values_warnv (vr
->min
, val
, strict_overflow_p
);
4342 if ((comp
== GT_EXPR
&& tst
== 1)
4343 || (comp
== GE_EXPR
&& (tst
== 0 || tst
== 1)))
4345 if (overflow_infinity_range_p (vr
))
4346 *strict_overflow_p
= true;
4347 return boolean_true_node
;
4350 /* If VR is to the left of VAL, return false. */
4351 tst
= compare_values_warnv (vr
->max
, val
, strict_overflow_p
);
4352 if ((comp
== GT_EXPR
&& (tst
== -1 || tst
== 0))
4353 || (comp
== GE_EXPR
&& tst
== -1))
4355 if (overflow_infinity_range_p (vr
))
4356 *strict_overflow_p
= true;
4357 return boolean_false_node
;
4360 /* Otherwise, we don't know. */
4368 /* Debugging dumps. */
4370 void dump_value_range (FILE *, value_range_t
*);
4371 void debug_value_range (value_range_t
*);
4372 void dump_all_value_ranges (FILE *);
4373 void debug_all_value_ranges (void);
4374 void dump_vr_equiv (FILE *, bitmap
);
4375 void debug_vr_equiv (bitmap
);
4378 /* Dump value range VR to FILE. */
4381 dump_value_range (FILE *file
, value_range_t
*vr
)
4384 fprintf (file
, "[]");
4385 else if (vr
->type
== VR_UNDEFINED
)
4386 fprintf (file
, "UNDEFINED");
4387 else if (vr
->type
== VR_RANGE
|| vr
->type
== VR_ANTI_RANGE
)
4389 tree type
= TREE_TYPE (vr
->min
);
4391 fprintf (file
, "%s[", (vr
->type
== VR_ANTI_RANGE
) ? "~" : "");
4393 if (is_negative_overflow_infinity (vr
->min
))
4394 fprintf (file
, "-INF(OVF)");
4395 else if (INTEGRAL_TYPE_P (type
)
4396 && !TYPE_UNSIGNED (type
)
4397 && vrp_val_is_min (vr
->min
))
4398 fprintf (file
, "-INF");
4400 print_generic_expr (file
, vr
->min
, 0);
4402 fprintf (file
, ", ");
4404 if (is_positive_overflow_infinity (vr
->max
))
4405 fprintf (file
, "+INF(OVF)");
4406 else if (INTEGRAL_TYPE_P (type
)
4407 && vrp_val_is_max (vr
->max
))
4408 fprintf (file
, "+INF");
4410 print_generic_expr (file
, vr
->max
, 0);
4412 fprintf (file
, "]");
4419 fprintf (file
, " EQUIVALENCES: { ");
4421 EXECUTE_IF_SET_IN_BITMAP (vr
->equiv
, 0, i
, bi
)
4423 print_generic_expr (file
, ssa_name (i
), 0);
4424 fprintf (file
, " ");
4428 fprintf (file
, "} (%u elements)", c
);
4431 else if (vr
->type
== VR_VARYING
)
4432 fprintf (file
, "VARYING");
4434 fprintf (file
, "INVALID RANGE");
4438 /* Dump value range VR to stderr. */
4441 debug_value_range (value_range_t
*vr
)
4443 dump_value_range (stderr
, vr
);
4444 fprintf (stderr
, "\n");
4448 /* Dump value ranges of all SSA_NAMEs to FILE. */
4451 dump_all_value_ranges (FILE *file
)
4455 for (i
= 0; i
< num_vr_values
; i
++)
4459 print_generic_expr (file
, ssa_name (i
), 0);
4460 fprintf (file
, ": ");
4461 dump_value_range (file
, vr_value
[i
]);
4462 fprintf (file
, "\n");
4466 fprintf (file
, "\n");
4470 /* Dump all value ranges to stderr. */
4473 debug_all_value_ranges (void)
4475 dump_all_value_ranges (stderr
);
4479 /* Given a COND_EXPR COND of the form 'V OP W', and an SSA name V,
4480 create a new SSA name N and return the assertion assignment
4481 'V = ASSERT_EXPR <V, V OP W>'. */
4484 build_assert_expr_for (tree cond
, tree v
)
4489 gcc_assert (TREE_CODE (v
) == SSA_NAME
4490 && COMPARISON_CLASS_P (cond
));
4492 a
= build2 (ASSERT_EXPR
, TREE_TYPE (v
), v
, cond
);
4493 assertion
= gimple_build_assign (NULL_TREE
, a
);
4495 /* The new ASSERT_EXPR, creates a new SSA name that replaces the
4496 operand of the ASSERT_EXPR. Create it so the new name and the old one
4497 are registered in the replacement table so that we can fix the SSA web
4498 after adding all the ASSERT_EXPRs. */
4499 create_new_def_for (v
, assertion
, NULL
);
4505 /* Return false if EXPR is a predicate expression involving floating
4509 fp_predicate (gimple stmt
)
4511 GIMPLE_CHECK (stmt
, GIMPLE_COND
);
4513 return FLOAT_TYPE_P (TREE_TYPE (gimple_cond_lhs (stmt
)));
4516 /* If the range of values taken by OP can be inferred after STMT executes,
4517 return the comparison code (COMP_CODE_P) and value (VAL_P) that
4518 describes the inferred range. Return true if a range could be
4522 infer_value_range (gimple stmt
, tree op
, enum tree_code
*comp_code_p
, tree
*val_p
)
4525 *comp_code_p
= ERROR_MARK
;
4527 /* Do not attempt to infer anything in names that flow through
4529 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op
))
4532 /* Similarly, don't infer anything from statements that may throw
4533 exceptions. ??? Relax this requirement? */
4534 if (stmt_could_throw_p (stmt
))
4537 /* If STMT is the last statement of a basic block with no
4538 successors, there is no point inferring anything about any of its
4539 operands. We would not be able to find a proper insertion point
4540 for the assertion, anyway. */
4541 if (stmt_ends_bb_p (stmt
) && EDGE_COUNT (gimple_bb (stmt
)->succs
) == 0)
4544 if (infer_nonnull_range (stmt
, op
, true, true))
4546 *val_p
= build_int_cst (TREE_TYPE (op
), 0);
4547 *comp_code_p
= NE_EXPR
;
4555 void dump_asserts_for (FILE *, tree
);
4556 void debug_asserts_for (tree
);
4557 void dump_all_asserts (FILE *);
4558 void debug_all_asserts (void);
4560 /* Dump all the registered assertions for NAME to FILE. */
4563 dump_asserts_for (FILE *file
, tree name
)
4567 fprintf (file
, "Assertions to be inserted for ");
4568 print_generic_expr (file
, name
, 0);
4569 fprintf (file
, "\n");
4571 loc
= asserts_for
[SSA_NAME_VERSION (name
)];
4574 fprintf (file
, "\t");
4575 print_gimple_stmt (file
, gsi_stmt (loc
->si
), 0, 0);
4576 fprintf (file
, "\n\tBB #%d", loc
->bb
->index
);
4579 fprintf (file
, "\n\tEDGE %d->%d", loc
->e
->src
->index
,
4580 loc
->e
->dest
->index
);
4581 dump_edge_info (file
, loc
->e
, dump_flags
, 0);
4583 fprintf (file
, "\n\tPREDICATE: ");
4584 print_generic_expr (file
, name
, 0);
4585 fprintf (file
, " %s ", get_tree_code_name (loc
->comp_code
));
4586 print_generic_expr (file
, loc
->val
, 0);
4587 fprintf (file
, "\n\n");
4591 fprintf (file
, "\n");
4595 /* Dump all the registered assertions for NAME to stderr. */
4598 debug_asserts_for (tree name
)
4600 dump_asserts_for (stderr
, name
);
4604 /* Dump all the registered assertions for all the names to FILE. */
4607 dump_all_asserts (FILE *file
)
4612 fprintf (file
, "\nASSERT_EXPRs to be inserted\n\n");
4613 EXECUTE_IF_SET_IN_BITMAP (need_assert_for
, 0, i
, bi
)
4614 dump_asserts_for (file
, ssa_name (i
));
4615 fprintf (file
, "\n");
4619 /* Dump all the registered assertions for all the names to stderr. */
4622 debug_all_asserts (void)
4624 dump_all_asserts (stderr
);
4628 /* If NAME doesn't have an ASSERT_EXPR registered for asserting
4629 'EXPR COMP_CODE VAL' at a location that dominates block BB or
4630 E->DEST, then register this location as a possible insertion point
4631 for ASSERT_EXPR <NAME, EXPR COMP_CODE VAL>.
4633 BB, E and SI provide the exact insertion point for the new
4634 ASSERT_EXPR. If BB is NULL, then the ASSERT_EXPR is to be inserted
4635 on edge E. Otherwise, if E is NULL, the ASSERT_EXPR is inserted on
4636 BB. If SI points to a COND_EXPR or a SWITCH_EXPR statement, then E
4637 must not be NULL. */
4640 register_new_assert_for (tree name
, tree expr
,
4641 enum tree_code comp_code
,
4645 gimple_stmt_iterator si
)
4647 assert_locus_t n
, loc
, last_loc
;
4648 basic_block dest_bb
;
4650 gcc_checking_assert (bb
== NULL
|| e
== NULL
);
4653 gcc_checking_assert (gimple_code (gsi_stmt (si
)) != GIMPLE_COND
4654 && gimple_code (gsi_stmt (si
)) != GIMPLE_SWITCH
);
4656 /* Never build an assert comparing against an integer constant with
4657 TREE_OVERFLOW set. This confuses our undefined overflow warning
4659 if (TREE_OVERFLOW_P (val
))
4660 val
= drop_tree_overflow (val
);
4662 /* The new assertion A will be inserted at BB or E. We need to
4663 determine if the new location is dominated by a previously
4664 registered location for A. If we are doing an edge insertion,
4665 assume that A will be inserted at E->DEST. Note that this is not
4668 If E is a critical edge, it will be split. But even if E is
4669 split, the new block will dominate the same set of blocks that
4672 The reverse, however, is not true, blocks dominated by E->DEST
4673 will not be dominated by the new block created to split E. So,
4674 if the insertion location is on a critical edge, we will not use
4675 the new location to move another assertion previously registered
4676 at a block dominated by E->DEST. */
4677 dest_bb
= (bb
) ? bb
: e
->dest
;
4679 /* If NAME already has an ASSERT_EXPR registered for COMP_CODE and
4680 VAL at a block dominating DEST_BB, then we don't need to insert a new
4681 one. Similarly, if the same assertion already exists at a block
4682 dominated by DEST_BB and the new location is not on a critical
4683 edge, then update the existing location for the assertion (i.e.,
4684 move the assertion up in the dominance tree).
4686 Note, this is implemented as a simple linked list because there
4687 should not be more than a handful of assertions registered per
4688 name. If this becomes a performance problem, a table hashed by
4689 COMP_CODE and VAL could be implemented. */
4690 loc
= asserts_for
[SSA_NAME_VERSION (name
)];
4694 if (loc
->comp_code
== comp_code
4696 || operand_equal_p (loc
->val
, val
, 0))
4697 && (loc
->expr
== expr
4698 || operand_equal_p (loc
->expr
, expr
, 0)))
4700 /* If E is not a critical edge and DEST_BB
4701 dominates the existing location for the assertion, move
4702 the assertion up in the dominance tree by updating its
4703 location information. */
4704 if ((e
== NULL
|| !EDGE_CRITICAL_P (e
))
4705 && dominated_by_p (CDI_DOMINATORS
, loc
->bb
, dest_bb
))
4714 /* Update the last node of the list and move to the next one. */
4719 /* If we didn't find an assertion already registered for
4720 NAME COMP_CODE VAL, add a new one at the end of the list of
4721 assertions associated with NAME. */
4722 n
= XNEW (struct assert_locus_d
);
4726 n
->comp_code
= comp_code
;
4734 asserts_for
[SSA_NAME_VERSION (name
)] = n
;
4736 bitmap_set_bit (need_assert_for
, SSA_NAME_VERSION (name
));
4739 /* (COND_OP0 COND_CODE COND_OP1) is a predicate which uses NAME.
4740 Extract a suitable test code and value and store them into *CODE_P and
4741 *VAL_P so the predicate is normalized to NAME *CODE_P *VAL_P.
4743 If no extraction was possible, return FALSE, otherwise return TRUE.
4745 If INVERT is true, then we invert the result stored into *CODE_P. */
4748 extract_code_and_val_from_cond_with_ops (tree name
, enum tree_code cond_code
,
4749 tree cond_op0
, tree cond_op1
,
4750 bool invert
, enum tree_code
*code_p
,
4753 enum tree_code comp_code
;
4756 /* Otherwise, we have a comparison of the form NAME COMP VAL
4757 or VAL COMP NAME. */
4758 if (name
== cond_op1
)
4760 /* If the predicate is of the form VAL COMP NAME, flip
4761 COMP around because we need to register NAME as the
4762 first operand in the predicate. */
4763 comp_code
= swap_tree_comparison (cond_code
);
4768 /* The comparison is of the form NAME COMP VAL, so the
4769 comparison code remains unchanged. */
4770 comp_code
= cond_code
;
4774 /* Invert the comparison code as necessary. */
4776 comp_code
= invert_tree_comparison (comp_code
, 0);
4778 /* VRP does not handle float types. */
4779 if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (val
)))
4782 /* Do not register always-false predicates.
4783 FIXME: this works around a limitation in fold() when dealing with
4784 enumerations. Given 'enum { N1, N2 } x;', fold will not
4785 fold 'if (x > N2)' to 'if (0)'. */
4786 if ((comp_code
== GT_EXPR
|| comp_code
== LT_EXPR
)
4787 && INTEGRAL_TYPE_P (TREE_TYPE (val
)))
4789 tree min
= TYPE_MIN_VALUE (TREE_TYPE (val
));
4790 tree max
= TYPE_MAX_VALUE (TREE_TYPE (val
));
4792 if (comp_code
== GT_EXPR
4794 || compare_values (val
, max
) == 0))
4797 if (comp_code
== LT_EXPR
4799 || compare_values (val
, min
) == 0))
4802 *code_p
= comp_code
;
4807 /* Find out smallest RES where RES > VAL && (RES & MASK) == RES, if any
4808 (otherwise return VAL). VAL and MASK must be zero-extended for
4809 precision PREC. If SGNBIT is non-zero, first xor VAL with SGNBIT
4810 (to transform signed values into unsigned) and at the end xor
4814 masked_increment (double_int val
, double_int mask
, double_int sgnbit
,
4817 double_int bit
= double_int_one
, res
;
4821 for (i
= 0; i
< prec
; i
++, bit
+= bit
)
4824 if ((res
& bit
).is_zero ())
4826 res
= bit
- double_int_one
;
4827 res
= (val
+ bit
).and_not (res
);
4830 return res
^ sgnbit
;
4832 return val
^ sgnbit
;
4835 /* Try to register an edge assertion for SSA name NAME on edge E for
4836 the condition COND contributing to the conditional jump pointed to by BSI.
4837 Invert the condition COND if INVERT is true.
4838 Return true if an assertion for NAME could be registered. */
4841 register_edge_assert_for_2 (tree name
, edge e
, gimple_stmt_iterator bsi
,
4842 enum tree_code cond_code
,
4843 tree cond_op0
, tree cond_op1
, bool invert
)
4846 enum tree_code comp_code
;
4847 bool retval
= false;
4849 if (!extract_code_and_val_from_cond_with_ops (name
, cond_code
,
4852 invert
, &comp_code
, &val
))
4855 /* Only register an ASSERT_EXPR if NAME was found in the sub-graph
4856 reachable from E. */
4857 if (live_on_edge (e
, name
)
4858 && !has_single_use (name
))
4860 register_new_assert_for (name
, name
, comp_code
, val
, NULL
, e
, bsi
);
4864 /* In the case of NAME <= CST and NAME being defined as
4865 NAME = (unsigned) NAME2 + CST2 we can assert NAME2 >= -CST2
4866 and NAME2 <= CST - CST2. We can do the same for NAME > CST.
4867 This catches range and anti-range tests. */
4868 if ((comp_code
== LE_EXPR
4869 || comp_code
== GT_EXPR
)
4870 && TREE_CODE (val
) == INTEGER_CST
4871 && TYPE_UNSIGNED (TREE_TYPE (val
)))
4873 gimple def_stmt
= SSA_NAME_DEF_STMT (name
);
4874 tree cst2
= NULL_TREE
, name2
= NULL_TREE
, name3
= NULL_TREE
;
4876 /* Extract CST2 from the (optional) addition. */
4877 if (is_gimple_assign (def_stmt
)
4878 && gimple_assign_rhs_code (def_stmt
) == PLUS_EXPR
)
4880 name2
= gimple_assign_rhs1 (def_stmt
);
4881 cst2
= gimple_assign_rhs2 (def_stmt
);
4882 if (TREE_CODE (name2
) == SSA_NAME
4883 && TREE_CODE (cst2
) == INTEGER_CST
)
4884 def_stmt
= SSA_NAME_DEF_STMT (name2
);
4887 /* Extract NAME2 from the (optional) sign-changing cast. */
4888 if (gimple_assign_cast_p (def_stmt
))
4890 if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt
))
4891 && ! TYPE_UNSIGNED (TREE_TYPE (gimple_assign_rhs1 (def_stmt
)))
4892 && (TYPE_PRECISION (gimple_expr_type (def_stmt
))
4893 == TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (def_stmt
)))))
4894 name3
= gimple_assign_rhs1 (def_stmt
);
4897 /* If name3 is used later, create an ASSERT_EXPR for it. */
4898 if (name3
!= NULL_TREE
4899 && TREE_CODE (name3
) == SSA_NAME
4900 && (cst2
== NULL_TREE
4901 || TREE_CODE (cst2
) == INTEGER_CST
)
4902 && INTEGRAL_TYPE_P (TREE_TYPE (name3
))
4903 && live_on_edge (e
, name3
)
4904 && !has_single_use (name3
))
4908 /* Build an expression for the range test. */
4909 tmp
= build1 (NOP_EXPR
, TREE_TYPE (name
), name3
);
4910 if (cst2
!= NULL_TREE
)
4911 tmp
= build2 (PLUS_EXPR
, TREE_TYPE (name
), tmp
, cst2
);
4915 fprintf (dump_file
, "Adding assert for ");
4916 print_generic_expr (dump_file
, name3
, 0);
4917 fprintf (dump_file
, " from ");
4918 print_generic_expr (dump_file
, tmp
, 0);
4919 fprintf (dump_file
, "\n");
4922 register_new_assert_for (name3
, tmp
, comp_code
, val
, NULL
, e
, bsi
);
4927 /* If name2 is used later, create an ASSERT_EXPR for it. */
4928 if (name2
!= NULL_TREE
4929 && TREE_CODE (name2
) == SSA_NAME
4930 && TREE_CODE (cst2
) == INTEGER_CST
4931 && INTEGRAL_TYPE_P (TREE_TYPE (name2
))
4932 && live_on_edge (e
, name2
)
4933 && !has_single_use (name2
))
4937 /* Build an expression for the range test. */
4939 if (TREE_TYPE (name
) != TREE_TYPE (name2
))
4940 tmp
= build1 (NOP_EXPR
, TREE_TYPE (name
), tmp
);
4941 if (cst2
!= NULL_TREE
)
4942 tmp
= build2 (PLUS_EXPR
, TREE_TYPE (name
), tmp
, cst2
);
4946 fprintf (dump_file
, "Adding assert for ");
4947 print_generic_expr (dump_file
, name2
, 0);
4948 fprintf (dump_file
, " from ");
4949 print_generic_expr (dump_file
, tmp
, 0);
4950 fprintf (dump_file
, "\n");
4953 register_new_assert_for (name2
, tmp
, comp_code
, val
, NULL
, e
, bsi
);
4959 /* In the case of post-in/decrement tests like if (i++) ... and uses
4960 of the in/decremented value on the edge the extra name we want to
4961 assert for is not on the def chain of the name compared. Instead
4962 it is in the set of use stmts. */
4963 if ((comp_code
== NE_EXPR
4964 || comp_code
== EQ_EXPR
)
4965 && TREE_CODE (val
) == INTEGER_CST
)
4967 imm_use_iterator ui
;
4969 FOR_EACH_IMM_USE_STMT (use_stmt
, ui
, name
)
4971 /* Cut off to use-stmts that are in the predecessor. */
4972 if (gimple_bb (use_stmt
) != e
->src
)
4975 if (!is_gimple_assign (use_stmt
))
4978 enum tree_code code
= gimple_assign_rhs_code (use_stmt
);
4979 if (code
!= PLUS_EXPR
4980 && code
!= MINUS_EXPR
)
4983 tree cst
= gimple_assign_rhs2 (use_stmt
);
4984 if (TREE_CODE (cst
) != INTEGER_CST
)
4987 tree name2
= gimple_assign_lhs (use_stmt
);
4988 if (live_on_edge (e
, name2
))
4990 cst
= int_const_binop (code
, val
, cst
);
4991 register_new_assert_for (name2
, name2
, comp_code
, cst
,
4998 if (TREE_CODE_CLASS (comp_code
) == tcc_comparison
4999 && TREE_CODE (val
) == INTEGER_CST
)
5001 gimple def_stmt
= SSA_NAME_DEF_STMT (name
);
5002 tree name2
= NULL_TREE
, names
[2], cst2
= NULL_TREE
;
5003 tree val2
= NULL_TREE
;
5004 double_int mask
= double_int_zero
;
5005 unsigned int prec
= TYPE_PRECISION (TREE_TYPE (val
));
5006 unsigned int nprec
= prec
;
5007 enum tree_code rhs_code
= ERROR_MARK
;
5009 if (is_gimple_assign (def_stmt
))
5010 rhs_code
= gimple_assign_rhs_code (def_stmt
);
5012 /* Add asserts for NAME cmp CST and NAME being defined
5013 as NAME = (int) NAME2. */
5014 if (!TYPE_UNSIGNED (TREE_TYPE (val
))
5015 && (comp_code
== LE_EXPR
|| comp_code
== LT_EXPR
5016 || comp_code
== GT_EXPR
|| comp_code
== GE_EXPR
)
5017 && gimple_assign_cast_p (def_stmt
))
5019 name2
= gimple_assign_rhs1 (def_stmt
);
5020 if (CONVERT_EXPR_CODE_P (rhs_code
)
5021 && INTEGRAL_TYPE_P (TREE_TYPE (name2
))
5022 && TYPE_UNSIGNED (TREE_TYPE (name2
))
5023 && prec
== TYPE_PRECISION (TREE_TYPE (name2
))
5024 && (comp_code
== LE_EXPR
|| comp_code
== GT_EXPR
5025 || !tree_int_cst_equal (val
,
5026 TYPE_MIN_VALUE (TREE_TYPE (val
))))
5027 && live_on_edge (e
, name2
)
5028 && !has_single_use (name2
))
5031 enum tree_code new_comp_code
= comp_code
;
5033 cst
= fold_convert (TREE_TYPE (name2
),
5034 TYPE_MIN_VALUE (TREE_TYPE (val
)));
5035 /* Build an expression for the range test. */
5036 tmp
= build2 (PLUS_EXPR
, TREE_TYPE (name2
), name2
, cst
);
5037 cst
= fold_build2 (PLUS_EXPR
, TREE_TYPE (name2
), cst
,
5038 fold_convert (TREE_TYPE (name2
), val
));
5039 if (comp_code
== LT_EXPR
|| comp_code
== GE_EXPR
)
5041 new_comp_code
= comp_code
== LT_EXPR
? LE_EXPR
: GT_EXPR
;
5042 cst
= fold_build2 (MINUS_EXPR
, TREE_TYPE (name2
), cst
,
5043 build_int_cst (TREE_TYPE (name2
), 1));
5048 fprintf (dump_file
, "Adding assert for ");
5049 print_generic_expr (dump_file
, name2
, 0);
5050 fprintf (dump_file
, " from ");
5051 print_generic_expr (dump_file
, tmp
, 0);
5052 fprintf (dump_file
, "\n");
5055 register_new_assert_for (name2
, tmp
, new_comp_code
, cst
, NULL
,
5062 /* Add asserts for NAME cmp CST and NAME being defined as
5063 NAME = NAME2 >> CST2.
5065 Extract CST2 from the right shift. */
5066 if (rhs_code
== RSHIFT_EXPR
)
5068 name2
= gimple_assign_rhs1 (def_stmt
);
5069 cst2
= gimple_assign_rhs2 (def_stmt
);
5070 if (TREE_CODE (name2
) == SSA_NAME
5071 && tree_fits_uhwi_p (cst2
)
5072 && INTEGRAL_TYPE_P (TREE_TYPE (name2
))
5073 && IN_RANGE (tree_to_uhwi (cst2
), 1, prec
- 1)
5074 && prec
<= HOST_BITS_PER_DOUBLE_INT
5075 && prec
== GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (val
)))
5076 && live_on_edge (e
, name2
)
5077 && !has_single_use (name2
))
5079 mask
= double_int::mask (tree_to_uhwi (cst2
));
5080 val2
= fold_binary (LSHIFT_EXPR
, TREE_TYPE (val
), val
, cst2
);
5083 if (val2
!= NULL_TREE
5084 && TREE_CODE (val2
) == INTEGER_CST
5085 && simple_cst_equal (fold_build2 (RSHIFT_EXPR
,
5089 enum tree_code new_comp_code
= comp_code
;
5093 if (comp_code
== EQ_EXPR
|| comp_code
== NE_EXPR
)
5095 if (!TYPE_UNSIGNED (TREE_TYPE (val
)))
5097 tree type
= build_nonstandard_integer_type (prec
, 1);
5098 tmp
= build1 (NOP_EXPR
, type
, name2
);
5099 val2
= fold_convert (type
, val2
);
5101 tmp
= fold_build2 (MINUS_EXPR
, TREE_TYPE (tmp
), tmp
, val2
);
5102 new_val
= double_int_to_tree (TREE_TYPE (tmp
), mask
);
5103 new_comp_code
= comp_code
== EQ_EXPR
? LE_EXPR
: GT_EXPR
;
5105 else if (comp_code
== LT_EXPR
|| comp_code
== GE_EXPR
)
5108 = double_int::min_value (prec
, TYPE_UNSIGNED (TREE_TYPE (val
)));
5110 if (minval
== tree_to_double_int (new_val
))
5111 new_val
= NULL_TREE
;
5116 = double_int::max_value (prec
, TYPE_UNSIGNED (TREE_TYPE (val
)));
5117 mask
|= tree_to_double_int (val2
);
5119 new_val
= NULL_TREE
;
5121 new_val
= double_int_to_tree (TREE_TYPE (val2
), mask
);
5128 fprintf (dump_file
, "Adding assert for ");
5129 print_generic_expr (dump_file
, name2
, 0);
5130 fprintf (dump_file
, " from ");
5131 print_generic_expr (dump_file
, tmp
, 0);
5132 fprintf (dump_file
, "\n");
5135 register_new_assert_for (name2
, tmp
, new_comp_code
, new_val
,
5141 /* Add asserts for NAME cmp CST and NAME being defined as
5142 NAME = NAME2 & CST2.
5144 Extract CST2 from the and.
5147 NAME = (unsigned) NAME2;
5148 casts where NAME's type is unsigned and has smaller precision
5149 than NAME2's type as if it was NAME = NAME2 & MASK. */
5150 names
[0] = NULL_TREE
;
5151 names
[1] = NULL_TREE
;
5153 if (rhs_code
== BIT_AND_EXPR
5154 || (CONVERT_EXPR_CODE_P (rhs_code
)
5155 && TREE_CODE (TREE_TYPE (val
)) == INTEGER_TYPE
5156 && TYPE_UNSIGNED (TREE_TYPE (val
))
5157 && TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (def_stmt
)))
5161 name2
= gimple_assign_rhs1 (def_stmt
);
5162 if (rhs_code
== BIT_AND_EXPR
)
5163 cst2
= gimple_assign_rhs2 (def_stmt
);
5166 cst2
= TYPE_MAX_VALUE (TREE_TYPE (val
));
5167 nprec
= TYPE_PRECISION (TREE_TYPE (name2
));
5169 if (TREE_CODE (name2
) == SSA_NAME
5170 && INTEGRAL_TYPE_P (TREE_TYPE (name2
))
5171 && TREE_CODE (cst2
) == INTEGER_CST
5172 && !integer_zerop (cst2
)
5173 && nprec
<= HOST_BITS_PER_DOUBLE_INT
5175 || TYPE_UNSIGNED (TREE_TYPE (val
))))
5177 gimple def_stmt2
= SSA_NAME_DEF_STMT (name2
);
5178 if (gimple_assign_cast_p (def_stmt2
))
5180 names
[1] = gimple_assign_rhs1 (def_stmt2
);
5181 if (!CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt2
))
5182 || !INTEGRAL_TYPE_P (TREE_TYPE (names
[1]))
5183 || (TYPE_PRECISION (TREE_TYPE (name2
))
5184 != TYPE_PRECISION (TREE_TYPE (names
[1])))
5185 || !live_on_edge (e
, names
[1])
5186 || has_single_use (names
[1]))
5187 names
[1] = NULL_TREE
;
5189 if (live_on_edge (e
, name2
)
5190 && !has_single_use (name2
))
5194 if (names
[0] || names
[1])
5196 double_int minv
, maxv
= double_int_zero
, valv
, cst2v
;
5197 double_int tem
, sgnbit
;
5198 bool valid_p
= false, valn
= false, cst2n
= false;
5199 enum tree_code ccode
= comp_code
;
5201 valv
= tree_to_double_int (val
).zext (nprec
);
5202 cst2v
= tree_to_double_int (cst2
).zext (nprec
);
5203 if (!TYPE_UNSIGNED (TREE_TYPE (val
)))
5205 valn
= valv
.sext (nprec
).is_negative ();
5206 cst2n
= cst2v
.sext (nprec
).is_negative ();
5208 /* If CST2 doesn't have most significant bit set,
5209 but VAL is negative, we have comparison like
5210 if ((x & 0x123) > -4) (always true). Just give up. */
5214 sgnbit
= double_int_one
.llshift (nprec
- 1, nprec
).zext (nprec
);
5216 sgnbit
= double_int_zero
;
5217 minv
= valv
& cst2v
;
5221 /* Minimum unsigned value for equality is VAL & CST2
5222 (should be equal to VAL, otherwise we probably should
5223 have folded the comparison into false) and
5224 maximum unsigned value is VAL | ~CST2. */
5225 maxv
= valv
| ~cst2v
;
5226 maxv
= maxv
.zext (nprec
);
5230 tem
= valv
| ~cst2v
;
5231 tem
= tem
.zext (nprec
);
5232 /* If VAL is 0, handle (X & CST2) != 0 as (X & CST2) > 0U. */
5233 if (valv
.is_zero ())
5236 sgnbit
= double_int_zero
;
5239 /* If (VAL | ~CST2) is all ones, handle it as
5240 (X & CST2) < VAL. */
5241 if (tem
== double_int::mask (nprec
))
5245 sgnbit
= double_int_zero
;
5249 && cst2v
.sext (nprec
).is_negative ())
5251 = double_int_one
.llshift (nprec
- 1, nprec
).zext (nprec
);
5252 if (!sgnbit
.is_zero ())
5260 if (tem
== double_int::mask (nprec
- 1))
5266 sgnbit
= double_int_zero
;
5270 /* Minimum unsigned value for >= if (VAL & CST2) == VAL
5271 is VAL and maximum unsigned value is ~0. For signed
5272 comparison, if CST2 doesn't have most significant bit
5273 set, handle it similarly. If CST2 has MSB set,
5274 the minimum is the same, and maximum is ~0U/2. */
5277 /* If (VAL & CST2) != VAL, X & CST2 can't be equal to
5279 minv
= masked_increment (valv
, cst2v
, sgnbit
, nprec
);
5283 maxv
= double_int::mask (nprec
- (cst2n
? 1 : 0));
5288 /* Find out smallest MINV where MINV > VAL
5289 && (MINV & CST2) == MINV, if any. If VAL is signed and
5290 CST2 has MSB set, compute it biased by 1 << (nprec - 1). */
5291 minv
= masked_increment (valv
, cst2v
, sgnbit
, nprec
);
5294 maxv
= double_int::mask (nprec
- (cst2n
? 1 : 0));
5298 /* Minimum unsigned value for <= is 0 and maximum
5299 unsigned value is VAL | ~CST2 if (VAL & CST2) == VAL.
5300 Otherwise, find smallest VAL2 where VAL2 > VAL
5301 && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
5303 For signed comparison, if CST2 doesn't have most
5304 significant bit set, handle it similarly. If CST2 has
5305 MSB set, the maximum is the same and minimum is INT_MIN. */
5310 maxv
= masked_increment (valv
, cst2v
, sgnbit
, nprec
);
5313 maxv
-= double_int_one
;
5316 maxv
= maxv
.zext (nprec
);
5322 /* Minimum unsigned value for < is 0 and maximum
5323 unsigned value is (VAL-1) | ~CST2 if (VAL & CST2) == VAL.
5324 Otherwise, find smallest VAL2 where VAL2 > VAL
5325 && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
5327 For signed comparison, if CST2 doesn't have most
5328 significant bit set, handle it similarly. If CST2 has
5329 MSB set, the maximum is the same and minimum is INT_MIN. */
5338 maxv
= masked_increment (valv
, cst2v
, sgnbit
, nprec
);
5342 maxv
-= double_int_one
;
5344 maxv
= maxv
.zext (nprec
);
5352 && (maxv
- minv
).zext (nprec
) != double_int::mask (nprec
))
5354 tree tmp
, new_val
, type
;
5357 for (i
= 0; i
< 2; i
++)
5360 double_int maxv2
= maxv
;
5362 type
= TREE_TYPE (names
[i
]);
5363 if (!TYPE_UNSIGNED (type
))
5365 type
= build_nonstandard_integer_type (nprec
, 1);
5366 tmp
= build1 (NOP_EXPR
, type
, names
[i
]);
5368 if (!minv
.is_zero ())
5370 tmp
= build2 (PLUS_EXPR
, type
, tmp
,
5371 double_int_to_tree (type
, -minv
));
5372 maxv2
= maxv
- minv
;
5374 new_val
= double_int_to_tree (type
, maxv2
);
5378 fprintf (dump_file
, "Adding assert for ");
5379 print_generic_expr (dump_file
, names
[i
], 0);
5380 fprintf (dump_file
, " from ");
5381 print_generic_expr (dump_file
, tmp
, 0);
5382 fprintf (dump_file
, "\n");
5385 register_new_assert_for (names
[i
], tmp
, LE_EXPR
,
5386 new_val
, NULL
, e
, bsi
);
5396 /* OP is an operand of a truth value expression which is known to have
5397 a particular value. Register any asserts for OP and for any
5398 operands in OP's defining statement.
5400 If CODE is EQ_EXPR, then we want to register OP is zero (false),
5401 if CODE is NE_EXPR, then we want to register OP is nonzero (true). */
5404 register_edge_assert_for_1 (tree op
, enum tree_code code
,
5405 edge e
, gimple_stmt_iterator bsi
)
5407 bool retval
= false;
5410 enum tree_code rhs_code
;
5412 /* We only care about SSA_NAMEs. */
5413 if (TREE_CODE (op
) != SSA_NAME
)
5416 /* We know that OP will have a zero or nonzero value. If OP is used
5417 more than once go ahead and register an assert for OP.
5419 The FOUND_IN_SUBGRAPH support is not helpful in this situation as
5420 it will always be set for OP (because OP is used in a COND_EXPR in
5422 if (!has_single_use (op
))
5424 val
= build_int_cst (TREE_TYPE (op
), 0);
5425 register_new_assert_for (op
, op
, code
, val
, NULL
, e
, bsi
);
5429 /* Now look at how OP is set. If it's set from a comparison,
5430 a truth operation or some bit operations, then we may be able
5431 to register information about the operands of that assignment. */
5432 op_def
= SSA_NAME_DEF_STMT (op
);
5433 if (gimple_code (op_def
) != GIMPLE_ASSIGN
)
5436 rhs_code
= gimple_assign_rhs_code (op_def
);
5438 if (TREE_CODE_CLASS (rhs_code
) == tcc_comparison
)
5440 bool invert
= (code
== EQ_EXPR
? true : false);
5441 tree op0
= gimple_assign_rhs1 (op_def
);
5442 tree op1
= gimple_assign_rhs2 (op_def
);
5444 if (TREE_CODE (op0
) == SSA_NAME
)
5445 retval
|= register_edge_assert_for_2 (op0
, e
, bsi
, rhs_code
, op0
, op1
,
5447 if (TREE_CODE (op1
) == SSA_NAME
)
5448 retval
|= register_edge_assert_for_2 (op1
, e
, bsi
, rhs_code
, op0
, op1
,
5451 else if ((code
== NE_EXPR
5452 && gimple_assign_rhs_code (op_def
) == BIT_AND_EXPR
)
5454 && gimple_assign_rhs_code (op_def
) == BIT_IOR_EXPR
))
5456 /* Recurse on each operand. */
5457 tree op0
= gimple_assign_rhs1 (op_def
);
5458 tree op1
= gimple_assign_rhs2 (op_def
);
5459 if (TREE_CODE (op0
) == SSA_NAME
5460 && has_single_use (op0
))
5461 retval
|= register_edge_assert_for_1 (op0
, code
, e
, bsi
);
5462 if (TREE_CODE (op1
) == SSA_NAME
5463 && has_single_use (op1
))
5464 retval
|= register_edge_assert_for_1 (op1
, code
, e
, bsi
);
5466 else if (gimple_assign_rhs_code (op_def
) == BIT_NOT_EXPR
5467 && TYPE_PRECISION (TREE_TYPE (gimple_assign_lhs (op_def
))) == 1)
5469 /* Recurse, flipping CODE. */
5470 code
= invert_tree_comparison (code
, false);
5471 retval
|= register_edge_assert_for_1 (gimple_assign_rhs1 (op_def
),
5474 else if (gimple_assign_rhs_code (op_def
) == SSA_NAME
)
5476 /* Recurse through the copy. */
5477 retval
|= register_edge_assert_for_1 (gimple_assign_rhs1 (op_def
),
5480 else if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (op_def
)))
5482 /* Recurse through the type conversion, unless it is a narrowing
5483 conversion or conversion from non-integral type. */
5484 tree rhs
= gimple_assign_rhs1 (op_def
);
5485 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs
))
5486 && (TYPE_PRECISION (TREE_TYPE (rhs
))
5487 <= TYPE_PRECISION (TREE_TYPE (op
))))
5488 retval
|= register_edge_assert_for_1 (rhs
, code
, e
, bsi
);
5494 /* Try to register an edge assertion for SSA name NAME on edge E for
5495 the condition COND contributing to the conditional jump pointed to by SI.
5496 Return true if an assertion for NAME could be registered. */
5499 register_edge_assert_for (tree name
, edge e
, gimple_stmt_iterator si
,
5500 enum tree_code cond_code
, tree cond_op0
,
5504 enum tree_code comp_code
;
5505 bool retval
= false;
5506 bool is_else_edge
= (e
->flags
& EDGE_FALSE_VALUE
) != 0;
5508 /* Do not attempt to infer anything in names that flow through
5510 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name
))
5513 if (!extract_code_and_val_from_cond_with_ops (name
, cond_code
,
5519 /* Register ASSERT_EXPRs for name. */
5520 retval
|= register_edge_assert_for_2 (name
, e
, si
, cond_code
, cond_op0
,
5521 cond_op1
, is_else_edge
);
5524 /* If COND is effectively an equality test of an SSA_NAME against
5525 the value zero or one, then we may be able to assert values
5526 for SSA_NAMEs which flow into COND. */
5528 /* In the case of NAME == 1 or NAME != 0, for BIT_AND_EXPR defining
5529 statement of NAME we can assert both operands of the BIT_AND_EXPR
5530 have nonzero value. */
5531 if (((comp_code
== EQ_EXPR
&& integer_onep (val
))
5532 || (comp_code
== NE_EXPR
&& integer_zerop (val
))))
5534 gimple def_stmt
= SSA_NAME_DEF_STMT (name
);
5536 if (is_gimple_assign (def_stmt
)
5537 && gimple_assign_rhs_code (def_stmt
) == BIT_AND_EXPR
)
5539 tree op0
= gimple_assign_rhs1 (def_stmt
);
5540 tree op1
= gimple_assign_rhs2 (def_stmt
);
5541 retval
|= register_edge_assert_for_1 (op0
, NE_EXPR
, e
, si
);
5542 retval
|= register_edge_assert_for_1 (op1
, NE_EXPR
, e
, si
);
5546 /* In the case of NAME == 0 or NAME != 1, for BIT_IOR_EXPR defining
5547 statement of NAME we can assert both operands of the BIT_IOR_EXPR
5549 if (((comp_code
== EQ_EXPR
&& integer_zerop (val
))
5550 || (comp_code
== NE_EXPR
&& integer_onep (val
))))
5552 gimple def_stmt
= SSA_NAME_DEF_STMT (name
);
5554 /* For BIT_IOR_EXPR only if NAME == 0 both operands have
5555 necessarily zero value, or if type-precision is one. */
5556 if (is_gimple_assign (def_stmt
)
5557 && (gimple_assign_rhs_code (def_stmt
) == BIT_IOR_EXPR
5558 && (TYPE_PRECISION (TREE_TYPE (name
)) == 1
5559 || comp_code
== EQ_EXPR
)))
5561 tree op0
= gimple_assign_rhs1 (def_stmt
);
5562 tree op1
= gimple_assign_rhs2 (def_stmt
);
5563 retval
|= register_edge_assert_for_1 (op0
, EQ_EXPR
, e
, si
);
5564 retval
|= register_edge_assert_for_1 (op1
, EQ_EXPR
, e
, si
);
5572 /* Determine whether the outgoing edges of BB should receive an
5573 ASSERT_EXPR for each of the operands of BB's LAST statement.
5574 The last statement of BB must be a COND_EXPR.
5576 If any of the sub-graphs rooted at BB have an interesting use of
5577 the predicate operands, an assert location node is added to the
5578 list of assertions for the corresponding operands. */
5581 find_conditional_asserts (basic_block bb
, gimple last
)
5584 gimple_stmt_iterator bsi
;
5590 need_assert
= false;
5591 bsi
= gsi_for_stmt (last
);
5593 /* Look for uses of the operands in each of the sub-graphs
5594 rooted at BB. We need to check each of the outgoing edges
5595 separately, so that we know what kind of ASSERT_EXPR to
5597 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
5602 /* Register the necessary assertions for each operand in the
5603 conditional predicate. */
5604 FOR_EACH_SSA_TREE_OPERAND (op
, last
, iter
, SSA_OP_USE
)
5606 need_assert
|= register_edge_assert_for (op
, e
, bsi
,
5607 gimple_cond_code (last
),
5608 gimple_cond_lhs (last
),
5609 gimple_cond_rhs (last
));
5622 /* Compare two case labels sorting first by the destination bb index
5623 and then by the case value. */
5626 compare_case_labels (const void *p1
, const void *p2
)
5628 const struct case_info
*ci1
= (const struct case_info
*) p1
;
5629 const struct case_info
*ci2
= (const struct case_info
*) p2
;
5630 int idx1
= ci1
->bb
->index
;
5631 int idx2
= ci2
->bb
->index
;
5635 else if (idx1
== idx2
)
5637 /* Make sure the default label is first in a group. */
5638 if (!CASE_LOW (ci1
->expr
))
5640 else if (!CASE_LOW (ci2
->expr
))
5643 return tree_int_cst_compare (CASE_LOW (ci1
->expr
),
5644 CASE_LOW (ci2
->expr
));
5650 /* Determine whether the outgoing edges of BB should receive an
5651 ASSERT_EXPR for each of the operands of BB's LAST statement.
5652 The last statement of BB must be a SWITCH_EXPR.
5654 If any of the sub-graphs rooted at BB have an interesting use of
5655 the predicate operands, an assert location node is added to the
5656 list of assertions for the corresponding operands. */
5659 find_switch_asserts (basic_block bb
, gimple last
)
5662 gimple_stmt_iterator bsi
;
5665 struct case_info
*ci
;
5666 size_t n
= gimple_switch_num_labels (last
);
5667 #if GCC_VERSION >= 4000
5670 /* Work around GCC 3.4 bug (PR 37086). */
5671 volatile unsigned int idx
;
5674 need_assert
= false;
5675 bsi
= gsi_for_stmt (last
);
5676 op
= gimple_switch_index (last
);
5677 if (TREE_CODE (op
) != SSA_NAME
)
5680 /* Build a vector of case labels sorted by destination label. */
5681 ci
= XNEWVEC (struct case_info
, n
);
5682 for (idx
= 0; idx
< n
; ++idx
)
5684 ci
[idx
].expr
= gimple_switch_label (last
, idx
);
5685 ci
[idx
].bb
= label_to_block (CASE_LABEL (ci
[idx
].expr
));
5687 qsort (ci
, n
, sizeof (struct case_info
), compare_case_labels
);
5689 for (idx
= 0; idx
< n
; ++idx
)
5692 tree cl
= ci
[idx
].expr
;
5693 basic_block cbb
= ci
[idx
].bb
;
5695 min
= CASE_LOW (cl
);
5696 max
= CASE_HIGH (cl
);
5698 /* If there are multiple case labels with the same destination
5699 we need to combine them to a single value range for the edge. */
5700 if (idx
+ 1 < n
&& cbb
== ci
[idx
+ 1].bb
)
5702 /* Skip labels until the last of the group. */
5705 } while (idx
< n
&& cbb
== ci
[idx
].bb
);
5708 /* Pick up the maximum of the case label range. */
5709 if (CASE_HIGH (ci
[idx
].expr
))
5710 max
= CASE_HIGH (ci
[idx
].expr
);
5712 max
= CASE_LOW (ci
[idx
].expr
);
5715 /* Nothing to do if the range includes the default label until we
5716 can register anti-ranges. */
5717 if (min
== NULL_TREE
)
5720 /* Find the edge to register the assert expr on. */
5721 e
= find_edge (bb
, cbb
);
5723 /* Register the necessary assertions for the operand in the
5725 need_assert
|= register_edge_assert_for (op
, e
, bsi
,
5726 max
? GE_EXPR
: EQ_EXPR
,
5728 fold_convert (TREE_TYPE (op
),
5732 need_assert
|= register_edge_assert_for (op
, e
, bsi
, LE_EXPR
,
5734 fold_convert (TREE_TYPE (op
),
5744 /* Traverse all the statements in block BB looking for statements that
5745 may generate useful assertions for the SSA names in their operand.
5746 If a statement produces a useful assertion A for name N_i, then the
5747 list of assertions already generated for N_i is scanned to
5748 determine if A is actually needed.
5750 If N_i already had the assertion A at a location dominating the
5751 current location, then nothing needs to be done. Otherwise, the
5752 new location for A is recorded instead.
5754 1- For every statement S in BB, all the variables used by S are
5755 added to bitmap FOUND_IN_SUBGRAPH.
5757 2- If statement S uses an operand N in a way that exposes a known
5758 value range for N, then if N was not already generated by an
5759 ASSERT_EXPR, create a new assert location for N. For instance,
5760 if N is a pointer and the statement dereferences it, we can
5761 assume that N is not NULL.
5763 3- COND_EXPRs are a special case of #2. We can derive range
5764 information from the predicate but need to insert different
5765 ASSERT_EXPRs for each of the sub-graphs rooted at the
5766 conditional block. If the last statement of BB is a conditional
5767 expression of the form 'X op Y', then
5769 a) Remove X and Y from the set FOUND_IN_SUBGRAPH.
5771 b) If the conditional is the only entry point to the sub-graph
5772 corresponding to the THEN_CLAUSE, recurse into it. On
5773 return, if X and/or Y are marked in FOUND_IN_SUBGRAPH, then
5774 an ASSERT_EXPR is added for the corresponding variable.
5776 c) Repeat step (b) on the ELSE_CLAUSE.
5778 d) Mark X and Y in FOUND_IN_SUBGRAPH.
5787 In this case, an assertion on the THEN clause is useful to
5788 determine that 'a' is always 9 on that edge. However, an assertion
5789 on the ELSE clause would be unnecessary.
5791 4- If BB does not end in a conditional expression, then we recurse
5792 into BB's dominator children.
5794 At the end of the recursive traversal, every SSA name will have a
5795 list of locations where ASSERT_EXPRs should be added. When a new
5796 location for name N is found, it is registered by calling
5797 register_new_assert_for. That function keeps track of all the
5798 registered assertions to prevent adding unnecessary assertions.
5799 For instance, if a pointer P_4 is dereferenced more than once in a
5800 dominator tree, only the location dominating all the dereference of
5801 P_4 will receive an ASSERT_EXPR.
5803 If this function returns true, then it means that there are names
5804 for which we need to generate ASSERT_EXPRs. Those assertions are
5805 inserted by process_assert_insertions. */
5808 find_assert_locations_1 (basic_block bb
, sbitmap live
)
5810 gimple_stmt_iterator si
;
5814 need_assert
= false;
5815 last
= last_stmt (bb
);
5817 /* If BB's last statement is a conditional statement involving integer
5818 operands, determine if we need to add ASSERT_EXPRs. */
5820 && gimple_code (last
) == GIMPLE_COND
5821 && !fp_predicate (last
)
5822 && !ZERO_SSA_OPERANDS (last
, SSA_OP_USE
))
5823 need_assert
|= find_conditional_asserts (bb
, last
);
5825 /* If BB's last statement is a switch statement involving integer
5826 operands, determine if we need to add ASSERT_EXPRs. */
5828 && gimple_code (last
) == GIMPLE_SWITCH
5829 && !ZERO_SSA_OPERANDS (last
, SSA_OP_USE
))
5830 need_assert
|= find_switch_asserts (bb
, last
);
5832 /* Traverse all the statements in BB marking used names and looking
5833 for statements that may infer assertions for their used operands. */
5834 for (si
= gsi_last_bb (bb
); !gsi_end_p (si
); gsi_prev (&si
))
5840 stmt
= gsi_stmt (si
);
5842 if (is_gimple_debug (stmt
))
5845 /* See if we can derive an assertion for any of STMT's operands. */
5846 FOR_EACH_SSA_TREE_OPERAND (op
, stmt
, i
, SSA_OP_USE
)
5849 enum tree_code comp_code
;
5851 /* If op is not live beyond this stmt, do not bother to insert
5853 if (!bitmap_bit_p (live
, SSA_NAME_VERSION (op
)))
5856 /* If OP is used in such a way that we can infer a value
5857 range for it, and we don't find a previous assertion for
5858 it, create a new assertion location node for OP. */
5859 if (infer_value_range (stmt
, op
, &comp_code
, &value
))
5861 /* If we are able to infer a nonzero value range for OP,
5862 then walk backwards through the use-def chain to see if OP
5863 was set via a typecast.
5865 If so, then we can also infer a nonzero value range
5866 for the operand of the NOP_EXPR. */
5867 if (comp_code
== NE_EXPR
&& integer_zerop (value
))
5870 gimple def_stmt
= SSA_NAME_DEF_STMT (t
);
5872 while (is_gimple_assign (def_stmt
)
5873 && gimple_assign_rhs_code (def_stmt
) == NOP_EXPR
5875 (gimple_assign_rhs1 (def_stmt
)) == SSA_NAME
5877 (TREE_TYPE (gimple_assign_rhs1 (def_stmt
))))
5879 t
= gimple_assign_rhs1 (def_stmt
);
5880 def_stmt
= SSA_NAME_DEF_STMT (t
);
5882 /* Note we want to register the assert for the
5883 operand of the NOP_EXPR after SI, not after the
5885 if (! has_single_use (t
))
5887 register_new_assert_for (t
, t
, comp_code
, value
,
5894 register_new_assert_for (op
, op
, comp_code
, value
, bb
, NULL
, si
);
5900 FOR_EACH_SSA_TREE_OPERAND (op
, stmt
, i
, SSA_OP_USE
)
5901 bitmap_set_bit (live
, SSA_NAME_VERSION (op
));
5902 FOR_EACH_SSA_TREE_OPERAND (op
, stmt
, i
, SSA_OP_DEF
)
5903 bitmap_clear_bit (live
, SSA_NAME_VERSION (op
));
5906 /* Traverse all PHI nodes in BB, updating live. */
5907 for (si
= gsi_start_phis (bb
); !gsi_end_p (si
); gsi_next (&si
))
5909 use_operand_p arg_p
;
5911 gimple phi
= gsi_stmt (si
);
5912 tree res
= gimple_phi_result (phi
);
5914 if (virtual_operand_p (res
))
5917 FOR_EACH_PHI_ARG (arg_p
, phi
, i
, SSA_OP_USE
)
5919 tree arg
= USE_FROM_PTR (arg_p
);
5920 if (TREE_CODE (arg
) == SSA_NAME
)
5921 bitmap_set_bit (live
, SSA_NAME_VERSION (arg
));
5924 bitmap_clear_bit (live
, SSA_NAME_VERSION (res
));
5930 /* Do an RPO walk over the function computing SSA name liveness
5931 on-the-fly and deciding on assert expressions to insert.
5932 Returns true if there are assert expressions to be inserted. */
5935 find_assert_locations (void)
5937 int *rpo
= XNEWVEC (int, last_basic_block
);
5938 int *bb_rpo
= XNEWVEC (int, last_basic_block
);
5939 int *last_rpo
= XCNEWVEC (int, last_basic_block
);
5943 live
= XCNEWVEC (sbitmap
, last_basic_block
);
5944 rpo_cnt
= pre_and_rev_post_order_compute (NULL
, rpo
, false);
5945 for (i
= 0; i
< rpo_cnt
; ++i
)
5948 /* Pre-seed loop latch liveness from loop header PHI nodes. Due to
5949 the order we compute liveness and insert asserts we otherwise
5950 fail to insert asserts into the loop latch. */
5952 FOR_EACH_LOOP (loop
, 0)
5954 i
= loop
->latch
->index
;
5955 unsigned int j
= single_succ_edge (loop
->latch
)->dest_idx
;
5956 for (gimple_stmt_iterator gsi
= gsi_start_phis (loop
->header
);
5957 !gsi_end_p (gsi
); gsi_next (&gsi
))
5959 gimple phi
= gsi_stmt (gsi
);
5960 if (virtual_operand_p (gimple_phi_result (phi
)))
5962 tree arg
= gimple_phi_arg_def (phi
, j
);
5963 if (TREE_CODE (arg
) == SSA_NAME
)
5965 if (live
[i
] == NULL
)
5967 live
[i
] = sbitmap_alloc (num_ssa_names
);
5968 bitmap_clear (live
[i
]);
5970 bitmap_set_bit (live
[i
], SSA_NAME_VERSION (arg
));
5975 need_asserts
= false;
5976 for (i
= rpo_cnt
- 1; i
>= 0; --i
)
5978 basic_block bb
= BASIC_BLOCK_FOR_FN (cfun
, rpo
[i
]);
5984 live
[rpo
[i
]] = sbitmap_alloc (num_ssa_names
);
5985 bitmap_clear (live
[rpo
[i
]]);
5988 /* Process BB and update the live information with uses in
5990 need_asserts
|= find_assert_locations_1 (bb
, live
[rpo
[i
]]);
5992 /* Merge liveness into the predecessor blocks and free it. */
5993 if (!bitmap_empty_p (live
[rpo
[i
]]))
5996 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
5998 int pred
= e
->src
->index
;
5999 if ((e
->flags
& EDGE_DFS_BACK
) || pred
== ENTRY_BLOCK
)
6004 live
[pred
] = sbitmap_alloc (num_ssa_names
);
6005 bitmap_clear (live
[pred
]);
6007 bitmap_ior (live
[pred
], live
[pred
], live
[rpo
[i
]]);
6009 if (bb_rpo
[pred
] < pred_rpo
)
6010 pred_rpo
= bb_rpo
[pred
];
6013 /* Record the RPO number of the last visited block that needs
6014 live information from this block. */
6015 last_rpo
[rpo
[i
]] = pred_rpo
;
6019 sbitmap_free (live
[rpo
[i
]]);
6020 live
[rpo
[i
]] = NULL
;
6023 /* We can free all successors live bitmaps if all their
6024 predecessors have been visited already. */
6025 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
6026 if (last_rpo
[e
->dest
->index
] == i
6027 && live
[e
->dest
->index
])
6029 sbitmap_free (live
[e
->dest
->index
]);
6030 live
[e
->dest
->index
] = NULL
;
6035 XDELETEVEC (bb_rpo
);
6036 XDELETEVEC (last_rpo
);
6037 for (i
= 0; i
< last_basic_block
; ++i
)
6039 sbitmap_free (live
[i
]);
6042 return need_asserts
;
6045 /* Create an ASSERT_EXPR for NAME and insert it in the location
6046 indicated by LOC. Return true if we made any edge insertions. */
6049 process_assert_insertions_for (tree name
, assert_locus_t loc
)
6051 /* Build the comparison expression NAME_i COMP_CODE VAL. */
6058 /* If we have X <=> X do not insert an assert expr for that. */
6059 if (loc
->expr
== loc
->val
)
6062 cond
= build2 (loc
->comp_code
, boolean_type_node
, loc
->expr
, loc
->val
);
6063 assert_stmt
= build_assert_expr_for (cond
, name
);
6066 /* We have been asked to insert the assertion on an edge. This
6067 is used only by COND_EXPR and SWITCH_EXPR assertions. */
6068 gcc_checking_assert (gimple_code (gsi_stmt (loc
->si
)) == GIMPLE_COND
6069 || (gimple_code (gsi_stmt (loc
->si
))
6072 gsi_insert_on_edge (loc
->e
, assert_stmt
);
6076 /* Otherwise, we can insert right after LOC->SI iff the
6077 statement must not be the last statement in the block. */
6078 stmt
= gsi_stmt (loc
->si
);
6079 if (!stmt_ends_bb_p (stmt
))
6081 gsi_insert_after (&loc
->si
, assert_stmt
, GSI_SAME_STMT
);
6085 /* If STMT must be the last statement in BB, we can only insert new
6086 assertions on the non-abnormal edge out of BB. Note that since
6087 STMT is not control flow, there may only be one non-abnormal edge
6089 FOR_EACH_EDGE (e
, ei
, loc
->bb
->succs
)
6090 if (!(e
->flags
& EDGE_ABNORMAL
))
6092 gsi_insert_on_edge (e
, assert_stmt
);
6100 /* Process all the insertions registered for every name N_i registered
6101 in NEED_ASSERT_FOR. The list of assertions to be inserted are
6102 found in ASSERTS_FOR[i]. */
6105 process_assert_insertions (void)
6109 bool update_edges_p
= false;
6110 int num_asserts
= 0;
6112 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6113 dump_all_asserts (dump_file
);
6115 EXECUTE_IF_SET_IN_BITMAP (need_assert_for
, 0, i
, bi
)
6117 assert_locus_t loc
= asserts_for
[i
];
6122 assert_locus_t next
= loc
->next
;
6123 update_edges_p
|= process_assert_insertions_for (ssa_name (i
), loc
);
6131 gsi_commit_edge_inserts ();
6133 statistics_counter_event (cfun
, "Number of ASSERT_EXPR expressions inserted",
6138 /* Traverse the flowgraph looking for conditional jumps to insert range
6139 expressions. These range expressions are meant to provide information
6140 to optimizations that need to reason in terms of value ranges. They
6141 will not be expanded into RTL. For instance, given:
6150 this pass will transform the code into:
6156 x = ASSERT_EXPR <x, x < y>
6161 y = ASSERT_EXPR <y, x <= y>
6165 The idea is that once copy and constant propagation have run, other
6166 optimizations will be able to determine what ranges of values can 'x'
6167 take in different paths of the code, simply by checking the reaching
6168 definition of 'x'. */
6171 insert_range_assertions (void)
6173 need_assert_for
= BITMAP_ALLOC (NULL
);
6174 asserts_for
= XCNEWVEC (assert_locus_t
, num_ssa_names
);
6176 calculate_dominance_info (CDI_DOMINATORS
);
6178 if (find_assert_locations ())
6180 process_assert_insertions ();
6181 update_ssa (TODO_update_ssa_no_phi
);
6184 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6186 fprintf (dump_file
, "\nSSA form after inserting ASSERT_EXPRs\n");
6187 dump_function_to_file (current_function_decl
, dump_file
, dump_flags
);
6191 BITMAP_FREE (need_assert_for
);
6194 /* Checks one ARRAY_REF in REF, located at LOCUS. Ignores flexible arrays
6195 and "struct" hacks. If VRP can determine that the
6196 array subscript is a constant, check if it is outside valid
6197 range. If the array subscript is a RANGE, warn if it is
6198 non-overlapping with valid range.
6199 IGNORE_OFF_BY_ONE is true if the ARRAY_REF is inside a ADDR_EXPR. */
6202 check_array_ref (location_t location
, tree ref
, bool ignore_off_by_one
)
6204 value_range_t
* vr
= NULL
;
6205 tree low_sub
, up_sub
;
6206 tree low_bound
, up_bound
, up_bound_p1
;
6209 if (TREE_NO_WARNING (ref
))
6212 low_sub
= up_sub
= TREE_OPERAND (ref
, 1);
6213 up_bound
= array_ref_up_bound (ref
);
6215 /* Can not check flexible arrays. */
6217 || TREE_CODE (up_bound
) != INTEGER_CST
)
6220 /* Accesses to trailing arrays via pointers may access storage
6221 beyond the types array bounds. */
6222 base
= get_base_address (ref
);
6223 if (base
&& TREE_CODE (base
) == MEM_REF
)
6225 tree cref
, next
= NULL_TREE
;
6227 if (TREE_CODE (TREE_OPERAND (ref
, 0)) != COMPONENT_REF
)
6230 cref
= TREE_OPERAND (ref
, 0);
6231 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (cref
, 0))) == RECORD_TYPE
)
6232 for (next
= DECL_CHAIN (TREE_OPERAND (cref
, 1));
6233 next
&& TREE_CODE (next
) != FIELD_DECL
;
6234 next
= DECL_CHAIN (next
))
6237 /* If this is the last field in a struct type or a field in a
6238 union type do not warn. */
6243 low_bound
= array_ref_low_bound (ref
);
6244 up_bound_p1
= int_const_binop (PLUS_EXPR
, up_bound
, integer_one_node
);
6246 if (TREE_CODE (low_sub
) == SSA_NAME
)
6248 vr
= get_value_range (low_sub
);
6249 if (vr
->type
== VR_RANGE
|| vr
->type
== VR_ANTI_RANGE
)
6251 low_sub
= vr
->type
== VR_RANGE
? vr
->max
: vr
->min
;
6252 up_sub
= vr
->type
== VR_RANGE
? vr
->min
: vr
->max
;
6256 if (vr
&& vr
->type
== VR_ANTI_RANGE
)
6258 if (TREE_CODE (up_sub
) == INTEGER_CST
6259 && tree_int_cst_lt (up_bound
, up_sub
)
6260 && TREE_CODE (low_sub
) == INTEGER_CST
6261 && tree_int_cst_lt (low_sub
, low_bound
))
6263 warning_at (location
, OPT_Warray_bounds
,
6264 "array subscript is outside array bounds");
6265 TREE_NO_WARNING (ref
) = 1;
6268 else if (TREE_CODE (up_sub
) == INTEGER_CST
6269 && (ignore_off_by_one
6270 ? (tree_int_cst_lt (up_bound
, up_sub
)
6271 && !tree_int_cst_equal (up_bound_p1
, up_sub
))
6272 : (tree_int_cst_lt (up_bound
, up_sub
)
6273 || tree_int_cst_equal (up_bound_p1
, up_sub
))))
6275 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6277 fprintf (dump_file
, "Array bound warning for ");
6278 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, ref
);
6279 fprintf (dump_file
, "\n");
6281 warning_at (location
, OPT_Warray_bounds
,
6282 "array subscript is above array bounds");
6283 TREE_NO_WARNING (ref
) = 1;
6285 else if (TREE_CODE (low_sub
) == INTEGER_CST
6286 && tree_int_cst_lt (low_sub
, low_bound
))
6288 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6290 fprintf (dump_file
, "Array bound warning for ");
6291 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, ref
);
6292 fprintf (dump_file
, "\n");
6294 warning_at (location
, OPT_Warray_bounds
,
6295 "array subscript is below array bounds");
6296 TREE_NO_WARNING (ref
) = 1;
6300 /* Searches if the expr T, located at LOCATION computes
6301 address of an ARRAY_REF, and call check_array_ref on it. */
6304 search_for_addr_array (tree t
, location_t location
)
6306 while (TREE_CODE (t
) == SSA_NAME
)
6308 gimple g
= SSA_NAME_DEF_STMT (t
);
6310 if (gimple_code (g
) != GIMPLE_ASSIGN
)
6313 if (get_gimple_rhs_class (gimple_assign_rhs_code (g
))
6314 != GIMPLE_SINGLE_RHS
)
6317 t
= gimple_assign_rhs1 (g
);
6321 /* We are only interested in addresses of ARRAY_REF's. */
6322 if (TREE_CODE (t
) != ADDR_EXPR
)
6325 /* Check each ARRAY_REFs in the reference chain. */
6328 if (TREE_CODE (t
) == ARRAY_REF
)
6329 check_array_ref (location
, t
, true /*ignore_off_by_one*/);
6331 t
= TREE_OPERAND (t
, 0);
6333 while (handled_component_p (t
));
6335 if (TREE_CODE (t
) == MEM_REF
6336 && TREE_CODE (TREE_OPERAND (t
, 0)) == ADDR_EXPR
6337 && !TREE_NO_WARNING (t
))
6339 tree tem
= TREE_OPERAND (TREE_OPERAND (t
, 0), 0);
6340 tree low_bound
, up_bound
, el_sz
;
6342 if (TREE_CODE (TREE_TYPE (tem
)) != ARRAY_TYPE
6343 || TREE_CODE (TREE_TYPE (TREE_TYPE (tem
))) == ARRAY_TYPE
6344 || !TYPE_DOMAIN (TREE_TYPE (tem
)))
6347 low_bound
= TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (tem
)));
6348 up_bound
= TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (tem
)));
6349 el_sz
= TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (tem
)));
6351 || TREE_CODE (low_bound
) != INTEGER_CST
6353 || TREE_CODE (up_bound
) != INTEGER_CST
6355 || TREE_CODE (el_sz
) != INTEGER_CST
)
6358 idx
= mem_ref_offset (t
);
6359 idx
= idx
.sdiv (tree_to_double_int (el_sz
), TRUNC_DIV_EXPR
);
6360 if (idx
.slt (double_int_zero
))
6362 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6364 fprintf (dump_file
, "Array bound warning for ");
6365 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, t
);
6366 fprintf (dump_file
, "\n");
6368 warning_at (location
, OPT_Warray_bounds
,
6369 "array subscript is below array bounds");
6370 TREE_NO_WARNING (t
) = 1;
6372 else if (idx
.sgt (tree_to_double_int (up_bound
)
6373 - tree_to_double_int (low_bound
)
6376 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6378 fprintf (dump_file
, "Array bound warning for ");
6379 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, t
);
6380 fprintf (dump_file
, "\n");
6382 warning_at (location
, OPT_Warray_bounds
,
6383 "array subscript is above array bounds");
6384 TREE_NO_WARNING (t
) = 1;
6389 /* walk_tree() callback that checks if *TP is
6390 an ARRAY_REF inside an ADDR_EXPR (in which an array
6391 subscript one outside the valid range is allowed). Call
6392 check_array_ref for each ARRAY_REF found. The location is
6396 check_array_bounds (tree
*tp
, int *walk_subtree
, void *data
)
6399 struct walk_stmt_info
*wi
= (struct walk_stmt_info
*) data
;
6400 location_t location
;
6402 if (EXPR_HAS_LOCATION (t
))
6403 location
= EXPR_LOCATION (t
);
6406 location_t
*locp
= (location_t
*) wi
->info
;
6410 *walk_subtree
= TRUE
;
6412 if (TREE_CODE (t
) == ARRAY_REF
)
6413 check_array_ref (location
, t
, false /*ignore_off_by_one*/);
6415 if (TREE_CODE (t
) == MEM_REF
6416 || (TREE_CODE (t
) == RETURN_EXPR
&& TREE_OPERAND (t
, 0)))
6417 search_for_addr_array (TREE_OPERAND (t
, 0), location
);
6419 if (TREE_CODE (t
) == ADDR_EXPR
)
6420 *walk_subtree
= FALSE
;
6425 /* Walk over all statements of all reachable BBs and call check_array_bounds
6429 check_all_array_refs (void)
6432 gimple_stmt_iterator si
;
6438 bool executable
= false;
6440 /* Skip blocks that were found to be unreachable. */
6441 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
6442 executable
|= !!(e
->flags
& EDGE_EXECUTABLE
);
6446 for (si
= gsi_start_bb (bb
); !gsi_end_p (si
); gsi_next (&si
))
6448 gimple stmt
= gsi_stmt (si
);
6449 struct walk_stmt_info wi
;
6450 if (!gimple_has_location (stmt
))
6453 if (is_gimple_call (stmt
))
6456 size_t n
= gimple_call_num_args (stmt
);
6457 for (i
= 0; i
< n
; i
++)
6459 tree arg
= gimple_call_arg (stmt
, i
);
6460 search_for_addr_array (arg
, gimple_location (stmt
));
6465 memset (&wi
, 0, sizeof (wi
));
6466 wi
.info
= CONST_CAST (void *, (const void *)
6467 gimple_location_ptr (stmt
));
6469 walk_gimple_op (gsi_stmt (si
),
6477 /* Return true if all imm uses of VAR are either in STMT, or
6478 feed (optionally through a chain of single imm uses) GIMPLE_COND
6479 in basic block COND_BB. */
6482 all_imm_uses_in_stmt_or_feed_cond (tree var
, gimple stmt
, basic_block cond_bb
)
6484 use_operand_p use_p
, use2_p
;
6485 imm_use_iterator iter
;
6487 FOR_EACH_IMM_USE_FAST (use_p
, iter
, var
)
6488 if (USE_STMT (use_p
) != stmt
)
6490 gimple use_stmt
= USE_STMT (use_p
), use_stmt2
;
6491 if (is_gimple_debug (use_stmt
))
6493 while (is_gimple_assign (use_stmt
)
6494 && TREE_CODE (gimple_assign_lhs (use_stmt
)) == SSA_NAME
6495 && single_imm_use (gimple_assign_lhs (use_stmt
),
6496 &use2_p
, &use_stmt2
))
6497 use_stmt
= use_stmt2
;
6498 if (gimple_code (use_stmt
) != GIMPLE_COND
6499 || gimple_bb (use_stmt
) != cond_bb
)
6512 __builtin_unreachable ();
6514 x_5 = ASSERT_EXPR <x_3, ...>;
6515 If x_3 has no other immediate uses (checked by caller),
6516 var is the x_3 var from ASSERT_EXPR, we can clear low 5 bits
6517 from the non-zero bitmask. */
6520 maybe_set_nonzero_bits (basic_block bb
, tree var
)
6522 edge e
= single_pred_edge (bb
);
6523 basic_block cond_bb
= e
->src
;
6524 gimple stmt
= last_stmt (cond_bb
);
6528 || gimple_code (stmt
) != GIMPLE_COND
6529 || gimple_cond_code (stmt
) != ((e
->flags
& EDGE_TRUE_VALUE
)
6530 ? EQ_EXPR
: NE_EXPR
)
6531 || TREE_CODE (gimple_cond_lhs (stmt
)) != SSA_NAME
6532 || !integer_zerop (gimple_cond_rhs (stmt
)))
6535 stmt
= SSA_NAME_DEF_STMT (gimple_cond_lhs (stmt
));
6536 if (!is_gimple_assign (stmt
)
6537 || gimple_assign_rhs_code (stmt
) != BIT_AND_EXPR
6538 || TREE_CODE (gimple_assign_rhs2 (stmt
)) != INTEGER_CST
)
6540 if (gimple_assign_rhs1 (stmt
) != var
)
6544 if (TREE_CODE (gimple_assign_rhs1 (stmt
)) != SSA_NAME
)
6546 stmt2
= SSA_NAME_DEF_STMT (gimple_assign_rhs1 (stmt
));
6547 if (!gimple_assign_cast_p (stmt2
)
6548 || gimple_assign_rhs1 (stmt2
) != var
6549 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (stmt2
))
6550 || (TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (stmt
)))
6551 != TYPE_PRECISION (TREE_TYPE (var
))))
6554 cst
= gimple_assign_rhs2 (stmt
);
6555 set_nonzero_bits (var
, (get_nonzero_bits (var
)
6556 & ~tree_to_double_int (cst
)));
6559 /* Convert range assertion expressions into the implied copies and
6560 copy propagate away the copies. Doing the trivial copy propagation
6561 here avoids the need to run the full copy propagation pass after
6564 FIXME, this will eventually lead to copy propagation removing the
6565 names that had useful range information attached to them. For
6566 instance, if we had the assertion N_i = ASSERT_EXPR <N_j, N_j > 3>,
6567 then N_i will have the range [3, +INF].
6569 However, by converting the assertion into the implied copy
6570 operation N_i = N_j, we will then copy-propagate N_j into the uses
6571 of N_i and lose the range information. We may want to hold on to
6572 ASSERT_EXPRs a little while longer as the ranges could be used in
6573 things like jump threading.
6575 The problem with keeping ASSERT_EXPRs around is that passes after
6576 VRP need to handle them appropriately.
6578 Another approach would be to make the range information a first
6579 class property of the SSA_NAME so that it can be queried from
6580 any pass. This is made somewhat more complex by the need for
6581 multiple ranges to be associated with one SSA_NAME. */
6584 remove_range_assertions (void)
6587 gimple_stmt_iterator si
;
6588 /* 1 if looking at ASSERT_EXPRs immediately at the beginning of
6589 a basic block preceeded by GIMPLE_COND branching to it and
6590 __builtin_trap, -1 if not yet checked, 0 otherwise. */
6593 /* Note that the BSI iterator bump happens at the bottom of the
6594 loop and no bump is necessary if we're removing the statement
6595 referenced by the current BSI. */
6597 for (si
= gsi_after_labels (bb
), is_unreachable
= -1; !gsi_end_p (si
);)
6599 gimple stmt
= gsi_stmt (si
);
6602 if (is_gimple_assign (stmt
)
6603 && gimple_assign_rhs_code (stmt
) == ASSERT_EXPR
)
6605 tree lhs
= gimple_assign_lhs (stmt
);
6606 tree rhs
= gimple_assign_rhs1 (stmt
);
6608 tree cond
= fold (ASSERT_EXPR_COND (rhs
));
6609 use_operand_p use_p
;
6610 imm_use_iterator iter
;
6612 gcc_assert (cond
!= boolean_false_node
);
6614 var
= ASSERT_EXPR_VAR (rhs
);
6615 gcc_assert (TREE_CODE (var
) == SSA_NAME
);
6617 if (!POINTER_TYPE_P (TREE_TYPE (lhs
))
6618 && SSA_NAME_RANGE_INFO (lhs
))
6620 if (is_unreachable
== -1)
6623 if (single_pred_p (bb
)
6624 && assert_unreachable_fallthru_edge_p
6625 (single_pred_edge (bb
)))
6629 if (x_7 >= 10 && x_7 < 20)
6630 __builtin_unreachable ();
6631 x_8 = ASSERT_EXPR <x_7, ...>;
6632 if the only uses of x_7 are in the ASSERT_EXPR and
6633 in the condition. In that case, we can copy the
6634 range info from x_8 computed in this pass also
6637 && all_imm_uses_in_stmt_or_feed_cond (var
, stmt
,
6640 set_range_info (var
, SSA_NAME_RANGE_TYPE (lhs
),
6641 SSA_NAME_RANGE_INFO (lhs
)->min
,
6642 SSA_NAME_RANGE_INFO (lhs
)->max
);
6643 maybe_set_nonzero_bits (bb
, var
);
6647 /* Propagate the RHS into every use of the LHS. */
6648 FOR_EACH_IMM_USE_STMT (use_stmt
, iter
, lhs
)
6649 FOR_EACH_IMM_USE_ON_STMT (use_p
, iter
)
6650 SET_USE (use_p
, var
);
6652 /* And finally, remove the copy, it is not needed. */
6653 gsi_remove (&si
, true);
6654 release_defs (stmt
);
6665 /* Return true if STMT is interesting for VRP. */
6668 stmt_interesting_for_vrp (gimple stmt
)
6670 if (gimple_code (stmt
) == GIMPLE_PHI
)
6672 tree res
= gimple_phi_result (stmt
);
6673 return (!virtual_operand_p (res
)
6674 && (INTEGRAL_TYPE_P (TREE_TYPE (res
))
6675 || POINTER_TYPE_P (TREE_TYPE (res
))));
6677 else if (is_gimple_assign (stmt
) || is_gimple_call (stmt
))
6679 tree lhs
= gimple_get_lhs (stmt
);
6681 /* In general, assignments with virtual operands are not useful
6682 for deriving ranges, with the obvious exception of calls to
6683 builtin functions. */
6684 if (lhs
&& TREE_CODE (lhs
) == SSA_NAME
6685 && (INTEGRAL_TYPE_P (TREE_TYPE (lhs
))
6686 || POINTER_TYPE_P (TREE_TYPE (lhs
)))
6687 && (is_gimple_call (stmt
)
6688 || !gimple_vuse (stmt
)))
6691 else if (gimple_code (stmt
) == GIMPLE_COND
6692 || gimple_code (stmt
) == GIMPLE_SWITCH
)
6699 /* Initialize local data structures for VRP. */
6702 vrp_initialize (void)
6706 values_propagated
= false;
6707 num_vr_values
= num_ssa_names
;
6708 vr_value
= XCNEWVEC (value_range_t
*, num_vr_values
);
6709 vr_phi_edge_counts
= XCNEWVEC (int, num_ssa_names
);
6713 gimple_stmt_iterator si
;
6715 for (si
= gsi_start_phis (bb
); !gsi_end_p (si
); gsi_next (&si
))
6717 gimple phi
= gsi_stmt (si
);
6718 if (!stmt_interesting_for_vrp (phi
))
6720 tree lhs
= PHI_RESULT (phi
);
6721 set_value_range_to_varying (get_value_range (lhs
));
6722 prop_set_simulate_again (phi
, false);
6725 prop_set_simulate_again (phi
, true);
6728 for (si
= gsi_start_bb (bb
); !gsi_end_p (si
); gsi_next (&si
))
6730 gimple stmt
= gsi_stmt (si
);
6732 /* If the statement is a control insn, then we do not
6733 want to avoid simulating the statement once. Failure
6734 to do so means that those edges will never get added. */
6735 if (stmt_ends_bb_p (stmt
))
6736 prop_set_simulate_again (stmt
, true);
6737 else if (!stmt_interesting_for_vrp (stmt
))
6741 FOR_EACH_SSA_TREE_OPERAND (def
, stmt
, i
, SSA_OP_DEF
)
6742 set_value_range_to_varying (get_value_range (def
));
6743 prop_set_simulate_again (stmt
, false);
6746 prop_set_simulate_again (stmt
, true);
6751 /* Return the singleton value-range for NAME or NAME. */
6754 vrp_valueize (tree name
)
6756 if (TREE_CODE (name
) == SSA_NAME
)
6758 value_range_t
*vr
= get_value_range (name
);
6759 if (vr
->type
== VR_RANGE
6760 && (vr
->min
== vr
->max
6761 || operand_equal_p (vr
->min
, vr
->max
, 0)))
6767 /* Visit assignment STMT. If it produces an interesting range, record
6768 the SSA name in *OUTPUT_P. */
6770 static enum ssa_prop_result
6771 vrp_visit_assignment_or_call (gimple stmt
, tree
*output_p
)
6775 enum gimple_code code
= gimple_code (stmt
);
6776 lhs
= gimple_get_lhs (stmt
);
6778 /* We only keep track of ranges in integral and pointer types. */
6779 if (TREE_CODE (lhs
) == SSA_NAME
6780 && ((INTEGRAL_TYPE_P (TREE_TYPE (lhs
))
6781 /* It is valid to have NULL MIN/MAX values on a type. See
6782 build_range_type. */
6783 && TYPE_MIN_VALUE (TREE_TYPE (lhs
))
6784 && TYPE_MAX_VALUE (TREE_TYPE (lhs
)))
6785 || POINTER_TYPE_P (TREE_TYPE (lhs
))))
6787 value_range_t new_vr
= VR_INITIALIZER
;
6789 /* Try folding the statement to a constant first. */
6790 tree tem
= gimple_fold_stmt_to_constant (stmt
, vrp_valueize
);
6792 set_value_range_to_value (&new_vr
, tem
, NULL
);
6793 /* Then dispatch to value-range extracting functions. */
6794 else if (code
== GIMPLE_CALL
)
6795 extract_range_basic (&new_vr
, stmt
);
6797 extract_range_from_assignment (&new_vr
, stmt
);
6799 if (update_value_range (lhs
, &new_vr
))
6803 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6805 fprintf (dump_file
, "Found new range for ");
6806 print_generic_expr (dump_file
, lhs
, 0);
6807 fprintf (dump_file
, ": ");
6808 dump_value_range (dump_file
, &new_vr
);
6809 fprintf (dump_file
, "\n\n");
6812 if (new_vr
.type
== VR_VARYING
)
6813 return SSA_PROP_VARYING
;
6815 return SSA_PROP_INTERESTING
;
6818 return SSA_PROP_NOT_INTERESTING
;
6821 /* Every other statement produces no useful ranges. */
6822 FOR_EACH_SSA_TREE_OPERAND (def
, stmt
, iter
, SSA_OP_DEF
)
6823 set_value_range_to_varying (get_value_range (def
));
6825 return SSA_PROP_VARYING
;
6828 /* Helper that gets the value range of the SSA_NAME with version I
6829 or a symbolic range containing the SSA_NAME only if the value range
6830 is varying or undefined. */
6832 static inline value_range_t
6833 get_vr_for_comparison (int i
)
6835 value_range_t vr
= *get_value_range (ssa_name (i
));
6837 /* If name N_i does not have a valid range, use N_i as its own
6838 range. This allows us to compare against names that may
6839 have N_i in their ranges. */
6840 if (vr
.type
== VR_VARYING
|| vr
.type
== VR_UNDEFINED
)
6843 vr
.min
= ssa_name (i
);
6844 vr
.max
= ssa_name (i
);
6850 /* Compare all the value ranges for names equivalent to VAR with VAL
6851 using comparison code COMP. Return the same value returned by
6852 compare_range_with_value, including the setting of
6853 *STRICT_OVERFLOW_P. */
6856 compare_name_with_value (enum tree_code comp
, tree var
, tree val
,
6857 bool *strict_overflow_p
)
6863 int used_strict_overflow
;
6865 value_range_t equiv_vr
;
6867 /* Get the set of equivalences for VAR. */
6868 e
= get_value_range (var
)->equiv
;
6870 /* Start at -1. Set it to 0 if we do a comparison without relying
6871 on overflow, or 1 if all comparisons rely on overflow. */
6872 used_strict_overflow
= -1;
6874 /* Compare vars' value range with val. */
6875 equiv_vr
= get_vr_for_comparison (SSA_NAME_VERSION (var
));
6877 retval
= compare_range_with_value (comp
, &equiv_vr
, val
, &sop
);
6879 used_strict_overflow
= sop
? 1 : 0;
6881 /* If the equiv set is empty we have done all work we need to do. */
6885 && used_strict_overflow
> 0)
6886 *strict_overflow_p
= true;
6890 EXECUTE_IF_SET_IN_BITMAP (e
, 0, i
, bi
)
6892 equiv_vr
= get_vr_for_comparison (i
);
6894 t
= compare_range_with_value (comp
, &equiv_vr
, val
, &sop
);
6897 /* If we get different answers from different members
6898 of the equivalence set this check must be in a dead
6899 code region. Folding it to a trap representation
6900 would be correct here. For now just return don't-know. */
6910 used_strict_overflow
= 0;
6911 else if (used_strict_overflow
< 0)
6912 used_strict_overflow
= 1;
6917 && used_strict_overflow
> 0)
6918 *strict_overflow_p
= true;
6924 /* Given a comparison code COMP and names N1 and N2, compare all the
6925 ranges equivalent to N1 against all the ranges equivalent to N2
6926 to determine the value of N1 COMP N2. Return the same value
6927 returned by compare_ranges. Set *STRICT_OVERFLOW_P to indicate
6928 whether we relied on an overflow infinity in the comparison. */
6932 compare_names (enum tree_code comp
, tree n1
, tree n2
,
6933 bool *strict_overflow_p
)
6937 bitmap_iterator bi1
, bi2
;
6939 int used_strict_overflow
;
6940 static bitmap_obstack
*s_obstack
= NULL
;
6941 static bitmap s_e1
= NULL
, s_e2
= NULL
;
6943 /* Compare the ranges of every name equivalent to N1 against the
6944 ranges of every name equivalent to N2. */
6945 e1
= get_value_range (n1
)->equiv
;
6946 e2
= get_value_range (n2
)->equiv
;
6948 /* Use the fake bitmaps if e1 or e2 are not available. */
6949 if (s_obstack
== NULL
)
6951 s_obstack
= XNEW (bitmap_obstack
);
6952 bitmap_obstack_initialize (s_obstack
);
6953 s_e1
= BITMAP_ALLOC (s_obstack
);
6954 s_e2
= BITMAP_ALLOC (s_obstack
);
6961 /* Add N1 and N2 to their own set of equivalences to avoid
6962 duplicating the body of the loop just to check N1 and N2
6964 bitmap_set_bit (e1
, SSA_NAME_VERSION (n1
));
6965 bitmap_set_bit (e2
, SSA_NAME_VERSION (n2
));
6967 /* If the equivalence sets have a common intersection, then the two
6968 names can be compared without checking their ranges. */
6969 if (bitmap_intersect_p (e1
, e2
))
6971 bitmap_clear_bit (e1
, SSA_NAME_VERSION (n1
));
6972 bitmap_clear_bit (e2
, SSA_NAME_VERSION (n2
));
6974 return (comp
== EQ_EXPR
|| comp
== GE_EXPR
|| comp
== LE_EXPR
)
6976 : boolean_false_node
;
6979 /* Start at -1. Set it to 0 if we do a comparison without relying
6980 on overflow, or 1 if all comparisons rely on overflow. */
6981 used_strict_overflow
= -1;
6983 /* Otherwise, compare all the equivalent ranges. First, add N1 and
6984 N2 to their own set of equivalences to avoid duplicating the body
6985 of the loop just to check N1 and N2 ranges. */
6986 EXECUTE_IF_SET_IN_BITMAP (e1
, 0, i1
, bi1
)
6988 value_range_t vr1
= get_vr_for_comparison (i1
);
6990 t
= retval
= NULL_TREE
;
6991 EXECUTE_IF_SET_IN_BITMAP (e2
, 0, i2
, bi2
)
6995 value_range_t vr2
= get_vr_for_comparison (i2
);
6997 t
= compare_ranges (comp
, &vr1
, &vr2
, &sop
);
7000 /* If we get different answers from different members
7001 of the equivalence set this check must be in a dead
7002 code region. Folding it to a trap representation
7003 would be correct here. For now just return don't-know. */
7007 bitmap_clear_bit (e1
, SSA_NAME_VERSION (n1
));
7008 bitmap_clear_bit (e2
, SSA_NAME_VERSION (n2
));
7014 used_strict_overflow
= 0;
7015 else if (used_strict_overflow
< 0)
7016 used_strict_overflow
= 1;
7022 bitmap_clear_bit (e1
, SSA_NAME_VERSION (n1
));
7023 bitmap_clear_bit (e2
, SSA_NAME_VERSION (n2
));
7024 if (used_strict_overflow
> 0)
7025 *strict_overflow_p
= true;
7030 /* None of the equivalent ranges are useful in computing this
7032 bitmap_clear_bit (e1
, SSA_NAME_VERSION (n1
));
7033 bitmap_clear_bit (e2
, SSA_NAME_VERSION (n2
));
7037 /* Helper function for vrp_evaluate_conditional_warnv. */
7040 vrp_evaluate_conditional_warnv_with_ops_using_ranges (enum tree_code code
,
7042 bool * strict_overflow_p
)
7044 value_range_t
*vr0
, *vr1
;
7046 vr0
= (TREE_CODE (op0
) == SSA_NAME
) ? get_value_range (op0
) : NULL
;
7047 vr1
= (TREE_CODE (op1
) == SSA_NAME
) ? get_value_range (op1
) : NULL
;
7050 return compare_ranges (code
, vr0
, vr1
, strict_overflow_p
);
7051 else if (vr0
&& vr1
== NULL
)
7052 return compare_range_with_value (code
, vr0
, op1
, strict_overflow_p
);
7053 else if (vr0
== NULL
&& vr1
)
7054 return (compare_range_with_value
7055 (swap_tree_comparison (code
), vr1
, op0
, strict_overflow_p
));
7059 /* Helper function for vrp_evaluate_conditional_warnv. */
7062 vrp_evaluate_conditional_warnv_with_ops (enum tree_code code
, tree op0
,
7063 tree op1
, bool use_equiv_p
,
7064 bool *strict_overflow_p
, bool *only_ranges
)
7068 *only_ranges
= true;
7070 /* We only deal with integral and pointer types. */
7071 if (!INTEGRAL_TYPE_P (TREE_TYPE (op0
))
7072 && !POINTER_TYPE_P (TREE_TYPE (op0
)))
7078 && (ret
= vrp_evaluate_conditional_warnv_with_ops_using_ranges
7079 (code
, op0
, op1
, strict_overflow_p
)))
7081 *only_ranges
= false;
7082 if (TREE_CODE (op0
) == SSA_NAME
&& TREE_CODE (op1
) == SSA_NAME
)
7083 return compare_names (code
, op0
, op1
, strict_overflow_p
);
7084 else if (TREE_CODE (op0
) == SSA_NAME
)
7085 return compare_name_with_value (code
, op0
, op1
, strict_overflow_p
);
7086 else if (TREE_CODE (op1
) == SSA_NAME
)
7087 return (compare_name_with_value
7088 (swap_tree_comparison (code
), op1
, op0
, strict_overflow_p
));
7091 return vrp_evaluate_conditional_warnv_with_ops_using_ranges (code
, op0
, op1
,
7096 /* Given (CODE OP0 OP1) within STMT, try to simplify it based on value range
7097 information. Return NULL if the conditional can not be evaluated.
7098 The ranges of all the names equivalent with the operands in COND
7099 will be used when trying to compute the value. If the result is
7100 based on undefined signed overflow, issue a warning if
7104 vrp_evaluate_conditional (enum tree_code code
, tree op0
, tree op1
, gimple stmt
)
7110 /* Some passes and foldings leak constants with overflow flag set
7111 into the IL. Avoid doing wrong things with these and bail out. */
7112 if ((TREE_CODE (op0
) == INTEGER_CST
7113 && TREE_OVERFLOW (op0
))
7114 || (TREE_CODE (op1
) == INTEGER_CST
7115 && TREE_OVERFLOW (op1
)))
7119 ret
= vrp_evaluate_conditional_warnv_with_ops (code
, op0
, op1
, true, &sop
,
7124 enum warn_strict_overflow_code wc
;
7125 const char* warnmsg
;
7127 if (is_gimple_min_invariant (ret
))
7129 wc
= WARN_STRICT_OVERFLOW_CONDITIONAL
;
7130 warnmsg
= G_("assuming signed overflow does not occur when "
7131 "simplifying conditional to constant");
7135 wc
= WARN_STRICT_OVERFLOW_COMPARISON
;
7136 warnmsg
= G_("assuming signed overflow does not occur when "
7137 "simplifying conditional");
7140 if (issue_strict_overflow_warning (wc
))
7142 location_t location
;
7144 if (!gimple_has_location (stmt
))
7145 location
= input_location
;
7147 location
= gimple_location (stmt
);
7148 warning_at (location
, OPT_Wstrict_overflow
, "%s", warnmsg
);
7152 if (warn_type_limits
7153 && ret
&& only_ranges
7154 && TREE_CODE_CLASS (code
) == tcc_comparison
7155 && TREE_CODE (op0
) == SSA_NAME
)
7157 /* If the comparison is being folded and the operand on the LHS
7158 is being compared against a constant value that is outside of
7159 the natural range of OP0's type, then the predicate will
7160 always fold regardless of the value of OP0. If -Wtype-limits
7161 was specified, emit a warning. */
7162 tree type
= TREE_TYPE (op0
);
7163 value_range_t
*vr0
= get_value_range (op0
);
7165 if (vr0
->type
!= VR_VARYING
7166 && INTEGRAL_TYPE_P (type
)
7167 && vrp_val_is_min (vr0
->min
)
7168 && vrp_val_is_max (vr0
->max
)
7169 && is_gimple_min_invariant (op1
))
7171 location_t location
;
7173 if (!gimple_has_location (stmt
))
7174 location
= input_location
;
7176 location
= gimple_location (stmt
);
7178 warning_at (location
, OPT_Wtype_limits
,
7180 ? G_("comparison always false "
7181 "due to limited range of data type")
7182 : G_("comparison always true "
7183 "due to limited range of data type"));
7191 /* Visit conditional statement STMT. If we can determine which edge
7192 will be taken out of STMT's basic block, record it in
7193 *TAKEN_EDGE_P and return SSA_PROP_INTERESTING. Otherwise, return
7194 SSA_PROP_VARYING. */
7196 static enum ssa_prop_result
7197 vrp_visit_cond_stmt (gimple stmt
, edge
*taken_edge_p
)
7202 *taken_edge_p
= NULL
;
7204 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7209 fprintf (dump_file
, "\nVisiting conditional with predicate: ");
7210 print_gimple_stmt (dump_file
, stmt
, 0, 0);
7211 fprintf (dump_file
, "\nWith known ranges\n");
7213 FOR_EACH_SSA_TREE_OPERAND (use
, stmt
, i
, SSA_OP_USE
)
7215 fprintf (dump_file
, "\t");
7216 print_generic_expr (dump_file
, use
, 0);
7217 fprintf (dump_file
, ": ");
7218 dump_value_range (dump_file
, vr_value
[SSA_NAME_VERSION (use
)]);
7221 fprintf (dump_file
, "\n");
7224 /* Compute the value of the predicate COND by checking the known
7225 ranges of each of its operands.
7227 Note that we cannot evaluate all the equivalent ranges here
7228 because those ranges may not yet be final and with the current
7229 propagation strategy, we cannot determine when the value ranges
7230 of the names in the equivalence set have changed.
7232 For instance, given the following code fragment
7236 i_14 = ASSERT_EXPR <i_5, i_5 != 0>
7240 Assume that on the first visit to i_14, i_5 has the temporary
7241 range [8, 8] because the second argument to the PHI function is
7242 not yet executable. We derive the range ~[0, 0] for i_14 and the
7243 equivalence set { i_5 }. So, when we visit 'if (i_14 == 1)' for
7244 the first time, since i_14 is equivalent to the range [8, 8], we
7245 determine that the predicate is always false.
7247 On the next round of propagation, i_13 is determined to be
7248 VARYING, which causes i_5 to drop down to VARYING. So, another
7249 visit to i_14 is scheduled. In this second visit, we compute the
7250 exact same range and equivalence set for i_14, namely ~[0, 0] and
7251 { i_5 }. But we did not have the previous range for i_5
7252 registered, so vrp_visit_assignment thinks that the range for
7253 i_14 has not changed. Therefore, the predicate 'if (i_14 == 1)'
7254 is not visited again, which stops propagation from visiting
7255 statements in the THEN clause of that if().
7257 To properly fix this we would need to keep the previous range
7258 value for the names in the equivalence set. This way we would've
7259 discovered that from one visit to the other i_5 changed from
7260 range [8, 8] to VR_VARYING.
7262 However, fixing this apparent limitation may not be worth the
7263 additional checking. Testing on several code bases (GCC, DLV,
7264 MICO, TRAMP3D and SPEC2000) showed that doing this results in
7265 4 more predicates folded in SPEC. */
7268 val
= vrp_evaluate_conditional_warnv_with_ops (gimple_cond_code (stmt
),
7269 gimple_cond_lhs (stmt
),
7270 gimple_cond_rhs (stmt
),
7275 *taken_edge_p
= find_taken_edge (gimple_bb (stmt
), val
);
7278 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7280 "\nIgnoring predicate evaluation because "
7281 "it assumes that signed overflow is undefined");
7286 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7288 fprintf (dump_file
, "\nPredicate evaluates to: ");
7289 if (val
== NULL_TREE
)
7290 fprintf (dump_file
, "DON'T KNOW\n");
7292 print_generic_stmt (dump_file
, val
, 0);
7295 return (*taken_edge_p
) ? SSA_PROP_INTERESTING
: SSA_PROP_VARYING
;
7298 /* Searches the case label vector VEC for the index *IDX of the CASE_LABEL
7299 that includes the value VAL. The search is restricted to the range
7300 [START_IDX, n - 1] where n is the size of VEC.
7302 If there is a CASE_LABEL for VAL, its index is placed in IDX and true is
7305 If there is no CASE_LABEL for VAL and there is one that is larger than VAL,
7306 it is placed in IDX and false is returned.
7308 If VAL is larger than any CASE_LABEL, n is placed on IDX and false is
7312 find_case_label_index (gimple stmt
, size_t start_idx
, tree val
, size_t *idx
)
7314 size_t n
= gimple_switch_num_labels (stmt
);
7317 /* Find case label for minimum of the value range or the next one.
7318 At each iteration we are searching in [low, high - 1]. */
7320 for (low
= start_idx
, high
= n
; high
!= low
; )
7324 /* Note that i != high, so we never ask for n. */
7325 size_t i
= (high
+ low
) / 2;
7326 t
= gimple_switch_label (stmt
, i
);
7328 /* Cache the result of comparing CASE_LOW and val. */
7329 cmp
= tree_int_cst_compare (CASE_LOW (t
), val
);
7333 /* Ranges cannot be empty. */
7342 if (CASE_HIGH (t
) != NULL
7343 && tree_int_cst_compare (CASE_HIGH (t
), val
) >= 0)
7355 /* Searches the case label vector VEC for the range of CASE_LABELs that is used
7356 for values between MIN and MAX. The first index is placed in MIN_IDX. The
7357 last index is placed in MAX_IDX. If the range of CASE_LABELs is empty
7358 then MAX_IDX < MIN_IDX.
7359 Returns true if the default label is not needed. */
7362 find_case_label_range (gimple stmt
, tree min
, tree max
, size_t *min_idx
,
7366 bool min_take_default
= !find_case_label_index (stmt
, 1, min
, &i
);
7367 bool max_take_default
= !find_case_label_index (stmt
, i
, max
, &j
);
7371 && max_take_default
)
7373 /* Only the default case label reached.
7374 Return an empty range. */
7381 bool take_default
= min_take_default
|| max_take_default
;
7385 if (max_take_default
)
7388 /* If the case label range is continuous, we do not need
7389 the default case label. Verify that. */
7390 high
= CASE_LOW (gimple_switch_label (stmt
, i
));
7391 if (CASE_HIGH (gimple_switch_label (stmt
, i
)))
7392 high
= CASE_HIGH (gimple_switch_label (stmt
, i
));
7393 for (k
= i
+ 1; k
<= j
; ++k
)
7395 low
= CASE_LOW (gimple_switch_label (stmt
, k
));
7396 if (!integer_onep (int_const_binop (MINUS_EXPR
, low
, high
)))
7398 take_default
= true;
7402 if (CASE_HIGH (gimple_switch_label (stmt
, k
)))
7403 high
= CASE_HIGH (gimple_switch_label (stmt
, k
));
7408 return !take_default
;
7412 /* Searches the case label vector VEC for the ranges of CASE_LABELs that are
7413 used in range VR. The indices are placed in MIN_IDX1, MAX_IDX, MIN_IDX2 and
7414 MAX_IDX2. If the ranges of CASE_LABELs are empty then MAX_IDX1 < MIN_IDX1.
7415 Returns true if the default label is not needed. */
7418 find_case_label_ranges (gimple stmt
, value_range_t
*vr
, size_t *min_idx1
,
7419 size_t *max_idx1
, size_t *min_idx2
,
7423 unsigned int n
= gimple_switch_num_labels (stmt
);
7425 tree case_low
, case_high
;
7426 tree min
= vr
->min
, max
= vr
->max
;
7428 gcc_checking_assert (vr
->type
== VR_RANGE
|| vr
->type
== VR_ANTI_RANGE
);
7430 take_default
= !find_case_label_range (stmt
, min
, max
, &i
, &j
);
7432 /* Set second range to emtpy. */
7436 if (vr
->type
== VR_RANGE
)
7440 return !take_default
;
7443 /* Set first range to all case labels. */
7450 /* Make sure all the values of case labels [i , j] are contained in
7451 range [MIN, MAX]. */
7452 case_low
= CASE_LOW (gimple_switch_label (stmt
, i
));
7453 case_high
= CASE_HIGH (gimple_switch_label (stmt
, j
));
7454 if (tree_int_cst_compare (case_low
, min
) < 0)
7456 if (case_high
!= NULL_TREE
7457 && tree_int_cst_compare (max
, case_high
) < 0)
7463 /* If the range spans case labels [i, j], the corresponding anti-range spans
7464 the labels [1, i - 1] and [j + 1, n - 1]. */
7490 /* Visit switch statement STMT. If we can determine which edge
7491 will be taken out of STMT's basic block, record it in
7492 *TAKEN_EDGE_P and return SSA_PROP_INTERESTING. Otherwise, return
7493 SSA_PROP_VARYING. */
7495 static enum ssa_prop_result
7496 vrp_visit_switch_stmt (gimple stmt
, edge
*taken_edge_p
)
7500 size_t i
= 0, j
= 0, k
, l
;
7503 *taken_edge_p
= NULL
;
7504 op
= gimple_switch_index (stmt
);
7505 if (TREE_CODE (op
) != SSA_NAME
)
7506 return SSA_PROP_VARYING
;
7508 vr
= get_value_range (op
);
7509 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7511 fprintf (dump_file
, "\nVisiting switch expression with operand ");
7512 print_generic_expr (dump_file
, op
, 0);
7513 fprintf (dump_file
, " with known range ");
7514 dump_value_range (dump_file
, vr
);
7515 fprintf (dump_file
, "\n");
7518 if ((vr
->type
!= VR_RANGE
7519 && vr
->type
!= VR_ANTI_RANGE
)
7520 || symbolic_range_p (vr
))
7521 return SSA_PROP_VARYING
;
7523 /* Find the single edge that is taken from the switch expression. */
7524 take_default
= !find_case_label_ranges (stmt
, vr
, &i
, &j
, &k
, &l
);
7526 /* Check if the range spans no CASE_LABEL. If so, we only reach the default
7530 gcc_assert (take_default
);
7531 val
= gimple_switch_default_label (stmt
);
7535 /* Check if labels with index i to j and maybe the default label
7536 are all reaching the same label. */
7538 val
= gimple_switch_label (stmt
, i
);
7540 && CASE_LABEL (gimple_switch_default_label (stmt
))
7541 != CASE_LABEL (val
))
7543 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7544 fprintf (dump_file
, " not a single destination for this "
7546 return SSA_PROP_VARYING
;
7548 for (++i
; i
<= j
; ++i
)
7550 if (CASE_LABEL (gimple_switch_label (stmt
, i
)) != CASE_LABEL (val
))
7552 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7553 fprintf (dump_file
, " not a single destination for this "
7555 return SSA_PROP_VARYING
;
7560 if (CASE_LABEL (gimple_switch_label (stmt
, k
)) != CASE_LABEL (val
))
7562 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7563 fprintf (dump_file
, " not a single destination for this "
7565 return SSA_PROP_VARYING
;
7570 *taken_edge_p
= find_edge (gimple_bb (stmt
),
7571 label_to_block (CASE_LABEL (val
)));
7573 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7575 fprintf (dump_file
, " will take edge to ");
7576 print_generic_stmt (dump_file
, CASE_LABEL (val
), 0);
7579 return SSA_PROP_INTERESTING
;
7583 /* Evaluate statement STMT. If the statement produces a useful range,
7584 return SSA_PROP_INTERESTING and record the SSA name with the
7585 interesting range into *OUTPUT_P.
7587 If STMT is a conditional branch and we can determine its truth
7588 value, the taken edge is recorded in *TAKEN_EDGE_P.
7590 If STMT produces a varying value, return SSA_PROP_VARYING. */
7592 static enum ssa_prop_result
7593 vrp_visit_stmt (gimple stmt
, edge
*taken_edge_p
, tree
*output_p
)
7598 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7600 fprintf (dump_file
, "\nVisiting statement:\n");
7601 print_gimple_stmt (dump_file
, stmt
, 0, dump_flags
);
7602 fprintf (dump_file
, "\n");
7605 if (!stmt_interesting_for_vrp (stmt
))
7606 gcc_assert (stmt_ends_bb_p (stmt
));
7607 else if (is_gimple_assign (stmt
) || is_gimple_call (stmt
))
7608 return vrp_visit_assignment_or_call (stmt
, output_p
);
7609 else if (gimple_code (stmt
) == GIMPLE_COND
)
7610 return vrp_visit_cond_stmt (stmt
, taken_edge_p
);
7611 else if (gimple_code (stmt
) == GIMPLE_SWITCH
)
7612 return vrp_visit_switch_stmt (stmt
, taken_edge_p
);
7614 /* All other statements produce nothing of interest for VRP, so mark
7615 their outputs varying and prevent further simulation. */
7616 FOR_EACH_SSA_TREE_OPERAND (def
, stmt
, iter
, SSA_OP_DEF
)
7617 set_value_range_to_varying (get_value_range (def
));
7619 return SSA_PROP_VARYING
;
7622 /* Union the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
7623 { VR1TYPE, VR0MIN, VR0MAX } and store the result
7624 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
7625 possible such range. The resulting range is not canonicalized. */
7628 union_ranges (enum value_range_type
*vr0type
,
7629 tree
*vr0min
, tree
*vr0max
,
7630 enum value_range_type vr1type
,
7631 tree vr1min
, tree vr1max
)
7633 bool mineq
= operand_equal_p (*vr0min
, vr1min
, 0);
7634 bool maxeq
= operand_equal_p (*vr0max
, vr1max
, 0);
7636 /* [] is vr0, () is vr1 in the following classification comments. */
7640 if (*vr0type
== vr1type
)
7641 /* Nothing to do for equal ranges. */
7643 else if ((*vr0type
== VR_RANGE
7644 && vr1type
== VR_ANTI_RANGE
)
7645 || (*vr0type
== VR_ANTI_RANGE
7646 && vr1type
== VR_RANGE
))
7648 /* For anti-range with range union the result is varying. */
7654 else if (operand_less_p (*vr0max
, vr1min
) == 1
7655 || operand_less_p (vr1max
, *vr0min
) == 1)
7657 /* [ ] ( ) or ( ) [ ]
7658 If the ranges have an empty intersection, result of the union
7659 operation is the anti-range or if both are anti-ranges
7661 if (*vr0type
== VR_ANTI_RANGE
7662 && vr1type
== VR_ANTI_RANGE
)
7664 else if (*vr0type
== VR_ANTI_RANGE
7665 && vr1type
== VR_RANGE
)
7667 else if (*vr0type
== VR_RANGE
7668 && vr1type
== VR_ANTI_RANGE
)
7674 else if (*vr0type
== VR_RANGE
7675 && vr1type
== VR_RANGE
)
7677 /* The result is the convex hull of both ranges. */
7678 if (operand_less_p (*vr0max
, vr1min
) == 1)
7680 /* If the result can be an anti-range, create one. */
7681 if (TREE_CODE (*vr0max
) == INTEGER_CST
7682 && TREE_CODE (vr1min
) == INTEGER_CST
7683 && vrp_val_is_min (*vr0min
)
7684 && vrp_val_is_max (vr1max
))
7686 tree min
= int_const_binop (PLUS_EXPR
,
7687 *vr0max
, integer_one_node
);
7688 tree max
= int_const_binop (MINUS_EXPR
,
7689 vr1min
, integer_one_node
);
7690 if (!operand_less_p (max
, min
))
7692 *vr0type
= VR_ANTI_RANGE
;
7704 /* If the result can be an anti-range, create one. */
7705 if (TREE_CODE (vr1max
) == INTEGER_CST
7706 && TREE_CODE (*vr0min
) == INTEGER_CST
7707 && vrp_val_is_min (vr1min
)
7708 && vrp_val_is_max (*vr0max
))
7710 tree min
= int_const_binop (PLUS_EXPR
,
7711 vr1max
, integer_one_node
);
7712 tree max
= int_const_binop (MINUS_EXPR
,
7713 *vr0min
, integer_one_node
);
7714 if (!operand_less_p (max
, min
))
7716 *vr0type
= VR_ANTI_RANGE
;
7730 else if ((maxeq
|| operand_less_p (vr1max
, *vr0max
) == 1)
7731 && (mineq
|| operand_less_p (*vr0min
, vr1min
) == 1))
7733 /* [ ( ) ] or [( ) ] or [ ( )] */
7734 if (*vr0type
== VR_RANGE
7735 && vr1type
== VR_RANGE
)
7737 else if (*vr0type
== VR_ANTI_RANGE
7738 && vr1type
== VR_ANTI_RANGE
)
7744 else if (*vr0type
== VR_ANTI_RANGE
7745 && vr1type
== VR_RANGE
)
7747 /* Arbitrarily choose the right or left gap. */
7748 if (!mineq
&& TREE_CODE (vr1min
) == INTEGER_CST
)
7749 *vr0max
= int_const_binop (MINUS_EXPR
, vr1min
, integer_one_node
);
7750 else if (!maxeq
&& TREE_CODE (vr1max
) == INTEGER_CST
)
7751 *vr0min
= int_const_binop (PLUS_EXPR
, vr1max
, integer_one_node
);
7755 else if (*vr0type
== VR_RANGE
7756 && vr1type
== VR_ANTI_RANGE
)
7757 /* The result covers everything. */
7762 else if ((maxeq
|| operand_less_p (*vr0max
, vr1max
) == 1)
7763 && (mineq
|| operand_less_p (vr1min
, *vr0min
) == 1))
7765 /* ( [ ] ) or ([ ] ) or ( [ ]) */
7766 if (*vr0type
== VR_RANGE
7767 && vr1type
== VR_RANGE
)
7773 else if (*vr0type
== VR_ANTI_RANGE
7774 && vr1type
== VR_ANTI_RANGE
)
7776 else if (*vr0type
== VR_RANGE
7777 && vr1type
== VR_ANTI_RANGE
)
7779 *vr0type
= VR_ANTI_RANGE
;
7780 if (!mineq
&& TREE_CODE (*vr0min
) == INTEGER_CST
)
7782 *vr0max
= int_const_binop (MINUS_EXPR
, *vr0min
, integer_one_node
);
7785 else if (!maxeq
&& TREE_CODE (*vr0max
) == INTEGER_CST
)
7787 *vr0min
= int_const_binop (PLUS_EXPR
, *vr0max
, integer_one_node
);
7793 else if (*vr0type
== VR_ANTI_RANGE
7794 && vr1type
== VR_RANGE
)
7795 /* The result covers everything. */
7800 else if ((operand_less_p (vr1min
, *vr0max
) == 1
7801 || operand_equal_p (vr1min
, *vr0max
, 0))
7802 && operand_less_p (*vr0min
, vr1min
) == 1
7803 && operand_less_p (*vr0max
, vr1max
) == 1)
7805 /* [ ( ] ) or [ ]( ) */
7806 if (*vr0type
== VR_RANGE
7807 && vr1type
== VR_RANGE
)
7809 else if (*vr0type
== VR_ANTI_RANGE
7810 && vr1type
== VR_ANTI_RANGE
)
7812 else if (*vr0type
== VR_ANTI_RANGE
7813 && vr1type
== VR_RANGE
)
7815 if (TREE_CODE (vr1min
) == INTEGER_CST
)
7816 *vr0max
= int_const_binop (MINUS_EXPR
, vr1min
, integer_one_node
);
7820 else if (*vr0type
== VR_RANGE
7821 && vr1type
== VR_ANTI_RANGE
)
7823 if (TREE_CODE (*vr0max
) == INTEGER_CST
)
7826 *vr0min
= int_const_binop (PLUS_EXPR
, *vr0max
, integer_one_node
);
7835 else if ((operand_less_p (*vr0min
, vr1max
) == 1
7836 || operand_equal_p (*vr0min
, vr1max
, 0))
7837 && operand_less_p (vr1min
, *vr0min
) == 1
7838 && operand_less_p (vr1max
, *vr0max
) == 1)
7840 /* ( [ ) ] or ( )[ ] */
7841 if (*vr0type
== VR_RANGE
7842 && vr1type
== VR_RANGE
)
7844 else if (*vr0type
== VR_ANTI_RANGE
7845 && vr1type
== VR_ANTI_RANGE
)
7847 else if (*vr0type
== VR_ANTI_RANGE
7848 && vr1type
== VR_RANGE
)
7850 if (TREE_CODE (vr1max
) == INTEGER_CST
)
7851 *vr0min
= int_const_binop (PLUS_EXPR
, vr1max
, integer_one_node
);
7855 else if (*vr0type
== VR_RANGE
7856 && vr1type
== VR_ANTI_RANGE
)
7858 if (TREE_CODE (*vr0min
) == INTEGER_CST
)
7862 *vr0max
= int_const_binop (MINUS_EXPR
, *vr0min
, integer_one_node
);
7876 *vr0type
= VR_VARYING
;
7877 *vr0min
= NULL_TREE
;
7878 *vr0max
= NULL_TREE
;
7881 /* Intersect the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
7882 { VR1TYPE, VR0MIN, VR0MAX } and store the result
7883 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
7884 possible such range. The resulting range is not canonicalized. */
7887 intersect_ranges (enum value_range_type
*vr0type
,
7888 tree
*vr0min
, tree
*vr0max
,
7889 enum value_range_type vr1type
,
7890 tree vr1min
, tree vr1max
)
7892 bool mineq
= operand_equal_p (*vr0min
, vr1min
, 0);
7893 bool maxeq
= operand_equal_p (*vr0max
, vr1max
, 0);
7895 /* [] is vr0, () is vr1 in the following classification comments. */
7899 if (*vr0type
== vr1type
)
7900 /* Nothing to do for equal ranges. */
7902 else if ((*vr0type
== VR_RANGE
7903 && vr1type
== VR_ANTI_RANGE
)
7904 || (*vr0type
== VR_ANTI_RANGE
7905 && vr1type
== VR_RANGE
))
7907 /* For anti-range with range intersection the result is empty. */
7908 *vr0type
= VR_UNDEFINED
;
7909 *vr0min
= NULL_TREE
;
7910 *vr0max
= NULL_TREE
;
7915 else if (operand_less_p (*vr0max
, vr1min
) == 1
7916 || operand_less_p (vr1max
, *vr0min
) == 1)
7918 /* [ ] ( ) or ( ) [ ]
7919 If the ranges have an empty intersection, the result of the
7920 intersect operation is the range for intersecting an
7921 anti-range with a range or empty when intersecting two ranges. */
7922 if (*vr0type
== VR_RANGE
7923 && vr1type
== VR_ANTI_RANGE
)
7925 else if (*vr0type
== VR_ANTI_RANGE
7926 && vr1type
== VR_RANGE
)
7932 else if (*vr0type
== VR_RANGE
7933 && vr1type
== VR_RANGE
)
7935 *vr0type
= VR_UNDEFINED
;
7936 *vr0min
= NULL_TREE
;
7937 *vr0max
= NULL_TREE
;
7939 else if (*vr0type
== VR_ANTI_RANGE
7940 && vr1type
== VR_ANTI_RANGE
)
7942 /* If the anti-ranges are adjacent to each other merge them. */
7943 if (TREE_CODE (*vr0max
) == INTEGER_CST
7944 && TREE_CODE (vr1min
) == INTEGER_CST
7945 && operand_less_p (*vr0max
, vr1min
) == 1
7946 && integer_onep (int_const_binop (MINUS_EXPR
,
7949 else if (TREE_CODE (vr1max
) == INTEGER_CST
7950 && TREE_CODE (*vr0min
) == INTEGER_CST
7951 && operand_less_p (vr1max
, *vr0min
) == 1
7952 && integer_onep (int_const_binop (MINUS_EXPR
,
7955 /* Else arbitrarily take VR0. */
7958 else if ((maxeq
|| operand_less_p (vr1max
, *vr0max
) == 1)
7959 && (mineq
|| operand_less_p (*vr0min
, vr1min
) == 1))
7961 /* [ ( ) ] or [( ) ] or [ ( )] */
7962 if (*vr0type
== VR_RANGE
7963 && vr1type
== VR_RANGE
)
7965 /* If both are ranges the result is the inner one. */
7970 else if (*vr0type
== VR_RANGE
7971 && vr1type
== VR_ANTI_RANGE
)
7973 /* Choose the right gap if the left one is empty. */
7976 if (TREE_CODE (vr1max
) == INTEGER_CST
)
7977 *vr0min
= int_const_binop (PLUS_EXPR
, vr1max
, integer_one_node
);
7981 /* Choose the left gap if the right one is empty. */
7984 if (TREE_CODE (vr1min
) == INTEGER_CST
)
7985 *vr0max
= int_const_binop (MINUS_EXPR
, vr1min
,
7990 /* Choose the anti-range if the range is effectively varying. */
7991 else if (vrp_val_is_min (*vr0min
)
7992 && vrp_val_is_max (*vr0max
))
7998 /* Else choose the range. */
8000 else if (*vr0type
== VR_ANTI_RANGE
8001 && vr1type
== VR_ANTI_RANGE
)
8002 /* If both are anti-ranges the result is the outer one. */
8004 else if (*vr0type
== VR_ANTI_RANGE
8005 && vr1type
== VR_RANGE
)
8007 /* The intersection is empty. */
8008 *vr0type
= VR_UNDEFINED
;
8009 *vr0min
= NULL_TREE
;
8010 *vr0max
= NULL_TREE
;
8015 else if ((maxeq
|| operand_less_p (*vr0max
, vr1max
) == 1)
8016 && (mineq
|| operand_less_p (vr1min
, *vr0min
) == 1))
8018 /* ( [ ] ) or ([ ] ) or ( [ ]) */
8019 if (*vr0type
== VR_RANGE
8020 && vr1type
== VR_RANGE
)
8021 /* Choose the inner range. */
8023 else if (*vr0type
== VR_ANTI_RANGE
8024 && vr1type
== VR_RANGE
)
8026 /* Choose the right gap if the left is empty. */
8029 *vr0type
= VR_RANGE
;
8030 if (TREE_CODE (*vr0max
) == INTEGER_CST
)
8031 *vr0min
= int_const_binop (PLUS_EXPR
, *vr0max
,
8037 /* Choose the left gap if the right is empty. */
8040 *vr0type
= VR_RANGE
;
8041 if (TREE_CODE (*vr0min
) == INTEGER_CST
)
8042 *vr0max
= int_const_binop (MINUS_EXPR
, *vr0min
,
8048 /* Choose the anti-range if the range is effectively varying. */
8049 else if (vrp_val_is_min (vr1min
)
8050 && vrp_val_is_max (vr1max
))
8052 /* Else choose the range. */
8060 else if (*vr0type
== VR_ANTI_RANGE
8061 && vr1type
== VR_ANTI_RANGE
)
8063 /* If both are anti-ranges the result is the outer one. */
8068 else if (vr1type
== VR_ANTI_RANGE
8069 && *vr0type
== VR_RANGE
)
8071 /* The intersection is empty. */
8072 *vr0type
= VR_UNDEFINED
;
8073 *vr0min
= NULL_TREE
;
8074 *vr0max
= NULL_TREE
;
8079 else if ((operand_less_p (vr1min
, *vr0max
) == 1
8080 || operand_equal_p (vr1min
, *vr0max
, 0))
8081 && operand_less_p (*vr0min
, vr1min
) == 1)
8083 /* [ ( ] ) or [ ]( ) */
8084 if (*vr0type
== VR_ANTI_RANGE
8085 && vr1type
== VR_ANTI_RANGE
)
8087 else if (*vr0type
== VR_RANGE
8088 && vr1type
== VR_RANGE
)
8090 else if (*vr0type
== VR_RANGE
8091 && vr1type
== VR_ANTI_RANGE
)
8093 if (TREE_CODE (vr1min
) == INTEGER_CST
)
8094 *vr0max
= int_const_binop (MINUS_EXPR
, vr1min
,
8099 else if (*vr0type
== VR_ANTI_RANGE
8100 && vr1type
== VR_RANGE
)
8102 *vr0type
= VR_RANGE
;
8103 if (TREE_CODE (*vr0max
) == INTEGER_CST
)
8104 *vr0min
= int_const_binop (PLUS_EXPR
, *vr0max
,
8113 else if ((operand_less_p (*vr0min
, vr1max
) == 1
8114 || operand_equal_p (*vr0min
, vr1max
, 0))
8115 && operand_less_p (vr1min
, *vr0min
) == 1)
8117 /* ( [ ) ] or ( )[ ] */
8118 if (*vr0type
== VR_ANTI_RANGE
8119 && vr1type
== VR_ANTI_RANGE
)
8121 else if (*vr0type
== VR_RANGE
8122 && vr1type
== VR_RANGE
)
8124 else if (*vr0type
== VR_RANGE
8125 && vr1type
== VR_ANTI_RANGE
)
8127 if (TREE_CODE (vr1max
) == INTEGER_CST
)
8128 *vr0min
= int_const_binop (PLUS_EXPR
, vr1max
,
8133 else if (*vr0type
== VR_ANTI_RANGE
8134 && vr1type
== VR_RANGE
)
8136 *vr0type
= VR_RANGE
;
8137 if (TREE_CODE (*vr0min
) == INTEGER_CST
)
8138 *vr0max
= int_const_binop (MINUS_EXPR
, *vr0min
,
8148 /* As a fallback simply use { *VRTYPE, *VR0MIN, *VR0MAX } as
8149 result for the intersection. That's always a conservative
8150 correct estimate. */
8156 /* Intersect the two value-ranges *VR0 and *VR1 and store the result
8157 in *VR0. This may not be the smallest possible such range. */
8160 vrp_intersect_ranges_1 (value_range_t
*vr0
, value_range_t
*vr1
)
8162 value_range_t saved
;
8164 /* If either range is VR_VARYING the other one wins. */
8165 if (vr1
->type
== VR_VARYING
)
8167 if (vr0
->type
== VR_VARYING
)
8169 copy_value_range (vr0
, vr1
);
8173 /* When either range is VR_UNDEFINED the resulting range is
8174 VR_UNDEFINED, too. */
8175 if (vr0
->type
== VR_UNDEFINED
)
8177 if (vr1
->type
== VR_UNDEFINED
)
8179 set_value_range_to_undefined (vr0
);
8183 /* Save the original vr0 so we can return it as conservative intersection
8184 result when our worker turns things to varying. */
8186 intersect_ranges (&vr0
->type
, &vr0
->min
, &vr0
->max
,
8187 vr1
->type
, vr1
->min
, vr1
->max
);
8188 /* Make sure to canonicalize the result though as the inversion of a
8189 VR_RANGE can still be a VR_RANGE. */
8190 set_and_canonicalize_value_range (vr0
, vr0
->type
,
8191 vr0
->min
, vr0
->max
, vr0
->equiv
);
8192 /* If that failed, use the saved original VR0. */
8193 if (vr0
->type
== VR_VARYING
)
8198 /* If the result is VR_UNDEFINED there is no need to mess with
8199 the equivalencies. */
8200 if (vr0
->type
== VR_UNDEFINED
)
8203 /* The resulting set of equivalences for range intersection is the union of
8205 if (vr0
->equiv
&& vr1
->equiv
&& vr0
->equiv
!= vr1
->equiv
)
8206 bitmap_ior_into (vr0
->equiv
, vr1
->equiv
);
8207 else if (vr1
->equiv
&& !vr0
->equiv
)
8208 bitmap_copy (vr0
->equiv
, vr1
->equiv
);
8212 vrp_intersect_ranges (value_range_t
*vr0
, value_range_t
*vr1
)
8214 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8216 fprintf (dump_file
, "Intersecting\n ");
8217 dump_value_range (dump_file
, vr0
);
8218 fprintf (dump_file
, "\nand\n ");
8219 dump_value_range (dump_file
, vr1
);
8220 fprintf (dump_file
, "\n");
8222 vrp_intersect_ranges_1 (vr0
, vr1
);
8223 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8225 fprintf (dump_file
, "to\n ");
8226 dump_value_range (dump_file
, vr0
);
8227 fprintf (dump_file
, "\n");
8231 /* Meet operation for value ranges. Given two value ranges VR0 and
8232 VR1, store in VR0 a range that contains both VR0 and VR1. This
8233 may not be the smallest possible such range. */
8236 vrp_meet_1 (value_range_t
*vr0
, value_range_t
*vr1
)
8238 value_range_t saved
;
8240 if (vr0
->type
== VR_UNDEFINED
)
8242 set_value_range (vr0
, vr1
->type
, vr1
->min
, vr1
->max
, vr1
->equiv
);
8246 if (vr1
->type
== VR_UNDEFINED
)
8248 /* VR0 already has the resulting range. */
8252 if (vr0
->type
== VR_VARYING
)
8254 /* Nothing to do. VR0 already has the resulting range. */
8258 if (vr1
->type
== VR_VARYING
)
8260 set_value_range_to_varying (vr0
);
8265 union_ranges (&vr0
->type
, &vr0
->min
, &vr0
->max
,
8266 vr1
->type
, vr1
->min
, vr1
->max
);
8267 if (vr0
->type
== VR_VARYING
)
8269 /* Failed to find an efficient meet. Before giving up and setting
8270 the result to VARYING, see if we can at least derive a useful
8271 anti-range. FIXME, all this nonsense about distinguishing
8272 anti-ranges from ranges is necessary because of the odd
8273 semantics of range_includes_zero_p and friends. */
8274 if (((saved
.type
== VR_RANGE
8275 && range_includes_zero_p (saved
.min
, saved
.max
) == 0)
8276 || (saved
.type
== VR_ANTI_RANGE
8277 && range_includes_zero_p (saved
.min
, saved
.max
) == 1))
8278 && ((vr1
->type
== VR_RANGE
8279 && range_includes_zero_p (vr1
->min
, vr1
->max
) == 0)
8280 || (vr1
->type
== VR_ANTI_RANGE
8281 && range_includes_zero_p (vr1
->min
, vr1
->max
) == 1)))
8283 set_value_range_to_nonnull (vr0
, TREE_TYPE (saved
.min
));
8285 /* Since this meet operation did not result from the meeting of
8286 two equivalent names, VR0 cannot have any equivalences. */
8288 bitmap_clear (vr0
->equiv
);
8292 set_value_range_to_varying (vr0
);
8295 set_and_canonicalize_value_range (vr0
, vr0
->type
, vr0
->min
, vr0
->max
,
8297 if (vr0
->type
== VR_VARYING
)
8300 /* The resulting set of equivalences is always the intersection of
8302 if (vr0
->equiv
&& vr1
->equiv
&& vr0
->equiv
!= vr1
->equiv
)
8303 bitmap_and_into (vr0
->equiv
, vr1
->equiv
);
8304 else if (vr0
->equiv
&& !vr1
->equiv
)
8305 bitmap_clear (vr0
->equiv
);
8309 vrp_meet (value_range_t
*vr0
, value_range_t
*vr1
)
8311 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8313 fprintf (dump_file
, "Meeting\n ");
8314 dump_value_range (dump_file
, vr0
);
8315 fprintf (dump_file
, "\nand\n ");
8316 dump_value_range (dump_file
, vr1
);
8317 fprintf (dump_file
, "\n");
8319 vrp_meet_1 (vr0
, vr1
);
8320 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8322 fprintf (dump_file
, "to\n ");
8323 dump_value_range (dump_file
, vr0
);
8324 fprintf (dump_file
, "\n");
8329 /* Visit all arguments for PHI node PHI that flow through executable
8330 edges. If a valid value range can be derived from all the incoming
8331 value ranges, set a new range for the LHS of PHI. */
8333 static enum ssa_prop_result
8334 vrp_visit_phi_node (gimple phi
)
8337 tree lhs
= PHI_RESULT (phi
);
8338 value_range_t
*lhs_vr
= get_value_range (lhs
);
8339 value_range_t vr_result
= VR_INITIALIZER
;
8341 int edges
, old_edges
;
8344 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8346 fprintf (dump_file
, "\nVisiting PHI node: ");
8347 print_gimple_stmt (dump_file
, phi
, 0, dump_flags
);
8351 for (i
= 0; i
< gimple_phi_num_args (phi
); i
++)
8353 edge e
= gimple_phi_arg_edge (phi
, i
);
8355 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8358 "\n Argument #%d (%d -> %d %sexecutable)\n",
8359 (int) i
, e
->src
->index
, e
->dest
->index
,
8360 (e
->flags
& EDGE_EXECUTABLE
) ? "" : "not ");
8363 if (e
->flags
& EDGE_EXECUTABLE
)
8365 tree arg
= PHI_ARG_DEF (phi
, i
);
8366 value_range_t vr_arg
;
8370 if (TREE_CODE (arg
) == SSA_NAME
)
8372 vr_arg
= *(get_value_range (arg
));
8373 /* Do not allow equivalences or symbolic ranges to leak in from
8374 backedges. That creates invalid equivalencies.
8375 See PR53465 and PR54767. */
8376 if (e
->flags
& EDGE_DFS_BACK
8377 && (vr_arg
.type
== VR_RANGE
8378 || vr_arg
.type
== VR_ANTI_RANGE
))
8380 vr_arg
.equiv
= NULL
;
8381 if (symbolic_range_p (&vr_arg
))
8383 vr_arg
.type
= VR_VARYING
;
8384 vr_arg
.min
= NULL_TREE
;
8385 vr_arg
.max
= NULL_TREE
;
8391 if (TREE_OVERFLOW_P (arg
))
8392 arg
= drop_tree_overflow (arg
);
8394 vr_arg
.type
= VR_RANGE
;
8397 vr_arg
.equiv
= NULL
;
8400 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8402 fprintf (dump_file
, "\t");
8403 print_generic_expr (dump_file
, arg
, dump_flags
);
8404 fprintf (dump_file
, "\n\tValue: ");
8405 dump_value_range (dump_file
, &vr_arg
);
8406 fprintf (dump_file
, "\n");
8410 copy_value_range (&vr_result
, &vr_arg
);
8412 vrp_meet (&vr_result
, &vr_arg
);
8415 if (vr_result
.type
== VR_VARYING
)
8420 if (vr_result
.type
== VR_VARYING
)
8422 else if (vr_result
.type
== VR_UNDEFINED
)
8425 old_edges
= vr_phi_edge_counts
[SSA_NAME_VERSION (lhs
)];
8426 vr_phi_edge_counts
[SSA_NAME_VERSION (lhs
)] = edges
;
8428 /* To prevent infinite iterations in the algorithm, derive ranges
8429 when the new value is slightly bigger or smaller than the
8430 previous one. We don't do this if we have seen a new executable
8431 edge; this helps us avoid an overflow infinity for conditionals
8432 which are not in a loop. If the old value-range was VR_UNDEFINED
8433 use the updated range and iterate one more time. */
8435 && gimple_phi_num_args (phi
) > 1
8436 && edges
== old_edges
8437 && lhs_vr
->type
!= VR_UNDEFINED
)
8439 int cmp_min
= compare_values (lhs_vr
->min
, vr_result
.min
);
8440 int cmp_max
= compare_values (lhs_vr
->max
, vr_result
.max
);
8442 /* For non VR_RANGE or for pointers fall back to varying if
8443 the range changed. */
8444 if ((lhs_vr
->type
!= VR_RANGE
|| vr_result
.type
!= VR_RANGE
8445 || POINTER_TYPE_P (TREE_TYPE (lhs
)))
8446 && (cmp_min
!= 0 || cmp_max
!= 0))
8449 /* If the new minimum is smaller or larger than the previous
8450 one, go all the way to -INF. In the first case, to avoid
8451 iterating millions of times to reach -INF, and in the
8452 other case to avoid infinite bouncing between different
8454 if (cmp_min
> 0 || cmp_min
< 0)
8456 if (!needs_overflow_infinity (TREE_TYPE (vr_result
.min
))
8457 || !vrp_var_may_overflow (lhs
, phi
))
8458 vr_result
.min
= TYPE_MIN_VALUE (TREE_TYPE (vr_result
.min
));
8459 else if (supports_overflow_infinity (TREE_TYPE (vr_result
.min
)))
8461 negative_overflow_infinity (TREE_TYPE (vr_result
.min
));
8464 /* Similarly, if the new maximum is smaller or larger than
8465 the previous one, go all the way to +INF. */
8466 if (cmp_max
< 0 || cmp_max
> 0)
8468 if (!needs_overflow_infinity (TREE_TYPE (vr_result
.max
))
8469 || !vrp_var_may_overflow (lhs
, phi
))
8470 vr_result
.max
= TYPE_MAX_VALUE (TREE_TYPE (vr_result
.max
));
8471 else if (supports_overflow_infinity (TREE_TYPE (vr_result
.max
)))
8473 positive_overflow_infinity (TREE_TYPE (vr_result
.max
));
8476 /* If we dropped either bound to +-INF then if this is a loop
8477 PHI node SCEV may known more about its value-range. */
8478 if ((cmp_min
> 0 || cmp_min
< 0
8479 || cmp_max
< 0 || cmp_max
> 0)
8481 && (l
= loop_containing_stmt (phi
))
8482 && l
->header
== gimple_bb (phi
))
8483 adjust_range_with_scev (&vr_result
, l
, phi
, lhs
);
8485 /* If we will end up with a (-INF, +INF) range, set it to
8486 VARYING. Same if the previous max value was invalid for
8487 the type and we end up with vr_result.min > vr_result.max. */
8488 if ((vrp_val_is_max (vr_result
.max
)
8489 && vrp_val_is_min (vr_result
.min
))
8490 || compare_values (vr_result
.min
,
8495 /* If the new range is different than the previous value, keep
8498 if (update_value_range (lhs
, &vr_result
))
8500 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8502 fprintf (dump_file
, "Found new range for ");
8503 print_generic_expr (dump_file
, lhs
, 0);
8504 fprintf (dump_file
, ": ");
8505 dump_value_range (dump_file
, &vr_result
);
8506 fprintf (dump_file
, "\n\n");
8509 return SSA_PROP_INTERESTING
;
8512 /* Nothing changed, don't add outgoing edges. */
8513 return SSA_PROP_NOT_INTERESTING
;
8515 /* No match found. Set the LHS to VARYING. */
8517 set_value_range_to_varying (lhs_vr
);
8518 return SSA_PROP_VARYING
;
8521 /* Simplify boolean operations if the source is known
8522 to be already a boolean. */
8524 simplify_truth_ops_using_ranges (gimple_stmt_iterator
*gsi
, gimple stmt
)
8526 enum tree_code rhs_code
= gimple_assign_rhs_code (stmt
);
8528 bool need_conversion
;
8530 /* We handle only !=/== case here. */
8531 gcc_assert (rhs_code
== EQ_EXPR
|| rhs_code
== NE_EXPR
);
8533 op0
= gimple_assign_rhs1 (stmt
);
8534 if (!op_with_boolean_value_range_p (op0
))
8537 op1
= gimple_assign_rhs2 (stmt
);
8538 if (!op_with_boolean_value_range_p (op1
))
8541 /* Reduce number of cases to handle to NE_EXPR. As there is no
8542 BIT_XNOR_EXPR we cannot replace A == B with a single statement. */
8543 if (rhs_code
== EQ_EXPR
)
8545 if (TREE_CODE (op1
) == INTEGER_CST
)
8546 op1
= int_const_binop (BIT_XOR_EXPR
, op1
, integer_one_node
);
8551 lhs
= gimple_assign_lhs (stmt
);
8553 = !useless_type_conversion_p (TREE_TYPE (lhs
), TREE_TYPE (op0
));
8555 /* Make sure to not sign-extend a 1-bit 1 when converting the result. */
8557 && !TYPE_UNSIGNED (TREE_TYPE (op0
))
8558 && TYPE_PRECISION (TREE_TYPE (op0
)) == 1
8559 && TYPE_PRECISION (TREE_TYPE (lhs
)) > 1)
8562 /* For A != 0 we can substitute A itself. */
8563 if (integer_zerop (op1
))
8564 gimple_assign_set_rhs_with_ops (gsi
,
8566 ? NOP_EXPR
: TREE_CODE (op0
),
8568 /* For A != B we substitute A ^ B. Either with conversion. */
8569 else if (need_conversion
)
8571 tree tem
= make_ssa_name (TREE_TYPE (op0
), NULL
);
8572 gimple newop
= gimple_build_assign_with_ops (BIT_XOR_EXPR
, tem
, op0
, op1
);
8573 gsi_insert_before (gsi
, newop
, GSI_SAME_STMT
);
8574 gimple_assign_set_rhs_with_ops (gsi
, NOP_EXPR
, tem
, NULL_TREE
);
8578 gimple_assign_set_rhs_with_ops (gsi
, BIT_XOR_EXPR
, op0
, op1
);
8579 update_stmt (gsi_stmt (*gsi
));
8584 /* Simplify a division or modulo operator to a right shift or
8585 bitwise and if the first operand is unsigned or is greater
8586 than zero and the second operand is an exact power of two. */
8589 simplify_div_or_mod_using_ranges (gimple stmt
)
8591 enum tree_code rhs_code
= gimple_assign_rhs_code (stmt
);
8593 tree op0
= gimple_assign_rhs1 (stmt
);
8594 tree op1
= gimple_assign_rhs2 (stmt
);
8595 value_range_t
*vr
= get_value_range (gimple_assign_rhs1 (stmt
));
8597 if (TYPE_UNSIGNED (TREE_TYPE (op0
)))
8599 val
= integer_one_node
;
8605 val
= compare_range_with_value (GE_EXPR
, vr
, integer_zero_node
, &sop
);
8609 && integer_onep (val
)
8610 && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC
))
8612 location_t location
;
8614 if (!gimple_has_location (stmt
))
8615 location
= input_location
;
8617 location
= gimple_location (stmt
);
8618 warning_at (location
, OPT_Wstrict_overflow
,
8619 "assuming signed overflow does not occur when "
8620 "simplifying %</%> or %<%%%> to %<>>%> or %<&%>");
8624 if (val
&& integer_onep (val
))
8628 if (rhs_code
== TRUNC_DIV_EXPR
)
8630 t
= build_int_cst (integer_type_node
, tree_log2 (op1
));
8631 gimple_assign_set_rhs_code (stmt
, RSHIFT_EXPR
);
8632 gimple_assign_set_rhs1 (stmt
, op0
);
8633 gimple_assign_set_rhs2 (stmt
, t
);
8637 t
= build_int_cst (TREE_TYPE (op1
), 1);
8638 t
= int_const_binop (MINUS_EXPR
, op1
, t
);
8639 t
= fold_convert (TREE_TYPE (op0
), t
);
8641 gimple_assign_set_rhs_code (stmt
, BIT_AND_EXPR
);
8642 gimple_assign_set_rhs1 (stmt
, op0
);
8643 gimple_assign_set_rhs2 (stmt
, t
);
8653 /* If the operand to an ABS_EXPR is >= 0, then eliminate the
8654 ABS_EXPR. If the operand is <= 0, then simplify the
8655 ABS_EXPR into a NEGATE_EXPR. */
8658 simplify_abs_using_ranges (gimple stmt
)
8661 tree op
= gimple_assign_rhs1 (stmt
);
8662 tree type
= TREE_TYPE (op
);
8663 value_range_t
*vr
= get_value_range (op
);
8665 if (TYPE_UNSIGNED (type
))
8667 val
= integer_zero_node
;
8673 val
= compare_range_with_value (LE_EXPR
, vr
, integer_zero_node
, &sop
);
8677 val
= compare_range_with_value (GE_EXPR
, vr
, integer_zero_node
,
8682 if (integer_zerop (val
))
8683 val
= integer_one_node
;
8684 else if (integer_onep (val
))
8685 val
= integer_zero_node
;
8690 && (integer_onep (val
) || integer_zerop (val
)))
8692 if (sop
&& issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC
))
8694 location_t location
;
8696 if (!gimple_has_location (stmt
))
8697 location
= input_location
;
8699 location
= gimple_location (stmt
);
8700 warning_at (location
, OPT_Wstrict_overflow
,
8701 "assuming signed overflow does not occur when "
8702 "simplifying %<abs (X)%> to %<X%> or %<-X%>");
8705 gimple_assign_set_rhs1 (stmt
, op
);
8706 if (integer_onep (val
))
8707 gimple_assign_set_rhs_code (stmt
, NEGATE_EXPR
);
8709 gimple_assign_set_rhs_code (stmt
, SSA_NAME
);
8718 /* Optimize away redundant BIT_AND_EXPR and BIT_IOR_EXPR.
8719 If all the bits that are being cleared by & are already
8720 known to be zero from VR, or all the bits that are being
8721 set by | are already known to be one from VR, the bit
8722 operation is redundant. */
8725 simplify_bit_ops_using_ranges (gimple_stmt_iterator
*gsi
, gimple stmt
)
8727 tree op0
= gimple_assign_rhs1 (stmt
);
8728 tree op1
= gimple_assign_rhs2 (stmt
);
8729 tree op
= NULL_TREE
;
8730 value_range_t vr0
= VR_INITIALIZER
;
8731 value_range_t vr1
= VR_INITIALIZER
;
8732 double_int may_be_nonzero0
, may_be_nonzero1
;
8733 double_int must_be_nonzero0
, must_be_nonzero1
;
8736 if (TREE_CODE (op0
) == SSA_NAME
)
8737 vr0
= *(get_value_range (op0
));
8738 else if (is_gimple_min_invariant (op0
))
8739 set_value_range_to_value (&vr0
, op0
, NULL
);
8743 if (TREE_CODE (op1
) == SSA_NAME
)
8744 vr1
= *(get_value_range (op1
));
8745 else if (is_gimple_min_invariant (op1
))
8746 set_value_range_to_value (&vr1
, op1
, NULL
);
8750 if (!zero_nonzero_bits_from_vr (&vr0
, &may_be_nonzero0
, &must_be_nonzero0
))
8752 if (!zero_nonzero_bits_from_vr (&vr1
, &may_be_nonzero1
, &must_be_nonzero1
))
8755 switch (gimple_assign_rhs_code (stmt
))
8758 mask
= may_be_nonzero0
.and_not (must_be_nonzero1
);
8759 if (mask
.is_zero ())
8764 mask
= may_be_nonzero1
.and_not (must_be_nonzero0
);
8765 if (mask
.is_zero ())
8772 mask
= may_be_nonzero0
.and_not (must_be_nonzero1
);
8773 if (mask
.is_zero ())
8778 mask
= may_be_nonzero1
.and_not (must_be_nonzero0
);
8779 if (mask
.is_zero ())
8789 if (op
== NULL_TREE
)
8792 gimple_assign_set_rhs_with_ops (gsi
, TREE_CODE (op
), op
, NULL
);
8793 update_stmt (gsi_stmt (*gsi
));
8797 /* We are comparing trees OP0 and OP1 using COND_CODE. OP0 has
8798 a known value range VR.
8800 If there is one and only one value which will satisfy the
8801 conditional, then return that value. Else return NULL. */
8804 test_for_singularity (enum tree_code cond_code
, tree op0
,
8805 tree op1
, value_range_t
*vr
)
8810 /* Extract minimum/maximum values which satisfy the
8811 the conditional as it was written. */
8812 if (cond_code
== LE_EXPR
|| cond_code
== LT_EXPR
)
8814 /* This should not be negative infinity; there is no overflow
8816 min
= TYPE_MIN_VALUE (TREE_TYPE (op0
));
8819 if (cond_code
== LT_EXPR
&& !is_overflow_infinity (max
))
8821 tree one
= build_int_cst (TREE_TYPE (op0
), 1);
8822 max
= fold_build2 (MINUS_EXPR
, TREE_TYPE (op0
), max
, one
);
8824 TREE_NO_WARNING (max
) = 1;
8827 else if (cond_code
== GE_EXPR
|| cond_code
== GT_EXPR
)
8829 /* This should not be positive infinity; there is no overflow
8831 max
= TYPE_MAX_VALUE (TREE_TYPE (op0
));
8834 if (cond_code
== GT_EXPR
&& !is_overflow_infinity (min
))
8836 tree one
= build_int_cst (TREE_TYPE (op0
), 1);
8837 min
= fold_build2 (PLUS_EXPR
, TREE_TYPE (op0
), min
, one
);
8839 TREE_NO_WARNING (min
) = 1;
8843 /* Now refine the minimum and maximum values using any
8844 value range information we have for op0. */
8847 if (compare_values (vr
->min
, min
) == 1)
8849 if (compare_values (vr
->max
, max
) == -1)
8852 /* If the new min/max values have converged to a single value,
8853 then there is only one value which can satisfy the condition,
8854 return that value. */
8855 if (operand_equal_p (min
, max
, 0) && is_gimple_min_invariant (min
))
8861 /* Return whether the value range *VR fits in an integer type specified
8862 by PRECISION and UNSIGNED_P. */
8865 range_fits_type_p (value_range_t
*vr
, unsigned precision
, bool unsigned_p
)
8868 unsigned src_precision
;
8871 /* We can only handle integral and pointer types. */
8872 src_type
= TREE_TYPE (vr
->min
);
8873 if (!INTEGRAL_TYPE_P (src_type
)
8874 && !POINTER_TYPE_P (src_type
))
8877 /* An extension is fine unless VR is signed and unsigned_p,
8878 and so is an identity transform. */
8879 src_precision
= TYPE_PRECISION (TREE_TYPE (vr
->min
));
8880 if ((src_precision
< precision
8881 && !(unsigned_p
&& !TYPE_UNSIGNED (src_type
)))
8882 || (src_precision
== precision
8883 && TYPE_UNSIGNED (src_type
) == unsigned_p
))
8886 /* Now we can only handle ranges with constant bounds. */
8887 if (vr
->type
!= VR_RANGE
8888 || TREE_CODE (vr
->min
) != INTEGER_CST
8889 || TREE_CODE (vr
->max
) != INTEGER_CST
)
8892 /* For sign changes, the MSB of the double_int has to be clear.
8893 An unsigned value with its MSB set cannot be represented by
8894 a signed double_int, while a negative value cannot be represented
8895 by an unsigned double_int. */
8896 if (TYPE_UNSIGNED (src_type
) != unsigned_p
8897 && (TREE_INT_CST_HIGH (vr
->min
) | TREE_INT_CST_HIGH (vr
->max
)) < 0)
8900 /* Then we can perform the conversion on both ends and compare
8901 the result for equality. */
8902 tem
= tree_to_double_int (vr
->min
).ext (precision
, unsigned_p
);
8903 if (tree_to_double_int (vr
->min
) != tem
)
8905 tem
= tree_to_double_int (vr
->max
).ext (precision
, unsigned_p
);
8906 if (tree_to_double_int (vr
->max
) != tem
)
8912 /* Simplify a conditional using a relational operator to an equality
8913 test if the range information indicates only one value can satisfy
8914 the original conditional. */
8917 simplify_cond_using_ranges (gimple stmt
)
8919 tree op0
= gimple_cond_lhs (stmt
);
8920 tree op1
= gimple_cond_rhs (stmt
);
8921 enum tree_code cond_code
= gimple_cond_code (stmt
);
8923 if (cond_code
!= NE_EXPR
8924 && cond_code
!= EQ_EXPR
8925 && TREE_CODE (op0
) == SSA_NAME
8926 && INTEGRAL_TYPE_P (TREE_TYPE (op0
))
8927 && is_gimple_min_invariant (op1
))
8929 value_range_t
*vr
= get_value_range (op0
);
8931 /* If we have range information for OP0, then we might be
8932 able to simplify this conditional. */
8933 if (vr
->type
== VR_RANGE
)
8935 tree new_tree
= test_for_singularity (cond_code
, op0
, op1
, vr
);
8941 fprintf (dump_file
, "Simplified relational ");
8942 print_gimple_stmt (dump_file
, stmt
, 0, 0);
8943 fprintf (dump_file
, " into ");
8946 gimple_cond_set_code (stmt
, EQ_EXPR
);
8947 gimple_cond_set_lhs (stmt
, op0
);
8948 gimple_cond_set_rhs (stmt
, new_tree
);
8954 print_gimple_stmt (dump_file
, stmt
, 0, 0);
8955 fprintf (dump_file
, "\n");
8961 /* Try again after inverting the condition. We only deal
8962 with integral types here, so no need to worry about
8963 issues with inverting FP comparisons. */
8964 cond_code
= invert_tree_comparison (cond_code
, false);
8965 new_tree
= test_for_singularity (cond_code
, op0
, op1
, vr
);
8971 fprintf (dump_file
, "Simplified relational ");
8972 print_gimple_stmt (dump_file
, stmt
, 0, 0);
8973 fprintf (dump_file
, " into ");
8976 gimple_cond_set_code (stmt
, NE_EXPR
);
8977 gimple_cond_set_lhs (stmt
, op0
);
8978 gimple_cond_set_rhs (stmt
, new_tree
);
8984 print_gimple_stmt (dump_file
, stmt
, 0, 0);
8985 fprintf (dump_file
, "\n");
8993 /* If we have a comparison of an SSA_NAME (OP0) against a constant,
8994 see if OP0 was set by a type conversion where the source of
8995 the conversion is another SSA_NAME with a range that fits
8996 into the range of OP0's type.
8998 If so, the conversion is redundant as the earlier SSA_NAME can be
8999 used for the comparison directly if we just massage the constant in the
9001 if (TREE_CODE (op0
) == SSA_NAME
9002 && TREE_CODE (op1
) == INTEGER_CST
)
9004 gimple def_stmt
= SSA_NAME_DEF_STMT (op0
);
9007 if (!is_gimple_assign (def_stmt
)
9008 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt
)))
9011 innerop
= gimple_assign_rhs1 (def_stmt
);
9013 if (TREE_CODE (innerop
) == SSA_NAME
9014 && !POINTER_TYPE_P (TREE_TYPE (innerop
)))
9016 value_range_t
*vr
= get_value_range (innerop
);
9018 if (range_int_cst_p (vr
)
9019 && range_fits_type_p (vr
,
9020 TYPE_PRECISION (TREE_TYPE (op0
)),
9021 TYPE_UNSIGNED (TREE_TYPE (op0
)))
9022 && int_fits_type_p (op1
, TREE_TYPE (innerop
))
9023 /* The range must not have overflowed, or if it did overflow
9024 we must not be wrapping/trapping overflow and optimizing
9025 with strict overflow semantics. */
9026 && ((!is_negative_overflow_infinity (vr
->min
)
9027 && !is_positive_overflow_infinity (vr
->max
))
9028 || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (innerop
))))
9030 /* If the range overflowed and the user has asked for warnings
9031 when strict overflow semantics were used to optimize code,
9032 issue an appropriate warning. */
9033 if ((is_negative_overflow_infinity (vr
->min
)
9034 || is_positive_overflow_infinity (vr
->max
))
9035 && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_CONDITIONAL
))
9037 location_t location
;
9039 if (!gimple_has_location (stmt
))
9040 location
= input_location
;
9042 location
= gimple_location (stmt
);
9043 warning_at (location
, OPT_Wstrict_overflow
,
9044 "assuming signed overflow does not occur when "
9045 "simplifying conditional");
9048 tree newconst
= fold_convert (TREE_TYPE (innerop
), op1
);
9049 gimple_cond_set_lhs (stmt
, innerop
);
9050 gimple_cond_set_rhs (stmt
, newconst
);
9059 /* Simplify a switch statement using the value range of the switch
9063 simplify_switch_using_ranges (gimple stmt
)
9065 tree op
= gimple_switch_index (stmt
);
9070 size_t i
= 0, j
= 0, n
, n2
;
9073 size_t k
= 1, l
= 0;
9075 if (TREE_CODE (op
) == SSA_NAME
)
9077 vr
= get_value_range (op
);
9079 /* We can only handle integer ranges. */
9080 if ((vr
->type
!= VR_RANGE
9081 && vr
->type
!= VR_ANTI_RANGE
)
9082 || symbolic_range_p (vr
))
9085 /* Find case label for min/max of the value range. */
9086 take_default
= !find_case_label_ranges (stmt
, vr
, &i
, &j
, &k
, &l
);
9088 else if (TREE_CODE (op
) == INTEGER_CST
)
9090 take_default
= !find_case_label_index (stmt
, 1, op
, &i
);
9104 n
= gimple_switch_num_labels (stmt
);
9106 /* Bail out if this is just all edges taken. */
9112 /* Build a new vector of taken case labels. */
9113 vec2
= make_tree_vec (j
- i
+ 1 + l
- k
+ 1 + (int)take_default
);
9116 /* Add the default edge, if necessary. */
9118 TREE_VEC_ELT (vec2
, n2
++) = gimple_switch_default_label (stmt
);
9120 for (; i
<= j
; ++i
, ++n2
)
9121 TREE_VEC_ELT (vec2
, n2
) = gimple_switch_label (stmt
, i
);
9123 for (; k
<= l
; ++k
, ++n2
)
9124 TREE_VEC_ELT (vec2
, n2
) = gimple_switch_label (stmt
, k
);
9126 /* Mark needed edges. */
9127 for (i
= 0; i
< n2
; ++i
)
9129 e
= find_edge (gimple_bb (stmt
),
9130 label_to_block (CASE_LABEL (TREE_VEC_ELT (vec2
, i
))));
9131 e
->aux
= (void *)-1;
9134 /* Queue not needed edges for later removal. */
9135 FOR_EACH_EDGE (e
, ei
, gimple_bb (stmt
)->succs
)
9137 if (e
->aux
== (void *)-1)
9143 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
9145 fprintf (dump_file
, "removing unreachable case label\n");
9147 to_remove_edges
.safe_push (e
);
9148 e
->flags
&= ~EDGE_EXECUTABLE
;
9151 /* And queue an update for the stmt. */
9154 to_update_switch_stmts
.safe_push (su
);
9158 /* Simplify an integral conversion from an SSA name in STMT. */
9161 simplify_conversion_using_ranges (gimple stmt
)
9163 tree innerop
, middleop
, finaltype
;
9165 value_range_t
*innervr
;
9166 bool inner_unsigned_p
, middle_unsigned_p
, final_unsigned_p
;
9167 unsigned inner_prec
, middle_prec
, final_prec
;
9168 double_int innermin
, innermed
, innermax
, middlemin
, middlemed
, middlemax
;
9170 finaltype
= TREE_TYPE (gimple_assign_lhs (stmt
));
9171 if (!INTEGRAL_TYPE_P (finaltype
))
9173 middleop
= gimple_assign_rhs1 (stmt
);
9174 def_stmt
= SSA_NAME_DEF_STMT (middleop
);
9175 if (!is_gimple_assign (def_stmt
)
9176 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt
)))
9178 innerop
= gimple_assign_rhs1 (def_stmt
);
9179 if (TREE_CODE (innerop
) != SSA_NAME
9180 || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (innerop
))
9183 /* Get the value-range of the inner operand. */
9184 innervr
= get_value_range (innerop
);
9185 if (innervr
->type
!= VR_RANGE
9186 || TREE_CODE (innervr
->min
) != INTEGER_CST
9187 || TREE_CODE (innervr
->max
) != INTEGER_CST
)
9190 /* Simulate the conversion chain to check if the result is equal if
9191 the middle conversion is removed. */
9192 innermin
= tree_to_double_int (innervr
->min
);
9193 innermax
= tree_to_double_int (innervr
->max
);
9195 inner_prec
= TYPE_PRECISION (TREE_TYPE (innerop
));
9196 middle_prec
= TYPE_PRECISION (TREE_TYPE (middleop
));
9197 final_prec
= TYPE_PRECISION (finaltype
);
9199 /* If the first conversion is not injective, the second must not
9201 if ((innermax
- innermin
).ugt (double_int::mask (middle_prec
))
9202 && middle_prec
< final_prec
)
9204 /* We also want a medium value so that we can track the effect that
9205 narrowing conversions with sign change have. */
9206 inner_unsigned_p
= TYPE_UNSIGNED (TREE_TYPE (innerop
));
9207 if (inner_unsigned_p
)
9208 innermed
= double_int::mask (inner_prec
).lrshift (1, inner_prec
);
9210 innermed
= double_int_zero
;
9211 if (innermin
.cmp (innermed
, inner_unsigned_p
) >= 0
9212 || innermed
.cmp (innermax
, inner_unsigned_p
) >= 0)
9213 innermed
= innermin
;
9215 middle_unsigned_p
= TYPE_UNSIGNED (TREE_TYPE (middleop
));
9216 middlemin
= innermin
.ext (middle_prec
, middle_unsigned_p
);
9217 middlemed
= innermed
.ext (middle_prec
, middle_unsigned_p
);
9218 middlemax
= innermax
.ext (middle_prec
, middle_unsigned_p
);
9220 /* Require that the final conversion applied to both the original
9221 and the intermediate range produces the same result. */
9222 final_unsigned_p
= TYPE_UNSIGNED (finaltype
);
9223 if (middlemin
.ext (final_prec
, final_unsigned_p
)
9224 != innermin
.ext (final_prec
, final_unsigned_p
)
9225 || middlemed
.ext (final_prec
, final_unsigned_p
)
9226 != innermed
.ext (final_prec
, final_unsigned_p
)
9227 || middlemax
.ext (final_prec
, final_unsigned_p
)
9228 != innermax
.ext (final_prec
, final_unsigned_p
))
9231 gimple_assign_set_rhs1 (stmt
, innerop
);
9236 /* Simplify a conversion from integral SSA name to float in STMT. */
9239 simplify_float_conversion_using_ranges (gimple_stmt_iterator
*gsi
, gimple stmt
)
9241 tree rhs1
= gimple_assign_rhs1 (stmt
);
9242 value_range_t
*vr
= get_value_range (rhs1
);
9243 enum machine_mode fltmode
= TYPE_MODE (TREE_TYPE (gimple_assign_lhs (stmt
)));
9244 enum machine_mode mode
;
9248 /* We can only handle constant ranges. */
9249 if (vr
->type
!= VR_RANGE
9250 || TREE_CODE (vr
->min
) != INTEGER_CST
9251 || TREE_CODE (vr
->max
) != INTEGER_CST
)
9254 /* First check if we can use a signed type in place of an unsigned. */
9255 if (TYPE_UNSIGNED (TREE_TYPE (rhs1
))
9256 && (can_float_p (fltmode
, TYPE_MODE (TREE_TYPE (rhs1
)), 0)
9257 != CODE_FOR_nothing
)
9258 && range_fits_type_p (vr
, GET_MODE_PRECISION
9259 (TYPE_MODE (TREE_TYPE (rhs1
))), 0))
9260 mode
= TYPE_MODE (TREE_TYPE (rhs1
));
9261 /* If we can do the conversion in the current input mode do nothing. */
9262 else if (can_float_p (fltmode
, TYPE_MODE (TREE_TYPE (rhs1
)),
9263 TYPE_UNSIGNED (TREE_TYPE (rhs1
))) != CODE_FOR_nothing
)
9265 /* Otherwise search for a mode we can use, starting from the narrowest
9266 integer mode available. */
9269 mode
= GET_CLASS_NARROWEST_MODE (MODE_INT
);
9272 /* If we cannot do a signed conversion to float from mode
9273 or if the value-range does not fit in the signed type
9274 try with a wider mode. */
9275 if (can_float_p (fltmode
, mode
, 0) != CODE_FOR_nothing
9276 && range_fits_type_p (vr
, GET_MODE_PRECISION (mode
), 0))
9279 mode
= GET_MODE_WIDER_MODE (mode
);
9280 /* But do not widen the input. Instead leave that to the
9281 optabs expansion code. */
9282 if (GET_MODE_PRECISION (mode
) > TYPE_PRECISION (TREE_TYPE (rhs1
)))
9285 while (mode
!= VOIDmode
);
9286 if (mode
== VOIDmode
)
9290 /* It works, insert a truncation or sign-change before the
9291 float conversion. */
9292 tem
= make_ssa_name (build_nonstandard_integer_type
9293 (GET_MODE_PRECISION (mode
), 0), NULL
);
9294 conv
= gimple_build_assign_with_ops (NOP_EXPR
, tem
, rhs1
, NULL_TREE
);
9295 gsi_insert_before (gsi
, conv
, GSI_SAME_STMT
);
9296 gimple_assign_set_rhs1 (stmt
, tem
);
9302 /* Simplify STMT using ranges if possible. */
9305 simplify_stmt_using_ranges (gimple_stmt_iterator
*gsi
)
9307 gimple stmt
= gsi_stmt (*gsi
);
9308 if (is_gimple_assign (stmt
))
9310 enum tree_code rhs_code
= gimple_assign_rhs_code (stmt
);
9311 tree rhs1
= gimple_assign_rhs1 (stmt
);
9317 /* Transform EQ_EXPR, NE_EXPR into BIT_XOR_EXPR or identity
9318 if the RHS is zero or one, and the LHS are known to be boolean
9320 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
9321 return simplify_truth_ops_using_ranges (gsi
, stmt
);
9324 /* Transform TRUNC_DIV_EXPR and TRUNC_MOD_EXPR into RSHIFT_EXPR
9325 and BIT_AND_EXPR respectively if the first operand is greater
9326 than zero and the second operand is an exact power of two. */
9327 case TRUNC_DIV_EXPR
:
9328 case TRUNC_MOD_EXPR
:
9329 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1
))
9330 && integer_pow2p (gimple_assign_rhs2 (stmt
)))
9331 return simplify_div_or_mod_using_ranges (stmt
);
9334 /* Transform ABS (X) into X or -X as appropriate. */
9336 if (TREE_CODE (rhs1
) == SSA_NAME
9337 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
9338 return simplify_abs_using_ranges (stmt
);
9343 /* Optimize away BIT_AND_EXPR and BIT_IOR_EXPR
9344 if all the bits being cleared are already cleared or
9345 all the bits being set are already set. */
9346 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
9347 return simplify_bit_ops_using_ranges (gsi
, stmt
);
9351 if (TREE_CODE (rhs1
) == SSA_NAME
9352 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
9353 return simplify_conversion_using_ranges (stmt
);
9357 if (TREE_CODE (rhs1
) == SSA_NAME
9358 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
9359 return simplify_float_conversion_using_ranges (gsi
, stmt
);
9366 else if (gimple_code (stmt
) == GIMPLE_COND
)
9367 return simplify_cond_using_ranges (stmt
);
9368 else if (gimple_code (stmt
) == GIMPLE_SWITCH
)
9369 return simplify_switch_using_ranges (stmt
);
9374 /* If the statement pointed by SI has a predicate whose value can be
9375 computed using the value range information computed by VRP, compute
9376 its value and return true. Otherwise, return false. */
9379 fold_predicate_in (gimple_stmt_iterator
*si
)
9381 bool assignment_p
= false;
9383 gimple stmt
= gsi_stmt (*si
);
9385 if (is_gimple_assign (stmt
)
9386 && TREE_CODE_CLASS (gimple_assign_rhs_code (stmt
)) == tcc_comparison
)
9388 assignment_p
= true;
9389 val
= vrp_evaluate_conditional (gimple_assign_rhs_code (stmt
),
9390 gimple_assign_rhs1 (stmt
),
9391 gimple_assign_rhs2 (stmt
),
9394 else if (gimple_code (stmt
) == GIMPLE_COND
)
9395 val
= vrp_evaluate_conditional (gimple_cond_code (stmt
),
9396 gimple_cond_lhs (stmt
),
9397 gimple_cond_rhs (stmt
),
9405 val
= fold_convert (gimple_expr_type (stmt
), val
);
9409 fprintf (dump_file
, "Folding predicate ");
9410 print_gimple_expr (dump_file
, stmt
, 0, 0);
9411 fprintf (dump_file
, " to ");
9412 print_generic_expr (dump_file
, val
, 0);
9413 fprintf (dump_file
, "\n");
9416 if (is_gimple_assign (stmt
))
9417 gimple_assign_set_rhs_from_tree (si
, val
);
9420 gcc_assert (gimple_code (stmt
) == GIMPLE_COND
);
9421 if (integer_zerop (val
))
9422 gimple_cond_make_false (stmt
);
9423 else if (integer_onep (val
))
9424 gimple_cond_make_true (stmt
);
9435 /* Callback for substitute_and_fold folding the stmt at *SI. */
9438 vrp_fold_stmt (gimple_stmt_iterator
*si
)
9440 if (fold_predicate_in (si
))
9443 return simplify_stmt_using_ranges (si
);
9446 /* Stack of dest,src equivalency pairs that need to be restored after
9447 each attempt to thread a block's incoming edge to an outgoing edge.
9449 A NULL entry is used to mark the end of pairs which need to be
9451 static vec
<tree
> equiv_stack
;
9453 /* A trivial wrapper so that we can present the generic jump threading
9454 code with a simple API for simplifying statements. STMT is the
9455 statement we want to simplify, WITHIN_STMT provides the location
9456 for any overflow warnings. */
9459 simplify_stmt_for_jump_threading (gimple stmt
, gimple within_stmt
)
9461 if (gimple_code (stmt
) == GIMPLE_COND
)
9462 return vrp_evaluate_conditional (gimple_cond_code (stmt
),
9463 gimple_cond_lhs (stmt
),
9464 gimple_cond_rhs (stmt
), within_stmt
);
9466 if (gimple_code (stmt
) == GIMPLE_ASSIGN
)
9468 value_range_t new_vr
= VR_INITIALIZER
;
9469 tree lhs
= gimple_assign_lhs (stmt
);
9471 if (TREE_CODE (lhs
) == SSA_NAME
9472 && (INTEGRAL_TYPE_P (TREE_TYPE (lhs
))
9473 || POINTER_TYPE_P (TREE_TYPE (lhs
))))
9475 extract_range_from_assignment (&new_vr
, stmt
);
9476 if (range_int_cst_singleton_p (&new_vr
))
9484 /* Blocks which have more than one predecessor and more than
9485 one successor present jump threading opportunities, i.e.,
9486 when the block is reached from a specific predecessor, we
9487 may be able to determine which of the outgoing edges will
9488 be traversed. When this optimization applies, we are able
9489 to avoid conditionals at runtime and we may expose secondary
9490 optimization opportunities.
9492 This routine is effectively a driver for the generic jump
9493 threading code. It basically just presents the generic code
9494 with edges that may be suitable for jump threading.
9496 Unlike DOM, we do not iterate VRP if jump threading was successful.
9497 While iterating may expose new opportunities for VRP, it is expected
9498 those opportunities would be very limited and the compile time cost
9499 to expose those opportunities would be significant.
9501 As jump threading opportunities are discovered, they are registered
9502 for later realization. */
9505 identify_jump_threads (void)
9512 /* Ugh. When substituting values earlier in this pass we can
9513 wipe the dominance information. So rebuild the dominator
9514 information as we need it within the jump threading code. */
9515 calculate_dominance_info (CDI_DOMINATORS
);
9517 /* We do not allow VRP information to be used for jump threading
9518 across a back edge in the CFG. Otherwise it becomes too
9519 difficult to avoid eliminating loop exit tests. Of course
9520 EDGE_DFS_BACK is not accurate at this time so we have to
9522 mark_dfs_back_edges ();
9524 /* Do not thread across edges we are about to remove. Just marking
9525 them as EDGE_DFS_BACK will do. */
9526 FOR_EACH_VEC_ELT (to_remove_edges
, i
, e
)
9527 e
->flags
|= EDGE_DFS_BACK
;
9529 /* Allocate our unwinder stack to unwind any temporary equivalences
9530 that might be recorded. */
9531 equiv_stack
.create (20);
9533 /* To avoid lots of silly node creation, we create a single
9534 conditional and just modify it in-place when attempting to
9536 dummy
= gimple_build_cond (EQ_EXPR
,
9537 integer_zero_node
, integer_zero_node
,
9540 /* Walk through all the blocks finding those which present a
9541 potential jump threading opportunity. We could set this up
9542 as a dominator walker and record data during the walk, but
9543 I doubt it's worth the effort for the classes of jump
9544 threading opportunities we are trying to identify at this
9545 point in compilation. */
9550 /* If the generic jump threading code does not find this block
9551 interesting, then there is nothing to do. */
9552 if (! potentially_threadable_block (bb
))
9555 /* We only care about blocks ending in a COND_EXPR. While there
9556 may be some value in handling SWITCH_EXPR here, I doubt it's
9557 terribly important. */
9558 last
= gsi_stmt (gsi_last_bb (bb
));
9560 /* We're basically looking for a switch or any kind of conditional with
9561 integral or pointer type arguments. Note the type of the second
9562 argument will be the same as the first argument, so no need to
9563 check it explicitly. */
9564 if (gimple_code (last
) == GIMPLE_SWITCH
9565 || (gimple_code (last
) == GIMPLE_COND
9566 && TREE_CODE (gimple_cond_lhs (last
)) == SSA_NAME
9567 && (INTEGRAL_TYPE_P (TREE_TYPE (gimple_cond_lhs (last
)))
9568 || POINTER_TYPE_P (TREE_TYPE (gimple_cond_lhs (last
))))
9569 && (TREE_CODE (gimple_cond_rhs (last
)) == SSA_NAME
9570 || is_gimple_min_invariant (gimple_cond_rhs (last
)))))
9574 /* We've got a block with multiple predecessors and multiple
9575 successors which also ends in a suitable conditional or
9576 switch statement. For each predecessor, see if we can thread
9577 it to a specific successor. */
9578 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
9580 /* Do not thread across back edges or abnormal edges
9582 if (e
->flags
& (EDGE_DFS_BACK
| EDGE_COMPLEX
))
9585 thread_across_edge (dummy
, e
, true, &equiv_stack
,
9586 simplify_stmt_for_jump_threading
);
9591 /* We do not actually update the CFG or SSA graphs at this point as
9592 ASSERT_EXPRs are still in the IL and cfg cleanup code does not yet
9593 handle ASSERT_EXPRs gracefully. */
9596 /* We identified all the jump threading opportunities earlier, but could
9597 not transform the CFG at that time. This routine transforms the
9598 CFG and arranges for the dominator tree to be rebuilt if necessary.
9600 Note the SSA graph update will occur during the normal TODO
9601 processing by the pass manager. */
9603 finalize_jump_threads (void)
9605 thread_through_all_blocks (false);
9606 equiv_stack
.release ();
9610 /* Traverse all the blocks folding conditionals with known ranges. */
9617 values_propagated
= true;
9621 fprintf (dump_file
, "\nValue ranges after VRP:\n\n");
9622 dump_all_value_ranges (dump_file
);
9623 fprintf (dump_file
, "\n");
9626 substitute_and_fold (op_with_constant_singleton_value_range
,
9627 vrp_fold_stmt
, false);
9629 if (warn_array_bounds
)
9630 check_all_array_refs ();
9632 /* We must identify jump threading opportunities before we release
9633 the datastructures built by VRP. */
9634 identify_jump_threads ();
9636 /* Set value range to non pointer SSA_NAMEs. */
9637 for (i
= 0; i
< num_vr_values
; i
++)
9640 tree name
= ssa_name (i
);
9643 || POINTER_TYPE_P (TREE_TYPE (name
))
9644 || (vr_value
[i
]->type
== VR_VARYING
)
9645 || (vr_value
[i
]->type
== VR_UNDEFINED
))
9648 if ((TREE_CODE (vr_value
[i
]->min
) == INTEGER_CST
)
9649 && (TREE_CODE (vr_value
[i
]->max
) == INTEGER_CST
)
9650 && (vr_value
[i
]->type
== VR_RANGE
9651 || vr_value
[i
]->type
== VR_ANTI_RANGE
))
9652 set_range_info (name
, vr_value
[i
]->type
,
9653 tree_to_double_int (vr_value
[i
]->min
),
9654 tree_to_double_int (vr_value
[i
]->max
));
9657 /* Free allocated memory. */
9658 for (i
= 0; i
< num_vr_values
; i
++)
9661 BITMAP_FREE (vr_value
[i
]->equiv
);
9666 free (vr_phi_edge_counts
);
9668 /* So that we can distinguish between VRP data being available
9669 and not available. */
9671 vr_phi_edge_counts
= NULL
;
9675 /* Main entry point to VRP (Value Range Propagation). This pass is
9676 loosely based on J. R. C. Patterson, ``Accurate Static Branch
9677 Prediction by Value Range Propagation,'' in SIGPLAN Conference on
9678 Programming Language Design and Implementation, pp. 67-78, 1995.
9679 Also available at http://citeseer.ist.psu.edu/patterson95accurate.html
9681 This is essentially an SSA-CCP pass modified to deal with ranges
9682 instead of constants.
9684 While propagating ranges, we may find that two or more SSA name
9685 have equivalent, though distinct ranges. For instance,
9688 2 p_4 = ASSERT_EXPR <p_3, p_3 != 0>
9690 4 p_5 = ASSERT_EXPR <p_4, p_4 == q_2>;
9694 In the code above, pointer p_5 has range [q_2, q_2], but from the
9695 code we can also determine that p_5 cannot be NULL and, if q_2 had
9696 a non-varying range, p_5's range should also be compatible with it.
9698 These equivalences are created by two expressions: ASSERT_EXPR and
9699 copy operations. Since p_5 is an assertion on p_4, and p_4 was the
9700 result of another assertion, then we can use the fact that p_5 and
9701 p_4 are equivalent when evaluating p_5's range.
9703 Together with value ranges, we also propagate these equivalences
9704 between names so that we can take advantage of information from
9705 multiple ranges when doing final replacement. Note that this
9706 equivalency relation is transitive but not symmetric.
9708 In the example above, p_5 is equivalent to p_4, q_2 and p_3, but we
9709 cannot assert that q_2 is equivalent to p_5 because q_2 may be used
9710 in contexts where that assertion does not hold (e.g., in line 6).
9712 TODO, the main difference between this pass and Patterson's is that
9713 we do not propagate edge probabilities. We only compute whether
9714 edges can be taken or not. That is, instead of having a spectrum
9715 of jump probabilities between 0 and 1, we only deal with 0, 1 and
9716 DON'T KNOW. In the future, it may be worthwhile to propagate
9717 probabilities to aid branch prediction. */
9726 loop_optimizer_init (LOOPS_NORMAL
| LOOPS_HAVE_RECORDED_EXITS
);
9727 rewrite_into_loop_closed_ssa (NULL
, TODO_update_ssa
);
9730 /* ??? This ends up using stale EDGE_DFS_BACK for liveness computation.
9731 Inserting assertions may split edges which will invalidate
9733 insert_range_assertions ();
9735 to_remove_edges
.create (10);
9736 to_update_switch_stmts
.create (5);
9737 threadedge_initialize_values ();
9739 /* For visiting PHI nodes we need EDGE_DFS_BACK computed. */
9740 mark_dfs_back_edges ();
9743 ssa_propagate (vrp_visit_stmt
, vrp_visit_phi_node
);
9746 free_numbers_of_iterations_estimates ();
9748 /* ASSERT_EXPRs must be removed before finalizing jump threads
9749 as finalizing jump threads calls the CFG cleanup code which
9750 does not properly handle ASSERT_EXPRs. */
9751 remove_range_assertions ();
9753 /* If we exposed any new variables, go ahead and put them into
9754 SSA form now, before we handle jump threading. This simplifies
9755 interactions between rewriting of _DECL nodes into SSA form
9756 and rewriting SSA_NAME nodes into SSA form after block
9757 duplication and CFG manipulation. */
9758 update_ssa (TODO_update_ssa
);
9760 finalize_jump_threads ();
9762 /* Remove dead edges from SWITCH_EXPR optimization. This leaves the
9763 CFG in a broken state and requires a cfg_cleanup run. */
9764 FOR_EACH_VEC_ELT (to_remove_edges
, i
, e
)
9766 /* Update SWITCH_EXPR case label vector. */
9767 FOR_EACH_VEC_ELT (to_update_switch_stmts
, i
, su
)
9770 size_t n
= TREE_VEC_LENGTH (su
->vec
);
9772 gimple_switch_set_num_labels (su
->stmt
, n
);
9773 for (j
= 0; j
< n
; j
++)
9774 gimple_switch_set_label (su
->stmt
, j
, TREE_VEC_ELT (su
->vec
, j
));
9775 /* As we may have replaced the default label with a regular one
9776 make sure to make it a real default label again. This ensures
9777 optimal expansion. */
9778 label
= gimple_switch_label (su
->stmt
, 0);
9779 CASE_LOW (label
) = NULL_TREE
;
9780 CASE_HIGH (label
) = NULL_TREE
;
9783 if (to_remove_edges
.length () > 0)
9785 free_dominance_info (CDI_DOMINATORS
);
9787 loops_state_set (LOOPS_NEED_FIXUP
);
9790 to_remove_edges
.release ();
9791 to_update_switch_stmts
.release ();
9792 threadedge_finalize_values ();
9795 loop_optimizer_finalize ();
9802 return flag_tree_vrp
!= 0;
9807 const pass_data pass_data_vrp
=
9809 GIMPLE_PASS
, /* type */
9811 OPTGROUP_NONE
, /* optinfo_flags */
9812 true, /* has_gate */
9813 true, /* has_execute */
9814 TV_TREE_VRP
, /* tv_id */
9815 PROP_ssa
, /* properties_required */
9816 0, /* properties_provided */
9817 0, /* properties_destroyed */
9818 0, /* todo_flags_start */
9819 ( TODO_cleanup_cfg
| TODO_update_ssa
9821 | TODO_verify_flow
), /* todo_flags_finish */
9824 class pass_vrp
: public gimple_opt_pass
9827 pass_vrp (gcc::context
*ctxt
)
9828 : gimple_opt_pass (pass_data_vrp
, ctxt
)
9831 /* opt_pass methods: */
9832 opt_pass
* clone () { return new pass_vrp (m_ctxt
); }
9833 bool gate () { return gate_vrp (); }
9834 unsigned int execute () { return execute_vrp (); }
9836 }; // class pass_vrp
9841 make_pass_vrp (gcc::context
*ctxt
)
9843 return new pass_vrp (ctxt
);