DR 1402
[official-gcc.git] / gcc / tree-vrp.c
blob32c5afa49c7254ef1d72a35d4a137d213f95c5d1
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 "timevar.h"
34 #include "tree-pretty-print.h"
35 #include "gimple-pretty-print.h"
36 #include "diagnostic-core.h"
37 #include "intl.h"
38 #include "cfgloop.h"
39 #include "tree-scalar-evolution.h"
40 #include "tree-ssa-propagate.h"
41 #include "tree-chrec.h"
42 #include "gimple-fold.h"
43 #include "expr.h"
44 #include "optabs.h"
47 /* Type of value ranges. See value_range_d for a description of these
48 types. */
49 enum value_range_type { VR_UNDEFINED, VR_RANGE, VR_ANTI_RANGE, VR_VARYING };
51 /* Range of values that can be associated with an SSA_NAME after VRP
52 has executed. */
53 struct value_range_d
55 /* Lattice value represented by this range. */
56 enum value_range_type type;
58 /* Minimum and maximum values represented by this range. These
59 values should be interpreted as follows:
61 - If TYPE is VR_UNDEFINED or VR_VARYING then MIN and MAX must
62 be NULL.
64 - If TYPE == VR_RANGE then MIN holds the minimum value and
65 MAX holds the maximum value of the range [MIN, MAX].
67 - If TYPE == ANTI_RANGE the variable is known to NOT
68 take any values in the range [MIN, MAX]. */
69 tree min;
70 tree max;
72 /* Set of SSA names whose value ranges are equivalent to this one.
73 This set is only valid when TYPE is VR_RANGE or VR_ANTI_RANGE. */
74 bitmap equiv;
77 typedef struct value_range_d value_range_t;
79 #define VR_INITIALIZER { VR_UNDEFINED, NULL_TREE, NULL_TREE, NULL }
81 /* Set of SSA names found live during the RPO traversal of the function
82 for still active basic-blocks. */
83 static sbitmap *live;
85 /* Return true if the SSA name NAME is live on the edge E. */
87 static bool
88 live_on_edge (edge e, tree name)
90 return (live[e->dest->index]
91 && TEST_BIT (live[e->dest->index], SSA_NAME_VERSION (name)));
94 /* Local functions. */
95 static int compare_values (tree val1, tree val2);
96 static int compare_values_warnv (tree val1, tree val2, bool *);
97 static void vrp_meet (value_range_t *, value_range_t *);
98 static void vrp_intersect_ranges (value_range_t *, value_range_t *);
99 static tree vrp_evaluate_conditional_warnv_with_ops (enum tree_code,
100 tree, tree, bool, bool *,
101 bool *);
103 /* Location information for ASSERT_EXPRs. Each instance of this
104 structure describes an ASSERT_EXPR for an SSA name. Since a single
105 SSA name may have more than one assertion associated with it, these
106 locations are kept in a linked list attached to the corresponding
107 SSA name. */
108 struct assert_locus_d
110 /* Basic block where the assertion would be inserted. */
111 basic_block bb;
113 /* Some assertions need to be inserted on an edge (e.g., assertions
114 generated by COND_EXPRs). In those cases, BB will be NULL. */
115 edge e;
117 /* Pointer to the statement that generated this assertion. */
118 gimple_stmt_iterator si;
120 /* Predicate code for the ASSERT_EXPR. Must be COMPARISON_CLASS_P. */
121 enum tree_code comp_code;
123 /* Value being compared against. */
124 tree val;
126 /* Expression to compare. */
127 tree expr;
129 /* Next node in the linked list. */
130 struct assert_locus_d *next;
133 typedef struct assert_locus_d *assert_locus_t;
135 /* If bit I is present, it means that SSA name N_i has a list of
136 assertions that should be inserted in the IL. */
137 static bitmap need_assert_for;
139 /* Array of locations lists where to insert assertions. ASSERTS_FOR[I]
140 holds a list of ASSERT_LOCUS_T nodes that describe where
141 ASSERT_EXPRs for SSA name N_I should be inserted. */
142 static assert_locus_t *asserts_for;
144 /* Value range array. After propagation, VR_VALUE[I] holds the range
145 of values that SSA name N_I may take. */
146 static unsigned num_vr_values;
147 static value_range_t **vr_value;
148 static bool values_propagated;
150 /* For a PHI node which sets SSA name N_I, VR_COUNTS[I] holds the
151 number of executable edges we saw the last time we visited the
152 node. */
153 static int *vr_phi_edge_counts;
155 typedef struct {
156 gimple stmt;
157 tree vec;
158 } switch_update;
160 static VEC (edge, heap) *to_remove_edges;
161 DEF_VEC_O(switch_update);
162 DEF_VEC_ALLOC_O(switch_update, heap);
163 static VEC (switch_update, heap) *to_update_switch_stmts;
166 /* Return the maximum value for TYPE. */
168 static inline tree
169 vrp_val_max (const_tree type)
171 if (!INTEGRAL_TYPE_P (type))
172 return NULL_TREE;
174 return TYPE_MAX_VALUE (type);
177 /* Return the minimum value for TYPE. */
179 static inline tree
180 vrp_val_min (const_tree type)
182 if (!INTEGRAL_TYPE_P (type))
183 return NULL_TREE;
185 return TYPE_MIN_VALUE (type);
188 /* Return whether VAL is equal to the maximum value of its type. This
189 will be true for a positive overflow infinity. We can't do a
190 simple equality comparison with TYPE_MAX_VALUE because C typedefs
191 and Ada subtypes can produce types whose TYPE_MAX_VALUE is not ==
192 to the integer constant with the same value in the type. */
194 static inline bool
195 vrp_val_is_max (const_tree val)
197 tree type_max = vrp_val_max (TREE_TYPE (val));
198 return (val == type_max
199 || (type_max != NULL_TREE
200 && operand_equal_p (val, type_max, 0)));
203 /* Return whether VAL is equal to the minimum value of its type. This
204 will be true for a negative overflow infinity. */
206 static inline bool
207 vrp_val_is_min (const_tree val)
209 tree type_min = vrp_val_min (TREE_TYPE (val));
210 return (val == type_min
211 || (type_min != NULL_TREE
212 && operand_equal_p (val, type_min, 0)));
216 /* Return whether TYPE should use an overflow infinity distinct from
217 TYPE_{MIN,MAX}_VALUE. We use an overflow infinity value to
218 represent a signed overflow during VRP computations. An infinity
219 is distinct from a half-range, which will go from some number to
220 TYPE_{MIN,MAX}_VALUE. */
222 static inline bool
223 needs_overflow_infinity (const_tree type)
225 return INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_WRAPS (type);
228 /* Return whether TYPE can support our overflow infinity
229 representation: we use the TREE_OVERFLOW flag, which only exists
230 for constants. If TYPE doesn't support this, we don't optimize
231 cases which would require signed overflow--we drop them to
232 VARYING. */
234 static inline bool
235 supports_overflow_infinity (const_tree type)
237 tree min = vrp_val_min (type), max = vrp_val_max (type);
238 #ifdef ENABLE_CHECKING
239 gcc_assert (needs_overflow_infinity (type));
240 #endif
241 return (min != NULL_TREE
242 && CONSTANT_CLASS_P (min)
243 && max != NULL_TREE
244 && CONSTANT_CLASS_P (max));
247 /* VAL is the maximum or minimum value of a type. Return a
248 corresponding overflow infinity. */
250 static inline tree
251 make_overflow_infinity (tree val)
253 gcc_checking_assert (val != NULL_TREE && CONSTANT_CLASS_P (val));
254 val = copy_node (val);
255 TREE_OVERFLOW (val) = 1;
256 return val;
259 /* Return a negative overflow infinity for TYPE. */
261 static inline tree
262 negative_overflow_infinity (tree type)
264 gcc_checking_assert (supports_overflow_infinity (type));
265 return make_overflow_infinity (vrp_val_min (type));
268 /* Return a positive overflow infinity for TYPE. */
270 static inline tree
271 positive_overflow_infinity (tree type)
273 gcc_checking_assert (supports_overflow_infinity (type));
274 return make_overflow_infinity (vrp_val_max (type));
277 /* Return whether VAL is a negative overflow infinity. */
279 static inline bool
280 is_negative_overflow_infinity (const_tree val)
282 return (needs_overflow_infinity (TREE_TYPE (val))
283 && CONSTANT_CLASS_P (val)
284 && TREE_OVERFLOW (val)
285 && vrp_val_is_min (val));
288 /* Return whether VAL is a positive overflow infinity. */
290 static inline bool
291 is_positive_overflow_infinity (const_tree val)
293 return (needs_overflow_infinity (TREE_TYPE (val))
294 && CONSTANT_CLASS_P (val)
295 && TREE_OVERFLOW (val)
296 && vrp_val_is_max (val));
299 /* Return whether VAL is a positive or negative overflow infinity. */
301 static inline bool
302 is_overflow_infinity (const_tree val)
304 return (needs_overflow_infinity (TREE_TYPE (val))
305 && CONSTANT_CLASS_P (val)
306 && TREE_OVERFLOW (val)
307 && (vrp_val_is_min (val) || vrp_val_is_max (val)));
310 /* Return whether STMT has a constant rhs that is_overflow_infinity. */
312 static inline bool
313 stmt_overflow_infinity (gimple stmt)
315 if (is_gimple_assign (stmt)
316 && get_gimple_rhs_class (gimple_assign_rhs_code (stmt)) ==
317 GIMPLE_SINGLE_RHS)
318 return is_overflow_infinity (gimple_assign_rhs1 (stmt));
319 return false;
322 /* If VAL is now an overflow infinity, return VAL. Otherwise, return
323 the same value with TREE_OVERFLOW clear. This can be used to avoid
324 confusing a regular value with an overflow value. */
326 static inline tree
327 avoid_overflow_infinity (tree val)
329 if (!is_overflow_infinity (val))
330 return val;
332 if (vrp_val_is_max (val))
333 return vrp_val_max (TREE_TYPE (val));
334 else
336 gcc_checking_assert (vrp_val_is_min (val));
337 return vrp_val_min (TREE_TYPE (val));
342 /* Return true if ARG is marked with the nonnull attribute in the
343 current function signature. */
345 static bool
346 nonnull_arg_p (const_tree arg)
348 tree t, attrs, fntype;
349 unsigned HOST_WIDE_INT arg_num;
351 gcc_assert (TREE_CODE (arg) == PARM_DECL && POINTER_TYPE_P (TREE_TYPE (arg)));
353 /* The static chain decl is always non null. */
354 if (arg == cfun->static_chain_decl)
355 return true;
357 fntype = TREE_TYPE (current_function_decl);
358 attrs = lookup_attribute ("nonnull", TYPE_ATTRIBUTES (fntype));
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;
386 return false;
390 /* Set value range VR to VR_UNDEFINED. */
392 static inline void
393 set_value_range_to_undefined (value_range_t *vr)
395 vr->type = VR_UNDEFINED;
396 vr->min = vr->max = NULL_TREE;
397 if (vr->equiv)
398 bitmap_clear (vr->equiv);
402 /* Set value range VR to VR_VARYING. */
404 static inline void
405 set_value_range_to_varying (value_range_t *vr)
407 vr->type = VR_VARYING;
408 vr->min = vr->max = NULL_TREE;
409 if (vr->equiv)
410 bitmap_clear (vr->equiv);
414 /* Set value range VR to {T, MIN, MAX, EQUIV}. */
416 static void
417 set_value_range (value_range_t *vr, enum value_range_type t, tree min,
418 tree max, bitmap equiv)
420 #if defined ENABLE_CHECKING
421 /* Check the validity of the range. */
422 if (t == VR_RANGE || t == VR_ANTI_RANGE)
424 int cmp;
426 gcc_assert (min && max);
428 if (INTEGRAL_TYPE_P (TREE_TYPE (min)) && t == VR_ANTI_RANGE)
429 gcc_assert (!vrp_val_is_min (min) || !vrp_val_is_max (max));
431 cmp = compare_values (min, max);
432 gcc_assert (cmp == 0 || cmp == -1 || cmp == -2);
434 if (needs_overflow_infinity (TREE_TYPE (min)))
435 gcc_assert (!is_overflow_infinity (min)
436 || !is_overflow_infinity (max));
439 if (t == VR_UNDEFINED || t == VR_VARYING)
440 gcc_assert (min == NULL_TREE && max == NULL_TREE);
442 if (t == VR_UNDEFINED || t == VR_VARYING)
443 gcc_assert (equiv == NULL || bitmap_empty_p (equiv));
444 #endif
446 vr->type = t;
447 vr->min = min;
448 vr->max = max;
450 /* Since updating the equivalence set involves deep copying the
451 bitmaps, only do it if absolutely necessary. */
452 if (vr->equiv == NULL
453 && equiv != NULL)
454 vr->equiv = BITMAP_ALLOC (NULL);
456 if (equiv != vr->equiv)
458 if (equiv && !bitmap_empty_p (equiv))
459 bitmap_copy (vr->equiv, equiv);
460 else
461 bitmap_clear (vr->equiv);
466 /* Set value range VR to the canonical form of {T, MIN, MAX, EQUIV}.
467 This means adjusting T, MIN and MAX representing the case of a
468 wrapping range with MAX < MIN covering [MIN, type_max] U [type_min, MAX]
469 as anti-rage ~[MAX+1, MIN-1]. Likewise for wrapping anti-ranges.
470 In corner cases where MAX+1 or MIN-1 wraps this will fall back
471 to varying.
472 This routine exists to ease canonicalization in the case where we
473 extract ranges from var + CST op limit. */
475 static void
476 set_and_canonicalize_value_range (value_range_t *vr, enum value_range_type t,
477 tree min, tree max, bitmap equiv)
479 /* Use the canonical setters for VR_UNDEFINED and VR_VARYING. */
480 if (t == VR_UNDEFINED)
482 set_value_range_to_undefined (vr);
483 return;
485 else if (t == VR_VARYING)
487 set_value_range_to_varying (vr);
488 return;
491 /* Nothing to canonicalize for symbolic ranges. */
492 if (TREE_CODE (min) != INTEGER_CST
493 || TREE_CODE (max) != INTEGER_CST)
495 set_value_range (vr, t, min, max, equiv);
496 return;
499 /* Wrong order for min and max, to swap them and the VR type we need
500 to adjust them. */
501 if (tree_int_cst_lt (max, min))
503 tree one = build_int_cst (TREE_TYPE (min), 1);
504 tree tmp = int_const_binop (PLUS_EXPR, max, one);
505 max = int_const_binop (MINUS_EXPR, min, one);
506 min = tmp;
508 /* There's one corner case, if we had [C+1, C] before we now have
509 that again. But this represents an empty value range, so drop
510 to varying in this case. */
511 if (tree_int_cst_lt (max, min))
513 set_value_range_to_varying (vr);
514 return;
517 t = t == VR_RANGE ? VR_ANTI_RANGE : VR_RANGE;
520 /* Anti-ranges that can be represented as ranges should be so. */
521 if (t == VR_ANTI_RANGE)
523 bool is_min = vrp_val_is_min (min);
524 bool is_max = vrp_val_is_max (max);
526 if (is_min && is_max)
528 /* We cannot deal with empty ranges, drop to varying.
529 ??? This could be VR_UNDEFINED instead. */
530 set_value_range_to_varying (vr);
531 return;
533 else if (is_min
534 /* As a special exception preserve non-null ranges. */
535 && !(TYPE_UNSIGNED (TREE_TYPE (min))
536 && integer_zerop (max)))
538 tree one = build_int_cst (TREE_TYPE (max), 1);
539 min = int_const_binop (PLUS_EXPR, max, one);
540 max = vrp_val_max (TREE_TYPE (max));
541 t = VR_RANGE;
543 else if (is_max)
545 tree one = build_int_cst (TREE_TYPE (min), 1);
546 max = int_const_binop (MINUS_EXPR, min, one);
547 min = vrp_val_min (TREE_TYPE (min));
548 t = VR_RANGE;
552 /* Drop [-INF(OVF), +INF(OVF)] to varying. */
553 if (needs_overflow_infinity (TREE_TYPE (min))
554 && is_overflow_infinity (min)
555 && is_overflow_infinity (max))
557 set_value_range_to_varying (vr);
558 return;
561 set_value_range (vr, t, min, max, equiv);
564 /* Copy value range FROM into value range TO. */
566 static inline void
567 copy_value_range (value_range_t *to, value_range_t *from)
569 set_value_range (to, from->type, from->min, from->max, from->equiv);
572 /* Set value range VR to a single value. This function is only called
573 with values we get from statements, and exists to clear the
574 TREE_OVERFLOW flag so that we don't think we have an overflow
575 infinity when we shouldn't. */
577 static inline void
578 set_value_range_to_value (value_range_t *vr, tree val, bitmap equiv)
580 gcc_assert (is_gimple_min_invariant (val));
581 val = avoid_overflow_infinity (val);
582 set_value_range (vr, VR_RANGE, val, val, equiv);
585 /* Set value range VR to a non-negative range of type TYPE.
586 OVERFLOW_INFINITY indicates whether to use an overflow infinity
587 rather than TYPE_MAX_VALUE; this should be true if we determine
588 that the range is nonnegative based on the assumption that signed
589 overflow does not occur. */
591 static inline void
592 set_value_range_to_nonnegative (value_range_t *vr, tree type,
593 bool overflow_infinity)
595 tree zero;
597 if (overflow_infinity && !supports_overflow_infinity (type))
599 set_value_range_to_varying (vr);
600 return;
603 zero = build_int_cst (type, 0);
604 set_value_range (vr, VR_RANGE, zero,
605 (overflow_infinity
606 ? positive_overflow_infinity (type)
607 : TYPE_MAX_VALUE (type)),
608 vr->equiv);
611 /* Set value range VR to a non-NULL range of type TYPE. */
613 static inline void
614 set_value_range_to_nonnull (value_range_t *vr, tree type)
616 tree zero = build_int_cst (type, 0);
617 set_value_range (vr, VR_ANTI_RANGE, zero, zero, vr->equiv);
621 /* Set value range VR to a NULL range of type TYPE. */
623 static inline void
624 set_value_range_to_null (value_range_t *vr, tree type)
626 set_value_range_to_value (vr, build_int_cst (type, 0), vr->equiv);
630 /* Set value range VR to a range of a truthvalue of type TYPE. */
632 static inline void
633 set_value_range_to_truthvalue (value_range_t *vr, tree type)
635 if (TYPE_PRECISION (type) == 1)
636 set_value_range_to_varying (vr);
637 else
638 set_value_range (vr, VR_RANGE,
639 build_int_cst (type, 0), build_int_cst (type, 1),
640 vr->equiv);
644 /* If abs (min) < abs (max), set VR to [-max, max], if
645 abs (min) >= abs (max), set VR to [-min, min]. */
647 static void
648 abs_extent_range (value_range_t *vr, tree min, tree max)
650 int cmp;
652 gcc_assert (TREE_CODE (min) == INTEGER_CST);
653 gcc_assert (TREE_CODE (max) == INTEGER_CST);
654 gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (min)));
655 gcc_assert (!TYPE_UNSIGNED (TREE_TYPE (min)));
656 min = fold_unary (ABS_EXPR, TREE_TYPE (min), min);
657 max = fold_unary (ABS_EXPR, TREE_TYPE (max), max);
658 if (TREE_OVERFLOW (min) || TREE_OVERFLOW (max))
660 set_value_range_to_varying (vr);
661 return;
663 cmp = compare_values (min, max);
664 if (cmp == -1)
665 min = fold_unary (NEGATE_EXPR, TREE_TYPE (min), max);
666 else if (cmp == 0 || cmp == 1)
668 max = min;
669 min = fold_unary (NEGATE_EXPR, TREE_TYPE (min), min);
671 else
673 set_value_range_to_varying (vr);
674 return;
676 set_and_canonicalize_value_range (vr, VR_RANGE, min, max, NULL);
680 /* Return value range information for VAR.
682 If we have no values ranges recorded (ie, VRP is not running), then
683 return NULL. Otherwise create an empty range if none existed for VAR. */
685 static value_range_t *
686 get_value_range (const_tree var)
688 static const struct value_range_d vr_const_varying
689 = { VR_VARYING, NULL_TREE, NULL_TREE, NULL };
690 value_range_t *vr;
691 tree sym;
692 unsigned ver = SSA_NAME_VERSION (var);
694 /* If we have no recorded ranges, then return NULL. */
695 if (! vr_value)
696 return NULL;
698 /* If we query the range for a new SSA name return an unmodifiable VARYING.
699 We should get here at most from the substitute-and-fold stage which
700 will never try to change values. */
701 if (ver >= num_vr_values)
702 return CONST_CAST (value_range_t *, &vr_const_varying);
704 vr = vr_value[ver];
705 if (vr)
706 return vr;
708 /* After propagation finished do not allocate new value-ranges. */
709 if (values_propagated)
710 return CONST_CAST (value_range_t *, &vr_const_varying);
712 /* Create a default value range. */
713 vr_value[ver] = vr = XCNEW (value_range_t);
715 /* Defer allocating the equivalence set. */
716 vr->equiv = NULL;
718 /* If VAR is a default definition of a parameter, the variable can
719 take any value in VAR's type. */
720 sym = SSA_NAME_VAR (var);
721 if (SSA_NAME_IS_DEFAULT_DEF (var))
723 if (TREE_CODE (sym) == PARM_DECL)
725 /* Try to use the "nonnull" attribute to create ~[0, 0]
726 anti-ranges for pointers. Note that this is only valid with
727 default definitions of PARM_DECLs. */
728 if (POINTER_TYPE_P (TREE_TYPE (sym))
729 && nonnull_arg_p (sym))
730 set_value_range_to_nonnull (vr, TREE_TYPE (sym));
731 else
732 set_value_range_to_varying (vr);
734 else if (TREE_CODE (sym) == RESULT_DECL
735 && DECL_BY_REFERENCE (sym))
736 set_value_range_to_nonnull (vr, TREE_TYPE (sym));
739 return vr;
742 /* Return true, if VAL1 and VAL2 are equal values for VRP purposes. */
744 static inline bool
745 vrp_operand_equal_p (const_tree val1, const_tree val2)
747 if (val1 == val2)
748 return true;
749 if (!val1 || !val2 || !operand_equal_p (val1, val2, 0))
750 return false;
751 if (is_overflow_infinity (val1))
752 return is_overflow_infinity (val2);
753 return true;
756 /* Return true, if the bitmaps B1 and B2 are equal. */
758 static inline bool
759 vrp_bitmap_equal_p (const_bitmap b1, const_bitmap b2)
761 return (b1 == b2
762 || ((!b1 || bitmap_empty_p (b1))
763 && (!b2 || bitmap_empty_p (b2)))
764 || (b1 && b2
765 && bitmap_equal_p (b1, b2)));
768 /* Update the value range and equivalence set for variable VAR to
769 NEW_VR. Return true if NEW_VR is different from VAR's previous
770 value.
772 NOTE: This function assumes that NEW_VR is a temporary value range
773 object created for the sole purpose of updating VAR's range. The
774 storage used by the equivalence set from NEW_VR will be freed by
775 this function. Do not call update_value_range when NEW_VR
776 is the range object associated with another SSA name. */
778 static inline bool
779 update_value_range (const_tree var, value_range_t *new_vr)
781 value_range_t *old_vr;
782 bool is_new;
784 /* Update the value range, if necessary. */
785 old_vr = get_value_range (var);
786 is_new = old_vr->type != new_vr->type
787 || !vrp_operand_equal_p (old_vr->min, new_vr->min)
788 || !vrp_operand_equal_p (old_vr->max, new_vr->max)
789 || !vrp_bitmap_equal_p (old_vr->equiv, new_vr->equiv);
791 if (is_new)
792 set_value_range (old_vr, new_vr->type, new_vr->min, new_vr->max,
793 new_vr->equiv);
795 BITMAP_FREE (new_vr->equiv);
797 return is_new;
801 /* Add VAR and VAR's equivalence set to EQUIV. This is the central
802 point where equivalence processing can be turned on/off. */
804 static void
805 add_equivalence (bitmap *equiv, const_tree var)
807 unsigned ver = SSA_NAME_VERSION (var);
808 value_range_t *vr = vr_value[ver];
810 if (*equiv == NULL)
811 *equiv = BITMAP_ALLOC (NULL);
812 bitmap_set_bit (*equiv, ver);
813 if (vr && vr->equiv)
814 bitmap_ior_into (*equiv, vr->equiv);
818 /* Return true if VR is ~[0, 0]. */
820 static inline bool
821 range_is_nonnull (value_range_t *vr)
823 return vr->type == VR_ANTI_RANGE
824 && integer_zerop (vr->min)
825 && integer_zerop (vr->max);
829 /* Return true if VR is [0, 0]. */
831 static inline bool
832 range_is_null (value_range_t *vr)
834 return vr->type == VR_RANGE
835 && integer_zerop (vr->min)
836 && integer_zerop (vr->max);
839 /* Return true if max and min of VR are INTEGER_CST. It's not necessary
840 a singleton. */
842 static inline bool
843 range_int_cst_p (value_range_t *vr)
845 return (vr->type == VR_RANGE
846 && TREE_CODE (vr->max) == INTEGER_CST
847 && TREE_CODE (vr->min) == INTEGER_CST);
850 /* Return true if VR is a INTEGER_CST singleton. */
852 static inline bool
853 range_int_cst_singleton_p (value_range_t *vr)
855 return (range_int_cst_p (vr)
856 && !TREE_OVERFLOW (vr->min)
857 && !TREE_OVERFLOW (vr->max)
858 && tree_int_cst_equal (vr->min, vr->max));
861 /* Return true if value range VR involves at least one symbol. */
863 static inline bool
864 symbolic_range_p (value_range_t *vr)
866 return (!is_gimple_min_invariant (vr->min)
867 || !is_gimple_min_invariant (vr->max));
870 /* Return true if value range VR uses an overflow infinity. */
872 static inline bool
873 overflow_infinity_range_p (value_range_t *vr)
875 return (vr->type == VR_RANGE
876 && (is_overflow_infinity (vr->min)
877 || is_overflow_infinity (vr->max)));
880 /* Return false if we can not make a valid comparison based on VR;
881 this will be the case if it uses an overflow infinity and overflow
882 is not undefined (i.e., -fno-strict-overflow is in effect).
883 Otherwise return true, and set *STRICT_OVERFLOW_P to true if VR
884 uses an overflow infinity. */
886 static bool
887 usable_range_p (value_range_t *vr, bool *strict_overflow_p)
889 gcc_assert (vr->type == VR_RANGE);
890 if (is_overflow_infinity (vr->min))
892 *strict_overflow_p = true;
893 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (vr->min)))
894 return false;
896 if (is_overflow_infinity (vr->max))
898 *strict_overflow_p = true;
899 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (vr->max)))
900 return false;
902 return true;
906 /* Return true if the result of assignment STMT is know to be non-negative.
907 If the return value is based on the assumption that signed overflow is
908 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
909 *STRICT_OVERFLOW_P.*/
911 static bool
912 gimple_assign_nonnegative_warnv_p (gimple stmt, bool *strict_overflow_p)
914 enum tree_code code = gimple_assign_rhs_code (stmt);
915 switch (get_gimple_rhs_class (code))
917 case GIMPLE_UNARY_RHS:
918 return tree_unary_nonnegative_warnv_p (gimple_assign_rhs_code (stmt),
919 gimple_expr_type (stmt),
920 gimple_assign_rhs1 (stmt),
921 strict_overflow_p);
922 case GIMPLE_BINARY_RHS:
923 return tree_binary_nonnegative_warnv_p (gimple_assign_rhs_code (stmt),
924 gimple_expr_type (stmt),
925 gimple_assign_rhs1 (stmt),
926 gimple_assign_rhs2 (stmt),
927 strict_overflow_p);
928 case GIMPLE_TERNARY_RHS:
929 return false;
930 case GIMPLE_SINGLE_RHS:
931 return tree_single_nonnegative_warnv_p (gimple_assign_rhs1 (stmt),
932 strict_overflow_p);
933 case GIMPLE_INVALID_RHS:
934 gcc_unreachable ();
935 default:
936 gcc_unreachable ();
940 /* Return true if return value of call STMT is know to be non-negative.
941 If the return value is based on the assumption that signed overflow is
942 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
943 *STRICT_OVERFLOW_P.*/
945 static bool
946 gimple_call_nonnegative_warnv_p (gimple stmt, bool *strict_overflow_p)
948 tree arg0 = gimple_call_num_args (stmt) > 0 ?
949 gimple_call_arg (stmt, 0) : NULL_TREE;
950 tree arg1 = gimple_call_num_args (stmt) > 1 ?
951 gimple_call_arg (stmt, 1) : NULL_TREE;
953 return tree_call_nonnegative_warnv_p (gimple_expr_type (stmt),
954 gimple_call_fndecl (stmt),
955 arg0,
956 arg1,
957 strict_overflow_p);
960 /* Return true if STMT is know to to compute a non-negative value.
961 If the return value is based on the assumption that signed overflow is
962 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
963 *STRICT_OVERFLOW_P.*/
965 static bool
966 gimple_stmt_nonnegative_warnv_p (gimple stmt, bool *strict_overflow_p)
968 switch (gimple_code (stmt))
970 case GIMPLE_ASSIGN:
971 return gimple_assign_nonnegative_warnv_p (stmt, strict_overflow_p);
972 case GIMPLE_CALL:
973 return gimple_call_nonnegative_warnv_p (stmt, strict_overflow_p);
974 default:
975 gcc_unreachable ();
979 /* Return true if the result of assignment STMT is know to be non-zero.
980 If the return value is based on the assumption that signed overflow is
981 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
982 *STRICT_OVERFLOW_P.*/
984 static bool
985 gimple_assign_nonzero_warnv_p (gimple stmt, bool *strict_overflow_p)
987 enum tree_code code = gimple_assign_rhs_code (stmt);
988 switch (get_gimple_rhs_class (code))
990 case GIMPLE_UNARY_RHS:
991 return tree_unary_nonzero_warnv_p (gimple_assign_rhs_code (stmt),
992 gimple_expr_type (stmt),
993 gimple_assign_rhs1 (stmt),
994 strict_overflow_p);
995 case GIMPLE_BINARY_RHS:
996 return tree_binary_nonzero_warnv_p (gimple_assign_rhs_code (stmt),
997 gimple_expr_type (stmt),
998 gimple_assign_rhs1 (stmt),
999 gimple_assign_rhs2 (stmt),
1000 strict_overflow_p);
1001 case GIMPLE_TERNARY_RHS:
1002 return false;
1003 case GIMPLE_SINGLE_RHS:
1004 return tree_single_nonzero_warnv_p (gimple_assign_rhs1 (stmt),
1005 strict_overflow_p);
1006 case GIMPLE_INVALID_RHS:
1007 gcc_unreachable ();
1008 default:
1009 gcc_unreachable ();
1013 /* Return true if STMT is know to to compute a non-zero value.
1014 If the return value is based on the assumption that signed overflow is
1015 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1016 *STRICT_OVERFLOW_P.*/
1018 static bool
1019 gimple_stmt_nonzero_warnv_p (gimple stmt, bool *strict_overflow_p)
1021 switch (gimple_code (stmt))
1023 case GIMPLE_ASSIGN:
1024 return gimple_assign_nonzero_warnv_p (stmt, strict_overflow_p);
1025 case GIMPLE_CALL:
1026 return gimple_alloca_call_p (stmt);
1027 default:
1028 gcc_unreachable ();
1032 /* Like tree_expr_nonzero_warnv_p, but this function uses value ranges
1033 obtained so far. */
1035 static bool
1036 vrp_stmt_computes_nonzero (gimple stmt, bool *strict_overflow_p)
1038 if (gimple_stmt_nonzero_warnv_p (stmt, strict_overflow_p))
1039 return true;
1041 /* If we have an expression of the form &X->a, then the expression
1042 is nonnull if X is nonnull. */
1043 if (is_gimple_assign (stmt)
1044 && gimple_assign_rhs_code (stmt) == ADDR_EXPR)
1046 tree expr = gimple_assign_rhs1 (stmt);
1047 tree base = get_base_address (TREE_OPERAND (expr, 0));
1049 if (base != NULL_TREE
1050 && TREE_CODE (base) == MEM_REF
1051 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
1053 value_range_t *vr = get_value_range (TREE_OPERAND (base, 0));
1054 if (range_is_nonnull (vr))
1055 return true;
1059 return false;
1062 /* Returns true if EXPR is a valid value (as expected by compare_values) --
1063 a gimple invariant, or SSA_NAME +- CST. */
1065 static bool
1066 valid_value_p (tree expr)
1068 if (TREE_CODE (expr) == SSA_NAME)
1069 return true;
1071 if (TREE_CODE (expr) == PLUS_EXPR
1072 || TREE_CODE (expr) == MINUS_EXPR)
1073 return (TREE_CODE (TREE_OPERAND (expr, 0)) == SSA_NAME
1074 && TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST);
1076 return is_gimple_min_invariant (expr);
1079 /* Return
1080 1 if VAL < VAL2
1081 0 if !(VAL < VAL2)
1082 -2 if those are incomparable. */
1083 static inline int
1084 operand_less_p (tree val, tree val2)
1086 /* LT is folded faster than GE and others. Inline the common case. */
1087 if (TREE_CODE (val) == INTEGER_CST && TREE_CODE (val2) == INTEGER_CST)
1089 if (TYPE_UNSIGNED (TREE_TYPE (val)))
1090 return INT_CST_LT_UNSIGNED (val, val2);
1091 else
1093 if (INT_CST_LT (val, val2))
1094 return 1;
1097 else
1099 tree tcmp;
1101 fold_defer_overflow_warnings ();
1103 tcmp = fold_binary_to_constant (LT_EXPR, boolean_type_node, val, val2);
1105 fold_undefer_and_ignore_overflow_warnings ();
1107 if (!tcmp
1108 || TREE_CODE (tcmp) != INTEGER_CST)
1109 return -2;
1111 if (!integer_zerop (tcmp))
1112 return 1;
1115 /* val >= val2, not considering overflow infinity. */
1116 if (is_negative_overflow_infinity (val))
1117 return is_negative_overflow_infinity (val2) ? 0 : 1;
1118 else if (is_positive_overflow_infinity (val2))
1119 return is_positive_overflow_infinity (val) ? 0 : 1;
1121 return 0;
1124 /* Compare two values VAL1 and VAL2. Return
1126 -2 if VAL1 and VAL2 cannot be compared at compile-time,
1127 -1 if VAL1 < VAL2,
1128 0 if VAL1 == VAL2,
1129 +1 if VAL1 > VAL2, and
1130 +2 if VAL1 != VAL2
1132 This is similar to tree_int_cst_compare but supports pointer values
1133 and values that cannot be compared at compile time.
1135 If STRICT_OVERFLOW_P is not NULL, then set *STRICT_OVERFLOW_P to
1136 true if the return value is only valid if we assume that signed
1137 overflow is undefined. */
1139 static int
1140 compare_values_warnv (tree val1, tree val2, bool *strict_overflow_p)
1142 if (val1 == val2)
1143 return 0;
1145 /* Below we rely on the fact that VAL1 and VAL2 are both pointers or
1146 both integers. */
1147 gcc_assert (POINTER_TYPE_P (TREE_TYPE (val1))
1148 == POINTER_TYPE_P (TREE_TYPE (val2)));
1149 /* Convert the two values into the same type. This is needed because
1150 sizetype causes sign extension even for unsigned types. */
1151 val2 = fold_convert (TREE_TYPE (val1), val2);
1152 STRIP_USELESS_TYPE_CONVERSION (val2);
1154 if ((TREE_CODE (val1) == SSA_NAME
1155 || TREE_CODE (val1) == PLUS_EXPR
1156 || TREE_CODE (val1) == MINUS_EXPR)
1157 && (TREE_CODE (val2) == SSA_NAME
1158 || TREE_CODE (val2) == PLUS_EXPR
1159 || TREE_CODE (val2) == MINUS_EXPR))
1161 tree n1, c1, n2, c2;
1162 enum tree_code code1, code2;
1164 /* If VAL1 and VAL2 are of the form 'NAME [+-] CST' or 'NAME',
1165 return -1 or +1 accordingly. If VAL1 and VAL2 don't use the
1166 same name, return -2. */
1167 if (TREE_CODE (val1) == SSA_NAME)
1169 code1 = SSA_NAME;
1170 n1 = val1;
1171 c1 = NULL_TREE;
1173 else
1175 code1 = TREE_CODE (val1);
1176 n1 = TREE_OPERAND (val1, 0);
1177 c1 = TREE_OPERAND (val1, 1);
1178 if (tree_int_cst_sgn (c1) == -1)
1180 if (is_negative_overflow_infinity (c1))
1181 return -2;
1182 c1 = fold_unary_to_constant (NEGATE_EXPR, TREE_TYPE (c1), c1);
1183 if (!c1)
1184 return -2;
1185 code1 = code1 == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR;
1189 if (TREE_CODE (val2) == SSA_NAME)
1191 code2 = SSA_NAME;
1192 n2 = val2;
1193 c2 = NULL_TREE;
1195 else
1197 code2 = TREE_CODE (val2);
1198 n2 = TREE_OPERAND (val2, 0);
1199 c2 = TREE_OPERAND (val2, 1);
1200 if (tree_int_cst_sgn (c2) == -1)
1202 if (is_negative_overflow_infinity (c2))
1203 return -2;
1204 c2 = fold_unary_to_constant (NEGATE_EXPR, TREE_TYPE (c2), c2);
1205 if (!c2)
1206 return -2;
1207 code2 = code2 == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR;
1211 /* Both values must use the same name. */
1212 if (n1 != n2)
1213 return -2;
1215 if (code1 == SSA_NAME
1216 && code2 == SSA_NAME)
1217 /* NAME == NAME */
1218 return 0;
1220 /* If overflow is defined we cannot simplify more. */
1221 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (val1)))
1222 return -2;
1224 if (strict_overflow_p != NULL
1225 && (code1 == SSA_NAME || !TREE_NO_WARNING (val1))
1226 && (code2 == SSA_NAME || !TREE_NO_WARNING (val2)))
1227 *strict_overflow_p = true;
1229 if (code1 == SSA_NAME)
1231 if (code2 == PLUS_EXPR)
1232 /* NAME < NAME + CST */
1233 return -1;
1234 else if (code2 == MINUS_EXPR)
1235 /* NAME > NAME - CST */
1236 return 1;
1238 else if (code1 == PLUS_EXPR)
1240 if (code2 == SSA_NAME)
1241 /* NAME + CST > NAME */
1242 return 1;
1243 else if (code2 == PLUS_EXPR)
1244 /* NAME + CST1 > NAME + CST2, if CST1 > CST2 */
1245 return compare_values_warnv (c1, c2, strict_overflow_p);
1246 else if (code2 == MINUS_EXPR)
1247 /* NAME + CST1 > NAME - CST2 */
1248 return 1;
1250 else if (code1 == MINUS_EXPR)
1252 if (code2 == SSA_NAME)
1253 /* NAME - CST < NAME */
1254 return -1;
1255 else if (code2 == PLUS_EXPR)
1256 /* NAME - CST1 < NAME + CST2 */
1257 return -1;
1258 else if (code2 == MINUS_EXPR)
1259 /* NAME - CST1 > NAME - CST2, if CST1 < CST2. Notice that
1260 C1 and C2 are swapped in the call to compare_values. */
1261 return compare_values_warnv (c2, c1, strict_overflow_p);
1264 gcc_unreachable ();
1267 /* We cannot compare non-constants. */
1268 if (!is_gimple_min_invariant (val1) || !is_gimple_min_invariant (val2))
1269 return -2;
1271 if (!POINTER_TYPE_P (TREE_TYPE (val1)))
1273 /* We cannot compare overflowed values, except for overflow
1274 infinities. */
1275 if (TREE_OVERFLOW (val1) || TREE_OVERFLOW (val2))
1277 if (strict_overflow_p != NULL)
1278 *strict_overflow_p = true;
1279 if (is_negative_overflow_infinity (val1))
1280 return is_negative_overflow_infinity (val2) ? 0 : -1;
1281 else if (is_negative_overflow_infinity (val2))
1282 return 1;
1283 else if (is_positive_overflow_infinity (val1))
1284 return is_positive_overflow_infinity (val2) ? 0 : 1;
1285 else if (is_positive_overflow_infinity (val2))
1286 return -1;
1287 return -2;
1290 return tree_int_cst_compare (val1, val2);
1292 else
1294 tree t;
1296 /* First see if VAL1 and VAL2 are not the same. */
1297 if (val1 == val2 || operand_equal_p (val1, val2, 0))
1298 return 0;
1300 /* If VAL1 is a lower address than VAL2, return -1. */
1301 if (operand_less_p (val1, val2) == 1)
1302 return -1;
1304 /* If VAL1 is a higher address than VAL2, return +1. */
1305 if (operand_less_p (val2, val1) == 1)
1306 return 1;
1308 /* If VAL1 is different than VAL2, return +2.
1309 For integer constants we either have already returned -1 or 1
1310 or they are equivalent. We still might succeed in proving
1311 something about non-trivial operands. */
1312 if (TREE_CODE (val1) != INTEGER_CST
1313 || TREE_CODE (val2) != INTEGER_CST)
1315 t = fold_binary_to_constant (NE_EXPR, boolean_type_node, val1, val2);
1316 if (t && integer_onep (t))
1317 return 2;
1320 return -2;
1324 /* Compare values like compare_values_warnv, but treat comparisons of
1325 nonconstants which rely on undefined overflow as incomparable. */
1327 static int
1328 compare_values (tree val1, tree val2)
1330 bool sop;
1331 int ret;
1333 sop = false;
1334 ret = compare_values_warnv (val1, val2, &sop);
1335 if (sop
1336 && (!is_gimple_min_invariant (val1) || !is_gimple_min_invariant (val2)))
1337 ret = -2;
1338 return ret;
1342 /* Return 1 if VAL is inside value range VR (VR->MIN <= VAL <= VR->MAX),
1343 0 if VAL is not inside VR,
1344 -2 if we cannot tell either way.
1346 FIXME, the current semantics of this functions are a bit quirky
1347 when taken in the context of VRP. In here we do not care
1348 about VR's type. If VR is the anti-range ~[3, 5] the call
1349 value_inside_range (4, VR) will return 1.
1351 This is counter-intuitive in a strict sense, but the callers
1352 currently expect this. They are calling the function
1353 merely to determine whether VR->MIN <= VAL <= VR->MAX. The
1354 callers are applying the VR_RANGE/VR_ANTI_RANGE semantics
1355 themselves.
1357 This also applies to value_ranges_intersect_p and
1358 range_includes_zero_p. The semantics of VR_RANGE and
1359 VR_ANTI_RANGE should be encoded here, but that also means
1360 adapting the users of these functions to the new semantics.
1362 Benchmark compile/20001226-1.c compilation time after changing this
1363 function. */
1365 static inline int
1366 value_inside_range (tree val, value_range_t * vr)
1368 int cmp1, cmp2;
1370 cmp1 = operand_less_p (val, vr->min);
1371 if (cmp1 == -2)
1372 return -2;
1373 if (cmp1 == 1)
1374 return 0;
1376 cmp2 = operand_less_p (vr->max, val);
1377 if (cmp2 == -2)
1378 return -2;
1380 return !cmp2;
1384 /* Return true if value ranges VR0 and VR1 have a non-empty
1385 intersection.
1387 Benchmark compile/20001226-1.c compilation time after changing this
1388 function.
1391 static inline bool
1392 value_ranges_intersect_p (value_range_t *vr0, value_range_t *vr1)
1394 /* The value ranges do not intersect if the maximum of the first range is
1395 less than the minimum of the second range or vice versa.
1396 When those relations are unknown, we can't do any better. */
1397 if (operand_less_p (vr0->max, vr1->min) != 0)
1398 return false;
1399 if (operand_less_p (vr1->max, vr0->min) != 0)
1400 return false;
1401 return true;
1405 /* Return true if VR includes the value zero, false otherwise. FIXME,
1406 currently this will return false for an anti-range like ~[-4, 3].
1407 This will be wrong when the semantics of value_inside_range are
1408 modified (currently the users of this function expect these
1409 semantics). */
1411 static inline bool
1412 range_includes_zero_p (value_range_t *vr)
1414 tree zero;
1416 gcc_assert (vr->type != VR_UNDEFINED
1417 && vr->type != VR_VARYING
1418 && !symbolic_range_p (vr));
1420 zero = build_int_cst (TREE_TYPE (vr->min), 0);
1421 return (value_inside_range (zero, vr) == 1);
1424 /* Return true if *VR is know to only contain nonnegative values. */
1426 static inline bool
1427 value_range_nonnegative_p (value_range_t *vr)
1429 /* Testing for VR_ANTI_RANGE is not useful here as any anti-range
1430 which would return a useful value should be encoded as a
1431 VR_RANGE. */
1432 if (vr->type == VR_RANGE)
1434 int result = compare_values (vr->min, integer_zero_node);
1435 return (result == 0 || result == 1);
1438 return false;
1441 /* Return true if T, an SSA_NAME, is known to be nonnegative. Return
1442 false otherwise or if no value range information is available. */
1444 bool
1445 ssa_name_nonnegative_p (const_tree t)
1447 value_range_t *vr = get_value_range (t);
1449 if (INTEGRAL_TYPE_P (t)
1450 && TYPE_UNSIGNED (t))
1451 return true;
1453 if (!vr)
1454 return false;
1456 return value_range_nonnegative_p (vr);
1459 /* If *VR has a value rante that is a single constant value return that,
1460 otherwise return NULL_TREE. */
1462 static tree
1463 value_range_constant_singleton (value_range_t *vr)
1465 if (vr->type == VR_RANGE
1466 && operand_equal_p (vr->min, vr->max, 0)
1467 && is_gimple_min_invariant (vr->min))
1468 return vr->min;
1470 return NULL_TREE;
1473 /* If OP has a value range with a single constant value return that,
1474 otherwise return NULL_TREE. This returns OP itself if OP is a
1475 constant. */
1477 static tree
1478 op_with_constant_singleton_value_range (tree op)
1480 if (is_gimple_min_invariant (op))
1481 return op;
1483 if (TREE_CODE (op) != SSA_NAME)
1484 return NULL_TREE;
1486 return value_range_constant_singleton (get_value_range (op));
1489 /* Return true if op is in a boolean [0, 1] value-range. */
1491 static bool
1492 op_with_boolean_value_range_p (tree op)
1494 value_range_t *vr;
1496 if (TYPE_PRECISION (TREE_TYPE (op)) == 1)
1497 return true;
1499 if (integer_zerop (op)
1500 || integer_onep (op))
1501 return true;
1503 if (TREE_CODE (op) != SSA_NAME)
1504 return false;
1506 vr = get_value_range (op);
1507 return (vr->type == VR_RANGE
1508 && integer_zerop (vr->min)
1509 && integer_onep (vr->max));
1512 /* Extract value range information from an ASSERT_EXPR EXPR and store
1513 it in *VR_P. */
1515 static void
1516 extract_range_from_assert (value_range_t *vr_p, tree expr)
1518 tree var, cond, limit, min, max, type;
1519 value_range_t *limit_vr;
1520 enum tree_code cond_code;
1522 var = ASSERT_EXPR_VAR (expr);
1523 cond = ASSERT_EXPR_COND (expr);
1525 gcc_assert (COMPARISON_CLASS_P (cond));
1527 /* Find VAR in the ASSERT_EXPR conditional. */
1528 if (var == TREE_OPERAND (cond, 0)
1529 || TREE_CODE (TREE_OPERAND (cond, 0)) == PLUS_EXPR
1530 || TREE_CODE (TREE_OPERAND (cond, 0)) == NOP_EXPR)
1532 /* If the predicate is of the form VAR COMP LIMIT, then we just
1533 take LIMIT from the RHS and use the same comparison code. */
1534 cond_code = TREE_CODE (cond);
1535 limit = TREE_OPERAND (cond, 1);
1536 cond = TREE_OPERAND (cond, 0);
1538 else
1540 /* If the predicate is of the form LIMIT COMP VAR, then we need
1541 to flip around the comparison code to create the proper range
1542 for VAR. */
1543 cond_code = swap_tree_comparison (TREE_CODE (cond));
1544 limit = TREE_OPERAND (cond, 0);
1545 cond = TREE_OPERAND (cond, 1);
1548 limit = avoid_overflow_infinity (limit);
1550 type = TREE_TYPE (var);
1551 gcc_assert (limit != var);
1553 /* For pointer arithmetic, we only keep track of pointer equality
1554 and inequality. */
1555 if (POINTER_TYPE_P (type) && cond_code != NE_EXPR && cond_code != EQ_EXPR)
1557 set_value_range_to_varying (vr_p);
1558 return;
1561 /* If LIMIT is another SSA name and LIMIT has a range of its own,
1562 try to use LIMIT's range to avoid creating symbolic ranges
1563 unnecessarily. */
1564 limit_vr = (TREE_CODE (limit) == SSA_NAME) ? get_value_range (limit) : NULL;
1566 /* LIMIT's range is only interesting if it has any useful information. */
1567 if (limit_vr
1568 && (limit_vr->type == VR_UNDEFINED
1569 || limit_vr->type == VR_VARYING
1570 || symbolic_range_p (limit_vr)))
1571 limit_vr = NULL;
1573 /* Initially, the new range has the same set of equivalences of
1574 VAR's range. This will be revised before returning the final
1575 value. Since assertions may be chained via mutually exclusive
1576 predicates, we will need to trim the set of equivalences before
1577 we are done. */
1578 gcc_assert (vr_p->equiv == NULL);
1579 add_equivalence (&vr_p->equiv, var);
1581 /* Extract a new range based on the asserted comparison for VAR and
1582 LIMIT's value range. Notice that if LIMIT has an anti-range, we
1583 will only use it for equality comparisons (EQ_EXPR). For any
1584 other kind of assertion, we cannot derive a range from LIMIT's
1585 anti-range that can be used to describe the new range. For
1586 instance, ASSERT_EXPR <x_2, x_2 <= b_4>. If b_4 is ~[2, 10],
1587 then b_4 takes on the ranges [-INF, 1] and [11, +INF]. There is
1588 no single range for x_2 that could describe LE_EXPR, so we might
1589 as well build the range [b_4, +INF] for it.
1590 One special case we handle is extracting a range from a
1591 range test encoded as (unsigned)var + CST <= limit. */
1592 if (TREE_CODE (cond) == NOP_EXPR
1593 || TREE_CODE (cond) == PLUS_EXPR)
1595 if (TREE_CODE (cond) == PLUS_EXPR)
1597 min = fold_build1 (NEGATE_EXPR, TREE_TYPE (TREE_OPERAND (cond, 1)),
1598 TREE_OPERAND (cond, 1));
1599 max = int_const_binop (PLUS_EXPR, limit, min);
1600 cond = TREE_OPERAND (cond, 0);
1602 else
1604 min = build_int_cst (TREE_TYPE (var), 0);
1605 max = limit;
1608 /* Make sure to not set TREE_OVERFLOW on the final type
1609 conversion. We are willingly interpreting large positive
1610 unsigned values as negative singed values here. */
1611 min = force_fit_type_double (TREE_TYPE (var), tree_to_double_int (min),
1612 0, false);
1613 max = force_fit_type_double (TREE_TYPE (var), tree_to_double_int (max),
1614 0, false);
1616 /* We can transform a max, min range to an anti-range or
1617 vice-versa. Use set_and_canonicalize_value_range which does
1618 this for us. */
1619 if (cond_code == LE_EXPR)
1620 set_and_canonicalize_value_range (vr_p, VR_RANGE,
1621 min, max, vr_p->equiv);
1622 else if (cond_code == GT_EXPR)
1623 set_and_canonicalize_value_range (vr_p, VR_ANTI_RANGE,
1624 min, max, vr_p->equiv);
1625 else
1626 gcc_unreachable ();
1628 else if (cond_code == EQ_EXPR)
1630 enum value_range_type range_type;
1632 if (limit_vr)
1634 range_type = limit_vr->type;
1635 min = limit_vr->min;
1636 max = limit_vr->max;
1638 else
1640 range_type = VR_RANGE;
1641 min = limit;
1642 max = limit;
1645 set_value_range (vr_p, range_type, min, max, vr_p->equiv);
1647 /* When asserting the equality VAR == LIMIT and LIMIT is another
1648 SSA name, the new range will also inherit the equivalence set
1649 from LIMIT. */
1650 if (TREE_CODE (limit) == SSA_NAME)
1651 add_equivalence (&vr_p->equiv, limit);
1653 else if (cond_code == NE_EXPR)
1655 /* As described above, when LIMIT's range is an anti-range and
1656 this assertion is an inequality (NE_EXPR), then we cannot
1657 derive anything from the anti-range. For instance, if
1658 LIMIT's range was ~[0, 0], the assertion 'VAR != LIMIT' does
1659 not imply that VAR's range is [0, 0]. So, in the case of
1660 anti-ranges, we just assert the inequality using LIMIT and
1661 not its anti-range.
1663 If LIMIT_VR is a range, we can only use it to build a new
1664 anti-range if LIMIT_VR is a single-valued range. For
1665 instance, if LIMIT_VR is [0, 1], the predicate
1666 VAR != [0, 1] does not mean that VAR's range is ~[0, 1].
1667 Rather, it means that for value 0 VAR should be ~[0, 0]
1668 and for value 1, VAR should be ~[1, 1]. We cannot
1669 represent these ranges.
1671 The only situation in which we can build a valid
1672 anti-range is when LIMIT_VR is a single-valued range
1673 (i.e., LIMIT_VR->MIN == LIMIT_VR->MAX). In that case,
1674 build the anti-range ~[LIMIT_VR->MIN, LIMIT_VR->MAX]. */
1675 if (limit_vr
1676 && limit_vr->type == VR_RANGE
1677 && compare_values (limit_vr->min, limit_vr->max) == 0)
1679 min = limit_vr->min;
1680 max = limit_vr->max;
1682 else
1684 /* In any other case, we cannot use LIMIT's range to build a
1685 valid anti-range. */
1686 min = max = limit;
1689 /* If MIN and MAX cover the whole range for their type, then
1690 just use the original LIMIT. */
1691 if (INTEGRAL_TYPE_P (type)
1692 && vrp_val_is_min (min)
1693 && vrp_val_is_max (max))
1694 min = max = limit;
1696 set_value_range (vr_p, VR_ANTI_RANGE, min, max, vr_p->equiv);
1698 else if (cond_code == LE_EXPR || cond_code == LT_EXPR)
1700 min = TYPE_MIN_VALUE (type);
1702 if (limit_vr == NULL || limit_vr->type == VR_ANTI_RANGE)
1703 max = limit;
1704 else
1706 /* If LIMIT_VR is of the form [N1, N2], we need to build the
1707 range [MIN, N2] for LE_EXPR and [MIN, N2 - 1] for
1708 LT_EXPR. */
1709 max = limit_vr->max;
1712 /* If the maximum value forces us to be out of bounds, simply punt.
1713 It would be pointless to try and do anything more since this
1714 all should be optimized away above us. */
1715 if ((cond_code == LT_EXPR
1716 && compare_values (max, min) == 0)
1717 || (CONSTANT_CLASS_P (max) && TREE_OVERFLOW (max)))
1718 set_value_range_to_varying (vr_p);
1719 else
1721 /* For LT_EXPR, we create the range [MIN, MAX - 1]. */
1722 if (cond_code == LT_EXPR)
1724 if (TYPE_PRECISION (TREE_TYPE (max)) == 1
1725 && !TYPE_UNSIGNED (TREE_TYPE (max)))
1726 max = fold_build2 (PLUS_EXPR, TREE_TYPE (max), max,
1727 build_int_cst (TREE_TYPE (max), -1));
1728 else
1729 max = fold_build2 (MINUS_EXPR, TREE_TYPE (max), max,
1730 build_int_cst (TREE_TYPE (max), 1));
1731 if (EXPR_P (max))
1732 TREE_NO_WARNING (max) = 1;
1735 set_value_range (vr_p, VR_RANGE, min, max, vr_p->equiv);
1738 else if (cond_code == GE_EXPR || cond_code == GT_EXPR)
1740 max = TYPE_MAX_VALUE (type);
1742 if (limit_vr == NULL || limit_vr->type == VR_ANTI_RANGE)
1743 min = limit;
1744 else
1746 /* If LIMIT_VR is of the form [N1, N2], we need to build the
1747 range [N1, MAX] for GE_EXPR and [N1 + 1, MAX] for
1748 GT_EXPR. */
1749 min = limit_vr->min;
1752 /* If the minimum value forces us to be out of bounds, simply punt.
1753 It would be pointless to try and do anything more since this
1754 all should be optimized away above us. */
1755 if ((cond_code == GT_EXPR
1756 && compare_values (min, max) == 0)
1757 || (CONSTANT_CLASS_P (min) && TREE_OVERFLOW (min)))
1758 set_value_range_to_varying (vr_p);
1759 else
1761 /* For GT_EXPR, we create the range [MIN + 1, MAX]. */
1762 if (cond_code == GT_EXPR)
1764 if (TYPE_PRECISION (TREE_TYPE (min)) == 1
1765 && !TYPE_UNSIGNED (TREE_TYPE (min)))
1766 min = fold_build2 (MINUS_EXPR, TREE_TYPE (min), min,
1767 build_int_cst (TREE_TYPE (min), -1));
1768 else
1769 min = fold_build2 (PLUS_EXPR, TREE_TYPE (min), min,
1770 build_int_cst (TREE_TYPE (min), 1));
1771 if (EXPR_P (min))
1772 TREE_NO_WARNING (min) = 1;
1775 set_value_range (vr_p, VR_RANGE, min, max, vr_p->equiv);
1778 else
1779 gcc_unreachable ();
1781 /* Finally intersect the new range with what we already know about var. */
1782 vrp_intersect_ranges (vr_p, get_value_range (var));
1786 /* Extract range information from SSA name VAR and store it in VR. If
1787 VAR has an interesting range, use it. Otherwise, create the
1788 range [VAR, VAR] and return it. This is useful in situations where
1789 we may have conditionals testing values of VARYING names. For
1790 instance,
1792 x_3 = y_5;
1793 if (x_3 > y_5)
1796 Even if y_5 is deemed VARYING, we can determine that x_3 > y_5 is
1797 always false. */
1799 static void
1800 extract_range_from_ssa_name (value_range_t *vr, tree var)
1802 value_range_t *var_vr = get_value_range (var);
1804 if (var_vr->type != VR_UNDEFINED && var_vr->type != VR_VARYING)
1805 copy_value_range (vr, var_vr);
1806 else
1807 set_value_range (vr, VR_RANGE, var, var, NULL);
1809 add_equivalence (&vr->equiv, var);
1813 /* Wrapper around int_const_binop. If the operation overflows and we
1814 are not using wrapping arithmetic, then adjust the result to be
1815 -INF or +INF depending on CODE, VAL1 and VAL2. This can return
1816 NULL_TREE if we need to use an overflow infinity representation but
1817 the type does not support it. */
1819 static tree
1820 vrp_int_const_binop (enum tree_code code, tree val1, tree val2)
1822 tree res;
1824 res = int_const_binop (code, val1, val2);
1826 /* If we are using unsigned arithmetic, operate symbolically
1827 on -INF and +INF as int_const_binop only handles signed overflow. */
1828 if (TYPE_UNSIGNED (TREE_TYPE (val1)))
1830 int checkz = compare_values (res, val1);
1831 bool overflow = false;
1833 /* Ensure that res = val1 [+*] val2 >= val1
1834 or that res = val1 - val2 <= val1. */
1835 if ((code == PLUS_EXPR
1836 && !(checkz == 1 || checkz == 0))
1837 || (code == MINUS_EXPR
1838 && !(checkz == 0 || checkz == -1)))
1840 overflow = true;
1842 /* Checking for multiplication overflow is done by dividing the
1843 output of the multiplication by the first input of the
1844 multiplication. If the result of that division operation is
1845 not equal to the second input of the multiplication, then the
1846 multiplication overflowed. */
1847 else if (code == MULT_EXPR && !integer_zerop (val1))
1849 tree tmp = int_const_binop (TRUNC_DIV_EXPR,
1850 res,
1851 val1);
1852 int check = compare_values (tmp, val2);
1854 if (check != 0)
1855 overflow = true;
1858 if (overflow)
1860 res = copy_node (res);
1861 TREE_OVERFLOW (res) = 1;
1865 else if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (val1)))
1866 /* If the singed operation wraps then int_const_binop has done
1867 everything we want. */
1869 else if ((TREE_OVERFLOW (res)
1870 && !TREE_OVERFLOW (val1)
1871 && !TREE_OVERFLOW (val2))
1872 || is_overflow_infinity (val1)
1873 || is_overflow_infinity (val2))
1875 /* If the operation overflowed but neither VAL1 nor VAL2 are
1876 overflown, return -INF or +INF depending on the operation
1877 and the combination of signs of the operands. */
1878 int sgn1 = tree_int_cst_sgn (val1);
1879 int sgn2 = tree_int_cst_sgn (val2);
1881 if (needs_overflow_infinity (TREE_TYPE (res))
1882 && !supports_overflow_infinity (TREE_TYPE (res)))
1883 return NULL_TREE;
1885 /* We have to punt on adding infinities of different signs,
1886 since we can't tell what the sign of the result should be.
1887 Likewise for subtracting infinities of the same sign. */
1888 if (((code == PLUS_EXPR && sgn1 != sgn2)
1889 || (code == MINUS_EXPR && sgn1 == sgn2))
1890 && is_overflow_infinity (val1)
1891 && is_overflow_infinity (val2))
1892 return NULL_TREE;
1894 /* Don't try to handle division or shifting of infinities. */
1895 if ((code == TRUNC_DIV_EXPR
1896 || code == FLOOR_DIV_EXPR
1897 || code == CEIL_DIV_EXPR
1898 || code == EXACT_DIV_EXPR
1899 || code == ROUND_DIV_EXPR
1900 || code == RSHIFT_EXPR)
1901 && (is_overflow_infinity (val1)
1902 || is_overflow_infinity (val2)))
1903 return NULL_TREE;
1905 /* Notice that we only need to handle the restricted set of
1906 operations handled by extract_range_from_binary_expr.
1907 Among them, only multiplication, addition and subtraction
1908 can yield overflow without overflown operands because we
1909 are working with integral types only... except in the
1910 case VAL1 = -INF and VAL2 = -1 which overflows to +INF
1911 for division too. */
1913 /* For multiplication, the sign of the overflow is given
1914 by the comparison of the signs of the operands. */
1915 if ((code == MULT_EXPR && sgn1 == sgn2)
1916 /* For addition, the operands must be of the same sign
1917 to yield an overflow. Its sign is therefore that
1918 of one of the operands, for example the first. For
1919 infinite operands X + -INF is negative, not positive. */
1920 || (code == PLUS_EXPR
1921 && (sgn1 >= 0
1922 ? !is_negative_overflow_infinity (val2)
1923 : is_positive_overflow_infinity (val2)))
1924 /* For subtraction, non-infinite operands must be of
1925 different signs to yield an overflow. Its sign is
1926 therefore that of the first operand or the opposite of
1927 that of the second operand. A first operand of 0 counts
1928 as positive here, for the corner case 0 - (-INF), which
1929 overflows, but must yield +INF. For infinite operands 0
1930 - INF is negative, not positive. */
1931 || (code == MINUS_EXPR
1932 && (sgn1 >= 0
1933 ? !is_positive_overflow_infinity (val2)
1934 : is_negative_overflow_infinity (val2)))
1935 /* We only get in here with positive shift count, so the
1936 overflow direction is the same as the sign of val1.
1937 Actually rshift does not overflow at all, but we only
1938 handle the case of shifting overflowed -INF and +INF. */
1939 || (code == RSHIFT_EXPR
1940 && sgn1 >= 0)
1941 /* For division, the only case is -INF / -1 = +INF. */
1942 || code == TRUNC_DIV_EXPR
1943 || code == FLOOR_DIV_EXPR
1944 || code == CEIL_DIV_EXPR
1945 || code == EXACT_DIV_EXPR
1946 || code == ROUND_DIV_EXPR)
1947 return (needs_overflow_infinity (TREE_TYPE (res))
1948 ? positive_overflow_infinity (TREE_TYPE (res))
1949 : TYPE_MAX_VALUE (TREE_TYPE (res)));
1950 else
1951 return (needs_overflow_infinity (TREE_TYPE (res))
1952 ? negative_overflow_infinity (TREE_TYPE (res))
1953 : TYPE_MIN_VALUE (TREE_TYPE (res)));
1956 return res;
1960 /* For range VR compute two double_int bitmasks. In *MAY_BE_NONZERO
1961 bitmask if some bit is unset, it means for all numbers in the range
1962 the bit is 0, otherwise it might be 0 or 1. In *MUST_BE_NONZERO
1963 bitmask if some bit is set, it means for all numbers in the range
1964 the bit is 1, otherwise it might be 0 or 1. */
1966 static bool
1967 zero_nonzero_bits_from_vr (value_range_t *vr,
1968 double_int *may_be_nonzero,
1969 double_int *must_be_nonzero)
1971 *may_be_nonzero = double_int_minus_one;
1972 *must_be_nonzero = double_int_zero;
1973 if (!range_int_cst_p (vr)
1974 || TREE_OVERFLOW (vr->min)
1975 || TREE_OVERFLOW (vr->max))
1976 return false;
1978 if (range_int_cst_singleton_p (vr))
1980 *may_be_nonzero = tree_to_double_int (vr->min);
1981 *must_be_nonzero = *may_be_nonzero;
1983 else if (tree_int_cst_sgn (vr->min) >= 0
1984 || tree_int_cst_sgn (vr->max) < 0)
1986 double_int dmin = tree_to_double_int (vr->min);
1987 double_int dmax = tree_to_double_int (vr->max);
1988 double_int xor_mask = double_int_xor (dmin, dmax);
1989 *may_be_nonzero = double_int_ior (dmin, dmax);
1990 *must_be_nonzero = double_int_and (dmin, dmax);
1991 if (xor_mask.high != 0)
1993 unsigned HOST_WIDE_INT mask
1994 = ((unsigned HOST_WIDE_INT) 1
1995 << floor_log2 (xor_mask.high)) - 1;
1996 may_be_nonzero->low = ALL_ONES;
1997 may_be_nonzero->high |= mask;
1998 must_be_nonzero->low = 0;
1999 must_be_nonzero->high &= ~mask;
2001 else if (xor_mask.low != 0)
2003 unsigned HOST_WIDE_INT mask
2004 = ((unsigned HOST_WIDE_INT) 1
2005 << floor_log2 (xor_mask.low)) - 1;
2006 may_be_nonzero->low |= mask;
2007 must_be_nonzero->low &= ~mask;
2011 return true;
2014 /* Create two value-ranges in *VR0 and *VR1 from the anti-range *AR
2015 so that *VR0 U *VR1 == *AR. Returns true if that is possible,
2016 false otherwise. If *AR can be represented with a single range
2017 *VR1 will be VR_UNDEFINED. */
2019 static bool
2020 ranges_from_anti_range (value_range_t *ar,
2021 value_range_t *vr0, value_range_t *vr1)
2023 tree type = TREE_TYPE (ar->min);
2025 vr0->type = VR_UNDEFINED;
2026 vr1->type = VR_UNDEFINED;
2028 if (ar->type != VR_ANTI_RANGE
2029 || TREE_CODE (ar->min) != INTEGER_CST
2030 || TREE_CODE (ar->max) != INTEGER_CST
2031 || !vrp_val_min (type)
2032 || !vrp_val_max (type))
2033 return false;
2035 if (!vrp_val_is_min (ar->min))
2037 vr0->type = VR_RANGE;
2038 vr0->min = vrp_val_min (type);
2039 vr0->max
2040 = double_int_to_tree (type,
2041 double_int_sub (tree_to_double_int (ar->min),
2042 double_int_one));
2044 if (!vrp_val_is_max (ar->max))
2046 vr1->type = VR_RANGE;
2047 vr1->min
2048 = double_int_to_tree (type,
2049 double_int_add (tree_to_double_int (ar->max),
2050 double_int_one));
2051 vr1->max = vrp_val_max (type);
2053 if (vr0->type == VR_UNDEFINED)
2055 *vr0 = *vr1;
2056 vr1->type = VR_UNDEFINED;
2059 return vr0->type != VR_UNDEFINED;
2062 /* Helper to extract a value-range *VR for a multiplicative operation
2063 *VR0 CODE *VR1. */
2065 static void
2066 extract_range_from_multiplicative_op_1 (value_range_t *vr,
2067 enum tree_code code,
2068 value_range_t *vr0, value_range_t *vr1)
2070 enum value_range_type type;
2071 tree val[4];
2072 size_t i;
2073 tree min, max;
2074 bool sop;
2075 int cmp;
2077 /* Multiplications, divisions and shifts are a bit tricky to handle,
2078 depending on the mix of signs we have in the two ranges, we
2079 need to operate on different values to get the minimum and
2080 maximum values for the new range. One approach is to figure
2081 out all the variations of range combinations and do the
2082 operations.
2084 However, this involves several calls to compare_values and it
2085 is pretty convoluted. It's simpler to do the 4 operations
2086 (MIN0 OP MIN1, MIN0 OP MAX1, MAX0 OP MIN1 and MAX0 OP MAX0 OP
2087 MAX1) and then figure the smallest and largest values to form
2088 the new range. */
2089 gcc_assert (code == MULT_EXPR
2090 || code == TRUNC_DIV_EXPR
2091 || code == FLOOR_DIV_EXPR
2092 || code == CEIL_DIV_EXPR
2093 || code == EXACT_DIV_EXPR
2094 || code == ROUND_DIV_EXPR
2095 || code == RSHIFT_EXPR);
2096 gcc_assert ((vr0->type == VR_RANGE
2097 || (code == MULT_EXPR && vr0->type == VR_ANTI_RANGE))
2098 && vr0->type == vr1->type);
2100 type = vr0->type;
2102 /* Compute the 4 cross operations. */
2103 sop = false;
2104 val[0] = vrp_int_const_binop (code, vr0->min, vr1->min);
2105 if (val[0] == NULL_TREE)
2106 sop = true;
2108 if (vr1->max == vr1->min)
2109 val[1] = NULL_TREE;
2110 else
2112 val[1] = vrp_int_const_binop (code, vr0->min, vr1->max);
2113 if (val[1] == NULL_TREE)
2114 sop = true;
2117 if (vr0->max == vr0->min)
2118 val[2] = NULL_TREE;
2119 else
2121 val[2] = vrp_int_const_binop (code, vr0->max, vr1->min);
2122 if (val[2] == NULL_TREE)
2123 sop = true;
2126 if (vr0->min == vr0->max || vr1->min == vr1->max)
2127 val[3] = NULL_TREE;
2128 else
2130 val[3] = vrp_int_const_binop (code, vr0->max, vr1->max);
2131 if (val[3] == NULL_TREE)
2132 sop = true;
2135 if (sop)
2137 set_value_range_to_varying (vr);
2138 return;
2141 /* Set MIN to the minimum of VAL[i] and MAX to the maximum
2142 of VAL[i]. */
2143 min = val[0];
2144 max = val[0];
2145 for (i = 1; i < 4; i++)
2147 if (!is_gimple_min_invariant (min)
2148 || (TREE_OVERFLOW (min) && !is_overflow_infinity (min))
2149 || !is_gimple_min_invariant (max)
2150 || (TREE_OVERFLOW (max) && !is_overflow_infinity (max)))
2151 break;
2153 if (val[i])
2155 if (!is_gimple_min_invariant (val[i])
2156 || (TREE_OVERFLOW (val[i])
2157 && !is_overflow_infinity (val[i])))
2159 /* If we found an overflowed value, set MIN and MAX
2160 to it so that we set the resulting range to
2161 VARYING. */
2162 min = max = val[i];
2163 break;
2166 if (compare_values (val[i], min) == -1)
2167 min = val[i];
2169 if (compare_values (val[i], max) == 1)
2170 max = val[i];
2174 /* If either MIN or MAX overflowed, then set the resulting range to
2175 VARYING. But we do accept an overflow infinity
2176 representation. */
2177 if (min == NULL_TREE
2178 || !is_gimple_min_invariant (min)
2179 || (TREE_OVERFLOW (min) && !is_overflow_infinity (min))
2180 || max == NULL_TREE
2181 || !is_gimple_min_invariant (max)
2182 || (TREE_OVERFLOW (max) && !is_overflow_infinity (max)))
2184 set_value_range_to_varying (vr);
2185 return;
2188 /* We punt if:
2189 1) [-INF, +INF]
2190 2) [-INF, +-INF(OVF)]
2191 3) [+-INF(OVF), +INF]
2192 4) [+-INF(OVF), +-INF(OVF)]
2193 We learn nothing when we have INF and INF(OVF) on both sides.
2194 Note that we do accept [-INF, -INF] and [+INF, +INF] without
2195 overflow. */
2196 if ((vrp_val_is_min (min) || is_overflow_infinity (min))
2197 && (vrp_val_is_max (max) || is_overflow_infinity (max)))
2199 set_value_range_to_varying (vr);
2200 return;
2203 cmp = compare_values (min, max);
2204 if (cmp == -2 || cmp == 1)
2206 /* If the new range has its limits swapped around (MIN > MAX),
2207 then the operation caused one of them to wrap around, mark
2208 the new range VARYING. */
2209 set_value_range_to_varying (vr);
2211 else
2212 set_value_range (vr, type, min, max, NULL);
2215 /* Extract range information from a binary operation CODE based on
2216 the ranges of each of its operands, *VR0 and *VR1 with resulting
2217 type EXPR_TYPE. The resulting range is stored in *VR. */
2219 static void
2220 extract_range_from_binary_expr_1 (value_range_t *vr,
2221 enum tree_code code, tree expr_type,
2222 value_range_t *vr0_, value_range_t *vr1_)
2224 value_range_t vr0 = *vr0_, vr1 = *vr1_;
2225 value_range_t vrtem0 = VR_INITIALIZER, vrtem1 = VR_INITIALIZER;
2226 enum value_range_type type;
2227 tree min = NULL_TREE, max = NULL_TREE;
2228 int cmp;
2230 if (!INTEGRAL_TYPE_P (expr_type)
2231 && !POINTER_TYPE_P (expr_type))
2233 set_value_range_to_varying (vr);
2234 return;
2237 /* Not all binary expressions can be applied to ranges in a
2238 meaningful way. Handle only arithmetic operations. */
2239 if (code != PLUS_EXPR
2240 && code != MINUS_EXPR
2241 && code != POINTER_PLUS_EXPR
2242 && code != MULT_EXPR
2243 && code != TRUNC_DIV_EXPR
2244 && code != FLOOR_DIV_EXPR
2245 && code != CEIL_DIV_EXPR
2246 && code != EXACT_DIV_EXPR
2247 && code != ROUND_DIV_EXPR
2248 && code != TRUNC_MOD_EXPR
2249 && code != RSHIFT_EXPR
2250 && code != LSHIFT_EXPR
2251 && code != MIN_EXPR
2252 && code != MAX_EXPR
2253 && code != BIT_AND_EXPR
2254 && code != BIT_IOR_EXPR
2255 && code != BIT_XOR_EXPR)
2257 set_value_range_to_varying (vr);
2258 return;
2261 /* If both ranges are UNDEFINED, so is the result. */
2262 if (vr0.type == VR_UNDEFINED && vr1.type == VR_UNDEFINED)
2264 set_value_range_to_undefined (vr);
2265 return;
2267 /* If one of the ranges is UNDEFINED drop it to VARYING for the following
2268 code. At some point we may want to special-case operations that
2269 have UNDEFINED result for all or some value-ranges of the not UNDEFINED
2270 operand. */
2271 else if (vr0.type == VR_UNDEFINED)
2272 set_value_range_to_varying (&vr0);
2273 else if (vr1.type == VR_UNDEFINED)
2274 set_value_range_to_varying (&vr1);
2276 /* Now canonicalize anti-ranges to ranges when they are not symbolic
2277 and express ~[] op X as ([]' op X) U ([]'' op X). */
2278 if (vr0.type == VR_ANTI_RANGE
2279 && ranges_from_anti_range (&vr0, &vrtem0, &vrtem1))
2281 extract_range_from_binary_expr_1 (vr, code, expr_type, &vrtem0, vr1_);
2282 if (vrtem1.type != VR_UNDEFINED)
2284 value_range_t vrres = VR_INITIALIZER;
2285 extract_range_from_binary_expr_1 (&vrres, code, expr_type,
2286 &vrtem1, vr1_);
2287 vrp_meet (vr, &vrres);
2289 return;
2291 /* Likewise for X op ~[]. */
2292 if (vr1.type == VR_ANTI_RANGE
2293 && ranges_from_anti_range (&vr1, &vrtem0, &vrtem1))
2295 extract_range_from_binary_expr_1 (vr, code, expr_type, vr0_, &vrtem0);
2296 if (vrtem1.type != VR_UNDEFINED)
2298 value_range_t vrres = VR_INITIALIZER;
2299 extract_range_from_binary_expr_1 (&vrres, code, expr_type,
2300 vr0_, &vrtem1);
2301 vrp_meet (vr, &vrres);
2303 return;
2306 /* The type of the resulting value range defaults to VR0.TYPE. */
2307 type = vr0.type;
2309 /* Refuse to operate on VARYING ranges, ranges of different kinds
2310 and symbolic ranges. As an exception, we allow BIT_AND_EXPR
2311 because we may be able to derive a useful range even if one of
2312 the operands is VR_VARYING or symbolic range. Similarly for
2313 divisions. TODO, we may be able to derive anti-ranges in
2314 some cases. */
2315 if (code != BIT_AND_EXPR
2316 && code != BIT_IOR_EXPR
2317 && code != TRUNC_DIV_EXPR
2318 && code != FLOOR_DIV_EXPR
2319 && code != CEIL_DIV_EXPR
2320 && code != EXACT_DIV_EXPR
2321 && code != ROUND_DIV_EXPR
2322 && code != TRUNC_MOD_EXPR
2323 && (vr0.type == VR_VARYING
2324 || vr1.type == VR_VARYING
2325 || vr0.type != vr1.type
2326 || symbolic_range_p (&vr0)
2327 || symbolic_range_p (&vr1)))
2329 set_value_range_to_varying (vr);
2330 return;
2333 /* Now evaluate the expression to determine the new range. */
2334 if (POINTER_TYPE_P (expr_type))
2336 if (code == MIN_EXPR || code == MAX_EXPR)
2338 /* For MIN/MAX expressions with pointers, we only care about
2339 nullness, if both are non null, then the result is nonnull.
2340 If both are null, then the result is null. Otherwise they
2341 are varying. */
2342 if (range_is_nonnull (&vr0) && range_is_nonnull (&vr1))
2343 set_value_range_to_nonnull (vr, expr_type);
2344 else if (range_is_null (&vr0) && range_is_null (&vr1))
2345 set_value_range_to_null (vr, expr_type);
2346 else
2347 set_value_range_to_varying (vr);
2349 else if (code == POINTER_PLUS_EXPR)
2351 /* For pointer types, we are really only interested in asserting
2352 whether the expression evaluates to non-NULL. */
2353 if (range_is_nonnull (&vr0) || range_is_nonnull (&vr1))
2354 set_value_range_to_nonnull (vr, expr_type);
2355 else if (range_is_null (&vr0) && range_is_null (&vr1))
2356 set_value_range_to_null (vr, expr_type);
2357 else
2358 set_value_range_to_varying (vr);
2360 else if (code == BIT_AND_EXPR)
2362 /* For pointer types, we are really only interested in asserting
2363 whether the expression evaluates to non-NULL. */
2364 if (range_is_nonnull (&vr0) && range_is_nonnull (&vr1))
2365 set_value_range_to_nonnull (vr, expr_type);
2366 else if (range_is_null (&vr0) || range_is_null (&vr1))
2367 set_value_range_to_null (vr, expr_type);
2368 else
2369 set_value_range_to_varying (vr);
2371 else
2372 set_value_range_to_varying (vr);
2374 return;
2377 /* For integer ranges, apply the operation to each end of the
2378 range and see what we end up with. */
2379 if (code == PLUS_EXPR)
2381 /* If we have a PLUS_EXPR with two VR_RANGE integer constant
2382 ranges compute the precise range for such case if possible. */
2383 if (range_int_cst_p (&vr0)
2384 && range_int_cst_p (&vr1)
2385 /* We attempt to do infinite precision signed integer arithmetic,
2386 thus we need two more bits than the possibly unsigned inputs. */
2387 && TYPE_PRECISION (expr_type) < HOST_BITS_PER_DOUBLE_INT - 1)
2389 double_int min0 = tree_to_double_int (vr0.min);
2390 double_int max0 = tree_to_double_int (vr0.max);
2391 double_int min1 = tree_to_double_int (vr1.min);
2392 double_int max1 = tree_to_double_int (vr1.max);
2393 bool uns = TYPE_UNSIGNED (expr_type);
2394 double_int type_min
2395 = double_int_min_value (TYPE_PRECISION (expr_type), uns);
2396 double_int type_max
2397 = double_int_max_value (TYPE_PRECISION (expr_type), uns);
2398 double_int dmin, dmax;
2400 dmin = double_int_add (min0, min1);
2401 dmax = double_int_add (max0, max1);
2403 if (TYPE_OVERFLOW_WRAPS (expr_type))
2405 /* If overflow wraps, truncate the values and adjust the
2406 range kind and bounds appropriately. */
2407 double_int tmin
2408 = double_int_ext (dmin, TYPE_PRECISION (expr_type), uns);
2409 double_int tmax
2410 = double_int_ext (dmax, TYPE_PRECISION (expr_type), uns);
2411 gcc_assert (double_int_scmp (dmin, dmax) <= 0);
2412 if ((double_int_scmp (dmin, type_min) == -1
2413 && double_int_scmp (dmax, type_min) == -1)
2414 || (double_int_scmp (dmin, type_max) == 1
2415 && double_int_scmp (dmax, type_max) == 1)
2416 || (double_int_scmp (type_min, dmin) <= 0
2417 && double_int_scmp (dmax, type_max) <= 0))
2419 /* No overflow or both overflow or underflow. The
2420 range kind stays VR_RANGE. */
2421 min = double_int_to_tree (expr_type, tmin);
2422 max = double_int_to_tree (expr_type, tmax);
2424 else if (double_int_scmp (dmin, type_min) == -1
2425 && double_int_scmp (dmax, type_max) == 1)
2427 /* Underflow and overflow, drop to VR_VARYING. */
2428 set_value_range_to_varying (vr);
2429 return;
2431 else
2433 /* Min underflow or max overflow. The range kind
2434 changes to VR_ANTI_RANGE. */
2435 double_int tem = tmin;
2436 gcc_assert ((double_int_scmp (dmin, type_min) == -1
2437 && double_int_scmp (dmax, type_min) >= 0
2438 && double_int_scmp (dmax, type_max) <= 0)
2439 || (double_int_scmp (dmax, type_max) == 1
2440 && double_int_scmp (dmin, type_min) >= 0
2441 && double_int_scmp (dmin, type_max) <= 0));
2442 type = VR_ANTI_RANGE;
2443 tmin = double_int_add (tmax, double_int_one);
2444 tmax = double_int_add (tem, double_int_minus_one);
2445 /* If the anti-range would cover nothing, drop to varying.
2446 Likewise if the anti-range bounds are outside of the
2447 types values. */
2448 if (double_int_cmp (tmin, tmax, uns) > 0
2449 || double_int_cmp (tmin, type_min, uns) < 0
2450 || double_int_cmp (tmax, type_max, uns) > 0)
2452 set_value_range_to_varying (vr);
2453 return;
2455 min = double_int_to_tree (expr_type, tmin);
2456 max = double_int_to_tree (expr_type, tmax);
2459 else
2461 /* For non-wrapping arithmetic look at possibly smaller
2462 value-ranges of the type. */
2463 if (vrp_val_min (expr_type))
2464 type_min = tree_to_double_int (vrp_val_min (expr_type));
2465 if (vrp_val_max (expr_type))
2466 type_max = tree_to_double_int (vrp_val_max (expr_type));
2468 /* If overflow does not wrap, saturate to the types min/max
2469 value. */
2470 if (double_int_scmp (dmin, type_min) == -1)
2472 if (needs_overflow_infinity (expr_type)
2473 && supports_overflow_infinity (expr_type))
2474 min = negative_overflow_infinity (expr_type);
2475 else
2476 min = double_int_to_tree (expr_type, type_min);
2478 else if (double_int_scmp (dmin, type_max) == 1)
2480 if (needs_overflow_infinity (expr_type)
2481 && supports_overflow_infinity (expr_type))
2482 min = positive_overflow_infinity (expr_type);
2483 else
2484 min = double_int_to_tree (expr_type, type_max);
2486 else
2487 min = double_int_to_tree (expr_type, dmin);
2489 if (double_int_scmp (dmax, type_min) == -1)
2491 if (needs_overflow_infinity (expr_type)
2492 && supports_overflow_infinity (expr_type))
2493 max = negative_overflow_infinity (expr_type);
2494 else
2495 max = double_int_to_tree (expr_type, type_min);
2497 else if (double_int_scmp (dmax, type_max) == 1)
2499 if (needs_overflow_infinity (expr_type)
2500 && supports_overflow_infinity (expr_type))
2501 max = positive_overflow_infinity (expr_type);
2502 else
2503 max = double_int_to_tree (expr_type, type_max);
2505 else
2506 max = double_int_to_tree (expr_type, dmax);
2508 if (needs_overflow_infinity (expr_type)
2509 && supports_overflow_infinity (expr_type))
2511 if (is_negative_overflow_infinity (vr0.min)
2512 || is_negative_overflow_infinity (vr1.min))
2513 min = negative_overflow_infinity (expr_type);
2514 if (is_positive_overflow_infinity (vr0.max)
2515 || is_positive_overflow_infinity (vr1.max))
2516 max = positive_overflow_infinity (expr_type);
2519 else
2521 /* For other cases, for example if we have a PLUS_EXPR with two
2522 VR_ANTI_RANGEs, drop to VR_VARYING. It would take more effort
2523 to compute a precise range for such a case.
2524 ??? General even mixed range kind operations can be expressed
2525 by for example transforming ~[3, 5] + [1, 2] to range-only
2526 operations and a union primitive:
2527 [-INF, 2] + [1, 2] U [5, +INF] + [1, 2]
2528 [-INF+1, 4] U [6, +INF(OVF)]
2529 though usually the union is not exactly representable with
2530 a single range or anti-range as the above is
2531 [-INF+1, +INF(OVF)] intersected with ~[5, 5]
2532 but one could use a scheme similar to equivalences for this. */
2533 set_value_range_to_varying (vr);
2534 return;
2537 else if (code == MIN_EXPR
2538 || code == MAX_EXPR)
2540 if (vr0.type == VR_ANTI_RANGE)
2542 /* For MIN_EXPR and MAX_EXPR with two VR_ANTI_RANGEs,
2543 the resulting VR_ANTI_RANGE is the same - intersection
2544 of the two ranges. */
2545 min = vrp_int_const_binop (MAX_EXPR, vr0.min, vr1.min);
2546 max = vrp_int_const_binop (MIN_EXPR, vr0.max, vr1.max);
2548 else
2550 /* For operations that make the resulting range directly
2551 proportional to the original ranges, apply the operation to
2552 the same end of each range. */
2553 min = vrp_int_const_binop (code, vr0.min, vr1.min);
2554 max = vrp_int_const_binop (code, vr0.max, vr1.max);
2557 else if (code == MULT_EXPR)
2559 /* If we have an unsigned MULT_EXPR with two VR_ANTI_RANGEs,
2560 drop to VR_VARYING. It would take more effort to compute a
2561 precise range for such a case. For example, if we have
2562 op0 == 65536 and op1 == 65536 with their ranges both being
2563 ~[0,0] on a 32-bit machine, we would have op0 * op1 == 0, so
2564 we cannot claim that the product is in ~[0,0]. Note that we
2565 are guaranteed to have vr0.type == vr1.type at this
2566 point. */
2567 if (vr0.type == VR_ANTI_RANGE
2568 && !TYPE_OVERFLOW_UNDEFINED (expr_type))
2570 set_value_range_to_varying (vr);
2571 return;
2574 extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1);
2575 return;
2577 else if (code == RSHIFT_EXPR)
2579 /* If we have a RSHIFT_EXPR with any shift values outside [0..prec-1],
2580 then drop to VR_VARYING. Outside of this range we get undefined
2581 behavior from the shift operation. We cannot even trust
2582 SHIFT_COUNT_TRUNCATED at this stage, because that applies to rtl
2583 shifts, and the operation at the tree level may be widened. */
2584 if (vr1.type != VR_RANGE
2585 || !value_range_nonnegative_p (&vr1)
2586 || TREE_CODE (vr1.max) != INTEGER_CST
2587 || compare_tree_int (vr1.max, TYPE_PRECISION (expr_type) - 1) == 1)
2589 set_value_range_to_varying (vr);
2590 return;
2593 extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1);
2594 return;
2596 else if (code == LSHIFT_EXPR)
2598 /* If we have a LSHIFT_EXPR with any shift values outside [0..prec-1],
2599 then drop to VR_VARYING. Outside of this range we get undefined
2600 behavior from the shift operation. We cannot even trust
2601 SHIFT_COUNT_TRUNCATED at this stage, because that applies to rtl
2602 shifts, and the operation at the tree level may be widened. */
2603 if (vr1.type != VR_RANGE
2604 || !value_range_nonnegative_p (&vr1)
2605 || TREE_CODE (vr1.max) != INTEGER_CST
2606 || compare_tree_int (vr1.max, TYPE_PRECISION (expr_type) - 1) == 1)
2608 set_value_range_to_varying (vr);
2609 return;
2612 /* We can map shifts by constants to MULT_EXPR handling. */
2613 if (range_int_cst_singleton_p (&vr1))
2615 value_range_t vr1p = VR_INITIALIZER;
2616 vr1p.type = VR_RANGE;
2617 vr1p.min
2618 = double_int_to_tree (expr_type,
2619 double_int_lshift (double_int_one,
2620 TREE_INT_CST_LOW (vr1.min),
2621 TYPE_PRECISION (expr_type),
2622 false));
2623 vr1p.max = vr1p.min;
2624 extract_range_from_multiplicative_op_1 (vr, MULT_EXPR, &vr0, &vr1p);
2625 return;
2628 set_value_range_to_varying (vr);
2629 return;
2631 else if (code == TRUNC_DIV_EXPR
2632 || code == FLOOR_DIV_EXPR
2633 || code == CEIL_DIV_EXPR
2634 || code == EXACT_DIV_EXPR
2635 || code == ROUND_DIV_EXPR)
2637 if (vr0.type != VR_RANGE || symbolic_range_p (&vr0))
2639 /* For division, if op1 has VR_RANGE but op0 does not, something
2640 can be deduced just from that range. Say [min, max] / [4, max]
2641 gives [min / 4, max / 4] range. */
2642 if (vr1.type == VR_RANGE
2643 && !symbolic_range_p (&vr1)
2644 && !range_includes_zero_p (&vr1))
2646 vr0.type = type = VR_RANGE;
2647 vr0.min = vrp_val_min (expr_type);
2648 vr0.max = vrp_val_max (expr_type);
2650 else
2652 set_value_range_to_varying (vr);
2653 return;
2657 /* For divisions, if flag_non_call_exceptions is true, we must
2658 not eliminate a division by zero. */
2659 if (cfun->can_throw_non_call_exceptions
2660 && (vr1.type != VR_RANGE
2661 || symbolic_range_p (&vr1)
2662 || range_includes_zero_p (&vr1)))
2664 set_value_range_to_varying (vr);
2665 return;
2668 /* For divisions, if op0 is VR_RANGE, we can deduce a range
2669 even if op1 is VR_VARYING, VR_ANTI_RANGE, symbolic or can
2670 include 0. */
2671 if (vr0.type == VR_RANGE
2672 && (vr1.type != VR_RANGE
2673 || symbolic_range_p (&vr1)
2674 || range_includes_zero_p (&vr1)))
2676 tree zero = build_int_cst (TREE_TYPE (vr0.min), 0);
2677 int cmp;
2679 min = NULL_TREE;
2680 max = NULL_TREE;
2681 if (TYPE_UNSIGNED (expr_type)
2682 || value_range_nonnegative_p (&vr1))
2684 /* For unsigned division or when divisor is known
2685 to be non-negative, the range has to cover
2686 all numbers from 0 to max for positive max
2687 and all numbers from min to 0 for negative min. */
2688 cmp = compare_values (vr0.max, zero);
2689 if (cmp == -1)
2690 max = zero;
2691 else if (cmp == 0 || cmp == 1)
2692 max = vr0.max;
2693 else
2694 type = VR_VARYING;
2695 cmp = compare_values (vr0.min, zero);
2696 if (cmp == 1)
2697 min = zero;
2698 else if (cmp == 0 || cmp == -1)
2699 min = vr0.min;
2700 else
2701 type = VR_VARYING;
2703 else
2705 /* Otherwise the range is -max .. max or min .. -min
2706 depending on which bound is bigger in absolute value,
2707 as the division can change the sign. */
2708 abs_extent_range (vr, vr0.min, vr0.max);
2709 return;
2711 if (type == VR_VARYING)
2713 set_value_range_to_varying (vr);
2714 return;
2717 else
2719 extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1);
2720 return;
2723 else if (code == TRUNC_MOD_EXPR)
2725 if (vr1.type != VR_RANGE
2726 || symbolic_range_p (&vr1)
2727 || range_includes_zero_p (&vr1)
2728 || vrp_val_is_min (vr1.min))
2730 set_value_range_to_varying (vr);
2731 return;
2733 type = VR_RANGE;
2734 /* Compute MAX <|vr1.min|, |vr1.max|> - 1. */
2735 max = fold_unary_to_constant (ABS_EXPR, expr_type, vr1.min);
2736 if (tree_int_cst_lt (max, vr1.max))
2737 max = vr1.max;
2738 max = int_const_binop (MINUS_EXPR, max, integer_one_node);
2739 /* If the dividend is non-negative the modulus will be
2740 non-negative as well. */
2741 if (TYPE_UNSIGNED (expr_type)
2742 || value_range_nonnegative_p (&vr0))
2743 min = build_int_cst (TREE_TYPE (max), 0);
2744 else
2745 min = fold_unary_to_constant (NEGATE_EXPR, expr_type, max);
2747 else if (code == MINUS_EXPR)
2749 /* If we have a MINUS_EXPR with two VR_ANTI_RANGEs, drop to
2750 VR_VARYING. It would take more effort to compute a precise
2751 range for such a case. For example, if we have op0 == 1 and
2752 op1 == 1 with their ranges both being ~[0,0], we would have
2753 op0 - op1 == 0, so we cannot claim that the difference is in
2754 ~[0,0]. Note that we are guaranteed to have
2755 vr0.type == vr1.type at this point. */
2756 if (vr0.type == VR_ANTI_RANGE)
2758 set_value_range_to_varying (vr);
2759 return;
2762 /* For MINUS_EXPR, apply the operation to the opposite ends of
2763 each range. */
2764 min = vrp_int_const_binop (code, vr0.min, vr1.max);
2765 max = vrp_int_const_binop (code, vr0.max, vr1.min);
2767 else if (code == BIT_AND_EXPR || code == BIT_IOR_EXPR || code == BIT_XOR_EXPR)
2769 bool int_cst_range0, int_cst_range1;
2770 double_int may_be_nonzero0, may_be_nonzero1;
2771 double_int must_be_nonzero0, must_be_nonzero1;
2773 int_cst_range0 = zero_nonzero_bits_from_vr (&vr0, &may_be_nonzero0,
2774 &must_be_nonzero0);
2775 int_cst_range1 = zero_nonzero_bits_from_vr (&vr1, &may_be_nonzero1,
2776 &must_be_nonzero1);
2778 type = VR_RANGE;
2779 if (code == BIT_AND_EXPR)
2781 double_int dmax;
2782 min = double_int_to_tree (expr_type,
2783 double_int_and (must_be_nonzero0,
2784 must_be_nonzero1));
2785 dmax = double_int_and (may_be_nonzero0, may_be_nonzero1);
2786 /* If both input ranges contain only negative values we can
2787 truncate the result range maximum to the minimum of the
2788 input range maxima. */
2789 if (int_cst_range0 && int_cst_range1
2790 && tree_int_cst_sgn (vr0.max) < 0
2791 && tree_int_cst_sgn (vr1.max) < 0)
2793 dmax = double_int_min (dmax, tree_to_double_int (vr0.max),
2794 TYPE_UNSIGNED (expr_type));
2795 dmax = double_int_min (dmax, tree_to_double_int (vr1.max),
2796 TYPE_UNSIGNED (expr_type));
2798 /* If either input range contains only non-negative values
2799 we can truncate the result range maximum to the respective
2800 maximum of the input range. */
2801 if (int_cst_range0 && tree_int_cst_sgn (vr0.min) >= 0)
2802 dmax = double_int_min (dmax, tree_to_double_int (vr0.max),
2803 TYPE_UNSIGNED (expr_type));
2804 if (int_cst_range1 && tree_int_cst_sgn (vr1.min) >= 0)
2805 dmax = double_int_min (dmax, tree_to_double_int (vr1.max),
2806 TYPE_UNSIGNED (expr_type));
2807 max = double_int_to_tree (expr_type, dmax);
2809 else if (code == BIT_IOR_EXPR)
2811 double_int dmin;
2812 max = double_int_to_tree (expr_type,
2813 double_int_ior (may_be_nonzero0,
2814 may_be_nonzero1));
2815 dmin = double_int_ior (must_be_nonzero0, must_be_nonzero1);
2816 /* If the input ranges contain only positive values we can
2817 truncate the minimum of the result range to the maximum
2818 of the input range minima. */
2819 if (int_cst_range0 && int_cst_range1
2820 && tree_int_cst_sgn (vr0.min) >= 0
2821 && tree_int_cst_sgn (vr1.min) >= 0)
2823 dmin = double_int_max (dmin, tree_to_double_int (vr0.min),
2824 TYPE_UNSIGNED (expr_type));
2825 dmin = double_int_max (dmin, tree_to_double_int (vr1.min),
2826 TYPE_UNSIGNED (expr_type));
2828 /* If either input range contains only negative values
2829 we can truncate the minimum of the result range to the
2830 respective minimum range. */
2831 if (int_cst_range0 && tree_int_cst_sgn (vr0.max) < 0)
2832 dmin = double_int_max (dmin, tree_to_double_int (vr0.min),
2833 TYPE_UNSIGNED (expr_type));
2834 if (int_cst_range1 && tree_int_cst_sgn (vr1.max) < 0)
2835 dmin = double_int_max (dmin, tree_to_double_int (vr1.min),
2836 TYPE_UNSIGNED (expr_type));
2837 min = double_int_to_tree (expr_type, dmin);
2839 else if (code == BIT_XOR_EXPR)
2841 double_int result_zero_bits, result_one_bits;
2842 result_zero_bits
2843 = double_int_ior (double_int_and (must_be_nonzero0,
2844 must_be_nonzero1),
2845 double_int_not
2846 (double_int_ior (may_be_nonzero0,
2847 may_be_nonzero1)));
2848 result_one_bits
2849 = double_int_ior (double_int_and
2850 (must_be_nonzero0,
2851 double_int_not (may_be_nonzero1)),
2852 double_int_and
2853 (must_be_nonzero1,
2854 double_int_not (may_be_nonzero0)));
2855 max = double_int_to_tree (expr_type,
2856 double_int_not (result_zero_bits));
2857 min = double_int_to_tree (expr_type, result_one_bits);
2858 /* If the range has all positive or all negative values the
2859 result is better than VARYING. */
2860 if (tree_int_cst_sgn (min) < 0
2861 || tree_int_cst_sgn (max) >= 0)
2863 else
2864 max = min = NULL_TREE;
2867 else
2868 gcc_unreachable ();
2870 /* If either MIN or MAX overflowed, then set the resulting range to
2871 VARYING. But we do accept an overflow infinity
2872 representation. */
2873 if (min == NULL_TREE
2874 || !is_gimple_min_invariant (min)
2875 || (TREE_OVERFLOW (min) && !is_overflow_infinity (min))
2876 || max == NULL_TREE
2877 || !is_gimple_min_invariant (max)
2878 || (TREE_OVERFLOW (max) && !is_overflow_infinity (max)))
2880 set_value_range_to_varying (vr);
2881 return;
2884 /* We punt if:
2885 1) [-INF, +INF]
2886 2) [-INF, +-INF(OVF)]
2887 3) [+-INF(OVF), +INF]
2888 4) [+-INF(OVF), +-INF(OVF)]
2889 We learn nothing when we have INF and INF(OVF) on both sides.
2890 Note that we do accept [-INF, -INF] and [+INF, +INF] without
2891 overflow. */
2892 if ((vrp_val_is_min (min) || is_overflow_infinity (min))
2893 && (vrp_val_is_max (max) || is_overflow_infinity (max)))
2895 set_value_range_to_varying (vr);
2896 return;
2899 cmp = compare_values (min, max);
2900 if (cmp == -2 || cmp == 1)
2902 /* If the new range has its limits swapped around (MIN > MAX),
2903 then the operation caused one of them to wrap around, mark
2904 the new range VARYING. */
2905 set_value_range_to_varying (vr);
2907 else
2908 set_value_range (vr, type, min, max, NULL);
2911 /* Extract range information from a binary expression OP0 CODE OP1 based on
2912 the ranges of each of its operands with resulting type EXPR_TYPE.
2913 The resulting range is stored in *VR. */
2915 static void
2916 extract_range_from_binary_expr (value_range_t *vr,
2917 enum tree_code code,
2918 tree expr_type, tree op0, tree op1)
2920 value_range_t vr0 = VR_INITIALIZER;
2921 value_range_t vr1 = VR_INITIALIZER;
2923 /* Get value ranges for each operand. For constant operands, create
2924 a new value range with the operand to simplify processing. */
2925 if (TREE_CODE (op0) == SSA_NAME)
2926 vr0 = *(get_value_range (op0));
2927 else if (is_gimple_min_invariant (op0))
2928 set_value_range_to_value (&vr0, op0, NULL);
2929 else
2930 set_value_range_to_varying (&vr0);
2932 if (TREE_CODE (op1) == SSA_NAME)
2933 vr1 = *(get_value_range (op1));
2934 else if (is_gimple_min_invariant (op1))
2935 set_value_range_to_value (&vr1, op1, NULL);
2936 else
2937 set_value_range_to_varying (&vr1);
2939 extract_range_from_binary_expr_1 (vr, code, expr_type, &vr0, &vr1);
2942 /* Extract range information from a unary operation CODE based on
2943 the range of its operand *VR0 with type OP0_TYPE with resulting type TYPE.
2944 The The resulting range is stored in *VR. */
2946 static void
2947 extract_range_from_unary_expr_1 (value_range_t *vr,
2948 enum tree_code code, tree type,
2949 value_range_t *vr0_, tree op0_type)
2951 value_range_t vr0 = *vr0_, vrtem0 = VR_INITIALIZER, vrtem1 = VR_INITIALIZER;
2953 /* VRP only operates on integral and pointer types. */
2954 if (!(INTEGRAL_TYPE_P (op0_type)
2955 || POINTER_TYPE_P (op0_type))
2956 || !(INTEGRAL_TYPE_P (type)
2957 || POINTER_TYPE_P (type)))
2959 set_value_range_to_varying (vr);
2960 return;
2963 /* If VR0 is UNDEFINED, so is the result. */
2964 if (vr0.type == VR_UNDEFINED)
2966 set_value_range_to_undefined (vr);
2967 return;
2970 /* Handle operations that we express in terms of others. */
2971 if (code == PAREN_EXPR)
2973 /* PAREN_EXPR is a simple copy. */
2974 copy_value_range (vr, &vr0);
2975 return;
2977 else if (code == NEGATE_EXPR)
2979 /* -X is simply 0 - X, so re-use existing code that also handles
2980 anti-ranges fine. */
2981 value_range_t zero = VR_INITIALIZER;
2982 set_value_range_to_value (&zero, build_int_cst (type, 0), NULL);
2983 extract_range_from_binary_expr_1 (vr, MINUS_EXPR, type, &zero, &vr0);
2984 return;
2986 else if (code == BIT_NOT_EXPR)
2988 /* ~X is simply -1 - X, so re-use existing code that also handles
2989 anti-ranges fine. */
2990 value_range_t minusone = VR_INITIALIZER;
2991 set_value_range_to_value (&minusone, build_int_cst (type, -1), NULL);
2992 extract_range_from_binary_expr_1 (vr, MINUS_EXPR,
2993 type, &minusone, &vr0);
2994 return;
2997 /* Now canonicalize anti-ranges to ranges when they are not symbolic
2998 and express op ~[] as (op []') U (op []''). */
2999 if (vr0.type == VR_ANTI_RANGE
3000 && ranges_from_anti_range (&vr0, &vrtem0, &vrtem1))
3002 extract_range_from_unary_expr_1 (vr, code, type, &vrtem0, op0_type);
3003 if (vrtem1.type != VR_UNDEFINED)
3005 value_range_t vrres = VR_INITIALIZER;
3006 extract_range_from_unary_expr_1 (&vrres, code, type,
3007 &vrtem1, op0_type);
3008 vrp_meet (vr, &vrres);
3010 return;
3013 if (CONVERT_EXPR_CODE_P (code))
3015 tree inner_type = op0_type;
3016 tree outer_type = type;
3018 /* If the expression evaluates to a pointer, we are only interested in
3019 determining if it evaluates to NULL [0, 0] or non-NULL (~[0, 0]). */
3020 if (POINTER_TYPE_P (type))
3022 if (range_is_nonnull (&vr0))
3023 set_value_range_to_nonnull (vr, type);
3024 else if (range_is_null (&vr0))
3025 set_value_range_to_null (vr, type);
3026 else
3027 set_value_range_to_varying (vr);
3028 return;
3031 /* If VR0 is varying and we increase the type precision, assume
3032 a full range for the following transformation. */
3033 if (vr0.type == VR_VARYING
3034 && INTEGRAL_TYPE_P (inner_type)
3035 && TYPE_PRECISION (inner_type) < TYPE_PRECISION (outer_type))
3037 vr0.type = VR_RANGE;
3038 vr0.min = TYPE_MIN_VALUE (inner_type);
3039 vr0.max = TYPE_MAX_VALUE (inner_type);
3042 /* If VR0 is a constant range or anti-range and the conversion is
3043 not truncating we can convert the min and max values and
3044 canonicalize the resulting range. Otherwise we can do the
3045 conversion if the size of the range is less than what the
3046 precision of the target type can represent and the range is
3047 not an anti-range. */
3048 if ((vr0.type == VR_RANGE
3049 || vr0.type == VR_ANTI_RANGE)
3050 && TREE_CODE (vr0.min) == INTEGER_CST
3051 && TREE_CODE (vr0.max) == INTEGER_CST
3052 && (!is_overflow_infinity (vr0.min)
3053 || (vr0.type == VR_RANGE
3054 && TYPE_PRECISION (outer_type) > TYPE_PRECISION (inner_type)
3055 && needs_overflow_infinity (outer_type)
3056 && supports_overflow_infinity (outer_type)))
3057 && (!is_overflow_infinity (vr0.max)
3058 || (vr0.type == VR_RANGE
3059 && TYPE_PRECISION (outer_type) > TYPE_PRECISION (inner_type)
3060 && needs_overflow_infinity (outer_type)
3061 && supports_overflow_infinity (outer_type)))
3062 && (TYPE_PRECISION (outer_type) >= TYPE_PRECISION (inner_type)
3063 || (vr0.type == VR_RANGE
3064 && integer_zerop (int_const_binop (RSHIFT_EXPR,
3065 int_const_binop (MINUS_EXPR, vr0.max, vr0.min),
3066 size_int (TYPE_PRECISION (outer_type)))))))
3068 tree new_min, new_max;
3069 if (is_overflow_infinity (vr0.min))
3070 new_min = negative_overflow_infinity (outer_type);
3071 else
3072 new_min = force_fit_type_double (outer_type,
3073 tree_to_double_int (vr0.min),
3074 0, false);
3075 if (is_overflow_infinity (vr0.max))
3076 new_max = positive_overflow_infinity (outer_type);
3077 else
3078 new_max = force_fit_type_double (outer_type,
3079 tree_to_double_int (vr0.max),
3080 0, false);
3081 set_and_canonicalize_value_range (vr, vr0.type,
3082 new_min, new_max, NULL);
3083 return;
3086 set_value_range_to_varying (vr);
3087 return;
3089 else if (code == ABS_EXPR)
3091 tree min, max;
3092 int cmp;
3094 /* Pass through vr0 in the easy cases. */
3095 if (TYPE_UNSIGNED (type)
3096 || value_range_nonnegative_p (&vr0))
3098 copy_value_range (vr, &vr0);
3099 return;
3102 /* For the remaining varying or symbolic ranges we can't do anything
3103 useful. */
3104 if (vr0.type == VR_VARYING
3105 || symbolic_range_p (&vr0))
3107 set_value_range_to_varying (vr);
3108 return;
3111 /* -TYPE_MIN_VALUE = TYPE_MIN_VALUE with flag_wrapv so we can't get a
3112 useful range. */
3113 if (!TYPE_OVERFLOW_UNDEFINED (type)
3114 && ((vr0.type == VR_RANGE
3115 && vrp_val_is_min (vr0.min))
3116 || (vr0.type == VR_ANTI_RANGE
3117 && !vrp_val_is_min (vr0.min))))
3119 set_value_range_to_varying (vr);
3120 return;
3123 /* ABS_EXPR may flip the range around, if the original range
3124 included negative values. */
3125 if (is_overflow_infinity (vr0.min))
3126 min = positive_overflow_infinity (type);
3127 else if (!vrp_val_is_min (vr0.min))
3128 min = fold_unary_to_constant (code, type, vr0.min);
3129 else if (!needs_overflow_infinity (type))
3130 min = TYPE_MAX_VALUE (type);
3131 else if (supports_overflow_infinity (type))
3132 min = positive_overflow_infinity (type);
3133 else
3135 set_value_range_to_varying (vr);
3136 return;
3139 if (is_overflow_infinity (vr0.max))
3140 max = positive_overflow_infinity (type);
3141 else if (!vrp_val_is_min (vr0.max))
3142 max = fold_unary_to_constant (code, type, vr0.max);
3143 else if (!needs_overflow_infinity (type))
3144 max = TYPE_MAX_VALUE (type);
3145 else if (supports_overflow_infinity (type)
3146 /* We shouldn't generate [+INF, +INF] as set_value_range
3147 doesn't like this and ICEs. */
3148 && !is_positive_overflow_infinity (min))
3149 max = positive_overflow_infinity (type);
3150 else
3152 set_value_range_to_varying (vr);
3153 return;
3156 cmp = compare_values (min, max);
3158 /* If a VR_ANTI_RANGEs contains zero, then we have
3159 ~[-INF, min(MIN, MAX)]. */
3160 if (vr0.type == VR_ANTI_RANGE)
3162 if (range_includes_zero_p (&vr0))
3164 /* Take the lower of the two values. */
3165 if (cmp != 1)
3166 max = min;
3168 /* Create ~[-INF, min (abs(MIN), abs(MAX))]
3169 or ~[-INF + 1, min (abs(MIN), abs(MAX))] when
3170 flag_wrapv is set and the original anti-range doesn't include
3171 TYPE_MIN_VALUE, remember -TYPE_MIN_VALUE = TYPE_MIN_VALUE. */
3172 if (TYPE_OVERFLOW_WRAPS (type))
3174 tree type_min_value = TYPE_MIN_VALUE (type);
3176 min = (vr0.min != type_min_value
3177 ? int_const_binop (PLUS_EXPR, type_min_value,
3178 integer_one_node)
3179 : type_min_value);
3181 else
3183 if (overflow_infinity_range_p (&vr0))
3184 min = negative_overflow_infinity (type);
3185 else
3186 min = TYPE_MIN_VALUE (type);
3189 else
3191 /* All else has failed, so create the range [0, INF], even for
3192 flag_wrapv since TYPE_MIN_VALUE is in the original
3193 anti-range. */
3194 vr0.type = VR_RANGE;
3195 min = build_int_cst (type, 0);
3196 if (needs_overflow_infinity (type))
3198 if (supports_overflow_infinity (type))
3199 max = positive_overflow_infinity (type);
3200 else
3202 set_value_range_to_varying (vr);
3203 return;
3206 else
3207 max = TYPE_MAX_VALUE (type);
3211 /* If the range contains zero then we know that the minimum value in the
3212 range will be zero. */
3213 else if (range_includes_zero_p (&vr0))
3215 if (cmp == 1)
3216 max = min;
3217 min = build_int_cst (type, 0);
3219 else
3221 /* If the range was reversed, swap MIN and MAX. */
3222 if (cmp == 1)
3224 tree t = min;
3225 min = max;
3226 max = t;
3230 cmp = compare_values (min, max);
3231 if (cmp == -2 || cmp == 1)
3233 /* If the new range has its limits swapped around (MIN > MAX),
3234 then the operation caused one of them to wrap around, mark
3235 the new range VARYING. */
3236 set_value_range_to_varying (vr);
3238 else
3239 set_value_range (vr, vr0.type, min, max, NULL);
3240 return;
3243 /* For unhandled operations fall back to varying. */
3244 set_value_range_to_varying (vr);
3245 return;
3249 /* Extract range information from a unary expression CODE OP0 based on
3250 the range of its operand with resulting type TYPE.
3251 The resulting range is stored in *VR. */
3253 static void
3254 extract_range_from_unary_expr (value_range_t *vr, enum tree_code code,
3255 tree type, tree op0)
3257 value_range_t vr0 = VR_INITIALIZER;
3259 /* Get value ranges for the operand. For constant operands, create
3260 a new value range with the operand to simplify processing. */
3261 if (TREE_CODE (op0) == SSA_NAME)
3262 vr0 = *(get_value_range (op0));
3263 else if (is_gimple_min_invariant (op0))
3264 set_value_range_to_value (&vr0, op0, NULL);
3265 else
3266 set_value_range_to_varying (&vr0);
3268 extract_range_from_unary_expr_1 (vr, code, type, &vr0, TREE_TYPE (op0));
3272 /* Extract range information from a conditional expression STMT based on
3273 the ranges of each of its operands and the expression code. */
3275 static void
3276 extract_range_from_cond_expr (value_range_t *vr, gimple stmt)
3278 tree op0, op1;
3279 value_range_t vr0 = VR_INITIALIZER;
3280 value_range_t vr1 = VR_INITIALIZER;
3282 /* Get value ranges for each operand. For constant operands, create
3283 a new value range with the operand to simplify processing. */
3284 op0 = gimple_assign_rhs2 (stmt);
3285 if (TREE_CODE (op0) == SSA_NAME)
3286 vr0 = *(get_value_range (op0));
3287 else if (is_gimple_min_invariant (op0))
3288 set_value_range_to_value (&vr0, op0, NULL);
3289 else
3290 set_value_range_to_varying (&vr0);
3292 op1 = gimple_assign_rhs3 (stmt);
3293 if (TREE_CODE (op1) == SSA_NAME)
3294 vr1 = *(get_value_range (op1));
3295 else if (is_gimple_min_invariant (op1))
3296 set_value_range_to_value (&vr1, op1, NULL);
3297 else
3298 set_value_range_to_varying (&vr1);
3300 /* The resulting value range is the union of the operand ranges */
3301 copy_value_range (vr, &vr0);
3302 vrp_meet (vr, &vr1);
3306 /* Extract range information from a comparison expression EXPR based
3307 on the range of its operand and the expression code. */
3309 static void
3310 extract_range_from_comparison (value_range_t *vr, enum tree_code code,
3311 tree type, tree op0, tree op1)
3313 bool sop = false;
3314 tree val;
3316 val = vrp_evaluate_conditional_warnv_with_ops (code, op0, op1, false, &sop,
3317 NULL);
3319 /* A disadvantage of using a special infinity as an overflow
3320 representation is that we lose the ability to record overflow
3321 when we don't have an infinity. So we have to ignore a result
3322 which relies on overflow. */
3324 if (val && !is_overflow_infinity (val) && !sop)
3326 /* Since this expression was found on the RHS of an assignment,
3327 its type may be different from _Bool. Convert VAL to EXPR's
3328 type. */
3329 val = fold_convert (type, val);
3330 if (is_gimple_min_invariant (val))
3331 set_value_range_to_value (vr, val, vr->equiv);
3332 else
3333 set_value_range (vr, VR_RANGE, val, val, vr->equiv);
3335 else
3336 /* The result of a comparison is always true or false. */
3337 set_value_range_to_truthvalue (vr, type);
3340 /* Try to derive a nonnegative or nonzero range out of STMT relying
3341 primarily on generic routines in fold in conjunction with range data.
3342 Store the result in *VR */
3344 static void
3345 extract_range_basic (value_range_t *vr, gimple stmt)
3347 bool sop = false;
3348 tree type = gimple_expr_type (stmt);
3350 if (INTEGRAL_TYPE_P (type)
3351 && gimple_stmt_nonnegative_warnv_p (stmt, &sop))
3352 set_value_range_to_nonnegative (vr, type,
3353 sop || stmt_overflow_infinity (stmt));
3354 else if (vrp_stmt_computes_nonzero (stmt, &sop)
3355 && !sop)
3356 set_value_range_to_nonnull (vr, type);
3357 else
3358 set_value_range_to_varying (vr);
3362 /* Try to compute a useful range out of assignment STMT and store it
3363 in *VR. */
3365 static void
3366 extract_range_from_assignment (value_range_t *vr, gimple stmt)
3368 enum tree_code code = gimple_assign_rhs_code (stmt);
3370 if (code == ASSERT_EXPR)
3371 extract_range_from_assert (vr, gimple_assign_rhs1 (stmt));
3372 else if (code == SSA_NAME)
3373 extract_range_from_ssa_name (vr, gimple_assign_rhs1 (stmt));
3374 else if (TREE_CODE_CLASS (code) == tcc_binary)
3375 extract_range_from_binary_expr (vr, gimple_assign_rhs_code (stmt),
3376 gimple_expr_type (stmt),
3377 gimple_assign_rhs1 (stmt),
3378 gimple_assign_rhs2 (stmt));
3379 else if (TREE_CODE_CLASS (code) == tcc_unary)
3380 extract_range_from_unary_expr (vr, gimple_assign_rhs_code (stmt),
3381 gimple_expr_type (stmt),
3382 gimple_assign_rhs1 (stmt));
3383 else if (code == COND_EXPR)
3384 extract_range_from_cond_expr (vr, stmt);
3385 else if (TREE_CODE_CLASS (code) == tcc_comparison)
3386 extract_range_from_comparison (vr, gimple_assign_rhs_code (stmt),
3387 gimple_expr_type (stmt),
3388 gimple_assign_rhs1 (stmt),
3389 gimple_assign_rhs2 (stmt));
3390 else if (get_gimple_rhs_class (code) == GIMPLE_SINGLE_RHS
3391 && is_gimple_min_invariant (gimple_assign_rhs1 (stmt)))
3392 set_value_range_to_value (vr, gimple_assign_rhs1 (stmt), NULL);
3393 else
3394 set_value_range_to_varying (vr);
3396 if (vr->type == VR_VARYING)
3397 extract_range_basic (vr, stmt);
3400 /* Given a range VR, a LOOP and a variable VAR, determine whether it
3401 would be profitable to adjust VR using scalar evolution information
3402 for VAR. If so, update VR with the new limits. */
3404 static void
3405 adjust_range_with_scev (value_range_t *vr, struct loop *loop,
3406 gimple stmt, tree var)
3408 tree init, step, chrec, tmin, tmax, min, max, type, tem;
3409 enum ev_direction dir;
3411 /* TODO. Don't adjust anti-ranges. An anti-range may provide
3412 better opportunities than a regular range, but I'm not sure. */
3413 if (vr->type == VR_ANTI_RANGE)
3414 return;
3416 chrec = instantiate_parameters (loop, analyze_scalar_evolution (loop, var));
3418 /* Like in PR19590, scev can return a constant function. */
3419 if (is_gimple_min_invariant (chrec))
3421 set_value_range_to_value (vr, chrec, vr->equiv);
3422 return;
3425 if (TREE_CODE (chrec) != POLYNOMIAL_CHREC)
3426 return;
3428 init = initial_condition_in_loop_num (chrec, loop->num);
3429 tem = op_with_constant_singleton_value_range (init);
3430 if (tem)
3431 init = tem;
3432 step = evolution_part_in_loop_num (chrec, loop->num);
3433 tem = op_with_constant_singleton_value_range (step);
3434 if (tem)
3435 step = tem;
3437 /* If STEP is symbolic, we can't know whether INIT will be the
3438 minimum or maximum value in the range. Also, unless INIT is
3439 a simple expression, compare_values and possibly other functions
3440 in tree-vrp won't be able to handle it. */
3441 if (step == NULL_TREE
3442 || !is_gimple_min_invariant (step)
3443 || !valid_value_p (init))
3444 return;
3446 dir = scev_direction (chrec);
3447 if (/* Do not adjust ranges if we do not know whether the iv increases
3448 or decreases, ... */
3449 dir == EV_DIR_UNKNOWN
3450 /* ... or if it may wrap. */
3451 || scev_probably_wraps_p (init, step, stmt, get_chrec_loop (chrec),
3452 true))
3453 return;
3455 /* We use TYPE_MIN_VALUE and TYPE_MAX_VALUE here instead of
3456 negative_overflow_infinity and positive_overflow_infinity,
3457 because we have concluded that the loop probably does not
3458 wrap. */
3460 type = TREE_TYPE (var);
3461 if (POINTER_TYPE_P (type) || !TYPE_MIN_VALUE (type))
3462 tmin = lower_bound_in_type (type, type);
3463 else
3464 tmin = TYPE_MIN_VALUE (type);
3465 if (POINTER_TYPE_P (type) || !TYPE_MAX_VALUE (type))
3466 tmax = upper_bound_in_type (type, type);
3467 else
3468 tmax = TYPE_MAX_VALUE (type);
3470 /* Try to use estimated number of iterations for the loop to constrain the
3471 final value in the evolution. */
3472 if (TREE_CODE (step) == INTEGER_CST
3473 && is_gimple_val (init)
3474 && (TREE_CODE (init) != SSA_NAME
3475 || get_value_range (init)->type == VR_RANGE))
3477 double_int nit;
3479 /* We are only entering here for loop header PHI nodes, so using
3480 the number of latch executions is the correct thing to use. */
3481 if (max_loop_iterations (loop, &nit))
3483 value_range_t maxvr = VR_INITIALIZER;
3484 double_int dtmp;
3485 bool unsigned_p = TYPE_UNSIGNED (TREE_TYPE (step));
3486 int overflow = 0;
3488 dtmp = double_int_mul_with_sign (tree_to_double_int (step), nit,
3489 unsigned_p, &overflow);
3490 /* If the multiplication overflowed we can't do a meaningful
3491 adjustment. Likewise if the result doesn't fit in the type
3492 of the induction variable. For a signed type we have to
3493 check whether the result has the expected signedness which
3494 is that of the step as number of iterations is unsigned. */
3495 if (!overflow
3496 && double_int_fits_to_tree_p (TREE_TYPE (init), dtmp)
3497 && (unsigned_p
3498 || ((dtmp.high ^ TREE_INT_CST_HIGH (step)) >= 0)))
3500 tem = double_int_to_tree (TREE_TYPE (init), dtmp);
3501 extract_range_from_binary_expr (&maxvr, PLUS_EXPR,
3502 TREE_TYPE (init), init, tem);
3503 /* Likewise if the addition did. */
3504 if (maxvr.type == VR_RANGE)
3506 tmin = maxvr.min;
3507 tmax = maxvr.max;
3513 if (vr->type == VR_VARYING || vr->type == VR_UNDEFINED)
3515 min = tmin;
3516 max = tmax;
3518 /* For VARYING or UNDEFINED ranges, just about anything we get
3519 from scalar evolutions should be better. */
3521 if (dir == EV_DIR_DECREASES)
3522 max = init;
3523 else
3524 min = init;
3526 /* If we would create an invalid range, then just assume we
3527 know absolutely nothing. This may be over-conservative,
3528 but it's clearly safe, and should happen only in unreachable
3529 parts of code, or for invalid programs. */
3530 if (compare_values (min, max) == 1)
3531 return;
3533 set_value_range (vr, VR_RANGE, min, max, vr->equiv);
3535 else if (vr->type == VR_RANGE)
3537 min = vr->min;
3538 max = vr->max;
3540 if (dir == EV_DIR_DECREASES)
3542 /* INIT is the maximum value. If INIT is lower than VR->MAX
3543 but no smaller than VR->MIN, set VR->MAX to INIT. */
3544 if (compare_values (init, max) == -1)
3545 max = init;
3547 /* According to the loop information, the variable does not
3548 overflow. If we think it does, probably because of an
3549 overflow due to arithmetic on a different INF value,
3550 reset now. */
3551 if (is_negative_overflow_infinity (min)
3552 || compare_values (min, tmin) == -1)
3553 min = tmin;
3556 else
3558 /* If INIT is bigger than VR->MIN, set VR->MIN to INIT. */
3559 if (compare_values (init, min) == 1)
3560 min = init;
3562 if (is_positive_overflow_infinity (max)
3563 || compare_values (tmax, max) == -1)
3564 max = tmax;
3567 /* If we just created an invalid range with the minimum
3568 greater than the maximum, we fail conservatively.
3569 This should happen only in unreachable
3570 parts of code, or for invalid programs. */
3571 if (compare_values (min, max) == 1)
3572 return;
3574 set_value_range (vr, VR_RANGE, min, max, vr->equiv);
3578 /* Return true if VAR may overflow at STMT. This checks any available
3579 loop information to see if we can determine that VAR does not
3580 overflow. */
3582 static bool
3583 vrp_var_may_overflow (tree var, gimple stmt)
3585 struct loop *l;
3586 tree chrec, init, step;
3588 if (current_loops == NULL)
3589 return true;
3591 l = loop_containing_stmt (stmt);
3592 if (l == NULL
3593 || !loop_outer (l))
3594 return true;
3596 chrec = instantiate_parameters (l, analyze_scalar_evolution (l, var));
3597 if (TREE_CODE (chrec) != POLYNOMIAL_CHREC)
3598 return true;
3600 init = initial_condition_in_loop_num (chrec, l->num);
3601 step = evolution_part_in_loop_num (chrec, l->num);
3603 if (step == NULL_TREE
3604 || !is_gimple_min_invariant (step)
3605 || !valid_value_p (init))
3606 return true;
3608 /* If we get here, we know something useful about VAR based on the
3609 loop information. If it wraps, it may overflow. */
3611 if (scev_probably_wraps_p (init, step, stmt, get_chrec_loop (chrec),
3612 true))
3613 return true;
3615 if (dump_file && (dump_flags & TDF_DETAILS) != 0)
3617 print_generic_expr (dump_file, var, 0);
3618 fprintf (dump_file, ": loop information indicates does not overflow\n");
3621 return false;
3625 /* Given two numeric value ranges VR0, VR1 and a comparison code COMP:
3627 - Return BOOLEAN_TRUE_NODE if VR0 COMP VR1 always returns true for
3628 all the values in the ranges.
3630 - Return BOOLEAN_FALSE_NODE if the comparison always returns false.
3632 - Return NULL_TREE if it is not always possible to determine the
3633 value of the comparison.
3635 Also set *STRICT_OVERFLOW_P to indicate whether a range with an
3636 overflow infinity was used in the test. */
3639 static tree
3640 compare_ranges (enum tree_code comp, value_range_t *vr0, value_range_t *vr1,
3641 bool *strict_overflow_p)
3643 /* VARYING or UNDEFINED ranges cannot be compared. */
3644 if (vr0->type == VR_VARYING
3645 || vr0->type == VR_UNDEFINED
3646 || vr1->type == VR_VARYING
3647 || vr1->type == VR_UNDEFINED)
3648 return NULL_TREE;
3650 /* Anti-ranges need to be handled separately. */
3651 if (vr0->type == VR_ANTI_RANGE || vr1->type == VR_ANTI_RANGE)
3653 /* If both are anti-ranges, then we cannot compute any
3654 comparison. */
3655 if (vr0->type == VR_ANTI_RANGE && vr1->type == VR_ANTI_RANGE)
3656 return NULL_TREE;
3658 /* These comparisons are never statically computable. */
3659 if (comp == GT_EXPR
3660 || comp == GE_EXPR
3661 || comp == LT_EXPR
3662 || comp == LE_EXPR)
3663 return NULL_TREE;
3665 /* Equality can be computed only between a range and an
3666 anti-range. ~[VAL1, VAL2] == [VAL1, VAL2] is always false. */
3667 if (vr0->type == VR_RANGE)
3669 /* To simplify processing, make VR0 the anti-range. */
3670 value_range_t *tmp = vr0;
3671 vr0 = vr1;
3672 vr1 = tmp;
3675 gcc_assert (comp == NE_EXPR || comp == EQ_EXPR);
3677 if (compare_values_warnv (vr0->min, vr1->min, strict_overflow_p) == 0
3678 && compare_values_warnv (vr0->max, vr1->max, strict_overflow_p) == 0)
3679 return (comp == NE_EXPR) ? boolean_true_node : boolean_false_node;
3681 return NULL_TREE;
3684 if (!usable_range_p (vr0, strict_overflow_p)
3685 || !usable_range_p (vr1, strict_overflow_p))
3686 return NULL_TREE;
3688 /* Simplify processing. If COMP is GT_EXPR or GE_EXPR, switch the
3689 operands around and change the comparison code. */
3690 if (comp == GT_EXPR || comp == GE_EXPR)
3692 value_range_t *tmp;
3693 comp = (comp == GT_EXPR) ? LT_EXPR : LE_EXPR;
3694 tmp = vr0;
3695 vr0 = vr1;
3696 vr1 = tmp;
3699 if (comp == EQ_EXPR)
3701 /* Equality may only be computed if both ranges represent
3702 exactly one value. */
3703 if (compare_values_warnv (vr0->min, vr0->max, strict_overflow_p) == 0
3704 && compare_values_warnv (vr1->min, vr1->max, strict_overflow_p) == 0)
3706 int cmp_min = compare_values_warnv (vr0->min, vr1->min,
3707 strict_overflow_p);
3708 int cmp_max = compare_values_warnv (vr0->max, vr1->max,
3709 strict_overflow_p);
3710 if (cmp_min == 0 && cmp_max == 0)
3711 return boolean_true_node;
3712 else if (cmp_min != -2 && cmp_max != -2)
3713 return boolean_false_node;
3715 /* If [V0_MIN, V1_MAX] < [V1_MIN, V1_MAX] then V0 != V1. */
3716 else if (compare_values_warnv (vr0->min, vr1->max,
3717 strict_overflow_p) == 1
3718 || compare_values_warnv (vr1->min, vr0->max,
3719 strict_overflow_p) == 1)
3720 return boolean_false_node;
3722 return NULL_TREE;
3724 else if (comp == NE_EXPR)
3726 int cmp1, cmp2;
3728 /* If VR0 is completely to the left or completely to the right
3729 of VR1, they are always different. Notice that we need to
3730 make sure that both comparisons yield similar results to
3731 avoid comparing values that cannot be compared at
3732 compile-time. */
3733 cmp1 = compare_values_warnv (vr0->max, vr1->min, strict_overflow_p);
3734 cmp2 = compare_values_warnv (vr0->min, vr1->max, strict_overflow_p);
3735 if ((cmp1 == -1 && cmp2 == -1) || (cmp1 == 1 && cmp2 == 1))
3736 return boolean_true_node;
3738 /* If VR0 and VR1 represent a single value and are identical,
3739 return false. */
3740 else if (compare_values_warnv (vr0->min, vr0->max,
3741 strict_overflow_p) == 0
3742 && compare_values_warnv (vr1->min, vr1->max,
3743 strict_overflow_p) == 0
3744 && compare_values_warnv (vr0->min, vr1->min,
3745 strict_overflow_p) == 0
3746 && compare_values_warnv (vr0->max, vr1->max,
3747 strict_overflow_p) == 0)
3748 return boolean_false_node;
3750 /* Otherwise, they may or may not be different. */
3751 else
3752 return NULL_TREE;
3754 else if (comp == LT_EXPR || comp == LE_EXPR)
3756 int tst;
3758 /* If VR0 is to the left of VR1, return true. */
3759 tst = compare_values_warnv (vr0->max, vr1->min, strict_overflow_p);
3760 if ((comp == LT_EXPR && tst == -1)
3761 || (comp == LE_EXPR && (tst == -1 || tst == 0)))
3763 if (overflow_infinity_range_p (vr0)
3764 || overflow_infinity_range_p (vr1))
3765 *strict_overflow_p = true;
3766 return boolean_true_node;
3769 /* If VR0 is to the right of VR1, return false. */
3770 tst = compare_values_warnv (vr0->min, vr1->max, strict_overflow_p);
3771 if ((comp == LT_EXPR && (tst == 0 || tst == 1))
3772 || (comp == LE_EXPR && tst == 1))
3774 if (overflow_infinity_range_p (vr0)
3775 || overflow_infinity_range_p (vr1))
3776 *strict_overflow_p = true;
3777 return boolean_false_node;
3780 /* Otherwise, we don't know. */
3781 return NULL_TREE;
3784 gcc_unreachable ();
3788 /* Given a value range VR, a value VAL and a comparison code COMP, return
3789 BOOLEAN_TRUE_NODE if VR COMP VAL always returns true for all the
3790 values in VR. Return BOOLEAN_FALSE_NODE if the comparison
3791 always returns false. Return NULL_TREE if it is not always
3792 possible to determine the value of the comparison. Also set
3793 *STRICT_OVERFLOW_P to indicate whether a range with an overflow
3794 infinity was used in the test. */
3796 static tree
3797 compare_range_with_value (enum tree_code comp, value_range_t *vr, tree val,
3798 bool *strict_overflow_p)
3800 if (vr->type == VR_VARYING || vr->type == VR_UNDEFINED)
3801 return NULL_TREE;
3803 /* Anti-ranges need to be handled separately. */
3804 if (vr->type == VR_ANTI_RANGE)
3806 /* For anti-ranges, the only predicates that we can compute at
3807 compile time are equality and inequality. */
3808 if (comp == GT_EXPR
3809 || comp == GE_EXPR
3810 || comp == LT_EXPR
3811 || comp == LE_EXPR)
3812 return NULL_TREE;
3814 /* ~[VAL_1, VAL_2] OP VAL is known if VAL_1 <= VAL <= VAL_2. */
3815 if (value_inside_range (val, vr) == 1)
3816 return (comp == NE_EXPR) ? boolean_true_node : boolean_false_node;
3818 return NULL_TREE;
3821 if (!usable_range_p (vr, strict_overflow_p))
3822 return NULL_TREE;
3824 if (comp == EQ_EXPR)
3826 /* EQ_EXPR may only be computed if VR represents exactly
3827 one value. */
3828 if (compare_values_warnv (vr->min, vr->max, strict_overflow_p) == 0)
3830 int cmp = compare_values_warnv (vr->min, val, strict_overflow_p);
3831 if (cmp == 0)
3832 return boolean_true_node;
3833 else if (cmp == -1 || cmp == 1 || cmp == 2)
3834 return boolean_false_node;
3836 else if (compare_values_warnv (val, vr->min, strict_overflow_p) == -1
3837 || compare_values_warnv (vr->max, val, strict_overflow_p) == -1)
3838 return boolean_false_node;
3840 return NULL_TREE;
3842 else if (comp == NE_EXPR)
3844 /* If VAL is not inside VR, then they are always different. */
3845 if (compare_values_warnv (vr->max, val, strict_overflow_p) == -1
3846 || compare_values_warnv (vr->min, val, strict_overflow_p) == 1)
3847 return boolean_true_node;
3849 /* If VR represents exactly one value equal to VAL, then return
3850 false. */
3851 if (compare_values_warnv (vr->min, vr->max, strict_overflow_p) == 0
3852 && compare_values_warnv (vr->min, val, strict_overflow_p) == 0)
3853 return boolean_false_node;
3855 /* Otherwise, they may or may not be different. */
3856 return NULL_TREE;
3858 else if (comp == LT_EXPR || comp == LE_EXPR)
3860 int tst;
3862 /* If VR is to the left of VAL, return true. */
3863 tst = compare_values_warnv (vr->max, val, strict_overflow_p);
3864 if ((comp == LT_EXPR && tst == -1)
3865 || (comp == LE_EXPR && (tst == -1 || tst == 0)))
3867 if (overflow_infinity_range_p (vr))
3868 *strict_overflow_p = true;
3869 return boolean_true_node;
3872 /* If VR is to the right of VAL, return false. */
3873 tst = compare_values_warnv (vr->min, val, strict_overflow_p);
3874 if ((comp == LT_EXPR && (tst == 0 || tst == 1))
3875 || (comp == LE_EXPR && tst == 1))
3877 if (overflow_infinity_range_p (vr))
3878 *strict_overflow_p = true;
3879 return boolean_false_node;
3882 /* Otherwise, we don't know. */
3883 return NULL_TREE;
3885 else if (comp == GT_EXPR || comp == GE_EXPR)
3887 int tst;
3889 /* If VR is to the right of VAL, return true. */
3890 tst = compare_values_warnv (vr->min, val, strict_overflow_p);
3891 if ((comp == GT_EXPR && tst == 1)
3892 || (comp == GE_EXPR && (tst == 0 || tst == 1)))
3894 if (overflow_infinity_range_p (vr))
3895 *strict_overflow_p = true;
3896 return boolean_true_node;
3899 /* If VR is to the left of VAL, return false. */
3900 tst = compare_values_warnv (vr->max, val, strict_overflow_p);
3901 if ((comp == GT_EXPR && (tst == -1 || tst == 0))
3902 || (comp == GE_EXPR && tst == -1))
3904 if (overflow_infinity_range_p (vr))
3905 *strict_overflow_p = true;
3906 return boolean_false_node;
3909 /* Otherwise, we don't know. */
3910 return NULL_TREE;
3913 gcc_unreachable ();
3917 /* Debugging dumps. */
3919 void dump_value_range (FILE *, value_range_t *);
3920 void debug_value_range (value_range_t *);
3921 void dump_all_value_ranges (FILE *);
3922 void debug_all_value_ranges (void);
3923 void dump_vr_equiv (FILE *, bitmap);
3924 void debug_vr_equiv (bitmap);
3927 /* Dump value range VR to FILE. */
3929 void
3930 dump_value_range (FILE *file, value_range_t *vr)
3932 if (vr == NULL)
3933 fprintf (file, "[]");
3934 else if (vr->type == VR_UNDEFINED)
3935 fprintf (file, "UNDEFINED");
3936 else if (vr->type == VR_RANGE || vr->type == VR_ANTI_RANGE)
3938 tree type = TREE_TYPE (vr->min);
3940 fprintf (file, "%s[", (vr->type == VR_ANTI_RANGE) ? "~" : "");
3942 if (is_negative_overflow_infinity (vr->min))
3943 fprintf (file, "-INF(OVF)");
3944 else if (INTEGRAL_TYPE_P (type)
3945 && !TYPE_UNSIGNED (type)
3946 && vrp_val_is_min (vr->min))
3947 fprintf (file, "-INF");
3948 else
3949 print_generic_expr (file, vr->min, 0);
3951 fprintf (file, ", ");
3953 if (is_positive_overflow_infinity (vr->max))
3954 fprintf (file, "+INF(OVF)");
3955 else if (INTEGRAL_TYPE_P (type)
3956 && vrp_val_is_max (vr->max))
3957 fprintf (file, "+INF");
3958 else
3959 print_generic_expr (file, vr->max, 0);
3961 fprintf (file, "]");
3963 if (vr->equiv)
3965 bitmap_iterator bi;
3966 unsigned i, c = 0;
3968 fprintf (file, " EQUIVALENCES: { ");
3970 EXECUTE_IF_SET_IN_BITMAP (vr->equiv, 0, i, bi)
3972 print_generic_expr (file, ssa_name (i), 0);
3973 fprintf (file, " ");
3974 c++;
3977 fprintf (file, "} (%u elements)", c);
3980 else if (vr->type == VR_VARYING)
3981 fprintf (file, "VARYING");
3982 else
3983 fprintf (file, "INVALID RANGE");
3987 /* Dump value range VR to stderr. */
3989 DEBUG_FUNCTION void
3990 debug_value_range (value_range_t *vr)
3992 dump_value_range (stderr, vr);
3993 fprintf (stderr, "\n");
3997 /* Dump value ranges of all SSA_NAMEs to FILE. */
3999 void
4000 dump_all_value_ranges (FILE *file)
4002 size_t i;
4004 for (i = 0; i < num_vr_values; i++)
4006 if (vr_value[i])
4008 print_generic_expr (file, ssa_name (i), 0);
4009 fprintf (file, ": ");
4010 dump_value_range (file, vr_value[i]);
4011 fprintf (file, "\n");
4015 fprintf (file, "\n");
4019 /* Dump all value ranges to stderr. */
4021 DEBUG_FUNCTION void
4022 debug_all_value_ranges (void)
4024 dump_all_value_ranges (stderr);
4028 /* Given a COND_EXPR COND of the form 'V OP W', and an SSA name V,
4029 create a new SSA name N and return the assertion assignment
4030 'V = ASSERT_EXPR <V, V OP W>'. */
4032 static gimple
4033 build_assert_expr_for (tree cond, tree v)
4035 tree n;
4036 gimple assertion;
4038 gcc_assert (TREE_CODE (v) == SSA_NAME);
4039 n = duplicate_ssa_name (v, NULL);
4041 if (COMPARISON_CLASS_P (cond))
4043 tree a = build2 (ASSERT_EXPR, TREE_TYPE (v), v, cond);
4044 assertion = gimple_build_assign (n, a);
4046 else if (TREE_CODE (cond) == SSA_NAME)
4048 /* Given V, build the assignment N = true. */
4049 gcc_assert (v == cond);
4050 assertion = gimple_build_assign (n, boolean_true_node);
4052 else
4053 gcc_unreachable ();
4055 SSA_NAME_DEF_STMT (n) = assertion;
4057 /* The new ASSERT_EXPR, creates a new SSA name that replaces the
4058 operand of the ASSERT_EXPR. Register the new name and the old one
4059 in the replacement table so that we can fix the SSA web after
4060 adding all the ASSERT_EXPRs. */
4061 register_new_name_mapping (n, v);
4063 return assertion;
4067 /* Return false if EXPR is a predicate expression involving floating
4068 point values. */
4070 static inline bool
4071 fp_predicate (gimple stmt)
4073 GIMPLE_CHECK (stmt, GIMPLE_COND);
4075 return FLOAT_TYPE_P (TREE_TYPE (gimple_cond_lhs (stmt)));
4079 /* If the range of values taken by OP can be inferred after STMT executes,
4080 return the comparison code (COMP_CODE_P) and value (VAL_P) that
4081 describes the inferred range. Return true if a range could be
4082 inferred. */
4084 static bool
4085 infer_value_range (gimple stmt, tree op, enum tree_code *comp_code_p, tree *val_p)
4087 *val_p = NULL_TREE;
4088 *comp_code_p = ERROR_MARK;
4090 /* Do not attempt to infer anything in names that flow through
4091 abnormal edges. */
4092 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op))
4093 return false;
4095 /* Similarly, don't infer anything from statements that may throw
4096 exceptions. */
4097 if (stmt_could_throw_p (stmt))
4098 return false;
4100 /* If STMT is the last statement of a basic block with no
4101 successors, there is no point inferring anything about any of its
4102 operands. We would not be able to find a proper insertion point
4103 for the assertion, anyway. */
4104 if (stmt_ends_bb_p (stmt) && EDGE_COUNT (gimple_bb (stmt)->succs) == 0)
4105 return false;
4107 /* We can only assume that a pointer dereference will yield
4108 non-NULL if -fdelete-null-pointer-checks is enabled. */
4109 if (flag_delete_null_pointer_checks
4110 && POINTER_TYPE_P (TREE_TYPE (op))
4111 && gimple_code (stmt) != GIMPLE_ASM)
4113 unsigned num_uses, num_loads, num_stores;
4115 count_uses_and_derefs (op, stmt, &num_uses, &num_loads, &num_stores);
4116 if (num_loads + num_stores > 0)
4118 *val_p = build_int_cst (TREE_TYPE (op), 0);
4119 *comp_code_p = NE_EXPR;
4120 return true;
4124 return false;
4128 void dump_asserts_for (FILE *, tree);
4129 void debug_asserts_for (tree);
4130 void dump_all_asserts (FILE *);
4131 void debug_all_asserts (void);
4133 /* Dump all the registered assertions for NAME to FILE. */
4135 void
4136 dump_asserts_for (FILE *file, tree name)
4138 assert_locus_t loc;
4140 fprintf (file, "Assertions to be inserted for ");
4141 print_generic_expr (file, name, 0);
4142 fprintf (file, "\n");
4144 loc = asserts_for[SSA_NAME_VERSION (name)];
4145 while (loc)
4147 fprintf (file, "\t");
4148 print_gimple_stmt (file, gsi_stmt (loc->si), 0, 0);
4149 fprintf (file, "\n\tBB #%d", loc->bb->index);
4150 if (loc->e)
4152 fprintf (file, "\n\tEDGE %d->%d", loc->e->src->index,
4153 loc->e->dest->index);
4154 dump_edge_info (file, loc->e, 0);
4156 fprintf (file, "\n\tPREDICATE: ");
4157 print_generic_expr (file, name, 0);
4158 fprintf (file, " %s ", tree_code_name[(int)loc->comp_code]);
4159 print_generic_expr (file, loc->val, 0);
4160 fprintf (file, "\n\n");
4161 loc = loc->next;
4164 fprintf (file, "\n");
4168 /* Dump all the registered assertions for NAME to stderr. */
4170 DEBUG_FUNCTION void
4171 debug_asserts_for (tree name)
4173 dump_asserts_for (stderr, name);
4177 /* Dump all the registered assertions for all the names to FILE. */
4179 void
4180 dump_all_asserts (FILE *file)
4182 unsigned i;
4183 bitmap_iterator bi;
4185 fprintf (file, "\nASSERT_EXPRs to be inserted\n\n");
4186 EXECUTE_IF_SET_IN_BITMAP (need_assert_for, 0, i, bi)
4187 dump_asserts_for (file, ssa_name (i));
4188 fprintf (file, "\n");
4192 /* Dump all the registered assertions for all the names to stderr. */
4194 DEBUG_FUNCTION void
4195 debug_all_asserts (void)
4197 dump_all_asserts (stderr);
4201 /* If NAME doesn't have an ASSERT_EXPR registered for asserting
4202 'EXPR COMP_CODE VAL' at a location that dominates block BB or
4203 E->DEST, then register this location as a possible insertion point
4204 for ASSERT_EXPR <NAME, EXPR COMP_CODE VAL>.
4206 BB, E and SI provide the exact insertion point for the new
4207 ASSERT_EXPR. If BB is NULL, then the ASSERT_EXPR is to be inserted
4208 on edge E. Otherwise, if E is NULL, the ASSERT_EXPR is inserted on
4209 BB. If SI points to a COND_EXPR or a SWITCH_EXPR statement, then E
4210 must not be NULL. */
4212 static void
4213 register_new_assert_for (tree name, tree expr,
4214 enum tree_code comp_code,
4215 tree val,
4216 basic_block bb,
4217 edge e,
4218 gimple_stmt_iterator si)
4220 assert_locus_t n, loc, last_loc;
4221 basic_block dest_bb;
4223 gcc_checking_assert (bb == NULL || e == NULL);
4225 if (e == NULL)
4226 gcc_checking_assert (gimple_code (gsi_stmt (si)) != GIMPLE_COND
4227 && gimple_code (gsi_stmt (si)) != GIMPLE_SWITCH);
4229 /* Never build an assert comparing against an integer constant with
4230 TREE_OVERFLOW set. This confuses our undefined overflow warning
4231 machinery. */
4232 if (TREE_CODE (val) == INTEGER_CST
4233 && TREE_OVERFLOW (val))
4234 val = build_int_cst_wide (TREE_TYPE (val),
4235 TREE_INT_CST_LOW (val), TREE_INT_CST_HIGH (val));
4237 /* The new assertion A will be inserted at BB or E. We need to
4238 determine if the new location is dominated by a previously
4239 registered location for A. If we are doing an edge insertion,
4240 assume that A will be inserted at E->DEST. Note that this is not
4241 necessarily true.
4243 If E is a critical edge, it will be split. But even if E is
4244 split, the new block will dominate the same set of blocks that
4245 E->DEST dominates.
4247 The reverse, however, is not true, blocks dominated by E->DEST
4248 will not be dominated by the new block created to split E. So,
4249 if the insertion location is on a critical edge, we will not use
4250 the new location to move another assertion previously registered
4251 at a block dominated by E->DEST. */
4252 dest_bb = (bb) ? bb : e->dest;
4254 /* If NAME already has an ASSERT_EXPR registered for COMP_CODE and
4255 VAL at a block dominating DEST_BB, then we don't need to insert a new
4256 one. Similarly, if the same assertion already exists at a block
4257 dominated by DEST_BB and the new location is not on a critical
4258 edge, then update the existing location for the assertion (i.e.,
4259 move the assertion up in the dominance tree).
4261 Note, this is implemented as a simple linked list because there
4262 should not be more than a handful of assertions registered per
4263 name. If this becomes a performance problem, a table hashed by
4264 COMP_CODE and VAL could be implemented. */
4265 loc = asserts_for[SSA_NAME_VERSION (name)];
4266 last_loc = loc;
4267 while (loc)
4269 if (loc->comp_code == comp_code
4270 && (loc->val == val
4271 || operand_equal_p (loc->val, val, 0))
4272 && (loc->expr == expr
4273 || operand_equal_p (loc->expr, expr, 0)))
4275 /* If the assertion NAME COMP_CODE VAL has already been
4276 registered at a basic block that dominates DEST_BB, then
4277 we don't need to insert the same assertion again. Note
4278 that we don't check strict dominance here to avoid
4279 replicating the same assertion inside the same basic
4280 block more than once (e.g., when a pointer is
4281 dereferenced several times inside a block).
4283 An exception to this rule are edge insertions. If the
4284 new assertion is to be inserted on edge E, then it will
4285 dominate all the other insertions that we may want to
4286 insert in DEST_BB. So, if we are doing an edge
4287 insertion, don't do this dominance check. */
4288 if (e == NULL
4289 && dominated_by_p (CDI_DOMINATORS, dest_bb, loc->bb))
4290 return;
4292 /* Otherwise, if E is not a critical edge and DEST_BB
4293 dominates the existing location for the assertion, move
4294 the assertion up in the dominance tree by updating its
4295 location information. */
4296 if ((e == NULL || !EDGE_CRITICAL_P (e))
4297 && dominated_by_p (CDI_DOMINATORS, loc->bb, dest_bb))
4299 loc->bb = dest_bb;
4300 loc->e = e;
4301 loc->si = si;
4302 return;
4306 /* Update the last node of the list and move to the next one. */
4307 last_loc = loc;
4308 loc = loc->next;
4311 /* If we didn't find an assertion already registered for
4312 NAME COMP_CODE VAL, add a new one at the end of the list of
4313 assertions associated with NAME. */
4314 n = XNEW (struct assert_locus_d);
4315 n->bb = dest_bb;
4316 n->e = e;
4317 n->si = si;
4318 n->comp_code = comp_code;
4319 n->val = val;
4320 n->expr = expr;
4321 n->next = NULL;
4323 if (last_loc)
4324 last_loc->next = n;
4325 else
4326 asserts_for[SSA_NAME_VERSION (name)] = n;
4328 bitmap_set_bit (need_assert_for, SSA_NAME_VERSION (name));
4331 /* (COND_OP0 COND_CODE COND_OP1) is a predicate which uses NAME.
4332 Extract a suitable test code and value and store them into *CODE_P and
4333 *VAL_P so the predicate is normalized to NAME *CODE_P *VAL_P.
4335 If no extraction was possible, return FALSE, otherwise return TRUE.
4337 If INVERT is true, then we invert the result stored into *CODE_P. */
4339 static bool
4340 extract_code_and_val_from_cond_with_ops (tree name, enum tree_code cond_code,
4341 tree cond_op0, tree cond_op1,
4342 bool invert, enum tree_code *code_p,
4343 tree *val_p)
4345 enum tree_code comp_code;
4346 tree val;
4348 /* Otherwise, we have a comparison of the form NAME COMP VAL
4349 or VAL COMP NAME. */
4350 if (name == cond_op1)
4352 /* If the predicate is of the form VAL COMP NAME, flip
4353 COMP around because we need to register NAME as the
4354 first operand in the predicate. */
4355 comp_code = swap_tree_comparison (cond_code);
4356 val = cond_op0;
4358 else
4360 /* The comparison is of the form NAME COMP VAL, so the
4361 comparison code remains unchanged. */
4362 comp_code = cond_code;
4363 val = cond_op1;
4366 /* Invert the comparison code as necessary. */
4367 if (invert)
4368 comp_code = invert_tree_comparison (comp_code, 0);
4370 /* VRP does not handle float types. */
4371 if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (val)))
4372 return false;
4374 /* Do not register always-false predicates.
4375 FIXME: this works around a limitation in fold() when dealing with
4376 enumerations. Given 'enum { N1, N2 } x;', fold will not
4377 fold 'if (x > N2)' to 'if (0)'. */
4378 if ((comp_code == GT_EXPR || comp_code == LT_EXPR)
4379 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
4381 tree min = TYPE_MIN_VALUE (TREE_TYPE (val));
4382 tree max = TYPE_MAX_VALUE (TREE_TYPE (val));
4384 if (comp_code == GT_EXPR
4385 && (!max
4386 || compare_values (val, max) == 0))
4387 return false;
4389 if (comp_code == LT_EXPR
4390 && (!min
4391 || compare_values (val, min) == 0))
4392 return false;
4394 *code_p = comp_code;
4395 *val_p = val;
4396 return true;
4399 /* Find out smallest RES where RES > VAL && (RES & MASK) == RES, if any
4400 (otherwise return VAL). VAL and MASK must be zero-extended for
4401 precision PREC. If SGNBIT is non-zero, first xor VAL with SGNBIT
4402 (to transform signed values into unsigned) and at the end xor
4403 SGNBIT back. */
4405 static double_int
4406 masked_increment (double_int val, double_int mask, double_int sgnbit,
4407 unsigned int prec)
4409 double_int bit = double_int_one, res;
4410 unsigned int i;
4412 val = double_int_xor (val, sgnbit);
4413 for (i = 0; i < prec; i++, bit = double_int_add (bit, bit))
4415 res = mask;
4416 if (double_int_zero_p (double_int_and (res, bit)))
4417 continue;
4418 res = double_int_sub (bit, double_int_one);
4419 res = double_int_and_not (double_int_add (val, bit), res);
4420 res = double_int_and (res, mask);
4421 if (double_int_ucmp (res, val) > 0)
4422 return double_int_xor (res, sgnbit);
4424 return double_int_xor (val, sgnbit);
4427 /* Try to register an edge assertion for SSA name NAME on edge E for
4428 the condition COND contributing to the conditional jump pointed to by BSI.
4429 Invert the condition COND if INVERT is true.
4430 Return true if an assertion for NAME could be registered. */
4432 static bool
4433 register_edge_assert_for_2 (tree name, edge e, gimple_stmt_iterator bsi,
4434 enum tree_code cond_code,
4435 tree cond_op0, tree cond_op1, bool invert)
4437 tree val;
4438 enum tree_code comp_code;
4439 bool retval = false;
4441 if (!extract_code_and_val_from_cond_with_ops (name, cond_code,
4442 cond_op0,
4443 cond_op1,
4444 invert, &comp_code, &val))
4445 return false;
4447 /* Only register an ASSERT_EXPR if NAME was found in the sub-graph
4448 reachable from E. */
4449 if (live_on_edge (e, name)
4450 && !has_single_use (name))
4452 register_new_assert_for (name, name, comp_code, val, NULL, e, bsi);
4453 retval = true;
4456 /* In the case of NAME <= CST and NAME being defined as
4457 NAME = (unsigned) NAME2 + CST2 we can assert NAME2 >= -CST2
4458 and NAME2 <= CST - CST2. We can do the same for NAME > CST.
4459 This catches range and anti-range tests. */
4460 if ((comp_code == LE_EXPR
4461 || comp_code == GT_EXPR)
4462 && TREE_CODE (val) == INTEGER_CST
4463 && TYPE_UNSIGNED (TREE_TYPE (val)))
4465 gimple def_stmt = SSA_NAME_DEF_STMT (name);
4466 tree cst2 = NULL_TREE, name2 = NULL_TREE, name3 = NULL_TREE;
4468 /* Extract CST2 from the (optional) addition. */
4469 if (is_gimple_assign (def_stmt)
4470 && gimple_assign_rhs_code (def_stmt) == PLUS_EXPR)
4472 name2 = gimple_assign_rhs1 (def_stmt);
4473 cst2 = gimple_assign_rhs2 (def_stmt);
4474 if (TREE_CODE (name2) == SSA_NAME
4475 && TREE_CODE (cst2) == INTEGER_CST)
4476 def_stmt = SSA_NAME_DEF_STMT (name2);
4479 /* Extract NAME2 from the (optional) sign-changing cast. */
4480 if (gimple_assign_cast_p (def_stmt))
4482 if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt))
4483 && ! TYPE_UNSIGNED (TREE_TYPE (gimple_assign_rhs1 (def_stmt)))
4484 && (TYPE_PRECISION (gimple_expr_type (def_stmt))
4485 == TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (def_stmt)))))
4486 name3 = gimple_assign_rhs1 (def_stmt);
4489 /* If name3 is used later, create an ASSERT_EXPR for it. */
4490 if (name3 != NULL_TREE
4491 && TREE_CODE (name3) == SSA_NAME
4492 && (cst2 == NULL_TREE
4493 || TREE_CODE (cst2) == INTEGER_CST)
4494 && INTEGRAL_TYPE_P (TREE_TYPE (name3))
4495 && live_on_edge (e, name3)
4496 && !has_single_use (name3))
4498 tree tmp;
4500 /* Build an expression for the range test. */
4501 tmp = build1 (NOP_EXPR, TREE_TYPE (name), name3);
4502 if (cst2 != NULL_TREE)
4503 tmp = build2 (PLUS_EXPR, TREE_TYPE (name), tmp, cst2);
4505 if (dump_file)
4507 fprintf (dump_file, "Adding assert for ");
4508 print_generic_expr (dump_file, name3, 0);
4509 fprintf (dump_file, " from ");
4510 print_generic_expr (dump_file, tmp, 0);
4511 fprintf (dump_file, "\n");
4514 register_new_assert_for (name3, tmp, comp_code, val, NULL, e, bsi);
4516 retval = true;
4519 /* If name2 is used later, create an ASSERT_EXPR for it. */
4520 if (name2 != NULL_TREE
4521 && TREE_CODE (name2) == SSA_NAME
4522 && TREE_CODE (cst2) == INTEGER_CST
4523 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
4524 && live_on_edge (e, name2)
4525 && !has_single_use (name2))
4527 tree tmp;
4529 /* Build an expression for the range test. */
4530 tmp = name2;
4531 if (TREE_TYPE (name) != TREE_TYPE (name2))
4532 tmp = build1 (NOP_EXPR, TREE_TYPE (name), tmp);
4533 if (cst2 != NULL_TREE)
4534 tmp = build2 (PLUS_EXPR, TREE_TYPE (name), tmp, cst2);
4536 if (dump_file)
4538 fprintf (dump_file, "Adding assert for ");
4539 print_generic_expr (dump_file, name2, 0);
4540 fprintf (dump_file, " from ");
4541 print_generic_expr (dump_file, tmp, 0);
4542 fprintf (dump_file, "\n");
4545 register_new_assert_for (name2, tmp, comp_code, val, NULL, e, bsi);
4547 retval = true;
4551 if (TREE_CODE_CLASS (comp_code) == tcc_comparison
4552 && TREE_CODE (val) == INTEGER_CST)
4554 gimple def_stmt = SSA_NAME_DEF_STMT (name);
4555 tree name2 = NULL_TREE, names[2], cst2 = NULL_TREE;
4556 tree val2 = NULL_TREE;
4557 double_int mask = double_int_zero;
4558 unsigned int prec = TYPE_PRECISION (TREE_TYPE (val));
4560 /* Add asserts for NAME cmp CST and NAME being defined
4561 as NAME = (int) NAME2. */
4562 if (!TYPE_UNSIGNED (TREE_TYPE (val))
4563 && (comp_code == LE_EXPR || comp_code == LT_EXPR
4564 || comp_code == GT_EXPR || comp_code == GE_EXPR)
4565 && gimple_assign_cast_p (def_stmt))
4567 name2 = gimple_assign_rhs1 (def_stmt);
4568 if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt))
4569 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
4570 && TYPE_UNSIGNED (TREE_TYPE (name2))
4571 && prec == TYPE_PRECISION (TREE_TYPE (name2))
4572 && (comp_code == LE_EXPR || comp_code == GT_EXPR
4573 || !tree_int_cst_equal (val,
4574 TYPE_MIN_VALUE (TREE_TYPE (val))))
4575 && live_on_edge (e, name2)
4576 && !has_single_use (name2))
4578 tree tmp, cst;
4579 enum tree_code new_comp_code = comp_code;
4581 cst = fold_convert (TREE_TYPE (name2),
4582 TYPE_MIN_VALUE (TREE_TYPE (val)));
4583 /* Build an expression for the range test. */
4584 tmp = build2 (PLUS_EXPR, TREE_TYPE (name2), name2, cst);
4585 cst = fold_build2 (PLUS_EXPR, TREE_TYPE (name2), cst,
4586 fold_convert (TREE_TYPE (name2), val));
4587 if (comp_code == LT_EXPR || comp_code == GE_EXPR)
4589 new_comp_code = comp_code == LT_EXPR ? LE_EXPR : GT_EXPR;
4590 cst = fold_build2 (MINUS_EXPR, TREE_TYPE (name2), cst,
4591 build_int_cst (TREE_TYPE (name2), 1));
4594 if (dump_file)
4596 fprintf (dump_file, "Adding assert for ");
4597 print_generic_expr (dump_file, name2, 0);
4598 fprintf (dump_file, " from ");
4599 print_generic_expr (dump_file, tmp, 0);
4600 fprintf (dump_file, "\n");
4603 register_new_assert_for (name2, tmp, new_comp_code, cst, NULL,
4604 e, bsi);
4606 retval = true;
4610 /* Add asserts for NAME cmp CST and NAME being defined as
4611 NAME = NAME2 >> CST2.
4613 Extract CST2 from the right shift. */
4614 if (is_gimple_assign (def_stmt)
4615 && gimple_assign_rhs_code (def_stmt) == RSHIFT_EXPR)
4617 name2 = gimple_assign_rhs1 (def_stmt);
4618 cst2 = gimple_assign_rhs2 (def_stmt);
4619 if (TREE_CODE (name2) == SSA_NAME
4620 && host_integerp (cst2, 1)
4621 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
4622 && IN_RANGE (tree_low_cst (cst2, 1), 1, prec - 1)
4623 && prec <= HOST_BITS_PER_DOUBLE_INT
4624 && prec == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (val)))
4625 && live_on_edge (e, name2)
4626 && !has_single_use (name2))
4628 mask = double_int_mask (tree_low_cst (cst2, 1));
4629 val2 = fold_binary (LSHIFT_EXPR, TREE_TYPE (val), val, cst2);
4632 if (val2 != NULL_TREE
4633 && TREE_CODE (val2) == INTEGER_CST
4634 && simple_cst_equal (fold_build2 (RSHIFT_EXPR,
4635 TREE_TYPE (val),
4636 val2, cst2), val))
4638 enum tree_code new_comp_code = comp_code;
4639 tree tmp, new_val;
4641 tmp = name2;
4642 if (comp_code == EQ_EXPR || comp_code == NE_EXPR)
4644 if (!TYPE_UNSIGNED (TREE_TYPE (val)))
4646 tree type = build_nonstandard_integer_type (prec, 1);
4647 tmp = build1 (NOP_EXPR, type, name2);
4648 val2 = fold_convert (type, val2);
4650 tmp = fold_build2 (MINUS_EXPR, TREE_TYPE (tmp), tmp, val2);
4651 new_val = double_int_to_tree (TREE_TYPE (tmp), mask);
4652 new_comp_code = comp_code == EQ_EXPR ? LE_EXPR : GT_EXPR;
4654 else if (comp_code == LT_EXPR || comp_code == GE_EXPR)
4655 new_val = val2;
4656 else
4658 double_int maxval
4659 = double_int_max_value (prec, TYPE_UNSIGNED (TREE_TYPE (val)));
4660 mask = double_int_ior (tree_to_double_int (val2), mask);
4661 if (double_int_equal_p (mask, maxval))
4662 new_val = NULL_TREE;
4663 else
4664 new_val = double_int_to_tree (TREE_TYPE (val2), mask);
4667 if (new_val)
4669 if (dump_file)
4671 fprintf (dump_file, "Adding assert for ");
4672 print_generic_expr (dump_file, name2, 0);
4673 fprintf (dump_file, " from ");
4674 print_generic_expr (dump_file, tmp, 0);
4675 fprintf (dump_file, "\n");
4678 register_new_assert_for (name2, tmp, new_comp_code, new_val,
4679 NULL, e, bsi);
4680 retval = true;
4684 /* Add asserts for NAME cmp CST and NAME being defined as
4685 NAME = NAME2 & CST2.
4687 Extract CST2 from the and. */
4688 names[0] = NULL_TREE;
4689 names[1] = NULL_TREE;
4690 cst2 = NULL_TREE;
4691 if (is_gimple_assign (def_stmt)
4692 && gimple_assign_rhs_code (def_stmt) == BIT_AND_EXPR)
4694 name2 = gimple_assign_rhs1 (def_stmt);
4695 cst2 = gimple_assign_rhs2 (def_stmt);
4696 if (TREE_CODE (name2) == SSA_NAME
4697 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
4698 && TREE_CODE (cst2) == INTEGER_CST
4699 && !integer_zerop (cst2)
4700 && prec <= HOST_BITS_PER_DOUBLE_INT
4701 && (prec > 1
4702 || TYPE_UNSIGNED (TREE_TYPE (val))))
4704 gimple def_stmt2 = SSA_NAME_DEF_STMT (name2);
4705 if (gimple_assign_cast_p (def_stmt2))
4707 names[1] = gimple_assign_rhs1 (def_stmt2);
4708 if (!CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt2))
4709 || !INTEGRAL_TYPE_P (TREE_TYPE (names[1]))
4710 || (TYPE_PRECISION (TREE_TYPE (name2))
4711 != TYPE_PRECISION (TREE_TYPE (names[1])))
4712 || !live_on_edge (e, names[1])
4713 || has_single_use (names[1]))
4714 names[1] = NULL_TREE;
4716 if (live_on_edge (e, name2)
4717 && !has_single_use (name2))
4718 names[0] = name2;
4721 if (names[0] || names[1])
4723 double_int minv, maxv = double_int_zero, valv, cst2v;
4724 double_int tem, sgnbit;
4725 bool valid_p = false, valn = false, cst2n = false;
4726 enum tree_code ccode = comp_code;
4728 valv = double_int_zext (tree_to_double_int (val), prec);
4729 cst2v = double_int_zext (tree_to_double_int (cst2), prec);
4730 if (!TYPE_UNSIGNED (TREE_TYPE (val)))
4732 valn = double_int_negative_p (double_int_sext (valv, prec));
4733 cst2n = double_int_negative_p (double_int_sext (cst2v, prec));
4735 /* If CST2 doesn't have most significant bit set,
4736 but VAL is negative, we have comparison like
4737 if ((x & 0x123) > -4) (always true). Just give up. */
4738 if (!cst2n && valn)
4739 ccode = ERROR_MARK;
4740 if (cst2n)
4741 sgnbit = double_int_zext (double_int_lshift (double_int_one,
4742 prec - 1, prec,
4743 false), prec);
4744 else
4745 sgnbit = double_int_zero;
4746 minv = double_int_and (valv, cst2v);
4747 switch (ccode)
4749 case EQ_EXPR:
4750 /* Minimum unsigned value for equality is VAL & CST2
4751 (should be equal to VAL, otherwise we probably should
4752 have folded the comparison into false) and
4753 maximum unsigned value is VAL | ~CST2. */
4754 maxv = double_int_ior (valv, double_int_not (cst2v));
4755 maxv = double_int_zext (maxv, prec);
4756 valid_p = true;
4757 break;
4758 case NE_EXPR:
4759 tem = double_int_ior (valv, double_int_not (cst2v));
4760 tem = double_int_zext (tem, prec);
4761 /* If VAL is 0, handle (X & CST2) != 0 as (X & CST2) > 0U. */
4762 if (double_int_zero_p (valv))
4764 cst2n = false;
4765 sgnbit = double_int_zero;
4766 goto gt_expr;
4768 /* If (VAL | ~CST2) is all ones, handle it as
4769 (X & CST2) < VAL. */
4770 if (double_int_equal_p (tem, double_int_mask (prec)))
4772 cst2n = false;
4773 valn = false;
4774 sgnbit = double_int_zero;
4775 goto lt_expr;
4777 if (!cst2n
4778 && double_int_negative_p (double_int_sext (cst2v, prec)))
4779 sgnbit = double_int_zext (double_int_lshift (double_int_one,
4780 prec - 1, prec,
4781 false), prec);
4782 if (!double_int_zero_p (sgnbit))
4784 if (double_int_equal_p (valv, sgnbit))
4786 cst2n = true;
4787 valn = true;
4788 goto gt_expr;
4790 if (double_int_equal_p (tem, double_int_mask (prec - 1)))
4792 cst2n = true;
4793 goto lt_expr;
4795 if (!cst2n)
4796 sgnbit = double_int_zero;
4798 break;
4799 case GE_EXPR:
4800 /* Minimum unsigned value for >= if (VAL & CST2) == VAL
4801 is VAL and maximum unsigned value is ~0. For signed
4802 comparison, if CST2 doesn't have most significant bit
4803 set, handle it similarly. If CST2 has MSB set,
4804 the minimum is the same, and maximum is ~0U/2. */
4805 if (!double_int_equal_p (minv, valv))
4807 /* If (VAL & CST2) != VAL, X & CST2 can't be equal to
4808 VAL. */
4809 minv = masked_increment (valv, cst2v, sgnbit, prec);
4810 if (double_int_equal_p (minv, valv))
4811 break;
4813 maxv = double_int_mask (prec - (cst2n ? 1 : 0));
4814 valid_p = true;
4815 break;
4816 case GT_EXPR:
4817 gt_expr:
4818 /* Find out smallest MINV where MINV > VAL
4819 && (MINV & CST2) == MINV, if any. If VAL is signed and
4820 CST2 has MSB set, compute it biased by 1 << (prec - 1). */
4821 minv = masked_increment (valv, cst2v, sgnbit, prec);
4822 if (double_int_equal_p (minv, valv))
4823 break;
4824 maxv = double_int_mask (prec - (cst2n ? 1 : 0));
4825 valid_p = true;
4826 break;
4827 case LE_EXPR:
4828 /* Minimum unsigned value for <= is 0 and maximum
4829 unsigned value is VAL | ~CST2 if (VAL & CST2) == VAL.
4830 Otherwise, find smallest VAL2 where VAL2 > VAL
4831 && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
4832 as maximum.
4833 For signed comparison, if CST2 doesn't have most
4834 significant bit set, handle it similarly. If CST2 has
4835 MSB set, the maximum is the same and minimum is INT_MIN. */
4836 if (double_int_equal_p (minv, valv))
4837 maxv = valv;
4838 else
4840 maxv = masked_increment (valv, cst2v, sgnbit, prec);
4841 if (double_int_equal_p (maxv, valv))
4842 break;
4843 maxv = double_int_sub (maxv, double_int_one);
4845 maxv = double_int_ior (maxv, double_int_not (cst2v));
4846 maxv = double_int_zext (maxv, prec);
4847 minv = sgnbit;
4848 valid_p = true;
4849 break;
4850 case LT_EXPR:
4851 lt_expr:
4852 /* Minimum unsigned value for < is 0 and maximum
4853 unsigned value is (VAL-1) | ~CST2 if (VAL & CST2) == VAL.
4854 Otherwise, find smallest VAL2 where VAL2 > VAL
4855 && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
4856 as maximum.
4857 For signed comparison, if CST2 doesn't have most
4858 significant bit set, handle it similarly. If CST2 has
4859 MSB set, the maximum is the same and minimum is INT_MIN. */
4860 if (double_int_equal_p (minv, valv))
4862 if (double_int_equal_p (valv, sgnbit))
4863 break;
4864 maxv = valv;
4866 else
4868 maxv = masked_increment (valv, cst2v, sgnbit, prec);
4869 if (double_int_equal_p (maxv, valv))
4870 break;
4872 maxv = double_int_sub (maxv, double_int_one);
4873 maxv = double_int_ior (maxv, double_int_not (cst2v));
4874 maxv = double_int_zext (maxv, prec);
4875 minv = sgnbit;
4876 valid_p = true;
4877 break;
4878 default:
4879 break;
4881 if (valid_p
4882 && !double_int_equal_p (double_int_zext (double_int_sub (maxv,
4883 minv),
4884 prec),
4885 double_int_mask (prec)))
4887 tree tmp, new_val, type;
4888 int i;
4890 for (i = 0; i < 2; i++)
4891 if (names[i])
4893 double_int maxv2 = maxv;
4894 tmp = names[i];
4895 type = TREE_TYPE (names[i]);
4896 if (!TYPE_UNSIGNED (type))
4898 type = build_nonstandard_integer_type (prec, 1);
4899 tmp = build1 (NOP_EXPR, type, names[i]);
4901 if (!double_int_zero_p (minv))
4903 tmp = build2 (PLUS_EXPR, type, tmp,
4904 double_int_to_tree (type,
4905 double_int_neg (minv)));
4906 maxv2 = double_int_sub (maxv, minv);
4908 new_val = double_int_to_tree (type, maxv2);
4910 if (dump_file)
4912 fprintf (dump_file, "Adding assert for ");
4913 print_generic_expr (dump_file, names[i], 0);
4914 fprintf (dump_file, " from ");
4915 print_generic_expr (dump_file, tmp, 0);
4916 fprintf (dump_file, "\n");
4919 register_new_assert_for (names[i], tmp, LE_EXPR,
4920 new_val, NULL, e, bsi);
4921 retval = true;
4927 return retval;
4930 /* OP is an operand of a truth value expression which is known to have
4931 a particular value. Register any asserts for OP and for any
4932 operands in OP's defining statement.
4934 If CODE is EQ_EXPR, then we want to register OP is zero (false),
4935 if CODE is NE_EXPR, then we want to register OP is nonzero (true). */
4937 static bool
4938 register_edge_assert_for_1 (tree op, enum tree_code code,
4939 edge e, gimple_stmt_iterator bsi)
4941 bool retval = false;
4942 gimple op_def;
4943 tree val;
4944 enum tree_code rhs_code;
4946 /* We only care about SSA_NAMEs. */
4947 if (TREE_CODE (op) != SSA_NAME)
4948 return false;
4950 /* We know that OP will have a zero or nonzero value. If OP is used
4951 more than once go ahead and register an assert for OP.
4953 The FOUND_IN_SUBGRAPH support is not helpful in this situation as
4954 it will always be set for OP (because OP is used in a COND_EXPR in
4955 the subgraph). */
4956 if (!has_single_use (op))
4958 val = build_int_cst (TREE_TYPE (op), 0);
4959 register_new_assert_for (op, op, code, val, NULL, e, bsi);
4960 retval = true;
4963 /* Now look at how OP is set. If it's set from a comparison,
4964 a truth operation or some bit operations, then we may be able
4965 to register information about the operands of that assignment. */
4966 op_def = SSA_NAME_DEF_STMT (op);
4967 if (gimple_code (op_def) != GIMPLE_ASSIGN)
4968 return retval;
4970 rhs_code = gimple_assign_rhs_code (op_def);
4972 if (TREE_CODE_CLASS (rhs_code) == tcc_comparison)
4974 bool invert = (code == EQ_EXPR ? true : false);
4975 tree op0 = gimple_assign_rhs1 (op_def);
4976 tree op1 = gimple_assign_rhs2 (op_def);
4978 if (TREE_CODE (op0) == SSA_NAME)
4979 retval |= register_edge_assert_for_2 (op0, e, bsi, rhs_code, op0, op1,
4980 invert);
4981 if (TREE_CODE (op1) == SSA_NAME)
4982 retval |= register_edge_assert_for_2 (op1, e, bsi, rhs_code, op0, op1,
4983 invert);
4985 else if ((code == NE_EXPR
4986 && gimple_assign_rhs_code (op_def) == BIT_AND_EXPR)
4987 || (code == EQ_EXPR
4988 && gimple_assign_rhs_code (op_def) == BIT_IOR_EXPR))
4990 /* Recurse on each operand. */
4991 retval |= register_edge_assert_for_1 (gimple_assign_rhs1 (op_def),
4992 code, e, bsi);
4993 retval |= register_edge_assert_for_1 (gimple_assign_rhs2 (op_def),
4994 code, e, bsi);
4996 else if (gimple_assign_rhs_code (op_def) == BIT_NOT_EXPR
4997 && TYPE_PRECISION (TREE_TYPE (gimple_assign_lhs (op_def))) == 1)
4999 /* Recurse, flipping CODE. */
5000 code = invert_tree_comparison (code, false);
5001 retval |= register_edge_assert_for_1 (gimple_assign_rhs1 (op_def),
5002 code, e, bsi);
5004 else if (gimple_assign_rhs_code (op_def) == SSA_NAME)
5006 /* Recurse through the copy. */
5007 retval |= register_edge_assert_for_1 (gimple_assign_rhs1 (op_def),
5008 code, e, bsi);
5010 else if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (op_def)))
5012 /* Recurse through the type conversion. */
5013 retval |= register_edge_assert_for_1 (gimple_assign_rhs1 (op_def),
5014 code, e, bsi);
5017 return retval;
5020 /* Try to register an edge assertion for SSA name NAME on edge E for
5021 the condition COND contributing to the conditional jump pointed to by SI.
5022 Return true if an assertion for NAME could be registered. */
5024 static bool
5025 register_edge_assert_for (tree name, edge e, gimple_stmt_iterator si,
5026 enum tree_code cond_code, tree cond_op0,
5027 tree cond_op1)
5029 tree val;
5030 enum tree_code comp_code;
5031 bool retval = false;
5032 bool is_else_edge = (e->flags & EDGE_FALSE_VALUE) != 0;
5034 /* Do not attempt to infer anything in names that flow through
5035 abnormal edges. */
5036 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name))
5037 return false;
5039 if (!extract_code_and_val_from_cond_with_ops (name, cond_code,
5040 cond_op0, cond_op1,
5041 is_else_edge,
5042 &comp_code, &val))
5043 return false;
5045 /* Register ASSERT_EXPRs for name. */
5046 retval |= register_edge_assert_for_2 (name, e, si, cond_code, cond_op0,
5047 cond_op1, is_else_edge);
5050 /* If COND is effectively an equality test of an SSA_NAME against
5051 the value zero or one, then we may be able to assert values
5052 for SSA_NAMEs which flow into COND. */
5054 /* In the case of NAME == 1 or NAME != 0, for BIT_AND_EXPR defining
5055 statement of NAME we can assert both operands of the BIT_AND_EXPR
5056 have nonzero value. */
5057 if (((comp_code == EQ_EXPR && integer_onep (val))
5058 || (comp_code == NE_EXPR && integer_zerop (val))))
5060 gimple def_stmt = SSA_NAME_DEF_STMT (name);
5062 if (is_gimple_assign (def_stmt)
5063 && gimple_assign_rhs_code (def_stmt) == BIT_AND_EXPR)
5065 tree op0 = gimple_assign_rhs1 (def_stmt);
5066 tree op1 = gimple_assign_rhs2 (def_stmt);
5067 retval |= register_edge_assert_for_1 (op0, NE_EXPR, e, si);
5068 retval |= register_edge_assert_for_1 (op1, NE_EXPR, e, si);
5072 /* In the case of NAME == 0 or NAME != 1, for BIT_IOR_EXPR defining
5073 statement of NAME we can assert both operands of the BIT_IOR_EXPR
5074 have zero value. */
5075 if (((comp_code == EQ_EXPR && integer_zerop (val))
5076 || (comp_code == NE_EXPR && integer_onep (val))))
5078 gimple def_stmt = SSA_NAME_DEF_STMT (name);
5080 /* For BIT_IOR_EXPR only if NAME == 0 both operands have
5081 necessarily zero value, or if type-precision is one. */
5082 if (is_gimple_assign (def_stmt)
5083 && (gimple_assign_rhs_code (def_stmt) == BIT_IOR_EXPR
5084 && (TYPE_PRECISION (TREE_TYPE (name)) == 1
5085 || comp_code == EQ_EXPR)))
5087 tree op0 = gimple_assign_rhs1 (def_stmt);
5088 tree op1 = gimple_assign_rhs2 (def_stmt);
5089 retval |= register_edge_assert_for_1 (op0, EQ_EXPR, e, si);
5090 retval |= register_edge_assert_for_1 (op1, EQ_EXPR, e, si);
5094 return retval;
5098 /* Determine whether the outgoing edges of BB should receive an
5099 ASSERT_EXPR for each of the operands of BB's LAST statement.
5100 The last statement of BB must be a COND_EXPR.
5102 If any of the sub-graphs rooted at BB have an interesting use of
5103 the predicate operands, an assert location node is added to the
5104 list of assertions for the corresponding operands. */
5106 static bool
5107 find_conditional_asserts (basic_block bb, gimple last)
5109 bool need_assert;
5110 gimple_stmt_iterator bsi;
5111 tree op;
5112 edge_iterator ei;
5113 edge e;
5114 ssa_op_iter iter;
5116 need_assert = false;
5117 bsi = gsi_for_stmt (last);
5119 /* Look for uses of the operands in each of the sub-graphs
5120 rooted at BB. We need to check each of the outgoing edges
5121 separately, so that we know what kind of ASSERT_EXPR to
5122 insert. */
5123 FOR_EACH_EDGE (e, ei, bb->succs)
5125 if (e->dest == bb)
5126 continue;
5128 /* Register the necessary assertions for each operand in the
5129 conditional predicate. */
5130 FOR_EACH_SSA_TREE_OPERAND (op, last, iter, SSA_OP_USE)
5132 need_assert |= register_edge_assert_for (op, e, bsi,
5133 gimple_cond_code (last),
5134 gimple_cond_lhs (last),
5135 gimple_cond_rhs (last));
5139 return need_assert;
5142 struct case_info
5144 tree expr;
5145 basic_block bb;
5148 /* Compare two case labels sorting first by the destination bb index
5149 and then by the case value. */
5151 static int
5152 compare_case_labels (const void *p1, const void *p2)
5154 const struct case_info *ci1 = (const struct case_info *) p1;
5155 const struct case_info *ci2 = (const struct case_info *) p2;
5156 int idx1 = ci1->bb->index;
5157 int idx2 = ci2->bb->index;
5159 if (idx1 < idx2)
5160 return -1;
5161 else if (idx1 == idx2)
5163 /* Make sure the default label is first in a group. */
5164 if (!CASE_LOW (ci1->expr))
5165 return -1;
5166 else if (!CASE_LOW (ci2->expr))
5167 return 1;
5168 else
5169 return tree_int_cst_compare (CASE_LOW (ci1->expr),
5170 CASE_LOW (ci2->expr));
5172 else
5173 return 1;
5176 /* Determine whether the outgoing edges of BB should receive an
5177 ASSERT_EXPR for each of the operands of BB's LAST statement.
5178 The last statement of BB must be a SWITCH_EXPR.
5180 If any of the sub-graphs rooted at BB have an interesting use of
5181 the predicate operands, an assert location node is added to the
5182 list of assertions for the corresponding operands. */
5184 static bool
5185 find_switch_asserts (basic_block bb, gimple last)
5187 bool need_assert;
5188 gimple_stmt_iterator bsi;
5189 tree op;
5190 edge e;
5191 struct case_info *ci;
5192 size_t n = gimple_switch_num_labels (last);
5193 #if GCC_VERSION >= 4000
5194 unsigned int idx;
5195 #else
5196 /* Work around GCC 3.4 bug (PR 37086). */
5197 volatile unsigned int idx;
5198 #endif
5200 need_assert = false;
5201 bsi = gsi_for_stmt (last);
5202 op = gimple_switch_index (last);
5203 if (TREE_CODE (op) != SSA_NAME)
5204 return false;
5206 /* Build a vector of case labels sorted by destination label. */
5207 ci = XNEWVEC (struct case_info, n);
5208 for (idx = 0; idx < n; ++idx)
5210 ci[idx].expr = gimple_switch_label (last, idx);
5211 ci[idx].bb = label_to_block (CASE_LABEL (ci[idx].expr));
5213 qsort (ci, n, sizeof (struct case_info), compare_case_labels);
5215 for (idx = 0; idx < n; ++idx)
5217 tree min, max;
5218 tree cl = ci[idx].expr;
5219 basic_block cbb = ci[idx].bb;
5221 min = CASE_LOW (cl);
5222 max = CASE_HIGH (cl);
5224 /* If there are multiple case labels with the same destination
5225 we need to combine them to a single value range for the edge. */
5226 if (idx + 1 < n && cbb == ci[idx + 1].bb)
5228 /* Skip labels until the last of the group. */
5229 do {
5230 ++idx;
5231 } while (idx < n && cbb == ci[idx].bb);
5232 --idx;
5234 /* Pick up the maximum of the case label range. */
5235 if (CASE_HIGH (ci[idx].expr))
5236 max = CASE_HIGH (ci[idx].expr);
5237 else
5238 max = CASE_LOW (ci[idx].expr);
5241 /* Nothing to do if the range includes the default label until we
5242 can register anti-ranges. */
5243 if (min == NULL_TREE)
5244 continue;
5246 /* Find the edge to register the assert expr on. */
5247 e = find_edge (bb, cbb);
5249 /* Register the necessary assertions for the operand in the
5250 SWITCH_EXPR. */
5251 need_assert |= register_edge_assert_for (op, e, bsi,
5252 max ? GE_EXPR : EQ_EXPR,
5254 fold_convert (TREE_TYPE (op),
5255 min));
5256 if (max)
5258 need_assert |= register_edge_assert_for (op, e, bsi, LE_EXPR,
5260 fold_convert (TREE_TYPE (op),
5261 max));
5265 XDELETEVEC (ci);
5266 return need_assert;
5270 /* Traverse all the statements in block BB looking for statements that
5271 may generate useful assertions for the SSA names in their operand.
5272 If a statement produces a useful assertion A for name N_i, then the
5273 list of assertions already generated for N_i is scanned to
5274 determine if A is actually needed.
5276 If N_i already had the assertion A at a location dominating the
5277 current location, then nothing needs to be done. Otherwise, the
5278 new location for A is recorded instead.
5280 1- For every statement S in BB, all the variables used by S are
5281 added to bitmap FOUND_IN_SUBGRAPH.
5283 2- If statement S uses an operand N in a way that exposes a known
5284 value range for N, then if N was not already generated by an
5285 ASSERT_EXPR, create a new assert location for N. For instance,
5286 if N is a pointer and the statement dereferences it, we can
5287 assume that N is not NULL.
5289 3- COND_EXPRs are a special case of #2. We can derive range
5290 information from the predicate but need to insert different
5291 ASSERT_EXPRs for each of the sub-graphs rooted at the
5292 conditional block. If the last statement of BB is a conditional
5293 expression of the form 'X op Y', then
5295 a) Remove X and Y from the set FOUND_IN_SUBGRAPH.
5297 b) If the conditional is the only entry point to the sub-graph
5298 corresponding to the THEN_CLAUSE, recurse into it. On
5299 return, if X and/or Y are marked in FOUND_IN_SUBGRAPH, then
5300 an ASSERT_EXPR is added for the corresponding variable.
5302 c) Repeat step (b) on the ELSE_CLAUSE.
5304 d) Mark X and Y in FOUND_IN_SUBGRAPH.
5306 For instance,
5308 if (a == 9)
5309 b = a;
5310 else
5311 b = c + 1;
5313 In this case, an assertion on the THEN clause is useful to
5314 determine that 'a' is always 9 on that edge. However, an assertion
5315 on the ELSE clause would be unnecessary.
5317 4- If BB does not end in a conditional expression, then we recurse
5318 into BB's dominator children.
5320 At the end of the recursive traversal, every SSA name will have a
5321 list of locations where ASSERT_EXPRs should be added. When a new
5322 location for name N is found, it is registered by calling
5323 register_new_assert_for. That function keeps track of all the
5324 registered assertions to prevent adding unnecessary assertions.
5325 For instance, if a pointer P_4 is dereferenced more than once in a
5326 dominator tree, only the location dominating all the dereference of
5327 P_4 will receive an ASSERT_EXPR.
5329 If this function returns true, then it means that there are names
5330 for which we need to generate ASSERT_EXPRs. Those assertions are
5331 inserted by process_assert_insertions. */
5333 static bool
5334 find_assert_locations_1 (basic_block bb, sbitmap live)
5336 gimple_stmt_iterator si;
5337 gimple last;
5338 gimple phi;
5339 bool need_assert;
5341 need_assert = false;
5342 last = last_stmt (bb);
5344 /* If BB's last statement is a conditional statement involving integer
5345 operands, determine if we need to add ASSERT_EXPRs. */
5346 if (last
5347 && gimple_code (last) == GIMPLE_COND
5348 && !fp_predicate (last)
5349 && !ZERO_SSA_OPERANDS (last, SSA_OP_USE))
5350 need_assert |= find_conditional_asserts (bb, last);
5352 /* If BB's last statement is a switch statement involving integer
5353 operands, determine if we need to add ASSERT_EXPRs. */
5354 if (last
5355 && gimple_code (last) == GIMPLE_SWITCH
5356 && !ZERO_SSA_OPERANDS (last, SSA_OP_USE))
5357 need_assert |= find_switch_asserts (bb, last);
5359 /* Traverse all the statements in BB marking used names and looking
5360 for statements that may infer assertions for their used operands. */
5361 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
5363 gimple stmt;
5364 tree op;
5365 ssa_op_iter i;
5367 stmt = gsi_stmt (si);
5369 if (is_gimple_debug (stmt))
5370 continue;
5372 /* See if we can derive an assertion for any of STMT's operands. */
5373 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_USE)
5375 tree value;
5376 enum tree_code comp_code;
5378 /* Mark OP in our live bitmap. */
5379 SET_BIT (live, SSA_NAME_VERSION (op));
5381 /* If OP is used in such a way that we can infer a value
5382 range for it, and we don't find a previous assertion for
5383 it, create a new assertion location node for OP. */
5384 if (infer_value_range (stmt, op, &comp_code, &value))
5386 /* If we are able to infer a nonzero value range for OP,
5387 then walk backwards through the use-def chain to see if OP
5388 was set via a typecast.
5390 If so, then we can also infer a nonzero value range
5391 for the operand of the NOP_EXPR. */
5392 if (comp_code == NE_EXPR && integer_zerop (value))
5394 tree t = op;
5395 gimple def_stmt = SSA_NAME_DEF_STMT (t);
5397 while (is_gimple_assign (def_stmt)
5398 && gimple_assign_rhs_code (def_stmt) == NOP_EXPR
5399 && TREE_CODE
5400 (gimple_assign_rhs1 (def_stmt)) == SSA_NAME
5401 && POINTER_TYPE_P
5402 (TREE_TYPE (gimple_assign_rhs1 (def_stmt))))
5404 t = gimple_assign_rhs1 (def_stmt);
5405 def_stmt = SSA_NAME_DEF_STMT (t);
5407 /* Note we want to register the assert for the
5408 operand of the NOP_EXPR after SI, not after the
5409 conversion. */
5410 if (! has_single_use (t))
5412 register_new_assert_for (t, t, comp_code, value,
5413 bb, NULL, si);
5414 need_assert = true;
5419 /* If OP is used only once, namely in this STMT, don't
5420 bother creating an ASSERT_EXPR for it. Such an
5421 ASSERT_EXPR would do nothing but increase compile time. */
5422 if (!has_single_use (op))
5424 register_new_assert_for (op, op, comp_code, value,
5425 bb, NULL, si);
5426 need_assert = true;
5432 /* Traverse all PHI nodes in BB marking used operands. */
5433 for (si = gsi_start_phis (bb); !gsi_end_p(si); gsi_next (&si))
5435 use_operand_p arg_p;
5436 ssa_op_iter i;
5437 phi = gsi_stmt (si);
5439 FOR_EACH_PHI_ARG (arg_p, phi, i, SSA_OP_USE)
5441 tree arg = USE_FROM_PTR (arg_p);
5442 if (TREE_CODE (arg) == SSA_NAME)
5443 SET_BIT (live, SSA_NAME_VERSION (arg));
5447 return need_assert;
5450 /* Do an RPO walk over the function computing SSA name liveness
5451 on-the-fly and deciding on assert expressions to insert.
5452 Returns true if there are assert expressions to be inserted. */
5454 static bool
5455 find_assert_locations (void)
5457 int *rpo = XCNEWVEC (int, last_basic_block + NUM_FIXED_BLOCKS);
5458 int *bb_rpo = XCNEWVEC (int, last_basic_block + NUM_FIXED_BLOCKS);
5459 int *last_rpo = XCNEWVEC (int, last_basic_block + NUM_FIXED_BLOCKS);
5460 int rpo_cnt, i;
5461 bool need_asserts;
5463 live = XCNEWVEC (sbitmap, last_basic_block + NUM_FIXED_BLOCKS);
5464 rpo_cnt = pre_and_rev_post_order_compute (NULL, rpo, false);
5465 for (i = 0; i < rpo_cnt; ++i)
5466 bb_rpo[rpo[i]] = i;
5468 need_asserts = false;
5469 for (i = rpo_cnt-1; i >= 0; --i)
5471 basic_block bb = BASIC_BLOCK (rpo[i]);
5472 edge e;
5473 edge_iterator ei;
5475 if (!live[rpo[i]])
5477 live[rpo[i]] = sbitmap_alloc (num_ssa_names);
5478 sbitmap_zero (live[rpo[i]]);
5481 /* Process BB and update the live information with uses in
5482 this block. */
5483 need_asserts |= find_assert_locations_1 (bb, live[rpo[i]]);
5485 /* Merge liveness into the predecessor blocks and free it. */
5486 if (!sbitmap_empty_p (live[rpo[i]]))
5488 int pred_rpo = i;
5489 FOR_EACH_EDGE (e, ei, bb->preds)
5491 int pred = e->src->index;
5492 if (e->flags & EDGE_DFS_BACK)
5493 continue;
5495 if (!live[pred])
5497 live[pred] = sbitmap_alloc (num_ssa_names);
5498 sbitmap_zero (live[pred]);
5500 sbitmap_a_or_b (live[pred], live[pred], live[rpo[i]]);
5502 if (bb_rpo[pred] < pred_rpo)
5503 pred_rpo = bb_rpo[pred];
5506 /* Record the RPO number of the last visited block that needs
5507 live information from this block. */
5508 last_rpo[rpo[i]] = pred_rpo;
5510 else
5512 sbitmap_free (live[rpo[i]]);
5513 live[rpo[i]] = NULL;
5516 /* We can free all successors live bitmaps if all their
5517 predecessors have been visited already. */
5518 FOR_EACH_EDGE (e, ei, bb->succs)
5519 if (last_rpo[e->dest->index] == i
5520 && live[e->dest->index])
5522 sbitmap_free (live[e->dest->index]);
5523 live[e->dest->index] = NULL;
5527 XDELETEVEC (rpo);
5528 XDELETEVEC (bb_rpo);
5529 XDELETEVEC (last_rpo);
5530 for (i = 0; i < last_basic_block + NUM_FIXED_BLOCKS; ++i)
5531 if (live[i])
5532 sbitmap_free (live[i]);
5533 XDELETEVEC (live);
5535 return need_asserts;
5538 /* Create an ASSERT_EXPR for NAME and insert it in the location
5539 indicated by LOC. Return true if we made any edge insertions. */
5541 static bool
5542 process_assert_insertions_for (tree name, assert_locus_t loc)
5544 /* Build the comparison expression NAME_i COMP_CODE VAL. */
5545 gimple stmt;
5546 tree cond;
5547 gimple assert_stmt;
5548 edge_iterator ei;
5549 edge e;
5551 /* If we have X <=> X do not insert an assert expr for that. */
5552 if (loc->expr == loc->val)
5553 return false;
5555 cond = build2 (loc->comp_code, boolean_type_node, loc->expr, loc->val);
5556 assert_stmt = build_assert_expr_for (cond, name);
5557 if (loc->e)
5559 /* We have been asked to insert the assertion on an edge. This
5560 is used only by COND_EXPR and SWITCH_EXPR assertions. */
5561 gcc_checking_assert (gimple_code (gsi_stmt (loc->si)) == GIMPLE_COND
5562 || (gimple_code (gsi_stmt (loc->si))
5563 == GIMPLE_SWITCH));
5565 gsi_insert_on_edge (loc->e, assert_stmt);
5566 return true;
5569 /* Otherwise, we can insert right after LOC->SI iff the
5570 statement must not be the last statement in the block. */
5571 stmt = gsi_stmt (loc->si);
5572 if (!stmt_ends_bb_p (stmt))
5574 gsi_insert_after (&loc->si, assert_stmt, GSI_SAME_STMT);
5575 return false;
5578 /* If STMT must be the last statement in BB, we can only insert new
5579 assertions on the non-abnormal edge out of BB. Note that since
5580 STMT is not control flow, there may only be one non-abnormal edge
5581 out of BB. */
5582 FOR_EACH_EDGE (e, ei, loc->bb->succs)
5583 if (!(e->flags & EDGE_ABNORMAL))
5585 gsi_insert_on_edge (e, assert_stmt);
5586 return true;
5589 gcc_unreachable ();
5593 /* Process all the insertions registered for every name N_i registered
5594 in NEED_ASSERT_FOR. The list of assertions to be inserted are
5595 found in ASSERTS_FOR[i]. */
5597 static void
5598 process_assert_insertions (void)
5600 unsigned i;
5601 bitmap_iterator bi;
5602 bool update_edges_p = false;
5603 int num_asserts = 0;
5605 if (dump_file && (dump_flags & TDF_DETAILS))
5606 dump_all_asserts (dump_file);
5608 EXECUTE_IF_SET_IN_BITMAP (need_assert_for, 0, i, bi)
5610 assert_locus_t loc = asserts_for[i];
5611 gcc_assert (loc);
5613 while (loc)
5615 assert_locus_t next = loc->next;
5616 update_edges_p |= process_assert_insertions_for (ssa_name (i), loc);
5617 free (loc);
5618 loc = next;
5619 num_asserts++;
5623 if (update_edges_p)
5624 gsi_commit_edge_inserts ();
5626 statistics_counter_event (cfun, "Number of ASSERT_EXPR expressions inserted",
5627 num_asserts);
5631 /* Traverse the flowgraph looking for conditional jumps to insert range
5632 expressions. These range expressions are meant to provide information
5633 to optimizations that need to reason in terms of value ranges. They
5634 will not be expanded into RTL. For instance, given:
5636 x = ...
5637 y = ...
5638 if (x < y)
5639 y = x - 2;
5640 else
5641 x = y + 3;
5643 this pass will transform the code into:
5645 x = ...
5646 y = ...
5647 if (x < y)
5649 x = ASSERT_EXPR <x, x < y>
5650 y = x - 2
5652 else
5654 y = ASSERT_EXPR <y, x <= y>
5655 x = y + 3
5658 The idea is that once copy and constant propagation have run, other
5659 optimizations will be able to determine what ranges of values can 'x'
5660 take in different paths of the code, simply by checking the reaching
5661 definition of 'x'. */
5663 static void
5664 insert_range_assertions (void)
5666 need_assert_for = BITMAP_ALLOC (NULL);
5667 asserts_for = XCNEWVEC (assert_locus_t, num_ssa_names);
5669 calculate_dominance_info (CDI_DOMINATORS);
5671 if (find_assert_locations ())
5673 process_assert_insertions ();
5674 update_ssa (TODO_update_ssa_no_phi);
5677 if (dump_file && (dump_flags & TDF_DETAILS))
5679 fprintf (dump_file, "\nSSA form after inserting ASSERT_EXPRs\n");
5680 dump_function_to_file (current_function_decl, dump_file, dump_flags);
5683 free (asserts_for);
5684 BITMAP_FREE (need_assert_for);
5687 /* Checks one ARRAY_REF in REF, located at LOCUS. Ignores flexible arrays
5688 and "struct" hacks. If VRP can determine that the
5689 array subscript is a constant, check if it is outside valid
5690 range. If the array subscript is a RANGE, warn if it is
5691 non-overlapping with valid range.
5692 IGNORE_OFF_BY_ONE is true if the ARRAY_REF is inside a ADDR_EXPR. */
5694 static void
5695 check_array_ref (location_t location, tree ref, bool ignore_off_by_one)
5697 value_range_t* vr = NULL;
5698 tree low_sub, up_sub;
5699 tree low_bound, up_bound, up_bound_p1;
5700 tree base;
5702 if (TREE_NO_WARNING (ref))
5703 return;
5705 low_sub = up_sub = TREE_OPERAND (ref, 1);
5706 up_bound = array_ref_up_bound (ref);
5708 /* Can not check flexible arrays. */
5709 if (!up_bound
5710 || TREE_CODE (up_bound) != INTEGER_CST)
5711 return;
5713 /* Accesses to trailing arrays via pointers may access storage
5714 beyond the types array bounds. */
5715 base = get_base_address (ref);
5716 if (base && TREE_CODE (base) == MEM_REF)
5718 tree cref, next = NULL_TREE;
5720 if (TREE_CODE (TREE_OPERAND (ref, 0)) != COMPONENT_REF)
5721 return;
5723 cref = TREE_OPERAND (ref, 0);
5724 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (cref, 0))) == RECORD_TYPE)
5725 for (next = DECL_CHAIN (TREE_OPERAND (cref, 1));
5726 next && TREE_CODE (next) != FIELD_DECL;
5727 next = DECL_CHAIN (next))
5730 /* If this is the last field in a struct type or a field in a
5731 union type do not warn. */
5732 if (!next)
5733 return;
5736 low_bound = array_ref_low_bound (ref);
5737 up_bound_p1 = int_const_binop (PLUS_EXPR, up_bound, integer_one_node);
5739 if (TREE_CODE (low_sub) == SSA_NAME)
5741 vr = get_value_range (low_sub);
5742 if (vr->type == VR_RANGE || vr->type == VR_ANTI_RANGE)
5744 low_sub = vr->type == VR_RANGE ? vr->max : vr->min;
5745 up_sub = vr->type == VR_RANGE ? vr->min : vr->max;
5749 if (vr && vr->type == VR_ANTI_RANGE)
5751 if (TREE_CODE (up_sub) == INTEGER_CST
5752 && tree_int_cst_lt (up_bound, up_sub)
5753 && TREE_CODE (low_sub) == INTEGER_CST
5754 && tree_int_cst_lt (low_sub, low_bound))
5756 warning_at (location, OPT_Warray_bounds,
5757 "array subscript is outside array bounds");
5758 TREE_NO_WARNING (ref) = 1;
5761 else if (TREE_CODE (up_sub) == INTEGER_CST
5762 && (ignore_off_by_one
5763 ? (tree_int_cst_lt (up_bound, up_sub)
5764 && !tree_int_cst_equal (up_bound_p1, up_sub))
5765 : (tree_int_cst_lt (up_bound, up_sub)
5766 || tree_int_cst_equal (up_bound_p1, up_sub))))
5768 warning_at (location, OPT_Warray_bounds,
5769 "array subscript is above array bounds");
5770 TREE_NO_WARNING (ref) = 1;
5772 else if (TREE_CODE (low_sub) == INTEGER_CST
5773 && tree_int_cst_lt (low_sub, low_bound))
5775 warning_at (location, OPT_Warray_bounds,
5776 "array subscript is below array bounds");
5777 TREE_NO_WARNING (ref) = 1;
5781 /* Searches if the expr T, located at LOCATION computes
5782 address of an ARRAY_REF, and call check_array_ref on it. */
5784 static void
5785 search_for_addr_array (tree t, location_t location)
5787 while (TREE_CODE (t) == SSA_NAME)
5789 gimple g = SSA_NAME_DEF_STMT (t);
5791 if (gimple_code (g) != GIMPLE_ASSIGN)
5792 return;
5794 if (get_gimple_rhs_class (gimple_assign_rhs_code (g))
5795 != GIMPLE_SINGLE_RHS)
5796 return;
5798 t = gimple_assign_rhs1 (g);
5802 /* We are only interested in addresses of ARRAY_REF's. */
5803 if (TREE_CODE (t) != ADDR_EXPR)
5804 return;
5806 /* Check each ARRAY_REFs in the reference chain. */
5809 if (TREE_CODE (t) == ARRAY_REF)
5810 check_array_ref (location, t, true /*ignore_off_by_one*/);
5812 t = TREE_OPERAND (t, 0);
5814 while (handled_component_p (t));
5816 if (TREE_CODE (t) == MEM_REF
5817 && TREE_CODE (TREE_OPERAND (t, 0)) == ADDR_EXPR
5818 && !TREE_NO_WARNING (t))
5820 tree tem = TREE_OPERAND (TREE_OPERAND (t, 0), 0);
5821 tree low_bound, up_bound, el_sz;
5822 double_int idx;
5823 if (TREE_CODE (TREE_TYPE (tem)) != ARRAY_TYPE
5824 || TREE_CODE (TREE_TYPE (TREE_TYPE (tem))) == ARRAY_TYPE
5825 || !TYPE_DOMAIN (TREE_TYPE (tem)))
5826 return;
5828 low_bound = TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (tem)));
5829 up_bound = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (tem)));
5830 el_sz = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (tem)));
5831 if (!low_bound
5832 || TREE_CODE (low_bound) != INTEGER_CST
5833 || !up_bound
5834 || TREE_CODE (up_bound) != INTEGER_CST
5835 || !el_sz
5836 || TREE_CODE (el_sz) != INTEGER_CST)
5837 return;
5839 idx = mem_ref_offset (t);
5840 idx = double_int_sdiv (idx, tree_to_double_int (el_sz), TRUNC_DIV_EXPR);
5841 if (double_int_scmp (idx, double_int_zero) < 0)
5843 warning_at (location, OPT_Warray_bounds,
5844 "array subscript is below array bounds");
5845 TREE_NO_WARNING (t) = 1;
5847 else if (double_int_scmp (idx,
5848 double_int_add
5849 (double_int_add
5850 (tree_to_double_int (up_bound),
5851 double_int_neg
5852 (tree_to_double_int (low_bound))),
5853 double_int_one)) > 0)
5855 warning_at (location, OPT_Warray_bounds,
5856 "array subscript is above array bounds");
5857 TREE_NO_WARNING (t) = 1;
5862 /* walk_tree() callback that checks if *TP is
5863 an ARRAY_REF inside an ADDR_EXPR (in which an array
5864 subscript one outside the valid range is allowed). Call
5865 check_array_ref for each ARRAY_REF found. The location is
5866 passed in DATA. */
5868 static tree
5869 check_array_bounds (tree *tp, int *walk_subtree, void *data)
5871 tree t = *tp;
5872 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
5873 location_t location;
5875 if (EXPR_HAS_LOCATION (t))
5876 location = EXPR_LOCATION (t);
5877 else
5879 location_t *locp = (location_t *) wi->info;
5880 location = *locp;
5883 *walk_subtree = TRUE;
5885 if (TREE_CODE (t) == ARRAY_REF)
5886 check_array_ref (location, t, false /*ignore_off_by_one*/);
5888 if (TREE_CODE (t) == MEM_REF
5889 || (TREE_CODE (t) == RETURN_EXPR && TREE_OPERAND (t, 0)))
5890 search_for_addr_array (TREE_OPERAND (t, 0), location);
5892 if (TREE_CODE (t) == ADDR_EXPR)
5893 *walk_subtree = FALSE;
5895 return NULL_TREE;
5898 /* Walk over all statements of all reachable BBs and call check_array_bounds
5899 on them. */
5901 static void
5902 check_all_array_refs (void)
5904 basic_block bb;
5905 gimple_stmt_iterator si;
5907 FOR_EACH_BB (bb)
5909 edge_iterator ei;
5910 edge e;
5911 bool executable = false;
5913 /* Skip blocks that were found to be unreachable. */
5914 FOR_EACH_EDGE (e, ei, bb->preds)
5915 executable |= !!(e->flags & EDGE_EXECUTABLE);
5916 if (!executable)
5917 continue;
5919 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
5921 gimple stmt = gsi_stmt (si);
5922 struct walk_stmt_info wi;
5923 if (!gimple_has_location (stmt))
5924 continue;
5926 if (is_gimple_call (stmt))
5928 size_t i;
5929 size_t n = gimple_call_num_args (stmt);
5930 for (i = 0; i < n; i++)
5932 tree arg = gimple_call_arg (stmt, i);
5933 search_for_addr_array (arg, gimple_location (stmt));
5936 else
5938 memset (&wi, 0, sizeof (wi));
5939 wi.info = CONST_CAST (void *, (const void *)
5940 gimple_location_ptr (stmt));
5942 walk_gimple_op (gsi_stmt (si),
5943 check_array_bounds,
5944 &wi);
5950 /* Convert range assertion expressions into the implied copies and
5951 copy propagate away the copies. Doing the trivial copy propagation
5952 here avoids the need to run the full copy propagation pass after
5953 VRP.
5955 FIXME, this will eventually lead to copy propagation removing the
5956 names that had useful range information attached to them. For
5957 instance, if we had the assertion N_i = ASSERT_EXPR <N_j, N_j > 3>,
5958 then N_i will have the range [3, +INF].
5960 However, by converting the assertion into the implied copy
5961 operation N_i = N_j, we will then copy-propagate N_j into the uses
5962 of N_i and lose the range information. We may want to hold on to
5963 ASSERT_EXPRs a little while longer as the ranges could be used in
5964 things like jump threading.
5966 The problem with keeping ASSERT_EXPRs around is that passes after
5967 VRP need to handle them appropriately.
5969 Another approach would be to make the range information a first
5970 class property of the SSA_NAME so that it can be queried from
5971 any pass. This is made somewhat more complex by the need for
5972 multiple ranges to be associated with one SSA_NAME. */
5974 static void
5975 remove_range_assertions (void)
5977 basic_block bb;
5978 gimple_stmt_iterator si;
5980 /* Note that the BSI iterator bump happens at the bottom of the
5981 loop and no bump is necessary if we're removing the statement
5982 referenced by the current BSI. */
5983 FOR_EACH_BB (bb)
5984 for (si = gsi_start_bb (bb); !gsi_end_p (si);)
5986 gimple stmt = gsi_stmt (si);
5987 gimple use_stmt;
5989 if (is_gimple_assign (stmt)
5990 && gimple_assign_rhs_code (stmt) == ASSERT_EXPR)
5992 tree rhs = gimple_assign_rhs1 (stmt);
5993 tree var;
5994 tree cond = fold (ASSERT_EXPR_COND (rhs));
5995 use_operand_p use_p;
5996 imm_use_iterator iter;
5998 gcc_assert (cond != boolean_false_node);
6000 /* Propagate the RHS into every use of the LHS. */
6001 var = ASSERT_EXPR_VAR (rhs);
6002 FOR_EACH_IMM_USE_STMT (use_stmt, iter,
6003 gimple_assign_lhs (stmt))
6004 FOR_EACH_IMM_USE_ON_STMT (use_p, iter)
6006 SET_USE (use_p, var);
6007 gcc_assert (TREE_CODE (var) == SSA_NAME);
6010 /* And finally, remove the copy, it is not needed. */
6011 gsi_remove (&si, true);
6012 release_defs (stmt);
6014 else
6015 gsi_next (&si);
6020 /* Return true if STMT is interesting for VRP. */
6022 static bool
6023 stmt_interesting_for_vrp (gimple stmt)
6025 if (gimple_code (stmt) == GIMPLE_PHI
6026 && is_gimple_reg (gimple_phi_result (stmt))
6027 && (INTEGRAL_TYPE_P (TREE_TYPE (gimple_phi_result (stmt)))
6028 || POINTER_TYPE_P (TREE_TYPE (gimple_phi_result (stmt)))))
6029 return true;
6030 else if (is_gimple_assign (stmt) || is_gimple_call (stmt))
6032 tree lhs = gimple_get_lhs (stmt);
6034 /* In general, assignments with virtual operands are not useful
6035 for deriving ranges, with the obvious exception of calls to
6036 builtin functions. */
6037 if (lhs && TREE_CODE (lhs) == SSA_NAME
6038 && (INTEGRAL_TYPE_P (TREE_TYPE (lhs))
6039 || POINTER_TYPE_P (TREE_TYPE (lhs)))
6040 && ((is_gimple_call (stmt)
6041 && gimple_call_fndecl (stmt) != NULL_TREE
6042 && DECL_BUILT_IN (gimple_call_fndecl (stmt)))
6043 || !gimple_vuse (stmt)))
6044 return true;
6046 else if (gimple_code (stmt) == GIMPLE_COND
6047 || gimple_code (stmt) == GIMPLE_SWITCH)
6048 return true;
6050 return false;
6054 /* Initialize local data structures for VRP. */
6056 static void
6057 vrp_initialize (void)
6059 basic_block bb;
6061 values_propagated = false;
6062 num_vr_values = num_ssa_names;
6063 vr_value = XCNEWVEC (value_range_t *, num_vr_values);
6064 vr_phi_edge_counts = XCNEWVEC (int, num_ssa_names);
6066 FOR_EACH_BB (bb)
6068 gimple_stmt_iterator si;
6070 for (si = gsi_start_phis (bb); !gsi_end_p (si); gsi_next (&si))
6072 gimple phi = gsi_stmt (si);
6073 if (!stmt_interesting_for_vrp (phi))
6075 tree lhs = PHI_RESULT (phi);
6076 set_value_range_to_varying (get_value_range (lhs));
6077 prop_set_simulate_again (phi, false);
6079 else
6080 prop_set_simulate_again (phi, true);
6083 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
6085 gimple stmt = gsi_stmt (si);
6087 /* If the statement is a control insn, then we do not
6088 want to avoid simulating the statement once. Failure
6089 to do so means that those edges will never get added. */
6090 if (stmt_ends_bb_p (stmt))
6091 prop_set_simulate_again (stmt, true);
6092 else if (!stmt_interesting_for_vrp (stmt))
6094 ssa_op_iter i;
6095 tree def;
6096 FOR_EACH_SSA_TREE_OPERAND (def, stmt, i, SSA_OP_DEF)
6097 set_value_range_to_varying (get_value_range (def));
6098 prop_set_simulate_again (stmt, false);
6100 else
6101 prop_set_simulate_again (stmt, true);
6106 /* Return the singleton value-range for NAME or NAME. */
6108 static inline tree
6109 vrp_valueize (tree name)
6111 if (TREE_CODE (name) == SSA_NAME)
6113 value_range_t *vr = get_value_range (name);
6114 if (vr->type == VR_RANGE
6115 && (vr->min == vr->max
6116 || operand_equal_p (vr->min, vr->max, 0)))
6117 return vr->min;
6119 return name;
6122 /* Visit assignment STMT. If it produces an interesting range, record
6123 the SSA name in *OUTPUT_P. */
6125 static enum ssa_prop_result
6126 vrp_visit_assignment_or_call (gimple stmt, tree *output_p)
6128 tree def, lhs;
6129 ssa_op_iter iter;
6130 enum gimple_code code = gimple_code (stmt);
6131 lhs = gimple_get_lhs (stmt);
6133 /* We only keep track of ranges in integral and pointer types. */
6134 if (TREE_CODE (lhs) == SSA_NAME
6135 && ((INTEGRAL_TYPE_P (TREE_TYPE (lhs))
6136 /* It is valid to have NULL MIN/MAX values on a type. See
6137 build_range_type. */
6138 && TYPE_MIN_VALUE (TREE_TYPE (lhs))
6139 && TYPE_MAX_VALUE (TREE_TYPE (lhs)))
6140 || POINTER_TYPE_P (TREE_TYPE (lhs))))
6142 value_range_t new_vr = VR_INITIALIZER;
6144 /* Try folding the statement to a constant first. */
6145 tree tem = gimple_fold_stmt_to_constant (stmt, vrp_valueize);
6146 if (tem && !is_overflow_infinity (tem))
6147 set_value_range (&new_vr, VR_RANGE, tem, tem, NULL);
6148 /* Then dispatch to value-range extracting functions. */
6149 else if (code == GIMPLE_CALL)
6150 extract_range_basic (&new_vr, stmt);
6151 else
6152 extract_range_from_assignment (&new_vr, stmt);
6154 if (update_value_range (lhs, &new_vr))
6156 *output_p = lhs;
6158 if (dump_file && (dump_flags & TDF_DETAILS))
6160 fprintf (dump_file, "Found new range for ");
6161 print_generic_expr (dump_file, lhs, 0);
6162 fprintf (dump_file, ": ");
6163 dump_value_range (dump_file, &new_vr);
6164 fprintf (dump_file, "\n\n");
6167 if (new_vr.type == VR_VARYING)
6168 return SSA_PROP_VARYING;
6170 return SSA_PROP_INTERESTING;
6173 return SSA_PROP_NOT_INTERESTING;
6176 /* Every other statement produces no useful ranges. */
6177 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_DEF)
6178 set_value_range_to_varying (get_value_range (def));
6180 return SSA_PROP_VARYING;
6183 /* Helper that gets the value range of the SSA_NAME with version I
6184 or a symbolic range containing the SSA_NAME only if the value range
6185 is varying or undefined. */
6187 static inline value_range_t
6188 get_vr_for_comparison (int i)
6190 value_range_t vr = *get_value_range (ssa_name (i));
6192 /* If name N_i does not have a valid range, use N_i as its own
6193 range. This allows us to compare against names that may
6194 have N_i in their ranges. */
6195 if (vr.type == VR_VARYING || vr.type == VR_UNDEFINED)
6197 vr.type = VR_RANGE;
6198 vr.min = ssa_name (i);
6199 vr.max = ssa_name (i);
6202 return vr;
6205 /* Compare all the value ranges for names equivalent to VAR with VAL
6206 using comparison code COMP. Return the same value returned by
6207 compare_range_with_value, including the setting of
6208 *STRICT_OVERFLOW_P. */
6210 static tree
6211 compare_name_with_value (enum tree_code comp, tree var, tree val,
6212 bool *strict_overflow_p)
6214 bitmap_iterator bi;
6215 unsigned i;
6216 bitmap e;
6217 tree retval, t;
6218 int used_strict_overflow;
6219 bool sop;
6220 value_range_t equiv_vr;
6222 /* Get the set of equivalences for VAR. */
6223 e = get_value_range (var)->equiv;
6225 /* Start at -1. Set it to 0 if we do a comparison without relying
6226 on overflow, or 1 if all comparisons rely on overflow. */
6227 used_strict_overflow = -1;
6229 /* Compare vars' value range with val. */
6230 equiv_vr = get_vr_for_comparison (SSA_NAME_VERSION (var));
6231 sop = false;
6232 retval = compare_range_with_value (comp, &equiv_vr, val, &sop);
6233 if (retval)
6234 used_strict_overflow = sop ? 1 : 0;
6236 /* If the equiv set is empty we have done all work we need to do. */
6237 if (e == NULL)
6239 if (retval
6240 && used_strict_overflow > 0)
6241 *strict_overflow_p = true;
6242 return retval;
6245 EXECUTE_IF_SET_IN_BITMAP (e, 0, i, bi)
6247 equiv_vr = get_vr_for_comparison (i);
6248 sop = false;
6249 t = compare_range_with_value (comp, &equiv_vr, val, &sop);
6250 if (t)
6252 /* If we get different answers from different members
6253 of the equivalence set this check must be in a dead
6254 code region. Folding it to a trap representation
6255 would be correct here. For now just return don't-know. */
6256 if (retval != NULL
6257 && t != retval)
6259 retval = NULL_TREE;
6260 break;
6262 retval = t;
6264 if (!sop)
6265 used_strict_overflow = 0;
6266 else if (used_strict_overflow < 0)
6267 used_strict_overflow = 1;
6271 if (retval
6272 && used_strict_overflow > 0)
6273 *strict_overflow_p = true;
6275 return retval;
6279 /* Given a comparison code COMP and names N1 and N2, compare all the
6280 ranges equivalent to N1 against all the ranges equivalent to N2
6281 to determine the value of N1 COMP N2. Return the same value
6282 returned by compare_ranges. Set *STRICT_OVERFLOW_P to indicate
6283 whether we relied on an overflow infinity in the comparison. */
6286 static tree
6287 compare_names (enum tree_code comp, tree n1, tree n2,
6288 bool *strict_overflow_p)
6290 tree t, retval;
6291 bitmap e1, e2;
6292 bitmap_iterator bi1, bi2;
6293 unsigned i1, i2;
6294 int used_strict_overflow;
6295 static bitmap_obstack *s_obstack = NULL;
6296 static bitmap s_e1 = NULL, s_e2 = NULL;
6298 /* Compare the ranges of every name equivalent to N1 against the
6299 ranges of every name equivalent to N2. */
6300 e1 = get_value_range (n1)->equiv;
6301 e2 = get_value_range (n2)->equiv;
6303 /* Use the fake bitmaps if e1 or e2 are not available. */
6304 if (s_obstack == NULL)
6306 s_obstack = XNEW (bitmap_obstack);
6307 bitmap_obstack_initialize (s_obstack);
6308 s_e1 = BITMAP_ALLOC (s_obstack);
6309 s_e2 = BITMAP_ALLOC (s_obstack);
6311 if (e1 == NULL)
6312 e1 = s_e1;
6313 if (e2 == NULL)
6314 e2 = s_e2;
6316 /* Add N1 and N2 to their own set of equivalences to avoid
6317 duplicating the body of the loop just to check N1 and N2
6318 ranges. */
6319 bitmap_set_bit (e1, SSA_NAME_VERSION (n1));
6320 bitmap_set_bit (e2, SSA_NAME_VERSION (n2));
6322 /* If the equivalence sets have a common intersection, then the two
6323 names can be compared without checking their ranges. */
6324 if (bitmap_intersect_p (e1, e2))
6326 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
6327 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
6329 return (comp == EQ_EXPR || comp == GE_EXPR || comp == LE_EXPR)
6330 ? boolean_true_node
6331 : boolean_false_node;
6334 /* Start at -1. Set it to 0 if we do a comparison without relying
6335 on overflow, or 1 if all comparisons rely on overflow. */
6336 used_strict_overflow = -1;
6338 /* Otherwise, compare all the equivalent ranges. First, add N1 and
6339 N2 to their own set of equivalences to avoid duplicating the body
6340 of the loop just to check N1 and N2 ranges. */
6341 EXECUTE_IF_SET_IN_BITMAP (e1, 0, i1, bi1)
6343 value_range_t vr1 = get_vr_for_comparison (i1);
6345 t = retval = NULL_TREE;
6346 EXECUTE_IF_SET_IN_BITMAP (e2, 0, i2, bi2)
6348 bool sop = false;
6350 value_range_t vr2 = get_vr_for_comparison (i2);
6352 t = compare_ranges (comp, &vr1, &vr2, &sop);
6353 if (t)
6355 /* If we get different answers from different members
6356 of the equivalence set this check must be in a dead
6357 code region. Folding it to a trap representation
6358 would be correct here. For now just return don't-know. */
6359 if (retval != NULL
6360 && t != retval)
6362 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
6363 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
6364 return NULL_TREE;
6366 retval = t;
6368 if (!sop)
6369 used_strict_overflow = 0;
6370 else if (used_strict_overflow < 0)
6371 used_strict_overflow = 1;
6375 if (retval)
6377 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
6378 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
6379 if (used_strict_overflow > 0)
6380 *strict_overflow_p = true;
6381 return retval;
6385 /* None of the equivalent ranges are useful in computing this
6386 comparison. */
6387 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
6388 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
6389 return NULL_TREE;
6392 /* Helper function for vrp_evaluate_conditional_warnv. */
6394 static tree
6395 vrp_evaluate_conditional_warnv_with_ops_using_ranges (enum tree_code code,
6396 tree op0, tree op1,
6397 bool * strict_overflow_p)
6399 value_range_t *vr0, *vr1;
6401 vr0 = (TREE_CODE (op0) == SSA_NAME) ? get_value_range (op0) : NULL;
6402 vr1 = (TREE_CODE (op1) == SSA_NAME) ? get_value_range (op1) : NULL;
6404 if (vr0 && vr1)
6405 return compare_ranges (code, vr0, vr1, strict_overflow_p);
6406 else if (vr0 && vr1 == NULL)
6407 return compare_range_with_value (code, vr0, op1, strict_overflow_p);
6408 else if (vr0 == NULL && vr1)
6409 return (compare_range_with_value
6410 (swap_tree_comparison (code), vr1, op0, strict_overflow_p));
6411 return NULL;
6414 /* Helper function for vrp_evaluate_conditional_warnv. */
6416 static tree
6417 vrp_evaluate_conditional_warnv_with_ops (enum tree_code code, tree op0,
6418 tree op1, bool use_equiv_p,
6419 bool *strict_overflow_p, bool *only_ranges)
6421 tree ret;
6422 if (only_ranges)
6423 *only_ranges = true;
6425 /* We only deal with integral and pointer types. */
6426 if (!INTEGRAL_TYPE_P (TREE_TYPE (op0))
6427 && !POINTER_TYPE_P (TREE_TYPE (op0)))
6428 return NULL_TREE;
6430 if (use_equiv_p)
6432 if (only_ranges
6433 && (ret = vrp_evaluate_conditional_warnv_with_ops_using_ranges
6434 (code, op0, op1, strict_overflow_p)))
6435 return ret;
6436 *only_ranges = false;
6437 if (TREE_CODE (op0) == SSA_NAME && TREE_CODE (op1) == SSA_NAME)
6438 return compare_names (code, op0, op1, strict_overflow_p);
6439 else if (TREE_CODE (op0) == SSA_NAME)
6440 return compare_name_with_value (code, op0, op1, strict_overflow_p);
6441 else if (TREE_CODE (op1) == SSA_NAME)
6442 return (compare_name_with_value
6443 (swap_tree_comparison (code), op1, op0, strict_overflow_p));
6445 else
6446 return vrp_evaluate_conditional_warnv_with_ops_using_ranges (code, op0, op1,
6447 strict_overflow_p);
6448 return NULL_TREE;
6451 /* Given (CODE OP0 OP1) within STMT, try to simplify it based on value range
6452 information. Return NULL if the conditional can not be evaluated.
6453 The ranges of all the names equivalent with the operands in COND
6454 will be used when trying to compute the value. If the result is
6455 based on undefined signed overflow, issue a warning if
6456 appropriate. */
6458 static tree
6459 vrp_evaluate_conditional (enum tree_code code, tree op0, tree op1, gimple stmt)
6461 bool sop;
6462 tree ret;
6463 bool only_ranges;
6465 /* Some passes and foldings leak constants with overflow flag set
6466 into the IL. Avoid doing wrong things with these and bail out. */
6467 if ((TREE_CODE (op0) == INTEGER_CST
6468 && TREE_OVERFLOW (op0))
6469 || (TREE_CODE (op1) == INTEGER_CST
6470 && TREE_OVERFLOW (op1)))
6471 return NULL_TREE;
6473 sop = false;
6474 ret = vrp_evaluate_conditional_warnv_with_ops (code, op0, op1, true, &sop,
6475 &only_ranges);
6477 if (ret && sop)
6479 enum warn_strict_overflow_code wc;
6480 const char* warnmsg;
6482 if (is_gimple_min_invariant (ret))
6484 wc = WARN_STRICT_OVERFLOW_CONDITIONAL;
6485 warnmsg = G_("assuming signed overflow does not occur when "
6486 "simplifying conditional to constant");
6488 else
6490 wc = WARN_STRICT_OVERFLOW_COMPARISON;
6491 warnmsg = G_("assuming signed overflow does not occur when "
6492 "simplifying conditional");
6495 if (issue_strict_overflow_warning (wc))
6497 location_t location;
6499 if (!gimple_has_location (stmt))
6500 location = input_location;
6501 else
6502 location = gimple_location (stmt);
6503 warning_at (location, OPT_Wstrict_overflow, "%s", warnmsg);
6507 if (warn_type_limits
6508 && ret && only_ranges
6509 && TREE_CODE_CLASS (code) == tcc_comparison
6510 && TREE_CODE (op0) == SSA_NAME)
6512 /* If the comparison is being folded and the operand on the LHS
6513 is being compared against a constant value that is outside of
6514 the natural range of OP0's type, then the predicate will
6515 always fold regardless of the value of OP0. If -Wtype-limits
6516 was specified, emit a warning. */
6517 tree type = TREE_TYPE (op0);
6518 value_range_t *vr0 = get_value_range (op0);
6520 if (vr0->type != VR_VARYING
6521 && INTEGRAL_TYPE_P (type)
6522 && vrp_val_is_min (vr0->min)
6523 && vrp_val_is_max (vr0->max)
6524 && is_gimple_min_invariant (op1))
6526 location_t location;
6528 if (!gimple_has_location (stmt))
6529 location = input_location;
6530 else
6531 location = gimple_location (stmt);
6533 warning_at (location, OPT_Wtype_limits,
6534 integer_zerop (ret)
6535 ? G_("comparison always false "
6536 "due to limited range of data type")
6537 : G_("comparison always true "
6538 "due to limited range of data type"));
6542 return ret;
6546 /* Visit conditional statement STMT. If we can determine which edge
6547 will be taken out of STMT's basic block, record it in
6548 *TAKEN_EDGE_P and return SSA_PROP_INTERESTING. Otherwise, return
6549 SSA_PROP_VARYING. */
6551 static enum ssa_prop_result
6552 vrp_visit_cond_stmt (gimple stmt, edge *taken_edge_p)
6554 tree val;
6555 bool sop;
6557 *taken_edge_p = NULL;
6559 if (dump_file && (dump_flags & TDF_DETAILS))
6561 tree use;
6562 ssa_op_iter i;
6564 fprintf (dump_file, "\nVisiting conditional with predicate: ");
6565 print_gimple_stmt (dump_file, stmt, 0, 0);
6566 fprintf (dump_file, "\nWith known ranges\n");
6568 FOR_EACH_SSA_TREE_OPERAND (use, stmt, i, SSA_OP_USE)
6570 fprintf (dump_file, "\t");
6571 print_generic_expr (dump_file, use, 0);
6572 fprintf (dump_file, ": ");
6573 dump_value_range (dump_file, vr_value[SSA_NAME_VERSION (use)]);
6576 fprintf (dump_file, "\n");
6579 /* Compute the value of the predicate COND by checking the known
6580 ranges of each of its operands.
6582 Note that we cannot evaluate all the equivalent ranges here
6583 because those ranges may not yet be final and with the current
6584 propagation strategy, we cannot determine when the value ranges
6585 of the names in the equivalence set have changed.
6587 For instance, given the following code fragment
6589 i_5 = PHI <8, i_13>
6591 i_14 = ASSERT_EXPR <i_5, i_5 != 0>
6592 if (i_14 == 1)
6595 Assume that on the first visit to i_14, i_5 has the temporary
6596 range [8, 8] because the second argument to the PHI function is
6597 not yet executable. We derive the range ~[0, 0] for i_14 and the
6598 equivalence set { i_5 }. So, when we visit 'if (i_14 == 1)' for
6599 the first time, since i_14 is equivalent to the range [8, 8], we
6600 determine that the predicate is always false.
6602 On the next round of propagation, i_13 is determined to be
6603 VARYING, which causes i_5 to drop down to VARYING. So, another
6604 visit to i_14 is scheduled. In this second visit, we compute the
6605 exact same range and equivalence set for i_14, namely ~[0, 0] and
6606 { i_5 }. But we did not have the previous range for i_5
6607 registered, so vrp_visit_assignment thinks that the range for
6608 i_14 has not changed. Therefore, the predicate 'if (i_14 == 1)'
6609 is not visited again, which stops propagation from visiting
6610 statements in the THEN clause of that if().
6612 To properly fix this we would need to keep the previous range
6613 value for the names in the equivalence set. This way we would've
6614 discovered that from one visit to the other i_5 changed from
6615 range [8, 8] to VR_VARYING.
6617 However, fixing this apparent limitation may not be worth the
6618 additional checking. Testing on several code bases (GCC, DLV,
6619 MICO, TRAMP3D and SPEC2000) showed that doing this results in
6620 4 more predicates folded in SPEC. */
6621 sop = false;
6623 val = vrp_evaluate_conditional_warnv_with_ops (gimple_cond_code (stmt),
6624 gimple_cond_lhs (stmt),
6625 gimple_cond_rhs (stmt),
6626 false, &sop, NULL);
6627 if (val)
6629 if (!sop)
6630 *taken_edge_p = find_taken_edge (gimple_bb (stmt), val);
6631 else
6633 if (dump_file && (dump_flags & TDF_DETAILS))
6634 fprintf (dump_file,
6635 "\nIgnoring predicate evaluation because "
6636 "it assumes that signed overflow is undefined");
6637 val = NULL_TREE;
6641 if (dump_file && (dump_flags & TDF_DETAILS))
6643 fprintf (dump_file, "\nPredicate evaluates to: ");
6644 if (val == NULL_TREE)
6645 fprintf (dump_file, "DON'T KNOW\n");
6646 else
6647 print_generic_stmt (dump_file, val, 0);
6650 return (*taken_edge_p) ? SSA_PROP_INTERESTING : SSA_PROP_VARYING;
6653 /* Searches the case label vector VEC for the index *IDX of the CASE_LABEL
6654 that includes the value VAL. The search is restricted to the range
6655 [START_IDX, n - 1] where n is the size of VEC.
6657 If there is a CASE_LABEL for VAL, its index is placed in IDX and true is
6658 returned.
6660 If there is no CASE_LABEL for VAL and there is one that is larger than VAL,
6661 it is placed in IDX and false is returned.
6663 If VAL is larger than any CASE_LABEL, n is placed on IDX and false is
6664 returned. */
6666 static bool
6667 find_case_label_index (gimple stmt, size_t start_idx, tree val, size_t *idx)
6669 size_t n = gimple_switch_num_labels (stmt);
6670 size_t low, high;
6672 /* Find case label for minimum of the value range or the next one.
6673 At each iteration we are searching in [low, high - 1]. */
6675 for (low = start_idx, high = n; high != low; )
6677 tree t;
6678 int cmp;
6679 /* Note that i != high, so we never ask for n. */
6680 size_t i = (high + low) / 2;
6681 t = gimple_switch_label (stmt, i);
6683 /* Cache the result of comparing CASE_LOW and val. */
6684 cmp = tree_int_cst_compare (CASE_LOW (t), val);
6686 if (cmp == 0)
6688 /* Ranges cannot be empty. */
6689 *idx = i;
6690 return true;
6692 else if (cmp > 0)
6693 high = i;
6694 else
6696 low = i + 1;
6697 if (CASE_HIGH (t) != NULL
6698 && tree_int_cst_compare (CASE_HIGH (t), val) >= 0)
6700 *idx = i;
6701 return true;
6706 *idx = high;
6707 return false;
6710 /* Searches the case label vector VEC for the range of CASE_LABELs that is used
6711 for values between MIN and MAX. The first index is placed in MIN_IDX. The
6712 last index is placed in MAX_IDX. If the range of CASE_LABELs is empty
6713 then MAX_IDX < MIN_IDX.
6714 Returns true if the default label is not needed. */
6716 static bool
6717 find_case_label_range (gimple stmt, tree min, tree max, size_t *min_idx,
6718 size_t *max_idx)
6720 size_t i, j;
6721 bool min_take_default = !find_case_label_index (stmt, 1, min, &i);
6722 bool max_take_default = !find_case_label_index (stmt, i, max, &j);
6724 if (i == j
6725 && min_take_default
6726 && max_take_default)
6728 /* Only the default case label reached.
6729 Return an empty range. */
6730 *min_idx = 1;
6731 *max_idx = 0;
6732 return false;
6734 else
6736 bool take_default = min_take_default || max_take_default;
6737 tree low, high;
6738 size_t k;
6740 if (max_take_default)
6741 j--;
6743 /* If the case label range is continuous, we do not need
6744 the default case label. Verify that. */
6745 high = CASE_LOW (gimple_switch_label (stmt, i));
6746 if (CASE_HIGH (gimple_switch_label (stmt, i)))
6747 high = CASE_HIGH (gimple_switch_label (stmt, i));
6748 for (k = i + 1; k <= j; ++k)
6750 low = CASE_LOW (gimple_switch_label (stmt, k));
6751 if (!integer_onep (int_const_binop (MINUS_EXPR, low, high)))
6753 take_default = true;
6754 break;
6756 high = low;
6757 if (CASE_HIGH (gimple_switch_label (stmt, k)))
6758 high = CASE_HIGH (gimple_switch_label (stmt, k));
6761 *min_idx = i;
6762 *max_idx = j;
6763 return !take_default;
6767 /* Visit switch statement STMT. If we can determine which edge
6768 will be taken out of STMT's basic block, record it in
6769 *TAKEN_EDGE_P and return SSA_PROP_INTERESTING. Otherwise, return
6770 SSA_PROP_VARYING. */
6772 static enum ssa_prop_result
6773 vrp_visit_switch_stmt (gimple stmt, edge *taken_edge_p)
6775 tree op, val;
6776 value_range_t *vr;
6777 size_t i = 0, j = 0;
6778 bool take_default;
6780 *taken_edge_p = NULL;
6781 op = gimple_switch_index (stmt);
6782 if (TREE_CODE (op) != SSA_NAME)
6783 return SSA_PROP_VARYING;
6785 vr = get_value_range (op);
6786 if (dump_file && (dump_flags & TDF_DETAILS))
6788 fprintf (dump_file, "\nVisiting switch expression with operand ");
6789 print_generic_expr (dump_file, op, 0);
6790 fprintf (dump_file, " with known range ");
6791 dump_value_range (dump_file, vr);
6792 fprintf (dump_file, "\n");
6795 if (vr->type != VR_RANGE
6796 || symbolic_range_p (vr))
6797 return SSA_PROP_VARYING;
6799 /* Find the single edge that is taken from the switch expression. */
6800 take_default = !find_case_label_range (stmt, vr->min, vr->max, &i, &j);
6802 /* Check if the range spans no CASE_LABEL. If so, we only reach the default
6803 label */
6804 if (j < i)
6806 gcc_assert (take_default);
6807 val = gimple_switch_default_label (stmt);
6809 else
6811 /* Check if labels with index i to j and maybe the default label
6812 are all reaching the same label. */
6814 val = gimple_switch_label (stmt, i);
6815 if (take_default
6816 && CASE_LABEL (gimple_switch_default_label (stmt))
6817 != CASE_LABEL (val))
6819 if (dump_file && (dump_flags & TDF_DETAILS))
6820 fprintf (dump_file, " not a single destination for this "
6821 "range\n");
6822 return SSA_PROP_VARYING;
6824 for (++i; i <= j; ++i)
6826 if (CASE_LABEL (gimple_switch_label (stmt, i)) != CASE_LABEL (val))
6828 if (dump_file && (dump_flags & TDF_DETAILS))
6829 fprintf (dump_file, " not a single destination for this "
6830 "range\n");
6831 return SSA_PROP_VARYING;
6836 *taken_edge_p = find_edge (gimple_bb (stmt),
6837 label_to_block (CASE_LABEL (val)));
6839 if (dump_file && (dump_flags & TDF_DETAILS))
6841 fprintf (dump_file, " will take edge to ");
6842 print_generic_stmt (dump_file, CASE_LABEL (val), 0);
6845 return SSA_PROP_INTERESTING;
6849 /* Evaluate statement STMT. If the statement produces a useful range,
6850 return SSA_PROP_INTERESTING and record the SSA name with the
6851 interesting range into *OUTPUT_P.
6853 If STMT is a conditional branch and we can determine its truth
6854 value, the taken edge is recorded in *TAKEN_EDGE_P.
6856 If STMT produces a varying value, return SSA_PROP_VARYING. */
6858 static enum ssa_prop_result
6859 vrp_visit_stmt (gimple stmt, edge *taken_edge_p, tree *output_p)
6861 tree def;
6862 ssa_op_iter iter;
6864 if (dump_file && (dump_flags & TDF_DETAILS))
6866 fprintf (dump_file, "\nVisiting statement:\n");
6867 print_gimple_stmt (dump_file, stmt, 0, dump_flags);
6868 fprintf (dump_file, "\n");
6871 if (!stmt_interesting_for_vrp (stmt))
6872 gcc_assert (stmt_ends_bb_p (stmt));
6873 else if (is_gimple_assign (stmt) || is_gimple_call (stmt))
6875 /* In general, assignments with virtual operands are not useful
6876 for deriving ranges, with the obvious exception of calls to
6877 builtin functions. */
6878 if ((is_gimple_call (stmt)
6879 && gimple_call_fndecl (stmt) != NULL_TREE
6880 && DECL_BUILT_IN (gimple_call_fndecl (stmt)))
6881 || !gimple_vuse (stmt))
6882 return vrp_visit_assignment_or_call (stmt, output_p);
6884 else if (gimple_code (stmt) == GIMPLE_COND)
6885 return vrp_visit_cond_stmt (stmt, taken_edge_p);
6886 else if (gimple_code (stmt) == GIMPLE_SWITCH)
6887 return vrp_visit_switch_stmt (stmt, taken_edge_p);
6889 /* All other statements produce nothing of interest for VRP, so mark
6890 their outputs varying and prevent further simulation. */
6891 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_DEF)
6892 set_value_range_to_varying (get_value_range (def));
6894 return SSA_PROP_VARYING;
6897 /* Union the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
6898 { VR1TYPE, VR0MIN, VR0MAX } and store the result
6899 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
6900 possible such range. The resulting range is not canonicalized. */
6902 static void
6903 union_ranges (enum value_range_type *vr0type,
6904 tree *vr0min, tree *vr0max,
6905 enum value_range_type vr1type,
6906 tree vr1min, tree vr1max)
6908 bool mineq = operand_equal_p (*vr0min, vr1min, 0);
6909 bool maxeq = operand_equal_p (*vr0max, vr1max, 0);
6911 /* [] is vr0, () is vr1 in the following classification comments. */
6912 if (mineq && maxeq)
6914 /* [( )] */
6915 if (*vr0type == vr1type)
6916 /* Nothing to do for equal ranges. */
6918 else if ((*vr0type == VR_RANGE
6919 && vr1type == VR_ANTI_RANGE)
6920 || (*vr0type == VR_ANTI_RANGE
6921 && vr1type == VR_RANGE))
6923 /* For anti-range with range union the result is varying. */
6924 goto give_up;
6926 else
6927 gcc_unreachable ();
6929 else if (operand_less_p (*vr0max, vr1min) == 1
6930 || operand_less_p (vr1max, *vr0min) == 1)
6932 /* [ ] ( ) or ( ) [ ]
6933 If the ranges have an empty intersection, result of the union
6934 operation is the anti-range or if both are anti-ranges
6935 it covers all. */
6936 if (*vr0type == VR_ANTI_RANGE
6937 && vr1type == VR_ANTI_RANGE)
6938 goto give_up;
6939 else if (*vr0type == VR_ANTI_RANGE
6940 && vr1type == VR_RANGE)
6942 else if (*vr0type == VR_RANGE
6943 && vr1type == VR_ANTI_RANGE)
6945 *vr0type = vr1type;
6946 *vr0min = vr1min;
6947 *vr0max = vr1max;
6949 else if (*vr0type == VR_RANGE
6950 && vr1type == VR_RANGE)
6952 /* The result is the convex hull of both ranges. */
6953 if (operand_less_p (*vr0max, vr1min) == 1)
6955 /* If the result can be an anti-range, create one. */
6956 if (TREE_CODE (*vr0max) == INTEGER_CST
6957 && TREE_CODE (vr1min) == INTEGER_CST
6958 && vrp_val_is_min (*vr0min)
6959 && vrp_val_is_max (vr1max))
6961 tree min = int_const_binop (PLUS_EXPR,
6962 *vr0max, integer_one_node);
6963 tree max = int_const_binop (MINUS_EXPR,
6964 vr1min, integer_one_node);
6965 if (!operand_less_p (max, min))
6967 *vr0type = VR_ANTI_RANGE;
6968 *vr0min = min;
6969 *vr0max = max;
6971 else
6972 *vr0max = vr1max;
6974 else
6975 *vr0max = vr1max;
6977 else
6979 /* If the result can be an anti-range, create one. */
6980 if (TREE_CODE (vr1max) == INTEGER_CST
6981 && TREE_CODE (*vr0min) == INTEGER_CST
6982 && vrp_val_is_min (vr1min)
6983 && vrp_val_is_max (*vr0max))
6985 tree min = int_const_binop (PLUS_EXPR,
6986 vr1max, integer_one_node);
6987 tree max = int_const_binop (MINUS_EXPR,
6988 *vr0min, integer_one_node);
6989 if (!operand_less_p (max, min))
6991 *vr0type = VR_ANTI_RANGE;
6992 *vr0min = min;
6993 *vr0max = max;
6995 else
6996 *vr0min = vr1min;
6998 else
6999 *vr0min = vr1min;
7002 else
7003 gcc_unreachable ();
7005 else if ((maxeq || operand_less_p (vr1max, *vr0max) == 1)
7006 && (mineq || operand_less_p (*vr0min, vr1min) == 1))
7008 /* [ ( ) ] or [( ) ] or [ ( )] */
7009 if (*vr0type == VR_RANGE
7010 && vr1type == VR_RANGE)
7012 else if (*vr0type == VR_ANTI_RANGE
7013 && vr1type == VR_ANTI_RANGE)
7015 *vr0type = vr1type;
7016 *vr0min = vr1min;
7017 *vr0max = vr1max;
7019 else if (*vr0type == VR_ANTI_RANGE
7020 && vr1type == VR_RANGE)
7022 /* Arbitrarily choose the right or left gap. */
7023 if (!mineq && TREE_CODE (vr1min) == INTEGER_CST)
7024 *vr0max = int_const_binop (MINUS_EXPR, vr1min, integer_one_node);
7025 else if (!maxeq && TREE_CODE (vr1max) == INTEGER_CST)
7026 *vr0min = int_const_binop (PLUS_EXPR, vr1max, integer_one_node);
7027 else
7028 goto give_up;
7030 else if (*vr0type == VR_RANGE
7031 && vr1type == VR_ANTI_RANGE)
7032 /* The result covers everything. */
7033 goto give_up;
7034 else
7035 gcc_unreachable ();
7037 else if ((maxeq || operand_less_p (*vr0max, vr1max) == 1)
7038 && (mineq || operand_less_p (vr1min, *vr0min) == 1))
7040 /* ( [ ] ) or ([ ] ) or ( [ ]) */
7041 if (*vr0type == VR_RANGE
7042 && vr1type == VR_RANGE)
7044 *vr0type = vr1type;
7045 *vr0min = vr1min;
7046 *vr0max = vr1max;
7048 else if (*vr0type == VR_ANTI_RANGE
7049 && vr1type == VR_ANTI_RANGE)
7051 else if (*vr0type == VR_RANGE
7052 && vr1type == VR_ANTI_RANGE)
7054 *vr0type = VR_ANTI_RANGE;
7055 if (!mineq && TREE_CODE (*vr0min) == INTEGER_CST)
7057 *vr0max = int_const_binop (MINUS_EXPR, *vr0min, integer_one_node);
7058 *vr0min = vr1min;
7060 else if (!maxeq && TREE_CODE (*vr0max) == INTEGER_CST)
7062 *vr0min = int_const_binop (PLUS_EXPR, *vr0max, integer_one_node);
7063 *vr0max = vr1max;
7065 else
7066 goto give_up;
7068 else if (*vr0type == VR_ANTI_RANGE
7069 && vr1type == VR_RANGE)
7070 /* The result covers everything. */
7071 goto give_up;
7072 else
7073 gcc_unreachable ();
7075 else if ((operand_less_p (vr1min, *vr0max) == 1
7076 || operand_equal_p (vr1min, *vr0max, 0))
7077 && operand_less_p (*vr0min, vr1min) == 1)
7079 /* [ ( ] ) or [ ]( ) */
7080 if (*vr0type == VR_RANGE
7081 && vr1type == VR_RANGE)
7082 *vr0max = vr1max;
7083 else if (*vr0type == VR_ANTI_RANGE
7084 && vr1type == VR_ANTI_RANGE)
7085 *vr0min = vr1min;
7086 else if (*vr0type == VR_ANTI_RANGE
7087 && vr1type == VR_RANGE)
7089 if (TREE_CODE (vr1min) == INTEGER_CST)
7090 *vr0max = int_const_binop (MINUS_EXPR, vr1min, integer_one_node);
7091 else
7092 goto give_up;
7094 else if (*vr0type == VR_RANGE
7095 && vr1type == VR_ANTI_RANGE)
7097 if (TREE_CODE (*vr0max) == INTEGER_CST)
7099 *vr0type = vr1type;
7100 *vr0min = int_const_binop (PLUS_EXPR, *vr0max, integer_one_node);
7101 *vr0max = vr1max;
7103 else
7104 goto give_up;
7106 else
7107 gcc_unreachable ();
7109 else if ((operand_less_p (*vr0min, vr1max) == 1
7110 || operand_equal_p (*vr0min, vr1max, 0))
7111 && operand_less_p (vr1min, *vr0min) == 1)
7113 /* ( [ ) ] or ( )[ ] */
7114 if (*vr0type == VR_RANGE
7115 && vr1type == VR_RANGE)
7116 *vr0min = vr1min;
7117 else if (*vr0type == VR_ANTI_RANGE
7118 && vr1type == VR_ANTI_RANGE)
7119 *vr0max = vr1max;
7120 else if (*vr0type == VR_ANTI_RANGE
7121 && vr1type == VR_RANGE)
7123 if (TREE_CODE (vr1max) == INTEGER_CST)
7124 *vr0min = int_const_binop (PLUS_EXPR, vr1max, integer_one_node);
7125 else
7126 goto give_up;
7128 else if (*vr0type == VR_RANGE
7129 && vr1type == VR_ANTI_RANGE)
7131 if (TREE_CODE (*vr0min) == INTEGER_CST)
7133 *vr0type = vr1type;
7134 *vr0min = vr1min;
7135 *vr0max = int_const_binop (MINUS_EXPR, *vr0min, integer_one_node);
7137 else
7138 goto give_up;
7140 else
7141 gcc_unreachable ();
7143 else
7144 goto give_up;
7146 return;
7148 give_up:
7149 *vr0type = VR_VARYING;
7150 *vr0min = NULL_TREE;
7151 *vr0max = NULL_TREE;
7154 /* Intersect the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
7155 { VR1TYPE, VR0MIN, VR0MAX } and store the result
7156 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
7157 possible such range. The resulting range is not canonicalized. */
7159 static void
7160 intersect_ranges (enum value_range_type *vr0type,
7161 tree *vr0min, tree *vr0max,
7162 enum value_range_type vr1type,
7163 tree vr1min, tree vr1max)
7165 bool mineq = operand_equal_p (*vr0min, vr1min, 0);
7166 bool maxeq = operand_equal_p (*vr0max, vr1max, 0);
7168 /* [] is vr0, () is vr1 in the following classification comments. */
7169 if (mineq && maxeq)
7171 /* [( )] */
7172 if (*vr0type == vr1type)
7173 /* Nothing to do for equal ranges. */
7175 else if ((*vr0type == VR_RANGE
7176 && vr1type == VR_ANTI_RANGE)
7177 || (*vr0type == VR_ANTI_RANGE
7178 && vr1type == VR_RANGE))
7180 /* For anti-range with range intersection the result is empty. */
7181 *vr0type = VR_UNDEFINED;
7182 *vr0min = NULL_TREE;
7183 *vr0max = NULL_TREE;
7185 else
7186 gcc_unreachable ();
7188 else if (operand_less_p (*vr0max, vr1min) == 1
7189 || operand_less_p (vr1max, *vr0min) == 1)
7191 /* [ ] ( ) or ( ) [ ]
7192 If the ranges have an empty intersection, the result of the
7193 intersect operation is the range for intersecting an
7194 anti-range with a range or empty when intersecting two ranges. */
7195 if (*vr0type == VR_RANGE
7196 && vr1type == VR_ANTI_RANGE)
7198 else if (*vr0type == VR_ANTI_RANGE
7199 && vr1type == VR_RANGE)
7201 *vr0type = vr1type;
7202 *vr0min = vr1min;
7203 *vr0max = vr1max;
7205 else if (*vr0type == VR_RANGE
7206 && vr1type == VR_RANGE)
7208 *vr0type = VR_UNDEFINED;
7209 *vr0min = NULL_TREE;
7210 *vr0max = NULL_TREE;
7212 else if (*vr0type == VR_ANTI_RANGE
7213 && vr1type == VR_ANTI_RANGE)
7215 /* If the anti-ranges are adjacent to each other merge them. */
7216 if (TREE_CODE (*vr0max) == INTEGER_CST
7217 && TREE_CODE (vr1min) == INTEGER_CST
7218 && operand_less_p (*vr0max, vr1min) == 1
7219 && integer_onep (int_const_binop (MINUS_EXPR,
7220 vr1min, *vr0max)))
7221 *vr0max = vr1max;
7222 else if (TREE_CODE (vr1max) == INTEGER_CST
7223 && TREE_CODE (*vr0min) == INTEGER_CST
7224 && operand_less_p (vr1max, *vr0min) == 1
7225 && integer_onep (int_const_binop (MINUS_EXPR,
7226 *vr0min, vr1max)))
7227 *vr0min = vr1min;
7228 /* Else arbitrarily take VR0. */
7231 else if ((maxeq || operand_less_p (vr1max, *vr0max) == 1)
7232 && (mineq || operand_less_p (*vr0min, vr1min) == 1))
7234 /* [ ( ) ] or [( ) ] or [ ( )] */
7235 if (*vr0type == VR_RANGE
7236 && vr1type == VR_RANGE)
7238 /* If both are ranges the result is the inner one. */
7239 *vr0type = vr1type;
7240 *vr0min = vr1min;
7241 *vr0max = vr1max;
7243 else if (*vr0type == VR_RANGE
7244 && vr1type == VR_ANTI_RANGE)
7246 /* Choose the right gap if the left one is empty. */
7247 if (mineq)
7249 if (TREE_CODE (vr1max) == INTEGER_CST)
7250 *vr0min = int_const_binop (PLUS_EXPR, vr1max, integer_one_node);
7251 else
7252 *vr0min = vr1max;
7254 /* Choose the left gap if the right one is empty. */
7255 else if (maxeq)
7257 if (TREE_CODE (vr1min) == INTEGER_CST)
7258 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
7259 integer_one_node);
7260 else
7261 *vr0max = vr1min;
7263 /* Choose the anti-range if the range is effectively varying. */
7264 else if (vrp_val_is_min (*vr0min)
7265 && vrp_val_is_max (*vr0max))
7267 *vr0type = vr1type;
7268 *vr0min = vr1min;
7269 *vr0max = vr1max;
7271 /* Else choose the range. */
7273 else if (*vr0type == VR_ANTI_RANGE
7274 && vr1type == VR_ANTI_RANGE)
7275 /* If both are anti-ranges the result is the outer one. */
7277 else if (*vr0type == VR_ANTI_RANGE
7278 && vr1type == VR_RANGE)
7280 /* The intersection is empty. */
7281 *vr0type = VR_UNDEFINED;
7282 *vr0min = NULL_TREE;
7283 *vr0max = NULL_TREE;
7285 else
7286 gcc_unreachable ();
7288 else if ((maxeq || operand_less_p (*vr0max, vr1max) == 1)
7289 && (mineq || operand_less_p (vr1min, *vr0min) == 1))
7291 /* ( [ ] ) or ([ ] ) or ( [ ]) */
7292 if (*vr0type == VR_RANGE
7293 && vr1type == VR_RANGE)
7294 /* Choose the inner range. */
7296 else if (*vr0type == VR_ANTI_RANGE
7297 && vr1type == VR_RANGE)
7299 /* Choose the right gap if the left is empty. */
7300 if (mineq)
7302 *vr0type = VR_RANGE;
7303 if (TREE_CODE (*vr0max) == INTEGER_CST)
7304 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
7305 integer_one_node);
7306 else
7307 *vr0min = *vr0max;
7308 *vr0max = vr1max;
7310 /* Choose the left gap if the right is empty. */
7311 else if (maxeq)
7313 *vr0type = VR_RANGE;
7314 if (TREE_CODE (*vr0min) == INTEGER_CST)
7315 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
7316 integer_one_node);
7317 else
7318 *vr0max = *vr0min;
7319 *vr0min = vr1min;
7321 /* Choose the anti-range if the range is effectively varying. */
7322 else if (vrp_val_is_min (vr1min)
7323 && vrp_val_is_max (vr1max))
7325 /* Else choose the range. */
7326 else
7328 *vr0type = vr1type;
7329 *vr0min = vr1min;
7330 *vr0max = vr1max;
7333 else if (*vr0type == VR_ANTI_RANGE
7334 && vr1type == VR_ANTI_RANGE)
7336 /* If both are anti-ranges the result is the outer one. */
7337 *vr0type = vr1type;
7338 *vr0min = vr1min;
7339 *vr0max = vr1max;
7341 else if (vr1type == VR_ANTI_RANGE
7342 && *vr0type == VR_RANGE)
7344 /* The intersection is empty. */
7345 *vr0type = VR_UNDEFINED;
7346 *vr0min = NULL_TREE;
7347 *vr0max = NULL_TREE;
7349 else
7350 gcc_unreachable ();
7352 else if ((operand_less_p (vr1min, *vr0max) == 1
7353 || operand_equal_p (vr1min, *vr0max, 0))
7354 && operand_less_p (*vr0min, vr1min) == 1)
7356 /* [ ( ] ) or [ ]( ) */
7357 if (*vr0type == VR_ANTI_RANGE
7358 && vr1type == VR_ANTI_RANGE)
7359 *vr0max = vr1max;
7360 else if (*vr0type == VR_RANGE
7361 && vr1type == VR_RANGE)
7362 *vr0min = vr1min;
7363 else if (*vr0type == VR_RANGE
7364 && vr1type == VR_ANTI_RANGE)
7366 if (TREE_CODE (vr1min) == INTEGER_CST)
7367 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
7368 integer_one_node);
7369 else
7370 *vr0max = vr1min;
7372 else if (*vr0type == VR_ANTI_RANGE
7373 && vr1type == VR_RANGE)
7375 *vr0type = VR_RANGE;
7376 if (TREE_CODE (*vr0max) == INTEGER_CST)
7377 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
7378 integer_one_node);
7379 else
7380 *vr0min = *vr0max;
7381 *vr0max = vr1max;
7383 else
7384 gcc_unreachable ();
7386 else if ((operand_less_p (*vr0min, vr1max) == 1
7387 || operand_equal_p (*vr0min, vr1max, 0))
7388 && operand_less_p (vr1min, *vr0min) == 1)
7390 /* ( [ ) ] or ( )[ ] */
7391 if (*vr0type == VR_ANTI_RANGE
7392 && vr1type == VR_ANTI_RANGE)
7393 *vr0min = vr1min;
7394 else if (*vr0type == VR_RANGE
7395 && vr1type == VR_RANGE)
7396 *vr0max = vr1max;
7397 else if (*vr0type == VR_RANGE
7398 && vr1type == VR_ANTI_RANGE)
7400 if (TREE_CODE (vr1max) == INTEGER_CST)
7401 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
7402 integer_one_node);
7403 else
7404 *vr0min = vr1max;
7406 else if (*vr0type == VR_ANTI_RANGE
7407 && vr1type == VR_RANGE)
7409 *vr0type = VR_RANGE;
7410 if (TREE_CODE (*vr0min) == INTEGER_CST)
7411 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
7412 integer_one_node);
7413 else
7414 *vr0max = *vr0min;
7415 *vr0min = vr1min;
7417 else
7418 gcc_unreachable ();
7421 /* As a fallback simply use { *VRTYPE, *VR0MIN, *VR0MAX } as
7422 result for the intersection. That's always a conservative
7423 correct estimate. */
7425 return;
7429 /* Intersect the two value-ranges *VR0 and *VR1 and store the result
7430 in *VR0. This may not be the smallest possible such range. */
7432 static void
7433 vrp_intersect_ranges_1 (value_range_t *vr0, value_range_t *vr1)
7435 value_range_t saved;
7437 /* If either range is VR_VARYING the other one wins. */
7438 if (vr1->type == VR_VARYING)
7439 return;
7440 if (vr0->type == VR_VARYING)
7442 copy_value_range (vr0, vr1);
7443 return;
7446 /* When either range is VR_UNDEFINED the resulting range is
7447 VR_UNDEFINED, too. */
7448 if (vr0->type == VR_UNDEFINED)
7449 return;
7450 if (vr1->type == VR_UNDEFINED)
7452 set_value_range_to_undefined (vr0);
7453 return;
7456 /* Save the original vr0 so we can return it as conservative intersection
7457 result when our worker turns things to varying. */
7458 saved = *vr0;
7459 intersect_ranges (&vr0->type, &vr0->min, &vr0->max,
7460 vr1->type, vr1->min, vr1->max);
7461 /* Make sure to canonicalize the result though as the inversion of a
7462 VR_RANGE can still be a VR_RANGE. */
7463 set_and_canonicalize_value_range (vr0, vr0->type,
7464 vr0->min, vr0->max, vr0->equiv);
7465 /* If that failed, use the saved original VR0. */
7466 if (vr0->type == VR_VARYING)
7468 *vr0 = saved;
7469 return;
7471 /* If the result is VR_UNDEFINED there is no need to mess with
7472 the equivalencies. */
7473 if (vr0->type == VR_UNDEFINED)
7474 return;
7476 /* The resulting set of equivalences for range intersection is the union of
7477 the two sets. */
7478 if (vr0->equiv && vr1->equiv && vr0->equiv != vr1->equiv)
7479 bitmap_ior_into (vr0->equiv, vr1->equiv);
7480 else if (vr1->equiv && !vr0->equiv)
7481 bitmap_copy (vr0->equiv, vr1->equiv);
7484 static void
7485 vrp_intersect_ranges (value_range_t *vr0, value_range_t *vr1)
7487 if (dump_file && (dump_flags & TDF_DETAILS))
7489 fprintf (dump_file, "Intersecting\n ");
7490 dump_value_range (dump_file, vr0);
7491 fprintf (dump_file, "\nand\n ");
7492 dump_value_range (dump_file, vr1);
7493 fprintf (dump_file, "\n");
7495 vrp_intersect_ranges_1 (vr0, vr1);
7496 if (dump_file && (dump_flags & TDF_DETAILS))
7498 fprintf (dump_file, "to\n ");
7499 dump_value_range (dump_file, vr0);
7500 fprintf (dump_file, "\n");
7504 /* Meet operation for value ranges. Given two value ranges VR0 and
7505 VR1, store in VR0 a range that contains both VR0 and VR1. This
7506 may not be the smallest possible such range. */
7508 static void
7509 vrp_meet_1 (value_range_t *vr0, value_range_t *vr1)
7511 value_range_t saved;
7513 if (vr0->type == VR_UNDEFINED)
7515 /* Drop equivalences. See PR53465. */
7516 set_value_range (vr0, vr1->type, vr1->min, vr1->max, NULL);
7517 return;
7520 if (vr1->type == VR_UNDEFINED)
7522 /* VR0 already has the resulting range, just drop equivalences.
7523 See PR53465. */
7524 if (vr0->equiv)
7525 bitmap_clear (vr0->equiv);
7526 return;
7529 if (vr0->type == VR_VARYING)
7531 /* Nothing to do. VR0 already has the resulting range. */
7532 return;
7535 if (vr1->type == VR_VARYING)
7537 set_value_range_to_varying (vr0);
7538 return;
7541 saved = *vr0;
7542 union_ranges (&vr0->type, &vr0->min, &vr0->max,
7543 vr1->type, vr1->min, vr1->max);
7544 if (vr0->type == VR_VARYING)
7546 /* Failed to find an efficient meet. Before giving up and setting
7547 the result to VARYING, see if we can at least derive a useful
7548 anti-range. FIXME, all this nonsense about distinguishing
7549 anti-ranges from ranges is necessary because of the odd
7550 semantics of range_includes_zero_p and friends. */
7551 if (!symbolic_range_p (&saved)
7552 && ((saved.type == VR_RANGE && !range_includes_zero_p (&saved))
7553 || (saved.type == VR_ANTI_RANGE && range_includes_zero_p (&saved)))
7554 && !symbolic_range_p (vr1)
7555 && ((vr1->type == VR_RANGE && !range_includes_zero_p (vr1))
7556 || (vr1->type == VR_ANTI_RANGE && range_includes_zero_p (vr1))))
7558 set_value_range_to_nonnull (vr0, TREE_TYPE (saved.min));
7560 /* Since this meet operation did not result from the meeting of
7561 two equivalent names, VR0 cannot have any equivalences. */
7562 if (vr0->equiv)
7563 bitmap_clear (vr0->equiv);
7564 return;
7567 set_value_range_to_varying (vr0);
7568 return;
7570 set_and_canonicalize_value_range (vr0, vr0->type, vr0->min, vr0->max,
7571 vr0->equiv);
7572 if (vr0->type == VR_VARYING)
7573 return;
7575 /* The resulting set of equivalences is always the intersection of
7576 the two sets. */
7577 if (vr0->equiv && vr1->equiv && vr0->equiv != vr1->equiv)
7578 bitmap_and_into (vr0->equiv, vr1->equiv);
7579 else if (vr0->equiv && !vr1->equiv)
7580 bitmap_clear (vr0->equiv);
7583 static void
7584 vrp_meet (value_range_t *vr0, value_range_t *vr1)
7586 if (dump_file && (dump_flags & TDF_DETAILS))
7588 fprintf (dump_file, "Meeting\n ");
7589 dump_value_range (dump_file, vr0);
7590 fprintf (dump_file, "\nand\n ");
7591 dump_value_range (dump_file, vr1);
7592 fprintf (dump_file, "\n");
7594 vrp_meet_1 (vr0, vr1);
7595 if (dump_file && (dump_flags & TDF_DETAILS))
7597 fprintf (dump_file, "to\n ");
7598 dump_value_range (dump_file, vr0);
7599 fprintf (dump_file, "\n");
7604 /* Visit all arguments for PHI node PHI that flow through executable
7605 edges. If a valid value range can be derived from all the incoming
7606 value ranges, set a new range for the LHS of PHI. */
7608 static enum ssa_prop_result
7609 vrp_visit_phi_node (gimple phi)
7611 size_t i;
7612 tree lhs = PHI_RESULT (phi);
7613 value_range_t *lhs_vr = get_value_range (lhs);
7614 value_range_t vr_result = VR_INITIALIZER;
7615 bool first = true;
7616 int edges, old_edges;
7617 struct loop *l;
7619 if (dump_file && (dump_flags & TDF_DETAILS))
7621 fprintf (dump_file, "\nVisiting PHI node: ");
7622 print_gimple_stmt (dump_file, phi, 0, dump_flags);
7625 edges = 0;
7626 for (i = 0; i < gimple_phi_num_args (phi); i++)
7628 edge e = gimple_phi_arg_edge (phi, i);
7630 if (dump_file && (dump_flags & TDF_DETAILS))
7632 fprintf (dump_file,
7633 "\n Argument #%d (%d -> %d %sexecutable)\n",
7634 (int) i, e->src->index, e->dest->index,
7635 (e->flags & EDGE_EXECUTABLE) ? "" : "not ");
7638 if (e->flags & EDGE_EXECUTABLE)
7640 tree arg = PHI_ARG_DEF (phi, i);
7641 value_range_t vr_arg;
7643 ++edges;
7645 if (TREE_CODE (arg) == SSA_NAME)
7647 vr_arg = *(get_value_range (arg));
7649 else
7651 if (is_overflow_infinity (arg))
7653 arg = copy_node (arg);
7654 TREE_OVERFLOW (arg) = 0;
7657 vr_arg.type = VR_RANGE;
7658 vr_arg.min = arg;
7659 vr_arg.max = arg;
7660 vr_arg.equiv = NULL;
7663 if (dump_file && (dump_flags & TDF_DETAILS))
7665 fprintf (dump_file, "\t");
7666 print_generic_expr (dump_file, arg, dump_flags);
7667 fprintf (dump_file, "\n\tValue: ");
7668 dump_value_range (dump_file, &vr_arg);
7669 fprintf (dump_file, "\n");
7672 if (first)
7673 copy_value_range (&vr_result, &vr_arg);
7674 else
7675 vrp_meet (&vr_result, &vr_arg);
7676 first = false;
7678 if (vr_result.type == VR_VARYING)
7679 break;
7683 if (vr_result.type == VR_VARYING)
7684 goto varying;
7685 else if (vr_result.type == VR_UNDEFINED)
7686 goto update_range;
7688 old_edges = vr_phi_edge_counts[SSA_NAME_VERSION (lhs)];
7689 vr_phi_edge_counts[SSA_NAME_VERSION (lhs)] = edges;
7691 /* To prevent infinite iterations in the algorithm, derive ranges
7692 when the new value is slightly bigger or smaller than the
7693 previous one. We don't do this if we have seen a new executable
7694 edge; this helps us avoid an overflow infinity for conditionals
7695 which are not in a loop. */
7696 if (edges > 0
7697 && gimple_phi_num_args (phi) > 1
7698 && edges == old_edges)
7700 int cmp_min = compare_values (lhs_vr->min, vr_result.min);
7701 int cmp_max = compare_values (lhs_vr->max, vr_result.max);
7703 /* For non VR_RANGE or for pointers fall back to varying if
7704 the range changed. */
7705 if ((lhs_vr->type != VR_RANGE || vr_result.type != VR_RANGE
7706 || POINTER_TYPE_P (TREE_TYPE (lhs)))
7707 && (cmp_min != 0 || cmp_max != 0))
7708 goto varying;
7710 /* If the new minimum is smaller or larger than the previous
7711 one, go all the way to -INF. In the first case, to avoid
7712 iterating millions of times to reach -INF, and in the
7713 other case to avoid infinite bouncing between different
7714 minimums. */
7715 if (cmp_min > 0 || cmp_min < 0)
7717 if (!needs_overflow_infinity (TREE_TYPE (vr_result.min))
7718 || !vrp_var_may_overflow (lhs, phi))
7719 vr_result.min = TYPE_MIN_VALUE (TREE_TYPE (vr_result.min));
7720 else if (supports_overflow_infinity (TREE_TYPE (vr_result.min)))
7721 vr_result.min =
7722 negative_overflow_infinity (TREE_TYPE (vr_result.min));
7725 /* Similarly, if the new maximum is smaller or larger than
7726 the previous one, go all the way to +INF. */
7727 if (cmp_max < 0 || cmp_max > 0)
7729 if (!needs_overflow_infinity (TREE_TYPE (vr_result.max))
7730 || !vrp_var_may_overflow (lhs, phi))
7731 vr_result.max = TYPE_MAX_VALUE (TREE_TYPE (vr_result.max));
7732 else if (supports_overflow_infinity (TREE_TYPE (vr_result.max)))
7733 vr_result.max =
7734 positive_overflow_infinity (TREE_TYPE (vr_result.max));
7737 /* If we dropped either bound to +-INF then if this is a loop
7738 PHI node SCEV may known more about its value-range. */
7739 if ((cmp_min > 0 || cmp_min < 0
7740 || cmp_max < 0 || cmp_max > 0)
7741 && current_loops
7742 && (l = loop_containing_stmt (phi))
7743 && l->header == gimple_bb (phi))
7744 adjust_range_with_scev (&vr_result, l, phi, lhs);
7746 /* If we will end up with a (-INF, +INF) range, set it to
7747 VARYING. Same if the previous max value was invalid for
7748 the type and we end up with vr_result.min > vr_result.max. */
7749 if ((vrp_val_is_max (vr_result.max)
7750 && vrp_val_is_min (vr_result.min))
7751 || compare_values (vr_result.min,
7752 vr_result.max) > 0)
7753 goto varying;
7756 /* If the new range is different than the previous value, keep
7757 iterating. */
7758 update_range:
7759 if (update_value_range (lhs, &vr_result))
7761 if (dump_file && (dump_flags & TDF_DETAILS))
7763 fprintf (dump_file, "Found new range for ");
7764 print_generic_expr (dump_file, lhs, 0);
7765 fprintf (dump_file, ": ");
7766 dump_value_range (dump_file, &vr_result);
7767 fprintf (dump_file, "\n\n");
7770 return SSA_PROP_INTERESTING;
7773 /* Nothing changed, don't add outgoing edges. */
7774 return SSA_PROP_NOT_INTERESTING;
7776 /* No match found. Set the LHS to VARYING. */
7777 varying:
7778 set_value_range_to_varying (lhs_vr);
7779 return SSA_PROP_VARYING;
7782 /* Simplify boolean operations if the source is known
7783 to be already a boolean. */
7784 static bool
7785 simplify_truth_ops_using_ranges (gimple_stmt_iterator *gsi, gimple stmt)
7787 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
7788 tree lhs, op0, op1;
7789 bool need_conversion;
7791 /* We handle only !=/== case here. */
7792 gcc_assert (rhs_code == EQ_EXPR || rhs_code == NE_EXPR);
7794 op0 = gimple_assign_rhs1 (stmt);
7795 if (!op_with_boolean_value_range_p (op0))
7796 return false;
7798 op1 = gimple_assign_rhs2 (stmt);
7799 if (!op_with_boolean_value_range_p (op1))
7800 return false;
7802 /* Reduce number of cases to handle to NE_EXPR. As there is no
7803 BIT_XNOR_EXPR we cannot replace A == B with a single statement. */
7804 if (rhs_code == EQ_EXPR)
7806 if (TREE_CODE (op1) == INTEGER_CST)
7807 op1 = int_const_binop (BIT_XOR_EXPR, op1, integer_one_node);
7808 else
7809 return false;
7812 lhs = gimple_assign_lhs (stmt);
7813 need_conversion
7814 = !useless_type_conversion_p (TREE_TYPE (lhs), TREE_TYPE (op0));
7816 /* Make sure to not sign-extend a 1-bit 1 when converting the result. */
7817 if (need_conversion
7818 && !TYPE_UNSIGNED (TREE_TYPE (op0))
7819 && TYPE_PRECISION (TREE_TYPE (op0)) == 1
7820 && TYPE_PRECISION (TREE_TYPE (lhs)) > 1)
7821 return false;
7823 /* For A != 0 we can substitute A itself. */
7824 if (integer_zerop (op1))
7825 gimple_assign_set_rhs_with_ops (gsi,
7826 need_conversion
7827 ? NOP_EXPR : TREE_CODE (op0),
7828 op0, NULL_TREE);
7829 /* For A != B we substitute A ^ B. Either with conversion. */
7830 else if (need_conversion)
7832 gimple newop;
7833 tree tem = create_tmp_reg (TREE_TYPE (op0), NULL);
7834 newop = gimple_build_assign_with_ops (BIT_XOR_EXPR, tem, op0, op1);
7835 tem = make_ssa_name (tem, newop);
7836 gimple_assign_set_lhs (newop, tem);
7837 gsi_insert_before (gsi, newop, GSI_SAME_STMT);
7838 update_stmt (newop);
7839 gimple_assign_set_rhs_with_ops (gsi, NOP_EXPR, tem, NULL_TREE);
7841 /* Or without. */
7842 else
7843 gimple_assign_set_rhs_with_ops (gsi, BIT_XOR_EXPR, op0, op1);
7844 update_stmt (gsi_stmt (*gsi));
7846 return true;
7849 /* Simplify a division or modulo operator to a right shift or
7850 bitwise and if the first operand is unsigned or is greater
7851 than zero and the second operand is an exact power of two. */
7853 static bool
7854 simplify_div_or_mod_using_ranges (gimple stmt)
7856 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
7857 tree val = NULL;
7858 tree op0 = gimple_assign_rhs1 (stmt);
7859 tree op1 = gimple_assign_rhs2 (stmt);
7860 value_range_t *vr = get_value_range (gimple_assign_rhs1 (stmt));
7862 if (TYPE_UNSIGNED (TREE_TYPE (op0)))
7864 val = integer_one_node;
7866 else
7868 bool sop = false;
7870 val = compare_range_with_value (GE_EXPR, vr, integer_zero_node, &sop);
7872 if (val
7873 && sop
7874 && integer_onep (val)
7875 && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC))
7877 location_t location;
7879 if (!gimple_has_location (stmt))
7880 location = input_location;
7881 else
7882 location = gimple_location (stmt);
7883 warning_at (location, OPT_Wstrict_overflow,
7884 "assuming signed overflow does not occur when "
7885 "simplifying %</%> or %<%%%> to %<>>%> or %<&%>");
7889 if (val && integer_onep (val))
7891 tree t;
7893 if (rhs_code == TRUNC_DIV_EXPR)
7895 t = build_int_cst (integer_type_node, tree_log2 (op1));
7896 gimple_assign_set_rhs_code (stmt, RSHIFT_EXPR);
7897 gimple_assign_set_rhs1 (stmt, op0);
7898 gimple_assign_set_rhs2 (stmt, t);
7900 else
7902 t = build_int_cst (TREE_TYPE (op1), 1);
7903 t = int_const_binop (MINUS_EXPR, op1, t);
7904 t = fold_convert (TREE_TYPE (op0), t);
7906 gimple_assign_set_rhs_code (stmt, BIT_AND_EXPR);
7907 gimple_assign_set_rhs1 (stmt, op0);
7908 gimple_assign_set_rhs2 (stmt, t);
7911 update_stmt (stmt);
7912 return true;
7915 return false;
7918 /* If the operand to an ABS_EXPR is >= 0, then eliminate the
7919 ABS_EXPR. If the operand is <= 0, then simplify the
7920 ABS_EXPR into a NEGATE_EXPR. */
7922 static bool
7923 simplify_abs_using_ranges (gimple stmt)
7925 tree val = NULL;
7926 tree op = gimple_assign_rhs1 (stmt);
7927 tree type = TREE_TYPE (op);
7928 value_range_t *vr = get_value_range (op);
7930 if (TYPE_UNSIGNED (type))
7932 val = integer_zero_node;
7934 else if (vr)
7936 bool sop = false;
7938 val = compare_range_with_value (LE_EXPR, vr, integer_zero_node, &sop);
7939 if (!val)
7941 sop = false;
7942 val = compare_range_with_value (GE_EXPR, vr, integer_zero_node,
7943 &sop);
7945 if (val)
7947 if (integer_zerop (val))
7948 val = integer_one_node;
7949 else if (integer_onep (val))
7950 val = integer_zero_node;
7954 if (val
7955 && (integer_onep (val) || integer_zerop (val)))
7957 if (sop && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC))
7959 location_t location;
7961 if (!gimple_has_location (stmt))
7962 location = input_location;
7963 else
7964 location = gimple_location (stmt);
7965 warning_at (location, OPT_Wstrict_overflow,
7966 "assuming signed overflow does not occur when "
7967 "simplifying %<abs (X)%> to %<X%> or %<-X%>");
7970 gimple_assign_set_rhs1 (stmt, op);
7971 if (integer_onep (val))
7972 gimple_assign_set_rhs_code (stmt, NEGATE_EXPR);
7973 else
7974 gimple_assign_set_rhs_code (stmt, SSA_NAME);
7975 update_stmt (stmt);
7976 return true;
7980 return false;
7983 /* Optimize away redundant BIT_AND_EXPR and BIT_IOR_EXPR.
7984 If all the bits that are being cleared by & are already
7985 known to be zero from VR, or all the bits that are being
7986 set by | are already known to be one from VR, the bit
7987 operation is redundant. */
7989 static bool
7990 simplify_bit_ops_using_ranges (gimple_stmt_iterator *gsi, gimple stmt)
7992 tree op0 = gimple_assign_rhs1 (stmt);
7993 tree op1 = gimple_assign_rhs2 (stmt);
7994 tree op = NULL_TREE;
7995 value_range_t vr0 = VR_INITIALIZER;
7996 value_range_t vr1 = VR_INITIALIZER;
7997 double_int may_be_nonzero0, may_be_nonzero1;
7998 double_int must_be_nonzero0, must_be_nonzero1;
7999 double_int mask;
8001 if (TREE_CODE (op0) == SSA_NAME)
8002 vr0 = *(get_value_range (op0));
8003 else if (is_gimple_min_invariant (op0))
8004 set_value_range_to_value (&vr0, op0, NULL);
8005 else
8006 return false;
8008 if (TREE_CODE (op1) == SSA_NAME)
8009 vr1 = *(get_value_range (op1));
8010 else if (is_gimple_min_invariant (op1))
8011 set_value_range_to_value (&vr1, op1, NULL);
8012 else
8013 return false;
8015 if (!zero_nonzero_bits_from_vr (&vr0, &may_be_nonzero0, &must_be_nonzero0))
8016 return false;
8017 if (!zero_nonzero_bits_from_vr (&vr1, &may_be_nonzero1, &must_be_nonzero1))
8018 return false;
8020 switch (gimple_assign_rhs_code (stmt))
8022 case BIT_AND_EXPR:
8023 mask = double_int_and_not (may_be_nonzero0, must_be_nonzero1);
8024 if (double_int_zero_p (mask))
8026 op = op0;
8027 break;
8029 mask = double_int_and_not (may_be_nonzero1, must_be_nonzero0);
8030 if (double_int_zero_p (mask))
8032 op = op1;
8033 break;
8035 break;
8036 case BIT_IOR_EXPR:
8037 mask = double_int_and_not (may_be_nonzero0, must_be_nonzero1);
8038 if (double_int_zero_p (mask))
8040 op = op1;
8041 break;
8043 mask = double_int_and_not (may_be_nonzero1, must_be_nonzero0);
8044 if (double_int_zero_p (mask))
8046 op = op0;
8047 break;
8049 break;
8050 default:
8051 gcc_unreachable ();
8054 if (op == NULL_TREE)
8055 return false;
8057 gimple_assign_set_rhs_with_ops (gsi, TREE_CODE (op), op, NULL);
8058 update_stmt (gsi_stmt (*gsi));
8059 return true;
8062 /* We are comparing trees OP0 and OP1 using COND_CODE. OP0 has
8063 a known value range VR.
8065 If there is one and only one value which will satisfy the
8066 conditional, then return that value. Else return NULL. */
8068 static tree
8069 test_for_singularity (enum tree_code cond_code, tree op0,
8070 tree op1, value_range_t *vr)
8072 tree min = NULL;
8073 tree max = NULL;
8075 /* Extract minimum/maximum values which satisfy the
8076 the conditional as it was written. */
8077 if (cond_code == LE_EXPR || cond_code == LT_EXPR)
8079 /* This should not be negative infinity; there is no overflow
8080 here. */
8081 min = TYPE_MIN_VALUE (TREE_TYPE (op0));
8083 max = op1;
8084 if (cond_code == LT_EXPR && !is_overflow_infinity (max))
8086 tree one = build_int_cst (TREE_TYPE (op0), 1);
8087 max = fold_build2 (MINUS_EXPR, TREE_TYPE (op0), max, one);
8088 if (EXPR_P (max))
8089 TREE_NO_WARNING (max) = 1;
8092 else if (cond_code == GE_EXPR || cond_code == GT_EXPR)
8094 /* This should not be positive infinity; there is no overflow
8095 here. */
8096 max = TYPE_MAX_VALUE (TREE_TYPE (op0));
8098 min = op1;
8099 if (cond_code == GT_EXPR && !is_overflow_infinity (min))
8101 tree one = build_int_cst (TREE_TYPE (op0), 1);
8102 min = fold_build2 (PLUS_EXPR, TREE_TYPE (op0), min, one);
8103 if (EXPR_P (min))
8104 TREE_NO_WARNING (min) = 1;
8108 /* Now refine the minimum and maximum values using any
8109 value range information we have for op0. */
8110 if (min && max)
8112 if (compare_values (vr->min, min) == 1)
8113 min = vr->min;
8114 if (compare_values (vr->max, max) == -1)
8115 max = vr->max;
8117 /* If the new min/max values have converged to a single value,
8118 then there is only one value which can satisfy the condition,
8119 return that value. */
8120 if (operand_equal_p (min, max, 0) && is_gimple_min_invariant (min))
8121 return min;
8123 return NULL;
8126 /* Simplify a conditional using a relational operator to an equality
8127 test if the range information indicates only one value can satisfy
8128 the original conditional. */
8130 static bool
8131 simplify_cond_using_ranges (gimple stmt)
8133 tree op0 = gimple_cond_lhs (stmt);
8134 tree op1 = gimple_cond_rhs (stmt);
8135 enum tree_code cond_code = gimple_cond_code (stmt);
8137 if (cond_code != NE_EXPR
8138 && cond_code != EQ_EXPR
8139 && TREE_CODE (op0) == SSA_NAME
8140 && INTEGRAL_TYPE_P (TREE_TYPE (op0))
8141 && is_gimple_min_invariant (op1))
8143 value_range_t *vr = get_value_range (op0);
8145 /* If we have range information for OP0, then we might be
8146 able to simplify this conditional. */
8147 if (vr->type == VR_RANGE)
8149 tree new_tree = test_for_singularity (cond_code, op0, op1, vr);
8151 if (new_tree)
8153 if (dump_file)
8155 fprintf (dump_file, "Simplified relational ");
8156 print_gimple_stmt (dump_file, stmt, 0, 0);
8157 fprintf (dump_file, " into ");
8160 gimple_cond_set_code (stmt, EQ_EXPR);
8161 gimple_cond_set_lhs (stmt, op0);
8162 gimple_cond_set_rhs (stmt, new_tree);
8164 update_stmt (stmt);
8166 if (dump_file)
8168 print_gimple_stmt (dump_file, stmt, 0, 0);
8169 fprintf (dump_file, "\n");
8172 return true;
8175 /* Try again after inverting the condition. We only deal
8176 with integral types here, so no need to worry about
8177 issues with inverting FP comparisons. */
8178 cond_code = invert_tree_comparison (cond_code, false);
8179 new_tree = test_for_singularity (cond_code, op0, op1, vr);
8181 if (new_tree)
8183 if (dump_file)
8185 fprintf (dump_file, "Simplified relational ");
8186 print_gimple_stmt (dump_file, stmt, 0, 0);
8187 fprintf (dump_file, " into ");
8190 gimple_cond_set_code (stmt, NE_EXPR);
8191 gimple_cond_set_lhs (stmt, op0);
8192 gimple_cond_set_rhs (stmt, new_tree);
8194 update_stmt (stmt);
8196 if (dump_file)
8198 print_gimple_stmt (dump_file, stmt, 0, 0);
8199 fprintf (dump_file, "\n");
8202 return true;
8207 return false;
8210 /* Simplify a switch statement using the value range of the switch
8211 argument. */
8213 static bool
8214 simplify_switch_using_ranges (gimple stmt)
8216 tree op = gimple_switch_index (stmt);
8217 value_range_t *vr;
8218 bool take_default;
8219 edge e;
8220 edge_iterator ei;
8221 size_t i = 0, j = 0, n, n2;
8222 tree vec2;
8223 switch_update su;
8225 if (TREE_CODE (op) == SSA_NAME)
8227 vr = get_value_range (op);
8229 /* We can only handle integer ranges. */
8230 if (vr->type != VR_RANGE
8231 || symbolic_range_p (vr))
8232 return false;
8234 /* Find case label for min/max of the value range. */
8235 take_default = !find_case_label_range (stmt, vr->min, vr->max, &i, &j);
8237 else if (TREE_CODE (op) == INTEGER_CST)
8239 take_default = !find_case_label_index (stmt, 1, op, &i);
8240 if (take_default)
8242 i = 1;
8243 j = 0;
8245 else
8247 j = i;
8250 else
8251 return false;
8253 n = gimple_switch_num_labels (stmt);
8255 /* Bail out if this is just all edges taken. */
8256 if (i == 1
8257 && j == n - 1
8258 && take_default)
8259 return false;
8261 /* Build a new vector of taken case labels. */
8262 vec2 = make_tree_vec (j - i + 1 + (int)take_default);
8263 n2 = 0;
8265 /* Add the default edge, if necessary. */
8266 if (take_default)
8267 TREE_VEC_ELT (vec2, n2++) = gimple_switch_default_label (stmt);
8269 for (; i <= j; ++i, ++n2)
8270 TREE_VEC_ELT (vec2, n2) = gimple_switch_label (stmt, i);
8272 /* Mark needed edges. */
8273 for (i = 0; i < n2; ++i)
8275 e = find_edge (gimple_bb (stmt),
8276 label_to_block (CASE_LABEL (TREE_VEC_ELT (vec2, i))));
8277 e->aux = (void *)-1;
8280 /* Queue not needed edges for later removal. */
8281 FOR_EACH_EDGE (e, ei, gimple_bb (stmt)->succs)
8283 if (e->aux == (void *)-1)
8285 e->aux = NULL;
8286 continue;
8289 if (dump_file && (dump_flags & TDF_DETAILS))
8291 fprintf (dump_file, "removing unreachable case label\n");
8293 VEC_safe_push (edge, heap, to_remove_edges, e);
8294 e->flags &= ~EDGE_EXECUTABLE;
8297 /* And queue an update for the stmt. */
8298 su.stmt = stmt;
8299 su.vec = vec2;
8300 VEC_safe_push (switch_update, heap, to_update_switch_stmts, &su);
8301 return false;
8304 /* Simplify an integral conversion from an SSA name in STMT. */
8306 static bool
8307 simplify_conversion_using_ranges (gimple stmt)
8309 tree innerop, middleop, finaltype;
8310 gimple def_stmt;
8311 value_range_t *innervr;
8312 bool inner_unsigned_p, middle_unsigned_p, final_unsigned_p;
8313 unsigned inner_prec, middle_prec, final_prec;
8314 double_int innermin, innermed, innermax, middlemin, middlemed, middlemax;
8316 finaltype = TREE_TYPE (gimple_assign_lhs (stmt));
8317 if (!INTEGRAL_TYPE_P (finaltype))
8318 return false;
8319 middleop = gimple_assign_rhs1 (stmt);
8320 def_stmt = SSA_NAME_DEF_STMT (middleop);
8321 if (!is_gimple_assign (def_stmt)
8322 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt)))
8323 return false;
8324 innerop = gimple_assign_rhs1 (def_stmt);
8325 if (TREE_CODE (innerop) != SSA_NAME)
8326 return false;
8328 /* Get the value-range of the inner operand. */
8329 innervr = get_value_range (innerop);
8330 if (innervr->type != VR_RANGE
8331 || TREE_CODE (innervr->min) != INTEGER_CST
8332 || TREE_CODE (innervr->max) != INTEGER_CST)
8333 return false;
8335 /* Simulate the conversion chain to check if the result is equal if
8336 the middle conversion is removed. */
8337 innermin = tree_to_double_int (innervr->min);
8338 innermax = tree_to_double_int (innervr->max);
8340 inner_prec = TYPE_PRECISION (TREE_TYPE (innerop));
8341 middle_prec = TYPE_PRECISION (TREE_TYPE (middleop));
8342 final_prec = TYPE_PRECISION (finaltype);
8344 /* If the first conversion is not injective, the second must not
8345 be widening. */
8346 if (double_int_cmp (double_int_sub (innermax, innermin),
8347 double_int_mask (middle_prec), true) > 0
8348 && middle_prec < final_prec)
8349 return false;
8350 /* We also want a medium value so that we can track the effect that
8351 narrowing conversions with sign change have. */
8352 inner_unsigned_p = TYPE_UNSIGNED (TREE_TYPE (innerop));
8353 if (inner_unsigned_p)
8354 innermed = double_int_rshift (double_int_mask (inner_prec),
8355 1, inner_prec, false);
8356 else
8357 innermed = double_int_zero;
8358 if (double_int_cmp (innermin, innermed, inner_unsigned_p) >= 0
8359 || double_int_cmp (innermed, innermax, inner_unsigned_p) >= 0)
8360 innermed = innermin;
8362 middle_unsigned_p = TYPE_UNSIGNED (TREE_TYPE (middleop));
8363 middlemin = double_int_ext (innermin, middle_prec, middle_unsigned_p);
8364 middlemed = double_int_ext (innermed, middle_prec, middle_unsigned_p);
8365 middlemax = double_int_ext (innermax, middle_prec, middle_unsigned_p);
8367 /* Require that the final conversion applied to both the original
8368 and the intermediate range produces the same result. */
8369 final_unsigned_p = TYPE_UNSIGNED (finaltype);
8370 if (!double_int_equal_p (double_int_ext (middlemin,
8371 final_prec, final_unsigned_p),
8372 double_int_ext (innermin,
8373 final_prec, final_unsigned_p))
8374 || !double_int_equal_p (double_int_ext (middlemed,
8375 final_prec, final_unsigned_p),
8376 double_int_ext (innermed,
8377 final_prec, final_unsigned_p))
8378 || !double_int_equal_p (double_int_ext (middlemax,
8379 final_prec, final_unsigned_p),
8380 double_int_ext (innermax,
8381 final_prec, final_unsigned_p)))
8382 return false;
8384 gimple_assign_set_rhs1 (stmt, innerop);
8385 update_stmt (stmt);
8386 return true;
8389 /* Return whether the value range *VR fits in an integer type specified
8390 by PRECISION and UNSIGNED_P. */
8392 static bool
8393 range_fits_type_p (value_range_t *vr, unsigned precision, bool unsigned_p)
8395 tree src_type;
8396 unsigned src_precision;
8397 double_int tem;
8399 /* We can only handle integral and pointer types. */
8400 src_type = TREE_TYPE (vr->min);
8401 if (!INTEGRAL_TYPE_P (src_type)
8402 && !POINTER_TYPE_P (src_type))
8403 return false;
8405 /* An extension is always fine, so is an identity transform. */
8406 src_precision = TYPE_PRECISION (TREE_TYPE (vr->min));
8407 if (src_precision < precision
8408 || (src_precision == precision
8409 && TYPE_UNSIGNED (src_type) == unsigned_p))
8410 return true;
8412 /* Now we can only handle ranges with constant bounds. */
8413 if (vr->type != VR_RANGE
8414 || TREE_CODE (vr->min) != INTEGER_CST
8415 || TREE_CODE (vr->max) != INTEGER_CST)
8416 return false;
8418 /* For precision-preserving sign-changes the MSB of the double-int
8419 has to be clear. */
8420 if (src_precision == precision
8421 && (TREE_INT_CST_HIGH (vr->min) | TREE_INT_CST_HIGH (vr->max)) < 0)
8422 return false;
8424 /* Then we can perform the conversion on both ends and compare
8425 the result for equality. */
8426 tem = double_int_ext (tree_to_double_int (vr->min), precision, unsigned_p);
8427 if (!double_int_equal_p (tree_to_double_int (vr->min), tem))
8428 return false;
8429 tem = double_int_ext (tree_to_double_int (vr->max), precision, unsigned_p);
8430 if (!double_int_equal_p (tree_to_double_int (vr->max), tem))
8431 return false;
8433 return true;
8436 /* Simplify a conversion from integral SSA name to float in STMT. */
8438 static bool
8439 simplify_float_conversion_using_ranges (gimple_stmt_iterator *gsi, gimple stmt)
8441 tree rhs1 = gimple_assign_rhs1 (stmt);
8442 value_range_t *vr = get_value_range (rhs1);
8443 enum machine_mode fltmode = TYPE_MODE (TREE_TYPE (gimple_assign_lhs (stmt)));
8444 enum machine_mode mode;
8445 tree tem;
8446 gimple conv;
8448 /* We can only handle constant ranges. */
8449 if (vr->type != VR_RANGE
8450 || TREE_CODE (vr->min) != INTEGER_CST
8451 || TREE_CODE (vr->max) != INTEGER_CST)
8452 return false;
8454 /* First check if we can use a signed type in place of an unsigned. */
8455 if (TYPE_UNSIGNED (TREE_TYPE (rhs1))
8456 && (can_float_p (fltmode, TYPE_MODE (TREE_TYPE (rhs1)), 0)
8457 != CODE_FOR_nothing)
8458 && range_fits_type_p (vr, GET_MODE_PRECISION
8459 (TYPE_MODE (TREE_TYPE (rhs1))), 0))
8460 mode = TYPE_MODE (TREE_TYPE (rhs1));
8461 /* If we can do the conversion in the current input mode do nothing. */
8462 else if (can_float_p (fltmode, TYPE_MODE (TREE_TYPE (rhs1)),
8463 TYPE_UNSIGNED (TREE_TYPE (rhs1))))
8464 return false;
8465 /* Otherwise search for a mode we can use, starting from the narrowest
8466 integer mode available. */
8467 else
8469 mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
8472 /* If we cannot do a signed conversion to float from mode
8473 or if the value-range does not fit in the signed type
8474 try with a wider mode. */
8475 if (can_float_p (fltmode, mode, 0) != CODE_FOR_nothing
8476 && range_fits_type_p (vr, GET_MODE_PRECISION (mode), 0))
8477 break;
8479 mode = GET_MODE_WIDER_MODE (mode);
8480 /* But do not widen the input. Instead leave that to the
8481 optabs expansion code. */
8482 if (GET_MODE_PRECISION (mode) > TYPE_PRECISION (TREE_TYPE (rhs1)))
8483 return false;
8485 while (mode != VOIDmode);
8486 if (mode == VOIDmode)
8487 return false;
8490 /* It works, insert a truncation or sign-change before the
8491 float conversion. */
8492 tem = create_tmp_var (build_nonstandard_integer_type
8493 (GET_MODE_PRECISION (mode), 0), NULL);
8494 conv = gimple_build_assign_with_ops (NOP_EXPR, tem, rhs1, NULL_TREE);
8495 tem = make_ssa_name (tem, conv);
8496 gimple_assign_set_lhs (conv, tem);
8497 gsi_insert_before (gsi, conv, GSI_SAME_STMT);
8498 gimple_assign_set_rhs1 (stmt, tem);
8499 update_stmt (stmt);
8501 return true;
8504 /* Simplify STMT using ranges if possible. */
8506 static bool
8507 simplify_stmt_using_ranges (gimple_stmt_iterator *gsi)
8509 gimple stmt = gsi_stmt (*gsi);
8510 if (is_gimple_assign (stmt))
8512 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
8513 tree rhs1 = gimple_assign_rhs1 (stmt);
8515 switch (rhs_code)
8517 case EQ_EXPR:
8518 case NE_EXPR:
8519 /* Transform EQ_EXPR, NE_EXPR into BIT_XOR_EXPR or identity
8520 if the RHS is zero or one, and the LHS are known to be boolean
8521 values. */
8522 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
8523 return simplify_truth_ops_using_ranges (gsi, stmt);
8524 break;
8526 /* Transform TRUNC_DIV_EXPR and TRUNC_MOD_EXPR into RSHIFT_EXPR
8527 and BIT_AND_EXPR respectively if the first operand is greater
8528 than zero and the second operand is an exact power of two. */
8529 case TRUNC_DIV_EXPR:
8530 case TRUNC_MOD_EXPR:
8531 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1))
8532 && integer_pow2p (gimple_assign_rhs2 (stmt)))
8533 return simplify_div_or_mod_using_ranges (stmt);
8534 break;
8536 /* Transform ABS (X) into X or -X as appropriate. */
8537 case ABS_EXPR:
8538 if (TREE_CODE (rhs1) == SSA_NAME
8539 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
8540 return simplify_abs_using_ranges (stmt);
8541 break;
8543 case BIT_AND_EXPR:
8544 case BIT_IOR_EXPR:
8545 /* Optimize away BIT_AND_EXPR and BIT_IOR_EXPR
8546 if all the bits being cleared are already cleared or
8547 all the bits being set are already set. */
8548 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
8549 return simplify_bit_ops_using_ranges (gsi, stmt);
8550 break;
8552 CASE_CONVERT:
8553 if (TREE_CODE (rhs1) == SSA_NAME
8554 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
8555 return simplify_conversion_using_ranges (stmt);
8556 break;
8558 case FLOAT_EXPR:
8559 if (TREE_CODE (rhs1) == SSA_NAME
8560 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
8561 return simplify_float_conversion_using_ranges (gsi, stmt);
8562 break;
8564 default:
8565 break;
8568 else if (gimple_code (stmt) == GIMPLE_COND)
8569 return simplify_cond_using_ranges (stmt);
8570 else if (gimple_code (stmt) == GIMPLE_SWITCH)
8571 return simplify_switch_using_ranges (stmt);
8573 return false;
8576 /* If the statement pointed by SI has a predicate whose value can be
8577 computed using the value range information computed by VRP, compute
8578 its value and return true. Otherwise, return false. */
8580 static bool
8581 fold_predicate_in (gimple_stmt_iterator *si)
8583 bool assignment_p = false;
8584 tree val;
8585 gimple stmt = gsi_stmt (*si);
8587 if (is_gimple_assign (stmt)
8588 && TREE_CODE_CLASS (gimple_assign_rhs_code (stmt)) == tcc_comparison)
8590 assignment_p = true;
8591 val = vrp_evaluate_conditional (gimple_assign_rhs_code (stmt),
8592 gimple_assign_rhs1 (stmt),
8593 gimple_assign_rhs2 (stmt),
8594 stmt);
8596 else if (gimple_code (stmt) == GIMPLE_COND)
8597 val = vrp_evaluate_conditional (gimple_cond_code (stmt),
8598 gimple_cond_lhs (stmt),
8599 gimple_cond_rhs (stmt),
8600 stmt);
8601 else
8602 return false;
8604 if (val)
8606 if (assignment_p)
8607 val = fold_convert (gimple_expr_type (stmt), val);
8609 if (dump_file)
8611 fprintf (dump_file, "Folding predicate ");
8612 print_gimple_expr (dump_file, stmt, 0, 0);
8613 fprintf (dump_file, " to ");
8614 print_generic_expr (dump_file, val, 0);
8615 fprintf (dump_file, "\n");
8618 if (is_gimple_assign (stmt))
8619 gimple_assign_set_rhs_from_tree (si, val);
8620 else
8622 gcc_assert (gimple_code (stmt) == GIMPLE_COND);
8623 if (integer_zerop (val))
8624 gimple_cond_make_false (stmt);
8625 else if (integer_onep (val))
8626 gimple_cond_make_true (stmt);
8627 else
8628 gcc_unreachable ();
8631 return true;
8634 return false;
8637 /* Callback for substitute_and_fold folding the stmt at *SI. */
8639 static bool
8640 vrp_fold_stmt (gimple_stmt_iterator *si)
8642 if (fold_predicate_in (si))
8643 return true;
8645 return simplify_stmt_using_ranges (si);
8648 /* Stack of dest,src equivalency pairs that need to be restored after
8649 each attempt to thread a block's incoming edge to an outgoing edge.
8651 A NULL entry is used to mark the end of pairs which need to be
8652 restored. */
8653 static VEC(tree,heap) *stack;
8655 /* A trivial wrapper so that we can present the generic jump threading
8656 code with a simple API for simplifying statements. STMT is the
8657 statement we want to simplify, WITHIN_STMT provides the location
8658 for any overflow warnings. */
8660 static tree
8661 simplify_stmt_for_jump_threading (gimple stmt, gimple within_stmt)
8663 /* We only use VRP information to simplify conditionals. This is
8664 overly conservative, but it's unclear if doing more would be
8665 worth the compile time cost. */
8666 if (gimple_code (stmt) != GIMPLE_COND)
8667 return NULL;
8669 return vrp_evaluate_conditional (gimple_cond_code (stmt),
8670 gimple_cond_lhs (stmt),
8671 gimple_cond_rhs (stmt), within_stmt);
8674 /* Blocks which have more than one predecessor and more than
8675 one successor present jump threading opportunities, i.e.,
8676 when the block is reached from a specific predecessor, we
8677 may be able to determine which of the outgoing edges will
8678 be traversed. When this optimization applies, we are able
8679 to avoid conditionals at runtime and we may expose secondary
8680 optimization opportunities.
8682 This routine is effectively a driver for the generic jump
8683 threading code. It basically just presents the generic code
8684 with edges that may be suitable for jump threading.
8686 Unlike DOM, we do not iterate VRP if jump threading was successful.
8687 While iterating may expose new opportunities for VRP, it is expected
8688 those opportunities would be very limited and the compile time cost
8689 to expose those opportunities would be significant.
8691 As jump threading opportunities are discovered, they are registered
8692 for later realization. */
8694 static void
8695 identify_jump_threads (void)
8697 basic_block bb;
8698 gimple dummy;
8699 int i;
8700 edge e;
8702 /* Ugh. When substituting values earlier in this pass we can
8703 wipe the dominance information. So rebuild the dominator
8704 information as we need it within the jump threading code. */
8705 calculate_dominance_info (CDI_DOMINATORS);
8707 /* We do not allow VRP information to be used for jump threading
8708 across a back edge in the CFG. Otherwise it becomes too
8709 difficult to avoid eliminating loop exit tests. Of course
8710 EDGE_DFS_BACK is not accurate at this time so we have to
8711 recompute it. */
8712 mark_dfs_back_edges ();
8714 /* Do not thread across edges we are about to remove. Just marking
8715 them as EDGE_DFS_BACK will do. */
8716 FOR_EACH_VEC_ELT (edge, to_remove_edges, i, e)
8717 e->flags |= EDGE_DFS_BACK;
8719 /* Allocate our unwinder stack to unwind any temporary equivalences
8720 that might be recorded. */
8721 stack = VEC_alloc (tree, heap, 20);
8723 /* To avoid lots of silly node creation, we create a single
8724 conditional and just modify it in-place when attempting to
8725 thread jumps. */
8726 dummy = gimple_build_cond (EQ_EXPR,
8727 integer_zero_node, integer_zero_node,
8728 NULL, NULL);
8730 /* Walk through all the blocks finding those which present a
8731 potential jump threading opportunity. We could set this up
8732 as a dominator walker and record data during the walk, but
8733 I doubt it's worth the effort for the classes of jump
8734 threading opportunities we are trying to identify at this
8735 point in compilation. */
8736 FOR_EACH_BB (bb)
8738 gimple last;
8740 /* If the generic jump threading code does not find this block
8741 interesting, then there is nothing to do. */
8742 if (! potentially_threadable_block (bb))
8743 continue;
8745 /* We only care about blocks ending in a COND_EXPR. While there
8746 may be some value in handling SWITCH_EXPR here, I doubt it's
8747 terribly important. */
8748 last = gsi_stmt (gsi_last_bb (bb));
8750 /* We're basically looking for a switch or any kind of conditional with
8751 integral or pointer type arguments. Note the type of the second
8752 argument will be the same as the first argument, so no need to
8753 check it explicitly. */
8754 if (gimple_code (last) == GIMPLE_SWITCH
8755 || (gimple_code (last) == GIMPLE_COND
8756 && TREE_CODE (gimple_cond_lhs (last)) == SSA_NAME
8757 && (INTEGRAL_TYPE_P (TREE_TYPE (gimple_cond_lhs (last)))
8758 || POINTER_TYPE_P (TREE_TYPE (gimple_cond_lhs (last))))
8759 && (TREE_CODE (gimple_cond_rhs (last)) == SSA_NAME
8760 || is_gimple_min_invariant (gimple_cond_rhs (last)))))
8762 edge_iterator ei;
8764 /* We've got a block with multiple predecessors and multiple
8765 successors which also ends in a suitable conditional or
8766 switch statement. For each predecessor, see if we can thread
8767 it to a specific successor. */
8768 FOR_EACH_EDGE (e, ei, bb->preds)
8770 /* Do not thread across back edges or abnormal edges
8771 in the CFG. */
8772 if (e->flags & (EDGE_DFS_BACK | EDGE_COMPLEX))
8773 continue;
8775 thread_across_edge (dummy, e, true, &stack,
8776 simplify_stmt_for_jump_threading);
8781 /* We do not actually update the CFG or SSA graphs at this point as
8782 ASSERT_EXPRs are still in the IL and cfg cleanup code does not yet
8783 handle ASSERT_EXPRs gracefully. */
8786 /* We identified all the jump threading opportunities earlier, but could
8787 not transform the CFG at that time. This routine transforms the
8788 CFG and arranges for the dominator tree to be rebuilt if necessary.
8790 Note the SSA graph update will occur during the normal TODO
8791 processing by the pass manager. */
8792 static void
8793 finalize_jump_threads (void)
8795 thread_through_all_blocks (false);
8796 VEC_free (tree, heap, stack);
8800 /* Traverse all the blocks folding conditionals with known ranges. */
8802 static void
8803 vrp_finalize (void)
8805 size_t i;
8807 values_propagated = true;
8809 if (dump_file)
8811 fprintf (dump_file, "\nValue ranges after VRP:\n\n");
8812 dump_all_value_ranges (dump_file);
8813 fprintf (dump_file, "\n");
8816 substitute_and_fold (op_with_constant_singleton_value_range,
8817 vrp_fold_stmt, false);
8819 if (warn_array_bounds)
8820 check_all_array_refs ();
8822 /* We must identify jump threading opportunities before we release
8823 the datastructures built by VRP. */
8824 identify_jump_threads ();
8826 /* Free allocated memory. */
8827 for (i = 0; i < num_vr_values; i++)
8828 if (vr_value[i])
8830 BITMAP_FREE (vr_value[i]->equiv);
8831 free (vr_value[i]);
8834 free (vr_value);
8835 free (vr_phi_edge_counts);
8837 /* So that we can distinguish between VRP data being available
8838 and not available. */
8839 vr_value = NULL;
8840 vr_phi_edge_counts = NULL;
8844 /* Main entry point to VRP (Value Range Propagation). This pass is
8845 loosely based on J. R. C. Patterson, ``Accurate Static Branch
8846 Prediction by Value Range Propagation,'' in SIGPLAN Conference on
8847 Programming Language Design and Implementation, pp. 67-78, 1995.
8848 Also available at http://citeseer.ist.psu.edu/patterson95accurate.html
8850 This is essentially an SSA-CCP pass modified to deal with ranges
8851 instead of constants.
8853 While propagating ranges, we may find that two or more SSA name
8854 have equivalent, though distinct ranges. For instance,
8856 1 x_9 = p_3->a;
8857 2 p_4 = ASSERT_EXPR <p_3, p_3 != 0>
8858 3 if (p_4 == q_2)
8859 4 p_5 = ASSERT_EXPR <p_4, p_4 == q_2>;
8860 5 endif
8861 6 if (q_2)
8863 In the code above, pointer p_5 has range [q_2, q_2], but from the
8864 code we can also determine that p_5 cannot be NULL and, if q_2 had
8865 a non-varying range, p_5's range should also be compatible with it.
8867 These equivalences are created by two expressions: ASSERT_EXPR and
8868 copy operations. Since p_5 is an assertion on p_4, and p_4 was the
8869 result of another assertion, then we can use the fact that p_5 and
8870 p_4 are equivalent when evaluating p_5's range.
8872 Together with value ranges, we also propagate these equivalences
8873 between names so that we can take advantage of information from
8874 multiple ranges when doing final replacement. Note that this
8875 equivalency relation is transitive but not symmetric.
8877 In the example above, p_5 is equivalent to p_4, q_2 and p_3, but we
8878 cannot assert that q_2 is equivalent to p_5 because q_2 may be used
8879 in contexts where that assertion does not hold (e.g., in line 6).
8881 TODO, the main difference between this pass and Patterson's is that
8882 we do not propagate edge probabilities. We only compute whether
8883 edges can be taken or not. That is, instead of having a spectrum
8884 of jump probabilities between 0 and 1, we only deal with 0, 1 and
8885 DON'T KNOW. In the future, it may be worthwhile to propagate
8886 probabilities to aid branch prediction. */
8888 static unsigned int
8889 execute_vrp (void)
8891 int i;
8892 edge e;
8893 switch_update *su;
8895 loop_optimizer_init (LOOPS_NORMAL | LOOPS_HAVE_RECORDED_EXITS);
8896 rewrite_into_loop_closed_ssa (NULL, TODO_update_ssa);
8897 scev_initialize ();
8899 insert_range_assertions ();
8901 to_remove_edges = VEC_alloc (edge, heap, 10);
8902 to_update_switch_stmts = VEC_alloc (switch_update, heap, 5);
8903 threadedge_initialize_values ();
8905 vrp_initialize ();
8906 ssa_propagate (vrp_visit_stmt, vrp_visit_phi_node);
8907 vrp_finalize ();
8909 free_numbers_of_iterations_estimates ();
8911 /* ASSERT_EXPRs must be removed before finalizing jump threads
8912 as finalizing jump threads calls the CFG cleanup code which
8913 does not properly handle ASSERT_EXPRs. */
8914 remove_range_assertions ();
8916 /* If we exposed any new variables, go ahead and put them into
8917 SSA form now, before we handle jump threading. This simplifies
8918 interactions between rewriting of _DECL nodes into SSA form
8919 and rewriting SSA_NAME nodes into SSA form after block
8920 duplication and CFG manipulation. */
8921 update_ssa (TODO_update_ssa);
8923 finalize_jump_threads ();
8925 /* Remove dead edges from SWITCH_EXPR optimization. This leaves the
8926 CFG in a broken state and requires a cfg_cleanup run. */
8927 FOR_EACH_VEC_ELT (edge, to_remove_edges, i, e)
8928 remove_edge (e);
8929 /* Update SWITCH_EXPR case label vector. */
8930 FOR_EACH_VEC_ELT (switch_update, to_update_switch_stmts, i, su)
8932 size_t j;
8933 size_t n = TREE_VEC_LENGTH (su->vec);
8934 tree label;
8935 gimple_switch_set_num_labels (su->stmt, n);
8936 for (j = 0; j < n; j++)
8937 gimple_switch_set_label (su->stmt, j, TREE_VEC_ELT (su->vec, j));
8938 /* As we may have replaced the default label with a regular one
8939 make sure to make it a real default label again. This ensures
8940 optimal expansion. */
8941 label = gimple_switch_default_label (su->stmt);
8942 CASE_LOW (label) = NULL_TREE;
8943 CASE_HIGH (label) = NULL_TREE;
8946 if (VEC_length (edge, to_remove_edges) > 0)
8947 free_dominance_info (CDI_DOMINATORS);
8949 VEC_free (edge, heap, to_remove_edges);
8950 VEC_free (switch_update, heap, to_update_switch_stmts);
8951 threadedge_finalize_values ();
8953 scev_finalize ();
8954 loop_optimizer_finalize ();
8955 return 0;
8958 static bool
8959 gate_vrp (void)
8961 return flag_tree_vrp != 0;
8964 struct gimple_opt_pass pass_vrp =
8967 GIMPLE_PASS,
8968 "vrp", /* name */
8969 gate_vrp, /* gate */
8970 execute_vrp, /* execute */
8971 NULL, /* sub */
8972 NULL, /* next */
8973 0, /* static_pass_number */
8974 TV_TREE_VRP, /* tv_id */
8975 PROP_ssa, /* properties_required */
8976 0, /* properties_provided */
8977 0, /* properties_destroyed */
8978 0, /* todo_flags_start */
8979 TODO_cleanup_cfg
8980 | TODO_update_ssa
8981 | TODO_verify_ssa
8982 | TODO_verify_flow
8983 | TODO_ggc_collect /* todo_flags_finish */