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"
29 #include "tree-flow.h"
30 #include "tree-pass.h"
31 #include "tree-dump.h"
32 #include "gimple-pretty-print.h"
33 #include "diagnostic-core.h"
36 #include "tree-scalar-evolution.h"
37 #include "tree-ssa-propagate.h"
38 #include "tree-chrec.h"
39 #include "gimple-fold.h"
44 /* Type of value ranges. See value_range_d for a description of these
46 enum value_range_type
{ VR_UNDEFINED
, VR_RANGE
, VR_ANTI_RANGE
, VR_VARYING
};
48 /* Range of values that can be associated with an SSA_NAME after VRP
52 /* Lattice value represented by this range. */
53 enum value_range_type type
;
55 /* Minimum and maximum values represented by this range. These
56 values should be interpreted as follows:
58 - If TYPE is VR_UNDEFINED or VR_VARYING then MIN and MAX must
61 - If TYPE == VR_RANGE then MIN holds the minimum value and
62 MAX holds the maximum value of the range [MIN, MAX].
64 - If TYPE == ANTI_RANGE the variable is known to NOT
65 take any values in the range [MIN, MAX]. */
69 /* Set of SSA names whose value ranges are equivalent to this one.
70 This set is only valid when TYPE is VR_RANGE or VR_ANTI_RANGE. */
74 typedef struct value_range_d value_range_t
;
76 #define VR_INITIALIZER { VR_UNDEFINED, NULL_TREE, NULL_TREE, NULL }
78 /* Set of SSA names found live during the RPO traversal of the function
79 for still active basic-blocks. */
82 /* Return true if the SSA name NAME is live on the edge E. */
85 live_on_edge (edge e
, tree name
)
87 return (live
[e
->dest
->index
]
88 && bitmap_bit_p (live
[e
->dest
->index
], SSA_NAME_VERSION (name
)));
91 /* Local functions. */
92 static int compare_values (tree val1
, tree val2
);
93 static int compare_values_warnv (tree val1
, tree val2
, bool *);
94 static void vrp_meet (value_range_t
*, value_range_t
*);
95 static void vrp_intersect_ranges (value_range_t
*, value_range_t
*);
96 static tree
vrp_evaluate_conditional_warnv_with_ops (enum tree_code
,
97 tree
, tree
, bool, bool *,
100 /* Location information for ASSERT_EXPRs. Each instance of this
101 structure describes an ASSERT_EXPR for an SSA name. Since a single
102 SSA name may have more than one assertion associated with it, these
103 locations are kept in a linked list attached to the corresponding
105 struct assert_locus_d
107 /* Basic block where the assertion would be inserted. */
110 /* Some assertions need to be inserted on an edge (e.g., assertions
111 generated by COND_EXPRs). In those cases, BB will be NULL. */
114 /* Pointer to the statement that generated this assertion. */
115 gimple_stmt_iterator si
;
117 /* Predicate code for the ASSERT_EXPR. Must be COMPARISON_CLASS_P. */
118 enum tree_code comp_code
;
120 /* Value being compared against. */
123 /* Expression to compare. */
126 /* Next node in the linked list. */
127 struct assert_locus_d
*next
;
130 typedef struct assert_locus_d
*assert_locus_t
;
132 /* If bit I is present, it means that SSA name N_i has a list of
133 assertions that should be inserted in the IL. */
134 static bitmap need_assert_for
;
136 /* Array of locations lists where to insert assertions. ASSERTS_FOR[I]
137 holds a list of ASSERT_LOCUS_T nodes that describe where
138 ASSERT_EXPRs for SSA name N_I should be inserted. */
139 static assert_locus_t
*asserts_for
;
141 /* Value range array. After propagation, VR_VALUE[I] holds the range
142 of values that SSA name N_I may take. */
143 static unsigned num_vr_values
;
144 static value_range_t
**vr_value
;
145 static bool values_propagated
;
147 /* For a PHI node which sets SSA name N_I, VR_COUNTS[I] holds the
148 number of executable edges we saw the last time we visited the
150 static int *vr_phi_edge_counts
;
157 static vec
<edge
> to_remove_edges
;
158 static vec
<switch_update
> to_update_switch_stmts
;
161 /* Return the maximum value for TYPE. */
164 vrp_val_max (const_tree type
)
166 if (!INTEGRAL_TYPE_P (type
))
169 return TYPE_MAX_VALUE (type
);
172 /* Return the minimum value for TYPE. */
175 vrp_val_min (const_tree type
)
177 if (!INTEGRAL_TYPE_P (type
))
180 return TYPE_MIN_VALUE (type
);
183 /* Return whether VAL is equal to the maximum value of its type. This
184 will be true for a positive overflow infinity. We can't do a
185 simple equality comparison with TYPE_MAX_VALUE because C typedefs
186 and Ada subtypes can produce types whose TYPE_MAX_VALUE is not ==
187 to the integer constant with the same value in the type. */
190 vrp_val_is_max (const_tree val
)
192 tree type_max
= vrp_val_max (TREE_TYPE (val
));
193 return (val
== type_max
194 || (type_max
!= NULL_TREE
195 && operand_equal_p (val
, type_max
, 0)));
198 /* Return whether VAL is equal to the minimum value of its type. This
199 will be true for a negative overflow infinity. */
202 vrp_val_is_min (const_tree val
)
204 tree type_min
= vrp_val_min (TREE_TYPE (val
));
205 return (val
== type_min
206 || (type_min
!= NULL_TREE
207 && operand_equal_p (val
, type_min
, 0)));
211 /* Return whether TYPE should use an overflow infinity distinct from
212 TYPE_{MIN,MAX}_VALUE. We use an overflow infinity value to
213 represent a signed overflow during VRP computations. An infinity
214 is distinct from a half-range, which will go from some number to
215 TYPE_{MIN,MAX}_VALUE. */
218 needs_overflow_infinity (const_tree type
)
220 return INTEGRAL_TYPE_P (type
) && !TYPE_OVERFLOW_WRAPS (type
);
223 /* Return whether TYPE can support our overflow infinity
224 representation: we use the TREE_OVERFLOW flag, which only exists
225 for constants. If TYPE doesn't support this, we don't optimize
226 cases which would require signed overflow--we drop them to
230 supports_overflow_infinity (const_tree type
)
232 tree min
= vrp_val_min (type
), max
= vrp_val_max (type
);
233 #ifdef ENABLE_CHECKING
234 gcc_assert (needs_overflow_infinity (type
));
236 return (min
!= NULL_TREE
237 && CONSTANT_CLASS_P (min
)
239 && CONSTANT_CLASS_P (max
));
242 /* VAL is the maximum or minimum value of a type. Return a
243 corresponding overflow infinity. */
246 make_overflow_infinity (tree val
)
248 gcc_checking_assert (val
!= NULL_TREE
&& CONSTANT_CLASS_P (val
));
249 val
= copy_node (val
);
250 TREE_OVERFLOW (val
) = 1;
254 /* Return a negative overflow infinity for TYPE. */
257 negative_overflow_infinity (tree type
)
259 gcc_checking_assert (supports_overflow_infinity (type
));
260 return make_overflow_infinity (vrp_val_min (type
));
263 /* Return a positive overflow infinity for TYPE. */
266 positive_overflow_infinity (tree type
)
268 gcc_checking_assert (supports_overflow_infinity (type
));
269 return make_overflow_infinity (vrp_val_max (type
));
272 /* Return whether VAL is a negative overflow infinity. */
275 is_negative_overflow_infinity (const_tree val
)
277 return (needs_overflow_infinity (TREE_TYPE (val
))
278 && CONSTANT_CLASS_P (val
)
279 && TREE_OVERFLOW (val
)
280 && vrp_val_is_min (val
));
283 /* Return whether VAL is a positive overflow infinity. */
286 is_positive_overflow_infinity (const_tree val
)
288 return (needs_overflow_infinity (TREE_TYPE (val
))
289 && CONSTANT_CLASS_P (val
)
290 && TREE_OVERFLOW (val
)
291 && vrp_val_is_max (val
));
294 /* Return whether VAL is a positive or negative overflow infinity. */
297 is_overflow_infinity (const_tree val
)
299 return (needs_overflow_infinity (TREE_TYPE (val
))
300 && CONSTANT_CLASS_P (val
)
301 && TREE_OVERFLOW (val
)
302 && (vrp_val_is_min (val
) || vrp_val_is_max (val
)));
305 /* Return whether STMT has a constant rhs that is_overflow_infinity. */
308 stmt_overflow_infinity (gimple stmt
)
310 if (is_gimple_assign (stmt
)
311 && get_gimple_rhs_class (gimple_assign_rhs_code (stmt
)) ==
313 return is_overflow_infinity (gimple_assign_rhs1 (stmt
));
317 /* If VAL is now an overflow infinity, return VAL. Otherwise, return
318 the same value with TREE_OVERFLOW clear. This can be used to avoid
319 confusing a regular value with an overflow value. */
322 avoid_overflow_infinity (tree val
)
324 if (!is_overflow_infinity (val
))
327 if (vrp_val_is_max (val
))
328 return vrp_val_max (TREE_TYPE (val
));
331 gcc_checking_assert (vrp_val_is_min (val
));
332 return vrp_val_min (TREE_TYPE (val
));
337 /* Return true if ARG is marked with the nonnull attribute in the
338 current function signature. */
341 nonnull_arg_p (const_tree arg
)
343 tree t
, attrs
, fntype
;
344 unsigned HOST_WIDE_INT arg_num
;
346 gcc_assert (TREE_CODE (arg
) == PARM_DECL
&& POINTER_TYPE_P (TREE_TYPE (arg
)));
348 /* The static chain decl is always non null. */
349 if (arg
== cfun
->static_chain_decl
)
352 fntype
= TREE_TYPE (current_function_decl
);
353 for (attrs
= TYPE_ATTRIBUTES (fntype
); attrs
; attrs
= TREE_CHAIN (attrs
))
355 attrs
= lookup_attribute ("nonnull", attrs
);
357 /* If "nonnull" wasn't specified, we know nothing about the argument. */
358 if (attrs
== NULL_TREE
)
361 /* If "nonnull" applies to all the arguments, then ARG is non-null. */
362 if (TREE_VALUE (attrs
) == NULL_TREE
)
365 /* Get the position number for ARG in the function signature. */
366 for (arg_num
= 1, t
= DECL_ARGUMENTS (current_function_decl
);
368 t
= DECL_CHAIN (t
), arg_num
++)
374 gcc_assert (t
== arg
);
376 /* Now see if ARG_NUM is mentioned in the nonnull list. */
377 for (t
= TREE_VALUE (attrs
); t
; t
= TREE_CHAIN (t
))
379 if (compare_tree_int (TREE_VALUE (t
), arg_num
) == 0)
388 /* Set value range VR to VR_UNDEFINED. */
391 set_value_range_to_undefined (value_range_t
*vr
)
393 vr
->type
= VR_UNDEFINED
;
394 vr
->min
= vr
->max
= NULL_TREE
;
396 bitmap_clear (vr
->equiv
);
400 /* Set value range VR to VR_VARYING. */
403 set_value_range_to_varying (value_range_t
*vr
)
405 vr
->type
= VR_VARYING
;
406 vr
->min
= vr
->max
= NULL_TREE
;
408 bitmap_clear (vr
->equiv
);
412 /* Set value range VR to {T, MIN, MAX, EQUIV}. */
415 set_value_range (value_range_t
*vr
, enum value_range_type t
, tree min
,
416 tree max
, bitmap equiv
)
418 #if defined ENABLE_CHECKING
419 /* Check the validity of the range. */
420 if (t
== VR_RANGE
|| t
== VR_ANTI_RANGE
)
424 gcc_assert (min
&& max
);
426 if (INTEGRAL_TYPE_P (TREE_TYPE (min
)) && t
== VR_ANTI_RANGE
)
427 gcc_assert (!vrp_val_is_min (min
) || !vrp_val_is_max (max
));
429 cmp
= compare_values (min
, max
);
430 gcc_assert (cmp
== 0 || cmp
== -1 || cmp
== -2);
432 if (needs_overflow_infinity (TREE_TYPE (min
)))
433 gcc_assert (!is_overflow_infinity (min
)
434 || !is_overflow_infinity (max
));
437 if (t
== VR_UNDEFINED
|| t
== VR_VARYING
)
438 gcc_assert (min
== NULL_TREE
&& max
== NULL_TREE
);
440 if (t
== VR_UNDEFINED
|| t
== VR_VARYING
)
441 gcc_assert (equiv
== NULL
|| bitmap_empty_p (equiv
));
448 /* Since updating the equivalence set involves deep copying the
449 bitmaps, only do it if absolutely necessary. */
450 if (vr
->equiv
== NULL
452 vr
->equiv
= BITMAP_ALLOC (NULL
);
454 if (equiv
!= vr
->equiv
)
456 if (equiv
&& !bitmap_empty_p (equiv
))
457 bitmap_copy (vr
->equiv
, equiv
);
459 bitmap_clear (vr
->equiv
);
464 /* Set value range VR to the canonical form of {T, MIN, MAX, EQUIV}.
465 This means adjusting T, MIN and MAX representing the case of a
466 wrapping range with MAX < MIN covering [MIN, type_max] U [type_min, MAX]
467 as anti-rage ~[MAX+1, MIN-1]. Likewise for wrapping anti-ranges.
468 In corner cases where MAX+1 or MIN-1 wraps this will fall back
470 This routine exists to ease canonicalization in the case where we
471 extract ranges from var + CST op limit. */
474 set_and_canonicalize_value_range (value_range_t
*vr
, enum value_range_type t
,
475 tree min
, tree max
, bitmap equiv
)
477 /* Use the canonical setters for VR_UNDEFINED and VR_VARYING. */
478 if (t
== VR_UNDEFINED
)
480 set_value_range_to_undefined (vr
);
483 else if (t
== VR_VARYING
)
485 set_value_range_to_varying (vr
);
489 /* Nothing to canonicalize for symbolic ranges. */
490 if (TREE_CODE (min
) != INTEGER_CST
491 || TREE_CODE (max
) != INTEGER_CST
)
493 set_value_range (vr
, t
, min
, max
, equiv
);
497 /* Wrong order for min and max, to swap them and the VR type we need
499 if (tree_int_cst_lt (max
, min
))
503 /* For one bit precision if max < min, then the swapped
504 range covers all values, so for VR_RANGE it is varying and
505 for VR_ANTI_RANGE empty range, so drop to varying as well. */
506 if (TYPE_PRECISION (TREE_TYPE (min
)) == 1)
508 set_value_range_to_varying (vr
);
512 one
= build_int_cst (TREE_TYPE (min
), 1);
513 tmp
= int_const_binop (PLUS_EXPR
, max
, one
);
514 max
= int_const_binop (MINUS_EXPR
, min
, one
);
517 /* There's one corner case, if we had [C+1, C] before we now have
518 that again. But this represents an empty value range, so drop
519 to varying in this case. */
520 if (tree_int_cst_lt (max
, min
))
522 set_value_range_to_varying (vr
);
526 t
= t
== VR_RANGE
? VR_ANTI_RANGE
: VR_RANGE
;
529 /* Anti-ranges that can be represented as ranges should be so. */
530 if (t
== VR_ANTI_RANGE
)
532 bool is_min
= vrp_val_is_min (min
);
533 bool is_max
= vrp_val_is_max (max
);
535 if (is_min
&& is_max
)
537 /* We cannot deal with empty ranges, drop to varying.
538 ??? This could be VR_UNDEFINED instead. */
539 set_value_range_to_varying (vr
);
542 else if (TYPE_PRECISION (TREE_TYPE (min
)) == 1
543 && (is_min
|| is_max
))
545 /* Non-empty boolean ranges can always be represented
546 as a singleton range. */
548 min
= max
= vrp_val_max (TREE_TYPE (min
));
550 min
= max
= vrp_val_min (TREE_TYPE (min
));
554 /* As a special exception preserve non-null ranges. */
555 && !(TYPE_UNSIGNED (TREE_TYPE (min
))
556 && integer_zerop (max
)))
558 tree one
= build_int_cst (TREE_TYPE (max
), 1);
559 min
= int_const_binop (PLUS_EXPR
, max
, one
);
560 max
= vrp_val_max (TREE_TYPE (max
));
565 tree one
= build_int_cst (TREE_TYPE (min
), 1);
566 max
= int_const_binop (MINUS_EXPR
, min
, one
);
567 min
= vrp_val_min (TREE_TYPE (min
));
572 /* Drop [-INF(OVF), +INF(OVF)] to varying. */
573 if (needs_overflow_infinity (TREE_TYPE (min
))
574 && is_overflow_infinity (min
)
575 && is_overflow_infinity (max
))
577 set_value_range_to_varying (vr
);
581 set_value_range (vr
, t
, min
, max
, equiv
);
584 /* Copy value range FROM into value range TO. */
587 copy_value_range (value_range_t
*to
, value_range_t
*from
)
589 set_value_range (to
, from
->type
, from
->min
, from
->max
, from
->equiv
);
592 /* Set value range VR to a single value. This function is only called
593 with values we get from statements, and exists to clear the
594 TREE_OVERFLOW flag so that we don't think we have an overflow
595 infinity when we shouldn't. */
598 set_value_range_to_value (value_range_t
*vr
, tree val
, bitmap equiv
)
600 gcc_assert (is_gimple_min_invariant (val
));
601 val
= avoid_overflow_infinity (val
);
602 set_value_range (vr
, VR_RANGE
, val
, val
, equiv
);
605 /* Set value range VR to a non-negative range of type TYPE.
606 OVERFLOW_INFINITY indicates whether to use an overflow infinity
607 rather than TYPE_MAX_VALUE; this should be true if we determine
608 that the range is nonnegative based on the assumption that signed
609 overflow does not occur. */
612 set_value_range_to_nonnegative (value_range_t
*vr
, tree type
,
613 bool overflow_infinity
)
617 if (overflow_infinity
&& !supports_overflow_infinity (type
))
619 set_value_range_to_varying (vr
);
623 zero
= build_int_cst (type
, 0);
624 set_value_range (vr
, VR_RANGE
, zero
,
626 ? positive_overflow_infinity (type
)
627 : TYPE_MAX_VALUE (type
)),
631 /* Set value range VR to a non-NULL range of type TYPE. */
634 set_value_range_to_nonnull (value_range_t
*vr
, tree type
)
636 tree zero
= build_int_cst (type
, 0);
637 set_value_range (vr
, VR_ANTI_RANGE
, zero
, zero
, vr
->equiv
);
641 /* Set value range VR to a NULL range of type TYPE. */
644 set_value_range_to_null (value_range_t
*vr
, tree type
)
646 set_value_range_to_value (vr
, build_int_cst (type
, 0), vr
->equiv
);
650 /* Set value range VR to a range of a truthvalue of type TYPE. */
653 set_value_range_to_truthvalue (value_range_t
*vr
, tree type
)
655 if (TYPE_PRECISION (type
) == 1)
656 set_value_range_to_varying (vr
);
658 set_value_range (vr
, VR_RANGE
,
659 build_int_cst (type
, 0), build_int_cst (type
, 1),
664 /* If abs (min) < abs (max), set VR to [-max, max], if
665 abs (min) >= abs (max), set VR to [-min, min]. */
668 abs_extent_range (value_range_t
*vr
, tree min
, tree max
)
672 gcc_assert (TREE_CODE (min
) == INTEGER_CST
);
673 gcc_assert (TREE_CODE (max
) == INTEGER_CST
);
674 gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (min
)));
675 gcc_assert (!TYPE_UNSIGNED (TREE_TYPE (min
)));
676 min
= fold_unary (ABS_EXPR
, TREE_TYPE (min
), min
);
677 max
= fold_unary (ABS_EXPR
, TREE_TYPE (max
), max
);
678 if (TREE_OVERFLOW (min
) || TREE_OVERFLOW (max
))
680 set_value_range_to_varying (vr
);
683 cmp
= compare_values (min
, max
);
685 min
= fold_unary (NEGATE_EXPR
, TREE_TYPE (min
), max
);
686 else if (cmp
== 0 || cmp
== 1)
689 min
= fold_unary (NEGATE_EXPR
, TREE_TYPE (min
), min
);
693 set_value_range_to_varying (vr
);
696 set_and_canonicalize_value_range (vr
, VR_RANGE
, min
, max
, NULL
);
700 /* Return value range information for VAR.
702 If we have no values ranges recorded (ie, VRP is not running), then
703 return NULL. Otherwise create an empty range if none existed for VAR. */
705 static value_range_t
*
706 get_value_range (const_tree var
)
708 static const struct value_range_d vr_const_varying
709 = { VR_VARYING
, NULL_TREE
, NULL_TREE
, NULL
};
712 unsigned ver
= SSA_NAME_VERSION (var
);
714 /* If we have no recorded ranges, then return NULL. */
718 /* If we query the range for a new SSA name return an unmodifiable VARYING.
719 We should get here at most from the substitute-and-fold stage which
720 will never try to change values. */
721 if (ver
>= num_vr_values
)
722 return CONST_CAST (value_range_t
*, &vr_const_varying
);
728 /* After propagation finished do not allocate new value-ranges. */
729 if (values_propagated
)
730 return CONST_CAST (value_range_t
*, &vr_const_varying
);
732 /* Create a default value range. */
733 vr_value
[ver
] = vr
= XCNEW (value_range_t
);
735 /* Defer allocating the equivalence set. */
738 /* If VAR is a default definition of a parameter, the variable can
739 take any value in VAR's type. */
740 if (SSA_NAME_IS_DEFAULT_DEF (var
))
742 sym
= SSA_NAME_VAR (var
);
743 if (TREE_CODE (sym
) == PARM_DECL
)
745 /* Try to use the "nonnull" attribute to create ~[0, 0]
746 anti-ranges for pointers. Note that this is only valid with
747 default definitions of PARM_DECLs. */
748 if (POINTER_TYPE_P (TREE_TYPE (sym
))
749 && nonnull_arg_p (sym
))
750 set_value_range_to_nonnull (vr
, TREE_TYPE (sym
));
752 set_value_range_to_varying (vr
);
754 else if (TREE_CODE (sym
) == RESULT_DECL
755 && DECL_BY_REFERENCE (sym
))
756 set_value_range_to_nonnull (vr
, TREE_TYPE (sym
));
762 /* Return true, if VAL1 and VAL2 are equal values for VRP purposes. */
765 vrp_operand_equal_p (const_tree val1
, const_tree val2
)
769 if (!val1
|| !val2
|| !operand_equal_p (val1
, val2
, 0))
771 if (is_overflow_infinity (val1
))
772 return is_overflow_infinity (val2
);
776 /* Return true, if the bitmaps B1 and B2 are equal. */
779 vrp_bitmap_equal_p (const_bitmap b1
, const_bitmap b2
)
782 || ((!b1
|| bitmap_empty_p (b1
))
783 && (!b2
|| bitmap_empty_p (b2
)))
785 && bitmap_equal_p (b1
, b2
)));
788 /* Update the value range and equivalence set for variable VAR to
789 NEW_VR. Return true if NEW_VR is different from VAR's previous
792 NOTE: This function assumes that NEW_VR is a temporary value range
793 object created for the sole purpose of updating VAR's range. The
794 storage used by the equivalence set from NEW_VR will be freed by
795 this function. Do not call update_value_range when NEW_VR
796 is the range object associated with another SSA name. */
799 update_value_range (const_tree var
, value_range_t
*new_vr
)
801 value_range_t
*old_vr
;
804 /* Update the value range, if necessary. */
805 old_vr
= get_value_range (var
);
806 is_new
= old_vr
->type
!= new_vr
->type
807 || !vrp_operand_equal_p (old_vr
->min
, new_vr
->min
)
808 || !vrp_operand_equal_p (old_vr
->max
, new_vr
->max
)
809 || !vrp_bitmap_equal_p (old_vr
->equiv
, new_vr
->equiv
);
813 /* Do not allow transitions up the lattice. The following
814 is slightly more awkward than just new_vr->type < old_vr->type
815 because VR_RANGE and VR_ANTI_RANGE need to be considered
816 the same. We may not have is_new when transitioning to
817 UNDEFINED or from VARYING. */
818 if (new_vr
->type
== VR_UNDEFINED
819 || old_vr
->type
== VR_VARYING
)
820 set_value_range_to_varying (old_vr
);
822 set_value_range (old_vr
, new_vr
->type
, new_vr
->min
, new_vr
->max
,
826 BITMAP_FREE (new_vr
->equiv
);
832 /* Add VAR and VAR's equivalence set to EQUIV. This is the central
833 point where equivalence processing can be turned on/off. */
836 add_equivalence (bitmap
*equiv
, const_tree var
)
838 unsigned ver
= SSA_NAME_VERSION (var
);
839 value_range_t
*vr
= vr_value
[ver
];
842 *equiv
= BITMAP_ALLOC (NULL
);
843 bitmap_set_bit (*equiv
, ver
);
845 bitmap_ior_into (*equiv
, vr
->equiv
);
849 /* Return true if VR is ~[0, 0]. */
852 range_is_nonnull (value_range_t
*vr
)
854 return vr
->type
== VR_ANTI_RANGE
855 && integer_zerop (vr
->min
)
856 && integer_zerop (vr
->max
);
860 /* Return true if VR is [0, 0]. */
863 range_is_null (value_range_t
*vr
)
865 return vr
->type
== VR_RANGE
866 && integer_zerop (vr
->min
)
867 && integer_zerop (vr
->max
);
870 /* Return true if max and min of VR are INTEGER_CST. It's not necessary
874 range_int_cst_p (value_range_t
*vr
)
876 return (vr
->type
== VR_RANGE
877 && TREE_CODE (vr
->max
) == INTEGER_CST
878 && TREE_CODE (vr
->min
) == INTEGER_CST
);
881 /* Return true if VR is a INTEGER_CST singleton. */
884 range_int_cst_singleton_p (value_range_t
*vr
)
886 return (range_int_cst_p (vr
)
887 && !TREE_OVERFLOW (vr
->min
)
888 && !TREE_OVERFLOW (vr
->max
)
889 && tree_int_cst_equal (vr
->min
, vr
->max
));
892 /* Return true if value range VR involves at least one symbol. */
895 symbolic_range_p (value_range_t
*vr
)
897 return (!is_gimple_min_invariant (vr
->min
)
898 || !is_gimple_min_invariant (vr
->max
));
901 /* Return true if value range VR uses an overflow infinity. */
904 overflow_infinity_range_p (value_range_t
*vr
)
906 return (vr
->type
== VR_RANGE
907 && (is_overflow_infinity (vr
->min
)
908 || is_overflow_infinity (vr
->max
)));
911 /* Return false if we can not make a valid comparison based on VR;
912 this will be the case if it uses an overflow infinity and overflow
913 is not undefined (i.e., -fno-strict-overflow is in effect).
914 Otherwise return true, and set *STRICT_OVERFLOW_P to true if VR
915 uses an overflow infinity. */
918 usable_range_p (value_range_t
*vr
, bool *strict_overflow_p
)
920 gcc_assert (vr
->type
== VR_RANGE
);
921 if (is_overflow_infinity (vr
->min
))
923 *strict_overflow_p
= true;
924 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (vr
->min
)))
927 if (is_overflow_infinity (vr
->max
))
929 *strict_overflow_p
= true;
930 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (vr
->max
)))
937 /* Return true if the result of assignment STMT is know to be non-negative.
938 If the return value is based on the assumption that signed overflow is
939 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
940 *STRICT_OVERFLOW_P.*/
943 gimple_assign_nonnegative_warnv_p (gimple stmt
, bool *strict_overflow_p
)
945 enum tree_code code
= gimple_assign_rhs_code (stmt
);
946 switch (get_gimple_rhs_class (code
))
948 case GIMPLE_UNARY_RHS
:
949 return tree_unary_nonnegative_warnv_p (gimple_assign_rhs_code (stmt
),
950 gimple_expr_type (stmt
),
951 gimple_assign_rhs1 (stmt
),
953 case GIMPLE_BINARY_RHS
:
954 return tree_binary_nonnegative_warnv_p (gimple_assign_rhs_code (stmt
),
955 gimple_expr_type (stmt
),
956 gimple_assign_rhs1 (stmt
),
957 gimple_assign_rhs2 (stmt
),
959 case GIMPLE_TERNARY_RHS
:
961 case GIMPLE_SINGLE_RHS
:
962 return tree_single_nonnegative_warnv_p (gimple_assign_rhs1 (stmt
),
964 case GIMPLE_INVALID_RHS
:
971 /* Return true if return value of call STMT is know to be non-negative.
972 If the return value is based on the assumption that signed overflow is
973 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
974 *STRICT_OVERFLOW_P.*/
977 gimple_call_nonnegative_warnv_p (gimple stmt
, bool *strict_overflow_p
)
979 tree arg0
= gimple_call_num_args (stmt
) > 0 ?
980 gimple_call_arg (stmt
, 0) : NULL_TREE
;
981 tree arg1
= gimple_call_num_args (stmt
) > 1 ?
982 gimple_call_arg (stmt
, 1) : NULL_TREE
;
984 return tree_call_nonnegative_warnv_p (gimple_expr_type (stmt
),
985 gimple_call_fndecl (stmt
),
991 /* Return true if STMT is know to to compute a non-negative value.
992 If the return value is based on the assumption that signed overflow is
993 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
994 *STRICT_OVERFLOW_P.*/
997 gimple_stmt_nonnegative_warnv_p (gimple stmt
, bool *strict_overflow_p
)
999 switch (gimple_code (stmt
))
1002 return gimple_assign_nonnegative_warnv_p (stmt
, strict_overflow_p
);
1004 return gimple_call_nonnegative_warnv_p (stmt
, strict_overflow_p
);
1010 /* Return true if the result of assignment STMT is know to be non-zero.
1011 If the return value is based on the assumption that signed overflow is
1012 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1013 *STRICT_OVERFLOW_P.*/
1016 gimple_assign_nonzero_warnv_p (gimple stmt
, bool *strict_overflow_p
)
1018 enum tree_code code
= gimple_assign_rhs_code (stmt
);
1019 switch (get_gimple_rhs_class (code
))
1021 case GIMPLE_UNARY_RHS
:
1022 return tree_unary_nonzero_warnv_p (gimple_assign_rhs_code (stmt
),
1023 gimple_expr_type (stmt
),
1024 gimple_assign_rhs1 (stmt
),
1026 case GIMPLE_BINARY_RHS
:
1027 return tree_binary_nonzero_warnv_p (gimple_assign_rhs_code (stmt
),
1028 gimple_expr_type (stmt
),
1029 gimple_assign_rhs1 (stmt
),
1030 gimple_assign_rhs2 (stmt
),
1032 case GIMPLE_TERNARY_RHS
:
1034 case GIMPLE_SINGLE_RHS
:
1035 return tree_single_nonzero_warnv_p (gimple_assign_rhs1 (stmt
),
1037 case GIMPLE_INVALID_RHS
:
1044 /* Return true if STMT is know to to compute a non-zero value.
1045 If the return value is based on the assumption that signed overflow is
1046 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1047 *STRICT_OVERFLOW_P.*/
1050 gimple_stmt_nonzero_warnv_p (gimple stmt
, bool *strict_overflow_p
)
1052 switch (gimple_code (stmt
))
1055 return gimple_assign_nonzero_warnv_p (stmt
, strict_overflow_p
);
1057 return gimple_alloca_call_p (stmt
);
1063 /* Like tree_expr_nonzero_warnv_p, but this function uses value ranges
1067 vrp_stmt_computes_nonzero (gimple stmt
, bool *strict_overflow_p
)
1069 if (gimple_stmt_nonzero_warnv_p (stmt
, strict_overflow_p
))
1072 /* If we have an expression of the form &X->a, then the expression
1073 is nonnull if X is nonnull. */
1074 if (is_gimple_assign (stmt
)
1075 && gimple_assign_rhs_code (stmt
) == ADDR_EXPR
)
1077 tree expr
= gimple_assign_rhs1 (stmt
);
1078 tree base
= get_base_address (TREE_OPERAND (expr
, 0));
1080 if (base
!= NULL_TREE
1081 && TREE_CODE (base
) == MEM_REF
1082 && TREE_CODE (TREE_OPERAND (base
, 0)) == SSA_NAME
)
1084 value_range_t
*vr
= get_value_range (TREE_OPERAND (base
, 0));
1085 if (range_is_nonnull (vr
))
1093 /* Returns true if EXPR is a valid value (as expected by compare_values) --
1094 a gimple invariant, or SSA_NAME +- CST. */
1097 valid_value_p (tree expr
)
1099 if (TREE_CODE (expr
) == SSA_NAME
)
1102 if (TREE_CODE (expr
) == PLUS_EXPR
1103 || TREE_CODE (expr
) == MINUS_EXPR
)
1104 return (TREE_CODE (TREE_OPERAND (expr
, 0)) == SSA_NAME
1105 && TREE_CODE (TREE_OPERAND (expr
, 1)) == INTEGER_CST
);
1107 return is_gimple_min_invariant (expr
);
1113 -2 if those are incomparable. */
1115 operand_less_p (tree val
, tree val2
)
1117 /* LT is folded faster than GE and others. Inline the common case. */
1118 if (TREE_CODE (val
) == INTEGER_CST
&& TREE_CODE (val2
) == INTEGER_CST
)
1120 if (TYPE_UNSIGNED (TREE_TYPE (val
)))
1121 return INT_CST_LT_UNSIGNED (val
, val2
);
1124 if (INT_CST_LT (val
, val2
))
1132 fold_defer_overflow_warnings ();
1134 tcmp
= fold_binary_to_constant (LT_EXPR
, boolean_type_node
, val
, val2
);
1136 fold_undefer_and_ignore_overflow_warnings ();
1139 || TREE_CODE (tcmp
) != INTEGER_CST
)
1142 if (!integer_zerop (tcmp
))
1146 /* val >= val2, not considering overflow infinity. */
1147 if (is_negative_overflow_infinity (val
))
1148 return is_negative_overflow_infinity (val2
) ? 0 : 1;
1149 else if (is_positive_overflow_infinity (val2
))
1150 return is_positive_overflow_infinity (val
) ? 0 : 1;
1155 /* Compare two values VAL1 and VAL2. Return
1157 -2 if VAL1 and VAL2 cannot be compared at compile-time,
1160 +1 if VAL1 > VAL2, and
1163 This is similar to tree_int_cst_compare but supports pointer values
1164 and values that cannot be compared at compile time.
1166 If STRICT_OVERFLOW_P is not NULL, then set *STRICT_OVERFLOW_P to
1167 true if the return value is only valid if we assume that signed
1168 overflow is undefined. */
1171 compare_values_warnv (tree val1
, tree val2
, bool *strict_overflow_p
)
1176 /* Below we rely on the fact that VAL1 and VAL2 are both pointers or
1178 gcc_assert (POINTER_TYPE_P (TREE_TYPE (val1
))
1179 == POINTER_TYPE_P (TREE_TYPE (val2
)));
1180 /* Convert the two values into the same type. This is needed because
1181 sizetype causes sign extension even for unsigned types. */
1182 val2
= fold_convert (TREE_TYPE (val1
), val2
);
1183 STRIP_USELESS_TYPE_CONVERSION (val2
);
1185 if ((TREE_CODE (val1
) == SSA_NAME
1186 || TREE_CODE (val1
) == PLUS_EXPR
1187 || TREE_CODE (val1
) == MINUS_EXPR
)
1188 && (TREE_CODE (val2
) == SSA_NAME
1189 || TREE_CODE (val2
) == PLUS_EXPR
1190 || TREE_CODE (val2
) == MINUS_EXPR
))
1192 tree n1
, c1
, n2
, c2
;
1193 enum tree_code code1
, code2
;
1195 /* If VAL1 and VAL2 are of the form 'NAME [+-] CST' or 'NAME',
1196 return -1 or +1 accordingly. If VAL1 and VAL2 don't use the
1197 same name, return -2. */
1198 if (TREE_CODE (val1
) == SSA_NAME
)
1206 code1
= TREE_CODE (val1
);
1207 n1
= TREE_OPERAND (val1
, 0);
1208 c1
= TREE_OPERAND (val1
, 1);
1209 if (tree_int_cst_sgn (c1
) == -1)
1211 if (is_negative_overflow_infinity (c1
))
1213 c1
= fold_unary_to_constant (NEGATE_EXPR
, TREE_TYPE (c1
), c1
);
1216 code1
= code1
== MINUS_EXPR
? PLUS_EXPR
: MINUS_EXPR
;
1220 if (TREE_CODE (val2
) == SSA_NAME
)
1228 code2
= TREE_CODE (val2
);
1229 n2
= TREE_OPERAND (val2
, 0);
1230 c2
= TREE_OPERAND (val2
, 1);
1231 if (tree_int_cst_sgn (c2
) == -1)
1233 if (is_negative_overflow_infinity (c2
))
1235 c2
= fold_unary_to_constant (NEGATE_EXPR
, TREE_TYPE (c2
), c2
);
1238 code2
= code2
== MINUS_EXPR
? PLUS_EXPR
: MINUS_EXPR
;
1242 /* Both values must use the same name. */
1246 if (code1
== SSA_NAME
1247 && code2
== SSA_NAME
)
1251 /* If overflow is defined we cannot simplify more. */
1252 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (val1
)))
1255 if (strict_overflow_p
!= NULL
1256 && (code1
== SSA_NAME
|| !TREE_NO_WARNING (val1
))
1257 && (code2
== SSA_NAME
|| !TREE_NO_WARNING (val2
)))
1258 *strict_overflow_p
= true;
1260 if (code1
== SSA_NAME
)
1262 if (code2
== PLUS_EXPR
)
1263 /* NAME < NAME + CST */
1265 else if (code2
== MINUS_EXPR
)
1266 /* NAME > NAME - CST */
1269 else if (code1
== PLUS_EXPR
)
1271 if (code2
== SSA_NAME
)
1272 /* NAME + CST > NAME */
1274 else if (code2
== PLUS_EXPR
)
1275 /* NAME + CST1 > NAME + CST2, if CST1 > CST2 */
1276 return compare_values_warnv (c1
, c2
, strict_overflow_p
);
1277 else if (code2
== MINUS_EXPR
)
1278 /* NAME + CST1 > NAME - CST2 */
1281 else if (code1
== MINUS_EXPR
)
1283 if (code2
== SSA_NAME
)
1284 /* NAME - CST < NAME */
1286 else if (code2
== PLUS_EXPR
)
1287 /* NAME - CST1 < NAME + CST2 */
1289 else if (code2
== MINUS_EXPR
)
1290 /* NAME - CST1 > NAME - CST2, if CST1 < CST2. Notice that
1291 C1 and C2 are swapped in the call to compare_values. */
1292 return compare_values_warnv (c2
, c1
, strict_overflow_p
);
1298 /* We cannot compare non-constants. */
1299 if (!is_gimple_min_invariant (val1
) || !is_gimple_min_invariant (val2
))
1302 if (!POINTER_TYPE_P (TREE_TYPE (val1
)))
1304 /* We cannot compare overflowed values, except for overflow
1306 if (TREE_OVERFLOW (val1
) || TREE_OVERFLOW (val2
))
1308 if (strict_overflow_p
!= NULL
)
1309 *strict_overflow_p
= true;
1310 if (is_negative_overflow_infinity (val1
))
1311 return is_negative_overflow_infinity (val2
) ? 0 : -1;
1312 else if (is_negative_overflow_infinity (val2
))
1314 else if (is_positive_overflow_infinity (val1
))
1315 return is_positive_overflow_infinity (val2
) ? 0 : 1;
1316 else if (is_positive_overflow_infinity (val2
))
1321 return tree_int_cst_compare (val1
, val2
);
1327 /* First see if VAL1 and VAL2 are not the same. */
1328 if (val1
== val2
|| operand_equal_p (val1
, val2
, 0))
1331 /* If VAL1 is a lower address than VAL2, return -1. */
1332 if (operand_less_p (val1
, val2
) == 1)
1335 /* If VAL1 is a higher address than VAL2, return +1. */
1336 if (operand_less_p (val2
, val1
) == 1)
1339 /* If VAL1 is different than VAL2, return +2.
1340 For integer constants we either have already returned -1 or 1
1341 or they are equivalent. We still might succeed in proving
1342 something about non-trivial operands. */
1343 if (TREE_CODE (val1
) != INTEGER_CST
1344 || TREE_CODE (val2
) != INTEGER_CST
)
1346 t
= fold_binary_to_constant (NE_EXPR
, boolean_type_node
, val1
, val2
);
1347 if (t
&& integer_onep (t
))
1355 /* Compare values like compare_values_warnv, but treat comparisons of
1356 nonconstants which rely on undefined overflow as incomparable. */
1359 compare_values (tree val1
, tree val2
)
1365 ret
= compare_values_warnv (val1
, val2
, &sop
);
1367 && (!is_gimple_min_invariant (val1
) || !is_gimple_min_invariant (val2
)))
1373 /* Return 1 if VAL is inside value range MIN <= VAL <= MAX,
1374 0 if VAL is not inside [MIN, MAX],
1375 -2 if we cannot tell either way.
1377 Benchmark compile/20001226-1.c compilation time after changing this
1381 value_inside_range (tree val
, tree min
, tree max
)
1385 cmp1
= operand_less_p (val
, min
);
1391 cmp2
= operand_less_p (max
, val
);
1399 /* Return true if value ranges VR0 and VR1 have a non-empty
1402 Benchmark compile/20001226-1.c compilation time after changing this
1407 value_ranges_intersect_p (value_range_t
*vr0
, value_range_t
*vr1
)
1409 /* The value ranges do not intersect if the maximum of the first range is
1410 less than the minimum of the second range or vice versa.
1411 When those relations are unknown, we can't do any better. */
1412 if (operand_less_p (vr0
->max
, vr1
->min
) != 0)
1414 if (operand_less_p (vr1
->max
, vr0
->min
) != 0)
1420 /* Return 1 if [MIN, MAX] includes the value zero, 0 if it does not
1421 include the value zero, -2 if we cannot tell. */
1424 range_includes_zero_p (tree min
, tree max
)
1426 tree zero
= build_int_cst (TREE_TYPE (min
), 0);
1427 return value_inside_range (zero
, min
, max
);
1430 /* Return true if *VR is know to only contain nonnegative values. */
1433 value_range_nonnegative_p (value_range_t
*vr
)
1435 /* Testing for VR_ANTI_RANGE is not useful here as any anti-range
1436 which would return a useful value should be encoded as a
1438 if (vr
->type
== VR_RANGE
)
1440 int result
= compare_values (vr
->min
, integer_zero_node
);
1441 return (result
== 0 || result
== 1);
1447 /* Return true if T, an SSA_NAME, is known to be nonnegative. Return
1448 false otherwise or if no value range information is available. */
1451 ssa_name_nonnegative_p (const_tree t
)
1453 value_range_t
*vr
= get_value_range (t
);
1455 if (INTEGRAL_TYPE_P (t
)
1456 && TYPE_UNSIGNED (t
))
1462 return value_range_nonnegative_p (vr
);
1465 /* If *VR has a value rante that is a single constant value return that,
1466 otherwise return NULL_TREE. */
1469 value_range_constant_singleton (value_range_t
*vr
)
1471 if (vr
->type
== VR_RANGE
1472 && operand_equal_p (vr
->min
, vr
->max
, 0)
1473 && is_gimple_min_invariant (vr
->min
))
1479 /* If OP has a value range with a single constant value return that,
1480 otherwise return NULL_TREE. This returns OP itself if OP is a
1484 op_with_constant_singleton_value_range (tree op
)
1486 if (is_gimple_min_invariant (op
))
1489 if (TREE_CODE (op
) != SSA_NAME
)
1492 return value_range_constant_singleton (get_value_range (op
));
1495 /* Return true if op is in a boolean [0, 1] value-range. */
1498 op_with_boolean_value_range_p (tree op
)
1502 if (TYPE_PRECISION (TREE_TYPE (op
)) == 1)
1505 if (integer_zerop (op
)
1506 || integer_onep (op
))
1509 if (TREE_CODE (op
) != SSA_NAME
)
1512 vr
= get_value_range (op
);
1513 return (vr
->type
== VR_RANGE
1514 && integer_zerop (vr
->min
)
1515 && integer_onep (vr
->max
));
1518 /* Extract value range information from an ASSERT_EXPR EXPR and store
1522 extract_range_from_assert (value_range_t
*vr_p
, tree expr
)
1524 tree var
, cond
, limit
, min
, max
, type
;
1525 value_range_t
*limit_vr
;
1526 enum tree_code cond_code
;
1528 var
= ASSERT_EXPR_VAR (expr
);
1529 cond
= ASSERT_EXPR_COND (expr
);
1531 gcc_assert (COMPARISON_CLASS_P (cond
));
1533 /* Find VAR in the ASSERT_EXPR conditional. */
1534 if (var
== TREE_OPERAND (cond
, 0)
1535 || TREE_CODE (TREE_OPERAND (cond
, 0)) == PLUS_EXPR
1536 || TREE_CODE (TREE_OPERAND (cond
, 0)) == NOP_EXPR
)
1538 /* If the predicate is of the form VAR COMP LIMIT, then we just
1539 take LIMIT from the RHS and use the same comparison code. */
1540 cond_code
= TREE_CODE (cond
);
1541 limit
= TREE_OPERAND (cond
, 1);
1542 cond
= TREE_OPERAND (cond
, 0);
1546 /* If the predicate is of the form LIMIT COMP VAR, then we need
1547 to flip around the comparison code to create the proper range
1549 cond_code
= swap_tree_comparison (TREE_CODE (cond
));
1550 limit
= TREE_OPERAND (cond
, 0);
1551 cond
= TREE_OPERAND (cond
, 1);
1554 limit
= avoid_overflow_infinity (limit
);
1556 type
= TREE_TYPE (var
);
1557 gcc_assert (limit
!= var
);
1559 /* For pointer arithmetic, we only keep track of pointer equality
1561 if (POINTER_TYPE_P (type
) && cond_code
!= NE_EXPR
&& cond_code
!= EQ_EXPR
)
1563 set_value_range_to_varying (vr_p
);
1567 /* If LIMIT is another SSA name and LIMIT has a range of its own,
1568 try to use LIMIT's range to avoid creating symbolic ranges
1570 limit_vr
= (TREE_CODE (limit
) == SSA_NAME
) ? get_value_range (limit
) : NULL
;
1572 /* LIMIT's range is only interesting if it has any useful information. */
1574 && (limit_vr
->type
== VR_UNDEFINED
1575 || limit_vr
->type
== VR_VARYING
1576 || symbolic_range_p (limit_vr
)))
1579 /* Initially, the new range has the same set of equivalences of
1580 VAR's range. This will be revised before returning the final
1581 value. Since assertions may be chained via mutually exclusive
1582 predicates, we will need to trim the set of equivalences before
1584 gcc_assert (vr_p
->equiv
== NULL
);
1585 add_equivalence (&vr_p
->equiv
, var
);
1587 /* Extract a new range based on the asserted comparison for VAR and
1588 LIMIT's value range. Notice that if LIMIT has an anti-range, we
1589 will only use it for equality comparisons (EQ_EXPR). For any
1590 other kind of assertion, we cannot derive a range from LIMIT's
1591 anti-range that can be used to describe the new range. For
1592 instance, ASSERT_EXPR <x_2, x_2 <= b_4>. If b_4 is ~[2, 10],
1593 then b_4 takes on the ranges [-INF, 1] and [11, +INF]. There is
1594 no single range for x_2 that could describe LE_EXPR, so we might
1595 as well build the range [b_4, +INF] for it.
1596 One special case we handle is extracting a range from a
1597 range test encoded as (unsigned)var + CST <= limit. */
1598 if (TREE_CODE (cond
) == NOP_EXPR
1599 || TREE_CODE (cond
) == PLUS_EXPR
)
1601 if (TREE_CODE (cond
) == PLUS_EXPR
)
1603 min
= fold_build1 (NEGATE_EXPR
, TREE_TYPE (TREE_OPERAND (cond
, 1)),
1604 TREE_OPERAND (cond
, 1));
1605 max
= int_const_binop (PLUS_EXPR
, limit
, min
);
1606 cond
= TREE_OPERAND (cond
, 0);
1610 min
= build_int_cst (TREE_TYPE (var
), 0);
1614 /* Make sure to not set TREE_OVERFLOW on the final type
1615 conversion. We are willingly interpreting large positive
1616 unsigned values as negative singed values here. */
1617 min
= force_fit_type (TREE_TYPE (var
),
1618 wide_int (min
).force_to_size (TYPE_PRECISION (TREE_TYPE (var
)),
1619 TYPE_SIGN (TREE_TYPE (min
))),
1621 max
= force_fit_type (TREE_TYPE (var
),
1622 wide_int (max
).force_to_size (TYPE_PRECISION (TREE_TYPE (var
)),
1623 TYPE_SIGN (TREE_TYPE (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 || (CONSTANT_CLASS_P (max
) && TREE_OVERFLOW (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 || (CONSTANT_CLASS_P (min
) && TREE_OVERFLOW (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 /* Signed division of -1/0 overflows and by the time it gets here
1881 returns NULL_TREE. */
1884 else if ((TREE_OVERFLOW (res
)
1885 && !TREE_OVERFLOW (val1
)
1886 && !TREE_OVERFLOW (val2
))
1887 || is_overflow_infinity (val1
)
1888 || is_overflow_infinity (val2
))
1890 /* If the operation overflowed but neither VAL1 nor VAL2 are
1891 overflown, return -INF or +INF depending on the operation
1892 and the combination of signs of the operands. */
1893 int sgn1
= tree_int_cst_sgn (val1
);
1894 int sgn2
= tree_int_cst_sgn (val2
);
1896 if (needs_overflow_infinity (TREE_TYPE (res
))
1897 && !supports_overflow_infinity (TREE_TYPE (res
)))
1900 /* We have to punt on adding infinities of different signs,
1901 since we can't tell what the sign of the result should be.
1902 Likewise for subtracting infinities of the same sign. */
1903 if (((code
== PLUS_EXPR
&& sgn1
!= sgn2
)
1904 || (code
== MINUS_EXPR
&& sgn1
== sgn2
))
1905 && is_overflow_infinity (val1
)
1906 && is_overflow_infinity (val2
))
1909 /* Don't try to handle division or shifting of infinities. */
1910 if ((code
== TRUNC_DIV_EXPR
1911 || code
== FLOOR_DIV_EXPR
1912 || code
== CEIL_DIV_EXPR
1913 || code
== EXACT_DIV_EXPR
1914 || code
== ROUND_DIV_EXPR
1915 || code
== RSHIFT_EXPR
)
1916 && (is_overflow_infinity (val1
)
1917 || is_overflow_infinity (val2
)))
1920 /* Notice that we only need to handle the restricted set of
1921 operations handled by extract_range_from_binary_expr.
1922 Among them, only multiplication, addition and subtraction
1923 can yield overflow without overflown operands because we
1924 are working with integral types only... except in the
1925 case VAL1 = -INF and VAL2 = -1 which overflows to +INF
1926 for division too. */
1928 /* For multiplication, the sign of the overflow is given
1929 by the comparison of the signs of the operands. */
1930 if ((code
== MULT_EXPR
&& sgn1
== sgn2
)
1931 /* For addition, the operands must be of the same sign
1932 to yield an overflow. Its sign is therefore that
1933 of one of the operands, for example the first. For
1934 infinite operands X + -INF is negative, not positive. */
1935 || (code
== PLUS_EXPR
1937 ? !is_negative_overflow_infinity (val2
)
1938 : is_positive_overflow_infinity (val2
)))
1939 /* For subtraction, non-infinite operands must be of
1940 different signs to yield an overflow. Its sign is
1941 therefore that of the first operand or the opposite of
1942 that of the second operand. A first operand of 0 counts
1943 as positive here, for the corner case 0 - (-INF), which
1944 overflows, but must yield +INF. For infinite operands 0
1945 - INF is negative, not positive. */
1946 || (code
== MINUS_EXPR
1948 ? !is_positive_overflow_infinity (val2
)
1949 : is_negative_overflow_infinity (val2
)))
1950 /* We only get in here with positive shift count, so the
1951 overflow direction is the same as the sign of val1.
1952 Actually rshift does not overflow at all, but we only
1953 handle the case of shifting overflowed -INF and +INF. */
1954 || (code
== RSHIFT_EXPR
1956 /* For division, the only case is -INF / -1 = +INF. */
1957 || code
== TRUNC_DIV_EXPR
1958 || code
== FLOOR_DIV_EXPR
1959 || code
== CEIL_DIV_EXPR
1960 || code
== EXACT_DIV_EXPR
1961 || code
== ROUND_DIV_EXPR
)
1962 return (needs_overflow_infinity (TREE_TYPE (res
))
1963 ? positive_overflow_infinity (TREE_TYPE (res
))
1964 : TYPE_MAX_VALUE (TREE_TYPE (res
)));
1966 return (needs_overflow_infinity (TREE_TYPE (res
))
1967 ? negative_overflow_infinity (TREE_TYPE (res
))
1968 : TYPE_MIN_VALUE (TREE_TYPE (res
)));
1975 /* For range VR compute two wide_int bitmasks. In *MAY_BE_NONZERO
1976 bitmask if some bit is unset, it means for all numbers in the range
1977 the bit is 0, otherwise it might be 0 or 1. In *MUST_BE_NONZERO
1978 bitmask if some bit is set, it means for all numbers in the range
1979 the bit is 1, otherwise it might be 0 or 1. */
1982 zero_nonzero_bits_from_vr (const tree expr_type
,
1984 wide_int
*may_be_nonzero
,
1985 wide_int
*must_be_nonzero
)
1987 *may_be_nonzero
= wide_int::minus_one (TYPE_PRECISION (expr_type
));
1988 *must_be_nonzero
= wide_int::zero (TYPE_PRECISION (expr_type
));
1989 if (!range_int_cst_p (vr
)
1990 || TREE_OVERFLOW (vr
->min
)
1991 || TREE_OVERFLOW (vr
->max
))
1994 if (range_int_cst_singleton_p (vr
))
1996 *may_be_nonzero
= vr
->min
;
1997 *must_be_nonzero
= *may_be_nonzero
;
1999 else if (tree_int_cst_sgn (vr
->min
) >= 0
2000 || tree_int_cst_sgn (vr
->max
) < 0)
2002 wide_int wmin
= vr
->min
;
2003 wide_int wmax
= vr
->max
;
2004 wide_int xor_mask
= wmin
^ wmax
;
2005 *may_be_nonzero
= wmin
| wmax
;
2006 *must_be_nonzero
= wmin
& wmax
;
2007 if (!xor_mask
.zero_p ())
2009 wide_int mask
= wide_int::mask (xor_mask
.floor_log2 ().to_shwi (),
2011 (*may_be_nonzero
).get_precision ());
2012 *may_be_nonzero
= (*may_be_nonzero
) | mask
;
2013 *must_be_nonzero
= (*must_be_nonzero
).and_not (mask
);
2020 /* Create two value-ranges in *VR0 and *VR1 from the anti-range *AR
2021 so that *VR0 U *VR1 == *AR. Returns true if that is possible,
2022 false otherwise. If *AR can be represented with a single range
2023 *VR1 will be VR_UNDEFINED. */
2026 ranges_from_anti_range (value_range_t
*ar
,
2027 value_range_t
*vr0
, value_range_t
*vr1
)
2029 tree type
= TREE_TYPE (ar
->min
);
2031 vr0
->type
= VR_UNDEFINED
;
2032 vr1
->type
= VR_UNDEFINED
;
2034 if (ar
->type
!= VR_ANTI_RANGE
2035 || TREE_CODE (ar
->min
) != INTEGER_CST
2036 || TREE_CODE (ar
->max
) != INTEGER_CST
2037 || !vrp_val_min (type
)
2038 || !vrp_val_max (type
))
2041 if (!vrp_val_is_min (ar
->min
))
2043 vr0
->type
= VR_RANGE
;
2044 vr0
->min
= vrp_val_min (type
);
2046 = wide_int_to_tree (type
,
2047 wide_int (ar
->min
) - 1);
2049 if (!vrp_val_is_max (ar
->max
))
2051 vr1
->type
= VR_RANGE
;
2053 = wide_int_to_tree (type
,
2054 wide_int (ar
->max
) + 1);
2055 vr1
->max
= vrp_val_max (type
);
2057 if (vr0
->type
== VR_UNDEFINED
)
2060 vr1
->type
= VR_UNDEFINED
;
2063 return vr0
->type
!= VR_UNDEFINED
;
2066 /* Helper to extract a value-range *VR for a multiplicative operation
2070 extract_range_from_multiplicative_op_1 (value_range_t
*vr
,
2071 enum tree_code code
,
2072 value_range_t
*vr0
, value_range_t
*vr1
)
2074 enum value_range_type type
;
2081 /* Multiplications, divisions and shifts are a bit tricky to handle,
2082 depending on the mix of signs we have in the two ranges, we
2083 need to operate on different values to get the minimum and
2084 maximum values for the new range. One approach is to figure
2085 out all the variations of range combinations and do the
2088 However, this involves several calls to compare_values and it
2089 is pretty convoluted. It's simpler to do the 4 operations
2090 (MIN0 OP MIN1, MIN0 OP MAX1, MAX0 OP MIN1 and MAX0 OP MAX0 OP
2091 MAX1) and then figure the smallest and largest values to form
2093 gcc_assert (code
== MULT_EXPR
2094 || code
== TRUNC_DIV_EXPR
2095 || code
== FLOOR_DIV_EXPR
2096 || code
== CEIL_DIV_EXPR
2097 || code
== EXACT_DIV_EXPR
2098 || code
== ROUND_DIV_EXPR
2099 || code
== RSHIFT_EXPR
2100 || code
== LSHIFT_EXPR
);
2101 gcc_assert ((vr0
->type
== VR_RANGE
2102 || (code
== MULT_EXPR
&& vr0
->type
== VR_ANTI_RANGE
))
2103 && vr0
->type
== vr1
->type
);
2107 /* Compute the 4 cross operations. */
2109 val
[0] = vrp_int_const_binop (code
, vr0
->min
, vr1
->min
);
2110 if (val
[0] == NULL_TREE
)
2113 if (vr1
->max
== vr1
->min
)
2117 val
[1] = vrp_int_const_binop (code
, vr0
->min
, vr1
->max
);
2118 if (val
[1] == NULL_TREE
)
2122 if (vr0
->max
== vr0
->min
)
2126 val
[2] = vrp_int_const_binop (code
, vr0
->max
, vr1
->min
);
2127 if (val
[2] == NULL_TREE
)
2131 if (vr0
->min
== vr0
->max
|| vr1
->min
== vr1
->max
)
2135 val
[3] = vrp_int_const_binop (code
, vr0
->max
, vr1
->max
);
2136 if (val
[3] == NULL_TREE
)
2142 set_value_range_to_varying (vr
);
2146 /* Set MIN to the minimum of VAL[i] and MAX to the maximum
2150 for (i
= 1; i
< 4; i
++)
2152 if (!is_gimple_min_invariant (min
)
2153 || (TREE_OVERFLOW (min
) && !is_overflow_infinity (min
))
2154 || !is_gimple_min_invariant (max
)
2155 || (TREE_OVERFLOW (max
) && !is_overflow_infinity (max
)))
2160 if (!is_gimple_min_invariant (val
[i
])
2161 || (TREE_OVERFLOW (val
[i
])
2162 && !is_overflow_infinity (val
[i
])))
2164 /* If we found an overflowed value, set MIN and MAX
2165 to it so that we set the resulting range to
2171 if (compare_values (val
[i
], min
) == -1)
2174 if (compare_values (val
[i
], max
) == 1)
2179 /* If either MIN or MAX overflowed, then set the resulting range to
2180 VARYING. But we do accept an overflow infinity
2182 if (min
== NULL_TREE
2183 || !is_gimple_min_invariant (min
)
2184 || (TREE_OVERFLOW (min
) && !is_overflow_infinity (min
))
2186 || !is_gimple_min_invariant (max
)
2187 || (TREE_OVERFLOW (max
) && !is_overflow_infinity (max
)))
2189 set_value_range_to_varying (vr
);
2195 2) [-INF, +-INF(OVF)]
2196 3) [+-INF(OVF), +INF]
2197 4) [+-INF(OVF), +-INF(OVF)]
2198 We learn nothing when we have INF and INF(OVF) on both sides.
2199 Note that we do accept [-INF, -INF] and [+INF, +INF] without
2201 if ((vrp_val_is_min (min
) || is_overflow_infinity (min
))
2202 && (vrp_val_is_max (max
) || is_overflow_infinity (max
)))
2204 set_value_range_to_varying (vr
);
2208 cmp
= compare_values (min
, max
);
2209 if (cmp
== -2 || cmp
== 1)
2211 /* If the new range has its limits swapped around (MIN > MAX),
2212 then the operation caused one of them to wrap around, mark
2213 the new range VARYING. */
2214 set_value_range_to_varying (vr
);
2217 set_value_range (vr
, type
, min
, max
, NULL
);
2220 /* Extract range information from a binary operation CODE based on
2221 the ranges of each of its operands, *VR0 and *VR1 with resulting
2222 type EXPR_TYPE. The resulting range is stored in *VR. */
2225 extract_range_from_binary_expr_1 (value_range_t
*vr
,
2226 enum tree_code code
, tree expr_type
,
2227 value_range_t
*vr0_
, value_range_t
*vr1_
)
2229 value_range_t vr0
= *vr0_
, vr1
= *vr1_
;
2230 value_range_t vrtem0
= VR_INITIALIZER
, vrtem1
= VR_INITIALIZER
;
2231 enum value_range_type type
;
2232 tree min
= NULL_TREE
, max
= NULL_TREE
;
2235 if (!INTEGRAL_TYPE_P (expr_type
)
2236 && !POINTER_TYPE_P (expr_type
))
2238 set_value_range_to_varying (vr
);
2242 /* Not all binary expressions can be applied to ranges in a
2243 meaningful way. Handle only arithmetic operations. */
2244 if (code
!= PLUS_EXPR
2245 && code
!= MINUS_EXPR
2246 && code
!= POINTER_PLUS_EXPR
2247 && code
!= MULT_EXPR
2248 && code
!= TRUNC_DIV_EXPR
2249 && code
!= FLOOR_DIV_EXPR
2250 && code
!= CEIL_DIV_EXPR
2251 && code
!= EXACT_DIV_EXPR
2252 && code
!= ROUND_DIV_EXPR
2253 && code
!= TRUNC_MOD_EXPR
2254 && code
!= RSHIFT_EXPR
2255 && code
!= LSHIFT_EXPR
2258 && code
!= BIT_AND_EXPR
2259 && code
!= BIT_IOR_EXPR
2260 && code
!= BIT_XOR_EXPR
)
2262 set_value_range_to_varying (vr
);
2266 /* If both ranges are UNDEFINED, so is the result. */
2267 if (vr0
.type
== VR_UNDEFINED
&& vr1
.type
== VR_UNDEFINED
)
2269 set_value_range_to_undefined (vr
);
2272 /* If one of the ranges is UNDEFINED drop it to VARYING for the following
2273 code. At some point we may want to special-case operations that
2274 have UNDEFINED result for all or some value-ranges of the not UNDEFINED
2276 else if (vr0
.type
== VR_UNDEFINED
)
2277 set_value_range_to_varying (&vr0
);
2278 else if (vr1
.type
== VR_UNDEFINED
)
2279 set_value_range_to_varying (&vr1
);
2281 /* Now canonicalize anti-ranges to ranges when they are not symbolic
2282 and express ~[] op X as ([]' op X) U ([]'' op X). */
2283 if (vr0
.type
== VR_ANTI_RANGE
2284 && ranges_from_anti_range (&vr0
, &vrtem0
, &vrtem1
))
2286 extract_range_from_binary_expr_1 (vr
, code
, expr_type
, &vrtem0
, vr1_
);
2287 if (vrtem1
.type
!= VR_UNDEFINED
)
2289 value_range_t vrres
= VR_INITIALIZER
;
2290 extract_range_from_binary_expr_1 (&vrres
, code
, expr_type
,
2292 vrp_meet (vr
, &vrres
);
2296 /* Likewise for X op ~[]. */
2297 if (vr1
.type
== VR_ANTI_RANGE
2298 && ranges_from_anti_range (&vr1
, &vrtem0
, &vrtem1
))
2300 extract_range_from_binary_expr_1 (vr
, code
, expr_type
, vr0_
, &vrtem0
);
2301 if (vrtem1
.type
!= VR_UNDEFINED
)
2303 value_range_t vrres
= VR_INITIALIZER
;
2304 extract_range_from_binary_expr_1 (&vrres
, code
, expr_type
,
2306 vrp_meet (vr
, &vrres
);
2311 /* The type of the resulting value range defaults to VR0.TYPE. */
2314 /* Refuse to operate on VARYING ranges, ranges of different kinds
2315 and symbolic ranges. As an exception, we allow BIT_AND_EXPR
2316 because we may be able to derive a useful range even if one of
2317 the operands is VR_VARYING or symbolic range. Similarly for
2318 divisions. TODO, we may be able to derive anti-ranges in
2320 if (code
!= BIT_AND_EXPR
2321 && code
!= BIT_IOR_EXPR
2322 && code
!= TRUNC_DIV_EXPR
2323 && code
!= FLOOR_DIV_EXPR
2324 && code
!= CEIL_DIV_EXPR
2325 && code
!= EXACT_DIV_EXPR
2326 && code
!= ROUND_DIV_EXPR
2327 && code
!= TRUNC_MOD_EXPR
2330 && (vr0
.type
== VR_VARYING
2331 || vr1
.type
== VR_VARYING
2332 || vr0
.type
!= vr1
.type
2333 || symbolic_range_p (&vr0
)
2334 || symbolic_range_p (&vr1
)))
2336 set_value_range_to_varying (vr
);
2340 /* Now evaluate the expression to determine the new range. */
2341 if (POINTER_TYPE_P (expr_type
))
2343 if (code
== MIN_EXPR
|| code
== MAX_EXPR
)
2345 /* For MIN/MAX expressions with pointers, we only care about
2346 nullness, if both are non null, then the result is nonnull.
2347 If both are null, then the result is null. Otherwise they
2349 if (range_is_nonnull (&vr0
) && range_is_nonnull (&vr1
))
2350 set_value_range_to_nonnull (vr
, expr_type
);
2351 else if (range_is_null (&vr0
) && range_is_null (&vr1
))
2352 set_value_range_to_null (vr
, expr_type
);
2354 set_value_range_to_varying (vr
);
2356 else if (code
== POINTER_PLUS_EXPR
)
2358 /* For pointer types, we are really only interested in asserting
2359 whether the expression evaluates to non-NULL. */
2360 if (range_is_nonnull (&vr0
) || range_is_nonnull (&vr1
))
2361 set_value_range_to_nonnull (vr
, expr_type
);
2362 else if (range_is_null (&vr0
) && range_is_null (&vr1
))
2363 set_value_range_to_null (vr
, expr_type
);
2365 set_value_range_to_varying (vr
);
2367 else if (code
== BIT_AND_EXPR
)
2369 /* For pointer types, we are really only interested in asserting
2370 whether the expression evaluates to non-NULL. */
2371 if (range_is_nonnull (&vr0
) && range_is_nonnull (&vr1
))
2372 set_value_range_to_nonnull (vr
, expr_type
);
2373 else if (range_is_null (&vr0
) || range_is_null (&vr1
))
2374 set_value_range_to_null (vr
, expr_type
);
2376 set_value_range_to_varying (vr
);
2379 set_value_range_to_varying (vr
);
2384 /* For integer ranges, apply the operation to each end of the
2385 range and see what we end up with. */
2386 if (code
== PLUS_EXPR
|| code
== MINUS_EXPR
)
2388 /* If we have a PLUS_EXPR with two VR_RANGE integer constant
2389 ranges compute the precise range for such case if possible. */
2390 if (range_int_cst_p (&vr0
)
2391 && range_int_cst_p (&vr1
))
2393 signop sgn
= TYPE_SIGN (expr_type
);
2394 unsigned int prec
= TYPE_PRECISION (expr_type
);
2395 wide_int min0
= wide_int (vr0
.min
);
2396 wide_int max0
= wide_int (vr0
.max
);
2397 wide_int min1
= wide_int (vr1
.min
);
2398 wide_int max1
= wide_int (vr1
.max
);
2400 = wide_int::min_value (TYPE_PRECISION (expr_type
), sgn
);
2402 = wide_int::max_value (TYPE_PRECISION (expr_type
), sgn
);
2403 wide_int wmin
, wmax
;
2407 if (code
== PLUS_EXPR
)
2412 /* Check for overflow. */
2413 if (min1
.cmp (0, sgn
) != wmin
.cmp (min0
, sgn
))
2414 min_ovf
= min0
.cmp (wmin
, sgn
);
2415 if (max1
.cmp (0, sgn
) != wmax
.cmp (max0
, sgn
))
2416 max_ovf
= max0
.cmp (wmax
, sgn
);
2418 else /* if (code == MINUS_EXPR) */
2423 if (wide_int (0).cmp (max1
, sgn
) != wmin
.cmp (min0
, sgn
))
2424 min_ovf
= min0
.cmp (max1
, sgn
);
2425 if (wide_int (0).cmp (min1
, sgn
) != wmax
.cmp (max0
, sgn
))
2426 max_ovf
= max0
.cmp (min1
, sgn
);
2429 /* For non-wrapping arithmetic look at possibly smaller
2430 value-ranges of the type. */
2431 if (!TYPE_OVERFLOW_WRAPS (expr_type
))
2433 if (vrp_val_min (expr_type
))
2434 type_min
= wide_int (vrp_val_min (expr_type
));
2435 if (vrp_val_max (expr_type
))
2436 type_max
= wide_int (vrp_val_max (expr_type
));
2439 /* Check for type overflow. */
2442 if (wmin
.cmp (type_min
, sgn
) == -1)
2444 else if (wmin
.cmp (type_max
, sgn
) == 1)
2449 if (wmax
.cmp (type_min
, sgn
) == -1)
2451 else if (wmax
.cmp (type_max
, sgn
) == 1)
2455 if (TYPE_OVERFLOW_WRAPS (expr_type
))
2457 /* If overflow wraps, truncate the values and adjust the
2458 range kind and bounds appropriately. */
2459 wide_int tmin
= wmin
.force_to_size (prec
, sgn
);
2460 wide_int tmax
= wmax
.force_to_size (prec
, sgn
);
2461 if (min_ovf
== max_ovf
)
2463 /* No overflow or both overflow or underflow. The
2464 range kind stays VR_RANGE. */
2465 min
= wide_int_to_tree (expr_type
, tmin
);
2466 max
= wide_int_to_tree (expr_type
, tmax
);
2468 else if (min_ovf
== -1
2471 /* Underflow and overflow, drop to VR_VARYING. */
2472 set_value_range_to_varying (vr
);
2477 /* Min underflow or max overflow. The range kind
2478 changes to VR_ANTI_RANGE. */
2479 bool covers
= false;
2480 wide_int tem
= tmin
;
2481 gcc_assert ((min_ovf
== -1 && max_ovf
== 0)
2482 || (max_ovf
== 1 && min_ovf
== 0));
2483 type
= VR_ANTI_RANGE
;
2485 if (tmin
.cmp (tmax
, sgn
) < 0)
2488 if (tmax
.cmp (tem
, sgn
) > 0)
2490 /* If the anti-range would cover nothing, drop to varying.
2491 Likewise if the anti-range bounds are outside of the
2493 if (covers
|| tmin
.cmp (tmax
, sgn
) > 0)
2495 set_value_range_to_varying (vr
);
2498 min
= wide_int_to_tree (expr_type
, tmin
);
2499 max
= wide_int_to_tree (expr_type
, tmax
);
2504 /* If overflow does not wrap, saturate to the types min/max
2508 if (needs_overflow_infinity (expr_type
)
2509 && supports_overflow_infinity (expr_type
))
2510 min
= negative_overflow_infinity (expr_type
);
2512 min
= wide_int_to_tree (expr_type
, type_min
);
2514 else if (min_ovf
== 1)
2516 if (needs_overflow_infinity (expr_type
)
2517 && supports_overflow_infinity (expr_type
))
2518 min
= positive_overflow_infinity (expr_type
);
2520 min
= wide_int_to_tree (expr_type
, type_max
);
2523 min
= wide_int_to_tree (expr_type
, wmin
);
2527 if (needs_overflow_infinity (expr_type
)
2528 && supports_overflow_infinity (expr_type
))
2529 max
= negative_overflow_infinity (expr_type
);
2531 max
= wide_int_to_tree (expr_type
, type_min
);
2533 else if (max_ovf
== 1)
2535 if (needs_overflow_infinity (expr_type
)
2536 && supports_overflow_infinity (expr_type
))
2537 max
= positive_overflow_infinity (expr_type
);
2539 max
= wide_int_to_tree (expr_type
, type_max
);
2542 max
= wide_int_to_tree (expr_type
, wmax
);
2544 if (needs_overflow_infinity (expr_type
)
2545 && supports_overflow_infinity (expr_type
))
2547 if (is_negative_overflow_infinity (vr0
.min
)
2548 || (code
== PLUS_EXPR
2549 ? is_negative_overflow_infinity (vr1
.min
)
2550 : is_positive_overflow_infinity (vr1
.max
)))
2551 min
= negative_overflow_infinity (expr_type
);
2552 if (is_positive_overflow_infinity (vr0
.max
)
2553 || (code
== PLUS_EXPR
2554 ? is_positive_overflow_infinity (vr1
.max
)
2555 : is_negative_overflow_infinity (vr1
.min
)))
2556 max
= positive_overflow_infinity (expr_type
);
2561 /* For other cases, for example if we have a PLUS_EXPR with two
2562 VR_ANTI_RANGEs, drop to VR_VARYING. It would take more effort
2563 to compute a precise range for such a case.
2564 ??? General even mixed range kind operations can be expressed
2565 by for example transforming ~[3, 5] + [1, 2] to range-only
2566 operations and a union primitive:
2567 [-INF, 2] + [1, 2] U [5, +INF] + [1, 2]
2568 [-INF+1, 4] U [6, +INF(OVF)]
2569 though usually the union is not exactly representable with
2570 a single range or anti-range as the above is
2571 [-INF+1, +INF(OVF)] intersected with ~[5, 5]
2572 but one could use a scheme similar to equivalences for this. */
2573 set_value_range_to_varying (vr
);
2577 else if (code
== MIN_EXPR
2578 || code
== MAX_EXPR
)
2580 if (vr0
.type
== VR_RANGE
2581 && !symbolic_range_p (&vr0
))
2584 if (vr1
.type
== VR_RANGE
2585 && !symbolic_range_p (&vr1
))
2587 /* For operations that make the resulting range directly
2588 proportional to the original ranges, apply the operation to
2589 the same end of each range. */
2590 min
= vrp_int_const_binop (code
, vr0
.min
, vr1
.min
);
2591 max
= vrp_int_const_binop (code
, vr0
.max
, vr1
.max
);
2593 else if (code
== MIN_EXPR
)
2595 min
= vrp_val_min (expr_type
);
2598 else if (code
== MAX_EXPR
)
2601 max
= vrp_val_max (expr_type
);
2604 else if (vr1
.type
== VR_RANGE
2605 && !symbolic_range_p (&vr1
))
2608 if (code
== MIN_EXPR
)
2610 min
= vrp_val_min (expr_type
);
2613 else if (code
== MAX_EXPR
)
2616 max
= vrp_val_max (expr_type
);
2621 set_value_range_to_varying (vr
);
2625 else if (code
== MULT_EXPR
)
2627 /* Fancy code so that with unsigned, [-3,-1]*[-3,-1] does not
2628 drop to varying. This test requires 2*prec bits if both
2629 operands are signed and 2*prec + 2 bits if either is not. */
2631 signop sign
= TYPE_SIGN (expr_type
);
2632 unsigned int prec
= TYPE_PRECISION (expr_type
);
2633 unsigned int prec2
= (prec
* 2) + (sign
== UNSIGNED
? 2 : 0);
2635 if (range_int_cst_p (&vr0
)
2636 && range_int_cst_p (&vr1
)
2637 && TYPE_OVERFLOW_WRAPS (expr_type
))
2639 wide_int min0
, max0
, min1
, max1
;
2640 wide_int prod0
, prod1
, prod2
, prod3
;
2641 wide_int sizem1
= wide_int::max_value (prec
, UNSIGNED
, prec2
);
2642 wide_int size
= sizem1
+ 1;
2644 /* Extend the values using the sign of the result to PREC2.
2645 From here on out, everthing is just signed math no matter
2646 what the input types were. */
2647 min0
= wide_int (vr0
.min
).force_to_size (prec2
, sign
);
2648 max0
= wide_int (vr0
.max
).force_to_size (prec2
, sign
);
2649 min1
= wide_int (vr1
.min
).force_to_size (prec2
, sign
);
2650 max1
= wide_int (vr1
.max
).force_to_size (prec2
, sign
);
2652 /* Canonicalize the intervals. */
2653 if (sign
== UNSIGNED
)
2655 if (size
.ltu_p (min0
+ max0
))
2661 if (size
.ltu_p (min1
+ max1
))
2668 prod0
= min0
* min1
;
2669 prod1
= min0
* max1
;
2670 prod2
= max0
* min1
;
2671 prod3
= max0
* max1
;
2673 /* Sort the 4 products so that min is in prod0 and max is in
2675 /* min0min1 > max0max1 */
2676 if (prod0
.gts_p (prod3
))
2678 wide_int tmp
= prod3
;
2683 /* min0max1 > max0min1 */
2684 if (prod1
.gts_p (prod2
))
2686 wide_int tmp
= prod2
;
2691 if (prod0
.gts_p (prod1
))
2693 wide_int tmp
= prod1
;
2698 if (prod2
.gts_p (prod3
))
2700 wide_int tmp
= prod3
;
2705 /* diff = max - min. */
2706 prod2
= prod3
- prod0
;
2707 if (prod2
.geu_p (sizem1
))
2709 /* the range covers all values. */
2710 set_value_range_to_varying (vr
);
2714 /* The following should handle the wrapping and selecting
2715 VR_ANTI_RANGE for us. */
2716 min
= wide_int_to_tree (expr_type
, prod0
);
2717 max
= wide_int_to_tree (expr_type
, prod3
);
2718 set_and_canonicalize_value_range (vr
, VR_RANGE
, min
, max
, NULL
);
2722 /* If we have an unsigned MULT_EXPR with two VR_ANTI_RANGEs,
2723 drop to VR_VARYING. It would take more effort to compute a
2724 precise range for such a case. For example, if we have
2725 op0 == 65536 and op1 == 65536 with their ranges both being
2726 ~[0,0] on a 32-bit machine, we would have op0 * op1 == 0, so
2727 we cannot claim that the product is in ~[0,0]. Note that we
2728 are guaranteed to have vr0.type == vr1.type at this
2730 if (vr0
.type
== VR_ANTI_RANGE
2731 && !TYPE_OVERFLOW_UNDEFINED (expr_type
))
2733 set_value_range_to_varying (vr
);
2737 extract_range_from_multiplicative_op_1 (vr
, code
, &vr0
, &vr1
);
2740 else if (code
== RSHIFT_EXPR
2741 || code
== LSHIFT_EXPR
)
2743 /* If we have a RSHIFT_EXPR with any shift values outside [0..prec-1],
2744 then drop to VR_VARYING. Outside of this range we get undefined
2745 behavior from the shift operation. We cannot even trust
2746 SHIFT_COUNT_TRUNCATED at this stage, because that applies to rtl
2747 shifts, and the operation at the tree level may be widened. */
2748 if (range_int_cst_p (&vr1
)
2749 && compare_tree_int (vr1
.min
, 0) >= 0
2750 && compare_tree_int (vr1
.max
, TYPE_PRECISION (expr_type
)) == -1)
2752 if (code
== RSHIFT_EXPR
)
2754 extract_range_from_multiplicative_op_1 (vr
, code
, &vr0
, &vr1
);
2757 /* We can map lshifts by constants to MULT_EXPR handling. */
2758 else if (code
== LSHIFT_EXPR
2759 && range_int_cst_singleton_p (&vr1
))
2761 bool saved_flag_wrapv
;
2762 value_range_t vr1p
= VR_INITIALIZER
;
2763 vr1p
.type
= VR_RANGE
;
2765 = wide_int_to_tree (expr_type
,
2766 wide_int::set_bit_in_zero (tree_to_shwi (vr1
.min
),
2767 TYPE_PRECISION (expr_type
)));
2768 vr1p
.max
= vr1p
.min
;
2769 /* We have to use a wrapping multiply though as signed overflow
2770 on lshifts is implementation defined in C89. */
2771 saved_flag_wrapv
= flag_wrapv
;
2773 extract_range_from_binary_expr_1 (vr
, MULT_EXPR
, expr_type
,
2775 flag_wrapv
= saved_flag_wrapv
;
2778 else if (code
== LSHIFT_EXPR
2779 && range_int_cst_p (&vr0
))
2781 int prec
= TYPE_PRECISION (expr_type
);
2782 int overflow_pos
= prec
;
2784 wide_int bound
, complement
, low_bound
, high_bound
;
2785 bool uns
= TYPE_UNSIGNED (expr_type
);
2786 bool in_bounds
= false;
2791 bound_shift
= overflow_pos
- tree_to_shwi (vr1
.max
);
2792 /* If bound_shift == HOST_BITS_PER_WIDE_INT, the llshift can
2793 overflow. However, for that to happen, vr1.max needs to be
2794 zero, which means vr1 is a singleton range of zero, which
2795 means it should be handled by the previous LSHIFT_EXPR
2797 bound
= wide_int::set_bit_in_zero (bound_shift
, prec
);
2798 complement
= ~(bound
- 1);
2803 high_bound
= complement
;
2804 if (wide_int::ltu_p (vr0
.max
, low_bound
))
2806 /* [5, 6] << [1, 2] == [10, 24]. */
2807 /* We're shifting out only zeroes, the value increases
2811 else if (high_bound
.ltu_p (vr0
.min
))
2813 /* [0xffffff00, 0xffffffff] << [1, 2]
2814 == [0xfffffc00, 0xfffffffe]. */
2815 /* We're shifting out only ones, the value decreases
2822 /* [-1, 1] << [1, 2] == [-4, 4]. */
2823 low_bound
= complement
;
2825 if (wide_int::lts_p (vr0
.max
, high_bound
)
2826 && low_bound
.lts_p (wide_int (vr0
.min
)))
2828 /* For non-negative numbers, we're shifting out only
2829 zeroes, the value increases monotonically.
2830 For negative numbers, we're shifting out only ones, the
2831 value decreases monotomically. */
2838 extract_range_from_multiplicative_op_1 (vr
, code
, &vr0
, &vr1
);
2843 set_value_range_to_varying (vr
);
2846 else if (code
== TRUNC_DIV_EXPR
2847 || code
== FLOOR_DIV_EXPR
2848 || code
== CEIL_DIV_EXPR
2849 || code
== EXACT_DIV_EXPR
2850 || code
== ROUND_DIV_EXPR
)
2852 if (vr0
.type
!= VR_RANGE
|| symbolic_range_p (&vr0
))
2854 /* For division, if op1 has VR_RANGE but op0 does not, something
2855 can be deduced just from that range. Say [min, max] / [4, max]
2856 gives [min / 4, max / 4] range. */
2857 if (vr1
.type
== VR_RANGE
2858 && !symbolic_range_p (&vr1
)
2859 && range_includes_zero_p (vr1
.min
, vr1
.max
) == 0)
2861 vr0
.type
= type
= VR_RANGE
;
2862 vr0
.min
= vrp_val_min (expr_type
);
2863 vr0
.max
= vrp_val_max (expr_type
);
2867 set_value_range_to_varying (vr
);
2872 /* For divisions, if flag_non_call_exceptions is true, we must
2873 not eliminate a division by zero. */
2874 if (cfun
->can_throw_non_call_exceptions
2875 && (vr1
.type
!= VR_RANGE
2876 || range_includes_zero_p (vr1
.min
, vr1
.max
) != 0))
2878 set_value_range_to_varying (vr
);
2882 /* For divisions, if op0 is VR_RANGE, we can deduce a range
2883 even if op1 is VR_VARYING, VR_ANTI_RANGE, symbolic or can
2885 if (vr0
.type
== VR_RANGE
2886 && (vr1
.type
!= VR_RANGE
2887 || range_includes_zero_p (vr1
.min
, vr1
.max
) != 0))
2889 tree zero
= build_int_cst (TREE_TYPE (vr0
.min
), 0);
2894 if (TYPE_UNSIGNED (expr_type
)
2895 || value_range_nonnegative_p (&vr1
))
2897 /* For unsigned division or when divisor is known
2898 to be non-negative, the range has to cover
2899 all numbers from 0 to max for positive max
2900 and all numbers from min to 0 for negative min. */
2901 cmp
= compare_values (vr0
.max
, zero
);
2904 else if (cmp
== 0 || cmp
== 1)
2908 cmp
= compare_values (vr0
.min
, zero
);
2911 else if (cmp
== 0 || cmp
== -1)
2918 /* Otherwise the range is -max .. max or min .. -min
2919 depending on which bound is bigger in absolute value,
2920 as the division can change the sign. */
2921 abs_extent_range (vr
, vr0
.min
, vr0
.max
);
2924 if (type
== VR_VARYING
)
2926 set_value_range_to_varying (vr
);
2932 extract_range_from_multiplicative_op_1 (vr
, code
, &vr0
, &vr1
);
2936 else if (code
== TRUNC_MOD_EXPR
)
2938 if (vr1
.type
!= VR_RANGE
2939 || range_includes_zero_p (vr1
.min
, vr1
.max
) != 0
2940 || vrp_val_is_min (vr1
.min
))
2942 set_value_range_to_varying (vr
);
2946 /* Compute MAX <|vr1.min|, |vr1.max|> - 1. */
2947 max
= fold_unary_to_constant (ABS_EXPR
, expr_type
, vr1
.min
);
2948 if (tree_int_cst_lt (max
, vr1
.max
))
2950 max
= int_const_binop (MINUS_EXPR
, max
, build_int_cst (TREE_TYPE (max
), 1));
2951 /* If the dividend is non-negative the modulus will be
2952 non-negative as well. */
2953 if (TYPE_UNSIGNED (expr_type
)
2954 || value_range_nonnegative_p (&vr0
))
2955 min
= build_int_cst (TREE_TYPE (max
), 0);
2957 min
= fold_unary_to_constant (NEGATE_EXPR
, expr_type
, max
);
2959 else if (code
== BIT_AND_EXPR
|| code
== BIT_IOR_EXPR
|| code
== BIT_XOR_EXPR
)
2961 bool int_cst_range0
, int_cst_range1
;
2962 wide_int may_be_nonzero0
, may_be_nonzero1
;
2963 wide_int must_be_nonzero0
, must_be_nonzero1
;
2965 int_cst_range0
= zero_nonzero_bits_from_vr (expr_type
, &vr0
, &may_be_nonzero0
,
2967 int_cst_range1
= zero_nonzero_bits_from_vr (expr_type
, &vr1
, &may_be_nonzero1
,
2971 if (code
== BIT_AND_EXPR
)
2974 min
= wide_int_to_tree (expr_type
,
2975 must_be_nonzero0
& must_be_nonzero1
);
2976 wmax
= may_be_nonzero0
& may_be_nonzero1
;
2977 /* If both input ranges contain only negative values we can
2978 truncate the result range maximum to the minimum of the
2979 input range maxima. */
2980 if (int_cst_range0
&& int_cst_range1
2981 && tree_int_cst_sgn (vr0
.max
) < 0
2982 && tree_int_cst_sgn (vr1
.max
) < 0)
2984 wmax
= wmax
.min (vr0
.max
, TYPE_SIGN (expr_type
));
2985 wmax
= wmax
.min (vr1
.max
, TYPE_SIGN (expr_type
));
2987 /* If either input range contains only non-negative values
2988 we can truncate the result range maximum to the respective
2989 maximum of the input range. */
2990 if (int_cst_range0
&& tree_int_cst_sgn (vr0
.min
) >= 0)
2991 wmax
= wmax
.min (vr0
.max
, TYPE_SIGN (expr_type
));
2992 if (int_cst_range1
&& tree_int_cst_sgn (vr1
.min
) >= 0)
2993 wmax
= wmax
.min (vr1
.max
, TYPE_SIGN (expr_type
));
2994 max
= wide_int_to_tree (expr_type
, wmax
);
2996 else if (code
== BIT_IOR_EXPR
)
2999 max
= wide_int_to_tree (expr_type
,
3000 may_be_nonzero0
| may_be_nonzero1
);
3001 wmin
= must_be_nonzero0
| must_be_nonzero1
;
3002 /* If the input ranges contain only positive values we can
3003 truncate the minimum of the result range to the maximum
3004 of the input range minima. */
3005 if (int_cst_range0
&& int_cst_range1
3006 && tree_int_cst_sgn (vr0
.min
) >= 0
3007 && tree_int_cst_sgn (vr1
.min
) >= 0)
3009 wmin
= wmin
.max (vr0
.min
, TYPE_SIGN (expr_type
));
3010 wmin
= wmin
.max (vr1
.min
, TYPE_SIGN (expr_type
));
3012 /* If either input range contains only negative values
3013 we can truncate the minimum of the result range to the
3014 respective minimum range. */
3015 if (int_cst_range0
&& tree_int_cst_sgn (vr0
.max
) < 0)
3016 wmin
= wmin
.max (vr0
.min
, TYPE_SIGN (expr_type
));
3017 if (int_cst_range1
&& tree_int_cst_sgn (vr1
.max
) < 0)
3018 wmin
= wmin
.max (vr1
.min
, TYPE_SIGN (expr_type
));
3019 min
= wide_int_to_tree (expr_type
, wmin
);
3021 else if (code
== BIT_XOR_EXPR
)
3023 wide_int result_zero_bits
, result_one_bits
;
3024 result_zero_bits
= (must_be_nonzero0
& must_be_nonzero1
)
3025 | ~(may_be_nonzero0
| may_be_nonzero1
);
3026 result_one_bits
= must_be_nonzero0
.and_not (may_be_nonzero1
)
3027 | must_be_nonzero1
.and_not (may_be_nonzero0
);
3028 max
= wide_int_to_tree (expr_type
, ~result_zero_bits
);
3029 min
= wide_int_to_tree (expr_type
, result_one_bits
);
3030 /* If the range has all positive or all negative values the
3031 result is better than VARYING. */
3032 if (tree_int_cst_sgn (min
) < 0
3033 || tree_int_cst_sgn (max
) >= 0)
3036 max
= min
= NULL_TREE
;
3042 /* If either MIN or MAX overflowed, then set the resulting range to
3043 VARYING. But we do accept an overflow infinity
3045 if (min
== NULL_TREE
3046 || !is_gimple_min_invariant (min
)
3047 || (TREE_OVERFLOW (min
) && !is_overflow_infinity (min
))
3049 || !is_gimple_min_invariant (max
)
3050 || (TREE_OVERFLOW (max
) && !is_overflow_infinity (max
)))
3052 set_value_range_to_varying (vr
);
3058 2) [-INF, +-INF(OVF)]
3059 3) [+-INF(OVF), +INF]
3060 4) [+-INF(OVF), +-INF(OVF)]
3061 We learn nothing when we have INF and INF(OVF) on both sides.
3062 Note that we do accept [-INF, -INF] and [+INF, +INF] without
3064 if ((vrp_val_is_min (min
) || is_overflow_infinity (min
))
3065 && (vrp_val_is_max (max
) || is_overflow_infinity (max
)))
3067 set_value_range_to_varying (vr
);
3071 cmp
= compare_values (min
, max
);
3072 if (cmp
== -2 || cmp
== 1)
3074 /* If the new range has its limits swapped around (MIN > MAX),
3075 then the operation caused one of them to wrap around, mark
3076 the new range VARYING. */
3077 set_value_range_to_varying (vr
);
3080 set_value_range (vr
, type
, min
, max
, NULL
);
3083 /* Extract range information from a binary expression OP0 CODE OP1 based on
3084 the ranges of each of its operands with resulting type EXPR_TYPE.
3085 The resulting range is stored in *VR. */
3088 extract_range_from_binary_expr (value_range_t
*vr
,
3089 enum tree_code code
,
3090 tree expr_type
, tree op0
, tree op1
)
3092 value_range_t vr0
= VR_INITIALIZER
;
3093 value_range_t vr1
= VR_INITIALIZER
;
3095 /* Get value ranges for each operand. For constant operands, create
3096 a new value range with the operand to simplify processing. */
3097 if (TREE_CODE (op0
) == SSA_NAME
)
3098 vr0
= *(get_value_range (op0
));
3099 else if (is_gimple_min_invariant (op0
))
3100 set_value_range_to_value (&vr0
, op0
, NULL
);
3102 set_value_range_to_varying (&vr0
);
3104 if (TREE_CODE (op1
) == SSA_NAME
)
3105 vr1
= *(get_value_range (op1
));
3106 else if (is_gimple_min_invariant (op1
))
3107 set_value_range_to_value (&vr1
, op1
, NULL
);
3109 set_value_range_to_varying (&vr1
);
3111 extract_range_from_binary_expr_1 (vr
, code
, expr_type
, &vr0
, &vr1
);
3114 /* Extract range information from a unary operation CODE based on
3115 the range of its operand *VR0 with type OP0_TYPE with resulting type TYPE.
3116 The The resulting range is stored in *VR. */
3119 extract_range_from_unary_expr_1 (value_range_t
*vr
,
3120 enum tree_code code
, tree type
,
3121 value_range_t
*vr0_
, tree op0_type
)
3123 value_range_t vr0
= *vr0_
, vrtem0
= VR_INITIALIZER
, vrtem1
= VR_INITIALIZER
;
3125 /* VRP only operates on integral and pointer types. */
3126 if (!(INTEGRAL_TYPE_P (op0_type
)
3127 || POINTER_TYPE_P (op0_type
))
3128 || !(INTEGRAL_TYPE_P (type
)
3129 || POINTER_TYPE_P (type
)))
3131 set_value_range_to_varying (vr
);
3135 /* If VR0 is UNDEFINED, so is the result. */
3136 if (vr0
.type
== VR_UNDEFINED
)
3138 set_value_range_to_undefined (vr
);
3142 /* Handle operations that we express in terms of others. */
3143 if (code
== PAREN_EXPR
)
3145 /* PAREN_EXPR is a simple copy. */
3146 copy_value_range (vr
, &vr0
);
3149 else if (code
== NEGATE_EXPR
)
3151 /* -X is simply 0 - X, so re-use existing code that also handles
3152 anti-ranges fine. */
3153 value_range_t zero
= VR_INITIALIZER
;
3154 set_value_range_to_value (&zero
, build_int_cst (type
, 0), NULL
);
3155 extract_range_from_binary_expr_1 (vr
, MINUS_EXPR
, type
, &zero
, &vr0
);
3158 else if (code
== BIT_NOT_EXPR
)
3160 /* ~X is simply -1 - X, so re-use existing code that also handles
3161 anti-ranges fine. */
3162 value_range_t minusone
= VR_INITIALIZER
;
3163 set_value_range_to_value (&minusone
, build_int_cst (type
, -1), NULL
);
3164 extract_range_from_binary_expr_1 (vr
, MINUS_EXPR
,
3165 type
, &minusone
, &vr0
);
3169 /* Now canonicalize anti-ranges to ranges when they are not symbolic
3170 and express op ~[] as (op []') U (op []''). */
3171 if (vr0
.type
== VR_ANTI_RANGE
3172 && ranges_from_anti_range (&vr0
, &vrtem0
, &vrtem1
))
3174 extract_range_from_unary_expr_1 (vr
, code
, type
, &vrtem0
, op0_type
);
3175 if (vrtem1
.type
!= VR_UNDEFINED
)
3177 value_range_t vrres
= VR_INITIALIZER
;
3178 extract_range_from_unary_expr_1 (&vrres
, code
, type
,
3180 vrp_meet (vr
, &vrres
);
3185 if (CONVERT_EXPR_CODE_P (code
))
3187 tree inner_type
= op0_type
;
3188 tree outer_type
= type
;
3190 /* If the expression evaluates to a pointer, we are only interested in
3191 determining if it evaluates to NULL [0, 0] or non-NULL (~[0, 0]). */
3192 if (POINTER_TYPE_P (type
))
3194 if (range_is_nonnull (&vr0
))
3195 set_value_range_to_nonnull (vr
, type
);
3196 else if (range_is_null (&vr0
))
3197 set_value_range_to_null (vr
, type
);
3199 set_value_range_to_varying (vr
);
3203 /* If VR0 is varying and we increase the type precision, assume
3204 a full range for the following transformation. */
3205 if (vr0
.type
== VR_VARYING
3206 && INTEGRAL_TYPE_P (inner_type
)
3207 && TYPE_PRECISION (inner_type
) < TYPE_PRECISION (outer_type
))
3209 vr0
.type
= VR_RANGE
;
3210 vr0
.min
= TYPE_MIN_VALUE (inner_type
);
3211 vr0
.max
= TYPE_MAX_VALUE (inner_type
);
3214 /* If VR0 is a constant range or anti-range and the conversion is
3215 not truncating we can convert the min and max values and
3216 canonicalize the resulting range. Otherwise we can do the
3217 conversion if the size of the range is less than what the
3218 precision of the target type can represent and the range is
3219 not an anti-range. */
3220 if ((vr0
.type
== VR_RANGE
3221 || vr0
.type
== VR_ANTI_RANGE
)
3222 && TREE_CODE (vr0
.min
) == INTEGER_CST
3223 && TREE_CODE (vr0
.max
) == INTEGER_CST
3224 && (!is_overflow_infinity (vr0
.min
)
3225 || (vr0
.type
== VR_RANGE
3226 && TYPE_PRECISION (outer_type
) > TYPE_PRECISION (inner_type
)
3227 && needs_overflow_infinity (outer_type
)
3228 && supports_overflow_infinity (outer_type
)))
3229 && (!is_overflow_infinity (vr0
.max
)
3230 || (vr0
.type
== VR_RANGE
3231 && TYPE_PRECISION (outer_type
) > TYPE_PRECISION (inner_type
)
3232 && needs_overflow_infinity (outer_type
)
3233 && supports_overflow_infinity (outer_type
)))
3234 && (TYPE_PRECISION (outer_type
) >= TYPE_PRECISION (inner_type
)
3235 || (vr0
.type
== VR_RANGE
3236 && integer_zerop (int_const_binop (RSHIFT_EXPR
,
3237 int_const_binop (MINUS_EXPR
, vr0
.max
, vr0
.min
),
3238 size_int (TYPE_PRECISION (outer_type
)))))))
3240 tree new_min
, new_max
;
3241 if (is_overflow_infinity (vr0
.min
))
3242 new_min
= negative_overflow_infinity (outer_type
);
3244 new_min
= force_fit_type (outer_type
,
3245 wide_int (vr0
.min
).force_to_size (TYPE_PRECISION (outer_type
),
3246 TYPE_SIGN (TREE_TYPE (vr0
.min
))),
3248 if (is_overflow_infinity (vr0
.max
))
3249 new_max
= positive_overflow_infinity (outer_type
);
3251 new_max
= force_fit_type (outer_type
,
3252 wide_int (vr0
.max
).force_to_size (TYPE_PRECISION (outer_type
),
3253 TYPE_SIGN (TREE_TYPE (vr0
.max
))),
3255 set_and_canonicalize_value_range (vr
, vr0
.type
,
3256 new_min
, new_max
, NULL
);
3260 set_value_range_to_varying (vr
);
3263 else if (code
== ABS_EXPR
)
3268 /* Pass through vr0 in the easy cases. */
3269 if (TYPE_UNSIGNED (type
)
3270 || value_range_nonnegative_p (&vr0
))
3272 copy_value_range (vr
, &vr0
);
3276 /* For the remaining varying or symbolic ranges we can't do anything
3278 if (vr0
.type
== VR_VARYING
3279 || symbolic_range_p (&vr0
))
3281 set_value_range_to_varying (vr
);
3285 /* -TYPE_MIN_VALUE = TYPE_MIN_VALUE with flag_wrapv so we can't get a
3287 if (!TYPE_OVERFLOW_UNDEFINED (type
)
3288 && ((vr0
.type
== VR_RANGE
3289 && vrp_val_is_min (vr0
.min
))
3290 || (vr0
.type
== VR_ANTI_RANGE
3291 && !vrp_val_is_min (vr0
.min
))))
3293 set_value_range_to_varying (vr
);
3297 /* ABS_EXPR may flip the range around, if the original range
3298 included negative values. */
3299 if (is_overflow_infinity (vr0
.min
))
3300 min
= positive_overflow_infinity (type
);
3301 else if (!vrp_val_is_min (vr0
.min
))
3302 min
= fold_unary_to_constant (code
, type
, vr0
.min
);
3303 else if (!needs_overflow_infinity (type
))
3304 min
= TYPE_MAX_VALUE (type
);
3305 else if (supports_overflow_infinity (type
))
3306 min
= positive_overflow_infinity (type
);
3309 set_value_range_to_varying (vr
);
3313 if (is_overflow_infinity (vr0
.max
))
3314 max
= positive_overflow_infinity (type
);
3315 else if (!vrp_val_is_min (vr0
.max
))
3316 max
= fold_unary_to_constant (code
, type
, vr0
.max
);
3317 else if (!needs_overflow_infinity (type
))
3318 max
= TYPE_MAX_VALUE (type
);
3319 else if (supports_overflow_infinity (type
)
3320 /* We shouldn't generate [+INF, +INF] as set_value_range
3321 doesn't like this and ICEs. */
3322 && !is_positive_overflow_infinity (min
))
3323 max
= positive_overflow_infinity (type
);
3326 set_value_range_to_varying (vr
);
3330 cmp
= compare_values (min
, max
);
3332 /* If a VR_ANTI_RANGEs contains zero, then we have
3333 ~[-INF, min(MIN, MAX)]. */
3334 if (vr0
.type
== VR_ANTI_RANGE
)
3336 if (range_includes_zero_p (vr0
.min
, vr0
.max
) == 1)
3338 /* Take the lower of the two values. */
3342 /* Create ~[-INF, min (abs(MIN), abs(MAX))]
3343 or ~[-INF + 1, min (abs(MIN), abs(MAX))] when
3344 flag_wrapv is set and the original anti-range doesn't include
3345 TYPE_MIN_VALUE, remember -TYPE_MIN_VALUE = TYPE_MIN_VALUE. */
3346 if (TYPE_OVERFLOW_WRAPS (type
))
3348 tree type_min_value
= TYPE_MIN_VALUE (type
);
3350 min
= (vr0
.min
!= type_min_value
3351 ? int_const_binop (PLUS_EXPR
, type_min_value
,
3352 build_int_cst (TREE_TYPE (type_min_value
), 1))
3357 if (overflow_infinity_range_p (&vr0
))
3358 min
= negative_overflow_infinity (type
);
3360 min
= TYPE_MIN_VALUE (type
);
3365 /* All else has failed, so create the range [0, INF], even for
3366 flag_wrapv since TYPE_MIN_VALUE is in the original
3368 vr0
.type
= VR_RANGE
;
3369 min
= build_int_cst (type
, 0);
3370 if (needs_overflow_infinity (type
))
3372 if (supports_overflow_infinity (type
))
3373 max
= positive_overflow_infinity (type
);
3376 set_value_range_to_varying (vr
);
3381 max
= TYPE_MAX_VALUE (type
);
3385 /* If the range contains zero then we know that the minimum value in the
3386 range will be zero. */
3387 else if (range_includes_zero_p (vr0
.min
, vr0
.max
) == 1)
3391 min
= build_int_cst (type
, 0);
3395 /* If the range was reversed, swap MIN and MAX. */
3404 cmp
= compare_values (min
, max
);
3405 if (cmp
== -2 || cmp
== 1)
3407 /* If the new range has its limits swapped around (MIN > MAX),
3408 then the operation caused one of them to wrap around, mark
3409 the new range VARYING. */
3410 set_value_range_to_varying (vr
);
3413 set_value_range (vr
, vr0
.type
, min
, max
, NULL
);
3417 /* For unhandled operations fall back to varying. */
3418 set_value_range_to_varying (vr
);
3423 /* Extract range information from a unary expression CODE OP0 based on
3424 the range of its operand with resulting type TYPE.
3425 The resulting range is stored in *VR. */
3428 extract_range_from_unary_expr (value_range_t
*vr
, enum tree_code code
,
3429 tree type
, tree op0
)
3431 value_range_t vr0
= VR_INITIALIZER
;
3433 /* Get value ranges for the operand. For constant operands, create
3434 a new value range with the operand to simplify processing. */
3435 if (TREE_CODE (op0
) == SSA_NAME
)
3436 vr0
= *(get_value_range (op0
));
3437 else if (is_gimple_min_invariant (op0
))
3438 set_value_range_to_value (&vr0
, op0
, NULL
);
3440 set_value_range_to_varying (&vr0
);
3442 extract_range_from_unary_expr_1 (vr
, code
, type
, &vr0
, TREE_TYPE (op0
));
3446 /* Extract range information from a conditional expression STMT based on
3447 the ranges of each of its operands and the expression code. */
3450 extract_range_from_cond_expr (value_range_t
*vr
, gimple stmt
)
3453 value_range_t vr0
= VR_INITIALIZER
;
3454 value_range_t vr1
= VR_INITIALIZER
;
3456 /* Get value ranges for each operand. For constant operands, create
3457 a new value range with the operand to simplify processing. */
3458 op0
= gimple_assign_rhs2 (stmt
);
3459 if (TREE_CODE (op0
) == SSA_NAME
)
3460 vr0
= *(get_value_range (op0
));
3461 else if (is_gimple_min_invariant (op0
))
3462 set_value_range_to_value (&vr0
, op0
, NULL
);
3464 set_value_range_to_varying (&vr0
);
3466 op1
= gimple_assign_rhs3 (stmt
);
3467 if (TREE_CODE (op1
) == SSA_NAME
)
3468 vr1
= *(get_value_range (op1
));
3469 else if (is_gimple_min_invariant (op1
))
3470 set_value_range_to_value (&vr1
, op1
, NULL
);
3472 set_value_range_to_varying (&vr1
);
3474 /* The resulting value range is the union of the operand ranges */
3475 copy_value_range (vr
, &vr0
);
3476 vrp_meet (vr
, &vr1
);
3480 /* Extract range information from a comparison expression EXPR based
3481 on the range of its operand and the expression code. */
3484 extract_range_from_comparison (value_range_t
*vr
, enum tree_code code
,
3485 tree type
, tree op0
, tree op1
)
3490 val
= vrp_evaluate_conditional_warnv_with_ops (code
, op0
, op1
, false, &sop
,
3493 /* A disadvantage of using a special infinity as an overflow
3494 representation is that we lose the ability to record overflow
3495 when we don't have an infinity. So we have to ignore a result
3496 which relies on overflow. */
3498 if (val
&& !is_overflow_infinity (val
) && !sop
)
3500 /* Since this expression was found on the RHS of an assignment,
3501 its type may be different from _Bool. Convert VAL to EXPR's
3503 val
= fold_convert (type
, val
);
3504 if (is_gimple_min_invariant (val
))
3505 set_value_range_to_value (vr
, val
, vr
->equiv
);
3507 set_value_range (vr
, VR_RANGE
, val
, val
, vr
->equiv
);
3510 /* The result of a comparison is always true or false. */
3511 set_value_range_to_truthvalue (vr
, type
);
3514 /* Try to derive a nonnegative or nonzero range out of STMT relying
3515 primarily on generic routines in fold in conjunction with range data.
3516 Store the result in *VR */
3519 extract_range_basic (value_range_t
*vr
, gimple stmt
)
3522 tree type
= gimple_expr_type (stmt
);
3524 if (gimple_call_builtin_p (stmt
, BUILT_IN_NORMAL
))
3526 tree fndecl
= gimple_call_fndecl (stmt
), arg
;
3527 int mini
, maxi
, zerov
= 0, prec
;
3529 switch (DECL_FUNCTION_CODE (fndecl
))
3531 case BUILT_IN_CONSTANT_P
:
3532 /* If the call is __builtin_constant_p and the argument is a
3533 function parameter resolve it to false. This avoids bogus
3534 array bound warnings.
3535 ??? We could do this as early as inlining is finished. */
3536 arg
= gimple_call_arg (stmt
, 0);
3537 if (TREE_CODE (arg
) == SSA_NAME
3538 && SSA_NAME_IS_DEFAULT_DEF (arg
)
3539 && TREE_CODE (SSA_NAME_VAR (arg
)) == PARM_DECL
)
3541 set_value_range_to_null (vr
, type
);
3545 /* Both __builtin_ffs* and __builtin_popcount return
3547 CASE_INT_FN (BUILT_IN_FFS
):
3548 CASE_INT_FN (BUILT_IN_POPCOUNT
):
3549 arg
= gimple_call_arg (stmt
, 0);
3550 prec
= TYPE_PRECISION (TREE_TYPE (arg
));
3553 if (TREE_CODE (arg
) == SSA_NAME
)
3555 value_range_t
*vr0
= get_value_range (arg
);
3556 /* If arg is non-zero, then ffs or popcount
3558 if (((vr0
->type
== VR_RANGE
3559 && integer_nonzerop (vr0
->min
))
3560 || (vr0
->type
== VR_ANTI_RANGE
3561 && integer_zerop (vr0
->min
)))
3562 && !TREE_OVERFLOW (vr0
->min
))
3564 /* If some high bits are known to be zero,
3565 we can decrease the maximum. */
3566 if (vr0
->type
== VR_RANGE
3567 && TREE_CODE (vr0
->max
) == INTEGER_CST
3568 && !TREE_OVERFLOW (vr0
->max
))
3569 maxi
= tree_floor_log2 (vr0
->max
) + 1;
3572 /* __builtin_parity* returns [0, 1]. */
3573 CASE_INT_FN (BUILT_IN_PARITY
):
3577 /* __builtin_c[lt]z* return [0, prec-1], except for
3578 when the argument is 0, but that is undefined behavior.
3579 On many targets where the CLZ RTL or optab value is defined
3580 for 0 the value is prec, so include that in the range
3582 CASE_INT_FN (BUILT_IN_CLZ
):
3583 arg
= gimple_call_arg (stmt
, 0);
3584 prec
= TYPE_PRECISION (TREE_TYPE (arg
));
3587 if (optab_handler (clz_optab
, TYPE_MODE (TREE_TYPE (arg
)))
3589 && CLZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (TREE_TYPE (arg
)),
3591 /* Handle only the single common value. */
3593 /* Magic value to give up, unless vr0 proves
3596 if (TREE_CODE (arg
) == SSA_NAME
)
3598 value_range_t
*vr0
= get_value_range (arg
);
3599 /* From clz of VR_RANGE minimum we can compute
3601 if (vr0
->type
== VR_RANGE
3602 && TREE_CODE (vr0
->min
) == INTEGER_CST
3603 && !TREE_OVERFLOW (vr0
->min
))
3605 maxi
= prec
- 1 - tree_floor_log2 (vr0
->min
);
3609 else if (vr0
->type
== VR_ANTI_RANGE
3610 && integer_zerop (vr0
->min
)
3611 && !TREE_OVERFLOW (vr0
->min
))
3618 /* From clz of VR_RANGE maximum we can compute
3620 if (vr0
->type
== VR_RANGE
3621 && TREE_CODE (vr0
->max
) == INTEGER_CST
3622 && !TREE_OVERFLOW (vr0
->max
))
3624 mini
= prec
- 1 - tree_floor_log2 (vr0
->max
);
3632 /* __builtin_ctz* return [0, prec-1], except for
3633 when the argument is 0, but that is undefined behavior.
3634 If there is a ctz optab for this mode and
3635 CTZ_DEFINED_VALUE_AT_ZERO, include that in the range,
3636 otherwise just assume 0 won't be seen. */
3637 CASE_INT_FN (BUILT_IN_CTZ
):
3638 arg
= gimple_call_arg (stmt
, 0);
3639 prec
= TYPE_PRECISION (TREE_TYPE (arg
));
3642 if (optab_handler (ctz_optab
, TYPE_MODE (TREE_TYPE (arg
)))
3644 && CTZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (TREE_TYPE (arg
)),
3647 /* Handle only the two common values. */
3650 else if (zerov
== prec
)
3653 /* Magic value to give up, unless vr0 proves
3657 if (TREE_CODE (arg
) == SSA_NAME
)
3659 value_range_t
*vr0
= get_value_range (arg
);
3660 /* If arg is non-zero, then use [0, prec - 1]. */
3661 if (((vr0
->type
== VR_RANGE
3662 && integer_nonzerop (vr0
->min
))
3663 || (vr0
->type
== VR_ANTI_RANGE
3664 && integer_zerop (vr0
->min
)))
3665 && !TREE_OVERFLOW (vr0
->min
))
3670 /* If some high bits are known to be zero,
3671 we can decrease the result maximum. */
3672 if (vr0
->type
== VR_RANGE
3673 && TREE_CODE (vr0
->max
) == INTEGER_CST
3674 && !TREE_OVERFLOW (vr0
->max
))
3676 maxi
= tree_floor_log2 (vr0
->max
);
3677 /* For vr0 [0, 0] give up. */
3685 /* __builtin_clrsb* returns [0, prec-1]. */
3686 CASE_INT_FN (BUILT_IN_CLRSB
):
3687 arg
= gimple_call_arg (stmt
, 0);
3688 prec
= TYPE_PRECISION (TREE_TYPE (arg
));
3693 set_value_range (vr
, VR_RANGE
, build_int_cst (type
, mini
),
3694 build_int_cst (type
, maxi
), NULL
);
3700 if (INTEGRAL_TYPE_P (type
)
3701 && gimple_stmt_nonnegative_warnv_p (stmt
, &sop
))
3702 set_value_range_to_nonnegative (vr
, type
,
3703 sop
|| stmt_overflow_infinity (stmt
));
3704 else if (vrp_stmt_computes_nonzero (stmt
, &sop
)
3706 set_value_range_to_nonnull (vr
, type
);
3708 set_value_range_to_varying (vr
);
3712 /* Try to compute a useful range out of assignment STMT and store it
3716 extract_range_from_assignment (value_range_t
*vr
, gimple stmt
)
3718 enum tree_code code
= gimple_assign_rhs_code (stmt
);
3720 if (code
== ASSERT_EXPR
)
3721 extract_range_from_assert (vr
, gimple_assign_rhs1 (stmt
));
3722 else if (code
== SSA_NAME
)
3723 extract_range_from_ssa_name (vr
, gimple_assign_rhs1 (stmt
));
3724 else if (TREE_CODE_CLASS (code
) == tcc_binary
)
3725 extract_range_from_binary_expr (vr
, gimple_assign_rhs_code (stmt
),
3726 gimple_expr_type (stmt
),
3727 gimple_assign_rhs1 (stmt
),
3728 gimple_assign_rhs2 (stmt
));
3729 else if (TREE_CODE_CLASS (code
) == tcc_unary
)
3730 extract_range_from_unary_expr (vr
, gimple_assign_rhs_code (stmt
),
3731 gimple_expr_type (stmt
),
3732 gimple_assign_rhs1 (stmt
));
3733 else if (code
== COND_EXPR
)
3734 extract_range_from_cond_expr (vr
, stmt
);
3735 else if (TREE_CODE_CLASS (code
) == tcc_comparison
)
3736 extract_range_from_comparison (vr
, gimple_assign_rhs_code (stmt
),
3737 gimple_expr_type (stmt
),
3738 gimple_assign_rhs1 (stmt
),
3739 gimple_assign_rhs2 (stmt
));
3740 else if (get_gimple_rhs_class (code
) == GIMPLE_SINGLE_RHS
3741 && is_gimple_min_invariant (gimple_assign_rhs1 (stmt
)))
3742 set_value_range_to_value (vr
, gimple_assign_rhs1 (stmt
), NULL
);
3744 set_value_range_to_varying (vr
);
3746 if (vr
->type
== VR_VARYING
)
3747 extract_range_basic (vr
, stmt
);
3750 /* Given a range VR, a LOOP and a variable VAR, determine whether it
3751 would be profitable to adjust VR using scalar evolution information
3752 for VAR. If so, update VR with the new limits. */
3755 adjust_range_with_scev (value_range_t
*vr
, struct loop
*loop
,
3756 gimple stmt
, tree var
)
3758 tree init
, step
, chrec
, tmin
, tmax
, min
, max
, type
, tem
;
3759 enum ev_direction dir
;
3761 /* TODO. Don't adjust anti-ranges. An anti-range may provide
3762 better opportunities than a regular range, but I'm not sure. */
3763 if (vr
->type
== VR_ANTI_RANGE
)
3766 chrec
= instantiate_parameters (loop
, analyze_scalar_evolution (loop
, var
));
3768 /* Like in PR19590, scev can return a constant function. */
3769 if (is_gimple_min_invariant (chrec
))
3771 set_value_range_to_value (vr
, chrec
, vr
->equiv
);
3775 if (TREE_CODE (chrec
) != POLYNOMIAL_CHREC
)
3778 init
= initial_condition_in_loop_num (chrec
, loop
->num
);
3779 tem
= op_with_constant_singleton_value_range (init
);
3782 step
= evolution_part_in_loop_num (chrec
, loop
->num
);
3783 tem
= op_with_constant_singleton_value_range (step
);
3787 /* If STEP is symbolic, we can't know whether INIT will be the
3788 minimum or maximum value in the range. Also, unless INIT is
3789 a simple expression, compare_values and possibly other functions
3790 in tree-vrp won't be able to handle it. */
3791 if (step
== NULL_TREE
3792 || !is_gimple_min_invariant (step
)
3793 || !valid_value_p (init
))
3796 dir
= scev_direction (chrec
);
3797 if (/* Do not adjust ranges if we do not know whether the iv increases
3798 or decreases, ... */
3799 dir
== EV_DIR_UNKNOWN
3800 /* ... or if it may wrap. */
3801 || scev_probably_wraps_p (init
, step
, stmt
, get_chrec_loop (chrec
),
3805 /* We use TYPE_MIN_VALUE and TYPE_MAX_VALUE here instead of
3806 negative_overflow_infinity and positive_overflow_infinity,
3807 because we have concluded that the loop probably does not
3810 type
= TREE_TYPE (var
);
3811 if (POINTER_TYPE_P (type
) || !TYPE_MIN_VALUE (type
))
3812 tmin
= lower_bound_in_type (type
, type
);
3814 tmin
= TYPE_MIN_VALUE (type
);
3815 if (POINTER_TYPE_P (type
) || !TYPE_MAX_VALUE (type
))
3816 tmax
= upper_bound_in_type (type
, type
);
3818 tmax
= TYPE_MAX_VALUE (type
);
3820 /* Try to use estimated number of iterations for the loop to constrain the
3821 final value in the evolution. */
3822 if (TREE_CODE (step
) == INTEGER_CST
3823 && is_gimple_val (init
)
3824 && (TREE_CODE (init
) != SSA_NAME
3825 || get_value_range (init
)->type
== VR_RANGE
))
3829 /* We are only entering here for loop header PHI nodes, so using
3830 the number of latch executions is the correct thing to use. */
3831 if (max_loop_iterations (loop
, &nit
))
3833 value_range_t maxvr
= VR_INITIALIZER
;
3835 signop sgn
= TYPE_SIGN (TREE_TYPE (step
));
3838 wtmp
= max_wide_int (step
).mul (nit
, sgn
, &overflow
);
3839 /* If the multiplication overflowed we can't do a meaningful
3840 adjustment. Likewise if the result doesn't fit in the type
3841 of the induction variable. For a signed type we have to
3842 check whether the result has the expected signedness which
3843 is that of the step as number of iterations is unsigned. */
3845 && wtmp
.fits_to_tree_p (TREE_TYPE (init
))
3847 || (wtmp
.gts_p (0) == wide_int::gts_p (step
, 0))))
3849 tem
= wide_int_to_tree (TREE_TYPE (init
), wtmp
);
3850 extract_range_from_binary_expr (&maxvr
, PLUS_EXPR
,
3851 TREE_TYPE (init
), init
, tem
);
3852 /* Likewise if the addition did. */
3853 if (maxvr
.type
== VR_RANGE
)
3862 if (vr
->type
== VR_VARYING
|| vr
->type
== VR_UNDEFINED
)
3867 /* For VARYING or UNDEFINED ranges, just about anything we get
3868 from scalar evolutions should be better. */
3870 if (dir
== EV_DIR_DECREASES
)
3875 /* If we would create an invalid range, then just assume we
3876 know absolutely nothing. This may be over-conservative,
3877 but it's clearly safe, and should happen only in unreachable
3878 parts of code, or for invalid programs. */
3879 if (compare_values (min
, max
) == 1)
3882 set_value_range (vr
, VR_RANGE
, min
, max
, vr
->equiv
);
3884 else if (vr
->type
== VR_RANGE
)
3889 if (dir
== EV_DIR_DECREASES
)
3891 /* INIT is the maximum value. If INIT is lower than VR->MAX
3892 but no smaller than VR->MIN, set VR->MAX to INIT. */
3893 if (compare_values (init
, max
) == -1)
3896 /* According to the loop information, the variable does not
3897 overflow. If we think it does, probably because of an
3898 overflow due to arithmetic on a different INF value,
3900 if (is_negative_overflow_infinity (min
)
3901 || compare_values (min
, tmin
) == -1)
3907 /* If INIT is bigger than VR->MIN, set VR->MIN to INIT. */
3908 if (compare_values (init
, min
) == 1)
3911 if (is_positive_overflow_infinity (max
)
3912 || compare_values (tmax
, max
) == -1)
3916 /* If we just created an invalid range with the minimum
3917 greater than the maximum, we fail conservatively.
3918 This should happen only in unreachable
3919 parts of code, or for invalid programs. */
3920 if (compare_values (min
, max
) == 1)
3923 set_value_range (vr
, VR_RANGE
, min
, max
, vr
->equiv
);
3927 /* Return true if VAR may overflow at STMT. This checks any available
3928 loop information to see if we can determine that VAR does not
3932 vrp_var_may_overflow (tree var
, gimple stmt
)
3935 tree chrec
, init
, step
;
3937 if (current_loops
== NULL
)
3940 l
= loop_containing_stmt (stmt
);
3945 chrec
= instantiate_parameters (l
, analyze_scalar_evolution (l
, var
));
3946 if (TREE_CODE (chrec
) != POLYNOMIAL_CHREC
)
3949 init
= initial_condition_in_loop_num (chrec
, l
->num
);
3950 step
= evolution_part_in_loop_num (chrec
, l
->num
);
3952 if (step
== NULL_TREE
3953 || !is_gimple_min_invariant (step
)
3954 || !valid_value_p (init
))
3957 /* If we get here, we know something useful about VAR based on the
3958 loop information. If it wraps, it may overflow. */
3960 if (scev_probably_wraps_p (init
, step
, stmt
, get_chrec_loop (chrec
),
3964 if (dump_file
&& (dump_flags
& TDF_DETAILS
) != 0)
3966 print_generic_expr (dump_file
, var
, 0);
3967 fprintf (dump_file
, ": loop information indicates does not overflow\n");
3974 /* Given two numeric value ranges VR0, VR1 and a comparison code COMP:
3976 - Return BOOLEAN_TRUE_NODE if VR0 COMP VR1 always returns true for
3977 all the values in the ranges.
3979 - Return BOOLEAN_FALSE_NODE if the comparison always returns false.
3981 - Return NULL_TREE if it is not always possible to determine the
3982 value of the comparison.
3984 Also set *STRICT_OVERFLOW_P to indicate whether a range with an
3985 overflow infinity was used in the test. */
3989 compare_ranges (enum tree_code comp
, value_range_t
*vr0
, value_range_t
*vr1
,
3990 bool *strict_overflow_p
)
3992 /* VARYING or UNDEFINED ranges cannot be compared. */
3993 if (vr0
->type
== VR_VARYING
3994 || vr0
->type
== VR_UNDEFINED
3995 || vr1
->type
== VR_VARYING
3996 || vr1
->type
== VR_UNDEFINED
)
3999 /* Anti-ranges need to be handled separately. */
4000 if (vr0
->type
== VR_ANTI_RANGE
|| vr1
->type
== VR_ANTI_RANGE
)
4002 /* If both are anti-ranges, then we cannot compute any
4004 if (vr0
->type
== VR_ANTI_RANGE
&& vr1
->type
== VR_ANTI_RANGE
)
4007 /* These comparisons are never statically computable. */
4014 /* Equality can be computed only between a range and an
4015 anti-range. ~[VAL1, VAL2] == [VAL1, VAL2] is always false. */
4016 if (vr0
->type
== VR_RANGE
)
4018 /* To simplify processing, make VR0 the anti-range. */
4019 value_range_t
*tmp
= vr0
;
4024 gcc_assert (comp
== NE_EXPR
|| comp
== EQ_EXPR
);
4026 if (compare_values_warnv (vr0
->min
, vr1
->min
, strict_overflow_p
) == 0
4027 && compare_values_warnv (vr0
->max
, vr1
->max
, strict_overflow_p
) == 0)
4028 return (comp
== NE_EXPR
) ? boolean_true_node
: boolean_false_node
;
4033 if (!usable_range_p (vr0
, strict_overflow_p
)
4034 || !usable_range_p (vr1
, strict_overflow_p
))
4037 /* Simplify processing. If COMP is GT_EXPR or GE_EXPR, switch the
4038 operands around and change the comparison code. */
4039 if (comp
== GT_EXPR
|| comp
== GE_EXPR
)
4042 comp
= (comp
== GT_EXPR
) ? LT_EXPR
: LE_EXPR
;
4048 if (comp
== EQ_EXPR
)
4050 /* Equality may only be computed if both ranges represent
4051 exactly one value. */
4052 if (compare_values_warnv (vr0
->min
, vr0
->max
, strict_overflow_p
) == 0
4053 && compare_values_warnv (vr1
->min
, vr1
->max
, strict_overflow_p
) == 0)
4055 int cmp_min
= compare_values_warnv (vr0
->min
, vr1
->min
,
4057 int cmp_max
= compare_values_warnv (vr0
->max
, vr1
->max
,
4059 if (cmp_min
== 0 && cmp_max
== 0)
4060 return boolean_true_node
;
4061 else if (cmp_min
!= -2 && cmp_max
!= -2)
4062 return boolean_false_node
;
4064 /* If [V0_MIN, V1_MAX] < [V1_MIN, V1_MAX] then V0 != V1. */
4065 else if (compare_values_warnv (vr0
->min
, vr1
->max
,
4066 strict_overflow_p
) == 1
4067 || compare_values_warnv (vr1
->min
, vr0
->max
,
4068 strict_overflow_p
) == 1)
4069 return boolean_false_node
;
4073 else if (comp
== NE_EXPR
)
4077 /* If VR0 is completely to the left or completely to the right
4078 of VR1, they are always different. Notice that we need to
4079 make sure that both comparisons yield similar results to
4080 avoid comparing values that cannot be compared at
4082 cmp1
= compare_values_warnv (vr0
->max
, vr1
->min
, strict_overflow_p
);
4083 cmp2
= compare_values_warnv (vr0
->min
, vr1
->max
, strict_overflow_p
);
4084 if ((cmp1
== -1 && cmp2
== -1) || (cmp1
== 1 && cmp2
== 1))
4085 return boolean_true_node
;
4087 /* If VR0 and VR1 represent a single value and are identical,
4089 else if (compare_values_warnv (vr0
->min
, vr0
->max
,
4090 strict_overflow_p
) == 0
4091 && compare_values_warnv (vr1
->min
, vr1
->max
,
4092 strict_overflow_p
) == 0
4093 && compare_values_warnv (vr0
->min
, vr1
->min
,
4094 strict_overflow_p
) == 0
4095 && compare_values_warnv (vr0
->max
, vr1
->max
,
4096 strict_overflow_p
) == 0)
4097 return boolean_false_node
;
4099 /* Otherwise, they may or may not be different. */
4103 else if (comp
== LT_EXPR
|| comp
== LE_EXPR
)
4107 /* If VR0 is to the left of VR1, return true. */
4108 tst
= compare_values_warnv (vr0
->max
, vr1
->min
, strict_overflow_p
);
4109 if ((comp
== LT_EXPR
&& tst
== -1)
4110 || (comp
== LE_EXPR
&& (tst
== -1 || tst
== 0)))
4112 if (overflow_infinity_range_p (vr0
)
4113 || overflow_infinity_range_p (vr1
))
4114 *strict_overflow_p
= true;
4115 return boolean_true_node
;
4118 /* If VR0 is to the right of VR1, return false. */
4119 tst
= compare_values_warnv (vr0
->min
, vr1
->max
, strict_overflow_p
);
4120 if ((comp
== LT_EXPR
&& (tst
== 0 || tst
== 1))
4121 || (comp
== LE_EXPR
&& tst
== 1))
4123 if (overflow_infinity_range_p (vr0
)
4124 || overflow_infinity_range_p (vr1
))
4125 *strict_overflow_p
= true;
4126 return boolean_false_node
;
4129 /* Otherwise, we don't know. */
4137 /* Given a value range VR, a value VAL and a comparison code COMP, return
4138 BOOLEAN_TRUE_NODE if VR COMP VAL always returns true for all the
4139 values in VR. Return BOOLEAN_FALSE_NODE if the comparison
4140 always returns false. Return NULL_TREE if it is not always
4141 possible to determine the value of the comparison. Also set
4142 *STRICT_OVERFLOW_P to indicate whether a range with an overflow
4143 infinity was used in the test. */
4146 compare_range_with_value (enum tree_code comp
, value_range_t
*vr
, tree val
,
4147 bool *strict_overflow_p
)
4149 if (vr
->type
== VR_VARYING
|| vr
->type
== VR_UNDEFINED
)
4152 /* Anti-ranges need to be handled separately. */
4153 if (vr
->type
== VR_ANTI_RANGE
)
4155 /* For anti-ranges, the only predicates that we can compute at
4156 compile time are equality and inequality. */
4163 /* ~[VAL_1, VAL_2] OP VAL is known if VAL_1 <= VAL <= VAL_2. */
4164 if (value_inside_range (val
, vr
->min
, vr
->max
) == 1)
4165 return (comp
== NE_EXPR
) ? boolean_true_node
: boolean_false_node
;
4170 if (!usable_range_p (vr
, strict_overflow_p
))
4173 if (comp
== EQ_EXPR
)
4175 /* EQ_EXPR may only be computed if VR represents exactly
4177 if (compare_values_warnv (vr
->min
, vr
->max
, strict_overflow_p
) == 0)
4179 int cmp
= compare_values_warnv (vr
->min
, val
, strict_overflow_p
);
4181 return boolean_true_node
;
4182 else if (cmp
== -1 || cmp
== 1 || cmp
== 2)
4183 return boolean_false_node
;
4185 else if (compare_values_warnv (val
, vr
->min
, strict_overflow_p
) == -1
4186 || compare_values_warnv (vr
->max
, val
, strict_overflow_p
) == -1)
4187 return boolean_false_node
;
4191 else if (comp
== NE_EXPR
)
4193 /* If VAL is not inside VR, then they are always different. */
4194 if (compare_values_warnv (vr
->max
, val
, strict_overflow_p
) == -1
4195 || compare_values_warnv (vr
->min
, val
, strict_overflow_p
) == 1)
4196 return boolean_true_node
;
4198 /* If VR represents exactly one value equal to VAL, then return
4200 if (compare_values_warnv (vr
->min
, vr
->max
, strict_overflow_p
) == 0
4201 && compare_values_warnv (vr
->min
, val
, strict_overflow_p
) == 0)
4202 return boolean_false_node
;
4204 /* Otherwise, they may or may not be different. */
4207 else if (comp
== LT_EXPR
|| comp
== LE_EXPR
)
4211 /* If VR is to the left of VAL, return true. */
4212 tst
= compare_values_warnv (vr
->max
, val
, strict_overflow_p
);
4213 if ((comp
== LT_EXPR
&& tst
== -1)
4214 || (comp
== LE_EXPR
&& (tst
== -1 || tst
== 0)))
4216 if (overflow_infinity_range_p (vr
))
4217 *strict_overflow_p
= true;
4218 return boolean_true_node
;
4221 /* If VR is to the right of VAL, return false. */
4222 tst
= compare_values_warnv (vr
->min
, val
, strict_overflow_p
);
4223 if ((comp
== LT_EXPR
&& (tst
== 0 || tst
== 1))
4224 || (comp
== LE_EXPR
&& tst
== 1))
4226 if (overflow_infinity_range_p (vr
))
4227 *strict_overflow_p
= true;
4228 return boolean_false_node
;
4231 /* Otherwise, we don't know. */
4234 else if (comp
== GT_EXPR
|| comp
== GE_EXPR
)
4238 /* If VR is to the right of VAL, return true. */
4239 tst
= compare_values_warnv (vr
->min
, val
, strict_overflow_p
);
4240 if ((comp
== GT_EXPR
&& tst
== 1)
4241 || (comp
== GE_EXPR
&& (tst
== 0 || tst
== 1)))
4243 if (overflow_infinity_range_p (vr
))
4244 *strict_overflow_p
= true;
4245 return boolean_true_node
;
4248 /* If VR is to the left of VAL, return false. */
4249 tst
= compare_values_warnv (vr
->max
, val
, strict_overflow_p
);
4250 if ((comp
== GT_EXPR
&& (tst
== -1 || tst
== 0))
4251 || (comp
== GE_EXPR
&& tst
== -1))
4253 if (overflow_infinity_range_p (vr
))
4254 *strict_overflow_p
= true;
4255 return boolean_false_node
;
4258 /* Otherwise, we don't know. */
4266 /* Debugging dumps. */
4268 void dump_value_range (FILE *, value_range_t
*);
4269 void debug_value_range (value_range_t
*);
4270 void dump_all_value_ranges (FILE *);
4271 void debug_all_value_ranges (void);
4272 void dump_vr_equiv (FILE *, bitmap
);
4273 void debug_vr_equiv (bitmap
);
4276 /* Dump value range VR to FILE. */
4279 dump_value_range (FILE *file
, value_range_t
*vr
)
4282 fprintf (file
, "[]");
4283 else if (vr
->type
== VR_UNDEFINED
)
4284 fprintf (file
, "UNDEFINED");
4285 else if (vr
->type
== VR_RANGE
|| vr
->type
== VR_ANTI_RANGE
)
4287 tree type
= TREE_TYPE (vr
->min
);
4289 fprintf (file
, "%s[", (vr
->type
== VR_ANTI_RANGE
) ? "~" : "");
4291 if (is_negative_overflow_infinity (vr
->min
))
4292 fprintf (file
, "-INF(OVF)");
4293 else if (INTEGRAL_TYPE_P (type
)
4294 && !TYPE_UNSIGNED (type
)
4295 && vrp_val_is_min (vr
->min
))
4296 fprintf (file
, "-INF");
4298 print_generic_expr (file
, vr
->min
, 0);
4300 fprintf (file
, ", ");
4302 if (is_positive_overflow_infinity (vr
->max
))
4303 fprintf (file
, "+INF(OVF)");
4304 else if (INTEGRAL_TYPE_P (type
)
4305 && vrp_val_is_max (vr
->max
))
4306 fprintf (file
, "+INF");
4308 print_generic_expr (file
, vr
->max
, 0);
4310 fprintf (file
, "]");
4317 fprintf (file
, " EQUIVALENCES: { ");
4319 EXECUTE_IF_SET_IN_BITMAP (vr
->equiv
, 0, i
, bi
)
4321 print_generic_expr (file
, ssa_name (i
), 0);
4322 fprintf (file
, " ");
4326 fprintf (file
, "} (%u elements)", c
);
4329 else if (vr
->type
== VR_VARYING
)
4330 fprintf (file
, "VARYING");
4332 fprintf (file
, "INVALID RANGE");
4336 /* Dump value range VR to stderr. */
4339 debug_value_range (value_range_t
*vr
)
4341 dump_value_range (stderr
, vr
);
4342 fprintf (stderr
, "\n");
4346 /* Dump value ranges of all SSA_NAMEs to FILE. */
4349 dump_all_value_ranges (FILE *file
)
4353 for (i
= 0; i
< num_vr_values
; i
++)
4357 print_generic_expr (file
, ssa_name (i
), 0);
4358 fprintf (file
, ": ");
4359 dump_value_range (file
, vr_value
[i
]);
4360 fprintf (file
, "\n");
4364 fprintf (file
, "\n");
4368 /* Dump all value ranges to stderr. */
4371 debug_all_value_ranges (void)
4373 dump_all_value_ranges (stderr
);
4377 /* Given a COND_EXPR COND of the form 'V OP W', and an SSA name V,
4378 create a new SSA name N and return the assertion assignment
4379 'V = ASSERT_EXPR <V, V OP W>'. */
4382 build_assert_expr_for (tree cond
, tree v
)
4387 gcc_assert (TREE_CODE (v
) == SSA_NAME
4388 && COMPARISON_CLASS_P (cond
));
4390 a
= build2 (ASSERT_EXPR
, TREE_TYPE (v
), v
, cond
);
4391 assertion
= gimple_build_assign (NULL_TREE
, a
);
4393 /* The new ASSERT_EXPR, creates a new SSA name that replaces the
4394 operand of the ASSERT_EXPR. Create it so the new name and the old one
4395 are registered in the replacement table so that we can fix the SSA web
4396 after adding all the ASSERT_EXPRs. */
4397 create_new_def_for (v
, assertion
, NULL
);
4403 /* Return false if EXPR is a predicate expression involving floating
4407 fp_predicate (gimple stmt
)
4409 GIMPLE_CHECK (stmt
, GIMPLE_COND
);
4411 return FLOAT_TYPE_P (TREE_TYPE (gimple_cond_lhs (stmt
)));
4415 /* If the range of values taken by OP can be inferred after STMT executes,
4416 return the comparison code (COMP_CODE_P) and value (VAL_P) that
4417 describes the inferred range. Return true if a range could be
4421 infer_value_range (gimple stmt
, tree op
, enum tree_code
*comp_code_p
, tree
*val_p
)
4424 *comp_code_p
= ERROR_MARK
;
4426 /* Do not attempt to infer anything in names that flow through
4428 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op
))
4431 /* Similarly, don't infer anything from statements that may throw
4433 if (stmt_could_throw_p (stmt
))
4436 /* If STMT is the last statement of a basic block with no
4437 successors, there is no point inferring anything about any of its
4438 operands. We would not be able to find a proper insertion point
4439 for the assertion, anyway. */
4440 if (stmt_ends_bb_p (stmt
) && EDGE_COUNT (gimple_bb (stmt
)->succs
) == 0)
4443 /* We can only assume that a pointer dereference will yield
4444 non-NULL if -fdelete-null-pointer-checks is enabled. */
4445 if (flag_delete_null_pointer_checks
4446 && POINTER_TYPE_P (TREE_TYPE (op
))
4447 && gimple_code (stmt
) != GIMPLE_ASM
)
4449 unsigned num_uses
, num_loads
, num_stores
;
4451 count_uses_and_derefs (op
, stmt
, &num_uses
, &num_loads
, &num_stores
);
4452 if (num_loads
+ num_stores
> 0)
4454 *val_p
= build_int_cst (TREE_TYPE (op
), 0);
4455 *comp_code_p
= NE_EXPR
;
4464 void dump_asserts_for (FILE *, tree
);
4465 void debug_asserts_for (tree
);
4466 void dump_all_asserts (FILE *);
4467 void debug_all_asserts (void);
4469 /* Dump all the registered assertions for NAME to FILE. */
4472 dump_asserts_for (FILE *file
, tree name
)
4476 fprintf (file
, "Assertions to be inserted for ");
4477 print_generic_expr (file
, name
, 0);
4478 fprintf (file
, "\n");
4480 loc
= asserts_for
[SSA_NAME_VERSION (name
)];
4483 fprintf (file
, "\t");
4484 print_gimple_stmt (file
, gsi_stmt (loc
->si
), 0, 0);
4485 fprintf (file
, "\n\tBB #%d", loc
->bb
->index
);
4488 fprintf (file
, "\n\tEDGE %d->%d", loc
->e
->src
->index
,
4489 loc
->e
->dest
->index
);
4490 dump_edge_info (file
, loc
->e
, dump_flags
, 0);
4492 fprintf (file
, "\n\tPREDICATE: ");
4493 print_generic_expr (file
, name
, 0);
4494 fprintf (file
, " %s ", tree_code_name
[(int)loc
->comp_code
]);
4495 print_generic_expr (file
, loc
->val
, 0);
4496 fprintf (file
, "\n\n");
4500 fprintf (file
, "\n");
4504 /* Dump all the registered assertions for NAME to stderr. */
4507 debug_asserts_for (tree name
)
4509 dump_asserts_for (stderr
, name
);
4513 /* Dump all the registered assertions for all the names to FILE. */
4516 dump_all_asserts (FILE *file
)
4521 fprintf (file
, "\nASSERT_EXPRs to be inserted\n\n");
4522 EXECUTE_IF_SET_IN_BITMAP (need_assert_for
, 0, i
, bi
)
4523 dump_asserts_for (file
, ssa_name (i
));
4524 fprintf (file
, "\n");
4528 /* Dump all the registered assertions for all the names to stderr. */
4531 debug_all_asserts (void)
4533 dump_all_asserts (stderr
);
4537 /* If NAME doesn't have an ASSERT_EXPR registered for asserting
4538 'EXPR COMP_CODE VAL' at a location that dominates block BB or
4539 E->DEST, then register this location as a possible insertion point
4540 for ASSERT_EXPR <NAME, EXPR COMP_CODE VAL>.
4542 BB, E and SI provide the exact insertion point for the new
4543 ASSERT_EXPR. If BB is NULL, then the ASSERT_EXPR is to be inserted
4544 on edge E. Otherwise, if E is NULL, the ASSERT_EXPR is inserted on
4545 BB. If SI points to a COND_EXPR or a SWITCH_EXPR statement, then E
4546 must not be NULL. */
4549 register_new_assert_for (tree name
, tree expr
,
4550 enum tree_code comp_code
,
4554 gimple_stmt_iterator si
)
4556 assert_locus_t n
, loc
, last_loc
;
4557 basic_block dest_bb
;
4559 gcc_checking_assert (bb
== NULL
|| e
== NULL
);
4562 gcc_checking_assert (gimple_code (gsi_stmt (si
)) != GIMPLE_COND
4563 && gimple_code (gsi_stmt (si
)) != GIMPLE_SWITCH
);
4565 /* Never build an assert comparing against an integer constant with
4566 TREE_OVERFLOW set. This confuses our undefined overflow warning
4568 if (TREE_CODE (val
) == INTEGER_CST
4569 && TREE_OVERFLOW (val
))
4570 val
= wide_int_to_tree (TREE_TYPE (val
), val
);
4572 /* The new assertion A will be inserted at BB or E. We need to
4573 determine if the new location is dominated by a previously
4574 registered location for A. If we are doing an edge insertion,
4575 assume that A will be inserted at E->DEST. Note that this is not
4578 If E is a critical edge, it will be split. But even if E is
4579 split, the new block will dominate the same set of blocks that
4582 The reverse, however, is not true, blocks dominated by E->DEST
4583 will not be dominated by the new block created to split E. So,
4584 if the insertion location is on a critical edge, we will not use
4585 the new location to move another assertion previously registered
4586 at a block dominated by E->DEST. */
4587 dest_bb
= (bb
) ? bb
: e
->dest
;
4589 /* If NAME already has an ASSERT_EXPR registered for COMP_CODE and
4590 VAL at a block dominating DEST_BB, then we don't need to insert a new
4591 one. Similarly, if the same assertion already exists at a block
4592 dominated by DEST_BB and the new location is not on a critical
4593 edge, then update the existing location for the assertion (i.e.,
4594 move the assertion up in the dominance tree).
4596 Note, this is implemented as a simple linked list because there
4597 should not be more than a handful of assertions registered per
4598 name. If this becomes a performance problem, a table hashed by
4599 COMP_CODE and VAL could be implemented. */
4600 loc
= asserts_for
[SSA_NAME_VERSION (name
)];
4604 if (loc
->comp_code
== comp_code
4606 || operand_equal_p (loc
->val
, val
, 0))
4607 && (loc
->expr
== expr
4608 || operand_equal_p (loc
->expr
, expr
, 0)))
4610 /* If E is not a critical edge and DEST_BB
4611 dominates the existing location for the assertion, move
4612 the assertion up in the dominance tree by updating its
4613 location information. */
4614 if ((e
== NULL
|| !EDGE_CRITICAL_P (e
))
4615 && dominated_by_p (CDI_DOMINATORS
, loc
->bb
, dest_bb
))
4624 /* Update the last node of the list and move to the next one. */
4629 /* If we didn't find an assertion already registered for
4630 NAME COMP_CODE VAL, add a new one at the end of the list of
4631 assertions associated with NAME. */
4632 n
= XNEW (struct assert_locus_d
);
4636 n
->comp_code
= comp_code
;
4644 asserts_for
[SSA_NAME_VERSION (name
)] = n
;
4646 bitmap_set_bit (need_assert_for
, SSA_NAME_VERSION (name
));
4649 /* (COND_OP0 COND_CODE COND_OP1) is a predicate which uses NAME.
4650 Extract a suitable test code and value and store them into *CODE_P and
4651 *VAL_P so the predicate is normalized to NAME *CODE_P *VAL_P.
4653 If no extraction was possible, return FALSE, otherwise return TRUE.
4655 If INVERT is true, then we invert the result stored into *CODE_P. */
4658 extract_code_and_val_from_cond_with_ops (tree name
, enum tree_code cond_code
,
4659 tree cond_op0
, tree cond_op1
,
4660 bool invert
, enum tree_code
*code_p
,
4663 enum tree_code comp_code
;
4666 /* Otherwise, we have a comparison of the form NAME COMP VAL
4667 or VAL COMP NAME. */
4668 if (name
== cond_op1
)
4670 /* If the predicate is of the form VAL COMP NAME, flip
4671 COMP around because we need to register NAME as the
4672 first operand in the predicate. */
4673 comp_code
= swap_tree_comparison (cond_code
);
4678 /* The comparison is of the form NAME COMP VAL, so the
4679 comparison code remains unchanged. */
4680 comp_code
= cond_code
;
4684 /* Invert the comparison code as necessary. */
4686 comp_code
= invert_tree_comparison (comp_code
, 0);
4688 /* VRP does not handle float types. */
4689 if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (val
)))
4692 /* Do not register always-false predicates.
4693 FIXME: this works around a limitation in fold() when dealing with
4694 enumerations. Given 'enum { N1, N2 } x;', fold will not
4695 fold 'if (x > N2)' to 'if (0)'. */
4696 if ((comp_code
== GT_EXPR
|| comp_code
== LT_EXPR
)
4697 && INTEGRAL_TYPE_P (TREE_TYPE (val
)))
4699 tree min
= TYPE_MIN_VALUE (TREE_TYPE (val
));
4700 tree max
= TYPE_MAX_VALUE (TREE_TYPE (val
));
4702 if (comp_code
== GT_EXPR
4704 || compare_values (val
, max
) == 0))
4707 if (comp_code
== LT_EXPR
4709 || compare_values (val
, min
) == 0))
4712 *code_p
= comp_code
;
4717 /* Find out smallest RES where RES > VAL && (RES & MASK) == RES, if any
4718 (otherwise return VAL). VAL and MASK must be zero-extended for
4719 precision PREC. If SGNBIT is non-zero, first xor VAL with SGNBIT
4720 (to transform signed values into unsigned) and at the end xor
4724 masked_increment (wide_int val
, wide_int mask
, wide_int sgnbit
,
4727 wide_int bit
= wide_int::one (prec
), res
;
4731 for (i
= 0; i
< prec
; i
++, bit
+= bit
)
4734 if ((res
& bit
).zero_p ())
4737 res
= (val
+ bit
).and_not (res
);
4739 if (res
.gtu_p (val
))
4740 return res
^ sgnbit
;
4742 return val
^ sgnbit
;
4745 /* Try to register an edge assertion for SSA name NAME on edge E for
4746 the condition COND contributing to the conditional jump pointed to by BSI.
4747 Invert the condition COND if INVERT is true.
4748 Return true if an assertion for NAME could be registered. */
4751 register_edge_assert_for_2 (tree name
, edge e
, gimple_stmt_iterator bsi
,
4752 enum tree_code cond_code
,
4753 tree cond_op0
, tree cond_op1
, bool invert
)
4756 enum tree_code comp_code
;
4757 bool retval
= false;
4759 if (!extract_code_and_val_from_cond_with_ops (name
, cond_code
,
4762 invert
, &comp_code
, &val
))
4765 /* Only register an ASSERT_EXPR if NAME was found in the sub-graph
4766 reachable from E. */
4767 if (live_on_edge (e
, name
)
4768 && !has_single_use (name
))
4770 register_new_assert_for (name
, name
, comp_code
, val
, NULL
, e
, bsi
);
4774 /* In the case of NAME <= CST and NAME being defined as
4775 NAME = (unsigned) NAME2 + CST2 we can assert NAME2 >= -CST2
4776 and NAME2 <= CST - CST2. We can do the same for NAME > CST.
4777 This catches range and anti-range tests. */
4778 if ((comp_code
== LE_EXPR
4779 || comp_code
== GT_EXPR
)
4780 && TREE_CODE (val
) == INTEGER_CST
4781 && TYPE_UNSIGNED (TREE_TYPE (val
)))
4783 gimple def_stmt
= SSA_NAME_DEF_STMT (name
);
4784 tree cst2
= NULL_TREE
, name2
= NULL_TREE
, name3
= NULL_TREE
;
4786 /* Extract CST2 from the (optional) addition. */
4787 if (is_gimple_assign (def_stmt
)
4788 && gimple_assign_rhs_code (def_stmt
) == PLUS_EXPR
)
4790 name2
= gimple_assign_rhs1 (def_stmt
);
4791 cst2
= gimple_assign_rhs2 (def_stmt
);
4792 if (TREE_CODE (name2
) == SSA_NAME
4793 && TREE_CODE (cst2
) == INTEGER_CST
)
4794 def_stmt
= SSA_NAME_DEF_STMT (name2
);
4797 /* Extract NAME2 from the (optional) sign-changing cast. */
4798 if (gimple_assign_cast_p (def_stmt
))
4800 if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt
))
4801 && ! TYPE_UNSIGNED (TREE_TYPE (gimple_assign_rhs1 (def_stmt
)))
4802 && (TYPE_PRECISION (gimple_expr_type (def_stmt
))
4803 == TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (def_stmt
)))))
4804 name3
= gimple_assign_rhs1 (def_stmt
);
4807 /* If name3 is used later, create an ASSERT_EXPR for it. */
4808 if (name3
!= NULL_TREE
4809 && TREE_CODE (name3
) == SSA_NAME
4810 && (cst2
== NULL_TREE
4811 || TREE_CODE (cst2
) == INTEGER_CST
)
4812 && INTEGRAL_TYPE_P (TREE_TYPE (name3
))
4813 && live_on_edge (e
, name3
)
4814 && !has_single_use (name3
))
4818 /* Build an expression for the range test. */
4819 tmp
= build1 (NOP_EXPR
, TREE_TYPE (name
), name3
);
4820 if (cst2
!= NULL_TREE
)
4821 tmp
= build2 (PLUS_EXPR
, TREE_TYPE (name
), tmp
, cst2
);
4825 fprintf (dump_file
, "Adding assert for ");
4826 print_generic_expr (dump_file
, name3
, 0);
4827 fprintf (dump_file
, " from ");
4828 print_generic_expr (dump_file
, tmp
, 0);
4829 fprintf (dump_file
, "\n");
4832 register_new_assert_for (name3
, tmp
, comp_code
, val
, NULL
, e
, bsi
);
4837 /* If name2 is used later, create an ASSERT_EXPR for it. */
4838 if (name2
!= NULL_TREE
4839 && TREE_CODE (name2
) == SSA_NAME
4840 && TREE_CODE (cst2
) == INTEGER_CST
4841 && INTEGRAL_TYPE_P (TREE_TYPE (name2
))
4842 && live_on_edge (e
, name2
)
4843 && !has_single_use (name2
))
4847 /* Build an expression for the range test. */
4849 if (TREE_TYPE (name
) != TREE_TYPE (name2
))
4850 tmp
= build1 (NOP_EXPR
, TREE_TYPE (name
), tmp
);
4851 if (cst2
!= NULL_TREE
)
4852 tmp
= build2 (PLUS_EXPR
, TREE_TYPE (name
), tmp
, cst2
);
4856 fprintf (dump_file
, "Adding assert for ");
4857 print_generic_expr (dump_file
, name2
, 0);
4858 fprintf (dump_file
, " from ");
4859 print_generic_expr (dump_file
, tmp
, 0);
4860 fprintf (dump_file
, "\n");
4863 register_new_assert_for (name2
, tmp
, comp_code
, val
, NULL
, e
, bsi
);
4869 /* In the case of post-in/decrement tests like if (i++) ... and uses
4870 of the in/decremented value on the edge the extra name we want to
4871 assert for is not on the def chain of the name compared. Instead
4872 it is in the set of use stmts. */
4873 if ((comp_code
== NE_EXPR
4874 || comp_code
== EQ_EXPR
)
4875 && TREE_CODE (val
) == INTEGER_CST
)
4877 imm_use_iterator ui
;
4879 FOR_EACH_IMM_USE_STMT (use_stmt
, ui
, name
)
4881 /* Cut off to use-stmts that are in the predecessor. */
4882 if (gimple_bb (use_stmt
) != e
->src
)
4885 if (!is_gimple_assign (use_stmt
))
4888 enum tree_code code
= gimple_assign_rhs_code (use_stmt
);
4889 if (code
!= PLUS_EXPR
4890 && code
!= MINUS_EXPR
)
4893 tree cst
= gimple_assign_rhs2 (use_stmt
);
4894 if (TREE_CODE (cst
) != INTEGER_CST
)
4897 tree name2
= gimple_assign_lhs (use_stmt
);
4898 if (live_on_edge (e
, name2
))
4900 cst
= int_const_binop (code
, val
, cst
);
4901 register_new_assert_for (name2
, name2
, comp_code
, cst
,
4908 if (TREE_CODE_CLASS (comp_code
) == tcc_comparison
4909 && TREE_CODE (val
) == INTEGER_CST
)
4911 gimple def_stmt
= SSA_NAME_DEF_STMT (name
);
4912 tree name2
= NULL_TREE
, names
[2], cst2
= NULL_TREE
;
4913 tree val2
= NULL_TREE
;
4915 unsigned int prec
= TYPE_PRECISION (TREE_TYPE (val
));
4916 unsigned int nprec
= prec
;
4917 enum tree_code rhs_code
= ERROR_MARK
;
4919 if (is_gimple_assign (def_stmt
))
4920 rhs_code
= gimple_assign_rhs_code (def_stmt
);
4922 /* Add asserts for NAME cmp CST and NAME being defined
4923 as NAME = (int) NAME2. */
4924 if (!TYPE_UNSIGNED (TREE_TYPE (val
))
4925 && (comp_code
== LE_EXPR
|| comp_code
== LT_EXPR
4926 || comp_code
== GT_EXPR
|| comp_code
== GE_EXPR
)
4927 && gimple_assign_cast_p (def_stmt
))
4929 name2
= gimple_assign_rhs1 (def_stmt
);
4930 if (CONVERT_EXPR_CODE_P (rhs_code
)
4931 && INTEGRAL_TYPE_P (TREE_TYPE (name2
))
4932 && TYPE_UNSIGNED (TREE_TYPE (name2
))
4933 && prec
== TYPE_PRECISION (TREE_TYPE (name2
))
4934 && (comp_code
== LE_EXPR
|| comp_code
== GT_EXPR
4935 || !tree_int_cst_equal (val
,
4936 TYPE_MIN_VALUE (TREE_TYPE (val
))))
4937 && live_on_edge (e
, name2
)
4938 && !has_single_use (name2
))
4941 enum tree_code new_comp_code
= comp_code
;
4943 cst
= fold_convert (TREE_TYPE (name2
),
4944 TYPE_MIN_VALUE (TREE_TYPE (val
)));
4945 /* Build an expression for the range test. */
4946 tmp
= build2 (PLUS_EXPR
, TREE_TYPE (name2
), name2
, cst
);
4947 cst
= fold_build2 (PLUS_EXPR
, TREE_TYPE (name2
), cst
,
4948 fold_convert (TREE_TYPE (name2
), val
));
4949 if (comp_code
== LT_EXPR
|| comp_code
== GE_EXPR
)
4951 new_comp_code
= comp_code
== LT_EXPR
? LE_EXPR
: GT_EXPR
;
4952 cst
= fold_build2 (MINUS_EXPR
, TREE_TYPE (name2
), cst
,
4953 build_int_cst (TREE_TYPE (name2
), 1));
4958 fprintf (dump_file
, "Adding assert for ");
4959 print_generic_expr (dump_file
, name2
, 0);
4960 fprintf (dump_file
, " from ");
4961 print_generic_expr (dump_file
, tmp
, 0);
4962 fprintf (dump_file
, "\n");
4965 register_new_assert_for (name2
, tmp
, new_comp_code
, cst
, NULL
,
4972 /* Add asserts for NAME cmp CST and NAME being defined as
4973 NAME = NAME2 >> CST2.
4975 Extract CST2 from the right shift. */
4976 if (rhs_code
== RSHIFT_EXPR
)
4978 name2
= gimple_assign_rhs1 (def_stmt
);
4979 cst2
= gimple_assign_rhs2 (def_stmt
);
4980 if (TREE_CODE (name2
) == SSA_NAME
4981 && tree_fits_uhwi_p (cst2
)
4982 && INTEGRAL_TYPE_P (TREE_TYPE (name2
))
4983 && IN_RANGE (tree_to_uhwi (cst2
), 1, prec
- 1)
4984 && prec
== GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (val
)))
4985 && live_on_edge (e
, name2
)
4986 && !has_single_use (name2
))
4988 mask
= wide_int::mask (tree_to_uhwi (cst2
), false, prec
);
4989 val2
= fold_binary (LSHIFT_EXPR
, TREE_TYPE (val
), val
, cst2
);
4992 if (val2
!= NULL_TREE
4993 && TREE_CODE (val2
) == INTEGER_CST
4994 && simple_cst_equal (fold_build2 (RSHIFT_EXPR
,
4998 enum tree_code new_comp_code
= comp_code
;
5002 if (comp_code
== EQ_EXPR
|| comp_code
== NE_EXPR
)
5004 if (!TYPE_UNSIGNED (TREE_TYPE (val
)))
5006 tree type
= build_nonstandard_integer_type (prec
, 1);
5007 tmp
= build1 (NOP_EXPR
, type
, name2
);
5008 val2
= fold_convert (type
, val2
);
5010 tmp
= fold_build2 (MINUS_EXPR
, TREE_TYPE (tmp
), tmp
, val2
);
5011 new_val
= wide_int_to_tree (TREE_TYPE (tmp
), mask
);
5012 new_comp_code
= comp_code
== EQ_EXPR
? LE_EXPR
: GT_EXPR
;
5014 else if (comp_code
== LT_EXPR
|| comp_code
== GE_EXPR
)
5017 = wide_int::min_value (prec
, TYPE_SIGN (TREE_TYPE (val
)));
5019 if (minval
== wide_int (new_val
))
5020 new_val
= NULL_TREE
;
5025 = wide_int::max_value (prec
, TYPE_SIGN (TREE_TYPE (val
)));
5026 mask
|= wide_int (val2
);
5028 new_val
= NULL_TREE
;
5030 new_val
= wide_int_to_tree (TREE_TYPE (val2
), mask
);
5037 fprintf (dump_file
, "Adding assert for ");
5038 print_generic_expr (dump_file
, name2
, 0);
5039 fprintf (dump_file
, " from ");
5040 print_generic_expr (dump_file
, tmp
, 0);
5041 fprintf (dump_file
, "\n");
5044 register_new_assert_for (name2
, tmp
, new_comp_code
, new_val
,
5050 /* Add asserts for NAME cmp CST and NAME being defined as
5051 NAME = NAME2 & CST2.
5053 Extract CST2 from the and.
5056 NAME = (unsigned) NAME2;
5057 casts where NAME's type is unsigned and has smaller precision
5058 than NAME2's type as if it was NAME = NAME2 & MASK. */
5059 names
[0] = NULL_TREE
;
5060 names
[1] = NULL_TREE
;
5062 if (rhs_code
== BIT_AND_EXPR
5063 || (CONVERT_EXPR_CODE_P (rhs_code
)
5064 && TREE_CODE (TREE_TYPE (val
)) == INTEGER_TYPE
5065 && TYPE_UNSIGNED (TREE_TYPE (val
))
5066 && TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (def_stmt
)))
5070 name2
= gimple_assign_rhs1 (def_stmt
);
5071 if (rhs_code
== BIT_AND_EXPR
)
5072 cst2
= gimple_assign_rhs2 (def_stmt
);
5075 cst2
= TYPE_MAX_VALUE (TREE_TYPE (val
));
5076 nprec
= TYPE_PRECISION (TREE_TYPE (name2
));
5078 if (TREE_CODE (name2
) == SSA_NAME
5079 && INTEGRAL_TYPE_P (TREE_TYPE (name2
))
5080 && TREE_CODE (cst2
) == INTEGER_CST
5081 && !integer_zerop (cst2
)
5083 || TYPE_UNSIGNED (TREE_TYPE (val
))))
5085 gimple def_stmt2
= SSA_NAME_DEF_STMT (name2
);
5086 if (gimple_assign_cast_p (def_stmt2
))
5088 names
[1] = gimple_assign_rhs1 (def_stmt2
);
5089 if (!CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt2
))
5090 || !INTEGRAL_TYPE_P (TREE_TYPE (names
[1]))
5091 || (TYPE_PRECISION (TREE_TYPE (name2
))
5092 != TYPE_PRECISION (TREE_TYPE (names
[1])))
5093 || !live_on_edge (e
, names
[1])
5094 || has_single_use (names
[1]))
5095 names
[1] = NULL_TREE
;
5097 if (live_on_edge (e
, name2
)
5098 && !has_single_use (name2
))
5102 if (names
[0] || names
[1])
5104 wide_int minv
, maxv
= 0, valv
, cst2v
;
5105 wide_int tem
, sgnbit
;
5106 bool valid_p
= false, valn
= false, cst2n
= false;
5107 enum tree_code ccode
= comp_code
;
5109 valv
= wide_int (val
).zforce_to_size (nprec
);
5110 cst2v
= wide_int (cst2
).zforce_to_size (nprec
);
5111 if (TYPE_SIGN (TREE_TYPE (val
)) == SIGNED
)
5113 valn
= valv
.sext (nprec
).neg_p ();
5114 cst2n
= cst2v
.sext (nprec
).neg_p ();
5116 /* If CST2 doesn't have most significant bit set,
5117 but VAL is negative, we have comparison like
5118 if ((x & 0x123) > -4) (always true). Just give up. */
5122 sgnbit
= wide_int::set_bit_in_zero (nprec
- 1, nprec
);
5124 sgnbit
= wide_int::zero (nprec
);
5125 minv
= valv
& cst2v
;
5129 /* Minimum unsigned value for equality is VAL & CST2
5130 (should be equal to VAL, otherwise we probably should
5131 have folded the comparison into false) and
5132 maximum unsigned value is VAL | ~CST2. */
5133 maxv
= valv
| ~cst2v
;
5134 maxv
= maxv
.zext (nprec
);
5139 tem
= valv
| ~cst2v
;
5140 tem
= tem
.zext (nprec
);
5141 /* If VAL is 0, handle (X & CST2) != 0 as (X & CST2) > 0U. */
5145 sgnbit
= wide_int::zero (nprec
);
5148 /* If (VAL | ~CST2) is all ones, handle it as
5149 (X & CST2) < VAL. */
5154 sgnbit
= wide_int::zero (nprec
);
5157 if (!cst2n
&& cst2v
.sext (nprec
).neg_p ())
5158 sgnbit
= wide_int::set_bit_in_zero (nprec
- 1, nprec
);
5159 if (!sgnbit
.zero_p ())
5167 if (tem
== wide_int::mask (nprec
- 1, false, nprec
))
5178 /* Minimum unsigned value for >= if (VAL & CST2) == VAL
5179 is VAL and maximum unsigned value is ~0. For signed
5180 comparison, if CST2 doesn't have most significant bit
5181 set, handle it similarly. If CST2 has MSB set,
5182 the minimum is the same, and maximum is ~0U/2. */
5185 /* If (VAL & CST2) != VAL, X & CST2 can't be equal to
5187 minv
= masked_increment (valv
, cst2v
, sgnbit
, nprec
);
5191 maxv
= wide_int::mask (nprec
- (cst2n
? 1 : 0), false, nprec
);
5197 /* Find out smallest MINV where MINV > VAL
5198 && (MINV & CST2) == MINV, if any. If VAL is signed and
5199 CST2 has MSB set, compute it biased by 1 << (nprec - 1). */
5200 minv
= masked_increment (valv
, cst2v
, sgnbit
, nprec
);
5203 maxv
= wide_int::mask (nprec
- (cst2n
? 1 : 0), false, nprec
);
5208 /* Minimum unsigned value for <= is 0 and maximum
5209 unsigned value is VAL | ~CST2 if (VAL & CST2) == VAL.
5210 Otherwise, find smallest VAL2 where VAL2 > VAL
5211 && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
5213 For signed comparison, if CST2 doesn't have most
5214 significant bit set, handle it similarly. If CST2 has
5215 MSB set, the maximum is the same and minimum is INT_MIN. */
5220 maxv
= masked_increment (valv
, cst2v
, sgnbit
, nprec
);
5226 maxv
= maxv
.zext (nprec
);
5233 /* Minimum unsigned value for < is 0 and maximum
5234 unsigned value is (VAL-1) | ~CST2 if (VAL & CST2) == VAL.
5235 Otherwise, find smallest VAL2 where VAL2 > VAL
5236 && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
5238 For signed comparison, if CST2 doesn't have most
5239 significant bit set, handle it similarly. If CST2 has
5240 MSB set, the maximum is the same and minimum is INT_MIN. */
5249 maxv
= masked_increment (valv
, cst2v
, sgnbit
, nprec
);
5255 maxv
= maxv
.zext (nprec
);
5264 && (maxv
- minv
).zext (nprec
) != wide_int::minus_one (nprec
))
5266 tree tmp
, new_val
, type
;
5269 for (i
= 0; i
< 2; i
++)
5272 wide_int maxv2
= maxv
;
5274 type
= TREE_TYPE (names
[i
]);
5275 if (!TYPE_UNSIGNED (type
))
5277 type
= build_nonstandard_integer_type (nprec
, 1);
5278 tmp
= build1 (NOP_EXPR
, type
, names
[i
]);
5280 if (!minv
.zero_p ())
5282 tmp
= build2 (PLUS_EXPR
, type
, tmp
,
5283 wide_int_to_tree (type
, -minv
));
5284 maxv2
= maxv
- minv
;
5286 new_val
= wide_int_to_tree (type
, maxv2
);
5290 fprintf (dump_file
, "Adding assert for ");
5291 print_generic_expr (dump_file
, names
[i
], 0);
5292 fprintf (dump_file
, " from ");
5293 print_generic_expr (dump_file
, tmp
, 0);
5294 fprintf (dump_file
, "\n");
5297 register_new_assert_for (names
[i
], tmp
, LE_EXPR
,
5298 new_val
, NULL
, e
, bsi
);
5308 /* OP is an operand of a truth value expression which is known to have
5309 a particular value. Register any asserts for OP and for any
5310 operands in OP's defining statement.
5312 If CODE is EQ_EXPR, then we want to register OP is zero (false),
5313 if CODE is NE_EXPR, then we want to register OP is nonzero (true). */
5316 register_edge_assert_for_1 (tree op
, enum tree_code code
,
5317 edge e
, gimple_stmt_iterator bsi
)
5319 bool retval
= false;
5322 enum tree_code rhs_code
;
5324 /* We only care about SSA_NAMEs. */
5325 if (TREE_CODE (op
) != SSA_NAME
)
5328 /* We know that OP will have a zero or nonzero value. If OP is used
5329 more than once go ahead and register an assert for OP.
5331 The FOUND_IN_SUBGRAPH support is not helpful in this situation as
5332 it will always be set for OP (because OP is used in a COND_EXPR in
5334 if (!has_single_use (op
))
5336 val
= build_int_cst (TREE_TYPE (op
), 0);
5337 register_new_assert_for (op
, op
, code
, val
, NULL
, e
, bsi
);
5341 /* Now look at how OP is set. If it's set from a comparison,
5342 a truth operation or some bit operations, then we may be able
5343 to register information about the operands of that assignment. */
5344 op_def
= SSA_NAME_DEF_STMT (op
);
5345 if (gimple_code (op_def
) != GIMPLE_ASSIGN
)
5348 rhs_code
= gimple_assign_rhs_code (op_def
);
5350 if (TREE_CODE_CLASS (rhs_code
) == tcc_comparison
)
5352 bool invert
= (code
== EQ_EXPR
? true : false);
5353 tree op0
= gimple_assign_rhs1 (op_def
);
5354 tree op1
= gimple_assign_rhs2 (op_def
);
5356 if (TREE_CODE (op0
) == SSA_NAME
)
5357 retval
|= register_edge_assert_for_2 (op0
, e
, bsi
, rhs_code
, op0
, op1
,
5359 if (TREE_CODE (op1
) == SSA_NAME
)
5360 retval
|= register_edge_assert_for_2 (op1
, e
, bsi
, rhs_code
, op0
, op1
,
5363 else if ((code
== NE_EXPR
5364 && gimple_assign_rhs_code (op_def
) == BIT_AND_EXPR
)
5366 && gimple_assign_rhs_code (op_def
) == BIT_IOR_EXPR
))
5368 /* Recurse on each operand. */
5369 tree op0
= gimple_assign_rhs1 (op_def
);
5370 tree op1
= gimple_assign_rhs2 (op_def
);
5371 if (TREE_CODE (op0
) == SSA_NAME
5372 && has_single_use (op0
))
5373 retval
|= register_edge_assert_for_1 (op0
, code
, e
, bsi
);
5374 if (TREE_CODE (op1
) == SSA_NAME
5375 && has_single_use (op1
))
5376 retval
|= register_edge_assert_for_1 (op1
, code
, e
, bsi
);
5378 else if (gimple_assign_rhs_code (op_def
) == BIT_NOT_EXPR
5379 && TYPE_PRECISION (TREE_TYPE (gimple_assign_lhs (op_def
))) == 1)
5381 /* Recurse, flipping CODE. */
5382 code
= invert_tree_comparison (code
, false);
5383 retval
|= register_edge_assert_for_1 (gimple_assign_rhs1 (op_def
),
5386 else if (gimple_assign_rhs_code (op_def
) == SSA_NAME
)
5388 /* Recurse through the copy. */
5389 retval
|= register_edge_assert_for_1 (gimple_assign_rhs1 (op_def
),
5392 else if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (op_def
)))
5394 /* Recurse through the type conversion. */
5395 retval
|= register_edge_assert_for_1 (gimple_assign_rhs1 (op_def
),
5402 /* Try to register an edge assertion for SSA name NAME on edge E for
5403 the condition COND contributing to the conditional jump pointed to by SI.
5404 Return true if an assertion for NAME could be registered. */
5407 register_edge_assert_for (tree name
, edge e
, gimple_stmt_iterator si
,
5408 enum tree_code cond_code
, tree cond_op0
,
5412 enum tree_code comp_code
;
5413 bool retval
= false;
5414 bool is_else_edge
= (e
->flags
& EDGE_FALSE_VALUE
) != 0;
5416 /* Do not attempt to infer anything in names that flow through
5418 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name
))
5421 if (!extract_code_and_val_from_cond_with_ops (name
, cond_code
,
5427 /* Register ASSERT_EXPRs for name. */
5428 retval
|= register_edge_assert_for_2 (name
, e
, si
, cond_code
, cond_op0
,
5429 cond_op1
, is_else_edge
);
5432 /* If COND is effectively an equality test of an SSA_NAME against
5433 the value zero or one, then we may be able to assert values
5434 for SSA_NAMEs which flow into COND. */
5436 /* In the case of NAME == 1 or NAME != 0, for BIT_AND_EXPR defining
5437 statement of NAME we can assert both operands of the BIT_AND_EXPR
5438 have nonzero value. */
5439 if (((comp_code
== EQ_EXPR
&& integer_onep (val
))
5440 || (comp_code
== NE_EXPR
&& integer_zerop (val
))))
5442 gimple def_stmt
= SSA_NAME_DEF_STMT (name
);
5444 if (is_gimple_assign (def_stmt
)
5445 && gimple_assign_rhs_code (def_stmt
) == BIT_AND_EXPR
)
5447 tree op0
= gimple_assign_rhs1 (def_stmt
);
5448 tree op1
= gimple_assign_rhs2 (def_stmt
);
5449 retval
|= register_edge_assert_for_1 (op0
, NE_EXPR
, e
, si
);
5450 retval
|= register_edge_assert_for_1 (op1
, NE_EXPR
, e
, si
);
5454 /* In the case of NAME == 0 or NAME != 1, for BIT_IOR_EXPR defining
5455 statement of NAME we can assert both operands of the BIT_IOR_EXPR
5457 if (((comp_code
== EQ_EXPR
&& integer_zerop (val
))
5458 || (comp_code
== NE_EXPR
&& integer_onep (val
))))
5460 gimple def_stmt
= SSA_NAME_DEF_STMT (name
);
5462 /* For BIT_IOR_EXPR only if NAME == 0 both operands have
5463 necessarily zero value, or if type-precision is one. */
5464 if (is_gimple_assign (def_stmt
)
5465 && (gimple_assign_rhs_code (def_stmt
) == BIT_IOR_EXPR
5466 && (TYPE_PRECISION (TREE_TYPE (name
)) == 1
5467 || comp_code
== EQ_EXPR
)))
5469 tree op0
= gimple_assign_rhs1 (def_stmt
);
5470 tree op1
= gimple_assign_rhs2 (def_stmt
);
5471 retval
|= register_edge_assert_for_1 (op0
, EQ_EXPR
, e
, si
);
5472 retval
|= register_edge_assert_for_1 (op1
, EQ_EXPR
, e
, si
);
5480 /* Determine whether the outgoing edges of BB should receive an
5481 ASSERT_EXPR for each of the operands of BB's LAST statement.
5482 The last statement of BB must be a COND_EXPR.
5484 If any of the sub-graphs rooted at BB have an interesting use of
5485 the predicate operands, an assert location node is added to the
5486 list of assertions for the corresponding operands. */
5489 find_conditional_asserts (basic_block bb
, gimple last
)
5492 gimple_stmt_iterator bsi
;
5498 need_assert
= false;
5499 bsi
= gsi_for_stmt (last
);
5501 /* Look for uses of the operands in each of the sub-graphs
5502 rooted at BB. We need to check each of the outgoing edges
5503 separately, so that we know what kind of ASSERT_EXPR to
5505 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
5510 /* Register the necessary assertions for each operand in the
5511 conditional predicate. */
5512 FOR_EACH_SSA_TREE_OPERAND (op
, last
, iter
, SSA_OP_USE
)
5514 need_assert
|= register_edge_assert_for (op
, e
, bsi
,
5515 gimple_cond_code (last
),
5516 gimple_cond_lhs (last
),
5517 gimple_cond_rhs (last
));
5530 /* Compare two case labels sorting first by the destination bb index
5531 and then by the case value. */
5534 compare_case_labels (const void *p1
, const void *p2
)
5536 const struct case_info
*ci1
= (const struct case_info
*) p1
;
5537 const struct case_info
*ci2
= (const struct case_info
*) p2
;
5538 int idx1
= ci1
->bb
->index
;
5539 int idx2
= ci2
->bb
->index
;
5543 else if (idx1
== idx2
)
5545 /* Make sure the default label is first in a group. */
5546 if (!CASE_LOW (ci1
->expr
))
5548 else if (!CASE_LOW (ci2
->expr
))
5551 return tree_int_cst_compare (CASE_LOW (ci1
->expr
),
5552 CASE_LOW (ci2
->expr
));
5558 /* Determine whether the outgoing edges of BB should receive an
5559 ASSERT_EXPR for each of the operands of BB's LAST statement.
5560 The last statement of BB must be a SWITCH_EXPR.
5562 If any of the sub-graphs rooted at BB have an interesting use of
5563 the predicate operands, an assert location node is added to the
5564 list of assertions for the corresponding operands. */
5567 find_switch_asserts (basic_block bb
, gimple last
)
5570 gimple_stmt_iterator bsi
;
5573 struct case_info
*ci
;
5574 size_t n
= gimple_switch_num_labels (last
);
5575 #if GCC_VERSION >= 4000
5578 /* Work around GCC 3.4 bug (PR 37086). */
5579 volatile unsigned int idx
;
5582 need_assert
= false;
5583 bsi
= gsi_for_stmt (last
);
5584 op
= gimple_switch_index (last
);
5585 if (TREE_CODE (op
) != SSA_NAME
)
5588 /* Build a vector of case labels sorted by destination label. */
5589 ci
= XNEWVEC (struct case_info
, n
);
5590 for (idx
= 0; idx
< n
; ++idx
)
5592 ci
[idx
].expr
= gimple_switch_label (last
, idx
);
5593 ci
[idx
].bb
= label_to_block (CASE_LABEL (ci
[idx
].expr
));
5595 qsort (ci
, n
, sizeof (struct case_info
), compare_case_labels
);
5597 for (idx
= 0; idx
< n
; ++idx
)
5600 tree cl
= ci
[idx
].expr
;
5601 basic_block cbb
= ci
[idx
].bb
;
5603 min
= CASE_LOW (cl
);
5604 max
= CASE_HIGH (cl
);
5606 /* If there are multiple case labels with the same destination
5607 we need to combine them to a single value range for the edge. */
5608 if (idx
+ 1 < n
&& cbb
== ci
[idx
+ 1].bb
)
5610 /* Skip labels until the last of the group. */
5613 } while (idx
< n
&& cbb
== ci
[idx
].bb
);
5616 /* Pick up the maximum of the case label range. */
5617 if (CASE_HIGH (ci
[idx
].expr
))
5618 max
= CASE_HIGH (ci
[idx
].expr
);
5620 max
= CASE_LOW (ci
[idx
].expr
);
5623 /* Nothing to do if the range includes the default label until we
5624 can register anti-ranges. */
5625 if (min
== NULL_TREE
)
5628 /* Find the edge to register the assert expr on. */
5629 e
= find_edge (bb
, cbb
);
5631 /* Register the necessary assertions for the operand in the
5633 need_assert
|= register_edge_assert_for (op
, e
, bsi
,
5634 max
? GE_EXPR
: EQ_EXPR
,
5636 fold_convert (TREE_TYPE (op
),
5640 need_assert
|= register_edge_assert_for (op
, e
, bsi
, LE_EXPR
,
5642 fold_convert (TREE_TYPE (op
),
5652 /* Traverse all the statements in block BB looking for statements that
5653 may generate useful assertions for the SSA names in their operand.
5654 If a statement produces a useful assertion A for name N_i, then the
5655 list of assertions already generated for N_i is scanned to
5656 determine if A is actually needed.
5658 If N_i already had the assertion A at a location dominating the
5659 current location, then nothing needs to be done. Otherwise, the
5660 new location for A is recorded instead.
5662 1- For every statement S in BB, all the variables used by S are
5663 added to bitmap FOUND_IN_SUBGRAPH.
5665 2- If statement S uses an operand N in a way that exposes a known
5666 value range for N, then if N was not already generated by an
5667 ASSERT_EXPR, create a new assert location for N. For instance,
5668 if N is a pointer and the statement dereferences it, we can
5669 assume that N is not NULL.
5671 3- COND_EXPRs are a special case of #2. We can derive range
5672 information from the predicate but need to insert different
5673 ASSERT_EXPRs for each of the sub-graphs rooted at the
5674 conditional block. If the last statement of BB is a conditional
5675 expression of the form 'X op Y', then
5677 a) Remove X and Y from the set FOUND_IN_SUBGRAPH.
5679 b) If the conditional is the only entry point to the sub-graph
5680 corresponding to the THEN_CLAUSE, recurse into it. On
5681 return, if X and/or Y are marked in FOUND_IN_SUBGRAPH, then
5682 an ASSERT_EXPR is added for the corresponding variable.
5684 c) Repeat step (b) on the ELSE_CLAUSE.
5686 d) Mark X and Y in FOUND_IN_SUBGRAPH.
5695 In this case, an assertion on the THEN clause is useful to
5696 determine that 'a' is always 9 on that edge. However, an assertion
5697 on the ELSE clause would be unnecessary.
5699 4- If BB does not end in a conditional expression, then we recurse
5700 into BB's dominator children.
5702 At the end of the recursive traversal, every SSA name will have a
5703 list of locations where ASSERT_EXPRs should be added. When a new
5704 location for name N is found, it is registered by calling
5705 register_new_assert_for. That function keeps track of all the
5706 registered assertions to prevent adding unnecessary assertions.
5707 For instance, if a pointer P_4 is dereferenced more than once in a
5708 dominator tree, only the location dominating all the dereference of
5709 P_4 will receive an ASSERT_EXPR.
5711 If this function returns true, then it means that there are names
5712 for which we need to generate ASSERT_EXPRs. Those assertions are
5713 inserted by process_assert_insertions. */
5716 find_assert_locations_1 (basic_block bb
, sbitmap live
)
5718 gimple_stmt_iterator si
;
5722 need_assert
= false;
5723 last
= last_stmt (bb
);
5725 /* If BB's last statement is a conditional statement involving integer
5726 operands, determine if we need to add ASSERT_EXPRs. */
5728 && gimple_code (last
) == GIMPLE_COND
5729 && !fp_predicate (last
)
5730 && !ZERO_SSA_OPERANDS (last
, SSA_OP_USE
))
5731 need_assert
|= find_conditional_asserts (bb
, last
);
5733 /* If BB's last statement is a switch statement involving integer
5734 operands, determine if we need to add ASSERT_EXPRs. */
5736 && gimple_code (last
) == GIMPLE_SWITCH
5737 && !ZERO_SSA_OPERANDS (last
, SSA_OP_USE
))
5738 need_assert
|= find_switch_asserts (bb
, last
);
5740 /* Traverse all the statements in BB marking used names and looking
5741 for statements that may infer assertions for their used operands. */
5742 for (si
= gsi_last_bb (bb
); !gsi_end_p (si
); gsi_prev (&si
))
5748 stmt
= gsi_stmt (si
);
5750 if (is_gimple_debug (stmt
))
5753 /* See if we can derive an assertion for any of STMT's operands. */
5754 FOR_EACH_SSA_TREE_OPERAND (op
, stmt
, i
, SSA_OP_USE
)
5757 enum tree_code comp_code
;
5759 /* If op is not live beyond this stmt, do not bother to insert
5761 if (!bitmap_bit_p (live
, SSA_NAME_VERSION (op
)))
5764 /* If OP is used in such a way that we can infer a value
5765 range for it, and we don't find a previous assertion for
5766 it, create a new assertion location node for OP. */
5767 if (infer_value_range (stmt
, op
, &comp_code
, &value
))
5769 /* If we are able to infer a nonzero value range for OP,
5770 then walk backwards through the use-def chain to see if OP
5771 was set via a typecast.
5773 If so, then we can also infer a nonzero value range
5774 for the operand of the NOP_EXPR. */
5775 if (comp_code
== NE_EXPR
&& integer_zerop (value
))
5778 gimple def_stmt
= SSA_NAME_DEF_STMT (t
);
5780 while (is_gimple_assign (def_stmt
)
5781 && gimple_assign_rhs_code (def_stmt
) == NOP_EXPR
5783 (gimple_assign_rhs1 (def_stmt
)) == SSA_NAME
5785 (TREE_TYPE (gimple_assign_rhs1 (def_stmt
))))
5787 t
= gimple_assign_rhs1 (def_stmt
);
5788 def_stmt
= SSA_NAME_DEF_STMT (t
);
5790 /* Note we want to register the assert for the
5791 operand of the NOP_EXPR after SI, not after the
5793 if (! has_single_use (t
))
5795 register_new_assert_for (t
, t
, comp_code
, value
,
5802 register_new_assert_for (op
, op
, comp_code
, value
, bb
, NULL
, si
);
5808 FOR_EACH_SSA_TREE_OPERAND (op
, stmt
, i
, SSA_OP_USE
)
5809 bitmap_set_bit (live
, SSA_NAME_VERSION (op
));
5810 FOR_EACH_SSA_TREE_OPERAND (op
, stmt
, i
, SSA_OP_DEF
)
5811 bitmap_clear_bit (live
, SSA_NAME_VERSION (op
));
5814 /* Traverse all PHI nodes in BB, updating live. */
5815 for (si
= gsi_start_phis (bb
); !gsi_end_p(si
); gsi_next (&si
))
5817 use_operand_p arg_p
;
5819 gimple phi
= gsi_stmt (si
);
5820 tree res
= gimple_phi_result (phi
);
5822 if (virtual_operand_p (res
))
5825 FOR_EACH_PHI_ARG (arg_p
, phi
, i
, SSA_OP_USE
)
5827 tree arg
= USE_FROM_PTR (arg_p
);
5828 if (TREE_CODE (arg
) == SSA_NAME
)
5829 bitmap_set_bit (live
, SSA_NAME_VERSION (arg
));
5832 bitmap_clear_bit (live
, SSA_NAME_VERSION (res
));
5838 /* Do an RPO walk over the function computing SSA name liveness
5839 on-the-fly and deciding on assert expressions to insert.
5840 Returns true if there are assert expressions to be inserted. */
5843 find_assert_locations (void)
5845 int *rpo
= XNEWVEC (int, last_basic_block
);
5846 int *bb_rpo
= XNEWVEC (int, last_basic_block
);
5847 int *last_rpo
= XCNEWVEC (int, last_basic_block
);
5851 live
= XCNEWVEC (sbitmap
, last_basic_block
);
5852 rpo_cnt
= pre_and_rev_post_order_compute (NULL
, rpo
, false);
5853 for (i
= 0; i
< rpo_cnt
; ++i
)
5856 need_asserts
= false;
5857 for (i
= rpo_cnt
- 1; i
>= 0; --i
)
5859 basic_block bb
= BASIC_BLOCK (rpo
[i
]);
5865 live
[rpo
[i
]] = sbitmap_alloc (num_ssa_names
);
5866 bitmap_clear (live
[rpo
[i
]]);
5869 /* Process BB and update the live information with uses in
5871 need_asserts
|= find_assert_locations_1 (bb
, live
[rpo
[i
]]);
5873 /* Merge liveness into the predecessor blocks and free it. */
5874 if (!bitmap_empty_p (live
[rpo
[i
]]))
5877 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
5879 int pred
= e
->src
->index
;
5880 if ((e
->flags
& EDGE_DFS_BACK
) || pred
== ENTRY_BLOCK
)
5885 live
[pred
] = sbitmap_alloc (num_ssa_names
);
5886 bitmap_clear (live
[pred
]);
5888 bitmap_ior (live
[pred
], live
[pred
], live
[rpo
[i
]]);
5890 if (bb_rpo
[pred
] < pred_rpo
)
5891 pred_rpo
= bb_rpo
[pred
];
5894 /* Record the RPO number of the last visited block that needs
5895 live information from this block. */
5896 last_rpo
[rpo
[i
]] = pred_rpo
;
5900 sbitmap_free (live
[rpo
[i
]]);
5901 live
[rpo
[i
]] = NULL
;
5904 /* We can free all successors live bitmaps if all their
5905 predecessors have been visited already. */
5906 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
5907 if (last_rpo
[e
->dest
->index
] == i
5908 && live
[e
->dest
->index
])
5910 sbitmap_free (live
[e
->dest
->index
]);
5911 live
[e
->dest
->index
] = NULL
;
5916 XDELETEVEC (bb_rpo
);
5917 XDELETEVEC (last_rpo
);
5918 for (i
= 0; i
< last_basic_block
; ++i
)
5920 sbitmap_free (live
[i
]);
5923 return need_asserts
;
5926 /* Create an ASSERT_EXPR for NAME and insert it in the location
5927 indicated by LOC. Return true if we made any edge insertions. */
5930 process_assert_insertions_for (tree name
, assert_locus_t loc
)
5932 /* Build the comparison expression NAME_i COMP_CODE VAL. */
5939 /* If we have X <=> X do not insert an assert expr for that. */
5940 if (loc
->expr
== loc
->val
)
5943 cond
= build2 (loc
->comp_code
, boolean_type_node
, loc
->expr
, loc
->val
);
5944 assert_stmt
= build_assert_expr_for (cond
, name
);
5947 /* We have been asked to insert the assertion on an edge. This
5948 is used only by COND_EXPR and SWITCH_EXPR assertions. */
5949 gcc_checking_assert (gimple_code (gsi_stmt (loc
->si
)) == GIMPLE_COND
5950 || (gimple_code (gsi_stmt (loc
->si
))
5953 gsi_insert_on_edge (loc
->e
, assert_stmt
);
5957 /* Otherwise, we can insert right after LOC->SI iff the
5958 statement must not be the last statement in the block. */
5959 stmt
= gsi_stmt (loc
->si
);
5960 if (!stmt_ends_bb_p (stmt
))
5962 gsi_insert_after (&loc
->si
, assert_stmt
, GSI_SAME_STMT
);
5966 /* If STMT must be the last statement in BB, we can only insert new
5967 assertions on the non-abnormal edge out of BB. Note that since
5968 STMT is not control flow, there may only be one non-abnormal edge
5970 FOR_EACH_EDGE (e
, ei
, loc
->bb
->succs
)
5971 if (!(e
->flags
& EDGE_ABNORMAL
))
5973 gsi_insert_on_edge (e
, assert_stmt
);
5981 /* Process all the insertions registered for every name N_i registered
5982 in NEED_ASSERT_FOR. The list of assertions to be inserted are
5983 found in ASSERTS_FOR[i]. */
5986 process_assert_insertions (void)
5990 bool update_edges_p
= false;
5991 int num_asserts
= 0;
5993 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
5994 dump_all_asserts (dump_file
);
5996 EXECUTE_IF_SET_IN_BITMAP (need_assert_for
, 0, i
, bi
)
5998 assert_locus_t loc
= asserts_for
[i
];
6003 assert_locus_t next
= loc
->next
;
6004 update_edges_p
|= process_assert_insertions_for (ssa_name (i
), loc
);
6012 gsi_commit_edge_inserts ();
6014 statistics_counter_event (cfun
, "Number of ASSERT_EXPR expressions inserted",
6019 /* Traverse the flowgraph looking for conditional jumps to insert range
6020 expressions. These range expressions are meant to provide information
6021 to optimizations that need to reason in terms of value ranges. They
6022 will not be expanded into RTL. For instance, given:
6031 this pass will transform the code into:
6037 x = ASSERT_EXPR <x, x < y>
6042 y = ASSERT_EXPR <y, x <= y>
6046 The idea is that once copy and constant propagation have run, other
6047 optimizations will be able to determine what ranges of values can 'x'
6048 take in different paths of the code, simply by checking the reaching
6049 definition of 'x'. */
6052 insert_range_assertions (void)
6054 need_assert_for
= BITMAP_ALLOC (NULL
);
6055 asserts_for
= XCNEWVEC (assert_locus_t
, num_ssa_names
);
6057 calculate_dominance_info (CDI_DOMINATORS
);
6059 if (find_assert_locations ())
6061 process_assert_insertions ();
6062 update_ssa (TODO_update_ssa_no_phi
);
6065 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6067 fprintf (dump_file
, "\nSSA form after inserting ASSERT_EXPRs\n");
6068 dump_function_to_file (current_function_decl
, dump_file
, dump_flags
);
6072 BITMAP_FREE (need_assert_for
);
6075 /* Checks one ARRAY_REF in REF, located at LOCUS. Ignores flexible arrays
6076 and "struct" hacks. If VRP can determine that the
6077 array subscript is a constant, check if it is outside valid
6078 range. If the array subscript is a RANGE, warn if it is
6079 non-overlapping with valid range.
6080 IGNORE_OFF_BY_ONE is true if the ARRAY_REF is inside a ADDR_EXPR. */
6083 check_array_ref (location_t location
, tree ref
, bool ignore_off_by_one
)
6085 value_range_t
* vr
= NULL
;
6086 tree low_sub
, up_sub
;
6087 tree low_bound
, up_bound
, up_bound_p1
;
6090 if (TREE_NO_WARNING (ref
))
6093 low_sub
= up_sub
= TREE_OPERAND (ref
, 1);
6094 up_bound
= array_ref_up_bound (ref
);
6096 /* Can not check flexible arrays. */
6098 || TREE_CODE (up_bound
) != INTEGER_CST
)
6101 /* Accesses to trailing arrays via pointers may access storage
6102 beyond the types array bounds. */
6103 base
= get_base_address (ref
);
6104 if (base
&& TREE_CODE (base
) == MEM_REF
)
6106 tree cref
, next
= NULL_TREE
;
6108 if (TREE_CODE (TREE_OPERAND (ref
, 0)) != COMPONENT_REF
)
6111 cref
= TREE_OPERAND (ref
, 0);
6112 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (cref
, 0))) == RECORD_TYPE
)
6113 for (next
= DECL_CHAIN (TREE_OPERAND (cref
, 1));
6114 next
&& TREE_CODE (next
) != FIELD_DECL
;
6115 next
= DECL_CHAIN (next
))
6118 /* If this is the last field in a struct type or a field in a
6119 union type do not warn. */
6124 low_bound
= array_ref_low_bound (ref
);
6125 up_bound_p1
= int_const_binop (PLUS_EXPR
, up_bound
,
6126 build_int_cst (TREE_TYPE (up_bound
), 1));
6128 if (TREE_CODE (low_sub
) == SSA_NAME
)
6130 vr
= get_value_range (low_sub
);
6131 if (vr
->type
== VR_RANGE
|| vr
->type
== VR_ANTI_RANGE
)
6133 low_sub
= vr
->type
== VR_RANGE
? vr
->max
: vr
->min
;
6134 up_sub
= vr
->type
== VR_RANGE
? vr
->min
: vr
->max
;
6138 if (vr
&& vr
->type
== VR_ANTI_RANGE
)
6140 if (TREE_CODE (up_sub
) == INTEGER_CST
6141 && tree_int_cst_lt (up_bound
, up_sub
)
6142 && TREE_CODE (low_sub
) == INTEGER_CST
6143 && tree_int_cst_lt (low_sub
, low_bound
))
6145 warning_at (location
, OPT_Warray_bounds
,
6146 "array subscript is outside array bounds");
6147 TREE_NO_WARNING (ref
) = 1;
6150 else if (TREE_CODE (up_sub
) == INTEGER_CST
6151 && (ignore_off_by_one
6152 ? (tree_int_cst_lt (up_bound
, up_sub
)
6153 && !tree_int_cst_equal (up_bound_p1
, up_sub
))
6154 : (tree_int_cst_lt (up_bound
, up_sub
)
6155 || tree_int_cst_equal (up_bound_p1
, up_sub
))))
6157 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6159 fprintf (dump_file
, "Array bound warning for ");
6160 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, ref
);
6161 fprintf (dump_file
, "\n");
6163 warning_at (location
, OPT_Warray_bounds
,
6164 "array subscript is above array bounds");
6165 TREE_NO_WARNING (ref
) = 1;
6167 else if (TREE_CODE (low_sub
) == INTEGER_CST
6168 && tree_int_cst_lt (low_sub
, low_bound
))
6170 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6172 fprintf (dump_file
, "Array bound warning for ");
6173 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, ref
);
6174 fprintf (dump_file
, "\n");
6176 warning_at (location
, OPT_Warray_bounds
,
6177 "array subscript is below array bounds");
6178 TREE_NO_WARNING (ref
) = 1;
6182 /* Searches if the expr T, located at LOCATION computes
6183 address of an ARRAY_REF, and call check_array_ref on it. */
6186 search_for_addr_array (tree t
, location_t location
)
6188 while (TREE_CODE (t
) == SSA_NAME
)
6190 gimple g
= SSA_NAME_DEF_STMT (t
);
6192 if (gimple_code (g
) != GIMPLE_ASSIGN
)
6195 if (get_gimple_rhs_class (gimple_assign_rhs_code (g
))
6196 != GIMPLE_SINGLE_RHS
)
6199 t
= gimple_assign_rhs1 (g
);
6203 /* We are only interested in addresses of ARRAY_REF's. */
6204 if (TREE_CODE (t
) != ADDR_EXPR
)
6207 /* Check each ARRAY_REFs in the reference chain. */
6210 if (TREE_CODE (t
) == ARRAY_REF
)
6211 check_array_ref (location
, t
, true /*ignore_off_by_one*/);
6213 t
= TREE_OPERAND (t
, 0);
6215 while (handled_component_p (t
));
6217 if (TREE_CODE (t
) == MEM_REF
6218 && TREE_CODE (TREE_OPERAND (t
, 0)) == ADDR_EXPR
6219 && !TREE_NO_WARNING (t
))
6221 tree tem
= TREE_OPERAND (TREE_OPERAND (t
, 0), 0);
6222 tree low_bound
, up_bound
, el_sz
;
6224 if (TREE_CODE (TREE_TYPE (tem
)) != ARRAY_TYPE
6225 || TREE_CODE (TREE_TYPE (TREE_TYPE (tem
))) == ARRAY_TYPE
6226 || !TYPE_DOMAIN (TREE_TYPE (tem
)))
6229 low_bound
= TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (tem
)));
6230 up_bound
= TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (tem
)));
6231 el_sz
= TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (tem
)));
6233 || TREE_CODE (low_bound
) != INTEGER_CST
6235 || TREE_CODE (up_bound
) != INTEGER_CST
6237 || TREE_CODE (el_sz
) != INTEGER_CST
)
6240 idx
= mem_ref_offset (t
);
6241 idx
= idx
.sdiv_trunc (addr_wide_int (el_sz
));
6244 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6246 fprintf (dump_file
, "Array bound warning for ");
6247 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, t
);
6248 fprintf (dump_file
, "\n");
6250 warning_at (location
, OPT_Warray_bounds
,
6251 "array subscript is below array bounds");
6252 TREE_NO_WARNING (t
) = 1;
6254 else if (idx
.gts_p (addr_wide_int (up_bound
)
6258 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6260 fprintf (dump_file
, "Array bound warning for ");
6261 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, t
);
6262 fprintf (dump_file
, "\n");
6264 warning_at (location
, OPT_Warray_bounds
,
6265 "array subscript is above array bounds");
6266 TREE_NO_WARNING (t
) = 1;
6271 /* walk_tree() callback that checks if *TP is
6272 an ARRAY_REF inside an ADDR_EXPR (in which an array
6273 subscript one outside the valid range is allowed). Call
6274 check_array_ref for each ARRAY_REF found. The location is
6278 check_array_bounds (tree
*tp
, int *walk_subtree
, void *data
)
6281 struct walk_stmt_info
*wi
= (struct walk_stmt_info
*) data
;
6282 location_t location
;
6284 if (EXPR_HAS_LOCATION (t
))
6285 location
= EXPR_LOCATION (t
);
6288 location_t
*locp
= (location_t
*) wi
->info
;
6292 *walk_subtree
= TRUE
;
6294 if (TREE_CODE (t
) == ARRAY_REF
)
6295 check_array_ref (location
, t
, false /*ignore_off_by_one*/);
6297 if (TREE_CODE (t
) == MEM_REF
6298 || (TREE_CODE (t
) == RETURN_EXPR
&& TREE_OPERAND (t
, 0)))
6299 search_for_addr_array (TREE_OPERAND (t
, 0), location
);
6301 if (TREE_CODE (t
) == ADDR_EXPR
)
6302 *walk_subtree
= FALSE
;
6307 /* Walk over all statements of all reachable BBs and call check_array_bounds
6311 check_all_array_refs (void)
6314 gimple_stmt_iterator si
;
6320 bool executable
= false;
6322 /* Skip blocks that were found to be unreachable. */
6323 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
6324 executable
|= !!(e
->flags
& EDGE_EXECUTABLE
);
6328 for (si
= gsi_start_bb (bb
); !gsi_end_p (si
); gsi_next (&si
))
6330 gimple stmt
= gsi_stmt (si
);
6331 struct walk_stmt_info wi
;
6332 if (!gimple_has_location (stmt
))
6335 if (is_gimple_call (stmt
))
6338 size_t n
= gimple_call_num_args (stmt
);
6339 for (i
= 0; i
< n
; i
++)
6341 tree arg
= gimple_call_arg (stmt
, i
);
6342 search_for_addr_array (arg
, gimple_location (stmt
));
6347 memset (&wi
, 0, sizeof (wi
));
6348 wi
.info
= CONST_CAST (void *, (const void *)
6349 gimple_location_ptr (stmt
));
6351 walk_gimple_op (gsi_stmt (si
),
6359 /* Convert range assertion expressions into the implied copies and
6360 copy propagate away the copies. Doing the trivial copy propagation
6361 here avoids the need to run the full copy propagation pass after
6364 FIXME, this will eventually lead to copy propagation removing the
6365 names that had useful range information attached to them. For
6366 instance, if we had the assertion N_i = ASSERT_EXPR <N_j, N_j > 3>,
6367 then N_i will have the range [3, +INF].
6369 However, by converting the assertion into the implied copy
6370 operation N_i = N_j, we will then copy-propagate N_j into the uses
6371 of N_i and lose the range information. We may want to hold on to
6372 ASSERT_EXPRs a little while longer as the ranges could be used in
6373 things like jump threading.
6375 The problem with keeping ASSERT_EXPRs around is that passes after
6376 VRP need to handle them appropriately.
6378 Another approach would be to make the range information a first
6379 class property of the SSA_NAME so that it can be queried from
6380 any pass. This is made somewhat more complex by the need for
6381 multiple ranges to be associated with one SSA_NAME. */
6384 remove_range_assertions (void)
6387 gimple_stmt_iterator si
;
6389 /* Note that the BSI iterator bump happens at the bottom of the
6390 loop and no bump is necessary if we're removing the statement
6391 referenced by the current BSI. */
6393 for (si
= gsi_start_bb (bb
); !gsi_end_p (si
);)
6395 gimple stmt
= gsi_stmt (si
);
6398 if (is_gimple_assign (stmt
)
6399 && gimple_assign_rhs_code (stmt
) == ASSERT_EXPR
)
6401 tree rhs
= gimple_assign_rhs1 (stmt
);
6403 tree cond
= fold (ASSERT_EXPR_COND (rhs
));
6404 use_operand_p use_p
;
6405 imm_use_iterator iter
;
6407 gcc_assert (cond
!= boolean_false_node
);
6409 /* Propagate the RHS into every use of the LHS. */
6410 var
= ASSERT_EXPR_VAR (rhs
);
6411 FOR_EACH_IMM_USE_STMT (use_stmt
, iter
,
6412 gimple_assign_lhs (stmt
))
6413 FOR_EACH_IMM_USE_ON_STMT (use_p
, iter
)
6415 SET_USE (use_p
, var
);
6416 gcc_assert (TREE_CODE (var
) == SSA_NAME
);
6419 /* And finally, remove the copy, it is not needed. */
6420 gsi_remove (&si
, true);
6421 release_defs (stmt
);
6429 /* Return true if STMT is interesting for VRP. */
6432 stmt_interesting_for_vrp (gimple stmt
)
6434 if (gimple_code (stmt
) == GIMPLE_PHI
)
6436 tree res
= gimple_phi_result (stmt
);
6437 return (!virtual_operand_p (res
)
6438 && (INTEGRAL_TYPE_P (TREE_TYPE (res
))
6439 || POINTER_TYPE_P (TREE_TYPE (res
))));
6441 else if (is_gimple_assign (stmt
) || is_gimple_call (stmt
))
6443 tree lhs
= gimple_get_lhs (stmt
);
6445 /* In general, assignments with virtual operands are not useful
6446 for deriving ranges, with the obvious exception of calls to
6447 builtin functions. */
6448 if (lhs
&& TREE_CODE (lhs
) == SSA_NAME
6449 && (INTEGRAL_TYPE_P (TREE_TYPE (lhs
))
6450 || POINTER_TYPE_P (TREE_TYPE (lhs
)))
6451 && ((is_gimple_call (stmt
)
6452 && gimple_call_fndecl (stmt
) != NULL_TREE
6453 && DECL_BUILT_IN (gimple_call_fndecl (stmt
)))
6454 || !gimple_vuse (stmt
)))
6457 else if (gimple_code (stmt
) == GIMPLE_COND
6458 || gimple_code (stmt
) == GIMPLE_SWITCH
)
6465 /* Initialize local data structures for VRP. */
6468 vrp_initialize (void)
6472 values_propagated
= false;
6473 num_vr_values
= num_ssa_names
;
6474 vr_value
= XCNEWVEC (value_range_t
*, num_vr_values
);
6475 vr_phi_edge_counts
= XCNEWVEC (int, num_ssa_names
);
6479 gimple_stmt_iterator si
;
6481 for (si
= gsi_start_phis (bb
); !gsi_end_p (si
); gsi_next (&si
))
6483 gimple phi
= gsi_stmt (si
);
6484 if (!stmt_interesting_for_vrp (phi
))
6486 tree lhs
= PHI_RESULT (phi
);
6487 set_value_range_to_varying (get_value_range (lhs
));
6488 prop_set_simulate_again (phi
, false);
6491 prop_set_simulate_again (phi
, true);
6494 for (si
= gsi_start_bb (bb
); !gsi_end_p (si
); gsi_next (&si
))
6496 gimple stmt
= gsi_stmt (si
);
6498 /* If the statement is a control insn, then we do not
6499 want to avoid simulating the statement once. Failure
6500 to do so means that those edges will never get added. */
6501 if (stmt_ends_bb_p (stmt
))
6502 prop_set_simulate_again (stmt
, true);
6503 else if (!stmt_interesting_for_vrp (stmt
))
6507 FOR_EACH_SSA_TREE_OPERAND (def
, stmt
, i
, SSA_OP_DEF
)
6508 set_value_range_to_varying (get_value_range (def
));
6509 prop_set_simulate_again (stmt
, false);
6512 prop_set_simulate_again (stmt
, true);
6517 /* Return the singleton value-range for NAME or NAME. */
6520 vrp_valueize (tree name
)
6522 if (TREE_CODE (name
) == SSA_NAME
)
6524 value_range_t
*vr
= get_value_range (name
);
6525 if (vr
->type
== VR_RANGE
6526 && (vr
->min
== vr
->max
6527 || operand_equal_p (vr
->min
, vr
->max
, 0)))
6533 /* Visit assignment STMT. If it produces an interesting range, record
6534 the SSA name in *OUTPUT_P. */
6536 static enum ssa_prop_result
6537 vrp_visit_assignment_or_call (gimple stmt
, tree
*output_p
)
6541 enum gimple_code code
= gimple_code (stmt
);
6542 lhs
= gimple_get_lhs (stmt
);
6544 /* We only keep track of ranges in integral and pointer types. */
6545 if (TREE_CODE (lhs
) == SSA_NAME
6546 && ((INTEGRAL_TYPE_P (TREE_TYPE (lhs
))
6547 /* It is valid to have NULL MIN/MAX values on a type. See
6548 build_range_type. */
6549 && TYPE_MIN_VALUE (TREE_TYPE (lhs
))
6550 && TYPE_MAX_VALUE (TREE_TYPE (lhs
)))
6551 || POINTER_TYPE_P (TREE_TYPE (lhs
))))
6553 value_range_t new_vr
= VR_INITIALIZER
;
6555 /* Try folding the statement to a constant first. */
6556 tree tem
= gimple_fold_stmt_to_constant (stmt
, vrp_valueize
);
6557 if (tem
&& !is_overflow_infinity (tem
))
6558 set_value_range (&new_vr
, VR_RANGE
, tem
, tem
, NULL
);
6559 /* Then dispatch to value-range extracting functions. */
6560 else if (code
== GIMPLE_CALL
)
6561 extract_range_basic (&new_vr
, stmt
);
6563 extract_range_from_assignment (&new_vr
, stmt
);
6565 if (update_value_range (lhs
, &new_vr
))
6569 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6571 fprintf (dump_file
, "Found new range for ");
6572 print_generic_expr (dump_file
, lhs
, 0);
6573 fprintf (dump_file
, ": ");
6574 dump_value_range (dump_file
, &new_vr
);
6575 fprintf (dump_file
, "\n\n");
6578 if (new_vr
.type
== VR_VARYING
)
6579 return SSA_PROP_VARYING
;
6581 return SSA_PROP_INTERESTING
;
6584 return SSA_PROP_NOT_INTERESTING
;
6587 /* Every other statement produces no useful ranges. */
6588 FOR_EACH_SSA_TREE_OPERAND (def
, stmt
, iter
, SSA_OP_DEF
)
6589 set_value_range_to_varying (get_value_range (def
));
6591 return SSA_PROP_VARYING
;
6594 /* Helper that gets the value range of the SSA_NAME with version I
6595 or a symbolic range containing the SSA_NAME only if the value range
6596 is varying or undefined. */
6598 static inline value_range_t
6599 get_vr_for_comparison (int i
)
6601 value_range_t vr
= *get_value_range (ssa_name (i
));
6603 /* If name N_i does not have a valid range, use N_i as its own
6604 range. This allows us to compare against names that may
6605 have N_i in their ranges. */
6606 if (vr
.type
== VR_VARYING
|| vr
.type
== VR_UNDEFINED
)
6609 vr
.min
= ssa_name (i
);
6610 vr
.max
= ssa_name (i
);
6616 /* Compare all the value ranges for names equivalent to VAR with VAL
6617 using comparison code COMP. Return the same value returned by
6618 compare_range_with_value, including the setting of
6619 *STRICT_OVERFLOW_P. */
6622 compare_name_with_value (enum tree_code comp
, tree var
, tree val
,
6623 bool *strict_overflow_p
)
6629 int used_strict_overflow
;
6631 value_range_t equiv_vr
;
6633 /* Get the set of equivalences for VAR. */
6634 e
= get_value_range (var
)->equiv
;
6636 /* Start at -1. Set it to 0 if we do a comparison without relying
6637 on overflow, or 1 if all comparisons rely on overflow. */
6638 used_strict_overflow
= -1;
6640 /* Compare vars' value range with val. */
6641 equiv_vr
= get_vr_for_comparison (SSA_NAME_VERSION (var
));
6643 retval
= compare_range_with_value (comp
, &equiv_vr
, val
, &sop
);
6645 used_strict_overflow
= sop
? 1 : 0;
6647 /* If the equiv set is empty we have done all work we need to do. */
6651 && used_strict_overflow
> 0)
6652 *strict_overflow_p
= true;
6656 EXECUTE_IF_SET_IN_BITMAP (e
, 0, i
, bi
)
6658 equiv_vr
= get_vr_for_comparison (i
);
6660 t
= compare_range_with_value (comp
, &equiv_vr
, val
, &sop
);
6663 /* If we get different answers from different members
6664 of the equivalence set this check must be in a dead
6665 code region. Folding it to a trap representation
6666 would be correct here. For now just return don't-know. */
6676 used_strict_overflow
= 0;
6677 else if (used_strict_overflow
< 0)
6678 used_strict_overflow
= 1;
6683 && used_strict_overflow
> 0)
6684 *strict_overflow_p
= true;
6690 /* Given a comparison code COMP and names N1 and N2, compare all the
6691 ranges equivalent to N1 against all the ranges equivalent to N2
6692 to determine the value of N1 COMP N2. Return the same value
6693 returned by compare_ranges. Set *STRICT_OVERFLOW_P to indicate
6694 whether we relied on an overflow infinity in the comparison. */
6698 compare_names (enum tree_code comp
, tree n1
, tree n2
,
6699 bool *strict_overflow_p
)
6703 bitmap_iterator bi1
, bi2
;
6705 int used_strict_overflow
;
6706 static bitmap_obstack
*s_obstack
= NULL
;
6707 static bitmap s_e1
= NULL
, s_e2
= NULL
;
6709 /* Compare the ranges of every name equivalent to N1 against the
6710 ranges of every name equivalent to N2. */
6711 e1
= get_value_range (n1
)->equiv
;
6712 e2
= get_value_range (n2
)->equiv
;
6714 /* Use the fake bitmaps if e1 or e2 are not available. */
6715 if (s_obstack
== NULL
)
6717 s_obstack
= XNEW (bitmap_obstack
);
6718 bitmap_obstack_initialize (s_obstack
);
6719 s_e1
= BITMAP_ALLOC (s_obstack
);
6720 s_e2
= BITMAP_ALLOC (s_obstack
);
6727 /* Add N1 and N2 to their own set of equivalences to avoid
6728 duplicating the body of the loop just to check N1 and N2
6730 bitmap_set_bit (e1
, SSA_NAME_VERSION (n1
));
6731 bitmap_set_bit (e2
, SSA_NAME_VERSION (n2
));
6733 /* If the equivalence sets have a common intersection, then the two
6734 names can be compared without checking their ranges. */
6735 if (bitmap_intersect_p (e1
, e2
))
6737 bitmap_clear_bit (e1
, SSA_NAME_VERSION (n1
));
6738 bitmap_clear_bit (e2
, SSA_NAME_VERSION (n2
));
6740 return (comp
== EQ_EXPR
|| comp
== GE_EXPR
|| comp
== LE_EXPR
)
6742 : boolean_false_node
;
6745 /* Start at -1. Set it to 0 if we do a comparison without relying
6746 on overflow, or 1 if all comparisons rely on overflow. */
6747 used_strict_overflow
= -1;
6749 /* Otherwise, compare all the equivalent ranges. First, add N1 and
6750 N2 to their own set of equivalences to avoid duplicating the body
6751 of the loop just to check N1 and N2 ranges. */
6752 EXECUTE_IF_SET_IN_BITMAP (e1
, 0, i1
, bi1
)
6754 value_range_t vr1
= get_vr_for_comparison (i1
);
6756 t
= retval
= NULL_TREE
;
6757 EXECUTE_IF_SET_IN_BITMAP (e2
, 0, i2
, bi2
)
6761 value_range_t vr2
= get_vr_for_comparison (i2
);
6763 t
= compare_ranges (comp
, &vr1
, &vr2
, &sop
);
6766 /* If we get different answers from different members
6767 of the equivalence set this check must be in a dead
6768 code region. Folding it to a trap representation
6769 would be correct here. For now just return don't-know. */
6773 bitmap_clear_bit (e1
, SSA_NAME_VERSION (n1
));
6774 bitmap_clear_bit (e2
, SSA_NAME_VERSION (n2
));
6780 used_strict_overflow
= 0;
6781 else if (used_strict_overflow
< 0)
6782 used_strict_overflow
= 1;
6788 bitmap_clear_bit (e1
, SSA_NAME_VERSION (n1
));
6789 bitmap_clear_bit (e2
, SSA_NAME_VERSION (n2
));
6790 if (used_strict_overflow
> 0)
6791 *strict_overflow_p
= true;
6796 /* None of the equivalent ranges are useful in computing this
6798 bitmap_clear_bit (e1
, SSA_NAME_VERSION (n1
));
6799 bitmap_clear_bit (e2
, SSA_NAME_VERSION (n2
));
6803 /* Helper function for vrp_evaluate_conditional_warnv. */
6806 vrp_evaluate_conditional_warnv_with_ops_using_ranges (enum tree_code code
,
6808 bool * strict_overflow_p
)
6810 value_range_t
*vr0
, *vr1
;
6812 vr0
= (TREE_CODE (op0
) == SSA_NAME
) ? get_value_range (op0
) : NULL
;
6813 vr1
= (TREE_CODE (op1
) == SSA_NAME
) ? get_value_range (op1
) : NULL
;
6816 return compare_ranges (code
, vr0
, vr1
, strict_overflow_p
);
6817 else if (vr0
&& vr1
== NULL
)
6818 return compare_range_with_value (code
, vr0
, op1
, strict_overflow_p
);
6819 else if (vr0
== NULL
&& vr1
)
6820 return (compare_range_with_value
6821 (swap_tree_comparison (code
), vr1
, op0
, strict_overflow_p
));
6825 /* Helper function for vrp_evaluate_conditional_warnv. */
6828 vrp_evaluate_conditional_warnv_with_ops (enum tree_code code
, tree op0
,
6829 tree op1
, bool use_equiv_p
,
6830 bool *strict_overflow_p
, bool *only_ranges
)
6834 *only_ranges
= true;
6836 /* We only deal with integral and pointer types. */
6837 if (!INTEGRAL_TYPE_P (TREE_TYPE (op0
))
6838 && !POINTER_TYPE_P (TREE_TYPE (op0
)))
6844 && (ret
= vrp_evaluate_conditional_warnv_with_ops_using_ranges
6845 (code
, op0
, op1
, strict_overflow_p
)))
6847 *only_ranges
= false;
6848 if (TREE_CODE (op0
) == SSA_NAME
&& TREE_CODE (op1
) == SSA_NAME
)
6849 return compare_names (code
, op0
, op1
, strict_overflow_p
);
6850 else if (TREE_CODE (op0
) == SSA_NAME
)
6851 return compare_name_with_value (code
, op0
, op1
, strict_overflow_p
);
6852 else if (TREE_CODE (op1
) == SSA_NAME
)
6853 return (compare_name_with_value
6854 (swap_tree_comparison (code
), op1
, op0
, strict_overflow_p
));
6857 return vrp_evaluate_conditional_warnv_with_ops_using_ranges (code
, op0
, op1
,
6862 /* Given (CODE OP0 OP1) within STMT, try to simplify it based on value range
6863 information. Return NULL if the conditional can not be evaluated.
6864 The ranges of all the names equivalent with the operands in COND
6865 will be used when trying to compute the value. If the result is
6866 based on undefined signed overflow, issue a warning if
6870 vrp_evaluate_conditional (enum tree_code code
, tree op0
, tree op1
, gimple stmt
)
6876 /* Some passes and foldings leak constants with overflow flag set
6877 into the IL. Avoid doing wrong things with these and bail out. */
6878 if ((TREE_CODE (op0
) == INTEGER_CST
6879 && TREE_OVERFLOW (op0
))
6880 || (TREE_CODE (op1
) == INTEGER_CST
6881 && TREE_OVERFLOW (op1
)))
6885 ret
= vrp_evaluate_conditional_warnv_with_ops (code
, op0
, op1
, true, &sop
,
6890 enum warn_strict_overflow_code wc
;
6891 const char* warnmsg
;
6893 if (is_gimple_min_invariant (ret
))
6895 wc
= WARN_STRICT_OVERFLOW_CONDITIONAL
;
6896 warnmsg
= G_("assuming signed overflow does not occur when "
6897 "simplifying conditional to constant");
6901 wc
= WARN_STRICT_OVERFLOW_COMPARISON
;
6902 warnmsg
= G_("assuming signed overflow does not occur when "
6903 "simplifying conditional");
6906 if (issue_strict_overflow_warning (wc
))
6908 location_t location
;
6910 if (!gimple_has_location (stmt
))
6911 location
= input_location
;
6913 location
= gimple_location (stmt
);
6914 warning_at (location
, OPT_Wstrict_overflow
, "%s", warnmsg
);
6918 if (warn_type_limits
6919 && ret
&& only_ranges
6920 && TREE_CODE_CLASS (code
) == tcc_comparison
6921 && TREE_CODE (op0
) == SSA_NAME
)
6923 /* If the comparison is being folded and the operand on the LHS
6924 is being compared against a constant value that is outside of
6925 the natural range of OP0's type, then the predicate will
6926 always fold regardless of the value of OP0. If -Wtype-limits
6927 was specified, emit a warning. */
6928 tree type
= TREE_TYPE (op0
);
6929 value_range_t
*vr0
= get_value_range (op0
);
6931 if (vr0
->type
!= VR_VARYING
6932 && INTEGRAL_TYPE_P (type
)
6933 && vrp_val_is_min (vr0
->min
)
6934 && vrp_val_is_max (vr0
->max
)
6935 && is_gimple_min_invariant (op1
))
6937 location_t location
;
6939 if (!gimple_has_location (stmt
))
6940 location
= input_location
;
6942 location
= gimple_location (stmt
);
6944 warning_at (location
, OPT_Wtype_limits
,
6946 ? G_("comparison always false "
6947 "due to limited range of data type")
6948 : G_("comparison always true "
6949 "due to limited range of data type"));
6957 /* Visit conditional statement STMT. If we can determine which edge
6958 will be taken out of STMT's basic block, record it in
6959 *TAKEN_EDGE_P and return SSA_PROP_INTERESTING. Otherwise, return
6960 SSA_PROP_VARYING. */
6962 static enum ssa_prop_result
6963 vrp_visit_cond_stmt (gimple stmt
, edge
*taken_edge_p
)
6968 *taken_edge_p
= NULL
;
6970 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6975 fprintf (dump_file
, "\nVisiting conditional with predicate: ");
6976 print_gimple_stmt (dump_file
, stmt
, 0, 0);
6977 fprintf (dump_file
, "\nWith known ranges\n");
6979 FOR_EACH_SSA_TREE_OPERAND (use
, stmt
, i
, SSA_OP_USE
)
6981 fprintf (dump_file
, "\t");
6982 print_generic_expr (dump_file
, use
, 0);
6983 fprintf (dump_file
, ": ");
6984 dump_value_range (dump_file
, vr_value
[SSA_NAME_VERSION (use
)]);
6987 fprintf (dump_file
, "\n");
6990 /* Compute the value of the predicate COND by checking the known
6991 ranges of each of its operands.
6993 Note that we cannot evaluate all the equivalent ranges here
6994 because those ranges may not yet be final and with the current
6995 propagation strategy, we cannot determine when the value ranges
6996 of the names in the equivalence set have changed.
6998 For instance, given the following code fragment
7002 i_14 = ASSERT_EXPR <i_5, i_5 != 0>
7006 Assume that on the first visit to i_14, i_5 has the temporary
7007 range [8, 8] because the second argument to the PHI function is
7008 not yet executable. We derive the range ~[0, 0] for i_14 and the
7009 equivalence set { i_5 }. So, when we visit 'if (i_14 == 1)' for
7010 the first time, since i_14 is equivalent to the range [8, 8], we
7011 determine that the predicate is always false.
7013 On the next round of propagation, i_13 is determined to be
7014 VARYING, which causes i_5 to drop down to VARYING. So, another
7015 visit to i_14 is scheduled. In this second visit, we compute the
7016 exact same range and equivalence set for i_14, namely ~[0, 0] and
7017 { i_5 }. But we did not have the previous range for i_5
7018 registered, so vrp_visit_assignment thinks that the range for
7019 i_14 has not changed. Therefore, the predicate 'if (i_14 == 1)'
7020 is not visited again, which stops propagation from visiting
7021 statements in the THEN clause of that if().
7023 To properly fix this we would need to keep the previous range
7024 value for the names in the equivalence set. This way we would've
7025 discovered that from one visit to the other i_5 changed from
7026 range [8, 8] to VR_VARYING.
7028 However, fixing this apparent limitation may not be worth the
7029 additional checking. Testing on several code bases (GCC, DLV,
7030 MICO, TRAMP3D and SPEC2000) showed that doing this results in
7031 4 more predicates folded in SPEC. */
7034 val
= vrp_evaluate_conditional_warnv_with_ops (gimple_cond_code (stmt
),
7035 gimple_cond_lhs (stmt
),
7036 gimple_cond_rhs (stmt
),
7041 *taken_edge_p
= find_taken_edge (gimple_bb (stmt
), val
);
7044 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7046 "\nIgnoring predicate evaluation because "
7047 "it assumes that signed overflow is undefined");
7052 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7054 fprintf (dump_file
, "\nPredicate evaluates to: ");
7055 if (val
== NULL_TREE
)
7056 fprintf (dump_file
, "DON'T KNOW\n");
7058 print_generic_stmt (dump_file
, val
, 0);
7061 return (*taken_edge_p
) ? SSA_PROP_INTERESTING
: SSA_PROP_VARYING
;
7064 /* Searches the case label vector VEC for the index *IDX of the CASE_LABEL
7065 that includes the value VAL. The search is restricted to the range
7066 [START_IDX, n - 1] where n is the size of VEC.
7068 If there is a CASE_LABEL for VAL, its index is placed in IDX and true is
7071 If there is no CASE_LABEL for VAL and there is one that is larger than VAL,
7072 it is placed in IDX and false is returned.
7074 If VAL is larger than any CASE_LABEL, n is placed on IDX and false is
7078 find_case_label_index (gimple stmt
, size_t start_idx
, tree val
, size_t *idx
)
7080 size_t n
= gimple_switch_num_labels (stmt
);
7083 /* Find case label for minimum of the value range or the next one.
7084 At each iteration we are searching in [low, high - 1]. */
7086 for (low
= start_idx
, high
= n
; high
!= low
; )
7090 /* Note that i != high, so we never ask for n. */
7091 size_t i
= (high
+ low
) / 2;
7092 t
= gimple_switch_label (stmt
, i
);
7094 /* Cache the result of comparing CASE_LOW and val. */
7095 cmp
= tree_int_cst_compare (CASE_LOW (t
), val
);
7099 /* Ranges cannot be empty. */
7108 if (CASE_HIGH (t
) != NULL
7109 && tree_int_cst_compare (CASE_HIGH (t
), val
) >= 0)
7121 /* Searches the case label vector VEC for the range of CASE_LABELs that is used
7122 for values between MIN and MAX. The first index is placed in MIN_IDX. The
7123 last index is placed in MAX_IDX. If the range of CASE_LABELs is empty
7124 then MAX_IDX < MIN_IDX.
7125 Returns true if the default label is not needed. */
7128 find_case_label_range (gimple stmt
, tree min
, tree max
, size_t *min_idx
,
7132 bool min_take_default
= !find_case_label_index (stmt
, 1, min
, &i
);
7133 bool max_take_default
= !find_case_label_index (stmt
, i
, max
, &j
);
7137 && max_take_default
)
7139 /* Only the default case label reached.
7140 Return an empty range. */
7147 bool take_default
= min_take_default
|| max_take_default
;
7151 if (max_take_default
)
7154 /* If the case label range is continuous, we do not need
7155 the default case label. Verify that. */
7156 high
= CASE_LOW (gimple_switch_label (stmt
, i
));
7157 if (CASE_HIGH (gimple_switch_label (stmt
, i
)))
7158 high
= CASE_HIGH (gimple_switch_label (stmt
, i
));
7159 for (k
= i
+ 1; k
<= j
; ++k
)
7161 low
= CASE_LOW (gimple_switch_label (stmt
, k
));
7162 if (!integer_onep (int_const_binop (MINUS_EXPR
, low
, high
)))
7164 take_default
= true;
7168 if (CASE_HIGH (gimple_switch_label (stmt
, k
)))
7169 high
= CASE_HIGH (gimple_switch_label (stmt
, k
));
7174 return !take_default
;
7178 /* Searches the case label vector VEC for the ranges of CASE_LABELs that are
7179 used in range VR. The indices are placed in MIN_IDX1, MAX_IDX, MIN_IDX2 and
7180 MAX_IDX2. If the ranges of CASE_LABELs are empty then MAX_IDX1 < MIN_IDX1.
7181 Returns true if the default label is not needed. */
7184 find_case_label_ranges (gimple stmt
, value_range_t
*vr
, size_t *min_idx1
,
7185 size_t *max_idx1
, size_t *min_idx2
,
7189 unsigned int n
= gimple_switch_num_labels (stmt
);
7191 tree case_low
, case_high
;
7192 tree min
= vr
->min
, max
= vr
->max
;
7194 gcc_checking_assert (vr
->type
== VR_RANGE
|| vr
->type
== VR_ANTI_RANGE
);
7196 take_default
= !find_case_label_range (stmt
, min
, max
, &i
, &j
);
7198 /* Set second range to emtpy. */
7202 if (vr
->type
== VR_RANGE
)
7206 return !take_default
;
7209 /* Set first range to all case labels. */
7216 /* Make sure all the values of case labels [i , j] are contained in
7217 range [MIN, MAX]. */
7218 case_low
= CASE_LOW (gimple_switch_label (stmt
, i
));
7219 case_high
= CASE_HIGH (gimple_switch_label (stmt
, j
));
7220 if (tree_int_cst_compare (case_low
, min
) < 0)
7222 if (case_high
!= NULL_TREE
7223 && tree_int_cst_compare (max
, case_high
) < 0)
7229 /* If the range spans case labels [i, j], the corresponding anti-range spans
7230 the labels [1, i - 1] and [j + 1, n - 1]. */
7256 /* Visit switch statement STMT. If we can determine which edge
7257 will be taken out of STMT's basic block, record it in
7258 *TAKEN_EDGE_P and return SSA_PROP_INTERESTING. Otherwise, return
7259 SSA_PROP_VARYING. */
7261 static enum ssa_prop_result
7262 vrp_visit_switch_stmt (gimple stmt
, edge
*taken_edge_p
)
7266 size_t i
= 0, j
= 0, k
, l
;
7269 *taken_edge_p
= NULL
;
7270 op
= gimple_switch_index (stmt
);
7271 if (TREE_CODE (op
) != SSA_NAME
)
7272 return SSA_PROP_VARYING
;
7274 vr
= get_value_range (op
);
7275 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7277 fprintf (dump_file
, "\nVisiting switch expression with operand ");
7278 print_generic_expr (dump_file
, op
, 0);
7279 fprintf (dump_file
, " with known range ");
7280 dump_value_range (dump_file
, vr
);
7281 fprintf (dump_file
, "\n");
7284 if ((vr
->type
!= VR_RANGE
7285 && vr
->type
!= VR_ANTI_RANGE
)
7286 || symbolic_range_p (vr
))
7287 return SSA_PROP_VARYING
;
7289 /* Find the single edge that is taken from the switch expression. */
7290 take_default
= !find_case_label_ranges (stmt
, vr
, &i
, &j
, &k
, &l
);
7292 /* Check if the range spans no CASE_LABEL. If so, we only reach the default
7296 gcc_assert (take_default
);
7297 val
= gimple_switch_default_label (stmt
);
7301 /* Check if labels with index i to j and maybe the default label
7302 are all reaching the same label. */
7304 val
= gimple_switch_label (stmt
, i
);
7306 && CASE_LABEL (gimple_switch_default_label (stmt
))
7307 != CASE_LABEL (val
))
7309 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7310 fprintf (dump_file
, " not a single destination for this "
7312 return SSA_PROP_VARYING
;
7314 for (++i
; i
<= j
; ++i
)
7316 if (CASE_LABEL (gimple_switch_label (stmt
, i
)) != CASE_LABEL (val
))
7318 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7319 fprintf (dump_file
, " not a single destination for this "
7321 return SSA_PROP_VARYING
;
7326 if (CASE_LABEL (gimple_switch_label (stmt
, k
)) != CASE_LABEL (val
))
7328 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7329 fprintf (dump_file
, " not a single destination for this "
7331 return SSA_PROP_VARYING
;
7336 *taken_edge_p
= find_edge (gimple_bb (stmt
),
7337 label_to_block (CASE_LABEL (val
)));
7339 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7341 fprintf (dump_file
, " will take edge to ");
7342 print_generic_stmt (dump_file
, CASE_LABEL (val
), 0);
7345 return SSA_PROP_INTERESTING
;
7349 /* Evaluate statement STMT. If the statement produces a useful range,
7350 return SSA_PROP_INTERESTING and record the SSA name with the
7351 interesting range into *OUTPUT_P.
7353 If STMT is a conditional branch and we can determine its truth
7354 value, the taken edge is recorded in *TAKEN_EDGE_P.
7356 If STMT produces a varying value, return SSA_PROP_VARYING. */
7358 static enum ssa_prop_result
7359 vrp_visit_stmt (gimple stmt
, edge
*taken_edge_p
, tree
*output_p
)
7364 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7366 fprintf (dump_file
, "\nVisiting statement:\n");
7367 print_gimple_stmt (dump_file
, stmt
, 0, dump_flags
);
7368 fprintf (dump_file
, "\n");
7371 if (!stmt_interesting_for_vrp (stmt
))
7372 gcc_assert (stmt_ends_bb_p (stmt
));
7373 else if (is_gimple_assign (stmt
) || is_gimple_call (stmt
))
7375 /* In general, assignments with virtual operands are not useful
7376 for deriving ranges, with the obvious exception of calls to
7377 builtin functions. */
7378 if ((is_gimple_call (stmt
)
7379 && gimple_call_fndecl (stmt
) != NULL_TREE
7380 && DECL_BUILT_IN (gimple_call_fndecl (stmt
)))
7381 || !gimple_vuse (stmt
))
7382 return vrp_visit_assignment_or_call (stmt
, output_p
);
7384 else if (gimple_code (stmt
) == GIMPLE_COND
)
7385 return vrp_visit_cond_stmt (stmt
, taken_edge_p
);
7386 else if (gimple_code (stmt
) == GIMPLE_SWITCH
)
7387 return vrp_visit_switch_stmt (stmt
, taken_edge_p
);
7389 /* All other statements produce nothing of interest for VRP, so mark
7390 their outputs varying and prevent further simulation. */
7391 FOR_EACH_SSA_TREE_OPERAND (def
, stmt
, iter
, SSA_OP_DEF
)
7392 set_value_range_to_varying (get_value_range (def
));
7394 return SSA_PROP_VARYING
;
7397 /* Union the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
7398 { VR1TYPE, VR0MIN, VR0MAX } and store the result
7399 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
7400 possible such range. The resulting range is not canonicalized. */
7403 union_ranges (enum value_range_type
*vr0type
,
7404 tree
*vr0min
, tree
*vr0max
,
7405 enum value_range_type vr1type
,
7406 tree vr1min
, tree vr1max
)
7408 bool mineq
= operand_equal_p (*vr0min
, vr1min
, 0);
7409 bool maxeq
= operand_equal_p (*vr0max
, vr1max
, 0);
7411 /* [] is vr0, () is vr1 in the following classification comments. */
7415 if (*vr0type
== vr1type
)
7416 /* Nothing to do for equal ranges. */
7418 else if ((*vr0type
== VR_RANGE
7419 && vr1type
== VR_ANTI_RANGE
)
7420 || (*vr0type
== VR_ANTI_RANGE
7421 && vr1type
== VR_RANGE
))
7423 /* For anti-range with range union the result is varying. */
7429 else if (operand_less_p (*vr0max
, vr1min
) == 1
7430 || operand_less_p (vr1max
, *vr0min
) == 1)
7432 /* [ ] ( ) or ( ) [ ]
7433 If the ranges have an empty intersection, result of the union
7434 operation is the anti-range or if both are anti-ranges
7436 if (*vr0type
== VR_ANTI_RANGE
7437 && vr1type
== VR_ANTI_RANGE
)
7439 else if (*vr0type
== VR_ANTI_RANGE
7440 && vr1type
== VR_RANGE
)
7442 else if (*vr0type
== VR_RANGE
7443 && vr1type
== VR_ANTI_RANGE
)
7449 else if (*vr0type
== VR_RANGE
7450 && vr1type
== VR_RANGE
)
7452 /* The result is the convex hull of both ranges. */
7453 if (operand_less_p (*vr0max
, vr1min
) == 1)
7455 /* If the result can be an anti-range, create one. */
7456 if (TREE_CODE (*vr0max
) == INTEGER_CST
7457 && TREE_CODE (vr1min
) == INTEGER_CST
7458 && vrp_val_is_min (*vr0min
)
7459 && vrp_val_is_max (vr1max
))
7461 tree min
= int_const_binop (PLUS_EXPR
,
7463 build_int_cst (TREE_TYPE (*vr0max
), 1));
7464 tree max
= int_const_binop (MINUS_EXPR
,
7466 build_int_cst (TREE_TYPE (vr1min
), 1));
7467 if (!operand_less_p (max
, min
))
7469 *vr0type
= VR_ANTI_RANGE
;
7481 /* If the result can be an anti-range, create one. */
7482 if (TREE_CODE (vr1max
) == INTEGER_CST
7483 && TREE_CODE (*vr0min
) == INTEGER_CST
7484 && vrp_val_is_min (vr1min
)
7485 && vrp_val_is_max (*vr0max
))
7487 tree min
= int_const_binop (PLUS_EXPR
,
7489 build_int_cst (TREE_TYPE (vr1max
), 1));
7490 tree max
= int_const_binop (MINUS_EXPR
,
7492 build_int_cst (TREE_TYPE (*vr0min
), 1));
7493 if (!operand_less_p (max
, min
))
7495 *vr0type
= VR_ANTI_RANGE
;
7509 else if ((maxeq
|| operand_less_p (vr1max
, *vr0max
) == 1)
7510 && (mineq
|| operand_less_p (*vr0min
, vr1min
) == 1))
7512 /* [ ( ) ] or [( ) ] or [ ( )] */
7513 if (*vr0type
== VR_RANGE
7514 && vr1type
== VR_RANGE
)
7516 else if (*vr0type
== VR_ANTI_RANGE
7517 && vr1type
== VR_ANTI_RANGE
)
7523 else if (*vr0type
== VR_ANTI_RANGE
7524 && vr1type
== VR_RANGE
)
7526 /* Arbitrarily choose the right or left gap. */
7527 if (!mineq
&& TREE_CODE (vr1min
) == INTEGER_CST
)
7528 *vr0max
= int_const_binop (MINUS_EXPR
, vr1min
,
7529 build_int_cst (TREE_TYPE (vr1min
), 1));
7530 else if (!maxeq
&& TREE_CODE (vr1max
) == INTEGER_CST
)
7531 *vr0min
= int_const_binop (PLUS_EXPR
, vr1max
,
7532 build_int_cst (TREE_TYPE (vr1max
), 1));
7536 else if (*vr0type
== VR_RANGE
7537 && vr1type
== VR_ANTI_RANGE
)
7538 /* The result covers everything. */
7543 else if ((maxeq
|| operand_less_p (*vr0max
, vr1max
) == 1)
7544 && (mineq
|| operand_less_p (vr1min
, *vr0min
) == 1))
7546 /* ( [ ] ) or ([ ] ) or ( [ ]) */
7547 if (*vr0type
== VR_RANGE
7548 && vr1type
== VR_RANGE
)
7554 else if (*vr0type
== VR_ANTI_RANGE
7555 && vr1type
== VR_ANTI_RANGE
)
7557 else if (*vr0type
== VR_RANGE
7558 && vr1type
== VR_ANTI_RANGE
)
7560 *vr0type
= VR_ANTI_RANGE
;
7561 if (!mineq
&& TREE_CODE (*vr0min
) == INTEGER_CST
)
7563 *vr0max
= int_const_binop (MINUS_EXPR
, *vr0min
,
7564 build_int_cst (TREE_TYPE (*vr0min
), 1));
7567 else if (!maxeq
&& TREE_CODE (*vr0max
) == INTEGER_CST
)
7569 *vr0min
= int_const_binop (PLUS_EXPR
, *vr0max
,
7570 build_int_cst (TREE_TYPE (*vr0max
), 1));
7576 else if (*vr0type
== VR_ANTI_RANGE
7577 && vr1type
== VR_RANGE
)
7578 /* The result covers everything. */
7583 else if ((operand_less_p (vr1min
, *vr0max
) == 1
7584 || operand_equal_p (vr1min
, *vr0max
, 0))
7585 && operand_less_p (*vr0min
, vr1min
) == 1)
7587 /* [ ( ] ) or [ ]( ) */
7588 if (*vr0type
== VR_RANGE
7589 && vr1type
== VR_RANGE
)
7591 else if (*vr0type
== VR_ANTI_RANGE
7592 && vr1type
== VR_ANTI_RANGE
)
7594 else if (*vr0type
== VR_ANTI_RANGE
7595 && vr1type
== VR_RANGE
)
7597 if (TREE_CODE (vr1min
) == INTEGER_CST
)
7598 *vr0max
= int_const_binop (MINUS_EXPR
, vr1min
,
7599 build_int_cst (TREE_TYPE (vr1min
), 1));
7603 else if (*vr0type
== VR_RANGE
7604 && vr1type
== VR_ANTI_RANGE
)
7606 if (TREE_CODE (*vr0max
) == INTEGER_CST
)
7609 *vr0min
= int_const_binop (PLUS_EXPR
, *vr0max
,
7610 build_int_cst (TREE_TYPE (*vr0max
), 1));
7619 else if ((operand_less_p (*vr0min
, vr1max
) == 1
7620 || operand_equal_p (*vr0min
, vr1max
, 0))
7621 && operand_less_p (vr1min
, *vr0min
) == 1)
7623 /* ( [ ) ] or ( )[ ] */
7624 if (*vr0type
== VR_RANGE
7625 && vr1type
== VR_RANGE
)
7627 else if (*vr0type
== VR_ANTI_RANGE
7628 && vr1type
== VR_ANTI_RANGE
)
7630 else if (*vr0type
== VR_ANTI_RANGE
7631 && vr1type
== VR_RANGE
)
7633 if (TREE_CODE (vr1max
) == INTEGER_CST
)
7634 *vr0min
= int_const_binop (PLUS_EXPR
, vr1max
,
7635 build_int_cst (TREE_TYPE (vr1max
), 1));
7639 else if (*vr0type
== VR_RANGE
7640 && vr1type
== VR_ANTI_RANGE
)
7642 if (TREE_CODE (*vr0min
) == INTEGER_CST
)
7646 *vr0max
= int_const_binop (MINUS_EXPR
, *vr0min
,
7647 build_int_cst (TREE_TYPE (*vr0min
), 1));
7661 *vr0type
= VR_VARYING
;
7662 *vr0min
= NULL_TREE
;
7663 *vr0max
= NULL_TREE
;
7666 /* Intersect the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
7667 { VR1TYPE, VR0MIN, VR0MAX } and store the result
7668 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
7669 possible such range. The resulting range is not canonicalized. */
7672 intersect_ranges (enum value_range_type
*vr0type
,
7673 tree
*vr0min
, tree
*vr0max
,
7674 enum value_range_type vr1type
,
7675 tree vr1min
, tree vr1max
)
7677 bool mineq
= operand_equal_p (*vr0min
, vr1min
, 0);
7678 bool maxeq
= operand_equal_p (*vr0max
, vr1max
, 0);
7680 /* [] is vr0, () is vr1 in the following classification comments. */
7684 if (*vr0type
== vr1type
)
7685 /* Nothing to do for equal ranges. */
7687 else if ((*vr0type
== VR_RANGE
7688 && vr1type
== VR_ANTI_RANGE
)
7689 || (*vr0type
== VR_ANTI_RANGE
7690 && vr1type
== VR_RANGE
))
7692 /* For anti-range with range intersection the result is empty. */
7693 *vr0type
= VR_UNDEFINED
;
7694 *vr0min
= NULL_TREE
;
7695 *vr0max
= NULL_TREE
;
7700 else if (operand_less_p (*vr0max
, vr1min
) == 1
7701 || operand_less_p (vr1max
, *vr0min
) == 1)
7703 /* [ ] ( ) or ( ) [ ]
7704 If the ranges have an empty intersection, the result of the
7705 intersect operation is the range for intersecting an
7706 anti-range with a range or empty when intersecting two ranges. */
7707 if (*vr0type
== VR_RANGE
7708 && vr1type
== VR_ANTI_RANGE
)
7710 else if (*vr0type
== VR_ANTI_RANGE
7711 && vr1type
== VR_RANGE
)
7717 else if (*vr0type
== VR_RANGE
7718 && vr1type
== VR_RANGE
)
7720 *vr0type
= VR_UNDEFINED
;
7721 *vr0min
= NULL_TREE
;
7722 *vr0max
= NULL_TREE
;
7724 else if (*vr0type
== VR_ANTI_RANGE
7725 && vr1type
== VR_ANTI_RANGE
)
7727 /* If the anti-ranges are adjacent to each other merge them. */
7728 if (TREE_CODE (*vr0max
) == INTEGER_CST
7729 && TREE_CODE (vr1min
) == INTEGER_CST
7730 && operand_less_p (*vr0max
, vr1min
) == 1
7731 && integer_onep (int_const_binop (MINUS_EXPR
,
7734 else if (TREE_CODE (vr1max
) == INTEGER_CST
7735 && TREE_CODE (*vr0min
) == INTEGER_CST
7736 && operand_less_p (vr1max
, *vr0min
) == 1
7737 && integer_onep (int_const_binop (MINUS_EXPR
,
7740 /* Else arbitrarily take VR0. */
7743 else if ((maxeq
|| operand_less_p (vr1max
, *vr0max
) == 1)
7744 && (mineq
|| operand_less_p (*vr0min
, vr1min
) == 1))
7746 /* [ ( ) ] or [( ) ] or [ ( )] */
7747 if (*vr0type
== VR_RANGE
7748 && vr1type
== VR_RANGE
)
7750 /* If both are ranges the result is the inner one. */
7755 else if (*vr0type
== VR_RANGE
7756 && vr1type
== VR_ANTI_RANGE
)
7758 /* Choose the right gap if the left one is empty. */
7761 if (TREE_CODE (vr1max
) == INTEGER_CST
)
7762 *vr0min
= int_const_binop (PLUS_EXPR
, vr1max
,
7763 build_int_cst (TREE_TYPE (vr1max
), 1));
7767 /* Choose the left gap if the right one is empty. */
7770 if (TREE_CODE (vr1min
) == INTEGER_CST
)
7771 *vr0max
= int_const_binop (MINUS_EXPR
, vr1min
,
7772 build_int_cst (TREE_TYPE (vr1min
), 1));
7776 /* Choose the anti-range if the range is effectively varying. */
7777 else if (vrp_val_is_min (*vr0min
)
7778 && vrp_val_is_max (*vr0max
))
7784 /* Else choose the range. */
7786 else if (*vr0type
== VR_ANTI_RANGE
7787 && vr1type
== VR_ANTI_RANGE
)
7788 /* If both are anti-ranges the result is the outer one. */
7790 else if (*vr0type
== VR_ANTI_RANGE
7791 && vr1type
== VR_RANGE
)
7793 /* The intersection is empty. */
7794 *vr0type
= VR_UNDEFINED
;
7795 *vr0min
= NULL_TREE
;
7796 *vr0max
= NULL_TREE
;
7801 else if ((maxeq
|| operand_less_p (*vr0max
, vr1max
) == 1)
7802 && (mineq
|| operand_less_p (vr1min
, *vr0min
) == 1))
7804 /* ( [ ] ) or ([ ] ) or ( [ ]) */
7805 if (*vr0type
== VR_RANGE
7806 && vr1type
== VR_RANGE
)
7807 /* Choose the inner range. */
7809 else if (*vr0type
== VR_ANTI_RANGE
7810 && vr1type
== VR_RANGE
)
7812 /* Choose the right gap if the left is empty. */
7815 *vr0type
= VR_RANGE
;
7816 if (TREE_CODE (*vr0max
) == INTEGER_CST
)
7817 *vr0min
= int_const_binop (PLUS_EXPR
, *vr0max
,
7818 build_int_cst (TREE_TYPE (*vr0max
), 1));
7823 /* Choose the left gap if the right is empty. */
7826 *vr0type
= VR_RANGE
;
7827 if (TREE_CODE (*vr0min
) == INTEGER_CST
)
7828 *vr0max
= int_const_binop (MINUS_EXPR
, *vr0min
,
7829 build_int_cst (TREE_TYPE (*vr0min
), 1));
7834 /* Choose the anti-range if the range is effectively varying. */
7835 else if (vrp_val_is_min (vr1min
)
7836 && vrp_val_is_max (vr1max
))
7838 /* Else choose the range. */
7846 else if (*vr0type
== VR_ANTI_RANGE
7847 && vr1type
== VR_ANTI_RANGE
)
7849 /* If both are anti-ranges the result is the outer one. */
7854 else if (vr1type
== VR_ANTI_RANGE
7855 && *vr0type
== VR_RANGE
)
7857 /* The intersection is empty. */
7858 *vr0type
= VR_UNDEFINED
;
7859 *vr0min
= NULL_TREE
;
7860 *vr0max
= NULL_TREE
;
7865 else if ((operand_less_p (vr1min
, *vr0max
) == 1
7866 || operand_equal_p (vr1min
, *vr0max
, 0))
7867 && operand_less_p (*vr0min
, vr1min
) == 1)
7869 /* [ ( ] ) or [ ]( ) */
7870 if (*vr0type
== VR_ANTI_RANGE
7871 && vr1type
== VR_ANTI_RANGE
)
7873 else if (*vr0type
== VR_RANGE
7874 && vr1type
== VR_RANGE
)
7876 else if (*vr0type
== VR_RANGE
7877 && vr1type
== VR_ANTI_RANGE
)
7879 if (TREE_CODE (vr1min
) == INTEGER_CST
)
7880 *vr0max
= int_const_binop (MINUS_EXPR
, vr1min
,
7881 build_int_cst (TREE_TYPE (vr1min
), 1));
7885 else if (*vr0type
== VR_ANTI_RANGE
7886 && vr1type
== VR_RANGE
)
7888 *vr0type
= VR_RANGE
;
7889 if (TREE_CODE (*vr0max
) == INTEGER_CST
)
7890 *vr0min
= int_const_binop (PLUS_EXPR
, *vr0max
,
7891 build_int_cst (TREE_TYPE (*vr0max
), 1));
7899 else if ((operand_less_p (*vr0min
, vr1max
) == 1
7900 || operand_equal_p (*vr0min
, vr1max
, 0))
7901 && operand_less_p (vr1min
, *vr0min
) == 1)
7903 /* ( [ ) ] or ( )[ ] */
7904 if (*vr0type
== VR_ANTI_RANGE
7905 && vr1type
== VR_ANTI_RANGE
)
7907 else if (*vr0type
== VR_RANGE
7908 && vr1type
== VR_RANGE
)
7910 else if (*vr0type
== VR_RANGE
7911 && vr1type
== VR_ANTI_RANGE
)
7913 if (TREE_CODE (vr1max
) == INTEGER_CST
)
7914 *vr0min
= int_const_binop (PLUS_EXPR
, vr1max
,
7915 build_int_cst (TREE_TYPE (vr1max
), 1));
7919 else if (*vr0type
== VR_ANTI_RANGE
7920 && vr1type
== VR_RANGE
)
7922 *vr0type
= VR_RANGE
;
7923 if (TREE_CODE (*vr0min
) == INTEGER_CST
)
7924 *vr0max
= int_const_binop (MINUS_EXPR
, *vr0min
,
7925 build_int_cst (TREE_TYPE (*vr0min
), 1));
7934 /* As a fallback simply use { *VRTYPE, *VR0MIN, *VR0MAX } as
7935 result for the intersection. That's always a conservative
7936 correct estimate. */
7942 /* Intersect the two value-ranges *VR0 and *VR1 and store the result
7943 in *VR0. This may not be the smallest possible such range. */
7946 vrp_intersect_ranges_1 (value_range_t
*vr0
, value_range_t
*vr1
)
7948 value_range_t saved
;
7950 /* If either range is VR_VARYING the other one wins. */
7951 if (vr1
->type
== VR_VARYING
)
7953 if (vr0
->type
== VR_VARYING
)
7955 copy_value_range (vr0
, vr1
);
7959 /* When either range is VR_UNDEFINED the resulting range is
7960 VR_UNDEFINED, too. */
7961 if (vr0
->type
== VR_UNDEFINED
)
7963 if (vr1
->type
== VR_UNDEFINED
)
7965 set_value_range_to_undefined (vr0
);
7969 /* Save the original vr0 so we can return it as conservative intersection
7970 result when our worker turns things to varying. */
7972 intersect_ranges (&vr0
->type
, &vr0
->min
, &vr0
->max
,
7973 vr1
->type
, vr1
->min
, vr1
->max
);
7974 /* Make sure to canonicalize the result though as the inversion of a
7975 VR_RANGE can still be a VR_RANGE. */
7976 set_and_canonicalize_value_range (vr0
, vr0
->type
,
7977 vr0
->min
, vr0
->max
, vr0
->equiv
);
7978 /* If that failed, use the saved original VR0. */
7979 if (vr0
->type
== VR_VARYING
)
7984 /* If the result is VR_UNDEFINED there is no need to mess with
7985 the equivalencies. */
7986 if (vr0
->type
== VR_UNDEFINED
)
7989 /* The resulting set of equivalences for range intersection is the union of
7991 if (vr0
->equiv
&& vr1
->equiv
&& vr0
->equiv
!= vr1
->equiv
)
7992 bitmap_ior_into (vr0
->equiv
, vr1
->equiv
);
7993 else if (vr1
->equiv
&& !vr0
->equiv
)
7994 bitmap_copy (vr0
->equiv
, vr1
->equiv
);
7998 vrp_intersect_ranges (value_range_t
*vr0
, value_range_t
*vr1
)
8000 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8002 fprintf (dump_file
, "Intersecting\n ");
8003 dump_value_range (dump_file
, vr0
);
8004 fprintf (dump_file
, "\nand\n ");
8005 dump_value_range (dump_file
, vr1
);
8006 fprintf (dump_file
, "\n");
8008 vrp_intersect_ranges_1 (vr0
, vr1
);
8009 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8011 fprintf (dump_file
, "to\n ");
8012 dump_value_range (dump_file
, vr0
);
8013 fprintf (dump_file
, "\n");
8017 /* Meet operation for value ranges. Given two value ranges VR0 and
8018 VR1, store in VR0 a range that contains both VR0 and VR1. This
8019 may not be the smallest possible such range. */
8022 vrp_meet_1 (value_range_t
*vr0
, value_range_t
*vr1
)
8024 value_range_t saved
;
8026 if (vr0
->type
== VR_UNDEFINED
)
8028 set_value_range (vr0
, vr1
->type
, vr1
->min
, vr1
->max
, vr1
->equiv
);
8032 if (vr1
->type
== VR_UNDEFINED
)
8034 /* VR0 already has the resulting range. */
8038 if (vr0
->type
== VR_VARYING
)
8040 /* Nothing to do. VR0 already has the resulting range. */
8044 if (vr1
->type
== VR_VARYING
)
8046 set_value_range_to_varying (vr0
);
8051 union_ranges (&vr0
->type
, &vr0
->min
, &vr0
->max
,
8052 vr1
->type
, vr1
->min
, vr1
->max
);
8053 if (vr0
->type
== VR_VARYING
)
8055 /* Failed to find an efficient meet. Before giving up and setting
8056 the result to VARYING, see if we can at least derive a useful
8057 anti-range. FIXME, all this nonsense about distinguishing
8058 anti-ranges from ranges is necessary because of the odd
8059 semantics of range_includes_zero_p and friends. */
8060 if (((saved
.type
== VR_RANGE
8061 && range_includes_zero_p (saved
.min
, saved
.max
) == 0)
8062 || (saved
.type
== VR_ANTI_RANGE
8063 && range_includes_zero_p (saved
.min
, saved
.max
) == 1))
8064 && ((vr1
->type
== VR_RANGE
8065 && range_includes_zero_p (vr1
->min
, vr1
->max
) == 0)
8066 || (vr1
->type
== VR_ANTI_RANGE
8067 && range_includes_zero_p (vr1
->min
, vr1
->max
) == 1)))
8069 set_value_range_to_nonnull (vr0
, TREE_TYPE (saved
.min
));
8071 /* Since this meet operation did not result from the meeting of
8072 two equivalent names, VR0 cannot have any equivalences. */
8074 bitmap_clear (vr0
->equiv
);
8078 set_value_range_to_varying (vr0
);
8081 set_and_canonicalize_value_range (vr0
, vr0
->type
, vr0
->min
, vr0
->max
,
8083 if (vr0
->type
== VR_VARYING
)
8086 /* The resulting set of equivalences is always the intersection of
8088 if (vr0
->equiv
&& vr1
->equiv
&& vr0
->equiv
!= vr1
->equiv
)
8089 bitmap_and_into (vr0
->equiv
, vr1
->equiv
);
8090 else if (vr0
->equiv
&& !vr1
->equiv
)
8091 bitmap_clear (vr0
->equiv
);
8095 vrp_meet (value_range_t
*vr0
, value_range_t
*vr1
)
8097 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8099 fprintf (dump_file
, "Meeting\n ");
8100 dump_value_range (dump_file
, vr0
);
8101 fprintf (dump_file
, "\nand\n ");
8102 dump_value_range (dump_file
, vr1
);
8103 fprintf (dump_file
, "\n");
8105 vrp_meet_1 (vr0
, vr1
);
8106 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8108 fprintf (dump_file
, "to\n ");
8109 dump_value_range (dump_file
, vr0
);
8110 fprintf (dump_file
, "\n");
8115 /* Visit all arguments for PHI node PHI that flow through executable
8116 edges. If a valid value range can be derived from all the incoming
8117 value ranges, set a new range for the LHS of PHI. */
8119 static enum ssa_prop_result
8120 vrp_visit_phi_node (gimple phi
)
8123 tree lhs
= PHI_RESULT (phi
);
8124 value_range_t
*lhs_vr
= get_value_range (lhs
);
8125 value_range_t vr_result
= VR_INITIALIZER
;
8127 int edges
, old_edges
;
8130 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8132 fprintf (dump_file
, "\nVisiting PHI node: ");
8133 print_gimple_stmt (dump_file
, phi
, 0, dump_flags
);
8137 for (i
= 0; i
< gimple_phi_num_args (phi
); i
++)
8139 edge e
= gimple_phi_arg_edge (phi
, i
);
8141 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8144 "\n Argument #%d (%d -> %d %sexecutable)\n",
8145 (int) i
, e
->src
->index
, e
->dest
->index
,
8146 (e
->flags
& EDGE_EXECUTABLE
) ? "" : "not ");
8149 if (e
->flags
& EDGE_EXECUTABLE
)
8151 tree arg
= PHI_ARG_DEF (phi
, i
);
8152 value_range_t vr_arg
;
8156 if (TREE_CODE (arg
) == SSA_NAME
)
8158 vr_arg
= *(get_value_range (arg
));
8159 /* Do not allow equivalences or symbolic ranges to leak in from
8160 backedges. That creates invalid equivalencies.
8161 See PR53465 and PR54767. */
8162 if (e
->flags
& EDGE_DFS_BACK
8163 && (vr_arg
.type
== VR_RANGE
8164 || vr_arg
.type
== VR_ANTI_RANGE
))
8166 vr_arg
.equiv
= NULL
;
8167 if (symbolic_range_p (&vr_arg
))
8169 vr_arg
.type
= VR_VARYING
;
8170 vr_arg
.min
= NULL_TREE
;
8171 vr_arg
.max
= NULL_TREE
;
8177 if (is_overflow_infinity (arg
))
8179 arg
= copy_node (arg
);
8180 TREE_OVERFLOW (arg
) = 0;
8183 vr_arg
.type
= VR_RANGE
;
8186 vr_arg
.equiv
= NULL
;
8189 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8191 fprintf (dump_file
, "\t");
8192 print_generic_expr (dump_file
, arg
, dump_flags
);
8193 fprintf (dump_file
, "\n\tValue: ");
8194 dump_value_range (dump_file
, &vr_arg
);
8195 fprintf (dump_file
, "\n");
8199 copy_value_range (&vr_result
, &vr_arg
);
8201 vrp_meet (&vr_result
, &vr_arg
);
8204 if (vr_result
.type
== VR_VARYING
)
8209 if (vr_result
.type
== VR_VARYING
)
8211 else if (vr_result
.type
== VR_UNDEFINED
)
8214 old_edges
= vr_phi_edge_counts
[SSA_NAME_VERSION (lhs
)];
8215 vr_phi_edge_counts
[SSA_NAME_VERSION (lhs
)] = edges
;
8217 /* To prevent infinite iterations in the algorithm, derive ranges
8218 when the new value is slightly bigger or smaller than the
8219 previous one. We don't do this if we have seen a new executable
8220 edge; this helps us avoid an overflow infinity for conditionals
8221 which are not in a loop. If the old value-range was VR_UNDEFINED
8222 use the updated range and iterate one more time. */
8224 && gimple_phi_num_args (phi
) > 1
8225 && edges
== old_edges
8226 && lhs_vr
->type
!= VR_UNDEFINED
)
8228 int cmp_min
= compare_values (lhs_vr
->min
, vr_result
.min
);
8229 int cmp_max
= compare_values (lhs_vr
->max
, vr_result
.max
);
8231 /* For non VR_RANGE or for pointers fall back to varying if
8232 the range changed. */
8233 if ((lhs_vr
->type
!= VR_RANGE
|| vr_result
.type
!= VR_RANGE
8234 || POINTER_TYPE_P (TREE_TYPE (lhs
)))
8235 && (cmp_min
!= 0 || cmp_max
!= 0))
8238 /* If the new minimum is smaller or larger than the previous
8239 one, go all the way to -INF. In the first case, to avoid
8240 iterating millions of times to reach -INF, and in the
8241 other case to avoid infinite bouncing between different
8243 if (cmp_min
> 0 || cmp_min
< 0)
8245 if (!needs_overflow_infinity (TREE_TYPE (vr_result
.min
))
8246 || !vrp_var_may_overflow (lhs
, phi
))
8247 vr_result
.min
= TYPE_MIN_VALUE (TREE_TYPE (vr_result
.min
));
8248 else if (supports_overflow_infinity (TREE_TYPE (vr_result
.min
)))
8250 negative_overflow_infinity (TREE_TYPE (vr_result
.min
));
8253 /* Similarly, if the new maximum is smaller or larger than
8254 the previous one, go all the way to +INF. */
8255 if (cmp_max
< 0 || cmp_max
> 0)
8257 if (!needs_overflow_infinity (TREE_TYPE (vr_result
.max
))
8258 || !vrp_var_may_overflow (lhs
, phi
))
8259 vr_result
.max
= TYPE_MAX_VALUE (TREE_TYPE (vr_result
.max
));
8260 else if (supports_overflow_infinity (TREE_TYPE (vr_result
.max
)))
8262 positive_overflow_infinity (TREE_TYPE (vr_result
.max
));
8265 /* If we dropped either bound to +-INF then if this is a loop
8266 PHI node SCEV may known more about its value-range. */
8267 if ((cmp_min
> 0 || cmp_min
< 0
8268 || cmp_max
< 0 || cmp_max
> 0)
8270 && (l
= loop_containing_stmt (phi
))
8271 && l
->header
== gimple_bb (phi
))
8272 adjust_range_with_scev (&vr_result
, l
, phi
, lhs
);
8274 /* If we will end up with a (-INF, +INF) range, set it to
8275 VARYING. Same if the previous max value was invalid for
8276 the type and we end up with vr_result.min > vr_result.max. */
8277 if ((vrp_val_is_max (vr_result
.max
)
8278 && vrp_val_is_min (vr_result
.min
))
8279 || compare_values (vr_result
.min
,
8284 /* If the new range is different than the previous value, keep
8287 if (update_value_range (lhs
, &vr_result
))
8289 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8291 fprintf (dump_file
, "Found new range for ");
8292 print_generic_expr (dump_file
, lhs
, 0);
8293 fprintf (dump_file
, ": ");
8294 dump_value_range (dump_file
, &vr_result
);
8295 fprintf (dump_file
, "\n\n");
8298 return SSA_PROP_INTERESTING
;
8301 /* Nothing changed, don't add outgoing edges. */
8302 return SSA_PROP_NOT_INTERESTING
;
8304 /* No match found. Set the LHS to VARYING. */
8306 set_value_range_to_varying (lhs_vr
);
8307 return SSA_PROP_VARYING
;
8310 /* Simplify boolean operations if the source is known
8311 to be already a boolean. */
8313 simplify_truth_ops_using_ranges (gimple_stmt_iterator
*gsi
, gimple stmt
)
8315 enum tree_code rhs_code
= gimple_assign_rhs_code (stmt
);
8317 bool need_conversion
;
8319 /* We handle only !=/== case here. */
8320 gcc_assert (rhs_code
== EQ_EXPR
|| rhs_code
== NE_EXPR
);
8322 op0
= gimple_assign_rhs1 (stmt
);
8323 if (!op_with_boolean_value_range_p (op0
))
8326 op1
= gimple_assign_rhs2 (stmt
);
8327 if (!op_with_boolean_value_range_p (op1
))
8330 /* Reduce number of cases to handle to NE_EXPR. As there is no
8331 BIT_XNOR_EXPR we cannot replace A == B with a single statement. */
8332 if (rhs_code
== EQ_EXPR
)
8334 if (TREE_CODE (op1
) == INTEGER_CST
)
8335 op1
= int_const_binop (BIT_XOR_EXPR
, op1
,
8336 build_int_cst (TREE_TYPE (op1
), 1));
8341 lhs
= gimple_assign_lhs (stmt
);
8343 = !useless_type_conversion_p (TREE_TYPE (lhs
), TREE_TYPE (op0
));
8345 /* Make sure to not sign-extend a 1-bit 1 when converting the result. */
8347 && !TYPE_UNSIGNED (TREE_TYPE (op0
))
8348 && TYPE_PRECISION (TREE_TYPE (op0
)) == 1
8349 && TYPE_PRECISION (TREE_TYPE (lhs
)) > 1)
8352 /* For A != 0 we can substitute A itself. */
8353 if (integer_zerop (op1
))
8354 gimple_assign_set_rhs_with_ops (gsi
,
8356 ? NOP_EXPR
: TREE_CODE (op0
),
8358 /* For A != B we substitute A ^ B. Either with conversion. */
8359 else if (need_conversion
)
8361 tree tem
= make_ssa_name (TREE_TYPE (op0
), NULL
);
8362 gimple newop
= gimple_build_assign_with_ops (BIT_XOR_EXPR
, tem
, op0
, op1
);
8363 gsi_insert_before (gsi
, newop
, GSI_SAME_STMT
);
8364 gimple_assign_set_rhs_with_ops (gsi
, NOP_EXPR
, tem
, NULL_TREE
);
8368 gimple_assign_set_rhs_with_ops (gsi
, BIT_XOR_EXPR
, op0
, op1
);
8369 update_stmt (gsi_stmt (*gsi
));
8374 /* Simplify a division or modulo operator to a right shift or
8375 bitwise and if the first operand is unsigned or is greater
8376 than zero and the second operand is an exact power of two. */
8379 simplify_div_or_mod_using_ranges (gimple stmt
)
8381 enum tree_code rhs_code
= gimple_assign_rhs_code (stmt
);
8383 tree op0
= gimple_assign_rhs1 (stmt
);
8384 tree op1
= gimple_assign_rhs2 (stmt
);
8385 value_range_t
*vr
= get_value_range (gimple_assign_rhs1 (stmt
));
8387 if (TYPE_UNSIGNED (TREE_TYPE (op0
)))
8389 val
= integer_one_node
;
8395 val
= compare_range_with_value (GE_EXPR
, vr
, integer_zero_node
, &sop
);
8399 && integer_onep (val
)
8400 && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC
))
8402 location_t location
;
8404 if (!gimple_has_location (stmt
))
8405 location
= input_location
;
8407 location
= gimple_location (stmt
);
8408 warning_at (location
, OPT_Wstrict_overflow
,
8409 "assuming signed overflow does not occur when "
8410 "simplifying %</%> or %<%%%> to %<>>%> or %<&%>");
8414 if (val
&& integer_onep (val
))
8418 if (rhs_code
== TRUNC_DIV_EXPR
)
8420 t
= build_int_cst (integer_type_node
, tree_log2 (op1
));
8421 gimple_assign_set_rhs_code (stmt
, RSHIFT_EXPR
);
8422 gimple_assign_set_rhs1 (stmt
, op0
);
8423 gimple_assign_set_rhs2 (stmt
, t
);
8427 t
= build_int_cst (TREE_TYPE (op1
), 1);
8428 t
= int_const_binop (MINUS_EXPR
, op1
, t
);
8429 t
= fold_convert (TREE_TYPE (op0
), t
);
8431 gimple_assign_set_rhs_code (stmt
, BIT_AND_EXPR
);
8432 gimple_assign_set_rhs1 (stmt
, op0
);
8433 gimple_assign_set_rhs2 (stmt
, t
);
8443 /* If the operand to an ABS_EXPR is >= 0, then eliminate the
8444 ABS_EXPR. If the operand is <= 0, then simplify the
8445 ABS_EXPR into a NEGATE_EXPR. */
8448 simplify_abs_using_ranges (gimple stmt
)
8451 tree op
= gimple_assign_rhs1 (stmt
);
8452 tree type
= TREE_TYPE (op
);
8453 value_range_t
*vr
= get_value_range (op
);
8455 if (TYPE_UNSIGNED (type
))
8457 val
= integer_zero_node
;
8463 val
= compare_range_with_value (LE_EXPR
, vr
, integer_zero_node
, &sop
);
8467 val
= compare_range_with_value (GE_EXPR
, vr
, integer_zero_node
,
8472 if (integer_zerop (val
))
8473 val
= integer_one_node
;
8474 else if (integer_onep (val
))
8475 val
= integer_zero_node
;
8480 && (integer_onep (val
) || integer_zerop (val
)))
8482 if (sop
&& issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC
))
8484 location_t location
;
8486 if (!gimple_has_location (stmt
))
8487 location
= input_location
;
8489 location
= gimple_location (stmt
);
8490 warning_at (location
, OPT_Wstrict_overflow
,
8491 "assuming signed overflow does not occur when "
8492 "simplifying %<abs (X)%> to %<X%> or %<-X%>");
8495 gimple_assign_set_rhs1 (stmt
, op
);
8496 if (integer_onep (val
))
8497 gimple_assign_set_rhs_code (stmt
, NEGATE_EXPR
);
8499 gimple_assign_set_rhs_code (stmt
, SSA_NAME
);
8508 /* Optimize away redundant BIT_AND_EXPR and BIT_IOR_EXPR.
8509 If all the bits that are being cleared by & are already
8510 known to be zero from VR, or all the bits that are being
8511 set by | are already known to be one from VR, the bit
8512 operation is redundant. */
8515 simplify_bit_ops_using_ranges (gimple_stmt_iterator
*gsi
, gimple stmt
)
8517 tree op0
= gimple_assign_rhs1 (stmt
);
8518 tree op1
= gimple_assign_rhs2 (stmt
);
8519 tree op
= NULL_TREE
;
8520 value_range_t vr0
= VR_INITIALIZER
;
8521 value_range_t vr1
= VR_INITIALIZER
;
8522 wide_int may_be_nonzero0
, may_be_nonzero1
;
8523 wide_int must_be_nonzero0
, must_be_nonzero1
;
8526 if (TREE_CODE (op0
) == SSA_NAME
)
8527 vr0
= *(get_value_range (op0
));
8528 else if (is_gimple_min_invariant (op0
))
8529 set_value_range_to_value (&vr0
, op0
, NULL
);
8533 if (TREE_CODE (op1
) == SSA_NAME
)
8534 vr1
= *(get_value_range (op1
));
8535 else if (is_gimple_min_invariant (op1
))
8536 set_value_range_to_value (&vr1
, op1
, NULL
);
8540 if (!zero_nonzero_bits_from_vr (TREE_TYPE (op0
), &vr0
, &may_be_nonzero0
, &must_be_nonzero0
))
8542 if (!zero_nonzero_bits_from_vr (TREE_TYPE (op1
), &vr1
, &may_be_nonzero1
, &must_be_nonzero1
))
8545 switch (gimple_assign_rhs_code (stmt
))
8548 mask
= may_be_nonzero0
.and_not (must_be_nonzero1
);
8554 mask
= may_be_nonzero1
.and_not (must_be_nonzero0
);
8562 mask
= may_be_nonzero0
.and_not (must_be_nonzero1
);
8568 mask
= may_be_nonzero1
.and_not (must_be_nonzero0
);
8579 if (op
== NULL_TREE
)
8582 gimple_assign_set_rhs_with_ops (gsi
, TREE_CODE (op
), op
, NULL
);
8583 update_stmt (gsi_stmt (*gsi
));
8587 /* We are comparing trees OP0 and OP1 using COND_CODE. OP0 has
8588 a known value range VR.
8590 If there is one and only one value which will satisfy the
8591 conditional, then return that value. Else return NULL. */
8594 test_for_singularity (enum tree_code cond_code
, tree op0
,
8595 tree op1
, value_range_t
*vr
)
8600 /* Extract minimum/maximum values which satisfy the
8601 the conditional as it was written. */
8602 if (cond_code
== LE_EXPR
|| cond_code
== LT_EXPR
)
8604 /* This should not be negative infinity; there is no overflow
8606 min
= TYPE_MIN_VALUE (TREE_TYPE (op0
));
8609 if (cond_code
== LT_EXPR
&& !is_overflow_infinity (max
))
8611 tree one
= build_int_cst (TREE_TYPE (op0
), 1);
8612 max
= fold_build2 (MINUS_EXPR
, TREE_TYPE (op0
), max
, one
);
8614 TREE_NO_WARNING (max
) = 1;
8617 else if (cond_code
== GE_EXPR
|| cond_code
== GT_EXPR
)
8619 /* This should not be positive infinity; there is no overflow
8621 max
= TYPE_MAX_VALUE (TREE_TYPE (op0
));
8624 if (cond_code
== GT_EXPR
&& !is_overflow_infinity (min
))
8626 tree one
= build_int_cst (TREE_TYPE (op0
), 1);
8627 min
= fold_build2 (PLUS_EXPR
, TREE_TYPE (op0
), min
, one
);
8629 TREE_NO_WARNING (min
) = 1;
8633 /* Now refine the minimum and maximum values using any
8634 value range information we have for op0. */
8637 if (compare_values (vr
->min
, min
) == 1)
8639 if (compare_values (vr
->max
, max
) == -1)
8642 /* If the new min/max values have converged to a single value,
8643 then there is only one value which can satisfy the condition,
8644 return that value. */
8645 if (operand_equal_p (min
, max
, 0) && is_gimple_min_invariant (min
))
8651 /* Return whether the value range *VR fits in an integer type specified
8652 by PRECISION and UNSIGNED_P. */
8655 range_fits_type_p (value_range_t
*vr
, unsigned dest_precision
, signop dest_sgn
)
8658 unsigned src_precision
;
8662 /* We can only handle integral and pointer types. */
8663 src_type
= TREE_TYPE (vr
->min
);
8664 if (!INTEGRAL_TYPE_P (src_type
)
8665 && !POINTER_TYPE_P (src_type
))
8668 /* An extension is fine unless VR is SIGNED and dest_sgn is UNSIGNED,
8669 and so is an identity transform. */
8670 src_precision
= TYPE_PRECISION (TREE_TYPE (vr
->min
));
8671 src_sgn
= TYPE_SIGN (src_type
);
8672 if ((src_precision
< dest_precision
8673 && !(dest_sgn
== UNSIGNED
&& src_sgn
== SIGNED
))
8674 || (src_precision
== dest_precision
&& src_sgn
== dest_sgn
))
8677 /* Now we can only handle ranges with constant bounds. */
8678 if (vr
->type
!= VR_RANGE
8679 || TREE_CODE (vr
->min
) != INTEGER_CST
8680 || TREE_CODE (vr
->max
) != INTEGER_CST
)
8683 /* For sign changes, the MSB of the wide_int has to be clear.
8684 An unsigned value with its MSB set cannot be represented by
8685 a signed wide_int, while a negative value cannot be represented
8686 by an unsigned wide_int. */
8687 if (src_sgn
!= dest_sgn
8688 && (max_wide_int (vr
->min
).lts_p (0) || max_wide_int (vr
->max
).lts_p (0)))
8691 /* Then we can perform the conversion on both ends and compare
8692 the result for equality. */
8693 tem
= max_wide_int (vr
->min
).ext (dest_precision
, dest_sgn
);
8694 if (max_wide_int (vr
->min
) != tem
)
8696 tem
= max_wide_int (vr
->max
).ext (dest_precision
, dest_sgn
);
8697 if (max_wide_int (vr
->max
) != tem
)
8703 /* Simplify a conditional using a relational operator to an equality
8704 test if the range information indicates only one value can satisfy
8705 the original conditional. */
8708 simplify_cond_using_ranges (gimple stmt
)
8710 tree op0
= gimple_cond_lhs (stmt
);
8711 tree op1
= gimple_cond_rhs (stmt
);
8712 enum tree_code cond_code
= gimple_cond_code (stmt
);
8714 if (cond_code
!= NE_EXPR
8715 && cond_code
!= EQ_EXPR
8716 && TREE_CODE (op0
) == SSA_NAME
8717 && INTEGRAL_TYPE_P (TREE_TYPE (op0
))
8718 && is_gimple_min_invariant (op1
))
8720 value_range_t
*vr
= get_value_range (op0
);
8722 /* If we have range information for OP0, then we might be
8723 able to simplify this conditional. */
8724 if (vr
->type
== VR_RANGE
)
8726 tree new_tree
= test_for_singularity (cond_code
, op0
, op1
, vr
);
8732 fprintf (dump_file
, "Simplified relational ");
8733 print_gimple_stmt (dump_file
, stmt
, 0, 0);
8734 fprintf (dump_file
, " into ");
8737 gimple_cond_set_code (stmt
, EQ_EXPR
);
8738 gimple_cond_set_lhs (stmt
, op0
);
8739 gimple_cond_set_rhs (stmt
, new_tree
);
8745 print_gimple_stmt (dump_file
, stmt
, 0, 0);
8746 fprintf (dump_file
, "\n");
8752 /* Try again after inverting the condition. We only deal
8753 with integral types here, so no need to worry about
8754 issues with inverting FP comparisons. */
8755 cond_code
= invert_tree_comparison (cond_code
, false);
8756 new_tree
= test_for_singularity (cond_code
, op0
, op1
, vr
);
8762 fprintf (dump_file
, "Simplified relational ");
8763 print_gimple_stmt (dump_file
, stmt
, 0, 0);
8764 fprintf (dump_file
, " into ");
8767 gimple_cond_set_code (stmt
, NE_EXPR
);
8768 gimple_cond_set_lhs (stmt
, op0
);
8769 gimple_cond_set_rhs (stmt
, new_tree
);
8775 print_gimple_stmt (dump_file
, stmt
, 0, 0);
8776 fprintf (dump_file
, "\n");
8784 /* If we have a comparison of an SSA_NAME (OP0) against a constant,
8785 see if OP0 was set by a type conversion where the source of
8786 the conversion is another SSA_NAME with a range that fits
8787 into the range of OP0's type.
8789 If so, the conversion is redundant as the earlier SSA_NAME can be
8790 used for the comparison directly if we just massage the constant in the
8792 if (TREE_CODE (op0
) == SSA_NAME
8793 && TREE_CODE (op1
) == INTEGER_CST
)
8795 gimple def_stmt
= SSA_NAME_DEF_STMT (op0
);
8798 if (!is_gimple_assign (def_stmt
)
8799 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt
)))
8802 innerop
= gimple_assign_rhs1 (def_stmt
);
8804 if (TREE_CODE (innerop
) == SSA_NAME
8805 && !POINTER_TYPE_P (TREE_TYPE (innerop
)))
8807 value_range_t
*vr
= get_value_range (innerop
);
8809 if (range_int_cst_p (vr
)
8810 && range_fits_type_p (vr
,
8811 TYPE_PRECISION (TREE_TYPE (op0
)),
8812 TYPE_SIGN (TREE_TYPE (op0
)))
8813 && int_fits_type_p (op1
, TREE_TYPE (innerop
))
8814 /* The range must not have overflowed, or if it did overflow
8815 we must not be wrapping/trapping overflow and optimizing
8816 with strict overflow semantics. */
8817 && ((!is_negative_overflow_infinity (vr
->min
)
8818 && !is_positive_overflow_infinity (vr
->max
))
8819 || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (innerop
))))
8821 /* If the range overflowed and the user has asked for warnings
8822 when strict overflow semantics were used to optimize code,
8823 issue an appropriate warning. */
8824 if ((is_negative_overflow_infinity (vr
->min
)
8825 || is_positive_overflow_infinity (vr
->max
))
8826 && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_CONDITIONAL
))
8828 location_t location
;
8830 if (!gimple_has_location (stmt
))
8831 location
= input_location
;
8833 location
= gimple_location (stmt
);
8834 warning_at (location
, OPT_Wstrict_overflow
,
8835 "assuming signed overflow does not occur when "
8836 "simplifying conditional");
8839 tree newconst
= fold_convert (TREE_TYPE (innerop
), op1
);
8840 gimple_cond_set_lhs (stmt
, innerop
);
8841 gimple_cond_set_rhs (stmt
, newconst
);
8850 /* Simplify a switch statement using the value range of the switch
8854 simplify_switch_using_ranges (gimple stmt
)
8856 tree op
= gimple_switch_index (stmt
);
8861 size_t i
= 0, j
= 0, n
, n2
;
8864 size_t k
= 1, l
= 0;
8866 if (TREE_CODE (op
) == SSA_NAME
)
8868 vr
= get_value_range (op
);
8870 /* We can only handle integer ranges. */
8871 if ((vr
->type
!= VR_RANGE
8872 && vr
->type
!= VR_ANTI_RANGE
)
8873 || symbolic_range_p (vr
))
8876 /* Find case label for min/max of the value range. */
8877 take_default
= !find_case_label_ranges (stmt
, vr
, &i
, &j
, &k
, &l
);
8879 else if (TREE_CODE (op
) == INTEGER_CST
)
8881 take_default
= !find_case_label_index (stmt
, 1, op
, &i
);
8895 n
= gimple_switch_num_labels (stmt
);
8897 /* Bail out if this is just all edges taken. */
8903 /* Build a new vector of taken case labels. */
8904 vec2
= make_tree_vec (j
- i
+ 1 + l
- k
+ 1 + (int)take_default
);
8907 /* Add the default edge, if necessary. */
8909 TREE_VEC_ELT (vec2
, n2
++) = gimple_switch_default_label (stmt
);
8911 for (; i
<= j
; ++i
, ++n2
)
8912 TREE_VEC_ELT (vec2
, n2
) = gimple_switch_label (stmt
, i
);
8914 for (; k
<= l
; ++k
, ++n2
)
8915 TREE_VEC_ELT (vec2
, n2
) = gimple_switch_label (stmt
, k
);
8917 /* Mark needed edges. */
8918 for (i
= 0; i
< n2
; ++i
)
8920 e
= find_edge (gimple_bb (stmt
),
8921 label_to_block (CASE_LABEL (TREE_VEC_ELT (vec2
, i
))));
8922 e
->aux
= (void *)-1;
8925 /* Queue not needed edges for later removal. */
8926 FOR_EACH_EDGE (e
, ei
, gimple_bb (stmt
)->succs
)
8928 if (e
->aux
== (void *)-1)
8934 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8936 fprintf (dump_file
, "removing unreachable case label\n");
8938 to_remove_edges
.safe_push (e
);
8939 e
->flags
&= ~EDGE_EXECUTABLE
;
8942 /* And queue an update for the stmt. */
8945 to_update_switch_stmts
.safe_push (su
);
8949 /* Simplify an integral conversion from an SSA name in STMT. */
8952 simplify_conversion_using_ranges (gimple stmt
)
8954 tree innerop
, middleop
, finaltype
;
8956 value_range_t
*innervr
;
8957 signop inner_sgn
, middle_sgn
, final_sgn
;
8958 unsigned inner_prec
, middle_prec
, final_prec
;
8959 max_wide_int innermin
, innermed
, innermax
, middlemin
, middlemed
, middlemax
;
8961 finaltype
= TREE_TYPE (gimple_assign_lhs (stmt
));
8962 if (!INTEGRAL_TYPE_P (finaltype
))
8964 middleop
= gimple_assign_rhs1 (stmt
);
8965 def_stmt
= SSA_NAME_DEF_STMT (middleop
);
8966 if (!is_gimple_assign (def_stmt
)
8967 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt
)))
8969 innerop
= gimple_assign_rhs1 (def_stmt
);
8970 if (TREE_CODE (innerop
) != SSA_NAME
8971 || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (innerop
))
8974 /* Get the value-range of the inner operand. */
8975 innervr
= get_value_range (innerop
);
8976 if (innervr
->type
!= VR_RANGE
8977 || TREE_CODE (innervr
->min
) != INTEGER_CST
8978 || TREE_CODE (innervr
->max
) != INTEGER_CST
)
8981 /* Simulate the conversion chain to check if the result is equal if
8982 the middle conversion is removed. */
8983 innermin
= innervr
->min
;
8984 innermax
= innervr
->max
;
8986 inner_prec
= TYPE_PRECISION (TREE_TYPE (innerop
));
8987 middle_prec
= TYPE_PRECISION (TREE_TYPE (middleop
));
8988 final_prec
= TYPE_PRECISION (finaltype
);
8990 /* If the first conversion is not injective, the second must not
8992 if ((innermax
- innermin
).gtu_p (max_wide_int::mask (middle_prec
, false))
8993 && middle_prec
< final_prec
)
8995 /* We also want a medium value so that we can track the effect that
8996 narrowing conversions with sign change have. */
8997 inner_sgn
= TYPE_SIGN (TREE_TYPE (innerop
));
8998 if (inner_sgn
== UNSIGNED
)
8999 innermed
= max_wide_int::shifted_mask (1, inner_prec
- 1, false);
9002 if (innermin
.cmp (innermed
, inner_sgn
) >= 0
9003 || innermed
.cmp (innermax
, inner_sgn
) >= 0)
9004 innermed
= innermin
;
9006 middle_sgn
= TYPE_SIGN (TREE_TYPE (middleop
));
9007 middlemin
= innermin
.ext (middle_prec
, middle_sgn
);
9008 middlemed
= innermed
.ext (middle_prec
, middle_sgn
);
9009 middlemax
= innermax
.ext (middle_prec
, middle_sgn
);
9011 /* Require that the final conversion applied to both the original
9012 and the intermediate range produces the same result. */
9013 final_sgn
= TYPE_SIGN (finaltype
);
9014 if (middlemin
.ext (final_prec
, final_sgn
)
9015 != innermin
.ext (final_prec
, final_sgn
)
9016 || middlemed
.ext (final_prec
, final_sgn
)
9017 != innermed
.ext (final_prec
, final_sgn
)
9018 || middlemax
.ext (final_prec
, final_sgn
)
9019 != innermax
.ext (final_prec
, final_sgn
))
9022 gimple_assign_set_rhs1 (stmt
, innerop
);
9027 /* Simplify a conversion from integral SSA name to float in STMT. */
9030 simplify_float_conversion_using_ranges (gimple_stmt_iterator
*gsi
, gimple stmt
)
9032 tree rhs1
= gimple_assign_rhs1 (stmt
);
9033 value_range_t
*vr
= get_value_range (rhs1
);
9034 enum machine_mode fltmode
= TYPE_MODE (TREE_TYPE (gimple_assign_lhs (stmt
)));
9035 enum machine_mode mode
;
9039 /* We can only handle constant ranges. */
9040 if (vr
->type
!= VR_RANGE
9041 || TREE_CODE (vr
->min
) != INTEGER_CST
9042 || TREE_CODE (vr
->max
) != INTEGER_CST
)
9045 /* First check if we can use a signed type in place of an unsigned. */
9046 if (TYPE_UNSIGNED (TREE_TYPE (rhs1
))
9047 && (can_float_p (fltmode
, TYPE_MODE (TREE_TYPE (rhs1
)), 0)
9048 != CODE_FOR_nothing
)
9049 && range_fits_type_p (vr
, TYPE_PRECISION (TREE_TYPE (rhs1
)), SIGNED
))
9050 mode
= TYPE_MODE (TREE_TYPE (rhs1
));
9051 /* If we can do the conversion in the current input mode do nothing. */
9052 else if (can_float_p (fltmode
, TYPE_MODE (TREE_TYPE (rhs1
)),
9053 TYPE_UNSIGNED (TREE_TYPE (rhs1
))) != CODE_FOR_nothing
)
9055 /* Otherwise search for a mode we can use, starting from the narrowest
9056 integer mode available. */
9059 mode
= GET_CLASS_NARROWEST_MODE (MODE_INT
);
9062 /* If we cannot do a signed conversion to float from mode
9063 or if the value-range does not fit in the signed type
9064 try with a wider mode. */
9065 if (can_float_p (fltmode
, mode
, 0) != CODE_FOR_nothing
9066 && range_fits_type_p (vr
, GET_MODE_PRECISION (mode
), SIGNED
))
9069 mode
= GET_MODE_WIDER_MODE (mode
);
9070 /* But do not widen the input. Instead leave that to the
9071 optabs expansion code. */
9072 if (GET_MODE_PRECISION (mode
) > TYPE_PRECISION (TREE_TYPE (rhs1
)))
9075 while (mode
!= VOIDmode
);
9076 if (mode
== VOIDmode
)
9080 /* It works, insert a truncation or sign-change before the
9081 float conversion. */
9082 tem
= make_ssa_name (build_nonstandard_integer_type
9083 (GET_MODE_PRECISION (mode
), 0), NULL
);
9084 conv
= gimple_build_assign_with_ops (NOP_EXPR
, tem
, rhs1
, NULL_TREE
);
9085 gsi_insert_before (gsi
, conv
, GSI_SAME_STMT
);
9086 gimple_assign_set_rhs1 (stmt
, tem
);
9092 /* Simplify STMT using ranges if possible. */
9095 simplify_stmt_using_ranges (gimple_stmt_iterator
*gsi
)
9097 gimple stmt
= gsi_stmt (*gsi
);
9099 if (is_gimple_assign (stmt
))
9101 enum tree_code rhs_code
= gimple_assign_rhs_code (stmt
);
9102 tree rhs1
= gimple_assign_rhs1 (stmt
);
9108 /* Transform EQ_EXPR, NE_EXPR into BIT_XOR_EXPR or identity
9109 if the RHS is zero or one, and the LHS are known to be boolean
9111 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
9112 return simplify_truth_ops_using_ranges (gsi
, stmt
);
9115 /* Transform TRUNC_DIV_EXPR and TRUNC_MOD_EXPR into RSHIFT_EXPR
9116 and BIT_AND_EXPR respectively if the first operand is greater
9117 than zero and the second operand is an exact power of two. */
9118 case TRUNC_DIV_EXPR
:
9119 case TRUNC_MOD_EXPR
:
9120 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1
))
9121 && integer_pow2p (gimple_assign_rhs2 (stmt
)))
9122 return simplify_div_or_mod_using_ranges (stmt
);
9125 /* Transform ABS (X) into X or -X as appropriate. */
9127 if (TREE_CODE (rhs1
) == SSA_NAME
9128 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
9129 return simplify_abs_using_ranges (stmt
);
9134 /* Optimize away BIT_AND_EXPR and BIT_IOR_EXPR
9135 if all the bits being cleared are already cleared or
9136 all the bits being set are already set. */
9137 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
9138 return simplify_bit_ops_using_ranges (gsi
, stmt
);
9142 if (TREE_CODE (rhs1
) == SSA_NAME
9143 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
9144 return simplify_conversion_using_ranges (stmt
);
9148 if (TREE_CODE (rhs1
) == SSA_NAME
9149 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
9150 return simplify_float_conversion_using_ranges (gsi
, stmt
);
9157 else if (gimple_code (stmt
) == GIMPLE_COND
)
9158 return simplify_cond_using_ranges (stmt
);
9159 else if (gimple_code (stmt
) == GIMPLE_SWITCH
)
9160 return simplify_switch_using_ranges (stmt
);
9165 /* If the statement pointed by SI has a predicate whose value can be
9166 computed using the value range information computed by VRP, compute
9167 its value and return true. Otherwise, return false. */
9170 fold_predicate_in (gimple_stmt_iterator
*si
)
9172 bool assignment_p
= false;
9174 gimple stmt
= gsi_stmt (*si
);
9176 if (is_gimple_assign (stmt
)
9177 && TREE_CODE_CLASS (gimple_assign_rhs_code (stmt
)) == tcc_comparison
)
9179 assignment_p
= true;
9180 val
= vrp_evaluate_conditional (gimple_assign_rhs_code (stmt
),
9181 gimple_assign_rhs1 (stmt
),
9182 gimple_assign_rhs2 (stmt
),
9185 else if (gimple_code (stmt
) == GIMPLE_COND
)
9186 val
= vrp_evaluate_conditional (gimple_cond_code (stmt
),
9187 gimple_cond_lhs (stmt
),
9188 gimple_cond_rhs (stmt
),
9196 val
= fold_convert (gimple_expr_type (stmt
), val
);
9200 fprintf (dump_file
, "Folding predicate ");
9201 print_gimple_expr (dump_file
, stmt
, 0, 0);
9202 fprintf (dump_file
, " to ");
9203 print_generic_expr (dump_file
, val
, 0);
9204 fprintf (dump_file
, "\n");
9207 if (is_gimple_assign (stmt
))
9208 gimple_assign_set_rhs_from_tree (si
, val
);
9211 gcc_assert (gimple_code (stmt
) == GIMPLE_COND
);
9212 if (integer_zerop (val
))
9213 gimple_cond_make_false (stmt
);
9214 else if (integer_onep (val
))
9215 gimple_cond_make_true (stmt
);
9226 /* Callback for substitute_and_fold folding the stmt at *SI. */
9229 vrp_fold_stmt (gimple_stmt_iterator
*si
)
9231 if (fold_predicate_in (si
))
9234 return simplify_stmt_using_ranges (si
);
9237 /* Stack of dest,src equivalency pairs that need to be restored after
9238 each attempt to thread a block's incoming edge to an outgoing edge.
9240 A NULL entry is used to mark the end of pairs which need to be
9242 static vec
<tree
> equiv_stack
;
9244 /* A trivial wrapper so that we can present the generic jump threading
9245 code with a simple API for simplifying statements. STMT is the
9246 statement we want to simplify, WITHIN_STMT provides the location
9247 for any overflow warnings. */
9250 simplify_stmt_for_jump_threading (gimple stmt
, gimple within_stmt
)
9252 if (gimple_code (stmt
) == GIMPLE_COND
)
9253 return vrp_evaluate_conditional (gimple_cond_code (stmt
),
9254 gimple_cond_lhs (stmt
),
9255 gimple_cond_rhs (stmt
), within_stmt
);
9257 if (gimple_code (stmt
) == GIMPLE_ASSIGN
)
9259 value_range_t new_vr
= VR_INITIALIZER
;
9260 tree lhs
= gimple_assign_lhs (stmt
);
9262 if (TREE_CODE (lhs
) == SSA_NAME
9263 && (INTEGRAL_TYPE_P (TREE_TYPE (lhs
))
9264 || POINTER_TYPE_P (TREE_TYPE (lhs
))))
9266 extract_range_from_assignment (&new_vr
, stmt
);
9267 if (range_int_cst_singleton_p (&new_vr
))
9275 /* Blocks which have more than one predecessor and more than
9276 one successor present jump threading opportunities, i.e.,
9277 when the block is reached from a specific predecessor, we
9278 may be able to determine which of the outgoing edges will
9279 be traversed. When this optimization applies, we are able
9280 to avoid conditionals at runtime and we may expose secondary
9281 optimization opportunities.
9283 This routine is effectively a driver for the generic jump
9284 threading code. It basically just presents the generic code
9285 with edges that may be suitable for jump threading.
9287 Unlike DOM, we do not iterate VRP if jump threading was successful.
9288 While iterating may expose new opportunities for VRP, it is expected
9289 those opportunities would be very limited and the compile time cost
9290 to expose those opportunities would be significant.
9292 As jump threading opportunities are discovered, they are registered
9293 for later realization. */
9296 identify_jump_threads (void)
9303 /* Ugh. When substituting values earlier in this pass we can
9304 wipe the dominance information. So rebuild the dominator
9305 information as we need it within the jump threading code. */
9306 calculate_dominance_info (CDI_DOMINATORS
);
9308 /* We do not allow VRP information to be used for jump threading
9309 across a back edge in the CFG. Otherwise it becomes too
9310 difficult to avoid eliminating loop exit tests. Of course
9311 EDGE_DFS_BACK is not accurate at this time so we have to
9313 mark_dfs_back_edges ();
9315 /* Do not thread across edges we are about to remove. Just marking
9316 them as EDGE_DFS_BACK will do. */
9317 FOR_EACH_VEC_ELT (to_remove_edges
, i
, e
)
9318 e
->flags
|= EDGE_DFS_BACK
;
9320 /* Allocate our unwinder stack to unwind any temporary equivalences
9321 that might be recorded. */
9322 equiv_stack
.create (20);
9324 /* To avoid lots of silly node creation, we create a single
9325 conditional and just modify it in-place when attempting to
9327 dummy
= gimple_build_cond (EQ_EXPR
,
9328 integer_zero_node
, integer_zero_node
,
9331 /* Walk through all the blocks finding those which present a
9332 potential jump threading opportunity. We could set this up
9333 as a dominator walker and record data during the walk, but
9334 I doubt it's worth the effort for the classes of jump
9335 threading opportunities we are trying to identify at this
9336 point in compilation. */
9341 /* If the generic jump threading code does not find this block
9342 interesting, then there is nothing to do. */
9343 if (! potentially_threadable_block (bb
))
9346 /* We only care about blocks ending in a COND_EXPR. While there
9347 may be some value in handling SWITCH_EXPR here, I doubt it's
9348 terribly important. */
9349 last
= gsi_stmt (gsi_last_bb (bb
));
9351 /* We're basically looking for a switch or any kind of conditional with
9352 integral or pointer type arguments. Note the type of the second
9353 argument will be the same as the first argument, so no need to
9354 check it explicitly. */
9355 if (gimple_code (last
) == GIMPLE_SWITCH
9356 || (gimple_code (last
) == GIMPLE_COND
9357 && TREE_CODE (gimple_cond_lhs (last
)) == SSA_NAME
9358 && (INTEGRAL_TYPE_P (TREE_TYPE (gimple_cond_lhs (last
)))
9359 || POINTER_TYPE_P (TREE_TYPE (gimple_cond_lhs (last
))))
9360 && (TREE_CODE (gimple_cond_rhs (last
)) == SSA_NAME
9361 || is_gimple_min_invariant (gimple_cond_rhs (last
)))))
9365 /* We've got a block with multiple predecessors and multiple
9366 successors which also ends in a suitable conditional or
9367 switch statement. For each predecessor, see if we can thread
9368 it to a specific successor. */
9369 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
9371 /* Do not thread across back edges or abnormal edges
9373 if (e
->flags
& (EDGE_DFS_BACK
| EDGE_COMPLEX
))
9376 thread_across_edge (dummy
, e
, true, &equiv_stack
,
9377 simplify_stmt_for_jump_threading
);
9382 /* We do not actually update the CFG or SSA graphs at this point as
9383 ASSERT_EXPRs are still in the IL and cfg cleanup code does not yet
9384 handle ASSERT_EXPRs gracefully. */
9387 /* We identified all the jump threading opportunities earlier, but could
9388 not transform the CFG at that time. This routine transforms the
9389 CFG and arranges for the dominator tree to be rebuilt if necessary.
9391 Note the SSA graph update will occur during the normal TODO
9392 processing by the pass manager. */
9394 finalize_jump_threads (void)
9396 thread_through_all_blocks (false);
9397 equiv_stack
.release ();
9401 /* Traverse all the blocks folding conditionals with known ranges. */
9408 values_propagated
= true;
9412 fprintf (dump_file
, "\nValue ranges after VRP:\n\n");
9413 dump_all_value_ranges (dump_file
);
9414 fprintf (dump_file
, "\n");
9417 substitute_and_fold (op_with_constant_singleton_value_range
,
9418 vrp_fold_stmt
, false);
9420 if (warn_array_bounds
)
9421 check_all_array_refs ();
9423 /* We must identify jump threading opportunities before we release
9424 the datastructures built by VRP. */
9425 identify_jump_threads ();
9427 /* Free allocated memory. */
9428 for (i
= 0; i
< num_vr_values
; i
++)
9431 BITMAP_FREE (vr_value
[i
]->equiv
);
9436 free (vr_phi_edge_counts
);
9438 /* So that we can distinguish between VRP data being available
9439 and not available. */
9441 vr_phi_edge_counts
= NULL
;
9445 /* Main entry point to VRP (Value Range Propagation). This pass is
9446 loosely based on J. R. C. Patterson, ``Accurate Static Branch
9447 Prediction by Value Range Propagation,'' in SIGPLAN Conference on
9448 Programming Language Design and Implementation, pp. 67-78, 1995.
9449 Also available at http://citeseer.ist.psu.edu/patterson95accurate.html
9451 This is essentially an SSA-CCP pass modified to deal with ranges
9452 instead of constants.
9454 While propagating ranges, we may find that two or more SSA name
9455 have equivalent, though distinct ranges. For instance,
9458 2 p_4 = ASSERT_EXPR <p_3, p_3 != 0>
9460 4 p_5 = ASSERT_EXPR <p_4, p_4 == q_2>;
9464 In the code above, pointer p_5 has range [q_2, q_2], but from the
9465 code we can also determine that p_5 cannot be NULL and, if q_2 had
9466 a non-varying range, p_5's range should also be compatible with it.
9468 These equivalences are created by two expressions: ASSERT_EXPR and
9469 copy operations. Since p_5 is an assertion on p_4, and p_4 was the
9470 result of another assertion, then we can use the fact that p_5 and
9471 p_4 are equivalent when evaluating p_5's range.
9473 Together with value ranges, we also propagate these equivalences
9474 between names so that we can take advantage of information from
9475 multiple ranges when doing final replacement. Note that this
9476 equivalency relation is transitive but not symmetric.
9478 In the example above, p_5 is equivalent to p_4, q_2 and p_3, but we
9479 cannot assert that q_2 is equivalent to p_5 because q_2 may be used
9480 in contexts where that assertion does not hold (e.g., in line 6).
9482 TODO, the main difference between this pass and Patterson's is that
9483 we do not propagate edge probabilities. We only compute whether
9484 edges can be taken or not. That is, instead of having a spectrum
9485 of jump probabilities between 0 and 1, we only deal with 0, 1 and
9486 DON'T KNOW. In the future, it may be worthwhile to propagate
9487 probabilities to aid branch prediction. */
9496 loop_optimizer_init (LOOPS_NORMAL
| LOOPS_HAVE_RECORDED_EXITS
);
9497 rewrite_into_loop_closed_ssa (NULL
, TODO_update_ssa
);
9500 /* ??? This ends up using stale EDGE_DFS_BACK for liveness computation.
9501 Inserting assertions may split edges which will invalidate
9503 insert_range_assertions ();
9505 to_remove_edges
.create (10);
9506 to_update_switch_stmts
.create (5);
9507 threadedge_initialize_values ();
9509 /* For visiting PHI nodes we need EDGE_DFS_BACK computed. */
9510 mark_dfs_back_edges ();
9513 ssa_propagate (vrp_visit_stmt
, vrp_visit_phi_node
);
9516 free_numbers_of_iterations_estimates ();
9518 /* ASSERT_EXPRs must be removed before finalizing jump threads
9519 as finalizing jump threads calls the CFG cleanup code which
9520 does not properly handle ASSERT_EXPRs. */
9521 remove_range_assertions ();
9523 /* If we exposed any new variables, go ahead and put them into
9524 SSA form now, before we handle jump threading. This simplifies
9525 interactions between rewriting of _DECL nodes into SSA form
9526 and rewriting SSA_NAME nodes into SSA form after block
9527 duplication and CFG manipulation. */
9528 update_ssa (TODO_update_ssa
);
9530 finalize_jump_threads ();
9532 /* Remove dead edges from SWITCH_EXPR optimization. This leaves the
9533 CFG in a broken state and requires a cfg_cleanup run. */
9534 FOR_EACH_VEC_ELT (to_remove_edges
, i
, e
)
9536 /* Update SWITCH_EXPR case label vector. */
9537 FOR_EACH_VEC_ELT (to_update_switch_stmts
, i
, su
)
9540 size_t n
= TREE_VEC_LENGTH (su
->vec
);
9542 gimple_switch_set_num_labels (su
->stmt
, n
);
9543 for (j
= 0; j
< n
; j
++)
9544 gimple_switch_set_label (su
->stmt
, j
, TREE_VEC_ELT (su
->vec
, j
));
9545 /* As we may have replaced the default label with a regular one
9546 make sure to make it a real default label again. This ensures
9547 optimal expansion. */
9548 label
= gimple_switch_label (su
->stmt
, 0);
9549 CASE_LOW (label
) = NULL_TREE
;
9550 CASE_HIGH (label
) = NULL_TREE
;
9553 if (to_remove_edges
.length () > 0)
9555 free_dominance_info (CDI_DOMINATORS
);
9557 loops_state_set (LOOPS_NEED_FIXUP
);
9560 to_remove_edges
.release ();
9561 to_update_switch_stmts
.release ();
9562 threadedge_finalize_values ();
9565 loop_optimizer_finalize ();
9572 return flag_tree_vrp
!= 0;
9577 const pass_data pass_data_vrp
=
9579 GIMPLE_PASS
, /* type */
9581 OPTGROUP_NONE
, /* optinfo_flags */
9582 true, /* has_gate */
9583 true, /* has_execute */
9584 TV_TREE_VRP
, /* tv_id */
9585 PROP_ssa
, /* properties_required */
9586 0, /* properties_provided */
9587 0, /* properties_destroyed */
9588 0, /* todo_flags_start */
9589 ( TODO_cleanup_cfg
| TODO_update_ssa
9591 | TODO_verify_flow
), /* todo_flags_finish */
9594 class pass_vrp
: public gimple_opt_pass
9597 pass_vrp(gcc::context
*ctxt
)
9598 : gimple_opt_pass(pass_data_vrp
, ctxt
)
9601 /* opt_pass methods: */
9602 opt_pass
* clone () { return new pass_vrp (ctxt_
); }
9603 bool gate () { return gate_vrp (); }
9604 unsigned int execute () { return execute_vrp (); }
9606 }; // class pass_vrp
9611 make_pass_vrp (gcc::context
*ctxt
)
9613 return new pass_vrp (ctxt
);