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 "stor-layout.h"
30 #include "basic-block.h"
32 #include "gimple-iterator.h"
33 #include "gimple-walk.h"
34 #include "gimple-ssa.h"
36 #include "tree-phinodes.h"
37 #include "ssa-iterators.h"
38 #include "stringpool.h"
39 #include "tree-ssanames.h"
40 #include "tree-ssa-loop-manip.h"
41 #include "tree-ssa-loop-niter.h"
42 #include "tree-ssa-loop.h"
43 #include "tree-into-ssa.h"
45 #include "tree-pass.h"
46 #include "tree-dump.h"
47 #include "gimple-pretty-print.h"
48 #include "diagnostic-core.h"
51 #include "tree-scalar-evolution.h"
52 #include "tree-ssa-propagate.h"
53 #include "tree-chrec.h"
54 #include "tree-ssa-threadupdate.h"
57 #include "tree-ssa-threadedge.h"
62 /* Range of values that can be associated with an SSA_NAME after VRP
66 /* Lattice value represented by this range. */
67 enum value_range_type type
;
69 /* Minimum and maximum values represented by this range. These
70 values should be interpreted as follows:
72 - If TYPE is VR_UNDEFINED or VR_VARYING then MIN and MAX must
75 - If TYPE == VR_RANGE then MIN holds the minimum value and
76 MAX holds the maximum value of the range [MIN, MAX].
78 - If TYPE == ANTI_RANGE the variable is known to NOT
79 take any values in the range [MIN, MAX]. */
83 /* Set of SSA names whose value ranges are equivalent to this one.
84 This set is only valid when TYPE is VR_RANGE or VR_ANTI_RANGE. */
88 typedef struct value_range_d value_range_t
;
90 #define VR_INITIALIZER { VR_UNDEFINED, NULL_TREE, NULL_TREE, NULL }
92 /* Set of SSA names found live during the RPO traversal of the function
93 for still active basic-blocks. */
96 /* Return true if the SSA name NAME is live on the edge E. */
99 live_on_edge (edge e
, tree name
)
101 return (live
[e
->dest
->index
]
102 && bitmap_bit_p (live
[e
->dest
->index
], SSA_NAME_VERSION (name
)));
105 /* Local functions. */
106 static int compare_values (tree val1
, tree val2
);
107 static int compare_values_warnv (tree val1
, tree val2
, bool *);
108 static void vrp_meet (value_range_t
*, value_range_t
*);
109 static void vrp_intersect_ranges (value_range_t
*, value_range_t
*);
110 static tree
vrp_evaluate_conditional_warnv_with_ops (enum tree_code
,
111 tree
, tree
, bool, bool *,
114 /* Location information for ASSERT_EXPRs. Each instance of this
115 structure describes an ASSERT_EXPR for an SSA name. Since a single
116 SSA name may have more than one assertion associated with it, these
117 locations are kept in a linked list attached to the corresponding
119 struct assert_locus_d
121 /* Basic block where the assertion would be inserted. */
124 /* Some assertions need to be inserted on an edge (e.g., assertions
125 generated by COND_EXPRs). In those cases, BB will be NULL. */
128 /* Pointer to the statement that generated this assertion. */
129 gimple_stmt_iterator si
;
131 /* Predicate code for the ASSERT_EXPR. Must be COMPARISON_CLASS_P. */
132 enum tree_code comp_code
;
134 /* Value being compared against. */
137 /* Expression to compare. */
140 /* Next node in the linked list. */
141 struct assert_locus_d
*next
;
144 typedef struct assert_locus_d
*assert_locus_t
;
146 /* If bit I is present, it means that SSA name N_i has a list of
147 assertions that should be inserted in the IL. */
148 static bitmap need_assert_for
;
150 /* Array of locations lists where to insert assertions. ASSERTS_FOR[I]
151 holds a list of ASSERT_LOCUS_T nodes that describe where
152 ASSERT_EXPRs for SSA name N_I should be inserted. */
153 static assert_locus_t
*asserts_for
;
155 /* Value range array. After propagation, VR_VALUE[I] holds the range
156 of values that SSA name N_I may take. */
157 static unsigned num_vr_values
;
158 static value_range_t
**vr_value
;
159 static bool values_propagated
;
161 /* For a PHI node which sets SSA name N_I, VR_COUNTS[I] holds the
162 number of executable edges we saw the last time we visited the
164 static int *vr_phi_edge_counts
;
171 static vec
<edge
> to_remove_edges
;
172 static vec
<switch_update
> to_update_switch_stmts
;
175 /* Return the maximum value for TYPE. */
178 vrp_val_max (const_tree type
)
180 if (!INTEGRAL_TYPE_P (type
))
183 return TYPE_MAX_VALUE (type
);
186 /* Return the minimum value for TYPE. */
189 vrp_val_min (const_tree type
)
191 if (!INTEGRAL_TYPE_P (type
))
194 return TYPE_MIN_VALUE (type
);
197 /* Return whether VAL is equal to the maximum value of its type. This
198 will be true for a positive overflow infinity. We can't do a
199 simple equality comparison with TYPE_MAX_VALUE because C typedefs
200 and Ada subtypes can produce types whose TYPE_MAX_VALUE is not ==
201 to the integer constant with the same value in the type. */
204 vrp_val_is_max (const_tree val
)
206 tree type_max
= vrp_val_max (TREE_TYPE (val
));
207 return (val
== type_max
208 || (type_max
!= NULL_TREE
209 && operand_equal_p (val
, type_max
, 0)));
212 /* Return whether VAL is equal to the minimum value of its type. This
213 will be true for a negative overflow infinity. */
216 vrp_val_is_min (const_tree val
)
218 tree type_min
= vrp_val_min (TREE_TYPE (val
));
219 return (val
== type_min
220 || (type_min
!= NULL_TREE
221 && operand_equal_p (val
, type_min
, 0)));
225 /* Return whether TYPE should use an overflow infinity distinct from
226 TYPE_{MIN,MAX}_VALUE. We use an overflow infinity value to
227 represent a signed overflow during VRP computations. An infinity
228 is distinct from a half-range, which will go from some number to
229 TYPE_{MIN,MAX}_VALUE. */
232 needs_overflow_infinity (const_tree type
)
234 return INTEGRAL_TYPE_P (type
) && !TYPE_OVERFLOW_WRAPS (type
);
237 /* Return whether TYPE can support our overflow infinity
238 representation: we use the TREE_OVERFLOW flag, which only exists
239 for constants. If TYPE doesn't support this, we don't optimize
240 cases which would require signed overflow--we drop them to
244 supports_overflow_infinity (const_tree type
)
246 tree min
= vrp_val_min (type
), max
= vrp_val_max (type
);
247 #ifdef ENABLE_CHECKING
248 gcc_assert (needs_overflow_infinity (type
));
250 return (min
!= NULL_TREE
251 && CONSTANT_CLASS_P (min
)
253 && CONSTANT_CLASS_P (max
));
256 /* VAL is the maximum or minimum value of a type. Return a
257 corresponding overflow infinity. */
260 make_overflow_infinity (tree val
)
262 gcc_checking_assert (val
!= NULL_TREE
&& CONSTANT_CLASS_P (val
));
263 val
= copy_node (val
);
264 TREE_OVERFLOW (val
) = 1;
268 /* Return a negative overflow infinity for TYPE. */
271 negative_overflow_infinity (tree type
)
273 gcc_checking_assert (supports_overflow_infinity (type
));
274 return make_overflow_infinity (vrp_val_min (type
));
277 /* Return a positive overflow infinity for TYPE. */
280 positive_overflow_infinity (tree type
)
282 gcc_checking_assert (supports_overflow_infinity (type
));
283 return make_overflow_infinity (vrp_val_max (type
));
286 /* Return whether VAL is a negative overflow infinity. */
289 is_negative_overflow_infinity (const_tree val
)
291 return (needs_overflow_infinity (TREE_TYPE (val
))
292 && CONSTANT_CLASS_P (val
)
293 && TREE_OVERFLOW (val
)
294 && vrp_val_is_min (val
));
297 /* Return whether VAL is a positive overflow infinity. */
300 is_positive_overflow_infinity (const_tree val
)
302 return (needs_overflow_infinity (TREE_TYPE (val
))
303 && CONSTANT_CLASS_P (val
)
304 && TREE_OVERFLOW (val
)
305 && vrp_val_is_max (val
));
308 /* Return whether VAL is a positive or negative overflow infinity. */
311 is_overflow_infinity (const_tree val
)
313 return (needs_overflow_infinity (TREE_TYPE (val
))
314 && CONSTANT_CLASS_P (val
)
315 && TREE_OVERFLOW (val
)
316 && (vrp_val_is_min (val
) || vrp_val_is_max (val
)));
319 /* Return whether STMT has a constant rhs that is_overflow_infinity. */
322 stmt_overflow_infinity (gimple stmt
)
324 if (is_gimple_assign (stmt
)
325 && get_gimple_rhs_class (gimple_assign_rhs_code (stmt
)) ==
327 return is_overflow_infinity (gimple_assign_rhs1 (stmt
));
331 /* If VAL is now an overflow infinity, return VAL. Otherwise, return
332 the same value with TREE_OVERFLOW clear. This can be used to avoid
333 confusing a regular value with an overflow value. */
336 avoid_overflow_infinity (tree val
)
338 if (!is_overflow_infinity (val
))
341 if (vrp_val_is_max (val
))
342 return vrp_val_max (TREE_TYPE (val
));
345 gcc_checking_assert (vrp_val_is_min (val
));
346 return vrp_val_min (TREE_TYPE (val
));
351 /* Return true if ARG is marked with the nonnull attribute in the
352 current function signature. */
355 nonnull_arg_p (const_tree arg
)
357 tree t
, attrs
, fntype
;
358 unsigned HOST_WIDE_INT arg_num
;
360 gcc_assert (TREE_CODE (arg
) == PARM_DECL
&& POINTER_TYPE_P (TREE_TYPE (arg
)));
362 /* The static chain decl is always non null. */
363 if (arg
== cfun
->static_chain_decl
)
366 fntype
= TREE_TYPE (current_function_decl
);
367 for (attrs
= TYPE_ATTRIBUTES (fntype
); attrs
; attrs
= TREE_CHAIN (attrs
))
369 attrs
= lookup_attribute ("nonnull", attrs
);
371 /* If "nonnull" wasn't specified, we know nothing about the argument. */
372 if (attrs
== NULL_TREE
)
375 /* If "nonnull" applies to all the arguments, then ARG is non-null. */
376 if (TREE_VALUE (attrs
) == NULL_TREE
)
379 /* Get the position number for ARG in the function signature. */
380 for (arg_num
= 1, t
= DECL_ARGUMENTS (current_function_decl
);
382 t
= DECL_CHAIN (t
), arg_num
++)
388 gcc_assert (t
== arg
);
390 /* Now see if ARG_NUM is mentioned in the nonnull list. */
391 for (t
= TREE_VALUE (attrs
); t
; t
= TREE_CHAIN (t
))
393 if (compare_tree_int (TREE_VALUE (t
), arg_num
) == 0)
402 /* Set value range VR to VR_UNDEFINED. */
405 set_value_range_to_undefined (value_range_t
*vr
)
407 vr
->type
= VR_UNDEFINED
;
408 vr
->min
= vr
->max
= NULL_TREE
;
410 bitmap_clear (vr
->equiv
);
414 /* Set value range VR to VR_VARYING. */
417 set_value_range_to_varying (value_range_t
*vr
)
419 vr
->type
= VR_VARYING
;
420 vr
->min
= vr
->max
= NULL_TREE
;
422 bitmap_clear (vr
->equiv
);
426 /* Set value range VR to {T, MIN, MAX, EQUIV}. */
429 set_value_range (value_range_t
*vr
, enum value_range_type t
, tree min
,
430 tree max
, bitmap equiv
)
432 #if defined ENABLE_CHECKING
433 /* Check the validity of the range. */
434 if (t
== VR_RANGE
|| t
== VR_ANTI_RANGE
)
438 gcc_assert (min
&& max
);
440 if (INTEGRAL_TYPE_P (TREE_TYPE (min
)) && t
== VR_ANTI_RANGE
)
441 gcc_assert (!vrp_val_is_min (min
) || !vrp_val_is_max (max
));
443 cmp
= compare_values (min
, max
);
444 gcc_assert (cmp
== 0 || cmp
== -1 || cmp
== -2);
446 if (needs_overflow_infinity (TREE_TYPE (min
)))
447 gcc_assert (!is_overflow_infinity (min
)
448 || !is_overflow_infinity (max
));
451 if (t
== VR_UNDEFINED
|| t
== VR_VARYING
)
452 gcc_assert (min
== NULL_TREE
&& max
== NULL_TREE
);
454 if (t
== VR_UNDEFINED
|| t
== VR_VARYING
)
455 gcc_assert (equiv
== NULL
|| bitmap_empty_p (equiv
));
462 /* Since updating the equivalence set involves deep copying the
463 bitmaps, only do it if absolutely necessary. */
464 if (vr
->equiv
== NULL
466 vr
->equiv
= BITMAP_ALLOC (NULL
);
468 if (equiv
!= vr
->equiv
)
470 if (equiv
&& !bitmap_empty_p (equiv
))
471 bitmap_copy (vr
->equiv
, equiv
);
473 bitmap_clear (vr
->equiv
);
478 /* Set value range VR to the canonical form of {T, MIN, MAX, EQUIV}.
479 This means adjusting T, MIN and MAX representing the case of a
480 wrapping range with MAX < MIN covering [MIN, type_max] U [type_min, MAX]
481 as anti-rage ~[MAX+1, MIN-1]. Likewise for wrapping anti-ranges.
482 In corner cases where MAX+1 or MIN-1 wraps this will fall back
484 This routine exists to ease canonicalization in the case where we
485 extract ranges from var + CST op limit. */
488 set_and_canonicalize_value_range (value_range_t
*vr
, enum value_range_type t
,
489 tree min
, tree max
, bitmap equiv
)
491 /* Use the canonical setters for VR_UNDEFINED and VR_VARYING. */
492 if (t
== VR_UNDEFINED
)
494 set_value_range_to_undefined (vr
);
497 else if (t
== VR_VARYING
)
499 set_value_range_to_varying (vr
);
503 /* Nothing to canonicalize for symbolic ranges. */
504 if (TREE_CODE (min
) != INTEGER_CST
505 || TREE_CODE (max
) != INTEGER_CST
)
507 set_value_range (vr
, t
, min
, max
, equiv
);
511 /* Wrong order for min and max, to swap them and the VR type we need
513 if (tree_int_cst_lt (max
, min
))
517 /* For one bit precision if max < min, then the swapped
518 range covers all values, so for VR_RANGE it is varying and
519 for VR_ANTI_RANGE empty range, so drop to varying as well. */
520 if (TYPE_PRECISION (TREE_TYPE (min
)) == 1)
522 set_value_range_to_varying (vr
);
526 one
= build_int_cst (TREE_TYPE (min
), 1);
527 tmp
= int_const_binop (PLUS_EXPR
, max
, one
);
528 max
= int_const_binop (MINUS_EXPR
, min
, one
);
531 /* There's one corner case, if we had [C+1, C] before we now have
532 that again. But this represents an empty value range, so drop
533 to varying in this case. */
534 if (tree_int_cst_lt (max
, min
))
536 set_value_range_to_varying (vr
);
540 t
= t
== VR_RANGE
? VR_ANTI_RANGE
: VR_RANGE
;
543 /* Anti-ranges that can be represented as ranges should be so. */
544 if (t
== VR_ANTI_RANGE
)
546 bool is_min
= vrp_val_is_min (min
);
547 bool is_max
= vrp_val_is_max (max
);
549 if (is_min
&& is_max
)
551 /* We cannot deal with empty ranges, drop to varying.
552 ??? This could be VR_UNDEFINED instead. */
553 set_value_range_to_varying (vr
);
556 else if (TYPE_PRECISION (TREE_TYPE (min
)) == 1
557 && (is_min
|| is_max
))
559 /* Non-empty boolean ranges can always be represented
560 as a singleton range. */
562 min
= max
= vrp_val_max (TREE_TYPE (min
));
564 min
= max
= vrp_val_min (TREE_TYPE (min
));
568 /* As a special exception preserve non-null ranges. */
569 && !(TYPE_UNSIGNED (TREE_TYPE (min
))
570 && integer_zerop (max
)))
572 tree one
= build_int_cst (TREE_TYPE (max
), 1);
573 min
= int_const_binop (PLUS_EXPR
, max
, one
);
574 max
= vrp_val_max (TREE_TYPE (max
));
579 tree one
= build_int_cst (TREE_TYPE (min
), 1);
580 max
= int_const_binop (MINUS_EXPR
, min
, one
);
581 min
= vrp_val_min (TREE_TYPE (min
));
586 /* Drop [-INF(OVF), +INF(OVF)] to varying. */
587 if (needs_overflow_infinity (TREE_TYPE (min
))
588 && is_overflow_infinity (min
)
589 && is_overflow_infinity (max
))
591 set_value_range_to_varying (vr
);
595 set_value_range (vr
, t
, min
, max
, equiv
);
598 /* Copy value range FROM into value range TO. */
601 copy_value_range (value_range_t
*to
, value_range_t
*from
)
603 set_value_range (to
, from
->type
, from
->min
, from
->max
, from
->equiv
);
606 /* Set value range VR to a single value. This function is only called
607 with values we get from statements, and exists to clear the
608 TREE_OVERFLOW flag so that we don't think we have an overflow
609 infinity when we shouldn't. */
612 set_value_range_to_value (value_range_t
*vr
, tree val
, bitmap equiv
)
614 gcc_assert (is_gimple_min_invariant (val
));
615 val
= avoid_overflow_infinity (val
);
616 set_value_range (vr
, VR_RANGE
, val
, val
, equiv
);
619 /* Set value range VR to a non-negative range of type TYPE.
620 OVERFLOW_INFINITY indicates whether to use an overflow infinity
621 rather than TYPE_MAX_VALUE; this should be true if we determine
622 that the range is nonnegative based on the assumption that signed
623 overflow does not occur. */
626 set_value_range_to_nonnegative (value_range_t
*vr
, tree type
,
627 bool overflow_infinity
)
631 if (overflow_infinity
&& !supports_overflow_infinity (type
))
633 set_value_range_to_varying (vr
);
637 zero
= build_int_cst (type
, 0);
638 set_value_range (vr
, VR_RANGE
, zero
,
640 ? positive_overflow_infinity (type
)
641 : TYPE_MAX_VALUE (type
)),
645 /* Set value range VR to a non-NULL range of type TYPE. */
648 set_value_range_to_nonnull (value_range_t
*vr
, tree type
)
650 tree zero
= build_int_cst (type
, 0);
651 set_value_range (vr
, VR_ANTI_RANGE
, zero
, zero
, vr
->equiv
);
655 /* Set value range VR to a NULL range of type TYPE. */
658 set_value_range_to_null (value_range_t
*vr
, tree type
)
660 set_value_range_to_value (vr
, build_int_cst (type
, 0), vr
->equiv
);
664 /* Set value range VR to a range of a truthvalue of type TYPE. */
667 set_value_range_to_truthvalue (value_range_t
*vr
, tree type
)
669 if (TYPE_PRECISION (type
) == 1)
670 set_value_range_to_varying (vr
);
672 set_value_range (vr
, VR_RANGE
,
673 build_int_cst (type
, 0), build_int_cst (type
, 1),
678 /* If abs (min) < abs (max), set VR to [-max, max], if
679 abs (min) >= abs (max), set VR to [-min, min]. */
682 abs_extent_range (value_range_t
*vr
, tree min
, tree max
)
686 gcc_assert (TREE_CODE (min
) == INTEGER_CST
);
687 gcc_assert (TREE_CODE (max
) == INTEGER_CST
);
688 gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (min
)));
689 gcc_assert (!TYPE_UNSIGNED (TREE_TYPE (min
)));
690 min
= fold_unary (ABS_EXPR
, TREE_TYPE (min
), min
);
691 max
= fold_unary (ABS_EXPR
, TREE_TYPE (max
), max
);
692 if (TREE_OVERFLOW (min
) || TREE_OVERFLOW (max
))
694 set_value_range_to_varying (vr
);
697 cmp
= compare_values (min
, max
);
699 min
= fold_unary (NEGATE_EXPR
, TREE_TYPE (min
), max
);
700 else if (cmp
== 0 || cmp
== 1)
703 min
= fold_unary (NEGATE_EXPR
, TREE_TYPE (min
), min
);
707 set_value_range_to_varying (vr
);
710 set_and_canonicalize_value_range (vr
, VR_RANGE
, min
, max
, NULL
);
714 /* Return value range information for VAR.
716 If we have no values ranges recorded (ie, VRP is not running), then
717 return NULL. Otherwise create an empty range if none existed for VAR. */
719 static value_range_t
*
720 get_value_range (const_tree var
)
722 static const struct value_range_d vr_const_varying
723 = { VR_VARYING
, NULL_TREE
, NULL_TREE
, NULL
};
726 unsigned ver
= SSA_NAME_VERSION (var
);
728 /* If we have no recorded ranges, then return NULL. */
732 /* If we query the range for a new SSA name return an unmodifiable VARYING.
733 We should get here at most from the substitute-and-fold stage which
734 will never try to change values. */
735 if (ver
>= num_vr_values
)
736 return CONST_CAST (value_range_t
*, &vr_const_varying
);
742 /* After propagation finished do not allocate new value-ranges. */
743 if (values_propagated
)
744 return CONST_CAST (value_range_t
*, &vr_const_varying
);
746 /* Create a default value range. */
747 vr_value
[ver
] = vr
= XCNEW (value_range_t
);
749 /* Defer allocating the equivalence set. */
752 /* If VAR is a default definition of a parameter, the variable can
753 take any value in VAR's type. */
754 if (SSA_NAME_IS_DEFAULT_DEF (var
))
756 sym
= SSA_NAME_VAR (var
);
757 if (TREE_CODE (sym
) == PARM_DECL
)
759 /* Try to use the "nonnull" attribute to create ~[0, 0]
760 anti-ranges for pointers. Note that this is only valid with
761 default definitions of PARM_DECLs. */
762 if (POINTER_TYPE_P (TREE_TYPE (sym
))
763 && nonnull_arg_p (sym
))
764 set_value_range_to_nonnull (vr
, TREE_TYPE (sym
));
766 set_value_range_to_varying (vr
);
768 else if (TREE_CODE (sym
) == RESULT_DECL
769 && DECL_BY_REFERENCE (sym
))
770 set_value_range_to_nonnull (vr
, TREE_TYPE (sym
));
776 /* Return true, if VAL1 and VAL2 are equal values for VRP purposes. */
779 vrp_operand_equal_p (const_tree val1
, const_tree val2
)
783 if (!val1
|| !val2
|| !operand_equal_p (val1
, val2
, 0))
785 if (is_overflow_infinity (val1
))
786 return is_overflow_infinity (val2
);
790 /* Return true, if the bitmaps B1 and B2 are equal. */
793 vrp_bitmap_equal_p (const_bitmap b1
, const_bitmap b2
)
796 || ((!b1
|| bitmap_empty_p (b1
))
797 && (!b2
|| bitmap_empty_p (b2
)))
799 && bitmap_equal_p (b1
, b2
)));
802 /* Update the value range and equivalence set for variable VAR to
803 NEW_VR. Return true if NEW_VR is different from VAR's previous
806 NOTE: This function assumes that NEW_VR is a temporary value range
807 object created for the sole purpose of updating VAR's range. The
808 storage used by the equivalence set from NEW_VR will be freed by
809 this function. Do not call update_value_range when NEW_VR
810 is the range object associated with another SSA name. */
813 update_value_range (const_tree var
, value_range_t
*new_vr
)
815 value_range_t
*old_vr
;
818 /* Update the value range, if necessary. */
819 old_vr
= get_value_range (var
);
820 is_new
= old_vr
->type
!= new_vr
->type
821 || !vrp_operand_equal_p (old_vr
->min
, new_vr
->min
)
822 || !vrp_operand_equal_p (old_vr
->max
, new_vr
->max
)
823 || !vrp_bitmap_equal_p (old_vr
->equiv
, new_vr
->equiv
);
827 /* Do not allow transitions up the lattice. The following
828 is slightly more awkward than just new_vr->type < old_vr->type
829 because VR_RANGE and VR_ANTI_RANGE need to be considered
830 the same. We may not have is_new when transitioning to
831 UNDEFINED or from VARYING. */
832 if (new_vr
->type
== VR_UNDEFINED
833 || old_vr
->type
== VR_VARYING
)
834 set_value_range_to_varying (old_vr
);
836 set_value_range (old_vr
, new_vr
->type
, new_vr
->min
, new_vr
->max
,
840 BITMAP_FREE (new_vr
->equiv
);
846 /* Add VAR and VAR's equivalence set to EQUIV. This is the central
847 point where equivalence processing can be turned on/off. */
850 add_equivalence (bitmap
*equiv
, const_tree var
)
852 unsigned ver
= SSA_NAME_VERSION (var
);
853 value_range_t
*vr
= vr_value
[ver
];
856 *equiv
= BITMAP_ALLOC (NULL
);
857 bitmap_set_bit (*equiv
, ver
);
859 bitmap_ior_into (*equiv
, vr
->equiv
);
863 /* Return true if VR is ~[0, 0]. */
866 range_is_nonnull (value_range_t
*vr
)
868 return vr
->type
== VR_ANTI_RANGE
869 && integer_zerop (vr
->min
)
870 && integer_zerop (vr
->max
);
874 /* Return true if VR is [0, 0]. */
877 range_is_null (value_range_t
*vr
)
879 return vr
->type
== VR_RANGE
880 && integer_zerop (vr
->min
)
881 && integer_zerop (vr
->max
);
884 /* Return true if max and min of VR are INTEGER_CST. It's not necessary
888 range_int_cst_p (value_range_t
*vr
)
890 return (vr
->type
== VR_RANGE
891 && TREE_CODE (vr
->max
) == INTEGER_CST
892 && TREE_CODE (vr
->min
) == INTEGER_CST
);
895 /* Return true if VR is a INTEGER_CST singleton. */
898 range_int_cst_singleton_p (value_range_t
*vr
)
900 return (range_int_cst_p (vr
)
901 && !is_overflow_infinity (vr
->min
)
902 && !is_overflow_infinity (vr
->max
)
903 && tree_int_cst_equal (vr
->min
, vr
->max
));
906 /* Return true if value range VR involves at least one symbol. */
909 symbolic_range_p (value_range_t
*vr
)
911 return (!is_gimple_min_invariant (vr
->min
)
912 || !is_gimple_min_invariant (vr
->max
));
915 /* Return true if value range VR uses an overflow infinity. */
918 overflow_infinity_range_p (value_range_t
*vr
)
920 return (vr
->type
== VR_RANGE
921 && (is_overflow_infinity (vr
->min
)
922 || is_overflow_infinity (vr
->max
)));
925 /* Return false if we can not make a valid comparison based on VR;
926 this will be the case if it uses an overflow infinity and overflow
927 is not undefined (i.e., -fno-strict-overflow is in effect).
928 Otherwise return true, and set *STRICT_OVERFLOW_P to true if VR
929 uses an overflow infinity. */
932 usable_range_p (value_range_t
*vr
, bool *strict_overflow_p
)
934 gcc_assert (vr
->type
== VR_RANGE
);
935 if (is_overflow_infinity (vr
->min
))
937 *strict_overflow_p
= true;
938 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (vr
->min
)))
941 if (is_overflow_infinity (vr
->max
))
943 *strict_overflow_p
= true;
944 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (vr
->max
)))
951 /* Return true if the result of assignment STMT is know to be non-negative.
952 If the return value is based on the assumption that signed overflow is
953 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
954 *STRICT_OVERFLOW_P.*/
957 gimple_assign_nonnegative_warnv_p (gimple stmt
, bool *strict_overflow_p
)
959 enum tree_code code
= gimple_assign_rhs_code (stmt
);
960 switch (get_gimple_rhs_class (code
))
962 case GIMPLE_UNARY_RHS
:
963 return tree_unary_nonnegative_warnv_p (gimple_assign_rhs_code (stmt
),
964 gimple_expr_type (stmt
),
965 gimple_assign_rhs1 (stmt
),
967 case GIMPLE_BINARY_RHS
:
968 return tree_binary_nonnegative_warnv_p (gimple_assign_rhs_code (stmt
),
969 gimple_expr_type (stmt
),
970 gimple_assign_rhs1 (stmt
),
971 gimple_assign_rhs2 (stmt
),
973 case GIMPLE_TERNARY_RHS
:
975 case GIMPLE_SINGLE_RHS
:
976 return tree_single_nonnegative_warnv_p (gimple_assign_rhs1 (stmt
),
978 case GIMPLE_INVALID_RHS
:
985 /* Return true if return value of call STMT is know to be non-negative.
986 If the return value is based on the assumption that signed overflow is
987 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
988 *STRICT_OVERFLOW_P.*/
991 gimple_call_nonnegative_warnv_p (gimple stmt
, bool *strict_overflow_p
)
993 tree arg0
= gimple_call_num_args (stmt
) > 0 ?
994 gimple_call_arg (stmt
, 0) : NULL_TREE
;
995 tree arg1
= gimple_call_num_args (stmt
) > 1 ?
996 gimple_call_arg (stmt
, 1) : NULL_TREE
;
998 return tree_call_nonnegative_warnv_p (gimple_expr_type (stmt
),
999 gimple_call_fndecl (stmt
),
1005 /* Return true if STMT is know to to compute a non-negative value.
1006 If the return value is based on the assumption that signed overflow is
1007 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1008 *STRICT_OVERFLOW_P.*/
1011 gimple_stmt_nonnegative_warnv_p (gimple stmt
, bool *strict_overflow_p
)
1013 switch (gimple_code (stmt
))
1016 return gimple_assign_nonnegative_warnv_p (stmt
, strict_overflow_p
);
1018 return gimple_call_nonnegative_warnv_p (stmt
, strict_overflow_p
);
1024 /* Return true if the result of assignment STMT is know to be non-zero.
1025 If the return value is based on the assumption that signed overflow is
1026 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1027 *STRICT_OVERFLOW_P.*/
1030 gimple_assign_nonzero_warnv_p (gimple stmt
, bool *strict_overflow_p
)
1032 enum tree_code code
= gimple_assign_rhs_code (stmt
);
1033 switch (get_gimple_rhs_class (code
))
1035 case GIMPLE_UNARY_RHS
:
1036 return tree_unary_nonzero_warnv_p (gimple_assign_rhs_code (stmt
),
1037 gimple_expr_type (stmt
),
1038 gimple_assign_rhs1 (stmt
),
1040 case GIMPLE_BINARY_RHS
:
1041 return tree_binary_nonzero_warnv_p (gimple_assign_rhs_code (stmt
),
1042 gimple_expr_type (stmt
),
1043 gimple_assign_rhs1 (stmt
),
1044 gimple_assign_rhs2 (stmt
),
1046 case GIMPLE_TERNARY_RHS
:
1048 case GIMPLE_SINGLE_RHS
:
1049 return tree_single_nonzero_warnv_p (gimple_assign_rhs1 (stmt
),
1051 case GIMPLE_INVALID_RHS
:
1058 /* Return true if STMT is known to compute a non-zero value.
1059 If the return value is based on the assumption that signed overflow is
1060 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1061 *STRICT_OVERFLOW_P.*/
1064 gimple_stmt_nonzero_warnv_p (gimple stmt
, bool *strict_overflow_p
)
1066 switch (gimple_code (stmt
))
1069 return gimple_assign_nonzero_warnv_p (stmt
, strict_overflow_p
);
1072 tree fndecl
= gimple_call_fndecl (stmt
);
1073 if (!fndecl
) return false;
1074 if (flag_delete_null_pointer_checks
&& !flag_check_new
1075 && DECL_IS_OPERATOR_NEW (fndecl
)
1076 && !TREE_NOTHROW (fndecl
))
1078 if (flag_delete_null_pointer_checks
&&
1079 lookup_attribute ("returns_nonnull",
1080 TYPE_ATTRIBUTES (gimple_call_fntype (stmt
))))
1082 return gimple_alloca_call_p (stmt
);
1089 /* Like tree_expr_nonzero_warnv_p, but this function uses value ranges
1093 vrp_stmt_computes_nonzero (gimple stmt
, bool *strict_overflow_p
)
1095 if (gimple_stmt_nonzero_warnv_p (stmt
, strict_overflow_p
))
1098 /* If we have an expression of the form &X->a, then the expression
1099 is nonnull if X is nonnull. */
1100 if (is_gimple_assign (stmt
)
1101 && gimple_assign_rhs_code (stmt
) == ADDR_EXPR
)
1103 tree expr
= gimple_assign_rhs1 (stmt
);
1104 tree base
= get_base_address (TREE_OPERAND (expr
, 0));
1106 if (base
!= NULL_TREE
1107 && TREE_CODE (base
) == MEM_REF
1108 && TREE_CODE (TREE_OPERAND (base
, 0)) == SSA_NAME
)
1110 value_range_t
*vr
= get_value_range (TREE_OPERAND (base
, 0));
1111 if (range_is_nonnull (vr
))
1119 /* Returns true if EXPR is a valid value (as expected by compare_values) --
1120 a gimple invariant, or SSA_NAME +- CST. */
1123 valid_value_p (tree expr
)
1125 if (TREE_CODE (expr
) == SSA_NAME
)
1128 if (TREE_CODE (expr
) == PLUS_EXPR
1129 || TREE_CODE (expr
) == MINUS_EXPR
)
1130 return (TREE_CODE (TREE_OPERAND (expr
, 0)) == SSA_NAME
1131 && TREE_CODE (TREE_OPERAND (expr
, 1)) == INTEGER_CST
);
1133 return is_gimple_min_invariant (expr
);
1139 -2 if those are incomparable. */
1141 operand_less_p (tree val
, tree val2
)
1143 /* LT is folded faster than GE and others. Inline the common case. */
1144 if (TREE_CODE (val
) == INTEGER_CST
&& TREE_CODE (val2
) == INTEGER_CST
)
1145 return INT_CST_LT (val
, val2
);
1150 fold_defer_overflow_warnings ();
1152 tcmp
= fold_binary_to_constant (LT_EXPR
, boolean_type_node
, val
, val2
);
1154 fold_undefer_and_ignore_overflow_warnings ();
1157 || TREE_CODE (tcmp
) != INTEGER_CST
)
1160 if (!integer_zerop (tcmp
))
1164 /* val >= val2, not considering overflow infinity. */
1165 if (is_negative_overflow_infinity (val
))
1166 return is_negative_overflow_infinity (val2
) ? 0 : 1;
1167 else if (is_positive_overflow_infinity (val2
))
1168 return is_positive_overflow_infinity (val
) ? 0 : 1;
1173 /* Compare two values VAL1 and VAL2. Return
1175 -2 if VAL1 and VAL2 cannot be compared at compile-time,
1178 +1 if VAL1 > VAL2, and
1181 This is similar to tree_int_cst_compare but supports pointer values
1182 and values that cannot be compared at compile time.
1184 If STRICT_OVERFLOW_P is not NULL, then set *STRICT_OVERFLOW_P to
1185 true if the return value is only valid if we assume that signed
1186 overflow is undefined. */
1189 compare_values_warnv (tree val1
, tree val2
, bool *strict_overflow_p
)
1194 /* Below we rely on the fact that VAL1 and VAL2 are both pointers or
1196 gcc_assert (POINTER_TYPE_P (TREE_TYPE (val1
))
1197 == POINTER_TYPE_P (TREE_TYPE (val2
)));
1198 /* Convert the two values into the same type. This is needed because
1199 sizetype causes sign extension even for unsigned types. */
1200 val2
= fold_convert (TREE_TYPE (val1
), val2
);
1201 STRIP_USELESS_TYPE_CONVERSION (val2
);
1203 if ((TREE_CODE (val1
) == SSA_NAME
1204 || TREE_CODE (val1
) == PLUS_EXPR
1205 || TREE_CODE (val1
) == MINUS_EXPR
)
1206 && (TREE_CODE (val2
) == SSA_NAME
1207 || TREE_CODE (val2
) == PLUS_EXPR
1208 || TREE_CODE (val2
) == MINUS_EXPR
))
1210 tree n1
, c1
, n2
, c2
;
1211 enum tree_code code1
, code2
;
1213 /* If VAL1 and VAL2 are of the form 'NAME [+-] CST' or 'NAME',
1214 return -1 or +1 accordingly. If VAL1 and VAL2 don't use the
1215 same name, return -2. */
1216 if (TREE_CODE (val1
) == SSA_NAME
)
1224 code1
= TREE_CODE (val1
);
1225 n1
= TREE_OPERAND (val1
, 0);
1226 c1
= TREE_OPERAND (val1
, 1);
1227 if (tree_int_cst_sgn (c1
) == -1)
1229 if (is_negative_overflow_infinity (c1
))
1231 c1
= fold_unary_to_constant (NEGATE_EXPR
, TREE_TYPE (c1
), c1
);
1234 code1
= code1
== MINUS_EXPR
? PLUS_EXPR
: MINUS_EXPR
;
1238 if (TREE_CODE (val2
) == SSA_NAME
)
1246 code2
= TREE_CODE (val2
);
1247 n2
= TREE_OPERAND (val2
, 0);
1248 c2
= TREE_OPERAND (val2
, 1);
1249 if (tree_int_cst_sgn (c2
) == -1)
1251 if (is_negative_overflow_infinity (c2
))
1253 c2
= fold_unary_to_constant (NEGATE_EXPR
, TREE_TYPE (c2
), c2
);
1256 code2
= code2
== MINUS_EXPR
? PLUS_EXPR
: MINUS_EXPR
;
1260 /* Both values must use the same name. */
1264 if (code1
== SSA_NAME
1265 && code2
== SSA_NAME
)
1269 /* If overflow is defined we cannot simplify more. */
1270 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (val1
)))
1273 if (strict_overflow_p
!= NULL
1274 && (code1
== SSA_NAME
|| !TREE_NO_WARNING (val1
))
1275 && (code2
== SSA_NAME
|| !TREE_NO_WARNING (val2
)))
1276 *strict_overflow_p
= true;
1278 if (code1
== SSA_NAME
)
1280 if (code2
== PLUS_EXPR
)
1281 /* NAME < NAME + CST */
1283 else if (code2
== MINUS_EXPR
)
1284 /* NAME > NAME - CST */
1287 else if (code1
== PLUS_EXPR
)
1289 if (code2
== SSA_NAME
)
1290 /* NAME + CST > NAME */
1292 else if (code2
== PLUS_EXPR
)
1293 /* NAME + CST1 > NAME + CST2, if CST1 > CST2 */
1294 return compare_values_warnv (c1
, c2
, strict_overflow_p
);
1295 else if (code2
== MINUS_EXPR
)
1296 /* NAME + CST1 > NAME - CST2 */
1299 else if (code1
== MINUS_EXPR
)
1301 if (code2
== SSA_NAME
)
1302 /* NAME - CST < NAME */
1304 else if (code2
== PLUS_EXPR
)
1305 /* NAME - CST1 < NAME + CST2 */
1307 else if (code2
== MINUS_EXPR
)
1308 /* NAME - CST1 > NAME - CST2, if CST1 < CST2. Notice that
1309 C1 and C2 are swapped in the call to compare_values. */
1310 return compare_values_warnv (c2
, c1
, strict_overflow_p
);
1316 /* We cannot compare non-constants. */
1317 if (!is_gimple_min_invariant (val1
) || !is_gimple_min_invariant (val2
))
1320 if (!POINTER_TYPE_P (TREE_TYPE (val1
)))
1322 /* We cannot compare overflowed values, except for overflow
1324 if (TREE_OVERFLOW (val1
) || TREE_OVERFLOW (val2
))
1326 if (strict_overflow_p
!= NULL
)
1327 *strict_overflow_p
= true;
1328 if (is_negative_overflow_infinity (val1
))
1329 return is_negative_overflow_infinity (val2
) ? 0 : -1;
1330 else if (is_negative_overflow_infinity (val2
))
1332 else if (is_positive_overflow_infinity (val1
))
1333 return is_positive_overflow_infinity (val2
) ? 0 : 1;
1334 else if (is_positive_overflow_infinity (val2
))
1339 return tree_int_cst_compare (val1
, val2
);
1345 /* First see if VAL1 and VAL2 are not the same. */
1346 if (val1
== val2
|| operand_equal_p (val1
, val2
, 0))
1349 /* If VAL1 is a lower address than VAL2, return -1. */
1350 if (operand_less_p (val1
, val2
) == 1)
1353 /* If VAL1 is a higher address than VAL2, return +1. */
1354 if (operand_less_p (val2
, val1
) == 1)
1357 /* If VAL1 is different than VAL2, return +2.
1358 For integer constants we either have already returned -1 or 1
1359 or they are equivalent. We still might succeed in proving
1360 something about non-trivial operands. */
1361 if (TREE_CODE (val1
) != INTEGER_CST
1362 || TREE_CODE (val2
) != INTEGER_CST
)
1364 t
= fold_binary_to_constant (NE_EXPR
, boolean_type_node
, val1
, val2
);
1365 if (t
&& integer_onep (t
))
1373 /* Compare values like compare_values_warnv, but treat comparisons of
1374 nonconstants which rely on undefined overflow as incomparable. */
1377 compare_values (tree val1
, tree val2
)
1383 ret
= compare_values_warnv (val1
, val2
, &sop
);
1385 && (!is_gimple_min_invariant (val1
) || !is_gimple_min_invariant (val2
)))
1391 /* Return 1 if VAL is inside value range MIN <= VAL <= MAX,
1392 0 if VAL is not inside [MIN, MAX],
1393 -2 if we cannot tell either way.
1395 Benchmark compile/20001226-1.c compilation time after changing this
1399 value_inside_range (tree val
, tree min
, tree max
)
1403 cmp1
= operand_less_p (val
, min
);
1409 cmp2
= operand_less_p (max
, val
);
1417 /* Return true if value ranges VR0 and VR1 have a non-empty
1420 Benchmark compile/20001226-1.c compilation time after changing this
1425 value_ranges_intersect_p (value_range_t
*vr0
, value_range_t
*vr1
)
1427 /* The value ranges do not intersect if the maximum of the first range is
1428 less than the minimum of the second range or vice versa.
1429 When those relations are unknown, we can't do any better. */
1430 if (operand_less_p (vr0
->max
, vr1
->min
) != 0)
1432 if (operand_less_p (vr1
->max
, vr0
->min
) != 0)
1438 /* Return 1 if [MIN, MAX] includes the value zero, 0 if it does not
1439 include the value zero, -2 if we cannot tell. */
1442 range_includes_zero_p (tree min
, tree max
)
1444 tree zero
= build_int_cst (TREE_TYPE (min
), 0);
1445 return value_inside_range (zero
, min
, max
);
1448 /* Return true if *VR is know to only contain nonnegative values. */
1451 value_range_nonnegative_p (value_range_t
*vr
)
1453 /* Testing for VR_ANTI_RANGE is not useful here as any anti-range
1454 which would return a useful value should be encoded as a
1456 if (vr
->type
== VR_RANGE
)
1458 int result
= compare_values (vr
->min
, integer_zero_node
);
1459 return (result
== 0 || result
== 1);
1465 /* If *VR has a value rante that is a single constant value return that,
1466 otherwise return NULL_TREE. */
1469 value_range_constant_singleton (value_range_t
*vr
)
1471 if (vr
->type
== VR_RANGE
1472 && operand_equal_p (vr
->min
, vr
->max
, 0)
1473 && is_gimple_min_invariant (vr
->min
))
1479 /* If OP has a value range with a single constant value return that,
1480 otherwise return NULL_TREE. This returns OP itself if OP is a
1484 op_with_constant_singleton_value_range (tree op
)
1486 if (is_gimple_min_invariant (op
))
1489 if (TREE_CODE (op
) != SSA_NAME
)
1492 return value_range_constant_singleton (get_value_range (op
));
1495 /* Return true if op is in a boolean [0, 1] value-range. */
1498 op_with_boolean_value_range_p (tree op
)
1502 if (TYPE_PRECISION (TREE_TYPE (op
)) == 1)
1505 if (integer_zerop (op
)
1506 || integer_onep (op
))
1509 if (TREE_CODE (op
) != SSA_NAME
)
1512 vr
= get_value_range (op
);
1513 return (vr
->type
== VR_RANGE
1514 && integer_zerop (vr
->min
)
1515 && integer_onep (vr
->max
));
1518 /* Extract value range information from an ASSERT_EXPR EXPR and store
1522 extract_range_from_assert (value_range_t
*vr_p
, tree expr
)
1524 tree var
, cond
, limit
, min
, max
, type
;
1525 value_range_t
*limit_vr
;
1526 enum tree_code cond_code
;
1528 var
= ASSERT_EXPR_VAR (expr
);
1529 cond
= ASSERT_EXPR_COND (expr
);
1531 gcc_assert (COMPARISON_CLASS_P (cond
));
1533 /* Find VAR in the ASSERT_EXPR conditional. */
1534 if (var
== TREE_OPERAND (cond
, 0)
1535 || TREE_CODE (TREE_OPERAND (cond
, 0)) == PLUS_EXPR
1536 || TREE_CODE (TREE_OPERAND (cond
, 0)) == NOP_EXPR
)
1538 /* If the predicate is of the form VAR COMP LIMIT, then we just
1539 take LIMIT from the RHS and use the same comparison code. */
1540 cond_code
= TREE_CODE (cond
);
1541 limit
= TREE_OPERAND (cond
, 1);
1542 cond
= TREE_OPERAND (cond
, 0);
1546 /* If the predicate is of the form LIMIT COMP VAR, then we need
1547 to flip around the comparison code to create the proper range
1549 cond_code
= swap_tree_comparison (TREE_CODE (cond
));
1550 limit
= TREE_OPERAND (cond
, 0);
1551 cond
= TREE_OPERAND (cond
, 1);
1554 limit
= avoid_overflow_infinity (limit
);
1556 type
= TREE_TYPE (var
);
1557 gcc_assert (limit
!= var
);
1559 /* For pointer arithmetic, we only keep track of pointer equality
1561 if (POINTER_TYPE_P (type
) && cond_code
!= NE_EXPR
&& cond_code
!= EQ_EXPR
)
1563 set_value_range_to_varying (vr_p
);
1567 /* If LIMIT is another SSA name and LIMIT has a range of its own,
1568 try to use LIMIT's range to avoid creating symbolic ranges
1570 limit_vr
= (TREE_CODE (limit
) == SSA_NAME
) ? get_value_range (limit
) : NULL
;
1572 /* LIMIT's range is only interesting if it has any useful information. */
1574 && (limit_vr
->type
== VR_UNDEFINED
1575 || limit_vr
->type
== VR_VARYING
1576 || symbolic_range_p (limit_vr
)))
1579 /* Initially, the new range has the same set of equivalences of
1580 VAR's range. This will be revised before returning the final
1581 value. Since assertions may be chained via mutually exclusive
1582 predicates, we will need to trim the set of equivalences before
1584 gcc_assert (vr_p
->equiv
== NULL
);
1585 add_equivalence (&vr_p
->equiv
, var
);
1587 /* Extract a new range based on the asserted comparison for VAR and
1588 LIMIT's value range. Notice that if LIMIT has an anti-range, we
1589 will only use it for equality comparisons (EQ_EXPR). For any
1590 other kind of assertion, we cannot derive a range from LIMIT's
1591 anti-range that can be used to describe the new range. For
1592 instance, ASSERT_EXPR <x_2, x_2 <= b_4>. If b_4 is ~[2, 10],
1593 then b_4 takes on the ranges [-INF, 1] and [11, +INF]. There is
1594 no single range for x_2 that could describe LE_EXPR, so we might
1595 as well build the range [b_4, +INF] for it.
1596 One special case we handle is extracting a range from a
1597 range test encoded as (unsigned)var + CST <= limit. */
1598 if (TREE_CODE (cond
) == NOP_EXPR
1599 || TREE_CODE (cond
) == PLUS_EXPR
)
1601 if (TREE_CODE (cond
) == PLUS_EXPR
)
1603 min
= fold_build1 (NEGATE_EXPR
, TREE_TYPE (TREE_OPERAND (cond
, 1)),
1604 TREE_OPERAND (cond
, 1));
1605 max
= int_const_binop (PLUS_EXPR
, limit
, min
);
1606 cond
= TREE_OPERAND (cond
, 0);
1610 min
= build_int_cst (TREE_TYPE (var
), 0);
1614 /* Make sure to not set TREE_OVERFLOW on the final type
1615 conversion. We are willingly interpreting large positive
1616 unsigned values as negative singed values here. */
1617 min
= force_fit_type (TREE_TYPE (var
), wi::to_widest (min
), 0, false);
1618 max
= force_fit_type (TREE_TYPE (var
), wi::to_widest (max
), 0, false);
1620 /* We can transform a max, min range to an anti-range or
1621 vice-versa. Use set_and_canonicalize_value_range which does
1623 if (cond_code
== LE_EXPR
)
1624 set_and_canonicalize_value_range (vr_p
, VR_RANGE
,
1625 min
, max
, vr_p
->equiv
);
1626 else if (cond_code
== GT_EXPR
)
1627 set_and_canonicalize_value_range (vr_p
, VR_ANTI_RANGE
,
1628 min
, max
, vr_p
->equiv
);
1632 else if (cond_code
== EQ_EXPR
)
1634 enum value_range_type range_type
;
1638 range_type
= limit_vr
->type
;
1639 min
= limit_vr
->min
;
1640 max
= limit_vr
->max
;
1644 range_type
= VR_RANGE
;
1649 set_value_range (vr_p
, range_type
, min
, max
, vr_p
->equiv
);
1651 /* When asserting the equality VAR == LIMIT and LIMIT is another
1652 SSA name, the new range will also inherit the equivalence set
1654 if (TREE_CODE (limit
) == SSA_NAME
)
1655 add_equivalence (&vr_p
->equiv
, limit
);
1657 else if (cond_code
== NE_EXPR
)
1659 /* As described above, when LIMIT's range is an anti-range and
1660 this assertion is an inequality (NE_EXPR), then we cannot
1661 derive anything from the anti-range. For instance, if
1662 LIMIT's range was ~[0, 0], the assertion 'VAR != LIMIT' does
1663 not imply that VAR's range is [0, 0]. So, in the case of
1664 anti-ranges, we just assert the inequality using LIMIT and
1667 If LIMIT_VR is a range, we can only use it to build a new
1668 anti-range if LIMIT_VR is a single-valued range. For
1669 instance, if LIMIT_VR is [0, 1], the predicate
1670 VAR != [0, 1] does not mean that VAR's range is ~[0, 1].
1671 Rather, it means that for value 0 VAR should be ~[0, 0]
1672 and for value 1, VAR should be ~[1, 1]. We cannot
1673 represent these ranges.
1675 The only situation in which we can build a valid
1676 anti-range is when LIMIT_VR is a single-valued range
1677 (i.e., LIMIT_VR->MIN == LIMIT_VR->MAX). In that case,
1678 build the anti-range ~[LIMIT_VR->MIN, LIMIT_VR->MAX]. */
1680 && limit_vr
->type
== VR_RANGE
1681 && compare_values (limit_vr
->min
, limit_vr
->max
) == 0)
1683 min
= limit_vr
->min
;
1684 max
= limit_vr
->max
;
1688 /* In any other case, we cannot use LIMIT's range to build a
1689 valid anti-range. */
1693 /* If MIN and MAX cover the whole range for their type, then
1694 just use the original LIMIT. */
1695 if (INTEGRAL_TYPE_P (type
)
1696 && vrp_val_is_min (min
)
1697 && vrp_val_is_max (max
))
1700 set_and_canonicalize_value_range (vr_p
, VR_ANTI_RANGE
,
1701 min
, max
, vr_p
->equiv
);
1703 else if (cond_code
== LE_EXPR
|| cond_code
== LT_EXPR
)
1705 min
= TYPE_MIN_VALUE (type
);
1707 if (limit_vr
== NULL
|| limit_vr
->type
== VR_ANTI_RANGE
)
1711 /* If LIMIT_VR is of the form [N1, N2], we need to build the
1712 range [MIN, N2] for LE_EXPR and [MIN, N2 - 1] for
1714 max
= limit_vr
->max
;
1717 /* If the maximum value forces us to be out of bounds, simply punt.
1718 It would be pointless to try and do anything more since this
1719 all should be optimized away above us. */
1720 if ((cond_code
== LT_EXPR
1721 && compare_values (max
, min
) == 0)
1722 || is_overflow_infinity (max
))
1723 set_value_range_to_varying (vr_p
);
1726 /* For LT_EXPR, we create the range [MIN, MAX - 1]. */
1727 if (cond_code
== LT_EXPR
)
1729 if (TYPE_PRECISION (TREE_TYPE (max
)) == 1
1730 && !TYPE_UNSIGNED (TREE_TYPE (max
)))
1731 max
= fold_build2 (PLUS_EXPR
, TREE_TYPE (max
), max
,
1732 build_int_cst (TREE_TYPE (max
), -1));
1734 max
= fold_build2 (MINUS_EXPR
, TREE_TYPE (max
), max
,
1735 build_int_cst (TREE_TYPE (max
), 1));
1737 TREE_NO_WARNING (max
) = 1;
1740 set_value_range (vr_p
, VR_RANGE
, min
, max
, vr_p
->equiv
);
1743 else if (cond_code
== GE_EXPR
|| cond_code
== GT_EXPR
)
1745 max
= TYPE_MAX_VALUE (type
);
1747 if (limit_vr
== NULL
|| limit_vr
->type
== VR_ANTI_RANGE
)
1751 /* If LIMIT_VR is of the form [N1, N2], we need to build the
1752 range [N1, MAX] for GE_EXPR and [N1 + 1, MAX] for
1754 min
= limit_vr
->min
;
1757 /* If the minimum value forces us to be out of bounds, simply punt.
1758 It would be pointless to try and do anything more since this
1759 all should be optimized away above us. */
1760 if ((cond_code
== GT_EXPR
1761 && compare_values (min
, max
) == 0)
1762 || is_overflow_infinity (min
))
1763 set_value_range_to_varying (vr_p
);
1766 /* For GT_EXPR, we create the range [MIN + 1, MAX]. */
1767 if (cond_code
== GT_EXPR
)
1769 if (TYPE_PRECISION (TREE_TYPE (min
)) == 1
1770 && !TYPE_UNSIGNED (TREE_TYPE (min
)))
1771 min
= fold_build2 (MINUS_EXPR
, TREE_TYPE (min
), min
,
1772 build_int_cst (TREE_TYPE (min
), -1));
1774 min
= fold_build2 (PLUS_EXPR
, TREE_TYPE (min
), min
,
1775 build_int_cst (TREE_TYPE (min
), 1));
1777 TREE_NO_WARNING (min
) = 1;
1780 set_value_range (vr_p
, VR_RANGE
, min
, max
, vr_p
->equiv
);
1786 /* Finally intersect the new range with what we already know about var. */
1787 vrp_intersect_ranges (vr_p
, get_value_range (var
));
1791 /* Extract range information from SSA name VAR and store it in VR. If
1792 VAR has an interesting range, use it. Otherwise, create the
1793 range [VAR, VAR] and return it. This is useful in situations where
1794 we may have conditionals testing values of VARYING names. For
1801 Even if y_5 is deemed VARYING, we can determine that x_3 > y_5 is
1805 extract_range_from_ssa_name (value_range_t
*vr
, tree var
)
1807 value_range_t
*var_vr
= get_value_range (var
);
1809 if (var_vr
->type
!= VR_UNDEFINED
&& var_vr
->type
!= VR_VARYING
)
1810 copy_value_range (vr
, var_vr
);
1812 set_value_range (vr
, VR_RANGE
, var
, var
, NULL
);
1814 add_equivalence (&vr
->equiv
, var
);
1818 /* Wrapper around int_const_binop. If the operation overflows and we
1819 are not using wrapping arithmetic, then adjust the result to be
1820 -INF or +INF depending on CODE, VAL1 and VAL2. This can return
1821 NULL_TREE if we need to use an overflow infinity representation but
1822 the type does not support it. */
1825 vrp_int_const_binop (enum tree_code code
, tree val1
, tree val2
)
1829 res
= int_const_binop (code
, val1
, val2
);
1831 /* If we are using unsigned arithmetic, operate symbolically
1832 on -INF and +INF as int_const_binop only handles signed overflow. */
1833 if (TYPE_UNSIGNED (TREE_TYPE (val1
)))
1835 int checkz
= compare_values (res
, val1
);
1836 bool overflow
= false;
1838 /* Ensure that res = val1 [+*] val2 >= val1
1839 or that res = val1 - val2 <= val1. */
1840 if ((code
== PLUS_EXPR
1841 && !(checkz
== 1 || checkz
== 0))
1842 || (code
== MINUS_EXPR
1843 && !(checkz
== 0 || checkz
== -1)))
1847 /* Checking for multiplication overflow is done by dividing the
1848 output of the multiplication by the first input of the
1849 multiplication. If the result of that division operation is
1850 not equal to the second input of the multiplication, then the
1851 multiplication overflowed. */
1852 else if (code
== MULT_EXPR
&& !integer_zerop (val1
))
1854 tree tmp
= int_const_binop (TRUNC_DIV_EXPR
,
1857 int check
= compare_values (tmp
, val2
);
1865 res
= copy_node (res
);
1866 TREE_OVERFLOW (res
) = 1;
1870 else if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (val1
)))
1871 /* If the singed operation wraps then int_const_binop has done
1872 everything we want. */
1874 /* Signed division of -1/0 overflows and by the time it gets here
1875 returns NULL_TREE. */
1878 else if ((TREE_OVERFLOW (res
)
1879 && !TREE_OVERFLOW (val1
)
1880 && !TREE_OVERFLOW (val2
))
1881 || is_overflow_infinity (val1
)
1882 || is_overflow_infinity (val2
))
1884 /* If the operation overflowed but neither VAL1 nor VAL2 are
1885 overflown, return -INF or +INF depending on the operation
1886 and the combination of signs of the operands. */
1887 int sgn1
= tree_int_cst_sgn (val1
);
1888 int sgn2
= tree_int_cst_sgn (val2
);
1890 if (needs_overflow_infinity (TREE_TYPE (res
))
1891 && !supports_overflow_infinity (TREE_TYPE (res
)))
1894 /* We have to punt on adding infinities of different signs,
1895 since we can't tell what the sign of the result should be.
1896 Likewise for subtracting infinities of the same sign. */
1897 if (((code
== PLUS_EXPR
&& sgn1
!= sgn2
)
1898 || (code
== MINUS_EXPR
&& sgn1
== sgn2
))
1899 && is_overflow_infinity (val1
)
1900 && is_overflow_infinity (val2
))
1903 /* Don't try to handle division or shifting of infinities. */
1904 if ((code
== TRUNC_DIV_EXPR
1905 || code
== FLOOR_DIV_EXPR
1906 || code
== CEIL_DIV_EXPR
1907 || code
== EXACT_DIV_EXPR
1908 || code
== ROUND_DIV_EXPR
1909 || code
== RSHIFT_EXPR
)
1910 && (is_overflow_infinity (val1
)
1911 || is_overflow_infinity (val2
)))
1914 /* Notice that we only need to handle the restricted set of
1915 operations handled by extract_range_from_binary_expr.
1916 Among them, only multiplication, addition and subtraction
1917 can yield overflow without overflown operands because we
1918 are working with integral types only... except in the
1919 case VAL1 = -INF and VAL2 = -1 which overflows to +INF
1920 for division too. */
1922 /* For multiplication, the sign of the overflow is given
1923 by the comparison of the signs of the operands. */
1924 if ((code
== MULT_EXPR
&& sgn1
== sgn2
)
1925 /* For addition, the operands must be of the same sign
1926 to yield an overflow. Its sign is therefore that
1927 of one of the operands, for example the first. For
1928 infinite operands X + -INF is negative, not positive. */
1929 || (code
== PLUS_EXPR
1931 ? !is_negative_overflow_infinity (val2
)
1932 : is_positive_overflow_infinity (val2
)))
1933 /* For subtraction, non-infinite operands must be of
1934 different signs to yield an overflow. Its sign is
1935 therefore that of the first operand or the opposite of
1936 that of the second operand. A first operand of 0 counts
1937 as positive here, for the corner case 0 - (-INF), which
1938 overflows, but must yield +INF. For infinite operands 0
1939 - INF is negative, not positive. */
1940 || (code
== MINUS_EXPR
1942 ? !is_positive_overflow_infinity (val2
)
1943 : is_negative_overflow_infinity (val2
)))
1944 /* We only get in here with positive shift count, so the
1945 overflow direction is the same as the sign of val1.
1946 Actually rshift does not overflow at all, but we only
1947 handle the case of shifting overflowed -INF and +INF. */
1948 || (code
== RSHIFT_EXPR
1950 /* For division, the only case is -INF / -1 = +INF. */
1951 || code
== TRUNC_DIV_EXPR
1952 || code
== FLOOR_DIV_EXPR
1953 || code
== CEIL_DIV_EXPR
1954 || code
== EXACT_DIV_EXPR
1955 || code
== ROUND_DIV_EXPR
)
1956 return (needs_overflow_infinity (TREE_TYPE (res
))
1957 ? positive_overflow_infinity (TREE_TYPE (res
))
1958 : TYPE_MAX_VALUE (TREE_TYPE (res
)));
1960 return (needs_overflow_infinity (TREE_TYPE (res
))
1961 ? negative_overflow_infinity (TREE_TYPE (res
))
1962 : TYPE_MIN_VALUE (TREE_TYPE (res
)));
1969 /* For range VR compute two wide_int bitmasks. In *MAY_BE_NONZERO
1970 bitmask if some bit is unset, it means for all numbers in the range
1971 the bit is 0, otherwise it might be 0 or 1. In *MUST_BE_NONZERO
1972 bitmask if some bit is set, it means for all numbers in the range
1973 the bit is 1, otherwise it might be 0 or 1. */
1976 zero_nonzero_bits_from_vr (const tree expr_type
,
1978 wide_int
*may_be_nonzero
,
1979 wide_int
*must_be_nonzero
)
1981 *may_be_nonzero
= wi::minus_one (TYPE_PRECISION (expr_type
));
1982 *must_be_nonzero
= wi::zero (TYPE_PRECISION (expr_type
));
1983 if (!range_int_cst_p (vr
)
1984 || is_overflow_infinity (vr
->min
)
1985 || is_overflow_infinity (vr
->max
))
1988 if (range_int_cst_singleton_p (vr
))
1990 *may_be_nonzero
= vr
->min
;
1991 *must_be_nonzero
= *may_be_nonzero
;
1993 else if (tree_int_cst_sgn (vr
->min
) >= 0
1994 || tree_int_cst_sgn (vr
->max
) < 0)
1996 wide_int xor_mask
= wi::bit_xor (vr
->min
, vr
->max
);
1997 *may_be_nonzero
= wi::bit_or (vr
->min
, vr
->max
);
1998 *must_be_nonzero
= wi::bit_and (vr
->min
, vr
->max
);
2001 wide_int mask
= wi::mask (wi::floor_log2 (xor_mask
), false,
2002 (*may_be_nonzero
).get_precision ());
2003 *may_be_nonzero
= (*may_be_nonzero
) | mask
;
2004 *must_be_nonzero
= (*must_be_nonzero
).and_not (mask
);
2011 /* Create two value-ranges in *VR0 and *VR1 from the anti-range *AR
2012 so that *VR0 U *VR1 == *AR. Returns true if that is possible,
2013 false otherwise. If *AR can be represented with a single range
2014 *VR1 will be VR_UNDEFINED. */
2017 ranges_from_anti_range (value_range_t
*ar
,
2018 value_range_t
*vr0
, value_range_t
*vr1
)
2020 tree type
= TREE_TYPE (ar
->min
);
2022 vr0
->type
= VR_UNDEFINED
;
2023 vr1
->type
= VR_UNDEFINED
;
2025 if (ar
->type
!= VR_ANTI_RANGE
2026 || TREE_CODE (ar
->min
) != INTEGER_CST
2027 || TREE_CODE (ar
->max
) != INTEGER_CST
2028 || !vrp_val_min (type
)
2029 || !vrp_val_max (type
))
2032 if (!vrp_val_is_min (ar
->min
))
2034 vr0
->type
= VR_RANGE
;
2035 vr0
->min
= vrp_val_min (type
);
2036 vr0
->max
= wide_int_to_tree (type
, wi::sub (ar
->min
, 1));
2038 if (!vrp_val_is_max (ar
->max
))
2040 vr1
->type
= VR_RANGE
;
2041 vr1
->min
= wide_int_to_tree (type
, wi::add (ar
->max
, 1));
2042 vr1
->max
= vrp_val_max (type
);
2044 if (vr0
->type
== VR_UNDEFINED
)
2047 vr1
->type
= VR_UNDEFINED
;
2050 return vr0
->type
!= VR_UNDEFINED
;
2053 /* Helper to extract a value-range *VR for a multiplicative operation
2057 extract_range_from_multiplicative_op_1 (value_range_t
*vr
,
2058 enum tree_code code
,
2059 value_range_t
*vr0
, value_range_t
*vr1
)
2061 enum value_range_type type
;
2068 /* Multiplications, divisions and shifts are a bit tricky to handle,
2069 depending on the mix of signs we have in the two ranges, we
2070 need to operate on different values to get the minimum and
2071 maximum values for the new range. One approach is to figure
2072 out all the variations of range combinations and do the
2075 However, this involves several calls to compare_values and it
2076 is pretty convoluted. It's simpler to do the 4 operations
2077 (MIN0 OP MIN1, MIN0 OP MAX1, MAX0 OP MIN1 and MAX0 OP MAX0 OP
2078 MAX1) and then figure the smallest and largest values to form
2080 gcc_assert (code
== MULT_EXPR
2081 || code
== TRUNC_DIV_EXPR
2082 || code
== FLOOR_DIV_EXPR
2083 || code
== CEIL_DIV_EXPR
2084 || code
== EXACT_DIV_EXPR
2085 || code
== ROUND_DIV_EXPR
2086 || code
== RSHIFT_EXPR
2087 || code
== LSHIFT_EXPR
);
2088 gcc_assert ((vr0
->type
== VR_RANGE
2089 || (code
== MULT_EXPR
&& vr0
->type
== VR_ANTI_RANGE
))
2090 && vr0
->type
== vr1
->type
);
2094 /* Compute the 4 cross operations. */
2096 val
[0] = vrp_int_const_binop (code
, vr0
->min
, vr1
->min
);
2097 if (val
[0] == NULL_TREE
)
2100 if (vr1
->max
== vr1
->min
)
2104 val
[1] = vrp_int_const_binop (code
, vr0
->min
, vr1
->max
);
2105 if (val
[1] == NULL_TREE
)
2109 if (vr0
->max
== vr0
->min
)
2113 val
[2] = vrp_int_const_binop (code
, vr0
->max
, vr1
->min
);
2114 if (val
[2] == NULL_TREE
)
2118 if (vr0
->min
== vr0
->max
|| vr1
->min
== vr1
->max
)
2122 val
[3] = vrp_int_const_binop (code
, vr0
->max
, vr1
->max
);
2123 if (val
[3] == NULL_TREE
)
2129 set_value_range_to_varying (vr
);
2133 /* Set MIN to the minimum of VAL[i] and MAX to the maximum
2137 for (i
= 1; i
< 4; i
++)
2139 if (!is_gimple_min_invariant (min
)
2140 || (TREE_OVERFLOW (min
) && !is_overflow_infinity (min
))
2141 || !is_gimple_min_invariant (max
)
2142 || (TREE_OVERFLOW (max
) && !is_overflow_infinity (max
)))
2147 if (!is_gimple_min_invariant (val
[i
])
2148 || (TREE_OVERFLOW (val
[i
])
2149 && !is_overflow_infinity (val
[i
])))
2151 /* If we found an overflowed value, set MIN and MAX
2152 to it so that we set the resulting range to
2158 if (compare_values (val
[i
], min
) == -1)
2161 if (compare_values (val
[i
], max
) == 1)
2166 /* If either MIN or MAX overflowed, then set the resulting range to
2167 VARYING. But we do accept an overflow infinity
2169 if (min
== NULL_TREE
2170 || !is_gimple_min_invariant (min
)
2171 || (TREE_OVERFLOW (min
) && !is_overflow_infinity (min
))
2173 || !is_gimple_min_invariant (max
)
2174 || (TREE_OVERFLOW (max
) && !is_overflow_infinity (max
)))
2176 set_value_range_to_varying (vr
);
2182 2) [-INF, +-INF(OVF)]
2183 3) [+-INF(OVF), +INF]
2184 4) [+-INF(OVF), +-INF(OVF)]
2185 We learn nothing when we have INF and INF(OVF) on both sides.
2186 Note that we do accept [-INF, -INF] and [+INF, +INF] without
2188 if ((vrp_val_is_min (min
) || is_overflow_infinity (min
))
2189 && (vrp_val_is_max (max
) || is_overflow_infinity (max
)))
2191 set_value_range_to_varying (vr
);
2195 cmp
= compare_values (min
, max
);
2196 if (cmp
== -2 || cmp
== 1)
2198 /* If the new range has its limits swapped around (MIN > MAX),
2199 then the operation caused one of them to wrap around, mark
2200 the new range VARYING. */
2201 set_value_range_to_varying (vr
);
2204 set_value_range (vr
, type
, min
, max
, NULL
);
2207 /* Extract range information from a binary operation CODE based on
2208 the ranges of each of its operands, *VR0 and *VR1 with resulting
2209 type EXPR_TYPE. The resulting range is stored in *VR. */
2212 extract_range_from_binary_expr_1 (value_range_t
*vr
,
2213 enum tree_code code
, tree expr_type
,
2214 value_range_t
*vr0_
, value_range_t
*vr1_
)
2216 value_range_t vr0
= *vr0_
, vr1
= *vr1_
;
2217 value_range_t vrtem0
= VR_INITIALIZER
, vrtem1
= VR_INITIALIZER
;
2218 enum value_range_type type
;
2219 tree min
= NULL_TREE
, max
= NULL_TREE
;
2222 if (!INTEGRAL_TYPE_P (expr_type
)
2223 && !POINTER_TYPE_P (expr_type
))
2225 set_value_range_to_varying (vr
);
2229 /* Not all binary expressions can be applied to ranges in a
2230 meaningful way. Handle only arithmetic operations. */
2231 if (code
!= PLUS_EXPR
2232 && code
!= MINUS_EXPR
2233 && code
!= POINTER_PLUS_EXPR
2234 && code
!= MULT_EXPR
2235 && code
!= TRUNC_DIV_EXPR
2236 && code
!= FLOOR_DIV_EXPR
2237 && code
!= CEIL_DIV_EXPR
2238 && code
!= EXACT_DIV_EXPR
2239 && code
!= ROUND_DIV_EXPR
2240 && code
!= TRUNC_MOD_EXPR
2241 && code
!= RSHIFT_EXPR
2242 && code
!= LSHIFT_EXPR
2245 && code
!= BIT_AND_EXPR
2246 && code
!= BIT_IOR_EXPR
2247 && code
!= BIT_XOR_EXPR
)
2249 set_value_range_to_varying (vr
);
2253 /* If both ranges are UNDEFINED, so is the result. */
2254 if (vr0
.type
== VR_UNDEFINED
&& vr1
.type
== VR_UNDEFINED
)
2256 set_value_range_to_undefined (vr
);
2259 /* If one of the ranges is UNDEFINED drop it to VARYING for the following
2260 code. At some point we may want to special-case operations that
2261 have UNDEFINED result for all or some value-ranges of the not UNDEFINED
2263 else if (vr0
.type
== VR_UNDEFINED
)
2264 set_value_range_to_varying (&vr0
);
2265 else if (vr1
.type
== VR_UNDEFINED
)
2266 set_value_range_to_varying (&vr1
);
2268 /* Now canonicalize anti-ranges to ranges when they are not symbolic
2269 and express ~[] op X as ([]' op X) U ([]'' op X). */
2270 if (vr0
.type
== VR_ANTI_RANGE
2271 && ranges_from_anti_range (&vr0
, &vrtem0
, &vrtem1
))
2273 extract_range_from_binary_expr_1 (vr
, code
, expr_type
, &vrtem0
, vr1_
);
2274 if (vrtem1
.type
!= VR_UNDEFINED
)
2276 value_range_t vrres
= VR_INITIALIZER
;
2277 extract_range_from_binary_expr_1 (&vrres
, code
, expr_type
,
2279 vrp_meet (vr
, &vrres
);
2283 /* Likewise for X op ~[]. */
2284 if (vr1
.type
== VR_ANTI_RANGE
2285 && ranges_from_anti_range (&vr1
, &vrtem0
, &vrtem1
))
2287 extract_range_from_binary_expr_1 (vr
, code
, expr_type
, vr0_
, &vrtem0
);
2288 if (vrtem1
.type
!= VR_UNDEFINED
)
2290 value_range_t vrres
= VR_INITIALIZER
;
2291 extract_range_from_binary_expr_1 (&vrres
, code
, expr_type
,
2293 vrp_meet (vr
, &vrres
);
2298 /* The type of the resulting value range defaults to VR0.TYPE. */
2301 /* Refuse to operate on VARYING ranges, ranges of different kinds
2302 and symbolic ranges. As an exception, we allow BIT_AND_EXPR
2303 because we may be able to derive a useful range even if one of
2304 the operands is VR_VARYING or symbolic range. Similarly for
2305 divisions. TODO, we may be able to derive anti-ranges in
2307 if (code
!= BIT_AND_EXPR
2308 && code
!= BIT_IOR_EXPR
2309 && code
!= TRUNC_DIV_EXPR
2310 && code
!= FLOOR_DIV_EXPR
2311 && code
!= CEIL_DIV_EXPR
2312 && code
!= EXACT_DIV_EXPR
2313 && code
!= ROUND_DIV_EXPR
2314 && code
!= TRUNC_MOD_EXPR
2317 && (vr0
.type
== VR_VARYING
2318 || vr1
.type
== VR_VARYING
2319 || vr0
.type
!= vr1
.type
2320 || symbolic_range_p (&vr0
)
2321 || symbolic_range_p (&vr1
)))
2323 set_value_range_to_varying (vr
);
2327 /* Now evaluate the expression to determine the new range. */
2328 if (POINTER_TYPE_P (expr_type
))
2330 if (code
== MIN_EXPR
|| code
== MAX_EXPR
)
2332 /* For MIN/MAX expressions with pointers, we only care about
2333 nullness, if both are non null, then the result is nonnull.
2334 If both are null, then the result is null. Otherwise they
2336 if (range_is_nonnull (&vr0
) && range_is_nonnull (&vr1
))
2337 set_value_range_to_nonnull (vr
, expr_type
);
2338 else if (range_is_null (&vr0
) && range_is_null (&vr1
))
2339 set_value_range_to_null (vr
, expr_type
);
2341 set_value_range_to_varying (vr
);
2343 else if (code
== POINTER_PLUS_EXPR
)
2345 /* For pointer types, we are really only interested in asserting
2346 whether the expression evaluates to non-NULL. */
2347 if (range_is_nonnull (&vr0
) || range_is_nonnull (&vr1
))
2348 set_value_range_to_nonnull (vr
, expr_type
);
2349 else if (range_is_null (&vr0
) && range_is_null (&vr1
))
2350 set_value_range_to_null (vr
, expr_type
);
2352 set_value_range_to_varying (vr
);
2354 else if (code
== BIT_AND_EXPR
)
2356 /* For pointer types, we are really only interested in asserting
2357 whether the expression evaluates to non-NULL. */
2358 if (range_is_nonnull (&vr0
) && range_is_nonnull (&vr1
))
2359 set_value_range_to_nonnull (vr
, expr_type
);
2360 else if (range_is_null (&vr0
) || range_is_null (&vr1
))
2361 set_value_range_to_null (vr
, expr_type
);
2363 set_value_range_to_varying (vr
);
2366 set_value_range_to_varying (vr
);
2371 /* For integer ranges, apply the operation to each end of the
2372 range and see what we end up with. */
2373 if (code
== PLUS_EXPR
|| code
== MINUS_EXPR
)
2375 /* If we have a PLUS_EXPR with two VR_RANGE integer constant
2376 ranges compute the precise range for such case if possible. */
2377 if (range_int_cst_p (&vr0
)
2378 && range_int_cst_p (&vr1
))
2380 signop sgn
= TYPE_SIGN (expr_type
);
2381 unsigned int prec
= TYPE_PRECISION (expr_type
);
2382 wide_int type_min
= wi::min_value (TYPE_PRECISION (expr_type
), sgn
);
2383 wide_int type_max
= wi::max_value (TYPE_PRECISION (expr_type
), sgn
);
2384 wide_int wmin
, wmax
;
2388 if (code
== PLUS_EXPR
)
2390 wmin
= wi::add (vr0
.min
, vr1
.min
);
2391 wmax
= wi::add (vr0
.max
, vr1
.max
);
2393 /* Check for overflow. */
2394 if (wi::cmp (vr1
.min
, 0, sgn
) != wi::cmp (wmin
, vr0
.min
, sgn
))
2395 min_ovf
= wi::cmp (vr0
.min
, wmin
, sgn
);
2396 if (wi::cmp (vr1
.max
, 0, sgn
) != wi::cmp (wmax
, vr0
.max
, sgn
))
2397 max_ovf
= wi::cmp (vr0
.max
, wmax
, sgn
);
2399 else /* if (code == MINUS_EXPR) */
2401 wmin
= wi::sub (vr0
.min
, vr1
.max
);
2402 wmax
= wi::sub (vr0
.max
, vr1
.min
);
2404 if (wi::cmp (0, vr1
.max
, sgn
) != wi::cmp (wmin
, vr0
.min
, sgn
))
2405 min_ovf
= wi::cmp (vr0
.min
, vr1
.max
, sgn
);
2406 if (wi::cmp (0, vr1
.min
, sgn
) != wi::cmp (wmax
, vr0
.max
, sgn
))
2407 max_ovf
= wi::cmp (vr0
.max
, vr1
.min
, sgn
);
2410 /* For non-wrapping arithmetic look at possibly smaller
2411 value-ranges of the type. */
2412 if (!TYPE_OVERFLOW_WRAPS (expr_type
))
2414 if (vrp_val_min (expr_type
))
2415 type_min
= vrp_val_min (expr_type
);
2416 if (vrp_val_max (expr_type
))
2417 type_max
= vrp_val_max (expr_type
);
2420 /* Check for type overflow. */
2423 if (wi::cmp (wmin
, type_min
, sgn
) == -1)
2425 else if (wi::cmp (wmin
, type_max
, sgn
) == 1)
2430 if (wi::cmp (wmax
, type_min
, sgn
) == -1)
2432 else if (wi::cmp (wmax
, type_max
, sgn
) == 1)
2436 if (TYPE_OVERFLOW_WRAPS (expr_type
))
2438 /* If overflow wraps, truncate the values and adjust the
2439 range kind and bounds appropriately. */
2440 wide_int tmin
= wide_int::from (wmin
, prec
, sgn
);
2441 wide_int tmax
= wide_int::from (wmax
, prec
, sgn
);
2442 if (min_ovf
== max_ovf
)
2444 /* No overflow or both overflow or underflow. The
2445 range kind stays VR_RANGE. */
2446 min
= wide_int_to_tree (expr_type
, tmin
);
2447 max
= wide_int_to_tree (expr_type
, tmax
);
2449 else if (min_ovf
== -1
2452 /* Underflow and overflow, drop to VR_VARYING. */
2453 set_value_range_to_varying (vr
);
2458 /* Min underflow or max overflow. The range kind
2459 changes to VR_ANTI_RANGE. */
2460 bool covers
= false;
2461 wide_int tem
= tmin
;
2462 gcc_assert ((min_ovf
== -1 && max_ovf
== 0)
2463 || (max_ovf
== 1 && min_ovf
== 0));
2464 type
= VR_ANTI_RANGE
;
2466 if (wi::cmp (tmin
, tmax
, sgn
) < 0)
2469 if (wi::cmp (tmax
, tem
, sgn
) > 0)
2471 /* If the anti-range would cover nothing, drop to varying.
2472 Likewise if the anti-range bounds are outside of the
2474 if (covers
|| wi::cmp (tmin
, tmax
, sgn
) > 0)
2476 set_value_range_to_varying (vr
);
2479 min
= wide_int_to_tree (expr_type
, tmin
);
2480 max
= wide_int_to_tree (expr_type
, tmax
);
2485 /* If overflow does not wrap, saturate to the types min/max
2489 if (needs_overflow_infinity (expr_type
)
2490 && supports_overflow_infinity (expr_type
))
2491 min
= negative_overflow_infinity (expr_type
);
2493 min
= wide_int_to_tree (expr_type
, type_min
);
2495 else if (min_ovf
== 1)
2497 if (needs_overflow_infinity (expr_type
)
2498 && supports_overflow_infinity (expr_type
))
2499 min
= positive_overflow_infinity (expr_type
);
2501 min
= wide_int_to_tree (expr_type
, type_max
);
2504 min
= wide_int_to_tree (expr_type
, wmin
);
2508 if (needs_overflow_infinity (expr_type
)
2509 && supports_overflow_infinity (expr_type
))
2510 max
= negative_overflow_infinity (expr_type
);
2512 max
= wide_int_to_tree (expr_type
, type_min
);
2514 else if (max_ovf
== 1)
2516 if (needs_overflow_infinity (expr_type
)
2517 && supports_overflow_infinity (expr_type
))
2518 max
= positive_overflow_infinity (expr_type
);
2520 max
= wide_int_to_tree (expr_type
, type_max
);
2523 max
= wide_int_to_tree (expr_type
, wmax
);
2525 if (needs_overflow_infinity (expr_type
)
2526 && supports_overflow_infinity (expr_type
))
2528 if (is_negative_overflow_infinity (vr0
.min
)
2529 || (code
== PLUS_EXPR
2530 ? is_negative_overflow_infinity (vr1
.min
)
2531 : is_positive_overflow_infinity (vr1
.max
)))
2532 min
= negative_overflow_infinity (expr_type
);
2533 if (is_positive_overflow_infinity (vr0
.max
)
2534 || (code
== PLUS_EXPR
2535 ? is_positive_overflow_infinity (vr1
.max
)
2536 : is_negative_overflow_infinity (vr1
.min
)))
2537 max
= positive_overflow_infinity (expr_type
);
2542 /* For other cases, for example if we have a PLUS_EXPR with two
2543 VR_ANTI_RANGEs, drop to VR_VARYING. It would take more effort
2544 to compute a precise range for such a case.
2545 ??? General even mixed range kind operations can be expressed
2546 by for example transforming ~[3, 5] + [1, 2] to range-only
2547 operations and a union primitive:
2548 [-INF, 2] + [1, 2] U [5, +INF] + [1, 2]
2549 [-INF+1, 4] U [6, +INF(OVF)]
2550 though usually the union is not exactly representable with
2551 a single range or anti-range as the above is
2552 [-INF+1, +INF(OVF)] intersected with ~[5, 5]
2553 but one could use a scheme similar to equivalences for this. */
2554 set_value_range_to_varying (vr
);
2558 else if (code
== MIN_EXPR
2559 || code
== MAX_EXPR
)
2561 if (vr0
.type
== VR_RANGE
2562 && !symbolic_range_p (&vr0
))
2565 if (vr1
.type
== VR_RANGE
2566 && !symbolic_range_p (&vr1
))
2568 /* For operations that make the resulting range directly
2569 proportional to the original ranges, apply the operation to
2570 the same end of each range. */
2571 min
= vrp_int_const_binop (code
, vr0
.min
, vr1
.min
);
2572 max
= vrp_int_const_binop (code
, vr0
.max
, vr1
.max
);
2574 else if (code
== MIN_EXPR
)
2576 min
= vrp_val_min (expr_type
);
2579 else if (code
== MAX_EXPR
)
2582 max
= vrp_val_max (expr_type
);
2585 else if (vr1
.type
== VR_RANGE
2586 && !symbolic_range_p (&vr1
))
2589 if (code
== MIN_EXPR
)
2591 min
= vrp_val_min (expr_type
);
2594 else if (code
== MAX_EXPR
)
2597 max
= vrp_val_max (expr_type
);
2602 set_value_range_to_varying (vr
);
2606 else if (code
== MULT_EXPR
)
2608 /* Fancy code so that with unsigned, [-3,-1]*[-3,-1] does not
2609 drop to varying. This test requires 2*prec bits if both
2610 operands are signed and 2*prec + 2 bits if either is not. */
2612 signop sign
= TYPE_SIGN (expr_type
);
2613 unsigned int prec
= TYPE_PRECISION (expr_type
);
2614 unsigned int prec2
= (prec
* 2) + (sign
== UNSIGNED
? 2 : 0);
2616 if (range_int_cst_p (&vr0
)
2617 && range_int_cst_p (&vr1
)
2618 && TYPE_OVERFLOW_WRAPS (expr_type
))
2620 wide_int sizem1
= wi::mask (prec
, false, prec2
);
2621 wide_int size
= sizem1
+ 1;
2623 /* Extend the values using the sign of the result to PREC2.
2624 From here on out, everthing is just signed math no matter
2625 what the input types were. */
2626 wide_int min0
= wide_int::from (vr0
.min
, prec2
, sign
);
2627 wide_int max0
= wide_int::from (vr0
.max
, prec2
, sign
);
2628 wide_int min1
= wide_int::from (vr1
.min
, prec2
, sign
);
2629 wide_int max1
= wide_int::from (vr1
.max
, prec2
, sign
);
2631 /* Canonicalize the intervals. */
2632 if (sign
== UNSIGNED
)
2634 if (wi::ltu_p (size
, min0
+ max0
))
2640 if (wi::ltu_p (size
, min1
+ max1
))
2647 wide_int prod0
= min0
* min1
;
2648 wide_int prod1
= min0
* max1
;
2649 wide_int prod2
= max0
* min1
;
2650 wide_int prod3
= max0
* max1
;
2652 /* Sort the 4 products so that min is in prod0 and max is in
2654 /* min0min1 > max0max1 */
2655 if (wi::gts_p (prod0
, prod3
))
2657 wide_int tmp
= prod3
;
2662 /* min0max1 > max0min1 */
2663 if (wi::gts_p (prod1
, prod2
))
2665 wide_int tmp
= prod2
;
2670 if (wi::gts_p (prod0
, prod1
))
2672 wide_int tmp
= prod1
;
2677 if (wi::gts_p (prod2
, prod3
))
2679 wide_int tmp
= prod3
;
2684 /* diff = max - min. */
2685 prod2
= prod3
- prod0
;
2686 if (wi::geu_p (prod2
, sizem1
))
2688 /* the range covers all values. */
2689 set_value_range_to_varying (vr
);
2693 /* The following should handle the wrapping and selecting
2694 VR_ANTI_RANGE for us. */
2695 min
= wide_int_to_tree (expr_type
, prod0
);
2696 max
= wide_int_to_tree (expr_type
, prod3
);
2697 set_and_canonicalize_value_range (vr
, VR_RANGE
, min
, max
, NULL
);
2701 /* If we have an unsigned MULT_EXPR with two VR_ANTI_RANGEs,
2702 drop to VR_VARYING. It would take more effort to compute a
2703 precise range for such a case. For example, if we have
2704 op0 == 65536 and op1 == 65536 with their ranges both being
2705 ~[0,0] on a 32-bit machine, we would have op0 * op1 == 0, so
2706 we cannot claim that the product is in ~[0,0]. Note that we
2707 are guaranteed to have vr0.type == vr1.type at this
2709 if (vr0
.type
== VR_ANTI_RANGE
2710 && !TYPE_OVERFLOW_UNDEFINED (expr_type
))
2712 set_value_range_to_varying (vr
);
2716 extract_range_from_multiplicative_op_1 (vr
, code
, &vr0
, &vr1
);
2719 else if (code
== RSHIFT_EXPR
2720 || code
== LSHIFT_EXPR
)
2722 /* If we have a RSHIFT_EXPR with any shift values outside [0..prec-1],
2723 then drop to VR_VARYING. Outside of this range we get undefined
2724 behavior from the shift operation. We cannot even trust
2725 SHIFT_COUNT_TRUNCATED at this stage, because that applies to rtl
2726 shifts, and the operation at the tree level may be widened. */
2727 if (range_int_cst_p (&vr1
)
2728 && compare_tree_int (vr1
.min
, 0) >= 0
2729 && compare_tree_int (vr1
.max
, TYPE_PRECISION (expr_type
)) == -1)
2731 if (code
== RSHIFT_EXPR
)
2733 extract_range_from_multiplicative_op_1 (vr
, code
, &vr0
, &vr1
);
2736 /* We can map lshifts by constants to MULT_EXPR handling. */
2737 else if (code
== LSHIFT_EXPR
2738 && range_int_cst_singleton_p (&vr1
))
2740 bool saved_flag_wrapv
;
2741 value_range_t vr1p
= VR_INITIALIZER
;
2742 vr1p
.type
= VR_RANGE
;
2743 vr1p
.min
= (wide_int_to_tree
2745 wi::set_bit_in_zero (tree_to_shwi (vr1
.min
),
2746 TYPE_PRECISION (expr_type
))));
2747 vr1p
.max
= vr1p
.min
;
2748 /* We have to use a wrapping multiply though as signed overflow
2749 on lshifts is implementation defined in C89. */
2750 saved_flag_wrapv
= flag_wrapv
;
2752 extract_range_from_binary_expr_1 (vr
, MULT_EXPR
, expr_type
,
2754 flag_wrapv
= saved_flag_wrapv
;
2757 else if (code
== LSHIFT_EXPR
2758 && range_int_cst_p (&vr0
))
2760 int prec
= TYPE_PRECISION (expr_type
);
2761 int overflow_pos
= prec
;
2763 wide_int low_bound
, high_bound
;
2764 bool uns
= TYPE_UNSIGNED (expr_type
);
2765 bool in_bounds
= false;
2770 bound_shift
= overflow_pos
- tree_to_shwi (vr1
.max
);
2771 /* If bound_shift == HOST_BITS_PER_WIDE_INT, the llshift can
2772 overflow. However, for that to happen, vr1.max needs to be
2773 zero, which means vr1 is a singleton range of zero, which
2774 means it should be handled by the previous LSHIFT_EXPR
2776 wide_int bound
= wi::set_bit_in_zero (bound_shift
, prec
);
2777 wide_int complement
= ~(bound
- 1);
2782 high_bound
= complement
;
2783 if (wi::ltu_p (vr0
.max
, low_bound
))
2785 /* [5, 6] << [1, 2] == [10, 24]. */
2786 /* We're shifting out only zeroes, the value increases
2790 else if (wi::ltu_p (high_bound
, vr0
.min
))
2792 /* [0xffffff00, 0xffffffff] << [1, 2]
2793 == [0xfffffc00, 0xfffffffe]. */
2794 /* We're shifting out only ones, the value decreases
2801 /* [-1, 1] << [1, 2] == [-4, 4]. */
2802 low_bound
= complement
;
2804 if (wi::lts_p (vr0
.max
, high_bound
)
2805 && wi::lts_p (low_bound
, vr0
.min
))
2807 /* For non-negative numbers, we're shifting out only
2808 zeroes, the value increases monotonically.
2809 For negative numbers, we're shifting out only ones, the
2810 value decreases monotomically. */
2817 extract_range_from_multiplicative_op_1 (vr
, code
, &vr0
, &vr1
);
2822 set_value_range_to_varying (vr
);
2825 else if (code
== TRUNC_DIV_EXPR
2826 || code
== FLOOR_DIV_EXPR
2827 || code
== CEIL_DIV_EXPR
2828 || code
== EXACT_DIV_EXPR
2829 || code
== ROUND_DIV_EXPR
)
2831 if (vr0
.type
!= VR_RANGE
|| symbolic_range_p (&vr0
))
2833 /* For division, if op1 has VR_RANGE but op0 does not, something
2834 can be deduced just from that range. Say [min, max] / [4, max]
2835 gives [min / 4, max / 4] range. */
2836 if (vr1
.type
== VR_RANGE
2837 && !symbolic_range_p (&vr1
)
2838 && range_includes_zero_p (vr1
.min
, vr1
.max
) == 0)
2840 vr0
.type
= type
= VR_RANGE
;
2841 vr0
.min
= vrp_val_min (expr_type
);
2842 vr0
.max
= vrp_val_max (expr_type
);
2846 set_value_range_to_varying (vr
);
2851 /* For divisions, if flag_non_call_exceptions is true, we must
2852 not eliminate a division by zero. */
2853 if (cfun
->can_throw_non_call_exceptions
2854 && (vr1
.type
!= VR_RANGE
2855 || range_includes_zero_p (vr1
.min
, vr1
.max
) != 0))
2857 set_value_range_to_varying (vr
);
2861 /* For divisions, if op0 is VR_RANGE, we can deduce a range
2862 even if op1 is VR_VARYING, VR_ANTI_RANGE, symbolic or can
2864 if (vr0
.type
== VR_RANGE
2865 && (vr1
.type
!= VR_RANGE
2866 || range_includes_zero_p (vr1
.min
, vr1
.max
) != 0))
2868 tree zero
= build_int_cst (TREE_TYPE (vr0
.min
), 0);
2873 if (TYPE_UNSIGNED (expr_type
)
2874 || value_range_nonnegative_p (&vr1
))
2876 /* For unsigned division or when divisor is known
2877 to be non-negative, the range has to cover
2878 all numbers from 0 to max for positive max
2879 and all numbers from min to 0 for negative min. */
2880 cmp
= compare_values (vr0
.max
, zero
);
2883 else if (cmp
== 0 || cmp
== 1)
2887 cmp
= compare_values (vr0
.min
, zero
);
2890 else if (cmp
== 0 || cmp
== -1)
2897 /* Otherwise the range is -max .. max or min .. -min
2898 depending on which bound is bigger in absolute value,
2899 as the division can change the sign. */
2900 abs_extent_range (vr
, vr0
.min
, vr0
.max
);
2903 if (type
== VR_VARYING
)
2905 set_value_range_to_varying (vr
);
2911 extract_range_from_multiplicative_op_1 (vr
, code
, &vr0
, &vr1
);
2915 else if (code
== TRUNC_MOD_EXPR
)
2917 if (vr1
.type
!= VR_RANGE
2918 || range_includes_zero_p (vr1
.min
, vr1
.max
) != 0
2919 || vrp_val_is_min (vr1
.min
))
2921 set_value_range_to_varying (vr
);
2925 /* Compute MAX <|vr1.min|, |vr1.max|> - 1. */
2926 max
= fold_unary_to_constant (ABS_EXPR
, expr_type
, vr1
.min
);
2927 if (tree_int_cst_lt (max
, vr1
.max
))
2929 max
= int_const_binop (MINUS_EXPR
, max
, build_int_cst (TREE_TYPE (max
), 1));
2930 /* If the dividend is non-negative the modulus will be
2931 non-negative as well. */
2932 if (TYPE_UNSIGNED (expr_type
)
2933 || value_range_nonnegative_p (&vr0
))
2934 min
= build_int_cst (TREE_TYPE (max
), 0);
2936 min
= fold_unary_to_constant (NEGATE_EXPR
, expr_type
, max
);
2938 else if (code
== BIT_AND_EXPR
|| code
== BIT_IOR_EXPR
|| code
== BIT_XOR_EXPR
)
2940 bool int_cst_range0
, int_cst_range1
;
2941 wide_int may_be_nonzero0
, may_be_nonzero1
;
2942 wide_int must_be_nonzero0
, must_be_nonzero1
;
2944 int_cst_range0
= zero_nonzero_bits_from_vr (expr_type
, &vr0
,
2947 int_cst_range1
= zero_nonzero_bits_from_vr (expr_type
, &vr1
,
2952 if (code
== BIT_AND_EXPR
)
2954 min
= wide_int_to_tree (expr_type
,
2955 must_be_nonzero0
& must_be_nonzero1
);
2956 wide_int wmax
= may_be_nonzero0
& may_be_nonzero1
;
2957 /* If both input ranges contain only negative values we can
2958 truncate the result range maximum to the minimum of the
2959 input range maxima. */
2960 if (int_cst_range0
&& int_cst_range1
2961 && tree_int_cst_sgn (vr0
.max
) < 0
2962 && tree_int_cst_sgn (vr1
.max
) < 0)
2964 wmax
= wi::min (wmax
, vr0
.max
, TYPE_SIGN (expr_type
));
2965 wmax
= wi::min (wmax
, vr1
.max
, TYPE_SIGN (expr_type
));
2967 /* If either input range contains only non-negative values
2968 we can truncate the result range maximum to the respective
2969 maximum of the input range. */
2970 if (int_cst_range0
&& tree_int_cst_sgn (vr0
.min
) >= 0)
2971 wmax
= wi::min (wmax
, vr0
.max
, TYPE_SIGN (expr_type
));
2972 if (int_cst_range1
&& tree_int_cst_sgn (vr1
.min
) >= 0)
2973 wmax
= wi::min (wmax
, vr1
.max
, TYPE_SIGN (expr_type
));
2974 max
= wide_int_to_tree (expr_type
, wmax
);
2976 else if (code
== BIT_IOR_EXPR
)
2978 max
= wide_int_to_tree (expr_type
,
2979 may_be_nonzero0
| may_be_nonzero1
);
2980 wide_int wmin
= must_be_nonzero0
| must_be_nonzero1
;
2981 /* If the input ranges contain only positive values we can
2982 truncate the minimum of the result range to the maximum
2983 of the input range minima. */
2984 if (int_cst_range0
&& int_cst_range1
2985 && tree_int_cst_sgn (vr0
.min
) >= 0
2986 && tree_int_cst_sgn (vr1
.min
) >= 0)
2988 wmin
= wi::max (wmin
, vr0
.min
, TYPE_SIGN (expr_type
));
2989 wmin
= wi::max (wmin
, vr1
.min
, TYPE_SIGN (expr_type
));
2991 /* If either input range contains only negative values
2992 we can truncate the minimum of the result range to the
2993 respective minimum range. */
2994 if (int_cst_range0
&& tree_int_cst_sgn (vr0
.max
) < 0)
2995 wmin
= wi::max (wmin
, vr0
.min
, TYPE_SIGN (expr_type
));
2996 if (int_cst_range1
&& tree_int_cst_sgn (vr1
.max
) < 0)
2997 wmin
= wi::max (wmin
, vr1
.min
, TYPE_SIGN (expr_type
));
2998 min
= wide_int_to_tree (expr_type
, wmin
);
3000 else if (code
== BIT_XOR_EXPR
)
3002 wide_int result_zero_bits
= ((must_be_nonzero0
& must_be_nonzero1
)
3003 | ~(may_be_nonzero0
| may_be_nonzero1
));
3004 wide_int result_one_bits
3005 = (must_be_nonzero0
.and_not (may_be_nonzero1
)
3006 | must_be_nonzero1
.and_not (may_be_nonzero0
));
3007 max
= wide_int_to_tree (expr_type
, ~result_zero_bits
);
3008 min
= wide_int_to_tree (expr_type
, result_one_bits
);
3009 /* If the range has all positive or all negative values the
3010 result is better than VARYING. */
3011 if (tree_int_cst_sgn (min
) < 0
3012 || tree_int_cst_sgn (max
) >= 0)
3015 max
= min
= NULL_TREE
;
3021 /* If either MIN or MAX overflowed, then set the resulting range to
3022 VARYING. But we do accept an overflow infinity
3024 if (min
== NULL_TREE
3025 || !is_gimple_min_invariant (min
)
3026 || (TREE_OVERFLOW (min
) && !is_overflow_infinity (min
))
3028 || !is_gimple_min_invariant (max
)
3029 || (TREE_OVERFLOW (max
) && !is_overflow_infinity (max
)))
3031 set_value_range_to_varying (vr
);
3037 2) [-INF, +-INF(OVF)]
3038 3) [+-INF(OVF), +INF]
3039 4) [+-INF(OVF), +-INF(OVF)]
3040 We learn nothing when we have INF and INF(OVF) on both sides.
3041 Note that we do accept [-INF, -INF] and [+INF, +INF] without
3043 if ((vrp_val_is_min (min
) || is_overflow_infinity (min
))
3044 && (vrp_val_is_max (max
) || is_overflow_infinity (max
)))
3046 set_value_range_to_varying (vr
);
3050 cmp
= compare_values (min
, max
);
3051 if (cmp
== -2 || cmp
== 1)
3053 /* If the new range has its limits swapped around (MIN > MAX),
3054 then the operation caused one of them to wrap around, mark
3055 the new range VARYING. */
3056 set_value_range_to_varying (vr
);
3059 set_value_range (vr
, type
, min
, max
, NULL
);
3062 /* Extract range information from a binary expression OP0 CODE OP1 based on
3063 the ranges of each of its operands with resulting type EXPR_TYPE.
3064 The resulting range is stored in *VR. */
3067 extract_range_from_binary_expr (value_range_t
*vr
,
3068 enum tree_code code
,
3069 tree expr_type
, tree op0
, tree op1
)
3071 value_range_t vr0
= VR_INITIALIZER
;
3072 value_range_t vr1
= VR_INITIALIZER
;
3074 /* Get value ranges for each operand. For constant operands, create
3075 a new value range with the operand to simplify processing. */
3076 if (TREE_CODE (op0
) == SSA_NAME
)
3077 vr0
= *(get_value_range (op0
));
3078 else if (is_gimple_min_invariant (op0
))
3079 set_value_range_to_value (&vr0
, op0
, NULL
);
3081 set_value_range_to_varying (&vr0
);
3083 if (TREE_CODE (op1
) == SSA_NAME
)
3084 vr1
= *(get_value_range (op1
));
3085 else if (is_gimple_min_invariant (op1
))
3086 set_value_range_to_value (&vr1
, op1
, NULL
);
3088 set_value_range_to_varying (&vr1
);
3090 extract_range_from_binary_expr_1 (vr
, code
, expr_type
, &vr0
, &vr1
);
3093 /* Extract range information from a unary operation CODE based on
3094 the range of its operand *VR0 with type OP0_TYPE with resulting type TYPE.
3095 The The resulting range is stored in *VR. */
3098 extract_range_from_unary_expr_1 (value_range_t
*vr
,
3099 enum tree_code code
, tree type
,
3100 value_range_t
*vr0_
, tree op0_type
)
3102 value_range_t vr0
= *vr0_
, vrtem0
= VR_INITIALIZER
, vrtem1
= VR_INITIALIZER
;
3104 /* VRP only operates on integral and pointer types. */
3105 if (!(INTEGRAL_TYPE_P (op0_type
)
3106 || POINTER_TYPE_P (op0_type
))
3107 || !(INTEGRAL_TYPE_P (type
)
3108 || POINTER_TYPE_P (type
)))
3110 set_value_range_to_varying (vr
);
3114 /* If VR0 is UNDEFINED, so is the result. */
3115 if (vr0
.type
== VR_UNDEFINED
)
3117 set_value_range_to_undefined (vr
);
3121 /* Handle operations that we express in terms of others. */
3122 if (code
== PAREN_EXPR
)
3124 /* PAREN_EXPR is a simple copy. */
3125 copy_value_range (vr
, &vr0
);
3128 else if (code
== NEGATE_EXPR
)
3130 /* -X is simply 0 - X, so re-use existing code that also handles
3131 anti-ranges fine. */
3132 value_range_t zero
= VR_INITIALIZER
;
3133 set_value_range_to_value (&zero
, build_int_cst (type
, 0), NULL
);
3134 extract_range_from_binary_expr_1 (vr
, MINUS_EXPR
, type
, &zero
, &vr0
);
3137 else if (code
== BIT_NOT_EXPR
)
3139 /* ~X is simply -1 - X, so re-use existing code that also handles
3140 anti-ranges fine. */
3141 value_range_t minusone
= VR_INITIALIZER
;
3142 set_value_range_to_value (&minusone
, build_int_cst (type
, -1), NULL
);
3143 extract_range_from_binary_expr_1 (vr
, MINUS_EXPR
,
3144 type
, &minusone
, &vr0
);
3148 /* Now canonicalize anti-ranges to ranges when they are not symbolic
3149 and express op ~[] as (op []') U (op []''). */
3150 if (vr0
.type
== VR_ANTI_RANGE
3151 && ranges_from_anti_range (&vr0
, &vrtem0
, &vrtem1
))
3153 extract_range_from_unary_expr_1 (vr
, code
, type
, &vrtem0
, op0_type
);
3154 if (vrtem1
.type
!= VR_UNDEFINED
)
3156 value_range_t vrres
= VR_INITIALIZER
;
3157 extract_range_from_unary_expr_1 (&vrres
, code
, type
,
3159 vrp_meet (vr
, &vrres
);
3164 if (CONVERT_EXPR_CODE_P (code
))
3166 tree inner_type
= op0_type
;
3167 tree outer_type
= type
;
3169 /* If the expression evaluates to a pointer, we are only interested in
3170 determining if it evaluates to NULL [0, 0] or non-NULL (~[0, 0]). */
3171 if (POINTER_TYPE_P (type
))
3173 if (range_is_nonnull (&vr0
))
3174 set_value_range_to_nonnull (vr
, type
);
3175 else if (range_is_null (&vr0
))
3176 set_value_range_to_null (vr
, type
);
3178 set_value_range_to_varying (vr
);
3182 /* If VR0 is varying and we increase the type precision, assume
3183 a full range for the following transformation. */
3184 if (vr0
.type
== VR_VARYING
3185 && INTEGRAL_TYPE_P (inner_type
)
3186 && TYPE_PRECISION (inner_type
) < TYPE_PRECISION (outer_type
))
3188 vr0
.type
= VR_RANGE
;
3189 vr0
.min
= TYPE_MIN_VALUE (inner_type
);
3190 vr0
.max
= TYPE_MAX_VALUE (inner_type
);
3193 /* If VR0 is a constant range or anti-range and the conversion is
3194 not truncating we can convert the min and max values and
3195 canonicalize the resulting range. Otherwise we can do the
3196 conversion if the size of the range is less than what the
3197 precision of the target type can represent and the range is
3198 not an anti-range. */
3199 if ((vr0
.type
== VR_RANGE
3200 || vr0
.type
== VR_ANTI_RANGE
)
3201 && TREE_CODE (vr0
.min
) == INTEGER_CST
3202 && TREE_CODE (vr0
.max
) == INTEGER_CST
3203 && (!is_overflow_infinity (vr0
.min
)
3204 || (vr0
.type
== VR_RANGE
3205 && TYPE_PRECISION (outer_type
) > TYPE_PRECISION (inner_type
)
3206 && needs_overflow_infinity (outer_type
)
3207 && supports_overflow_infinity (outer_type
)))
3208 && (!is_overflow_infinity (vr0
.max
)
3209 || (vr0
.type
== VR_RANGE
3210 && TYPE_PRECISION (outer_type
) > TYPE_PRECISION (inner_type
)
3211 && needs_overflow_infinity (outer_type
)
3212 && supports_overflow_infinity (outer_type
)))
3213 && (TYPE_PRECISION (outer_type
) >= TYPE_PRECISION (inner_type
)
3214 || (vr0
.type
== VR_RANGE
3215 && integer_zerop (int_const_binop (RSHIFT_EXPR
,
3216 int_const_binop (MINUS_EXPR
, vr0
.max
, vr0
.min
),
3217 size_int (TYPE_PRECISION (outer_type
)))))))
3219 tree new_min
, new_max
;
3220 if (is_overflow_infinity (vr0
.min
))
3221 new_min
= negative_overflow_infinity (outer_type
);
3223 new_min
= force_fit_type (outer_type
, wi::to_widest (vr0
.min
),
3225 if (is_overflow_infinity (vr0
.max
))
3226 new_max
= positive_overflow_infinity (outer_type
);
3228 new_max
= force_fit_type (outer_type
, wi::to_widest (vr0
.max
),
3230 set_and_canonicalize_value_range (vr
, vr0
.type
,
3231 new_min
, new_max
, NULL
);
3235 set_value_range_to_varying (vr
);
3238 else if (code
== ABS_EXPR
)
3243 /* Pass through vr0 in the easy cases. */
3244 if (TYPE_UNSIGNED (type
)
3245 || value_range_nonnegative_p (&vr0
))
3247 copy_value_range (vr
, &vr0
);
3251 /* For the remaining varying or symbolic ranges we can't do anything
3253 if (vr0
.type
== VR_VARYING
3254 || symbolic_range_p (&vr0
))
3256 set_value_range_to_varying (vr
);
3260 /* -TYPE_MIN_VALUE = TYPE_MIN_VALUE with flag_wrapv so we can't get a
3262 if (!TYPE_OVERFLOW_UNDEFINED (type
)
3263 && ((vr0
.type
== VR_RANGE
3264 && vrp_val_is_min (vr0
.min
))
3265 || (vr0
.type
== VR_ANTI_RANGE
3266 && !vrp_val_is_min (vr0
.min
))))
3268 set_value_range_to_varying (vr
);
3272 /* ABS_EXPR may flip the range around, if the original range
3273 included negative values. */
3274 if (is_overflow_infinity (vr0
.min
))
3275 min
= positive_overflow_infinity (type
);
3276 else if (!vrp_val_is_min (vr0
.min
))
3277 min
= fold_unary_to_constant (code
, type
, vr0
.min
);
3278 else if (!needs_overflow_infinity (type
))
3279 min
= TYPE_MAX_VALUE (type
);
3280 else if (supports_overflow_infinity (type
))
3281 min
= positive_overflow_infinity (type
);
3284 set_value_range_to_varying (vr
);
3288 if (is_overflow_infinity (vr0
.max
))
3289 max
= positive_overflow_infinity (type
);
3290 else if (!vrp_val_is_min (vr0
.max
))
3291 max
= fold_unary_to_constant (code
, type
, vr0
.max
);
3292 else if (!needs_overflow_infinity (type
))
3293 max
= TYPE_MAX_VALUE (type
);
3294 else if (supports_overflow_infinity (type
)
3295 /* We shouldn't generate [+INF, +INF] as set_value_range
3296 doesn't like this and ICEs. */
3297 && !is_positive_overflow_infinity (min
))
3298 max
= positive_overflow_infinity (type
);
3301 set_value_range_to_varying (vr
);
3305 cmp
= compare_values (min
, max
);
3307 /* If a VR_ANTI_RANGEs contains zero, then we have
3308 ~[-INF, min(MIN, MAX)]. */
3309 if (vr0
.type
== VR_ANTI_RANGE
)
3311 if (range_includes_zero_p (vr0
.min
, vr0
.max
) == 1)
3313 /* Take the lower of the two values. */
3317 /* Create ~[-INF, min (abs(MIN), abs(MAX))]
3318 or ~[-INF + 1, min (abs(MIN), abs(MAX))] when
3319 flag_wrapv is set and the original anti-range doesn't include
3320 TYPE_MIN_VALUE, remember -TYPE_MIN_VALUE = TYPE_MIN_VALUE. */
3321 if (TYPE_OVERFLOW_WRAPS (type
))
3323 tree type_min_value
= TYPE_MIN_VALUE (type
);
3325 min
= (vr0
.min
!= type_min_value
3326 ? int_const_binop (PLUS_EXPR
, type_min_value
,
3327 build_int_cst (TREE_TYPE (type_min_value
), 1))
3332 if (overflow_infinity_range_p (&vr0
))
3333 min
= negative_overflow_infinity (type
);
3335 min
= TYPE_MIN_VALUE (type
);
3340 /* All else has failed, so create the range [0, INF], even for
3341 flag_wrapv since TYPE_MIN_VALUE is in the original
3343 vr0
.type
= VR_RANGE
;
3344 min
= build_int_cst (type
, 0);
3345 if (needs_overflow_infinity (type
))
3347 if (supports_overflow_infinity (type
))
3348 max
= positive_overflow_infinity (type
);
3351 set_value_range_to_varying (vr
);
3356 max
= TYPE_MAX_VALUE (type
);
3360 /* If the range contains zero then we know that the minimum value in the
3361 range will be zero. */
3362 else if (range_includes_zero_p (vr0
.min
, vr0
.max
) == 1)
3366 min
= build_int_cst (type
, 0);
3370 /* If the range was reversed, swap MIN and MAX. */
3379 cmp
= compare_values (min
, max
);
3380 if (cmp
== -2 || cmp
== 1)
3382 /* If the new range has its limits swapped around (MIN > MAX),
3383 then the operation caused one of them to wrap around, mark
3384 the new range VARYING. */
3385 set_value_range_to_varying (vr
);
3388 set_value_range (vr
, vr0
.type
, min
, max
, NULL
);
3392 /* For unhandled operations fall back to varying. */
3393 set_value_range_to_varying (vr
);
3398 /* Extract range information from a unary expression CODE OP0 based on
3399 the range of its operand with resulting type TYPE.
3400 The resulting range is stored in *VR. */
3403 extract_range_from_unary_expr (value_range_t
*vr
, enum tree_code code
,
3404 tree type
, tree op0
)
3406 value_range_t vr0
= VR_INITIALIZER
;
3408 /* Get value ranges for the operand. For constant operands, create
3409 a new value range with the operand to simplify processing. */
3410 if (TREE_CODE (op0
) == SSA_NAME
)
3411 vr0
= *(get_value_range (op0
));
3412 else if (is_gimple_min_invariant (op0
))
3413 set_value_range_to_value (&vr0
, op0
, NULL
);
3415 set_value_range_to_varying (&vr0
);
3417 extract_range_from_unary_expr_1 (vr
, code
, type
, &vr0
, TREE_TYPE (op0
));
3421 /* Extract range information from a conditional expression STMT based on
3422 the ranges of each of its operands and the expression code. */
3425 extract_range_from_cond_expr (value_range_t
*vr
, gimple stmt
)
3428 value_range_t vr0
= VR_INITIALIZER
;
3429 value_range_t vr1
= VR_INITIALIZER
;
3431 /* Get value ranges for each operand. For constant operands, create
3432 a new value range with the operand to simplify processing. */
3433 op0
= gimple_assign_rhs2 (stmt
);
3434 if (TREE_CODE (op0
) == SSA_NAME
)
3435 vr0
= *(get_value_range (op0
));
3436 else if (is_gimple_min_invariant (op0
))
3437 set_value_range_to_value (&vr0
, op0
, NULL
);
3439 set_value_range_to_varying (&vr0
);
3441 op1
= gimple_assign_rhs3 (stmt
);
3442 if (TREE_CODE (op1
) == SSA_NAME
)
3443 vr1
= *(get_value_range (op1
));
3444 else if (is_gimple_min_invariant (op1
))
3445 set_value_range_to_value (&vr1
, op1
, NULL
);
3447 set_value_range_to_varying (&vr1
);
3449 /* The resulting value range is the union of the operand ranges */
3450 copy_value_range (vr
, &vr0
);
3451 vrp_meet (vr
, &vr1
);
3455 /* Extract range information from a comparison expression EXPR based
3456 on the range of its operand and the expression code. */
3459 extract_range_from_comparison (value_range_t
*vr
, enum tree_code code
,
3460 tree type
, tree op0
, tree op1
)
3465 val
= vrp_evaluate_conditional_warnv_with_ops (code
, op0
, op1
, false, &sop
,
3468 /* A disadvantage of using a special infinity as an overflow
3469 representation is that we lose the ability to record overflow
3470 when we don't have an infinity. So we have to ignore a result
3471 which relies on overflow. */
3473 if (val
&& !is_overflow_infinity (val
) && !sop
)
3475 /* Since this expression was found on the RHS of an assignment,
3476 its type may be different from _Bool. Convert VAL to EXPR's
3478 val
= fold_convert (type
, val
);
3479 if (is_gimple_min_invariant (val
))
3480 set_value_range_to_value (vr
, val
, vr
->equiv
);
3482 set_value_range (vr
, VR_RANGE
, val
, val
, vr
->equiv
);
3485 /* The result of a comparison is always true or false. */
3486 set_value_range_to_truthvalue (vr
, type
);
3489 /* Try to derive a nonnegative or nonzero range out of STMT relying
3490 primarily on generic routines in fold in conjunction with range data.
3491 Store the result in *VR */
3494 extract_range_basic (value_range_t
*vr
, gimple stmt
)
3497 tree type
= gimple_expr_type (stmt
);
3499 if (gimple_call_builtin_p (stmt
, BUILT_IN_NORMAL
))
3501 tree fndecl
= gimple_call_fndecl (stmt
), arg
;
3502 int mini
, maxi
, zerov
= 0, prec
;
3504 switch (DECL_FUNCTION_CODE (fndecl
))
3506 case BUILT_IN_CONSTANT_P
:
3507 /* If the call is __builtin_constant_p and the argument is a
3508 function parameter resolve it to false. This avoids bogus
3509 array bound warnings.
3510 ??? We could do this as early as inlining is finished. */
3511 arg
= gimple_call_arg (stmt
, 0);
3512 if (TREE_CODE (arg
) == SSA_NAME
3513 && SSA_NAME_IS_DEFAULT_DEF (arg
)
3514 && TREE_CODE (SSA_NAME_VAR (arg
)) == PARM_DECL
)
3516 set_value_range_to_null (vr
, type
);
3520 /* Both __builtin_ffs* and __builtin_popcount return
3522 CASE_INT_FN (BUILT_IN_FFS
):
3523 CASE_INT_FN (BUILT_IN_POPCOUNT
):
3524 arg
= gimple_call_arg (stmt
, 0);
3525 prec
= TYPE_PRECISION (TREE_TYPE (arg
));
3528 if (TREE_CODE (arg
) == SSA_NAME
)
3530 value_range_t
*vr0
= get_value_range (arg
);
3531 /* If arg is non-zero, then ffs or popcount
3533 if (((vr0
->type
== VR_RANGE
3534 && integer_nonzerop (vr0
->min
))
3535 || (vr0
->type
== VR_ANTI_RANGE
3536 && integer_zerop (vr0
->min
)))
3537 && !is_overflow_infinity (vr0
->min
))
3539 /* If some high bits are known to be zero,
3540 we can decrease the maximum. */
3541 if (vr0
->type
== VR_RANGE
3542 && TREE_CODE (vr0
->max
) == INTEGER_CST
3543 && !is_overflow_infinity (vr0
->max
))
3544 maxi
= tree_floor_log2 (vr0
->max
) + 1;
3547 /* __builtin_parity* returns [0, 1]. */
3548 CASE_INT_FN (BUILT_IN_PARITY
):
3552 /* __builtin_c[lt]z* return [0, prec-1], except for
3553 when the argument is 0, but that is undefined behavior.
3554 On many targets where the CLZ RTL or optab value is defined
3555 for 0 the value is prec, so include that in the range
3557 CASE_INT_FN (BUILT_IN_CLZ
):
3558 arg
= gimple_call_arg (stmt
, 0);
3559 prec
= TYPE_PRECISION (TREE_TYPE (arg
));
3562 if (optab_handler (clz_optab
, TYPE_MODE (TREE_TYPE (arg
)))
3564 && CLZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (TREE_TYPE (arg
)),
3566 /* Handle only the single common value. */
3568 /* Magic value to give up, unless vr0 proves
3571 if (TREE_CODE (arg
) == SSA_NAME
)
3573 value_range_t
*vr0
= get_value_range (arg
);
3574 /* From clz of VR_RANGE minimum we can compute
3576 if (vr0
->type
== VR_RANGE
3577 && TREE_CODE (vr0
->min
) == INTEGER_CST
3578 && !is_overflow_infinity (vr0
->min
))
3580 maxi
= prec
- 1 - tree_floor_log2 (vr0
->min
);
3584 else if (vr0
->type
== VR_ANTI_RANGE
3585 && integer_zerop (vr0
->min
)
3586 && !is_overflow_infinity (vr0
->min
))
3593 /* From clz of VR_RANGE maximum we can compute
3595 if (vr0
->type
== VR_RANGE
3596 && TREE_CODE (vr0
->max
) == INTEGER_CST
3597 && !is_overflow_infinity (vr0
->max
))
3599 mini
= prec
- 1 - tree_floor_log2 (vr0
->max
);
3607 /* __builtin_ctz* return [0, prec-1], except for
3608 when the argument is 0, but that is undefined behavior.
3609 If there is a ctz optab for this mode and
3610 CTZ_DEFINED_VALUE_AT_ZERO, include that in the range,
3611 otherwise just assume 0 won't be seen. */
3612 CASE_INT_FN (BUILT_IN_CTZ
):
3613 arg
= gimple_call_arg (stmt
, 0);
3614 prec
= TYPE_PRECISION (TREE_TYPE (arg
));
3617 if (optab_handler (ctz_optab
, TYPE_MODE (TREE_TYPE (arg
)))
3619 && CTZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (TREE_TYPE (arg
)),
3622 /* Handle only the two common values. */
3625 else if (zerov
== prec
)
3628 /* Magic value to give up, unless vr0 proves
3632 if (TREE_CODE (arg
) == SSA_NAME
)
3634 value_range_t
*vr0
= get_value_range (arg
);
3635 /* If arg is non-zero, then use [0, prec - 1]. */
3636 if (((vr0
->type
== VR_RANGE
3637 && integer_nonzerop (vr0
->min
))
3638 || (vr0
->type
== VR_ANTI_RANGE
3639 && integer_zerop (vr0
->min
)))
3640 && !is_overflow_infinity (vr0
->min
))
3645 /* If some high bits are known to be zero,
3646 we can decrease the result maximum. */
3647 if (vr0
->type
== VR_RANGE
3648 && TREE_CODE (vr0
->max
) == INTEGER_CST
3649 && !is_overflow_infinity (vr0
->max
))
3651 maxi
= tree_floor_log2 (vr0
->max
);
3652 /* For vr0 [0, 0] give up. */
3660 /* __builtin_clrsb* returns [0, prec-1]. */
3661 CASE_INT_FN (BUILT_IN_CLRSB
):
3662 arg
= gimple_call_arg (stmt
, 0);
3663 prec
= TYPE_PRECISION (TREE_TYPE (arg
));
3668 set_value_range (vr
, VR_RANGE
, build_int_cst (type
, mini
),
3669 build_int_cst (type
, maxi
), NULL
);
3675 if (INTEGRAL_TYPE_P (type
)
3676 && gimple_stmt_nonnegative_warnv_p (stmt
, &sop
))
3677 set_value_range_to_nonnegative (vr
, type
,
3678 sop
|| stmt_overflow_infinity (stmt
));
3679 else if (vrp_stmt_computes_nonzero (stmt
, &sop
)
3681 set_value_range_to_nonnull (vr
, type
);
3683 set_value_range_to_varying (vr
);
3687 /* Try to compute a useful range out of assignment STMT and store it
3691 extract_range_from_assignment (value_range_t
*vr
, gimple stmt
)
3693 enum tree_code code
= gimple_assign_rhs_code (stmt
);
3695 if (code
== ASSERT_EXPR
)
3696 extract_range_from_assert (vr
, gimple_assign_rhs1 (stmt
));
3697 else if (code
== SSA_NAME
)
3698 extract_range_from_ssa_name (vr
, gimple_assign_rhs1 (stmt
));
3699 else if (TREE_CODE_CLASS (code
) == tcc_binary
)
3700 extract_range_from_binary_expr (vr
, gimple_assign_rhs_code (stmt
),
3701 gimple_expr_type (stmt
),
3702 gimple_assign_rhs1 (stmt
),
3703 gimple_assign_rhs2 (stmt
));
3704 else if (TREE_CODE_CLASS (code
) == tcc_unary
)
3705 extract_range_from_unary_expr (vr
, gimple_assign_rhs_code (stmt
),
3706 gimple_expr_type (stmt
),
3707 gimple_assign_rhs1 (stmt
));
3708 else if (code
== COND_EXPR
)
3709 extract_range_from_cond_expr (vr
, stmt
);
3710 else if (TREE_CODE_CLASS (code
) == tcc_comparison
)
3711 extract_range_from_comparison (vr
, gimple_assign_rhs_code (stmt
),
3712 gimple_expr_type (stmt
),
3713 gimple_assign_rhs1 (stmt
),
3714 gimple_assign_rhs2 (stmt
));
3715 else if (get_gimple_rhs_class (code
) == GIMPLE_SINGLE_RHS
3716 && is_gimple_min_invariant (gimple_assign_rhs1 (stmt
)))
3717 set_value_range_to_value (vr
, gimple_assign_rhs1 (stmt
), NULL
);
3719 set_value_range_to_varying (vr
);
3721 if (vr
->type
== VR_VARYING
)
3722 extract_range_basic (vr
, stmt
);
3725 /* Given a range VR, a LOOP and a variable VAR, determine whether it
3726 would be profitable to adjust VR using scalar evolution information
3727 for VAR. If so, update VR with the new limits. */
3730 adjust_range_with_scev (value_range_t
*vr
, struct loop
*loop
,
3731 gimple stmt
, tree var
)
3733 tree init
, step
, chrec
, tmin
, tmax
, min
, max
, type
, tem
;
3734 enum ev_direction dir
;
3736 /* TODO. Don't adjust anti-ranges. An anti-range may provide
3737 better opportunities than a regular range, but I'm not sure. */
3738 if (vr
->type
== VR_ANTI_RANGE
)
3741 chrec
= instantiate_parameters (loop
, analyze_scalar_evolution (loop
, var
));
3743 /* Like in PR19590, scev can return a constant function. */
3744 if (is_gimple_min_invariant (chrec
))
3746 set_value_range_to_value (vr
, chrec
, vr
->equiv
);
3750 if (TREE_CODE (chrec
) != POLYNOMIAL_CHREC
)
3753 init
= initial_condition_in_loop_num (chrec
, loop
->num
);
3754 tem
= op_with_constant_singleton_value_range (init
);
3757 step
= evolution_part_in_loop_num (chrec
, loop
->num
);
3758 tem
= op_with_constant_singleton_value_range (step
);
3762 /* If STEP is symbolic, we can't know whether INIT will be the
3763 minimum or maximum value in the range. Also, unless INIT is
3764 a simple expression, compare_values and possibly other functions
3765 in tree-vrp won't be able to handle it. */
3766 if (step
== NULL_TREE
3767 || !is_gimple_min_invariant (step
)
3768 || !valid_value_p (init
))
3771 dir
= scev_direction (chrec
);
3772 if (/* Do not adjust ranges if we do not know whether the iv increases
3773 or decreases, ... */
3774 dir
== EV_DIR_UNKNOWN
3775 /* ... or if it may wrap. */
3776 || scev_probably_wraps_p (init
, step
, stmt
, get_chrec_loop (chrec
),
3780 /* We use TYPE_MIN_VALUE and TYPE_MAX_VALUE here instead of
3781 negative_overflow_infinity and positive_overflow_infinity,
3782 because we have concluded that the loop probably does not
3785 type
= TREE_TYPE (var
);
3786 if (POINTER_TYPE_P (type
) || !TYPE_MIN_VALUE (type
))
3787 tmin
= lower_bound_in_type (type
, type
);
3789 tmin
= TYPE_MIN_VALUE (type
);
3790 if (POINTER_TYPE_P (type
) || !TYPE_MAX_VALUE (type
))
3791 tmax
= upper_bound_in_type (type
, type
);
3793 tmax
= TYPE_MAX_VALUE (type
);
3795 /* Try to use estimated number of iterations for the loop to constrain the
3796 final value in the evolution. */
3797 if (TREE_CODE (step
) == INTEGER_CST
3798 && is_gimple_val (init
)
3799 && (TREE_CODE (init
) != SSA_NAME
3800 || get_value_range (init
)->type
== VR_RANGE
))
3804 /* We are only entering here for loop header PHI nodes, so using
3805 the number of latch executions is the correct thing to use. */
3806 if (max_loop_iterations (loop
, &nit
))
3808 value_range_t maxvr
= VR_INITIALIZER
;
3809 signop sgn
= TYPE_SIGN (TREE_TYPE (step
));
3812 wide_int wtmp
= wi::mul (wi::to_widest (step
), nit
, sgn
, &overflow
);
3813 /* If the multiplication overflowed we can't do a meaningful
3814 adjustment. Likewise if the result doesn't fit in the type
3815 of the induction variable. For a signed type we have to
3816 check whether the result has the expected signedness which
3817 is that of the step as number of iterations is unsigned. */
3819 && wi::fits_to_tree_p (wtmp
, TREE_TYPE (init
))
3821 || wi::gts_p (wtmp
, 0) == wi::gts_p (step
, 0)))
3823 tem
= wide_int_to_tree (TREE_TYPE (init
), wtmp
);
3824 extract_range_from_binary_expr (&maxvr
, PLUS_EXPR
,
3825 TREE_TYPE (init
), init
, tem
);
3826 /* Likewise if the addition did. */
3827 if (maxvr
.type
== VR_RANGE
)
3836 if (vr
->type
== VR_VARYING
|| vr
->type
== VR_UNDEFINED
)
3841 /* For VARYING or UNDEFINED ranges, just about anything we get
3842 from scalar evolutions should be better. */
3844 if (dir
== EV_DIR_DECREASES
)
3849 /* If we would create an invalid range, then just assume we
3850 know absolutely nothing. This may be over-conservative,
3851 but it's clearly safe, and should happen only in unreachable
3852 parts of code, or for invalid programs. */
3853 if (compare_values (min
, max
) == 1)
3856 set_value_range (vr
, VR_RANGE
, min
, max
, vr
->equiv
);
3858 else if (vr
->type
== VR_RANGE
)
3863 if (dir
== EV_DIR_DECREASES
)
3865 /* INIT is the maximum value. If INIT is lower than VR->MAX
3866 but no smaller than VR->MIN, set VR->MAX to INIT. */
3867 if (compare_values (init
, max
) == -1)
3870 /* According to the loop information, the variable does not
3871 overflow. If we think it does, probably because of an
3872 overflow due to arithmetic on a different INF value,
3874 if (is_negative_overflow_infinity (min
)
3875 || compare_values (min
, tmin
) == -1)
3881 /* If INIT is bigger than VR->MIN, set VR->MIN to INIT. */
3882 if (compare_values (init
, min
) == 1)
3885 if (is_positive_overflow_infinity (max
)
3886 || compare_values (tmax
, max
) == -1)
3890 /* If we just created an invalid range with the minimum
3891 greater than the maximum, we fail conservatively.
3892 This should happen only in unreachable
3893 parts of code, or for invalid programs. */
3894 if (compare_values (min
, max
) == 1)
3897 set_value_range (vr
, VR_RANGE
, min
, max
, vr
->equiv
);
3901 /* Return true if VAR may overflow at STMT. This checks any available
3902 loop information to see if we can determine that VAR does not
3906 vrp_var_may_overflow (tree var
, gimple stmt
)
3909 tree chrec
, init
, step
;
3911 if (current_loops
== NULL
)
3914 l
= loop_containing_stmt (stmt
);
3919 chrec
= instantiate_parameters (l
, analyze_scalar_evolution (l
, var
));
3920 if (TREE_CODE (chrec
) != POLYNOMIAL_CHREC
)
3923 init
= initial_condition_in_loop_num (chrec
, l
->num
);
3924 step
= evolution_part_in_loop_num (chrec
, l
->num
);
3926 if (step
== NULL_TREE
3927 || !is_gimple_min_invariant (step
)
3928 || !valid_value_p (init
))
3931 /* If we get here, we know something useful about VAR based on the
3932 loop information. If it wraps, it may overflow. */
3934 if (scev_probably_wraps_p (init
, step
, stmt
, get_chrec_loop (chrec
),
3938 if (dump_file
&& (dump_flags
& TDF_DETAILS
) != 0)
3940 print_generic_expr (dump_file
, var
, 0);
3941 fprintf (dump_file
, ": loop information indicates does not overflow\n");
3948 /* Given two numeric value ranges VR0, VR1 and a comparison code COMP:
3950 - Return BOOLEAN_TRUE_NODE if VR0 COMP VR1 always returns true for
3951 all the values in the ranges.
3953 - Return BOOLEAN_FALSE_NODE if the comparison always returns false.
3955 - Return NULL_TREE if it is not always possible to determine the
3956 value of the comparison.
3958 Also set *STRICT_OVERFLOW_P to indicate whether a range with an
3959 overflow infinity was used in the test. */
3963 compare_ranges (enum tree_code comp
, value_range_t
*vr0
, value_range_t
*vr1
,
3964 bool *strict_overflow_p
)
3966 /* VARYING or UNDEFINED ranges cannot be compared. */
3967 if (vr0
->type
== VR_VARYING
3968 || vr0
->type
== VR_UNDEFINED
3969 || vr1
->type
== VR_VARYING
3970 || vr1
->type
== VR_UNDEFINED
)
3973 /* Anti-ranges need to be handled separately. */
3974 if (vr0
->type
== VR_ANTI_RANGE
|| vr1
->type
== VR_ANTI_RANGE
)
3976 /* If both are anti-ranges, then we cannot compute any
3978 if (vr0
->type
== VR_ANTI_RANGE
&& vr1
->type
== VR_ANTI_RANGE
)
3981 /* These comparisons are never statically computable. */
3988 /* Equality can be computed only between a range and an
3989 anti-range. ~[VAL1, VAL2] == [VAL1, VAL2] is always false. */
3990 if (vr0
->type
== VR_RANGE
)
3992 /* To simplify processing, make VR0 the anti-range. */
3993 value_range_t
*tmp
= vr0
;
3998 gcc_assert (comp
== NE_EXPR
|| comp
== EQ_EXPR
);
4000 if (compare_values_warnv (vr0
->min
, vr1
->min
, strict_overflow_p
) == 0
4001 && compare_values_warnv (vr0
->max
, vr1
->max
, strict_overflow_p
) == 0)
4002 return (comp
== NE_EXPR
) ? boolean_true_node
: boolean_false_node
;
4007 if (!usable_range_p (vr0
, strict_overflow_p
)
4008 || !usable_range_p (vr1
, strict_overflow_p
))
4011 /* Simplify processing. If COMP is GT_EXPR or GE_EXPR, switch the
4012 operands around and change the comparison code. */
4013 if (comp
== GT_EXPR
|| comp
== GE_EXPR
)
4016 comp
= (comp
== GT_EXPR
) ? LT_EXPR
: LE_EXPR
;
4022 if (comp
== EQ_EXPR
)
4024 /* Equality may only be computed if both ranges represent
4025 exactly one value. */
4026 if (compare_values_warnv (vr0
->min
, vr0
->max
, strict_overflow_p
) == 0
4027 && compare_values_warnv (vr1
->min
, vr1
->max
, strict_overflow_p
) == 0)
4029 int cmp_min
= compare_values_warnv (vr0
->min
, vr1
->min
,
4031 int cmp_max
= compare_values_warnv (vr0
->max
, vr1
->max
,
4033 if (cmp_min
== 0 && cmp_max
== 0)
4034 return boolean_true_node
;
4035 else if (cmp_min
!= -2 && cmp_max
!= -2)
4036 return boolean_false_node
;
4038 /* If [V0_MIN, V1_MAX] < [V1_MIN, V1_MAX] then V0 != V1. */
4039 else if (compare_values_warnv (vr0
->min
, vr1
->max
,
4040 strict_overflow_p
) == 1
4041 || compare_values_warnv (vr1
->min
, vr0
->max
,
4042 strict_overflow_p
) == 1)
4043 return boolean_false_node
;
4047 else if (comp
== NE_EXPR
)
4051 /* If VR0 is completely to the left or completely to the right
4052 of VR1, they are always different. Notice that we need to
4053 make sure that both comparisons yield similar results to
4054 avoid comparing values that cannot be compared at
4056 cmp1
= compare_values_warnv (vr0
->max
, vr1
->min
, strict_overflow_p
);
4057 cmp2
= compare_values_warnv (vr0
->min
, vr1
->max
, strict_overflow_p
);
4058 if ((cmp1
== -1 && cmp2
== -1) || (cmp1
== 1 && cmp2
== 1))
4059 return boolean_true_node
;
4061 /* If VR0 and VR1 represent a single value and are identical,
4063 else if (compare_values_warnv (vr0
->min
, vr0
->max
,
4064 strict_overflow_p
) == 0
4065 && compare_values_warnv (vr1
->min
, vr1
->max
,
4066 strict_overflow_p
) == 0
4067 && compare_values_warnv (vr0
->min
, vr1
->min
,
4068 strict_overflow_p
) == 0
4069 && compare_values_warnv (vr0
->max
, vr1
->max
,
4070 strict_overflow_p
) == 0)
4071 return boolean_false_node
;
4073 /* Otherwise, they may or may not be different. */
4077 else if (comp
== LT_EXPR
|| comp
== LE_EXPR
)
4081 /* If VR0 is to the left of VR1, return true. */
4082 tst
= compare_values_warnv (vr0
->max
, vr1
->min
, strict_overflow_p
);
4083 if ((comp
== LT_EXPR
&& tst
== -1)
4084 || (comp
== LE_EXPR
&& (tst
== -1 || tst
== 0)))
4086 if (overflow_infinity_range_p (vr0
)
4087 || overflow_infinity_range_p (vr1
))
4088 *strict_overflow_p
= true;
4089 return boolean_true_node
;
4092 /* If VR0 is to the right of VR1, return false. */
4093 tst
= compare_values_warnv (vr0
->min
, vr1
->max
, strict_overflow_p
);
4094 if ((comp
== LT_EXPR
&& (tst
== 0 || tst
== 1))
4095 || (comp
== LE_EXPR
&& tst
== 1))
4097 if (overflow_infinity_range_p (vr0
)
4098 || overflow_infinity_range_p (vr1
))
4099 *strict_overflow_p
= true;
4100 return boolean_false_node
;
4103 /* Otherwise, we don't know. */
4111 /* Given a value range VR, a value VAL and a comparison code COMP, return
4112 BOOLEAN_TRUE_NODE if VR COMP VAL always returns true for all the
4113 values in VR. Return BOOLEAN_FALSE_NODE if the comparison
4114 always returns false. Return NULL_TREE if it is not always
4115 possible to determine the value of the comparison. Also set
4116 *STRICT_OVERFLOW_P to indicate whether a range with an overflow
4117 infinity was used in the test. */
4120 compare_range_with_value (enum tree_code comp
, value_range_t
*vr
, tree val
,
4121 bool *strict_overflow_p
)
4123 if (vr
->type
== VR_VARYING
|| vr
->type
== VR_UNDEFINED
)
4126 /* Anti-ranges need to be handled separately. */
4127 if (vr
->type
== VR_ANTI_RANGE
)
4129 /* For anti-ranges, the only predicates that we can compute at
4130 compile time are equality and inequality. */
4137 /* ~[VAL_1, VAL_2] OP VAL is known if VAL_1 <= VAL <= VAL_2. */
4138 if (value_inside_range (val
, vr
->min
, vr
->max
) == 1)
4139 return (comp
== NE_EXPR
) ? boolean_true_node
: boolean_false_node
;
4144 if (!usable_range_p (vr
, strict_overflow_p
))
4147 if (comp
== EQ_EXPR
)
4149 /* EQ_EXPR may only be computed if VR represents exactly
4151 if (compare_values_warnv (vr
->min
, vr
->max
, strict_overflow_p
) == 0)
4153 int cmp
= compare_values_warnv (vr
->min
, val
, strict_overflow_p
);
4155 return boolean_true_node
;
4156 else if (cmp
== -1 || cmp
== 1 || cmp
== 2)
4157 return boolean_false_node
;
4159 else if (compare_values_warnv (val
, vr
->min
, strict_overflow_p
) == -1
4160 || compare_values_warnv (vr
->max
, val
, strict_overflow_p
) == -1)
4161 return boolean_false_node
;
4165 else if (comp
== NE_EXPR
)
4167 /* If VAL is not inside VR, then they are always different. */
4168 if (compare_values_warnv (vr
->max
, val
, strict_overflow_p
) == -1
4169 || compare_values_warnv (vr
->min
, val
, strict_overflow_p
) == 1)
4170 return boolean_true_node
;
4172 /* If VR represents exactly one value equal to VAL, then return
4174 if (compare_values_warnv (vr
->min
, vr
->max
, strict_overflow_p
) == 0
4175 && compare_values_warnv (vr
->min
, val
, strict_overflow_p
) == 0)
4176 return boolean_false_node
;
4178 /* Otherwise, they may or may not be different. */
4181 else if (comp
== LT_EXPR
|| comp
== LE_EXPR
)
4185 /* If VR is to the left of VAL, return true. */
4186 tst
= compare_values_warnv (vr
->max
, val
, strict_overflow_p
);
4187 if ((comp
== LT_EXPR
&& tst
== -1)
4188 || (comp
== LE_EXPR
&& (tst
== -1 || tst
== 0)))
4190 if (overflow_infinity_range_p (vr
))
4191 *strict_overflow_p
= true;
4192 return boolean_true_node
;
4195 /* If VR is to the right of VAL, return false. */
4196 tst
= compare_values_warnv (vr
->min
, val
, strict_overflow_p
);
4197 if ((comp
== LT_EXPR
&& (tst
== 0 || tst
== 1))
4198 || (comp
== LE_EXPR
&& tst
== 1))
4200 if (overflow_infinity_range_p (vr
))
4201 *strict_overflow_p
= true;
4202 return boolean_false_node
;
4205 /* Otherwise, we don't know. */
4208 else if (comp
== GT_EXPR
|| comp
== GE_EXPR
)
4212 /* If VR is to the right of VAL, return true. */
4213 tst
= compare_values_warnv (vr
->min
, val
, strict_overflow_p
);
4214 if ((comp
== GT_EXPR
&& tst
== 1)
4215 || (comp
== GE_EXPR
&& (tst
== 0 || tst
== 1)))
4217 if (overflow_infinity_range_p (vr
))
4218 *strict_overflow_p
= true;
4219 return boolean_true_node
;
4222 /* If VR is to the left of VAL, return false. */
4223 tst
= compare_values_warnv (vr
->max
, val
, strict_overflow_p
);
4224 if ((comp
== GT_EXPR
&& (tst
== -1 || tst
== 0))
4225 || (comp
== GE_EXPR
&& tst
== -1))
4227 if (overflow_infinity_range_p (vr
))
4228 *strict_overflow_p
= true;
4229 return boolean_false_node
;
4232 /* Otherwise, we don't know. */
4240 /* Debugging dumps. */
4242 void dump_value_range (FILE *, value_range_t
*);
4243 void debug_value_range (value_range_t
*);
4244 void dump_all_value_ranges (FILE *);
4245 void debug_all_value_ranges (void);
4246 void dump_vr_equiv (FILE *, bitmap
);
4247 void debug_vr_equiv (bitmap
);
4250 /* Dump value range VR to FILE. */
4253 dump_value_range (FILE *file
, value_range_t
*vr
)
4256 fprintf (file
, "[]");
4257 else if (vr
->type
== VR_UNDEFINED
)
4258 fprintf (file
, "UNDEFINED");
4259 else if (vr
->type
== VR_RANGE
|| vr
->type
== VR_ANTI_RANGE
)
4261 tree type
= TREE_TYPE (vr
->min
);
4263 fprintf (file
, "%s[", (vr
->type
== VR_ANTI_RANGE
) ? "~" : "");
4265 if (is_negative_overflow_infinity (vr
->min
))
4266 fprintf (file
, "-INF(OVF)");
4267 else if (INTEGRAL_TYPE_P (type
)
4268 && !TYPE_UNSIGNED (type
)
4269 && vrp_val_is_min (vr
->min
))
4270 fprintf (file
, "-INF");
4272 print_generic_expr (file
, vr
->min
, 0);
4274 fprintf (file
, ", ");
4276 if (is_positive_overflow_infinity (vr
->max
))
4277 fprintf (file
, "+INF(OVF)");
4278 else if (INTEGRAL_TYPE_P (type
)
4279 && vrp_val_is_max (vr
->max
))
4280 fprintf (file
, "+INF");
4282 print_generic_expr (file
, vr
->max
, 0);
4284 fprintf (file
, "]");
4291 fprintf (file
, " EQUIVALENCES: { ");
4293 EXECUTE_IF_SET_IN_BITMAP (vr
->equiv
, 0, i
, bi
)
4295 print_generic_expr (file
, ssa_name (i
), 0);
4296 fprintf (file
, " ");
4300 fprintf (file
, "} (%u elements)", c
);
4303 else if (vr
->type
== VR_VARYING
)
4304 fprintf (file
, "VARYING");
4306 fprintf (file
, "INVALID RANGE");
4310 /* Dump value range VR to stderr. */
4313 debug_value_range (value_range_t
*vr
)
4315 dump_value_range (stderr
, vr
);
4316 fprintf (stderr
, "\n");
4320 /* Dump value ranges of all SSA_NAMEs to FILE. */
4323 dump_all_value_ranges (FILE *file
)
4327 for (i
= 0; i
< num_vr_values
; i
++)
4331 print_generic_expr (file
, ssa_name (i
), 0);
4332 fprintf (file
, ": ");
4333 dump_value_range (file
, vr_value
[i
]);
4334 fprintf (file
, "\n");
4338 fprintf (file
, "\n");
4342 /* Dump all value ranges to stderr. */
4345 debug_all_value_ranges (void)
4347 dump_all_value_ranges (stderr
);
4351 /* Given a COND_EXPR COND of the form 'V OP W', and an SSA name V,
4352 create a new SSA name N and return the assertion assignment
4353 'V = ASSERT_EXPR <V, V OP W>'. */
4356 build_assert_expr_for (tree cond
, tree v
)
4361 gcc_assert (TREE_CODE (v
) == SSA_NAME
4362 && COMPARISON_CLASS_P (cond
));
4364 a
= build2 (ASSERT_EXPR
, TREE_TYPE (v
), v
, cond
);
4365 assertion
= gimple_build_assign (NULL_TREE
, a
);
4367 /* The new ASSERT_EXPR, creates a new SSA name that replaces the
4368 operand of the ASSERT_EXPR. Create it so the new name and the old one
4369 are registered in the replacement table so that we can fix the SSA web
4370 after adding all the ASSERT_EXPRs. */
4371 create_new_def_for (v
, assertion
, NULL
);
4377 /* Return false if EXPR is a predicate expression involving floating
4381 fp_predicate (gimple stmt
)
4383 GIMPLE_CHECK (stmt
, GIMPLE_COND
);
4385 return FLOAT_TYPE_P (TREE_TYPE (gimple_cond_lhs (stmt
)));
4388 /* If the range of values taken by OP can be inferred after STMT executes,
4389 return the comparison code (COMP_CODE_P) and value (VAL_P) that
4390 describes the inferred range. Return true if a range could be
4394 infer_value_range (gimple stmt
, tree op
, enum tree_code
*comp_code_p
, tree
*val_p
)
4397 *comp_code_p
= ERROR_MARK
;
4399 /* Do not attempt to infer anything in names that flow through
4401 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op
))
4404 /* Similarly, don't infer anything from statements that may throw
4405 exceptions. ??? Relax this requirement? */
4406 if (stmt_could_throw_p (stmt
))
4409 /* If STMT is the last statement of a basic block with no
4410 successors, there is no point inferring anything about any of its
4411 operands. We would not be able to find a proper insertion point
4412 for the assertion, anyway. */
4413 if (stmt_ends_bb_p (stmt
) && EDGE_COUNT (gimple_bb (stmt
)->succs
) == 0)
4416 if (infer_nonnull_range (stmt
, op
))
4418 *val_p
= build_int_cst (TREE_TYPE (op
), 0);
4419 *comp_code_p
= NE_EXPR
;
4427 void dump_asserts_for (FILE *, tree
);
4428 void debug_asserts_for (tree
);
4429 void dump_all_asserts (FILE *);
4430 void debug_all_asserts (void);
4432 /* Dump all the registered assertions for NAME to FILE. */
4435 dump_asserts_for (FILE *file
, tree name
)
4439 fprintf (file
, "Assertions to be inserted for ");
4440 print_generic_expr (file
, name
, 0);
4441 fprintf (file
, "\n");
4443 loc
= asserts_for
[SSA_NAME_VERSION (name
)];
4446 fprintf (file
, "\t");
4447 print_gimple_stmt (file
, gsi_stmt (loc
->si
), 0, 0);
4448 fprintf (file
, "\n\tBB #%d", loc
->bb
->index
);
4451 fprintf (file
, "\n\tEDGE %d->%d", loc
->e
->src
->index
,
4452 loc
->e
->dest
->index
);
4453 dump_edge_info (file
, loc
->e
, dump_flags
, 0);
4455 fprintf (file
, "\n\tPREDICATE: ");
4456 print_generic_expr (file
, name
, 0);
4457 fprintf (file
, " %s ", get_tree_code_name (loc
->comp_code
));
4458 print_generic_expr (file
, loc
->val
, 0);
4459 fprintf (file
, "\n\n");
4463 fprintf (file
, "\n");
4467 /* Dump all the registered assertions for NAME to stderr. */
4470 debug_asserts_for (tree name
)
4472 dump_asserts_for (stderr
, name
);
4476 /* Dump all the registered assertions for all the names to FILE. */
4479 dump_all_asserts (FILE *file
)
4484 fprintf (file
, "\nASSERT_EXPRs to be inserted\n\n");
4485 EXECUTE_IF_SET_IN_BITMAP (need_assert_for
, 0, i
, bi
)
4486 dump_asserts_for (file
, ssa_name (i
));
4487 fprintf (file
, "\n");
4491 /* Dump all the registered assertions for all the names to stderr. */
4494 debug_all_asserts (void)
4496 dump_all_asserts (stderr
);
4500 /* If NAME doesn't have an ASSERT_EXPR registered for asserting
4501 'EXPR COMP_CODE VAL' at a location that dominates block BB or
4502 E->DEST, then register this location as a possible insertion point
4503 for ASSERT_EXPR <NAME, EXPR COMP_CODE VAL>.
4505 BB, E and SI provide the exact insertion point for the new
4506 ASSERT_EXPR. If BB is NULL, then the ASSERT_EXPR is to be inserted
4507 on edge E. Otherwise, if E is NULL, the ASSERT_EXPR is inserted on
4508 BB. If SI points to a COND_EXPR or a SWITCH_EXPR statement, then E
4509 must not be NULL. */
4512 register_new_assert_for (tree name
, tree expr
,
4513 enum tree_code comp_code
,
4517 gimple_stmt_iterator si
)
4519 assert_locus_t n
, loc
, last_loc
;
4520 basic_block dest_bb
;
4522 gcc_checking_assert (bb
== NULL
|| e
== NULL
);
4525 gcc_checking_assert (gimple_code (gsi_stmt (si
)) != GIMPLE_COND
4526 && gimple_code (gsi_stmt (si
)) != GIMPLE_SWITCH
);
4528 /* Never build an assert comparing against an integer constant with
4529 TREE_OVERFLOW set. This confuses our undefined overflow warning
4531 if (TREE_OVERFLOW_P (val
))
4532 val
= drop_tree_overflow (val
);
4534 /* The new assertion A will be inserted at BB or E. We need to
4535 determine if the new location is dominated by a previously
4536 registered location for A. If we are doing an edge insertion,
4537 assume that A will be inserted at E->DEST. Note that this is not
4540 If E is a critical edge, it will be split. But even if E is
4541 split, the new block will dominate the same set of blocks that
4544 The reverse, however, is not true, blocks dominated by E->DEST
4545 will not be dominated by the new block created to split E. So,
4546 if the insertion location is on a critical edge, we will not use
4547 the new location to move another assertion previously registered
4548 at a block dominated by E->DEST. */
4549 dest_bb
= (bb
) ? bb
: e
->dest
;
4551 /* If NAME already has an ASSERT_EXPR registered for COMP_CODE and
4552 VAL at a block dominating DEST_BB, then we don't need to insert a new
4553 one. Similarly, if the same assertion already exists at a block
4554 dominated by DEST_BB and the new location is not on a critical
4555 edge, then update the existing location for the assertion (i.e.,
4556 move the assertion up in the dominance tree).
4558 Note, this is implemented as a simple linked list because there
4559 should not be more than a handful of assertions registered per
4560 name. If this becomes a performance problem, a table hashed by
4561 COMP_CODE and VAL could be implemented. */
4562 loc
= asserts_for
[SSA_NAME_VERSION (name
)];
4566 if (loc
->comp_code
== comp_code
4568 || operand_equal_p (loc
->val
, val
, 0))
4569 && (loc
->expr
== expr
4570 || operand_equal_p (loc
->expr
, expr
, 0)))
4572 /* If E is not a critical edge and DEST_BB
4573 dominates the existing location for the assertion, move
4574 the assertion up in the dominance tree by updating its
4575 location information. */
4576 if ((e
== NULL
|| !EDGE_CRITICAL_P (e
))
4577 && dominated_by_p (CDI_DOMINATORS
, loc
->bb
, dest_bb
))
4586 /* Update the last node of the list and move to the next one. */
4591 /* If we didn't find an assertion already registered for
4592 NAME COMP_CODE VAL, add a new one at the end of the list of
4593 assertions associated with NAME. */
4594 n
= XNEW (struct assert_locus_d
);
4598 n
->comp_code
= comp_code
;
4606 asserts_for
[SSA_NAME_VERSION (name
)] = n
;
4608 bitmap_set_bit (need_assert_for
, SSA_NAME_VERSION (name
));
4611 /* (COND_OP0 COND_CODE COND_OP1) is a predicate which uses NAME.
4612 Extract a suitable test code and value and store them into *CODE_P and
4613 *VAL_P so the predicate is normalized to NAME *CODE_P *VAL_P.
4615 If no extraction was possible, return FALSE, otherwise return TRUE.
4617 If INVERT is true, then we invert the result stored into *CODE_P. */
4620 extract_code_and_val_from_cond_with_ops (tree name
, enum tree_code cond_code
,
4621 tree cond_op0
, tree cond_op1
,
4622 bool invert
, enum tree_code
*code_p
,
4625 enum tree_code comp_code
;
4628 /* Otherwise, we have a comparison of the form NAME COMP VAL
4629 or VAL COMP NAME. */
4630 if (name
== cond_op1
)
4632 /* If the predicate is of the form VAL COMP NAME, flip
4633 COMP around because we need to register NAME as the
4634 first operand in the predicate. */
4635 comp_code
= swap_tree_comparison (cond_code
);
4640 /* The comparison is of the form NAME COMP VAL, so the
4641 comparison code remains unchanged. */
4642 comp_code
= cond_code
;
4646 /* Invert the comparison code as necessary. */
4648 comp_code
= invert_tree_comparison (comp_code
, 0);
4650 /* VRP does not handle float types. */
4651 if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (val
)))
4654 /* Do not register always-false predicates.
4655 FIXME: this works around a limitation in fold() when dealing with
4656 enumerations. Given 'enum { N1, N2 } x;', fold will not
4657 fold 'if (x > N2)' to 'if (0)'. */
4658 if ((comp_code
== GT_EXPR
|| comp_code
== LT_EXPR
)
4659 && INTEGRAL_TYPE_P (TREE_TYPE (val
)))
4661 tree min
= TYPE_MIN_VALUE (TREE_TYPE (val
));
4662 tree max
= TYPE_MAX_VALUE (TREE_TYPE (val
));
4664 if (comp_code
== GT_EXPR
4666 || compare_values (val
, max
) == 0))
4669 if (comp_code
== LT_EXPR
4671 || compare_values (val
, min
) == 0))
4674 *code_p
= comp_code
;
4679 /* Find out smallest RES where RES > VAL && (RES & MASK) == RES, if any
4680 (otherwise return VAL). VAL and MASK must be zero-extended for
4681 precision PREC. If SGNBIT is non-zero, first xor VAL with SGNBIT
4682 (to transform signed values into unsigned) and at the end xor
4686 masked_increment (wide_int val
, wide_int mask
, wide_int sgnbit
,
4689 wide_int bit
= wi::one (prec
), res
;
4693 for (i
= 0; i
< prec
; i
++, bit
+= bit
)
4696 if ((res
& bit
) == 0)
4699 res
= (val
+ bit
).and_not (res
);
4701 if (wi::gtu_p (res
, val
))
4702 return res
^ sgnbit
;
4704 return val
^ sgnbit
;
4707 /* Try to register an edge assertion for SSA name NAME on edge E for
4708 the condition COND contributing to the conditional jump pointed to by BSI.
4709 Invert the condition COND if INVERT is true.
4710 Return true if an assertion for NAME could be registered. */
4713 register_edge_assert_for_2 (tree name
, edge e
, gimple_stmt_iterator bsi
,
4714 enum tree_code cond_code
,
4715 tree cond_op0
, tree cond_op1
, bool invert
)
4718 enum tree_code comp_code
;
4719 bool retval
= false;
4721 if (!extract_code_and_val_from_cond_with_ops (name
, cond_code
,
4724 invert
, &comp_code
, &val
))
4727 /* Only register an ASSERT_EXPR if NAME was found in the sub-graph
4728 reachable from E. */
4729 if (live_on_edge (e
, name
)
4730 && !has_single_use (name
))
4732 register_new_assert_for (name
, name
, comp_code
, val
, NULL
, e
, bsi
);
4736 /* In the case of NAME <= CST and NAME being defined as
4737 NAME = (unsigned) NAME2 + CST2 we can assert NAME2 >= -CST2
4738 and NAME2 <= CST - CST2. We can do the same for NAME > CST.
4739 This catches range and anti-range tests. */
4740 if ((comp_code
== LE_EXPR
4741 || comp_code
== GT_EXPR
)
4742 && TREE_CODE (val
) == INTEGER_CST
4743 && TYPE_UNSIGNED (TREE_TYPE (val
)))
4745 gimple def_stmt
= SSA_NAME_DEF_STMT (name
);
4746 tree cst2
= NULL_TREE
, name2
= NULL_TREE
, name3
= NULL_TREE
;
4748 /* Extract CST2 from the (optional) addition. */
4749 if (is_gimple_assign (def_stmt
)
4750 && gimple_assign_rhs_code (def_stmt
) == PLUS_EXPR
)
4752 name2
= gimple_assign_rhs1 (def_stmt
);
4753 cst2
= gimple_assign_rhs2 (def_stmt
);
4754 if (TREE_CODE (name2
) == SSA_NAME
4755 && TREE_CODE (cst2
) == INTEGER_CST
)
4756 def_stmt
= SSA_NAME_DEF_STMT (name2
);
4759 /* Extract NAME2 from the (optional) sign-changing cast. */
4760 if (gimple_assign_cast_p (def_stmt
))
4762 if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt
))
4763 && ! TYPE_UNSIGNED (TREE_TYPE (gimple_assign_rhs1 (def_stmt
)))
4764 && (TYPE_PRECISION (gimple_expr_type (def_stmt
))
4765 == TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (def_stmt
)))))
4766 name3
= gimple_assign_rhs1 (def_stmt
);
4769 /* If name3 is used later, create an ASSERT_EXPR for it. */
4770 if (name3
!= NULL_TREE
4771 && TREE_CODE (name3
) == SSA_NAME
4772 && (cst2
== NULL_TREE
4773 || TREE_CODE (cst2
) == INTEGER_CST
)
4774 && INTEGRAL_TYPE_P (TREE_TYPE (name3
))
4775 && live_on_edge (e
, name3
)
4776 && !has_single_use (name3
))
4780 /* Build an expression for the range test. */
4781 tmp
= build1 (NOP_EXPR
, TREE_TYPE (name
), name3
);
4782 if (cst2
!= NULL_TREE
)
4783 tmp
= build2 (PLUS_EXPR
, TREE_TYPE (name
), tmp
, cst2
);
4787 fprintf (dump_file
, "Adding assert for ");
4788 print_generic_expr (dump_file
, name3
, 0);
4789 fprintf (dump_file
, " from ");
4790 print_generic_expr (dump_file
, tmp
, 0);
4791 fprintf (dump_file
, "\n");
4794 register_new_assert_for (name3
, tmp
, comp_code
, val
, NULL
, e
, bsi
);
4799 /* If name2 is used later, create an ASSERT_EXPR for it. */
4800 if (name2
!= NULL_TREE
4801 && TREE_CODE (name2
) == SSA_NAME
4802 && TREE_CODE (cst2
) == INTEGER_CST
4803 && INTEGRAL_TYPE_P (TREE_TYPE (name2
))
4804 && live_on_edge (e
, name2
)
4805 && !has_single_use (name2
))
4809 /* Build an expression for the range test. */
4811 if (TREE_TYPE (name
) != TREE_TYPE (name2
))
4812 tmp
= build1 (NOP_EXPR
, TREE_TYPE (name
), tmp
);
4813 if (cst2
!= NULL_TREE
)
4814 tmp
= build2 (PLUS_EXPR
, TREE_TYPE (name
), tmp
, cst2
);
4818 fprintf (dump_file
, "Adding assert for ");
4819 print_generic_expr (dump_file
, name2
, 0);
4820 fprintf (dump_file
, " from ");
4821 print_generic_expr (dump_file
, tmp
, 0);
4822 fprintf (dump_file
, "\n");
4825 register_new_assert_for (name2
, tmp
, comp_code
, val
, NULL
, e
, bsi
);
4831 /* In the case of post-in/decrement tests like if (i++) ... and uses
4832 of the in/decremented value on the edge the extra name we want to
4833 assert for is not on the def chain of the name compared. Instead
4834 it is in the set of use stmts. */
4835 if ((comp_code
== NE_EXPR
4836 || comp_code
== EQ_EXPR
)
4837 && TREE_CODE (val
) == INTEGER_CST
)
4839 imm_use_iterator ui
;
4841 FOR_EACH_IMM_USE_STMT (use_stmt
, ui
, name
)
4843 /* Cut off to use-stmts that are in the predecessor. */
4844 if (gimple_bb (use_stmt
) != e
->src
)
4847 if (!is_gimple_assign (use_stmt
))
4850 enum tree_code code
= gimple_assign_rhs_code (use_stmt
);
4851 if (code
!= PLUS_EXPR
4852 && code
!= MINUS_EXPR
)
4855 tree cst
= gimple_assign_rhs2 (use_stmt
);
4856 if (TREE_CODE (cst
) != INTEGER_CST
)
4859 tree name2
= gimple_assign_lhs (use_stmt
);
4860 if (live_on_edge (e
, name2
))
4862 cst
= int_const_binop (code
, val
, cst
);
4863 register_new_assert_for (name2
, name2
, comp_code
, cst
,
4870 if (TREE_CODE_CLASS (comp_code
) == tcc_comparison
4871 && TREE_CODE (val
) == INTEGER_CST
)
4873 gimple def_stmt
= SSA_NAME_DEF_STMT (name
);
4874 tree name2
= NULL_TREE
, names
[2], cst2
= NULL_TREE
;
4875 tree val2
= NULL_TREE
;
4876 unsigned int prec
= TYPE_PRECISION (TREE_TYPE (val
));
4877 wide_int mask
= wi::zero (prec
);
4878 unsigned int nprec
= prec
;
4879 enum tree_code rhs_code
= ERROR_MARK
;
4881 if (is_gimple_assign (def_stmt
))
4882 rhs_code
= gimple_assign_rhs_code (def_stmt
);
4884 /* Add asserts for NAME cmp CST and NAME being defined
4885 as NAME = (int) NAME2. */
4886 if (!TYPE_UNSIGNED (TREE_TYPE (val
))
4887 && (comp_code
== LE_EXPR
|| comp_code
== LT_EXPR
4888 || comp_code
== GT_EXPR
|| comp_code
== GE_EXPR
)
4889 && gimple_assign_cast_p (def_stmt
))
4891 name2
= gimple_assign_rhs1 (def_stmt
);
4892 if (CONVERT_EXPR_CODE_P (rhs_code
)
4893 && INTEGRAL_TYPE_P (TREE_TYPE (name2
))
4894 && TYPE_UNSIGNED (TREE_TYPE (name2
))
4895 && prec
== TYPE_PRECISION (TREE_TYPE (name2
))
4896 && (comp_code
== LE_EXPR
|| comp_code
== GT_EXPR
4897 || !tree_int_cst_equal (val
,
4898 TYPE_MIN_VALUE (TREE_TYPE (val
))))
4899 && live_on_edge (e
, name2
)
4900 && !has_single_use (name2
))
4903 enum tree_code new_comp_code
= comp_code
;
4905 cst
= fold_convert (TREE_TYPE (name2
),
4906 TYPE_MIN_VALUE (TREE_TYPE (val
)));
4907 /* Build an expression for the range test. */
4908 tmp
= build2 (PLUS_EXPR
, TREE_TYPE (name2
), name2
, cst
);
4909 cst
= fold_build2 (PLUS_EXPR
, TREE_TYPE (name2
), cst
,
4910 fold_convert (TREE_TYPE (name2
), val
));
4911 if (comp_code
== LT_EXPR
|| comp_code
== GE_EXPR
)
4913 new_comp_code
= comp_code
== LT_EXPR
? LE_EXPR
: GT_EXPR
;
4914 cst
= fold_build2 (MINUS_EXPR
, TREE_TYPE (name2
), cst
,
4915 build_int_cst (TREE_TYPE (name2
), 1));
4920 fprintf (dump_file
, "Adding assert for ");
4921 print_generic_expr (dump_file
, name2
, 0);
4922 fprintf (dump_file
, " from ");
4923 print_generic_expr (dump_file
, tmp
, 0);
4924 fprintf (dump_file
, "\n");
4927 register_new_assert_for (name2
, tmp
, new_comp_code
, cst
, NULL
,
4934 /* Add asserts for NAME cmp CST and NAME being defined as
4935 NAME = NAME2 >> CST2.
4937 Extract CST2 from the right shift. */
4938 if (rhs_code
== RSHIFT_EXPR
)
4940 name2
= gimple_assign_rhs1 (def_stmt
);
4941 cst2
= gimple_assign_rhs2 (def_stmt
);
4942 if (TREE_CODE (name2
) == SSA_NAME
4943 && tree_fits_uhwi_p (cst2
)
4944 && INTEGRAL_TYPE_P (TREE_TYPE (name2
))
4945 && IN_RANGE (tree_to_uhwi (cst2
), 1, prec
- 1)
4946 && prec
== GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (val
)))
4947 && live_on_edge (e
, name2
)
4948 && !has_single_use (name2
))
4950 mask
= wi::mask (tree_to_uhwi (cst2
), false, prec
);
4951 val2
= fold_binary (LSHIFT_EXPR
, TREE_TYPE (val
), val
, cst2
);
4954 if (val2
!= NULL_TREE
4955 && TREE_CODE (val2
) == INTEGER_CST
4956 && simple_cst_equal (fold_build2 (RSHIFT_EXPR
,
4960 enum tree_code new_comp_code
= comp_code
;
4964 if (comp_code
== EQ_EXPR
|| comp_code
== NE_EXPR
)
4966 if (!TYPE_UNSIGNED (TREE_TYPE (val
)))
4968 tree type
= build_nonstandard_integer_type (prec
, 1);
4969 tmp
= build1 (NOP_EXPR
, type
, name2
);
4970 val2
= fold_convert (type
, val2
);
4972 tmp
= fold_build2 (MINUS_EXPR
, TREE_TYPE (tmp
), tmp
, val2
);
4973 new_val
= wide_int_to_tree (TREE_TYPE (tmp
), mask
);
4974 new_comp_code
= comp_code
== EQ_EXPR
? LE_EXPR
: GT_EXPR
;
4976 else if (comp_code
== LT_EXPR
|| comp_code
== GE_EXPR
)
4979 = wi::min_value (prec
, TYPE_SIGN (TREE_TYPE (val
)));
4981 if (minval
== new_val
)
4982 new_val
= NULL_TREE
;
4987 = wi::max_value (prec
, TYPE_SIGN (TREE_TYPE (val
)));
4990 new_val
= NULL_TREE
;
4992 new_val
= wide_int_to_tree (TREE_TYPE (val2
), mask
);
4999 fprintf (dump_file
, "Adding assert for ");
5000 print_generic_expr (dump_file
, name2
, 0);
5001 fprintf (dump_file
, " from ");
5002 print_generic_expr (dump_file
, tmp
, 0);
5003 fprintf (dump_file
, "\n");
5006 register_new_assert_for (name2
, tmp
, new_comp_code
, new_val
,
5012 /* Add asserts for NAME cmp CST and NAME being defined as
5013 NAME = NAME2 & CST2.
5015 Extract CST2 from the and.
5018 NAME = (unsigned) NAME2;
5019 casts where NAME's type is unsigned and has smaller precision
5020 than NAME2's type as if it was NAME = NAME2 & MASK. */
5021 names
[0] = NULL_TREE
;
5022 names
[1] = NULL_TREE
;
5024 if (rhs_code
== BIT_AND_EXPR
5025 || (CONVERT_EXPR_CODE_P (rhs_code
)
5026 && TREE_CODE (TREE_TYPE (val
)) == INTEGER_TYPE
5027 && TYPE_UNSIGNED (TREE_TYPE (val
))
5028 && TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (def_stmt
)))
5032 name2
= gimple_assign_rhs1 (def_stmt
);
5033 if (rhs_code
== BIT_AND_EXPR
)
5034 cst2
= gimple_assign_rhs2 (def_stmt
);
5037 cst2
= TYPE_MAX_VALUE (TREE_TYPE (val
));
5038 nprec
= TYPE_PRECISION (TREE_TYPE (name2
));
5040 if (TREE_CODE (name2
) == SSA_NAME
5041 && INTEGRAL_TYPE_P (TREE_TYPE (name2
))
5042 && TREE_CODE (cst2
) == INTEGER_CST
5043 && !integer_zerop (cst2
)
5045 || TYPE_UNSIGNED (TREE_TYPE (val
))))
5047 gimple def_stmt2
= SSA_NAME_DEF_STMT (name2
);
5048 if (gimple_assign_cast_p (def_stmt2
))
5050 names
[1] = gimple_assign_rhs1 (def_stmt2
);
5051 if (!CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt2
))
5052 || !INTEGRAL_TYPE_P (TREE_TYPE (names
[1]))
5053 || (TYPE_PRECISION (TREE_TYPE (name2
))
5054 != TYPE_PRECISION (TREE_TYPE (names
[1])))
5055 || !live_on_edge (e
, names
[1])
5056 || has_single_use (names
[1]))
5057 names
[1] = NULL_TREE
;
5059 if (live_on_edge (e
, name2
)
5060 && !has_single_use (name2
))
5064 if (names
[0] || names
[1])
5066 wide_int minv
, maxv
, valv
, cst2v
;
5067 wide_int tem
, sgnbit
;
5068 bool valid_p
= false, valn
= false, cst2n
= false;
5069 enum tree_code ccode
= comp_code
;
5071 valv
= wide_int::from (val
, nprec
, UNSIGNED
);
5072 cst2v
= wide_int::from (cst2
, nprec
, UNSIGNED
);
5073 if (TYPE_SIGN (TREE_TYPE (val
)) == SIGNED
)
5075 valn
= wi::neg_p (wi::sext (valv
, nprec
));
5076 cst2n
= wi::neg_p (wi::sext (cst2v
, nprec
));
5078 /* If CST2 doesn't have most significant bit set,
5079 but VAL is negative, we have comparison like
5080 if ((x & 0x123) > -4) (always true). Just give up. */
5084 sgnbit
= wi::set_bit_in_zero (nprec
- 1, nprec
);
5086 sgnbit
= wi::zero (nprec
);
5087 minv
= valv
& cst2v
;
5091 /* Minimum unsigned value for equality is VAL & CST2
5092 (should be equal to VAL, otherwise we probably should
5093 have folded the comparison into false) and
5094 maximum unsigned value is VAL | ~CST2. */
5095 maxv
= valv
| ~cst2v
;
5096 maxv
= wi::zext (maxv
, nprec
);
5101 tem
= valv
| ~cst2v
;
5102 tem
= wi::zext (tem
, nprec
);
5103 /* If VAL is 0, handle (X & CST2) != 0 as (X & CST2) > 0U. */
5107 sgnbit
= wi::zero (nprec
);
5110 /* If (VAL | ~CST2) is all ones, handle it as
5111 (X & CST2) < VAL. */
5116 sgnbit
= wi::zero (nprec
);
5119 if (!cst2n
&& wi::neg_p (wi::sext (cst2v
, nprec
)))
5120 sgnbit
= wi::set_bit_in_zero (nprec
- 1, nprec
);
5129 if (tem
== wi::mask (nprec
- 1, false, nprec
))
5135 sgnbit
= wi::zero (nprec
);
5140 /* Minimum unsigned value for >= if (VAL & CST2) == VAL
5141 is VAL and maximum unsigned value is ~0. For signed
5142 comparison, if CST2 doesn't have most significant bit
5143 set, handle it similarly. If CST2 has MSB set,
5144 the minimum is the same, and maximum is ~0U/2. */
5147 /* If (VAL & CST2) != VAL, X & CST2 can't be equal to
5149 minv
= masked_increment (valv
, cst2v
, sgnbit
, nprec
);
5153 maxv
= wi::mask (nprec
- (cst2n
? 1 : 0), false, nprec
);
5159 /* Find out smallest MINV where MINV > VAL
5160 && (MINV & CST2) == MINV, if any. If VAL is signed and
5161 CST2 has MSB set, compute it biased by 1 << (nprec - 1). */
5162 minv
= masked_increment (valv
, cst2v
, sgnbit
, nprec
);
5165 maxv
= wi::mask (nprec
- (cst2n
? 1 : 0), false, nprec
);
5170 /* Minimum unsigned value for <= is 0 and maximum
5171 unsigned value is VAL | ~CST2 if (VAL & CST2) == VAL.
5172 Otherwise, find smallest VAL2 where VAL2 > VAL
5173 && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
5175 For signed comparison, if CST2 doesn't have most
5176 significant bit set, handle it similarly. If CST2 has
5177 MSB set, the maximum is the same and minimum is INT_MIN. */
5182 maxv
= masked_increment (valv
, cst2v
, sgnbit
, nprec
);
5188 maxv
= wi::zext (maxv
, nprec
);
5195 /* Minimum unsigned value for < is 0 and maximum
5196 unsigned value is (VAL-1) | ~CST2 if (VAL & CST2) == VAL.
5197 Otherwise, find smallest VAL2 where VAL2 > VAL
5198 && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
5200 For signed comparison, if CST2 doesn't have most
5201 significant bit set, handle it similarly. If CST2 has
5202 MSB set, the maximum is the same and minimum is INT_MIN. */
5211 maxv
= masked_increment (valv
, cst2v
, sgnbit
, nprec
);
5217 maxv
= wi::zext (maxv
, nprec
);
5226 && wi::zext (maxv
- minv
, nprec
) != wi::minus_one (nprec
))
5228 tree tmp
, new_val
, type
;
5231 for (i
= 0; i
< 2; i
++)
5234 wide_int maxv2
= maxv
;
5236 type
= TREE_TYPE (names
[i
]);
5237 if (!TYPE_UNSIGNED (type
))
5239 type
= build_nonstandard_integer_type (nprec
, 1);
5240 tmp
= build1 (NOP_EXPR
, type
, names
[i
]);
5244 tmp
= build2 (PLUS_EXPR
, type
, tmp
,
5245 wide_int_to_tree (type
, -minv
));
5246 maxv2
= maxv
- minv
;
5248 new_val
= wide_int_to_tree (type
, maxv2
);
5252 fprintf (dump_file
, "Adding assert for ");
5253 print_generic_expr (dump_file
, names
[i
], 0);
5254 fprintf (dump_file
, " from ");
5255 print_generic_expr (dump_file
, tmp
, 0);
5256 fprintf (dump_file
, "\n");
5259 register_new_assert_for (names
[i
], tmp
, LE_EXPR
,
5260 new_val
, NULL
, e
, bsi
);
5270 /* OP is an operand of a truth value expression which is known to have
5271 a particular value. Register any asserts for OP and for any
5272 operands in OP's defining statement.
5274 If CODE is EQ_EXPR, then we want to register OP is zero (false),
5275 if CODE is NE_EXPR, then we want to register OP is nonzero (true). */
5278 register_edge_assert_for_1 (tree op
, enum tree_code code
,
5279 edge e
, gimple_stmt_iterator bsi
)
5281 bool retval
= false;
5284 enum tree_code rhs_code
;
5286 /* We only care about SSA_NAMEs. */
5287 if (TREE_CODE (op
) != SSA_NAME
)
5290 /* We know that OP will have a zero or nonzero value. If OP is used
5291 more than once go ahead and register an assert for OP.
5293 The FOUND_IN_SUBGRAPH support is not helpful in this situation as
5294 it will always be set for OP (because OP is used in a COND_EXPR in
5296 if (!has_single_use (op
))
5298 val
= build_int_cst (TREE_TYPE (op
), 0);
5299 register_new_assert_for (op
, op
, code
, val
, NULL
, e
, bsi
);
5303 /* Now look at how OP is set. If it's set from a comparison,
5304 a truth operation or some bit operations, then we may be able
5305 to register information about the operands of that assignment. */
5306 op_def
= SSA_NAME_DEF_STMT (op
);
5307 if (gimple_code (op_def
) != GIMPLE_ASSIGN
)
5310 rhs_code
= gimple_assign_rhs_code (op_def
);
5312 if (TREE_CODE_CLASS (rhs_code
) == tcc_comparison
)
5314 bool invert
= (code
== EQ_EXPR
? true : false);
5315 tree op0
= gimple_assign_rhs1 (op_def
);
5316 tree op1
= gimple_assign_rhs2 (op_def
);
5318 if (TREE_CODE (op0
) == SSA_NAME
)
5319 retval
|= register_edge_assert_for_2 (op0
, e
, bsi
, rhs_code
, op0
, op1
,
5321 if (TREE_CODE (op1
) == SSA_NAME
)
5322 retval
|= register_edge_assert_for_2 (op1
, e
, bsi
, rhs_code
, op0
, op1
,
5325 else if ((code
== NE_EXPR
5326 && gimple_assign_rhs_code (op_def
) == BIT_AND_EXPR
)
5328 && gimple_assign_rhs_code (op_def
) == BIT_IOR_EXPR
))
5330 /* Recurse on each operand. */
5331 tree op0
= gimple_assign_rhs1 (op_def
);
5332 tree op1
= gimple_assign_rhs2 (op_def
);
5333 if (TREE_CODE (op0
) == SSA_NAME
5334 && has_single_use (op0
))
5335 retval
|= register_edge_assert_for_1 (op0
, code
, e
, bsi
);
5336 if (TREE_CODE (op1
) == SSA_NAME
5337 && has_single_use (op1
))
5338 retval
|= register_edge_assert_for_1 (op1
, code
, e
, bsi
);
5340 else if (gimple_assign_rhs_code (op_def
) == BIT_NOT_EXPR
5341 && TYPE_PRECISION (TREE_TYPE (gimple_assign_lhs (op_def
))) == 1)
5343 /* Recurse, flipping CODE. */
5344 code
= invert_tree_comparison (code
, false);
5345 retval
|= register_edge_assert_for_1 (gimple_assign_rhs1 (op_def
),
5348 else if (gimple_assign_rhs_code (op_def
) == SSA_NAME
)
5350 /* Recurse through the copy. */
5351 retval
|= register_edge_assert_for_1 (gimple_assign_rhs1 (op_def
),
5354 else if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (op_def
)))
5356 /* Recurse through the type conversion. */
5357 retval
|= register_edge_assert_for_1 (gimple_assign_rhs1 (op_def
),
5364 /* Try to register an edge assertion for SSA name NAME on edge E for
5365 the condition COND contributing to the conditional jump pointed to by SI.
5366 Return true if an assertion for NAME could be registered. */
5369 register_edge_assert_for (tree name
, edge e
, gimple_stmt_iterator si
,
5370 enum tree_code cond_code
, tree cond_op0
,
5374 enum tree_code comp_code
;
5375 bool retval
= false;
5376 bool is_else_edge
= (e
->flags
& EDGE_FALSE_VALUE
) != 0;
5378 /* Do not attempt to infer anything in names that flow through
5380 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name
))
5383 if (!extract_code_and_val_from_cond_with_ops (name
, cond_code
,
5389 /* Register ASSERT_EXPRs for name. */
5390 retval
|= register_edge_assert_for_2 (name
, e
, si
, cond_code
, cond_op0
,
5391 cond_op1
, is_else_edge
);
5394 /* If COND is effectively an equality test of an SSA_NAME against
5395 the value zero or one, then we may be able to assert values
5396 for SSA_NAMEs which flow into COND. */
5398 /* In the case of NAME == 1 or NAME != 0, for BIT_AND_EXPR defining
5399 statement of NAME we can assert both operands of the BIT_AND_EXPR
5400 have nonzero value. */
5401 if (((comp_code
== EQ_EXPR
&& integer_onep (val
))
5402 || (comp_code
== NE_EXPR
&& integer_zerop (val
))))
5404 gimple def_stmt
= SSA_NAME_DEF_STMT (name
);
5406 if (is_gimple_assign (def_stmt
)
5407 && gimple_assign_rhs_code (def_stmt
) == BIT_AND_EXPR
)
5409 tree op0
= gimple_assign_rhs1 (def_stmt
);
5410 tree op1
= gimple_assign_rhs2 (def_stmt
);
5411 retval
|= register_edge_assert_for_1 (op0
, NE_EXPR
, e
, si
);
5412 retval
|= register_edge_assert_for_1 (op1
, NE_EXPR
, e
, si
);
5416 /* In the case of NAME == 0 or NAME != 1, for BIT_IOR_EXPR defining
5417 statement of NAME we can assert both operands of the BIT_IOR_EXPR
5419 if (((comp_code
== EQ_EXPR
&& integer_zerop (val
))
5420 || (comp_code
== NE_EXPR
&& integer_onep (val
))))
5422 gimple def_stmt
= SSA_NAME_DEF_STMT (name
);
5424 /* For BIT_IOR_EXPR only if NAME == 0 both operands have
5425 necessarily zero value, or if type-precision is one. */
5426 if (is_gimple_assign (def_stmt
)
5427 && (gimple_assign_rhs_code (def_stmt
) == BIT_IOR_EXPR
5428 && (TYPE_PRECISION (TREE_TYPE (name
)) == 1
5429 || comp_code
== EQ_EXPR
)))
5431 tree op0
= gimple_assign_rhs1 (def_stmt
);
5432 tree op1
= gimple_assign_rhs2 (def_stmt
);
5433 retval
|= register_edge_assert_for_1 (op0
, EQ_EXPR
, e
, si
);
5434 retval
|= register_edge_assert_for_1 (op1
, EQ_EXPR
, e
, si
);
5442 /* Determine whether the outgoing edges of BB should receive an
5443 ASSERT_EXPR for each of the operands of BB's LAST statement.
5444 The last statement of BB must be a COND_EXPR.
5446 If any of the sub-graphs rooted at BB have an interesting use of
5447 the predicate operands, an assert location node is added to the
5448 list of assertions for the corresponding operands. */
5451 find_conditional_asserts (basic_block bb
, gimple last
)
5454 gimple_stmt_iterator bsi
;
5460 need_assert
= false;
5461 bsi
= gsi_for_stmt (last
);
5463 /* Look for uses of the operands in each of the sub-graphs
5464 rooted at BB. We need to check each of the outgoing edges
5465 separately, so that we know what kind of ASSERT_EXPR to
5467 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
5472 /* Register the necessary assertions for each operand in the
5473 conditional predicate. */
5474 FOR_EACH_SSA_TREE_OPERAND (op
, last
, iter
, SSA_OP_USE
)
5476 need_assert
|= register_edge_assert_for (op
, e
, bsi
,
5477 gimple_cond_code (last
),
5478 gimple_cond_lhs (last
),
5479 gimple_cond_rhs (last
));
5492 /* Compare two case labels sorting first by the destination bb index
5493 and then by the case value. */
5496 compare_case_labels (const void *p1
, const void *p2
)
5498 const struct case_info
*ci1
= (const struct case_info
*) p1
;
5499 const struct case_info
*ci2
= (const struct case_info
*) p2
;
5500 int idx1
= ci1
->bb
->index
;
5501 int idx2
= ci2
->bb
->index
;
5505 else if (idx1
== idx2
)
5507 /* Make sure the default label is first in a group. */
5508 if (!CASE_LOW (ci1
->expr
))
5510 else if (!CASE_LOW (ci2
->expr
))
5513 return tree_int_cst_compare (CASE_LOW (ci1
->expr
),
5514 CASE_LOW (ci2
->expr
));
5520 /* Determine whether the outgoing edges of BB should receive an
5521 ASSERT_EXPR for each of the operands of BB's LAST statement.
5522 The last statement of BB must be a SWITCH_EXPR.
5524 If any of the sub-graphs rooted at BB have an interesting use of
5525 the predicate operands, an assert location node is added to the
5526 list of assertions for the corresponding operands. */
5529 find_switch_asserts (basic_block bb
, gimple last
)
5532 gimple_stmt_iterator bsi
;
5535 struct case_info
*ci
;
5536 size_t n
= gimple_switch_num_labels (last
);
5537 #if GCC_VERSION >= 4000
5540 /* Work around GCC 3.4 bug (PR 37086). */
5541 volatile unsigned int idx
;
5544 need_assert
= false;
5545 bsi
= gsi_for_stmt (last
);
5546 op
= gimple_switch_index (last
);
5547 if (TREE_CODE (op
) != SSA_NAME
)
5550 /* Build a vector of case labels sorted by destination label. */
5551 ci
= XNEWVEC (struct case_info
, n
);
5552 for (idx
= 0; idx
< n
; ++idx
)
5554 ci
[idx
].expr
= gimple_switch_label (last
, idx
);
5555 ci
[idx
].bb
= label_to_block (CASE_LABEL (ci
[idx
].expr
));
5557 qsort (ci
, n
, sizeof (struct case_info
), compare_case_labels
);
5559 for (idx
= 0; idx
< n
; ++idx
)
5562 tree cl
= ci
[idx
].expr
;
5563 basic_block cbb
= ci
[idx
].bb
;
5565 min
= CASE_LOW (cl
);
5566 max
= CASE_HIGH (cl
);
5568 /* If there are multiple case labels with the same destination
5569 we need to combine them to a single value range for the edge. */
5570 if (idx
+ 1 < n
&& cbb
== ci
[idx
+ 1].bb
)
5572 /* Skip labels until the last of the group. */
5575 } while (idx
< n
&& cbb
== ci
[idx
].bb
);
5578 /* Pick up the maximum of the case label range. */
5579 if (CASE_HIGH (ci
[idx
].expr
))
5580 max
= CASE_HIGH (ci
[idx
].expr
);
5582 max
= CASE_LOW (ci
[idx
].expr
);
5585 /* Nothing to do if the range includes the default label until we
5586 can register anti-ranges. */
5587 if (min
== NULL_TREE
)
5590 /* Find the edge to register the assert expr on. */
5591 e
= find_edge (bb
, cbb
);
5593 /* Register the necessary assertions for the operand in the
5595 need_assert
|= register_edge_assert_for (op
, e
, bsi
,
5596 max
? GE_EXPR
: EQ_EXPR
,
5598 fold_convert (TREE_TYPE (op
),
5602 need_assert
|= register_edge_assert_for (op
, e
, bsi
, LE_EXPR
,
5604 fold_convert (TREE_TYPE (op
),
5614 /* Traverse all the statements in block BB looking for statements that
5615 may generate useful assertions for the SSA names in their operand.
5616 If a statement produces a useful assertion A for name N_i, then the
5617 list of assertions already generated for N_i is scanned to
5618 determine if A is actually needed.
5620 If N_i already had the assertion A at a location dominating the
5621 current location, then nothing needs to be done. Otherwise, the
5622 new location for A is recorded instead.
5624 1- For every statement S in BB, all the variables used by S are
5625 added to bitmap FOUND_IN_SUBGRAPH.
5627 2- If statement S uses an operand N in a way that exposes a known
5628 value range for N, then if N was not already generated by an
5629 ASSERT_EXPR, create a new assert location for N. For instance,
5630 if N is a pointer and the statement dereferences it, we can
5631 assume that N is not NULL.
5633 3- COND_EXPRs are a special case of #2. We can derive range
5634 information from the predicate but need to insert different
5635 ASSERT_EXPRs for each of the sub-graphs rooted at the
5636 conditional block. If the last statement of BB is a conditional
5637 expression of the form 'X op Y', then
5639 a) Remove X and Y from the set FOUND_IN_SUBGRAPH.
5641 b) If the conditional is the only entry point to the sub-graph
5642 corresponding to the THEN_CLAUSE, recurse into it. On
5643 return, if X and/or Y are marked in FOUND_IN_SUBGRAPH, then
5644 an ASSERT_EXPR is added for the corresponding variable.
5646 c) Repeat step (b) on the ELSE_CLAUSE.
5648 d) Mark X and Y in FOUND_IN_SUBGRAPH.
5657 In this case, an assertion on the THEN clause is useful to
5658 determine that 'a' is always 9 on that edge. However, an assertion
5659 on the ELSE clause would be unnecessary.
5661 4- If BB does not end in a conditional expression, then we recurse
5662 into BB's dominator children.
5664 At the end of the recursive traversal, every SSA name will have a
5665 list of locations where ASSERT_EXPRs should be added. When a new
5666 location for name N is found, it is registered by calling
5667 register_new_assert_for. That function keeps track of all the
5668 registered assertions to prevent adding unnecessary assertions.
5669 For instance, if a pointer P_4 is dereferenced more than once in a
5670 dominator tree, only the location dominating all the dereference of
5671 P_4 will receive an ASSERT_EXPR.
5673 If this function returns true, then it means that there are names
5674 for which we need to generate ASSERT_EXPRs. Those assertions are
5675 inserted by process_assert_insertions. */
5678 find_assert_locations_1 (basic_block bb
, sbitmap live
)
5680 gimple_stmt_iterator si
;
5684 need_assert
= false;
5685 last
= last_stmt (bb
);
5687 /* If BB's last statement is a conditional statement involving integer
5688 operands, determine if we need to add ASSERT_EXPRs. */
5690 && gimple_code (last
) == GIMPLE_COND
5691 && !fp_predicate (last
)
5692 && !ZERO_SSA_OPERANDS (last
, SSA_OP_USE
))
5693 need_assert
|= find_conditional_asserts (bb
, last
);
5695 /* If BB's last statement is a switch statement involving integer
5696 operands, determine if we need to add ASSERT_EXPRs. */
5698 && gimple_code (last
) == GIMPLE_SWITCH
5699 && !ZERO_SSA_OPERANDS (last
, SSA_OP_USE
))
5700 need_assert
|= find_switch_asserts (bb
, last
);
5702 /* Traverse all the statements in BB marking used names and looking
5703 for statements that may infer assertions for their used operands. */
5704 for (si
= gsi_last_bb (bb
); !gsi_end_p (si
); gsi_prev (&si
))
5710 stmt
= gsi_stmt (si
);
5712 if (is_gimple_debug (stmt
))
5715 /* See if we can derive an assertion for any of STMT's operands. */
5716 FOR_EACH_SSA_TREE_OPERAND (op
, stmt
, i
, SSA_OP_USE
)
5719 enum tree_code comp_code
;
5721 /* If op is not live beyond this stmt, do not bother to insert
5723 if (!bitmap_bit_p (live
, SSA_NAME_VERSION (op
)))
5726 /* If OP is used in such a way that we can infer a value
5727 range for it, and we don't find a previous assertion for
5728 it, create a new assertion location node for OP. */
5729 if (infer_value_range (stmt
, op
, &comp_code
, &value
))
5731 /* If we are able to infer a nonzero value range for OP,
5732 then walk backwards through the use-def chain to see if OP
5733 was set via a typecast.
5735 If so, then we can also infer a nonzero value range
5736 for the operand of the NOP_EXPR. */
5737 if (comp_code
== NE_EXPR
&& integer_zerop (value
))
5740 gimple def_stmt
= SSA_NAME_DEF_STMT (t
);
5742 while (is_gimple_assign (def_stmt
)
5743 && gimple_assign_rhs_code (def_stmt
) == NOP_EXPR
5745 (gimple_assign_rhs1 (def_stmt
)) == SSA_NAME
5747 (TREE_TYPE (gimple_assign_rhs1 (def_stmt
))))
5749 t
= gimple_assign_rhs1 (def_stmt
);
5750 def_stmt
= SSA_NAME_DEF_STMT (t
);
5752 /* Note we want to register the assert for the
5753 operand of the NOP_EXPR after SI, not after the
5755 if (! has_single_use (t
))
5757 register_new_assert_for (t
, t
, comp_code
, value
,
5764 register_new_assert_for (op
, op
, comp_code
, value
, bb
, NULL
, si
);
5770 FOR_EACH_SSA_TREE_OPERAND (op
, stmt
, i
, SSA_OP_USE
)
5771 bitmap_set_bit (live
, SSA_NAME_VERSION (op
));
5772 FOR_EACH_SSA_TREE_OPERAND (op
, stmt
, i
, SSA_OP_DEF
)
5773 bitmap_clear_bit (live
, SSA_NAME_VERSION (op
));
5776 /* Traverse all PHI nodes in BB, updating live. */
5777 for (si
= gsi_start_phis (bb
); !gsi_end_p (si
); gsi_next (&si
))
5779 use_operand_p arg_p
;
5781 gimple phi
= gsi_stmt (si
);
5782 tree res
= gimple_phi_result (phi
);
5784 if (virtual_operand_p (res
))
5787 FOR_EACH_PHI_ARG (arg_p
, phi
, i
, SSA_OP_USE
)
5789 tree arg
= USE_FROM_PTR (arg_p
);
5790 if (TREE_CODE (arg
) == SSA_NAME
)
5791 bitmap_set_bit (live
, SSA_NAME_VERSION (arg
));
5794 bitmap_clear_bit (live
, SSA_NAME_VERSION (res
));
5800 /* Do an RPO walk over the function computing SSA name liveness
5801 on-the-fly and deciding on assert expressions to insert.
5802 Returns true if there are assert expressions to be inserted. */
5805 find_assert_locations (void)
5807 int *rpo
= XNEWVEC (int, last_basic_block
);
5808 int *bb_rpo
= XNEWVEC (int, last_basic_block
);
5809 int *last_rpo
= XCNEWVEC (int, last_basic_block
);
5813 live
= XCNEWVEC (sbitmap
, last_basic_block
);
5814 rpo_cnt
= pre_and_rev_post_order_compute (NULL
, rpo
, false);
5815 for (i
= 0; i
< rpo_cnt
; ++i
)
5818 /* Pre-seed loop latch liveness from loop header PHI nodes. Due to
5819 the order we compute liveness and insert asserts we otherwise
5820 fail to insert asserts into the loop latch. */
5822 FOR_EACH_LOOP (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
);