2013-10-22 Jan-Benedict Glaw <jbglaw@lug-owl.de>
[official-gcc.git] / gcc / tree-vrp.c
blob0a059645c50cf1e4eda6dd9eaeafe1b35a866f58
1 /* Support routines for Value Range Propagation (VRP).
2 Copyright (C) 2005-2013 Free Software Foundation, Inc.
3 Contributed by Diego Novillo <dnovillo@redhat.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "ggc.h"
26 #include "flags.h"
27 #include "tree.h"
28 #include "basic-block.h"
29 #include "tree-ssa.h"
30 #include "tree-pass.h"
31 #include "tree-dump.h"
32 #include "gimple-pretty-print.h"
33 #include "diagnostic-core.h"
34 #include "intl.h"
35 #include "cfgloop.h"
36 #include "tree-scalar-evolution.h"
37 #include "tree-ssa-propagate.h"
38 #include "tree-chrec.h"
39 #include "tree-ssa-threadupdate.h"
40 #include "expr.h"
41 #include "optabs.h"
42 #include "tree-ssa-threadedge.h"
46 /* Range of values that can be associated with an SSA_NAME after VRP
47 has executed. */
48 struct value_range_d
50 /* Lattice value represented by this range. */
51 enum value_range_type type;
53 /* Minimum and maximum values represented by this range. These
54 values should be interpreted as follows:
56 - If TYPE is VR_UNDEFINED or VR_VARYING then MIN and MAX must
57 be NULL.
59 - If TYPE == VR_RANGE then MIN holds the minimum value and
60 MAX holds the maximum value of the range [MIN, MAX].
62 - If TYPE == ANTI_RANGE the variable is known to NOT
63 take any values in the range [MIN, MAX]. */
64 tree min;
65 tree max;
67 /* Set of SSA names whose value ranges are equivalent to this one.
68 This set is only valid when TYPE is VR_RANGE or VR_ANTI_RANGE. */
69 bitmap equiv;
72 typedef struct value_range_d value_range_t;
74 #define VR_INITIALIZER { VR_UNDEFINED, NULL_TREE, NULL_TREE, NULL }
76 /* Set of SSA names found live during the RPO traversal of the function
77 for still active basic-blocks. */
78 static sbitmap *live;
80 /* Return true if the SSA name NAME is live on the edge E. */
82 static bool
83 live_on_edge (edge e, tree name)
85 return (live[e->dest->index]
86 && bitmap_bit_p (live[e->dest->index], SSA_NAME_VERSION (name)));
89 /* Local functions. */
90 static int compare_values (tree val1, tree val2);
91 static int compare_values_warnv (tree val1, tree val2, bool *);
92 static void vrp_meet (value_range_t *, value_range_t *);
93 static void vrp_intersect_ranges (value_range_t *, value_range_t *);
94 static tree vrp_evaluate_conditional_warnv_with_ops (enum tree_code,
95 tree, tree, bool, bool *,
96 bool *);
98 /* Location information for ASSERT_EXPRs. Each instance of this
99 structure describes an ASSERT_EXPR for an SSA name. Since a single
100 SSA name may have more than one assertion associated with it, these
101 locations are kept in a linked list attached to the corresponding
102 SSA name. */
103 struct assert_locus_d
105 /* Basic block where the assertion would be inserted. */
106 basic_block bb;
108 /* Some assertions need to be inserted on an edge (e.g., assertions
109 generated by COND_EXPRs). In those cases, BB will be NULL. */
110 edge e;
112 /* Pointer to the statement that generated this assertion. */
113 gimple_stmt_iterator si;
115 /* Predicate code for the ASSERT_EXPR. Must be COMPARISON_CLASS_P. */
116 enum tree_code comp_code;
118 /* Value being compared against. */
119 tree val;
121 /* Expression to compare. */
122 tree expr;
124 /* Next node in the linked list. */
125 struct assert_locus_d *next;
128 typedef struct assert_locus_d *assert_locus_t;
130 /* If bit I is present, it means that SSA name N_i has a list of
131 assertions that should be inserted in the IL. */
132 static bitmap need_assert_for;
134 /* Array of locations lists where to insert assertions. ASSERTS_FOR[I]
135 holds a list of ASSERT_LOCUS_T nodes that describe where
136 ASSERT_EXPRs for SSA name N_I should be inserted. */
137 static assert_locus_t *asserts_for;
139 /* Value range array. After propagation, VR_VALUE[I] holds the range
140 of values that SSA name N_I may take. */
141 static unsigned num_vr_values;
142 static value_range_t **vr_value;
143 static bool values_propagated;
145 /* For a PHI node which sets SSA name N_I, VR_COUNTS[I] holds the
146 number of executable edges we saw the last time we visited the
147 node. */
148 static int *vr_phi_edge_counts;
150 typedef struct {
151 gimple stmt;
152 tree vec;
153 } switch_update;
155 static vec<edge> to_remove_edges;
156 static vec<switch_update> to_update_switch_stmts;
159 /* Return the maximum value for TYPE. */
161 static inline tree
162 vrp_val_max (const_tree type)
164 if (!INTEGRAL_TYPE_P (type))
165 return NULL_TREE;
167 return TYPE_MAX_VALUE (type);
170 /* Return the minimum value for TYPE. */
172 static inline tree
173 vrp_val_min (const_tree type)
175 if (!INTEGRAL_TYPE_P (type))
176 return NULL_TREE;
178 return TYPE_MIN_VALUE (type);
181 /* Return whether VAL is equal to the maximum value of its type. This
182 will be true for a positive overflow infinity. We can't do a
183 simple equality comparison with TYPE_MAX_VALUE because C typedefs
184 and Ada subtypes can produce types whose TYPE_MAX_VALUE is not ==
185 to the integer constant with the same value in the type. */
187 static inline bool
188 vrp_val_is_max (const_tree val)
190 tree type_max = vrp_val_max (TREE_TYPE (val));
191 return (val == type_max
192 || (type_max != NULL_TREE
193 && operand_equal_p (val, type_max, 0)));
196 /* Return whether VAL is equal to the minimum value of its type. This
197 will be true for a negative overflow infinity. */
199 static inline bool
200 vrp_val_is_min (const_tree val)
202 tree type_min = vrp_val_min (TREE_TYPE (val));
203 return (val == type_min
204 || (type_min != NULL_TREE
205 && operand_equal_p (val, type_min, 0)));
209 /* Return whether TYPE should use an overflow infinity distinct from
210 TYPE_{MIN,MAX}_VALUE. We use an overflow infinity value to
211 represent a signed overflow during VRP computations. An infinity
212 is distinct from a half-range, which will go from some number to
213 TYPE_{MIN,MAX}_VALUE. */
215 static inline bool
216 needs_overflow_infinity (const_tree type)
218 return INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_WRAPS (type);
221 /* Return whether TYPE can support our overflow infinity
222 representation: we use the TREE_OVERFLOW flag, which only exists
223 for constants. If TYPE doesn't support this, we don't optimize
224 cases which would require signed overflow--we drop them to
225 VARYING. */
227 static inline bool
228 supports_overflow_infinity (const_tree type)
230 tree min = vrp_val_min (type), max = vrp_val_max (type);
231 #ifdef ENABLE_CHECKING
232 gcc_assert (needs_overflow_infinity (type));
233 #endif
234 return (min != NULL_TREE
235 && CONSTANT_CLASS_P (min)
236 && max != NULL_TREE
237 && CONSTANT_CLASS_P (max));
240 /* VAL is the maximum or minimum value of a type. Return a
241 corresponding overflow infinity. */
243 static inline tree
244 make_overflow_infinity (tree val)
246 gcc_checking_assert (val != NULL_TREE && CONSTANT_CLASS_P (val));
247 val = copy_node (val);
248 TREE_OVERFLOW (val) = 1;
249 return val;
252 /* Return a negative overflow infinity for TYPE. */
254 static inline tree
255 negative_overflow_infinity (tree type)
257 gcc_checking_assert (supports_overflow_infinity (type));
258 return make_overflow_infinity (vrp_val_min (type));
261 /* Return a positive overflow infinity for TYPE. */
263 static inline tree
264 positive_overflow_infinity (tree type)
266 gcc_checking_assert (supports_overflow_infinity (type));
267 return make_overflow_infinity (vrp_val_max (type));
270 /* Return whether VAL is a negative overflow infinity. */
272 static inline bool
273 is_negative_overflow_infinity (const_tree val)
275 return (needs_overflow_infinity (TREE_TYPE (val))
276 && CONSTANT_CLASS_P (val)
277 && TREE_OVERFLOW (val)
278 && vrp_val_is_min (val));
281 /* Return whether VAL is a positive overflow infinity. */
283 static inline bool
284 is_positive_overflow_infinity (const_tree val)
286 return (needs_overflow_infinity (TREE_TYPE (val))
287 && CONSTANT_CLASS_P (val)
288 && TREE_OVERFLOW (val)
289 && vrp_val_is_max (val));
292 /* Return whether VAL is a positive or negative overflow infinity. */
294 static inline bool
295 is_overflow_infinity (const_tree val)
297 return (needs_overflow_infinity (TREE_TYPE (val))
298 && CONSTANT_CLASS_P (val)
299 && TREE_OVERFLOW (val)
300 && (vrp_val_is_min (val) || vrp_val_is_max (val)));
303 /* Return whether STMT has a constant rhs that is_overflow_infinity. */
305 static inline bool
306 stmt_overflow_infinity (gimple stmt)
308 if (is_gimple_assign (stmt)
309 && get_gimple_rhs_class (gimple_assign_rhs_code (stmt)) ==
310 GIMPLE_SINGLE_RHS)
311 return is_overflow_infinity (gimple_assign_rhs1 (stmt));
312 return false;
315 /* If VAL is now an overflow infinity, return VAL. Otherwise, return
316 the same value with TREE_OVERFLOW clear. This can be used to avoid
317 confusing a regular value with an overflow value. */
319 static inline tree
320 avoid_overflow_infinity (tree val)
322 if (!is_overflow_infinity (val))
323 return val;
325 if (vrp_val_is_max (val))
326 return vrp_val_max (TREE_TYPE (val));
327 else
329 gcc_checking_assert (vrp_val_is_min (val));
330 return vrp_val_min (TREE_TYPE (val));
335 /* Return true if ARG is marked with the nonnull attribute in the
336 current function signature. */
338 static bool
339 nonnull_arg_p (const_tree arg)
341 tree t, attrs, fntype;
342 unsigned HOST_WIDE_INT arg_num;
344 gcc_assert (TREE_CODE (arg) == PARM_DECL && POINTER_TYPE_P (TREE_TYPE (arg)));
346 /* The static chain decl is always non null. */
347 if (arg == cfun->static_chain_decl)
348 return true;
350 fntype = TREE_TYPE (current_function_decl);
351 for (attrs = TYPE_ATTRIBUTES (fntype); attrs; attrs = TREE_CHAIN (attrs))
353 attrs = lookup_attribute ("nonnull", attrs);
355 /* If "nonnull" wasn't specified, we know nothing about the argument. */
356 if (attrs == NULL_TREE)
357 return false;
359 /* If "nonnull" applies to all the arguments, then ARG is non-null. */
360 if (TREE_VALUE (attrs) == NULL_TREE)
361 return true;
363 /* Get the position number for ARG in the function signature. */
364 for (arg_num = 1, t = DECL_ARGUMENTS (current_function_decl);
366 t = DECL_CHAIN (t), arg_num++)
368 if (t == arg)
369 break;
372 gcc_assert (t == arg);
374 /* Now see if ARG_NUM is mentioned in the nonnull list. */
375 for (t = TREE_VALUE (attrs); t; t = TREE_CHAIN (t))
377 if (compare_tree_int (TREE_VALUE (t), arg_num) == 0)
378 return true;
382 return false;
386 /* Set value range VR to VR_UNDEFINED. */
388 static inline void
389 set_value_range_to_undefined (value_range_t *vr)
391 vr->type = VR_UNDEFINED;
392 vr->min = vr->max = NULL_TREE;
393 if (vr->equiv)
394 bitmap_clear (vr->equiv);
398 /* Set value range VR to VR_VARYING. */
400 static inline void
401 set_value_range_to_varying (value_range_t *vr)
403 vr->type = VR_VARYING;
404 vr->min = vr->max = NULL_TREE;
405 if (vr->equiv)
406 bitmap_clear (vr->equiv);
410 /* Set value range VR to {T, MIN, MAX, EQUIV}. */
412 static void
413 set_value_range (value_range_t *vr, enum value_range_type t, tree min,
414 tree max, bitmap equiv)
416 #if defined ENABLE_CHECKING
417 /* Check the validity of the range. */
418 if (t == VR_RANGE || t == VR_ANTI_RANGE)
420 int cmp;
422 gcc_assert (min && max);
424 if (INTEGRAL_TYPE_P (TREE_TYPE (min)) && t == VR_ANTI_RANGE)
425 gcc_assert (!vrp_val_is_min (min) || !vrp_val_is_max (max));
427 cmp = compare_values (min, max);
428 gcc_assert (cmp == 0 || cmp == -1 || cmp == -2);
430 if (needs_overflow_infinity (TREE_TYPE (min)))
431 gcc_assert (!is_overflow_infinity (min)
432 || !is_overflow_infinity (max));
435 if (t == VR_UNDEFINED || t == VR_VARYING)
436 gcc_assert (min == NULL_TREE && max == NULL_TREE);
438 if (t == VR_UNDEFINED || t == VR_VARYING)
439 gcc_assert (equiv == NULL || bitmap_empty_p (equiv));
440 #endif
442 vr->type = t;
443 vr->min = min;
444 vr->max = max;
446 /* Since updating the equivalence set involves deep copying the
447 bitmaps, only do it if absolutely necessary. */
448 if (vr->equiv == NULL
449 && equiv != NULL)
450 vr->equiv = BITMAP_ALLOC (NULL);
452 if (equiv != vr->equiv)
454 if (equiv && !bitmap_empty_p (equiv))
455 bitmap_copy (vr->equiv, equiv);
456 else
457 bitmap_clear (vr->equiv);
462 /* Set value range VR to the canonical form of {T, MIN, MAX, EQUIV}.
463 This means adjusting T, MIN and MAX representing the case of a
464 wrapping range with MAX < MIN covering [MIN, type_max] U [type_min, MAX]
465 as anti-rage ~[MAX+1, MIN-1]. Likewise for wrapping anti-ranges.
466 In corner cases where MAX+1 or MIN-1 wraps this will fall back
467 to varying.
468 This routine exists to ease canonicalization in the case where we
469 extract ranges from var + CST op limit. */
471 static void
472 set_and_canonicalize_value_range (value_range_t *vr, enum value_range_type t,
473 tree min, tree max, bitmap equiv)
475 /* Use the canonical setters for VR_UNDEFINED and VR_VARYING. */
476 if (t == VR_UNDEFINED)
478 set_value_range_to_undefined (vr);
479 return;
481 else if (t == VR_VARYING)
483 set_value_range_to_varying (vr);
484 return;
487 /* Nothing to canonicalize for symbolic ranges. */
488 if (TREE_CODE (min) != INTEGER_CST
489 || TREE_CODE (max) != INTEGER_CST)
491 set_value_range (vr, t, min, max, equiv);
492 return;
495 /* Wrong order for min and max, to swap them and the VR type we need
496 to adjust them. */
497 if (tree_int_cst_lt (max, min))
499 tree one, tmp;
501 /* For one bit precision if max < min, then the swapped
502 range covers all values, so for VR_RANGE it is varying and
503 for VR_ANTI_RANGE empty range, so drop to varying as well. */
504 if (TYPE_PRECISION (TREE_TYPE (min)) == 1)
506 set_value_range_to_varying (vr);
507 return;
510 one = build_int_cst (TREE_TYPE (min), 1);
511 tmp = int_const_binop (PLUS_EXPR, max, one);
512 max = int_const_binop (MINUS_EXPR, min, one);
513 min = tmp;
515 /* There's one corner case, if we had [C+1, C] before we now have
516 that again. But this represents an empty value range, so drop
517 to varying in this case. */
518 if (tree_int_cst_lt (max, min))
520 set_value_range_to_varying (vr);
521 return;
524 t = t == VR_RANGE ? VR_ANTI_RANGE : VR_RANGE;
527 /* Anti-ranges that can be represented as ranges should be so. */
528 if (t == VR_ANTI_RANGE)
530 bool is_min = vrp_val_is_min (min);
531 bool is_max = vrp_val_is_max (max);
533 if (is_min && is_max)
535 /* We cannot deal with empty ranges, drop to varying.
536 ??? This could be VR_UNDEFINED instead. */
537 set_value_range_to_varying (vr);
538 return;
540 else if (TYPE_PRECISION (TREE_TYPE (min)) == 1
541 && (is_min || is_max))
543 /* Non-empty boolean ranges can always be represented
544 as a singleton range. */
545 if (is_min)
546 min = max = vrp_val_max (TREE_TYPE (min));
547 else
548 min = max = vrp_val_min (TREE_TYPE (min));
549 t = VR_RANGE;
551 else if (is_min
552 /* As a special exception preserve non-null ranges. */
553 && !(TYPE_UNSIGNED (TREE_TYPE (min))
554 && integer_zerop (max)))
556 tree one = build_int_cst (TREE_TYPE (max), 1);
557 min = int_const_binop (PLUS_EXPR, max, one);
558 max = vrp_val_max (TREE_TYPE (max));
559 t = VR_RANGE;
561 else if (is_max)
563 tree one = build_int_cst (TREE_TYPE (min), 1);
564 max = int_const_binop (MINUS_EXPR, min, one);
565 min = vrp_val_min (TREE_TYPE (min));
566 t = VR_RANGE;
570 /* Drop [-INF(OVF), +INF(OVF)] to varying. */
571 if (needs_overflow_infinity (TREE_TYPE (min))
572 && is_overflow_infinity (min)
573 && is_overflow_infinity (max))
575 set_value_range_to_varying (vr);
576 return;
579 set_value_range (vr, t, min, max, equiv);
582 /* Copy value range FROM into value range TO. */
584 static inline void
585 copy_value_range (value_range_t *to, value_range_t *from)
587 set_value_range (to, from->type, from->min, from->max, from->equiv);
590 /* Set value range VR to a single value. This function is only called
591 with values we get from statements, and exists to clear the
592 TREE_OVERFLOW flag so that we don't think we have an overflow
593 infinity when we shouldn't. */
595 static inline void
596 set_value_range_to_value (value_range_t *vr, tree val, bitmap equiv)
598 gcc_assert (is_gimple_min_invariant (val));
599 val = avoid_overflow_infinity (val);
600 set_value_range (vr, VR_RANGE, val, val, equiv);
603 /* Set value range VR to a non-negative range of type TYPE.
604 OVERFLOW_INFINITY indicates whether to use an overflow infinity
605 rather than TYPE_MAX_VALUE; this should be true if we determine
606 that the range is nonnegative based on the assumption that signed
607 overflow does not occur. */
609 static inline void
610 set_value_range_to_nonnegative (value_range_t *vr, tree type,
611 bool overflow_infinity)
613 tree zero;
615 if (overflow_infinity && !supports_overflow_infinity (type))
617 set_value_range_to_varying (vr);
618 return;
621 zero = build_int_cst (type, 0);
622 set_value_range (vr, VR_RANGE, zero,
623 (overflow_infinity
624 ? positive_overflow_infinity (type)
625 : TYPE_MAX_VALUE (type)),
626 vr->equiv);
629 /* Set value range VR to a non-NULL range of type TYPE. */
631 static inline void
632 set_value_range_to_nonnull (value_range_t *vr, tree type)
634 tree zero = build_int_cst (type, 0);
635 set_value_range (vr, VR_ANTI_RANGE, zero, zero, vr->equiv);
639 /* Set value range VR to a NULL range of type TYPE. */
641 static inline void
642 set_value_range_to_null (value_range_t *vr, tree type)
644 set_value_range_to_value (vr, build_int_cst (type, 0), vr->equiv);
648 /* Set value range VR to a range of a truthvalue of type TYPE. */
650 static inline void
651 set_value_range_to_truthvalue (value_range_t *vr, tree type)
653 if (TYPE_PRECISION (type) == 1)
654 set_value_range_to_varying (vr);
655 else
656 set_value_range (vr, VR_RANGE,
657 build_int_cst (type, 0), build_int_cst (type, 1),
658 vr->equiv);
662 /* If abs (min) < abs (max), set VR to [-max, max], if
663 abs (min) >= abs (max), set VR to [-min, min]. */
665 static void
666 abs_extent_range (value_range_t *vr, tree min, tree max)
668 int cmp;
670 gcc_assert (TREE_CODE (min) == INTEGER_CST);
671 gcc_assert (TREE_CODE (max) == INTEGER_CST);
672 gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (min)));
673 gcc_assert (!TYPE_UNSIGNED (TREE_TYPE (min)));
674 min = fold_unary (ABS_EXPR, TREE_TYPE (min), min);
675 max = fold_unary (ABS_EXPR, TREE_TYPE (max), max);
676 if (TREE_OVERFLOW (min) || TREE_OVERFLOW (max))
678 set_value_range_to_varying (vr);
679 return;
681 cmp = compare_values (min, max);
682 if (cmp == -1)
683 min = fold_unary (NEGATE_EXPR, TREE_TYPE (min), max);
684 else if (cmp == 0 || cmp == 1)
686 max = min;
687 min = fold_unary (NEGATE_EXPR, TREE_TYPE (min), min);
689 else
691 set_value_range_to_varying (vr);
692 return;
694 set_and_canonicalize_value_range (vr, VR_RANGE, min, max, NULL);
698 /* Return value range information for VAR.
700 If we have no values ranges recorded (ie, VRP is not running), then
701 return NULL. Otherwise create an empty range if none existed for VAR. */
703 static value_range_t *
704 get_value_range (const_tree var)
706 static const struct value_range_d vr_const_varying
707 = { VR_VARYING, NULL_TREE, NULL_TREE, NULL };
708 value_range_t *vr;
709 tree sym;
710 unsigned ver = SSA_NAME_VERSION (var);
712 /* If we have no recorded ranges, then return NULL. */
713 if (! vr_value)
714 return NULL;
716 /* If we query the range for a new SSA name return an unmodifiable VARYING.
717 We should get here at most from the substitute-and-fold stage which
718 will never try to change values. */
719 if (ver >= num_vr_values)
720 return CONST_CAST (value_range_t *, &vr_const_varying);
722 vr = vr_value[ver];
723 if (vr)
724 return vr;
726 /* After propagation finished do not allocate new value-ranges. */
727 if (values_propagated)
728 return CONST_CAST (value_range_t *, &vr_const_varying);
730 /* Create a default value range. */
731 vr_value[ver] = vr = XCNEW (value_range_t);
733 /* Defer allocating the equivalence set. */
734 vr->equiv = NULL;
736 /* If VAR is a default definition of a parameter, the variable can
737 take any value in VAR's type. */
738 if (SSA_NAME_IS_DEFAULT_DEF (var))
740 sym = SSA_NAME_VAR (var);
741 if (TREE_CODE (sym) == PARM_DECL)
743 /* Try to use the "nonnull" attribute to create ~[0, 0]
744 anti-ranges for pointers. Note that this is only valid with
745 default definitions of PARM_DECLs. */
746 if (POINTER_TYPE_P (TREE_TYPE (sym))
747 && nonnull_arg_p (sym))
748 set_value_range_to_nonnull (vr, TREE_TYPE (sym));
749 else
750 set_value_range_to_varying (vr);
752 else if (TREE_CODE (sym) == RESULT_DECL
753 && DECL_BY_REFERENCE (sym))
754 set_value_range_to_nonnull (vr, TREE_TYPE (sym));
757 return vr;
760 /* Return true, if VAL1 and VAL2 are equal values for VRP purposes. */
762 static inline bool
763 vrp_operand_equal_p (const_tree val1, const_tree val2)
765 if (val1 == val2)
766 return true;
767 if (!val1 || !val2 || !operand_equal_p (val1, val2, 0))
768 return false;
769 if (is_overflow_infinity (val1))
770 return is_overflow_infinity (val2);
771 return true;
774 /* Return true, if the bitmaps B1 and B2 are equal. */
776 static inline bool
777 vrp_bitmap_equal_p (const_bitmap b1, const_bitmap b2)
779 return (b1 == b2
780 || ((!b1 || bitmap_empty_p (b1))
781 && (!b2 || bitmap_empty_p (b2)))
782 || (b1 && b2
783 && bitmap_equal_p (b1, b2)));
786 /* Update the value range and equivalence set for variable VAR to
787 NEW_VR. Return true if NEW_VR is different from VAR's previous
788 value.
790 NOTE: This function assumes that NEW_VR is a temporary value range
791 object created for the sole purpose of updating VAR's range. The
792 storage used by the equivalence set from NEW_VR will be freed by
793 this function. Do not call update_value_range when NEW_VR
794 is the range object associated with another SSA name. */
796 static inline bool
797 update_value_range (const_tree var, value_range_t *new_vr)
799 value_range_t *old_vr;
800 bool is_new;
802 /* Update the value range, if necessary. */
803 old_vr = get_value_range (var);
804 is_new = old_vr->type != new_vr->type
805 || !vrp_operand_equal_p (old_vr->min, new_vr->min)
806 || !vrp_operand_equal_p (old_vr->max, new_vr->max)
807 || !vrp_bitmap_equal_p (old_vr->equiv, new_vr->equiv);
809 if (is_new)
811 /* Do not allow transitions up the lattice. The following
812 is slightly more awkward than just new_vr->type < old_vr->type
813 because VR_RANGE and VR_ANTI_RANGE need to be considered
814 the same. We may not have is_new when transitioning to
815 UNDEFINED or from VARYING. */
816 if (new_vr->type == VR_UNDEFINED
817 || old_vr->type == VR_VARYING)
818 set_value_range_to_varying (old_vr);
819 else
820 set_value_range (old_vr, new_vr->type, new_vr->min, new_vr->max,
821 new_vr->equiv);
824 BITMAP_FREE (new_vr->equiv);
826 return is_new;
830 /* Add VAR and VAR's equivalence set to EQUIV. This is the central
831 point where equivalence processing can be turned on/off. */
833 static void
834 add_equivalence (bitmap *equiv, const_tree var)
836 unsigned ver = SSA_NAME_VERSION (var);
837 value_range_t *vr = vr_value[ver];
839 if (*equiv == NULL)
840 *equiv = BITMAP_ALLOC (NULL);
841 bitmap_set_bit (*equiv, ver);
842 if (vr && vr->equiv)
843 bitmap_ior_into (*equiv, vr->equiv);
847 /* Return true if VR is ~[0, 0]. */
849 static inline bool
850 range_is_nonnull (value_range_t *vr)
852 return vr->type == VR_ANTI_RANGE
853 && integer_zerop (vr->min)
854 && integer_zerop (vr->max);
858 /* Return true if VR is [0, 0]. */
860 static inline bool
861 range_is_null (value_range_t *vr)
863 return vr->type == VR_RANGE
864 && integer_zerop (vr->min)
865 && integer_zerop (vr->max);
868 /* Return true if max and min of VR are INTEGER_CST. It's not necessary
869 a singleton. */
871 static inline bool
872 range_int_cst_p (value_range_t *vr)
874 return (vr->type == VR_RANGE
875 && TREE_CODE (vr->max) == INTEGER_CST
876 && TREE_CODE (vr->min) == INTEGER_CST);
879 /* Return true if VR is a INTEGER_CST singleton. */
881 static inline bool
882 range_int_cst_singleton_p (value_range_t *vr)
884 return (range_int_cst_p (vr)
885 && !TREE_OVERFLOW (vr->min)
886 && !TREE_OVERFLOW (vr->max)
887 && tree_int_cst_equal (vr->min, vr->max));
890 /* Return true if value range VR involves at least one symbol. */
892 static inline bool
893 symbolic_range_p (value_range_t *vr)
895 return (!is_gimple_min_invariant (vr->min)
896 || !is_gimple_min_invariant (vr->max));
899 /* Return true if value range VR uses an overflow infinity. */
901 static inline bool
902 overflow_infinity_range_p (value_range_t *vr)
904 return (vr->type == VR_RANGE
905 && (is_overflow_infinity (vr->min)
906 || is_overflow_infinity (vr->max)));
909 /* Return false if we can not make a valid comparison based on VR;
910 this will be the case if it uses an overflow infinity and overflow
911 is not undefined (i.e., -fno-strict-overflow is in effect).
912 Otherwise return true, and set *STRICT_OVERFLOW_P to true if VR
913 uses an overflow infinity. */
915 static bool
916 usable_range_p (value_range_t *vr, bool *strict_overflow_p)
918 gcc_assert (vr->type == VR_RANGE);
919 if (is_overflow_infinity (vr->min))
921 *strict_overflow_p = true;
922 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (vr->min)))
923 return false;
925 if (is_overflow_infinity (vr->max))
927 *strict_overflow_p = true;
928 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (vr->max)))
929 return false;
931 return true;
935 /* Return true if the result of assignment STMT is know to be non-negative.
936 If the return value is based on the assumption that signed overflow is
937 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
938 *STRICT_OVERFLOW_P.*/
940 static bool
941 gimple_assign_nonnegative_warnv_p (gimple stmt, bool *strict_overflow_p)
943 enum tree_code code = gimple_assign_rhs_code (stmt);
944 switch (get_gimple_rhs_class (code))
946 case GIMPLE_UNARY_RHS:
947 return tree_unary_nonnegative_warnv_p (gimple_assign_rhs_code (stmt),
948 gimple_expr_type (stmt),
949 gimple_assign_rhs1 (stmt),
950 strict_overflow_p);
951 case GIMPLE_BINARY_RHS:
952 return tree_binary_nonnegative_warnv_p (gimple_assign_rhs_code (stmt),
953 gimple_expr_type (stmt),
954 gimple_assign_rhs1 (stmt),
955 gimple_assign_rhs2 (stmt),
956 strict_overflow_p);
957 case GIMPLE_TERNARY_RHS:
958 return false;
959 case GIMPLE_SINGLE_RHS:
960 return tree_single_nonnegative_warnv_p (gimple_assign_rhs1 (stmt),
961 strict_overflow_p);
962 case GIMPLE_INVALID_RHS:
963 gcc_unreachable ();
964 default:
965 gcc_unreachable ();
969 /* Return true if return value of call STMT is know to be non-negative.
970 If the return value is based on the assumption that signed overflow is
971 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
972 *STRICT_OVERFLOW_P.*/
974 static bool
975 gimple_call_nonnegative_warnv_p (gimple stmt, bool *strict_overflow_p)
977 tree arg0 = gimple_call_num_args (stmt) > 0 ?
978 gimple_call_arg (stmt, 0) : NULL_TREE;
979 tree arg1 = gimple_call_num_args (stmt) > 1 ?
980 gimple_call_arg (stmt, 1) : NULL_TREE;
982 return tree_call_nonnegative_warnv_p (gimple_expr_type (stmt),
983 gimple_call_fndecl (stmt),
984 arg0,
985 arg1,
986 strict_overflow_p);
989 /* Return true if STMT is know to to compute a non-negative value.
990 If the return value is based on the assumption that signed overflow is
991 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
992 *STRICT_OVERFLOW_P.*/
994 static bool
995 gimple_stmt_nonnegative_warnv_p (gimple stmt, bool *strict_overflow_p)
997 switch (gimple_code (stmt))
999 case GIMPLE_ASSIGN:
1000 return gimple_assign_nonnegative_warnv_p (stmt, strict_overflow_p);
1001 case GIMPLE_CALL:
1002 return gimple_call_nonnegative_warnv_p (stmt, strict_overflow_p);
1003 default:
1004 gcc_unreachable ();
1008 /* Return true if the result of assignment STMT is know to be non-zero.
1009 If the return value is based on the assumption that signed overflow is
1010 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1011 *STRICT_OVERFLOW_P.*/
1013 static bool
1014 gimple_assign_nonzero_warnv_p (gimple stmt, bool *strict_overflow_p)
1016 enum tree_code code = gimple_assign_rhs_code (stmt);
1017 switch (get_gimple_rhs_class (code))
1019 case GIMPLE_UNARY_RHS:
1020 return tree_unary_nonzero_warnv_p (gimple_assign_rhs_code (stmt),
1021 gimple_expr_type (stmt),
1022 gimple_assign_rhs1 (stmt),
1023 strict_overflow_p);
1024 case GIMPLE_BINARY_RHS:
1025 return tree_binary_nonzero_warnv_p (gimple_assign_rhs_code (stmt),
1026 gimple_expr_type (stmt),
1027 gimple_assign_rhs1 (stmt),
1028 gimple_assign_rhs2 (stmt),
1029 strict_overflow_p);
1030 case GIMPLE_TERNARY_RHS:
1031 return false;
1032 case GIMPLE_SINGLE_RHS:
1033 return tree_single_nonzero_warnv_p (gimple_assign_rhs1 (stmt),
1034 strict_overflow_p);
1035 case GIMPLE_INVALID_RHS:
1036 gcc_unreachable ();
1037 default:
1038 gcc_unreachable ();
1042 /* Return true if STMT is known to compute a non-zero value.
1043 If the return value is based on the assumption that signed overflow is
1044 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1045 *STRICT_OVERFLOW_P.*/
1047 static bool
1048 gimple_stmt_nonzero_warnv_p (gimple stmt, bool *strict_overflow_p)
1050 switch (gimple_code (stmt))
1052 case GIMPLE_ASSIGN:
1053 return gimple_assign_nonzero_warnv_p (stmt, strict_overflow_p);
1054 case GIMPLE_CALL:
1056 tree fndecl = gimple_call_fndecl (stmt);
1057 if (!fndecl) return false;
1058 if (flag_delete_null_pointer_checks && !flag_check_new
1059 && DECL_IS_OPERATOR_NEW (fndecl)
1060 && !TREE_NOTHROW (fndecl))
1061 return true;
1062 if (flag_delete_null_pointer_checks &&
1063 lookup_attribute ("returns_nonnull",
1064 TYPE_ATTRIBUTES (gimple_call_fntype (stmt))))
1065 return true;
1066 return gimple_alloca_call_p (stmt);
1068 default:
1069 gcc_unreachable ();
1073 /* Like tree_expr_nonzero_warnv_p, but this function uses value ranges
1074 obtained so far. */
1076 static bool
1077 vrp_stmt_computes_nonzero (gimple stmt, bool *strict_overflow_p)
1079 if (gimple_stmt_nonzero_warnv_p (stmt, strict_overflow_p))
1080 return true;
1082 /* If we have an expression of the form &X->a, then the expression
1083 is nonnull if X is nonnull. */
1084 if (is_gimple_assign (stmt)
1085 && gimple_assign_rhs_code (stmt) == ADDR_EXPR)
1087 tree expr = gimple_assign_rhs1 (stmt);
1088 tree base = get_base_address (TREE_OPERAND (expr, 0));
1090 if (base != NULL_TREE
1091 && TREE_CODE (base) == MEM_REF
1092 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
1094 value_range_t *vr = get_value_range (TREE_OPERAND (base, 0));
1095 if (range_is_nonnull (vr))
1096 return true;
1100 return false;
1103 /* Returns true if EXPR is a valid value (as expected by compare_values) --
1104 a gimple invariant, or SSA_NAME +- CST. */
1106 static bool
1107 valid_value_p (tree expr)
1109 if (TREE_CODE (expr) == SSA_NAME)
1110 return true;
1112 if (TREE_CODE (expr) == PLUS_EXPR
1113 || TREE_CODE (expr) == MINUS_EXPR)
1114 return (TREE_CODE (TREE_OPERAND (expr, 0)) == SSA_NAME
1115 && TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST);
1117 return is_gimple_min_invariant (expr);
1120 /* Return
1121 1 if VAL < VAL2
1122 0 if !(VAL < VAL2)
1123 -2 if those are incomparable. */
1124 static inline int
1125 operand_less_p (tree val, tree val2)
1127 /* LT is folded faster than GE and others. Inline the common case. */
1128 if (TREE_CODE (val) == INTEGER_CST && TREE_CODE (val2) == INTEGER_CST)
1130 if (TYPE_UNSIGNED (TREE_TYPE (val)))
1131 return INT_CST_LT_UNSIGNED (val, val2);
1132 else
1134 if (INT_CST_LT (val, val2))
1135 return 1;
1138 else
1140 tree tcmp;
1142 fold_defer_overflow_warnings ();
1144 tcmp = fold_binary_to_constant (LT_EXPR, boolean_type_node, val, val2);
1146 fold_undefer_and_ignore_overflow_warnings ();
1148 if (!tcmp
1149 || TREE_CODE (tcmp) != INTEGER_CST)
1150 return -2;
1152 if (!integer_zerop (tcmp))
1153 return 1;
1156 /* val >= val2, not considering overflow infinity. */
1157 if (is_negative_overflow_infinity (val))
1158 return is_negative_overflow_infinity (val2) ? 0 : 1;
1159 else if (is_positive_overflow_infinity (val2))
1160 return is_positive_overflow_infinity (val) ? 0 : 1;
1162 return 0;
1165 /* Compare two values VAL1 and VAL2. Return
1167 -2 if VAL1 and VAL2 cannot be compared at compile-time,
1168 -1 if VAL1 < VAL2,
1169 0 if VAL1 == VAL2,
1170 +1 if VAL1 > VAL2, and
1171 +2 if VAL1 != VAL2
1173 This is similar to tree_int_cst_compare but supports pointer values
1174 and values that cannot be compared at compile time.
1176 If STRICT_OVERFLOW_P is not NULL, then set *STRICT_OVERFLOW_P to
1177 true if the return value is only valid if we assume that signed
1178 overflow is undefined. */
1180 static int
1181 compare_values_warnv (tree val1, tree val2, bool *strict_overflow_p)
1183 if (val1 == val2)
1184 return 0;
1186 /* Below we rely on the fact that VAL1 and VAL2 are both pointers or
1187 both integers. */
1188 gcc_assert (POINTER_TYPE_P (TREE_TYPE (val1))
1189 == POINTER_TYPE_P (TREE_TYPE (val2)));
1190 /* Convert the two values into the same type. This is needed because
1191 sizetype causes sign extension even for unsigned types. */
1192 val2 = fold_convert (TREE_TYPE (val1), val2);
1193 STRIP_USELESS_TYPE_CONVERSION (val2);
1195 if ((TREE_CODE (val1) == SSA_NAME
1196 || TREE_CODE (val1) == PLUS_EXPR
1197 || TREE_CODE (val1) == MINUS_EXPR)
1198 && (TREE_CODE (val2) == SSA_NAME
1199 || TREE_CODE (val2) == PLUS_EXPR
1200 || TREE_CODE (val2) == MINUS_EXPR))
1202 tree n1, c1, n2, c2;
1203 enum tree_code code1, code2;
1205 /* If VAL1 and VAL2 are of the form 'NAME [+-] CST' or 'NAME',
1206 return -1 or +1 accordingly. If VAL1 and VAL2 don't use the
1207 same name, return -2. */
1208 if (TREE_CODE (val1) == SSA_NAME)
1210 code1 = SSA_NAME;
1211 n1 = val1;
1212 c1 = NULL_TREE;
1214 else
1216 code1 = TREE_CODE (val1);
1217 n1 = TREE_OPERAND (val1, 0);
1218 c1 = TREE_OPERAND (val1, 1);
1219 if (tree_int_cst_sgn (c1) == -1)
1221 if (is_negative_overflow_infinity (c1))
1222 return -2;
1223 c1 = fold_unary_to_constant (NEGATE_EXPR, TREE_TYPE (c1), c1);
1224 if (!c1)
1225 return -2;
1226 code1 = code1 == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR;
1230 if (TREE_CODE (val2) == SSA_NAME)
1232 code2 = SSA_NAME;
1233 n2 = val2;
1234 c2 = NULL_TREE;
1236 else
1238 code2 = TREE_CODE (val2);
1239 n2 = TREE_OPERAND (val2, 0);
1240 c2 = TREE_OPERAND (val2, 1);
1241 if (tree_int_cst_sgn (c2) == -1)
1243 if (is_negative_overflow_infinity (c2))
1244 return -2;
1245 c2 = fold_unary_to_constant (NEGATE_EXPR, TREE_TYPE (c2), c2);
1246 if (!c2)
1247 return -2;
1248 code2 = code2 == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR;
1252 /* Both values must use the same name. */
1253 if (n1 != n2)
1254 return -2;
1256 if (code1 == SSA_NAME
1257 && code2 == SSA_NAME)
1258 /* NAME == NAME */
1259 return 0;
1261 /* If overflow is defined we cannot simplify more. */
1262 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (val1)))
1263 return -2;
1265 if (strict_overflow_p != NULL
1266 && (code1 == SSA_NAME || !TREE_NO_WARNING (val1))
1267 && (code2 == SSA_NAME || !TREE_NO_WARNING (val2)))
1268 *strict_overflow_p = true;
1270 if (code1 == SSA_NAME)
1272 if (code2 == PLUS_EXPR)
1273 /* NAME < NAME + CST */
1274 return -1;
1275 else if (code2 == MINUS_EXPR)
1276 /* NAME > NAME - CST */
1277 return 1;
1279 else if (code1 == PLUS_EXPR)
1281 if (code2 == SSA_NAME)
1282 /* NAME + CST > NAME */
1283 return 1;
1284 else if (code2 == PLUS_EXPR)
1285 /* NAME + CST1 > NAME + CST2, if CST1 > CST2 */
1286 return compare_values_warnv (c1, c2, strict_overflow_p);
1287 else if (code2 == MINUS_EXPR)
1288 /* NAME + CST1 > NAME - CST2 */
1289 return 1;
1291 else if (code1 == MINUS_EXPR)
1293 if (code2 == SSA_NAME)
1294 /* NAME - CST < NAME */
1295 return -1;
1296 else if (code2 == PLUS_EXPR)
1297 /* NAME - CST1 < NAME + CST2 */
1298 return -1;
1299 else if (code2 == MINUS_EXPR)
1300 /* NAME - CST1 > NAME - CST2, if CST1 < CST2. Notice that
1301 C1 and C2 are swapped in the call to compare_values. */
1302 return compare_values_warnv (c2, c1, strict_overflow_p);
1305 gcc_unreachable ();
1308 /* We cannot compare non-constants. */
1309 if (!is_gimple_min_invariant (val1) || !is_gimple_min_invariant (val2))
1310 return -2;
1312 if (!POINTER_TYPE_P (TREE_TYPE (val1)))
1314 /* We cannot compare overflowed values, except for overflow
1315 infinities. */
1316 if (TREE_OVERFLOW (val1) || TREE_OVERFLOW (val2))
1318 if (strict_overflow_p != NULL)
1319 *strict_overflow_p = true;
1320 if (is_negative_overflow_infinity (val1))
1321 return is_negative_overflow_infinity (val2) ? 0 : -1;
1322 else if (is_negative_overflow_infinity (val2))
1323 return 1;
1324 else if (is_positive_overflow_infinity (val1))
1325 return is_positive_overflow_infinity (val2) ? 0 : 1;
1326 else if (is_positive_overflow_infinity (val2))
1327 return -1;
1328 return -2;
1331 return tree_int_cst_compare (val1, val2);
1333 else
1335 tree t;
1337 /* First see if VAL1 and VAL2 are not the same. */
1338 if (val1 == val2 || operand_equal_p (val1, val2, 0))
1339 return 0;
1341 /* If VAL1 is a lower address than VAL2, return -1. */
1342 if (operand_less_p (val1, val2) == 1)
1343 return -1;
1345 /* If VAL1 is a higher address than VAL2, return +1. */
1346 if (operand_less_p (val2, val1) == 1)
1347 return 1;
1349 /* If VAL1 is different than VAL2, return +2.
1350 For integer constants we either have already returned -1 or 1
1351 or they are equivalent. We still might succeed in proving
1352 something about non-trivial operands. */
1353 if (TREE_CODE (val1) != INTEGER_CST
1354 || TREE_CODE (val2) != INTEGER_CST)
1356 t = fold_binary_to_constant (NE_EXPR, boolean_type_node, val1, val2);
1357 if (t && integer_onep (t))
1358 return 2;
1361 return -2;
1365 /* Compare values like compare_values_warnv, but treat comparisons of
1366 nonconstants which rely on undefined overflow as incomparable. */
1368 static int
1369 compare_values (tree val1, tree val2)
1371 bool sop;
1372 int ret;
1374 sop = false;
1375 ret = compare_values_warnv (val1, val2, &sop);
1376 if (sop
1377 && (!is_gimple_min_invariant (val1) || !is_gimple_min_invariant (val2)))
1378 ret = -2;
1379 return ret;
1383 /* Return 1 if VAL is inside value range MIN <= VAL <= MAX,
1384 0 if VAL is not inside [MIN, MAX],
1385 -2 if we cannot tell either way.
1387 Benchmark compile/20001226-1.c compilation time after changing this
1388 function. */
1390 static inline int
1391 value_inside_range (tree val, tree min, tree max)
1393 int cmp1, cmp2;
1395 cmp1 = operand_less_p (val, min);
1396 if (cmp1 == -2)
1397 return -2;
1398 if (cmp1 == 1)
1399 return 0;
1401 cmp2 = operand_less_p (max, val);
1402 if (cmp2 == -2)
1403 return -2;
1405 return !cmp2;
1409 /* Return true if value ranges VR0 and VR1 have a non-empty
1410 intersection.
1412 Benchmark compile/20001226-1.c compilation time after changing this
1413 function.
1416 static inline bool
1417 value_ranges_intersect_p (value_range_t *vr0, value_range_t *vr1)
1419 /* The value ranges do not intersect if the maximum of the first range is
1420 less than the minimum of the second range or vice versa.
1421 When those relations are unknown, we can't do any better. */
1422 if (operand_less_p (vr0->max, vr1->min) != 0)
1423 return false;
1424 if (operand_less_p (vr1->max, vr0->min) != 0)
1425 return false;
1426 return true;
1430 /* Return 1 if [MIN, MAX] includes the value zero, 0 if it does not
1431 include the value zero, -2 if we cannot tell. */
1433 static inline int
1434 range_includes_zero_p (tree min, tree max)
1436 tree zero = build_int_cst (TREE_TYPE (min), 0);
1437 return value_inside_range (zero, min, max);
1440 /* Return true if *VR is know to only contain nonnegative values. */
1442 static inline bool
1443 value_range_nonnegative_p (value_range_t *vr)
1445 /* Testing for VR_ANTI_RANGE is not useful here as any anti-range
1446 which would return a useful value should be encoded as a
1447 VR_RANGE. */
1448 if (vr->type == VR_RANGE)
1450 int result = compare_values (vr->min, integer_zero_node);
1451 return (result == 0 || result == 1);
1454 return false;
1457 /* Return true if T, an SSA_NAME, is known to be nonnegative. Return
1458 false otherwise or if no value range information is available. */
1460 bool
1461 ssa_name_nonnegative_p (const_tree t)
1463 value_range_t *vr = get_value_range (t);
1465 if (INTEGRAL_TYPE_P (t)
1466 && TYPE_UNSIGNED (t))
1467 return true;
1469 if (!vr)
1470 return false;
1472 return value_range_nonnegative_p (vr);
1475 /* If *VR has a value rante that is a single constant value return that,
1476 otherwise return NULL_TREE. */
1478 static tree
1479 value_range_constant_singleton (value_range_t *vr)
1481 if (vr->type == VR_RANGE
1482 && operand_equal_p (vr->min, vr->max, 0)
1483 && is_gimple_min_invariant (vr->min))
1484 return vr->min;
1486 return NULL_TREE;
1489 /* If OP has a value range with a single constant value return that,
1490 otherwise return NULL_TREE. This returns OP itself if OP is a
1491 constant. */
1493 static tree
1494 op_with_constant_singleton_value_range (tree op)
1496 if (is_gimple_min_invariant (op))
1497 return op;
1499 if (TREE_CODE (op) != SSA_NAME)
1500 return NULL_TREE;
1502 return value_range_constant_singleton (get_value_range (op));
1505 /* Return true if op is in a boolean [0, 1] value-range. */
1507 static bool
1508 op_with_boolean_value_range_p (tree op)
1510 value_range_t *vr;
1512 if (TYPE_PRECISION (TREE_TYPE (op)) == 1)
1513 return true;
1515 if (integer_zerop (op)
1516 || integer_onep (op))
1517 return true;
1519 if (TREE_CODE (op) != SSA_NAME)
1520 return false;
1522 vr = get_value_range (op);
1523 return (vr->type == VR_RANGE
1524 && integer_zerop (vr->min)
1525 && integer_onep (vr->max));
1528 /* Extract value range information from an ASSERT_EXPR EXPR and store
1529 it in *VR_P. */
1531 static void
1532 extract_range_from_assert (value_range_t *vr_p, tree expr)
1534 tree var, cond, limit, min, max, type;
1535 value_range_t *limit_vr;
1536 enum tree_code cond_code;
1538 var = ASSERT_EXPR_VAR (expr);
1539 cond = ASSERT_EXPR_COND (expr);
1541 gcc_assert (COMPARISON_CLASS_P (cond));
1543 /* Find VAR in the ASSERT_EXPR conditional. */
1544 if (var == TREE_OPERAND (cond, 0)
1545 || TREE_CODE (TREE_OPERAND (cond, 0)) == PLUS_EXPR
1546 || TREE_CODE (TREE_OPERAND (cond, 0)) == NOP_EXPR)
1548 /* If the predicate is of the form VAR COMP LIMIT, then we just
1549 take LIMIT from the RHS and use the same comparison code. */
1550 cond_code = TREE_CODE (cond);
1551 limit = TREE_OPERAND (cond, 1);
1552 cond = TREE_OPERAND (cond, 0);
1554 else
1556 /* If the predicate is of the form LIMIT COMP VAR, then we need
1557 to flip around the comparison code to create the proper range
1558 for VAR. */
1559 cond_code = swap_tree_comparison (TREE_CODE (cond));
1560 limit = TREE_OPERAND (cond, 0);
1561 cond = TREE_OPERAND (cond, 1);
1564 limit = avoid_overflow_infinity (limit);
1566 type = TREE_TYPE (var);
1567 gcc_assert (limit != var);
1569 /* For pointer arithmetic, we only keep track of pointer equality
1570 and inequality. */
1571 if (POINTER_TYPE_P (type) && cond_code != NE_EXPR && cond_code != EQ_EXPR)
1573 set_value_range_to_varying (vr_p);
1574 return;
1577 /* If LIMIT is another SSA name and LIMIT has a range of its own,
1578 try to use LIMIT's range to avoid creating symbolic ranges
1579 unnecessarily. */
1580 limit_vr = (TREE_CODE (limit) == SSA_NAME) ? get_value_range (limit) : NULL;
1582 /* LIMIT's range is only interesting if it has any useful information. */
1583 if (limit_vr
1584 && (limit_vr->type == VR_UNDEFINED
1585 || limit_vr->type == VR_VARYING
1586 || symbolic_range_p (limit_vr)))
1587 limit_vr = NULL;
1589 /* Initially, the new range has the same set of equivalences of
1590 VAR's range. This will be revised before returning the final
1591 value. Since assertions may be chained via mutually exclusive
1592 predicates, we will need to trim the set of equivalences before
1593 we are done. */
1594 gcc_assert (vr_p->equiv == NULL);
1595 add_equivalence (&vr_p->equiv, var);
1597 /* Extract a new range based on the asserted comparison for VAR and
1598 LIMIT's value range. Notice that if LIMIT has an anti-range, we
1599 will only use it for equality comparisons (EQ_EXPR). For any
1600 other kind of assertion, we cannot derive a range from LIMIT's
1601 anti-range that can be used to describe the new range. For
1602 instance, ASSERT_EXPR <x_2, x_2 <= b_4>. If b_4 is ~[2, 10],
1603 then b_4 takes on the ranges [-INF, 1] and [11, +INF]. There is
1604 no single range for x_2 that could describe LE_EXPR, so we might
1605 as well build the range [b_4, +INF] for it.
1606 One special case we handle is extracting a range from a
1607 range test encoded as (unsigned)var + CST <= limit. */
1608 if (TREE_CODE (cond) == NOP_EXPR
1609 || TREE_CODE (cond) == PLUS_EXPR)
1611 if (TREE_CODE (cond) == PLUS_EXPR)
1613 min = fold_build1 (NEGATE_EXPR, TREE_TYPE (TREE_OPERAND (cond, 1)),
1614 TREE_OPERAND (cond, 1));
1615 max = int_const_binop (PLUS_EXPR, limit, min);
1616 cond = TREE_OPERAND (cond, 0);
1618 else
1620 min = build_int_cst (TREE_TYPE (var), 0);
1621 max = limit;
1624 /* Make sure to not set TREE_OVERFLOW on the final type
1625 conversion. We are willingly interpreting large positive
1626 unsigned values as negative singed values here. */
1627 min = force_fit_type_double (TREE_TYPE (var), tree_to_double_int (min),
1628 0, false);
1629 max = force_fit_type_double (TREE_TYPE (var), tree_to_double_int (max),
1630 0, false);
1632 /* We can transform a max, min range to an anti-range or
1633 vice-versa. Use set_and_canonicalize_value_range which does
1634 this for us. */
1635 if (cond_code == LE_EXPR)
1636 set_and_canonicalize_value_range (vr_p, VR_RANGE,
1637 min, max, vr_p->equiv);
1638 else if (cond_code == GT_EXPR)
1639 set_and_canonicalize_value_range (vr_p, VR_ANTI_RANGE,
1640 min, max, vr_p->equiv);
1641 else
1642 gcc_unreachable ();
1644 else if (cond_code == EQ_EXPR)
1646 enum value_range_type range_type;
1648 if (limit_vr)
1650 range_type = limit_vr->type;
1651 min = limit_vr->min;
1652 max = limit_vr->max;
1654 else
1656 range_type = VR_RANGE;
1657 min = limit;
1658 max = limit;
1661 set_value_range (vr_p, range_type, min, max, vr_p->equiv);
1663 /* When asserting the equality VAR == LIMIT and LIMIT is another
1664 SSA name, the new range will also inherit the equivalence set
1665 from LIMIT. */
1666 if (TREE_CODE (limit) == SSA_NAME)
1667 add_equivalence (&vr_p->equiv, limit);
1669 else if (cond_code == NE_EXPR)
1671 /* As described above, when LIMIT's range is an anti-range and
1672 this assertion is an inequality (NE_EXPR), then we cannot
1673 derive anything from the anti-range. For instance, if
1674 LIMIT's range was ~[0, 0], the assertion 'VAR != LIMIT' does
1675 not imply that VAR's range is [0, 0]. So, in the case of
1676 anti-ranges, we just assert the inequality using LIMIT and
1677 not its anti-range.
1679 If LIMIT_VR is a range, we can only use it to build a new
1680 anti-range if LIMIT_VR is a single-valued range. For
1681 instance, if LIMIT_VR is [0, 1], the predicate
1682 VAR != [0, 1] does not mean that VAR's range is ~[0, 1].
1683 Rather, it means that for value 0 VAR should be ~[0, 0]
1684 and for value 1, VAR should be ~[1, 1]. We cannot
1685 represent these ranges.
1687 The only situation in which we can build a valid
1688 anti-range is when LIMIT_VR is a single-valued range
1689 (i.e., LIMIT_VR->MIN == LIMIT_VR->MAX). In that case,
1690 build the anti-range ~[LIMIT_VR->MIN, LIMIT_VR->MAX]. */
1691 if (limit_vr
1692 && limit_vr->type == VR_RANGE
1693 && compare_values (limit_vr->min, limit_vr->max) == 0)
1695 min = limit_vr->min;
1696 max = limit_vr->max;
1698 else
1700 /* In any other case, we cannot use LIMIT's range to build a
1701 valid anti-range. */
1702 min = max = limit;
1705 /* If MIN and MAX cover the whole range for their type, then
1706 just use the original LIMIT. */
1707 if (INTEGRAL_TYPE_P (type)
1708 && vrp_val_is_min (min)
1709 && vrp_val_is_max (max))
1710 min = max = limit;
1712 set_and_canonicalize_value_range (vr_p, VR_ANTI_RANGE,
1713 min, max, vr_p->equiv);
1715 else if (cond_code == LE_EXPR || cond_code == LT_EXPR)
1717 min = TYPE_MIN_VALUE (type);
1719 if (limit_vr == NULL || limit_vr->type == VR_ANTI_RANGE)
1720 max = limit;
1721 else
1723 /* If LIMIT_VR is of the form [N1, N2], we need to build the
1724 range [MIN, N2] for LE_EXPR and [MIN, N2 - 1] for
1725 LT_EXPR. */
1726 max = limit_vr->max;
1729 /* If the maximum value forces us to be out of bounds, simply punt.
1730 It would be pointless to try and do anything more since this
1731 all should be optimized away above us. */
1732 if ((cond_code == LT_EXPR
1733 && compare_values (max, min) == 0)
1734 || (CONSTANT_CLASS_P (max) && TREE_OVERFLOW (max)))
1735 set_value_range_to_varying (vr_p);
1736 else
1738 /* For LT_EXPR, we create the range [MIN, MAX - 1]. */
1739 if (cond_code == LT_EXPR)
1741 if (TYPE_PRECISION (TREE_TYPE (max)) == 1
1742 && !TYPE_UNSIGNED (TREE_TYPE (max)))
1743 max = fold_build2 (PLUS_EXPR, TREE_TYPE (max), max,
1744 build_int_cst (TREE_TYPE (max), -1));
1745 else
1746 max = fold_build2 (MINUS_EXPR, TREE_TYPE (max), max,
1747 build_int_cst (TREE_TYPE (max), 1));
1748 if (EXPR_P (max))
1749 TREE_NO_WARNING (max) = 1;
1752 set_value_range (vr_p, VR_RANGE, min, max, vr_p->equiv);
1755 else if (cond_code == GE_EXPR || cond_code == GT_EXPR)
1757 max = TYPE_MAX_VALUE (type);
1759 if (limit_vr == NULL || limit_vr->type == VR_ANTI_RANGE)
1760 min = limit;
1761 else
1763 /* If LIMIT_VR is of the form [N1, N2], we need to build the
1764 range [N1, MAX] for GE_EXPR and [N1 + 1, MAX] for
1765 GT_EXPR. */
1766 min = limit_vr->min;
1769 /* If the minimum value forces us to be out of bounds, simply punt.
1770 It would be pointless to try and do anything more since this
1771 all should be optimized away above us. */
1772 if ((cond_code == GT_EXPR
1773 && compare_values (min, max) == 0)
1774 || (CONSTANT_CLASS_P (min) && TREE_OVERFLOW (min)))
1775 set_value_range_to_varying (vr_p);
1776 else
1778 /* For GT_EXPR, we create the range [MIN + 1, MAX]. */
1779 if (cond_code == GT_EXPR)
1781 if (TYPE_PRECISION (TREE_TYPE (min)) == 1
1782 && !TYPE_UNSIGNED (TREE_TYPE (min)))
1783 min = fold_build2 (MINUS_EXPR, TREE_TYPE (min), min,
1784 build_int_cst (TREE_TYPE (min), -1));
1785 else
1786 min = fold_build2 (PLUS_EXPR, TREE_TYPE (min), min,
1787 build_int_cst (TREE_TYPE (min), 1));
1788 if (EXPR_P (min))
1789 TREE_NO_WARNING (min) = 1;
1792 set_value_range (vr_p, VR_RANGE, min, max, vr_p->equiv);
1795 else
1796 gcc_unreachable ();
1798 /* Finally intersect the new range with what we already know about var. */
1799 vrp_intersect_ranges (vr_p, get_value_range (var));
1803 /* Extract range information from SSA name VAR and store it in VR. If
1804 VAR has an interesting range, use it. Otherwise, create the
1805 range [VAR, VAR] and return it. This is useful in situations where
1806 we may have conditionals testing values of VARYING names. For
1807 instance,
1809 x_3 = y_5;
1810 if (x_3 > y_5)
1813 Even if y_5 is deemed VARYING, we can determine that x_3 > y_5 is
1814 always false. */
1816 static void
1817 extract_range_from_ssa_name (value_range_t *vr, tree var)
1819 value_range_t *var_vr = get_value_range (var);
1821 if (var_vr->type != VR_UNDEFINED && var_vr->type != VR_VARYING)
1822 copy_value_range (vr, var_vr);
1823 else
1824 set_value_range (vr, VR_RANGE, var, var, NULL);
1826 add_equivalence (&vr->equiv, var);
1830 /* Wrapper around int_const_binop. If the operation overflows and we
1831 are not using wrapping arithmetic, then adjust the result to be
1832 -INF or +INF depending on CODE, VAL1 and VAL2. This can return
1833 NULL_TREE if we need to use an overflow infinity representation but
1834 the type does not support it. */
1836 static tree
1837 vrp_int_const_binop (enum tree_code code, tree val1, tree val2)
1839 tree res;
1841 res = int_const_binop (code, val1, val2);
1843 /* If we are using unsigned arithmetic, operate symbolically
1844 on -INF and +INF as int_const_binop only handles signed overflow. */
1845 if (TYPE_UNSIGNED (TREE_TYPE (val1)))
1847 int checkz = compare_values (res, val1);
1848 bool overflow = false;
1850 /* Ensure that res = val1 [+*] val2 >= val1
1851 or that res = val1 - val2 <= val1. */
1852 if ((code == PLUS_EXPR
1853 && !(checkz == 1 || checkz == 0))
1854 || (code == MINUS_EXPR
1855 && !(checkz == 0 || checkz == -1)))
1857 overflow = true;
1859 /* Checking for multiplication overflow is done by dividing the
1860 output of the multiplication by the first input of the
1861 multiplication. If the result of that division operation is
1862 not equal to the second input of the multiplication, then the
1863 multiplication overflowed. */
1864 else if (code == MULT_EXPR && !integer_zerop (val1))
1866 tree tmp = int_const_binop (TRUNC_DIV_EXPR,
1867 res,
1868 val1);
1869 int check = compare_values (tmp, val2);
1871 if (check != 0)
1872 overflow = true;
1875 if (overflow)
1877 res = copy_node (res);
1878 TREE_OVERFLOW (res) = 1;
1882 else if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (val1)))
1883 /* If the singed operation wraps then int_const_binop has done
1884 everything we want. */
1886 else if ((TREE_OVERFLOW (res)
1887 && !TREE_OVERFLOW (val1)
1888 && !TREE_OVERFLOW (val2))
1889 || is_overflow_infinity (val1)
1890 || is_overflow_infinity (val2))
1892 /* If the operation overflowed but neither VAL1 nor VAL2 are
1893 overflown, return -INF or +INF depending on the operation
1894 and the combination of signs of the operands. */
1895 int sgn1 = tree_int_cst_sgn (val1);
1896 int sgn2 = tree_int_cst_sgn (val2);
1898 if (needs_overflow_infinity (TREE_TYPE (res))
1899 && !supports_overflow_infinity (TREE_TYPE (res)))
1900 return NULL_TREE;
1902 /* We have to punt on adding infinities of different signs,
1903 since we can't tell what the sign of the result should be.
1904 Likewise for subtracting infinities of the same sign. */
1905 if (((code == PLUS_EXPR && sgn1 != sgn2)
1906 || (code == MINUS_EXPR && sgn1 == sgn2))
1907 && is_overflow_infinity (val1)
1908 && is_overflow_infinity (val2))
1909 return NULL_TREE;
1911 /* Don't try to handle division or shifting of infinities. */
1912 if ((code == TRUNC_DIV_EXPR
1913 || code == FLOOR_DIV_EXPR
1914 || code == CEIL_DIV_EXPR
1915 || code == EXACT_DIV_EXPR
1916 || code == ROUND_DIV_EXPR
1917 || code == RSHIFT_EXPR)
1918 && (is_overflow_infinity (val1)
1919 || is_overflow_infinity (val2)))
1920 return NULL_TREE;
1922 /* Notice that we only need to handle the restricted set of
1923 operations handled by extract_range_from_binary_expr.
1924 Among them, only multiplication, addition and subtraction
1925 can yield overflow without overflown operands because we
1926 are working with integral types only... except in the
1927 case VAL1 = -INF and VAL2 = -1 which overflows to +INF
1928 for division too. */
1930 /* For multiplication, the sign of the overflow is given
1931 by the comparison of the signs of the operands. */
1932 if ((code == MULT_EXPR && sgn1 == sgn2)
1933 /* For addition, the operands must be of the same sign
1934 to yield an overflow. Its sign is therefore that
1935 of one of the operands, for example the first. For
1936 infinite operands X + -INF is negative, not positive. */
1937 || (code == PLUS_EXPR
1938 && (sgn1 >= 0
1939 ? !is_negative_overflow_infinity (val2)
1940 : is_positive_overflow_infinity (val2)))
1941 /* For subtraction, non-infinite operands must be of
1942 different signs to yield an overflow. Its sign is
1943 therefore that of the first operand or the opposite of
1944 that of the second operand. A first operand of 0 counts
1945 as positive here, for the corner case 0 - (-INF), which
1946 overflows, but must yield +INF. For infinite operands 0
1947 - INF is negative, not positive. */
1948 || (code == MINUS_EXPR
1949 && (sgn1 >= 0
1950 ? !is_positive_overflow_infinity (val2)
1951 : is_negative_overflow_infinity (val2)))
1952 /* We only get in here with positive shift count, so the
1953 overflow direction is the same as the sign of val1.
1954 Actually rshift does not overflow at all, but we only
1955 handle the case of shifting overflowed -INF and +INF. */
1956 || (code == RSHIFT_EXPR
1957 && sgn1 >= 0)
1958 /* For division, the only case is -INF / -1 = +INF. */
1959 || code == TRUNC_DIV_EXPR
1960 || code == FLOOR_DIV_EXPR
1961 || code == CEIL_DIV_EXPR
1962 || code == EXACT_DIV_EXPR
1963 || code == ROUND_DIV_EXPR)
1964 return (needs_overflow_infinity (TREE_TYPE (res))
1965 ? positive_overflow_infinity (TREE_TYPE (res))
1966 : TYPE_MAX_VALUE (TREE_TYPE (res)));
1967 else
1968 return (needs_overflow_infinity (TREE_TYPE (res))
1969 ? negative_overflow_infinity (TREE_TYPE (res))
1970 : TYPE_MIN_VALUE (TREE_TYPE (res)));
1973 return res;
1977 /* For range VR compute two double_int bitmasks. In *MAY_BE_NONZERO
1978 bitmask if some bit is unset, it means for all numbers in the range
1979 the bit is 0, otherwise it might be 0 or 1. In *MUST_BE_NONZERO
1980 bitmask if some bit is set, it means for all numbers in the range
1981 the bit is 1, otherwise it might be 0 or 1. */
1983 static bool
1984 zero_nonzero_bits_from_vr (value_range_t *vr,
1985 double_int *may_be_nonzero,
1986 double_int *must_be_nonzero)
1988 *may_be_nonzero = double_int_minus_one;
1989 *must_be_nonzero = double_int_zero;
1990 if (!range_int_cst_p (vr)
1991 || TREE_OVERFLOW (vr->min)
1992 || TREE_OVERFLOW (vr->max))
1993 return false;
1995 if (range_int_cst_singleton_p (vr))
1997 *may_be_nonzero = tree_to_double_int (vr->min);
1998 *must_be_nonzero = *may_be_nonzero;
2000 else if (tree_int_cst_sgn (vr->min) >= 0
2001 || tree_int_cst_sgn (vr->max) < 0)
2003 double_int dmin = tree_to_double_int (vr->min);
2004 double_int dmax = tree_to_double_int (vr->max);
2005 double_int xor_mask = dmin ^ dmax;
2006 *may_be_nonzero = dmin | dmax;
2007 *must_be_nonzero = dmin & dmax;
2008 if (xor_mask.high != 0)
2010 unsigned HOST_WIDE_INT mask
2011 = ((unsigned HOST_WIDE_INT) 1
2012 << floor_log2 (xor_mask.high)) - 1;
2013 may_be_nonzero->low = ALL_ONES;
2014 may_be_nonzero->high |= mask;
2015 must_be_nonzero->low = 0;
2016 must_be_nonzero->high &= ~mask;
2018 else if (xor_mask.low != 0)
2020 unsigned HOST_WIDE_INT mask
2021 = ((unsigned HOST_WIDE_INT) 1
2022 << floor_log2 (xor_mask.low)) - 1;
2023 may_be_nonzero->low |= mask;
2024 must_be_nonzero->low &= ~mask;
2028 return true;
2031 /* Create two value-ranges in *VR0 and *VR1 from the anti-range *AR
2032 so that *VR0 U *VR1 == *AR. Returns true if that is possible,
2033 false otherwise. If *AR can be represented with a single range
2034 *VR1 will be VR_UNDEFINED. */
2036 static bool
2037 ranges_from_anti_range (value_range_t *ar,
2038 value_range_t *vr0, value_range_t *vr1)
2040 tree type = TREE_TYPE (ar->min);
2042 vr0->type = VR_UNDEFINED;
2043 vr1->type = VR_UNDEFINED;
2045 if (ar->type != VR_ANTI_RANGE
2046 || TREE_CODE (ar->min) != INTEGER_CST
2047 || TREE_CODE (ar->max) != INTEGER_CST
2048 || !vrp_val_min (type)
2049 || !vrp_val_max (type))
2050 return false;
2052 if (!vrp_val_is_min (ar->min))
2054 vr0->type = VR_RANGE;
2055 vr0->min = vrp_val_min (type);
2056 vr0->max
2057 = double_int_to_tree (type,
2058 tree_to_double_int (ar->min) - double_int_one);
2060 if (!vrp_val_is_max (ar->max))
2062 vr1->type = VR_RANGE;
2063 vr1->min
2064 = double_int_to_tree (type,
2065 tree_to_double_int (ar->max) + double_int_one);
2066 vr1->max = vrp_val_max (type);
2068 if (vr0->type == VR_UNDEFINED)
2070 *vr0 = *vr1;
2071 vr1->type = VR_UNDEFINED;
2074 return vr0->type != VR_UNDEFINED;
2077 /* Helper to extract a value-range *VR for a multiplicative operation
2078 *VR0 CODE *VR1. */
2080 static void
2081 extract_range_from_multiplicative_op_1 (value_range_t *vr,
2082 enum tree_code code,
2083 value_range_t *vr0, value_range_t *vr1)
2085 enum value_range_type type;
2086 tree val[4];
2087 size_t i;
2088 tree min, max;
2089 bool sop;
2090 int cmp;
2092 /* Multiplications, divisions and shifts are a bit tricky to handle,
2093 depending on the mix of signs we have in the two ranges, we
2094 need to operate on different values to get the minimum and
2095 maximum values for the new range. One approach is to figure
2096 out all the variations of range combinations and do the
2097 operations.
2099 However, this involves several calls to compare_values and it
2100 is pretty convoluted. It's simpler to do the 4 operations
2101 (MIN0 OP MIN1, MIN0 OP MAX1, MAX0 OP MIN1 and MAX0 OP MAX0 OP
2102 MAX1) and then figure the smallest and largest values to form
2103 the new range. */
2104 gcc_assert (code == MULT_EXPR
2105 || code == TRUNC_DIV_EXPR
2106 || code == FLOOR_DIV_EXPR
2107 || code == CEIL_DIV_EXPR
2108 || code == EXACT_DIV_EXPR
2109 || code == ROUND_DIV_EXPR
2110 || code == RSHIFT_EXPR
2111 || code == LSHIFT_EXPR);
2112 gcc_assert ((vr0->type == VR_RANGE
2113 || (code == MULT_EXPR && vr0->type == VR_ANTI_RANGE))
2114 && vr0->type == vr1->type);
2116 type = vr0->type;
2118 /* Compute the 4 cross operations. */
2119 sop = false;
2120 val[0] = vrp_int_const_binop (code, vr0->min, vr1->min);
2121 if (val[0] == NULL_TREE)
2122 sop = true;
2124 if (vr1->max == vr1->min)
2125 val[1] = NULL_TREE;
2126 else
2128 val[1] = vrp_int_const_binop (code, vr0->min, vr1->max);
2129 if (val[1] == NULL_TREE)
2130 sop = true;
2133 if (vr0->max == vr0->min)
2134 val[2] = NULL_TREE;
2135 else
2137 val[2] = vrp_int_const_binop (code, vr0->max, vr1->min);
2138 if (val[2] == NULL_TREE)
2139 sop = true;
2142 if (vr0->min == vr0->max || vr1->min == vr1->max)
2143 val[3] = NULL_TREE;
2144 else
2146 val[3] = vrp_int_const_binop (code, vr0->max, vr1->max);
2147 if (val[3] == NULL_TREE)
2148 sop = true;
2151 if (sop)
2153 set_value_range_to_varying (vr);
2154 return;
2157 /* Set MIN to the minimum of VAL[i] and MAX to the maximum
2158 of VAL[i]. */
2159 min = val[0];
2160 max = val[0];
2161 for (i = 1; i < 4; i++)
2163 if (!is_gimple_min_invariant (min)
2164 || (TREE_OVERFLOW (min) && !is_overflow_infinity (min))
2165 || !is_gimple_min_invariant (max)
2166 || (TREE_OVERFLOW (max) && !is_overflow_infinity (max)))
2167 break;
2169 if (val[i])
2171 if (!is_gimple_min_invariant (val[i])
2172 || (TREE_OVERFLOW (val[i])
2173 && !is_overflow_infinity (val[i])))
2175 /* If we found an overflowed value, set MIN and MAX
2176 to it so that we set the resulting range to
2177 VARYING. */
2178 min = max = val[i];
2179 break;
2182 if (compare_values (val[i], min) == -1)
2183 min = val[i];
2185 if (compare_values (val[i], max) == 1)
2186 max = val[i];
2190 /* If either MIN or MAX overflowed, then set the resulting range to
2191 VARYING. But we do accept an overflow infinity
2192 representation. */
2193 if (min == NULL_TREE
2194 || !is_gimple_min_invariant (min)
2195 || (TREE_OVERFLOW (min) && !is_overflow_infinity (min))
2196 || max == NULL_TREE
2197 || !is_gimple_min_invariant (max)
2198 || (TREE_OVERFLOW (max) && !is_overflow_infinity (max)))
2200 set_value_range_to_varying (vr);
2201 return;
2204 /* We punt if:
2205 1) [-INF, +INF]
2206 2) [-INF, +-INF(OVF)]
2207 3) [+-INF(OVF), +INF]
2208 4) [+-INF(OVF), +-INF(OVF)]
2209 We learn nothing when we have INF and INF(OVF) on both sides.
2210 Note that we do accept [-INF, -INF] and [+INF, +INF] without
2211 overflow. */
2212 if ((vrp_val_is_min (min) || is_overflow_infinity (min))
2213 && (vrp_val_is_max (max) || is_overflow_infinity (max)))
2215 set_value_range_to_varying (vr);
2216 return;
2219 cmp = compare_values (min, max);
2220 if (cmp == -2 || cmp == 1)
2222 /* If the new range has its limits swapped around (MIN > MAX),
2223 then the operation caused one of them to wrap around, mark
2224 the new range VARYING. */
2225 set_value_range_to_varying (vr);
2227 else
2228 set_value_range (vr, type, min, max, NULL);
2231 /* Some quadruple precision helpers. */
2232 static int
2233 quad_int_cmp (double_int l0, double_int h0,
2234 double_int l1, double_int h1, bool uns)
2236 int c = h0.cmp (h1, uns);
2237 if (c != 0) return c;
2238 return l0.ucmp (l1);
2241 static void
2242 quad_int_pair_sort (double_int *l0, double_int *h0,
2243 double_int *l1, double_int *h1, bool uns)
2245 if (quad_int_cmp (*l0, *h0, *l1, *h1, uns) > 0)
2247 double_int tmp;
2248 tmp = *l0; *l0 = *l1; *l1 = tmp;
2249 tmp = *h0; *h0 = *h1; *h1 = tmp;
2253 /* Extract range information from a binary operation CODE based on
2254 the ranges of each of its operands, *VR0 and *VR1 with resulting
2255 type EXPR_TYPE. The resulting range is stored in *VR. */
2257 static void
2258 extract_range_from_binary_expr_1 (value_range_t *vr,
2259 enum tree_code code, tree expr_type,
2260 value_range_t *vr0_, value_range_t *vr1_)
2262 value_range_t vr0 = *vr0_, vr1 = *vr1_;
2263 value_range_t vrtem0 = VR_INITIALIZER, vrtem1 = VR_INITIALIZER;
2264 enum value_range_type type;
2265 tree min = NULL_TREE, max = NULL_TREE;
2266 int cmp;
2268 if (!INTEGRAL_TYPE_P (expr_type)
2269 && !POINTER_TYPE_P (expr_type))
2271 set_value_range_to_varying (vr);
2272 return;
2275 /* Not all binary expressions can be applied to ranges in a
2276 meaningful way. Handle only arithmetic operations. */
2277 if (code != PLUS_EXPR
2278 && code != MINUS_EXPR
2279 && code != POINTER_PLUS_EXPR
2280 && code != MULT_EXPR
2281 && code != TRUNC_DIV_EXPR
2282 && code != FLOOR_DIV_EXPR
2283 && code != CEIL_DIV_EXPR
2284 && code != EXACT_DIV_EXPR
2285 && code != ROUND_DIV_EXPR
2286 && code != TRUNC_MOD_EXPR
2287 && code != RSHIFT_EXPR
2288 && code != LSHIFT_EXPR
2289 && code != MIN_EXPR
2290 && code != MAX_EXPR
2291 && code != BIT_AND_EXPR
2292 && code != BIT_IOR_EXPR
2293 && code != BIT_XOR_EXPR)
2295 set_value_range_to_varying (vr);
2296 return;
2299 /* If both ranges are UNDEFINED, so is the result. */
2300 if (vr0.type == VR_UNDEFINED && vr1.type == VR_UNDEFINED)
2302 set_value_range_to_undefined (vr);
2303 return;
2305 /* If one of the ranges is UNDEFINED drop it to VARYING for the following
2306 code. At some point we may want to special-case operations that
2307 have UNDEFINED result for all or some value-ranges of the not UNDEFINED
2308 operand. */
2309 else if (vr0.type == VR_UNDEFINED)
2310 set_value_range_to_varying (&vr0);
2311 else if (vr1.type == VR_UNDEFINED)
2312 set_value_range_to_varying (&vr1);
2314 /* Now canonicalize anti-ranges to ranges when they are not symbolic
2315 and express ~[] op X as ([]' op X) U ([]'' op X). */
2316 if (vr0.type == VR_ANTI_RANGE
2317 && ranges_from_anti_range (&vr0, &vrtem0, &vrtem1))
2319 extract_range_from_binary_expr_1 (vr, code, expr_type, &vrtem0, vr1_);
2320 if (vrtem1.type != VR_UNDEFINED)
2322 value_range_t vrres = VR_INITIALIZER;
2323 extract_range_from_binary_expr_1 (&vrres, code, expr_type,
2324 &vrtem1, vr1_);
2325 vrp_meet (vr, &vrres);
2327 return;
2329 /* Likewise for X op ~[]. */
2330 if (vr1.type == VR_ANTI_RANGE
2331 && ranges_from_anti_range (&vr1, &vrtem0, &vrtem1))
2333 extract_range_from_binary_expr_1 (vr, code, expr_type, vr0_, &vrtem0);
2334 if (vrtem1.type != VR_UNDEFINED)
2336 value_range_t vrres = VR_INITIALIZER;
2337 extract_range_from_binary_expr_1 (&vrres, code, expr_type,
2338 vr0_, &vrtem1);
2339 vrp_meet (vr, &vrres);
2341 return;
2344 /* The type of the resulting value range defaults to VR0.TYPE. */
2345 type = vr0.type;
2347 /* Refuse to operate on VARYING ranges, ranges of different kinds
2348 and symbolic ranges. As an exception, we allow BIT_AND_EXPR
2349 because we may be able to derive a useful range even if one of
2350 the operands is VR_VARYING or symbolic range. Similarly for
2351 divisions. TODO, we may be able to derive anti-ranges in
2352 some cases. */
2353 if (code != BIT_AND_EXPR
2354 && code != BIT_IOR_EXPR
2355 && code != TRUNC_DIV_EXPR
2356 && code != FLOOR_DIV_EXPR
2357 && code != CEIL_DIV_EXPR
2358 && code != EXACT_DIV_EXPR
2359 && code != ROUND_DIV_EXPR
2360 && code != TRUNC_MOD_EXPR
2361 && code != MIN_EXPR
2362 && code != MAX_EXPR
2363 && (vr0.type == VR_VARYING
2364 || vr1.type == VR_VARYING
2365 || vr0.type != vr1.type
2366 || symbolic_range_p (&vr0)
2367 || symbolic_range_p (&vr1)))
2369 set_value_range_to_varying (vr);
2370 return;
2373 /* Now evaluate the expression to determine the new range. */
2374 if (POINTER_TYPE_P (expr_type))
2376 if (code == MIN_EXPR || code == MAX_EXPR)
2378 /* For MIN/MAX expressions with pointers, we only care about
2379 nullness, if both are non null, then the result is nonnull.
2380 If both are null, then the result is null. Otherwise they
2381 are varying. */
2382 if (range_is_nonnull (&vr0) && range_is_nonnull (&vr1))
2383 set_value_range_to_nonnull (vr, expr_type);
2384 else if (range_is_null (&vr0) && range_is_null (&vr1))
2385 set_value_range_to_null (vr, expr_type);
2386 else
2387 set_value_range_to_varying (vr);
2389 else if (code == POINTER_PLUS_EXPR)
2391 /* For pointer types, we are really only interested in asserting
2392 whether the expression evaluates to non-NULL. */
2393 if (range_is_nonnull (&vr0) || range_is_nonnull (&vr1))
2394 set_value_range_to_nonnull (vr, expr_type);
2395 else if (range_is_null (&vr0) && range_is_null (&vr1))
2396 set_value_range_to_null (vr, expr_type);
2397 else
2398 set_value_range_to_varying (vr);
2400 else if (code == BIT_AND_EXPR)
2402 /* For pointer types, we are really only interested in asserting
2403 whether the expression evaluates to non-NULL. */
2404 if (range_is_nonnull (&vr0) && range_is_nonnull (&vr1))
2405 set_value_range_to_nonnull (vr, expr_type);
2406 else if (range_is_null (&vr0) || range_is_null (&vr1))
2407 set_value_range_to_null (vr, expr_type);
2408 else
2409 set_value_range_to_varying (vr);
2411 else
2412 set_value_range_to_varying (vr);
2414 return;
2417 /* For integer ranges, apply the operation to each end of the
2418 range and see what we end up with. */
2419 if (code == PLUS_EXPR || code == MINUS_EXPR)
2421 /* If we have a PLUS_EXPR with two VR_RANGE integer constant
2422 ranges compute the precise range for such case if possible. */
2423 if (range_int_cst_p (&vr0)
2424 && range_int_cst_p (&vr1)
2425 /* We need as many bits as the possibly unsigned inputs. */
2426 && TYPE_PRECISION (expr_type) <= HOST_BITS_PER_DOUBLE_INT)
2428 double_int min0 = tree_to_double_int (vr0.min);
2429 double_int max0 = tree_to_double_int (vr0.max);
2430 double_int min1 = tree_to_double_int (vr1.min);
2431 double_int max1 = tree_to_double_int (vr1.max);
2432 bool uns = TYPE_UNSIGNED (expr_type);
2433 double_int type_min
2434 = double_int::min_value (TYPE_PRECISION (expr_type), uns);
2435 double_int type_max
2436 = double_int::max_value (TYPE_PRECISION (expr_type), uns);
2437 double_int dmin, dmax;
2438 int min_ovf = 0;
2439 int max_ovf = 0;
2441 if (code == PLUS_EXPR)
2443 dmin = min0 + min1;
2444 dmax = max0 + max1;
2446 /* Check for overflow in double_int. */
2447 if (min1.cmp (double_int_zero, uns) != dmin.cmp (min0, uns))
2448 min_ovf = min0.cmp (dmin, uns);
2449 if (max1.cmp (double_int_zero, uns) != dmax.cmp (max0, uns))
2450 max_ovf = max0.cmp (dmax, uns);
2452 else /* if (code == MINUS_EXPR) */
2454 dmin = min0 - max1;
2455 dmax = max0 - min1;
2457 if (double_int_zero.cmp (max1, uns) != dmin.cmp (min0, uns))
2458 min_ovf = min0.cmp (max1, uns);
2459 if (double_int_zero.cmp (min1, uns) != dmax.cmp (max0, uns))
2460 max_ovf = max0.cmp (min1, uns);
2463 /* For non-wrapping arithmetic look at possibly smaller
2464 value-ranges of the type. */
2465 if (!TYPE_OVERFLOW_WRAPS (expr_type))
2467 if (vrp_val_min (expr_type))
2468 type_min = tree_to_double_int (vrp_val_min (expr_type));
2469 if (vrp_val_max (expr_type))
2470 type_max = tree_to_double_int (vrp_val_max (expr_type));
2473 /* Check for type overflow. */
2474 if (min_ovf == 0)
2476 if (dmin.cmp (type_min, uns) == -1)
2477 min_ovf = -1;
2478 else if (dmin.cmp (type_max, uns) == 1)
2479 min_ovf = 1;
2481 if (max_ovf == 0)
2483 if (dmax.cmp (type_min, uns) == -1)
2484 max_ovf = -1;
2485 else if (dmax.cmp (type_max, uns) == 1)
2486 max_ovf = 1;
2489 if (TYPE_OVERFLOW_WRAPS (expr_type))
2491 /* If overflow wraps, truncate the values and adjust the
2492 range kind and bounds appropriately. */
2493 double_int tmin
2494 = dmin.ext (TYPE_PRECISION (expr_type), uns);
2495 double_int tmax
2496 = dmax.ext (TYPE_PRECISION (expr_type), uns);
2497 if (min_ovf == max_ovf)
2499 /* No overflow or both overflow or underflow. The
2500 range kind stays VR_RANGE. */
2501 min = double_int_to_tree (expr_type, tmin);
2502 max = double_int_to_tree (expr_type, tmax);
2504 else if (min_ovf == -1
2505 && max_ovf == 1)
2507 /* Underflow and overflow, drop to VR_VARYING. */
2508 set_value_range_to_varying (vr);
2509 return;
2511 else
2513 /* Min underflow or max overflow. The range kind
2514 changes to VR_ANTI_RANGE. */
2515 bool covers = false;
2516 double_int tem = tmin;
2517 gcc_assert ((min_ovf == -1 && max_ovf == 0)
2518 || (max_ovf == 1 && min_ovf == 0));
2519 type = VR_ANTI_RANGE;
2520 tmin = tmax + double_int_one;
2521 if (tmin.cmp (tmax, uns) < 0)
2522 covers = true;
2523 tmax = tem + double_int_minus_one;
2524 if (tmax.cmp (tem, uns) > 0)
2525 covers = true;
2526 /* If the anti-range would cover nothing, drop to varying.
2527 Likewise if the anti-range bounds are outside of the
2528 types values. */
2529 if (covers || tmin.cmp (tmax, uns) > 0)
2531 set_value_range_to_varying (vr);
2532 return;
2534 min = double_int_to_tree (expr_type, tmin);
2535 max = double_int_to_tree (expr_type, tmax);
2538 else
2540 /* If overflow does not wrap, saturate to the types min/max
2541 value. */
2542 if (min_ovf == -1)
2544 if (needs_overflow_infinity (expr_type)
2545 && supports_overflow_infinity (expr_type))
2546 min = negative_overflow_infinity (expr_type);
2547 else
2548 min = double_int_to_tree (expr_type, type_min);
2550 else if (min_ovf == 1)
2552 if (needs_overflow_infinity (expr_type)
2553 && supports_overflow_infinity (expr_type))
2554 min = positive_overflow_infinity (expr_type);
2555 else
2556 min = double_int_to_tree (expr_type, type_max);
2558 else
2559 min = double_int_to_tree (expr_type, dmin);
2561 if (max_ovf == -1)
2563 if (needs_overflow_infinity (expr_type)
2564 && supports_overflow_infinity (expr_type))
2565 max = negative_overflow_infinity (expr_type);
2566 else
2567 max = double_int_to_tree (expr_type, type_min);
2569 else if (max_ovf == 1)
2571 if (needs_overflow_infinity (expr_type)
2572 && supports_overflow_infinity (expr_type))
2573 max = positive_overflow_infinity (expr_type);
2574 else
2575 max = double_int_to_tree (expr_type, type_max);
2577 else
2578 max = double_int_to_tree (expr_type, dmax);
2580 if (needs_overflow_infinity (expr_type)
2581 && supports_overflow_infinity (expr_type))
2583 if (is_negative_overflow_infinity (vr0.min)
2584 || (code == PLUS_EXPR
2585 ? is_negative_overflow_infinity (vr1.min)
2586 : is_positive_overflow_infinity (vr1.max)))
2587 min = negative_overflow_infinity (expr_type);
2588 if (is_positive_overflow_infinity (vr0.max)
2589 || (code == PLUS_EXPR
2590 ? is_positive_overflow_infinity (vr1.max)
2591 : is_negative_overflow_infinity (vr1.min)))
2592 max = positive_overflow_infinity (expr_type);
2595 else
2597 /* For other cases, for example if we have a PLUS_EXPR with two
2598 VR_ANTI_RANGEs, drop to VR_VARYING. It would take more effort
2599 to compute a precise range for such a case.
2600 ??? General even mixed range kind operations can be expressed
2601 by for example transforming ~[3, 5] + [1, 2] to range-only
2602 operations and a union primitive:
2603 [-INF, 2] + [1, 2] U [5, +INF] + [1, 2]
2604 [-INF+1, 4] U [6, +INF(OVF)]
2605 though usually the union is not exactly representable with
2606 a single range or anti-range as the above is
2607 [-INF+1, +INF(OVF)] intersected with ~[5, 5]
2608 but one could use a scheme similar to equivalences for this. */
2609 set_value_range_to_varying (vr);
2610 return;
2613 else if (code == MIN_EXPR
2614 || code == MAX_EXPR)
2616 if (vr0.type == VR_RANGE
2617 && !symbolic_range_p (&vr0))
2619 type = VR_RANGE;
2620 if (vr1.type == VR_RANGE
2621 && !symbolic_range_p (&vr1))
2623 /* For operations that make the resulting range directly
2624 proportional to the original ranges, apply the operation to
2625 the same end of each range. */
2626 min = vrp_int_const_binop (code, vr0.min, vr1.min);
2627 max = vrp_int_const_binop (code, vr0.max, vr1.max);
2629 else if (code == MIN_EXPR)
2631 min = vrp_val_min (expr_type);
2632 max = vr0.max;
2634 else if (code == MAX_EXPR)
2636 min = vr0.min;
2637 max = vrp_val_max (expr_type);
2640 else if (vr1.type == VR_RANGE
2641 && !symbolic_range_p (&vr1))
2643 type = VR_RANGE;
2644 if (code == MIN_EXPR)
2646 min = vrp_val_min (expr_type);
2647 max = vr1.max;
2649 else if (code == MAX_EXPR)
2651 min = vr1.min;
2652 max = vrp_val_max (expr_type);
2655 else
2657 set_value_range_to_varying (vr);
2658 return;
2661 else if (code == MULT_EXPR)
2663 /* Fancy code so that with unsigned, [-3,-1]*[-3,-1] does not
2664 drop to varying. */
2665 if (range_int_cst_p (&vr0)
2666 && range_int_cst_p (&vr1)
2667 && TYPE_OVERFLOW_WRAPS (expr_type))
2669 double_int min0, max0, min1, max1, sizem1, size;
2670 double_int prod0l, prod0h, prod1l, prod1h,
2671 prod2l, prod2h, prod3l, prod3h;
2672 bool uns0, uns1, uns;
2674 sizem1 = double_int::max_value (TYPE_PRECISION (expr_type), true);
2675 size = sizem1 + double_int_one;
2677 min0 = tree_to_double_int (vr0.min);
2678 max0 = tree_to_double_int (vr0.max);
2679 min1 = tree_to_double_int (vr1.min);
2680 max1 = tree_to_double_int (vr1.max);
2682 uns0 = TYPE_UNSIGNED (expr_type);
2683 uns1 = uns0;
2685 /* Canonicalize the intervals. */
2686 if (TYPE_UNSIGNED (expr_type))
2688 double_int min2 = size - min0;
2689 if (!min2.is_zero () && min2.cmp (max0, true) < 0)
2691 min0 = -min2;
2692 max0 -= size;
2693 uns0 = false;
2696 min2 = size - min1;
2697 if (!min2.is_zero () && min2.cmp (max1, true) < 0)
2699 min1 = -min2;
2700 max1 -= size;
2701 uns1 = false;
2704 uns = uns0 & uns1;
2706 bool overflow;
2707 prod0l = min0.wide_mul_with_sign (min1, true, &prod0h, &overflow);
2708 if (!uns0 && min0.is_negative ())
2709 prod0h -= min1;
2710 if (!uns1 && min1.is_negative ())
2711 prod0h -= min0;
2713 prod1l = min0.wide_mul_with_sign (max1, true, &prod1h, &overflow);
2714 if (!uns0 && min0.is_negative ())
2715 prod1h -= max1;
2716 if (!uns1 && max1.is_negative ())
2717 prod1h -= min0;
2719 prod2l = max0.wide_mul_with_sign (min1, true, &prod2h, &overflow);
2720 if (!uns0 && max0.is_negative ())
2721 prod2h -= min1;
2722 if (!uns1 && min1.is_negative ())
2723 prod2h -= max0;
2725 prod3l = max0.wide_mul_with_sign (max1, true, &prod3h, &overflow);
2726 if (!uns0 && max0.is_negative ())
2727 prod3h -= max1;
2728 if (!uns1 && max1.is_negative ())
2729 prod3h -= max0;
2731 /* Sort the 4 products. */
2732 quad_int_pair_sort (&prod0l, &prod0h, &prod3l, &prod3h, uns);
2733 quad_int_pair_sort (&prod1l, &prod1h, &prod2l, &prod2h, uns);
2734 quad_int_pair_sort (&prod0l, &prod0h, &prod1l, &prod1h, uns);
2735 quad_int_pair_sort (&prod2l, &prod2h, &prod3l, &prod3h, uns);
2737 /* Max - min. */
2738 if (prod0l.is_zero ())
2740 prod1l = double_int_zero;
2741 prod1h = -prod0h;
2743 else
2745 prod1l = -prod0l;
2746 prod1h = ~prod0h;
2748 prod2l = prod3l + prod1l;
2749 prod2h = prod3h + prod1h;
2750 if (prod2l.ult (prod3l))
2751 prod2h += double_int_one; /* carry */
2753 if (!prod2h.is_zero ()
2754 || prod2l.cmp (sizem1, true) >= 0)
2756 /* the range covers all values. */
2757 set_value_range_to_varying (vr);
2758 return;
2761 /* The following should handle the wrapping and selecting
2762 VR_ANTI_RANGE for us. */
2763 min = double_int_to_tree (expr_type, prod0l);
2764 max = double_int_to_tree (expr_type, prod3l);
2765 set_and_canonicalize_value_range (vr, VR_RANGE, min, max, NULL);
2766 return;
2769 /* If we have an unsigned MULT_EXPR with two VR_ANTI_RANGEs,
2770 drop to VR_VARYING. It would take more effort to compute a
2771 precise range for such a case. For example, if we have
2772 op0 == 65536 and op1 == 65536 with their ranges both being
2773 ~[0,0] on a 32-bit machine, we would have op0 * op1 == 0, so
2774 we cannot claim that the product is in ~[0,0]. Note that we
2775 are guaranteed to have vr0.type == vr1.type at this
2776 point. */
2777 if (vr0.type == VR_ANTI_RANGE
2778 && !TYPE_OVERFLOW_UNDEFINED (expr_type))
2780 set_value_range_to_varying (vr);
2781 return;
2784 extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1);
2785 return;
2787 else if (code == RSHIFT_EXPR
2788 || code == LSHIFT_EXPR)
2790 /* If we have a RSHIFT_EXPR with any shift values outside [0..prec-1],
2791 then drop to VR_VARYING. Outside of this range we get undefined
2792 behavior from the shift operation. We cannot even trust
2793 SHIFT_COUNT_TRUNCATED at this stage, because that applies to rtl
2794 shifts, and the operation at the tree level may be widened. */
2795 if (range_int_cst_p (&vr1)
2796 && compare_tree_int (vr1.min, 0) >= 0
2797 && compare_tree_int (vr1.max, TYPE_PRECISION (expr_type)) == -1)
2799 if (code == RSHIFT_EXPR)
2801 extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1);
2802 return;
2804 /* We can map lshifts by constants to MULT_EXPR handling. */
2805 else if (code == LSHIFT_EXPR
2806 && range_int_cst_singleton_p (&vr1))
2808 bool saved_flag_wrapv;
2809 value_range_t vr1p = VR_INITIALIZER;
2810 vr1p.type = VR_RANGE;
2811 vr1p.min
2812 = double_int_to_tree (expr_type,
2813 double_int_one
2814 .llshift (TREE_INT_CST_LOW (vr1.min),
2815 TYPE_PRECISION (expr_type)));
2816 vr1p.max = vr1p.min;
2817 /* We have to use a wrapping multiply though as signed overflow
2818 on lshifts is implementation defined in C89. */
2819 saved_flag_wrapv = flag_wrapv;
2820 flag_wrapv = 1;
2821 extract_range_from_binary_expr_1 (vr, MULT_EXPR, expr_type,
2822 &vr0, &vr1p);
2823 flag_wrapv = saved_flag_wrapv;
2824 return;
2826 else if (code == LSHIFT_EXPR
2827 && range_int_cst_p (&vr0))
2829 int prec = TYPE_PRECISION (expr_type);
2830 int overflow_pos = prec;
2831 int bound_shift;
2832 double_int bound, complement, low_bound, high_bound;
2833 bool uns = TYPE_UNSIGNED (expr_type);
2834 bool in_bounds = false;
2836 if (!uns)
2837 overflow_pos -= 1;
2839 bound_shift = overflow_pos - TREE_INT_CST_LOW (vr1.max);
2840 /* If bound_shift == HOST_BITS_PER_DOUBLE_INT, the llshift can
2841 overflow. However, for that to happen, vr1.max needs to be
2842 zero, which means vr1 is a singleton range of zero, which
2843 means it should be handled by the previous LSHIFT_EXPR
2844 if-clause. */
2845 bound = double_int_one.llshift (bound_shift, prec);
2846 complement = ~(bound - double_int_one);
2848 if (uns)
2850 low_bound = bound.zext (prec);
2851 high_bound = complement.zext (prec);
2852 if (tree_to_double_int (vr0.max).ult (low_bound))
2854 /* [5, 6] << [1, 2] == [10, 24]. */
2855 /* We're shifting out only zeroes, the value increases
2856 monotonically. */
2857 in_bounds = true;
2859 else if (high_bound.ult (tree_to_double_int (vr0.min)))
2861 /* [0xffffff00, 0xffffffff] << [1, 2]
2862 == [0xfffffc00, 0xfffffffe]. */
2863 /* We're shifting out only ones, the value decreases
2864 monotonically. */
2865 in_bounds = true;
2868 else
2870 /* [-1, 1] << [1, 2] == [-4, 4]. */
2871 low_bound = complement.sext (prec);
2872 high_bound = bound;
2873 if (tree_to_double_int (vr0.max).slt (high_bound)
2874 && low_bound.slt (tree_to_double_int (vr0.min)))
2876 /* For non-negative numbers, we're shifting out only
2877 zeroes, the value increases monotonically.
2878 For negative numbers, we're shifting out only ones, the
2879 value decreases monotomically. */
2880 in_bounds = true;
2884 if (in_bounds)
2886 extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1);
2887 return;
2891 set_value_range_to_varying (vr);
2892 return;
2894 else if (code == TRUNC_DIV_EXPR
2895 || code == FLOOR_DIV_EXPR
2896 || code == CEIL_DIV_EXPR
2897 || code == EXACT_DIV_EXPR
2898 || code == ROUND_DIV_EXPR)
2900 if (vr0.type != VR_RANGE || symbolic_range_p (&vr0))
2902 /* For division, if op1 has VR_RANGE but op0 does not, something
2903 can be deduced just from that range. Say [min, max] / [4, max]
2904 gives [min / 4, max / 4] range. */
2905 if (vr1.type == VR_RANGE
2906 && !symbolic_range_p (&vr1)
2907 && range_includes_zero_p (vr1.min, vr1.max) == 0)
2909 vr0.type = type = VR_RANGE;
2910 vr0.min = vrp_val_min (expr_type);
2911 vr0.max = vrp_val_max (expr_type);
2913 else
2915 set_value_range_to_varying (vr);
2916 return;
2920 /* For divisions, if flag_non_call_exceptions is true, we must
2921 not eliminate a division by zero. */
2922 if (cfun->can_throw_non_call_exceptions
2923 && (vr1.type != VR_RANGE
2924 || range_includes_zero_p (vr1.min, vr1.max) != 0))
2926 set_value_range_to_varying (vr);
2927 return;
2930 /* For divisions, if op0 is VR_RANGE, we can deduce a range
2931 even if op1 is VR_VARYING, VR_ANTI_RANGE, symbolic or can
2932 include 0. */
2933 if (vr0.type == VR_RANGE
2934 && (vr1.type != VR_RANGE
2935 || range_includes_zero_p (vr1.min, vr1.max) != 0))
2937 tree zero = build_int_cst (TREE_TYPE (vr0.min), 0);
2938 int cmp;
2940 min = NULL_TREE;
2941 max = NULL_TREE;
2942 if (TYPE_UNSIGNED (expr_type)
2943 || value_range_nonnegative_p (&vr1))
2945 /* For unsigned division or when divisor is known
2946 to be non-negative, the range has to cover
2947 all numbers from 0 to max for positive max
2948 and all numbers from min to 0 for negative min. */
2949 cmp = compare_values (vr0.max, zero);
2950 if (cmp == -1)
2951 max = zero;
2952 else if (cmp == 0 || cmp == 1)
2953 max = vr0.max;
2954 else
2955 type = VR_VARYING;
2956 cmp = compare_values (vr0.min, zero);
2957 if (cmp == 1)
2958 min = zero;
2959 else if (cmp == 0 || cmp == -1)
2960 min = vr0.min;
2961 else
2962 type = VR_VARYING;
2964 else
2966 /* Otherwise the range is -max .. max or min .. -min
2967 depending on which bound is bigger in absolute value,
2968 as the division can change the sign. */
2969 abs_extent_range (vr, vr0.min, vr0.max);
2970 return;
2972 if (type == VR_VARYING)
2974 set_value_range_to_varying (vr);
2975 return;
2978 else
2980 extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1);
2981 return;
2984 else if (code == TRUNC_MOD_EXPR)
2986 if (vr1.type != VR_RANGE
2987 || range_includes_zero_p (vr1.min, vr1.max) != 0
2988 || vrp_val_is_min (vr1.min))
2990 set_value_range_to_varying (vr);
2991 return;
2993 type = VR_RANGE;
2994 /* Compute MAX <|vr1.min|, |vr1.max|> - 1. */
2995 max = fold_unary_to_constant (ABS_EXPR, expr_type, vr1.min);
2996 if (tree_int_cst_lt (max, vr1.max))
2997 max = vr1.max;
2998 max = int_const_binop (MINUS_EXPR, max, integer_one_node);
2999 /* If the dividend is non-negative the modulus will be
3000 non-negative as well. */
3001 if (TYPE_UNSIGNED (expr_type)
3002 || value_range_nonnegative_p (&vr0))
3003 min = build_int_cst (TREE_TYPE (max), 0);
3004 else
3005 min = fold_unary_to_constant (NEGATE_EXPR, expr_type, max);
3007 else if (code == BIT_AND_EXPR || code == BIT_IOR_EXPR || code == BIT_XOR_EXPR)
3009 bool int_cst_range0, int_cst_range1;
3010 double_int may_be_nonzero0, may_be_nonzero1;
3011 double_int must_be_nonzero0, must_be_nonzero1;
3013 int_cst_range0 = zero_nonzero_bits_from_vr (&vr0, &may_be_nonzero0,
3014 &must_be_nonzero0);
3015 int_cst_range1 = zero_nonzero_bits_from_vr (&vr1, &may_be_nonzero1,
3016 &must_be_nonzero1);
3018 type = VR_RANGE;
3019 if (code == BIT_AND_EXPR)
3021 double_int dmax;
3022 min = double_int_to_tree (expr_type,
3023 must_be_nonzero0 & must_be_nonzero1);
3024 dmax = may_be_nonzero0 & may_be_nonzero1;
3025 /* If both input ranges contain only negative values we can
3026 truncate the result range maximum to the minimum of the
3027 input range maxima. */
3028 if (int_cst_range0 && int_cst_range1
3029 && tree_int_cst_sgn (vr0.max) < 0
3030 && tree_int_cst_sgn (vr1.max) < 0)
3032 dmax = dmax.min (tree_to_double_int (vr0.max),
3033 TYPE_UNSIGNED (expr_type));
3034 dmax = dmax.min (tree_to_double_int (vr1.max),
3035 TYPE_UNSIGNED (expr_type));
3037 /* If either input range contains only non-negative values
3038 we can truncate the result range maximum to the respective
3039 maximum of the input range. */
3040 if (int_cst_range0 && tree_int_cst_sgn (vr0.min) >= 0)
3041 dmax = dmax.min (tree_to_double_int (vr0.max),
3042 TYPE_UNSIGNED (expr_type));
3043 if (int_cst_range1 && tree_int_cst_sgn (vr1.min) >= 0)
3044 dmax = dmax.min (tree_to_double_int (vr1.max),
3045 TYPE_UNSIGNED (expr_type));
3046 max = double_int_to_tree (expr_type, dmax);
3048 else if (code == BIT_IOR_EXPR)
3050 double_int dmin;
3051 max = double_int_to_tree (expr_type,
3052 may_be_nonzero0 | may_be_nonzero1);
3053 dmin = must_be_nonzero0 | must_be_nonzero1;
3054 /* If the input ranges contain only positive values we can
3055 truncate the minimum of the result range to the maximum
3056 of the input range minima. */
3057 if (int_cst_range0 && int_cst_range1
3058 && tree_int_cst_sgn (vr0.min) >= 0
3059 && tree_int_cst_sgn (vr1.min) >= 0)
3061 dmin = dmin.max (tree_to_double_int (vr0.min),
3062 TYPE_UNSIGNED (expr_type));
3063 dmin = dmin.max (tree_to_double_int (vr1.min),
3064 TYPE_UNSIGNED (expr_type));
3066 /* If either input range contains only negative values
3067 we can truncate the minimum of the result range to the
3068 respective minimum range. */
3069 if (int_cst_range0 && tree_int_cst_sgn (vr0.max) < 0)
3070 dmin = dmin.max (tree_to_double_int (vr0.min),
3071 TYPE_UNSIGNED (expr_type));
3072 if (int_cst_range1 && tree_int_cst_sgn (vr1.max) < 0)
3073 dmin = dmin.max (tree_to_double_int (vr1.min),
3074 TYPE_UNSIGNED (expr_type));
3075 min = double_int_to_tree (expr_type, dmin);
3077 else if (code == BIT_XOR_EXPR)
3079 double_int result_zero_bits, result_one_bits;
3080 result_zero_bits = (must_be_nonzero0 & must_be_nonzero1)
3081 | ~(may_be_nonzero0 | may_be_nonzero1);
3082 result_one_bits = must_be_nonzero0.and_not (may_be_nonzero1)
3083 | must_be_nonzero1.and_not (may_be_nonzero0);
3084 max = double_int_to_tree (expr_type, ~result_zero_bits);
3085 min = double_int_to_tree (expr_type, result_one_bits);
3086 /* If the range has all positive or all negative values the
3087 result is better than VARYING. */
3088 if (tree_int_cst_sgn (min) < 0
3089 || tree_int_cst_sgn (max) >= 0)
3091 else
3092 max = min = NULL_TREE;
3095 else
3096 gcc_unreachable ();
3098 /* If either MIN or MAX overflowed, then set the resulting range to
3099 VARYING. But we do accept an overflow infinity
3100 representation. */
3101 if (min == NULL_TREE
3102 || !is_gimple_min_invariant (min)
3103 || (TREE_OVERFLOW (min) && !is_overflow_infinity (min))
3104 || max == NULL_TREE
3105 || !is_gimple_min_invariant (max)
3106 || (TREE_OVERFLOW (max) && !is_overflow_infinity (max)))
3108 set_value_range_to_varying (vr);
3109 return;
3112 /* We punt if:
3113 1) [-INF, +INF]
3114 2) [-INF, +-INF(OVF)]
3115 3) [+-INF(OVF), +INF]
3116 4) [+-INF(OVF), +-INF(OVF)]
3117 We learn nothing when we have INF and INF(OVF) on both sides.
3118 Note that we do accept [-INF, -INF] and [+INF, +INF] without
3119 overflow. */
3120 if ((vrp_val_is_min (min) || is_overflow_infinity (min))
3121 && (vrp_val_is_max (max) || is_overflow_infinity (max)))
3123 set_value_range_to_varying (vr);
3124 return;
3127 cmp = compare_values (min, max);
3128 if (cmp == -2 || cmp == 1)
3130 /* If the new range has its limits swapped around (MIN > MAX),
3131 then the operation caused one of them to wrap around, mark
3132 the new range VARYING. */
3133 set_value_range_to_varying (vr);
3135 else
3136 set_value_range (vr, type, min, max, NULL);
3139 /* Extract range information from a binary expression OP0 CODE OP1 based on
3140 the ranges of each of its operands with resulting type EXPR_TYPE.
3141 The resulting range is stored in *VR. */
3143 static void
3144 extract_range_from_binary_expr (value_range_t *vr,
3145 enum tree_code code,
3146 tree expr_type, tree op0, tree op1)
3148 value_range_t vr0 = VR_INITIALIZER;
3149 value_range_t vr1 = VR_INITIALIZER;
3151 /* Get value ranges for each operand. For constant operands, create
3152 a new value range with the operand to simplify processing. */
3153 if (TREE_CODE (op0) == SSA_NAME)
3154 vr0 = *(get_value_range (op0));
3155 else if (is_gimple_min_invariant (op0))
3156 set_value_range_to_value (&vr0, op0, NULL);
3157 else
3158 set_value_range_to_varying (&vr0);
3160 if (TREE_CODE (op1) == SSA_NAME)
3161 vr1 = *(get_value_range (op1));
3162 else if (is_gimple_min_invariant (op1))
3163 set_value_range_to_value (&vr1, op1, NULL);
3164 else
3165 set_value_range_to_varying (&vr1);
3167 extract_range_from_binary_expr_1 (vr, code, expr_type, &vr0, &vr1);
3170 /* Extract range information from a unary operation CODE based on
3171 the range of its operand *VR0 with type OP0_TYPE with resulting type TYPE.
3172 The The resulting range is stored in *VR. */
3174 static void
3175 extract_range_from_unary_expr_1 (value_range_t *vr,
3176 enum tree_code code, tree type,
3177 value_range_t *vr0_, tree op0_type)
3179 value_range_t vr0 = *vr0_, vrtem0 = VR_INITIALIZER, vrtem1 = VR_INITIALIZER;
3181 /* VRP only operates on integral and pointer types. */
3182 if (!(INTEGRAL_TYPE_P (op0_type)
3183 || POINTER_TYPE_P (op0_type))
3184 || !(INTEGRAL_TYPE_P (type)
3185 || POINTER_TYPE_P (type)))
3187 set_value_range_to_varying (vr);
3188 return;
3191 /* If VR0 is UNDEFINED, so is the result. */
3192 if (vr0.type == VR_UNDEFINED)
3194 set_value_range_to_undefined (vr);
3195 return;
3198 /* Handle operations that we express in terms of others. */
3199 if (code == PAREN_EXPR)
3201 /* PAREN_EXPR is a simple copy. */
3202 copy_value_range (vr, &vr0);
3203 return;
3205 else if (code == NEGATE_EXPR)
3207 /* -X is simply 0 - X, so re-use existing code that also handles
3208 anti-ranges fine. */
3209 value_range_t zero = VR_INITIALIZER;
3210 set_value_range_to_value (&zero, build_int_cst (type, 0), NULL);
3211 extract_range_from_binary_expr_1 (vr, MINUS_EXPR, type, &zero, &vr0);
3212 return;
3214 else if (code == BIT_NOT_EXPR)
3216 /* ~X is simply -1 - X, so re-use existing code that also handles
3217 anti-ranges fine. */
3218 value_range_t minusone = VR_INITIALIZER;
3219 set_value_range_to_value (&minusone, build_int_cst (type, -1), NULL);
3220 extract_range_from_binary_expr_1 (vr, MINUS_EXPR,
3221 type, &minusone, &vr0);
3222 return;
3225 /* Now canonicalize anti-ranges to ranges when they are not symbolic
3226 and express op ~[] as (op []') U (op []''). */
3227 if (vr0.type == VR_ANTI_RANGE
3228 && ranges_from_anti_range (&vr0, &vrtem0, &vrtem1))
3230 extract_range_from_unary_expr_1 (vr, code, type, &vrtem0, op0_type);
3231 if (vrtem1.type != VR_UNDEFINED)
3233 value_range_t vrres = VR_INITIALIZER;
3234 extract_range_from_unary_expr_1 (&vrres, code, type,
3235 &vrtem1, op0_type);
3236 vrp_meet (vr, &vrres);
3238 return;
3241 if (CONVERT_EXPR_CODE_P (code))
3243 tree inner_type = op0_type;
3244 tree outer_type = type;
3246 /* If the expression evaluates to a pointer, we are only interested in
3247 determining if it evaluates to NULL [0, 0] or non-NULL (~[0, 0]). */
3248 if (POINTER_TYPE_P (type))
3250 if (range_is_nonnull (&vr0))
3251 set_value_range_to_nonnull (vr, type);
3252 else if (range_is_null (&vr0))
3253 set_value_range_to_null (vr, type);
3254 else
3255 set_value_range_to_varying (vr);
3256 return;
3259 /* If VR0 is varying and we increase the type precision, assume
3260 a full range for the following transformation. */
3261 if (vr0.type == VR_VARYING
3262 && INTEGRAL_TYPE_P (inner_type)
3263 && TYPE_PRECISION (inner_type) < TYPE_PRECISION (outer_type))
3265 vr0.type = VR_RANGE;
3266 vr0.min = TYPE_MIN_VALUE (inner_type);
3267 vr0.max = TYPE_MAX_VALUE (inner_type);
3270 /* If VR0 is a constant range or anti-range and the conversion is
3271 not truncating we can convert the min and max values and
3272 canonicalize the resulting range. Otherwise we can do the
3273 conversion if the size of the range is less than what the
3274 precision of the target type can represent and the range is
3275 not an anti-range. */
3276 if ((vr0.type == VR_RANGE
3277 || vr0.type == VR_ANTI_RANGE)
3278 && TREE_CODE (vr0.min) == INTEGER_CST
3279 && TREE_CODE (vr0.max) == INTEGER_CST
3280 && (!is_overflow_infinity (vr0.min)
3281 || (vr0.type == VR_RANGE
3282 && TYPE_PRECISION (outer_type) > TYPE_PRECISION (inner_type)
3283 && needs_overflow_infinity (outer_type)
3284 && supports_overflow_infinity (outer_type)))
3285 && (!is_overflow_infinity (vr0.max)
3286 || (vr0.type == VR_RANGE
3287 && TYPE_PRECISION (outer_type) > TYPE_PRECISION (inner_type)
3288 && needs_overflow_infinity (outer_type)
3289 && supports_overflow_infinity (outer_type)))
3290 && (TYPE_PRECISION (outer_type) >= TYPE_PRECISION (inner_type)
3291 || (vr0.type == VR_RANGE
3292 && integer_zerop (int_const_binop (RSHIFT_EXPR,
3293 int_const_binop (MINUS_EXPR, vr0.max, vr0.min),
3294 size_int (TYPE_PRECISION (outer_type)))))))
3296 tree new_min, new_max;
3297 if (is_overflow_infinity (vr0.min))
3298 new_min = negative_overflow_infinity (outer_type);
3299 else
3300 new_min = force_fit_type_double (outer_type,
3301 tree_to_double_int (vr0.min),
3302 0, false);
3303 if (is_overflow_infinity (vr0.max))
3304 new_max = positive_overflow_infinity (outer_type);
3305 else
3306 new_max = force_fit_type_double (outer_type,
3307 tree_to_double_int (vr0.max),
3308 0, false);
3309 set_and_canonicalize_value_range (vr, vr0.type,
3310 new_min, new_max, NULL);
3311 return;
3314 set_value_range_to_varying (vr);
3315 return;
3317 else if (code == ABS_EXPR)
3319 tree min, max;
3320 int cmp;
3322 /* Pass through vr0 in the easy cases. */
3323 if (TYPE_UNSIGNED (type)
3324 || value_range_nonnegative_p (&vr0))
3326 copy_value_range (vr, &vr0);
3327 return;
3330 /* For the remaining varying or symbolic ranges we can't do anything
3331 useful. */
3332 if (vr0.type == VR_VARYING
3333 || symbolic_range_p (&vr0))
3335 set_value_range_to_varying (vr);
3336 return;
3339 /* -TYPE_MIN_VALUE = TYPE_MIN_VALUE with flag_wrapv so we can't get a
3340 useful range. */
3341 if (!TYPE_OVERFLOW_UNDEFINED (type)
3342 && ((vr0.type == VR_RANGE
3343 && vrp_val_is_min (vr0.min))
3344 || (vr0.type == VR_ANTI_RANGE
3345 && !vrp_val_is_min (vr0.min))))
3347 set_value_range_to_varying (vr);
3348 return;
3351 /* ABS_EXPR may flip the range around, if the original range
3352 included negative values. */
3353 if (is_overflow_infinity (vr0.min))
3354 min = positive_overflow_infinity (type);
3355 else if (!vrp_val_is_min (vr0.min))
3356 min = fold_unary_to_constant (code, type, vr0.min);
3357 else if (!needs_overflow_infinity (type))
3358 min = TYPE_MAX_VALUE (type);
3359 else if (supports_overflow_infinity (type))
3360 min = positive_overflow_infinity (type);
3361 else
3363 set_value_range_to_varying (vr);
3364 return;
3367 if (is_overflow_infinity (vr0.max))
3368 max = positive_overflow_infinity (type);
3369 else if (!vrp_val_is_min (vr0.max))
3370 max = fold_unary_to_constant (code, type, vr0.max);
3371 else if (!needs_overflow_infinity (type))
3372 max = TYPE_MAX_VALUE (type);
3373 else if (supports_overflow_infinity (type)
3374 /* We shouldn't generate [+INF, +INF] as set_value_range
3375 doesn't like this and ICEs. */
3376 && !is_positive_overflow_infinity (min))
3377 max = positive_overflow_infinity (type);
3378 else
3380 set_value_range_to_varying (vr);
3381 return;
3384 cmp = compare_values (min, max);
3386 /* If a VR_ANTI_RANGEs contains zero, then we have
3387 ~[-INF, min(MIN, MAX)]. */
3388 if (vr0.type == VR_ANTI_RANGE)
3390 if (range_includes_zero_p (vr0.min, vr0.max) == 1)
3392 /* Take the lower of the two values. */
3393 if (cmp != 1)
3394 max = min;
3396 /* Create ~[-INF, min (abs(MIN), abs(MAX))]
3397 or ~[-INF + 1, min (abs(MIN), abs(MAX))] when
3398 flag_wrapv is set and the original anti-range doesn't include
3399 TYPE_MIN_VALUE, remember -TYPE_MIN_VALUE = TYPE_MIN_VALUE. */
3400 if (TYPE_OVERFLOW_WRAPS (type))
3402 tree type_min_value = TYPE_MIN_VALUE (type);
3404 min = (vr0.min != type_min_value
3405 ? int_const_binop (PLUS_EXPR, type_min_value,
3406 integer_one_node)
3407 : type_min_value);
3409 else
3411 if (overflow_infinity_range_p (&vr0))
3412 min = negative_overflow_infinity (type);
3413 else
3414 min = TYPE_MIN_VALUE (type);
3417 else
3419 /* All else has failed, so create the range [0, INF], even for
3420 flag_wrapv since TYPE_MIN_VALUE is in the original
3421 anti-range. */
3422 vr0.type = VR_RANGE;
3423 min = build_int_cst (type, 0);
3424 if (needs_overflow_infinity (type))
3426 if (supports_overflow_infinity (type))
3427 max = positive_overflow_infinity (type);
3428 else
3430 set_value_range_to_varying (vr);
3431 return;
3434 else
3435 max = TYPE_MAX_VALUE (type);
3439 /* If the range contains zero then we know that the minimum value in the
3440 range will be zero. */
3441 else if (range_includes_zero_p (vr0.min, vr0.max) == 1)
3443 if (cmp == 1)
3444 max = min;
3445 min = build_int_cst (type, 0);
3447 else
3449 /* If the range was reversed, swap MIN and MAX. */
3450 if (cmp == 1)
3452 tree t = min;
3453 min = max;
3454 max = t;
3458 cmp = compare_values (min, max);
3459 if (cmp == -2 || cmp == 1)
3461 /* If the new range has its limits swapped around (MIN > MAX),
3462 then the operation caused one of them to wrap around, mark
3463 the new range VARYING. */
3464 set_value_range_to_varying (vr);
3466 else
3467 set_value_range (vr, vr0.type, min, max, NULL);
3468 return;
3471 /* For unhandled operations fall back to varying. */
3472 set_value_range_to_varying (vr);
3473 return;
3477 /* Extract range information from a unary expression CODE OP0 based on
3478 the range of its operand with resulting type TYPE.
3479 The resulting range is stored in *VR. */
3481 static void
3482 extract_range_from_unary_expr (value_range_t *vr, enum tree_code code,
3483 tree type, tree op0)
3485 value_range_t vr0 = VR_INITIALIZER;
3487 /* Get value ranges for the operand. For constant operands, create
3488 a new value range with the operand to simplify processing. */
3489 if (TREE_CODE (op0) == SSA_NAME)
3490 vr0 = *(get_value_range (op0));
3491 else if (is_gimple_min_invariant (op0))
3492 set_value_range_to_value (&vr0, op0, NULL);
3493 else
3494 set_value_range_to_varying (&vr0);
3496 extract_range_from_unary_expr_1 (vr, code, type, &vr0, TREE_TYPE (op0));
3500 /* Extract range information from a conditional expression STMT based on
3501 the ranges of each of its operands and the expression code. */
3503 static void
3504 extract_range_from_cond_expr (value_range_t *vr, gimple stmt)
3506 tree op0, op1;
3507 value_range_t vr0 = VR_INITIALIZER;
3508 value_range_t vr1 = VR_INITIALIZER;
3510 /* Get value ranges for each operand. For constant operands, create
3511 a new value range with the operand to simplify processing. */
3512 op0 = gimple_assign_rhs2 (stmt);
3513 if (TREE_CODE (op0) == SSA_NAME)
3514 vr0 = *(get_value_range (op0));
3515 else if (is_gimple_min_invariant (op0))
3516 set_value_range_to_value (&vr0, op0, NULL);
3517 else
3518 set_value_range_to_varying (&vr0);
3520 op1 = gimple_assign_rhs3 (stmt);
3521 if (TREE_CODE (op1) == SSA_NAME)
3522 vr1 = *(get_value_range (op1));
3523 else if (is_gimple_min_invariant (op1))
3524 set_value_range_to_value (&vr1, op1, NULL);
3525 else
3526 set_value_range_to_varying (&vr1);
3528 /* The resulting value range is the union of the operand ranges */
3529 copy_value_range (vr, &vr0);
3530 vrp_meet (vr, &vr1);
3534 /* Extract range information from a comparison expression EXPR based
3535 on the range of its operand and the expression code. */
3537 static void
3538 extract_range_from_comparison (value_range_t *vr, enum tree_code code,
3539 tree type, tree op0, tree op1)
3541 bool sop = false;
3542 tree val;
3544 val = vrp_evaluate_conditional_warnv_with_ops (code, op0, op1, false, &sop,
3545 NULL);
3547 /* A disadvantage of using a special infinity as an overflow
3548 representation is that we lose the ability to record overflow
3549 when we don't have an infinity. So we have to ignore a result
3550 which relies on overflow. */
3552 if (val && !is_overflow_infinity (val) && !sop)
3554 /* Since this expression was found on the RHS of an assignment,
3555 its type may be different from _Bool. Convert VAL to EXPR's
3556 type. */
3557 val = fold_convert (type, val);
3558 if (is_gimple_min_invariant (val))
3559 set_value_range_to_value (vr, val, vr->equiv);
3560 else
3561 set_value_range (vr, VR_RANGE, val, val, vr->equiv);
3563 else
3564 /* The result of a comparison is always true or false. */
3565 set_value_range_to_truthvalue (vr, type);
3568 /* Try to derive a nonnegative or nonzero range out of STMT relying
3569 primarily on generic routines in fold in conjunction with range data.
3570 Store the result in *VR */
3572 static void
3573 extract_range_basic (value_range_t *vr, gimple stmt)
3575 bool sop = false;
3576 tree type = gimple_expr_type (stmt);
3578 if (gimple_call_builtin_p (stmt, BUILT_IN_NORMAL))
3580 tree fndecl = gimple_call_fndecl (stmt), arg;
3581 int mini, maxi, zerov = 0, prec;
3583 switch (DECL_FUNCTION_CODE (fndecl))
3585 case BUILT_IN_CONSTANT_P:
3586 /* If the call is __builtin_constant_p and the argument is a
3587 function parameter resolve it to false. This avoids bogus
3588 array bound warnings.
3589 ??? We could do this as early as inlining is finished. */
3590 arg = gimple_call_arg (stmt, 0);
3591 if (TREE_CODE (arg) == SSA_NAME
3592 && SSA_NAME_IS_DEFAULT_DEF (arg)
3593 && TREE_CODE (SSA_NAME_VAR (arg)) == PARM_DECL)
3595 set_value_range_to_null (vr, type);
3596 return;
3598 break;
3599 /* Both __builtin_ffs* and __builtin_popcount return
3600 [0, prec]. */
3601 CASE_INT_FN (BUILT_IN_FFS):
3602 CASE_INT_FN (BUILT_IN_POPCOUNT):
3603 arg = gimple_call_arg (stmt, 0);
3604 prec = TYPE_PRECISION (TREE_TYPE (arg));
3605 mini = 0;
3606 maxi = prec;
3607 if (TREE_CODE (arg) == SSA_NAME)
3609 value_range_t *vr0 = get_value_range (arg);
3610 /* If arg is non-zero, then ffs or popcount
3611 are non-zero. */
3612 if (((vr0->type == VR_RANGE
3613 && integer_nonzerop (vr0->min))
3614 || (vr0->type == VR_ANTI_RANGE
3615 && integer_zerop (vr0->min)))
3616 && !TREE_OVERFLOW (vr0->min))
3617 mini = 1;
3618 /* If some high bits are known to be zero,
3619 we can decrease the maximum. */
3620 if (vr0->type == VR_RANGE
3621 && TREE_CODE (vr0->max) == INTEGER_CST
3622 && !TREE_OVERFLOW (vr0->max))
3623 maxi = tree_floor_log2 (vr0->max) + 1;
3625 goto bitop_builtin;
3626 /* __builtin_parity* returns [0, 1]. */
3627 CASE_INT_FN (BUILT_IN_PARITY):
3628 mini = 0;
3629 maxi = 1;
3630 goto bitop_builtin;
3631 /* __builtin_c[lt]z* return [0, prec-1], except for
3632 when the argument is 0, but that is undefined behavior.
3633 On many targets where the CLZ RTL or optab value is defined
3634 for 0 the value is prec, so include that in the range
3635 by default. */
3636 CASE_INT_FN (BUILT_IN_CLZ):
3637 arg = gimple_call_arg (stmt, 0);
3638 prec = TYPE_PRECISION (TREE_TYPE (arg));
3639 mini = 0;
3640 maxi = prec;
3641 if (optab_handler (clz_optab, TYPE_MODE (TREE_TYPE (arg)))
3642 != CODE_FOR_nothing
3643 && CLZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (TREE_TYPE (arg)),
3644 zerov)
3645 /* Handle only the single common value. */
3646 && zerov != prec)
3647 /* Magic value to give up, unless vr0 proves
3648 arg is non-zero. */
3649 mini = -2;
3650 if (TREE_CODE (arg) == SSA_NAME)
3652 value_range_t *vr0 = get_value_range (arg);
3653 /* From clz of VR_RANGE minimum we can compute
3654 result maximum. */
3655 if (vr0->type == VR_RANGE
3656 && TREE_CODE (vr0->min) == INTEGER_CST
3657 && !TREE_OVERFLOW (vr0->min))
3659 maxi = prec - 1 - tree_floor_log2 (vr0->min);
3660 if (maxi != prec)
3661 mini = 0;
3663 else if (vr0->type == VR_ANTI_RANGE
3664 && integer_zerop (vr0->min)
3665 && !TREE_OVERFLOW (vr0->min))
3667 maxi = prec - 1;
3668 mini = 0;
3670 if (mini == -2)
3671 break;
3672 /* From clz of VR_RANGE maximum we can compute
3673 result minimum. */
3674 if (vr0->type == VR_RANGE
3675 && TREE_CODE (vr0->max) == INTEGER_CST
3676 && !TREE_OVERFLOW (vr0->max))
3678 mini = prec - 1 - tree_floor_log2 (vr0->max);
3679 if (mini == prec)
3680 break;
3683 if (mini == -2)
3684 break;
3685 goto bitop_builtin;
3686 /* __builtin_ctz* return [0, prec-1], except for
3687 when the argument is 0, but that is undefined behavior.
3688 If there is a ctz optab for this mode and
3689 CTZ_DEFINED_VALUE_AT_ZERO, include that in the range,
3690 otherwise just assume 0 won't be seen. */
3691 CASE_INT_FN (BUILT_IN_CTZ):
3692 arg = gimple_call_arg (stmt, 0);
3693 prec = TYPE_PRECISION (TREE_TYPE (arg));
3694 mini = 0;
3695 maxi = prec - 1;
3696 if (optab_handler (ctz_optab, TYPE_MODE (TREE_TYPE (arg)))
3697 != CODE_FOR_nothing
3698 && CTZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (TREE_TYPE (arg)),
3699 zerov))
3701 /* Handle only the two common values. */
3702 if (zerov == -1)
3703 mini = -1;
3704 else if (zerov == prec)
3705 maxi = prec;
3706 else
3707 /* Magic value to give up, unless vr0 proves
3708 arg is non-zero. */
3709 mini = -2;
3711 if (TREE_CODE (arg) == SSA_NAME)
3713 value_range_t *vr0 = get_value_range (arg);
3714 /* If arg is non-zero, then use [0, prec - 1]. */
3715 if (((vr0->type == VR_RANGE
3716 && integer_nonzerop (vr0->min))
3717 || (vr0->type == VR_ANTI_RANGE
3718 && integer_zerop (vr0->min)))
3719 && !TREE_OVERFLOW (vr0->min))
3721 mini = 0;
3722 maxi = prec - 1;
3724 /* If some high bits are known to be zero,
3725 we can decrease the result maximum. */
3726 if (vr0->type == VR_RANGE
3727 && TREE_CODE (vr0->max) == INTEGER_CST
3728 && !TREE_OVERFLOW (vr0->max))
3730 maxi = tree_floor_log2 (vr0->max);
3731 /* For vr0 [0, 0] give up. */
3732 if (maxi == -1)
3733 break;
3736 if (mini == -2)
3737 break;
3738 goto bitop_builtin;
3739 /* __builtin_clrsb* returns [0, prec-1]. */
3740 CASE_INT_FN (BUILT_IN_CLRSB):
3741 arg = gimple_call_arg (stmt, 0);
3742 prec = TYPE_PRECISION (TREE_TYPE (arg));
3743 mini = 0;
3744 maxi = prec - 1;
3745 goto bitop_builtin;
3746 bitop_builtin:
3747 set_value_range (vr, VR_RANGE, build_int_cst (type, mini),
3748 build_int_cst (type, maxi), NULL);
3749 return;
3750 default:
3751 break;
3754 if (INTEGRAL_TYPE_P (type)
3755 && gimple_stmt_nonnegative_warnv_p (stmt, &sop))
3756 set_value_range_to_nonnegative (vr, type,
3757 sop || stmt_overflow_infinity (stmt));
3758 else if (vrp_stmt_computes_nonzero (stmt, &sop)
3759 && !sop)
3760 set_value_range_to_nonnull (vr, type);
3761 else
3762 set_value_range_to_varying (vr);
3766 /* Try to compute a useful range out of assignment STMT and store it
3767 in *VR. */
3769 static void
3770 extract_range_from_assignment (value_range_t *vr, gimple stmt)
3772 enum tree_code code = gimple_assign_rhs_code (stmt);
3774 if (code == ASSERT_EXPR)
3775 extract_range_from_assert (vr, gimple_assign_rhs1 (stmt));
3776 else if (code == SSA_NAME)
3777 extract_range_from_ssa_name (vr, gimple_assign_rhs1 (stmt));
3778 else if (TREE_CODE_CLASS (code) == tcc_binary)
3779 extract_range_from_binary_expr (vr, gimple_assign_rhs_code (stmt),
3780 gimple_expr_type (stmt),
3781 gimple_assign_rhs1 (stmt),
3782 gimple_assign_rhs2 (stmt));
3783 else if (TREE_CODE_CLASS (code) == tcc_unary)
3784 extract_range_from_unary_expr (vr, gimple_assign_rhs_code (stmt),
3785 gimple_expr_type (stmt),
3786 gimple_assign_rhs1 (stmt));
3787 else if (code == COND_EXPR)
3788 extract_range_from_cond_expr (vr, stmt);
3789 else if (TREE_CODE_CLASS (code) == tcc_comparison)
3790 extract_range_from_comparison (vr, gimple_assign_rhs_code (stmt),
3791 gimple_expr_type (stmt),
3792 gimple_assign_rhs1 (stmt),
3793 gimple_assign_rhs2 (stmt));
3794 else if (get_gimple_rhs_class (code) == GIMPLE_SINGLE_RHS
3795 && is_gimple_min_invariant (gimple_assign_rhs1 (stmt)))
3796 set_value_range_to_value (vr, gimple_assign_rhs1 (stmt), NULL);
3797 else
3798 set_value_range_to_varying (vr);
3800 if (vr->type == VR_VARYING)
3801 extract_range_basic (vr, stmt);
3804 /* Given a range VR, a LOOP and a variable VAR, determine whether it
3805 would be profitable to adjust VR using scalar evolution information
3806 for VAR. If so, update VR with the new limits. */
3808 static void
3809 adjust_range_with_scev (value_range_t *vr, struct loop *loop,
3810 gimple stmt, tree var)
3812 tree init, step, chrec, tmin, tmax, min, max, type, tem;
3813 enum ev_direction dir;
3815 /* TODO. Don't adjust anti-ranges. An anti-range may provide
3816 better opportunities than a regular range, but I'm not sure. */
3817 if (vr->type == VR_ANTI_RANGE)
3818 return;
3820 chrec = instantiate_parameters (loop, analyze_scalar_evolution (loop, var));
3822 /* Like in PR19590, scev can return a constant function. */
3823 if (is_gimple_min_invariant (chrec))
3825 set_value_range_to_value (vr, chrec, vr->equiv);
3826 return;
3829 if (TREE_CODE (chrec) != POLYNOMIAL_CHREC)
3830 return;
3832 init = initial_condition_in_loop_num (chrec, loop->num);
3833 tem = op_with_constant_singleton_value_range (init);
3834 if (tem)
3835 init = tem;
3836 step = evolution_part_in_loop_num (chrec, loop->num);
3837 tem = op_with_constant_singleton_value_range (step);
3838 if (tem)
3839 step = tem;
3841 /* If STEP is symbolic, we can't know whether INIT will be the
3842 minimum or maximum value in the range. Also, unless INIT is
3843 a simple expression, compare_values and possibly other functions
3844 in tree-vrp won't be able to handle it. */
3845 if (step == NULL_TREE
3846 || !is_gimple_min_invariant (step)
3847 || !valid_value_p (init))
3848 return;
3850 dir = scev_direction (chrec);
3851 if (/* Do not adjust ranges if we do not know whether the iv increases
3852 or decreases, ... */
3853 dir == EV_DIR_UNKNOWN
3854 /* ... or if it may wrap. */
3855 || scev_probably_wraps_p (init, step, stmt, get_chrec_loop (chrec),
3856 true))
3857 return;
3859 /* We use TYPE_MIN_VALUE and TYPE_MAX_VALUE here instead of
3860 negative_overflow_infinity and positive_overflow_infinity,
3861 because we have concluded that the loop probably does not
3862 wrap. */
3864 type = TREE_TYPE (var);
3865 if (POINTER_TYPE_P (type) || !TYPE_MIN_VALUE (type))
3866 tmin = lower_bound_in_type (type, type);
3867 else
3868 tmin = TYPE_MIN_VALUE (type);
3869 if (POINTER_TYPE_P (type) || !TYPE_MAX_VALUE (type))
3870 tmax = upper_bound_in_type (type, type);
3871 else
3872 tmax = TYPE_MAX_VALUE (type);
3874 /* Try to use estimated number of iterations for the loop to constrain the
3875 final value in the evolution. */
3876 if (TREE_CODE (step) == INTEGER_CST
3877 && is_gimple_val (init)
3878 && (TREE_CODE (init) != SSA_NAME
3879 || get_value_range (init)->type == VR_RANGE))
3881 double_int nit;
3883 /* We are only entering here for loop header PHI nodes, so using
3884 the number of latch executions is the correct thing to use. */
3885 if (max_loop_iterations (loop, &nit))
3887 value_range_t maxvr = VR_INITIALIZER;
3888 double_int dtmp;
3889 bool unsigned_p = TYPE_UNSIGNED (TREE_TYPE (step));
3890 bool overflow = false;
3892 dtmp = tree_to_double_int (step)
3893 .mul_with_sign (nit, unsigned_p, &overflow);
3894 /* If the multiplication overflowed we can't do a meaningful
3895 adjustment. Likewise if the result doesn't fit in the type
3896 of the induction variable. For a signed type we have to
3897 check whether the result has the expected signedness which
3898 is that of the step as number of iterations is unsigned. */
3899 if (!overflow
3900 && double_int_fits_to_tree_p (TREE_TYPE (init), dtmp)
3901 && (unsigned_p
3902 || ((dtmp.high ^ TREE_INT_CST_HIGH (step)) >= 0)))
3904 tem = double_int_to_tree (TREE_TYPE (init), dtmp);
3905 extract_range_from_binary_expr (&maxvr, PLUS_EXPR,
3906 TREE_TYPE (init), init, tem);
3907 /* Likewise if the addition did. */
3908 if (maxvr.type == VR_RANGE)
3910 tmin = maxvr.min;
3911 tmax = maxvr.max;
3917 if (vr->type == VR_VARYING || vr->type == VR_UNDEFINED)
3919 min = tmin;
3920 max = tmax;
3922 /* For VARYING or UNDEFINED ranges, just about anything we get
3923 from scalar evolutions should be better. */
3925 if (dir == EV_DIR_DECREASES)
3926 max = init;
3927 else
3928 min = init;
3930 /* If we would create an invalid range, then just assume we
3931 know absolutely nothing. This may be over-conservative,
3932 but it's clearly safe, and should happen only in unreachable
3933 parts of code, or for invalid programs. */
3934 if (compare_values (min, max) == 1)
3935 return;
3937 set_value_range (vr, VR_RANGE, min, max, vr->equiv);
3939 else if (vr->type == VR_RANGE)
3941 min = vr->min;
3942 max = vr->max;
3944 if (dir == EV_DIR_DECREASES)
3946 /* INIT is the maximum value. If INIT is lower than VR->MAX
3947 but no smaller than VR->MIN, set VR->MAX to INIT. */
3948 if (compare_values (init, max) == -1)
3949 max = init;
3951 /* According to the loop information, the variable does not
3952 overflow. If we think it does, probably because of an
3953 overflow due to arithmetic on a different INF value,
3954 reset now. */
3955 if (is_negative_overflow_infinity (min)
3956 || compare_values (min, tmin) == -1)
3957 min = tmin;
3960 else
3962 /* If INIT is bigger than VR->MIN, set VR->MIN to INIT. */
3963 if (compare_values (init, min) == 1)
3964 min = init;
3966 if (is_positive_overflow_infinity (max)
3967 || compare_values (tmax, max) == -1)
3968 max = tmax;
3971 /* If we just created an invalid range with the minimum
3972 greater than the maximum, we fail conservatively.
3973 This should happen only in unreachable
3974 parts of code, or for invalid programs. */
3975 if (compare_values (min, max) == 1)
3976 return;
3978 set_value_range (vr, VR_RANGE, min, max, vr->equiv);
3982 /* Return true if VAR may overflow at STMT. This checks any available
3983 loop information to see if we can determine that VAR does not
3984 overflow. */
3986 static bool
3987 vrp_var_may_overflow (tree var, gimple stmt)
3989 struct loop *l;
3990 tree chrec, init, step;
3992 if (current_loops == NULL)
3993 return true;
3995 l = loop_containing_stmt (stmt);
3996 if (l == NULL
3997 || !loop_outer (l))
3998 return true;
4000 chrec = instantiate_parameters (l, analyze_scalar_evolution (l, var));
4001 if (TREE_CODE (chrec) != POLYNOMIAL_CHREC)
4002 return true;
4004 init = initial_condition_in_loop_num (chrec, l->num);
4005 step = evolution_part_in_loop_num (chrec, l->num);
4007 if (step == NULL_TREE
4008 || !is_gimple_min_invariant (step)
4009 || !valid_value_p (init))
4010 return true;
4012 /* If we get here, we know something useful about VAR based on the
4013 loop information. If it wraps, it may overflow. */
4015 if (scev_probably_wraps_p (init, step, stmt, get_chrec_loop (chrec),
4016 true))
4017 return true;
4019 if (dump_file && (dump_flags & TDF_DETAILS) != 0)
4021 print_generic_expr (dump_file, var, 0);
4022 fprintf (dump_file, ": loop information indicates does not overflow\n");
4025 return false;
4029 /* Given two numeric value ranges VR0, VR1 and a comparison code COMP:
4031 - Return BOOLEAN_TRUE_NODE if VR0 COMP VR1 always returns true for
4032 all the values in the ranges.
4034 - Return BOOLEAN_FALSE_NODE if the comparison always returns false.
4036 - Return NULL_TREE if it is not always possible to determine the
4037 value of the comparison.
4039 Also set *STRICT_OVERFLOW_P to indicate whether a range with an
4040 overflow infinity was used in the test. */
4043 static tree
4044 compare_ranges (enum tree_code comp, value_range_t *vr0, value_range_t *vr1,
4045 bool *strict_overflow_p)
4047 /* VARYING or UNDEFINED ranges cannot be compared. */
4048 if (vr0->type == VR_VARYING
4049 || vr0->type == VR_UNDEFINED
4050 || vr1->type == VR_VARYING
4051 || vr1->type == VR_UNDEFINED)
4052 return NULL_TREE;
4054 /* Anti-ranges need to be handled separately. */
4055 if (vr0->type == VR_ANTI_RANGE || vr1->type == VR_ANTI_RANGE)
4057 /* If both are anti-ranges, then we cannot compute any
4058 comparison. */
4059 if (vr0->type == VR_ANTI_RANGE && vr1->type == VR_ANTI_RANGE)
4060 return NULL_TREE;
4062 /* These comparisons are never statically computable. */
4063 if (comp == GT_EXPR
4064 || comp == GE_EXPR
4065 || comp == LT_EXPR
4066 || comp == LE_EXPR)
4067 return NULL_TREE;
4069 /* Equality can be computed only between a range and an
4070 anti-range. ~[VAL1, VAL2] == [VAL1, VAL2] is always false. */
4071 if (vr0->type == VR_RANGE)
4073 /* To simplify processing, make VR0 the anti-range. */
4074 value_range_t *tmp = vr0;
4075 vr0 = vr1;
4076 vr1 = tmp;
4079 gcc_assert (comp == NE_EXPR || comp == EQ_EXPR);
4081 if (compare_values_warnv (vr0->min, vr1->min, strict_overflow_p) == 0
4082 && compare_values_warnv (vr0->max, vr1->max, strict_overflow_p) == 0)
4083 return (comp == NE_EXPR) ? boolean_true_node : boolean_false_node;
4085 return NULL_TREE;
4088 if (!usable_range_p (vr0, strict_overflow_p)
4089 || !usable_range_p (vr1, strict_overflow_p))
4090 return NULL_TREE;
4092 /* Simplify processing. If COMP is GT_EXPR or GE_EXPR, switch the
4093 operands around and change the comparison code. */
4094 if (comp == GT_EXPR || comp == GE_EXPR)
4096 value_range_t *tmp;
4097 comp = (comp == GT_EXPR) ? LT_EXPR : LE_EXPR;
4098 tmp = vr0;
4099 vr0 = vr1;
4100 vr1 = tmp;
4103 if (comp == EQ_EXPR)
4105 /* Equality may only be computed if both ranges represent
4106 exactly one value. */
4107 if (compare_values_warnv (vr0->min, vr0->max, strict_overflow_p) == 0
4108 && compare_values_warnv (vr1->min, vr1->max, strict_overflow_p) == 0)
4110 int cmp_min = compare_values_warnv (vr0->min, vr1->min,
4111 strict_overflow_p);
4112 int cmp_max = compare_values_warnv (vr0->max, vr1->max,
4113 strict_overflow_p);
4114 if (cmp_min == 0 && cmp_max == 0)
4115 return boolean_true_node;
4116 else if (cmp_min != -2 && cmp_max != -2)
4117 return boolean_false_node;
4119 /* If [V0_MIN, V1_MAX] < [V1_MIN, V1_MAX] then V0 != V1. */
4120 else if (compare_values_warnv (vr0->min, vr1->max,
4121 strict_overflow_p) == 1
4122 || compare_values_warnv (vr1->min, vr0->max,
4123 strict_overflow_p) == 1)
4124 return boolean_false_node;
4126 return NULL_TREE;
4128 else if (comp == NE_EXPR)
4130 int cmp1, cmp2;
4132 /* If VR0 is completely to the left or completely to the right
4133 of VR1, they are always different. Notice that we need to
4134 make sure that both comparisons yield similar results to
4135 avoid comparing values that cannot be compared at
4136 compile-time. */
4137 cmp1 = compare_values_warnv (vr0->max, vr1->min, strict_overflow_p);
4138 cmp2 = compare_values_warnv (vr0->min, vr1->max, strict_overflow_p);
4139 if ((cmp1 == -1 && cmp2 == -1) || (cmp1 == 1 && cmp2 == 1))
4140 return boolean_true_node;
4142 /* If VR0 and VR1 represent a single value and are identical,
4143 return false. */
4144 else if (compare_values_warnv (vr0->min, vr0->max,
4145 strict_overflow_p) == 0
4146 && compare_values_warnv (vr1->min, vr1->max,
4147 strict_overflow_p) == 0
4148 && compare_values_warnv (vr0->min, vr1->min,
4149 strict_overflow_p) == 0
4150 && compare_values_warnv (vr0->max, vr1->max,
4151 strict_overflow_p) == 0)
4152 return boolean_false_node;
4154 /* Otherwise, they may or may not be different. */
4155 else
4156 return NULL_TREE;
4158 else if (comp == LT_EXPR || comp == LE_EXPR)
4160 int tst;
4162 /* If VR0 is to the left of VR1, return true. */
4163 tst = compare_values_warnv (vr0->max, vr1->min, strict_overflow_p);
4164 if ((comp == LT_EXPR && tst == -1)
4165 || (comp == LE_EXPR && (tst == -1 || tst == 0)))
4167 if (overflow_infinity_range_p (vr0)
4168 || overflow_infinity_range_p (vr1))
4169 *strict_overflow_p = true;
4170 return boolean_true_node;
4173 /* If VR0 is to the right of VR1, return false. */
4174 tst = compare_values_warnv (vr0->min, vr1->max, strict_overflow_p);
4175 if ((comp == LT_EXPR && (tst == 0 || tst == 1))
4176 || (comp == LE_EXPR && tst == 1))
4178 if (overflow_infinity_range_p (vr0)
4179 || overflow_infinity_range_p (vr1))
4180 *strict_overflow_p = true;
4181 return boolean_false_node;
4184 /* Otherwise, we don't know. */
4185 return NULL_TREE;
4188 gcc_unreachable ();
4192 /* Given a value range VR, a value VAL and a comparison code COMP, return
4193 BOOLEAN_TRUE_NODE if VR COMP VAL always returns true for all the
4194 values in VR. Return BOOLEAN_FALSE_NODE if the comparison
4195 always returns false. Return NULL_TREE if it is not always
4196 possible to determine the value of the comparison. Also set
4197 *STRICT_OVERFLOW_P to indicate whether a range with an overflow
4198 infinity was used in the test. */
4200 static tree
4201 compare_range_with_value (enum tree_code comp, value_range_t *vr, tree val,
4202 bool *strict_overflow_p)
4204 if (vr->type == VR_VARYING || vr->type == VR_UNDEFINED)
4205 return NULL_TREE;
4207 /* Anti-ranges need to be handled separately. */
4208 if (vr->type == VR_ANTI_RANGE)
4210 /* For anti-ranges, the only predicates that we can compute at
4211 compile time are equality and inequality. */
4212 if (comp == GT_EXPR
4213 || comp == GE_EXPR
4214 || comp == LT_EXPR
4215 || comp == LE_EXPR)
4216 return NULL_TREE;
4218 /* ~[VAL_1, VAL_2] OP VAL is known if VAL_1 <= VAL <= VAL_2. */
4219 if (value_inside_range (val, vr->min, vr->max) == 1)
4220 return (comp == NE_EXPR) ? boolean_true_node : boolean_false_node;
4222 return NULL_TREE;
4225 if (!usable_range_p (vr, strict_overflow_p))
4226 return NULL_TREE;
4228 if (comp == EQ_EXPR)
4230 /* EQ_EXPR may only be computed if VR represents exactly
4231 one value. */
4232 if (compare_values_warnv (vr->min, vr->max, strict_overflow_p) == 0)
4234 int cmp = compare_values_warnv (vr->min, val, strict_overflow_p);
4235 if (cmp == 0)
4236 return boolean_true_node;
4237 else if (cmp == -1 || cmp == 1 || cmp == 2)
4238 return boolean_false_node;
4240 else if (compare_values_warnv (val, vr->min, strict_overflow_p) == -1
4241 || compare_values_warnv (vr->max, val, strict_overflow_p) == -1)
4242 return boolean_false_node;
4244 return NULL_TREE;
4246 else if (comp == NE_EXPR)
4248 /* If VAL is not inside VR, then they are always different. */
4249 if (compare_values_warnv (vr->max, val, strict_overflow_p) == -1
4250 || compare_values_warnv (vr->min, val, strict_overflow_p) == 1)
4251 return boolean_true_node;
4253 /* If VR represents exactly one value equal to VAL, then return
4254 false. */
4255 if (compare_values_warnv (vr->min, vr->max, strict_overflow_p) == 0
4256 && compare_values_warnv (vr->min, val, strict_overflow_p) == 0)
4257 return boolean_false_node;
4259 /* Otherwise, they may or may not be different. */
4260 return NULL_TREE;
4262 else if (comp == LT_EXPR || comp == LE_EXPR)
4264 int tst;
4266 /* If VR is to the left of VAL, return true. */
4267 tst = compare_values_warnv (vr->max, val, strict_overflow_p);
4268 if ((comp == LT_EXPR && tst == -1)
4269 || (comp == LE_EXPR && (tst == -1 || tst == 0)))
4271 if (overflow_infinity_range_p (vr))
4272 *strict_overflow_p = true;
4273 return boolean_true_node;
4276 /* If VR is to the right of VAL, return false. */
4277 tst = compare_values_warnv (vr->min, val, strict_overflow_p);
4278 if ((comp == LT_EXPR && (tst == 0 || tst == 1))
4279 || (comp == LE_EXPR && tst == 1))
4281 if (overflow_infinity_range_p (vr))
4282 *strict_overflow_p = true;
4283 return boolean_false_node;
4286 /* Otherwise, we don't know. */
4287 return NULL_TREE;
4289 else if (comp == GT_EXPR || comp == GE_EXPR)
4291 int tst;
4293 /* If VR is to the right of VAL, return true. */
4294 tst = compare_values_warnv (vr->min, val, strict_overflow_p);
4295 if ((comp == GT_EXPR && tst == 1)
4296 || (comp == GE_EXPR && (tst == 0 || tst == 1)))
4298 if (overflow_infinity_range_p (vr))
4299 *strict_overflow_p = true;
4300 return boolean_true_node;
4303 /* If VR is to the left of VAL, return false. */
4304 tst = compare_values_warnv (vr->max, val, strict_overflow_p);
4305 if ((comp == GT_EXPR && (tst == -1 || tst == 0))
4306 || (comp == GE_EXPR && tst == -1))
4308 if (overflow_infinity_range_p (vr))
4309 *strict_overflow_p = true;
4310 return boolean_false_node;
4313 /* Otherwise, we don't know. */
4314 return NULL_TREE;
4317 gcc_unreachable ();
4321 /* Debugging dumps. */
4323 void dump_value_range (FILE *, value_range_t *);
4324 void debug_value_range (value_range_t *);
4325 void dump_all_value_ranges (FILE *);
4326 void debug_all_value_ranges (void);
4327 void dump_vr_equiv (FILE *, bitmap);
4328 void debug_vr_equiv (bitmap);
4331 /* Dump value range VR to FILE. */
4333 void
4334 dump_value_range (FILE *file, value_range_t *vr)
4336 if (vr == NULL)
4337 fprintf (file, "[]");
4338 else if (vr->type == VR_UNDEFINED)
4339 fprintf (file, "UNDEFINED");
4340 else if (vr->type == VR_RANGE || vr->type == VR_ANTI_RANGE)
4342 tree type = TREE_TYPE (vr->min);
4344 fprintf (file, "%s[", (vr->type == VR_ANTI_RANGE) ? "~" : "");
4346 if (is_negative_overflow_infinity (vr->min))
4347 fprintf (file, "-INF(OVF)");
4348 else if (INTEGRAL_TYPE_P (type)
4349 && !TYPE_UNSIGNED (type)
4350 && vrp_val_is_min (vr->min))
4351 fprintf (file, "-INF");
4352 else
4353 print_generic_expr (file, vr->min, 0);
4355 fprintf (file, ", ");
4357 if (is_positive_overflow_infinity (vr->max))
4358 fprintf (file, "+INF(OVF)");
4359 else if (INTEGRAL_TYPE_P (type)
4360 && vrp_val_is_max (vr->max))
4361 fprintf (file, "+INF");
4362 else
4363 print_generic_expr (file, vr->max, 0);
4365 fprintf (file, "]");
4367 if (vr->equiv)
4369 bitmap_iterator bi;
4370 unsigned i, c = 0;
4372 fprintf (file, " EQUIVALENCES: { ");
4374 EXECUTE_IF_SET_IN_BITMAP (vr->equiv, 0, i, bi)
4376 print_generic_expr (file, ssa_name (i), 0);
4377 fprintf (file, " ");
4378 c++;
4381 fprintf (file, "} (%u elements)", c);
4384 else if (vr->type == VR_VARYING)
4385 fprintf (file, "VARYING");
4386 else
4387 fprintf (file, "INVALID RANGE");
4391 /* Dump value range VR to stderr. */
4393 DEBUG_FUNCTION void
4394 debug_value_range (value_range_t *vr)
4396 dump_value_range (stderr, vr);
4397 fprintf (stderr, "\n");
4401 /* Dump value ranges of all SSA_NAMEs to FILE. */
4403 void
4404 dump_all_value_ranges (FILE *file)
4406 size_t i;
4408 for (i = 0; i < num_vr_values; i++)
4410 if (vr_value[i])
4412 print_generic_expr (file, ssa_name (i), 0);
4413 fprintf (file, ": ");
4414 dump_value_range (file, vr_value[i]);
4415 fprintf (file, "\n");
4419 fprintf (file, "\n");
4423 /* Dump all value ranges to stderr. */
4425 DEBUG_FUNCTION void
4426 debug_all_value_ranges (void)
4428 dump_all_value_ranges (stderr);
4432 /* Given a COND_EXPR COND of the form 'V OP W', and an SSA name V,
4433 create a new SSA name N and return the assertion assignment
4434 'V = ASSERT_EXPR <V, V OP W>'. */
4436 static gimple
4437 build_assert_expr_for (tree cond, tree v)
4439 tree a;
4440 gimple assertion;
4442 gcc_assert (TREE_CODE (v) == SSA_NAME
4443 && COMPARISON_CLASS_P (cond));
4445 a = build2 (ASSERT_EXPR, TREE_TYPE (v), v, cond);
4446 assertion = gimple_build_assign (NULL_TREE, a);
4448 /* The new ASSERT_EXPR, creates a new SSA name that replaces the
4449 operand of the ASSERT_EXPR. Create it so the new name and the old one
4450 are registered in the replacement table so that we can fix the SSA web
4451 after adding all the ASSERT_EXPRs. */
4452 create_new_def_for (v, assertion, NULL);
4454 return assertion;
4458 /* Return false if EXPR is a predicate expression involving floating
4459 point values. */
4461 static inline bool
4462 fp_predicate (gimple stmt)
4464 GIMPLE_CHECK (stmt, GIMPLE_COND);
4466 return FLOAT_TYPE_P (TREE_TYPE (gimple_cond_lhs (stmt)));
4470 /* If OP can be inferred to be non-zero after STMT executes, return true. */
4472 static bool
4473 infer_nonnull_range (gimple stmt, tree op)
4475 /* We can only assume that a pointer dereference will yield
4476 non-NULL if -fdelete-null-pointer-checks is enabled. */
4477 if (!flag_delete_null_pointer_checks
4478 || !POINTER_TYPE_P (TREE_TYPE (op))
4479 || gimple_code (stmt) == GIMPLE_ASM)
4480 return false;
4482 unsigned num_uses, num_loads, num_stores;
4484 count_uses_and_derefs (op, stmt, &num_uses, &num_loads, &num_stores);
4485 if (num_loads + num_stores > 0)
4486 return true;
4488 if (is_gimple_call (stmt) && !gimple_call_internal_p (stmt))
4490 tree fntype = gimple_call_fntype (stmt);
4491 tree attrs = TYPE_ATTRIBUTES (fntype);
4492 for (; attrs; attrs = TREE_CHAIN (attrs))
4494 attrs = lookup_attribute ("nonnull", attrs);
4496 /* If "nonnull" wasn't specified, we know nothing about
4497 the argument. */
4498 if (attrs == NULL_TREE)
4499 return false;
4501 /* If "nonnull" applies to all the arguments, then ARG
4502 is non-null. */
4503 if (TREE_VALUE (attrs) == NULL_TREE)
4504 return true;
4506 /* Now see if op appears in the nonnull list. */
4507 for (tree t = TREE_VALUE (attrs); t; t = TREE_CHAIN (t))
4509 int idx = TREE_INT_CST_LOW (TREE_VALUE (t)) - 1;
4510 tree arg = gimple_call_arg (stmt, idx);
4511 if (op == arg)
4512 return true;
4517 return false;
4520 /* If the range of values taken by OP can be inferred after STMT executes,
4521 return the comparison code (COMP_CODE_P) and value (VAL_P) that
4522 describes the inferred range. Return true if a range could be
4523 inferred. */
4525 static bool
4526 infer_value_range (gimple stmt, tree op, enum tree_code *comp_code_p, tree *val_p)
4528 *val_p = NULL_TREE;
4529 *comp_code_p = ERROR_MARK;
4531 /* Do not attempt to infer anything in names that flow through
4532 abnormal edges. */
4533 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op))
4534 return false;
4536 /* Similarly, don't infer anything from statements that may throw
4537 exceptions. ??? Relax this requirement? */
4538 if (stmt_could_throw_p (stmt))
4539 return false;
4541 /* If STMT is the last statement of a basic block with no
4542 successors, there is no point inferring anything about any of its
4543 operands. We would not be able to find a proper insertion point
4544 for the assertion, anyway. */
4545 if (stmt_ends_bb_p (stmt) && EDGE_COUNT (gimple_bb (stmt)->succs) == 0)
4546 return false;
4548 if (infer_nonnull_range (stmt, op))
4550 *val_p = build_int_cst (TREE_TYPE (op), 0);
4551 *comp_code_p = NE_EXPR;
4552 return true;
4555 return false;
4559 void dump_asserts_for (FILE *, tree);
4560 void debug_asserts_for (tree);
4561 void dump_all_asserts (FILE *);
4562 void debug_all_asserts (void);
4564 /* Dump all the registered assertions for NAME to FILE. */
4566 void
4567 dump_asserts_for (FILE *file, tree name)
4569 assert_locus_t loc;
4571 fprintf (file, "Assertions to be inserted for ");
4572 print_generic_expr (file, name, 0);
4573 fprintf (file, "\n");
4575 loc = asserts_for[SSA_NAME_VERSION (name)];
4576 while (loc)
4578 fprintf (file, "\t");
4579 print_gimple_stmt (file, gsi_stmt (loc->si), 0, 0);
4580 fprintf (file, "\n\tBB #%d", loc->bb->index);
4581 if (loc->e)
4583 fprintf (file, "\n\tEDGE %d->%d", loc->e->src->index,
4584 loc->e->dest->index);
4585 dump_edge_info (file, loc->e, dump_flags, 0);
4587 fprintf (file, "\n\tPREDICATE: ");
4588 print_generic_expr (file, name, 0);
4589 fprintf (file, " %s ", get_tree_code_name (loc->comp_code));
4590 print_generic_expr (file, loc->val, 0);
4591 fprintf (file, "\n\n");
4592 loc = loc->next;
4595 fprintf (file, "\n");
4599 /* Dump all the registered assertions for NAME to stderr. */
4601 DEBUG_FUNCTION void
4602 debug_asserts_for (tree name)
4604 dump_asserts_for (stderr, name);
4608 /* Dump all the registered assertions for all the names to FILE. */
4610 void
4611 dump_all_asserts (FILE *file)
4613 unsigned i;
4614 bitmap_iterator bi;
4616 fprintf (file, "\nASSERT_EXPRs to be inserted\n\n");
4617 EXECUTE_IF_SET_IN_BITMAP (need_assert_for, 0, i, bi)
4618 dump_asserts_for (file, ssa_name (i));
4619 fprintf (file, "\n");
4623 /* Dump all the registered assertions for all the names to stderr. */
4625 DEBUG_FUNCTION void
4626 debug_all_asserts (void)
4628 dump_all_asserts (stderr);
4632 /* If NAME doesn't have an ASSERT_EXPR registered for asserting
4633 'EXPR COMP_CODE VAL' at a location that dominates block BB or
4634 E->DEST, then register this location as a possible insertion point
4635 for ASSERT_EXPR <NAME, EXPR COMP_CODE VAL>.
4637 BB, E and SI provide the exact insertion point for the new
4638 ASSERT_EXPR. If BB is NULL, then the ASSERT_EXPR is to be inserted
4639 on edge E. Otherwise, if E is NULL, the ASSERT_EXPR is inserted on
4640 BB. If SI points to a COND_EXPR or a SWITCH_EXPR statement, then E
4641 must not be NULL. */
4643 static void
4644 register_new_assert_for (tree name, tree expr,
4645 enum tree_code comp_code,
4646 tree val,
4647 basic_block bb,
4648 edge e,
4649 gimple_stmt_iterator si)
4651 assert_locus_t n, loc, last_loc;
4652 basic_block dest_bb;
4654 gcc_checking_assert (bb == NULL || e == NULL);
4656 if (e == NULL)
4657 gcc_checking_assert (gimple_code (gsi_stmt (si)) != GIMPLE_COND
4658 && gimple_code (gsi_stmt (si)) != GIMPLE_SWITCH);
4660 /* Never build an assert comparing against an integer constant with
4661 TREE_OVERFLOW set. This confuses our undefined overflow warning
4662 machinery. */
4663 if (TREE_CODE (val) == INTEGER_CST
4664 && TREE_OVERFLOW (val))
4665 val = build_int_cst_wide (TREE_TYPE (val),
4666 TREE_INT_CST_LOW (val), TREE_INT_CST_HIGH (val));
4668 /* The new assertion A will be inserted at BB or E. We need to
4669 determine if the new location is dominated by a previously
4670 registered location for A. If we are doing an edge insertion,
4671 assume that A will be inserted at E->DEST. Note that this is not
4672 necessarily true.
4674 If E is a critical edge, it will be split. But even if E is
4675 split, the new block will dominate the same set of blocks that
4676 E->DEST dominates.
4678 The reverse, however, is not true, blocks dominated by E->DEST
4679 will not be dominated by the new block created to split E. So,
4680 if the insertion location is on a critical edge, we will not use
4681 the new location to move another assertion previously registered
4682 at a block dominated by E->DEST. */
4683 dest_bb = (bb) ? bb : e->dest;
4685 /* If NAME already has an ASSERT_EXPR registered for COMP_CODE and
4686 VAL at a block dominating DEST_BB, then we don't need to insert a new
4687 one. Similarly, if the same assertion already exists at a block
4688 dominated by DEST_BB and the new location is not on a critical
4689 edge, then update the existing location for the assertion (i.e.,
4690 move the assertion up in the dominance tree).
4692 Note, this is implemented as a simple linked list because there
4693 should not be more than a handful of assertions registered per
4694 name. If this becomes a performance problem, a table hashed by
4695 COMP_CODE and VAL could be implemented. */
4696 loc = asserts_for[SSA_NAME_VERSION (name)];
4697 last_loc = loc;
4698 while (loc)
4700 if (loc->comp_code == comp_code
4701 && (loc->val == val
4702 || operand_equal_p (loc->val, val, 0))
4703 && (loc->expr == expr
4704 || operand_equal_p (loc->expr, expr, 0)))
4706 /* If E is not a critical edge and DEST_BB
4707 dominates the existing location for the assertion, move
4708 the assertion up in the dominance tree by updating its
4709 location information. */
4710 if ((e == NULL || !EDGE_CRITICAL_P (e))
4711 && dominated_by_p (CDI_DOMINATORS, loc->bb, dest_bb))
4713 loc->bb = dest_bb;
4714 loc->e = e;
4715 loc->si = si;
4716 return;
4720 /* Update the last node of the list and move to the next one. */
4721 last_loc = loc;
4722 loc = loc->next;
4725 /* If we didn't find an assertion already registered for
4726 NAME COMP_CODE VAL, add a new one at the end of the list of
4727 assertions associated with NAME. */
4728 n = XNEW (struct assert_locus_d);
4729 n->bb = dest_bb;
4730 n->e = e;
4731 n->si = si;
4732 n->comp_code = comp_code;
4733 n->val = val;
4734 n->expr = expr;
4735 n->next = NULL;
4737 if (last_loc)
4738 last_loc->next = n;
4739 else
4740 asserts_for[SSA_NAME_VERSION (name)] = n;
4742 bitmap_set_bit (need_assert_for, SSA_NAME_VERSION (name));
4745 /* (COND_OP0 COND_CODE COND_OP1) is a predicate which uses NAME.
4746 Extract a suitable test code and value and store them into *CODE_P and
4747 *VAL_P so the predicate is normalized to NAME *CODE_P *VAL_P.
4749 If no extraction was possible, return FALSE, otherwise return TRUE.
4751 If INVERT is true, then we invert the result stored into *CODE_P. */
4753 static bool
4754 extract_code_and_val_from_cond_with_ops (tree name, enum tree_code cond_code,
4755 tree cond_op0, tree cond_op1,
4756 bool invert, enum tree_code *code_p,
4757 tree *val_p)
4759 enum tree_code comp_code;
4760 tree val;
4762 /* Otherwise, we have a comparison of the form NAME COMP VAL
4763 or VAL COMP NAME. */
4764 if (name == cond_op1)
4766 /* If the predicate is of the form VAL COMP NAME, flip
4767 COMP around because we need to register NAME as the
4768 first operand in the predicate. */
4769 comp_code = swap_tree_comparison (cond_code);
4770 val = cond_op0;
4772 else
4774 /* The comparison is of the form NAME COMP VAL, so the
4775 comparison code remains unchanged. */
4776 comp_code = cond_code;
4777 val = cond_op1;
4780 /* Invert the comparison code as necessary. */
4781 if (invert)
4782 comp_code = invert_tree_comparison (comp_code, 0);
4784 /* VRP does not handle float types. */
4785 if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (val)))
4786 return false;
4788 /* Do not register always-false predicates.
4789 FIXME: this works around a limitation in fold() when dealing with
4790 enumerations. Given 'enum { N1, N2 } x;', fold will not
4791 fold 'if (x > N2)' to 'if (0)'. */
4792 if ((comp_code == GT_EXPR || comp_code == LT_EXPR)
4793 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
4795 tree min = TYPE_MIN_VALUE (TREE_TYPE (val));
4796 tree max = TYPE_MAX_VALUE (TREE_TYPE (val));
4798 if (comp_code == GT_EXPR
4799 && (!max
4800 || compare_values (val, max) == 0))
4801 return false;
4803 if (comp_code == LT_EXPR
4804 && (!min
4805 || compare_values (val, min) == 0))
4806 return false;
4808 *code_p = comp_code;
4809 *val_p = val;
4810 return true;
4813 /* Find out smallest RES where RES > VAL && (RES & MASK) == RES, if any
4814 (otherwise return VAL). VAL and MASK must be zero-extended for
4815 precision PREC. If SGNBIT is non-zero, first xor VAL with SGNBIT
4816 (to transform signed values into unsigned) and at the end xor
4817 SGNBIT back. */
4819 static double_int
4820 masked_increment (double_int val, double_int mask, double_int sgnbit,
4821 unsigned int prec)
4823 double_int bit = double_int_one, res;
4824 unsigned int i;
4826 val ^= sgnbit;
4827 for (i = 0; i < prec; i++, bit += bit)
4829 res = mask;
4830 if ((res & bit).is_zero ())
4831 continue;
4832 res = bit - double_int_one;
4833 res = (val + bit).and_not (res);
4834 res &= mask;
4835 if (res.ugt (val))
4836 return res ^ sgnbit;
4838 return val ^ sgnbit;
4841 /* Try to register an edge assertion for SSA name NAME on edge E for
4842 the condition COND contributing to the conditional jump pointed to by BSI.
4843 Invert the condition COND if INVERT is true.
4844 Return true if an assertion for NAME could be registered. */
4846 static bool
4847 register_edge_assert_for_2 (tree name, edge e, gimple_stmt_iterator bsi,
4848 enum tree_code cond_code,
4849 tree cond_op0, tree cond_op1, bool invert)
4851 tree val;
4852 enum tree_code comp_code;
4853 bool retval = false;
4855 if (!extract_code_and_val_from_cond_with_ops (name, cond_code,
4856 cond_op0,
4857 cond_op1,
4858 invert, &comp_code, &val))
4859 return false;
4861 /* Only register an ASSERT_EXPR if NAME was found in the sub-graph
4862 reachable from E. */
4863 if (live_on_edge (e, name)
4864 && !has_single_use (name))
4866 register_new_assert_for (name, name, comp_code, val, NULL, e, bsi);
4867 retval = true;
4870 /* In the case of NAME <= CST and NAME being defined as
4871 NAME = (unsigned) NAME2 + CST2 we can assert NAME2 >= -CST2
4872 and NAME2 <= CST - CST2. We can do the same for NAME > CST.
4873 This catches range and anti-range tests. */
4874 if ((comp_code == LE_EXPR
4875 || comp_code == GT_EXPR)
4876 && TREE_CODE (val) == INTEGER_CST
4877 && TYPE_UNSIGNED (TREE_TYPE (val)))
4879 gimple def_stmt = SSA_NAME_DEF_STMT (name);
4880 tree cst2 = NULL_TREE, name2 = NULL_TREE, name3 = NULL_TREE;
4882 /* Extract CST2 from the (optional) addition. */
4883 if (is_gimple_assign (def_stmt)
4884 && gimple_assign_rhs_code (def_stmt) == PLUS_EXPR)
4886 name2 = gimple_assign_rhs1 (def_stmt);
4887 cst2 = gimple_assign_rhs2 (def_stmt);
4888 if (TREE_CODE (name2) == SSA_NAME
4889 && TREE_CODE (cst2) == INTEGER_CST)
4890 def_stmt = SSA_NAME_DEF_STMT (name2);
4893 /* Extract NAME2 from the (optional) sign-changing cast. */
4894 if (gimple_assign_cast_p (def_stmt))
4896 if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt))
4897 && ! TYPE_UNSIGNED (TREE_TYPE (gimple_assign_rhs1 (def_stmt)))
4898 && (TYPE_PRECISION (gimple_expr_type (def_stmt))
4899 == TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (def_stmt)))))
4900 name3 = gimple_assign_rhs1 (def_stmt);
4903 /* If name3 is used later, create an ASSERT_EXPR for it. */
4904 if (name3 != NULL_TREE
4905 && TREE_CODE (name3) == SSA_NAME
4906 && (cst2 == NULL_TREE
4907 || TREE_CODE (cst2) == INTEGER_CST)
4908 && INTEGRAL_TYPE_P (TREE_TYPE (name3))
4909 && live_on_edge (e, name3)
4910 && !has_single_use (name3))
4912 tree tmp;
4914 /* Build an expression for the range test. */
4915 tmp = build1 (NOP_EXPR, TREE_TYPE (name), name3);
4916 if (cst2 != NULL_TREE)
4917 tmp = build2 (PLUS_EXPR, TREE_TYPE (name), tmp, cst2);
4919 if (dump_file)
4921 fprintf (dump_file, "Adding assert for ");
4922 print_generic_expr (dump_file, name3, 0);
4923 fprintf (dump_file, " from ");
4924 print_generic_expr (dump_file, tmp, 0);
4925 fprintf (dump_file, "\n");
4928 register_new_assert_for (name3, tmp, comp_code, val, NULL, e, bsi);
4930 retval = true;
4933 /* If name2 is used later, create an ASSERT_EXPR for it. */
4934 if (name2 != NULL_TREE
4935 && TREE_CODE (name2) == SSA_NAME
4936 && TREE_CODE (cst2) == INTEGER_CST
4937 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
4938 && live_on_edge (e, name2)
4939 && !has_single_use (name2))
4941 tree tmp;
4943 /* Build an expression for the range test. */
4944 tmp = name2;
4945 if (TREE_TYPE (name) != TREE_TYPE (name2))
4946 tmp = build1 (NOP_EXPR, TREE_TYPE (name), tmp);
4947 if (cst2 != NULL_TREE)
4948 tmp = build2 (PLUS_EXPR, TREE_TYPE (name), tmp, cst2);
4950 if (dump_file)
4952 fprintf (dump_file, "Adding assert for ");
4953 print_generic_expr (dump_file, name2, 0);
4954 fprintf (dump_file, " from ");
4955 print_generic_expr (dump_file, tmp, 0);
4956 fprintf (dump_file, "\n");
4959 register_new_assert_for (name2, tmp, comp_code, val, NULL, e, bsi);
4961 retval = true;
4965 /* In the case of post-in/decrement tests like if (i++) ... and uses
4966 of the in/decremented value on the edge the extra name we want to
4967 assert for is not on the def chain of the name compared. Instead
4968 it is in the set of use stmts. */
4969 if ((comp_code == NE_EXPR
4970 || comp_code == EQ_EXPR)
4971 && TREE_CODE (val) == INTEGER_CST)
4973 imm_use_iterator ui;
4974 gimple use_stmt;
4975 FOR_EACH_IMM_USE_STMT (use_stmt, ui, name)
4977 /* Cut off to use-stmts that are in the predecessor. */
4978 if (gimple_bb (use_stmt) != e->src)
4979 continue;
4981 if (!is_gimple_assign (use_stmt))
4982 continue;
4984 enum tree_code code = gimple_assign_rhs_code (use_stmt);
4985 if (code != PLUS_EXPR
4986 && code != MINUS_EXPR)
4987 continue;
4989 tree cst = gimple_assign_rhs2 (use_stmt);
4990 if (TREE_CODE (cst) != INTEGER_CST)
4991 continue;
4993 tree name2 = gimple_assign_lhs (use_stmt);
4994 if (live_on_edge (e, name2))
4996 cst = int_const_binop (code, val, cst);
4997 register_new_assert_for (name2, name2, comp_code, cst,
4998 NULL, e, bsi);
4999 retval = true;
5004 if (TREE_CODE_CLASS (comp_code) == tcc_comparison
5005 && TREE_CODE (val) == INTEGER_CST)
5007 gimple def_stmt = SSA_NAME_DEF_STMT (name);
5008 tree name2 = NULL_TREE, names[2], cst2 = NULL_TREE;
5009 tree val2 = NULL_TREE;
5010 double_int mask = double_int_zero;
5011 unsigned int prec = TYPE_PRECISION (TREE_TYPE (val));
5012 unsigned int nprec = prec;
5013 enum tree_code rhs_code = ERROR_MARK;
5015 if (is_gimple_assign (def_stmt))
5016 rhs_code = gimple_assign_rhs_code (def_stmt);
5018 /* Add asserts for NAME cmp CST and NAME being defined
5019 as NAME = (int) NAME2. */
5020 if (!TYPE_UNSIGNED (TREE_TYPE (val))
5021 && (comp_code == LE_EXPR || comp_code == LT_EXPR
5022 || comp_code == GT_EXPR || comp_code == GE_EXPR)
5023 && gimple_assign_cast_p (def_stmt))
5025 name2 = gimple_assign_rhs1 (def_stmt);
5026 if (CONVERT_EXPR_CODE_P (rhs_code)
5027 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
5028 && TYPE_UNSIGNED (TREE_TYPE (name2))
5029 && prec == TYPE_PRECISION (TREE_TYPE (name2))
5030 && (comp_code == LE_EXPR || comp_code == GT_EXPR
5031 || !tree_int_cst_equal (val,
5032 TYPE_MIN_VALUE (TREE_TYPE (val))))
5033 && live_on_edge (e, name2)
5034 && !has_single_use (name2))
5036 tree tmp, cst;
5037 enum tree_code new_comp_code = comp_code;
5039 cst = fold_convert (TREE_TYPE (name2),
5040 TYPE_MIN_VALUE (TREE_TYPE (val)));
5041 /* Build an expression for the range test. */
5042 tmp = build2 (PLUS_EXPR, TREE_TYPE (name2), name2, cst);
5043 cst = fold_build2 (PLUS_EXPR, TREE_TYPE (name2), cst,
5044 fold_convert (TREE_TYPE (name2), val));
5045 if (comp_code == LT_EXPR || comp_code == GE_EXPR)
5047 new_comp_code = comp_code == LT_EXPR ? LE_EXPR : GT_EXPR;
5048 cst = fold_build2 (MINUS_EXPR, TREE_TYPE (name2), cst,
5049 build_int_cst (TREE_TYPE (name2), 1));
5052 if (dump_file)
5054 fprintf (dump_file, "Adding assert for ");
5055 print_generic_expr (dump_file, name2, 0);
5056 fprintf (dump_file, " from ");
5057 print_generic_expr (dump_file, tmp, 0);
5058 fprintf (dump_file, "\n");
5061 register_new_assert_for (name2, tmp, new_comp_code, cst, NULL,
5062 e, bsi);
5064 retval = true;
5068 /* Add asserts for NAME cmp CST and NAME being defined as
5069 NAME = NAME2 >> CST2.
5071 Extract CST2 from the right shift. */
5072 if (rhs_code == RSHIFT_EXPR)
5074 name2 = gimple_assign_rhs1 (def_stmt);
5075 cst2 = gimple_assign_rhs2 (def_stmt);
5076 if (TREE_CODE (name2) == SSA_NAME
5077 && host_integerp (cst2, 1)
5078 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
5079 && IN_RANGE (tree_low_cst (cst2, 1), 1, prec - 1)
5080 && prec <= HOST_BITS_PER_DOUBLE_INT
5081 && prec == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (val)))
5082 && live_on_edge (e, name2)
5083 && !has_single_use (name2))
5085 mask = double_int::mask (tree_low_cst (cst2, 1));
5086 val2 = fold_binary (LSHIFT_EXPR, TREE_TYPE (val), val, cst2);
5089 if (val2 != NULL_TREE
5090 && TREE_CODE (val2) == INTEGER_CST
5091 && simple_cst_equal (fold_build2 (RSHIFT_EXPR,
5092 TREE_TYPE (val),
5093 val2, cst2), val))
5095 enum tree_code new_comp_code = comp_code;
5096 tree tmp, new_val;
5098 tmp = name2;
5099 if (comp_code == EQ_EXPR || comp_code == NE_EXPR)
5101 if (!TYPE_UNSIGNED (TREE_TYPE (val)))
5103 tree type = build_nonstandard_integer_type (prec, 1);
5104 tmp = build1 (NOP_EXPR, type, name2);
5105 val2 = fold_convert (type, val2);
5107 tmp = fold_build2 (MINUS_EXPR, TREE_TYPE (tmp), tmp, val2);
5108 new_val = double_int_to_tree (TREE_TYPE (tmp), mask);
5109 new_comp_code = comp_code == EQ_EXPR ? LE_EXPR : GT_EXPR;
5111 else if (comp_code == LT_EXPR || comp_code == GE_EXPR)
5113 double_int minval
5114 = double_int::min_value (prec, TYPE_UNSIGNED (TREE_TYPE (val)));
5115 new_val = val2;
5116 if (minval == tree_to_double_int (new_val))
5117 new_val = NULL_TREE;
5119 else
5121 double_int maxval
5122 = double_int::max_value (prec, TYPE_UNSIGNED (TREE_TYPE (val)));
5123 mask |= tree_to_double_int (val2);
5124 if (mask == maxval)
5125 new_val = NULL_TREE;
5126 else
5127 new_val = double_int_to_tree (TREE_TYPE (val2), mask);
5130 if (new_val)
5132 if (dump_file)
5134 fprintf (dump_file, "Adding assert for ");
5135 print_generic_expr (dump_file, name2, 0);
5136 fprintf (dump_file, " from ");
5137 print_generic_expr (dump_file, tmp, 0);
5138 fprintf (dump_file, "\n");
5141 register_new_assert_for (name2, tmp, new_comp_code, new_val,
5142 NULL, e, bsi);
5143 retval = true;
5147 /* Add asserts for NAME cmp CST and NAME being defined as
5148 NAME = NAME2 & CST2.
5150 Extract CST2 from the and.
5152 Also handle
5153 NAME = (unsigned) NAME2;
5154 casts where NAME's type is unsigned and has smaller precision
5155 than NAME2's type as if it was NAME = NAME2 & MASK. */
5156 names[0] = NULL_TREE;
5157 names[1] = NULL_TREE;
5158 cst2 = NULL_TREE;
5159 if (rhs_code == BIT_AND_EXPR
5160 || (CONVERT_EXPR_CODE_P (rhs_code)
5161 && TREE_CODE (TREE_TYPE (val)) == INTEGER_TYPE
5162 && TYPE_UNSIGNED (TREE_TYPE (val))
5163 && TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (def_stmt)))
5164 > prec
5165 && !retval))
5167 name2 = gimple_assign_rhs1 (def_stmt);
5168 if (rhs_code == BIT_AND_EXPR)
5169 cst2 = gimple_assign_rhs2 (def_stmt);
5170 else
5172 cst2 = TYPE_MAX_VALUE (TREE_TYPE (val));
5173 nprec = TYPE_PRECISION (TREE_TYPE (name2));
5175 if (TREE_CODE (name2) == SSA_NAME
5176 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
5177 && TREE_CODE (cst2) == INTEGER_CST
5178 && !integer_zerop (cst2)
5179 && nprec <= HOST_BITS_PER_DOUBLE_INT
5180 && (nprec > 1
5181 || TYPE_UNSIGNED (TREE_TYPE (val))))
5183 gimple def_stmt2 = SSA_NAME_DEF_STMT (name2);
5184 if (gimple_assign_cast_p (def_stmt2))
5186 names[1] = gimple_assign_rhs1 (def_stmt2);
5187 if (!CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt2))
5188 || !INTEGRAL_TYPE_P (TREE_TYPE (names[1]))
5189 || (TYPE_PRECISION (TREE_TYPE (name2))
5190 != TYPE_PRECISION (TREE_TYPE (names[1])))
5191 || !live_on_edge (e, names[1])
5192 || has_single_use (names[1]))
5193 names[1] = NULL_TREE;
5195 if (live_on_edge (e, name2)
5196 && !has_single_use (name2))
5197 names[0] = name2;
5200 if (names[0] || names[1])
5202 double_int minv, maxv = double_int_zero, valv, cst2v;
5203 double_int tem, sgnbit;
5204 bool valid_p = false, valn = false, cst2n = false;
5205 enum tree_code ccode = comp_code;
5207 valv = tree_to_double_int (val).zext (nprec);
5208 cst2v = tree_to_double_int (cst2).zext (nprec);
5209 if (!TYPE_UNSIGNED (TREE_TYPE (val)))
5211 valn = valv.sext (nprec).is_negative ();
5212 cst2n = cst2v.sext (nprec).is_negative ();
5214 /* If CST2 doesn't have most significant bit set,
5215 but VAL is negative, we have comparison like
5216 if ((x & 0x123) > -4) (always true). Just give up. */
5217 if (!cst2n && valn)
5218 ccode = ERROR_MARK;
5219 if (cst2n)
5220 sgnbit = double_int_one.llshift (nprec - 1, nprec).zext (nprec);
5221 else
5222 sgnbit = double_int_zero;
5223 minv = valv & cst2v;
5224 switch (ccode)
5226 case EQ_EXPR:
5227 /* Minimum unsigned value for equality is VAL & CST2
5228 (should be equal to VAL, otherwise we probably should
5229 have folded the comparison into false) and
5230 maximum unsigned value is VAL | ~CST2. */
5231 maxv = valv | ~cst2v;
5232 maxv = maxv.zext (nprec);
5233 valid_p = true;
5234 break;
5235 case NE_EXPR:
5236 tem = valv | ~cst2v;
5237 tem = tem.zext (nprec);
5238 /* If VAL is 0, handle (X & CST2) != 0 as (X & CST2) > 0U. */
5239 if (valv.is_zero ())
5241 cst2n = false;
5242 sgnbit = double_int_zero;
5243 goto gt_expr;
5245 /* If (VAL | ~CST2) is all ones, handle it as
5246 (X & CST2) < VAL. */
5247 if (tem == double_int::mask (nprec))
5249 cst2n = false;
5250 valn = false;
5251 sgnbit = double_int_zero;
5252 goto lt_expr;
5254 if (!cst2n
5255 && cst2v.sext (nprec).is_negative ())
5256 sgnbit
5257 = double_int_one.llshift (nprec - 1, nprec).zext (nprec);
5258 if (!sgnbit.is_zero ())
5260 if (valv == sgnbit)
5262 cst2n = true;
5263 valn = true;
5264 goto gt_expr;
5266 if (tem == double_int::mask (nprec - 1))
5268 cst2n = true;
5269 goto lt_expr;
5271 if (!cst2n)
5272 sgnbit = double_int_zero;
5274 break;
5275 case GE_EXPR:
5276 /* Minimum unsigned value for >= if (VAL & CST2) == VAL
5277 is VAL and maximum unsigned value is ~0. For signed
5278 comparison, if CST2 doesn't have most significant bit
5279 set, handle it similarly. If CST2 has MSB set,
5280 the minimum is the same, and maximum is ~0U/2. */
5281 if (minv != valv)
5283 /* If (VAL & CST2) != VAL, X & CST2 can't be equal to
5284 VAL. */
5285 minv = masked_increment (valv, cst2v, sgnbit, nprec);
5286 if (minv == valv)
5287 break;
5289 maxv = double_int::mask (nprec - (cst2n ? 1 : 0));
5290 valid_p = true;
5291 break;
5292 case GT_EXPR:
5293 gt_expr:
5294 /* Find out smallest MINV where MINV > VAL
5295 && (MINV & CST2) == MINV, if any. If VAL is signed and
5296 CST2 has MSB set, compute it biased by 1 << (nprec - 1). */
5297 minv = masked_increment (valv, cst2v, sgnbit, nprec);
5298 if (minv == valv)
5299 break;
5300 maxv = double_int::mask (nprec - (cst2n ? 1 : 0));
5301 valid_p = true;
5302 break;
5303 case LE_EXPR:
5304 /* Minimum unsigned value for <= is 0 and maximum
5305 unsigned value is VAL | ~CST2 if (VAL & CST2) == VAL.
5306 Otherwise, find smallest VAL2 where VAL2 > VAL
5307 && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
5308 as maximum.
5309 For signed comparison, if CST2 doesn't have most
5310 significant bit set, handle it similarly. If CST2 has
5311 MSB set, the maximum is the same and minimum is INT_MIN. */
5312 if (minv == valv)
5313 maxv = valv;
5314 else
5316 maxv = masked_increment (valv, cst2v, sgnbit, nprec);
5317 if (maxv == valv)
5318 break;
5319 maxv -= double_int_one;
5321 maxv |= ~cst2v;
5322 maxv = maxv.zext (nprec);
5323 minv = sgnbit;
5324 valid_p = true;
5325 break;
5326 case LT_EXPR:
5327 lt_expr:
5328 /* Minimum unsigned value for < is 0 and maximum
5329 unsigned value is (VAL-1) | ~CST2 if (VAL & CST2) == VAL.
5330 Otherwise, find smallest VAL2 where VAL2 > VAL
5331 && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
5332 as maximum.
5333 For signed comparison, if CST2 doesn't have most
5334 significant bit set, handle it similarly. If CST2 has
5335 MSB set, the maximum is the same and minimum is INT_MIN. */
5336 if (minv == valv)
5338 if (valv == sgnbit)
5339 break;
5340 maxv = valv;
5342 else
5344 maxv = masked_increment (valv, cst2v, sgnbit, nprec);
5345 if (maxv == valv)
5346 break;
5348 maxv -= double_int_one;
5349 maxv |= ~cst2v;
5350 maxv = maxv.zext (nprec);
5351 minv = sgnbit;
5352 valid_p = true;
5353 break;
5354 default:
5355 break;
5357 if (valid_p
5358 && (maxv - minv).zext (nprec) != double_int::mask (nprec))
5360 tree tmp, new_val, type;
5361 int i;
5363 for (i = 0; i < 2; i++)
5364 if (names[i])
5366 double_int maxv2 = maxv;
5367 tmp = names[i];
5368 type = TREE_TYPE (names[i]);
5369 if (!TYPE_UNSIGNED (type))
5371 type = build_nonstandard_integer_type (nprec, 1);
5372 tmp = build1 (NOP_EXPR, type, names[i]);
5374 if (!minv.is_zero ())
5376 tmp = build2 (PLUS_EXPR, type, tmp,
5377 double_int_to_tree (type, -minv));
5378 maxv2 = maxv - minv;
5380 new_val = double_int_to_tree (type, maxv2);
5382 if (dump_file)
5384 fprintf (dump_file, "Adding assert for ");
5385 print_generic_expr (dump_file, names[i], 0);
5386 fprintf (dump_file, " from ");
5387 print_generic_expr (dump_file, tmp, 0);
5388 fprintf (dump_file, "\n");
5391 register_new_assert_for (names[i], tmp, LE_EXPR,
5392 new_val, NULL, e, bsi);
5393 retval = true;
5399 return retval;
5402 /* OP is an operand of a truth value expression which is known to have
5403 a particular value. Register any asserts for OP and for any
5404 operands in OP's defining statement.
5406 If CODE is EQ_EXPR, then we want to register OP is zero (false),
5407 if CODE is NE_EXPR, then we want to register OP is nonzero (true). */
5409 static bool
5410 register_edge_assert_for_1 (tree op, enum tree_code code,
5411 edge e, gimple_stmt_iterator bsi)
5413 bool retval = false;
5414 gimple op_def;
5415 tree val;
5416 enum tree_code rhs_code;
5418 /* We only care about SSA_NAMEs. */
5419 if (TREE_CODE (op) != SSA_NAME)
5420 return false;
5422 /* We know that OP will have a zero or nonzero value. If OP is used
5423 more than once go ahead and register an assert for OP.
5425 The FOUND_IN_SUBGRAPH support is not helpful in this situation as
5426 it will always be set for OP (because OP is used in a COND_EXPR in
5427 the subgraph). */
5428 if (!has_single_use (op))
5430 val = build_int_cst (TREE_TYPE (op), 0);
5431 register_new_assert_for (op, op, code, val, NULL, e, bsi);
5432 retval = true;
5435 /* Now look at how OP is set. If it's set from a comparison,
5436 a truth operation or some bit operations, then we may be able
5437 to register information about the operands of that assignment. */
5438 op_def = SSA_NAME_DEF_STMT (op);
5439 if (gimple_code (op_def) != GIMPLE_ASSIGN)
5440 return retval;
5442 rhs_code = gimple_assign_rhs_code (op_def);
5444 if (TREE_CODE_CLASS (rhs_code) == tcc_comparison)
5446 bool invert = (code == EQ_EXPR ? true : false);
5447 tree op0 = gimple_assign_rhs1 (op_def);
5448 tree op1 = gimple_assign_rhs2 (op_def);
5450 if (TREE_CODE (op0) == SSA_NAME)
5451 retval |= register_edge_assert_for_2 (op0, e, bsi, rhs_code, op0, op1,
5452 invert);
5453 if (TREE_CODE (op1) == SSA_NAME)
5454 retval |= register_edge_assert_for_2 (op1, e, bsi, rhs_code, op0, op1,
5455 invert);
5457 else if ((code == NE_EXPR
5458 && gimple_assign_rhs_code (op_def) == BIT_AND_EXPR)
5459 || (code == EQ_EXPR
5460 && gimple_assign_rhs_code (op_def) == BIT_IOR_EXPR))
5462 /* Recurse on each operand. */
5463 tree op0 = gimple_assign_rhs1 (op_def);
5464 tree op1 = gimple_assign_rhs2 (op_def);
5465 if (TREE_CODE (op0) == SSA_NAME
5466 && has_single_use (op0))
5467 retval |= register_edge_assert_for_1 (op0, code, e, bsi);
5468 if (TREE_CODE (op1) == SSA_NAME
5469 && has_single_use (op1))
5470 retval |= register_edge_assert_for_1 (op1, code, e, bsi);
5472 else if (gimple_assign_rhs_code (op_def) == BIT_NOT_EXPR
5473 && TYPE_PRECISION (TREE_TYPE (gimple_assign_lhs (op_def))) == 1)
5475 /* Recurse, flipping CODE. */
5476 code = invert_tree_comparison (code, false);
5477 retval |= register_edge_assert_for_1 (gimple_assign_rhs1 (op_def),
5478 code, e, bsi);
5480 else if (gimple_assign_rhs_code (op_def) == SSA_NAME)
5482 /* Recurse through the copy. */
5483 retval |= register_edge_assert_for_1 (gimple_assign_rhs1 (op_def),
5484 code, e, bsi);
5486 else if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (op_def)))
5488 /* Recurse through the type conversion. */
5489 retval |= register_edge_assert_for_1 (gimple_assign_rhs1 (op_def),
5490 code, e, bsi);
5493 return retval;
5496 /* Try to register an edge assertion for SSA name NAME on edge E for
5497 the condition COND contributing to the conditional jump pointed to by SI.
5498 Return true if an assertion for NAME could be registered. */
5500 static bool
5501 register_edge_assert_for (tree name, edge e, gimple_stmt_iterator si,
5502 enum tree_code cond_code, tree cond_op0,
5503 tree cond_op1)
5505 tree val;
5506 enum tree_code comp_code;
5507 bool retval = false;
5508 bool is_else_edge = (e->flags & EDGE_FALSE_VALUE) != 0;
5510 /* Do not attempt to infer anything in names that flow through
5511 abnormal edges. */
5512 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name))
5513 return false;
5515 if (!extract_code_and_val_from_cond_with_ops (name, cond_code,
5516 cond_op0, cond_op1,
5517 is_else_edge,
5518 &comp_code, &val))
5519 return false;
5521 /* Register ASSERT_EXPRs for name. */
5522 retval |= register_edge_assert_for_2 (name, e, si, cond_code, cond_op0,
5523 cond_op1, is_else_edge);
5526 /* If COND is effectively an equality test of an SSA_NAME against
5527 the value zero or one, then we may be able to assert values
5528 for SSA_NAMEs which flow into COND. */
5530 /* In the case of NAME == 1 or NAME != 0, for BIT_AND_EXPR defining
5531 statement of NAME we can assert both operands of the BIT_AND_EXPR
5532 have nonzero value. */
5533 if (((comp_code == EQ_EXPR && integer_onep (val))
5534 || (comp_code == NE_EXPR && integer_zerop (val))))
5536 gimple def_stmt = SSA_NAME_DEF_STMT (name);
5538 if (is_gimple_assign (def_stmt)
5539 && gimple_assign_rhs_code (def_stmt) == BIT_AND_EXPR)
5541 tree op0 = gimple_assign_rhs1 (def_stmt);
5542 tree op1 = gimple_assign_rhs2 (def_stmt);
5543 retval |= register_edge_assert_for_1 (op0, NE_EXPR, e, si);
5544 retval |= register_edge_assert_for_1 (op1, NE_EXPR, e, si);
5548 /* In the case of NAME == 0 or NAME != 1, for BIT_IOR_EXPR defining
5549 statement of NAME we can assert both operands of the BIT_IOR_EXPR
5550 have zero value. */
5551 if (((comp_code == EQ_EXPR && integer_zerop (val))
5552 || (comp_code == NE_EXPR && integer_onep (val))))
5554 gimple def_stmt = SSA_NAME_DEF_STMT (name);
5556 /* For BIT_IOR_EXPR only if NAME == 0 both operands have
5557 necessarily zero value, or if type-precision is one. */
5558 if (is_gimple_assign (def_stmt)
5559 && (gimple_assign_rhs_code (def_stmt) == BIT_IOR_EXPR
5560 && (TYPE_PRECISION (TREE_TYPE (name)) == 1
5561 || comp_code == EQ_EXPR)))
5563 tree op0 = gimple_assign_rhs1 (def_stmt);
5564 tree op1 = gimple_assign_rhs2 (def_stmt);
5565 retval |= register_edge_assert_for_1 (op0, EQ_EXPR, e, si);
5566 retval |= register_edge_assert_for_1 (op1, EQ_EXPR, e, si);
5570 return retval;
5574 /* Determine whether the outgoing edges of BB should receive an
5575 ASSERT_EXPR for each of the operands of BB's LAST statement.
5576 The last statement of BB must be a COND_EXPR.
5578 If any of the sub-graphs rooted at BB have an interesting use of
5579 the predicate operands, an assert location node is added to the
5580 list of assertions for the corresponding operands. */
5582 static bool
5583 find_conditional_asserts (basic_block bb, gimple last)
5585 bool need_assert;
5586 gimple_stmt_iterator bsi;
5587 tree op;
5588 edge_iterator ei;
5589 edge e;
5590 ssa_op_iter iter;
5592 need_assert = false;
5593 bsi = gsi_for_stmt (last);
5595 /* Look for uses of the operands in each of the sub-graphs
5596 rooted at BB. We need to check each of the outgoing edges
5597 separately, so that we know what kind of ASSERT_EXPR to
5598 insert. */
5599 FOR_EACH_EDGE (e, ei, bb->succs)
5601 if (e->dest == bb)
5602 continue;
5604 /* Register the necessary assertions for each operand in the
5605 conditional predicate. */
5606 FOR_EACH_SSA_TREE_OPERAND (op, last, iter, SSA_OP_USE)
5608 need_assert |= register_edge_assert_for (op, e, bsi,
5609 gimple_cond_code (last),
5610 gimple_cond_lhs (last),
5611 gimple_cond_rhs (last));
5615 return need_assert;
5618 struct case_info
5620 tree expr;
5621 basic_block bb;
5624 /* Compare two case labels sorting first by the destination bb index
5625 and then by the case value. */
5627 static int
5628 compare_case_labels (const void *p1, const void *p2)
5630 const struct case_info *ci1 = (const struct case_info *) p1;
5631 const struct case_info *ci2 = (const struct case_info *) p2;
5632 int idx1 = ci1->bb->index;
5633 int idx2 = ci2->bb->index;
5635 if (idx1 < idx2)
5636 return -1;
5637 else if (idx1 == idx2)
5639 /* Make sure the default label is first in a group. */
5640 if (!CASE_LOW (ci1->expr))
5641 return -1;
5642 else if (!CASE_LOW (ci2->expr))
5643 return 1;
5644 else
5645 return tree_int_cst_compare (CASE_LOW (ci1->expr),
5646 CASE_LOW (ci2->expr));
5648 else
5649 return 1;
5652 /* Determine whether the outgoing edges of BB should receive an
5653 ASSERT_EXPR for each of the operands of BB's LAST statement.
5654 The last statement of BB must be a SWITCH_EXPR.
5656 If any of the sub-graphs rooted at BB have an interesting use of
5657 the predicate operands, an assert location node is added to the
5658 list of assertions for the corresponding operands. */
5660 static bool
5661 find_switch_asserts (basic_block bb, gimple last)
5663 bool need_assert;
5664 gimple_stmt_iterator bsi;
5665 tree op;
5666 edge e;
5667 struct case_info *ci;
5668 size_t n = gimple_switch_num_labels (last);
5669 #if GCC_VERSION >= 4000
5670 unsigned int idx;
5671 #else
5672 /* Work around GCC 3.4 bug (PR 37086). */
5673 volatile unsigned int idx;
5674 #endif
5676 need_assert = false;
5677 bsi = gsi_for_stmt (last);
5678 op = gimple_switch_index (last);
5679 if (TREE_CODE (op) != SSA_NAME)
5680 return false;
5682 /* Build a vector of case labels sorted by destination label. */
5683 ci = XNEWVEC (struct case_info, n);
5684 for (idx = 0; idx < n; ++idx)
5686 ci[idx].expr = gimple_switch_label (last, idx);
5687 ci[idx].bb = label_to_block (CASE_LABEL (ci[idx].expr));
5689 qsort (ci, n, sizeof (struct case_info), compare_case_labels);
5691 for (idx = 0; idx < n; ++idx)
5693 tree min, max;
5694 tree cl = ci[idx].expr;
5695 basic_block cbb = ci[idx].bb;
5697 min = CASE_LOW (cl);
5698 max = CASE_HIGH (cl);
5700 /* If there are multiple case labels with the same destination
5701 we need to combine them to a single value range for the edge. */
5702 if (idx + 1 < n && cbb == ci[idx + 1].bb)
5704 /* Skip labels until the last of the group. */
5705 do {
5706 ++idx;
5707 } while (idx < n && cbb == ci[idx].bb);
5708 --idx;
5710 /* Pick up the maximum of the case label range. */
5711 if (CASE_HIGH (ci[idx].expr))
5712 max = CASE_HIGH (ci[idx].expr);
5713 else
5714 max = CASE_LOW (ci[idx].expr);
5717 /* Nothing to do if the range includes the default label until we
5718 can register anti-ranges. */
5719 if (min == NULL_TREE)
5720 continue;
5722 /* Find the edge to register the assert expr on. */
5723 e = find_edge (bb, cbb);
5725 /* Register the necessary assertions for the operand in the
5726 SWITCH_EXPR. */
5727 need_assert |= register_edge_assert_for (op, e, bsi,
5728 max ? GE_EXPR : EQ_EXPR,
5730 fold_convert (TREE_TYPE (op),
5731 min));
5732 if (max)
5734 need_assert |= register_edge_assert_for (op, e, bsi, LE_EXPR,
5736 fold_convert (TREE_TYPE (op),
5737 max));
5741 XDELETEVEC (ci);
5742 return need_assert;
5746 /* Traverse all the statements in block BB looking for statements that
5747 may generate useful assertions for the SSA names in their operand.
5748 If a statement produces a useful assertion A for name N_i, then the
5749 list of assertions already generated for N_i is scanned to
5750 determine if A is actually needed.
5752 If N_i already had the assertion A at a location dominating the
5753 current location, then nothing needs to be done. Otherwise, the
5754 new location for A is recorded instead.
5756 1- For every statement S in BB, all the variables used by S are
5757 added to bitmap FOUND_IN_SUBGRAPH.
5759 2- If statement S uses an operand N in a way that exposes a known
5760 value range for N, then if N was not already generated by an
5761 ASSERT_EXPR, create a new assert location for N. For instance,
5762 if N is a pointer and the statement dereferences it, we can
5763 assume that N is not NULL.
5765 3- COND_EXPRs are a special case of #2. We can derive range
5766 information from the predicate but need to insert different
5767 ASSERT_EXPRs for each of the sub-graphs rooted at the
5768 conditional block. If the last statement of BB is a conditional
5769 expression of the form 'X op Y', then
5771 a) Remove X and Y from the set FOUND_IN_SUBGRAPH.
5773 b) If the conditional is the only entry point to the sub-graph
5774 corresponding to the THEN_CLAUSE, recurse into it. On
5775 return, if X and/or Y are marked in FOUND_IN_SUBGRAPH, then
5776 an ASSERT_EXPR is added for the corresponding variable.
5778 c) Repeat step (b) on the ELSE_CLAUSE.
5780 d) Mark X and Y in FOUND_IN_SUBGRAPH.
5782 For instance,
5784 if (a == 9)
5785 b = a;
5786 else
5787 b = c + 1;
5789 In this case, an assertion on the THEN clause is useful to
5790 determine that 'a' is always 9 on that edge. However, an assertion
5791 on the ELSE clause would be unnecessary.
5793 4- If BB does not end in a conditional expression, then we recurse
5794 into BB's dominator children.
5796 At the end of the recursive traversal, every SSA name will have a
5797 list of locations where ASSERT_EXPRs should be added. When a new
5798 location for name N is found, it is registered by calling
5799 register_new_assert_for. That function keeps track of all the
5800 registered assertions to prevent adding unnecessary assertions.
5801 For instance, if a pointer P_4 is dereferenced more than once in a
5802 dominator tree, only the location dominating all the dereference of
5803 P_4 will receive an ASSERT_EXPR.
5805 If this function returns true, then it means that there are names
5806 for which we need to generate ASSERT_EXPRs. Those assertions are
5807 inserted by process_assert_insertions. */
5809 static bool
5810 find_assert_locations_1 (basic_block bb, sbitmap live)
5812 gimple_stmt_iterator si;
5813 gimple last;
5814 bool need_assert;
5816 need_assert = false;
5817 last = last_stmt (bb);
5819 /* If BB's last statement is a conditional statement involving integer
5820 operands, determine if we need to add ASSERT_EXPRs. */
5821 if (last
5822 && gimple_code (last) == GIMPLE_COND
5823 && !fp_predicate (last)
5824 && !ZERO_SSA_OPERANDS (last, SSA_OP_USE))
5825 need_assert |= find_conditional_asserts (bb, last);
5827 /* If BB's last statement is a switch statement involving integer
5828 operands, determine if we need to add ASSERT_EXPRs. */
5829 if (last
5830 && gimple_code (last) == GIMPLE_SWITCH
5831 && !ZERO_SSA_OPERANDS (last, SSA_OP_USE))
5832 need_assert |= find_switch_asserts (bb, last);
5834 /* Traverse all the statements in BB marking used names and looking
5835 for statements that may infer assertions for their used operands. */
5836 for (si = gsi_last_bb (bb); !gsi_end_p (si); gsi_prev (&si))
5838 gimple stmt;
5839 tree op;
5840 ssa_op_iter i;
5842 stmt = gsi_stmt (si);
5844 if (is_gimple_debug (stmt))
5845 continue;
5847 /* See if we can derive an assertion for any of STMT's operands. */
5848 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_USE)
5850 tree value;
5851 enum tree_code comp_code;
5853 /* If op is not live beyond this stmt, do not bother to insert
5854 asserts for it. */
5855 if (!bitmap_bit_p (live, SSA_NAME_VERSION (op)))
5856 continue;
5858 /* If OP is used in such a way that we can infer a value
5859 range for it, and we don't find a previous assertion for
5860 it, create a new assertion location node for OP. */
5861 if (infer_value_range (stmt, op, &comp_code, &value))
5863 /* If we are able to infer a nonzero value range for OP,
5864 then walk backwards through the use-def chain to see if OP
5865 was set via a typecast.
5867 If so, then we can also infer a nonzero value range
5868 for the operand of the NOP_EXPR. */
5869 if (comp_code == NE_EXPR && integer_zerop (value))
5871 tree t = op;
5872 gimple def_stmt = SSA_NAME_DEF_STMT (t);
5874 while (is_gimple_assign (def_stmt)
5875 && gimple_assign_rhs_code (def_stmt) == NOP_EXPR
5876 && TREE_CODE
5877 (gimple_assign_rhs1 (def_stmt)) == SSA_NAME
5878 && POINTER_TYPE_P
5879 (TREE_TYPE (gimple_assign_rhs1 (def_stmt))))
5881 t = gimple_assign_rhs1 (def_stmt);
5882 def_stmt = SSA_NAME_DEF_STMT (t);
5884 /* Note we want to register the assert for the
5885 operand of the NOP_EXPR after SI, not after the
5886 conversion. */
5887 if (! has_single_use (t))
5889 register_new_assert_for (t, t, comp_code, value,
5890 bb, NULL, si);
5891 need_assert = true;
5896 register_new_assert_for (op, op, comp_code, value, bb, NULL, si);
5897 need_assert = true;
5901 /* Update live. */
5902 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_USE)
5903 bitmap_set_bit (live, SSA_NAME_VERSION (op));
5904 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_DEF)
5905 bitmap_clear_bit (live, SSA_NAME_VERSION (op));
5908 /* Traverse all PHI nodes in BB, updating live. */
5909 for (si = gsi_start_phis (bb); !gsi_end_p (si); gsi_next (&si))
5911 use_operand_p arg_p;
5912 ssa_op_iter i;
5913 gimple phi = gsi_stmt (si);
5914 tree res = gimple_phi_result (phi);
5916 if (virtual_operand_p (res))
5917 continue;
5919 FOR_EACH_PHI_ARG (arg_p, phi, i, SSA_OP_USE)
5921 tree arg = USE_FROM_PTR (arg_p);
5922 if (TREE_CODE (arg) == SSA_NAME)
5923 bitmap_set_bit (live, SSA_NAME_VERSION (arg));
5926 bitmap_clear_bit (live, SSA_NAME_VERSION (res));
5929 return need_assert;
5932 /* Do an RPO walk over the function computing SSA name liveness
5933 on-the-fly and deciding on assert expressions to insert.
5934 Returns true if there are assert expressions to be inserted. */
5936 static bool
5937 find_assert_locations (void)
5939 int *rpo = XNEWVEC (int, last_basic_block);
5940 int *bb_rpo = XNEWVEC (int, last_basic_block);
5941 int *last_rpo = XCNEWVEC (int, last_basic_block);
5942 int rpo_cnt, i;
5943 bool need_asserts;
5945 live = XCNEWVEC (sbitmap, last_basic_block);
5946 rpo_cnt = pre_and_rev_post_order_compute (NULL, rpo, false);
5947 for (i = 0; i < rpo_cnt; ++i)
5948 bb_rpo[rpo[i]] = i;
5950 need_asserts = false;
5951 for (i = rpo_cnt - 1; i >= 0; --i)
5953 basic_block bb = BASIC_BLOCK (rpo[i]);
5954 edge e;
5955 edge_iterator ei;
5957 if (!live[rpo[i]])
5959 live[rpo[i]] = sbitmap_alloc (num_ssa_names);
5960 bitmap_clear (live[rpo[i]]);
5963 /* Process BB and update the live information with uses in
5964 this block. */
5965 need_asserts |= find_assert_locations_1 (bb, live[rpo[i]]);
5967 /* Merge liveness into the predecessor blocks and free it. */
5968 if (!bitmap_empty_p (live[rpo[i]]))
5970 int pred_rpo = i;
5971 FOR_EACH_EDGE (e, ei, bb->preds)
5973 int pred = e->src->index;
5974 if ((e->flags & EDGE_DFS_BACK) || pred == ENTRY_BLOCK)
5975 continue;
5977 if (!live[pred])
5979 live[pred] = sbitmap_alloc (num_ssa_names);
5980 bitmap_clear (live[pred]);
5982 bitmap_ior (live[pred], live[pred], live[rpo[i]]);
5984 if (bb_rpo[pred] < pred_rpo)
5985 pred_rpo = bb_rpo[pred];
5988 /* Record the RPO number of the last visited block that needs
5989 live information from this block. */
5990 last_rpo[rpo[i]] = pred_rpo;
5992 else
5994 sbitmap_free (live[rpo[i]]);
5995 live[rpo[i]] = NULL;
5998 /* We can free all successors live bitmaps if all their
5999 predecessors have been visited already. */
6000 FOR_EACH_EDGE (e, ei, bb->succs)
6001 if (last_rpo[e->dest->index] == i
6002 && live[e->dest->index])
6004 sbitmap_free (live[e->dest->index]);
6005 live[e->dest->index] = NULL;
6009 XDELETEVEC (rpo);
6010 XDELETEVEC (bb_rpo);
6011 XDELETEVEC (last_rpo);
6012 for (i = 0; i < last_basic_block; ++i)
6013 if (live[i])
6014 sbitmap_free (live[i]);
6015 XDELETEVEC (live);
6017 return need_asserts;
6020 /* Create an ASSERT_EXPR for NAME and insert it in the location
6021 indicated by LOC. Return true if we made any edge insertions. */
6023 static bool
6024 process_assert_insertions_for (tree name, assert_locus_t loc)
6026 /* Build the comparison expression NAME_i COMP_CODE VAL. */
6027 gimple stmt;
6028 tree cond;
6029 gimple assert_stmt;
6030 edge_iterator ei;
6031 edge e;
6033 /* If we have X <=> X do not insert an assert expr for that. */
6034 if (loc->expr == loc->val)
6035 return false;
6037 cond = build2 (loc->comp_code, boolean_type_node, loc->expr, loc->val);
6038 assert_stmt = build_assert_expr_for (cond, name);
6039 if (loc->e)
6041 /* We have been asked to insert the assertion on an edge. This
6042 is used only by COND_EXPR and SWITCH_EXPR assertions. */
6043 gcc_checking_assert (gimple_code (gsi_stmt (loc->si)) == GIMPLE_COND
6044 || (gimple_code (gsi_stmt (loc->si))
6045 == GIMPLE_SWITCH));
6047 gsi_insert_on_edge (loc->e, assert_stmt);
6048 return true;
6051 /* Otherwise, we can insert right after LOC->SI iff the
6052 statement must not be the last statement in the block. */
6053 stmt = gsi_stmt (loc->si);
6054 if (!stmt_ends_bb_p (stmt))
6056 gsi_insert_after (&loc->si, assert_stmt, GSI_SAME_STMT);
6057 return false;
6060 /* If STMT must be the last statement in BB, we can only insert new
6061 assertions on the non-abnormal edge out of BB. Note that since
6062 STMT is not control flow, there may only be one non-abnormal edge
6063 out of BB. */
6064 FOR_EACH_EDGE (e, ei, loc->bb->succs)
6065 if (!(e->flags & EDGE_ABNORMAL))
6067 gsi_insert_on_edge (e, assert_stmt);
6068 return true;
6071 gcc_unreachable ();
6075 /* Process all the insertions registered for every name N_i registered
6076 in NEED_ASSERT_FOR. The list of assertions to be inserted are
6077 found in ASSERTS_FOR[i]. */
6079 static void
6080 process_assert_insertions (void)
6082 unsigned i;
6083 bitmap_iterator bi;
6084 bool update_edges_p = false;
6085 int num_asserts = 0;
6087 if (dump_file && (dump_flags & TDF_DETAILS))
6088 dump_all_asserts (dump_file);
6090 EXECUTE_IF_SET_IN_BITMAP (need_assert_for, 0, i, bi)
6092 assert_locus_t loc = asserts_for[i];
6093 gcc_assert (loc);
6095 while (loc)
6097 assert_locus_t next = loc->next;
6098 update_edges_p |= process_assert_insertions_for (ssa_name (i), loc);
6099 free (loc);
6100 loc = next;
6101 num_asserts++;
6105 if (update_edges_p)
6106 gsi_commit_edge_inserts ();
6108 statistics_counter_event (cfun, "Number of ASSERT_EXPR expressions inserted",
6109 num_asserts);
6113 /* Traverse the flowgraph looking for conditional jumps to insert range
6114 expressions. These range expressions are meant to provide information
6115 to optimizations that need to reason in terms of value ranges. They
6116 will not be expanded into RTL. For instance, given:
6118 x = ...
6119 y = ...
6120 if (x < y)
6121 y = x - 2;
6122 else
6123 x = y + 3;
6125 this pass will transform the code into:
6127 x = ...
6128 y = ...
6129 if (x < y)
6131 x = ASSERT_EXPR <x, x < y>
6132 y = x - 2
6134 else
6136 y = ASSERT_EXPR <y, x <= y>
6137 x = y + 3
6140 The idea is that once copy and constant propagation have run, other
6141 optimizations will be able to determine what ranges of values can 'x'
6142 take in different paths of the code, simply by checking the reaching
6143 definition of 'x'. */
6145 static void
6146 insert_range_assertions (void)
6148 need_assert_for = BITMAP_ALLOC (NULL);
6149 asserts_for = XCNEWVEC (assert_locus_t, num_ssa_names);
6151 calculate_dominance_info (CDI_DOMINATORS);
6153 if (find_assert_locations ())
6155 process_assert_insertions ();
6156 update_ssa (TODO_update_ssa_no_phi);
6159 if (dump_file && (dump_flags & TDF_DETAILS))
6161 fprintf (dump_file, "\nSSA form after inserting ASSERT_EXPRs\n");
6162 dump_function_to_file (current_function_decl, dump_file, dump_flags);
6165 free (asserts_for);
6166 BITMAP_FREE (need_assert_for);
6169 /* Checks one ARRAY_REF in REF, located at LOCUS. Ignores flexible arrays
6170 and "struct" hacks. If VRP can determine that the
6171 array subscript is a constant, check if it is outside valid
6172 range. If the array subscript is a RANGE, warn if it is
6173 non-overlapping with valid range.
6174 IGNORE_OFF_BY_ONE is true if the ARRAY_REF is inside a ADDR_EXPR. */
6176 static void
6177 check_array_ref (location_t location, tree ref, bool ignore_off_by_one)
6179 value_range_t* vr = NULL;
6180 tree low_sub, up_sub;
6181 tree low_bound, up_bound, up_bound_p1;
6182 tree base;
6184 if (TREE_NO_WARNING (ref))
6185 return;
6187 low_sub = up_sub = TREE_OPERAND (ref, 1);
6188 up_bound = array_ref_up_bound (ref);
6190 /* Can not check flexible arrays. */
6191 if (!up_bound
6192 || TREE_CODE (up_bound) != INTEGER_CST)
6193 return;
6195 /* Accesses to trailing arrays via pointers may access storage
6196 beyond the types array bounds. */
6197 base = get_base_address (ref);
6198 if (base && TREE_CODE (base) == MEM_REF)
6200 tree cref, next = NULL_TREE;
6202 if (TREE_CODE (TREE_OPERAND (ref, 0)) != COMPONENT_REF)
6203 return;
6205 cref = TREE_OPERAND (ref, 0);
6206 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (cref, 0))) == RECORD_TYPE)
6207 for (next = DECL_CHAIN (TREE_OPERAND (cref, 1));
6208 next && TREE_CODE (next) != FIELD_DECL;
6209 next = DECL_CHAIN (next))
6212 /* If this is the last field in a struct type or a field in a
6213 union type do not warn. */
6214 if (!next)
6215 return;
6218 low_bound = array_ref_low_bound (ref);
6219 up_bound_p1 = int_const_binop (PLUS_EXPR, up_bound, integer_one_node);
6221 if (TREE_CODE (low_sub) == SSA_NAME)
6223 vr = get_value_range (low_sub);
6224 if (vr->type == VR_RANGE || vr->type == VR_ANTI_RANGE)
6226 low_sub = vr->type == VR_RANGE ? vr->max : vr->min;
6227 up_sub = vr->type == VR_RANGE ? vr->min : vr->max;
6231 if (vr && vr->type == VR_ANTI_RANGE)
6233 if (TREE_CODE (up_sub) == INTEGER_CST
6234 && tree_int_cst_lt (up_bound, up_sub)
6235 && TREE_CODE (low_sub) == INTEGER_CST
6236 && tree_int_cst_lt (low_sub, low_bound))
6238 warning_at (location, OPT_Warray_bounds,
6239 "array subscript is outside array bounds");
6240 TREE_NO_WARNING (ref) = 1;
6243 else if (TREE_CODE (up_sub) == INTEGER_CST
6244 && (ignore_off_by_one
6245 ? (tree_int_cst_lt (up_bound, up_sub)
6246 && !tree_int_cst_equal (up_bound_p1, up_sub))
6247 : (tree_int_cst_lt (up_bound, up_sub)
6248 || tree_int_cst_equal (up_bound_p1, up_sub))))
6250 if (dump_file && (dump_flags & TDF_DETAILS))
6252 fprintf (dump_file, "Array bound warning for ");
6253 dump_generic_expr (MSG_NOTE, TDF_SLIM, ref);
6254 fprintf (dump_file, "\n");
6256 warning_at (location, OPT_Warray_bounds,
6257 "array subscript is above array bounds");
6258 TREE_NO_WARNING (ref) = 1;
6260 else if (TREE_CODE (low_sub) == INTEGER_CST
6261 && tree_int_cst_lt (low_sub, low_bound))
6263 if (dump_file && (dump_flags & TDF_DETAILS))
6265 fprintf (dump_file, "Array bound warning for ");
6266 dump_generic_expr (MSG_NOTE, TDF_SLIM, ref);
6267 fprintf (dump_file, "\n");
6269 warning_at (location, OPT_Warray_bounds,
6270 "array subscript is below array bounds");
6271 TREE_NO_WARNING (ref) = 1;
6275 /* Searches if the expr T, located at LOCATION computes
6276 address of an ARRAY_REF, and call check_array_ref on it. */
6278 static void
6279 search_for_addr_array (tree t, location_t location)
6281 while (TREE_CODE (t) == SSA_NAME)
6283 gimple g = SSA_NAME_DEF_STMT (t);
6285 if (gimple_code (g) != GIMPLE_ASSIGN)
6286 return;
6288 if (get_gimple_rhs_class (gimple_assign_rhs_code (g))
6289 != GIMPLE_SINGLE_RHS)
6290 return;
6292 t = gimple_assign_rhs1 (g);
6296 /* We are only interested in addresses of ARRAY_REF's. */
6297 if (TREE_CODE (t) != ADDR_EXPR)
6298 return;
6300 /* Check each ARRAY_REFs in the reference chain. */
6303 if (TREE_CODE (t) == ARRAY_REF)
6304 check_array_ref (location, t, true /*ignore_off_by_one*/);
6306 t = TREE_OPERAND (t, 0);
6308 while (handled_component_p (t));
6310 if (TREE_CODE (t) == MEM_REF
6311 && TREE_CODE (TREE_OPERAND (t, 0)) == ADDR_EXPR
6312 && !TREE_NO_WARNING (t))
6314 tree tem = TREE_OPERAND (TREE_OPERAND (t, 0), 0);
6315 tree low_bound, up_bound, el_sz;
6316 double_int idx;
6317 if (TREE_CODE (TREE_TYPE (tem)) != ARRAY_TYPE
6318 || TREE_CODE (TREE_TYPE (TREE_TYPE (tem))) == ARRAY_TYPE
6319 || !TYPE_DOMAIN (TREE_TYPE (tem)))
6320 return;
6322 low_bound = TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (tem)));
6323 up_bound = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (tem)));
6324 el_sz = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (tem)));
6325 if (!low_bound
6326 || TREE_CODE (low_bound) != INTEGER_CST
6327 || !up_bound
6328 || TREE_CODE (up_bound) != INTEGER_CST
6329 || !el_sz
6330 || TREE_CODE (el_sz) != INTEGER_CST)
6331 return;
6333 idx = mem_ref_offset (t);
6334 idx = idx.sdiv (tree_to_double_int (el_sz), TRUNC_DIV_EXPR);
6335 if (idx.slt (double_int_zero))
6337 if (dump_file && (dump_flags & TDF_DETAILS))
6339 fprintf (dump_file, "Array bound warning for ");
6340 dump_generic_expr (MSG_NOTE, TDF_SLIM, t);
6341 fprintf (dump_file, "\n");
6343 warning_at (location, OPT_Warray_bounds,
6344 "array subscript is below array bounds");
6345 TREE_NO_WARNING (t) = 1;
6347 else if (idx.sgt (tree_to_double_int (up_bound)
6348 - tree_to_double_int (low_bound)
6349 + double_int_one))
6351 if (dump_file && (dump_flags & TDF_DETAILS))
6353 fprintf (dump_file, "Array bound warning for ");
6354 dump_generic_expr (MSG_NOTE, TDF_SLIM, t);
6355 fprintf (dump_file, "\n");
6357 warning_at (location, OPT_Warray_bounds,
6358 "array subscript is above array bounds");
6359 TREE_NO_WARNING (t) = 1;
6364 /* walk_tree() callback that checks if *TP is
6365 an ARRAY_REF inside an ADDR_EXPR (in which an array
6366 subscript one outside the valid range is allowed). Call
6367 check_array_ref for each ARRAY_REF found. The location is
6368 passed in DATA. */
6370 static tree
6371 check_array_bounds (tree *tp, int *walk_subtree, void *data)
6373 tree t = *tp;
6374 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
6375 location_t location;
6377 if (EXPR_HAS_LOCATION (t))
6378 location = EXPR_LOCATION (t);
6379 else
6381 location_t *locp = (location_t *) wi->info;
6382 location = *locp;
6385 *walk_subtree = TRUE;
6387 if (TREE_CODE (t) == ARRAY_REF)
6388 check_array_ref (location, t, false /*ignore_off_by_one*/);
6390 if (TREE_CODE (t) == MEM_REF
6391 || (TREE_CODE (t) == RETURN_EXPR && TREE_OPERAND (t, 0)))
6392 search_for_addr_array (TREE_OPERAND (t, 0), location);
6394 if (TREE_CODE (t) == ADDR_EXPR)
6395 *walk_subtree = FALSE;
6397 return NULL_TREE;
6400 /* Walk over all statements of all reachable BBs and call check_array_bounds
6401 on them. */
6403 static void
6404 check_all_array_refs (void)
6406 basic_block bb;
6407 gimple_stmt_iterator si;
6409 FOR_EACH_BB (bb)
6411 edge_iterator ei;
6412 edge e;
6413 bool executable = false;
6415 /* Skip blocks that were found to be unreachable. */
6416 FOR_EACH_EDGE (e, ei, bb->preds)
6417 executable |= !!(e->flags & EDGE_EXECUTABLE);
6418 if (!executable)
6419 continue;
6421 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
6423 gimple stmt = gsi_stmt (si);
6424 struct walk_stmt_info wi;
6425 if (!gimple_has_location (stmt))
6426 continue;
6428 if (is_gimple_call (stmt))
6430 size_t i;
6431 size_t n = gimple_call_num_args (stmt);
6432 for (i = 0; i < n; i++)
6434 tree arg = gimple_call_arg (stmt, i);
6435 search_for_addr_array (arg, gimple_location (stmt));
6438 else
6440 memset (&wi, 0, sizeof (wi));
6441 wi.info = CONST_CAST (void *, (const void *)
6442 gimple_location_ptr (stmt));
6444 walk_gimple_op (gsi_stmt (si),
6445 check_array_bounds,
6446 &wi);
6452 /* Convert range assertion expressions into the implied copies and
6453 copy propagate away the copies. Doing the trivial copy propagation
6454 here avoids the need to run the full copy propagation pass after
6455 VRP.
6457 FIXME, this will eventually lead to copy propagation removing the
6458 names that had useful range information attached to them. For
6459 instance, if we had the assertion N_i = ASSERT_EXPR <N_j, N_j > 3>,
6460 then N_i will have the range [3, +INF].
6462 However, by converting the assertion into the implied copy
6463 operation N_i = N_j, we will then copy-propagate N_j into the uses
6464 of N_i and lose the range information. We may want to hold on to
6465 ASSERT_EXPRs a little while longer as the ranges could be used in
6466 things like jump threading.
6468 The problem with keeping ASSERT_EXPRs around is that passes after
6469 VRP need to handle them appropriately.
6471 Another approach would be to make the range information a first
6472 class property of the SSA_NAME so that it can be queried from
6473 any pass. This is made somewhat more complex by the need for
6474 multiple ranges to be associated with one SSA_NAME. */
6476 static void
6477 remove_range_assertions (void)
6479 basic_block bb;
6480 gimple_stmt_iterator si;
6482 /* Note that the BSI iterator bump happens at the bottom of the
6483 loop and no bump is necessary if we're removing the statement
6484 referenced by the current BSI. */
6485 FOR_EACH_BB (bb)
6486 for (si = gsi_start_bb (bb); !gsi_end_p (si);)
6488 gimple stmt = gsi_stmt (si);
6489 gimple use_stmt;
6491 if (is_gimple_assign (stmt)
6492 && gimple_assign_rhs_code (stmt) == ASSERT_EXPR)
6494 tree rhs = gimple_assign_rhs1 (stmt);
6495 tree var;
6496 tree cond = fold (ASSERT_EXPR_COND (rhs));
6497 use_operand_p use_p;
6498 imm_use_iterator iter;
6500 gcc_assert (cond != boolean_false_node);
6502 /* Propagate the RHS into every use of the LHS. */
6503 var = ASSERT_EXPR_VAR (rhs);
6504 FOR_EACH_IMM_USE_STMT (use_stmt, iter,
6505 gimple_assign_lhs (stmt))
6506 FOR_EACH_IMM_USE_ON_STMT (use_p, iter)
6508 SET_USE (use_p, var);
6509 gcc_assert (TREE_CODE (var) == SSA_NAME);
6512 /* And finally, remove the copy, it is not needed. */
6513 gsi_remove (&si, true);
6514 release_defs (stmt);
6516 else
6517 gsi_next (&si);
6522 /* Return true if STMT is interesting for VRP. */
6524 static bool
6525 stmt_interesting_for_vrp (gimple stmt)
6527 if (gimple_code (stmt) == GIMPLE_PHI)
6529 tree res = gimple_phi_result (stmt);
6530 return (!virtual_operand_p (res)
6531 && (INTEGRAL_TYPE_P (TREE_TYPE (res))
6532 || POINTER_TYPE_P (TREE_TYPE (res))));
6534 else if (is_gimple_assign (stmt) || is_gimple_call (stmt))
6536 tree lhs = gimple_get_lhs (stmt);
6538 /* In general, assignments with virtual operands are not useful
6539 for deriving ranges, with the obvious exception of calls to
6540 builtin functions. */
6541 if (lhs && TREE_CODE (lhs) == SSA_NAME
6542 && (INTEGRAL_TYPE_P (TREE_TYPE (lhs))
6543 || POINTER_TYPE_P (TREE_TYPE (lhs)))
6544 && (is_gimple_call (stmt)
6545 || !gimple_vuse (stmt)))
6546 return true;
6548 else if (gimple_code (stmt) == GIMPLE_COND
6549 || gimple_code (stmt) == GIMPLE_SWITCH)
6550 return true;
6552 return false;
6556 /* Initialize local data structures for VRP. */
6558 static void
6559 vrp_initialize (void)
6561 basic_block bb;
6563 values_propagated = false;
6564 num_vr_values = num_ssa_names;
6565 vr_value = XCNEWVEC (value_range_t *, num_vr_values);
6566 vr_phi_edge_counts = XCNEWVEC (int, num_ssa_names);
6568 FOR_EACH_BB (bb)
6570 gimple_stmt_iterator si;
6572 for (si = gsi_start_phis (bb); !gsi_end_p (si); gsi_next (&si))
6574 gimple phi = gsi_stmt (si);
6575 if (!stmt_interesting_for_vrp (phi))
6577 tree lhs = PHI_RESULT (phi);
6578 set_value_range_to_varying (get_value_range (lhs));
6579 prop_set_simulate_again (phi, false);
6581 else
6582 prop_set_simulate_again (phi, true);
6585 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
6587 gimple stmt = gsi_stmt (si);
6589 /* If the statement is a control insn, then we do not
6590 want to avoid simulating the statement once. Failure
6591 to do so means that those edges will never get added. */
6592 if (stmt_ends_bb_p (stmt))
6593 prop_set_simulate_again (stmt, true);
6594 else if (!stmt_interesting_for_vrp (stmt))
6596 ssa_op_iter i;
6597 tree def;
6598 FOR_EACH_SSA_TREE_OPERAND (def, stmt, i, SSA_OP_DEF)
6599 set_value_range_to_varying (get_value_range (def));
6600 prop_set_simulate_again (stmt, false);
6602 else
6603 prop_set_simulate_again (stmt, true);
6608 /* Return the singleton value-range for NAME or NAME. */
6610 static inline tree
6611 vrp_valueize (tree name)
6613 if (TREE_CODE (name) == SSA_NAME)
6615 value_range_t *vr = get_value_range (name);
6616 if (vr->type == VR_RANGE
6617 && (vr->min == vr->max
6618 || operand_equal_p (vr->min, vr->max, 0)))
6619 return vr->min;
6621 return name;
6624 /* Visit assignment STMT. If it produces an interesting range, record
6625 the SSA name in *OUTPUT_P. */
6627 static enum ssa_prop_result
6628 vrp_visit_assignment_or_call (gimple stmt, tree *output_p)
6630 tree def, lhs;
6631 ssa_op_iter iter;
6632 enum gimple_code code = gimple_code (stmt);
6633 lhs = gimple_get_lhs (stmt);
6635 /* We only keep track of ranges in integral and pointer types. */
6636 if (TREE_CODE (lhs) == SSA_NAME
6637 && ((INTEGRAL_TYPE_P (TREE_TYPE (lhs))
6638 /* It is valid to have NULL MIN/MAX values on a type. See
6639 build_range_type. */
6640 && TYPE_MIN_VALUE (TREE_TYPE (lhs))
6641 && TYPE_MAX_VALUE (TREE_TYPE (lhs)))
6642 || POINTER_TYPE_P (TREE_TYPE (lhs))))
6644 value_range_t new_vr = VR_INITIALIZER;
6646 /* Try folding the statement to a constant first. */
6647 tree tem = gimple_fold_stmt_to_constant (stmt, vrp_valueize);
6648 if (tem && !is_overflow_infinity (tem))
6649 set_value_range (&new_vr, VR_RANGE, tem, tem, NULL);
6650 /* Then dispatch to value-range extracting functions. */
6651 else if (code == GIMPLE_CALL)
6652 extract_range_basic (&new_vr, stmt);
6653 else
6654 extract_range_from_assignment (&new_vr, stmt);
6656 if (update_value_range (lhs, &new_vr))
6658 *output_p = lhs;
6660 if (dump_file && (dump_flags & TDF_DETAILS))
6662 fprintf (dump_file, "Found new range for ");
6663 print_generic_expr (dump_file, lhs, 0);
6664 fprintf (dump_file, ": ");
6665 dump_value_range (dump_file, &new_vr);
6666 fprintf (dump_file, "\n\n");
6669 if (new_vr.type == VR_VARYING)
6670 return SSA_PROP_VARYING;
6672 return SSA_PROP_INTERESTING;
6675 return SSA_PROP_NOT_INTERESTING;
6678 /* Every other statement produces no useful ranges. */
6679 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_DEF)
6680 set_value_range_to_varying (get_value_range (def));
6682 return SSA_PROP_VARYING;
6685 /* Helper that gets the value range of the SSA_NAME with version I
6686 or a symbolic range containing the SSA_NAME only if the value range
6687 is varying or undefined. */
6689 static inline value_range_t
6690 get_vr_for_comparison (int i)
6692 value_range_t vr = *get_value_range (ssa_name (i));
6694 /* If name N_i does not have a valid range, use N_i as its own
6695 range. This allows us to compare against names that may
6696 have N_i in their ranges. */
6697 if (vr.type == VR_VARYING || vr.type == VR_UNDEFINED)
6699 vr.type = VR_RANGE;
6700 vr.min = ssa_name (i);
6701 vr.max = ssa_name (i);
6704 return vr;
6707 /* Compare all the value ranges for names equivalent to VAR with VAL
6708 using comparison code COMP. Return the same value returned by
6709 compare_range_with_value, including the setting of
6710 *STRICT_OVERFLOW_P. */
6712 static tree
6713 compare_name_with_value (enum tree_code comp, tree var, tree val,
6714 bool *strict_overflow_p)
6716 bitmap_iterator bi;
6717 unsigned i;
6718 bitmap e;
6719 tree retval, t;
6720 int used_strict_overflow;
6721 bool sop;
6722 value_range_t equiv_vr;
6724 /* Get the set of equivalences for VAR. */
6725 e = get_value_range (var)->equiv;
6727 /* Start at -1. Set it to 0 if we do a comparison without relying
6728 on overflow, or 1 if all comparisons rely on overflow. */
6729 used_strict_overflow = -1;
6731 /* Compare vars' value range with val. */
6732 equiv_vr = get_vr_for_comparison (SSA_NAME_VERSION (var));
6733 sop = false;
6734 retval = compare_range_with_value (comp, &equiv_vr, val, &sop);
6735 if (retval)
6736 used_strict_overflow = sop ? 1 : 0;
6738 /* If the equiv set is empty we have done all work we need to do. */
6739 if (e == NULL)
6741 if (retval
6742 && used_strict_overflow > 0)
6743 *strict_overflow_p = true;
6744 return retval;
6747 EXECUTE_IF_SET_IN_BITMAP (e, 0, i, bi)
6749 equiv_vr = get_vr_for_comparison (i);
6750 sop = false;
6751 t = compare_range_with_value (comp, &equiv_vr, val, &sop);
6752 if (t)
6754 /* If we get different answers from different members
6755 of the equivalence set this check must be in a dead
6756 code region. Folding it to a trap representation
6757 would be correct here. For now just return don't-know. */
6758 if (retval != NULL
6759 && t != retval)
6761 retval = NULL_TREE;
6762 break;
6764 retval = t;
6766 if (!sop)
6767 used_strict_overflow = 0;
6768 else if (used_strict_overflow < 0)
6769 used_strict_overflow = 1;
6773 if (retval
6774 && used_strict_overflow > 0)
6775 *strict_overflow_p = true;
6777 return retval;
6781 /* Given a comparison code COMP and names N1 and N2, compare all the
6782 ranges equivalent to N1 against all the ranges equivalent to N2
6783 to determine the value of N1 COMP N2. Return the same value
6784 returned by compare_ranges. Set *STRICT_OVERFLOW_P to indicate
6785 whether we relied on an overflow infinity in the comparison. */
6788 static tree
6789 compare_names (enum tree_code comp, tree n1, tree n2,
6790 bool *strict_overflow_p)
6792 tree t, retval;
6793 bitmap e1, e2;
6794 bitmap_iterator bi1, bi2;
6795 unsigned i1, i2;
6796 int used_strict_overflow;
6797 static bitmap_obstack *s_obstack = NULL;
6798 static bitmap s_e1 = NULL, s_e2 = NULL;
6800 /* Compare the ranges of every name equivalent to N1 against the
6801 ranges of every name equivalent to N2. */
6802 e1 = get_value_range (n1)->equiv;
6803 e2 = get_value_range (n2)->equiv;
6805 /* Use the fake bitmaps if e1 or e2 are not available. */
6806 if (s_obstack == NULL)
6808 s_obstack = XNEW (bitmap_obstack);
6809 bitmap_obstack_initialize (s_obstack);
6810 s_e1 = BITMAP_ALLOC (s_obstack);
6811 s_e2 = BITMAP_ALLOC (s_obstack);
6813 if (e1 == NULL)
6814 e1 = s_e1;
6815 if (e2 == NULL)
6816 e2 = s_e2;
6818 /* Add N1 and N2 to their own set of equivalences to avoid
6819 duplicating the body of the loop just to check N1 and N2
6820 ranges. */
6821 bitmap_set_bit (e1, SSA_NAME_VERSION (n1));
6822 bitmap_set_bit (e2, SSA_NAME_VERSION (n2));
6824 /* If the equivalence sets have a common intersection, then the two
6825 names can be compared without checking their ranges. */
6826 if (bitmap_intersect_p (e1, e2))
6828 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
6829 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
6831 return (comp == EQ_EXPR || comp == GE_EXPR || comp == LE_EXPR)
6832 ? boolean_true_node
6833 : boolean_false_node;
6836 /* Start at -1. Set it to 0 if we do a comparison without relying
6837 on overflow, or 1 if all comparisons rely on overflow. */
6838 used_strict_overflow = -1;
6840 /* Otherwise, compare all the equivalent ranges. First, add N1 and
6841 N2 to their own set of equivalences to avoid duplicating the body
6842 of the loop just to check N1 and N2 ranges. */
6843 EXECUTE_IF_SET_IN_BITMAP (e1, 0, i1, bi1)
6845 value_range_t vr1 = get_vr_for_comparison (i1);
6847 t = retval = NULL_TREE;
6848 EXECUTE_IF_SET_IN_BITMAP (e2, 0, i2, bi2)
6850 bool sop = false;
6852 value_range_t vr2 = get_vr_for_comparison (i2);
6854 t = compare_ranges (comp, &vr1, &vr2, &sop);
6855 if (t)
6857 /* If we get different answers from different members
6858 of the equivalence set this check must be in a dead
6859 code region. Folding it to a trap representation
6860 would be correct here. For now just return don't-know. */
6861 if (retval != NULL
6862 && t != retval)
6864 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
6865 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
6866 return NULL_TREE;
6868 retval = t;
6870 if (!sop)
6871 used_strict_overflow = 0;
6872 else if (used_strict_overflow < 0)
6873 used_strict_overflow = 1;
6877 if (retval)
6879 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
6880 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
6881 if (used_strict_overflow > 0)
6882 *strict_overflow_p = true;
6883 return retval;
6887 /* None of the equivalent ranges are useful in computing this
6888 comparison. */
6889 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
6890 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
6891 return NULL_TREE;
6894 /* Helper function for vrp_evaluate_conditional_warnv. */
6896 static tree
6897 vrp_evaluate_conditional_warnv_with_ops_using_ranges (enum tree_code code,
6898 tree op0, tree op1,
6899 bool * strict_overflow_p)
6901 value_range_t *vr0, *vr1;
6903 vr0 = (TREE_CODE (op0) == SSA_NAME) ? get_value_range (op0) : NULL;
6904 vr1 = (TREE_CODE (op1) == SSA_NAME) ? get_value_range (op1) : NULL;
6906 if (vr0 && vr1)
6907 return compare_ranges (code, vr0, vr1, strict_overflow_p);
6908 else if (vr0 && vr1 == NULL)
6909 return compare_range_with_value (code, vr0, op1, strict_overflow_p);
6910 else if (vr0 == NULL && vr1)
6911 return (compare_range_with_value
6912 (swap_tree_comparison (code), vr1, op0, strict_overflow_p));
6913 return NULL;
6916 /* Helper function for vrp_evaluate_conditional_warnv. */
6918 static tree
6919 vrp_evaluate_conditional_warnv_with_ops (enum tree_code code, tree op0,
6920 tree op1, bool use_equiv_p,
6921 bool *strict_overflow_p, bool *only_ranges)
6923 tree ret;
6924 if (only_ranges)
6925 *only_ranges = true;
6927 /* We only deal with integral and pointer types. */
6928 if (!INTEGRAL_TYPE_P (TREE_TYPE (op0))
6929 && !POINTER_TYPE_P (TREE_TYPE (op0)))
6930 return NULL_TREE;
6932 if (use_equiv_p)
6934 if (only_ranges
6935 && (ret = vrp_evaluate_conditional_warnv_with_ops_using_ranges
6936 (code, op0, op1, strict_overflow_p)))
6937 return ret;
6938 *only_ranges = false;
6939 if (TREE_CODE (op0) == SSA_NAME && TREE_CODE (op1) == SSA_NAME)
6940 return compare_names (code, op0, op1, strict_overflow_p);
6941 else if (TREE_CODE (op0) == SSA_NAME)
6942 return compare_name_with_value (code, op0, op1, strict_overflow_p);
6943 else if (TREE_CODE (op1) == SSA_NAME)
6944 return (compare_name_with_value
6945 (swap_tree_comparison (code), op1, op0, strict_overflow_p));
6947 else
6948 return vrp_evaluate_conditional_warnv_with_ops_using_ranges (code, op0, op1,
6949 strict_overflow_p);
6950 return NULL_TREE;
6953 /* Given (CODE OP0 OP1) within STMT, try to simplify it based on value range
6954 information. Return NULL if the conditional can not be evaluated.
6955 The ranges of all the names equivalent with the operands in COND
6956 will be used when trying to compute the value. If the result is
6957 based on undefined signed overflow, issue a warning if
6958 appropriate. */
6960 static tree
6961 vrp_evaluate_conditional (enum tree_code code, tree op0, tree op1, gimple stmt)
6963 bool sop;
6964 tree ret;
6965 bool only_ranges;
6967 /* Some passes and foldings leak constants with overflow flag set
6968 into the IL. Avoid doing wrong things with these and bail out. */
6969 if ((TREE_CODE (op0) == INTEGER_CST
6970 && TREE_OVERFLOW (op0))
6971 || (TREE_CODE (op1) == INTEGER_CST
6972 && TREE_OVERFLOW (op1)))
6973 return NULL_TREE;
6975 sop = false;
6976 ret = vrp_evaluate_conditional_warnv_with_ops (code, op0, op1, true, &sop,
6977 &only_ranges);
6979 if (ret && sop)
6981 enum warn_strict_overflow_code wc;
6982 const char* warnmsg;
6984 if (is_gimple_min_invariant (ret))
6986 wc = WARN_STRICT_OVERFLOW_CONDITIONAL;
6987 warnmsg = G_("assuming signed overflow does not occur when "
6988 "simplifying conditional to constant");
6990 else
6992 wc = WARN_STRICT_OVERFLOW_COMPARISON;
6993 warnmsg = G_("assuming signed overflow does not occur when "
6994 "simplifying conditional");
6997 if (issue_strict_overflow_warning (wc))
6999 location_t location;
7001 if (!gimple_has_location (stmt))
7002 location = input_location;
7003 else
7004 location = gimple_location (stmt);
7005 warning_at (location, OPT_Wstrict_overflow, "%s", warnmsg);
7009 if (warn_type_limits
7010 && ret && only_ranges
7011 && TREE_CODE_CLASS (code) == tcc_comparison
7012 && TREE_CODE (op0) == SSA_NAME)
7014 /* If the comparison is being folded and the operand on the LHS
7015 is being compared against a constant value that is outside of
7016 the natural range of OP0's type, then the predicate will
7017 always fold regardless of the value of OP0. If -Wtype-limits
7018 was specified, emit a warning. */
7019 tree type = TREE_TYPE (op0);
7020 value_range_t *vr0 = get_value_range (op0);
7022 if (vr0->type != VR_VARYING
7023 && INTEGRAL_TYPE_P (type)
7024 && vrp_val_is_min (vr0->min)
7025 && vrp_val_is_max (vr0->max)
7026 && is_gimple_min_invariant (op1))
7028 location_t location;
7030 if (!gimple_has_location (stmt))
7031 location = input_location;
7032 else
7033 location = gimple_location (stmt);
7035 warning_at (location, OPT_Wtype_limits,
7036 integer_zerop (ret)
7037 ? G_("comparison always false "
7038 "due to limited range of data type")
7039 : G_("comparison always true "
7040 "due to limited range of data type"));
7044 return ret;
7048 /* Visit conditional statement STMT. If we can determine which edge
7049 will be taken out of STMT's basic block, record it in
7050 *TAKEN_EDGE_P and return SSA_PROP_INTERESTING. Otherwise, return
7051 SSA_PROP_VARYING. */
7053 static enum ssa_prop_result
7054 vrp_visit_cond_stmt (gimple stmt, edge *taken_edge_p)
7056 tree val;
7057 bool sop;
7059 *taken_edge_p = NULL;
7061 if (dump_file && (dump_flags & TDF_DETAILS))
7063 tree use;
7064 ssa_op_iter i;
7066 fprintf (dump_file, "\nVisiting conditional with predicate: ");
7067 print_gimple_stmt (dump_file, stmt, 0, 0);
7068 fprintf (dump_file, "\nWith known ranges\n");
7070 FOR_EACH_SSA_TREE_OPERAND (use, stmt, i, SSA_OP_USE)
7072 fprintf (dump_file, "\t");
7073 print_generic_expr (dump_file, use, 0);
7074 fprintf (dump_file, ": ");
7075 dump_value_range (dump_file, vr_value[SSA_NAME_VERSION (use)]);
7078 fprintf (dump_file, "\n");
7081 /* Compute the value of the predicate COND by checking the known
7082 ranges of each of its operands.
7084 Note that we cannot evaluate all the equivalent ranges here
7085 because those ranges may not yet be final and with the current
7086 propagation strategy, we cannot determine when the value ranges
7087 of the names in the equivalence set have changed.
7089 For instance, given the following code fragment
7091 i_5 = PHI <8, i_13>
7093 i_14 = ASSERT_EXPR <i_5, i_5 != 0>
7094 if (i_14 == 1)
7097 Assume that on the first visit to i_14, i_5 has the temporary
7098 range [8, 8] because the second argument to the PHI function is
7099 not yet executable. We derive the range ~[0, 0] for i_14 and the
7100 equivalence set { i_5 }. So, when we visit 'if (i_14 == 1)' for
7101 the first time, since i_14 is equivalent to the range [8, 8], we
7102 determine that the predicate is always false.
7104 On the next round of propagation, i_13 is determined to be
7105 VARYING, which causes i_5 to drop down to VARYING. So, another
7106 visit to i_14 is scheduled. In this second visit, we compute the
7107 exact same range and equivalence set for i_14, namely ~[0, 0] and
7108 { i_5 }. But we did not have the previous range for i_5
7109 registered, so vrp_visit_assignment thinks that the range for
7110 i_14 has not changed. Therefore, the predicate 'if (i_14 == 1)'
7111 is not visited again, which stops propagation from visiting
7112 statements in the THEN clause of that if().
7114 To properly fix this we would need to keep the previous range
7115 value for the names in the equivalence set. This way we would've
7116 discovered that from one visit to the other i_5 changed from
7117 range [8, 8] to VR_VARYING.
7119 However, fixing this apparent limitation may not be worth the
7120 additional checking. Testing on several code bases (GCC, DLV,
7121 MICO, TRAMP3D and SPEC2000) showed that doing this results in
7122 4 more predicates folded in SPEC. */
7123 sop = false;
7125 val = vrp_evaluate_conditional_warnv_with_ops (gimple_cond_code (stmt),
7126 gimple_cond_lhs (stmt),
7127 gimple_cond_rhs (stmt),
7128 false, &sop, NULL);
7129 if (val)
7131 if (!sop)
7132 *taken_edge_p = find_taken_edge (gimple_bb (stmt), val);
7133 else
7135 if (dump_file && (dump_flags & TDF_DETAILS))
7136 fprintf (dump_file,
7137 "\nIgnoring predicate evaluation because "
7138 "it assumes that signed overflow is undefined");
7139 val = NULL_TREE;
7143 if (dump_file && (dump_flags & TDF_DETAILS))
7145 fprintf (dump_file, "\nPredicate evaluates to: ");
7146 if (val == NULL_TREE)
7147 fprintf (dump_file, "DON'T KNOW\n");
7148 else
7149 print_generic_stmt (dump_file, val, 0);
7152 return (*taken_edge_p) ? SSA_PROP_INTERESTING : SSA_PROP_VARYING;
7155 /* Searches the case label vector VEC for the index *IDX of the CASE_LABEL
7156 that includes the value VAL. The search is restricted to the range
7157 [START_IDX, n - 1] where n is the size of VEC.
7159 If there is a CASE_LABEL for VAL, its index is placed in IDX and true is
7160 returned.
7162 If there is no CASE_LABEL for VAL and there is one that is larger than VAL,
7163 it is placed in IDX and false is returned.
7165 If VAL is larger than any CASE_LABEL, n is placed on IDX and false is
7166 returned. */
7168 static bool
7169 find_case_label_index (gimple stmt, size_t start_idx, tree val, size_t *idx)
7171 size_t n = gimple_switch_num_labels (stmt);
7172 size_t low, high;
7174 /* Find case label for minimum of the value range or the next one.
7175 At each iteration we are searching in [low, high - 1]. */
7177 for (low = start_idx, high = n; high != low; )
7179 tree t;
7180 int cmp;
7181 /* Note that i != high, so we never ask for n. */
7182 size_t i = (high + low) / 2;
7183 t = gimple_switch_label (stmt, i);
7185 /* Cache the result of comparing CASE_LOW and val. */
7186 cmp = tree_int_cst_compare (CASE_LOW (t), val);
7188 if (cmp == 0)
7190 /* Ranges cannot be empty. */
7191 *idx = i;
7192 return true;
7194 else if (cmp > 0)
7195 high = i;
7196 else
7198 low = i + 1;
7199 if (CASE_HIGH (t) != NULL
7200 && tree_int_cst_compare (CASE_HIGH (t), val) >= 0)
7202 *idx = i;
7203 return true;
7208 *idx = high;
7209 return false;
7212 /* Searches the case label vector VEC for the range of CASE_LABELs that is used
7213 for values between MIN and MAX. The first index is placed in MIN_IDX. The
7214 last index is placed in MAX_IDX. If the range of CASE_LABELs is empty
7215 then MAX_IDX < MIN_IDX.
7216 Returns true if the default label is not needed. */
7218 static bool
7219 find_case_label_range (gimple stmt, tree min, tree max, size_t *min_idx,
7220 size_t *max_idx)
7222 size_t i, j;
7223 bool min_take_default = !find_case_label_index (stmt, 1, min, &i);
7224 bool max_take_default = !find_case_label_index (stmt, i, max, &j);
7226 if (i == j
7227 && min_take_default
7228 && max_take_default)
7230 /* Only the default case label reached.
7231 Return an empty range. */
7232 *min_idx = 1;
7233 *max_idx = 0;
7234 return false;
7236 else
7238 bool take_default = min_take_default || max_take_default;
7239 tree low, high;
7240 size_t k;
7242 if (max_take_default)
7243 j--;
7245 /* If the case label range is continuous, we do not need
7246 the default case label. Verify that. */
7247 high = CASE_LOW (gimple_switch_label (stmt, i));
7248 if (CASE_HIGH (gimple_switch_label (stmt, i)))
7249 high = CASE_HIGH (gimple_switch_label (stmt, i));
7250 for (k = i + 1; k <= j; ++k)
7252 low = CASE_LOW (gimple_switch_label (stmt, k));
7253 if (!integer_onep (int_const_binop (MINUS_EXPR, low, high)))
7255 take_default = true;
7256 break;
7258 high = low;
7259 if (CASE_HIGH (gimple_switch_label (stmt, k)))
7260 high = CASE_HIGH (gimple_switch_label (stmt, k));
7263 *min_idx = i;
7264 *max_idx = j;
7265 return !take_default;
7269 /* Searches the case label vector VEC for the ranges of CASE_LABELs that are
7270 used in range VR. The indices are placed in MIN_IDX1, MAX_IDX, MIN_IDX2 and
7271 MAX_IDX2. If the ranges of CASE_LABELs are empty then MAX_IDX1 < MIN_IDX1.
7272 Returns true if the default label is not needed. */
7274 static bool
7275 find_case_label_ranges (gimple stmt, value_range_t *vr, size_t *min_idx1,
7276 size_t *max_idx1, size_t *min_idx2,
7277 size_t *max_idx2)
7279 size_t i, j, k, l;
7280 unsigned int n = gimple_switch_num_labels (stmt);
7281 bool take_default;
7282 tree case_low, case_high;
7283 tree min = vr->min, max = vr->max;
7285 gcc_checking_assert (vr->type == VR_RANGE || vr->type == VR_ANTI_RANGE);
7287 take_default = !find_case_label_range (stmt, min, max, &i, &j);
7289 /* Set second range to emtpy. */
7290 *min_idx2 = 1;
7291 *max_idx2 = 0;
7293 if (vr->type == VR_RANGE)
7295 *min_idx1 = i;
7296 *max_idx1 = j;
7297 return !take_default;
7300 /* Set first range to all case labels. */
7301 *min_idx1 = 1;
7302 *max_idx1 = n - 1;
7304 if (i > j)
7305 return false;
7307 /* Make sure all the values of case labels [i , j] are contained in
7308 range [MIN, MAX]. */
7309 case_low = CASE_LOW (gimple_switch_label (stmt, i));
7310 case_high = CASE_HIGH (gimple_switch_label (stmt, j));
7311 if (tree_int_cst_compare (case_low, min) < 0)
7312 i += 1;
7313 if (case_high != NULL_TREE
7314 && tree_int_cst_compare (max, case_high) < 0)
7315 j -= 1;
7317 if (i > j)
7318 return false;
7320 /* If the range spans case labels [i, j], the corresponding anti-range spans
7321 the labels [1, i - 1] and [j + 1, n - 1]. */
7322 k = j + 1;
7323 l = n - 1;
7324 if (k > l)
7326 k = 1;
7327 l = 0;
7330 j = i - 1;
7331 i = 1;
7332 if (i > j)
7334 i = k;
7335 j = l;
7336 k = 1;
7337 l = 0;
7340 *min_idx1 = i;
7341 *max_idx1 = j;
7342 *min_idx2 = k;
7343 *max_idx2 = l;
7344 return false;
7347 /* Visit switch statement STMT. If we can determine which edge
7348 will be taken out of STMT's basic block, record it in
7349 *TAKEN_EDGE_P and return SSA_PROP_INTERESTING. Otherwise, return
7350 SSA_PROP_VARYING. */
7352 static enum ssa_prop_result
7353 vrp_visit_switch_stmt (gimple stmt, edge *taken_edge_p)
7355 tree op, val;
7356 value_range_t *vr;
7357 size_t i = 0, j = 0, k, l;
7358 bool take_default;
7360 *taken_edge_p = NULL;
7361 op = gimple_switch_index (stmt);
7362 if (TREE_CODE (op) != SSA_NAME)
7363 return SSA_PROP_VARYING;
7365 vr = get_value_range (op);
7366 if (dump_file && (dump_flags & TDF_DETAILS))
7368 fprintf (dump_file, "\nVisiting switch expression with operand ");
7369 print_generic_expr (dump_file, op, 0);
7370 fprintf (dump_file, " with known range ");
7371 dump_value_range (dump_file, vr);
7372 fprintf (dump_file, "\n");
7375 if ((vr->type != VR_RANGE
7376 && vr->type != VR_ANTI_RANGE)
7377 || symbolic_range_p (vr))
7378 return SSA_PROP_VARYING;
7380 /* Find the single edge that is taken from the switch expression. */
7381 take_default = !find_case_label_ranges (stmt, vr, &i, &j, &k, &l);
7383 /* Check if the range spans no CASE_LABEL. If so, we only reach the default
7384 label */
7385 if (j < i)
7387 gcc_assert (take_default);
7388 val = gimple_switch_default_label (stmt);
7390 else
7392 /* Check if labels with index i to j and maybe the default label
7393 are all reaching the same label. */
7395 val = gimple_switch_label (stmt, i);
7396 if (take_default
7397 && CASE_LABEL (gimple_switch_default_label (stmt))
7398 != CASE_LABEL (val))
7400 if (dump_file && (dump_flags & TDF_DETAILS))
7401 fprintf (dump_file, " not a single destination for this "
7402 "range\n");
7403 return SSA_PROP_VARYING;
7405 for (++i; i <= j; ++i)
7407 if (CASE_LABEL (gimple_switch_label (stmt, i)) != CASE_LABEL (val))
7409 if (dump_file && (dump_flags & TDF_DETAILS))
7410 fprintf (dump_file, " not a single destination for this "
7411 "range\n");
7412 return SSA_PROP_VARYING;
7415 for (; k <= l; ++k)
7417 if (CASE_LABEL (gimple_switch_label (stmt, k)) != CASE_LABEL (val))
7419 if (dump_file && (dump_flags & TDF_DETAILS))
7420 fprintf (dump_file, " not a single destination for this "
7421 "range\n");
7422 return SSA_PROP_VARYING;
7427 *taken_edge_p = find_edge (gimple_bb (stmt),
7428 label_to_block (CASE_LABEL (val)));
7430 if (dump_file && (dump_flags & TDF_DETAILS))
7432 fprintf (dump_file, " will take edge to ");
7433 print_generic_stmt (dump_file, CASE_LABEL (val), 0);
7436 return SSA_PROP_INTERESTING;
7440 /* Evaluate statement STMT. If the statement produces a useful range,
7441 return SSA_PROP_INTERESTING and record the SSA name with the
7442 interesting range into *OUTPUT_P.
7444 If STMT is a conditional branch and we can determine its truth
7445 value, the taken edge is recorded in *TAKEN_EDGE_P.
7447 If STMT produces a varying value, return SSA_PROP_VARYING. */
7449 static enum ssa_prop_result
7450 vrp_visit_stmt (gimple stmt, edge *taken_edge_p, tree *output_p)
7452 tree def;
7453 ssa_op_iter iter;
7455 if (dump_file && (dump_flags & TDF_DETAILS))
7457 fprintf (dump_file, "\nVisiting statement:\n");
7458 print_gimple_stmt (dump_file, stmt, 0, dump_flags);
7459 fprintf (dump_file, "\n");
7462 if (!stmt_interesting_for_vrp (stmt))
7463 gcc_assert (stmt_ends_bb_p (stmt));
7464 else if (is_gimple_assign (stmt) || is_gimple_call (stmt))
7465 return vrp_visit_assignment_or_call (stmt, output_p);
7466 else if (gimple_code (stmt) == GIMPLE_COND)
7467 return vrp_visit_cond_stmt (stmt, taken_edge_p);
7468 else if (gimple_code (stmt) == GIMPLE_SWITCH)
7469 return vrp_visit_switch_stmt (stmt, taken_edge_p);
7471 /* All other statements produce nothing of interest for VRP, so mark
7472 their outputs varying and prevent further simulation. */
7473 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_DEF)
7474 set_value_range_to_varying (get_value_range (def));
7476 return SSA_PROP_VARYING;
7479 /* Union the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
7480 { VR1TYPE, VR0MIN, VR0MAX } and store the result
7481 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
7482 possible such range. The resulting range is not canonicalized. */
7484 static void
7485 union_ranges (enum value_range_type *vr0type,
7486 tree *vr0min, tree *vr0max,
7487 enum value_range_type vr1type,
7488 tree vr1min, tree vr1max)
7490 bool mineq = operand_equal_p (*vr0min, vr1min, 0);
7491 bool maxeq = operand_equal_p (*vr0max, vr1max, 0);
7493 /* [] is vr0, () is vr1 in the following classification comments. */
7494 if (mineq && maxeq)
7496 /* [( )] */
7497 if (*vr0type == vr1type)
7498 /* Nothing to do for equal ranges. */
7500 else if ((*vr0type == VR_RANGE
7501 && vr1type == VR_ANTI_RANGE)
7502 || (*vr0type == VR_ANTI_RANGE
7503 && vr1type == VR_RANGE))
7505 /* For anti-range with range union the result is varying. */
7506 goto give_up;
7508 else
7509 gcc_unreachable ();
7511 else if (operand_less_p (*vr0max, vr1min) == 1
7512 || operand_less_p (vr1max, *vr0min) == 1)
7514 /* [ ] ( ) or ( ) [ ]
7515 If the ranges have an empty intersection, result of the union
7516 operation is the anti-range or if both are anti-ranges
7517 it covers all. */
7518 if (*vr0type == VR_ANTI_RANGE
7519 && vr1type == VR_ANTI_RANGE)
7520 goto give_up;
7521 else if (*vr0type == VR_ANTI_RANGE
7522 && vr1type == VR_RANGE)
7524 else if (*vr0type == VR_RANGE
7525 && vr1type == VR_ANTI_RANGE)
7527 *vr0type = vr1type;
7528 *vr0min = vr1min;
7529 *vr0max = vr1max;
7531 else if (*vr0type == VR_RANGE
7532 && vr1type == VR_RANGE)
7534 /* The result is the convex hull of both ranges. */
7535 if (operand_less_p (*vr0max, vr1min) == 1)
7537 /* If the result can be an anti-range, create one. */
7538 if (TREE_CODE (*vr0max) == INTEGER_CST
7539 && TREE_CODE (vr1min) == INTEGER_CST
7540 && vrp_val_is_min (*vr0min)
7541 && vrp_val_is_max (vr1max))
7543 tree min = int_const_binop (PLUS_EXPR,
7544 *vr0max, integer_one_node);
7545 tree max = int_const_binop (MINUS_EXPR,
7546 vr1min, integer_one_node);
7547 if (!operand_less_p (max, min))
7549 *vr0type = VR_ANTI_RANGE;
7550 *vr0min = min;
7551 *vr0max = max;
7553 else
7554 *vr0max = vr1max;
7556 else
7557 *vr0max = vr1max;
7559 else
7561 /* If the result can be an anti-range, create one. */
7562 if (TREE_CODE (vr1max) == INTEGER_CST
7563 && TREE_CODE (*vr0min) == INTEGER_CST
7564 && vrp_val_is_min (vr1min)
7565 && vrp_val_is_max (*vr0max))
7567 tree min = int_const_binop (PLUS_EXPR,
7568 vr1max, integer_one_node);
7569 tree max = int_const_binop (MINUS_EXPR,
7570 *vr0min, integer_one_node);
7571 if (!operand_less_p (max, min))
7573 *vr0type = VR_ANTI_RANGE;
7574 *vr0min = min;
7575 *vr0max = max;
7577 else
7578 *vr0min = vr1min;
7580 else
7581 *vr0min = vr1min;
7584 else
7585 gcc_unreachable ();
7587 else if ((maxeq || operand_less_p (vr1max, *vr0max) == 1)
7588 && (mineq || operand_less_p (*vr0min, vr1min) == 1))
7590 /* [ ( ) ] or [( ) ] or [ ( )] */
7591 if (*vr0type == VR_RANGE
7592 && vr1type == VR_RANGE)
7594 else if (*vr0type == VR_ANTI_RANGE
7595 && vr1type == VR_ANTI_RANGE)
7597 *vr0type = vr1type;
7598 *vr0min = vr1min;
7599 *vr0max = vr1max;
7601 else if (*vr0type == VR_ANTI_RANGE
7602 && vr1type == VR_RANGE)
7604 /* Arbitrarily choose the right or left gap. */
7605 if (!mineq && TREE_CODE (vr1min) == INTEGER_CST)
7606 *vr0max = int_const_binop (MINUS_EXPR, vr1min, integer_one_node);
7607 else if (!maxeq && TREE_CODE (vr1max) == INTEGER_CST)
7608 *vr0min = int_const_binop (PLUS_EXPR, vr1max, integer_one_node);
7609 else
7610 goto give_up;
7612 else if (*vr0type == VR_RANGE
7613 && vr1type == VR_ANTI_RANGE)
7614 /* The result covers everything. */
7615 goto give_up;
7616 else
7617 gcc_unreachable ();
7619 else if ((maxeq || operand_less_p (*vr0max, vr1max) == 1)
7620 && (mineq || operand_less_p (vr1min, *vr0min) == 1))
7622 /* ( [ ] ) or ([ ] ) or ( [ ]) */
7623 if (*vr0type == VR_RANGE
7624 && vr1type == VR_RANGE)
7626 *vr0type = vr1type;
7627 *vr0min = vr1min;
7628 *vr0max = vr1max;
7630 else if (*vr0type == VR_ANTI_RANGE
7631 && vr1type == VR_ANTI_RANGE)
7633 else if (*vr0type == VR_RANGE
7634 && vr1type == VR_ANTI_RANGE)
7636 *vr0type = VR_ANTI_RANGE;
7637 if (!mineq && TREE_CODE (*vr0min) == INTEGER_CST)
7639 *vr0max = int_const_binop (MINUS_EXPR, *vr0min, integer_one_node);
7640 *vr0min = vr1min;
7642 else if (!maxeq && TREE_CODE (*vr0max) == INTEGER_CST)
7644 *vr0min = int_const_binop (PLUS_EXPR, *vr0max, integer_one_node);
7645 *vr0max = vr1max;
7647 else
7648 goto give_up;
7650 else if (*vr0type == VR_ANTI_RANGE
7651 && vr1type == VR_RANGE)
7652 /* The result covers everything. */
7653 goto give_up;
7654 else
7655 gcc_unreachable ();
7657 else if ((operand_less_p (vr1min, *vr0max) == 1
7658 || operand_equal_p (vr1min, *vr0max, 0))
7659 && operand_less_p (*vr0min, vr1min) == 1)
7661 /* [ ( ] ) or [ ]( ) */
7662 if (*vr0type == VR_RANGE
7663 && vr1type == VR_RANGE)
7664 *vr0max = vr1max;
7665 else if (*vr0type == VR_ANTI_RANGE
7666 && vr1type == VR_ANTI_RANGE)
7667 *vr0min = vr1min;
7668 else if (*vr0type == VR_ANTI_RANGE
7669 && vr1type == VR_RANGE)
7671 if (TREE_CODE (vr1min) == INTEGER_CST)
7672 *vr0max = int_const_binop (MINUS_EXPR, vr1min, integer_one_node);
7673 else
7674 goto give_up;
7676 else if (*vr0type == VR_RANGE
7677 && vr1type == VR_ANTI_RANGE)
7679 if (TREE_CODE (*vr0max) == INTEGER_CST)
7681 *vr0type = vr1type;
7682 *vr0min = int_const_binop (PLUS_EXPR, *vr0max, integer_one_node);
7683 *vr0max = vr1max;
7685 else
7686 goto give_up;
7688 else
7689 gcc_unreachable ();
7691 else if ((operand_less_p (*vr0min, vr1max) == 1
7692 || operand_equal_p (*vr0min, vr1max, 0))
7693 && operand_less_p (vr1min, *vr0min) == 1)
7695 /* ( [ ) ] or ( )[ ] */
7696 if (*vr0type == VR_RANGE
7697 && vr1type == VR_RANGE)
7698 *vr0min = vr1min;
7699 else if (*vr0type == VR_ANTI_RANGE
7700 && vr1type == VR_ANTI_RANGE)
7701 *vr0max = vr1max;
7702 else if (*vr0type == VR_ANTI_RANGE
7703 && vr1type == VR_RANGE)
7705 if (TREE_CODE (vr1max) == INTEGER_CST)
7706 *vr0min = int_const_binop (PLUS_EXPR, vr1max, integer_one_node);
7707 else
7708 goto give_up;
7710 else if (*vr0type == VR_RANGE
7711 && vr1type == VR_ANTI_RANGE)
7713 if (TREE_CODE (*vr0min) == INTEGER_CST)
7715 *vr0type = vr1type;
7716 *vr0min = vr1min;
7717 *vr0max = int_const_binop (MINUS_EXPR, *vr0min, integer_one_node);
7719 else
7720 goto give_up;
7722 else
7723 gcc_unreachable ();
7725 else
7726 goto give_up;
7728 return;
7730 give_up:
7731 *vr0type = VR_VARYING;
7732 *vr0min = NULL_TREE;
7733 *vr0max = NULL_TREE;
7736 /* Intersect the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
7737 { VR1TYPE, VR0MIN, VR0MAX } and store the result
7738 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
7739 possible such range. The resulting range is not canonicalized. */
7741 static void
7742 intersect_ranges (enum value_range_type *vr0type,
7743 tree *vr0min, tree *vr0max,
7744 enum value_range_type vr1type,
7745 tree vr1min, tree vr1max)
7747 bool mineq = operand_equal_p (*vr0min, vr1min, 0);
7748 bool maxeq = operand_equal_p (*vr0max, vr1max, 0);
7750 /* [] is vr0, () is vr1 in the following classification comments. */
7751 if (mineq && maxeq)
7753 /* [( )] */
7754 if (*vr0type == vr1type)
7755 /* Nothing to do for equal ranges. */
7757 else if ((*vr0type == VR_RANGE
7758 && vr1type == VR_ANTI_RANGE)
7759 || (*vr0type == VR_ANTI_RANGE
7760 && vr1type == VR_RANGE))
7762 /* For anti-range with range intersection the result is empty. */
7763 *vr0type = VR_UNDEFINED;
7764 *vr0min = NULL_TREE;
7765 *vr0max = NULL_TREE;
7767 else
7768 gcc_unreachable ();
7770 else if (operand_less_p (*vr0max, vr1min) == 1
7771 || operand_less_p (vr1max, *vr0min) == 1)
7773 /* [ ] ( ) or ( ) [ ]
7774 If the ranges have an empty intersection, the result of the
7775 intersect operation is the range for intersecting an
7776 anti-range with a range or empty when intersecting two ranges. */
7777 if (*vr0type == VR_RANGE
7778 && vr1type == VR_ANTI_RANGE)
7780 else if (*vr0type == VR_ANTI_RANGE
7781 && vr1type == VR_RANGE)
7783 *vr0type = vr1type;
7784 *vr0min = vr1min;
7785 *vr0max = vr1max;
7787 else if (*vr0type == VR_RANGE
7788 && vr1type == VR_RANGE)
7790 *vr0type = VR_UNDEFINED;
7791 *vr0min = NULL_TREE;
7792 *vr0max = NULL_TREE;
7794 else if (*vr0type == VR_ANTI_RANGE
7795 && vr1type == VR_ANTI_RANGE)
7797 /* If the anti-ranges are adjacent to each other merge them. */
7798 if (TREE_CODE (*vr0max) == INTEGER_CST
7799 && TREE_CODE (vr1min) == INTEGER_CST
7800 && operand_less_p (*vr0max, vr1min) == 1
7801 && integer_onep (int_const_binop (MINUS_EXPR,
7802 vr1min, *vr0max)))
7803 *vr0max = vr1max;
7804 else if (TREE_CODE (vr1max) == INTEGER_CST
7805 && TREE_CODE (*vr0min) == INTEGER_CST
7806 && operand_less_p (vr1max, *vr0min) == 1
7807 && integer_onep (int_const_binop (MINUS_EXPR,
7808 *vr0min, vr1max)))
7809 *vr0min = vr1min;
7810 /* Else arbitrarily take VR0. */
7813 else if ((maxeq || operand_less_p (vr1max, *vr0max) == 1)
7814 && (mineq || operand_less_p (*vr0min, vr1min) == 1))
7816 /* [ ( ) ] or [( ) ] or [ ( )] */
7817 if (*vr0type == VR_RANGE
7818 && vr1type == VR_RANGE)
7820 /* If both are ranges the result is the inner one. */
7821 *vr0type = vr1type;
7822 *vr0min = vr1min;
7823 *vr0max = vr1max;
7825 else if (*vr0type == VR_RANGE
7826 && vr1type == VR_ANTI_RANGE)
7828 /* Choose the right gap if the left one is empty. */
7829 if (mineq)
7831 if (TREE_CODE (vr1max) == INTEGER_CST)
7832 *vr0min = int_const_binop (PLUS_EXPR, vr1max, integer_one_node);
7833 else
7834 *vr0min = vr1max;
7836 /* Choose the left gap if the right one is empty. */
7837 else if (maxeq)
7839 if (TREE_CODE (vr1min) == INTEGER_CST)
7840 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
7841 integer_one_node);
7842 else
7843 *vr0max = vr1min;
7845 /* Choose the anti-range if the range is effectively varying. */
7846 else if (vrp_val_is_min (*vr0min)
7847 && vrp_val_is_max (*vr0max))
7849 *vr0type = vr1type;
7850 *vr0min = vr1min;
7851 *vr0max = vr1max;
7853 /* Else choose the range. */
7855 else if (*vr0type == VR_ANTI_RANGE
7856 && vr1type == VR_ANTI_RANGE)
7857 /* If both are anti-ranges the result is the outer one. */
7859 else if (*vr0type == VR_ANTI_RANGE
7860 && vr1type == VR_RANGE)
7862 /* The intersection is empty. */
7863 *vr0type = VR_UNDEFINED;
7864 *vr0min = NULL_TREE;
7865 *vr0max = NULL_TREE;
7867 else
7868 gcc_unreachable ();
7870 else if ((maxeq || operand_less_p (*vr0max, vr1max) == 1)
7871 && (mineq || operand_less_p (vr1min, *vr0min) == 1))
7873 /* ( [ ] ) or ([ ] ) or ( [ ]) */
7874 if (*vr0type == VR_RANGE
7875 && vr1type == VR_RANGE)
7876 /* Choose the inner range. */
7878 else if (*vr0type == VR_ANTI_RANGE
7879 && vr1type == VR_RANGE)
7881 /* Choose the right gap if the left is empty. */
7882 if (mineq)
7884 *vr0type = VR_RANGE;
7885 if (TREE_CODE (*vr0max) == INTEGER_CST)
7886 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
7887 integer_one_node);
7888 else
7889 *vr0min = *vr0max;
7890 *vr0max = vr1max;
7892 /* Choose the left gap if the right is empty. */
7893 else if (maxeq)
7895 *vr0type = VR_RANGE;
7896 if (TREE_CODE (*vr0min) == INTEGER_CST)
7897 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
7898 integer_one_node);
7899 else
7900 *vr0max = *vr0min;
7901 *vr0min = vr1min;
7903 /* Choose the anti-range if the range is effectively varying. */
7904 else if (vrp_val_is_min (vr1min)
7905 && vrp_val_is_max (vr1max))
7907 /* Else choose the range. */
7908 else
7910 *vr0type = vr1type;
7911 *vr0min = vr1min;
7912 *vr0max = vr1max;
7915 else if (*vr0type == VR_ANTI_RANGE
7916 && vr1type == VR_ANTI_RANGE)
7918 /* If both are anti-ranges the result is the outer one. */
7919 *vr0type = vr1type;
7920 *vr0min = vr1min;
7921 *vr0max = vr1max;
7923 else if (vr1type == VR_ANTI_RANGE
7924 && *vr0type == VR_RANGE)
7926 /* The intersection is empty. */
7927 *vr0type = VR_UNDEFINED;
7928 *vr0min = NULL_TREE;
7929 *vr0max = NULL_TREE;
7931 else
7932 gcc_unreachable ();
7934 else if ((operand_less_p (vr1min, *vr0max) == 1
7935 || operand_equal_p (vr1min, *vr0max, 0))
7936 && operand_less_p (*vr0min, vr1min) == 1)
7938 /* [ ( ] ) or [ ]( ) */
7939 if (*vr0type == VR_ANTI_RANGE
7940 && vr1type == VR_ANTI_RANGE)
7941 *vr0max = vr1max;
7942 else if (*vr0type == VR_RANGE
7943 && vr1type == VR_RANGE)
7944 *vr0min = vr1min;
7945 else if (*vr0type == VR_RANGE
7946 && vr1type == VR_ANTI_RANGE)
7948 if (TREE_CODE (vr1min) == INTEGER_CST)
7949 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
7950 integer_one_node);
7951 else
7952 *vr0max = vr1min;
7954 else if (*vr0type == VR_ANTI_RANGE
7955 && vr1type == VR_RANGE)
7957 *vr0type = VR_RANGE;
7958 if (TREE_CODE (*vr0max) == INTEGER_CST)
7959 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
7960 integer_one_node);
7961 else
7962 *vr0min = *vr0max;
7963 *vr0max = vr1max;
7965 else
7966 gcc_unreachable ();
7968 else if ((operand_less_p (*vr0min, vr1max) == 1
7969 || operand_equal_p (*vr0min, vr1max, 0))
7970 && operand_less_p (vr1min, *vr0min) == 1)
7972 /* ( [ ) ] or ( )[ ] */
7973 if (*vr0type == VR_ANTI_RANGE
7974 && vr1type == VR_ANTI_RANGE)
7975 *vr0min = vr1min;
7976 else if (*vr0type == VR_RANGE
7977 && vr1type == VR_RANGE)
7978 *vr0max = vr1max;
7979 else if (*vr0type == VR_RANGE
7980 && vr1type == VR_ANTI_RANGE)
7982 if (TREE_CODE (vr1max) == INTEGER_CST)
7983 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
7984 integer_one_node);
7985 else
7986 *vr0min = vr1max;
7988 else if (*vr0type == VR_ANTI_RANGE
7989 && vr1type == VR_RANGE)
7991 *vr0type = VR_RANGE;
7992 if (TREE_CODE (*vr0min) == INTEGER_CST)
7993 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
7994 integer_one_node);
7995 else
7996 *vr0max = *vr0min;
7997 *vr0min = vr1min;
7999 else
8000 gcc_unreachable ();
8003 /* As a fallback simply use { *VRTYPE, *VR0MIN, *VR0MAX } as
8004 result for the intersection. That's always a conservative
8005 correct estimate. */
8007 return;
8011 /* Intersect the two value-ranges *VR0 and *VR1 and store the result
8012 in *VR0. This may not be the smallest possible such range. */
8014 static void
8015 vrp_intersect_ranges_1 (value_range_t *vr0, value_range_t *vr1)
8017 value_range_t saved;
8019 /* If either range is VR_VARYING the other one wins. */
8020 if (vr1->type == VR_VARYING)
8021 return;
8022 if (vr0->type == VR_VARYING)
8024 copy_value_range (vr0, vr1);
8025 return;
8028 /* When either range is VR_UNDEFINED the resulting range is
8029 VR_UNDEFINED, too. */
8030 if (vr0->type == VR_UNDEFINED)
8031 return;
8032 if (vr1->type == VR_UNDEFINED)
8034 set_value_range_to_undefined (vr0);
8035 return;
8038 /* Save the original vr0 so we can return it as conservative intersection
8039 result when our worker turns things to varying. */
8040 saved = *vr0;
8041 intersect_ranges (&vr0->type, &vr0->min, &vr0->max,
8042 vr1->type, vr1->min, vr1->max);
8043 /* Make sure to canonicalize the result though as the inversion of a
8044 VR_RANGE can still be a VR_RANGE. */
8045 set_and_canonicalize_value_range (vr0, vr0->type,
8046 vr0->min, vr0->max, vr0->equiv);
8047 /* If that failed, use the saved original VR0. */
8048 if (vr0->type == VR_VARYING)
8050 *vr0 = saved;
8051 return;
8053 /* If the result is VR_UNDEFINED there is no need to mess with
8054 the equivalencies. */
8055 if (vr0->type == VR_UNDEFINED)
8056 return;
8058 /* The resulting set of equivalences for range intersection is the union of
8059 the two sets. */
8060 if (vr0->equiv && vr1->equiv && vr0->equiv != vr1->equiv)
8061 bitmap_ior_into (vr0->equiv, vr1->equiv);
8062 else if (vr1->equiv && !vr0->equiv)
8063 bitmap_copy (vr0->equiv, vr1->equiv);
8066 static void
8067 vrp_intersect_ranges (value_range_t *vr0, value_range_t *vr1)
8069 if (dump_file && (dump_flags & TDF_DETAILS))
8071 fprintf (dump_file, "Intersecting\n ");
8072 dump_value_range (dump_file, vr0);
8073 fprintf (dump_file, "\nand\n ");
8074 dump_value_range (dump_file, vr1);
8075 fprintf (dump_file, "\n");
8077 vrp_intersect_ranges_1 (vr0, vr1);
8078 if (dump_file && (dump_flags & TDF_DETAILS))
8080 fprintf (dump_file, "to\n ");
8081 dump_value_range (dump_file, vr0);
8082 fprintf (dump_file, "\n");
8086 /* Meet operation for value ranges. Given two value ranges VR0 and
8087 VR1, store in VR0 a range that contains both VR0 and VR1. This
8088 may not be the smallest possible such range. */
8090 static void
8091 vrp_meet_1 (value_range_t *vr0, value_range_t *vr1)
8093 value_range_t saved;
8095 if (vr0->type == VR_UNDEFINED)
8097 set_value_range (vr0, vr1->type, vr1->min, vr1->max, vr1->equiv);
8098 return;
8101 if (vr1->type == VR_UNDEFINED)
8103 /* VR0 already has the resulting range. */
8104 return;
8107 if (vr0->type == VR_VARYING)
8109 /* Nothing to do. VR0 already has the resulting range. */
8110 return;
8113 if (vr1->type == VR_VARYING)
8115 set_value_range_to_varying (vr0);
8116 return;
8119 saved = *vr0;
8120 union_ranges (&vr0->type, &vr0->min, &vr0->max,
8121 vr1->type, vr1->min, vr1->max);
8122 if (vr0->type == VR_VARYING)
8124 /* Failed to find an efficient meet. Before giving up and setting
8125 the result to VARYING, see if we can at least derive a useful
8126 anti-range. FIXME, all this nonsense about distinguishing
8127 anti-ranges from ranges is necessary because of the odd
8128 semantics of range_includes_zero_p and friends. */
8129 if (((saved.type == VR_RANGE
8130 && range_includes_zero_p (saved.min, saved.max) == 0)
8131 || (saved.type == VR_ANTI_RANGE
8132 && range_includes_zero_p (saved.min, saved.max) == 1))
8133 && ((vr1->type == VR_RANGE
8134 && range_includes_zero_p (vr1->min, vr1->max) == 0)
8135 || (vr1->type == VR_ANTI_RANGE
8136 && range_includes_zero_p (vr1->min, vr1->max) == 1)))
8138 set_value_range_to_nonnull (vr0, TREE_TYPE (saved.min));
8140 /* Since this meet operation did not result from the meeting of
8141 two equivalent names, VR0 cannot have any equivalences. */
8142 if (vr0->equiv)
8143 bitmap_clear (vr0->equiv);
8144 return;
8147 set_value_range_to_varying (vr0);
8148 return;
8150 set_and_canonicalize_value_range (vr0, vr0->type, vr0->min, vr0->max,
8151 vr0->equiv);
8152 if (vr0->type == VR_VARYING)
8153 return;
8155 /* The resulting set of equivalences is always the intersection of
8156 the two sets. */
8157 if (vr0->equiv && vr1->equiv && vr0->equiv != vr1->equiv)
8158 bitmap_and_into (vr0->equiv, vr1->equiv);
8159 else if (vr0->equiv && !vr1->equiv)
8160 bitmap_clear (vr0->equiv);
8163 static void
8164 vrp_meet (value_range_t *vr0, value_range_t *vr1)
8166 if (dump_file && (dump_flags & TDF_DETAILS))
8168 fprintf (dump_file, "Meeting\n ");
8169 dump_value_range (dump_file, vr0);
8170 fprintf (dump_file, "\nand\n ");
8171 dump_value_range (dump_file, vr1);
8172 fprintf (dump_file, "\n");
8174 vrp_meet_1 (vr0, vr1);
8175 if (dump_file && (dump_flags & TDF_DETAILS))
8177 fprintf (dump_file, "to\n ");
8178 dump_value_range (dump_file, vr0);
8179 fprintf (dump_file, "\n");
8184 /* Visit all arguments for PHI node PHI that flow through executable
8185 edges. If a valid value range can be derived from all the incoming
8186 value ranges, set a new range for the LHS of PHI. */
8188 static enum ssa_prop_result
8189 vrp_visit_phi_node (gimple phi)
8191 size_t i;
8192 tree lhs = PHI_RESULT (phi);
8193 value_range_t *lhs_vr = get_value_range (lhs);
8194 value_range_t vr_result = VR_INITIALIZER;
8195 bool first = true;
8196 int edges, old_edges;
8197 struct loop *l;
8199 if (dump_file && (dump_flags & TDF_DETAILS))
8201 fprintf (dump_file, "\nVisiting PHI node: ");
8202 print_gimple_stmt (dump_file, phi, 0, dump_flags);
8205 edges = 0;
8206 for (i = 0; i < gimple_phi_num_args (phi); i++)
8208 edge e = gimple_phi_arg_edge (phi, i);
8210 if (dump_file && (dump_flags & TDF_DETAILS))
8212 fprintf (dump_file,
8213 "\n Argument #%d (%d -> %d %sexecutable)\n",
8214 (int) i, e->src->index, e->dest->index,
8215 (e->flags & EDGE_EXECUTABLE) ? "" : "not ");
8218 if (e->flags & EDGE_EXECUTABLE)
8220 tree arg = PHI_ARG_DEF (phi, i);
8221 value_range_t vr_arg;
8223 ++edges;
8225 if (TREE_CODE (arg) == SSA_NAME)
8227 vr_arg = *(get_value_range (arg));
8228 /* Do not allow equivalences or symbolic ranges to leak in from
8229 backedges. That creates invalid equivalencies.
8230 See PR53465 and PR54767. */
8231 if (e->flags & EDGE_DFS_BACK
8232 && (vr_arg.type == VR_RANGE
8233 || vr_arg.type == VR_ANTI_RANGE))
8235 vr_arg.equiv = NULL;
8236 if (symbolic_range_p (&vr_arg))
8238 vr_arg.type = VR_VARYING;
8239 vr_arg.min = NULL_TREE;
8240 vr_arg.max = NULL_TREE;
8244 else
8246 if (is_overflow_infinity (arg))
8248 arg = copy_node (arg);
8249 TREE_OVERFLOW (arg) = 0;
8252 vr_arg.type = VR_RANGE;
8253 vr_arg.min = arg;
8254 vr_arg.max = arg;
8255 vr_arg.equiv = NULL;
8258 if (dump_file && (dump_flags & TDF_DETAILS))
8260 fprintf (dump_file, "\t");
8261 print_generic_expr (dump_file, arg, dump_flags);
8262 fprintf (dump_file, "\n\tValue: ");
8263 dump_value_range (dump_file, &vr_arg);
8264 fprintf (dump_file, "\n");
8267 if (first)
8268 copy_value_range (&vr_result, &vr_arg);
8269 else
8270 vrp_meet (&vr_result, &vr_arg);
8271 first = false;
8273 if (vr_result.type == VR_VARYING)
8274 break;
8278 if (vr_result.type == VR_VARYING)
8279 goto varying;
8280 else if (vr_result.type == VR_UNDEFINED)
8281 goto update_range;
8283 old_edges = vr_phi_edge_counts[SSA_NAME_VERSION (lhs)];
8284 vr_phi_edge_counts[SSA_NAME_VERSION (lhs)] = edges;
8286 /* To prevent infinite iterations in the algorithm, derive ranges
8287 when the new value is slightly bigger or smaller than the
8288 previous one. We don't do this if we have seen a new executable
8289 edge; this helps us avoid an overflow infinity for conditionals
8290 which are not in a loop. If the old value-range was VR_UNDEFINED
8291 use the updated range and iterate one more time. */
8292 if (edges > 0
8293 && gimple_phi_num_args (phi) > 1
8294 && edges == old_edges
8295 && lhs_vr->type != VR_UNDEFINED)
8297 int cmp_min = compare_values (lhs_vr->min, vr_result.min);
8298 int cmp_max = compare_values (lhs_vr->max, vr_result.max);
8300 /* For non VR_RANGE or for pointers fall back to varying if
8301 the range changed. */
8302 if ((lhs_vr->type != VR_RANGE || vr_result.type != VR_RANGE
8303 || POINTER_TYPE_P (TREE_TYPE (lhs)))
8304 && (cmp_min != 0 || cmp_max != 0))
8305 goto varying;
8307 /* If the new minimum is smaller or larger than the previous
8308 one, go all the way to -INF. In the first case, to avoid
8309 iterating millions of times to reach -INF, and in the
8310 other case to avoid infinite bouncing between different
8311 minimums. */
8312 if (cmp_min > 0 || cmp_min < 0)
8314 if (!needs_overflow_infinity (TREE_TYPE (vr_result.min))
8315 || !vrp_var_may_overflow (lhs, phi))
8316 vr_result.min = TYPE_MIN_VALUE (TREE_TYPE (vr_result.min));
8317 else if (supports_overflow_infinity (TREE_TYPE (vr_result.min)))
8318 vr_result.min =
8319 negative_overflow_infinity (TREE_TYPE (vr_result.min));
8322 /* Similarly, if the new maximum is smaller or larger than
8323 the previous one, go all the way to +INF. */
8324 if (cmp_max < 0 || cmp_max > 0)
8326 if (!needs_overflow_infinity (TREE_TYPE (vr_result.max))
8327 || !vrp_var_may_overflow (lhs, phi))
8328 vr_result.max = TYPE_MAX_VALUE (TREE_TYPE (vr_result.max));
8329 else if (supports_overflow_infinity (TREE_TYPE (vr_result.max)))
8330 vr_result.max =
8331 positive_overflow_infinity (TREE_TYPE (vr_result.max));
8334 /* If we dropped either bound to +-INF then if this is a loop
8335 PHI node SCEV may known more about its value-range. */
8336 if ((cmp_min > 0 || cmp_min < 0
8337 || cmp_max < 0 || cmp_max > 0)
8338 && current_loops
8339 && (l = loop_containing_stmt (phi))
8340 && l->header == gimple_bb (phi))
8341 adjust_range_with_scev (&vr_result, l, phi, lhs);
8343 /* If we will end up with a (-INF, +INF) range, set it to
8344 VARYING. Same if the previous max value was invalid for
8345 the type and we end up with vr_result.min > vr_result.max. */
8346 if ((vrp_val_is_max (vr_result.max)
8347 && vrp_val_is_min (vr_result.min))
8348 || compare_values (vr_result.min,
8349 vr_result.max) > 0)
8350 goto varying;
8353 /* If the new range is different than the previous value, keep
8354 iterating. */
8355 update_range:
8356 if (update_value_range (lhs, &vr_result))
8358 if (dump_file && (dump_flags & TDF_DETAILS))
8360 fprintf (dump_file, "Found new range for ");
8361 print_generic_expr (dump_file, lhs, 0);
8362 fprintf (dump_file, ": ");
8363 dump_value_range (dump_file, &vr_result);
8364 fprintf (dump_file, "\n\n");
8367 return SSA_PROP_INTERESTING;
8370 /* Nothing changed, don't add outgoing edges. */
8371 return SSA_PROP_NOT_INTERESTING;
8373 /* No match found. Set the LHS to VARYING. */
8374 varying:
8375 set_value_range_to_varying (lhs_vr);
8376 return SSA_PROP_VARYING;
8379 /* Simplify boolean operations if the source is known
8380 to be already a boolean. */
8381 static bool
8382 simplify_truth_ops_using_ranges (gimple_stmt_iterator *gsi, gimple stmt)
8384 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
8385 tree lhs, op0, op1;
8386 bool need_conversion;
8388 /* We handle only !=/== case here. */
8389 gcc_assert (rhs_code == EQ_EXPR || rhs_code == NE_EXPR);
8391 op0 = gimple_assign_rhs1 (stmt);
8392 if (!op_with_boolean_value_range_p (op0))
8393 return false;
8395 op1 = gimple_assign_rhs2 (stmt);
8396 if (!op_with_boolean_value_range_p (op1))
8397 return false;
8399 /* Reduce number of cases to handle to NE_EXPR. As there is no
8400 BIT_XNOR_EXPR we cannot replace A == B with a single statement. */
8401 if (rhs_code == EQ_EXPR)
8403 if (TREE_CODE (op1) == INTEGER_CST)
8404 op1 = int_const_binop (BIT_XOR_EXPR, op1, integer_one_node);
8405 else
8406 return false;
8409 lhs = gimple_assign_lhs (stmt);
8410 need_conversion
8411 = !useless_type_conversion_p (TREE_TYPE (lhs), TREE_TYPE (op0));
8413 /* Make sure to not sign-extend a 1-bit 1 when converting the result. */
8414 if (need_conversion
8415 && !TYPE_UNSIGNED (TREE_TYPE (op0))
8416 && TYPE_PRECISION (TREE_TYPE (op0)) == 1
8417 && TYPE_PRECISION (TREE_TYPE (lhs)) > 1)
8418 return false;
8420 /* For A != 0 we can substitute A itself. */
8421 if (integer_zerop (op1))
8422 gimple_assign_set_rhs_with_ops (gsi,
8423 need_conversion
8424 ? NOP_EXPR : TREE_CODE (op0),
8425 op0, NULL_TREE);
8426 /* For A != B we substitute A ^ B. Either with conversion. */
8427 else if (need_conversion)
8429 tree tem = make_ssa_name (TREE_TYPE (op0), NULL);
8430 gimple newop = gimple_build_assign_with_ops (BIT_XOR_EXPR, tem, op0, op1);
8431 gsi_insert_before (gsi, newop, GSI_SAME_STMT);
8432 gimple_assign_set_rhs_with_ops (gsi, NOP_EXPR, tem, NULL_TREE);
8434 /* Or without. */
8435 else
8436 gimple_assign_set_rhs_with_ops (gsi, BIT_XOR_EXPR, op0, op1);
8437 update_stmt (gsi_stmt (*gsi));
8439 return true;
8442 /* Simplify a division or modulo operator to a right shift or
8443 bitwise and if the first operand is unsigned or is greater
8444 than zero and the second operand is an exact power of two. */
8446 static bool
8447 simplify_div_or_mod_using_ranges (gimple stmt)
8449 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
8450 tree val = NULL;
8451 tree op0 = gimple_assign_rhs1 (stmt);
8452 tree op1 = gimple_assign_rhs2 (stmt);
8453 value_range_t *vr = get_value_range (gimple_assign_rhs1 (stmt));
8455 if (TYPE_UNSIGNED (TREE_TYPE (op0)))
8457 val = integer_one_node;
8459 else
8461 bool sop = false;
8463 val = compare_range_with_value (GE_EXPR, vr, integer_zero_node, &sop);
8465 if (val
8466 && sop
8467 && integer_onep (val)
8468 && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC))
8470 location_t location;
8472 if (!gimple_has_location (stmt))
8473 location = input_location;
8474 else
8475 location = gimple_location (stmt);
8476 warning_at (location, OPT_Wstrict_overflow,
8477 "assuming signed overflow does not occur when "
8478 "simplifying %</%> or %<%%%> to %<>>%> or %<&%>");
8482 if (val && integer_onep (val))
8484 tree t;
8486 if (rhs_code == TRUNC_DIV_EXPR)
8488 t = build_int_cst (integer_type_node, tree_log2 (op1));
8489 gimple_assign_set_rhs_code (stmt, RSHIFT_EXPR);
8490 gimple_assign_set_rhs1 (stmt, op0);
8491 gimple_assign_set_rhs2 (stmt, t);
8493 else
8495 t = build_int_cst (TREE_TYPE (op1), 1);
8496 t = int_const_binop (MINUS_EXPR, op1, t);
8497 t = fold_convert (TREE_TYPE (op0), t);
8499 gimple_assign_set_rhs_code (stmt, BIT_AND_EXPR);
8500 gimple_assign_set_rhs1 (stmt, op0);
8501 gimple_assign_set_rhs2 (stmt, t);
8504 update_stmt (stmt);
8505 return true;
8508 return false;
8511 /* If the operand to an ABS_EXPR is >= 0, then eliminate the
8512 ABS_EXPR. If the operand is <= 0, then simplify the
8513 ABS_EXPR into a NEGATE_EXPR. */
8515 static bool
8516 simplify_abs_using_ranges (gimple stmt)
8518 tree val = NULL;
8519 tree op = gimple_assign_rhs1 (stmt);
8520 tree type = TREE_TYPE (op);
8521 value_range_t *vr = get_value_range (op);
8523 if (TYPE_UNSIGNED (type))
8525 val = integer_zero_node;
8527 else if (vr)
8529 bool sop = false;
8531 val = compare_range_with_value (LE_EXPR, vr, integer_zero_node, &sop);
8532 if (!val)
8534 sop = false;
8535 val = compare_range_with_value (GE_EXPR, vr, integer_zero_node,
8536 &sop);
8538 if (val)
8540 if (integer_zerop (val))
8541 val = integer_one_node;
8542 else if (integer_onep (val))
8543 val = integer_zero_node;
8547 if (val
8548 && (integer_onep (val) || integer_zerop (val)))
8550 if (sop && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC))
8552 location_t location;
8554 if (!gimple_has_location (stmt))
8555 location = input_location;
8556 else
8557 location = gimple_location (stmt);
8558 warning_at (location, OPT_Wstrict_overflow,
8559 "assuming signed overflow does not occur when "
8560 "simplifying %<abs (X)%> to %<X%> or %<-X%>");
8563 gimple_assign_set_rhs1 (stmt, op);
8564 if (integer_onep (val))
8565 gimple_assign_set_rhs_code (stmt, NEGATE_EXPR);
8566 else
8567 gimple_assign_set_rhs_code (stmt, SSA_NAME);
8568 update_stmt (stmt);
8569 return true;
8573 return false;
8576 /* Optimize away redundant BIT_AND_EXPR and BIT_IOR_EXPR.
8577 If all the bits that are being cleared by & are already
8578 known to be zero from VR, or all the bits that are being
8579 set by | are already known to be one from VR, the bit
8580 operation is redundant. */
8582 static bool
8583 simplify_bit_ops_using_ranges (gimple_stmt_iterator *gsi, gimple stmt)
8585 tree op0 = gimple_assign_rhs1 (stmt);
8586 tree op1 = gimple_assign_rhs2 (stmt);
8587 tree op = NULL_TREE;
8588 value_range_t vr0 = VR_INITIALIZER;
8589 value_range_t vr1 = VR_INITIALIZER;
8590 double_int may_be_nonzero0, may_be_nonzero1;
8591 double_int must_be_nonzero0, must_be_nonzero1;
8592 double_int mask;
8594 if (TREE_CODE (op0) == SSA_NAME)
8595 vr0 = *(get_value_range (op0));
8596 else if (is_gimple_min_invariant (op0))
8597 set_value_range_to_value (&vr0, op0, NULL);
8598 else
8599 return false;
8601 if (TREE_CODE (op1) == SSA_NAME)
8602 vr1 = *(get_value_range (op1));
8603 else if (is_gimple_min_invariant (op1))
8604 set_value_range_to_value (&vr1, op1, NULL);
8605 else
8606 return false;
8608 if (!zero_nonzero_bits_from_vr (&vr0, &may_be_nonzero0, &must_be_nonzero0))
8609 return false;
8610 if (!zero_nonzero_bits_from_vr (&vr1, &may_be_nonzero1, &must_be_nonzero1))
8611 return false;
8613 switch (gimple_assign_rhs_code (stmt))
8615 case BIT_AND_EXPR:
8616 mask = may_be_nonzero0.and_not (must_be_nonzero1);
8617 if (mask.is_zero ())
8619 op = op0;
8620 break;
8622 mask = may_be_nonzero1.and_not (must_be_nonzero0);
8623 if (mask.is_zero ())
8625 op = op1;
8626 break;
8628 break;
8629 case BIT_IOR_EXPR:
8630 mask = may_be_nonzero0.and_not (must_be_nonzero1);
8631 if (mask.is_zero ())
8633 op = op1;
8634 break;
8636 mask = may_be_nonzero1.and_not (must_be_nonzero0);
8637 if (mask.is_zero ())
8639 op = op0;
8640 break;
8642 break;
8643 default:
8644 gcc_unreachable ();
8647 if (op == NULL_TREE)
8648 return false;
8650 gimple_assign_set_rhs_with_ops (gsi, TREE_CODE (op), op, NULL);
8651 update_stmt (gsi_stmt (*gsi));
8652 return true;
8655 /* We are comparing trees OP0 and OP1 using COND_CODE. OP0 has
8656 a known value range VR.
8658 If there is one and only one value which will satisfy the
8659 conditional, then return that value. Else return NULL. */
8661 static tree
8662 test_for_singularity (enum tree_code cond_code, tree op0,
8663 tree op1, value_range_t *vr)
8665 tree min = NULL;
8666 tree max = NULL;
8668 /* Extract minimum/maximum values which satisfy the
8669 the conditional as it was written. */
8670 if (cond_code == LE_EXPR || cond_code == LT_EXPR)
8672 /* This should not be negative infinity; there is no overflow
8673 here. */
8674 min = TYPE_MIN_VALUE (TREE_TYPE (op0));
8676 max = op1;
8677 if (cond_code == LT_EXPR && !is_overflow_infinity (max))
8679 tree one = build_int_cst (TREE_TYPE (op0), 1);
8680 max = fold_build2 (MINUS_EXPR, TREE_TYPE (op0), max, one);
8681 if (EXPR_P (max))
8682 TREE_NO_WARNING (max) = 1;
8685 else if (cond_code == GE_EXPR || cond_code == GT_EXPR)
8687 /* This should not be positive infinity; there is no overflow
8688 here. */
8689 max = TYPE_MAX_VALUE (TREE_TYPE (op0));
8691 min = op1;
8692 if (cond_code == GT_EXPR && !is_overflow_infinity (min))
8694 tree one = build_int_cst (TREE_TYPE (op0), 1);
8695 min = fold_build2 (PLUS_EXPR, TREE_TYPE (op0), min, one);
8696 if (EXPR_P (min))
8697 TREE_NO_WARNING (min) = 1;
8701 /* Now refine the minimum and maximum values using any
8702 value range information we have for op0. */
8703 if (min && max)
8705 if (compare_values (vr->min, min) == 1)
8706 min = vr->min;
8707 if (compare_values (vr->max, max) == -1)
8708 max = vr->max;
8710 /* If the new min/max values have converged to a single value,
8711 then there is only one value which can satisfy the condition,
8712 return that value. */
8713 if (operand_equal_p (min, max, 0) && is_gimple_min_invariant (min))
8714 return min;
8716 return NULL;
8719 /* Return whether the value range *VR fits in an integer type specified
8720 by PRECISION and UNSIGNED_P. */
8722 static bool
8723 range_fits_type_p (value_range_t *vr, unsigned precision, bool unsigned_p)
8725 tree src_type;
8726 unsigned src_precision;
8727 double_int tem;
8729 /* We can only handle integral and pointer types. */
8730 src_type = TREE_TYPE (vr->min);
8731 if (!INTEGRAL_TYPE_P (src_type)
8732 && !POINTER_TYPE_P (src_type))
8733 return false;
8735 /* An extension is fine unless VR is signed and unsigned_p,
8736 and so is an identity transform. */
8737 src_precision = TYPE_PRECISION (TREE_TYPE (vr->min));
8738 if ((src_precision < precision
8739 && !(unsigned_p && !TYPE_UNSIGNED (src_type)))
8740 || (src_precision == precision
8741 && TYPE_UNSIGNED (src_type) == unsigned_p))
8742 return true;
8744 /* Now we can only handle ranges with constant bounds. */
8745 if (vr->type != VR_RANGE
8746 || TREE_CODE (vr->min) != INTEGER_CST
8747 || TREE_CODE (vr->max) != INTEGER_CST)
8748 return false;
8750 /* For sign changes, the MSB of the double_int has to be clear.
8751 An unsigned value with its MSB set cannot be represented by
8752 a signed double_int, while a negative value cannot be represented
8753 by an unsigned double_int. */
8754 if (TYPE_UNSIGNED (src_type) != unsigned_p
8755 && (TREE_INT_CST_HIGH (vr->min) | TREE_INT_CST_HIGH (vr->max)) < 0)
8756 return false;
8758 /* Then we can perform the conversion on both ends and compare
8759 the result for equality. */
8760 tem = tree_to_double_int (vr->min).ext (precision, unsigned_p);
8761 if (tree_to_double_int (vr->min) != tem)
8762 return false;
8763 tem = tree_to_double_int (vr->max).ext (precision, unsigned_p);
8764 if (tree_to_double_int (vr->max) != tem)
8765 return false;
8767 return true;
8770 /* Simplify a conditional using a relational operator to an equality
8771 test if the range information indicates only one value can satisfy
8772 the original conditional. */
8774 static bool
8775 simplify_cond_using_ranges (gimple stmt)
8777 tree op0 = gimple_cond_lhs (stmt);
8778 tree op1 = gimple_cond_rhs (stmt);
8779 enum tree_code cond_code = gimple_cond_code (stmt);
8781 if (cond_code != NE_EXPR
8782 && cond_code != EQ_EXPR
8783 && TREE_CODE (op0) == SSA_NAME
8784 && INTEGRAL_TYPE_P (TREE_TYPE (op0))
8785 && is_gimple_min_invariant (op1))
8787 value_range_t *vr = get_value_range (op0);
8789 /* If we have range information for OP0, then we might be
8790 able to simplify this conditional. */
8791 if (vr->type == VR_RANGE)
8793 tree new_tree = test_for_singularity (cond_code, op0, op1, vr);
8795 if (new_tree)
8797 if (dump_file)
8799 fprintf (dump_file, "Simplified relational ");
8800 print_gimple_stmt (dump_file, stmt, 0, 0);
8801 fprintf (dump_file, " into ");
8804 gimple_cond_set_code (stmt, EQ_EXPR);
8805 gimple_cond_set_lhs (stmt, op0);
8806 gimple_cond_set_rhs (stmt, new_tree);
8808 update_stmt (stmt);
8810 if (dump_file)
8812 print_gimple_stmt (dump_file, stmt, 0, 0);
8813 fprintf (dump_file, "\n");
8816 return true;
8819 /* Try again after inverting the condition. We only deal
8820 with integral types here, so no need to worry about
8821 issues with inverting FP comparisons. */
8822 cond_code = invert_tree_comparison (cond_code, false);
8823 new_tree = test_for_singularity (cond_code, op0, op1, vr);
8825 if (new_tree)
8827 if (dump_file)
8829 fprintf (dump_file, "Simplified relational ");
8830 print_gimple_stmt (dump_file, stmt, 0, 0);
8831 fprintf (dump_file, " into ");
8834 gimple_cond_set_code (stmt, NE_EXPR);
8835 gimple_cond_set_lhs (stmt, op0);
8836 gimple_cond_set_rhs (stmt, new_tree);
8838 update_stmt (stmt);
8840 if (dump_file)
8842 print_gimple_stmt (dump_file, stmt, 0, 0);
8843 fprintf (dump_file, "\n");
8846 return true;
8851 /* If we have a comparison of an SSA_NAME (OP0) against a constant,
8852 see if OP0 was set by a type conversion where the source of
8853 the conversion is another SSA_NAME with a range that fits
8854 into the range of OP0's type.
8856 If so, the conversion is redundant as the earlier SSA_NAME can be
8857 used for the comparison directly if we just massage the constant in the
8858 comparison. */
8859 if (TREE_CODE (op0) == SSA_NAME
8860 && TREE_CODE (op1) == INTEGER_CST)
8862 gimple def_stmt = SSA_NAME_DEF_STMT (op0);
8863 tree innerop;
8865 if (!is_gimple_assign (def_stmt)
8866 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt)))
8867 return false;
8869 innerop = gimple_assign_rhs1 (def_stmt);
8871 if (TREE_CODE (innerop) == SSA_NAME
8872 && !POINTER_TYPE_P (TREE_TYPE (innerop)))
8874 value_range_t *vr = get_value_range (innerop);
8876 if (range_int_cst_p (vr)
8877 && range_fits_type_p (vr,
8878 TYPE_PRECISION (TREE_TYPE (op0)),
8879 TYPE_UNSIGNED (TREE_TYPE (op0)))
8880 && int_fits_type_p (op1, TREE_TYPE (innerop))
8881 /* The range must not have overflowed, or if it did overflow
8882 we must not be wrapping/trapping overflow and optimizing
8883 with strict overflow semantics. */
8884 && ((!is_negative_overflow_infinity (vr->min)
8885 && !is_positive_overflow_infinity (vr->max))
8886 || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (innerop))))
8888 /* If the range overflowed and the user has asked for warnings
8889 when strict overflow semantics were used to optimize code,
8890 issue an appropriate warning. */
8891 if ((is_negative_overflow_infinity (vr->min)
8892 || is_positive_overflow_infinity (vr->max))
8893 && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_CONDITIONAL))
8895 location_t location;
8897 if (!gimple_has_location (stmt))
8898 location = input_location;
8899 else
8900 location = gimple_location (stmt);
8901 warning_at (location, OPT_Wstrict_overflow,
8902 "assuming signed overflow does not occur when "
8903 "simplifying conditional");
8906 tree newconst = fold_convert (TREE_TYPE (innerop), op1);
8907 gimple_cond_set_lhs (stmt, innerop);
8908 gimple_cond_set_rhs (stmt, newconst);
8909 return true;
8914 return false;
8917 /* Simplify a switch statement using the value range of the switch
8918 argument. */
8920 static bool
8921 simplify_switch_using_ranges (gimple stmt)
8923 tree op = gimple_switch_index (stmt);
8924 value_range_t *vr;
8925 bool take_default;
8926 edge e;
8927 edge_iterator ei;
8928 size_t i = 0, j = 0, n, n2;
8929 tree vec2;
8930 switch_update su;
8931 size_t k = 1, l = 0;
8933 if (TREE_CODE (op) == SSA_NAME)
8935 vr = get_value_range (op);
8937 /* We can only handle integer ranges. */
8938 if ((vr->type != VR_RANGE
8939 && vr->type != VR_ANTI_RANGE)
8940 || symbolic_range_p (vr))
8941 return false;
8943 /* Find case label for min/max of the value range. */
8944 take_default = !find_case_label_ranges (stmt, vr, &i, &j, &k, &l);
8946 else if (TREE_CODE (op) == INTEGER_CST)
8948 take_default = !find_case_label_index (stmt, 1, op, &i);
8949 if (take_default)
8951 i = 1;
8952 j = 0;
8954 else
8956 j = i;
8959 else
8960 return false;
8962 n = gimple_switch_num_labels (stmt);
8964 /* Bail out if this is just all edges taken. */
8965 if (i == 1
8966 && j == n - 1
8967 && take_default)
8968 return false;
8970 /* Build a new vector of taken case labels. */
8971 vec2 = make_tree_vec (j - i + 1 + l - k + 1 + (int)take_default);
8972 n2 = 0;
8974 /* Add the default edge, if necessary. */
8975 if (take_default)
8976 TREE_VEC_ELT (vec2, n2++) = gimple_switch_default_label (stmt);
8978 for (; i <= j; ++i, ++n2)
8979 TREE_VEC_ELT (vec2, n2) = gimple_switch_label (stmt, i);
8981 for (; k <= l; ++k, ++n2)
8982 TREE_VEC_ELT (vec2, n2) = gimple_switch_label (stmt, k);
8984 /* Mark needed edges. */
8985 for (i = 0; i < n2; ++i)
8987 e = find_edge (gimple_bb (stmt),
8988 label_to_block (CASE_LABEL (TREE_VEC_ELT (vec2, i))));
8989 e->aux = (void *)-1;
8992 /* Queue not needed edges for later removal. */
8993 FOR_EACH_EDGE (e, ei, gimple_bb (stmt)->succs)
8995 if (e->aux == (void *)-1)
8997 e->aux = NULL;
8998 continue;
9001 if (dump_file && (dump_flags & TDF_DETAILS))
9003 fprintf (dump_file, "removing unreachable case label\n");
9005 to_remove_edges.safe_push (e);
9006 e->flags &= ~EDGE_EXECUTABLE;
9009 /* And queue an update for the stmt. */
9010 su.stmt = stmt;
9011 su.vec = vec2;
9012 to_update_switch_stmts.safe_push (su);
9013 return false;
9016 /* Simplify an integral conversion from an SSA name in STMT. */
9018 static bool
9019 simplify_conversion_using_ranges (gimple stmt)
9021 tree innerop, middleop, finaltype;
9022 gimple def_stmt;
9023 value_range_t *innervr;
9024 bool inner_unsigned_p, middle_unsigned_p, final_unsigned_p;
9025 unsigned inner_prec, middle_prec, final_prec;
9026 double_int innermin, innermed, innermax, middlemin, middlemed, middlemax;
9028 finaltype = TREE_TYPE (gimple_assign_lhs (stmt));
9029 if (!INTEGRAL_TYPE_P (finaltype))
9030 return false;
9031 middleop = gimple_assign_rhs1 (stmt);
9032 def_stmt = SSA_NAME_DEF_STMT (middleop);
9033 if (!is_gimple_assign (def_stmt)
9034 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt)))
9035 return false;
9036 innerop = gimple_assign_rhs1 (def_stmt);
9037 if (TREE_CODE (innerop) != SSA_NAME
9038 || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (innerop))
9039 return false;
9041 /* Get the value-range of the inner operand. */
9042 innervr = get_value_range (innerop);
9043 if (innervr->type != VR_RANGE
9044 || TREE_CODE (innervr->min) != INTEGER_CST
9045 || TREE_CODE (innervr->max) != INTEGER_CST)
9046 return false;
9048 /* Simulate the conversion chain to check if the result is equal if
9049 the middle conversion is removed. */
9050 innermin = tree_to_double_int (innervr->min);
9051 innermax = tree_to_double_int (innervr->max);
9053 inner_prec = TYPE_PRECISION (TREE_TYPE (innerop));
9054 middle_prec = TYPE_PRECISION (TREE_TYPE (middleop));
9055 final_prec = TYPE_PRECISION (finaltype);
9057 /* If the first conversion is not injective, the second must not
9058 be widening. */
9059 if ((innermax - innermin).ugt (double_int::mask (middle_prec))
9060 && middle_prec < final_prec)
9061 return false;
9062 /* We also want a medium value so that we can track the effect that
9063 narrowing conversions with sign change have. */
9064 inner_unsigned_p = TYPE_UNSIGNED (TREE_TYPE (innerop));
9065 if (inner_unsigned_p)
9066 innermed = double_int::mask (inner_prec).lrshift (1, inner_prec);
9067 else
9068 innermed = double_int_zero;
9069 if (innermin.cmp (innermed, inner_unsigned_p) >= 0
9070 || innermed.cmp (innermax, inner_unsigned_p) >= 0)
9071 innermed = innermin;
9073 middle_unsigned_p = TYPE_UNSIGNED (TREE_TYPE (middleop));
9074 middlemin = innermin.ext (middle_prec, middle_unsigned_p);
9075 middlemed = innermed.ext (middle_prec, middle_unsigned_p);
9076 middlemax = innermax.ext (middle_prec, middle_unsigned_p);
9078 /* Require that the final conversion applied to both the original
9079 and the intermediate range produces the same result. */
9080 final_unsigned_p = TYPE_UNSIGNED (finaltype);
9081 if (middlemin.ext (final_prec, final_unsigned_p)
9082 != innermin.ext (final_prec, final_unsigned_p)
9083 || middlemed.ext (final_prec, final_unsigned_p)
9084 != innermed.ext (final_prec, final_unsigned_p)
9085 || middlemax.ext (final_prec, final_unsigned_p)
9086 != innermax.ext (final_prec, final_unsigned_p))
9087 return false;
9089 gimple_assign_set_rhs1 (stmt, innerop);
9090 update_stmt (stmt);
9091 return true;
9094 /* Simplify a conversion from integral SSA name to float in STMT. */
9096 static bool
9097 simplify_float_conversion_using_ranges (gimple_stmt_iterator *gsi, gimple stmt)
9099 tree rhs1 = gimple_assign_rhs1 (stmt);
9100 value_range_t *vr = get_value_range (rhs1);
9101 enum machine_mode fltmode = TYPE_MODE (TREE_TYPE (gimple_assign_lhs (stmt)));
9102 enum machine_mode mode;
9103 tree tem;
9104 gimple conv;
9106 /* We can only handle constant ranges. */
9107 if (vr->type != VR_RANGE
9108 || TREE_CODE (vr->min) != INTEGER_CST
9109 || TREE_CODE (vr->max) != INTEGER_CST)
9110 return false;
9112 /* First check if we can use a signed type in place of an unsigned. */
9113 if (TYPE_UNSIGNED (TREE_TYPE (rhs1))
9114 && (can_float_p (fltmode, TYPE_MODE (TREE_TYPE (rhs1)), 0)
9115 != CODE_FOR_nothing)
9116 && range_fits_type_p (vr, GET_MODE_PRECISION
9117 (TYPE_MODE (TREE_TYPE (rhs1))), 0))
9118 mode = TYPE_MODE (TREE_TYPE (rhs1));
9119 /* If we can do the conversion in the current input mode do nothing. */
9120 else if (can_float_p (fltmode, TYPE_MODE (TREE_TYPE (rhs1)),
9121 TYPE_UNSIGNED (TREE_TYPE (rhs1))) != CODE_FOR_nothing)
9122 return false;
9123 /* Otherwise search for a mode we can use, starting from the narrowest
9124 integer mode available. */
9125 else
9127 mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
9130 /* If we cannot do a signed conversion to float from mode
9131 or if the value-range does not fit in the signed type
9132 try with a wider mode. */
9133 if (can_float_p (fltmode, mode, 0) != CODE_FOR_nothing
9134 && range_fits_type_p (vr, GET_MODE_PRECISION (mode), 0))
9135 break;
9137 mode = GET_MODE_WIDER_MODE (mode);
9138 /* But do not widen the input. Instead leave that to the
9139 optabs expansion code. */
9140 if (GET_MODE_PRECISION (mode) > TYPE_PRECISION (TREE_TYPE (rhs1)))
9141 return false;
9143 while (mode != VOIDmode);
9144 if (mode == VOIDmode)
9145 return false;
9148 /* It works, insert a truncation or sign-change before the
9149 float conversion. */
9150 tem = make_ssa_name (build_nonstandard_integer_type
9151 (GET_MODE_PRECISION (mode), 0), NULL);
9152 conv = gimple_build_assign_with_ops (NOP_EXPR, tem, rhs1, NULL_TREE);
9153 gsi_insert_before (gsi, conv, GSI_SAME_STMT);
9154 gimple_assign_set_rhs1 (stmt, tem);
9155 update_stmt (stmt);
9157 return true;
9160 /* Simplify STMT using ranges if possible. */
9162 static bool
9163 simplify_stmt_using_ranges (gimple_stmt_iterator *gsi)
9165 gimple stmt = gsi_stmt (*gsi);
9166 if (is_gimple_assign (stmt))
9168 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
9169 tree rhs1 = gimple_assign_rhs1 (stmt);
9171 switch (rhs_code)
9173 case EQ_EXPR:
9174 case NE_EXPR:
9175 /* Transform EQ_EXPR, NE_EXPR into BIT_XOR_EXPR or identity
9176 if the RHS is zero or one, and the LHS are known to be boolean
9177 values. */
9178 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
9179 return simplify_truth_ops_using_ranges (gsi, stmt);
9180 break;
9182 /* Transform TRUNC_DIV_EXPR and TRUNC_MOD_EXPR into RSHIFT_EXPR
9183 and BIT_AND_EXPR respectively if the first operand is greater
9184 than zero and the second operand is an exact power of two. */
9185 case TRUNC_DIV_EXPR:
9186 case TRUNC_MOD_EXPR:
9187 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1))
9188 && integer_pow2p (gimple_assign_rhs2 (stmt)))
9189 return simplify_div_or_mod_using_ranges (stmt);
9190 break;
9192 /* Transform ABS (X) into X or -X as appropriate. */
9193 case ABS_EXPR:
9194 if (TREE_CODE (rhs1) == SSA_NAME
9195 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
9196 return simplify_abs_using_ranges (stmt);
9197 break;
9199 case BIT_AND_EXPR:
9200 case BIT_IOR_EXPR:
9201 /* Optimize away BIT_AND_EXPR and BIT_IOR_EXPR
9202 if all the bits being cleared are already cleared or
9203 all the bits being set are already set. */
9204 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
9205 return simplify_bit_ops_using_ranges (gsi, stmt);
9206 break;
9208 CASE_CONVERT:
9209 if (TREE_CODE (rhs1) == SSA_NAME
9210 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
9211 return simplify_conversion_using_ranges (stmt);
9212 break;
9214 case FLOAT_EXPR:
9215 if (TREE_CODE (rhs1) == SSA_NAME
9216 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
9217 return simplify_float_conversion_using_ranges (gsi, stmt);
9218 break;
9220 default:
9221 break;
9224 else if (gimple_code (stmt) == GIMPLE_COND)
9225 return simplify_cond_using_ranges (stmt);
9226 else if (gimple_code (stmt) == GIMPLE_SWITCH)
9227 return simplify_switch_using_ranges (stmt);
9229 return false;
9232 /* If the statement pointed by SI has a predicate whose value can be
9233 computed using the value range information computed by VRP, compute
9234 its value and return true. Otherwise, return false. */
9236 static bool
9237 fold_predicate_in (gimple_stmt_iterator *si)
9239 bool assignment_p = false;
9240 tree val;
9241 gimple stmt = gsi_stmt (*si);
9243 if (is_gimple_assign (stmt)
9244 && TREE_CODE_CLASS (gimple_assign_rhs_code (stmt)) == tcc_comparison)
9246 assignment_p = true;
9247 val = vrp_evaluate_conditional (gimple_assign_rhs_code (stmt),
9248 gimple_assign_rhs1 (stmt),
9249 gimple_assign_rhs2 (stmt),
9250 stmt);
9252 else if (gimple_code (stmt) == GIMPLE_COND)
9253 val = vrp_evaluate_conditional (gimple_cond_code (stmt),
9254 gimple_cond_lhs (stmt),
9255 gimple_cond_rhs (stmt),
9256 stmt);
9257 else
9258 return false;
9260 if (val)
9262 if (assignment_p)
9263 val = fold_convert (gimple_expr_type (stmt), val);
9265 if (dump_file)
9267 fprintf (dump_file, "Folding predicate ");
9268 print_gimple_expr (dump_file, stmt, 0, 0);
9269 fprintf (dump_file, " to ");
9270 print_generic_expr (dump_file, val, 0);
9271 fprintf (dump_file, "\n");
9274 if (is_gimple_assign (stmt))
9275 gimple_assign_set_rhs_from_tree (si, val);
9276 else
9278 gcc_assert (gimple_code (stmt) == GIMPLE_COND);
9279 if (integer_zerop (val))
9280 gimple_cond_make_false (stmt);
9281 else if (integer_onep (val))
9282 gimple_cond_make_true (stmt);
9283 else
9284 gcc_unreachable ();
9287 return true;
9290 return false;
9293 /* Callback for substitute_and_fold folding the stmt at *SI. */
9295 static bool
9296 vrp_fold_stmt (gimple_stmt_iterator *si)
9298 if (fold_predicate_in (si))
9299 return true;
9301 return simplify_stmt_using_ranges (si);
9304 /* Stack of dest,src equivalency pairs that need to be restored after
9305 each attempt to thread a block's incoming edge to an outgoing edge.
9307 A NULL entry is used to mark the end of pairs which need to be
9308 restored. */
9309 static vec<tree> equiv_stack;
9311 /* A trivial wrapper so that we can present the generic jump threading
9312 code with a simple API for simplifying statements. STMT is the
9313 statement we want to simplify, WITHIN_STMT provides the location
9314 for any overflow warnings. */
9316 static tree
9317 simplify_stmt_for_jump_threading (gimple stmt, gimple within_stmt)
9319 if (gimple_code (stmt) == GIMPLE_COND)
9320 return vrp_evaluate_conditional (gimple_cond_code (stmt),
9321 gimple_cond_lhs (stmt),
9322 gimple_cond_rhs (stmt), within_stmt);
9324 if (gimple_code (stmt) == GIMPLE_ASSIGN)
9326 value_range_t new_vr = VR_INITIALIZER;
9327 tree lhs = gimple_assign_lhs (stmt);
9329 if (TREE_CODE (lhs) == SSA_NAME
9330 && (INTEGRAL_TYPE_P (TREE_TYPE (lhs))
9331 || POINTER_TYPE_P (TREE_TYPE (lhs))))
9333 extract_range_from_assignment (&new_vr, stmt);
9334 if (range_int_cst_singleton_p (&new_vr))
9335 return new_vr.min;
9339 return NULL_TREE;
9342 /* Blocks which have more than one predecessor and more than
9343 one successor present jump threading opportunities, i.e.,
9344 when the block is reached from a specific predecessor, we
9345 may be able to determine which of the outgoing edges will
9346 be traversed. When this optimization applies, we are able
9347 to avoid conditionals at runtime and we may expose secondary
9348 optimization opportunities.
9350 This routine is effectively a driver for the generic jump
9351 threading code. It basically just presents the generic code
9352 with edges that may be suitable for jump threading.
9354 Unlike DOM, we do not iterate VRP if jump threading was successful.
9355 While iterating may expose new opportunities for VRP, it is expected
9356 those opportunities would be very limited and the compile time cost
9357 to expose those opportunities would be significant.
9359 As jump threading opportunities are discovered, they are registered
9360 for later realization. */
9362 static void
9363 identify_jump_threads (void)
9365 basic_block bb;
9366 gimple dummy;
9367 int i;
9368 edge e;
9370 /* Ugh. When substituting values earlier in this pass we can
9371 wipe the dominance information. So rebuild the dominator
9372 information as we need it within the jump threading code. */
9373 calculate_dominance_info (CDI_DOMINATORS);
9375 /* We do not allow VRP information to be used for jump threading
9376 across a back edge in the CFG. Otherwise it becomes too
9377 difficult to avoid eliminating loop exit tests. Of course
9378 EDGE_DFS_BACK is not accurate at this time so we have to
9379 recompute it. */
9380 mark_dfs_back_edges ();
9382 /* Do not thread across edges we are about to remove. Just marking
9383 them as EDGE_DFS_BACK will do. */
9384 FOR_EACH_VEC_ELT (to_remove_edges, i, e)
9385 e->flags |= EDGE_DFS_BACK;
9387 /* Allocate our unwinder stack to unwind any temporary equivalences
9388 that might be recorded. */
9389 equiv_stack.create (20);
9391 /* To avoid lots of silly node creation, we create a single
9392 conditional and just modify it in-place when attempting to
9393 thread jumps. */
9394 dummy = gimple_build_cond (EQ_EXPR,
9395 integer_zero_node, integer_zero_node,
9396 NULL, NULL);
9398 /* Walk through all the blocks finding those which present a
9399 potential jump threading opportunity. We could set this up
9400 as a dominator walker and record data during the walk, but
9401 I doubt it's worth the effort for the classes of jump
9402 threading opportunities we are trying to identify at this
9403 point in compilation. */
9404 FOR_EACH_BB (bb)
9406 gimple last;
9408 /* If the generic jump threading code does not find this block
9409 interesting, then there is nothing to do. */
9410 if (! potentially_threadable_block (bb))
9411 continue;
9413 /* We only care about blocks ending in a COND_EXPR. While there
9414 may be some value in handling SWITCH_EXPR here, I doubt it's
9415 terribly important. */
9416 last = gsi_stmt (gsi_last_bb (bb));
9418 /* We're basically looking for a switch or any kind of conditional with
9419 integral or pointer type arguments. Note the type of the second
9420 argument will be the same as the first argument, so no need to
9421 check it explicitly. */
9422 if (gimple_code (last) == GIMPLE_SWITCH
9423 || (gimple_code (last) == GIMPLE_COND
9424 && TREE_CODE (gimple_cond_lhs (last)) == SSA_NAME
9425 && (INTEGRAL_TYPE_P (TREE_TYPE (gimple_cond_lhs (last)))
9426 || POINTER_TYPE_P (TREE_TYPE (gimple_cond_lhs (last))))
9427 && (TREE_CODE (gimple_cond_rhs (last)) == SSA_NAME
9428 || is_gimple_min_invariant (gimple_cond_rhs (last)))))
9430 edge_iterator ei;
9432 /* We've got a block with multiple predecessors and multiple
9433 successors which also ends in a suitable conditional or
9434 switch statement. For each predecessor, see if we can thread
9435 it to a specific successor. */
9436 FOR_EACH_EDGE (e, ei, bb->preds)
9438 /* Do not thread across back edges or abnormal edges
9439 in the CFG. */
9440 if (e->flags & (EDGE_DFS_BACK | EDGE_COMPLEX))
9441 continue;
9443 thread_across_edge (dummy, e, true, &equiv_stack,
9444 simplify_stmt_for_jump_threading);
9449 /* We do not actually update the CFG or SSA graphs at this point as
9450 ASSERT_EXPRs are still in the IL and cfg cleanup code does not yet
9451 handle ASSERT_EXPRs gracefully. */
9454 /* We identified all the jump threading opportunities earlier, but could
9455 not transform the CFG at that time. This routine transforms the
9456 CFG and arranges for the dominator tree to be rebuilt if necessary.
9458 Note the SSA graph update will occur during the normal TODO
9459 processing by the pass manager. */
9460 static void
9461 finalize_jump_threads (void)
9463 thread_through_all_blocks (false);
9464 equiv_stack.release ();
9468 /* Traverse all the blocks folding conditionals with known ranges. */
9470 static void
9471 vrp_finalize (void)
9473 size_t i;
9475 values_propagated = true;
9477 if (dump_file)
9479 fprintf (dump_file, "\nValue ranges after VRP:\n\n");
9480 dump_all_value_ranges (dump_file);
9481 fprintf (dump_file, "\n");
9484 substitute_and_fold (op_with_constant_singleton_value_range,
9485 vrp_fold_stmt, false);
9487 if (warn_array_bounds)
9488 check_all_array_refs ();
9490 /* We must identify jump threading opportunities before we release
9491 the datastructures built by VRP. */
9492 identify_jump_threads ();
9494 /* Set value range to non pointer SSA_NAMEs. */
9495 for (i = 0; i < num_vr_values; i++)
9496 if (vr_value[i])
9498 tree name = ssa_name (i);
9500 if (!name
9501 || POINTER_TYPE_P (TREE_TYPE (name))
9502 || (vr_value[i]->type == VR_VARYING)
9503 || (vr_value[i]->type == VR_UNDEFINED))
9504 continue;
9506 if ((TREE_CODE (vr_value[i]->min) == INTEGER_CST)
9507 && (TREE_CODE (vr_value[i]->max) == INTEGER_CST))
9509 if (vr_value[i]->type == VR_RANGE)
9510 set_range_info (name,
9511 tree_to_double_int (vr_value[i]->min),
9512 tree_to_double_int (vr_value[i]->max));
9513 else if (vr_value[i]->type == VR_ANTI_RANGE)
9515 /* VR_ANTI_RANGE ~[min, max] is encoded compactly as
9516 [max + 1, min - 1] without additional attributes.
9517 When min value > max value, we know that it is
9518 VR_ANTI_RANGE; it is VR_RANGE otherwise. */
9520 /* ~[0,0] anti-range is represented as
9521 range. */
9522 if (TYPE_UNSIGNED (TREE_TYPE (name))
9523 && integer_zerop (vr_value[i]->min)
9524 && integer_zerop (vr_value[i]->max))
9525 set_range_info (name,
9526 double_int_one,
9527 double_int::max_value
9528 (TYPE_PRECISION (TREE_TYPE (name)), true));
9529 else
9530 set_range_info (name,
9531 tree_to_double_int (vr_value[i]->max)
9532 + double_int_one,
9533 tree_to_double_int (vr_value[i]->min)
9534 - double_int_one);
9539 /* Free allocated memory. */
9540 for (i = 0; i < num_vr_values; i++)
9541 if (vr_value[i])
9543 BITMAP_FREE (vr_value[i]->equiv);
9544 free (vr_value[i]);
9547 free (vr_value);
9548 free (vr_phi_edge_counts);
9550 /* So that we can distinguish between VRP data being available
9551 and not available. */
9552 vr_value = NULL;
9553 vr_phi_edge_counts = NULL;
9557 /* Main entry point to VRP (Value Range Propagation). This pass is
9558 loosely based on J. R. C. Patterson, ``Accurate Static Branch
9559 Prediction by Value Range Propagation,'' in SIGPLAN Conference on
9560 Programming Language Design and Implementation, pp. 67-78, 1995.
9561 Also available at http://citeseer.ist.psu.edu/patterson95accurate.html
9563 This is essentially an SSA-CCP pass modified to deal with ranges
9564 instead of constants.
9566 While propagating ranges, we may find that two or more SSA name
9567 have equivalent, though distinct ranges. For instance,
9569 1 x_9 = p_3->a;
9570 2 p_4 = ASSERT_EXPR <p_3, p_3 != 0>
9571 3 if (p_4 == q_2)
9572 4 p_5 = ASSERT_EXPR <p_4, p_4 == q_2>;
9573 5 endif
9574 6 if (q_2)
9576 In the code above, pointer p_5 has range [q_2, q_2], but from the
9577 code we can also determine that p_5 cannot be NULL and, if q_2 had
9578 a non-varying range, p_5's range should also be compatible with it.
9580 These equivalences are created by two expressions: ASSERT_EXPR and
9581 copy operations. Since p_5 is an assertion on p_4, and p_4 was the
9582 result of another assertion, then we can use the fact that p_5 and
9583 p_4 are equivalent when evaluating p_5's range.
9585 Together with value ranges, we also propagate these equivalences
9586 between names so that we can take advantage of information from
9587 multiple ranges when doing final replacement. Note that this
9588 equivalency relation is transitive but not symmetric.
9590 In the example above, p_5 is equivalent to p_4, q_2 and p_3, but we
9591 cannot assert that q_2 is equivalent to p_5 because q_2 may be used
9592 in contexts where that assertion does not hold (e.g., in line 6).
9594 TODO, the main difference between this pass and Patterson's is that
9595 we do not propagate edge probabilities. We only compute whether
9596 edges can be taken or not. That is, instead of having a spectrum
9597 of jump probabilities between 0 and 1, we only deal with 0, 1 and
9598 DON'T KNOW. In the future, it may be worthwhile to propagate
9599 probabilities to aid branch prediction. */
9601 static unsigned int
9602 execute_vrp (void)
9604 int i;
9605 edge e;
9606 switch_update *su;
9608 loop_optimizer_init (LOOPS_NORMAL | LOOPS_HAVE_RECORDED_EXITS);
9609 rewrite_into_loop_closed_ssa (NULL, TODO_update_ssa);
9610 scev_initialize ();
9612 /* ??? This ends up using stale EDGE_DFS_BACK for liveness computation.
9613 Inserting assertions may split edges which will invalidate
9614 EDGE_DFS_BACK. */
9615 insert_range_assertions ();
9617 to_remove_edges.create (10);
9618 to_update_switch_stmts.create (5);
9619 threadedge_initialize_values ();
9621 /* For visiting PHI nodes we need EDGE_DFS_BACK computed. */
9622 mark_dfs_back_edges ();
9624 vrp_initialize ();
9625 ssa_propagate (vrp_visit_stmt, vrp_visit_phi_node);
9626 vrp_finalize ();
9628 free_numbers_of_iterations_estimates ();
9630 /* ASSERT_EXPRs must be removed before finalizing jump threads
9631 as finalizing jump threads calls the CFG cleanup code which
9632 does not properly handle ASSERT_EXPRs. */
9633 remove_range_assertions ();
9635 /* If we exposed any new variables, go ahead and put them into
9636 SSA form now, before we handle jump threading. This simplifies
9637 interactions between rewriting of _DECL nodes into SSA form
9638 and rewriting SSA_NAME nodes into SSA form after block
9639 duplication and CFG manipulation. */
9640 update_ssa (TODO_update_ssa);
9642 finalize_jump_threads ();
9644 /* Remove dead edges from SWITCH_EXPR optimization. This leaves the
9645 CFG in a broken state and requires a cfg_cleanup run. */
9646 FOR_EACH_VEC_ELT (to_remove_edges, i, e)
9647 remove_edge (e);
9648 /* Update SWITCH_EXPR case label vector. */
9649 FOR_EACH_VEC_ELT (to_update_switch_stmts, i, su)
9651 size_t j;
9652 size_t n = TREE_VEC_LENGTH (su->vec);
9653 tree label;
9654 gimple_switch_set_num_labels (su->stmt, n);
9655 for (j = 0; j < n; j++)
9656 gimple_switch_set_label (su->stmt, j, TREE_VEC_ELT (su->vec, j));
9657 /* As we may have replaced the default label with a regular one
9658 make sure to make it a real default label again. This ensures
9659 optimal expansion. */
9660 label = gimple_switch_label (su->stmt, 0);
9661 CASE_LOW (label) = NULL_TREE;
9662 CASE_HIGH (label) = NULL_TREE;
9665 if (to_remove_edges.length () > 0)
9667 free_dominance_info (CDI_DOMINATORS);
9668 if (current_loops)
9669 loops_state_set (LOOPS_NEED_FIXUP);
9672 to_remove_edges.release ();
9673 to_update_switch_stmts.release ();
9674 threadedge_finalize_values ();
9676 scev_finalize ();
9677 loop_optimizer_finalize ();
9678 return 0;
9681 static bool
9682 gate_vrp (void)
9684 return flag_tree_vrp != 0;
9687 namespace {
9689 const pass_data pass_data_vrp =
9691 GIMPLE_PASS, /* type */
9692 "vrp", /* name */
9693 OPTGROUP_NONE, /* optinfo_flags */
9694 true, /* has_gate */
9695 true, /* has_execute */
9696 TV_TREE_VRP, /* tv_id */
9697 PROP_ssa, /* properties_required */
9698 0, /* properties_provided */
9699 0, /* properties_destroyed */
9700 0, /* todo_flags_start */
9701 ( TODO_cleanup_cfg | TODO_update_ssa
9702 | TODO_verify_ssa
9703 | TODO_verify_flow ), /* todo_flags_finish */
9706 class pass_vrp : public gimple_opt_pass
9708 public:
9709 pass_vrp (gcc::context *ctxt)
9710 : gimple_opt_pass (pass_data_vrp, ctxt)
9713 /* opt_pass methods: */
9714 opt_pass * clone () { return new pass_vrp (m_ctxt); }
9715 bool gate () { return gate_vrp (); }
9716 unsigned int execute () { return execute_vrp (); }
9718 }; // class pass_vrp
9720 } // anon namespace
9722 gimple_opt_pass *
9723 make_pass_vrp (gcc::context *ctxt)
9725 return new pass_vrp (ctxt);