1 /* Support routines for Value Range Propagation (VRP).
2 Copyright (C) 2005-2014 Free Software Foundation, Inc.
3 Contributed by Diego Novillo <dnovillo@redhat.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
23 #include "coretypes.h"
27 #include "stor-layout.h"
34 #include "hard-reg-set.h"
37 #include "dominance.h"
40 #include "basic-block.h"
41 #include "tree-ssa-alias.h"
42 #include "internal-fn.h"
43 #include "gimple-fold.h"
45 #include "gimple-expr.h"
48 #include "gimple-iterator.h"
49 #include "gimple-walk.h"
50 #include "gimple-ssa.h"
52 #include "tree-phinodes.h"
53 #include "ssa-iterators.h"
54 #include "stringpool.h"
55 #include "tree-ssanames.h"
56 #include "tree-ssa-loop-manip.h"
57 #include "tree-ssa-loop-niter.h"
58 #include "tree-ssa-loop.h"
59 #include "tree-into-ssa.h"
61 #include "tree-pass.h"
62 #include "tree-dump.h"
63 #include "gimple-pretty-print.h"
64 #include "diagnostic-core.h"
67 #include "tree-scalar-evolution.h"
68 #include "tree-ssa-propagate.h"
69 #include "tree-chrec.h"
70 #include "tree-ssa-threadupdate.h"
73 #include "tree-ssa-threadedge.h"
78 /* Range of values that can be associated with an SSA_NAME after VRP
82 /* Lattice value represented by this range. */
83 enum value_range_type type
;
85 /* Minimum and maximum values represented by this range. These
86 values should be interpreted as follows:
88 - If TYPE is VR_UNDEFINED or VR_VARYING then MIN and MAX must
91 - If TYPE == VR_RANGE then MIN holds the minimum value and
92 MAX holds the maximum value of the range [MIN, MAX].
94 - If TYPE == ANTI_RANGE the variable is known to NOT
95 take any values in the range [MIN, MAX]. */
99 /* Set of SSA names whose value ranges are equivalent to this one.
100 This set is only valid when TYPE is VR_RANGE or VR_ANTI_RANGE. */
104 typedef struct value_range_d value_range_t
;
106 #define VR_INITIALIZER { VR_UNDEFINED, NULL_TREE, NULL_TREE, NULL }
108 /* Set of SSA names found live during the RPO traversal of the function
109 for still active basic-blocks. */
110 static sbitmap
*live
;
112 /* Return true if the SSA name NAME is live on the edge E. */
115 live_on_edge (edge e
, tree name
)
117 return (live
[e
->dest
->index
]
118 && bitmap_bit_p (live
[e
->dest
->index
], SSA_NAME_VERSION (name
)));
121 /* Local functions. */
122 static int compare_values (tree val1
, tree val2
);
123 static int compare_values_warnv (tree val1
, tree val2
, bool *);
124 static void vrp_meet (value_range_t
*, value_range_t
*);
125 static void vrp_intersect_ranges (value_range_t
*, value_range_t
*);
126 static tree
vrp_evaluate_conditional_warnv_with_ops (enum tree_code
,
127 tree
, tree
, bool, bool *,
130 /* Location information for ASSERT_EXPRs. Each instance of this
131 structure describes an ASSERT_EXPR for an SSA name. Since a single
132 SSA name may have more than one assertion associated with it, these
133 locations are kept in a linked list attached to the corresponding
135 struct assert_locus_d
137 /* Basic block where the assertion would be inserted. */
140 /* Some assertions need to be inserted on an edge (e.g., assertions
141 generated by COND_EXPRs). In those cases, BB will be NULL. */
144 /* Pointer to the statement that generated this assertion. */
145 gimple_stmt_iterator si
;
147 /* Predicate code for the ASSERT_EXPR. Must be COMPARISON_CLASS_P. */
148 enum tree_code comp_code
;
150 /* Value being compared against. */
153 /* Expression to compare. */
156 /* Next node in the linked list. */
157 struct assert_locus_d
*next
;
160 typedef struct assert_locus_d
*assert_locus_t
;
162 /* If bit I is present, it means that SSA name N_i has a list of
163 assertions that should be inserted in the IL. */
164 static bitmap need_assert_for
;
166 /* Array of locations lists where to insert assertions. ASSERTS_FOR[I]
167 holds a list of ASSERT_LOCUS_T nodes that describe where
168 ASSERT_EXPRs for SSA name N_I should be inserted. */
169 static assert_locus_t
*asserts_for
;
171 /* Value range array. After propagation, VR_VALUE[I] holds the range
172 of values that SSA name N_I may take. */
173 static unsigned num_vr_values
;
174 static value_range_t
**vr_value
;
175 static bool values_propagated
;
177 /* For a PHI node which sets SSA name N_I, VR_COUNTS[I] holds the
178 number of executable edges we saw the last time we visited the
180 static int *vr_phi_edge_counts
;
187 static vec
<edge
> to_remove_edges
;
188 static vec
<switch_update
> to_update_switch_stmts
;
191 /* Return the maximum value for TYPE. */
194 vrp_val_max (const_tree type
)
196 if (!INTEGRAL_TYPE_P (type
))
199 return TYPE_MAX_VALUE (type
);
202 /* Return the minimum value for TYPE. */
205 vrp_val_min (const_tree type
)
207 if (!INTEGRAL_TYPE_P (type
))
210 return TYPE_MIN_VALUE (type
);
213 /* Return whether VAL is equal to the maximum value of its type. This
214 will be true for a positive overflow infinity. We can't do a
215 simple equality comparison with TYPE_MAX_VALUE because C typedefs
216 and Ada subtypes can produce types whose TYPE_MAX_VALUE is not ==
217 to the integer constant with the same value in the type. */
220 vrp_val_is_max (const_tree val
)
222 tree type_max
= vrp_val_max (TREE_TYPE (val
));
223 return (val
== type_max
224 || (type_max
!= NULL_TREE
225 && operand_equal_p (val
, type_max
, 0)));
228 /* Return whether VAL is equal to the minimum value of its type. This
229 will be true for a negative overflow infinity. */
232 vrp_val_is_min (const_tree val
)
234 tree type_min
= vrp_val_min (TREE_TYPE (val
));
235 return (val
== type_min
236 || (type_min
!= NULL_TREE
237 && operand_equal_p (val
, type_min
, 0)));
241 /* Return whether TYPE should use an overflow infinity distinct from
242 TYPE_{MIN,MAX}_VALUE. We use an overflow infinity value to
243 represent a signed overflow during VRP computations. An infinity
244 is distinct from a half-range, which will go from some number to
245 TYPE_{MIN,MAX}_VALUE. */
248 needs_overflow_infinity (const_tree type
)
250 return INTEGRAL_TYPE_P (type
) && !TYPE_OVERFLOW_WRAPS (type
);
253 /* Return whether TYPE can support our overflow infinity
254 representation: we use the TREE_OVERFLOW flag, which only exists
255 for constants. If TYPE doesn't support this, we don't optimize
256 cases which would require signed overflow--we drop them to
260 supports_overflow_infinity (const_tree type
)
262 tree min
= vrp_val_min (type
), max
= vrp_val_max (type
);
263 #ifdef ENABLE_CHECKING
264 gcc_assert (needs_overflow_infinity (type
));
266 return (min
!= NULL_TREE
267 && CONSTANT_CLASS_P (min
)
269 && CONSTANT_CLASS_P (max
));
272 /* VAL is the maximum or minimum value of a type. Return a
273 corresponding overflow infinity. */
276 make_overflow_infinity (tree val
)
278 gcc_checking_assert (val
!= NULL_TREE
&& CONSTANT_CLASS_P (val
));
279 val
= copy_node (val
);
280 TREE_OVERFLOW (val
) = 1;
284 /* Return a negative overflow infinity for TYPE. */
287 negative_overflow_infinity (tree type
)
289 gcc_checking_assert (supports_overflow_infinity (type
));
290 return make_overflow_infinity (vrp_val_min (type
));
293 /* Return a positive overflow infinity for TYPE. */
296 positive_overflow_infinity (tree type
)
298 gcc_checking_assert (supports_overflow_infinity (type
));
299 return make_overflow_infinity (vrp_val_max (type
));
302 /* Return whether VAL is a negative overflow infinity. */
305 is_negative_overflow_infinity (const_tree val
)
307 return (TREE_OVERFLOW_P (val
)
308 && needs_overflow_infinity (TREE_TYPE (val
))
309 && vrp_val_is_min (val
));
312 /* Return whether VAL is a positive overflow infinity. */
315 is_positive_overflow_infinity (const_tree val
)
317 return (TREE_OVERFLOW_P (val
)
318 && needs_overflow_infinity (TREE_TYPE (val
))
319 && vrp_val_is_max (val
));
322 /* Return whether VAL is a positive or negative overflow infinity. */
325 is_overflow_infinity (const_tree val
)
327 return (TREE_OVERFLOW_P (val
)
328 && needs_overflow_infinity (TREE_TYPE (val
))
329 && (vrp_val_is_min (val
) || vrp_val_is_max (val
)));
332 /* Return whether STMT has a constant rhs that is_overflow_infinity. */
335 stmt_overflow_infinity (gimple stmt
)
337 if (is_gimple_assign (stmt
)
338 && get_gimple_rhs_class (gimple_assign_rhs_code (stmt
)) ==
340 return is_overflow_infinity (gimple_assign_rhs1 (stmt
));
344 /* If VAL is now an overflow infinity, return VAL. Otherwise, return
345 the same value with TREE_OVERFLOW clear. This can be used to avoid
346 confusing a regular value with an overflow value. */
349 avoid_overflow_infinity (tree val
)
351 if (!is_overflow_infinity (val
))
354 if (vrp_val_is_max (val
))
355 return vrp_val_max (TREE_TYPE (val
));
358 gcc_checking_assert (vrp_val_is_min (val
));
359 return vrp_val_min (TREE_TYPE (val
));
364 /* Return true if ARG is marked with the nonnull attribute in the
365 current function signature. */
368 nonnull_arg_p (const_tree arg
)
370 tree t
, attrs
, fntype
;
371 unsigned HOST_WIDE_INT arg_num
;
373 gcc_assert (TREE_CODE (arg
) == PARM_DECL
&& POINTER_TYPE_P (TREE_TYPE (arg
)));
375 /* The static chain decl is always non null. */
376 if (arg
== cfun
->static_chain_decl
)
379 fntype
= TREE_TYPE (current_function_decl
);
380 for (attrs
= TYPE_ATTRIBUTES (fntype
); attrs
; attrs
= TREE_CHAIN (attrs
))
382 attrs
= lookup_attribute ("nonnull", attrs
);
384 /* If "nonnull" wasn't specified, we know nothing about the argument. */
385 if (attrs
== NULL_TREE
)
388 /* If "nonnull" applies to all the arguments, then ARG is non-null. */
389 if (TREE_VALUE (attrs
) == NULL_TREE
)
392 /* Get the position number for ARG in the function signature. */
393 for (arg_num
= 1, t
= DECL_ARGUMENTS (current_function_decl
);
395 t
= DECL_CHAIN (t
), arg_num
++)
401 gcc_assert (t
== arg
);
403 /* Now see if ARG_NUM is mentioned in the nonnull list. */
404 for (t
= TREE_VALUE (attrs
); t
; t
= TREE_CHAIN (t
))
406 if (compare_tree_int (TREE_VALUE (t
), arg_num
) == 0)
415 /* Set value range VR to VR_UNDEFINED. */
418 set_value_range_to_undefined (value_range_t
*vr
)
420 vr
->type
= VR_UNDEFINED
;
421 vr
->min
= vr
->max
= NULL_TREE
;
423 bitmap_clear (vr
->equiv
);
427 /* Set value range VR to VR_VARYING. */
430 set_value_range_to_varying (value_range_t
*vr
)
432 vr
->type
= VR_VARYING
;
433 vr
->min
= vr
->max
= NULL_TREE
;
435 bitmap_clear (vr
->equiv
);
439 /* Set value range VR to {T, MIN, MAX, EQUIV}. */
442 set_value_range (value_range_t
*vr
, enum value_range_type t
, tree min
,
443 tree max
, bitmap equiv
)
445 #if defined ENABLE_CHECKING
446 /* Check the validity of the range. */
447 if (t
== VR_RANGE
|| t
== VR_ANTI_RANGE
)
451 gcc_assert (min
&& max
);
453 gcc_assert ((!TREE_OVERFLOW_P (min
) || is_overflow_infinity (min
))
454 && (!TREE_OVERFLOW_P (max
) || is_overflow_infinity (max
)));
456 if (INTEGRAL_TYPE_P (TREE_TYPE (min
)) && t
== VR_ANTI_RANGE
)
457 gcc_assert (!vrp_val_is_min (min
) || !vrp_val_is_max (max
));
459 cmp
= compare_values (min
, max
);
460 gcc_assert (cmp
== 0 || cmp
== -1 || cmp
== -2);
462 if (needs_overflow_infinity (TREE_TYPE (min
)))
463 gcc_assert (!is_overflow_infinity (min
)
464 || !is_overflow_infinity (max
));
467 if (t
== VR_UNDEFINED
|| t
== VR_VARYING
)
468 gcc_assert (min
== NULL_TREE
&& max
== NULL_TREE
);
470 if (t
== VR_UNDEFINED
|| t
== VR_VARYING
)
471 gcc_assert (equiv
== NULL
|| bitmap_empty_p (equiv
));
478 /* Since updating the equivalence set involves deep copying the
479 bitmaps, only do it if absolutely necessary. */
480 if (vr
->equiv
== NULL
482 vr
->equiv
= BITMAP_ALLOC (NULL
);
484 if (equiv
!= vr
->equiv
)
486 if (equiv
&& !bitmap_empty_p (equiv
))
487 bitmap_copy (vr
->equiv
, equiv
);
489 bitmap_clear (vr
->equiv
);
494 /* Set value range VR to the canonical form of {T, MIN, MAX, EQUIV}.
495 This means adjusting T, MIN and MAX representing the case of a
496 wrapping range with MAX < MIN covering [MIN, type_max] U [type_min, MAX]
497 as anti-rage ~[MAX+1, MIN-1]. Likewise for wrapping anti-ranges.
498 In corner cases where MAX+1 or MIN-1 wraps this will fall back
500 This routine exists to ease canonicalization in the case where we
501 extract ranges from var + CST op limit. */
504 set_and_canonicalize_value_range (value_range_t
*vr
, enum value_range_type t
,
505 tree min
, tree max
, bitmap equiv
)
507 /* Use the canonical setters for VR_UNDEFINED and VR_VARYING. */
508 if (t
== VR_UNDEFINED
)
510 set_value_range_to_undefined (vr
);
513 else if (t
== VR_VARYING
)
515 set_value_range_to_varying (vr
);
519 /* Nothing to canonicalize for symbolic ranges. */
520 if (TREE_CODE (min
) != INTEGER_CST
521 || TREE_CODE (max
) != INTEGER_CST
)
523 set_value_range (vr
, t
, min
, max
, equiv
);
527 /* Wrong order for min and max, to swap them and the VR type we need
529 if (tree_int_cst_lt (max
, min
))
533 /* For one bit precision if max < min, then the swapped
534 range covers all values, so for VR_RANGE it is varying and
535 for VR_ANTI_RANGE empty range, so drop to varying as well. */
536 if (TYPE_PRECISION (TREE_TYPE (min
)) == 1)
538 set_value_range_to_varying (vr
);
542 one
= build_int_cst (TREE_TYPE (min
), 1);
543 tmp
= int_const_binop (PLUS_EXPR
, max
, one
);
544 max
= int_const_binop (MINUS_EXPR
, min
, one
);
547 /* There's one corner case, if we had [C+1, C] before we now have
548 that again. But this represents an empty value range, so drop
549 to varying in this case. */
550 if (tree_int_cst_lt (max
, min
))
552 set_value_range_to_varying (vr
);
556 t
= t
== VR_RANGE
? VR_ANTI_RANGE
: VR_RANGE
;
559 /* Anti-ranges that can be represented as ranges should be so. */
560 if (t
== VR_ANTI_RANGE
)
562 bool is_min
= vrp_val_is_min (min
);
563 bool is_max
= vrp_val_is_max (max
);
565 if (is_min
&& is_max
)
567 /* We cannot deal with empty ranges, drop to varying.
568 ??? This could be VR_UNDEFINED instead. */
569 set_value_range_to_varying (vr
);
572 else if (TYPE_PRECISION (TREE_TYPE (min
)) == 1
573 && (is_min
|| is_max
))
575 /* Non-empty boolean ranges can always be represented
576 as a singleton range. */
578 min
= max
= vrp_val_max (TREE_TYPE (min
));
580 min
= max
= vrp_val_min (TREE_TYPE (min
));
584 /* As a special exception preserve non-null ranges. */
585 && !(TYPE_UNSIGNED (TREE_TYPE (min
))
586 && integer_zerop (max
)))
588 tree one
= build_int_cst (TREE_TYPE (max
), 1);
589 min
= int_const_binop (PLUS_EXPR
, max
, one
);
590 max
= vrp_val_max (TREE_TYPE (max
));
595 tree one
= build_int_cst (TREE_TYPE (min
), 1);
596 max
= int_const_binop (MINUS_EXPR
, min
, one
);
597 min
= vrp_val_min (TREE_TYPE (min
));
602 /* Drop [-INF(OVF), +INF(OVF)] to varying. */
603 if (needs_overflow_infinity (TREE_TYPE (min
))
604 && is_overflow_infinity (min
)
605 && is_overflow_infinity (max
))
607 set_value_range_to_varying (vr
);
611 set_value_range (vr
, t
, min
, max
, equiv
);
614 /* Copy value range FROM into value range TO. */
617 copy_value_range (value_range_t
*to
, value_range_t
*from
)
619 set_value_range (to
, from
->type
, from
->min
, from
->max
, from
->equiv
);
622 /* Set value range VR to a single value. This function is only called
623 with values we get from statements, and exists to clear the
624 TREE_OVERFLOW flag so that we don't think we have an overflow
625 infinity when we shouldn't. */
628 set_value_range_to_value (value_range_t
*vr
, tree val
, bitmap equiv
)
630 gcc_assert (is_gimple_min_invariant (val
));
631 if (TREE_OVERFLOW_P (val
))
632 val
= drop_tree_overflow (val
);
633 set_value_range (vr
, VR_RANGE
, val
, val
, equiv
);
636 /* Set value range VR to a non-negative range of type TYPE.
637 OVERFLOW_INFINITY indicates whether to use an overflow infinity
638 rather than TYPE_MAX_VALUE; this should be true if we determine
639 that the range is nonnegative based on the assumption that signed
640 overflow does not occur. */
643 set_value_range_to_nonnegative (value_range_t
*vr
, tree type
,
644 bool overflow_infinity
)
648 if (overflow_infinity
&& !supports_overflow_infinity (type
))
650 set_value_range_to_varying (vr
);
654 zero
= build_int_cst (type
, 0);
655 set_value_range (vr
, VR_RANGE
, zero
,
657 ? positive_overflow_infinity (type
)
658 : TYPE_MAX_VALUE (type
)),
662 /* Set value range VR to a non-NULL range of type TYPE. */
665 set_value_range_to_nonnull (value_range_t
*vr
, tree type
)
667 tree zero
= build_int_cst (type
, 0);
668 set_value_range (vr
, VR_ANTI_RANGE
, zero
, zero
, vr
->equiv
);
672 /* Set value range VR to a NULL range of type TYPE. */
675 set_value_range_to_null (value_range_t
*vr
, tree type
)
677 set_value_range_to_value (vr
, build_int_cst (type
, 0), vr
->equiv
);
681 /* Set value range VR to a range of a truthvalue of type TYPE. */
684 set_value_range_to_truthvalue (value_range_t
*vr
, tree type
)
686 if (TYPE_PRECISION (type
) == 1)
687 set_value_range_to_varying (vr
);
689 set_value_range (vr
, VR_RANGE
,
690 build_int_cst (type
, 0), build_int_cst (type
, 1),
695 /* If abs (min) < abs (max), set VR to [-max, max], if
696 abs (min) >= abs (max), set VR to [-min, min]. */
699 abs_extent_range (value_range_t
*vr
, tree min
, tree max
)
703 gcc_assert (TREE_CODE (min
) == INTEGER_CST
);
704 gcc_assert (TREE_CODE (max
) == INTEGER_CST
);
705 gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (min
)));
706 gcc_assert (!TYPE_UNSIGNED (TREE_TYPE (min
)));
707 min
= fold_unary (ABS_EXPR
, TREE_TYPE (min
), min
);
708 max
= fold_unary (ABS_EXPR
, TREE_TYPE (max
), max
);
709 if (TREE_OVERFLOW (min
) || TREE_OVERFLOW (max
))
711 set_value_range_to_varying (vr
);
714 cmp
= compare_values (min
, max
);
716 min
= fold_unary (NEGATE_EXPR
, TREE_TYPE (min
), max
);
717 else if (cmp
== 0 || cmp
== 1)
720 min
= fold_unary (NEGATE_EXPR
, TREE_TYPE (min
), min
);
724 set_value_range_to_varying (vr
);
727 set_and_canonicalize_value_range (vr
, VR_RANGE
, min
, max
, NULL
);
731 /* Return value range information for VAR.
733 If we have no values ranges recorded (ie, VRP is not running), then
734 return NULL. Otherwise create an empty range if none existed for VAR. */
736 static value_range_t
*
737 get_value_range (const_tree var
)
739 static const struct value_range_d vr_const_varying
740 = { VR_VARYING
, NULL_TREE
, NULL_TREE
, NULL
};
743 unsigned ver
= SSA_NAME_VERSION (var
);
745 /* If we have no recorded ranges, then return NULL. */
749 /* If we query the range for a new SSA name return an unmodifiable VARYING.
750 We should get here at most from the substitute-and-fold stage which
751 will never try to change values. */
752 if (ver
>= num_vr_values
)
753 return CONST_CAST (value_range_t
*, &vr_const_varying
);
759 /* After propagation finished do not allocate new value-ranges. */
760 if (values_propagated
)
761 return CONST_CAST (value_range_t
*, &vr_const_varying
);
763 /* Create a default value range. */
764 vr_value
[ver
] = vr
= XCNEW (value_range_t
);
766 /* Defer allocating the equivalence set. */
769 /* If VAR is a default definition of a parameter, the variable can
770 take any value in VAR's type. */
771 if (SSA_NAME_IS_DEFAULT_DEF (var
))
773 sym
= SSA_NAME_VAR (var
);
774 if (TREE_CODE (sym
) == PARM_DECL
)
776 /* Try to use the "nonnull" attribute to create ~[0, 0]
777 anti-ranges for pointers. Note that this is only valid with
778 default definitions of PARM_DECLs. */
779 if (POINTER_TYPE_P (TREE_TYPE (sym
))
780 && nonnull_arg_p (sym
))
781 set_value_range_to_nonnull (vr
, TREE_TYPE (sym
));
783 set_value_range_to_varying (vr
);
785 else if (TREE_CODE (sym
) == RESULT_DECL
786 && DECL_BY_REFERENCE (sym
))
787 set_value_range_to_nonnull (vr
, TREE_TYPE (sym
));
793 /* Return true, if VAL1 and VAL2 are equal values for VRP purposes. */
796 vrp_operand_equal_p (const_tree val1
, const_tree val2
)
800 if (!val1
|| !val2
|| !operand_equal_p (val1
, val2
, 0))
802 return is_overflow_infinity (val1
) == is_overflow_infinity (val2
);
805 /* Return true, if the bitmaps B1 and B2 are equal. */
808 vrp_bitmap_equal_p (const_bitmap b1
, const_bitmap b2
)
811 || ((!b1
|| bitmap_empty_p (b1
))
812 && (!b2
|| bitmap_empty_p (b2
)))
814 && bitmap_equal_p (b1
, b2
)));
817 /* Update the value range and equivalence set for variable VAR to
818 NEW_VR. Return true if NEW_VR is different from VAR's previous
821 NOTE: This function assumes that NEW_VR is a temporary value range
822 object created for the sole purpose of updating VAR's range. The
823 storage used by the equivalence set from NEW_VR will be freed by
824 this function. Do not call update_value_range when NEW_VR
825 is the range object associated with another SSA name. */
828 update_value_range (const_tree var
, value_range_t
*new_vr
)
830 value_range_t
*old_vr
;
833 /* Update the value range, if necessary. */
834 old_vr
= get_value_range (var
);
835 is_new
= old_vr
->type
!= new_vr
->type
836 || !vrp_operand_equal_p (old_vr
->min
, new_vr
->min
)
837 || !vrp_operand_equal_p (old_vr
->max
, new_vr
->max
)
838 || !vrp_bitmap_equal_p (old_vr
->equiv
, new_vr
->equiv
);
842 /* Do not allow transitions up the lattice. The following
843 is slightly more awkward than just new_vr->type < old_vr->type
844 because VR_RANGE and VR_ANTI_RANGE need to be considered
845 the same. We may not have is_new when transitioning to
846 UNDEFINED or from VARYING. */
847 if (new_vr
->type
== VR_UNDEFINED
848 || old_vr
->type
== VR_VARYING
)
849 set_value_range_to_varying (old_vr
);
851 set_value_range (old_vr
, new_vr
->type
, new_vr
->min
, new_vr
->max
,
855 BITMAP_FREE (new_vr
->equiv
);
861 /* Add VAR and VAR's equivalence set to EQUIV. This is the central
862 point where equivalence processing can be turned on/off. */
865 add_equivalence (bitmap
*equiv
, const_tree var
)
867 unsigned ver
= SSA_NAME_VERSION (var
);
868 value_range_t
*vr
= vr_value
[ver
];
871 *equiv
= BITMAP_ALLOC (NULL
);
872 bitmap_set_bit (*equiv
, ver
);
874 bitmap_ior_into (*equiv
, vr
->equiv
);
878 /* Return true if VR is ~[0, 0]. */
881 range_is_nonnull (value_range_t
*vr
)
883 return vr
->type
== VR_ANTI_RANGE
884 && integer_zerop (vr
->min
)
885 && integer_zerop (vr
->max
);
889 /* Return true if VR is [0, 0]. */
892 range_is_null (value_range_t
*vr
)
894 return vr
->type
== VR_RANGE
895 && integer_zerop (vr
->min
)
896 && integer_zerop (vr
->max
);
899 /* Return true if max and min of VR are INTEGER_CST. It's not necessary
903 range_int_cst_p (value_range_t
*vr
)
905 return (vr
->type
== VR_RANGE
906 && TREE_CODE (vr
->max
) == INTEGER_CST
907 && TREE_CODE (vr
->min
) == INTEGER_CST
);
910 /* Return true if VR is a INTEGER_CST singleton. */
913 range_int_cst_singleton_p (value_range_t
*vr
)
915 return (range_int_cst_p (vr
)
916 && !is_overflow_infinity (vr
->min
)
917 && !is_overflow_infinity (vr
->max
)
918 && tree_int_cst_equal (vr
->min
, vr
->max
));
921 /* Return true if value range VR involves at least one symbol. */
924 symbolic_range_p (value_range_t
*vr
)
926 return (!is_gimple_min_invariant (vr
->min
)
927 || !is_gimple_min_invariant (vr
->max
));
930 /* Return the single symbol (an SSA_NAME) contained in T if any, or NULL_TREE
931 otherwise. We only handle additive operations and set NEG to true if the
932 symbol is negated and INV to the invariant part, if any. */
935 get_single_symbol (tree t
, bool *neg
, tree
*inv
)
940 if (TREE_CODE (t
) == PLUS_EXPR
941 || TREE_CODE (t
) == POINTER_PLUS_EXPR
942 || TREE_CODE (t
) == MINUS_EXPR
)
944 if (is_gimple_min_invariant (TREE_OPERAND (t
, 0)))
946 neg_
= (TREE_CODE (t
) == MINUS_EXPR
);
947 inv_
= TREE_OPERAND (t
, 0);
948 t
= TREE_OPERAND (t
, 1);
950 else if (is_gimple_min_invariant (TREE_OPERAND (t
, 1)))
953 inv_
= TREE_OPERAND (t
, 1);
954 t
= TREE_OPERAND (t
, 0);
965 if (TREE_CODE (t
) == NEGATE_EXPR
)
967 t
= TREE_OPERAND (t
, 0);
971 if (TREE_CODE (t
) != SSA_NAME
)
979 /* The reverse operation: build a symbolic expression with TYPE
980 from symbol SYM, negated according to NEG, and invariant INV. */
983 build_symbolic_expr (tree type
, tree sym
, bool neg
, tree inv
)
985 const bool pointer_p
= POINTER_TYPE_P (type
);
989 t
= build1 (NEGATE_EXPR
, type
, t
);
991 if (integer_zerop (inv
))
994 return build2 (pointer_p
? POINTER_PLUS_EXPR
: PLUS_EXPR
, type
, t
, inv
);
997 /* Return true if value range VR involves exactly one symbol SYM. */
1000 symbolic_range_based_on_p (value_range_t
*vr
, const_tree sym
)
1002 bool neg
, min_has_symbol
, max_has_symbol
;
1005 if (is_gimple_min_invariant (vr
->min
))
1006 min_has_symbol
= false;
1007 else if (get_single_symbol (vr
->min
, &neg
, &inv
) == sym
)
1008 min_has_symbol
= true;
1012 if (is_gimple_min_invariant (vr
->max
))
1013 max_has_symbol
= false;
1014 else if (get_single_symbol (vr
->max
, &neg
, &inv
) == sym
)
1015 max_has_symbol
= true;
1019 return (min_has_symbol
|| max_has_symbol
);
1022 /* Return true if value range VR uses an overflow infinity. */
1025 overflow_infinity_range_p (value_range_t
*vr
)
1027 return (vr
->type
== VR_RANGE
1028 && (is_overflow_infinity (vr
->min
)
1029 || is_overflow_infinity (vr
->max
)));
1032 /* Return false if we can not make a valid comparison based on VR;
1033 this will be the case if it uses an overflow infinity and overflow
1034 is not undefined (i.e., -fno-strict-overflow is in effect).
1035 Otherwise return true, and set *STRICT_OVERFLOW_P to true if VR
1036 uses an overflow infinity. */
1039 usable_range_p (value_range_t
*vr
, bool *strict_overflow_p
)
1041 gcc_assert (vr
->type
== VR_RANGE
);
1042 if (is_overflow_infinity (vr
->min
))
1044 *strict_overflow_p
= true;
1045 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (vr
->min
)))
1048 if (is_overflow_infinity (vr
->max
))
1050 *strict_overflow_p
= true;
1051 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (vr
->max
)))
1058 /* Return true if the result of assignment STMT is know to be non-negative.
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_assign_nonnegative_warnv_p (gimple stmt
, bool *strict_overflow_p
)
1066 enum tree_code code
= gimple_assign_rhs_code (stmt
);
1067 switch (get_gimple_rhs_class (code
))
1069 case GIMPLE_UNARY_RHS
:
1070 return tree_unary_nonnegative_warnv_p (gimple_assign_rhs_code (stmt
),
1071 gimple_expr_type (stmt
),
1072 gimple_assign_rhs1 (stmt
),
1074 case GIMPLE_BINARY_RHS
:
1075 return tree_binary_nonnegative_warnv_p (gimple_assign_rhs_code (stmt
),
1076 gimple_expr_type (stmt
),
1077 gimple_assign_rhs1 (stmt
),
1078 gimple_assign_rhs2 (stmt
),
1080 case GIMPLE_TERNARY_RHS
:
1082 case GIMPLE_SINGLE_RHS
:
1083 return tree_single_nonnegative_warnv_p (gimple_assign_rhs1 (stmt
),
1085 case GIMPLE_INVALID_RHS
:
1092 /* Return true if return value of call STMT is know to be non-negative.
1093 If the return value is based on the assumption that signed overflow is
1094 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1095 *STRICT_OVERFLOW_P.*/
1098 gimple_call_nonnegative_warnv_p (gimple stmt
, bool *strict_overflow_p
)
1100 tree arg0
= gimple_call_num_args (stmt
) > 0 ?
1101 gimple_call_arg (stmt
, 0) : NULL_TREE
;
1102 tree arg1
= gimple_call_num_args (stmt
) > 1 ?
1103 gimple_call_arg (stmt
, 1) : NULL_TREE
;
1105 return tree_call_nonnegative_warnv_p (gimple_expr_type (stmt
),
1106 gimple_call_fndecl (stmt
),
1112 /* Return true if STMT is know to to compute a non-negative value.
1113 If the return value is based on the assumption that signed overflow is
1114 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1115 *STRICT_OVERFLOW_P.*/
1118 gimple_stmt_nonnegative_warnv_p (gimple stmt
, bool *strict_overflow_p
)
1120 switch (gimple_code (stmt
))
1123 return gimple_assign_nonnegative_warnv_p (stmt
, strict_overflow_p
);
1125 return gimple_call_nonnegative_warnv_p (stmt
, strict_overflow_p
);
1131 /* Return true if the result of assignment STMT is know to be non-zero.
1132 If the return value is based on the assumption that signed overflow is
1133 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1134 *STRICT_OVERFLOW_P.*/
1137 gimple_assign_nonzero_warnv_p (gimple stmt
, bool *strict_overflow_p
)
1139 enum tree_code code
= gimple_assign_rhs_code (stmt
);
1140 switch (get_gimple_rhs_class (code
))
1142 case GIMPLE_UNARY_RHS
:
1143 return tree_unary_nonzero_warnv_p (gimple_assign_rhs_code (stmt
),
1144 gimple_expr_type (stmt
),
1145 gimple_assign_rhs1 (stmt
),
1147 case GIMPLE_BINARY_RHS
:
1148 return tree_binary_nonzero_warnv_p (gimple_assign_rhs_code (stmt
),
1149 gimple_expr_type (stmt
),
1150 gimple_assign_rhs1 (stmt
),
1151 gimple_assign_rhs2 (stmt
),
1153 case GIMPLE_TERNARY_RHS
:
1155 case GIMPLE_SINGLE_RHS
:
1156 return tree_single_nonzero_warnv_p (gimple_assign_rhs1 (stmt
),
1158 case GIMPLE_INVALID_RHS
:
1165 /* Return true if STMT is known to compute a non-zero value.
1166 If the return value is based on the assumption that signed overflow is
1167 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1168 *STRICT_OVERFLOW_P.*/
1171 gimple_stmt_nonzero_warnv_p (gimple stmt
, bool *strict_overflow_p
)
1173 switch (gimple_code (stmt
))
1176 return gimple_assign_nonzero_warnv_p (stmt
, strict_overflow_p
);
1179 tree fndecl
= gimple_call_fndecl (stmt
);
1180 if (!fndecl
) return false;
1181 if (flag_delete_null_pointer_checks
&& !flag_check_new
1182 && DECL_IS_OPERATOR_NEW (fndecl
)
1183 && !TREE_NOTHROW (fndecl
))
1185 if (flag_delete_null_pointer_checks
&&
1186 lookup_attribute ("returns_nonnull",
1187 TYPE_ATTRIBUTES (gimple_call_fntype (stmt
))))
1189 return gimple_alloca_call_p (stmt
);
1196 /* Like tree_expr_nonzero_warnv_p, but this function uses value ranges
1200 vrp_stmt_computes_nonzero (gimple stmt
, bool *strict_overflow_p
)
1202 if (gimple_stmt_nonzero_warnv_p (stmt
, strict_overflow_p
))
1205 /* If we have an expression of the form &X->a, then the expression
1206 is nonnull if X is nonnull. */
1207 if (is_gimple_assign (stmt
)
1208 && gimple_assign_rhs_code (stmt
) == ADDR_EXPR
)
1210 tree expr
= gimple_assign_rhs1 (stmt
);
1211 tree base
= get_base_address (TREE_OPERAND (expr
, 0));
1213 if (base
!= NULL_TREE
1214 && TREE_CODE (base
) == MEM_REF
1215 && TREE_CODE (TREE_OPERAND (base
, 0)) == SSA_NAME
)
1217 value_range_t
*vr
= get_value_range (TREE_OPERAND (base
, 0));
1218 if (range_is_nonnull (vr
))
1226 /* Returns true if EXPR is a valid value (as expected by compare_values) --
1227 a gimple invariant, or SSA_NAME +- CST. */
1230 valid_value_p (tree expr
)
1232 if (TREE_CODE (expr
) == SSA_NAME
)
1235 if (TREE_CODE (expr
) == PLUS_EXPR
1236 || TREE_CODE (expr
) == MINUS_EXPR
)
1237 return (TREE_CODE (TREE_OPERAND (expr
, 0)) == SSA_NAME
1238 && TREE_CODE (TREE_OPERAND (expr
, 1)) == INTEGER_CST
);
1240 return is_gimple_min_invariant (expr
);
1246 -2 if those are incomparable. */
1248 operand_less_p (tree val
, tree val2
)
1250 /* LT is folded faster than GE and others. Inline the common case. */
1251 if (TREE_CODE (val
) == INTEGER_CST
&& TREE_CODE (val2
) == INTEGER_CST
)
1252 return tree_int_cst_lt (val
, val2
);
1257 fold_defer_overflow_warnings ();
1259 tcmp
= fold_binary_to_constant (LT_EXPR
, boolean_type_node
, val
, val2
);
1261 fold_undefer_and_ignore_overflow_warnings ();
1264 || TREE_CODE (tcmp
) != INTEGER_CST
)
1267 if (!integer_zerop (tcmp
))
1271 /* val >= val2, not considering overflow infinity. */
1272 if (is_negative_overflow_infinity (val
))
1273 return is_negative_overflow_infinity (val2
) ? 0 : 1;
1274 else if (is_positive_overflow_infinity (val2
))
1275 return is_positive_overflow_infinity (val
) ? 0 : 1;
1280 /* Compare two values VAL1 and VAL2. Return
1282 -2 if VAL1 and VAL2 cannot be compared at compile-time,
1285 +1 if VAL1 > VAL2, and
1288 This is similar to tree_int_cst_compare but supports pointer values
1289 and values that cannot be compared at compile time.
1291 If STRICT_OVERFLOW_P is not NULL, then set *STRICT_OVERFLOW_P to
1292 true if the return value is only valid if we assume that signed
1293 overflow is undefined. */
1296 compare_values_warnv (tree val1
, tree val2
, bool *strict_overflow_p
)
1301 /* Below we rely on the fact that VAL1 and VAL2 are both pointers or
1303 gcc_assert (POINTER_TYPE_P (TREE_TYPE (val1
))
1304 == POINTER_TYPE_P (TREE_TYPE (val2
)));
1306 /* Convert the two values into the same type. This is needed because
1307 sizetype causes sign extension even for unsigned types. */
1308 val2
= fold_convert (TREE_TYPE (val1
), val2
);
1309 STRIP_USELESS_TYPE_CONVERSION (val2
);
1311 if ((TREE_CODE (val1
) == SSA_NAME
1312 || (TREE_CODE (val1
) == NEGATE_EXPR
1313 && TREE_CODE (TREE_OPERAND (val1
, 0)) == SSA_NAME
)
1314 || TREE_CODE (val1
) == PLUS_EXPR
1315 || TREE_CODE (val1
) == MINUS_EXPR
)
1316 && (TREE_CODE (val2
) == SSA_NAME
1317 || (TREE_CODE (val2
) == NEGATE_EXPR
1318 && TREE_CODE (TREE_OPERAND (val2
, 0)) == SSA_NAME
)
1319 || TREE_CODE (val2
) == PLUS_EXPR
1320 || TREE_CODE (val2
) == MINUS_EXPR
))
1322 tree n1
, c1
, n2
, c2
;
1323 enum tree_code code1
, code2
;
1325 /* If VAL1 and VAL2 are of the form '[-]NAME [+-] CST' or 'NAME',
1326 return -1 or +1 accordingly. If VAL1 and VAL2 don't use the
1327 same name, return -2. */
1328 if (TREE_CODE (val1
) == SSA_NAME
|| TREE_CODE (val1
) == NEGATE_EXPR
)
1336 code1
= TREE_CODE (val1
);
1337 n1
= TREE_OPERAND (val1
, 0);
1338 c1
= TREE_OPERAND (val1
, 1);
1339 if (tree_int_cst_sgn (c1
) == -1)
1341 if (is_negative_overflow_infinity (c1
))
1343 c1
= fold_unary_to_constant (NEGATE_EXPR
, TREE_TYPE (c1
), c1
);
1346 code1
= code1
== MINUS_EXPR
? PLUS_EXPR
: MINUS_EXPR
;
1350 if (TREE_CODE (val2
) == SSA_NAME
|| TREE_CODE (val2
) == NEGATE_EXPR
)
1358 code2
= TREE_CODE (val2
);
1359 n2
= TREE_OPERAND (val2
, 0);
1360 c2
= TREE_OPERAND (val2
, 1);
1361 if (tree_int_cst_sgn (c2
) == -1)
1363 if (is_negative_overflow_infinity (c2
))
1365 c2
= fold_unary_to_constant (NEGATE_EXPR
, TREE_TYPE (c2
), c2
);
1368 code2
= code2
== MINUS_EXPR
? PLUS_EXPR
: MINUS_EXPR
;
1372 /* Both values must use the same name. */
1373 if (TREE_CODE (n1
) == NEGATE_EXPR
&& TREE_CODE (n2
) == NEGATE_EXPR
)
1375 n1
= TREE_OPERAND (n1
, 0);
1376 n2
= TREE_OPERAND (n2
, 0);
1381 if (code1
== SSA_NAME
&& code2
== SSA_NAME
)
1385 /* If overflow is defined we cannot simplify more. */
1386 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (val1
)))
1389 if (strict_overflow_p
!= NULL
1390 && (code1
== SSA_NAME
|| !TREE_NO_WARNING (val1
))
1391 && (code2
== SSA_NAME
|| !TREE_NO_WARNING (val2
)))
1392 *strict_overflow_p
= true;
1394 if (code1
== SSA_NAME
)
1396 if (code2
== PLUS_EXPR
)
1397 /* NAME < NAME + CST */
1399 else if (code2
== MINUS_EXPR
)
1400 /* NAME > NAME - CST */
1403 else if (code1
== PLUS_EXPR
)
1405 if (code2
== SSA_NAME
)
1406 /* NAME + CST > NAME */
1408 else if (code2
== PLUS_EXPR
)
1409 /* NAME + CST1 > NAME + CST2, if CST1 > CST2 */
1410 return compare_values_warnv (c1
, c2
, strict_overflow_p
);
1411 else if (code2
== MINUS_EXPR
)
1412 /* NAME + CST1 > NAME - CST2 */
1415 else if (code1
== MINUS_EXPR
)
1417 if (code2
== SSA_NAME
)
1418 /* NAME - CST < NAME */
1420 else if (code2
== PLUS_EXPR
)
1421 /* NAME - CST1 < NAME + CST2 */
1423 else if (code2
== MINUS_EXPR
)
1424 /* NAME - CST1 > NAME - CST2, if CST1 < CST2. Notice that
1425 C1 and C2 are swapped in the call to compare_values. */
1426 return compare_values_warnv (c2
, c1
, strict_overflow_p
);
1432 /* We cannot compare non-constants. */
1433 if (!is_gimple_min_invariant (val1
) || !is_gimple_min_invariant (val2
))
1436 if (!POINTER_TYPE_P (TREE_TYPE (val1
)))
1438 /* We cannot compare overflowed values, except for overflow
1440 if (TREE_OVERFLOW (val1
) || TREE_OVERFLOW (val2
))
1442 if (strict_overflow_p
!= NULL
)
1443 *strict_overflow_p
= true;
1444 if (is_negative_overflow_infinity (val1
))
1445 return is_negative_overflow_infinity (val2
) ? 0 : -1;
1446 else if (is_negative_overflow_infinity (val2
))
1448 else if (is_positive_overflow_infinity (val1
))
1449 return is_positive_overflow_infinity (val2
) ? 0 : 1;
1450 else if (is_positive_overflow_infinity (val2
))
1455 return tree_int_cst_compare (val1
, val2
);
1461 /* First see if VAL1 and VAL2 are not the same. */
1462 if (val1
== val2
|| operand_equal_p (val1
, val2
, 0))
1465 /* If VAL1 is a lower address than VAL2, return -1. */
1466 if (operand_less_p (val1
, val2
) == 1)
1469 /* If VAL1 is a higher address than VAL2, return +1. */
1470 if (operand_less_p (val2
, val1
) == 1)
1473 /* If VAL1 is different than VAL2, return +2.
1474 For integer constants we either have already returned -1 or 1
1475 or they are equivalent. We still might succeed in proving
1476 something about non-trivial operands. */
1477 if (TREE_CODE (val1
) != INTEGER_CST
1478 || TREE_CODE (val2
) != INTEGER_CST
)
1480 t
= fold_binary_to_constant (NE_EXPR
, boolean_type_node
, val1
, val2
);
1481 if (t
&& integer_onep (t
))
1489 /* Compare values like compare_values_warnv, but treat comparisons of
1490 nonconstants which rely on undefined overflow as incomparable. */
1493 compare_values (tree val1
, tree val2
)
1499 ret
= compare_values_warnv (val1
, val2
, &sop
);
1501 && (!is_gimple_min_invariant (val1
) || !is_gimple_min_invariant (val2
)))
1507 /* Return 1 if VAL is inside value range MIN <= VAL <= MAX,
1508 0 if VAL is not inside [MIN, MAX],
1509 -2 if we cannot tell either way.
1511 Benchmark compile/20001226-1.c compilation time after changing this
1515 value_inside_range (tree val
, tree min
, tree max
)
1519 cmp1
= operand_less_p (val
, min
);
1525 cmp2
= operand_less_p (max
, val
);
1533 /* Return true if value ranges VR0 and VR1 have a non-empty
1536 Benchmark compile/20001226-1.c compilation time after changing this
1541 value_ranges_intersect_p (value_range_t
*vr0
, value_range_t
*vr1
)
1543 /* The value ranges do not intersect if the maximum of the first range is
1544 less than the minimum of the second range or vice versa.
1545 When those relations are unknown, we can't do any better. */
1546 if (operand_less_p (vr0
->max
, vr1
->min
) != 0)
1548 if (operand_less_p (vr1
->max
, vr0
->min
) != 0)
1554 /* Return 1 if [MIN, MAX] includes the value zero, 0 if it does not
1555 include the value zero, -2 if we cannot tell. */
1558 range_includes_zero_p (tree min
, tree max
)
1560 tree zero
= build_int_cst (TREE_TYPE (min
), 0);
1561 return value_inside_range (zero
, min
, max
);
1564 /* Return true if *VR is know to only contain nonnegative values. */
1567 value_range_nonnegative_p (value_range_t
*vr
)
1569 /* Testing for VR_ANTI_RANGE is not useful here as any anti-range
1570 which would return a useful value should be encoded as a
1572 if (vr
->type
== VR_RANGE
)
1574 int result
= compare_values (vr
->min
, integer_zero_node
);
1575 return (result
== 0 || result
== 1);
1581 /* If *VR has a value rante that is a single constant value return that,
1582 otherwise return NULL_TREE. */
1585 value_range_constant_singleton (value_range_t
*vr
)
1587 if (vr
->type
== VR_RANGE
1588 && operand_equal_p (vr
->min
, vr
->max
, 0)
1589 && is_gimple_min_invariant (vr
->min
))
1595 /* If OP has a value range with a single constant value return that,
1596 otherwise return NULL_TREE. This returns OP itself if OP is a
1600 op_with_constant_singleton_value_range (tree op
)
1602 if (is_gimple_min_invariant (op
))
1605 if (TREE_CODE (op
) != SSA_NAME
)
1608 return value_range_constant_singleton (get_value_range (op
));
1611 /* Return true if op is in a boolean [0, 1] value-range. */
1614 op_with_boolean_value_range_p (tree op
)
1618 if (TYPE_PRECISION (TREE_TYPE (op
)) == 1)
1621 if (integer_zerop (op
)
1622 || integer_onep (op
))
1625 if (TREE_CODE (op
) != SSA_NAME
)
1628 vr
= get_value_range (op
);
1629 return (vr
->type
== VR_RANGE
1630 && integer_zerop (vr
->min
)
1631 && integer_onep (vr
->max
));
1634 /* Extract value range information from an ASSERT_EXPR EXPR and store
1638 extract_range_from_assert (value_range_t
*vr_p
, tree expr
)
1640 tree var
, cond
, limit
, min
, max
, type
;
1641 value_range_t
*limit_vr
;
1642 enum tree_code cond_code
;
1644 var
= ASSERT_EXPR_VAR (expr
);
1645 cond
= ASSERT_EXPR_COND (expr
);
1647 gcc_assert (COMPARISON_CLASS_P (cond
));
1649 /* Find VAR in the ASSERT_EXPR conditional. */
1650 if (var
== TREE_OPERAND (cond
, 0)
1651 || TREE_CODE (TREE_OPERAND (cond
, 0)) == PLUS_EXPR
1652 || TREE_CODE (TREE_OPERAND (cond
, 0)) == NOP_EXPR
)
1654 /* If the predicate is of the form VAR COMP LIMIT, then we just
1655 take LIMIT from the RHS and use the same comparison code. */
1656 cond_code
= TREE_CODE (cond
);
1657 limit
= TREE_OPERAND (cond
, 1);
1658 cond
= TREE_OPERAND (cond
, 0);
1662 /* If the predicate is of the form LIMIT COMP VAR, then we need
1663 to flip around the comparison code to create the proper range
1665 cond_code
= swap_tree_comparison (TREE_CODE (cond
));
1666 limit
= TREE_OPERAND (cond
, 0);
1667 cond
= TREE_OPERAND (cond
, 1);
1670 limit
= avoid_overflow_infinity (limit
);
1672 type
= TREE_TYPE (var
);
1673 gcc_assert (limit
!= var
);
1675 /* For pointer arithmetic, we only keep track of pointer equality
1677 if (POINTER_TYPE_P (type
) && cond_code
!= NE_EXPR
&& cond_code
!= EQ_EXPR
)
1679 set_value_range_to_varying (vr_p
);
1683 /* If LIMIT is another SSA name and LIMIT has a range of its own,
1684 try to use LIMIT's range to avoid creating symbolic ranges
1686 limit_vr
= (TREE_CODE (limit
) == SSA_NAME
) ? get_value_range (limit
) : NULL
;
1688 /* LIMIT's range is only interesting if it has any useful information. */
1690 && (limit_vr
->type
== VR_UNDEFINED
1691 || limit_vr
->type
== VR_VARYING
1692 || symbolic_range_p (limit_vr
)))
1695 /* Initially, the new range has the same set of equivalences of
1696 VAR's range. This will be revised before returning the final
1697 value. Since assertions may be chained via mutually exclusive
1698 predicates, we will need to trim the set of equivalences before
1700 gcc_assert (vr_p
->equiv
== NULL
);
1701 add_equivalence (&vr_p
->equiv
, var
);
1703 /* Extract a new range based on the asserted comparison for VAR and
1704 LIMIT's value range. Notice that if LIMIT has an anti-range, we
1705 will only use it for equality comparisons (EQ_EXPR). For any
1706 other kind of assertion, we cannot derive a range from LIMIT's
1707 anti-range that can be used to describe the new range. For
1708 instance, ASSERT_EXPR <x_2, x_2 <= b_4>. If b_4 is ~[2, 10],
1709 then b_4 takes on the ranges [-INF, 1] and [11, +INF]. There is
1710 no single range for x_2 that could describe LE_EXPR, so we might
1711 as well build the range [b_4, +INF] for it.
1712 One special case we handle is extracting a range from a
1713 range test encoded as (unsigned)var + CST <= limit. */
1714 if (TREE_CODE (cond
) == NOP_EXPR
1715 || TREE_CODE (cond
) == PLUS_EXPR
)
1717 if (TREE_CODE (cond
) == PLUS_EXPR
)
1719 min
= fold_build1 (NEGATE_EXPR
, TREE_TYPE (TREE_OPERAND (cond
, 1)),
1720 TREE_OPERAND (cond
, 1));
1721 max
= int_const_binop (PLUS_EXPR
, limit
, min
);
1722 cond
= TREE_OPERAND (cond
, 0);
1726 min
= build_int_cst (TREE_TYPE (var
), 0);
1730 /* Make sure to not set TREE_OVERFLOW on the final type
1731 conversion. We are willingly interpreting large positive
1732 unsigned values as negative signed values here. */
1733 min
= force_fit_type (TREE_TYPE (var
), wi::to_widest (min
), 0, false);
1734 max
= force_fit_type (TREE_TYPE (var
), wi::to_widest (max
), 0, false);
1736 /* We can transform a max, min range to an anti-range or
1737 vice-versa. Use set_and_canonicalize_value_range which does
1739 if (cond_code
== LE_EXPR
)
1740 set_and_canonicalize_value_range (vr_p
, VR_RANGE
,
1741 min
, max
, vr_p
->equiv
);
1742 else if (cond_code
== GT_EXPR
)
1743 set_and_canonicalize_value_range (vr_p
, VR_ANTI_RANGE
,
1744 min
, max
, vr_p
->equiv
);
1748 else if (cond_code
== EQ_EXPR
)
1750 enum value_range_type range_type
;
1754 range_type
= limit_vr
->type
;
1755 min
= limit_vr
->min
;
1756 max
= limit_vr
->max
;
1760 range_type
= VR_RANGE
;
1765 set_value_range (vr_p
, range_type
, min
, max
, vr_p
->equiv
);
1767 /* When asserting the equality VAR == LIMIT and LIMIT is another
1768 SSA name, the new range will also inherit the equivalence set
1770 if (TREE_CODE (limit
) == SSA_NAME
)
1771 add_equivalence (&vr_p
->equiv
, limit
);
1773 else if (cond_code
== NE_EXPR
)
1775 /* As described above, when LIMIT's range is an anti-range and
1776 this assertion is an inequality (NE_EXPR), then we cannot
1777 derive anything from the anti-range. For instance, if
1778 LIMIT's range was ~[0, 0], the assertion 'VAR != LIMIT' does
1779 not imply that VAR's range is [0, 0]. So, in the case of
1780 anti-ranges, we just assert the inequality using LIMIT and
1783 If LIMIT_VR is a range, we can only use it to build a new
1784 anti-range if LIMIT_VR is a single-valued range. For
1785 instance, if LIMIT_VR is [0, 1], the predicate
1786 VAR != [0, 1] does not mean that VAR's range is ~[0, 1].
1787 Rather, it means that for value 0 VAR should be ~[0, 0]
1788 and for value 1, VAR should be ~[1, 1]. We cannot
1789 represent these ranges.
1791 The only situation in which we can build a valid
1792 anti-range is when LIMIT_VR is a single-valued range
1793 (i.e., LIMIT_VR->MIN == LIMIT_VR->MAX). In that case,
1794 build the anti-range ~[LIMIT_VR->MIN, LIMIT_VR->MAX]. */
1796 && limit_vr
->type
== VR_RANGE
1797 && compare_values (limit_vr
->min
, limit_vr
->max
) == 0)
1799 min
= limit_vr
->min
;
1800 max
= limit_vr
->max
;
1804 /* In any other case, we cannot use LIMIT's range to build a
1805 valid anti-range. */
1809 /* If MIN and MAX cover the whole range for their type, then
1810 just use the original LIMIT. */
1811 if (INTEGRAL_TYPE_P (type
)
1812 && vrp_val_is_min (min
)
1813 && vrp_val_is_max (max
))
1816 set_and_canonicalize_value_range (vr_p
, VR_ANTI_RANGE
,
1817 min
, max
, vr_p
->equiv
);
1819 else if (cond_code
== LE_EXPR
|| cond_code
== LT_EXPR
)
1821 min
= TYPE_MIN_VALUE (type
);
1823 if (limit_vr
== NULL
|| limit_vr
->type
== VR_ANTI_RANGE
)
1827 /* If LIMIT_VR is of the form [N1, N2], we need to build the
1828 range [MIN, N2] for LE_EXPR and [MIN, N2 - 1] for
1830 max
= limit_vr
->max
;
1833 /* If the maximum value forces us to be out of bounds, simply punt.
1834 It would be pointless to try and do anything more since this
1835 all should be optimized away above us. */
1836 if ((cond_code
== LT_EXPR
1837 && compare_values (max
, min
) == 0)
1838 || is_overflow_infinity (max
))
1839 set_value_range_to_varying (vr_p
);
1842 /* For LT_EXPR, we create the range [MIN, MAX - 1]. */
1843 if (cond_code
== LT_EXPR
)
1845 if (TYPE_PRECISION (TREE_TYPE (max
)) == 1
1846 && !TYPE_UNSIGNED (TREE_TYPE (max
)))
1847 max
= fold_build2 (PLUS_EXPR
, TREE_TYPE (max
), max
,
1848 build_int_cst (TREE_TYPE (max
), -1));
1850 max
= fold_build2 (MINUS_EXPR
, TREE_TYPE (max
), max
,
1851 build_int_cst (TREE_TYPE (max
), 1));
1853 TREE_NO_WARNING (max
) = 1;
1856 set_value_range (vr_p
, VR_RANGE
, min
, max
, vr_p
->equiv
);
1859 else if (cond_code
== GE_EXPR
|| cond_code
== GT_EXPR
)
1861 max
= TYPE_MAX_VALUE (type
);
1863 if (limit_vr
== NULL
|| limit_vr
->type
== VR_ANTI_RANGE
)
1867 /* If LIMIT_VR is of the form [N1, N2], we need to build the
1868 range [N1, MAX] for GE_EXPR and [N1 + 1, MAX] for
1870 min
= limit_vr
->min
;
1873 /* If the minimum value forces us to be out of bounds, simply punt.
1874 It would be pointless to try and do anything more since this
1875 all should be optimized away above us. */
1876 if ((cond_code
== GT_EXPR
1877 && compare_values (min
, max
) == 0)
1878 || is_overflow_infinity (min
))
1879 set_value_range_to_varying (vr_p
);
1882 /* For GT_EXPR, we create the range [MIN + 1, MAX]. */
1883 if (cond_code
== GT_EXPR
)
1885 if (TYPE_PRECISION (TREE_TYPE (min
)) == 1
1886 && !TYPE_UNSIGNED (TREE_TYPE (min
)))
1887 min
= fold_build2 (MINUS_EXPR
, TREE_TYPE (min
), min
,
1888 build_int_cst (TREE_TYPE (min
), -1));
1890 min
= fold_build2 (PLUS_EXPR
, TREE_TYPE (min
), min
,
1891 build_int_cst (TREE_TYPE (min
), 1));
1893 TREE_NO_WARNING (min
) = 1;
1896 set_value_range (vr_p
, VR_RANGE
, min
, max
, vr_p
->equiv
);
1902 /* Finally intersect the new range with what we already know about var. */
1903 vrp_intersect_ranges (vr_p
, get_value_range (var
));
1907 /* Extract range information from SSA name VAR and store it in VR. If
1908 VAR has an interesting range, use it. Otherwise, create the
1909 range [VAR, VAR] and return it. This is useful in situations where
1910 we may have conditionals testing values of VARYING names. For
1917 Even if y_5 is deemed VARYING, we can determine that x_3 > y_5 is
1921 extract_range_from_ssa_name (value_range_t
*vr
, tree var
)
1923 value_range_t
*var_vr
= get_value_range (var
);
1925 if (var_vr
->type
!= VR_VARYING
)
1926 copy_value_range (vr
, var_vr
);
1928 set_value_range (vr
, VR_RANGE
, var
, var
, NULL
);
1930 add_equivalence (&vr
->equiv
, var
);
1934 /* Wrapper around int_const_binop. If the operation overflows and we
1935 are not using wrapping arithmetic, then adjust the result to be
1936 -INF or +INF depending on CODE, VAL1 and VAL2. This can return
1937 NULL_TREE if we need to use an overflow infinity representation but
1938 the type does not support it. */
1941 vrp_int_const_binop (enum tree_code code
, tree val1
, tree val2
)
1945 res
= int_const_binop (code
, val1
, val2
);
1947 /* If we are using unsigned arithmetic, operate symbolically
1948 on -INF and +INF as int_const_binop only handles signed overflow. */
1949 if (TYPE_UNSIGNED (TREE_TYPE (val1
)))
1951 int checkz
= compare_values (res
, val1
);
1952 bool overflow
= false;
1954 /* Ensure that res = val1 [+*] val2 >= val1
1955 or that res = val1 - val2 <= val1. */
1956 if ((code
== PLUS_EXPR
1957 && !(checkz
== 1 || checkz
== 0))
1958 || (code
== MINUS_EXPR
1959 && !(checkz
== 0 || checkz
== -1)))
1963 /* Checking for multiplication overflow is done by dividing the
1964 output of the multiplication by the first input of the
1965 multiplication. If the result of that division operation is
1966 not equal to the second input of the multiplication, then the
1967 multiplication overflowed. */
1968 else if (code
== MULT_EXPR
&& !integer_zerop (val1
))
1970 tree tmp
= int_const_binop (TRUNC_DIV_EXPR
,
1973 int check
= compare_values (tmp
, val2
);
1981 res
= copy_node (res
);
1982 TREE_OVERFLOW (res
) = 1;
1986 else if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (val1
)))
1987 /* If the singed operation wraps then int_const_binop has done
1988 everything we want. */
1990 /* Signed division of -1/0 overflows and by the time it gets here
1991 returns NULL_TREE. */
1994 else if ((TREE_OVERFLOW (res
)
1995 && !TREE_OVERFLOW (val1
)
1996 && !TREE_OVERFLOW (val2
))
1997 || is_overflow_infinity (val1
)
1998 || is_overflow_infinity (val2
))
2000 /* If the operation overflowed but neither VAL1 nor VAL2 are
2001 overflown, return -INF or +INF depending on the operation
2002 and the combination of signs of the operands. */
2003 int sgn1
= tree_int_cst_sgn (val1
);
2004 int sgn2
= tree_int_cst_sgn (val2
);
2006 if (needs_overflow_infinity (TREE_TYPE (res
))
2007 && !supports_overflow_infinity (TREE_TYPE (res
)))
2010 /* We have to punt on adding infinities of different signs,
2011 since we can't tell what the sign of the result should be.
2012 Likewise for subtracting infinities of the same sign. */
2013 if (((code
== PLUS_EXPR
&& sgn1
!= sgn2
)
2014 || (code
== MINUS_EXPR
&& sgn1
== sgn2
))
2015 && is_overflow_infinity (val1
)
2016 && is_overflow_infinity (val2
))
2019 /* Don't try to handle division or shifting of infinities. */
2020 if ((code
== TRUNC_DIV_EXPR
2021 || code
== FLOOR_DIV_EXPR
2022 || code
== CEIL_DIV_EXPR
2023 || code
== EXACT_DIV_EXPR
2024 || code
== ROUND_DIV_EXPR
2025 || code
== RSHIFT_EXPR
)
2026 && (is_overflow_infinity (val1
)
2027 || is_overflow_infinity (val2
)))
2030 /* Notice that we only need to handle the restricted set of
2031 operations handled by extract_range_from_binary_expr.
2032 Among them, only multiplication, addition and subtraction
2033 can yield overflow without overflown operands because we
2034 are working with integral types only... except in the
2035 case VAL1 = -INF and VAL2 = -1 which overflows to +INF
2036 for division too. */
2038 /* For multiplication, the sign of the overflow is given
2039 by the comparison of the signs of the operands. */
2040 if ((code
== MULT_EXPR
&& sgn1
== sgn2
)
2041 /* For addition, the operands must be of the same sign
2042 to yield an overflow. Its sign is therefore that
2043 of one of the operands, for example the first. For
2044 infinite operands X + -INF is negative, not positive. */
2045 || (code
== PLUS_EXPR
2047 ? !is_negative_overflow_infinity (val2
)
2048 : is_positive_overflow_infinity (val2
)))
2049 /* For subtraction, non-infinite operands must be of
2050 different signs to yield an overflow. Its sign is
2051 therefore that of the first operand or the opposite of
2052 that of the second operand. A first operand of 0 counts
2053 as positive here, for the corner case 0 - (-INF), which
2054 overflows, but must yield +INF. For infinite operands 0
2055 - INF is negative, not positive. */
2056 || (code
== MINUS_EXPR
2058 ? !is_positive_overflow_infinity (val2
)
2059 : is_negative_overflow_infinity (val2
)))
2060 /* We only get in here with positive shift count, so the
2061 overflow direction is the same as the sign of val1.
2062 Actually rshift does not overflow at all, but we only
2063 handle the case of shifting overflowed -INF and +INF. */
2064 || (code
== RSHIFT_EXPR
2066 /* For division, the only case is -INF / -1 = +INF. */
2067 || code
== TRUNC_DIV_EXPR
2068 || code
== FLOOR_DIV_EXPR
2069 || code
== CEIL_DIV_EXPR
2070 || code
== EXACT_DIV_EXPR
2071 || code
== ROUND_DIV_EXPR
)
2072 return (needs_overflow_infinity (TREE_TYPE (res
))
2073 ? positive_overflow_infinity (TREE_TYPE (res
))
2074 : TYPE_MAX_VALUE (TREE_TYPE (res
)));
2076 return (needs_overflow_infinity (TREE_TYPE (res
))
2077 ? negative_overflow_infinity (TREE_TYPE (res
))
2078 : TYPE_MIN_VALUE (TREE_TYPE (res
)));
2085 /* For range VR compute two wide_int bitmasks. In *MAY_BE_NONZERO
2086 bitmask if some bit is unset, it means for all numbers in the range
2087 the bit is 0, otherwise it might be 0 or 1. In *MUST_BE_NONZERO
2088 bitmask if some bit is set, it means for all numbers in the range
2089 the bit is 1, otherwise it might be 0 or 1. */
2092 zero_nonzero_bits_from_vr (const tree expr_type
,
2094 wide_int
*may_be_nonzero
,
2095 wide_int
*must_be_nonzero
)
2097 *may_be_nonzero
= wi::minus_one (TYPE_PRECISION (expr_type
));
2098 *must_be_nonzero
= wi::zero (TYPE_PRECISION (expr_type
));
2099 if (!range_int_cst_p (vr
)
2100 || is_overflow_infinity (vr
->min
)
2101 || is_overflow_infinity (vr
->max
))
2104 if (range_int_cst_singleton_p (vr
))
2106 *may_be_nonzero
= vr
->min
;
2107 *must_be_nonzero
= *may_be_nonzero
;
2109 else if (tree_int_cst_sgn (vr
->min
) >= 0
2110 || tree_int_cst_sgn (vr
->max
) < 0)
2112 wide_int xor_mask
= wi::bit_xor (vr
->min
, vr
->max
);
2113 *may_be_nonzero
= wi::bit_or (vr
->min
, vr
->max
);
2114 *must_be_nonzero
= wi::bit_and (vr
->min
, vr
->max
);
2117 wide_int mask
= wi::mask (wi::floor_log2 (xor_mask
), false,
2118 may_be_nonzero
->get_precision ());
2119 *may_be_nonzero
= *may_be_nonzero
| mask
;
2120 *must_be_nonzero
= must_be_nonzero
->and_not (mask
);
2127 /* Create two value-ranges in *VR0 and *VR1 from the anti-range *AR
2128 so that *VR0 U *VR1 == *AR. Returns true if that is possible,
2129 false otherwise. If *AR can be represented with a single range
2130 *VR1 will be VR_UNDEFINED. */
2133 ranges_from_anti_range (value_range_t
*ar
,
2134 value_range_t
*vr0
, value_range_t
*vr1
)
2136 tree type
= TREE_TYPE (ar
->min
);
2138 vr0
->type
= VR_UNDEFINED
;
2139 vr1
->type
= VR_UNDEFINED
;
2141 if (ar
->type
!= VR_ANTI_RANGE
2142 || TREE_CODE (ar
->min
) != INTEGER_CST
2143 || TREE_CODE (ar
->max
) != INTEGER_CST
2144 || !vrp_val_min (type
)
2145 || !vrp_val_max (type
))
2148 if (!vrp_val_is_min (ar
->min
))
2150 vr0
->type
= VR_RANGE
;
2151 vr0
->min
= vrp_val_min (type
);
2152 vr0
->max
= wide_int_to_tree (type
, wi::sub (ar
->min
, 1));
2154 if (!vrp_val_is_max (ar
->max
))
2156 vr1
->type
= VR_RANGE
;
2157 vr1
->min
= wide_int_to_tree (type
, wi::add (ar
->max
, 1));
2158 vr1
->max
= vrp_val_max (type
);
2160 if (vr0
->type
== VR_UNDEFINED
)
2163 vr1
->type
= VR_UNDEFINED
;
2166 return vr0
->type
!= VR_UNDEFINED
;
2169 /* Helper to extract a value-range *VR for a multiplicative operation
2173 extract_range_from_multiplicative_op_1 (value_range_t
*vr
,
2174 enum tree_code code
,
2175 value_range_t
*vr0
, value_range_t
*vr1
)
2177 enum value_range_type type
;
2184 /* Multiplications, divisions and shifts are a bit tricky to handle,
2185 depending on the mix of signs we have in the two ranges, we
2186 need to operate on different values to get the minimum and
2187 maximum values for the new range. One approach is to figure
2188 out all the variations of range combinations and do the
2191 However, this involves several calls to compare_values and it
2192 is pretty convoluted. It's simpler to do the 4 operations
2193 (MIN0 OP MIN1, MIN0 OP MAX1, MAX0 OP MIN1 and MAX0 OP MAX0 OP
2194 MAX1) and then figure the smallest and largest values to form
2196 gcc_assert (code
== MULT_EXPR
2197 || code
== TRUNC_DIV_EXPR
2198 || code
== FLOOR_DIV_EXPR
2199 || code
== CEIL_DIV_EXPR
2200 || code
== EXACT_DIV_EXPR
2201 || code
== ROUND_DIV_EXPR
2202 || code
== RSHIFT_EXPR
2203 || code
== LSHIFT_EXPR
);
2204 gcc_assert ((vr0
->type
== VR_RANGE
2205 || (code
== MULT_EXPR
&& vr0
->type
== VR_ANTI_RANGE
))
2206 && vr0
->type
== vr1
->type
);
2210 /* Compute the 4 cross operations. */
2212 val
[0] = vrp_int_const_binop (code
, vr0
->min
, vr1
->min
);
2213 if (val
[0] == NULL_TREE
)
2216 if (vr1
->max
== vr1
->min
)
2220 val
[1] = vrp_int_const_binop (code
, vr0
->min
, vr1
->max
);
2221 if (val
[1] == NULL_TREE
)
2225 if (vr0
->max
== vr0
->min
)
2229 val
[2] = vrp_int_const_binop (code
, vr0
->max
, vr1
->min
);
2230 if (val
[2] == NULL_TREE
)
2234 if (vr0
->min
== vr0
->max
|| vr1
->min
== vr1
->max
)
2238 val
[3] = vrp_int_const_binop (code
, vr0
->max
, vr1
->max
);
2239 if (val
[3] == NULL_TREE
)
2245 set_value_range_to_varying (vr
);
2249 /* Set MIN to the minimum of VAL[i] and MAX to the maximum
2253 for (i
= 1; i
< 4; i
++)
2255 if (!is_gimple_min_invariant (min
)
2256 || (TREE_OVERFLOW (min
) && !is_overflow_infinity (min
))
2257 || !is_gimple_min_invariant (max
)
2258 || (TREE_OVERFLOW (max
) && !is_overflow_infinity (max
)))
2263 if (!is_gimple_min_invariant (val
[i
])
2264 || (TREE_OVERFLOW (val
[i
])
2265 && !is_overflow_infinity (val
[i
])))
2267 /* If we found an overflowed value, set MIN and MAX
2268 to it so that we set the resulting range to
2274 if (compare_values (val
[i
], min
) == -1)
2277 if (compare_values (val
[i
], max
) == 1)
2282 /* If either MIN or MAX overflowed, then set the resulting range to
2283 VARYING. But we do accept an overflow infinity
2285 if (min
== NULL_TREE
2286 || !is_gimple_min_invariant (min
)
2287 || (TREE_OVERFLOW (min
) && !is_overflow_infinity (min
))
2289 || !is_gimple_min_invariant (max
)
2290 || (TREE_OVERFLOW (max
) && !is_overflow_infinity (max
)))
2292 set_value_range_to_varying (vr
);
2298 2) [-INF, +-INF(OVF)]
2299 3) [+-INF(OVF), +INF]
2300 4) [+-INF(OVF), +-INF(OVF)]
2301 We learn nothing when we have INF and INF(OVF) on both sides.
2302 Note that we do accept [-INF, -INF] and [+INF, +INF] without
2304 if ((vrp_val_is_min (min
) || is_overflow_infinity (min
))
2305 && (vrp_val_is_max (max
) || is_overflow_infinity (max
)))
2307 set_value_range_to_varying (vr
);
2311 cmp
= compare_values (min
, max
);
2312 if (cmp
== -2 || cmp
== 1)
2314 /* If the new range has its limits swapped around (MIN > MAX),
2315 then the operation caused one of them to wrap around, mark
2316 the new range VARYING. */
2317 set_value_range_to_varying (vr
);
2320 set_value_range (vr
, type
, min
, max
, NULL
);
2323 /* Extract range information from a binary operation CODE based on
2324 the ranges of each of its operands *VR0 and *VR1 with resulting
2325 type EXPR_TYPE. The resulting range is stored in *VR. */
2328 extract_range_from_binary_expr_1 (value_range_t
*vr
,
2329 enum tree_code code
, tree expr_type
,
2330 value_range_t
*vr0_
, value_range_t
*vr1_
)
2332 value_range_t vr0
= *vr0_
, vr1
= *vr1_
;
2333 value_range_t vrtem0
= VR_INITIALIZER
, vrtem1
= VR_INITIALIZER
;
2334 enum value_range_type type
;
2335 tree min
= NULL_TREE
, max
= NULL_TREE
;
2338 if (!INTEGRAL_TYPE_P (expr_type
)
2339 && !POINTER_TYPE_P (expr_type
))
2341 set_value_range_to_varying (vr
);
2345 /* Not all binary expressions can be applied to ranges in a
2346 meaningful way. Handle only arithmetic operations. */
2347 if (code
!= PLUS_EXPR
2348 && code
!= MINUS_EXPR
2349 && code
!= POINTER_PLUS_EXPR
2350 && code
!= MULT_EXPR
2351 && code
!= TRUNC_DIV_EXPR
2352 && code
!= FLOOR_DIV_EXPR
2353 && code
!= CEIL_DIV_EXPR
2354 && code
!= EXACT_DIV_EXPR
2355 && code
!= ROUND_DIV_EXPR
2356 && code
!= TRUNC_MOD_EXPR
2357 && code
!= RSHIFT_EXPR
2358 && code
!= LSHIFT_EXPR
2361 && code
!= BIT_AND_EXPR
2362 && code
!= BIT_IOR_EXPR
2363 && code
!= BIT_XOR_EXPR
)
2365 set_value_range_to_varying (vr
);
2369 /* If both ranges are UNDEFINED, so is the result. */
2370 if (vr0
.type
== VR_UNDEFINED
&& vr1
.type
== VR_UNDEFINED
)
2372 set_value_range_to_undefined (vr
);
2375 /* If one of the ranges is UNDEFINED drop it to VARYING for the following
2376 code. At some point we may want to special-case operations that
2377 have UNDEFINED result for all or some value-ranges of the not UNDEFINED
2379 else if (vr0
.type
== VR_UNDEFINED
)
2380 set_value_range_to_varying (&vr0
);
2381 else if (vr1
.type
== VR_UNDEFINED
)
2382 set_value_range_to_varying (&vr1
);
2384 /* Now canonicalize anti-ranges to ranges when they are not symbolic
2385 and express ~[] op X as ([]' op X) U ([]'' op X). */
2386 if (vr0
.type
== VR_ANTI_RANGE
2387 && ranges_from_anti_range (&vr0
, &vrtem0
, &vrtem1
))
2389 extract_range_from_binary_expr_1 (vr
, code
, expr_type
, &vrtem0
, vr1_
);
2390 if (vrtem1
.type
!= VR_UNDEFINED
)
2392 value_range_t vrres
= VR_INITIALIZER
;
2393 extract_range_from_binary_expr_1 (&vrres
, code
, expr_type
,
2395 vrp_meet (vr
, &vrres
);
2399 /* Likewise for X op ~[]. */
2400 if (vr1
.type
== VR_ANTI_RANGE
2401 && ranges_from_anti_range (&vr1
, &vrtem0
, &vrtem1
))
2403 extract_range_from_binary_expr_1 (vr
, code
, expr_type
, vr0_
, &vrtem0
);
2404 if (vrtem1
.type
!= VR_UNDEFINED
)
2406 value_range_t vrres
= VR_INITIALIZER
;
2407 extract_range_from_binary_expr_1 (&vrres
, code
, expr_type
,
2409 vrp_meet (vr
, &vrres
);
2414 /* The type of the resulting value range defaults to VR0.TYPE. */
2417 /* Refuse to operate on VARYING ranges, ranges of different kinds
2418 and symbolic ranges. As an exception, we allow BIT_{AND,IOR}
2419 because we may be able to derive a useful range even if one of
2420 the operands is VR_VARYING or symbolic range. Similarly for
2421 divisions, MIN/MAX and PLUS/MINUS.
2423 TODO, we may be able to derive anti-ranges in some cases. */
2424 if (code
!= BIT_AND_EXPR
2425 && code
!= BIT_IOR_EXPR
2426 && code
!= TRUNC_DIV_EXPR
2427 && code
!= FLOOR_DIV_EXPR
2428 && code
!= CEIL_DIV_EXPR
2429 && code
!= EXACT_DIV_EXPR
2430 && code
!= ROUND_DIV_EXPR
2431 && code
!= TRUNC_MOD_EXPR
2434 && code
!= PLUS_EXPR
2435 && code
!= MINUS_EXPR
2436 && (vr0
.type
== VR_VARYING
2437 || vr1
.type
== VR_VARYING
2438 || vr0
.type
!= vr1
.type
2439 || symbolic_range_p (&vr0
)
2440 || symbolic_range_p (&vr1
)))
2442 set_value_range_to_varying (vr
);
2446 /* Now evaluate the expression to determine the new range. */
2447 if (POINTER_TYPE_P (expr_type
))
2449 if (code
== MIN_EXPR
|| code
== MAX_EXPR
)
2451 /* For MIN/MAX expressions with pointers, we only care about
2452 nullness, if both are non null, then the result is nonnull.
2453 If both are null, then the result is null. Otherwise they
2455 if (range_is_nonnull (&vr0
) && range_is_nonnull (&vr1
))
2456 set_value_range_to_nonnull (vr
, expr_type
);
2457 else if (range_is_null (&vr0
) && range_is_null (&vr1
))
2458 set_value_range_to_null (vr
, expr_type
);
2460 set_value_range_to_varying (vr
);
2462 else if (code
== POINTER_PLUS_EXPR
)
2464 /* For pointer types, we are really only interested in asserting
2465 whether the expression evaluates to non-NULL. */
2466 if (range_is_nonnull (&vr0
) || range_is_nonnull (&vr1
))
2467 set_value_range_to_nonnull (vr
, expr_type
);
2468 else if (range_is_null (&vr0
) && range_is_null (&vr1
))
2469 set_value_range_to_null (vr
, expr_type
);
2471 set_value_range_to_varying (vr
);
2473 else if (code
== BIT_AND_EXPR
)
2475 /* For pointer types, we are really only interested in asserting
2476 whether the expression evaluates to non-NULL. */
2477 if (range_is_nonnull (&vr0
) && range_is_nonnull (&vr1
))
2478 set_value_range_to_nonnull (vr
, expr_type
);
2479 else if (range_is_null (&vr0
) || range_is_null (&vr1
))
2480 set_value_range_to_null (vr
, expr_type
);
2482 set_value_range_to_varying (vr
);
2485 set_value_range_to_varying (vr
);
2490 /* For integer ranges, apply the operation to each end of the
2491 range and see what we end up with. */
2492 if (code
== PLUS_EXPR
|| code
== MINUS_EXPR
)
2494 const bool minus_p
= (code
== MINUS_EXPR
);
2495 tree min_op0
= vr0
.min
;
2496 tree min_op1
= minus_p
? vr1
.max
: vr1
.min
;
2497 tree max_op0
= vr0
.max
;
2498 tree max_op1
= minus_p
? vr1
.min
: vr1
.max
;
2499 tree sym_min_op0
= NULL_TREE
;
2500 tree sym_min_op1
= NULL_TREE
;
2501 tree sym_max_op0
= NULL_TREE
;
2502 tree sym_max_op1
= NULL_TREE
;
2503 bool neg_min_op0
, neg_min_op1
, neg_max_op0
, neg_max_op1
;
2505 /* If we have a PLUS or MINUS with two VR_RANGEs, either constant or
2506 single-symbolic ranges, try to compute the precise resulting range,
2507 but only if we know that this resulting range will also be constant
2508 or single-symbolic. */
2509 if (vr0
.type
== VR_RANGE
&& vr1
.type
== VR_RANGE
2510 && (TREE_CODE (min_op0
) == INTEGER_CST
2512 = get_single_symbol (min_op0
, &neg_min_op0
, &min_op0
)))
2513 && (TREE_CODE (min_op1
) == INTEGER_CST
2515 = get_single_symbol (min_op1
, &neg_min_op1
, &min_op1
)))
2516 && (!(sym_min_op0
&& sym_min_op1
)
2517 || (sym_min_op0
== sym_min_op1
2518 && neg_min_op0
== (minus_p
? neg_min_op1
: !neg_min_op1
)))
2519 && (TREE_CODE (max_op0
) == INTEGER_CST
2521 = get_single_symbol (max_op0
, &neg_max_op0
, &max_op0
)))
2522 && (TREE_CODE (max_op1
) == INTEGER_CST
2524 = get_single_symbol (max_op1
, &neg_max_op1
, &max_op1
)))
2525 && (!(sym_max_op0
&& sym_max_op1
)
2526 || (sym_max_op0
== sym_max_op1
2527 && neg_max_op0
== (minus_p
? neg_max_op1
: !neg_max_op1
))))
2529 const signop sgn
= TYPE_SIGN (expr_type
);
2530 const unsigned int prec
= TYPE_PRECISION (expr_type
);
2531 wide_int type_min
, type_max
, wmin
, wmax
;
2535 /* Get the lower and upper bounds of the type. */
2536 if (TYPE_OVERFLOW_WRAPS (expr_type
))
2538 type_min
= wi::min_value (prec
, sgn
);
2539 type_max
= wi::max_value (prec
, sgn
);
2543 type_min
= vrp_val_min (expr_type
);
2544 type_max
= vrp_val_max (expr_type
);
2547 /* Combine the lower bounds, if any. */
2548 if (min_op0
&& min_op1
)
2552 wmin
= wi::sub (min_op0
, min_op1
);
2554 /* Check for overflow. */
2555 if (wi::cmp (0, min_op1
, sgn
)
2556 != wi::cmp (wmin
, min_op0
, sgn
))
2557 min_ovf
= wi::cmp (min_op0
, min_op1
, sgn
);
2561 wmin
= wi::add (min_op0
, min_op1
);
2563 /* Check for overflow. */
2564 if (wi::cmp (min_op1
, 0, sgn
)
2565 != wi::cmp (wmin
, min_op0
, sgn
))
2566 min_ovf
= wi::cmp (min_op0
, wmin
, sgn
);
2572 wmin
= minus_p
? wi::neg (min_op1
) : min_op1
;
2574 wmin
= wi::shwi (0, prec
);
2576 /* Combine the upper bounds, if any. */
2577 if (max_op0
&& max_op1
)
2581 wmax
= wi::sub (max_op0
, max_op1
);
2583 /* Check for overflow. */
2584 if (wi::cmp (0, max_op1
, sgn
)
2585 != wi::cmp (wmax
, max_op0
, sgn
))
2586 max_ovf
= wi::cmp (max_op0
, max_op1
, sgn
);
2590 wmax
= wi::add (max_op0
, max_op1
);
2592 if (wi::cmp (max_op1
, 0, sgn
)
2593 != wi::cmp (wmax
, max_op0
, sgn
))
2594 max_ovf
= wi::cmp (max_op0
, wmax
, sgn
);
2600 wmax
= minus_p
? wi::neg (max_op1
) : max_op1
;
2602 wmax
= wi::shwi (0, prec
);
2604 /* Check for type overflow. */
2607 if (wi::cmp (wmin
, type_min
, sgn
) == -1)
2609 else if (wi::cmp (wmin
, type_max
, sgn
) == 1)
2614 if (wi::cmp (wmax
, type_min
, sgn
) == -1)
2616 else if (wi::cmp (wmax
, type_max
, sgn
) == 1)
2620 /* If we have overflow for the constant part and the resulting
2621 range will be symbolic, drop to VR_VARYING. */
2622 if ((min_ovf
&& sym_min_op0
!= sym_min_op1
)
2623 || (max_ovf
&& sym_max_op0
!= sym_max_op1
))
2625 set_value_range_to_varying (vr
);
2629 if (TYPE_OVERFLOW_WRAPS (expr_type
))
2631 /* If overflow wraps, truncate the values and adjust the
2632 range kind and bounds appropriately. */
2633 wide_int tmin
= wide_int::from (wmin
, prec
, sgn
);
2634 wide_int tmax
= wide_int::from (wmax
, prec
, sgn
);
2635 if (min_ovf
== max_ovf
)
2637 /* No overflow or both overflow or underflow. The
2638 range kind stays VR_RANGE. */
2639 min
= wide_int_to_tree (expr_type
, tmin
);
2640 max
= wide_int_to_tree (expr_type
, tmax
);
2642 else if (min_ovf
== -1 && max_ovf
== 1)
2644 /* Underflow and overflow, drop to VR_VARYING. */
2645 set_value_range_to_varying (vr
);
2650 /* Min underflow or max overflow. The range kind
2651 changes to VR_ANTI_RANGE. */
2652 bool covers
= false;
2653 wide_int tem
= tmin
;
2654 gcc_assert ((min_ovf
== -1 && max_ovf
== 0)
2655 || (max_ovf
== 1 && min_ovf
== 0));
2656 type
= VR_ANTI_RANGE
;
2658 if (wi::cmp (tmin
, tmax
, sgn
) < 0)
2661 if (wi::cmp (tmax
, tem
, sgn
) > 0)
2663 /* If the anti-range would cover nothing, drop to varying.
2664 Likewise if the anti-range bounds are outside of the
2666 if (covers
|| wi::cmp (tmin
, tmax
, sgn
) > 0)
2668 set_value_range_to_varying (vr
);
2671 min
= wide_int_to_tree (expr_type
, tmin
);
2672 max
= wide_int_to_tree (expr_type
, tmax
);
2677 /* If overflow does not wrap, saturate to the types min/max
2681 if (needs_overflow_infinity (expr_type
)
2682 && supports_overflow_infinity (expr_type
))
2683 min
= negative_overflow_infinity (expr_type
);
2685 min
= wide_int_to_tree (expr_type
, type_min
);
2687 else if (min_ovf
== 1)
2689 if (needs_overflow_infinity (expr_type
)
2690 && supports_overflow_infinity (expr_type
))
2691 min
= positive_overflow_infinity (expr_type
);
2693 min
= wide_int_to_tree (expr_type
, type_max
);
2696 min
= wide_int_to_tree (expr_type
, wmin
);
2700 if (needs_overflow_infinity (expr_type
)
2701 && supports_overflow_infinity (expr_type
))
2702 max
= negative_overflow_infinity (expr_type
);
2704 max
= wide_int_to_tree (expr_type
, type_min
);
2706 else if (max_ovf
== 1)
2708 if (needs_overflow_infinity (expr_type
)
2709 && supports_overflow_infinity (expr_type
))
2710 max
= positive_overflow_infinity (expr_type
);
2712 max
= wide_int_to_tree (expr_type
, type_max
);
2715 max
= wide_int_to_tree (expr_type
, wmax
);
2718 if (needs_overflow_infinity (expr_type
)
2719 && supports_overflow_infinity (expr_type
))
2721 if ((min_op0
&& is_negative_overflow_infinity (min_op0
))
2724 ? is_positive_overflow_infinity (min_op1
)
2725 : is_negative_overflow_infinity (min_op1
))))
2726 min
= negative_overflow_infinity (expr_type
);
2727 if ((max_op0
&& is_positive_overflow_infinity (max_op0
))
2730 ? is_negative_overflow_infinity (max_op1
)
2731 : is_positive_overflow_infinity (max_op1
))))
2732 max
= positive_overflow_infinity (expr_type
);
2735 /* If the result lower bound is constant, we're done;
2736 otherwise, build the symbolic lower bound. */
2737 if (sym_min_op0
== sym_min_op1
)
2739 else if (sym_min_op0
)
2740 min
= build_symbolic_expr (expr_type
, sym_min_op0
,
2742 else if (sym_min_op1
)
2743 min
= build_symbolic_expr (expr_type
, sym_min_op1
,
2744 neg_min_op1
^ minus_p
, min
);
2746 /* Likewise for the upper bound. */
2747 if (sym_max_op0
== sym_max_op1
)
2749 else if (sym_max_op0
)
2750 max
= build_symbolic_expr (expr_type
, sym_max_op0
,
2752 else if (sym_max_op1
)
2753 max
= build_symbolic_expr (expr_type
, sym_max_op1
,
2754 neg_max_op1
^ minus_p
, max
);
2758 /* For other cases, for example if we have a PLUS_EXPR with two
2759 VR_ANTI_RANGEs, drop to VR_VARYING. It would take more effort
2760 to compute a precise range for such a case.
2761 ??? General even mixed range kind operations can be expressed
2762 by for example transforming ~[3, 5] + [1, 2] to range-only
2763 operations and a union primitive:
2764 [-INF, 2] + [1, 2] U [5, +INF] + [1, 2]
2765 [-INF+1, 4] U [6, +INF(OVF)]
2766 though usually the union is not exactly representable with
2767 a single range or anti-range as the above is
2768 [-INF+1, +INF(OVF)] intersected with ~[5, 5]
2769 but one could use a scheme similar to equivalences for this. */
2770 set_value_range_to_varying (vr
);
2774 else if (code
== MIN_EXPR
2775 || code
== MAX_EXPR
)
2777 if (vr0
.type
== VR_RANGE
2778 && !symbolic_range_p (&vr0
))
2781 if (vr1
.type
== VR_RANGE
2782 && !symbolic_range_p (&vr1
))
2784 /* For operations that make the resulting range directly
2785 proportional to the original ranges, apply the operation to
2786 the same end of each range. */
2787 min
= vrp_int_const_binop (code
, vr0
.min
, vr1
.min
);
2788 max
= vrp_int_const_binop (code
, vr0
.max
, vr1
.max
);
2790 else if (code
== MIN_EXPR
)
2792 min
= vrp_val_min (expr_type
);
2795 else if (code
== MAX_EXPR
)
2798 max
= vrp_val_max (expr_type
);
2801 else if (vr1
.type
== VR_RANGE
2802 && !symbolic_range_p (&vr1
))
2805 if (code
== MIN_EXPR
)
2807 min
= vrp_val_min (expr_type
);
2810 else if (code
== MAX_EXPR
)
2813 max
= vrp_val_max (expr_type
);
2818 set_value_range_to_varying (vr
);
2822 else if (code
== MULT_EXPR
)
2824 /* Fancy code so that with unsigned, [-3,-1]*[-3,-1] does not
2825 drop to varying. This test requires 2*prec bits if both
2826 operands are signed and 2*prec + 2 bits if either is not. */
2828 signop sign
= TYPE_SIGN (expr_type
);
2829 unsigned int prec
= TYPE_PRECISION (expr_type
);
2831 if (range_int_cst_p (&vr0
)
2832 && range_int_cst_p (&vr1
)
2833 && TYPE_OVERFLOW_WRAPS (expr_type
))
2835 typedef FIXED_WIDE_INT (WIDE_INT_MAX_PRECISION
* 2) vrp_int
;
2836 typedef generic_wide_int
2837 <wi::extended_tree
<WIDE_INT_MAX_PRECISION
* 2> > vrp_int_cst
;
2838 vrp_int sizem1
= wi::mask
<vrp_int
> (prec
, false);
2839 vrp_int size
= sizem1
+ 1;
2841 /* Extend the values using the sign of the result to PREC2.
2842 From here on out, everthing is just signed math no matter
2843 what the input types were. */
2844 vrp_int min0
= vrp_int_cst (vr0
.min
);
2845 vrp_int max0
= vrp_int_cst (vr0
.max
);
2846 vrp_int min1
= vrp_int_cst (vr1
.min
);
2847 vrp_int max1
= vrp_int_cst (vr1
.max
);
2848 /* Canonicalize the intervals. */
2849 if (sign
== UNSIGNED
)
2851 if (wi::ltu_p (size
, min0
+ max0
))
2857 if (wi::ltu_p (size
, min1
+ max1
))
2864 vrp_int prod0
= min0
* min1
;
2865 vrp_int prod1
= min0
* max1
;
2866 vrp_int prod2
= max0
* min1
;
2867 vrp_int prod3
= max0
* max1
;
2869 /* Sort the 4 products so that min is in prod0 and max is in
2871 /* min0min1 > max0max1 */
2872 if (wi::gts_p (prod0
, prod3
))
2874 vrp_int tmp
= prod3
;
2879 /* min0max1 > max0min1 */
2880 if (wi::gts_p (prod1
, prod2
))
2882 vrp_int tmp
= prod2
;
2887 if (wi::gts_p (prod0
, prod1
))
2889 vrp_int tmp
= prod1
;
2894 if (wi::gts_p (prod2
, prod3
))
2896 vrp_int tmp
= prod3
;
2901 /* diff = max - min. */
2902 prod2
= prod3
- prod0
;
2903 if (wi::geu_p (prod2
, sizem1
))
2905 /* the range covers all values. */
2906 set_value_range_to_varying (vr
);
2910 /* The following should handle the wrapping and selecting
2911 VR_ANTI_RANGE for us. */
2912 min
= wide_int_to_tree (expr_type
, prod0
);
2913 max
= wide_int_to_tree (expr_type
, prod3
);
2914 set_and_canonicalize_value_range (vr
, VR_RANGE
, min
, max
, NULL
);
2918 /* If we have an unsigned MULT_EXPR with two VR_ANTI_RANGEs,
2919 drop to VR_VARYING. It would take more effort to compute a
2920 precise range for such a case. For example, if we have
2921 op0 == 65536 and op1 == 65536 with their ranges both being
2922 ~[0,0] on a 32-bit machine, we would have op0 * op1 == 0, so
2923 we cannot claim that the product is in ~[0,0]. Note that we
2924 are guaranteed to have vr0.type == vr1.type at this
2926 if (vr0
.type
== VR_ANTI_RANGE
2927 && !TYPE_OVERFLOW_UNDEFINED (expr_type
))
2929 set_value_range_to_varying (vr
);
2933 extract_range_from_multiplicative_op_1 (vr
, code
, &vr0
, &vr1
);
2936 else if (code
== RSHIFT_EXPR
2937 || code
== LSHIFT_EXPR
)
2939 /* If we have a RSHIFT_EXPR with any shift values outside [0..prec-1],
2940 then drop to VR_VARYING. Outside of this range we get undefined
2941 behavior from the shift operation. We cannot even trust
2942 SHIFT_COUNT_TRUNCATED at this stage, because that applies to rtl
2943 shifts, and the operation at the tree level may be widened. */
2944 if (range_int_cst_p (&vr1
)
2945 && compare_tree_int (vr1
.min
, 0) >= 0
2946 && compare_tree_int (vr1
.max
, TYPE_PRECISION (expr_type
)) == -1)
2948 if (code
== RSHIFT_EXPR
)
2950 extract_range_from_multiplicative_op_1 (vr
, code
, &vr0
, &vr1
);
2953 /* We can map lshifts by constants to MULT_EXPR handling. */
2954 else if (code
== LSHIFT_EXPR
2955 && range_int_cst_singleton_p (&vr1
))
2957 bool saved_flag_wrapv
;
2958 value_range_t vr1p
= VR_INITIALIZER
;
2959 vr1p
.type
= VR_RANGE
;
2960 vr1p
.min
= (wide_int_to_tree
2962 wi::set_bit_in_zero (tree_to_shwi (vr1
.min
),
2963 TYPE_PRECISION (expr_type
))));
2964 vr1p
.max
= vr1p
.min
;
2965 /* We have to use a wrapping multiply though as signed overflow
2966 on lshifts is implementation defined in C89. */
2967 saved_flag_wrapv
= flag_wrapv
;
2969 extract_range_from_binary_expr_1 (vr
, MULT_EXPR
, expr_type
,
2971 flag_wrapv
= saved_flag_wrapv
;
2974 else if (code
== LSHIFT_EXPR
2975 && range_int_cst_p (&vr0
))
2977 int prec
= TYPE_PRECISION (expr_type
);
2978 int overflow_pos
= prec
;
2980 wide_int low_bound
, high_bound
;
2981 bool uns
= TYPE_UNSIGNED (expr_type
);
2982 bool in_bounds
= false;
2987 bound_shift
= overflow_pos
- tree_to_shwi (vr1
.max
);
2988 /* If bound_shift == HOST_BITS_PER_WIDE_INT, the llshift can
2989 overflow. However, for that to happen, vr1.max needs to be
2990 zero, which means vr1 is a singleton range of zero, which
2991 means it should be handled by the previous LSHIFT_EXPR
2993 wide_int bound
= wi::set_bit_in_zero (bound_shift
, prec
);
2994 wide_int complement
= ~(bound
- 1);
2999 high_bound
= complement
;
3000 if (wi::ltu_p (vr0
.max
, low_bound
))
3002 /* [5, 6] << [1, 2] == [10, 24]. */
3003 /* We're shifting out only zeroes, the value increases
3007 else if (wi::ltu_p (high_bound
, vr0
.min
))
3009 /* [0xffffff00, 0xffffffff] << [1, 2]
3010 == [0xfffffc00, 0xfffffffe]. */
3011 /* We're shifting out only ones, the value decreases
3018 /* [-1, 1] << [1, 2] == [-4, 4]. */
3019 low_bound
= complement
;
3021 if (wi::lts_p (vr0
.max
, high_bound
)
3022 && wi::lts_p (low_bound
, vr0
.min
))
3024 /* For non-negative numbers, we're shifting out only
3025 zeroes, the value increases monotonically.
3026 For negative numbers, we're shifting out only ones, the
3027 value decreases monotomically. */
3034 extract_range_from_multiplicative_op_1 (vr
, code
, &vr0
, &vr1
);
3039 set_value_range_to_varying (vr
);
3042 else if (code
== TRUNC_DIV_EXPR
3043 || code
== FLOOR_DIV_EXPR
3044 || code
== CEIL_DIV_EXPR
3045 || code
== EXACT_DIV_EXPR
3046 || code
== ROUND_DIV_EXPR
)
3048 if (vr0
.type
!= VR_RANGE
|| symbolic_range_p (&vr0
))
3050 /* For division, if op1 has VR_RANGE but op0 does not, something
3051 can be deduced just from that range. Say [min, max] / [4, max]
3052 gives [min / 4, max / 4] range. */
3053 if (vr1
.type
== VR_RANGE
3054 && !symbolic_range_p (&vr1
)
3055 && range_includes_zero_p (vr1
.min
, vr1
.max
) == 0)
3057 vr0
.type
= type
= VR_RANGE
;
3058 vr0
.min
= vrp_val_min (expr_type
);
3059 vr0
.max
= vrp_val_max (expr_type
);
3063 set_value_range_to_varying (vr
);
3068 /* For divisions, if flag_non_call_exceptions is true, we must
3069 not eliminate a division by zero. */
3070 if (cfun
->can_throw_non_call_exceptions
3071 && (vr1
.type
!= VR_RANGE
3072 || range_includes_zero_p (vr1
.min
, vr1
.max
) != 0))
3074 set_value_range_to_varying (vr
);
3078 /* For divisions, if op0 is VR_RANGE, we can deduce a range
3079 even if op1 is VR_VARYING, VR_ANTI_RANGE, symbolic or can
3081 if (vr0
.type
== VR_RANGE
3082 && (vr1
.type
!= VR_RANGE
3083 || range_includes_zero_p (vr1
.min
, vr1
.max
) != 0))
3085 tree zero
= build_int_cst (TREE_TYPE (vr0
.min
), 0);
3090 if (TYPE_UNSIGNED (expr_type
)
3091 || value_range_nonnegative_p (&vr1
))
3093 /* For unsigned division or when divisor is known
3094 to be non-negative, the range has to cover
3095 all numbers from 0 to max for positive max
3096 and all numbers from min to 0 for negative min. */
3097 cmp
= compare_values (vr0
.max
, zero
);
3100 else if (cmp
== 0 || cmp
== 1)
3104 cmp
= compare_values (vr0
.min
, zero
);
3107 else if (cmp
== 0 || cmp
== -1)
3114 /* Otherwise the range is -max .. max or min .. -min
3115 depending on which bound is bigger in absolute value,
3116 as the division can change the sign. */
3117 abs_extent_range (vr
, vr0
.min
, vr0
.max
);
3120 if (type
== VR_VARYING
)
3122 set_value_range_to_varying (vr
);
3128 extract_range_from_multiplicative_op_1 (vr
, code
, &vr0
, &vr1
);
3132 else if (code
== TRUNC_MOD_EXPR
)
3134 if (vr1
.type
!= VR_RANGE
3135 || range_includes_zero_p (vr1
.min
, vr1
.max
) != 0
3136 || vrp_val_is_min (vr1
.min
))
3138 set_value_range_to_varying (vr
);
3142 /* Compute MAX <|vr1.min|, |vr1.max|> - 1. */
3143 max
= fold_unary_to_constant (ABS_EXPR
, expr_type
, vr1
.min
);
3144 if (tree_int_cst_lt (max
, vr1
.max
))
3146 max
= int_const_binop (MINUS_EXPR
, max
, build_int_cst (TREE_TYPE (max
), 1));
3147 /* If the dividend is non-negative the modulus will be
3148 non-negative as well. */
3149 if (TYPE_UNSIGNED (expr_type
)
3150 || value_range_nonnegative_p (&vr0
))
3151 min
= build_int_cst (TREE_TYPE (max
), 0);
3153 min
= fold_unary_to_constant (NEGATE_EXPR
, expr_type
, max
);
3155 else if (code
== BIT_AND_EXPR
|| code
== BIT_IOR_EXPR
|| code
== BIT_XOR_EXPR
)
3157 bool int_cst_range0
, int_cst_range1
;
3158 wide_int may_be_nonzero0
, may_be_nonzero1
;
3159 wide_int must_be_nonzero0
, must_be_nonzero1
;
3161 int_cst_range0
= zero_nonzero_bits_from_vr (expr_type
, &vr0
,
3164 int_cst_range1
= zero_nonzero_bits_from_vr (expr_type
, &vr1
,
3169 if (code
== BIT_AND_EXPR
)
3171 min
= wide_int_to_tree (expr_type
,
3172 must_be_nonzero0
& must_be_nonzero1
);
3173 wide_int wmax
= may_be_nonzero0
& may_be_nonzero1
;
3174 /* If both input ranges contain only negative values we can
3175 truncate the result range maximum to the minimum of the
3176 input range maxima. */
3177 if (int_cst_range0
&& int_cst_range1
3178 && tree_int_cst_sgn (vr0
.max
) < 0
3179 && tree_int_cst_sgn (vr1
.max
) < 0)
3181 wmax
= wi::min (wmax
, vr0
.max
, TYPE_SIGN (expr_type
));
3182 wmax
= wi::min (wmax
, vr1
.max
, TYPE_SIGN (expr_type
));
3184 /* If either input range contains only non-negative values
3185 we can truncate the result range maximum to the respective
3186 maximum of the input range. */
3187 if (int_cst_range0
&& tree_int_cst_sgn (vr0
.min
) >= 0)
3188 wmax
= wi::min (wmax
, vr0
.max
, TYPE_SIGN (expr_type
));
3189 if (int_cst_range1
&& tree_int_cst_sgn (vr1
.min
) >= 0)
3190 wmax
= wi::min (wmax
, vr1
.max
, TYPE_SIGN (expr_type
));
3191 max
= wide_int_to_tree (expr_type
, wmax
);
3193 else if (code
== BIT_IOR_EXPR
)
3195 max
= wide_int_to_tree (expr_type
,
3196 may_be_nonzero0
| may_be_nonzero1
);
3197 wide_int wmin
= must_be_nonzero0
| must_be_nonzero1
;
3198 /* If the input ranges contain only positive values we can
3199 truncate the minimum of the result range to the maximum
3200 of the input range minima. */
3201 if (int_cst_range0
&& int_cst_range1
3202 && tree_int_cst_sgn (vr0
.min
) >= 0
3203 && tree_int_cst_sgn (vr1
.min
) >= 0)
3205 wmin
= wi::max (wmin
, vr0
.min
, TYPE_SIGN (expr_type
));
3206 wmin
= wi::max (wmin
, vr1
.min
, TYPE_SIGN (expr_type
));
3208 /* If either input range contains only negative values
3209 we can truncate the minimum of the result range to the
3210 respective minimum range. */
3211 if (int_cst_range0
&& tree_int_cst_sgn (vr0
.max
) < 0)
3212 wmin
= wi::max (wmin
, vr0
.min
, TYPE_SIGN (expr_type
));
3213 if (int_cst_range1
&& tree_int_cst_sgn (vr1
.max
) < 0)
3214 wmin
= wi::max (wmin
, vr1
.min
, TYPE_SIGN (expr_type
));
3215 min
= wide_int_to_tree (expr_type
, wmin
);
3217 else if (code
== BIT_XOR_EXPR
)
3219 wide_int result_zero_bits
= ((must_be_nonzero0
& must_be_nonzero1
)
3220 | ~(may_be_nonzero0
| may_be_nonzero1
));
3221 wide_int result_one_bits
3222 = (must_be_nonzero0
.and_not (may_be_nonzero1
)
3223 | must_be_nonzero1
.and_not (may_be_nonzero0
));
3224 max
= wide_int_to_tree (expr_type
, ~result_zero_bits
);
3225 min
= wide_int_to_tree (expr_type
, result_one_bits
);
3226 /* If the range has all positive or all negative values the
3227 result is better than VARYING. */
3228 if (tree_int_cst_sgn (min
) < 0
3229 || tree_int_cst_sgn (max
) >= 0)
3232 max
= min
= NULL_TREE
;
3238 /* If either MIN or MAX overflowed, then set the resulting range to
3239 VARYING. But we do accept an overflow infinity representation. */
3240 if (min
== NULL_TREE
3241 || (TREE_OVERFLOW_P (min
) && !is_overflow_infinity (min
))
3243 || (TREE_OVERFLOW_P (max
) && !is_overflow_infinity (max
)))
3245 set_value_range_to_varying (vr
);
3251 2) [-INF, +-INF(OVF)]
3252 3) [+-INF(OVF), +INF]
3253 4) [+-INF(OVF), +-INF(OVF)]
3254 We learn nothing when we have INF and INF(OVF) on both sides.
3255 Note that we do accept [-INF, -INF] and [+INF, +INF] without
3257 if ((vrp_val_is_min (min
) || is_overflow_infinity (min
))
3258 && (vrp_val_is_max (max
) || is_overflow_infinity (max
)))
3260 set_value_range_to_varying (vr
);
3264 cmp
= compare_values (min
, max
);
3265 if (cmp
== -2 || cmp
== 1)
3267 /* If the new range has its limits swapped around (MIN > MAX),
3268 then the operation caused one of them to wrap around, mark
3269 the new range VARYING. */
3270 set_value_range_to_varying (vr
);
3273 set_value_range (vr
, type
, min
, max
, NULL
);
3276 /* Extract range information from a binary expression OP0 CODE OP1 based on
3277 the ranges of each of its operands with resulting type EXPR_TYPE.
3278 The resulting range is stored in *VR. */
3281 extract_range_from_binary_expr (value_range_t
*vr
,
3282 enum tree_code code
,
3283 tree expr_type
, tree op0
, tree op1
)
3285 value_range_t vr0
= VR_INITIALIZER
;
3286 value_range_t vr1
= VR_INITIALIZER
;
3288 /* Get value ranges for each operand. For constant operands, create
3289 a new value range with the operand to simplify processing. */
3290 if (TREE_CODE (op0
) == SSA_NAME
)
3291 vr0
= *(get_value_range (op0
));
3292 else if (is_gimple_min_invariant (op0
))
3293 set_value_range_to_value (&vr0
, op0
, NULL
);
3295 set_value_range_to_varying (&vr0
);
3297 if (TREE_CODE (op1
) == SSA_NAME
)
3298 vr1
= *(get_value_range (op1
));
3299 else if (is_gimple_min_invariant (op1
))
3300 set_value_range_to_value (&vr1
, op1
, NULL
);
3302 set_value_range_to_varying (&vr1
);
3304 extract_range_from_binary_expr_1 (vr
, code
, expr_type
, &vr0
, &vr1
);
3306 /* Try harder for PLUS and MINUS if the range of one operand is symbolic
3307 and based on the other operand, for example if it was deduced from a
3308 symbolic comparison. When a bound of the range of the first operand
3309 is invariant, we set the corresponding bound of the new range to INF
3310 in order to avoid recursing on the range of the second operand. */
3311 if (vr
->type
== VR_VARYING
3312 && (code
== PLUS_EXPR
|| code
== MINUS_EXPR
)
3313 && TREE_CODE (op1
) == SSA_NAME
3314 && vr0
.type
== VR_RANGE
3315 && symbolic_range_based_on_p (&vr0
, op1
))
3317 const bool minus_p
= (code
== MINUS_EXPR
);
3318 value_range_t n_vr1
= VR_INITIALIZER
;
3320 /* Try with VR0 and [-INF, OP1]. */
3321 if (is_gimple_min_invariant (minus_p
? vr0
.max
: vr0
.min
))
3322 set_value_range (&n_vr1
, VR_RANGE
, vrp_val_min (expr_type
), op1
, NULL
);
3324 /* Try with VR0 and [OP1, +INF]. */
3325 else if (is_gimple_min_invariant (minus_p
? vr0
.min
: vr0
.max
))
3326 set_value_range (&n_vr1
, VR_RANGE
, op1
, vrp_val_max (expr_type
), NULL
);
3328 /* Try with VR0 and [OP1, OP1]. */
3330 set_value_range (&n_vr1
, VR_RANGE
, op1
, op1
, NULL
);
3332 extract_range_from_binary_expr_1 (vr
, code
, expr_type
, &vr0
, &n_vr1
);
3335 if (vr
->type
== VR_VARYING
3336 && (code
== PLUS_EXPR
|| code
== MINUS_EXPR
)
3337 && TREE_CODE (op0
) == SSA_NAME
3338 && vr1
.type
== VR_RANGE
3339 && symbolic_range_based_on_p (&vr1
, op0
))
3341 const bool minus_p
= (code
== MINUS_EXPR
);
3342 value_range_t n_vr0
= VR_INITIALIZER
;
3344 /* Try with [-INF, OP0] and VR1. */
3345 if (is_gimple_min_invariant (minus_p
? vr1
.max
: vr1
.min
))
3346 set_value_range (&n_vr0
, VR_RANGE
, vrp_val_min (expr_type
), op0
, NULL
);
3348 /* Try with [OP0, +INF] and VR1. */
3349 else if (is_gimple_min_invariant (minus_p
? vr1
.min
: vr1
.max
))
3350 set_value_range (&n_vr0
, VR_RANGE
, op0
, vrp_val_max (expr_type
), NULL
);
3352 /* Try with [OP0, OP0] and VR1. */
3354 set_value_range (&n_vr0
, VR_RANGE
, op0
, op0
, NULL
);
3356 extract_range_from_binary_expr_1 (vr
, code
, expr_type
, &n_vr0
, &vr1
);
3360 /* Extract range information from a unary operation CODE based on
3361 the range of its operand *VR0 with type OP0_TYPE with resulting type TYPE.
3362 The The resulting range is stored in *VR. */
3365 extract_range_from_unary_expr_1 (value_range_t
*vr
,
3366 enum tree_code code
, tree type
,
3367 value_range_t
*vr0_
, tree op0_type
)
3369 value_range_t vr0
= *vr0_
, vrtem0
= VR_INITIALIZER
, vrtem1
= VR_INITIALIZER
;
3371 /* VRP only operates on integral and pointer types. */
3372 if (!(INTEGRAL_TYPE_P (op0_type
)
3373 || POINTER_TYPE_P (op0_type
))
3374 || !(INTEGRAL_TYPE_P (type
)
3375 || POINTER_TYPE_P (type
)))
3377 set_value_range_to_varying (vr
);
3381 /* If VR0 is UNDEFINED, so is the result. */
3382 if (vr0
.type
== VR_UNDEFINED
)
3384 set_value_range_to_undefined (vr
);
3388 /* Handle operations that we express in terms of others. */
3389 if (code
== PAREN_EXPR
|| code
== OBJ_TYPE_REF
)
3391 /* PAREN_EXPR and OBJ_TYPE_REF are simple copies. */
3392 copy_value_range (vr
, &vr0
);
3395 else if (code
== NEGATE_EXPR
)
3397 /* -X is simply 0 - X, so re-use existing code that also handles
3398 anti-ranges fine. */
3399 value_range_t zero
= VR_INITIALIZER
;
3400 set_value_range_to_value (&zero
, build_int_cst (type
, 0), NULL
);
3401 extract_range_from_binary_expr_1 (vr
, MINUS_EXPR
, type
, &zero
, &vr0
);
3404 else if (code
== BIT_NOT_EXPR
)
3406 /* ~X is simply -1 - X, so re-use existing code that also handles
3407 anti-ranges fine. */
3408 value_range_t minusone
= VR_INITIALIZER
;
3409 set_value_range_to_value (&minusone
, build_int_cst (type
, -1), NULL
);
3410 extract_range_from_binary_expr_1 (vr
, MINUS_EXPR
,
3411 type
, &minusone
, &vr0
);
3415 /* Now canonicalize anti-ranges to ranges when they are not symbolic
3416 and express op ~[] as (op []') U (op []''). */
3417 if (vr0
.type
== VR_ANTI_RANGE
3418 && ranges_from_anti_range (&vr0
, &vrtem0
, &vrtem1
))
3420 extract_range_from_unary_expr_1 (vr
, code
, type
, &vrtem0
, op0_type
);
3421 if (vrtem1
.type
!= VR_UNDEFINED
)
3423 value_range_t vrres
= VR_INITIALIZER
;
3424 extract_range_from_unary_expr_1 (&vrres
, code
, type
,
3426 vrp_meet (vr
, &vrres
);
3431 if (CONVERT_EXPR_CODE_P (code
))
3433 tree inner_type
= op0_type
;
3434 tree outer_type
= type
;
3436 /* If the expression evaluates to a pointer, we are only interested in
3437 determining if it evaluates to NULL [0, 0] or non-NULL (~[0, 0]). */
3438 if (POINTER_TYPE_P (type
))
3440 if (range_is_nonnull (&vr0
))
3441 set_value_range_to_nonnull (vr
, type
);
3442 else if (range_is_null (&vr0
))
3443 set_value_range_to_null (vr
, type
);
3445 set_value_range_to_varying (vr
);
3449 /* If VR0 is varying and we increase the type precision, assume
3450 a full range for the following transformation. */
3451 if (vr0
.type
== VR_VARYING
3452 && INTEGRAL_TYPE_P (inner_type
)
3453 && TYPE_PRECISION (inner_type
) < TYPE_PRECISION (outer_type
))
3455 vr0
.type
= VR_RANGE
;
3456 vr0
.min
= TYPE_MIN_VALUE (inner_type
);
3457 vr0
.max
= TYPE_MAX_VALUE (inner_type
);
3460 /* If VR0 is a constant range or anti-range and the conversion is
3461 not truncating we can convert the min and max values and
3462 canonicalize the resulting range. Otherwise we can do the
3463 conversion if the size of the range is less than what the
3464 precision of the target type can represent and the range is
3465 not an anti-range. */
3466 if ((vr0
.type
== VR_RANGE
3467 || vr0
.type
== VR_ANTI_RANGE
)
3468 && TREE_CODE (vr0
.min
) == INTEGER_CST
3469 && TREE_CODE (vr0
.max
) == INTEGER_CST
3470 && (!is_overflow_infinity (vr0
.min
)
3471 || (vr0
.type
== VR_RANGE
3472 && TYPE_PRECISION (outer_type
) > TYPE_PRECISION (inner_type
)
3473 && needs_overflow_infinity (outer_type
)
3474 && supports_overflow_infinity (outer_type
)))
3475 && (!is_overflow_infinity (vr0
.max
)
3476 || (vr0
.type
== VR_RANGE
3477 && TYPE_PRECISION (outer_type
) > TYPE_PRECISION (inner_type
)
3478 && needs_overflow_infinity (outer_type
)
3479 && supports_overflow_infinity (outer_type
)))
3480 && (TYPE_PRECISION (outer_type
) >= TYPE_PRECISION (inner_type
)
3481 || (vr0
.type
== VR_RANGE
3482 && integer_zerop (int_const_binop (RSHIFT_EXPR
,
3483 int_const_binop (MINUS_EXPR
, vr0
.max
, vr0
.min
),
3484 size_int (TYPE_PRECISION (outer_type
)))))))
3486 tree new_min
, new_max
;
3487 if (is_overflow_infinity (vr0
.min
))
3488 new_min
= negative_overflow_infinity (outer_type
);
3490 new_min
= force_fit_type (outer_type
, wi::to_widest (vr0
.min
),
3492 if (is_overflow_infinity (vr0
.max
))
3493 new_max
= positive_overflow_infinity (outer_type
);
3495 new_max
= force_fit_type (outer_type
, wi::to_widest (vr0
.max
),
3497 set_and_canonicalize_value_range (vr
, vr0
.type
,
3498 new_min
, new_max
, NULL
);
3502 set_value_range_to_varying (vr
);
3505 else if (code
== ABS_EXPR
)
3510 /* Pass through vr0 in the easy cases. */
3511 if (TYPE_UNSIGNED (type
)
3512 || value_range_nonnegative_p (&vr0
))
3514 copy_value_range (vr
, &vr0
);
3518 /* For the remaining varying or symbolic ranges we can't do anything
3520 if (vr0
.type
== VR_VARYING
3521 || symbolic_range_p (&vr0
))
3523 set_value_range_to_varying (vr
);
3527 /* -TYPE_MIN_VALUE = TYPE_MIN_VALUE with flag_wrapv so we can't get a
3529 if (!TYPE_OVERFLOW_UNDEFINED (type
)
3530 && ((vr0
.type
== VR_RANGE
3531 && vrp_val_is_min (vr0
.min
))
3532 || (vr0
.type
== VR_ANTI_RANGE
3533 && !vrp_val_is_min (vr0
.min
))))
3535 set_value_range_to_varying (vr
);
3539 /* ABS_EXPR may flip the range around, if the original range
3540 included negative values. */
3541 if (is_overflow_infinity (vr0
.min
))
3542 min
= positive_overflow_infinity (type
);
3543 else if (!vrp_val_is_min (vr0
.min
))
3544 min
= fold_unary_to_constant (code
, type
, vr0
.min
);
3545 else if (!needs_overflow_infinity (type
))
3546 min
= TYPE_MAX_VALUE (type
);
3547 else if (supports_overflow_infinity (type
))
3548 min
= positive_overflow_infinity (type
);
3551 set_value_range_to_varying (vr
);
3555 if (is_overflow_infinity (vr0
.max
))
3556 max
= positive_overflow_infinity (type
);
3557 else if (!vrp_val_is_min (vr0
.max
))
3558 max
= fold_unary_to_constant (code
, type
, vr0
.max
);
3559 else if (!needs_overflow_infinity (type
))
3560 max
= TYPE_MAX_VALUE (type
);
3561 else if (supports_overflow_infinity (type
)
3562 /* We shouldn't generate [+INF, +INF] as set_value_range
3563 doesn't like this and ICEs. */
3564 && !is_positive_overflow_infinity (min
))
3565 max
= positive_overflow_infinity (type
);
3568 set_value_range_to_varying (vr
);
3572 cmp
= compare_values (min
, max
);
3574 /* If a VR_ANTI_RANGEs contains zero, then we have
3575 ~[-INF, min(MIN, MAX)]. */
3576 if (vr0
.type
== VR_ANTI_RANGE
)
3578 if (range_includes_zero_p (vr0
.min
, vr0
.max
) == 1)
3580 /* Take the lower of the two values. */
3584 /* Create ~[-INF, min (abs(MIN), abs(MAX))]
3585 or ~[-INF + 1, min (abs(MIN), abs(MAX))] when
3586 flag_wrapv is set and the original anti-range doesn't include
3587 TYPE_MIN_VALUE, remember -TYPE_MIN_VALUE = TYPE_MIN_VALUE. */
3588 if (TYPE_OVERFLOW_WRAPS (type
))
3590 tree type_min_value
= TYPE_MIN_VALUE (type
);
3592 min
= (vr0
.min
!= type_min_value
3593 ? int_const_binop (PLUS_EXPR
, type_min_value
,
3594 build_int_cst (TREE_TYPE (type_min_value
), 1))
3599 if (overflow_infinity_range_p (&vr0
))
3600 min
= negative_overflow_infinity (type
);
3602 min
= TYPE_MIN_VALUE (type
);
3607 /* All else has failed, so create the range [0, INF], even for
3608 flag_wrapv since TYPE_MIN_VALUE is in the original
3610 vr0
.type
= VR_RANGE
;
3611 min
= build_int_cst (type
, 0);
3612 if (needs_overflow_infinity (type
))
3614 if (supports_overflow_infinity (type
))
3615 max
= positive_overflow_infinity (type
);
3618 set_value_range_to_varying (vr
);
3623 max
= TYPE_MAX_VALUE (type
);
3627 /* If the range contains zero then we know that the minimum value in the
3628 range will be zero. */
3629 else if (range_includes_zero_p (vr0
.min
, vr0
.max
) == 1)
3633 min
= build_int_cst (type
, 0);
3637 /* If the range was reversed, swap MIN and MAX. */
3646 cmp
= compare_values (min
, max
);
3647 if (cmp
== -2 || cmp
== 1)
3649 /* If the new range has its limits swapped around (MIN > MAX),
3650 then the operation caused one of them to wrap around, mark
3651 the new range VARYING. */
3652 set_value_range_to_varying (vr
);
3655 set_value_range (vr
, vr0
.type
, min
, max
, NULL
);
3659 /* For unhandled operations fall back to varying. */
3660 set_value_range_to_varying (vr
);
3665 /* Extract range information from a unary expression CODE OP0 based on
3666 the range of its operand with resulting type TYPE.
3667 The resulting range is stored in *VR. */
3670 extract_range_from_unary_expr (value_range_t
*vr
, enum tree_code code
,
3671 tree type
, tree op0
)
3673 value_range_t vr0
= VR_INITIALIZER
;
3675 /* Get value ranges for the operand. For constant operands, create
3676 a new value range with the operand to simplify processing. */
3677 if (TREE_CODE (op0
) == SSA_NAME
)
3678 vr0
= *(get_value_range (op0
));
3679 else if (is_gimple_min_invariant (op0
))
3680 set_value_range_to_value (&vr0
, op0
, NULL
);
3682 set_value_range_to_varying (&vr0
);
3684 extract_range_from_unary_expr_1 (vr
, code
, type
, &vr0
, TREE_TYPE (op0
));
3688 /* Extract range information from a conditional expression STMT based on
3689 the ranges of each of its operands and the expression code. */
3692 extract_range_from_cond_expr (value_range_t
*vr
, gimple stmt
)
3695 value_range_t vr0
= VR_INITIALIZER
;
3696 value_range_t vr1
= VR_INITIALIZER
;
3698 /* Get value ranges for each operand. For constant operands, create
3699 a new value range with the operand to simplify processing. */
3700 op0
= gimple_assign_rhs2 (stmt
);
3701 if (TREE_CODE (op0
) == SSA_NAME
)
3702 vr0
= *(get_value_range (op0
));
3703 else if (is_gimple_min_invariant (op0
))
3704 set_value_range_to_value (&vr0
, op0
, NULL
);
3706 set_value_range_to_varying (&vr0
);
3708 op1
= gimple_assign_rhs3 (stmt
);
3709 if (TREE_CODE (op1
) == SSA_NAME
)
3710 vr1
= *(get_value_range (op1
));
3711 else if (is_gimple_min_invariant (op1
))
3712 set_value_range_to_value (&vr1
, op1
, NULL
);
3714 set_value_range_to_varying (&vr1
);
3716 /* The resulting value range is the union of the operand ranges */
3717 copy_value_range (vr
, &vr0
);
3718 vrp_meet (vr
, &vr1
);
3722 /* Extract range information from a comparison expression EXPR based
3723 on the range of its operand and the expression code. */
3726 extract_range_from_comparison (value_range_t
*vr
, enum tree_code code
,
3727 tree type
, tree op0
, tree op1
)
3732 val
= vrp_evaluate_conditional_warnv_with_ops (code
, op0
, op1
, false, &sop
,
3735 /* A disadvantage of using a special infinity as an overflow
3736 representation is that we lose the ability to record overflow
3737 when we don't have an infinity. So we have to ignore a result
3738 which relies on overflow. */
3740 if (val
&& !is_overflow_infinity (val
) && !sop
)
3742 /* Since this expression was found on the RHS of an assignment,
3743 its type may be different from _Bool. Convert VAL to EXPR's
3745 val
= fold_convert (type
, val
);
3746 if (is_gimple_min_invariant (val
))
3747 set_value_range_to_value (vr
, val
, vr
->equiv
);
3749 set_value_range (vr
, VR_RANGE
, val
, val
, vr
->equiv
);
3752 /* The result of a comparison is always true or false. */
3753 set_value_range_to_truthvalue (vr
, type
);
3756 /* Try to derive a nonnegative or nonzero range out of STMT relying
3757 primarily on generic routines in fold in conjunction with range data.
3758 Store the result in *VR */
3761 extract_range_basic (value_range_t
*vr
, gimple stmt
)
3764 tree type
= gimple_expr_type (stmt
);
3766 if (gimple_call_builtin_p (stmt
, BUILT_IN_NORMAL
))
3768 tree fndecl
= gimple_call_fndecl (stmt
), arg
;
3769 int mini
, maxi
, zerov
= 0, prec
;
3771 switch (DECL_FUNCTION_CODE (fndecl
))
3773 case BUILT_IN_CONSTANT_P
:
3774 /* If the call is __builtin_constant_p and the argument is a
3775 function parameter resolve it to false. This avoids bogus
3776 array bound warnings.
3777 ??? We could do this as early as inlining is finished. */
3778 arg
= gimple_call_arg (stmt
, 0);
3779 if (TREE_CODE (arg
) == SSA_NAME
3780 && SSA_NAME_IS_DEFAULT_DEF (arg
)
3781 && TREE_CODE (SSA_NAME_VAR (arg
)) == PARM_DECL
)
3783 set_value_range_to_null (vr
, type
);
3787 /* Both __builtin_ffs* and __builtin_popcount return
3789 CASE_INT_FN (BUILT_IN_FFS
):
3790 CASE_INT_FN (BUILT_IN_POPCOUNT
):
3791 arg
= gimple_call_arg (stmt
, 0);
3792 prec
= TYPE_PRECISION (TREE_TYPE (arg
));
3795 if (TREE_CODE (arg
) == SSA_NAME
)
3797 value_range_t
*vr0
= get_value_range (arg
);
3798 /* If arg is non-zero, then ffs or popcount
3800 if (((vr0
->type
== VR_RANGE
3801 && range_includes_zero_p (vr0
->min
, vr0
->max
) == 0)
3802 || (vr0
->type
== VR_ANTI_RANGE
3803 && range_includes_zero_p (vr0
->min
, vr0
->max
) == 1))
3804 && !is_overflow_infinity (vr0
->min
)
3805 && !is_overflow_infinity (vr0
->max
))
3807 /* If some high bits are known to be zero,
3808 we can decrease the maximum. */
3809 if (vr0
->type
== VR_RANGE
3810 && TREE_CODE (vr0
->max
) == INTEGER_CST
3811 && !operand_less_p (vr0
->min
,
3812 build_zero_cst (TREE_TYPE (vr0
->min
)))
3813 && !is_overflow_infinity (vr0
->max
))
3814 maxi
= tree_floor_log2 (vr0
->max
) + 1;
3817 /* __builtin_parity* returns [0, 1]. */
3818 CASE_INT_FN (BUILT_IN_PARITY
):
3822 /* __builtin_c[lt]z* return [0, prec-1], except for
3823 when the argument is 0, but that is undefined behavior.
3824 On many targets where the CLZ RTL or optab value is defined
3825 for 0 the value is prec, so include that in the range
3827 CASE_INT_FN (BUILT_IN_CLZ
):
3828 arg
= gimple_call_arg (stmt
, 0);
3829 prec
= TYPE_PRECISION (TREE_TYPE (arg
));
3832 if (optab_handler (clz_optab
, TYPE_MODE (TREE_TYPE (arg
)))
3834 && CLZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (TREE_TYPE (arg
)),
3836 /* Handle only the single common value. */
3838 /* Magic value to give up, unless vr0 proves
3841 if (TREE_CODE (arg
) == SSA_NAME
)
3843 value_range_t
*vr0
= get_value_range (arg
);
3844 /* From clz of VR_RANGE minimum we can compute
3846 if (vr0
->type
== VR_RANGE
3847 && TREE_CODE (vr0
->min
) == INTEGER_CST
3848 && !is_overflow_infinity (vr0
->min
))
3850 maxi
= prec
- 1 - tree_floor_log2 (vr0
->min
);
3854 else if (vr0
->type
== VR_ANTI_RANGE
3855 && integer_zerop (vr0
->min
)
3856 && !is_overflow_infinity (vr0
->min
))
3863 /* From clz of VR_RANGE maximum we can compute
3865 if (vr0
->type
== VR_RANGE
3866 && TREE_CODE (vr0
->max
) == INTEGER_CST
3867 && !is_overflow_infinity (vr0
->max
))
3869 mini
= prec
- 1 - tree_floor_log2 (vr0
->max
);
3877 /* __builtin_ctz* return [0, prec-1], except for
3878 when the argument is 0, but that is undefined behavior.
3879 If there is a ctz optab for this mode and
3880 CTZ_DEFINED_VALUE_AT_ZERO, include that in the range,
3881 otherwise just assume 0 won't be seen. */
3882 CASE_INT_FN (BUILT_IN_CTZ
):
3883 arg
= gimple_call_arg (stmt
, 0);
3884 prec
= TYPE_PRECISION (TREE_TYPE (arg
));
3887 if (optab_handler (ctz_optab
, TYPE_MODE (TREE_TYPE (arg
)))
3889 && CTZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (TREE_TYPE (arg
)),
3892 /* Handle only the two common values. */
3895 else if (zerov
== prec
)
3898 /* Magic value to give up, unless vr0 proves
3902 if (TREE_CODE (arg
) == SSA_NAME
)
3904 value_range_t
*vr0
= get_value_range (arg
);
3905 /* If arg is non-zero, then use [0, prec - 1]. */
3906 if (((vr0
->type
== VR_RANGE
3907 && integer_nonzerop (vr0
->min
))
3908 || (vr0
->type
== VR_ANTI_RANGE
3909 && integer_zerop (vr0
->min
)))
3910 && !is_overflow_infinity (vr0
->min
))
3915 /* If some high bits are known to be zero,
3916 we can decrease the result maximum. */
3917 if (vr0
->type
== VR_RANGE
3918 && TREE_CODE (vr0
->max
) == INTEGER_CST
3919 && !is_overflow_infinity (vr0
->max
))
3921 maxi
= tree_floor_log2 (vr0
->max
);
3922 /* For vr0 [0, 0] give up. */
3930 /* __builtin_clrsb* returns [0, prec-1]. */
3931 CASE_INT_FN (BUILT_IN_CLRSB
):
3932 arg
= gimple_call_arg (stmt
, 0);
3933 prec
= TYPE_PRECISION (TREE_TYPE (arg
));
3938 set_value_range (vr
, VR_RANGE
, build_int_cst (type
, mini
),
3939 build_int_cst (type
, maxi
), NULL
);
3945 else if (is_gimple_call (stmt
)
3946 && gimple_call_internal_p (stmt
))
3948 enum tree_code subcode
= ERROR_MARK
;
3949 switch (gimple_call_internal_fn (stmt
))
3951 case IFN_UBSAN_CHECK_ADD
:
3952 subcode
= PLUS_EXPR
;
3954 case IFN_UBSAN_CHECK_SUB
:
3955 subcode
= MINUS_EXPR
;
3957 case IFN_UBSAN_CHECK_MUL
:
3958 subcode
= MULT_EXPR
;
3963 if (subcode
!= ERROR_MARK
)
3965 bool saved_flag_wrapv
= flag_wrapv
;
3966 /* Pretend the arithmetics is wrapping. If there is
3967 any overflow, we'll complain, but will actually do
3968 wrapping operation. */
3970 extract_range_from_binary_expr (vr
, subcode
, type
,
3971 gimple_call_arg (stmt
, 0),
3972 gimple_call_arg (stmt
, 1));
3973 flag_wrapv
= saved_flag_wrapv
;
3975 /* If for both arguments vrp_valueize returned non-NULL,
3976 this should have been already folded and if not, it
3977 wasn't folded because of overflow. Avoid removing the
3978 UBSAN_CHECK_* calls in that case. */
3979 if (vr
->type
== VR_RANGE
3980 && (vr
->min
== vr
->max
3981 || operand_equal_p (vr
->min
, vr
->max
, 0)))
3982 set_value_range_to_varying (vr
);
3986 if (INTEGRAL_TYPE_P (type
)
3987 && gimple_stmt_nonnegative_warnv_p (stmt
, &sop
))
3988 set_value_range_to_nonnegative (vr
, type
,
3989 sop
|| stmt_overflow_infinity (stmt
));
3990 else if (vrp_stmt_computes_nonzero (stmt
, &sop
)
3992 set_value_range_to_nonnull (vr
, type
);
3994 set_value_range_to_varying (vr
);
3998 /* Try to compute a useful range out of assignment STMT and store it
4002 extract_range_from_assignment (value_range_t
*vr
, gimple stmt
)
4004 enum tree_code code
= gimple_assign_rhs_code (stmt
);
4006 if (code
== ASSERT_EXPR
)
4007 extract_range_from_assert (vr
, gimple_assign_rhs1 (stmt
));
4008 else if (code
== SSA_NAME
)
4009 extract_range_from_ssa_name (vr
, gimple_assign_rhs1 (stmt
));
4010 else if (TREE_CODE_CLASS (code
) == tcc_binary
)
4011 extract_range_from_binary_expr (vr
, gimple_assign_rhs_code (stmt
),
4012 gimple_expr_type (stmt
),
4013 gimple_assign_rhs1 (stmt
),
4014 gimple_assign_rhs2 (stmt
));
4015 else if (TREE_CODE_CLASS (code
) == tcc_unary
)
4016 extract_range_from_unary_expr (vr
, gimple_assign_rhs_code (stmt
),
4017 gimple_expr_type (stmt
),
4018 gimple_assign_rhs1 (stmt
));
4019 else if (code
== COND_EXPR
)
4020 extract_range_from_cond_expr (vr
, stmt
);
4021 else if (TREE_CODE_CLASS (code
) == tcc_comparison
)
4022 extract_range_from_comparison (vr
, gimple_assign_rhs_code (stmt
),
4023 gimple_expr_type (stmt
),
4024 gimple_assign_rhs1 (stmt
),
4025 gimple_assign_rhs2 (stmt
));
4026 else if (get_gimple_rhs_class (code
) == GIMPLE_SINGLE_RHS
4027 && is_gimple_min_invariant (gimple_assign_rhs1 (stmt
)))
4028 set_value_range_to_value (vr
, gimple_assign_rhs1 (stmt
), NULL
);
4030 set_value_range_to_varying (vr
);
4032 if (vr
->type
== VR_VARYING
)
4033 extract_range_basic (vr
, stmt
);
4036 /* Given a range VR, a LOOP and a variable VAR, determine whether it
4037 would be profitable to adjust VR using scalar evolution information
4038 for VAR. If so, update VR with the new limits. */
4041 adjust_range_with_scev (value_range_t
*vr
, struct loop
*loop
,
4042 gimple stmt
, tree var
)
4044 tree init
, step
, chrec
, tmin
, tmax
, min
, max
, type
, tem
;
4045 enum ev_direction dir
;
4047 /* TODO. Don't adjust anti-ranges. An anti-range may provide
4048 better opportunities than a regular range, but I'm not sure. */
4049 if (vr
->type
== VR_ANTI_RANGE
)
4052 chrec
= instantiate_parameters (loop
, analyze_scalar_evolution (loop
, var
));
4054 /* Like in PR19590, scev can return a constant function. */
4055 if (is_gimple_min_invariant (chrec
))
4057 set_value_range_to_value (vr
, chrec
, vr
->equiv
);
4061 if (TREE_CODE (chrec
) != POLYNOMIAL_CHREC
)
4064 init
= initial_condition_in_loop_num (chrec
, loop
->num
);
4065 tem
= op_with_constant_singleton_value_range (init
);
4068 step
= evolution_part_in_loop_num (chrec
, loop
->num
);
4069 tem
= op_with_constant_singleton_value_range (step
);
4073 /* If STEP is symbolic, we can't know whether INIT will be the
4074 minimum or maximum value in the range. Also, unless INIT is
4075 a simple expression, compare_values and possibly other functions
4076 in tree-vrp won't be able to handle it. */
4077 if (step
== NULL_TREE
4078 || !is_gimple_min_invariant (step
)
4079 || !valid_value_p (init
))
4082 dir
= scev_direction (chrec
);
4083 if (/* Do not adjust ranges if we do not know whether the iv increases
4084 or decreases, ... */
4085 dir
== EV_DIR_UNKNOWN
4086 /* ... or if it may wrap. */
4087 || scev_probably_wraps_p (init
, step
, stmt
, get_chrec_loop (chrec
),
4091 /* We use TYPE_MIN_VALUE and TYPE_MAX_VALUE here instead of
4092 negative_overflow_infinity and positive_overflow_infinity,
4093 because we have concluded that the loop probably does not
4096 type
= TREE_TYPE (var
);
4097 if (POINTER_TYPE_P (type
) || !TYPE_MIN_VALUE (type
))
4098 tmin
= lower_bound_in_type (type
, type
);
4100 tmin
= TYPE_MIN_VALUE (type
);
4101 if (POINTER_TYPE_P (type
) || !TYPE_MAX_VALUE (type
))
4102 tmax
= upper_bound_in_type (type
, type
);
4104 tmax
= TYPE_MAX_VALUE (type
);
4106 /* Try to use estimated number of iterations for the loop to constrain the
4107 final value in the evolution. */
4108 if (TREE_CODE (step
) == INTEGER_CST
4109 && is_gimple_val (init
)
4110 && (TREE_CODE (init
) != SSA_NAME
4111 || get_value_range (init
)->type
== VR_RANGE
))
4115 /* We are only entering here for loop header PHI nodes, so using
4116 the number of latch executions is the correct thing to use. */
4117 if (max_loop_iterations (loop
, &nit
))
4119 value_range_t maxvr
= VR_INITIALIZER
;
4120 signop sgn
= TYPE_SIGN (TREE_TYPE (step
));
4123 widest_int wtmp
= wi::mul (wi::to_widest (step
), nit
, sgn
,
4125 /* If the multiplication overflowed we can't do a meaningful
4126 adjustment. Likewise if the result doesn't fit in the type
4127 of the induction variable. For a signed type we have to
4128 check whether the result has the expected signedness which
4129 is that of the step as number of iterations is unsigned. */
4131 && wi::fits_to_tree_p (wtmp
, TREE_TYPE (init
))
4133 || wi::gts_p (wtmp
, 0) == wi::gts_p (step
, 0)))
4135 tem
= wide_int_to_tree (TREE_TYPE (init
), wtmp
);
4136 extract_range_from_binary_expr (&maxvr
, PLUS_EXPR
,
4137 TREE_TYPE (init
), init
, tem
);
4138 /* Likewise if the addition did. */
4139 if (maxvr
.type
== VR_RANGE
)
4148 if (vr
->type
== VR_VARYING
|| vr
->type
== VR_UNDEFINED
)
4153 /* For VARYING or UNDEFINED ranges, just about anything we get
4154 from scalar evolutions should be better. */
4156 if (dir
== EV_DIR_DECREASES
)
4161 else if (vr
->type
== VR_RANGE
)
4166 if (dir
== EV_DIR_DECREASES
)
4168 /* INIT is the maximum value. If INIT is lower than VR->MAX
4169 but no smaller than VR->MIN, set VR->MAX to INIT. */
4170 if (compare_values (init
, max
) == -1)
4173 /* According to the loop information, the variable does not
4174 overflow. If we think it does, probably because of an
4175 overflow due to arithmetic on a different INF value,
4177 if (is_negative_overflow_infinity (min
)
4178 || compare_values (min
, tmin
) == -1)
4184 /* If INIT is bigger than VR->MIN, set VR->MIN to INIT. */
4185 if (compare_values (init
, min
) == 1)
4188 if (is_positive_overflow_infinity (max
)
4189 || compare_values (tmax
, max
) == -1)
4196 /* If we just created an invalid range with the minimum
4197 greater than the maximum, we fail conservatively.
4198 This should happen only in unreachable
4199 parts of code, or for invalid programs. */
4200 if (compare_values (min
, max
) == 1
4201 || (is_negative_overflow_infinity (min
)
4202 && is_positive_overflow_infinity (max
)))
4205 set_value_range (vr
, VR_RANGE
, min
, max
, vr
->equiv
);
4209 /* Given two numeric value ranges VR0, VR1 and a comparison code COMP:
4211 - Return BOOLEAN_TRUE_NODE if VR0 COMP VR1 always returns true for
4212 all the values in the ranges.
4214 - Return BOOLEAN_FALSE_NODE if the comparison always returns false.
4216 - Return NULL_TREE if it is not always possible to determine the
4217 value of the comparison.
4219 Also set *STRICT_OVERFLOW_P to indicate whether a range with an
4220 overflow infinity was used in the test. */
4224 compare_ranges (enum tree_code comp
, value_range_t
*vr0
, value_range_t
*vr1
,
4225 bool *strict_overflow_p
)
4227 /* VARYING or UNDEFINED ranges cannot be compared. */
4228 if (vr0
->type
== VR_VARYING
4229 || vr0
->type
== VR_UNDEFINED
4230 || vr1
->type
== VR_VARYING
4231 || vr1
->type
== VR_UNDEFINED
)
4234 /* Anti-ranges need to be handled separately. */
4235 if (vr0
->type
== VR_ANTI_RANGE
|| vr1
->type
== VR_ANTI_RANGE
)
4237 /* If both are anti-ranges, then we cannot compute any
4239 if (vr0
->type
== VR_ANTI_RANGE
&& vr1
->type
== VR_ANTI_RANGE
)
4242 /* These comparisons are never statically computable. */
4249 /* Equality can be computed only between a range and an
4250 anti-range. ~[VAL1, VAL2] == [VAL1, VAL2] is always false. */
4251 if (vr0
->type
== VR_RANGE
)
4253 /* To simplify processing, make VR0 the anti-range. */
4254 value_range_t
*tmp
= vr0
;
4259 gcc_assert (comp
== NE_EXPR
|| comp
== EQ_EXPR
);
4261 if (compare_values_warnv (vr0
->min
, vr1
->min
, strict_overflow_p
) == 0
4262 && compare_values_warnv (vr0
->max
, vr1
->max
, strict_overflow_p
) == 0)
4263 return (comp
== NE_EXPR
) ? boolean_true_node
: boolean_false_node
;
4268 if (!usable_range_p (vr0
, strict_overflow_p
)
4269 || !usable_range_p (vr1
, strict_overflow_p
))
4272 /* Simplify processing. If COMP is GT_EXPR or GE_EXPR, switch the
4273 operands around and change the comparison code. */
4274 if (comp
== GT_EXPR
|| comp
== GE_EXPR
)
4277 comp
= (comp
== GT_EXPR
) ? LT_EXPR
: LE_EXPR
;
4283 if (comp
== EQ_EXPR
)
4285 /* Equality may only be computed if both ranges represent
4286 exactly one value. */
4287 if (compare_values_warnv (vr0
->min
, vr0
->max
, strict_overflow_p
) == 0
4288 && compare_values_warnv (vr1
->min
, vr1
->max
, strict_overflow_p
) == 0)
4290 int cmp_min
= compare_values_warnv (vr0
->min
, vr1
->min
,
4292 int cmp_max
= compare_values_warnv (vr0
->max
, vr1
->max
,
4294 if (cmp_min
== 0 && cmp_max
== 0)
4295 return boolean_true_node
;
4296 else if (cmp_min
!= -2 && cmp_max
!= -2)
4297 return boolean_false_node
;
4299 /* If [V0_MIN, V1_MAX] < [V1_MIN, V1_MAX] then V0 != V1. */
4300 else if (compare_values_warnv (vr0
->min
, vr1
->max
,
4301 strict_overflow_p
) == 1
4302 || compare_values_warnv (vr1
->min
, vr0
->max
,
4303 strict_overflow_p
) == 1)
4304 return boolean_false_node
;
4308 else if (comp
== NE_EXPR
)
4312 /* If VR0 is completely to the left or completely to the right
4313 of VR1, they are always different. Notice that we need to
4314 make sure that both comparisons yield similar results to
4315 avoid comparing values that cannot be compared at
4317 cmp1
= compare_values_warnv (vr0
->max
, vr1
->min
, strict_overflow_p
);
4318 cmp2
= compare_values_warnv (vr0
->min
, vr1
->max
, strict_overflow_p
);
4319 if ((cmp1
== -1 && cmp2
== -1) || (cmp1
== 1 && cmp2
== 1))
4320 return boolean_true_node
;
4322 /* If VR0 and VR1 represent a single value and are identical,
4324 else if (compare_values_warnv (vr0
->min
, vr0
->max
,
4325 strict_overflow_p
) == 0
4326 && compare_values_warnv (vr1
->min
, vr1
->max
,
4327 strict_overflow_p
) == 0
4328 && compare_values_warnv (vr0
->min
, vr1
->min
,
4329 strict_overflow_p
) == 0
4330 && compare_values_warnv (vr0
->max
, vr1
->max
,
4331 strict_overflow_p
) == 0)
4332 return boolean_false_node
;
4334 /* Otherwise, they may or may not be different. */
4338 else if (comp
== LT_EXPR
|| comp
== LE_EXPR
)
4342 /* If VR0 is to the left of VR1, return true. */
4343 tst
= compare_values_warnv (vr0
->max
, vr1
->min
, strict_overflow_p
);
4344 if ((comp
== LT_EXPR
&& tst
== -1)
4345 || (comp
== LE_EXPR
&& (tst
== -1 || tst
== 0)))
4347 if (overflow_infinity_range_p (vr0
)
4348 || overflow_infinity_range_p (vr1
))
4349 *strict_overflow_p
= true;
4350 return boolean_true_node
;
4353 /* If VR0 is to the right of VR1, return false. */
4354 tst
= compare_values_warnv (vr0
->min
, vr1
->max
, strict_overflow_p
);
4355 if ((comp
== LT_EXPR
&& (tst
== 0 || tst
== 1))
4356 || (comp
== LE_EXPR
&& tst
== 1))
4358 if (overflow_infinity_range_p (vr0
)
4359 || overflow_infinity_range_p (vr1
))
4360 *strict_overflow_p
= true;
4361 return boolean_false_node
;
4364 /* Otherwise, we don't know. */
4372 /* Given a value range VR, a value VAL and a comparison code COMP, return
4373 BOOLEAN_TRUE_NODE if VR COMP VAL always returns true for all the
4374 values in VR. Return BOOLEAN_FALSE_NODE if the comparison
4375 always returns false. Return NULL_TREE if it is not always
4376 possible to determine the value of the comparison. Also set
4377 *STRICT_OVERFLOW_P to indicate whether a range with an overflow
4378 infinity was used in the test. */
4381 compare_range_with_value (enum tree_code comp
, value_range_t
*vr
, tree val
,
4382 bool *strict_overflow_p
)
4384 if (vr
->type
== VR_VARYING
|| vr
->type
== VR_UNDEFINED
)
4387 /* Anti-ranges need to be handled separately. */
4388 if (vr
->type
== VR_ANTI_RANGE
)
4390 /* For anti-ranges, the only predicates that we can compute at
4391 compile time are equality and inequality. */
4398 /* ~[VAL_1, VAL_2] OP VAL is known if VAL_1 <= VAL <= VAL_2. */
4399 if (value_inside_range (val
, vr
->min
, vr
->max
) == 1)
4400 return (comp
== NE_EXPR
) ? boolean_true_node
: boolean_false_node
;
4405 if (!usable_range_p (vr
, strict_overflow_p
))
4408 if (comp
== EQ_EXPR
)
4410 /* EQ_EXPR may only be computed if VR represents exactly
4412 if (compare_values_warnv (vr
->min
, vr
->max
, strict_overflow_p
) == 0)
4414 int cmp
= compare_values_warnv (vr
->min
, val
, strict_overflow_p
);
4416 return boolean_true_node
;
4417 else if (cmp
== -1 || cmp
== 1 || cmp
== 2)
4418 return boolean_false_node
;
4420 else if (compare_values_warnv (val
, vr
->min
, strict_overflow_p
) == -1
4421 || compare_values_warnv (vr
->max
, val
, strict_overflow_p
) == -1)
4422 return boolean_false_node
;
4426 else if (comp
== NE_EXPR
)
4428 /* If VAL is not inside VR, then they are always different. */
4429 if (compare_values_warnv (vr
->max
, val
, strict_overflow_p
) == -1
4430 || compare_values_warnv (vr
->min
, val
, strict_overflow_p
) == 1)
4431 return boolean_true_node
;
4433 /* If VR represents exactly one value equal to VAL, then return
4435 if (compare_values_warnv (vr
->min
, vr
->max
, strict_overflow_p
) == 0
4436 && compare_values_warnv (vr
->min
, val
, strict_overflow_p
) == 0)
4437 return boolean_false_node
;
4439 /* Otherwise, they may or may not be different. */
4442 else if (comp
== LT_EXPR
|| comp
== LE_EXPR
)
4446 /* If VR is to the left of VAL, return true. */
4447 tst
= compare_values_warnv (vr
->max
, val
, strict_overflow_p
);
4448 if ((comp
== LT_EXPR
&& tst
== -1)
4449 || (comp
== LE_EXPR
&& (tst
== -1 || tst
== 0)))
4451 if (overflow_infinity_range_p (vr
))
4452 *strict_overflow_p
= true;
4453 return boolean_true_node
;
4456 /* If VR is to the right of VAL, return false. */
4457 tst
= compare_values_warnv (vr
->min
, val
, strict_overflow_p
);
4458 if ((comp
== LT_EXPR
&& (tst
== 0 || tst
== 1))
4459 || (comp
== LE_EXPR
&& tst
== 1))
4461 if (overflow_infinity_range_p (vr
))
4462 *strict_overflow_p
= true;
4463 return boolean_false_node
;
4466 /* Otherwise, we don't know. */
4469 else if (comp
== GT_EXPR
|| comp
== GE_EXPR
)
4473 /* If VR is to the right of VAL, return true. */
4474 tst
= compare_values_warnv (vr
->min
, val
, strict_overflow_p
);
4475 if ((comp
== GT_EXPR
&& tst
== 1)
4476 || (comp
== GE_EXPR
&& (tst
== 0 || tst
== 1)))
4478 if (overflow_infinity_range_p (vr
))
4479 *strict_overflow_p
= true;
4480 return boolean_true_node
;
4483 /* If VR is to the left of VAL, return false. */
4484 tst
= compare_values_warnv (vr
->max
, val
, strict_overflow_p
);
4485 if ((comp
== GT_EXPR
&& (tst
== -1 || tst
== 0))
4486 || (comp
== GE_EXPR
&& tst
== -1))
4488 if (overflow_infinity_range_p (vr
))
4489 *strict_overflow_p
= true;
4490 return boolean_false_node
;
4493 /* Otherwise, we don't know. */
4501 /* Debugging dumps. */
4503 void dump_value_range (FILE *, value_range_t
*);
4504 void debug_value_range (value_range_t
*);
4505 void dump_all_value_ranges (FILE *);
4506 void debug_all_value_ranges (void);
4507 void dump_vr_equiv (FILE *, bitmap
);
4508 void debug_vr_equiv (bitmap
);
4511 /* Dump value range VR to FILE. */
4514 dump_value_range (FILE *file
, value_range_t
*vr
)
4517 fprintf (file
, "[]");
4518 else if (vr
->type
== VR_UNDEFINED
)
4519 fprintf (file
, "UNDEFINED");
4520 else if (vr
->type
== VR_RANGE
|| vr
->type
== VR_ANTI_RANGE
)
4522 tree type
= TREE_TYPE (vr
->min
);
4524 fprintf (file
, "%s[", (vr
->type
== VR_ANTI_RANGE
) ? "~" : "");
4526 if (is_negative_overflow_infinity (vr
->min
))
4527 fprintf (file
, "-INF(OVF)");
4528 else if (INTEGRAL_TYPE_P (type
)
4529 && !TYPE_UNSIGNED (type
)
4530 && vrp_val_is_min (vr
->min
))
4531 fprintf (file
, "-INF");
4533 print_generic_expr (file
, vr
->min
, 0);
4535 fprintf (file
, ", ");
4537 if (is_positive_overflow_infinity (vr
->max
))
4538 fprintf (file
, "+INF(OVF)");
4539 else if (INTEGRAL_TYPE_P (type
)
4540 && vrp_val_is_max (vr
->max
))
4541 fprintf (file
, "+INF");
4543 print_generic_expr (file
, vr
->max
, 0);
4545 fprintf (file
, "]");
4552 fprintf (file
, " EQUIVALENCES: { ");
4554 EXECUTE_IF_SET_IN_BITMAP (vr
->equiv
, 0, i
, bi
)
4556 print_generic_expr (file
, ssa_name (i
), 0);
4557 fprintf (file
, " ");
4561 fprintf (file
, "} (%u elements)", c
);
4564 else if (vr
->type
== VR_VARYING
)
4565 fprintf (file
, "VARYING");
4567 fprintf (file
, "INVALID RANGE");
4571 /* Dump value range VR to stderr. */
4574 debug_value_range (value_range_t
*vr
)
4576 dump_value_range (stderr
, vr
);
4577 fprintf (stderr
, "\n");
4581 /* Dump value ranges of all SSA_NAMEs to FILE. */
4584 dump_all_value_ranges (FILE *file
)
4588 for (i
= 0; i
< num_vr_values
; i
++)
4592 print_generic_expr (file
, ssa_name (i
), 0);
4593 fprintf (file
, ": ");
4594 dump_value_range (file
, vr_value
[i
]);
4595 fprintf (file
, "\n");
4599 fprintf (file
, "\n");
4603 /* Dump all value ranges to stderr. */
4606 debug_all_value_ranges (void)
4608 dump_all_value_ranges (stderr
);
4612 /* Given a COND_EXPR COND of the form 'V OP W', and an SSA name V,
4613 create a new SSA name N and return the assertion assignment
4614 'N = ASSERT_EXPR <V, V OP W>'. */
4617 build_assert_expr_for (tree cond
, tree v
)
4622 gcc_assert (TREE_CODE (v
) == SSA_NAME
4623 && COMPARISON_CLASS_P (cond
));
4625 a
= build2 (ASSERT_EXPR
, TREE_TYPE (v
), v
, cond
);
4626 assertion
= gimple_build_assign (NULL_TREE
, a
);
4628 /* The new ASSERT_EXPR, creates a new SSA name that replaces the
4629 operand of the ASSERT_EXPR. Create it so the new name and the old one
4630 are registered in the replacement table so that we can fix the SSA web
4631 after adding all the ASSERT_EXPRs. */
4632 create_new_def_for (v
, assertion
, NULL
);
4638 /* Return false if EXPR is a predicate expression involving floating
4642 fp_predicate (gimple stmt
)
4644 GIMPLE_CHECK (stmt
, GIMPLE_COND
);
4646 return FLOAT_TYPE_P (TREE_TYPE (gimple_cond_lhs (stmt
)));
4649 /* If the range of values taken by OP can be inferred after STMT executes,
4650 return the comparison code (COMP_CODE_P) and value (VAL_P) that
4651 describes the inferred range. Return true if a range could be
4655 infer_value_range (gimple stmt
, tree op
, enum tree_code
*comp_code_p
, tree
*val_p
)
4658 *comp_code_p
= ERROR_MARK
;
4660 /* Do not attempt to infer anything in names that flow through
4662 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op
))
4665 /* Similarly, don't infer anything from statements that may throw
4666 exceptions. ??? Relax this requirement? */
4667 if (stmt_could_throw_p (stmt
))
4670 /* If STMT is the last statement of a basic block with no normal
4671 successors, there is no point inferring anything about any of its
4672 operands. We would not be able to find a proper insertion point
4673 for the assertion, anyway. */
4674 if (stmt_ends_bb_p (stmt
))
4679 FOR_EACH_EDGE (e
, ei
, gimple_bb (stmt
)->succs
)
4680 if (!(e
->flags
& EDGE_ABNORMAL
))
4686 if (infer_nonnull_range (stmt
, op
, true, true))
4688 *val_p
= build_int_cst (TREE_TYPE (op
), 0);
4689 *comp_code_p
= NE_EXPR
;
4697 void dump_asserts_for (FILE *, tree
);
4698 void debug_asserts_for (tree
);
4699 void dump_all_asserts (FILE *);
4700 void debug_all_asserts (void);
4702 /* Dump all the registered assertions for NAME to FILE. */
4705 dump_asserts_for (FILE *file
, tree name
)
4709 fprintf (file
, "Assertions to be inserted for ");
4710 print_generic_expr (file
, name
, 0);
4711 fprintf (file
, "\n");
4713 loc
= asserts_for
[SSA_NAME_VERSION (name
)];
4716 fprintf (file
, "\t");
4717 print_gimple_stmt (file
, gsi_stmt (loc
->si
), 0, 0);
4718 fprintf (file
, "\n\tBB #%d", loc
->bb
->index
);
4721 fprintf (file
, "\n\tEDGE %d->%d", loc
->e
->src
->index
,
4722 loc
->e
->dest
->index
);
4723 dump_edge_info (file
, loc
->e
, dump_flags
, 0);
4725 fprintf (file
, "\n\tPREDICATE: ");
4726 print_generic_expr (file
, name
, 0);
4727 fprintf (file
, " %s ", get_tree_code_name (loc
->comp_code
));
4728 print_generic_expr (file
, loc
->val
, 0);
4729 fprintf (file
, "\n\n");
4733 fprintf (file
, "\n");
4737 /* Dump all the registered assertions for NAME to stderr. */
4740 debug_asserts_for (tree name
)
4742 dump_asserts_for (stderr
, name
);
4746 /* Dump all the registered assertions for all the names to FILE. */
4749 dump_all_asserts (FILE *file
)
4754 fprintf (file
, "\nASSERT_EXPRs to be inserted\n\n");
4755 EXECUTE_IF_SET_IN_BITMAP (need_assert_for
, 0, i
, bi
)
4756 dump_asserts_for (file
, ssa_name (i
));
4757 fprintf (file
, "\n");
4761 /* Dump all the registered assertions for all the names to stderr. */
4764 debug_all_asserts (void)
4766 dump_all_asserts (stderr
);
4770 /* If NAME doesn't have an ASSERT_EXPR registered for asserting
4771 'EXPR COMP_CODE VAL' at a location that dominates block BB or
4772 E->DEST, then register this location as a possible insertion point
4773 for ASSERT_EXPR <NAME, EXPR COMP_CODE VAL>.
4775 BB, E and SI provide the exact insertion point for the new
4776 ASSERT_EXPR. If BB is NULL, then the ASSERT_EXPR is to be inserted
4777 on edge E. Otherwise, if E is NULL, the ASSERT_EXPR is inserted on
4778 BB. If SI points to a COND_EXPR or a SWITCH_EXPR statement, then E
4779 must not be NULL. */
4782 register_new_assert_for (tree name
, tree expr
,
4783 enum tree_code comp_code
,
4787 gimple_stmt_iterator si
)
4789 assert_locus_t n
, loc
, last_loc
;
4790 basic_block dest_bb
;
4792 gcc_checking_assert (bb
== NULL
|| e
== NULL
);
4795 gcc_checking_assert (gimple_code (gsi_stmt (si
)) != GIMPLE_COND
4796 && gimple_code (gsi_stmt (si
)) != GIMPLE_SWITCH
);
4798 /* Never build an assert comparing against an integer constant with
4799 TREE_OVERFLOW set. This confuses our undefined overflow warning
4801 if (TREE_OVERFLOW_P (val
))
4802 val
= drop_tree_overflow (val
);
4804 /* The new assertion A will be inserted at BB or E. We need to
4805 determine if the new location is dominated by a previously
4806 registered location for A. If we are doing an edge insertion,
4807 assume that A will be inserted at E->DEST. Note that this is not
4810 If E is a critical edge, it will be split. But even if E is
4811 split, the new block will dominate the same set of blocks that
4814 The reverse, however, is not true, blocks dominated by E->DEST
4815 will not be dominated by the new block created to split E. So,
4816 if the insertion location is on a critical edge, we will not use
4817 the new location to move another assertion previously registered
4818 at a block dominated by E->DEST. */
4819 dest_bb
= (bb
) ? bb
: e
->dest
;
4821 /* If NAME already has an ASSERT_EXPR registered for COMP_CODE and
4822 VAL at a block dominating DEST_BB, then we don't need to insert a new
4823 one. Similarly, if the same assertion already exists at a block
4824 dominated by DEST_BB and the new location is not on a critical
4825 edge, then update the existing location for the assertion (i.e.,
4826 move the assertion up in the dominance tree).
4828 Note, this is implemented as a simple linked list because there
4829 should not be more than a handful of assertions registered per
4830 name. If this becomes a performance problem, a table hashed by
4831 COMP_CODE and VAL could be implemented. */
4832 loc
= asserts_for
[SSA_NAME_VERSION (name
)];
4836 if (loc
->comp_code
== comp_code
4838 || operand_equal_p (loc
->val
, val
, 0))
4839 && (loc
->expr
== expr
4840 || operand_equal_p (loc
->expr
, expr
, 0)))
4842 /* If E is not a critical edge and DEST_BB
4843 dominates the existing location for the assertion, move
4844 the assertion up in the dominance tree by updating its
4845 location information. */
4846 if ((e
== NULL
|| !EDGE_CRITICAL_P (e
))
4847 && dominated_by_p (CDI_DOMINATORS
, loc
->bb
, dest_bb
))
4856 /* Update the last node of the list and move to the next one. */
4861 /* If we didn't find an assertion already registered for
4862 NAME COMP_CODE VAL, add a new one at the end of the list of
4863 assertions associated with NAME. */
4864 n
= XNEW (struct assert_locus_d
);
4868 n
->comp_code
= comp_code
;
4876 asserts_for
[SSA_NAME_VERSION (name
)] = n
;
4878 bitmap_set_bit (need_assert_for
, SSA_NAME_VERSION (name
));
4881 /* (COND_OP0 COND_CODE COND_OP1) is a predicate which uses NAME.
4882 Extract a suitable test code and value and store them into *CODE_P and
4883 *VAL_P so the predicate is normalized to NAME *CODE_P *VAL_P.
4885 If no extraction was possible, return FALSE, otherwise return TRUE.
4887 If INVERT is true, then we invert the result stored into *CODE_P. */
4890 extract_code_and_val_from_cond_with_ops (tree name
, enum tree_code cond_code
,
4891 tree cond_op0
, tree cond_op1
,
4892 bool invert
, enum tree_code
*code_p
,
4895 enum tree_code comp_code
;
4898 /* Otherwise, we have a comparison of the form NAME COMP VAL
4899 or VAL COMP NAME. */
4900 if (name
== cond_op1
)
4902 /* If the predicate is of the form VAL COMP NAME, flip
4903 COMP around because we need to register NAME as the
4904 first operand in the predicate. */
4905 comp_code
= swap_tree_comparison (cond_code
);
4910 /* The comparison is of the form NAME COMP VAL, so the
4911 comparison code remains unchanged. */
4912 comp_code
= cond_code
;
4916 /* Invert the comparison code as necessary. */
4918 comp_code
= invert_tree_comparison (comp_code
, 0);
4920 /* VRP does not handle float types. */
4921 if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (val
)))
4924 /* Do not register always-false predicates.
4925 FIXME: this works around a limitation in fold() when dealing with
4926 enumerations. Given 'enum { N1, N2 } x;', fold will not
4927 fold 'if (x > N2)' to 'if (0)'. */
4928 if ((comp_code
== GT_EXPR
|| comp_code
== LT_EXPR
)
4929 && INTEGRAL_TYPE_P (TREE_TYPE (val
)))
4931 tree min
= TYPE_MIN_VALUE (TREE_TYPE (val
));
4932 tree max
= TYPE_MAX_VALUE (TREE_TYPE (val
));
4934 if (comp_code
== GT_EXPR
4936 || compare_values (val
, max
) == 0))
4939 if (comp_code
== LT_EXPR
4941 || compare_values (val
, min
) == 0))
4944 *code_p
= comp_code
;
4949 /* Find out smallest RES where RES > VAL && (RES & MASK) == RES, if any
4950 (otherwise return VAL). VAL and MASK must be zero-extended for
4951 precision PREC. If SGNBIT is non-zero, first xor VAL with SGNBIT
4952 (to transform signed values into unsigned) and at the end xor
4956 masked_increment (const wide_int
&val_in
, const wide_int
&mask
,
4957 const wide_int
&sgnbit
, unsigned int prec
)
4959 wide_int bit
= wi::one (prec
), res
;
4962 wide_int val
= val_in
^ sgnbit
;
4963 for (i
= 0; i
< prec
; i
++, bit
+= bit
)
4966 if ((res
& bit
) == 0)
4969 res
= (val
+ bit
).and_not (res
);
4971 if (wi::gtu_p (res
, val
))
4972 return res
^ sgnbit
;
4974 return val
^ sgnbit
;
4977 /* Try to register an edge assertion for SSA name NAME on edge E for
4978 the condition COND contributing to the conditional jump pointed to by BSI.
4979 Invert the condition COND if INVERT is true.
4980 Return true if an assertion for NAME could be registered. */
4983 register_edge_assert_for_2 (tree name
, edge e
, gimple_stmt_iterator bsi
,
4984 enum tree_code cond_code
,
4985 tree cond_op0
, tree cond_op1
, bool invert
)
4988 enum tree_code comp_code
;
4989 bool retval
= false;
4991 if (!extract_code_and_val_from_cond_with_ops (name
, cond_code
,
4994 invert
, &comp_code
, &val
))
4997 /* Only register an ASSERT_EXPR if NAME was found in the sub-graph
4998 reachable from E. */
4999 if (live_on_edge (e
, name
)
5000 && !has_single_use (name
))
5002 register_new_assert_for (name
, name
, comp_code
, val
, NULL
, e
, bsi
);
5006 /* In the case of NAME <= CST and NAME being defined as
5007 NAME = (unsigned) NAME2 + CST2 we can assert NAME2 >= -CST2
5008 and NAME2 <= CST - CST2. We can do the same for NAME > CST.
5009 This catches range and anti-range tests. */
5010 if ((comp_code
== LE_EXPR
5011 || comp_code
== GT_EXPR
)
5012 && TREE_CODE (val
) == INTEGER_CST
5013 && TYPE_UNSIGNED (TREE_TYPE (val
)))
5015 gimple def_stmt
= SSA_NAME_DEF_STMT (name
);
5016 tree cst2
= NULL_TREE
, name2
= NULL_TREE
, name3
= NULL_TREE
;
5018 /* Extract CST2 from the (optional) addition. */
5019 if (is_gimple_assign (def_stmt
)
5020 && gimple_assign_rhs_code (def_stmt
) == PLUS_EXPR
)
5022 name2
= gimple_assign_rhs1 (def_stmt
);
5023 cst2
= gimple_assign_rhs2 (def_stmt
);
5024 if (TREE_CODE (name2
) == SSA_NAME
5025 && TREE_CODE (cst2
) == INTEGER_CST
)
5026 def_stmt
= SSA_NAME_DEF_STMT (name2
);
5029 /* Extract NAME2 from the (optional) sign-changing cast. */
5030 if (gimple_assign_cast_p (def_stmt
))
5032 if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt
))
5033 && ! TYPE_UNSIGNED (TREE_TYPE (gimple_assign_rhs1 (def_stmt
)))
5034 && (TYPE_PRECISION (gimple_expr_type (def_stmt
))
5035 == TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (def_stmt
)))))
5036 name3
= gimple_assign_rhs1 (def_stmt
);
5039 /* If name3 is used later, create an ASSERT_EXPR for it. */
5040 if (name3
!= NULL_TREE
5041 && TREE_CODE (name3
) == SSA_NAME
5042 && (cst2
== NULL_TREE
5043 || TREE_CODE (cst2
) == INTEGER_CST
)
5044 && INTEGRAL_TYPE_P (TREE_TYPE (name3
))
5045 && live_on_edge (e
, name3
)
5046 && !has_single_use (name3
))
5050 /* Build an expression for the range test. */
5051 tmp
= build1 (NOP_EXPR
, TREE_TYPE (name
), name3
);
5052 if (cst2
!= NULL_TREE
)
5053 tmp
= build2 (PLUS_EXPR
, TREE_TYPE (name
), tmp
, cst2
);
5057 fprintf (dump_file
, "Adding assert for ");
5058 print_generic_expr (dump_file
, name3
, 0);
5059 fprintf (dump_file
, " from ");
5060 print_generic_expr (dump_file
, tmp
, 0);
5061 fprintf (dump_file
, "\n");
5064 register_new_assert_for (name3
, tmp
, comp_code
, val
, NULL
, e
, bsi
);
5069 /* If name2 is used later, create an ASSERT_EXPR for it. */
5070 if (name2
!= NULL_TREE
5071 && TREE_CODE (name2
) == SSA_NAME
5072 && TREE_CODE (cst2
) == INTEGER_CST
5073 && INTEGRAL_TYPE_P (TREE_TYPE (name2
))
5074 && live_on_edge (e
, name2
)
5075 && !has_single_use (name2
))
5079 /* Build an expression for the range test. */
5081 if (TREE_TYPE (name
) != TREE_TYPE (name2
))
5082 tmp
= build1 (NOP_EXPR
, TREE_TYPE (name
), tmp
);
5083 if (cst2
!= NULL_TREE
)
5084 tmp
= build2 (PLUS_EXPR
, TREE_TYPE (name
), tmp
, cst2
);
5088 fprintf (dump_file
, "Adding assert for ");
5089 print_generic_expr (dump_file
, name2
, 0);
5090 fprintf (dump_file
, " from ");
5091 print_generic_expr (dump_file
, tmp
, 0);
5092 fprintf (dump_file
, "\n");
5095 register_new_assert_for (name2
, tmp
, comp_code
, val
, NULL
, e
, bsi
);
5101 /* In the case of post-in/decrement tests like if (i++) ... and uses
5102 of the in/decremented value on the edge the extra name we want to
5103 assert for is not on the def chain of the name compared. Instead
5104 it is in the set of use stmts. */
5105 if ((comp_code
== NE_EXPR
5106 || comp_code
== EQ_EXPR
)
5107 && TREE_CODE (val
) == INTEGER_CST
)
5109 imm_use_iterator ui
;
5111 FOR_EACH_IMM_USE_STMT (use_stmt
, ui
, name
)
5113 /* Cut off to use-stmts that are in the predecessor. */
5114 if (gimple_bb (use_stmt
) != e
->src
)
5117 if (!is_gimple_assign (use_stmt
))
5120 enum tree_code code
= gimple_assign_rhs_code (use_stmt
);
5121 if (code
!= PLUS_EXPR
5122 && code
!= MINUS_EXPR
)
5125 tree cst
= gimple_assign_rhs2 (use_stmt
);
5126 if (TREE_CODE (cst
) != INTEGER_CST
)
5129 tree name2
= gimple_assign_lhs (use_stmt
);
5130 if (live_on_edge (e
, name2
))
5132 cst
= int_const_binop (code
, val
, cst
);
5133 register_new_assert_for (name2
, name2
, comp_code
, cst
,
5140 if (TREE_CODE_CLASS (comp_code
) == tcc_comparison
5141 && TREE_CODE (val
) == INTEGER_CST
)
5143 gimple def_stmt
= SSA_NAME_DEF_STMT (name
);
5144 tree name2
= NULL_TREE
, names
[2], cst2
= NULL_TREE
;
5145 tree val2
= NULL_TREE
;
5146 unsigned int prec
= TYPE_PRECISION (TREE_TYPE (val
));
5147 wide_int mask
= wi::zero (prec
);
5148 unsigned int nprec
= prec
;
5149 enum tree_code rhs_code
= ERROR_MARK
;
5151 if (is_gimple_assign (def_stmt
))
5152 rhs_code
= gimple_assign_rhs_code (def_stmt
);
5154 /* Add asserts for NAME cmp CST and NAME being defined
5155 as NAME = (int) NAME2. */
5156 if (!TYPE_UNSIGNED (TREE_TYPE (val
))
5157 && (comp_code
== LE_EXPR
|| comp_code
== LT_EXPR
5158 || comp_code
== GT_EXPR
|| comp_code
== GE_EXPR
)
5159 && gimple_assign_cast_p (def_stmt
))
5161 name2
= gimple_assign_rhs1 (def_stmt
);
5162 if (CONVERT_EXPR_CODE_P (rhs_code
)
5163 && INTEGRAL_TYPE_P (TREE_TYPE (name2
))
5164 && TYPE_UNSIGNED (TREE_TYPE (name2
))
5165 && prec
== TYPE_PRECISION (TREE_TYPE (name2
))
5166 && (comp_code
== LE_EXPR
|| comp_code
== GT_EXPR
5167 || !tree_int_cst_equal (val
,
5168 TYPE_MIN_VALUE (TREE_TYPE (val
))))
5169 && live_on_edge (e
, name2
)
5170 && !has_single_use (name2
))
5173 enum tree_code new_comp_code
= comp_code
;
5175 cst
= fold_convert (TREE_TYPE (name2
),
5176 TYPE_MIN_VALUE (TREE_TYPE (val
)));
5177 /* Build an expression for the range test. */
5178 tmp
= build2 (PLUS_EXPR
, TREE_TYPE (name2
), name2
, cst
);
5179 cst
= fold_build2 (PLUS_EXPR
, TREE_TYPE (name2
), cst
,
5180 fold_convert (TREE_TYPE (name2
), val
));
5181 if (comp_code
== LT_EXPR
|| comp_code
== GE_EXPR
)
5183 new_comp_code
= comp_code
== LT_EXPR
? LE_EXPR
: GT_EXPR
;
5184 cst
= fold_build2 (MINUS_EXPR
, TREE_TYPE (name2
), cst
,
5185 build_int_cst (TREE_TYPE (name2
), 1));
5190 fprintf (dump_file
, "Adding assert for ");
5191 print_generic_expr (dump_file
, name2
, 0);
5192 fprintf (dump_file
, " from ");
5193 print_generic_expr (dump_file
, tmp
, 0);
5194 fprintf (dump_file
, "\n");
5197 register_new_assert_for (name2
, tmp
, new_comp_code
, cst
, NULL
,
5204 /* Add asserts for NAME cmp CST and NAME being defined as
5205 NAME = NAME2 >> CST2.
5207 Extract CST2 from the right shift. */
5208 if (rhs_code
== RSHIFT_EXPR
)
5210 name2
= gimple_assign_rhs1 (def_stmt
);
5211 cst2
= gimple_assign_rhs2 (def_stmt
);
5212 if (TREE_CODE (name2
) == SSA_NAME
5213 && tree_fits_uhwi_p (cst2
)
5214 && INTEGRAL_TYPE_P (TREE_TYPE (name2
))
5215 && IN_RANGE (tree_to_uhwi (cst2
), 1, prec
- 1)
5216 && prec
== GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (val
)))
5217 && live_on_edge (e
, name2
)
5218 && !has_single_use (name2
))
5220 mask
= wi::mask (tree_to_uhwi (cst2
), false, prec
);
5221 val2
= fold_binary (LSHIFT_EXPR
, TREE_TYPE (val
), val
, cst2
);
5224 if (val2
!= NULL_TREE
5225 && TREE_CODE (val2
) == INTEGER_CST
5226 && simple_cst_equal (fold_build2 (RSHIFT_EXPR
,
5230 enum tree_code new_comp_code
= comp_code
;
5234 if (comp_code
== EQ_EXPR
|| comp_code
== NE_EXPR
)
5236 if (!TYPE_UNSIGNED (TREE_TYPE (val
)))
5238 tree type
= build_nonstandard_integer_type (prec
, 1);
5239 tmp
= build1 (NOP_EXPR
, type
, name2
);
5240 val2
= fold_convert (type
, val2
);
5242 tmp
= fold_build2 (MINUS_EXPR
, TREE_TYPE (tmp
), tmp
, val2
);
5243 new_val
= wide_int_to_tree (TREE_TYPE (tmp
), mask
);
5244 new_comp_code
= comp_code
== EQ_EXPR
? LE_EXPR
: GT_EXPR
;
5246 else if (comp_code
== LT_EXPR
|| comp_code
== GE_EXPR
)
5249 = wi::min_value (prec
, TYPE_SIGN (TREE_TYPE (val
)));
5251 if (minval
== new_val
)
5252 new_val
= NULL_TREE
;
5257 = wi::max_value (prec
, TYPE_SIGN (TREE_TYPE (val
)));
5260 new_val
= NULL_TREE
;
5262 new_val
= wide_int_to_tree (TREE_TYPE (val2
), mask
);
5269 fprintf (dump_file
, "Adding assert for ");
5270 print_generic_expr (dump_file
, name2
, 0);
5271 fprintf (dump_file
, " from ");
5272 print_generic_expr (dump_file
, tmp
, 0);
5273 fprintf (dump_file
, "\n");
5276 register_new_assert_for (name2
, tmp
, new_comp_code
, new_val
,
5282 /* Add asserts for NAME cmp CST and NAME being defined as
5283 NAME = NAME2 & CST2.
5285 Extract CST2 from the and.
5288 NAME = (unsigned) NAME2;
5289 casts where NAME's type is unsigned and has smaller precision
5290 than NAME2's type as if it was NAME = NAME2 & MASK. */
5291 names
[0] = NULL_TREE
;
5292 names
[1] = NULL_TREE
;
5294 if (rhs_code
== BIT_AND_EXPR
5295 || (CONVERT_EXPR_CODE_P (rhs_code
)
5296 && TREE_CODE (TREE_TYPE (val
)) == INTEGER_TYPE
5297 && TYPE_UNSIGNED (TREE_TYPE (val
))
5298 && TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (def_stmt
)))
5302 name2
= gimple_assign_rhs1 (def_stmt
);
5303 if (rhs_code
== BIT_AND_EXPR
)
5304 cst2
= gimple_assign_rhs2 (def_stmt
);
5307 cst2
= TYPE_MAX_VALUE (TREE_TYPE (val
));
5308 nprec
= TYPE_PRECISION (TREE_TYPE (name2
));
5310 if (TREE_CODE (name2
) == SSA_NAME
5311 && INTEGRAL_TYPE_P (TREE_TYPE (name2
))
5312 && TREE_CODE (cst2
) == INTEGER_CST
5313 && !integer_zerop (cst2
)
5315 || TYPE_UNSIGNED (TREE_TYPE (val
))))
5317 gimple def_stmt2
= SSA_NAME_DEF_STMT (name2
);
5318 if (gimple_assign_cast_p (def_stmt2
))
5320 names
[1] = gimple_assign_rhs1 (def_stmt2
);
5321 if (!CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt2
))
5322 || !INTEGRAL_TYPE_P (TREE_TYPE (names
[1]))
5323 || (TYPE_PRECISION (TREE_TYPE (name2
))
5324 != TYPE_PRECISION (TREE_TYPE (names
[1])))
5325 || !live_on_edge (e
, names
[1])
5326 || has_single_use (names
[1]))
5327 names
[1] = NULL_TREE
;
5329 if (live_on_edge (e
, name2
)
5330 && !has_single_use (name2
))
5334 if (names
[0] || names
[1])
5336 wide_int minv
, maxv
, valv
, cst2v
;
5337 wide_int tem
, sgnbit
;
5338 bool valid_p
= false, valn
, cst2n
;
5339 enum tree_code ccode
= comp_code
;
5341 valv
= wide_int::from (val
, nprec
, UNSIGNED
);
5342 cst2v
= wide_int::from (cst2
, nprec
, UNSIGNED
);
5343 valn
= wi::neg_p (valv
, TYPE_SIGN (TREE_TYPE (val
)));
5344 cst2n
= wi::neg_p (cst2v
, TYPE_SIGN (TREE_TYPE (val
)));
5345 /* If CST2 doesn't have most significant bit set,
5346 but VAL is negative, we have comparison like
5347 if ((x & 0x123) > -4) (always true). Just give up. */
5351 sgnbit
= wi::set_bit_in_zero (nprec
- 1, nprec
);
5353 sgnbit
= wi::zero (nprec
);
5354 minv
= valv
& cst2v
;
5358 /* Minimum unsigned value for equality is VAL & CST2
5359 (should be equal to VAL, otherwise we probably should
5360 have folded the comparison into false) and
5361 maximum unsigned value is VAL | ~CST2. */
5362 maxv
= valv
| ~cst2v
;
5367 tem
= valv
| ~cst2v
;
5368 /* If VAL is 0, handle (X & CST2) != 0 as (X & CST2) > 0U. */
5372 sgnbit
= wi::zero (nprec
);
5375 /* If (VAL | ~CST2) is all ones, handle it as
5376 (X & CST2) < VAL. */
5381 sgnbit
= wi::zero (nprec
);
5384 if (!cst2n
&& wi::neg_p (cst2v
))
5385 sgnbit
= wi::set_bit_in_zero (nprec
- 1, nprec
);
5394 if (tem
== wi::mask (nprec
- 1, false, nprec
))
5400 sgnbit
= wi::zero (nprec
);
5405 /* Minimum unsigned value for >= if (VAL & CST2) == VAL
5406 is VAL and maximum unsigned value is ~0. For signed
5407 comparison, if CST2 doesn't have most significant bit
5408 set, handle it similarly. If CST2 has MSB set,
5409 the minimum is the same, and maximum is ~0U/2. */
5412 /* If (VAL & CST2) != VAL, X & CST2 can't be equal to
5414 minv
= masked_increment (valv
, cst2v
, sgnbit
, nprec
);
5418 maxv
= wi::mask (nprec
- (cst2n
? 1 : 0), false, nprec
);
5424 /* Find out smallest MINV where MINV > VAL
5425 && (MINV & CST2) == MINV, if any. If VAL is signed and
5426 CST2 has MSB set, compute it biased by 1 << (nprec - 1). */
5427 minv
= masked_increment (valv
, cst2v
, sgnbit
, nprec
);
5430 maxv
= wi::mask (nprec
- (cst2n
? 1 : 0), false, nprec
);
5435 /* Minimum unsigned value for <= is 0 and maximum
5436 unsigned value is VAL | ~CST2 if (VAL & CST2) == VAL.
5437 Otherwise, find smallest VAL2 where VAL2 > VAL
5438 && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
5440 For signed comparison, if CST2 doesn't have most
5441 significant bit set, handle it similarly. If CST2 has
5442 MSB set, the maximum is the same and minimum is INT_MIN. */
5447 maxv
= masked_increment (valv
, cst2v
, sgnbit
, nprec
);
5459 /* Minimum unsigned value for < is 0 and maximum
5460 unsigned value is (VAL-1) | ~CST2 if (VAL & CST2) == VAL.
5461 Otherwise, find smallest VAL2 where VAL2 > VAL
5462 && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
5464 For signed comparison, if CST2 doesn't have most
5465 significant bit set, handle it similarly. If CST2 has
5466 MSB set, the maximum is the same and minimum is INT_MIN. */
5475 maxv
= masked_increment (valv
, cst2v
, sgnbit
, nprec
);
5489 && (maxv
- minv
) != -1)
5491 tree tmp
, new_val
, type
;
5494 for (i
= 0; i
< 2; i
++)
5497 wide_int maxv2
= maxv
;
5499 type
= TREE_TYPE (names
[i
]);
5500 if (!TYPE_UNSIGNED (type
))
5502 type
= build_nonstandard_integer_type (nprec
, 1);
5503 tmp
= build1 (NOP_EXPR
, type
, names
[i
]);
5507 tmp
= build2 (PLUS_EXPR
, type
, tmp
,
5508 wide_int_to_tree (type
, -minv
));
5509 maxv2
= maxv
- minv
;
5511 new_val
= wide_int_to_tree (type
, maxv2
);
5515 fprintf (dump_file
, "Adding assert for ");
5516 print_generic_expr (dump_file
, names
[i
], 0);
5517 fprintf (dump_file
, " from ");
5518 print_generic_expr (dump_file
, tmp
, 0);
5519 fprintf (dump_file
, "\n");
5522 register_new_assert_for (names
[i
], tmp
, LE_EXPR
,
5523 new_val
, NULL
, e
, bsi
);
5533 /* OP is an operand of a truth value expression which is known to have
5534 a particular value. Register any asserts for OP and for any
5535 operands in OP's defining statement.
5537 If CODE is EQ_EXPR, then we want to register OP is zero (false),
5538 if CODE is NE_EXPR, then we want to register OP is nonzero (true). */
5541 register_edge_assert_for_1 (tree op
, enum tree_code code
,
5542 edge e
, gimple_stmt_iterator bsi
)
5544 bool retval
= false;
5547 enum tree_code rhs_code
;
5549 /* We only care about SSA_NAMEs. */
5550 if (TREE_CODE (op
) != SSA_NAME
)
5553 /* We know that OP will have a zero or nonzero value. If OP is used
5554 more than once go ahead and register an assert for OP. */
5555 if (live_on_edge (e
, op
)
5556 && !has_single_use (op
))
5558 val
= build_int_cst (TREE_TYPE (op
), 0);
5559 register_new_assert_for (op
, op
, code
, val
, NULL
, e
, bsi
);
5563 /* Now look at how OP is set. If it's set from a comparison,
5564 a truth operation or some bit operations, then we may be able
5565 to register information about the operands of that assignment. */
5566 op_def
= SSA_NAME_DEF_STMT (op
);
5567 if (gimple_code (op_def
) != GIMPLE_ASSIGN
)
5570 rhs_code
= gimple_assign_rhs_code (op_def
);
5572 if (TREE_CODE_CLASS (rhs_code
) == tcc_comparison
)
5574 bool invert
= (code
== EQ_EXPR
? true : false);
5575 tree op0
= gimple_assign_rhs1 (op_def
);
5576 tree op1
= gimple_assign_rhs2 (op_def
);
5578 if (TREE_CODE (op0
) == SSA_NAME
)
5579 retval
|= register_edge_assert_for_2 (op0
, e
, bsi
, rhs_code
, op0
, op1
,
5581 if (TREE_CODE (op1
) == SSA_NAME
)
5582 retval
|= register_edge_assert_for_2 (op1
, e
, bsi
, rhs_code
, op0
, op1
,
5585 else if ((code
== NE_EXPR
5586 && gimple_assign_rhs_code (op_def
) == BIT_AND_EXPR
)
5588 && gimple_assign_rhs_code (op_def
) == BIT_IOR_EXPR
))
5590 /* Recurse on each operand. */
5591 tree op0
= gimple_assign_rhs1 (op_def
);
5592 tree op1
= gimple_assign_rhs2 (op_def
);
5593 if (TREE_CODE (op0
) == SSA_NAME
5594 && has_single_use (op0
))
5595 retval
|= register_edge_assert_for_1 (op0
, code
, e
, bsi
);
5596 if (TREE_CODE (op1
) == SSA_NAME
5597 && has_single_use (op1
))
5598 retval
|= register_edge_assert_for_1 (op1
, code
, e
, bsi
);
5600 else if (gimple_assign_rhs_code (op_def
) == BIT_NOT_EXPR
5601 && TYPE_PRECISION (TREE_TYPE (gimple_assign_lhs (op_def
))) == 1)
5603 /* Recurse, flipping CODE. */
5604 code
= invert_tree_comparison (code
, false);
5605 retval
|= register_edge_assert_for_1 (gimple_assign_rhs1 (op_def
),
5608 else if (gimple_assign_rhs_code (op_def
) == SSA_NAME
)
5610 /* Recurse through the copy. */
5611 retval
|= register_edge_assert_for_1 (gimple_assign_rhs1 (op_def
),
5614 else if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (op_def
)))
5616 /* Recurse through the type conversion, unless it is a narrowing
5617 conversion or conversion from non-integral type. */
5618 tree rhs
= gimple_assign_rhs1 (op_def
);
5619 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs
))
5620 && (TYPE_PRECISION (TREE_TYPE (rhs
))
5621 <= TYPE_PRECISION (TREE_TYPE (op
))))
5622 retval
|= register_edge_assert_for_1 (rhs
, code
, e
, bsi
);
5628 /* Try to register an edge assertion for SSA name NAME on edge E for
5629 the condition COND contributing to the conditional jump pointed to by SI.
5630 Return true if an assertion for NAME could be registered. */
5633 register_edge_assert_for (tree name
, edge e
, gimple_stmt_iterator si
,
5634 enum tree_code cond_code
, tree cond_op0
,
5638 enum tree_code comp_code
;
5639 bool retval
= false;
5640 bool is_else_edge
= (e
->flags
& EDGE_FALSE_VALUE
) != 0;
5642 /* Do not attempt to infer anything in names that flow through
5644 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name
))
5647 if (!extract_code_and_val_from_cond_with_ops (name
, cond_code
,
5653 /* Register ASSERT_EXPRs for name. */
5654 retval
|= register_edge_assert_for_2 (name
, e
, si
, cond_code
, cond_op0
,
5655 cond_op1
, is_else_edge
);
5658 /* If COND is effectively an equality test of an SSA_NAME against
5659 the value zero or one, then we may be able to assert values
5660 for SSA_NAMEs which flow into COND. */
5662 /* In the case of NAME == 1 or NAME != 0, for BIT_AND_EXPR defining
5663 statement of NAME we can assert both operands of the BIT_AND_EXPR
5664 have nonzero value. */
5665 if (((comp_code
== EQ_EXPR
&& integer_onep (val
))
5666 || (comp_code
== NE_EXPR
&& integer_zerop (val
))))
5668 gimple def_stmt
= SSA_NAME_DEF_STMT (name
);
5670 if (is_gimple_assign (def_stmt
)
5671 && gimple_assign_rhs_code (def_stmt
) == BIT_AND_EXPR
)
5673 tree op0
= gimple_assign_rhs1 (def_stmt
);
5674 tree op1
= gimple_assign_rhs2 (def_stmt
);
5675 retval
|= register_edge_assert_for_1 (op0
, NE_EXPR
, e
, si
);
5676 retval
|= register_edge_assert_for_1 (op1
, NE_EXPR
, e
, si
);
5680 /* In the case of NAME == 0 or NAME != 1, for BIT_IOR_EXPR defining
5681 statement of NAME we can assert both operands of the BIT_IOR_EXPR
5683 if (((comp_code
== EQ_EXPR
&& integer_zerop (val
))
5684 || (comp_code
== NE_EXPR
&& integer_onep (val
))))
5686 gimple def_stmt
= SSA_NAME_DEF_STMT (name
);
5688 /* For BIT_IOR_EXPR only if NAME == 0 both operands have
5689 necessarily zero value, or if type-precision is one. */
5690 if (is_gimple_assign (def_stmt
)
5691 && (gimple_assign_rhs_code (def_stmt
) == BIT_IOR_EXPR
5692 && (TYPE_PRECISION (TREE_TYPE (name
)) == 1
5693 || comp_code
== EQ_EXPR
)))
5695 tree op0
= gimple_assign_rhs1 (def_stmt
);
5696 tree op1
= gimple_assign_rhs2 (def_stmt
);
5697 retval
|= register_edge_assert_for_1 (op0
, EQ_EXPR
, e
, si
);
5698 retval
|= register_edge_assert_for_1 (op1
, EQ_EXPR
, e
, si
);
5706 /* Determine whether the outgoing edges of BB should receive an
5707 ASSERT_EXPR for each of the operands of BB's LAST statement.
5708 The last statement of BB must be a COND_EXPR.
5710 If any of the sub-graphs rooted at BB have an interesting use of
5711 the predicate operands, an assert location node is added to the
5712 list of assertions for the corresponding operands. */
5715 find_conditional_asserts (basic_block bb
, gimple last
)
5718 gimple_stmt_iterator bsi
;
5724 need_assert
= false;
5725 bsi
= gsi_for_stmt (last
);
5727 /* Look for uses of the operands in each of the sub-graphs
5728 rooted at BB. We need to check each of the outgoing edges
5729 separately, so that we know what kind of ASSERT_EXPR to
5731 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
5736 /* Register the necessary assertions for each operand in the
5737 conditional predicate. */
5738 FOR_EACH_SSA_TREE_OPERAND (op
, last
, iter
, SSA_OP_USE
)
5740 need_assert
|= register_edge_assert_for (op
, e
, bsi
,
5741 gimple_cond_code (last
),
5742 gimple_cond_lhs (last
),
5743 gimple_cond_rhs (last
));
5756 /* Compare two case labels sorting first by the destination bb index
5757 and then by the case value. */
5760 compare_case_labels (const void *p1
, const void *p2
)
5762 const struct case_info
*ci1
= (const struct case_info
*) p1
;
5763 const struct case_info
*ci2
= (const struct case_info
*) p2
;
5764 int idx1
= ci1
->bb
->index
;
5765 int idx2
= ci2
->bb
->index
;
5769 else if (idx1
== idx2
)
5771 /* Make sure the default label is first in a group. */
5772 if (!CASE_LOW (ci1
->expr
))
5774 else if (!CASE_LOW (ci2
->expr
))
5777 return tree_int_cst_compare (CASE_LOW (ci1
->expr
),
5778 CASE_LOW (ci2
->expr
));
5784 /* Determine whether the outgoing edges of BB should receive an
5785 ASSERT_EXPR for each of the operands of BB's LAST statement.
5786 The last statement of BB must be a SWITCH_EXPR.
5788 If any of the sub-graphs rooted at BB have an interesting use of
5789 the predicate operands, an assert location node is added to the
5790 list of assertions for the corresponding operands. */
5793 find_switch_asserts (basic_block bb
, gimple last
)
5796 gimple_stmt_iterator bsi
;
5799 struct case_info
*ci
;
5800 size_t n
= gimple_switch_num_labels (last
);
5801 #if GCC_VERSION >= 4000
5804 /* Work around GCC 3.4 bug (PR 37086). */
5805 volatile unsigned int idx
;
5808 need_assert
= false;
5809 bsi
= gsi_for_stmt (last
);
5810 op
= gimple_switch_index (last
);
5811 if (TREE_CODE (op
) != SSA_NAME
)
5814 /* Build a vector of case labels sorted by destination label. */
5815 ci
= XNEWVEC (struct case_info
, n
);
5816 for (idx
= 0; idx
< n
; ++idx
)
5818 ci
[idx
].expr
= gimple_switch_label (last
, idx
);
5819 ci
[idx
].bb
= label_to_block (CASE_LABEL (ci
[idx
].expr
));
5821 qsort (ci
, n
, sizeof (struct case_info
), compare_case_labels
);
5823 for (idx
= 0; idx
< n
; ++idx
)
5826 tree cl
= ci
[idx
].expr
;
5827 basic_block cbb
= ci
[idx
].bb
;
5829 min
= CASE_LOW (cl
);
5830 max
= CASE_HIGH (cl
);
5832 /* If there are multiple case labels with the same destination
5833 we need to combine them to a single value range for the edge. */
5834 if (idx
+ 1 < n
&& cbb
== ci
[idx
+ 1].bb
)
5836 /* Skip labels until the last of the group. */
5839 } while (idx
< n
&& cbb
== ci
[idx
].bb
);
5842 /* Pick up the maximum of the case label range. */
5843 if (CASE_HIGH (ci
[idx
].expr
))
5844 max
= CASE_HIGH (ci
[idx
].expr
);
5846 max
= CASE_LOW (ci
[idx
].expr
);
5849 /* Nothing to do if the range includes the default label until we
5850 can register anti-ranges. */
5851 if (min
== NULL_TREE
)
5854 /* Find the edge to register the assert expr on. */
5855 e
= find_edge (bb
, cbb
);
5857 /* Register the necessary assertions for the operand in the
5859 need_assert
|= register_edge_assert_for (op
, e
, bsi
,
5860 max
? GE_EXPR
: EQ_EXPR
,
5862 fold_convert (TREE_TYPE (op
),
5866 need_assert
|= register_edge_assert_for (op
, e
, bsi
, LE_EXPR
,
5868 fold_convert (TREE_TYPE (op
),
5878 /* Traverse all the statements in block BB looking for statements that
5879 may generate useful assertions for the SSA names in their operand.
5880 If a statement produces a useful assertion A for name N_i, then the
5881 list of assertions already generated for N_i is scanned to
5882 determine if A is actually needed.
5884 If N_i already had the assertion A at a location dominating the
5885 current location, then nothing needs to be done. Otherwise, the
5886 new location for A is recorded instead.
5888 1- For every statement S in BB, all the variables used by S are
5889 added to bitmap FOUND_IN_SUBGRAPH.
5891 2- If statement S uses an operand N in a way that exposes a known
5892 value range for N, then if N was not already generated by an
5893 ASSERT_EXPR, create a new assert location for N. For instance,
5894 if N is a pointer and the statement dereferences it, we can
5895 assume that N is not NULL.
5897 3- COND_EXPRs are a special case of #2. We can derive range
5898 information from the predicate but need to insert different
5899 ASSERT_EXPRs for each of the sub-graphs rooted at the
5900 conditional block. If the last statement of BB is a conditional
5901 expression of the form 'X op Y', then
5903 a) Remove X and Y from the set FOUND_IN_SUBGRAPH.
5905 b) If the conditional is the only entry point to the sub-graph
5906 corresponding to the THEN_CLAUSE, recurse into it. On
5907 return, if X and/or Y are marked in FOUND_IN_SUBGRAPH, then
5908 an ASSERT_EXPR is added for the corresponding variable.
5910 c) Repeat step (b) on the ELSE_CLAUSE.
5912 d) Mark X and Y in FOUND_IN_SUBGRAPH.
5921 In this case, an assertion on the THEN clause is useful to
5922 determine that 'a' is always 9 on that edge. However, an assertion
5923 on the ELSE clause would be unnecessary.
5925 4- If BB does not end in a conditional expression, then we recurse
5926 into BB's dominator children.
5928 At the end of the recursive traversal, every SSA name will have a
5929 list of locations where ASSERT_EXPRs should be added. When a new
5930 location for name N is found, it is registered by calling
5931 register_new_assert_for. That function keeps track of all the
5932 registered assertions to prevent adding unnecessary assertions.
5933 For instance, if a pointer P_4 is dereferenced more than once in a
5934 dominator tree, only the location dominating all the dereference of
5935 P_4 will receive an ASSERT_EXPR.
5937 If this function returns true, then it means that there are names
5938 for which we need to generate ASSERT_EXPRs. Those assertions are
5939 inserted by process_assert_insertions. */
5942 find_assert_locations_1 (basic_block bb
, sbitmap live
)
5944 gimple_stmt_iterator si
;
5948 need_assert
= false;
5949 last
= last_stmt (bb
);
5951 /* If BB's last statement is a conditional statement involving integer
5952 operands, determine if we need to add ASSERT_EXPRs. */
5954 && gimple_code (last
) == GIMPLE_COND
5955 && !fp_predicate (last
)
5956 && !ZERO_SSA_OPERANDS (last
, SSA_OP_USE
))
5957 need_assert
|= find_conditional_asserts (bb
, last
);
5959 /* If BB's last statement is a switch statement involving integer
5960 operands, determine if we need to add ASSERT_EXPRs. */
5962 && gimple_code (last
) == GIMPLE_SWITCH
5963 && !ZERO_SSA_OPERANDS (last
, SSA_OP_USE
))
5964 need_assert
|= find_switch_asserts (bb
, last
);
5966 /* Traverse all the statements in BB marking used names and looking
5967 for statements that may infer assertions for their used operands. */
5968 for (si
= gsi_last_bb (bb
); !gsi_end_p (si
); gsi_prev (&si
))
5974 stmt
= gsi_stmt (si
);
5976 if (is_gimple_debug (stmt
))
5979 /* See if we can derive an assertion for any of STMT's operands. */
5980 FOR_EACH_SSA_TREE_OPERAND (op
, stmt
, i
, SSA_OP_USE
)
5983 enum tree_code comp_code
;
5985 /* If op is not live beyond this stmt, do not bother to insert
5987 if (!bitmap_bit_p (live
, SSA_NAME_VERSION (op
)))
5990 /* If OP is used in such a way that we can infer a value
5991 range for it, and we don't find a previous assertion for
5992 it, create a new assertion location node for OP. */
5993 if (infer_value_range (stmt
, op
, &comp_code
, &value
))
5995 /* If we are able to infer a nonzero value range for OP,
5996 then walk backwards through the use-def chain to see if OP
5997 was set via a typecast.
5999 If so, then we can also infer a nonzero value range
6000 for the operand of the NOP_EXPR. */
6001 if (comp_code
== NE_EXPR
&& integer_zerop (value
))
6004 gimple def_stmt
= SSA_NAME_DEF_STMT (t
);
6006 while (is_gimple_assign (def_stmt
)
6007 && gimple_assign_rhs_code (def_stmt
) == NOP_EXPR
6009 (gimple_assign_rhs1 (def_stmt
)) == SSA_NAME
6011 (TREE_TYPE (gimple_assign_rhs1 (def_stmt
))))
6013 t
= gimple_assign_rhs1 (def_stmt
);
6014 def_stmt
= SSA_NAME_DEF_STMT (t
);
6016 /* Note we want to register the assert for the
6017 operand of the NOP_EXPR after SI, not after the
6019 if (! has_single_use (t
))
6021 register_new_assert_for (t
, t
, comp_code
, value
,
6028 register_new_assert_for (op
, op
, comp_code
, value
, bb
, NULL
, si
);
6034 FOR_EACH_SSA_TREE_OPERAND (op
, stmt
, i
, SSA_OP_USE
)
6035 bitmap_set_bit (live
, SSA_NAME_VERSION (op
));
6036 FOR_EACH_SSA_TREE_OPERAND (op
, stmt
, i
, SSA_OP_DEF
)
6037 bitmap_clear_bit (live
, SSA_NAME_VERSION (op
));
6040 /* Traverse all PHI nodes in BB, updating live. */
6041 for (si
= gsi_start_phis (bb
); !gsi_end_p (si
); gsi_next (&si
))
6043 use_operand_p arg_p
;
6045 gimple phi
= gsi_stmt (si
);
6046 tree res
= gimple_phi_result (phi
);
6048 if (virtual_operand_p (res
))
6051 FOR_EACH_PHI_ARG (arg_p
, phi
, i
, SSA_OP_USE
)
6053 tree arg
= USE_FROM_PTR (arg_p
);
6054 if (TREE_CODE (arg
) == SSA_NAME
)
6055 bitmap_set_bit (live
, SSA_NAME_VERSION (arg
));
6058 bitmap_clear_bit (live
, SSA_NAME_VERSION (res
));
6064 /* Do an RPO walk over the function computing SSA name liveness
6065 on-the-fly and deciding on assert expressions to insert.
6066 Returns true if there are assert expressions to be inserted. */
6069 find_assert_locations (void)
6071 int *rpo
= XNEWVEC (int, last_basic_block_for_fn (cfun
));
6072 int *bb_rpo
= XNEWVEC (int, last_basic_block_for_fn (cfun
));
6073 int *last_rpo
= XCNEWVEC (int, last_basic_block_for_fn (cfun
));
6077 live
= XCNEWVEC (sbitmap
, last_basic_block_for_fn (cfun
));
6078 rpo_cnt
= pre_and_rev_post_order_compute (NULL
, rpo
, false);
6079 for (i
= 0; i
< rpo_cnt
; ++i
)
6082 /* Pre-seed loop latch liveness from loop header PHI nodes. Due to
6083 the order we compute liveness and insert asserts we otherwise
6084 fail to insert asserts into the loop latch. */
6086 FOR_EACH_LOOP (loop
, 0)
6088 i
= loop
->latch
->index
;
6089 unsigned int j
= single_succ_edge (loop
->latch
)->dest_idx
;
6090 for (gimple_stmt_iterator gsi
= gsi_start_phis (loop
->header
);
6091 !gsi_end_p (gsi
); gsi_next (&gsi
))
6093 gimple phi
= gsi_stmt (gsi
);
6094 if (virtual_operand_p (gimple_phi_result (phi
)))
6096 tree arg
= gimple_phi_arg_def (phi
, j
);
6097 if (TREE_CODE (arg
) == SSA_NAME
)
6099 if (live
[i
] == NULL
)
6101 live
[i
] = sbitmap_alloc (num_ssa_names
);
6102 bitmap_clear (live
[i
]);
6104 bitmap_set_bit (live
[i
], SSA_NAME_VERSION (arg
));
6109 need_asserts
= false;
6110 for (i
= rpo_cnt
- 1; i
>= 0; --i
)
6112 basic_block bb
= BASIC_BLOCK_FOR_FN (cfun
, rpo
[i
]);
6118 live
[rpo
[i
]] = sbitmap_alloc (num_ssa_names
);
6119 bitmap_clear (live
[rpo
[i
]]);
6122 /* Process BB and update the live information with uses in
6124 need_asserts
|= find_assert_locations_1 (bb
, live
[rpo
[i
]]);
6126 /* Merge liveness into the predecessor blocks and free it. */
6127 if (!bitmap_empty_p (live
[rpo
[i
]]))
6130 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
6132 int pred
= e
->src
->index
;
6133 if ((e
->flags
& EDGE_DFS_BACK
) || pred
== ENTRY_BLOCK
)
6138 live
[pred
] = sbitmap_alloc (num_ssa_names
);
6139 bitmap_clear (live
[pred
]);
6141 bitmap_ior (live
[pred
], live
[pred
], live
[rpo
[i
]]);
6143 if (bb_rpo
[pred
] < pred_rpo
)
6144 pred_rpo
= bb_rpo
[pred
];
6147 /* Record the RPO number of the last visited block that needs
6148 live information from this block. */
6149 last_rpo
[rpo
[i
]] = pred_rpo
;
6153 sbitmap_free (live
[rpo
[i
]]);
6154 live
[rpo
[i
]] = NULL
;
6157 /* We can free all successors live bitmaps if all their
6158 predecessors have been visited already. */
6159 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
6160 if (last_rpo
[e
->dest
->index
] == i
6161 && live
[e
->dest
->index
])
6163 sbitmap_free (live
[e
->dest
->index
]);
6164 live
[e
->dest
->index
] = NULL
;
6169 XDELETEVEC (bb_rpo
);
6170 XDELETEVEC (last_rpo
);
6171 for (i
= 0; i
< last_basic_block_for_fn (cfun
); ++i
)
6173 sbitmap_free (live
[i
]);
6176 return need_asserts
;
6179 /* Create an ASSERT_EXPR for NAME and insert it in the location
6180 indicated by LOC. Return true if we made any edge insertions. */
6183 process_assert_insertions_for (tree name
, assert_locus_t loc
)
6185 /* Build the comparison expression NAME_i COMP_CODE VAL. */
6192 /* If we have X <=> X do not insert an assert expr for that. */
6193 if (loc
->expr
== loc
->val
)
6196 cond
= build2 (loc
->comp_code
, boolean_type_node
, loc
->expr
, loc
->val
);
6197 assert_stmt
= build_assert_expr_for (cond
, name
);
6200 /* We have been asked to insert the assertion on an edge. This
6201 is used only by COND_EXPR and SWITCH_EXPR assertions. */
6202 gcc_checking_assert (gimple_code (gsi_stmt (loc
->si
)) == GIMPLE_COND
6203 || (gimple_code (gsi_stmt (loc
->si
))
6206 gsi_insert_on_edge (loc
->e
, assert_stmt
);
6210 /* Otherwise, we can insert right after LOC->SI iff the
6211 statement must not be the last statement in the block. */
6212 stmt
= gsi_stmt (loc
->si
);
6213 if (!stmt_ends_bb_p (stmt
))
6215 gsi_insert_after (&loc
->si
, assert_stmt
, GSI_SAME_STMT
);
6219 /* If STMT must be the last statement in BB, we can only insert new
6220 assertions on the non-abnormal edge out of BB. Note that since
6221 STMT is not control flow, there may only be one non-abnormal edge
6223 FOR_EACH_EDGE (e
, ei
, loc
->bb
->succs
)
6224 if (!(e
->flags
& EDGE_ABNORMAL
))
6226 gsi_insert_on_edge (e
, assert_stmt
);
6234 /* Process all the insertions registered for every name N_i registered
6235 in NEED_ASSERT_FOR. The list of assertions to be inserted are
6236 found in ASSERTS_FOR[i]. */
6239 process_assert_insertions (void)
6243 bool update_edges_p
= false;
6244 int num_asserts
= 0;
6246 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6247 dump_all_asserts (dump_file
);
6249 EXECUTE_IF_SET_IN_BITMAP (need_assert_for
, 0, i
, bi
)
6251 assert_locus_t loc
= asserts_for
[i
];
6256 assert_locus_t next
= loc
->next
;
6257 update_edges_p
|= process_assert_insertions_for (ssa_name (i
), loc
);
6265 gsi_commit_edge_inserts ();
6267 statistics_counter_event (cfun
, "Number of ASSERT_EXPR expressions inserted",
6272 /* Traverse the flowgraph looking for conditional jumps to insert range
6273 expressions. These range expressions are meant to provide information
6274 to optimizations that need to reason in terms of value ranges. They
6275 will not be expanded into RTL. For instance, given:
6284 this pass will transform the code into:
6290 x = ASSERT_EXPR <x, x < y>
6295 y = ASSERT_EXPR <y, x >= y>
6299 The idea is that once copy and constant propagation have run, other
6300 optimizations will be able to determine what ranges of values can 'x'
6301 take in different paths of the code, simply by checking the reaching
6302 definition of 'x'. */
6305 insert_range_assertions (void)
6307 need_assert_for
= BITMAP_ALLOC (NULL
);
6308 asserts_for
= XCNEWVEC (assert_locus_t
, num_ssa_names
);
6310 calculate_dominance_info (CDI_DOMINATORS
);
6312 if (find_assert_locations ())
6314 process_assert_insertions ();
6315 update_ssa (TODO_update_ssa_no_phi
);
6318 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6320 fprintf (dump_file
, "\nSSA form after inserting ASSERT_EXPRs\n");
6321 dump_function_to_file (current_function_decl
, dump_file
, dump_flags
);
6325 BITMAP_FREE (need_assert_for
);
6328 /* Checks one ARRAY_REF in REF, located at LOCUS. Ignores flexible arrays
6329 and "struct" hacks. If VRP can determine that the
6330 array subscript is a constant, check if it is outside valid
6331 range. If the array subscript is a RANGE, warn if it is
6332 non-overlapping with valid range.
6333 IGNORE_OFF_BY_ONE is true if the ARRAY_REF is inside a ADDR_EXPR. */
6336 check_array_ref (location_t location
, tree ref
, bool ignore_off_by_one
)
6338 value_range_t
* vr
= NULL
;
6339 tree low_sub
, up_sub
;
6340 tree low_bound
, up_bound
, up_bound_p1
;
6343 if (TREE_NO_WARNING (ref
))
6346 low_sub
= up_sub
= TREE_OPERAND (ref
, 1);
6347 up_bound
= array_ref_up_bound (ref
);
6349 /* Can not check flexible arrays. */
6351 || TREE_CODE (up_bound
) != INTEGER_CST
)
6354 /* Accesses to trailing arrays via pointers may access storage
6355 beyond the types array bounds. */
6356 base
= get_base_address (ref
);
6357 if (base
&& TREE_CODE (base
) == MEM_REF
)
6359 tree cref
, next
= NULL_TREE
;
6361 if (TREE_CODE (TREE_OPERAND (ref
, 0)) != COMPONENT_REF
)
6364 cref
= TREE_OPERAND (ref
, 0);
6365 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (cref
, 0))) == RECORD_TYPE
)
6366 for (next
= DECL_CHAIN (TREE_OPERAND (cref
, 1));
6367 next
&& TREE_CODE (next
) != FIELD_DECL
;
6368 next
= DECL_CHAIN (next
))
6371 /* If this is the last field in a struct type or a field in a
6372 union type do not warn. */
6377 low_bound
= array_ref_low_bound (ref
);
6378 up_bound_p1
= int_const_binop (PLUS_EXPR
, up_bound
,
6379 build_int_cst (TREE_TYPE (up_bound
), 1));
6381 if (TREE_CODE (low_sub
) == SSA_NAME
)
6383 vr
= get_value_range (low_sub
);
6384 if (vr
->type
== VR_RANGE
|| vr
->type
== VR_ANTI_RANGE
)
6386 low_sub
= vr
->type
== VR_RANGE
? vr
->max
: vr
->min
;
6387 up_sub
= vr
->type
== VR_RANGE
? vr
->min
: vr
->max
;
6391 if (vr
&& vr
->type
== VR_ANTI_RANGE
)
6393 if (TREE_CODE (up_sub
) == INTEGER_CST
6394 && tree_int_cst_lt (up_bound
, up_sub
)
6395 && TREE_CODE (low_sub
) == INTEGER_CST
6396 && tree_int_cst_lt (low_sub
, low_bound
))
6398 warning_at (location
, OPT_Warray_bounds
,
6399 "array subscript is outside array bounds");
6400 TREE_NO_WARNING (ref
) = 1;
6403 else if (TREE_CODE (up_sub
) == INTEGER_CST
6404 && (ignore_off_by_one
6405 ? (tree_int_cst_lt (up_bound
, up_sub
)
6406 && !tree_int_cst_equal (up_bound_p1
, up_sub
))
6407 : (tree_int_cst_lt (up_bound
, up_sub
)
6408 || tree_int_cst_equal (up_bound_p1
, up_sub
))))
6410 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6412 fprintf (dump_file
, "Array bound warning for ");
6413 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, ref
);
6414 fprintf (dump_file
, "\n");
6416 warning_at (location
, OPT_Warray_bounds
,
6417 "array subscript is above array bounds");
6418 TREE_NO_WARNING (ref
) = 1;
6420 else if (TREE_CODE (low_sub
) == INTEGER_CST
6421 && tree_int_cst_lt (low_sub
, low_bound
))
6423 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6425 fprintf (dump_file
, "Array bound warning for ");
6426 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, ref
);
6427 fprintf (dump_file
, "\n");
6429 warning_at (location
, OPT_Warray_bounds
,
6430 "array subscript is below array bounds");
6431 TREE_NO_WARNING (ref
) = 1;
6435 /* Searches if the expr T, located at LOCATION computes
6436 address of an ARRAY_REF, and call check_array_ref on it. */
6439 search_for_addr_array (tree t
, location_t location
)
6441 while (TREE_CODE (t
) == SSA_NAME
)
6443 gimple g
= SSA_NAME_DEF_STMT (t
);
6445 if (gimple_code (g
) != GIMPLE_ASSIGN
)
6448 if (get_gimple_rhs_class (gimple_assign_rhs_code (g
))
6449 != GIMPLE_SINGLE_RHS
)
6452 t
= gimple_assign_rhs1 (g
);
6456 /* We are only interested in addresses of ARRAY_REF's. */
6457 if (TREE_CODE (t
) != ADDR_EXPR
)
6460 /* Check each ARRAY_REFs in the reference chain. */
6463 if (TREE_CODE (t
) == ARRAY_REF
)
6464 check_array_ref (location
, t
, true /*ignore_off_by_one*/);
6466 t
= TREE_OPERAND (t
, 0);
6468 while (handled_component_p (t
));
6470 if (TREE_CODE (t
) == MEM_REF
6471 && TREE_CODE (TREE_OPERAND (t
, 0)) == ADDR_EXPR
6472 && !TREE_NO_WARNING (t
))
6474 tree tem
= TREE_OPERAND (TREE_OPERAND (t
, 0), 0);
6475 tree low_bound
, up_bound
, el_sz
;
6477 if (TREE_CODE (TREE_TYPE (tem
)) != ARRAY_TYPE
6478 || TREE_CODE (TREE_TYPE (TREE_TYPE (tem
))) == ARRAY_TYPE
6479 || !TYPE_DOMAIN (TREE_TYPE (tem
)))
6482 low_bound
= TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (tem
)));
6483 up_bound
= TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (tem
)));
6484 el_sz
= TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (tem
)));
6486 || TREE_CODE (low_bound
) != INTEGER_CST
6488 || TREE_CODE (up_bound
) != INTEGER_CST
6490 || TREE_CODE (el_sz
) != INTEGER_CST
)
6493 idx
= mem_ref_offset (t
);
6494 idx
= wi::sdiv_trunc (idx
, wi::to_offset (el_sz
));
6495 if (wi::lts_p (idx
, 0))
6497 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6499 fprintf (dump_file
, "Array bound warning for ");
6500 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, t
);
6501 fprintf (dump_file
, "\n");
6503 warning_at (location
, OPT_Warray_bounds
,
6504 "array subscript is below array bounds");
6505 TREE_NO_WARNING (t
) = 1;
6507 else if (wi::gts_p (idx
, (wi::to_offset (up_bound
)
6508 - wi::to_offset (low_bound
) + 1)))
6510 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6512 fprintf (dump_file
, "Array bound warning for ");
6513 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, t
);
6514 fprintf (dump_file
, "\n");
6516 warning_at (location
, OPT_Warray_bounds
,
6517 "array subscript is above array bounds");
6518 TREE_NO_WARNING (t
) = 1;
6523 /* walk_tree() callback that checks if *TP is
6524 an ARRAY_REF inside an ADDR_EXPR (in which an array
6525 subscript one outside the valid range is allowed). Call
6526 check_array_ref for each ARRAY_REF found. The location is
6530 check_array_bounds (tree
*tp
, int *walk_subtree
, void *data
)
6533 struct walk_stmt_info
*wi
= (struct walk_stmt_info
*) data
;
6534 location_t location
;
6536 if (EXPR_HAS_LOCATION (t
))
6537 location
= EXPR_LOCATION (t
);
6540 location_t
*locp
= (location_t
*) wi
->info
;
6544 *walk_subtree
= TRUE
;
6546 if (TREE_CODE (t
) == ARRAY_REF
)
6547 check_array_ref (location
, t
, false /*ignore_off_by_one*/);
6549 if (TREE_CODE (t
) == MEM_REF
6550 || (TREE_CODE (t
) == RETURN_EXPR
&& TREE_OPERAND (t
, 0)))
6551 search_for_addr_array (TREE_OPERAND (t
, 0), location
);
6553 if (TREE_CODE (t
) == ADDR_EXPR
)
6554 *walk_subtree
= FALSE
;
6559 /* Walk over all statements of all reachable BBs and call check_array_bounds
6563 check_all_array_refs (void)
6566 gimple_stmt_iterator si
;
6568 FOR_EACH_BB_FN (bb
, cfun
)
6572 bool executable
= false;
6574 /* Skip blocks that were found to be unreachable. */
6575 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
6576 executable
|= !!(e
->flags
& EDGE_EXECUTABLE
);
6580 for (si
= gsi_start_bb (bb
); !gsi_end_p (si
); gsi_next (&si
))
6582 gimple stmt
= gsi_stmt (si
);
6583 struct walk_stmt_info wi
;
6584 if (!gimple_has_location (stmt
))
6587 if (is_gimple_call (stmt
))
6590 size_t n
= gimple_call_num_args (stmt
);
6591 for (i
= 0; i
< n
; i
++)
6593 tree arg
= gimple_call_arg (stmt
, i
);
6594 search_for_addr_array (arg
, gimple_location (stmt
));
6599 memset (&wi
, 0, sizeof (wi
));
6600 wi
.info
= CONST_CAST (void *, (const void *)
6601 gimple_location_ptr (stmt
));
6603 walk_gimple_op (gsi_stmt (si
),
6611 /* Return true if all imm uses of VAR are either in STMT, or
6612 feed (optionally through a chain of single imm uses) GIMPLE_COND
6613 in basic block COND_BB. */
6616 all_imm_uses_in_stmt_or_feed_cond (tree var
, gimple stmt
, basic_block cond_bb
)
6618 use_operand_p use_p
, use2_p
;
6619 imm_use_iterator iter
;
6621 FOR_EACH_IMM_USE_FAST (use_p
, iter
, var
)
6622 if (USE_STMT (use_p
) != stmt
)
6624 gimple use_stmt
= USE_STMT (use_p
), use_stmt2
;
6625 if (is_gimple_debug (use_stmt
))
6627 while (is_gimple_assign (use_stmt
)
6628 && TREE_CODE (gimple_assign_lhs (use_stmt
)) == SSA_NAME
6629 && single_imm_use (gimple_assign_lhs (use_stmt
),
6630 &use2_p
, &use_stmt2
))
6631 use_stmt
= use_stmt2
;
6632 if (gimple_code (use_stmt
) != GIMPLE_COND
6633 || gimple_bb (use_stmt
) != cond_bb
)
6646 __builtin_unreachable ();
6648 x_5 = ASSERT_EXPR <x_3, ...>;
6649 If x_3 has no other immediate uses (checked by caller),
6650 var is the x_3 var from ASSERT_EXPR, we can clear low 5 bits
6651 from the non-zero bitmask. */
6654 maybe_set_nonzero_bits (basic_block bb
, tree var
)
6656 edge e
= single_pred_edge (bb
);
6657 basic_block cond_bb
= e
->src
;
6658 gimple stmt
= last_stmt (cond_bb
);
6662 || gimple_code (stmt
) != GIMPLE_COND
6663 || gimple_cond_code (stmt
) != ((e
->flags
& EDGE_TRUE_VALUE
)
6664 ? EQ_EXPR
: NE_EXPR
)
6665 || TREE_CODE (gimple_cond_lhs (stmt
)) != SSA_NAME
6666 || !integer_zerop (gimple_cond_rhs (stmt
)))
6669 stmt
= SSA_NAME_DEF_STMT (gimple_cond_lhs (stmt
));
6670 if (!is_gimple_assign (stmt
)
6671 || gimple_assign_rhs_code (stmt
) != BIT_AND_EXPR
6672 || TREE_CODE (gimple_assign_rhs2 (stmt
)) != INTEGER_CST
)
6674 if (gimple_assign_rhs1 (stmt
) != var
)
6678 if (TREE_CODE (gimple_assign_rhs1 (stmt
)) != SSA_NAME
)
6680 stmt2
= SSA_NAME_DEF_STMT (gimple_assign_rhs1 (stmt
));
6681 if (!gimple_assign_cast_p (stmt2
)
6682 || gimple_assign_rhs1 (stmt2
) != var
6683 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (stmt2
))
6684 || (TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (stmt
)))
6685 != TYPE_PRECISION (TREE_TYPE (var
))))
6688 cst
= gimple_assign_rhs2 (stmt
);
6689 set_nonzero_bits (var
, wi::bit_and_not (get_nonzero_bits (var
), cst
));
6692 /* Convert range assertion expressions into the implied copies and
6693 copy propagate away the copies. Doing the trivial copy propagation
6694 here avoids the need to run the full copy propagation pass after
6697 FIXME, this will eventually lead to copy propagation removing the
6698 names that had useful range information attached to them. For
6699 instance, if we had the assertion N_i = ASSERT_EXPR <N_j, N_j > 3>,
6700 then N_i will have the range [3, +INF].
6702 However, by converting the assertion into the implied copy
6703 operation N_i = N_j, we will then copy-propagate N_j into the uses
6704 of N_i and lose the range information. We may want to hold on to
6705 ASSERT_EXPRs a little while longer as the ranges could be used in
6706 things like jump threading.
6708 The problem with keeping ASSERT_EXPRs around is that passes after
6709 VRP need to handle them appropriately.
6711 Another approach would be to make the range information a first
6712 class property of the SSA_NAME so that it can be queried from
6713 any pass. This is made somewhat more complex by the need for
6714 multiple ranges to be associated with one SSA_NAME. */
6717 remove_range_assertions (void)
6720 gimple_stmt_iterator si
;
6721 /* 1 if looking at ASSERT_EXPRs immediately at the beginning of
6722 a basic block preceeded by GIMPLE_COND branching to it and
6723 __builtin_trap, -1 if not yet checked, 0 otherwise. */
6726 /* Note that the BSI iterator bump happens at the bottom of the
6727 loop and no bump is necessary if we're removing the statement
6728 referenced by the current BSI. */
6729 FOR_EACH_BB_FN (bb
, cfun
)
6730 for (si
= gsi_after_labels (bb
), is_unreachable
= -1; !gsi_end_p (si
);)
6732 gimple stmt
= gsi_stmt (si
);
6735 if (is_gimple_assign (stmt
)
6736 && gimple_assign_rhs_code (stmt
) == ASSERT_EXPR
)
6738 tree lhs
= gimple_assign_lhs (stmt
);
6739 tree rhs
= gimple_assign_rhs1 (stmt
);
6741 tree cond
= fold (ASSERT_EXPR_COND (rhs
));
6742 use_operand_p use_p
;
6743 imm_use_iterator iter
;
6745 gcc_assert (cond
!= boolean_false_node
);
6747 var
= ASSERT_EXPR_VAR (rhs
);
6748 gcc_assert (TREE_CODE (var
) == SSA_NAME
);
6750 if (!POINTER_TYPE_P (TREE_TYPE (lhs
))
6751 && SSA_NAME_RANGE_INFO (lhs
))
6753 if (is_unreachable
== -1)
6756 if (single_pred_p (bb
)
6757 && assert_unreachable_fallthru_edge_p
6758 (single_pred_edge (bb
)))
6762 if (x_7 >= 10 && x_7 < 20)
6763 __builtin_unreachable ();
6764 x_8 = ASSERT_EXPR <x_7, ...>;
6765 if the only uses of x_7 are in the ASSERT_EXPR and
6766 in the condition. In that case, we can copy the
6767 range info from x_8 computed in this pass also
6770 && all_imm_uses_in_stmt_or_feed_cond (var
, stmt
,
6773 set_range_info (var
, SSA_NAME_RANGE_TYPE (lhs
),
6774 SSA_NAME_RANGE_INFO (lhs
)->get_min (),
6775 SSA_NAME_RANGE_INFO (lhs
)->get_max ());
6776 maybe_set_nonzero_bits (bb
, var
);
6780 /* Propagate the RHS into every use of the LHS. */
6781 FOR_EACH_IMM_USE_STMT (use_stmt
, iter
, lhs
)
6782 FOR_EACH_IMM_USE_ON_STMT (use_p
, iter
)
6783 SET_USE (use_p
, var
);
6785 /* And finally, remove the copy, it is not needed. */
6786 gsi_remove (&si
, true);
6787 release_defs (stmt
);
6791 if (!is_gimple_debug (gsi_stmt (si
)))
6799 /* Return true if STMT is interesting for VRP. */
6802 stmt_interesting_for_vrp (gimple stmt
)
6804 if (gimple_code (stmt
) == GIMPLE_PHI
)
6806 tree res
= gimple_phi_result (stmt
);
6807 return (!virtual_operand_p (res
)
6808 && (INTEGRAL_TYPE_P (TREE_TYPE (res
))
6809 || POINTER_TYPE_P (TREE_TYPE (res
))));
6811 else if (is_gimple_assign (stmt
) || is_gimple_call (stmt
))
6813 tree lhs
= gimple_get_lhs (stmt
);
6815 /* In general, assignments with virtual operands are not useful
6816 for deriving ranges, with the obvious exception of calls to
6817 builtin functions. */
6818 if (lhs
&& TREE_CODE (lhs
) == SSA_NAME
6819 && (INTEGRAL_TYPE_P (TREE_TYPE (lhs
))
6820 || POINTER_TYPE_P (TREE_TYPE (lhs
)))
6821 && (is_gimple_call (stmt
)
6822 || !gimple_vuse (stmt
)))
6825 else if (gimple_code (stmt
) == GIMPLE_COND
6826 || gimple_code (stmt
) == GIMPLE_SWITCH
)
6833 /* Initialize local data structures for VRP. */
6836 vrp_initialize (void)
6840 values_propagated
= false;
6841 num_vr_values
= num_ssa_names
;
6842 vr_value
= XCNEWVEC (value_range_t
*, num_vr_values
);
6843 vr_phi_edge_counts
= XCNEWVEC (int, num_ssa_names
);
6845 FOR_EACH_BB_FN (bb
, cfun
)
6847 gimple_stmt_iterator si
;
6849 for (si
= gsi_start_phis (bb
); !gsi_end_p (si
); gsi_next (&si
))
6851 gimple phi
= gsi_stmt (si
);
6852 if (!stmt_interesting_for_vrp (phi
))
6854 tree lhs
= PHI_RESULT (phi
);
6855 set_value_range_to_varying (get_value_range (lhs
));
6856 prop_set_simulate_again (phi
, false);
6859 prop_set_simulate_again (phi
, true);
6862 for (si
= gsi_start_bb (bb
); !gsi_end_p (si
); gsi_next (&si
))
6864 gimple stmt
= gsi_stmt (si
);
6866 /* If the statement is a control insn, then we do not
6867 want to avoid simulating the statement once. Failure
6868 to do so means that those edges will never get added. */
6869 if (stmt_ends_bb_p (stmt
))
6870 prop_set_simulate_again (stmt
, true);
6871 else if (!stmt_interesting_for_vrp (stmt
))
6875 FOR_EACH_SSA_TREE_OPERAND (def
, stmt
, i
, SSA_OP_DEF
)
6876 set_value_range_to_varying (get_value_range (def
));
6877 prop_set_simulate_again (stmt
, false);
6880 prop_set_simulate_again (stmt
, true);
6885 /* Return the singleton value-range for NAME or NAME. */
6888 vrp_valueize (tree name
)
6890 if (TREE_CODE (name
) == SSA_NAME
)
6892 value_range_t
*vr
= get_value_range (name
);
6893 if (vr
->type
== VR_RANGE
6894 && (vr
->min
== vr
->max
6895 || operand_equal_p (vr
->min
, vr
->max
, 0)))
6901 /* Visit assignment STMT. If it produces an interesting range, record
6902 the SSA name in *OUTPUT_P. */
6904 static enum ssa_prop_result
6905 vrp_visit_assignment_or_call (gimple stmt
, tree
*output_p
)
6909 enum gimple_code code
= gimple_code (stmt
);
6910 lhs
= gimple_get_lhs (stmt
);
6912 /* We only keep track of ranges in integral and pointer types. */
6913 if (TREE_CODE (lhs
) == SSA_NAME
6914 && ((INTEGRAL_TYPE_P (TREE_TYPE (lhs
))
6915 /* It is valid to have NULL MIN/MAX values on a type. See
6916 build_range_type. */
6917 && TYPE_MIN_VALUE (TREE_TYPE (lhs
))
6918 && TYPE_MAX_VALUE (TREE_TYPE (lhs
)))
6919 || POINTER_TYPE_P (TREE_TYPE (lhs
))))
6921 value_range_t new_vr
= VR_INITIALIZER
;
6923 /* Try folding the statement to a constant first. */
6924 tree tem
= gimple_fold_stmt_to_constant (stmt
, vrp_valueize
);
6926 set_value_range_to_value (&new_vr
, tem
, NULL
);
6927 /* Then dispatch to value-range extracting functions. */
6928 else if (code
== GIMPLE_CALL
)
6929 extract_range_basic (&new_vr
, stmt
);
6931 extract_range_from_assignment (&new_vr
, stmt
);
6933 if (update_value_range (lhs
, &new_vr
))
6937 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6939 fprintf (dump_file
, "Found new range for ");
6940 print_generic_expr (dump_file
, lhs
, 0);
6941 fprintf (dump_file
, ": ");
6942 dump_value_range (dump_file
, &new_vr
);
6943 fprintf (dump_file
, "\n");
6946 if (new_vr
.type
== VR_VARYING
)
6947 return SSA_PROP_VARYING
;
6949 return SSA_PROP_INTERESTING
;
6952 return SSA_PROP_NOT_INTERESTING
;
6955 /* Every other statement produces no useful ranges. */
6956 FOR_EACH_SSA_TREE_OPERAND (def
, stmt
, iter
, SSA_OP_DEF
)
6957 set_value_range_to_varying (get_value_range (def
));
6959 return SSA_PROP_VARYING
;
6962 /* Helper that gets the value range of the SSA_NAME with version I
6963 or a symbolic range containing the SSA_NAME only if the value range
6964 is varying or undefined. */
6966 static inline value_range_t
6967 get_vr_for_comparison (int i
)
6969 value_range_t vr
= *get_value_range (ssa_name (i
));
6971 /* If name N_i does not have a valid range, use N_i as its own
6972 range. This allows us to compare against names that may
6973 have N_i in their ranges. */
6974 if (vr
.type
== VR_VARYING
|| vr
.type
== VR_UNDEFINED
)
6977 vr
.min
= ssa_name (i
);
6978 vr
.max
= ssa_name (i
);
6984 /* Compare all the value ranges for names equivalent to VAR with VAL
6985 using comparison code COMP. Return the same value returned by
6986 compare_range_with_value, including the setting of
6987 *STRICT_OVERFLOW_P. */
6990 compare_name_with_value (enum tree_code comp
, tree var
, tree val
,
6991 bool *strict_overflow_p
)
6997 int used_strict_overflow
;
6999 value_range_t equiv_vr
;
7001 /* Get the set of equivalences for VAR. */
7002 e
= get_value_range (var
)->equiv
;
7004 /* Start at -1. Set it to 0 if we do a comparison without relying
7005 on overflow, or 1 if all comparisons rely on overflow. */
7006 used_strict_overflow
= -1;
7008 /* Compare vars' value range with val. */
7009 equiv_vr
= get_vr_for_comparison (SSA_NAME_VERSION (var
));
7011 retval
= compare_range_with_value (comp
, &equiv_vr
, val
, &sop
);
7013 used_strict_overflow
= sop
? 1 : 0;
7015 /* If the equiv set is empty we have done all work we need to do. */
7019 && used_strict_overflow
> 0)
7020 *strict_overflow_p
= true;
7024 EXECUTE_IF_SET_IN_BITMAP (e
, 0, i
, bi
)
7026 equiv_vr
= get_vr_for_comparison (i
);
7028 t
= compare_range_with_value (comp
, &equiv_vr
, val
, &sop
);
7031 /* If we get different answers from different members
7032 of the equivalence set this check must be in a dead
7033 code region. Folding it to a trap representation
7034 would be correct here. For now just return don't-know. */
7044 used_strict_overflow
= 0;
7045 else if (used_strict_overflow
< 0)
7046 used_strict_overflow
= 1;
7051 && used_strict_overflow
> 0)
7052 *strict_overflow_p
= true;
7058 /* Given a comparison code COMP and names N1 and N2, compare all the
7059 ranges equivalent to N1 against all the ranges equivalent to N2
7060 to determine the value of N1 COMP N2. Return the same value
7061 returned by compare_ranges. Set *STRICT_OVERFLOW_P to indicate
7062 whether we relied on an overflow infinity in the comparison. */
7066 compare_names (enum tree_code comp
, tree n1
, tree n2
,
7067 bool *strict_overflow_p
)
7071 bitmap_iterator bi1
, bi2
;
7073 int used_strict_overflow
;
7074 static bitmap_obstack
*s_obstack
= NULL
;
7075 static bitmap s_e1
= NULL
, s_e2
= NULL
;
7077 /* Compare the ranges of every name equivalent to N1 against the
7078 ranges of every name equivalent to N2. */
7079 e1
= get_value_range (n1
)->equiv
;
7080 e2
= get_value_range (n2
)->equiv
;
7082 /* Use the fake bitmaps if e1 or e2 are not available. */
7083 if (s_obstack
== NULL
)
7085 s_obstack
= XNEW (bitmap_obstack
);
7086 bitmap_obstack_initialize (s_obstack
);
7087 s_e1
= BITMAP_ALLOC (s_obstack
);
7088 s_e2
= BITMAP_ALLOC (s_obstack
);
7095 /* Add N1 and N2 to their own set of equivalences to avoid
7096 duplicating the body of the loop just to check N1 and N2
7098 bitmap_set_bit (e1
, SSA_NAME_VERSION (n1
));
7099 bitmap_set_bit (e2
, SSA_NAME_VERSION (n2
));
7101 /* If the equivalence sets have a common intersection, then the two
7102 names can be compared without checking their ranges. */
7103 if (bitmap_intersect_p (e1
, e2
))
7105 bitmap_clear_bit (e1
, SSA_NAME_VERSION (n1
));
7106 bitmap_clear_bit (e2
, SSA_NAME_VERSION (n2
));
7108 return (comp
== EQ_EXPR
|| comp
== GE_EXPR
|| comp
== LE_EXPR
)
7110 : boolean_false_node
;
7113 /* Start at -1. Set it to 0 if we do a comparison without relying
7114 on overflow, or 1 if all comparisons rely on overflow. */
7115 used_strict_overflow
= -1;
7117 /* Otherwise, compare all the equivalent ranges. First, add N1 and
7118 N2 to their own set of equivalences to avoid duplicating the body
7119 of the loop just to check N1 and N2 ranges. */
7120 EXECUTE_IF_SET_IN_BITMAP (e1
, 0, i1
, bi1
)
7122 value_range_t vr1
= get_vr_for_comparison (i1
);
7124 t
= retval
= NULL_TREE
;
7125 EXECUTE_IF_SET_IN_BITMAP (e2
, 0, i2
, bi2
)
7129 value_range_t vr2
= get_vr_for_comparison (i2
);
7131 t
= compare_ranges (comp
, &vr1
, &vr2
, &sop
);
7134 /* If we get different answers from different members
7135 of the equivalence set this check must be in a dead
7136 code region. Folding it to a trap representation
7137 would be correct here. For now just return don't-know. */
7141 bitmap_clear_bit (e1
, SSA_NAME_VERSION (n1
));
7142 bitmap_clear_bit (e2
, SSA_NAME_VERSION (n2
));
7148 used_strict_overflow
= 0;
7149 else if (used_strict_overflow
< 0)
7150 used_strict_overflow
= 1;
7156 bitmap_clear_bit (e1
, SSA_NAME_VERSION (n1
));
7157 bitmap_clear_bit (e2
, SSA_NAME_VERSION (n2
));
7158 if (used_strict_overflow
> 0)
7159 *strict_overflow_p
= true;
7164 /* None of the equivalent ranges are useful in computing this
7166 bitmap_clear_bit (e1
, SSA_NAME_VERSION (n1
));
7167 bitmap_clear_bit (e2
, SSA_NAME_VERSION (n2
));
7171 /* Helper function for vrp_evaluate_conditional_warnv. */
7174 vrp_evaluate_conditional_warnv_with_ops_using_ranges (enum tree_code code
,
7176 bool * strict_overflow_p
)
7178 value_range_t
*vr0
, *vr1
;
7180 vr0
= (TREE_CODE (op0
) == SSA_NAME
) ? get_value_range (op0
) : NULL
;
7181 vr1
= (TREE_CODE (op1
) == SSA_NAME
) ? get_value_range (op1
) : NULL
;
7183 tree res
= NULL_TREE
;
7185 res
= compare_ranges (code
, vr0
, vr1
, strict_overflow_p
);
7187 res
= compare_range_with_value (code
, vr0
, op1
, strict_overflow_p
);
7189 res
= (compare_range_with_value
7190 (swap_tree_comparison (code
), vr1
, op0
, strict_overflow_p
));
7194 /* Helper function for vrp_evaluate_conditional_warnv. */
7197 vrp_evaluate_conditional_warnv_with_ops (enum tree_code code
, tree op0
,
7198 tree op1
, bool use_equiv_p
,
7199 bool *strict_overflow_p
, bool *only_ranges
)
7203 *only_ranges
= true;
7205 /* We only deal with integral and pointer types. */
7206 if (!INTEGRAL_TYPE_P (TREE_TYPE (op0
))
7207 && !POINTER_TYPE_P (TREE_TYPE (op0
)))
7213 && (ret
= vrp_evaluate_conditional_warnv_with_ops_using_ranges
7214 (code
, op0
, op1
, strict_overflow_p
)))
7216 *only_ranges
= false;
7217 if (TREE_CODE (op0
) == SSA_NAME
&& TREE_CODE (op1
) == SSA_NAME
)
7218 return compare_names (code
, op0
, op1
, strict_overflow_p
);
7219 else if (TREE_CODE (op0
) == SSA_NAME
)
7220 return compare_name_with_value (code
, op0
, op1
, strict_overflow_p
);
7221 else if (TREE_CODE (op1
) == SSA_NAME
)
7222 return (compare_name_with_value
7223 (swap_tree_comparison (code
), op1
, op0
, strict_overflow_p
));
7226 return vrp_evaluate_conditional_warnv_with_ops_using_ranges (code
, op0
, op1
,
7231 /* Given (CODE OP0 OP1) within STMT, try to simplify it based on value range
7232 information. Return NULL if the conditional can not be evaluated.
7233 The ranges of all the names equivalent with the operands in COND
7234 will be used when trying to compute the value. If the result is
7235 based on undefined signed overflow, issue a warning if
7239 vrp_evaluate_conditional (enum tree_code code
, tree op0
, tree op1
, gimple stmt
)
7245 /* Some passes and foldings leak constants with overflow flag set
7246 into the IL. Avoid doing wrong things with these and bail out. */
7247 if ((TREE_CODE (op0
) == INTEGER_CST
7248 && TREE_OVERFLOW (op0
))
7249 || (TREE_CODE (op1
) == INTEGER_CST
7250 && TREE_OVERFLOW (op1
)))
7254 ret
= vrp_evaluate_conditional_warnv_with_ops (code
, op0
, op1
, true, &sop
,
7259 enum warn_strict_overflow_code wc
;
7260 const char* warnmsg
;
7262 if (is_gimple_min_invariant (ret
))
7264 wc
= WARN_STRICT_OVERFLOW_CONDITIONAL
;
7265 warnmsg
= G_("assuming signed overflow does not occur when "
7266 "simplifying conditional to constant");
7270 wc
= WARN_STRICT_OVERFLOW_COMPARISON
;
7271 warnmsg
= G_("assuming signed overflow does not occur when "
7272 "simplifying conditional");
7275 if (issue_strict_overflow_warning (wc
))
7277 location_t location
;
7279 if (!gimple_has_location (stmt
))
7280 location
= input_location
;
7282 location
= gimple_location (stmt
);
7283 warning_at (location
, OPT_Wstrict_overflow
, "%s", warnmsg
);
7287 if (warn_type_limits
7288 && ret
&& only_ranges
7289 && TREE_CODE_CLASS (code
) == tcc_comparison
7290 && TREE_CODE (op0
) == SSA_NAME
)
7292 /* If the comparison is being folded and the operand on the LHS
7293 is being compared against a constant value that is outside of
7294 the natural range of OP0's type, then the predicate will
7295 always fold regardless of the value of OP0. If -Wtype-limits
7296 was specified, emit a warning. */
7297 tree type
= TREE_TYPE (op0
);
7298 value_range_t
*vr0
= get_value_range (op0
);
7300 if (vr0
->type
!= VR_VARYING
7301 && INTEGRAL_TYPE_P (type
)
7302 && vrp_val_is_min (vr0
->min
)
7303 && vrp_val_is_max (vr0
->max
)
7304 && is_gimple_min_invariant (op1
))
7306 location_t location
;
7308 if (!gimple_has_location (stmt
))
7309 location
= input_location
;
7311 location
= gimple_location (stmt
);
7313 warning_at (location
, OPT_Wtype_limits
,
7315 ? G_("comparison always false "
7316 "due to limited range of data type")
7317 : G_("comparison always true "
7318 "due to limited range of data type"));
7326 /* Visit conditional statement STMT. If we can determine which edge
7327 will be taken out of STMT's basic block, record it in
7328 *TAKEN_EDGE_P and return SSA_PROP_INTERESTING. Otherwise, return
7329 SSA_PROP_VARYING. */
7331 static enum ssa_prop_result
7332 vrp_visit_cond_stmt (gimple stmt
, edge
*taken_edge_p
)
7337 *taken_edge_p
= NULL
;
7339 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7344 fprintf (dump_file
, "\nVisiting conditional with predicate: ");
7345 print_gimple_stmt (dump_file
, stmt
, 0, 0);
7346 fprintf (dump_file
, "\nWith known ranges\n");
7348 FOR_EACH_SSA_TREE_OPERAND (use
, stmt
, i
, SSA_OP_USE
)
7350 fprintf (dump_file
, "\t");
7351 print_generic_expr (dump_file
, use
, 0);
7352 fprintf (dump_file
, ": ");
7353 dump_value_range (dump_file
, vr_value
[SSA_NAME_VERSION (use
)]);
7356 fprintf (dump_file
, "\n");
7359 /* Compute the value of the predicate COND by checking the known
7360 ranges of each of its operands.
7362 Note that we cannot evaluate all the equivalent ranges here
7363 because those ranges may not yet be final and with the current
7364 propagation strategy, we cannot determine when the value ranges
7365 of the names in the equivalence set have changed.
7367 For instance, given the following code fragment
7371 i_14 = ASSERT_EXPR <i_5, i_5 != 0>
7375 Assume that on the first visit to i_14, i_5 has the temporary
7376 range [8, 8] because the second argument to the PHI function is
7377 not yet executable. We derive the range ~[0, 0] for i_14 and the
7378 equivalence set { i_5 }. So, when we visit 'if (i_14 == 1)' for
7379 the first time, since i_14 is equivalent to the range [8, 8], we
7380 determine that the predicate is always false.
7382 On the next round of propagation, i_13 is determined to be
7383 VARYING, which causes i_5 to drop down to VARYING. So, another
7384 visit to i_14 is scheduled. In this second visit, we compute the
7385 exact same range and equivalence set for i_14, namely ~[0, 0] and
7386 { i_5 }. But we did not have the previous range for i_5
7387 registered, so vrp_visit_assignment thinks that the range for
7388 i_14 has not changed. Therefore, the predicate 'if (i_14 == 1)'
7389 is not visited again, which stops propagation from visiting
7390 statements in the THEN clause of that if().
7392 To properly fix this we would need to keep the previous range
7393 value for the names in the equivalence set. This way we would've
7394 discovered that from one visit to the other i_5 changed from
7395 range [8, 8] to VR_VARYING.
7397 However, fixing this apparent limitation may not be worth the
7398 additional checking. Testing on several code bases (GCC, DLV,
7399 MICO, TRAMP3D and SPEC2000) showed that doing this results in
7400 4 more predicates folded in SPEC. */
7403 val
= vrp_evaluate_conditional_warnv_with_ops (gimple_cond_code (stmt
),
7404 gimple_cond_lhs (stmt
),
7405 gimple_cond_rhs (stmt
),
7410 *taken_edge_p
= find_taken_edge (gimple_bb (stmt
), val
);
7413 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7415 "\nIgnoring predicate evaluation because "
7416 "it assumes that signed overflow is undefined");
7421 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7423 fprintf (dump_file
, "\nPredicate evaluates to: ");
7424 if (val
== NULL_TREE
)
7425 fprintf (dump_file
, "DON'T KNOW\n");
7427 print_generic_stmt (dump_file
, val
, 0);
7430 return (*taken_edge_p
) ? SSA_PROP_INTERESTING
: SSA_PROP_VARYING
;
7433 /* Searches the case label vector VEC for the index *IDX of the CASE_LABEL
7434 that includes the value VAL. The search is restricted to the range
7435 [START_IDX, n - 1] where n is the size of VEC.
7437 If there is a CASE_LABEL for VAL, its index is placed in IDX and true is
7440 If there is no CASE_LABEL for VAL and there is one that is larger than VAL,
7441 it is placed in IDX and false is returned.
7443 If VAL is larger than any CASE_LABEL, n is placed on IDX and false is
7447 find_case_label_index (gimple stmt
, size_t start_idx
, tree val
, size_t *idx
)
7449 size_t n
= gimple_switch_num_labels (stmt
);
7452 /* Find case label for minimum of the value range or the next one.
7453 At each iteration we are searching in [low, high - 1]. */
7455 for (low
= start_idx
, high
= n
; high
!= low
; )
7459 /* Note that i != high, so we never ask for n. */
7460 size_t i
= (high
+ low
) / 2;
7461 t
= gimple_switch_label (stmt
, i
);
7463 /* Cache the result of comparing CASE_LOW and val. */
7464 cmp
= tree_int_cst_compare (CASE_LOW (t
), val
);
7468 /* Ranges cannot be empty. */
7477 if (CASE_HIGH (t
) != NULL
7478 && tree_int_cst_compare (CASE_HIGH (t
), val
) >= 0)
7490 /* Searches the case label vector VEC for the range of CASE_LABELs that is used
7491 for values between MIN and MAX. The first index is placed in MIN_IDX. The
7492 last index is placed in MAX_IDX. If the range of CASE_LABELs is empty
7493 then MAX_IDX < MIN_IDX.
7494 Returns true if the default label is not needed. */
7497 find_case_label_range (gimple stmt
, tree min
, tree max
, size_t *min_idx
,
7501 bool min_take_default
= !find_case_label_index (stmt
, 1, min
, &i
);
7502 bool max_take_default
= !find_case_label_index (stmt
, i
, max
, &j
);
7506 && max_take_default
)
7508 /* Only the default case label reached.
7509 Return an empty range. */
7516 bool take_default
= min_take_default
|| max_take_default
;
7520 if (max_take_default
)
7523 /* If the case label range is continuous, we do not need
7524 the default case label. Verify that. */
7525 high
= CASE_LOW (gimple_switch_label (stmt
, i
));
7526 if (CASE_HIGH (gimple_switch_label (stmt
, i
)))
7527 high
= CASE_HIGH (gimple_switch_label (stmt
, i
));
7528 for (k
= i
+ 1; k
<= j
; ++k
)
7530 low
= CASE_LOW (gimple_switch_label (stmt
, k
));
7531 if (!integer_onep (int_const_binop (MINUS_EXPR
, low
, high
)))
7533 take_default
= true;
7537 if (CASE_HIGH (gimple_switch_label (stmt
, k
)))
7538 high
= CASE_HIGH (gimple_switch_label (stmt
, k
));
7543 return !take_default
;
7547 /* Searches the case label vector VEC for the ranges of CASE_LABELs that are
7548 used in range VR. The indices are placed in MIN_IDX1, MAX_IDX, MIN_IDX2 and
7549 MAX_IDX2. If the ranges of CASE_LABELs are empty then MAX_IDX1 < MIN_IDX1.
7550 Returns true if the default label is not needed. */
7553 find_case_label_ranges (gimple stmt
, value_range_t
*vr
, size_t *min_idx1
,
7554 size_t *max_idx1
, size_t *min_idx2
,
7558 unsigned int n
= gimple_switch_num_labels (stmt
);
7560 tree case_low
, case_high
;
7561 tree min
= vr
->min
, max
= vr
->max
;
7563 gcc_checking_assert (vr
->type
== VR_RANGE
|| vr
->type
== VR_ANTI_RANGE
);
7565 take_default
= !find_case_label_range (stmt
, min
, max
, &i
, &j
);
7567 /* Set second range to emtpy. */
7571 if (vr
->type
== VR_RANGE
)
7575 return !take_default
;
7578 /* Set first range to all case labels. */
7585 /* Make sure all the values of case labels [i , j] are contained in
7586 range [MIN, MAX]. */
7587 case_low
= CASE_LOW (gimple_switch_label (stmt
, i
));
7588 case_high
= CASE_HIGH (gimple_switch_label (stmt
, j
));
7589 if (tree_int_cst_compare (case_low
, min
) < 0)
7591 if (case_high
!= NULL_TREE
7592 && tree_int_cst_compare (max
, case_high
) < 0)
7598 /* If the range spans case labels [i, j], the corresponding anti-range spans
7599 the labels [1, i - 1] and [j + 1, n - 1]. */
7625 /* Visit switch statement STMT. If we can determine which edge
7626 will be taken out of STMT's basic block, record it in
7627 *TAKEN_EDGE_P and return SSA_PROP_INTERESTING. Otherwise, return
7628 SSA_PROP_VARYING. */
7630 static enum ssa_prop_result
7631 vrp_visit_switch_stmt (gimple stmt
, edge
*taken_edge_p
)
7635 size_t i
= 0, j
= 0, k
, l
;
7638 *taken_edge_p
= NULL
;
7639 op
= gimple_switch_index (stmt
);
7640 if (TREE_CODE (op
) != SSA_NAME
)
7641 return SSA_PROP_VARYING
;
7643 vr
= get_value_range (op
);
7644 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7646 fprintf (dump_file
, "\nVisiting switch expression with operand ");
7647 print_generic_expr (dump_file
, op
, 0);
7648 fprintf (dump_file
, " with known range ");
7649 dump_value_range (dump_file
, vr
);
7650 fprintf (dump_file
, "\n");
7653 if ((vr
->type
!= VR_RANGE
7654 && vr
->type
!= VR_ANTI_RANGE
)
7655 || symbolic_range_p (vr
))
7656 return SSA_PROP_VARYING
;
7658 /* Find the single edge that is taken from the switch expression. */
7659 take_default
= !find_case_label_ranges (stmt
, vr
, &i
, &j
, &k
, &l
);
7661 /* Check if the range spans no CASE_LABEL. If so, we only reach the default
7665 gcc_assert (take_default
);
7666 val
= gimple_switch_default_label (stmt
);
7670 /* Check if labels with index i to j and maybe the default label
7671 are all reaching the same label. */
7673 val
= gimple_switch_label (stmt
, i
);
7675 && CASE_LABEL (gimple_switch_default_label (stmt
))
7676 != CASE_LABEL (val
))
7678 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7679 fprintf (dump_file
, " not a single destination for this "
7681 return SSA_PROP_VARYING
;
7683 for (++i
; i
<= j
; ++i
)
7685 if (CASE_LABEL (gimple_switch_label (stmt
, i
)) != CASE_LABEL (val
))
7687 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7688 fprintf (dump_file
, " not a single destination for this "
7690 return SSA_PROP_VARYING
;
7695 if (CASE_LABEL (gimple_switch_label (stmt
, k
)) != CASE_LABEL (val
))
7697 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7698 fprintf (dump_file
, " not a single destination for this "
7700 return SSA_PROP_VARYING
;
7705 *taken_edge_p
= find_edge (gimple_bb (stmt
),
7706 label_to_block (CASE_LABEL (val
)));
7708 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7710 fprintf (dump_file
, " will take edge to ");
7711 print_generic_stmt (dump_file
, CASE_LABEL (val
), 0);
7714 return SSA_PROP_INTERESTING
;
7718 /* Evaluate statement STMT. If the statement produces a useful range,
7719 return SSA_PROP_INTERESTING and record the SSA name with the
7720 interesting range into *OUTPUT_P.
7722 If STMT is a conditional branch and we can determine its truth
7723 value, the taken edge is recorded in *TAKEN_EDGE_P.
7725 If STMT produces a varying value, return SSA_PROP_VARYING. */
7727 static enum ssa_prop_result
7728 vrp_visit_stmt (gimple stmt
, edge
*taken_edge_p
, tree
*output_p
)
7733 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7735 fprintf (dump_file
, "\nVisiting statement:\n");
7736 print_gimple_stmt (dump_file
, stmt
, 0, dump_flags
);
7739 if (!stmt_interesting_for_vrp (stmt
))
7740 gcc_assert (stmt_ends_bb_p (stmt
));
7741 else if (is_gimple_assign (stmt
) || is_gimple_call (stmt
))
7742 return vrp_visit_assignment_or_call (stmt
, output_p
);
7743 else if (gimple_code (stmt
) == GIMPLE_COND
)
7744 return vrp_visit_cond_stmt (stmt
, taken_edge_p
);
7745 else if (gimple_code (stmt
) == GIMPLE_SWITCH
)
7746 return vrp_visit_switch_stmt (stmt
, taken_edge_p
);
7748 /* All other statements produce nothing of interest for VRP, so mark
7749 their outputs varying and prevent further simulation. */
7750 FOR_EACH_SSA_TREE_OPERAND (def
, stmt
, iter
, SSA_OP_DEF
)
7751 set_value_range_to_varying (get_value_range (def
));
7753 return SSA_PROP_VARYING
;
7756 /* Union the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
7757 { VR1TYPE, VR0MIN, VR0MAX } and store the result
7758 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
7759 possible such range. The resulting range is not canonicalized. */
7762 union_ranges (enum value_range_type
*vr0type
,
7763 tree
*vr0min
, tree
*vr0max
,
7764 enum value_range_type vr1type
,
7765 tree vr1min
, tree vr1max
)
7767 bool mineq
= operand_equal_p (*vr0min
, vr1min
, 0);
7768 bool maxeq
= operand_equal_p (*vr0max
, vr1max
, 0);
7770 /* [] is vr0, () is vr1 in the following classification comments. */
7774 if (*vr0type
== vr1type
)
7775 /* Nothing to do for equal ranges. */
7777 else if ((*vr0type
== VR_RANGE
7778 && vr1type
== VR_ANTI_RANGE
)
7779 || (*vr0type
== VR_ANTI_RANGE
7780 && vr1type
== VR_RANGE
))
7782 /* For anti-range with range union the result is varying. */
7788 else if (operand_less_p (*vr0max
, vr1min
) == 1
7789 || operand_less_p (vr1max
, *vr0min
) == 1)
7791 /* [ ] ( ) or ( ) [ ]
7792 If the ranges have an empty intersection, result of the union
7793 operation is the anti-range or if both are anti-ranges
7795 if (*vr0type
== VR_ANTI_RANGE
7796 && vr1type
== VR_ANTI_RANGE
)
7798 else if (*vr0type
== VR_ANTI_RANGE
7799 && vr1type
== VR_RANGE
)
7801 else if (*vr0type
== VR_RANGE
7802 && vr1type
== VR_ANTI_RANGE
)
7808 else if (*vr0type
== VR_RANGE
7809 && vr1type
== VR_RANGE
)
7811 /* The result is the convex hull of both ranges. */
7812 if (operand_less_p (*vr0max
, vr1min
) == 1)
7814 /* If the result can be an anti-range, create one. */
7815 if (TREE_CODE (*vr0max
) == INTEGER_CST
7816 && TREE_CODE (vr1min
) == INTEGER_CST
7817 && vrp_val_is_min (*vr0min
)
7818 && vrp_val_is_max (vr1max
))
7820 tree min
= int_const_binop (PLUS_EXPR
,
7822 build_int_cst (TREE_TYPE (*vr0max
), 1));
7823 tree max
= int_const_binop (MINUS_EXPR
,
7825 build_int_cst (TREE_TYPE (vr1min
), 1));
7826 if (!operand_less_p (max
, min
))
7828 *vr0type
= VR_ANTI_RANGE
;
7840 /* If the result can be an anti-range, create one. */
7841 if (TREE_CODE (vr1max
) == INTEGER_CST
7842 && TREE_CODE (*vr0min
) == INTEGER_CST
7843 && vrp_val_is_min (vr1min
)
7844 && vrp_val_is_max (*vr0max
))
7846 tree min
= int_const_binop (PLUS_EXPR
,
7848 build_int_cst (TREE_TYPE (vr1max
), 1));
7849 tree max
= int_const_binop (MINUS_EXPR
,
7851 build_int_cst (TREE_TYPE (*vr0min
), 1));
7852 if (!operand_less_p (max
, min
))
7854 *vr0type
= VR_ANTI_RANGE
;
7868 else if ((maxeq
|| operand_less_p (vr1max
, *vr0max
) == 1)
7869 && (mineq
|| operand_less_p (*vr0min
, vr1min
) == 1))
7871 /* [ ( ) ] or [( ) ] or [ ( )] */
7872 if (*vr0type
== VR_RANGE
7873 && vr1type
== VR_RANGE
)
7875 else if (*vr0type
== VR_ANTI_RANGE
7876 && vr1type
== VR_ANTI_RANGE
)
7882 else if (*vr0type
== VR_ANTI_RANGE
7883 && vr1type
== VR_RANGE
)
7885 /* Arbitrarily choose the right or left gap. */
7886 if (!mineq
&& TREE_CODE (vr1min
) == INTEGER_CST
)
7887 *vr0max
= int_const_binop (MINUS_EXPR
, vr1min
,
7888 build_int_cst (TREE_TYPE (vr1min
), 1));
7889 else if (!maxeq
&& TREE_CODE (vr1max
) == INTEGER_CST
)
7890 *vr0min
= int_const_binop (PLUS_EXPR
, vr1max
,
7891 build_int_cst (TREE_TYPE (vr1max
), 1));
7895 else if (*vr0type
== VR_RANGE
7896 && vr1type
== VR_ANTI_RANGE
)
7897 /* The result covers everything. */
7902 else if ((maxeq
|| operand_less_p (*vr0max
, vr1max
) == 1)
7903 && (mineq
|| operand_less_p (vr1min
, *vr0min
) == 1))
7905 /* ( [ ] ) or ([ ] ) or ( [ ]) */
7906 if (*vr0type
== VR_RANGE
7907 && vr1type
== VR_RANGE
)
7913 else if (*vr0type
== VR_ANTI_RANGE
7914 && vr1type
== VR_ANTI_RANGE
)
7916 else if (*vr0type
== VR_RANGE
7917 && vr1type
== VR_ANTI_RANGE
)
7919 *vr0type
= VR_ANTI_RANGE
;
7920 if (!mineq
&& TREE_CODE (*vr0min
) == INTEGER_CST
)
7922 *vr0max
= int_const_binop (MINUS_EXPR
, *vr0min
,
7923 build_int_cst (TREE_TYPE (*vr0min
), 1));
7926 else if (!maxeq
&& TREE_CODE (*vr0max
) == INTEGER_CST
)
7928 *vr0min
= int_const_binop (PLUS_EXPR
, *vr0max
,
7929 build_int_cst (TREE_TYPE (*vr0max
), 1));
7935 else if (*vr0type
== VR_ANTI_RANGE
7936 && vr1type
== VR_RANGE
)
7937 /* The result covers everything. */
7942 else if ((operand_less_p (vr1min
, *vr0max
) == 1
7943 || operand_equal_p (vr1min
, *vr0max
, 0))
7944 && operand_less_p (*vr0min
, vr1min
) == 1
7945 && operand_less_p (*vr0max
, vr1max
) == 1)
7947 /* [ ( ] ) or [ ]( ) */
7948 if (*vr0type
== VR_RANGE
7949 && vr1type
== VR_RANGE
)
7951 else if (*vr0type
== VR_ANTI_RANGE
7952 && vr1type
== VR_ANTI_RANGE
)
7954 else if (*vr0type
== VR_ANTI_RANGE
7955 && vr1type
== VR_RANGE
)
7957 if (TREE_CODE (vr1min
) == INTEGER_CST
)
7958 *vr0max
= int_const_binop (MINUS_EXPR
, vr1min
,
7959 build_int_cst (TREE_TYPE (vr1min
), 1));
7963 else if (*vr0type
== VR_RANGE
7964 && vr1type
== VR_ANTI_RANGE
)
7966 if (TREE_CODE (*vr0max
) == INTEGER_CST
)
7969 *vr0min
= int_const_binop (PLUS_EXPR
, *vr0max
,
7970 build_int_cst (TREE_TYPE (*vr0max
), 1));
7979 else if ((operand_less_p (*vr0min
, vr1max
) == 1
7980 || operand_equal_p (*vr0min
, vr1max
, 0))
7981 && operand_less_p (vr1min
, *vr0min
) == 1
7982 && operand_less_p (vr1max
, *vr0max
) == 1)
7984 /* ( [ ) ] or ( )[ ] */
7985 if (*vr0type
== VR_RANGE
7986 && vr1type
== VR_RANGE
)
7988 else if (*vr0type
== VR_ANTI_RANGE
7989 && vr1type
== VR_ANTI_RANGE
)
7991 else if (*vr0type
== VR_ANTI_RANGE
7992 && vr1type
== VR_RANGE
)
7994 if (TREE_CODE (vr1max
) == INTEGER_CST
)
7995 *vr0min
= int_const_binop (PLUS_EXPR
, vr1max
,
7996 build_int_cst (TREE_TYPE (vr1max
), 1));
8000 else if (*vr0type
== VR_RANGE
8001 && vr1type
== VR_ANTI_RANGE
)
8003 if (TREE_CODE (*vr0min
) == INTEGER_CST
)
8007 *vr0max
= int_const_binop (MINUS_EXPR
, *vr0min
,
8008 build_int_cst (TREE_TYPE (*vr0min
), 1));
8022 *vr0type
= VR_VARYING
;
8023 *vr0min
= NULL_TREE
;
8024 *vr0max
= NULL_TREE
;
8027 /* Intersect the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
8028 { VR1TYPE, VR0MIN, VR0MAX } and store the result
8029 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
8030 possible such range. The resulting range is not canonicalized. */
8033 intersect_ranges (enum value_range_type
*vr0type
,
8034 tree
*vr0min
, tree
*vr0max
,
8035 enum value_range_type vr1type
,
8036 tree vr1min
, tree vr1max
)
8038 bool mineq
= operand_equal_p (*vr0min
, vr1min
, 0);
8039 bool maxeq
= operand_equal_p (*vr0max
, vr1max
, 0);
8041 /* [] is vr0, () is vr1 in the following classification comments. */
8045 if (*vr0type
== vr1type
)
8046 /* Nothing to do for equal ranges. */
8048 else if ((*vr0type
== VR_RANGE
8049 && vr1type
== VR_ANTI_RANGE
)
8050 || (*vr0type
== VR_ANTI_RANGE
8051 && vr1type
== VR_RANGE
))
8053 /* For anti-range with range intersection the result is empty. */
8054 *vr0type
= VR_UNDEFINED
;
8055 *vr0min
= NULL_TREE
;
8056 *vr0max
= NULL_TREE
;
8061 else if (operand_less_p (*vr0max
, vr1min
) == 1
8062 || operand_less_p (vr1max
, *vr0min
) == 1)
8064 /* [ ] ( ) or ( ) [ ]
8065 If the ranges have an empty intersection, the result of the
8066 intersect operation is the range for intersecting an
8067 anti-range with a range or empty when intersecting two ranges. */
8068 if (*vr0type
== VR_RANGE
8069 && vr1type
== VR_ANTI_RANGE
)
8071 else if (*vr0type
== VR_ANTI_RANGE
8072 && vr1type
== VR_RANGE
)
8078 else if (*vr0type
== VR_RANGE
8079 && vr1type
== VR_RANGE
)
8081 *vr0type
= VR_UNDEFINED
;
8082 *vr0min
= NULL_TREE
;
8083 *vr0max
= NULL_TREE
;
8085 else if (*vr0type
== VR_ANTI_RANGE
8086 && vr1type
== VR_ANTI_RANGE
)
8088 /* If the anti-ranges are adjacent to each other merge them. */
8089 if (TREE_CODE (*vr0max
) == INTEGER_CST
8090 && TREE_CODE (vr1min
) == INTEGER_CST
8091 && operand_less_p (*vr0max
, vr1min
) == 1
8092 && integer_onep (int_const_binop (MINUS_EXPR
,
8095 else if (TREE_CODE (vr1max
) == INTEGER_CST
8096 && TREE_CODE (*vr0min
) == INTEGER_CST
8097 && operand_less_p (vr1max
, *vr0min
) == 1
8098 && integer_onep (int_const_binop (MINUS_EXPR
,
8101 /* Else arbitrarily take VR0. */
8104 else if ((maxeq
|| operand_less_p (vr1max
, *vr0max
) == 1)
8105 && (mineq
|| operand_less_p (*vr0min
, vr1min
) == 1))
8107 /* [ ( ) ] or [( ) ] or [ ( )] */
8108 if (*vr0type
== VR_RANGE
8109 && vr1type
== VR_RANGE
)
8111 /* If both are ranges the result is the inner one. */
8116 else if (*vr0type
== VR_RANGE
8117 && vr1type
== VR_ANTI_RANGE
)
8119 /* Choose the right gap if the left one is empty. */
8122 if (TREE_CODE (vr1max
) == INTEGER_CST
)
8123 *vr0min
= int_const_binop (PLUS_EXPR
, vr1max
,
8124 build_int_cst (TREE_TYPE (vr1max
), 1));
8128 /* Choose the left gap if the right one is empty. */
8131 if (TREE_CODE (vr1min
) == INTEGER_CST
)
8132 *vr0max
= int_const_binop (MINUS_EXPR
, vr1min
,
8133 build_int_cst (TREE_TYPE (vr1min
), 1));
8137 /* Choose the anti-range if the range is effectively varying. */
8138 else if (vrp_val_is_min (*vr0min
)
8139 && vrp_val_is_max (*vr0max
))
8145 /* Else choose the range. */
8147 else if (*vr0type
== VR_ANTI_RANGE
8148 && vr1type
== VR_ANTI_RANGE
)
8149 /* If both are anti-ranges the result is the outer one. */
8151 else if (*vr0type
== VR_ANTI_RANGE
8152 && vr1type
== VR_RANGE
)
8154 /* The intersection is empty. */
8155 *vr0type
= VR_UNDEFINED
;
8156 *vr0min
= NULL_TREE
;
8157 *vr0max
= NULL_TREE
;
8162 else if ((maxeq
|| operand_less_p (*vr0max
, vr1max
) == 1)
8163 && (mineq
|| operand_less_p (vr1min
, *vr0min
) == 1))
8165 /* ( [ ] ) or ([ ] ) or ( [ ]) */
8166 if (*vr0type
== VR_RANGE
8167 && vr1type
== VR_RANGE
)
8168 /* Choose the inner range. */
8170 else if (*vr0type
== VR_ANTI_RANGE
8171 && vr1type
== VR_RANGE
)
8173 /* Choose the right gap if the left is empty. */
8176 *vr0type
= VR_RANGE
;
8177 if (TREE_CODE (*vr0max
) == INTEGER_CST
)
8178 *vr0min
= int_const_binop (PLUS_EXPR
, *vr0max
,
8179 build_int_cst (TREE_TYPE (*vr0max
), 1));
8184 /* Choose the left gap if the right is empty. */
8187 *vr0type
= VR_RANGE
;
8188 if (TREE_CODE (*vr0min
) == INTEGER_CST
)
8189 *vr0max
= int_const_binop (MINUS_EXPR
, *vr0min
,
8190 build_int_cst (TREE_TYPE (*vr0min
), 1));
8195 /* Choose the anti-range if the range is effectively varying. */
8196 else if (vrp_val_is_min (vr1min
)
8197 && vrp_val_is_max (vr1max
))
8199 /* Else choose the range. */
8207 else if (*vr0type
== VR_ANTI_RANGE
8208 && vr1type
== VR_ANTI_RANGE
)
8210 /* If both are anti-ranges the result is the outer one. */
8215 else if (vr1type
== VR_ANTI_RANGE
8216 && *vr0type
== VR_RANGE
)
8218 /* The intersection is empty. */
8219 *vr0type
= VR_UNDEFINED
;
8220 *vr0min
= NULL_TREE
;
8221 *vr0max
= NULL_TREE
;
8226 else if ((operand_less_p (vr1min
, *vr0max
) == 1
8227 || operand_equal_p (vr1min
, *vr0max
, 0))
8228 && operand_less_p (*vr0min
, vr1min
) == 1)
8230 /* [ ( ] ) or [ ]( ) */
8231 if (*vr0type
== VR_ANTI_RANGE
8232 && vr1type
== VR_ANTI_RANGE
)
8234 else if (*vr0type
== VR_RANGE
8235 && vr1type
== VR_RANGE
)
8237 else if (*vr0type
== VR_RANGE
8238 && vr1type
== VR_ANTI_RANGE
)
8240 if (TREE_CODE (vr1min
) == INTEGER_CST
)
8241 *vr0max
= int_const_binop (MINUS_EXPR
, vr1min
,
8242 build_int_cst (TREE_TYPE (vr1min
), 1));
8246 else if (*vr0type
== VR_ANTI_RANGE
8247 && vr1type
== VR_RANGE
)
8249 *vr0type
= VR_RANGE
;
8250 if (TREE_CODE (*vr0max
) == INTEGER_CST
)
8251 *vr0min
= int_const_binop (PLUS_EXPR
, *vr0max
,
8252 build_int_cst (TREE_TYPE (*vr0max
), 1));
8260 else if ((operand_less_p (*vr0min
, vr1max
) == 1
8261 || operand_equal_p (*vr0min
, vr1max
, 0))
8262 && operand_less_p (vr1min
, *vr0min
) == 1)
8264 /* ( [ ) ] or ( )[ ] */
8265 if (*vr0type
== VR_ANTI_RANGE
8266 && vr1type
== VR_ANTI_RANGE
)
8268 else if (*vr0type
== VR_RANGE
8269 && vr1type
== VR_RANGE
)
8271 else if (*vr0type
== VR_RANGE
8272 && vr1type
== VR_ANTI_RANGE
)
8274 if (TREE_CODE (vr1max
) == INTEGER_CST
)
8275 *vr0min
= int_const_binop (PLUS_EXPR
, vr1max
,
8276 build_int_cst (TREE_TYPE (vr1max
), 1));
8280 else if (*vr0type
== VR_ANTI_RANGE
8281 && vr1type
== VR_RANGE
)
8283 *vr0type
= VR_RANGE
;
8284 if (TREE_CODE (*vr0min
) == INTEGER_CST
)
8285 *vr0max
= int_const_binop (MINUS_EXPR
, *vr0min
,
8286 build_int_cst (TREE_TYPE (*vr0min
), 1));
8295 /* As a fallback simply use { *VRTYPE, *VR0MIN, *VR0MAX } as
8296 result for the intersection. That's always a conservative
8297 correct estimate. */
8303 /* Intersect the two value-ranges *VR0 and *VR1 and store the result
8304 in *VR0. This may not be the smallest possible such range. */
8307 vrp_intersect_ranges_1 (value_range_t
*vr0
, value_range_t
*vr1
)
8309 value_range_t saved
;
8311 /* If either range is VR_VARYING the other one wins. */
8312 if (vr1
->type
== VR_VARYING
)
8314 if (vr0
->type
== VR_VARYING
)
8316 copy_value_range (vr0
, vr1
);
8320 /* When either range is VR_UNDEFINED the resulting range is
8321 VR_UNDEFINED, too. */
8322 if (vr0
->type
== VR_UNDEFINED
)
8324 if (vr1
->type
== VR_UNDEFINED
)
8326 set_value_range_to_undefined (vr0
);
8330 /* Save the original vr0 so we can return it as conservative intersection
8331 result when our worker turns things to varying. */
8333 intersect_ranges (&vr0
->type
, &vr0
->min
, &vr0
->max
,
8334 vr1
->type
, vr1
->min
, vr1
->max
);
8335 /* Make sure to canonicalize the result though as the inversion of a
8336 VR_RANGE can still be a VR_RANGE. */
8337 set_and_canonicalize_value_range (vr0
, vr0
->type
,
8338 vr0
->min
, vr0
->max
, vr0
->equiv
);
8339 /* If that failed, use the saved original VR0. */
8340 if (vr0
->type
== VR_VARYING
)
8345 /* If the result is VR_UNDEFINED there is no need to mess with
8346 the equivalencies. */
8347 if (vr0
->type
== VR_UNDEFINED
)
8350 /* The resulting set of equivalences for range intersection is the union of
8352 if (vr0
->equiv
&& vr1
->equiv
&& vr0
->equiv
!= vr1
->equiv
)
8353 bitmap_ior_into (vr0
->equiv
, vr1
->equiv
);
8354 else if (vr1
->equiv
&& !vr0
->equiv
)
8355 bitmap_copy (vr0
->equiv
, vr1
->equiv
);
8359 vrp_intersect_ranges (value_range_t
*vr0
, value_range_t
*vr1
)
8361 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8363 fprintf (dump_file
, "Intersecting\n ");
8364 dump_value_range (dump_file
, vr0
);
8365 fprintf (dump_file
, "\nand\n ");
8366 dump_value_range (dump_file
, vr1
);
8367 fprintf (dump_file
, "\n");
8369 vrp_intersect_ranges_1 (vr0
, vr1
);
8370 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8372 fprintf (dump_file
, "to\n ");
8373 dump_value_range (dump_file
, vr0
);
8374 fprintf (dump_file
, "\n");
8378 /* Meet operation for value ranges. Given two value ranges VR0 and
8379 VR1, store in VR0 a range that contains both VR0 and VR1. This
8380 may not be the smallest possible such range. */
8383 vrp_meet_1 (value_range_t
*vr0
, value_range_t
*vr1
)
8385 value_range_t saved
;
8387 if (vr0
->type
== VR_UNDEFINED
)
8389 set_value_range (vr0
, vr1
->type
, vr1
->min
, vr1
->max
, vr1
->equiv
);
8393 if (vr1
->type
== VR_UNDEFINED
)
8395 /* VR0 already has the resulting range. */
8399 if (vr0
->type
== VR_VARYING
)
8401 /* Nothing to do. VR0 already has the resulting range. */
8405 if (vr1
->type
== VR_VARYING
)
8407 set_value_range_to_varying (vr0
);
8412 union_ranges (&vr0
->type
, &vr0
->min
, &vr0
->max
,
8413 vr1
->type
, vr1
->min
, vr1
->max
);
8414 if (vr0
->type
== VR_VARYING
)
8416 /* Failed to find an efficient meet. Before giving up and setting
8417 the result to VARYING, see if we can at least derive a useful
8418 anti-range. FIXME, all this nonsense about distinguishing
8419 anti-ranges from ranges is necessary because of the odd
8420 semantics of range_includes_zero_p and friends. */
8421 if (((saved
.type
== VR_RANGE
8422 && range_includes_zero_p (saved
.min
, saved
.max
) == 0)
8423 || (saved
.type
== VR_ANTI_RANGE
8424 && range_includes_zero_p (saved
.min
, saved
.max
) == 1))
8425 && ((vr1
->type
== VR_RANGE
8426 && range_includes_zero_p (vr1
->min
, vr1
->max
) == 0)
8427 || (vr1
->type
== VR_ANTI_RANGE
8428 && range_includes_zero_p (vr1
->min
, vr1
->max
) == 1)))
8430 set_value_range_to_nonnull (vr0
, TREE_TYPE (saved
.min
));
8432 /* Since this meet operation did not result from the meeting of
8433 two equivalent names, VR0 cannot have any equivalences. */
8435 bitmap_clear (vr0
->equiv
);
8439 set_value_range_to_varying (vr0
);
8442 set_and_canonicalize_value_range (vr0
, vr0
->type
, vr0
->min
, vr0
->max
,
8444 if (vr0
->type
== VR_VARYING
)
8447 /* The resulting set of equivalences is always the intersection of
8449 if (vr0
->equiv
&& vr1
->equiv
&& vr0
->equiv
!= vr1
->equiv
)
8450 bitmap_and_into (vr0
->equiv
, vr1
->equiv
);
8451 else if (vr0
->equiv
&& !vr1
->equiv
)
8452 bitmap_clear (vr0
->equiv
);
8456 vrp_meet (value_range_t
*vr0
, value_range_t
*vr1
)
8458 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8460 fprintf (dump_file
, "Meeting\n ");
8461 dump_value_range (dump_file
, vr0
);
8462 fprintf (dump_file
, "\nand\n ");
8463 dump_value_range (dump_file
, vr1
);
8464 fprintf (dump_file
, "\n");
8466 vrp_meet_1 (vr0
, vr1
);
8467 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8469 fprintf (dump_file
, "to\n ");
8470 dump_value_range (dump_file
, vr0
);
8471 fprintf (dump_file
, "\n");
8476 /* Visit all arguments for PHI node PHI that flow through executable
8477 edges. If a valid value range can be derived from all the incoming
8478 value ranges, set a new range for the LHS of PHI. */
8480 static enum ssa_prop_result
8481 vrp_visit_phi_node (gimple phi
)
8484 tree lhs
= PHI_RESULT (phi
);
8485 value_range_t
*lhs_vr
= get_value_range (lhs
);
8486 value_range_t vr_result
= VR_INITIALIZER
;
8488 int edges
, old_edges
;
8491 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8493 fprintf (dump_file
, "\nVisiting PHI node: ");
8494 print_gimple_stmt (dump_file
, phi
, 0, dump_flags
);
8498 for (i
= 0; i
< gimple_phi_num_args (phi
); i
++)
8500 edge e
= gimple_phi_arg_edge (phi
, i
);
8502 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8505 " Argument #%d (%d -> %d %sexecutable)\n",
8506 (int) i
, e
->src
->index
, e
->dest
->index
,
8507 (e
->flags
& EDGE_EXECUTABLE
) ? "" : "not ");
8510 if (e
->flags
& EDGE_EXECUTABLE
)
8512 tree arg
= PHI_ARG_DEF (phi
, i
);
8513 value_range_t vr_arg
;
8517 if (TREE_CODE (arg
) == SSA_NAME
)
8519 vr_arg
= *(get_value_range (arg
));
8520 /* Do not allow equivalences or symbolic ranges to leak in from
8521 backedges. That creates invalid equivalencies.
8522 See PR53465 and PR54767. */
8523 if (e
->flags
& EDGE_DFS_BACK
)
8525 if (vr_arg
.type
== VR_RANGE
8526 || vr_arg
.type
== VR_ANTI_RANGE
)
8528 vr_arg
.equiv
= NULL
;
8529 if (symbolic_range_p (&vr_arg
))
8531 vr_arg
.type
= VR_VARYING
;
8532 vr_arg
.min
= NULL_TREE
;
8533 vr_arg
.max
= NULL_TREE
;
8539 /* If the non-backedge arguments range is VR_VARYING then
8540 we can still try recording a simple equivalence. */
8541 if (vr_arg
.type
== VR_VARYING
)
8543 vr_arg
.type
= VR_RANGE
;
8546 vr_arg
.equiv
= NULL
;
8552 if (TREE_OVERFLOW_P (arg
))
8553 arg
= drop_tree_overflow (arg
);
8555 vr_arg
.type
= VR_RANGE
;
8558 vr_arg
.equiv
= NULL
;
8561 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8563 fprintf (dump_file
, "\t");
8564 print_generic_expr (dump_file
, arg
, dump_flags
);
8565 fprintf (dump_file
, ": ");
8566 dump_value_range (dump_file
, &vr_arg
);
8567 fprintf (dump_file
, "\n");
8571 copy_value_range (&vr_result
, &vr_arg
);
8573 vrp_meet (&vr_result
, &vr_arg
);
8576 if (vr_result
.type
== VR_VARYING
)
8581 if (vr_result
.type
== VR_VARYING
)
8583 else if (vr_result
.type
== VR_UNDEFINED
)
8586 old_edges
= vr_phi_edge_counts
[SSA_NAME_VERSION (lhs
)];
8587 vr_phi_edge_counts
[SSA_NAME_VERSION (lhs
)] = edges
;
8589 /* To prevent infinite iterations in the algorithm, derive ranges
8590 when the new value is slightly bigger or smaller than the
8591 previous one. We don't do this if we have seen a new executable
8592 edge; this helps us avoid an overflow infinity for conditionals
8593 which are not in a loop. If the old value-range was VR_UNDEFINED
8594 use the updated range and iterate one more time. */
8596 && gimple_phi_num_args (phi
) > 1
8597 && edges
== old_edges
8598 && lhs_vr
->type
!= VR_UNDEFINED
)
8600 /* Compare old and new ranges, fall back to varying if the
8601 values are not comparable. */
8602 int cmp_min
= compare_values (lhs_vr
->min
, vr_result
.min
);
8605 int cmp_max
= compare_values (lhs_vr
->max
, vr_result
.max
);
8609 /* For non VR_RANGE or for pointers fall back to varying if
8610 the range changed. */
8611 if ((lhs_vr
->type
!= VR_RANGE
|| vr_result
.type
!= VR_RANGE
8612 || POINTER_TYPE_P (TREE_TYPE (lhs
)))
8613 && (cmp_min
!= 0 || cmp_max
!= 0))
8616 /* If the new minimum is larger than than the previous one
8617 retain the old value. If the new minimum value is smaller
8618 than the previous one and not -INF go all the way to -INF + 1.
8619 In the first case, to avoid infinite bouncing between different
8620 minimums, and in the other case to avoid iterating millions of
8621 times to reach -INF. Going to -INF + 1 also lets the following
8622 iteration compute whether there will be any overflow, at the
8623 expense of one additional iteration. */
8625 vr_result
.min
= lhs_vr
->min
;
8626 else if (cmp_min
> 0
8627 && !vrp_val_is_min (vr_result
.min
))
8629 = int_const_binop (PLUS_EXPR
,
8630 vrp_val_min (TREE_TYPE (vr_result
.min
)),
8631 build_int_cst (TREE_TYPE (vr_result
.min
), 1));
8633 /* Similarly for the maximum value. */
8635 vr_result
.max
= lhs_vr
->max
;
8636 else if (cmp_max
< 0
8637 && !vrp_val_is_max (vr_result
.max
))
8639 = int_const_binop (MINUS_EXPR
,
8640 vrp_val_max (TREE_TYPE (vr_result
.min
)),
8641 build_int_cst (TREE_TYPE (vr_result
.min
), 1));
8643 /* If we dropped either bound to +-INF then if this is a loop
8644 PHI node SCEV may known more about its value-range. */
8645 if ((cmp_min
> 0 || cmp_min
< 0
8646 || cmp_max
< 0 || cmp_max
> 0)
8647 && (l
= loop_containing_stmt (phi
))
8648 && l
->header
== gimple_bb (phi
))
8649 adjust_range_with_scev (&vr_result
, l
, phi
, lhs
);
8651 /* If we will end up with a (-INF, +INF) range, set it to
8652 VARYING. Same if the previous max value was invalid for
8653 the type and we end up with vr_result.min > vr_result.max. */
8654 if ((vrp_val_is_max (vr_result
.max
)
8655 && vrp_val_is_min (vr_result
.min
))
8656 || compare_values (vr_result
.min
,
8661 /* If the new range is different than the previous value, keep
8664 if (update_value_range (lhs
, &vr_result
))
8666 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8668 fprintf (dump_file
, "Found new range for ");
8669 print_generic_expr (dump_file
, lhs
, 0);
8670 fprintf (dump_file
, ": ");
8671 dump_value_range (dump_file
, &vr_result
);
8672 fprintf (dump_file
, "\n");
8675 return SSA_PROP_INTERESTING
;
8678 /* Nothing changed, don't add outgoing edges. */
8679 return SSA_PROP_NOT_INTERESTING
;
8681 /* No match found. Set the LHS to VARYING. */
8683 set_value_range_to_varying (lhs_vr
);
8684 return SSA_PROP_VARYING
;
8687 /* Simplify boolean operations if the source is known
8688 to be already a boolean. */
8690 simplify_truth_ops_using_ranges (gimple_stmt_iterator
*gsi
, gimple stmt
)
8692 enum tree_code rhs_code
= gimple_assign_rhs_code (stmt
);
8694 bool need_conversion
;
8696 /* We handle only !=/== case here. */
8697 gcc_assert (rhs_code
== EQ_EXPR
|| rhs_code
== NE_EXPR
);
8699 op0
= gimple_assign_rhs1 (stmt
);
8700 if (!op_with_boolean_value_range_p (op0
))
8703 op1
= gimple_assign_rhs2 (stmt
);
8704 if (!op_with_boolean_value_range_p (op1
))
8707 /* Reduce number of cases to handle to NE_EXPR. As there is no
8708 BIT_XNOR_EXPR we cannot replace A == B with a single statement. */
8709 if (rhs_code
== EQ_EXPR
)
8711 if (TREE_CODE (op1
) == INTEGER_CST
)
8712 op1
= int_const_binop (BIT_XOR_EXPR
, op1
,
8713 build_int_cst (TREE_TYPE (op1
), 1));
8718 lhs
= gimple_assign_lhs (stmt
);
8720 = !useless_type_conversion_p (TREE_TYPE (lhs
), TREE_TYPE (op0
));
8722 /* Make sure to not sign-extend a 1-bit 1 when converting the result. */
8724 && !TYPE_UNSIGNED (TREE_TYPE (op0
))
8725 && TYPE_PRECISION (TREE_TYPE (op0
)) == 1
8726 && TYPE_PRECISION (TREE_TYPE (lhs
)) > 1)
8729 /* For A != 0 we can substitute A itself. */
8730 if (integer_zerop (op1
))
8731 gimple_assign_set_rhs_with_ops (gsi
,
8733 ? NOP_EXPR
: TREE_CODE (op0
),
8735 /* For A != B we substitute A ^ B. Either with conversion. */
8736 else if (need_conversion
)
8738 tree tem
= make_ssa_name (TREE_TYPE (op0
), NULL
);
8739 gimple newop
= gimple_build_assign_with_ops (BIT_XOR_EXPR
, tem
, op0
, op1
);
8740 gsi_insert_before (gsi
, newop
, GSI_SAME_STMT
);
8741 gimple_assign_set_rhs_with_ops (gsi
, NOP_EXPR
, tem
, NULL_TREE
);
8745 gimple_assign_set_rhs_with_ops (gsi
, BIT_XOR_EXPR
, op0
, op1
);
8746 update_stmt (gsi_stmt (*gsi
));
8751 /* Simplify a division or modulo operator to a right shift or
8752 bitwise and if the first operand is unsigned or is greater
8753 than zero and the second operand is an exact power of two. */
8756 simplify_div_or_mod_using_ranges (gimple stmt
)
8758 enum tree_code rhs_code
= gimple_assign_rhs_code (stmt
);
8760 tree op0
= gimple_assign_rhs1 (stmt
);
8761 tree op1
= gimple_assign_rhs2 (stmt
);
8762 value_range_t
*vr
= get_value_range (gimple_assign_rhs1 (stmt
));
8764 if (TYPE_UNSIGNED (TREE_TYPE (op0
)))
8766 val
= integer_one_node
;
8772 val
= compare_range_with_value (GE_EXPR
, vr
, integer_zero_node
, &sop
);
8776 && integer_onep (val
)
8777 && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC
))
8779 location_t location
;
8781 if (!gimple_has_location (stmt
))
8782 location
= input_location
;
8784 location
= gimple_location (stmt
);
8785 warning_at (location
, OPT_Wstrict_overflow
,
8786 "assuming signed overflow does not occur when "
8787 "simplifying %</%> or %<%%%> to %<>>%> or %<&%>");
8791 if (val
&& integer_onep (val
))
8795 if (rhs_code
== TRUNC_DIV_EXPR
)
8797 t
= build_int_cst (integer_type_node
, tree_log2 (op1
));
8798 gimple_assign_set_rhs_code (stmt
, RSHIFT_EXPR
);
8799 gimple_assign_set_rhs1 (stmt
, op0
);
8800 gimple_assign_set_rhs2 (stmt
, t
);
8804 t
= build_int_cst (TREE_TYPE (op1
), 1);
8805 t
= int_const_binop (MINUS_EXPR
, op1
, t
);
8806 t
= fold_convert (TREE_TYPE (op0
), t
);
8808 gimple_assign_set_rhs_code (stmt
, BIT_AND_EXPR
);
8809 gimple_assign_set_rhs1 (stmt
, op0
);
8810 gimple_assign_set_rhs2 (stmt
, t
);
8820 /* If the operand to an ABS_EXPR is >= 0, then eliminate the
8821 ABS_EXPR. If the operand is <= 0, then simplify the
8822 ABS_EXPR into a NEGATE_EXPR. */
8825 simplify_abs_using_ranges (gimple stmt
)
8828 tree op
= gimple_assign_rhs1 (stmt
);
8829 tree type
= TREE_TYPE (op
);
8830 value_range_t
*vr
= get_value_range (op
);
8832 if (TYPE_UNSIGNED (type
))
8834 val
= integer_zero_node
;
8840 val
= compare_range_with_value (LE_EXPR
, vr
, integer_zero_node
, &sop
);
8844 val
= compare_range_with_value (GE_EXPR
, vr
, integer_zero_node
,
8849 if (integer_zerop (val
))
8850 val
= integer_one_node
;
8851 else if (integer_onep (val
))
8852 val
= integer_zero_node
;
8857 && (integer_onep (val
) || integer_zerop (val
)))
8859 if (sop
&& issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC
))
8861 location_t location
;
8863 if (!gimple_has_location (stmt
))
8864 location
= input_location
;
8866 location
= gimple_location (stmt
);
8867 warning_at (location
, OPT_Wstrict_overflow
,
8868 "assuming signed overflow does not occur when "
8869 "simplifying %<abs (X)%> to %<X%> or %<-X%>");
8872 gimple_assign_set_rhs1 (stmt
, op
);
8873 if (integer_onep (val
))
8874 gimple_assign_set_rhs_code (stmt
, NEGATE_EXPR
);
8876 gimple_assign_set_rhs_code (stmt
, SSA_NAME
);
8885 /* Optimize away redundant BIT_AND_EXPR and BIT_IOR_EXPR.
8886 If all the bits that are being cleared by & are already
8887 known to be zero from VR, or all the bits that are being
8888 set by | are already known to be one from VR, the bit
8889 operation is redundant. */
8892 simplify_bit_ops_using_ranges (gimple_stmt_iterator
*gsi
, gimple stmt
)
8894 tree op0
= gimple_assign_rhs1 (stmt
);
8895 tree op1
= gimple_assign_rhs2 (stmt
);
8896 tree op
= NULL_TREE
;
8897 value_range_t vr0
= VR_INITIALIZER
;
8898 value_range_t vr1
= VR_INITIALIZER
;
8899 wide_int may_be_nonzero0
, may_be_nonzero1
;
8900 wide_int must_be_nonzero0
, must_be_nonzero1
;
8903 if (TREE_CODE (op0
) == SSA_NAME
)
8904 vr0
= *(get_value_range (op0
));
8905 else if (is_gimple_min_invariant (op0
))
8906 set_value_range_to_value (&vr0
, op0
, NULL
);
8910 if (TREE_CODE (op1
) == SSA_NAME
)
8911 vr1
= *(get_value_range (op1
));
8912 else if (is_gimple_min_invariant (op1
))
8913 set_value_range_to_value (&vr1
, op1
, NULL
);
8917 if (!zero_nonzero_bits_from_vr (TREE_TYPE (op0
), &vr0
, &may_be_nonzero0
,
8920 if (!zero_nonzero_bits_from_vr (TREE_TYPE (op1
), &vr1
, &may_be_nonzero1
,
8924 switch (gimple_assign_rhs_code (stmt
))
8927 mask
= may_be_nonzero0
.and_not (must_be_nonzero1
);
8933 mask
= may_be_nonzero1
.and_not (must_be_nonzero0
);
8941 mask
= may_be_nonzero0
.and_not (must_be_nonzero1
);
8947 mask
= may_be_nonzero1
.and_not (must_be_nonzero0
);
8958 if (op
== NULL_TREE
)
8961 gimple_assign_set_rhs_with_ops (gsi
, TREE_CODE (op
), op
, NULL
);
8962 update_stmt (gsi_stmt (*gsi
));
8966 /* We are comparing trees OP0 and OP1 using COND_CODE. OP0 has
8967 a known value range VR.
8969 If there is one and only one value which will satisfy the
8970 conditional, then return that value. Else return NULL. */
8973 test_for_singularity (enum tree_code cond_code
, tree op0
,
8974 tree op1
, value_range_t
*vr
)
8979 /* Extract minimum/maximum values which satisfy the
8980 the conditional as it was written. */
8981 if (cond_code
== LE_EXPR
|| cond_code
== LT_EXPR
)
8983 /* This should not be negative infinity; there is no overflow
8985 min
= TYPE_MIN_VALUE (TREE_TYPE (op0
));
8988 if (cond_code
== LT_EXPR
&& !is_overflow_infinity (max
))
8990 tree one
= build_int_cst (TREE_TYPE (op0
), 1);
8991 max
= fold_build2 (MINUS_EXPR
, TREE_TYPE (op0
), max
, one
);
8993 TREE_NO_WARNING (max
) = 1;
8996 else if (cond_code
== GE_EXPR
|| cond_code
== GT_EXPR
)
8998 /* This should not be positive infinity; there is no overflow
9000 max
= TYPE_MAX_VALUE (TREE_TYPE (op0
));
9003 if (cond_code
== GT_EXPR
&& !is_overflow_infinity (min
))
9005 tree one
= build_int_cst (TREE_TYPE (op0
), 1);
9006 min
= fold_build2 (PLUS_EXPR
, TREE_TYPE (op0
), min
, one
);
9008 TREE_NO_WARNING (min
) = 1;
9012 /* Now refine the minimum and maximum values using any
9013 value range information we have for op0. */
9016 if (compare_values (vr
->min
, min
) == 1)
9018 if (compare_values (vr
->max
, max
) == -1)
9021 /* If the new min/max values have converged to a single value,
9022 then there is only one value which can satisfy the condition,
9023 return that value. */
9024 if (operand_equal_p (min
, max
, 0) && is_gimple_min_invariant (min
))
9030 /* Return whether the value range *VR fits in an integer type specified
9031 by PRECISION and UNSIGNED_P. */
9034 range_fits_type_p (value_range_t
*vr
, unsigned dest_precision
, signop dest_sgn
)
9037 unsigned src_precision
;
9041 /* We can only handle integral and pointer types. */
9042 src_type
= TREE_TYPE (vr
->min
);
9043 if (!INTEGRAL_TYPE_P (src_type
)
9044 && !POINTER_TYPE_P (src_type
))
9047 /* An extension is fine unless VR is SIGNED and dest_sgn is UNSIGNED,
9048 and so is an identity transform. */
9049 src_precision
= TYPE_PRECISION (TREE_TYPE (vr
->min
));
9050 src_sgn
= TYPE_SIGN (src_type
);
9051 if ((src_precision
< dest_precision
9052 && !(dest_sgn
== UNSIGNED
&& src_sgn
== SIGNED
))
9053 || (src_precision
== dest_precision
&& src_sgn
== dest_sgn
))
9056 /* Now we can only handle ranges with constant bounds. */
9057 if (vr
->type
!= VR_RANGE
9058 || TREE_CODE (vr
->min
) != INTEGER_CST
9059 || TREE_CODE (vr
->max
) != INTEGER_CST
)
9062 /* For sign changes, the MSB of the wide_int has to be clear.
9063 An unsigned value with its MSB set cannot be represented by
9064 a signed wide_int, while a negative value cannot be represented
9065 by an unsigned wide_int. */
9066 if (src_sgn
!= dest_sgn
9067 && (wi::lts_p (vr
->min
, 0) || wi::lts_p (vr
->max
, 0)))
9070 /* Then we can perform the conversion on both ends and compare
9071 the result for equality. */
9072 tem
= wi::ext (wi::to_widest (vr
->min
), dest_precision
, dest_sgn
);
9073 if (tem
!= wi::to_widest (vr
->min
))
9075 tem
= wi::ext (wi::to_widest (vr
->max
), dest_precision
, dest_sgn
);
9076 if (tem
!= wi::to_widest (vr
->max
))
9082 /* Simplify a conditional using a relational operator to an equality
9083 test if the range information indicates only one value can satisfy
9084 the original conditional. */
9087 simplify_cond_using_ranges (gimple stmt
)
9089 tree op0
= gimple_cond_lhs (stmt
);
9090 tree op1
= gimple_cond_rhs (stmt
);
9091 enum tree_code cond_code
= gimple_cond_code (stmt
);
9093 if (cond_code
!= NE_EXPR
9094 && cond_code
!= EQ_EXPR
9095 && TREE_CODE (op0
) == SSA_NAME
9096 && INTEGRAL_TYPE_P (TREE_TYPE (op0
))
9097 && is_gimple_min_invariant (op1
))
9099 value_range_t
*vr
= get_value_range (op0
);
9101 /* If we have range information for OP0, then we might be
9102 able to simplify this conditional. */
9103 if (vr
->type
== VR_RANGE
)
9105 tree new_tree
= test_for_singularity (cond_code
, op0
, op1
, vr
);
9111 fprintf (dump_file
, "Simplified relational ");
9112 print_gimple_stmt (dump_file
, stmt
, 0, 0);
9113 fprintf (dump_file
, " into ");
9116 gimple_cond_set_code (stmt
, EQ_EXPR
);
9117 gimple_cond_set_lhs (stmt
, op0
);
9118 gimple_cond_set_rhs (stmt
, new_tree
);
9124 print_gimple_stmt (dump_file
, stmt
, 0, 0);
9125 fprintf (dump_file
, "\n");
9131 /* Try again after inverting the condition. We only deal
9132 with integral types here, so no need to worry about
9133 issues with inverting FP comparisons. */
9134 cond_code
= invert_tree_comparison (cond_code
, false);
9135 new_tree
= test_for_singularity (cond_code
, op0
, op1
, vr
);
9141 fprintf (dump_file
, "Simplified relational ");
9142 print_gimple_stmt (dump_file
, stmt
, 0, 0);
9143 fprintf (dump_file
, " into ");
9146 gimple_cond_set_code (stmt
, NE_EXPR
);
9147 gimple_cond_set_lhs (stmt
, op0
);
9148 gimple_cond_set_rhs (stmt
, new_tree
);
9154 print_gimple_stmt (dump_file
, stmt
, 0, 0);
9155 fprintf (dump_file
, "\n");
9163 /* If we have a comparison of an SSA_NAME (OP0) against a constant,
9164 see if OP0 was set by a type conversion where the source of
9165 the conversion is another SSA_NAME with a range that fits
9166 into the range of OP0's type.
9168 If so, the conversion is redundant as the earlier SSA_NAME can be
9169 used for the comparison directly if we just massage the constant in the
9171 if (TREE_CODE (op0
) == SSA_NAME
9172 && TREE_CODE (op1
) == INTEGER_CST
)
9174 gimple def_stmt
= SSA_NAME_DEF_STMT (op0
);
9177 if (!is_gimple_assign (def_stmt
)
9178 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt
)))
9181 innerop
= gimple_assign_rhs1 (def_stmt
);
9183 if (TREE_CODE (innerop
) == SSA_NAME
9184 && !POINTER_TYPE_P (TREE_TYPE (innerop
)))
9186 value_range_t
*vr
= get_value_range (innerop
);
9188 if (range_int_cst_p (vr
)
9189 && range_fits_type_p (vr
,
9190 TYPE_PRECISION (TREE_TYPE (op0
)),
9191 TYPE_SIGN (TREE_TYPE (op0
)))
9192 && int_fits_type_p (op1
, TREE_TYPE (innerop
))
9193 /* The range must not have overflowed, or if it did overflow
9194 we must not be wrapping/trapping overflow and optimizing
9195 with strict overflow semantics. */
9196 && ((!is_negative_overflow_infinity (vr
->min
)
9197 && !is_positive_overflow_infinity (vr
->max
))
9198 || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (innerop
))))
9200 /* If the range overflowed and the user has asked for warnings
9201 when strict overflow semantics were used to optimize code,
9202 issue an appropriate warning. */
9203 if (cond_code
!= EQ_EXPR
&& cond_code
!= NE_EXPR
9204 && (is_negative_overflow_infinity (vr
->min
)
9205 || is_positive_overflow_infinity (vr
->max
))
9206 && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_CONDITIONAL
))
9208 location_t location
;
9210 if (!gimple_has_location (stmt
))
9211 location
= input_location
;
9213 location
= gimple_location (stmt
);
9214 warning_at (location
, OPT_Wstrict_overflow
,
9215 "assuming signed overflow does not occur when "
9216 "simplifying conditional");
9219 tree newconst
= fold_convert (TREE_TYPE (innerop
), op1
);
9220 gimple_cond_set_lhs (stmt
, innerop
);
9221 gimple_cond_set_rhs (stmt
, newconst
);
9230 /* Simplify a switch statement using the value range of the switch
9234 simplify_switch_using_ranges (gimple stmt
)
9236 tree op
= gimple_switch_index (stmt
);
9241 size_t i
= 0, j
= 0, n
, n2
;
9244 size_t k
= 1, l
= 0;
9246 if (TREE_CODE (op
) == SSA_NAME
)
9248 vr
= get_value_range (op
);
9250 /* We can only handle integer ranges. */
9251 if ((vr
->type
!= VR_RANGE
9252 && vr
->type
!= VR_ANTI_RANGE
)
9253 || symbolic_range_p (vr
))
9256 /* Find case label for min/max of the value range. */
9257 take_default
= !find_case_label_ranges (stmt
, vr
, &i
, &j
, &k
, &l
);
9259 else if (TREE_CODE (op
) == INTEGER_CST
)
9261 take_default
= !find_case_label_index (stmt
, 1, op
, &i
);
9275 n
= gimple_switch_num_labels (stmt
);
9277 /* Bail out if this is just all edges taken. */
9283 /* Build a new vector of taken case labels. */
9284 vec2
= make_tree_vec (j
- i
+ 1 + l
- k
+ 1 + (int)take_default
);
9287 /* Add the default edge, if necessary. */
9289 TREE_VEC_ELT (vec2
, n2
++) = gimple_switch_default_label (stmt
);
9291 for (; i
<= j
; ++i
, ++n2
)
9292 TREE_VEC_ELT (vec2
, n2
) = gimple_switch_label (stmt
, i
);
9294 for (; k
<= l
; ++k
, ++n2
)
9295 TREE_VEC_ELT (vec2
, n2
) = gimple_switch_label (stmt
, k
);
9297 /* Mark needed edges. */
9298 for (i
= 0; i
< n2
; ++i
)
9300 e
= find_edge (gimple_bb (stmt
),
9301 label_to_block (CASE_LABEL (TREE_VEC_ELT (vec2
, i
))));
9302 e
->aux
= (void *)-1;
9305 /* Queue not needed edges for later removal. */
9306 FOR_EACH_EDGE (e
, ei
, gimple_bb (stmt
)->succs
)
9308 if (e
->aux
== (void *)-1)
9314 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
9316 fprintf (dump_file
, "removing unreachable case label\n");
9318 to_remove_edges
.safe_push (e
);
9319 e
->flags
&= ~EDGE_EXECUTABLE
;
9322 /* And queue an update for the stmt. */
9325 to_update_switch_stmts
.safe_push (su
);
9329 /* Simplify an integral conversion from an SSA name in STMT. */
9332 simplify_conversion_using_ranges (gimple stmt
)
9334 tree innerop
, middleop
, finaltype
;
9336 value_range_t
*innervr
;
9337 signop inner_sgn
, middle_sgn
, final_sgn
;
9338 unsigned inner_prec
, middle_prec
, final_prec
;
9339 widest_int innermin
, innermed
, innermax
, middlemin
, middlemed
, middlemax
;
9341 finaltype
= TREE_TYPE (gimple_assign_lhs (stmt
));
9342 if (!INTEGRAL_TYPE_P (finaltype
))
9344 middleop
= gimple_assign_rhs1 (stmt
);
9345 def_stmt
= SSA_NAME_DEF_STMT (middleop
);
9346 if (!is_gimple_assign (def_stmt
)
9347 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt
)))
9349 innerop
= gimple_assign_rhs1 (def_stmt
);
9350 if (TREE_CODE (innerop
) != SSA_NAME
9351 || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (innerop
))
9354 /* Get the value-range of the inner operand. */
9355 innervr
= get_value_range (innerop
);
9356 if (innervr
->type
!= VR_RANGE
9357 || TREE_CODE (innervr
->min
) != INTEGER_CST
9358 || TREE_CODE (innervr
->max
) != INTEGER_CST
)
9361 /* Simulate the conversion chain to check if the result is equal if
9362 the middle conversion is removed. */
9363 innermin
= wi::to_widest (innervr
->min
);
9364 innermax
= wi::to_widest (innervr
->max
);
9366 inner_prec
= TYPE_PRECISION (TREE_TYPE (innerop
));
9367 middle_prec
= TYPE_PRECISION (TREE_TYPE (middleop
));
9368 final_prec
= TYPE_PRECISION (finaltype
);
9370 /* If the first conversion is not injective, the second must not
9372 if (wi::gtu_p (innermax
- innermin
,
9373 wi::mask
<widest_int
> (middle_prec
, false))
9374 && middle_prec
< final_prec
)
9376 /* We also want a medium value so that we can track the effect that
9377 narrowing conversions with sign change have. */
9378 inner_sgn
= TYPE_SIGN (TREE_TYPE (innerop
));
9379 if (inner_sgn
== UNSIGNED
)
9380 innermed
= wi::shifted_mask
<widest_int
> (1, inner_prec
- 1, false);
9383 if (wi::cmp (innermin
, innermed
, inner_sgn
) >= 0
9384 || wi::cmp (innermed
, innermax
, inner_sgn
) >= 0)
9385 innermed
= innermin
;
9387 middle_sgn
= TYPE_SIGN (TREE_TYPE (middleop
));
9388 middlemin
= wi::ext (innermin
, middle_prec
, middle_sgn
);
9389 middlemed
= wi::ext (innermed
, middle_prec
, middle_sgn
);
9390 middlemax
= wi::ext (innermax
, middle_prec
, middle_sgn
);
9392 /* Require that the final conversion applied to both the original
9393 and the intermediate range produces the same result. */
9394 final_sgn
= TYPE_SIGN (finaltype
);
9395 if (wi::ext (middlemin
, final_prec
, final_sgn
)
9396 != wi::ext (innermin
, final_prec
, final_sgn
)
9397 || wi::ext (middlemed
, final_prec
, final_sgn
)
9398 != wi::ext (innermed
, final_prec
, final_sgn
)
9399 || wi::ext (middlemax
, final_prec
, final_sgn
)
9400 != wi::ext (innermax
, final_prec
, final_sgn
))
9403 gimple_assign_set_rhs1 (stmt
, innerop
);
9408 /* Simplify a conversion from integral SSA name to float in STMT. */
9411 simplify_float_conversion_using_ranges (gimple_stmt_iterator
*gsi
, gimple stmt
)
9413 tree rhs1
= gimple_assign_rhs1 (stmt
);
9414 value_range_t
*vr
= get_value_range (rhs1
);
9415 enum machine_mode fltmode
= TYPE_MODE (TREE_TYPE (gimple_assign_lhs (stmt
)));
9416 enum machine_mode mode
;
9420 /* We can only handle constant ranges. */
9421 if (vr
->type
!= VR_RANGE
9422 || TREE_CODE (vr
->min
) != INTEGER_CST
9423 || TREE_CODE (vr
->max
) != INTEGER_CST
)
9426 /* First check if we can use a signed type in place of an unsigned. */
9427 if (TYPE_UNSIGNED (TREE_TYPE (rhs1
))
9428 && (can_float_p (fltmode
, TYPE_MODE (TREE_TYPE (rhs1
)), 0)
9429 != CODE_FOR_nothing
)
9430 && range_fits_type_p (vr
, TYPE_PRECISION (TREE_TYPE (rhs1
)), SIGNED
))
9431 mode
= TYPE_MODE (TREE_TYPE (rhs1
));
9432 /* If we can do the conversion in the current input mode do nothing. */
9433 else if (can_float_p (fltmode
, TYPE_MODE (TREE_TYPE (rhs1
)),
9434 TYPE_UNSIGNED (TREE_TYPE (rhs1
))) != CODE_FOR_nothing
)
9436 /* Otherwise search for a mode we can use, starting from the narrowest
9437 integer mode available. */
9440 mode
= GET_CLASS_NARROWEST_MODE (MODE_INT
);
9443 /* If we cannot do a signed conversion to float from mode
9444 or if the value-range does not fit in the signed type
9445 try with a wider mode. */
9446 if (can_float_p (fltmode
, mode
, 0) != CODE_FOR_nothing
9447 && range_fits_type_p (vr
, GET_MODE_PRECISION (mode
), SIGNED
))
9450 mode
= GET_MODE_WIDER_MODE (mode
);
9451 /* But do not widen the input. Instead leave that to the
9452 optabs expansion code. */
9453 if (GET_MODE_PRECISION (mode
) > TYPE_PRECISION (TREE_TYPE (rhs1
)))
9456 while (mode
!= VOIDmode
);
9457 if (mode
== VOIDmode
)
9461 /* It works, insert a truncation or sign-change before the
9462 float conversion. */
9463 tem
= make_ssa_name (build_nonstandard_integer_type
9464 (GET_MODE_PRECISION (mode
), 0), NULL
);
9465 conv
= gimple_build_assign_with_ops (NOP_EXPR
, tem
, rhs1
, NULL_TREE
);
9466 gsi_insert_before (gsi
, conv
, GSI_SAME_STMT
);
9467 gimple_assign_set_rhs1 (stmt
, tem
);
9473 /* Simplify an internal fn call using ranges if possible. */
9476 simplify_internal_call_using_ranges (gimple_stmt_iterator
*gsi
, gimple stmt
)
9478 enum tree_code subcode
;
9479 switch (gimple_call_internal_fn (stmt
))
9481 case IFN_UBSAN_CHECK_ADD
:
9482 subcode
= PLUS_EXPR
;
9484 case IFN_UBSAN_CHECK_SUB
:
9485 subcode
= MINUS_EXPR
;
9487 case IFN_UBSAN_CHECK_MUL
:
9488 subcode
= MULT_EXPR
;
9494 value_range_t vr0
= VR_INITIALIZER
;
9495 value_range_t vr1
= VR_INITIALIZER
;
9496 tree op0
= gimple_call_arg (stmt
, 0);
9497 tree op1
= gimple_call_arg (stmt
, 1);
9499 if (TREE_CODE (op0
) == SSA_NAME
)
9500 vr0
= *get_value_range (op0
);
9501 else if (TREE_CODE (op0
) == INTEGER_CST
)
9502 set_value_range_to_value (&vr0
, op0
, NULL
);
9504 set_value_range_to_varying (&vr0
);
9506 if (TREE_CODE (op1
) == SSA_NAME
)
9507 vr1
= *get_value_range (op1
);
9508 else if (TREE_CODE (op1
) == INTEGER_CST
)
9509 set_value_range_to_value (&vr1
, op1
, NULL
);
9511 set_value_range_to_varying (&vr1
);
9513 if (!range_int_cst_p (&vr0
))
9515 /* If one range is VR_ANTI_RANGE, VR_VARYING etc.,
9516 optimize at least x = y + 0; x = y - 0; x = y * 0;
9517 and x = y * 1; which never overflow. */
9518 if (!range_int_cst_p (&vr1
))
9520 if (tree_int_cst_sgn (vr1
.min
) == -1)
9522 if (compare_tree_int (vr1
.max
, subcode
== MULT_EXPR
) == 1)
9525 else if (!range_int_cst_p (&vr1
))
9527 /* If one range is VR_ANTI_RANGE, VR_VARYING etc.,
9528 optimize at least x = 0 + y; x = 0 * y; and x = 1 * y;
9529 which never overflow. */
9530 if (subcode
== MINUS_EXPR
)
9532 if (!range_int_cst_p (&vr0
))
9534 if (tree_int_cst_sgn (vr0
.min
) == -1)
9536 if (compare_tree_int (vr0
.max
, subcode
== MULT_EXPR
) == 1)
9541 tree r1
= int_const_binop (subcode
, vr0
.min
, vr1
.min
);
9542 tree r2
= int_const_binop (subcode
, vr0
.max
, vr1
.max
);
9543 if (r1
== NULL_TREE
|| TREE_OVERFLOW (r1
)
9544 || r2
== NULL_TREE
|| TREE_OVERFLOW (r2
))
9546 if (subcode
== MULT_EXPR
)
9548 tree r3
= int_const_binop (subcode
, vr0
.min
, vr1
.max
);
9549 tree r4
= int_const_binop (subcode
, vr0
.max
, vr1
.min
);
9550 if (r3
== NULL_TREE
|| TREE_OVERFLOW (r3
)
9551 || r4
== NULL_TREE
|| TREE_OVERFLOW (r4
))
9556 gimple g
= gimple_build_assign_with_ops (subcode
, gimple_call_lhs (stmt
),
9558 gsi_replace (gsi
, g
, false);
9562 /* Simplify STMT using ranges if possible. */
9565 simplify_stmt_using_ranges (gimple_stmt_iterator
*gsi
)
9567 gimple stmt
= gsi_stmt (*gsi
);
9568 if (is_gimple_assign (stmt
))
9570 enum tree_code rhs_code
= gimple_assign_rhs_code (stmt
);
9571 tree rhs1
= gimple_assign_rhs1 (stmt
);
9577 /* Transform EQ_EXPR, NE_EXPR into BIT_XOR_EXPR or identity
9578 if the RHS is zero or one, and the LHS are known to be boolean
9580 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
9581 return simplify_truth_ops_using_ranges (gsi
, stmt
);
9584 /* Transform TRUNC_DIV_EXPR and TRUNC_MOD_EXPR into RSHIFT_EXPR
9585 and BIT_AND_EXPR respectively if the first operand is greater
9586 than zero and the second operand is an exact power of two. */
9587 case TRUNC_DIV_EXPR
:
9588 case TRUNC_MOD_EXPR
:
9589 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1
))
9590 && integer_pow2p (gimple_assign_rhs2 (stmt
)))
9591 return simplify_div_or_mod_using_ranges (stmt
);
9594 /* Transform ABS (X) into X or -X as appropriate. */
9596 if (TREE_CODE (rhs1
) == SSA_NAME
9597 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
9598 return simplify_abs_using_ranges (stmt
);
9603 /* Optimize away BIT_AND_EXPR and BIT_IOR_EXPR
9604 if all the bits being cleared are already cleared or
9605 all the bits being set are already set. */
9606 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
9607 return simplify_bit_ops_using_ranges (gsi
, stmt
);
9611 if (TREE_CODE (rhs1
) == SSA_NAME
9612 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
9613 return simplify_conversion_using_ranges (stmt
);
9617 if (TREE_CODE (rhs1
) == SSA_NAME
9618 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
9619 return simplify_float_conversion_using_ranges (gsi
, stmt
);
9626 else if (gimple_code (stmt
) == GIMPLE_COND
)
9627 return simplify_cond_using_ranges (stmt
);
9628 else if (gimple_code (stmt
) == GIMPLE_SWITCH
)
9629 return simplify_switch_using_ranges (stmt
);
9630 else if (is_gimple_call (stmt
)
9631 && gimple_call_internal_p (stmt
))
9632 return simplify_internal_call_using_ranges (gsi
, stmt
);
9637 /* If the statement pointed by SI has a predicate whose value can be
9638 computed using the value range information computed by VRP, compute
9639 its value and return true. Otherwise, return false. */
9642 fold_predicate_in (gimple_stmt_iterator
*si
)
9644 bool assignment_p
= false;
9646 gimple stmt
= gsi_stmt (*si
);
9648 if (is_gimple_assign (stmt
)
9649 && TREE_CODE_CLASS (gimple_assign_rhs_code (stmt
)) == tcc_comparison
)
9651 assignment_p
= true;
9652 val
= vrp_evaluate_conditional (gimple_assign_rhs_code (stmt
),
9653 gimple_assign_rhs1 (stmt
),
9654 gimple_assign_rhs2 (stmt
),
9657 else if (gimple_code (stmt
) == GIMPLE_COND
)
9658 val
= vrp_evaluate_conditional (gimple_cond_code (stmt
),
9659 gimple_cond_lhs (stmt
),
9660 gimple_cond_rhs (stmt
),
9668 val
= fold_convert (gimple_expr_type (stmt
), val
);
9672 fprintf (dump_file
, "Folding predicate ");
9673 print_gimple_expr (dump_file
, stmt
, 0, 0);
9674 fprintf (dump_file
, " to ");
9675 print_generic_expr (dump_file
, val
, 0);
9676 fprintf (dump_file
, "\n");
9679 if (is_gimple_assign (stmt
))
9680 gimple_assign_set_rhs_from_tree (si
, val
);
9683 gcc_assert (gimple_code (stmt
) == GIMPLE_COND
);
9684 if (integer_zerop (val
))
9685 gimple_cond_make_false (stmt
);
9686 else if (integer_onep (val
))
9687 gimple_cond_make_true (stmt
);
9698 /* Callback for substitute_and_fold folding the stmt at *SI. */
9701 vrp_fold_stmt (gimple_stmt_iterator
*si
)
9703 if (fold_predicate_in (si
))
9706 return simplify_stmt_using_ranges (si
);
9709 /* Stack of dest,src equivalency pairs that need to be restored after
9710 each attempt to thread a block's incoming edge to an outgoing edge.
9712 A NULL entry is used to mark the end of pairs which need to be
9714 static vec
<tree
> equiv_stack
;
9716 /* A trivial wrapper so that we can present the generic jump threading
9717 code with a simple API for simplifying statements. STMT is the
9718 statement we want to simplify, WITHIN_STMT provides the location
9719 for any overflow warnings. */
9722 simplify_stmt_for_jump_threading (gimple stmt
, gimple within_stmt
)
9724 if (gimple_code (stmt
) == GIMPLE_COND
)
9725 return vrp_evaluate_conditional (gimple_cond_code (stmt
),
9726 gimple_cond_lhs (stmt
),
9727 gimple_cond_rhs (stmt
), within_stmt
);
9729 if (gimple_code (stmt
) == GIMPLE_ASSIGN
)
9731 value_range_t new_vr
= VR_INITIALIZER
;
9732 tree lhs
= gimple_assign_lhs (stmt
);
9734 if (TREE_CODE (lhs
) == SSA_NAME
9735 && (INTEGRAL_TYPE_P (TREE_TYPE (lhs
))
9736 || POINTER_TYPE_P (TREE_TYPE (lhs
))))
9738 extract_range_from_assignment (&new_vr
, stmt
);
9739 if (range_int_cst_singleton_p (&new_vr
))
9747 /* Blocks which have more than one predecessor and more than
9748 one successor present jump threading opportunities, i.e.,
9749 when the block is reached from a specific predecessor, we
9750 may be able to determine which of the outgoing edges will
9751 be traversed. When this optimization applies, we are able
9752 to avoid conditionals at runtime and we may expose secondary
9753 optimization opportunities.
9755 This routine is effectively a driver for the generic jump
9756 threading code. It basically just presents the generic code
9757 with edges that may be suitable for jump threading.
9759 Unlike DOM, we do not iterate VRP if jump threading was successful.
9760 While iterating may expose new opportunities for VRP, it is expected
9761 those opportunities would be very limited and the compile time cost
9762 to expose those opportunities would be significant.
9764 As jump threading opportunities are discovered, they are registered
9765 for later realization. */
9768 identify_jump_threads (void)
9775 /* Ugh. When substituting values earlier in this pass we can
9776 wipe the dominance information. So rebuild the dominator
9777 information as we need it within the jump threading code. */
9778 calculate_dominance_info (CDI_DOMINATORS
);
9780 /* We do not allow VRP information to be used for jump threading
9781 across a back edge in the CFG. Otherwise it becomes too
9782 difficult to avoid eliminating loop exit tests. Of course
9783 EDGE_DFS_BACK is not accurate at this time so we have to
9785 mark_dfs_back_edges ();
9787 /* Do not thread across edges we are about to remove. Just marking
9788 them as EDGE_DFS_BACK will do. */
9789 FOR_EACH_VEC_ELT (to_remove_edges
, i
, e
)
9790 e
->flags
|= EDGE_DFS_BACK
;
9792 /* Allocate our unwinder stack to unwind any temporary equivalences
9793 that might be recorded. */
9794 equiv_stack
.create (20);
9796 /* To avoid lots of silly node creation, we create a single
9797 conditional and just modify it in-place when attempting to
9799 dummy
= gimple_build_cond (EQ_EXPR
,
9800 integer_zero_node
, integer_zero_node
,
9803 /* Walk through all the blocks finding those which present a
9804 potential jump threading opportunity. We could set this up
9805 as a dominator walker and record data during the walk, but
9806 I doubt it's worth the effort for the classes of jump
9807 threading opportunities we are trying to identify at this
9808 point in compilation. */
9809 FOR_EACH_BB_FN (bb
, cfun
)
9813 /* If the generic jump threading code does not find this block
9814 interesting, then there is nothing to do. */
9815 if (! potentially_threadable_block (bb
))
9818 /* We only care about blocks ending in a COND_EXPR. While there
9819 may be some value in handling SWITCH_EXPR here, I doubt it's
9820 terribly important. */
9821 last
= gsi_stmt (gsi_last_bb (bb
));
9823 /* We're basically looking for a switch or any kind of conditional with
9824 integral or pointer type arguments. Note the type of the second
9825 argument will be the same as the first argument, so no need to
9826 check it explicitly. */
9827 if (gimple_code (last
) == GIMPLE_SWITCH
9828 || (gimple_code (last
) == GIMPLE_COND
9829 && TREE_CODE (gimple_cond_lhs (last
)) == SSA_NAME
9830 && (INTEGRAL_TYPE_P (TREE_TYPE (gimple_cond_lhs (last
)))
9831 || POINTER_TYPE_P (TREE_TYPE (gimple_cond_lhs (last
))))
9832 && (TREE_CODE (gimple_cond_rhs (last
)) == SSA_NAME
9833 || is_gimple_min_invariant (gimple_cond_rhs (last
)))))
9837 /* We've got a block with multiple predecessors and multiple
9838 successors which also ends in a suitable conditional or
9839 switch statement. For each predecessor, see if we can thread
9840 it to a specific successor. */
9841 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
9843 /* Do not thread across back edges or abnormal edges
9845 if (e
->flags
& (EDGE_DFS_BACK
| EDGE_COMPLEX
))
9848 thread_across_edge (dummy
, e
, true, &equiv_stack
,
9849 simplify_stmt_for_jump_threading
);
9854 /* We do not actually update the CFG or SSA graphs at this point as
9855 ASSERT_EXPRs are still in the IL and cfg cleanup code does not yet
9856 handle ASSERT_EXPRs gracefully. */
9859 /* We identified all the jump threading opportunities earlier, but could
9860 not transform the CFG at that time. This routine transforms the
9861 CFG and arranges for the dominator tree to be rebuilt if necessary.
9863 Note the SSA graph update will occur during the normal TODO
9864 processing by the pass manager. */
9866 finalize_jump_threads (void)
9868 thread_through_all_blocks (false);
9869 equiv_stack
.release ();
9873 /* Traverse all the blocks folding conditionals with known ranges. */
9880 values_propagated
= true;
9884 fprintf (dump_file
, "\nValue ranges after VRP:\n\n");
9885 dump_all_value_ranges (dump_file
);
9886 fprintf (dump_file
, "\n");
9889 substitute_and_fold (op_with_constant_singleton_value_range
,
9890 vrp_fold_stmt
, false);
9892 if (warn_array_bounds
)
9893 check_all_array_refs ();
9895 /* We must identify jump threading opportunities before we release
9896 the datastructures built by VRP. */
9897 identify_jump_threads ();
9899 /* Set value range to non pointer SSA_NAMEs. */
9900 for (i
= 0; i
< num_vr_values
; i
++)
9903 tree name
= ssa_name (i
);
9906 || POINTER_TYPE_P (TREE_TYPE (name
))
9907 || (vr_value
[i
]->type
== VR_VARYING
)
9908 || (vr_value
[i
]->type
== VR_UNDEFINED
))
9911 if ((TREE_CODE (vr_value
[i
]->min
) == INTEGER_CST
)
9912 && (TREE_CODE (vr_value
[i
]->max
) == INTEGER_CST
)
9913 && (vr_value
[i
]->type
== VR_RANGE
9914 || vr_value
[i
]->type
== VR_ANTI_RANGE
))
9915 set_range_info (name
, vr_value
[i
]->type
, vr_value
[i
]->min
,
9919 /* Free allocated memory. */
9920 for (i
= 0; i
< num_vr_values
; i
++)
9923 BITMAP_FREE (vr_value
[i
]->equiv
);
9928 free (vr_phi_edge_counts
);
9930 /* So that we can distinguish between VRP data being available
9931 and not available. */
9933 vr_phi_edge_counts
= NULL
;
9937 /* Main entry point to VRP (Value Range Propagation). This pass is
9938 loosely based on J. R. C. Patterson, ``Accurate Static Branch
9939 Prediction by Value Range Propagation,'' in SIGPLAN Conference on
9940 Programming Language Design and Implementation, pp. 67-78, 1995.
9941 Also available at http://citeseer.ist.psu.edu/patterson95accurate.html
9943 This is essentially an SSA-CCP pass modified to deal with ranges
9944 instead of constants.
9946 While propagating ranges, we may find that two or more SSA name
9947 have equivalent, though distinct ranges. For instance,
9950 2 p_4 = ASSERT_EXPR <p_3, p_3 != 0>
9952 4 p_5 = ASSERT_EXPR <p_4, p_4 == q_2>;
9956 In the code above, pointer p_5 has range [q_2, q_2], but from the
9957 code we can also determine that p_5 cannot be NULL and, if q_2 had
9958 a non-varying range, p_5's range should also be compatible with it.
9960 These equivalences are created by two expressions: ASSERT_EXPR and
9961 copy operations. Since p_5 is an assertion on p_4, and p_4 was the
9962 result of another assertion, then we can use the fact that p_5 and
9963 p_4 are equivalent when evaluating p_5's range.
9965 Together with value ranges, we also propagate these equivalences
9966 between names so that we can take advantage of information from
9967 multiple ranges when doing final replacement. Note that this
9968 equivalency relation is transitive but not symmetric.
9970 In the example above, p_5 is equivalent to p_4, q_2 and p_3, but we
9971 cannot assert that q_2 is equivalent to p_5 because q_2 may be used
9972 in contexts where that assertion does not hold (e.g., in line 6).
9974 TODO, the main difference between this pass and Patterson's is that
9975 we do not propagate edge probabilities. We only compute whether
9976 edges can be taken or not. That is, instead of having a spectrum
9977 of jump probabilities between 0 and 1, we only deal with 0, 1 and
9978 DON'T KNOW. In the future, it may be worthwhile to propagate
9979 probabilities to aid branch prediction. */
9988 loop_optimizer_init (LOOPS_NORMAL
| LOOPS_HAVE_RECORDED_EXITS
);
9989 rewrite_into_loop_closed_ssa (NULL
, TODO_update_ssa
);
9992 /* ??? This ends up using stale EDGE_DFS_BACK for liveness computation.
9993 Inserting assertions may split edges which will invalidate
9995 insert_range_assertions ();
9997 to_remove_edges
.create (10);
9998 to_update_switch_stmts
.create (5);
9999 threadedge_initialize_values ();
10001 /* For visiting PHI nodes we need EDGE_DFS_BACK computed. */
10002 mark_dfs_back_edges ();
10005 ssa_propagate (vrp_visit_stmt
, vrp_visit_phi_node
);
10008 free_numbers_of_iterations_estimates ();
10010 /* ASSERT_EXPRs must be removed before finalizing jump threads
10011 as finalizing jump threads calls the CFG cleanup code which
10012 does not properly handle ASSERT_EXPRs. */
10013 remove_range_assertions ();
10015 /* If we exposed any new variables, go ahead and put them into
10016 SSA form now, before we handle jump threading. This simplifies
10017 interactions between rewriting of _DECL nodes into SSA form
10018 and rewriting SSA_NAME nodes into SSA form after block
10019 duplication and CFG manipulation. */
10020 update_ssa (TODO_update_ssa
);
10022 finalize_jump_threads ();
10024 /* Remove dead edges from SWITCH_EXPR optimization. This leaves the
10025 CFG in a broken state and requires a cfg_cleanup run. */
10026 FOR_EACH_VEC_ELT (to_remove_edges
, i
, e
)
10028 /* Update SWITCH_EXPR case label vector. */
10029 FOR_EACH_VEC_ELT (to_update_switch_stmts
, i
, su
)
10032 size_t n
= TREE_VEC_LENGTH (su
->vec
);
10034 gimple_switch_set_num_labels (su
->stmt
, n
);
10035 for (j
= 0; j
< n
; j
++)
10036 gimple_switch_set_label (su
->stmt
, j
, TREE_VEC_ELT (su
->vec
, j
));
10037 /* As we may have replaced the default label with a regular one
10038 make sure to make it a real default label again. This ensures
10039 optimal expansion. */
10040 label
= gimple_switch_label (su
->stmt
, 0);
10041 CASE_LOW (label
) = NULL_TREE
;
10042 CASE_HIGH (label
) = NULL_TREE
;
10045 if (to_remove_edges
.length () > 0)
10047 free_dominance_info (CDI_DOMINATORS
);
10048 loops_state_set (LOOPS_NEED_FIXUP
);
10051 to_remove_edges
.release ();
10052 to_update_switch_stmts
.release ();
10053 threadedge_finalize_values ();
10056 loop_optimizer_finalize ();
10062 const pass_data pass_data_vrp
=
10064 GIMPLE_PASS
, /* type */
10066 OPTGROUP_NONE
, /* optinfo_flags */
10067 TV_TREE_VRP
, /* tv_id */
10068 PROP_ssa
, /* properties_required */
10069 0, /* properties_provided */
10070 0, /* properties_destroyed */
10071 0, /* todo_flags_start */
10072 ( TODO_cleanup_cfg
| TODO_update_ssa
), /* todo_flags_finish */
10075 class pass_vrp
: public gimple_opt_pass
10078 pass_vrp (gcc::context
*ctxt
)
10079 : gimple_opt_pass (pass_data_vrp
, ctxt
)
10082 /* opt_pass methods: */
10083 opt_pass
* clone () { return new pass_vrp (m_ctxt
); }
10084 virtual bool gate (function
*) { return flag_tree_vrp
!= 0; }
10085 virtual unsigned int execute (function
*) { return execute_vrp (); }
10087 }; // class pass_vrp
10089 } // anon namespace
10092 make_pass_vrp (gcc::context
*ctxt
)
10094 return new pass_vrp (ctxt
);