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"
28 #include "basic-block.h"
30 #include "gimple-iterator.h"
31 #include "gimple-walk.h"
32 #include "gimple-ssa.h"
34 #include "tree-phinodes.h"
35 #include "ssa-iterators.h"
36 #include "tree-ssanames.h"
37 #include "tree-ssa-loop-manip.h"
38 #include "tree-ssa-loop-niter.h"
39 #include "tree-ssa-loop.h"
40 #include "tree-into-ssa.h"
42 #include "tree-pass.h"
43 #include "tree-dump.h"
44 #include "gimple-pretty-print.h"
45 #include "diagnostic-core.h"
48 #include "tree-scalar-evolution.h"
49 #include "tree-ssa-propagate.h"
50 #include "tree-chrec.h"
51 #include "tree-ssa-threadupdate.h"
54 #include "tree-ssa-threadedge.h"
58 /* Range of values that can be associated with an SSA_NAME after VRP
62 /* Lattice value represented by this range. */
63 enum value_range_type type
;
65 /* Minimum and maximum values represented by this range. These
66 values should be interpreted as follows:
68 - If TYPE is VR_UNDEFINED or VR_VARYING then MIN and MAX must
71 - If TYPE == VR_RANGE then MIN holds the minimum value and
72 MAX holds the maximum value of the range [MIN, MAX].
74 - If TYPE == ANTI_RANGE the variable is known to NOT
75 take any values in the range [MIN, MAX]. */
79 /* Set of SSA names whose value ranges are equivalent to this one.
80 This set is only valid when TYPE is VR_RANGE or VR_ANTI_RANGE. */
84 typedef struct value_range_d value_range_t
;
86 #define VR_INITIALIZER { VR_UNDEFINED, NULL_TREE, NULL_TREE, NULL }
88 /* Set of SSA names found live during the RPO traversal of the function
89 for still active basic-blocks. */
92 /* Return true if the SSA name NAME is live on the edge E. */
95 live_on_edge (edge e
, tree name
)
97 return (live
[e
->dest
->index
]
98 && bitmap_bit_p (live
[e
->dest
->index
], SSA_NAME_VERSION (name
)));
101 /* Local functions. */
102 static int compare_values (tree val1
, tree val2
);
103 static int compare_values_warnv (tree val1
, tree val2
, bool *);
104 static void vrp_meet (value_range_t
*, value_range_t
*);
105 static void vrp_intersect_ranges (value_range_t
*, value_range_t
*);
106 static tree
vrp_evaluate_conditional_warnv_with_ops (enum tree_code
,
107 tree
, tree
, bool, bool *,
110 /* Location information for ASSERT_EXPRs. Each instance of this
111 structure describes an ASSERT_EXPR for an SSA name. Since a single
112 SSA name may have more than one assertion associated with it, these
113 locations are kept in a linked list attached to the corresponding
115 struct assert_locus_d
117 /* Basic block where the assertion would be inserted. */
120 /* Some assertions need to be inserted on an edge (e.g., assertions
121 generated by COND_EXPRs). In those cases, BB will be NULL. */
124 /* Pointer to the statement that generated this assertion. */
125 gimple_stmt_iterator si
;
127 /* Predicate code for the ASSERT_EXPR. Must be COMPARISON_CLASS_P. */
128 enum tree_code comp_code
;
130 /* Value being compared against. */
133 /* Expression to compare. */
136 /* Next node in the linked list. */
137 struct assert_locus_d
*next
;
140 typedef struct assert_locus_d
*assert_locus_t
;
142 /* If bit I is present, it means that SSA name N_i has a list of
143 assertions that should be inserted in the IL. */
144 static bitmap need_assert_for
;
146 /* Array of locations lists where to insert assertions. ASSERTS_FOR[I]
147 holds a list of ASSERT_LOCUS_T nodes that describe where
148 ASSERT_EXPRs for SSA name N_I should be inserted. */
149 static assert_locus_t
*asserts_for
;
151 /* Value range array. After propagation, VR_VALUE[I] holds the range
152 of values that SSA name N_I may take. */
153 static unsigned num_vr_values
;
154 static value_range_t
**vr_value
;
155 static bool values_propagated
;
157 /* For a PHI node which sets SSA name N_I, VR_COUNTS[I] holds the
158 number of executable edges we saw the last time we visited the
160 static int *vr_phi_edge_counts
;
167 static vec
<edge
> to_remove_edges
;
168 static vec
<switch_update
> to_update_switch_stmts
;
171 /* Return the maximum value for TYPE. */
174 vrp_val_max (const_tree type
)
176 if (!INTEGRAL_TYPE_P (type
))
179 return TYPE_MAX_VALUE (type
);
182 /* Return the minimum value for TYPE. */
185 vrp_val_min (const_tree type
)
187 if (!INTEGRAL_TYPE_P (type
))
190 return TYPE_MIN_VALUE (type
);
193 /* Return whether VAL is equal to the maximum value of its type. This
194 will be true for a positive overflow infinity. We can't do a
195 simple equality comparison with TYPE_MAX_VALUE because C typedefs
196 and Ada subtypes can produce types whose TYPE_MAX_VALUE is not ==
197 to the integer constant with the same value in the type. */
200 vrp_val_is_max (const_tree val
)
202 tree type_max
= vrp_val_max (TREE_TYPE (val
));
203 return (val
== type_max
204 || (type_max
!= NULL_TREE
205 && operand_equal_p (val
, type_max
, 0)));
208 /* Return whether VAL is equal to the minimum value of its type. This
209 will be true for a negative overflow infinity. */
212 vrp_val_is_min (const_tree val
)
214 tree type_min
= vrp_val_min (TREE_TYPE (val
));
215 return (val
== type_min
216 || (type_min
!= NULL_TREE
217 && operand_equal_p (val
, type_min
, 0)));
221 /* Return whether TYPE should use an overflow infinity distinct from
222 TYPE_{MIN,MAX}_VALUE. We use an overflow infinity value to
223 represent a signed overflow during VRP computations. An infinity
224 is distinct from a half-range, which will go from some number to
225 TYPE_{MIN,MAX}_VALUE. */
228 needs_overflow_infinity (const_tree type
)
230 return INTEGRAL_TYPE_P (type
) && !TYPE_OVERFLOW_WRAPS (type
);
233 /* Return whether TYPE can support our overflow infinity
234 representation: we use the TREE_OVERFLOW flag, which only exists
235 for constants. If TYPE doesn't support this, we don't optimize
236 cases which would require signed overflow--we drop them to
240 supports_overflow_infinity (const_tree type
)
242 tree min
= vrp_val_min (type
), max
= vrp_val_max (type
);
243 #ifdef ENABLE_CHECKING
244 gcc_assert (needs_overflow_infinity (type
));
246 return (min
!= NULL_TREE
247 && CONSTANT_CLASS_P (min
)
249 && CONSTANT_CLASS_P (max
));
252 /* VAL is the maximum or minimum value of a type. Return a
253 corresponding overflow infinity. */
256 make_overflow_infinity (tree val
)
258 gcc_checking_assert (val
!= NULL_TREE
&& CONSTANT_CLASS_P (val
));
259 val
= copy_node (val
);
260 TREE_OVERFLOW (val
) = 1;
264 /* Return a negative overflow infinity for TYPE. */
267 negative_overflow_infinity (tree type
)
269 gcc_checking_assert (supports_overflow_infinity (type
));
270 return make_overflow_infinity (vrp_val_min (type
));
273 /* Return a positive overflow infinity for TYPE. */
276 positive_overflow_infinity (tree type
)
278 gcc_checking_assert (supports_overflow_infinity (type
));
279 return make_overflow_infinity (vrp_val_max (type
));
282 /* Return whether VAL is a negative overflow infinity. */
285 is_negative_overflow_infinity (const_tree val
)
287 return (needs_overflow_infinity (TREE_TYPE (val
))
288 && CONSTANT_CLASS_P (val
)
289 && TREE_OVERFLOW (val
)
290 && vrp_val_is_min (val
));
293 /* Return whether VAL is a positive overflow infinity. */
296 is_positive_overflow_infinity (const_tree val
)
298 return (needs_overflow_infinity (TREE_TYPE (val
))
299 && CONSTANT_CLASS_P (val
)
300 && TREE_OVERFLOW (val
)
301 && vrp_val_is_max (val
));
304 /* Return whether VAL is a positive or negative overflow infinity. */
307 is_overflow_infinity (const_tree val
)
309 return (needs_overflow_infinity (TREE_TYPE (val
))
310 && CONSTANT_CLASS_P (val
)
311 && TREE_OVERFLOW (val
)
312 && (vrp_val_is_min (val
) || vrp_val_is_max (val
)));
315 /* Return whether STMT has a constant rhs that is_overflow_infinity. */
318 stmt_overflow_infinity (gimple stmt
)
320 if (is_gimple_assign (stmt
)
321 && get_gimple_rhs_class (gimple_assign_rhs_code (stmt
)) ==
323 return is_overflow_infinity (gimple_assign_rhs1 (stmt
));
327 /* If VAL is now an overflow infinity, return VAL. Otherwise, return
328 the same value with TREE_OVERFLOW clear. This can be used to avoid
329 confusing a regular value with an overflow value. */
332 avoid_overflow_infinity (tree val
)
334 if (!is_overflow_infinity (val
))
337 if (vrp_val_is_max (val
))
338 return vrp_val_max (TREE_TYPE (val
));
341 gcc_checking_assert (vrp_val_is_min (val
));
342 return vrp_val_min (TREE_TYPE (val
));
347 /* Return true if ARG is marked with the nonnull attribute in the
348 current function signature. */
351 nonnull_arg_p (const_tree arg
)
353 tree t
, attrs
, fntype
;
354 unsigned HOST_WIDE_INT arg_num
;
356 gcc_assert (TREE_CODE (arg
) == PARM_DECL
&& POINTER_TYPE_P (TREE_TYPE (arg
)));
358 /* The static chain decl is always non null. */
359 if (arg
== cfun
->static_chain_decl
)
362 fntype
= TREE_TYPE (current_function_decl
);
363 for (attrs
= TYPE_ATTRIBUTES (fntype
); attrs
; attrs
= TREE_CHAIN (attrs
))
365 attrs
= lookup_attribute ("nonnull", attrs
);
367 /* If "nonnull" wasn't specified, we know nothing about the argument. */
368 if (attrs
== NULL_TREE
)
371 /* If "nonnull" applies to all the arguments, then ARG is non-null. */
372 if (TREE_VALUE (attrs
) == NULL_TREE
)
375 /* Get the position number for ARG in the function signature. */
376 for (arg_num
= 1, t
= DECL_ARGUMENTS (current_function_decl
);
378 t
= DECL_CHAIN (t
), arg_num
++)
384 gcc_assert (t
== arg
);
386 /* Now see if ARG_NUM is mentioned in the nonnull list. */
387 for (t
= TREE_VALUE (attrs
); t
; t
= TREE_CHAIN (t
))
389 if (compare_tree_int (TREE_VALUE (t
), arg_num
) == 0)
398 /* Set value range VR to VR_UNDEFINED. */
401 set_value_range_to_undefined (value_range_t
*vr
)
403 vr
->type
= VR_UNDEFINED
;
404 vr
->min
= vr
->max
= NULL_TREE
;
406 bitmap_clear (vr
->equiv
);
410 /* Set value range VR to VR_VARYING. */
413 set_value_range_to_varying (value_range_t
*vr
)
415 vr
->type
= VR_VARYING
;
416 vr
->min
= vr
->max
= NULL_TREE
;
418 bitmap_clear (vr
->equiv
);
422 /* Set value range VR to {T, MIN, MAX, EQUIV}. */
425 set_value_range (value_range_t
*vr
, enum value_range_type t
, tree min
,
426 tree max
, bitmap equiv
)
428 #if defined ENABLE_CHECKING
429 /* Check the validity of the range. */
430 if (t
== VR_RANGE
|| t
== VR_ANTI_RANGE
)
434 gcc_assert (min
&& max
);
436 if (INTEGRAL_TYPE_P (TREE_TYPE (min
)) && t
== VR_ANTI_RANGE
)
437 gcc_assert (!vrp_val_is_min (min
) || !vrp_val_is_max (max
));
439 cmp
= compare_values (min
, max
);
440 gcc_assert (cmp
== 0 || cmp
== -1 || cmp
== -2);
442 if (needs_overflow_infinity (TREE_TYPE (min
)))
443 gcc_assert (!is_overflow_infinity (min
)
444 || !is_overflow_infinity (max
));
447 if (t
== VR_UNDEFINED
|| t
== VR_VARYING
)
448 gcc_assert (min
== NULL_TREE
&& max
== NULL_TREE
);
450 if (t
== VR_UNDEFINED
|| t
== VR_VARYING
)
451 gcc_assert (equiv
== NULL
|| bitmap_empty_p (equiv
));
458 /* Since updating the equivalence set involves deep copying the
459 bitmaps, only do it if absolutely necessary. */
460 if (vr
->equiv
== NULL
462 vr
->equiv
= BITMAP_ALLOC (NULL
);
464 if (equiv
!= vr
->equiv
)
466 if (equiv
&& !bitmap_empty_p (equiv
))
467 bitmap_copy (vr
->equiv
, equiv
);
469 bitmap_clear (vr
->equiv
);
474 /* Set value range VR to the canonical form of {T, MIN, MAX, EQUIV}.
475 This means adjusting T, MIN and MAX representing the case of a
476 wrapping range with MAX < MIN covering [MIN, type_max] U [type_min, MAX]
477 as anti-rage ~[MAX+1, MIN-1]. Likewise for wrapping anti-ranges.
478 In corner cases where MAX+1 or MIN-1 wraps this will fall back
480 This routine exists to ease canonicalization in the case where we
481 extract ranges from var + CST op limit. */
484 set_and_canonicalize_value_range (value_range_t
*vr
, enum value_range_type t
,
485 tree min
, tree max
, bitmap equiv
)
487 /* Use the canonical setters for VR_UNDEFINED and VR_VARYING. */
488 if (t
== VR_UNDEFINED
)
490 set_value_range_to_undefined (vr
);
493 else if (t
== VR_VARYING
)
495 set_value_range_to_varying (vr
);
499 /* Nothing to canonicalize for symbolic ranges. */
500 if (TREE_CODE (min
) != INTEGER_CST
501 || TREE_CODE (max
) != INTEGER_CST
)
503 set_value_range (vr
, t
, min
, max
, equiv
);
507 /* Wrong order for min and max, to swap them and the VR type we need
509 if (tree_int_cst_lt (max
, min
))
513 /* For one bit precision if max < min, then the swapped
514 range covers all values, so for VR_RANGE it is varying and
515 for VR_ANTI_RANGE empty range, so drop to varying as well. */
516 if (TYPE_PRECISION (TREE_TYPE (min
)) == 1)
518 set_value_range_to_varying (vr
);
522 one
= build_int_cst (TREE_TYPE (min
), 1);
523 tmp
= int_const_binop (PLUS_EXPR
, max
, one
);
524 max
= int_const_binop (MINUS_EXPR
, min
, one
);
527 /* There's one corner case, if we had [C+1, C] before we now have
528 that again. But this represents an empty value range, so drop
529 to varying in this case. */
530 if (tree_int_cst_lt (max
, min
))
532 set_value_range_to_varying (vr
);
536 t
= t
== VR_RANGE
? VR_ANTI_RANGE
: VR_RANGE
;
539 /* Anti-ranges that can be represented as ranges should be so. */
540 if (t
== VR_ANTI_RANGE
)
542 bool is_min
= vrp_val_is_min (min
);
543 bool is_max
= vrp_val_is_max (max
);
545 if (is_min
&& is_max
)
547 /* We cannot deal with empty ranges, drop to varying.
548 ??? This could be VR_UNDEFINED instead. */
549 set_value_range_to_varying (vr
);
552 else if (TYPE_PRECISION (TREE_TYPE (min
)) == 1
553 && (is_min
|| is_max
))
555 /* Non-empty boolean ranges can always be represented
556 as a singleton range. */
558 min
= max
= vrp_val_max (TREE_TYPE (min
));
560 min
= max
= vrp_val_min (TREE_TYPE (min
));
564 /* As a special exception preserve non-null ranges. */
565 && !(TYPE_UNSIGNED (TREE_TYPE (min
))
566 && integer_zerop (max
)))
568 tree one
= build_int_cst (TREE_TYPE (max
), 1);
569 min
= int_const_binop (PLUS_EXPR
, max
, one
);
570 max
= vrp_val_max (TREE_TYPE (max
));
575 tree one
= build_int_cst (TREE_TYPE (min
), 1);
576 max
= int_const_binop (MINUS_EXPR
, min
, one
);
577 min
= vrp_val_min (TREE_TYPE (min
));
582 /* Drop [-INF(OVF), +INF(OVF)] to varying. */
583 if (needs_overflow_infinity (TREE_TYPE (min
))
584 && is_overflow_infinity (min
)
585 && is_overflow_infinity (max
))
587 set_value_range_to_varying (vr
);
591 set_value_range (vr
, t
, min
, max
, equiv
);
594 /* Copy value range FROM into value range TO. */
597 copy_value_range (value_range_t
*to
, value_range_t
*from
)
599 set_value_range (to
, from
->type
, from
->min
, from
->max
, from
->equiv
);
602 /* Set value range VR to a single value. This function is only called
603 with values we get from statements, and exists to clear the
604 TREE_OVERFLOW flag so that we don't think we have an overflow
605 infinity when we shouldn't. */
608 set_value_range_to_value (value_range_t
*vr
, tree val
, bitmap equiv
)
610 gcc_assert (is_gimple_min_invariant (val
));
611 val
= avoid_overflow_infinity (val
);
612 set_value_range (vr
, VR_RANGE
, val
, val
, equiv
);
615 /* Set value range VR to a non-negative range of type TYPE.
616 OVERFLOW_INFINITY indicates whether to use an overflow infinity
617 rather than TYPE_MAX_VALUE; this should be true if we determine
618 that the range is nonnegative based on the assumption that signed
619 overflow does not occur. */
622 set_value_range_to_nonnegative (value_range_t
*vr
, tree type
,
623 bool overflow_infinity
)
627 if (overflow_infinity
&& !supports_overflow_infinity (type
))
629 set_value_range_to_varying (vr
);
633 zero
= build_int_cst (type
, 0);
634 set_value_range (vr
, VR_RANGE
, zero
,
636 ? positive_overflow_infinity (type
)
637 : TYPE_MAX_VALUE (type
)),
641 /* Set value range VR to a non-NULL range of type TYPE. */
644 set_value_range_to_nonnull (value_range_t
*vr
, tree type
)
646 tree zero
= build_int_cst (type
, 0);
647 set_value_range (vr
, VR_ANTI_RANGE
, zero
, zero
, vr
->equiv
);
651 /* Set value range VR to a NULL range of type TYPE. */
654 set_value_range_to_null (value_range_t
*vr
, tree type
)
656 set_value_range_to_value (vr
, build_int_cst (type
, 0), vr
->equiv
);
660 /* Set value range VR to a range of a truthvalue of type TYPE. */
663 set_value_range_to_truthvalue (value_range_t
*vr
, tree type
)
665 if (TYPE_PRECISION (type
) == 1)
666 set_value_range_to_varying (vr
);
668 set_value_range (vr
, VR_RANGE
,
669 build_int_cst (type
, 0), build_int_cst (type
, 1),
674 /* If abs (min) < abs (max), set VR to [-max, max], if
675 abs (min) >= abs (max), set VR to [-min, min]. */
678 abs_extent_range (value_range_t
*vr
, tree min
, tree max
)
682 gcc_assert (TREE_CODE (min
) == INTEGER_CST
);
683 gcc_assert (TREE_CODE (max
) == INTEGER_CST
);
684 gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (min
)));
685 gcc_assert (!TYPE_UNSIGNED (TREE_TYPE (min
)));
686 min
= fold_unary (ABS_EXPR
, TREE_TYPE (min
), min
);
687 max
= fold_unary (ABS_EXPR
, TREE_TYPE (max
), max
);
688 if (TREE_OVERFLOW (min
) || TREE_OVERFLOW (max
))
690 set_value_range_to_varying (vr
);
693 cmp
= compare_values (min
, max
);
695 min
= fold_unary (NEGATE_EXPR
, TREE_TYPE (min
), max
);
696 else if (cmp
== 0 || cmp
== 1)
699 min
= fold_unary (NEGATE_EXPR
, TREE_TYPE (min
), min
);
703 set_value_range_to_varying (vr
);
706 set_and_canonicalize_value_range (vr
, VR_RANGE
, min
, max
, NULL
);
710 /* Return value range information for VAR.
712 If we have no values ranges recorded (ie, VRP is not running), then
713 return NULL. Otherwise create an empty range if none existed for VAR. */
715 static value_range_t
*
716 get_value_range (const_tree var
)
718 static const struct value_range_d vr_const_varying
719 = { VR_VARYING
, NULL_TREE
, NULL_TREE
, NULL
};
722 unsigned ver
= SSA_NAME_VERSION (var
);
724 /* If we have no recorded ranges, then return NULL. */
728 /* If we query the range for a new SSA name return an unmodifiable VARYING.
729 We should get here at most from the substitute-and-fold stage which
730 will never try to change values. */
731 if (ver
>= num_vr_values
)
732 return CONST_CAST (value_range_t
*, &vr_const_varying
);
738 /* After propagation finished do not allocate new value-ranges. */
739 if (values_propagated
)
740 return CONST_CAST (value_range_t
*, &vr_const_varying
);
742 /* Create a default value range. */
743 vr_value
[ver
] = vr
= XCNEW (value_range_t
);
745 /* Defer allocating the equivalence set. */
748 /* If VAR is a default definition of a parameter, the variable can
749 take any value in VAR's type. */
750 if (SSA_NAME_IS_DEFAULT_DEF (var
))
752 sym
= SSA_NAME_VAR (var
);
753 if (TREE_CODE (sym
) == PARM_DECL
)
755 /* Try to use the "nonnull" attribute to create ~[0, 0]
756 anti-ranges for pointers. Note that this is only valid with
757 default definitions of PARM_DECLs. */
758 if (POINTER_TYPE_P (TREE_TYPE (sym
))
759 && nonnull_arg_p (sym
))
760 set_value_range_to_nonnull (vr
, TREE_TYPE (sym
));
762 set_value_range_to_varying (vr
);
764 else if (TREE_CODE (sym
) == RESULT_DECL
765 && DECL_BY_REFERENCE (sym
))
766 set_value_range_to_nonnull (vr
, TREE_TYPE (sym
));
772 /* Return true, if VAL1 and VAL2 are equal values for VRP purposes. */
775 vrp_operand_equal_p (const_tree val1
, const_tree val2
)
779 if (!val1
|| !val2
|| !operand_equal_p (val1
, val2
, 0))
781 if (is_overflow_infinity (val1
))
782 return is_overflow_infinity (val2
);
786 /* Return true, if the bitmaps B1 and B2 are equal. */
789 vrp_bitmap_equal_p (const_bitmap b1
, const_bitmap b2
)
792 || ((!b1
|| bitmap_empty_p (b1
))
793 && (!b2
|| bitmap_empty_p (b2
)))
795 && bitmap_equal_p (b1
, b2
)));
798 /* Update the value range and equivalence set for variable VAR to
799 NEW_VR. Return true if NEW_VR is different from VAR's previous
802 NOTE: This function assumes that NEW_VR is a temporary value range
803 object created for the sole purpose of updating VAR's range. The
804 storage used by the equivalence set from NEW_VR will be freed by
805 this function. Do not call update_value_range when NEW_VR
806 is the range object associated with another SSA name. */
809 update_value_range (const_tree var
, value_range_t
*new_vr
)
811 value_range_t
*old_vr
;
814 /* Update the value range, if necessary. */
815 old_vr
= get_value_range (var
);
816 is_new
= old_vr
->type
!= new_vr
->type
817 || !vrp_operand_equal_p (old_vr
->min
, new_vr
->min
)
818 || !vrp_operand_equal_p (old_vr
->max
, new_vr
->max
)
819 || !vrp_bitmap_equal_p (old_vr
->equiv
, new_vr
->equiv
);
823 /* Do not allow transitions up the lattice. The following
824 is slightly more awkward than just new_vr->type < old_vr->type
825 because VR_RANGE and VR_ANTI_RANGE need to be considered
826 the same. We may not have is_new when transitioning to
827 UNDEFINED or from VARYING. */
828 if (new_vr
->type
== VR_UNDEFINED
829 || old_vr
->type
== VR_VARYING
)
830 set_value_range_to_varying (old_vr
);
832 set_value_range (old_vr
, new_vr
->type
, new_vr
->min
, new_vr
->max
,
836 BITMAP_FREE (new_vr
->equiv
);
842 /* Add VAR and VAR's equivalence set to EQUIV. This is the central
843 point where equivalence processing can be turned on/off. */
846 add_equivalence (bitmap
*equiv
, const_tree var
)
848 unsigned ver
= SSA_NAME_VERSION (var
);
849 value_range_t
*vr
= vr_value
[ver
];
852 *equiv
= BITMAP_ALLOC (NULL
);
853 bitmap_set_bit (*equiv
, ver
);
855 bitmap_ior_into (*equiv
, vr
->equiv
);
859 /* Return true if VR is ~[0, 0]. */
862 range_is_nonnull (value_range_t
*vr
)
864 return vr
->type
== VR_ANTI_RANGE
865 && integer_zerop (vr
->min
)
866 && integer_zerop (vr
->max
);
870 /* Return true if VR is [0, 0]. */
873 range_is_null (value_range_t
*vr
)
875 return vr
->type
== VR_RANGE
876 && integer_zerop (vr
->min
)
877 && integer_zerop (vr
->max
);
880 /* Return true if max and min of VR are INTEGER_CST. It's not necessary
884 range_int_cst_p (value_range_t
*vr
)
886 return (vr
->type
== VR_RANGE
887 && TREE_CODE (vr
->max
) == INTEGER_CST
888 && TREE_CODE (vr
->min
) == INTEGER_CST
);
891 /* Return true if VR is a INTEGER_CST singleton. */
894 range_int_cst_singleton_p (value_range_t
*vr
)
896 return (range_int_cst_p (vr
)
897 && !is_overflow_infinity (vr
->min
)
898 && !is_overflow_infinity (vr
->max
)
899 && tree_int_cst_equal (vr
->min
, vr
->max
));
902 /* Return true if value range VR involves at least one symbol. */
905 symbolic_range_p (value_range_t
*vr
)
907 return (!is_gimple_min_invariant (vr
->min
)
908 || !is_gimple_min_invariant (vr
->max
));
911 /* Return true if value range VR uses an overflow infinity. */
914 overflow_infinity_range_p (value_range_t
*vr
)
916 return (vr
->type
== VR_RANGE
917 && (is_overflow_infinity (vr
->min
)
918 || is_overflow_infinity (vr
->max
)));
921 /* Return false if we can not make a valid comparison based on VR;
922 this will be the case if it uses an overflow infinity and overflow
923 is not undefined (i.e., -fno-strict-overflow is in effect).
924 Otherwise return true, and set *STRICT_OVERFLOW_P to true if VR
925 uses an overflow infinity. */
928 usable_range_p (value_range_t
*vr
, bool *strict_overflow_p
)
930 gcc_assert (vr
->type
== VR_RANGE
);
931 if (is_overflow_infinity (vr
->min
))
933 *strict_overflow_p
= true;
934 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (vr
->min
)))
937 if (is_overflow_infinity (vr
->max
))
939 *strict_overflow_p
= true;
940 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (vr
->max
)))
947 /* Return true if the result of assignment STMT is know to be non-negative.
948 If the return value is based on the assumption that signed overflow is
949 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
950 *STRICT_OVERFLOW_P.*/
953 gimple_assign_nonnegative_warnv_p (gimple stmt
, bool *strict_overflow_p
)
955 enum tree_code code
= gimple_assign_rhs_code (stmt
);
956 switch (get_gimple_rhs_class (code
))
958 case GIMPLE_UNARY_RHS
:
959 return tree_unary_nonnegative_warnv_p (gimple_assign_rhs_code (stmt
),
960 gimple_expr_type (stmt
),
961 gimple_assign_rhs1 (stmt
),
963 case GIMPLE_BINARY_RHS
:
964 return tree_binary_nonnegative_warnv_p (gimple_assign_rhs_code (stmt
),
965 gimple_expr_type (stmt
),
966 gimple_assign_rhs1 (stmt
),
967 gimple_assign_rhs2 (stmt
),
969 case GIMPLE_TERNARY_RHS
:
971 case GIMPLE_SINGLE_RHS
:
972 return tree_single_nonnegative_warnv_p (gimple_assign_rhs1 (stmt
),
974 case GIMPLE_INVALID_RHS
:
981 /* Return true if return value of call STMT is know to be non-negative.
982 If the return value is based on the assumption that signed overflow is
983 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
984 *STRICT_OVERFLOW_P.*/
987 gimple_call_nonnegative_warnv_p (gimple stmt
, bool *strict_overflow_p
)
989 tree arg0
= gimple_call_num_args (stmt
) > 0 ?
990 gimple_call_arg (stmt
, 0) : NULL_TREE
;
991 tree arg1
= gimple_call_num_args (stmt
) > 1 ?
992 gimple_call_arg (stmt
, 1) : NULL_TREE
;
994 return tree_call_nonnegative_warnv_p (gimple_expr_type (stmt
),
995 gimple_call_fndecl (stmt
),
1001 /* Return true if STMT is know to to compute a non-negative value.
1002 If the return value is based on the assumption that signed overflow is
1003 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1004 *STRICT_OVERFLOW_P.*/
1007 gimple_stmt_nonnegative_warnv_p (gimple stmt
, bool *strict_overflow_p
)
1009 switch (gimple_code (stmt
))
1012 return gimple_assign_nonnegative_warnv_p (stmt
, strict_overflow_p
);
1014 return gimple_call_nonnegative_warnv_p (stmt
, strict_overflow_p
);
1020 /* Return true if the result of assignment STMT is know to be non-zero.
1021 If the return value is based on the assumption that signed overflow is
1022 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1023 *STRICT_OVERFLOW_P.*/
1026 gimple_assign_nonzero_warnv_p (gimple stmt
, bool *strict_overflow_p
)
1028 enum tree_code code
= gimple_assign_rhs_code (stmt
);
1029 switch (get_gimple_rhs_class (code
))
1031 case GIMPLE_UNARY_RHS
:
1032 return tree_unary_nonzero_warnv_p (gimple_assign_rhs_code (stmt
),
1033 gimple_expr_type (stmt
),
1034 gimple_assign_rhs1 (stmt
),
1036 case GIMPLE_BINARY_RHS
:
1037 return tree_binary_nonzero_warnv_p (gimple_assign_rhs_code (stmt
),
1038 gimple_expr_type (stmt
),
1039 gimple_assign_rhs1 (stmt
),
1040 gimple_assign_rhs2 (stmt
),
1042 case GIMPLE_TERNARY_RHS
:
1044 case GIMPLE_SINGLE_RHS
:
1045 return tree_single_nonzero_warnv_p (gimple_assign_rhs1 (stmt
),
1047 case GIMPLE_INVALID_RHS
:
1054 /* Return true if STMT is known to compute a non-zero value.
1055 If the return value is based on the assumption that signed overflow is
1056 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1057 *STRICT_OVERFLOW_P.*/
1060 gimple_stmt_nonzero_warnv_p (gimple stmt
, bool *strict_overflow_p
)
1062 switch (gimple_code (stmt
))
1065 return gimple_assign_nonzero_warnv_p (stmt
, strict_overflow_p
);
1068 tree fndecl
= gimple_call_fndecl (stmt
);
1069 if (!fndecl
) return false;
1070 if (flag_delete_null_pointer_checks
&& !flag_check_new
1071 && DECL_IS_OPERATOR_NEW (fndecl
)
1072 && !TREE_NOTHROW (fndecl
))
1074 if (flag_delete_null_pointer_checks
&&
1075 lookup_attribute ("returns_nonnull",
1076 TYPE_ATTRIBUTES (gimple_call_fntype (stmt
))))
1078 return gimple_alloca_call_p (stmt
);
1085 /* Like tree_expr_nonzero_warnv_p, but this function uses value ranges
1089 vrp_stmt_computes_nonzero (gimple stmt
, bool *strict_overflow_p
)
1091 if (gimple_stmt_nonzero_warnv_p (stmt
, strict_overflow_p
))
1094 /* If we have an expression of the form &X->a, then the expression
1095 is nonnull if X is nonnull. */
1096 if (is_gimple_assign (stmt
)
1097 && gimple_assign_rhs_code (stmt
) == ADDR_EXPR
)
1099 tree expr
= gimple_assign_rhs1 (stmt
);
1100 tree base
= get_base_address (TREE_OPERAND (expr
, 0));
1102 if (base
!= NULL_TREE
1103 && TREE_CODE (base
) == MEM_REF
1104 && TREE_CODE (TREE_OPERAND (base
, 0)) == SSA_NAME
)
1106 value_range_t
*vr
= get_value_range (TREE_OPERAND (base
, 0));
1107 if (range_is_nonnull (vr
))
1115 /* Returns true if EXPR is a valid value (as expected by compare_values) --
1116 a gimple invariant, or SSA_NAME +- CST. */
1119 valid_value_p (tree expr
)
1121 if (TREE_CODE (expr
) == SSA_NAME
)
1124 if (TREE_CODE (expr
) == PLUS_EXPR
1125 || TREE_CODE (expr
) == MINUS_EXPR
)
1126 return (TREE_CODE (TREE_OPERAND (expr
, 0)) == SSA_NAME
1127 && TREE_CODE (TREE_OPERAND (expr
, 1)) == INTEGER_CST
);
1129 return is_gimple_min_invariant (expr
);
1135 -2 if those are incomparable. */
1137 operand_less_p (tree val
, tree val2
)
1139 /* LT is folded faster than GE and others. Inline the common case. */
1140 if (TREE_CODE (val
) == INTEGER_CST
&& TREE_CODE (val2
) == INTEGER_CST
)
1142 if (TYPE_UNSIGNED (TREE_TYPE (val
)))
1143 return INT_CST_LT_UNSIGNED (val
, val2
);
1146 if (INT_CST_LT (val
, val2
))
1154 fold_defer_overflow_warnings ();
1156 tcmp
= fold_binary_to_constant (LT_EXPR
, boolean_type_node
, val
, val2
);
1158 fold_undefer_and_ignore_overflow_warnings ();
1161 || TREE_CODE (tcmp
) != INTEGER_CST
)
1164 if (!integer_zerop (tcmp
))
1168 /* val >= val2, not considering overflow infinity. */
1169 if (is_negative_overflow_infinity (val
))
1170 return is_negative_overflow_infinity (val2
) ? 0 : 1;
1171 else if (is_positive_overflow_infinity (val2
))
1172 return is_positive_overflow_infinity (val
) ? 0 : 1;
1177 /* Compare two values VAL1 and VAL2. Return
1179 -2 if VAL1 and VAL2 cannot be compared at compile-time,
1182 +1 if VAL1 > VAL2, and
1185 This is similar to tree_int_cst_compare but supports pointer values
1186 and values that cannot be compared at compile time.
1188 If STRICT_OVERFLOW_P is not NULL, then set *STRICT_OVERFLOW_P to
1189 true if the return value is only valid if we assume that signed
1190 overflow is undefined. */
1193 compare_values_warnv (tree val1
, tree val2
, bool *strict_overflow_p
)
1198 /* Below we rely on the fact that VAL1 and VAL2 are both pointers or
1200 gcc_assert (POINTER_TYPE_P (TREE_TYPE (val1
))
1201 == POINTER_TYPE_P (TREE_TYPE (val2
)));
1202 /* Convert the two values into the same type. This is needed because
1203 sizetype causes sign extension even for unsigned types. */
1204 val2
= fold_convert (TREE_TYPE (val1
), val2
);
1205 STRIP_USELESS_TYPE_CONVERSION (val2
);
1207 if ((TREE_CODE (val1
) == SSA_NAME
1208 || TREE_CODE (val1
) == PLUS_EXPR
1209 || TREE_CODE (val1
) == MINUS_EXPR
)
1210 && (TREE_CODE (val2
) == SSA_NAME
1211 || TREE_CODE (val2
) == PLUS_EXPR
1212 || TREE_CODE (val2
) == MINUS_EXPR
))
1214 tree n1
, c1
, n2
, c2
;
1215 enum tree_code code1
, code2
;
1217 /* If VAL1 and VAL2 are of the form 'NAME [+-] CST' or 'NAME',
1218 return -1 or +1 accordingly. If VAL1 and VAL2 don't use the
1219 same name, return -2. */
1220 if (TREE_CODE (val1
) == SSA_NAME
)
1228 code1
= TREE_CODE (val1
);
1229 n1
= TREE_OPERAND (val1
, 0);
1230 c1
= TREE_OPERAND (val1
, 1);
1231 if (tree_int_cst_sgn (c1
) == -1)
1233 if (is_negative_overflow_infinity (c1
))
1235 c1
= fold_unary_to_constant (NEGATE_EXPR
, TREE_TYPE (c1
), c1
);
1238 code1
= code1
== MINUS_EXPR
? PLUS_EXPR
: MINUS_EXPR
;
1242 if (TREE_CODE (val2
) == SSA_NAME
)
1250 code2
= TREE_CODE (val2
);
1251 n2
= TREE_OPERAND (val2
, 0);
1252 c2
= TREE_OPERAND (val2
, 1);
1253 if (tree_int_cst_sgn (c2
) == -1)
1255 if (is_negative_overflow_infinity (c2
))
1257 c2
= fold_unary_to_constant (NEGATE_EXPR
, TREE_TYPE (c2
), c2
);
1260 code2
= code2
== MINUS_EXPR
? PLUS_EXPR
: MINUS_EXPR
;
1264 /* Both values must use the same name. */
1268 if (code1
== SSA_NAME
1269 && code2
== SSA_NAME
)
1273 /* If overflow is defined we cannot simplify more. */
1274 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (val1
)))
1277 if (strict_overflow_p
!= NULL
1278 && (code1
== SSA_NAME
|| !TREE_NO_WARNING (val1
))
1279 && (code2
== SSA_NAME
|| !TREE_NO_WARNING (val2
)))
1280 *strict_overflow_p
= true;
1282 if (code1
== SSA_NAME
)
1284 if (code2
== PLUS_EXPR
)
1285 /* NAME < NAME + CST */
1287 else if (code2
== MINUS_EXPR
)
1288 /* NAME > NAME - CST */
1291 else if (code1
== PLUS_EXPR
)
1293 if (code2
== SSA_NAME
)
1294 /* NAME + CST > NAME */
1296 else if (code2
== PLUS_EXPR
)
1297 /* NAME + CST1 > NAME + CST2, if CST1 > CST2 */
1298 return compare_values_warnv (c1
, c2
, strict_overflow_p
);
1299 else if (code2
== MINUS_EXPR
)
1300 /* NAME + CST1 > NAME - CST2 */
1303 else if (code1
== MINUS_EXPR
)
1305 if (code2
== SSA_NAME
)
1306 /* NAME - CST < NAME */
1308 else if (code2
== PLUS_EXPR
)
1309 /* NAME - CST1 < NAME + CST2 */
1311 else if (code2
== MINUS_EXPR
)
1312 /* NAME - CST1 > NAME - CST2, if CST1 < CST2. Notice that
1313 C1 and C2 are swapped in the call to compare_values. */
1314 return compare_values_warnv (c2
, c1
, strict_overflow_p
);
1320 /* We cannot compare non-constants. */
1321 if (!is_gimple_min_invariant (val1
) || !is_gimple_min_invariant (val2
))
1324 if (!POINTER_TYPE_P (TREE_TYPE (val1
)))
1326 /* We cannot compare overflowed values, except for overflow
1328 if (TREE_OVERFLOW (val1
) || TREE_OVERFLOW (val2
))
1330 if (strict_overflow_p
!= NULL
)
1331 *strict_overflow_p
= true;
1332 if (is_negative_overflow_infinity (val1
))
1333 return is_negative_overflow_infinity (val2
) ? 0 : -1;
1334 else if (is_negative_overflow_infinity (val2
))
1336 else if (is_positive_overflow_infinity (val1
))
1337 return is_positive_overflow_infinity (val2
) ? 0 : 1;
1338 else if (is_positive_overflow_infinity (val2
))
1343 return tree_int_cst_compare (val1
, val2
);
1349 /* First see if VAL1 and VAL2 are not the same. */
1350 if (val1
== val2
|| operand_equal_p (val1
, val2
, 0))
1353 /* If VAL1 is a lower address than VAL2, return -1. */
1354 if (operand_less_p (val1
, val2
) == 1)
1357 /* If VAL1 is a higher address than VAL2, return +1. */
1358 if (operand_less_p (val2
, val1
) == 1)
1361 /* If VAL1 is different than VAL2, return +2.
1362 For integer constants we either have already returned -1 or 1
1363 or they are equivalent. We still might succeed in proving
1364 something about non-trivial operands. */
1365 if (TREE_CODE (val1
) != INTEGER_CST
1366 || TREE_CODE (val2
) != INTEGER_CST
)
1368 t
= fold_binary_to_constant (NE_EXPR
, boolean_type_node
, val1
, val2
);
1369 if (t
&& integer_onep (t
))
1377 /* Compare values like compare_values_warnv, but treat comparisons of
1378 nonconstants which rely on undefined overflow as incomparable. */
1381 compare_values (tree val1
, tree val2
)
1387 ret
= compare_values_warnv (val1
, val2
, &sop
);
1389 && (!is_gimple_min_invariant (val1
) || !is_gimple_min_invariant (val2
)))
1395 /* Return 1 if VAL is inside value range MIN <= VAL <= MAX,
1396 0 if VAL is not inside [MIN, MAX],
1397 -2 if we cannot tell either way.
1399 Benchmark compile/20001226-1.c compilation time after changing this
1403 value_inside_range (tree val
, tree min
, tree max
)
1407 cmp1
= operand_less_p (val
, min
);
1413 cmp2
= operand_less_p (max
, val
);
1421 /* Return true if value ranges VR0 and VR1 have a non-empty
1424 Benchmark compile/20001226-1.c compilation time after changing this
1429 value_ranges_intersect_p (value_range_t
*vr0
, value_range_t
*vr1
)
1431 /* The value ranges do not intersect if the maximum of the first range is
1432 less than the minimum of the second range or vice versa.
1433 When those relations are unknown, we can't do any better. */
1434 if (operand_less_p (vr0
->max
, vr1
->min
) != 0)
1436 if (operand_less_p (vr1
->max
, vr0
->min
) != 0)
1442 /* Return 1 if [MIN, MAX] includes the value zero, 0 if it does not
1443 include the value zero, -2 if we cannot tell. */
1446 range_includes_zero_p (tree min
, tree max
)
1448 tree zero
= build_int_cst (TREE_TYPE (min
), 0);
1449 return value_inside_range (zero
, min
, max
);
1452 /* Return true if *VR is know to only contain nonnegative values. */
1455 value_range_nonnegative_p (value_range_t
*vr
)
1457 /* Testing for VR_ANTI_RANGE is not useful here as any anti-range
1458 which would return a useful value should be encoded as a
1460 if (vr
->type
== VR_RANGE
)
1462 int result
= compare_values (vr
->min
, integer_zero_node
);
1463 return (result
== 0 || result
== 1);
1469 /* If *VR has a value rante that is a single constant value return that,
1470 otherwise return NULL_TREE. */
1473 value_range_constant_singleton (value_range_t
*vr
)
1475 if (vr
->type
== VR_RANGE
1476 && operand_equal_p (vr
->min
, vr
->max
, 0)
1477 && is_gimple_min_invariant (vr
->min
))
1483 /* If OP has a value range with a single constant value return that,
1484 otherwise return NULL_TREE. This returns OP itself if OP is a
1488 op_with_constant_singleton_value_range (tree op
)
1490 if (is_gimple_min_invariant (op
))
1493 if (TREE_CODE (op
) != SSA_NAME
)
1496 return value_range_constant_singleton (get_value_range (op
));
1499 /* Return true if op is in a boolean [0, 1] value-range. */
1502 op_with_boolean_value_range_p (tree op
)
1506 if (TYPE_PRECISION (TREE_TYPE (op
)) == 1)
1509 if (integer_zerop (op
)
1510 || integer_onep (op
))
1513 if (TREE_CODE (op
) != SSA_NAME
)
1516 vr
= get_value_range (op
);
1517 return (vr
->type
== VR_RANGE
1518 && integer_zerop (vr
->min
)
1519 && integer_onep (vr
->max
));
1522 /* Extract value range information from an ASSERT_EXPR EXPR and store
1526 extract_range_from_assert (value_range_t
*vr_p
, tree expr
)
1528 tree var
, cond
, limit
, min
, max
, type
;
1529 value_range_t
*limit_vr
;
1530 enum tree_code cond_code
;
1532 var
= ASSERT_EXPR_VAR (expr
);
1533 cond
= ASSERT_EXPR_COND (expr
);
1535 gcc_assert (COMPARISON_CLASS_P (cond
));
1537 /* Find VAR in the ASSERT_EXPR conditional. */
1538 if (var
== TREE_OPERAND (cond
, 0)
1539 || TREE_CODE (TREE_OPERAND (cond
, 0)) == PLUS_EXPR
1540 || TREE_CODE (TREE_OPERAND (cond
, 0)) == NOP_EXPR
)
1542 /* If the predicate is of the form VAR COMP LIMIT, then we just
1543 take LIMIT from the RHS and use the same comparison code. */
1544 cond_code
= TREE_CODE (cond
);
1545 limit
= TREE_OPERAND (cond
, 1);
1546 cond
= TREE_OPERAND (cond
, 0);
1550 /* If the predicate is of the form LIMIT COMP VAR, then we need
1551 to flip around the comparison code to create the proper range
1553 cond_code
= swap_tree_comparison (TREE_CODE (cond
));
1554 limit
= TREE_OPERAND (cond
, 0);
1555 cond
= TREE_OPERAND (cond
, 1);
1558 limit
= avoid_overflow_infinity (limit
);
1560 type
= TREE_TYPE (var
);
1561 gcc_assert (limit
!= var
);
1563 /* For pointer arithmetic, we only keep track of pointer equality
1565 if (POINTER_TYPE_P (type
) && cond_code
!= NE_EXPR
&& cond_code
!= EQ_EXPR
)
1567 set_value_range_to_varying (vr_p
);
1571 /* If LIMIT is another SSA name and LIMIT has a range of its own,
1572 try to use LIMIT's range to avoid creating symbolic ranges
1574 limit_vr
= (TREE_CODE (limit
) == SSA_NAME
) ? get_value_range (limit
) : NULL
;
1576 /* LIMIT's range is only interesting if it has any useful information. */
1578 && (limit_vr
->type
== VR_UNDEFINED
1579 || limit_vr
->type
== VR_VARYING
1580 || symbolic_range_p (limit_vr
)))
1583 /* Initially, the new range has the same set of equivalences of
1584 VAR's range. This will be revised before returning the final
1585 value. Since assertions may be chained via mutually exclusive
1586 predicates, we will need to trim the set of equivalences before
1588 gcc_assert (vr_p
->equiv
== NULL
);
1589 add_equivalence (&vr_p
->equiv
, var
);
1591 /* Extract a new range based on the asserted comparison for VAR and
1592 LIMIT's value range. Notice that if LIMIT has an anti-range, we
1593 will only use it for equality comparisons (EQ_EXPR). For any
1594 other kind of assertion, we cannot derive a range from LIMIT's
1595 anti-range that can be used to describe the new range. For
1596 instance, ASSERT_EXPR <x_2, x_2 <= b_4>. If b_4 is ~[2, 10],
1597 then b_4 takes on the ranges [-INF, 1] and [11, +INF]. There is
1598 no single range for x_2 that could describe LE_EXPR, so we might
1599 as well build the range [b_4, +INF] for it.
1600 One special case we handle is extracting a range from a
1601 range test encoded as (unsigned)var + CST <= limit. */
1602 if (TREE_CODE (cond
) == NOP_EXPR
1603 || TREE_CODE (cond
) == PLUS_EXPR
)
1605 if (TREE_CODE (cond
) == PLUS_EXPR
)
1607 min
= fold_build1 (NEGATE_EXPR
, TREE_TYPE (TREE_OPERAND (cond
, 1)),
1608 TREE_OPERAND (cond
, 1));
1609 max
= int_const_binop (PLUS_EXPR
, limit
, min
);
1610 cond
= TREE_OPERAND (cond
, 0);
1614 min
= build_int_cst (TREE_TYPE (var
), 0);
1618 /* Make sure to not set TREE_OVERFLOW on the final type
1619 conversion. We are willingly interpreting large positive
1620 unsigned values as negative singed values here. */
1621 min
= force_fit_type_double (TREE_TYPE (var
), tree_to_double_int (min
),
1623 max
= force_fit_type_double (TREE_TYPE (var
), tree_to_double_int (max
),
1626 /* We can transform a max, min range to an anti-range or
1627 vice-versa. Use set_and_canonicalize_value_range which does
1629 if (cond_code
== LE_EXPR
)
1630 set_and_canonicalize_value_range (vr_p
, VR_RANGE
,
1631 min
, max
, vr_p
->equiv
);
1632 else if (cond_code
== GT_EXPR
)
1633 set_and_canonicalize_value_range (vr_p
, VR_ANTI_RANGE
,
1634 min
, max
, vr_p
->equiv
);
1638 else if (cond_code
== EQ_EXPR
)
1640 enum value_range_type range_type
;
1644 range_type
= limit_vr
->type
;
1645 min
= limit_vr
->min
;
1646 max
= limit_vr
->max
;
1650 range_type
= VR_RANGE
;
1655 set_value_range (vr_p
, range_type
, min
, max
, vr_p
->equiv
);
1657 /* When asserting the equality VAR == LIMIT and LIMIT is another
1658 SSA name, the new range will also inherit the equivalence set
1660 if (TREE_CODE (limit
) == SSA_NAME
)
1661 add_equivalence (&vr_p
->equiv
, limit
);
1663 else if (cond_code
== NE_EXPR
)
1665 /* As described above, when LIMIT's range is an anti-range and
1666 this assertion is an inequality (NE_EXPR), then we cannot
1667 derive anything from the anti-range. For instance, if
1668 LIMIT's range was ~[0, 0], the assertion 'VAR != LIMIT' does
1669 not imply that VAR's range is [0, 0]. So, in the case of
1670 anti-ranges, we just assert the inequality using LIMIT and
1673 If LIMIT_VR is a range, we can only use it to build a new
1674 anti-range if LIMIT_VR is a single-valued range. For
1675 instance, if LIMIT_VR is [0, 1], the predicate
1676 VAR != [0, 1] does not mean that VAR's range is ~[0, 1].
1677 Rather, it means that for value 0 VAR should be ~[0, 0]
1678 and for value 1, VAR should be ~[1, 1]. We cannot
1679 represent these ranges.
1681 The only situation in which we can build a valid
1682 anti-range is when LIMIT_VR is a single-valued range
1683 (i.e., LIMIT_VR->MIN == LIMIT_VR->MAX). In that case,
1684 build the anti-range ~[LIMIT_VR->MIN, LIMIT_VR->MAX]. */
1686 && limit_vr
->type
== VR_RANGE
1687 && compare_values (limit_vr
->min
, limit_vr
->max
) == 0)
1689 min
= limit_vr
->min
;
1690 max
= limit_vr
->max
;
1694 /* In any other case, we cannot use LIMIT's range to build a
1695 valid anti-range. */
1699 /* If MIN and MAX cover the whole range for their type, then
1700 just use the original LIMIT. */
1701 if (INTEGRAL_TYPE_P (type
)
1702 && vrp_val_is_min (min
)
1703 && vrp_val_is_max (max
))
1706 set_and_canonicalize_value_range (vr_p
, VR_ANTI_RANGE
,
1707 min
, max
, vr_p
->equiv
);
1709 else if (cond_code
== LE_EXPR
|| cond_code
== LT_EXPR
)
1711 min
= TYPE_MIN_VALUE (type
);
1713 if (limit_vr
== NULL
|| limit_vr
->type
== VR_ANTI_RANGE
)
1717 /* If LIMIT_VR is of the form [N1, N2], we need to build the
1718 range [MIN, N2] for LE_EXPR and [MIN, N2 - 1] for
1720 max
= limit_vr
->max
;
1723 /* If the maximum value forces us to be out of bounds, simply punt.
1724 It would be pointless to try and do anything more since this
1725 all should be optimized away above us. */
1726 if ((cond_code
== LT_EXPR
1727 && compare_values (max
, min
) == 0)
1728 || is_overflow_infinity (max
))
1729 set_value_range_to_varying (vr_p
);
1732 /* For LT_EXPR, we create the range [MIN, MAX - 1]. */
1733 if (cond_code
== LT_EXPR
)
1735 if (TYPE_PRECISION (TREE_TYPE (max
)) == 1
1736 && !TYPE_UNSIGNED (TREE_TYPE (max
)))
1737 max
= fold_build2 (PLUS_EXPR
, TREE_TYPE (max
), max
,
1738 build_int_cst (TREE_TYPE (max
), -1));
1740 max
= fold_build2 (MINUS_EXPR
, TREE_TYPE (max
), max
,
1741 build_int_cst (TREE_TYPE (max
), 1));
1743 TREE_NO_WARNING (max
) = 1;
1746 set_value_range (vr_p
, VR_RANGE
, min
, max
, vr_p
->equiv
);
1749 else if (cond_code
== GE_EXPR
|| cond_code
== GT_EXPR
)
1751 max
= TYPE_MAX_VALUE (type
);
1753 if (limit_vr
== NULL
|| limit_vr
->type
== VR_ANTI_RANGE
)
1757 /* If LIMIT_VR is of the form [N1, N2], we need to build the
1758 range [N1, MAX] for GE_EXPR and [N1 + 1, MAX] for
1760 min
= limit_vr
->min
;
1763 /* If the minimum value forces us to be out of bounds, simply punt.
1764 It would be pointless to try and do anything more since this
1765 all should be optimized away above us. */
1766 if ((cond_code
== GT_EXPR
1767 && compare_values (min
, max
) == 0)
1768 || is_overflow_infinity (min
))
1769 set_value_range_to_varying (vr_p
);
1772 /* For GT_EXPR, we create the range [MIN + 1, MAX]. */
1773 if (cond_code
== GT_EXPR
)
1775 if (TYPE_PRECISION (TREE_TYPE (min
)) == 1
1776 && !TYPE_UNSIGNED (TREE_TYPE (min
)))
1777 min
= fold_build2 (MINUS_EXPR
, TREE_TYPE (min
), min
,
1778 build_int_cst (TREE_TYPE (min
), -1));
1780 min
= fold_build2 (PLUS_EXPR
, TREE_TYPE (min
), min
,
1781 build_int_cst (TREE_TYPE (min
), 1));
1783 TREE_NO_WARNING (min
) = 1;
1786 set_value_range (vr_p
, VR_RANGE
, min
, max
, vr_p
->equiv
);
1792 /* Finally intersect the new range with what we already know about var. */
1793 vrp_intersect_ranges (vr_p
, get_value_range (var
));
1797 /* Extract range information from SSA name VAR and store it in VR. If
1798 VAR has an interesting range, use it. Otherwise, create the
1799 range [VAR, VAR] and return it. This is useful in situations where
1800 we may have conditionals testing values of VARYING names. For
1807 Even if y_5 is deemed VARYING, we can determine that x_3 > y_5 is
1811 extract_range_from_ssa_name (value_range_t
*vr
, tree var
)
1813 value_range_t
*var_vr
= get_value_range (var
);
1815 if (var_vr
->type
!= VR_UNDEFINED
&& var_vr
->type
!= VR_VARYING
)
1816 copy_value_range (vr
, var_vr
);
1818 set_value_range (vr
, VR_RANGE
, var
, var
, NULL
);
1820 add_equivalence (&vr
->equiv
, var
);
1824 /* Wrapper around int_const_binop. If the operation overflows and we
1825 are not using wrapping arithmetic, then adjust the result to be
1826 -INF or +INF depending on CODE, VAL1 and VAL2. This can return
1827 NULL_TREE if we need to use an overflow infinity representation but
1828 the type does not support it. */
1831 vrp_int_const_binop (enum tree_code code
, tree val1
, tree val2
)
1835 res
= int_const_binop (code
, val1
, val2
);
1837 /* If we are using unsigned arithmetic, operate symbolically
1838 on -INF and +INF as int_const_binop only handles signed overflow. */
1839 if (TYPE_UNSIGNED (TREE_TYPE (val1
)))
1841 int checkz
= compare_values (res
, val1
);
1842 bool overflow
= false;
1844 /* Ensure that res = val1 [+*] val2 >= val1
1845 or that res = val1 - val2 <= val1. */
1846 if ((code
== PLUS_EXPR
1847 && !(checkz
== 1 || checkz
== 0))
1848 || (code
== MINUS_EXPR
1849 && !(checkz
== 0 || checkz
== -1)))
1853 /* Checking for multiplication overflow is done by dividing the
1854 output of the multiplication by the first input of the
1855 multiplication. If the result of that division operation is
1856 not equal to the second input of the multiplication, then the
1857 multiplication overflowed. */
1858 else if (code
== MULT_EXPR
&& !integer_zerop (val1
))
1860 tree tmp
= int_const_binop (TRUNC_DIV_EXPR
,
1863 int check
= compare_values (tmp
, val2
);
1871 res
= copy_node (res
);
1872 TREE_OVERFLOW (res
) = 1;
1876 else if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (val1
)))
1877 /* If the singed operation wraps then int_const_binop has done
1878 everything we want. */
1880 else if ((TREE_OVERFLOW (res
)
1881 && !TREE_OVERFLOW (val1
)
1882 && !TREE_OVERFLOW (val2
))
1883 || is_overflow_infinity (val1
)
1884 || is_overflow_infinity (val2
))
1886 /* If the operation overflowed but neither VAL1 nor VAL2 are
1887 overflown, return -INF or +INF depending on the operation
1888 and the combination of signs of the operands. */
1889 int sgn1
= tree_int_cst_sgn (val1
);
1890 int sgn2
= tree_int_cst_sgn (val2
);
1892 if (needs_overflow_infinity (TREE_TYPE (res
))
1893 && !supports_overflow_infinity (TREE_TYPE (res
)))
1896 /* We have to punt on adding infinities of different signs,
1897 since we can't tell what the sign of the result should be.
1898 Likewise for subtracting infinities of the same sign. */
1899 if (((code
== PLUS_EXPR
&& sgn1
!= sgn2
)
1900 || (code
== MINUS_EXPR
&& sgn1
== sgn2
))
1901 && is_overflow_infinity (val1
)
1902 && is_overflow_infinity (val2
))
1905 /* Don't try to handle division or shifting of infinities. */
1906 if ((code
== TRUNC_DIV_EXPR
1907 || code
== FLOOR_DIV_EXPR
1908 || code
== CEIL_DIV_EXPR
1909 || code
== EXACT_DIV_EXPR
1910 || code
== ROUND_DIV_EXPR
1911 || code
== RSHIFT_EXPR
)
1912 && (is_overflow_infinity (val1
)
1913 || is_overflow_infinity (val2
)))
1916 /* Notice that we only need to handle the restricted set of
1917 operations handled by extract_range_from_binary_expr.
1918 Among them, only multiplication, addition and subtraction
1919 can yield overflow without overflown operands because we
1920 are working with integral types only... except in the
1921 case VAL1 = -INF and VAL2 = -1 which overflows to +INF
1922 for division too. */
1924 /* For multiplication, the sign of the overflow is given
1925 by the comparison of the signs of the operands. */
1926 if ((code
== MULT_EXPR
&& sgn1
== sgn2
)
1927 /* For addition, the operands must be of the same sign
1928 to yield an overflow. Its sign is therefore that
1929 of one of the operands, for example the first. For
1930 infinite operands X + -INF is negative, not positive. */
1931 || (code
== PLUS_EXPR
1933 ? !is_negative_overflow_infinity (val2
)
1934 : is_positive_overflow_infinity (val2
)))
1935 /* For subtraction, non-infinite operands must be of
1936 different signs to yield an overflow. Its sign is
1937 therefore that of the first operand or the opposite of
1938 that of the second operand. A first operand of 0 counts
1939 as positive here, for the corner case 0 - (-INF), which
1940 overflows, but must yield +INF. For infinite operands 0
1941 - INF is negative, not positive. */
1942 || (code
== MINUS_EXPR
1944 ? !is_positive_overflow_infinity (val2
)
1945 : is_negative_overflow_infinity (val2
)))
1946 /* We only get in here with positive shift count, so the
1947 overflow direction is the same as the sign of val1.
1948 Actually rshift does not overflow at all, but we only
1949 handle the case of shifting overflowed -INF and +INF. */
1950 || (code
== RSHIFT_EXPR
1952 /* For division, the only case is -INF / -1 = +INF. */
1953 || code
== TRUNC_DIV_EXPR
1954 || code
== FLOOR_DIV_EXPR
1955 || code
== CEIL_DIV_EXPR
1956 || code
== EXACT_DIV_EXPR
1957 || code
== ROUND_DIV_EXPR
)
1958 return (needs_overflow_infinity (TREE_TYPE (res
))
1959 ? positive_overflow_infinity (TREE_TYPE (res
))
1960 : TYPE_MAX_VALUE (TREE_TYPE (res
)));
1962 return (needs_overflow_infinity (TREE_TYPE (res
))
1963 ? negative_overflow_infinity (TREE_TYPE (res
))
1964 : TYPE_MIN_VALUE (TREE_TYPE (res
)));
1971 /* For range VR compute two double_int bitmasks. In *MAY_BE_NONZERO
1972 bitmask if some bit is unset, it means for all numbers in the range
1973 the bit is 0, otherwise it might be 0 or 1. In *MUST_BE_NONZERO
1974 bitmask if some bit is set, it means for all numbers in the range
1975 the bit is 1, otherwise it might be 0 or 1. */
1978 zero_nonzero_bits_from_vr (value_range_t
*vr
,
1979 double_int
*may_be_nonzero
,
1980 double_int
*must_be_nonzero
)
1982 *may_be_nonzero
= double_int_minus_one
;
1983 *must_be_nonzero
= double_int_zero
;
1984 if (!range_int_cst_p (vr
)
1985 || is_overflow_infinity (vr
->min
)
1986 || is_overflow_infinity (vr
->max
))
1989 if (range_int_cst_singleton_p (vr
))
1991 *may_be_nonzero
= tree_to_double_int (vr
->min
);
1992 *must_be_nonzero
= *may_be_nonzero
;
1994 else if (tree_int_cst_sgn (vr
->min
) >= 0
1995 || tree_int_cst_sgn (vr
->max
) < 0)
1997 double_int dmin
= tree_to_double_int (vr
->min
);
1998 double_int dmax
= tree_to_double_int (vr
->max
);
1999 double_int xor_mask
= dmin
^ dmax
;
2000 *may_be_nonzero
= dmin
| dmax
;
2001 *must_be_nonzero
= dmin
& dmax
;
2002 if (xor_mask
.high
!= 0)
2004 unsigned HOST_WIDE_INT mask
2005 = ((unsigned HOST_WIDE_INT
) 1
2006 << floor_log2 (xor_mask
.high
)) - 1;
2007 may_be_nonzero
->low
= ALL_ONES
;
2008 may_be_nonzero
->high
|= mask
;
2009 must_be_nonzero
->low
= 0;
2010 must_be_nonzero
->high
&= ~mask
;
2012 else if (xor_mask
.low
!= 0)
2014 unsigned HOST_WIDE_INT mask
2015 = ((unsigned HOST_WIDE_INT
) 1
2016 << floor_log2 (xor_mask
.low
)) - 1;
2017 may_be_nonzero
->low
|= mask
;
2018 must_be_nonzero
->low
&= ~mask
;
2025 /* Create two value-ranges in *VR0 and *VR1 from the anti-range *AR
2026 so that *VR0 U *VR1 == *AR. Returns true if that is possible,
2027 false otherwise. If *AR can be represented with a single range
2028 *VR1 will be VR_UNDEFINED. */
2031 ranges_from_anti_range (value_range_t
*ar
,
2032 value_range_t
*vr0
, value_range_t
*vr1
)
2034 tree type
= TREE_TYPE (ar
->min
);
2036 vr0
->type
= VR_UNDEFINED
;
2037 vr1
->type
= VR_UNDEFINED
;
2039 if (ar
->type
!= VR_ANTI_RANGE
2040 || TREE_CODE (ar
->min
) != INTEGER_CST
2041 || TREE_CODE (ar
->max
) != INTEGER_CST
2042 || !vrp_val_min (type
)
2043 || !vrp_val_max (type
))
2046 if (!vrp_val_is_min (ar
->min
))
2048 vr0
->type
= VR_RANGE
;
2049 vr0
->min
= vrp_val_min (type
);
2051 = double_int_to_tree (type
,
2052 tree_to_double_int (ar
->min
) - double_int_one
);
2054 if (!vrp_val_is_max (ar
->max
))
2056 vr1
->type
= VR_RANGE
;
2058 = double_int_to_tree (type
,
2059 tree_to_double_int (ar
->max
) + double_int_one
);
2060 vr1
->max
= vrp_val_max (type
);
2062 if (vr0
->type
== VR_UNDEFINED
)
2065 vr1
->type
= VR_UNDEFINED
;
2068 return vr0
->type
!= VR_UNDEFINED
;
2071 /* Helper to extract a value-range *VR for a multiplicative operation
2075 extract_range_from_multiplicative_op_1 (value_range_t
*vr
,
2076 enum tree_code code
,
2077 value_range_t
*vr0
, value_range_t
*vr1
)
2079 enum value_range_type type
;
2086 /* Multiplications, divisions and shifts are a bit tricky to handle,
2087 depending on the mix of signs we have in the two ranges, we
2088 need to operate on different values to get the minimum and
2089 maximum values for the new range. One approach is to figure
2090 out all the variations of range combinations and do the
2093 However, this involves several calls to compare_values and it
2094 is pretty convoluted. It's simpler to do the 4 operations
2095 (MIN0 OP MIN1, MIN0 OP MAX1, MAX0 OP MIN1 and MAX0 OP MAX0 OP
2096 MAX1) and then figure the smallest and largest values to form
2098 gcc_assert (code
== MULT_EXPR
2099 || code
== TRUNC_DIV_EXPR
2100 || code
== FLOOR_DIV_EXPR
2101 || code
== CEIL_DIV_EXPR
2102 || code
== EXACT_DIV_EXPR
2103 || code
== ROUND_DIV_EXPR
2104 || code
== RSHIFT_EXPR
2105 || code
== LSHIFT_EXPR
);
2106 gcc_assert ((vr0
->type
== VR_RANGE
2107 || (code
== MULT_EXPR
&& vr0
->type
== VR_ANTI_RANGE
))
2108 && vr0
->type
== vr1
->type
);
2112 /* Compute the 4 cross operations. */
2114 val
[0] = vrp_int_const_binop (code
, vr0
->min
, vr1
->min
);
2115 if (val
[0] == NULL_TREE
)
2118 if (vr1
->max
== vr1
->min
)
2122 val
[1] = vrp_int_const_binop (code
, vr0
->min
, vr1
->max
);
2123 if (val
[1] == NULL_TREE
)
2127 if (vr0
->max
== vr0
->min
)
2131 val
[2] = vrp_int_const_binop (code
, vr0
->max
, vr1
->min
);
2132 if (val
[2] == NULL_TREE
)
2136 if (vr0
->min
== vr0
->max
|| vr1
->min
== vr1
->max
)
2140 val
[3] = vrp_int_const_binop (code
, vr0
->max
, vr1
->max
);
2141 if (val
[3] == NULL_TREE
)
2147 set_value_range_to_varying (vr
);
2151 /* Set MIN to the minimum of VAL[i] and MAX to the maximum
2155 for (i
= 1; i
< 4; i
++)
2157 if (!is_gimple_min_invariant (min
)
2158 || (TREE_OVERFLOW (min
) && !is_overflow_infinity (min
))
2159 || !is_gimple_min_invariant (max
)
2160 || (TREE_OVERFLOW (max
) && !is_overflow_infinity (max
)))
2165 if (!is_gimple_min_invariant (val
[i
])
2166 || (TREE_OVERFLOW (val
[i
])
2167 && !is_overflow_infinity (val
[i
])))
2169 /* If we found an overflowed value, set MIN and MAX
2170 to it so that we set the resulting range to
2176 if (compare_values (val
[i
], min
) == -1)
2179 if (compare_values (val
[i
], max
) == 1)
2184 /* If either MIN or MAX overflowed, then set the resulting range to
2185 VARYING. But we do accept an overflow infinity
2187 if (min
== NULL_TREE
2188 || !is_gimple_min_invariant (min
)
2189 || (TREE_OVERFLOW (min
) && !is_overflow_infinity (min
))
2191 || !is_gimple_min_invariant (max
)
2192 || (TREE_OVERFLOW (max
) && !is_overflow_infinity (max
)))
2194 set_value_range_to_varying (vr
);
2200 2) [-INF, +-INF(OVF)]
2201 3) [+-INF(OVF), +INF]
2202 4) [+-INF(OVF), +-INF(OVF)]
2203 We learn nothing when we have INF and INF(OVF) on both sides.
2204 Note that we do accept [-INF, -INF] and [+INF, +INF] without
2206 if ((vrp_val_is_min (min
) || is_overflow_infinity (min
))
2207 && (vrp_val_is_max (max
) || is_overflow_infinity (max
)))
2209 set_value_range_to_varying (vr
);
2213 cmp
= compare_values (min
, max
);
2214 if (cmp
== -2 || cmp
== 1)
2216 /* If the new range has its limits swapped around (MIN > MAX),
2217 then the operation caused one of them to wrap around, mark
2218 the new range VARYING. */
2219 set_value_range_to_varying (vr
);
2222 set_value_range (vr
, type
, min
, max
, NULL
);
2225 /* Some quadruple precision helpers. */
2227 quad_int_cmp (double_int l0
, double_int h0
,
2228 double_int l1
, double_int h1
, bool uns
)
2230 int c
= h0
.cmp (h1
, uns
);
2231 if (c
!= 0) return c
;
2232 return l0
.ucmp (l1
);
2236 quad_int_pair_sort (double_int
*l0
, double_int
*h0
,
2237 double_int
*l1
, double_int
*h1
, bool uns
)
2239 if (quad_int_cmp (*l0
, *h0
, *l1
, *h1
, uns
) > 0)
2242 tmp
= *l0
; *l0
= *l1
; *l1
= tmp
;
2243 tmp
= *h0
; *h0
= *h1
; *h1
= tmp
;
2247 /* Extract range information from a binary operation CODE based on
2248 the ranges of each of its operands, *VR0 and *VR1 with resulting
2249 type EXPR_TYPE. The resulting range is stored in *VR. */
2252 extract_range_from_binary_expr_1 (value_range_t
*vr
,
2253 enum tree_code code
, tree expr_type
,
2254 value_range_t
*vr0_
, value_range_t
*vr1_
)
2256 value_range_t vr0
= *vr0_
, vr1
= *vr1_
;
2257 value_range_t vrtem0
= VR_INITIALIZER
, vrtem1
= VR_INITIALIZER
;
2258 enum value_range_type type
;
2259 tree min
= NULL_TREE
, max
= NULL_TREE
;
2262 if (!INTEGRAL_TYPE_P (expr_type
)
2263 && !POINTER_TYPE_P (expr_type
))
2265 set_value_range_to_varying (vr
);
2269 /* Not all binary expressions can be applied to ranges in a
2270 meaningful way. Handle only arithmetic operations. */
2271 if (code
!= PLUS_EXPR
2272 && code
!= MINUS_EXPR
2273 && code
!= POINTER_PLUS_EXPR
2274 && code
!= MULT_EXPR
2275 && code
!= TRUNC_DIV_EXPR
2276 && code
!= FLOOR_DIV_EXPR
2277 && code
!= CEIL_DIV_EXPR
2278 && code
!= EXACT_DIV_EXPR
2279 && code
!= ROUND_DIV_EXPR
2280 && code
!= TRUNC_MOD_EXPR
2281 && code
!= RSHIFT_EXPR
2282 && code
!= LSHIFT_EXPR
2285 && code
!= BIT_AND_EXPR
2286 && code
!= BIT_IOR_EXPR
2287 && code
!= BIT_XOR_EXPR
)
2289 set_value_range_to_varying (vr
);
2293 /* If both ranges are UNDEFINED, so is the result. */
2294 if (vr0
.type
== VR_UNDEFINED
&& vr1
.type
== VR_UNDEFINED
)
2296 set_value_range_to_undefined (vr
);
2299 /* If one of the ranges is UNDEFINED drop it to VARYING for the following
2300 code. At some point we may want to special-case operations that
2301 have UNDEFINED result for all or some value-ranges of the not UNDEFINED
2303 else if (vr0
.type
== VR_UNDEFINED
)
2304 set_value_range_to_varying (&vr0
);
2305 else if (vr1
.type
== VR_UNDEFINED
)
2306 set_value_range_to_varying (&vr1
);
2308 /* Now canonicalize anti-ranges to ranges when they are not symbolic
2309 and express ~[] op X as ([]' op X) U ([]'' op X). */
2310 if (vr0
.type
== VR_ANTI_RANGE
2311 && ranges_from_anti_range (&vr0
, &vrtem0
, &vrtem1
))
2313 extract_range_from_binary_expr_1 (vr
, code
, expr_type
, &vrtem0
, vr1_
);
2314 if (vrtem1
.type
!= VR_UNDEFINED
)
2316 value_range_t vrres
= VR_INITIALIZER
;
2317 extract_range_from_binary_expr_1 (&vrres
, code
, expr_type
,
2319 vrp_meet (vr
, &vrres
);
2323 /* Likewise for X op ~[]. */
2324 if (vr1
.type
== VR_ANTI_RANGE
2325 && ranges_from_anti_range (&vr1
, &vrtem0
, &vrtem1
))
2327 extract_range_from_binary_expr_1 (vr
, code
, expr_type
, vr0_
, &vrtem0
);
2328 if (vrtem1
.type
!= VR_UNDEFINED
)
2330 value_range_t vrres
= VR_INITIALIZER
;
2331 extract_range_from_binary_expr_1 (&vrres
, code
, expr_type
,
2333 vrp_meet (vr
, &vrres
);
2338 /* The type of the resulting value range defaults to VR0.TYPE. */
2341 /* Refuse to operate on VARYING ranges, ranges of different kinds
2342 and symbolic ranges. As an exception, we allow BIT_AND_EXPR
2343 because we may be able to derive a useful range even if one of
2344 the operands is VR_VARYING or symbolic range. Similarly for
2345 divisions. TODO, we may be able to derive anti-ranges in
2347 if (code
!= BIT_AND_EXPR
2348 && code
!= BIT_IOR_EXPR
2349 && code
!= TRUNC_DIV_EXPR
2350 && code
!= FLOOR_DIV_EXPR
2351 && code
!= CEIL_DIV_EXPR
2352 && code
!= EXACT_DIV_EXPR
2353 && code
!= ROUND_DIV_EXPR
2354 && code
!= TRUNC_MOD_EXPR
2357 && (vr0
.type
== VR_VARYING
2358 || vr1
.type
== VR_VARYING
2359 || vr0
.type
!= vr1
.type
2360 || symbolic_range_p (&vr0
)
2361 || symbolic_range_p (&vr1
)))
2363 set_value_range_to_varying (vr
);
2367 /* Now evaluate the expression to determine the new range. */
2368 if (POINTER_TYPE_P (expr_type
))
2370 if (code
== MIN_EXPR
|| code
== MAX_EXPR
)
2372 /* For MIN/MAX expressions with pointers, we only care about
2373 nullness, if both are non null, then the result is nonnull.
2374 If both are null, then the result is null. Otherwise they
2376 if (range_is_nonnull (&vr0
) && range_is_nonnull (&vr1
))
2377 set_value_range_to_nonnull (vr
, expr_type
);
2378 else if (range_is_null (&vr0
) && range_is_null (&vr1
))
2379 set_value_range_to_null (vr
, expr_type
);
2381 set_value_range_to_varying (vr
);
2383 else if (code
== POINTER_PLUS_EXPR
)
2385 /* For pointer types, we are really only interested in asserting
2386 whether the expression evaluates to non-NULL. */
2387 if (range_is_nonnull (&vr0
) || range_is_nonnull (&vr1
))
2388 set_value_range_to_nonnull (vr
, expr_type
);
2389 else if (range_is_null (&vr0
) && range_is_null (&vr1
))
2390 set_value_range_to_null (vr
, expr_type
);
2392 set_value_range_to_varying (vr
);
2394 else if (code
== BIT_AND_EXPR
)
2396 /* For pointer types, we are really only interested in asserting
2397 whether the expression evaluates to non-NULL. */
2398 if (range_is_nonnull (&vr0
) && range_is_nonnull (&vr1
))
2399 set_value_range_to_nonnull (vr
, expr_type
);
2400 else if (range_is_null (&vr0
) || range_is_null (&vr1
))
2401 set_value_range_to_null (vr
, expr_type
);
2403 set_value_range_to_varying (vr
);
2406 set_value_range_to_varying (vr
);
2411 /* For integer ranges, apply the operation to each end of the
2412 range and see what we end up with. */
2413 if (code
== PLUS_EXPR
|| code
== MINUS_EXPR
)
2415 /* If we have a PLUS_EXPR with two VR_RANGE integer constant
2416 ranges compute the precise range for such case if possible. */
2417 if (range_int_cst_p (&vr0
)
2418 && range_int_cst_p (&vr1
)
2419 /* We need as many bits as the possibly unsigned inputs. */
2420 && TYPE_PRECISION (expr_type
) <= HOST_BITS_PER_DOUBLE_INT
)
2422 double_int min0
= tree_to_double_int (vr0
.min
);
2423 double_int max0
= tree_to_double_int (vr0
.max
);
2424 double_int min1
= tree_to_double_int (vr1
.min
);
2425 double_int max1
= tree_to_double_int (vr1
.max
);
2426 bool uns
= TYPE_UNSIGNED (expr_type
);
2428 = double_int::min_value (TYPE_PRECISION (expr_type
), uns
);
2430 = double_int::max_value (TYPE_PRECISION (expr_type
), uns
);
2431 double_int dmin
, dmax
;
2435 if (code
== PLUS_EXPR
)
2440 /* Check for overflow in double_int. */
2441 if (min1
.cmp (double_int_zero
, uns
) != dmin
.cmp (min0
, uns
))
2442 min_ovf
= min0
.cmp (dmin
, uns
);
2443 if (max1
.cmp (double_int_zero
, uns
) != dmax
.cmp (max0
, uns
))
2444 max_ovf
= max0
.cmp (dmax
, uns
);
2446 else /* if (code == MINUS_EXPR) */
2451 if (double_int_zero
.cmp (max1
, uns
) != dmin
.cmp (min0
, uns
))
2452 min_ovf
= min0
.cmp (max1
, uns
);
2453 if (double_int_zero
.cmp (min1
, uns
) != dmax
.cmp (max0
, uns
))
2454 max_ovf
= max0
.cmp (min1
, uns
);
2457 /* For non-wrapping arithmetic look at possibly smaller
2458 value-ranges of the type. */
2459 if (!TYPE_OVERFLOW_WRAPS (expr_type
))
2461 if (vrp_val_min (expr_type
))
2462 type_min
= tree_to_double_int (vrp_val_min (expr_type
));
2463 if (vrp_val_max (expr_type
))
2464 type_max
= tree_to_double_int (vrp_val_max (expr_type
));
2467 /* Check for type overflow. */
2470 if (dmin
.cmp (type_min
, uns
) == -1)
2472 else if (dmin
.cmp (type_max
, uns
) == 1)
2477 if (dmax
.cmp (type_min
, uns
) == -1)
2479 else if (dmax
.cmp (type_max
, uns
) == 1)
2483 if (TYPE_OVERFLOW_WRAPS (expr_type
))
2485 /* If overflow wraps, truncate the values and adjust the
2486 range kind and bounds appropriately. */
2488 = dmin
.ext (TYPE_PRECISION (expr_type
), uns
);
2490 = dmax
.ext (TYPE_PRECISION (expr_type
), uns
);
2491 if (min_ovf
== max_ovf
)
2493 /* No overflow or both overflow or underflow. The
2494 range kind stays VR_RANGE. */
2495 min
= double_int_to_tree (expr_type
, tmin
);
2496 max
= double_int_to_tree (expr_type
, tmax
);
2498 else if (min_ovf
== -1
2501 /* Underflow and overflow, drop to VR_VARYING. */
2502 set_value_range_to_varying (vr
);
2507 /* Min underflow or max overflow. The range kind
2508 changes to VR_ANTI_RANGE. */
2509 bool covers
= false;
2510 double_int tem
= tmin
;
2511 gcc_assert ((min_ovf
== -1 && max_ovf
== 0)
2512 || (max_ovf
== 1 && min_ovf
== 0));
2513 type
= VR_ANTI_RANGE
;
2514 tmin
= tmax
+ double_int_one
;
2515 if (tmin
.cmp (tmax
, uns
) < 0)
2517 tmax
= tem
+ double_int_minus_one
;
2518 if (tmax
.cmp (tem
, uns
) > 0)
2520 /* If the anti-range would cover nothing, drop to varying.
2521 Likewise if the anti-range bounds are outside of the
2523 if (covers
|| tmin
.cmp (tmax
, uns
) > 0)
2525 set_value_range_to_varying (vr
);
2528 min
= double_int_to_tree (expr_type
, tmin
);
2529 max
= double_int_to_tree (expr_type
, tmax
);
2534 /* If overflow does not wrap, saturate to the types min/max
2538 if (needs_overflow_infinity (expr_type
)
2539 && supports_overflow_infinity (expr_type
))
2540 min
= negative_overflow_infinity (expr_type
);
2542 min
= double_int_to_tree (expr_type
, type_min
);
2544 else if (min_ovf
== 1)
2546 if (needs_overflow_infinity (expr_type
)
2547 && supports_overflow_infinity (expr_type
))
2548 min
= positive_overflow_infinity (expr_type
);
2550 min
= double_int_to_tree (expr_type
, type_max
);
2553 min
= double_int_to_tree (expr_type
, dmin
);
2557 if (needs_overflow_infinity (expr_type
)
2558 && supports_overflow_infinity (expr_type
))
2559 max
= negative_overflow_infinity (expr_type
);
2561 max
= double_int_to_tree (expr_type
, type_min
);
2563 else if (max_ovf
== 1)
2565 if (needs_overflow_infinity (expr_type
)
2566 && supports_overflow_infinity (expr_type
))
2567 max
= positive_overflow_infinity (expr_type
);
2569 max
= double_int_to_tree (expr_type
, type_max
);
2572 max
= double_int_to_tree (expr_type
, dmax
);
2574 if (needs_overflow_infinity (expr_type
)
2575 && supports_overflow_infinity (expr_type
))
2577 if (is_negative_overflow_infinity (vr0
.min
)
2578 || (code
== PLUS_EXPR
2579 ? is_negative_overflow_infinity (vr1
.min
)
2580 : is_positive_overflow_infinity (vr1
.max
)))
2581 min
= negative_overflow_infinity (expr_type
);
2582 if (is_positive_overflow_infinity (vr0
.max
)
2583 || (code
== PLUS_EXPR
2584 ? is_positive_overflow_infinity (vr1
.max
)
2585 : is_negative_overflow_infinity (vr1
.min
)))
2586 max
= positive_overflow_infinity (expr_type
);
2591 /* For other cases, for example if we have a PLUS_EXPR with two
2592 VR_ANTI_RANGEs, drop to VR_VARYING. It would take more effort
2593 to compute a precise range for such a case.
2594 ??? General even mixed range kind operations can be expressed
2595 by for example transforming ~[3, 5] + [1, 2] to range-only
2596 operations and a union primitive:
2597 [-INF, 2] + [1, 2] U [5, +INF] + [1, 2]
2598 [-INF+1, 4] U [6, +INF(OVF)]
2599 though usually the union is not exactly representable with
2600 a single range or anti-range as the above is
2601 [-INF+1, +INF(OVF)] intersected with ~[5, 5]
2602 but one could use a scheme similar to equivalences for this. */
2603 set_value_range_to_varying (vr
);
2607 else if (code
== MIN_EXPR
2608 || code
== MAX_EXPR
)
2610 if (vr0
.type
== VR_RANGE
2611 && !symbolic_range_p (&vr0
))
2614 if (vr1
.type
== VR_RANGE
2615 && !symbolic_range_p (&vr1
))
2617 /* For operations that make the resulting range directly
2618 proportional to the original ranges, apply the operation to
2619 the same end of each range. */
2620 min
= vrp_int_const_binop (code
, vr0
.min
, vr1
.min
);
2621 max
= vrp_int_const_binop (code
, vr0
.max
, vr1
.max
);
2623 else if (code
== MIN_EXPR
)
2625 min
= vrp_val_min (expr_type
);
2628 else if (code
== MAX_EXPR
)
2631 max
= vrp_val_max (expr_type
);
2634 else if (vr1
.type
== VR_RANGE
2635 && !symbolic_range_p (&vr1
))
2638 if (code
== MIN_EXPR
)
2640 min
= vrp_val_min (expr_type
);
2643 else if (code
== MAX_EXPR
)
2646 max
= vrp_val_max (expr_type
);
2651 set_value_range_to_varying (vr
);
2655 else if (code
== MULT_EXPR
)
2657 /* Fancy code so that with unsigned, [-3,-1]*[-3,-1] does not
2659 if (range_int_cst_p (&vr0
)
2660 && range_int_cst_p (&vr1
)
2661 && TYPE_OVERFLOW_WRAPS (expr_type
))
2663 double_int min0
, max0
, min1
, max1
, sizem1
, size
;
2664 double_int prod0l
, prod0h
, prod1l
, prod1h
,
2665 prod2l
, prod2h
, prod3l
, prod3h
;
2666 bool uns0
, uns1
, uns
;
2668 sizem1
= double_int::max_value (TYPE_PRECISION (expr_type
), true);
2669 size
= sizem1
+ double_int_one
;
2671 min0
= tree_to_double_int (vr0
.min
);
2672 max0
= tree_to_double_int (vr0
.max
);
2673 min1
= tree_to_double_int (vr1
.min
);
2674 max1
= tree_to_double_int (vr1
.max
);
2676 uns0
= TYPE_UNSIGNED (expr_type
);
2679 /* Canonicalize the intervals. */
2680 if (TYPE_UNSIGNED (expr_type
))
2682 double_int min2
= size
- min0
;
2683 if (!min2
.is_zero () && min2
.cmp (max0
, true) < 0)
2691 if (!min2
.is_zero () && min2
.cmp (max1
, true) < 0)
2701 prod0l
= min0
.wide_mul_with_sign (min1
, true, &prod0h
, &overflow
);
2702 if (!uns0
&& min0
.is_negative ())
2704 if (!uns1
&& min1
.is_negative ())
2707 prod1l
= min0
.wide_mul_with_sign (max1
, true, &prod1h
, &overflow
);
2708 if (!uns0
&& min0
.is_negative ())
2710 if (!uns1
&& max1
.is_negative ())
2713 prod2l
= max0
.wide_mul_with_sign (min1
, true, &prod2h
, &overflow
);
2714 if (!uns0
&& max0
.is_negative ())
2716 if (!uns1
&& min1
.is_negative ())
2719 prod3l
= max0
.wide_mul_with_sign (max1
, true, &prod3h
, &overflow
);
2720 if (!uns0
&& max0
.is_negative ())
2722 if (!uns1
&& max1
.is_negative ())
2725 /* Sort the 4 products. */
2726 quad_int_pair_sort (&prod0l
, &prod0h
, &prod3l
, &prod3h
, uns
);
2727 quad_int_pair_sort (&prod1l
, &prod1h
, &prod2l
, &prod2h
, uns
);
2728 quad_int_pair_sort (&prod0l
, &prod0h
, &prod1l
, &prod1h
, uns
);
2729 quad_int_pair_sort (&prod2l
, &prod2h
, &prod3l
, &prod3h
, uns
);
2732 if (prod0l
.is_zero ())
2734 prod1l
= double_int_zero
;
2742 prod2l
= prod3l
+ prod1l
;
2743 prod2h
= prod3h
+ prod1h
;
2744 if (prod2l
.ult (prod3l
))
2745 prod2h
+= double_int_one
; /* carry */
2747 if (!prod2h
.is_zero ()
2748 || prod2l
.cmp (sizem1
, true) >= 0)
2750 /* the range covers all values. */
2751 set_value_range_to_varying (vr
);
2755 /* The following should handle the wrapping and selecting
2756 VR_ANTI_RANGE for us. */
2757 min
= double_int_to_tree (expr_type
, prod0l
);
2758 max
= double_int_to_tree (expr_type
, prod3l
);
2759 set_and_canonicalize_value_range (vr
, VR_RANGE
, min
, max
, NULL
);
2763 /* If we have an unsigned MULT_EXPR with two VR_ANTI_RANGEs,
2764 drop to VR_VARYING. It would take more effort to compute a
2765 precise range for such a case. For example, if we have
2766 op0 == 65536 and op1 == 65536 with their ranges both being
2767 ~[0,0] on a 32-bit machine, we would have op0 * op1 == 0, so
2768 we cannot claim that the product is in ~[0,0]. Note that we
2769 are guaranteed to have vr0.type == vr1.type at this
2771 if (vr0
.type
== VR_ANTI_RANGE
2772 && !TYPE_OVERFLOW_UNDEFINED (expr_type
))
2774 set_value_range_to_varying (vr
);
2778 extract_range_from_multiplicative_op_1 (vr
, code
, &vr0
, &vr1
);
2781 else if (code
== RSHIFT_EXPR
2782 || code
== LSHIFT_EXPR
)
2784 /* If we have a RSHIFT_EXPR with any shift values outside [0..prec-1],
2785 then drop to VR_VARYING. Outside of this range we get undefined
2786 behavior from the shift operation. We cannot even trust
2787 SHIFT_COUNT_TRUNCATED at this stage, because that applies to rtl
2788 shifts, and the operation at the tree level may be widened. */
2789 if (range_int_cst_p (&vr1
)
2790 && compare_tree_int (vr1
.min
, 0) >= 0
2791 && compare_tree_int (vr1
.max
, TYPE_PRECISION (expr_type
)) == -1)
2793 if (code
== RSHIFT_EXPR
)
2795 extract_range_from_multiplicative_op_1 (vr
, code
, &vr0
, &vr1
);
2798 /* We can map lshifts by constants to MULT_EXPR handling. */
2799 else if (code
== LSHIFT_EXPR
2800 && range_int_cst_singleton_p (&vr1
))
2802 bool saved_flag_wrapv
;
2803 value_range_t vr1p
= VR_INITIALIZER
;
2804 vr1p
.type
= VR_RANGE
;
2806 = double_int_to_tree (expr_type
,
2808 .llshift (TREE_INT_CST_LOW (vr1
.min
),
2809 TYPE_PRECISION (expr_type
)));
2810 vr1p
.max
= vr1p
.min
;
2811 /* We have to use a wrapping multiply though as signed overflow
2812 on lshifts is implementation defined in C89. */
2813 saved_flag_wrapv
= flag_wrapv
;
2815 extract_range_from_binary_expr_1 (vr
, MULT_EXPR
, expr_type
,
2817 flag_wrapv
= saved_flag_wrapv
;
2820 else if (code
== LSHIFT_EXPR
2821 && range_int_cst_p (&vr0
))
2823 int prec
= TYPE_PRECISION (expr_type
);
2824 int overflow_pos
= prec
;
2826 double_int bound
, complement
, low_bound
, high_bound
;
2827 bool uns
= TYPE_UNSIGNED (expr_type
);
2828 bool in_bounds
= false;
2833 bound_shift
= overflow_pos
- TREE_INT_CST_LOW (vr1
.max
);
2834 /* If bound_shift == HOST_BITS_PER_DOUBLE_INT, the llshift can
2835 overflow. However, for that to happen, vr1.max needs to be
2836 zero, which means vr1 is a singleton range of zero, which
2837 means it should be handled by the previous LSHIFT_EXPR
2839 bound
= double_int_one
.llshift (bound_shift
, prec
);
2840 complement
= ~(bound
- double_int_one
);
2844 low_bound
= bound
.zext (prec
);
2845 high_bound
= complement
.zext (prec
);
2846 if (tree_to_double_int (vr0
.max
).ult (low_bound
))
2848 /* [5, 6] << [1, 2] == [10, 24]. */
2849 /* We're shifting out only zeroes, the value increases
2853 else if (high_bound
.ult (tree_to_double_int (vr0
.min
)))
2855 /* [0xffffff00, 0xffffffff] << [1, 2]
2856 == [0xfffffc00, 0xfffffffe]. */
2857 /* We're shifting out only ones, the value decreases
2864 /* [-1, 1] << [1, 2] == [-4, 4]. */
2865 low_bound
= complement
.sext (prec
);
2867 if (tree_to_double_int (vr0
.max
).slt (high_bound
)
2868 && low_bound
.slt (tree_to_double_int (vr0
.min
)))
2870 /* For non-negative numbers, we're shifting out only
2871 zeroes, the value increases monotonically.
2872 For negative numbers, we're shifting out only ones, the
2873 value decreases monotomically. */
2880 extract_range_from_multiplicative_op_1 (vr
, code
, &vr0
, &vr1
);
2885 set_value_range_to_varying (vr
);
2888 else if (code
== TRUNC_DIV_EXPR
2889 || code
== FLOOR_DIV_EXPR
2890 || code
== CEIL_DIV_EXPR
2891 || code
== EXACT_DIV_EXPR
2892 || code
== ROUND_DIV_EXPR
)
2894 if (vr0
.type
!= VR_RANGE
|| symbolic_range_p (&vr0
))
2896 /* For division, if op1 has VR_RANGE but op0 does not, something
2897 can be deduced just from that range. Say [min, max] / [4, max]
2898 gives [min / 4, max / 4] range. */
2899 if (vr1
.type
== VR_RANGE
2900 && !symbolic_range_p (&vr1
)
2901 && range_includes_zero_p (vr1
.min
, vr1
.max
) == 0)
2903 vr0
.type
= type
= VR_RANGE
;
2904 vr0
.min
= vrp_val_min (expr_type
);
2905 vr0
.max
= vrp_val_max (expr_type
);
2909 set_value_range_to_varying (vr
);
2914 /* For divisions, if flag_non_call_exceptions is true, we must
2915 not eliminate a division by zero. */
2916 if (cfun
->can_throw_non_call_exceptions
2917 && (vr1
.type
!= VR_RANGE
2918 || range_includes_zero_p (vr1
.min
, vr1
.max
) != 0))
2920 set_value_range_to_varying (vr
);
2924 /* For divisions, if op0 is VR_RANGE, we can deduce a range
2925 even if op1 is VR_VARYING, VR_ANTI_RANGE, symbolic or can
2927 if (vr0
.type
== VR_RANGE
2928 && (vr1
.type
!= VR_RANGE
2929 || range_includes_zero_p (vr1
.min
, vr1
.max
) != 0))
2931 tree zero
= build_int_cst (TREE_TYPE (vr0
.min
), 0);
2936 if (TYPE_UNSIGNED (expr_type
)
2937 || value_range_nonnegative_p (&vr1
))
2939 /* For unsigned division or when divisor is known
2940 to be non-negative, the range has to cover
2941 all numbers from 0 to max for positive max
2942 and all numbers from min to 0 for negative min. */
2943 cmp
= compare_values (vr0
.max
, zero
);
2946 else if (cmp
== 0 || cmp
== 1)
2950 cmp
= compare_values (vr0
.min
, zero
);
2953 else if (cmp
== 0 || cmp
== -1)
2960 /* Otherwise the range is -max .. max or min .. -min
2961 depending on which bound is bigger in absolute value,
2962 as the division can change the sign. */
2963 abs_extent_range (vr
, vr0
.min
, vr0
.max
);
2966 if (type
== VR_VARYING
)
2968 set_value_range_to_varying (vr
);
2974 extract_range_from_multiplicative_op_1 (vr
, code
, &vr0
, &vr1
);
2978 else if (code
== TRUNC_MOD_EXPR
)
2980 if (vr1
.type
!= VR_RANGE
2981 || range_includes_zero_p (vr1
.min
, vr1
.max
) != 0
2982 || vrp_val_is_min (vr1
.min
))
2984 set_value_range_to_varying (vr
);
2988 /* Compute MAX <|vr1.min|, |vr1.max|> - 1. */
2989 max
= fold_unary_to_constant (ABS_EXPR
, expr_type
, vr1
.min
);
2990 if (tree_int_cst_lt (max
, vr1
.max
))
2992 max
= int_const_binop (MINUS_EXPR
, max
, integer_one_node
);
2993 /* If the dividend is non-negative the modulus will be
2994 non-negative as well. */
2995 if (TYPE_UNSIGNED (expr_type
)
2996 || value_range_nonnegative_p (&vr0
))
2997 min
= build_int_cst (TREE_TYPE (max
), 0);
2999 min
= fold_unary_to_constant (NEGATE_EXPR
, expr_type
, max
);
3001 else if (code
== BIT_AND_EXPR
|| code
== BIT_IOR_EXPR
|| code
== BIT_XOR_EXPR
)
3003 bool int_cst_range0
, int_cst_range1
;
3004 double_int may_be_nonzero0
, may_be_nonzero1
;
3005 double_int must_be_nonzero0
, must_be_nonzero1
;
3007 int_cst_range0
= zero_nonzero_bits_from_vr (&vr0
, &may_be_nonzero0
,
3009 int_cst_range1
= zero_nonzero_bits_from_vr (&vr1
, &may_be_nonzero1
,
3013 if (code
== BIT_AND_EXPR
)
3016 min
= double_int_to_tree (expr_type
,
3017 must_be_nonzero0
& must_be_nonzero1
);
3018 dmax
= may_be_nonzero0
& may_be_nonzero1
;
3019 /* If both input ranges contain only negative values we can
3020 truncate the result range maximum to the minimum of the
3021 input range maxima. */
3022 if (int_cst_range0
&& int_cst_range1
3023 && tree_int_cst_sgn (vr0
.max
) < 0
3024 && tree_int_cst_sgn (vr1
.max
) < 0)
3026 dmax
= dmax
.min (tree_to_double_int (vr0
.max
),
3027 TYPE_UNSIGNED (expr_type
));
3028 dmax
= dmax
.min (tree_to_double_int (vr1
.max
),
3029 TYPE_UNSIGNED (expr_type
));
3031 /* If either input range contains only non-negative values
3032 we can truncate the result range maximum to the respective
3033 maximum of the input range. */
3034 if (int_cst_range0
&& tree_int_cst_sgn (vr0
.min
) >= 0)
3035 dmax
= dmax
.min (tree_to_double_int (vr0
.max
),
3036 TYPE_UNSIGNED (expr_type
));
3037 if (int_cst_range1
&& tree_int_cst_sgn (vr1
.min
) >= 0)
3038 dmax
= dmax
.min (tree_to_double_int (vr1
.max
),
3039 TYPE_UNSIGNED (expr_type
));
3040 max
= double_int_to_tree (expr_type
, dmax
);
3042 else if (code
== BIT_IOR_EXPR
)
3045 max
= double_int_to_tree (expr_type
,
3046 may_be_nonzero0
| may_be_nonzero1
);
3047 dmin
= must_be_nonzero0
| must_be_nonzero1
;
3048 /* If the input ranges contain only positive values we can
3049 truncate the minimum of the result range to the maximum
3050 of the input range minima. */
3051 if (int_cst_range0
&& int_cst_range1
3052 && tree_int_cst_sgn (vr0
.min
) >= 0
3053 && tree_int_cst_sgn (vr1
.min
) >= 0)
3055 dmin
= dmin
.max (tree_to_double_int (vr0
.min
),
3056 TYPE_UNSIGNED (expr_type
));
3057 dmin
= dmin
.max (tree_to_double_int (vr1
.min
),
3058 TYPE_UNSIGNED (expr_type
));
3060 /* If either input range contains only negative values
3061 we can truncate the minimum of the result range to the
3062 respective minimum range. */
3063 if (int_cst_range0
&& tree_int_cst_sgn (vr0
.max
) < 0)
3064 dmin
= dmin
.max (tree_to_double_int (vr0
.min
),
3065 TYPE_UNSIGNED (expr_type
));
3066 if (int_cst_range1
&& tree_int_cst_sgn (vr1
.max
) < 0)
3067 dmin
= dmin
.max (tree_to_double_int (vr1
.min
),
3068 TYPE_UNSIGNED (expr_type
));
3069 min
= double_int_to_tree (expr_type
, dmin
);
3071 else if (code
== BIT_XOR_EXPR
)
3073 double_int result_zero_bits
, result_one_bits
;
3074 result_zero_bits
= (must_be_nonzero0
& must_be_nonzero1
)
3075 | ~(may_be_nonzero0
| may_be_nonzero1
);
3076 result_one_bits
= must_be_nonzero0
.and_not (may_be_nonzero1
)
3077 | must_be_nonzero1
.and_not (may_be_nonzero0
);
3078 max
= double_int_to_tree (expr_type
, ~result_zero_bits
);
3079 min
= double_int_to_tree (expr_type
, result_one_bits
);
3080 /* If the range has all positive or all negative values the
3081 result is better than VARYING. */
3082 if (tree_int_cst_sgn (min
) < 0
3083 || tree_int_cst_sgn (max
) >= 0)
3086 max
= min
= NULL_TREE
;
3092 /* If either MIN or MAX overflowed, then set the resulting range to
3093 VARYING. But we do accept an overflow infinity
3095 if (min
== NULL_TREE
3096 || !is_gimple_min_invariant (min
)
3097 || (TREE_OVERFLOW (min
) && !is_overflow_infinity (min
))
3099 || !is_gimple_min_invariant (max
)
3100 || (TREE_OVERFLOW (max
) && !is_overflow_infinity (max
)))
3102 set_value_range_to_varying (vr
);
3108 2) [-INF, +-INF(OVF)]
3109 3) [+-INF(OVF), +INF]
3110 4) [+-INF(OVF), +-INF(OVF)]
3111 We learn nothing when we have INF and INF(OVF) on both sides.
3112 Note that we do accept [-INF, -INF] and [+INF, +INF] without
3114 if ((vrp_val_is_min (min
) || is_overflow_infinity (min
))
3115 && (vrp_val_is_max (max
) || is_overflow_infinity (max
)))
3117 set_value_range_to_varying (vr
);
3121 cmp
= compare_values (min
, max
);
3122 if (cmp
== -2 || cmp
== 1)
3124 /* If the new range has its limits swapped around (MIN > MAX),
3125 then the operation caused one of them to wrap around, mark
3126 the new range VARYING. */
3127 set_value_range_to_varying (vr
);
3130 set_value_range (vr
, type
, min
, max
, NULL
);
3133 /* Extract range information from a binary expression OP0 CODE OP1 based on
3134 the ranges of each of its operands with resulting type EXPR_TYPE.
3135 The resulting range is stored in *VR. */
3138 extract_range_from_binary_expr (value_range_t
*vr
,
3139 enum tree_code code
,
3140 tree expr_type
, tree op0
, tree op1
)
3142 value_range_t vr0
= VR_INITIALIZER
;
3143 value_range_t vr1
= VR_INITIALIZER
;
3145 /* Get value ranges for each operand. For constant operands, create
3146 a new value range with the operand to simplify processing. */
3147 if (TREE_CODE (op0
) == SSA_NAME
)
3148 vr0
= *(get_value_range (op0
));
3149 else if (is_gimple_min_invariant (op0
))
3150 set_value_range_to_value (&vr0
, op0
, NULL
);
3152 set_value_range_to_varying (&vr0
);
3154 if (TREE_CODE (op1
) == SSA_NAME
)
3155 vr1
= *(get_value_range (op1
));
3156 else if (is_gimple_min_invariant (op1
))
3157 set_value_range_to_value (&vr1
, op1
, NULL
);
3159 set_value_range_to_varying (&vr1
);
3161 extract_range_from_binary_expr_1 (vr
, code
, expr_type
, &vr0
, &vr1
);
3164 /* Extract range information from a unary operation CODE based on
3165 the range of its operand *VR0 with type OP0_TYPE with resulting type TYPE.
3166 The The resulting range is stored in *VR. */
3169 extract_range_from_unary_expr_1 (value_range_t
*vr
,
3170 enum tree_code code
, tree type
,
3171 value_range_t
*vr0_
, tree op0_type
)
3173 value_range_t vr0
= *vr0_
, vrtem0
= VR_INITIALIZER
, vrtem1
= VR_INITIALIZER
;
3175 /* VRP only operates on integral and pointer types. */
3176 if (!(INTEGRAL_TYPE_P (op0_type
)
3177 || POINTER_TYPE_P (op0_type
))
3178 || !(INTEGRAL_TYPE_P (type
)
3179 || POINTER_TYPE_P (type
)))
3181 set_value_range_to_varying (vr
);
3185 /* If VR0 is UNDEFINED, so is the result. */
3186 if (vr0
.type
== VR_UNDEFINED
)
3188 set_value_range_to_undefined (vr
);
3192 /* Handle operations that we express in terms of others. */
3193 if (code
== PAREN_EXPR
)
3195 /* PAREN_EXPR is a simple copy. */
3196 copy_value_range (vr
, &vr0
);
3199 else if (code
== NEGATE_EXPR
)
3201 /* -X is simply 0 - X, so re-use existing code that also handles
3202 anti-ranges fine. */
3203 value_range_t zero
= VR_INITIALIZER
;
3204 set_value_range_to_value (&zero
, build_int_cst (type
, 0), NULL
);
3205 extract_range_from_binary_expr_1 (vr
, MINUS_EXPR
, type
, &zero
, &vr0
);
3208 else if (code
== BIT_NOT_EXPR
)
3210 /* ~X is simply -1 - X, so re-use existing code that also handles
3211 anti-ranges fine. */
3212 value_range_t minusone
= VR_INITIALIZER
;
3213 set_value_range_to_value (&minusone
, build_int_cst (type
, -1), NULL
);
3214 extract_range_from_binary_expr_1 (vr
, MINUS_EXPR
,
3215 type
, &minusone
, &vr0
);
3219 /* Now canonicalize anti-ranges to ranges when they are not symbolic
3220 and express op ~[] as (op []') U (op []''). */
3221 if (vr0
.type
== VR_ANTI_RANGE
3222 && ranges_from_anti_range (&vr0
, &vrtem0
, &vrtem1
))
3224 extract_range_from_unary_expr_1 (vr
, code
, type
, &vrtem0
, op0_type
);
3225 if (vrtem1
.type
!= VR_UNDEFINED
)
3227 value_range_t vrres
= VR_INITIALIZER
;
3228 extract_range_from_unary_expr_1 (&vrres
, code
, type
,
3230 vrp_meet (vr
, &vrres
);
3235 if (CONVERT_EXPR_CODE_P (code
))
3237 tree inner_type
= op0_type
;
3238 tree outer_type
= type
;
3240 /* If the expression evaluates to a pointer, we are only interested in
3241 determining if it evaluates to NULL [0, 0] or non-NULL (~[0, 0]). */
3242 if (POINTER_TYPE_P (type
))
3244 if (range_is_nonnull (&vr0
))
3245 set_value_range_to_nonnull (vr
, type
);
3246 else if (range_is_null (&vr0
))
3247 set_value_range_to_null (vr
, type
);
3249 set_value_range_to_varying (vr
);
3253 /* If VR0 is varying and we increase the type precision, assume
3254 a full range for the following transformation. */
3255 if (vr0
.type
== VR_VARYING
3256 && INTEGRAL_TYPE_P (inner_type
)
3257 && TYPE_PRECISION (inner_type
) < TYPE_PRECISION (outer_type
))
3259 vr0
.type
= VR_RANGE
;
3260 vr0
.min
= TYPE_MIN_VALUE (inner_type
);
3261 vr0
.max
= TYPE_MAX_VALUE (inner_type
);
3264 /* If VR0 is a constant range or anti-range and the conversion is
3265 not truncating we can convert the min and max values and
3266 canonicalize the resulting range. Otherwise we can do the
3267 conversion if the size of the range is less than what the
3268 precision of the target type can represent and the range is
3269 not an anti-range. */
3270 if ((vr0
.type
== VR_RANGE
3271 || vr0
.type
== VR_ANTI_RANGE
)
3272 && TREE_CODE (vr0
.min
) == INTEGER_CST
3273 && TREE_CODE (vr0
.max
) == INTEGER_CST
3274 && (!is_overflow_infinity (vr0
.min
)
3275 || (vr0
.type
== VR_RANGE
3276 && TYPE_PRECISION (outer_type
) > TYPE_PRECISION (inner_type
)
3277 && needs_overflow_infinity (outer_type
)
3278 && supports_overflow_infinity (outer_type
)))
3279 && (!is_overflow_infinity (vr0
.max
)
3280 || (vr0
.type
== VR_RANGE
3281 && TYPE_PRECISION (outer_type
) > TYPE_PRECISION (inner_type
)
3282 && needs_overflow_infinity (outer_type
)
3283 && supports_overflow_infinity (outer_type
)))
3284 && (TYPE_PRECISION (outer_type
) >= TYPE_PRECISION (inner_type
)
3285 || (vr0
.type
== VR_RANGE
3286 && integer_zerop (int_const_binop (RSHIFT_EXPR
,
3287 int_const_binop (MINUS_EXPR
, vr0
.max
, vr0
.min
),
3288 size_int (TYPE_PRECISION (outer_type
)))))))
3290 tree new_min
, new_max
;
3291 if (is_overflow_infinity (vr0
.min
))
3292 new_min
= negative_overflow_infinity (outer_type
);
3294 new_min
= force_fit_type_double (outer_type
,
3295 tree_to_double_int (vr0
.min
),
3297 if (is_overflow_infinity (vr0
.max
))
3298 new_max
= positive_overflow_infinity (outer_type
);
3300 new_max
= force_fit_type_double (outer_type
,
3301 tree_to_double_int (vr0
.max
),
3303 set_and_canonicalize_value_range (vr
, vr0
.type
,
3304 new_min
, new_max
, NULL
);
3308 set_value_range_to_varying (vr
);
3311 else if (code
== ABS_EXPR
)
3316 /* Pass through vr0 in the easy cases. */
3317 if (TYPE_UNSIGNED (type
)
3318 || value_range_nonnegative_p (&vr0
))
3320 copy_value_range (vr
, &vr0
);
3324 /* For the remaining varying or symbolic ranges we can't do anything
3326 if (vr0
.type
== VR_VARYING
3327 || symbolic_range_p (&vr0
))
3329 set_value_range_to_varying (vr
);
3333 /* -TYPE_MIN_VALUE = TYPE_MIN_VALUE with flag_wrapv so we can't get a
3335 if (!TYPE_OVERFLOW_UNDEFINED (type
)
3336 && ((vr0
.type
== VR_RANGE
3337 && vrp_val_is_min (vr0
.min
))
3338 || (vr0
.type
== VR_ANTI_RANGE
3339 && !vrp_val_is_min (vr0
.min
))))
3341 set_value_range_to_varying (vr
);
3345 /* ABS_EXPR may flip the range around, if the original range
3346 included negative values. */
3347 if (is_overflow_infinity (vr0
.min
))
3348 min
= positive_overflow_infinity (type
);
3349 else if (!vrp_val_is_min (vr0
.min
))
3350 min
= fold_unary_to_constant (code
, type
, vr0
.min
);
3351 else if (!needs_overflow_infinity (type
))
3352 min
= TYPE_MAX_VALUE (type
);
3353 else if (supports_overflow_infinity (type
))
3354 min
= positive_overflow_infinity (type
);
3357 set_value_range_to_varying (vr
);
3361 if (is_overflow_infinity (vr0
.max
))
3362 max
= positive_overflow_infinity (type
);
3363 else if (!vrp_val_is_min (vr0
.max
))
3364 max
= fold_unary_to_constant (code
, type
, vr0
.max
);
3365 else if (!needs_overflow_infinity (type
))
3366 max
= TYPE_MAX_VALUE (type
);
3367 else if (supports_overflow_infinity (type
)
3368 /* We shouldn't generate [+INF, +INF] as set_value_range
3369 doesn't like this and ICEs. */
3370 && !is_positive_overflow_infinity (min
))
3371 max
= positive_overflow_infinity (type
);
3374 set_value_range_to_varying (vr
);
3378 cmp
= compare_values (min
, max
);
3380 /* If a VR_ANTI_RANGEs contains zero, then we have
3381 ~[-INF, min(MIN, MAX)]. */
3382 if (vr0
.type
== VR_ANTI_RANGE
)
3384 if (range_includes_zero_p (vr0
.min
, vr0
.max
) == 1)
3386 /* Take the lower of the two values. */
3390 /* Create ~[-INF, min (abs(MIN), abs(MAX))]
3391 or ~[-INF + 1, min (abs(MIN), abs(MAX))] when
3392 flag_wrapv is set and the original anti-range doesn't include
3393 TYPE_MIN_VALUE, remember -TYPE_MIN_VALUE = TYPE_MIN_VALUE. */
3394 if (TYPE_OVERFLOW_WRAPS (type
))
3396 tree type_min_value
= TYPE_MIN_VALUE (type
);
3398 min
= (vr0
.min
!= type_min_value
3399 ? int_const_binop (PLUS_EXPR
, type_min_value
,
3405 if (overflow_infinity_range_p (&vr0
))
3406 min
= negative_overflow_infinity (type
);
3408 min
= TYPE_MIN_VALUE (type
);
3413 /* All else has failed, so create the range [0, INF], even for
3414 flag_wrapv since TYPE_MIN_VALUE is in the original
3416 vr0
.type
= VR_RANGE
;
3417 min
= build_int_cst (type
, 0);
3418 if (needs_overflow_infinity (type
))
3420 if (supports_overflow_infinity (type
))
3421 max
= positive_overflow_infinity (type
);
3424 set_value_range_to_varying (vr
);
3429 max
= TYPE_MAX_VALUE (type
);
3433 /* If the range contains zero then we know that the minimum value in the
3434 range will be zero. */
3435 else if (range_includes_zero_p (vr0
.min
, vr0
.max
) == 1)
3439 min
= build_int_cst (type
, 0);
3443 /* If the range was reversed, swap MIN and MAX. */
3452 cmp
= compare_values (min
, max
);
3453 if (cmp
== -2 || cmp
== 1)
3455 /* If the new range has its limits swapped around (MIN > MAX),
3456 then the operation caused one of them to wrap around, mark
3457 the new range VARYING. */
3458 set_value_range_to_varying (vr
);
3461 set_value_range (vr
, vr0
.type
, min
, max
, NULL
);
3465 /* For unhandled operations fall back to varying. */
3466 set_value_range_to_varying (vr
);
3471 /* Extract range information from a unary expression CODE OP0 based on
3472 the range of its operand with resulting type TYPE.
3473 The resulting range is stored in *VR. */
3476 extract_range_from_unary_expr (value_range_t
*vr
, enum tree_code code
,
3477 tree type
, tree op0
)
3479 value_range_t vr0
= VR_INITIALIZER
;
3481 /* Get value ranges for the operand. For constant operands, create
3482 a new value range with the operand to simplify processing. */
3483 if (TREE_CODE (op0
) == SSA_NAME
)
3484 vr0
= *(get_value_range (op0
));
3485 else if (is_gimple_min_invariant (op0
))
3486 set_value_range_to_value (&vr0
, op0
, NULL
);
3488 set_value_range_to_varying (&vr0
);
3490 extract_range_from_unary_expr_1 (vr
, code
, type
, &vr0
, TREE_TYPE (op0
));
3494 /* Extract range information from a conditional expression STMT based on
3495 the ranges of each of its operands and the expression code. */
3498 extract_range_from_cond_expr (value_range_t
*vr
, gimple stmt
)
3501 value_range_t vr0
= VR_INITIALIZER
;
3502 value_range_t vr1
= VR_INITIALIZER
;
3504 /* Get value ranges for each operand. For constant operands, create
3505 a new value range with the operand to simplify processing. */
3506 op0
= gimple_assign_rhs2 (stmt
);
3507 if (TREE_CODE (op0
) == SSA_NAME
)
3508 vr0
= *(get_value_range (op0
));
3509 else if (is_gimple_min_invariant (op0
))
3510 set_value_range_to_value (&vr0
, op0
, NULL
);
3512 set_value_range_to_varying (&vr0
);
3514 op1
= gimple_assign_rhs3 (stmt
);
3515 if (TREE_CODE (op1
) == SSA_NAME
)
3516 vr1
= *(get_value_range (op1
));
3517 else if (is_gimple_min_invariant (op1
))
3518 set_value_range_to_value (&vr1
, op1
, NULL
);
3520 set_value_range_to_varying (&vr1
);
3522 /* The resulting value range is the union of the operand ranges */
3523 copy_value_range (vr
, &vr0
);
3524 vrp_meet (vr
, &vr1
);
3528 /* Extract range information from a comparison expression EXPR based
3529 on the range of its operand and the expression code. */
3532 extract_range_from_comparison (value_range_t
*vr
, enum tree_code code
,
3533 tree type
, tree op0
, tree op1
)
3538 val
= vrp_evaluate_conditional_warnv_with_ops (code
, op0
, op1
, false, &sop
,
3541 /* A disadvantage of using a special infinity as an overflow
3542 representation is that we lose the ability to record overflow
3543 when we don't have an infinity. So we have to ignore a result
3544 which relies on overflow. */
3546 if (val
&& !is_overflow_infinity (val
) && !sop
)
3548 /* Since this expression was found on the RHS of an assignment,
3549 its type may be different from _Bool. Convert VAL to EXPR's
3551 val
= fold_convert (type
, val
);
3552 if (is_gimple_min_invariant (val
))
3553 set_value_range_to_value (vr
, val
, vr
->equiv
);
3555 set_value_range (vr
, VR_RANGE
, val
, val
, vr
->equiv
);
3558 /* The result of a comparison is always true or false. */
3559 set_value_range_to_truthvalue (vr
, type
);
3562 /* Try to derive a nonnegative or nonzero range out of STMT relying
3563 primarily on generic routines in fold in conjunction with range data.
3564 Store the result in *VR */
3567 extract_range_basic (value_range_t
*vr
, gimple stmt
)
3570 tree type
= gimple_expr_type (stmt
);
3572 if (gimple_call_builtin_p (stmt
, BUILT_IN_NORMAL
))
3574 tree fndecl
= gimple_call_fndecl (stmt
), arg
;
3575 int mini
, maxi
, zerov
= 0, prec
;
3577 switch (DECL_FUNCTION_CODE (fndecl
))
3579 case BUILT_IN_CONSTANT_P
:
3580 /* If the call is __builtin_constant_p and the argument is a
3581 function parameter resolve it to false. This avoids bogus
3582 array bound warnings.
3583 ??? We could do this as early as inlining is finished. */
3584 arg
= gimple_call_arg (stmt
, 0);
3585 if (TREE_CODE (arg
) == SSA_NAME
3586 && SSA_NAME_IS_DEFAULT_DEF (arg
)
3587 && TREE_CODE (SSA_NAME_VAR (arg
)) == PARM_DECL
)
3589 set_value_range_to_null (vr
, type
);
3593 /* Both __builtin_ffs* and __builtin_popcount return
3595 CASE_INT_FN (BUILT_IN_FFS
):
3596 CASE_INT_FN (BUILT_IN_POPCOUNT
):
3597 arg
= gimple_call_arg (stmt
, 0);
3598 prec
= TYPE_PRECISION (TREE_TYPE (arg
));
3601 if (TREE_CODE (arg
) == SSA_NAME
)
3603 value_range_t
*vr0
= get_value_range (arg
);
3604 /* If arg is non-zero, then ffs or popcount
3606 if (((vr0
->type
== VR_RANGE
3607 && integer_nonzerop (vr0
->min
))
3608 || (vr0
->type
== VR_ANTI_RANGE
3609 && integer_zerop (vr0
->min
)))
3610 && !is_overflow_infinity (vr0
->min
))
3612 /* If some high bits are known to be zero,
3613 we can decrease the maximum. */
3614 if (vr0
->type
== VR_RANGE
3615 && TREE_CODE (vr0
->max
) == INTEGER_CST
3616 && !is_overflow_infinity (vr0
->max
))
3617 maxi
= tree_floor_log2 (vr0
->max
) + 1;
3620 /* __builtin_parity* returns [0, 1]. */
3621 CASE_INT_FN (BUILT_IN_PARITY
):
3625 /* __builtin_c[lt]z* return [0, prec-1], except for
3626 when the argument is 0, but that is undefined behavior.
3627 On many targets where the CLZ RTL or optab value is defined
3628 for 0 the value is prec, so include that in the range
3630 CASE_INT_FN (BUILT_IN_CLZ
):
3631 arg
= gimple_call_arg (stmt
, 0);
3632 prec
= TYPE_PRECISION (TREE_TYPE (arg
));
3635 if (optab_handler (clz_optab
, TYPE_MODE (TREE_TYPE (arg
)))
3637 && CLZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (TREE_TYPE (arg
)),
3639 /* Handle only the single common value. */
3641 /* Magic value to give up, unless vr0 proves
3644 if (TREE_CODE (arg
) == SSA_NAME
)
3646 value_range_t
*vr0
= get_value_range (arg
);
3647 /* From clz of VR_RANGE minimum we can compute
3649 if (vr0
->type
== VR_RANGE
3650 && TREE_CODE (vr0
->min
) == INTEGER_CST
3651 && !is_overflow_infinity (vr0
->min
))
3653 maxi
= prec
- 1 - tree_floor_log2 (vr0
->min
);
3657 else if (vr0
->type
== VR_ANTI_RANGE
3658 && integer_zerop (vr0
->min
)
3659 && !is_overflow_infinity (vr0
->min
))
3666 /* From clz of VR_RANGE maximum we can compute
3668 if (vr0
->type
== VR_RANGE
3669 && TREE_CODE (vr0
->max
) == INTEGER_CST
3670 && !is_overflow_infinity (vr0
->max
))
3672 mini
= prec
- 1 - tree_floor_log2 (vr0
->max
);
3680 /* __builtin_ctz* return [0, prec-1], except for
3681 when the argument is 0, but that is undefined behavior.
3682 If there is a ctz optab for this mode and
3683 CTZ_DEFINED_VALUE_AT_ZERO, include that in the range,
3684 otherwise just assume 0 won't be seen. */
3685 CASE_INT_FN (BUILT_IN_CTZ
):
3686 arg
= gimple_call_arg (stmt
, 0);
3687 prec
= TYPE_PRECISION (TREE_TYPE (arg
));
3690 if (optab_handler (ctz_optab
, TYPE_MODE (TREE_TYPE (arg
)))
3692 && CTZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (TREE_TYPE (arg
)),
3695 /* Handle only the two common values. */
3698 else if (zerov
== prec
)
3701 /* Magic value to give up, unless vr0 proves
3705 if (TREE_CODE (arg
) == SSA_NAME
)
3707 value_range_t
*vr0
= get_value_range (arg
);
3708 /* If arg is non-zero, then use [0, prec - 1]. */
3709 if (((vr0
->type
== VR_RANGE
3710 && integer_nonzerop (vr0
->min
))
3711 || (vr0
->type
== VR_ANTI_RANGE
3712 && integer_zerop (vr0
->min
)))
3713 && !is_overflow_infinity (vr0
->min
))
3718 /* If some high bits are known to be zero,
3719 we can decrease the result maximum. */
3720 if (vr0
->type
== VR_RANGE
3721 && TREE_CODE (vr0
->max
) == INTEGER_CST
3722 && !is_overflow_infinity (vr0
->max
))
3724 maxi
= tree_floor_log2 (vr0
->max
);
3725 /* For vr0 [0, 0] give up. */
3733 /* __builtin_clrsb* returns [0, prec-1]. */
3734 CASE_INT_FN (BUILT_IN_CLRSB
):
3735 arg
= gimple_call_arg (stmt
, 0);
3736 prec
= TYPE_PRECISION (TREE_TYPE (arg
));
3741 set_value_range (vr
, VR_RANGE
, build_int_cst (type
, mini
),
3742 build_int_cst (type
, maxi
), NULL
);
3748 if (INTEGRAL_TYPE_P (type
)
3749 && gimple_stmt_nonnegative_warnv_p (stmt
, &sop
))
3750 set_value_range_to_nonnegative (vr
, type
,
3751 sop
|| stmt_overflow_infinity (stmt
));
3752 else if (vrp_stmt_computes_nonzero (stmt
, &sop
)
3754 set_value_range_to_nonnull (vr
, type
);
3756 set_value_range_to_varying (vr
);
3760 /* Try to compute a useful range out of assignment STMT and store it
3764 extract_range_from_assignment (value_range_t
*vr
, gimple stmt
)
3766 enum tree_code code
= gimple_assign_rhs_code (stmt
);
3768 if (code
== ASSERT_EXPR
)
3769 extract_range_from_assert (vr
, gimple_assign_rhs1 (stmt
));
3770 else if (code
== SSA_NAME
)
3771 extract_range_from_ssa_name (vr
, gimple_assign_rhs1 (stmt
));
3772 else if (TREE_CODE_CLASS (code
) == tcc_binary
)
3773 extract_range_from_binary_expr (vr
, gimple_assign_rhs_code (stmt
),
3774 gimple_expr_type (stmt
),
3775 gimple_assign_rhs1 (stmt
),
3776 gimple_assign_rhs2 (stmt
));
3777 else if (TREE_CODE_CLASS (code
) == tcc_unary
)
3778 extract_range_from_unary_expr (vr
, gimple_assign_rhs_code (stmt
),
3779 gimple_expr_type (stmt
),
3780 gimple_assign_rhs1 (stmt
));
3781 else if (code
== COND_EXPR
)
3782 extract_range_from_cond_expr (vr
, stmt
);
3783 else if (TREE_CODE_CLASS (code
) == tcc_comparison
)
3784 extract_range_from_comparison (vr
, gimple_assign_rhs_code (stmt
),
3785 gimple_expr_type (stmt
),
3786 gimple_assign_rhs1 (stmt
),
3787 gimple_assign_rhs2 (stmt
));
3788 else if (get_gimple_rhs_class (code
) == GIMPLE_SINGLE_RHS
3789 && is_gimple_min_invariant (gimple_assign_rhs1 (stmt
)))
3790 set_value_range_to_value (vr
, gimple_assign_rhs1 (stmt
), NULL
);
3792 set_value_range_to_varying (vr
);
3794 if (vr
->type
== VR_VARYING
)
3795 extract_range_basic (vr
, stmt
);
3798 /* Given a range VR, a LOOP and a variable VAR, determine whether it
3799 would be profitable to adjust VR using scalar evolution information
3800 for VAR. If so, update VR with the new limits. */
3803 adjust_range_with_scev (value_range_t
*vr
, struct loop
*loop
,
3804 gimple stmt
, tree var
)
3806 tree init
, step
, chrec
, tmin
, tmax
, min
, max
, type
, tem
;
3807 enum ev_direction dir
;
3809 /* TODO. Don't adjust anti-ranges. An anti-range may provide
3810 better opportunities than a regular range, but I'm not sure. */
3811 if (vr
->type
== VR_ANTI_RANGE
)
3814 chrec
= instantiate_parameters (loop
, analyze_scalar_evolution (loop
, var
));
3816 /* Like in PR19590, scev can return a constant function. */
3817 if (is_gimple_min_invariant (chrec
))
3819 set_value_range_to_value (vr
, chrec
, vr
->equiv
);
3823 if (TREE_CODE (chrec
) != POLYNOMIAL_CHREC
)
3826 init
= initial_condition_in_loop_num (chrec
, loop
->num
);
3827 tem
= op_with_constant_singleton_value_range (init
);
3830 step
= evolution_part_in_loop_num (chrec
, loop
->num
);
3831 tem
= op_with_constant_singleton_value_range (step
);
3835 /* If STEP is symbolic, we can't know whether INIT will be the
3836 minimum or maximum value in the range. Also, unless INIT is
3837 a simple expression, compare_values and possibly other functions
3838 in tree-vrp won't be able to handle it. */
3839 if (step
== NULL_TREE
3840 || !is_gimple_min_invariant (step
)
3841 || !valid_value_p (init
))
3844 dir
= scev_direction (chrec
);
3845 if (/* Do not adjust ranges if we do not know whether the iv increases
3846 or decreases, ... */
3847 dir
== EV_DIR_UNKNOWN
3848 /* ... or if it may wrap. */
3849 || scev_probably_wraps_p (init
, step
, stmt
, get_chrec_loop (chrec
),
3853 /* We use TYPE_MIN_VALUE and TYPE_MAX_VALUE here instead of
3854 negative_overflow_infinity and positive_overflow_infinity,
3855 because we have concluded that the loop probably does not
3858 type
= TREE_TYPE (var
);
3859 if (POINTER_TYPE_P (type
) || !TYPE_MIN_VALUE (type
))
3860 tmin
= lower_bound_in_type (type
, type
);
3862 tmin
= TYPE_MIN_VALUE (type
);
3863 if (POINTER_TYPE_P (type
) || !TYPE_MAX_VALUE (type
))
3864 tmax
= upper_bound_in_type (type
, type
);
3866 tmax
= TYPE_MAX_VALUE (type
);
3868 /* Try to use estimated number of iterations for the loop to constrain the
3869 final value in the evolution. */
3870 if (TREE_CODE (step
) == INTEGER_CST
3871 && is_gimple_val (init
)
3872 && (TREE_CODE (init
) != SSA_NAME
3873 || get_value_range (init
)->type
== VR_RANGE
))
3877 /* We are only entering here for loop header PHI nodes, so using
3878 the number of latch executions is the correct thing to use. */
3879 if (max_loop_iterations (loop
, &nit
))
3881 value_range_t maxvr
= VR_INITIALIZER
;
3883 bool unsigned_p
= TYPE_UNSIGNED (TREE_TYPE (step
));
3884 bool overflow
= false;
3886 dtmp
= tree_to_double_int (step
)
3887 .mul_with_sign (nit
, unsigned_p
, &overflow
);
3888 /* If the multiplication overflowed we can't do a meaningful
3889 adjustment. Likewise if the result doesn't fit in the type
3890 of the induction variable. For a signed type we have to
3891 check whether the result has the expected signedness which
3892 is that of the step as number of iterations is unsigned. */
3894 && double_int_fits_to_tree_p (TREE_TYPE (init
), dtmp
)
3896 || ((dtmp
.high
^ TREE_INT_CST_HIGH (step
)) >= 0)))
3898 tem
= double_int_to_tree (TREE_TYPE (init
), dtmp
);
3899 extract_range_from_binary_expr (&maxvr
, PLUS_EXPR
,
3900 TREE_TYPE (init
), init
, tem
);
3901 /* Likewise if the addition did. */
3902 if (maxvr
.type
== VR_RANGE
)
3911 if (vr
->type
== VR_VARYING
|| vr
->type
== VR_UNDEFINED
)
3916 /* For VARYING or UNDEFINED ranges, just about anything we get
3917 from scalar evolutions should be better. */
3919 if (dir
== EV_DIR_DECREASES
)
3924 /* If we would create an invalid range, then just assume we
3925 know absolutely nothing. This may be over-conservative,
3926 but it's clearly safe, and should happen only in unreachable
3927 parts of code, or for invalid programs. */
3928 if (compare_values (min
, max
) == 1)
3931 set_value_range (vr
, VR_RANGE
, min
, max
, vr
->equiv
);
3933 else if (vr
->type
== VR_RANGE
)
3938 if (dir
== EV_DIR_DECREASES
)
3940 /* INIT is the maximum value. If INIT is lower than VR->MAX
3941 but no smaller than VR->MIN, set VR->MAX to INIT. */
3942 if (compare_values (init
, max
) == -1)
3945 /* According to the loop information, the variable does not
3946 overflow. If we think it does, probably because of an
3947 overflow due to arithmetic on a different INF value,
3949 if (is_negative_overflow_infinity (min
)
3950 || compare_values (min
, tmin
) == -1)
3956 /* If INIT is bigger than VR->MIN, set VR->MIN to INIT. */
3957 if (compare_values (init
, min
) == 1)
3960 if (is_positive_overflow_infinity (max
)
3961 || compare_values (tmax
, max
) == -1)
3965 /* If we just created an invalid range with the minimum
3966 greater than the maximum, we fail conservatively.
3967 This should happen only in unreachable
3968 parts of code, or for invalid programs. */
3969 if (compare_values (min
, max
) == 1)
3972 set_value_range (vr
, VR_RANGE
, min
, max
, vr
->equiv
);
3976 /* Return true if VAR may overflow at STMT. This checks any available
3977 loop information to see if we can determine that VAR does not
3981 vrp_var_may_overflow (tree var
, gimple stmt
)
3984 tree chrec
, init
, step
;
3986 if (current_loops
== NULL
)
3989 l
= loop_containing_stmt (stmt
);
3994 chrec
= instantiate_parameters (l
, analyze_scalar_evolution (l
, var
));
3995 if (TREE_CODE (chrec
) != POLYNOMIAL_CHREC
)
3998 init
= initial_condition_in_loop_num (chrec
, l
->num
);
3999 step
= evolution_part_in_loop_num (chrec
, l
->num
);
4001 if (step
== NULL_TREE
4002 || !is_gimple_min_invariant (step
)
4003 || !valid_value_p (init
))
4006 /* If we get here, we know something useful about VAR based on the
4007 loop information. If it wraps, it may overflow. */
4009 if (scev_probably_wraps_p (init
, step
, stmt
, get_chrec_loop (chrec
),
4013 if (dump_file
&& (dump_flags
& TDF_DETAILS
) != 0)
4015 print_generic_expr (dump_file
, var
, 0);
4016 fprintf (dump_file
, ": loop information indicates does not overflow\n");
4023 /* Given two numeric value ranges VR0, VR1 and a comparison code COMP:
4025 - Return BOOLEAN_TRUE_NODE if VR0 COMP VR1 always returns true for
4026 all the values in the ranges.
4028 - Return BOOLEAN_FALSE_NODE if the comparison always returns false.
4030 - Return NULL_TREE if it is not always possible to determine the
4031 value of the comparison.
4033 Also set *STRICT_OVERFLOW_P to indicate whether a range with an
4034 overflow infinity was used in the test. */
4038 compare_ranges (enum tree_code comp
, value_range_t
*vr0
, value_range_t
*vr1
,
4039 bool *strict_overflow_p
)
4041 /* VARYING or UNDEFINED ranges cannot be compared. */
4042 if (vr0
->type
== VR_VARYING
4043 || vr0
->type
== VR_UNDEFINED
4044 || vr1
->type
== VR_VARYING
4045 || vr1
->type
== VR_UNDEFINED
)
4048 /* Anti-ranges need to be handled separately. */
4049 if (vr0
->type
== VR_ANTI_RANGE
|| vr1
->type
== VR_ANTI_RANGE
)
4051 /* If both are anti-ranges, then we cannot compute any
4053 if (vr0
->type
== VR_ANTI_RANGE
&& vr1
->type
== VR_ANTI_RANGE
)
4056 /* These comparisons are never statically computable. */
4063 /* Equality can be computed only between a range and an
4064 anti-range. ~[VAL1, VAL2] == [VAL1, VAL2] is always false. */
4065 if (vr0
->type
== VR_RANGE
)
4067 /* To simplify processing, make VR0 the anti-range. */
4068 value_range_t
*tmp
= vr0
;
4073 gcc_assert (comp
== NE_EXPR
|| comp
== EQ_EXPR
);
4075 if (compare_values_warnv (vr0
->min
, vr1
->min
, strict_overflow_p
) == 0
4076 && compare_values_warnv (vr0
->max
, vr1
->max
, strict_overflow_p
) == 0)
4077 return (comp
== NE_EXPR
) ? boolean_true_node
: boolean_false_node
;
4082 if (!usable_range_p (vr0
, strict_overflow_p
)
4083 || !usable_range_p (vr1
, strict_overflow_p
))
4086 /* Simplify processing. If COMP is GT_EXPR or GE_EXPR, switch the
4087 operands around and change the comparison code. */
4088 if (comp
== GT_EXPR
|| comp
== GE_EXPR
)
4091 comp
= (comp
== GT_EXPR
) ? LT_EXPR
: LE_EXPR
;
4097 if (comp
== EQ_EXPR
)
4099 /* Equality may only be computed if both ranges represent
4100 exactly one value. */
4101 if (compare_values_warnv (vr0
->min
, vr0
->max
, strict_overflow_p
) == 0
4102 && compare_values_warnv (vr1
->min
, vr1
->max
, strict_overflow_p
) == 0)
4104 int cmp_min
= compare_values_warnv (vr0
->min
, vr1
->min
,
4106 int cmp_max
= compare_values_warnv (vr0
->max
, vr1
->max
,
4108 if (cmp_min
== 0 && cmp_max
== 0)
4109 return boolean_true_node
;
4110 else if (cmp_min
!= -2 && cmp_max
!= -2)
4111 return boolean_false_node
;
4113 /* If [V0_MIN, V1_MAX] < [V1_MIN, V1_MAX] then V0 != V1. */
4114 else if (compare_values_warnv (vr0
->min
, vr1
->max
,
4115 strict_overflow_p
) == 1
4116 || compare_values_warnv (vr1
->min
, vr0
->max
,
4117 strict_overflow_p
) == 1)
4118 return boolean_false_node
;
4122 else if (comp
== NE_EXPR
)
4126 /* If VR0 is completely to the left or completely to the right
4127 of VR1, they are always different. Notice that we need to
4128 make sure that both comparisons yield similar results to
4129 avoid comparing values that cannot be compared at
4131 cmp1
= compare_values_warnv (vr0
->max
, vr1
->min
, strict_overflow_p
);
4132 cmp2
= compare_values_warnv (vr0
->min
, vr1
->max
, strict_overflow_p
);
4133 if ((cmp1
== -1 && cmp2
== -1) || (cmp1
== 1 && cmp2
== 1))
4134 return boolean_true_node
;
4136 /* If VR0 and VR1 represent a single value and are identical,
4138 else if (compare_values_warnv (vr0
->min
, vr0
->max
,
4139 strict_overflow_p
) == 0
4140 && compare_values_warnv (vr1
->min
, vr1
->max
,
4141 strict_overflow_p
) == 0
4142 && compare_values_warnv (vr0
->min
, vr1
->min
,
4143 strict_overflow_p
) == 0
4144 && compare_values_warnv (vr0
->max
, vr1
->max
,
4145 strict_overflow_p
) == 0)
4146 return boolean_false_node
;
4148 /* Otherwise, they may or may not be different. */
4152 else if (comp
== LT_EXPR
|| comp
== LE_EXPR
)
4156 /* If VR0 is to the left of VR1, return true. */
4157 tst
= compare_values_warnv (vr0
->max
, vr1
->min
, strict_overflow_p
);
4158 if ((comp
== LT_EXPR
&& tst
== -1)
4159 || (comp
== LE_EXPR
&& (tst
== -1 || tst
== 0)))
4161 if (overflow_infinity_range_p (vr0
)
4162 || overflow_infinity_range_p (vr1
))
4163 *strict_overflow_p
= true;
4164 return boolean_true_node
;
4167 /* If VR0 is to the right of VR1, return false. */
4168 tst
= compare_values_warnv (vr0
->min
, vr1
->max
, strict_overflow_p
);
4169 if ((comp
== LT_EXPR
&& (tst
== 0 || tst
== 1))
4170 || (comp
== LE_EXPR
&& tst
== 1))
4172 if (overflow_infinity_range_p (vr0
)
4173 || overflow_infinity_range_p (vr1
))
4174 *strict_overflow_p
= true;
4175 return boolean_false_node
;
4178 /* Otherwise, we don't know. */
4186 /* Given a value range VR, a value VAL and a comparison code COMP, return
4187 BOOLEAN_TRUE_NODE if VR COMP VAL always returns true for all the
4188 values in VR. Return BOOLEAN_FALSE_NODE if the comparison
4189 always returns false. Return NULL_TREE if it is not always
4190 possible to determine the value of the comparison. Also set
4191 *STRICT_OVERFLOW_P to indicate whether a range with an overflow
4192 infinity was used in the test. */
4195 compare_range_with_value (enum tree_code comp
, value_range_t
*vr
, tree val
,
4196 bool *strict_overflow_p
)
4198 if (vr
->type
== VR_VARYING
|| vr
->type
== VR_UNDEFINED
)
4201 /* Anti-ranges need to be handled separately. */
4202 if (vr
->type
== VR_ANTI_RANGE
)
4204 /* For anti-ranges, the only predicates that we can compute at
4205 compile time are equality and inequality. */
4212 /* ~[VAL_1, VAL_2] OP VAL is known if VAL_1 <= VAL <= VAL_2. */
4213 if (value_inside_range (val
, vr
->min
, vr
->max
) == 1)
4214 return (comp
== NE_EXPR
) ? boolean_true_node
: boolean_false_node
;
4219 if (!usable_range_p (vr
, strict_overflow_p
))
4222 if (comp
== EQ_EXPR
)
4224 /* EQ_EXPR may only be computed if VR represents exactly
4226 if (compare_values_warnv (vr
->min
, vr
->max
, strict_overflow_p
) == 0)
4228 int cmp
= compare_values_warnv (vr
->min
, val
, strict_overflow_p
);
4230 return boolean_true_node
;
4231 else if (cmp
== -1 || cmp
== 1 || cmp
== 2)
4232 return boolean_false_node
;
4234 else if (compare_values_warnv (val
, vr
->min
, strict_overflow_p
) == -1
4235 || compare_values_warnv (vr
->max
, val
, strict_overflow_p
) == -1)
4236 return boolean_false_node
;
4240 else if (comp
== NE_EXPR
)
4242 /* If VAL is not inside VR, then they are always different. */
4243 if (compare_values_warnv (vr
->max
, val
, strict_overflow_p
) == -1
4244 || compare_values_warnv (vr
->min
, val
, strict_overflow_p
) == 1)
4245 return boolean_true_node
;
4247 /* If VR represents exactly one value equal to VAL, then return
4249 if (compare_values_warnv (vr
->min
, vr
->max
, strict_overflow_p
) == 0
4250 && compare_values_warnv (vr
->min
, val
, strict_overflow_p
) == 0)
4251 return boolean_false_node
;
4253 /* Otherwise, they may or may not be different. */
4256 else if (comp
== LT_EXPR
|| comp
== LE_EXPR
)
4260 /* If VR is to the left of VAL, return true. */
4261 tst
= compare_values_warnv (vr
->max
, val
, strict_overflow_p
);
4262 if ((comp
== LT_EXPR
&& tst
== -1)
4263 || (comp
== LE_EXPR
&& (tst
== -1 || tst
== 0)))
4265 if (overflow_infinity_range_p (vr
))
4266 *strict_overflow_p
= true;
4267 return boolean_true_node
;
4270 /* If VR is to the right of VAL, return false. */
4271 tst
= compare_values_warnv (vr
->min
, val
, strict_overflow_p
);
4272 if ((comp
== LT_EXPR
&& (tst
== 0 || tst
== 1))
4273 || (comp
== LE_EXPR
&& tst
== 1))
4275 if (overflow_infinity_range_p (vr
))
4276 *strict_overflow_p
= true;
4277 return boolean_false_node
;
4280 /* Otherwise, we don't know. */
4283 else if (comp
== GT_EXPR
|| comp
== GE_EXPR
)
4287 /* If VR is to the right of VAL, return true. */
4288 tst
= compare_values_warnv (vr
->min
, val
, strict_overflow_p
);
4289 if ((comp
== GT_EXPR
&& tst
== 1)
4290 || (comp
== GE_EXPR
&& (tst
== 0 || tst
== 1)))
4292 if (overflow_infinity_range_p (vr
))
4293 *strict_overflow_p
= true;
4294 return boolean_true_node
;
4297 /* If VR is to the left of VAL, return false. */
4298 tst
= compare_values_warnv (vr
->max
, val
, strict_overflow_p
);
4299 if ((comp
== GT_EXPR
&& (tst
== -1 || tst
== 0))
4300 || (comp
== GE_EXPR
&& tst
== -1))
4302 if (overflow_infinity_range_p (vr
))
4303 *strict_overflow_p
= true;
4304 return boolean_false_node
;
4307 /* Otherwise, we don't know. */
4315 /* Debugging dumps. */
4317 void dump_value_range (FILE *, value_range_t
*);
4318 void debug_value_range (value_range_t
*);
4319 void dump_all_value_ranges (FILE *);
4320 void debug_all_value_ranges (void);
4321 void dump_vr_equiv (FILE *, bitmap
);
4322 void debug_vr_equiv (bitmap
);
4325 /* Dump value range VR to FILE. */
4328 dump_value_range (FILE *file
, value_range_t
*vr
)
4331 fprintf (file
, "[]");
4332 else if (vr
->type
== VR_UNDEFINED
)
4333 fprintf (file
, "UNDEFINED");
4334 else if (vr
->type
== VR_RANGE
|| vr
->type
== VR_ANTI_RANGE
)
4336 tree type
= TREE_TYPE (vr
->min
);
4338 fprintf (file
, "%s[", (vr
->type
== VR_ANTI_RANGE
) ? "~" : "");
4340 if (is_negative_overflow_infinity (vr
->min
))
4341 fprintf (file
, "-INF(OVF)");
4342 else if (INTEGRAL_TYPE_P (type
)
4343 && !TYPE_UNSIGNED (type
)
4344 && vrp_val_is_min (vr
->min
))
4345 fprintf (file
, "-INF");
4347 print_generic_expr (file
, vr
->min
, 0);
4349 fprintf (file
, ", ");
4351 if (is_positive_overflow_infinity (vr
->max
))
4352 fprintf (file
, "+INF(OVF)");
4353 else if (INTEGRAL_TYPE_P (type
)
4354 && vrp_val_is_max (vr
->max
))
4355 fprintf (file
, "+INF");
4357 print_generic_expr (file
, vr
->max
, 0);
4359 fprintf (file
, "]");
4366 fprintf (file
, " EQUIVALENCES: { ");
4368 EXECUTE_IF_SET_IN_BITMAP (vr
->equiv
, 0, i
, bi
)
4370 print_generic_expr (file
, ssa_name (i
), 0);
4371 fprintf (file
, " ");
4375 fprintf (file
, "} (%u elements)", c
);
4378 else if (vr
->type
== VR_VARYING
)
4379 fprintf (file
, "VARYING");
4381 fprintf (file
, "INVALID RANGE");
4385 /* Dump value range VR to stderr. */
4388 debug_value_range (value_range_t
*vr
)
4390 dump_value_range (stderr
, vr
);
4391 fprintf (stderr
, "\n");
4395 /* Dump value ranges of all SSA_NAMEs to FILE. */
4398 dump_all_value_ranges (FILE *file
)
4402 for (i
= 0; i
< num_vr_values
; i
++)
4406 print_generic_expr (file
, ssa_name (i
), 0);
4407 fprintf (file
, ": ");
4408 dump_value_range (file
, vr_value
[i
]);
4409 fprintf (file
, "\n");
4413 fprintf (file
, "\n");
4417 /* Dump all value ranges to stderr. */
4420 debug_all_value_ranges (void)
4422 dump_all_value_ranges (stderr
);
4426 /* Given a COND_EXPR COND of the form 'V OP W', and an SSA name V,
4427 create a new SSA name N and return the assertion assignment
4428 'V = ASSERT_EXPR <V, V OP W>'. */
4431 build_assert_expr_for (tree cond
, tree v
)
4436 gcc_assert (TREE_CODE (v
) == SSA_NAME
4437 && COMPARISON_CLASS_P (cond
));
4439 a
= build2 (ASSERT_EXPR
, TREE_TYPE (v
), v
, cond
);
4440 assertion
= gimple_build_assign (NULL_TREE
, a
);
4442 /* The new ASSERT_EXPR, creates a new SSA name that replaces the
4443 operand of the ASSERT_EXPR. Create it so the new name and the old one
4444 are registered in the replacement table so that we can fix the SSA web
4445 after adding all the ASSERT_EXPRs. */
4446 create_new_def_for (v
, assertion
, NULL
);
4452 /* Return false if EXPR is a predicate expression involving floating
4456 fp_predicate (gimple stmt
)
4458 GIMPLE_CHECK (stmt
, GIMPLE_COND
);
4460 return FLOAT_TYPE_P (TREE_TYPE (gimple_cond_lhs (stmt
)));
4463 /* If the range of values taken by OP can be inferred after STMT executes,
4464 return the comparison code (COMP_CODE_P) and value (VAL_P) that
4465 describes the inferred range. Return true if a range could be
4469 infer_value_range (gimple stmt
, tree op
, enum tree_code
*comp_code_p
, tree
*val_p
)
4472 *comp_code_p
= ERROR_MARK
;
4474 /* Do not attempt to infer anything in names that flow through
4476 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op
))
4479 /* Similarly, don't infer anything from statements that may throw
4480 exceptions. ??? Relax this requirement? */
4481 if (stmt_could_throw_p (stmt
))
4484 /* If STMT is the last statement of a basic block with no
4485 successors, there is no point inferring anything about any of its
4486 operands. We would not be able to find a proper insertion point
4487 for the assertion, anyway. */
4488 if (stmt_ends_bb_p (stmt
) && EDGE_COUNT (gimple_bb (stmt
)->succs
) == 0)
4491 if (infer_nonnull_range (stmt
, op
))
4493 *val_p
= build_int_cst (TREE_TYPE (op
), 0);
4494 *comp_code_p
= NE_EXPR
;
4502 void dump_asserts_for (FILE *, tree
);
4503 void debug_asserts_for (tree
);
4504 void dump_all_asserts (FILE *);
4505 void debug_all_asserts (void);
4507 /* Dump all the registered assertions for NAME to FILE. */
4510 dump_asserts_for (FILE *file
, tree name
)
4514 fprintf (file
, "Assertions to be inserted for ");
4515 print_generic_expr (file
, name
, 0);
4516 fprintf (file
, "\n");
4518 loc
= asserts_for
[SSA_NAME_VERSION (name
)];
4521 fprintf (file
, "\t");
4522 print_gimple_stmt (file
, gsi_stmt (loc
->si
), 0, 0);
4523 fprintf (file
, "\n\tBB #%d", loc
->bb
->index
);
4526 fprintf (file
, "\n\tEDGE %d->%d", loc
->e
->src
->index
,
4527 loc
->e
->dest
->index
);
4528 dump_edge_info (file
, loc
->e
, dump_flags
, 0);
4530 fprintf (file
, "\n\tPREDICATE: ");
4531 print_generic_expr (file
, name
, 0);
4532 fprintf (file
, " %s ", get_tree_code_name (loc
->comp_code
));
4533 print_generic_expr (file
, loc
->val
, 0);
4534 fprintf (file
, "\n\n");
4538 fprintf (file
, "\n");
4542 /* Dump all the registered assertions for NAME to stderr. */
4545 debug_asserts_for (tree name
)
4547 dump_asserts_for (stderr
, name
);
4551 /* Dump all the registered assertions for all the names to FILE. */
4554 dump_all_asserts (FILE *file
)
4559 fprintf (file
, "\nASSERT_EXPRs to be inserted\n\n");
4560 EXECUTE_IF_SET_IN_BITMAP (need_assert_for
, 0, i
, bi
)
4561 dump_asserts_for (file
, ssa_name (i
));
4562 fprintf (file
, "\n");
4566 /* Dump all the registered assertions for all the names to stderr. */
4569 debug_all_asserts (void)
4571 dump_all_asserts (stderr
);
4575 /* If NAME doesn't have an ASSERT_EXPR registered for asserting
4576 'EXPR COMP_CODE VAL' at a location that dominates block BB or
4577 E->DEST, then register this location as a possible insertion point
4578 for ASSERT_EXPR <NAME, EXPR COMP_CODE VAL>.
4580 BB, E and SI provide the exact insertion point for the new
4581 ASSERT_EXPR. If BB is NULL, then the ASSERT_EXPR is to be inserted
4582 on edge E. Otherwise, if E is NULL, the ASSERT_EXPR is inserted on
4583 BB. If SI points to a COND_EXPR or a SWITCH_EXPR statement, then E
4584 must not be NULL. */
4587 register_new_assert_for (tree name
, tree expr
,
4588 enum tree_code comp_code
,
4592 gimple_stmt_iterator si
)
4594 assert_locus_t n
, loc
, last_loc
;
4595 basic_block dest_bb
;
4597 gcc_checking_assert (bb
== NULL
|| e
== NULL
);
4600 gcc_checking_assert (gimple_code (gsi_stmt (si
)) != GIMPLE_COND
4601 && gimple_code (gsi_stmt (si
)) != GIMPLE_SWITCH
);
4603 /* Never build an assert comparing against an integer constant with
4604 TREE_OVERFLOW set. This confuses our undefined overflow warning
4606 if (TREE_OVERFLOW_P (val
))
4607 val
= drop_tree_overflow (val
);
4609 /* The new assertion A will be inserted at BB or E. We need to
4610 determine if the new location is dominated by a previously
4611 registered location for A. If we are doing an edge insertion,
4612 assume that A will be inserted at E->DEST. Note that this is not
4615 If E is a critical edge, it will be split. But even if E is
4616 split, the new block will dominate the same set of blocks that
4619 The reverse, however, is not true, blocks dominated by E->DEST
4620 will not be dominated by the new block created to split E. So,
4621 if the insertion location is on a critical edge, we will not use
4622 the new location to move another assertion previously registered
4623 at a block dominated by E->DEST. */
4624 dest_bb
= (bb
) ? bb
: e
->dest
;
4626 /* If NAME already has an ASSERT_EXPR registered for COMP_CODE and
4627 VAL at a block dominating DEST_BB, then we don't need to insert a new
4628 one. Similarly, if the same assertion already exists at a block
4629 dominated by DEST_BB and the new location is not on a critical
4630 edge, then update the existing location for the assertion (i.e.,
4631 move the assertion up in the dominance tree).
4633 Note, this is implemented as a simple linked list because there
4634 should not be more than a handful of assertions registered per
4635 name. If this becomes a performance problem, a table hashed by
4636 COMP_CODE and VAL could be implemented. */
4637 loc
= asserts_for
[SSA_NAME_VERSION (name
)];
4641 if (loc
->comp_code
== comp_code
4643 || operand_equal_p (loc
->val
, val
, 0))
4644 && (loc
->expr
== expr
4645 || operand_equal_p (loc
->expr
, expr
, 0)))
4647 /* If E is not a critical edge and DEST_BB
4648 dominates the existing location for the assertion, move
4649 the assertion up in the dominance tree by updating its
4650 location information. */
4651 if ((e
== NULL
|| !EDGE_CRITICAL_P (e
))
4652 && dominated_by_p (CDI_DOMINATORS
, loc
->bb
, dest_bb
))
4661 /* Update the last node of the list and move to the next one. */
4666 /* If we didn't find an assertion already registered for
4667 NAME COMP_CODE VAL, add a new one at the end of the list of
4668 assertions associated with NAME. */
4669 n
= XNEW (struct assert_locus_d
);
4673 n
->comp_code
= comp_code
;
4681 asserts_for
[SSA_NAME_VERSION (name
)] = n
;
4683 bitmap_set_bit (need_assert_for
, SSA_NAME_VERSION (name
));
4686 /* (COND_OP0 COND_CODE COND_OP1) is a predicate which uses NAME.
4687 Extract a suitable test code and value and store them into *CODE_P and
4688 *VAL_P so the predicate is normalized to NAME *CODE_P *VAL_P.
4690 If no extraction was possible, return FALSE, otherwise return TRUE.
4692 If INVERT is true, then we invert the result stored into *CODE_P. */
4695 extract_code_and_val_from_cond_with_ops (tree name
, enum tree_code cond_code
,
4696 tree cond_op0
, tree cond_op1
,
4697 bool invert
, enum tree_code
*code_p
,
4700 enum tree_code comp_code
;
4703 /* Otherwise, we have a comparison of the form NAME COMP VAL
4704 or VAL COMP NAME. */
4705 if (name
== cond_op1
)
4707 /* If the predicate is of the form VAL COMP NAME, flip
4708 COMP around because we need to register NAME as the
4709 first operand in the predicate. */
4710 comp_code
= swap_tree_comparison (cond_code
);
4715 /* The comparison is of the form NAME COMP VAL, so the
4716 comparison code remains unchanged. */
4717 comp_code
= cond_code
;
4721 /* Invert the comparison code as necessary. */
4723 comp_code
= invert_tree_comparison (comp_code
, 0);
4725 /* VRP does not handle float types. */
4726 if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (val
)))
4729 /* Do not register always-false predicates.
4730 FIXME: this works around a limitation in fold() when dealing with
4731 enumerations. Given 'enum { N1, N2 } x;', fold will not
4732 fold 'if (x > N2)' to 'if (0)'. */
4733 if ((comp_code
== GT_EXPR
|| comp_code
== LT_EXPR
)
4734 && INTEGRAL_TYPE_P (TREE_TYPE (val
)))
4736 tree min
= TYPE_MIN_VALUE (TREE_TYPE (val
));
4737 tree max
= TYPE_MAX_VALUE (TREE_TYPE (val
));
4739 if (comp_code
== GT_EXPR
4741 || compare_values (val
, max
) == 0))
4744 if (comp_code
== LT_EXPR
4746 || compare_values (val
, min
) == 0))
4749 *code_p
= comp_code
;
4754 /* Find out smallest RES where RES > VAL && (RES & MASK) == RES, if any
4755 (otherwise return VAL). VAL and MASK must be zero-extended for
4756 precision PREC. If SGNBIT is non-zero, first xor VAL with SGNBIT
4757 (to transform signed values into unsigned) and at the end xor
4761 masked_increment (double_int val
, double_int mask
, double_int sgnbit
,
4764 double_int bit
= double_int_one
, res
;
4768 for (i
= 0; i
< prec
; i
++, bit
+= bit
)
4771 if ((res
& bit
).is_zero ())
4773 res
= bit
- double_int_one
;
4774 res
= (val
+ bit
).and_not (res
);
4777 return res
^ sgnbit
;
4779 return val
^ sgnbit
;
4782 /* Try to register an edge assertion for SSA name NAME on edge E for
4783 the condition COND contributing to the conditional jump pointed to by BSI.
4784 Invert the condition COND if INVERT is true.
4785 Return true if an assertion for NAME could be registered. */
4788 register_edge_assert_for_2 (tree name
, edge e
, gimple_stmt_iterator bsi
,
4789 enum tree_code cond_code
,
4790 tree cond_op0
, tree cond_op1
, bool invert
)
4793 enum tree_code comp_code
;
4794 bool retval
= false;
4796 if (!extract_code_and_val_from_cond_with_ops (name
, cond_code
,
4799 invert
, &comp_code
, &val
))
4802 /* Only register an ASSERT_EXPR if NAME was found in the sub-graph
4803 reachable from E. */
4804 if (live_on_edge (e
, name
)
4805 && !has_single_use (name
))
4807 register_new_assert_for (name
, name
, comp_code
, val
, NULL
, e
, bsi
);
4811 /* In the case of NAME <= CST and NAME being defined as
4812 NAME = (unsigned) NAME2 + CST2 we can assert NAME2 >= -CST2
4813 and NAME2 <= CST - CST2. We can do the same for NAME > CST.
4814 This catches range and anti-range tests. */
4815 if ((comp_code
== LE_EXPR
4816 || comp_code
== GT_EXPR
)
4817 && TREE_CODE (val
) == INTEGER_CST
4818 && TYPE_UNSIGNED (TREE_TYPE (val
)))
4820 gimple def_stmt
= SSA_NAME_DEF_STMT (name
);
4821 tree cst2
= NULL_TREE
, name2
= NULL_TREE
, name3
= NULL_TREE
;
4823 /* Extract CST2 from the (optional) addition. */
4824 if (is_gimple_assign (def_stmt
)
4825 && gimple_assign_rhs_code (def_stmt
) == PLUS_EXPR
)
4827 name2
= gimple_assign_rhs1 (def_stmt
);
4828 cst2
= gimple_assign_rhs2 (def_stmt
);
4829 if (TREE_CODE (name2
) == SSA_NAME
4830 && TREE_CODE (cst2
) == INTEGER_CST
)
4831 def_stmt
= SSA_NAME_DEF_STMT (name2
);
4834 /* Extract NAME2 from the (optional) sign-changing cast. */
4835 if (gimple_assign_cast_p (def_stmt
))
4837 if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt
))
4838 && ! TYPE_UNSIGNED (TREE_TYPE (gimple_assign_rhs1 (def_stmt
)))
4839 && (TYPE_PRECISION (gimple_expr_type (def_stmt
))
4840 == TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (def_stmt
)))))
4841 name3
= gimple_assign_rhs1 (def_stmt
);
4844 /* If name3 is used later, create an ASSERT_EXPR for it. */
4845 if (name3
!= NULL_TREE
4846 && TREE_CODE (name3
) == SSA_NAME
4847 && (cst2
== NULL_TREE
4848 || TREE_CODE (cst2
) == INTEGER_CST
)
4849 && INTEGRAL_TYPE_P (TREE_TYPE (name3
))
4850 && live_on_edge (e
, name3
)
4851 && !has_single_use (name3
))
4855 /* Build an expression for the range test. */
4856 tmp
= build1 (NOP_EXPR
, TREE_TYPE (name
), name3
);
4857 if (cst2
!= NULL_TREE
)
4858 tmp
= build2 (PLUS_EXPR
, TREE_TYPE (name
), tmp
, cst2
);
4862 fprintf (dump_file
, "Adding assert for ");
4863 print_generic_expr (dump_file
, name3
, 0);
4864 fprintf (dump_file
, " from ");
4865 print_generic_expr (dump_file
, tmp
, 0);
4866 fprintf (dump_file
, "\n");
4869 register_new_assert_for (name3
, tmp
, comp_code
, val
, NULL
, e
, bsi
);
4874 /* If name2 is used later, create an ASSERT_EXPR for it. */
4875 if (name2
!= NULL_TREE
4876 && TREE_CODE (name2
) == SSA_NAME
4877 && TREE_CODE (cst2
) == INTEGER_CST
4878 && INTEGRAL_TYPE_P (TREE_TYPE (name2
))
4879 && live_on_edge (e
, name2
)
4880 && !has_single_use (name2
))
4884 /* Build an expression for the range test. */
4886 if (TREE_TYPE (name
) != TREE_TYPE (name2
))
4887 tmp
= build1 (NOP_EXPR
, TREE_TYPE (name
), tmp
);
4888 if (cst2
!= NULL_TREE
)
4889 tmp
= build2 (PLUS_EXPR
, TREE_TYPE (name
), tmp
, cst2
);
4893 fprintf (dump_file
, "Adding assert for ");
4894 print_generic_expr (dump_file
, name2
, 0);
4895 fprintf (dump_file
, " from ");
4896 print_generic_expr (dump_file
, tmp
, 0);
4897 fprintf (dump_file
, "\n");
4900 register_new_assert_for (name2
, tmp
, comp_code
, val
, NULL
, e
, bsi
);
4906 /* In the case of post-in/decrement tests like if (i++) ... and uses
4907 of the in/decremented value on the edge the extra name we want to
4908 assert for is not on the def chain of the name compared. Instead
4909 it is in the set of use stmts. */
4910 if ((comp_code
== NE_EXPR
4911 || comp_code
== EQ_EXPR
)
4912 && TREE_CODE (val
) == INTEGER_CST
)
4914 imm_use_iterator ui
;
4916 FOR_EACH_IMM_USE_STMT (use_stmt
, ui
, name
)
4918 /* Cut off to use-stmts that are in the predecessor. */
4919 if (gimple_bb (use_stmt
) != e
->src
)
4922 if (!is_gimple_assign (use_stmt
))
4925 enum tree_code code
= gimple_assign_rhs_code (use_stmt
);
4926 if (code
!= PLUS_EXPR
4927 && code
!= MINUS_EXPR
)
4930 tree cst
= gimple_assign_rhs2 (use_stmt
);
4931 if (TREE_CODE (cst
) != INTEGER_CST
)
4934 tree name2
= gimple_assign_lhs (use_stmt
);
4935 if (live_on_edge (e
, name2
))
4937 cst
= int_const_binop (code
, val
, cst
);
4938 register_new_assert_for (name2
, name2
, comp_code
, cst
,
4945 if (TREE_CODE_CLASS (comp_code
) == tcc_comparison
4946 && TREE_CODE (val
) == INTEGER_CST
)
4948 gimple def_stmt
= SSA_NAME_DEF_STMT (name
);
4949 tree name2
= NULL_TREE
, names
[2], cst2
= NULL_TREE
;
4950 tree val2
= NULL_TREE
;
4951 double_int mask
= double_int_zero
;
4952 unsigned int prec
= TYPE_PRECISION (TREE_TYPE (val
));
4953 unsigned int nprec
= prec
;
4954 enum tree_code rhs_code
= ERROR_MARK
;
4956 if (is_gimple_assign (def_stmt
))
4957 rhs_code
= gimple_assign_rhs_code (def_stmt
);
4959 /* Add asserts for NAME cmp CST and NAME being defined
4960 as NAME = (int) NAME2. */
4961 if (!TYPE_UNSIGNED (TREE_TYPE (val
))
4962 && (comp_code
== LE_EXPR
|| comp_code
== LT_EXPR
4963 || comp_code
== GT_EXPR
|| comp_code
== GE_EXPR
)
4964 && gimple_assign_cast_p (def_stmt
))
4966 name2
= gimple_assign_rhs1 (def_stmt
);
4967 if (CONVERT_EXPR_CODE_P (rhs_code
)
4968 && INTEGRAL_TYPE_P (TREE_TYPE (name2
))
4969 && TYPE_UNSIGNED (TREE_TYPE (name2
))
4970 && prec
== TYPE_PRECISION (TREE_TYPE (name2
))
4971 && (comp_code
== LE_EXPR
|| comp_code
== GT_EXPR
4972 || !tree_int_cst_equal (val
,
4973 TYPE_MIN_VALUE (TREE_TYPE (val
))))
4974 && live_on_edge (e
, name2
)
4975 && !has_single_use (name2
))
4978 enum tree_code new_comp_code
= comp_code
;
4980 cst
= fold_convert (TREE_TYPE (name2
),
4981 TYPE_MIN_VALUE (TREE_TYPE (val
)));
4982 /* Build an expression for the range test. */
4983 tmp
= build2 (PLUS_EXPR
, TREE_TYPE (name2
), name2
, cst
);
4984 cst
= fold_build2 (PLUS_EXPR
, TREE_TYPE (name2
), cst
,
4985 fold_convert (TREE_TYPE (name2
), val
));
4986 if (comp_code
== LT_EXPR
|| comp_code
== GE_EXPR
)
4988 new_comp_code
= comp_code
== LT_EXPR
? LE_EXPR
: GT_EXPR
;
4989 cst
= fold_build2 (MINUS_EXPR
, TREE_TYPE (name2
), cst
,
4990 build_int_cst (TREE_TYPE (name2
), 1));
4995 fprintf (dump_file
, "Adding assert for ");
4996 print_generic_expr (dump_file
, name2
, 0);
4997 fprintf (dump_file
, " from ");
4998 print_generic_expr (dump_file
, tmp
, 0);
4999 fprintf (dump_file
, "\n");
5002 register_new_assert_for (name2
, tmp
, new_comp_code
, cst
, NULL
,
5009 /* Add asserts for NAME cmp CST and NAME being defined as
5010 NAME = NAME2 >> CST2.
5012 Extract CST2 from the right shift. */
5013 if (rhs_code
== RSHIFT_EXPR
)
5015 name2
= gimple_assign_rhs1 (def_stmt
);
5016 cst2
= gimple_assign_rhs2 (def_stmt
);
5017 if (TREE_CODE (name2
) == SSA_NAME
5018 && tree_fits_uhwi_p (cst2
)
5019 && INTEGRAL_TYPE_P (TREE_TYPE (name2
))
5020 && IN_RANGE (tree_to_uhwi (cst2
), 1, prec
- 1)
5021 && prec
<= HOST_BITS_PER_DOUBLE_INT
5022 && prec
== GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (val
)))
5023 && live_on_edge (e
, name2
)
5024 && !has_single_use (name2
))
5026 mask
= double_int::mask (tree_to_uhwi (cst2
));
5027 val2
= fold_binary (LSHIFT_EXPR
, TREE_TYPE (val
), val
, cst2
);
5030 if (val2
!= NULL_TREE
5031 && TREE_CODE (val2
) == INTEGER_CST
5032 && simple_cst_equal (fold_build2 (RSHIFT_EXPR
,
5036 enum tree_code new_comp_code
= comp_code
;
5040 if (comp_code
== EQ_EXPR
|| comp_code
== NE_EXPR
)
5042 if (!TYPE_UNSIGNED (TREE_TYPE (val
)))
5044 tree type
= build_nonstandard_integer_type (prec
, 1);
5045 tmp
= build1 (NOP_EXPR
, type
, name2
);
5046 val2
= fold_convert (type
, val2
);
5048 tmp
= fold_build2 (MINUS_EXPR
, TREE_TYPE (tmp
), tmp
, val2
);
5049 new_val
= double_int_to_tree (TREE_TYPE (tmp
), mask
);
5050 new_comp_code
= comp_code
== EQ_EXPR
? LE_EXPR
: GT_EXPR
;
5052 else if (comp_code
== LT_EXPR
|| comp_code
== GE_EXPR
)
5055 = double_int::min_value (prec
, TYPE_UNSIGNED (TREE_TYPE (val
)));
5057 if (minval
== tree_to_double_int (new_val
))
5058 new_val
= NULL_TREE
;
5063 = double_int::max_value (prec
, TYPE_UNSIGNED (TREE_TYPE (val
)));
5064 mask
|= tree_to_double_int (val2
);
5066 new_val
= NULL_TREE
;
5068 new_val
= double_int_to_tree (TREE_TYPE (val2
), mask
);
5075 fprintf (dump_file
, "Adding assert for ");
5076 print_generic_expr (dump_file
, name2
, 0);
5077 fprintf (dump_file
, " from ");
5078 print_generic_expr (dump_file
, tmp
, 0);
5079 fprintf (dump_file
, "\n");
5082 register_new_assert_for (name2
, tmp
, new_comp_code
, new_val
,
5088 /* Add asserts for NAME cmp CST and NAME being defined as
5089 NAME = NAME2 & CST2.
5091 Extract CST2 from the and.
5094 NAME = (unsigned) NAME2;
5095 casts where NAME's type is unsigned and has smaller precision
5096 than NAME2's type as if it was NAME = NAME2 & MASK. */
5097 names
[0] = NULL_TREE
;
5098 names
[1] = NULL_TREE
;
5100 if (rhs_code
== BIT_AND_EXPR
5101 || (CONVERT_EXPR_CODE_P (rhs_code
)
5102 && TREE_CODE (TREE_TYPE (val
)) == INTEGER_TYPE
5103 && TYPE_UNSIGNED (TREE_TYPE (val
))
5104 && TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (def_stmt
)))
5108 name2
= gimple_assign_rhs1 (def_stmt
);
5109 if (rhs_code
== BIT_AND_EXPR
)
5110 cst2
= gimple_assign_rhs2 (def_stmt
);
5113 cst2
= TYPE_MAX_VALUE (TREE_TYPE (val
));
5114 nprec
= TYPE_PRECISION (TREE_TYPE (name2
));
5116 if (TREE_CODE (name2
) == SSA_NAME
5117 && INTEGRAL_TYPE_P (TREE_TYPE (name2
))
5118 && TREE_CODE (cst2
) == INTEGER_CST
5119 && !integer_zerop (cst2
)
5120 && nprec
<= HOST_BITS_PER_DOUBLE_INT
5122 || TYPE_UNSIGNED (TREE_TYPE (val
))))
5124 gimple def_stmt2
= SSA_NAME_DEF_STMT (name2
);
5125 if (gimple_assign_cast_p (def_stmt2
))
5127 names
[1] = gimple_assign_rhs1 (def_stmt2
);
5128 if (!CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt2
))
5129 || !INTEGRAL_TYPE_P (TREE_TYPE (names
[1]))
5130 || (TYPE_PRECISION (TREE_TYPE (name2
))
5131 != TYPE_PRECISION (TREE_TYPE (names
[1])))
5132 || !live_on_edge (e
, names
[1])
5133 || has_single_use (names
[1]))
5134 names
[1] = NULL_TREE
;
5136 if (live_on_edge (e
, name2
)
5137 && !has_single_use (name2
))
5141 if (names
[0] || names
[1])
5143 double_int minv
, maxv
= double_int_zero
, valv
, cst2v
;
5144 double_int tem
, sgnbit
;
5145 bool valid_p
= false, valn
= false, cst2n
= false;
5146 enum tree_code ccode
= comp_code
;
5148 valv
= tree_to_double_int (val
).zext (nprec
);
5149 cst2v
= tree_to_double_int (cst2
).zext (nprec
);
5150 if (!TYPE_UNSIGNED (TREE_TYPE (val
)))
5152 valn
= valv
.sext (nprec
).is_negative ();
5153 cst2n
= cst2v
.sext (nprec
).is_negative ();
5155 /* If CST2 doesn't have most significant bit set,
5156 but VAL is negative, we have comparison like
5157 if ((x & 0x123) > -4) (always true). Just give up. */
5161 sgnbit
= double_int_one
.llshift (nprec
- 1, nprec
).zext (nprec
);
5163 sgnbit
= double_int_zero
;
5164 minv
= valv
& cst2v
;
5168 /* Minimum unsigned value for equality is VAL & CST2
5169 (should be equal to VAL, otherwise we probably should
5170 have folded the comparison into false) and
5171 maximum unsigned value is VAL | ~CST2. */
5172 maxv
= valv
| ~cst2v
;
5173 maxv
= maxv
.zext (nprec
);
5177 tem
= valv
| ~cst2v
;
5178 tem
= tem
.zext (nprec
);
5179 /* If VAL is 0, handle (X & CST2) != 0 as (X & CST2) > 0U. */
5180 if (valv
.is_zero ())
5183 sgnbit
= double_int_zero
;
5186 /* If (VAL | ~CST2) is all ones, handle it as
5187 (X & CST2) < VAL. */
5188 if (tem
== double_int::mask (nprec
))
5192 sgnbit
= double_int_zero
;
5196 && cst2v
.sext (nprec
).is_negative ())
5198 = double_int_one
.llshift (nprec
- 1, nprec
).zext (nprec
);
5199 if (!sgnbit
.is_zero ())
5207 if (tem
== double_int::mask (nprec
- 1))
5213 sgnbit
= double_int_zero
;
5217 /* Minimum unsigned value for >= if (VAL & CST2) == VAL
5218 is VAL and maximum unsigned value is ~0. For signed
5219 comparison, if CST2 doesn't have most significant bit
5220 set, handle it similarly. If CST2 has MSB set,
5221 the minimum is the same, and maximum is ~0U/2. */
5224 /* If (VAL & CST2) != VAL, X & CST2 can't be equal to
5226 minv
= masked_increment (valv
, cst2v
, sgnbit
, nprec
);
5230 maxv
= double_int::mask (nprec
- (cst2n
? 1 : 0));
5235 /* Find out smallest MINV where MINV > VAL
5236 && (MINV & CST2) == MINV, if any. If VAL is signed and
5237 CST2 has MSB set, compute it biased by 1 << (nprec - 1). */
5238 minv
= masked_increment (valv
, cst2v
, sgnbit
, nprec
);
5241 maxv
= double_int::mask (nprec
- (cst2n
? 1 : 0));
5245 /* Minimum unsigned value for <= is 0 and maximum
5246 unsigned value is VAL | ~CST2 if (VAL & CST2) == VAL.
5247 Otherwise, find smallest VAL2 where VAL2 > VAL
5248 && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
5250 For signed comparison, if CST2 doesn't have most
5251 significant bit set, handle it similarly. If CST2 has
5252 MSB set, the maximum is the same and minimum is INT_MIN. */
5257 maxv
= masked_increment (valv
, cst2v
, sgnbit
, nprec
);
5260 maxv
-= double_int_one
;
5263 maxv
= maxv
.zext (nprec
);
5269 /* Minimum unsigned value for < is 0 and maximum
5270 unsigned value is (VAL-1) | ~CST2 if (VAL & CST2) == VAL.
5271 Otherwise, find smallest VAL2 where VAL2 > VAL
5272 && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
5274 For signed comparison, if CST2 doesn't have most
5275 significant bit set, handle it similarly. If CST2 has
5276 MSB set, the maximum is the same and minimum is INT_MIN. */
5285 maxv
= masked_increment (valv
, cst2v
, sgnbit
, nprec
);
5289 maxv
-= double_int_one
;
5291 maxv
= maxv
.zext (nprec
);
5299 && (maxv
- minv
).zext (nprec
) != double_int::mask (nprec
))
5301 tree tmp
, new_val
, type
;
5304 for (i
= 0; i
< 2; i
++)
5307 double_int maxv2
= maxv
;
5309 type
= TREE_TYPE (names
[i
]);
5310 if (!TYPE_UNSIGNED (type
))
5312 type
= build_nonstandard_integer_type (nprec
, 1);
5313 tmp
= build1 (NOP_EXPR
, type
, names
[i
]);
5315 if (!minv
.is_zero ())
5317 tmp
= build2 (PLUS_EXPR
, type
, tmp
,
5318 double_int_to_tree (type
, -minv
));
5319 maxv2
= maxv
- minv
;
5321 new_val
= double_int_to_tree (type
, maxv2
);
5325 fprintf (dump_file
, "Adding assert for ");
5326 print_generic_expr (dump_file
, names
[i
], 0);
5327 fprintf (dump_file
, " from ");
5328 print_generic_expr (dump_file
, tmp
, 0);
5329 fprintf (dump_file
, "\n");
5332 register_new_assert_for (names
[i
], tmp
, LE_EXPR
,
5333 new_val
, NULL
, e
, bsi
);
5343 /* OP is an operand of a truth value expression which is known to have
5344 a particular value. Register any asserts for OP and for any
5345 operands in OP's defining statement.
5347 If CODE is EQ_EXPR, then we want to register OP is zero (false),
5348 if CODE is NE_EXPR, then we want to register OP is nonzero (true). */
5351 register_edge_assert_for_1 (tree op
, enum tree_code code
,
5352 edge e
, gimple_stmt_iterator bsi
)
5354 bool retval
= false;
5357 enum tree_code rhs_code
;
5359 /* We only care about SSA_NAMEs. */
5360 if (TREE_CODE (op
) != SSA_NAME
)
5363 /* We know that OP will have a zero or nonzero value. If OP is used
5364 more than once go ahead and register an assert for OP.
5366 The FOUND_IN_SUBGRAPH support is not helpful in this situation as
5367 it will always be set for OP (because OP is used in a COND_EXPR in
5369 if (!has_single_use (op
))
5371 val
= build_int_cst (TREE_TYPE (op
), 0);
5372 register_new_assert_for (op
, op
, code
, val
, NULL
, e
, bsi
);
5376 /* Now look at how OP is set. If it's set from a comparison,
5377 a truth operation or some bit operations, then we may be able
5378 to register information about the operands of that assignment. */
5379 op_def
= SSA_NAME_DEF_STMT (op
);
5380 if (gimple_code (op_def
) != GIMPLE_ASSIGN
)
5383 rhs_code
= gimple_assign_rhs_code (op_def
);
5385 if (TREE_CODE_CLASS (rhs_code
) == tcc_comparison
)
5387 bool invert
= (code
== EQ_EXPR
? true : false);
5388 tree op0
= gimple_assign_rhs1 (op_def
);
5389 tree op1
= gimple_assign_rhs2 (op_def
);
5391 if (TREE_CODE (op0
) == SSA_NAME
)
5392 retval
|= register_edge_assert_for_2 (op0
, e
, bsi
, rhs_code
, op0
, op1
,
5394 if (TREE_CODE (op1
) == SSA_NAME
)
5395 retval
|= register_edge_assert_for_2 (op1
, e
, bsi
, rhs_code
, op0
, op1
,
5398 else if ((code
== NE_EXPR
5399 && gimple_assign_rhs_code (op_def
) == BIT_AND_EXPR
)
5401 && gimple_assign_rhs_code (op_def
) == BIT_IOR_EXPR
))
5403 /* Recurse on each operand. */
5404 tree op0
= gimple_assign_rhs1 (op_def
);
5405 tree op1
= gimple_assign_rhs2 (op_def
);
5406 if (TREE_CODE (op0
) == SSA_NAME
5407 && has_single_use (op0
))
5408 retval
|= register_edge_assert_for_1 (op0
, code
, e
, bsi
);
5409 if (TREE_CODE (op1
) == SSA_NAME
5410 && has_single_use (op1
))
5411 retval
|= register_edge_assert_for_1 (op1
, code
, e
, bsi
);
5413 else if (gimple_assign_rhs_code (op_def
) == BIT_NOT_EXPR
5414 && TYPE_PRECISION (TREE_TYPE (gimple_assign_lhs (op_def
))) == 1)
5416 /* Recurse, flipping CODE. */
5417 code
= invert_tree_comparison (code
, false);
5418 retval
|= register_edge_assert_for_1 (gimple_assign_rhs1 (op_def
),
5421 else if (gimple_assign_rhs_code (op_def
) == SSA_NAME
)
5423 /* Recurse through the copy. */
5424 retval
|= register_edge_assert_for_1 (gimple_assign_rhs1 (op_def
),
5427 else if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (op_def
)))
5429 /* Recurse through the type conversion. */
5430 retval
|= register_edge_assert_for_1 (gimple_assign_rhs1 (op_def
),
5437 /* Try to register an edge assertion for SSA name NAME on edge E for
5438 the condition COND contributing to the conditional jump pointed to by SI.
5439 Return true if an assertion for NAME could be registered. */
5442 register_edge_assert_for (tree name
, edge e
, gimple_stmt_iterator si
,
5443 enum tree_code cond_code
, tree cond_op0
,
5447 enum tree_code comp_code
;
5448 bool retval
= false;
5449 bool is_else_edge
= (e
->flags
& EDGE_FALSE_VALUE
) != 0;
5451 /* Do not attempt to infer anything in names that flow through
5453 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name
))
5456 if (!extract_code_and_val_from_cond_with_ops (name
, cond_code
,
5462 /* Register ASSERT_EXPRs for name. */
5463 retval
|= register_edge_assert_for_2 (name
, e
, si
, cond_code
, cond_op0
,
5464 cond_op1
, is_else_edge
);
5467 /* If COND is effectively an equality test of an SSA_NAME against
5468 the value zero or one, then we may be able to assert values
5469 for SSA_NAMEs which flow into COND. */
5471 /* In the case of NAME == 1 or NAME != 0, for BIT_AND_EXPR defining
5472 statement of NAME we can assert both operands of the BIT_AND_EXPR
5473 have nonzero value. */
5474 if (((comp_code
== EQ_EXPR
&& integer_onep (val
))
5475 || (comp_code
== NE_EXPR
&& integer_zerop (val
))))
5477 gimple def_stmt
= SSA_NAME_DEF_STMT (name
);
5479 if (is_gimple_assign (def_stmt
)
5480 && gimple_assign_rhs_code (def_stmt
) == BIT_AND_EXPR
)
5482 tree op0
= gimple_assign_rhs1 (def_stmt
);
5483 tree op1
= gimple_assign_rhs2 (def_stmt
);
5484 retval
|= register_edge_assert_for_1 (op0
, NE_EXPR
, e
, si
);
5485 retval
|= register_edge_assert_for_1 (op1
, NE_EXPR
, e
, si
);
5489 /* In the case of NAME == 0 or NAME != 1, for BIT_IOR_EXPR defining
5490 statement of NAME we can assert both operands of the BIT_IOR_EXPR
5492 if (((comp_code
== EQ_EXPR
&& integer_zerop (val
))
5493 || (comp_code
== NE_EXPR
&& integer_onep (val
))))
5495 gimple def_stmt
= SSA_NAME_DEF_STMT (name
);
5497 /* For BIT_IOR_EXPR only if NAME == 0 both operands have
5498 necessarily zero value, or if type-precision is one. */
5499 if (is_gimple_assign (def_stmt
)
5500 && (gimple_assign_rhs_code (def_stmt
) == BIT_IOR_EXPR
5501 && (TYPE_PRECISION (TREE_TYPE (name
)) == 1
5502 || comp_code
== EQ_EXPR
)))
5504 tree op0
= gimple_assign_rhs1 (def_stmt
);
5505 tree op1
= gimple_assign_rhs2 (def_stmt
);
5506 retval
|= register_edge_assert_for_1 (op0
, EQ_EXPR
, e
, si
);
5507 retval
|= register_edge_assert_for_1 (op1
, EQ_EXPR
, e
, si
);
5515 /* Determine whether the outgoing edges of BB should receive an
5516 ASSERT_EXPR for each of the operands of BB's LAST statement.
5517 The last statement of BB must be a COND_EXPR.
5519 If any of the sub-graphs rooted at BB have an interesting use of
5520 the predicate operands, an assert location node is added to the
5521 list of assertions for the corresponding operands. */
5524 find_conditional_asserts (basic_block bb
, gimple last
)
5527 gimple_stmt_iterator bsi
;
5533 need_assert
= false;
5534 bsi
= gsi_for_stmt (last
);
5536 /* Look for uses of the operands in each of the sub-graphs
5537 rooted at BB. We need to check each of the outgoing edges
5538 separately, so that we know what kind of ASSERT_EXPR to
5540 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
5545 /* Register the necessary assertions for each operand in the
5546 conditional predicate. */
5547 FOR_EACH_SSA_TREE_OPERAND (op
, last
, iter
, SSA_OP_USE
)
5549 need_assert
|= register_edge_assert_for (op
, e
, bsi
,
5550 gimple_cond_code (last
),
5551 gimple_cond_lhs (last
),
5552 gimple_cond_rhs (last
));
5565 /* Compare two case labels sorting first by the destination bb index
5566 and then by the case value. */
5569 compare_case_labels (const void *p1
, const void *p2
)
5571 const struct case_info
*ci1
= (const struct case_info
*) p1
;
5572 const struct case_info
*ci2
= (const struct case_info
*) p2
;
5573 int idx1
= ci1
->bb
->index
;
5574 int idx2
= ci2
->bb
->index
;
5578 else if (idx1
== idx2
)
5580 /* Make sure the default label is first in a group. */
5581 if (!CASE_LOW (ci1
->expr
))
5583 else if (!CASE_LOW (ci2
->expr
))
5586 return tree_int_cst_compare (CASE_LOW (ci1
->expr
),
5587 CASE_LOW (ci2
->expr
));
5593 /* Determine whether the outgoing edges of BB should receive an
5594 ASSERT_EXPR for each of the operands of BB's LAST statement.
5595 The last statement of BB must be a SWITCH_EXPR.
5597 If any of the sub-graphs rooted at BB have an interesting use of
5598 the predicate operands, an assert location node is added to the
5599 list of assertions for the corresponding operands. */
5602 find_switch_asserts (basic_block bb
, gimple last
)
5605 gimple_stmt_iterator bsi
;
5608 struct case_info
*ci
;
5609 size_t n
= gimple_switch_num_labels (last
);
5610 #if GCC_VERSION >= 4000
5613 /* Work around GCC 3.4 bug (PR 37086). */
5614 volatile unsigned int idx
;
5617 need_assert
= false;
5618 bsi
= gsi_for_stmt (last
);
5619 op
= gimple_switch_index (last
);
5620 if (TREE_CODE (op
) != SSA_NAME
)
5623 /* Build a vector of case labels sorted by destination label. */
5624 ci
= XNEWVEC (struct case_info
, n
);
5625 for (idx
= 0; idx
< n
; ++idx
)
5627 ci
[idx
].expr
= gimple_switch_label (last
, idx
);
5628 ci
[idx
].bb
= label_to_block (CASE_LABEL (ci
[idx
].expr
));
5630 qsort (ci
, n
, sizeof (struct case_info
), compare_case_labels
);
5632 for (idx
= 0; idx
< n
; ++idx
)
5635 tree cl
= ci
[idx
].expr
;
5636 basic_block cbb
= ci
[idx
].bb
;
5638 min
= CASE_LOW (cl
);
5639 max
= CASE_HIGH (cl
);
5641 /* If there are multiple case labels with the same destination
5642 we need to combine them to a single value range for the edge. */
5643 if (idx
+ 1 < n
&& cbb
== ci
[idx
+ 1].bb
)
5645 /* Skip labels until the last of the group. */
5648 } while (idx
< n
&& cbb
== ci
[idx
].bb
);
5651 /* Pick up the maximum of the case label range. */
5652 if (CASE_HIGH (ci
[idx
].expr
))
5653 max
= CASE_HIGH (ci
[idx
].expr
);
5655 max
= CASE_LOW (ci
[idx
].expr
);
5658 /* Nothing to do if the range includes the default label until we
5659 can register anti-ranges. */
5660 if (min
== NULL_TREE
)
5663 /* Find the edge to register the assert expr on. */
5664 e
= find_edge (bb
, cbb
);
5666 /* Register the necessary assertions for the operand in the
5668 need_assert
|= register_edge_assert_for (op
, e
, bsi
,
5669 max
? GE_EXPR
: EQ_EXPR
,
5671 fold_convert (TREE_TYPE (op
),
5675 need_assert
|= register_edge_assert_for (op
, e
, bsi
, LE_EXPR
,
5677 fold_convert (TREE_TYPE (op
),
5687 /* Traverse all the statements in block BB looking for statements that
5688 may generate useful assertions for the SSA names in their operand.
5689 If a statement produces a useful assertion A for name N_i, then the
5690 list of assertions already generated for N_i is scanned to
5691 determine if A is actually needed.
5693 If N_i already had the assertion A at a location dominating the
5694 current location, then nothing needs to be done. Otherwise, the
5695 new location for A is recorded instead.
5697 1- For every statement S in BB, all the variables used by S are
5698 added to bitmap FOUND_IN_SUBGRAPH.
5700 2- If statement S uses an operand N in a way that exposes a known
5701 value range for N, then if N was not already generated by an
5702 ASSERT_EXPR, create a new assert location for N. For instance,
5703 if N is a pointer and the statement dereferences it, we can
5704 assume that N is not NULL.
5706 3- COND_EXPRs are a special case of #2. We can derive range
5707 information from the predicate but need to insert different
5708 ASSERT_EXPRs for each of the sub-graphs rooted at the
5709 conditional block. If the last statement of BB is a conditional
5710 expression of the form 'X op Y', then
5712 a) Remove X and Y from the set FOUND_IN_SUBGRAPH.
5714 b) If the conditional is the only entry point to the sub-graph
5715 corresponding to the THEN_CLAUSE, recurse into it. On
5716 return, if X and/or Y are marked in FOUND_IN_SUBGRAPH, then
5717 an ASSERT_EXPR is added for the corresponding variable.
5719 c) Repeat step (b) on the ELSE_CLAUSE.
5721 d) Mark X and Y in FOUND_IN_SUBGRAPH.
5730 In this case, an assertion on the THEN clause is useful to
5731 determine that 'a' is always 9 on that edge. However, an assertion
5732 on the ELSE clause would be unnecessary.
5734 4- If BB does not end in a conditional expression, then we recurse
5735 into BB's dominator children.
5737 At the end of the recursive traversal, every SSA name will have a
5738 list of locations where ASSERT_EXPRs should be added. When a new
5739 location for name N is found, it is registered by calling
5740 register_new_assert_for. That function keeps track of all the
5741 registered assertions to prevent adding unnecessary assertions.
5742 For instance, if a pointer P_4 is dereferenced more than once in a
5743 dominator tree, only the location dominating all the dereference of
5744 P_4 will receive an ASSERT_EXPR.
5746 If this function returns true, then it means that there are names
5747 for which we need to generate ASSERT_EXPRs. Those assertions are
5748 inserted by process_assert_insertions. */
5751 find_assert_locations_1 (basic_block bb
, sbitmap live
)
5753 gimple_stmt_iterator si
;
5757 need_assert
= false;
5758 last
= last_stmt (bb
);
5760 /* If BB's last statement is a conditional statement involving integer
5761 operands, determine if we need to add ASSERT_EXPRs. */
5763 && gimple_code (last
) == GIMPLE_COND
5764 && !fp_predicate (last
)
5765 && !ZERO_SSA_OPERANDS (last
, SSA_OP_USE
))
5766 need_assert
|= find_conditional_asserts (bb
, last
);
5768 /* If BB's last statement is a switch statement involving integer
5769 operands, determine if we need to add ASSERT_EXPRs. */
5771 && gimple_code (last
) == GIMPLE_SWITCH
5772 && !ZERO_SSA_OPERANDS (last
, SSA_OP_USE
))
5773 need_assert
|= find_switch_asserts (bb
, last
);
5775 /* Traverse all the statements in BB marking used names and looking
5776 for statements that may infer assertions for their used operands. */
5777 for (si
= gsi_last_bb (bb
); !gsi_end_p (si
); gsi_prev (&si
))
5783 stmt
= gsi_stmt (si
);
5785 if (is_gimple_debug (stmt
))
5788 /* See if we can derive an assertion for any of STMT's operands. */
5789 FOR_EACH_SSA_TREE_OPERAND (op
, stmt
, i
, SSA_OP_USE
)
5792 enum tree_code comp_code
;
5794 /* If op is not live beyond this stmt, do not bother to insert
5796 if (!bitmap_bit_p (live
, SSA_NAME_VERSION (op
)))
5799 /* If OP is used in such a way that we can infer a value
5800 range for it, and we don't find a previous assertion for
5801 it, create a new assertion location node for OP. */
5802 if (infer_value_range (stmt
, op
, &comp_code
, &value
))
5804 /* If we are able to infer a nonzero value range for OP,
5805 then walk backwards through the use-def chain to see if OP
5806 was set via a typecast.
5808 If so, then we can also infer a nonzero value range
5809 for the operand of the NOP_EXPR. */
5810 if (comp_code
== NE_EXPR
&& integer_zerop (value
))
5813 gimple def_stmt
= SSA_NAME_DEF_STMT (t
);
5815 while (is_gimple_assign (def_stmt
)
5816 && gimple_assign_rhs_code (def_stmt
) == NOP_EXPR
5818 (gimple_assign_rhs1 (def_stmt
)) == SSA_NAME
5820 (TREE_TYPE (gimple_assign_rhs1 (def_stmt
))))
5822 t
= gimple_assign_rhs1 (def_stmt
);
5823 def_stmt
= SSA_NAME_DEF_STMT (t
);
5825 /* Note we want to register the assert for the
5826 operand of the NOP_EXPR after SI, not after the
5828 if (! has_single_use (t
))
5830 register_new_assert_for (t
, t
, comp_code
, value
,
5837 register_new_assert_for (op
, op
, comp_code
, value
, bb
, NULL
, si
);
5843 FOR_EACH_SSA_TREE_OPERAND (op
, stmt
, i
, SSA_OP_USE
)
5844 bitmap_set_bit (live
, SSA_NAME_VERSION (op
));
5845 FOR_EACH_SSA_TREE_OPERAND (op
, stmt
, i
, SSA_OP_DEF
)
5846 bitmap_clear_bit (live
, SSA_NAME_VERSION (op
));
5849 /* Traverse all PHI nodes in BB, updating live. */
5850 for (si
= gsi_start_phis (bb
); !gsi_end_p (si
); gsi_next (&si
))
5852 use_operand_p arg_p
;
5854 gimple phi
= gsi_stmt (si
);
5855 tree res
= gimple_phi_result (phi
);
5857 if (virtual_operand_p (res
))
5860 FOR_EACH_PHI_ARG (arg_p
, phi
, i
, SSA_OP_USE
)
5862 tree arg
= USE_FROM_PTR (arg_p
);
5863 if (TREE_CODE (arg
) == SSA_NAME
)
5864 bitmap_set_bit (live
, SSA_NAME_VERSION (arg
));
5867 bitmap_clear_bit (live
, SSA_NAME_VERSION (res
));
5873 /* Do an RPO walk over the function computing SSA name liveness
5874 on-the-fly and deciding on assert expressions to insert.
5875 Returns true if there are assert expressions to be inserted. */
5878 find_assert_locations (void)
5880 int *rpo
= XNEWVEC (int, last_basic_block
);
5881 int *bb_rpo
= XNEWVEC (int, last_basic_block
);
5882 int *last_rpo
= XCNEWVEC (int, last_basic_block
);
5886 live
= XCNEWVEC (sbitmap
, last_basic_block
);
5887 rpo_cnt
= pre_and_rev_post_order_compute (NULL
, rpo
, false);
5888 for (i
= 0; i
< rpo_cnt
; ++i
)
5891 /* Pre-seed loop latch liveness from loop header PHI nodes. Due to
5892 the order we compute liveness and insert asserts we otherwise
5893 fail to insert asserts into the loop latch. */
5896 FOR_EACH_LOOP (li
, loop
, 0)
5898 i
= loop
->latch
->index
;
5899 unsigned int j
= single_succ_edge (loop
->latch
)->dest_idx
;
5900 for (gimple_stmt_iterator gsi
= gsi_start_phis (loop
->header
);
5901 !gsi_end_p (gsi
); gsi_next (&gsi
))
5903 gimple phi
= gsi_stmt (gsi
);
5904 if (virtual_operand_p (gimple_phi_result (phi
)))
5906 tree arg
= gimple_phi_arg_def (phi
, j
);
5907 if (TREE_CODE (arg
) == SSA_NAME
)
5909 if (live
[i
] == NULL
)
5911 live
[i
] = sbitmap_alloc (num_ssa_names
);
5912 bitmap_clear (live
[i
]);
5914 bitmap_set_bit (live
[i
], SSA_NAME_VERSION (arg
));
5919 need_asserts
= false;
5920 for (i
= rpo_cnt
- 1; i
>= 0; --i
)
5922 basic_block bb
= BASIC_BLOCK (rpo
[i
]);
5928 live
[rpo
[i
]] = sbitmap_alloc (num_ssa_names
);
5929 bitmap_clear (live
[rpo
[i
]]);
5932 /* Process BB and update the live information with uses in
5934 need_asserts
|= find_assert_locations_1 (bb
, live
[rpo
[i
]]);
5936 /* Merge liveness into the predecessor blocks and free it. */
5937 if (!bitmap_empty_p (live
[rpo
[i
]]))
5940 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
5942 int pred
= e
->src
->index
;
5943 if ((e
->flags
& EDGE_DFS_BACK
) || pred
== ENTRY_BLOCK
)
5948 live
[pred
] = sbitmap_alloc (num_ssa_names
);
5949 bitmap_clear (live
[pred
]);
5951 bitmap_ior (live
[pred
], live
[pred
], live
[rpo
[i
]]);
5953 if (bb_rpo
[pred
] < pred_rpo
)
5954 pred_rpo
= bb_rpo
[pred
];
5957 /* Record the RPO number of the last visited block that needs
5958 live information from this block. */
5959 last_rpo
[rpo
[i
]] = pred_rpo
;
5963 sbitmap_free (live
[rpo
[i
]]);
5964 live
[rpo
[i
]] = NULL
;
5967 /* We can free all successors live bitmaps if all their
5968 predecessors have been visited already. */
5969 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
5970 if (last_rpo
[e
->dest
->index
] == i
5971 && live
[e
->dest
->index
])
5973 sbitmap_free (live
[e
->dest
->index
]);
5974 live
[e
->dest
->index
] = NULL
;
5979 XDELETEVEC (bb_rpo
);
5980 XDELETEVEC (last_rpo
);
5981 for (i
= 0; i
< last_basic_block
; ++i
)
5983 sbitmap_free (live
[i
]);
5986 return need_asserts
;
5989 /* Create an ASSERT_EXPR for NAME and insert it in the location
5990 indicated by LOC. Return true if we made any edge insertions. */
5993 process_assert_insertions_for (tree name
, assert_locus_t loc
)
5995 /* Build the comparison expression NAME_i COMP_CODE VAL. */
6002 /* If we have X <=> X do not insert an assert expr for that. */
6003 if (loc
->expr
== loc
->val
)
6006 cond
= build2 (loc
->comp_code
, boolean_type_node
, loc
->expr
, loc
->val
);
6007 assert_stmt
= build_assert_expr_for (cond
, name
);
6010 /* We have been asked to insert the assertion on an edge. This
6011 is used only by COND_EXPR and SWITCH_EXPR assertions. */
6012 gcc_checking_assert (gimple_code (gsi_stmt (loc
->si
)) == GIMPLE_COND
6013 || (gimple_code (gsi_stmt (loc
->si
))
6016 gsi_insert_on_edge (loc
->e
, assert_stmt
);
6020 /* Otherwise, we can insert right after LOC->SI iff the
6021 statement must not be the last statement in the block. */
6022 stmt
= gsi_stmt (loc
->si
);
6023 if (!stmt_ends_bb_p (stmt
))
6025 gsi_insert_after (&loc
->si
, assert_stmt
, GSI_SAME_STMT
);
6029 /* If STMT must be the last statement in BB, we can only insert new
6030 assertions on the non-abnormal edge out of BB. Note that since
6031 STMT is not control flow, there may only be one non-abnormal edge
6033 FOR_EACH_EDGE (e
, ei
, loc
->bb
->succs
)
6034 if (!(e
->flags
& EDGE_ABNORMAL
))
6036 gsi_insert_on_edge (e
, assert_stmt
);
6044 /* Process all the insertions registered for every name N_i registered
6045 in NEED_ASSERT_FOR. The list of assertions to be inserted are
6046 found in ASSERTS_FOR[i]. */
6049 process_assert_insertions (void)
6053 bool update_edges_p
= false;
6054 int num_asserts
= 0;
6056 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6057 dump_all_asserts (dump_file
);
6059 EXECUTE_IF_SET_IN_BITMAP (need_assert_for
, 0, i
, bi
)
6061 assert_locus_t loc
= asserts_for
[i
];
6066 assert_locus_t next
= loc
->next
;
6067 update_edges_p
|= process_assert_insertions_for (ssa_name (i
), loc
);
6075 gsi_commit_edge_inserts ();
6077 statistics_counter_event (cfun
, "Number of ASSERT_EXPR expressions inserted",
6082 /* Traverse the flowgraph looking for conditional jumps to insert range
6083 expressions. These range expressions are meant to provide information
6084 to optimizations that need to reason in terms of value ranges. They
6085 will not be expanded into RTL. For instance, given:
6094 this pass will transform the code into:
6100 x = ASSERT_EXPR <x, x < y>
6105 y = ASSERT_EXPR <y, x <= y>
6109 The idea is that once copy and constant propagation have run, other
6110 optimizations will be able to determine what ranges of values can 'x'
6111 take in different paths of the code, simply by checking the reaching
6112 definition of 'x'. */
6115 insert_range_assertions (void)
6117 need_assert_for
= BITMAP_ALLOC (NULL
);
6118 asserts_for
= XCNEWVEC (assert_locus_t
, num_ssa_names
);
6120 calculate_dominance_info (CDI_DOMINATORS
);
6122 if (find_assert_locations ())
6124 process_assert_insertions ();
6125 update_ssa (TODO_update_ssa_no_phi
);
6128 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6130 fprintf (dump_file
, "\nSSA form after inserting ASSERT_EXPRs\n");
6131 dump_function_to_file (current_function_decl
, dump_file
, dump_flags
);
6135 BITMAP_FREE (need_assert_for
);
6138 /* Checks one ARRAY_REF in REF, located at LOCUS. Ignores flexible arrays
6139 and "struct" hacks. If VRP can determine that the
6140 array subscript is a constant, check if it is outside valid
6141 range. If the array subscript is a RANGE, warn if it is
6142 non-overlapping with valid range.
6143 IGNORE_OFF_BY_ONE is true if the ARRAY_REF is inside a ADDR_EXPR. */
6146 check_array_ref (location_t location
, tree ref
, bool ignore_off_by_one
)
6148 value_range_t
* vr
= NULL
;
6149 tree low_sub
, up_sub
;
6150 tree low_bound
, up_bound
, up_bound_p1
;
6153 if (TREE_NO_WARNING (ref
))
6156 low_sub
= up_sub
= TREE_OPERAND (ref
, 1);
6157 up_bound
= array_ref_up_bound (ref
);
6159 /* Can not check flexible arrays. */
6161 || TREE_CODE (up_bound
) != INTEGER_CST
)
6164 /* Accesses to trailing arrays via pointers may access storage
6165 beyond the types array bounds. */
6166 base
= get_base_address (ref
);
6167 if (base
&& TREE_CODE (base
) == MEM_REF
)
6169 tree cref
, next
= NULL_TREE
;
6171 if (TREE_CODE (TREE_OPERAND (ref
, 0)) != COMPONENT_REF
)
6174 cref
= TREE_OPERAND (ref
, 0);
6175 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (cref
, 0))) == RECORD_TYPE
)
6176 for (next
= DECL_CHAIN (TREE_OPERAND (cref
, 1));
6177 next
&& TREE_CODE (next
) != FIELD_DECL
;
6178 next
= DECL_CHAIN (next
))
6181 /* If this is the last field in a struct type or a field in a
6182 union type do not warn. */
6187 low_bound
= array_ref_low_bound (ref
);
6188 up_bound_p1
= int_const_binop (PLUS_EXPR
, up_bound
, integer_one_node
);
6190 if (TREE_CODE (low_sub
) == SSA_NAME
)
6192 vr
= get_value_range (low_sub
);
6193 if (vr
->type
== VR_RANGE
|| vr
->type
== VR_ANTI_RANGE
)
6195 low_sub
= vr
->type
== VR_RANGE
? vr
->max
: vr
->min
;
6196 up_sub
= vr
->type
== VR_RANGE
? vr
->min
: vr
->max
;
6200 if (vr
&& vr
->type
== VR_ANTI_RANGE
)
6202 if (TREE_CODE (up_sub
) == INTEGER_CST
6203 && tree_int_cst_lt (up_bound
, up_sub
)
6204 && TREE_CODE (low_sub
) == INTEGER_CST
6205 && tree_int_cst_lt (low_sub
, low_bound
))
6207 warning_at (location
, OPT_Warray_bounds
,
6208 "array subscript is outside array bounds");
6209 TREE_NO_WARNING (ref
) = 1;
6212 else if (TREE_CODE (up_sub
) == INTEGER_CST
6213 && (ignore_off_by_one
6214 ? (tree_int_cst_lt (up_bound
, up_sub
)
6215 && !tree_int_cst_equal (up_bound_p1
, up_sub
))
6216 : (tree_int_cst_lt (up_bound
, up_sub
)
6217 || tree_int_cst_equal (up_bound_p1
, up_sub
))))
6219 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6221 fprintf (dump_file
, "Array bound warning for ");
6222 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, ref
);
6223 fprintf (dump_file
, "\n");
6225 warning_at (location
, OPT_Warray_bounds
,
6226 "array subscript is above array bounds");
6227 TREE_NO_WARNING (ref
) = 1;
6229 else if (TREE_CODE (low_sub
) == INTEGER_CST
6230 && tree_int_cst_lt (low_sub
, low_bound
))
6232 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6234 fprintf (dump_file
, "Array bound warning for ");
6235 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, ref
);
6236 fprintf (dump_file
, "\n");
6238 warning_at (location
, OPT_Warray_bounds
,
6239 "array subscript is below array bounds");
6240 TREE_NO_WARNING (ref
) = 1;
6244 /* Searches if the expr T, located at LOCATION computes
6245 address of an ARRAY_REF, and call check_array_ref on it. */
6248 search_for_addr_array (tree t
, location_t location
)
6250 while (TREE_CODE (t
) == SSA_NAME
)
6252 gimple g
= SSA_NAME_DEF_STMT (t
);
6254 if (gimple_code (g
) != GIMPLE_ASSIGN
)
6257 if (get_gimple_rhs_class (gimple_assign_rhs_code (g
))
6258 != GIMPLE_SINGLE_RHS
)
6261 t
= gimple_assign_rhs1 (g
);
6265 /* We are only interested in addresses of ARRAY_REF's. */
6266 if (TREE_CODE (t
) != ADDR_EXPR
)
6269 /* Check each ARRAY_REFs in the reference chain. */
6272 if (TREE_CODE (t
) == ARRAY_REF
)
6273 check_array_ref (location
, t
, true /*ignore_off_by_one*/);
6275 t
= TREE_OPERAND (t
, 0);
6277 while (handled_component_p (t
));
6279 if (TREE_CODE (t
) == MEM_REF
6280 && TREE_CODE (TREE_OPERAND (t
, 0)) == ADDR_EXPR
6281 && !TREE_NO_WARNING (t
))
6283 tree tem
= TREE_OPERAND (TREE_OPERAND (t
, 0), 0);
6284 tree low_bound
, up_bound
, el_sz
;
6286 if (TREE_CODE (TREE_TYPE (tem
)) != ARRAY_TYPE
6287 || TREE_CODE (TREE_TYPE (TREE_TYPE (tem
))) == ARRAY_TYPE
6288 || !TYPE_DOMAIN (TREE_TYPE (tem
)))
6291 low_bound
= TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (tem
)));
6292 up_bound
= TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (tem
)));
6293 el_sz
= TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (tem
)));
6295 || TREE_CODE (low_bound
) != INTEGER_CST
6297 || TREE_CODE (up_bound
) != INTEGER_CST
6299 || TREE_CODE (el_sz
) != INTEGER_CST
)
6302 idx
= mem_ref_offset (t
);
6303 idx
= idx
.sdiv (tree_to_double_int (el_sz
), TRUNC_DIV_EXPR
);
6304 if (idx
.slt (double_int_zero
))
6306 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6308 fprintf (dump_file
, "Array bound warning for ");
6309 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, t
);
6310 fprintf (dump_file
, "\n");
6312 warning_at (location
, OPT_Warray_bounds
,
6313 "array subscript is below array bounds");
6314 TREE_NO_WARNING (t
) = 1;
6316 else if (idx
.sgt (tree_to_double_int (up_bound
)
6317 - tree_to_double_int (low_bound
)
6320 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6322 fprintf (dump_file
, "Array bound warning for ");
6323 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, t
);
6324 fprintf (dump_file
, "\n");
6326 warning_at (location
, OPT_Warray_bounds
,
6327 "array subscript is above array bounds");
6328 TREE_NO_WARNING (t
) = 1;
6333 /* walk_tree() callback that checks if *TP is
6334 an ARRAY_REF inside an ADDR_EXPR (in which an array
6335 subscript one outside the valid range is allowed). Call
6336 check_array_ref for each ARRAY_REF found. The location is
6340 check_array_bounds (tree
*tp
, int *walk_subtree
, void *data
)
6343 struct walk_stmt_info
*wi
= (struct walk_stmt_info
*) data
;
6344 location_t location
;
6346 if (EXPR_HAS_LOCATION (t
))
6347 location
= EXPR_LOCATION (t
);
6350 location_t
*locp
= (location_t
*) wi
->info
;
6354 *walk_subtree
= TRUE
;
6356 if (TREE_CODE (t
) == ARRAY_REF
)
6357 check_array_ref (location
, t
, false /*ignore_off_by_one*/);
6359 if (TREE_CODE (t
) == MEM_REF
6360 || (TREE_CODE (t
) == RETURN_EXPR
&& TREE_OPERAND (t
, 0)))
6361 search_for_addr_array (TREE_OPERAND (t
, 0), location
);
6363 if (TREE_CODE (t
) == ADDR_EXPR
)
6364 *walk_subtree
= FALSE
;
6369 /* Walk over all statements of all reachable BBs and call check_array_bounds
6373 check_all_array_refs (void)
6376 gimple_stmt_iterator si
;
6382 bool executable
= false;
6384 /* Skip blocks that were found to be unreachable. */
6385 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
6386 executable
|= !!(e
->flags
& EDGE_EXECUTABLE
);
6390 for (si
= gsi_start_bb (bb
); !gsi_end_p (si
); gsi_next (&si
))
6392 gimple stmt
= gsi_stmt (si
);
6393 struct walk_stmt_info wi
;
6394 if (!gimple_has_location (stmt
))
6397 if (is_gimple_call (stmt
))
6400 size_t n
= gimple_call_num_args (stmt
);
6401 for (i
= 0; i
< n
; i
++)
6403 tree arg
= gimple_call_arg (stmt
, i
);
6404 search_for_addr_array (arg
, gimple_location (stmt
));
6409 memset (&wi
, 0, sizeof (wi
));
6410 wi
.info
= CONST_CAST (void *, (const void *)
6411 gimple_location_ptr (stmt
));
6413 walk_gimple_op (gsi_stmt (si
),
6421 /* Return true if all imm uses of VAR are either in STMT, or
6422 feed (optionally through a chain of single imm uses) GIMPLE_COND
6423 in basic block COND_BB. */
6426 all_imm_uses_in_stmt_or_feed_cond (tree var
, gimple stmt
, basic_block cond_bb
)
6428 use_operand_p use_p
, use2_p
;
6429 imm_use_iterator iter
;
6431 FOR_EACH_IMM_USE_FAST (use_p
, iter
, var
)
6432 if (USE_STMT (use_p
) != stmt
)
6434 gimple use_stmt
= USE_STMT (use_p
), use_stmt2
;
6435 if (is_gimple_debug (use_stmt
))
6437 while (is_gimple_assign (use_stmt
)
6438 && TREE_CODE (gimple_assign_lhs (use_stmt
)) == SSA_NAME
6439 && single_imm_use (gimple_assign_lhs (use_stmt
),
6440 &use2_p
, &use_stmt2
))
6441 use_stmt
= use_stmt2
;
6442 if (gimple_code (use_stmt
) != GIMPLE_COND
6443 || gimple_bb (use_stmt
) != cond_bb
)
6456 __builtin_unreachable ();
6458 x_5 = ASSERT_EXPR <x_3, ...>;
6459 If x_3 has no other immediate uses (checked by caller),
6460 var is the x_3 var from ASSERT_EXPR, we can clear low 5 bits
6461 from the non-zero bitmask. */
6464 maybe_set_nonzero_bits (basic_block bb
, tree var
)
6466 edge e
= single_pred_edge (bb
);
6467 basic_block cond_bb
= e
->src
;
6468 gimple stmt
= last_stmt (cond_bb
);
6472 || gimple_code (stmt
) != GIMPLE_COND
6473 || gimple_cond_code (stmt
) != ((e
->flags
& EDGE_TRUE_VALUE
)
6474 ? EQ_EXPR
: NE_EXPR
)
6475 || TREE_CODE (gimple_cond_lhs (stmt
)) != SSA_NAME
6476 || !integer_zerop (gimple_cond_rhs (stmt
)))
6479 stmt
= SSA_NAME_DEF_STMT (gimple_cond_lhs (stmt
));
6480 if (!is_gimple_assign (stmt
)
6481 || gimple_assign_rhs_code (stmt
) != BIT_AND_EXPR
6482 || TREE_CODE (gimple_assign_rhs2 (stmt
)) != INTEGER_CST
)
6484 if (gimple_assign_rhs1 (stmt
) != var
)
6488 if (TREE_CODE (gimple_assign_rhs1 (stmt
)) != SSA_NAME
)
6490 stmt2
= SSA_NAME_DEF_STMT (gimple_assign_rhs1 (stmt
));
6491 if (!gimple_assign_cast_p (stmt2
)
6492 || gimple_assign_rhs1 (stmt2
) != var
6493 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (stmt2
))
6494 || (TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (stmt
)))
6495 != TYPE_PRECISION (TREE_TYPE (var
))))
6498 cst
= gimple_assign_rhs2 (stmt
);
6499 set_nonzero_bits (var
, (get_nonzero_bits (var
)
6500 & ~tree_to_double_int (cst
)));
6503 /* Convert range assertion expressions into the implied copies and
6504 copy propagate away the copies. Doing the trivial copy propagation
6505 here avoids the need to run the full copy propagation pass after
6508 FIXME, this will eventually lead to copy propagation removing the
6509 names that had useful range information attached to them. For
6510 instance, if we had the assertion N_i = ASSERT_EXPR <N_j, N_j > 3>,
6511 then N_i will have the range [3, +INF].
6513 However, by converting the assertion into the implied copy
6514 operation N_i = N_j, we will then copy-propagate N_j into the uses
6515 of N_i and lose the range information. We may want to hold on to
6516 ASSERT_EXPRs a little while longer as the ranges could be used in
6517 things like jump threading.
6519 The problem with keeping ASSERT_EXPRs around is that passes after
6520 VRP need to handle them appropriately.
6522 Another approach would be to make the range information a first
6523 class property of the SSA_NAME so that it can be queried from
6524 any pass. This is made somewhat more complex by the need for
6525 multiple ranges to be associated with one SSA_NAME. */
6528 remove_range_assertions (void)
6531 gimple_stmt_iterator si
;
6532 /* 1 if looking at ASSERT_EXPRs immediately at the beginning of
6533 a basic block preceeded by GIMPLE_COND branching to it and
6534 __builtin_trap, -1 if not yet checked, 0 otherwise. */
6537 /* Note that the BSI iterator bump happens at the bottom of the
6538 loop and no bump is necessary if we're removing the statement
6539 referenced by the current BSI. */
6541 for (si
= gsi_after_labels (bb
), is_unreachable
= -1; !gsi_end_p (si
);)
6543 gimple stmt
= gsi_stmt (si
);
6546 if (is_gimple_assign (stmt
)
6547 && gimple_assign_rhs_code (stmt
) == ASSERT_EXPR
)
6549 tree lhs
= gimple_assign_lhs (stmt
);
6550 tree rhs
= gimple_assign_rhs1 (stmt
);
6552 tree cond
= fold (ASSERT_EXPR_COND (rhs
));
6553 use_operand_p use_p
;
6554 imm_use_iterator iter
;
6556 gcc_assert (cond
!= boolean_false_node
);
6558 var
= ASSERT_EXPR_VAR (rhs
);
6559 gcc_assert (TREE_CODE (var
) == SSA_NAME
);
6561 if (!POINTER_TYPE_P (TREE_TYPE (lhs
))
6562 && SSA_NAME_RANGE_INFO (lhs
))
6564 if (is_unreachable
== -1)
6567 if (single_pred_p (bb
)
6568 && assert_unreachable_fallthru_edge_p
6569 (single_pred_edge (bb
)))
6573 if (x_7 >= 10 && x_7 < 20)
6574 __builtin_unreachable ();
6575 x_8 = ASSERT_EXPR <x_7, ...>;
6576 if the only uses of x_7 are in the ASSERT_EXPR and
6577 in the condition. In that case, we can copy the
6578 range info from x_8 computed in this pass also
6581 && all_imm_uses_in_stmt_or_feed_cond (var
, stmt
,
6584 set_range_info (var
, SSA_NAME_RANGE_INFO (lhs
)->min
,
6585 SSA_NAME_RANGE_INFO (lhs
)->max
);
6586 maybe_set_nonzero_bits (bb
, var
);
6590 /* Propagate the RHS into every use of the LHS. */
6591 FOR_EACH_IMM_USE_STMT (use_stmt
, iter
, lhs
)
6592 FOR_EACH_IMM_USE_ON_STMT (use_p
, iter
)
6593 SET_USE (use_p
, var
);
6595 /* And finally, remove the copy, it is not needed. */
6596 gsi_remove (&si
, true);
6597 release_defs (stmt
);
6608 /* Return true if STMT is interesting for VRP. */
6611 stmt_interesting_for_vrp (gimple stmt
)
6613 if (gimple_code (stmt
) == GIMPLE_PHI
)
6615 tree res
= gimple_phi_result (stmt
);
6616 return (!virtual_operand_p (res
)
6617 && (INTEGRAL_TYPE_P (TREE_TYPE (res
))
6618 || POINTER_TYPE_P (TREE_TYPE (res
))));
6620 else if (is_gimple_assign (stmt
) || is_gimple_call (stmt
))
6622 tree lhs
= gimple_get_lhs (stmt
);
6624 /* In general, assignments with virtual operands are not useful
6625 for deriving ranges, with the obvious exception of calls to
6626 builtin functions. */
6627 if (lhs
&& TREE_CODE (lhs
) == SSA_NAME
6628 && (INTEGRAL_TYPE_P (TREE_TYPE (lhs
))
6629 || POINTER_TYPE_P (TREE_TYPE (lhs
)))
6630 && (is_gimple_call (stmt
)
6631 || !gimple_vuse (stmt
)))
6634 else if (gimple_code (stmt
) == GIMPLE_COND
6635 || gimple_code (stmt
) == GIMPLE_SWITCH
)
6642 /* Initialize local data structures for VRP. */
6645 vrp_initialize (void)
6649 values_propagated
= false;
6650 num_vr_values
= num_ssa_names
;
6651 vr_value
= XCNEWVEC (value_range_t
*, num_vr_values
);
6652 vr_phi_edge_counts
= XCNEWVEC (int, num_ssa_names
);
6656 gimple_stmt_iterator si
;
6658 for (si
= gsi_start_phis (bb
); !gsi_end_p (si
); gsi_next (&si
))
6660 gimple phi
= gsi_stmt (si
);
6661 if (!stmt_interesting_for_vrp (phi
))
6663 tree lhs
= PHI_RESULT (phi
);
6664 set_value_range_to_varying (get_value_range (lhs
));
6665 prop_set_simulate_again (phi
, false);
6668 prop_set_simulate_again (phi
, true);
6671 for (si
= gsi_start_bb (bb
); !gsi_end_p (si
); gsi_next (&si
))
6673 gimple stmt
= gsi_stmt (si
);
6675 /* If the statement is a control insn, then we do not
6676 want to avoid simulating the statement once. Failure
6677 to do so means that those edges will never get added. */
6678 if (stmt_ends_bb_p (stmt
))
6679 prop_set_simulate_again (stmt
, true);
6680 else if (!stmt_interesting_for_vrp (stmt
))
6684 FOR_EACH_SSA_TREE_OPERAND (def
, stmt
, i
, SSA_OP_DEF
)
6685 set_value_range_to_varying (get_value_range (def
));
6686 prop_set_simulate_again (stmt
, false);
6689 prop_set_simulate_again (stmt
, true);
6694 /* Return the singleton value-range for NAME or NAME. */
6697 vrp_valueize (tree name
)
6699 if (TREE_CODE (name
) == SSA_NAME
)
6701 value_range_t
*vr
= get_value_range (name
);
6702 if (vr
->type
== VR_RANGE
6703 && (vr
->min
== vr
->max
6704 || operand_equal_p (vr
->min
, vr
->max
, 0)))
6710 /* Visit assignment STMT. If it produces an interesting range, record
6711 the SSA name in *OUTPUT_P. */
6713 static enum ssa_prop_result
6714 vrp_visit_assignment_or_call (gimple stmt
, tree
*output_p
)
6718 enum gimple_code code
= gimple_code (stmt
);
6719 lhs
= gimple_get_lhs (stmt
);
6721 /* We only keep track of ranges in integral and pointer types. */
6722 if (TREE_CODE (lhs
) == SSA_NAME
6723 && ((INTEGRAL_TYPE_P (TREE_TYPE (lhs
))
6724 /* It is valid to have NULL MIN/MAX values on a type. See
6725 build_range_type. */
6726 && TYPE_MIN_VALUE (TREE_TYPE (lhs
))
6727 && TYPE_MAX_VALUE (TREE_TYPE (lhs
)))
6728 || POINTER_TYPE_P (TREE_TYPE (lhs
))))
6730 value_range_t new_vr
= VR_INITIALIZER
;
6732 /* Try folding the statement to a constant first. */
6733 tree tem
= gimple_fold_stmt_to_constant (stmt
, vrp_valueize
);
6734 if (tem
&& !is_overflow_infinity (tem
))
6735 set_value_range (&new_vr
, VR_RANGE
, tem
, tem
, NULL
);
6736 /* Then dispatch to value-range extracting functions. */
6737 else if (code
== GIMPLE_CALL
)
6738 extract_range_basic (&new_vr
, stmt
);
6740 extract_range_from_assignment (&new_vr
, stmt
);
6742 if (update_value_range (lhs
, &new_vr
))
6746 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6748 fprintf (dump_file
, "Found new range for ");
6749 print_generic_expr (dump_file
, lhs
, 0);
6750 fprintf (dump_file
, ": ");
6751 dump_value_range (dump_file
, &new_vr
);
6752 fprintf (dump_file
, "\n\n");
6755 if (new_vr
.type
== VR_VARYING
)
6756 return SSA_PROP_VARYING
;
6758 return SSA_PROP_INTERESTING
;
6761 return SSA_PROP_NOT_INTERESTING
;
6764 /* Every other statement produces no useful ranges. */
6765 FOR_EACH_SSA_TREE_OPERAND (def
, stmt
, iter
, SSA_OP_DEF
)
6766 set_value_range_to_varying (get_value_range (def
));
6768 return SSA_PROP_VARYING
;
6771 /* Helper that gets the value range of the SSA_NAME with version I
6772 or a symbolic range containing the SSA_NAME only if the value range
6773 is varying or undefined. */
6775 static inline value_range_t
6776 get_vr_for_comparison (int i
)
6778 value_range_t vr
= *get_value_range (ssa_name (i
));
6780 /* If name N_i does not have a valid range, use N_i as its own
6781 range. This allows us to compare against names that may
6782 have N_i in their ranges. */
6783 if (vr
.type
== VR_VARYING
|| vr
.type
== VR_UNDEFINED
)
6786 vr
.min
= ssa_name (i
);
6787 vr
.max
= ssa_name (i
);
6793 /* Compare all the value ranges for names equivalent to VAR with VAL
6794 using comparison code COMP. Return the same value returned by
6795 compare_range_with_value, including the setting of
6796 *STRICT_OVERFLOW_P. */
6799 compare_name_with_value (enum tree_code comp
, tree var
, tree val
,
6800 bool *strict_overflow_p
)
6806 int used_strict_overflow
;
6808 value_range_t equiv_vr
;
6810 /* Get the set of equivalences for VAR. */
6811 e
= get_value_range (var
)->equiv
;
6813 /* Start at -1. Set it to 0 if we do a comparison without relying
6814 on overflow, or 1 if all comparisons rely on overflow. */
6815 used_strict_overflow
= -1;
6817 /* Compare vars' value range with val. */
6818 equiv_vr
= get_vr_for_comparison (SSA_NAME_VERSION (var
));
6820 retval
= compare_range_with_value (comp
, &equiv_vr
, val
, &sop
);
6822 used_strict_overflow
= sop
? 1 : 0;
6824 /* If the equiv set is empty we have done all work we need to do. */
6828 && used_strict_overflow
> 0)
6829 *strict_overflow_p
= true;
6833 EXECUTE_IF_SET_IN_BITMAP (e
, 0, i
, bi
)
6835 equiv_vr
= get_vr_for_comparison (i
);
6837 t
= compare_range_with_value (comp
, &equiv_vr
, val
, &sop
);
6840 /* If we get different answers from different members
6841 of the equivalence set this check must be in a dead
6842 code region. Folding it to a trap representation
6843 would be correct here. For now just return don't-know. */
6853 used_strict_overflow
= 0;
6854 else if (used_strict_overflow
< 0)
6855 used_strict_overflow
= 1;
6860 && used_strict_overflow
> 0)
6861 *strict_overflow_p
= true;
6867 /* Given a comparison code COMP and names N1 and N2, compare all the
6868 ranges equivalent to N1 against all the ranges equivalent to N2
6869 to determine the value of N1 COMP N2. Return the same value
6870 returned by compare_ranges. Set *STRICT_OVERFLOW_P to indicate
6871 whether we relied on an overflow infinity in the comparison. */
6875 compare_names (enum tree_code comp
, tree n1
, tree n2
,
6876 bool *strict_overflow_p
)
6880 bitmap_iterator bi1
, bi2
;
6882 int used_strict_overflow
;
6883 static bitmap_obstack
*s_obstack
= NULL
;
6884 static bitmap s_e1
= NULL
, s_e2
= NULL
;
6886 /* Compare the ranges of every name equivalent to N1 against the
6887 ranges of every name equivalent to N2. */
6888 e1
= get_value_range (n1
)->equiv
;
6889 e2
= get_value_range (n2
)->equiv
;
6891 /* Use the fake bitmaps if e1 or e2 are not available. */
6892 if (s_obstack
== NULL
)
6894 s_obstack
= XNEW (bitmap_obstack
);
6895 bitmap_obstack_initialize (s_obstack
);
6896 s_e1
= BITMAP_ALLOC (s_obstack
);
6897 s_e2
= BITMAP_ALLOC (s_obstack
);
6904 /* Add N1 and N2 to their own set of equivalences to avoid
6905 duplicating the body of the loop just to check N1 and N2
6907 bitmap_set_bit (e1
, SSA_NAME_VERSION (n1
));
6908 bitmap_set_bit (e2
, SSA_NAME_VERSION (n2
));
6910 /* If the equivalence sets have a common intersection, then the two
6911 names can be compared without checking their ranges. */
6912 if (bitmap_intersect_p (e1
, e2
))
6914 bitmap_clear_bit (e1
, SSA_NAME_VERSION (n1
));
6915 bitmap_clear_bit (e2
, SSA_NAME_VERSION (n2
));
6917 return (comp
== EQ_EXPR
|| comp
== GE_EXPR
|| comp
== LE_EXPR
)
6919 : boolean_false_node
;
6922 /* Start at -1. Set it to 0 if we do a comparison without relying
6923 on overflow, or 1 if all comparisons rely on overflow. */
6924 used_strict_overflow
= -1;
6926 /* Otherwise, compare all the equivalent ranges. First, add N1 and
6927 N2 to their own set of equivalences to avoid duplicating the body
6928 of the loop just to check N1 and N2 ranges. */
6929 EXECUTE_IF_SET_IN_BITMAP (e1
, 0, i1
, bi1
)
6931 value_range_t vr1
= get_vr_for_comparison (i1
);
6933 t
= retval
= NULL_TREE
;
6934 EXECUTE_IF_SET_IN_BITMAP (e2
, 0, i2
, bi2
)
6938 value_range_t vr2
= get_vr_for_comparison (i2
);
6940 t
= compare_ranges (comp
, &vr1
, &vr2
, &sop
);
6943 /* If we get different answers from different members
6944 of the equivalence set this check must be in a dead
6945 code region. Folding it to a trap representation
6946 would be correct here. For now just return don't-know. */
6950 bitmap_clear_bit (e1
, SSA_NAME_VERSION (n1
));
6951 bitmap_clear_bit (e2
, SSA_NAME_VERSION (n2
));
6957 used_strict_overflow
= 0;
6958 else if (used_strict_overflow
< 0)
6959 used_strict_overflow
= 1;
6965 bitmap_clear_bit (e1
, SSA_NAME_VERSION (n1
));
6966 bitmap_clear_bit (e2
, SSA_NAME_VERSION (n2
));
6967 if (used_strict_overflow
> 0)
6968 *strict_overflow_p
= true;
6973 /* None of the equivalent ranges are useful in computing this
6975 bitmap_clear_bit (e1
, SSA_NAME_VERSION (n1
));
6976 bitmap_clear_bit (e2
, SSA_NAME_VERSION (n2
));
6980 /* Helper function for vrp_evaluate_conditional_warnv. */
6983 vrp_evaluate_conditional_warnv_with_ops_using_ranges (enum tree_code code
,
6985 bool * strict_overflow_p
)
6987 value_range_t
*vr0
, *vr1
;
6989 vr0
= (TREE_CODE (op0
) == SSA_NAME
) ? get_value_range (op0
) : NULL
;
6990 vr1
= (TREE_CODE (op1
) == SSA_NAME
) ? get_value_range (op1
) : NULL
;
6993 return compare_ranges (code
, vr0
, vr1
, strict_overflow_p
);
6994 else if (vr0
&& vr1
== NULL
)
6995 return compare_range_with_value (code
, vr0
, op1
, strict_overflow_p
);
6996 else if (vr0
== NULL
&& vr1
)
6997 return (compare_range_with_value
6998 (swap_tree_comparison (code
), vr1
, op0
, strict_overflow_p
));
7002 /* Helper function for vrp_evaluate_conditional_warnv. */
7005 vrp_evaluate_conditional_warnv_with_ops (enum tree_code code
, tree op0
,
7006 tree op1
, bool use_equiv_p
,
7007 bool *strict_overflow_p
, bool *only_ranges
)
7011 *only_ranges
= true;
7013 /* We only deal with integral and pointer types. */
7014 if (!INTEGRAL_TYPE_P (TREE_TYPE (op0
))
7015 && !POINTER_TYPE_P (TREE_TYPE (op0
)))
7021 && (ret
= vrp_evaluate_conditional_warnv_with_ops_using_ranges
7022 (code
, op0
, op1
, strict_overflow_p
)))
7024 *only_ranges
= false;
7025 if (TREE_CODE (op0
) == SSA_NAME
&& TREE_CODE (op1
) == SSA_NAME
)
7026 return compare_names (code
, op0
, op1
, strict_overflow_p
);
7027 else if (TREE_CODE (op0
) == SSA_NAME
)
7028 return compare_name_with_value (code
, op0
, op1
, strict_overflow_p
);
7029 else if (TREE_CODE (op1
) == SSA_NAME
)
7030 return (compare_name_with_value
7031 (swap_tree_comparison (code
), op1
, op0
, strict_overflow_p
));
7034 return vrp_evaluate_conditional_warnv_with_ops_using_ranges (code
, op0
, op1
,
7039 /* Given (CODE OP0 OP1) within STMT, try to simplify it based on value range
7040 information. Return NULL if the conditional can not be evaluated.
7041 The ranges of all the names equivalent with the operands in COND
7042 will be used when trying to compute the value. If the result is
7043 based on undefined signed overflow, issue a warning if
7047 vrp_evaluate_conditional (enum tree_code code
, tree op0
, tree op1
, gimple stmt
)
7053 /* Some passes and foldings leak constants with overflow flag set
7054 into the IL. Avoid doing wrong things with these and bail out. */
7055 if ((TREE_CODE (op0
) == INTEGER_CST
7056 && TREE_OVERFLOW (op0
))
7057 || (TREE_CODE (op1
) == INTEGER_CST
7058 && TREE_OVERFLOW (op1
)))
7062 ret
= vrp_evaluate_conditional_warnv_with_ops (code
, op0
, op1
, true, &sop
,
7067 enum warn_strict_overflow_code wc
;
7068 const char* warnmsg
;
7070 if (is_gimple_min_invariant (ret
))
7072 wc
= WARN_STRICT_OVERFLOW_CONDITIONAL
;
7073 warnmsg
= G_("assuming signed overflow does not occur when "
7074 "simplifying conditional to constant");
7078 wc
= WARN_STRICT_OVERFLOW_COMPARISON
;
7079 warnmsg
= G_("assuming signed overflow does not occur when "
7080 "simplifying conditional");
7083 if (issue_strict_overflow_warning (wc
))
7085 location_t location
;
7087 if (!gimple_has_location (stmt
))
7088 location
= input_location
;
7090 location
= gimple_location (stmt
);
7091 warning_at (location
, OPT_Wstrict_overflow
, "%s", warnmsg
);
7095 if (warn_type_limits
7096 && ret
&& only_ranges
7097 && TREE_CODE_CLASS (code
) == tcc_comparison
7098 && TREE_CODE (op0
) == SSA_NAME
)
7100 /* If the comparison is being folded and the operand on the LHS
7101 is being compared against a constant value that is outside of
7102 the natural range of OP0's type, then the predicate will
7103 always fold regardless of the value of OP0. If -Wtype-limits
7104 was specified, emit a warning. */
7105 tree type
= TREE_TYPE (op0
);
7106 value_range_t
*vr0
= get_value_range (op0
);
7108 if (vr0
->type
!= VR_VARYING
7109 && INTEGRAL_TYPE_P (type
)
7110 && vrp_val_is_min (vr0
->min
)
7111 && vrp_val_is_max (vr0
->max
)
7112 && is_gimple_min_invariant (op1
))
7114 location_t location
;
7116 if (!gimple_has_location (stmt
))
7117 location
= input_location
;
7119 location
= gimple_location (stmt
);
7121 warning_at (location
, OPT_Wtype_limits
,
7123 ? G_("comparison always false "
7124 "due to limited range of data type")
7125 : G_("comparison always true "
7126 "due to limited range of data type"));
7134 /* Visit conditional statement STMT. If we can determine which edge
7135 will be taken out of STMT's basic block, record it in
7136 *TAKEN_EDGE_P and return SSA_PROP_INTERESTING. Otherwise, return
7137 SSA_PROP_VARYING. */
7139 static enum ssa_prop_result
7140 vrp_visit_cond_stmt (gimple stmt
, edge
*taken_edge_p
)
7145 *taken_edge_p
= NULL
;
7147 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7152 fprintf (dump_file
, "\nVisiting conditional with predicate: ");
7153 print_gimple_stmt (dump_file
, stmt
, 0, 0);
7154 fprintf (dump_file
, "\nWith known ranges\n");
7156 FOR_EACH_SSA_TREE_OPERAND (use
, stmt
, i
, SSA_OP_USE
)
7158 fprintf (dump_file
, "\t");
7159 print_generic_expr (dump_file
, use
, 0);
7160 fprintf (dump_file
, ": ");
7161 dump_value_range (dump_file
, vr_value
[SSA_NAME_VERSION (use
)]);
7164 fprintf (dump_file
, "\n");
7167 /* Compute the value of the predicate COND by checking the known
7168 ranges of each of its operands.
7170 Note that we cannot evaluate all the equivalent ranges here
7171 because those ranges may not yet be final and with the current
7172 propagation strategy, we cannot determine when the value ranges
7173 of the names in the equivalence set have changed.
7175 For instance, given the following code fragment
7179 i_14 = ASSERT_EXPR <i_5, i_5 != 0>
7183 Assume that on the first visit to i_14, i_5 has the temporary
7184 range [8, 8] because the second argument to the PHI function is
7185 not yet executable. We derive the range ~[0, 0] for i_14 and the
7186 equivalence set { i_5 }. So, when we visit 'if (i_14 == 1)' for
7187 the first time, since i_14 is equivalent to the range [8, 8], we
7188 determine that the predicate is always false.
7190 On the next round of propagation, i_13 is determined to be
7191 VARYING, which causes i_5 to drop down to VARYING. So, another
7192 visit to i_14 is scheduled. In this second visit, we compute the
7193 exact same range and equivalence set for i_14, namely ~[0, 0] and
7194 { i_5 }. But we did not have the previous range for i_5
7195 registered, so vrp_visit_assignment thinks that the range for
7196 i_14 has not changed. Therefore, the predicate 'if (i_14 == 1)'
7197 is not visited again, which stops propagation from visiting
7198 statements in the THEN clause of that if().
7200 To properly fix this we would need to keep the previous range
7201 value for the names in the equivalence set. This way we would've
7202 discovered that from one visit to the other i_5 changed from
7203 range [8, 8] to VR_VARYING.
7205 However, fixing this apparent limitation may not be worth the
7206 additional checking. Testing on several code bases (GCC, DLV,
7207 MICO, TRAMP3D and SPEC2000) showed that doing this results in
7208 4 more predicates folded in SPEC. */
7211 val
= vrp_evaluate_conditional_warnv_with_ops (gimple_cond_code (stmt
),
7212 gimple_cond_lhs (stmt
),
7213 gimple_cond_rhs (stmt
),
7218 *taken_edge_p
= find_taken_edge (gimple_bb (stmt
), val
);
7221 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7223 "\nIgnoring predicate evaluation because "
7224 "it assumes that signed overflow is undefined");
7229 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7231 fprintf (dump_file
, "\nPredicate evaluates to: ");
7232 if (val
== NULL_TREE
)
7233 fprintf (dump_file
, "DON'T KNOW\n");
7235 print_generic_stmt (dump_file
, val
, 0);
7238 return (*taken_edge_p
) ? SSA_PROP_INTERESTING
: SSA_PROP_VARYING
;
7241 /* Searches the case label vector VEC for the index *IDX of the CASE_LABEL
7242 that includes the value VAL. The search is restricted to the range
7243 [START_IDX, n - 1] where n is the size of VEC.
7245 If there is a CASE_LABEL for VAL, its index is placed in IDX and true is
7248 If there is no CASE_LABEL for VAL and there is one that is larger than VAL,
7249 it is placed in IDX and false is returned.
7251 If VAL is larger than any CASE_LABEL, n is placed on IDX and false is
7255 find_case_label_index (gimple stmt
, size_t start_idx
, tree val
, size_t *idx
)
7257 size_t n
= gimple_switch_num_labels (stmt
);
7260 /* Find case label for minimum of the value range or the next one.
7261 At each iteration we are searching in [low, high - 1]. */
7263 for (low
= start_idx
, high
= n
; high
!= low
; )
7267 /* Note that i != high, so we never ask for n. */
7268 size_t i
= (high
+ low
) / 2;
7269 t
= gimple_switch_label (stmt
, i
);
7271 /* Cache the result of comparing CASE_LOW and val. */
7272 cmp
= tree_int_cst_compare (CASE_LOW (t
), val
);
7276 /* Ranges cannot be empty. */
7285 if (CASE_HIGH (t
) != NULL
7286 && tree_int_cst_compare (CASE_HIGH (t
), val
) >= 0)
7298 /* Searches the case label vector VEC for the range of CASE_LABELs that is used
7299 for values between MIN and MAX. The first index is placed in MIN_IDX. The
7300 last index is placed in MAX_IDX. If the range of CASE_LABELs is empty
7301 then MAX_IDX < MIN_IDX.
7302 Returns true if the default label is not needed. */
7305 find_case_label_range (gimple stmt
, tree min
, tree max
, size_t *min_idx
,
7309 bool min_take_default
= !find_case_label_index (stmt
, 1, min
, &i
);
7310 bool max_take_default
= !find_case_label_index (stmt
, i
, max
, &j
);
7314 && max_take_default
)
7316 /* Only the default case label reached.
7317 Return an empty range. */
7324 bool take_default
= min_take_default
|| max_take_default
;
7328 if (max_take_default
)
7331 /* If the case label range is continuous, we do not need
7332 the default case label. Verify that. */
7333 high
= CASE_LOW (gimple_switch_label (stmt
, i
));
7334 if (CASE_HIGH (gimple_switch_label (stmt
, i
)))
7335 high
= CASE_HIGH (gimple_switch_label (stmt
, i
));
7336 for (k
= i
+ 1; k
<= j
; ++k
)
7338 low
= CASE_LOW (gimple_switch_label (stmt
, k
));
7339 if (!integer_onep (int_const_binop (MINUS_EXPR
, low
, high
)))
7341 take_default
= true;
7345 if (CASE_HIGH (gimple_switch_label (stmt
, k
)))
7346 high
= CASE_HIGH (gimple_switch_label (stmt
, k
));
7351 return !take_default
;
7355 /* Searches the case label vector VEC for the ranges of CASE_LABELs that are
7356 used in range VR. The indices are placed in MIN_IDX1, MAX_IDX, MIN_IDX2 and
7357 MAX_IDX2. If the ranges of CASE_LABELs are empty then MAX_IDX1 < MIN_IDX1.
7358 Returns true if the default label is not needed. */
7361 find_case_label_ranges (gimple stmt
, value_range_t
*vr
, size_t *min_idx1
,
7362 size_t *max_idx1
, size_t *min_idx2
,
7366 unsigned int n
= gimple_switch_num_labels (stmt
);
7368 tree case_low
, case_high
;
7369 tree min
= vr
->min
, max
= vr
->max
;
7371 gcc_checking_assert (vr
->type
== VR_RANGE
|| vr
->type
== VR_ANTI_RANGE
);
7373 take_default
= !find_case_label_range (stmt
, min
, max
, &i
, &j
);
7375 /* Set second range to emtpy. */
7379 if (vr
->type
== VR_RANGE
)
7383 return !take_default
;
7386 /* Set first range to all case labels. */
7393 /* Make sure all the values of case labels [i , j] are contained in
7394 range [MIN, MAX]. */
7395 case_low
= CASE_LOW (gimple_switch_label (stmt
, i
));
7396 case_high
= CASE_HIGH (gimple_switch_label (stmt
, j
));
7397 if (tree_int_cst_compare (case_low
, min
) < 0)
7399 if (case_high
!= NULL_TREE
7400 && tree_int_cst_compare (max
, case_high
) < 0)
7406 /* If the range spans case labels [i, j], the corresponding anti-range spans
7407 the labels [1, i - 1] and [j + 1, n - 1]. */
7433 /* Visit switch statement STMT. If we can determine which edge
7434 will be taken out of STMT's basic block, record it in
7435 *TAKEN_EDGE_P and return SSA_PROP_INTERESTING. Otherwise, return
7436 SSA_PROP_VARYING. */
7438 static enum ssa_prop_result
7439 vrp_visit_switch_stmt (gimple stmt
, edge
*taken_edge_p
)
7443 size_t i
= 0, j
= 0, k
, l
;
7446 *taken_edge_p
= NULL
;
7447 op
= gimple_switch_index (stmt
);
7448 if (TREE_CODE (op
) != SSA_NAME
)
7449 return SSA_PROP_VARYING
;
7451 vr
= get_value_range (op
);
7452 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7454 fprintf (dump_file
, "\nVisiting switch expression with operand ");
7455 print_generic_expr (dump_file
, op
, 0);
7456 fprintf (dump_file
, " with known range ");
7457 dump_value_range (dump_file
, vr
);
7458 fprintf (dump_file
, "\n");
7461 if ((vr
->type
!= VR_RANGE
7462 && vr
->type
!= VR_ANTI_RANGE
)
7463 || symbolic_range_p (vr
))
7464 return SSA_PROP_VARYING
;
7466 /* Find the single edge that is taken from the switch expression. */
7467 take_default
= !find_case_label_ranges (stmt
, vr
, &i
, &j
, &k
, &l
);
7469 /* Check if the range spans no CASE_LABEL. If so, we only reach the default
7473 gcc_assert (take_default
);
7474 val
= gimple_switch_default_label (stmt
);
7478 /* Check if labels with index i to j and maybe the default label
7479 are all reaching the same label. */
7481 val
= gimple_switch_label (stmt
, i
);
7483 && CASE_LABEL (gimple_switch_default_label (stmt
))
7484 != CASE_LABEL (val
))
7486 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7487 fprintf (dump_file
, " not a single destination for this "
7489 return SSA_PROP_VARYING
;
7491 for (++i
; i
<= j
; ++i
)
7493 if (CASE_LABEL (gimple_switch_label (stmt
, i
)) != CASE_LABEL (val
))
7495 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7496 fprintf (dump_file
, " not a single destination for this "
7498 return SSA_PROP_VARYING
;
7503 if (CASE_LABEL (gimple_switch_label (stmt
, k
)) != CASE_LABEL (val
))
7505 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7506 fprintf (dump_file
, " not a single destination for this "
7508 return SSA_PROP_VARYING
;
7513 *taken_edge_p
= find_edge (gimple_bb (stmt
),
7514 label_to_block (CASE_LABEL (val
)));
7516 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7518 fprintf (dump_file
, " will take edge to ");
7519 print_generic_stmt (dump_file
, CASE_LABEL (val
), 0);
7522 return SSA_PROP_INTERESTING
;
7526 /* Evaluate statement STMT. If the statement produces a useful range,
7527 return SSA_PROP_INTERESTING and record the SSA name with the
7528 interesting range into *OUTPUT_P.
7530 If STMT is a conditional branch and we can determine its truth
7531 value, the taken edge is recorded in *TAKEN_EDGE_P.
7533 If STMT produces a varying value, return SSA_PROP_VARYING. */
7535 static enum ssa_prop_result
7536 vrp_visit_stmt (gimple stmt
, edge
*taken_edge_p
, tree
*output_p
)
7541 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7543 fprintf (dump_file
, "\nVisiting statement:\n");
7544 print_gimple_stmt (dump_file
, stmt
, 0, dump_flags
);
7545 fprintf (dump_file
, "\n");
7548 if (!stmt_interesting_for_vrp (stmt
))
7549 gcc_assert (stmt_ends_bb_p (stmt
));
7550 else if (is_gimple_assign (stmt
) || is_gimple_call (stmt
))
7551 return vrp_visit_assignment_or_call (stmt
, output_p
);
7552 else if (gimple_code (stmt
) == GIMPLE_COND
)
7553 return vrp_visit_cond_stmt (stmt
, taken_edge_p
);
7554 else if (gimple_code (stmt
) == GIMPLE_SWITCH
)
7555 return vrp_visit_switch_stmt (stmt
, taken_edge_p
);
7557 /* All other statements produce nothing of interest for VRP, so mark
7558 their outputs varying and prevent further simulation. */
7559 FOR_EACH_SSA_TREE_OPERAND (def
, stmt
, iter
, SSA_OP_DEF
)
7560 set_value_range_to_varying (get_value_range (def
));
7562 return SSA_PROP_VARYING
;
7565 /* Union the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
7566 { VR1TYPE, VR0MIN, VR0MAX } and store the result
7567 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
7568 possible such range. The resulting range is not canonicalized. */
7571 union_ranges (enum value_range_type
*vr0type
,
7572 tree
*vr0min
, tree
*vr0max
,
7573 enum value_range_type vr1type
,
7574 tree vr1min
, tree vr1max
)
7576 bool mineq
= operand_equal_p (*vr0min
, vr1min
, 0);
7577 bool maxeq
= operand_equal_p (*vr0max
, vr1max
, 0);
7579 /* [] is vr0, () is vr1 in the following classification comments. */
7583 if (*vr0type
== vr1type
)
7584 /* Nothing to do for equal ranges. */
7586 else if ((*vr0type
== VR_RANGE
7587 && vr1type
== VR_ANTI_RANGE
)
7588 || (*vr0type
== VR_ANTI_RANGE
7589 && vr1type
== VR_RANGE
))
7591 /* For anti-range with range union the result is varying. */
7597 else if (operand_less_p (*vr0max
, vr1min
) == 1
7598 || operand_less_p (vr1max
, *vr0min
) == 1)
7600 /* [ ] ( ) or ( ) [ ]
7601 If the ranges have an empty intersection, result of the union
7602 operation is the anti-range or if both are anti-ranges
7604 if (*vr0type
== VR_ANTI_RANGE
7605 && vr1type
== VR_ANTI_RANGE
)
7607 else if (*vr0type
== VR_ANTI_RANGE
7608 && vr1type
== VR_RANGE
)
7610 else if (*vr0type
== VR_RANGE
7611 && vr1type
== VR_ANTI_RANGE
)
7617 else if (*vr0type
== VR_RANGE
7618 && vr1type
== VR_RANGE
)
7620 /* The result is the convex hull of both ranges. */
7621 if (operand_less_p (*vr0max
, vr1min
) == 1)
7623 /* If the result can be an anti-range, create one. */
7624 if (TREE_CODE (*vr0max
) == INTEGER_CST
7625 && TREE_CODE (vr1min
) == INTEGER_CST
7626 && vrp_val_is_min (*vr0min
)
7627 && vrp_val_is_max (vr1max
))
7629 tree min
= int_const_binop (PLUS_EXPR
,
7630 *vr0max
, integer_one_node
);
7631 tree max
= int_const_binop (MINUS_EXPR
,
7632 vr1min
, integer_one_node
);
7633 if (!operand_less_p (max
, min
))
7635 *vr0type
= VR_ANTI_RANGE
;
7647 /* If the result can be an anti-range, create one. */
7648 if (TREE_CODE (vr1max
) == INTEGER_CST
7649 && TREE_CODE (*vr0min
) == INTEGER_CST
7650 && vrp_val_is_min (vr1min
)
7651 && vrp_val_is_max (*vr0max
))
7653 tree min
= int_const_binop (PLUS_EXPR
,
7654 vr1max
, integer_one_node
);
7655 tree max
= int_const_binop (MINUS_EXPR
,
7656 *vr0min
, integer_one_node
);
7657 if (!operand_less_p (max
, min
))
7659 *vr0type
= VR_ANTI_RANGE
;
7673 else if ((maxeq
|| operand_less_p (vr1max
, *vr0max
) == 1)
7674 && (mineq
|| operand_less_p (*vr0min
, vr1min
) == 1))
7676 /* [ ( ) ] or [( ) ] or [ ( )] */
7677 if (*vr0type
== VR_RANGE
7678 && vr1type
== VR_RANGE
)
7680 else if (*vr0type
== VR_ANTI_RANGE
7681 && vr1type
== VR_ANTI_RANGE
)
7687 else if (*vr0type
== VR_ANTI_RANGE
7688 && vr1type
== VR_RANGE
)
7690 /* Arbitrarily choose the right or left gap. */
7691 if (!mineq
&& TREE_CODE (vr1min
) == INTEGER_CST
)
7692 *vr0max
= int_const_binop (MINUS_EXPR
, vr1min
, integer_one_node
);
7693 else if (!maxeq
&& TREE_CODE (vr1max
) == INTEGER_CST
)
7694 *vr0min
= int_const_binop (PLUS_EXPR
, vr1max
, integer_one_node
);
7698 else if (*vr0type
== VR_RANGE
7699 && vr1type
== VR_ANTI_RANGE
)
7700 /* The result covers everything. */
7705 else if ((maxeq
|| operand_less_p (*vr0max
, vr1max
) == 1)
7706 && (mineq
|| operand_less_p (vr1min
, *vr0min
) == 1))
7708 /* ( [ ] ) or ([ ] ) or ( [ ]) */
7709 if (*vr0type
== VR_RANGE
7710 && vr1type
== VR_RANGE
)
7716 else if (*vr0type
== VR_ANTI_RANGE
7717 && vr1type
== VR_ANTI_RANGE
)
7719 else if (*vr0type
== VR_RANGE
7720 && vr1type
== VR_ANTI_RANGE
)
7722 *vr0type
= VR_ANTI_RANGE
;
7723 if (!mineq
&& TREE_CODE (*vr0min
) == INTEGER_CST
)
7725 *vr0max
= int_const_binop (MINUS_EXPR
, *vr0min
, integer_one_node
);
7728 else if (!maxeq
&& TREE_CODE (*vr0max
) == INTEGER_CST
)
7730 *vr0min
= int_const_binop (PLUS_EXPR
, *vr0max
, integer_one_node
);
7736 else if (*vr0type
== VR_ANTI_RANGE
7737 && vr1type
== VR_RANGE
)
7738 /* The result covers everything. */
7743 else if ((operand_less_p (vr1min
, *vr0max
) == 1
7744 || operand_equal_p (vr1min
, *vr0max
, 0))
7745 && operand_less_p (*vr0min
, vr1min
) == 1)
7747 /* [ ( ] ) or [ ]( ) */
7748 if (*vr0type
== VR_RANGE
7749 && vr1type
== VR_RANGE
)
7751 else if (*vr0type
== VR_ANTI_RANGE
7752 && vr1type
== VR_ANTI_RANGE
)
7754 else if (*vr0type
== VR_ANTI_RANGE
7755 && vr1type
== VR_RANGE
)
7757 if (TREE_CODE (vr1min
) == INTEGER_CST
)
7758 *vr0max
= int_const_binop (MINUS_EXPR
, vr1min
, integer_one_node
);
7762 else if (*vr0type
== VR_RANGE
7763 && vr1type
== VR_ANTI_RANGE
)
7765 if (TREE_CODE (*vr0max
) == INTEGER_CST
)
7768 *vr0min
= int_const_binop (PLUS_EXPR
, *vr0max
, integer_one_node
);
7777 else if ((operand_less_p (*vr0min
, vr1max
) == 1
7778 || operand_equal_p (*vr0min
, vr1max
, 0))
7779 && operand_less_p (vr1min
, *vr0min
) == 1)
7781 /* ( [ ) ] or ( )[ ] */
7782 if (*vr0type
== VR_RANGE
7783 && vr1type
== VR_RANGE
)
7785 else if (*vr0type
== VR_ANTI_RANGE
7786 && vr1type
== VR_ANTI_RANGE
)
7788 else if (*vr0type
== VR_ANTI_RANGE
7789 && vr1type
== VR_RANGE
)
7791 if (TREE_CODE (vr1max
) == INTEGER_CST
)
7792 *vr0min
= int_const_binop (PLUS_EXPR
, vr1max
, integer_one_node
);
7796 else if (*vr0type
== VR_RANGE
7797 && vr1type
== VR_ANTI_RANGE
)
7799 if (TREE_CODE (*vr0min
) == INTEGER_CST
)
7803 *vr0max
= int_const_binop (MINUS_EXPR
, *vr0min
, integer_one_node
);
7817 *vr0type
= VR_VARYING
;
7818 *vr0min
= NULL_TREE
;
7819 *vr0max
= NULL_TREE
;
7822 /* Intersect the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
7823 { VR1TYPE, VR0MIN, VR0MAX } and store the result
7824 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
7825 possible such range. The resulting range is not canonicalized. */
7828 intersect_ranges (enum value_range_type
*vr0type
,
7829 tree
*vr0min
, tree
*vr0max
,
7830 enum value_range_type vr1type
,
7831 tree vr1min
, tree vr1max
)
7833 bool mineq
= operand_equal_p (*vr0min
, vr1min
, 0);
7834 bool maxeq
= operand_equal_p (*vr0max
, vr1max
, 0);
7836 /* [] is vr0, () is vr1 in the following classification comments. */
7840 if (*vr0type
== vr1type
)
7841 /* Nothing to do for equal ranges. */
7843 else if ((*vr0type
== VR_RANGE
7844 && vr1type
== VR_ANTI_RANGE
)
7845 || (*vr0type
== VR_ANTI_RANGE
7846 && vr1type
== VR_RANGE
))
7848 /* For anti-range with range intersection the result is empty. */
7849 *vr0type
= VR_UNDEFINED
;
7850 *vr0min
= NULL_TREE
;
7851 *vr0max
= NULL_TREE
;
7856 else if (operand_less_p (*vr0max
, vr1min
) == 1
7857 || operand_less_p (vr1max
, *vr0min
) == 1)
7859 /* [ ] ( ) or ( ) [ ]
7860 If the ranges have an empty intersection, the result of the
7861 intersect operation is the range for intersecting an
7862 anti-range with a range or empty when intersecting two ranges. */
7863 if (*vr0type
== VR_RANGE
7864 && vr1type
== VR_ANTI_RANGE
)
7866 else if (*vr0type
== VR_ANTI_RANGE
7867 && vr1type
== VR_RANGE
)
7873 else if (*vr0type
== VR_RANGE
7874 && vr1type
== VR_RANGE
)
7876 *vr0type
= VR_UNDEFINED
;
7877 *vr0min
= NULL_TREE
;
7878 *vr0max
= NULL_TREE
;
7880 else if (*vr0type
== VR_ANTI_RANGE
7881 && vr1type
== VR_ANTI_RANGE
)
7883 /* If the anti-ranges are adjacent to each other merge them. */
7884 if (TREE_CODE (*vr0max
) == INTEGER_CST
7885 && TREE_CODE (vr1min
) == INTEGER_CST
7886 && operand_less_p (*vr0max
, vr1min
) == 1
7887 && integer_onep (int_const_binop (MINUS_EXPR
,
7890 else if (TREE_CODE (vr1max
) == INTEGER_CST
7891 && TREE_CODE (*vr0min
) == INTEGER_CST
7892 && operand_less_p (vr1max
, *vr0min
) == 1
7893 && integer_onep (int_const_binop (MINUS_EXPR
,
7896 /* Else arbitrarily take VR0. */
7899 else if ((maxeq
|| operand_less_p (vr1max
, *vr0max
) == 1)
7900 && (mineq
|| operand_less_p (*vr0min
, vr1min
) == 1))
7902 /* [ ( ) ] or [( ) ] or [ ( )] */
7903 if (*vr0type
== VR_RANGE
7904 && vr1type
== VR_RANGE
)
7906 /* If both are ranges the result is the inner one. */
7911 else if (*vr0type
== VR_RANGE
7912 && vr1type
== VR_ANTI_RANGE
)
7914 /* Choose the right gap if the left one is empty. */
7917 if (TREE_CODE (vr1max
) == INTEGER_CST
)
7918 *vr0min
= int_const_binop (PLUS_EXPR
, vr1max
, integer_one_node
);
7922 /* Choose the left gap if the right one is empty. */
7925 if (TREE_CODE (vr1min
) == INTEGER_CST
)
7926 *vr0max
= int_const_binop (MINUS_EXPR
, vr1min
,
7931 /* Choose the anti-range if the range is effectively varying. */
7932 else if (vrp_val_is_min (*vr0min
)
7933 && vrp_val_is_max (*vr0max
))
7939 /* Else choose the range. */
7941 else if (*vr0type
== VR_ANTI_RANGE
7942 && vr1type
== VR_ANTI_RANGE
)
7943 /* If both are anti-ranges the result is the outer one. */
7945 else if (*vr0type
== VR_ANTI_RANGE
7946 && vr1type
== VR_RANGE
)
7948 /* The intersection is empty. */
7949 *vr0type
= VR_UNDEFINED
;
7950 *vr0min
= NULL_TREE
;
7951 *vr0max
= NULL_TREE
;
7956 else if ((maxeq
|| operand_less_p (*vr0max
, vr1max
) == 1)
7957 && (mineq
|| operand_less_p (vr1min
, *vr0min
) == 1))
7959 /* ( [ ] ) or ([ ] ) or ( [ ]) */
7960 if (*vr0type
== VR_RANGE
7961 && vr1type
== VR_RANGE
)
7962 /* Choose the inner range. */
7964 else if (*vr0type
== VR_ANTI_RANGE
7965 && vr1type
== VR_RANGE
)
7967 /* Choose the right gap if the left is empty. */
7970 *vr0type
= VR_RANGE
;
7971 if (TREE_CODE (*vr0max
) == INTEGER_CST
)
7972 *vr0min
= int_const_binop (PLUS_EXPR
, *vr0max
,
7978 /* Choose the left gap if the right is empty. */
7981 *vr0type
= VR_RANGE
;
7982 if (TREE_CODE (*vr0min
) == INTEGER_CST
)
7983 *vr0max
= int_const_binop (MINUS_EXPR
, *vr0min
,
7989 /* Choose the anti-range if the range is effectively varying. */
7990 else if (vrp_val_is_min (vr1min
)
7991 && vrp_val_is_max (vr1max
))
7993 /* Else choose the range. */
8001 else if (*vr0type
== VR_ANTI_RANGE
8002 && vr1type
== VR_ANTI_RANGE
)
8004 /* If both are anti-ranges the result is the outer one. */
8009 else if (vr1type
== VR_ANTI_RANGE
8010 && *vr0type
== VR_RANGE
)
8012 /* The intersection is empty. */
8013 *vr0type
= VR_UNDEFINED
;
8014 *vr0min
= NULL_TREE
;
8015 *vr0max
= NULL_TREE
;
8020 else if ((operand_less_p (vr1min
, *vr0max
) == 1
8021 || operand_equal_p (vr1min
, *vr0max
, 0))
8022 && operand_less_p (*vr0min
, vr1min
) == 1)
8024 /* [ ( ] ) or [ ]( ) */
8025 if (*vr0type
== VR_ANTI_RANGE
8026 && vr1type
== VR_ANTI_RANGE
)
8028 else if (*vr0type
== VR_RANGE
8029 && vr1type
== VR_RANGE
)
8031 else if (*vr0type
== VR_RANGE
8032 && vr1type
== VR_ANTI_RANGE
)
8034 if (TREE_CODE (vr1min
) == INTEGER_CST
)
8035 *vr0max
= int_const_binop (MINUS_EXPR
, vr1min
,
8040 else if (*vr0type
== VR_ANTI_RANGE
8041 && vr1type
== VR_RANGE
)
8043 *vr0type
= VR_RANGE
;
8044 if (TREE_CODE (*vr0max
) == INTEGER_CST
)
8045 *vr0min
= int_const_binop (PLUS_EXPR
, *vr0max
,
8054 else if ((operand_less_p (*vr0min
, vr1max
) == 1
8055 || operand_equal_p (*vr0min
, vr1max
, 0))
8056 && operand_less_p (vr1min
, *vr0min
) == 1)
8058 /* ( [ ) ] or ( )[ ] */
8059 if (*vr0type
== VR_ANTI_RANGE
8060 && vr1type
== VR_ANTI_RANGE
)
8062 else if (*vr0type
== VR_RANGE
8063 && vr1type
== VR_RANGE
)
8065 else if (*vr0type
== VR_RANGE
8066 && vr1type
== VR_ANTI_RANGE
)
8068 if (TREE_CODE (vr1max
) == INTEGER_CST
)
8069 *vr0min
= int_const_binop (PLUS_EXPR
, vr1max
,
8074 else if (*vr0type
== VR_ANTI_RANGE
8075 && vr1type
== VR_RANGE
)
8077 *vr0type
= VR_RANGE
;
8078 if (TREE_CODE (*vr0min
) == INTEGER_CST
)
8079 *vr0max
= int_const_binop (MINUS_EXPR
, *vr0min
,
8089 /* As a fallback simply use { *VRTYPE, *VR0MIN, *VR0MAX } as
8090 result for the intersection. That's always a conservative
8091 correct estimate. */
8097 /* Intersect the two value-ranges *VR0 and *VR1 and store the result
8098 in *VR0. This may not be the smallest possible such range. */
8101 vrp_intersect_ranges_1 (value_range_t
*vr0
, value_range_t
*vr1
)
8103 value_range_t saved
;
8105 /* If either range is VR_VARYING the other one wins. */
8106 if (vr1
->type
== VR_VARYING
)
8108 if (vr0
->type
== VR_VARYING
)
8110 copy_value_range (vr0
, vr1
);
8114 /* When either range is VR_UNDEFINED the resulting range is
8115 VR_UNDEFINED, too. */
8116 if (vr0
->type
== VR_UNDEFINED
)
8118 if (vr1
->type
== VR_UNDEFINED
)
8120 set_value_range_to_undefined (vr0
);
8124 /* Save the original vr0 so we can return it as conservative intersection
8125 result when our worker turns things to varying. */
8127 intersect_ranges (&vr0
->type
, &vr0
->min
, &vr0
->max
,
8128 vr1
->type
, vr1
->min
, vr1
->max
);
8129 /* Make sure to canonicalize the result though as the inversion of a
8130 VR_RANGE can still be a VR_RANGE. */
8131 set_and_canonicalize_value_range (vr0
, vr0
->type
,
8132 vr0
->min
, vr0
->max
, vr0
->equiv
);
8133 /* If that failed, use the saved original VR0. */
8134 if (vr0
->type
== VR_VARYING
)
8139 /* If the result is VR_UNDEFINED there is no need to mess with
8140 the equivalencies. */
8141 if (vr0
->type
== VR_UNDEFINED
)
8144 /* The resulting set of equivalences for range intersection is the union of
8146 if (vr0
->equiv
&& vr1
->equiv
&& vr0
->equiv
!= vr1
->equiv
)
8147 bitmap_ior_into (vr0
->equiv
, vr1
->equiv
);
8148 else if (vr1
->equiv
&& !vr0
->equiv
)
8149 bitmap_copy (vr0
->equiv
, vr1
->equiv
);
8153 vrp_intersect_ranges (value_range_t
*vr0
, value_range_t
*vr1
)
8155 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8157 fprintf (dump_file
, "Intersecting\n ");
8158 dump_value_range (dump_file
, vr0
);
8159 fprintf (dump_file
, "\nand\n ");
8160 dump_value_range (dump_file
, vr1
);
8161 fprintf (dump_file
, "\n");
8163 vrp_intersect_ranges_1 (vr0
, vr1
);
8164 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8166 fprintf (dump_file
, "to\n ");
8167 dump_value_range (dump_file
, vr0
);
8168 fprintf (dump_file
, "\n");
8172 /* Meet operation for value ranges. Given two value ranges VR0 and
8173 VR1, store in VR0 a range that contains both VR0 and VR1. This
8174 may not be the smallest possible such range. */
8177 vrp_meet_1 (value_range_t
*vr0
, value_range_t
*vr1
)
8179 value_range_t saved
;
8181 if (vr0
->type
== VR_UNDEFINED
)
8183 set_value_range (vr0
, vr1
->type
, vr1
->min
, vr1
->max
, vr1
->equiv
);
8187 if (vr1
->type
== VR_UNDEFINED
)
8189 /* VR0 already has the resulting range. */
8193 if (vr0
->type
== VR_VARYING
)
8195 /* Nothing to do. VR0 already has the resulting range. */
8199 if (vr1
->type
== VR_VARYING
)
8201 set_value_range_to_varying (vr0
);
8206 union_ranges (&vr0
->type
, &vr0
->min
, &vr0
->max
,
8207 vr1
->type
, vr1
->min
, vr1
->max
);
8208 if (vr0
->type
== VR_VARYING
)
8210 /* Failed to find an efficient meet. Before giving up and setting
8211 the result to VARYING, see if we can at least derive a useful
8212 anti-range. FIXME, all this nonsense about distinguishing
8213 anti-ranges from ranges is necessary because of the odd
8214 semantics of range_includes_zero_p and friends. */
8215 if (((saved
.type
== VR_RANGE
8216 && range_includes_zero_p (saved
.min
, saved
.max
) == 0)
8217 || (saved
.type
== VR_ANTI_RANGE
8218 && range_includes_zero_p (saved
.min
, saved
.max
) == 1))
8219 && ((vr1
->type
== VR_RANGE
8220 && range_includes_zero_p (vr1
->min
, vr1
->max
) == 0)
8221 || (vr1
->type
== VR_ANTI_RANGE
8222 && range_includes_zero_p (vr1
->min
, vr1
->max
) == 1)))
8224 set_value_range_to_nonnull (vr0
, TREE_TYPE (saved
.min
));
8226 /* Since this meet operation did not result from the meeting of
8227 two equivalent names, VR0 cannot have any equivalences. */
8229 bitmap_clear (vr0
->equiv
);
8233 set_value_range_to_varying (vr0
);
8236 set_and_canonicalize_value_range (vr0
, vr0
->type
, vr0
->min
, vr0
->max
,
8238 if (vr0
->type
== VR_VARYING
)
8241 /* The resulting set of equivalences is always the intersection of
8243 if (vr0
->equiv
&& vr1
->equiv
&& vr0
->equiv
!= vr1
->equiv
)
8244 bitmap_and_into (vr0
->equiv
, vr1
->equiv
);
8245 else if (vr0
->equiv
&& !vr1
->equiv
)
8246 bitmap_clear (vr0
->equiv
);
8250 vrp_meet (value_range_t
*vr0
, value_range_t
*vr1
)
8252 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8254 fprintf (dump_file
, "Meeting\n ");
8255 dump_value_range (dump_file
, vr0
);
8256 fprintf (dump_file
, "\nand\n ");
8257 dump_value_range (dump_file
, vr1
);
8258 fprintf (dump_file
, "\n");
8260 vrp_meet_1 (vr0
, vr1
);
8261 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8263 fprintf (dump_file
, "to\n ");
8264 dump_value_range (dump_file
, vr0
);
8265 fprintf (dump_file
, "\n");
8270 /* Visit all arguments for PHI node PHI that flow through executable
8271 edges. If a valid value range can be derived from all the incoming
8272 value ranges, set a new range for the LHS of PHI. */
8274 static enum ssa_prop_result
8275 vrp_visit_phi_node (gimple phi
)
8278 tree lhs
= PHI_RESULT (phi
);
8279 value_range_t
*lhs_vr
= get_value_range (lhs
);
8280 value_range_t vr_result
= VR_INITIALIZER
;
8282 int edges
, old_edges
;
8285 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8287 fprintf (dump_file
, "\nVisiting PHI node: ");
8288 print_gimple_stmt (dump_file
, phi
, 0, dump_flags
);
8292 for (i
= 0; i
< gimple_phi_num_args (phi
); i
++)
8294 edge e
= gimple_phi_arg_edge (phi
, i
);
8296 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8299 "\n Argument #%d (%d -> %d %sexecutable)\n",
8300 (int) i
, e
->src
->index
, e
->dest
->index
,
8301 (e
->flags
& EDGE_EXECUTABLE
) ? "" : "not ");
8304 if (e
->flags
& EDGE_EXECUTABLE
)
8306 tree arg
= PHI_ARG_DEF (phi
, i
);
8307 value_range_t vr_arg
;
8311 if (TREE_CODE (arg
) == SSA_NAME
)
8313 vr_arg
= *(get_value_range (arg
));
8314 /* Do not allow equivalences or symbolic ranges to leak in from
8315 backedges. That creates invalid equivalencies.
8316 See PR53465 and PR54767. */
8317 if (e
->flags
& EDGE_DFS_BACK
8318 && (vr_arg
.type
== VR_RANGE
8319 || vr_arg
.type
== VR_ANTI_RANGE
))
8321 vr_arg
.equiv
= NULL
;
8322 if (symbolic_range_p (&vr_arg
))
8324 vr_arg
.type
= VR_VARYING
;
8325 vr_arg
.min
= NULL_TREE
;
8326 vr_arg
.max
= NULL_TREE
;
8332 if (is_overflow_infinity (arg
))
8333 arg
= drop_tree_overflow (arg
);
8335 vr_arg
.type
= VR_RANGE
;
8338 vr_arg
.equiv
= NULL
;
8341 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8343 fprintf (dump_file
, "\t");
8344 print_generic_expr (dump_file
, arg
, dump_flags
);
8345 fprintf (dump_file
, "\n\tValue: ");
8346 dump_value_range (dump_file
, &vr_arg
);
8347 fprintf (dump_file
, "\n");
8351 copy_value_range (&vr_result
, &vr_arg
);
8353 vrp_meet (&vr_result
, &vr_arg
);
8356 if (vr_result
.type
== VR_VARYING
)
8361 if (vr_result
.type
== VR_VARYING
)
8363 else if (vr_result
.type
== VR_UNDEFINED
)
8366 old_edges
= vr_phi_edge_counts
[SSA_NAME_VERSION (lhs
)];
8367 vr_phi_edge_counts
[SSA_NAME_VERSION (lhs
)] = edges
;
8369 /* To prevent infinite iterations in the algorithm, derive ranges
8370 when the new value is slightly bigger or smaller than the
8371 previous one. We don't do this if we have seen a new executable
8372 edge; this helps us avoid an overflow infinity for conditionals
8373 which are not in a loop. If the old value-range was VR_UNDEFINED
8374 use the updated range and iterate one more time. */
8376 && gimple_phi_num_args (phi
) > 1
8377 && edges
== old_edges
8378 && lhs_vr
->type
!= VR_UNDEFINED
)
8380 int cmp_min
= compare_values (lhs_vr
->min
, vr_result
.min
);
8381 int cmp_max
= compare_values (lhs_vr
->max
, vr_result
.max
);
8383 /* For non VR_RANGE or for pointers fall back to varying if
8384 the range changed. */
8385 if ((lhs_vr
->type
!= VR_RANGE
|| vr_result
.type
!= VR_RANGE
8386 || POINTER_TYPE_P (TREE_TYPE (lhs
)))
8387 && (cmp_min
!= 0 || cmp_max
!= 0))
8390 /* If the new minimum is smaller or larger than the previous
8391 one, go all the way to -INF. In the first case, to avoid
8392 iterating millions of times to reach -INF, and in the
8393 other case to avoid infinite bouncing between different
8395 if (cmp_min
> 0 || cmp_min
< 0)
8397 if (!needs_overflow_infinity (TREE_TYPE (vr_result
.min
))
8398 || !vrp_var_may_overflow (lhs
, phi
))
8399 vr_result
.min
= TYPE_MIN_VALUE (TREE_TYPE (vr_result
.min
));
8400 else if (supports_overflow_infinity (TREE_TYPE (vr_result
.min
)))
8402 negative_overflow_infinity (TREE_TYPE (vr_result
.min
));
8405 /* Similarly, if the new maximum is smaller or larger than
8406 the previous one, go all the way to +INF. */
8407 if (cmp_max
< 0 || cmp_max
> 0)
8409 if (!needs_overflow_infinity (TREE_TYPE (vr_result
.max
))
8410 || !vrp_var_may_overflow (lhs
, phi
))
8411 vr_result
.max
= TYPE_MAX_VALUE (TREE_TYPE (vr_result
.max
));
8412 else if (supports_overflow_infinity (TREE_TYPE (vr_result
.max
)))
8414 positive_overflow_infinity (TREE_TYPE (vr_result
.max
));
8417 /* If we dropped either bound to +-INF then if this is a loop
8418 PHI node SCEV may known more about its value-range. */
8419 if ((cmp_min
> 0 || cmp_min
< 0
8420 || cmp_max
< 0 || cmp_max
> 0)
8422 && (l
= loop_containing_stmt (phi
))
8423 && l
->header
== gimple_bb (phi
))
8424 adjust_range_with_scev (&vr_result
, l
, phi
, lhs
);
8426 /* If we will end up with a (-INF, +INF) range, set it to
8427 VARYING. Same if the previous max value was invalid for
8428 the type and we end up with vr_result.min > vr_result.max. */
8429 if ((vrp_val_is_max (vr_result
.max
)
8430 && vrp_val_is_min (vr_result
.min
))
8431 || compare_values (vr_result
.min
,
8436 /* If the new range is different than the previous value, keep
8439 if (update_value_range (lhs
, &vr_result
))
8441 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8443 fprintf (dump_file
, "Found new range for ");
8444 print_generic_expr (dump_file
, lhs
, 0);
8445 fprintf (dump_file
, ": ");
8446 dump_value_range (dump_file
, &vr_result
);
8447 fprintf (dump_file
, "\n\n");
8450 return SSA_PROP_INTERESTING
;
8453 /* Nothing changed, don't add outgoing edges. */
8454 return SSA_PROP_NOT_INTERESTING
;
8456 /* No match found. Set the LHS to VARYING. */
8458 set_value_range_to_varying (lhs_vr
);
8459 return SSA_PROP_VARYING
;
8462 /* Simplify boolean operations if the source is known
8463 to be already a boolean. */
8465 simplify_truth_ops_using_ranges (gimple_stmt_iterator
*gsi
, gimple stmt
)
8467 enum tree_code rhs_code
= gimple_assign_rhs_code (stmt
);
8469 bool need_conversion
;
8471 /* We handle only !=/== case here. */
8472 gcc_assert (rhs_code
== EQ_EXPR
|| rhs_code
== NE_EXPR
);
8474 op0
= gimple_assign_rhs1 (stmt
);
8475 if (!op_with_boolean_value_range_p (op0
))
8478 op1
= gimple_assign_rhs2 (stmt
);
8479 if (!op_with_boolean_value_range_p (op1
))
8482 /* Reduce number of cases to handle to NE_EXPR. As there is no
8483 BIT_XNOR_EXPR we cannot replace A == B with a single statement. */
8484 if (rhs_code
== EQ_EXPR
)
8486 if (TREE_CODE (op1
) == INTEGER_CST
)
8487 op1
= int_const_binop (BIT_XOR_EXPR
, op1
, integer_one_node
);
8492 lhs
= gimple_assign_lhs (stmt
);
8494 = !useless_type_conversion_p (TREE_TYPE (lhs
), TREE_TYPE (op0
));
8496 /* Make sure to not sign-extend a 1-bit 1 when converting the result. */
8498 && !TYPE_UNSIGNED (TREE_TYPE (op0
))
8499 && TYPE_PRECISION (TREE_TYPE (op0
)) == 1
8500 && TYPE_PRECISION (TREE_TYPE (lhs
)) > 1)
8503 /* For A != 0 we can substitute A itself. */
8504 if (integer_zerop (op1
))
8505 gimple_assign_set_rhs_with_ops (gsi
,
8507 ? NOP_EXPR
: TREE_CODE (op0
),
8509 /* For A != B we substitute A ^ B. Either with conversion. */
8510 else if (need_conversion
)
8512 tree tem
= make_ssa_name (TREE_TYPE (op0
), NULL
);
8513 gimple newop
= gimple_build_assign_with_ops (BIT_XOR_EXPR
, tem
, op0
, op1
);
8514 gsi_insert_before (gsi
, newop
, GSI_SAME_STMT
);
8515 gimple_assign_set_rhs_with_ops (gsi
, NOP_EXPR
, tem
, NULL_TREE
);
8519 gimple_assign_set_rhs_with_ops (gsi
, BIT_XOR_EXPR
, op0
, op1
);
8520 update_stmt (gsi_stmt (*gsi
));
8525 /* Simplify a division or modulo operator to a right shift or
8526 bitwise and if the first operand is unsigned or is greater
8527 than zero and the second operand is an exact power of two. */
8530 simplify_div_or_mod_using_ranges (gimple stmt
)
8532 enum tree_code rhs_code
= gimple_assign_rhs_code (stmt
);
8534 tree op0
= gimple_assign_rhs1 (stmt
);
8535 tree op1
= gimple_assign_rhs2 (stmt
);
8536 value_range_t
*vr
= get_value_range (gimple_assign_rhs1 (stmt
));
8538 if (TYPE_UNSIGNED (TREE_TYPE (op0
)))
8540 val
= integer_one_node
;
8546 val
= compare_range_with_value (GE_EXPR
, vr
, integer_zero_node
, &sop
);
8550 && integer_onep (val
)
8551 && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC
))
8553 location_t location
;
8555 if (!gimple_has_location (stmt
))
8556 location
= input_location
;
8558 location
= gimple_location (stmt
);
8559 warning_at (location
, OPT_Wstrict_overflow
,
8560 "assuming signed overflow does not occur when "
8561 "simplifying %</%> or %<%%%> to %<>>%> or %<&%>");
8565 if (val
&& integer_onep (val
))
8569 if (rhs_code
== TRUNC_DIV_EXPR
)
8571 t
= build_int_cst (integer_type_node
, tree_log2 (op1
));
8572 gimple_assign_set_rhs_code (stmt
, RSHIFT_EXPR
);
8573 gimple_assign_set_rhs1 (stmt
, op0
);
8574 gimple_assign_set_rhs2 (stmt
, t
);
8578 t
= build_int_cst (TREE_TYPE (op1
), 1);
8579 t
= int_const_binop (MINUS_EXPR
, op1
, t
);
8580 t
= fold_convert (TREE_TYPE (op0
), t
);
8582 gimple_assign_set_rhs_code (stmt
, BIT_AND_EXPR
);
8583 gimple_assign_set_rhs1 (stmt
, op0
);
8584 gimple_assign_set_rhs2 (stmt
, t
);
8594 /* If the operand to an ABS_EXPR is >= 0, then eliminate the
8595 ABS_EXPR. If the operand is <= 0, then simplify the
8596 ABS_EXPR into a NEGATE_EXPR. */
8599 simplify_abs_using_ranges (gimple stmt
)
8602 tree op
= gimple_assign_rhs1 (stmt
);
8603 tree type
= TREE_TYPE (op
);
8604 value_range_t
*vr
= get_value_range (op
);
8606 if (TYPE_UNSIGNED (type
))
8608 val
= integer_zero_node
;
8614 val
= compare_range_with_value (LE_EXPR
, vr
, integer_zero_node
, &sop
);
8618 val
= compare_range_with_value (GE_EXPR
, vr
, integer_zero_node
,
8623 if (integer_zerop (val
))
8624 val
= integer_one_node
;
8625 else if (integer_onep (val
))
8626 val
= integer_zero_node
;
8631 && (integer_onep (val
) || integer_zerop (val
)))
8633 if (sop
&& issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC
))
8635 location_t location
;
8637 if (!gimple_has_location (stmt
))
8638 location
= input_location
;
8640 location
= gimple_location (stmt
);
8641 warning_at (location
, OPT_Wstrict_overflow
,
8642 "assuming signed overflow does not occur when "
8643 "simplifying %<abs (X)%> to %<X%> or %<-X%>");
8646 gimple_assign_set_rhs1 (stmt
, op
);
8647 if (integer_onep (val
))
8648 gimple_assign_set_rhs_code (stmt
, NEGATE_EXPR
);
8650 gimple_assign_set_rhs_code (stmt
, SSA_NAME
);
8659 /* Optimize away redundant BIT_AND_EXPR and BIT_IOR_EXPR.
8660 If all the bits that are being cleared by & are already
8661 known to be zero from VR, or all the bits that are being
8662 set by | are already known to be one from VR, the bit
8663 operation is redundant. */
8666 simplify_bit_ops_using_ranges (gimple_stmt_iterator
*gsi
, gimple stmt
)
8668 tree op0
= gimple_assign_rhs1 (stmt
);
8669 tree op1
= gimple_assign_rhs2 (stmt
);
8670 tree op
= NULL_TREE
;
8671 value_range_t vr0
= VR_INITIALIZER
;
8672 value_range_t vr1
= VR_INITIALIZER
;
8673 double_int may_be_nonzero0
, may_be_nonzero1
;
8674 double_int must_be_nonzero0
, must_be_nonzero1
;
8677 if (TREE_CODE (op0
) == SSA_NAME
)
8678 vr0
= *(get_value_range (op0
));
8679 else if (is_gimple_min_invariant (op0
))
8680 set_value_range_to_value (&vr0
, op0
, NULL
);
8684 if (TREE_CODE (op1
) == SSA_NAME
)
8685 vr1
= *(get_value_range (op1
));
8686 else if (is_gimple_min_invariant (op1
))
8687 set_value_range_to_value (&vr1
, op1
, NULL
);
8691 if (!zero_nonzero_bits_from_vr (&vr0
, &may_be_nonzero0
, &must_be_nonzero0
))
8693 if (!zero_nonzero_bits_from_vr (&vr1
, &may_be_nonzero1
, &must_be_nonzero1
))
8696 switch (gimple_assign_rhs_code (stmt
))
8699 mask
= may_be_nonzero0
.and_not (must_be_nonzero1
);
8700 if (mask
.is_zero ())
8705 mask
= may_be_nonzero1
.and_not (must_be_nonzero0
);
8706 if (mask
.is_zero ())
8713 mask
= may_be_nonzero0
.and_not (must_be_nonzero1
);
8714 if (mask
.is_zero ())
8719 mask
= may_be_nonzero1
.and_not (must_be_nonzero0
);
8720 if (mask
.is_zero ())
8730 if (op
== NULL_TREE
)
8733 gimple_assign_set_rhs_with_ops (gsi
, TREE_CODE (op
), op
, NULL
);
8734 update_stmt (gsi_stmt (*gsi
));
8738 /* We are comparing trees OP0 and OP1 using COND_CODE. OP0 has
8739 a known value range VR.
8741 If there is one and only one value which will satisfy the
8742 conditional, then return that value. Else return NULL. */
8745 test_for_singularity (enum tree_code cond_code
, tree op0
,
8746 tree op1
, value_range_t
*vr
)
8751 /* Extract minimum/maximum values which satisfy the
8752 the conditional as it was written. */
8753 if (cond_code
== LE_EXPR
|| cond_code
== LT_EXPR
)
8755 /* This should not be negative infinity; there is no overflow
8757 min
= TYPE_MIN_VALUE (TREE_TYPE (op0
));
8760 if (cond_code
== LT_EXPR
&& !is_overflow_infinity (max
))
8762 tree one
= build_int_cst (TREE_TYPE (op0
), 1);
8763 max
= fold_build2 (MINUS_EXPR
, TREE_TYPE (op0
), max
, one
);
8765 TREE_NO_WARNING (max
) = 1;
8768 else if (cond_code
== GE_EXPR
|| cond_code
== GT_EXPR
)
8770 /* This should not be positive infinity; there is no overflow
8772 max
= TYPE_MAX_VALUE (TREE_TYPE (op0
));
8775 if (cond_code
== GT_EXPR
&& !is_overflow_infinity (min
))
8777 tree one
= build_int_cst (TREE_TYPE (op0
), 1);
8778 min
= fold_build2 (PLUS_EXPR
, TREE_TYPE (op0
), min
, one
);
8780 TREE_NO_WARNING (min
) = 1;
8784 /* Now refine the minimum and maximum values using any
8785 value range information we have for op0. */
8788 if (compare_values (vr
->min
, min
) == 1)
8790 if (compare_values (vr
->max
, max
) == -1)
8793 /* If the new min/max values have converged to a single value,
8794 then there is only one value which can satisfy the condition,
8795 return that value. */
8796 if (operand_equal_p (min
, max
, 0) && is_gimple_min_invariant (min
))
8802 /* Return whether the value range *VR fits in an integer type specified
8803 by PRECISION and UNSIGNED_P. */
8806 range_fits_type_p (value_range_t
*vr
, unsigned precision
, bool unsigned_p
)
8809 unsigned src_precision
;
8812 /* We can only handle integral and pointer types. */
8813 src_type
= TREE_TYPE (vr
->min
);
8814 if (!INTEGRAL_TYPE_P (src_type
)
8815 && !POINTER_TYPE_P (src_type
))
8818 /* An extension is fine unless VR is signed and unsigned_p,
8819 and so is an identity transform. */
8820 src_precision
= TYPE_PRECISION (TREE_TYPE (vr
->min
));
8821 if ((src_precision
< precision
8822 && !(unsigned_p
&& !TYPE_UNSIGNED (src_type
)))
8823 || (src_precision
== precision
8824 && TYPE_UNSIGNED (src_type
) == unsigned_p
))
8827 /* Now we can only handle ranges with constant bounds. */
8828 if (vr
->type
!= VR_RANGE
8829 || TREE_CODE (vr
->min
) != INTEGER_CST
8830 || TREE_CODE (vr
->max
) != INTEGER_CST
)
8833 /* For sign changes, the MSB of the double_int has to be clear.
8834 An unsigned value with its MSB set cannot be represented by
8835 a signed double_int, while a negative value cannot be represented
8836 by an unsigned double_int. */
8837 if (TYPE_UNSIGNED (src_type
) != unsigned_p
8838 && (TREE_INT_CST_HIGH (vr
->min
) | TREE_INT_CST_HIGH (vr
->max
)) < 0)
8841 /* Then we can perform the conversion on both ends and compare
8842 the result for equality. */
8843 tem
= tree_to_double_int (vr
->min
).ext (precision
, unsigned_p
);
8844 if (tree_to_double_int (vr
->min
) != tem
)
8846 tem
= tree_to_double_int (vr
->max
).ext (precision
, unsigned_p
);
8847 if (tree_to_double_int (vr
->max
) != tem
)
8853 /* Simplify a conditional using a relational operator to an equality
8854 test if the range information indicates only one value can satisfy
8855 the original conditional. */
8858 simplify_cond_using_ranges (gimple stmt
)
8860 tree op0
= gimple_cond_lhs (stmt
);
8861 tree op1
= gimple_cond_rhs (stmt
);
8862 enum tree_code cond_code
= gimple_cond_code (stmt
);
8864 if (cond_code
!= NE_EXPR
8865 && cond_code
!= EQ_EXPR
8866 && TREE_CODE (op0
) == SSA_NAME
8867 && INTEGRAL_TYPE_P (TREE_TYPE (op0
))
8868 && is_gimple_min_invariant (op1
))
8870 value_range_t
*vr
= get_value_range (op0
);
8872 /* If we have range information for OP0, then we might be
8873 able to simplify this conditional. */
8874 if (vr
->type
== VR_RANGE
)
8876 tree new_tree
= test_for_singularity (cond_code
, op0
, op1
, vr
);
8882 fprintf (dump_file
, "Simplified relational ");
8883 print_gimple_stmt (dump_file
, stmt
, 0, 0);
8884 fprintf (dump_file
, " into ");
8887 gimple_cond_set_code (stmt
, EQ_EXPR
);
8888 gimple_cond_set_lhs (stmt
, op0
);
8889 gimple_cond_set_rhs (stmt
, new_tree
);
8895 print_gimple_stmt (dump_file
, stmt
, 0, 0);
8896 fprintf (dump_file
, "\n");
8902 /* Try again after inverting the condition. We only deal
8903 with integral types here, so no need to worry about
8904 issues with inverting FP comparisons. */
8905 cond_code
= invert_tree_comparison (cond_code
, false);
8906 new_tree
= test_for_singularity (cond_code
, op0
, op1
, vr
);
8912 fprintf (dump_file
, "Simplified relational ");
8913 print_gimple_stmt (dump_file
, stmt
, 0, 0);
8914 fprintf (dump_file
, " into ");
8917 gimple_cond_set_code (stmt
, NE_EXPR
);
8918 gimple_cond_set_lhs (stmt
, op0
);
8919 gimple_cond_set_rhs (stmt
, new_tree
);
8925 print_gimple_stmt (dump_file
, stmt
, 0, 0);
8926 fprintf (dump_file
, "\n");
8934 /* If we have a comparison of an SSA_NAME (OP0) against a constant,
8935 see if OP0 was set by a type conversion where the source of
8936 the conversion is another SSA_NAME with a range that fits
8937 into the range of OP0's type.
8939 If so, the conversion is redundant as the earlier SSA_NAME can be
8940 used for the comparison directly if we just massage the constant in the
8942 if (TREE_CODE (op0
) == SSA_NAME
8943 && TREE_CODE (op1
) == INTEGER_CST
)
8945 gimple def_stmt
= SSA_NAME_DEF_STMT (op0
);
8948 if (!is_gimple_assign (def_stmt
)
8949 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt
)))
8952 innerop
= gimple_assign_rhs1 (def_stmt
);
8954 if (TREE_CODE (innerop
) == SSA_NAME
8955 && !POINTER_TYPE_P (TREE_TYPE (innerop
)))
8957 value_range_t
*vr
= get_value_range (innerop
);
8959 if (range_int_cst_p (vr
)
8960 && range_fits_type_p (vr
,
8961 TYPE_PRECISION (TREE_TYPE (op0
)),
8962 TYPE_UNSIGNED (TREE_TYPE (op0
)))
8963 && int_fits_type_p (op1
, TREE_TYPE (innerop
))
8964 /* The range must not have overflowed, or if it did overflow
8965 we must not be wrapping/trapping overflow and optimizing
8966 with strict overflow semantics. */
8967 && ((!is_negative_overflow_infinity (vr
->min
)
8968 && !is_positive_overflow_infinity (vr
->max
))
8969 || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (innerop
))))
8971 /* If the range overflowed and the user has asked for warnings
8972 when strict overflow semantics were used to optimize code,
8973 issue an appropriate warning. */
8974 if ((is_negative_overflow_infinity (vr
->min
)
8975 || is_positive_overflow_infinity (vr
->max
))
8976 && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_CONDITIONAL
))
8978 location_t location
;
8980 if (!gimple_has_location (stmt
))
8981 location
= input_location
;
8983 location
= gimple_location (stmt
);
8984 warning_at (location
, OPT_Wstrict_overflow
,
8985 "assuming signed overflow does not occur when "
8986 "simplifying conditional");
8989 tree newconst
= fold_convert (TREE_TYPE (innerop
), op1
);
8990 gimple_cond_set_lhs (stmt
, innerop
);
8991 gimple_cond_set_rhs (stmt
, newconst
);
9000 /* Simplify a switch statement using the value range of the switch
9004 simplify_switch_using_ranges (gimple stmt
)
9006 tree op
= gimple_switch_index (stmt
);
9011 size_t i
= 0, j
= 0, n
, n2
;
9014 size_t k
= 1, l
= 0;
9016 if (TREE_CODE (op
) == SSA_NAME
)
9018 vr
= get_value_range (op
);
9020 /* We can only handle integer ranges. */
9021 if ((vr
->type
!= VR_RANGE
9022 && vr
->type
!= VR_ANTI_RANGE
)
9023 || symbolic_range_p (vr
))
9026 /* Find case label for min/max of the value range. */
9027 take_default
= !find_case_label_ranges (stmt
, vr
, &i
, &j
, &k
, &l
);
9029 else if (TREE_CODE (op
) == INTEGER_CST
)
9031 take_default
= !find_case_label_index (stmt
, 1, op
, &i
);
9045 n
= gimple_switch_num_labels (stmt
);
9047 /* Bail out if this is just all edges taken. */
9053 /* Build a new vector of taken case labels. */
9054 vec2
= make_tree_vec (j
- i
+ 1 + l
- k
+ 1 + (int)take_default
);
9057 /* Add the default edge, if necessary. */
9059 TREE_VEC_ELT (vec2
, n2
++) = gimple_switch_default_label (stmt
);
9061 for (; i
<= j
; ++i
, ++n2
)
9062 TREE_VEC_ELT (vec2
, n2
) = gimple_switch_label (stmt
, i
);
9064 for (; k
<= l
; ++k
, ++n2
)
9065 TREE_VEC_ELT (vec2
, n2
) = gimple_switch_label (stmt
, k
);
9067 /* Mark needed edges. */
9068 for (i
= 0; i
< n2
; ++i
)
9070 e
= find_edge (gimple_bb (stmt
),
9071 label_to_block (CASE_LABEL (TREE_VEC_ELT (vec2
, i
))));
9072 e
->aux
= (void *)-1;
9075 /* Queue not needed edges for later removal. */
9076 FOR_EACH_EDGE (e
, ei
, gimple_bb (stmt
)->succs
)
9078 if (e
->aux
== (void *)-1)
9084 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
9086 fprintf (dump_file
, "removing unreachable case label\n");
9088 to_remove_edges
.safe_push (e
);
9089 e
->flags
&= ~EDGE_EXECUTABLE
;
9092 /* And queue an update for the stmt. */
9095 to_update_switch_stmts
.safe_push (su
);
9099 /* Simplify an integral conversion from an SSA name in STMT. */
9102 simplify_conversion_using_ranges (gimple stmt
)
9104 tree innerop
, middleop
, finaltype
;
9106 value_range_t
*innervr
;
9107 bool inner_unsigned_p
, middle_unsigned_p
, final_unsigned_p
;
9108 unsigned inner_prec
, middle_prec
, final_prec
;
9109 double_int innermin
, innermed
, innermax
, middlemin
, middlemed
, middlemax
;
9111 finaltype
= TREE_TYPE (gimple_assign_lhs (stmt
));
9112 if (!INTEGRAL_TYPE_P (finaltype
))
9114 middleop
= gimple_assign_rhs1 (stmt
);
9115 def_stmt
= SSA_NAME_DEF_STMT (middleop
);
9116 if (!is_gimple_assign (def_stmt
)
9117 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt
)))
9119 innerop
= gimple_assign_rhs1 (def_stmt
);
9120 if (TREE_CODE (innerop
) != SSA_NAME
9121 || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (innerop
))
9124 /* Get the value-range of the inner operand. */
9125 innervr
= get_value_range (innerop
);
9126 if (innervr
->type
!= VR_RANGE
9127 || TREE_CODE (innervr
->min
) != INTEGER_CST
9128 || TREE_CODE (innervr
->max
) != INTEGER_CST
)
9131 /* Simulate the conversion chain to check if the result is equal if
9132 the middle conversion is removed. */
9133 innermin
= tree_to_double_int (innervr
->min
);
9134 innermax
= tree_to_double_int (innervr
->max
);
9136 inner_prec
= TYPE_PRECISION (TREE_TYPE (innerop
));
9137 middle_prec
= TYPE_PRECISION (TREE_TYPE (middleop
));
9138 final_prec
= TYPE_PRECISION (finaltype
);
9140 /* If the first conversion is not injective, the second must not
9142 if ((innermax
- innermin
).ugt (double_int::mask (middle_prec
))
9143 && middle_prec
< final_prec
)
9145 /* We also want a medium value so that we can track the effect that
9146 narrowing conversions with sign change have. */
9147 inner_unsigned_p
= TYPE_UNSIGNED (TREE_TYPE (innerop
));
9148 if (inner_unsigned_p
)
9149 innermed
= double_int::mask (inner_prec
).lrshift (1, inner_prec
);
9151 innermed
= double_int_zero
;
9152 if (innermin
.cmp (innermed
, inner_unsigned_p
) >= 0
9153 || innermed
.cmp (innermax
, inner_unsigned_p
) >= 0)
9154 innermed
= innermin
;
9156 middle_unsigned_p
= TYPE_UNSIGNED (TREE_TYPE (middleop
));
9157 middlemin
= innermin
.ext (middle_prec
, middle_unsigned_p
);
9158 middlemed
= innermed
.ext (middle_prec
, middle_unsigned_p
);
9159 middlemax
= innermax
.ext (middle_prec
, middle_unsigned_p
);
9161 /* Require that the final conversion applied to both the original
9162 and the intermediate range produces the same result. */
9163 final_unsigned_p
= TYPE_UNSIGNED (finaltype
);
9164 if (middlemin
.ext (final_prec
, final_unsigned_p
)
9165 != innermin
.ext (final_prec
, final_unsigned_p
)
9166 || middlemed
.ext (final_prec
, final_unsigned_p
)
9167 != innermed
.ext (final_prec
, final_unsigned_p
)
9168 || middlemax
.ext (final_prec
, final_unsigned_p
)
9169 != innermax
.ext (final_prec
, final_unsigned_p
))
9172 gimple_assign_set_rhs1 (stmt
, innerop
);
9177 /* Simplify a conversion from integral SSA name to float in STMT. */
9180 simplify_float_conversion_using_ranges (gimple_stmt_iterator
*gsi
, gimple stmt
)
9182 tree rhs1
= gimple_assign_rhs1 (stmt
);
9183 value_range_t
*vr
= get_value_range (rhs1
);
9184 enum machine_mode fltmode
= TYPE_MODE (TREE_TYPE (gimple_assign_lhs (stmt
)));
9185 enum machine_mode mode
;
9189 /* We can only handle constant ranges. */
9190 if (vr
->type
!= VR_RANGE
9191 || TREE_CODE (vr
->min
) != INTEGER_CST
9192 || TREE_CODE (vr
->max
) != INTEGER_CST
)
9195 /* First check if we can use a signed type in place of an unsigned. */
9196 if (TYPE_UNSIGNED (TREE_TYPE (rhs1
))
9197 && (can_float_p (fltmode
, TYPE_MODE (TREE_TYPE (rhs1
)), 0)
9198 != CODE_FOR_nothing
)
9199 && range_fits_type_p (vr
, GET_MODE_PRECISION
9200 (TYPE_MODE (TREE_TYPE (rhs1
))), 0))
9201 mode
= TYPE_MODE (TREE_TYPE (rhs1
));
9202 /* If we can do the conversion in the current input mode do nothing. */
9203 else if (can_float_p (fltmode
, TYPE_MODE (TREE_TYPE (rhs1
)),
9204 TYPE_UNSIGNED (TREE_TYPE (rhs1
))) != CODE_FOR_nothing
)
9206 /* Otherwise search for a mode we can use, starting from the narrowest
9207 integer mode available. */
9210 mode
= GET_CLASS_NARROWEST_MODE (MODE_INT
);
9213 /* If we cannot do a signed conversion to float from mode
9214 or if the value-range does not fit in the signed type
9215 try with a wider mode. */
9216 if (can_float_p (fltmode
, mode
, 0) != CODE_FOR_nothing
9217 && range_fits_type_p (vr
, GET_MODE_PRECISION (mode
), 0))
9220 mode
= GET_MODE_WIDER_MODE (mode
);
9221 /* But do not widen the input. Instead leave that to the
9222 optabs expansion code. */
9223 if (GET_MODE_PRECISION (mode
) > TYPE_PRECISION (TREE_TYPE (rhs1
)))
9226 while (mode
!= VOIDmode
);
9227 if (mode
== VOIDmode
)
9231 /* It works, insert a truncation or sign-change before the
9232 float conversion. */
9233 tem
= make_ssa_name (build_nonstandard_integer_type
9234 (GET_MODE_PRECISION (mode
), 0), NULL
);
9235 conv
= gimple_build_assign_with_ops (NOP_EXPR
, tem
, rhs1
, NULL_TREE
);
9236 gsi_insert_before (gsi
, conv
, GSI_SAME_STMT
);
9237 gimple_assign_set_rhs1 (stmt
, tem
);
9243 /* Simplify STMT using ranges if possible. */
9246 simplify_stmt_using_ranges (gimple_stmt_iterator
*gsi
)
9248 gimple stmt
= gsi_stmt (*gsi
);
9249 if (is_gimple_assign (stmt
))
9251 enum tree_code rhs_code
= gimple_assign_rhs_code (stmt
);
9252 tree rhs1
= gimple_assign_rhs1 (stmt
);
9258 /* Transform EQ_EXPR, NE_EXPR into BIT_XOR_EXPR or identity
9259 if the RHS is zero or one, and the LHS are known to be boolean
9261 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
9262 return simplify_truth_ops_using_ranges (gsi
, stmt
);
9265 /* Transform TRUNC_DIV_EXPR and TRUNC_MOD_EXPR into RSHIFT_EXPR
9266 and BIT_AND_EXPR respectively if the first operand is greater
9267 than zero and the second operand is an exact power of two. */
9268 case TRUNC_DIV_EXPR
:
9269 case TRUNC_MOD_EXPR
:
9270 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1
))
9271 && integer_pow2p (gimple_assign_rhs2 (stmt
)))
9272 return simplify_div_or_mod_using_ranges (stmt
);
9275 /* Transform ABS (X) into X or -X as appropriate. */
9277 if (TREE_CODE (rhs1
) == SSA_NAME
9278 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
9279 return simplify_abs_using_ranges (stmt
);
9284 /* Optimize away BIT_AND_EXPR and BIT_IOR_EXPR
9285 if all the bits being cleared are already cleared or
9286 all the bits being set are already set. */
9287 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
9288 return simplify_bit_ops_using_ranges (gsi
, stmt
);
9292 if (TREE_CODE (rhs1
) == SSA_NAME
9293 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
9294 return simplify_conversion_using_ranges (stmt
);
9298 if (TREE_CODE (rhs1
) == SSA_NAME
9299 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
9300 return simplify_float_conversion_using_ranges (gsi
, stmt
);
9307 else if (gimple_code (stmt
) == GIMPLE_COND
)
9308 return simplify_cond_using_ranges (stmt
);
9309 else if (gimple_code (stmt
) == GIMPLE_SWITCH
)
9310 return simplify_switch_using_ranges (stmt
);
9315 /* If the statement pointed by SI has a predicate whose value can be
9316 computed using the value range information computed by VRP, compute
9317 its value and return true. Otherwise, return false. */
9320 fold_predicate_in (gimple_stmt_iterator
*si
)
9322 bool assignment_p
= false;
9324 gimple stmt
= gsi_stmt (*si
);
9326 if (is_gimple_assign (stmt
)
9327 && TREE_CODE_CLASS (gimple_assign_rhs_code (stmt
)) == tcc_comparison
)
9329 assignment_p
= true;
9330 val
= vrp_evaluate_conditional (gimple_assign_rhs_code (stmt
),
9331 gimple_assign_rhs1 (stmt
),
9332 gimple_assign_rhs2 (stmt
),
9335 else if (gimple_code (stmt
) == GIMPLE_COND
)
9336 val
= vrp_evaluate_conditional (gimple_cond_code (stmt
),
9337 gimple_cond_lhs (stmt
),
9338 gimple_cond_rhs (stmt
),
9346 val
= fold_convert (gimple_expr_type (stmt
), val
);
9350 fprintf (dump_file
, "Folding predicate ");
9351 print_gimple_expr (dump_file
, stmt
, 0, 0);
9352 fprintf (dump_file
, " to ");
9353 print_generic_expr (dump_file
, val
, 0);
9354 fprintf (dump_file
, "\n");
9357 if (is_gimple_assign (stmt
))
9358 gimple_assign_set_rhs_from_tree (si
, val
);
9361 gcc_assert (gimple_code (stmt
) == GIMPLE_COND
);
9362 if (integer_zerop (val
))
9363 gimple_cond_make_false (stmt
);
9364 else if (integer_onep (val
))
9365 gimple_cond_make_true (stmt
);
9376 /* Callback for substitute_and_fold folding the stmt at *SI. */
9379 vrp_fold_stmt (gimple_stmt_iterator
*si
)
9381 if (fold_predicate_in (si
))
9384 return simplify_stmt_using_ranges (si
);
9387 /* Stack of dest,src equivalency pairs that need to be restored after
9388 each attempt to thread a block's incoming edge to an outgoing edge.
9390 A NULL entry is used to mark the end of pairs which need to be
9392 static vec
<tree
> equiv_stack
;
9394 /* A trivial wrapper so that we can present the generic jump threading
9395 code with a simple API for simplifying statements. STMT is the
9396 statement we want to simplify, WITHIN_STMT provides the location
9397 for any overflow warnings. */
9400 simplify_stmt_for_jump_threading (gimple stmt
, gimple within_stmt
)
9402 if (gimple_code (stmt
) == GIMPLE_COND
)
9403 return vrp_evaluate_conditional (gimple_cond_code (stmt
),
9404 gimple_cond_lhs (stmt
),
9405 gimple_cond_rhs (stmt
), within_stmt
);
9407 if (gimple_code (stmt
) == GIMPLE_ASSIGN
)
9409 value_range_t new_vr
= VR_INITIALIZER
;
9410 tree lhs
= gimple_assign_lhs (stmt
);
9412 if (TREE_CODE (lhs
) == SSA_NAME
9413 && (INTEGRAL_TYPE_P (TREE_TYPE (lhs
))
9414 || POINTER_TYPE_P (TREE_TYPE (lhs
))))
9416 extract_range_from_assignment (&new_vr
, stmt
);
9417 if (range_int_cst_singleton_p (&new_vr
))
9425 /* Blocks which have more than one predecessor and more than
9426 one successor present jump threading opportunities, i.e.,
9427 when the block is reached from a specific predecessor, we
9428 may be able to determine which of the outgoing edges will
9429 be traversed. When this optimization applies, we are able
9430 to avoid conditionals at runtime and we may expose secondary
9431 optimization opportunities.
9433 This routine is effectively a driver for the generic jump
9434 threading code. It basically just presents the generic code
9435 with edges that may be suitable for jump threading.
9437 Unlike DOM, we do not iterate VRP if jump threading was successful.
9438 While iterating may expose new opportunities for VRP, it is expected
9439 those opportunities would be very limited and the compile time cost
9440 to expose those opportunities would be significant.
9442 As jump threading opportunities are discovered, they are registered
9443 for later realization. */
9446 identify_jump_threads (void)
9453 /* Ugh. When substituting values earlier in this pass we can
9454 wipe the dominance information. So rebuild the dominator
9455 information as we need it within the jump threading code. */
9456 calculate_dominance_info (CDI_DOMINATORS
);
9458 /* We do not allow VRP information to be used for jump threading
9459 across a back edge in the CFG. Otherwise it becomes too
9460 difficult to avoid eliminating loop exit tests. Of course
9461 EDGE_DFS_BACK is not accurate at this time so we have to
9463 mark_dfs_back_edges ();
9465 /* Do not thread across edges we are about to remove. Just marking
9466 them as EDGE_DFS_BACK will do. */
9467 FOR_EACH_VEC_ELT (to_remove_edges
, i
, e
)
9468 e
->flags
|= EDGE_DFS_BACK
;
9470 /* Allocate our unwinder stack to unwind any temporary equivalences
9471 that might be recorded. */
9472 equiv_stack
.create (20);
9474 /* To avoid lots of silly node creation, we create a single
9475 conditional and just modify it in-place when attempting to
9477 dummy
= gimple_build_cond (EQ_EXPR
,
9478 integer_zero_node
, integer_zero_node
,
9481 /* Walk through all the blocks finding those which present a
9482 potential jump threading opportunity. We could set this up
9483 as a dominator walker and record data during the walk, but
9484 I doubt it's worth the effort for the classes of jump
9485 threading opportunities we are trying to identify at this
9486 point in compilation. */
9491 /* If the generic jump threading code does not find this block
9492 interesting, then there is nothing to do. */
9493 if (! potentially_threadable_block (bb
))
9496 /* We only care about blocks ending in a COND_EXPR. While there
9497 may be some value in handling SWITCH_EXPR here, I doubt it's
9498 terribly important. */
9499 last
= gsi_stmt (gsi_last_bb (bb
));
9501 /* We're basically looking for a switch or any kind of conditional with
9502 integral or pointer type arguments. Note the type of the second
9503 argument will be the same as the first argument, so no need to
9504 check it explicitly. */
9505 if (gimple_code (last
) == GIMPLE_SWITCH
9506 || (gimple_code (last
) == GIMPLE_COND
9507 && TREE_CODE (gimple_cond_lhs (last
)) == SSA_NAME
9508 && (INTEGRAL_TYPE_P (TREE_TYPE (gimple_cond_lhs (last
)))
9509 || POINTER_TYPE_P (TREE_TYPE (gimple_cond_lhs (last
))))
9510 && (TREE_CODE (gimple_cond_rhs (last
)) == SSA_NAME
9511 || is_gimple_min_invariant (gimple_cond_rhs (last
)))))
9515 /* We've got a block with multiple predecessors and multiple
9516 successors which also ends in a suitable conditional or
9517 switch statement. For each predecessor, see if we can thread
9518 it to a specific successor. */
9519 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
9521 /* Do not thread across back edges or abnormal edges
9523 if (e
->flags
& (EDGE_DFS_BACK
| EDGE_COMPLEX
))
9526 thread_across_edge (dummy
, e
, true, &equiv_stack
,
9527 simplify_stmt_for_jump_threading
);
9532 /* We do not actually update the CFG or SSA graphs at this point as
9533 ASSERT_EXPRs are still in the IL and cfg cleanup code does not yet
9534 handle ASSERT_EXPRs gracefully. */
9537 /* We identified all the jump threading opportunities earlier, but could
9538 not transform the CFG at that time. This routine transforms the
9539 CFG and arranges for the dominator tree to be rebuilt if necessary.
9541 Note the SSA graph update will occur during the normal TODO
9542 processing by the pass manager. */
9544 finalize_jump_threads (void)
9546 thread_through_all_blocks (false);
9547 equiv_stack
.release ();
9551 /* Traverse all the blocks folding conditionals with known ranges. */
9558 values_propagated
= true;
9562 fprintf (dump_file
, "\nValue ranges after VRP:\n\n");
9563 dump_all_value_ranges (dump_file
);
9564 fprintf (dump_file
, "\n");
9567 substitute_and_fold (op_with_constant_singleton_value_range
,
9568 vrp_fold_stmt
, false);
9570 if (warn_array_bounds
)
9571 check_all_array_refs ();
9573 /* We must identify jump threading opportunities before we release
9574 the datastructures built by VRP. */
9575 identify_jump_threads ();
9577 /* Set value range to non pointer SSA_NAMEs. */
9578 for (i
= 0; i
< num_vr_values
; i
++)
9581 tree name
= ssa_name (i
);
9584 || POINTER_TYPE_P (TREE_TYPE (name
))
9585 || (vr_value
[i
]->type
== VR_VARYING
)
9586 || (vr_value
[i
]->type
== VR_UNDEFINED
))
9589 if ((TREE_CODE (vr_value
[i
]->min
) == INTEGER_CST
)
9590 && (TREE_CODE (vr_value
[i
]->max
) == INTEGER_CST
))
9592 if (vr_value
[i
]->type
== VR_RANGE
)
9593 set_range_info (name
,
9594 tree_to_double_int (vr_value
[i
]->min
),
9595 tree_to_double_int (vr_value
[i
]->max
));
9596 else if (vr_value
[i
]->type
== VR_ANTI_RANGE
)
9598 /* VR_ANTI_RANGE ~[min, max] is encoded compactly as
9599 [max + 1, min - 1] without additional attributes.
9600 When min value > max value, we know that it is
9601 VR_ANTI_RANGE; it is VR_RANGE otherwise. */
9603 /* ~[0,0] anti-range is represented as
9605 if (TYPE_UNSIGNED (TREE_TYPE (name
))
9606 && integer_zerop (vr_value
[i
]->min
)
9607 && integer_zerop (vr_value
[i
]->max
))
9608 set_range_info (name
,
9610 double_int::max_value
9611 (TYPE_PRECISION (TREE_TYPE (name
)), true));
9613 set_range_info (name
,
9614 tree_to_double_int (vr_value
[i
]->max
)
9616 tree_to_double_int (vr_value
[i
]->min
)
9622 /* Free allocated memory. */
9623 for (i
= 0; i
< num_vr_values
; i
++)
9626 BITMAP_FREE (vr_value
[i
]->equiv
);
9631 free (vr_phi_edge_counts
);
9633 /* So that we can distinguish between VRP data being available
9634 and not available. */
9636 vr_phi_edge_counts
= NULL
;
9640 /* Main entry point to VRP (Value Range Propagation). This pass is
9641 loosely based on J. R. C. Patterson, ``Accurate Static Branch
9642 Prediction by Value Range Propagation,'' in SIGPLAN Conference on
9643 Programming Language Design and Implementation, pp. 67-78, 1995.
9644 Also available at http://citeseer.ist.psu.edu/patterson95accurate.html
9646 This is essentially an SSA-CCP pass modified to deal with ranges
9647 instead of constants.
9649 While propagating ranges, we may find that two or more SSA name
9650 have equivalent, though distinct ranges. For instance,
9653 2 p_4 = ASSERT_EXPR <p_3, p_3 != 0>
9655 4 p_5 = ASSERT_EXPR <p_4, p_4 == q_2>;
9659 In the code above, pointer p_5 has range [q_2, q_2], but from the
9660 code we can also determine that p_5 cannot be NULL and, if q_2 had
9661 a non-varying range, p_5's range should also be compatible with it.
9663 These equivalences are created by two expressions: ASSERT_EXPR and
9664 copy operations. Since p_5 is an assertion on p_4, and p_4 was the
9665 result of another assertion, then we can use the fact that p_5 and
9666 p_4 are equivalent when evaluating p_5's range.
9668 Together with value ranges, we also propagate these equivalences
9669 between names so that we can take advantage of information from
9670 multiple ranges when doing final replacement. Note that this
9671 equivalency relation is transitive but not symmetric.
9673 In the example above, p_5 is equivalent to p_4, q_2 and p_3, but we
9674 cannot assert that q_2 is equivalent to p_5 because q_2 may be used
9675 in contexts where that assertion does not hold (e.g., in line 6).
9677 TODO, the main difference between this pass and Patterson's is that
9678 we do not propagate edge probabilities. We only compute whether
9679 edges can be taken or not. That is, instead of having a spectrum
9680 of jump probabilities between 0 and 1, we only deal with 0, 1 and
9681 DON'T KNOW. In the future, it may be worthwhile to propagate
9682 probabilities to aid branch prediction. */
9691 loop_optimizer_init (LOOPS_NORMAL
| LOOPS_HAVE_RECORDED_EXITS
);
9692 rewrite_into_loop_closed_ssa (NULL
, TODO_update_ssa
);
9695 /* ??? This ends up using stale EDGE_DFS_BACK for liveness computation.
9696 Inserting assertions may split edges which will invalidate
9698 insert_range_assertions ();
9700 to_remove_edges
.create (10);
9701 to_update_switch_stmts
.create (5);
9702 threadedge_initialize_values ();
9704 /* For visiting PHI nodes we need EDGE_DFS_BACK computed. */
9705 mark_dfs_back_edges ();
9708 ssa_propagate (vrp_visit_stmt
, vrp_visit_phi_node
);
9711 free_numbers_of_iterations_estimates ();
9713 /* ASSERT_EXPRs must be removed before finalizing jump threads
9714 as finalizing jump threads calls the CFG cleanup code which
9715 does not properly handle ASSERT_EXPRs. */
9716 remove_range_assertions ();
9718 /* If we exposed any new variables, go ahead and put them into
9719 SSA form now, before we handle jump threading. This simplifies
9720 interactions between rewriting of _DECL nodes into SSA form
9721 and rewriting SSA_NAME nodes into SSA form after block
9722 duplication and CFG manipulation. */
9723 update_ssa (TODO_update_ssa
);
9725 finalize_jump_threads ();
9727 /* Remove dead edges from SWITCH_EXPR optimization. This leaves the
9728 CFG in a broken state and requires a cfg_cleanup run. */
9729 FOR_EACH_VEC_ELT (to_remove_edges
, i
, e
)
9731 /* Update SWITCH_EXPR case label vector. */
9732 FOR_EACH_VEC_ELT (to_update_switch_stmts
, i
, su
)
9735 size_t n
= TREE_VEC_LENGTH (su
->vec
);
9737 gimple_switch_set_num_labels (su
->stmt
, n
);
9738 for (j
= 0; j
< n
; j
++)
9739 gimple_switch_set_label (su
->stmt
, j
, TREE_VEC_ELT (su
->vec
, j
));
9740 /* As we may have replaced the default label with a regular one
9741 make sure to make it a real default label again. This ensures
9742 optimal expansion. */
9743 label
= gimple_switch_label (su
->stmt
, 0);
9744 CASE_LOW (label
) = NULL_TREE
;
9745 CASE_HIGH (label
) = NULL_TREE
;
9748 if (to_remove_edges
.length () > 0)
9750 free_dominance_info (CDI_DOMINATORS
);
9752 loops_state_set (LOOPS_NEED_FIXUP
);
9755 to_remove_edges
.release ();
9756 to_update_switch_stmts
.release ();
9757 threadedge_finalize_values ();
9760 loop_optimizer_finalize ();
9767 return flag_tree_vrp
!= 0;
9772 const pass_data pass_data_vrp
=
9774 GIMPLE_PASS
, /* type */
9776 OPTGROUP_NONE
, /* optinfo_flags */
9777 true, /* has_gate */
9778 true, /* has_execute */
9779 TV_TREE_VRP
, /* tv_id */
9780 PROP_ssa
, /* properties_required */
9781 0, /* properties_provided */
9782 0, /* properties_destroyed */
9783 0, /* todo_flags_start */
9784 ( TODO_cleanup_cfg
| TODO_update_ssa
9786 | TODO_verify_flow
), /* todo_flags_finish */
9789 class pass_vrp
: public gimple_opt_pass
9792 pass_vrp (gcc::context
*ctxt
)
9793 : gimple_opt_pass (pass_data_vrp
, ctxt
)
9796 /* opt_pass methods: */
9797 opt_pass
* clone () { return new pass_vrp (m_ctxt
); }
9798 bool gate () { return gate_vrp (); }
9799 unsigned int execute () { return execute_vrp (); }
9801 }; // class pass_vrp
9806 make_pass_vrp (gcc::context
*ctxt
)
9808 return new pass_vrp (ctxt
);