Merge trunk version 193672 into gupc branch.
[official-gcc.git] / gcc / tree-vrp.c
blob7f3e082446a8ded2d0834365abf758fee4e73b19
1 /* Support routines for Value Range Propagation (VRP).
2 Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
3 Free Software Foundation, Inc.
4 Contributed by Diego Novillo <dnovillo@redhat.com>.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "ggc.h"
27 #include "flags.h"
28 #include "tree.h"
29 #include "basic-block.h"
30 #include "tree-flow.h"
31 #include "tree-pass.h"
32 #include "tree-dump.h"
33 #include "gimple-pretty-print.h"
34 #include "diagnostic-core.h"
35 #include "intl.h"
36 #include "cfgloop.h"
37 #include "tree-scalar-evolution.h"
38 #include "tree-ssa-propagate.h"
39 #include "tree-chrec.h"
40 #include "gimple-fold.h"
41 #include "expr.h"
42 #include "optabs.h"
45 /* Type of value ranges. See value_range_d for a description of these
46 types. */
47 enum value_range_type { VR_UNDEFINED, VR_RANGE, VR_ANTI_RANGE, VR_VARYING };
49 /* Range of values that can be associated with an SSA_NAME after VRP
50 has executed. */
51 struct value_range_d
53 /* Lattice value represented by this range. */
54 enum value_range_type type;
56 /* Minimum and maximum values represented by this range. These
57 values should be interpreted as follows:
59 - If TYPE is VR_UNDEFINED or VR_VARYING then MIN and MAX must
60 be NULL.
62 - If TYPE == VR_RANGE then MIN holds the minimum value and
63 MAX holds the maximum value of the range [MIN, MAX].
65 - If TYPE == ANTI_RANGE the variable is known to NOT
66 take any values in the range [MIN, MAX]. */
67 tree min;
68 tree max;
70 /* Set of SSA names whose value ranges are equivalent to this one.
71 This set is only valid when TYPE is VR_RANGE or VR_ANTI_RANGE. */
72 bitmap equiv;
75 typedef struct value_range_d value_range_t;
77 #define VR_INITIALIZER { VR_UNDEFINED, NULL_TREE, NULL_TREE, NULL }
79 /* Set of SSA names found live during the RPO traversal of the function
80 for still active basic-blocks. */
81 static sbitmap *live;
83 /* Return true if the SSA name NAME is live on the edge E. */
85 static bool
86 live_on_edge (edge e, tree name)
88 return (live[e->dest->index]
89 && bitmap_bit_p (live[e->dest->index], SSA_NAME_VERSION (name)));
92 /* Local functions. */
93 static int compare_values (tree val1, tree val2);
94 static int compare_values_warnv (tree val1, tree val2, bool *);
95 static void vrp_meet (value_range_t *, value_range_t *);
96 static void vrp_intersect_ranges (value_range_t *, value_range_t *);
97 static tree vrp_evaluate_conditional_warnv_with_ops (enum tree_code,
98 tree, tree, bool, bool *,
99 bool *);
101 /* Location information for ASSERT_EXPRs. Each instance of this
102 structure describes an ASSERT_EXPR for an SSA name. Since a single
103 SSA name may have more than one assertion associated with it, these
104 locations are kept in a linked list attached to the corresponding
105 SSA name. */
106 struct assert_locus_d
108 /* Basic block where the assertion would be inserted. */
109 basic_block bb;
111 /* Some assertions need to be inserted on an edge (e.g., assertions
112 generated by COND_EXPRs). In those cases, BB will be NULL. */
113 edge e;
115 /* Pointer to the statement that generated this assertion. */
116 gimple_stmt_iterator si;
118 /* Predicate code for the ASSERT_EXPR. Must be COMPARISON_CLASS_P. */
119 enum tree_code comp_code;
121 /* Value being compared against. */
122 tree val;
124 /* Expression to compare. */
125 tree expr;
127 /* Next node in the linked list. */
128 struct assert_locus_d *next;
131 typedef struct assert_locus_d *assert_locus_t;
133 /* If bit I is present, it means that SSA name N_i has a list of
134 assertions that should be inserted in the IL. */
135 static bitmap need_assert_for;
137 /* Array of locations lists where to insert assertions. ASSERTS_FOR[I]
138 holds a list of ASSERT_LOCUS_T nodes that describe where
139 ASSERT_EXPRs for SSA name N_I should be inserted. */
140 static assert_locus_t *asserts_for;
142 /* Value range array. After propagation, VR_VALUE[I] holds the range
143 of values that SSA name N_I may take. */
144 static unsigned num_vr_values;
145 static value_range_t **vr_value;
146 static bool values_propagated;
148 /* For a PHI node which sets SSA name N_I, VR_COUNTS[I] holds the
149 number of executable edges we saw the last time we visited the
150 node. */
151 static int *vr_phi_edge_counts;
153 typedef struct {
154 gimple stmt;
155 tree vec;
156 } switch_update;
158 static vec<edge> to_remove_edges;
159 static vec<switch_update> to_update_switch_stmts;
162 /* Return the maximum value for TYPE. */
164 static inline tree
165 vrp_val_max (const_tree type)
167 if (!INTEGRAL_TYPE_P (type))
168 return NULL_TREE;
170 return TYPE_MAX_VALUE (type);
173 /* Return the minimum value for TYPE. */
175 static inline tree
176 vrp_val_min (const_tree type)
178 if (!INTEGRAL_TYPE_P (type))
179 return NULL_TREE;
181 return TYPE_MIN_VALUE (type);
184 /* Return whether VAL is equal to the maximum value of its type. This
185 will be true for a positive overflow infinity. We can't do a
186 simple equality comparison with TYPE_MAX_VALUE because C typedefs
187 and Ada subtypes can produce types whose TYPE_MAX_VALUE is not ==
188 to the integer constant with the same value in the type. */
190 static inline bool
191 vrp_val_is_max (const_tree val)
193 tree type_max = vrp_val_max (TREE_TYPE (val));
194 return (val == type_max
195 || (type_max != NULL_TREE
196 && operand_equal_p (val, type_max, 0)));
199 /* Return whether VAL is equal to the minimum value of its type. This
200 will be true for a negative overflow infinity. */
202 static inline bool
203 vrp_val_is_min (const_tree val)
205 tree type_min = vrp_val_min (TREE_TYPE (val));
206 return (val == type_min
207 || (type_min != NULL_TREE
208 && operand_equal_p (val, type_min, 0)));
212 /* Return whether TYPE should use an overflow infinity distinct from
213 TYPE_{MIN,MAX}_VALUE. We use an overflow infinity value to
214 represent a signed overflow during VRP computations. An infinity
215 is distinct from a half-range, which will go from some number to
216 TYPE_{MIN,MAX}_VALUE. */
218 static inline bool
219 needs_overflow_infinity (const_tree type)
221 return INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_WRAPS (type);
224 /* Return whether TYPE can support our overflow infinity
225 representation: we use the TREE_OVERFLOW flag, which only exists
226 for constants. If TYPE doesn't support this, we don't optimize
227 cases which would require signed overflow--we drop them to
228 VARYING. */
230 static inline bool
231 supports_overflow_infinity (const_tree type)
233 tree min = vrp_val_min (type), max = vrp_val_max (type);
234 #ifdef ENABLE_CHECKING
235 gcc_assert (needs_overflow_infinity (type));
236 #endif
237 return (min != NULL_TREE
238 && CONSTANT_CLASS_P (min)
239 && max != NULL_TREE
240 && CONSTANT_CLASS_P (max));
243 /* VAL is the maximum or minimum value of a type. Return a
244 corresponding overflow infinity. */
246 static inline tree
247 make_overflow_infinity (tree val)
249 gcc_checking_assert (val != NULL_TREE && CONSTANT_CLASS_P (val));
250 val = copy_node (val);
251 TREE_OVERFLOW (val) = 1;
252 return val;
255 /* Return a negative overflow infinity for TYPE. */
257 static inline tree
258 negative_overflow_infinity (tree type)
260 gcc_checking_assert (supports_overflow_infinity (type));
261 return make_overflow_infinity (vrp_val_min (type));
264 /* Return a positive overflow infinity for TYPE. */
266 static inline tree
267 positive_overflow_infinity (tree type)
269 gcc_checking_assert (supports_overflow_infinity (type));
270 return make_overflow_infinity (vrp_val_max (type));
273 /* Return whether VAL is a negative overflow infinity. */
275 static inline bool
276 is_negative_overflow_infinity (const_tree val)
278 return (needs_overflow_infinity (TREE_TYPE (val))
279 && CONSTANT_CLASS_P (val)
280 && TREE_OVERFLOW (val)
281 && vrp_val_is_min (val));
284 /* Return whether VAL is a positive overflow infinity. */
286 static inline bool
287 is_positive_overflow_infinity (const_tree val)
289 return (needs_overflow_infinity (TREE_TYPE (val))
290 && CONSTANT_CLASS_P (val)
291 && TREE_OVERFLOW (val)
292 && vrp_val_is_max (val));
295 /* Return whether VAL is a positive or negative overflow infinity. */
297 static inline bool
298 is_overflow_infinity (const_tree val)
300 return (needs_overflow_infinity (TREE_TYPE (val))
301 && CONSTANT_CLASS_P (val)
302 && TREE_OVERFLOW (val)
303 && (vrp_val_is_min (val) || vrp_val_is_max (val)));
306 /* Return whether STMT has a constant rhs that is_overflow_infinity. */
308 static inline bool
309 stmt_overflow_infinity (gimple stmt)
311 if (is_gimple_assign (stmt)
312 && get_gimple_rhs_class (gimple_assign_rhs_code (stmt)) ==
313 GIMPLE_SINGLE_RHS)
314 return is_overflow_infinity (gimple_assign_rhs1 (stmt));
315 return false;
318 /* If VAL is now an overflow infinity, return VAL. Otherwise, return
319 the same value with TREE_OVERFLOW clear. This can be used to avoid
320 confusing a regular value with an overflow value. */
322 static inline tree
323 avoid_overflow_infinity (tree val)
325 if (!is_overflow_infinity (val))
326 return val;
328 if (vrp_val_is_max (val))
329 return vrp_val_max (TREE_TYPE (val));
330 else
332 gcc_checking_assert (vrp_val_is_min (val));
333 return vrp_val_min (TREE_TYPE (val));
338 /* Return true if ARG is marked with the nonnull attribute in the
339 current function signature. */
341 static bool
342 nonnull_arg_p (const_tree arg)
344 tree t, attrs, fntype;
345 unsigned HOST_WIDE_INT arg_num;
347 gcc_assert (TREE_CODE (arg) == PARM_DECL && POINTER_TYPE_P (TREE_TYPE (arg)));
349 /* The static chain decl is always non null. */
350 if (arg == cfun->static_chain_decl)
351 return true;
353 fntype = TREE_TYPE (current_function_decl);
354 for (attrs = TYPE_ATTRIBUTES (fntype); attrs; attrs = TREE_CHAIN (attrs))
356 attrs = lookup_attribute ("nonnull", attrs);
358 /* If "nonnull" wasn't specified, we know nothing about the argument. */
359 if (attrs == NULL_TREE)
360 return false;
362 /* If "nonnull" applies to all the arguments, then ARG is non-null. */
363 if (TREE_VALUE (attrs) == NULL_TREE)
364 return true;
366 /* Get the position number for ARG in the function signature. */
367 for (arg_num = 1, t = DECL_ARGUMENTS (current_function_decl);
369 t = DECL_CHAIN (t), arg_num++)
371 if (t == arg)
372 break;
375 gcc_assert (t == arg);
377 /* Now see if ARG_NUM is mentioned in the nonnull list. */
378 for (t = TREE_VALUE (attrs); t; t = TREE_CHAIN (t))
380 if (compare_tree_int (TREE_VALUE (t), arg_num) == 0)
381 return true;
385 return false;
389 /* Set value range VR to VR_UNDEFINED. */
391 static inline void
392 set_value_range_to_undefined (value_range_t *vr)
394 vr->type = VR_UNDEFINED;
395 vr->min = vr->max = NULL_TREE;
396 if (vr->equiv)
397 bitmap_clear (vr->equiv);
401 /* Set value range VR to VR_VARYING. */
403 static inline void
404 set_value_range_to_varying (value_range_t *vr)
406 vr->type = VR_VARYING;
407 vr->min = vr->max = NULL_TREE;
408 if (vr->equiv)
409 bitmap_clear (vr->equiv);
413 /* Set value range VR to {T, MIN, MAX, EQUIV}. */
415 static void
416 set_value_range (value_range_t *vr, enum value_range_type t, tree min,
417 tree max, bitmap equiv)
419 #if defined ENABLE_CHECKING
420 /* Check the validity of the range. */
421 if (t == VR_RANGE || t == VR_ANTI_RANGE)
423 int cmp;
425 gcc_assert (min && max);
427 if (INTEGRAL_TYPE_P (TREE_TYPE (min)) && t == VR_ANTI_RANGE)
428 gcc_assert (!vrp_val_is_min (min) || !vrp_val_is_max (max));
430 cmp = compare_values (min, max);
431 gcc_assert (cmp == 0 || cmp == -1 || cmp == -2);
433 if (needs_overflow_infinity (TREE_TYPE (min)))
434 gcc_assert (!is_overflow_infinity (min)
435 || !is_overflow_infinity (max));
438 if (t == VR_UNDEFINED || t == VR_VARYING)
439 gcc_assert (min == NULL_TREE && max == NULL_TREE);
441 if (t == VR_UNDEFINED || t == VR_VARYING)
442 gcc_assert (equiv == NULL || bitmap_empty_p (equiv));
443 #endif
445 vr->type = t;
446 vr->min = min;
447 vr->max = max;
449 /* Since updating the equivalence set involves deep copying the
450 bitmaps, only do it if absolutely necessary. */
451 if (vr->equiv == NULL
452 && equiv != NULL)
453 vr->equiv = BITMAP_ALLOC (NULL);
455 if (equiv != vr->equiv)
457 if (equiv && !bitmap_empty_p (equiv))
458 bitmap_copy (vr->equiv, equiv);
459 else
460 bitmap_clear (vr->equiv);
465 /* Set value range VR to the canonical form of {T, MIN, MAX, EQUIV}.
466 This means adjusting T, MIN and MAX representing the case of a
467 wrapping range with MAX < MIN covering [MIN, type_max] U [type_min, MAX]
468 as anti-rage ~[MAX+1, MIN-1]. Likewise for wrapping anti-ranges.
469 In corner cases where MAX+1 or MIN-1 wraps this will fall back
470 to varying.
471 This routine exists to ease canonicalization in the case where we
472 extract ranges from var + CST op limit. */
474 static void
475 set_and_canonicalize_value_range (value_range_t *vr, enum value_range_type t,
476 tree min, tree max, bitmap equiv)
478 /* Use the canonical setters for VR_UNDEFINED and VR_VARYING. */
479 if (t == VR_UNDEFINED)
481 set_value_range_to_undefined (vr);
482 return;
484 else if (t == VR_VARYING)
486 set_value_range_to_varying (vr);
487 return;
490 /* Nothing to canonicalize for symbolic ranges. */
491 if (TREE_CODE (min) != INTEGER_CST
492 || TREE_CODE (max) != INTEGER_CST)
494 set_value_range (vr, t, min, max, equiv);
495 return;
498 /* Wrong order for min and max, to swap them and the VR type we need
499 to adjust them. */
500 if (tree_int_cst_lt (max, min))
502 tree one, tmp;
504 /* For one bit precision if max < min, then the swapped
505 range covers all values, so for VR_RANGE it is varying and
506 for VR_ANTI_RANGE empty range, so drop to varying as well. */
507 if (TYPE_PRECISION (TREE_TYPE (min)) == 1)
509 set_value_range_to_varying (vr);
510 return;
513 one = build_int_cst (TREE_TYPE (min), 1);
514 tmp = int_const_binop (PLUS_EXPR, max, one);
515 max = int_const_binop (MINUS_EXPR, min, one);
516 min = tmp;
518 /* There's one corner case, if we had [C+1, C] before we now have
519 that again. But this represents an empty value range, so drop
520 to varying in this case. */
521 if (tree_int_cst_lt (max, min))
523 set_value_range_to_varying (vr);
524 return;
527 t = t == VR_RANGE ? VR_ANTI_RANGE : VR_RANGE;
530 /* Anti-ranges that can be represented as ranges should be so. */
531 if (t == VR_ANTI_RANGE)
533 bool is_min = vrp_val_is_min (min);
534 bool is_max = vrp_val_is_max (max);
536 if (is_min && is_max)
538 /* We cannot deal with empty ranges, drop to varying.
539 ??? This could be VR_UNDEFINED instead. */
540 set_value_range_to_varying (vr);
541 return;
543 else if (TYPE_PRECISION (TREE_TYPE (min)) == 1
544 && !TYPE_UNSIGNED (TREE_TYPE (min))
545 && (is_min || is_max))
547 /* For signed 1-bit precision, one is not in-range and
548 thus adding/subtracting it would result in overflows. */
549 if (operand_equal_p (min, max, 0))
551 min = max = is_min ? vrp_val_max (TREE_TYPE (min))
552 : vrp_val_min (TREE_TYPE (min));
553 t = VR_RANGE;
555 else
557 set_value_range_to_varying (vr);
558 return;
561 else if (is_min
562 /* As a special exception preserve non-null ranges. */
563 && !(TYPE_UNSIGNED (TREE_TYPE (min))
564 && integer_zerop (max)))
566 tree one = build_int_cst (TREE_TYPE (max), 1);
567 min = int_const_binop (PLUS_EXPR, max, one);
568 max = vrp_val_max (TREE_TYPE (max));
569 t = VR_RANGE;
571 else if (is_max)
573 tree one = build_int_cst (TREE_TYPE (min), 1);
574 max = int_const_binop (MINUS_EXPR, min, one);
575 min = vrp_val_min (TREE_TYPE (min));
576 t = VR_RANGE;
580 /* Drop [-INF(OVF), +INF(OVF)] to varying. */
581 if (needs_overflow_infinity (TREE_TYPE (min))
582 && is_overflow_infinity (min)
583 && is_overflow_infinity (max))
585 set_value_range_to_varying (vr);
586 return;
589 set_value_range (vr, t, min, max, equiv);
592 /* Copy value range FROM into value range TO. */
594 static inline void
595 copy_value_range (value_range_t *to, value_range_t *from)
597 set_value_range (to, from->type, from->min, from->max, from->equiv);
600 /* Set value range VR to a single value. This function is only called
601 with values we get from statements, and exists to clear the
602 TREE_OVERFLOW flag so that we don't think we have an overflow
603 infinity when we shouldn't. */
605 static inline void
606 set_value_range_to_value (value_range_t *vr, tree val, bitmap equiv)
608 gcc_assert (is_gimple_min_invariant (val));
609 val = avoid_overflow_infinity (val);
610 set_value_range (vr, VR_RANGE, val, val, equiv);
613 /* Set value range VR to a non-negative range of type TYPE.
614 OVERFLOW_INFINITY indicates whether to use an overflow infinity
615 rather than TYPE_MAX_VALUE; this should be true if we determine
616 that the range is nonnegative based on the assumption that signed
617 overflow does not occur. */
619 static inline void
620 set_value_range_to_nonnegative (value_range_t *vr, tree type,
621 bool overflow_infinity)
623 tree zero;
625 if (overflow_infinity && !supports_overflow_infinity (type))
627 set_value_range_to_varying (vr);
628 return;
631 zero = build_int_cst (type, 0);
632 set_value_range (vr, VR_RANGE, zero,
633 (overflow_infinity
634 ? positive_overflow_infinity (type)
635 : TYPE_MAX_VALUE (type)),
636 vr->equiv);
639 /* Set value range VR to a non-NULL range of type TYPE. */
641 static inline void
642 set_value_range_to_nonnull (value_range_t *vr, tree type)
644 tree zero = build_int_cst (type, 0);
645 set_value_range (vr, VR_ANTI_RANGE, zero, zero, vr->equiv);
649 /* Set value range VR to a NULL range of type TYPE. */
651 static inline void
652 set_value_range_to_null (value_range_t *vr, tree type)
654 set_value_range_to_value (vr, build_int_cst (type, 0), vr->equiv);
658 /* Set value range VR to a range of a truthvalue of type TYPE. */
660 static inline void
661 set_value_range_to_truthvalue (value_range_t *vr, tree type)
663 if (TYPE_PRECISION (type) == 1)
664 set_value_range_to_varying (vr);
665 else
666 set_value_range (vr, VR_RANGE,
667 build_int_cst (type, 0), build_int_cst (type, 1),
668 vr->equiv);
672 /* If abs (min) < abs (max), set VR to [-max, max], if
673 abs (min) >= abs (max), set VR to [-min, min]. */
675 static void
676 abs_extent_range (value_range_t *vr, tree min, tree max)
678 int cmp;
680 gcc_assert (TREE_CODE (min) == INTEGER_CST);
681 gcc_assert (TREE_CODE (max) == INTEGER_CST);
682 gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (min)));
683 gcc_assert (!TYPE_UNSIGNED (TREE_TYPE (min)));
684 min = fold_unary (ABS_EXPR, TREE_TYPE (min), min);
685 max = fold_unary (ABS_EXPR, TREE_TYPE (max), max);
686 if (TREE_OVERFLOW (min) || TREE_OVERFLOW (max))
688 set_value_range_to_varying (vr);
689 return;
691 cmp = compare_values (min, max);
692 if (cmp == -1)
693 min = fold_unary (NEGATE_EXPR, TREE_TYPE (min), max);
694 else if (cmp == 0 || cmp == 1)
696 max = min;
697 min = fold_unary (NEGATE_EXPR, TREE_TYPE (min), min);
699 else
701 set_value_range_to_varying (vr);
702 return;
704 set_and_canonicalize_value_range (vr, VR_RANGE, min, max, NULL);
708 /* Return value range information for VAR.
710 If we have no values ranges recorded (ie, VRP is not running), then
711 return NULL. Otherwise create an empty range if none existed for VAR. */
713 static value_range_t *
714 get_value_range (const_tree var)
716 static const struct value_range_d vr_const_varying
717 = { VR_VARYING, NULL_TREE, NULL_TREE, NULL };
718 value_range_t *vr;
719 tree sym;
720 unsigned ver = SSA_NAME_VERSION (var);
722 /* If we have no recorded ranges, then return NULL. */
723 if (! vr_value)
724 return NULL;
726 /* If we query the range for a new SSA name return an unmodifiable VARYING.
727 We should get here at most from the substitute-and-fold stage which
728 will never try to change values. */
729 if (ver >= num_vr_values)
730 return CONST_CAST (value_range_t *, &vr_const_varying);
732 vr = vr_value[ver];
733 if (vr)
734 return vr;
736 /* After propagation finished do not allocate new value-ranges. */
737 if (values_propagated)
738 return CONST_CAST (value_range_t *, &vr_const_varying);
740 /* Create a default value range. */
741 vr_value[ver] = vr = XCNEW (value_range_t);
743 /* Defer allocating the equivalence set. */
744 vr->equiv = NULL;
746 /* If VAR is a default definition of a parameter, the variable can
747 take any value in VAR's type. */
748 if (SSA_NAME_IS_DEFAULT_DEF (var))
750 sym = SSA_NAME_VAR (var);
751 if (TREE_CODE (sym) == PARM_DECL)
753 /* Try to use the "nonnull" attribute to create ~[0, 0]
754 anti-ranges for pointers. Note that this is only valid with
755 default definitions of PARM_DECLs. */
756 if (POINTER_TYPE_P (TREE_TYPE (sym))
757 && nonnull_arg_p (sym))
758 set_value_range_to_nonnull (vr, TREE_TYPE (sym));
759 else
760 set_value_range_to_varying (vr);
762 else if (TREE_CODE (sym) == RESULT_DECL
763 && DECL_BY_REFERENCE (sym))
764 set_value_range_to_nonnull (vr, TREE_TYPE (sym));
767 return vr;
770 /* Return true, if VAL1 and VAL2 are equal values for VRP purposes. */
772 static inline bool
773 vrp_operand_equal_p (const_tree val1, const_tree val2)
775 if (val1 == val2)
776 return true;
777 if (!val1 || !val2 || !operand_equal_p (val1, val2, 0))
778 return false;
779 if (is_overflow_infinity (val1))
780 return is_overflow_infinity (val2);
781 return true;
784 /* Return true, if the bitmaps B1 and B2 are equal. */
786 static inline bool
787 vrp_bitmap_equal_p (const_bitmap b1, const_bitmap b2)
789 return (b1 == b2
790 || ((!b1 || bitmap_empty_p (b1))
791 && (!b2 || bitmap_empty_p (b2)))
792 || (b1 && b2
793 && bitmap_equal_p (b1, b2)));
796 /* Update the value range and equivalence set for variable VAR to
797 NEW_VR. Return true if NEW_VR is different from VAR's previous
798 value.
800 NOTE: This function assumes that NEW_VR is a temporary value range
801 object created for the sole purpose of updating VAR's range. The
802 storage used by the equivalence set from NEW_VR will be freed by
803 this function. Do not call update_value_range when NEW_VR
804 is the range object associated with another SSA name. */
806 static inline bool
807 update_value_range (const_tree var, value_range_t *new_vr)
809 value_range_t *old_vr;
810 bool is_new;
812 /* Update the value range, if necessary. */
813 old_vr = get_value_range (var);
814 is_new = old_vr->type != new_vr->type
815 || !vrp_operand_equal_p (old_vr->min, new_vr->min)
816 || !vrp_operand_equal_p (old_vr->max, new_vr->max)
817 || !vrp_bitmap_equal_p (old_vr->equiv, new_vr->equiv);
819 if (is_new)
821 /* Do not allow transitions up the lattice. The following
822 is slightly more awkward than just new_vr->type < old_vr->type
823 because VR_RANGE and VR_ANTI_RANGE need to be considered
824 the same. We may not have is_new when transitioning to
825 UNDEFINED or from VARYING. */
826 if (new_vr->type == VR_UNDEFINED
827 || old_vr->type == VR_VARYING)
828 set_value_range_to_varying (old_vr);
829 else
830 set_value_range (old_vr, new_vr->type, new_vr->min, new_vr->max,
831 new_vr->equiv);
834 BITMAP_FREE (new_vr->equiv);
836 return is_new;
840 /* Add VAR and VAR's equivalence set to EQUIV. This is the central
841 point where equivalence processing can be turned on/off. */
843 static void
844 add_equivalence (bitmap *equiv, const_tree var)
846 unsigned ver = SSA_NAME_VERSION (var);
847 value_range_t *vr = vr_value[ver];
849 if (*equiv == NULL)
850 *equiv = BITMAP_ALLOC (NULL);
851 bitmap_set_bit (*equiv, ver);
852 if (vr && vr->equiv)
853 bitmap_ior_into (*equiv, vr->equiv);
857 /* Return true if VR is ~[0, 0]. */
859 static inline bool
860 range_is_nonnull (value_range_t *vr)
862 return vr->type == VR_ANTI_RANGE
863 && integer_zerop (vr->min)
864 && integer_zerop (vr->max);
868 /* Return true if VR is [0, 0]. */
870 static inline bool
871 range_is_null (value_range_t *vr)
873 return vr->type == VR_RANGE
874 && integer_zerop (vr->min)
875 && integer_zerop (vr->max);
878 /* Return true if max and min of VR are INTEGER_CST. It's not necessary
879 a singleton. */
881 static inline bool
882 range_int_cst_p (value_range_t *vr)
884 return (vr->type == VR_RANGE
885 && TREE_CODE (vr->max) == INTEGER_CST
886 && TREE_CODE (vr->min) == INTEGER_CST);
889 /* Return true if VR is a INTEGER_CST singleton. */
891 static inline bool
892 range_int_cst_singleton_p (value_range_t *vr)
894 return (range_int_cst_p (vr)
895 && !TREE_OVERFLOW (vr->min)
896 && !TREE_OVERFLOW (vr->max)
897 && tree_int_cst_equal (vr->min, vr->max));
900 /* Return true if value range VR involves at least one symbol. */
902 static inline bool
903 symbolic_range_p (value_range_t *vr)
905 return (!is_gimple_min_invariant (vr->min)
906 || !is_gimple_min_invariant (vr->max));
909 /* Return true if value range VR uses an overflow infinity. */
911 static inline bool
912 overflow_infinity_range_p (value_range_t *vr)
914 return (vr->type == VR_RANGE
915 && (is_overflow_infinity (vr->min)
916 || is_overflow_infinity (vr->max)));
919 /* Return false if we can not make a valid comparison based on VR;
920 this will be the case if it uses an overflow infinity and overflow
921 is not undefined (i.e., -fno-strict-overflow is in effect).
922 Otherwise return true, and set *STRICT_OVERFLOW_P to true if VR
923 uses an overflow infinity. */
925 static bool
926 usable_range_p (value_range_t *vr, bool *strict_overflow_p)
928 gcc_assert (vr->type == VR_RANGE);
929 if (is_overflow_infinity (vr->min))
931 *strict_overflow_p = true;
932 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (vr->min)))
933 return false;
935 if (is_overflow_infinity (vr->max))
937 *strict_overflow_p = true;
938 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (vr->max)))
939 return false;
941 return true;
945 /* Return true if the result of assignment STMT is know to be non-negative.
946 If the return value is based on the assumption that signed overflow is
947 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
948 *STRICT_OVERFLOW_P.*/
950 static bool
951 gimple_assign_nonnegative_warnv_p (gimple stmt, bool *strict_overflow_p)
953 enum tree_code code = gimple_assign_rhs_code (stmt);
954 switch (get_gimple_rhs_class (code))
956 case GIMPLE_UNARY_RHS:
957 return tree_unary_nonnegative_warnv_p (gimple_assign_rhs_code (stmt),
958 gimple_expr_type (stmt),
959 gimple_assign_rhs1 (stmt),
960 strict_overflow_p);
961 case GIMPLE_BINARY_RHS:
962 return tree_binary_nonnegative_warnv_p (gimple_assign_rhs_code (stmt),
963 gimple_expr_type (stmt),
964 gimple_assign_rhs1 (stmt),
965 gimple_assign_rhs2 (stmt),
966 strict_overflow_p);
967 case GIMPLE_TERNARY_RHS:
968 return false;
969 case GIMPLE_SINGLE_RHS:
970 return tree_single_nonnegative_warnv_p (gimple_assign_rhs1 (stmt),
971 strict_overflow_p);
972 case GIMPLE_INVALID_RHS:
973 gcc_unreachable ();
974 default:
975 gcc_unreachable ();
979 /* Return true if return value of call STMT is know to be non-negative.
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_call_nonnegative_warnv_p (gimple stmt, bool *strict_overflow_p)
987 tree arg0 = gimple_call_num_args (stmt) > 0 ?
988 gimple_call_arg (stmt, 0) : NULL_TREE;
989 tree arg1 = gimple_call_num_args (stmt) > 1 ?
990 gimple_call_arg (stmt, 1) : NULL_TREE;
992 return tree_call_nonnegative_warnv_p (gimple_expr_type (stmt),
993 gimple_call_fndecl (stmt),
994 arg0,
995 arg1,
996 strict_overflow_p);
999 /* Return true if STMT is know to to compute a non-negative value.
1000 If the return value is based on the assumption that signed overflow is
1001 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1002 *STRICT_OVERFLOW_P.*/
1004 static bool
1005 gimple_stmt_nonnegative_warnv_p (gimple stmt, bool *strict_overflow_p)
1007 switch (gimple_code (stmt))
1009 case GIMPLE_ASSIGN:
1010 return gimple_assign_nonnegative_warnv_p (stmt, strict_overflow_p);
1011 case GIMPLE_CALL:
1012 return gimple_call_nonnegative_warnv_p (stmt, strict_overflow_p);
1013 default:
1014 gcc_unreachable ();
1018 /* Return true if the result of assignment STMT is know to be non-zero.
1019 If the return value is based on the assumption that signed overflow is
1020 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1021 *STRICT_OVERFLOW_P.*/
1023 static bool
1024 gimple_assign_nonzero_warnv_p (gimple stmt, bool *strict_overflow_p)
1026 enum tree_code code = gimple_assign_rhs_code (stmt);
1027 switch (get_gimple_rhs_class (code))
1029 case GIMPLE_UNARY_RHS:
1030 return tree_unary_nonzero_warnv_p (gimple_assign_rhs_code (stmt),
1031 gimple_expr_type (stmt),
1032 gimple_assign_rhs1 (stmt),
1033 strict_overflow_p);
1034 case GIMPLE_BINARY_RHS:
1035 return tree_binary_nonzero_warnv_p (gimple_assign_rhs_code (stmt),
1036 gimple_expr_type (stmt),
1037 gimple_assign_rhs1 (stmt),
1038 gimple_assign_rhs2 (stmt),
1039 strict_overflow_p);
1040 case GIMPLE_TERNARY_RHS:
1041 return false;
1042 case GIMPLE_SINGLE_RHS:
1043 return tree_single_nonzero_warnv_p (gimple_assign_rhs1 (stmt),
1044 strict_overflow_p);
1045 case GIMPLE_INVALID_RHS:
1046 gcc_unreachable ();
1047 default:
1048 gcc_unreachable ();
1052 /* Return true if STMT is know to to compute a non-zero value.
1053 If the return value is based on the assumption that signed overflow is
1054 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1055 *STRICT_OVERFLOW_P.*/
1057 static bool
1058 gimple_stmt_nonzero_warnv_p (gimple stmt, bool *strict_overflow_p)
1060 switch (gimple_code (stmt))
1062 case GIMPLE_ASSIGN:
1063 return gimple_assign_nonzero_warnv_p (stmt, strict_overflow_p);
1064 case GIMPLE_CALL:
1065 return gimple_alloca_call_p (stmt);
1066 default:
1067 gcc_unreachable ();
1071 /* Like tree_expr_nonzero_warnv_p, but this function uses value ranges
1072 obtained so far. */
1074 static bool
1075 vrp_stmt_computes_nonzero (gimple stmt, bool *strict_overflow_p)
1077 if (gimple_stmt_nonzero_warnv_p (stmt, strict_overflow_p))
1078 return true;
1080 /* If we have an expression of the form &X->a, then the expression
1081 is nonnull if X is nonnull. */
1082 if (is_gimple_assign (stmt)
1083 && gimple_assign_rhs_code (stmt) == ADDR_EXPR)
1085 tree expr = gimple_assign_rhs1 (stmt);
1086 tree base = get_base_address (TREE_OPERAND (expr, 0));
1088 if (base != NULL_TREE
1089 && TREE_CODE (base) == MEM_REF
1090 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
1092 value_range_t *vr = get_value_range (TREE_OPERAND (base, 0));
1093 if (range_is_nonnull (vr))
1094 return true;
1098 return false;
1101 /* Returns true if EXPR is a valid value (as expected by compare_values) --
1102 a gimple invariant, or SSA_NAME +- CST. */
1104 static bool
1105 valid_value_p (tree expr)
1107 if (TREE_CODE (expr) == SSA_NAME)
1108 return true;
1110 if (TREE_CODE (expr) == PLUS_EXPR
1111 || TREE_CODE (expr) == MINUS_EXPR)
1112 return (TREE_CODE (TREE_OPERAND (expr, 0)) == SSA_NAME
1113 && TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST);
1115 return is_gimple_min_invariant (expr);
1118 /* Return
1119 1 if VAL < VAL2
1120 0 if !(VAL < VAL2)
1121 -2 if those are incomparable. */
1122 static inline int
1123 operand_less_p (tree val, tree val2)
1125 /* LT is folded faster than GE and others. Inline the common case. */
1126 if (TREE_CODE (val) == INTEGER_CST && TREE_CODE (val2) == INTEGER_CST)
1128 if (TYPE_UNSIGNED (TREE_TYPE (val)))
1129 return INT_CST_LT_UNSIGNED (val, val2);
1130 else
1132 if (INT_CST_LT (val, val2))
1133 return 1;
1136 else
1138 tree tcmp;
1140 fold_defer_overflow_warnings ();
1142 tcmp = fold_binary_to_constant (LT_EXPR, boolean_type_node, val, val2);
1144 fold_undefer_and_ignore_overflow_warnings ();
1146 if (!tcmp
1147 || TREE_CODE (tcmp) != INTEGER_CST)
1148 return -2;
1150 if (!integer_zerop (tcmp))
1151 return 1;
1154 /* val >= val2, not considering overflow infinity. */
1155 if (is_negative_overflow_infinity (val))
1156 return is_negative_overflow_infinity (val2) ? 0 : 1;
1157 else if (is_positive_overflow_infinity (val2))
1158 return is_positive_overflow_infinity (val) ? 0 : 1;
1160 return 0;
1163 /* Compare two values VAL1 and VAL2. Return
1165 -2 if VAL1 and VAL2 cannot be compared at compile-time,
1166 -1 if VAL1 < VAL2,
1167 0 if VAL1 == VAL2,
1168 +1 if VAL1 > VAL2, and
1169 +2 if VAL1 != VAL2
1171 This is similar to tree_int_cst_compare but supports pointer values
1172 and values that cannot be compared at compile time.
1174 If STRICT_OVERFLOW_P is not NULL, then set *STRICT_OVERFLOW_P to
1175 true if the return value is only valid if we assume that signed
1176 overflow is undefined. */
1178 static int
1179 compare_values_warnv (tree val1, tree val2, bool *strict_overflow_p)
1181 if (val1 == val2)
1182 return 0;
1184 /* Below we rely on the fact that VAL1 and VAL2 are both pointers or
1185 both integers. */
1186 gcc_assert (POINTER_TYPE_P (TREE_TYPE (val1))
1187 == POINTER_TYPE_P (TREE_TYPE (val2)));
1188 /* Convert the two values into the same type. This is needed because
1189 sizetype causes sign extension even for unsigned types. */
1190 val2 = fold_convert (TREE_TYPE (val1), val2);
1191 STRIP_USELESS_TYPE_CONVERSION (val2);
1193 if ((TREE_CODE (val1) == SSA_NAME
1194 || TREE_CODE (val1) == PLUS_EXPR
1195 || TREE_CODE (val1) == MINUS_EXPR)
1196 && (TREE_CODE (val2) == SSA_NAME
1197 || TREE_CODE (val2) == PLUS_EXPR
1198 || TREE_CODE (val2) == MINUS_EXPR))
1200 tree n1, c1, n2, c2;
1201 enum tree_code code1, code2;
1203 /* If VAL1 and VAL2 are of the form 'NAME [+-] CST' or 'NAME',
1204 return -1 or +1 accordingly. If VAL1 and VAL2 don't use the
1205 same name, return -2. */
1206 if (TREE_CODE (val1) == SSA_NAME)
1208 code1 = SSA_NAME;
1209 n1 = val1;
1210 c1 = NULL_TREE;
1212 else
1214 code1 = TREE_CODE (val1);
1215 n1 = TREE_OPERAND (val1, 0);
1216 c1 = TREE_OPERAND (val1, 1);
1217 if (tree_int_cst_sgn (c1) == -1)
1219 if (is_negative_overflow_infinity (c1))
1220 return -2;
1221 c1 = fold_unary_to_constant (NEGATE_EXPR, TREE_TYPE (c1), c1);
1222 if (!c1)
1223 return -2;
1224 code1 = code1 == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR;
1228 if (TREE_CODE (val2) == SSA_NAME)
1230 code2 = SSA_NAME;
1231 n2 = val2;
1232 c2 = NULL_TREE;
1234 else
1236 code2 = TREE_CODE (val2);
1237 n2 = TREE_OPERAND (val2, 0);
1238 c2 = TREE_OPERAND (val2, 1);
1239 if (tree_int_cst_sgn (c2) == -1)
1241 if (is_negative_overflow_infinity (c2))
1242 return -2;
1243 c2 = fold_unary_to_constant (NEGATE_EXPR, TREE_TYPE (c2), c2);
1244 if (!c2)
1245 return -2;
1246 code2 = code2 == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR;
1250 /* Both values must use the same name. */
1251 if (n1 != n2)
1252 return -2;
1254 if (code1 == SSA_NAME
1255 && code2 == SSA_NAME)
1256 /* NAME == NAME */
1257 return 0;
1259 /* If overflow is defined we cannot simplify more. */
1260 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (val1)))
1261 return -2;
1263 if (strict_overflow_p != NULL
1264 && (code1 == SSA_NAME || !TREE_NO_WARNING (val1))
1265 && (code2 == SSA_NAME || !TREE_NO_WARNING (val2)))
1266 *strict_overflow_p = true;
1268 if (code1 == SSA_NAME)
1270 if (code2 == PLUS_EXPR)
1271 /* NAME < NAME + CST */
1272 return -1;
1273 else if (code2 == MINUS_EXPR)
1274 /* NAME > NAME - CST */
1275 return 1;
1277 else if (code1 == PLUS_EXPR)
1279 if (code2 == SSA_NAME)
1280 /* NAME + CST > NAME */
1281 return 1;
1282 else if (code2 == PLUS_EXPR)
1283 /* NAME + CST1 > NAME + CST2, if CST1 > CST2 */
1284 return compare_values_warnv (c1, c2, strict_overflow_p);
1285 else if (code2 == MINUS_EXPR)
1286 /* NAME + CST1 > NAME - CST2 */
1287 return 1;
1289 else if (code1 == MINUS_EXPR)
1291 if (code2 == SSA_NAME)
1292 /* NAME - CST < NAME */
1293 return -1;
1294 else if (code2 == PLUS_EXPR)
1295 /* NAME - CST1 < NAME + CST2 */
1296 return -1;
1297 else if (code2 == MINUS_EXPR)
1298 /* NAME - CST1 > NAME - CST2, if CST1 < CST2. Notice that
1299 C1 and C2 are swapped in the call to compare_values. */
1300 return compare_values_warnv (c2, c1, strict_overflow_p);
1303 gcc_unreachable ();
1306 /* We cannot compare non-constants. */
1307 if (!is_gimple_min_invariant (val1) || !is_gimple_min_invariant (val2))
1308 return -2;
1310 if (!POINTER_TYPE_P (TREE_TYPE (val1)))
1312 /* We cannot compare overflowed values, except for overflow
1313 infinities. */
1314 if (TREE_OVERFLOW (val1) || TREE_OVERFLOW (val2))
1316 if (strict_overflow_p != NULL)
1317 *strict_overflow_p = true;
1318 if (is_negative_overflow_infinity (val1))
1319 return is_negative_overflow_infinity (val2) ? 0 : -1;
1320 else if (is_negative_overflow_infinity (val2))
1321 return 1;
1322 else if (is_positive_overflow_infinity (val1))
1323 return is_positive_overflow_infinity (val2) ? 0 : 1;
1324 else if (is_positive_overflow_infinity (val2))
1325 return -1;
1326 return -2;
1329 return tree_int_cst_compare (val1, val2);
1331 else
1333 tree t;
1335 /* First see if VAL1 and VAL2 are not the same. */
1336 if (val1 == val2 || operand_equal_p (val1, val2, 0))
1337 return 0;
1339 /* If VAL1 is a lower address than VAL2, return -1. */
1340 if (operand_less_p (val1, val2) == 1)
1341 return -1;
1343 /* If VAL1 is a higher address than VAL2, return +1. */
1344 if (operand_less_p (val2, val1) == 1)
1345 return 1;
1347 /* If VAL1 is different than VAL2, return +2.
1348 For integer constants we either have already returned -1 or 1
1349 or they are equivalent. We still might succeed in proving
1350 something about non-trivial operands. */
1351 if (TREE_CODE (val1) != INTEGER_CST
1352 || TREE_CODE (val2) != INTEGER_CST)
1354 t = fold_binary_to_constant (NE_EXPR, boolean_type_node, val1, val2);
1355 if (t && integer_onep (t))
1356 return 2;
1359 return -2;
1363 /* Compare values like compare_values_warnv, but treat comparisons of
1364 nonconstants which rely on undefined overflow as incomparable. */
1366 static int
1367 compare_values (tree val1, tree val2)
1369 bool sop;
1370 int ret;
1372 sop = false;
1373 ret = compare_values_warnv (val1, val2, &sop);
1374 if (sop
1375 && (!is_gimple_min_invariant (val1) || !is_gimple_min_invariant (val2)))
1376 ret = -2;
1377 return ret;
1381 /* Return 1 if VAL is inside value range MIN <= VAL <= MAX,
1382 0 if VAL is not inside [MIN, MAX],
1383 -2 if we cannot tell either way.
1385 Benchmark compile/20001226-1.c compilation time after changing this
1386 function. */
1388 static inline int
1389 value_inside_range (tree val, tree min, tree max)
1391 int cmp1, cmp2;
1393 cmp1 = operand_less_p (val, min);
1394 if (cmp1 == -2)
1395 return -2;
1396 if (cmp1 == 1)
1397 return 0;
1399 cmp2 = operand_less_p (max, val);
1400 if (cmp2 == -2)
1401 return -2;
1403 return !cmp2;
1407 /* Return true if value ranges VR0 and VR1 have a non-empty
1408 intersection.
1410 Benchmark compile/20001226-1.c compilation time after changing this
1411 function.
1414 static inline bool
1415 value_ranges_intersect_p (value_range_t *vr0, value_range_t *vr1)
1417 /* The value ranges do not intersect if the maximum of the first range is
1418 less than the minimum of the second range or vice versa.
1419 When those relations are unknown, we can't do any better. */
1420 if (operand_less_p (vr0->max, vr1->min) != 0)
1421 return false;
1422 if (operand_less_p (vr1->max, vr0->min) != 0)
1423 return false;
1424 return true;
1428 /* Return 1 if [MIN, MAX] includes the value zero, 0 if it does not
1429 include the value zero, -2 if we cannot tell. */
1431 static inline int
1432 range_includes_zero_p (tree min, tree max)
1434 tree zero = build_int_cst (TREE_TYPE (min), 0);
1435 return value_inside_range (zero, min, max);
1438 /* Return true if *VR is know to only contain nonnegative values. */
1440 static inline bool
1441 value_range_nonnegative_p (value_range_t *vr)
1443 /* Testing for VR_ANTI_RANGE is not useful here as any anti-range
1444 which would return a useful value should be encoded as a
1445 VR_RANGE. */
1446 if (vr->type == VR_RANGE)
1448 int result = compare_values (vr->min, integer_zero_node);
1449 return (result == 0 || result == 1);
1452 return false;
1455 /* Return true if T, an SSA_NAME, is known to be nonnegative. Return
1456 false otherwise or if no value range information is available. */
1458 bool
1459 ssa_name_nonnegative_p (const_tree t)
1461 value_range_t *vr = get_value_range (t);
1463 if (INTEGRAL_TYPE_P (t)
1464 && TYPE_UNSIGNED (t))
1465 return true;
1467 if (!vr)
1468 return false;
1470 return value_range_nonnegative_p (vr);
1473 /* If *VR has a value rante that is a single constant value return that,
1474 otherwise return NULL_TREE. */
1476 static tree
1477 value_range_constant_singleton (value_range_t *vr)
1479 if (vr->type == VR_RANGE
1480 && operand_equal_p (vr->min, vr->max, 0)
1481 && is_gimple_min_invariant (vr->min))
1482 return vr->min;
1484 return NULL_TREE;
1487 /* If OP has a value range with a single constant value return that,
1488 otherwise return NULL_TREE. This returns OP itself if OP is a
1489 constant. */
1491 static tree
1492 op_with_constant_singleton_value_range (tree op)
1494 if (is_gimple_min_invariant (op))
1495 return op;
1497 if (TREE_CODE (op) != SSA_NAME)
1498 return NULL_TREE;
1500 return value_range_constant_singleton (get_value_range (op));
1503 /* Return true if op is in a boolean [0, 1] value-range. */
1505 static bool
1506 op_with_boolean_value_range_p (tree op)
1508 value_range_t *vr;
1510 if (TYPE_PRECISION (TREE_TYPE (op)) == 1)
1511 return true;
1513 if (integer_zerop (op)
1514 || integer_onep (op))
1515 return true;
1517 if (TREE_CODE (op) != SSA_NAME)
1518 return false;
1520 vr = get_value_range (op);
1521 return (vr->type == VR_RANGE
1522 && integer_zerop (vr->min)
1523 && integer_onep (vr->max));
1526 /* Extract value range information from an ASSERT_EXPR EXPR and store
1527 it in *VR_P. */
1529 static void
1530 extract_range_from_assert (value_range_t *vr_p, tree expr)
1532 tree var, cond, limit, min, max, type;
1533 value_range_t *limit_vr;
1534 enum tree_code cond_code;
1536 var = ASSERT_EXPR_VAR (expr);
1537 cond = ASSERT_EXPR_COND (expr);
1539 gcc_assert (COMPARISON_CLASS_P (cond));
1541 /* Find VAR in the ASSERT_EXPR conditional. */
1542 if (var == TREE_OPERAND (cond, 0)
1543 || TREE_CODE (TREE_OPERAND (cond, 0)) == PLUS_EXPR
1544 || TREE_CODE (TREE_OPERAND (cond, 0)) == NOP_EXPR)
1546 /* If the predicate is of the form VAR COMP LIMIT, then we just
1547 take LIMIT from the RHS and use the same comparison code. */
1548 cond_code = TREE_CODE (cond);
1549 limit = TREE_OPERAND (cond, 1);
1550 cond = TREE_OPERAND (cond, 0);
1552 else
1554 /* If the predicate is of the form LIMIT COMP VAR, then we need
1555 to flip around the comparison code to create the proper range
1556 for VAR. */
1557 cond_code = swap_tree_comparison (TREE_CODE (cond));
1558 limit = TREE_OPERAND (cond, 0);
1559 cond = TREE_OPERAND (cond, 1);
1562 limit = avoid_overflow_infinity (limit);
1564 type = TREE_TYPE (var);
1565 gcc_assert (limit != var);
1567 /* For pointer arithmetic, we only keep track of pointer equality
1568 and inequality. */
1569 if (POINTER_TYPE_P (type) && cond_code != NE_EXPR && cond_code != EQ_EXPR)
1571 set_value_range_to_varying (vr_p);
1572 return;
1575 /* If LIMIT is another SSA name and LIMIT has a range of its own,
1576 try to use LIMIT's range to avoid creating symbolic ranges
1577 unnecessarily. */
1578 limit_vr = (TREE_CODE (limit) == SSA_NAME) ? get_value_range (limit) : NULL;
1580 /* LIMIT's range is only interesting if it has any useful information. */
1581 if (limit_vr
1582 && (limit_vr->type == VR_UNDEFINED
1583 || limit_vr->type == VR_VARYING
1584 || symbolic_range_p (limit_vr)))
1585 limit_vr = NULL;
1587 /* Initially, the new range has the same set of equivalences of
1588 VAR's range. This will be revised before returning the final
1589 value. Since assertions may be chained via mutually exclusive
1590 predicates, we will need to trim the set of equivalences before
1591 we are done. */
1592 gcc_assert (vr_p->equiv == NULL);
1593 add_equivalence (&vr_p->equiv, var);
1595 /* Extract a new range based on the asserted comparison for VAR and
1596 LIMIT's value range. Notice that if LIMIT has an anti-range, we
1597 will only use it for equality comparisons (EQ_EXPR). For any
1598 other kind of assertion, we cannot derive a range from LIMIT's
1599 anti-range that can be used to describe the new range. For
1600 instance, ASSERT_EXPR <x_2, x_2 <= b_4>. If b_4 is ~[2, 10],
1601 then b_4 takes on the ranges [-INF, 1] and [11, +INF]. There is
1602 no single range for x_2 that could describe LE_EXPR, so we might
1603 as well build the range [b_4, +INF] for it.
1604 One special case we handle is extracting a range from a
1605 range test encoded as (unsigned)var + CST <= limit. */
1606 if (TREE_CODE (cond) == NOP_EXPR
1607 || TREE_CODE (cond) == PLUS_EXPR)
1609 if (TREE_CODE (cond) == PLUS_EXPR)
1611 min = fold_build1 (NEGATE_EXPR, TREE_TYPE (TREE_OPERAND (cond, 1)),
1612 TREE_OPERAND (cond, 1));
1613 max = int_const_binop (PLUS_EXPR, limit, min);
1614 cond = TREE_OPERAND (cond, 0);
1616 else
1618 min = build_int_cst (TREE_TYPE (var), 0);
1619 max = limit;
1622 /* Make sure to not set TREE_OVERFLOW on the final type
1623 conversion. We are willingly interpreting large positive
1624 unsigned values as negative singed values here. */
1625 min = force_fit_type_double (TREE_TYPE (var), tree_to_double_int (min),
1626 0, false);
1627 max = force_fit_type_double (TREE_TYPE (var), tree_to_double_int (max),
1628 0, false);
1630 /* We can transform a max, min range to an anti-range or
1631 vice-versa. Use set_and_canonicalize_value_range which does
1632 this for us. */
1633 if (cond_code == LE_EXPR)
1634 set_and_canonicalize_value_range (vr_p, VR_RANGE,
1635 min, max, vr_p->equiv);
1636 else if (cond_code == GT_EXPR)
1637 set_and_canonicalize_value_range (vr_p, VR_ANTI_RANGE,
1638 min, max, vr_p->equiv);
1639 else
1640 gcc_unreachable ();
1642 else if (cond_code == EQ_EXPR)
1644 enum value_range_type range_type;
1646 if (limit_vr)
1648 range_type = limit_vr->type;
1649 min = limit_vr->min;
1650 max = limit_vr->max;
1652 else
1654 range_type = VR_RANGE;
1655 min = limit;
1656 max = limit;
1659 set_value_range (vr_p, range_type, min, max, vr_p->equiv);
1661 /* When asserting the equality VAR == LIMIT and LIMIT is another
1662 SSA name, the new range will also inherit the equivalence set
1663 from LIMIT. */
1664 if (TREE_CODE (limit) == SSA_NAME)
1665 add_equivalence (&vr_p->equiv, limit);
1667 else if (cond_code == NE_EXPR)
1669 /* As described above, when LIMIT's range is an anti-range and
1670 this assertion is an inequality (NE_EXPR), then we cannot
1671 derive anything from the anti-range. For instance, if
1672 LIMIT's range was ~[0, 0], the assertion 'VAR != LIMIT' does
1673 not imply that VAR's range is [0, 0]. So, in the case of
1674 anti-ranges, we just assert the inequality using LIMIT and
1675 not its anti-range.
1677 If LIMIT_VR is a range, we can only use it to build a new
1678 anti-range if LIMIT_VR is a single-valued range. For
1679 instance, if LIMIT_VR is [0, 1], the predicate
1680 VAR != [0, 1] does not mean that VAR's range is ~[0, 1].
1681 Rather, it means that for value 0 VAR should be ~[0, 0]
1682 and for value 1, VAR should be ~[1, 1]. We cannot
1683 represent these ranges.
1685 The only situation in which we can build a valid
1686 anti-range is when LIMIT_VR is a single-valued range
1687 (i.e., LIMIT_VR->MIN == LIMIT_VR->MAX). In that case,
1688 build the anti-range ~[LIMIT_VR->MIN, LIMIT_VR->MAX]. */
1689 if (limit_vr
1690 && limit_vr->type == VR_RANGE
1691 && compare_values (limit_vr->min, limit_vr->max) == 0)
1693 min = limit_vr->min;
1694 max = limit_vr->max;
1696 else
1698 /* In any other case, we cannot use LIMIT's range to build a
1699 valid anti-range. */
1700 min = max = limit;
1703 /* If MIN and MAX cover the whole range for their type, then
1704 just use the original LIMIT. */
1705 if (INTEGRAL_TYPE_P (type)
1706 && vrp_val_is_min (min)
1707 && vrp_val_is_max (max))
1708 min = max = limit;
1710 set_value_range (vr_p, VR_ANTI_RANGE, min, max, vr_p->equiv);
1712 else if (cond_code == LE_EXPR || cond_code == LT_EXPR)
1714 min = TYPE_MIN_VALUE (type);
1716 if (limit_vr == NULL || limit_vr->type == VR_ANTI_RANGE)
1717 max = limit;
1718 else
1720 /* If LIMIT_VR is of the form [N1, N2], we need to build the
1721 range [MIN, N2] for LE_EXPR and [MIN, N2 - 1] for
1722 LT_EXPR. */
1723 max = limit_vr->max;
1726 /* If the maximum value forces us to be out of bounds, simply punt.
1727 It would be pointless to try and do anything more since this
1728 all should be optimized away above us. */
1729 if ((cond_code == LT_EXPR
1730 && compare_values (max, min) == 0)
1731 || (CONSTANT_CLASS_P (max) && TREE_OVERFLOW (max)))
1732 set_value_range_to_varying (vr_p);
1733 else
1735 /* For LT_EXPR, we create the range [MIN, MAX - 1]. */
1736 if (cond_code == LT_EXPR)
1738 if (TYPE_PRECISION (TREE_TYPE (max)) == 1
1739 && !TYPE_UNSIGNED (TREE_TYPE (max)))
1740 max = fold_build2 (PLUS_EXPR, TREE_TYPE (max), max,
1741 build_int_cst (TREE_TYPE (max), -1));
1742 else
1743 max = fold_build2 (MINUS_EXPR, TREE_TYPE (max), max,
1744 build_int_cst (TREE_TYPE (max), 1));
1745 if (EXPR_P (max))
1746 TREE_NO_WARNING (max) = 1;
1749 set_value_range (vr_p, VR_RANGE, min, max, vr_p->equiv);
1752 else if (cond_code == GE_EXPR || cond_code == GT_EXPR)
1754 max = TYPE_MAX_VALUE (type);
1756 if (limit_vr == NULL || limit_vr->type == VR_ANTI_RANGE)
1757 min = limit;
1758 else
1760 /* If LIMIT_VR is of the form [N1, N2], we need to build the
1761 range [N1, MAX] for GE_EXPR and [N1 + 1, MAX] for
1762 GT_EXPR. */
1763 min = limit_vr->min;
1766 /* If the minimum value forces us to be out of bounds, simply punt.
1767 It would be pointless to try and do anything more since this
1768 all should be optimized away above us. */
1769 if ((cond_code == GT_EXPR
1770 && compare_values (min, max) == 0)
1771 || (CONSTANT_CLASS_P (min) && TREE_OVERFLOW (min)))
1772 set_value_range_to_varying (vr_p);
1773 else
1775 /* For GT_EXPR, we create the range [MIN + 1, MAX]. */
1776 if (cond_code == GT_EXPR)
1778 if (TYPE_PRECISION (TREE_TYPE (min)) == 1
1779 && !TYPE_UNSIGNED (TREE_TYPE (min)))
1780 min = fold_build2 (MINUS_EXPR, TREE_TYPE (min), min,
1781 build_int_cst (TREE_TYPE (min), -1));
1782 else
1783 min = fold_build2 (PLUS_EXPR, TREE_TYPE (min), min,
1784 build_int_cst (TREE_TYPE (min), 1));
1785 if (EXPR_P (min))
1786 TREE_NO_WARNING (min) = 1;
1789 set_value_range (vr_p, VR_RANGE, min, max, vr_p->equiv);
1792 else
1793 gcc_unreachable ();
1795 /* Finally intersect the new range with what we already know about var. */
1796 vrp_intersect_ranges (vr_p, get_value_range (var));
1800 /* Extract range information from SSA name VAR and store it in VR. If
1801 VAR has an interesting range, use it. Otherwise, create the
1802 range [VAR, VAR] and return it. This is useful in situations where
1803 we may have conditionals testing values of VARYING names. For
1804 instance,
1806 x_3 = y_5;
1807 if (x_3 > y_5)
1810 Even if y_5 is deemed VARYING, we can determine that x_3 > y_5 is
1811 always false. */
1813 static void
1814 extract_range_from_ssa_name (value_range_t *vr, tree var)
1816 value_range_t *var_vr = get_value_range (var);
1818 if (var_vr->type != VR_UNDEFINED && var_vr->type != VR_VARYING)
1819 copy_value_range (vr, var_vr);
1820 else
1821 set_value_range (vr, VR_RANGE, var, var, NULL);
1823 add_equivalence (&vr->equiv, var);
1827 /* Wrapper around int_const_binop. If the operation overflows and we
1828 are not using wrapping arithmetic, then adjust the result to be
1829 -INF or +INF depending on CODE, VAL1 and VAL2. This can return
1830 NULL_TREE if we need to use an overflow infinity representation but
1831 the type does not support it. */
1833 static tree
1834 vrp_int_const_binop (enum tree_code code, tree val1, tree val2)
1836 tree res;
1838 res = int_const_binop (code, val1, val2);
1840 /* If we are using unsigned arithmetic, operate symbolically
1841 on -INF and +INF as int_const_binop only handles signed overflow. */
1842 if (TYPE_UNSIGNED (TREE_TYPE (val1)))
1844 int checkz = compare_values (res, val1);
1845 bool overflow = false;
1847 /* Ensure that res = val1 [+*] val2 >= val1
1848 or that res = val1 - val2 <= val1. */
1849 if ((code == PLUS_EXPR
1850 && !(checkz == 1 || checkz == 0))
1851 || (code == MINUS_EXPR
1852 && !(checkz == 0 || checkz == -1)))
1854 overflow = true;
1856 /* Checking for multiplication overflow is done by dividing the
1857 output of the multiplication by the first input of the
1858 multiplication. If the result of that division operation is
1859 not equal to the second input of the multiplication, then the
1860 multiplication overflowed. */
1861 else if (code == MULT_EXPR && !integer_zerop (val1))
1863 tree tmp = int_const_binop (TRUNC_DIV_EXPR,
1864 res,
1865 val1);
1866 int check = compare_values (tmp, val2);
1868 if (check != 0)
1869 overflow = true;
1872 if (overflow)
1874 res = copy_node (res);
1875 TREE_OVERFLOW (res) = 1;
1879 else if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (val1)))
1880 /* If the singed operation wraps then int_const_binop has done
1881 everything we want. */
1883 else if ((TREE_OVERFLOW (res)
1884 && !TREE_OVERFLOW (val1)
1885 && !TREE_OVERFLOW (val2))
1886 || is_overflow_infinity (val1)
1887 || is_overflow_infinity (val2))
1889 /* If the operation overflowed but neither VAL1 nor VAL2 are
1890 overflown, return -INF or +INF depending on the operation
1891 and the combination of signs of the operands. */
1892 int sgn1 = tree_int_cst_sgn (val1);
1893 int sgn2 = tree_int_cst_sgn (val2);
1895 if (needs_overflow_infinity (TREE_TYPE (res))
1896 && !supports_overflow_infinity (TREE_TYPE (res)))
1897 return NULL_TREE;
1899 /* We have to punt on adding infinities of different signs,
1900 since we can't tell what the sign of the result should be.
1901 Likewise for subtracting infinities of the same sign. */
1902 if (((code == PLUS_EXPR && sgn1 != sgn2)
1903 || (code == MINUS_EXPR && sgn1 == sgn2))
1904 && is_overflow_infinity (val1)
1905 && is_overflow_infinity (val2))
1906 return NULL_TREE;
1908 /* Don't try to handle division or shifting of infinities. */
1909 if ((code == TRUNC_DIV_EXPR
1910 || code == FLOOR_DIV_EXPR
1911 || code == CEIL_DIV_EXPR
1912 || code == EXACT_DIV_EXPR
1913 || code == ROUND_DIV_EXPR
1914 || code == RSHIFT_EXPR)
1915 && (is_overflow_infinity (val1)
1916 || is_overflow_infinity (val2)))
1917 return NULL_TREE;
1919 /* Notice that we only need to handle the restricted set of
1920 operations handled by extract_range_from_binary_expr.
1921 Among them, only multiplication, addition and subtraction
1922 can yield overflow without overflown operands because we
1923 are working with integral types only... except in the
1924 case VAL1 = -INF and VAL2 = -1 which overflows to +INF
1925 for division too. */
1927 /* For multiplication, the sign of the overflow is given
1928 by the comparison of the signs of the operands. */
1929 if ((code == MULT_EXPR && sgn1 == sgn2)
1930 /* For addition, the operands must be of the same sign
1931 to yield an overflow. Its sign is therefore that
1932 of one of the operands, for example the first. For
1933 infinite operands X + -INF is negative, not positive. */
1934 || (code == PLUS_EXPR
1935 && (sgn1 >= 0
1936 ? !is_negative_overflow_infinity (val2)
1937 : is_positive_overflow_infinity (val2)))
1938 /* For subtraction, non-infinite operands must be of
1939 different signs to yield an overflow. Its sign is
1940 therefore that of the first operand or the opposite of
1941 that of the second operand. A first operand of 0 counts
1942 as positive here, for the corner case 0 - (-INF), which
1943 overflows, but must yield +INF. For infinite operands 0
1944 - INF is negative, not positive. */
1945 || (code == MINUS_EXPR
1946 && (sgn1 >= 0
1947 ? !is_positive_overflow_infinity (val2)
1948 : is_negative_overflow_infinity (val2)))
1949 /* We only get in here with positive shift count, so the
1950 overflow direction is the same as the sign of val1.
1951 Actually rshift does not overflow at all, but we only
1952 handle the case of shifting overflowed -INF and +INF. */
1953 || (code == RSHIFT_EXPR
1954 && sgn1 >= 0)
1955 /* For division, the only case is -INF / -1 = +INF. */
1956 || code == TRUNC_DIV_EXPR
1957 || code == FLOOR_DIV_EXPR
1958 || code == CEIL_DIV_EXPR
1959 || code == EXACT_DIV_EXPR
1960 || code == ROUND_DIV_EXPR)
1961 return (needs_overflow_infinity (TREE_TYPE (res))
1962 ? positive_overflow_infinity (TREE_TYPE (res))
1963 : TYPE_MAX_VALUE (TREE_TYPE (res)));
1964 else
1965 return (needs_overflow_infinity (TREE_TYPE (res))
1966 ? negative_overflow_infinity (TREE_TYPE (res))
1967 : TYPE_MIN_VALUE (TREE_TYPE (res)));
1970 return res;
1974 /* For range VR compute two double_int bitmasks. In *MAY_BE_NONZERO
1975 bitmask if some bit is unset, it means for all numbers in the range
1976 the bit is 0, otherwise it might be 0 or 1. In *MUST_BE_NONZERO
1977 bitmask if some bit is set, it means for all numbers in the range
1978 the bit is 1, otherwise it might be 0 or 1. */
1980 static bool
1981 zero_nonzero_bits_from_vr (value_range_t *vr,
1982 double_int *may_be_nonzero,
1983 double_int *must_be_nonzero)
1985 *may_be_nonzero = double_int_minus_one;
1986 *must_be_nonzero = double_int_zero;
1987 if (!range_int_cst_p (vr)
1988 || TREE_OVERFLOW (vr->min)
1989 || TREE_OVERFLOW (vr->max))
1990 return false;
1992 if (range_int_cst_singleton_p (vr))
1994 *may_be_nonzero = tree_to_double_int (vr->min);
1995 *must_be_nonzero = *may_be_nonzero;
1997 else if (tree_int_cst_sgn (vr->min) >= 0
1998 || tree_int_cst_sgn (vr->max) < 0)
2000 double_int dmin = tree_to_double_int (vr->min);
2001 double_int dmax = tree_to_double_int (vr->max);
2002 double_int xor_mask = dmin ^ dmax;
2003 *may_be_nonzero = dmin | dmax;
2004 *must_be_nonzero = dmin & dmax;
2005 if (xor_mask.high != 0)
2007 unsigned HOST_WIDE_INT mask
2008 = ((unsigned HOST_WIDE_INT) 1
2009 << floor_log2 (xor_mask.high)) - 1;
2010 may_be_nonzero->low = ALL_ONES;
2011 may_be_nonzero->high |= mask;
2012 must_be_nonzero->low = 0;
2013 must_be_nonzero->high &= ~mask;
2015 else if (xor_mask.low != 0)
2017 unsigned HOST_WIDE_INT mask
2018 = ((unsigned HOST_WIDE_INT) 1
2019 << floor_log2 (xor_mask.low)) - 1;
2020 may_be_nonzero->low |= mask;
2021 must_be_nonzero->low &= ~mask;
2025 return true;
2028 /* Create two value-ranges in *VR0 and *VR1 from the anti-range *AR
2029 so that *VR0 U *VR1 == *AR. Returns true if that is possible,
2030 false otherwise. If *AR can be represented with a single range
2031 *VR1 will be VR_UNDEFINED. */
2033 static bool
2034 ranges_from_anti_range (value_range_t *ar,
2035 value_range_t *vr0, value_range_t *vr1)
2037 tree type = TREE_TYPE (ar->min);
2039 vr0->type = VR_UNDEFINED;
2040 vr1->type = VR_UNDEFINED;
2042 if (ar->type != VR_ANTI_RANGE
2043 || TREE_CODE (ar->min) != INTEGER_CST
2044 || TREE_CODE (ar->max) != INTEGER_CST
2045 || !vrp_val_min (type)
2046 || !vrp_val_max (type))
2047 return false;
2049 if (!vrp_val_is_min (ar->min))
2051 vr0->type = VR_RANGE;
2052 vr0->min = vrp_val_min (type);
2053 vr0->max
2054 = double_int_to_tree (type,
2055 tree_to_double_int (ar->min) - double_int_one);
2057 if (!vrp_val_is_max (ar->max))
2059 vr1->type = VR_RANGE;
2060 vr1->min
2061 = double_int_to_tree (type,
2062 tree_to_double_int (ar->max) + double_int_one);
2063 vr1->max = vrp_val_max (type);
2065 if (vr0->type == VR_UNDEFINED)
2067 *vr0 = *vr1;
2068 vr1->type = VR_UNDEFINED;
2071 return vr0->type != VR_UNDEFINED;
2074 /* Helper to extract a value-range *VR for a multiplicative operation
2075 *VR0 CODE *VR1. */
2077 static void
2078 extract_range_from_multiplicative_op_1 (value_range_t *vr,
2079 enum tree_code code,
2080 value_range_t *vr0, value_range_t *vr1)
2082 enum value_range_type type;
2083 tree val[4];
2084 size_t i;
2085 tree min, max;
2086 bool sop;
2087 int cmp;
2089 /* Multiplications, divisions and shifts are a bit tricky to handle,
2090 depending on the mix of signs we have in the two ranges, we
2091 need to operate on different values to get the minimum and
2092 maximum values for the new range. One approach is to figure
2093 out all the variations of range combinations and do the
2094 operations.
2096 However, this involves several calls to compare_values and it
2097 is pretty convoluted. It's simpler to do the 4 operations
2098 (MIN0 OP MIN1, MIN0 OP MAX1, MAX0 OP MIN1 and MAX0 OP MAX0 OP
2099 MAX1) and then figure the smallest and largest values to form
2100 the new range. */
2101 gcc_assert (code == MULT_EXPR
2102 || code == TRUNC_DIV_EXPR
2103 || code == FLOOR_DIV_EXPR
2104 || code == CEIL_DIV_EXPR
2105 || code == EXACT_DIV_EXPR
2106 || code == ROUND_DIV_EXPR
2107 || code == RSHIFT_EXPR
2108 || code == LSHIFT_EXPR);
2109 gcc_assert ((vr0->type == VR_RANGE
2110 || (code == MULT_EXPR && vr0->type == VR_ANTI_RANGE))
2111 && vr0->type == vr1->type);
2113 type = vr0->type;
2115 /* Compute the 4 cross operations. */
2116 sop = false;
2117 val[0] = vrp_int_const_binop (code, vr0->min, vr1->min);
2118 if (val[0] == NULL_TREE)
2119 sop = true;
2121 if (vr1->max == vr1->min)
2122 val[1] = NULL_TREE;
2123 else
2125 val[1] = vrp_int_const_binop (code, vr0->min, vr1->max);
2126 if (val[1] == NULL_TREE)
2127 sop = true;
2130 if (vr0->max == vr0->min)
2131 val[2] = NULL_TREE;
2132 else
2134 val[2] = vrp_int_const_binop (code, vr0->max, vr1->min);
2135 if (val[2] == NULL_TREE)
2136 sop = true;
2139 if (vr0->min == vr0->max || vr1->min == vr1->max)
2140 val[3] = NULL_TREE;
2141 else
2143 val[3] = vrp_int_const_binop (code, vr0->max, vr1->max);
2144 if (val[3] == NULL_TREE)
2145 sop = true;
2148 if (sop)
2150 set_value_range_to_varying (vr);
2151 return;
2154 /* Set MIN to the minimum of VAL[i] and MAX to the maximum
2155 of VAL[i]. */
2156 min = val[0];
2157 max = val[0];
2158 for (i = 1; i < 4; i++)
2160 if (!is_gimple_min_invariant (min)
2161 || (TREE_OVERFLOW (min) && !is_overflow_infinity (min))
2162 || !is_gimple_min_invariant (max)
2163 || (TREE_OVERFLOW (max) && !is_overflow_infinity (max)))
2164 break;
2166 if (val[i])
2168 if (!is_gimple_min_invariant (val[i])
2169 || (TREE_OVERFLOW (val[i])
2170 && !is_overflow_infinity (val[i])))
2172 /* If we found an overflowed value, set MIN and MAX
2173 to it so that we set the resulting range to
2174 VARYING. */
2175 min = max = val[i];
2176 break;
2179 if (compare_values (val[i], min) == -1)
2180 min = val[i];
2182 if (compare_values (val[i], max) == 1)
2183 max = val[i];
2187 /* If either MIN or MAX overflowed, then set the resulting range to
2188 VARYING. But we do accept an overflow infinity
2189 representation. */
2190 if (min == NULL_TREE
2191 || !is_gimple_min_invariant (min)
2192 || (TREE_OVERFLOW (min) && !is_overflow_infinity (min))
2193 || max == NULL_TREE
2194 || !is_gimple_min_invariant (max)
2195 || (TREE_OVERFLOW (max) && !is_overflow_infinity (max)))
2197 set_value_range_to_varying (vr);
2198 return;
2201 /* We punt if:
2202 1) [-INF, +INF]
2203 2) [-INF, +-INF(OVF)]
2204 3) [+-INF(OVF), +INF]
2205 4) [+-INF(OVF), +-INF(OVF)]
2206 We learn nothing when we have INF and INF(OVF) on both sides.
2207 Note that we do accept [-INF, -INF] and [+INF, +INF] without
2208 overflow. */
2209 if ((vrp_val_is_min (min) || is_overflow_infinity (min))
2210 && (vrp_val_is_max (max) || is_overflow_infinity (max)))
2212 set_value_range_to_varying (vr);
2213 return;
2216 cmp = compare_values (min, max);
2217 if (cmp == -2 || cmp == 1)
2219 /* If the new range has its limits swapped around (MIN > MAX),
2220 then the operation caused one of them to wrap around, mark
2221 the new range VARYING. */
2222 set_value_range_to_varying (vr);
2224 else
2225 set_value_range (vr, type, min, max, NULL);
2228 /* Some quadruple precision helpers. */
2229 static int
2230 quad_int_cmp (double_int l0, double_int h0,
2231 double_int l1, double_int h1, bool uns)
2233 int c = h0.cmp (h1, uns);
2234 if (c != 0) return c;
2235 return l0.ucmp (l1);
2238 static void
2239 quad_int_pair_sort (double_int *l0, double_int *h0,
2240 double_int *l1, double_int *h1, bool uns)
2242 if (quad_int_cmp (*l0, *h0, *l1, *h1, uns) > 0)
2244 double_int tmp;
2245 tmp = *l0; *l0 = *l1; *l1 = tmp;
2246 tmp = *h0; *h0 = *h1; *h1 = tmp;
2250 /* Extract range information from a binary operation CODE based on
2251 the ranges of each of its operands, *VR0 and *VR1 with resulting
2252 type EXPR_TYPE. The resulting range is stored in *VR. */
2254 static void
2255 extract_range_from_binary_expr_1 (value_range_t *vr,
2256 enum tree_code code, tree expr_type,
2257 value_range_t *vr0_, value_range_t *vr1_)
2259 value_range_t vr0 = *vr0_, vr1 = *vr1_;
2260 value_range_t vrtem0 = VR_INITIALIZER, vrtem1 = VR_INITIALIZER;
2261 enum value_range_type type;
2262 tree min = NULL_TREE, max = NULL_TREE;
2263 int cmp;
2265 if (!INTEGRAL_TYPE_P (expr_type)
2266 && !POINTER_TYPE_P (expr_type))
2268 set_value_range_to_varying (vr);
2269 return;
2272 /* Not all binary expressions can be applied to ranges in a
2273 meaningful way. Handle only arithmetic operations. */
2274 if (code != PLUS_EXPR
2275 && code != MINUS_EXPR
2276 && code != POINTER_PLUS_EXPR
2277 && code != MULT_EXPR
2278 && code != TRUNC_DIV_EXPR
2279 && code != FLOOR_DIV_EXPR
2280 && code != CEIL_DIV_EXPR
2281 && code != EXACT_DIV_EXPR
2282 && code != ROUND_DIV_EXPR
2283 && code != TRUNC_MOD_EXPR
2284 && code != RSHIFT_EXPR
2285 && code != LSHIFT_EXPR
2286 && code != MIN_EXPR
2287 && code != MAX_EXPR
2288 && code != BIT_AND_EXPR
2289 && code != BIT_IOR_EXPR
2290 && code != BIT_XOR_EXPR)
2292 set_value_range_to_varying (vr);
2293 return;
2296 /* If both ranges are UNDEFINED, so is the result. */
2297 if (vr0.type == VR_UNDEFINED && vr1.type == VR_UNDEFINED)
2299 set_value_range_to_undefined (vr);
2300 return;
2302 /* If one of the ranges is UNDEFINED drop it to VARYING for the following
2303 code. At some point we may want to special-case operations that
2304 have UNDEFINED result for all or some value-ranges of the not UNDEFINED
2305 operand. */
2306 else if (vr0.type == VR_UNDEFINED)
2307 set_value_range_to_varying (&vr0);
2308 else if (vr1.type == VR_UNDEFINED)
2309 set_value_range_to_varying (&vr1);
2311 /* Now canonicalize anti-ranges to ranges when they are not symbolic
2312 and express ~[] op X as ([]' op X) U ([]'' op X). */
2313 if (vr0.type == VR_ANTI_RANGE
2314 && ranges_from_anti_range (&vr0, &vrtem0, &vrtem1))
2316 extract_range_from_binary_expr_1 (vr, code, expr_type, &vrtem0, vr1_);
2317 if (vrtem1.type != VR_UNDEFINED)
2319 value_range_t vrres = VR_INITIALIZER;
2320 extract_range_from_binary_expr_1 (&vrres, code, expr_type,
2321 &vrtem1, vr1_);
2322 vrp_meet (vr, &vrres);
2324 return;
2326 /* Likewise for X op ~[]. */
2327 if (vr1.type == VR_ANTI_RANGE
2328 && ranges_from_anti_range (&vr1, &vrtem0, &vrtem1))
2330 extract_range_from_binary_expr_1 (vr, code, expr_type, vr0_, &vrtem0);
2331 if (vrtem1.type != VR_UNDEFINED)
2333 value_range_t vrres = VR_INITIALIZER;
2334 extract_range_from_binary_expr_1 (&vrres, code, expr_type,
2335 vr0_, &vrtem1);
2336 vrp_meet (vr, &vrres);
2338 return;
2341 /* The type of the resulting value range defaults to VR0.TYPE. */
2342 type = vr0.type;
2344 /* Refuse to operate on VARYING ranges, ranges of different kinds
2345 and symbolic ranges. As an exception, we allow BIT_AND_EXPR
2346 because we may be able to derive a useful range even if one of
2347 the operands is VR_VARYING or symbolic range. Similarly for
2348 divisions. TODO, we may be able to derive anti-ranges in
2349 some cases. */
2350 if (code != BIT_AND_EXPR
2351 && code != BIT_IOR_EXPR
2352 && code != TRUNC_DIV_EXPR
2353 && code != FLOOR_DIV_EXPR
2354 && code != CEIL_DIV_EXPR
2355 && code != EXACT_DIV_EXPR
2356 && code != ROUND_DIV_EXPR
2357 && code != TRUNC_MOD_EXPR
2358 && (vr0.type == VR_VARYING
2359 || vr1.type == VR_VARYING
2360 || vr0.type != vr1.type
2361 || symbolic_range_p (&vr0)
2362 || symbolic_range_p (&vr1)))
2364 set_value_range_to_varying (vr);
2365 return;
2368 /* Now evaluate the expression to determine the new range. */
2369 if (POINTER_TYPE_P (expr_type))
2371 if (code == MIN_EXPR || code == MAX_EXPR)
2373 /* For MIN/MAX expressions with pointers, we only care about
2374 nullness, if both are non null, then the result is nonnull.
2375 If both are null, then the result is null. Otherwise they
2376 are varying. */
2377 if (range_is_nonnull (&vr0) && range_is_nonnull (&vr1))
2378 set_value_range_to_nonnull (vr, expr_type);
2379 else if (range_is_null (&vr0) && range_is_null (&vr1))
2380 set_value_range_to_null (vr, expr_type);
2381 else
2382 set_value_range_to_varying (vr);
2384 else if (code == POINTER_PLUS_EXPR)
2386 /* For pointer types, we are really only interested in asserting
2387 whether the expression evaluates to non-NULL. */
2388 if (range_is_nonnull (&vr0) || range_is_nonnull (&vr1))
2389 set_value_range_to_nonnull (vr, expr_type);
2390 else if (range_is_null (&vr0) && range_is_null (&vr1))
2391 set_value_range_to_null (vr, expr_type);
2392 else
2393 set_value_range_to_varying (vr);
2395 else if (code == BIT_AND_EXPR)
2397 /* For pointer types, we are really only interested in asserting
2398 whether the expression evaluates to non-NULL. */
2399 if (range_is_nonnull (&vr0) && range_is_nonnull (&vr1))
2400 set_value_range_to_nonnull (vr, expr_type);
2401 else if (range_is_null (&vr0) || range_is_null (&vr1))
2402 set_value_range_to_null (vr, expr_type);
2403 else
2404 set_value_range_to_varying (vr);
2406 else
2407 set_value_range_to_varying (vr);
2409 return;
2412 /* For integer ranges, apply the operation to each end of the
2413 range and see what we end up with. */
2414 if (code == PLUS_EXPR || code == MINUS_EXPR)
2416 /* If we have a PLUS_EXPR with two VR_RANGE integer constant
2417 ranges compute the precise range for such case if possible. */
2418 if (range_int_cst_p (&vr0)
2419 && range_int_cst_p (&vr1)
2420 /* We need as many bits as the possibly unsigned inputs. */
2421 && TYPE_PRECISION (expr_type) <= HOST_BITS_PER_DOUBLE_INT)
2423 double_int min0 = tree_to_double_int (vr0.min);
2424 double_int max0 = tree_to_double_int (vr0.max);
2425 double_int min1 = tree_to_double_int (vr1.min);
2426 double_int max1 = tree_to_double_int (vr1.max);
2427 bool uns = TYPE_UNSIGNED (expr_type);
2428 double_int type_min
2429 = double_int::min_value (TYPE_PRECISION (expr_type), uns);
2430 double_int type_max
2431 = double_int::max_value (TYPE_PRECISION (expr_type), uns);
2432 double_int dmin, dmax;
2433 int min_ovf = 0;
2434 int max_ovf = 0;
2436 if (code == PLUS_EXPR)
2438 dmin = min0 + min1;
2439 dmax = max0 + max1;
2441 /* Check for overflow in double_int. */
2442 if (min1.cmp (double_int_zero, uns) != dmin.cmp (min0, uns))
2443 min_ovf = min0.cmp (dmin, uns);
2444 if (max1.cmp (double_int_zero, uns) != dmax.cmp (max0, uns))
2445 max_ovf = max0.cmp (dmax, uns);
2447 else /* if (code == MINUS_EXPR) */
2449 dmin = min0 - max1;
2450 dmax = max0 - min1;
2452 if (double_int_zero.cmp (max1, uns) != dmin.cmp (min0, uns))
2453 min_ovf = min0.cmp (max1, uns);
2454 if (double_int_zero.cmp (min1, uns) != dmax.cmp (max0, uns))
2455 max_ovf = max0.cmp (min1, uns);
2458 /* For non-wrapping arithmetic look at possibly smaller
2459 value-ranges of the type. */
2460 if (!TYPE_OVERFLOW_WRAPS (expr_type))
2462 if (vrp_val_min (expr_type))
2463 type_min = tree_to_double_int (vrp_val_min (expr_type));
2464 if (vrp_val_max (expr_type))
2465 type_max = tree_to_double_int (vrp_val_max (expr_type));
2468 /* Check for type overflow. */
2469 if (min_ovf == 0)
2471 if (dmin.cmp (type_min, uns) == -1)
2472 min_ovf = -1;
2473 else if (dmin.cmp (type_max, uns) == 1)
2474 min_ovf = 1;
2476 if (max_ovf == 0)
2478 if (dmax.cmp (type_min, uns) == -1)
2479 max_ovf = -1;
2480 else if (dmax.cmp (type_max, uns) == 1)
2481 max_ovf = 1;
2484 if (TYPE_OVERFLOW_WRAPS (expr_type))
2486 /* If overflow wraps, truncate the values and adjust the
2487 range kind and bounds appropriately. */
2488 double_int tmin
2489 = dmin.ext (TYPE_PRECISION (expr_type), uns);
2490 double_int tmax
2491 = dmax.ext (TYPE_PRECISION (expr_type), uns);
2492 if (min_ovf == max_ovf)
2494 /* No overflow or both overflow or underflow. The
2495 range kind stays VR_RANGE. */
2496 min = double_int_to_tree (expr_type, tmin);
2497 max = double_int_to_tree (expr_type, tmax);
2499 else if (min_ovf == -1
2500 && max_ovf == 1)
2502 /* Underflow and overflow, drop to VR_VARYING. */
2503 set_value_range_to_varying (vr);
2504 return;
2506 else
2508 /* Min underflow or max overflow. The range kind
2509 changes to VR_ANTI_RANGE. */
2510 bool covers = false;
2511 double_int tem = tmin;
2512 gcc_assert ((min_ovf == -1 && max_ovf == 0)
2513 || (max_ovf == 1 && min_ovf == 0));
2514 type = VR_ANTI_RANGE;
2515 tmin = tmax + double_int_one;
2516 if (tmin.cmp (tmax, uns) < 0)
2517 covers = true;
2518 tmax = tem + double_int_minus_one;
2519 if (tmax.cmp (tem, uns) > 0)
2520 covers = true;
2521 /* If the anti-range would cover nothing, drop to varying.
2522 Likewise if the anti-range bounds are outside of the
2523 types values. */
2524 if (covers || tmin.cmp (tmax, uns) > 0)
2526 set_value_range_to_varying (vr);
2527 return;
2529 min = double_int_to_tree (expr_type, tmin);
2530 max = double_int_to_tree (expr_type, tmax);
2533 else
2535 /* If overflow does not wrap, saturate to the types min/max
2536 value. */
2537 if (min_ovf == -1)
2539 if (needs_overflow_infinity (expr_type)
2540 && supports_overflow_infinity (expr_type))
2541 min = negative_overflow_infinity (expr_type);
2542 else
2543 min = double_int_to_tree (expr_type, type_min);
2545 else if (min_ovf == 1)
2547 if (needs_overflow_infinity (expr_type)
2548 && supports_overflow_infinity (expr_type))
2549 min = positive_overflow_infinity (expr_type);
2550 else
2551 min = double_int_to_tree (expr_type, type_max);
2553 else
2554 min = double_int_to_tree (expr_type, dmin);
2556 if (max_ovf == -1)
2558 if (needs_overflow_infinity (expr_type)
2559 && supports_overflow_infinity (expr_type))
2560 max = negative_overflow_infinity (expr_type);
2561 else
2562 max = double_int_to_tree (expr_type, type_min);
2564 else if (max_ovf == 1)
2566 if (needs_overflow_infinity (expr_type)
2567 && supports_overflow_infinity (expr_type))
2568 max = positive_overflow_infinity (expr_type);
2569 else
2570 max = double_int_to_tree (expr_type, type_max);
2572 else
2573 max = double_int_to_tree (expr_type, dmax);
2575 if (needs_overflow_infinity (expr_type)
2576 && supports_overflow_infinity (expr_type))
2578 if (is_negative_overflow_infinity (vr0.min)
2579 || (code == PLUS_EXPR
2580 ? is_negative_overflow_infinity (vr1.min)
2581 : is_positive_overflow_infinity (vr1.max)))
2582 min = negative_overflow_infinity (expr_type);
2583 if (is_positive_overflow_infinity (vr0.max)
2584 || (code == PLUS_EXPR
2585 ? is_positive_overflow_infinity (vr1.max)
2586 : is_negative_overflow_infinity (vr1.min)))
2587 max = positive_overflow_infinity (expr_type);
2590 else
2592 /* For other cases, for example if we have a PLUS_EXPR with two
2593 VR_ANTI_RANGEs, drop to VR_VARYING. It would take more effort
2594 to compute a precise range for such a case.
2595 ??? General even mixed range kind operations can be expressed
2596 by for example transforming ~[3, 5] + [1, 2] to range-only
2597 operations and a union primitive:
2598 [-INF, 2] + [1, 2] U [5, +INF] + [1, 2]
2599 [-INF+1, 4] U [6, +INF(OVF)]
2600 though usually the union is not exactly representable with
2601 a single range or anti-range as the above is
2602 [-INF+1, +INF(OVF)] intersected with ~[5, 5]
2603 but one could use a scheme similar to equivalences for this. */
2604 set_value_range_to_varying (vr);
2605 return;
2608 else if (code == MIN_EXPR
2609 || code == MAX_EXPR)
2611 if (vr0.type == VR_ANTI_RANGE)
2613 /* For MIN_EXPR and MAX_EXPR with two VR_ANTI_RANGEs,
2614 the resulting VR_ANTI_RANGE is the same - intersection
2615 of the two ranges. */
2616 min = vrp_int_const_binop (MAX_EXPR, vr0.min, vr1.min);
2617 max = vrp_int_const_binop (MIN_EXPR, vr0.max, vr1.max);
2619 else
2621 /* For operations that make the resulting range directly
2622 proportional to the original ranges, apply the operation to
2623 the same end of each range. */
2624 min = vrp_int_const_binop (code, vr0.min, vr1.min);
2625 max = vrp_int_const_binop (code, vr0.max, vr1.max);
2628 else if (code == MULT_EXPR)
2630 /* Fancy code so that with unsigned, [-3,-1]*[-3,-1] does not
2631 drop to varying. */
2632 if (range_int_cst_p (&vr0)
2633 && range_int_cst_p (&vr1)
2634 && TYPE_OVERFLOW_WRAPS (expr_type))
2636 double_int min0, max0, min1, max1, sizem1, size;
2637 double_int prod0l, prod0h, prod1l, prod1h,
2638 prod2l, prod2h, prod3l, prod3h;
2639 bool uns0, uns1, uns;
2641 sizem1 = double_int::max_value (TYPE_PRECISION (expr_type), true);
2642 size = sizem1 + double_int_one;
2644 min0 = tree_to_double_int (vr0.min);
2645 max0 = tree_to_double_int (vr0.max);
2646 min1 = tree_to_double_int (vr1.min);
2647 max1 = tree_to_double_int (vr1.max);
2649 uns0 = TYPE_UNSIGNED (expr_type);
2650 uns1 = uns0;
2652 /* Canonicalize the intervals. */
2653 if (TYPE_UNSIGNED (expr_type))
2655 double_int min2 = size - min0;
2656 if (min2.cmp (max0, true) < 0)
2658 min0 = -min2;
2659 max0 -= size;
2660 uns0 = false;
2663 min2 = size - min1;
2664 if (min2.cmp (max1, true) < 0)
2666 min1 = -min2;
2667 max1 -= size;
2668 uns1 = false;
2671 uns = uns0 & uns1;
2673 bool overflow;
2674 prod0l = min0.wide_mul_with_sign (min1, true, &prod0h, &overflow);
2675 if (!uns0 && min0.is_negative ())
2676 prod0h -= min1;
2677 if (!uns1 && min1.is_negative ())
2678 prod0h -= min0;
2680 prod1l = min0.wide_mul_with_sign (max1, true, &prod1h, &overflow);
2681 if (!uns0 && min0.is_negative ())
2682 prod1h -= max1;
2683 if (!uns1 && max1.is_negative ())
2684 prod1h -= min0;
2686 prod2l = max0.wide_mul_with_sign (min1, true, &prod2h, &overflow);
2687 if (!uns0 && max0.is_negative ())
2688 prod2h -= min1;
2689 if (!uns1 && min1.is_negative ())
2690 prod2h -= max0;
2692 prod3l = max0.wide_mul_with_sign (max1, true, &prod3h, &overflow);
2693 if (!uns0 && max0.is_negative ())
2694 prod3h -= max1;
2695 if (!uns1 && max1.is_negative ())
2696 prod3h -= max0;
2698 /* Sort the 4 products. */
2699 quad_int_pair_sort (&prod0l, &prod0h, &prod3l, &prod3h, uns);
2700 quad_int_pair_sort (&prod1l, &prod1h, &prod2l, &prod2h, uns);
2701 quad_int_pair_sort (&prod0l, &prod0h, &prod1l, &prod1h, uns);
2702 quad_int_pair_sort (&prod2l, &prod2h, &prod3l, &prod3h, uns);
2704 /* Max - min. */
2705 if (prod0l.is_zero ())
2707 prod1l = double_int_zero;
2708 prod1h = -prod0h;
2710 else
2712 prod1l = -prod0l;
2713 prod1h = ~prod0h;
2715 prod2l = prod3l + prod1l;
2716 prod2h = prod3h + prod1h;
2717 if (prod2l.ult (prod3l))
2718 prod2h += double_int_one; /* carry */
2720 if (!prod2h.is_zero ()
2721 || prod2l.cmp (sizem1, true) >= 0)
2723 /* the range covers all values. */
2724 set_value_range_to_varying (vr);
2725 return;
2728 /* The following should handle the wrapping and selecting
2729 VR_ANTI_RANGE for us. */
2730 min = double_int_to_tree (expr_type, prod0l);
2731 max = double_int_to_tree (expr_type, prod3l);
2732 set_and_canonicalize_value_range (vr, VR_RANGE, min, max, NULL);
2733 return;
2736 /* If we have an unsigned MULT_EXPR with two VR_ANTI_RANGEs,
2737 drop to VR_VARYING. It would take more effort to compute a
2738 precise range for such a case. For example, if we have
2739 op0 == 65536 and op1 == 65536 with their ranges both being
2740 ~[0,0] on a 32-bit machine, we would have op0 * op1 == 0, so
2741 we cannot claim that the product is in ~[0,0]. Note that we
2742 are guaranteed to have vr0.type == vr1.type at this
2743 point. */
2744 if (vr0.type == VR_ANTI_RANGE
2745 && !TYPE_OVERFLOW_UNDEFINED (expr_type))
2747 set_value_range_to_varying (vr);
2748 return;
2751 extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1);
2752 return;
2754 else if (code == RSHIFT_EXPR
2755 || code == LSHIFT_EXPR)
2757 /* If we have a RSHIFT_EXPR with any shift values outside [0..prec-1],
2758 then drop to VR_VARYING. Outside of this range we get undefined
2759 behavior from the shift operation. We cannot even trust
2760 SHIFT_COUNT_TRUNCATED at this stage, because that applies to rtl
2761 shifts, and the operation at the tree level may be widened. */
2762 if (range_int_cst_p (&vr1)
2763 && compare_tree_int (vr1.min, 0) >= 0
2764 && compare_tree_int (vr1.max, TYPE_PRECISION (expr_type)) == -1)
2766 if (code == RSHIFT_EXPR)
2768 extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1);
2769 return;
2771 /* We can map lshifts by constants to MULT_EXPR handling. */
2772 else if (code == LSHIFT_EXPR
2773 && range_int_cst_singleton_p (&vr1))
2775 bool saved_flag_wrapv;
2776 value_range_t vr1p = VR_INITIALIZER;
2777 vr1p.type = VR_RANGE;
2778 vr1p.min
2779 = double_int_to_tree (expr_type,
2780 double_int_one
2781 .llshift (TREE_INT_CST_LOW (vr1.min),
2782 TYPE_PRECISION (expr_type)));
2783 vr1p.max = vr1p.min;
2784 /* We have to use a wrapping multiply though as signed overflow
2785 on lshifts is implementation defined in C89. */
2786 saved_flag_wrapv = flag_wrapv;
2787 flag_wrapv = 1;
2788 extract_range_from_binary_expr_1 (vr, MULT_EXPR, expr_type,
2789 &vr0, &vr1p);
2790 flag_wrapv = saved_flag_wrapv;
2791 return;
2793 else if (code == LSHIFT_EXPR
2794 && range_int_cst_p (&vr0))
2796 int prec = TYPE_PRECISION (expr_type);
2797 int overflow_pos = prec;
2798 int bound_shift;
2799 double_int bound, complement, low_bound, high_bound;
2800 bool uns = TYPE_UNSIGNED (expr_type);
2801 bool in_bounds = false;
2803 if (!uns)
2804 overflow_pos -= 1;
2806 bound_shift = overflow_pos - TREE_INT_CST_LOW (vr1.max);
2807 /* If bound_shift == HOST_BITS_PER_DOUBLE_INT, the llshift can
2808 overflow. However, for that to happen, vr1.max needs to be
2809 zero, which means vr1 is a singleton range of zero, which
2810 means it should be handled by the previous LSHIFT_EXPR
2811 if-clause. */
2812 bound = double_int_one.llshift (bound_shift, prec);
2813 complement = ~(bound - double_int_one);
2815 if (uns)
2817 low_bound = bound;
2818 high_bound = complement.zext (prec);
2819 if (tree_to_double_int (vr0.max).ult (low_bound))
2821 /* [5, 6] << [1, 2] == [10, 24]. */
2822 /* We're shifting out only zeroes, the value increases
2823 monotonically. */
2824 in_bounds = true;
2826 else if (high_bound.ult (tree_to_double_int (vr0.min)))
2828 /* [0xffffff00, 0xffffffff] << [1, 2]
2829 == [0xfffffc00, 0xfffffffe]. */
2830 /* We're shifting out only ones, the value decreases
2831 monotonically. */
2832 in_bounds = true;
2835 else
2837 /* [-1, 1] << [1, 2] == [-4, 4]. */
2838 low_bound = complement.sext (prec);
2839 high_bound = bound;
2840 if (tree_to_double_int (vr0.max).slt (high_bound)
2841 && low_bound.slt (tree_to_double_int (vr0.min)))
2843 /* For non-negative numbers, we're shifting out only
2844 zeroes, the value increases monotonically.
2845 For negative numbers, we're shifting out only ones, the
2846 value decreases monotomically. */
2847 in_bounds = true;
2851 if (in_bounds)
2853 extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1);
2854 return;
2858 set_value_range_to_varying (vr);
2859 return;
2861 else if (code == TRUNC_DIV_EXPR
2862 || code == FLOOR_DIV_EXPR
2863 || code == CEIL_DIV_EXPR
2864 || code == EXACT_DIV_EXPR
2865 || code == ROUND_DIV_EXPR)
2867 if (vr0.type != VR_RANGE || symbolic_range_p (&vr0))
2869 /* For division, if op1 has VR_RANGE but op0 does not, something
2870 can be deduced just from that range. Say [min, max] / [4, max]
2871 gives [min / 4, max / 4] range. */
2872 if (vr1.type == VR_RANGE
2873 && !symbolic_range_p (&vr1)
2874 && range_includes_zero_p (vr1.min, vr1.max) == 0)
2876 vr0.type = type = VR_RANGE;
2877 vr0.min = vrp_val_min (expr_type);
2878 vr0.max = vrp_val_max (expr_type);
2880 else
2882 set_value_range_to_varying (vr);
2883 return;
2887 /* For divisions, if flag_non_call_exceptions is true, we must
2888 not eliminate a division by zero. */
2889 if (cfun->can_throw_non_call_exceptions
2890 && (vr1.type != VR_RANGE
2891 || range_includes_zero_p (vr1.min, vr1.max) != 0))
2893 set_value_range_to_varying (vr);
2894 return;
2897 /* For divisions, if op0 is VR_RANGE, we can deduce a range
2898 even if op1 is VR_VARYING, VR_ANTI_RANGE, symbolic or can
2899 include 0. */
2900 if (vr0.type == VR_RANGE
2901 && (vr1.type != VR_RANGE
2902 || range_includes_zero_p (vr1.min, vr1.max) != 0))
2904 tree zero = build_int_cst (TREE_TYPE (vr0.min), 0);
2905 int cmp;
2907 min = NULL_TREE;
2908 max = NULL_TREE;
2909 if (TYPE_UNSIGNED (expr_type)
2910 || value_range_nonnegative_p (&vr1))
2912 /* For unsigned division or when divisor is known
2913 to be non-negative, the range has to cover
2914 all numbers from 0 to max for positive max
2915 and all numbers from min to 0 for negative min. */
2916 cmp = compare_values (vr0.max, zero);
2917 if (cmp == -1)
2918 max = zero;
2919 else if (cmp == 0 || cmp == 1)
2920 max = vr0.max;
2921 else
2922 type = VR_VARYING;
2923 cmp = compare_values (vr0.min, zero);
2924 if (cmp == 1)
2925 min = zero;
2926 else if (cmp == 0 || cmp == -1)
2927 min = vr0.min;
2928 else
2929 type = VR_VARYING;
2931 else
2933 /* Otherwise the range is -max .. max or min .. -min
2934 depending on which bound is bigger in absolute value,
2935 as the division can change the sign. */
2936 abs_extent_range (vr, vr0.min, vr0.max);
2937 return;
2939 if (type == VR_VARYING)
2941 set_value_range_to_varying (vr);
2942 return;
2945 else
2947 extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1);
2948 return;
2951 else if (code == TRUNC_MOD_EXPR)
2953 if (vr1.type != VR_RANGE
2954 || range_includes_zero_p (vr1.min, vr1.max) != 0
2955 || vrp_val_is_min (vr1.min))
2957 set_value_range_to_varying (vr);
2958 return;
2960 type = VR_RANGE;
2961 /* Compute MAX <|vr1.min|, |vr1.max|> - 1. */
2962 max = fold_unary_to_constant (ABS_EXPR, expr_type, vr1.min);
2963 if (tree_int_cst_lt (max, vr1.max))
2964 max = vr1.max;
2965 max = int_const_binop (MINUS_EXPR, max, integer_one_node);
2966 /* If the dividend is non-negative the modulus will be
2967 non-negative as well. */
2968 if (TYPE_UNSIGNED (expr_type)
2969 || value_range_nonnegative_p (&vr0))
2970 min = build_int_cst (TREE_TYPE (max), 0);
2971 else
2972 min = fold_unary_to_constant (NEGATE_EXPR, expr_type, max);
2974 else if (code == BIT_AND_EXPR || code == BIT_IOR_EXPR || code == BIT_XOR_EXPR)
2976 bool int_cst_range0, int_cst_range1;
2977 double_int may_be_nonzero0, may_be_nonzero1;
2978 double_int must_be_nonzero0, must_be_nonzero1;
2980 int_cst_range0 = zero_nonzero_bits_from_vr (&vr0, &may_be_nonzero0,
2981 &must_be_nonzero0);
2982 int_cst_range1 = zero_nonzero_bits_from_vr (&vr1, &may_be_nonzero1,
2983 &must_be_nonzero1);
2985 type = VR_RANGE;
2986 if (code == BIT_AND_EXPR)
2988 double_int dmax;
2989 min = double_int_to_tree (expr_type,
2990 must_be_nonzero0 & must_be_nonzero1);
2991 dmax = may_be_nonzero0 & may_be_nonzero1;
2992 /* If both input ranges contain only negative values we can
2993 truncate the result range maximum to the minimum of the
2994 input range maxima. */
2995 if (int_cst_range0 && int_cst_range1
2996 && tree_int_cst_sgn (vr0.max) < 0
2997 && tree_int_cst_sgn (vr1.max) < 0)
2999 dmax = dmax.min (tree_to_double_int (vr0.max),
3000 TYPE_UNSIGNED (expr_type));
3001 dmax = dmax.min (tree_to_double_int (vr1.max),
3002 TYPE_UNSIGNED (expr_type));
3004 /* If either input range contains only non-negative values
3005 we can truncate the result range maximum to the respective
3006 maximum of the input range. */
3007 if (int_cst_range0 && tree_int_cst_sgn (vr0.min) >= 0)
3008 dmax = dmax.min (tree_to_double_int (vr0.max),
3009 TYPE_UNSIGNED (expr_type));
3010 if (int_cst_range1 && tree_int_cst_sgn (vr1.min) >= 0)
3011 dmax = dmax.min (tree_to_double_int (vr1.max),
3012 TYPE_UNSIGNED (expr_type));
3013 max = double_int_to_tree (expr_type, dmax);
3015 else if (code == BIT_IOR_EXPR)
3017 double_int dmin;
3018 max = double_int_to_tree (expr_type,
3019 may_be_nonzero0 | may_be_nonzero1);
3020 dmin = must_be_nonzero0 | must_be_nonzero1;
3021 /* If the input ranges contain only positive values we can
3022 truncate the minimum of the result range to the maximum
3023 of the input range minima. */
3024 if (int_cst_range0 && int_cst_range1
3025 && tree_int_cst_sgn (vr0.min) >= 0
3026 && tree_int_cst_sgn (vr1.min) >= 0)
3028 dmin = dmin.max (tree_to_double_int (vr0.min),
3029 TYPE_UNSIGNED (expr_type));
3030 dmin = dmin.max (tree_to_double_int (vr1.min),
3031 TYPE_UNSIGNED (expr_type));
3033 /* If either input range contains only negative values
3034 we can truncate the minimum of the result range to the
3035 respective minimum range. */
3036 if (int_cst_range0 && tree_int_cst_sgn (vr0.max) < 0)
3037 dmin = dmin.max (tree_to_double_int (vr0.min),
3038 TYPE_UNSIGNED (expr_type));
3039 if (int_cst_range1 && tree_int_cst_sgn (vr1.max) < 0)
3040 dmin = dmin.max (tree_to_double_int (vr1.min),
3041 TYPE_UNSIGNED (expr_type));
3042 min = double_int_to_tree (expr_type, dmin);
3044 else if (code == BIT_XOR_EXPR)
3046 double_int result_zero_bits, result_one_bits;
3047 result_zero_bits = (must_be_nonzero0 & must_be_nonzero1)
3048 | ~(may_be_nonzero0 | may_be_nonzero1);
3049 result_one_bits = must_be_nonzero0.and_not (may_be_nonzero1)
3050 | must_be_nonzero1.and_not (may_be_nonzero0);
3051 max = double_int_to_tree (expr_type, ~result_zero_bits);
3052 min = double_int_to_tree (expr_type, result_one_bits);
3053 /* If the range has all positive or all negative values the
3054 result is better than VARYING. */
3055 if (tree_int_cst_sgn (min) < 0
3056 || tree_int_cst_sgn (max) >= 0)
3058 else
3059 max = min = NULL_TREE;
3062 else
3063 gcc_unreachable ();
3065 /* If either MIN or MAX overflowed, then set the resulting range to
3066 VARYING. But we do accept an overflow infinity
3067 representation. */
3068 if (min == NULL_TREE
3069 || !is_gimple_min_invariant (min)
3070 || (TREE_OVERFLOW (min) && !is_overflow_infinity (min))
3071 || max == NULL_TREE
3072 || !is_gimple_min_invariant (max)
3073 || (TREE_OVERFLOW (max) && !is_overflow_infinity (max)))
3075 set_value_range_to_varying (vr);
3076 return;
3079 /* We punt if:
3080 1) [-INF, +INF]
3081 2) [-INF, +-INF(OVF)]
3082 3) [+-INF(OVF), +INF]
3083 4) [+-INF(OVF), +-INF(OVF)]
3084 We learn nothing when we have INF and INF(OVF) on both sides.
3085 Note that we do accept [-INF, -INF] and [+INF, +INF] without
3086 overflow. */
3087 if ((vrp_val_is_min (min) || is_overflow_infinity (min))
3088 && (vrp_val_is_max (max) || is_overflow_infinity (max)))
3090 set_value_range_to_varying (vr);
3091 return;
3094 cmp = compare_values (min, max);
3095 if (cmp == -2 || cmp == 1)
3097 /* If the new range has its limits swapped around (MIN > MAX),
3098 then the operation caused one of them to wrap around, mark
3099 the new range VARYING. */
3100 set_value_range_to_varying (vr);
3102 else
3103 set_value_range (vr, type, min, max, NULL);
3106 /* Extract range information from a binary expression OP0 CODE OP1 based on
3107 the ranges of each of its operands with resulting type EXPR_TYPE.
3108 The resulting range is stored in *VR. */
3110 static void
3111 extract_range_from_binary_expr (value_range_t *vr,
3112 enum tree_code code,
3113 tree expr_type, tree op0, tree op1)
3115 value_range_t vr0 = VR_INITIALIZER;
3116 value_range_t vr1 = VR_INITIALIZER;
3118 /* Get value ranges for each operand. For constant operands, create
3119 a new value range with the operand to simplify processing. */
3120 if (TREE_CODE (op0) == SSA_NAME)
3121 vr0 = *(get_value_range (op0));
3122 else if (is_gimple_min_invariant (op0))
3123 set_value_range_to_value (&vr0, op0, NULL);
3124 else
3125 set_value_range_to_varying (&vr0);
3127 if (TREE_CODE (op1) == SSA_NAME)
3128 vr1 = *(get_value_range (op1));
3129 else if (is_gimple_min_invariant (op1))
3130 set_value_range_to_value (&vr1, op1, NULL);
3131 else
3132 set_value_range_to_varying (&vr1);
3134 extract_range_from_binary_expr_1 (vr, code, expr_type, &vr0, &vr1);
3137 /* Extract range information from a unary operation CODE based on
3138 the range of its operand *VR0 with type OP0_TYPE with resulting type TYPE.
3139 The The resulting range is stored in *VR. */
3141 static void
3142 extract_range_from_unary_expr_1 (value_range_t *vr,
3143 enum tree_code code, tree type,
3144 value_range_t *vr0_, tree op0_type)
3146 value_range_t vr0 = *vr0_, vrtem0 = VR_INITIALIZER, vrtem1 = VR_INITIALIZER;
3148 /* VRP only operates on integral and pointer types. */
3149 if (!(INTEGRAL_TYPE_P (op0_type)
3150 || POINTER_TYPE_P (op0_type))
3151 || !(INTEGRAL_TYPE_P (type)
3152 || POINTER_TYPE_P (type)))
3154 set_value_range_to_varying (vr);
3155 return;
3158 /* If VR0 is UNDEFINED, so is the result. */
3159 if (vr0.type == VR_UNDEFINED)
3161 set_value_range_to_undefined (vr);
3162 return;
3165 /* Handle operations that we express in terms of others. */
3166 if (code == PAREN_EXPR)
3168 /* PAREN_EXPR is a simple copy. */
3169 copy_value_range (vr, &vr0);
3170 return;
3172 else if (code == NEGATE_EXPR)
3174 /* -X is simply 0 - X, so re-use existing code that also handles
3175 anti-ranges fine. */
3176 value_range_t zero = VR_INITIALIZER;
3177 set_value_range_to_value (&zero, build_int_cst (type, 0), NULL);
3178 extract_range_from_binary_expr_1 (vr, MINUS_EXPR, type, &zero, &vr0);
3179 return;
3181 else if (code == BIT_NOT_EXPR)
3183 /* ~X is simply -1 - X, so re-use existing code that also handles
3184 anti-ranges fine. */
3185 value_range_t minusone = VR_INITIALIZER;
3186 set_value_range_to_value (&minusone, build_int_cst (type, -1), NULL);
3187 extract_range_from_binary_expr_1 (vr, MINUS_EXPR,
3188 type, &minusone, &vr0);
3189 return;
3192 /* Now canonicalize anti-ranges to ranges when they are not symbolic
3193 and express op ~[] as (op []') U (op []''). */
3194 if (vr0.type == VR_ANTI_RANGE
3195 && ranges_from_anti_range (&vr0, &vrtem0, &vrtem1))
3197 extract_range_from_unary_expr_1 (vr, code, type, &vrtem0, op0_type);
3198 if (vrtem1.type != VR_UNDEFINED)
3200 value_range_t vrres = VR_INITIALIZER;
3201 extract_range_from_unary_expr_1 (&vrres, code, type,
3202 &vrtem1, op0_type);
3203 vrp_meet (vr, &vrres);
3205 return;
3208 if (CONVERT_EXPR_CODE_P (code))
3210 tree inner_type = op0_type;
3211 tree outer_type = type;
3213 /* If the expression evaluates to a pointer, we are only interested in
3214 determining if it evaluates to NULL [0, 0] or non-NULL (~[0, 0]). */
3215 if (POINTER_TYPE_P (type))
3217 if (range_is_nonnull (&vr0))
3218 set_value_range_to_nonnull (vr, type);
3219 else if (range_is_null (&vr0))
3220 set_value_range_to_null (vr, type);
3221 else
3222 set_value_range_to_varying (vr);
3223 return;
3226 /* If VR0 is varying and we increase the type precision, assume
3227 a full range for the following transformation. */
3228 if (vr0.type == VR_VARYING
3229 && INTEGRAL_TYPE_P (inner_type)
3230 && TYPE_PRECISION (inner_type) < TYPE_PRECISION (outer_type))
3232 vr0.type = VR_RANGE;
3233 vr0.min = TYPE_MIN_VALUE (inner_type);
3234 vr0.max = TYPE_MAX_VALUE (inner_type);
3237 /* If VR0 is a constant range or anti-range and the conversion is
3238 not truncating we can convert the min and max values and
3239 canonicalize the resulting range. Otherwise we can do the
3240 conversion if the size of the range is less than what the
3241 precision of the target type can represent and the range is
3242 not an anti-range. */
3243 if ((vr0.type == VR_RANGE
3244 || vr0.type == VR_ANTI_RANGE)
3245 && TREE_CODE (vr0.min) == INTEGER_CST
3246 && TREE_CODE (vr0.max) == INTEGER_CST
3247 && (!is_overflow_infinity (vr0.min)
3248 || (vr0.type == VR_RANGE
3249 && TYPE_PRECISION (outer_type) > TYPE_PRECISION (inner_type)
3250 && needs_overflow_infinity (outer_type)
3251 && supports_overflow_infinity (outer_type)))
3252 && (!is_overflow_infinity (vr0.max)
3253 || (vr0.type == VR_RANGE
3254 && TYPE_PRECISION (outer_type) > TYPE_PRECISION (inner_type)
3255 && needs_overflow_infinity (outer_type)
3256 && supports_overflow_infinity (outer_type)))
3257 && (TYPE_PRECISION (outer_type) >= TYPE_PRECISION (inner_type)
3258 || (vr0.type == VR_RANGE
3259 && integer_zerop (int_const_binop (RSHIFT_EXPR,
3260 int_const_binop (MINUS_EXPR, vr0.max, vr0.min),
3261 size_int (TYPE_PRECISION (outer_type)))))))
3263 tree new_min, new_max;
3264 if (is_overflow_infinity (vr0.min))
3265 new_min = negative_overflow_infinity (outer_type);
3266 else
3267 new_min = force_fit_type_double (outer_type,
3268 tree_to_double_int (vr0.min),
3269 0, false);
3270 if (is_overflow_infinity (vr0.max))
3271 new_max = positive_overflow_infinity (outer_type);
3272 else
3273 new_max = force_fit_type_double (outer_type,
3274 tree_to_double_int (vr0.max),
3275 0, false);
3276 set_and_canonicalize_value_range (vr, vr0.type,
3277 new_min, new_max, NULL);
3278 return;
3281 set_value_range_to_varying (vr);
3282 return;
3284 else if (code == ABS_EXPR)
3286 tree min, max;
3287 int cmp;
3289 /* Pass through vr0 in the easy cases. */
3290 if (TYPE_UNSIGNED (type)
3291 || value_range_nonnegative_p (&vr0))
3293 copy_value_range (vr, &vr0);
3294 return;
3297 /* For the remaining varying or symbolic ranges we can't do anything
3298 useful. */
3299 if (vr0.type == VR_VARYING
3300 || symbolic_range_p (&vr0))
3302 set_value_range_to_varying (vr);
3303 return;
3306 /* -TYPE_MIN_VALUE = TYPE_MIN_VALUE with flag_wrapv so we can't get a
3307 useful range. */
3308 if (!TYPE_OVERFLOW_UNDEFINED (type)
3309 && ((vr0.type == VR_RANGE
3310 && vrp_val_is_min (vr0.min))
3311 || (vr0.type == VR_ANTI_RANGE
3312 && !vrp_val_is_min (vr0.min))))
3314 set_value_range_to_varying (vr);
3315 return;
3318 /* ABS_EXPR may flip the range around, if the original range
3319 included negative values. */
3320 if (is_overflow_infinity (vr0.min))
3321 min = positive_overflow_infinity (type);
3322 else if (!vrp_val_is_min (vr0.min))
3323 min = fold_unary_to_constant (code, type, vr0.min);
3324 else if (!needs_overflow_infinity (type))
3325 min = TYPE_MAX_VALUE (type);
3326 else if (supports_overflow_infinity (type))
3327 min = positive_overflow_infinity (type);
3328 else
3330 set_value_range_to_varying (vr);
3331 return;
3334 if (is_overflow_infinity (vr0.max))
3335 max = positive_overflow_infinity (type);
3336 else if (!vrp_val_is_min (vr0.max))
3337 max = fold_unary_to_constant (code, type, vr0.max);
3338 else if (!needs_overflow_infinity (type))
3339 max = TYPE_MAX_VALUE (type);
3340 else if (supports_overflow_infinity (type)
3341 /* We shouldn't generate [+INF, +INF] as set_value_range
3342 doesn't like this and ICEs. */
3343 && !is_positive_overflow_infinity (min))
3344 max = positive_overflow_infinity (type);
3345 else
3347 set_value_range_to_varying (vr);
3348 return;
3351 cmp = compare_values (min, max);
3353 /* If a VR_ANTI_RANGEs contains zero, then we have
3354 ~[-INF, min(MIN, MAX)]. */
3355 if (vr0.type == VR_ANTI_RANGE)
3357 if (range_includes_zero_p (vr0.min, vr0.max) == 1)
3359 /* Take the lower of the two values. */
3360 if (cmp != 1)
3361 max = min;
3363 /* Create ~[-INF, min (abs(MIN), abs(MAX))]
3364 or ~[-INF + 1, min (abs(MIN), abs(MAX))] when
3365 flag_wrapv is set and the original anti-range doesn't include
3366 TYPE_MIN_VALUE, remember -TYPE_MIN_VALUE = TYPE_MIN_VALUE. */
3367 if (TYPE_OVERFLOW_WRAPS (type))
3369 tree type_min_value = TYPE_MIN_VALUE (type);
3371 min = (vr0.min != type_min_value
3372 ? int_const_binop (PLUS_EXPR, type_min_value,
3373 integer_one_node)
3374 : type_min_value);
3376 else
3378 if (overflow_infinity_range_p (&vr0))
3379 min = negative_overflow_infinity (type);
3380 else
3381 min = TYPE_MIN_VALUE (type);
3384 else
3386 /* All else has failed, so create the range [0, INF], even for
3387 flag_wrapv since TYPE_MIN_VALUE is in the original
3388 anti-range. */
3389 vr0.type = VR_RANGE;
3390 min = build_int_cst (type, 0);
3391 if (needs_overflow_infinity (type))
3393 if (supports_overflow_infinity (type))
3394 max = positive_overflow_infinity (type);
3395 else
3397 set_value_range_to_varying (vr);
3398 return;
3401 else
3402 max = TYPE_MAX_VALUE (type);
3406 /* If the range contains zero then we know that the minimum value in the
3407 range will be zero. */
3408 else if (range_includes_zero_p (vr0.min, vr0.max) == 1)
3410 if (cmp == 1)
3411 max = min;
3412 min = build_int_cst (type, 0);
3414 else
3416 /* If the range was reversed, swap MIN and MAX. */
3417 if (cmp == 1)
3419 tree t = min;
3420 min = max;
3421 max = t;
3425 cmp = compare_values (min, max);
3426 if (cmp == -2 || cmp == 1)
3428 /* If the new range has its limits swapped around (MIN > MAX),
3429 then the operation caused one of them to wrap around, mark
3430 the new range VARYING. */
3431 set_value_range_to_varying (vr);
3433 else
3434 set_value_range (vr, vr0.type, min, max, NULL);
3435 return;
3438 /* For unhandled operations fall back to varying. */
3439 set_value_range_to_varying (vr);
3440 return;
3444 /* Extract range information from a unary expression CODE OP0 based on
3445 the range of its operand with resulting type TYPE.
3446 The resulting range is stored in *VR. */
3448 static void
3449 extract_range_from_unary_expr (value_range_t *vr, enum tree_code code,
3450 tree type, tree op0)
3452 value_range_t vr0 = VR_INITIALIZER;
3454 /* Get value ranges for the operand. For constant operands, create
3455 a new value range with the operand to simplify processing. */
3456 if (TREE_CODE (op0) == SSA_NAME)
3457 vr0 = *(get_value_range (op0));
3458 else if (is_gimple_min_invariant (op0))
3459 set_value_range_to_value (&vr0, op0, NULL);
3460 else
3461 set_value_range_to_varying (&vr0);
3463 extract_range_from_unary_expr_1 (vr, code, type, &vr0, TREE_TYPE (op0));
3467 /* Extract range information from a conditional expression STMT based on
3468 the ranges of each of its operands and the expression code. */
3470 static void
3471 extract_range_from_cond_expr (value_range_t *vr, gimple stmt)
3473 tree op0, op1;
3474 value_range_t vr0 = VR_INITIALIZER;
3475 value_range_t vr1 = VR_INITIALIZER;
3477 /* Get value ranges for each operand. For constant operands, create
3478 a new value range with the operand to simplify processing. */
3479 op0 = gimple_assign_rhs2 (stmt);
3480 if (TREE_CODE (op0) == SSA_NAME)
3481 vr0 = *(get_value_range (op0));
3482 else if (is_gimple_min_invariant (op0))
3483 set_value_range_to_value (&vr0, op0, NULL);
3484 else
3485 set_value_range_to_varying (&vr0);
3487 op1 = gimple_assign_rhs3 (stmt);
3488 if (TREE_CODE (op1) == SSA_NAME)
3489 vr1 = *(get_value_range (op1));
3490 else if (is_gimple_min_invariant (op1))
3491 set_value_range_to_value (&vr1, op1, NULL);
3492 else
3493 set_value_range_to_varying (&vr1);
3495 /* The resulting value range is the union of the operand ranges */
3496 copy_value_range (vr, &vr0);
3497 vrp_meet (vr, &vr1);
3501 /* Extract range information from a comparison expression EXPR based
3502 on the range of its operand and the expression code. */
3504 static void
3505 extract_range_from_comparison (value_range_t *vr, enum tree_code code,
3506 tree type, tree op0, tree op1)
3508 bool sop = false;
3509 tree val;
3511 val = vrp_evaluate_conditional_warnv_with_ops (code, op0, op1, false, &sop,
3512 NULL);
3514 /* A disadvantage of using a special infinity as an overflow
3515 representation is that we lose the ability to record overflow
3516 when we don't have an infinity. So we have to ignore a result
3517 which relies on overflow. */
3519 if (val && !is_overflow_infinity (val) && !sop)
3521 /* Since this expression was found on the RHS of an assignment,
3522 its type may be different from _Bool. Convert VAL to EXPR's
3523 type. */
3524 val = fold_convert (type, val);
3525 if (is_gimple_min_invariant (val))
3526 set_value_range_to_value (vr, val, vr->equiv);
3527 else
3528 set_value_range (vr, VR_RANGE, val, val, vr->equiv);
3530 else
3531 /* The result of a comparison is always true or false. */
3532 set_value_range_to_truthvalue (vr, type);
3535 /* Try to derive a nonnegative or nonzero range out of STMT relying
3536 primarily on generic routines in fold in conjunction with range data.
3537 Store the result in *VR */
3539 static void
3540 extract_range_basic (value_range_t *vr, gimple stmt)
3542 bool sop = false;
3543 tree type = gimple_expr_type (stmt);
3545 if (INTEGRAL_TYPE_P (type)
3546 && gimple_stmt_nonnegative_warnv_p (stmt, &sop))
3547 set_value_range_to_nonnegative (vr, type,
3548 sop || stmt_overflow_infinity (stmt));
3549 else if (vrp_stmt_computes_nonzero (stmt, &sop)
3550 && !sop)
3551 set_value_range_to_nonnull (vr, type);
3552 else
3553 set_value_range_to_varying (vr);
3557 /* Try to compute a useful range out of assignment STMT and store it
3558 in *VR. */
3560 static void
3561 extract_range_from_assignment (value_range_t *vr, gimple stmt)
3563 enum tree_code code = gimple_assign_rhs_code (stmt);
3565 if (code == ASSERT_EXPR)
3566 extract_range_from_assert (vr, gimple_assign_rhs1 (stmt));
3567 else if (code == SSA_NAME)
3568 extract_range_from_ssa_name (vr, gimple_assign_rhs1 (stmt));
3569 else if (TREE_CODE_CLASS (code) == tcc_binary)
3570 extract_range_from_binary_expr (vr, gimple_assign_rhs_code (stmt),
3571 gimple_expr_type (stmt),
3572 gimple_assign_rhs1 (stmt),
3573 gimple_assign_rhs2 (stmt));
3574 else if (TREE_CODE_CLASS (code) == tcc_unary)
3575 extract_range_from_unary_expr (vr, gimple_assign_rhs_code (stmt),
3576 gimple_expr_type (stmt),
3577 gimple_assign_rhs1 (stmt));
3578 else if (code == COND_EXPR)
3579 extract_range_from_cond_expr (vr, stmt);
3580 else if (TREE_CODE_CLASS (code) == tcc_comparison)
3581 extract_range_from_comparison (vr, gimple_assign_rhs_code (stmt),
3582 gimple_expr_type (stmt),
3583 gimple_assign_rhs1 (stmt),
3584 gimple_assign_rhs2 (stmt));
3585 else if (get_gimple_rhs_class (code) == GIMPLE_SINGLE_RHS
3586 && is_gimple_min_invariant (gimple_assign_rhs1 (stmt)))
3587 set_value_range_to_value (vr, gimple_assign_rhs1 (stmt), NULL);
3588 else
3589 set_value_range_to_varying (vr);
3591 if (vr->type == VR_VARYING)
3592 extract_range_basic (vr, stmt);
3595 /* Given a range VR, a LOOP and a variable VAR, determine whether it
3596 would be profitable to adjust VR using scalar evolution information
3597 for VAR. If so, update VR with the new limits. */
3599 static void
3600 adjust_range_with_scev (value_range_t *vr, struct loop *loop,
3601 gimple stmt, tree var)
3603 tree init, step, chrec, tmin, tmax, min, max, type, tem;
3604 enum ev_direction dir;
3606 /* TODO. Don't adjust anti-ranges. An anti-range may provide
3607 better opportunities than a regular range, but I'm not sure. */
3608 if (vr->type == VR_ANTI_RANGE)
3609 return;
3611 chrec = instantiate_parameters (loop, analyze_scalar_evolution (loop, var));
3613 /* Like in PR19590, scev can return a constant function. */
3614 if (is_gimple_min_invariant (chrec))
3616 set_value_range_to_value (vr, chrec, vr->equiv);
3617 return;
3620 if (TREE_CODE (chrec) != POLYNOMIAL_CHREC)
3621 return;
3623 init = initial_condition_in_loop_num (chrec, loop->num);
3624 tem = op_with_constant_singleton_value_range (init);
3625 if (tem)
3626 init = tem;
3627 step = evolution_part_in_loop_num (chrec, loop->num);
3628 tem = op_with_constant_singleton_value_range (step);
3629 if (tem)
3630 step = tem;
3632 /* If STEP is symbolic, we can't know whether INIT will be the
3633 minimum or maximum value in the range. Also, unless INIT is
3634 a simple expression, compare_values and possibly other functions
3635 in tree-vrp won't be able to handle it. */
3636 if (step == NULL_TREE
3637 || !is_gimple_min_invariant (step)
3638 || !valid_value_p (init))
3639 return;
3641 dir = scev_direction (chrec);
3642 if (/* Do not adjust ranges if we do not know whether the iv increases
3643 or decreases, ... */
3644 dir == EV_DIR_UNKNOWN
3645 /* ... or if it may wrap. */
3646 || scev_probably_wraps_p (init, step, stmt, get_chrec_loop (chrec),
3647 true))
3648 return;
3650 /* We use TYPE_MIN_VALUE and TYPE_MAX_VALUE here instead of
3651 negative_overflow_infinity and positive_overflow_infinity,
3652 because we have concluded that the loop probably does not
3653 wrap. */
3655 type = TREE_TYPE (var);
3656 if (POINTER_TYPE_P (type) || !TYPE_MIN_VALUE (type))
3657 tmin = lower_bound_in_type (type, type);
3658 else
3659 tmin = TYPE_MIN_VALUE (type);
3660 if (POINTER_TYPE_P (type) || !TYPE_MAX_VALUE (type))
3661 tmax = upper_bound_in_type (type, type);
3662 else
3663 tmax = TYPE_MAX_VALUE (type);
3665 /* Try to use estimated number of iterations for the loop to constrain the
3666 final value in the evolution. */
3667 if (TREE_CODE (step) == INTEGER_CST
3668 && is_gimple_val (init)
3669 && (TREE_CODE (init) != SSA_NAME
3670 || get_value_range (init)->type == VR_RANGE))
3672 double_int nit;
3674 /* We are only entering here for loop header PHI nodes, so using
3675 the number of latch executions is the correct thing to use. */
3676 if (max_loop_iterations (loop, &nit))
3678 value_range_t maxvr = VR_INITIALIZER;
3679 double_int dtmp;
3680 bool unsigned_p = TYPE_UNSIGNED (TREE_TYPE (step));
3681 bool overflow = false;
3683 dtmp = tree_to_double_int (step)
3684 .mul_with_sign (nit, unsigned_p, &overflow);
3685 /* If the multiplication overflowed we can't do a meaningful
3686 adjustment. Likewise if the result doesn't fit in the type
3687 of the induction variable. For a signed type we have to
3688 check whether the result has the expected signedness which
3689 is that of the step as number of iterations is unsigned. */
3690 if (!overflow
3691 && double_int_fits_to_tree_p (TREE_TYPE (init), dtmp)
3692 && (unsigned_p
3693 || ((dtmp.high ^ TREE_INT_CST_HIGH (step)) >= 0)))
3695 tem = double_int_to_tree (TREE_TYPE (init), dtmp);
3696 extract_range_from_binary_expr (&maxvr, PLUS_EXPR,
3697 TREE_TYPE (init), init, tem);
3698 /* Likewise if the addition did. */
3699 if (maxvr.type == VR_RANGE)
3701 tmin = maxvr.min;
3702 tmax = maxvr.max;
3708 if (vr->type == VR_VARYING || vr->type == VR_UNDEFINED)
3710 min = tmin;
3711 max = tmax;
3713 /* For VARYING or UNDEFINED ranges, just about anything we get
3714 from scalar evolutions should be better. */
3716 if (dir == EV_DIR_DECREASES)
3717 max = init;
3718 else
3719 min = init;
3721 /* If we would create an invalid range, then just assume we
3722 know absolutely nothing. This may be over-conservative,
3723 but it's clearly safe, and should happen only in unreachable
3724 parts of code, or for invalid programs. */
3725 if (compare_values (min, max) == 1)
3726 return;
3728 set_value_range (vr, VR_RANGE, min, max, vr->equiv);
3730 else if (vr->type == VR_RANGE)
3732 min = vr->min;
3733 max = vr->max;
3735 if (dir == EV_DIR_DECREASES)
3737 /* INIT is the maximum value. If INIT is lower than VR->MAX
3738 but no smaller than VR->MIN, set VR->MAX to INIT. */
3739 if (compare_values (init, max) == -1)
3740 max = init;
3742 /* According to the loop information, the variable does not
3743 overflow. If we think it does, probably because of an
3744 overflow due to arithmetic on a different INF value,
3745 reset now. */
3746 if (is_negative_overflow_infinity (min)
3747 || compare_values (min, tmin) == -1)
3748 min = tmin;
3751 else
3753 /* If INIT is bigger than VR->MIN, set VR->MIN to INIT. */
3754 if (compare_values (init, min) == 1)
3755 min = init;
3757 if (is_positive_overflow_infinity (max)
3758 || compare_values (tmax, max) == -1)
3759 max = tmax;
3762 /* If we just created an invalid range with the minimum
3763 greater than the maximum, we fail conservatively.
3764 This should happen only in unreachable
3765 parts of code, or for invalid programs. */
3766 if (compare_values (min, max) == 1)
3767 return;
3769 set_value_range (vr, VR_RANGE, min, max, vr->equiv);
3773 /* Return true if VAR may overflow at STMT. This checks any available
3774 loop information to see if we can determine that VAR does not
3775 overflow. */
3777 static bool
3778 vrp_var_may_overflow (tree var, gimple stmt)
3780 struct loop *l;
3781 tree chrec, init, step;
3783 if (current_loops == NULL)
3784 return true;
3786 l = loop_containing_stmt (stmt);
3787 if (l == NULL
3788 || !loop_outer (l))
3789 return true;
3791 chrec = instantiate_parameters (l, analyze_scalar_evolution (l, var));
3792 if (TREE_CODE (chrec) != POLYNOMIAL_CHREC)
3793 return true;
3795 init = initial_condition_in_loop_num (chrec, l->num);
3796 step = evolution_part_in_loop_num (chrec, l->num);
3798 if (step == NULL_TREE
3799 || !is_gimple_min_invariant (step)
3800 || !valid_value_p (init))
3801 return true;
3803 /* If we get here, we know something useful about VAR based on the
3804 loop information. If it wraps, it may overflow. */
3806 if (scev_probably_wraps_p (init, step, stmt, get_chrec_loop (chrec),
3807 true))
3808 return true;
3810 if (dump_file && (dump_flags & TDF_DETAILS) != 0)
3812 print_generic_expr (dump_file, var, 0);
3813 fprintf (dump_file, ": loop information indicates does not overflow\n");
3816 return false;
3820 /* Given two numeric value ranges VR0, VR1 and a comparison code COMP:
3822 - Return BOOLEAN_TRUE_NODE if VR0 COMP VR1 always returns true for
3823 all the values in the ranges.
3825 - Return BOOLEAN_FALSE_NODE if the comparison always returns false.
3827 - Return NULL_TREE if it is not always possible to determine the
3828 value of the comparison.
3830 Also set *STRICT_OVERFLOW_P to indicate whether a range with an
3831 overflow infinity was used in the test. */
3834 static tree
3835 compare_ranges (enum tree_code comp, value_range_t *vr0, value_range_t *vr1,
3836 bool *strict_overflow_p)
3838 /* VARYING or UNDEFINED ranges cannot be compared. */
3839 if (vr0->type == VR_VARYING
3840 || vr0->type == VR_UNDEFINED
3841 || vr1->type == VR_VARYING
3842 || vr1->type == VR_UNDEFINED)
3843 return NULL_TREE;
3845 /* Anti-ranges need to be handled separately. */
3846 if (vr0->type == VR_ANTI_RANGE || vr1->type == VR_ANTI_RANGE)
3848 /* If both are anti-ranges, then we cannot compute any
3849 comparison. */
3850 if (vr0->type == VR_ANTI_RANGE && vr1->type == VR_ANTI_RANGE)
3851 return NULL_TREE;
3853 /* These comparisons are never statically computable. */
3854 if (comp == GT_EXPR
3855 || comp == GE_EXPR
3856 || comp == LT_EXPR
3857 || comp == LE_EXPR)
3858 return NULL_TREE;
3860 /* Equality can be computed only between a range and an
3861 anti-range. ~[VAL1, VAL2] == [VAL1, VAL2] is always false. */
3862 if (vr0->type == VR_RANGE)
3864 /* To simplify processing, make VR0 the anti-range. */
3865 value_range_t *tmp = vr0;
3866 vr0 = vr1;
3867 vr1 = tmp;
3870 gcc_assert (comp == NE_EXPR || comp == EQ_EXPR);
3872 if (compare_values_warnv (vr0->min, vr1->min, strict_overflow_p) == 0
3873 && compare_values_warnv (vr0->max, vr1->max, strict_overflow_p) == 0)
3874 return (comp == NE_EXPR) ? boolean_true_node : boolean_false_node;
3876 return NULL_TREE;
3879 if (!usable_range_p (vr0, strict_overflow_p)
3880 || !usable_range_p (vr1, strict_overflow_p))
3881 return NULL_TREE;
3883 /* Simplify processing. If COMP is GT_EXPR or GE_EXPR, switch the
3884 operands around and change the comparison code. */
3885 if (comp == GT_EXPR || comp == GE_EXPR)
3887 value_range_t *tmp;
3888 comp = (comp == GT_EXPR) ? LT_EXPR : LE_EXPR;
3889 tmp = vr0;
3890 vr0 = vr1;
3891 vr1 = tmp;
3894 if (comp == EQ_EXPR)
3896 /* Equality may only be computed if both ranges represent
3897 exactly one value. */
3898 if (compare_values_warnv (vr0->min, vr0->max, strict_overflow_p) == 0
3899 && compare_values_warnv (vr1->min, vr1->max, strict_overflow_p) == 0)
3901 int cmp_min = compare_values_warnv (vr0->min, vr1->min,
3902 strict_overflow_p);
3903 int cmp_max = compare_values_warnv (vr0->max, vr1->max,
3904 strict_overflow_p);
3905 if (cmp_min == 0 && cmp_max == 0)
3906 return boolean_true_node;
3907 else if (cmp_min != -2 && cmp_max != -2)
3908 return boolean_false_node;
3910 /* If [V0_MIN, V1_MAX] < [V1_MIN, V1_MAX] then V0 != V1. */
3911 else if (compare_values_warnv (vr0->min, vr1->max,
3912 strict_overflow_p) == 1
3913 || compare_values_warnv (vr1->min, vr0->max,
3914 strict_overflow_p) == 1)
3915 return boolean_false_node;
3917 return NULL_TREE;
3919 else if (comp == NE_EXPR)
3921 int cmp1, cmp2;
3923 /* If VR0 is completely to the left or completely to the right
3924 of VR1, they are always different. Notice that we need to
3925 make sure that both comparisons yield similar results to
3926 avoid comparing values that cannot be compared at
3927 compile-time. */
3928 cmp1 = compare_values_warnv (vr0->max, vr1->min, strict_overflow_p);
3929 cmp2 = compare_values_warnv (vr0->min, vr1->max, strict_overflow_p);
3930 if ((cmp1 == -1 && cmp2 == -1) || (cmp1 == 1 && cmp2 == 1))
3931 return boolean_true_node;
3933 /* If VR0 and VR1 represent a single value and are identical,
3934 return false. */
3935 else if (compare_values_warnv (vr0->min, vr0->max,
3936 strict_overflow_p) == 0
3937 && compare_values_warnv (vr1->min, vr1->max,
3938 strict_overflow_p) == 0
3939 && compare_values_warnv (vr0->min, vr1->min,
3940 strict_overflow_p) == 0
3941 && compare_values_warnv (vr0->max, vr1->max,
3942 strict_overflow_p) == 0)
3943 return boolean_false_node;
3945 /* Otherwise, they may or may not be different. */
3946 else
3947 return NULL_TREE;
3949 else if (comp == LT_EXPR || comp == LE_EXPR)
3951 int tst;
3953 /* If VR0 is to the left of VR1, return true. */
3954 tst = compare_values_warnv (vr0->max, vr1->min, strict_overflow_p);
3955 if ((comp == LT_EXPR && tst == -1)
3956 || (comp == LE_EXPR && (tst == -1 || tst == 0)))
3958 if (overflow_infinity_range_p (vr0)
3959 || overflow_infinity_range_p (vr1))
3960 *strict_overflow_p = true;
3961 return boolean_true_node;
3964 /* If VR0 is to the right of VR1, return false. */
3965 tst = compare_values_warnv (vr0->min, vr1->max, strict_overflow_p);
3966 if ((comp == LT_EXPR && (tst == 0 || tst == 1))
3967 || (comp == LE_EXPR && tst == 1))
3969 if (overflow_infinity_range_p (vr0)
3970 || overflow_infinity_range_p (vr1))
3971 *strict_overflow_p = true;
3972 return boolean_false_node;
3975 /* Otherwise, we don't know. */
3976 return NULL_TREE;
3979 gcc_unreachable ();
3983 /* Given a value range VR, a value VAL and a comparison code COMP, return
3984 BOOLEAN_TRUE_NODE if VR COMP VAL always returns true for all the
3985 values in VR. Return BOOLEAN_FALSE_NODE if the comparison
3986 always returns false. Return NULL_TREE if it is not always
3987 possible to determine the value of the comparison. Also set
3988 *STRICT_OVERFLOW_P to indicate whether a range with an overflow
3989 infinity was used in the test. */
3991 static tree
3992 compare_range_with_value (enum tree_code comp, value_range_t *vr, tree val,
3993 bool *strict_overflow_p)
3995 if (vr->type == VR_VARYING || vr->type == VR_UNDEFINED)
3996 return NULL_TREE;
3998 /* Anti-ranges need to be handled separately. */
3999 if (vr->type == VR_ANTI_RANGE)
4001 /* For anti-ranges, the only predicates that we can compute at
4002 compile time are equality and inequality. */
4003 if (comp == GT_EXPR
4004 || comp == GE_EXPR
4005 || comp == LT_EXPR
4006 || comp == LE_EXPR)
4007 return NULL_TREE;
4009 /* ~[VAL_1, VAL_2] OP VAL is known if VAL_1 <= VAL <= VAL_2. */
4010 if (value_inside_range (val, vr->min, vr->max) == 1)
4011 return (comp == NE_EXPR) ? boolean_true_node : boolean_false_node;
4013 return NULL_TREE;
4016 if (!usable_range_p (vr, strict_overflow_p))
4017 return NULL_TREE;
4019 if (comp == EQ_EXPR)
4021 /* EQ_EXPR may only be computed if VR represents exactly
4022 one value. */
4023 if (compare_values_warnv (vr->min, vr->max, strict_overflow_p) == 0)
4025 int cmp = compare_values_warnv (vr->min, val, strict_overflow_p);
4026 if (cmp == 0)
4027 return boolean_true_node;
4028 else if (cmp == -1 || cmp == 1 || cmp == 2)
4029 return boolean_false_node;
4031 else if (compare_values_warnv (val, vr->min, strict_overflow_p) == -1
4032 || compare_values_warnv (vr->max, val, strict_overflow_p) == -1)
4033 return boolean_false_node;
4035 return NULL_TREE;
4037 else if (comp == NE_EXPR)
4039 /* If VAL is not inside VR, then they are always different. */
4040 if (compare_values_warnv (vr->max, val, strict_overflow_p) == -1
4041 || compare_values_warnv (vr->min, val, strict_overflow_p) == 1)
4042 return boolean_true_node;
4044 /* If VR represents exactly one value equal to VAL, then return
4045 false. */
4046 if (compare_values_warnv (vr->min, vr->max, strict_overflow_p) == 0
4047 && compare_values_warnv (vr->min, val, strict_overflow_p) == 0)
4048 return boolean_false_node;
4050 /* Otherwise, they may or may not be different. */
4051 return NULL_TREE;
4053 else if (comp == LT_EXPR || comp == LE_EXPR)
4055 int tst;
4057 /* If VR is to the left of VAL, return true. */
4058 tst = compare_values_warnv (vr->max, val, strict_overflow_p);
4059 if ((comp == LT_EXPR && tst == -1)
4060 || (comp == LE_EXPR && (tst == -1 || tst == 0)))
4062 if (overflow_infinity_range_p (vr))
4063 *strict_overflow_p = true;
4064 return boolean_true_node;
4067 /* If VR is to the right of VAL, return false. */
4068 tst = compare_values_warnv (vr->min, val, strict_overflow_p);
4069 if ((comp == LT_EXPR && (tst == 0 || tst == 1))
4070 || (comp == LE_EXPR && tst == 1))
4072 if (overflow_infinity_range_p (vr))
4073 *strict_overflow_p = true;
4074 return boolean_false_node;
4077 /* Otherwise, we don't know. */
4078 return NULL_TREE;
4080 else if (comp == GT_EXPR || comp == GE_EXPR)
4082 int tst;
4084 /* If VR is to the right of VAL, return true. */
4085 tst = compare_values_warnv (vr->min, val, strict_overflow_p);
4086 if ((comp == GT_EXPR && tst == 1)
4087 || (comp == GE_EXPR && (tst == 0 || tst == 1)))
4089 if (overflow_infinity_range_p (vr))
4090 *strict_overflow_p = true;
4091 return boolean_true_node;
4094 /* If VR is to the left of VAL, return false. */
4095 tst = compare_values_warnv (vr->max, val, strict_overflow_p);
4096 if ((comp == GT_EXPR && (tst == -1 || tst == 0))
4097 || (comp == GE_EXPR && tst == -1))
4099 if (overflow_infinity_range_p (vr))
4100 *strict_overflow_p = true;
4101 return boolean_false_node;
4104 /* Otherwise, we don't know. */
4105 return NULL_TREE;
4108 gcc_unreachable ();
4112 /* Debugging dumps. */
4114 void dump_value_range (FILE *, value_range_t *);
4115 void debug_value_range (value_range_t *);
4116 void dump_all_value_ranges (FILE *);
4117 void debug_all_value_ranges (void);
4118 void dump_vr_equiv (FILE *, bitmap);
4119 void debug_vr_equiv (bitmap);
4122 /* Dump value range VR to FILE. */
4124 void
4125 dump_value_range (FILE *file, value_range_t *vr)
4127 if (vr == NULL)
4128 fprintf (file, "[]");
4129 else if (vr->type == VR_UNDEFINED)
4130 fprintf (file, "UNDEFINED");
4131 else if (vr->type == VR_RANGE || vr->type == VR_ANTI_RANGE)
4133 tree type = TREE_TYPE (vr->min);
4135 fprintf (file, "%s[", (vr->type == VR_ANTI_RANGE) ? "~" : "");
4137 if (is_negative_overflow_infinity (vr->min))
4138 fprintf (file, "-INF(OVF)");
4139 else if (INTEGRAL_TYPE_P (type)
4140 && !TYPE_UNSIGNED (type)
4141 && vrp_val_is_min (vr->min))
4142 fprintf (file, "-INF");
4143 else
4144 print_generic_expr (file, vr->min, 0);
4146 fprintf (file, ", ");
4148 if (is_positive_overflow_infinity (vr->max))
4149 fprintf (file, "+INF(OVF)");
4150 else if (INTEGRAL_TYPE_P (type)
4151 && vrp_val_is_max (vr->max))
4152 fprintf (file, "+INF");
4153 else
4154 print_generic_expr (file, vr->max, 0);
4156 fprintf (file, "]");
4158 if (vr->equiv)
4160 bitmap_iterator bi;
4161 unsigned i, c = 0;
4163 fprintf (file, " EQUIVALENCES: { ");
4165 EXECUTE_IF_SET_IN_BITMAP (vr->equiv, 0, i, bi)
4167 print_generic_expr (file, ssa_name (i), 0);
4168 fprintf (file, " ");
4169 c++;
4172 fprintf (file, "} (%u elements)", c);
4175 else if (vr->type == VR_VARYING)
4176 fprintf (file, "VARYING");
4177 else
4178 fprintf (file, "INVALID RANGE");
4182 /* Dump value range VR to stderr. */
4184 DEBUG_FUNCTION void
4185 debug_value_range (value_range_t *vr)
4187 dump_value_range (stderr, vr);
4188 fprintf (stderr, "\n");
4192 /* Dump value ranges of all SSA_NAMEs to FILE. */
4194 void
4195 dump_all_value_ranges (FILE *file)
4197 size_t i;
4199 for (i = 0; i < num_vr_values; i++)
4201 if (vr_value[i])
4203 print_generic_expr (file, ssa_name (i), 0);
4204 fprintf (file, ": ");
4205 dump_value_range (file, vr_value[i]);
4206 fprintf (file, "\n");
4210 fprintf (file, "\n");
4214 /* Dump all value ranges to stderr. */
4216 DEBUG_FUNCTION void
4217 debug_all_value_ranges (void)
4219 dump_all_value_ranges (stderr);
4223 /* Given a COND_EXPR COND of the form 'V OP W', and an SSA name V,
4224 create a new SSA name N and return the assertion assignment
4225 'V = ASSERT_EXPR <V, V OP W>'. */
4227 static gimple
4228 build_assert_expr_for (tree cond, tree v)
4230 tree a;
4231 gimple assertion;
4233 gcc_assert (TREE_CODE (v) == SSA_NAME
4234 && COMPARISON_CLASS_P (cond));
4236 a = build2 (ASSERT_EXPR, TREE_TYPE (v), v, cond);
4237 assertion = gimple_build_assign (NULL_TREE, a);
4239 /* The new ASSERT_EXPR, creates a new SSA name that replaces the
4240 operand of the ASSERT_EXPR. Create it so the new name and the old one
4241 are registered in the replacement table so that we can fix the SSA web
4242 after adding all the ASSERT_EXPRs. */
4243 create_new_def_for (v, assertion, NULL);
4245 return assertion;
4249 /* Return false if EXPR is a predicate expression involving floating
4250 point values. */
4252 static inline bool
4253 fp_predicate (gimple stmt)
4255 GIMPLE_CHECK (stmt, GIMPLE_COND);
4257 return FLOAT_TYPE_P (TREE_TYPE (gimple_cond_lhs (stmt)));
4261 /* If the range of values taken by OP can be inferred after STMT executes,
4262 return the comparison code (COMP_CODE_P) and value (VAL_P) that
4263 describes the inferred range. Return true if a range could be
4264 inferred. */
4266 static bool
4267 infer_value_range (gimple stmt, tree op, enum tree_code *comp_code_p, tree *val_p)
4269 *val_p = NULL_TREE;
4270 *comp_code_p = ERROR_MARK;
4272 /* Do not attempt to infer anything in names that flow through
4273 abnormal edges. */
4274 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op))
4275 return false;
4277 /* Similarly, don't infer anything from statements that may throw
4278 exceptions. */
4279 if (stmt_could_throw_p (stmt))
4280 return false;
4282 /* If STMT is the last statement of a basic block with no
4283 successors, there is no point inferring anything about any of its
4284 operands. We would not be able to find a proper insertion point
4285 for the assertion, anyway. */
4286 if (stmt_ends_bb_p (stmt) && EDGE_COUNT (gimple_bb (stmt)->succs) == 0)
4287 return false;
4289 /* We can only assume that a pointer dereference will yield
4290 non-NULL if -fdelete-null-pointer-checks is enabled. */
4291 if (flag_delete_null_pointer_checks
4292 && POINTER_TYPE_P (TREE_TYPE (op))
4293 && gimple_code (stmt) != GIMPLE_ASM)
4295 unsigned num_uses, num_loads, num_stores;
4297 count_uses_and_derefs (op, stmt, &num_uses, &num_loads, &num_stores);
4298 if (num_loads + num_stores > 0)
4300 *val_p = build_int_cst (TREE_TYPE (op), 0);
4301 *comp_code_p = NE_EXPR;
4302 return true;
4306 return false;
4310 void dump_asserts_for (FILE *, tree);
4311 void debug_asserts_for (tree);
4312 void dump_all_asserts (FILE *);
4313 void debug_all_asserts (void);
4315 /* Dump all the registered assertions for NAME to FILE. */
4317 void
4318 dump_asserts_for (FILE *file, tree name)
4320 assert_locus_t loc;
4322 fprintf (file, "Assertions to be inserted for ");
4323 print_generic_expr (file, name, 0);
4324 fprintf (file, "\n");
4326 loc = asserts_for[SSA_NAME_VERSION (name)];
4327 while (loc)
4329 fprintf (file, "\t");
4330 print_gimple_stmt (file, gsi_stmt (loc->si), 0, 0);
4331 fprintf (file, "\n\tBB #%d", loc->bb->index);
4332 if (loc->e)
4334 fprintf (file, "\n\tEDGE %d->%d", loc->e->src->index,
4335 loc->e->dest->index);
4336 dump_edge_info (file, loc->e, dump_flags, 0);
4338 fprintf (file, "\n\tPREDICATE: ");
4339 print_generic_expr (file, name, 0);
4340 fprintf (file, " %s ", tree_code_name[(int)loc->comp_code]);
4341 print_generic_expr (file, loc->val, 0);
4342 fprintf (file, "\n\n");
4343 loc = loc->next;
4346 fprintf (file, "\n");
4350 /* Dump all the registered assertions for NAME to stderr. */
4352 DEBUG_FUNCTION void
4353 debug_asserts_for (tree name)
4355 dump_asserts_for (stderr, name);
4359 /* Dump all the registered assertions for all the names to FILE. */
4361 void
4362 dump_all_asserts (FILE *file)
4364 unsigned i;
4365 bitmap_iterator bi;
4367 fprintf (file, "\nASSERT_EXPRs to be inserted\n\n");
4368 EXECUTE_IF_SET_IN_BITMAP (need_assert_for, 0, i, bi)
4369 dump_asserts_for (file, ssa_name (i));
4370 fprintf (file, "\n");
4374 /* Dump all the registered assertions for all the names to stderr. */
4376 DEBUG_FUNCTION void
4377 debug_all_asserts (void)
4379 dump_all_asserts (stderr);
4383 /* If NAME doesn't have an ASSERT_EXPR registered for asserting
4384 'EXPR COMP_CODE VAL' at a location that dominates block BB or
4385 E->DEST, then register this location as a possible insertion point
4386 for ASSERT_EXPR <NAME, EXPR COMP_CODE VAL>.
4388 BB, E and SI provide the exact insertion point for the new
4389 ASSERT_EXPR. If BB is NULL, then the ASSERT_EXPR is to be inserted
4390 on edge E. Otherwise, if E is NULL, the ASSERT_EXPR is inserted on
4391 BB. If SI points to a COND_EXPR or a SWITCH_EXPR statement, then E
4392 must not be NULL. */
4394 static void
4395 register_new_assert_for (tree name, tree expr,
4396 enum tree_code comp_code,
4397 tree val,
4398 basic_block bb,
4399 edge e,
4400 gimple_stmt_iterator si)
4402 assert_locus_t n, loc, last_loc;
4403 basic_block dest_bb;
4405 gcc_checking_assert (bb == NULL || e == NULL);
4407 if (e == NULL)
4408 gcc_checking_assert (gimple_code (gsi_stmt (si)) != GIMPLE_COND
4409 && gimple_code (gsi_stmt (si)) != GIMPLE_SWITCH);
4411 /* Never build an assert comparing against an integer constant with
4412 TREE_OVERFLOW set. This confuses our undefined overflow warning
4413 machinery. */
4414 if (TREE_CODE (val) == INTEGER_CST
4415 && TREE_OVERFLOW (val))
4416 val = build_int_cst_wide (TREE_TYPE (val),
4417 TREE_INT_CST_LOW (val), TREE_INT_CST_HIGH (val));
4419 /* The new assertion A will be inserted at BB or E. We need to
4420 determine if the new location is dominated by a previously
4421 registered location for A. If we are doing an edge insertion,
4422 assume that A will be inserted at E->DEST. Note that this is not
4423 necessarily true.
4425 If E is a critical edge, it will be split. But even if E is
4426 split, the new block will dominate the same set of blocks that
4427 E->DEST dominates.
4429 The reverse, however, is not true, blocks dominated by E->DEST
4430 will not be dominated by the new block created to split E. So,
4431 if the insertion location is on a critical edge, we will not use
4432 the new location to move another assertion previously registered
4433 at a block dominated by E->DEST. */
4434 dest_bb = (bb) ? bb : e->dest;
4436 /* If NAME already has an ASSERT_EXPR registered for COMP_CODE and
4437 VAL at a block dominating DEST_BB, then we don't need to insert a new
4438 one. Similarly, if the same assertion already exists at a block
4439 dominated by DEST_BB and the new location is not on a critical
4440 edge, then update the existing location for the assertion (i.e.,
4441 move the assertion up in the dominance tree).
4443 Note, this is implemented as a simple linked list because there
4444 should not be more than a handful of assertions registered per
4445 name. If this becomes a performance problem, a table hashed by
4446 COMP_CODE and VAL could be implemented. */
4447 loc = asserts_for[SSA_NAME_VERSION (name)];
4448 last_loc = loc;
4449 while (loc)
4451 if (loc->comp_code == comp_code
4452 && (loc->val == val
4453 || operand_equal_p (loc->val, val, 0))
4454 && (loc->expr == expr
4455 || operand_equal_p (loc->expr, expr, 0)))
4457 /* If E is not a critical edge and DEST_BB
4458 dominates the existing location for the assertion, move
4459 the assertion up in the dominance tree by updating its
4460 location information. */
4461 if ((e == NULL || !EDGE_CRITICAL_P (e))
4462 && dominated_by_p (CDI_DOMINATORS, loc->bb, dest_bb))
4464 loc->bb = dest_bb;
4465 loc->e = e;
4466 loc->si = si;
4467 return;
4471 /* Update the last node of the list and move to the next one. */
4472 last_loc = loc;
4473 loc = loc->next;
4476 /* If we didn't find an assertion already registered for
4477 NAME COMP_CODE VAL, add a new one at the end of the list of
4478 assertions associated with NAME. */
4479 n = XNEW (struct assert_locus_d);
4480 n->bb = dest_bb;
4481 n->e = e;
4482 n->si = si;
4483 n->comp_code = comp_code;
4484 n->val = val;
4485 n->expr = expr;
4486 n->next = NULL;
4488 if (last_loc)
4489 last_loc->next = n;
4490 else
4491 asserts_for[SSA_NAME_VERSION (name)] = n;
4493 bitmap_set_bit (need_assert_for, SSA_NAME_VERSION (name));
4496 /* (COND_OP0 COND_CODE COND_OP1) is a predicate which uses NAME.
4497 Extract a suitable test code and value and store them into *CODE_P and
4498 *VAL_P so the predicate is normalized to NAME *CODE_P *VAL_P.
4500 If no extraction was possible, return FALSE, otherwise return TRUE.
4502 If INVERT is true, then we invert the result stored into *CODE_P. */
4504 static bool
4505 extract_code_and_val_from_cond_with_ops (tree name, enum tree_code cond_code,
4506 tree cond_op0, tree cond_op1,
4507 bool invert, enum tree_code *code_p,
4508 tree *val_p)
4510 enum tree_code comp_code;
4511 tree val;
4513 /* Otherwise, we have a comparison of the form NAME COMP VAL
4514 or VAL COMP NAME. */
4515 if (name == cond_op1)
4517 /* If the predicate is of the form VAL COMP NAME, flip
4518 COMP around because we need to register NAME as the
4519 first operand in the predicate. */
4520 comp_code = swap_tree_comparison (cond_code);
4521 val = cond_op0;
4523 else
4525 /* The comparison is of the form NAME COMP VAL, so the
4526 comparison code remains unchanged. */
4527 comp_code = cond_code;
4528 val = cond_op1;
4531 /* Invert the comparison code as necessary. */
4532 if (invert)
4533 comp_code = invert_tree_comparison (comp_code, 0);
4535 /* VRP does not handle float types. */
4536 if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (val)))
4537 return false;
4539 /* Do not register always-false predicates.
4540 FIXME: this works around a limitation in fold() when dealing with
4541 enumerations. Given 'enum { N1, N2 } x;', fold will not
4542 fold 'if (x > N2)' to 'if (0)'. */
4543 if ((comp_code == GT_EXPR || comp_code == LT_EXPR)
4544 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
4546 tree min = TYPE_MIN_VALUE (TREE_TYPE (val));
4547 tree max = TYPE_MAX_VALUE (TREE_TYPE (val));
4549 if (comp_code == GT_EXPR
4550 && (!max
4551 || compare_values (val, max) == 0))
4552 return false;
4554 if (comp_code == LT_EXPR
4555 && (!min
4556 || compare_values (val, min) == 0))
4557 return false;
4559 *code_p = comp_code;
4560 *val_p = val;
4561 return true;
4564 /* Find out smallest RES where RES > VAL && (RES & MASK) == RES, if any
4565 (otherwise return VAL). VAL and MASK must be zero-extended for
4566 precision PREC. If SGNBIT is non-zero, first xor VAL with SGNBIT
4567 (to transform signed values into unsigned) and at the end xor
4568 SGNBIT back. */
4570 static double_int
4571 masked_increment (double_int val, double_int mask, double_int sgnbit,
4572 unsigned int prec)
4574 double_int bit = double_int_one, res;
4575 unsigned int i;
4577 val ^= sgnbit;
4578 for (i = 0; i < prec; i++, bit += bit)
4580 res = mask;
4581 if ((res & bit).is_zero ())
4582 continue;
4583 res = bit - double_int_one;
4584 res = (val + bit).and_not (res);
4585 res &= mask;
4586 if (res.ugt (val))
4587 return res ^ sgnbit;
4589 return val ^ sgnbit;
4592 /* Try to register an edge assertion for SSA name NAME on edge E for
4593 the condition COND contributing to the conditional jump pointed to by BSI.
4594 Invert the condition COND if INVERT is true.
4595 Return true if an assertion for NAME could be registered. */
4597 static bool
4598 register_edge_assert_for_2 (tree name, edge e, gimple_stmt_iterator bsi,
4599 enum tree_code cond_code,
4600 tree cond_op0, tree cond_op1, bool invert)
4602 tree val;
4603 enum tree_code comp_code;
4604 bool retval = false;
4606 if (!extract_code_and_val_from_cond_with_ops (name, cond_code,
4607 cond_op0,
4608 cond_op1,
4609 invert, &comp_code, &val))
4610 return false;
4612 /* Only register an ASSERT_EXPR if NAME was found in the sub-graph
4613 reachable from E. */
4614 if (live_on_edge (e, name)
4615 && !has_single_use (name))
4617 register_new_assert_for (name, name, comp_code, val, NULL, e, bsi);
4618 retval = true;
4621 /* In the case of NAME <= CST and NAME being defined as
4622 NAME = (unsigned) NAME2 + CST2 we can assert NAME2 >= -CST2
4623 and NAME2 <= CST - CST2. We can do the same for NAME > CST.
4624 This catches range and anti-range tests. */
4625 if ((comp_code == LE_EXPR
4626 || comp_code == GT_EXPR)
4627 && TREE_CODE (val) == INTEGER_CST
4628 && TYPE_UNSIGNED (TREE_TYPE (val)))
4630 gimple def_stmt = SSA_NAME_DEF_STMT (name);
4631 tree cst2 = NULL_TREE, name2 = NULL_TREE, name3 = NULL_TREE;
4633 /* Extract CST2 from the (optional) addition. */
4634 if (is_gimple_assign (def_stmt)
4635 && gimple_assign_rhs_code (def_stmt) == PLUS_EXPR)
4637 name2 = gimple_assign_rhs1 (def_stmt);
4638 cst2 = gimple_assign_rhs2 (def_stmt);
4639 if (TREE_CODE (name2) == SSA_NAME
4640 && TREE_CODE (cst2) == INTEGER_CST)
4641 def_stmt = SSA_NAME_DEF_STMT (name2);
4644 /* Extract NAME2 from the (optional) sign-changing cast. */
4645 if (gimple_assign_cast_p (def_stmt))
4647 if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt))
4648 && ! TYPE_UNSIGNED (TREE_TYPE (gimple_assign_rhs1 (def_stmt)))
4649 && (TYPE_PRECISION (gimple_expr_type (def_stmt))
4650 == TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (def_stmt)))))
4651 name3 = gimple_assign_rhs1 (def_stmt);
4654 /* If name3 is used later, create an ASSERT_EXPR for it. */
4655 if (name3 != NULL_TREE
4656 && TREE_CODE (name3) == SSA_NAME
4657 && (cst2 == NULL_TREE
4658 || TREE_CODE (cst2) == INTEGER_CST)
4659 && INTEGRAL_TYPE_P (TREE_TYPE (name3))
4660 && live_on_edge (e, name3)
4661 && !has_single_use (name3))
4663 tree tmp;
4665 /* Build an expression for the range test. */
4666 tmp = build1 (NOP_EXPR, TREE_TYPE (name), name3);
4667 if (cst2 != NULL_TREE)
4668 tmp = build2 (PLUS_EXPR, TREE_TYPE (name), tmp, cst2);
4670 if (dump_file)
4672 fprintf (dump_file, "Adding assert for ");
4673 print_generic_expr (dump_file, name3, 0);
4674 fprintf (dump_file, " from ");
4675 print_generic_expr (dump_file, tmp, 0);
4676 fprintf (dump_file, "\n");
4679 register_new_assert_for (name3, tmp, comp_code, val, NULL, e, bsi);
4681 retval = true;
4684 /* If name2 is used later, create an ASSERT_EXPR for it. */
4685 if (name2 != NULL_TREE
4686 && TREE_CODE (name2) == SSA_NAME
4687 && TREE_CODE (cst2) == INTEGER_CST
4688 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
4689 && live_on_edge (e, name2)
4690 && !has_single_use (name2))
4692 tree tmp;
4694 /* Build an expression for the range test. */
4695 tmp = name2;
4696 if (TREE_TYPE (name) != TREE_TYPE (name2))
4697 tmp = build1 (NOP_EXPR, TREE_TYPE (name), tmp);
4698 if (cst2 != NULL_TREE)
4699 tmp = build2 (PLUS_EXPR, TREE_TYPE (name), tmp, cst2);
4701 if (dump_file)
4703 fprintf (dump_file, "Adding assert for ");
4704 print_generic_expr (dump_file, name2, 0);
4705 fprintf (dump_file, " from ");
4706 print_generic_expr (dump_file, tmp, 0);
4707 fprintf (dump_file, "\n");
4710 register_new_assert_for (name2, tmp, comp_code, val, NULL, e, bsi);
4712 retval = true;
4716 if (TREE_CODE_CLASS (comp_code) == tcc_comparison
4717 && TREE_CODE (val) == INTEGER_CST)
4719 gimple def_stmt = SSA_NAME_DEF_STMT (name);
4720 tree name2 = NULL_TREE, names[2], cst2 = NULL_TREE;
4721 tree val2 = NULL_TREE;
4722 double_int mask = double_int_zero;
4723 unsigned int prec = TYPE_PRECISION (TREE_TYPE (val));
4724 unsigned int nprec = prec;
4725 enum tree_code rhs_code = ERROR_MARK;
4727 if (is_gimple_assign (def_stmt))
4728 rhs_code = gimple_assign_rhs_code (def_stmt);
4730 /* Add asserts for NAME cmp CST and NAME being defined
4731 as NAME = (int) NAME2. */
4732 if (!TYPE_UNSIGNED (TREE_TYPE (val))
4733 && (comp_code == LE_EXPR || comp_code == LT_EXPR
4734 || comp_code == GT_EXPR || comp_code == GE_EXPR)
4735 && gimple_assign_cast_p (def_stmt))
4737 name2 = gimple_assign_rhs1 (def_stmt);
4738 if (CONVERT_EXPR_CODE_P (rhs_code)
4739 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
4740 && TYPE_UNSIGNED (TREE_TYPE (name2))
4741 && prec == TYPE_PRECISION (TREE_TYPE (name2))
4742 && (comp_code == LE_EXPR || comp_code == GT_EXPR
4743 || !tree_int_cst_equal (val,
4744 TYPE_MIN_VALUE (TREE_TYPE (val))))
4745 && live_on_edge (e, name2)
4746 && !has_single_use (name2))
4748 tree tmp, cst;
4749 enum tree_code new_comp_code = comp_code;
4751 cst = fold_convert (TREE_TYPE (name2),
4752 TYPE_MIN_VALUE (TREE_TYPE (val)));
4753 /* Build an expression for the range test. */
4754 tmp = build2 (PLUS_EXPR, TREE_TYPE (name2), name2, cst);
4755 cst = fold_build2 (PLUS_EXPR, TREE_TYPE (name2), cst,
4756 fold_convert (TREE_TYPE (name2), val));
4757 if (comp_code == LT_EXPR || comp_code == GE_EXPR)
4759 new_comp_code = comp_code == LT_EXPR ? LE_EXPR : GT_EXPR;
4760 cst = fold_build2 (MINUS_EXPR, TREE_TYPE (name2), cst,
4761 build_int_cst (TREE_TYPE (name2), 1));
4764 if (dump_file)
4766 fprintf (dump_file, "Adding assert for ");
4767 print_generic_expr (dump_file, name2, 0);
4768 fprintf (dump_file, " from ");
4769 print_generic_expr (dump_file, tmp, 0);
4770 fprintf (dump_file, "\n");
4773 register_new_assert_for (name2, tmp, new_comp_code, cst, NULL,
4774 e, bsi);
4776 retval = true;
4780 /* Add asserts for NAME cmp CST and NAME being defined as
4781 NAME = NAME2 >> CST2.
4783 Extract CST2 from the right shift. */
4784 if (rhs_code == RSHIFT_EXPR)
4786 name2 = gimple_assign_rhs1 (def_stmt);
4787 cst2 = gimple_assign_rhs2 (def_stmt);
4788 if (TREE_CODE (name2) == SSA_NAME
4789 && host_integerp (cst2, 1)
4790 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
4791 && IN_RANGE (tree_low_cst (cst2, 1), 1, prec - 1)
4792 && prec <= HOST_BITS_PER_DOUBLE_INT
4793 && prec == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (val)))
4794 && live_on_edge (e, name2)
4795 && !has_single_use (name2))
4797 mask = double_int::mask (tree_low_cst (cst2, 1));
4798 val2 = fold_binary (LSHIFT_EXPR, TREE_TYPE (val), val, cst2);
4801 if (val2 != NULL_TREE
4802 && TREE_CODE (val2) == INTEGER_CST
4803 && simple_cst_equal (fold_build2 (RSHIFT_EXPR,
4804 TREE_TYPE (val),
4805 val2, cst2), val))
4807 enum tree_code new_comp_code = comp_code;
4808 tree tmp, new_val;
4810 tmp = name2;
4811 if (comp_code == EQ_EXPR || comp_code == NE_EXPR)
4813 if (!TYPE_UNSIGNED (TREE_TYPE (val)))
4815 tree type = build_nonstandard_integer_type (prec, 1);
4816 tmp = build1 (NOP_EXPR, type, name2);
4817 val2 = fold_convert (type, val2);
4819 tmp = fold_build2 (MINUS_EXPR, TREE_TYPE (tmp), tmp, val2);
4820 new_val = double_int_to_tree (TREE_TYPE (tmp), mask);
4821 new_comp_code = comp_code == EQ_EXPR ? LE_EXPR : GT_EXPR;
4823 else if (comp_code == LT_EXPR || comp_code == GE_EXPR)
4824 new_val = val2;
4825 else
4827 double_int maxval
4828 = double_int::max_value (prec, TYPE_UNSIGNED (TREE_TYPE (val)));
4829 mask |= tree_to_double_int (val2);
4830 if (mask == maxval)
4831 new_val = NULL_TREE;
4832 else
4833 new_val = double_int_to_tree (TREE_TYPE (val2), mask);
4836 if (new_val)
4838 if (dump_file)
4840 fprintf (dump_file, "Adding assert for ");
4841 print_generic_expr (dump_file, name2, 0);
4842 fprintf (dump_file, " from ");
4843 print_generic_expr (dump_file, tmp, 0);
4844 fprintf (dump_file, "\n");
4847 register_new_assert_for (name2, tmp, new_comp_code, new_val,
4848 NULL, e, bsi);
4849 retval = true;
4853 /* Add asserts for NAME cmp CST and NAME being defined as
4854 NAME = NAME2 & CST2.
4856 Extract CST2 from the and.
4858 Also handle
4859 NAME = (unsigned) NAME2;
4860 casts where NAME's type is unsigned and has smaller precision
4861 than NAME2's type as if it was NAME = NAME2 & MASK. */
4862 names[0] = NULL_TREE;
4863 names[1] = NULL_TREE;
4864 cst2 = NULL_TREE;
4865 if (rhs_code == BIT_AND_EXPR
4866 || (CONVERT_EXPR_CODE_P (rhs_code)
4867 && TREE_CODE (TREE_TYPE (val)) == INTEGER_TYPE
4868 && TYPE_UNSIGNED (TREE_TYPE (val))
4869 && TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (def_stmt)))
4870 > prec
4871 && !retval))
4873 name2 = gimple_assign_rhs1 (def_stmt);
4874 if (rhs_code == BIT_AND_EXPR)
4875 cst2 = gimple_assign_rhs2 (def_stmt);
4876 else
4878 cst2 = TYPE_MAX_VALUE (TREE_TYPE (val));
4879 nprec = TYPE_PRECISION (TREE_TYPE (name2));
4881 if (TREE_CODE (name2) == SSA_NAME
4882 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
4883 && TREE_CODE (cst2) == INTEGER_CST
4884 && !integer_zerop (cst2)
4885 && nprec <= HOST_BITS_PER_DOUBLE_INT
4886 && (nprec > 1
4887 || TYPE_UNSIGNED (TREE_TYPE (val))))
4889 gimple def_stmt2 = SSA_NAME_DEF_STMT (name2);
4890 if (gimple_assign_cast_p (def_stmt2))
4892 names[1] = gimple_assign_rhs1 (def_stmt2);
4893 if (!CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt2))
4894 || !INTEGRAL_TYPE_P (TREE_TYPE (names[1]))
4895 || (TYPE_PRECISION (TREE_TYPE (name2))
4896 != TYPE_PRECISION (TREE_TYPE (names[1])))
4897 || !live_on_edge (e, names[1])
4898 || has_single_use (names[1]))
4899 names[1] = NULL_TREE;
4901 if (live_on_edge (e, name2)
4902 && !has_single_use (name2))
4903 names[0] = name2;
4906 if (names[0] || names[1])
4908 double_int minv, maxv = double_int_zero, valv, cst2v;
4909 double_int tem, sgnbit;
4910 bool valid_p = false, valn = false, cst2n = false;
4911 enum tree_code ccode = comp_code;
4913 valv = tree_to_double_int (val).zext (nprec);
4914 cst2v = tree_to_double_int (cst2).zext (nprec);
4915 if (!TYPE_UNSIGNED (TREE_TYPE (val)))
4917 valn = valv.sext (nprec).is_negative ();
4918 cst2n = cst2v.sext (nprec).is_negative ();
4920 /* If CST2 doesn't have most significant bit set,
4921 but VAL is negative, we have comparison like
4922 if ((x & 0x123) > -4) (always true). Just give up. */
4923 if (!cst2n && valn)
4924 ccode = ERROR_MARK;
4925 if (cst2n)
4926 sgnbit = double_int_one.llshift (nprec - 1, nprec).zext (nprec);
4927 else
4928 sgnbit = double_int_zero;
4929 minv = valv & cst2v;
4930 switch (ccode)
4932 case EQ_EXPR:
4933 /* Minimum unsigned value for equality is VAL & CST2
4934 (should be equal to VAL, otherwise we probably should
4935 have folded the comparison into false) and
4936 maximum unsigned value is VAL | ~CST2. */
4937 maxv = valv | ~cst2v;
4938 maxv = maxv.zext (nprec);
4939 valid_p = true;
4940 break;
4941 case NE_EXPR:
4942 tem = valv | ~cst2v;
4943 tem = tem.zext (nprec);
4944 /* If VAL is 0, handle (X & CST2) != 0 as (X & CST2) > 0U. */
4945 if (valv.is_zero ())
4947 cst2n = false;
4948 sgnbit = double_int_zero;
4949 goto gt_expr;
4951 /* If (VAL | ~CST2) is all ones, handle it as
4952 (X & CST2) < VAL. */
4953 if (tem == double_int::mask (nprec))
4955 cst2n = false;
4956 valn = false;
4957 sgnbit = double_int_zero;
4958 goto lt_expr;
4960 if (!cst2n
4961 && cst2v.sext (nprec).is_negative ())
4962 sgnbit
4963 = double_int_one.llshift (nprec - 1, nprec).zext (nprec);
4964 if (!sgnbit.is_zero ())
4966 if (valv == sgnbit)
4968 cst2n = true;
4969 valn = true;
4970 goto gt_expr;
4972 if (tem == double_int::mask (nprec - 1))
4974 cst2n = true;
4975 goto lt_expr;
4977 if (!cst2n)
4978 sgnbit = double_int_zero;
4980 break;
4981 case GE_EXPR:
4982 /* Minimum unsigned value for >= if (VAL & CST2) == VAL
4983 is VAL and maximum unsigned value is ~0. For signed
4984 comparison, if CST2 doesn't have most significant bit
4985 set, handle it similarly. If CST2 has MSB set,
4986 the minimum is the same, and maximum is ~0U/2. */
4987 if (minv != valv)
4989 /* If (VAL & CST2) != VAL, X & CST2 can't be equal to
4990 VAL. */
4991 minv = masked_increment (valv, cst2v, sgnbit, nprec);
4992 if (minv == valv)
4993 break;
4995 maxv = double_int::mask (nprec - (cst2n ? 1 : 0));
4996 valid_p = true;
4997 break;
4998 case GT_EXPR:
4999 gt_expr:
5000 /* Find out smallest MINV where MINV > VAL
5001 && (MINV & CST2) == MINV, if any. If VAL is signed and
5002 CST2 has MSB set, compute it biased by 1 << (nprec - 1). */
5003 minv = masked_increment (valv, cst2v, sgnbit, nprec);
5004 if (minv == valv)
5005 break;
5006 maxv = double_int::mask (nprec - (cst2n ? 1 : 0));
5007 valid_p = true;
5008 break;
5009 case LE_EXPR:
5010 /* Minimum unsigned value for <= is 0 and maximum
5011 unsigned value is VAL | ~CST2 if (VAL & CST2) == VAL.
5012 Otherwise, find smallest VAL2 where VAL2 > VAL
5013 && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
5014 as maximum.
5015 For signed comparison, if CST2 doesn't have most
5016 significant bit set, handle it similarly. If CST2 has
5017 MSB set, the maximum is the same and minimum is INT_MIN. */
5018 if (minv == valv)
5019 maxv = valv;
5020 else
5022 maxv = masked_increment (valv, cst2v, sgnbit, nprec);
5023 if (maxv == valv)
5024 break;
5025 maxv -= double_int_one;
5027 maxv |= ~cst2v;
5028 maxv = maxv.zext (nprec);
5029 minv = sgnbit;
5030 valid_p = true;
5031 break;
5032 case LT_EXPR:
5033 lt_expr:
5034 /* Minimum unsigned value for < is 0 and maximum
5035 unsigned value is (VAL-1) | ~CST2 if (VAL & CST2) == VAL.
5036 Otherwise, find smallest VAL2 where VAL2 > VAL
5037 && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
5038 as maximum.
5039 For signed comparison, if CST2 doesn't have most
5040 significant bit set, handle it similarly. If CST2 has
5041 MSB set, the maximum is the same and minimum is INT_MIN. */
5042 if (minv == valv)
5044 if (valv == sgnbit)
5045 break;
5046 maxv = valv;
5048 else
5050 maxv = masked_increment (valv, cst2v, sgnbit, nprec);
5051 if (maxv == valv)
5052 break;
5054 maxv -= double_int_one;
5055 maxv |= ~cst2v;
5056 maxv = maxv.zext (nprec);
5057 minv = sgnbit;
5058 valid_p = true;
5059 break;
5060 default:
5061 break;
5063 if (valid_p
5064 && (maxv - minv).zext (nprec) != double_int::mask (nprec))
5066 tree tmp, new_val, type;
5067 int i;
5069 for (i = 0; i < 2; i++)
5070 if (names[i])
5072 double_int maxv2 = maxv;
5073 tmp = names[i];
5074 type = TREE_TYPE (names[i]);
5075 if (!TYPE_UNSIGNED (type))
5077 type = build_nonstandard_integer_type (nprec, 1);
5078 tmp = build1 (NOP_EXPR, type, names[i]);
5080 if (!minv.is_zero ())
5082 tmp = build2 (PLUS_EXPR, type, tmp,
5083 double_int_to_tree (type, -minv));
5084 maxv2 = maxv - minv;
5086 new_val = double_int_to_tree (type, maxv2);
5088 if (dump_file)
5090 fprintf (dump_file, "Adding assert for ");
5091 print_generic_expr (dump_file, names[i], 0);
5092 fprintf (dump_file, " from ");
5093 print_generic_expr (dump_file, tmp, 0);
5094 fprintf (dump_file, "\n");
5097 register_new_assert_for (names[i], tmp, LE_EXPR,
5098 new_val, NULL, e, bsi);
5099 retval = true;
5105 return retval;
5108 /* OP is an operand of a truth value expression which is known to have
5109 a particular value. Register any asserts for OP and for any
5110 operands in OP's defining statement.
5112 If CODE is EQ_EXPR, then we want to register OP is zero (false),
5113 if CODE is NE_EXPR, then we want to register OP is nonzero (true). */
5115 static bool
5116 register_edge_assert_for_1 (tree op, enum tree_code code,
5117 edge e, gimple_stmt_iterator bsi)
5119 bool retval = false;
5120 gimple op_def;
5121 tree val;
5122 enum tree_code rhs_code;
5124 /* We only care about SSA_NAMEs. */
5125 if (TREE_CODE (op) != SSA_NAME)
5126 return false;
5128 /* We know that OP will have a zero or nonzero value. If OP is used
5129 more than once go ahead and register an assert for OP.
5131 The FOUND_IN_SUBGRAPH support is not helpful in this situation as
5132 it will always be set for OP (because OP is used in a COND_EXPR in
5133 the subgraph). */
5134 if (!has_single_use (op))
5136 val = build_int_cst (TREE_TYPE (op), 0);
5137 register_new_assert_for (op, op, code, val, NULL, e, bsi);
5138 retval = true;
5141 /* Now look at how OP is set. If it's set from a comparison,
5142 a truth operation or some bit operations, then we may be able
5143 to register information about the operands of that assignment. */
5144 op_def = SSA_NAME_DEF_STMT (op);
5145 if (gimple_code (op_def) != GIMPLE_ASSIGN)
5146 return retval;
5148 rhs_code = gimple_assign_rhs_code (op_def);
5150 if (TREE_CODE_CLASS (rhs_code) == tcc_comparison)
5152 bool invert = (code == EQ_EXPR ? true : false);
5153 tree op0 = gimple_assign_rhs1 (op_def);
5154 tree op1 = gimple_assign_rhs2 (op_def);
5156 if (TREE_CODE (op0) == SSA_NAME)
5157 retval |= register_edge_assert_for_2 (op0, e, bsi, rhs_code, op0, op1,
5158 invert);
5159 if (TREE_CODE (op1) == SSA_NAME)
5160 retval |= register_edge_assert_for_2 (op1, e, bsi, rhs_code, op0, op1,
5161 invert);
5163 else if ((code == NE_EXPR
5164 && gimple_assign_rhs_code (op_def) == BIT_AND_EXPR)
5165 || (code == EQ_EXPR
5166 && gimple_assign_rhs_code (op_def) == BIT_IOR_EXPR))
5168 /* Recurse on each operand. */
5169 retval |= register_edge_assert_for_1 (gimple_assign_rhs1 (op_def),
5170 code, e, bsi);
5171 retval |= register_edge_assert_for_1 (gimple_assign_rhs2 (op_def),
5172 code, e, bsi);
5174 else if (gimple_assign_rhs_code (op_def) == BIT_NOT_EXPR
5175 && TYPE_PRECISION (TREE_TYPE (gimple_assign_lhs (op_def))) == 1)
5177 /* Recurse, flipping CODE. */
5178 code = invert_tree_comparison (code, false);
5179 retval |= register_edge_assert_for_1 (gimple_assign_rhs1 (op_def),
5180 code, e, bsi);
5182 else if (gimple_assign_rhs_code (op_def) == SSA_NAME)
5184 /* Recurse through the copy. */
5185 retval |= register_edge_assert_for_1 (gimple_assign_rhs1 (op_def),
5186 code, e, bsi);
5188 else if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (op_def)))
5190 /* Recurse through the type conversion. */
5191 retval |= register_edge_assert_for_1 (gimple_assign_rhs1 (op_def),
5192 code, e, bsi);
5195 return retval;
5198 /* Try to register an edge assertion for SSA name NAME on edge E for
5199 the condition COND contributing to the conditional jump pointed to by SI.
5200 Return true if an assertion for NAME could be registered. */
5202 static bool
5203 register_edge_assert_for (tree name, edge e, gimple_stmt_iterator si,
5204 enum tree_code cond_code, tree cond_op0,
5205 tree cond_op1)
5207 tree val;
5208 enum tree_code comp_code;
5209 bool retval = false;
5210 bool is_else_edge = (e->flags & EDGE_FALSE_VALUE) != 0;
5212 /* Do not attempt to infer anything in names that flow through
5213 abnormal edges. */
5214 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name))
5215 return false;
5217 if (!extract_code_and_val_from_cond_with_ops (name, cond_code,
5218 cond_op0, cond_op1,
5219 is_else_edge,
5220 &comp_code, &val))
5221 return false;
5223 /* Register ASSERT_EXPRs for name. */
5224 retval |= register_edge_assert_for_2 (name, e, si, cond_code, cond_op0,
5225 cond_op1, is_else_edge);
5228 /* If COND is effectively an equality test of an SSA_NAME against
5229 the value zero or one, then we may be able to assert values
5230 for SSA_NAMEs which flow into COND. */
5232 /* In the case of NAME == 1 or NAME != 0, for BIT_AND_EXPR defining
5233 statement of NAME we can assert both operands of the BIT_AND_EXPR
5234 have nonzero value. */
5235 if (((comp_code == EQ_EXPR && integer_onep (val))
5236 || (comp_code == NE_EXPR && integer_zerop (val))))
5238 gimple def_stmt = SSA_NAME_DEF_STMT (name);
5240 if (is_gimple_assign (def_stmt)
5241 && gimple_assign_rhs_code (def_stmt) == BIT_AND_EXPR)
5243 tree op0 = gimple_assign_rhs1 (def_stmt);
5244 tree op1 = gimple_assign_rhs2 (def_stmt);
5245 retval |= register_edge_assert_for_1 (op0, NE_EXPR, e, si);
5246 retval |= register_edge_assert_for_1 (op1, NE_EXPR, e, si);
5250 /* In the case of NAME == 0 or NAME != 1, for BIT_IOR_EXPR defining
5251 statement of NAME we can assert both operands of the BIT_IOR_EXPR
5252 have zero value. */
5253 if (((comp_code == EQ_EXPR && integer_zerop (val))
5254 || (comp_code == NE_EXPR && integer_onep (val))))
5256 gimple def_stmt = SSA_NAME_DEF_STMT (name);
5258 /* For BIT_IOR_EXPR only if NAME == 0 both operands have
5259 necessarily zero value, or if type-precision is one. */
5260 if (is_gimple_assign (def_stmt)
5261 && (gimple_assign_rhs_code (def_stmt) == BIT_IOR_EXPR
5262 && (TYPE_PRECISION (TREE_TYPE (name)) == 1
5263 || comp_code == EQ_EXPR)))
5265 tree op0 = gimple_assign_rhs1 (def_stmt);
5266 tree op1 = gimple_assign_rhs2 (def_stmt);
5267 retval |= register_edge_assert_for_1 (op0, EQ_EXPR, e, si);
5268 retval |= register_edge_assert_for_1 (op1, EQ_EXPR, e, si);
5272 return retval;
5276 /* Determine whether the outgoing edges of BB should receive an
5277 ASSERT_EXPR for each of the operands of BB's LAST statement.
5278 The last statement of BB must be a COND_EXPR.
5280 If any of the sub-graphs rooted at BB have an interesting use of
5281 the predicate operands, an assert location node is added to the
5282 list of assertions for the corresponding operands. */
5284 static bool
5285 find_conditional_asserts (basic_block bb, gimple last)
5287 bool need_assert;
5288 gimple_stmt_iterator bsi;
5289 tree op;
5290 edge_iterator ei;
5291 edge e;
5292 ssa_op_iter iter;
5294 need_assert = false;
5295 bsi = gsi_for_stmt (last);
5297 /* Look for uses of the operands in each of the sub-graphs
5298 rooted at BB. We need to check each of the outgoing edges
5299 separately, so that we know what kind of ASSERT_EXPR to
5300 insert. */
5301 FOR_EACH_EDGE (e, ei, bb->succs)
5303 if (e->dest == bb)
5304 continue;
5306 /* Register the necessary assertions for each operand in the
5307 conditional predicate. */
5308 FOR_EACH_SSA_TREE_OPERAND (op, last, iter, SSA_OP_USE)
5310 need_assert |= register_edge_assert_for (op, e, bsi,
5311 gimple_cond_code (last),
5312 gimple_cond_lhs (last),
5313 gimple_cond_rhs (last));
5317 return need_assert;
5320 struct case_info
5322 tree expr;
5323 basic_block bb;
5326 /* Compare two case labels sorting first by the destination bb index
5327 and then by the case value. */
5329 static int
5330 compare_case_labels (const void *p1, const void *p2)
5332 const struct case_info *ci1 = (const struct case_info *) p1;
5333 const struct case_info *ci2 = (const struct case_info *) p2;
5334 int idx1 = ci1->bb->index;
5335 int idx2 = ci2->bb->index;
5337 if (idx1 < idx2)
5338 return -1;
5339 else if (idx1 == idx2)
5341 /* Make sure the default label is first in a group. */
5342 if (!CASE_LOW (ci1->expr))
5343 return -1;
5344 else if (!CASE_LOW (ci2->expr))
5345 return 1;
5346 else
5347 return tree_int_cst_compare (CASE_LOW (ci1->expr),
5348 CASE_LOW (ci2->expr));
5350 else
5351 return 1;
5354 /* Determine whether the outgoing edges of BB should receive an
5355 ASSERT_EXPR for each of the operands of BB's LAST statement.
5356 The last statement of BB must be a SWITCH_EXPR.
5358 If any of the sub-graphs rooted at BB have an interesting use of
5359 the predicate operands, an assert location node is added to the
5360 list of assertions for the corresponding operands. */
5362 static bool
5363 find_switch_asserts (basic_block bb, gimple last)
5365 bool need_assert;
5366 gimple_stmt_iterator bsi;
5367 tree op;
5368 edge e;
5369 struct case_info *ci;
5370 size_t n = gimple_switch_num_labels (last);
5371 #if GCC_VERSION >= 4000
5372 unsigned int idx;
5373 #else
5374 /* Work around GCC 3.4 bug (PR 37086). */
5375 volatile unsigned int idx;
5376 #endif
5378 need_assert = false;
5379 bsi = gsi_for_stmt (last);
5380 op = gimple_switch_index (last);
5381 if (TREE_CODE (op) != SSA_NAME)
5382 return false;
5384 /* Build a vector of case labels sorted by destination label. */
5385 ci = XNEWVEC (struct case_info, n);
5386 for (idx = 0; idx < n; ++idx)
5388 ci[idx].expr = gimple_switch_label (last, idx);
5389 ci[idx].bb = label_to_block (CASE_LABEL (ci[idx].expr));
5391 qsort (ci, n, sizeof (struct case_info), compare_case_labels);
5393 for (idx = 0; idx < n; ++idx)
5395 tree min, max;
5396 tree cl = ci[idx].expr;
5397 basic_block cbb = ci[idx].bb;
5399 min = CASE_LOW (cl);
5400 max = CASE_HIGH (cl);
5402 /* If there are multiple case labels with the same destination
5403 we need to combine them to a single value range for the edge. */
5404 if (idx + 1 < n && cbb == ci[idx + 1].bb)
5406 /* Skip labels until the last of the group. */
5407 do {
5408 ++idx;
5409 } while (idx < n && cbb == ci[idx].bb);
5410 --idx;
5412 /* Pick up the maximum of the case label range. */
5413 if (CASE_HIGH (ci[idx].expr))
5414 max = CASE_HIGH (ci[idx].expr);
5415 else
5416 max = CASE_LOW (ci[idx].expr);
5419 /* Nothing to do if the range includes the default label until we
5420 can register anti-ranges. */
5421 if (min == NULL_TREE)
5422 continue;
5424 /* Find the edge to register the assert expr on. */
5425 e = find_edge (bb, cbb);
5427 /* Register the necessary assertions for the operand in the
5428 SWITCH_EXPR. */
5429 need_assert |= register_edge_assert_for (op, e, bsi,
5430 max ? GE_EXPR : EQ_EXPR,
5432 fold_convert (TREE_TYPE (op),
5433 min));
5434 if (max)
5436 need_assert |= register_edge_assert_for (op, e, bsi, LE_EXPR,
5438 fold_convert (TREE_TYPE (op),
5439 max));
5443 XDELETEVEC (ci);
5444 return need_assert;
5448 /* Traverse all the statements in block BB looking for statements that
5449 may generate useful assertions for the SSA names in their operand.
5450 If a statement produces a useful assertion A for name N_i, then the
5451 list of assertions already generated for N_i is scanned to
5452 determine if A is actually needed.
5454 If N_i already had the assertion A at a location dominating the
5455 current location, then nothing needs to be done. Otherwise, the
5456 new location for A is recorded instead.
5458 1- For every statement S in BB, all the variables used by S are
5459 added to bitmap FOUND_IN_SUBGRAPH.
5461 2- If statement S uses an operand N in a way that exposes a known
5462 value range for N, then if N was not already generated by an
5463 ASSERT_EXPR, create a new assert location for N. For instance,
5464 if N is a pointer and the statement dereferences it, we can
5465 assume that N is not NULL.
5467 3- COND_EXPRs are a special case of #2. We can derive range
5468 information from the predicate but need to insert different
5469 ASSERT_EXPRs for each of the sub-graphs rooted at the
5470 conditional block. If the last statement of BB is a conditional
5471 expression of the form 'X op Y', then
5473 a) Remove X and Y from the set FOUND_IN_SUBGRAPH.
5475 b) If the conditional is the only entry point to the sub-graph
5476 corresponding to the THEN_CLAUSE, recurse into it. On
5477 return, if X and/or Y are marked in FOUND_IN_SUBGRAPH, then
5478 an ASSERT_EXPR is added for the corresponding variable.
5480 c) Repeat step (b) on the ELSE_CLAUSE.
5482 d) Mark X and Y in FOUND_IN_SUBGRAPH.
5484 For instance,
5486 if (a == 9)
5487 b = a;
5488 else
5489 b = c + 1;
5491 In this case, an assertion on the THEN clause is useful to
5492 determine that 'a' is always 9 on that edge. However, an assertion
5493 on the ELSE clause would be unnecessary.
5495 4- If BB does not end in a conditional expression, then we recurse
5496 into BB's dominator children.
5498 At the end of the recursive traversal, every SSA name will have a
5499 list of locations where ASSERT_EXPRs should be added. When a new
5500 location for name N is found, it is registered by calling
5501 register_new_assert_for. That function keeps track of all the
5502 registered assertions to prevent adding unnecessary assertions.
5503 For instance, if a pointer P_4 is dereferenced more than once in a
5504 dominator tree, only the location dominating all the dereference of
5505 P_4 will receive an ASSERT_EXPR.
5507 If this function returns true, then it means that there are names
5508 for which we need to generate ASSERT_EXPRs. Those assertions are
5509 inserted by process_assert_insertions. */
5511 static bool
5512 find_assert_locations_1 (basic_block bb, sbitmap live)
5514 gimple_stmt_iterator si;
5515 gimple last;
5516 bool need_assert;
5518 need_assert = false;
5519 last = last_stmt (bb);
5521 /* If BB's last statement is a conditional statement involving integer
5522 operands, determine if we need to add ASSERT_EXPRs. */
5523 if (last
5524 && gimple_code (last) == GIMPLE_COND
5525 && !fp_predicate (last)
5526 && !ZERO_SSA_OPERANDS (last, SSA_OP_USE))
5527 need_assert |= find_conditional_asserts (bb, last);
5529 /* If BB's last statement is a switch statement involving integer
5530 operands, determine if we need to add ASSERT_EXPRs. */
5531 if (last
5532 && gimple_code (last) == GIMPLE_SWITCH
5533 && !ZERO_SSA_OPERANDS (last, SSA_OP_USE))
5534 need_assert |= find_switch_asserts (bb, last);
5536 /* Traverse all the statements in BB marking used names and looking
5537 for statements that may infer assertions for their used operands. */
5538 for (si = gsi_last_bb (bb); !gsi_end_p (si); gsi_prev (&si))
5540 gimple stmt;
5541 tree op;
5542 ssa_op_iter i;
5544 stmt = gsi_stmt (si);
5546 if (is_gimple_debug (stmt))
5547 continue;
5549 /* See if we can derive an assertion for any of STMT's operands. */
5550 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_USE)
5552 tree value;
5553 enum tree_code comp_code;
5555 /* If op is not live beyond this stmt, do not bother to insert
5556 asserts for it. */
5557 if (!bitmap_bit_p (live, SSA_NAME_VERSION (op)))
5558 continue;
5560 /* If OP is used in such a way that we can infer a value
5561 range for it, and we don't find a previous assertion for
5562 it, create a new assertion location node for OP. */
5563 if (infer_value_range (stmt, op, &comp_code, &value))
5565 /* If we are able to infer a nonzero value range for OP,
5566 then walk backwards through the use-def chain to see if OP
5567 was set via a typecast.
5569 If so, then we can also infer a nonzero value range
5570 for the operand of the NOP_EXPR. */
5571 if (comp_code == NE_EXPR && integer_zerop (value))
5573 tree t = op;
5574 gimple def_stmt = SSA_NAME_DEF_STMT (t);
5576 while (is_gimple_assign (def_stmt)
5577 && gimple_assign_rhs_code (def_stmt) == NOP_EXPR
5578 && TREE_CODE
5579 (gimple_assign_rhs1 (def_stmt)) == SSA_NAME
5580 && POINTER_TYPE_P
5581 (TREE_TYPE (gimple_assign_rhs1 (def_stmt))))
5583 t = gimple_assign_rhs1 (def_stmt);
5584 def_stmt = SSA_NAME_DEF_STMT (t);
5586 /* Note we want to register the assert for the
5587 operand of the NOP_EXPR after SI, not after the
5588 conversion. */
5589 if (! has_single_use (t))
5591 register_new_assert_for (t, t, comp_code, value,
5592 bb, NULL, si);
5593 need_assert = true;
5598 register_new_assert_for (op, op, comp_code, value, bb, NULL, si);
5599 need_assert = true;
5603 /* Update live. */
5604 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_USE)
5605 bitmap_set_bit (live, SSA_NAME_VERSION (op));
5606 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_DEF)
5607 bitmap_clear_bit (live, SSA_NAME_VERSION (op));
5610 /* Traverse all PHI nodes in BB, updating live. */
5611 for (si = gsi_start_phis (bb); !gsi_end_p(si); gsi_next (&si))
5613 use_operand_p arg_p;
5614 ssa_op_iter i;
5615 gimple phi = gsi_stmt (si);
5616 tree res = gimple_phi_result (phi);
5618 if (virtual_operand_p (res))
5619 continue;
5621 FOR_EACH_PHI_ARG (arg_p, phi, i, SSA_OP_USE)
5623 tree arg = USE_FROM_PTR (arg_p);
5624 if (TREE_CODE (arg) == SSA_NAME)
5625 bitmap_set_bit (live, SSA_NAME_VERSION (arg));
5628 bitmap_clear_bit (live, SSA_NAME_VERSION (res));
5631 return need_assert;
5634 /* Do an RPO walk over the function computing SSA name liveness
5635 on-the-fly and deciding on assert expressions to insert.
5636 Returns true if there are assert expressions to be inserted. */
5638 static bool
5639 find_assert_locations (void)
5641 int *rpo = XNEWVEC (int, last_basic_block);
5642 int *bb_rpo = XNEWVEC (int, last_basic_block);
5643 int *last_rpo = XCNEWVEC (int, last_basic_block);
5644 int rpo_cnt, i;
5645 bool need_asserts;
5647 live = XCNEWVEC (sbitmap, last_basic_block);
5648 rpo_cnt = pre_and_rev_post_order_compute (NULL, rpo, false);
5649 for (i = 0; i < rpo_cnt; ++i)
5650 bb_rpo[rpo[i]] = i;
5652 need_asserts = false;
5653 for (i = rpo_cnt - 1; i >= 0; --i)
5655 basic_block bb = BASIC_BLOCK (rpo[i]);
5656 edge e;
5657 edge_iterator ei;
5659 if (!live[rpo[i]])
5661 live[rpo[i]] = sbitmap_alloc (num_ssa_names);
5662 bitmap_clear (live[rpo[i]]);
5665 /* Process BB and update the live information with uses in
5666 this block. */
5667 need_asserts |= find_assert_locations_1 (bb, live[rpo[i]]);
5669 /* Merge liveness into the predecessor blocks and free it. */
5670 if (!bitmap_empty_p (live[rpo[i]]))
5672 int pred_rpo = i;
5673 FOR_EACH_EDGE (e, ei, bb->preds)
5675 int pred = e->src->index;
5676 if ((e->flags & EDGE_DFS_BACK) || pred == ENTRY_BLOCK)
5677 continue;
5679 if (!live[pred])
5681 live[pred] = sbitmap_alloc (num_ssa_names);
5682 bitmap_clear (live[pred]);
5684 bitmap_ior (live[pred], live[pred], live[rpo[i]]);
5686 if (bb_rpo[pred] < pred_rpo)
5687 pred_rpo = bb_rpo[pred];
5690 /* Record the RPO number of the last visited block that needs
5691 live information from this block. */
5692 last_rpo[rpo[i]] = pred_rpo;
5694 else
5696 sbitmap_free (live[rpo[i]]);
5697 live[rpo[i]] = NULL;
5700 /* We can free all successors live bitmaps if all their
5701 predecessors have been visited already. */
5702 FOR_EACH_EDGE (e, ei, bb->succs)
5703 if (last_rpo[e->dest->index] == i
5704 && live[e->dest->index])
5706 sbitmap_free (live[e->dest->index]);
5707 live[e->dest->index] = NULL;
5711 XDELETEVEC (rpo);
5712 XDELETEVEC (bb_rpo);
5713 XDELETEVEC (last_rpo);
5714 for (i = 0; i < last_basic_block; ++i)
5715 if (live[i])
5716 sbitmap_free (live[i]);
5717 XDELETEVEC (live);
5719 return need_asserts;
5722 /* Create an ASSERT_EXPR for NAME and insert it in the location
5723 indicated by LOC. Return true if we made any edge insertions. */
5725 static bool
5726 process_assert_insertions_for (tree name, assert_locus_t loc)
5728 /* Build the comparison expression NAME_i COMP_CODE VAL. */
5729 gimple stmt;
5730 tree cond;
5731 gimple assert_stmt;
5732 edge_iterator ei;
5733 edge e;
5735 /* If we have X <=> X do not insert an assert expr for that. */
5736 if (loc->expr == loc->val)
5737 return false;
5739 cond = build2 (loc->comp_code, boolean_type_node, loc->expr, loc->val);
5740 assert_stmt = build_assert_expr_for (cond, name);
5741 if (loc->e)
5743 /* We have been asked to insert the assertion on an edge. This
5744 is used only by COND_EXPR and SWITCH_EXPR assertions. */
5745 gcc_checking_assert (gimple_code (gsi_stmt (loc->si)) == GIMPLE_COND
5746 || (gimple_code (gsi_stmt (loc->si))
5747 == GIMPLE_SWITCH));
5749 gsi_insert_on_edge (loc->e, assert_stmt);
5750 return true;
5753 /* Otherwise, we can insert right after LOC->SI iff the
5754 statement must not be the last statement in the block. */
5755 stmt = gsi_stmt (loc->si);
5756 if (!stmt_ends_bb_p (stmt))
5758 gsi_insert_after (&loc->si, assert_stmt, GSI_SAME_STMT);
5759 return false;
5762 /* If STMT must be the last statement in BB, we can only insert new
5763 assertions on the non-abnormal edge out of BB. Note that since
5764 STMT is not control flow, there may only be one non-abnormal edge
5765 out of BB. */
5766 FOR_EACH_EDGE (e, ei, loc->bb->succs)
5767 if (!(e->flags & EDGE_ABNORMAL))
5769 gsi_insert_on_edge (e, assert_stmt);
5770 return true;
5773 gcc_unreachable ();
5777 /* Process all the insertions registered for every name N_i registered
5778 in NEED_ASSERT_FOR. The list of assertions to be inserted are
5779 found in ASSERTS_FOR[i]. */
5781 static void
5782 process_assert_insertions (void)
5784 unsigned i;
5785 bitmap_iterator bi;
5786 bool update_edges_p = false;
5787 int num_asserts = 0;
5789 if (dump_file && (dump_flags & TDF_DETAILS))
5790 dump_all_asserts (dump_file);
5792 EXECUTE_IF_SET_IN_BITMAP (need_assert_for, 0, i, bi)
5794 assert_locus_t loc = asserts_for[i];
5795 gcc_assert (loc);
5797 while (loc)
5799 assert_locus_t next = loc->next;
5800 update_edges_p |= process_assert_insertions_for (ssa_name (i), loc);
5801 free (loc);
5802 loc = next;
5803 num_asserts++;
5807 if (update_edges_p)
5808 gsi_commit_edge_inserts ();
5810 statistics_counter_event (cfun, "Number of ASSERT_EXPR expressions inserted",
5811 num_asserts);
5815 /* Traverse the flowgraph looking for conditional jumps to insert range
5816 expressions. These range expressions are meant to provide information
5817 to optimizations that need to reason in terms of value ranges. They
5818 will not be expanded into RTL. For instance, given:
5820 x = ...
5821 y = ...
5822 if (x < y)
5823 y = x - 2;
5824 else
5825 x = y + 3;
5827 this pass will transform the code into:
5829 x = ...
5830 y = ...
5831 if (x < y)
5833 x = ASSERT_EXPR <x, x < y>
5834 y = x - 2
5836 else
5838 y = ASSERT_EXPR <y, x <= y>
5839 x = y + 3
5842 The idea is that once copy and constant propagation have run, other
5843 optimizations will be able to determine what ranges of values can 'x'
5844 take in different paths of the code, simply by checking the reaching
5845 definition of 'x'. */
5847 static void
5848 insert_range_assertions (void)
5850 need_assert_for = BITMAP_ALLOC (NULL);
5851 asserts_for = XCNEWVEC (assert_locus_t, num_ssa_names);
5853 calculate_dominance_info (CDI_DOMINATORS);
5855 if (find_assert_locations ())
5857 process_assert_insertions ();
5858 update_ssa (TODO_update_ssa_no_phi);
5861 if (dump_file && (dump_flags & TDF_DETAILS))
5863 fprintf (dump_file, "\nSSA form after inserting ASSERT_EXPRs\n");
5864 dump_function_to_file (current_function_decl, dump_file, dump_flags);
5867 free (asserts_for);
5868 BITMAP_FREE (need_assert_for);
5871 /* Checks one ARRAY_REF in REF, located at LOCUS. Ignores flexible arrays
5872 and "struct" hacks. If VRP can determine that the
5873 array subscript is a constant, check if it is outside valid
5874 range. If the array subscript is a RANGE, warn if it is
5875 non-overlapping with valid range.
5876 IGNORE_OFF_BY_ONE is true if the ARRAY_REF is inside a ADDR_EXPR. */
5878 static void
5879 check_array_ref (location_t location, tree ref, bool ignore_off_by_one)
5881 value_range_t* vr = NULL;
5882 tree low_sub, up_sub;
5883 tree low_bound, up_bound, up_bound_p1;
5884 tree base;
5886 if (TREE_NO_WARNING (ref))
5887 return;
5889 low_sub = up_sub = TREE_OPERAND (ref, 1);
5890 up_bound = array_ref_up_bound (ref);
5892 /* Can not check flexible arrays. */
5893 if (!up_bound
5894 || TREE_CODE (up_bound) != INTEGER_CST)
5895 return;
5897 /* Accesses to trailing arrays via pointers may access storage
5898 beyond the types array bounds. */
5899 base = get_base_address (ref);
5900 if (base && TREE_CODE (base) == MEM_REF)
5902 tree cref, next = NULL_TREE;
5904 if (TREE_CODE (TREE_OPERAND (ref, 0)) != COMPONENT_REF)
5905 return;
5907 cref = TREE_OPERAND (ref, 0);
5908 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (cref, 0))) == RECORD_TYPE)
5909 for (next = DECL_CHAIN (TREE_OPERAND (cref, 1));
5910 next && TREE_CODE (next) != FIELD_DECL;
5911 next = DECL_CHAIN (next))
5914 /* If this is the last field in a struct type or a field in a
5915 union type do not warn. */
5916 if (!next)
5917 return;
5920 low_bound = array_ref_low_bound (ref);
5921 up_bound_p1 = int_const_binop (PLUS_EXPR, up_bound, integer_one_node);
5923 if (TREE_CODE (low_sub) == SSA_NAME)
5925 vr = get_value_range (low_sub);
5926 if (vr->type == VR_RANGE || vr->type == VR_ANTI_RANGE)
5928 low_sub = vr->type == VR_RANGE ? vr->max : vr->min;
5929 up_sub = vr->type == VR_RANGE ? vr->min : vr->max;
5933 if (vr && vr->type == VR_ANTI_RANGE)
5935 if (TREE_CODE (up_sub) == INTEGER_CST
5936 && tree_int_cst_lt (up_bound, up_sub)
5937 && TREE_CODE (low_sub) == INTEGER_CST
5938 && tree_int_cst_lt (low_sub, low_bound))
5940 warning_at (location, OPT_Warray_bounds,
5941 "array subscript is outside array bounds");
5942 TREE_NO_WARNING (ref) = 1;
5945 else if (TREE_CODE (up_sub) == INTEGER_CST
5946 && (ignore_off_by_one
5947 ? (tree_int_cst_lt (up_bound, up_sub)
5948 && !tree_int_cst_equal (up_bound_p1, up_sub))
5949 : (tree_int_cst_lt (up_bound, up_sub)
5950 || tree_int_cst_equal (up_bound_p1, up_sub))))
5952 warning_at (location, OPT_Warray_bounds,
5953 "array subscript is above array bounds");
5954 TREE_NO_WARNING (ref) = 1;
5956 else if (TREE_CODE (low_sub) == INTEGER_CST
5957 && tree_int_cst_lt (low_sub, low_bound))
5959 warning_at (location, OPT_Warray_bounds,
5960 "array subscript is below array bounds");
5961 TREE_NO_WARNING (ref) = 1;
5965 /* Searches if the expr T, located at LOCATION computes
5966 address of an ARRAY_REF, and call check_array_ref on it. */
5968 static void
5969 search_for_addr_array (tree t, location_t location)
5971 while (TREE_CODE (t) == SSA_NAME)
5973 gimple g = SSA_NAME_DEF_STMT (t);
5975 if (gimple_code (g) != GIMPLE_ASSIGN)
5976 return;
5978 if (get_gimple_rhs_class (gimple_assign_rhs_code (g))
5979 != GIMPLE_SINGLE_RHS)
5980 return;
5982 t = gimple_assign_rhs1 (g);
5986 /* We are only interested in addresses of ARRAY_REF's. */
5987 if (TREE_CODE (t) != ADDR_EXPR)
5988 return;
5990 /* Check each ARRAY_REFs in the reference chain. */
5993 if (TREE_CODE (t) == ARRAY_REF)
5994 check_array_ref (location, t, true /*ignore_off_by_one*/);
5996 t = TREE_OPERAND (t, 0);
5998 while (handled_component_p (t));
6000 if (TREE_CODE (t) == MEM_REF
6001 && TREE_CODE (TREE_OPERAND (t, 0)) == ADDR_EXPR
6002 && !TREE_NO_WARNING (t))
6004 tree tem = TREE_OPERAND (TREE_OPERAND (t, 0), 0);
6005 tree low_bound, up_bound, el_sz;
6006 double_int idx;
6007 if (TREE_CODE (TREE_TYPE (tem)) != ARRAY_TYPE
6008 || TREE_CODE (TREE_TYPE (TREE_TYPE (tem))) == ARRAY_TYPE
6009 || !TYPE_DOMAIN (TREE_TYPE (tem)))
6010 return;
6012 low_bound = TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (tem)));
6013 up_bound = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (tem)));
6014 el_sz = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (tem)));
6015 if (!low_bound
6016 || TREE_CODE (low_bound) != INTEGER_CST
6017 || !up_bound
6018 || TREE_CODE (up_bound) != INTEGER_CST
6019 || !el_sz
6020 || TREE_CODE (el_sz) != INTEGER_CST)
6021 return;
6023 idx = mem_ref_offset (t);
6024 idx = idx.sdiv (tree_to_double_int (el_sz), TRUNC_DIV_EXPR);
6025 if (idx.slt (double_int_zero))
6027 warning_at (location, OPT_Warray_bounds,
6028 "array subscript is below array bounds");
6029 TREE_NO_WARNING (t) = 1;
6031 else if (idx.sgt (tree_to_double_int (up_bound)
6032 - tree_to_double_int (low_bound)
6033 + double_int_one))
6035 warning_at (location, OPT_Warray_bounds,
6036 "array subscript is above array bounds");
6037 TREE_NO_WARNING (t) = 1;
6042 /* walk_tree() callback that checks if *TP is
6043 an ARRAY_REF inside an ADDR_EXPR (in which an array
6044 subscript one outside the valid range is allowed). Call
6045 check_array_ref for each ARRAY_REF found. The location is
6046 passed in DATA. */
6048 static tree
6049 check_array_bounds (tree *tp, int *walk_subtree, void *data)
6051 tree t = *tp;
6052 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
6053 location_t location;
6055 if (EXPR_HAS_LOCATION (t))
6056 location = EXPR_LOCATION (t);
6057 else
6059 location_t *locp = (location_t *) wi->info;
6060 location = *locp;
6063 *walk_subtree = TRUE;
6065 if (TREE_CODE (t) == ARRAY_REF)
6066 check_array_ref (location, t, false /*ignore_off_by_one*/);
6068 if (TREE_CODE (t) == MEM_REF
6069 || (TREE_CODE (t) == RETURN_EXPR && TREE_OPERAND (t, 0)))
6070 search_for_addr_array (TREE_OPERAND (t, 0), location);
6072 if (TREE_CODE (t) == ADDR_EXPR)
6073 *walk_subtree = FALSE;
6075 return NULL_TREE;
6078 /* Walk over all statements of all reachable BBs and call check_array_bounds
6079 on them. */
6081 static void
6082 check_all_array_refs (void)
6084 basic_block bb;
6085 gimple_stmt_iterator si;
6087 FOR_EACH_BB (bb)
6089 edge_iterator ei;
6090 edge e;
6091 bool executable = false;
6093 /* Skip blocks that were found to be unreachable. */
6094 FOR_EACH_EDGE (e, ei, bb->preds)
6095 executable |= !!(e->flags & EDGE_EXECUTABLE);
6096 if (!executable)
6097 continue;
6099 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
6101 gimple stmt = gsi_stmt (si);
6102 struct walk_stmt_info wi;
6103 if (!gimple_has_location (stmt))
6104 continue;
6106 if (is_gimple_call (stmt))
6108 size_t i;
6109 size_t n = gimple_call_num_args (stmt);
6110 for (i = 0; i < n; i++)
6112 tree arg = gimple_call_arg (stmt, i);
6113 search_for_addr_array (arg, gimple_location (stmt));
6116 else
6118 memset (&wi, 0, sizeof (wi));
6119 wi.info = CONST_CAST (void *, (const void *)
6120 gimple_location_ptr (stmt));
6122 walk_gimple_op (gsi_stmt (si),
6123 check_array_bounds,
6124 &wi);
6130 /* Convert range assertion expressions into the implied copies and
6131 copy propagate away the copies. Doing the trivial copy propagation
6132 here avoids the need to run the full copy propagation pass after
6133 VRP.
6135 FIXME, this will eventually lead to copy propagation removing the
6136 names that had useful range information attached to them. For
6137 instance, if we had the assertion N_i = ASSERT_EXPR <N_j, N_j > 3>,
6138 then N_i will have the range [3, +INF].
6140 However, by converting the assertion into the implied copy
6141 operation N_i = N_j, we will then copy-propagate N_j into the uses
6142 of N_i and lose the range information. We may want to hold on to
6143 ASSERT_EXPRs a little while longer as the ranges could be used in
6144 things like jump threading.
6146 The problem with keeping ASSERT_EXPRs around is that passes after
6147 VRP need to handle them appropriately.
6149 Another approach would be to make the range information a first
6150 class property of the SSA_NAME so that it can be queried from
6151 any pass. This is made somewhat more complex by the need for
6152 multiple ranges to be associated with one SSA_NAME. */
6154 static void
6155 remove_range_assertions (void)
6157 basic_block bb;
6158 gimple_stmt_iterator si;
6160 /* Note that the BSI iterator bump happens at the bottom of the
6161 loop and no bump is necessary if we're removing the statement
6162 referenced by the current BSI. */
6163 FOR_EACH_BB (bb)
6164 for (si = gsi_start_bb (bb); !gsi_end_p (si);)
6166 gimple stmt = gsi_stmt (si);
6167 gimple use_stmt;
6169 if (is_gimple_assign (stmt)
6170 && gimple_assign_rhs_code (stmt) == ASSERT_EXPR)
6172 tree rhs = gimple_assign_rhs1 (stmt);
6173 tree var;
6174 tree cond = fold (ASSERT_EXPR_COND (rhs));
6175 use_operand_p use_p;
6176 imm_use_iterator iter;
6178 gcc_assert (cond != boolean_false_node);
6180 /* Propagate the RHS into every use of the LHS. */
6181 var = ASSERT_EXPR_VAR (rhs);
6182 FOR_EACH_IMM_USE_STMT (use_stmt, iter,
6183 gimple_assign_lhs (stmt))
6184 FOR_EACH_IMM_USE_ON_STMT (use_p, iter)
6186 SET_USE (use_p, var);
6187 gcc_assert (TREE_CODE (var) == SSA_NAME);
6190 /* And finally, remove the copy, it is not needed. */
6191 gsi_remove (&si, true);
6192 release_defs (stmt);
6194 else
6195 gsi_next (&si);
6200 /* Return true if STMT is interesting for VRP. */
6202 static bool
6203 stmt_interesting_for_vrp (gimple stmt)
6205 if (gimple_code (stmt) == GIMPLE_PHI)
6207 tree res = gimple_phi_result (stmt);
6208 return (!virtual_operand_p (res)
6209 && (INTEGRAL_TYPE_P (TREE_TYPE (res))
6210 || POINTER_TYPE_P (TREE_TYPE (res))));
6212 else if (is_gimple_assign (stmt) || is_gimple_call (stmt))
6214 tree lhs = gimple_get_lhs (stmt);
6216 /* In general, assignments with virtual operands are not useful
6217 for deriving ranges, with the obvious exception of calls to
6218 builtin functions. */
6219 if (lhs && TREE_CODE (lhs) == SSA_NAME
6220 && (INTEGRAL_TYPE_P (TREE_TYPE (lhs))
6221 || POINTER_TYPE_P (TREE_TYPE (lhs)))
6222 && ((is_gimple_call (stmt)
6223 && gimple_call_fndecl (stmt) != NULL_TREE
6224 && DECL_BUILT_IN (gimple_call_fndecl (stmt)))
6225 || !gimple_vuse (stmt)))
6226 return true;
6228 else if (gimple_code (stmt) == GIMPLE_COND
6229 || gimple_code (stmt) == GIMPLE_SWITCH)
6230 return true;
6232 return false;
6236 /* Initialize local data structures for VRP. */
6238 static void
6239 vrp_initialize (void)
6241 basic_block bb;
6243 values_propagated = false;
6244 num_vr_values = num_ssa_names;
6245 vr_value = XCNEWVEC (value_range_t *, num_vr_values);
6246 vr_phi_edge_counts = XCNEWVEC (int, num_ssa_names);
6248 FOR_EACH_BB (bb)
6250 gimple_stmt_iterator si;
6252 for (si = gsi_start_phis (bb); !gsi_end_p (si); gsi_next (&si))
6254 gimple phi = gsi_stmt (si);
6255 if (!stmt_interesting_for_vrp (phi))
6257 tree lhs = PHI_RESULT (phi);
6258 set_value_range_to_varying (get_value_range (lhs));
6259 prop_set_simulate_again (phi, false);
6261 else
6262 prop_set_simulate_again (phi, true);
6265 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
6267 gimple stmt = gsi_stmt (si);
6269 /* If the statement is a control insn, then we do not
6270 want to avoid simulating the statement once. Failure
6271 to do so means that those edges will never get added. */
6272 if (stmt_ends_bb_p (stmt))
6273 prop_set_simulate_again (stmt, true);
6274 else if (!stmt_interesting_for_vrp (stmt))
6276 ssa_op_iter i;
6277 tree def;
6278 FOR_EACH_SSA_TREE_OPERAND (def, stmt, i, SSA_OP_DEF)
6279 set_value_range_to_varying (get_value_range (def));
6280 prop_set_simulate_again (stmt, false);
6282 else
6283 prop_set_simulate_again (stmt, true);
6288 /* Return the singleton value-range for NAME or NAME. */
6290 static inline tree
6291 vrp_valueize (tree name)
6293 if (TREE_CODE (name) == SSA_NAME)
6295 value_range_t *vr = get_value_range (name);
6296 if (vr->type == VR_RANGE
6297 && (vr->min == vr->max
6298 || operand_equal_p (vr->min, vr->max, 0)))
6299 return vr->min;
6301 return name;
6304 /* Visit assignment STMT. If it produces an interesting range, record
6305 the SSA name in *OUTPUT_P. */
6307 static enum ssa_prop_result
6308 vrp_visit_assignment_or_call (gimple stmt, tree *output_p)
6310 tree def, lhs;
6311 ssa_op_iter iter;
6312 enum gimple_code code = gimple_code (stmt);
6313 lhs = gimple_get_lhs (stmt);
6315 /* We only keep track of ranges in integral and pointer types. */
6316 if (TREE_CODE (lhs) == SSA_NAME
6317 && ((INTEGRAL_TYPE_P (TREE_TYPE (lhs))
6318 /* It is valid to have NULL MIN/MAX values on a type. See
6319 build_range_type. */
6320 && TYPE_MIN_VALUE (TREE_TYPE (lhs))
6321 && TYPE_MAX_VALUE (TREE_TYPE (lhs)))
6322 || POINTER_TYPE_P (TREE_TYPE (lhs))))
6324 value_range_t new_vr = VR_INITIALIZER;
6326 /* Try folding the statement to a constant first. */
6327 tree tem = gimple_fold_stmt_to_constant (stmt, vrp_valueize);
6328 if (tem && !is_overflow_infinity (tem))
6329 set_value_range (&new_vr, VR_RANGE, tem, tem, NULL);
6330 /* Then dispatch to value-range extracting functions. */
6331 else if (code == GIMPLE_CALL)
6332 extract_range_basic (&new_vr, stmt);
6333 else
6334 extract_range_from_assignment (&new_vr, stmt);
6336 if (update_value_range (lhs, &new_vr))
6338 *output_p = lhs;
6340 if (dump_file && (dump_flags & TDF_DETAILS))
6342 fprintf (dump_file, "Found new range for ");
6343 print_generic_expr (dump_file, lhs, 0);
6344 fprintf (dump_file, ": ");
6345 dump_value_range (dump_file, &new_vr);
6346 fprintf (dump_file, "\n\n");
6349 if (new_vr.type == VR_VARYING)
6350 return SSA_PROP_VARYING;
6352 return SSA_PROP_INTERESTING;
6355 return SSA_PROP_NOT_INTERESTING;
6358 /* Every other statement produces no useful ranges. */
6359 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_DEF)
6360 set_value_range_to_varying (get_value_range (def));
6362 return SSA_PROP_VARYING;
6365 /* Helper that gets the value range of the SSA_NAME with version I
6366 or a symbolic range containing the SSA_NAME only if the value range
6367 is varying or undefined. */
6369 static inline value_range_t
6370 get_vr_for_comparison (int i)
6372 value_range_t vr = *get_value_range (ssa_name (i));
6374 /* If name N_i does not have a valid range, use N_i as its own
6375 range. This allows us to compare against names that may
6376 have N_i in their ranges. */
6377 if (vr.type == VR_VARYING || vr.type == VR_UNDEFINED)
6379 vr.type = VR_RANGE;
6380 vr.min = ssa_name (i);
6381 vr.max = ssa_name (i);
6384 return vr;
6387 /* Compare all the value ranges for names equivalent to VAR with VAL
6388 using comparison code COMP. Return the same value returned by
6389 compare_range_with_value, including the setting of
6390 *STRICT_OVERFLOW_P. */
6392 static tree
6393 compare_name_with_value (enum tree_code comp, tree var, tree val,
6394 bool *strict_overflow_p)
6396 bitmap_iterator bi;
6397 unsigned i;
6398 bitmap e;
6399 tree retval, t;
6400 int used_strict_overflow;
6401 bool sop;
6402 value_range_t equiv_vr;
6404 /* Get the set of equivalences for VAR. */
6405 e = get_value_range (var)->equiv;
6407 /* Start at -1. Set it to 0 if we do a comparison without relying
6408 on overflow, or 1 if all comparisons rely on overflow. */
6409 used_strict_overflow = -1;
6411 /* Compare vars' value range with val. */
6412 equiv_vr = get_vr_for_comparison (SSA_NAME_VERSION (var));
6413 sop = false;
6414 retval = compare_range_with_value (comp, &equiv_vr, val, &sop);
6415 if (retval)
6416 used_strict_overflow = sop ? 1 : 0;
6418 /* If the equiv set is empty we have done all work we need to do. */
6419 if (e == NULL)
6421 if (retval
6422 && used_strict_overflow > 0)
6423 *strict_overflow_p = true;
6424 return retval;
6427 EXECUTE_IF_SET_IN_BITMAP (e, 0, i, bi)
6429 equiv_vr = get_vr_for_comparison (i);
6430 sop = false;
6431 t = compare_range_with_value (comp, &equiv_vr, val, &sop);
6432 if (t)
6434 /* If we get different answers from different members
6435 of the equivalence set this check must be in a dead
6436 code region. Folding it to a trap representation
6437 would be correct here. For now just return don't-know. */
6438 if (retval != NULL
6439 && t != retval)
6441 retval = NULL_TREE;
6442 break;
6444 retval = t;
6446 if (!sop)
6447 used_strict_overflow = 0;
6448 else if (used_strict_overflow < 0)
6449 used_strict_overflow = 1;
6453 if (retval
6454 && used_strict_overflow > 0)
6455 *strict_overflow_p = true;
6457 return retval;
6461 /* Given a comparison code COMP and names N1 and N2, compare all the
6462 ranges equivalent to N1 against all the ranges equivalent to N2
6463 to determine the value of N1 COMP N2. Return the same value
6464 returned by compare_ranges. Set *STRICT_OVERFLOW_P to indicate
6465 whether we relied on an overflow infinity in the comparison. */
6468 static tree
6469 compare_names (enum tree_code comp, tree n1, tree n2,
6470 bool *strict_overflow_p)
6472 tree t, retval;
6473 bitmap e1, e2;
6474 bitmap_iterator bi1, bi2;
6475 unsigned i1, i2;
6476 int used_strict_overflow;
6477 static bitmap_obstack *s_obstack = NULL;
6478 static bitmap s_e1 = NULL, s_e2 = NULL;
6480 /* Compare the ranges of every name equivalent to N1 against the
6481 ranges of every name equivalent to N2. */
6482 e1 = get_value_range (n1)->equiv;
6483 e2 = get_value_range (n2)->equiv;
6485 /* Use the fake bitmaps if e1 or e2 are not available. */
6486 if (s_obstack == NULL)
6488 s_obstack = XNEW (bitmap_obstack);
6489 bitmap_obstack_initialize (s_obstack);
6490 s_e1 = BITMAP_ALLOC (s_obstack);
6491 s_e2 = BITMAP_ALLOC (s_obstack);
6493 if (e1 == NULL)
6494 e1 = s_e1;
6495 if (e2 == NULL)
6496 e2 = s_e2;
6498 /* Add N1 and N2 to their own set of equivalences to avoid
6499 duplicating the body of the loop just to check N1 and N2
6500 ranges. */
6501 bitmap_set_bit (e1, SSA_NAME_VERSION (n1));
6502 bitmap_set_bit (e2, SSA_NAME_VERSION (n2));
6504 /* If the equivalence sets have a common intersection, then the two
6505 names can be compared without checking their ranges. */
6506 if (bitmap_intersect_p (e1, e2))
6508 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
6509 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
6511 return (comp == EQ_EXPR || comp == GE_EXPR || comp == LE_EXPR)
6512 ? boolean_true_node
6513 : boolean_false_node;
6516 /* Start at -1. Set it to 0 if we do a comparison without relying
6517 on overflow, or 1 if all comparisons rely on overflow. */
6518 used_strict_overflow = -1;
6520 /* Otherwise, compare all the equivalent ranges. First, add N1 and
6521 N2 to their own set of equivalences to avoid duplicating the body
6522 of the loop just to check N1 and N2 ranges. */
6523 EXECUTE_IF_SET_IN_BITMAP (e1, 0, i1, bi1)
6525 value_range_t vr1 = get_vr_for_comparison (i1);
6527 t = retval = NULL_TREE;
6528 EXECUTE_IF_SET_IN_BITMAP (e2, 0, i2, bi2)
6530 bool sop = false;
6532 value_range_t vr2 = get_vr_for_comparison (i2);
6534 t = compare_ranges (comp, &vr1, &vr2, &sop);
6535 if (t)
6537 /* If we get different answers from different members
6538 of the equivalence set this check must be in a dead
6539 code region. Folding it to a trap representation
6540 would be correct here. For now just return don't-know. */
6541 if (retval != NULL
6542 && t != retval)
6544 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
6545 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
6546 return NULL_TREE;
6548 retval = t;
6550 if (!sop)
6551 used_strict_overflow = 0;
6552 else if (used_strict_overflow < 0)
6553 used_strict_overflow = 1;
6557 if (retval)
6559 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
6560 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
6561 if (used_strict_overflow > 0)
6562 *strict_overflow_p = true;
6563 return retval;
6567 /* None of the equivalent ranges are useful in computing this
6568 comparison. */
6569 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
6570 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
6571 return NULL_TREE;
6574 /* Helper function for vrp_evaluate_conditional_warnv. */
6576 static tree
6577 vrp_evaluate_conditional_warnv_with_ops_using_ranges (enum tree_code code,
6578 tree op0, tree op1,
6579 bool * strict_overflow_p)
6581 value_range_t *vr0, *vr1;
6583 vr0 = (TREE_CODE (op0) == SSA_NAME) ? get_value_range (op0) : NULL;
6584 vr1 = (TREE_CODE (op1) == SSA_NAME) ? get_value_range (op1) : NULL;
6586 if (vr0 && vr1)
6587 return compare_ranges (code, vr0, vr1, strict_overflow_p);
6588 else if (vr0 && vr1 == NULL)
6589 return compare_range_with_value (code, vr0, op1, strict_overflow_p);
6590 else if (vr0 == NULL && vr1)
6591 return (compare_range_with_value
6592 (swap_tree_comparison (code), vr1, op0, strict_overflow_p));
6593 return NULL;
6596 /* Helper function for vrp_evaluate_conditional_warnv. */
6598 static tree
6599 vrp_evaluate_conditional_warnv_with_ops (enum tree_code code, tree op0,
6600 tree op1, bool use_equiv_p,
6601 bool *strict_overflow_p, bool *only_ranges)
6603 tree ret;
6604 if (only_ranges)
6605 *only_ranges = true;
6607 /* We only deal with integral and pointer types. */
6608 if (!INTEGRAL_TYPE_P (TREE_TYPE (op0))
6609 && !POINTER_TYPE_P (TREE_TYPE (op0)))
6610 return NULL_TREE;
6612 if (use_equiv_p)
6614 if (only_ranges
6615 && (ret = vrp_evaluate_conditional_warnv_with_ops_using_ranges
6616 (code, op0, op1, strict_overflow_p)))
6617 return ret;
6618 *only_ranges = false;
6619 if (TREE_CODE (op0) == SSA_NAME && TREE_CODE (op1) == SSA_NAME)
6620 return compare_names (code, op0, op1, strict_overflow_p);
6621 else if (TREE_CODE (op0) == SSA_NAME)
6622 return compare_name_with_value (code, op0, op1, strict_overflow_p);
6623 else if (TREE_CODE (op1) == SSA_NAME)
6624 return (compare_name_with_value
6625 (swap_tree_comparison (code), op1, op0, strict_overflow_p));
6627 else
6628 return vrp_evaluate_conditional_warnv_with_ops_using_ranges (code, op0, op1,
6629 strict_overflow_p);
6630 return NULL_TREE;
6633 /* Given (CODE OP0 OP1) within STMT, try to simplify it based on value range
6634 information. Return NULL if the conditional can not be evaluated.
6635 The ranges of all the names equivalent with the operands in COND
6636 will be used when trying to compute the value. If the result is
6637 based on undefined signed overflow, issue a warning if
6638 appropriate. */
6640 static tree
6641 vrp_evaluate_conditional (enum tree_code code, tree op0, tree op1, gimple stmt)
6643 bool sop;
6644 tree ret;
6645 bool only_ranges;
6647 /* Some passes and foldings leak constants with overflow flag set
6648 into the IL. Avoid doing wrong things with these and bail out. */
6649 if ((TREE_CODE (op0) == INTEGER_CST
6650 && TREE_OVERFLOW (op0))
6651 || (TREE_CODE (op1) == INTEGER_CST
6652 && TREE_OVERFLOW (op1)))
6653 return NULL_TREE;
6655 sop = false;
6656 ret = vrp_evaluate_conditional_warnv_with_ops (code, op0, op1, true, &sop,
6657 &only_ranges);
6659 if (ret && sop)
6661 enum warn_strict_overflow_code wc;
6662 const char* warnmsg;
6664 if (is_gimple_min_invariant (ret))
6666 wc = WARN_STRICT_OVERFLOW_CONDITIONAL;
6667 warnmsg = G_("assuming signed overflow does not occur when "
6668 "simplifying conditional to constant");
6670 else
6672 wc = WARN_STRICT_OVERFLOW_COMPARISON;
6673 warnmsg = G_("assuming signed overflow does not occur when "
6674 "simplifying conditional");
6677 if (issue_strict_overflow_warning (wc))
6679 location_t location;
6681 if (!gimple_has_location (stmt))
6682 location = input_location;
6683 else
6684 location = gimple_location (stmt);
6685 warning_at (location, OPT_Wstrict_overflow, "%s", warnmsg);
6689 if (warn_type_limits
6690 && ret && only_ranges
6691 && TREE_CODE_CLASS (code) == tcc_comparison
6692 && TREE_CODE (op0) == SSA_NAME)
6694 /* If the comparison is being folded and the operand on the LHS
6695 is being compared against a constant value that is outside of
6696 the natural range of OP0's type, then the predicate will
6697 always fold regardless of the value of OP0. If -Wtype-limits
6698 was specified, emit a warning. */
6699 tree type = TREE_TYPE (op0);
6700 value_range_t *vr0 = get_value_range (op0);
6702 if (vr0->type != VR_VARYING
6703 && INTEGRAL_TYPE_P (type)
6704 && vrp_val_is_min (vr0->min)
6705 && vrp_val_is_max (vr0->max)
6706 && is_gimple_min_invariant (op1))
6708 location_t location;
6710 if (!gimple_has_location (stmt))
6711 location = input_location;
6712 else
6713 location = gimple_location (stmt);
6715 warning_at (location, OPT_Wtype_limits,
6716 integer_zerop (ret)
6717 ? G_("comparison always false "
6718 "due to limited range of data type")
6719 : G_("comparison always true "
6720 "due to limited range of data type"));
6724 return ret;
6728 /* Visit conditional statement STMT. If we can determine which edge
6729 will be taken out of STMT's basic block, record it in
6730 *TAKEN_EDGE_P and return SSA_PROP_INTERESTING. Otherwise, return
6731 SSA_PROP_VARYING. */
6733 static enum ssa_prop_result
6734 vrp_visit_cond_stmt (gimple stmt, edge *taken_edge_p)
6736 tree val;
6737 bool sop;
6739 *taken_edge_p = NULL;
6741 if (dump_file && (dump_flags & TDF_DETAILS))
6743 tree use;
6744 ssa_op_iter i;
6746 fprintf (dump_file, "\nVisiting conditional with predicate: ");
6747 print_gimple_stmt (dump_file, stmt, 0, 0);
6748 fprintf (dump_file, "\nWith known ranges\n");
6750 FOR_EACH_SSA_TREE_OPERAND (use, stmt, i, SSA_OP_USE)
6752 fprintf (dump_file, "\t");
6753 print_generic_expr (dump_file, use, 0);
6754 fprintf (dump_file, ": ");
6755 dump_value_range (dump_file, vr_value[SSA_NAME_VERSION (use)]);
6758 fprintf (dump_file, "\n");
6761 /* Compute the value of the predicate COND by checking the known
6762 ranges of each of its operands.
6764 Note that we cannot evaluate all the equivalent ranges here
6765 because those ranges may not yet be final and with the current
6766 propagation strategy, we cannot determine when the value ranges
6767 of the names in the equivalence set have changed.
6769 For instance, given the following code fragment
6771 i_5 = PHI <8, i_13>
6773 i_14 = ASSERT_EXPR <i_5, i_5 != 0>
6774 if (i_14 == 1)
6777 Assume that on the first visit to i_14, i_5 has the temporary
6778 range [8, 8] because the second argument to the PHI function is
6779 not yet executable. We derive the range ~[0, 0] for i_14 and the
6780 equivalence set { i_5 }. So, when we visit 'if (i_14 == 1)' for
6781 the first time, since i_14 is equivalent to the range [8, 8], we
6782 determine that the predicate is always false.
6784 On the next round of propagation, i_13 is determined to be
6785 VARYING, which causes i_5 to drop down to VARYING. So, another
6786 visit to i_14 is scheduled. In this second visit, we compute the
6787 exact same range and equivalence set for i_14, namely ~[0, 0] and
6788 { i_5 }. But we did not have the previous range for i_5
6789 registered, so vrp_visit_assignment thinks that the range for
6790 i_14 has not changed. Therefore, the predicate 'if (i_14 == 1)'
6791 is not visited again, which stops propagation from visiting
6792 statements in the THEN clause of that if().
6794 To properly fix this we would need to keep the previous range
6795 value for the names in the equivalence set. This way we would've
6796 discovered that from one visit to the other i_5 changed from
6797 range [8, 8] to VR_VARYING.
6799 However, fixing this apparent limitation may not be worth the
6800 additional checking. Testing on several code bases (GCC, DLV,
6801 MICO, TRAMP3D and SPEC2000) showed that doing this results in
6802 4 more predicates folded in SPEC. */
6803 sop = false;
6805 val = vrp_evaluate_conditional_warnv_with_ops (gimple_cond_code (stmt),
6806 gimple_cond_lhs (stmt),
6807 gimple_cond_rhs (stmt),
6808 false, &sop, NULL);
6809 if (val)
6811 if (!sop)
6812 *taken_edge_p = find_taken_edge (gimple_bb (stmt), val);
6813 else
6815 if (dump_file && (dump_flags & TDF_DETAILS))
6816 fprintf (dump_file,
6817 "\nIgnoring predicate evaluation because "
6818 "it assumes that signed overflow is undefined");
6819 val = NULL_TREE;
6823 if (dump_file && (dump_flags & TDF_DETAILS))
6825 fprintf (dump_file, "\nPredicate evaluates to: ");
6826 if (val == NULL_TREE)
6827 fprintf (dump_file, "DON'T KNOW\n");
6828 else
6829 print_generic_stmt (dump_file, val, 0);
6832 return (*taken_edge_p) ? SSA_PROP_INTERESTING : SSA_PROP_VARYING;
6835 /* Searches the case label vector VEC for the index *IDX of the CASE_LABEL
6836 that includes the value VAL. The search is restricted to the range
6837 [START_IDX, n - 1] where n is the size of VEC.
6839 If there is a CASE_LABEL for VAL, its index is placed in IDX and true is
6840 returned.
6842 If there is no CASE_LABEL for VAL and there is one that is larger than VAL,
6843 it is placed in IDX and false is returned.
6845 If VAL is larger than any CASE_LABEL, n is placed on IDX and false is
6846 returned. */
6848 static bool
6849 find_case_label_index (gimple stmt, size_t start_idx, tree val, size_t *idx)
6851 size_t n = gimple_switch_num_labels (stmt);
6852 size_t low, high;
6854 /* Find case label for minimum of the value range or the next one.
6855 At each iteration we are searching in [low, high - 1]. */
6857 for (low = start_idx, high = n; high != low; )
6859 tree t;
6860 int cmp;
6861 /* Note that i != high, so we never ask for n. */
6862 size_t i = (high + low) / 2;
6863 t = gimple_switch_label (stmt, i);
6865 /* Cache the result of comparing CASE_LOW and val. */
6866 cmp = tree_int_cst_compare (CASE_LOW (t), val);
6868 if (cmp == 0)
6870 /* Ranges cannot be empty. */
6871 *idx = i;
6872 return true;
6874 else if (cmp > 0)
6875 high = i;
6876 else
6878 low = i + 1;
6879 if (CASE_HIGH (t) != NULL
6880 && tree_int_cst_compare (CASE_HIGH (t), val) >= 0)
6882 *idx = i;
6883 return true;
6888 *idx = high;
6889 return false;
6892 /* Searches the case label vector VEC for the range of CASE_LABELs that is used
6893 for values between MIN and MAX. The first index is placed in MIN_IDX. The
6894 last index is placed in MAX_IDX. If the range of CASE_LABELs is empty
6895 then MAX_IDX < MIN_IDX.
6896 Returns true if the default label is not needed. */
6898 static bool
6899 find_case_label_range (gimple stmt, tree min, tree max, size_t *min_idx,
6900 size_t *max_idx)
6902 size_t i, j;
6903 bool min_take_default = !find_case_label_index (stmt, 1, min, &i);
6904 bool max_take_default = !find_case_label_index (stmt, i, max, &j);
6906 if (i == j
6907 && min_take_default
6908 && max_take_default)
6910 /* Only the default case label reached.
6911 Return an empty range. */
6912 *min_idx = 1;
6913 *max_idx = 0;
6914 return false;
6916 else
6918 bool take_default = min_take_default || max_take_default;
6919 tree low, high;
6920 size_t k;
6922 if (max_take_default)
6923 j--;
6925 /* If the case label range is continuous, we do not need
6926 the default case label. Verify that. */
6927 high = CASE_LOW (gimple_switch_label (stmt, i));
6928 if (CASE_HIGH (gimple_switch_label (stmt, i)))
6929 high = CASE_HIGH (gimple_switch_label (stmt, i));
6930 for (k = i + 1; k <= j; ++k)
6932 low = CASE_LOW (gimple_switch_label (stmt, k));
6933 if (!integer_onep (int_const_binop (MINUS_EXPR, low, high)))
6935 take_default = true;
6936 break;
6938 high = low;
6939 if (CASE_HIGH (gimple_switch_label (stmt, k)))
6940 high = CASE_HIGH (gimple_switch_label (stmt, k));
6943 *min_idx = i;
6944 *max_idx = j;
6945 return !take_default;
6949 /* Searches the case label vector VEC for the ranges of CASE_LABELs that are
6950 used in range VR. The indices are placed in MIN_IDX1, MAX_IDX, MIN_IDX2 and
6951 MAX_IDX2. If the ranges of CASE_LABELs are empty then MAX_IDX1 < MIN_IDX1.
6952 Returns true if the default label is not needed. */
6954 static bool
6955 find_case_label_ranges (gimple stmt, value_range_t *vr, size_t *min_idx1,
6956 size_t *max_idx1, size_t *min_idx2,
6957 size_t *max_idx2)
6959 size_t i, j, k, l;
6960 unsigned int n = gimple_switch_num_labels (stmt);
6961 bool take_default;
6962 tree case_low, case_high;
6963 tree min = vr->min, max = vr->max;
6965 gcc_checking_assert (vr->type == VR_RANGE || vr->type == VR_ANTI_RANGE);
6967 take_default = !find_case_label_range (stmt, min, max, &i, &j);
6969 /* Set second range to emtpy. */
6970 *min_idx2 = 1;
6971 *max_idx2 = 0;
6973 if (vr->type == VR_RANGE)
6975 *min_idx1 = i;
6976 *max_idx1 = j;
6977 return !take_default;
6980 /* Set first range to all case labels. */
6981 *min_idx1 = 1;
6982 *max_idx1 = n - 1;
6984 if (i > j)
6985 return false;
6987 /* Make sure all the values of case labels [i , j] are contained in
6988 range [MIN, MAX]. */
6989 case_low = CASE_LOW (gimple_switch_label (stmt, i));
6990 case_high = CASE_HIGH (gimple_switch_label (stmt, j));
6991 if (tree_int_cst_compare (case_low, min) < 0)
6992 i += 1;
6993 if (case_high != NULL_TREE
6994 && tree_int_cst_compare (max, case_high) < 0)
6995 j -= 1;
6997 if (i > j)
6998 return false;
7000 /* If the range spans case labels [i, j], the corresponding anti-range spans
7001 the labels [1, i - 1] and [j + 1, n - 1]. */
7002 k = j + 1;
7003 l = n - 1;
7004 if (k > l)
7006 k = 1;
7007 l = 0;
7010 j = i - 1;
7011 i = 1;
7012 if (i > j)
7014 i = k;
7015 j = l;
7016 k = 1;
7017 l = 0;
7020 *min_idx1 = i;
7021 *max_idx1 = j;
7022 *min_idx2 = k;
7023 *max_idx2 = l;
7024 return false;
7027 /* Visit switch statement STMT. If we can determine which edge
7028 will be taken out of STMT's basic block, record it in
7029 *TAKEN_EDGE_P and return SSA_PROP_INTERESTING. Otherwise, return
7030 SSA_PROP_VARYING. */
7032 static enum ssa_prop_result
7033 vrp_visit_switch_stmt (gimple stmt, edge *taken_edge_p)
7035 tree op, val;
7036 value_range_t *vr;
7037 size_t i = 0, j = 0, k, l;
7038 bool take_default;
7040 *taken_edge_p = NULL;
7041 op = gimple_switch_index (stmt);
7042 if (TREE_CODE (op) != SSA_NAME)
7043 return SSA_PROP_VARYING;
7045 vr = get_value_range (op);
7046 if (dump_file && (dump_flags & TDF_DETAILS))
7048 fprintf (dump_file, "\nVisiting switch expression with operand ");
7049 print_generic_expr (dump_file, op, 0);
7050 fprintf (dump_file, " with known range ");
7051 dump_value_range (dump_file, vr);
7052 fprintf (dump_file, "\n");
7055 if ((vr->type != VR_RANGE
7056 && vr->type != VR_ANTI_RANGE)
7057 || symbolic_range_p (vr))
7058 return SSA_PROP_VARYING;
7060 /* Find the single edge that is taken from the switch expression. */
7061 take_default = !find_case_label_ranges (stmt, vr, &i, &j, &k, &l);
7063 /* Check if the range spans no CASE_LABEL. If so, we only reach the default
7064 label */
7065 if (j < i)
7067 gcc_assert (take_default);
7068 val = gimple_switch_default_label (stmt);
7070 else
7072 /* Check if labels with index i to j and maybe the default label
7073 are all reaching the same label. */
7075 val = gimple_switch_label (stmt, i);
7076 if (take_default
7077 && CASE_LABEL (gimple_switch_default_label (stmt))
7078 != CASE_LABEL (val))
7080 if (dump_file && (dump_flags & TDF_DETAILS))
7081 fprintf (dump_file, " not a single destination for this "
7082 "range\n");
7083 return SSA_PROP_VARYING;
7085 for (++i; i <= j; ++i)
7087 if (CASE_LABEL (gimple_switch_label (stmt, i)) != CASE_LABEL (val))
7089 if (dump_file && (dump_flags & TDF_DETAILS))
7090 fprintf (dump_file, " not a single destination for this "
7091 "range\n");
7092 return SSA_PROP_VARYING;
7095 for (; k <= l; ++k)
7097 if (CASE_LABEL (gimple_switch_label (stmt, k)) != CASE_LABEL (val))
7099 if (dump_file && (dump_flags & TDF_DETAILS))
7100 fprintf (dump_file, " not a single destination for this "
7101 "range\n");
7102 return SSA_PROP_VARYING;
7107 *taken_edge_p = find_edge (gimple_bb (stmt),
7108 label_to_block (CASE_LABEL (val)));
7110 if (dump_file && (dump_flags & TDF_DETAILS))
7112 fprintf (dump_file, " will take edge to ");
7113 print_generic_stmt (dump_file, CASE_LABEL (val), 0);
7116 return SSA_PROP_INTERESTING;
7120 /* Evaluate statement STMT. If the statement produces a useful range,
7121 return SSA_PROP_INTERESTING and record the SSA name with the
7122 interesting range into *OUTPUT_P.
7124 If STMT is a conditional branch and we can determine its truth
7125 value, the taken edge is recorded in *TAKEN_EDGE_P.
7127 If STMT produces a varying value, return SSA_PROP_VARYING. */
7129 static enum ssa_prop_result
7130 vrp_visit_stmt (gimple stmt, edge *taken_edge_p, tree *output_p)
7132 tree def;
7133 ssa_op_iter iter;
7135 if (dump_file && (dump_flags & TDF_DETAILS))
7137 fprintf (dump_file, "\nVisiting statement:\n");
7138 print_gimple_stmt (dump_file, stmt, 0, dump_flags);
7139 fprintf (dump_file, "\n");
7142 if (!stmt_interesting_for_vrp (stmt))
7143 gcc_assert (stmt_ends_bb_p (stmt));
7144 else if (is_gimple_assign (stmt) || is_gimple_call (stmt))
7146 /* In general, assignments with virtual operands are not useful
7147 for deriving ranges, with the obvious exception of calls to
7148 builtin functions. */
7149 if ((is_gimple_call (stmt)
7150 && gimple_call_fndecl (stmt) != NULL_TREE
7151 && DECL_BUILT_IN (gimple_call_fndecl (stmt)))
7152 || !gimple_vuse (stmt))
7153 return vrp_visit_assignment_or_call (stmt, output_p);
7155 else if (gimple_code (stmt) == GIMPLE_COND)
7156 return vrp_visit_cond_stmt (stmt, taken_edge_p);
7157 else if (gimple_code (stmt) == GIMPLE_SWITCH)
7158 return vrp_visit_switch_stmt (stmt, taken_edge_p);
7160 /* All other statements produce nothing of interest for VRP, so mark
7161 their outputs varying and prevent further simulation. */
7162 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_DEF)
7163 set_value_range_to_varying (get_value_range (def));
7165 return SSA_PROP_VARYING;
7168 /* Union the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
7169 { VR1TYPE, VR0MIN, VR0MAX } and store the result
7170 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
7171 possible such range. The resulting range is not canonicalized. */
7173 static void
7174 union_ranges (enum value_range_type *vr0type,
7175 tree *vr0min, tree *vr0max,
7176 enum value_range_type vr1type,
7177 tree vr1min, tree vr1max)
7179 bool mineq = operand_equal_p (*vr0min, vr1min, 0);
7180 bool maxeq = operand_equal_p (*vr0max, vr1max, 0);
7182 /* [] is vr0, () is vr1 in the following classification comments. */
7183 if (mineq && maxeq)
7185 /* [( )] */
7186 if (*vr0type == vr1type)
7187 /* Nothing to do for equal ranges. */
7189 else if ((*vr0type == VR_RANGE
7190 && vr1type == VR_ANTI_RANGE)
7191 || (*vr0type == VR_ANTI_RANGE
7192 && vr1type == VR_RANGE))
7194 /* For anti-range with range union the result is varying. */
7195 goto give_up;
7197 else
7198 gcc_unreachable ();
7200 else if (operand_less_p (*vr0max, vr1min) == 1
7201 || operand_less_p (vr1max, *vr0min) == 1)
7203 /* [ ] ( ) or ( ) [ ]
7204 If the ranges have an empty intersection, result of the union
7205 operation is the anti-range or if both are anti-ranges
7206 it covers all. */
7207 if (*vr0type == VR_ANTI_RANGE
7208 && vr1type == VR_ANTI_RANGE)
7209 goto give_up;
7210 else if (*vr0type == VR_ANTI_RANGE
7211 && vr1type == VR_RANGE)
7213 else if (*vr0type == VR_RANGE
7214 && vr1type == VR_ANTI_RANGE)
7216 *vr0type = vr1type;
7217 *vr0min = vr1min;
7218 *vr0max = vr1max;
7220 else if (*vr0type == VR_RANGE
7221 && vr1type == VR_RANGE)
7223 /* The result is the convex hull of both ranges. */
7224 if (operand_less_p (*vr0max, vr1min) == 1)
7226 /* If the result can be an anti-range, create one. */
7227 if (TREE_CODE (*vr0max) == INTEGER_CST
7228 && TREE_CODE (vr1min) == INTEGER_CST
7229 && vrp_val_is_min (*vr0min)
7230 && vrp_val_is_max (vr1max))
7232 tree min = int_const_binop (PLUS_EXPR,
7233 *vr0max, integer_one_node);
7234 tree max = int_const_binop (MINUS_EXPR,
7235 vr1min, integer_one_node);
7236 if (!operand_less_p (max, min))
7238 *vr0type = VR_ANTI_RANGE;
7239 *vr0min = min;
7240 *vr0max = max;
7242 else
7243 *vr0max = vr1max;
7245 else
7246 *vr0max = vr1max;
7248 else
7250 /* If the result can be an anti-range, create one. */
7251 if (TREE_CODE (vr1max) == INTEGER_CST
7252 && TREE_CODE (*vr0min) == INTEGER_CST
7253 && vrp_val_is_min (vr1min)
7254 && vrp_val_is_max (*vr0max))
7256 tree min = int_const_binop (PLUS_EXPR,
7257 vr1max, integer_one_node);
7258 tree max = int_const_binop (MINUS_EXPR,
7259 *vr0min, integer_one_node);
7260 if (!operand_less_p (max, min))
7262 *vr0type = VR_ANTI_RANGE;
7263 *vr0min = min;
7264 *vr0max = max;
7266 else
7267 *vr0min = vr1min;
7269 else
7270 *vr0min = vr1min;
7273 else
7274 gcc_unreachable ();
7276 else if ((maxeq || operand_less_p (vr1max, *vr0max) == 1)
7277 && (mineq || operand_less_p (*vr0min, vr1min) == 1))
7279 /* [ ( ) ] or [( ) ] or [ ( )] */
7280 if (*vr0type == VR_RANGE
7281 && vr1type == VR_RANGE)
7283 else if (*vr0type == VR_ANTI_RANGE
7284 && vr1type == VR_ANTI_RANGE)
7286 *vr0type = vr1type;
7287 *vr0min = vr1min;
7288 *vr0max = vr1max;
7290 else if (*vr0type == VR_ANTI_RANGE
7291 && vr1type == VR_RANGE)
7293 /* Arbitrarily choose the right or left gap. */
7294 if (!mineq && TREE_CODE (vr1min) == INTEGER_CST)
7295 *vr0max = int_const_binop (MINUS_EXPR, vr1min, integer_one_node);
7296 else if (!maxeq && TREE_CODE (vr1max) == INTEGER_CST)
7297 *vr0min = int_const_binop (PLUS_EXPR, vr1max, integer_one_node);
7298 else
7299 goto give_up;
7301 else if (*vr0type == VR_RANGE
7302 && vr1type == VR_ANTI_RANGE)
7303 /* The result covers everything. */
7304 goto give_up;
7305 else
7306 gcc_unreachable ();
7308 else if ((maxeq || operand_less_p (*vr0max, vr1max) == 1)
7309 && (mineq || operand_less_p (vr1min, *vr0min) == 1))
7311 /* ( [ ] ) or ([ ] ) or ( [ ]) */
7312 if (*vr0type == VR_RANGE
7313 && vr1type == VR_RANGE)
7315 *vr0type = vr1type;
7316 *vr0min = vr1min;
7317 *vr0max = vr1max;
7319 else if (*vr0type == VR_ANTI_RANGE
7320 && vr1type == VR_ANTI_RANGE)
7322 else if (*vr0type == VR_RANGE
7323 && vr1type == VR_ANTI_RANGE)
7325 *vr0type = VR_ANTI_RANGE;
7326 if (!mineq && TREE_CODE (*vr0min) == INTEGER_CST)
7328 *vr0max = int_const_binop (MINUS_EXPR, *vr0min, integer_one_node);
7329 *vr0min = vr1min;
7331 else if (!maxeq && TREE_CODE (*vr0max) == INTEGER_CST)
7333 *vr0min = int_const_binop (PLUS_EXPR, *vr0max, integer_one_node);
7334 *vr0max = vr1max;
7336 else
7337 goto give_up;
7339 else if (*vr0type == VR_ANTI_RANGE
7340 && vr1type == VR_RANGE)
7341 /* The result covers everything. */
7342 goto give_up;
7343 else
7344 gcc_unreachable ();
7346 else if ((operand_less_p (vr1min, *vr0max) == 1
7347 || operand_equal_p (vr1min, *vr0max, 0))
7348 && operand_less_p (*vr0min, vr1min) == 1)
7350 /* [ ( ] ) or [ ]( ) */
7351 if (*vr0type == VR_RANGE
7352 && vr1type == VR_RANGE)
7353 *vr0max = vr1max;
7354 else if (*vr0type == VR_ANTI_RANGE
7355 && vr1type == VR_ANTI_RANGE)
7356 *vr0min = vr1min;
7357 else if (*vr0type == VR_ANTI_RANGE
7358 && vr1type == VR_RANGE)
7360 if (TREE_CODE (vr1min) == INTEGER_CST)
7361 *vr0max = int_const_binop (MINUS_EXPR, vr1min, integer_one_node);
7362 else
7363 goto give_up;
7365 else if (*vr0type == VR_RANGE
7366 && vr1type == VR_ANTI_RANGE)
7368 if (TREE_CODE (*vr0max) == INTEGER_CST)
7370 *vr0type = vr1type;
7371 *vr0min = int_const_binop (PLUS_EXPR, *vr0max, integer_one_node);
7372 *vr0max = vr1max;
7374 else
7375 goto give_up;
7377 else
7378 gcc_unreachable ();
7380 else if ((operand_less_p (*vr0min, vr1max) == 1
7381 || operand_equal_p (*vr0min, vr1max, 0))
7382 && operand_less_p (vr1min, *vr0min) == 1)
7384 /* ( [ ) ] or ( )[ ] */
7385 if (*vr0type == VR_RANGE
7386 && vr1type == VR_RANGE)
7387 *vr0min = vr1min;
7388 else if (*vr0type == VR_ANTI_RANGE
7389 && vr1type == VR_ANTI_RANGE)
7390 *vr0max = vr1max;
7391 else if (*vr0type == VR_ANTI_RANGE
7392 && vr1type == VR_RANGE)
7394 if (TREE_CODE (vr1max) == INTEGER_CST)
7395 *vr0min = int_const_binop (PLUS_EXPR, vr1max, integer_one_node);
7396 else
7397 goto give_up;
7399 else if (*vr0type == VR_RANGE
7400 && vr1type == VR_ANTI_RANGE)
7402 if (TREE_CODE (*vr0min) == INTEGER_CST)
7404 *vr0type = vr1type;
7405 *vr0min = vr1min;
7406 *vr0max = int_const_binop (MINUS_EXPR, *vr0min, integer_one_node);
7408 else
7409 goto give_up;
7411 else
7412 gcc_unreachable ();
7414 else
7415 goto give_up;
7417 return;
7419 give_up:
7420 *vr0type = VR_VARYING;
7421 *vr0min = NULL_TREE;
7422 *vr0max = NULL_TREE;
7425 /* Intersect the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
7426 { VR1TYPE, VR0MIN, VR0MAX } and store the result
7427 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
7428 possible such range. The resulting range is not canonicalized. */
7430 static void
7431 intersect_ranges (enum value_range_type *vr0type,
7432 tree *vr0min, tree *vr0max,
7433 enum value_range_type vr1type,
7434 tree vr1min, tree vr1max)
7436 bool mineq = operand_equal_p (*vr0min, vr1min, 0);
7437 bool maxeq = operand_equal_p (*vr0max, vr1max, 0);
7439 /* [] is vr0, () is vr1 in the following classification comments. */
7440 if (mineq && maxeq)
7442 /* [( )] */
7443 if (*vr0type == vr1type)
7444 /* Nothing to do for equal ranges. */
7446 else if ((*vr0type == VR_RANGE
7447 && vr1type == VR_ANTI_RANGE)
7448 || (*vr0type == VR_ANTI_RANGE
7449 && vr1type == VR_RANGE))
7451 /* For anti-range with range intersection the result is empty. */
7452 *vr0type = VR_UNDEFINED;
7453 *vr0min = NULL_TREE;
7454 *vr0max = NULL_TREE;
7456 else
7457 gcc_unreachable ();
7459 else if (operand_less_p (*vr0max, vr1min) == 1
7460 || operand_less_p (vr1max, *vr0min) == 1)
7462 /* [ ] ( ) or ( ) [ ]
7463 If the ranges have an empty intersection, the result of the
7464 intersect operation is the range for intersecting an
7465 anti-range with a range or empty when intersecting two ranges. */
7466 if (*vr0type == VR_RANGE
7467 && vr1type == VR_ANTI_RANGE)
7469 else if (*vr0type == VR_ANTI_RANGE
7470 && vr1type == VR_RANGE)
7472 *vr0type = vr1type;
7473 *vr0min = vr1min;
7474 *vr0max = vr1max;
7476 else if (*vr0type == VR_RANGE
7477 && vr1type == VR_RANGE)
7479 *vr0type = VR_UNDEFINED;
7480 *vr0min = NULL_TREE;
7481 *vr0max = NULL_TREE;
7483 else if (*vr0type == VR_ANTI_RANGE
7484 && vr1type == VR_ANTI_RANGE)
7486 /* If the anti-ranges are adjacent to each other merge them. */
7487 if (TREE_CODE (*vr0max) == INTEGER_CST
7488 && TREE_CODE (vr1min) == INTEGER_CST
7489 && operand_less_p (*vr0max, vr1min) == 1
7490 && integer_onep (int_const_binop (MINUS_EXPR,
7491 vr1min, *vr0max)))
7492 *vr0max = vr1max;
7493 else if (TREE_CODE (vr1max) == INTEGER_CST
7494 && TREE_CODE (*vr0min) == INTEGER_CST
7495 && operand_less_p (vr1max, *vr0min) == 1
7496 && integer_onep (int_const_binop (MINUS_EXPR,
7497 *vr0min, vr1max)))
7498 *vr0min = vr1min;
7499 /* Else arbitrarily take VR0. */
7502 else if ((maxeq || operand_less_p (vr1max, *vr0max) == 1)
7503 && (mineq || operand_less_p (*vr0min, vr1min) == 1))
7505 /* [ ( ) ] or [( ) ] or [ ( )] */
7506 if (*vr0type == VR_RANGE
7507 && vr1type == VR_RANGE)
7509 /* If both are ranges the result is the inner one. */
7510 *vr0type = vr1type;
7511 *vr0min = vr1min;
7512 *vr0max = vr1max;
7514 else if (*vr0type == VR_RANGE
7515 && vr1type == VR_ANTI_RANGE)
7517 /* Choose the right gap if the left one is empty. */
7518 if (mineq)
7520 if (TREE_CODE (vr1max) == INTEGER_CST)
7521 *vr0min = int_const_binop (PLUS_EXPR, vr1max, integer_one_node);
7522 else
7523 *vr0min = vr1max;
7525 /* Choose the left gap if the right one is empty. */
7526 else if (maxeq)
7528 if (TREE_CODE (vr1min) == INTEGER_CST)
7529 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
7530 integer_one_node);
7531 else
7532 *vr0max = vr1min;
7534 /* Choose the anti-range if the range is effectively varying. */
7535 else if (vrp_val_is_min (*vr0min)
7536 && vrp_val_is_max (*vr0max))
7538 *vr0type = vr1type;
7539 *vr0min = vr1min;
7540 *vr0max = vr1max;
7542 /* Else choose the range. */
7544 else if (*vr0type == VR_ANTI_RANGE
7545 && vr1type == VR_ANTI_RANGE)
7546 /* If both are anti-ranges the result is the outer one. */
7548 else if (*vr0type == VR_ANTI_RANGE
7549 && vr1type == VR_RANGE)
7551 /* The intersection is empty. */
7552 *vr0type = VR_UNDEFINED;
7553 *vr0min = NULL_TREE;
7554 *vr0max = NULL_TREE;
7556 else
7557 gcc_unreachable ();
7559 else if ((maxeq || operand_less_p (*vr0max, vr1max) == 1)
7560 && (mineq || operand_less_p (vr1min, *vr0min) == 1))
7562 /* ( [ ] ) or ([ ] ) or ( [ ]) */
7563 if (*vr0type == VR_RANGE
7564 && vr1type == VR_RANGE)
7565 /* Choose the inner range. */
7567 else if (*vr0type == VR_ANTI_RANGE
7568 && vr1type == VR_RANGE)
7570 /* Choose the right gap if the left is empty. */
7571 if (mineq)
7573 *vr0type = VR_RANGE;
7574 if (TREE_CODE (*vr0max) == INTEGER_CST)
7575 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
7576 integer_one_node);
7577 else
7578 *vr0min = *vr0max;
7579 *vr0max = vr1max;
7581 /* Choose the left gap if the right is empty. */
7582 else if (maxeq)
7584 *vr0type = VR_RANGE;
7585 if (TREE_CODE (*vr0min) == INTEGER_CST)
7586 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
7587 integer_one_node);
7588 else
7589 *vr0max = *vr0min;
7590 *vr0min = vr1min;
7592 /* Choose the anti-range if the range is effectively varying. */
7593 else if (vrp_val_is_min (vr1min)
7594 && vrp_val_is_max (vr1max))
7596 /* Else choose the range. */
7597 else
7599 *vr0type = vr1type;
7600 *vr0min = vr1min;
7601 *vr0max = vr1max;
7604 else if (*vr0type == VR_ANTI_RANGE
7605 && vr1type == VR_ANTI_RANGE)
7607 /* If both are anti-ranges the result is the outer one. */
7608 *vr0type = vr1type;
7609 *vr0min = vr1min;
7610 *vr0max = vr1max;
7612 else if (vr1type == VR_ANTI_RANGE
7613 && *vr0type == VR_RANGE)
7615 /* The intersection is empty. */
7616 *vr0type = VR_UNDEFINED;
7617 *vr0min = NULL_TREE;
7618 *vr0max = NULL_TREE;
7620 else
7621 gcc_unreachable ();
7623 else if ((operand_less_p (vr1min, *vr0max) == 1
7624 || operand_equal_p (vr1min, *vr0max, 0))
7625 && operand_less_p (*vr0min, vr1min) == 1)
7627 /* [ ( ] ) or [ ]( ) */
7628 if (*vr0type == VR_ANTI_RANGE
7629 && vr1type == VR_ANTI_RANGE)
7630 *vr0max = vr1max;
7631 else if (*vr0type == VR_RANGE
7632 && vr1type == VR_RANGE)
7633 *vr0min = vr1min;
7634 else if (*vr0type == VR_RANGE
7635 && vr1type == VR_ANTI_RANGE)
7637 if (TREE_CODE (vr1min) == INTEGER_CST)
7638 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
7639 integer_one_node);
7640 else
7641 *vr0max = vr1min;
7643 else if (*vr0type == VR_ANTI_RANGE
7644 && vr1type == VR_RANGE)
7646 *vr0type = VR_RANGE;
7647 if (TREE_CODE (*vr0max) == INTEGER_CST)
7648 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
7649 integer_one_node);
7650 else
7651 *vr0min = *vr0max;
7652 *vr0max = vr1max;
7654 else
7655 gcc_unreachable ();
7657 else if ((operand_less_p (*vr0min, vr1max) == 1
7658 || operand_equal_p (*vr0min, vr1max, 0))
7659 && operand_less_p (vr1min, *vr0min) == 1)
7661 /* ( [ ) ] or ( )[ ] */
7662 if (*vr0type == VR_ANTI_RANGE
7663 && vr1type == VR_ANTI_RANGE)
7664 *vr0min = vr1min;
7665 else if (*vr0type == VR_RANGE
7666 && vr1type == VR_RANGE)
7667 *vr0max = vr1max;
7668 else if (*vr0type == VR_RANGE
7669 && vr1type == VR_ANTI_RANGE)
7671 if (TREE_CODE (vr1max) == INTEGER_CST)
7672 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
7673 integer_one_node);
7674 else
7675 *vr0min = vr1max;
7677 else if (*vr0type == VR_ANTI_RANGE
7678 && vr1type == VR_RANGE)
7680 *vr0type = VR_RANGE;
7681 if (TREE_CODE (*vr0min) == INTEGER_CST)
7682 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
7683 integer_one_node);
7684 else
7685 *vr0max = *vr0min;
7686 *vr0min = vr1min;
7688 else
7689 gcc_unreachable ();
7692 /* As a fallback simply use { *VRTYPE, *VR0MIN, *VR0MAX } as
7693 result for the intersection. That's always a conservative
7694 correct estimate. */
7696 return;
7700 /* Intersect the two value-ranges *VR0 and *VR1 and store the result
7701 in *VR0. This may not be the smallest possible such range. */
7703 static void
7704 vrp_intersect_ranges_1 (value_range_t *vr0, value_range_t *vr1)
7706 value_range_t saved;
7708 /* If either range is VR_VARYING the other one wins. */
7709 if (vr1->type == VR_VARYING)
7710 return;
7711 if (vr0->type == VR_VARYING)
7713 copy_value_range (vr0, vr1);
7714 return;
7717 /* When either range is VR_UNDEFINED the resulting range is
7718 VR_UNDEFINED, too. */
7719 if (vr0->type == VR_UNDEFINED)
7720 return;
7721 if (vr1->type == VR_UNDEFINED)
7723 set_value_range_to_undefined (vr0);
7724 return;
7727 /* Save the original vr0 so we can return it as conservative intersection
7728 result when our worker turns things to varying. */
7729 saved = *vr0;
7730 intersect_ranges (&vr0->type, &vr0->min, &vr0->max,
7731 vr1->type, vr1->min, vr1->max);
7732 /* Make sure to canonicalize the result though as the inversion of a
7733 VR_RANGE can still be a VR_RANGE. */
7734 set_and_canonicalize_value_range (vr0, vr0->type,
7735 vr0->min, vr0->max, vr0->equiv);
7736 /* If that failed, use the saved original VR0. */
7737 if (vr0->type == VR_VARYING)
7739 *vr0 = saved;
7740 return;
7742 /* If the result is VR_UNDEFINED there is no need to mess with
7743 the equivalencies. */
7744 if (vr0->type == VR_UNDEFINED)
7745 return;
7747 /* The resulting set of equivalences for range intersection is the union of
7748 the two sets. */
7749 if (vr0->equiv && vr1->equiv && vr0->equiv != vr1->equiv)
7750 bitmap_ior_into (vr0->equiv, vr1->equiv);
7751 else if (vr1->equiv && !vr0->equiv)
7752 bitmap_copy (vr0->equiv, vr1->equiv);
7755 static void
7756 vrp_intersect_ranges (value_range_t *vr0, value_range_t *vr1)
7758 if (dump_file && (dump_flags & TDF_DETAILS))
7760 fprintf (dump_file, "Intersecting\n ");
7761 dump_value_range (dump_file, vr0);
7762 fprintf (dump_file, "\nand\n ");
7763 dump_value_range (dump_file, vr1);
7764 fprintf (dump_file, "\n");
7766 vrp_intersect_ranges_1 (vr0, vr1);
7767 if (dump_file && (dump_flags & TDF_DETAILS))
7769 fprintf (dump_file, "to\n ");
7770 dump_value_range (dump_file, vr0);
7771 fprintf (dump_file, "\n");
7775 /* Meet operation for value ranges. Given two value ranges VR0 and
7776 VR1, store in VR0 a range that contains both VR0 and VR1. This
7777 may not be the smallest possible such range. */
7779 static void
7780 vrp_meet_1 (value_range_t *vr0, value_range_t *vr1)
7782 value_range_t saved;
7784 if (vr0->type == VR_UNDEFINED)
7786 /* Drop equivalences. See PR53465. */
7787 set_value_range (vr0, vr1->type, vr1->min, vr1->max, NULL);
7788 return;
7791 if (vr1->type == VR_UNDEFINED)
7793 /* VR0 already has the resulting range, just drop equivalences.
7794 See PR53465. */
7795 if (vr0->equiv)
7796 bitmap_clear (vr0->equiv);
7797 return;
7800 if (vr0->type == VR_VARYING)
7802 /* Nothing to do. VR0 already has the resulting range. */
7803 return;
7806 if (vr1->type == VR_VARYING)
7808 set_value_range_to_varying (vr0);
7809 return;
7812 saved = *vr0;
7813 union_ranges (&vr0->type, &vr0->min, &vr0->max,
7814 vr1->type, vr1->min, vr1->max);
7815 if (vr0->type == VR_VARYING)
7817 /* Failed to find an efficient meet. Before giving up and setting
7818 the result to VARYING, see if we can at least derive a useful
7819 anti-range. FIXME, all this nonsense about distinguishing
7820 anti-ranges from ranges is necessary because of the odd
7821 semantics of range_includes_zero_p and friends. */
7822 if (((saved.type == VR_RANGE
7823 && range_includes_zero_p (saved.min, saved.max) == 0)
7824 || (saved.type == VR_ANTI_RANGE
7825 && range_includes_zero_p (saved.min, saved.max) == 1))
7826 && ((vr1->type == VR_RANGE
7827 && range_includes_zero_p (vr1->min, vr1->max) == 0)
7828 || (vr1->type == VR_ANTI_RANGE
7829 && range_includes_zero_p (vr1->min, vr1->max) == 1)))
7831 set_value_range_to_nonnull (vr0, TREE_TYPE (saved.min));
7833 /* Since this meet operation did not result from the meeting of
7834 two equivalent names, VR0 cannot have any equivalences. */
7835 if (vr0->equiv)
7836 bitmap_clear (vr0->equiv);
7837 return;
7840 set_value_range_to_varying (vr0);
7841 return;
7843 set_and_canonicalize_value_range (vr0, vr0->type, vr0->min, vr0->max,
7844 vr0->equiv);
7845 if (vr0->type == VR_VARYING)
7846 return;
7848 /* The resulting set of equivalences is always the intersection of
7849 the two sets. */
7850 if (vr0->equiv && vr1->equiv && vr0->equiv != vr1->equiv)
7851 bitmap_and_into (vr0->equiv, vr1->equiv);
7852 else if (vr0->equiv && !vr1->equiv)
7853 bitmap_clear (vr0->equiv);
7856 static void
7857 vrp_meet (value_range_t *vr0, value_range_t *vr1)
7859 if (dump_file && (dump_flags & TDF_DETAILS))
7861 fprintf (dump_file, "Meeting\n ");
7862 dump_value_range (dump_file, vr0);
7863 fprintf (dump_file, "\nand\n ");
7864 dump_value_range (dump_file, vr1);
7865 fprintf (dump_file, "\n");
7867 vrp_meet_1 (vr0, vr1);
7868 if (dump_file && (dump_flags & TDF_DETAILS))
7870 fprintf (dump_file, "to\n ");
7871 dump_value_range (dump_file, vr0);
7872 fprintf (dump_file, "\n");
7877 /* Visit all arguments for PHI node PHI that flow through executable
7878 edges. If a valid value range can be derived from all the incoming
7879 value ranges, set a new range for the LHS of PHI. */
7881 static enum ssa_prop_result
7882 vrp_visit_phi_node (gimple phi)
7884 size_t i;
7885 tree lhs = PHI_RESULT (phi);
7886 value_range_t *lhs_vr = get_value_range (lhs);
7887 value_range_t vr_result = VR_INITIALIZER;
7888 bool first = true;
7889 int edges, old_edges;
7890 struct loop *l;
7892 if (dump_file && (dump_flags & TDF_DETAILS))
7894 fprintf (dump_file, "\nVisiting PHI node: ");
7895 print_gimple_stmt (dump_file, phi, 0, dump_flags);
7898 edges = 0;
7899 for (i = 0; i < gimple_phi_num_args (phi); i++)
7901 edge e = gimple_phi_arg_edge (phi, i);
7903 if (dump_file && (dump_flags & TDF_DETAILS))
7905 fprintf (dump_file,
7906 "\n Argument #%d (%d -> %d %sexecutable)\n",
7907 (int) i, e->src->index, e->dest->index,
7908 (e->flags & EDGE_EXECUTABLE) ? "" : "not ");
7911 if (e->flags & EDGE_EXECUTABLE)
7913 tree arg = PHI_ARG_DEF (phi, i);
7914 value_range_t vr_arg;
7916 ++edges;
7918 if (TREE_CODE (arg) == SSA_NAME)
7920 vr_arg = *(get_value_range (arg));
7922 else
7924 if (is_overflow_infinity (arg))
7926 arg = copy_node (arg);
7927 TREE_OVERFLOW (arg) = 0;
7930 vr_arg.type = VR_RANGE;
7931 vr_arg.min = arg;
7932 vr_arg.max = arg;
7933 vr_arg.equiv = NULL;
7936 if (dump_file && (dump_flags & TDF_DETAILS))
7938 fprintf (dump_file, "\t");
7939 print_generic_expr (dump_file, arg, dump_flags);
7940 fprintf (dump_file, "\n\tValue: ");
7941 dump_value_range (dump_file, &vr_arg);
7942 fprintf (dump_file, "\n");
7945 if (first)
7946 copy_value_range (&vr_result, &vr_arg);
7947 else
7948 vrp_meet (&vr_result, &vr_arg);
7949 first = false;
7951 if (vr_result.type == VR_VARYING)
7952 break;
7956 if (vr_result.type == VR_VARYING)
7957 goto varying;
7958 else if (vr_result.type == VR_UNDEFINED)
7959 goto update_range;
7961 old_edges = vr_phi_edge_counts[SSA_NAME_VERSION (lhs)];
7962 vr_phi_edge_counts[SSA_NAME_VERSION (lhs)] = edges;
7964 /* To prevent infinite iterations in the algorithm, derive ranges
7965 when the new value is slightly bigger or smaller than the
7966 previous one. We don't do this if we have seen a new executable
7967 edge; this helps us avoid an overflow infinity for conditionals
7968 which are not in a loop. If the old value-range was VR_UNDEFINED
7969 use the updated range and iterate one more time. */
7970 if (edges > 0
7971 && gimple_phi_num_args (phi) > 1
7972 && edges == old_edges
7973 && lhs_vr->type != VR_UNDEFINED)
7975 int cmp_min = compare_values (lhs_vr->min, vr_result.min);
7976 int cmp_max = compare_values (lhs_vr->max, vr_result.max);
7978 /* For non VR_RANGE or for pointers fall back to varying if
7979 the range changed. */
7980 if ((lhs_vr->type != VR_RANGE || vr_result.type != VR_RANGE
7981 || POINTER_TYPE_P (TREE_TYPE (lhs)))
7982 && (cmp_min != 0 || cmp_max != 0))
7983 goto varying;
7985 /* If the new minimum is smaller or larger than the previous
7986 one, go all the way to -INF. In the first case, to avoid
7987 iterating millions of times to reach -INF, and in the
7988 other case to avoid infinite bouncing between different
7989 minimums. */
7990 if (cmp_min > 0 || cmp_min < 0)
7992 if (!needs_overflow_infinity (TREE_TYPE (vr_result.min))
7993 || !vrp_var_may_overflow (lhs, phi))
7994 vr_result.min = TYPE_MIN_VALUE (TREE_TYPE (vr_result.min));
7995 else if (supports_overflow_infinity (TREE_TYPE (vr_result.min)))
7996 vr_result.min =
7997 negative_overflow_infinity (TREE_TYPE (vr_result.min));
8000 /* Similarly, if the new maximum is smaller or larger than
8001 the previous one, go all the way to +INF. */
8002 if (cmp_max < 0 || cmp_max > 0)
8004 if (!needs_overflow_infinity (TREE_TYPE (vr_result.max))
8005 || !vrp_var_may_overflow (lhs, phi))
8006 vr_result.max = TYPE_MAX_VALUE (TREE_TYPE (vr_result.max));
8007 else if (supports_overflow_infinity (TREE_TYPE (vr_result.max)))
8008 vr_result.max =
8009 positive_overflow_infinity (TREE_TYPE (vr_result.max));
8012 /* If we dropped either bound to +-INF then if this is a loop
8013 PHI node SCEV may known more about its value-range. */
8014 if ((cmp_min > 0 || cmp_min < 0
8015 || cmp_max < 0 || cmp_max > 0)
8016 && current_loops
8017 && (l = loop_containing_stmt (phi))
8018 && l->header == gimple_bb (phi))
8019 adjust_range_with_scev (&vr_result, l, phi, lhs);
8021 /* If we will end up with a (-INF, +INF) range, set it to
8022 VARYING. Same if the previous max value was invalid for
8023 the type and we end up with vr_result.min > vr_result.max. */
8024 if ((vrp_val_is_max (vr_result.max)
8025 && vrp_val_is_min (vr_result.min))
8026 || compare_values (vr_result.min,
8027 vr_result.max) > 0)
8028 goto varying;
8031 /* If the new range is different than the previous value, keep
8032 iterating. */
8033 update_range:
8034 if (update_value_range (lhs, &vr_result))
8036 if (dump_file && (dump_flags & TDF_DETAILS))
8038 fprintf (dump_file, "Found new range for ");
8039 print_generic_expr (dump_file, lhs, 0);
8040 fprintf (dump_file, ": ");
8041 dump_value_range (dump_file, &vr_result);
8042 fprintf (dump_file, "\n\n");
8045 return SSA_PROP_INTERESTING;
8048 /* Nothing changed, don't add outgoing edges. */
8049 return SSA_PROP_NOT_INTERESTING;
8051 /* No match found. Set the LHS to VARYING. */
8052 varying:
8053 set_value_range_to_varying (lhs_vr);
8054 return SSA_PROP_VARYING;
8057 /* Simplify boolean operations if the source is known
8058 to be already a boolean. */
8059 static bool
8060 simplify_truth_ops_using_ranges (gimple_stmt_iterator *gsi, gimple stmt)
8062 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
8063 tree lhs, op0, op1;
8064 bool need_conversion;
8066 /* We handle only !=/== case here. */
8067 gcc_assert (rhs_code == EQ_EXPR || rhs_code == NE_EXPR);
8069 op0 = gimple_assign_rhs1 (stmt);
8070 if (!op_with_boolean_value_range_p (op0))
8071 return false;
8073 op1 = gimple_assign_rhs2 (stmt);
8074 if (!op_with_boolean_value_range_p (op1))
8075 return false;
8077 /* Reduce number of cases to handle to NE_EXPR. As there is no
8078 BIT_XNOR_EXPR we cannot replace A == B with a single statement. */
8079 if (rhs_code == EQ_EXPR)
8081 if (TREE_CODE (op1) == INTEGER_CST)
8082 op1 = int_const_binop (BIT_XOR_EXPR, op1, integer_one_node);
8083 else
8084 return false;
8087 lhs = gimple_assign_lhs (stmt);
8088 need_conversion
8089 = !useless_type_conversion_p (TREE_TYPE (lhs), TREE_TYPE (op0));
8091 /* Make sure to not sign-extend a 1-bit 1 when converting the result. */
8092 if (need_conversion
8093 && !TYPE_UNSIGNED (TREE_TYPE (op0))
8094 && TYPE_PRECISION (TREE_TYPE (op0)) == 1
8095 && TYPE_PRECISION (TREE_TYPE (lhs)) > 1)
8096 return false;
8098 /* For A != 0 we can substitute A itself. */
8099 if (integer_zerop (op1))
8100 gimple_assign_set_rhs_with_ops (gsi,
8101 need_conversion
8102 ? NOP_EXPR : TREE_CODE (op0),
8103 op0, NULL_TREE);
8104 /* For A != B we substitute A ^ B. Either with conversion. */
8105 else if (need_conversion)
8107 tree tem = make_ssa_name (TREE_TYPE (op0), NULL);
8108 gimple newop = gimple_build_assign_with_ops (BIT_XOR_EXPR, tem, op0, op1);
8109 gsi_insert_before (gsi, newop, GSI_SAME_STMT);
8110 gimple_assign_set_rhs_with_ops (gsi, NOP_EXPR, tem, NULL_TREE);
8112 /* Or without. */
8113 else
8114 gimple_assign_set_rhs_with_ops (gsi, BIT_XOR_EXPR, op0, op1);
8115 update_stmt (gsi_stmt (*gsi));
8117 return true;
8120 /* Simplify a division or modulo operator to a right shift or
8121 bitwise and if the first operand is unsigned or is greater
8122 than zero and the second operand is an exact power of two. */
8124 static bool
8125 simplify_div_or_mod_using_ranges (gimple stmt)
8127 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
8128 tree val = NULL;
8129 tree op0 = gimple_assign_rhs1 (stmt);
8130 tree op1 = gimple_assign_rhs2 (stmt);
8131 value_range_t *vr = get_value_range (gimple_assign_rhs1 (stmt));
8133 if (TYPE_UNSIGNED (TREE_TYPE (op0)))
8135 val = integer_one_node;
8137 else
8139 bool sop = false;
8141 val = compare_range_with_value (GE_EXPR, vr, integer_zero_node, &sop);
8143 if (val
8144 && sop
8145 && integer_onep (val)
8146 && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC))
8148 location_t location;
8150 if (!gimple_has_location (stmt))
8151 location = input_location;
8152 else
8153 location = gimple_location (stmt);
8154 warning_at (location, OPT_Wstrict_overflow,
8155 "assuming signed overflow does not occur when "
8156 "simplifying %</%> or %<%%%> to %<>>%> or %<&%>");
8160 if (val && integer_onep (val))
8162 tree t;
8164 if (rhs_code == TRUNC_DIV_EXPR)
8166 t = build_int_cst (integer_type_node, tree_log2 (op1));
8167 gimple_assign_set_rhs_code (stmt, RSHIFT_EXPR);
8168 gimple_assign_set_rhs1 (stmt, op0);
8169 gimple_assign_set_rhs2 (stmt, t);
8171 else
8173 t = build_int_cst (TREE_TYPE (op1), 1);
8174 t = int_const_binop (MINUS_EXPR, op1, t);
8175 t = fold_convert (TREE_TYPE (op0), t);
8177 gimple_assign_set_rhs_code (stmt, BIT_AND_EXPR);
8178 gimple_assign_set_rhs1 (stmt, op0);
8179 gimple_assign_set_rhs2 (stmt, t);
8182 update_stmt (stmt);
8183 return true;
8186 return false;
8189 /* If the operand to an ABS_EXPR is >= 0, then eliminate the
8190 ABS_EXPR. If the operand is <= 0, then simplify the
8191 ABS_EXPR into a NEGATE_EXPR. */
8193 static bool
8194 simplify_abs_using_ranges (gimple stmt)
8196 tree val = NULL;
8197 tree op = gimple_assign_rhs1 (stmt);
8198 tree type = TREE_TYPE (op);
8199 value_range_t *vr = get_value_range (op);
8201 if (TYPE_UNSIGNED (type))
8203 val = integer_zero_node;
8205 else if (vr)
8207 bool sop = false;
8209 val = compare_range_with_value (LE_EXPR, vr, integer_zero_node, &sop);
8210 if (!val)
8212 sop = false;
8213 val = compare_range_with_value (GE_EXPR, vr, integer_zero_node,
8214 &sop);
8216 if (val)
8218 if (integer_zerop (val))
8219 val = integer_one_node;
8220 else if (integer_onep (val))
8221 val = integer_zero_node;
8225 if (val
8226 && (integer_onep (val) || integer_zerop (val)))
8228 if (sop && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC))
8230 location_t location;
8232 if (!gimple_has_location (stmt))
8233 location = input_location;
8234 else
8235 location = gimple_location (stmt);
8236 warning_at (location, OPT_Wstrict_overflow,
8237 "assuming signed overflow does not occur when "
8238 "simplifying %<abs (X)%> to %<X%> or %<-X%>");
8241 gimple_assign_set_rhs1 (stmt, op);
8242 if (integer_onep (val))
8243 gimple_assign_set_rhs_code (stmt, NEGATE_EXPR);
8244 else
8245 gimple_assign_set_rhs_code (stmt, SSA_NAME);
8246 update_stmt (stmt);
8247 return true;
8251 return false;
8254 /* Optimize away redundant BIT_AND_EXPR and BIT_IOR_EXPR.
8255 If all the bits that are being cleared by & are already
8256 known to be zero from VR, or all the bits that are being
8257 set by | are already known to be one from VR, the bit
8258 operation is redundant. */
8260 static bool
8261 simplify_bit_ops_using_ranges (gimple_stmt_iterator *gsi, gimple stmt)
8263 tree op0 = gimple_assign_rhs1 (stmt);
8264 tree op1 = gimple_assign_rhs2 (stmt);
8265 tree op = NULL_TREE;
8266 value_range_t vr0 = VR_INITIALIZER;
8267 value_range_t vr1 = VR_INITIALIZER;
8268 double_int may_be_nonzero0, may_be_nonzero1;
8269 double_int must_be_nonzero0, must_be_nonzero1;
8270 double_int mask;
8272 if (TREE_CODE (op0) == SSA_NAME)
8273 vr0 = *(get_value_range (op0));
8274 else if (is_gimple_min_invariant (op0))
8275 set_value_range_to_value (&vr0, op0, NULL);
8276 else
8277 return false;
8279 if (TREE_CODE (op1) == SSA_NAME)
8280 vr1 = *(get_value_range (op1));
8281 else if (is_gimple_min_invariant (op1))
8282 set_value_range_to_value (&vr1, op1, NULL);
8283 else
8284 return false;
8286 if (!zero_nonzero_bits_from_vr (&vr0, &may_be_nonzero0, &must_be_nonzero0))
8287 return false;
8288 if (!zero_nonzero_bits_from_vr (&vr1, &may_be_nonzero1, &must_be_nonzero1))
8289 return false;
8291 switch (gimple_assign_rhs_code (stmt))
8293 case BIT_AND_EXPR:
8294 mask = may_be_nonzero0.and_not (must_be_nonzero1);
8295 if (mask.is_zero ())
8297 op = op0;
8298 break;
8300 mask = may_be_nonzero1.and_not (must_be_nonzero0);
8301 if (mask.is_zero ())
8303 op = op1;
8304 break;
8306 break;
8307 case BIT_IOR_EXPR:
8308 mask = may_be_nonzero0.and_not (must_be_nonzero1);
8309 if (mask.is_zero ())
8311 op = op1;
8312 break;
8314 mask = may_be_nonzero1.and_not (must_be_nonzero0);
8315 if (mask.is_zero ())
8317 op = op0;
8318 break;
8320 break;
8321 default:
8322 gcc_unreachable ();
8325 if (op == NULL_TREE)
8326 return false;
8328 gimple_assign_set_rhs_with_ops (gsi, TREE_CODE (op), op, NULL);
8329 update_stmt (gsi_stmt (*gsi));
8330 return true;
8333 /* We are comparing trees OP0 and OP1 using COND_CODE. OP0 has
8334 a known value range VR.
8336 If there is one and only one value which will satisfy the
8337 conditional, then return that value. Else return NULL. */
8339 static tree
8340 test_for_singularity (enum tree_code cond_code, tree op0,
8341 tree op1, value_range_t *vr)
8343 tree min = NULL;
8344 tree max = NULL;
8346 /* Extract minimum/maximum values which satisfy the
8347 the conditional as it was written. */
8348 if (cond_code == LE_EXPR || cond_code == LT_EXPR)
8350 /* This should not be negative infinity; there is no overflow
8351 here. */
8352 min = TYPE_MIN_VALUE (TREE_TYPE (op0));
8354 max = op1;
8355 if (cond_code == LT_EXPR && !is_overflow_infinity (max))
8357 tree one = build_int_cst (TREE_TYPE (op0), 1);
8358 max = fold_build2 (MINUS_EXPR, TREE_TYPE (op0), max, one);
8359 if (EXPR_P (max))
8360 TREE_NO_WARNING (max) = 1;
8363 else if (cond_code == GE_EXPR || cond_code == GT_EXPR)
8365 /* This should not be positive infinity; there is no overflow
8366 here. */
8367 max = TYPE_MAX_VALUE (TREE_TYPE (op0));
8369 min = op1;
8370 if (cond_code == GT_EXPR && !is_overflow_infinity (min))
8372 tree one = build_int_cst (TREE_TYPE (op0), 1);
8373 min = fold_build2 (PLUS_EXPR, TREE_TYPE (op0), min, one);
8374 if (EXPR_P (min))
8375 TREE_NO_WARNING (min) = 1;
8379 /* Now refine the minimum and maximum values using any
8380 value range information we have for op0. */
8381 if (min && max)
8383 if (compare_values (vr->min, min) == 1)
8384 min = vr->min;
8385 if (compare_values (vr->max, max) == -1)
8386 max = vr->max;
8388 /* If the new min/max values have converged to a single value,
8389 then there is only one value which can satisfy the condition,
8390 return that value. */
8391 if (operand_equal_p (min, max, 0) && is_gimple_min_invariant (min))
8392 return min;
8394 return NULL;
8397 /* Simplify a conditional using a relational operator to an equality
8398 test if the range information indicates only one value can satisfy
8399 the original conditional. */
8401 static bool
8402 simplify_cond_using_ranges (gimple stmt)
8404 tree op0 = gimple_cond_lhs (stmt);
8405 tree op1 = gimple_cond_rhs (stmt);
8406 enum tree_code cond_code = gimple_cond_code (stmt);
8408 if (cond_code != NE_EXPR
8409 && cond_code != EQ_EXPR
8410 && TREE_CODE (op0) == SSA_NAME
8411 && INTEGRAL_TYPE_P (TREE_TYPE (op0))
8412 && is_gimple_min_invariant (op1))
8414 value_range_t *vr = get_value_range (op0);
8416 /* If we have range information for OP0, then we might be
8417 able to simplify this conditional. */
8418 if (vr->type == VR_RANGE)
8420 tree new_tree = test_for_singularity (cond_code, op0, op1, vr);
8422 if (new_tree)
8424 if (dump_file)
8426 fprintf (dump_file, "Simplified relational ");
8427 print_gimple_stmt (dump_file, stmt, 0, 0);
8428 fprintf (dump_file, " into ");
8431 gimple_cond_set_code (stmt, EQ_EXPR);
8432 gimple_cond_set_lhs (stmt, op0);
8433 gimple_cond_set_rhs (stmt, new_tree);
8435 update_stmt (stmt);
8437 if (dump_file)
8439 print_gimple_stmt (dump_file, stmt, 0, 0);
8440 fprintf (dump_file, "\n");
8443 return true;
8446 /* Try again after inverting the condition. We only deal
8447 with integral types here, so no need to worry about
8448 issues with inverting FP comparisons. */
8449 cond_code = invert_tree_comparison (cond_code, false);
8450 new_tree = test_for_singularity (cond_code, op0, op1, vr);
8452 if (new_tree)
8454 if (dump_file)
8456 fprintf (dump_file, "Simplified relational ");
8457 print_gimple_stmt (dump_file, stmt, 0, 0);
8458 fprintf (dump_file, " into ");
8461 gimple_cond_set_code (stmt, NE_EXPR);
8462 gimple_cond_set_lhs (stmt, op0);
8463 gimple_cond_set_rhs (stmt, new_tree);
8465 update_stmt (stmt);
8467 if (dump_file)
8469 print_gimple_stmt (dump_file, stmt, 0, 0);
8470 fprintf (dump_file, "\n");
8473 return true;
8478 return false;
8481 /* Simplify a switch statement using the value range of the switch
8482 argument. */
8484 static bool
8485 simplify_switch_using_ranges (gimple stmt)
8487 tree op = gimple_switch_index (stmt);
8488 value_range_t *vr;
8489 bool take_default;
8490 edge e;
8491 edge_iterator ei;
8492 size_t i = 0, j = 0, n, n2;
8493 tree vec2;
8494 switch_update su;
8495 size_t k = 1, l = 0;
8497 if (TREE_CODE (op) == SSA_NAME)
8499 vr = get_value_range (op);
8501 /* We can only handle integer ranges. */
8502 if ((vr->type != VR_RANGE
8503 && vr->type != VR_ANTI_RANGE)
8504 || symbolic_range_p (vr))
8505 return false;
8507 /* Find case label for min/max of the value range. */
8508 take_default = !find_case_label_ranges (stmt, vr, &i, &j, &k, &l);
8510 else if (TREE_CODE (op) == INTEGER_CST)
8512 take_default = !find_case_label_index (stmt, 1, op, &i);
8513 if (take_default)
8515 i = 1;
8516 j = 0;
8518 else
8520 j = i;
8523 else
8524 return false;
8526 n = gimple_switch_num_labels (stmt);
8528 /* Bail out if this is just all edges taken. */
8529 if (i == 1
8530 && j == n - 1
8531 && take_default)
8532 return false;
8534 /* Build a new vector of taken case labels. */
8535 vec2 = make_tree_vec (j - i + 1 + l - k + 1 + (int)take_default);
8536 n2 = 0;
8538 /* Add the default edge, if necessary. */
8539 if (take_default)
8540 TREE_VEC_ELT (vec2, n2++) = gimple_switch_default_label (stmt);
8542 for (; i <= j; ++i, ++n2)
8543 TREE_VEC_ELT (vec2, n2) = gimple_switch_label (stmt, i);
8545 for (; k <= l; ++k, ++n2)
8546 TREE_VEC_ELT (vec2, n2) = gimple_switch_label (stmt, k);
8548 /* Mark needed edges. */
8549 for (i = 0; i < n2; ++i)
8551 e = find_edge (gimple_bb (stmt),
8552 label_to_block (CASE_LABEL (TREE_VEC_ELT (vec2, i))));
8553 e->aux = (void *)-1;
8556 /* Queue not needed edges for later removal. */
8557 FOR_EACH_EDGE (e, ei, gimple_bb (stmt)->succs)
8559 if (e->aux == (void *)-1)
8561 e->aux = NULL;
8562 continue;
8565 if (dump_file && (dump_flags & TDF_DETAILS))
8567 fprintf (dump_file, "removing unreachable case label\n");
8569 to_remove_edges.safe_push (e);
8570 e->flags &= ~EDGE_EXECUTABLE;
8573 /* And queue an update for the stmt. */
8574 su.stmt = stmt;
8575 su.vec = vec2;
8576 to_update_switch_stmts.safe_push (su);
8577 return false;
8580 /* Simplify an integral conversion from an SSA name in STMT. */
8582 static bool
8583 simplify_conversion_using_ranges (gimple stmt)
8585 tree innerop, middleop, finaltype;
8586 gimple def_stmt;
8587 value_range_t *innervr;
8588 bool inner_unsigned_p, middle_unsigned_p, final_unsigned_p;
8589 unsigned inner_prec, middle_prec, final_prec;
8590 double_int innermin, innermed, innermax, middlemin, middlemed, middlemax;
8592 finaltype = TREE_TYPE (gimple_assign_lhs (stmt));
8593 if (!INTEGRAL_TYPE_P (finaltype))
8594 return false;
8595 middleop = gimple_assign_rhs1 (stmt);
8596 def_stmt = SSA_NAME_DEF_STMT (middleop);
8597 if (!is_gimple_assign (def_stmt)
8598 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt)))
8599 return false;
8600 innerop = gimple_assign_rhs1 (def_stmt);
8601 if (TREE_CODE (innerop) != SSA_NAME)
8602 return false;
8604 /* Get the value-range of the inner operand. */
8605 innervr = get_value_range (innerop);
8606 if (innervr->type != VR_RANGE
8607 || TREE_CODE (innervr->min) != INTEGER_CST
8608 || TREE_CODE (innervr->max) != INTEGER_CST)
8609 return false;
8611 /* Simulate the conversion chain to check if the result is equal if
8612 the middle conversion is removed. */
8613 innermin = tree_to_double_int (innervr->min);
8614 innermax = tree_to_double_int (innervr->max);
8616 inner_prec = TYPE_PRECISION (TREE_TYPE (innerop));
8617 middle_prec = TYPE_PRECISION (TREE_TYPE (middleop));
8618 final_prec = TYPE_PRECISION (finaltype);
8620 /* If the first conversion is not injective, the second must not
8621 be widening. */
8622 if ((innermax - innermin).ugt (double_int::mask (middle_prec))
8623 && middle_prec < final_prec)
8624 return false;
8625 /* We also want a medium value so that we can track the effect that
8626 narrowing conversions with sign change have. */
8627 inner_unsigned_p = TYPE_UNSIGNED (TREE_TYPE (innerop));
8628 if (inner_unsigned_p)
8629 innermed = double_int::mask (inner_prec).lrshift (1, inner_prec);
8630 else
8631 innermed = double_int_zero;
8632 if (innermin.cmp (innermed, inner_unsigned_p) >= 0
8633 || innermed.cmp (innermax, inner_unsigned_p) >= 0)
8634 innermed = innermin;
8636 middle_unsigned_p = TYPE_UNSIGNED (TREE_TYPE (middleop));
8637 middlemin = innermin.ext (middle_prec, middle_unsigned_p);
8638 middlemed = innermed.ext (middle_prec, middle_unsigned_p);
8639 middlemax = innermax.ext (middle_prec, middle_unsigned_p);
8641 /* Require that the final conversion applied to both the original
8642 and the intermediate range produces the same result. */
8643 final_unsigned_p = TYPE_UNSIGNED (finaltype);
8644 if (middlemin.ext (final_prec, final_unsigned_p)
8645 != innermin.ext (final_prec, final_unsigned_p)
8646 || middlemed.ext (final_prec, final_unsigned_p)
8647 != innermed.ext (final_prec, final_unsigned_p)
8648 || middlemax.ext (final_prec, final_unsigned_p)
8649 != innermax.ext (final_prec, final_unsigned_p))
8650 return false;
8652 gimple_assign_set_rhs1 (stmt, innerop);
8653 update_stmt (stmt);
8654 return true;
8657 /* Return whether the value range *VR fits in an integer type specified
8658 by PRECISION and UNSIGNED_P. */
8660 static bool
8661 range_fits_type_p (value_range_t *vr, unsigned precision, bool unsigned_p)
8663 tree src_type;
8664 unsigned src_precision;
8665 double_int tem;
8667 /* We can only handle integral and pointer types. */
8668 src_type = TREE_TYPE (vr->min);
8669 if (!INTEGRAL_TYPE_P (src_type)
8670 && !POINTER_TYPE_P (src_type))
8671 return false;
8673 /* An extension is always fine, so is an identity transform. */
8674 src_precision = TYPE_PRECISION (TREE_TYPE (vr->min));
8675 if (src_precision < precision
8676 || (src_precision == precision
8677 && TYPE_UNSIGNED (src_type) == unsigned_p))
8678 return true;
8680 /* Now we can only handle ranges with constant bounds. */
8681 if (vr->type != VR_RANGE
8682 || TREE_CODE (vr->min) != INTEGER_CST
8683 || TREE_CODE (vr->max) != INTEGER_CST)
8684 return false;
8686 /* For precision-preserving sign-changes the MSB of the double-int
8687 has to be clear. */
8688 if (src_precision == precision
8689 && (TREE_INT_CST_HIGH (vr->min) | TREE_INT_CST_HIGH (vr->max)) < 0)
8690 return false;
8692 /* Then we can perform the conversion on both ends and compare
8693 the result for equality. */
8694 tem = tree_to_double_int (vr->min).ext (precision, unsigned_p);
8695 if (tree_to_double_int (vr->min) != tem)
8696 return false;
8697 tem = tree_to_double_int (vr->max).ext (precision, unsigned_p);
8698 if (tree_to_double_int (vr->max) != tem)
8699 return false;
8701 return true;
8704 /* Simplify a conversion from integral SSA name to float in STMT. */
8706 static bool
8707 simplify_float_conversion_using_ranges (gimple_stmt_iterator *gsi, gimple stmt)
8709 tree rhs1 = gimple_assign_rhs1 (stmt);
8710 value_range_t *vr = get_value_range (rhs1);
8711 enum machine_mode fltmode = TYPE_MODE (TREE_TYPE (gimple_assign_lhs (stmt)));
8712 enum machine_mode mode;
8713 tree tem;
8714 gimple conv;
8716 /* We can only handle constant ranges. */
8717 if (vr->type != VR_RANGE
8718 || TREE_CODE (vr->min) != INTEGER_CST
8719 || TREE_CODE (vr->max) != INTEGER_CST)
8720 return false;
8722 /* First check if we can use a signed type in place of an unsigned. */
8723 if (TYPE_UNSIGNED (TREE_TYPE (rhs1))
8724 && (can_float_p (fltmode, TYPE_MODE (TREE_TYPE (rhs1)), 0)
8725 != CODE_FOR_nothing)
8726 && range_fits_type_p (vr, GET_MODE_PRECISION
8727 (TYPE_MODE (TREE_TYPE (rhs1))), 0))
8728 mode = TYPE_MODE (TREE_TYPE (rhs1));
8729 /* If we can do the conversion in the current input mode do nothing. */
8730 else if (can_float_p (fltmode, TYPE_MODE (TREE_TYPE (rhs1)),
8731 TYPE_UNSIGNED (TREE_TYPE (rhs1))))
8732 return false;
8733 /* Otherwise search for a mode we can use, starting from the narrowest
8734 integer mode available. */
8735 else
8737 mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
8740 /* If we cannot do a signed conversion to float from mode
8741 or if the value-range does not fit in the signed type
8742 try with a wider mode. */
8743 if (can_float_p (fltmode, mode, 0) != CODE_FOR_nothing
8744 && range_fits_type_p (vr, GET_MODE_PRECISION (mode), 0))
8745 break;
8747 mode = GET_MODE_WIDER_MODE (mode);
8748 /* But do not widen the input. Instead leave that to the
8749 optabs expansion code. */
8750 if (GET_MODE_PRECISION (mode) > TYPE_PRECISION (TREE_TYPE (rhs1)))
8751 return false;
8753 while (mode != VOIDmode);
8754 if (mode == VOIDmode)
8755 return false;
8758 /* It works, insert a truncation or sign-change before the
8759 float conversion. */
8760 tem = make_ssa_name (build_nonstandard_integer_type
8761 (GET_MODE_PRECISION (mode), 0), NULL);
8762 conv = gimple_build_assign_with_ops (NOP_EXPR, tem, rhs1, NULL_TREE);
8763 gsi_insert_before (gsi, conv, GSI_SAME_STMT);
8764 gimple_assign_set_rhs1 (stmt, tem);
8765 update_stmt (stmt);
8767 return true;
8770 /* Simplify STMT using ranges if possible. */
8772 static bool
8773 simplify_stmt_using_ranges (gimple_stmt_iterator *gsi)
8775 gimple stmt = gsi_stmt (*gsi);
8776 if (is_gimple_assign (stmt))
8778 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
8779 tree rhs1 = gimple_assign_rhs1 (stmt);
8781 switch (rhs_code)
8783 case EQ_EXPR:
8784 case NE_EXPR:
8785 /* Transform EQ_EXPR, NE_EXPR into BIT_XOR_EXPR or identity
8786 if the RHS is zero or one, and the LHS are known to be boolean
8787 values. */
8788 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
8789 return simplify_truth_ops_using_ranges (gsi, stmt);
8790 break;
8792 /* Transform TRUNC_DIV_EXPR and TRUNC_MOD_EXPR into RSHIFT_EXPR
8793 and BIT_AND_EXPR respectively if the first operand is greater
8794 than zero and the second operand is an exact power of two. */
8795 case TRUNC_DIV_EXPR:
8796 case TRUNC_MOD_EXPR:
8797 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1))
8798 && integer_pow2p (gimple_assign_rhs2 (stmt)))
8799 return simplify_div_or_mod_using_ranges (stmt);
8800 break;
8802 /* Transform ABS (X) into X or -X as appropriate. */
8803 case ABS_EXPR:
8804 if (TREE_CODE (rhs1) == SSA_NAME
8805 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
8806 return simplify_abs_using_ranges (stmt);
8807 break;
8809 case BIT_AND_EXPR:
8810 case BIT_IOR_EXPR:
8811 /* Optimize away BIT_AND_EXPR and BIT_IOR_EXPR
8812 if all the bits being cleared are already cleared or
8813 all the bits being set are already set. */
8814 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
8815 return simplify_bit_ops_using_ranges (gsi, stmt);
8816 break;
8818 CASE_CONVERT:
8819 if (TREE_CODE (rhs1) == SSA_NAME
8820 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
8821 return simplify_conversion_using_ranges (stmt);
8822 break;
8824 case FLOAT_EXPR:
8825 if (TREE_CODE (rhs1) == SSA_NAME
8826 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
8827 return simplify_float_conversion_using_ranges (gsi, stmt);
8828 break;
8830 default:
8831 break;
8834 else if (gimple_code (stmt) == GIMPLE_COND)
8835 return simplify_cond_using_ranges (stmt);
8836 else if (gimple_code (stmt) == GIMPLE_SWITCH)
8837 return simplify_switch_using_ranges (stmt);
8839 return false;
8842 /* If the statement pointed by SI has a predicate whose value can be
8843 computed using the value range information computed by VRP, compute
8844 its value and return true. Otherwise, return false. */
8846 static bool
8847 fold_predicate_in (gimple_stmt_iterator *si)
8849 bool assignment_p = false;
8850 tree val;
8851 gimple stmt = gsi_stmt (*si);
8853 if (is_gimple_assign (stmt)
8854 && TREE_CODE_CLASS (gimple_assign_rhs_code (stmt)) == tcc_comparison)
8856 assignment_p = true;
8857 val = vrp_evaluate_conditional (gimple_assign_rhs_code (stmt),
8858 gimple_assign_rhs1 (stmt),
8859 gimple_assign_rhs2 (stmt),
8860 stmt);
8862 else if (gimple_code (stmt) == GIMPLE_COND)
8863 val = vrp_evaluate_conditional (gimple_cond_code (stmt),
8864 gimple_cond_lhs (stmt),
8865 gimple_cond_rhs (stmt),
8866 stmt);
8867 else
8868 return false;
8870 if (val)
8872 if (assignment_p)
8873 val = fold_convert (gimple_expr_type (stmt), val);
8875 if (dump_file)
8877 fprintf (dump_file, "Folding predicate ");
8878 print_gimple_expr (dump_file, stmt, 0, 0);
8879 fprintf (dump_file, " to ");
8880 print_generic_expr (dump_file, val, 0);
8881 fprintf (dump_file, "\n");
8884 if (is_gimple_assign (stmt))
8885 gimple_assign_set_rhs_from_tree (si, val);
8886 else
8888 gcc_assert (gimple_code (stmt) == GIMPLE_COND);
8889 if (integer_zerop (val))
8890 gimple_cond_make_false (stmt);
8891 else if (integer_onep (val))
8892 gimple_cond_make_true (stmt);
8893 else
8894 gcc_unreachable ();
8897 return true;
8900 return false;
8903 /* Callback for substitute_and_fold folding the stmt at *SI. */
8905 static bool
8906 vrp_fold_stmt (gimple_stmt_iterator *si)
8908 if (fold_predicate_in (si))
8909 return true;
8911 return simplify_stmt_using_ranges (si);
8914 /* Stack of dest,src equivalency pairs that need to be restored after
8915 each attempt to thread a block's incoming edge to an outgoing edge.
8917 A NULL entry is used to mark the end of pairs which need to be
8918 restored. */
8919 static vec<tree> equiv_stack;
8921 /* A trivial wrapper so that we can present the generic jump threading
8922 code with a simple API for simplifying statements. STMT is the
8923 statement we want to simplify, WITHIN_STMT provides the location
8924 for any overflow warnings. */
8926 static tree
8927 simplify_stmt_for_jump_threading (gimple stmt, gimple within_stmt)
8929 /* We only use VRP information to simplify conditionals. This is
8930 overly conservative, but it's unclear if doing more would be
8931 worth the compile time cost. */
8932 if (gimple_code (stmt) != GIMPLE_COND)
8933 return NULL;
8935 return vrp_evaluate_conditional (gimple_cond_code (stmt),
8936 gimple_cond_lhs (stmt),
8937 gimple_cond_rhs (stmt), within_stmt);
8940 /* Blocks which have more than one predecessor and more than
8941 one successor present jump threading opportunities, i.e.,
8942 when the block is reached from a specific predecessor, we
8943 may be able to determine which of the outgoing edges will
8944 be traversed. When this optimization applies, we are able
8945 to avoid conditionals at runtime and we may expose secondary
8946 optimization opportunities.
8948 This routine is effectively a driver for the generic jump
8949 threading code. It basically just presents the generic code
8950 with edges that may be suitable for jump threading.
8952 Unlike DOM, we do not iterate VRP if jump threading was successful.
8953 While iterating may expose new opportunities for VRP, it is expected
8954 those opportunities would be very limited and the compile time cost
8955 to expose those opportunities would be significant.
8957 As jump threading opportunities are discovered, they are registered
8958 for later realization. */
8960 static void
8961 identify_jump_threads (void)
8963 basic_block bb;
8964 gimple dummy;
8965 int i;
8966 edge e;
8968 /* Ugh. When substituting values earlier in this pass we can
8969 wipe the dominance information. So rebuild the dominator
8970 information as we need it within the jump threading code. */
8971 calculate_dominance_info (CDI_DOMINATORS);
8973 /* We do not allow VRP information to be used for jump threading
8974 across a back edge in the CFG. Otherwise it becomes too
8975 difficult to avoid eliminating loop exit tests. Of course
8976 EDGE_DFS_BACK is not accurate at this time so we have to
8977 recompute it. */
8978 mark_dfs_back_edges ();
8980 /* Do not thread across edges we are about to remove. Just marking
8981 them as EDGE_DFS_BACK will do. */
8982 FOR_EACH_VEC_ELT (to_remove_edges, i, e)
8983 e->flags |= EDGE_DFS_BACK;
8985 /* Allocate our unwinder stack to unwind any temporary equivalences
8986 that might be recorded. */
8987 equiv_stack.create (20);
8989 /* To avoid lots of silly node creation, we create a single
8990 conditional and just modify it in-place when attempting to
8991 thread jumps. */
8992 dummy = gimple_build_cond (EQ_EXPR,
8993 integer_zero_node, integer_zero_node,
8994 NULL, NULL);
8996 /* Walk through all the blocks finding those which present a
8997 potential jump threading opportunity. We could set this up
8998 as a dominator walker and record data during the walk, but
8999 I doubt it's worth the effort for the classes of jump
9000 threading opportunities we are trying to identify at this
9001 point in compilation. */
9002 FOR_EACH_BB (bb)
9004 gimple last;
9006 /* If the generic jump threading code does not find this block
9007 interesting, then there is nothing to do. */
9008 if (! potentially_threadable_block (bb))
9009 continue;
9011 /* We only care about blocks ending in a COND_EXPR. While there
9012 may be some value in handling SWITCH_EXPR here, I doubt it's
9013 terribly important. */
9014 last = gsi_stmt (gsi_last_bb (bb));
9016 /* We're basically looking for a switch or any kind of conditional with
9017 integral or pointer type arguments. Note the type of the second
9018 argument will be the same as the first argument, so no need to
9019 check it explicitly. */
9020 if (gimple_code (last) == GIMPLE_SWITCH
9021 || (gimple_code (last) == GIMPLE_COND
9022 && TREE_CODE (gimple_cond_lhs (last)) == SSA_NAME
9023 && (INTEGRAL_TYPE_P (TREE_TYPE (gimple_cond_lhs (last)))
9024 || POINTER_TYPE_P (TREE_TYPE (gimple_cond_lhs (last))))
9025 && (TREE_CODE (gimple_cond_rhs (last)) == SSA_NAME
9026 || is_gimple_min_invariant (gimple_cond_rhs (last)))))
9028 edge_iterator ei;
9030 /* We've got a block with multiple predecessors and multiple
9031 successors which also ends in a suitable conditional or
9032 switch statement. For each predecessor, see if we can thread
9033 it to a specific successor. */
9034 FOR_EACH_EDGE (e, ei, bb->preds)
9036 /* Do not thread across back edges or abnormal edges
9037 in the CFG. */
9038 if (e->flags & (EDGE_DFS_BACK | EDGE_COMPLEX))
9039 continue;
9041 thread_across_edge (dummy, e, true, &equiv_stack,
9042 simplify_stmt_for_jump_threading);
9047 /* We do not actually update the CFG or SSA graphs at this point as
9048 ASSERT_EXPRs are still in the IL and cfg cleanup code does not yet
9049 handle ASSERT_EXPRs gracefully. */
9052 /* We identified all the jump threading opportunities earlier, but could
9053 not transform the CFG at that time. This routine transforms the
9054 CFG and arranges for the dominator tree to be rebuilt if necessary.
9056 Note the SSA graph update will occur during the normal TODO
9057 processing by the pass manager. */
9058 static void
9059 finalize_jump_threads (void)
9061 thread_through_all_blocks (false);
9062 equiv_stack.release ();
9066 /* Traverse all the blocks folding conditionals with known ranges. */
9068 static void
9069 vrp_finalize (void)
9071 size_t i;
9073 values_propagated = true;
9075 if (dump_file)
9077 fprintf (dump_file, "\nValue ranges after VRP:\n\n");
9078 dump_all_value_ranges (dump_file);
9079 fprintf (dump_file, "\n");
9082 substitute_and_fold (op_with_constant_singleton_value_range,
9083 vrp_fold_stmt, false);
9085 if (warn_array_bounds)
9086 check_all_array_refs ();
9088 /* We must identify jump threading opportunities before we release
9089 the datastructures built by VRP. */
9090 identify_jump_threads ();
9092 /* Free allocated memory. */
9093 for (i = 0; i < num_vr_values; i++)
9094 if (vr_value[i])
9096 BITMAP_FREE (vr_value[i]->equiv);
9097 free (vr_value[i]);
9100 free (vr_value);
9101 free (vr_phi_edge_counts);
9103 /* So that we can distinguish between VRP data being available
9104 and not available. */
9105 vr_value = NULL;
9106 vr_phi_edge_counts = NULL;
9110 /* Main entry point to VRP (Value Range Propagation). This pass is
9111 loosely based on J. R. C. Patterson, ``Accurate Static Branch
9112 Prediction by Value Range Propagation,'' in SIGPLAN Conference on
9113 Programming Language Design and Implementation, pp. 67-78, 1995.
9114 Also available at http://citeseer.ist.psu.edu/patterson95accurate.html
9116 This is essentially an SSA-CCP pass modified to deal with ranges
9117 instead of constants.
9119 While propagating ranges, we may find that two or more SSA name
9120 have equivalent, though distinct ranges. For instance,
9122 1 x_9 = p_3->a;
9123 2 p_4 = ASSERT_EXPR <p_3, p_3 != 0>
9124 3 if (p_4 == q_2)
9125 4 p_5 = ASSERT_EXPR <p_4, p_4 == q_2>;
9126 5 endif
9127 6 if (q_2)
9129 In the code above, pointer p_5 has range [q_2, q_2], but from the
9130 code we can also determine that p_5 cannot be NULL and, if q_2 had
9131 a non-varying range, p_5's range should also be compatible with it.
9133 These equivalences are created by two expressions: ASSERT_EXPR and
9134 copy operations. Since p_5 is an assertion on p_4, and p_4 was the
9135 result of another assertion, then we can use the fact that p_5 and
9136 p_4 are equivalent when evaluating p_5's range.
9138 Together with value ranges, we also propagate these equivalences
9139 between names so that we can take advantage of information from
9140 multiple ranges when doing final replacement. Note that this
9141 equivalency relation is transitive but not symmetric.
9143 In the example above, p_5 is equivalent to p_4, q_2 and p_3, but we
9144 cannot assert that q_2 is equivalent to p_5 because q_2 may be used
9145 in contexts where that assertion does not hold (e.g., in line 6).
9147 TODO, the main difference between this pass and Patterson's is that
9148 we do not propagate edge probabilities. We only compute whether
9149 edges can be taken or not. That is, instead of having a spectrum
9150 of jump probabilities between 0 and 1, we only deal with 0, 1 and
9151 DON'T KNOW. In the future, it may be worthwhile to propagate
9152 probabilities to aid branch prediction. */
9154 static unsigned int
9155 execute_vrp (void)
9157 int i;
9158 edge e;
9159 switch_update *su;
9161 loop_optimizer_init (LOOPS_NORMAL | LOOPS_HAVE_RECORDED_EXITS);
9162 rewrite_into_loop_closed_ssa (NULL, TODO_update_ssa);
9163 scev_initialize ();
9165 insert_range_assertions ();
9167 to_remove_edges.create (10);
9168 to_update_switch_stmts.create (5);
9169 threadedge_initialize_values ();
9171 vrp_initialize ();
9172 ssa_propagate (vrp_visit_stmt, vrp_visit_phi_node);
9173 vrp_finalize ();
9175 free_numbers_of_iterations_estimates ();
9177 /* ASSERT_EXPRs must be removed before finalizing jump threads
9178 as finalizing jump threads calls the CFG cleanup code which
9179 does not properly handle ASSERT_EXPRs. */
9180 remove_range_assertions ();
9182 /* If we exposed any new variables, go ahead and put them into
9183 SSA form now, before we handle jump threading. This simplifies
9184 interactions between rewriting of _DECL nodes into SSA form
9185 and rewriting SSA_NAME nodes into SSA form after block
9186 duplication and CFG manipulation. */
9187 update_ssa (TODO_update_ssa);
9189 finalize_jump_threads ();
9191 /* Remove dead edges from SWITCH_EXPR optimization. This leaves the
9192 CFG in a broken state and requires a cfg_cleanup run. */
9193 FOR_EACH_VEC_ELT (to_remove_edges, i, e)
9194 remove_edge (e);
9195 /* Update SWITCH_EXPR case label vector. */
9196 FOR_EACH_VEC_ELT (to_update_switch_stmts, i, su)
9198 size_t j;
9199 size_t n = TREE_VEC_LENGTH (su->vec);
9200 tree label;
9201 gimple_switch_set_num_labels (su->stmt, n);
9202 for (j = 0; j < n; j++)
9203 gimple_switch_set_label (su->stmt, j, TREE_VEC_ELT (su->vec, j));
9204 /* As we may have replaced the default label with a regular one
9205 make sure to make it a real default label again. This ensures
9206 optimal expansion. */
9207 label = gimple_switch_label (su->stmt, 0);
9208 CASE_LOW (label) = NULL_TREE;
9209 CASE_HIGH (label) = NULL_TREE;
9212 if (to_remove_edges.length () > 0)
9213 free_dominance_info (CDI_DOMINATORS);
9215 to_remove_edges.release ();
9216 to_update_switch_stmts.release ();
9217 threadedge_finalize_values ();
9219 scev_finalize ();
9220 loop_optimizer_finalize ();
9221 return 0;
9224 static bool
9225 gate_vrp (void)
9227 return flag_tree_vrp != 0;
9230 struct gimple_opt_pass pass_vrp =
9233 GIMPLE_PASS,
9234 "vrp", /* name */
9235 OPTGROUP_NONE, /* optinfo_flags */
9236 gate_vrp, /* gate */
9237 execute_vrp, /* execute */
9238 NULL, /* sub */
9239 NULL, /* next */
9240 0, /* static_pass_number */
9241 TV_TREE_VRP, /* tv_id */
9242 PROP_ssa, /* properties_required */
9243 0, /* properties_provided */
9244 0, /* properties_destroyed */
9245 0, /* todo_flags_start */
9246 TODO_cleanup_cfg
9247 | TODO_update_ssa
9248 | TODO_verify_ssa
9249 | TODO_verify_flow
9250 | TODO_ggc_collect /* todo_flags_finish */