Bump date stamp to 20121110
[official-gcc.git] / gcc / tree-vrp.c
blob213c15851a4a710725778ad5fac431de975206d5
1 /* Support routines for Value Range Propagation (VRP).
2 Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
3 Free Software Foundation, Inc.
4 Contributed by Diego Novillo <dnovillo@redhat.com>.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "ggc.h"
27 #include "flags.h"
28 #include "tree.h"
29 #include "basic-block.h"
30 #include "tree-flow.h"
31 #include "tree-pass.h"
32 #include "tree-dump.h"
33 #include "gimple-pretty-print.h"
34 #include "diagnostic-core.h"
35 #include "intl.h"
36 #include "cfgloop.h"
37 #include "tree-scalar-evolution.h"
38 #include "tree-ssa-propagate.h"
39 #include "tree-chrec.h"
40 #include "gimple-fold.h"
41 #include "expr.h"
42 #include "optabs.h"
45 /* Type of value ranges. See value_range_d for a description of these
46 types. */
47 enum value_range_type { VR_UNDEFINED, VR_RANGE, VR_ANTI_RANGE, VR_VARYING };
49 /* Range of values that can be associated with an SSA_NAME after VRP
50 has executed. */
51 struct value_range_d
53 /* Lattice value represented by this range. */
54 enum value_range_type type;
56 /* Minimum and maximum values represented by this range. These
57 values should be interpreted as follows:
59 - If TYPE is VR_UNDEFINED or VR_VARYING then MIN and MAX must
60 be NULL.
62 - If TYPE == VR_RANGE then MIN holds the minimum value and
63 MAX holds the maximum value of the range [MIN, MAX].
65 - If TYPE == ANTI_RANGE the variable is known to NOT
66 take any values in the range [MIN, MAX]. */
67 tree min;
68 tree max;
70 /* Set of SSA names whose value ranges are equivalent to this one.
71 This set is only valid when TYPE is VR_RANGE or VR_ANTI_RANGE. */
72 bitmap equiv;
75 typedef struct value_range_d value_range_t;
77 #define VR_INITIALIZER { VR_UNDEFINED, NULL_TREE, NULL_TREE, NULL }
79 /* Set of SSA names found live during the RPO traversal of the function
80 for still active basic-blocks. */
81 static sbitmap *live;
83 /* Return true if the SSA name NAME is live on the edge E. */
85 static bool
86 live_on_edge (edge e, tree name)
88 return (live[e->dest->index]
89 && bitmap_bit_p (live[e->dest->index], SSA_NAME_VERSION (name)));
92 /* Local functions. */
93 static int compare_values (tree val1, tree val2);
94 static int compare_values_warnv (tree val1, tree val2, bool *);
95 static void vrp_meet (value_range_t *, value_range_t *);
96 static void vrp_intersect_ranges (value_range_t *, value_range_t *);
97 static tree vrp_evaluate_conditional_warnv_with_ops (enum tree_code,
98 tree, tree, bool, bool *,
99 bool *);
101 /* Location information for ASSERT_EXPRs. Each instance of this
102 structure describes an ASSERT_EXPR for an SSA name. Since a single
103 SSA name may have more than one assertion associated with it, these
104 locations are kept in a linked list attached to the corresponding
105 SSA name. */
106 struct assert_locus_d
108 /* Basic block where the assertion would be inserted. */
109 basic_block bb;
111 /* Some assertions need to be inserted on an edge (e.g., assertions
112 generated by COND_EXPRs). In those cases, BB will be NULL. */
113 edge e;
115 /* Pointer to the statement that generated this assertion. */
116 gimple_stmt_iterator si;
118 /* Predicate code for the ASSERT_EXPR. Must be COMPARISON_CLASS_P. */
119 enum tree_code comp_code;
121 /* Value being compared against. */
122 tree val;
124 /* Expression to compare. */
125 tree expr;
127 /* Next node in the linked list. */
128 struct assert_locus_d *next;
131 typedef struct assert_locus_d *assert_locus_t;
133 /* If bit I is present, it means that SSA name N_i has a list of
134 assertions that should be inserted in the IL. */
135 static bitmap need_assert_for;
137 /* Array of locations lists where to insert assertions. ASSERTS_FOR[I]
138 holds a list of ASSERT_LOCUS_T nodes that describe where
139 ASSERT_EXPRs for SSA name N_I should be inserted. */
140 static assert_locus_t *asserts_for;
142 /* Value range array. After propagation, VR_VALUE[I] holds the range
143 of values that SSA name N_I may take. */
144 static unsigned num_vr_values;
145 static value_range_t **vr_value;
146 static bool values_propagated;
148 /* For a PHI node which sets SSA name N_I, VR_COUNTS[I] holds the
149 number of executable edges we saw the last time we visited the
150 node. */
151 static int *vr_phi_edge_counts;
153 typedef struct {
154 gimple stmt;
155 tree vec;
156 } switch_update;
158 static VEC (edge, heap) *to_remove_edges;
159 DEF_VEC_O(switch_update);
160 DEF_VEC_ALLOC_O(switch_update, heap);
161 static VEC (switch_update, heap) *to_update_switch_stmts;
164 /* Return the maximum value for TYPE. */
166 static inline tree
167 vrp_val_max (const_tree type)
169 if (!INTEGRAL_TYPE_P (type))
170 return NULL_TREE;
172 return TYPE_MAX_VALUE (type);
175 /* Return the minimum value for TYPE. */
177 static inline tree
178 vrp_val_min (const_tree type)
180 if (!INTEGRAL_TYPE_P (type))
181 return NULL_TREE;
183 return TYPE_MIN_VALUE (type);
186 /* Return whether VAL is equal to the maximum value of its type. This
187 will be true for a positive overflow infinity. We can't do a
188 simple equality comparison with TYPE_MAX_VALUE because C typedefs
189 and Ada subtypes can produce types whose TYPE_MAX_VALUE is not ==
190 to the integer constant with the same value in the type. */
192 static inline bool
193 vrp_val_is_max (const_tree val)
195 tree type_max = vrp_val_max (TREE_TYPE (val));
196 return (val == type_max
197 || (type_max != NULL_TREE
198 && operand_equal_p (val, type_max, 0)));
201 /* Return whether VAL is equal to the minimum value of its type. This
202 will be true for a negative overflow infinity. */
204 static inline bool
205 vrp_val_is_min (const_tree val)
207 tree type_min = vrp_val_min (TREE_TYPE (val));
208 return (val == type_min
209 || (type_min != NULL_TREE
210 && operand_equal_p (val, type_min, 0)));
214 /* Return whether TYPE should use an overflow infinity distinct from
215 TYPE_{MIN,MAX}_VALUE. We use an overflow infinity value to
216 represent a signed overflow during VRP computations. An infinity
217 is distinct from a half-range, which will go from some number to
218 TYPE_{MIN,MAX}_VALUE. */
220 static inline bool
221 needs_overflow_infinity (const_tree type)
223 return INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_WRAPS (type);
226 /* Return whether TYPE can support our overflow infinity
227 representation: we use the TREE_OVERFLOW flag, which only exists
228 for constants. If TYPE doesn't support this, we don't optimize
229 cases which would require signed overflow--we drop them to
230 VARYING. */
232 static inline bool
233 supports_overflow_infinity (const_tree type)
235 tree min = vrp_val_min (type), max = vrp_val_max (type);
236 #ifdef ENABLE_CHECKING
237 gcc_assert (needs_overflow_infinity (type));
238 #endif
239 return (min != NULL_TREE
240 && CONSTANT_CLASS_P (min)
241 && max != NULL_TREE
242 && CONSTANT_CLASS_P (max));
245 /* VAL is the maximum or minimum value of a type. Return a
246 corresponding overflow infinity. */
248 static inline tree
249 make_overflow_infinity (tree val)
251 gcc_checking_assert (val != NULL_TREE && CONSTANT_CLASS_P (val));
252 val = copy_node (val);
253 TREE_OVERFLOW (val) = 1;
254 return val;
257 /* Return a negative overflow infinity for TYPE. */
259 static inline tree
260 negative_overflow_infinity (tree type)
262 gcc_checking_assert (supports_overflow_infinity (type));
263 return make_overflow_infinity (vrp_val_min (type));
266 /* Return a positive overflow infinity for TYPE. */
268 static inline tree
269 positive_overflow_infinity (tree type)
271 gcc_checking_assert (supports_overflow_infinity (type));
272 return make_overflow_infinity (vrp_val_max (type));
275 /* Return whether VAL is a negative overflow infinity. */
277 static inline bool
278 is_negative_overflow_infinity (const_tree val)
280 return (needs_overflow_infinity (TREE_TYPE (val))
281 && CONSTANT_CLASS_P (val)
282 && TREE_OVERFLOW (val)
283 && vrp_val_is_min (val));
286 /* Return whether VAL is a positive overflow infinity. */
288 static inline bool
289 is_positive_overflow_infinity (const_tree val)
291 return (needs_overflow_infinity (TREE_TYPE (val))
292 && CONSTANT_CLASS_P (val)
293 && TREE_OVERFLOW (val)
294 && vrp_val_is_max (val));
297 /* Return whether VAL is a positive or negative overflow infinity. */
299 static inline bool
300 is_overflow_infinity (const_tree val)
302 return (needs_overflow_infinity (TREE_TYPE (val))
303 && CONSTANT_CLASS_P (val)
304 && TREE_OVERFLOW (val)
305 && (vrp_val_is_min (val) || vrp_val_is_max (val)));
308 /* Return whether STMT has a constant rhs that is_overflow_infinity. */
310 static inline bool
311 stmt_overflow_infinity (gimple stmt)
313 if (is_gimple_assign (stmt)
314 && get_gimple_rhs_class (gimple_assign_rhs_code (stmt)) ==
315 GIMPLE_SINGLE_RHS)
316 return is_overflow_infinity (gimple_assign_rhs1 (stmt));
317 return false;
320 /* If VAL is now an overflow infinity, return VAL. Otherwise, return
321 the same value with TREE_OVERFLOW clear. This can be used to avoid
322 confusing a regular value with an overflow value. */
324 static inline tree
325 avoid_overflow_infinity (tree val)
327 if (!is_overflow_infinity (val))
328 return val;
330 if (vrp_val_is_max (val))
331 return vrp_val_max (TREE_TYPE (val));
332 else
334 gcc_checking_assert (vrp_val_is_min (val));
335 return vrp_val_min (TREE_TYPE (val));
340 /* Return true if ARG is marked with the nonnull attribute in the
341 current function signature. */
343 static bool
344 nonnull_arg_p (const_tree arg)
346 tree t, attrs, fntype;
347 unsigned HOST_WIDE_INT arg_num;
349 gcc_assert (TREE_CODE (arg) == PARM_DECL && POINTER_TYPE_P (TREE_TYPE (arg)));
351 /* The static chain decl is always non null. */
352 if (arg == cfun->static_chain_decl)
353 return true;
355 fntype = TREE_TYPE (current_function_decl);
356 for (attrs = TYPE_ATTRIBUTES (fntype); attrs; attrs = TREE_CHAIN (attrs))
358 attrs = lookup_attribute ("nonnull", attrs);
360 /* If "nonnull" wasn't specified, we know nothing about the argument. */
361 if (attrs == NULL_TREE)
362 return false;
364 /* If "nonnull" applies to all the arguments, then ARG is non-null. */
365 if (TREE_VALUE (attrs) == NULL_TREE)
366 return true;
368 /* Get the position number for ARG in the function signature. */
369 for (arg_num = 1, t = DECL_ARGUMENTS (current_function_decl);
371 t = DECL_CHAIN (t), arg_num++)
373 if (t == arg)
374 break;
377 gcc_assert (t == arg);
379 /* Now see if ARG_NUM is mentioned in the nonnull list. */
380 for (t = TREE_VALUE (attrs); t; t = TREE_CHAIN (t))
382 if (compare_tree_int (TREE_VALUE (t), arg_num) == 0)
383 return true;
387 return false;
391 /* Set value range VR to VR_UNDEFINED. */
393 static inline void
394 set_value_range_to_undefined (value_range_t *vr)
396 vr->type = VR_UNDEFINED;
397 vr->min = vr->max = NULL_TREE;
398 if (vr->equiv)
399 bitmap_clear (vr->equiv);
403 /* Set value range VR to VR_VARYING. */
405 static inline void
406 set_value_range_to_varying (value_range_t *vr)
408 vr->type = VR_VARYING;
409 vr->min = vr->max = NULL_TREE;
410 if (vr->equiv)
411 bitmap_clear (vr->equiv);
415 /* Set value range VR to {T, MIN, MAX, EQUIV}. */
417 static void
418 set_value_range (value_range_t *vr, enum value_range_type t, tree min,
419 tree max, bitmap equiv)
421 #if defined ENABLE_CHECKING
422 /* Check the validity of the range. */
423 if (t == VR_RANGE || t == VR_ANTI_RANGE)
425 int cmp;
427 gcc_assert (min && max);
429 if (INTEGRAL_TYPE_P (TREE_TYPE (min)) && t == VR_ANTI_RANGE)
430 gcc_assert (!vrp_val_is_min (min) || !vrp_val_is_max (max));
432 cmp = compare_values (min, max);
433 gcc_assert (cmp == 0 || cmp == -1 || cmp == -2);
435 if (needs_overflow_infinity (TREE_TYPE (min)))
436 gcc_assert (!is_overflow_infinity (min)
437 || !is_overflow_infinity (max));
440 if (t == VR_UNDEFINED || t == VR_VARYING)
441 gcc_assert (min == NULL_TREE && max == NULL_TREE);
443 if (t == VR_UNDEFINED || t == VR_VARYING)
444 gcc_assert (equiv == NULL || bitmap_empty_p (equiv));
445 #endif
447 vr->type = t;
448 vr->min = min;
449 vr->max = max;
451 /* Since updating the equivalence set involves deep copying the
452 bitmaps, only do it if absolutely necessary. */
453 if (vr->equiv == NULL
454 && equiv != NULL)
455 vr->equiv = BITMAP_ALLOC (NULL);
457 if (equiv != vr->equiv)
459 if (equiv && !bitmap_empty_p (equiv))
460 bitmap_copy (vr->equiv, equiv);
461 else
462 bitmap_clear (vr->equiv);
467 /* Set value range VR to the canonical form of {T, MIN, MAX, EQUIV}.
468 This means adjusting T, MIN and MAX representing the case of a
469 wrapping range with MAX < MIN covering [MIN, type_max] U [type_min, MAX]
470 as anti-rage ~[MAX+1, MIN-1]. Likewise for wrapping anti-ranges.
471 In corner cases where MAX+1 or MIN-1 wraps this will fall back
472 to varying.
473 This routine exists to ease canonicalization in the case where we
474 extract ranges from var + CST op limit. */
476 static void
477 set_and_canonicalize_value_range (value_range_t *vr, enum value_range_type t,
478 tree min, tree max, bitmap equiv)
480 /* Use the canonical setters for VR_UNDEFINED and VR_VARYING. */
481 if (t == VR_UNDEFINED)
483 set_value_range_to_undefined (vr);
484 return;
486 else if (t == VR_VARYING)
488 set_value_range_to_varying (vr);
489 return;
492 /* Nothing to canonicalize for symbolic ranges. */
493 if (TREE_CODE (min) != INTEGER_CST
494 || TREE_CODE (max) != INTEGER_CST)
496 set_value_range (vr, t, min, max, equiv);
497 return;
500 /* Wrong order for min and max, to swap them and the VR type we need
501 to adjust them. */
502 if (tree_int_cst_lt (max, min))
504 tree one, tmp;
506 /* For one bit precision if max < min, then the swapped
507 range covers all values, so for VR_RANGE it is varying and
508 for VR_ANTI_RANGE empty range, so drop to varying as well. */
509 if (TYPE_PRECISION (TREE_TYPE (min)) == 1)
511 set_value_range_to_varying (vr);
512 return;
515 one = build_int_cst (TREE_TYPE (min), 1);
516 tmp = int_const_binop (PLUS_EXPR, max, one);
517 max = int_const_binop (MINUS_EXPR, min, one);
518 min = tmp;
520 /* There's one corner case, if we had [C+1, C] before we now have
521 that again. But this represents an empty value range, so drop
522 to varying in this case. */
523 if (tree_int_cst_lt (max, min))
525 set_value_range_to_varying (vr);
526 return;
529 t = t == VR_RANGE ? VR_ANTI_RANGE : VR_RANGE;
532 /* Anti-ranges that can be represented as ranges should be so. */
533 if (t == VR_ANTI_RANGE)
535 bool is_min = vrp_val_is_min (min);
536 bool is_max = vrp_val_is_max (max);
538 if (is_min && is_max)
540 /* We cannot deal with empty ranges, drop to varying.
541 ??? This could be VR_UNDEFINED instead. */
542 set_value_range_to_varying (vr);
543 return;
545 else if (TYPE_PRECISION (TREE_TYPE (min)) == 1
546 && !TYPE_UNSIGNED (TREE_TYPE (min))
547 && (is_min || is_max))
549 /* For signed 1-bit precision, one is not in-range and
550 thus adding/subtracting it would result in overflows. */
551 if (operand_equal_p (min, max, 0))
553 min = max = is_min ? vrp_val_max (TREE_TYPE (min))
554 : vrp_val_min (TREE_TYPE (min));
555 t = VR_RANGE;
557 else
559 set_value_range_to_varying (vr);
560 return;
563 else if (is_min
564 /* As a special exception preserve non-null ranges. */
565 && !(TYPE_UNSIGNED (TREE_TYPE (min))
566 && integer_zerop (max)))
568 tree one = build_int_cst (TREE_TYPE (max), 1);
569 min = int_const_binop (PLUS_EXPR, max, one);
570 max = vrp_val_max (TREE_TYPE (max));
571 t = VR_RANGE;
573 else if (is_max)
575 tree one = build_int_cst (TREE_TYPE (min), 1);
576 max = int_const_binop (MINUS_EXPR, min, one);
577 min = vrp_val_min (TREE_TYPE (min));
578 t = VR_RANGE;
582 /* Drop [-INF(OVF), +INF(OVF)] to varying. */
583 if (needs_overflow_infinity (TREE_TYPE (min))
584 && is_overflow_infinity (min)
585 && is_overflow_infinity (max))
587 set_value_range_to_varying (vr);
588 return;
591 set_value_range (vr, t, min, max, equiv);
594 /* Copy value range FROM into value range TO. */
596 static inline void
597 copy_value_range (value_range_t *to, value_range_t *from)
599 set_value_range (to, from->type, from->min, from->max, from->equiv);
602 /* Set value range VR to a single value. This function is only called
603 with values we get from statements, and exists to clear the
604 TREE_OVERFLOW flag so that we don't think we have an overflow
605 infinity when we shouldn't. */
607 static inline void
608 set_value_range_to_value (value_range_t *vr, tree val, bitmap equiv)
610 gcc_assert (is_gimple_min_invariant (val));
611 val = avoid_overflow_infinity (val);
612 set_value_range (vr, VR_RANGE, val, val, equiv);
615 /* Set value range VR to a non-negative range of type TYPE.
616 OVERFLOW_INFINITY indicates whether to use an overflow infinity
617 rather than TYPE_MAX_VALUE; this should be true if we determine
618 that the range is nonnegative based on the assumption that signed
619 overflow does not occur. */
621 static inline void
622 set_value_range_to_nonnegative (value_range_t *vr, tree type,
623 bool overflow_infinity)
625 tree zero;
627 if (overflow_infinity && !supports_overflow_infinity (type))
629 set_value_range_to_varying (vr);
630 return;
633 zero = build_int_cst (type, 0);
634 set_value_range (vr, VR_RANGE, zero,
635 (overflow_infinity
636 ? positive_overflow_infinity (type)
637 : TYPE_MAX_VALUE (type)),
638 vr->equiv);
641 /* Set value range VR to a non-NULL range of type TYPE. */
643 static inline void
644 set_value_range_to_nonnull (value_range_t *vr, tree type)
646 tree zero = build_int_cst (type, 0);
647 set_value_range (vr, VR_ANTI_RANGE, zero, zero, vr->equiv);
651 /* Set value range VR to a NULL range of type TYPE. */
653 static inline void
654 set_value_range_to_null (value_range_t *vr, tree type)
656 set_value_range_to_value (vr, build_int_cst (type, 0), vr->equiv);
660 /* Set value range VR to a range of a truthvalue of type TYPE. */
662 static inline void
663 set_value_range_to_truthvalue (value_range_t *vr, tree type)
665 if (TYPE_PRECISION (type) == 1)
666 set_value_range_to_varying (vr);
667 else
668 set_value_range (vr, VR_RANGE,
669 build_int_cst (type, 0), build_int_cst (type, 1),
670 vr->equiv);
674 /* If abs (min) < abs (max), set VR to [-max, max], if
675 abs (min) >= abs (max), set VR to [-min, min]. */
677 static void
678 abs_extent_range (value_range_t *vr, tree min, tree max)
680 int cmp;
682 gcc_assert (TREE_CODE (min) == INTEGER_CST);
683 gcc_assert (TREE_CODE (max) == INTEGER_CST);
684 gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (min)));
685 gcc_assert (!TYPE_UNSIGNED (TREE_TYPE (min)));
686 min = fold_unary (ABS_EXPR, TREE_TYPE (min), min);
687 max = fold_unary (ABS_EXPR, TREE_TYPE (max), max);
688 if (TREE_OVERFLOW (min) || TREE_OVERFLOW (max))
690 set_value_range_to_varying (vr);
691 return;
693 cmp = compare_values (min, max);
694 if (cmp == -1)
695 min = fold_unary (NEGATE_EXPR, TREE_TYPE (min), max);
696 else if (cmp == 0 || cmp == 1)
698 max = min;
699 min = fold_unary (NEGATE_EXPR, TREE_TYPE (min), min);
701 else
703 set_value_range_to_varying (vr);
704 return;
706 set_and_canonicalize_value_range (vr, VR_RANGE, min, max, NULL);
710 /* Return value range information for VAR.
712 If we have no values ranges recorded (ie, VRP is not running), then
713 return NULL. Otherwise create an empty range if none existed for VAR. */
715 static value_range_t *
716 get_value_range (const_tree var)
718 static const struct value_range_d vr_const_varying
719 = { VR_VARYING, NULL_TREE, NULL_TREE, NULL };
720 value_range_t *vr;
721 tree sym;
722 unsigned ver = SSA_NAME_VERSION (var);
724 /* If we have no recorded ranges, then return NULL. */
725 if (! vr_value)
726 return NULL;
728 /* If we query the range for a new SSA name return an unmodifiable VARYING.
729 We should get here at most from the substitute-and-fold stage which
730 will never try to change values. */
731 if (ver >= num_vr_values)
732 return CONST_CAST (value_range_t *, &vr_const_varying);
734 vr = vr_value[ver];
735 if (vr)
736 return vr;
738 /* After propagation finished do not allocate new value-ranges. */
739 if (values_propagated)
740 return CONST_CAST (value_range_t *, &vr_const_varying);
742 /* Create a default value range. */
743 vr_value[ver] = vr = XCNEW (value_range_t);
745 /* Defer allocating the equivalence set. */
746 vr->equiv = NULL;
748 /* If VAR is a default definition of a parameter, the variable can
749 take any value in VAR's type. */
750 if (SSA_NAME_IS_DEFAULT_DEF (var))
752 sym = SSA_NAME_VAR (var);
753 if (TREE_CODE (sym) == PARM_DECL)
755 /* Try to use the "nonnull" attribute to create ~[0, 0]
756 anti-ranges for pointers. Note that this is only valid with
757 default definitions of PARM_DECLs. */
758 if (POINTER_TYPE_P (TREE_TYPE (sym))
759 && nonnull_arg_p (sym))
760 set_value_range_to_nonnull (vr, TREE_TYPE (sym));
761 else
762 set_value_range_to_varying (vr);
764 else if (TREE_CODE (sym) == RESULT_DECL
765 && DECL_BY_REFERENCE (sym))
766 set_value_range_to_nonnull (vr, TREE_TYPE (sym));
769 return vr;
772 /* Return true, if VAL1 and VAL2 are equal values for VRP purposes. */
774 static inline bool
775 vrp_operand_equal_p (const_tree val1, const_tree val2)
777 if (val1 == val2)
778 return true;
779 if (!val1 || !val2 || !operand_equal_p (val1, val2, 0))
780 return false;
781 if (is_overflow_infinity (val1))
782 return is_overflow_infinity (val2);
783 return true;
786 /* Return true, if the bitmaps B1 and B2 are equal. */
788 static inline bool
789 vrp_bitmap_equal_p (const_bitmap b1, const_bitmap b2)
791 return (b1 == b2
792 || ((!b1 || bitmap_empty_p (b1))
793 && (!b2 || bitmap_empty_p (b2)))
794 || (b1 && b2
795 && bitmap_equal_p (b1, b2)));
798 /* Update the value range and equivalence set for variable VAR to
799 NEW_VR. Return true if NEW_VR is different from VAR's previous
800 value.
802 NOTE: This function assumes that NEW_VR is a temporary value range
803 object created for the sole purpose of updating VAR's range. The
804 storage used by the equivalence set from NEW_VR will be freed by
805 this function. Do not call update_value_range when NEW_VR
806 is the range object associated with another SSA name. */
808 static inline bool
809 update_value_range (const_tree var, value_range_t *new_vr)
811 value_range_t *old_vr;
812 bool is_new;
814 /* Update the value range, if necessary. */
815 old_vr = get_value_range (var);
816 is_new = old_vr->type != new_vr->type
817 || !vrp_operand_equal_p (old_vr->min, new_vr->min)
818 || !vrp_operand_equal_p (old_vr->max, new_vr->max)
819 || !vrp_bitmap_equal_p (old_vr->equiv, new_vr->equiv);
821 if (is_new)
823 /* Do not allow transitions up the lattice. The following
824 is slightly more awkward than just new_vr->type < old_vr->type
825 because VR_RANGE and VR_ANTI_RANGE need to be considered
826 the same. We may not have is_new when transitioning to
827 UNDEFINED or from VARYING. */
828 if (new_vr->type == VR_UNDEFINED
829 || old_vr->type == VR_VARYING)
830 set_value_range_to_varying (old_vr);
831 else
832 set_value_range (old_vr, new_vr->type, new_vr->min, new_vr->max,
833 new_vr->equiv);
836 BITMAP_FREE (new_vr->equiv);
838 return is_new;
842 /* Add VAR and VAR's equivalence set to EQUIV. This is the central
843 point where equivalence processing can be turned on/off. */
845 static void
846 add_equivalence (bitmap *equiv, const_tree var)
848 unsigned ver = SSA_NAME_VERSION (var);
849 value_range_t *vr = vr_value[ver];
851 if (*equiv == NULL)
852 *equiv = BITMAP_ALLOC (NULL);
853 bitmap_set_bit (*equiv, ver);
854 if (vr && vr->equiv)
855 bitmap_ior_into (*equiv, vr->equiv);
859 /* Return true if VR is ~[0, 0]. */
861 static inline bool
862 range_is_nonnull (value_range_t *vr)
864 return vr->type == VR_ANTI_RANGE
865 && integer_zerop (vr->min)
866 && integer_zerop (vr->max);
870 /* Return true if VR is [0, 0]. */
872 static inline bool
873 range_is_null (value_range_t *vr)
875 return vr->type == VR_RANGE
876 && integer_zerop (vr->min)
877 && integer_zerop (vr->max);
880 /* Return true if max and min of VR are INTEGER_CST. It's not necessary
881 a singleton. */
883 static inline bool
884 range_int_cst_p (value_range_t *vr)
886 return (vr->type == VR_RANGE
887 && TREE_CODE (vr->max) == INTEGER_CST
888 && TREE_CODE (vr->min) == INTEGER_CST);
891 /* Return true if VR is a INTEGER_CST singleton. */
893 static inline bool
894 range_int_cst_singleton_p (value_range_t *vr)
896 return (range_int_cst_p (vr)
897 && !TREE_OVERFLOW (vr->min)
898 && !TREE_OVERFLOW (vr->max)
899 && tree_int_cst_equal (vr->min, vr->max));
902 /* Return true if value range VR involves at least one symbol. */
904 static inline bool
905 symbolic_range_p (value_range_t *vr)
907 return (!is_gimple_min_invariant (vr->min)
908 || !is_gimple_min_invariant (vr->max));
911 /* Return true if value range VR uses an overflow infinity. */
913 static inline bool
914 overflow_infinity_range_p (value_range_t *vr)
916 return (vr->type == VR_RANGE
917 && (is_overflow_infinity (vr->min)
918 || is_overflow_infinity (vr->max)));
921 /* Return false if we can not make a valid comparison based on VR;
922 this will be the case if it uses an overflow infinity and overflow
923 is not undefined (i.e., -fno-strict-overflow is in effect).
924 Otherwise return true, and set *STRICT_OVERFLOW_P to true if VR
925 uses an overflow infinity. */
927 static bool
928 usable_range_p (value_range_t *vr, bool *strict_overflow_p)
930 gcc_assert (vr->type == VR_RANGE);
931 if (is_overflow_infinity (vr->min))
933 *strict_overflow_p = true;
934 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (vr->min)))
935 return false;
937 if (is_overflow_infinity (vr->max))
939 *strict_overflow_p = true;
940 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (vr->max)))
941 return false;
943 return true;
947 /* Return true if the result of assignment STMT is know to be non-negative.
948 If the return value is based on the assumption that signed overflow is
949 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
950 *STRICT_OVERFLOW_P.*/
952 static bool
953 gimple_assign_nonnegative_warnv_p (gimple stmt, bool *strict_overflow_p)
955 enum tree_code code = gimple_assign_rhs_code (stmt);
956 switch (get_gimple_rhs_class (code))
958 case GIMPLE_UNARY_RHS:
959 return tree_unary_nonnegative_warnv_p (gimple_assign_rhs_code (stmt),
960 gimple_expr_type (stmt),
961 gimple_assign_rhs1 (stmt),
962 strict_overflow_p);
963 case GIMPLE_BINARY_RHS:
964 return tree_binary_nonnegative_warnv_p (gimple_assign_rhs_code (stmt),
965 gimple_expr_type (stmt),
966 gimple_assign_rhs1 (stmt),
967 gimple_assign_rhs2 (stmt),
968 strict_overflow_p);
969 case GIMPLE_TERNARY_RHS:
970 return false;
971 case GIMPLE_SINGLE_RHS:
972 return tree_single_nonnegative_warnv_p (gimple_assign_rhs1 (stmt),
973 strict_overflow_p);
974 case GIMPLE_INVALID_RHS:
975 gcc_unreachable ();
976 default:
977 gcc_unreachable ();
981 /* Return true if return value of call STMT is know to be non-negative.
982 If the return value is based on the assumption that signed overflow is
983 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
984 *STRICT_OVERFLOW_P.*/
986 static bool
987 gimple_call_nonnegative_warnv_p (gimple stmt, bool *strict_overflow_p)
989 tree arg0 = gimple_call_num_args (stmt) > 0 ?
990 gimple_call_arg (stmt, 0) : NULL_TREE;
991 tree arg1 = gimple_call_num_args (stmt) > 1 ?
992 gimple_call_arg (stmt, 1) : NULL_TREE;
994 return tree_call_nonnegative_warnv_p (gimple_expr_type (stmt),
995 gimple_call_fndecl (stmt),
996 arg0,
997 arg1,
998 strict_overflow_p);
1001 /* Return true if STMT is know to to compute a non-negative value.
1002 If the return value is based on the assumption that signed overflow is
1003 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1004 *STRICT_OVERFLOW_P.*/
1006 static bool
1007 gimple_stmt_nonnegative_warnv_p (gimple stmt, bool *strict_overflow_p)
1009 switch (gimple_code (stmt))
1011 case GIMPLE_ASSIGN:
1012 return gimple_assign_nonnegative_warnv_p (stmt, strict_overflow_p);
1013 case GIMPLE_CALL:
1014 return gimple_call_nonnegative_warnv_p (stmt, strict_overflow_p);
1015 default:
1016 gcc_unreachable ();
1020 /* Return true if the result of assignment STMT is know to be non-zero.
1021 If the return value is based on the assumption that signed overflow is
1022 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1023 *STRICT_OVERFLOW_P.*/
1025 static bool
1026 gimple_assign_nonzero_warnv_p (gimple stmt, bool *strict_overflow_p)
1028 enum tree_code code = gimple_assign_rhs_code (stmt);
1029 switch (get_gimple_rhs_class (code))
1031 case GIMPLE_UNARY_RHS:
1032 return tree_unary_nonzero_warnv_p (gimple_assign_rhs_code (stmt),
1033 gimple_expr_type (stmt),
1034 gimple_assign_rhs1 (stmt),
1035 strict_overflow_p);
1036 case GIMPLE_BINARY_RHS:
1037 return tree_binary_nonzero_warnv_p (gimple_assign_rhs_code (stmt),
1038 gimple_expr_type (stmt),
1039 gimple_assign_rhs1 (stmt),
1040 gimple_assign_rhs2 (stmt),
1041 strict_overflow_p);
1042 case GIMPLE_TERNARY_RHS:
1043 return false;
1044 case GIMPLE_SINGLE_RHS:
1045 return tree_single_nonzero_warnv_p (gimple_assign_rhs1 (stmt),
1046 strict_overflow_p);
1047 case GIMPLE_INVALID_RHS:
1048 gcc_unreachable ();
1049 default:
1050 gcc_unreachable ();
1054 /* Return true if STMT is know to to compute a non-zero value.
1055 If the return value is based on the assumption that signed overflow is
1056 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1057 *STRICT_OVERFLOW_P.*/
1059 static bool
1060 gimple_stmt_nonzero_warnv_p (gimple stmt, bool *strict_overflow_p)
1062 switch (gimple_code (stmt))
1064 case GIMPLE_ASSIGN:
1065 return gimple_assign_nonzero_warnv_p (stmt, strict_overflow_p);
1066 case GIMPLE_CALL:
1067 return gimple_alloca_call_p (stmt);
1068 default:
1069 gcc_unreachable ();
1073 /* Like tree_expr_nonzero_warnv_p, but this function uses value ranges
1074 obtained so far. */
1076 static bool
1077 vrp_stmt_computes_nonzero (gimple stmt, bool *strict_overflow_p)
1079 if (gimple_stmt_nonzero_warnv_p (stmt, strict_overflow_p))
1080 return true;
1082 /* If we have an expression of the form &X->a, then the expression
1083 is nonnull if X is nonnull. */
1084 if (is_gimple_assign (stmt)
1085 && gimple_assign_rhs_code (stmt) == ADDR_EXPR)
1087 tree expr = gimple_assign_rhs1 (stmt);
1088 tree base = get_base_address (TREE_OPERAND (expr, 0));
1090 if (base != NULL_TREE
1091 && TREE_CODE (base) == MEM_REF
1092 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
1094 value_range_t *vr = get_value_range (TREE_OPERAND (base, 0));
1095 if (range_is_nonnull (vr))
1096 return true;
1100 return false;
1103 /* Returns true if EXPR is a valid value (as expected by compare_values) --
1104 a gimple invariant, or SSA_NAME +- CST. */
1106 static bool
1107 valid_value_p (tree expr)
1109 if (TREE_CODE (expr) == SSA_NAME)
1110 return true;
1112 if (TREE_CODE (expr) == PLUS_EXPR
1113 || TREE_CODE (expr) == MINUS_EXPR)
1114 return (TREE_CODE (TREE_OPERAND (expr, 0)) == SSA_NAME
1115 && TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST);
1117 return is_gimple_min_invariant (expr);
1120 /* Return
1121 1 if VAL < VAL2
1122 0 if !(VAL < VAL2)
1123 -2 if those are incomparable. */
1124 static inline int
1125 operand_less_p (tree val, tree val2)
1127 /* LT is folded faster than GE and others. Inline the common case. */
1128 if (TREE_CODE (val) == INTEGER_CST && TREE_CODE (val2) == INTEGER_CST)
1130 if (TYPE_UNSIGNED (TREE_TYPE (val)))
1131 return INT_CST_LT_UNSIGNED (val, val2);
1132 else
1134 if (INT_CST_LT (val, val2))
1135 return 1;
1138 else
1140 tree tcmp;
1142 fold_defer_overflow_warnings ();
1144 tcmp = fold_binary_to_constant (LT_EXPR, boolean_type_node, val, val2);
1146 fold_undefer_and_ignore_overflow_warnings ();
1148 if (!tcmp
1149 || TREE_CODE (tcmp) != INTEGER_CST)
1150 return -2;
1152 if (!integer_zerop (tcmp))
1153 return 1;
1156 /* val >= val2, not considering overflow infinity. */
1157 if (is_negative_overflow_infinity (val))
1158 return is_negative_overflow_infinity (val2) ? 0 : 1;
1159 else if (is_positive_overflow_infinity (val2))
1160 return is_positive_overflow_infinity (val) ? 0 : 1;
1162 return 0;
1165 /* Compare two values VAL1 and VAL2. Return
1167 -2 if VAL1 and VAL2 cannot be compared at compile-time,
1168 -1 if VAL1 < VAL2,
1169 0 if VAL1 == VAL2,
1170 +1 if VAL1 > VAL2, and
1171 +2 if VAL1 != VAL2
1173 This is similar to tree_int_cst_compare but supports pointer values
1174 and values that cannot be compared at compile time.
1176 If STRICT_OVERFLOW_P is not NULL, then set *STRICT_OVERFLOW_P to
1177 true if the return value is only valid if we assume that signed
1178 overflow is undefined. */
1180 static int
1181 compare_values_warnv (tree val1, tree val2, bool *strict_overflow_p)
1183 if (val1 == val2)
1184 return 0;
1186 /* Below we rely on the fact that VAL1 and VAL2 are both pointers or
1187 both integers. */
1188 gcc_assert (POINTER_TYPE_P (TREE_TYPE (val1))
1189 == POINTER_TYPE_P (TREE_TYPE (val2)));
1190 /* Convert the two values into the same type. This is needed because
1191 sizetype causes sign extension even for unsigned types. */
1192 val2 = fold_convert (TREE_TYPE (val1), val2);
1193 STRIP_USELESS_TYPE_CONVERSION (val2);
1195 if ((TREE_CODE (val1) == SSA_NAME
1196 || TREE_CODE (val1) == PLUS_EXPR
1197 || TREE_CODE (val1) == MINUS_EXPR)
1198 && (TREE_CODE (val2) == SSA_NAME
1199 || TREE_CODE (val2) == PLUS_EXPR
1200 || TREE_CODE (val2) == MINUS_EXPR))
1202 tree n1, c1, n2, c2;
1203 enum tree_code code1, code2;
1205 /* If VAL1 and VAL2 are of the form 'NAME [+-] CST' or 'NAME',
1206 return -1 or +1 accordingly. If VAL1 and VAL2 don't use the
1207 same name, return -2. */
1208 if (TREE_CODE (val1) == SSA_NAME)
1210 code1 = SSA_NAME;
1211 n1 = val1;
1212 c1 = NULL_TREE;
1214 else
1216 code1 = TREE_CODE (val1);
1217 n1 = TREE_OPERAND (val1, 0);
1218 c1 = TREE_OPERAND (val1, 1);
1219 if (tree_int_cst_sgn (c1) == -1)
1221 if (is_negative_overflow_infinity (c1))
1222 return -2;
1223 c1 = fold_unary_to_constant (NEGATE_EXPR, TREE_TYPE (c1), c1);
1224 if (!c1)
1225 return -2;
1226 code1 = code1 == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR;
1230 if (TREE_CODE (val2) == SSA_NAME)
1232 code2 = SSA_NAME;
1233 n2 = val2;
1234 c2 = NULL_TREE;
1236 else
1238 code2 = TREE_CODE (val2);
1239 n2 = TREE_OPERAND (val2, 0);
1240 c2 = TREE_OPERAND (val2, 1);
1241 if (tree_int_cst_sgn (c2) == -1)
1243 if (is_negative_overflow_infinity (c2))
1244 return -2;
1245 c2 = fold_unary_to_constant (NEGATE_EXPR, TREE_TYPE (c2), c2);
1246 if (!c2)
1247 return -2;
1248 code2 = code2 == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR;
1252 /* Both values must use the same name. */
1253 if (n1 != n2)
1254 return -2;
1256 if (code1 == SSA_NAME
1257 && code2 == SSA_NAME)
1258 /* NAME == NAME */
1259 return 0;
1261 /* If overflow is defined we cannot simplify more. */
1262 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (val1)))
1263 return -2;
1265 if (strict_overflow_p != NULL
1266 && (code1 == SSA_NAME || !TREE_NO_WARNING (val1))
1267 && (code2 == SSA_NAME || !TREE_NO_WARNING (val2)))
1268 *strict_overflow_p = true;
1270 if (code1 == SSA_NAME)
1272 if (code2 == PLUS_EXPR)
1273 /* NAME < NAME + CST */
1274 return -1;
1275 else if (code2 == MINUS_EXPR)
1276 /* NAME > NAME - CST */
1277 return 1;
1279 else if (code1 == PLUS_EXPR)
1281 if (code2 == SSA_NAME)
1282 /* NAME + CST > NAME */
1283 return 1;
1284 else if (code2 == PLUS_EXPR)
1285 /* NAME + CST1 > NAME + CST2, if CST1 > CST2 */
1286 return compare_values_warnv (c1, c2, strict_overflow_p);
1287 else if (code2 == MINUS_EXPR)
1288 /* NAME + CST1 > NAME - CST2 */
1289 return 1;
1291 else if (code1 == MINUS_EXPR)
1293 if (code2 == SSA_NAME)
1294 /* NAME - CST < NAME */
1295 return -1;
1296 else if (code2 == PLUS_EXPR)
1297 /* NAME - CST1 < NAME + CST2 */
1298 return -1;
1299 else if (code2 == MINUS_EXPR)
1300 /* NAME - CST1 > NAME - CST2, if CST1 < CST2. Notice that
1301 C1 and C2 are swapped in the call to compare_values. */
1302 return compare_values_warnv (c2, c1, strict_overflow_p);
1305 gcc_unreachable ();
1308 /* We cannot compare non-constants. */
1309 if (!is_gimple_min_invariant (val1) || !is_gimple_min_invariant (val2))
1310 return -2;
1312 if (!POINTER_TYPE_P (TREE_TYPE (val1)))
1314 /* We cannot compare overflowed values, except for overflow
1315 infinities. */
1316 if (TREE_OVERFLOW (val1) || TREE_OVERFLOW (val2))
1318 if (strict_overflow_p != NULL)
1319 *strict_overflow_p = true;
1320 if (is_negative_overflow_infinity (val1))
1321 return is_negative_overflow_infinity (val2) ? 0 : -1;
1322 else if (is_negative_overflow_infinity (val2))
1323 return 1;
1324 else if (is_positive_overflow_infinity (val1))
1325 return is_positive_overflow_infinity (val2) ? 0 : 1;
1326 else if (is_positive_overflow_infinity (val2))
1327 return -1;
1328 return -2;
1331 return tree_int_cst_compare (val1, val2);
1333 else
1335 tree t;
1337 /* First see if VAL1 and VAL2 are not the same. */
1338 if (val1 == val2 || operand_equal_p (val1, val2, 0))
1339 return 0;
1341 /* If VAL1 is a lower address than VAL2, return -1. */
1342 if (operand_less_p (val1, val2) == 1)
1343 return -1;
1345 /* If VAL1 is a higher address than VAL2, return +1. */
1346 if (operand_less_p (val2, val1) == 1)
1347 return 1;
1349 /* If VAL1 is different than VAL2, return +2.
1350 For integer constants we either have already returned -1 or 1
1351 or they are equivalent. We still might succeed in proving
1352 something about non-trivial operands. */
1353 if (TREE_CODE (val1) != INTEGER_CST
1354 || TREE_CODE (val2) != INTEGER_CST)
1356 t = fold_binary_to_constant (NE_EXPR, boolean_type_node, val1, val2);
1357 if (t && integer_onep (t))
1358 return 2;
1361 return -2;
1365 /* Compare values like compare_values_warnv, but treat comparisons of
1366 nonconstants which rely on undefined overflow as incomparable. */
1368 static int
1369 compare_values (tree val1, tree val2)
1371 bool sop;
1372 int ret;
1374 sop = false;
1375 ret = compare_values_warnv (val1, val2, &sop);
1376 if (sop
1377 && (!is_gimple_min_invariant (val1) || !is_gimple_min_invariant (val2)))
1378 ret = -2;
1379 return ret;
1383 /* Return 1 if VAL is inside value range MIN <= VAL <= MAX,
1384 0 if VAL is not inside [MIN, MAX],
1385 -2 if we cannot tell either way.
1387 Benchmark compile/20001226-1.c compilation time after changing this
1388 function. */
1390 static inline int
1391 value_inside_range (tree val, tree min, tree max)
1393 int cmp1, cmp2;
1395 cmp1 = operand_less_p (val, min);
1396 if (cmp1 == -2)
1397 return -2;
1398 if (cmp1 == 1)
1399 return 0;
1401 cmp2 = operand_less_p (max, val);
1402 if (cmp2 == -2)
1403 return -2;
1405 return !cmp2;
1409 /* Return true if value ranges VR0 and VR1 have a non-empty
1410 intersection.
1412 Benchmark compile/20001226-1.c compilation time after changing this
1413 function.
1416 static inline bool
1417 value_ranges_intersect_p (value_range_t *vr0, value_range_t *vr1)
1419 /* The value ranges do not intersect if the maximum of the first range is
1420 less than the minimum of the second range or vice versa.
1421 When those relations are unknown, we can't do any better. */
1422 if (operand_less_p (vr0->max, vr1->min) != 0)
1423 return false;
1424 if (operand_less_p (vr1->max, vr0->min) != 0)
1425 return false;
1426 return true;
1430 /* Return 1 if [MIN, MAX] includes the value zero, 0 if it does not
1431 include the value zero, -2 if we cannot tell. */
1433 static inline int
1434 range_includes_zero_p (tree min, tree max)
1436 tree zero = build_int_cst (TREE_TYPE (min), 0);
1437 return value_inside_range (zero, min, max);
1440 /* Return true if *VR is know to only contain nonnegative values. */
1442 static inline bool
1443 value_range_nonnegative_p (value_range_t *vr)
1445 /* Testing for VR_ANTI_RANGE is not useful here as any anti-range
1446 which would return a useful value should be encoded as a
1447 VR_RANGE. */
1448 if (vr->type == VR_RANGE)
1450 int result = compare_values (vr->min, integer_zero_node);
1451 return (result == 0 || result == 1);
1454 return false;
1457 /* Return true if T, an SSA_NAME, is known to be nonnegative. Return
1458 false otherwise or if no value range information is available. */
1460 bool
1461 ssa_name_nonnegative_p (const_tree t)
1463 value_range_t *vr = get_value_range (t);
1465 if (INTEGRAL_TYPE_P (t)
1466 && TYPE_UNSIGNED (t))
1467 return true;
1469 if (!vr)
1470 return false;
1472 return value_range_nonnegative_p (vr);
1475 /* If *VR has a value rante that is a single constant value return that,
1476 otherwise return NULL_TREE. */
1478 static tree
1479 value_range_constant_singleton (value_range_t *vr)
1481 if (vr->type == VR_RANGE
1482 && operand_equal_p (vr->min, vr->max, 0)
1483 && is_gimple_min_invariant (vr->min))
1484 return vr->min;
1486 return NULL_TREE;
1489 /* If OP has a value range with a single constant value return that,
1490 otherwise return NULL_TREE. This returns OP itself if OP is a
1491 constant. */
1493 static tree
1494 op_with_constant_singleton_value_range (tree op)
1496 if (is_gimple_min_invariant (op))
1497 return op;
1499 if (TREE_CODE (op) != SSA_NAME)
1500 return NULL_TREE;
1502 return value_range_constant_singleton (get_value_range (op));
1505 /* Return true if op is in a boolean [0, 1] value-range. */
1507 static bool
1508 op_with_boolean_value_range_p (tree op)
1510 value_range_t *vr;
1512 if (TYPE_PRECISION (TREE_TYPE (op)) == 1)
1513 return true;
1515 if (integer_zerop (op)
1516 || integer_onep (op))
1517 return true;
1519 if (TREE_CODE (op) != SSA_NAME)
1520 return false;
1522 vr = get_value_range (op);
1523 return (vr->type == VR_RANGE
1524 && integer_zerop (vr->min)
1525 && integer_onep (vr->max));
1528 /* Extract value range information from an ASSERT_EXPR EXPR and store
1529 it in *VR_P. */
1531 static void
1532 extract_range_from_assert (value_range_t *vr_p, tree expr)
1534 tree var, cond, limit, min, max, type;
1535 value_range_t *limit_vr;
1536 enum tree_code cond_code;
1538 var = ASSERT_EXPR_VAR (expr);
1539 cond = ASSERT_EXPR_COND (expr);
1541 gcc_assert (COMPARISON_CLASS_P (cond));
1543 /* Find VAR in the ASSERT_EXPR conditional. */
1544 if (var == TREE_OPERAND (cond, 0)
1545 || TREE_CODE (TREE_OPERAND (cond, 0)) == PLUS_EXPR
1546 || TREE_CODE (TREE_OPERAND (cond, 0)) == NOP_EXPR)
1548 /* If the predicate is of the form VAR COMP LIMIT, then we just
1549 take LIMIT from the RHS and use the same comparison code. */
1550 cond_code = TREE_CODE (cond);
1551 limit = TREE_OPERAND (cond, 1);
1552 cond = TREE_OPERAND (cond, 0);
1554 else
1556 /* If the predicate is of the form LIMIT COMP VAR, then we need
1557 to flip around the comparison code to create the proper range
1558 for VAR. */
1559 cond_code = swap_tree_comparison (TREE_CODE (cond));
1560 limit = TREE_OPERAND (cond, 0);
1561 cond = TREE_OPERAND (cond, 1);
1564 limit = avoid_overflow_infinity (limit);
1566 type = TREE_TYPE (var);
1567 gcc_assert (limit != var);
1569 /* For pointer arithmetic, we only keep track of pointer equality
1570 and inequality. */
1571 if (POINTER_TYPE_P (type) && cond_code != NE_EXPR && cond_code != EQ_EXPR)
1573 set_value_range_to_varying (vr_p);
1574 return;
1577 /* If LIMIT is another SSA name and LIMIT has a range of its own,
1578 try to use LIMIT's range to avoid creating symbolic ranges
1579 unnecessarily. */
1580 limit_vr = (TREE_CODE (limit) == SSA_NAME) ? get_value_range (limit) : NULL;
1582 /* LIMIT's range is only interesting if it has any useful information. */
1583 if (limit_vr
1584 && (limit_vr->type == VR_UNDEFINED
1585 || limit_vr->type == VR_VARYING
1586 || symbolic_range_p (limit_vr)))
1587 limit_vr = NULL;
1589 /* Initially, the new range has the same set of equivalences of
1590 VAR's range. This will be revised before returning the final
1591 value. Since assertions may be chained via mutually exclusive
1592 predicates, we will need to trim the set of equivalences before
1593 we are done. */
1594 gcc_assert (vr_p->equiv == NULL);
1595 add_equivalence (&vr_p->equiv, var);
1597 /* Extract a new range based on the asserted comparison for VAR and
1598 LIMIT's value range. Notice that if LIMIT has an anti-range, we
1599 will only use it for equality comparisons (EQ_EXPR). For any
1600 other kind of assertion, we cannot derive a range from LIMIT's
1601 anti-range that can be used to describe the new range. For
1602 instance, ASSERT_EXPR <x_2, x_2 <= b_4>. If b_4 is ~[2, 10],
1603 then b_4 takes on the ranges [-INF, 1] and [11, +INF]. There is
1604 no single range for x_2 that could describe LE_EXPR, so we might
1605 as well build the range [b_4, +INF] for it.
1606 One special case we handle is extracting a range from a
1607 range test encoded as (unsigned)var + CST <= limit. */
1608 if (TREE_CODE (cond) == NOP_EXPR
1609 || TREE_CODE (cond) == PLUS_EXPR)
1611 if (TREE_CODE (cond) == PLUS_EXPR)
1613 min = fold_build1 (NEGATE_EXPR, TREE_TYPE (TREE_OPERAND (cond, 1)),
1614 TREE_OPERAND (cond, 1));
1615 max = int_const_binop (PLUS_EXPR, limit, min);
1616 cond = TREE_OPERAND (cond, 0);
1618 else
1620 min = build_int_cst (TREE_TYPE (var), 0);
1621 max = limit;
1624 /* Make sure to not set TREE_OVERFLOW on the final type
1625 conversion. We are willingly interpreting large positive
1626 unsigned values as negative singed values here. */
1627 min = force_fit_type_double (TREE_TYPE (var), tree_to_double_int (min),
1628 0, false);
1629 max = force_fit_type_double (TREE_TYPE (var), tree_to_double_int (max),
1630 0, false);
1632 /* We can transform a max, min range to an anti-range or
1633 vice-versa. Use set_and_canonicalize_value_range which does
1634 this for us. */
1635 if (cond_code == LE_EXPR)
1636 set_and_canonicalize_value_range (vr_p, VR_RANGE,
1637 min, max, vr_p->equiv);
1638 else if (cond_code == GT_EXPR)
1639 set_and_canonicalize_value_range (vr_p, VR_ANTI_RANGE,
1640 min, max, vr_p->equiv);
1641 else
1642 gcc_unreachable ();
1644 else if (cond_code == EQ_EXPR)
1646 enum value_range_type range_type;
1648 if (limit_vr)
1650 range_type = limit_vr->type;
1651 min = limit_vr->min;
1652 max = limit_vr->max;
1654 else
1656 range_type = VR_RANGE;
1657 min = limit;
1658 max = limit;
1661 set_value_range (vr_p, range_type, min, max, vr_p->equiv);
1663 /* When asserting the equality VAR == LIMIT and LIMIT is another
1664 SSA name, the new range will also inherit the equivalence set
1665 from LIMIT. */
1666 if (TREE_CODE (limit) == SSA_NAME)
1667 add_equivalence (&vr_p->equiv, limit);
1669 else if (cond_code == NE_EXPR)
1671 /* As described above, when LIMIT's range is an anti-range and
1672 this assertion is an inequality (NE_EXPR), then we cannot
1673 derive anything from the anti-range. For instance, if
1674 LIMIT's range was ~[0, 0], the assertion 'VAR != LIMIT' does
1675 not imply that VAR's range is [0, 0]. So, in the case of
1676 anti-ranges, we just assert the inequality using LIMIT and
1677 not its anti-range.
1679 If LIMIT_VR is a range, we can only use it to build a new
1680 anti-range if LIMIT_VR is a single-valued range. For
1681 instance, if LIMIT_VR is [0, 1], the predicate
1682 VAR != [0, 1] does not mean that VAR's range is ~[0, 1].
1683 Rather, it means that for value 0 VAR should be ~[0, 0]
1684 and for value 1, VAR should be ~[1, 1]. We cannot
1685 represent these ranges.
1687 The only situation in which we can build a valid
1688 anti-range is when LIMIT_VR is a single-valued range
1689 (i.e., LIMIT_VR->MIN == LIMIT_VR->MAX). In that case,
1690 build the anti-range ~[LIMIT_VR->MIN, LIMIT_VR->MAX]. */
1691 if (limit_vr
1692 && limit_vr->type == VR_RANGE
1693 && compare_values (limit_vr->min, limit_vr->max) == 0)
1695 min = limit_vr->min;
1696 max = limit_vr->max;
1698 else
1700 /* In any other case, we cannot use LIMIT's range to build a
1701 valid anti-range. */
1702 min = max = limit;
1705 /* If MIN and MAX cover the whole range for their type, then
1706 just use the original LIMIT. */
1707 if (INTEGRAL_TYPE_P (type)
1708 && vrp_val_is_min (min)
1709 && vrp_val_is_max (max))
1710 min = max = limit;
1712 set_value_range (vr_p, VR_ANTI_RANGE, min, max, vr_p->equiv);
1714 else if (cond_code == LE_EXPR || cond_code == LT_EXPR)
1716 min = TYPE_MIN_VALUE (type);
1718 if (limit_vr == NULL || limit_vr->type == VR_ANTI_RANGE)
1719 max = limit;
1720 else
1722 /* If LIMIT_VR is of the form [N1, N2], we need to build the
1723 range [MIN, N2] for LE_EXPR and [MIN, N2 - 1] for
1724 LT_EXPR. */
1725 max = limit_vr->max;
1728 /* If the maximum value forces us to be out of bounds, simply punt.
1729 It would be pointless to try and do anything more since this
1730 all should be optimized away above us. */
1731 if ((cond_code == LT_EXPR
1732 && compare_values (max, min) == 0)
1733 || (CONSTANT_CLASS_P (max) && TREE_OVERFLOW (max)))
1734 set_value_range_to_varying (vr_p);
1735 else
1737 /* For LT_EXPR, we create the range [MIN, MAX - 1]. */
1738 if (cond_code == LT_EXPR)
1740 if (TYPE_PRECISION (TREE_TYPE (max)) == 1
1741 && !TYPE_UNSIGNED (TREE_TYPE (max)))
1742 max = fold_build2 (PLUS_EXPR, TREE_TYPE (max), max,
1743 build_int_cst (TREE_TYPE (max), -1));
1744 else
1745 max = fold_build2 (MINUS_EXPR, TREE_TYPE (max), max,
1746 build_int_cst (TREE_TYPE (max), 1));
1747 if (EXPR_P (max))
1748 TREE_NO_WARNING (max) = 1;
1751 set_value_range (vr_p, VR_RANGE, min, max, vr_p->equiv);
1754 else if (cond_code == GE_EXPR || cond_code == GT_EXPR)
1756 max = TYPE_MAX_VALUE (type);
1758 if (limit_vr == NULL || limit_vr->type == VR_ANTI_RANGE)
1759 min = limit;
1760 else
1762 /* If LIMIT_VR is of the form [N1, N2], we need to build the
1763 range [N1, MAX] for GE_EXPR and [N1 + 1, MAX] for
1764 GT_EXPR. */
1765 min = limit_vr->min;
1768 /* If the minimum value forces us to be out of bounds, simply punt.
1769 It would be pointless to try and do anything more since this
1770 all should be optimized away above us. */
1771 if ((cond_code == GT_EXPR
1772 && compare_values (min, max) == 0)
1773 || (CONSTANT_CLASS_P (min) && TREE_OVERFLOW (min)))
1774 set_value_range_to_varying (vr_p);
1775 else
1777 /* For GT_EXPR, we create the range [MIN + 1, MAX]. */
1778 if (cond_code == GT_EXPR)
1780 if (TYPE_PRECISION (TREE_TYPE (min)) == 1
1781 && !TYPE_UNSIGNED (TREE_TYPE (min)))
1782 min = fold_build2 (MINUS_EXPR, TREE_TYPE (min), min,
1783 build_int_cst (TREE_TYPE (min), -1));
1784 else
1785 min = fold_build2 (PLUS_EXPR, TREE_TYPE (min), min,
1786 build_int_cst (TREE_TYPE (min), 1));
1787 if (EXPR_P (min))
1788 TREE_NO_WARNING (min) = 1;
1791 set_value_range (vr_p, VR_RANGE, min, max, vr_p->equiv);
1794 else
1795 gcc_unreachable ();
1797 /* Finally intersect the new range with what we already know about var. */
1798 vrp_intersect_ranges (vr_p, get_value_range (var));
1802 /* Extract range information from SSA name VAR and store it in VR. If
1803 VAR has an interesting range, use it. Otherwise, create the
1804 range [VAR, VAR] and return it. This is useful in situations where
1805 we may have conditionals testing values of VARYING names. For
1806 instance,
1808 x_3 = y_5;
1809 if (x_3 > y_5)
1812 Even if y_5 is deemed VARYING, we can determine that x_3 > y_5 is
1813 always false. */
1815 static void
1816 extract_range_from_ssa_name (value_range_t *vr, tree var)
1818 value_range_t *var_vr = get_value_range (var);
1820 if (var_vr->type != VR_UNDEFINED && var_vr->type != VR_VARYING)
1821 copy_value_range (vr, var_vr);
1822 else
1823 set_value_range (vr, VR_RANGE, var, var, NULL);
1825 add_equivalence (&vr->equiv, var);
1829 /* Wrapper around int_const_binop. If the operation overflows and we
1830 are not using wrapping arithmetic, then adjust the result to be
1831 -INF or +INF depending on CODE, VAL1 and VAL2. This can return
1832 NULL_TREE if we need to use an overflow infinity representation but
1833 the type does not support it. */
1835 static tree
1836 vrp_int_const_binop (enum tree_code code, tree val1, tree val2)
1838 tree res;
1840 res = int_const_binop (code, val1, val2);
1842 /* If we are using unsigned arithmetic, operate symbolically
1843 on -INF and +INF as int_const_binop only handles signed overflow. */
1844 if (TYPE_UNSIGNED (TREE_TYPE (val1)))
1846 int checkz = compare_values (res, val1);
1847 bool overflow = false;
1849 /* Ensure that res = val1 [+*] val2 >= val1
1850 or that res = val1 - val2 <= val1. */
1851 if ((code == PLUS_EXPR
1852 && !(checkz == 1 || checkz == 0))
1853 || (code == MINUS_EXPR
1854 && !(checkz == 0 || checkz == -1)))
1856 overflow = true;
1858 /* Checking for multiplication overflow is done by dividing the
1859 output of the multiplication by the first input of the
1860 multiplication. If the result of that division operation is
1861 not equal to the second input of the multiplication, then the
1862 multiplication overflowed. */
1863 else if (code == MULT_EXPR && !integer_zerop (val1))
1865 tree tmp = int_const_binop (TRUNC_DIV_EXPR,
1866 res,
1867 val1);
1868 int check = compare_values (tmp, val2);
1870 if (check != 0)
1871 overflow = true;
1874 if (overflow)
1876 res = copy_node (res);
1877 TREE_OVERFLOW (res) = 1;
1881 else if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (val1)))
1882 /* If the singed operation wraps then int_const_binop has done
1883 everything we want. */
1885 else if ((TREE_OVERFLOW (res)
1886 && !TREE_OVERFLOW (val1)
1887 && !TREE_OVERFLOW (val2))
1888 || is_overflow_infinity (val1)
1889 || is_overflow_infinity (val2))
1891 /* If the operation overflowed but neither VAL1 nor VAL2 are
1892 overflown, return -INF or +INF depending on the operation
1893 and the combination of signs of the operands. */
1894 int sgn1 = tree_int_cst_sgn (val1);
1895 int sgn2 = tree_int_cst_sgn (val2);
1897 if (needs_overflow_infinity (TREE_TYPE (res))
1898 && !supports_overflow_infinity (TREE_TYPE (res)))
1899 return NULL_TREE;
1901 /* We have to punt on adding infinities of different signs,
1902 since we can't tell what the sign of the result should be.
1903 Likewise for subtracting infinities of the same sign. */
1904 if (((code == PLUS_EXPR && sgn1 != sgn2)
1905 || (code == MINUS_EXPR && sgn1 == sgn2))
1906 && is_overflow_infinity (val1)
1907 && is_overflow_infinity (val2))
1908 return NULL_TREE;
1910 /* Don't try to handle division or shifting of infinities. */
1911 if ((code == TRUNC_DIV_EXPR
1912 || code == FLOOR_DIV_EXPR
1913 || code == CEIL_DIV_EXPR
1914 || code == EXACT_DIV_EXPR
1915 || code == ROUND_DIV_EXPR
1916 || code == RSHIFT_EXPR)
1917 && (is_overflow_infinity (val1)
1918 || is_overflow_infinity (val2)))
1919 return NULL_TREE;
1921 /* Notice that we only need to handle the restricted set of
1922 operations handled by extract_range_from_binary_expr.
1923 Among them, only multiplication, addition and subtraction
1924 can yield overflow without overflown operands because we
1925 are working with integral types only... except in the
1926 case VAL1 = -INF and VAL2 = -1 which overflows to +INF
1927 for division too. */
1929 /* For multiplication, the sign of the overflow is given
1930 by the comparison of the signs of the operands. */
1931 if ((code == MULT_EXPR && sgn1 == sgn2)
1932 /* For addition, the operands must be of the same sign
1933 to yield an overflow. Its sign is therefore that
1934 of one of the operands, for example the first. For
1935 infinite operands X + -INF is negative, not positive. */
1936 || (code == PLUS_EXPR
1937 && (sgn1 >= 0
1938 ? !is_negative_overflow_infinity (val2)
1939 : is_positive_overflow_infinity (val2)))
1940 /* For subtraction, non-infinite operands must be of
1941 different signs to yield an overflow. Its sign is
1942 therefore that of the first operand or the opposite of
1943 that of the second operand. A first operand of 0 counts
1944 as positive here, for the corner case 0 - (-INF), which
1945 overflows, but must yield +INF. For infinite operands 0
1946 - INF is negative, not positive. */
1947 || (code == MINUS_EXPR
1948 && (sgn1 >= 0
1949 ? !is_positive_overflow_infinity (val2)
1950 : is_negative_overflow_infinity (val2)))
1951 /* We only get in here with positive shift count, so the
1952 overflow direction is the same as the sign of val1.
1953 Actually rshift does not overflow at all, but we only
1954 handle the case of shifting overflowed -INF and +INF. */
1955 || (code == RSHIFT_EXPR
1956 && sgn1 >= 0)
1957 /* For division, the only case is -INF / -1 = +INF. */
1958 || code == TRUNC_DIV_EXPR
1959 || code == FLOOR_DIV_EXPR
1960 || code == CEIL_DIV_EXPR
1961 || code == EXACT_DIV_EXPR
1962 || code == ROUND_DIV_EXPR)
1963 return (needs_overflow_infinity (TREE_TYPE (res))
1964 ? positive_overflow_infinity (TREE_TYPE (res))
1965 : TYPE_MAX_VALUE (TREE_TYPE (res)));
1966 else
1967 return (needs_overflow_infinity (TREE_TYPE (res))
1968 ? negative_overflow_infinity (TREE_TYPE (res))
1969 : TYPE_MIN_VALUE (TREE_TYPE (res)));
1972 return res;
1976 /* For range VR compute two double_int bitmasks. In *MAY_BE_NONZERO
1977 bitmask if some bit is unset, it means for all numbers in the range
1978 the bit is 0, otherwise it might be 0 or 1. In *MUST_BE_NONZERO
1979 bitmask if some bit is set, it means for all numbers in the range
1980 the bit is 1, otherwise it might be 0 or 1. */
1982 static bool
1983 zero_nonzero_bits_from_vr (value_range_t *vr,
1984 double_int *may_be_nonzero,
1985 double_int *must_be_nonzero)
1987 *may_be_nonzero = double_int_minus_one;
1988 *must_be_nonzero = double_int_zero;
1989 if (!range_int_cst_p (vr)
1990 || TREE_OVERFLOW (vr->min)
1991 || TREE_OVERFLOW (vr->max))
1992 return false;
1994 if (range_int_cst_singleton_p (vr))
1996 *may_be_nonzero = tree_to_double_int (vr->min);
1997 *must_be_nonzero = *may_be_nonzero;
1999 else if (tree_int_cst_sgn (vr->min) >= 0
2000 || tree_int_cst_sgn (vr->max) < 0)
2002 double_int dmin = tree_to_double_int (vr->min);
2003 double_int dmax = tree_to_double_int (vr->max);
2004 double_int xor_mask = dmin ^ dmax;
2005 *may_be_nonzero = dmin | dmax;
2006 *must_be_nonzero = dmin & dmax;
2007 if (xor_mask.high != 0)
2009 unsigned HOST_WIDE_INT mask
2010 = ((unsigned HOST_WIDE_INT) 1
2011 << floor_log2 (xor_mask.high)) - 1;
2012 may_be_nonzero->low = ALL_ONES;
2013 may_be_nonzero->high |= mask;
2014 must_be_nonzero->low = 0;
2015 must_be_nonzero->high &= ~mask;
2017 else if (xor_mask.low != 0)
2019 unsigned HOST_WIDE_INT mask
2020 = ((unsigned HOST_WIDE_INT) 1
2021 << floor_log2 (xor_mask.low)) - 1;
2022 may_be_nonzero->low |= mask;
2023 must_be_nonzero->low &= ~mask;
2027 return true;
2030 /* Create two value-ranges in *VR0 and *VR1 from the anti-range *AR
2031 so that *VR0 U *VR1 == *AR. Returns true if that is possible,
2032 false otherwise. If *AR can be represented with a single range
2033 *VR1 will be VR_UNDEFINED. */
2035 static bool
2036 ranges_from_anti_range (value_range_t *ar,
2037 value_range_t *vr0, value_range_t *vr1)
2039 tree type = TREE_TYPE (ar->min);
2041 vr0->type = VR_UNDEFINED;
2042 vr1->type = VR_UNDEFINED;
2044 if (ar->type != VR_ANTI_RANGE
2045 || TREE_CODE (ar->min) != INTEGER_CST
2046 || TREE_CODE (ar->max) != INTEGER_CST
2047 || !vrp_val_min (type)
2048 || !vrp_val_max (type))
2049 return false;
2051 if (!vrp_val_is_min (ar->min))
2053 vr0->type = VR_RANGE;
2054 vr0->min = vrp_val_min (type);
2055 vr0->max
2056 = double_int_to_tree (type,
2057 tree_to_double_int (ar->min) - double_int_one);
2059 if (!vrp_val_is_max (ar->max))
2061 vr1->type = VR_RANGE;
2062 vr1->min
2063 = double_int_to_tree (type,
2064 tree_to_double_int (ar->max) + double_int_one);
2065 vr1->max = vrp_val_max (type);
2067 if (vr0->type == VR_UNDEFINED)
2069 *vr0 = *vr1;
2070 vr1->type = VR_UNDEFINED;
2073 return vr0->type != VR_UNDEFINED;
2076 /* Helper to extract a value-range *VR for a multiplicative operation
2077 *VR0 CODE *VR1. */
2079 static void
2080 extract_range_from_multiplicative_op_1 (value_range_t *vr,
2081 enum tree_code code,
2082 value_range_t *vr0, value_range_t *vr1)
2084 enum value_range_type type;
2085 tree val[4];
2086 size_t i;
2087 tree min, max;
2088 bool sop;
2089 int cmp;
2091 /* Multiplications, divisions and shifts are a bit tricky to handle,
2092 depending on the mix of signs we have in the two ranges, we
2093 need to operate on different values to get the minimum and
2094 maximum values for the new range. One approach is to figure
2095 out all the variations of range combinations and do the
2096 operations.
2098 However, this involves several calls to compare_values and it
2099 is pretty convoluted. It's simpler to do the 4 operations
2100 (MIN0 OP MIN1, MIN0 OP MAX1, MAX0 OP MIN1 and MAX0 OP MAX0 OP
2101 MAX1) and then figure the smallest and largest values to form
2102 the new range. */
2103 gcc_assert (code == MULT_EXPR
2104 || code == TRUNC_DIV_EXPR
2105 || code == FLOOR_DIV_EXPR
2106 || code == CEIL_DIV_EXPR
2107 || code == EXACT_DIV_EXPR
2108 || code == ROUND_DIV_EXPR
2109 || code == RSHIFT_EXPR
2110 || code == LSHIFT_EXPR);
2111 gcc_assert ((vr0->type == VR_RANGE
2112 || (code == MULT_EXPR && vr0->type == VR_ANTI_RANGE))
2113 && vr0->type == vr1->type);
2115 type = vr0->type;
2117 /* Compute the 4 cross operations. */
2118 sop = false;
2119 val[0] = vrp_int_const_binop (code, vr0->min, vr1->min);
2120 if (val[0] == NULL_TREE)
2121 sop = true;
2123 if (vr1->max == vr1->min)
2124 val[1] = NULL_TREE;
2125 else
2127 val[1] = vrp_int_const_binop (code, vr0->min, vr1->max);
2128 if (val[1] == NULL_TREE)
2129 sop = true;
2132 if (vr0->max == vr0->min)
2133 val[2] = NULL_TREE;
2134 else
2136 val[2] = vrp_int_const_binop (code, vr0->max, vr1->min);
2137 if (val[2] == NULL_TREE)
2138 sop = true;
2141 if (vr0->min == vr0->max || vr1->min == vr1->max)
2142 val[3] = NULL_TREE;
2143 else
2145 val[3] = vrp_int_const_binop (code, vr0->max, vr1->max);
2146 if (val[3] == NULL_TREE)
2147 sop = true;
2150 if (sop)
2152 set_value_range_to_varying (vr);
2153 return;
2156 /* Set MIN to the minimum of VAL[i] and MAX to the maximum
2157 of VAL[i]. */
2158 min = val[0];
2159 max = val[0];
2160 for (i = 1; i < 4; i++)
2162 if (!is_gimple_min_invariant (min)
2163 || (TREE_OVERFLOW (min) && !is_overflow_infinity (min))
2164 || !is_gimple_min_invariant (max)
2165 || (TREE_OVERFLOW (max) && !is_overflow_infinity (max)))
2166 break;
2168 if (val[i])
2170 if (!is_gimple_min_invariant (val[i])
2171 || (TREE_OVERFLOW (val[i])
2172 && !is_overflow_infinity (val[i])))
2174 /* If we found an overflowed value, set MIN and MAX
2175 to it so that we set the resulting range to
2176 VARYING. */
2177 min = max = val[i];
2178 break;
2181 if (compare_values (val[i], min) == -1)
2182 min = val[i];
2184 if (compare_values (val[i], max) == 1)
2185 max = val[i];
2189 /* If either MIN or MAX overflowed, then set the resulting range to
2190 VARYING. But we do accept an overflow infinity
2191 representation. */
2192 if (min == NULL_TREE
2193 || !is_gimple_min_invariant (min)
2194 || (TREE_OVERFLOW (min) && !is_overflow_infinity (min))
2195 || max == NULL_TREE
2196 || !is_gimple_min_invariant (max)
2197 || (TREE_OVERFLOW (max) && !is_overflow_infinity (max)))
2199 set_value_range_to_varying (vr);
2200 return;
2203 /* We punt if:
2204 1) [-INF, +INF]
2205 2) [-INF, +-INF(OVF)]
2206 3) [+-INF(OVF), +INF]
2207 4) [+-INF(OVF), +-INF(OVF)]
2208 We learn nothing when we have INF and INF(OVF) on both sides.
2209 Note that we do accept [-INF, -INF] and [+INF, +INF] without
2210 overflow. */
2211 if ((vrp_val_is_min (min) || is_overflow_infinity (min))
2212 && (vrp_val_is_max (max) || is_overflow_infinity (max)))
2214 set_value_range_to_varying (vr);
2215 return;
2218 cmp = compare_values (min, max);
2219 if (cmp == -2 || cmp == 1)
2221 /* If the new range has its limits swapped around (MIN > MAX),
2222 then the operation caused one of them to wrap around, mark
2223 the new range VARYING. */
2224 set_value_range_to_varying (vr);
2226 else
2227 set_value_range (vr, type, min, max, NULL);
2230 /* Some quadruple precision helpers. */
2231 static int
2232 quad_int_cmp (double_int l0, double_int h0,
2233 double_int l1, double_int h1, bool uns)
2235 int c = h0.cmp (h1, uns);
2236 if (c != 0) return c;
2237 return l0.ucmp (l1);
2240 static void
2241 quad_int_pair_sort (double_int *l0, double_int *h0,
2242 double_int *l1, double_int *h1, bool uns)
2244 if (quad_int_cmp (*l0, *h0, *l1, *h1, uns) > 0)
2246 double_int tmp;
2247 tmp = *l0; *l0 = *l1; *l1 = tmp;
2248 tmp = *h0; *h0 = *h1; *h1 = tmp;
2252 /* Extract range information from a binary operation CODE based on
2253 the ranges of each of its operands, *VR0 and *VR1 with resulting
2254 type EXPR_TYPE. The resulting range is stored in *VR. */
2256 static void
2257 extract_range_from_binary_expr_1 (value_range_t *vr,
2258 enum tree_code code, tree expr_type,
2259 value_range_t *vr0_, value_range_t *vr1_)
2261 value_range_t vr0 = *vr0_, vr1 = *vr1_;
2262 value_range_t vrtem0 = VR_INITIALIZER, vrtem1 = VR_INITIALIZER;
2263 enum value_range_type type;
2264 tree min = NULL_TREE, max = NULL_TREE;
2265 int cmp;
2267 if (!INTEGRAL_TYPE_P (expr_type)
2268 && !POINTER_TYPE_P (expr_type))
2270 set_value_range_to_varying (vr);
2271 return;
2274 /* Not all binary expressions can be applied to ranges in a
2275 meaningful way. Handle only arithmetic operations. */
2276 if (code != PLUS_EXPR
2277 && code != MINUS_EXPR
2278 && code != POINTER_PLUS_EXPR
2279 && code != MULT_EXPR
2280 && code != TRUNC_DIV_EXPR
2281 && code != FLOOR_DIV_EXPR
2282 && code != CEIL_DIV_EXPR
2283 && code != EXACT_DIV_EXPR
2284 && code != ROUND_DIV_EXPR
2285 && code != TRUNC_MOD_EXPR
2286 && code != RSHIFT_EXPR
2287 && code != LSHIFT_EXPR
2288 && code != MIN_EXPR
2289 && code != MAX_EXPR
2290 && code != BIT_AND_EXPR
2291 && code != BIT_IOR_EXPR
2292 && code != BIT_XOR_EXPR)
2294 set_value_range_to_varying (vr);
2295 return;
2298 /* If both ranges are UNDEFINED, so is the result. */
2299 if (vr0.type == VR_UNDEFINED && vr1.type == VR_UNDEFINED)
2301 set_value_range_to_undefined (vr);
2302 return;
2304 /* If one of the ranges is UNDEFINED drop it to VARYING for the following
2305 code. At some point we may want to special-case operations that
2306 have UNDEFINED result for all or some value-ranges of the not UNDEFINED
2307 operand. */
2308 else if (vr0.type == VR_UNDEFINED)
2309 set_value_range_to_varying (&vr0);
2310 else if (vr1.type == VR_UNDEFINED)
2311 set_value_range_to_varying (&vr1);
2313 /* Now canonicalize anti-ranges to ranges when they are not symbolic
2314 and express ~[] op X as ([]' op X) U ([]'' op X). */
2315 if (vr0.type == VR_ANTI_RANGE
2316 && ranges_from_anti_range (&vr0, &vrtem0, &vrtem1))
2318 extract_range_from_binary_expr_1 (vr, code, expr_type, &vrtem0, vr1_);
2319 if (vrtem1.type != VR_UNDEFINED)
2321 value_range_t vrres = VR_INITIALIZER;
2322 extract_range_from_binary_expr_1 (&vrres, code, expr_type,
2323 &vrtem1, vr1_);
2324 vrp_meet (vr, &vrres);
2326 return;
2328 /* Likewise for X op ~[]. */
2329 if (vr1.type == VR_ANTI_RANGE
2330 && ranges_from_anti_range (&vr1, &vrtem0, &vrtem1))
2332 extract_range_from_binary_expr_1 (vr, code, expr_type, vr0_, &vrtem0);
2333 if (vrtem1.type != VR_UNDEFINED)
2335 value_range_t vrres = VR_INITIALIZER;
2336 extract_range_from_binary_expr_1 (&vrres, code, expr_type,
2337 vr0_, &vrtem1);
2338 vrp_meet (vr, &vrres);
2340 return;
2343 /* The type of the resulting value range defaults to VR0.TYPE. */
2344 type = vr0.type;
2346 /* Refuse to operate on VARYING ranges, ranges of different kinds
2347 and symbolic ranges. As an exception, we allow BIT_AND_EXPR
2348 because we may be able to derive a useful range even if one of
2349 the operands is VR_VARYING or symbolic range. Similarly for
2350 divisions. TODO, we may be able to derive anti-ranges in
2351 some cases. */
2352 if (code != BIT_AND_EXPR
2353 && code != BIT_IOR_EXPR
2354 && code != TRUNC_DIV_EXPR
2355 && code != FLOOR_DIV_EXPR
2356 && code != CEIL_DIV_EXPR
2357 && code != EXACT_DIV_EXPR
2358 && code != ROUND_DIV_EXPR
2359 && code != TRUNC_MOD_EXPR
2360 && (vr0.type == VR_VARYING
2361 || vr1.type == VR_VARYING
2362 || vr0.type != vr1.type
2363 || symbolic_range_p (&vr0)
2364 || symbolic_range_p (&vr1)))
2366 set_value_range_to_varying (vr);
2367 return;
2370 /* Now evaluate the expression to determine the new range. */
2371 if (POINTER_TYPE_P (expr_type))
2373 if (code == MIN_EXPR || code == MAX_EXPR)
2375 /* For MIN/MAX expressions with pointers, we only care about
2376 nullness, if both are non null, then the result is nonnull.
2377 If both are null, then the result is null. Otherwise they
2378 are varying. */
2379 if (range_is_nonnull (&vr0) && range_is_nonnull (&vr1))
2380 set_value_range_to_nonnull (vr, expr_type);
2381 else if (range_is_null (&vr0) && range_is_null (&vr1))
2382 set_value_range_to_null (vr, expr_type);
2383 else
2384 set_value_range_to_varying (vr);
2386 else if (code == POINTER_PLUS_EXPR)
2388 /* For pointer types, we are really only interested in asserting
2389 whether the expression evaluates to non-NULL. */
2390 if (range_is_nonnull (&vr0) || range_is_nonnull (&vr1))
2391 set_value_range_to_nonnull (vr, expr_type);
2392 else if (range_is_null (&vr0) && range_is_null (&vr1))
2393 set_value_range_to_null (vr, expr_type);
2394 else
2395 set_value_range_to_varying (vr);
2397 else if (code == BIT_AND_EXPR)
2399 /* For pointer types, we are really only interested in asserting
2400 whether the expression evaluates to non-NULL. */
2401 if (range_is_nonnull (&vr0) && range_is_nonnull (&vr1))
2402 set_value_range_to_nonnull (vr, expr_type);
2403 else if (range_is_null (&vr0) || range_is_null (&vr1))
2404 set_value_range_to_null (vr, expr_type);
2405 else
2406 set_value_range_to_varying (vr);
2408 else
2409 set_value_range_to_varying (vr);
2411 return;
2414 /* For integer ranges, apply the operation to each end of the
2415 range and see what we end up with. */
2416 if (code == PLUS_EXPR || code == MINUS_EXPR)
2418 /* If we have a PLUS_EXPR with two VR_RANGE integer constant
2419 ranges compute the precise range for such case if possible. */
2420 if (range_int_cst_p (&vr0)
2421 && range_int_cst_p (&vr1)
2422 /* We need as many bits as the possibly unsigned inputs. */
2423 && TYPE_PRECISION (expr_type) <= HOST_BITS_PER_DOUBLE_INT)
2425 double_int min0 = tree_to_double_int (vr0.min);
2426 double_int max0 = tree_to_double_int (vr0.max);
2427 double_int min1 = tree_to_double_int (vr1.min);
2428 double_int max1 = tree_to_double_int (vr1.max);
2429 bool uns = TYPE_UNSIGNED (expr_type);
2430 double_int type_min
2431 = double_int::min_value (TYPE_PRECISION (expr_type), uns);
2432 double_int type_max
2433 = double_int::max_value (TYPE_PRECISION (expr_type), uns);
2434 double_int dmin, dmax;
2435 int min_ovf = 0;
2436 int max_ovf = 0;
2438 if (code == PLUS_EXPR)
2440 dmin = min0 + min1;
2441 dmax = max0 + max1;
2443 /* Check for overflow in double_int. */
2444 if (min1.cmp (double_int_zero, uns) != dmin.cmp (min0, uns))
2445 min_ovf = min0.cmp (dmin, uns);
2446 if (max1.cmp (double_int_zero, uns) != dmax.cmp (max0, uns))
2447 max_ovf = max0.cmp (dmax, uns);
2449 else /* if (code == MINUS_EXPR) */
2451 dmin = min0 - max1;
2452 dmax = max0 - min1;
2454 if (double_int_zero.cmp (max1, uns) != dmin.cmp (min0, uns))
2455 min_ovf = min0.cmp (max1, uns);
2456 if (double_int_zero.cmp (min1, uns) != dmax.cmp (max0, uns))
2457 max_ovf = max0.cmp (min1, uns);
2460 /* For non-wrapping arithmetic look at possibly smaller
2461 value-ranges of the type. */
2462 if (!TYPE_OVERFLOW_WRAPS (expr_type))
2464 if (vrp_val_min (expr_type))
2465 type_min = tree_to_double_int (vrp_val_min (expr_type));
2466 if (vrp_val_max (expr_type))
2467 type_max = tree_to_double_int (vrp_val_max (expr_type));
2470 /* Check for type overflow. */
2471 if (min_ovf == 0)
2473 if (dmin.cmp (type_min, uns) == -1)
2474 min_ovf = -1;
2475 else if (dmin.cmp (type_max, uns) == 1)
2476 min_ovf = 1;
2478 if (max_ovf == 0)
2480 if (dmax.cmp (type_min, uns) == -1)
2481 max_ovf = -1;
2482 else if (dmax.cmp (type_max, uns) == 1)
2483 max_ovf = 1;
2486 if (TYPE_OVERFLOW_WRAPS (expr_type))
2488 /* If overflow wraps, truncate the values and adjust the
2489 range kind and bounds appropriately. */
2490 double_int tmin
2491 = dmin.ext (TYPE_PRECISION (expr_type), uns);
2492 double_int tmax
2493 = dmax.ext (TYPE_PRECISION (expr_type), uns);
2494 if (min_ovf == max_ovf)
2496 /* No overflow or both overflow or underflow. The
2497 range kind stays VR_RANGE. */
2498 min = double_int_to_tree (expr_type, tmin);
2499 max = double_int_to_tree (expr_type, tmax);
2501 else if (min_ovf == -1
2502 && max_ovf == 1)
2504 /* Underflow and overflow, drop to VR_VARYING. */
2505 set_value_range_to_varying (vr);
2506 return;
2508 else
2510 /* Min underflow or max overflow. The range kind
2511 changes to VR_ANTI_RANGE. */
2512 bool covers = false;
2513 double_int tem = tmin;
2514 gcc_assert ((min_ovf == -1 && max_ovf == 0)
2515 || (max_ovf == 1 && min_ovf == 0));
2516 type = VR_ANTI_RANGE;
2517 tmin = tmax + double_int_one;
2518 if (tmin.cmp (tmax, uns) < 0)
2519 covers = true;
2520 tmax = tem + double_int_minus_one;
2521 if (tmax.cmp (tem, uns) > 0)
2522 covers = true;
2523 /* If the anti-range would cover nothing, drop to varying.
2524 Likewise if the anti-range bounds are outside of the
2525 types values. */
2526 if (covers || tmin.cmp (tmax, uns) > 0)
2528 set_value_range_to_varying (vr);
2529 return;
2531 min = double_int_to_tree (expr_type, tmin);
2532 max = double_int_to_tree (expr_type, tmax);
2535 else
2537 /* If overflow does not wrap, saturate to the types min/max
2538 value. */
2539 if (min_ovf == -1)
2541 if (needs_overflow_infinity (expr_type)
2542 && supports_overflow_infinity (expr_type))
2543 min = negative_overflow_infinity (expr_type);
2544 else
2545 min = double_int_to_tree (expr_type, type_min);
2547 else if (min_ovf == 1)
2549 if (needs_overflow_infinity (expr_type)
2550 && supports_overflow_infinity (expr_type))
2551 min = positive_overflow_infinity (expr_type);
2552 else
2553 min = double_int_to_tree (expr_type, type_max);
2555 else
2556 min = double_int_to_tree (expr_type, dmin);
2558 if (max_ovf == -1)
2560 if (needs_overflow_infinity (expr_type)
2561 && supports_overflow_infinity (expr_type))
2562 max = negative_overflow_infinity (expr_type);
2563 else
2564 max = double_int_to_tree (expr_type, type_min);
2566 else if (max_ovf == 1)
2568 if (needs_overflow_infinity (expr_type)
2569 && supports_overflow_infinity (expr_type))
2570 max = positive_overflow_infinity (expr_type);
2571 else
2572 max = double_int_to_tree (expr_type, type_max);
2574 else
2575 max = double_int_to_tree (expr_type, dmax);
2577 if (needs_overflow_infinity (expr_type)
2578 && supports_overflow_infinity (expr_type))
2580 if (is_negative_overflow_infinity (vr0.min)
2581 || (code == PLUS_EXPR
2582 ? is_negative_overflow_infinity (vr1.min)
2583 : is_positive_overflow_infinity (vr1.max)))
2584 min = negative_overflow_infinity (expr_type);
2585 if (is_positive_overflow_infinity (vr0.max)
2586 || (code == PLUS_EXPR
2587 ? is_positive_overflow_infinity (vr1.max)
2588 : is_negative_overflow_infinity (vr1.min)))
2589 max = positive_overflow_infinity (expr_type);
2592 else
2594 /* For other cases, for example if we have a PLUS_EXPR with two
2595 VR_ANTI_RANGEs, drop to VR_VARYING. It would take more effort
2596 to compute a precise range for such a case.
2597 ??? General even mixed range kind operations can be expressed
2598 by for example transforming ~[3, 5] + [1, 2] to range-only
2599 operations and a union primitive:
2600 [-INF, 2] + [1, 2] U [5, +INF] + [1, 2]
2601 [-INF+1, 4] U [6, +INF(OVF)]
2602 though usually the union is not exactly representable with
2603 a single range or anti-range as the above is
2604 [-INF+1, +INF(OVF)] intersected with ~[5, 5]
2605 but one could use a scheme similar to equivalences for this. */
2606 set_value_range_to_varying (vr);
2607 return;
2610 else if (code == MIN_EXPR
2611 || code == MAX_EXPR)
2613 if (vr0.type == VR_ANTI_RANGE)
2615 /* For MIN_EXPR and MAX_EXPR with two VR_ANTI_RANGEs,
2616 the resulting VR_ANTI_RANGE is the same - intersection
2617 of the two ranges. */
2618 min = vrp_int_const_binop (MAX_EXPR, vr0.min, vr1.min);
2619 max = vrp_int_const_binop (MIN_EXPR, vr0.max, vr1.max);
2621 else
2623 /* For operations that make the resulting range directly
2624 proportional to the original ranges, apply the operation to
2625 the same end of each range. */
2626 min = vrp_int_const_binop (code, vr0.min, vr1.min);
2627 max = vrp_int_const_binop (code, vr0.max, vr1.max);
2630 else if (code == MULT_EXPR)
2632 /* Fancy code so that with unsigned, [-3,-1]*[-3,-1] does not
2633 drop to varying. */
2634 if (range_int_cst_p (&vr0)
2635 && range_int_cst_p (&vr1)
2636 && TYPE_OVERFLOW_WRAPS (expr_type))
2638 double_int min0, max0, min1, max1, sizem1, size;
2639 double_int prod0l, prod0h, prod1l, prod1h,
2640 prod2l, prod2h, prod3l, prod3h;
2641 bool uns0, uns1, uns;
2643 sizem1 = double_int::max_value (TYPE_PRECISION (expr_type), true);
2644 size = sizem1 + double_int_one;
2646 min0 = tree_to_double_int (vr0.min);
2647 max0 = tree_to_double_int (vr0.max);
2648 min1 = tree_to_double_int (vr1.min);
2649 max1 = tree_to_double_int (vr1.max);
2651 uns0 = TYPE_UNSIGNED (expr_type);
2652 uns1 = uns0;
2654 /* Canonicalize the intervals. */
2655 if (TYPE_UNSIGNED (expr_type))
2657 double_int min2 = size - min0;
2658 if (min2.cmp (max0, true) < 0)
2660 min0 = -min2;
2661 max0 -= size;
2662 uns0 = false;
2665 min2 = size - min1;
2666 if (min2.cmp (max1, true) < 0)
2668 min1 = -min2;
2669 max1 -= size;
2670 uns1 = false;
2673 uns = uns0 & uns1;
2675 bool overflow;
2676 prod0l = min0.wide_mul_with_sign (min1, true, &prod0h, &overflow);
2677 if (!uns0 && min0.is_negative ())
2678 prod0h -= min1;
2679 if (!uns1 && min1.is_negative ())
2680 prod0h -= min0;
2682 prod1l = min0.wide_mul_with_sign (max1, true, &prod1h, &overflow);
2683 if (!uns0 && min0.is_negative ())
2684 prod1h -= max1;
2685 if (!uns1 && max1.is_negative ())
2686 prod1h -= min0;
2688 prod2l = max0.wide_mul_with_sign (min1, true, &prod2h, &overflow);
2689 if (!uns0 && max0.is_negative ())
2690 prod2h -= min1;
2691 if (!uns1 && min1.is_negative ())
2692 prod2h -= max0;
2694 prod3l = max0.wide_mul_with_sign (max1, true, &prod3h, &overflow);
2695 if (!uns0 && max0.is_negative ())
2696 prod3h -= max1;
2697 if (!uns1 && max1.is_negative ())
2698 prod3h -= max0;
2700 /* Sort the 4 products. */
2701 quad_int_pair_sort (&prod0l, &prod0h, &prod3l, &prod3h, uns);
2702 quad_int_pair_sort (&prod1l, &prod1h, &prod2l, &prod2h, uns);
2703 quad_int_pair_sort (&prod0l, &prod0h, &prod1l, &prod1h, uns);
2704 quad_int_pair_sort (&prod2l, &prod2h, &prod3l, &prod3h, uns);
2706 /* Max - min. */
2707 if (prod0l.is_zero ())
2709 prod1l = double_int_zero;
2710 prod1h = -prod0h;
2712 else
2714 prod1l = -prod0l;
2715 prod1h = ~prod0h;
2717 prod2l = prod3l + prod1l;
2718 prod2h = prod3h + prod1h;
2719 if (prod2l.ult (prod3l))
2720 prod2h += double_int_one; /* carry */
2722 if (!prod2h.is_zero ()
2723 || prod2l.cmp (sizem1, true) >= 0)
2725 /* the range covers all values. */
2726 set_value_range_to_varying (vr);
2727 return;
2730 /* The following should handle the wrapping and selecting
2731 VR_ANTI_RANGE for us. */
2732 min = double_int_to_tree (expr_type, prod0l);
2733 max = double_int_to_tree (expr_type, prod3l);
2734 set_and_canonicalize_value_range (vr, VR_RANGE, min, max, NULL);
2735 return;
2738 /* If we have an unsigned MULT_EXPR with two VR_ANTI_RANGEs,
2739 drop to VR_VARYING. It would take more effort to compute a
2740 precise range for such a case. For example, if we have
2741 op0 == 65536 and op1 == 65536 with their ranges both being
2742 ~[0,0] on a 32-bit machine, we would have op0 * op1 == 0, so
2743 we cannot claim that the product is in ~[0,0]. Note that we
2744 are guaranteed to have vr0.type == vr1.type at this
2745 point. */
2746 if (vr0.type == VR_ANTI_RANGE
2747 && !TYPE_OVERFLOW_UNDEFINED (expr_type))
2749 set_value_range_to_varying (vr);
2750 return;
2753 extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1);
2754 return;
2756 else if (code == RSHIFT_EXPR
2757 || code == LSHIFT_EXPR)
2759 /* If we have a RSHIFT_EXPR with any shift values outside [0..prec-1],
2760 then drop to VR_VARYING. Outside of this range we get undefined
2761 behavior from the shift operation. We cannot even trust
2762 SHIFT_COUNT_TRUNCATED at this stage, because that applies to rtl
2763 shifts, and the operation at the tree level may be widened. */
2764 if (range_int_cst_p (&vr1)
2765 && compare_tree_int (vr1.min, 0) >= 0
2766 && compare_tree_int (vr1.max, TYPE_PRECISION (expr_type)) == -1)
2768 if (code == RSHIFT_EXPR)
2770 extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1);
2771 return;
2773 /* We can map lshifts by constants to MULT_EXPR handling. */
2774 else if (code == LSHIFT_EXPR
2775 && range_int_cst_singleton_p (&vr1))
2777 bool saved_flag_wrapv;
2778 value_range_t vr1p = VR_INITIALIZER;
2779 vr1p.type = VR_RANGE;
2780 vr1p.min
2781 = double_int_to_tree (expr_type,
2782 double_int_one
2783 .llshift (TREE_INT_CST_LOW (vr1.min),
2784 TYPE_PRECISION (expr_type)));
2785 vr1p.max = vr1p.min;
2786 /* We have to use a wrapping multiply though as signed overflow
2787 on lshifts is implementation defined in C89. */
2788 saved_flag_wrapv = flag_wrapv;
2789 flag_wrapv = 1;
2790 extract_range_from_binary_expr_1 (vr, MULT_EXPR, expr_type,
2791 &vr0, &vr1p);
2792 flag_wrapv = saved_flag_wrapv;
2793 return;
2795 else if (code == LSHIFT_EXPR
2796 && range_int_cst_p (&vr0))
2798 int prec = TYPE_PRECISION (expr_type);
2799 int overflow_pos = prec;
2800 int bound_shift;
2801 double_int bound, complement, low_bound, high_bound;
2802 bool uns = TYPE_UNSIGNED (expr_type);
2803 bool in_bounds = false;
2805 if (!uns)
2806 overflow_pos -= 1;
2808 bound_shift = overflow_pos - TREE_INT_CST_LOW (vr1.max);
2809 /* If bound_shift == HOST_BITS_PER_DOUBLE_INT, the llshift can
2810 overflow. However, for that to happen, vr1.max needs to be
2811 zero, which means vr1 is a singleton range of zero, which
2812 means it should be handled by the previous LSHIFT_EXPR
2813 if-clause. */
2814 bound = double_int_one.llshift (bound_shift, prec);
2815 complement = ~(bound - double_int_one);
2817 if (uns)
2819 low_bound = bound;
2820 high_bound = complement.zext (prec);
2821 if (tree_to_double_int (vr0.max).ult (low_bound))
2823 /* [5, 6] << [1, 2] == [10, 24]. */
2824 /* We're shifting out only zeroes, the value increases
2825 monotonically. */
2826 in_bounds = true;
2828 else if (high_bound.ult (tree_to_double_int (vr0.min)))
2830 /* [0xffffff00, 0xffffffff] << [1, 2]
2831 == [0xfffffc00, 0xfffffffe]. */
2832 /* We're shifting out only ones, the value decreases
2833 monotonically. */
2834 in_bounds = true;
2837 else
2839 /* [-1, 1] << [1, 2] == [-4, 4]. */
2840 low_bound = complement.sext (prec);
2841 high_bound = bound;
2842 if (tree_to_double_int (vr0.max).slt (high_bound)
2843 && low_bound.slt (tree_to_double_int (vr0.min)))
2845 /* For non-negative numbers, we're shifting out only
2846 zeroes, the value increases monotonically.
2847 For negative numbers, we're shifting out only ones, the
2848 value decreases monotomically. */
2849 in_bounds = true;
2853 if (in_bounds)
2855 extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1);
2856 return;
2860 set_value_range_to_varying (vr);
2861 return;
2863 else if (code == TRUNC_DIV_EXPR
2864 || code == FLOOR_DIV_EXPR
2865 || code == CEIL_DIV_EXPR
2866 || code == EXACT_DIV_EXPR
2867 || code == ROUND_DIV_EXPR)
2869 if (vr0.type != VR_RANGE || symbolic_range_p (&vr0))
2871 /* For division, if op1 has VR_RANGE but op0 does not, something
2872 can be deduced just from that range. Say [min, max] / [4, max]
2873 gives [min / 4, max / 4] range. */
2874 if (vr1.type == VR_RANGE
2875 && !symbolic_range_p (&vr1)
2876 && range_includes_zero_p (vr1.min, vr1.max) == 0)
2878 vr0.type = type = VR_RANGE;
2879 vr0.min = vrp_val_min (expr_type);
2880 vr0.max = vrp_val_max (expr_type);
2882 else
2884 set_value_range_to_varying (vr);
2885 return;
2889 /* For divisions, if flag_non_call_exceptions is true, we must
2890 not eliminate a division by zero. */
2891 if (cfun->can_throw_non_call_exceptions
2892 && (vr1.type != VR_RANGE
2893 || range_includes_zero_p (vr1.min, vr1.max) != 0))
2895 set_value_range_to_varying (vr);
2896 return;
2899 /* For divisions, if op0 is VR_RANGE, we can deduce a range
2900 even if op1 is VR_VARYING, VR_ANTI_RANGE, symbolic or can
2901 include 0. */
2902 if (vr0.type == VR_RANGE
2903 && (vr1.type != VR_RANGE
2904 || range_includes_zero_p (vr1.min, vr1.max) != 0))
2906 tree zero = build_int_cst (TREE_TYPE (vr0.min), 0);
2907 int cmp;
2909 min = NULL_TREE;
2910 max = NULL_TREE;
2911 if (TYPE_UNSIGNED (expr_type)
2912 || value_range_nonnegative_p (&vr1))
2914 /* For unsigned division or when divisor is known
2915 to be non-negative, the range has to cover
2916 all numbers from 0 to max for positive max
2917 and all numbers from min to 0 for negative min. */
2918 cmp = compare_values (vr0.max, zero);
2919 if (cmp == -1)
2920 max = zero;
2921 else if (cmp == 0 || cmp == 1)
2922 max = vr0.max;
2923 else
2924 type = VR_VARYING;
2925 cmp = compare_values (vr0.min, zero);
2926 if (cmp == 1)
2927 min = zero;
2928 else if (cmp == 0 || cmp == -1)
2929 min = vr0.min;
2930 else
2931 type = VR_VARYING;
2933 else
2935 /* Otherwise the range is -max .. max or min .. -min
2936 depending on which bound is bigger in absolute value,
2937 as the division can change the sign. */
2938 abs_extent_range (vr, vr0.min, vr0.max);
2939 return;
2941 if (type == VR_VARYING)
2943 set_value_range_to_varying (vr);
2944 return;
2947 else
2949 extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1);
2950 return;
2953 else if (code == TRUNC_MOD_EXPR)
2955 if (vr1.type != VR_RANGE
2956 || range_includes_zero_p (vr1.min, vr1.max) != 0
2957 || vrp_val_is_min (vr1.min))
2959 set_value_range_to_varying (vr);
2960 return;
2962 type = VR_RANGE;
2963 /* Compute MAX <|vr1.min|, |vr1.max|> - 1. */
2964 max = fold_unary_to_constant (ABS_EXPR, expr_type, vr1.min);
2965 if (tree_int_cst_lt (max, vr1.max))
2966 max = vr1.max;
2967 max = int_const_binop (MINUS_EXPR, max, integer_one_node);
2968 /* If the dividend is non-negative the modulus will be
2969 non-negative as well. */
2970 if (TYPE_UNSIGNED (expr_type)
2971 || value_range_nonnegative_p (&vr0))
2972 min = build_int_cst (TREE_TYPE (max), 0);
2973 else
2974 min = fold_unary_to_constant (NEGATE_EXPR, expr_type, max);
2976 else if (code == BIT_AND_EXPR || code == BIT_IOR_EXPR || code == BIT_XOR_EXPR)
2978 bool int_cst_range0, int_cst_range1;
2979 double_int may_be_nonzero0, may_be_nonzero1;
2980 double_int must_be_nonzero0, must_be_nonzero1;
2982 int_cst_range0 = zero_nonzero_bits_from_vr (&vr0, &may_be_nonzero0,
2983 &must_be_nonzero0);
2984 int_cst_range1 = zero_nonzero_bits_from_vr (&vr1, &may_be_nonzero1,
2985 &must_be_nonzero1);
2987 type = VR_RANGE;
2988 if (code == BIT_AND_EXPR)
2990 double_int dmax;
2991 min = double_int_to_tree (expr_type,
2992 must_be_nonzero0 & must_be_nonzero1);
2993 dmax = may_be_nonzero0 & may_be_nonzero1;
2994 /* If both input ranges contain only negative values we can
2995 truncate the result range maximum to the minimum of the
2996 input range maxima. */
2997 if (int_cst_range0 && int_cst_range1
2998 && tree_int_cst_sgn (vr0.max) < 0
2999 && tree_int_cst_sgn (vr1.max) < 0)
3001 dmax = dmax.min (tree_to_double_int (vr0.max),
3002 TYPE_UNSIGNED (expr_type));
3003 dmax = dmax.min (tree_to_double_int (vr1.max),
3004 TYPE_UNSIGNED (expr_type));
3006 /* If either input range contains only non-negative values
3007 we can truncate the result range maximum to the respective
3008 maximum of the input range. */
3009 if (int_cst_range0 && tree_int_cst_sgn (vr0.min) >= 0)
3010 dmax = dmax.min (tree_to_double_int (vr0.max),
3011 TYPE_UNSIGNED (expr_type));
3012 if (int_cst_range1 && tree_int_cst_sgn (vr1.min) >= 0)
3013 dmax = dmax.min (tree_to_double_int (vr1.max),
3014 TYPE_UNSIGNED (expr_type));
3015 max = double_int_to_tree (expr_type, dmax);
3017 else if (code == BIT_IOR_EXPR)
3019 double_int dmin;
3020 max = double_int_to_tree (expr_type,
3021 may_be_nonzero0 | may_be_nonzero1);
3022 dmin = must_be_nonzero0 | must_be_nonzero1;
3023 /* If the input ranges contain only positive values we can
3024 truncate the minimum of the result range to the maximum
3025 of the input range minima. */
3026 if (int_cst_range0 && int_cst_range1
3027 && tree_int_cst_sgn (vr0.min) >= 0
3028 && tree_int_cst_sgn (vr1.min) >= 0)
3030 dmin = dmin.max (tree_to_double_int (vr0.min),
3031 TYPE_UNSIGNED (expr_type));
3032 dmin = dmin.max (tree_to_double_int (vr1.min),
3033 TYPE_UNSIGNED (expr_type));
3035 /* If either input range contains only negative values
3036 we can truncate the minimum of the result range to the
3037 respective minimum range. */
3038 if (int_cst_range0 && tree_int_cst_sgn (vr0.max) < 0)
3039 dmin = dmin.max (tree_to_double_int (vr0.min),
3040 TYPE_UNSIGNED (expr_type));
3041 if (int_cst_range1 && tree_int_cst_sgn (vr1.max) < 0)
3042 dmin = dmin.max (tree_to_double_int (vr1.min),
3043 TYPE_UNSIGNED (expr_type));
3044 min = double_int_to_tree (expr_type, dmin);
3046 else if (code == BIT_XOR_EXPR)
3048 double_int result_zero_bits, result_one_bits;
3049 result_zero_bits = (must_be_nonzero0 & must_be_nonzero1)
3050 | ~(may_be_nonzero0 | may_be_nonzero1);
3051 result_one_bits = must_be_nonzero0.and_not (may_be_nonzero1)
3052 | must_be_nonzero1.and_not (may_be_nonzero0);
3053 max = double_int_to_tree (expr_type, ~result_zero_bits);
3054 min = double_int_to_tree (expr_type, result_one_bits);
3055 /* If the range has all positive or all negative values the
3056 result is better than VARYING. */
3057 if (tree_int_cst_sgn (min) < 0
3058 || tree_int_cst_sgn (max) >= 0)
3060 else
3061 max = min = NULL_TREE;
3064 else
3065 gcc_unreachable ();
3067 /* If either MIN or MAX overflowed, then set the resulting range to
3068 VARYING. But we do accept an overflow infinity
3069 representation. */
3070 if (min == NULL_TREE
3071 || !is_gimple_min_invariant (min)
3072 || (TREE_OVERFLOW (min) && !is_overflow_infinity (min))
3073 || max == NULL_TREE
3074 || !is_gimple_min_invariant (max)
3075 || (TREE_OVERFLOW (max) && !is_overflow_infinity (max)))
3077 set_value_range_to_varying (vr);
3078 return;
3081 /* We punt if:
3082 1) [-INF, +INF]
3083 2) [-INF, +-INF(OVF)]
3084 3) [+-INF(OVF), +INF]
3085 4) [+-INF(OVF), +-INF(OVF)]
3086 We learn nothing when we have INF and INF(OVF) on both sides.
3087 Note that we do accept [-INF, -INF] and [+INF, +INF] without
3088 overflow. */
3089 if ((vrp_val_is_min (min) || is_overflow_infinity (min))
3090 && (vrp_val_is_max (max) || is_overflow_infinity (max)))
3092 set_value_range_to_varying (vr);
3093 return;
3096 cmp = compare_values (min, max);
3097 if (cmp == -2 || cmp == 1)
3099 /* If the new range has its limits swapped around (MIN > MAX),
3100 then the operation caused one of them to wrap around, mark
3101 the new range VARYING. */
3102 set_value_range_to_varying (vr);
3104 else
3105 set_value_range (vr, type, min, max, NULL);
3108 /* Extract range information from a binary expression OP0 CODE OP1 based on
3109 the ranges of each of its operands with resulting type EXPR_TYPE.
3110 The resulting range is stored in *VR. */
3112 static void
3113 extract_range_from_binary_expr (value_range_t *vr,
3114 enum tree_code code,
3115 tree expr_type, tree op0, tree op1)
3117 value_range_t vr0 = VR_INITIALIZER;
3118 value_range_t vr1 = VR_INITIALIZER;
3120 /* Get value ranges for each operand. For constant operands, create
3121 a new value range with the operand to simplify processing. */
3122 if (TREE_CODE (op0) == SSA_NAME)
3123 vr0 = *(get_value_range (op0));
3124 else if (is_gimple_min_invariant (op0))
3125 set_value_range_to_value (&vr0, op0, NULL);
3126 else
3127 set_value_range_to_varying (&vr0);
3129 if (TREE_CODE (op1) == SSA_NAME)
3130 vr1 = *(get_value_range (op1));
3131 else if (is_gimple_min_invariant (op1))
3132 set_value_range_to_value (&vr1, op1, NULL);
3133 else
3134 set_value_range_to_varying (&vr1);
3136 extract_range_from_binary_expr_1 (vr, code, expr_type, &vr0, &vr1);
3139 /* Extract range information from a unary operation CODE based on
3140 the range of its operand *VR0 with type OP0_TYPE with resulting type TYPE.
3141 The The resulting range is stored in *VR. */
3143 static void
3144 extract_range_from_unary_expr_1 (value_range_t *vr,
3145 enum tree_code code, tree type,
3146 value_range_t *vr0_, tree op0_type)
3148 value_range_t vr0 = *vr0_, vrtem0 = VR_INITIALIZER, vrtem1 = VR_INITIALIZER;
3150 /* VRP only operates on integral and pointer types. */
3151 if (!(INTEGRAL_TYPE_P (op0_type)
3152 || POINTER_TYPE_P (op0_type))
3153 || !(INTEGRAL_TYPE_P (type)
3154 || POINTER_TYPE_P (type)))
3156 set_value_range_to_varying (vr);
3157 return;
3160 /* If VR0 is UNDEFINED, so is the result. */
3161 if (vr0.type == VR_UNDEFINED)
3163 set_value_range_to_undefined (vr);
3164 return;
3167 /* Handle operations that we express in terms of others. */
3168 if (code == PAREN_EXPR)
3170 /* PAREN_EXPR is a simple copy. */
3171 copy_value_range (vr, &vr0);
3172 return;
3174 else if (code == NEGATE_EXPR)
3176 /* -X is simply 0 - X, so re-use existing code that also handles
3177 anti-ranges fine. */
3178 value_range_t zero = VR_INITIALIZER;
3179 set_value_range_to_value (&zero, build_int_cst (type, 0), NULL);
3180 extract_range_from_binary_expr_1 (vr, MINUS_EXPR, type, &zero, &vr0);
3181 return;
3183 else if (code == BIT_NOT_EXPR)
3185 /* ~X is simply -1 - X, so re-use existing code that also handles
3186 anti-ranges fine. */
3187 value_range_t minusone = VR_INITIALIZER;
3188 set_value_range_to_value (&minusone, build_int_cst (type, -1), NULL);
3189 extract_range_from_binary_expr_1 (vr, MINUS_EXPR,
3190 type, &minusone, &vr0);
3191 return;
3194 /* Now canonicalize anti-ranges to ranges when they are not symbolic
3195 and express op ~[] as (op []') U (op []''). */
3196 if (vr0.type == VR_ANTI_RANGE
3197 && ranges_from_anti_range (&vr0, &vrtem0, &vrtem1))
3199 extract_range_from_unary_expr_1 (vr, code, type, &vrtem0, op0_type);
3200 if (vrtem1.type != VR_UNDEFINED)
3202 value_range_t vrres = VR_INITIALIZER;
3203 extract_range_from_unary_expr_1 (&vrres, code, type,
3204 &vrtem1, op0_type);
3205 vrp_meet (vr, &vrres);
3207 return;
3210 if (CONVERT_EXPR_CODE_P (code))
3212 tree inner_type = op0_type;
3213 tree outer_type = type;
3215 /* If the expression evaluates to a pointer, we are only interested in
3216 determining if it evaluates to NULL [0, 0] or non-NULL (~[0, 0]). */
3217 if (POINTER_TYPE_P (type))
3219 if (range_is_nonnull (&vr0))
3220 set_value_range_to_nonnull (vr, type);
3221 else if (range_is_null (&vr0))
3222 set_value_range_to_null (vr, type);
3223 else
3224 set_value_range_to_varying (vr);
3225 return;
3228 /* If VR0 is varying and we increase the type precision, assume
3229 a full range for the following transformation. */
3230 if (vr0.type == VR_VARYING
3231 && INTEGRAL_TYPE_P (inner_type)
3232 && TYPE_PRECISION (inner_type) < TYPE_PRECISION (outer_type))
3234 vr0.type = VR_RANGE;
3235 vr0.min = TYPE_MIN_VALUE (inner_type);
3236 vr0.max = TYPE_MAX_VALUE (inner_type);
3239 /* If VR0 is a constant range or anti-range and the conversion is
3240 not truncating we can convert the min and max values and
3241 canonicalize the resulting range. Otherwise we can do the
3242 conversion if the size of the range is less than what the
3243 precision of the target type can represent and the range is
3244 not an anti-range. */
3245 if ((vr0.type == VR_RANGE
3246 || vr0.type == VR_ANTI_RANGE)
3247 && TREE_CODE (vr0.min) == INTEGER_CST
3248 && TREE_CODE (vr0.max) == INTEGER_CST
3249 && (!is_overflow_infinity (vr0.min)
3250 || (vr0.type == VR_RANGE
3251 && TYPE_PRECISION (outer_type) > TYPE_PRECISION (inner_type)
3252 && needs_overflow_infinity (outer_type)
3253 && supports_overflow_infinity (outer_type)))
3254 && (!is_overflow_infinity (vr0.max)
3255 || (vr0.type == VR_RANGE
3256 && TYPE_PRECISION (outer_type) > TYPE_PRECISION (inner_type)
3257 && needs_overflow_infinity (outer_type)
3258 && supports_overflow_infinity (outer_type)))
3259 && (TYPE_PRECISION (outer_type) >= TYPE_PRECISION (inner_type)
3260 || (vr0.type == VR_RANGE
3261 && integer_zerop (int_const_binop (RSHIFT_EXPR,
3262 int_const_binop (MINUS_EXPR, vr0.max, vr0.min),
3263 size_int (TYPE_PRECISION (outer_type)))))))
3265 tree new_min, new_max;
3266 if (is_overflow_infinity (vr0.min))
3267 new_min = negative_overflow_infinity (outer_type);
3268 else
3269 new_min = force_fit_type_double (outer_type,
3270 tree_to_double_int (vr0.min),
3271 0, false);
3272 if (is_overflow_infinity (vr0.max))
3273 new_max = positive_overflow_infinity (outer_type);
3274 else
3275 new_max = force_fit_type_double (outer_type,
3276 tree_to_double_int (vr0.max),
3277 0, false);
3278 set_and_canonicalize_value_range (vr, vr0.type,
3279 new_min, new_max, NULL);
3280 return;
3283 set_value_range_to_varying (vr);
3284 return;
3286 else if (code == ABS_EXPR)
3288 tree min, max;
3289 int cmp;
3291 /* Pass through vr0 in the easy cases. */
3292 if (TYPE_UNSIGNED (type)
3293 || value_range_nonnegative_p (&vr0))
3295 copy_value_range (vr, &vr0);
3296 return;
3299 /* For the remaining varying or symbolic ranges we can't do anything
3300 useful. */
3301 if (vr0.type == VR_VARYING
3302 || symbolic_range_p (&vr0))
3304 set_value_range_to_varying (vr);
3305 return;
3308 /* -TYPE_MIN_VALUE = TYPE_MIN_VALUE with flag_wrapv so we can't get a
3309 useful range. */
3310 if (!TYPE_OVERFLOW_UNDEFINED (type)
3311 && ((vr0.type == VR_RANGE
3312 && vrp_val_is_min (vr0.min))
3313 || (vr0.type == VR_ANTI_RANGE
3314 && !vrp_val_is_min (vr0.min))))
3316 set_value_range_to_varying (vr);
3317 return;
3320 /* ABS_EXPR may flip the range around, if the original range
3321 included negative values. */
3322 if (is_overflow_infinity (vr0.min))
3323 min = positive_overflow_infinity (type);
3324 else if (!vrp_val_is_min (vr0.min))
3325 min = fold_unary_to_constant (code, type, vr0.min);
3326 else if (!needs_overflow_infinity (type))
3327 min = TYPE_MAX_VALUE (type);
3328 else if (supports_overflow_infinity (type))
3329 min = positive_overflow_infinity (type);
3330 else
3332 set_value_range_to_varying (vr);
3333 return;
3336 if (is_overflow_infinity (vr0.max))
3337 max = positive_overflow_infinity (type);
3338 else if (!vrp_val_is_min (vr0.max))
3339 max = fold_unary_to_constant (code, type, vr0.max);
3340 else if (!needs_overflow_infinity (type))
3341 max = TYPE_MAX_VALUE (type);
3342 else if (supports_overflow_infinity (type)
3343 /* We shouldn't generate [+INF, +INF] as set_value_range
3344 doesn't like this and ICEs. */
3345 && !is_positive_overflow_infinity (min))
3346 max = positive_overflow_infinity (type);
3347 else
3349 set_value_range_to_varying (vr);
3350 return;
3353 cmp = compare_values (min, max);
3355 /* If a VR_ANTI_RANGEs contains zero, then we have
3356 ~[-INF, min(MIN, MAX)]. */
3357 if (vr0.type == VR_ANTI_RANGE)
3359 if (range_includes_zero_p (vr0.min, vr0.max) == 1)
3361 /* Take the lower of the two values. */
3362 if (cmp != 1)
3363 max = min;
3365 /* Create ~[-INF, min (abs(MIN), abs(MAX))]
3366 or ~[-INF + 1, min (abs(MIN), abs(MAX))] when
3367 flag_wrapv is set and the original anti-range doesn't include
3368 TYPE_MIN_VALUE, remember -TYPE_MIN_VALUE = TYPE_MIN_VALUE. */
3369 if (TYPE_OVERFLOW_WRAPS (type))
3371 tree type_min_value = TYPE_MIN_VALUE (type);
3373 min = (vr0.min != type_min_value
3374 ? int_const_binop (PLUS_EXPR, type_min_value,
3375 integer_one_node)
3376 : type_min_value);
3378 else
3380 if (overflow_infinity_range_p (&vr0))
3381 min = negative_overflow_infinity (type);
3382 else
3383 min = TYPE_MIN_VALUE (type);
3386 else
3388 /* All else has failed, so create the range [0, INF], even for
3389 flag_wrapv since TYPE_MIN_VALUE is in the original
3390 anti-range. */
3391 vr0.type = VR_RANGE;
3392 min = build_int_cst (type, 0);
3393 if (needs_overflow_infinity (type))
3395 if (supports_overflow_infinity (type))
3396 max = positive_overflow_infinity (type);
3397 else
3399 set_value_range_to_varying (vr);
3400 return;
3403 else
3404 max = TYPE_MAX_VALUE (type);
3408 /* If the range contains zero then we know that the minimum value in the
3409 range will be zero. */
3410 else if (range_includes_zero_p (vr0.min, vr0.max) == 1)
3412 if (cmp == 1)
3413 max = min;
3414 min = build_int_cst (type, 0);
3416 else
3418 /* If the range was reversed, swap MIN and MAX. */
3419 if (cmp == 1)
3421 tree t = min;
3422 min = max;
3423 max = t;
3427 cmp = compare_values (min, max);
3428 if (cmp == -2 || cmp == 1)
3430 /* If the new range has its limits swapped around (MIN > MAX),
3431 then the operation caused one of them to wrap around, mark
3432 the new range VARYING. */
3433 set_value_range_to_varying (vr);
3435 else
3436 set_value_range (vr, vr0.type, min, max, NULL);
3437 return;
3440 /* For unhandled operations fall back to varying. */
3441 set_value_range_to_varying (vr);
3442 return;
3446 /* Extract range information from a unary expression CODE OP0 based on
3447 the range of its operand with resulting type TYPE.
3448 The resulting range is stored in *VR. */
3450 static void
3451 extract_range_from_unary_expr (value_range_t *vr, enum tree_code code,
3452 tree type, tree op0)
3454 value_range_t vr0 = VR_INITIALIZER;
3456 /* Get value ranges for the operand. For constant operands, create
3457 a new value range with the operand to simplify processing. */
3458 if (TREE_CODE (op0) == SSA_NAME)
3459 vr0 = *(get_value_range (op0));
3460 else if (is_gimple_min_invariant (op0))
3461 set_value_range_to_value (&vr0, op0, NULL);
3462 else
3463 set_value_range_to_varying (&vr0);
3465 extract_range_from_unary_expr_1 (vr, code, type, &vr0, TREE_TYPE (op0));
3469 /* Extract range information from a conditional expression STMT based on
3470 the ranges of each of its operands and the expression code. */
3472 static void
3473 extract_range_from_cond_expr (value_range_t *vr, gimple stmt)
3475 tree op0, op1;
3476 value_range_t vr0 = VR_INITIALIZER;
3477 value_range_t vr1 = VR_INITIALIZER;
3479 /* Get value ranges for each operand. For constant operands, create
3480 a new value range with the operand to simplify processing. */
3481 op0 = gimple_assign_rhs2 (stmt);
3482 if (TREE_CODE (op0) == SSA_NAME)
3483 vr0 = *(get_value_range (op0));
3484 else if (is_gimple_min_invariant (op0))
3485 set_value_range_to_value (&vr0, op0, NULL);
3486 else
3487 set_value_range_to_varying (&vr0);
3489 op1 = gimple_assign_rhs3 (stmt);
3490 if (TREE_CODE (op1) == SSA_NAME)
3491 vr1 = *(get_value_range (op1));
3492 else if (is_gimple_min_invariant (op1))
3493 set_value_range_to_value (&vr1, op1, NULL);
3494 else
3495 set_value_range_to_varying (&vr1);
3497 /* The resulting value range is the union of the operand ranges */
3498 copy_value_range (vr, &vr0);
3499 vrp_meet (vr, &vr1);
3503 /* Extract range information from a comparison expression EXPR based
3504 on the range of its operand and the expression code. */
3506 static void
3507 extract_range_from_comparison (value_range_t *vr, enum tree_code code,
3508 tree type, tree op0, tree op1)
3510 bool sop = false;
3511 tree val;
3513 val = vrp_evaluate_conditional_warnv_with_ops (code, op0, op1, false, &sop,
3514 NULL);
3516 /* A disadvantage of using a special infinity as an overflow
3517 representation is that we lose the ability to record overflow
3518 when we don't have an infinity. So we have to ignore a result
3519 which relies on overflow. */
3521 if (val && !is_overflow_infinity (val) && !sop)
3523 /* Since this expression was found on the RHS of an assignment,
3524 its type may be different from _Bool. Convert VAL to EXPR's
3525 type. */
3526 val = fold_convert (type, val);
3527 if (is_gimple_min_invariant (val))
3528 set_value_range_to_value (vr, val, vr->equiv);
3529 else
3530 set_value_range (vr, VR_RANGE, val, val, vr->equiv);
3532 else
3533 /* The result of a comparison is always true or false. */
3534 set_value_range_to_truthvalue (vr, type);
3537 /* Try to derive a nonnegative or nonzero range out of STMT relying
3538 primarily on generic routines in fold in conjunction with range data.
3539 Store the result in *VR */
3541 static void
3542 extract_range_basic (value_range_t *vr, gimple stmt)
3544 bool sop = false;
3545 tree type = gimple_expr_type (stmt);
3547 if (INTEGRAL_TYPE_P (type)
3548 && gimple_stmt_nonnegative_warnv_p (stmt, &sop))
3549 set_value_range_to_nonnegative (vr, type,
3550 sop || stmt_overflow_infinity (stmt));
3551 else if (vrp_stmt_computes_nonzero (stmt, &sop)
3552 && !sop)
3553 set_value_range_to_nonnull (vr, type);
3554 else
3555 set_value_range_to_varying (vr);
3559 /* Try to compute a useful range out of assignment STMT and store it
3560 in *VR. */
3562 static void
3563 extract_range_from_assignment (value_range_t *vr, gimple stmt)
3565 enum tree_code code = gimple_assign_rhs_code (stmt);
3567 if (code == ASSERT_EXPR)
3568 extract_range_from_assert (vr, gimple_assign_rhs1 (stmt));
3569 else if (code == SSA_NAME)
3570 extract_range_from_ssa_name (vr, gimple_assign_rhs1 (stmt));
3571 else if (TREE_CODE_CLASS (code) == tcc_binary)
3572 extract_range_from_binary_expr (vr, gimple_assign_rhs_code (stmt),
3573 gimple_expr_type (stmt),
3574 gimple_assign_rhs1 (stmt),
3575 gimple_assign_rhs2 (stmt));
3576 else if (TREE_CODE_CLASS (code) == tcc_unary)
3577 extract_range_from_unary_expr (vr, gimple_assign_rhs_code (stmt),
3578 gimple_expr_type (stmt),
3579 gimple_assign_rhs1 (stmt));
3580 else if (code == COND_EXPR)
3581 extract_range_from_cond_expr (vr, stmt);
3582 else if (TREE_CODE_CLASS (code) == tcc_comparison)
3583 extract_range_from_comparison (vr, gimple_assign_rhs_code (stmt),
3584 gimple_expr_type (stmt),
3585 gimple_assign_rhs1 (stmt),
3586 gimple_assign_rhs2 (stmt));
3587 else if (get_gimple_rhs_class (code) == GIMPLE_SINGLE_RHS
3588 && is_gimple_min_invariant (gimple_assign_rhs1 (stmt)))
3589 set_value_range_to_value (vr, gimple_assign_rhs1 (stmt), NULL);
3590 else
3591 set_value_range_to_varying (vr);
3593 if (vr->type == VR_VARYING)
3594 extract_range_basic (vr, stmt);
3597 /* Given a range VR, a LOOP and a variable VAR, determine whether it
3598 would be profitable to adjust VR using scalar evolution information
3599 for VAR. If so, update VR with the new limits. */
3601 static void
3602 adjust_range_with_scev (value_range_t *vr, struct loop *loop,
3603 gimple stmt, tree var)
3605 tree init, step, chrec, tmin, tmax, min, max, type, tem;
3606 enum ev_direction dir;
3608 /* TODO. Don't adjust anti-ranges. An anti-range may provide
3609 better opportunities than a regular range, but I'm not sure. */
3610 if (vr->type == VR_ANTI_RANGE)
3611 return;
3613 chrec = instantiate_parameters (loop, analyze_scalar_evolution (loop, var));
3615 /* Like in PR19590, scev can return a constant function. */
3616 if (is_gimple_min_invariant (chrec))
3618 set_value_range_to_value (vr, chrec, vr->equiv);
3619 return;
3622 if (TREE_CODE (chrec) != POLYNOMIAL_CHREC)
3623 return;
3625 init = initial_condition_in_loop_num (chrec, loop->num);
3626 tem = op_with_constant_singleton_value_range (init);
3627 if (tem)
3628 init = tem;
3629 step = evolution_part_in_loop_num (chrec, loop->num);
3630 tem = op_with_constant_singleton_value_range (step);
3631 if (tem)
3632 step = tem;
3634 /* If STEP is symbolic, we can't know whether INIT will be the
3635 minimum or maximum value in the range. Also, unless INIT is
3636 a simple expression, compare_values and possibly other functions
3637 in tree-vrp won't be able to handle it. */
3638 if (step == NULL_TREE
3639 || !is_gimple_min_invariant (step)
3640 || !valid_value_p (init))
3641 return;
3643 dir = scev_direction (chrec);
3644 if (/* Do not adjust ranges if we do not know whether the iv increases
3645 or decreases, ... */
3646 dir == EV_DIR_UNKNOWN
3647 /* ... or if it may wrap. */
3648 || scev_probably_wraps_p (init, step, stmt, get_chrec_loop (chrec),
3649 true))
3650 return;
3652 /* We use TYPE_MIN_VALUE and TYPE_MAX_VALUE here instead of
3653 negative_overflow_infinity and positive_overflow_infinity,
3654 because we have concluded that the loop probably does not
3655 wrap. */
3657 type = TREE_TYPE (var);
3658 if (POINTER_TYPE_P (type) || !TYPE_MIN_VALUE (type))
3659 tmin = lower_bound_in_type (type, type);
3660 else
3661 tmin = TYPE_MIN_VALUE (type);
3662 if (POINTER_TYPE_P (type) || !TYPE_MAX_VALUE (type))
3663 tmax = upper_bound_in_type (type, type);
3664 else
3665 tmax = TYPE_MAX_VALUE (type);
3667 /* Try to use estimated number of iterations for the loop to constrain the
3668 final value in the evolution. */
3669 if (TREE_CODE (step) == INTEGER_CST
3670 && is_gimple_val (init)
3671 && (TREE_CODE (init) != SSA_NAME
3672 || get_value_range (init)->type == VR_RANGE))
3674 double_int nit;
3676 /* We are only entering here for loop header PHI nodes, so using
3677 the number of latch executions is the correct thing to use. */
3678 if (max_loop_iterations (loop, &nit))
3680 value_range_t maxvr = VR_INITIALIZER;
3681 double_int dtmp;
3682 bool unsigned_p = TYPE_UNSIGNED (TREE_TYPE (step));
3683 bool overflow = false;
3685 dtmp = tree_to_double_int (step)
3686 .mul_with_sign (nit, unsigned_p, &overflow);
3687 /* If the multiplication overflowed we can't do a meaningful
3688 adjustment. Likewise if the result doesn't fit in the type
3689 of the induction variable. For a signed type we have to
3690 check whether the result has the expected signedness which
3691 is that of the step as number of iterations is unsigned. */
3692 if (!overflow
3693 && double_int_fits_to_tree_p (TREE_TYPE (init), dtmp)
3694 && (unsigned_p
3695 || ((dtmp.high ^ TREE_INT_CST_HIGH (step)) >= 0)))
3697 tem = double_int_to_tree (TREE_TYPE (init), dtmp);
3698 extract_range_from_binary_expr (&maxvr, PLUS_EXPR,
3699 TREE_TYPE (init), init, tem);
3700 /* Likewise if the addition did. */
3701 if (maxvr.type == VR_RANGE)
3703 tmin = maxvr.min;
3704 tmax = maxvr.max;
3710 if (vr->type == VR_VARYING || vr->type == VR_UNDEFINED)
3712 min = tmin;
3713 max = tmax;
3715 /* For VARYING or UNDEFINED ranges, just about anything we get
3716 from scalar evolutions should be better. */
3718 if (dir == EV_DIR_DECREASES)
3719 max = init;
3720 else
3721 min = init;
3723 /* If we would create an invalid range, then just assume we
3724 know absolutely nothing. This may be over-conservative,
3725 but it's clearly safe, and should happen only in unreachable
3726 parts of code, or for invalid programs. */
3727 if (compare_values (min, max) == 1)
3728 return;
3730 set_value_range (vr, VR_RANGE, min, max, vr->equiv);
3732 else if (vr->type == VR_RANGE)
3734 min = vr->min;
3735 max = vr->max;
3737 if (dir == EV_DIR_DECREASES)
3739 /* INIT is the maximum value. If INIT is lower than VR->MAX
3740 but no smaller than VR->MIN, set VR->MAX to INIT. */
3741 if (compare_values (init, max) == -1)
3742 max = init;
3744 /* According to the loop information, the variable does not
3745 overflow. If we think it does, probably because of an
3746 overflow due to arithmetic on a different INF value,
3747 reset now. */
3748 if (is_negative_overflow_infinity (min)
3749 || compare_values (min, tmin) == -1)
3750 min = tmin;
3753 else
3755 /* If INIT is bigger than VR->MIN, set VR->MIN to INIT. */
3756 if (compare_values (init, min) == 1)
3757 min = init;
3759 if (is_positive_overflow_infinity (max)
3760 || compare_values (tmax, max) == -1)
3761 max = tmax;
3764 /* If we just created an invalid range with the minimum
3765 greater than the maximum, we fail conservatively.
3766 This should happen only in unreachable
3767 parts of code, or for invalid programs. */
3768 if (compare_values (min, max) == 1)
3769 return;
3771 set_value_range (vr, VR_RANGE, min, max, vr->equiv);
3775 /* Return true if VAR may overflow at STMT. This checks any available
3776 loop information to see if we can determine that VAR does not
3777 overflow. */
3779 static bool
3780 vrp_var_may_overflow (tree var, gimple stmt)
3782 struct loop *l;
3783 tree chrec, init, step;
3785 if (current_loops == NULL)
3786 return true;
3788 l = loop_containing_stmt (stmt);
3789 if (l == NULL
3790 || !loop_outer (l))
3791 return true;
3793 chrec = instantiate_parameters (l, analyze_scalar_evolution (l, var));
3794 if (TREE_CODE (chrec) != POLYNOMIAL_CHREC)
3795 return true;
3797 init = initial_condition_in_loop_num (chrec, l->num);
3798 step = evolution_part_in_loop_num (chrec, l->num);
3800 if (step == NULL_TREE
3801 || !is_gimple_min_invariant (step)
3802 || !valid_value_p (init))
3803 return true;
3805 /* If we get here, we know something useful about VAR based on the
3806 loop information. If it wraps, it may overflow. */
3808 if (scev_probably_wraps_p (init, step, stmt, get_chrec_loop (chrec),
3809 true))
3810 return true;
3812 if (dump_file && (dump_flags & TDF_DETAILS) != 0)
3814 print_generic_expr (dump_file, var, 0);
3815 fprintf (dump_file, ": loop information indicates does not overflow\n");
3818 return false;
3822 /* Given two numeric value ranges VR0, VR1 and a comparison code COMP:
3824 - Return BOOLEAN_TRUE_NODE if VR0 COMP VR1 always returns true for
3825 all the values in the ranges.
3827 - Return BOOLEAN_FALSE_NODE if the comparison always returns false.
3829 - Return NULL_TREE if it is not always possible to determine the
3830 value of the comparison.
3832 Also set *STRICT_OVERFLOW_P to indicate whether a range with an
3833 overflow infinity was used in the test. */
3836 static tree
3837 compare_ranges (enum tree_code comp, value_range_t *vr0, value_range_t *vr1,
3838 bool *strict_overflow_p)
3840 /* VARYING or UNDEFINED ranges cannot be compared. */
3841 if (vr0->type == VR_VARYING
3842 || vr0->type == VR_UNDEFINED
3843 || vr1->type == VR_VARYING
3844 || vr1->type == VR_UNDEFINED)
3845 return NULL_TREE;
3847 /* Anti-ranges need to be handled separately. */
3848 if (vr0->type == VR_ANTI_RANGE || vr1->type == VR_ANTI_RANGE)
3850 /* If both are anti-ranges, then we cannot compute any
3851 comparison. */
3852 if (vr0->type == VR_ANTI_RANGE && vr1->type == VR_ANTI_RANGE)
3853 return NULL_TREE;
3855 /* These comparisons are never statically computable. */
3856 if (comp == GT_EXPR
3857 || comp == GE_EXPR
3858 || comp == LT_EXPR
3859 || comp == LE_EXPR)
3860 return NULL_TREE;
3862 /* Equality can be computed only between a range and an
3863 anti-range. ~[VAL1, VAL2] == [VAL1, VAL2] is always false. */
3864 if (vr0->type == VR_RANGE)
3866 /* To simplify processing, make VR0 the anti-range. */
3867 value_range_t *tmp = vr0;
3868 vr0 = vr1;
3869 vr1 = tmp;
3872 gcc_assert (comp == NE_EXPR || comp == EQ_EXPR);
3874 if (compare_values_warnv (vr0->min, vr1->min, strict_overflow_p) == 0
3875 && compare_values_warnv (vr0->max, vr1->max, strict_overflow_p) == 0)
3876 return (comp == NE_EXPR) ? boolean_true_node : boolean_false_node;
3878 return NULL_TREE;
3881 if (!usable_range_p (vr0, strict_overflow_p)
3882 || !usable_range_p (vr1, strict_overflow_p))
3883 return NULL_TREE;
3885 /* Simplify processing. If COMP is GT_EXPR or GE_EXPR, switch the
3886 operands around and change the comparison code. */
3887 if (comp == GT_EXPR || comp == GE_EXPR)
3889 value_range_t *tmp;
3890 comp = (comp == GT_EXPR) ? LT_EXPR : LE_EXPR;
3891 tmp = vr0;
3892 vr0 = vr1;
3893 vr1 = tmp;
3896 if (comp == EQ_EXPR)
3898 /* Equality may only be computed if both ranges represent
3899 exactly one value. */
3900 if (compare_values_warnv (vr0->min, vr0->max, strict_overflow_p) == 0
3901 && compare_values_warnv (vr1->min, vr1->max, strict_overflow_p) == 0)
3903 int cmp_min = compare_values_warnv (vr0->min, vr1->min,
3904 strict_overflow_p);
3905 int cmp_max = compare_values_warnv (vr0->max, vr1->max,
3906 strict_overflow_p);
3907 if (cmp_min == 0 && cmp_max == 0)
3908 return boolean_true_node;
3909 else if (cmp_min != -2 && cmp_max != -2)
3910 return boolean_false_node;
3912 /* If [V0_MIN, V1_MAX] < [V1_MIN, V1_MAX] then V0 != V1. */
3913 else if (compare_values_warnv (vr0->min, vr1->max,
3914 strict_overflow_p) == 1
3915 || compare_values_warnv (vr1->min, vr0->max,
3916 strict_overflow_p) == 1)
3917 return boolean_false_node;
3919 return NULL_TREE;
3921 else if (comp == NE_EXPR)
3923 int cmp1, cmp2;
3925 /* If VR0 is completely to the left or completely to the right
3926 of VR1, they are always different. Notice that we need to
3927 make sure that both comparisons yield similar results to
3928 avoid comparing values that cannot be compared at
3929 compile-time. */
3930 cmp1 = compare_values_warnv (vr0->max, vr1->min, strict_overflow_p);
3931 cmp2 = compare_values_warnv (vr0->min, vr1->max, strict_overflow_p);
3932 if ((cmp1 == -1 && cmp2 == -1) || (cmp1 == 1 && cmp2 == 1))
3933 return boolean_true_node;
3935 /* If VR0 and VR1 represent a single value and are identical,
3936 return false. */
3937 else if (compare_values_warnv (vr0->min, vr0->max,
3938 strict_overflow_p) == 0
3939 && compare_values_warnv (vr1->min, vr1->max,
3940 strict_overflow_p) == 0
3941 && compare_values_warnv (vr0->min, vr1->min,
3942 strict_overflow_p) == 0
3943 && compare_values_warnv (vr0->max, vr1->max,
3944 strict_overflow_p) == 0)
3945 return boolean_false_node;
3947 /* Otherwise, they may or may not be different. */
3948 else
3949 return NULL_TREE;
3951 else if (comp == LT_EXPR || comp == LE_EXPR)
3953 int tst;
3955 /* If VR0 is to the left of VR1, return true. */
3956 tst = compare_values_warnv (vr0->max, vr1->min, strict_overflow_p);
3957 if ((comp == LT_EXPR && tst == -1)
3958 || (comp == LE_EXPR && (tst == -1 || tst == 0)))
3960 if (overflow_infinity_range_p (vr0)
3961 || overflow_infinity_range_p (vr1))
3962 *strict_overflow_p = true;
3963 return boolean_true_node;
3966 /* If VR0 is to the right of VR1, return false. */
3967 tst = compare_values_warnv (vr0->min, vr1->max, strict_overflow_p);
3968 if ((comp == LT_EXPR && (tst == 0 || tst == 1))
3969 || (comp == LE_EXPR && tst == 1))
3971 if (overflow_infinity_range_p (vr0)
3972 || overflow_infinity_range_p (vr1))
3973 *strict_overflow_p = true;
3974 return boolean_false_node;
3977 /* Otherwise, we don't know. */
3978 return NULL_TREE;
3981 gcc_unreachable ();
3985 /* Given a value range VR, a value VAL and a comparison code COMP, return
3986 BOOLEAN_TRUE_NODE if VR COMP VAL always returns true for all the
3987 values in VR. Return BOOLEAN_FALSE_NODE if the comparison
3988 always returns false. Return NULL_TREE if it is not always
3989 possible to determine the value of the comparison. Also set
3990 *STRICT_OVERFLOW_P to indicate whether a range with an overflow
3991 infinity was used in the test. */
3993 static tree
3994 compare_range_with_value (enum tree_code comp, value_range_t *vr, tree val,
3995 bool *strict_overflow_p)
3997 if (vr->type == VR_VARYING || vr->type == VR_UNDEFINED)
3998 return NULL_TREE;
4000 /* Anti-ranges need to be handled separately. */
4001 if (vr->type == VR_ANTI_RANGE)
4003 /* For anti-ranges, the only predicates that we can compute at
4004 compile time are equality and inequality. */
4005 if (comp == GT_EXPR
4006 || comp == GE_EXPR
4007 || comp == LT_EXPR
4008 || comp == LE_EXPR)
4009 return NULL_TREE;
4011 /* ~[VAL_1, VAL_2] OP VAL is known if VAL_1 <= VAL <= VAL_2. */
4012 if (value_inside_range (val, vr->min, vr->max) == 1)
4013 return (comp == NE_EXPR) ? boolean_true_node : boolean_false_node;
4015 return NULL_TREE;
4018 if (!usable_range_p (vr, strict_overflow_p))
4019 return NULL_TREE;
4021 if (comp == EQ_EXPR)
4023 /* EQ_EXPR may only be computed if VR represents exactly
4024 one value. */
4025 if (compare_values_warnv (vr->min, vr->max, strict_overflow_p) == 0)
4027 int cmp = compare_values_warnv (vr->min, val, strict_overflow_p);
4028 if (cmp == 0)
4029 return boolean_true_node;
4030 else if (cmp == -1 || cmp == 1 || cmp == 2)
4031 return boolean_false_node;
4033 else if (compare_values_warnv (val, vr->min, strict_overflow_p) == -1
4034 || compare_values_warnv (vr->max, val, strict_overflow_p) == -1)
4035 return boolean_false_node;
4037 return NULL_TREE;
4039 else if (comp == NE_EXPR)
4041 /* If VAL is not inside VR, then they are always different. */
4042 if (compare_values_warnv (vr->max, val, strict_overflow_p) == -1
4043 || compare_values_warnv (vr->min, val, strict_overflow_p) == 1)
4044 return boolean_true_node;
4046 /* If VR represents exactly one value equal to VAL, then return
4047 false. */
4048 if (compare_values_warnv (vr->min, vr->max, strict_overflow_p) == 0
4049 && compare_values_warnv (vr->min, val, strict_overflow_p) == 0)
4050 return boolean_false_node;
4052 /* Otherwise, they may or may not be different. */
4053 return NULL_TREE;
4055 else if (comp == LT_EXPR || comp == LE_EXPR)
4057 int tst;
4059 /* If VR is to the left of VAL, return true. */
4060 tst = compare_values_warnv (vr->max, val, strict_overflow_p);
4061 if ((comp == LT_EXPR && tst == -1)
4062 || (comp == LE_EXPR && (tst == -1 || tst == 0)))
4064 if (overflow_infinity_range_p (vr))
4065 *strict_overflow_p = true;
4066 return boolean_true_node;
4069 /* If VR is to the right of VAL, return false. */
4070 tst = compare_values_warnv (vr->min, val, strict_overflow_p);
4071 if ((comp == LT_EXPR && (tst == 0 || tst == 1))
4072 || (comp == LE_EXPR && tst == 1))
4074 if (overflow_infinity_range_p (vr))
4075 *strict_overflow_p = true;
4076 return boolean_false_node;
4079 /* Otherwise, we don't know. */
4080 return NULL_TREE;
4082 else if (comp == GT_EXPR || comp == GE_EXPR)
4084 int tst;
4086 /* If VR is to the right of VAL, return true. */
4087 tst = compare_values_warnv (vr->min, val, strict_overflow_p);
4088 if ((comp == GT_EXPR && tst == 1)
4089 || (comp == GE_EXPR && (tst == 0 || tst == 1)))
4091 if (overflow_infinity_range_p (vr))
4092 *strict_overflow_p = true;
4093 return boolean_true_node;
4096 /* If VR is to the left of VAL, return false. */
4097 tst = compare_values_warnv (vr->max, val, strict_overflow_p);
4098 if ((comp == GT_EXPR && (tst == -1 || tst == 0))
4099 || (comp == GE_EXPR && tst == -1))
4101 if (overflow_infinity_range_p (vr))
4102 *strict_overflow_p = true;
4103 return boolean_false_node;
4106 /* Otherwise, we don't know. */
4107 return NULL_TREE;
4110 gcc_unreachable ();
4114 /* Debugging dumps. */
4116 void dump_value_range (FILE *, value_range_t *);
4117 void debug_value_range (value_range_t *);
4118 void dump_all_value_ranges (FILE *);
4119 void debug_all_value_ranges (void);
4120 void dump_vr_equiv (FILE *, bitmap);
4121 void debug_vr_equiv (bitmap);
4124 /* Dump value range VR to FILE. */
4126 void
4127 dump_value_range (FILE *file, value_range_t *vr)
4129 if (vr == NULL)
4130 fprintf (file, "[]");
4131 else if (vr->type == VR_UNDEFINED)
4132 fprintf (file, "UNDEFINED");
4133 else if (vr->type == VR_RANGE || vr->type == VR_ANTI_RANGE)
4135 tree type = TREE_TYPE (vr->min);
4137 fprintf (file, "%s[", (vr->type == VR_ANTI_RANGE) ? "~" : "");
4139 if (is_negative_overflow_infinity (vr->min))
4140 fprintf (file, "-INF(OVF)");
4141 else if (INTEGRAL_TYPE_P (type)
4142 && !TYPE_UNSIGNED (type)
4143 && vrp_val_is_min (vr->min))
4144 fprintf (file, "-INF");
4145 else
4146 print_generic_expr (file, vr->min, 0);
4148 fprintf (file, ", ");
4150 if (is_positive_overflow_infinity (vr->max))
4151 fprintf (file, "+INF(OVF)");
4152 else if (INTEGRAL_TYPE_P (type)
4153 && vrp_val_is_max (vr->max))
4154 fprintf (file, "+INF");
4155 else
4156 print_generic_expr (file, vr->max, 0);
4158 fprintf (file, "]");
4160 if (vr->equiv)
4162 bitmap_iterator bi;
4163 unsigned i, c = 0;
4165 fprintf (file, " EQUIVALENCES: { ");
4167 EXECUTE_IF_SET_IN_BITMAP (vr->equiv, 0, i, bi)
4169 print_generic_expr (file, ssa_name (i), 0);
4170 fprintf (file, " ");
4171 c++;
4174 fprintf (file, "} (%u elements)", c);
4177 else if (vr->type == VR_VARYING)
4178 fprintf (file, "VARYING");
4179 else
4180 fprintf (file, "INVALID RANGE");
4184 /* Dump value range VR to stderr. */
4186 DEBUG_FUNCTION void
4187 debug_value_range (value_range_t *vr)
4189 dump_value_range (stderr, vr);
4190 fprintf (stderr, "\n");
4194 /* Dump value ranges of all SSA_NAMEs to FILE. */
4196 void
4197 dump_all_value_ranges (FILE *file)
4199 size_t i;
4201 for (i = 0; i < num_vr_values; i++)
4203 if (vr_value[i])
4205 print_generic_expr (file, ssa_name (i), 0);
4206 fprintf (file, ": ");
4207 dump_value_range (file, vr_value[i]);
4208 fprintf (file, "\n");
4212 fprintf (file, "\n");
4216 /* Dump all value ranges to stderr. */
4218 DEBUG_FUNCTION void
4219 debug_all_value_ranges (void)
4221 dump_all_value_ranges (stderr);
4225 /* Given a COND_EXPR COND of the form 'V OP W', and an SSA name V,
4226 create a new SSA name N and return the assertion assignment
4227 'V = ASSERT_EXPR <V, V OP W>'. */
4229 static gimple
4230 build_assert_expr_for (tree cond, tree v)
4232 tree a;
4233 gimple assertion;
4235 gcc_assert (TREE_CODE (v) == SSA_NAME
4236 && COMPARISON_CLASS_P (cond));
4238 a = build2 (ASSERT_EXPR, TREE_TYPE (v), v, cond);
4239 assertion = gimple_build_assign (NULL_TREE, a);
4241 /* The new ASSERT_EXPR, creates a new SSA name that replaces the
4242 operand of the ASSERT_EXPR. Create it so the new name and the old one
4243 are registered in the replacement table so that we can fix the SSA web
4244 after adding all the ASSERT_EXPRs. */
4245 create_new_def_for (v, assertion, NULL);
4247 return assertion;
4251 /* Return false if EXPR is a predicate expression involving floating
4252 point values. */
4254 static inline bool
4255 fp_predicate (gimple stmt)
4257 GIMPLE_CHECK (stmt, GIMPLE_COND);
4259 return FLOAT_TYPE_P (TREE_TYPE (gimple_cond_lhs (stmt)));
4263 /* If the range of values taken by OP can be inferred after STMT executes,
4264 return the comparison code (COMP_CODE_P) and value (VAL_P) that
4265 describes the inferred range. Return true if a range could be
4266 inferred. */
4268 static bool
4269 infer_value_range (gimple stmt, tree op, enum tree_code *comp_code_p, tree *val_p)
4271 *val_p = NULL_TREE;
4272 *comp_code_p = ERROR_MARK;
4274 /* Do not attempt to infer anything in names that flow through
4275 abnormal edges. */
4276 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op))
4277 return false;
4279 /* Similarly, don't infer anything from statements that may throw
4280 exceptions. */
4281 if (stmt_could_throw_p (stmt))
4282 return false;
4284 /* If STMT is the last statement of a basic block with no
4285 successors, there is no point inferring anything about any of its
4286 operands. We would not be able to find a proper insertion point
4287 for the assertion, anyway. */
4288 if (stmt_ends_bb_p (stmt) && EDGE_COUNT (gimple_bb (stmt)->succs) == 0)
4289 return false;
4291 /* We can only assume that a pointer dereference will yield
4292 non-NULL if -fdelete-null-pointer-checks is enabled. */
4293 if (flag_delete_null_pointer_checks
4294 && POINTER_TYPE_P (TREE_TYPE (op))
4295 && gimple_code (stmt) != GIMPLE_ASM)
4297 unsigned num_uses, num_loads, num_stores;
4299 count_uses_and_derefs (op, stmt, &num_uses, &num_loads, &num_stores);
4300 if (num_loads + num_stores > 0)
4302 *val_p = build_int_cst (TREE_TYPE (op), 0);
4303 *comp_code_p = NE_EXPR;
4304 return true;
4308 return false;
4312 void dump_asserts_for (FILE *, tree);
4313 void debug_asserts_for (tree);
4314 void dump_all_asserts (FILE *);
4315 void debug_all_asserts (void);
4317 /* Dump all the registered assertions for NAME to FILE. */
4319 void
4320 dump_asserts_for (FILE *file, tree name)
4322 assert_locus_t loc;
4324 fprintf (file, "Assertions to be inserted for ");
4325 print_generic_expr (file, name, 0);
4326 fprintf (file, "\n");
4328 loc = asserts_for[SSA_NAME_VERSION (name)];
4329 while (loc)
4331 fprintf (file, "\t");
4332 print_gimple_stmt (file, gsi_stmt (loc->si), 0, 0);
4333 fprintf (file, "\n\tBB #%d", loc->bb->index);
4334 if (loc->e)
4336 fprintf (file, "\n\tEDGE %d->%d", loc->e->src->index,
4337 loc->e->dest->index);
4338 dump_edge_info (file, loc->e, dump_flags, 0);
4340 fprintf (file, "\n\tPREDICATE: ");
4341 print_generic_expr (file, name, 0);
4342 fprintf (file, " %s ", tree_code_name[(int)loc->comp_code]);
4343 print_generic_expr (file, loc->val, 0);
4344 fprintf (file, "\n\n");
4345 loc = loc->next;
4348 fprintf (file, "\n");
4352 /* Dump all the registered assertions for NAME to stderr. */
4354 DEBUG_FUNCTION void
4355 debug_asserts_for (tree name)
4357 dump_asserts_for (stderr, name);
4361 /* Dump all the registered assertions for all the names to FILE. */
4363 void
4364 dump_all_asserts (FILE *file)
4366 unsigned i;
4367 bitmap_iterator bi;
4369 fprintf (file, "\nASSERT_EXPRs to be inserted\n\n");
4370 EXECUTE_IF_SET_IN_BITMAP (need_assert_for, 0, i, bi)
4371 dump_asserts_for (file, ssa_name (i));
4372 fprintf (file, "\n");
4376 /* Dump all the registered assertions for all the names to stderr. */
4378 DEBUG_FUNCTION void
4379 debug_all_asserts (void)
4381 dump_all_asserts (stderr);
4385 /* If NAME doesn't have an ASSERT_EXPR registered for asserting
4386 'EXPR COMP_CODE VAL' at a location that dominates block BB or
4387 E->DEST, then register this location as a possible insertion point
4388 for ASSERT_EXPR <NAME, EXPR COMP_CODE VAL>.
4390 BB, E and SI provide the exact insertion point for the new
4391 ASSERT_EXPR. If BB is NULL, then the ASSERT_EXPR is to be inserted
4392 on edge E. Otherwise, if E is NULL, the ASSERT_EXPR is inserted on
4393 BB. If SI points to a COND_EXPR or a SWITCH_EXPR statement, then E
4394 must not be NULL. */
4396 static void
4397 register_new_assert_for (tree name, tree expr,
4398 enum tree_code comp_code,
4399 tree val,
4400 basic_block bb,
4401 edge e,
4402 gimple_stmt_iterator si)
4404 assert_locus_t n, loc, last_loc;
4405 basic_block dest_bb;
4407 gcc_checking_assert (bb == NULL || e == NULL);
4409 if (e == NULL)
4410 gcc_checking_assert (gimple_code (gsi_stmt (si)) != GIMPLE_COND
4411 && gimple_code (gsi_stmt (si)) != GIMPLE_SWITCH);
4413 /* Never build an assert comparing against an integer constant with
4414 TREE_OVERFLOW set. This confuses our undefined overflow warning
4415 machinery. */
4416 if (TREE_CODE (val) == INTEGER_CST
4417 && TREE_OVERFLOW (val))
4418 val = build_int_cst_wide (TREE_TYPE (val),
4419 TREE_INT_CST_LOW (val), TREE_INT_CST_HIGH (val));
4421 /* The new assertion A will be inserted at BB or E. We need to
4422 determine if the new location is dominated by a previously
4423 registered location for A. If we are doing an edge insertion,
4424 assume that A will be inserted at E->DEST. Note that this is not
4425 necessarily true.
4427 If E is a critical edge, it will be split. But even if E is
4428 split, the new block will dominate the same set of blocks that
4429 E->DEST dominates.
4431 The reverse, however, is not true, blocks dominated by E->DEST
4432 will not be dominated by the new block created to split E. So,
4433 if the insertion location is on a critical edge, we will not use
4434 the new location to move another assertion previously registered
4435 at a block dominated by E->DEST. */
4436 dest_bb = (bb) ? bb : e->dest;
4438 /* If NAME already has an ASSERT_EXPR registered for COMP_CODE and
4439 VAL at a block dominating DEST_BB, then we don't need to insert a new
4440 one. Similarly, if the same assertion already exists at a block
4441 dominated by DEST_BB and the new location is not on a critical
4442 edge, then update the existing location for the assertion (i.e.,
4443 move the assertion up in the dominance tree).
4445 Note, this is implemented as a simple linked list because there
4446 should not be more than a handful of assertions registered per
4447 name. If this becomes a performance problem, a table hashed by
4448 COMP_CODE and VAL could be implemented. */
4449 loc = asserts_for[SSA_NAME_VERSION (name)];
4450 last_loc = loc;
4451 while (loc)
4453 if (loc->comp_code == comp_code
4454 && (loc->val == val
4455 || operand_equal_p (loc->val, val, 0))
4456 && (loc->expr == expr
4457 || operand_equal_p (loc->expr, expr, 0)))
4459 /* If E is not a critical edge and DEST_BB
4460 dominates the existing location for the assertion, move
4461 the assertion up in the dominance tree by updating its
4462 location information. */
4463 if ((e == NULL || !EDGE_CRITICAL_P (e))
4464 && dominated_by_p (CDI_DOMINATORS, loc->bb, dest_bb))
4466 loc->bb = dest_bb;
4467 loc->e = e;
4468 loc->si = si;
4469 return;
4473 /* Update the last node of the list and move to the next one. */
4474 last_loc = loc;
4475 loc = loc->next;
4478 /* If we didn't find an assertion already registered for
4479 NAME COMP_CODE VAL, add a new one at the end of the list of
4480 assertions associated with NAME. */
4481 n = XNEW (struct assert_locus_d);
4482 n->bb = dest_bb;
4483 n->e = e;
4484 n->si = si;
4485 n->comp_code = comp_code;
4486 n->val = val;
4487 n->expr = expr;
4488 n->next = NULL;
4490 if (last_loc)
4491 last_loc->next = n;
4492 else
4493 asserts_for[SSA_NAME_VERSION (name)] = n;
4495 bitmap_set_bit (need_assert_for, SSA_NAME_VERSION (name));
4498 /* (COND_OP0 COND_CODE COND_OP1) is a predicate which uses NAME.
4499 Extract a suitable test code and value and store them into *CODE_P and
4500 *VAL_P so the predicate is normalized to NAME *CODE_P *VAL_P.
4502 If no extraction was possible, return FALSE, otherwise return TRUE.
4504 If INVERT is true, then we invert the result stored into *CODE_P. */
4506 static bool
4507 extract_code_and_val_from_cond_with_ops (tree name, enum tree_code cond_code,
4508 tree cond_op0, tree cond_op1,
4509 bool invert, enum tree_code *code_p,
4510 tree *val_p)
4512 enum tree_code comp_code;
4513 tree val;
4515 /* Otherwise, we have a comparison of the form NAME COMP VAL
4516 or VAL COMP NAME. */
4517 if (name == cond_op1)
4519 /* If the predicate is of the form VAL COMP NAME, flip
4520 COMP around because we need to register NAME as the
4521 first operand in the predicate. */
4522 comp_code = swap_tree_comparison (cond_code);
4523 val = cond_op0;
4525 else
4527 /* The comparison is of the form NAME COMP VAL, so the
4528 comparison code remains unchanged. */
4529 comp_code = cond_code;
4530 val = cond_op1;
4533 /* Invert the comparison code as necessary. */
4534 if (invert)
4535 comp_code = invert_tree_comparison (comp_code, 0);
4537 /* VRP does not handle float types. */
4538 if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (val)))
4539 return false;
4541 /* Do not register always-false predicates.
4542 FIXME: this works around a limitation in fold() when dealing with
4543 enumerations. Given 'enum { N1, N2 } x;', fold will not
4544 fold 'if (x > N2)' to 'if (0)'. */
4545 if ((comp_code == GT_EXPR || comp_code == LT_EXPR)
4546 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
4548 tree min = TYPE_MIN_VALUE (TREE_TYPE (val));
4549 tree max = TYPE_MAX_VALUE (TREE_TYPE (val));
4551 if (comp_code == GT_EXPR
4552 && (!max
4553 || compare_values (val, max) == 0))
4554 return false;
4556 if (comp_code == LT_EXPR
4557 && (!min
4558 || compare_values (val, min) == 0))
4559 return false;
4561 *code_p = comp_code;
4562 *val_p = val;
4563 return true;
4566 /* Find out smallest RES where RES > VAL && (RES & MASK) == RES, if any
4567 (otherwise return VAL). VAL and MASK must be zero-extended for
4568 precision PREC. If SGNBIT is non-zero, first xor VAL with SGNBIT
4569 (to transform signed values into unsigned) and at the end xor
4570 SGNBIT back. */
4572 static double_int
4573 masked_increment (double_int val, double_int mask, double_int sgnbit,
4574 unsigned int prec)
4576 double_int bit = double_int_one, res;
4577 unsigned int i;
4579 val ^= sgnbit;
4580 for (i = 0; i < prec; i++, bit += bit)
4582 res = mask;
4583 if ((res & bit).is_zero ())
4584 continue;
4585 res = bit - double_int_one;
4586 res = (val + bit).and_not (res);
4587 res &= mask;
4588 if (res.ugt (val))
4589 return res ^ sgnbit;
4591 return val ^ sgnbit;
4594 /* Try to register an edge assertion for SSA name NAME on edge E for
4595 the condition COND contributing to the conditional jump pointed to by BSI.
4596 Invert the condition COND if INVERT is true.
4597 Return true if an assertion for NAME could be registered. */
4599 static bool
4600 register_edge_assert_for_2 (tree name, edge e, gimple_stmt_iterator bsi,
4601 enum tree_code cond_code,
4602 tree cond_op0, tree cond_op1, bool invert)
4604 tree val;
4605 enum tree_code comp_code;
4606 bool retval = false;
4608 if (!extract_code_and_val_from_cond_with_ops (name, cond_code,
4609 cond_op0,
4610 cond_op1,
4611 invert, &comp_code, &val))
4612 return false;
4614 /* Only register an ASSERT_EXPR if NAME was found in the sub-graph
4615 reachable from E. */
4616 if (live_on_edge (e, name)
4617 && !has_single_use (name))
4619 register_new_assert_for (name, name, comp_code, val, NULL, e, bsi);
4620 retval = true;
4623 /* In the case of NAME <= CST and NAME being defined as
4624 NAME = (unsigned) NAME2 + CST2 we can assert NAME2 >= -CST2
4625 and NAME2 <= CST - CST2. We can do the same for NAME > CST.
4626 This catches range and anti-range tests. */
4627 if ((comp_code == LE_EXPR
4628 || comp_code == GT_EXPR)
4629 && TREE_CODE (val) == INTEGER_CST
4630 && TYPE_UNSIGNED (TREE_TYPE (val)))
4632 gimple def_stmt = SSA_NAME_DEF_STMT (name);
4633 tree cst2 = NULL_TREE, name2 = NULL_TREE, name3 = NULL_TREE;
4635 /* Extract CST2 from the (optional) addition. */
4636 if (is_gimple_assign (def_stmt)
4637 && gimple_assign_rhs_code (def_stmt) == PLUS_EXPR)
4639 name2 = gimple_assign_rhs1 (def_stmt);
4640 cst2 = gimple_assign_rhs2 (def_stmt);
4641 if (TREE_CODE (name2) == SSA_NAME
4642 && TREE_CODE (cst2) == INTEGER_CST)
4643 def_stmt = SSA_NAME_DEF_STMT (name2);
4646 /* Extract NAME2 from the (optional) sign-changing cast. */
4647 if (gimple_assign_cast_p (def_stmt))
4649 if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt))
4650 && ! TYPE_UNSIGNED (TREE_TYPE (gimple_assign_rhs1 (def_stmt)))
4651 && (TYPE_PRECISION (gimple_expr_type (def_stmt))
4652 == TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (def_stmt)))))
4653 name3 = gimple_assign_rhs1 (def_stmt);
4656 /* If name3 is used later, create an ASSERT_EXPR for it. */
4657 if (name3 != NULL_TREE
4658 && TREE_CODE (name3) == SSA_NAME
4659 && (cst2 == NULL_TREE
4660 || TREE_CODE (cst2) == INTEGER_CST)
4661 && INTEGRAL_TYPE_P (TREE_TYPE (name3))
4662 && live_on_edge (e, name3)
4663 && !has_single_use (name3))
4665 tree tmp;
4667 /* Build an expression for the range test. */
4668 tmp = build1 (NOP_EXPR, TREE_TYPE (name), name3);
4669 if (cst2 != NULL_TREE)
4670 tmp = build2 (PLUS_EXPR, TREE_TYPE (name), tmp, cst2);
4672 if (dump_file)
4674 fprintf (dump_file, "Adding assert for ");
4675 print_generic_expr (dump_file, name3, 0);
4676 fprintf (dump_file, " from ");
4677 print_generic_expr (dump_file, tmp, 0);
4678 fprintf (dump_file, "\n");
4681 register_new_assert_for (name3, tmp, comp_code, val, NULL, e, bsi);
4683 retval = true;
4686 /* If name2 is used later, create an ASSERT_EXPR for it. */
4687 if (name2 != NULL_TREE
4688 && TREE_CODE (name2) == SSA_NAME
4689 && TREE_CODE (cst2) == INTEGER_CST
4690 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
4691 && live_on_edge (e, name2)
4692 && !has_single_use (name2))
4694 tree tmp;
4696 /* Build an expression for the range test. */
4697 tmp = name2;
4698 if (TREE_TYPE (name) != TREE_TYPE (name2))
4699 tmp = build1 (NOP_EXPR, TREE_TYPE (name), tmp);
4700 if (cst2 != NULL_TREE)
4701 tmp = build2 (PLUS_EXPR, TREE_TYPE (name), tmp, cst2);
4703 if (dump_file)
4705 fprintf (dump_file, "Adding assert for ");
4706 print_generic_expr (dump_file, name2, 0);
4707 fprintf (dump_file, " from ");
4708 print_generic_expr (dump_file, tmp, 0);
4709 fprintf (dump_file, "\n");
4712 register_new_assert_for (name2, tmp, comp_code, val, NULL, e, bsi);
4714 retval = true;
4718 if (TREE_CODE_CLASS (comp_code) == tcc_comparison
4719 && TREE_CODE (val) == INTEGER_CST)
4721 gimple def_stmt = SSA_NAME_DEF_STMT (name);
4722 tree name2 = NULL_TREE, names[2], cst2 = NULL_TREE;
4723 tree val2 = NULL_TREE;
4724 double_int mask = double_int_zero;
4725 unsigned int prec = TYPE_PRECISION (TREE_TYPE (val));
4726 unsigned int nprec = prec;
4727 enum tree_code rhs_code = ERROR_MARK;
4729 if (is_gimple_assign (def_stmt))
4730 rhs_code = gimple_assign_rhs_code (def_stmt);
4732 /* Add asserts for NAME cmp CST and NAME being defined
4733 as NAME = (int) NAME2. */
4734 if (!TYPE_UNSIGNED (TREE_TYPE (val))
4735 && (comp_code == LE_EXPR || comp_code == LT_EXPR
4736 || comp_code == GT_EXPR || comp_code == GE_EXPR)
4737 && gimple_assign_cast_p (def_stmt))
4739 name2 = gimple_assign_rhs1 (def_stmt);
4740 if (CONVERT_EXPR_CODE_P (rhs_code)
4741 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
4742 && TYPE_UNSIGNED (TREE_TYPE (name2))
4743 && prec == TYPE_PRECISION (TREE_TYPE (name2))
4744 && (comp_code == LE_EXPR || comp_code == GT_EXPR
4745 || !tree_int_cst_equal (val,
4746 TYPE_MIN_VALUE (TREE_TYPE (val))))
4747 && live_on_edge (e, name2)
4748 && !has_single_use (name2))
4750 tree tmp, cst;
4751 enum tree_code new_comp_code = comp_code;
4753 cst = fold_convert (TREE_TYPE (name2),
4754 TYPE_MIN_VALUE (TREE_TYPE (val)));
4755 /* Build an expression for the range test. */
4756 tmp = build2 (PLUS_EXPR, TREE_TYPE (name2), name2, cst);
4757 cst = fold_build2 (PLUS_EXPR, TREE_TYPE (name2), cst,
4758 fold_convert (TREE_TYPE (name2), val));
4759 if (comp_code == LT_EXPR || comp_code == GE_EXPR)
4761 new_comp_code = comp_code == LT_EXPR ? LE_EXPR : GT_EXPR;
4762 cst = fold_build2 (MINUS_EXPR, TREE_TYPE (name2), cst,
4763 build_int_cst (TREE_TYPE (name2), 1));
4766 if (dump_file)
4768 fprintf (dump_file, "Adding assert for ");
4769 print_generic_expr (dump_file, name2, 0);
4770 fprintf (dump_file, " from ");
4771 print_generic_expr (dump_file, tmp, 0);
4772 fprintf (dump_file, "\n");
4775 register_new_assert_for (name2, tmp, new_comp_code, cst, NULL,
4776 e, bsi);
4778 retval = true;
4782 /* Add asserts for NAME cmp CST and NAME being defined as
4783 NAME = NAME2 >> CST2.
4785 Extract CST2 from the right shift. */
4786 if (rhs_code == RSHIFT_EXPR)
4788 name2 = gimple_assign_rhs1 (def_stmt);
4789 cst2 = gimple_assign_rhs2 (def_stmt);
4790 if (TREE_CODE (name2) == SSA_NAME
4791 && host_integerp (cst2, 1)
4792 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
4793 && IN_RANGE (tree_low_cst (cst2, 1), 1, prec - 1)
4794 && prec <= HOST_BITS_PER_DOUBLE_INT
4795 && prec == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (val)))
4796 && live_on_edge (e, name2)
4797 && !has_single_use (name2))
4799 mask = double_int::mask (tree_low_cst (cst2, 1));
4800 val2 = fold_binary (LSHIFT_EXPR, TREE_TYPE (val), val, cst2);
4803 if (val2 != NULL_TREE
4804 && TREE_CODE (val2) == INTEGER_CST
4805 && simple_cst_equal (fold_build2 (RSHIFT_EXPR,
4806 TREE_TYPE (val),
4807 val2, cst2), val))
4809 enum tree_code new_comp_code = comp_code;
4810 tree tmp, new_val;
4812 tmp = name2;
4813 if (comp_code == EQ_EXPR || comp_code == NE_EXPR)
4815 if (!TYPE_UNSIGNED (TREE_TYPE (val)))
4817 tree type = build_nonstandard_integer_type (prec, 1);
4818 tmp = build1 (NOP_EXPR, type, name2);
4819 val2 = fold_convert (type, val2);
4821 tmp = fold_build2 (MINUS_EXPR, TREE_TYPE (tmp), tmp, val2);
4822 new_val = double_int_to_tree (TREE_TYPE (tmp), mask);
4823 new_comp_code = comp_code == EQ_EXPR ? LE_EXPR : GT_EXPR;
4825 else if (comp_code == LT_EXPR || comp_code == GE_EXPR)
4826 new_val = val2;
4827 else
4829 double_int maxval
4830 = double_int::max_value (prec, TYPE_UNSIGNED (TREE_TYPE (val)));
4831 mask |= tree_to_double_int (val2);
4832 if (mask == maxval)
4833 new_val = NULL_TREE;
4834 else
4835 new_val = double_int_to_tree (TREE_TYPE (val2), mask);
4838 if (new_val)
4840 if (dump_file)
4842 fprintf (dump_file, "Adding assert for ");
4843 print_generic_expr (dump_file, name2, 0);
4844 fprintf (dump_file, " from ");
4845 print_generic_expr (dump_file, tmp, 0);
4846 fprintf (dump_file, "\n");
4849 register_new_assert_for (name2, tmp, new_comp_code, new_val,
4850 NULL, e, bsi);
4851 retval = true;
4855 /* Add asserts for NAME cmp CST and NAME being defined as
4856 NAME = NAME2 & CST2.
4858 Extract CST2 from the and.
4860 Also handle
4861 NAME = (unsigned) NAME2;
4862 casts where NAME's type is unsigned and has smaller precision
4863 than NAME2's type as if it was NAME = NAME2 & MASK. */
4864 names[0] = NULL_TREE;
4865 names[1] = NULL_TREE;
4866 cst2 = NULL_TREE;
4867 if (rhs_code == BIT_AND_EXPR
4868 || (CONVERT_EXPR_CODE_P (rhs_code)
4869 && TREE_CODE (TREE_TYPE (val)) == INTEGER_TYPE
4870 && TYPE_UNSIGNED (TREE_TYPE (val))
4871 && TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (def_stmt)))
4872 > prec
4873 && !retval))
4875 name2 = gimple_assign_rhs1 (def_stmt);
4876 if (rhs_code == BIT_AND_EXPR)
4877 cst2 = gimple_assign_rhs2 (def_stmt);
4878 else
4880 cst2 = TYPE_MAX_VALUE (TREE_TYPE (val));
4881 nprec = TYPE_PRECISION (TREE_TYPE (name2));
4883 if (TREE_CODE (name2) == SSA_NAME
4884 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
4885 && TREE_CODE (cst2) == INTEGER_CST
4886 && !integer_zerop (cst2)
4887 && nprec <= HOST_BITS_PER_DOUBLE_INT
4888 && (nprec > 1
4889 || TYPE_UNSIGNED (TREE_TYPE (val))))
4891 gimple def_stmt2 = SSA_NAME_DEF_STMT (name2);
4892 if (gimple_assign_cast_p (def_stmt2))
4894 names[1] = gimple_assign_rhs1 (def_stmt2);
4895 if (!CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt2))
4896 || !INTEGRAL_TYPE_P (TREE_TYPE (names[1]))
4897 || (TYPE_PRECISION (TREE_TYPE (name2))
4898 != TYPE_PRECISION (TREE_TYPE (names[1])))
4899 || !live_on_edge (e, names[1])
4900 || has_single_use (names[1]))
4901 names[1] = NULL_TREE;
4903 if (live_on_edge (e, name2)
4904 && !has_single_use (name2))
4905 names[0] = name2;
4908 if (names[0] || names[1])
4910 double_int minv, maxv = double_int_zero, valv, cst2v;
4911 double_int tem, sgnbit;
4912 bool valid_p = false, valn = false, cst2n = false;
4913 enum tree_code ccode = comp_code;
4915 valv = tree_to_double_int (val).zext (nprec);
4916 cst2v = tree_to_double_int (cst2).zext (nprec);
4917 if (!TYPE_UNSIGNED (TREE_TYPE (val)))
4919 valn = valv.sext (nprec).is_negative ();
4920 cst2n = cst2v.sext (nprec).is_negative ();
4922 /* If CST2 doesn't have most significant bit set,
4923 but VAL is negative, we have comparison like
4924 if ((x & 0x123) > -4) (always true). Just give up. */
4925 if (!cst2n && valn)
4926 ccode = ERROR_MARK;
4927 if (cst2n)
4928 sgnbit = double_int_one.llshift (nprec - 1, nprec).zext (nprec);
4929 else
4930 sgnbit = double_int_zero;
4931 minv = valv & cst2v;
4932 switch (ccode)
4934 case EQ_EXPR:
4935 /* Minimum unsigned value for equality is VAL & CST2
4936 (should be equal to VAL, otherwise we probably should
4937 have folded the comparison into false) and
4938 maximum unsigned value is VAL | ~CST2. */
4939 maxv = valv | ~cst2v;
4940 maxv = maxv.zext (nprec);
4941 valid_p = true;
4942 break;
4943 case NE_EXPR:
4944 tem = valv | ~cst2v;
4945 tem = tem.zext (nprec);
4946 /* If VAL is 0, handle (X & CST2) != 0 as (X & CST2) > 0U. */
4947 if (valv.is_zero ())
4949 cst2n = false;
4950 sgnbit = double_int_zero;
4951 goto gt_expr;
4953 /* If (VAL | ~CST2) is all ones, handle it as
4954 (X & CST2) < VAL. */
4955 if (tem == double_int::mask (nprec))
4957 cst2n = false;
4958 valn = false;
4959 sgnbit = double_int_zero;
4960 goto lt_expr;
4962 if (!cst2n
4963 && cst2v.sext (nprec).is_negative ())
4964 sgnbit
4965 = double_int_one.llshift (nprec - 1, nprec).zext (nprec);
4966 if (!sgnbit.is_zero ())
4968 if (valv == sgnbit)
4970 cst2n = true;
4971 valn = true;
4972 goto gt_expr;
4974 if (tem == double_int::mask (nprec - 1))
4976 cst2n = true;
4977 goto lt_expr;
4979 if (!cst2n)
4980 sgnbit = double_int_zero;
4982 break;
4983 case GE_EXPR:
4984 /* Minimum unsigned value for >= if (VAL & CST2) == VAL
4985 is VAL and maximum unsigned value is ~0. For signed
4986 comparison, if CST2 doesn't have most significant bit
4987 set, handle it similarly. If CST2 has MSB set,
4988 the minimum is the same, and maximum is ~0U/2. */
4989 if (minv != valv)
4991 /* If (VAL & CST2) != VAL, X & CST2 can't be equal to
4992 VAL. */
4993 minv = masked_increment (valv, cst2v, sgnbit, nprec);
4994 if (minv == valv)
4995 break;
4997 maxv = double_int::mask (nprec - (cst2n ? 1 : 0));
4998 valid_p = true;
4999 break;
5000 case GT_EXPR:
5001 gt_expr:
5002 /* Find out smallest MINV where MINV > VAL
5003 && (MINV & CST2) == MINV, if any. If VAL is signed and
5004 CST2 has MSB set, compute it biased by 1 << (nprec - 1). */
5005 minv = masked_increment (valv, cst2v, sgnbit, nprec);
5006 if (minv == valv)
5007 break;
5008 maxv = double_int::mask (nprec - (cst2n ? 1 : 0));
5009 valid_p = true;
5010 break;
5011 case LE_EXPR:
5012 /* Minimum unsigned value for <= is 0 and maximum
5013 unsigned value is VAL | ~CST2 if (VAL & CST2) == VAL.
5014 Otherwise, find smallest VAL2 where VAL2 > VAL
5015 && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
5016 as maximum.
5017 For signed comparison, if CST2 doesn't have most
5018 significant bit set, handle it similarly. If CST2 has
5019 MSB set, the maximum is the same and minimum is INT_MIN. */
5020 if (minv == valv)
5021 maxv = valv;
5022 else
5024 maxv = masked_increment (valv, cst2v, sgnbit, nprec);
5025 if (maxv == valv)
5026 break;
5027 maxv -= double_int_one;
5029 maxv |= ~cst2v;
5030 maxv = maxv.zext (nprec);
5031 minv = sgnbit;
5032 valid_p = true;
5033 break;
5034 case LT_EXPR:
5035 lt_expr:
5036 /* Minimum unsigned value for < is 0 and maximum
5037 unsigned value is (VAL-1) | ~CST2 if (VAL & CST2) == VAL.
5038 Otherwise, find smallest VAL2 where VAL2 > VAL
5039 && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
5040 as maximum.
5041 For signed comparison, if CST2 doesn't have most
5042 significant bit set, handle it similarly. If CST2 has
5043 MSB set, the maximum is the same and minimum is INT_MIN. */
5044 if (minv == valv)
5046 if (valv == sgnbit)
5047 break;
5048 maxv = valv;
5050 else
5052 maxv = masked_increment (valv, cst2v, sgnbit, nprec);
5053 if (maxv == valv)
5054 break;
5056 maxv -= double_int_one;
5057 maxv |= ~cst2v;
5058 maxv = maxv.zext (nprec);
5059 minv = sgnbit;
5060 valid_p = true;
5061 break;
5062 default:
5063 break;
5065 if (valid_p
5066 && (maxv - minv).zext (nprec) != double_int::mask (nprec))
5068 tree tmp, new_val, type;
5069 int i;
5071 for (i = 0; i < 2; i++)
5072 if (names[i])
5074 double_int maxv2 = maxv;
5075 tmp = names[i];
5076 type = TREE_TYPE (names[i]);
5077 if (!TYPE_UNSIGNED (type))
5079 type = build_nonstandard_integer_type (nprec, 1);
5080 tmp = build1 (NOP_EXPR, type, names[i]);
5082 if (!minv.is_zero ())
5084 tmp = build2 (PLUS_EXPR, type, tmp,
5085 double_int_to_tree (type, -minv));
5086 maxv2 = maxv - minv;
5088 new_val = double_int_to_tree (type, maxv2);
5090 if (dump_file)
5092 fprintf (dump_file, "Adding assert for ");
5093 print_generic_expr (dump_file, names[i], 0);
5094 fprintf (dump_file, " from ");
5095 print_generic_expr (dump_file, tmp, 0);
5096 fprintf (dump_file, "\n");
5099 register_new_assert_for (names[i], tmp, LE_EXPR,
5100 new_val, NULL, e, bsi);
5101 retval = true;
5107 return retval;
5110 /* OP is an operand of a truth value expression which is known to have
5111 a particular value. Register any asserts for OP and for any
5112 operands in OP's defining statement.
5114 If CODE is EQ_EXPR, then we want to register OP is zero (false),
5115 if CODE is NE_EXPR, then we want to register OP is nonzero (true). */
5117 static bool
5118 register_edge_assert_for_1 (tree op, enum tree_code code,
5119 edge e, gimple_stmt_iterator bsi)
5121 bool retval = false;
5122 gimple op_def;
5123 tree val;
5124 enum tree_code rhs_code;
5126 /* We only care about SSA_NAMEs. */
5127 if (TREE_CODE (op) != SSA_NAME)
5128 return false;
5130 /* We know that OP will have a zero or nonzero value. If OP is used
5131 more than once go ahead and register an assert for OP.
5133 The FOUND_IN_SUBGRAPH support is not helpful in this situation as
5134 it will always be set for OP (because OP is used in a COND_EXPR in
5135 the subgraph). */
5136 if (!has_single_use (op))
5138 val = build_int_cst (TREE_TYPE (op), 0);
5139 register_new_assert_for (op, op, code, val, NULL, e, bsi);
5140 retval = true;
5143 /* Now look at how OP is set. If it's set from a comparison,
5144 a truth operation or some bit operations, then we may be able
5145 to register information about the operands of that assignment. */
5146 op_def = SSA_NAME_DEF_STMT (op);
5147 if (gimple_code (op_def) != GIMPLE_ASSIGN)
5148 return retval;
5150 rhs_code = gimple_assign_rhs_code (op_def);
5152 if (TREE_CODE_CLASS (rhs_code) == tcc_comparison)
5154 bool invert = (code == EQ_EXPR ? true : false);
5155 tree op0 = gimple_assign_rhs1 (op_def);
5156 tree op1 = gimple_assign_rhs2 (op_def);
5158 if (TREE_CODE (op0) == SSA_NAME)
5159 retval |= register_edge_assert_for_2 (op0, e, bsi, rhs_code, op0, op1,
5160 invert);
5161 if (TREE_CODE (op1) == SSA_NAME)
5162 retval |= register_edge_assert_for_2 (op1, e, bsi, rhs_code, op0, op1,
5163 invert);
5165 else if ((code == NE_EXPR
5166 && gimple_assign_rhs_code (op_def) == BIT_AND_EXPR)
5167 || (code == EQ_EXPR
5168 && gimple_assign_rhs_code (op_def) == BIT_IOR_EXPR))
5170 /* Recurse on each operand. */
5171 retval |= register_edge_assert_for_1 (gimple_assign_rhs1 (op_def),
5172 code, e, bsi);
5173 retval |= register_edge_assert_for_1 (gimple_assign_rhs2 (op_def),
5174 code, e, bsi);
5176 else if (gimple_assign_rhs_code (op_def) == BIT_NOT_EXPR
5177 && TYPE_PRECISION (TREE_TYPE (gimple_assign_lhs (op_def))) == 1)
5179 /* Recurse, flipping CODE. */
5180 code = invert_tree_comparison (code, false);
5181 retval |= register_edge_assert_for_1 (gimple_assign_rhs1 (op_def),
5182 code, e, bsi);
5184 else if (gimple_assign_rhs_code (op_def) == SSA_NAME)
5186 /* Recurse through the copy. */
5187 retval |= register_edge_assert_for_1 (gimple_assign_rhs1 (op_def),
5188 code, e, bsi);
5190 else if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (op_def)))
5192 /* Recurse through the type conversion. */
5193 retval |= register_edge_assert_for_1 (gimple_assign_rhs1 (op_def),
5194 code, e, bsi);
5197 return retval;
5200 /* Try to register an edge assertion for SSA name NAME on edge E for
5201 the condition COND contributing to the conditional jump pointed to by SI.
5202 Return true if an assertion for NAME could be registered. */
5204 static bool
5205 register_edge_assert_for (tree name, edge e, gimple_stmt_iterator si,
5206 enum tree_code cond_code, tree cond_op0,
5207 tree cond_op1)
5209 tree val;
5210 enum tree_code comp_code;
5211 bool retval = false;
5212 bool is_else_edge = (e->flags & EDGE_FALSE_VALUE) != 0;
5214 /* Do not attempt to infer anything in names that flow through
5215 abnormal edges. */
5216 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name))
5217 return false;
5219 if (!extract_code_and_val_from_cond_with_ops (name, cond_code,
5220 cond_op0, cond_op1,
5221 is_else_edge,
5222 &comp_code, &val))
5223 return false;
5225 /* Register ASSERT_EXPRs for name. */
5226 retval |= register_edge_assert_for_2 (name, e, si, cond_code, cond_op0,
5227 cond_op1, is_else_edge);
5230 /* If COND is effectively an equality test of an SSA_NAME against
5231 the value zero or one, then we may be able to assert values
5232 for SSA_NAMEs which flow into COND. */
5234 /* In the case of NAME == 1 or NAME != 0, for BIT_AND_EXPR defining
5235 statement of NAME we can assert both operands of the BIT_AND_EXPR
5236 have nonzero value. */
5237 if (((comp_code == EQ_EXPR && integer_onep (val))
5238 || (comp_code == NE_EXPR && integer_zerop (val))))
5240 gimple def_stmt = SSA_NAME_DEF_STMT (name);
5242 if (is_gimple_assign (def_stmt)
5243 && gimple_assign_rhs_code (def_stmt) == BIT_AND_EXPR)
5245 tree op0 = gimple_assign_rhs1 (def_stmt);
5246 tree op1 = gimple_assign_rhs2 (def_stmt);
5247 retval |= register_edge_assert_for_1 (op0, NE_EXPR, e, si);
5248 retval |= register_edge_assert_for_1 (op1, NE_EXPR, e, si);
5252 /* In the case of NAME == 0 or NAME != 1, for BIT_IOR_EXPR defining
5253 statement of NAME we can assert both operands of the BIT_IOR_EXPR
5254 have zero value. */
5255 if (((comp_code == EQ_EXPR && integer_zerop (val))
5256 || (comp_code == NE_EXPR && integer_onep (val))))
5258 gimple def_stmt = SSA_NAME_DEF_STMT (name);
5260 /* For BIT_IOR_EXPR only if NAME == 0 both operands have
5261 necessarily zero value, or if type-precision is one. */
5262 if (is_gimple_assign (def_stmt)
5263 && (gimple_assign_rhs_code (def_stmt) == BIT_IOR_EXPR
5264 && (TYPE_PRECISION (TREE_TYPE (name)) == 1
5265 || comp_code == EQ_EXPR)))
5267 tree op0 = gimple_assign_rhs1 (def_stmt);
5268 tree op1 = gimple_assign_rhs2 (def_stmt);
5269 retval |= register_edge_assert_for_1 (op0, EQ_EXPR, e, si);
5270 retval |= register_edge_assert_for_1 (op1, EQ_EXPR, e, si);
5274 return retval;
5278 /* Determine whether the outgoing edges of BB should receive an
5279 ASSERT_EXPR for each of the operands of BB's LAST statement.
5280 The last statement of BB must be a COND_EXPR.
5282 If any of the sub-graphs rooted at BB have an interesting use of
5283 the predicate operands, an assert location node is added to the
5284 list of assertions for the corresponding operands. */
5286 static bool
5287 find_conditional_asserts (basic_block bb, gimple last)
5289 bool need_assert;
5290 gimple_stmt_iterator bsi;
5291 tree op;
5292 edge_iterator ei;
5293 edge e;
5294 ssa_op_iter iter;
5296 need_assert = false;
5297 bsi = gsi_for_stmt (last);
5299 /* Look for uses of the operands in each of the sub-graphs
5300 rooted at BB. We need to check each of the outgoing edges
5301 separately, so that we know what kind of ASSERT_EXPR to
5302 insert. */
5303 FOR_EACH_EDGE (e, ei, bb->succs)
5305 if (e->dest == bb)
5306 continue;
5308 /* Register the necessary assertions for each operand in the
5309 conditional predicate. */
5310 FOR_EACH_SSA_TREE_OPERAND (op, last, iter, SSA_OP_USE)
5312 need_assert |= register_edge_assert_for (op, e, bsi,
5313 gimple_cond_code (last),
5314 gimple_cond_lhs (last),
5315 gimple_cond_rhs (last));
5319 return need_assert;
5322 struct case_info
5324 tree expr;
5325 basic_block bb;
5328 /* Compare two case labels sorting first by the destination bb index
5329 and then by the case value. */
5331 static int
5332 compare_case_labels (const void *p1, const void *p2)
5334 const struct case_info *ci1 = (const struct case_info *) p1;
5335 const struct case_info *ci2 = (const struct case_info *) p2;
5336 int idx1 = ci1->bb->index;
5337 int idx2 = ci2->bb->index;
5339 if (idx1 < idx2)
5340 return -1;
5341 else if (idx1 == idx2)
5343 /* Make sure the default label is first in a group. */
5344 if (!CASE_LOW (ci1->expr))
5345 return -1;
5346 else if (!CASE_LOW (ci2->expr))
5347 return 1;
5348 else
5349 return tree_int_cst_compare (CASE_LOW (ci1->expr),
5350 CASE_LOW (ci2->expr));
5352 else
5353 return 1;
5356 /* Determine whether the outgoing edges of BB should receive an
5357 ASSERT_EXPR for each of the operands of BB's LAST statement.
5358 The last statement of BB must be a SWITCH_EXPR.
5360 If any of the sub-graphs rooted at BB have an interesting use of
5361 the predicate operands, an assert location node is added to the
5362 list of assertions for the corresponding operands. */
5364 static bool
5365 find_switch_asserts (basic_block bb, gimple last)
5367 bool need_assert;
5368 gimple_stmt_iterator bsi;
5369 tree op;
5370 edge e;
5371 struct case_info *ci;
5372 size_t n = gimple_switch_num_labels (last);
5373 #if GCC_VERSION >= 4000
5374 unsigned int idx;
5375 #else
5376 /* Work around GCC 3.4 bug (PR 37086). */
5377 volatile unsigned int idx;
5378 #endif
5380 need_assert = false;
5381 bsi = gsi_for_stmt (last);
5382 op = gimple_switch_index (last);
5383 if (TREE_CODE (op) != SSA_NAME)
5384 return false;
5386 /* Build a vector of case labels sorted by destination label. */
5387 ci = XNEWVEC (struct case_info, n);
5388 for (idx = 0; idx < n; ++idx)
5390 ci[idx].expr = gimple_switch_label (last, idx);
5391 ci[idx].bb = label_to_block (CASE_LABEL (ci[idx].expr));
5393 qsort (ci, n, sizeof (struct case_info), compare_case_labels);
5395 for (idx = 0; idx < n; ++idx)
5397 tree min, max;
5398 tree cl = ci[idx].expr;
5399 basic_block cbb = ci[idx].bb;
5401 min = CASE_LOW (cl);
5402 max = CASE_HIGH (cl);
5404 /* If there are multiple case labels with the same destination
5405 we need to combine them to a single value range for the edge. */
5406 if (idx + 1 < n && cbb == ci[idx + 1].bb)
5408 /* Skip labels until the last of the group. */
5409 do {
5410 ++idx;
5411 } while (idx < n && cbb == ci[idx].bb);
5412 --idx;
5414 /* Pick up the maximum of the case label range. */
5415 if (CASE_HIGH (ci[idx].expr))
5416 max = CASE_HIGH (ci[idx].expr);
5417 else
5418 max = CASE_LOW (ci[idx].expr);
5421 /* Nothing to do if the range includes the default label until we
5422 can register anti-ranges. */
5423 if (min == NULL_TREE)
5424 continue;
5426 /* Find the edge to register the assert expr on. */
5427 e = find_edge (bb, cbb);
5429 /* Register the necessary assertions for the operand in the
5430 SWITCH_EXPR. */
5431 need_assert |= register_edge_assert_for (op, e, bsi,
5432 max ? GE_EXPR : EQ_EXPR,
5434 fold_convert (TREE_TYPE (op),
5435 min));
5436 if (max)
5438 need_assert |= register_edge_assert_for (op, e, bsi, LE_EXPR,
5440 fold_convert (TREE_TYPE (op),
5441 max));
5445 XDELETEVEC (ci);
5446 return need_assert;
5450 /* Traverse all the statements in block BB looking for statements that
5451 may generate useful assertions for the SSA names in their operand.
5452 If a statement produces a useful assertion A for name N_i, then the
5453 list of assertions already generated for N_i is scanned to
5454 determine if A is actually needed.
5456 If N_i already had the assertion A at a location dominating the
5457 current location, then nothing needs to be done. Otherwise, the
5458 new location for A is recorded instead.
5460 1- For every statement S in BB, all the variables used by S are
5461 added to bitmap FOUND_IN_SUBGRAPH.
5463 2- If statement S uses an operand N in a way that exposes a known
5464 value range for N, then if N was not already generated by an
5465 ASSERT_EXPR, create a new assert location for N. For instance,
5466 if N is a pointer and the statement dereferences it, we can
5467 assume that N is not NULL.
5469 3- COND_EXPRs are a special case of #2. We can derive range
5470 information from the predicate but need to insert different
5471 ASSERT_EXPRs for each of the sub-graphs rooted at the
5472 conditional block. If the last statement of BB is a conditional
5473 expression of the form 'X op Y', then
5475 a) Remove X and Y from the set FOUND_IN_SUBGRAPH.
5477 b) If the conditional is the only entry point to the sub-graph
5478 corresponding to the THEN_CLAUSE, recurse into it. On
5479 return, if X and/or Y are marked in FOUND_IN_SUBGRAPH, then
5480 an ASSERT_EXPR is added for the corresponding variable.
5482 c) Repeat step (b) on the ELSE_CLAUSE.
5484 d) Mark X and Y in FOUND_IN_SUBGRAPH.
5486 For instance,
5488 if (a == 9)
5489 b = a;
5490 else
5491 b = c + 1;
5493 In this case, an assertion on the THEN clause is useful to
5494 determine that 'a' is always 9 on that edge. However, an assertion
5495 on the ELSE clause would be unnecessary.
5497 4- If BB does not end in a conditional expression, then we recurse
5498 into BB's dominator children.
5500 At the end of the recursive traversal, every SSA name will have a
5501 list of locations where ASSERT_EXPRs should be added. When a new
5502 location for name N is found, it is registered by calling
5503 register_new_assert_for. That function keeps track of all the
5504 registered assertions to prevent adding unnecessary assertions.
5505 For instance, if a pointer P_4 is dereferenced more than once in a
5506 dominator tree, only the location dominating all the dereference of
5507 P_4 will receive an ASSERT_EXPR.
5509 If this function returns true, then it means that there are names
5510 for which we need to generate ASSERT_EXPRs. Those assertions are
5511 inserted by process_assert_insertions. */
5513 static bool
5514 find_assert_locations_1 (basic_block bb, sbitmap live)
5516 gimple_stmt_iterator si;
5517 gimple last;
5518 bool need_assert;
5520 need_assert = false;
5521 last = last_stmt (bb);
5523 /* If BB's last statement is a conditional statement involving integer
5524 operands, determine if we need to add ASSERT_EXPRs. */
5525 if (last
5526 && gimple_code (last) == GIMPLE_COND
5527 && !fp_predicate (last)
5528 && !ZERO_SSA_OPERANDS (last, SSA_OP_USE))
5529 need_assert |= find_conditional_asserts (bb, last);
5531 /* If BB's last statement is a switch statement involving integer
5532 operands, determine if we need to add ASSERT_EXPRs. */
5533 if (last
5534 && gimple_code (last) == GIMPLE_SWITCH
5535 && !ZERO_SSA_OPERANDS (last, SSA_OP_USE))
5536 need_assert |= find_switch_asserts (bb, last);
5538 /* Traverse all the statements in BB marking used names and looking
5539 for statements that may infer assertions for their used operands. */
5540 for (si = gsi_last_bb (bb); !gsi_end_p (si); gsi_prev (&si))
5542 gimple stmt;
5543 tree op;
5544 ssa_op_iter i;
5546 stmt = gsi_stmt (si);
5548 if (is_gimple_debug (stmt))
5549 continue;
5551 /* See if we can derive an assertion for any of STMT's operands. */
5552 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_USE)
5554 tree value;
5555 enum tree_code comp_code;
5557 /* If op is not live beyond this stmt, do not bother to insert
5558 asserts for it. */
5559 if (!bitmap_bit_p (live, SSA_NAME_VERSION (op)))
5560 continue;
5562 /* If OP is used in such a way that we can infer a value
5563 range for it, and we don't find a previous assertion for
5564 it, create a new assertion location node for OP. */
5565 if (infer_value_range (stmt, op, &comp_code, &value))
5567 /* If we are able to infer a nonzero value range for OP,
5568 then walk backwards through the use-def chain to see if OP
5569 was set via a typecast.
5571 If so, then we can also infer a nonzero value range
5572 for the operand of the NOP_EXPR. */
5573 if (comp_code == NE_EXPR && integer_zerop (value))
5575 tree t = op;
5576 gimple def_stmt = SSA_NAME_DEF_STMT (t);
5578 while (is_gimple_assign (def_stmt)
5579 && gimple_assign_rhs_code (def_stmt) == NOP_EXPR
5580 && TREE_CODE
5581 (gimple_assign_rhs1 (def_stmt)) == SSA_NAME
5582 && POINTER_TYPE_P
5583 (TREE_TYPE (gimple_assign_rhs1 (def_stmt))))
5585 t = gimple_assign_rhs1 (def_stmt);
5586 def_stmt = SSA_NAME_DEF_STMT (t);
5588 /* Note we want to register the assert for the
5589 operand of the NOP_EXPR after SI, not after the
5590 conversion. */
5591 if (! has_single_use (t))
5593 register_new_assert_for (t, t, comp_code, value,
5594 bb, NULL, si);
5595 need_assert = true;
5600 register_new_assert_for (op, op, comp_code, value, bb, NULL, si);
5601 need_assert = true;
5605 /* Update live. */
5606 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_USE)
5607 bitmap_set_bit (live, SSA_NAME_VERSION (op));
5608 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_DEF)
5609 bitmap_clear_bit (live, SSA_NAME_VERSION (op));
5612 /* Traverse all PHI nodes in BB, updating live. */
5613 for (si = gsi_start_phis (bb); !gsi_end_p(si); gsi_next (&si))
5615 use_operand_p arg_p;
5616 ssa_op_iter i;
5617 gimple phi = gsi_stmt (si);
5618 tree res = gimple_phi_result (phi);
5620 if (virtual_operand_p (res))
5621 continue;
5623 FOR_EACH_PHI_ARG (arg_p, phi, i, SSA_OP_USE)
5625 tree arg = USE_FROM_PTR (arg_p);
5626 if (TREE_CODE (arg) == SSA_NAME)
5627 bitmap_set_bit (live, SSA_NAME_VERSION (arg));
5630 bitmap_clear_bit (live, SSA_NAME_VERSION (res));
5633 return need_assert;
5636 /* Do an RPO walk over the function computing SSA name liveness
5637 on-the-fly and deciding on assert expressions to insert.
5638 Returns true if there are assert expressions to be inserted. */
5640 static bool
5641 find_assert_locations (void)
5643 int *rpo = XNEWVEC (int, last_basic_block);
5644 int *bb_rpo = XNEWVEC (int, last_basic_block);
5645 int *last_rpo = XCNEWVEC (int, last_basic_block);
5646 int rpo_cnt, i;
5647 bool need_asserts;
5649 live = XCNEWVEC (sbitmap, last_basic_block);
5650 rpo_cnt = pre_and_rev_post_order_compute (NULL, rpo, false);
5651 for (i = 0; i < rpo_cnt; ++i)
5652 bb_rpo[rpo[i]] = i;
5654 need_asserts = false;
5655 for (i = rpo_cnt - 1; i >= 0; --i)
5657 basic_block bb = BASIC_BLOCK (rpo[i]);
5658 edge e;
5659 edge_iterator ei;
5661 if (!live[rpo[i]])
5663 live[rpo[i]] = sbitmap_alloc (num_ssa_names);
5664 bitmap_clear (live[rpo[i]]);
5667 /* Process BB and update the live information with uses in
5668 this block. */
5669 need_asserts |= find_assert_locations_1 (bb, live[rpo[i]]);
5671 /* Merge liveness into the predecessor blocks and free it. */
5672 if (!bitmap_empty_p (live[rpo[i]]))
5674 int pred_rpo = i;
5675 FOR_EACH_EDGE (e, ei, bb->preds)
5677 int pred = e->src->index;
5678 if ((e->flags & EDGE_DFS_BACK) || pred == ENTRY_BLOCK)
5679 continue;
5681 if (!live[pred])
5683 live[pred] = sbitmap_alloc (num_ssa_names);
5684 bitmap_clear (live[pred]);
5686 bitmap_ior (live[pred], live[pred], live[rpo[i]]);
5688 if (bb_rpo[pred] < pred_rpo)
5689 pred_rpo = bb_rpo[pred];
5692 /* Record the RPO number of the last visited block that needs
5693 live information from this block. */
5694 last_rpo[rpo[i]] = pred_rpo;
5696 else
5698 sbitmap_free (live[rpo[i]]);
5699 live[rpo[i]] = NULL;
5702 /* We can free all successors live bitmaps if all their
5703 predecessors have been visited already. */
5704 FOR_EACH_EDGE (e, ei, bb->succs)
5705 if (last_rpo[e->dest->index] == i
5706 && live[e->dest->index])
5708 sbitmap_free (live[e->dest->index]);
5709 live[e->dest->index] = NULL;
5713 XDELETEVEC (rpo);
5714 XDELETEVEC (bb_rpo);
5715 XDELETEVEC (last_rpo);
5716 for (i = 0; i < last_basic_block; ++i)
5717 if (live[i])
5718 sbitmap_free (live[i]);
5719 XDELETEVEC (live);
5721 return need_asserts;
5724 /* Create an ASSERT_EXPR for NAME and insert it in the location
5725 indicated by LOC. Return true if we made any edge insertions. */
5727 static bool
5728 process_assert_insertions_for (tree name, assert_locus_t loc)
5730 /* Build the comparison expression NAME_i COMP_CODE VAL. */
5731 gimple stmt;
5732 tree cond;
5733 gimple assert_stmt;
5734 edge_iterator ei;
5735 edge e;
5737 /* If we have X <=> X do not insert an assert expr for that. */
5738 if (loc->expr == loc->val)
5739 return false;
5741 cond = build2 (loc->comp_code, boolean_type_node, loc->expr, loc->val);
5742 assert_stmt = build_assert_expr_for (cond, name);
5743 if (loc->e)
5745 /* We have been asked to insert the assertion on an edge. This
5746 is used only by COND_EXPR and SWITCH_EXPR assertions. */
5747 gcc_checking_assert (gimple_code (gsi_stmt (loc->si)) == GIMPLE_COND
5748 || (gimple_code (gsi_stmt (loc->si))
5749 == GIMPLE_SWITCH));
5751 gsi_insert_on_edge (loc->e, assert_stmt);
5752 return true;
5755 /* Otherwise, we can insert right after LOC->SI iff the
5756 statement must not be the last statement in the block. */
5757 stmt = gsi_stmt (loc->si);
5758 if (!stmt_ends_bb_p (stmt))
5760 gsi_insert_after (&loc->si, assert_stmt, GSI_SAME_STMT);
5761 return false;
5764 /* If STMT must be the last statement in BB, we can only insert new
5765 assertions on the non-abnormal edge out of BB. Note that since
5766 STMT is not control flow, there may only be one non-abnormal edge
5767 out of BB. */
5768 FOR_EACH_EDGE (e, ei, loc->bb->succs)
5769 if (!(e->flags & EDGE_ABNORMAL))
5771 gsi_insert_on_edge (e, assert_stmt);
5772 return true;
5775 gcc_unreachable ();
5779 /* Process all the insertions registered for every name N_i registered
5780 in NEED_ASSERT_FOR. The list of assertions to be inserted are
5781 found in ASSERTS_FOR[i]. */
5783 static void
5784 process_assert_insertions (void)
5786 unsigned i;
5787 bitmap_iterator bi;
5788 bool update_edges_p = false;
5789 int num_asserts = 0;
5791 if (dump_file && (dump_flags & TDF_DETAILS))
5792 dump_all_asserts (dump_file);
5794 EXECUTE_IF_SET_IN_BITMAP (need_assert_for, 0, i, bi)
5796 assert_locus_t loc = asserts_for[i];
5797 gcc_assert (loc);
5799 while (loc)
5801 assert_locus_t next = loc->next;
5802 update_edges_p |= process_assert_insertions_for (ssa_name (i), loc);
5803 free (loc);
5804 loc = next;
5805 num_asserts++;
5809 if (update_edges_p)
5810 gsi_commit_edge_inserts ();
5812 statistics_counter_event (cfun, "Number of ASSERT_EXPR expressions inserted",
5813 num_asserts);
5817 /* Traverse the flowgraph looking for conditional jumps to insert range
5818 expressions. These range expressions are meant to provide information
5819 to optimizations that need to reason in terms of value ranges. They
5820 will not be expanded into RTL. For instance, given:
5822 x = ...
5823 y = ...
5824 if (x < y)
5825 y = x - 2;
5826 else
5827 x = y + 3;
5829 this pass will transform the code into:
5831 x = ...
5832 y = ...
5833 if (x < y)
5835 x = ASSERT_EXPR <x, x < y>
5836 y = x - 2
5838 else
5840 y = ASSERT_EXPR <y, x <= y>
5841 x = y + 3
5844 The idea is that once copy and constant propagation have run, other
5845 optimizations will be able to determine what ranges of values can 'x'
5846 take in different paths of the code, simply by checking the reaching
5847 definition of 'x'. */
5849 static void
5850 insert_range_assertions (void)
5852 need_assert_for = BITMAP_ALLOC (NULL);
5853 asserts_for = XCNEWVEC (assert_locus_t, num_ssa_names);
5855 calculate_dominance_info (CDI_DOMINATORS);
5857 if (find_assert_locations ())
5859 process_assert_insertions ();
5860 update_ssa (TODO_update_ssa_no_phi);
5863 if (dump_file && (dump_flags & TDF_DETAILS))
5865 fprintf (dump_file, "\nSSA form after inserting ASSERT_EXPRs\n");
5866 dump_function_to_file (current_function_decl, dump_file, dump_flags);
5869 free (asserts_for);
5870 BITMAP_FREE (need_assert_for);
5873 /* Checks one ARRAY_REF in REF, located at LOCUS. Ignores flexible arrays
5874 and "struct" hacks. If VRP can determine that the
5875 array subscript is a constant, check if it is outside valid
5876 range. If the array subscript is a RANGE, warn if it is
5877 non-overlapping with valid range.
5878 IGNORE_OFF_BY_ONE is true if the ARRAY_REF is inside a ADDR_EXPR. */
5880 static void
5881 check_array_ref (location_t location, tree ref, bool ignore_off_by_one)
5883 value_range_t* vr = NULL;
5884 tree low_sub, up_sub;
5885 tree low_bound, up_bound, up_bound_p1;
5886 tree base;
5888 if (TREE_NO_WARNING (ref))
5889 return;
5891 low_sub = up_sub = TREE_OPERAND (ref, 1);
5892 up_bound = array_ref_up_bound (ref);
5894 /* Can not check flexible arrays. */
5895 if (!up_bound
5896 || TREE_CODE (up_bound) != INTEGER_CST)
5897 return;
5899 /* Accesses to trailing arrays via pointers may access storage
5900 beyond the types array bounds. */
5901 base = get_base_address (ref);
5902 if (base && TREE_CODE (base) == MEM_REF)
5904 tree cref, next = NULL_TREE;
5906 if (TREE_CODE (TREE_OPERAND (ref, 0)) != COMPONENT_REF)
5907 return;
5909 cref = TREE_OPERAND (ref, 0);
5910 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (cref, 0))) == RECORD_TYPE)
5911 for (next = DECL_CHAIN (TREE_OPERAND (cref, 1));
5912 next && TREE_CODE (next) != FIELD_DECL;
5913 next = DECL_CHAIN (next))
5916 /* If this is the last field in a struct type or a field in a
5917 union type do not warn. */
5918 if (!next)
5919 return;
5922 low_bound = array_ref_low_bound (ref);
5923 up_bound_p1 = int_const_binop (PLUS_EXPR, up_bound, integer_one_node);
5925 if (TREE_CODE (low_sub) == SSA_NAME)
5927 vr = get_value_range (low_sub);
5928 if (vr->type == VR_RANGE || vr->type == VR_ANTI_RANGE)
5930 low_sub = vr->type == VR_RANGE ? vr->max : vr->min;
5931 up_sub = vr->type == VR_RANGE ? vr->min : vr->max;
5935 if (vr && vr->type == VR_ANTI_RANGE)
5937 if (TREE_CODE (up_sub) == INTEGER_CST
5938 && tree_int_cst_lt (up_bound, up_sub)
5939 && TREE_CODE (low_sub) == INTEGER_CST
5940 && tree_int_cst_lt (low_sub, low_bound))
5942 warning_at (location, OPT_Warray_bounds,
5943 "array subscript is outside array bounds");
5944 TREE_NO_WARNING (ref) = 1;
5947 else if (TREE_CODE (up_sub) == INTEGER_CST
5948 && (ignore_off_by_one
5949 ? (tree_int_cst_lt (up_bound, up_sub)
5950 && !tree_int_cst_equal (up_bound_p1, up_sub))
5951 : (tree_int_cst_lt (up_bound, up_sub)
5952 || tree_int_cst_equal (up_bound_p1, up_sub))))
5954 warning_at (location, OPT_Warray_bounds,
5955 "array subscript is above array bounds");
5956 TREE_NO_WARNING (ref) = 1;
5958 else if (TREE_CODE (low_sub) == INTEGER_CST
5959 && tree_int_cst_lt (low_sub, low_bound))
5961 warning_at (location, OPT_Warray_bounds,
5962 "array subscript is below array bounds");
5963 TREE_NO_WARNING (ref) = 1;
5967 /* Searches if the expr T, located at LOCATION computes
5968 address of an ARRAY_REF, and call check_array_ref on it. */
5970 static void
5971 search_for_addr_array (tree t, location_t location)
5973 while (TREE_CODE (t) == SSA_NAME)
5975 gimple g = SSA_NAME_DEF_STMT (t);
5977 if (gimple_code (g) != GIMPLE_ASSIGN)
5978 return;
5980 if (get_gimple_rhs_class (gimple_assign_rhs_code (g))
5981 != GIMPLE_SINGLE_RHS)
5982 return;
5984 t = gimple_assign_rhs1 (g);
5988 /* We are only interested in addresses of ARRAY_REF's. */
5989 if (TREE_CODE (t) != ADDR_EXPR)
5990 return;
5992 /* Check each ARRAY_REFs in the reference chain. */
5995 if (TREE_CODE (t) == ARRAY_REF)
5996 check_array_ref (location, t, true /*ignore_off_by_one*/);
5998 t = TREE_OPERAND (t, 0);
6000 while (handled_component_p (t));
6002 if (TREE_CODE (t) == MEM_REF
6003 && TREE_CODE (TREE_OPERAND (t, 0)) == ADDR_EXPR
6004 && !TREE_NO_WARNING (t))
6006 tree tem = TREE_OPERAND (TREE_OPERAND (t, 0), 0);
6007 tree low_bound, up_bound, el_sz;
6008 double_int idx;
6009 if (TREE_CODE (TREE_TYPE (tem)) != ARRAY_TYPE
6010 || TREE_CODE (TREE_TYPE (TREE_TYPE (tem))) == ARRAY_TYPE
6011 || !TYPE_DOMAIN (TREE_TYPE (tem)))
6012 return;
6014 low_bound = TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (tem)));
6015 up_bound = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (tem)));
6016 el_sz = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (tem)));
6017 if (!low_bound
6018 || TREE_CODE (low_bound) != INTEGER_CST
6019 || !up_bound
6020 || TREE_CODE (up_bound) != INTEGER_CST
6021 || !el_sz
6022 || TREE_CODE (el_sz) != INTEGER_CST)
6023 return;
6025 idx = mem_ref_offset (t);
6026 idx = idx.sdiv (tree_to_double_int (el_sz), TRUNC_DIV_EXPR);
6027 if (idx.slt (double_int_zero))
6029 warning_at (location, OPT_Warray_bounds,
6030 "array subscript is below array bounds");
6031 TREE_NO_WARNING (t) = 1;
6033 else if (idx.sgt (tree_to_double_int (up_bound)
6034 - tree_to_double_int (low_bound)
6035 + double_int_one))
6037 warning_at (location, OPT_Warray_bounds,
6038 "array subscript is above array bounds");
6039 TREE_NO_WARNING (t) = 1;
6044 /* walk_tree() callback that checks if *TP is
6045 an ARRAY_REF inside an ADDR_EXPR (in which an array
6046 subscript one outside the valid range is allowed). Call
6047 check_array_ref for each ARRAY_REF found. The location is
6048 passed in DATA. */
6050 static tree
6051 check_array_bounds (tree *tp, int *walk_subtree, void *data)
6053 tree t = *tp;
6054 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
6055 location_t location;
6057 if (EXPR_HAS_LOCATION (t))
6058 location = EXPR_LOCATION (t);
6059 else
6061 location_t *locp = (location_t *) wi->info;
6062 location = *locp;
6065 *walk_subtree = TRUE;
6067 if (TREE_CODE (t) == ARRAY_REF)
6068 check_array_ref (location, t, false /*ignore_off_by_one*/);
6070 if (TREE_CODE (t) == MEM_REF
6071 || (TREE_CODE (t) == RETURN_EXPR && TREE_OPERAND (t, 0)))
6072 search_for_addr_array (TREE_OPERAND (t, 0), location);
6074 if (TREE_CODE (t) == ADDR_EXPR)
6075 *walk_subtree = FALSE;
6077 return NULL_TREE;
6080 /* Walk over all statements of all reachable BBs and call check_array_bounds
6081 on them. */
6083 static void
6084 check_all_array_refs (void)
6086 basic_block bb;
6087 gimple_stmt_iterator si;
6089 FOR_EACH_BB (bb)
6091 edge_iterator ei;
6092 edge e;
6093 bool executable = false;
6095 /* Skip blocks that were found to be unreachable. */
6096 FOR_EACH_EDGE (e, ei, bb->preds)
6097 executable |= !!(e->flags & EDGE_EXECUTABLE);
6098 if (!executable)
6099 continue;
6101 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
6103 gimple stmt = gsi_stmt (si);
6104 struct walk_stmt_info wi;
6105 if (!gimple_has_location (stmt))
6106 continue;
6108 if (is_gimple_call (stmt))
6110 size_t i;
6111 size_t n = gimple_call_num_args (stmt);
6112 for (i = 0; i < n; i++)
6114 tree arg = gimple_call_arg (stmt, i);
6115 search_for_addr_array (arg, gimple_location (stmt));
6118 else
6120 memset (&wi, 0, sizeof (wi));
6121 wi.info = CONST_CAST (void *, (const void *)
6122 gimple_location_ptr (stmt));
6124 walk_gimple_op (gsi_stmt (si),
6125 check_array_bounds,
6126 &wi);
6132 /* Convert range assertion expressions into the implied copies and
6133 copy propagate away the copies. Doing the trivial copy propagation
6134 here avoids the need to run the full copy propagation pass after
6135 VRP.
6137 FIXME, this will eventually lead to copy propagation removing the
6138 names that had useful range information attached to them. For
6139 instance, if we had the assertion N_i = ASSERT_EXPR <N_j, N_j > 3>,
6140 then N_i will have the range [3, +INF].
6142 However, by converting the assertion into the implied copy
6143 operation N_i = N_j, we will then copy-propagate N_j into the uses
6144 of N_i and lose the range information. We may want to hold on to
6145 ASSERT_EXPRs a little while longer as the ranges could be used in
6146 things like jump threading.
6148 The problem with keeping ASSERT_EXPRs around is that passes after
6149 VRP need to handle them appropriately.
6151 Another approach would be to make the range information a first
6152 class property of the SSA_NAME so that it can be queried from
6153 any pass. This is made somewhat more complex by the need for
6154 multiple ranges to be associated with one SSA_NAME. */
6156 static void
6157 remove_range_assertions (void)
6159 basic_block bb;
6160 gimple_stmt_iterator si;
6162 /* Note that the BSI iterator bump happens at the bottom of the
6163 loop and no bump is necessary if we're removing the statement
6164 referenced by the current BSI. */
6165 FOR_EACH_BB (bb)
6166 for (si = gsi_start_bb (bb); !gsi_end_p (si);)
6168 gimple stmt = gsi_stmt (si);
6169 gimple use_stmt;
6171 if (is_gimple_assign (stmt)
6172 && gimple_assign_rhs_code (stmt) == ASSERT_EXPR)
6174 tree rhs = gimple_assign_rhs1 (stmt);
6175 tree var;
6176 tree cond = fold (ASSERT_EXPR_COND (rhs));
6177 use_operand_p use_p;
6178 imm_use_iterator iter;
6180 gcc_assert (cond != boolean_false_node);
6182 /* Propagate the RHS into every use of the LHS. */
6183 var = ASSERT_EXPR_VAR (rhs);
6184 FOR_EACH_IMM_USE_STMT (use_stmt, iter,
6185 gimple_assign_lhs (stmt))
6186 FOR_EACH_IMM_USE_ON_STMT (use_p, iter)
6188 SET_USE (use_p, var);
6189 gcc_assert (TREE_CODE (var) == SSA_NAME);
6192 /* And finally, remove the copy, it is not needed. */
6193 gsi_remove (&si, true);
6194 release_defs (stmt);
6196 else
6197 gsi_next (&si);
6202 /* Return true if STMT is interesting for VRP. */
6204 static bool
6205 stmt_interesting_for_vrp (gimple stmt)
6207 if (gimple_code (stmt) == GIMPLE_PHI)
6209 tree res = gimple_phi_result (stmt);
6210 return (!virtual_operand_p (res)
6211 && (INTEGRAL_TYPE_P (TREE_TYPE (res))
6212 || POINTER_TYPE_P (TREE_TYPE (res))));
6214 else if (is_gimple_assign (stmt) || is_gimple_call (stmt))
6216 tree lhs = gimple_get_lhs (stmt);
6218 /* In general, assignments with virtual operands are not useful
6219 for deriving ranges, with the obvious exception of calls to
6220 builtin functions. */
6221 if (lhs && TREE_CODE (lhs) == SSA_NAME
6222 && (INTEGRAL_TYPE_P (TREE_TYPE (lhs))
6223 || POINTER_TYPE_P (TREE_TYPE (lhs)))
6224 && ((is_gimple_call (stmt)
6225 && gimple_call_fndecl (stmt) != NULL_TREE
6226 && DECL_BUILT_IN (gimple_call_fndecl (stmt)))
6227 || !gimple_vuse (stmt)))
6228 return true;
6230 else if (gimple_code (stmt) == GIMPLE_COND
6231 || gimple_code (stmt) == GIMPLE_SWITCH)
6232 return true;
6234 return false;
6238 /* Initialize local data structures for VRP. */
6240 static void
6241 vrp_initialize (void)
6243 basic_block bb;
6245 values_propagated = false;
6246 num_vr_values = num_ssa_names;
6247 vr_value = XCNEWVEC (value_range_t *, num_vr_values);
6248 vr_phi_edge_counts = XCNEWVEC (int, num_ssa_names);
6250 FOR_EACH_BB (bb)
6252 gimple_stmt_iterator si;
6254 for (si = gsi_start_phis (bb); !gsi_end_p (si); gsi_next (&si))
6256 gimple phi = gsi_stmt (si);
6257 if (!stmt_interesting_for_vrp (phi))
6259 tree lhs = PHI_RESULT (phi);
6260 set_value_range_to_varying (get_value_range (lhs));
6261 prop_set_simulate_again (phi, false);
6263 else
6264 prop_set_simulate_again (phi, true);
6267 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
6269 gimple stmt = gsi_stmt (si);
6271 /* If the statement is a control insn, then we do not
6272 want to avoid simulating the statement once. Failure
6273 to do so means that those edges will never get added. */
6274 if (stmt_ends_bb_p (stmt))
6275 prop_set_simulate_again (stmt, true);
6276 else if (!stmt_interesting_for_vrp (stmt))
6278 ssa_op_iter i;
6279 tree def;
6280 FOR_EACH_SSA_TREE_OPERAND (def, stmt, i, SSA_OP_DEF)
6281 set_value_range_to_varying (get_value_range (def));
6282 prop_set_simulate_again (stmt, false);
6284 else
6285 prop_set_simulate_again (stmt, true);
6290 /* Return the singleton value-range for NAME or NAME. */
6292 static inline tree
6293 vrp_valueize (tree name)
6295 if (TREE_CODE (name) == SSA_NAME)
6297 value_range_t *vr = get_value_range (name);
6298 if (vr->type == VR_RANGE
6299 && (vr->min == vr->max
6300 || operand_equal_p (vr->min, vr->max, 0)))
6301 return vr->min;
6303 return name;
6306 /* Visit assignment STMT. If it produces an interesting range, record
6307 the SSA name in *OUTPUT_P. */
6309 static enum ssa_prop_result
6310 vrp_visit_assignment_or_call (gimple stmt, tree *output_p)
6312 tree def, lhs;
6313 ssa_op_iter iter;
6314 enum gimple_code code = gimple_code (stmt);
6315 lhs = gimple_get_lhs (stmt);
6317 /* We only keep track of ranges in integral and pointer types. */
6318 if (TREE_CODE (lhs) == SSA_NAME
6319 && ((INTEGRAL_TYPE_P (TREE_TYPE (lhs))
6320 /* It is valid to have NULL MIN/MAX values on a type. See
6321 build_range_type. */
6322 && TYPE_MIN_VALUE (TREE_TYPE (lhs))
6323 && TYPE_MAX_VALUE (TREE_TYPE (lhs)))
6324 || POINTER_TYPE_P (TREE_TYPE (lhs))))
6326 value_range_t new_vr = VR_INITIALIZER;
6328 /* Try folding the statement to a constant first. */
6329 tree tem = gimple_fold_stmt_to_constant (stmt, vrp_valueize);
6330 if (tem && !is_overflow_infinity (tem))
6331 set_value_range (&new_vr, VR_RANGE, tem, tem, NULL);
6332 /* Then dispatch to value-range extracting functions. */
6333 else if (code == GIMPLE_CALL)
6334 extract_range_basic (&new_vr, stmt);
6335 else
6336 extract_range_from_assignment (&new_vr, stmt);
6338 if (update_value_range (lhs, &new_vr))
6340 *output_p = lhs;
6342 if (dump_file && (dump_flags & TDF_DETAILS))
6344 fprintf (dump_file, "Found new range for ");
6345 print_generic_expr (dump_file, lhs, 0);
6346 fprintf (dump_file, ": ");
6347 dump_value_range (dump_file, &new_vr);
6348 fprintf (dump_file, "\n\n");
6351 if (new_vr.type == VR_VARYING)
6352 return SSA_PROP_VARYING;
6354 return SSA_PROP_INTERESTING;
6357 return SSA_PROP_NOT_INTERESTING;
6360 /* Every other statement produces no useful ranges. */
6361 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_DEF)
6362 set_value_range_to_varying (get_value_range (def));
6364 return SSA_PROP_VARYING;
6367 /* Helper that gets the value range of the SSA_NAME with version I
6368 or a symbolic range containing the SSA_NAME only if the value range
6369 is varying or undefined. */
6371 static inline value_range_t
6372 get_vr_for_comparison (int i)
6374 value_range_t vr = *get_value_range (ssa_name (i));
6376 /* If name N_i does not have a valid range, use N_i as its own
6377 range. This allows us to compare against names that may
6378 have N_i in their ranges. */
6379 if (vr.type == VR_VARYING || vr.type == VR_UNDEFINED)
6381 vr.type = VR_RANGE;
6382 vr.min = ssa_name (i);
6383 vr.max = ssa_name (i);
6386 return vr;
6389 /* Compare all the value ranges for names equivalent to VAR with VAL
6390 using comparison code COMP. Return the same value returned by
6391 compare_range_with_value, including the setting of
6392 *STRICT_OVERFLOW_P. */
6394 static tree
6395 compare_name_with_value (enum tree_code comp, tree var, tree val,
6396 bool *strict_overflow_p)
6398 bitmap_iterator bi;
6399 unsigned i;
6400 bitmap e;
6401 tree retval, t;
6402 int used_strict_overflow;
6403 bool sop;
6404 value_range_t equiv_vr;
6406 /* Get the set of equivalences for VAR. */
6407 e = get_value_range (var)->equiv;
6409 /* Start at -1. Set it to 0 if we do a comparison without relying
6410 on overflow, or 1 if all comparisons rely on overflow. */
6411 used_strict_overflow = -1;
6413 /* Compare vars' value range with val. */
6414 equiv_vr = get_vr_for_comparison (SSA_NAME_VERSION (var));
6415 sop = false;
6416 retval = compare_range_with_value (comp, &equiv_vr, val, &sop);
6417 if (retval)
6418 used_strict_overflow = sop ? 1 : 0;
6420 /* If the equiv set is empty we have done all work we need to do. */
6421 if (e == NULL)
6423 if (retval
6424 && used_strict_overflow > 0)
6425 *strict_overflow_p = true;
6426 return retval;
6429 EXECUTE_IF_SET_IN_BITMAP (e, 0, i, bi)
6431 equiv_vr = get_vr_for_comparison (i);
6432 sop = false;
6433 t = compare_range_with_value (comp, &equiv_vr, val, &sop);
6434 if (t)
6436 /* If we get different answers from different members
6437 of the equivalence set this check must be in a dead
6438 code region. Folding it to a trap representation
6439 would be correct here. For now just return don't-know. */
6440 if (retval != NULL
6441 && t != retval)
6443 retval = NULL_TREE;
6444 break;
6446 retval = t;
6448 if (!sop)
6449 used_strict_overflow = 0;
6450 else if (used_strict_overflow < 0)
6451 used_strict_overflow = 1;
6455 if (retval
6456 && used_strict_overflow > 0)
6457 *strict_overflow_p = true;
6459 return retval;
6463 /* Given a comparison code COMP and names N1 and N2, compare all the
6464 ranges equivalent to N1 against all the ranges equivalent to N2
6465 to determine the value of N1 COMP N2. Return the same value
6466 returned by compare_ranges. Set *STRICT_OVERFLOW_P to indicate
6467 whether we relied on an overflow infinity in the comparison. */
6470 static tree
6471 compare_names (enum tree_code comp, tree n1, tree n2,
6472 bool *strict_overflow_p)
6474 tree t, retval;
6475 bitmap e1, e2;
6476 bitmap_iterator bi1, bi2;
6477 unsigned i1, i2;
6478 int used_strict_overflow;
6479 static bitmap_obstack *s_obstack = NULL;
6480 static bitmap s_e1 = NULL, s_e2 = NULL;
6482 /* Compare the ranges of every name equivalent to N1 against the
6483 ranges of every name equivalent to N2. */
6484 e1 = get_value_range (n1)->equiv;
6485 e2 = get_value_range (n2)->equiv;
6487 /* Use the fake bitmaps if e1 or e2 are not available. */
6488 if (s_obstack == NULL)
6490 s_obstack = XNEW (bitmap_obstack);
6491 bitmap_obstack_initialize (s_obstack);
6492 s_e1 = BITMAP_ALLOC (s_obstack);
6493 s_e2 = BITMAP_ALLOC (s_obstack);
6495 if (e1 == NULL)
6496 e1 = s_e1;
6497 if (e2 == NULL)
6498 e2 = s_e2;
6500 /* Add N1 and N2 to their own set of equivalences to avoid
6501 duplicating the body of the loop just to check N1 and N2
6502 ranges. */
6503 bitmap_set_bit (e1, SSA_NAME_VERSION (n1));
6504 bitmap_set_bit (e2, SSA_NAME_VERSION (n2));
6506 /* If the equivalence sets have a common intersection, then the two
6507 names can be compared without checking their ranges. */
6508 if (bitmap_intersect_p (e1, e2))
6510 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
6511 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
6513 return (comp == EQ_EXPR || comp == GE_EXPR || comp == LE_EXPR)
6514 ? boolean_true_node
6515 : boolean_false_node;
6518 /* Start at -1. Set it to 0 if we do a comparison without relying
6519 on overflow, or 1 if all comparisons rely on overflow. */
6520 used_strict_overflow = -1;
6522 /* Otherwise, compare all the equivalent ranges. First, add N1 and
6523 N2 to their own set of equivalences to avoid duplicating the body
6524 of the loop just to check N1 and N2 ranges. */
6525 EXECUTE_IF_SET_IN_BITMAP (e1, 0, i1, bi1)
6527 value_range_t vr1 = get_vr_for_comparison (i1);
6529 t = retval = NULL_TREE;
6530 EXECUTE_IF_SET_IN_BITMAP (e2, 0, i2, bi2)
6532 bool sop = false;
6534 value_range_t vr2 = get_vr_for_comparison (i2);
6536 t = compare_ranges (comp, &vr1, &vr2, &sop);
6537 if (t)
6539 /* If we get different answers from different members
6540 of the equivalence set this check must be in a dead
6541 code region. Folding it to a trap representation
6542 would be correct here. For now just return don't-know. */
6543 if (retval != NULL
6544 && t != retval)
6546 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
6547 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
6548 return NULL_TREE;
6550 retval = t;
6552 if (!sop)
6553 used_strict_overflow = 0;
6554 else if (used_strict_overflow < 0)
6555 used_strict_overflow = 1;
6559 if (retval)
6561 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
6562 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
6563 if (used_strict_overflow > 0)
6564 *strict_overflow_p = true;
6565 return retval;
6569 /* None of the equivalent ranges are useful in computing this
6570 comparison. */
6571 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
6572 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
6573 return NULL_TREE;
6576 /* Helper function for vrp_evaluate_conditional_warnv. */
6578 static tree
6579 vrp_evaluate_conditional_warnv_with_ops_using_ranges (enum tree_code code,
6580 tree op0, tree op1,
6581 bool * strict_overflow_p)
6583 value_range_t *vr0, *vr1;
6585 vr0 = (TREE_CODE (op0) == SSA_NAME) ? get_value_range (op0) : NULL;
6586 vr1 = (TREE_CODE (op1) == SSA_NAME) ? get_value_range (op1) : NULL;
6588 if (vr0 && vr1)
6589 return compare_ranges (code, vr0, vr1, strict_overflow_p);
6590 else if (vr0 && vr1 == NULL)
6591 return compare_range_with_value (code, vr0, op1, strict_overflow_p);
6592 else if (vr0 == NULL && vr1)
6593 return (compare_range_with_value
6594 (swap_tree_comparison (code), vr1, op0, strict_overflow_p));
6595 return NULL;
6598 /* Helper function for vrp_evaluate_conditional_warnv. */
6600 static tree
6601 vrp_evaluate_conditional_warnv_with_ops (enum tree_code code, tree op0,
6602 tree op1, bool use_equiv_p,
6603 bool *strict_overflow_p, bool *only_ranges)
6605 tree ret;
6606 if (only_ranges)
6607 *only_ranges = true;
6609 /* We only deal with integral and pointer types. */
6610 if (!INTEGRAL_TYPE_P (TREE_TYPE (op0))
6611 && !POINTER_TYPE_P (TREE_TYPE (op0)))
6612 return NULL_TREE;
6614 if (use_equiv_p)
6616 if (only_ranges
6617 && (ret = vrp_evaluate_conditional_warnv_with_ops_using_ranges
6618 (code, op0, op1, strict_overflow_p)))
6619 return ret;
6620 *only_ranges = false;
6621 if (TREE_CODE (op0) == SSA_NAME && TREE_CODE (op1) == SSA_NAME)
6622 return compare_names (code, op0, op1, strict_overflow_p);
6623 else if (TREE_CODE (op0) == SSA_NAME)
6624 return compare_name_with_value (code, op0, op1, strict_overflow_p);
6625 else if (TREE_CODE (op1) == SSA_NAME)
6626 return (compare_name_with_value
6627 (swap_tree_comparison (code), op1, op0, strict_overflow_p));
6629 else
6630 return vrp_evaluate_conditional_warnv_with_ops_using_ranges (code, op0, op1,
6631 strict_overflow_p);
6632 return NULL_TREE;
6635 /* Given (CODE OP0 OP1) within STMT, try to simplify it based on value range
6636 information. Return NULL if the conditional can not be evaluated.
6637 The ranges of all the names equivalent with the operands in COND
6638 will be used when trying to compute the value. If the result is
6639 based on undefined signed overflow, issue a warning if
6640 appropriate. */
6642 static tree
6643 vrp_evaluate_conditional (enum tree_code code, tree op0, tree op1, gimple stmt)
6645 bool sop;
6646 tree ret;
6647 bool only_ranges;
6649 /* Some passes and foldings leak constants with overflow flag set
6650 into the IL. Avoid doing wrong things with these and bail out. */
6651 if ((TREE_CODE (op0) == INTEGER_CST
6652 && TREE_OVERFLOW (op0))
6653 || (TREE_CODE (op1) == INTEGER_CST
6654 && TREE_OVERFLOW (op1)))
6655 return NULL_TREE;
6657 sop = false;
6658 ret = vrp_evaluate_conditional_warnv_with_ops (code, op0, op1, true, &sop,
6659 &only_ranges);
6661 if (ret && sop)
6663 enum warn_strict_overflow_code wc;
6664 const char* warnmsg;
6666 if (is_gimple_min_invariant (ret))
6668 wc = WARN_STRICT_OVERFLOW_CONDITIONAL;
6669 warnmsg = G_("assuming signed overflow does not occur when "
6670 "simplifying conditional to constant");
6672 else
6674 wc = WARN_STRICT_OVERFLOW_COMPARISON;
6675 warnmsg = G_("assuming signed overflow does not occur when "
6676 "simplifying conditional");
6679 if (issue_strict_overflow_warning (wc))
6681 location_t location;
6683 if (!gimple_has_location (stmt))
6684 location = input_location;
6685 else
6686 location = gimple_location (stmt);
6687 warning_at (location, OPT_Wstrict_overflow, "%s", warnmsg);
6691 if (warn_type_limits
6692 && ret && only_ranges
6693 && TREE_CODE_CLASS (code) == tcc_comparison
6694 && TREE_CODE (op0) == SSA_NAME)
6696 /* If the comparison is being folded and the operand on the LHS
6697 is being compared against a constant value that is outside of
6698 the natural range of OP0's type, then the predicate will
6699 always fold regardless of the value of OP0. If -Wtype-limits
6700 was specified, emit a warning. */
6701 tree type = TREE_TYPE (op0);
6702 value_range_t *vr0 = get_value_range (op0);
6704 if (vr0->type != VR_VARYING
6705 && INTEGRAL_TYPE_P (type)
6706 && vrp_val_is_min (vr0->min)
6707 && vrp_val_is_max (vr0->max)
6708 && is_gimple_min_invariant (op1))
6710 location_t location;
6712 if (!gimple_has_location (stmt))
6713 location = input_location;
6714 else
6715 location = gimple_location (stmt);
6717 warning_at (location, OPT_Wtype_limits,
6718 integer_zerop (ret)
6719 ? G_("comparison always false "
6720 "due to limited range of data type")
6721 : G_("comparison always true "
6722 "due to limited range of data type"));
6726 return ret;
6730 /* Visit conditional statement STMT. If we can determine which edge
6731 will be taken out of STMT's basic block, record it in
6732 *TAKEN_EDGE_P and return SSA_PROP_INTERESTING. Otherwise, return
6733 SSA_PROP_VARYING. */
6735 static enum ssa_prop_result
6736 vrp_visit_cond_stmt (gimple stmt, edge *taken_edge_p)
6738 tree val;
6739 bool sop;
6741 *taken_edge_p = NULL;
6743 if (dump_file && (dump_flags & TDF_DETAILS))
6745 tree use;
6746 ssa_op_iter i;
6748 fprintf (dump_file, "\nVisiting conditional with predicate: ");
6749 print_gimple_stmt (dump_file, stmt, 0, 0);
6750 fprintf (dump_file, "\nWith known ranges\n");
6752 FOR_EACH_SSA_TREE_OPERAND (use, stmt, i, SSA_OP_USE)
6754 fprintf (dump_file, "\t");
6755 print_generic_expr (dump_file, use, 0);
6756 fprintf (dump_file, ": ");
6757 dump_value_range (dump_file, vr_value[SSA_NAME_VERSION (use)]);
6760 fprintf (dump_file, "\n");
6763 /* Compute the value of the predicate COND by checking the known
6764 ranges of each of its operands.
6766 Note that we cannot evaluate all the equivalent ranges here
6767 because those ranges may not yet be final and with the current
6768 propagation strategy, we cannot determine when the value ranges
6769 of the names in the equivalence set have changed.
6771 For instance, given the following code fragment
6773 i_5 = PHI <8, i_13>
6775 i_14 = ASSERT_EXPR <i_5, i_5 != 0>
6776 if (i_14 == 1)
6779 Assume that on the first visit to i_14, i_5 has the temporary
6780 range [8, 8] because the second argument to the PHI function is
6781 not yet executable. We derive the range ~[0, 0] for i_14 and the
6782 equivalence set { i_5 }. So, when we visit 'if (i_14 == 1)' for
6783 the first time, since i_14 is equivalent to the range [8, 8], we
6784 determine that the predicate is always false.
6786 On the next round of propagation, i_13 is determined to be
6787 VARYING, which causes i_5 to drop down to VARYING. So, another
6788 visit to i_14 is scheduled. In this second visit, we compute the
6789 exact same range and equivalence set for i_14, namely ~[0, 0] and
6790 { i_5 }. But we did not have the previous range for i_5
6791 registered, so vrp_visit_assignment thinks that the range for
6792 i_14 has not changed. Therefore, the predicate 'if (i_14 == 1)'
6793 is not visited again, which stops propagation from visiting
6794 statements in the THEN clause of that if().
6796 To properly fix this we would need to keep the previous range
6797 value for the names in the equivalence set. This way we would've
6798 discovered that from one visit to the other i_5 changed from
6799 range [8, 8] to VR_VARYING.
6801 However, fixing this apparent limitation may not be worth the
6802 additional checking. Testing on several code bases (GCC, DLV,
6803 MICO, TRAMP3D and SPEC2000) showed that doing this results in
6804 4 more predicates folded in SPEC. */
6805 sop = false;
6807 val = vrp_evaluate_conditional_warnv_with_ops (gimple_cond_code (stmt),
6808 gimple_cond_lhs (stmt),
6809 gimple_cond_rhs (stmt),
6810 false, &sop, NULL);
6811 if (val)
6813 if (!sop)
6814 *taken_edge_p = find_taken_edge (gimple_bb (stmt), val);
6815 else
6817 if (dump_file && (dump_flags & TDF_DETAILS))
6818 fprintf (dump_file,
6819 "\nIgnoring predicate evaluation because "
6820 "it assumes that signed overflow is undefined");
6821 val = NULL_TREE;
6825 if (dump_file && (dump_flags & TDF_DETAILS))
6827 fprintf (dump_file, "\nPredicate evaluates to: ");
6828 if (val == NULL_TREE)
6829 fprintf (dump_file, "DON'T KNOW\n");
6830 else
6831 print_generic_stmt (dump_file, val, 0);
6834 return (*taken_edge_p) ? SSA_PROP_INTERESTING : SSA_PROP_VARYING;
6837 /* Searches the case label vector VEC for the index *IDX of the CASE_LABEL
6838 that includes the value VAL. The search is restricted to the range
6839 [START_IDX, n - 1] where n is the size of VEC.
6841 If there is a CASE_LABEL for VAL, its index is placed in IDX and true is
6842 returned.
6844 If there is no CASE_LABEL for VAL and there is one that is larger than VAL,
6845 it is placed in IDX and false is returned.
6847 If VAL is larger than any CASE_LABEL, n is placed on IDX and false is
6848 returned. */
6850 static bool
6851 find_case_label_index (gimple stmt, size_t start_idx, tree val, size_t *idx)
6853 size_t n = gimple_switch_num_labels (stmt);
6854 size_t low, high;
6856 /* Find case label for minimum of the value range or the next one.
6857 At each iteration we are searching in [low, high - 1]. */
6859 for (low = start_idx, high = n; high != low; )
6861 tree t;
6862 int cmp;
6863 /* Note that i != high, so we never ask for n. */
6864 size_t i = (high + low) / 2;
6865 t = gimple_switch_label (stmt, i);
6867 /* Cache the result of comparing CASE_LOW and val. */
6868 cmp = tree_int_cst_compare (CASE_LOW (t), val);
6870 if (cmp == 0)
6872 /* Ranges cannot be empty. */
6873 *idx = i;
6874 return true;
6876 else if (cmp > 0)
6877 high = i;
6878 else
6880 low = i + 1;
6881 if (CASE_HIGH (t) != NULL
6882 && tree_int_cst_compare (CASE_HIGH (t), val) >= 0)
6884 *idx = i;
6885 return true;
6890 *idx = high;
6891 return false;
6894 /* Searches the case label vector VEC for the range of CASE_LABELs that is used
6895 for values between MIN and MAX. The first index is placed in MIN_IDX. The
6896 last index is placed in MAX_IDX. If the range of CASE_LABELs is empty
6897 then MAX_IDX < MIN_IDX.
6898 Returns true if the default label is not needed. */
6900 static bool
6901 find_case_label_range (gimple stmt, tree min, tree max, size_t *min_idx,
6902 size_t *max_idx)
6904 size_t i, j;
6905 bool min_take_default = !find_case_label_index (stmt, 1, min, &i);
6906 bool max_take_default = !find_case_label_index (stmt, i, max, &j);
6908 if (i == j
6909 && min_take_default
6910 && max_take_default)
6912 /* Only the default case label reached.
6913 Return an empty range. */
6914 *min_idx = 1;
6915 *max_idx = 0;
6916 return false;
6918 else
6920 bool take_default = min_take_default || max_take_default;
6921 tree low, high;
6922 size_t k;
6924 if (max_take_default)
6925 j--;
6927 /* If the case label range is continuous, we do not need
6928 the default case label. Verify that. */
6929 high = CASE_LOW (gimple_switch_label (stmt, i));
6930 if (CASE_HIGH (gimple_switch_label (stmt, i)))
6931 high = CASE_HIGH (gimple_switch_label (stmt, i));
6932 for (k = i + 1; k <= j; ++k)
6934 low = CASE_LOW (gimple_switch_label (stmt, k));
6935 if (!integer_onep (int_const_binop (MINUS_EXPR, low, high)))
6937 take_default = true;
6938 break;
6940 high = low;
6941 if (CASE_HIGH (gimple_switch_label (stmt, k)))
6942 high = CASE_HIGH (gimple_switch_label (stmt, k));
6945 *min_idx = i;
6946 *max_idx = j;
6947 return !take_default;
6951 /* Searches the case label vector VEC for the ranges of CASE_LABELs that are
6952 used in range VR. The indices are placed in MIN_IDX1, MAX_IDX, MIN_IDX2 and
6953 MAX_IDX2. If the ranges of CASE_LABELs are empty then MAX_IDX1 < MIN_IDX1.
6954 Returns true if the default label is not needed. */
6956 static bool
6957 find_case_label_ranges (gimple stmt, value_range_t *vr, size_t *min_idx1,
6958 size_t *max_idx1, size_t *min_idx2,
6959 size_t *max_idx2)
6961 size_t i, j, k, l;
6962 unsigned int n = gimple_switch_num_labels (stmt);
6963 bool take_default;
6964 tree case_low, case_high;
6965 tree min = vr->min, max = vr->max;
6967 gcc_checking_assert (vr->type == VR_RANGE || vr->type == VR_ANTI_RANGE);
6969 take_default = !find_case_label_range (stmt, min, max, &i, &j);
6971 /* Set second range to emtpy. */
6972 *min_idx2 = 1;
6973 *max_idx2 = 0;
6975 if (vr->type == VR_RANGE)
6977 *min_idx1 = i;
6978 *max_idx1 = j;
6979 return !take_default;
6982 /* Set first range to all case labels. */
6983 *min_idx1 = 1;
6984 *max_idx1 = n - 1;
6986 if (i > j)
6987 return false;
6989 /* Make sure all the values of case labels [i , j] are contained in
6990 range [MIN, MAX]. */
6991 case_low = CASE_LOW (gimple_switch_label (stmt, i));
6992 case_high = CASE_HIGH (gimple_switch_label (stmt, j));
6993 if (tree_int_cst_compare (case_low, min) < 0)
6994 i += 1;
6995 if (case_high != NULL_TREE
6996 && tree_int_cst_compare (max, case_high) < 0)
6997 j -= 1;
6999 if (i > j)
7000 return false;
7002 /* If the range spans case labels [i, j], the corresponding anti-range spans
7003 the labels [1, i - 1] and [j + 1, n - 1]. */
7004 k = j + 1;
7005 l = n - 1;
7006 if (k > l)
7008 k = 1;
7009 l = 0;
7012 j = i - 1;
7013 i = 1;
7014 if (i > j)
7016 i = k;
7017 j = l;
7018 k = 1;
7019 l = 0;
7022 *min_idx1 = i;
7023 *max_idx1 = j;
7024 *min_idx2 = k;
7025 *max_idx2 = l;
7026 return false;
7029 /* Visit switch statement STMT. If we can determine which edge
7030 will be taken out of STMT's basic block, record it in
7031 *TAKEN_EDGE_P and return SSA_PROP_INTERESTING. Otherwise, return
7032 SSA_PROP_VARYING. */
7034 static enum ssa_prop_result
7035 vrp_visit_switch_stmt (gimple stmt, edge *taken_edge_p)
7037 tree op, val;
7038 value_range_t *vr;
7039 size_t i = 0, j = 0, k, l;
7040 bool take_default;
7042 *taken_edge_p = NULL;
7043 op = gimple_switch_index (stmt);
7044 if (TREE_CODE (op) != SSA_NAME)
7045 return SSA_PROP_VARYING;
7047 vr = get_value_range (op);
7048 if (dump_file && (dump_flags & TDF_DETAILS))
7050 fprintf (dump_file, "\nVisiting switch expression with operand ");
7051 print_generic_expr (dump_file, op, 0);
7052 fprintf (dump_file, " with known range ");
7053 dump_value_range (dump_file, vr);
7054 fprintf (dump_file, "\n");
7057 if ((vr->type != VR_RANGE
7058 && vr->type != VR_ANTI_RANGE)
7059 || symbolic_range_p (vr))
7060 return SSA_PROP_VARYING;
7062 /* Find the single edge that is taken from the switch expression. */
7063 take_default = !find_case_label_ranges (stmt, vr, &i, &j, &k, &l);
7065 /* Check if the range spans no CASE_LABEL. If so, we only reach the default
7066 label */
7067 if (j < i)
7069 gcc_assert (take_default);
7070 val = gimple_switch_default_label (stmt);
7072 else
7074 /* Check if labels with index i to j and maybe the default label
7075 are all reaching the same label. */
7077 val = gimple_switch_label (stmt, i);
7078 if (take_default
7079 && CASE_LABEL (gimple_switch_default_label (stmt))
7080 != CASE_LABEL (val))
7082 if (dump_file && (dump_flags & TDF_DETAILS))
7083 fprintf (dump_file, " not a single destination for this "
7084 "range\n");
7085 return SSA_PROP_VARYING;
7087 for (++i; i <= j; ++i)
7089 if (CASE_LABEL (gimple_switch_label (stmt, i)) != CASE_LABEL (val))
7091 if (dump_file && (dump_flags & TDF_DETAILS))
7092 fprintf (dump_file, " not a single destination for this "
7093 "range\n");
7094 return SSA_PROP_VARYING;
7097 for (; k <= l; ++k)
7099 if (CASE_LABEL (gimple_switch_label (stmt, k)) != CASE_LABEL (val))
7101 if (dump_file && (dump_flags & TDF_DETAILS))
7102 fprintf (dump_file, " not a single destination for this "
7103 "range\n");
7104 return SSA_PROP_VARYING;
7109 *taken_edge_p = find_edge (gimple_bb (stmt),
7110 label_to_block (CASE_LABEL (val)));
7112 if (dump_file && (dump_flags & TDF_DETAILS))
7114 fprintf (dump_file, " will take edge to ");
7115 print_generic_stmt (dump_file, CASE_LABEL (val), 0);
7118 return SSA_PROP_INTERESTING;
7122 /* Evaluate statement STMT. If the statement produces a useful range,
7123 return SSA_PROP_INTERESTING and record the SSA name with the
7124 interesting range into *OUTPUT_P.
7126 If STMT is a conditional branch and we can determine its truth
7127 value, the taken edge is recorded in *TAKEN_EDGE_P.
7129 If STMT produces a varying value, return SSA_PROP_VARYING. */
7131 static enum ssa_prop_result
7132 vrp_visit_stmt (gimple stmt, edge *taken_edge_p, tree *output_p)
7134 tree def;
7135 ssa_op_iter iter;
7137 if (dump_file && (dump_flags & TDF_DETAILS))
7139 fprintf (dump_file, "\nVisiting statement:\n");
7140 print_gimple_stmt (dump_file, stmt, 0, dump_flags);
7141 fprintf (dump_file, "\n");
7144 if (!stmt_interesting_for_vrp (stmt))
7145 gcc_assert (stmt_ends_bb_p (stmt));
7146 else if (is_gimple_assign (stmt) || is_gimple_call (stmt))
7148 /* In general, assignments with virtual operands are not useful
7149 for deriving ranges, with the obvious exception of calls to
7150 builtin functions. */
7151 if ((is_gimple_call (stmt)
7152 && gimple_call_fndecl (stmt) != NULL_TREE
7153 && DECL_BUILT_IN (gimple_call_fndecl (stmt)))
7154 || !gimple_vuse (stmt))
7155 return vrp_visit_assignment_or_call (stmt, output_p);
7157 else if (gimple_code (stmt) == GIMPLE_COND)
7158 return vrp_visit_cond_stmt (stmt, taken_edge_p);
7159 else if (gimple_code (stmt) == GIMPLE_SWITCH)
7160 return vrp_visit_switch_stmt (stmt, taken_edge_p);
7162 /* All other statements produce nothing of interest for VRP, so mark
7163 their outputs varying and prevent further simulation. */
7164 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_DEF)
7165 set_value_range_to_varying (get_value_range (def));
7167 return SSA_PROP_VARYING;
7170 /* Union the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
7171 { VR1TYPE, VR0MIN, VR0MAX } and store the result
7172 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
7173 possible such range. The resulting range is not canonicalized. */
7175 static void
7176 union_ranges (enum value_range_type *vr0type,
7177 tree *vr0min, tree *vr0max,
7178 enum value_range_type vr1type,
7179 tree vr1min, tree vr1max)
7181 bool mineq = operand_equal_p (*vr0min, vr1min, 0);
7182 bool maxeq = operand_equal_p (*vr0max, vr1max, 0);
7184 /* [] is vr0, () is vr1 in the following classification comments. */
7185 if (mineq && maxeq)
7187 /* [( )] */
7188 if (*vr0type == vr1type)
7189 /* Nothing to do for equal ranges. */
7191 else if ((*vr0type == VR_RANGE
7192 && vr1type == VR_ANTI_RANGE)
7193 || (*vr0type == VR_ANTI_RANGE
7194 && vr1type == VR_RANGE))
7196 /* For anti-range with range union the result is varying. */
7197 goto give_up;
7199 else
7200 gcc_unreachable ();
7202 else if (operand_less_p (*vr0max, vr1min) == 1
7203 || operand_less_p (vr1max, *vr0min) == 1)
7205 /* [ ] ( ) or ( ) [ ]
7206 If the ranges have an empty intersection, result of the union
7207 operation is the anti-range or if both are anti-ranges
7208 it covers all. */
7209 if (*vr0type == VR_ANTI_RANGE
7210 && vr1type == VR_ANTI_RANGE)
7211 goto give_up;
7212 else if (*vr0type == VR_ANTI_RANGE
7213 && vr1type == VR_RANGE)
7215 else if (*vr0type == VR_RANGE
7216 && vr1type == VR_ANTI_RANGE)
7218 *vr0type = vr1type;
7219 *vr0min = vr1min;
7220 *vr0max = vr1max;
7222 else if (*vr0type == VR_RANGE
7223 && vr1type == VR_RANGE)
7225 /* The result is the convex hull of both ranges. */
7226 if (operand_less_p (*vr0max, vr1min) == 1)
7228 /* If the result can be an anti-range, create one. */
7229 if (TREE_CODE (*vr0max) == INTEGER_CST
7230 && TREE_CODE (vr1min) == INTEGER_CST
7231 && vrp_val_is_min (*vr0min)
7232 && vrp_val_is_max (vr1max))
7234 tree min = int_const_binop (PLUS_EXPR,
7235 *vr0max, integer_one_node);
7236 tree max = int_const_binop (MINUS_EXPR,
7237 vr1min, integer_one_node);
7238 if (!operand_less_p (max, min))
7240 *vr0type = VR_ANTI_RANGE;
7241 *vr0min = min;
7242 *vr0max = max;
7244 else
7245 *vr0max = vr1max;
7247 else
7248 *vr0max = vr1max;
7250 else
7252 /* If the result can be an anti-range, create one. */
7253 if (TREE_CODE (vr1max) == INTEGER_CST
7254 && TREE_CODE (*vr0min) == INTEGER_CST
7255 && vrp_val_is_min (vr1min)
7256 && vrp_val_is_max (*vr0max))
7258 tree min = int_const_binop (PLUS_EXPR,
7259 vr1max, integer_one_node);
7260 tree max = int_const_binop (MINUS_EXPR,
7261 *vr0min, integer_one_node);
7262 if (!operand_less_p (max, min))
7264 *vr0type = VR_ANTI_RANGE;
7265 *vr0min = min;
7266 *vr0max = max;
7268 else
7269 *vr0min = vr1min;
7271 else
7272 *vr0min = vr1min;
7275 else
7276 gcc_unreachable ();
7278 else if ((maxeq || operand_less_p (vr1max, *vr0max) == 1)
7279 && (mineq || operand_less_p (*vr0min, vr1min) == 1))
7281 /* [ ( ) ] or [( ) ] or [ ( )] */
7282 if (*vr0type == VR_RANGE
7283 && vr1type == VR_RANGE)
7285 else if (*vr0type == VR_ANTI_RANGE
7286 && vr1type == VR_ANTI_RANGE)
7288 *vr0type = vr1type;
7289 *vr0min = vr1min;
7290 *vr0max = vr1max;
7292 else if (*vr0type == VR_ANTI_RANGE
7293 && vr1type == VR_RANGE)
7295 /* Arbitrarily choose the right or left gap. */
7296 if (!mineq && TREE_CODE (vr1min) == INTEGER_CST)
7297 *vr0max = int_const_binop (MINUS_EXPR, vr1min, integer_one_node);
7298 else if (!maxeq && TREE_CODE (vr1max) == INTEGER_CST)
7299 *vr0min = int_const_binop (PLUS_EXPR, vr1max, integer_one_node);
7300 else
7301 goto give_up;
7303 else if (*vr0type == VR_RANGE
7304 && vr1type == VR_ANTI_RANGE)
7305 /* The result covers everything. */
7306 goto give_up;
7307 else
7308 gcc_unreachable ();
7310 else if ((maxeq || operand_less_p (*vr0max, vr1max) == 1)
7311 && (mineq || operand_less_p (vr1min, *vr0min) == 1))
7313 /* ( [ ] ) or ([ ] ) or ( [ ]) */
7314 if (*vr0type == VR_RANGE
7315 && vr1type == VR_RANGE)
7317 *vr0type = vr1type;
7318 *vr0min = vr1min;
7319 *vr0max = vr1max;
7321 else if (*vr0type == VR_ANTI_RANGE
7322 && vr1type == VR_ANTI_RANGE)
7324 else if (*vr0type == VR_RANGE
7325 && vr1type == VR_ANTI_RANGE)
7327 *vr0type = VR_ANTI_RANGE;
7328 if (!mineq && TREE_CODE (*vr0min) == INTEGER_CST)
7330 *vr0max = int_const_binop (MINUS_EXPR, *vr0min, integer_one_node);
7331 *vr0min = vr1min;
7333 else if (!maxeq && TREE_CODE (*vr0max) == INTEGER_CST)
7335 *vr0min = int_const_binop (PLUS_EXPR, *vr0max, integer_one_node);
7336 *vr0max = vr1max;
7338 else
7339 goto give_up;
7341 else if (*vr0type == VR_ANTI_RANGE
7342 && vr1type == VR_RANGE)
7343 /* The result covers everything. */
7344 goto give_up;
7345 else
7346 gcc_unreachable ();
7348 else if ((operand_less_p (vr1min, *vr0max) == 1
7349 || operand_equal_p (vr1min, *vr0max, 0))
7350 && operand_less_p (*vr0min, vr1min) == 1)
7352 /* [ ( ] ) or [ ]( ) */
7353 if (*vr0type == VR_RANGE
7354 && vr1type == VR_RANGE)
7355 *vr0max = vr1max;
7356 else if (*vr0type == VR_ANTI_RANGE
7357 && vr1type == VR_ANTI_RANGE)
7358 *vr0min = vr1min;
7359 else if (*vr0type == VR_ANTI_RANGE
7360 && vr1type == VR_RANGE)
7362 if (TREE_CODE (vr1min) == INTEGER_CST)
7363 *vr0max = int_const_binop (MINUS_EXPR, vr1min, integer_one_node);
7364 else
7365 goto give_up;
7367 else if (*vr0type == VR_RANGE
7368 && vr1type == VR_ANTI_RANGE)
7370 if (TREE_CODE (*vr0max) == INTEGER_CST)
7372 *vr0type = vr1type;
7373 *vr0min = int_const_binop (PLUS_EXPR, *vr0max, integer_one_node);
7374 *vr0max = vr1max;
7376 else
7377 goto give_up;
7379 else
7380 gcc_unreachable ();
7382 else if ((operand_less_p (*vr0min, vr1max) == 1
7383 || operand_equal_p (*vr0min, vr1max, 0))
7384 && operand_less_p (vr1min, *vr0min) == 1)
7386 /* ( [ ) ] or ( )[ ] */
7387 if (*vr0type == VR_RANGE
7388 && vr1type == VR_RANGE)
7389 *vr0min = vr1min;
7390 else if (*vr0type == VR_ANTI_RANGE
7391 && vr1type == VR_ANTI_RANGE)
7392 *vr0max = vr1max;
7393 else if (*vr0type == VR_ANTI_RANGE
7394 && vr1type == VR_RANGE)
7396 if (TREE_CODE (vr1max) == INTEGER_CST)
7397 *vr0min = int_const_binop (PLUS_EXPR, vr1max, integer_one_node);
7398 else
7399 goto give_up;
7401 else if (*vr0type == VR_RANGE
7402 && vr1type == VR_ANTI_RANGE)
7404 if (TREE_CODE (*vr0min) == INTEGER_CST)
7406 *vr0type = vr1type;
7407 *vr0min = vr1min;
7408 *vr0max = int_const_binop (MINUS_EXPR, *vr0min, integer_one_node);
7410 else
7411 goto give_up;
7413 else
7414 gcc_unreachable ();
7416 else
7417 goto give_up;
7419 return;
7421 give_up:
7422 *vr0type = VR_VARYING;
7423 *vr0min = NULL_TREE;
7424 *vr0max = NULL_TREE;
7427 /* Intersect the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
7428 { VR1TYPE, VR0MIN, VR0MAX } and store the result
7429 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
7430 possible such range. The resulting range is not canonicalized. */
7432 static void
7433 intersect_ranges (enum value_range_type *vr0type,
7434 tree *vr0min, tree *vr0max,
7435 enum value_range_type vr1type,
7436 tree vr1min, tree vr1max)
7438 bool mineq = operand_equal_p (*vr0min, vr1min, 0);
7439 bool maxeq = operand_equal_p (*vr0max, vr1max, 0);
7441 /* [] is vr0, () is vr1 in the following classification comments. */
7442 if (mineq && maxeq)
7444 /* [( )] */
7445 if (*vr0type == vr1type)
7446 /* Nothing to do for equal ranges. */
7448 else if ((*vr0type == VR_RANGE
7449 && vr1type == VR_ANTI_RANGE)
7450 || (*vr0type == VR_ANTI_RANGE
7451 && vr1type == VR_RANGE))
7453 /* For anti-range with range intersection the result is empty. */
7454 *vr0type = VR_UNDEFINED;
7455 *vr0min = NULL_TREE;
7456 *vr0max = NULL_TREE;
7458 else
7459 gcc_unreachable ();
7461 else if (operand_less_p (*vr0max, vr1min) == 1
7462 || operand_less_p (vr1max, *vr0min) == 1)
7464 /* [ ] ( ) or ( ) [ ]
7465 If the ranges have an empty intersection, the result of the
7466 intersect operation is the range for intersecting an
7467 anti-range with a range or empty when intersecting two ranges. */
7468 if (*vr0type == VR_RANGE
7469 && vr1type == VR_ANTI_RANGE)
7471 else if (*vr0type == VR_ANTI_RANGE
7472 && vr1type == VR_RANGE)
7474 *vr0type = vr1type;
7475 *vr0min = vr1min;
7476 *vr0max = vr1max;
7478 else if (*vr0type == VR_RANGE
7479 && vr1type == VR_RANGE)
7481 *vr0type = VR_UNDEFINED;
7482 *vr0min = NULL_TREE;
7483 *vr0max = NULL_TREE;
7485 else if (*vr0type == VR_ANTI_RANGE
7486 && vr1type == VR_ANTI_RANGE)
7488 /* If the anti-ranges are adjacent to each other merge them. */
7489 if (TREE_CODE (*vr0max) == INTEGER_CST
7490 && TREE_CODE (vr1min) == INTEGER_CST
7491 && operand_less_p (*vr0max, vr1min) == 1
7492 && integer_onep (int_const_binop (MINUS_EXPR,
7493 vr1min, *vr0max)))
7494 *vr0max = vr1max;
7495 else if (TREE_CODE (vr1max) == INTEGER_CST
7496 && TREE_CODE (*vr0min) == INTEGER_CST
7497 && operand_less_p (vr1max, *vr0min) == 1
7498 && integer_onep (int_const_binop (MINUS_EXPR,
7499 *vr0min, vr1max)))
7500 *vr0min = vr1min;
7501 /* Else arbitrarily take VR0. */
7504 else if ((maxeq || operand_less_p (vr1max, *vr0max) == 1)
7505 && (mineq || operand_less_p (*vr0min, vr1min) == 1))
7507 /* [ ( ) ] or [( ) ] or [ ( )] */
7508 if (*vr0type == VR_RANGE
7509 && vr1type == VR_RANGE)
7511 /* If both are ranges the result is the inner one. */
7512 *vr0type = vr1type;
7513 *vr0min = vr1min;
7514 *vr0max = vr1max;
7516 else if (*vr0type == VR_RANGE
7517 && vr1type == VR_ANTI_RANGE)
7519 /* Choose the right gap if the left one is empty. */
7520 if (mineq)
7522 if (TREE_CODE (vr1max) == INTEGER_CST)
7523 *vr0min = int_const_binop (PLUS_EXPR, vr1max, integer_one_node);
7524 else
7525 *vr0min = vr1max;
7527 /* Choose the left gap if the right one is empty. */
7528 else if (maxeq)
7530 if (TREE_CODE (vr1min) == INTEGER_CST)
7531 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
7532 integer_one_node);
7533 else
7534 *vr0max = vr1min;
7536 /* Choose the anti-range if the range is effectively varying. */
7537 else if (vrp_val_is_min (*vr0min)
7538 && vrp_val_is_max (*vr0max))
7540 *vr0type = vr1type;
7541 *vr0min = vr1min;
7542 *vr0max = vr1max;
7544 /* Else choose the range. */
7546 else if (*vr0type == VR_ANTI_RANGE
7547 && vr1type == VR_ANTI_RANGE)
7548 /* If both are anti-ranges the result is the outer one. */
7550 else if (*vr0type == VR_ANTI_RANGE
7551 && vr1type == VR_RANGE)
7553 /* The intersection is empty. */
7554 *vr0type = VR_UNDEFINED;
7555 *vr0min = NULL_TREE;
7556 *vr0max = NULL_TREE;
7558 else
7559 gcc_unreachable ();
7561 else if ((maxeq || operand_less_p (*vr0max, vr1max) == 1)
7562 && (mineq || operand_less_p (vr1min, *vr0min) == 1))
7564 /* ( [ ] ) or ([ ] ) or ( [ ]) */
7565 if (*vr0type == VR_RANGE
7566 && vr1type == VR_RANGE)
7567 /* Choose the inner range. */
7569 else if (*vr0type == VR_ANTI_RANGE
7570 && vr1type == VR_RANGE)
7572 /* Choose the right gap if the left is empty. */
7573 if (mineq)
7575 *vr0type = VR_RANGE;
7576 if (TREE_CODE (*vr0max) == INTEGER_CST)
7577 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
7578 integer_one_node);
7579 else
7580 *vr0min = *vr0max;
7581 *vr0max = vr1max;
7583 /* Choose the left gap if the right is empty. */
7584 else if (maxeq)
7586 *vr0type = VR_RANGE;
7587 if (TREE_CODE (*vr0min) == INTEGER_CST)
7588 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
7589 integer_one_node);
7590 else
7591 *vr0max = *vr0min;
7592 *vr0min = vr1min;
7594 /* Choose the anti-range if the range is effectively varying. */
7595 else if (vrp_val_is_min (vr1min)
7596 && vrp_val_is_max (vr1max))
7598 /* Else choose the range. */
7599 else
7601 *vr0type = vr1type;
7602 *vr0min = vr1min;
7603 *vr0max = vr1max;
7606 else if (*vr0type == VR_ANTI_RANGE
7607 && vr1type == VR_ANTI_RANGE)
7609 /* If both are anti-ranges the result is the outer one. */
7610 *vr0type = vr1type;
7611 *vr0min = vr1min;
7612 *vr0max = vr1max;
7614 else if (vr1type == VR_ANTI_RANGE
7615 && *vr0type == VR_RANGE)
7617 /* The intersection is empty. */
7618 *vr0type = VR_UNDEFINED;
7619 *vr0min = NULL_TREE;
7620 *vr0max = NULL_TREE;
7622 else
7623 gcc_unreachable ();
7625 else if ((operand_less_p (vr1min, *vr0max) == 1
7626 || operand_equal_p (vr1min, *vr0max, 0))
7627 && operand_less_p (*vr0min, vr1min) == 1)
7629 /* [ ( ] ) or [ ]( ) */
7630 if (*vr0type == VR_ANTI_RANGE
7631 && vr1type == VR_ANTI_RANGE)
7632 *vr0max = vr1max;
7633 else if (*vr0type == VR_RANGE
7634 && vr1type == VR_RANGE)
7635 *vr0min = vr1min;
7636 else if (*vr0type == VR_RANGE
7637 && vr1type == VR_ANTI_RANGE)
7639 if (TREE_CODE (vr1min) == INTEGER_CST)
7640 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
7641 integer_one_node);
7642 else
7643 *vr0max = vr1min;
7645 else if (*vr0type == VR_ANTI_RANGE
7646 && vr1type == VR_RANGE)
7648 *vr0type = VR_RANGE;
7649 if (TREE_CODE (*vr0max) == INTEGER_CST)
7650 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
7651 integer_one_node);
7652 else
7653 *vr0min = *vr0max;
7654 *vr0max = vr1max;
7656 else
7657 gcc_unreachable ();
7659 else if ((operand_less_p (*vr0min, vr1max) == 1
7660 || operand_equal_p (*vr0min, vr1max, 0))
7661 && operand_less_p (vr1min, *vr0min) == 1)
7663 /* ( [ ) ] or ( )[ ] */
7664 if (*vr0type == VR_ANTI_RANGE
7665 && vr1type == VR_ANTI_RANGE)
7666 *vr0min = vr1min;
7667 else if (*vr0type == VR_RANGE
7668 && vr1type == VR_RANGE)
7669 *vr0max = vr1max;
7670 else if (*vr0type == VR_RANGE
7671 && vr1type == VR_ANTI_RANGE)
7673 if (TREE_CODE (vr1max) == INTEGER_CST)
7674 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
7675 integer_one_node);
7676 else
7677 *vr0min = vr1max;
7679 else if (*vr0type == VR_ANTI_RANGE
7680 && vr1type == VR_RANGE)
7682 *vr0type = VR_RANGE;
7683 if (TREE_CODE (*vr0min) == INTEGER_CST)
7684 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
7685 integer_one_node);
7686 else
7687 *vr0max = *vr0min;
7688 *vr0min = vr1min;
7690 else
7691 gcc_unreachable ();
7694 /* As a fallback simply use { *VRTYPE, *VR0MIN, *VR0MAX } as
7695 result for the intersection. That's always a conservative
7696 correct estimate. */
7698 return;
7702 /* Intersect the two value-ranges *VR0 and *VR1 and store the result
7703 in *VR0. This may not be the smallest possible such range. */
7705 static void
7706 vrp_intersect_ranges_1 (value_range_t *vr0, value_range_t *vr1)
7708 value_range_t saved;
7710 /* If either range is VR_VARYING the other one wins. */
7711 if (vr1->type == VR_VARYING)
7712 return;
7713 if (vr0->type == VR_VARYING)
7715 copy_value_range (vr0, vr1);
7716 return;
7719 /* When either range is VR_UNDEFINED the resulting range is
7720 VR_UNDEFINED, too. */
7721 if (vr0->type == VR_UNDEFINED)
7722 return;
7723 if (vr1->type == VR_UNDEFINED)
7725 set_value_range_to_undefined (vr0);
7726 return;
7729 /* Save the original vr0 so we can return it as conservative intersection
7730 result when our worker turns things to varying. */
7731 saved = *vr0;
7732 intersect_ranges (&vr0->type, &vr0->min, &vr0->max,
7733 vr1->type, vr1->min, vr1->max);
7734 /* Make sure to canonicalize the result though as the inversion of a
7735 VR_RANGE can still be a VR_RANGE. */
7736 set_and_canonicalize_value_range (vr0, vr0->type,
7737 vr0->min, vr0->max, vr0->equiv);
7738 /* If that failed, use the saved original VR0. */
7739 if (vr0->type == VR_VARYING)
7741 *vr0 = saved;
7742 return;
7744 /* If the result is VR_UNDEFINED there is no need to mess with
7745 the equivalencies. */
7746 if (vr0->type == VR_UNDEFINED)
7747 return;
7749 /* The resulting set of equivalences for range intersection is the union of
7750 the two sets. */
7751 if (vr0->equiv && vr1->equiv && vr0->equiv != vr1->equiv)
7752 bitmap_ior_into (vr0->equiv, vr1->equiv);
7753 else if (vr1->equiv && !vr0->equiv)
7754 bitmap_copy (vr0->equiv, vr1->equiv);
7757 static void
7758 vrp_intersect_ranges (value_range_t *vr0, value_range_t *vr1)
7760 if (dump_file && (dump_flags & TDF_DETAILS))
7762 fprintf (dump_file, "Intersecting\n ");
7763 dump_value_range (dump_file, vr0);
7764 fprintf (dump_file, "\nand\n ");
7765 dump_value_range (dump_file, vr1);
7766 fprintf (dump_file, "\n");
7768 vrp_intersect_ranges_1 (vr0, vr1);
7769 if (dump_file && (dump_flags & TDF_DETAILS))
7771 fprintf (dump_file, "to\n ");
7772 dump_value_range (dump_file, vr0);
7773 fprintf (dump_file, "\n");
7777 /* Meet operation for value ranges. Given two value ranges VR0 and
7778 VR1, store in VR0 a range that contains both VR0 and VR1. This
7779 may not be the smallest possible such range. */
7781 static void
7782 vrp_meet_1 (value_range_t *vr0, value_range_t *vr1)
7784 value_range_t saved;
7786 if (vr0->type == VR_UNDEFINED)
7788 /* Drop equivalences. See PR53465. */
7789 set_value_range (vr0, vr1->type, vr1->min, vr1->max, NULL);
7790 return;
7793 if (vr1->type == VR_UNDEFINED)
7795 /* VR0 already has the resulting range, just drop equivalences.
7796 See PR53465. */
7797 if (vr0->equiv)
7798 bitmap_clear (vr0->equiv);
7799 return;
7802 if (vr0->type == VR_VARYING)
7804 /* Nothing to do. VR0 already has the resulting range. */
7805 return;
7808 if (vr1->type == VR_VARYING)
7810 set_value_range_to_varying (vr0);
7811 return;
7814 saved = *vr0;
7815 union_ranges (&vr0->type, &vr0->min, &vr0->max,
7816 vr1->type, vr1->min, vr1->max);
7817 if (vr0->type == VR_VARYING)
7819 /* Failed to find an efficient meet. Before giving up and setting
7820 the result to VARYING, see if we can at least derive a useful
7821 anti-range. FIXME, all this nonsense about distinguishing
7822 anti-ranges from ranges is necessary because of the odd
7823 semantics of range_includes_zero_p and friends. */
7824 if (((saved.type == VR_RANGE
7825 && range_includes_zero_p (saved.min, saved.max) == 0)
7826 || (saved.type == VR_ANTI_RANGE
7827 && range_includes_zero_p (saved.min, saved.max) == 1))
7828 && ((vr1->type == VR_RANGE
7829 && range_includes_zero_p (vr1->min, vr1->max) == 0)
7830 || (vr1->type == VR_ANTI_RANGE
7831 && range_includes_zero_p (vr1->min, vr1->max) == 1)))
7833 set_value_range_to_nonnull (vr0, TREE_TYPE (saved.min));
7835 /* Since this meet operation did not result from the meeting of
7836 two equivalent names, VR0 cannot have any equivalences. */
7837 if (vr0->equiv)
7838 bitmap_clear (vr0->equiv);
7839 return;
7842 set_value_range_to_varying (vr0);
7843 return;
7845 set_and_canonicalize_value_range (vr0, vr0->type, vr0->min, vr0->max,
7846 vr0->equiv);
7847 if (vr0->type == VR_VARYING)
7848 return;
7850 /* The resulting set of equivalences is always the intersection of
7851 the two sets. */
7852 if (vr0->equiv && vr1->equiv && vr0->equiv != vr1->equiv)
7853 bitmap_and_into (vr0->equiv, vr1->equiv);
7854 else if (vr0->equiv && !vr1->equiv)
7855 bitmap_clear (vr0->equiv);
7858 static void
7859 vrp_meet (value_range_t *vr0, value_range_t *vr1)
7861 if (dump_file && (dump_flags & TDF_DETAILS))
7863 fprintf (dump_file, "Meeting\n ");
7864 dump_value_range (dump_file, vr0);
7865 fprintf (dump_file, "\nand\n ");
7866 dump_value_range (dump_file, vr1);
7867 fprintf (dump_file, "\n");
7869 vrp_meet_1 (vr0, vr1);
7870 if (dump_file && (dump_flags & TDF_DETAILS))
7872 fprintf (dump_file, "to\n ");
7873 dump_value_range (dump_file, vr0);
7874 fprintf (dump_file, "\n");
7879 /* Visit all arguments for PHI node PHI that flow through executable
7880 edges. If a valid value range can be derived from all the incoming
7881 value ranges, set a new range for the LHS of PHI. */
7883 static enum ssa_prop_result
7884 vrp_visit_phi_node (gimple phi)
7886 size_t i;
7887 tree lhs = PHI_RESULT (phi);
7888 value_range_t *lhs_vr = get_value_range (lhs);
7889 value_range_t vr_result = VR_INITIALIZER;
7890 bool first = true;
7891 int edges, old_edges;
7892 struct loop *l;
7894 if (dump_file && (dump_flags & TDF_DETAILS))
7896 fprintf (dump_file, "\nVisiting PHI node: ");
7897 print_gimple_stmt (dump_file, phi, 0, dump_flags);
7900 edges = 0;
7901 for (i = 0; i < gimple_phi_num_args (phi); i++)
7903 edge e = gimple_phi_arg_edge (phi, i);
7905 if (dump_file && (dump_flags & TDF_DETAILS))
7907 fprintf (dump_file,
7908 "\n Argument #%d (%d -> %d %sexecutable)\n",
7909 (int) i, e->src->index, e->dest->index,
7910 (e->flags & EDGE_EXECUTABLE) ? "" : "not ");
7913 if (e->flags & EDGE_EXECUTABLE)
7915 tree arg = PHI_ARG_DEF (phi, i);
7916 value_range_t vr_arg;
7918 ++edges;
7920 if (TREE_CODE (arg) == SSA_NAME)
7922 vr_arg = *(get_value_range (arg));
7924 else
7926 if (is_overflow_infinity (arg))
7928 arg = copy_node (arg);
7929 TREE_OVERFLOW (arg) = 0;
7932 vr_arg.type = VR_RANGE;
7933 vr_arg.min = arg;
7934 vr_arg.max = arg;
7935 vr_arg.equiv = NULL;
7938 if (dump_file && (dump_flags & TDF_DETAILS))
7940 fprintf (dump_file, "\t");
7941 print_generic_expr (dump_file, arg, dump_flags);
7942 fprintf (dump_file, "\n\tValue: ");
7943 dump_value_range (dump_file, &vr_arg);
7944 fprintf (dump_file, "\n");
7947 if (first)
7948 copy_value_range (&vr_result, &vr_arg);
7949 else
7950 vrp_meet (&vr_result, &vr_arg);
7951 first = false;
7953 if (vr_result.type == VR_VARYING)
7954 break;
7958 if (vr_result.type == VR_VARYING)
7959 goto varying;
7960 else if (vr_result.type == VR_UNDEFINED)
7961 goto update_range;
7963 old_edges = vr_phi_edge_counts[SSA_NAME_VERSION (lhs)];
7964 vr_phi_edge_counts[SSA_NAME_VERSION (lhs)] = edges;
7966 /* To prevent infinite iterations in the algorithm, derive ranges
7967 when the new value is slightly bigger or smaller than the
7968 previous one. We don't do this if we have seen a new executable
7969 edge; this helps us avoid an overflow infinity for conditionals
7970 which are not in a loop. If the old value-range was VR_UNDEFINED
7971 use the updated range and iterate one more time. */
7972 if (edges > 0
7973 && gimple_phi_num_args (phi) > 1
7974 && edges == old_edges
7975 && lhs_vr->type != VR_UNDEFINED)
7977 int cmp_min = compare_values (lhs_vr->min, vr_result.min);
7978 int cmp_max = compare_values (lhs_vr->max, vr_result.max);
7980 /* For non VR_RANGE or for pointers fall back to varying if
7981 the range changed. */
7982 if ((lhs_vr->type != VR_RANGE || vr_result.type != VR_RANGE
7983 || POINTER_TYPE_P (TREE_TYPE (lhs)))
7984 && (cmp_min != 0 || cmp_max != 0))
7985 goto varying;
7987 /* If the new minimum is smaller or larger than the previous
7988 one, go all the way to -INF. In the first case, to avoid
7989 iterating millions of times to reach -INF, and in the
7990 other case to avoid infinite bouncing between different
7991 minimums. */
7992 if (cmp_min > 0 || cmp_min < 0)
7994 if (!needs_overflow_infinity (TREE_TYPE (vr_result.min))
7995 || !vrp_var_may_overflow (lhs, phi))
7996 vr_result.min = TYPE_MIN_VALUE (TREE_TYPE (vr_result.min));
7997 else if (supports_overflow_infinity (TREE_TYPE (vr_result.min)))
7998 vr_result.min =
7999 negative_overflow_infinity (TREE_TYPE (vr_result.min));
8002 /* Similarly, if the new maximum is smaller or larger than
8003 the previous one, go all the way to +INF. */
8004 if (cmp_max < 0 || cmp_max > 0)
8006 if (!needs_overflow_infinity (TREE_TYPE (vr_result.max))
8007 || !vrp_var_may_overflow (lhs, phi))
8008 vr_result.max = TYPE_MAX_VALUE (TREE_TYPE (vr_result.max));
8009 else if (supports_overflow_infinity (TREE_TYPE (vr_result.max)))
8010 vr_result.max =
8011 positive_overflow_infinity (TREE_TYPE (vr_result.max));
8014 /* If we dropped either bound to +-INF then if this is a loop
8015 PHI node SCEV may known more about its value-range. */
8016 if ((cmp_min > 0 || cmp_min < 0
8017 || cmp_max < 0 || cmp_max > 0)
8018 && current_loops
8019 && (l = loop_containing_stmt (phi))
8020 && l->header == gimple_bb (phi))
8021 adjust_range_with_scev (&vr_result, l, phi, lhs);
8023 /* If we will end up with a (-INF, +INF) range, set it to
8024 VARYING. Same if the previous max value was invalid for
8025 the type and we end up with vr_result.min > vr_result.max. */
8026 if ((vrp_val_is_max (vr_result.max)
8027 && vrp_val_is_min (vr_result.min))
8028 || compare_values (vr_result.min,
8029 vr_result.max) > 0)
8030 goto varying;
8033 /* If the new range is different than the previous value, keep
8034 iterating. */
8035 update_range:
8036 if (update_value_range (lhs, &vr_result))
8038 if (dump_file && (dump_flags & TDF_DETAILS))
8040 fprintf (dump_file, "Found new range for ");
8041 print_generic_expr (dump_file, lhs, 0);
8042 fprintf (dump_file, ": ");
8043 dump_value_range (dump_file, &vr_result);
8044 fprintf (dump_file, "\n\n");
8047 return SSA_PROP_INTERESTING;
8050 /* Nothing changed, don't add outgoing edges. */
8051 return SSA_PROP_NOT_INTERESTING;
8053 /* No match found. Set the LHS to VARYING. */
8054 varying:
8055 set_value_range_to_varying (lhs_vr);
8056 return SSA_PROP_VARYING;
8059 /* Simplify boolean operations if the source is known
8060 to be already a boolean. */
8061 static bool
8062 simplify_truth_ops_using_ranges (gimple_stmt_iterator *gsi, gimple stmt)
8064 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
8065 tree lhs, op0, op1;
8066 bool need_conversion;
8068 /* We handle only !=/== case here. */
8069 gcc_assert (rhs_code == EQ_EXPR || rhs_code == NE_EXPR);
8071 op0 = gimple_assign_rhs1 (stmt);
8072 if (!op_with_boolean_value_range_p (op0))
8073 return false;
8075 op1 = gimple_assign_rhs2 (stmt);
8076 if (!op_with_boolean_value_range_p (op1))
8077 return false;
8079 /* Reduce number of cases to handle to NE_EXPR. As there is no
8080 BIT_XNOR_EXPR we cannot replace A == B with a single statement. */
8081 if (rhs_code == EQ_EXPR)
8083 if (TREE_CODE (op1) == INTEGER_CST)
8084 op1 = int_const_binop (BIT_XOR_EXPR, op1, integer_one_node);
8085 else
8086 return false;
8089 lhs = gimple_assign_lhs (stmt);
8090 need_conversion
8091 = !useless_type_conversion_p (TREE_TYPE (lhs), TREE_TYPE (op0));
8093 /* Make sure to not sign-extend a 1-bit 1 when converting the result. */
8094 if (need_conversion
8095 && !TYPE_UNSIGNED (TREE_TYPE (op0))
8096 && TYPE_PRECISION (TREE_TYPE (op0)) == 1
8097 && TYPE_PRECISION (TREE_TYPE (lhs)) > 1)
8098 return false;
8100 /* For A != 0 we can substitute A itself. */
8101 if (integer_zerop (op1))
8102 gimple_assign_set_rhs_with_ops (gsi,
8103 need_conversion
8104 ? NOP_EXPR : TREE_CODE (op0),
8105 op0, NULL_TREE);
8106 /* For A != B we substitute A ^ B. Either with conversion. */
8107 else if (need_conversion)
8109 tree tem = make_ssa_name (TREE_TYPE (op0), NULL);
8110 gimple newop = gimple_build_assign_with_ops (BIT_XOR_EXPR, tem, op0, op1);
8111 gsi_insert_before (gsi, newop, GSI_SAME_STMT);
8112 gimple_assign_set_rhs_with_ops (gsi, NOP_EXPR, tem, NULL_TREE);
8114 /* Or without. */
8115 else
8116 gimple_assign_set_rhs_with_ops (gsi, BIT_XOR_EXPR, op0, op1);
8117 update_stmt (gsi_stmt (*gsi));
8119 return true;
8122 /* Simplify a division or modulo operator to a right shift or
8123 bitwise and if the first operand is unsigned or is greater
8124 than zero and the second operand is an exact power of two. */
8126 static bool
8127 simplify_div_or_mod_using_ranges (gimple stmt)
8129 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
8130 tree val = NULL;
8131 tree op0 = gimple_assign_rhs1 (stmt);
8132 tree op1 = gimple_assign_rhs2 (stmt);
8133 value_range_t *vr = get_value_range (gimple_assign_rhs1 (stmt));
8135 if (TYPE_UNSIGNED (TREE_TYPE (op0)))
8137 val = integer_one_node;
8139 else
8141 bool sop = false;
8143 val = compare_range_with_value (GE_EXPR, vr, integer_zero_node, &sop);
8145 if (val
8146 && sop
8147 && integer_onep (val)
8148 && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC))
8150 location_t location;
8152 if (!gimple_has_location (stmt))
8153 location = input_location;
8154 else
8155 location = gimple_location (stmt);
8156 warning_at (location, OPT_Wstrict_overflow,
8157 "assuming signed overflow does not occur when "
8158 "simplifying %</%> or %<%%%> to %<>>%> or %<&%>");
8162 if (val && integer_onep (val))
8164 tree t;
8166 if (rhs_code == TRUNC_DIV_EXPR)
8168 t = build_int_cst (integer_type_node, tree_log2 (op1));
8169 gimple_assign_set_rhs_code (stmt, RSHIFT_EXPR);
8170 gimple_assign_set_rhs1 (stmt, op0);
8171 gimple_assign_set_rhs2 (stmt, t);
8173 else
8175 t = build_int_cst (TREE_TYPE (op1), 1);
8176 t = int_const_binop (MINUS_EXPR, op1, t);
8177 t = fold_convert (TREE_TYPE (op0), t);
8179 gimple_assign_set_rhs_code (stmt, BIT_AND_EXPR);
8180 gimple_assign_set_rhs1 (stmt, op0);
8181 gimple_assign_set_rhs2 (stmt, t);
8184 update_stmt (stmt);
8185 return true;
8188 return false;
8191 /* If the operand to an ABS_EXPR is >= 0, then eliminate the
8192 ABS_EXPR. If the operand is <= 0, then simplify the
8193 ABS_EXPR into a NEGATE_EXPR. */
8195 static bool
8196 simplify_abs_using_ranges (gimple stmt)
8198 tree val = NULL;
8199 tree op = gimple_assign_rhs1 (stmt);
8200 tree type = TREE_TYPE (op);
8201 value_range_t *vr = get_value_range (op);
8203 if (TYPE_UNSIGNED (type))
8205 val = integer_zero_node;
8207 else if (vr)
8209 bool sop = false;
8211 val = compare_range_with_value (LE_EXPR, vr, integer_zero_node, &sop);
8212 if (!val)
8214 sop = false;
8215 val = compare_range_with_value (GE_EXPR, vr, integer_zero_node,
8216 &sop);
8218 if (val)
8220 if (integer_zerop (val))
8221 val = integer_one_node;
8222 else if (integer_onep (val))
8223 val = integer_zero_node;
8227 if (val
8228 && (integer_onep (val) || integer_zerop (val)))
8230 if (sop && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC))
8232 location_t location;
8234 if (!gimple_has_location (stmt))
8235 location = input_location;
8236 else
8237 location = gimple_location (stmt);
8238 warning_at (location, OPT_Wstrict_overflow,
8239 "assuming signed overflow does not occur when "
8240 "simplifying %<abs (X)%> to %<X%> or %<-X%>");
8243 gimple_assign_set_rhs1 (stmt, op);
8244 if (integer_onep (val))
8245 gimple_assign_set_rhs_code (stmt, NEGATE_EXPR);
8246 else
8247 gimple_assign_set_rhs_code (stmt, SSA_NAME);
8248 update_stmt (stmt);
8249 return true;
8253 return false;
8256 /* Optimize away redundant BIT_AND_EXPR and BIT_IOR_EXPR.
8257 If all the bits that are being cleared by & are already
8258 known to be zero from VR, or all the bits that are being
8259 set by | are already known to be one from VR, the bit
8260 operation is redundant. */
8262 static bool
8263 simplify_bit_ops_using_ranges (gimple_stmt_iterator *gsi, gimple stmt)
8265 tree op0 = gimple_assign_rhs1 (stmt);
8266 tree op1 = gimple_assign_rhs2 (stmt);
8267 tree op = NULL_TREE;
8268 value_range_t vr0 = VR_INITIALIZER;
8269 value_range_t vr1 = VR_INITIALIZER;
8270 double_int may_be_nonzero0, may_be_nonzero1;
8271 double_int must_be_nonzero0, must_be_nonzero1;
8272 double_int mask;
8274 if (TREE_CODE (op0) == SSA_NAME)
8275 vr0 = *(get_value_range (op0));
8276 else if (is_gimple_min_invariant (op0))
8277 set_value_range_to_value (&vr0, op0, NULL);
8278 else
8279 return false;
8281 if (TREE_CODE (op1) == SSA_NAME)
8282 vr1 = *(get_value_range (op1));
8283 else if (is_gimple_min_invariant (op1))
8284 set_value_range_to_value (&vr1, op1, NULL);
8285 else
8286 return false;
8288 if (!zero_nonzero_bits_from_vr (&vr0, &may_be_nonzero0, &must_be_nonzero0))
8289 return false;
8290 if (!zero_nonzero_bits_from_vr (&vr1, &may_be_nonzero1, &must_be_nonzero1))
8291 return false;
8293 switch (gimple_assign_rhs_code (stmt))
8295 case BIT_AND_EXPR:
8296 mask = may_be_nonzero0.and_not (must_be_nonzero1);
8297 if (mask.is_zero ())
8299 op = op0;
8300 break;
8302 mask = may_be_nonzero1.and_not (must_be_nonzero0);
8303 if (mask.is_zero ())
8305 op = op1;
8306 break;
8308 break;
8309 case BIT_IOR_EXPR:
8310 mask = may_be_nonzero0.and_not (must_be_nonzero1);
8311 if (mask.is_zero ())
8313 op = op1;
8314 break;
8316 mask = may_be_nonzero1.and_not (must_be_nonzero0);
8317 if (mask.is_zero ())
8319 op = op0;
8320 break;
8322 break;
8323 default:
8324 gcc_unreachable ();
8327 if (op == NULL_TREE)
8328 return false;
8330 gimple_assign_set_rhs_with_ops (gsi, TREE_CODE (op), op, NULL);
8331 update_stmt (gsi_stmt (*gsi));
8332 return true;
8335 /* We are comparing trees OP0 and OP1 using COND_CODE. OP0 has
8336 a known value range VR.
8338 If there is one and only one value which will satisfy the
8339 conditional, then return that value. Else return NULL. */
8341 static tree
8342 test_for_singularity (enum tree_code cond_code, tree op0,
8343 tree op1, value_range_t *vr)
8345 tree min = NULL;
8346 tree max = NULL;
8348 /* Extract minimum/maximum values which satisfy the
8349 the conditional as it was written. */
8350 if (cond_code == LE_EXPR || cond_code == LT_EXPR)
8352 /* This should not be negative infinity; there is no overflow
8353 here. */
8354 min = TYPE_MIN_VALUE (TREE_TYPE (op0));
8356 max = op1;
8357 if (cond_code == LT_EXPR && !is_overflow_infinity (max))
8359 tree one = build_int_cst (TREE_TYPE (op0), 1);
8360 max = fold_build2 (MINUS_EXPR, TREE_TYPE (op0), max, one);
8361 if (EXPR_P (max))
8362 TREE_NO_WARNING (max) = 1;
8365 else if (cond_code == GE_EXPR || cond_code == GT_EXPR)
8367 /* This should not be positive infinity; there is no overflow
8368 here. */
8369 max = TYPE_MAX_VALUE (TREE_TYPE (op0));
8371 min = op1;
8372 if (cond_code == GT_EXPR && !is_overflow_infinity (min))
8374 tree one = build_int_cst (TREE_TYPE (op0), 1);
8375 min = fold_build2 (PLUS_EXPR, TREE_TYPE (op0), min, one);
8376 if (EXPR_P (min))
8377 TREE_NO_WARNING (min) = 1;
8381 /* Now refine the minimum and maximum values using any
8382 value range information we have for op0. */
8383 if (min && max)
8385 if (compare_values (vr->min, min) == 1)
8386 min = vr->min;
8387 if (compare_values (vr->max, max) == -1)
8388 max = vr->max;
8390 /* If the new min/max values have converged to a single value,
8391 then there is only one value which can satisfy the condition,
8392 return that value. */
8393 if (operand_equal_p (min, max, 0) && is_gimple_min_invariant (min))
8394 return min;
8396 return NULL;
8399 /* Simplify a conditional using a relational operator to an equality
8400 test if the range information indicates only one value can satisfy
8401 the original conditional. */
8403 static bool
8404 simplify_cond_using_ranges (gimple stmt)
8406 tree op0 = gimple_cond_lhs (stmt);
8407 tree op1 = gimple_cond_rhs (stmt);
8408 enum tree_code cond_code = gimple_cond_code (stmt);
8410 if (cond_code != NE_EXPR
8411 && cond_code != EQ_EXPR
8412 && TREE_CODE (op0) == SSA_NAME
8413 && INTEGRAL_TYPE_P (TREE_TYPE (op0))
8414 && is_gimple_min_invariant (op1))
8416 value_range_t *vr = get_value_range (op0);
8418 /* If we have range information for OP0, then we might be
8419 able to simplify this conditional. */
8420 if (vr->type == VR_RANGE)
8422 tree new_tree = test_for_singularity (cond_code, op0, op1, vr);
8424 if (new_tree)
8426 if (dump_file)
8428 fprintf (dump_file, "Simplified relational ");
8429 print_gimple_stmt (dump_file, stmt, 0, 0);
8430 fprintf (dump_file, " into ");
8433 gimple_cond_set_code (stmt, EQ_EXPR);
8434 gimple_cond_set_lhs (stmt, op0);
8435 gimple_cond_set_rhs (stmt, new_tree);
8437 update_stmt (stmt);
8439 if (dump_file)
8441 print_gimple_stmt (dump_file, stmt, 0, 0);
8442 fprintf (dump_file, "\n");
8445 return true;
8448 /* Try again after inverting the condition. We only deal
8449 with integral types here, so no need to worry about
8450 issues with inverting FP comparisons. */
8451 cond_code = invert_tree_comparison (cond_code, false);
8452 new_tree = test_for_singularity (cond_code, op0, op1, vr);
8454 if (new_tree)
8456 if (dump_file)
8458 fprintf (dump_file, "Simplified relational ");
8459 print_gimple_stmt (dump_file, stmt, 0, 0);
8460 fprintf (dump_file, " into ");
8463 gimple_cond_set_code (stmt, NE_EXPR);
8464 gimple_cond_set_lhs (stmt, op0);
8465 gimple_cond_set_rhs (stmt, new_tree);
8467 update_stmt (stmt);
8469 if (dump_file)
8471 print_gimple_stmt (dump_file, stmt, 0, 0);
8472 fprintf (dump_file, "\n");
8475 return true;
8480 return false;
8483 /* Simplify a switch statement using the value range of the switch
8484 argument. */
8486 static bool
8487 simplify_switch_using_ranges (gimple stmt)
8489 tree op = gimple_switch_index (stmt);
8490 value_range_t *vr;
8491 bool take_default;
8492 edge e;
8493 edge_iterator ei;
8494 size_t i = 0, j = 0, n, n2;
8495 tree vec2;
8496 switch_update su;
8497 size_t k = 1, l = 0;
8499 if (TREE_CODE (op) == SSA_NAME)
8501 vr = get_value_range (op);
8503 /* We can only handle integer ranges. */
8504 if ((vr->type != VR_RANGE
8505 && vr->type != VR_ANTI_RANGE)
8506 || symbolic_range_p (vr))
8507 return false;
8509 /* Find case label for min/max of the value range. */
8510 take_default = !find_case_label_ranges (stmt, vr, &i, &j, &k, &l);
8512 else if (TREE_CODE (op) == INTEGER_CST)
8514 take_default = !find_case_label_index (stmt, 1, op, &i);
8515 if (take_default)
8517 i = 1;
8518 j = 0;
8520 else
8522 j = i;
8525 else
8526 return false;
8528 n = gimple_switch_num_labels (stmt);
8530 /* Bail out if this is just all edges taken. */
8531 if (i == 1
8532 && j == n - 1
8533 && take_default)
8534 return false;
8536 /* Build a new vector of taken case labels. */
8537 vec2 = make_tree_vec (j - i + 1 + l - k + 1 + (int)take_default);
8538 n2 = 0;
8540 /* Add the default edge, if necessary. */
8541 if (take_default)
8542 TREE_VEC_ELT (vec2, n2++) = gimple_switch_default_label (stmt);
8544 for (; i <= j; ++i, ++n2)
8545 TREE_VEC_ELT (vec2, n2) = gimple_switch_label (stmt, i);
8547 for (; k <= l; ++k, ++n2)
8548 TREE_VEC_ELT (vec2, n2) = gimple_switch_label (stmt, k);
8550 /* Mark needed edges. */
8551 for (i = 0; i < n2; ++i)
8553 e = find_edge (gimple_bb (stmt),
8554 label_to_block (CASE_LABEL (TREE_VEC_ELT (vec2, i))));
8555 e->aux = (void *)-1;
8558 /* Queue not needed edges for later removal. */
8559 FOR_EACH_EDGE (e, ei, gimple_bb (stmt)->succs)
8561 if (e->aux == (void *)-1)
8563 e->aux = NULL;
8564 continue;
8567 if (dump_file && (dump_flags & TDF_DETAILS))
8569 fprintf (dump_file, "removing unreachable case label\n");
8571 VEC_safe_push (edge, heap, to_remove_edges, e);
8572 e->flags &= ~EDGE_EXECUTABLE;
8575 /* And queue an update for the stmt. */
8576 su.stmt = stmt;
8577 su.vec = vec2;
8578 VEC_safe_push (switch_update, heap, to_update_switch_stmts, su);
8579 return false;
8582 /* Simplify an integral conversion from an SSA name in STMT. */
8584 static bool
8585 simplify_conversion_using_ranges (gimple stmt)
8587 tree innerop, middleop, finaltype;
8588 gimple def_stmt;
8589 value_range_t *innervr;
8590 bool inner_unsigned_p, middle_unsigned_p, final_unsigned_p;
8591 unsigned inner_prec, middle_prec, final_prec;
8592 double_int innermin, innermed, innermax, middlemin, middlemed, middlemax;
8594 finaltype = TREE_TYPE (gimple_assign_lhs (stmt));
8595 if (!INTEGRAL_TYPE_P (finaltype))
8596 return false;
8597 middleop = gimple_assign_rhs1 (stmt);
8598 def_stmt = SSA_NAME_DEF_STMT (middleop);
8599 if (!is_gimple_assign (def_stmt)
8600 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt)))
8601 return false;
8602 innerop = gimple_assign_rhs1 (def_stmt);
8603 if (TREE_CODE (innerop) != SSA_NAME)
8604 return false;
8606 /* Get the value-range of the inner operand. */
8607 innervr = get_value_range (innerop);
8608 if (innervr->type != VR_RANGE
8609 || TREE_CODE (innervr->min) != INTEGER_CST
8610 || TREE_CODE (innervr->max) != INTEGER_CST)
8611 return false;
8613 /* Simulate the conversion chain to check if the result is equal if
8614 the middle conversion is removed. */
8615 innermin = tree_to_double_int (innervr->min);
8616 innermax = tree_to_double_int (innervr->max);
8618 inner_prec = TYPE_PRECISION (TREE_TYPE (innerop));
8619 middle_prec = TYPE_PRECISION (TREE_TYPE (middleop));
8620 final_prec = TYPE_PRECISION (finaltype);
8622 /* If the first conversion is not injective, the second must not
8623 be widening. */
8624 if ((innermax - innermin).ugt (double_int::mask (middle_prec))
8625 && middle_prec < final_prec)
8626 return false;
8627 /* We also want a medium value so that we can track the effect that
8628 narrowing conversions with sign change have. */
8629 inner_unsigned_p = TYPE_UNSIGNED (TREE_TYPE (innerop));
8630 if (inner_unsigned_p)
8631 innermed = double_int::mask (inner_prec).lrshift (1, inner_prec);
8632 else
8633 innermed = double_int_zero;
8634 if (innermin.cmp (innermed, inner_unsigned_p) >= 0
8635 || innermed.cmp (innermax, inner_unsigned_p) >= 0)
8636 innermed = innermin;
8638 middle_unsigned_p = TYPE_UNSIGNED (TREE_TYPE (middleop));
8639 middlemin = innermin.ext (middle_prec, middle_unsigned_p);
8640 middlemed = innermed.ext (middle_prec, middle_unsigned_p);
8641 middlemax = innermax.ext (middle_prec, middle_unsigned_p);
8643 /* Require that the final conversion applied to both the original
8644 and the intermediate range produces the same result. */
8645 final_unsigned_p = TYPE_UNSIGNED (finaltype);
8646 if (middlemin.ext (final_prec, final_unsigned_p)
8647 != innermin.ext (final_prec, final_unsigned_p)
8648 || middlemed.ext (final_prec, final_unsigned_p)
8649 != innermed.ext (final_prec, final_unsigned_p)
8650 || middlemax.ext (final_prec, final_unsigned_p)
8651 != innermax.ext (final_prec, final_unsigned_p))
8652 return false;
8654 gimple_assign_set_rhs1 (stmt, innerop);
8655 update_stmt (stmt);
8656 return true;
8659 /* Return whether the value range *VR fits in an integer type specified
8660 by PRECISION and UNSIGNED_P. */
8662 static bool
8663 range_fits_type_p (value_range_t *vr, unsigned precision, bool unsigned_p)
8665 tree src_type;
8666 unsigned src_precision;
8667 double_int tem;
8669 /* We can only handle integral and pointer types. */
8670 src_type = TREE_TYPE (vr->min);
8671 if (!INTEGRAL_TYPE_P (src_type)
8672 && !POINTER_TYPE_P (src_type))
8673 return false;
8675 /* An extension is always fine, so is an identity transform. */
8676 src_precision = TYPE_PRECISION (TREE_TYPE (vr->min));
8677 if (src_precision < precision
8678 || (src_precision == precision
8679 && TYPE_UNSIGNED (src_type) == unsigned_p))
8680 return true;
8682 /* Now we can only handle ranges with constant bounds. */
8683 if (vr->type != VR_RANGE
8684 || TREE_CODE (vr->min) != INTEGER_CST
8685 || TREE_CODE (vr->max) != INTEGER_CST)
8686 return false;
8688 /* For precision-preserving sign-changes the MSB of the double-int
8689 has to be clear. */
8690 if (src_precision == precision
8691 && (TREE_INT_CST_HIGH (vr->min) | TREE_INT_CST_HIGH (vr->max)) < 0)
8692 return false;
8694 /* Then we can perform the conversion on both ends and compare
8695 the result for equality. */
8696 tem = tree_to_double_int (vr->min).ext (precision, unsigned_p);
8697 if (tree_to_double_int (vr->min) != tem)
8698 return false;
8699 tem = tree_to_double_int (vr->max).ext (precision, unsigned_p);
8700 if (tree_to_double_int (vr->max) != tem)
8701 return false;
8703 return true;
8706 /* Simplify a conversion from integral SSA name to float in STMT. */
8708 static bool
8709 simplify_float_conversion_using_ranges (gimple_stmt_iterator *gsi, gimple stmt)
8711 tree rhs1 = gimple_assign_rhs1 (stmt);
8712 value_range_t *vr = get_value_range (rhs1);
8713 enum machine_mode fltmode = TYPE_MODE (TREE_TYPE (gimple_assign_lhs (stmt)));
8714 enum machine_mode mode;
8715 tree tem;
8716 gimple conv;
8718 /* We can only handle constant ranges. */
8719 if (vr->type != VR_RANGE
8720 || TREE_CODE (vr->min) != INTEGER_CST
8721 || TREE_CODE (vr->max) != INTEGER_CST)
8722 return false;
8724 /* First check if we can use a signed type in place of an unsigned. */
8725 if (TYPE_UNSIGNED (TREE_TYPE (rhs1))
8726 && (can_float_p (fltmode, TYPE_MODE (TREE_TYPE (rhs1)), 0)
8727 != CODE_FOR_nothing)
8728 && range_fits_type_p (vr, GET_MODE_PRECISION
8729 (TYPE_MODE (TREE_TYPE (rhs1))), 0))
8730 mode = TYPE_MODE (TREE_TYPE (rhs1));
8731 /* If we can do the conversion in the current input mode do nothing. */
8732 else if (can_float_p (fltmode, TYPE_MODE (TREE_TYPE (rhs1)),
8733 TYPE_UNSIGNED (TREE_TYPE (rhs1))))
8734 return false;
8735 /* Otherwise search for a mode we can use, starting from the narrowest
8736 integer mode available. */
8737 else
8739 mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
8742 /* If we cannot do a signed conversion to float from mode
8743 or if the value-range does not fit in the signed type
8744 try with a wider mode. */
8745 if (can_float_p (fltmode, mode, 0) != CODE_FOR_nothing
8746 && range_fits_type_p (vr, GET_MODE_PRECISION (mode), 0))
8747 break;
8749 mode = GET_MODE_WIDER_MODE (mode);
8750 /* But do not widen the input. Instead leave that to the
8751 optabs expansion code. */
8752 if (GET_MODE_PRECISION (mode) > TYPE_PRECISION (TREE_TYPE (rhs1)))
8753 return false;
8755 while (mode != VOIDmode);
8756 if (mode == VOIDmode)
8757 return false;
8760 /* It works, insert a truncation or sign-change before the
8761 float conversion. */
8762 tem = make_ssa_name (build_nonstandard_integer_type
8763 (GET_MODE_PRECISION (mode), 0), NULL);
8764 conv = gimple_build_assign_with_ops (NOP_EXPR, tem, rhs1, NULL_TREE);
8765 gsi_insert_before (gsi, conv, GSI_SAME_STMT);
8766 gimple_assign_set_rhs1 (stmt, tem);
8767 update_stmt (stmt);
8769 return true;
8772 /* Simplify STMT using ranges if possible. */
8774 static bool
8775 simplify_stmt_using_ranges (gimple_stmt_iterator *gsi)
8777 gimple stmt = gsi_stmt (*gsi);
8778 if (is_gimple_assign (stmt))
8780 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
8781 tree rhs1 = gimple_assign_rhs1 (stmt);
8783 switch (rhs_code)
8785 case EQ_EXPR:
8786 case NE_EXPR:
8787 /* Transform EQ_EXPR, NE_EXPR into BIT_XOR_EXPR or identity
8788 if the RHS is zero or one, and the LHS are known to be boolean
8789 values. */
8790 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
8791 return simplify_truth_ops_using_ranges (gsi, stmt);
8792 break;
8794 /* Transform TRUNC_DIV_EXPR and TRUNC_MOD_EXPR into RSHIFT_EXPR
8795 and BIT_AND_EXPR respectively if the first operand is greater
8796 than zero and the second operand is an exact power of two. */
8797 case TRUNC_DIV_EXPR:
8798 case TRUNC_MOD_EXPR:
8799 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1))
8800 && integer_pow2p (gimple_assign_rhs2 (stmt)))
8801 return simplify_div_or_mod_using_ranges (stmt);
8802 break;
8804 /* Transform ABS (X) into X or -X as appropriate. */
8805 case ABS_EXPR:
8806 if (TREE_CODE (rhs1) == SSA_NAME
8807 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
8808 return simplify_abs_using_ranges (stmt);
8809 break;
8811 case BIT_AND_EXPR:
8812 case BIT_IOR_EXPR:
8813 /* Optimize away BIT_AND_EXPR and BIT_IOR_EXPR
8814 if all the bits being cleared are already cleared or
8815 all the bits being set are already set. */
8816 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
8817 return simplify_bit_ops_using_ranges (gsi, stmt);
8818 break;
8820 CASE_CONVERT:
8821 if (TREE_CODE (rhs1) == SSA_NAME
8822 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
8823 return simplify_conversion_using_ranges (stmt);
8824 break;
8826 case FLOAT_EXPR:
8827 if (TREE_CODE (rhs1) == SSA_NAME
8828 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
8829 return simplify_float_conversion_using_ranges (gsi, stmt);
8830 break;
8832 default:
8833 break;
8836 else if (gimple_code (stmt) == GIMPLE_COND)
8837 return simplify_cond_using_ranges (stmt);
8838 else if (gimple_code (stmt) == GIMPLE_SWITCH)
8839 return simplify_switch_using_ranges (stmt);
8841 return false;
8844 /* If the statement pointed by SI has a predicate whose value can be
8845 computed using the value range information computed by VRP, compute
8846 its value and return true. Otherwise, return false. */
8848 static bool
8849 fold_predicate_in (gimple_stmt_iterator *si)
8851 bool assignment_p = false;
8852 tree val;
8853 gimple stmt = gsi_stmt (*si);
8855 if (is_gimple_assign (stmt)
8856 && TREE_CODE_CLASS (gimple_assign_rhs_code (stmt)) == tcc_comparison)
8858 assignment_p = true;
8859 val = vrp_evaluate_conditional (gimple_assign_rhs_code (stmt),
8860 gimple_assign_rhs1 (stmt),
8861 gimple_assign_rhs2 (stmt),
8862 stmt);
8864 else if (gimple_code (stmt) == GIMPLE_COND)
8865 val = vrp_evaluate_conditional (gimple_cond_code (stmt),
8866 gimple_cond_lhs (stmt),
8867 gimple_cond_rhs (stmt),
8868 stmt);
8869 else
8870 return false;
8872 if (val)
8874 if (assignment_p)
8875 val = fold_convert (gimple_expr_type (stmt), val);
8877 if (dump_file)
8879 fprintf (dump_file, "Folding predicate ");
8880 print_gimple_expr (dump_file, stmt, 0, 0);
8881 fprintf (dump_file, " to ");
8882 print_generic_expr (dump_file, val, 0);
8883 fprintf (dump_file, "\n");
8886 if (is_gimple_assign (stmt))
8887 gimple_assign_set_rhs_from_tree (si, val);
8888 else
8890 gcc_assert (gimple_code (stmt) == GIMPLE_COND);
8891 if (integer_zerop (val))
8892 gimple_cond_make_false (stmt);
8893 else if (integer_onep (val))
8894 gimple_cond_make_true (stmt);
8895 else
8896 gcc_unreachable ();
8899 return true;
8902 return false;
8905 /* Callback for substitute_and_fold folding the stmt at *SI. */
8907 static bool
8908 vrp_fold_stmt (gimple_stmt_iterator *si)
8910 if (fold_predicate_in (si))
8911 return true;
8913 return simplify_stmt_using_ranges (si);
8916 /* Stack of dest,src equivalency pairs that need to be restored after
8917 each attempt to thread a block's incoming edge to an outgoing edge.
8919 A NULL entry is used to mark the end of pairs which need to be
8920 restored. */
8921 static VEC(tree,heap) *equiv_stack;
8923 /* A trivial wrapper so that we can present the generic jump threading
8924 code with a simple API for simplifying statements. STMT is the
8925 statement we want to simplify, WITHIN_STMT provides the location
8926 for any overflow warnings. */
8928 static tree
8929 simplify_stmt_for_jump_threading (gimple stmt, gimple within_stmt)
8931 /* We only use VRP information to simplify conditionals. This is
8932 overly conservative, but it's unclear if doing more would be
8933 worth the compile time cost. */
8934 if (gimple_code (stmt) != GIMPLE_COND)
8935 return NULL;
8937 return vrp_evaluate_conditional (gimple_cond_code (stmt),
8938 gimple_cond_lhs (stmt),
8939 gimple_cond_rhs (stmt), within_stmt);
8942 /* Blocks which have more than one predecessor and more than
8943 one successor present jump threading opportunities, i.e.,
8944 when the block is reached from a specific predecessor, we
8945 may be able to determine which of the outgoing edges will
8946 be traversed. When this optimization applies, we are able
8947 to avoid conditionals at runtime and we may expose secondary
8948 optimization opportunities.
8950 This routine is effectively a driver for the generic jump
8951 threading code. It basically just presents the generic code
8952 with edges that may be suitable for jump threading.
8954 Unlike DOM, we do not iterate VRP if jump threading was successful.
8955 While iterating may expose new opportunities for VRP, it is expected
8956 those opportunities would be very limited and the compile time cost
8957 to expose those opportunities would be significant.
8959 As jump threading opportunities are discovered, they are registered
8960 for later realization. */
8962 static void
8963 identify_jump_threads (void)
8965 basic_block bb;
8966 gimple dummy;
8967 int i;
8968 edge e;
8970 /* Ugh. When substituting values earlier in this pass we can
8971 wipe the dominance information. So rebuild the dominator
8972 information as we need it within the jump threading code. */
8973 calculate_dominance_info (CDI_DOMINATORS);
8975 /* We do not allow VRP information to be used for jump threading
8976 across a back edge in the CFG. Otherwise it becomes too
8977 difficult to avoid eliminating loop exit tests. Of course
8978 EDGE_DFS_BACK is not accurate at this time so we have to
8979 recompute it. */
8980 mark_dfs_back_edges ();
8982 /* Do not thread across edges we are about to remove. Just marking
8983 them as EDGE_DFS_BACK will do. */
8984 FOR_EACH_VEC_ELT (edge, to_remove_edges, i, e)
8985 e->flags |= EDGE_DFS_BACK;
8987 /* Allocate our unwinder stack to unwind any temporary equivalences
8988 that might be recorded. */
8989 equiv_stack = VEC_alloc (tree, heap, 20);
8991 /* To avoid lots of silly node creation, we create a single
8992 conditional and just modify it in-place when attempting to
8993 thread jumps. */
8994 dummy = gimple_build_cond (EQ_EXPR,
8995 integer_zero_node, integer_zero_node,
8996 NULL, NULL);
8998 /* Walk through all the blocks finding those which present a
8999 potential jump threading opportunity. We could set this up
9000 as a dominator walker and record data during the walk, but
9001 I doubt it's worth the effort for the classes of jump
9002 threading opportunities we are trying to identify at this
9003 point in compilation. */
9004 FOR_EACH_BB (bb)
9006 gimple last;
9008 /* If the generic jump threading code does not find this block
9009 interesting, then there is nothing to do. */
9010 if (! potentially_threadable_block (bb))
9011 continue;
9013 /* We only care about blocks ending in a COND_EXPR. While there
9014 may be some value in handling SWITCH_EXPR here, I doubt it's
9015 terribly important. */
9016 last = gsi_stmt (gsi_last_bb (bb));
9018 /* We're basically looking for a switch or any kind of conditional with
9019 integral or pointer type arguments. Note the type of the second
9020 argument will be the same as the first argument, so no need to
9021 check it explicitly. */
9022 if (gimple_code (last) == GIMPLE_SWITCH
9023 || (gimple_code (last) == GIMPLE_COND
9024 && TREE_CODE (gimple_cond_lhs (last)) == SSA_NAME
9025 && (INTEGRAL_TYPE_P (TREE_TYPE (gimple_cond_lhs (last)))
9026 || POINTER_TYPE_P (TREE_TYPE (gimple_cond_lhs (last))))
9027 && (TREE_CODE (gimple_cond_rhs (last)) == SSA_NAME
9028 || is_gimple_min_invariant (gimple_cond_rhs (last)))))
9030 edge_iterator ei;
9032 /* We've got a block with multiple predecessors and multiple
9033 successors which also ends in a suitable conditional or
9034 switch statement. For each predecessor, see if we can thread
9035 it to a specific successor. */
9036 FOR_EACH_EDGE (e, ei, bb->preds)
9038 /* Do not thread across back edges or abnormal edges
9039 in the CFG. */
9040 if (e->flags & (EDGE_DFS_BACK | EDGE_COMPLEX))
9041 continue;
9043 thread_across_edge (dummy, e, true, &equiv_stack,
9044 simplify_stmt_for_jump_threading);
9049 /* We do not actually update the CFG or SSA graphs at this point as
9050 ASSERT_EXPRs are still in the IL and cfg cleanup code does not yet
9051 handle ASSERT_EXPRs gracefully. */
9054 /* We identified all the jump threading opportunities earlier, but could
9055 not transform the CFG at that time. This routine transforms the
9056 CFG and arranges for the dominator tree to be rebuilt if necessary.
9058 Note the SSA graph update will occur during the normal TODO
9059 processing by the pass manager. */
9060 static void
9061 finalize_jump_threads (void)
9063 thread_through_all_blocks (false);
9064 VEC_free (tree, heap, equiv_stack);
9068 /* Traverse all the blocks folding conditionals with known ranges. */
9070 static void
9071 vrp_finalize (void)
9073 size_t i;
9075 values_propagated = true;
9077 if (dump_file)
9079 fprintf (dump_file, "\nValue ranges after VRP:\n\n");
9080 dump_all_value_ranges (dump_file);
9081 fprintf (dump_file, "\n");
9084 substitute_and_fold (op_with_constant_singleton_value_range,
9085 vrp_fold_stmt, false);
9087 if (warn_array_bounds)
9088 check_all_array_refs ();
9090 /* We must identify jump threading opportunities before we release
9091 the datastructures built by VRP. */
9092 identify_jump_threads ();
9094 /* Free allocated memory. */
9095 for (i = 0; i < num_vr_values; i++)
9096 if (vr_value[i])
9098 BITMAP_FREE (vr_value[i]->equiv);
9099 free (vr_value[i]);
9102 free (vr_value);
9103 free (vr_phi_edge_counts);
9105 /* So that we can distinguish between VRP data being available
9106 and not available. */
9107 vr_value = NULL;
9108 vr_phi_edge_counts = NULL;
9112 /* Main entry point to VRP (Value Range Propagation). This pass is
9113 loosely based on J. R. C. Patterson, ``Accurate Static Branch
9114 Prediction by Value Range Propagation,'' in SIGPLAN Conference on
9115 Programming Language Design and Implementation, pp. 67-78, 1995.
9116 Also available at http://citeseer.ist.psu.edu/patterson95accurate.html
9118 This is essentially an SSA-CCP pass modified to deal with ranges
9119 instead of constants.
9121 While propagating ranges, we may find that two or more SSA name
9122 have equivalent, though distinct ranges. For instance,
9124 1 x_9 = p_3->a;
9125 2 p_4 = ASSERT_EXPR <p_3, p_3 != 0>
9126 3 if (p_4 == q_2)
9127 4 p_5 = ASSERT_EXPR <p_4, p_4 == q_2>;
9128 5 endif
9129 6 if (q_2)
9131 In the code above, pointer p_5 has range [q_2, q_2], but from the
9132 code we can also determine that p_5 cannot be NULL and, if q_2 had
9133 a non-varying range, p_5's range should also be compatible with it.
9135 These equivalences are created by two expressions: ASSERT_EXPR and
9136 copy operations. Since p_5 is an assertion on p_4, and p_4 was the
9137 result of another assertion, then we can use the fact that p_5 and
9138 p_4 are equivalent when evaluating p_5's range.
9140 Together with value ranges, we also propagate these equivalences
9141 between names so that we can take advantage of information from
9142 multiple ranges when doing final replacement. Note that this
9143 equivalency relation is transitive but not symmetric.
9145 In the example above, p_5 is equivalent to p_4, q_2 and p_3, but we
9146 cannot assert that q_2 is equivalent to p_5 because q_2 may be used
9147 in contexts where that assertion does not hold (e.g., in line 6).
9149 TODO, the main difference between this pass and Patterson's is that
9150 we do not propagate edge probabilities. We only compute whether
9151 edges can be taken or not. That is, instead of having a spectrum
9152 of jump probabilities between 0 and 1, we only deal with 0, 1 and
9153 DON'T KNOW. In the future, it may be worthwhile to propagate
9154 probabilities to aid branch prediction. */
9156 static unsigned int
9157 execute_vrp (void)
9159 int i;
9160 edge e;
9161 switch_update *su;
9163 loop_optimizer_init (LOOPS_NORMAL | LOOPS_HAVE_RECORDED_EXITS);
9164 rewrite_into_loop_closed_ssa (NULL, TODO_update_ssa);
9165 scev_initialize ();
9167 insert_range_assertions ();
9169 to_remove_edges = VEC_alloc (edge, heap, 10);
9170 to_update_switch_stmts = VEC_alloc (switch_update, heap, 5);
9171 threadedge_initialize_values ();
9173 vrp_initialize ();
9174 ssa_propagate (vrp_visit_stmt, vrp_visit_phi_node);
9175 vrp_finalize ();
9177 free_numbers_of_iterations_estimates ();
9179 /* ASSERT_EXPRs must be removed before finalizing jump threads
9180 as finalizing jump threads calls the CFG cleanup code which
9181 does not properly handle ASSERT_EXPRs. */
9182 remove_range_assertions ();
9184 /* If we exposed any new variables, go ahead and put them into
9185 SSA form now, before we handle jump threading. This simplifies
9186 interactions between rewriting of _DECL nodes into SSA form
9187 and rewriting SSA_NAME nodes into SSA form after block
9188 duplication and CFG manipulation. */
9189 update_ssa (TODO_update_ssa);
9191 finalize_jump_threads ();
9193 /* Remove dead edges from SWITCH_EXPR optimization. This leaves the
9194 CFG in a broken state and requires a cfg_cleanup run. */
9195 FOR_EACH_VEC_ELT (edge, to_remove_edges, i, e)
9196 remove_edge (e);
9197 /* Update SWITCH_EXPR case label vector. */
9198 FOR_EACH_VEC_ELT (switch_update, to_update_switch_stmts, i, su)
9200 size_t j;
9201 size_t n = TREE_VEC_LENGTH (su->vec);
9202 tree label;
9203 gimple_switch_set_num_labels (su->stmt, n);
9204 for (j = 0; j < n; j++)
9205 gimple_switch_set_label (su->stmt, j, TREE_VEC_ELT (su->vec, j));
9206 /* As we may have replaced the default label with a regular one
9207 make sure to make it a real default label again. This ensures
9208 optimal expansion. */
9209 label = gimple_switch_label (su->stmt, 0);
9210 CASE_LOW (label) = NULL_TREE;
9211 CASE_HIGH (label) = NULL_TREE;
9214 if (VEC_length (edge, to_remove_edges) > 0)
9215 free_dominance_info (CDI_DOMINATORS);
9217 VEC_free (edge, heap, to_remove_edges);
9218 VEC_free (switch_update, heap, to_update_switch_stmts);
9219 threadedge_finalize_values ();
9221 scev_finalize ();
9222 loop_optimizer_finalize ();
9223 return 0;
9226 static bool
9227 gate_vrp (void)
9229 return flag_tree_vrp != 0;
9232 struct gimple_opt_pass pass_vrp =
9235 GIMPLE_PASS,
9236 "vrp", /* name */
9237 OPTGROUP_NONE, /* optinfo_flags */
9238 gate_vrp, /* gate */
9239 execute_vrp, /* execute */
9240 NULL, /* sub */
9241 NULL, /* next */
9242 0, /* static_pass_number */
9243 TV_TREE_VRP, /* tv_id */
9244 PROP_ssa, /* properties_required */
9245 0, /* properties_provided */
9246 0, /* properties_destroyed */
9247 0, /* todo_flags_start */
9248 TODO_cleanup_cfg
9249 | TODO_update_ssa
9250 | TODO_verify_ssa
9251 | TODO_verify_flow
9252 | TODO_ggc_collect /* todo_flags_finish */