1 /* Support routines for Value Range Propagation (VRP).
2 Copyright (C) 2005-2013 Free Software Foundation, Inc.
3 Contributed by Diego Novillo <dnovillo@redhat.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
23 #include "coretypes.h"
28 #include "basic-block.h"
30 #include "gimple-ssa.h"
32 #include "tree-phinodes.h"
33 #include "ssa-iterators.h"
34 #include "tree-ssanames.h"
35 #include "tree-ssa-loop-manip.h"
36 #include "tree-ssa-loop-niter.h"
37 #include "tree-ssa-loop.h"
38 #include "tree-into-ssa.h"
40 #include "tree-pass.h"
41 #include "tree-dump.h"
42 #include "gimple-pretty-print.h"
43 #include "diagnostic-core.h"
46 #include "tree-scalar-evolution.h"
47 #include "tree-ssa-propagate.h"
48 #include "tree-chrec.h"
49 #include "tree-ssa-threadupdate.h"
52 #include "tree-ssa-threadedge.h"
57 /* Range of values that can be associated with an SSA_NAME after VRP
61 /* Lattice value represented by this range. */
62 enum value_range_type type
;
64 /* Minimum and maximum values represented by this range. These
65 values should be interpreted as follows:
67 - If TYPE is VR_UNDEFINED or VR_VARYING then MIN and MAX must
70 - If TYPE == VR_RANGE then MIN holds the minimum value and
71 MAX holds the maximum value of the range [MIN, MAX].
73 - If TYPE == ANTI_RANGE the variable is known to NOT
74 take any values in the range [MIN, MAX]. */
78 /* Set of SSA names whose value ranges are equivalent to this one.
79 This set is only valid when TYPE is VR_RANGE or VR_ANTI_RANGE. */
83 typedef struct value_range_d value_range_t
;
85 #define VR_INITIALIZER { VR_UNDEFINED, NULL_TREE, NULL_TREE, NULL }
87 /* Set of SSA names found live during the RPO traversal of the function
88 for still active basic-blocks. */
91 /* Return true if the SSA name NAME is live on the edge E. */
94 live_on_edge (edge e
, tree name
)
96 return (live
[e
->dest
->index
]
97 && bitmap_bit_p (live
[e
->dest
->index
], SSA_NAME_VERSION (name
)));
100 /* Local functions. */
101 static int compare_values (tree val1
, tree val2
);
102 static int compare_values_warnv (tree val1
, tree val2
, bool *);
103 static void vrp_meet (value_range_t
*, value_range_t
*);
104 static void vrp_intersect_ranges (value_range_t
*, value_range_t
*);
105 static tree
vrp_evaluate_conditional_warnv_with_ops (enum tree_code
,
106 tree
, tree
, bool, bool *,
109 /* Location information for ASSERT_EXPRs. Each instance of this
110 structure describes an ASSERT_EXPR for an SSA name. Since a single
111 SSA name may have more than one assertion associated with it, these
112 locations are kept in a linked list attached to the corresponding
114 struct assert_locus_d
116 /* Basic block where the assertion would be inserted. */
119 /* Some assertions need to be inserted on an edge (e.g., assertions
120 generated by COND_EXPRs). In those cases, BB will be NULL. */
123 /* Pointer to the statement that generated this assertion. */
124 gimple_stmt_iterator si
;
126 /* Predicate code for the ASSERT_EXPR. Must be COMPARISON_CLASS_P. */
127 enum tree_code comp_code
;
129 /* Value being compared against. */
132 /* Expression to compare. */
135 /* Next node in the linked list. */
136 struct assert_locus_d
*next
;
139 typedef struct assert_locus_d
*assert_locus_t
;
141 /* If bit I is present, it means that SSA name N_i has a list of
142 assertions that should be inserted in the IL. */
143 static bitmap need_assert_for
;
145 /* Array of locations lists where to insert assertions. ASSERTS_FOR[I]
146 holds a list of ASSERT_LOCUS_T nodes that describe where
147 ASSERT_EXPRs for SSA name N_I should be inserted. */
148 static assert_locus_t
*asserts_for
;
150 /* Value range array. After propagation, VR_VALUE[I] holds the range
151 of values that SSA name N_I may take. */
152 static unsigned num_vr_values
;
153 static value_range_t
**vr_value
;
154 static bool values_propagated
;
156 /* For a PHI node which sets SSA name N_I, VR_COUNTS[I] holds the
157 number of executable edges we saw the last time we visited the
159 static int *vr_phi_edge_counts
;
166 static vec
<edge
> to_remove_edges
;
167 static vec
<switch_update
> to_update_switch_stmts
;
170 /* Return the maximum value for TYPE. */
173 vrp_val_max (const_tree type
)
175 if (!INTEGRAL_TYPE_P (type
))
178 return TYPE_MAX_VALUE (type
);
181 /* Return the minimum value for TYPE. */
184 vrp_val_min (const_tree type
)
186 if (!INTEGRAL_TYPE_P (type
))
189 return TYPE_MIN_VALUE (type
);
192 /* Return whether VAL is equal to the maximum value of its type. This
193 will be true for a positive overflow infinity. We can't do a
194 simple equality comparison with TYPE_MAX_VALUE because C typedefs
195 and Ada subtypes can produce types whose TYPE_MAX_VALUE is not ==
196 to the integer constant with the same value in the type. */
199 vrp_val_is_max (const_tree val
)
201 tree type_max
= vrp_val_max (TREE_TYPE (val
));
202 return (val
== type_max
203 || (type_max
!= NULL_TREE
204 && operand_equal_p (val
, type_max
, 0)));
207 /* Return whether VAL is equal to the minimum value of its type. This
208 will be true for a negative overflow infinity. */
211 vrp_val_is_min (const_tree val
)
213 tree type_min
= vrp_val_min (TREE_TYPE (val
));
214 return (val
== type_min
215 || (type_min
!= NULL_TREE
216 && operand_equal_p (val
, type_min
, 0)));
220 /* Return whether TYPE should use an overflow infinity distinct from
221 TYPE_{MIN,MAX}_VALUE. We use an overflow infinity value to
222 represent a signed overflow during VRP computations. An infinity
223 is distinct from a half-range, which will go from some number to
224 TYPE_{MIN,MAX}_VALUE. */
227 needs_overflow_infinity (const_tree type
)
229 return INTEGRAL_TYPE_P (type
) && !TYPE_OVERFLOW_WRAPS (type
);
232 /* Return whether TYPE can support our overflow infinity
233 representation: we use the TREE_OVERFLOW flag, which only exists
234 for constants. If TYPE doesn't support this, we don't optimize
235 cases which would require signed overflow--we drop them to
239 supports_overflow_infinity (const_tree type
)
241 tree min
= vrp_val_min (type
), max
= vrp_val_max (type
);
242 #ifdef ENABLE_CHECKING
243 gcc_assert (needs_overflow_infinity (type
));
245 return (min
!= NULL_TREE
246 && CONSTANT_CLASS_P (min
)
248 && CONSTANT_CLASS_P (max
));
251 /* VAL is the maximum or minimum value of a type. Return a
252 corresponding overflow infinity. */
255 make_overflow_infinity (tree val
)
257 gcc_checking_assert (val
!= NULL_TREE
&& CONSTANT_CLASS_P (val
));
258 val
= copy_node (val
);
259 TREE_OVERFLOW (val
) = 1;
263 /* Return a negative overflow infinity for TYPE. */
266 negative_overflow_infinity (tree type
)
268 gcc_checking_assert (supports_overflow_infinity (type
));
269 return make_overflow_infinity (vrp_val_min (type
));
272 /* Return a positive overflow infinity for TYPE. */
275 positive_overflow_infinity (tree type
)
277 gcc_checking_assert (supports_overflow_infinity (type
));
278 return make_overflow_infinity (vrp_val_max (type
));
281 /* Return whether VAL is a negative overflow infinity. */
284 is_negative_overflow_infinity (const_tree val
)
286 return (needs_overflow_infinity (TREE_TYPE (val
))
287 && CONSTANT_CLASS_P (val
)
288 && TREE_OVERFLOW (val
)
289 && vrp_val_is_min (val
));
292 /* Return whether VAL is a positive overflow infinity. */
295 is_positive_overflow_infinity (const_tree val
)
297 return (needs_overflow_infinity (TREE_TYPE (val
))
298 && CONSTANT_CLASS_P (val
)
299 && TREE_OVERFLOW (val
)
300 && vrp_val_is_max (val
));
303 /* Return whether VAL is a positive or negative overflow infinity. */
306 is_overflow_infinity (const_tree val
)
308 return (needs_overflow_infinity (TREE_TYPE (val
))
309 && CONSTANT_CLASS_P (val
)
310 && TREE_OVERFLOW (val
)
311 && (vrp_val_is_min (val
) || vrp_val_is_max (val
)));
314 /* Return whether STMT has a constant rhs that is_overflow_infinity. */
317 stmt_overflow_infinity (gimple stmt
)
319 if (is_gimple_assign (stmt
)
320 && get_gimple_rhs_class (gimple_assign_rhs_code (stmt
)) ==
322 return is_overflow_infinity (gimple_assign_rhs1 (stmt
));
326 /* If VAL is now an overflow infinity, return VAL. Otherwise, return
327 the same value with TREE_OVERFLOW clear. This can be used to avoid
328 confusing a regular value with an overflow value. */
331 avoid_overflow_infinity (tree val
)
333 if (!is_overflow_infinity (val
))
336 if (vrp_val_is_max (val
))
337 return vrp_val_max (TREE_TYPE (val
));
340 gcc_checking_assert (vrp_val_is_min (val
));
341 return vrp_val_min (TREE_TYPE (val
));
346 /* Return true if ARG is marked with the nonnull attribute in the
347 current function signature. */
350 nonnull_arg_p (const_tree arg
)
352 tree t
, attrs
, fntype
;
353 unsigned HOST_WIDE_INT arg_num
;
355 gcc_assert (TREE_CODE (arg
) == PARM_DECL
&& POINTER_TYPE_P (TREE_TYPE (arg
)));
357 /* The static chain decl is always non null. */
358 if (arg
== cfun
->static_chain_decl
)
361 fntype
= TREE_TYPE (current_function_decl
);
362 for (attrs
= TYPE_ATTRIBUTES (fntype
); attrs
; attrs
= TREE_CHAIN (attrs
))
364 attrs
= lookup_attribute ("nonnull", attrs
);
366 /* If "nonnull" wasn't specified, we know nothing about the argument. */
367 if (attrs
== NULL_TREE
)
370 /* If "nonnull" applies to all the arguments, then ARG is non-null. */
371 if (TREE_VALUE (attrs
) == NULL_TREE
)
374 /* Get the position number for ARG in the function signature. */
375 for (arg_num
= 1, t
= DECL_ARGUMENTS (current_function_decl
);
377 t
= DECL_CHAIN (t
), arg_num
++)
383 gcc_assert (t
== arg
);
385 /* Now see if ARG_NUM is mentioned in the nonnull list. */
386 for (t
= TREE_VALUE (attrs
); t
; t
= TREE_CHAIN (t
))
388 if (compare_tree_int (TREE_VALUE (t
), arg_num
) == 0)
397 /* Set value range VR to VR_UNDEFINED. */
400 set_value_range_to_undefined (value_range_t
*vr
)
402 vr
->type
= VR_UNDEFINED
;
403 vr
->min
= vr
->max
= NULL_TREE
;
405 bitmap_clear (vr
->equiv
);
409 /* Set value range VR to VR_VARYING. */
412 set_value_range_to_varying (value_range_t
*vr
)
414 vr
->type
= VR_VARYING
;
415 vr
->min
= vr
->max
= NULL_TREE
;
417 bitmap_clear (vr
->equiv
);
421 /* Set value range VR to {T, MIN, MAX, EQUIV}. */
424 set_value_range (value_range_t
*vr
, enum value_range_type t
, tree min
,
425 tree max
, bitmap equiv
)
427 #if defined ENABLE_CHECKING
428 /* Check the validity of the range. */
429 if (t
== VR_RANGE
|| t
== VR_ANTI_RANGE
)
433 gcc_assert (min
&& max
);
435 if (INTEGRAL_TYPE_P (TREE_TYPE (min
)) && t
== VR_ANTI_RANGE
)
436 gcc_assert (!vrp_val_is_min (min
) || !vrp_val_is_max (max
));
438 cmp
= compare_values (min
, max
);
439 gcc_assert (cmp
== 0 || cmp
== -1 || cmp
== -2);
441 if (needs_overflow_infinity (TREE_TYPE (min
)))
442 gcc_assert (!is_overflow_infinity (min
)
443 || !is_overflow_infinity (max
));
446 if (t
== VR_UNDEFINED
|| t
== VR_VARYING
)
447 gcc_assert (min
== NULL_TREE
&& max
== NULL_TREE
);
449 if (t
== VR_UNDEFINED
|| t
== VR_VARYING
)
450 gcc_assert (equiv
== NULL
|| bitmap_empty_p (equiv
));
457 /* Since updating the equivalence set involves deep copying the
458 bitmaps, only do it if absolutely necessary. */
459 if (vr
->equiv
== NULL
461 vr
->equiv
= BITMAP_ALLOC (NULL
);
463 if (equiv
!= vr
->equiv
)
465 if (equiv
&& !bitmap_empty_p (equiv
))
466 bitmap_copy (vr
->equiv
, equiv
);
468 bitmap_clear (vr
->equiv
);
473 /* Set value range VR to the canonical form of {T, MIN, MAX, EQUIV}.
474 This means adjusting T, MIN and MAX representing the case of a
475 wrapping range with MAX < MIN covering [MIN, type_max] U [type_min, MAX]
476 as anti-rage ~[MAX+1, MIN-1]. Likewise for wrapping anti-ranges.
477 In corner cases where MAX+1 or MIN-1 wraps this will fall back
479 This routine exists to ease canonicalization in the case where we
480 extract ranges from var + CST op limit. */
483 set_and_canonicalize_value_range (value_range_t
*vr
, enum value_range_type t
,
484 tree min
, tree max
, bitmap equiv
)
486 /* Use the canonical setters for VR_UNDEFINED and VR_VARYING. */
487 if (t
== VR_UNDEFINED
)
489 set_value_range_to_undefined (vr
);
492 else if (t
== VR_VARYING
)
494 set_value_range_to_varying (vr
);
498 /* Nothing to canonicalize for symbolic ranges. */
499 if (TREE_CODE (min
) != INTEGER_CST
500 || TREE_CODE (max
) != INTEGER_CST
)
502 set_value_range (vr
, t
, min
, max
, equiv
);
506 /* Wrong order for min and max, to swap them and the VR type we need
508 if (tree_int_cst_lt (max
, min
))
512 /* For one bit precision if max < min, then the swapped
513 range covers all values, so for VR_RANGE it is varying and
514 for VR_ANTI_RANGE empty range, so drop to varying as well. */
515 if (TYPE_PRECISION (TREE_TYPE (min
)) == 1)
517 set_value_range_to_varying (vr
);
521 one
= build_int_cst (TREE_TYPE (min
), 1);
522 tmp
= int_const_binop (PLUS_EXPR
, max
, one
);
523 max
= int_const_binop (MINUS_EXPR
, min
, one
);
526 /* There's one corner case, if we had [C+1, C] before we now have
527 that again. But this represents an empty value range, so drop
528 to varying in this case. */
529 if (tree_int_cst_lt (max
, min
))
531 set_value_range_to_varying (vr
);
535 t
= t
== VR_RANGE
? VR_ANTI_RANGE
: VR_RANGE
;
538 /* Anti-ranges that can be represented as ranges should be so. */
539 if (t
== VR_ANTI_RANGE
)
541 bool is_min
= vrp_val_is_min (min
);
542 bool is_max
= vrp_val_is_max (max
);
544 if (is_min
&& is_max
)
546 /* We cannot deal with empty ranges, drop to varying.
547 ??? This could be VR_UNDEFINED instead. */
548 set_value_range_to_varying (vr
);
551 else if (TYPE_PRECISION (TREE_TYPE (min
)) == 1
552 && (is_min
|| is_max
))
554 /* Non-empty boolean ranges can always be represented
555 as a singleton range. */
557 min
= max
= vrp_val_max (TREE_TYPE (min
));
559 min
= max
= vrp_val_min (TREE_TYPE (min
));
563 /* As a special exception preserve non-null ranges. */
564 && !(TYPE_UNSIGNED (TREE_TYPE (min
))
565 && integer_zerop (max
)))
567 tree one
= build_int_cst (TREE_TYPE (max
), 1);
568 min
= int_const_binop (PLUS_EXPR
, max
, one
);
569 max
= vrp_val_max (TREE_TYPE (max
));
574 tree one
= build_int_cst (TREE_TYPE (min
), 1);
575 max
= int_const_binop (MINUS_EXPR
, min
, one
);
576 min
= vrp_val_min (TREE_TYPE (min
));
581 /* Drop [-INF(OVF), +INF(OVF)] to varying. */
582 if (needs_overflow_infinity (TREE_TYPE (min
))
583 && is_overflow_infinity (min
)
584 && is_overflow_infinity (max
))
586 set_value_range_to_varying (vr
);
590 set_value_range (vr
, t
, min
, max
, equiv
);
593 /* Copy value range FROM into value range TO. */
596 copy_value_range (value_range_t
*to
, value_range_t
*from
)
598 set_value_range (to
, from
->type
, from
->min
, from
->max
, from
->equiv
);
601 /* Set value range VR to a single value. This function is only called
602 with values we get from statements, and exists to clear the
603 TREE_OVERFLOW flag so that we don't think we have an overflow
604 infinity when we shouldn't. */
607 set_value_range_to_value (value_range_t
*vr
, tree val
, bitmap equiv
)
609 gcc_assert (is_gimple_min_invariant (val
));
610 val
= avoid_overflow_infinity (val
);
611 set_value_range (vr
, VR_RANGE
, val
, val
, equiv
);
614 /* Set value range VR to a non-negative range of type TYPE.
615 OVERFLOW_INFINITY indicates whether to use an overflow infinity
616 rather than TYPE_MAX_VALUE; this should be true if we determine
617 that the range is nonnegative based on the assumption that signed
618 overflow does not occur. */
621 set_value_range_to_nonnegative (value_range_t
*vr
, tree type
,
622 bool overflow_infinity
)
626 if (overflow_infinity
&& !supports_overflow_infinity (type
))
628 set_value_range_to_varying (vr
);
632 zero
= build_int_cst (type
, 0);
633 set_value_range (vr
, VR_RANGE
, zero
,
635 ? positive_overflow_infinity (type
)
636 : TYPE_MAX_VALUE (type
)),
640 /* Set value range VR to a non-NULL range of type TYPE. */
643 set_value_range_to_nonnull (value_range_t
*vr
, tree type
)
645 tree zero
= build_int_cst (type
, 0);
646 set_value_range (vr
, VR_ANTI_RANGE
, zero
, zero
, vr
->equiv
);
650 /* Set value range VR to a NULL range of type TYPE. */
653 set_value_range_to_null (value_range_t
*vr
, tree type
)
655 set_value_range_to_value (vr
, build_int_cst (type
, 0), vr
->equiv
);
659 /* Set value range VR to a range of a truthvalue of type TYPE. */
662 set_value_range_to_truthvalue (value_range_t
*vr
, tree type
)
664 if (TYPE_PRECISION (type
) == 1)
665 set_value_range_to_varying (vr
);
667 set_value_range (vr
, VR_RANGE
,
668 build_int_cst (type
, 0), build_int_cst (type
, 1),
673 /* If abs (min) < abs (max), set VR to [-max, max], if
674 abs (min) >= abs (max), set VR to [-min, min]. */
677 abs_extent_range (value_range_t
*vr
, tree min
, tree max
)
681 gcc_assert (TREE_CODE (min
) == INTEGER_CST
);
682 gcc_assert (TREE_CODE (max
) == INTEGER_CST
);
683 gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (min
)));
684 gcc_assert (!TYPE_UNSIGNED (TREE_TYPE (min
)));
685 min
= fold_unary (ABS_EXPR
, TREE_TYPE (min
), min
);
686 max
= fold_unary (ABS_EXPR
, TREE_TYPE (max
), max
);
687 if (TREE_OVERFLOW (min
) || TREE_OVERFLOW (max
))
689 set_value_range_to_varying (vr
);
692 cmp
= compare_values (min
, max
);
694 min
= fold_unary (NEGATE_EXPR
, TREE_TYPE (min
), max
);
695 else if (cmp
== 0 || cmp
== 1)
698 min
= fold_unary (NEGATE_EXPR
, TREE_TYPE (min
), min
);
702 set_value_range_to_varying (vr
);
705 set_and_canonicalize_value_range (vr
, VR_RANGE
, min
, max
, NULL
);
709 /* Return value range information for VAR.
711 If we have no values ranges recorded (ie, VRP is not running), then
712 return NULL. Otherwise create an empty range if none existed for VAR. */
714 static value_range_t
*
715 get_value_range (const_tree var
)
717 static const struct value_range_d vr_const_varying
718 = { VR_VARYING
, NULL_TREE
, NULL_TREE
, NULL
};
721 unsigned ver
= SSA_NAME_VERSION (var
);
723 /* If we have no recorded ranges, then return NULL. */
727 /* If we query the range for a new SSA name return an unmodifiable VARYING.
728 We should get here at most from the substitute-and-fold stage which
729 will never try to change values. */
730 if (ver
>= num_vr_values
)
731 return CONST_CAST (value_range_t
*, &vr_const_varying
);
737 /* After propagation finished do not allocate new value-ranges. */
738 if (values_propagated
)
739 return CONST_CAST (value_range_t
*, &vr_const_varying
);
741 /* Create a default value range. */
742 vr_value
[ver
] = vr
= XCNEW (value_range_t
);
744 /* Defer allocating the equivalence set. */
747 /* If VAR is a default definition of a parameter, the variable can
748 take any value in VAR's type. */
749 if (SSA_NAME_IS_DEFAULT_DEF (var
))
751 sym
= SSA_NAME_VAR (var
);
752 if (TREE_CODE (sym
) == PARM_DECL
)
754 /* Try to use the "nonnull" attribute to create ~[0, 0]
755 anti-ranges for pointers. Note that this is only valid with
756 default definitions of PARM_DECLs. */
757 if (POINTER_TYPE_P (TREE_TYPE (sym
))
758 && nonnull_arg_p (sym
))
759 set_value_range_to_nonnull (vr
, TREE_TYPE (sym
));
761 set_value_range_to_varying (vr
);
763 else if (TREE_CODE (sym
) == RESULT_DECL
764 && DECL_BY_REFERENCE (sym
))
765 set_value_range_to_nonnull (vr
, TREE_TYPE (sym
));
771 /* Return true, if VAL1 and VAL2 are equal values for VRP purposes. */
774 vrp_operand_equal_p (const_tree val1
, const_tree val2
)
778 if (!val1
|| !val2
|| !operand_equal_p (val1
, val2
, 0))
780 if (is_overflow_infinity (val1
))
781 return is_overflow_infinity (val2
);
785 /* Return true, if the bitmaps B1 and B2 are equal. */
788 vrp_bitmap_equal_p (const_bitmap b1
, const_bitmap b2
)
791 || ((!b1
|| bitmap_empty_p (b1
))
792 && (!b2
|| bitmap_empty_p (b2
)))
794 && bitmap_equal_p (b1
, b2
)));
797 /* Update the value range and equivalence set for variable VAR to
798 NEW_VR. Return true if NEW_VR is different from VAR's previous
801 NOTE: This function assumes that NEW_VR is a temporary value range
802 object created for the sole purpose of updating VAR's range. The
803 storage used by the equivalence set from NEW_VR will be freed by
804 this function. Do not call update_value_range when NEW_VR
805 is the range object associated with another SSA name. */
808 update_value_range (const_tree var
, value_range_t
*new_vr
)
810 value_range_t
*old_vr
;
813 /* Update the value range, if necessary. */
814 old_vr
= get_value_range (var
);
815 is_new
= old_vr
->type
!= new_vr
->type
816 || !vrp_operand_equal_p (old_vr
->min
, new_vr
->min
)
817 || !vrp_operand_equal_p (old_vr
->max
, new_vr
->max
)
818 || !vrp_bitmap_equal_p (old_vr
->equiv
, new_vr
->equiv
);
822 /* Do not allow transitions up the lattice. The following
823 is slightly more awkward than just new_vr->type < old_vr->type
824 because VR_RANGE and VR_ANTI_RANGE need to be considered
825 the same. We may not have is_new when transitioning to
826 UNDEFINED or from VARYING. */
827 if (new_vr
->type
== VR_UNDEFINED
828 || old_vr
->type
== VR_VARYING
)
829 set_value_range_to_varying (old_vr
);
831 set_value_range (old_vr
, new_vr
->type
, new_vr
->min
, new_vr
->max
,
835 BITMAP_FREE (new_vr
->equiv
);
841 /* Add VAR and VAR's equivalence set to EQUIV. This is the central
842 point where equivalence processing can be turned on/off. */
845 add_equivalence (bitmap
*equiv
, const_tree var
)
847 unsigned ver
= SSA_NAME_VERSION (var
);
848 value_range_t
*vr
= vr_value
[ver
];
851 *equiv
= BITMAP_ALLOC (NULL
);
852 bitmap_set_bit (*equiv
, ver
);
854 bitmap_ior_into (*equiv
, vr
->equiv
);
858 /* Return true if VR is ~[0, 0]. */
861 range_is_nonnull (value_range_t
*vr
)
863 return vr
->type
== VR_ANTI_RANGE
864 && integer_zerop (vr
->min
)
865 && integer_zerop (vr
->max
);
869 /* Return true if VR is [0, 0]. */
872 range_is_null (value_range_t
*vr
)
874 return vr
->type
== VR_RANGE
875 && integer_zerop (vr
->min
)
876 && integer_zerop (vr
->max
);
879 /* Return true if max and min of VR are INTEGER_CST. It's not necessary
883 range_int_cst_p (value_range_t
*vr
)
885 return (vr
->type
== VR_RANGE
886 && TREE_CODE (vr
->max
) == INTEGER_CST
887 && TREE_CODE (vr
->min
) == INTEGER_CST
);
890 /* Return true if VR is a INTEGER_CST singleton. */
893 range_int_cst_singleton_p (value_range_t
*vr
)
895 return (range_int_cst_p (vr
)
896 && !is_overflow_infinity (vr
->min
)
897 && !is_overflow_infinity (vr
->max
)
898 && tree_int_cst_equal (vr
->min
, vr
->max
));
901 /* Return true if value range VR involves at least one symbol. */
904 symbolic_range_p (value_range_t
*vr
)
906 return (!is_gimple_min_invariant (vr
->min
)
907 || !is_gimple_min_invariant (vr
->max
));
910 /* Return true if value range VR uses an overflow infinity. */
913 overflow_infinity_range_p (value_range_t
*vr
)
915 return (vr
->type
== VR_RANGE
916 && (is_overflow_infinity (vr
->min
)
917 || is_overflow_infinity (vr
->max
)));
920 /* Return false if we can not make a valid comparison based on VR;
921 this will be the case if it uses an overflow infinity and overflow
922 is not undefined (i.e., -fno-strict-overflow is in effect).
923 Otherwise return true, and set *STRICT_OVERFLOW_P to true if VR
924 uses an overflow infinity. */
927 usable_range_p (value_range_t
*vr
, bool *strict_overflow_p
)
929 gcc_assert (vr
->type
== VR_RANGE
);
930 if (is_overflow_infinity (vr
->min
))
932 *strict_overflow_p
= true;
933 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (vr
->min
)))
936 if (is_overflow_infinity (vr
->max
))
938 *strict_overflow_p
= true;
939 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (vr
->max
)))
946 /* Return true if the result of assignment STMT is know to be non-negative.
947 If the return value is based on the assumption that signed overflow is
948 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
949 *STRICT_OVERFLOW_P.*/
952 gimple_assign_nonnegative_warnv_p (gimple stmt
, bool *strict_overflow_p
)
954 enum tree_code code
= gimple_assign_rhs_code (stmt
);
955 switch (get_gimple_rhs_class (code
))
957 case GIMPLE_UNARY_RHS
:
958 return tree_unary_nonnegative_warnv_p (gimple_assign_rhs_code (stmt
),
959 gimple_expr_type (stmt
),
960 gimple_assign_rhs1 (stmt
),
962 case GIMPLE_BINARY_RHS
:
963 return tree_binary_nonnegative_warnv_p (gimple_assign_rhs_code (stmt
),
964 gimple_expr_type (stmt
),
965 gimple_assign_rhs1 (stmt
),
966 gimple_assign_rhs2 (stmt
),
968 case GIMPLE_TERNARY_RHS
:
970 case GIMPLE_SINGLE_RHS
:
971 return tree_single_nonnegative_warnv_p (gimple_assign_rhs1 (stmt
),
973 case GIMPLE_INVALID_RHS
:
980 /* Return true if return value of call STMT is know to be non-negative.
981 If the return value is based on the assumption that signed overflow is
982 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
983 *STRICT_OVERFLOW_P.*/
986 gimple_call_nonnegative_warnv_p (gimple stmt
, bool *strict_overflow_p
)
988 tree arg0
= gimple_call_num_args (stmt
) > 0 ?
989 gimple_call_arg (stmt
, 0) : NULL_TREE
;
990 tree arg1
= gimple_call_num_args (stmt
) > 1 ?
991 gimple_call_arg (stmt
, 1) : NULL_TREE
;
993 return tree_call_nonnegative_warnv_p (gimple_expr_type (stmt
),
994 gimple_call_fndecl (stmt
),
1000 /* Return true if STMT is know to to compute a non-negative value.
1001 If the return value is based on the assumption that signed overflow is
1002 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1003 *STRICT_OVERFLOW_P.*/
1006 gimple_stmt_nonnegative_warnv_p (gimple stmt
, bool *strict_overflow_p
)
1008 switch (gimple_code (stmt
))
1011 return gimple_assign_nonnegative_warnv_p (stmt
, strict_overflow_p
);
1013 return gimple_call_nonnegative_warnv_p (stmt
, strict_overflow_p
);
1019 /* Return true if the result of assignment STMT is know to be non-zero.
1020 If the return value is based on the assumption that signed overflow is
1021 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1022 *STRICT_OVERFLOW_P.*/
1025 gimple_assign_nonzero_warnv_p (gimple stmt
, bool *strict_overflow_p
)
1027 enum tree_code code
= gimple_assign_rhs_code (stmt
);
1028 switch (get_gimple_rhs_class (code
))
1030 case GIMPLE_UNARY_RHS
:
1031 return tree_unary_nonzero_warnv_p (gimple_assign_rhs_code (stmt
),
1032 gimple_expr_type (stmt
),
1033 gimple_assign_rhs1 (stmt
),
1035 case GIMPLE_BINARY_RHS
:
1036 return tree_binary_nonzero_warnv_p (gimple_assign_rhs_code (stmt
),
1037 gimple_expr_type (stmt
),
1038 gimple_assign_rhs1 (stmt
),
1039 gimple_assign_rhs2 (stmt
),
1041 case GIMPLE_TERNARY_RHS
:
1043 case GIMPLE_SINGLE_RHS
:
1044 return tree_single_nonzero_warnv_p (gimple_assign_rhs1 (stmt
),
1046 case GIMPLE_INVALID_RHS
:
1053 /* Return true if STMT is known to compute a non-zero value.
1054 If the return value is based on the assumption that signed overflow is
1055 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1056 *STRICT_OVERFLOW_P.*/
1059 gimple_stmt_nonzero_warnv_p (gimple stmt
, bool *strict_overflow_p
)
1061 switch (gimple_code (stmt
))
1064 return gimple_assign_nonzero_warnv_p (stmt
, strict_overflow_p
);
1067 tree fndecl
= gimple_call_fndecl (stmt
);
1068 if (!fndecl
) return false;
1069 if (flag_delete_null_pointer_checks
&& !flag_check_new
1070 && DECL_IS_OPERATOR_NEW (fndecl
)
1071 && !TREE_NOTHROW (fndecl
))
1073 if (flag_delete_null_pointer_checks
&&
1074 lookup_attribute ("returns_nonnull",
1075 TYPE_ATTRIBUTES (gimple_call_fntype (stmt
))))
1077 return gimple_alloca_call_p (stmt
);
1084 /* Like tree_expr_nonzero_warnv_p, but this function uses value ranges
1088 vrp_stmt_computes_nonzero (gimple stmt
, bool *strict_overflow_p
)
1090 if (gimple_stmt_nonzero_warnv_p (stmt
, strict_overflow_p
))
1093 /* If we have an expression of the form &X->a, then the expression
1094 is nonnull if X is nonnull. */
1095 if (is_gimple_assign (stmt
)
1096 && gimple_assign_rhs_code (stmt
) == ADDR_EXPR
)
1098 tree expr
= gimple_assign_rhs1 (stmt
);
1099 tree base
= get_base_address (TREE_OPERAND (expr
, 0));
1101 if (base
!= NULL_TREE
1102 && TREE_CODE (base
) == MEM_REF
1103 && TREE_CODE (TREE_OPERAND (base
, 0)) == SSA_NAME
)
1105 value_range_t
*vr
= get_value_range (TREE_OPERAND (base
, 0));
1106 if (range_is_nonnull (vr
))
1114 /* Returns true if EXPR is a valid value (as expected by compare_values) --
1115 a gimple invariant, or SSA_NAME +- CST. */
1118 valid_value_p (tree expr
)
1120 if (TREE_CODE (expr
) == SSA_NAME
)
1123 if (TREE_CODE (expr
) == PLUS_EXPR
1124 || TREE_CODE (expr
) == MINUS_EXPR
)
1125 return (TREE_CODE (TREE_OPERAND (expr
, 0)) == SSA_NAME
1126 && TREE_CODE (TREE_OPERAND (expr
, 1)) == INTEGER_CST
);
1128 return is_gimple_min_invariant (expr
);
1134 -2 if those are incomparable. */
1136 operand_less_p (tree val
, tree val2
)
1138 /* LT is folded faster than GE and others. Inline the common case. */
1139 if (TREE_CODE (val
) == INTEGER_CST
&& TREE_CODE (val2
) == INTEGER_CST
)
1140 return INT_CST_LT (val
, val2
);
1145 fold_defer_overflow_warnings ();
1147 tcmp
= fold_binary_to_constant (LT_EXPR
, boolean_type_node
, val
, val2
);
1149 fold_undefer_and_ignore_overflow_warnings ();
1152 || TREE_CODE (tcmp
) != INTEGER_CST
)
1155 if (!integer_zerop (tcmp
))
1159 /* val >= val2, not considering overflow infinity. */
1160 if (is_negative_overflow_infinity (val
))
1161 return is_negative_overflow_infinity (val2
) ? 0 : 1;
1162 else if (is_positive_overflow_infinity (val2
))
1163 return is_positive_overflow_infinity (val
) ? 0 : 1;
1168 /* Compare two values VAL1 and VAL2. Return
1170 -2 if VAL1 and VAL2 cannot be compared at compile-time,
1173 +1 if VAL1 > VAL2, and
1176 This is similar to tree_int_cst_compare but supports pointer values
1177 and values that cannot be compared at compile time.
1179 If STRICT_OVERFLOW_P is not NULL, then set *STRICT_OVERFLOW_P to
1180 true if the return value is only valid if we assume that signed
1181 overflow is undefined. */
1184 compare_values_warnv (tree val1
, tree val2
, bool *strict_overflow_p
)
1189 /* Below we rely on the fact that VAL1 and VAL2 are both pointers or
1191 gcc_assert (POINTER_TYPE_P (TREE_TYPE (val1
))
1192 == POINTER_TYPE_P (TREE_TYPE (val2
)));
1193 /* Convert the two values into the same type. This is needed because
1194 sizetype causes sign extension even for unsigned types. */
1195 val2
= fold_convert (TREE_TYPE (val1
), val2
);
1196 STRIP_USELESS_TYPE_CONVERSION (val2
);
1198 if ((TREE_CODE (val1
) == SSA_NAME
1199 || TREE_CODE (val1
) == PLUS_EXPR
1200 || TREE_CODE (val1
) == MINUS_EXPR
)
1201 && (TREE_CODE (val2
) == SSA_NAME
1202 || TREE_CODE (val2
) == PLUS_EXPR
1203 || TREE_CODE (val2
) == MINUS_EXPR
))
1205 tree n1
, c1
, n2
, c2
;
1206 enum tree_code code1
, code2
;
1208 /* If VAL1 and VAL2 are of the form 'NAME [+-] CST' or 'NAME',
1209 return -1 or +1 accordingly. If VAL1 and VAL2 don't use the
1210 same name, return -2. */
1211 if (TREE_CODE (val1
) == SSA_NAME
)
1219 code1
= TREE_CODE (val1
);
1220 n1
= TREE_OPERAND (val1
, 0);
1221 c1
= TREE_OPERAND (val1
, 1);
1222 if (tree_int_cst_sgn (c1
) == -1)
1224 if (is_negative_overflow_infinity (c1
))
1226 c1
= fold_unary_to_constant (NEGATE_EXPR
, TREE_TYPE (c1
), c1
);
1229 code1
= code1
== MINUS_EXPR
? PLUS_EXPR
: MINUS_EXPR
;
1233 if (TREE_CODE (val2
) == SSA_NAME
)
1241 code2
= TREE_CODE (val2
);
1242 n2
= TREE_OPERAND (val2
, 0);
1243 c2
= TREE_OPERAND (val2
, 1);
1244 if (tree_int_cst_sgn (c2
) == -1)
1246 if (is_negative_overflow_infinity (c2
))
1248 c2
= fold_unary_to_constant (NEGATE_EXPR
, TREE_TYPE (c2
), c2
);
1251 code2
= code2
== MINUS_EXPR
? PLUS_EXPR
: MINUS_EXPR
;
1255 /* Both values must use the same name. */
1259 if (code1
== SSA_NAME
1260 && code2
== SSA_NAME
)
1264 /* If overflow is defined we cannot simplify more. */
1265 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (val1
)))
1268 if (strict_overflow_p
!= NULL
1269 && (code1
== SSA_NAME
|| !TREE_NO_WARNING (val1
))
1270 && (code2
== SSA_NAME
|| !TREE_NO_WARNING (val2
)))
1271 *strict_overflow_p
= true;
1273 if (code1
== SSA_NAME
)
1275 if (code2
== PLUS_EXPR
)
1276 /* NAME < NAME + CST */
1278 else if (code2
== MINUS_EXPR
)
1279 /* NAME > NAME - CST */
1282 else if (code1
== PLUS_EXPR
)
1284 if (code2
== SSA_NAME
)
1285 /* NAME + CST > NAME */
1287 else if (code2
== PLUS_EXPR
)
1288 /* NAME + CST1 > NAME + CST2, if CST1 > CST2 */
1289 return compare_values_warnv (c1
, c2
, strict_overflow_p
);
1290 else if (code2
== MINUS_EXPR
)
1291 /* NAME + CST1 > NAME - CST2 */
1294 else if (code1
== MINUS_EXPR
)
1296 if (code2
== SSA_NAME
)
1297 /* NAME - CST < NAME */
1299 else if (code2
== PLUS_EXPR
)
1300 /* NAME - CST1 < NAME + CST2 */
1302 else if (code2
== MINUS_EXPR
)
1303 /* NAME - CST1 > NAME - CST2, if CST1 < CST2. Notice that
1304 C1 and C2 are swapped in the call to compare_values. */
1305 return compare_values_warnv (c2
, c1
, strict_overflow_p
);
1311 /* We cannot compare non-constants. */
1312 if (!is_gimple_min_invariant (val1
) || !is_gimple_min_invariant (val2
))
1315 if (!POINTER_TYPE_P (TREE_TYPE (val1
)))
1317 /* We cannot compare overflowed values, except for overflow
1319 if (TREE_OVERFLOW (val1
) || TREE_OVERFLOW (val2
))
1321 if (strict_overflow_p
!= NULL
)
1322 *strict_overflow_p
= true;
1323 if (is_negative_overflow_infinity (val1
))
1324 return is_negative_overflow_infinity (val2
) ? 0 : -1;
1325 else if (is_negative_overflow_infinity (val2
))
1327 else if (is_positive_overflow_infinity (val1
))
1328 return is_positive_overflow_infinity (val2
) ? 0 : 1;
1329 else if (is_positive_overflow_infinity (val2
))
1334 return tree_int_cst_compare (val1
, val2
);
1340 /* First see if VAL1 and VAL2 are not the same. */
1341 if (val1
== val2
|| operand_equal_p (val1
, val2
, 0))
1344 /* If VAL1 is a lower address than VAL2, return -1. */
1345 if (operand_less_p (val1
, val2
) == 1)
1348 /* If VAL1 is a higher address than VAL2, return +1. */
1349 if (operand_less_p (val2
, val1
) == 1)
1352 /* If VAL1 is different than VAL2, return +2.
1353 For integer constants we either have already returned -1 or 1
1354 or they are equivalent. We still might succeed in proving
1355 something about non-trivial operands. */
1356 if (TREE_CODE (val1
) != INTEGER_CST
1357 || TREE_CODE (val2
) != INTEGER_CST
)
1359 t
= fold_binary_to_constant (NE_EXPR
, boolean_type_node
, val1
, val2
);
1360 if (t
&& integer_onep (t
))
1368 /* Compare values like compare_values_warnv, but treat comparisons of
1369 nonconstants which rely on undefined overflow as incomparable. */
1372 compare_values (tree val1
, tree val2
)
1378 ret
= compare_values_warnv (val1
, val2
, &sop
);
1380 && (!is_gimple_min_invariant (val1
) || !is_gimple_min_invariant (val2
)))
1386 /* Return 1 if VAL is inside value range MIN <= VAL <= MAX,
1387 0 if VAL is not inside [MIN, MAX],
1388 -2 if we cannot tell either way.
1390 Benchmark compile/20001226-1.c compilation time after changing this
1394 value_inside_range (tree val
, tree min
, tree max
)
1398 cmp1
= operand_less_p (val
, min
);
1404 cmp2
= operand_less_p (max
, val
);
1412 /* Return true if value ranges VR0 and VR1 have a non-empty
1415 Benchmark compile/20001226-1.c compilation time after changing this
1420 value_ranges_intersect_p (value_range_t
*vr0
, value_range_t
*vr1
)
1422 /* The value ranges do not intersect if the maximum of the first range is
1423 less than the minimum of the second range or vice versa.
1424 When those relations are unknown, we can't do any better. */
1425 if (operand_less_p (vr0
->max
, vr1
->min
) != 0)
1427 if (operand_less_p (vr1
->max
, vr0
->min
) != 0)
1433 /* Return 1 if [MIN, MAX] includes the value zero, 0 if it does not
1434 include the value zero, -2 if we cannot tell. */
1437 range_includes_zero_p (tree min
, tree max
)
1439 tree zero
= build_int_cst (TREE_TYPE (min
), 0);
1440 return value_inside_range (zero
, min
, max
);
1443 /* Return true if *VR is know to only contain nonnegative values. */
1446 value_range_nonnegative_p (value_range_t
*vr
)
1448 /* Testing for VR_ANTI_RANGE is not useful here as any anti-range
1449 which would return a useful value should be encoded as a
1451 if (vr
->type
== VR_RANGE
)
1453 int result
= compare_values (vr
->min
, integer_zero_node
);
1454 return (result
== 0 || result
== 1);
1460 /* If *VR has a value rante that is a single constant value return that,
1461 otherwise return NULL_TREE. */
1464 value_range_constant_singleton (value_range_t
*vr
)
1466 if (vr
->type
== VR_RANGE
1467 && operand_equal_p (vr
->min
, vr
->max
, 0)
1468 && is_gimple_min_invariant (vr
->min
))
1474 /* If OP has a value range with a single constant value return that,
1475 otherwise return NULL_TREE. This returns OP itself if OP is a
1479 op_with_constant_singleton_value_range (tree op
)
1481 if (is_gimple_min_invariant (op
))
1484 if (TREE_CODE (op
) != SSA_NAME
)
1487 return value_range_constant_singleton (get_value_range (op
));
1490 /* Return true if op is in a boolean [0, 1] value-range. */
1493 op_with_boolean_value_range_p (tree op
)
1497 if (TYPE_PRECISION (TREE_TYPE (op
)) == 1)
1500 if (integer_zerop (op
)
1501 || integer_onep (op
))
1504 if (TREE_CODE (op
) != SSA_NAME
)
1507 vr
= get_value_range (op
);
1508 return (vr
->type
== VR_RANGE
1509 && integer_zerop (vr
->min
)
1510 && integer_onep (vr
->max
));
1513 /* Extract value range information from an ASSERT_EXPR EXPR and store
1517 extract_range_from_assert (value_range_t
*vr_p
, tree expr
)
1519 tree var
, cond
, limit
, min
, max
, type
;
1520 value_range_t
*limit_vr
;
1521 enum tree_code cond_code
;
1523 var
= ASSERT_EXPR_VAR (expr
);
1524 cond
= ASSERT_EXPR_COND (expr
);
1526 gcc_assert (COMPARISON_CLASS_P (cond
));
1528 /* Find VAR in the ASSERT_EXPR conditional. */
1529 if (var
== TREE_OPERAND (cond
, 0)
1530 || TREE_CODE (TREE_OPERAND (cond
, 0)) == PLUS_EXPR
1531 || TREE_CODE (TREE_OPERAND (cond
, 0)) == NOP_EXPR
)
1533 /* If the predicate is of the form VAR COMP LIMIT, then we just
1534 take LIMIT from the RHS and use the same comparison code. */
1535 cond_code
= TREE_CODE (cond
);
1536 limit
= TREE_OPERAND (cond
, 1);
1537 cond
= TREE_OPERAND (cond
, 0);
1541 /* If the predicate is of the form LIMIT COMP VAR, then we need
1542 to flip around the comparison code to create the proper range
1544 cond_code
= swap_tree_comparison (TREE_CODE (cond
));
1545 limit
= TREE_OPERAND (cond
, 0);
1546 cond
= TREE_OPERAND (cond
, 1);
1549 limit
= avoid_overflow_infinity (limit
);
1551 type
= TREE_TYPE (var
);
1552 gcc_assert (limit
!= var
);
1554 /* For pointer arithmetic, we only keep track of pointer equality
1556 if (POINTER_TYPE_P (type
) && cond_code
!= NE_EXPR
&& cond_code
!= EQ_EXPR
)
1558 set_value_range_to_varying (vr_p
);
1562 /* If LIMIT is another SSA name and LIMIT has a range of its own,
1563 try to use LIMIT's range to avoid creating symbolic ranges
1565 limit_vr
= (TREE_CODE (limit
) == SSA_NAME
) ? get_value_range (limit
) : NULL
;
1567 /* LIMIT's range is only interesting if it has any useful information. */
1569 && (limit_vr
->type
== VR_UNDEFINED
1570 || limit_vr
->type
== VR_VARYING
1571 || symbolic_range_p (limit_vr
)))
1574 /* Initially, the new range has the same set of equivalences of
1575 VAR's range. This will be revised before returning the final
1576 value. Since assertions may be chained via mutually exclusive
1577 predicates, we will need to trim the set of equivalences before
1579 gcc_assert (vr_p
->equiv
== NULL
);
1580 add_equivalence (&vr_p
->equiv
, var
);
1582 /* Extract a new range based on the asserted comparison for VAR and
1583 LIMIT's value range. Notice that if LIMIT has an anti-range, we
1584 will only use it for equality comparisons (EQ_EXPR). For any
1585 other kind of assertion, we cannot derive a range from LIMIT's
1586 anti-range that can be used to describe the new range. For
1587 instance, ASSERT_EXPR <x_2, x_2 <= b_4>. If b_4 is ~[2, 10],
1588 then b_4 takes on the ranges [-INF, 1] and [11, +INF]. There is
1589 no single range for x_2 that could describe LE_EXPR, so we might
1590 as well build the range [b_4, +INF] for it.
1591 One special case we handle is extracting a range from a
1592 range test encoded as (unsigned)var + CST <= limit. */
1593 if (TREE_CODE (cond
) == NOP_EXPR
1594 || TREE_CODE (cond
) == PLUS_EXPR
)
1596 if (TREE_CODE (cond
) == PLUS_EXPR
)
1598 min
= fold_build1 (NEGATE_EXPR
, TREE_TYPE (TREE_OPERAND (cond
, 1)),
1599 TREE_OPERAND (cond
, 1));
1600 max
= int_const_binop (PLUS_EXPR
, limit
, min
);
1601 cond
= TREE_OPERAND (cond
, 0);
1605 min
= build_int_cst (TREE_TYPE (var
), 0);
1609 /* Make sure to not set TREE_OVERFLOW on the final type
1610 conversion. We are willingly interpreting large positive
1611 unsigned values as negative singed values here. */
1612 min
= force_fit_type (TREE_TYPE (var
), wi::to_widest (min
), 0, false);
1613 max
= force_fit_type (TREE_TYPE (var
), wi::to_widest (max
), 0, false);
1615 /* We can transform a max, min range to an anti-range or
1616 vice-versa. Use set_and_canonicalize_value_range which does
1618 if (cond_code
== LE_EXPR
)
1619 set_and_canonicalize_value_range (vr_p
, VR_RANGE
,
1620 min
, max
, vr_p
->equiv
);
1621 else if (cond_code
== GT_EXPR
)
1622 set_and_canonicalize_value_range (vr_p
, VR_ANTI_RANGE
,
1623 min
, max
, vr_p
->equiv
);
1627 else if (cond_code
== EQ_EXPR
)
1629 enum value_range_type range_type
;
1633 range_type
= limit_vr
->type
;
1634 min
= limit_vr
->min
;
1635 max
= limit_vr
->max
;
1639 range_type
= VR_RANGE
;
1644 set_value_range (vr_p
, range_type
, min
, max
, vr_p
->equiv
);
1646 /* When asserting the equality VAR == LIMIT and LIMIT is another
1647 SSA name, the new range will also inherit the equivalence set
1649 if (TREE_CODE (limit
) == SSA_NAME
)
1650 add_equivalence (&vr_p
->equiv
, limit
);
1652 else if (cond_code
== NE_EXPR
)
1654 /* As described above, when LIMIT's range is an anti-range and
1655 this assertion is an inequality (NE_EXPR), then we cannot
1656 derive anything from the anti-range. For instance, if
1657 LIMIT's range was ~[0, 0], the assertion 'VAR != LIMIT' does
1658 not imply that VAR's range is [0, 0]. So, in the case of
1659 anti-ranges, we just assert the inequality using LIMIT and
1662 If LIMIT_VR is a range, we can only use it to build a new
1663 anti-range if LIMIT_VR is a single-valued range. For
1664 instance, if LIMIT_VR is [0, 1], the predicate
1665 VAR != [0, 1] does not mean that VAR's range is ~[0, 1].
1666 Rather, it means that for value 0 VAR should be ~[0, 0]
1667 and for value 1, VAR should be ~[1, 1]. We cannot
1668 represent these ranges.
1670 The only situation in which we can build a valid
1671 anti-range is when LIMIT_VR is a single-valued range
1672 (i.e., LIMIT_VR->MIN == LIMIT_VR->MAX). In that case,
1673 build the anti-range ~[LIMIT_VR->MIN, LIMIT_VR->MAX]. */
1675 && limit_vr
->type
== VR_RANGE
1676 && compare_values (limit_vr
->min
, limit_vr
->max
) == 0)
1678 min
= limit_vr
->min
;
1679 max
= limit_vr
->max
;
1683 /* In any other case, we cannot use LIMIT's range to build a
1684 valid anti-range. */
1688 /* If MIN and MAX cover the whole range for their type, then
1689 just use the original LIMIT. */
1690 if (INTEGRAL_TYPE_P (type
)
1691 && vrp_val_is_min (min
)
1692 && vrp_val_is_max (max
))
1695 set_and_canonicalize_value_range (vr_p
, VR_ANTI_RANGE
,
1696 min
, max
, vr_p
->equiv
);
1698 else if (cond_code
== LE_EXPR
|| cond_code
== LT_EXPR
)
1700 min
= TYPE_MIN_VALUE (type
);
1702 if (limit_vr
== NULL
|| limit_vr
->type
== VR_ANTI_RANGE
)
1706 /* If LIMIT_VR is of the form [N1, N2], we need to build the
1707 range [MIN, N2] for LE_EXPR and [MIN, N2 - 1] for
1709 max
= limit_vr
->max
;
1712 /* If the maximum value forces us to be out of bounds, simply punt.
1713 It would be pointless to try and do anything more since this
1714 all should be optimized away above us. */
1715 if ((cond_code
== LT_EXPR
1716 && compare_values (max
, min
) == 0)
1717 || is_overflow_infinity (max
))
1718 set_value_range_to_varying (vr_p
);
1721 /* For LT_EXPR, we create the range [MIN, MAX - 1]. */
1722 if (cond_code
== LT_EXPR
)
1724 if (TYPE_PRECISION (TREE_TYPE (max
)) == 1
1725 && !TYPE_UNSIGNED (TREE_TYPE (max
)))
1726 max
= fold_build2 (PLUS_EXPR
, TREE_TYPE (max
), max
,
1727 build_int_cst (TREE_TYPE (max
), -1));
1729 max
= fold_build2 (MINUS_EXPR
, TREE_TYPE (max
), max
,
1730 build_int_cst (TREE_TYPE (max
), 1));
1732 TREE_NO_WARNING (max
) = 1;
1735 set_value_range (vr_p
, VR_RANGE
, min
, max
, vr_p
->equiv
);
1738 else if (cond_code
== GE_EXPR
|| cond_code
== GT_EXPR
)
1740 max
= TYPE_MAX_VALUE (type
);
1742 if (limit_vr
== NULL
|| limit_vr
->type
== VR_ANTI_RANGE
)
1746 /* If LIMIT_VR is of the form [N1, N2], we need to build the
1747 range [N1, MAX] for GE_EXPR and [N1 + 1, MAX] for
1749 min
= limit_vr
->min
;
1752 /* If the minimum value forces us to be out of bounds, simply punt.
1753 It would be pointless to try and do anything more since this
1754 all should be optimized away above us. */
1755 if ((cond_code
== GT_EXPR
1756 && compare_values (min
, max
) == 0)
1757 || is_overflow_infinity (min
))
1758 set_value_range_to_varying (vr_p
);
1761 /* For GT_EXPR, we create the range [MIN + 1, MAX]. */
1762 if (cond_code
== GT_EXPR
)
1764 if (TYPE_PRECISION (TREE_TYPE (min
)) == 1
1765 && !TYPE_UNSIGNED (TREE_TYPE (min
)))
1766 min
= fold_build2 (MINUS_EXPR
, TREE_TYPE (min
), min
,
1767 build_int_cst (TREE_TYPE (min
), -1));
1769 min
= fold_build2 (PLUS_EXPR
, TREE_TYPE (min
), min
,
1770 build_int_cst (TREE_TYPE (min
), 1));
1772 TREE_NO_WARNING (min
) = 1;
1775 set_value_range (vr_p
, VR_RANGE
, min
, max
, vr_p
->equiv
);
1781 /* Finally intersect the new range with what we already know about var. */
1782 vrp_intersect_ranges (vr_p
, get_value_range (var
));
1786 /* Extract range information from SSA name VAR and store it in VR. If
1787 VAR has an interesting range, use it. Otherwise, create the
1788 range [VAR, VAR] and return it. This is useful in situations where
1789 we may have conditionals testing values of VARYING names. For
1796 Even if y_5 is deemed VARYING, we can determine that x_3 > y_5 is
1800 extract_range_from_ssa_name (value_range_t
*vr
, tree var
)
1802 value_range_t
*var_vr
= get_value_range (var
);
1804 if (var_vr
->type
!= VR_UNDEFINED
&& var_vr
->type
!= VR_VARYING
)
1805 copy_value_range (vr
, var_vr
);
1807 set_value_range (vr
, VR_RANGE
, var
, var
, NULL
);
1809 add_equivalence (&vr
->equiv
, var
);
1813 /* Wrapper around int_const_binop. If the operation overflows and we
1814 are not using wrapping arithmetic, then adjust the result to be
1815 -INF or +INF depending on CODE, VAL1 and VAL2. This can return
1816 NULL_TREE if we need to use an overflow infinity representation but
1817 the type does not support it. */
1820 vrp_int_const_binop (enum tree_code code
, tree val1
, tree val2
)
1824 res
= int_const_binop (code
, val1
, val2
);
1826 /* If we are using unsigned arithmetic, operate symbolically
1827 on -INF and +INF as int_const_binop only handles signed overflow. */
1828 if (TYPE_UNSIGNED (TREE_TYPE (val1
)))
1830 int checkz
= compare_values (res
, val1
);
1831 bool overflow
= false;
1833 /* Ensure that res = val1 [+*] val2 >= val1
1834 or that res = val1 - val2 <= val1. */
1835 if ((code
== PLUS_EXPR
1836 && !(checkz
== 1 || checkz
== 0))
1837 || (code
== MINUS_EXPR
1838 && !(checkz
== 0 || checkz
== -1)))
1842 /* Checking for multiplication overflow is done by dividing the
1843 output of the multiplication by the first input of the
1844 multiplication. If the result of that division operation is
1845 not equal to the second input of the multiplication, then the
1846 multiplication overflowed. */
1847 else if (code
== MULT_EXPR
&& !integer_zerop (val1
))
1849 tree tmp
= int_const_binop (TRUNC_DIV_EXPR
,
1852 int check
= compare_values (tmp
, val2
);
1860 res
= copy_node (res
);
1861 TREE_OVERFLOW (res
) = 1;
1865 else if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (val1
)))
1866 /* If the singed operation wraps then int_const_binop has done
1867 everything we want. */
1869 /* Signed division of -1/0 overflows and by the time it gets here
1870 returns NULL_TREE. */
1873 else if ((TREE_OVERFLOW (res
)
1874 && !TREE_OVERFLOW (val1
)
1875 && !TREE_OVERFLOW (val2
))
1876 || is_overflow_infinity (val1
)
1877 || is_overflow_infinity (val2
))
1879 /* If the operation overflowed but neither VAL1 nor VAL2 are
1880 overflown, return -INF or +INF depending on the operation
1881 and the combination of signs of the operands. */
1882 int sgn1
= tree_int_cst_sgn (val1
);
1883 int sgn2
= tree_int_cst_sgn (val2
);
1885 if (needs_overflow_infinity (TREE_TYPE (res
))
1886 && !supports_overflow_infinity (TREE_TYPE (res
)))
1889 /* We have to punt on adding infinities of different signs,
1890 since we can't tell what the sign of the result should be.
1891 Likewise for subtracting infinities of the same sign. */
1892 if (((code
== PLUS_EXPR
&& sgn1
!= sgn2
)
1893 || (code
== MINUS_EXPR
&& sgn1
== sgn2
))
1894 && is_overflow_infinity (val1
)
1895 && is_overflow_infinity (val2
))
1898 /* Don't try to handle division or shifting of infinities. */
1899 if ((code
== TRUNC_DIV_EXPR
1900 || code
== FLOOR_DIV_EXPR
1901 || code
== CEIL_DIV_EXPR
1902 || code
== EXACT_DIV_EXPR
1903 || code
== ROUND_DIV_EXPR
1904 || code
== RSHIFT_EXPR
)
1905 && (is_overflow_infinity (val1
)
1906 || is_overflow_infinity (val2
)))
1909 /* Notice that we only need to handle the restricted set of
1910 operations handled by extract_range_from_binary_expr.
1911 Among them, only multiplication, addition and subtraction
1912 can yield overflow without overflown operands because we
1913 are working with integral types only... except in the
1914 case VAL1 = -INF and VAL2 = -1 which overflows to +INF
1915 for division too. */
1917 /* For multiplication, the sign of the overflow is given
1918 by the comparison of the signs of the operands. */
1919 if ((code
== MULT_EXPR
&& sgn1
== sgn2
)
1920 /* For addition, the operands must be of the same sign
1921 to yield an overflow. Its sign is therefore that
1922 of one of the operands, for example the first. For
1923 infinite operands X + -INF is negative, not positive. */
1924 || (code
== PLUS_EXPR
1926 ? !is_negative_overflow_infinity (val2
)
1927 : is_positive_overflow_infinity (val2
)))
1928 /* For subtraction, non-infinite operands must be of
1929 different signs to yield an overflow. Its sign is
1930 therefore that of the first operand or the opposite of
1931 that of the second operand. A first operand of 0 counts
1932 as positive here, for the corner case 0 - (-INF), which
1933 overflows, but must yield +INF. For infinite operands 0
1934 - INF is negative, not positive. */
1935 || (code
== MINUS_EXPR
1937 ? !is_positive_overflow_infinity (val2
)
1938 : is_negative_overflow_infinity (val2
)))
1939 /* We only get in here with positive shift count, so the
1940 overflow direction is the same as the sign of val1.
1941 Actually rshift does not overflow at all, but we only
1942 handle the case of shifting overflowed -INF and +INF. */
1943 || (code
== RSHIFT_EXPR
1945 /* For division, the only case is -INF / -1 = +INF. */
1946 || code
== TRUNC_DIV_EXPR
1947 || code
== FLOOR_DIV_EXPR
1948 || code
== CEIL_DIV_EXPR
1949 || code
== EXACT_DIV_EXPR
1950 || code
== ROUND_DIV_EXPR
)
1951 return (needs_overflow_infinity (TREE_TYPE (res
))
1952 ? positive_overflow_infinity (TREE_TYPE (res
))
1953 : TYPE_MAX_VALUE (TREE_TYPE (res
)));
1955 return (needs_overflow_infinity (TREE_TYPE (res
))
1956 ? negative_overflow_infinity (TREE_TYPE (res
))
1957 : TYPE_MIN_VALUE (TREE_TYPE (res
)));
1964 /* For range VR compute two wide_int bitmasks. In *MAY_BE_NONZERO
1965 bitmask if some bit is unset, it means for all numbers in the range
1966 the bit is 0, otherwise it might be 0 or 1. In *MUST_BE_NONZERO
1967 bitmask if some bit is set, it means for all numbers in the range
1968 the bit is 1, otherwise it might be 0 or 1. */
1971 zero_nonzero_bits_from_vr (const tree expr_type
,
1973 wide_int
*may_be_nonzero
,
1974 wide_int
*must_be_nonzero
)
1976 *may_be_nonzero
= wi::minus_one (TYPE_PRECISION (expr_type
));
1977 *must_be_nonzero
= wi::zero (TYPE_PRECISION (expr_type
));
1978 if (!range_int_cst_p (vr
)
1979 || is_overflow_infinity (vr
->min
)
1980 || is_overflow_infinity (vr
->max
))
1983 if (range_int_cst_singleton_p (vr
))
1985 *may_be_nonzero
= vr
->min
;
1986 *must_be_nonzero
= *may_be_nonzero
;
1988 else if (tree_int_cst_sgn (vr
->min
) >= 0
1989 || tree_int_cst_sgn (vr
->max
) < 0)
1991 wide_int xor_mask
= wi::bit_xor (vr
->min
, vr
->max
);
1992 *may_be_nonzero
= wi::bit_or (vr
->min
, vr
->max
);
1993 *must_be_nonzero
= wi::bit_and (vr
->min
, vr
->max
);
1996 wide_int mask
= wi::mask (wi::floor_log2 (xor_mask
), false,
1997 (*may_be_nonzero
).get_precision ());
1998 *may_be_nonzero
= (*may_be_nonzero
) | mask
;
1999 *must_be_nonzero
= (*must_be_nonzero
).and_not (mask
);
2006 /* Create two value-ranges in *VR0 and *VR1 from the anti-range *AR
2007 so that *VR0 U *VR1 == *AR. Returns true if that is possible,
2008 false otherwise. If *AR can be represented with a single range
2009 *VR1 will be VR_UNDEFINED. */
2012 ranges_from_anti_range (value_range_t
*ar
,
2013 value_range_t
*vr0
, value_range_t
*vr1
)
2015 tree type
= TREE_TYPE (ar
->min
);
2017 vr0
->type
= VR_UNDEFINED
;
2018 vr1
->type
= VR_UNDEFINED
;
2020 if (ar
->type
!= VR_ANTI_RANGE
2021 || TREE_CODE (ar
->min
) != INTEGER_CST
2022 || TREE_CODE (ar
->max
) != INTEGER_CST
2023 || !vrp_val_min (type
)
2024 || !vrp_val_max (type
))
2027 if (!vrp_val_is_min (ar
->min
))
2029 vr0
->type
= VR_RANGE
;
2030 vr0
->min
= vrp_val_min (type
);
2032 = wide_int_to_tree (type
,
2033 wide_int (ar
->min
) - 1);
2035 if (!vrp_val_is_max (ar
->max
))
2037 vr1
->type
= VR_RANGE
;
2039 = wide_int_to_tree (type
,
2040 wide_int (ar
->max
) + 1);
2041 vr1
->max
= vrp_val_max (type
);
2043 if (vr0
->type
== VR_UNDEFINED
)
2046 vr1
->type
= VR_UNDEFINED
;
2049 return vr0
->type
!= VR_UNDEFINED
;
2052 /* Helper to extract a value-range *VR for a multiplicative operation
2056 extract_range_from_multiplicative_op_1 (value_range_t
*vr
,
2057 enum tree_code code
,
2058 value_range_t
*vr0
, value_range_t
*vr1
)
2060 enum value_range_type type
;
2067 /* Multiplications, divisions and shifts are a bit tricky to handle,
2068 depending on the mix of signs we have in the two ranges, we
2069 need to operate on different values to get the minimum and
2070 maximum values for the new range. One approach is to figure
2071 out all the variations of range combinations and do the
2074 However, this involves several calls to compare_values and it
2075 is pretty convoluted. It's simpler to do the 4 operations
2076 (MIN0 OP MIN1, MIN0 OP MAX1, MAX0 OP MIN1 and MAX0 OP MAX0 OP
2077 MAX1) and then figure the smallest and largest values to form
2079 gcc_assert (code
== MULT_EXPR
2080 || code
== TRUNC_DIV_EXPR
2081 || code
== FLOOR_DIV_EXPR
2082 || code
== CEIL_DIV_EXPR
2083 || code
== EXACT_DIV_EXPR
2084 || code
== ROUND_DIV_EXPR
2085 || code
== RSHIFT_EXPR
2086 || code
== LSHIFT_EXPR
);
2087 gcc_assert ((vr0
->type
== VR_RANGE
2088 || (code
== MULT_EXPR
&& vr0
->type
== VR_ANTI_RANGE
))
2089 && vr0
->type
== vr1
->type
);
2093 /* Compute the 4 cross operations. */
2095 val
[0] = vrp_int_const_binop (code
, vr0
->min
, vr1
->min
);
2096 if (val
[0] == NULL_TREE
)
2099 if (vr1
->max
== vr1
->min
)
2103 val
[1] = vrp_int_const_binop (code
, vr0
->min
, vr1
->max
);
2104 if (val
[1] == NULL_TREE
)
2108 if (vr0
->max
== vr0
->min
)
2112 val
[2] = vrp_int_const_binop (code
, vr0
->max
, vr1
->min
);
2113 if (val
[2] == NULL_TREE
)
2117 if (vr0
->min
== vr0
->max
|| vr1
->min
== vr1
->max
)
2121 val
[3] = vrp_int_const_binop (code
, vr0
->max
, vr1
->max
);
2122 if (val
[3] == NULL_TREE
)
2128 set_value_range_to_varying (vr
);
2132 /* Set MIN to the minimum of VAL[i] and MAX to the maximum
2136 for (i
= 1; i
< 4; i
++)
2138 if (!is_gimple_min_invariant (min
)
2139 || (TREE_OVERFLOW (min
) && !is_overflow_infinity (min
))
2140 || !is_gimple_min_invariant (max
)
2141 || (TREE_OVERFLOW (max
) && !is_overflow_infinity (max
)))
2146 if (!is_gimple_min_invariant (val
[i
])
2147 || (TREE_OVERFLOW (val
[i
])
2148 && !is_overflow_infinity (val
[i
])))
2150 /* If we found an overflowed value, set MIN and MAX
2151 to it so that we set the resulting range to
2157 if (compare_values (val
[i
], min
) == -1)
2160 if (compare_values (val
[i
], max
) == 1)
2165 /* If either MIN or MAX overflowed, then set the resulting range to
2166 VARYING. But we do accept an overflow infinity
2168 if (min
== NULL_TREE
2169 || !is_gimple_min_invariant (min
)
2170 || (TREE_OVERFLOW (min
) && !is_overflow_infinity (min
))
2172 || !is_gimple_min_invariant (max
)
2173 || (TREE_OVERFLOW (max
) && !is_overflow_infinity (max
)))
2175 set_value_range_to_varying (vr
);
2181 2) [-INF, +-INF(OVF)]
2182 3) [+-INF(OVF), +INF]
2183 4) [+-INF(OVF), +-INF(OVF)]
2184 We learn nothing when we have INF and INF(OVF) on both sides.
2185 Note that we do accept [-INF, -INF] and [+INF, +INF] without
2187 if ((vrp_val_is_min (min
) || is_overflow_infinity (min
))
2188 && (vrp_val_is_max (max
) || is_overflow_infinity (max
)))
2190 set_value_range_to_varying (vr
);
2194 cmp
= compare_values (min
, max
);
2195 if (cmp
== -2 || cmp
== 1)
2197 /* If the new range has its limits swapped around (MIN > MAX),
2198 then the operation caused one of them to wrap around, mark
2199 the new range VARYING. */
2200 set_value_range_to_varying (vr
);
2203 set_value_range (vr
, type
, min
, max
, NULL
);
2206 /* Extract range information from a binary operation CODE based on
2207 the ranges of each of its operands, *VR0 and *VR1 with resulting
2208 type EXPR_TYPE. The resulting range is stored in *VR. */
2211 extract_range_from_binary_expr_1 (value_range_t
*vr
,
2212 enum tree_code code
, tree expr_type
,
2213 value_range_t
*vr0_
, value_range_t
*vr1_
)
2215 value_range_t vr0
= *vr0_
, vr1
= *vr1_
;
2216 value_range_t vrtem0
= VR_INITIALIZER
, vrtem1
= VR_INITIALIZER
;
2217 enum value_range_type type
;
2218 tree min
= NULL_TREE
, max
= NULL_TREE
;
2221 if (!INTEGRAL_TYPE_P (expr_type
)
2222 && !POINTER_TYPE_P (expr_type
))
2224 set_value_range_to_varying (vr
);
2228 /* Not all binary expressions can be applied to ranges in a
2229 meaningful way. Handle only arithmetic operations. */
2230 if (code
!= PLUS_EXPR
2231 && code
!= MINUS_EXPR
2232 && code
!= POINTER_PLUS_EXPR
2233 && code
!= MULT_EXPR
2234 && code
!= TRUNC_DIV_EXPR
2235 && code
!= FLOOR_DIV_EXPR
2236 && code
!= CEIL_DIV_EXPR
2237 && code
!= EXACT_DIV_EXPR
2238 && code
!= ROUND_DIV_EXPR
2239 && code
!= TRUNC_MOD_EXPR
2240 && code
!= RSHIFT_EXPR
2241 && code
!= LSHIFT_EXPR
2244 && code
!= BIT_AND_EXPR
2245 && code
!= BIT_IOR_EXPR
2246 && code
!= BIT_XOR_EXPR
)
2248 set_value_range_to_varying (vr
);
2252 /* If both ranges are UNDEFINED, so is the result. */
2253 if (vr0
.type
== VR_UNDEFINED
&& vr1
.type
== VR_UNDEFINED
)
2255 set_value_range_to_undefined (vr
);
2258 /* If one of the ranges is UNDEFINED drop it to VARYING for the following
2259 code. At some point we may want to special-case operations that
2260 have UNDEFINED result for all or some value-ranges of the not UNDEFINED
2262 else if (vr0
.type
== VR_UNDEFINED
)
2263 set_value_range_to_varying (&vr0
);
2264 else if (vr1
.type
== VR_UNDEFINED
)
2265 set_value_range_to_varying (&vr1
);
2267 /* Now canonicalize anti-ranges to ranges when they are not symbolic
2268 and express ~[] op X as ([]' op X) U ([]'' op X). */
2269 if (vr0
.type
== VR_ANTI_RANGE
2270 && ranges_from_anti_range (&vr0
, &vrtem0
, &vrtem1
))
2272 extract_range_from_binary_expr_1 (vr
, code
, expr_type
, &vrtem0
, vr1_
);
2273 if (vrtem1
.type
!= VR_UNDEFINED
)
2275 value_range_t vrres
= VR_INITIALIZER
;
2276 extract_range_from_binary_expr_1 (&vrres
, code
, expr_type
,
2278 vrp_meet (vr
, &vrres
);
2282 /* Likewise for X op ~[]. */
2283 if (vr1
.type
== VR_ANTI_RANGE
2284 && ranges_from_anti_range (&vr1
, &vrtem0
, &vrtem1
))
2286 extract_range_from_binary_expr_1 (vr
, code
, expr_type
, vr0_
, &vrtem0
);
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
);
2297 /* The type of the resulting value range defaults to VR0.TYPE. */
2300 /* Refuse to operate on VARYING ranges, ranges of different kinds
2301 and symbolic ranges. As an exception, we allow BIT_AND_EXPR
2302 because we may be able to derive a useful range even if one of
2303 the operands is VR_VARYING or symbolic range. Similarly for
2304 divisions. TODO, we may be able to derive anti-ranges in
2306 if (code
!= BIT_AND_EXPR
2307 && code
!= BIT_IOR_EXPR
2308 && code
!= TRUNC_DIV_EXPR
2309 && code
!= FLOOR_DIV_EXPR
2310 && code
!= CEIL_DIV_EXPR
2311 && code
!= EXACT_DIV_EXPR
2312 && code
!= ROUND_DIV_EXPR
2313 && code
!= TRUNC_MOD_EXPR
2316 && (vr0
.type
== VR_VARYING
2317 || vr1
.type
== VR_VARYING
2318 || vr0
.type
!= vr1
.type
2319 || symbolic_range_p (&vr0
)
2320 || symbolic_range_p (&vr1
)))
2322 set_value_range_to_varying (vr
);
2326 /* Now evaluate the expression to determine the new range. */
2327 if (POINTER_TYPE_P (expr_type
))
2329 if (code
== MIN_EXPR
|| code
== MAX_EXPR
)
2331 /* For MIN/MAX expressions with pointers, we only care about
2332 nullness, if both are non null, then the result is nonnull.
2333 If both are null, then the result is null. Otherwise they
2335 if (range_is_nonnull (&vr0
) && range_is_nonnull (&vr1
))
2336 set_value_range_to_nonnull (vr
, expr_type
);
2337 else if (range_is_null (&vr0
) && range_is_null (&vr1
))
2338 set_value_range_to_null (vr
, expr_type
);
2340 set_value_range_to_varying (vr
);
2342 else if (code
== POINTER_PLUS_EXPR
)
2344 /* For pointer types, we are really only interested in asserting
2345 whether the expression evaluates to non-NULL. */
2346 if (range_is_nonnull (&vr0
) || range_is_nonnull (&vr1
))
2347 set_value_range_to_nonnull (vr
, expr_type
);
2348 else if (range_is_null (&vr0
) && range_is_null (&vr1
))
2349 set_value_range_to_null (vr
, expr_type
);
2351 set_value_range_to_varying (vr
);
2353 else if (code
== BIT_AND_EXPR
)
2355 /* For pointer types, we are really only interested in asserting
2356 whether the expression evaluates to non-NULL. */
2357 if (range_is_nonnull (&vr0
) && range_is_nonnull (&vr1
))
2358 set_value_range_to_nonnull (vr
, expr_type
);
2359 else if (range_is_null (&vr0
) || range_is_null (&vr1
))
2360 set_value_range_to_null (vr
, expr_type
);
2362 set_value_range_to_varying (vr
);
2365 set_value_range_to_varying (vr
);
2370 /* For integer ranges, apply the operation to each end of the
2371 range and see what we end up with. */
2372 if (code
== PLUS_EXPR
|| code
== MINUS_EXPR
)
2374 /* If we have a PLUS_EXPR with two VR_RANGE integer constant
2375 ranges compute the precise range for such case if possible. */
2376 if (range_int_cst_p (&vr0
)
2377 && range_int_cst_p (&vr1
))
2379 signop sgn
= TYPE_SIGN (expr_type
);
2380 unsigned int prec
= TYPE_PRECISION (expr_type
);
2381 wide_int type_min
= wi::min_value (TYPE_PRECISION (expr_type
), sgn
);
2382 wide_int type_max
= wi::max_value (TYPE_PRECISION (expr_type
), sgn
);
2383 wide_int wmin
, wmax
;
2387 if (code
== PLUS_EXPR
)
2389 wmin
= wi::add (vr0
.min
, vr1
.min
);
2390 wmax
= wi::add (vr0
.max
, vr1
.max
);
2392 /* Check for overflow. */
2393 if (wi::cmp (vr1
.min
, 0, sgn
) != wi::cmp (wmin
, vr0
.min
, sgn
))
2394 min_ovf
= wi::cmp (vr0
.min
, wmin
, sgn
);
2395 if (wi::cmp (vr1
.max
, 0, sgn
) != wi::cmp (wmax
, vr0
.max
, sgn
))
2396 max_ovf
= wi::cmp (vr0
.max
, wmax
, sgn
);
2398 else /* if (code == MINUS_EXPR) */
2400 wmin
= wi::sub (vr0
.min
, vr1
.max
);
2401 wmax
= wi::sub (vr0
.max
, vr1
.min
);
2403 if (wi::cmp (0, vr1
.max
, sgn
) != wi::cmp (wmin
, vr0
.min
, sgn
))
2404 min_ovf
= wi::cmp (vr0
.min
, vr1
.max
, sgn
);
2405 if (wi::cmp (0, vr1
.min
, sgn
) != wi::cmp (wmax
, vr0
.max
, sgn
))
2406 max_ovf
= wi::cmp (vr0
.max
, vr1
.min
, sgn
);
2409 /* For non-wrapping arithmetic look at possibly smaller
2410 value-ranges of the type. */
2411 if (!TYPE_OVERFLOW_WRAPS (expr_type
))
2413 if (vrp_val_min (expr_type
))
2414 type_min
= wide_int (vrp_val_min (expr_type
));
2415 if (vrp_val_max (expr_type
))
2416 type_max
= wide_int (vrp_val_max (expr_type
));
2419 /* Check for type overflow. */
2422 if (wi::cmp (wmin
, type_min
, sgn
) == -1)
2424 else if (wi::cmp (wmin
, type_max
, sgn
) == 1)
2429 if (wi::cmp (wmax
, type_min
, sgn
) == -1)
2431 else if (wi::cmp (wmax
, type_max
, sgn
) == 1)
2435 if (TYPE_OVERFLOW_WRAPS (expr_type
))
2437 /* If overflow wraps, truncate the values and adjust the
2438 range kind and bounds appropriately. */
2439 wide_int tmin
= wide_int::from (wmin
, prec
, sgn
);
2440 wide_int tmax
= wide_int::from (wmax
, prec
, sgn
);
2441 if (min_ovf
== max_ovf
)
2443 /* No overflow or both overflow or underflow. The
2444 range kind stays VR_RANGE. */
2445 min
= wide_int_to_tree (expr_type
, tmin
);
2446 max
= wide_int_to_tree (expr_type
, tmax
);
2448 else if (min_ovf
== -1
2451 /* Underflow and overflow, drop to VR_VARYING. */
2452 set_value_range_to_varying (vr
);
2457 /* Min underflow or max overflow. The range kind
2458 changes to VR_ANTI_RANGE. */
2459 bool covers
= false;
2460 wide_int tem
= tmin
;
2461 gcc_assert ((min_ovf
== -1 && max_ovf
== 0)
2462 || (max_ovf
== 1 && min_ovf
== 0));
2463 type
= VR_ANTI_RANGE
;
2465 if (wi::cmp (tmin
, tmax
, sgn
) < 0)
2468 if (wi::cmp (tmax
, tem
, sgn
) > 0)
2470 /* If the anti-range would cover nothing, drop to varying.
2471 Likewise if the anti-range bounds are outside of the
2473 if (covers
|| wi::cmp (tmin
, tmax
, sgn
) > 0)
2475 set_value_range_to_varying (vr
);
2478 min
= wide_int_to_tree (expr_type
, tmin
);
2479 max
= wide_int_to_tree (expr_type
, tmax
);
2484 /* If overflow does not wrap, saturate to the types min/max
2488 if (needs_overflow_infinity (expr_type
)
2489 && supports_overflow_infinity (expr_type
))
2490 min
= negative_overflow_infinity (expr_type
);
2492 min
= wide_int_to_tree (expr_type
, type_min
);
2494 else if (min_ovf
== 1)
2496 if (needs_overflow_infinity (expr_type
)
2497 && supports_overflow_infinity (expr_type
))
2498 min
= positive_overflow_infinity (expr_type
);
2500 min
= wide_int_to_tree (expr_type
, type_max
);
2503 min
= wide_int_to_tree (expr_type
, wmin
);
2507 if (needs_overflow_infinity (expr_type
)
2508 && supports_overflow_infinity (expr_type
))
2509 max
= negative_overflow_infinity (expr_type
);
2511 max
= wide_int_to_tree (expr_type
, type_min
);
2513 else if (max_ovf
== 1)
2515 if (needs_overflow_infinity (expr_type
)
2516 && supports_overflow_infinity (expr_type
))
2517 max
= positive_overflow_infinity (expr_type
);
2519 max
= wide_int_to_tree (expr_type
, type_max
);
2522 max
= wide_int_to_tree (expr_type
, wmax
);
2524 if (needs_overflow_infinity (expr_type
)
2525 && supports_overflow_infinity (expr_type
))
2527 if (is_negative_overflow_infinity (vr0
.min
)
2528 || (code
== PLUS_EXPR
2529 ? is_negative_overflow_infinity (vr1
.min
)
2530 : is_positive_overflow_infinity (vr1
.max
)))
2531 min
= negative_overflow_infinity (expr_type
);
2532 if (is_positive_overflow_infinity (vr0
.max
)
2533 || (code
== PLUS_EXPR
2534 ? is_positive_overflow_infinity (vr1
.max
)
2535 : is_negative_overflow_infinity (vr1
.min
)))
2536 max
= positive_overflow_infinity (expr_type
);
2541 /* For other cases, for example if we have a PLUS_EXPR with two
2542 VR_ANTI_RANGEs, drop to VR_VARYING. It would take more effort
2543 to compute a precise range for such a case.
2544 ??? General even mixed range kind operations can be expressed
2545 by for example transforming ~[3, 5] + [1, 2] to range-only
2546 operations and a union primitive:
2547 [-INF, 2] + [1, 2] U [5, +INF] + [1, 2]
2548 [-INF+1, 4] U [6, +INF(OVF)]
2549 though usually the union is not exactly representable with
2550 a single range or anti-range as the above is
2551 [-INF+1, +INF(OVF)] intersected with ~[5, 5]
2552 but one could use a scheme similar to equivalences for this. */
2553 set_value_range_to_varying (vr
);
2557 else if (code
== MIN_EXPR
2558 || code
== MAX_EXPR
)
2560 if (vr0
.type
== VR_RANGE
2561 && !symbolic_range_p (&vr0
))
2564 if (vr1
.type
== VR_RANGE
2565 && !symbolic_range_p (&vr1
))
2567 /* For operations that make the resulting range directly
2568 proportional to the original ranges, apply the operation to
2569 the same end of each range. */
2570 min
= vrp_int_const_binop (code
, vr0
.min
, vr1
.min
);
2571 max
= vrp_int_const_binop (code
, vr0
.max
, vr1
.max
);
2573 else if (code
== MIN_EXPR
)
2575 min
= vrp_val_min (expr_type
);
2578 else if (code
== MAX_EXPR
)
2581 max
= vrp_val_max (expr_type
);
2584 else if (vr1
.type
== VR_RANGE
2585 && !symbolic_range_p (&vr1
))
2588 if (code
== MIN_EXPR
)
2590 min
= vrp_val_min (expr_type
);
2593 else if (code
== MAX_EXPR
)
2596 max
= vrp_val_max (expr_type
);
2601 set_value_range_to_varying (vr
);
2605 else if (code
== MULT_EXPR
)
2607 /* Fancy code so that with unsigned, [-3,-1]*[-3,-1] does not
2608 drop to varying. This test requires 2*prec bits if both
2609 operands are signed and 2*prec + 2 bits if either is not. */
2611 signop sign
= TYPE_SIGN (expr_type
);
2612 unsigned int prec
= TYPE_PRECISION (expr_type
);
2613 unsigned int prec2
= (prec
* 2) + (sign
== UNSIGNED
? 2 : 0);
2615 if (range_int_cst_p (&vr0
)
2616 && range_int_cst_p (&vr1
)
2617 && TYPE_OVERFLOW_WRAPS (expr_type
))
2619 wide_int sizem1
= wi::mask (prec
, false, prec2
);
2620 wide_int size
= sizem1
+ 1;
2622 /* Extend the values using the sign of the result to PREC2.
2623 From here on out, everthing is just signed math no matter
2624 what the input types were. */
2625 wide_int min0
= wide_int::from (vr0
.min
, prec2
, sign
);
2626 wide_int max0
= wide_int::from (vr0
.max
, prec2
, sign
);
2627 wide_int min1
= wide_int::from (vr1
.min
, prec2
, sign
);
2628 wide_int max1
= wide_int::from (vr1
.max
, prec2
, sign
);
2630 /* Canonicalize the intervals. */
2631 if (sign
== UNSIGNED
)
2633 if (wi::ltu_p (size
, min0
+ max0
))
2639 if (wi::ltu_p (size
, min1
+ max1
))
2646 wide_int prod0
= min0
* min1
;
2647 wide_int prod1
= min0
* max1
;
2648 wide_int prod2
= max0
* min1
;
2649 wide_int prod3
= max0
* max1
;
2651 /* Sort the 4 products so that min is in prod0 and max is in
2653 /* min0min1 > max0max1 */
2654 if (wi::gts_p (prod0
, prod3
))
2656 wide_int tmp
= prod3
;
2661 /* min0max1 > max0min1 */
2662 if (wi::gts_p (prod1
, prod2
))
2664 wide_int tmp
= prod2
;
2669 if (wi::gts_p (prod0
, prod1
))
2671 wide_int tmp
= prod1
;
2676 if (wi::gts_p (prod2
, prod3
))
2678 wide_int tmp
= prod3
;
2683 /* diff = max - min. */
2684 prod2
= prod3
- prod0
;
2685 if (wi::geu_p (prod2
, sizem1
))
2687 /* the range covers all values. */
2688 set_value_range_to_varying (vr
);
2692 /* The following should handle the wrapping and selecting
2693 VR_ANTI_RANGE for us. */
2694 min
= wide_int_to_tree (expr_type
, prod0
);
2695 max
= wide_int_to_tree (expr_type
, prod3
);
2696 set_and_canonicalize_value_range (vr
, VR_RANGE
, min
, max
, NULL
);
2700 /* If we have an unsigned MULT_EXPR with two VR_ANTI_RANGEs,
2701 drop to VR_VARYING. It would take more effort to compute a
2702 precise range for such a case. For example, if we have
2703 op0 == 65536 and op1 == 65536 with their ranges both being
2704 ~[0,0] on a 32-bit machine, we would have op0 * op1 == 0, so
2705 we cannot claim that the product is in ~[0,0]. Note that we
2706 are guaranteed to have vr0.type == vr1.type at this
2708 if (vr0
.type
== VR_ANTI_RANGE
2709 && !TYPE_OVERFLOW_UNDEFINED (expr_type
))
2711 set_value_range_to_varying (vr
);
2715 extract_range_from_multiplicative_op_1 (vr
, code
, &vr0
, &vr1
);
2718 else if (code
== RSHIFT_EXPR
2719 || code
== LSHIFT_EXPR
)
2721 /* If we have a RSHIFT_EXPR with any shift values outside [0..prec-1],
2722 then drop to VR_VARYING. Outside of this range we get undefined
2723 behavior from the shift operation. We cannot even trust
2724 SHIFT_COUNT_TRUNCATED at this stage, because that applies to rtl
2725 shifts, and the operation at the tree level may be widened. */
2726 if (range_int_cst_p (&vr1
)
2727 && compare_tree_int (vr1
.min
, 0) >= 0
2728 && compare_tree_int (vr1
.max
, TYPE_PRECISION (expr_type
)) == -1)
2730 if (code
== RSHIFT_EXPR
)
2732 extract_range_from_multiplicative_op_1 (vr
, code
, &vr0
, &vr1
);
2735 /* We can map lshifts by constants to MULT_EXPR handling. */
2736 else if (code
== LSHIFT_EXPR
2737 && range_int_cst_singleton_p (&vr1
))
2739 bool saved_flag_wrapv
;
2740 value_range_t vr1p
= VR_INITIALIZER
;
2741 vr1p
.type
= VR_RANGE
;
2742 vr1p
.min
= (wide_int_to_tree
2744 wi::set_bit_in_zero (tree_to_shwi (vr1
.min
),
2745 TYPE_PRECISION (expr_type
))));
2746 vr1p
.max
= vr1p
.min
;
2747 /* We have to use a wrapping multiply though as signed overflow
2748 on lshifts is implementation defined in C89. */
2749 saved_flag_wrapv
= flag_wrapv
;
2751 extract_range_from_binary_expr_1 (vr
, MULT_EXPR
, expr_type
,
2753 flag_wrapv
= saved_flag_wrapv
;
2756 else if (code
== LSHIFT_EXPR
2757 && range_int_cst_p (&vr0
))
2759 int prec
= TYPE_PRECISION (expr_type
);
2760 int overflow_pos
= prec
;
2762 wide_int low_bound
, high_bound
;
2763 bool uns
= TYPE_UNSIGNED (expr_type
);
2764 bool in_bounds
= false;
2769 bound_shift
= overflow_pos
- tree_to_shwi (vr1
.max
);
2770 /* If bound_shift == HOST_BITS_PER_WIDE_INT, the llshift can
2771 overflow. However, for that to happen, vr1.max needs to be
2772 zero, which means vr1 is a singleton range of zero, which
2773 means it should be handled by the previous LSHIFT_EXPR
2775 wide_int bound
= wi::set_bit_in_zero (bound_shift
, prec
);
2776 wide_int complement
= ~(bound
- 1);
2781 high_bound
= complement
;
2782 if (wi::ltu_p (vr0
.max
, low_bound
))
2784 /* [5, 6] << [1, 2] == [10, 24]. */
2785 /* We're shifting out only zeroes, the value increases
2789 else if (wi::ltu_p (high_bound
, vr0
.min
))
2791 /* [0xffffff00, 0xffffffff] << [1, 2]
2792 == [0xfffffc00, 0xfffffffe]. */
2793 /* We're shifting out only ones, the value decreases
2800 /* [-1, 1] << [1, 2] == [-4, 4]. */
2801 low_bound
= complement
;
2803 if (wi::lts_p (vr0
.max
, high_bound
)
2804 && wi::lts_p (low_bound
, vr0
.min
))
2806 /* For non-negative numbers, we're shifting out only
2807 zeroes, the value increases monotonically.
2808 For negative numbers, we're shifting out only ones, the
2809 value decreases monotomically. */
2816 extract_range_from_multiplicative_op_1 (vr
, code
, &vr0
, &vr1
);
2821 set_value_range_to_varying (vr
);
2824 else if (code
== TRUNC_DIV_EXPR
2825 || code
== FLOOR_DIV_EXPR
2826 || code
== CEIL_DIV_EXPR
2827 || code
== EXACT_DIV_EXPR
2828 || code
== ROUND_DIV_EXPR
)
2830 if (vr0
.type
!= VR_RANGE
|| symbolic_range_p (&vr0
))
2832 /* For division, if op1 has VR_RANGE but op0 does not, something
2833 can be deduced just from that range. Say [min, max] / [4, max]
2834 gives [min / 4, max / 4] range. */
2835 if (vr1
.type
== VR_RANGE
2836 && !symbolic_range_p (&vr1
)
2837 && range_includes_zero_p (vr1
.min
, vr1
.max
) == 0)
2839 vr0
.type
= type
= VR_RANGE
;
2840 vr0
.min
= vrp_val_min (expr_type
);
2841 vr0
.max
= vrp_val_max (expr_type
);
2845 set_value_range_to_varying (vr
);
2850 /* For divisions, if flag_non_call_exceptions is true, we must
2851 not eliminate a division by zero. */
2852 if (cfun
->can_throw_non_call_exceptions
2853 && (vr1
.type
!= VR_RANGE
2854 || range_includes_zero_p (vr1
.min
, vr1
.max
) != 0))
2856 set_value_range_to_varying (vr
);
2860 /* For divisions, if op0 is VR_RANGE, we can deduce a range
2861 even if op1 is VR_VARYING, VR_ANTI_RANGE, symbolic or can
2863 if (vr0
.type
== VR_RANGE
2864 && (vr1
.type
!= VR_RANGE
2865 || range_includes_zero_p (vr1
.min
, vr1
.max
) != 0))
2867 tree zero
= build_int_cst (TREE_TYPE (vr0
.min
), 0);
2872 if (TYPE_UNSIGNED (expr_type
)
2873 || value_range_nonnegative_p (&vr1
))
2875 /* For unsigned division or when divisor is known
2876 to be non-negative, the range has to cover
2877 all numbers from 0 to max for positive max
2878 and all numbers from min to 0 for negative min. */
2879 cmp
= compare_values (vr0
.max
, zero
);
2882 else if (cmp
== 0 || cmp
== 1)
2886 cmp
= compare_values (vr0
.min
, zero
);
2889 else if (cmp
== 0 || cmp
== -1)
2896 /* Otherwise the range is -max .. max or min .. -min
2897 depending on which bound is bigger in absolute value,
2898 as the division can change the sign. */
2899 abs_extent_range (vr
, vr0
.min
, vr0
.max
);
2902 if (type
== VR_VARYING
)
2904 set_value_range_to_varying (vr
);
2910 extract_range_from_multiplicative_op_1 (vr
, code
, &vr0
, &vr1
);
2914 else if (code
== TRUNC_MOD_EXPR
)
2916 if (vr1
.type
!= VR_RANGE
2917 || range_includes_zero_p (vr1
.min
, vr1
.max
) != 0
2918 || vrp_val_is_min (vr1
.min
))
2920 set_value_range_to_varying (vr
);
2924 /* Compute MAX <|vr1.min|, |vr1.max|> - 1. */
2925 max
= fold_unary_to_constant (ABS_EXPR
, expr_type
, vr1
.min
);
2926 if (tree_int_cst_lt (max
, vr1
.max
))
2928 max
= int_const_binop (MINUS_EXPR
, max
, build_int_cst (TREE_TYPE (max
), 1));
2929 /* If the dividend is non-negative the modulus will be
2930 non-negative as well. */
2931 if (TYPE_UNSIGNED (expr_type
)
2932 || value_range_nonnegative_p (&vr0
))
2933 min
= build_int_cst (TREE_TYPE (max
), 0);
2935 min
= fold_unary_to_constant (NEGATE_EXPR
, expr_type
, max
);
2937 else if (code
== BIT_AND_EXPR
|| code
== BIT_IOR_EXPR
|| code
== BIT_XOR_EXPR
)
2939 bool int_cst_range0
, int_cst_range1
;
2940 wide_int may_be_nonzero0
, may_be_nonzero1
;
2941 wide_int must_be_nonzero0
, must_be_nonzero1
;
2943 int_cst_range0
= zero_nonzero_bits_from_vr (expr_type
, &vr0
,
2946 int_cst_range1
= zero_nonzero_bits_from_vr (expr_type
, &vr1
,
2951 if (code
== BIT_AND_EXPR
)
2953 min
= wide_int_to_tree (expr_type
,
2954 must_be_nonzero0
& must_be_nonzero1
);
2955 wide_int wmax
= may_be_nonzero0
& may_be_nonzero1
;
2956 /* If both input ranges contain only negative values we can
2957 truncate the result range maximum to the minimum of the
2958 input range maxima. */
2959 if (int_cst_range0
&& int_cst_range1
2960 && tree_int_cst_sgn (vr0
.max
) < 0
2961 && tree_int_cst_sgn (vr1
.max
) < 0)
2963 wmax
= wi::min (wmax
, vr0
.max
, TYPE_SIGN (expr_type
));
2964 wmax
= wi::min (wmax
, vr1
.max
, TYPE_SIGN (expr_type
));
2966 /* If either input range contains only non-negative values
2967 we can truncate the result range maximum to the respective
2968 maximum of the input range. */
2969 if (int_cst_range0
&& tree_int_cst_sgn (vr0
.min
) >= 0)
2970 wmax
= wi::min (wmax
, vr0
.max
, TYPE_SIGN (expr_type
));
2971 if (int_cst_range1
&& tree_int_cst_sgn (vr1
.min
) >= 0)
2972 wmax
= wi::min (wmax
, vr1
.max
, TYPE_SIGN (expr_type
));
2973 max
= wide_int_to_tree (expr_type
, wmax
);
2975 else if (code
== BIT_IOR_EXPR
)
2977 max
= wide_int_to_tree (expr_type
,
2978 may_be_nonzero0
| may_be_nonzero1
);
2979 wide_int wmin
= must_be_nonzero0
| must_be_nonzero1
;
2980 /* If the input ranges contain only positive values we can
2981 truncate the minimum of the result range to the maximum
2982 of the input range minima. */
2983 if (int_cst_range0
&& int_cst_range1
2984 && tree_int_cst_sgn (vr0
.min
) >= 0
2985 && tree_int_cst_sgn (vr1
.min
) >= 0)
2987 wmin
= wi::max (wmin
, vr0
.min
, TYPE_SIGN (expr_type
));
2988 wmin
= wi::max (wmin
, vr1
.min
, TYPE_SIGN (expr_type
));
2990 /* If either input range contains only negative values
2991 we can truncate the minimum of the result range to the
2992 respective minimum range. */
2993 if (int_cst_range0
&& tree_int_cst_sgn (vr0
.max
) < 0)
2994 wmin
= wi::max (wmin
, vr0
.min
, TYPE_SIGN (expr_type
));
2995 if (int_cst_range1
&& tree_int_cst_sgn (vr1
.max
) < 0)
2996 wmin
= wi::max (wmin
, vr1
.min
, TYPE_SIGN (expr_type
));
2997 min
= wide_int_to_tree (expr_type
, wmin
);
2999 else if (code
== BIT_XOR_EXPR
)
3001 wide_int result_zero_bits
= ((must_be_nonzero0
& must_be_nonzero1
)
3002 | ~(may_be_nonzero0
| may_be_nonzero1
));
3003 wide_int result_one_bits
3004 = (must_be_nonzero0
.and_not (may_be_nonzero1
)
3005 | must_be_nonzero1
.and_not (may_be_nonzero0
));
3006 max
= wide_int_to_tree (expr_type
, ~result_zero_bits
);
3007 min
= wide_int_to_tree (expr_type
, result_one_bits
);
3008 /* If the range has all positive or all negative values the
3009 result is better than VARYING. */
3010 if (tree_int_cst_sgn (min
) < 0
3011 || tree_int_cst_sgn (max
) >= 0)
3014 max
= min
= NULL_TREE
;
3020 /* If either MIN or MAX overflowed, then set the resulting range to
3021 VARYING. But we do accept an overflow infinity
3023 if (min
== NULL_TREE
3024 || !is_gimple_min_invariant (min
)
3025 || (TREE_OVERFLOW (min
) && !is_overflow_infinity (min
))
3027 || !is_gimple_min_invariant (max
)
3028 || (TREE_OVERFLOW (max
) && !is_overflow_infinity (max
)))
3030 set_value_range_to_varying (vr
);
3036 2) [-INF, +-INF(OVF)]
3037 3) [+-INF(OVF), +INF]
3038 4) [+-INF(OVF), +-INF(OVF)]
3039 We learn nothing when we have INF and INF(OVF) on both sides.
3040 Note that we do accept [-INF, -INF] and [+INF, +INF] without
3042 if ((vrp_val_is_min (min
) || is_overflow_infinity (min
))
3043 && (vrp_val_is_max (max
) || is_overflow_infinity (max
)))
3045 set_value_range_to_varying (vr
);
3049 cmp
= compare_values (min
, max
);
3050 if (cmp
== -2 || cmp
== 1)
3052 /* If the new range has its limits swapped around (MIN > MAX),
3053 then the operation caused one of them to wrap around, mark
3054 the new range VARYING. */
3055 set_value_range_to_varying (vr
);
3058 set_value_range (vr
, type
, min
, max
, NULL
);
3061 /* Extract range information from a binary expression OP0 CODE OP1 based on
3062 the ranges of each of its operands with resulting type EXPR_TYPE.
3063 The resulting range is stored in *VR. */
3066 extract_range_from_binary_expr (value_range_t
*vr
,
3067 enum tree_code code
,
3068 tree expr_type
, tree op0
, tree op1
)
3070 value_range_t vr0
= VR_INITIALIZER
;
3071 value_range_t vr1
= VR_INITIALIZER
;
3073 /* Get value ranges for each operand. For constant operands, create
3074 a new value range with the operand to simplify processing. */
3075 if (TREE_CODE (op0
) == SSA_NAME
)
3076 vr0
= *(get_value_range (op0
));
3077 else if (is_gimple_min_invariant (op0
))
3078 set_value_range_to_value (&vr0
, op0
, NULL
);
3080 set_value_range_to_varying (&vr0
);
3082 if (TREE_CODE (op1
) == SSA_NAME
)
3083 vr1
= *(get_value_range (op1
));
3084 else if (is_gimple_min_invariant (op1
))
3085 set_value_range_to_value (&vr1
, op1
, NULL
);
3087 set_value_range_to_varying (&vr1
);
3089 extract_range_from_binary_expr_1 (vr
, code
, expr_type
, &vr0
, &vr1
);
3092 /* Extract range information from a unary operation CODE based on
3093 the range of its operand *VR0 with type OP0_TYPE with resulting type TYPE.
3094 The The resulting range is stored in *VR. */
3097 extract_range_from_unary_expr_1 (value_range_t
*vr
,
3098 enum tree_code code
, tree type
,
3099 value_range_t
*vr0_
, tree op0_type
)
3101 value_range_t vr0
= *vr0_
, vrtem0
= VR_INITIALIZER
, vrtem1
= VR_INITIALIZER
;
3103 /* VRP only operates on integral and pointer types. */
3104 if (!(INTEGRAL_TYPE_P (op0_type
)
3105 || POINTER_TYPE_P (op0_type
))
3106 || !(INTEGRAL_TYPE_P (type
)
3107 || POINTER_TYPE_P (type
)))
3109 set_value_range_to_varying (vr
);
3113 /* If VR0 is UNDEFINED, so is the result. */
3114 if (vr0
.type
== VR_UNDEFINED
)
3116 set_value_range_to_undefined (vr
);
3120 /* Handle operations that we express in terms of others. */
3121 if (code
== PAREN_EXPR
)
3123 /* PAREN_EXPR is a simple copy. */
3124 copy_value_range (vr
, &vr0
);
3127 else if (code
== NEGATE_EXPR
)
3129 /* -X is simply 0 - X, so re-use existing code that also handles
3130 anti-ranges fine. */
3131 value_range_t zero
= VR_INITIALIZER
;
3132 set_value_range_to_value (&zero
, build_int_cst (type
, 0), NULL
);
3133 extract_range_from_binary_expr_1 (vr
, MINUS_EXPR
, type
, &zero
, &vr0
);
3136 else if (code
== BIT_NOT_EXPR
)
3138 /* ~X is simply -1 - X, so re-use existing code that also handles
3139 anti-ranges fine. */
3140 value_range_t minusone
= VR_INITIALIZER
;
3141 set_value_range_to_value (&minusone
, build_int_cst (type
, -1), NULL
);
3142 extract_range_from_binary_expr_1 (vr
, MINUS_EXPR
,
3143 type
, &minusone
, &vr0
);
3147 /* Now canonicalize anti-ranges to ranges when they are not symbolic
3148 and express op ~[] as (op []') U (op []''). */
3149 if (vr0
.type
== VR_ANTI_RANGE
3150 && ranges_from_anti_range (&vr0
, &vrtem0
, &vrtem1
))
3152 extract_range_from_unary_expr_1 (vr
, code
, type
, &vrtem0
, op0_type
);
3153 if (vrtem1
.type
!= VR_UNDEFINED
)
3155 value_range_t vrres
= VR_INITIALIZER
;
3156 extract_range_from_unary_expr_1 (&vrres
, code
, type
,
3158 vrp_meet (vr
, &vrres
);
3163 if (CONVERT_EXPR_CODE_P (code
))
3165 tree inner_type
= op0_type
;
3166 tree outer_type
= type
;
3168 /* If the expression evaluates to a pointer, we are only interested in
3169 determining if it evaluates to NULL [0, 0] or non-NULL (~[0, 0]). */
3170 if (POINTER_TYPE_P (type
))
3172 if (range_is_nonnull (&vr0
))
3173 set_value_range_to_nonnull (vr
, type
);
3174 else if (range_is_null (&vr0
))
3175 set_value_range_to_null (vr
, type
);
3177 set_value_range_to_varying (vr
);
3181 /* If VR0 is varying and we increase the type precision, assume
3182 a full range for the following transformation. */
3183 if (vr0
.type
== VR_VARYING
3184 && INTEGRAL_TYPE_P (inner_type
)
3185 && TYPE_PRECISION (inner_type
) < TYPE_PRECISION (outer_type
))
3187 vr0
.type
= VR_RANGE
;
3188 vr0
.min
= TYPE_MIN_VALUE (inner_type
);
3189 vr0
.max
= TYPE_MAX_VALUE (inner_type
);
3192 /* If VR0 is a constant range or anti-range and the conversion is
3193 not truncating we can convert the min and max values and
3194 canonicalize the resulting range. Otherwise we can do the
3195 conversion if the size of the range is less than what the
3196 precision of the target type can represent and the range is
3197 not an anti-range. */
3198 if ((vr0
.type
== VR_RANGE
3199 || vr0
.type
== VR_ANTI_RANGE
)
3200 && TREE_CODE (vr0
.min
) == INTEGER_CST
3201 && TREE_CODE (vr0
.max
) == INTEGER_CST
3202 && (!is_overflow_infinity (vr0
.min
)
3203 || (vr0
.type
== VR_RANGE
3204 && TYPE_PRECISION (outer_type
) > TYPE_PRECISION (inner_type
)
3205 && needs_overflow_infinity (outer_type
)
3206 && supports_overflow_infinity (outer_type
)))
3207 && (!is_overflow_infinity (vr0
.max
)
3208 || (vr0
.type
== VR_RANGE
3209 && TYPE_PRECISION (outer_type
) > TYPE_PRECISION (inner_type
)
3210 && needs_overflow_infinity (outer_type
)
3211 && supports_overflow_infinity (outer_type
)))
3212 && (TYPE_PRECISION (outer_type
) >= TYPE_PRECISION (inner_type
)
3213 || (vr0
.type
== VR_RANGE
3214 && integer_zerop (int_const_binop (RSHIFT_EXPR
,
3215 int_const_binop (MINUS_EXPR
, vr0
.max
, vr0
.min
),
3216 size_int (TYPE_PRECISION (outer_type
)))))))
3218 tree new_min
, new_max
;
3219 if (is_overflow_infinity (vr0
.min
))
3220 new_min
= negative_overflow_infinity (outer_type
);
3222 new_min
= force_fit_type (outer_type
, wi::to_widest (vr0
.min
),
3224 if (is_overflow_infinity (vr0
.max
))
3225 new_max
= positive_overflow_infinity (outer_type
);
3227 new_max
= force_fit_type (outer_type
, wi::to_widest (vr0
.max
),
3229 set_and_canonicalize_value_range (vr
, vr0
.type
,
3230 new_min
, new_max
, NULL
);
3234 set_value_range_to_varying (vr
);
3237 else if (code
== ABS_EXPR
)
3242 /* Pass through vr0 in the easy cases. */
3243 if (TYPE_UNSIGNED (type
)
3244 || value_range_nonnegative_p (&vr0
))
3246 copy_value_range (vr
, &vr0
);
3250 /* For the remaining varying or symbolic ranges we can't do anything
3252 if (vr0
.type
== VR_VARYING
3253 || symbolic_range_p (&vr0
))
3255 set_value_range_to_varying (vr
);
3259 /* -TYPE_MIN_VALUE = TYPE_MIN_VALUE with flag_wrapv so we can't get a
3261 if (!TYPE_OVERFLOW_UNDEFINED (type
)
3262 && ((vr0
.type
== VR_RANGE
3263 && vrp_val_is_min (vr0
.min
))
3264 || (vr0
.type
== VR_ANTI_RANGE
3265 && !vrp_val_is_min (vr0
.min
))))
3267 set_value_range_to_varying (vr
);
3271 /* ABS_EXPR may flip the range around, if the original range
3272 included negative values. */
3273 if (is_overflow_infinity (vr0
.min
))
3274 min
= positive_overflow_infinity (type
);
3275 else if (!vrp_val_is_min (vr0
.min
))
3276 min
= fold_unary_to_constant (code
, type
, vr0
.min
);
3277 else if (!needs_overflow_infinity (type
))
3278 min
= TYPE_MAX_VALUE (type
);
3279 else if (supports_overflow_infinity (type
))
3280 min
= positive_overflow_infinity (type
);
3283 set_value_range_to_varying (vr
);
3287 if (is_overflow_infinity (vr0
.max
))
3288 max
= positive_overflow_infinity (type
);
3289 else if (!vrp_val_is_min (vr0
.max
))
3290 max
= fold_unary_to_constant (code
, type
, vr0
.max
);
3291 else if (!needs_overflow_infinity (type
))
3292 max
= TYPE_MAX_VALUE (type
);
3293 else if (supports_overflow_infinity (type
)
3294 /* We shouldn't generate [+INF, +INF] as set_value_range
3295 doesn't like this and ICEs. */
3296 && !is_positive_overflow_infinity (min
))
3297 max
= positive_overflow_infinity (type
);
3300 set_value_range_to_varying (vr
);
3304 cmp
= compare_values (min
, max
);
3306 /* If a VR_ANTI_RANGEs contains zero, then we have
3307 ~[-INF, min(MIN, MAX)]. */
3308 if (vr0
.type
== VR_ANTI_RANGE
)
3310 if (range_includes_zero_p (vr0
.min
, vr0
.max
) == 1)
3312 /* Take the lower of the two values. */
3316 /* Create ~[-INF, min (abs(MIN), abs(MAX))]
3317 or ~[-INF + 1, min (abs(MIN), abs(MAX))] when
3318 flag_wrapv is set and the original anti-range doesn't include
3319 TYPE_MIN_VALUE, remember -TYPE_MIN_VALUE = TYPE_MIN_VALUE. */
3320 if (TYPE_OVERFLOW_WRAPS (type
))
3322 tree type_min_value
= TYPE_MIN_VALUE (type
);
3324 min
= (vr0
.min
!= type_min_value
3325 ? int_const_binop (PLUS_EXPR
, type_min_value
,
3326 build_int_cst (TREE_TYPE (type_min_value
), 1))
3331 if (overflow_infinity_range_p (&vr0
))
3332 min
= negative_overflow_infinity (type
);
3334 min
= TYPE_MIN_VALUE (type
);
3339 /* All else has failed, so create the range [0, INF], even for
3340 flag_wrapv since TYPE_MIN_VALUE is in the original
3342 vr0
.type
= VR_RANGE
;
3343 min
= build_int_cst (type
, 0);
3344 if (needs_overflow_infinity (type
))
3346 if (supports_overflow_infinity (type
))
3347 max
= positive_overflow_infinity (type
);
3350 set_value_range_to_varying (vr
);
3355 max
= TYPE_MAX_VALUE (type
);
3359 /* If the range contains zero then we know that the minimum value in the
3360 range will be zero. */
3361 else if (range_includes_zero_p (vr0
.min
, vr0
.max
) == 1)
3365 min
= build_int_cst (type
, 0);
3369 /* If the range was reversed, swap MIN and MAX. */
3378 cmp
= compare_values (min
, max
);
3379 if (cmp
== -2 || cmp
== 1)
3381 /* If the new range has its limits swapped around (MIN > MAX),
3382 then the operation caused one of them to wrap around, mark
3383 the new range VARYING. */
3384 set_value_range_to_varying (vr
);
3387 set_value_range (vr
, vr0
.type
, min
, max
, NULL
);
3391 /* For unhandled operations fall back to varying. */
3392 set_value_range_to_varying (vr
);
3397 /* Extract range information from a unary expression CODE OP0 based on
3398 the range of its operand with resulting type TYPE.
3399 The resulting range is stored in *VR. */
3402 extract_range_from_unary_expr (value_range_t
*vr
, enum tree_code code
,
3403 tree type
, tree op0
)
3405 value_range_t vr0
= VR_INITIALIZER
;
3407 /* Get value ranges for the operand. For constant operands, create
3408 a new value range with the operand to simplify processing. */
3409 if (TREE_CODE (op0
) == SSA_NAME
)
3410 vr0
= *(get_value_range (op0
));
3411 else if (is_gimple_min_invariant (op0
))
3412 set_value_range_to_value (&vr0
, op0
, NULL
);
3414 set_value_range_to_varying (&vr0
);
3416 extract_range_from_unary_expr_1 (vr
, code
, type
, &vr0
, TREE_TYPE (op0
));
3420 /* Extract range information from a conditional expression STMT based on
3421 the ranges of each of its operands and the expression code. */
3424 extract_range_from_cond_expr (value_range_t
*vr
, gimple stmt
)
3427 value_range_t vr0
= VR_INITIALIZER
;
3428 value_range_t vr1
= VR_INITIALIZER
;
3430 /* Get value ranges for each operand. For constant operands, create
3431 a new value range with the operand to simplify processing. */
3432 op0
= gimple_assign_rhs2 (stmt
);
3433 if (TREE_CODE (op0
) == SSA_NAME
)
3434 vr0
= *(get_value_range (op0
));
3435 else if (is_gimple_min_invariant (op0
))
3436 set_value_range_to_value (&vr0
, op0
, NULL
);
3438 set_value_range_to_varying (&vr0
);
3440 op1
= gimple_assign_rhs3 (stmt
);
3441 if (TREE_CODE (op1
) == SSA_NAME
)
3442 vr1
= *(get_value_range (op1
));
3443 else if (is_gimple_min_invariant (op1
))
3444 set_value_range_to_value (&vr1
, op1
, NULL
);
3446 set_value_range_to_varying (&vr1
);
3448 /* The resulting value range is the union of the operand ranges */
3449 copy_value_range (vr
, &vr0
);
3450 vrp_meet (vr
, &vr1
);
3454 /* Extract range information from a comparison expression EXPR based
3455 on the range of its operand and the expression code. */
3458 extract_range_from_comparison (value_range_t
*vr
, enum tree_code code
,
3459 tree type
, tree op0
, tree op1
)
3464 val
= vrp_evaluate_conditional_warnv_with_ops (code
, op0
, op1
, false, &sop
,
3467 /* A disadvantage of using a special infinity as an overflow
3468 representation is that we lose the ability to record overflow
3469 when we don't have an infinity. So we have to ignore a result
3470 which relies on overflow. */
3472 if (val
&& !is_overflow_infinity (val
) && !sop
)
3474 /* Since this expression was found on the RHS of an assignment,
3475 its type may be different from _Bool. Convert VAL to EXPR's
3477 val
= fold_convert (type
, val
);
3478 if (is_gimple_min_invariant (val
))
3479 set_value_range_to_value (vr
, val
, vr
->equiv
);
3481 set_value_range (vr
, VR_RANGE
, val
, val
, vr
->equiv
);
3484 /* The result of a comparison is always true or false. */
3485 set_value_range_to_truthvalue (vr
, type
);
3488 /* Try to derive a nonnegative or nonzero range out of STMT relying
3489 primarily on generic routines in fold in conjunction with range data.
3490 Store the result in *VR */
3493 extract_range_basic (value_range_t
*vr
, gimple stmt
)
3496 tree type
= gimple_expr_type (stmt
);
3498 if (gimple_call_builtin_p (stmt
, BUILT_IN_NORMAL
))
3500 tree fndecl
= gimple_call_fndecl (stmt
), arg
;
3501 int mini
, maxi
, zerov
= 0, prec
;
3503 switch (DECL_FUNCTION_CODE (fndecl
))
3505 case BUILT_IN_CONSTANT_P
:
3506 /* If the call is __builtin_constant_p and the argument is a
3507 function parameter resolve it to false. This avoids bogus
3508 array bound warnings.
3509 ??? We could do this as early as inlining is finished. */
3510 arg
= gimple_call_arg (stmt
, 0);
3511 if (TREE_CODE (arg
) == SSA_NAME
3512 && SSA_NAME_IS_DEFAULT_DEF (arg
)
3513 && TREE_CODE (SSA_NAME_VAR (arg
)) == PARM_DECL
)
3515 set_value_range_to_null (vr
, type
);
3519 /* Both __builtin_ffs* and __builtin_popcount return
3521 CASE_INT_FN (BUILT_IN_FFS
):
3522 CASE_INT_FN (BUILT_IN_POPCOUNT
):
3523 arg
= gimple_call_arg (stmt
, 0);
3524 prec
= TYPE_PRECISION (TREE_TYPE (arg
));
3527 if (TREE_CODE (arg
) == SSA_NAME
)
3529 value_range_t
*vr0
= get_value_range (arg
);
3530 /* If arg is non-zero, then ffs or popcount
3532 if (((vr0
->type
== VR_RANGE
3533 && integer_nonzerop (vr0
->min
))
3534 || (vr0
->type
== VR_ANTI_RANGE
3535 && integer_zerop (vr0
->min
)))
3536 && !is_overflow_infinity (vr0
->min
))
3538 /* If some high bits are known to be zero,
3539 we can decrease the maximum. */
3540 if (vr0
->type
== VR_RANGE
3541 && TREE_CODE (vr0
->max
) == INTEGER_CST
3542 && !is_overflow_infinity (vr0
->max
))
3543 maxi
= tree_floor_log2 (vr0
->max
) + 1;
3546 /* __builtin_parity* returns [0, 1]. */
3547 CASE_INT_FN (BUILT_IN_PARITY
):
3551 /* __builtin_c[lt]z* return [0, prec-1], except for
3552 when the argument is 0, but that is undefined behavior.
3553 On many targets where the CLZ RTL or optab value is defined
3554 for 0 the value is prec, so include that in the range
3556 CASE_INT_FN (BUILT_IN_CLZ
):
3557 arg
= gimple_call_arg (stmt
, 0);
3558 prec
= TYPE_PRECISION (TREE_TYPE (arg
));
3561 if (optab_handler (clz_optab
, TYPE_MODE (TREE_TYPE (arg
)))
3563 && CLZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (TREE_TYPE (arg
)),
3565 /* Handle only the single common value. */
3567 /* Magic value to give up, unless vr0 proves
3570 if (TREE_CODE (arg
) == SSA_NAME
)
3572 value_range_t
*vr0
= get_value_range (arg
);
3573 /* From clz of VR_RANGE minimum we can compute
3575 if (vr0
->type
== VR_RANGE
3576 && TREE_CODE (vr0
->min
) == INTEGER_CST
3577 && !is_overflow_infinity (vr0
->min
))
3579 maxi
= prec
- 1 - tree_floor_log2 (vr0
->min
);
3583 else if (vr0
->type
== VR_ANTI_RANGE
3584 && integer_zerop (vr0
->min
)
3585 && !is_overflow_infinity (vr0
->min
))
3592 /* From clz of VR_RANGE maximum we can compute
3594 if (vr0
->type
== VR_RANGE
3595 && TREE_CODE (vr0
->max
) == INTEGER_CST
3596 && !is_overflow_infinity (vr0
->max
))
3598 mini
= prec
- 1 - tree_floor_log2 (vr0
->max
);
3606 /* __builtin_ctz* return [0, prec-1], except for
3607 when the argument is 0, but that is undefined behavior.
3608 If there is a ctz optab for this mode and
3609 CTZ_DEFINED_VALUE_AT_ZERO, include that in the range,
3610 otherwise just assume 0 won't be seen. */
3611 CASE_INT_FN (BUILT_IN_CTZ
):
3612 arg
= gimple_call_arg (stmt
, 0);
3613 prec
= TYPE_PRECISION (TREE_TYPE (arg
));
3616 if (optab_handler (ctz_optab
, TYPE_MODE (TREE_TYPE (arg
)))
3618 && CTZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (TREE_TYPE (arg
)),
3621 /* Handle only the two common values. */
3624 else if (zerov
== prec
)
3627 /* Magic value to give up, unless vr0 proves
3631 if (TREE_CODE (arg
) == SSA_NAME
)
3633 value_range_t
*vr0
= get_value_range (arg
);
3634 /* If arg is non-zero, then use [0, prec - 1]. */
3635 if (((vr0
->type
== VR_RANGE
3636 && integer_nonzerop (vr0
->min
))
3637 || (vr0
->type
== VR_ANTI_RANGE
3638 && integer_zerop (vr0
->min
)))
3639 && !is_overflow_infinity (vr0
->min
))
3644 /* If some high bits are known to be zero,
3645 we can decrease the result maximum. */
3646 if (vr0
->type
== VR_RANGE
3647 && TREE_CODE (vr0
->max
) == INTEGER_CST
3648 && !is_overflow_infinity (vr0
->max
))
3650 maxi
= tree_floor_log2 (vr0
->max
);
3651 /* For vr0 [0, 0] give up. */
3659 /* __builtin_clrsb* returns [0, prec-1]. */
3660 CASE_INT_FN (BUILT_IN_CLRSB
):
3661 arg
= gimple_call_arg (stmt
, 0);
3662 prec
= TYPE_PRECISION (TREE_TYPE (arg
));
3667 set_value_range (vr
, VR_RANGE
, build_int_cst (type
, mini
),
3668 build_int_cst (type
, maxi
), NULL
);
3674 if (INTEGRAL_TYPE_P (type
)
3675 && gimple_stmt_nonnegative_warnv_p (stmt
, &sop
))
3676 set_value_range_to_nonnegative (vr
, type
,
3677 sop
|| stmt_overflow_infinity (stmt
));
3678 else if (vrp_stmt_computes_nonzero (stmt
, &sop
)
3680 set_value_range_to_nonnull (vr
, type
);
3682 set_value_range_to_varying (vr
);
3686 /* Try to compute a useful range out of assignment STMT and store it
3690 extract_range_from_assignment (value_range_t
*vr
, gimple stmt
)
3692 enum tree_code code
= gimple_assign_rhs_code (stmt
);
3694 if (code
== ASSERT_EXPR
)
3695 extract_range_from_assert (vr
, gimple_assign_rhs1 (stmt
));
3696 else if (code
== SSA_NAME
)
3697 extract_range_from_ssa_name (vr
, gimple_assign_rhs1 (stmt
));
3698 else if (TREE_CODE_CLASS (code
) == tcc_binary
)
3699 extract_range_from_binary_expr (vr
, gimple_assign_rhs_code (stmt
),
3700 gimple_expr_type (stmt
),
3701 gimple_assign_rhs1 (stmt
),
3702 gimple_assign_rhs2 (stmt
));
3703 else if (TREE_CODE_CLASS (code
) == tcc_unary
)
3704 extract_range_from_unary_expr (vr
, gimple_assign_rhs_code (stmt
),
3705 gimple_expr_type (stmt
),
3706 gimple_assign_rhs1 (stmt
));
3707 else if (code
== COND_EXPR
)
3708 extract_range_from_cond_expr (vr
, stmt
);
3709 else if (TREE_CODE_CLASS (code
) == tcc_comparison
)
3710 extract_range_from_comparison (vr
, gimple_assign_rhs_code (stmt
),
3711 gimple_expr_type (stmt
),
3712 gimple_assign_rhs1 (stmt
),
3713 gimple_assign_rhs2 (stmt
));
3714 else if (get_gimple_rhs_class (code
) == GIMPLE_SINGLE_RHS
3715 && is_gimple_min_invariant (gimple_assign_rhs1 (stmt
)))
3716 set_value_range_to_value (vr
, gimple_assign_rhs1 (stmt
), NULL
);
3718 set_value_range_to_varying (vr
);
3720 if (vr
->type
== VR_VARYING
)
3721 extract_range_basic (vr
, stmt
);
3724 /* Given a range VR, a LOOP and a variable VAR, determine whether it
3725 would be profitable to adjust VR using scalar evolution information
3726 for VAR. If so, update VR with the new limits. */
3729 adjust_range_with_scev (value_range_t
*vr
, struct loop
*loop
,
3730 gimple stmt
, tree var
)
3732 tree init
, step
, chrec
, tmin
, tmax
, min
, max
, type
, tem
;
3733 enum ev_direction dir
;
3735 /* TODO. Don't adjust anti-ranges. An anti-range may provide
3736 better opportunities than a regular range, but I'm not sure. */
3737 if (vr
->type
== VR_ANTI_RANGE
)
3740 chrec
= instantiate_parameters (loop
, analyze_scalar_evolution (loop
, var
));
3742 /* Like in PR19590, scev can return a constant function. */
3743 if (is_gimple_min_invariant (chrec
))
3745 set_value_range_to_value (vr
, chrec
, vr
->equiv
);
3749 if (TREE_CODE (chrec
) != POLYNOMIAL_CHREC
)
3752 init
= initial_condition_in_loop_num (chrec
, loop
->num
);
3753 tem
= op_with_constant_singleton_value_range (init
);
3756 step
= evolution_part_in_loop_num (chrec
, loop
->num
);
3757 tem
= op_with_constant_singleton_value_range (step
);
3761 /* If STEP is symbolic, we can't know whether INIT will be the
3762 minimum or maximum value in the range. Also, unless INIT is
3763 a simple expression, compare_values and possibly other functions
3764 in tree-vrp won't be able to handle it. */
3765 if (step
== NULL_TREE
3766 || !is_gimple_min_invariant (step
)
3767 || !valid_value_p (init
))
3770 dir
= scev_direction (chrec
);
3771 if (/* Do not adjust ranges if we do not know whether the iv increases
3772 or decreases, ... */
3773 dir
== EV_DIR_UNKNOWN
3774 /* ... or if it may wrap. */
3775 || scev_probably_wraps_p (init
, step
, stmt
, get_chrec_loop (chrec
),
3779 /* We use TYPE_MIN_VALUE and TYPE_MAX_VALUE here instead of
3780 negative_overflow_infinity and positive_overflow_infinity,
3781 because we have concluded that the loop probably does not
3784 type
= TREE_TYPE (var
);
3785 if (POINTER_TYPE_P (type
) || !TYPE_MIN_VALUE (type
))
3786 tmin
= lower_bound_in_type (type
, type
);
3788 tmin
= TYPE_MIN_VALUE (type
);
3789 if (POINTER_TYPE_P (type
) || !TYPE_MAX_VALUE (type
))
3790 tmax
= upper_bound_in_type (type
, type
);
3792 tmax
= TYPE_MAX_VALUE (type
);
3794 /* Try to use estimated number of iterations for the loop to constrain the
3795 final value in the evolution. */
3796 if (TREE_CODE (step
) == INTEGER_CST
3797 && is_gimple_val (init
)
3798 && (TREE_CODE (init
) != SSA_NAME
3799 || get_value_range (init
)->type
== VR_RANGE
))
3803 /* We are only entering here for loop header PHI nodes, so using
3804 the number of latch executions is the correct thing to use. */
3805 if (max_loop_iterations (loop
, &nit
))
3807 value_range_t maxvr
= VR_INITIALIZER
;
3808 signop sgn
= TYPE_SIGN (TREE_TYPE (step
));
3811 wide_int wtmp
= wi::mul (wi::to_widest (step
), nit
, sgn
, &overflow
);
3812 /* If the multiplication overflowed we can't do a meaningful
3813 adjustment. Likewise if the result doesn't fit in the type
3814 of the induction variable. For a signed type we have to
3815 check whether the result has the expected signedness which
3816 is that of the step as number of iterations is unsigned. */
3818 && wi::fits_to_tree_p (wtmp
, TREE_TYPE (init
))
3820 || wi::gts_p (wtmp
, 0) == wi::gts_p (step
, 0)))
3822 tem
= wide_int_to_tree (TREE_TYPE (init
), wtmp
);
3823 extract_range_from_binary_expr (&maxvr
, PLUS_EXPR
,
3824 TREE_TYPE (init
), init
, tem
);
3825 /* Likewise if the addition did. */
3826 if (maxvr
.type
== VR_RANGE
)
3835 if (vr
->type
== VR_VARYING
|| vr
->type
== VR_UNDEFINED
)
3840 /* For VARYING or UNDEFINED ranges, just about anything we get
3841 from scalar evolutions should be better. */
3843 if (dir
== EV_DIR_DECREASES
)
3848 /* If we would create an invalid range, then just assume we
3849 know absolutely nothing. This may be over-conservative,
3850 but it's clearly safe, and should happen only in unreachable
3851 parts of code, or for invalid programs. */
3852 if (compare_values (min
, max
) == 1)
3855 set_value_range (vr
, VR_RANGE
, min
, max
, vr
->equiv
);
3857 else if (vr
->type
== VR_RANGE
)
3862 if (dir
== EV_DIR_DECREASES
)
3864 /* INIT is the maximum value. If INIT is lower than VR->MAX
3865 but no smaller than VR->MIN, set VR->MAX to INIT. */
3866 if (compare_values (init
, max
) == -1)
3869 /* According to the loop information, the variable does not
3870 overflow. If we think it does, probably because of an
3871 overflow due to arithmetic on a different INF value,
3873 if (is_negative_overflow_infinity (min
)
3874 || compare_values (min
, tmin
) == -1)
3880 /* If INIT is bigger than VR->MIN, set VR->MIN to INIT. */
3881 if (compare_values (init
, min
) == 1)
3884 if (is_positive_overflow_infinity (max
)
3885 || compare_values (tmax
, max
) == -1)
3889 /* If we just created an invalid range with the minimum
3890 greater than the maximum, we fail conservatively.
3891 This should happen only in unreachable
3892 parts of code, or for invalid programs. */
3893 if (compare_values (min
, max
) == 1)
3896 set_value_range (vr
, VR_RANGE
, min
, max
, vr
->equiv
);
3900 /* Return true if VAR may overflow at STMT. This checks any available
3901 loop information to see if we can determine that VAR does not
3905 vrp_var_may_overflow (tree var
, gimple stmt
)
3908 tree chrec
, init
, step
;
3910 if (current_loops
== NULL
)
3913 l
= loop_containing_stmt (stmt
);
3918 chrec
= instantiate_parameters (l
, analyze_scalar_evolution (l
, var
));
3919 if (TREE_CODE (chrec
) != POLYNOMIAL_CHREC
)
3922 init
= initial_condition_in_loop_num (chrec
, l
->num
);
3923 step
= evolution_part_in_loop_num (chrec
, l
->num
);
3925 if (step
== NULL_TREE
3926 || !is_gimple_min_invariant (step
)
3927 || !valid_value_p (init
))
3930 /* If we get here, we know something useful about VAR based on the
3931 loop information. If it wraps, it may overflow. */
3933 if (scev_probably_wraps_p (init
, step
, stmt
, get_chrec_loop (chrec
),
3937 if (dump_file
&& (dump_flags
& TDF_DETAILS
) != 0)
3939 print_generic_expr (dump_file
, var
, 0);
3940 fprintf (dump_file
, ": loop information indicates does not overflow\n");
3947 /* Given two numeric value ranges VR0, VR1 and a comparison code COMP:
3949 - Return BOOLEAN_TRUE_NODE if VR0 COMP VR1 always returns true for
3950 all the values in the ranges.
3952 - Return BOOLEAN_FALSE_NODE if the comparison always returns false.
3954 - Return NULL_TREE if it is not always possible to determine the
3955 value of the comparison.
3957 Also set *STRICT_OVERFLOW_P to indicate whether a range with an
3958 overflow infinity was used in the test. */
3962 compare_ranges (enum tree_code comp
, value_range_t
*vr0
, value_range_t
*vr1
,
3963 bool *strict_overflow_p
)
3965 /* VARYING or UNDEFINED ranges cannot be compared. */
3966 if (vr0
->type
== VR_VARYING
3967 || vr0
->type
== VR_UNDEFINED
3968 || vr1
->type
== VR_VARYING
3969 || vr1
->type
== VR_UNDEFINED
)
3972 /* Anti-ranges need to be handled separately. */
3973 if (vr0
->type
== VR_ANTI_RANGE
|| vr1
->type
== VR_ANTI_RANGE
)
3975 /* If both are anti-ranges, then we cannot compute any
3977 if (vr0
->type
== VR_ANTI_RANGE
&& vr1
->type
== VR_ANTI_RANGE
)
3980 /* These comparisons are never statically computable. */
3987 /* Equality can be computed only between a range and an
3988 anti-range. ~[VAL1, VAL2] == [VAL1, VAL2] is always false. */
3989 if (vr0
->type
== VR_RANGE
)
3991 /* To simplify processing, make VR0 the anti-range. */
3992 value_range_t
*tmp
= vr0
;
3997 gcc_assert (comp
== NE_EXPR
|| comp
== EQ_EXPR
);
3999 if (compare_values_warnv (vr0
->min
, vr1
->min
, strict_overflow_p
) == 0
4000 && compare_values_warnv (vr0
->max
, vr1
->max
, strict_overflow_p
) == 0)
4001 return (comp
== NE_EXPR
) ? boolean_true_node
: boolean_false_node
;
4006 if (!usable_range_p (vr0
, strict_overflow_p
)
4007 || !usable_range_p (vr1
, strict_overflow_p
))
4010 /* Simplify processing. If COMP is GT_EXPR or GE_EXPR, switch the
4011 operands around and change the comparison code. */
4012 if (comp
== GT_EXPR
|| comp
== GE_EXPR
)
4015 comp
= (comp
== GT_EXPR
) ? LT_EXPR
: LE_EXPR
;
4021 if (comp
== EQ_EXPR
)
4023 /* Equality may only be computed if both ranges represent
4024 exactly one value. */
4025 if (compare_values_warnv (vr0
->min
, vr0
->max
, strict_overflow_p
) == 0
4026 && compare_values_warnv (vr1
->min
, vr1
->max
, strict_overflow_p
) == 0)
4028 int cmp_min
= compare_values_warnv (vr0
->min
, vr1
->min
,
4030 int cmp_max
= compare_values_warnv (vr0
->max
, vr1
->max
,
4032 if (cmp_min
== 0 && cmp_max
== 0)
4033 return boolean_true_node
;
4034 else if (cmp_min
!= -2 && cmp_max
!= -2)
4035 return boolean_false_node
;
4037 /* If [V0_MIN, V1_MAX] < [V1_MIN, V1_MAX] then V0 != V1. */
4038 else if (compare_values_warnv (vr0
->min
, vr1
->max
,
4039 strict_overflow_p
) == 1
4040 || compare_values_warnv (vr1
->min
, vr0
->max
,
4041 strict_overflow_p
) == 1)
4042 return boolean_false_node
;
4046 else if (comp
== NE_EXPR
)
4050 /* If VR0 is completely to the left or completely to the right
4051 of VR1, they are always different. Notice that we need to
4052 make sure that both comparisons yield similar results to
4053 avoid comparing values that cannot be compared at
4055 cmp1
= compare_values_warnv (vr0
->max
, vr1
->min
, strict_overflow_p
);
4056 cmp2
= compare_values_warnv (vr0
->min
, vr1
->max
, strict_overflow_p
);
4057 if ((cmp1
== -1 && cmp2
== -1) || (cmp1
== 1 && cmp2
== 1))
4058 return boolean_true_node
;
4060 /* If VR0 and VR1 represent a single value and are identical,
4062 else if (compare_values_warnv (vr0
->min
, vr0
->max
,
4063 strict_overflow_p
) == 0
4064 && compare_values_warnv (vr1
->min
, vr1
->max
,
4065 strict_overflow_p
) == 0
4066 && compare_values_warnv (vr0
->min
, vr1
->min
,
4067 strict_overflow_p
) == 0
4068 && compare_values_warnv (vr0
->max
, vr1
->max
,
4069 strict_overflow_p
) == 0)
4070 return boolean_false_node
;
4072 /* Otherwise, they may or may not be different. */
4076 else if (comp
== LT_EXPR
|| comp
== LE_EXPR
)
4080 /* If VR0 is to the left of VR1, return true. */
4081 tst
= compare_values_warnv (vr0
->max
, vr1
->min
, strict_overflow_p
);
4082 if ((comp
== LT_EXPR
&& tst
== -1)
4083 || (comp
== LE_EXPR
&& (tst
== -1 || tst
== 0)))
4085 if (overflow_infinity_range_p (vr0
)
4086 || overflow_infinity_range_p (vr1
))
4087 *strict_overflow_p
= true;
4088 return boolean_true_node
;
4091 /* If VR0 is to the right of VR1, return false. */
4092 tst
= compare_values_warnv (vr0
->min
, vr1
->max
, strict_overflow_p
);
4093 if ((comp
== LT_EXPR
&& (tst
== 0 || tst
== 1))
4094 || (comp
== LE_EXPR
&& tst
== 1))
4096 if (overflow_infinity_range_p (vr0
)
4097 || overflow_infinity_range_p (vr1
))
4098 *strict_overflow_p
= true;
4099 return boolean_false_node
;
4102 /* Otherwise, we don't know. */
4110 /* Given a value range VR, a value VAL and a comparison code COMP, return
4111 BOOLEAN_TRUE_NODE if VR COMP VAL always returns true for all the
4112 values in VR. Return BOOLEAN_FALSE_NODE if the comparison
4113 always returns false. Return NULL_TREE if it is not always
4114 possible to determine the value of the comparison. Also set
4115 *STRICT_OVERFLOW_P to indicate whether a range with an overflow
4116 infinity was used in the test. */
4119 compare_range_with_value (enum tree_code comp
, value_range_t
*vr
, tree val
,
4120 bool *strict_overflow_p
)
4122 if (vr
->type
== VR_VARYING
|| vr
->type
== VR_UNDEFINED
)
4125 /* Anti-ranges need to be handled separately. */
4126 if (vr
->type
== VR_ANTI_RANGE
)
4128 /* For anti-ranges, the only predicates that we can compute at
4129 compile time are equality and inequality. */
4136 /* ~[VAL_1, VAL_2] OP VAL is known if VAL_1 <= VAL <= VAL_2. */
4137 if (value_inside_range (val
, vr
->min
, vr
->max
) == 1)
4138 return (comp
== NE_EXPR
) ? boolean_true_node
: boolean_false_node
;
4143 if (!usable_range_p (vr
, strict_overflow_p
))
4146 if (comp
== EQ_EXPR
)
4148 /* EQ_EXPR may only be computed if VR represents exactly
4150 if (compare_values_warnv (vr
->min
, vr
->max
, strict_overflow_p
) == 0)
4152 int cmp
= compare_values_warnv (vr
->min
, val
, strict_overflow_p
);
4154 return boolean_true_node
;
4155 else if (cmp
== -1 || cmp
== 1 || cmp
== 2)
4156 return boolean_false_node
;
4158 else if (compare_values_warnv (val
, vr
->min
, strict_overflow_p
) == -1
4159 || compare_values_warnv (vr
->max
, val
, strict_overflow_p
) == -1)
4160 return boolean_false_node
;
4164 else if (comp
== NE_EXPR
)
4166 /* If VAL is not inside VR, then they are always different. */
4167 if (compare_values_warnv (vr
->max
, val
, strict_overflow_p
) == -1
4168 || compare_values_warnv (vr
->min
, val
, strict_overflow_p
) == 1)
4169 return boolean_true_node
;
4171 /* If VR represents exactly one value equal to VAL, then return
4173 if (compare_values_warnv (vr
->min
, vr
->max
, strict_overflow_p
) == 0
4174 && compare_values_warnv (vr
->min
, val
, strict_overflow_p
) == 0)
4175 return boolean_false_node
;
4177 /* Otherwise, they may or may not be different. */
4180 else if (comp
== LT_EXPR
|| comp
== LE_EXPR
)
4184 /* If VR is to the left of VAL, return true. */
4185 tst
= compare_values_warnv (vr
->max
, val
, strict_overflow_p
);
4186 if ((comp
== LT_EXPR
&& tst
== -1)
4187 || (comp
== LE_EXPR
&& (tst
== -1 || tst
== 0)))
4189 if (overflow_infinity_range_p (vr
))
4190 *strict_overflow_p
= true;
4191 return boolean_true_node
;
4194 /* If VR is to the right of VAL, return false. */
4195 tst
= compare_values_warnv (vr
->min
, val
, strict_overflow_p
);
4196 if ((comp
== LT_EXPR
&& (tst
== 0 || tst
== 1))
4197 || (comp
== LE_EXPR
&& tst
== 1))
4199 if (overflow_infinity_range_p (vr
))
4200 *strict_overflow_p
= true;
4201 return boolean_false_node
;
4204 /* Otherwise, we don't know. */
4207 else if (comp
== GT_EXPR
|| comp
== GE_EXPR
)
4211 /* If VR is to the right of VAL, return true. */
4212 tst
= compare_values_warnv (vr
->min
, val
, strict_overflow_p
);
4213 if ((comp
== GT_EXPR
&& tst
== 1)
4214 || (comp
== GE_EXPR
&& (tst
== 0 || tst
== 1)))
4216 if (overflow_infinity_range_p (vr
))
4217 *strict_overflow_p
= true;
4218 return boolean_true_node
;
4221 /* If VR is to the left of VAL, return false. */
4222 tst
= compare_values_warnv (vr
->max
, val
, strict_overflow_p
);
4223 if ((comp
== GT_EXPR
&& (tst
== -1 || tst
== 0))
4224 || (comp
== GE_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. */
4239 /* Debugging dumps. */
4241 void dump_value_range (FILE *, value_range_t
*);
4242 void debug_value_range (value_range_t
*);
4243 void dump_all_value_ranges (FILE *);
4244 void debug_all_value_ranges (void);
4245 void dump_vr_equiv (FILE *, bitmap
);
4246 void debug_vr_equiv (bitmap
);
4249 /* Dump value range VR to FILE. */
4252 dump_value_range (FILE *file
, value_range_t
*vr
)
4255 fprintf (file
, "[]");
4256 else if (vr
->type
== VR_UNDEFINED
)
4257 fprintf (file
, "UNDEFINED");
4258 else if (vr
->type
== VR_RANGE
|| vr
->type
== VR_ANTI_RANGE
)
4260 tree type
= TREE_TYPE (vr
->min
);
4262 fprintf (file
, "%s[", (vr
->type
== VR_ANTI_RANGE
) ? "~" : "");
4264 if (is_negative_overflow_infinity (vr
->min
))
4265 fprintf (file
, "-INF(OVF)");
4266 else if (INTEGRAL_TYPE_P (type
)
4267 && !TYPE_UNSIGNED (type
)
4268 && vrp_val_is_min (vr
->min
))
4269 fprintf (file
, "-INF");
4271 print_generic_expr (file
, vr
->min
, 0);
4273 fprintf (file
, ", ");
4275 if (is_positive_overflow_infinity (vr
->max
))
4276 fprintf (file
, "+INF(OVF)");
4277 else if (INTEGRAL_TYPE_P (type
)
4278 && vrp_val_is_max (vr
->max
))
4279 fprintf (file
, "+INF");
4281 print_generic_expr (file
, vr
->max
, 0);
4283 fprintf (file
, "]");
4290 fprintf (file
, " EQUIVALENCES: { ");
4292 EXECUTE_IF_SET_IN_BITMAP (vr
->equiv
, 0, i
, bi
)
4294 print_generic_expr (file
, ssa_name (i
), 0);
4295 fprintf (file
, " ");
4299 fprintf (file
, "} (%u elements)", c
);
4302 else if (vr
->type
== VR_VARYING
)
4303 fprintf (file
, "VARYING");
4305 fprintf (file
, "INVALID RANGE");
4309 /* Dump value range VR to stderr. */
4312 debug_value_range (value_range_t
*vr
)
4314 dump_value_range (stderr
, vr
);
4315 fprintf (stderr
, "\n");
4319 /* Dump value ranges of all SSA_NAMEs to FILE. */
4322 dump_all_value_ranges (FILE *file
)
4326 for (i
= 0; i
< num_vr_values
; i
++)
4330 print_generic_expr (file
, ssa_name (i
), 0);
4331 fprintf (file
, ": ");
4332 dump_value_range (file
, vr_value
[i
]);
4333 fprintf (file
, "\n");
4337 fprintf (file
, "\n");
4341 /* Dump all value ranges to stderr. */
4344 debug_all_value_ranges (void)
4346 dump_all_value_ranges (stderr
);
4350 /* Given a COND_EXPR COND of the form 'V OP W', and an SSA name V,
4351 create a new SSA name N and return the assertion assignment
4352 'V = ASSERT_EXPR <V, V OP W>'. */
4355 build_assert_expr_for (tree cond
, tree v
)
4360 gcc_assert (TREE_CODE (v
) == SSA_NAME
4361 && COMPARISON_CLASS_P (cond
));
4363 a
= build2 (ASSERT_EXPR
, TREE_TYPE (v
), v
, cond
);
4364 assertion
= gimple_build_assign (NULL_TREE
, a
);
4366 /* The new ASSERT_EXPR, creates a new SSA name that replaces the
4367 operand of the ASSERT_EXPR. Create it so the new name and the old one
4368 are registered in the replacement table so that we can fix the SSA web
4369 after adding all the ASSERT_EXPRs. */
4370 create_new_def_for (v
, assertion
, NULL
);
4376 /* Return false if EXPR is a predicate expression involving floating
4380 fp_predicate (gimple stmt
)
4382 GIMPLE_CHECK (stmt
, GIMPLE_COND
);
4384 return FLOAT_TYPE_P (TREE_TYPE (gimple_cond_lhs (stmt
)));
4387 /* If the range of values taken by OP can be inferred after STMT executes,
4388 return the comparison code (COMP_CODE_P) and value (VAL_P) that
4389 describes the inferred range. Return true if a range could be
4393 infer_value_range (gimple stmt
, tree op
, enum tree_code
*comp_code_p
, tree
*val_p
)
4396 *comp_code_p
= ERROR_MARK
;
4398 /* Do not attempt to infer anything in names that flow through
4400 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op
))
4403 /* Similarly, don't infer anything from statements that may throw
4404 exceptions. ??? Relax this requirement? */
4405 if (stmt_could_throw_p (stmt
))
4408 /* If STMT is the last statement of a basic block with no
4409 successors, there is no point inferring anything about any of its
4410 operands. We would not be able to find a proper insertion point
4411 for the assertion, anyway. */
4412 if (stmt_ends_bb_p (stmt
) && EDGE_COUNT (gimple_bb (stmt
)->succs
) == 0)
4415 if (infer_nonnull_range (stmt
, op
))
4417 *val_p
= build_int_cst (TREE_TYPE (op
), 0);
4418 *comp_code_p
= NE_EXPR
;
4426 void dump_asserts_for (FILE *, tree
);
4427 void debug_asserts_for (tree
);
4428 void dump_all_asserts (FILE *);
4429 void debug_all_asserts (void);
4431 /* Dump all the registered assertions for NAME to FILE. */
4434 dump_asserts_for (FILE *file
, tree name
)
4438 fprintf (file
, "Assertions to be inserted for ");
4439 print_generic_expr (file
, name
, 0);
4440 fprintf (file
, "\n");
4442 loc
= asserts_for
[SSA_NAME_VERSION (name
)];
4445 fprintf (file
, "\t");
4446 print_gimple_stmt (file
, gsi_stmt (loc
->si
), 0, 0);
4447 fprintf (file
, "\n\tBB #%d", loc
->bb
->index
);
4450 fprintf (file
, "\n\tEDGE %d->%d", loc
->e
->src
->index
,
4451 loc
->e
->dest
->index
);
4452 dump_edge_info (file
, loc
->e
, dump_flags
, 0);
4454 fprintf (file
, "\n\tPREDICATE: ");
4455 print_generic_expr (file
, name
, 0);
4456 fprintf (file
, " %s ", get_tree_code_name (loc
->comp_code
));
4457 print_generic_expr (file
, loc
->val
, 0);
4458 fprintf (file
, "\n\n");
4462 fprintf (file
, "\n");
4466 /* Dump all the registered assertions for NAME to stderr. */
4469 debug_asserts_for (tree name
)
4471 dump_asserts_for (stderr
, name
);
4475 /* Dump all the registered assertions for all the names to FILE. */
4478 dump_all_asserts (FILE *file
)
4483 fprintf (file
, "\nASSERT_EXPRs to be inserted\n\n");
4484 EXECUTE_IF_SET_IN_BITMAP (need_assert_for
, 0, i
, bi
)
4485 dump_asserts_for (file
, ssa_name (i
));
4486 fprintf (file
, "\n");
4490 /* Dump all the registered assertions for all the names to stderr. */
4493 debug_all_asserts (void)
4495 dump_all_asserts (stderr
);
4499 /* If NAME doesn't have an ASSERT_EXPR registered for asserting
4500 'EXPR COMP_CODE VAL' at a location that dominates block BB or
4501 E->DEST, then register this location as a possible insertion point
4502 for ASSERT_EXPR <NAME, EXPR COMP_CODE VAL>.
4504 BB, E and SI provide the exact insertion point for the new
4505 ASSERT_EXPR. If BB is NULL, then the ASSERT_EXPR is to be inserted
4506 on edge E. Otherwise, if E is NULL, the ASSERT_EXPR is inserted on
4507 BB. If SI points to a COND_EXPR or a SWITCH_EXPR statement, then E
4508 must not be NULL. */
4511 register_new_assert_for (tree name
, tree expr
,
4512 enum tree_code comp_code
,
4516 gimple_stmt_iterator si
)
4518 assert_locus_t n
, loc
, last_loc
;
4519 basic_block dest_bb
;
4521 gcc_checking_assert (bb
== NULL
|| e
== NULL
);
4524 gcc_checking_assert (gimple_code (gsi_stmt (si
)) != GIMPLE_COND
4525 && gimple_code (gsi_stmt (si
)) != GIMPLE_SWITCH
);
4527 /* Never build an assert comparing against an integer constant with
4528 TREE_OVERFLOW set. This confuses our undefined overflow warning
4530 if (TREE_OVERFLOW_P (val
))
4531 val
= drop_tree_overflow (val
);
4533 /* The new assertion A will be inserted at BB or E. We need to
4534 determine if the new location is dominated by a previously
4535 registered location for A. If we are doing an edge insertion,
4536 assume that A will be inserted at E->DEST. Note that this is not
4539 If E is a critical edge, it will be split. But even if E is
4540 split, the new block will dominate the same set of blocks that
4543 The reverse, however, is not true, blocks dominated by E->DEST
4544 will not be dominated by the new block created to split E. So,
4545 if the insertion location is on a critical edge, we will not use
4546 the new location to move another assertion previously registered
4547 at a block dominated by E->DEST. */
4548 dest_bb
= (bb
) ? bb
: e
->dest
;
4550 /* If NAME already has an ASSERT_EXPR registered for COMP_CODE and
4551 VAL at a block dominating DEST_BB, then we don't need to insert a new
4552 one. Similarly, if the same assertion already exists at a block
4553 dominated by DEST_BB and the new location is not on a critical
4554 edge, then update the existing location for the assertion (i.e.,
4555 move the assertion up in the dominance tree).
4557 Note, this is implemented as a simple linked list because there
4558 should not be more than a handful of assertions registered per
4559 name. If this becomes a performance problem, a table hashed by
4560 COMP_CODE and VAL could be implemented. */
4561 loc
= asserts_for
[SSA_NAME_VERSION (name
)];
4565 if (loc
->comp_code
== comp_code
4567 || operand_equal_p (loc
->val
, val
, 0))
4568 && (loc
->expr
== expr
4569 || operand_equal_p (loc
->expr
, expr
, 0)))
4571 /* If E is not a critical edge and DEST_BB
4572 dominates the existing location for the assertion, move
4573 the assertion up in the dominance tree by updating its
4574 location information. */
4575 if ((e
== NULL
|| !EDGE_CRITICAL_P (e
))
4576 && dominated_by_p (CDI_DOMINATORS
, loc
->bb
, dest_bb
))
4585 /* Update the last node of the list and move to the next one. */
4590 /* If we didn't find an assertion already registered for
4591 NAME COMP_CODE VAL, add a new one at the end of the list of
4592 assertions associated with NAME. */
4593 n
= XNEW (struct assert_locus_d
);
4597 n
->comp_code
= comp_code
;
4605 asserts_for
[SSA_NAME_VERSION (name
)] = n
;
4607 bitmap_set_bit (need_assert_for
, SSA_NAME_VERSION (name
));
4610 /* (COND_OP0 COND_CODE COND_OP1) is a predicate which uses NAME.
4611 Extract a suitable test code and value and store them into *CODE_P and
4612 *VAL_P so the predicate is normalized to NAME *CODE_P *VAL_P.
4614 If no extraction was possible, return FALSE, otherwise return TRUE.
4616 If INVERT is true, then we invert the result stored into *CODE_P. */
4619 extract_code_and_val_from_cond_with_ops (tree name
, enum tree_code cond_code
,
4620 tree cond_op0
, tree cond_op1
,
4621 bool invert
, enum tree_code
*code_p
,
4624 enum tree_code comp_code
;
4627 /* Otherwise, we have a comparison of the form NAME COMP VAL
4628 or VAL COMP NAME. */
4629 if (name
== cond_op1
)
4631 /* If the predicate is of the form VAL COMP NAME, flip
4632 COMP around because we need to register NAME as the
4633 first operand in the predicate. */
4634 comp_code
= swap_tree_comparison (cond_code
);
4639 /* The comparison is of the form NAME COMP VAL, so the
4640 comparison code remains unchanged. */
4641 comp_code
= cond_code
;
4645 /* Invert the comparison code as necessary. */
4647 comp_code
= invert_tree_comparison (comp_code
, 0);
4649 /* VRP does not handle float types. */
4650 if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (val
)))
4653 /* Do not register always-false predicates.
4654 FIXME: this works around a limitation in fold() when dealing with
4655 enumerations. Given 'enum { N1, N2 } x;', fold will not
4656 fold 'if (x > N2)' to 'if (0)'. */
4657 if ((comp_code
== GT_EXPR
|| comp_code
== LT_EXPR
)
4658 && INTEGRAL_TYPE_P (TREE_TYPE (val
)))
4660 tree min
= TYPE_MIN_VALUE (TREE_TYPE (val
));
4661 tree max
= TYPE_MAX_VALUE (TREE_TYPE (val
));
4663 if (comp_code
== GT_EXPR
4665 || compare_values (val
, max
) == 0))
4668 if (comp_code
== LT_EXPR
4670 || compare_values (val
, min
) == 0))
4673 *code_p
= comp_code
;
4678 /* Find out smallest RES where RES > VAL && (RES & MASK) == RES, if any
4679 (otherwise return VAL). VAL and MASK must be zero-extended for
4680 precision PREC. If SGNBIT is non-zero, first xor VAL with SGNBIT
4681 (to transform signed values into unsigned) and at the end xor
4685 masked_increment (wide_int val
, wide_int mask
, wide_int sgnbit
,
4688 wide_int bit
= wi::one (prec
), res
;
4692 for (i
= 0; i
< prec
; i
++, bit
+= bit
)
4695 if ((res
& bit
) == 0)
4698 res
= (val
+ bit
).and_not (res
);
4700 if (wi::gtu_p (res
, val
))
4701 return res
^ sgnbit
;
4703 return val
^ sgnbit
;
4706 /* Try to register an edge assertion for SSA name NAME on edge E for
4707 the condition COND contributing to the conditional jump pointed to by BSI.
4708 Invert the condition COND if INVERT is true.
4709 Return true if an assertion for NAME could be registered. */
4712 register_edge_assert_for_2 (tree name
, edge e
, gimple_stmt_iterator bsi
,
4713 enum tree_code cond_code
,
4714 tree cond_op0
, tree cond_op1
, bool invert
)
4717 enum tree_code comp_code
;
4718 bool retval
= false;
4720 if (!extract_code_and_val_from_cond_with_ops (name
, cond_code
,
4723 invert
, &comp_code
, &val
))
4726 /* Only register an ASSERT_EXPR if NAME was found in the sub-graph
4727 reachable from E. */
4728 if (live_on_edge (e
, name
)
4729 && !has_single_use (name
))
4731 register_new_assert_for (name
, name
, comp_code
, val
, NULL
, e
, bsi
);
4735 /* In the case of NAME <= CST and NAME being defined as
4736 NAME = (unsigned) NAME2 + CST2 we can assert NAME2 >= -CST2
4737 and NAME2 <= CST - CST2. We can do the same for NAME > CST.
4738 This catches range and anti-range tests. */
4739 if ((comp_code
== LE_EXPR
4740 || comp_code
== GT_EXPR
)
4741 && TREE_CODE (val
) == INTEGER_CST
4742 && TYPE_UNSIGNED (TREE_TYPE (val
)))
4744 gimple def_stmt
= SSA_NAME_DEF_STMT (name
);
4745 tree cst2
= NULL_TREE
, name2
= NULL_TREE
, name3
= NULL_TREE
;
4747 /* Extract CST2 from the (optional) addition. */
4748 if (is_gimple_assign (def_stmt
)
4749 && gimple_assign_rhs_code (def_stmt
) == PLUS_EXPR
)
4751 name2
= gimple_assign_rhs1 (def_stmt
);
4752 cst2
= gimple_assign_rhs2 (def_stmt
);
4753 if (TREE_CODE (name2
) == SSA_NAME
4754 && TREE_CODE (cst2
) == INTEGER_CST
)
4755 def_stmt
= SSA_NAME_DEF_STMT (name2
);
4758 /* Extract NAME2 from the (optional) sign-changing cast. */
4759 if (gimple_assign_cast_p (def_stmt
))
4761 if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt
))
4762 && ! TYPE_UNSIGNED (TREE_TYPE (gimple_assign_rhs1 (def_stmt
)))
4763 && (TYPE_PRECISION (gimple_expr_type (def_stmt
))
4764 == TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (def_stmt
)))))
4765 name3
= gimple_assign_rhs1 (def_stmt
);
4768 /* If name3 is used later, create an ASSERT_EXPR for it. */
4769 if (name3
!= NULL_TREE
4770 && TREE_CODE (name3
) == SSA_NAME
4771 && (cst2
== NULL_TREE
4772 || TREE_CODE (cst2
) == INTEGER_CST
)
4773 && INTEGRAL_TYPE_P (TREE_TYPE (name3
))
4774 && live_on_edge (e
, name3
)
4775 && !has_single_use (name3
))
4779 /* Build an expression for the range test. */
4780 tmp
= build1 (NOP_EXPR
, TREE_TYPE (name
), name3
);
4781 if (cst2
!= NULL_TREE
)
4782 tmp
= build2 (PLUS_EXPR
, TREE_TYPE (name
), tmp
, cst2
);
4786 fprintf (dump_file
, "Adding assert for ");
4787 print_generic_expr (dump_file
, name3
, 0);
4788 fprintf (dump_file
, " from ");
4789 print_generic_expr (dump_file
, tmp
, 0);
4790 fprintf (dump_file
, "\n");
4793 register_new_assert_for (name3
, tmp
, comp_code
, val
, NULL
, e
, bsi
);
4798 /* If name2 is used later, create an ASSERT_EXPR for it. */
4799 if (name2
!= NULL_TREE
4800 && TREE_CODE (name2
) == SSA_NAME
4801 && TREE_CODE (cst2
) == INTEGER_CST
4802 && INTEGRAL_TYPE_P (TREE_TYPE (name2
))
4803 && live_on_edge (e
, name2
)
4804 && !has_single_use (name2
))
4808 /* Build an expression for the range test. */
4810 if (TREE_TYPE (name
) != TREE_TYPE (name2
))
4811 tmp
= build1 (NOP_EXPR
, TREE_TYPE (name
), tmp
);
4812 if (cst2
!= NULL_TREE
)
4813 tmp
= build2 (PLUS_EXPR
, TREE_TYPE (name
), tmp
, cst2
);
4817 fprintf (dump_file
, "Adding assert for ");
4818 print_generic_expr (dump_file
, name2
, 0);
4819 fprintf (dump_file
, " from ");
4820 print_generic_expr (dump_file
, tmp
, 0);
4821 fprintf (dump_file
, "\n");
4824 register_new_assert_for (name2
, tmp
, comp_code
, val
, NULL
, e
, bsi
);
4830 /* In the case of post-in/decrement tests like if (i++) ... and uses
4831 of the in/decremented value on the edge the extra name we want to
4832 assert for is not on the def chain of the name compared. Instead
4833 it is in the set of use stmts. */
4834 if ((comp_code
== NE_EXPR
4835 || comp_code
== EQ_EXPR
)
4836 && TREE_CODE (val
) == INTEGER_CST
)
4838 imm_use_iterator ui
;
4840 FOR_EACH_IMM_USE_STMT (use_stmt
, ui
, name
)
4842 /* Cut off to use-stmts that are in the predecessor. */
4843 if (gimple_bb (use_stmt
) != e
->src
)
4846 if (!is_gimple_assign (use_stmt
))
4849 enum tree_code code
= gimple_assign_rhs_code (use_stmt
);
4850 if (code
!= PLUS_EXPR
4851 && code
!= MINUS_EXPR
)
4854 tree cst
= gimple_assign_rhs2 (use_stmt
);
4855 if (TREE_CODE (cst
) != INTEGER_CST
)
4858 tree name2
= gimple_assign_lhs (use_stmt
);
4859 if (live_on_edge (e
, name2
))
4861 cst
= int_const_binop (code
, val
, cst
);
4862 register_new_assert_for (name2
, name2
, comp_code
, cst
,
4869 if (TREE_CODE_CLASS (comp_code
) == tcc_comparison
4870 && TREE_CODE (val
) == INTEGER_CST
)
4872 gimple def_stmt
= SSA_NAME_DEF_STMT (name
);
4873 tree name2
= NULL_TREE
, names
[2], cst2
= NULL_TREE
;
4874 tree val2
= NULL_TREE
;
4875 unsigned int prec
= TYPE_PRECISION (TREE_TYPE (val
));
4876 wide_int mask
= wi::zero (prec
);
4877 unsigned int nprec
= prec
;
4878 enum tree_code rhs_code
= ERROR_MARK
;
4880 if (is_gimple_assign (def_stmt
))
4881 rhs_code
= gimple_assign_rhs_code (def_stmt
);
4883 /* Add asserts for NAME cmp CST and NAME being defined
4884 as NAME = (int) NAME2. */
4885 if (!TYPE_UNSIGNED (TREE_TYPE (val
))
4886 && (comp_code
== LE_EXPR
|| comp_code
== LT_EXPR
4887 || comp_code
== GT_EXPR
|| comp_code
== GE_EXPR
)
4888 && gimple_assign_cast_p (def_stmt
))
4890 name2
= gimple_assign_rhs1 (def_stmt
);
4891 if (CONVERT_EXPR_CODE_P (rhs_code
)
4892 && INTEGRAL_TYPE_P (TREE_TYPE (name2
))
4893 && TYPE_UNSIGNED (TREE_TYPE (name2
))
4894 && prec
== TYPE_PRECISION (TREE_TYPE (name2
))
4895 && (comp_code
== LE_EXPR
|| comp_code
== GT_EXPR
4896 || !tree_int_cst_equal (val
,
4897 TYPE_MIN_VALUE (TREE_TYPE (val
))))
4898 && live_on_edge (e
, name2
)
4899 && !has_single_use (name2
))
4902 enum tree_code new_comp_code
= comp_code
;
4904 cst
= fold_convert (TREE_TYPE (name2
),
4905 TYPE_MIN_VALUE (TREE_TYPE (val
)));
4906 /* Build an expression for the range test. */
4907 tmp
= build2 (PLUS_EXPR
, TREE_TYPE (name2
), name2
, cst
);
4908 cst
= fold_build2 (PLUS_EXPR
, TREE_TYPE (name2
), cst
,
4909 fold_convert (TREE_TYPE (name2
), val
));
4910 if (comp_code
== LT_EXPR
|| comp_code
== GE_EXPR
)
4912 new_comp_code
= comp_code
== LT_EXPR
? LE_EXPR
: GT_EXPR
;
4913 cst
= fold_build2 (MINUS_EXPR
, TREE_TYPE (name2
), cst
,
4914 build_int_cst (TREE_TYPE (name2
), 1));
4919 fprintf (dump_file
, "Adding assert for ");
4920 print_generic_expr (dump_file
, name2
, 0);
4921 fprintf (dump_file
, " from ");
4922 print_generic_expr (dump_file
, tmp
, 0);
4923 fprintf (dump_file
, "\n");
4926 register_new_assert_for (name2
, tmp
, new_comp_code
, cst
, NULL
,
4933 /* Add asserts for NAME cmp CST and NAME being defined as
4934 NAME = NAME2 >> CST2.
4936 Extract CST2 from the right shift. */
4937 if (rhs_code
== RSHIFT_EXPR
)
4939 name2
= gimple_assign_rhs1 (def_stmt
);
4940 cst2
= gimple_assign_rhs2 (def_stmt
);
4941 if (TREE_CODE (name2
) == SSA_NAME
4942 && tree_fits_uhwi_p (cst2
)
4943 && INTEGRAL_TYPE_P (TREE_TYPE (name2
))
4944 && IN_RANGE (tree_to_uhwi (cst2
), 1, prec
- 1)
4945 && prec
== GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (val
)))
4946 && live_on_edge (e
, name2
)
4947 && !has_single_use (name2
))
4949 mask
= wi::mask (tree_to_uhwi (cst2
), false, prec
);
4950 val2
= fold_binary (LSHIFT_EXPR
, TREE_TYPE (val
), val
, cst2
);
4953 if (val2
!= NULL_TREE
4954 && TREE_CODE (val2
) == INTEGER_CST
4955 && simple_cst_equal (fold_build2 (RSHIFT_EXPR
,
4959 enum tree_code new_comp_code
= comp_code
;
4963 if (comp_code
== EQ_EXPR
|| comp_code
== NE_EXPR
)
4965 if (!TYPE_UNSIGNED (TREE_TYPE (val
)))
4967 tree type
= build_nonstandard_integer_type (prec
, 1);
4968 tmp
= build1 (NOP_EXPR
, type
, name2
);
4969 val2
= fold_convert (type
, val2
);
4971 tmp
= fold_build2 (MINUS_EXPR
, TREE_TYPE (tmp
), tmp
, val2
);
4972 new_val
= wide_int_to_tree (TREE_TYPE (tmp
), mask
);
4973 new_comp_code
= comp_code
== EQ_EXPR
? LE_EXPR
: GT_EXPR
;
4975 else if (comp_code
== LT_EXPR
|| comp_code
== GE_EXPR
)
4978 = wi::min_value (prec
, TYPE_SIGN (TREE_TYPE (val
)));
4980 if (minval
== wide_int (new_val
))
4981 new_val
= NULL_TREE
;
4986 = wi::max_value (prec
, TYPE_SIGN (TREE_TYPE (val
)));
4987 mask
|= wide_int (val2
);
4989 new_val
= NULL_TREE
;
4991 new_val
= wide_int_to_tree (TREE_TYPE (val2
), mask
);
4998 fprintf (dump_file
, "Adding assert for ");
4999 print_generic_expr (dump_file
, name2
, 0);
5000 fprintf (dump_file
, " from ");
5001 print_generic_expr (dump_file
, tmp
, 0);
5002 fprintf (dump_file
, "\n");
5005 register_new_assert_for (name2
, tmp
, new_comp_code
, new_val
,
5011 /* Add asserts for NAME cmp CST and NAME being defined as
5012 NAME = NAME2 & CST2.
5014 Extract CST2 from the and.
5017 NAME = (unsigned) NAME2;
5018 casts where NAME's type is unsigned and has smaller precision
5019 than NAME2's type as if it was NAME = NAME2 & MASK. */
5020 names
[0] = NULL_TREE
;
5021 names
[1] = NULL_TREE
;
5023 if (rhs_code
== BIT_AND_EXPR
5024 || (CONVERT_EXPR_CODE_P (rhs_code
)
5025 && TREE_CODE (TREE_TYPE (val
)) == INTEGER_TYPE
5026 && TYPE_UNSIGNED (TREE_TYPE (val
))
5027 && TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (def_stmt
)))
5031 name2
= gimple_assign_rhs1 (def_stmt
);
5032 if (rhs_code
== BIT_AND_EXPR
)
5033 cst2
= gimple_assign_rhs2 (def_stmt
);
5036 cst2
= TYPE_MAX_VALUE (TREE_TYPE (val
));
5037 nprec
= TYPE_PRECISION (TREE_TYPE (name2
));
5039 if (TREE_CODE (name2
) == SSA_NAME
5040 && INTEGRAL_TYPE_P (TREE_TYPE (name2
))
5041 && TREE_CODE (cst2
) == INTEGER_CST
5042 && !integer_zerop (cst2
)
5044 || TYPE_UNSIGNED (TREE_TYPE (val
))))
5046 gimple def_stmt2
= SSA_NAME_DEF_STMT (name2
);
5047 if (gimple_assign_cast_p (def_stmt2
))
5049 names
[1] = gimple_assign_rhs1 (def_stmt2
);
5050 if (!CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt2
))
5051 || !INTEGRAL_TYPE_P (TREE_TYPE (names
[1]))
5052 || (TYPE_PRECISION (TREE_TYPE (name2
))
5053 != TYPE_PRECISION (TREE_TYPE (names
[1])))
5054 || !live_on_edge (e
, names
[1])
5055 || has_single_use (names
[1]))
5056 names
[1] = NULL_TREE
;
5058 if (live_on_edge (e
, name2
)
5059 && !has_single_use (name2
))
5063 if (names
[0] || names
[1])
5065 wide_int minv
, maxv
, valv
, cst2v
;
5066 wide_int tem
, sgnbit
;
5067 bool valid_p
= false, valn
= false, cst2n
= false;
5068 enum tree_code ccode
= comp_code
;
5070 valv
= wide_int::from (val
, nprec
, UNSIGNED
);
5071 cst2v
= wide_int::from (cst2
, nprec
, UNSIGNED
);
5072 if (TYPE_SIGN (TREE_TYPE (val
)) == SIGNED
)
5074 valn
= wi::neg_p (wi::sext (valv
, nprec
));
5075 cst2n
= wi::neg_p (wi::sext (cst2v
, nprec
));
5077 /* If CST2 doesn't have most significant bit set,
5078 but VAL is negative, we have comparison like
5079 if ((x & 0x123) > -4) (always true). Just give up. */
5083 sgnbit
= wi::set_bit_in_zero (nprec
- 1, nprec
);
5085 sgnbit
= wi::zero (nprec
);
5086 minv
= valv
& cst2v
;
5090 /* Minimum unsigned value for equality is VAL & CST2
5091 (should be equal to VAL, otherwise we probably should
5092 have folded the comparison into false) and
5093 maximum unsigned value is VAL | ~CST2. */
5094 maxv
= valv
| ~cst2v
;
5095 maxv
= wi::zext (maxv
, nprec
);
5100 tem
= valv
| ~cst2v
;
5101 tem
= wi::zext (tem
, nprec
);
5102 /* If VAL is 0, handle (X & CST2) != 0 as (X & CST2) > 0U. */
5106 sgnbit
= wi::zero (nprec
);
5109 /* If (VAL | ~CST2) is all ones, handle it as
5110 (X & CST2) < VAL. */
5115 sgnbit
= wi::zero (nprec
);
5118 if (!cst2n
&& wi::neg_p (wi::sext (cst2v
, nprec
)))
5119 sgnbit
= wi::set_bit_in_zero (nprec
- 1, nprec
);
5128 if (tem
== wi::mask (nprec
- 1, false, nprec
))
5134 sgnbit
= wi::zero (nprec
);
5139 /* Minimum unsigned value for >= if (VAL & CST2) == VAL
5140 is VAL and maximum unsigned value is ~0. For signed
5141 comparison, if CST2 doesn't have most significant bit
5142 set, handle it similarly. If CST2 has MSB set,
5143 the minimum is the same, and maximum is ~0U/2. */
5146 /* If (VAL & CST2) != VAL, X & CST2 can't be equal to
5148 minv
= masked_increment (valv
, cst2v
, sgnbit
, nprec
);
5152 maxv
= wi::mask (nprec
- (cst2n
? 1 : 0), false, nprec
);
5158 /* Find out smallest MINV where MINV > VAL
5159 && (MINV & CST2) == MINV, if any. If VAL is signed and
5160 CST2 has MSB set, compute it biased by 1 << (nprec - 1). */
5161 minv
= masked_increment (valv
, cst2v
, sgnbit
, nprec
);
5164 maxv
= wi::mask (nprec
- (cst2n
? 1 : 0), false, nprec
);
5169 /* Minimum unsigned value for <= is 0 and maximum
5170 unsigned value is VAL | ~CST2 if (VAL & CST2) == VAL.
5171 Otherwise, find smallest VAL2 where VAL2 > VAL
5172 && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
5174 For signed comparison, if CST2 doesn't have most
5175 significant bit set, handle it similarly. If CST2 has
5176 MSB set, the maximum is the same and minimum is INT_MIN. */
5181 maxv
= masked_increment (valv
, cst2v
, sgnbit
, nprec
);
5187 maxv
= wi::zext (maxv
, nprec
);
5194 /* Minimum unsigned value for < is 0 and maximum
5195 unsigned value is (VAL-1) | ~CST2 if (VAL & CST2) == VAL.
5196 Otherwise, find smallest VAL2 where VAL2 > VAL
5197 && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
5199 For signed comparison, if CST2 doesn't have most
5200 significant bit set, handle it similarly. If CST2 has
5201 MSB set, the maximum is the same and minimum is INT_MIN. */
5210 maxv
= masked_increment (valv
, cst2v
, sgnbit
, nprec
);
5216 maxv
= wi::zext (maxv
, nprec
);
5225 && wi::zext (maxv
- minv
, nprec
) != wi::minus_one (nprec
))
5227 tree tmp
, new_val
, type
;
5230 for (i
= 0; i
< 2; i
++)
5233 wide_int maxv2
= maxv
;
5235 type
= TREE_TYPE (names
[i
]);
5236 if (!TYPE_UNSIGNED (type
))
5238 type
= build_nonstandard_integer_type (nprec
, 1);
5239 tmp
= build1 (NOP_EXPR
, type
, names
[i
]);
5243 tmp
= build2 (PLUS_EXPR
, type
, tmp
,
5244 wide_int_to_tree (type
, -minv
));
5245 maxv2
= maxv
- minv
;
5247 new_val
= wide_int_to_tree (type
, maxv2
);
5251 fprintf (dump_file
, "Adding assert for ");
5252 print_generic_expr (dump_file
, names
[i
], 0);
5253 fprintf (dump_file
, " from ");
5254 print_generic_expr (dump_file
, tmp
, 0);
5255 fprintf (dump_file
, "\n");
5258 register_new_assert_for (names
[i
], tmp
, LE_EXPR
,
5259 new_val
, NULL
, e
, bsi
);
5269 /* OP is an operand of a truth value expression which is known to have
5270 a particular value. Register any asserts for OP and for any
5271 operands in OP's defining statement.
5273 If CODE is EQ_EXPR, then we want to register OP is zero (false),
5274 if CODE is NE_EXPR, then we want to register OP is nonzero (true). */
5277 register_edge_assert_for_1 (tree op
, enum tree_code code
,
5278 edge e
, gimple_stmt_iterator bsi
)
5280 bool retval
= false;
5283 enum tree_code rhs_code
;
5285 /* We only care about SSA_NAMEs. */
5286 if (TREE_CODE (op
) != SSA_NAME
)
5289 /* We know that OP will have a zero or nonzero value. If OP is used
5290 more than once go ahead and register an assert for OP.
5292 The FOUND_IN_SUBGRAPH support is not helpful in this situation as
5293 it will always be set for OP (because OP is used in a COND_EXPR in
5295 if (!has_single_use (op
))
5297 val
= build_int_cst (TREE_TYPE (op
), 0);
5298 register_new_assert_for (op
, op
, code
, val
, NULL
, e
, bsi
);
5302 /* Now look at how OP is set. If it's set from a comparison,
5303 a truth operation or some bit operations, then we may be able
5304 to register information about the operands of that assignment. */
5305 op_def
= SSA_NAME_DEF_STMT (op
);
5306 if (gimple_code (op_def
) != GIMPLE_ASSIGN
)
5309 rhs_code
= gimple_assign_rhs_code (op_def
);
5311 if (TREE_CODE_CLASS (rhs_code
) == tcc_comparison
)
5313 bool invert
= (code
== EQ_EXPR
? true : false);
5314 tree op0
= gimple_assign_rhs1 (op_def
);
5315 tree op1
= gimple_assign_rhs2 (op_def
);
5317 if (TREE_CODE (op0
) == SSA_NAME
)
5318 retval
|= register_edge_assert_for_2 (op0
, e
, bsi
, rhs_code
, op0
, op1
,
5320 if (TREE_CODE (op1
) == SSA_NAME
)
5321 retval
|= register_edge_assert_for_2 (op1
, e
, bsi
, rhs_code
, op0
, op1
,
5324 else if ((code
== NE_EXPR
5325 && gimple_assign_rhs_code (op_def
) == BIT_AND_EXPR
)
5327 && gimple_assign_rhs_code (op_def
) == BIT_IOR_EXPR
))
5329 /* Recurse on each operand. */
5330 tree op0
= gimple_assign_rhs1 (op_def
);
5331 tree op1
= gimple_assign_rhs2 (op_def
);
5332 if (TREE_CODE (op0
) == SSA_NAME
5333 && has_single_use (op0
))
5334 retval
|= register_edge_assert_for_1 (op0
, code
, e
, bsi
);
5335 if (TREE_CODE (op1
) == SSA_NAME
5336 && has_single_use (op1
))
5337 retval
|= register_edge_assert_for_1 (op1
, code
, e
, bsi
);
5339 else if (gimple_assign_rhs_code (op_def
) == BIT_NOT_EXPR
5340 && TYPE_PRECISION (TREE_TYPE (gimple_assign_lhs (op_def
))) == 1)
5342 /* Recurse, flipping CODE. */
5343 code
= invert_tree_comparison (code
, false);
5344 retval
|= register_edge_assert_for_1 (gimple_assign_rhs1 (op_def
),
5347 else if (gimple_assign_rhs_code (op_def
) == SSA_NAME
)
5349 /* Recurse through the copy. */
5350 retval
|= register_edge_assert_for_1 (gimple_assign_rhs1 (op_def
),
5353 else if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (op_def
)))
5355 /* Recurse through the type conversion. */
5356 retval
|= register_edge_assert_for_1 (gimple_assign_rhs1 (op_def
),
5363 /* Try to register an edge assertion for SSA name NAME on edge E for
5364 the condition COND contributing to the conditional jump pointed to by SI.
5365 Return true if an assertion for NAME could be registered. */
5368 register_edge_assert_for (tree name
, edge e
, gimple_stmt_iterator si
,
5369 enum tree_code cond_code
, tree cond_op0
,
5373 enum tree_code comp_code
;
5374 bool retval
= false;
5375 bool is_else_edge
= (e
->flags
& EDGE_FALSE_VALUE
) != 0;
5377 /* Do not attempt to infer anything in names that flow through
5379 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name
))
5382 if (!extract_code_and_val_from_cond_with_ops (name
, cond_code
,
5388 /* Register ASSERT_EXPRs for name. */
5389 retval
|= register_edge_assert_for_2 (name
, e
, si
, cond_code
, cond_op0
,
5390 cond_op1
, is_else_edge
);
5393 /* If COND is effectively an equality test of an SSA_NAME against
5394 the value zero or one, then we may be able to assert values
5395 for SSA_NAMEs which flow into COND. */
5397 /* In the case of NAME == 1 or NAME != 0, for BIT_AND_EXPR defining
5398 statement of NAME we can assert both operands of the BIT_AND_EXPR
5399 have nonzero value. */
5400 if (((comp_code
== EQ_EXPR
&& integer_onep (val
))
5401 || (comp_code
== NE_EXPR
&& integer_zerop (val
))))
5403 gimple def_stmt
= SSA_NAME_DEF_STMT (name
);
5405 if (is_gimple_assign (def_stmt
)
5406 && gimple_assign_rhs_code (def_stmt
) == BIT_AND_EXPR
)
5408 tree op0
= gimple_assign_rhs1 (def_stmt
);
5409 tree op1
= gimple_assign_rhs2 (def_stmt
);
5410 retval
|= register_edge_assert_for_1 (op0
, NE_EXPR
, e
, si
);
5411 retval
|= register_edge_assert_for_1 (op1
, NE_EXPR
, e
, si
);
5415 /* In the case of NAME == 0 or NAME != 1, for BIT_IOR_EXPR defining
5416 statement of NAME we can assert both operands of the BIT_IOR_EXPR
5418 if (((comp_code
== EQ_EXPR
&& integer_zerop (val
))
5419 || (comp_code
== NE_EXPR
&& integer_onep (val
))))
5421 gimple def_stmt
= SSA_NAME_DEF_STMT (name
);
5423 /* For BIT_IOR_EXPR only if NAME == 0 both operands have
5424 necessarily zero value, or if type-precision is one. */
5425 if (is_gimple_assign (def_stmt
)
5426 && (gimple_assign_rhs_code (def_stmt
) == BIT_IOR_EXPR
5427 && (TYPE_PRECISION (TREE_TYPE (name
)) == 1
5428 || comp_code
== EQ_EXPR
)))
5430 tree op0
= gimple_assign_rhs1 (def_stmt
);
5431 tree op1
= gimple_assign_rhs2 (def_stmt
);
5432 retval
|= register_edge_assert_for_1 (op0
, EQ_EXPR
, e
, si
);
5433 retval
|= register_edge_assert_for_1 (op1
, EQ_EXPR
, e
, si
);
5441 /* Determine whether the outgoing edges of BB should receive an
5442 ASSERT_EXPR for each of the operands of BB's LAST statement.
5443 The last statement of BB must be a COND_EXPR.
5445 If any of the sub-graphs rooted at BB have an interesting use of
5446 the predicate operands, an assert location node is added to the
5447 list of assertions for the corresponding operands. */
5450 find_conditional_asserts (basic_block bb
, gimple last
)
5453 gimple_stmt_iterator bsi
;
5459 need_assert
= false;
5460 bsi
= gsi_for_stmt (last
);
5462 /* Look for uses of the operands in each of the sub-graphs
5463 rooted at BB. We need to check each of the outgoing edges
5464 separately, so that we know what kind of ASSERT_EXPR to
5466 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
5471 /* Register the necessary assertions for each operand in the
5472 conditional predicate. */
5473 FOR_EACH_SSA_TREE_OPERAND (op
, last
, iter
, SSA_OP_USE
)
5475 need_assert
|= register_edge_assert_for (op
, e
, bsi
,
5476 gimple_cond_code (last
),
5477 gimple_cond_lhs (last
),
5478 gimple_cond_rhs (last
));
5491 /* Compare two case labels sorting first by the destination bb index
5492 and then by the case value. */
5495 compare_case_labels (const void *p1
, const void *p2
)
5497 const struct case_info
*ci1
= (const struct case_info
*) p1
;
5498 const struct case_info
*ci2
= (const struct case_info
*) p2
;
5499 int idx1
= ci1
->bb
->index
;
5500 int idx2
= ci2
->bb
->index
;
5504 else if (idx1
== idx2
)
5506 /* Make sure the default label is first in a group. */
5507 if (!CASE_LOW (ci1
->expr
))
5509 else if (!CASE_LOW (ci2
->expr
))
5512 return tree_int_cst_compare (CASE_LOW (ci1
->expr
),
5513 CASE_LOW (ci2
->expr
));
5519 /* Determine whether the outgoing edges of BB should receive an
5520 ASSERT_EXPR for each of the operands of BB's LAST statement.
5521 The last statement of BB must be a SWITCH_EXPR.
5523 If any of the sub-graphs rooted at BB have an interesting use of
5524 the predicate operands, an assert location node is added to the
5525 list of assertions for the corresponding operands. */
5528 find_switch_asserts (basic_block bb
, gimple last
)
5531 gimple_stmt_iterator bsi
;
5534 struct case_info
*ci
;
5535 size_t n
= gimple_switch_num_labels (last
);
5536 #if GCC_VERSION >= 4000
5539 /* Work around GCC 3.4 bug (PR 37086). */
5540 volatile unsigned int idx
;
5543 need_assert
= false;
5544 bsi
= gsi_for_stmt (last
);
5545 op
= gimple_switch_index (last
);
5546 if (TREE_CODE (op
) != SSA_NAME
)
5549 /* Build a vector of case labels sorted by destination label. */
5550 ci
= XNEWVEC (struct case_info
, n
);
5551 for (idx
= 0; idx
< n
; ++idx
)
5553 ci
[idx
].expr
= gimple_switch_label (last
, idx
);
5554 ci
[idx
].bb
= label_to_block (CASE_LABEL (ci
[idx
].expr
));
5556 qsort (ci
, n
, sizeof (struct case_info
), compare_case_labels
);
5558 for (idx
= 0; idx
< n
; ++idx
)
5561 tree cl
= ci
[idx
].expr
;
5562 basic_block cbb
= ci
[idx
].bb
;
5564 min
= CASE_LOW (cl
);
5565 max
= CASE_HIGH (cl
);
5567 /* If there are multiple case labels with the same destination
5568 we need to combine them to a single value range for the edge. */
5569 if (idx
+ 1 < n
&& cbb
== ci
[idx
+ 1].bb
)
5571 /* Skip labels until the last of the group. */
5574 } while (idx
< n
&& cbb
== ci
[idx
].bb
);
5577 /* Pick up the maximum of the case label range. */
5578 if (CASE_HIGH (ci
[idx
].expr
))
5579 max
= CASE_HIGH (ci
[idx
].expr
);
5581 max
= CASE_LOW (ci
[idx
].expr
);
5584 /* Nothing to do if the range includes the default label until we
5585 can register anti-ranges. */
5586 if (min
== NULL_TREE
)
5589 /* Find the edge to register the assert expr on. */
5590 e
= find_edge (bb
, cbb
);
5592 /* Register the necessary assertions for the operand in the
5594 need_assert
|= register_edge_assert_for (op
, e
, bsi
,
5595 max
? GE_EXPR
: EQ_EXPR
,
5597 fold_convert (TREE_TYPE (op
),
5601 need_assert
|= register_edge_assert_for (op
, e
, bsi
, LE_EXPR
,
5603 fold_convert (TREE_TYPE (op
),
5613 /* Traverse all the statements in block BB looking for statements that
5614 may generate useful assertions for the SSA names in their operand.
5615 If a statement produces a useful assertion A for name N_i, then the
5616 list of assertions already generated for N_i is scanned to
5617 determine if A is actually needed.
5619 If N_i already had the assertion A at a location dominating the
5620 current location, then nothing needs to be done. Otherwise, the
5621 new location for A is recorded instead.
5623 1- For every statement S in BB, all the variables used by S are
5624 added to bitmap FOUND_IN_SUBGRAPH.
5626 2- If statement S uses an operand N in a way that exposes a known
5627 value range for N, then if N was not already generated by an
5628 ASSERT_EXPR, create a new assert location for N. For instance,
5629 if N is a pointer and the statement dereferences it, we can
5630 assume that N is not NULL.
5632 3- COND_EXPRs are a special case of #2. We can derive range
5633 information from the predicate but need to insert different
5634 ASSERT_EXPRs for each of the sub-graphs rooted at the
5635 conditional block. If the last statement of BB is a conditional
5636 expression of the form 'X op Y', then
5638 a) Remove X and Y from the set FOUND_IN_SUBGRAPH.
5640 b) If the conditional is the only entry point to the sub-graph
5641 corresponding to the THEN_CLAUSE, recurse into it. On
5642 return, if X and/or Y are marked in FOUND_IN_SUBGRAPH, then
5643 an ASSERT_EXPR is added for the corresponding variable.
5645 c) Repeat step (b) on the ELSE_CLAUSE.
5647 d) Mark X and Y in FOUND_IN_SUBGRAPH.
5656 In this case, an assertion on the THEN clause is useful to
5657 determine that 'a' is always 9 on that edge. However, an assertion
5658 on the ELSE clause would be unnecessary.
5660 4- If BB does not end in a conditional expression, then we recurse
5661 into BB's dominator children.
5663 At the end of the recursive traversal, every SSA name will have a
5664 list of locations where ASSERT_EXPRs should be added. When a new
5665 location for name N is found, it is registered by calling
5666 register_new_assert_for. That function keeps track of all the
5667 registered assertions to prevent adding unnecessary assertions.
5668 For instance, if a pointer P_4 is dereferenced more than once in a
5669 dominator tree, only the location dominating all the dereference of
5670 P_4 will receive an ASSERT_EXPR.
5672 If this function returns true, then it means that there are names
5673 for which we need to generate ASSERT_EXPRs. Those assertions are
5674 inserted by process_assert_insertions. */
5677 find_assert_locations_1 (basic_block bb
, sbitmap live
)
5679 gimple_stmt_iterator si
;
5683 need_assert
= false;
5684 last
= last_stmt (bb
);
5686 /* If BB's last statement is a conditional statement involving integer
5687 operands, determine if we need to add ASSERT_EXPRs. */
5689 && gimple_code (last
) == GIMPLE_COND
5690 && !fp_predicate (last
)
5691 && !ZERO_SSA_OPERANDS (last
, SSA_OP_USE
))
5692 need_assert
|= find_conditional_asserts (bb
, last
);
5694 /* If BB's last statement is a switch statement involving integer
5695 operands, determine if we need to add ASSERT_EXPRs. */
5697 && gimple_code (last
) == GIMPLE_SWITCH
5698 && !ZERO_SSA_OPERANDS (last
, SSA_OP_USE
))
5699 need_assert
|= find_switch_asserts (bb
, last
);
5701 /* Traverse all the statements in BB marking used names and looking
5702 for statements that may infer assertions for their used operands. */
5703 for (si
= gsi_last_bb (bb
); !gsi_end_p (si
); gsi_prev (&si
))
5709 stmt
= gsi_stmt (si
);
5711 if (is_gimple_debug (stmt
))
5714 /* See if we can derive an assertion for any of STMT's operands. */
5715 FOR_EACH_SSA_TREE_OPERAND (op
, stmt
, i
, SSA_OP_USE
)
5718 enum tree_code comp_code
;
5720 /* If op is not live beyond this stmt, do not bother to insert
5722 if (!bitmap_bit_p (live
, SSA_NAME_VERSION (op
)))
5725 /* If OP is used in such a way that we can infer a value
5726 range for it, and we don't find a previous assertion for
5727 it, create a new assertion location node for OP. */
5728 if (infer_value_range (stmt
, op
, &comp_code
, &value
))
5730 /* If we are able to infer a nonzero value range for OP,
5731 then walk backwards through the use-def chain to see if OP
5732 was set via a typecast.
5734 If so, then we can also infer a nonzero value range
5735 for the operand of the NOP_EXPR. */
5736 if (comp_code
== NE_EXPR
&& integer_zerop (value
))
5739 gimple def_stmt
= SSA_NAME_DEF_STMT (t
);
5741 while (is_gimple_assign (def_stmt
)
5742 && gimple_assign_rhs_code (def_stmt
) == NOP_EXPR
5744 (gimple_assign_rhs1 (def_stmt
)) == SSA_NAME
5746 (TREE_TYPE (gimple_assign_rhs1 (def_stmt
))))
5748 t
= gimple_assign_rhs1 (def_stmt
);
5749 def_stmt
= SSA_NAME_DEF_STMT (t
);
5751 /* Note we want to register the assert for the
5752 operand of the NOP_EXPR after SI, not after the
5754 if (! has_single_use (t
))
5756 register_new_assert_for (t
, t
, comp_code
, value
,
5763 register_new_assert_for (op
, op
, comp_code
, value
, bb
, NULL
, si
);
5769 FOR_EACH_SSA_TREE_OPERAND (op
, stmt
, i
, SSA_OP_USE
)
5770 bitmap_set_bit (live
, SSA_NAME_VERSION (op
));
5771 FOR_EACH_SSA_TREE_OPERAND (op
, stmt
, i
, SSA_OP_DEF
)
5772 bitmap_clear_bit (live
, SSA_NAME_VERSION (op
));
5775 /* Traverse all PHI nodes in BB, updating live. */
5776 for (si
= gsi_start_phis (bb
); !gsi_end_p (si
); gsi_next (&si
))
5778 use_operand_p arg_p
;
5780 gimple phi
= gsi_stmt (si
);
5781 tree res
= gimple_phi_result (phi
);
5783 if (virtual_operand_p (res
))
5786 FOR_EACH_PHI_ARG (arg_p
, phi
, i
, SSA_OP_USE
)
5788 tree arg
= USE_FROM_PTR (arg_p
);
5789 if (TREE_CODE (arg
) == SSA_NAME
)
5790 bitmap_set_bit (live
, SSA_NAME_VERSION (arg
));
5793 bitmap_clear_bit (live
, SSA_NAME_VERSION (res
));
5799 /* Do an RPO walk over the function computing SSA name liveness
5800 on-the-fly and deciding on assert expressions to insert.
5801 Returns true if there are assert expressions to be inserted. */
5804 find_assert_locations (void)
5806 int *rpo
= XNEWVEC (int, last_basic_block
);
5807 int *bb_rpo
= XNEWVEC (int, last_basic_block
);
5808 int *last_rpo
= XCNEWVEC (int, last_basic_block
);
5812 live
= XCNEWVEC (sbitmap
, last_basic_block
);
5813 rpo_cnt
= pre_and_rev_post_order_compute (NULL
, rpo
, false);
5814 for (i
= 0; i
< rpo_cnt
; ++i
)
5817 /* Pre-seed loop latch liveness from loop header PHI nodes. Due to
5818 the order we compute liveness and insert asserts we otherwise
5819 fail to insert asserts into the loop latch. */
5822 FOR_EACH_LOOP (li
, loop
, 0)
5824 i
= loop
->latch
->index
;
5825 unsigned int j
= single_succ_edge (loop
->latch
)->dest_idx
;
5826 for (gimple_stmt_iterator gsi
= gsi_start_phis (loop
->header
);
5827 !gsi_end_p (gsi
); gsi_next (&gsi
))
5829 gimple phi
= gsi_stmt (gsi
);
5830 if (virtual_operand_p (gimple_phi_result (phi
)))
5832 tree arg
= gimple_phi_arg_def (phi
, j
);
5833 if (TREE_CODE (arg
) == SSA_NAME
)
5835 if (live
[i
] == NULL
)
5837 live
[i
] = sbitmap_alloc (num_ssa_names
);
5838 bitmap_clear (live
[i
]);
5840 bitmap_set_bit (live
[i
], SSA_NAME_VERSION (arg
));
5845 need_asserts
= false;
5846 for (i
= rpo_cnt
- 1; i
>= 0; --i
)
5848 basic_block bb
= BASIC_BLOCK (rpo
[i
]);
5854 live
[rpo
[i
]] = sbitmap_alloc (num_ssa_names
);
5855 bitmap_clear (live
[rpo
[i
]]);
5858 /* Process BB and update the live information with uses in
5860 need_asserts
|= find_assert_locations_1 (bb
, live
[rpo
[i
]]);
5862 /* Merge liveness into the predecessor blocks and free it. */
5863 if (!bitmap_empty_p (live
[rpo
[i
]]))
5866 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
5868 int pred
= e
->src
->index
;
5869 if ((e
->flags
& EDGE_DFS_BACK
) || pred
== ENTRY_BLOCK
)
5874 live
[pred
] = sbitmap_alloc (num_ssa_names
);
5875 bitmap_clear (live
[pred
]);
5877 bitmap_ior (live
[pred
], live
[pred
], live
[rpo
[i
]]);
5879 if (bb_rpo
[pred
] < pred_rpo
)
5880 pred_rpo
= bb_rpo
[pred
];
5883 /* Record the RPO number of the last visited block that needs
5884 live information from this block. */
5885 last_rpo
[rpo
[i
]] = pred_rpo
;
5889 sbitmap_free (live
[rpo
[i
]]);
5890 live
[rpo
[i
]] = NULL
;
5893 /* We can free all successors live bitmaps if all their
5894 predecessors have been visited already. */
5895 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
5896 if (last_rpo
[e
->dest
->index
] == i
5897 && live
[e
->dest
->index
])
5899 sbitmap_free (live
[e
->dest
->index
]);
5900 live
[e
->dest
->index
] = NULL
;
5905 XDELETEVEC (bb_rpo
);
5906 XDELETEVEC (last_rpo
);
5907 for (i
= 0; i
< last_basic_block
; ++i
)
5909 sbitmap_free (live
[i
]);
5912 return need_asserts
;
5915 /* Create an ASSERT_EXPR for NAME and insert it in the location
5916 indicated by LOC. Return true if we made any edge insertions. */
5919 process_assert_insertions_for (tree name
, assert_locus_t loc
)
5921 /* Build the comparison expression NAME_i COMP_CODE VAL. */
5928 /* If we have X <=> X do not insert an assert expr for that. */
5929 if (loc
->expr
== loc
->val
)
5932 cond
= build2 (loc
->comp_code
, boolean_type_node
, loc
->expr
, loc
->val
);
5933 assert_stmt
= build_assert_expr_for (cond
, name
);
5936 /* We have been asked to insert the assertion on an edge. This
5937 is used only by COND_EXPR and SWITCH_EXPR assertions. */
5938 gcc_checking_assert (gimple_code (gsi_stmt (loc
->si
)) == GIMPLE_COND
5939 || (gimple_code (gsi_stmt (loc
->si
))
5942 gsi_insert_on_edge (loc
->e
, assert_stmt
);
5946 /* Otherwise, we can insert right after LOC->SI iff the
5947 statement must not be the last statement in the block. */
5948 stmt
= gsi_stmt (loc
->si
);
5949 if (!stmt_ends_bb_p (stmt
))
5951 gsi_insert_after (&loc
->si
, assert_stmt
, GSI_SAME_STMT
);
5955 /* If STMT must be the last statement in BB, we can only insert new
5956 assertions on the non-abnormal edge out of BB. Note that since
5957 STMT is not control flow, there may only be one non-abnormal edge
5959 FOR_EACH_EDGE (e
, ei
, loc
->bb
->succs
)
5960 if (!(e
->flags
& EDGE_ABNORMAL
))
5962 gsi_insert_on_edge (e
, assert_stmt
);
5970 /* Process all the insertions registered for every name N_i registered
5971 in NEED_ASSERT_FOR. The list of assertions to be inserted are
5972 found in ASSERTS_FOR[i]. */
5975 process_assert_insertions (void)
5979 bool update_edges_p
= false;
5980 int num_asserts
= 0;
5982 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
5983 dump_all_asserts (dump_file
);
5985 EXECUTE_IF_SET_IN_BITMAP (need_assert_for
, 0, i
, bi
)
5987 assert_locus_t loc
= asserts_for
[i
];
5992 assert_locus_t next
= loc
->next
;
5993 update_edges_p
|= process_assert_insertions_for (ssa_name (i
), loc
);
6001 gsi_commit_edge_inserts ();
6003 statistics_counter_event (cfun
, "Number of ASSERT_EXPR expressions inserted",
6008 /* Traverse the flowgraph looking for conditional jumps to insert range
6009 expressions. These range expressions are meant to provide information
6010 to optimizations that need to reason in terms of value ranges. They
6011 will not be expanded into RTL. For instance, given:
6020 this pass will transform the code into:
6026 x = ASSERT_EXPR <x, x < y>
6031 y = ASSERT_EXPR <y, x <= y>
6035 The idea is that once copy and constant propagation have run, other
6036 optimizations will be able to determine what ranges of values can 'x'
6037 take in different paths of the code, simply by checking the reaching
6038 definition of 'x'. */
6041 insert_range_assertions (void)
6043 need_assert_for
= BITMAP_ALLOC (NULL
);
6044 asserts_for
= XCNEWVEC (assert_locus_t
, num_ssa_names
);
6046 calculate_dominance_info (CDI_DOMINATORS
);
6048 if (find_assert_locations ())
6050 process_assert_insertions ();
6051 update_ssa (TODO_update_ssa_no_phi
);
6054 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6056 fprintf (dump_file
, "\nSSA form after inserting ASSERT_EXPRs\n");
6057 dump_function_to_file (current_function_decl
, dump_file
, dump_flags
);
6061 BITMAP_FREE (need_assert_for
);
6064 /* Checks one ARRAY_REF in REF, located at LOCUS. Ignores flexible arrays
6065 and "struct" hacks. If VRP can determine that the
6066 array subscript is a constant, check if it is outside valid
6067 range. If the array subscript is a RANGE, warn if it is
6068 non-overlapping with valid range.
6069 IGNORE_OFF_BY_ONE is true if the ARRAY_REF is inside a ADDR_EXPR. */
6072 check_array_ref (location_t location
, tree ref
, bool ignore_off_by_one
)
6074 value_range_t
* vr
= NULL
;
6075 tree low_sub
, up_sub
;
6076 tree low_bound
, up_bound
, up_bound_p1
;
6079 if (TREE_NO_WARNING (ref
))
6082 low_sub
= up_sub
= TREE_OPERAND (ref
, 1);
6083 up_bound
= array_ref_up_bound (ref
);
6085 /* Can not check flexible arrays. */
6087 || TREE_CODE (up_bound
) != INTEGER_CST
)
6090 /* Accesses to trailing arrays via pointers may access storage
6091 beyond the types array bounds. */
6092 base
= get_base_address (ref
);
6093 if (base
&& TREE_CODE (base
) == MEM_REF
)
6095 tree cref
, next
= NULL_TREE
;
6097 if (TREE_CODE (TREE_OPERAND (ref
, 0)) != COMPONENT_REF
)
6100 cref
= TREE_OPERAND (ref
, 0);
6101 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (cref
, 0))) == RECORD_TYPE
)
6102 for (next
= DECL_CHAIN (TREE_OPERAND (cref
, 1));
6103 next
&& TREE_CODE (next
) != FIELD_DECL
;
6104 next
= DECL_CHAIN (next
))
6107 /* If this is the last field in a struct type or a field in a
6108 union type do not warn. */
6113 low_bound
= array_ref_low_bound (ref
);
6114 up_bound_p1
= int_const_binop (PLUS_EXPR
, up_bound
,
6115 build_int_cst (TREE_TYPE (up_bound
), 1));
6117 if (TREE_CODE (low_sub
) == SSA_NAME
)
6119 vr
= get_value_range (low_sub
);
6120 if (vr
->type
== VR_RANGE
|| vr
->type
== VR_ANTI_RANGE
)
6122 low_sub
= vr
->type
== VR_RANGE
? vr
->max
: vr
->min
;
6123 up_sub
= vr
->type
== VR_RANGE
? vr
->min
: vr
->max
;
6127 if (vr
&& vr
->type
== VR_ANTI_RANGE
)
6129 if (TREE_CODE (up_sub
) == INTEGER_CST
6130 && tree_int_cst_lt (up_bound
, up_sub
)
6131 && TREE_CODE (low_sub
) == INTEGER_CST
6132 && tree_int_cst_lt (low_sub
, low_bound
))
6134 warning_at (location
, OPT_Warray_bounds
,
6135 "array subscript is outside array bounds");
6136 TREE_NO_WARNING (ref
) = 1;
6139 else if (TREE_CODE (up_sub
) == INTEGER_CST
6140 && (ignore_off_by_one
6141 ? (tree_int_cst_lt (up_bound
, up_sub
)
6142 && !tree_int_cst_equal (up_bound_p1
, up_sub
))
6143 : (tree_int_cst_lt (up_bound
, up_sub
)
6144 || tree_int_cst_equal (up_bound_p1
, up_sub
))))
6146 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6148 fprintf (dump_file
, "Array bound warning for ");
6149 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, ref
);
6150 fprintf (dump_file
, "\n");
6152 warning_at (location
, OPT_Warray_bounds
,
6153 "array subscript is above array bounds");
6154 TREE_NO_WARNING (ref
) = 1;
6156 else if (TREE_CODE (low_sub
) == INTEGER_CST
6157 && tree_int_cst_lt (low_sub
, low_bound
))
6159 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6161 fprintf (dump_file
, "Array bound warning for ");
6162 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, ref
);
6163 fprintf (dump_file
, "\n");
6165 warning_at (location
, OPT_Warray_bounds
,
6166 "array subscript is below array bounds");
6167 TREE_NO_WARNING (ref
) = 1;
6171 /* Searches if the expr T, located at LOCATION computes
6172 address of an ARRAY_REF, and call check_array_ref on it. */
6175 search_for_addr_array (tree t
, location_t location
)
6177 while (TREE_CODE (t
) == SSA_NAME
)
6179 gimple g
= SSA_NAME_DEF_STMT (t
);
6181 if (gimple_code (g
) != GIMPLE_ASSIGN
)
6184 if (get_gimple_rhs_class (gimple_assign_rhs_code (g
))
6185 != GIMPLE_SINGLE_RHS
)
6188 t
= gimple_assign_rhs1 (g
);
6192 /* We are only interested in addresses of ARRAY_REF's. */
6193 if (TREE_CODE (t
) != ADDR_EXPR
)
6196 /* Check each ARRAY_REFs in the reference chain. */
6199 if (TREE_CODE (t
) == ARRAY_REF
)
6200 check_array_ref (location
, t
, true /*ignore_off_by_one*/);
6202 t
= TREE_OPERAND (t
, 0);
6204 while (handled_component_p (t
));
6206 if (TREE_CODE (t
) == MEM_REF
6207 && TREE_CODE (TREE_OPERAND (t
, 0)) == ADDR_EXPR
6208 && !TREE_NO_WARNING (t
))
6210 tree tem
= TREE_OPERAND (TREE_OPERAND (t
, 0), 0);
6211 tree low_bound
, up_bound
, el_sz
;
6213 if (TREE_CODE (TREE_TYPE (tem
)) != ARRAY_TYPE
6214 || TREE_CODE (TREE_TYPE (TREE_TYPE (tem
))) == ARRAY_TYPE
6215 || !TYPE_DOMAIN (TREE_TYPE (tem
)))
6218 low_bound
= TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (tem
)));
6219 up_bound
= TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (tem
)));
6220 el_sz
= TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (tem
)));
6222 || TREE_CODE (low_bound
) != INTEGER_CST
6224 || TREE_CODE (up_bound
) != INTEGER_CST
6226 || TREE_CODE (el_sz
) != INTEGER_CST
)
6229 idx
= mem_ref_offset (t
);
6230 idx
= wi::sdiv_trunc (idx
, wi::to_offset (el_sz
));
6231 if (wi::lts_p (idx
, 0))
6233 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6235 fprintf (dump_file
, "Array bound warning for ");
6236 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, t
);
6237 fprintf (dump_file
, "\n");
6239 warning_at (location
, OPT_Warray_bounds
,
6240 "array subscript is below array bounds");
6241 TREE_NO_WARNING (t
) = 1;
6243 else if (wi::gts_p (idx
, (wi::to_offset (up_bound
)
6244 - wi::to_offset (low_bound
) + 1)))
6246 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6248 fprintf (dump_file
, "Array bound warning for ");
6249 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, t
);
6250 fprintf (dump_file
, "\n");
6252 warning_at (location
, OPT_Warray_bounds
,
6253 "array subscript is above array bounds");
6254 TREE_NO_WARNING (t
) = 1;
6259 /* walk_tree() callback that checks if *TP is
6260 an ARRAY_REF inside an ADDR_EXPR (in which an array
6261 subscript one outside the valid range is allowed). Call
6262 check_array_ref for each ARRAY_REF found. The location is
6266 check_array_bounds (tree
*tp
, int *walk_subtree
, void *data
)
6269 struct walk_stmt_info
*wi
= (struct walk_stmt_info
*) data
;
6270 location_t location
;
6272 if (EXPR_HAS_LOCATION (t
))
6273 location
= EXPR_LOCATION (t
);
6276 location_t
*locp
= (location_t
*) wi
->info
;
6280 *walk_subtree
= TRUE
;
6282 if (TREE_CODE (t
) == ARRAY_REF
)
6283 check_array_ref (location
, t
, false /*ignore_off_by_one*/);
6285 if (TREE_CODE (t
) == MEM_REF
6286 || (TREE_CODE (t
) == RETURN_EXPR
&& TREE_OPERAND (t
, 0)))
6287 search_for_addr_array (TREE_OPERAND (t
, 0), location
);
6289 if (TREE_CODE (t
) == ADDR_EXPR
)
6290 *walk_subtree
= FALSE
;
6295 /* Walk over all statements of all reachable BBs and call check_array_bounds
6299 check_all_array_refs (void)
6302 gimple_stmt_iterator si
;
6308 bool executable
= false;
6310 /* Skip blocks that were found to be unreachable. */
6311 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
6312 executable
|= !!(e
->flags
& EDGE_EXECUTABLE
);
6316 for (si
= gsi_start_bb (bb
); !gsi_end_p (si
); gsi_next (&si
))
6318 gimple stmt
= gsi_stmt (si
);
6319 struct walk_stmt_info wi
;
6320 if (!gimple_has_location (stmt
))
6323 if (is_gimple_call (stmt
))
6326 size_t n
= gimple_call_num_args (stmt
);
6327 for (i
= 0; i
< n
; i
++)
6329 tree arg
= gimple_call_arg (stmt
, i
);
6330 search_for_addr_array (arg
, gimple_location (stmt
));
6335 memset (&wi
, 0, sizeof (wi
));
6336 wi
.info
= CONST_CAST (void *, (const void *)
6337 gimple_location_ptr (stmt
));
6339 walk_gimple_op (gsi_stmt (si
),
6347 /* Return true if all imm uses of VAR are either in STMT, or
6348 feed (optionally through a chain of single imm uses) GIMPLE_COND
6349 in basic block COND_BB. */
6352 all_imm_uses_in_stmt_or_feed_cond (tree var
, gimple stmt
, basic_block cond_bb
)
6354 use_operand_p use_p
, use2_p
;
6355 imm_use_iterator iter
;
6357 FOR_EACH_IMM_USE_FAST (use_p
, iter
, var
)
6358 if (USE_STMT (use_p
) != stmt
)
6360 gimple use_stmt
= USE_STMT (use_p
), use_stmt2
;
6361 if (is_gimple_debug (use_stmt
))
6363 while (is_gimple_assign (use_stmt
)
6364 && TREE_CODE (gimple_assign_lhs (use_stmt
)) == SSA_NAME
6365 && single_imm_use (gimple_assign_lhs (use_stmt
),
6366 &use2_p
, &use_stmt2
))
6367 use_stmt
= use_stmt2
;
6368 if (gimple_code (use_stmt
) != GIMPLE_COND
6369 || gimple_bb (use_stmt
) != cond_bb
)
6382 __builtin_unreachable ();
6384 x_5 = ASSERT_EXPR <x_3, ...>;
6385 If x_3 has no other immediate uses (checked by caller),
6386 var is the x_3 var from ASSERT_EXPR, we can clear low 5 bits
6387 from the non-zero bitmask. */
6390 maybe_set_nonzero_bits (basic_block bb
, tree var
)
6392 edge e
= single_pred_edge (bb
);
6393 basic_block cond_bb
= e
->src
;
6394 gimple stmt
= last_stmt (cond_bb
);
6398 || gimple_code (stmt
) != GIMPLE_COND
6399 || gimple_cond_code (stmt
) != ((e
->flags
& EDGE_TRUE_VALUE
)
6400 ? EQ_EXPR
: NE_EXPR
)
6401 || TREE_CODE (gimple_cond_lhs (stmt
)) != SSA_NAME
6402 || !integer_zerop (gimple_cond_rhs (stmt
)))
6405 stmt
= SSA_NAME_DEF_STMT (gimple_cond_lhs (stmt
));
6406 if (!is_gimple_assign (stmt
)
6407 || gimple_assign_rhs_code (stmt
) != BIT_AND_EXPR
6408 || TREE_CODE (gimple_assign_rhs2 (stmt
)) != INTEGER_CST
)
6410 if (gimple_assign_rhs1 (stmt
) != var
)
6414 if (TREE_CODE (gimple_assign_rhs1 (stmt
)) != SSA_NAME
)
6416 stmt2
= SSA_NAME_DEF_STMT (gimple_assign_rhs1 (stmt
));
6417 if (!gimple_assign_cast_p (stmt2
)
6418 || gimple_assign_rhs1 (stmt2
) != var
6419 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (stmt2
))
6420 || (TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (stmt
)))
6421 != TYPE_PRECISION (TREE_TYPE (var
))))
6424 cst
= gimple_assign_rhs2 (stmt
);
6425 set_nonzero_bits (var
, (get_nonzero_bits (var
)
6426 & ~wi::to_widest (cst
)));
6429 /* Convert range assertion expressions into the implied copies and
6430 copy propagate away the copies. Doing the trivial copy propagation
6431 here avoids the need to run the full copy propagation pass after
6434 FIXME, this will eventually lead to copy propagation removing the
6435 names that had useful range information attached to them. For
6436 instance, if we had the assertion N_i = ASSERT_EXPR <N_j, N_j > 3>,
6437 then N_i will have the range [3, +INF].
6439 However, by converting the assertion into the implied copy
6440 operation N_i = N_j, we will then copy-propagate N_j into the uses
6441 of N_i and lose the range information. We may want to hold on to
6442 ASSERT_EXPRs a little while longer as the ranges could be used in
6443 things like jump threading.
6445 The problem with keeping ASSERT_EXPRs around is that passes after
6446 VRP need to handle them appropriately.
6448 Another approach would be to make the range information a first
6449 class property of the SSA_NAME so that it can be queried from
6450 any pass. This is made somewhat more complex by the need for
6451 multiple ranges to be associated with one SSA_NAME. */
6454 remove_range_assertions (void)
6457 gimple_stmt_iterator si
;
6458 /* 1 if looking at ASSERT_EXPRs immediately at the beginning of
6459 a basic block preceeded by GIMPLE_COND branching to it and
6460 __builtin_trap, -1 if not yet checked, 0 otherwise. */
6463 /* Note that the BSI iterator bump happens at the bottom of the
6464 loop and no bump is necessary if we're removing the statement
6465 referenced by the current BSI. */
6467 for (si
= gsi_after_labels (bb
), is_unreachable
= -1; !gsi_end_p (si
);)
6469 gimple stmt
= gsi_stmt (si
);
6472 if (is_gimple_assign (stmt
)
6473 && gimple_assign_rhs_code (stmt
) == ASSERT_EXPR
)
6475 tree lhs
= gimple_assign_lhs (stmt
);
6476 tree rhs
= gimple_assign_rhs1 (stmt
);
6478 tree cond
= fold (ASSERT_EXPR_COND (rhs
));
6479 use_operand_p use_p
;
6480 imm_use_iterator iter
;
6482 gcc_assert (cond
!= boolean_false_node
);
6484 var
= ASSERT_EXPR_VAR (rhs
);
6485 gcc_assert (TREE_CODE (var
) == SSA_NAME
);
6487 if (!POINTER_TYPE_P (TREE_TYPE (lhs
))
6488 && SSA_NAME_RANGE_INFO (lhs
))
6490 if (is_unreachable
== -1)
6493 if (single_pred_p (bb
)
6494 && assert_unreachable_fallthru_edge_p
6495 (single_pred_edge (bb
)))
6499 if (x_7 >= 10 && x_7 < 20)
6500 __builtin_unreachable ();
6501 x_8 = ASSERT_EXPR <x_7, ...>;
6502 if the only uses of x_7 are in the ASSERT_EXPR and
6503 in the condition. In that case, we can copy the
6504 range info from x_8 computed in this pass also
6507 && all_imm_uses_in_stmt_or_feed_cond (var
, stmt
,
6510 set_range_info (var
, SSA_NAME_RANGE_INFO (lhs
)->min
,
6511 SSA_NAME_RANGE_INFO (lhs
)->max
);
6512 maybe_set_nonzero_bits (bb
, var
);
6516 /* Propagate the RHS into every use of the LHS. */
6517 FOR_EACH_IMM_USE_STMT (use_stmt
, iter
, lhs
)
6518 FOR_EACH_IMM_USE_ON_STMT (use_p
, iter
)
6519 SET_USE (use_p
, var
);
6521 /* And finally, remove the copy, it is not needed. */
6522 gsi_remove (&si
, true);
6523 release_defs (stmt
);
6534 /* Return true if STMT is interesting for VRP. */
6537 stmt_interesting_for_vrp (gimple stmt
)
6539 if (gimple_code (stmt
) == GIMPLE_PHI
)
6541 tree res
= gimple_phi_result (stmt
);
6542 return (!virtual_operand_p (res
)
6543 && (INTEGRAL_TYPE_P (TREE_TYPE (res
))
6544 || POINTER_TYPE_P (TREE_TYPE (res
))));
6546 else if (is_gimple_assign (stmt
) || is_gimple_call (stmt
))
6548 tree lhs
= gimple_get_lhs (stmt
);
6550 /* In general, assignments with virtual operands are not useful
6551 for deriving ranges, with the obvious exception of calls to
6552 builtin functions. */
6553 if (lhs
&& TREE_CODE (lhs
) == SSA_NAME
6554 && (INTEGRAL_TYPE_P (TREE_TYPE (lhs
))
6555 || POINTER_TYPE_P (TREE_TYPE (lhs
)))
6556 && (is_gimple_call (stmt
)
6557 || !gimple_vuse (stmt
)))
6560 else if (gimple_code (stmt
) == GIMPLE_COND
6561 || gimple_code (stmt
) == GIMPLE_SWITCH
)
6568 /* Initialize local data structures for VRP. */
6571 vrp_initialize (void)
6575 values_propagated
= false;
6576 num_vr_values
= num_ssa_names
;
6577 vr_value
= XCNEWVEC (value_range_t
*, num_vr_values
);
6578 vr_phi_edge_counts
= XCNEWVEC (int, num_ssa_names
);
6582 gimple_stmt_iterator si
;
6584 for (si
= gsi_start_phis (bb
); !gsi_end_p (si
); gsi_next (&si
))
6586 gimple phi
= gsi_stmt (si
);
6587 if (!stmt_interesting_for_vrp (phi
))
6589 tree lhs
= PHI_RESULT (phi
);
6590 set_value_range_to_varying (get_value_range (lhs
));
6591 prop_set_simulate_again (phi
, false);
6594 prop_set_simulate_again (phi
, true);
6597 for (si
= gsi_start_bb (bb
); !gsi_end_p (si
); gsi_next (&si
))
6599 gimple stmt
= gsi_stmt (si
);
6601 /* If the statement is a control insn, then we do not
6602 want to avoid simulating the statement once. Failure
6603 to do so means that those edges will never get added. */
6604 if (stmt_ends_bb_p (stmt
))
6605 prop_set_simulate_again (stmt
, true);
6606 else if (!stmt_interesting_for_vrp (stmt
))
6610 FOR_EACH_SSA_TREE_OPERAND (def
, stmt
, i
, SSA_OP_DEF
)
6611 set_value_range_to_varying (get_value_range (def
));
6612 prop_set_simulate_again (stmt
, false);
6615 prop_set_simulate_again (stmt
, true);
6620 /* Return the singleton value-range for NAME or NAME. */
6623 vrp_valueize (tree name
)
6625 if (TREE_CODE (name
) == SSA_NAME
)
6627 value_range_t
*vr
= get_value_range (name
);
6628 if (vr
->type
== VR_RANGE
6629 && (vr
->min
== vr
->max
6630 || operand_equal_p (vr
->min
, vr
->max
, 0)))
6636 /* Visit assignment STMT. If it produces an interesting range, record
6637 the SSA name in *OUTPUT_P. */
6639 static enum ssa_prop_result
6640 vrp_visit_assignment_or_call (gimple stmt
, tree
*output_p
)
6644 enum gimple_code code
= gimple_code (stmt
);
6645 lhs
= gimple_get_lhs (stmt
);
6647 /* We only keep track of ranges in integral and pointer types. */
6648 if (TREE_CODE (lhs
) == SSA_NAME
6649 && ((INTEGRAL_TYPE_P (TREE_TYPE (lhs
))
6650 /* It is valid to have NULL MIN/MAX values on a type. See
6651 build_range_type. */
6652 && TYPE_MIN_VALUE (TREE_TYPE (lhs
))
6653 && TYPE_MAX_VALUE (TREE_TYPE (lhs
)))
6654 || POINTER_TYPE_P (TREE_TYPE (lhs
))))
6656 value_range_t new_vr
= VR_INITIALIZER
;
6658 /* Try folding the statement to a constant first. */
6659 tree tem
= gimple_fold_stmt_to_constant (stmt
, vrp_valueize
);
6660 if (tem
&& !is_overflow_infinity (tem
))
6661 set_value_range (&new_vr
, VR_RANGE
, tem
, tem
, NULL
);
6662 /* Then dispatch to value-range extracting functions. */
6663 else if (code
== GIMPLE_CALL
)
6664 extract_range_basic (&new_vr
, stmt
);
6666 extract_range_from_assignment (&new_vr
, stmt
);
6668 if (update_value_range (lhs
, &new_vr
))
6672 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6674 fprintf (dump_file
, "Found new range for ");
6675 print_generic_expr (dump_file
, lhs
, 0);
6676 fprintf (dump_file
, ": ");
6677 dump_value_range (dump_file
, &new_vr
);
6678 fprintf (dump_file
, "\n\n");
6681 if (new_vr
.type
== VR_VARYING
)
6682 return SSA_PROP_VARYING
;
6684 return SSA_PROP_INTERESTING
;
6687 return SSA_PROP_NOT_INTERESTING
;
6690 /* Every other statement produces no useful ranges. */
6691 FOR_EACH_SSA_TREE_OPERAND (def
, stmt
, iter
, SSA_OP_DEF
)
6692 set_value_range_to_varying (get_value_range (def
));
6694 return SSA_PROP_VARYING
;
6697 /* Helper that gets the value range of the SSA_NAME with version I
6698 or a symbolic range containing the SSA_NAME only if the value range
6699 is varying or undefined. */
6701 static inline value_range_t
6702 get_vr_for_comparison (int i
)
6704 value_range_t vr
= *get_value_range (ssa_name (i
));
6706 /* If name N_i does not have a valid range, use N_i as its own
6707 range. This allows us to compare against names that may
6708 have N_i in their ranges. */
6709 if (vr
.type
== VR_VARYING
|| vr
.type
== VR_UNDEFINED
)
6712 vr
.min
= ssa_name (i
);
6713 vr
.max
= ssa_name (i
);
6719 /* Compare all the value ranges for names equivalent to VAR with VAL
6720 using comparison code COMP. Return the same value returned by
6721 compare_range_with_value, including the setting of
6722 *STRICT_OVERFLOW_P. */
6725 compare_name_with_value (enum tree_code comp
, tree var
, tree val
,
6726 bool *strict_overflow_p
)
6732 int used_strict_overflow
;
6734 value_range_t equiv_vr
;
6736 /* Get the set of equivalences for VAR. */
6737 e
= get_value_range (var
)->equiv
;
6739 /* Start at -1. Set it to 0 if we do a comparison without relying
6740 on overflow, or 1 if all comparisons rely on overflow. */
6741 used_strict_overflow
= -1;
6743 /* Compare vars' value range with val. */
6744 equiv_vr
= get_vr_for_comparison (SSA_NAME_VERSION (var
));
6746 retval
= compare_range_with_value (comp
, &equiv_vr
, val
, &sop
);
6748 used_strict_overflow
= sop
? 1 : 0;
6750 /* If the equiv set is empty we have done all work we need to do. */
6754 && used_strict_overflow
> 0)
6755 *strict_overflow_p
= true;
6759 EXECUTE_IF_SET_IN_BITMAP (e
, 0, i
, bi
)
6761 equiv_vr
= get_vr_for_comparison (i
);
6763 t
= compare_range_with_value (comp
, &equiv_vr
, val
, &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. */
6779 used_strict_overflow
= 0;
6780 else if (used_strict_overflow
< 0)
6781 used_strict_overflow
= 1;
6786 && used_strict_overflow
> 0)
6787 *strict_overflow_p
= true;
6793 /* Given a comparison code COMP and names N1 and N2, compare all the
6794 ranges equivalent to N1 against all the ranges equivalent to N2
6795 to determine the value of N1 COMP N2. Return the same value
6796 returned by compare_ranges. Set *STRICT_OVERFLOW_P to indicate
6797 whether we relied on an overflow infinity in the comparison. */
6801 compare_names (enum tree_code comp
, tree n1
, tree n2
,
6802 bool *strict_overflow_p
)
6806 bitmap_iterator bi1
, bi2
;
6808 int used_strict_overflow
;
6809 static bitmap_obstack
*s_obstack
= NULL
;
6810 static bitmap s_e1
= NULL
, s_e2
= NULL
;
6812 /* Compare the ranges of every name equivalent to N1 against the
6813 ranges of every name equivalent to N2. */
6814 e1
= get_value_range (n1
)->equiv
;
6815 e2
= get_value_range (n2
)->equiv
;
6817 /* Use the fake bitmaps if e1 or e2 are not available. */
6818 if (s_obstack
== NULL
)
6820 s_obstack
= XNEW (bitmap_obstack
);
6821 bitmap_obstack_initialize (s_obstack
);
6822 s_e1
= BITMAP_ALLOC (s_obstack
);
6823 s_e2
= BITMAP_ALLOC (s_obstack
);
6830 /* Add N1 and N2 to their own set of equivalences to avoid
6831 duplicating the body of the loop just to check N1 and N2
6833 bitmap_set_bit (e1
, SSA_NAME_VERSION (n1
));
6834 bitmap_set_bit (e2
, SSA_NAME_VERSION (n2
));
6836 /* If the equivalence sets have a common intersection, then the two
6837 names can be compared without checking their ranges. */
6838 if (bitmap_intersect_p (e1
, e2
))
6840 bitmap_clear_bit (e1
, SSA_NAME_VERSION (n1
));
6841 bitmap_clear_bit (e2
, SSA_NAME_VERSION (n2
));
6843 return (comp
== EQ_EXPR
|| comp
== GE_EXPR
|| comp
== LE_EXPR
)
6845 : boolean_false_node
;
6848 /* Start at -1. Set it to 0 if we do a comparison without relying
6849 on overflow, or 1 if all comparisons rely on overflow. */
6850 used_strict_overflow
= -1;
6852 /* Otherwise, compare all the equivalent ranges. First, add N1 and
6853 N2 to their own set of equivalences to avoid duplicating the body
6854 of the loop just to check N1 and N2 ranges. */
6855 EXECUTE_IF_SET_IN_BITMAP (e1
, 0, i1
, bi1
)
6857 value_range_t vr1
= get_vr_for_comparison (i1
);
6859 t
= retval
= NULL_TREE
;
6860 EXECUTE_IF_SET_IN_BITMAP (e2
, 0, i2
, bi2
)
6864 value_range_t vr2
= get_vr_for_comparison (i2
);
6866 t
= compare_ranges (comp
, &vr1
, &vr2
, &sop
);
6869 /* If we get different answers from different members
6870 of the equivalence set this check must be in a dead
6871 code region. Folding it to a trap representation
6872 would be correct here. For now just return don't-know. */
6876 bitmap_clear_bit (e1
, SSA_NAME_VERSION (n1
));
6877 bitmap_clear_bit (e2
, SSA_NAME_VERSION (n2
));
6883 used_strict_overflow
= 0;
6884 else if (used_strict_overflow
< 0)
6885 used_strict_overflow
= 1;
6891 bitmap_clear_bit (e1
, SSA_NAME_VERSION (n1
));
6892 bitmap_clear_bit (e2
, SSA_NAME_VERSION (n2
));
6893 if (used_strict_overflow
> 0)
6894 *strict_overflow_p
= true;
6899 /* None of the equivalent ranges are useful in computing this
6901 bitmap_clear_bit (e1
, SSA_NAME_VERSION (n1
));
6902 bitmap_clear_bit (e2
, SSA_NAME_VERSION (n2
));
6906 /* Helper function for vrp_evaluate_conditional_warnv. */
6909 vrp_evaluate_conditional_warnv_with_ops_using_ranges (enum tree_code code
,
6911 bool * strict_overflow_p
)
6913 value_range_t
*vr0
, *vr1
;
6915 vr0
= (TREE_CODE (op0
) == SSA_NAME
) ? get_value_range (op0
) : NULL
;
6916 vr1
= (TREE_CODE (op1
) == SSA_NAME
) ? get_value_range (op1
) : NULL
;
6919 return compare_ranges (code
, vr0
, vr1
, strict_overflow_p
);
6920 else if (vr0
&& vr1
== NULL
)
6921 return compare_range_with_value (code
, vr0
, op1
, strict_overflow_p
);
6922 else if (vr0
== NULL
&& vr1
)
6923 return (compare_range_with_value
6924 (swap_tree_comparison (code
), vr1
, op0
, strict_overflow_p
));
6928 /* Helper function for vrp_evaluate_conditional_warnv. */
6931 vrp_evaluate_conditional_warnv_with_ops (enum tree_code code
, tree op0
,
6932 tree op1
, bool use_equiv_p
,
6933 bool *strict_overflow_p
, bool *only_ranges
)
6937 *only_ranges
= true;
6939 /* We only deal with integral and pointer types. */
6940 if (!INTEGRAL_TYPE_P (TREE_TYPE (op0
))
6941 && !POINTER_TYPE_P (TREE_TYPE (op0
)))
6947 && (ret
= vrp_evaluate_conditional_warnv_with_ops_using_ranges
6948 (code
, op0
, op1
, strict_overflow_p
)))
6950 *only_ranges
= false;
6951 if (TREE_CODE (op0
) == SSA_NAME
&& TREE_CODE (op1
) == SSA_NAME
)
6952 return compare_names (code
, op0
, op1
, strict_overflow_p
);
6953 else if (TREE_CODE (op0
) == SSA_NAME
)
6954 return compare_name_with_value (code
, op0
, op1
, strict_overflow_p
);
6955 else if (TREE_CODE (op1
) == SSA_NAME
)
6956 return (compare_name_with_value
6957 (swap_tree_comparison (code
), op1
, op0
, strict_overflow_p
));
6960 return vrp_evaluate_conditional_warnv_with_ops_using_ranges (code
, op0
, op1
,
6965 /* Given (CODE OP0 OP1) within STMT, try to simplify it based on value range
6966 information. Return NULL if the conditional can not be evaluated.
6967 The ranges of all the names equivalent with the operands in COND
6968 will be used when trying to compute the value. If the result is
6969 based on undefined signed overflow, issue a warning if
6973 vrp_evaluate_conditional (enum tree_code code
, tree op0
, tree op1
, gimple stmt
)
6979 /* Some passes and foldings leak constants with overflow flag set
6980 into the IL. Avoid doing wrong things with these and bail out. */
6981 if ((TREE_CODE (op0
) == INTEGER_CST
6982 && TREE_OVERFLOW (op0
))
6983 || (TREE_CODE (op1
) == INTEGER_CST
6984 && TREE_OVERFLOW (op1
)))
6988 ret
= vrp_evaluate_conditional_warnv_with_ops (code
, op0
, op1
, true, &sop
,
6993 enum warn_strict_overflow_code wc
;
6994 const char* warnmsg
;
6996 if (is_gimple_min_invariant (ret
))
6998 wc
= WARN_STRICT_OVERFLOW_CONDITIONAL
;
6999 warnmsg
= G_("assuming signed overflow does not occur when "
7000 "simplifying conditional to constant");
7004 wc
= WARN_STRICT_OVERFLOW_COMPARISON
;
7005 warnmsg
= G_("assuming signed overflow does not occur when "
7006 "simplifying conditional");
7009 if (issue_strict_overflow_warning (wc
))
7011 location_t location
;
7013 if (!gimple_has_location (stmt
))
7014 location
= input_location
;
7016 location
= gimple_location (stmt
);
7017 warning_at (location
, OPT_Wstrict_overflow
, "%s", warnmsg
);
7021 if (warn_type_limits
7022 && ret
&& only_ranges
7023 && TREE_CODE_CLASS (code
) == tcc_comparison
7024 && TREE_CODE (op0
) == SSA_NAME
)
7026 /* If the comparison is being folded and the operand on the LHS
7027 is being compared against a constant value that is outside of
7028 the natural range of OP0's type, then the predicate will
7029 always fold regardless of the value of OP0. If -Wtype-limits
7030 was specified, emit a warning. */
7031 tree type
= TREE_TYPE (op0
);
7032 value_range_t
*vr0
= get_value_range (op0
);
7034 if (vr0
->type
!= VR_VARYING
7035 && INTEGRAL_TYPE_P (type
)
7036 && vrp_val_is_min (vr0
->min
)
7037 && vrp_val_is_max (vr0
->max
)
7038 && is_gimple_min_invariant (op1
))
7040 location_t location
;
7042 if (!gimple_has_location (stmt
))
7043 location
= input_location
;
7045 location
= gimple_location (stmt
);
7047 warning_at (location
, OPT_Wtype_limits
,
7049 ? G_("comparison always false "
7050 "due to limited range of data type")
7051 : G_("comparison always true "
7052 "due to limited range of data type"));
7060 /* Visit conditional statement STMT. If we can determine which edge
7061 will be taken out of STMT's basic block, record it in
7062 *TAKEN_EDGE_P and return SSA_PROP_INTERESTING. Otherwise, return
7063 SSA_PROP_VARYING. */
7065 static enum ssa_prop_result
7066 vrp_visit_cond_stmt (gimple stmt
, edge
*taken_edge_p
)
7071 *taken_edge_p
= NULL
;
7073 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7078 fprintf (dump_file
, "\nVisiting conditional with predicate: ");
7079 print_gimple_stmt (dump_file
, stmt
, 0, 0);
7080 fprintf (dump_file
, "\nWith known ranges\n");
7082 FOR_EACH_SSA_TREE_OPERAND (use
, stmt
, i
, SSA_OP_USE
)
7084 fprintf (dump_file
, "\t");
7085 print_generic_expr (dump_file
, use
, 0);
7086 fprintf (dump_file
, ": ");
7087 dump_value_range (dump_file
, vr_value
[SSA_NAME_VERSION (use
)]);
7090 fprintf (dump_file
, "\n");
7093 /* Compute the value of the predicate COND by checking the known
7094 ranges of each of its operands.
7096 Note that we cannot evaluate all the equivalent ranges here
7097 because those ranges may not yet be final and with the current
7098 propagation strategy, we cannot determine when the value ranges
7099 of the names in the equivalence set have changed.
7101 For instance, given the following code fragment
7105 i_14 = ASSERT_EXPR <i_5, i_5 != 0>
7109 Assume that on the first visit to i_14, i_5 has the temporary
7110 range [8, 8] because the second argument to the PHI function is
7111 not yet executable. We derive the range ~[0, 0] for i_14 and the
7112 equivalence set { i_5 }. So, when we visit 'if (i_14 == 1)' for
7113 the first time, since i_14 is equivalent to the range [8, 8], we
7114 determine that the predicate is always false.
7116 On the next round of propagation, i_13 is determined to be
7117 VARYING, which causes i_5 to drop down to VARYING. So, another
7118 visit to i_14 is scheduled. In this second visit, we compute the
7119 exact same range and equivalence set for i_14, namely ~[0, 0] and
7120 { i_5 }. But we did not have the previous range for i_5
7121 registered, so vrp_visit_assignment thinks that the range for
7122 i_14 has not changed. Therefore, the predicate 'if (i_14 == 1)'
7123 is not visited again, which stops propagation from visiting
7124 statements in the THEN clause of that if().
7126 To properly fix this we would need to keep the previous range
7127 value for the names in the equivalence set. This way we would've
7128 discovered that from one visit to the other i_5 changed from
7129 range [8, 8] to VR_VARYING.
7131 However, fixing this apparent limitation may not be worth the
7132 additional checking. Testing on several code bases (GCC, DLV,
7133 MICO, TRAMP3D and SPEC2000) showed that doing this results in
7134 4 more predicates folded in SPEC. */
7137 val
= vrp_evaluate_conditional_warnv_with_ops (gimple_cond_code (stmt
),
7138 gimple_cond_lhs (stmt
),
7139 gimple_cond_rhs (stmt
),
7144 *taken_edge_p
= find_taken_edge (gimple_bb (stmt
), val
);
7147 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7149 "\nIgnoring predicate evaluation because "
7150 "it assumes that signed overflow is undefined");
7155 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7157 fprintf (dump_file
, "\nPredicate evaluates to: ");
7158 if (val
== NULL_TREE
)
7159 fprintf (dump_file
, "DON'T KNOW\n");
7161 print_generic_stmt (dump_file
, val
, 0);
7164 return (*taken_edge_p
) ? SSA_PROP_INTERESTING
: SSA_PROP_VARYING
;
7167 /* Searches the case label vector VEC for the index *IDX of the CASE_LABEL
7168 that includes the value VAL. The search is restricted to the range
7169 [START_IDX, n - 1] where n is the size of VEC.
7171 If there is a CASE_LABEL for VAL, its index is placed in IDX and true is
7174 If there is no CASE_LABEL for VAL and there is one that is larger than VAL,
7175 it is placed in IDX and false is returned.
7177 If VAL is larger than any CASE_LABEL, n is placed on IDX and false is
7181 find_case_label_index (gimple stmt
, size_t start_idx
, tree val
, size_t *idx
)
7183 size_t n
= gimple_switch_num_labels (stmt
);
7186 /* Find case label for minimum of the value range or the next one.
7187 At each iteration we are searching in [low, high - 1]. */
7189 for (low
= start_idx
, high
= n
; high
!= low
; )
7193 /* Note that i != high, so we never ask for n. */
7194 size_t i
= (high
+ low
) / 2;
7195 t
= gimple_switch_label (stmt
, i
);
7197 /* Cache the result of comparing CASE_LOW and val. */
7198 cmp
= tree_int_cst_compare (CASE_LOW (t
), val
);
7202 /* Ranges cannot be empty. */
7211 if (CASE_HIGH (t
) != NULL
7212 && tree_int_cst_compare (CASE_HIGH (t
), val
) >= 0)
7224 /* Searches the case label vector VEC for the range of CASE_LABELs that is used
7225 for values between MIN and MAX. The first index is placed in MIN_IDX. The
7226 last index is placed in MAX_IDX. If the range of CASE_LABELs is empty
7227 then MAX_IDX < MIN_IDX.
7228 Returns true if the default label is not needed. */
7231 find_case_label_range (gimple stmt
, tree min
, tree max
, size_t *min_idx
,
7235 bool min_take_default
= !find_case_label_index (stmt
, 1, min
, &i
);
7236 bool max_take_default
= !find_case_label_index (stmt
, i
, max
, &j
);
7240 && max_take_default
)
7242 /* Only the default case label reached.
7243 Return an empty range. */
7250 bool take_default
= min_take_default
|| max_take_default
;
7254 if (max_take_default
)
7257 /* If the case label range is continuous, we do not need
7258 the default case label. Verify that. */
7259 high
= CASE_LOW (gimple_switch_label (stmt
, i
));
7260 if (CASE_HIGH (gimple_switch_label (stmt
, i
)))
7261 high
= CASE_HIGH (gimple_switch_label (stmt
, i
));
7262 for (k
= i
+ 1; k
<= j
; ++k
)
7264 low
= CASE_LOW (gimple_switch_label (stmt
, k
));
7265 if (!integer_onep (int_const_binop (MINUS_EXPR
, low
, high
)))
7267 take_default
= true;
7271 if (CASE_HIGH (gimple_switch_label (stmt
, k
)))
7272 high
= CASE_HIGH (gimple_switch_label (stmt
, k
));
7277 return !take_default
;
7281 /* Searches the case label vector VEC for the ranges of CASE_LABELs that are
7282 used in range VR. The indices are placed in MIN_IDX1, MAX_IDX, MIN_IDX2 and
7283 MAX_IDX2. If the ranges of CASE_LABELs are empty then MAX_IDX1 < MIN_IDX1.
7284 Returns true if the default label is not needed. */
7287 find_case_label_ranges (gimple stmt
, value_range_t
*vr
, size_t *min_idx1
,
7288 size_t *max_idx1
, size_t *min_idx2
,
7292 unsigned int n
= gimple_switch_num_labels (stmt
);
7294 tree case_low
, case_high
;
7295 tree min
= vr
->min
, max
= vr
->max
;
7297 gcc_checking_assert (vr
->type
== VR_RANGE
|| vr
->type
== VR_ANTI_RANGE
);
7299 take_default
= !find_case_label_range (stmt
, min
, max
, &i
, &j
);
7301 /* Set second range to emtpy. */
7305 if (vr
->type
== VR_RANGE
)
7309 return !take_default
;
7312 /* Set first range to all case labels. */
7319 /* Make sure all the values of case labels [i , j] are contained in
7320 range [MIN, MAX]. */
7321 case_low
= CASE_LOW (gimple_switch_label (stmt
, i
));
7322 case_high
= CASE_HIGH (gimple_switch_label (stmt
, j
));
7323 if (tree_int_cst_compare (case_low
, min
) < 0)
7325 if (case_high
!= NULL_TREE
7326 && tree_int_cst_compare (max
, case_high
) < 0)
7332 /* If the range spans case labels [i, j], the corresponding anti-range spans
7333 the labels [1, i - 1] and [j + 1, n - 1]. */
7359 /* Visit switch statement STMT. If we can determine which edge
7360 will be taken out of STMT's basic block, record it in
7361 *TAKEN_EDGE_P and return SSA_PROP_INTERESTING. Otherwise, return
7362 SSA_PROP_VARYING. */
7364 static enum ssa_prop_result
7365 vrp_visit_switch_stmt (gimple stmt
, edge
*taken_edge_p
)
7369 size_t i
= 0, j
= 0, k
, l
;
7372 *taken_edge_p
= NULL
;
7373 op
= gimple_switch_index (stmt
);
7374 if (TREE_CODE (op
) != SSA_NAME
)
7375 return SSA_PROP_VARYING
;
7377 vr
= get_value_range (op
);
7378 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7380 fprintf (dump_file
, "\nVisiting switch expression with operand ");
7381 print_generic_expr (dump_file
, op
, 0);
7382 fprintf (dump_file
, " with known range ");
7383 dump_value_range (dump_file
, vr
);
7384 fprintf (dump_file
, "\n");
7387 if ((vr
->type
!= VR_RANGE
7388 && vr
->type
!= VR_ANTI_RANGE
)
7389 || symbolic_range_p (vr
))
7390 return SSA_PROP_VARYING
;
7392 /* Find the single edge that is taken from the switch expression. */
7393 take_default
= !find_case_label_ranges (stmt
, vr
, &i
, &j
, &k
, &l
);
7395 /* Check if the range spans no CASE_LABEL. If so, we only reach the default
7399 gcc_assert (take_default
);
7400 val
= gimple_switch_default_label (stmt
);
7404 /* Check if labels with index i to j and maybe the default label
7405 are all reaching the same label. */
7407 val
= gimple_switch_label (stmt
, i
);
7409 && CASE_LABEL (gimple_switch_default_label (stmt
))
7410 != CASE_LABEL (val
))
7412 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7413 fprintf (dump_file
, " not a single destination for this "
7415 return SSA_PROP_VARYING
;
7417 for (++i
; i
<= j
; ++i
)
7419 if (CASE_LABEL (gimple_switch_label (stmt
, i
)) != CASE_LABEL (val
))
7421 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7422 fprintf (dump_file
, " not a single destination for this "
7424 return SSA_PROP_VARYING
;
7429 if (CASE_LABEL (gimple_switch_label (stmt
, k
)) != CASE_LABEL (val
))
7431 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7432 fprintf (dump_file
, " not a single destination for this "
7434 return SSA_PROP_VARYING
;
7439 *taken_edge_p
= find_edge (gimple_bb (stmt
),
7440 label_to_block (CASE_LABEL (val
)));
7442 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7444 fprintf (dump_file
, " will take edge to ");
7445 print_generic_stmt (dump_file
, CASE_LABEL (val
), 0);
7448 return SSA_PROP_INTERESTING
;
7452 /* Evaluate statement STMT. If the statement produces a useful range,
7453 return SSA_PROP_INTERESTING and record the SSA name with the
7454 interesting range into *OUTPUT_P.
7456 If STMT is a conditional branch and we can determine its truth
7457 value, the taken edge is recorded in *TAKEN_EDGE_P.
7459 If STMT produces a varying value, return SSA_PROP_VARYING. */
7461 static enum ssa_prop_result
7462 vrp_visit_stmt (gimple stmt
, edge
*taken_edge_p
, tree
*output_p
)
7467 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7469 fprintf (dump_file
, "\nVisiting statement:\n");
7470 print_gimple_stmt (dump_file
, stmt
, 0, dump_flags
);
7471 fprintf (dump_file
, "\n");
7474 if (!stmt_interesting_for_vrp (stmt
))
7475 gcc_assert (stmt_ends_bb_p (stmt
));
7476 else if (is_gimple_assign (stmt
) || is_gimple_call (stmt
))
7477 return vrp_visit_assignment_or_call (stmt
, output_p
);
7478 else if (gimple_code (stmt
) == GIMPLE_COND
)
7479 return vrp_visit_cond_stmt (stmt
, taken_edge_p
);
7480 else if (gimple_code (stmt
) == GIMPLE_SWITCH
)
7481 return vrp_visit_switch_stmt (stmt
, taken_edge_p
);
7483 /* All other statements produce nothing of interest for VRP, so mark
7484 their outputs varying and prevent further simulation. */
7485 FOR_EACH_SSA_TREE_OPERAND (def
, stmt
, iter
, SSA_OP_DEF
)
7486 set_value_range_to_varying (get_value_range (def
));
7488 return SSA_PROP_VARYING
;
7491 /* Union the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
7492 { VR1TYPE, VR0MIN, VR0MAX } and store the result
7493 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
7494 possible such range. The resulting range is not canonicalized. */
7497 union_ranges (enum value_range_type
*vr0type
,
7498 tree
*vr0min
, tree
*vr0max
,
7499 enum value_range_type vr1type
,
7500 tree vr1min
, tree vr1max
)
7502 bool mineq
= operand_equal_p (*vr0min
, vr1min
, 0);
7503 bool maxeq
= operand_equal_p (*vr0max
, vr1max
, 0);
7505 /* [] is vr0, () is vr1 in the following classification comments. */
7509 if (*vr0type
== vr1type
)
7510 /* Nothing to do for equal ranges. */
7512 else if ((*vr0type
== VR_RANGE
7513 && vr1type
== VR_ANTI_RANGE
)
7514 || (*vr0type
== VR_ANTI_RANGE
7515 && vr1type
== VR_RANGE
))
7517 /* For anti-range with range union the result is varying. */
7523 else if (operand_less_p (*vr0max
, vr1min
) == 1
7524 || operand_less_p (vr1max
, *vr0min
) == 1)
7526 /* [ ] ( ) or ( ) [ ]
7527 If the ranges have an empty intersection, result of the union
7528 operation is the anti-range or if both are anti-ranges
7530 if (*vr0type
== VR_ANTI_RANGE
7531 && vr1type
== VR_ANTI_RANGE
)
7533 else if (*vr0type
== VR_ANTI_RANGE
7534 && vr1type
== VR_RANGE
)
7536 else if (*vr0type
== VR_RANGE
7537 && vr1type
== VR_ANTI_RANGE
)
7543 else if (*vr0type
== VR_RANGE
7544 && vr1type
== VR_RANGE
)
7546 /* The result is the convex hull of both ranges. */
7547 if (operand_less_p (*vr0max
, vr1min
) == 1)
7549 /* If the result can be an anti-range, create one. */
7550 if (TREE_CODE (*vr0max
) == INTEGER_CST
7551 && TREE_CODE (vr1min
) == INTEGER_CST
7552 && vrp_val_is_min (*vr0min
)
7553 && vrp_val_is_max (vr1max
))
7555 tree min
= int_const_binop (PLUS_EXPR
,
7557 build_int_cst (TREE_TYPE (*vr0max
), 1));
7558 tree max
= int_const_binop (MINUS_EXPR
,
7560 build_int_cst (TREE_TYPE (vr1min
), 1));
7561 if (!operand_less_p (max
, min
))
7563 *vr0type
= VR_ANTI_RANGE
;
7575 /* If the result can be an anti-range, create one. */
7576 if (TREE_CODE (vr1max
) == INTEGER_CST
7577 && TREE_CODE (*vr0min
) == INTEGER_CST
7578 && vrp_val_is_min (vr1min
)
7579 && vrp_val_is_max (*vr0max
))
7581 tree min
= int_const_binop (PLUS_EXPR
,
7583 build_int_cst (TREE_TYPE (vr1max
), 1));
7584 tree max
= int_const_binop (MINUS_EXPR
,
7586 build_int_cst (TREE_TYPE (*vr0min
), 1));
7587 if (!operand_less_p (max
, min
))
7589 *vr0type
= VR_ANTI_RANGE
;
7603 else if ((maxeq
|| operand_less_p (vr1max
, *vr0max
) == 1)
7604 && (mineq
|| operand_less_p (*vr0min
, vr1min
) == 1))
7606 /* [ ( ) ] or [( ) ] or [ ( )] */
7607 if (*vr0type
== VR_RANGE
7608 && vr1type
== VR_RANGE
)
7610 else if (*vr0type
== VR_ANTI_RANGE
7611 && vr1type
== VR_ANTI_RANGE
)
7617 else if (*vr0type
== VR_ANTI_RANGE
7618 && vr1type
== VR_RANGE
)
7620 /* Arbitrarily choose the right or left gap. */
7621 if (!mineq
&& TREE_CODE (vr1min
) == INTEGER_CST
)
7622 *vr0max
= int_const_binop (MINUS_EXPR
, vr1min
,
7623 build_int_cst (TREE_TYPE (vr1min
), 1));
7624 else if (!maxeq
&& TREE_CODE (vr1max
) == INTEGER_CST
)
7625 *vr0min
= int_const_binop (PLUS_EXPR
, vr1max
,
7626 build_int_cst (TREE_TYPE (vr1max
), 1));
7630 else if (*vr0type
== VR_RANGE
7631 && vr1type
== VR_ANTI_RANGE
)
7632 /* The result covers everything. */
7637 else if ((maxeq
|| operand_less_p (*vr0max
, vr1max
) == 1)
7638 && (mineq
|| operand_less_p (vr1min
, *vr0min
) == 1))
7640 /* ( [ ] ) or ([ ] ) or ( [ ]) */
7641 if (*vr0type
== VR_RANGE
7642 && vr1type
== VR_RANGE
)
7648 else if (*vr0type
== VR_ANTI_RANGE
7649 && vr1type
== VR_ANTI_RANGE
)
7651 else if (*vr0type
== VR_RANGE
7652 && vr1type
== VR_ANTI_RANGE
)
7654 *vr0type
= VR_ANTI_RANGE
;
7655 if (!mineq
&& TREE_CODE (*vr0min
) == INTEGER_CST
)
7657 *vr0max
= int_const_binop (MINUS_EXPR
, *vr0min
,
7658 build_int_cst (TREE_TYPE (*vr0min
), 1));
7661 else if (!maxeq
&& TREE_CODE (*vr0max
) == INTEGER_CST
)
7663 *vr0min
= int_const_binop (PLUS_EXPR
, *vr0max
,
7664 build_int_cst (TREE_TYPE (*vr0max
), 1));
7670 else if (*vr0type
== VR_ANTI_RANGE
7671 && vr1type
== VR_RANGE
)
7672 /* The result covers everything. */
7677 else if ((operand_less_p (vr1min
, *vr0max
) == 1
7678 || operand_equal_p (vr1min
, *vr0max
, 0))
7679 && operand_less_p (*vr0min
, vr1min
) == 1)
7681 /* [ ( ] ) or [ ]( ) */
7682 if (*vr0type
== VR_RANGE
7683 && vr1type
== VR_RANGE
)
7685 else if (*vr0type
== VR_ANTI_RANGE
7686 && vr1type
== VR_ANTI_RANGE
)
7688 else if (*vr0type
== VR_ANTI_RANGE
7689 && vr1type
== VR_RANGE
)
7691 if (TREE_CODE (vr1min
) == INTEGER_CST
)
7692 *vr0max
= int_const_binop (MINUS_EXPR
, vr1min
,
7693 build_int_cst (TREE_TYPE (vr1min
), 1));
7697 else if (*vr0type
== VR_RANGE
7698 && vr1type
== VR_ANTI_RANGE
)
7700 if (TREE_CODE (*vr0max
) == INTEGER_CST
)
7703 *vr0min
= int_const_binop (PLUS_EXPR
, *vr0max
,
7704 build_int_cst (TREE_TYPE (*vr0max
), 1));
7713 else if ((operand_less_p (*vr0min
, vr1max
) == 1
7714 || operand_equal_p (*vr0min
, vr1max
, 0))
7715 && operand_less_p (vr1min
, *vr0min
) == 1)
7717 /* ( [ ) ] or ( )[ ] */
7718 if (*vr0type
== VR_RANGE
7719 && vr1type
== VR_RANGE
)
7721 else if (*vr0type
== VR_ANTI_RANGE
7722 && vr1type
== VR_ANTI_RANGE
)
7724 else if (*vr0type
== VR_ANTI_RANGE
7725 && vr1type
== VR_RANGE
)
7727 if (TREE_CODE (vr1max
) == INTEGER_CST
)
7728 *vr0min
= int_const_binop (PLUS_EXPR
, vr1max
,
7729 build_int_cst (TREE_TYPE (vr1max
), 1));
7733 else if (*vr0type
== VR_RANGE
7734 && vr1type
== VR_ANTI_RANGE
)
7736 if (TREE_CODE (*vr0min
) == INTEGER_CST
)
7740 *vr0max
= int_const_binop (MINUS_EXPR
, *vr0min
,
7741 build_int_cst (TREE_TYPE (*vr0min
), 1));
7755 *vr0type
= VR_VARYING
;
7756 *vr0min
= NULL_TREE
;
7757 *vr0max
= NULL_TREE
;
7760 /* Intersect the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
7761 { VR1TYPE, VR0MIN, VR0MAX } and store the result
7762 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
7763 possible such range. The resulting range is not canonicalized. */
7766 intersect_ranges (enum value_range_type
*vr0type
,
7767 tree
*vr0min
, tree
*vr0max
,
7768 enum value_range_type vr1type
,
7769 tree vr1min
, tree vr1max
)
7771 bool mineq
= operand_equal_p (*vr0min
, vr1min
, 0);
7772 bool maxeq
= operand_equal_p (*vr0max
, vr1max
, 0);
7774 /* [] is vr0, () is vr1 in the following classification comments. */
7778 if (*vr0type
== vr1type
)
7779 /* Nothing to do for equal ranges. */
7781 else if ((*vr0type
== VR_RANGE
7782 && vr1type
== VR_ANTI_RANGE
)
7783 || (*vr0type
== VR_ANTI_RANGE
7784 && vr1type
== VR_RANGE
))
7786 /* For anti-range with range intersection the result is empty. */
7787 *vr0type
= VR_UNDEFINED
;
7788 *vr0min
= NULL_TREE
;
7789 *vr0max
= NULL_TREE
;
7794 else if (operand_less_p (*vr0max
, vr1min
) == 1
7795 || operand_less_p (vr1max
, *vr0min
) == 1)
7797 /* [ ] ( ) or ( ) [ ]
7798 If the ranges have an empty intersection, the result of the
7799 intersect operation is the range for intersecting an
7800 anti-range with a range or empty when intersecting two ranges. */
7801 if (*vr0type
== VR_RANGE
7802 && vr1type
== VR_ANTI_RANGE
)
7804 else if (*vr0type
== VR_ANTI_RANGE
7805 && vr1type
== VR_RANGE
)
7811 else if (*vr0type
== VR_RANGE
7812 && vr1type
== VR_RANGE
)
7814 *vr0type
= VR_UNDEFINED
;
7815 *vr0min
= NULL_TREE
;
7816 *vr0max
= NULL_TREE
;
7818 else if (*vr0type
== VR_ANTI_RANGE
7819 && vr1type
== VR_ANTI_RANGE
)
7821 /* If the anti-ranges are adjacent to each other merge them. */
7822 if (TREE_CODE (*vr0max
) == INTEGER_CST
7823 && TREE_CODE (vr1min
) == INTEGER_CST
7824 && operand_less_p (*vr0max
, vr1min
) == 1
7825 && integer_onep (int_const_binop (MINUS_EXPR
,
7828 else if (TREE_CODE (vr1max
) == INTEGER_CST
7829 && TREE_CODE (*vr0min
) == INTEGER_CST
7830 && operand_less_p (vr1max
, *vr0min
) == 1
7831 && integer_onep (int_const_binop (MINUS_EXPR
,
7834 /* Else arbitrarily take VR0. */
7837 else if ((maxeq
|| operand_less_p (vr1max
, *vr0max
) == 1)
7838 && (mineq
|| operand_less_p (*vr0min
, vr1min
) == 1))
7840 /* [ ( ) ] or [( ) ] or [ ( )] */
7841 if (*vr0type
== VR_RANGE
7842 && vr1type
== VR_RANGE
)
7844 /* If both are ranges the result is the inner one. */
7849 else if (*vr0type
== VR_RANGE
7850 && vr1type
== VR_ANTI_RANGE
)
7852 /* Choose the right gap if the left one is empty. */
7855 if (TREE_CODE (vr1max
) == INTEGER_CST
)
7856 *vr0min
= int_const_binop (PLUS_EXPR
, vr1max
,
7857 build_int_cst (TREE_TYPE (vr1max
), 1));
7861 /* Choose the left gap if the right one is empty. */
7864 if (TREE_CODE (vr1min
) == INTEGER_CST
)
7865 *vr0max
= int_const_binop (MINUS_EXPR
, vr1min
,
7866 build_int_cst (TREE_TYPE (vr1min
), 1));
7870 /* Choose the anti-range if the range is effectively varying. */
7871 else if (vrp_val_is_min (*vr0min
)
7872 && vrp_val_is_max (*vr0max
))
7878 /* Else choose the range. */
7880 else if (*vr0type
== VR_ANTI_RANGE
7881 && vr1type
== VR_ANTI_RANGE
)
7882 /* If both are anti-ranges the result is the outer one. */
7884 else if (*vr0type
== VR_ANTI_RANGE
7885 && vr1type
== VR_RANGE
)
7887 /* The intersection is empty. */
7888 *vr0type
= VR_UNDEFINED
;
7889 *vr0min
= NULL_TREE
;
7890 *vr0max
= NULL_TREE
;
7895 else if ((maxeq
|| operand_less_p (*vr0max
, vr1max
) == 1)
7896 && (mineq
|| operand_less_p (vr1min
, *vr0min
) == 1))
7898 /* ( [ ] ) or ([ ] ) or ( [ ]) */
7899 if (*vr0type
== VR_RANGE
7900 && vr1type
== VR_RANGE
)
7901 /* Choose the inner range. */
7903 else if (*vr0type
== VR_ANTI_RANGE
7904 && vr1type
== VR_RANGE
)
7906 /* Choose the right gap if the left is empty. */
7909 *vr0type
= VR_RANGE
;
7910 if (TREE_CODE (*vr0max
) == INTEGER_CST
)
7911 *vr0min
= int_const_binop (PLUS_EXPR
, *vr0max
,
7912 build_int_cst (TREE_TYPE (*vr0max
), 1));
7917 /* Choose the left gap if the right is empty. */
7920 *vr0type
= VR_RANGE
;
7921 if (TREE_CODE (*vr0min
) == INTEGER_CST
)
7922 *vr0max
= int_const_binop (MINUS_EXPR
, *vr0min
,
7923 build_int_cst (TREE_TYPE (*vr0min
), 1));
7928 /* Choose the anti-range if the range is effectively varying. */
7929 else if (vrp_val_is_min (vr1min
)
7930 && vrp_val_is_max (vr1max
))
7932 /* Else choose the range. */
7940 else if (*vr0type
== VR_ANTI_RANGE
7941 && vr1type
== VR_ANTI_RANGE
)
7943 /* If both are anti-ranges the result is the outer one. */
7948 else if (vr1type
== VR_ANTI_RANGE
7949 && *vr0type
== VR_RANGE
)
7951 /* The intersection is empty. */
7952 *vr0type
= VR_UNDEFINED
;
7953 *vr0min
= NULL_TREE
;
7954 *vr0max
= NULL_TREE
;
7959 else if ((operand_less_p (vr1min
, *vr0max
) == 1
7960 || operand_equal_p (vr1min
, *vr0max
, 0))
7961 && operand_less_p (*vr0min
, vr1min
) == 1)
7963 /* [ ( ] ) or [ ]( ) */
7964 if (*vr0type
== VR_ANTI_RANGE
7965 && vr1type
== VR_ANTI_RANGE
)
7967 else if (*vr0type
== VR_RANGE
7968 && vr1type
== VR_RANGE
)
7970 else if (*vr0type
== VR_RANGE
7971 && vr1type
== VR_ANTI_RANGE
)
7973 if (TREE_CODE (vr1min
) == INTEGER_CST
)
7974 *vr0max
= int_const_binop (MINUS_EXPR
, vr1min
,
7975 build_int_cst (TREE_TYPE (vr1min
), 1));
7979 else if (*vr0type
== VR_ANTI_RANGE
7980 && vr1type
== VR_RANGE
)
7982 *vr0type
= VR_RANGE
;
7983 if (TREE_CODE (*vr0max
) == INTEGER_CST
)
7984 *vr0min
= int_const_binop (PLUS_EXPR
, *vr0max
,
7985 build_int_cst (TREE_TYPE (*vr0max
), 1));
7993 else if ((operand_less_p (*vr0min
, vr1max
) == 1
7994 || operand_equal_p (*vr0min
, vr1max
, 0))
7995 && operand_less_p (vr1min
, *vr0min
) == 1)
7997 /* ( [ ) ] or ( )[ ] */
7998 if (*vr0type
== VR_ANTI_RANGE
7999 && vr1type
== VR_ANTI_RANGE
)
8001 else if (*vr0type
== VR_RANGE
8002 && vr1type
== VR_RANGE
)
8004 else if (*vr0type
== VR_RANGE
8005 && vr1type
== VR_ANTI_RANGE
)
8007 if (TREE_CODE (vr1max
) == INTEGER_CST
)
8008 *vr0min
= int_const_binop (PLUS_EXPR
, vr1max
,
8009 build_int_cst (TREE_TYPE (vr1max
), 1));
8013 else if (*vr0type
== VR_ANTI_RANGE
8014 && vr1type
== VR_RANGE
)
8016 *vr0type
= VR_RANGE
;
8017 if (TREE_CODE (*vr0min
) == INTEGER_CST
)
8018 *vr0max
= int_const_binop (MINUS_EXPR
, *vr0min
,
8019 build_int_cst (TREE_TYPE (*vr0min
), 1));
8028 /* As a fallback simply use { *VRTYPE, *VR0MIN, *VR0MAX } as
8029 result for the intersection. That's always a conservative
8030 correct estimate. */
8036 /* Intersect the two value-ranges *VR0 and *VR1 and store the result
8037 in *VR0. This may not be the smallest possible such range. */
8040 vrp_intersect_ranges_1 (value_range_t
*vr0
, value_range_t
*vr1
)
8042 value_range_t saved
;
8044 /* If either range is VR_VARYING the other one wins. */
8045 if (vr1
->type
== VR_VARYING
)
8047 if (vr0
->type
== VR_VARYING
)
8049 copy_value_range (vr0
, vr1
);
8053 /* When either range is VR_UNDEFINED the resulting range is
8054 VR_UNDEFINED, too. */
8055 if (vr0
->type
== VR_UNDEFINED
)
8057 if (vr1
->type
== VR_UNDEFINED
)
8059 set_value_range_to_undefined (vr0
);
8063 /* Save the original vr0 so we can return it as conservative intersection
8064 result when our worker turns things to varying. */
8066 intersect_ranges (&vr0
->type
, &vr0
->min
, &vr0
->max
,
8067 vr1
->type
, vr1
->min
, vr1
->max
);
8068 /* Make sure to canonicalize the result though as the inversion of a
8069 VR_RANGE can still be a VR_RANGE. */
8070 set_and_canonicalize_value_range (vr0
, vr0
->type
,
8071 vr0
->min
, vr0
->max
, vr0
->equiv
);
8072 /* If that failed, use the saved original VR0. */
8073 if (vr0
->type
== VR_VARYING
)
8078 /* If the result is VR_UNDEFINED there is no need to mess with
8079 the equivalencies. */
8080 if (vr0
->type
== VR_UNDEFINED
)
8083 /* The resulting set of equivalences for range intersection is the union of
8085 if (vr0
->equiv
&& vr1
->equiv
&& vr0
->equiv
!= vr1
->equiv
)
8086 bitmap_ior_into (vr0
->equiv
, vr1
->equiv
);
8087 else if (vr1
->equiv
&& !vr0
->equiv
)
8088 bitmap_copy (vr0
->equiv
, vr1
->equiv
);
8092 vrp_intersect_ranges (value_range_t
*vr0
, value_range_t
*vr1
)
8094 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8096 fprintf (dump_file
, "Intersecting\n ");
8097 dump_value_range (dump_file
, vr0
);
8098 fprintf (dump_file
, "\nand\n ");
8099 dump_value_range (dump_file
, vr1
);
8100 fprintf (dump_file
, "\n");
8102 vrp_intersect_ranges_1 (vr0
, vr1
);
8103 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8105 fprintf (dump_file
, "to\n ");
8106 dump_value_range (dump_file
, vr0
);
8107 fprintf (dump_file
, "\n");
8111 /* Meet operation for value ranges. Given two value ranges VR0 and
8112 VR1, store in VR0 a range that contains both VR0 and VR1. This
8113 may not be the smallest possible such range. */
8116 vrp_meet_1 (value_range_t
*vr0
, value_range_t
*vr1
)
8118 value_range_t saved
;
8120 if (vr0
->type
== VR_UNDEFINED
)
8122 set_value_range (vr0
, vr1
->type
, vr1
->min
, vr1
->max
, vr1
->equiv
);
8126 if (vr1
->type
== VR_UNDEFINED
)
8128 /* VR0 already has the resulting range. */
8132 if (vr0
->type
== VR_VARYING
)
8134 /* Nothing to do. VR0 already has the resulting range. */
8138 if (vr1
->type
== VR_VARYING
)
8140 set_value_range_to_varying (vr0
);
8145 union_ranges (&vr0
->type
, &vr0
->min
, &vr0
->max
,
8146 vr1
->type
, vr1
->min
, vr1
->max
);
8147 if (vr0
->type
== VR_VARYING
)
8149 /* Failed to find an efficient meet. Before giving up and setting
8150 the result to VARYING, see if we can at least derive a useful
8151 anti-range. FIXME, all this nonsense about distinguishing
8152 anti-ranges from ranges is necessary because of the odd
8153 semantics of range_includes_zero_p and friends. */
8154 if (((saved
.type
== VR_RANGE
8155 && range_includes_zero_p (saved
.min
, saved
.max
) == 0)
8156 || (saved
.type
== VR_ANTI_RANGE
8157 && range_includes_zero_p (saved
.min
, saved
.max
) == 1))
8158 && ((vr1
->type
== VR_RANGE
8159 && range_includes_zero_p (vr1
->min
, vr1
->max
) == 0)
8160 || (vr1
->type
== VR_ANTI_RANGE
8161 && range_includes_zero_p (vr1
->min
, vr1
->max
) == 1)))
8163 set_value_range_to_nonnull (vr0
, TREE_TYPE (saved
.min
));
8165 /* Since this meet operation did not result from the meeting of
8166 two equivalent names, VR0 cannot have any equivalences. */
8168 bitmap_clear (vr0
->equiv
);
8172 set_value_range_to_varying (vr0
);
8175 set_and_canonicalize_value_range (vr0
, vr0
->type
, vr0
->min
, vr0
->max
,
8177 if (vr0
->type
== VR_VARYING
)
8180 /* The resulting set of equivalences is always the intersection of
8182 if (vr0
->equiv
&& vr1
->equiv
&& vr0
->equiv
!= vr1
->equiv
)
8183 bitmap_and_into (vr0
->equiv
, vr1
->equiv
);
8184 else if (vr0
->equiv
&& !vr1
->equiv
)
8185 bitmap_clear (vr0
->equiv
);
8189 vrp_meet (value_range_t
*vr0
, value_range_t
*vr1
)
8191 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8193 fprintf (dump_file
, "Meeting\n ");
8194 dump_value_range (dump_file
, vr0
);
8195 fprintf (dump_file
, "\nand\n ");
8196 dump_value_range (dump_file
, vr1
);
8197 fprintf (dump_file
, "\n");
8199 vrp_meet_1 (vr0
, vr1
);
8200 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8202 fprintf (dump_file
, "to\n ");
8203 dump_value_range (dump_file
, vr0
);
8204 fprintf (dump_file
, "\n");
8209 /* Visit all arguments for PHI node PHI that flow through executable
8210 edges. If a valid value range can be derived from all the incoming
8211 value ranges, set a new range for the LHS of PHI. */
8213 static enum ssa_prop_result
8214 vrp_visit_phi_node (gimple phi
)
8217 tree lhs
= PHI_RESULT (phi
);
8218 value_range_t
*lhs_vr
= get_value_range (lhs
);
8219 value_range_t vr_result
= VR_INITIALIZER
;
8221 int edges
, old_edges
;
8224 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8226 fprintf (dump_file
, "\nVisiting PHI node: ");
8227 print_gimple_stmt (dump_file
, phi
, 0, dump_flags
);
8231 for (i
= 0; i
< gimple_phi_num_args (phi
); i
++)
8233 edge e
= gimple_phi_arg_edge (phi
, i
);
8235 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8238 "\n Argument #%d (%d -> %d %sexecutable)\n",
8239 (int) i
, e
->src
->index
, e
->dest
->index
,
8240 (e
->flags
& EDGE_EXECUTABLE
) ? "" : "not ");
8243 if (e
->flags
& EDGE_EXECUTABLE
)
8245 tree arg
= PHI_ARG_DEF (phi
, i
);
8246 value_range_t vr_arg
;
8250 if (TREE_CODE (arg
) == SSA_NAME
)
8252 vr_arg
= *(get_value_range (arg
));
8253 /* Do not allow equivalences or symbolic ranges to leak in from
8254 backedges. That creates invalid equivalencies.
8255 See PR53465 and PR54767. */
8256 if (e
->flags
& EDGE_DFS_BACK
8257 && (vr_arg
.type
== VR_RANGE
8258 || vr_arg
.type
== VR_ANTI_RANGE
))
8260 vr_arg
.equiv
= NULL
;
8261 if (symbolic_range_p (&vr_arg
))
8263 vr_arg
.type
= VR_VARYING
;
8264 vr_arg
.min
= NULL_TREE
;
8265 vr_arg
.max
= NULL_TREE
;
8271 if (is_overflow_infinity (arg
))
8272 arg
= drop_tree_overflow (arg
);
8274 vr_arg
.type
= VR_RANGE
;
8277 vr_arg
.equiv
= NULL
;
8280 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8282 fprintf (dump_file
, "\t");
8283 print_generic_expr (dump_file
, arg
, dump_flags
);
8284 fprintf (dump_file
, "\n\tValue: ");
8285 dump_value_range (dump_file
, &vr_arg
);
8286 fprintf (dump_file
, "\n");
8290 copy_value_range (&vr_result
, &vr_arg
);
8292 vrp_meet (&vr_result
, &vr_arg
);
8295 if (vr_result
.type
== VR_VARYING
)
8300 if (vr_result
.type
== VR_VARYING
)
8302 else if (vr_result
.type
== VR_UNDEFINED
)
8305 old_edges
= vr_phi_edge_counts
[SSA_NAME_VERSION (lhs
)];
8306 vr_phi_edge_counts
[SSA_NAME_VERSION (lhs
)] = edges
;
8308 /* To prevent infinite iterations in the algorithm, derive ranges
8309 when the new value is slightly bigger or smaller than the
8310 previous one. We don't do this if we have seen a new executable
8311 edge; this helps us avoid an overflow infinity for conditionals
8312 which are not in a loop. If the old value-range was VR_UNDEFINED
8313 use the updated range and iterate one more time. */
8315 && gimple_phi_num_args (phi
) > 1
8316 && edges
== old_edges
8317 && lhs_vr
->type
!= VR_UNDEFINED
)
8319 int cmp_min
= compare_values (lhs_vr
->min
, vr_result
.min
);
8320 int cmp_max
= compare_values (lhs_vr
->max
, vr_result
.max
);
8322 /* For non VR_RANGE or for pointers fall back to varying if
8323 the range changed. */
8324 if ((lhs_vr
->type
!= VR_RANGE
|| vr_result
.type
!= VR_RANGE
8325 || POINTER_TYPE_P (TREE_TYPE (lhs
)))
8326 && (cmp_min
!= 0 || cmp_max
!= 0))
8329 /* If the new minimum is smaller or larger than the previous
8330 one, go all the way to -INF. In the first case, to avoid
8331 iterating millions of times to reach -INF, and in the
8332 other case to avoid infinite bouncing between different
8334 if (cmp_min
> 0 || cmp_min
< 0)
8336 if (!needs_overflow_infinity (TREE_TYPE (vr_result
.min
))
8337 || !vrp_var_may_overflow (lhs
, phi
))
8338 vr_result
.min
= TYPE_MIN_VALUE (TREE_TYPE (vr_result
.min
));
8339 else if (supports_overflow_infinity (TREE_TYPE (vr_result
.min
)))
8341 negative_overflow_infinity (TREE_TYPE (vr_result
.min
));
8344 /* Similarly, if the new maximum is smaller or larger than
8345 the previous one, go all the way to +INF. */
8346 if (cmp_max
< 0 || cmp_max
> 0)
8348 if (!needs_overflow_infinity (TREE_TYPE (vr_result
.max
))
8349 || !vrp_var_may_overflow (lhs
, phi
))
8350 vr_result
.max
= TYPE_MAX_VALUE (TREE_TYPE (vr_result
.max
));
8351 else if (supports_overflow_infinity (TREE_TYPE (vr_result
.max
)))
8353 positive_overflow_infinity (TREE_TYPE (vr_result
.max
));
8356 /* If we dropped either bound to +-INF then if this is a loop
8357 PHI node SCEV may known more about its value-range. */
8358 if ((cmp_min
> 0 || cmp_min
< 0
8359 || cmp_max
< 0 || cmp_max
> 0)
8361 && (l
= loop_containing_stmt (phi
))
8362 && l
->header
== gimple_bb (phi
))
8363 adjust_range_with_scev (&vr_result
, l
, phi
, lhs
);
8365 /* If we will end up with a (-INF, +INF) range, set it to
8366 VARYING. Same if the previous max value was invalid for
8367 the type and we end up with vr_result.min > vr_result.max. */
8368 if ((vrp_val_is_max (vr_result
.max
)
8369 && vrp_val_is_min (vr_result
.min
))
8370 || compare_values (vr_result
.min
,
8375 /* If the new range is different than the previous value, keep
8378 if (update_value_range (lhs
, &vr_result
))
8380 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8382 fprintf (dump_file
, "Found new range for ");
8383 print_generic_expr (dump_file
, lhs
, 0);
8384 fprintf (dump_file
, ": ");
8385 dump_value_range (dump_file
, &vr_result
);
8386 fprintf (dump_file
, "\n\n");
8389 return SSA_PROP_INTERESTING
;
8392 /* Nothing changed, don't add outgoing edges. */
8393 return SSA_PROP_NOT_INTERESTING
;
8395 /* No match found. Set the LHS to VARYING. */
8397 set_value_range_to_varying (lhs_vr
);
8398 return SSA_PROP_VARYING
;
8401 /* Simplify boolean operations if the source is known
8402 to be already a boolean. */
8404 simplify_truth_ops_using_ranges (gimple_stmt_iterator
*gsi
, gimple stmt
)
8406 enum tree_code rhs_code
= gimple_assign_rhs_code (stmt
);
8408 bool need_conversion
;
8410 /* We handle only !=/== case here. */
8411 gcc_assert (rhs_code
== EQ_EXPR
|| rhs_code
== NE_EXPR
);
8413 op0
= gimple_assign_rhs1 (stmt
);
8414 if (!op_with_boolean_value_range_p (op0
))
8417 op1
= gimple_assign_rhs2 (stmt
);
8418 if (!op_with_boolean_value_range_p (op1
))
8421 /* Reduce number of cases to handle to NE_EXPR. As there is no
8422 BIT_XNOR_EXPR we cannot replace A == B with a single statement. */
8423 if (rhs_code
== EQ_EXPR
)
8425 if (TREE_CODE (op1
) == INTEGER_CST
)
8426 op1
= int_const_binop (BIT_XOR_EXPR
, op1
,
8427 build_int_cst (TREE_TYPE (op1
), 1));
8432 lhs
= gimple_assign_lhs (stmt
);
8434 = !useless_type_conversion_p (TREE_TYPE (lhs
), TREE_TYPE (op0
));
8436 /* Make sure to not sign-extend a 1-bit 1 when converting the result. */
8438 && !TYPE_UNSIGNED (TREE_TYPE (op0
))
8439 && TYPE_PRECISION (TREE_TYPE (op0
)) == 1
8440 && TYPE_PRECISION (TREE_TYPE (lhs
)) > 1)
8443 /* For A != 0 we can substitute A itself. */
8444 if (integer_zerop (op1
))
8445 gimple_assign_set_rhs_with_ops (gsi
,
8447 ? NOP_EXPR
: TREE_CODE (op0
),
8449 /* For A != B we substitute A ^ B. Either with conversion. */
8450 else if (need_conversion
)
8452 tree tem
= make_ssa_name (TREE_TYPE (op0
), NULL
);
8453 gimple newop
= gimple_build_assign_with_ops (BIT_XOR_EXPR
, tem
, op0
, op1
);
8454 gsi_insert_before (gsi
, newop
, GSI_SAME_STMT
);
8455 gimple_assign_set_rhs_with_ops (gsi
, NOP_EXPR
, tem
, NULL_TREE
);
8459 gimple_assign_set_rhs_with_ops (gsi
, BIT_XOR_EXPR
, op0
, op1
);
8460 update_stmt (gsi_stmt (*gsi
));
8465 /* Simplify a division or modulo operator to a right shift or
8466 bitwise and if the first operand is unsigned or is greater
8467 than zero and the second operand is an exact power of two. */
8470 simplify_div_or_mod_using_ranges (gimple stmt
)
8472 enum tree_code rhs_code
= gimple_assign_rhs_code (stmt
);
8474 tree op0
= gimple_assign_rhs1 (stmt
);
8475 tree op1
= gimple_assign_rhs2 (stmt
);
8476 value_range_t
*vr
= get_value_range (gimple_assign_rhs1 (stmt
));
8478 if (TYPE_UNSIGNED (TREE_TYPE (op0
)))
8480 val
= integer_one_node
;
8486 val
= compare_range_with_value (GE_EXPR
, vr
, integer_zero_node
, &sop
);
8490 && integer_onep (val
)
8491 && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC
))
8493 location_t location
;
8495 if (!gimple_has_location (stmt
))
8496 location
= input_location
;
8498 location
= gimple_location (stmt
);
8499 warning_at (location
, OPT_Wstrict_overflow
,
8500 "assuming signed overflow does not occur when "
8501 "simplifying %</%> or %<%%%> to %<>>%> or %<&%>");
8505 if (val
&& integer_onep (val
))
8509 if (rhs_code
== TRUNC_DIV_EXPR
)
8511 t
= build_int_cst (integer_type_node
, tree_log2 (op1
));
8512 gimple_assign_set_rhs_code (stmt
, RSHIFT_EXPR
);
8513 gimple_assign_set_rhs1 (stmt
, op0
);
8514 gimple_assign_set_rhs2 (stmt
, t
);
8518 t
= build_int_cst (TREE_TYPE (op1
), 1);
8519 t
= int_const_binop (MINUS_EXPR
, op1
, t
);
8520 t
= fold_convert (TREE_TYPE (op0
), t
);
8522 gimple_assign_set_rhs_code (stmt
, BIT_AND_EXPR
);
8523 gimple_assign_set_rhs1 (stmt
, op0
);
8524 gimple_assign_set_rhs2 (stmt
, t
);
8534 /* If the operand to an ABS_EXPR is >= 0, then eliminate the
8535 ABS_EXPR. If the operand is <= 0, then simplify the
8536 ABS_EXPR into a NEGATE_EXPR. */
8539 simplify_abs_using_ranges (gimple stmt
)
8542 tree op
= gimple_assign_rhs1 (stmt
);
8543 tree type
= TREE_TYPE (op
);
8544 value_range_t
*vr
= get_value_range (op
);
8546 if (TYPE_UNSIGNED (type
))
8548 val
= integer_zero_node
;
8554 val
= compare_range_with_value (LE_EXPR
, vr
, integer_zero_node
, &sop
);
8558 val
= compare_range_with_value (GE_EXPR
, vr
, integer_zero_node
,
8563 if (integer_zerop (val
))
8564 val
= integer_one_node
;
8565 else if (integer_onep (val
))
8566 val
= integer_zero_node
;
8571 && (integer_onep (val
) || integer_zerop (val
)))
8573 if (sop
&& issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC
))
8575 location_t location
;
8577 if (!gimple_has_location (stmt
))
8578 location
= input_location
;
8580 location
= gimple_location (stmt
);
8581 warning_at (location
, OPT_Wstrict_overflow
,
8582 "assuming signed overflow does not occur when "
8583 "simplifying %<abs (X)%> to %<X%> or %<-X%>");
8586 gimple_assign_set_rhs1 (stmt
, op
);
8587 if (integer_onep (val
))
8588 gimple_assign_set_rhs_code (stmt
, NEGATE_EXPR
);
8590 gimple_assign_set_rhs_code (stmt
, SSA_NAME
);
8599 /* Optimize away redundant BIT_AND_EXPR and BIT_IOR_EXPR.
8600 If all the bits that are being cleared by & are already
8601 known to be zero from VR, or all the bits that are being
8602 set by | are already known to be one from VR, the bit
8603 operation is redundant. */
8606 simplify_bit_ops_using_ranges (gimple_stmt_iterator
*gsi
, gimple stmt
)
8608 tree op0
= gimple_assign_rhs1 (stmt
);
8609 tree op1
= gimple_assign_rhs2 (stmt
);
8610 tree op
= NULL_TREE
;
8611 value_range_t vr0
= VR_INITIALIZER
;
8612 value_range_t vr1
= VR_INITIALIZER
;
8613 wide_int may_be_nonzero0
, may_be_nonzero1
;
8614 wide_int must_be_nonzero0
, must_be_nonzero1
;
8617 if (TREE_CODE (op0
) == SSA_NAME
)
8618 vr0
= *(get_value_range (op0
));
8619 else if (is_gimple_min_invariant (op0
))
8620 set_value_range_to_value (&vr0
, op0
, NULL
);
8624 if (TREE_CODE (op1
) == SSA_NAME
)
8625 vr1
= *(get_value_range (op1
));
8626 else if (is_gimple_min_invariant (op1
))
8627 set_value_range_to_value (&vr1
, op1
, NULL
);
8631 if (!zero_nonzero_bits_from_vr (TREE_TYPE (op0
), &vr0
, &may_be_nonzero0
,
8634 if (!zero_nonzero_bits_from_vr (TREE_TYPE (op1
), &vr1
, &may_be_nonzero1
,
8638 switch (gimple_assign_rhs_code (stmt
))
8641 mask
= may_be_nonzero0
.and_not (must_be_nonzero1
);
8647 mask
= may_be_nonzero1
.and_not (must_be_nonzero0
);
8655 mask
= may_be_nonzero0
.and_not (must_be_nonzero1
);
8661 mask
= may_be_nonzero1
.and_not (must_be_nonzero0
);
8672 if (op
== NULL_TREE
)
8675 gimple_assign_set_rhs_with_ops (gsi
, TREE_CODE (op
), op
, NULL
);
8676 update_stmt (gsi_stmt (*gsi
));
8680 /* We are comparing trees OP0 and OP1 using COND_CODE. OP0 has
8681 a known value range VR.
8683 If there is one and only one value which will satisfy the
8684 conditional, then return that value. Else return NULL. */
8687 test_for_singularity (enum tree_code cond_code
, tree op0
,
8688 tree op1
, value_range_t
*vr
)
8693 /* Extract minimum/maximum values which satisfy the
8694 the conditional as it was written. */
8695 if (cond_code
== LE_EXPR
|| cond_code
== LT_EXPR
)
8697 /* This should not be negative infinity; there is no overflow
8699 min
= TYPE_MIN_VALUE (TREE_TYPE (op0
));
8702 if (cond_code
== LT_EXPR
&& !is_overflow_infinity (max
))
8704 tree one
= build_int_cst (TREE_TYPE (op0
), 1);
8705 max
= fold_build2 (MINUS_EXPR
, TREE_TYPE (op0
), max
, one
);
8707 TREE_NO_WARNING (max
) = 1;
8710 else if (cond_code
== GE_EXPR
|| cond_code
== GT_EXPR
)
8712 /* This should not be positive infinity; there is no overflow
8714 max
= TYPE_MAX_VALUE (TREE_TYPE (op0
));
8717 if (cond_code
== GT_EXPR
&& !is_overflow_infinity (min
))
8719 tree one
= build_int_cst (TREE_TYPE (op0
), 1);
8720 min
= fold_build2 (PLUS_EXPR
, TREE_TYPE (op0
), min
, one
);
8722 TREE_NO_WARNING (min
) = 1;
8726 /* Now refine the minimum and maximum values using any
8727 value range information we have for op0. */
8730 if (compare_values (vr
->min
, min
) == 1)
8732 if (compare_values (vr
->max
, max
) == -1)
8735 /* If the new min/max values have converged to a single value,
8736 then there is only one value which can satisfy the condition,
8737 return that value. */
8738 if (operand_equal_p (min
, max
, 0) && is_gimple_min_invariant (min
))
8744 /* Return whether the value range *VR fits in an integer type specified
8745 by PRECISION and UNSIGNED_P. */
8748 range_fits_type_p (value_range_t
*vr
, unsigned dest_precision
, signop dest_sgn
)
8751 unsigned src_precision
;
8755 /* We can only handle integral and pointer types. */
8756 src_type
= TREE_TYPE (vr
->min
);
8757 if (!INTEGRAL_TYPE_P (src_type
)
8758 && !POINTER_TYPE_P (src_type
))
8761 /* An extension is fine unless VR is SIGNED and dest_sgn is UNSIGNED,
8762 and so is an identity transform. */
8763 src_precision
= TYPE_PRECISION (TREE_TYPE (vr
->min
));
8764 src_sgn
= TYPE_SIGN (src_type
);
8765 if ((src_precision
< dest_precision
8766 && !(dest_sgn
== UNSIGNED
&& src_sgn
== SIGNED
))
8767 || (src_precision
== dest_precision
&& src_sgn
== dest_sgn
))
8770 /* Now we can only handle ranges with constant bounds. */
8771 if (vr
->type
!= VR_RANGE
8772 || TREE_CODE (vr
->min
) != INTEGER_CST
8773 || TREE_CODE (vr
->max
) != INTEGER_CST
)
8776 /* For sign changes, the MSB of the wide_int has to be clear.
8777 An unsigned value with its MSB set cannot be represented by
8778 a signed wide_int, while a negative value cannot be represented
8779 by an unsigned wide_int. */
8780 if (src_sgn
!= dest_sgn
8781 && (wi::lts_p (vr
->min
, 0) || wi::lts_p (vr
->max
, 0)))
8784 /* Then we can perform the conversion on both ends and compare
8785 the result for equality. */
8786 tem
= wi::ext (wi::to_widest (vr
->min
), dest_precision
, dest_sgn
);
8787 if (tem
!= wi::to_widest (vr
->min
))
8789 tem
= wi::ext (wi::to_widest (vr
->max
), dest_precision
, dest_sgn
);
8790 if (tem
!= wi::to_widest (vr
->max
))
8796 /* Simplify a conditional using a relational operator to an equality
8797 test if the range information indicates only one value can satisfy
8798 the original conditional. */
8801 simplify_cond_using_ranges (gimple stmt
)
8803 tree op0
= gimple_cond_lhs (stmt
);
8804 tree op1
= gimple_cond_rhs (stmt
);
8805 enum tree_code cond_code
= gimple_cond_code (stmt
);
8807 if (cond_code
!= NE_EXPR
8808 && cond_code
!= EQ_EXPR
8809 && TREE_CODE (op0
) == SSA_NAME
8810 && INTEGRAL_TYPE_P (TREE_TYPE (op0
))
8811 && is_gimple_min_invariant (op1
))
8813 value_range_t
*vr
= get_value_range (op0
);
8815 /* If we have range information for OP0, then we might be
8816 able to simplify this conditional. */
8817 if (vr
->type
== VR_RANGE
)
8819 tree new_tree
= test_for_singularity (cond_code
, op0
, op1
, vr
);
8825 fprintf (dump_file
, "Simplified relational ");
8826 print_gimple_stmt (dump_file
, stmt
, 0, 0);
8827 fprintf (dump_file
, " into ");
8830 gimple_cond_set_code (stmt
, EQ_EXPR
);
8831 gimple_cond_set_lhs (stmt
, op0
);
8832 gimple_cond_set_rhs (stmt
, new_tree
);
8838 print_gimple_stmt (dump_file
, stmt
, 0, 0);
8839 fprintf (dump_file
, "\n");
8845 /* Try again after inverting the condition. We only deal
8846 with integral types here, so no need to worry about
8847 issues with inverting FP comparisons. */
8848 cond_code
= invert_tree_comparison (cond_code
, false);
8849 new_tree
= test_for_singularity (cond_code
, op0
, op1
, vr
);
8855 fprintf (dump_file
, "Simplified relational ");
8856 print_gimple_stmt (dump_file
, stmt
, 0, 0);
8857 fprintf (dump_file
, " into ");
8860 gimple_cond_set_code (stmt
, NE_EXPR
);
8861 gimple_cond_set_lhs (stmt
, op0
);
8862 gimple_cond_set_rhs (stmt
, new_tree
);
8868 print_gimple_stmt (dump_file
, stmt
, 0, 0);
8869 fprintf (dump_file
, "\n");
8877 /* If we have a comparison of an SSA_NAME (OP0) against a constant,
8878 see if OP0 was set by a type conversion where the source of
8879 the conversion is another SSA_NAME with a range that fits
8880 into the range of OP0's type.
8882 If so, the conversion is redundant as the earlier SSA_NAME can be
8883 used for the comparison directly if we just massage the constant in the
8885 if (TREE_CODE (op0
) == SSA_NAME
8886 && TREE_CODE (op1
) == INTEGER_CST
)
8888 gimple def_stmt
= SSA_NAME_DEF_STMT (op0
);
8891 if (!is_gimple_assign (def_stmt
)
8892 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt
)))
8895 innerop
= gimple_assign_rhs1 (def_stmt
);
8897 if (TREE_CODE (innerop
) == SSA_NAME
8898 && !POINTER_TYPE_P (TREE_TYPE (innerop
)))
8900 value_range_t
*vr
= get_value_range (innerop
);
8902 if (range_int_cst_p (vr
)
8903 && range_fits_type_p (vr
,
8904 TYPE_PRECISION (TREE_TYPE (op0
)),
8905 TYPE_SIGN (TREE_TYPE (op0
)))
8906 && int_fits_type_p (op1
, TREE_TYPE (innerop
))
8907 /* The range must not have overflowed, or if it did overflow
8908 we must not be wrapping/trapping overflow and optimizing
8909 with strict overflow semantics. */
8910 && ((!is_negative_overflow_infinity (vr
->min
)
8911 && !is_positive_overflow_infinity (vr
->max
))
8912 || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (innerop
))))
8914 /* If the range overflowed and the user has asked for warnings
8915 when strict overflow semantics were used to optimize code,
8916 issue an appropriate warning. */
8917 if ((is_negative_overflow_infinity (vr
->min
)
8918 || is_positive_overflow_infinity (vr
->max
))
8919 && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_CONDITIONAL
))
8921 location_t location
;
8923 if (!gimple_has_location (stmt
))
8924 location
= input_location
;
8926 location
= gimple_location (stmt
);
8927 warning_at (location
, OPT_Wstrict_overflow
,
8928 "assuming signed overflow does not occur when "
8929 "simplifying conditional");
8932 tree newconst
= fold_convert (TREE_TYPE (innerop
), op1
);
8933 gimple_cond_set_lhs (stmt
, innerop
);
8934 gimple_cond_set_rhs (stmt
, newconst
);
8943 /* Simplify a switch statement using the value range of the switch
8947 simplify_switch_using_ranges (gimple stmt
)
8949 tree op
= gimple_switch_index (stmt
);
8954 size_t i
= 0, j
= 0, n
, n2
;
8957 size_t k
= 1, l
= 0;
8959 if (TREE_CODE (op
) == SSA_NAME
)
8961 vr
= get_value_range (op
);
8963 /* We can only handle integer ranges. */
8964 if ((vr
->type
!= VR_RANGE
8965 && vr
->type
!= VR_ANTI_RANGE
)
8966 || symbolic_range_p (vr
))
8969 /* Find case label for min/max of the value range. */
8970 take_default
= !find_case_label_ranges (stmt
, vr
, &i
, &j
, &k
, &l
);
8972 else if (TREE_CODE (op
) == INTEGER_CST
)
8974 take_default
= !find_case_label_index (stmt
, 1, op
, &i
);
8988 n
= gimple_switch_num_labels (stmt
);
8990 /* Bail out if this is just all edges taken. */
8996 /* Build a new vector of taken case labels. */
8997 vec2
= make_tree_vec (j
- i
+ 1 + l
- k
+ 1 + (int)take_default
);
9000 /* Add the default edge, if necessary. */
9002 TREE_VEC_ELT (vec2
, n2
++) = gimple_switch_default_label (stmt
);
9004 for (; i
<= j
; ++i
, ++n2
)
9005 TREE_VEC_ELT (vec2
, n2
) = gimple_switch_label (stmt
, i
);
9007 for (; k
<= l
; ++k
, ++n2
)
9008 TREE_VEC_ELT (vec2
, n2
) = gimple_switch_label (stmt
, k
);
9010 /* Mark needed edges. */
9011 for (i
= 0; i
< n2
; ++i
)
9013 e
= find_edge (gimple_bb (stmt
),
9014 label_to_block (CASE_LABEL (TREE_VEC_ELT (vec2
, i
))));
9015 e
->aux
= (void *)-1;
9018 /* Queue not needed edges for later removal. */
9019 FOR_EACH_EDGE (e
, ei
, gimple_bb (stmt
)->succs
)
9021 if (e
->aux
== (void *)-1)
9027 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
9029 fprintf (dump_file
, "removing unreachable case label\n");
9031 to_remove_edges
.safe_push (e
);
9032 e
->flags
&= ~EDGE_EXECUTABLE
;
9035 /* And queue an update for the stmt. */
9038 to_update_switch_stmts
.safe_push (su
);
9042 /* Simplify an integral conversion from an SSA name in STMT. */
9045 simplify_conversion_using_ranges (gimple stmt
)
9047 tree innerop
, middleop
, finaltype
;
9049 value_range_t
*innervr
;
9050 signop inner_sgn
, middle_sgn
, final_sgn
;
9051 unsigned inner_prec
, middle_prec
, final_prec
;
9052 widest_int innermin
, innermed
, innermax
, middlemin
, middlemed
, middlemax
;
9054 finaltype
= TREE_TYPE (gimple_assign_lhs (stmt
));
9055 if (!INTEGRAL_TYPE_P (finaltype
))
9057 middleop
= gimple_assign_rhs1 (stmt
);
9058 def_stmt
= SSA_NAME_DEF_STMT (middleop
);
9059 if (!is_gimple_assign (def_stmt
)
9060 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt
)))
9062 innerop
= gimple_assign_rhs1 (def_stmt
);
9063 if (TREE_CODE (innerop
) != SSA_NAME
9064 || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (innerop
))
9067 /* Get the value-range of the inner operand. */
9068 innervr
= get_value_range (innerop
);
9069 if (innervr
->type
!= VR_RANGE
9070 || TREE_CODE (innervr
->min
) != INTEGER_CST
9071 || TREE_CODE (innervr
->max
) != INTEGER_CST
)
9074 /* Simulate the conversion chain to check if the result is equal if
9075 the middle conversion is removed. */
9076 innermin
= wi::to_widest (innervr
->min
);
9077 innermax
= wi::to_widest (innervr
->max
);
9079 inner_prec
= TYPE_PRECISION (TREE_TYPE (innerop
));
9080 middle_prec
= TYPE_PRECISION (TREE_TYPE (middleop
));
9081 final_prec
= TYPE_PRECISION (finaltype
);
9083 /* If the first conversion is not injective, the second must not
9085 if (wi::gtu_p (innermax
- innermin
,
9086 wi::mask
<widest_int
> (middle_prec
, false))
9087 && middle_prec
< final_prec
)
9089 /* We also want a medium value so that we can track the effect that
9090 narrowing conversions with sign change have. */
9091 inner_sgn
= TYPE_SIGN (TREE_TYPE (innerop
));
9092 if (inner_sgn
== UNSIGNED
)
9093 innermed
= wi::shifted_mask
<widest_int
> (1, inner_prec
- 1, false);
9096 if (wi::cmp (innermin
, innermed
, inner_sgn
) >= 0
9097 || wi::cmp (innermed
, innermax
, inner_sgn
) >= 0)
9098 innermed
= innermin
;
9100 middle_sgn
= TYPE_SIGN (TREE_TYPE (middleop
));
9101 middlemin
= wi::ext (innermin
, middle_prec
, middle_sgn
);
9102 middlemed
= wi::ext (innermed
, middle_prec
, middle_sgn
);
9103 middlemax
= wi::ext (innermax
, middle_prec
, middle_sgn
);
9105 /* Require that the final conversion applied to both the original
9106 and the intermediate range produces the same result. */
9107 final_sgn
= TYPE_SIGN (finaltype
);
9108 if (wi::ext (middlemin
, final_prec
, final_sgn
)
9109 != wi::ext (innermin
, final_prec
, final_sgn
)
9110 || wi::ext (middlemed
, final_prec
, final_sgn
)
9111 != wi::ext (innermed
, final_prec
, final_sgn
)
9112 || wi::ext (middlemax
, final_prec
, final_sgn
)
9113 != wi::ext (innermax
, final_prec
, final_sgn
))
9116 gimple_assign_set_rhs1 (stmt
, innerop
);
9121 /* Simplify a conversion from integral SSA name to float in STMT. */
9124 simplify_float_conversion_using_ranges (gimple_stmt_iterator
*gsi
, gimple stmt
)
9126 tree rhs1
= gimple_assign_rhs1 (stmt
);
9127 value_range_t
*vr
= get_value_range (rhs1
);
9128 enum machine_mode fltmode
= TYPE_MODE (TREE_TYPE (gimple_assign_lhs (stmt
)));
9129 enum machine_mode mode
;
9133 /* We can only handle constant ranges. */
9134 if (vr
->type
!= VR_RANGE
9135 || TREE_CODE (vr
->min
) != INTEGER_CST
9136 || TREE_CODE (vr
->max
) != INTEGER_CST
)
9139 /* First check if we can use a signed type in place of an unsigned. */
9140 if (TYPE_UNSIGNED (TREE_TYPE (rhs1
))
9141 && (can_float_p (fltmode
, TYPE_MODE (TREE_TYPE (rhs1
)), 0)
9142 != CODE_FOR_nothing
)
9143 && range_fits_type_p (vr
, TYPE_PRECISION (TREE_TYPE (rhs1
)), SIGNED
))
9144 mode
= TYPE_MODE (TREE_TYPE (rhs1
));
9145 /* If we can do the conversion in the current input mode do nothing. */
9146 else if (can_float_p (fltmode
, TYPE_MODE (TREE_TYPE (rhs1
)),
9147 TYPE_UNSIGNED (TREE_TYPE (rhs1
))) != CODE_FOR_nothing
)
9149 /* Otherwise search for a mode we can use, starting from the narrowest
9150 integer mode available. */
9153 mode
= GET_CLASS_NARROWEST_MODE (MODE_INT
);
9156 /* If we cannot do a signed conversion to float from mode
9157 or if the value-range does not fit in the signed type
9158 try with a wider mode. */
9159 if (can_float_p (fltmode
, mode
, 0) != CODE_FOR_nothing
9160 && range_fits_type_p (vr
, GET_MODE_PRECISION (mode
), SIGNED
))
9163 mode
= GET_MODE_WIDER_MODE (mode
);
9164 /* But do not widen the input. Instead leave that to the
9165 optabs expansion code. */
9166 if (GET_MODE_PRECISION (mode
) > TYPE_PRECISION (TREE_TYPE (rhs1
)))
9169 while (mode
!= VOIDmode
);
9170 if (mode
== VOIDmode
)
9174 /* It works, insert a truncation or sign-change before the
9175 float conversion. */
9176 tem
= make_ssa_name (build_nonstandard_integer_type
9177 (GET_MODE_PRECISION (mode
), 0), NULL
);
9178 conv
= gimple_build_assign_with_ops (NOP_EXPR
, tem
, rhs1
, NULL_TREE
);
9179 gsi_insert_before (gsi
, conv
, GSI_SAME_STMT
);
9180 gimple_assign_set_rhs1 (stmt
, tem
);
9186 /* Simplify STMT using ranges if possible. */
9189 simplify_stmt_using_ranges (gimple_stmt_iterator
*gsi
)
9191 gimple stmt
= gsi_stmt (*gsi
);
9192 if (is_gimple_assign (stmt
))
9194 enum tree_code rhs_code
= gimple_assign_rhs_code (stmt
);
9195 tree rhs1
= gimple_assign_rhs1 (stmt
);
9201 /* Transform EQ_EXPR, NE_EXPR into BIT_XOR_EXPR or identity
9202 if the RHS is zero or one, and the LHS are known to be boolean
9204 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
9205 return simplify_truth_ops_using_ranges (gsi
, stmt
);
9208 /* Transform TRUNC_DIV_EXPR and TRUNC_MOD_EXPR into RSHIFT_EXPR
9209 and BIT_AND_EXPR respectively if the first operand is greater
9210 than zero and the second operand is an exact power of two. */
9211 case TRUNC_DIV_EXPR
:
9212 case TRUNC_MOD_EXPR
:
9213 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1
))
9214 && integer_pow2p (gimple_assign_rhs2 (stmt
)))
9215 return simplify_div_or_mod_using_ranges (stmt
);
9218 /* Transform ABS (X) into X or -X as appropriate. */
9220 if (TREE_CODE (rhs1
) == SSA_NAME
9221 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
9222 return simplify_abs_using_ranges (stmt
);
9227 /* Optimize away BIT_AND_EXPR and BIT_IOR_EXPR
9228 if all the bits being cleared are already cleared or
9229 all the bits being set are already set. */
9230 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
9231 return simplify_bit_ops_using_ranges (gsi
, stmt
);
9235 if (TREE_CODE (rhs1
) == SSA_NAME
9236 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
9237 return simplify_conversion_using_ranges (stmt
);
9241 if (TREE_CODE (rhs1
) == SSA_NAME
9242 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
9243 return simplify_float_conversion_using_ranges (gsi
, stmt
);
9250 else if (gimple_code (stmt
) == GIMPLE_COND
)
9251 return simplify_cond_using_ranges (stmt
);
9252 else if (gimple_code (stmt
) == GIMPLE_SWITCH
)
9253 return simplify_switch_using_ranges (stmt
);
9258 /* If the statement pointed by SI has a predicate whose value can be
9259 computed using the value range information computed by VRP, compute
9260 its value and return true. Otherwise, return false. */
9263 fold_predicate_in (gimple_stmt_iterator
*si
)
9265 bool assignment_p
= false;
9267 gimple stmt
= gsi_stmt (*si
);
9269 if (is_gimple_assign (stmt
)
9270 && TREE_CODE_CLASS (gimple_assign_rhs_code (stmt
)) == tcc_comparison
)
9272 assignment_p
= true;
9273 val
= vrp_evaluate_conditional (gimple_assign_rhs_code (stmt
),
9274 gimple_assign_rhs1 (stmt
),
9275 gimple_assign_rhs2 (stmt
),
9278 else if (gimple_code (stmt
) == GIMPLE_COND
)
9279 val
= vrp_evaluate_conditional (gimple_cond_code (stmt
),
9280 gimple_cond_lhs (stmt
),
9281 gimple_cond_rhs (stmt
),
9289 val
= fold_convert (gimple_expr_type (stmt
), val
);
9293 fprintf (dump_file
, "Folding predicate ");
9294 print_gimple_expr (dump_file
, stmt
, 0, 0);
9295 fprintf (dump_file
, " to ");
9296 print_generic_expr (dump_file
, val
, 0);
9297 fprintf (dump_file
, "\n");
9300 if (is_gimple_assign (stmt
))
9301 gimple_assign_set_rhs_from_tree (si
, val
);
9304 gcc_assert (gimple_code (stmt
) == GIMPLE_COND
);
9305 if (integer_zerop (val
))
9306 gimple_cond_make_false (stmt
);
9307 else if (integer_onep (val
))
9308 gimple_cond_make_true (stmt
);
9319 /* Callback for substitute_and_fold folding the stmt at *SI. */
9322 vrp_fold_stmt (gimple_stmt_iterator
*si
)
9324 if (fold_predicate_in (si
))
9327 return simplify_stmt_using_ranges (si
);
9330 /* Stack of dest,src equivalency pairs that need to be restored after
9331 each attempt to thread a block's incoming edge to an outgoing edge.
9333 A NULL entry is used to mark the end of pairs which need to be
9335 static vec
<tree
> equiv_stack
;
9337 /* A trivial wrapper so that we can present the generic jump threading
9338 code with a simple API for simplifying statements. STMT is the
9339 statement we want to simplify, WITHIN_STMT provides the location
9340 for any overflow warnings. */
9343 simplify_stmt_for_jump_threading (gimple stmt
, gimple within_stmt
)
9345 if (gimple_code (stmt
) == GIMPLE_COND
)
9346 return vrp_evaluate_conditional (gimple_cond_code (stmt
),
9347 gimple_cond_lhs (stmt
),
9348 gimple_cond_rhs (stmt
), within_stmt
);
9350 if (gimple_code (stmt
) == GIMPLE_ASSIGN
)
9352 value_range_t new_vr
= VR_INITIALIZER
;
9353 tree lhs
= gimple_assign_lhs (stmt
);
9355 if (TREE_CODE (lhs
) == SSA_NAME
9356 && (INTEGRAL_TYPE_P (TREE_TYPE (lhs
))
9357 || POINTER_TYPE_P (TREE_TYPE (lhs
))))
9359 extract_range_from_assignment (&new_vr
, stmt
);
9360 if (range_int_cst_singleton_p (&new_vr
))
9368 /* Blocks which have more than one predecessor and more than
9369 one successor present jump threading opportunities, i.e.,
9370 when the block is reached from a specific predecessor, we
9371 may be able to determine which of the outgoing edges will
9372 be traversed. When this optimization applies, we are able
9373 to avoid conditionals at runtime and we may expose secondary
9374 optimization opportunities.
9376 This routine is effectively a driver for the generic jump
9377 threading code. It basically just presents the generic code
9378 with edges that may be suitable for jump threading.
9380 Unlike DOM, we do not iterate VRP if jump threading was successful.
9381 While iterating may expose new opportunities for VRP, it is expected
9382 those opportunities would be very limited and the compile time cost
9383 to expose those opportunities would be significant.
9385 As jump threading opportunities are discovered, they are registered
9386 for later realization. */
9389 identify_jump_threads (void)
9396 /* Ugh. When substituting values earlier in this pass we can
9397 wipe the dominance information. So rebuild the dominator
9398 information as we need it within the jump threading code. */
9399 calculate_dominance_info (CDI_DOMINATORS
);
9401 /* We do not allow VRP information to be used for jump threading
9402 across a back edge in the CFG. Otherwise it becomes too
9403 difficult to avoid eliminating loop exit tests. Of course
9404 EDGE_DFS_BACK is not accurate at this time so we have to
9406 mark_dfs_back_edges ();
9408 /* Do not thread across edges we are about to remove. Just marking
9409 them as EDGE_DFS_BACK will do. */
9410 FOR_EACH_VEC_ELT (to_remove_edges
, i
, e
)
9411 e
->flags
|= EDGE_DFS_BACK
;
9413 /* Allocate our unwinder stack to unwind any temporary equivalences
9414 that might be recorded. */
9415 equiv_stack
.create (20);
9417 /* To avoid lots of silly node creation, we create a single
9418 conditional and just modify it in-place when attempting to
9420 dummy
= gimple_build_cond (EQ_EXPR
,
9421 integer_zero_node
, integer_zero_node
,
9424 /* Walk through all the blocks finding those which present a
9425 potential jump threading opportunity. We could set this up
9426 as a dominator walker and record data during the walk, but
9427 I doubt it's worth the effort for the classes of jump
9428 threading opportunities we are trying to identify at this
9429 point in compilation. */
9434 /* If the generic jump threading code does not find this block
9435 interesting, then there is nothing to do. */
9436 if (! potentially_threadable_block (bb
))
9439 /* We only care about blocks ending in a COND_EXPR. While there
9440 may be some value in handling SWITCH_EXPR here, I doubt it's
9441 terribly important. */
9442 last
= gsi_stmt (gsi_last_bb (bb
));
9444 /* We're basically looking for a switch or any kind of conditional with
9445 integral or pointer type arguments. Note the type of the second
9446 argument will be the same as the first argument, so no need to
9447 check it explicitly. */
9448 if (gimple_code (last
) == GIMPLE_SWITCH
9449 || (gimple_code (last
) == GIMPLE_COND
9450 && TREE_CODE (gimple_cond_lhs (last
)) == SSA_NAME
9451 && (INTEGRAL_TYPE_P (TREE_TYPE (gimple_cond_lhs (last
)))
9452 || POINTER_TYPE_P (TREE_TYPE (gimple_cond_lhs (last
))))
9453 && (TREE_CODE (gimple_cond_rhs (last
)) == SSA_NAME
9454 || is_gimple_min_invariant (gimple_cond_rhs (last
)))))
9458 /* We've got a block with multiple predecessors and multiple
9459 successors which also ends in a suitable conditional or
9460 switch statement. For each predecessor, see if we can thread
9461 it to a specific successor. */
9462 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
9464 /* Do not thread across back edges or abnormal edges
9466 if (e
->flags
& (EDGE_DFS_BACK
| EDGE_COMPLEX
))
9469 thread_across_edge (dummy
, e
, true, &equiv_stack
,
9470 simplify_stmt_for_jump_threading
);
9475 /* We do not actually update the CFG or SSA graphs at this point as
9476 ASSERT_EXPRs are still in the IL and cfg cleanup code does not yet
9477 handle ASSERT_EXPRs gracefully. */
9480 /* We identified all the jump threading opportunities earlier, but could
9481 not transform the CFG at that time. This routine transforms the
9482 CFG and arranges for the dominator tree to be rebuilt if necessary.
9484 Note the SSA graph update will occur during the normal TODO
9485 processing by the pass manager. */
9487 finalize_jump_threads (void)
9489 thread_through_all_blocks (false);
9490 equiv_stack
.release ();
9494 /* Traverse all the blocks folding conditionals with known ranges. */
9501 values_propagated
= true;
9505 fprintf (dump_file
, "\nValue ranges after VRP:\n\n");
9506 dump_all_value_ranges (dump_file
);
9507 fprintf (dump_file
, "\n");
9510 substitute_and_fold (op_with_constant_singleton_value_range
,
9511 vrp_fold_stmt
, false);
9513 if (warn_array_bounds
)
9514 check_all_array_refs ();
9516 /* We must identify jump threading opportunities before we release
9517 the datastructures built by VRP. */
9518 identify_jump_threads ();
9520 /* Set value range to non pointer SSA_NAMEs. */
9521 for (i
= 0; i
< num_vr_values
; i
++)
9524 tree name
= ssa_name (i
);
9527 || POINTER_TYPE_P (TREE_TYPE (name
))
9528 || (vr_value
[i
]->type
== VR_VARYING
)
9529 || (vr_value
[i
]->type
== VR_UNDEFINED
))
9532 if ((TREE_CODE (vr_value
[i
]->min
) == INTEGER_CST
)
9533 && (TREE_CODE (vr_value
[i
]->max
) == INTEGER_CST
))
9535 if (vr_value
[i
]->type
== VR_RANGE
)
9536 set_range_info (name
, wi::to_widest (vr_value
[i
]->min
),
9537 wi::to_widest (vr_value
[i
]->max
));
9538 else if (vr_value
[i
]->type
== VR_ANTI_RANGE
)
9540 /* VR_ANTI_RANGE ~[min, max] is encoded compactly as
9541 [max + 1, min - 1] without additional attributes.
9542 When min value > max value, we know that it is
9543 VR_ANTI_RANGE; it is VR_RANGE otherwise. */
9545 /* ~[0,0] anti-range is represented as
9547 if (TYPE_UNSIGNED (TREE_TYPE (name
))
9548 && integer_zerop (vr_value
[i
]->min
)
9549 && integer_zerop (vr_value
[i
]->max
))
9551 unsigned prec
= TYPE_PRECISION (TREE_TYPE (name
));
9552 set_range_info (name
, 1,
9553 wi::mask
<widest_int
> (prec
, false));
9556 set_range_info (name
,
9557 wi::to_widest (vr_value
[i
]->max
) + 1,
9558 wi::to_widest (vr_value
[i
]->min
) - 1);
9563 /* Free allocated memory. */
9564 for (i
= 0; i
< num_vr_values
; i
++)
9567 BITMAP_FREE (vr_value
[i
]->equiv
);
9572 free (vr_phi_edge_counts
);
9574 /* So that we can distinguish between VRP data being available
9575 and not available. */
9577 vr_phi_edge_counts
= NULL
;
9581 /* Main entry point to VRP (Value Range Propagation). This pass is
9582 loosely based on J. R. C. Patterson, ``Accurate Static Branch
9583 Prediction by Value Range Propagation,'' in SIGPLAN Conference on
9584 Programming Language Design and Implementation, pp. 67-78, 1995.
9585 Also available at http://citeseer.ist.psu.edu/patterson95accurate.html
9587 This is essentially an SSA-CCP pass modified to deal with ranges
9588 instead of constants.
9590 While propagating ranges, we may find that two or more SSA name
9591 have equivalent, though distinct ranges. For instance,
9594 2 p_4 = ASSERT_EXPR <p_3, p_3 != 0>
9596 4 p_5 = ASSERT_EXPR <p_4, p_4 == q_2>;
9600 In the code above, pointer p_5 has range [q_2, q_2], but from the
9601 code we can also determine that p_5 cannot be NULL and, if q_2 had
9602 a non-varying range, p_5's range should also be compatible with it.
9604 These equivalences are created by two expressions: ASSERT_EXPR and
9605 copy operations. Since p_5 is an assertion on p_4, and p_4 was the
9606 result of another assertion, then we can use the fact that p_5 and
9607 p_4 are equivalent when evaluating p_5's range.
9609 Together with value ranges, we also propagate these equivalences
9610 between names so that we can take advantage of information from
9611 multiple ranges when doing final replacement. Note that this
9612 equivalency relation is transitive but not symmetric.
9614 In the example above, p_5 is equivalent to p_4, q_2 and p_3, but we
9615 cannot assert that q_2 is equivalent to p_5 because q_2 may be used
9616 in contexts where that assertion does not hold (e.g., in line 6).
9618 TODO, the main difference between this pass and Patterson's is that
9619 we do not propagate edge probabilities. We only compute whether
9620 edges can be taken or not. That is, instead of having a spectrum
9621 of jump probabilities between 0 and 1, we only deal with 0, 1 and
9622 DON'T KNOW. In the future, it may be worthwhile to propagate
9623 probabilities to aid branch prediction. */
9632 loop_optimizer_init (LOOPS_NORMAL
| LOOPS_HAVE_RECORDED_EXITS
);
9633 rewrite_into_loop_closed_ssa (NULL
, TODO_update_ssa
);
9636 /* ??? This ends up using stale EDGE_DFS_BACK for liveness computation.
9637 Inserting assertions may split edges which will invalidate
9639 insert_range_assertions ();
9641 to_remove_edges
.create (10);
9642 to_update_switch_stmts
.create (5);
9643 threadedge_initialize_values ();
9645 /* For visiting PHI nodes we need EDGE_DFS_BACK computed. */
9646 mark_dfs_back_edges ();
9649 ssa_propagate (vrp_visit_stmt
, vrp_visit_phi_node
);
9652 free_numbers_of_iterations_estimates ();
9654 /* ASSERT_EXPRs must be removed before finalizing jump threads
9655 as finalizing jump threads calls the CFG cleanup code which
9656 does not properly handle ASSERT_EXPRs. */
9657 remove_range_assertions ();
9659 /* If we exposed any new variables, go ahead and put them into
9660 SSA form now, before we handle jump threading. This simplifies
9661 interactions between rewriting of _DECL nodes into SSA form
9662 and rewriting SSA_NAME nodes into SSA form after block
9663 duplication and CFG manipulation. */
9664 update_ssa (TODO_update_ssa
);
9666 finalize_jump_threads ();
9668 /* Remove dead edges from SWITCH_EXPR optimization. This leaves the
9669 CFG in a broken state and requires a cfg_cleanup run. */
9670 FOR_EACH_VEC_ELT (to_remove_edges
, i
, e
)
9672 /* Update SWITCH_EXPR case label vector. */
9673 FOR_EACH_VEC_ELT (to_update_switch_stmts
, i
, su
)
9676 size_t n
= TREE_VEC_LENGTH (su
->vec
);
9678 gimple_switch_set_num_labels (su
->stmt
, n
);
9679 for (j
= 0; j
< n
; j
++)
9680 gimple_switch_set_label (su
->stmt
, j
, TREE_VEC_ELT (su
->vec
, j
));
9681 /* As we may have replaced the default label with a regular one
9682 make sure to make it a real default label again. This ensures
9683 optimal expansion. */
9684 label
= gimple_switch_label (su
->stmt
, 0);
9685 CASE_LOW (label
) = NULL_TREE
;
9686 CASE_HIGH (label
) = NULL_TREE
;
9689 if (to_remove_edges
.length () > 0)
9691 free_dominance_info (CDI_DOMINATORS
);
9693 loops_state_set (LOOPS_NEED_FIXUP
);
9696 to_remove_edges
.release ();
9697 to_update_switch_stmts
.release ();
9698 threadedge_finalize_values ();
9701 loop_optimizer_finalize ();
9708 return flag_tree_vrp
!= 0;
9713 const pass_data pass_data_vrp
=
9715 GIMPLE_PASS
, /* type */
9717 OPTGROUP_NONE
, /* optinfo_flags */
9718 true, /* has_gate */
9719 true, /* has_execute */
9720 TV_TREE_VRP
, /* tv_id */
9721 PROP_ssa
, /* properties_required */
9722 0, /* properties_provided */
9723 0, /* properties_destroyed */
9724 0, /* todo_flags_start */
9725 ( TODO_cleanup_cfg
| TODO_update_ssa
9727 | TODO_verify_flow
), /* todo_flags_finish */
9730 class pass_vrp
: public gimple_opt_pass
9733 pass_vrp (gcc::context
*ctxt
)
9734 : gimple_opt_pass (pass_data_vrp
, ctxt
)
9737 /* opt_pass methods: */
9738 opt_pass
* clone () { return new pass_vrp (m_ctxt
); }
9739 bool gate () { return gate_vrp (); }
9740 unsigned int execute () { return execute_vrp (); }
9742 }; // class pass_vrp
9747 make_pass_vrp (gcc::context
*ctxt
)
9749 return new pass_vrp (ctxt
);