/cp
[official-gcc.git] / gcc / tree-vrp.c
blob156a58ff5ca1385a864b97520ee54314ff48dcee
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"
45 /* Range of values that can be associated with an SSA_NAME after VRP
46 has executed. */
47 struct value_range_d
49 /* Lattice value represented by this range. */
50 enum value_range_type type;
52 /* Minimum and maximum values represented by this range. These
53 values should be interpreted as follows:
55 - If TYPE is VR_UNDEFINED or VR_VARYING then MIN and MAX must
56 be NULL.
58 - If TYPE == VR_RANGE then MIN holds the minimum value and
59 MAX holds the maximum value of the range [MIN, MAX].
61 - If TYPE == ANTI_RANGE the variable is known to NOT
62 take any values in the range [MIN, MAX]. */
63 tree min;
64 tree max;
66 /* Set of SSA names whose value ranges are equivalent to this one.
67 This set is only valid when TYPE is VR_RANGE or VR_ANTI_RANGE. */
68 bitmap equiv;
71 typedef struct value_range_d value_range_t;
73 #define VR_INITIALIZER { VR_UNDEFINED, NULL_TREE, NULL_TREE, NULL }
75 /* Set of SSA names found live during the RPO traversal of the function
76 for still active basic-blocks. */
77 static sbitmap *live;
79 /* Return true if the SSA name NAME is live on the edge E. */
81 static bool
82 live_on_edge (edge e, tree name)
84 return (live[e->dest->index]
85 && bitmap_bit_p (live[e->dest->index], SSA_NAME_VERSION (name)));
88 /* Local functions. */
89 static int compare_values (tree val1, tree val2);
90 static int compare_values_warnv (tree val1, tree val2, bool *);
91 static void vrp_meet (value_range_t *, value_range_t *);
92 static void vrp_intersect_ranges (value_range_t *, value_range_t *);
93 static tree vrp_evaluate_conditional_warnv_with_ops (enum tree_code,
94 tree, tree, bool, bool *,
95 bool *);
97 /* Location information for ASSERT_EXPRs. Each instance of this
98 structure describes an ASSERT_EXPR for an SSA name. Since a single
99 SSA name may have more than one assertion associated with it, these
100 locations are kept in a linked list attached to the corresponding
101 SSA name. */
102 struct assert_locus_d
104 /* Basic block where the assertion would be inserted. */
105 basic_block bb;
107 /* Some assertions need to be inserted on an edge (e.g., assertions
108 generated by COND_EXPRs). In those cases, BB will be NULL. */
109 edge e;
111 /* Pointer to the statement that generated this assertion. */
112 gimple_stmt_iterator si;
114 /* Predicate code for the ASSERT_EXPR. Must be COMPARISON_CLASS_P. */
115 enum tree_code comp_code;
117 /* Value being compared against. */
118 tree val;
120 /* Expression to compare. */
121 tree expr;
123 /* Next node in the linked list. */
124 struct assert_locus_d *next;
127 typedef struct assert_locus_d *assert_locus_t;
129 /* If bit I is present, it means that SSA name N_i has a list of
130 assertions that should be inserted in the IL. */
131 static bitmap need_assert_for;
133 /* Array of locations lists where to insert assertions. ASSERTS_FOR[I]
134 holds a list of ASSERT_LOCUS_T nodes that describe where
135 ASSERT_EXPRs for SSA name N_I should be inserted. */
136 static assert_locus_t *asserts_for;
138 /* Value range array. After propagation, VR_VALUE[I] holds the range
139 of values that SSA name N_I may take. */
140 static unsigned num_vr_values;
141 static value_range_t **vr_value;
142 static bool values_propagated;
144 /* For a PHI node which sets SSA name N_I, VR_COUNTS[I] holds the
145 number of executable edges we saw the last time we visited the
146 node. */
147 static int *vr_phi_edge_counts;
149 typedef struct {
150 gimple stmt;
151 tree vec;
152 } switch_update;
154 static vec<edge> to_remove_edges;
155 static vec<switch_update> to_update_switch_stmts;
158 /* Return the maximum value for TYPE. */
160 static inline tree
161 vrp_val_max (const_tree type)
163 if (!INTEGRAL_TYPE_P (type))
164 return NULL_TREE;
166 return TYPE_MAX_VALUE (type);
169 /* Return the minimum value for TYPE. */
171 static inline tree
172 vrp_val_min (const_tree type)
174 if (!INTEGRAL_TYPE_P (type))
175 return NULL_TREE;
177 return TYPE_MIN_VALUE (type);
180 /* Return whether VAL is equal to the maximum value of its type. This
181 will be true for a positive overflow infinity. We can't do a
182 simple equality comparison with TYPE_MAX_VALUE because C typedefs
183 and Ada subtypes can produce types whose TYPE_MAX_VALUE is not ==
184 to the integer constant with the same value in the type. */
186 static inline bool
187 vrp_val_is_max (const_tree val)
189 tree type_max = vrp_val_max (TREE_TYPE (val));
190 return (val == type_max
191 || (type_max != NULL_TREE
192 && operand_equal_p (val, type_max, 0)));
195 /* Return whether VAL is equal to the minimum value of its type. This
196 will be true for a negative overflow infinity. */
198 static inline bool
199 vrp_val_is_min (const_tree val)
201 tree type_min = vrp_val_min (TREE_TYPE (val));
202 return (val == type_min
203 || (type_min != NULL_TREE
204 && operand_equal_p (val, type_min, 0)));
208 /* Return whether TYPE should use an overflow infinity distinct from
209 TYPE_{MIN,MAX}_VALUE. We use an overflow infinity value to
210 represent a signed overflow during VRP computations. An infinity
211 is distinct from a half-range, which will go from some number to
212 TYPE_{MIN,MAX}_VALUE. */
214 static inline bool
215 needs_overflow_infinity (const_tree type)
217 return INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_WRAPS (type);
220 /* Return whether TYPE can support our overflow infinity
221 representation: we use the TREE_OVERFLOW flag, which only exists
222 for constants. If TYPE doesn't support this, we don't optimize
223 cases which would require signed overflow--we drop them to
224 VARYING. */
226 static inline bool
227 supports_overflow_infinity (const_tree type)
229 tree min = vrp_val_min (type), max = vrp_val_max (type);
230 #ifdef ENABLE_CHECKING
231 gcc_assert (needs_overflow_infinity (type));
232 #endif
233 return (min != NULL_TREE
234 && CONSTANT_CLASS_P (min)
235 && max != NULL_TREE
236 && CONSTANT_CLASS_P (max));
239 /* VAL is the maximum or minimum value of a type. Return a
240 corresponding overflow infinity. */
242 static inline tree
243 make_overflow_infinity (tree val)
245 gcc_checking_assert (val != NULL_TREE && CONSTANT_CLASS_P (val));
246 val = copy_node (val);
247 TREE_OVERFLOW (val) = 1;
248 return val;
251 /* Return a negative overflow infinity for TYPE. */
253 static inline tree
254 negative_overflow_infinity (tree type)
256 gcc_checking_assert (supports_overflow_infinity (type));
257 return make_overflow_infinity (vrp_val_min (type));
260 /* Return a positive overflow infinity for TYPE. */
262 static inline tree
263 positive_overflow_infinity (tree type)
265 gcc_checking_assert (supports_overflow_infinity (type));
266 return make_overflow_infinity (vrp_val_max (type));
269 /* Return whether VAL is a negative overflow infinity. */
271 static inline bool
272 is_negative_overflow_infinity (const_tree val)
274 return (needs_overflow_infinity (TREE_TYPE (val))
275 && CONSTANT_CLASS_P (val)
276 && TREE_OVERFLOW (val)
277 && vrp_val_is_min (val));
280 /* Return whether VAL is a positive overflow infinity. */
282 static inline bool
283 is_positive_overflow_infinity (const_tree val)
285 return (needs_overflow_infinity (TREE_TYPE (val))
286 && CONSTANT_CLASS_P (val)
287 && TREE_OVERFLOW (val)
288 && vrp_val_is_max (val));
291 /* Return whether VAL is a positive or negative overflow infinity. */
293 static inline bool
294 is_overflow_infinity (const_tree val)
296 return (needs_overflow_infinity (TREE_TYPE (val))
297 && CONSTANT_CLASS_P (val)
298 && TREE_OVERFLOW (val)
299 && (vrp_val_is_min (val) || vrp_val_is_max (val)));
302 /* Return whether STMT has a constant rhs that is_overflow_infinity. */
304 static inline bool
305 stmt_overflow_infinity (gimple stmt)
307 if (is_gimple_assign (stmt)
308 && get_gimple_rhs_class (gimple_assign_rhs_code (stmt)) ==
309 GIMPLE_SINGLE_RHS)
310 return is_overflow_infinity (gimple_assign_rhs1 (stmt));
311 return false;
314 /* If VAL is now an overflow infinity, return VAL. Otherwise, return
315 the same value with TREE_OVERFLOW clear. This can be used to avoid
316 confusing a regular value with an overflow value. */
318 static inline tree
319 avoid_overflow_infinity (tree val)
321 if (!is_overflow_infinity (val))
322 return val;
324 if (vrp_val_is_max (val))
325 return vrp_val_max (TREE_TYPE (val));
326 else
328 gcc_checking_assert (vrp_val_is_min (val));
329 return vrp_val_min (TREE_TYPE (val));
334 /* Return true if ARG is marked with the nonnull attribute in the
335 current function signature. */
337 static bool
338 nonnull_arg_p (const_tree arg)
340 tree t, attrs, fntype;
341 unsigned HOST_WIDE_INT arg_num;
343 gcc_assert (TREE_CODE (arg) == PARM_DECL && POINTER_TYPE_P (TREE_TYPE (arg)));
345 /* The static chain decl is always non null. */
346 if (arg == cfun->static_chain_decl)
347 return true;
349 fntype = TREE_TYPE (current_function_decl);
350 for (attrs = TYPE_ATTRIBUTES (fntype); attrs; attrs = TREE_CHAIN (attrs))
352 attrs = lookup_attribute ("nonnull", attrs);
354 /* If "nonnull" wasn't specified, we know nothing about the argument. */
355 if (attrs == NULL_TREE)
356 return false;
358 /* If "nonnull" applies to all the arguments, then ARG is non-null. */
359 if (TREE_VALUE (attrs) == NULL_TREE)
360 return true;
362 /* Get the position number for ARG in the function signature. */
363 for (arg_num = 1, t = DECL_ARGUMENTS (current_function_decl);
365 t = DECL_CHAIN (t), arg_num++)
367 if (t == arg)
368 break;
371 gcc_assert (t == arg);
373 /* Now see if ARG_NUM is mentioned in the nonnull list. */
374 for (t = TREE_VALUE (attrs); t; t = TREE_CHAIN (t))
376 if (compare_tree_int (TREE_VALUE (t), arg_num) == 0)
377 return true;
381 return false;
385 /* Set value range VR to VR_UNDEFINED. */
387 static inline void
388 set_value_range_to_undefined (value_range_t *vr)
390 vr->type = VR_UNDEFINED;
391 vr->min = vr->max = NULL_TREE;
392 if (vr->equiv)
393 bitmap_clear (vr->equiv);
397 /* Set value range VR to VR_VARYING. */
399 static inline void
400 set_value_range_to_varying (value_range_t *vr)
402 vr->type = VR_VARYING;
403 vr->min = vr->max = NULL_TREE;
404 if (vr->equiv)
405 bitmap_clear (vr->equiv);
409 /* Set value range VR to {T, MIN, MAX, EQUIV}. */
411 static void
412 set_value_range (value_range_t *vr, enum value_range_type t, tree min,
413 tree max, bitmap equiv)
415 #if defined ENABLE_CHECKING
416 /* Check the validity of the range. */
417 if (t == VR_RANGE || t == VR_ANTI_RANGE)
419 int cmp;
421 gcc_assert (min && max);
423 if (INTEGRAL_TYPE_P (TREE_TYPE (min)) && t == VR_ANTI_RANGE)
424 gcc_assert (!vrp_val_is_min (min) || !vrp_val_is_max (max));
426 cmp = compare_values (min, max);
427 gcc_assert (cmp == 0 || cmp == -1 || cmp == -2);
429 if (needs_overflow_infinity (TREE_TYPE (min)))
430 gcc_assert (!is_overflow_infinity (min)
431 || !is_overflow_infinity (max));
434 if (t == VR_UNDEFINED || t == VR_VARYING)
435 gcc_assert (min == NULL_TREE && max == NULL_TREE);
437 if (t == VR_UNDEFINED || t == VR_VARYING)
438 gcc_assert (equiv == NULL || bitmap_empty_p (equiv));
439 #endif
441 vr->type = t;
442 vr->min = min;
443 vr->max = max;
445 /* Since updating the equivalence set involves deep copying the
446 bitmaps, only do it if absolutely necessary. */
447 if (vr->equiv == NULL
448 && equiv != NULL)
449 vr->equiv = BITMAP_ALLOC (NULL);
451 if (equiv != vr->equiv)
453 if (equiv && !bitmap_empty_p (equiv))
454 bitmap_copy (vr->equiv, equiv);
455 else
456 bitmap_clear (vr->equiv);
461 /* Set value range VR to the canonical form of {T, MIN, MAX, EQUIV}.
462 This means adjusting T, MIN and MAX representing the case of a
463 wrapping range with MAX < MIN covering [MIN, type_max] U [type_min, MAX]
464 as anti-rage ~[MAX+1, MIN-1]. Likewise for wrapping anti-ranges.
465 In corner cases where MAX+1 or MIN-1 wraps this will fall back
466 to varying.
467 This routine exists to ease canonicalization in the case where we
468 extract ranges from var + CST op limit. */
470 static void
471 set_and_canonicalize_value_range (value_range_t *vr, enum value_range_type t,
472 tree min, tree max, bitmap equiv)
474 /* Use the canonical setters for VR_UNDEFINED and VR_VARYING. */
475 if (t == VR_UNDEFINED)
477 set_value_range_to_undefined (vr);
478 return;
480 else if (t == VR_VARYING)
482 set_value_range_to_varying (vr);
483 return;
486 /* Nothing to canonicalize for symbolic ranges. */
487 if (TREE_CODE (min) != INTEGER_CST
488 || TREE_CODE (max) != INTEGER_CST)
490 set_value_range (vr, t, min, max, equiv);
491 return;
494 /* Wrong order for min and max, to swap them and the VR type we need
495 to adjust them. */
496 if (tree_int_cst_lt (max, min))
498 tree one, tmp;
500 /* For one bit precision if max < min, then the swapped
501 range covers all values, so for VR_RANGE it is varying and
502 for VR_ANTI_RANGE empty range, so drop to varying as well. */
503 if (TYPE_PRECISION (TREE_TYPE (min)) == 1)
505 set_value_range_to_varying (vr);
506 return;
509 one = build_int_cst (TREE_TYPE (min), 1);
510 tmp = int_const_binop (PLUS_EXPR, max, one);
511 max = int_const_binop (MINUS_EXPR, min, one);
512 min = tmp;
514 /* There's one corner case, if we had [C+1, C] before we now have
515 that again. But this represents an empty value range, so drop
516 to varying in this case. */
517 if (tree_int_cst_lt (max, min))
519 set_value_range_to_varying (vr);
520 return;
523 t = t == VR_RANGE ? VR_ANTI_RANGE : VR_RANGE;
526 /* Anti-ranges that can be represented as ranges should be so. */
527 if (t == VR_ANTI_RANGE)
529 bool is_min = vrp_val_is_min (min);
530 bool is_max = vrp_val_is_max (max);
532 if (is_min && is_max)
534 /* We cannot deal with empty ranges, drop to varying.
535 ??? This could be VR_UNDEFINED instead. */
536 set_value_range_to_varying (vr);
537 return;
539 else if (TYPE_PRECISION (TREE_TYPE (min)) == 1
540 && (is_min || is_max))
542 /* Non-empty boolean ranges can always be represented
543 as a singleton range. */
544 if (is_min)
545 min = max = vrp_val_max (TREE_TYPE (min));
546 else
547 min = max = vrp_val_min (TREE_TYPE (min));
548 t = VR_RANGE;
550 else if (is_min
551 /* As a special exception preserve non-null ranges. */
552 && !(TYPE_UNSIGNED (TREE_TYPE (min))
553 && integer_zerop (max)))
555 tree one = build_int_cst (TREE_TYPE (max), 1);
556 min = int_const_binop (PLUS_EXPR, max, one);
557 max = vrp_val_max (TREE_TYPE (max));
558 t = VR_RANGE;
560 else if (is_max)
562 tree one = build_int_cst (TREE_TYPE (min), 1);
563 max = int_const_binop (MINUS_EXPR, min, one);
564 min = vrp_val_min (TREE_TYPE (min));
565 t = VR_RANGE;
569 /* Drop [-INF(OVF), +INF(OVF)] to varying. */
570 if (needs_overflow_infinity (TREE_TYPE (min))
571 && is_overflow_infinity (min)
572 && is_overflow_infinity (max))
574 set_value_range_to_varying (vr);
575 return;
578 set_value_range (vr, t, min, max, equiv);
581 /* Copy value range FROM into value range TO. */
583 static inline void
584 copy_value_range (value_range_t *to, value_range_t *from)
586 set_value_range (to, from->type, from->min, from->max, from->equiv);
589 /* Set value range VR to a single value. This function is only called
590 with values we get from statements, and exists to clear the
591 TREE_OVERFLOW flag so that we don't think we have an overflow
592 infinity when we shouldn't. */
594 static inline void
595 set_value_range_to_value (value_range_t *vr, tree val, bitmap equiv)
597 gcc_assert (is_gimple_min_invariant (val));
598 val = avoid_overflow_infinity (val);
599 set_value_range (vr, VR_RANGE, val, val, equiv);
602 /* Set value range VR to a non-negative range of type TYPE.
603 OVERFLOW_INFINITY indicates whether to use an overflow infinity
604 rather than TYPE_MAX_VALUE; this should be true if we determine
605 that the range is nonnegative based on the assumption that signed
606 overflow does not occur. */
608 static inline void
609 set_value_range_to_nonnegative (value_range_t *vr, tree type,
610 bool overflow_infinity)
612 tree zero;
614 if (overflow_infinity && !supports_overflow_infinity (type))
616 set_value_range_to_varying (vr);
617 return;
620 zero = build_int_cst (type, 0);
621 set_value_range (vr, VR_RANGE, zero,
622 (overflow_infinity
623 ? positive_overflow_infinity (type)
624 : TYPE_MAX_VALUE (type)),
625 vr->equiv);
628 /* Set value range VR to a non-NULL range of type TYPE. */
630 static inline void
631 set_value_range_to_nonnull (value_range_t *vr, tree type)
633 tree zero = build_int_cst (type, 0);
634 set_value_range (vr, VR_ANTI_RANGE, zero, zero, vr->equiv);
638 /* Set value range VR to a NULL range of type TYPE. */
640 static inline void
641 set_value_range_to_null (value_range_t *vr, tree type)
643 set_value_range_to_value (vr, build_int_cst (type, 0), vr->equiv);
647 /* Set value range VR to a range of a truthvalue of type TYPE. */
649 static inline void
650 set_value_range_to_truthvalue (value_range_t *vr, tree type)
652 if (TYPE_PRECISION (type) == 1)
653 set_value_range_to_varying (vr);
654 else
655 set_value_range (vr, VR_RANGE,
656 build_int_cst (type, 0), build_int_cst (type, 1),
657 vr->equiv);
661 /* If abs (min) < abs (max), set VR to [-max, max], if
662 abs (min) >= abs (max), set VR to [-min, min]. */
664 static void
665 abs_extent_range (value_range_t *vr, tree min, tree max)
667 int cmp;
669 gcc_assert (TREE_CODE (min) == INTEGER_CST);
670 gcc_assert (TREE_CODE (max) == INTEGER_CST);
671 gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (min)));
672 gcc_assert (!TYPE_UNSIGNED (TREE_TYPE (min)));
673 min = fold_unary (ABS_EXPR, TREE_TYPE (min), min);
674 max = fold_unary (ABS_EXPR, TREE_TYPE (max), max);
675 if (TREE_OVERFLOW (min) || TREE_OVERFLOW (max))
677 set_value_range_to_varying (vr);
678 return;
680 cmp = compare_values (min, max);
681 if (cmp == -1)
682 min = fold_unary (NEGATE_EXPR, TREE_TYPE (min), max);
683 else if (cmp == 0 || cmp == 1)
685 max = min;
686 min = fold_unary (NEGATE_EXPR, TREE_TYPE (min), min);
688 else
690 set_value_range_to_varying (vr);
691 return;
693 set_and_canonicalize_value_range (vr, VR_RANGE, min, max, NULL);
697 /* Return value range information for VAR.
699 If we have no values ranges recorded (ie, VRP is not running), then
700 return NULL. Otherwise create an empty range if none existed for VAR. */
702 static value_range_t *
703 get_value_range (const_tree var)
705 static const struct value_range_d vr_const_varying
706 = { VR_VARYING, NULL_TREE, NULL_TREE, NULL };
707 value_range_t *vr;
708 tree sym;
709 unsigned ver = SSA_NAME_VERSION (var);
711 /* If we have no recorded ranges, then return NULL. */
712 if (! vr_value)
713 return NULL;
715 /* If we query the range for a new SSA name return an unmodifiable VARYING.
716 We should get here at most from the substitute-and-fold stage which
717 will never try to change values. */
718 if (ver >= num_vr_values)
719 return CONST_CAST (value_range_t *, &vr_const_varying);
721 vr = vr_value[ver];
722 if (vr)
723 return vr;
725 /* After propagation finished do not allocate new value-ranges. */
726 if (values_propagated)
727 return CONST_CAST (value_range_t *, &vr_const_varying);
729 /* Create a default value range. */
730 vr_value[ver] = vr = XCNEW (value_range_t);
732 /* Defer allocating the equivalence set. */
733 vr->equiv = NULL;
735 /* If VAR is a default definition of a parameter, the variable can
736 take any value in VAR's type. */
737 if (SSA_NAME_IS_DEFAULT_DEF (var))
739 sym = SSA_NAME_VAR (var);
740 if (TREE_CODE (sym) == PARM_DECL)
742 /* Try to use the "nonnull" attribute to create ~[0, 0]
743 anti-ranges for pointers. Note that this is only valid with
744 default definitions of PARM_DECLs. */
745 if (POINTER_TYPE_P (TREE_TYPE (sym))
746 && nonnull_arg_p (sym))
747 set_value_range_to_nonnull (vr, TREE_TYPE (sym));
748 else
749 set_value_range_to_varying (vr);
751 else if (TREE_CODE (sym) == RESULT_DECL
752 && DECL_BY_REFERENCE (sym))
753 set_value_range_to_nonnull (vr, TREE_TYPE (sym));
756 return vr;
759 /* Return true, if VAL1 and VAL2 are equal values for VRP purposes. */
761 static inline bool
762 vrp_operand_equal_p (const_tree val1, const_tree val2)
764 if (val1 == val2)
765 return true;
766 if (!val1 || !val2 || !operand_equal_p (val1, val2, 0))
767 return false;
768 if (is_overflow_infinity (val1))
769 return is_overflow_infinity (val2);
770 return true;
773 /* Return true, if the bitmaps B1 and B2 are equal. */
775 static inline bool
776 vrp_bitmap_equal_p (const_bitmap b1, const_bitmap b2)
778 return (b1 == b2
779 || ((!b1 || bitmap_empty_p (b1))
780 && (!b2 || bitmap_empty_p (b2)))
781 || (b1 && b2
782 && bitmap_equal_p (b1, b2)));
785 /* Update the value range and equivalence set for variable VAR to
786 NEW_VR. Return true if NEW_VR is different from VAR's previous
787 value.
789 NOTE: This function assumes that NEW_VR is a temporary value range
790 object created for the sole purpose of updating VAR's range. The
791 storage used by the equivalence set from NEW_VR will be freed by
792 this function. Do not call update_value_range when NEW_VR
793 is the range object associated with another SSA name. */
795 static inline bool
796 update_value_range (const_tree var, value_range_t *new_vr)
798 value_range_t *old_vr;
799 bool is_new;
801 /* Update the value range, if necessary. */
802 old_vr = get_value_range (var);
803 is_new = old_vr->type != new_vr->type
804 || !vrp_operand_equal_p (old_vr->min, new_vr->min)
805 || !vrp_operand_equal_p (old_vr->max, new_vr->max)
806 || !vrp_bitmap_equal_p (old_vr->equiv, new_vr->equiv);
808 if (is_new)
810 /* Do not allow transitions up the lattice. The following
811 is slightly more awkward than just new_vr->type < old_vr->type
812 because VR_RANGE and VR_ANTI_RANGE need to be considered
813 the same. We may not have is_new when transitioning to
814 UNDEFINED or from VARYING. */
815 if (new_vr->type == VR_UNDEFINED
816 || old_vr->type == VR_VARYING)
817 set_value_range_to_varying (old_vr);
818 else
819 set_value_range (old_vr, new_vr->type, new_vr->min, new_vr->max,
820 new_vr->equiv);
823 BITMAP_FREE (new_vr->equiv);
825 return is_new;
829 /* Add VAR and VAR's equivalence set to EQUIV. This is the central
830 point where equivalence processing can be turned on/off. */
832 static void
833 add_equivalence (bitmap *equiv, const_tree var)
835 unsigned ver = SSA_NAME_VERSION (var);
836 value_range_t *vr = vr_value[ver];
838 if (*equiv == NULL)
839 *equiv = BITMAP_ALLOC (NULL);
840 bitmap_set_bit (*equiv, ver);
841 if (vr && vr->equiv)
842 bitmap_ior_into (*equiv, vr->equiv);
846 /* Return true if VR is ~[0, 0]. */
848 static inline bool
849 range_is_nonnull (value_range_t *vr)
851 return vr->type == VR_ANTI_RANGE
852 && integer_zerop (vr->min)
853 && integer_zerop (vr->max);
857 /* Return true if VR is [0, 0]. */
859 static inline bool
860 range_is_null (value_range_t *vr)
862 return vr->type == VR_RANGE
863 && integer_zerop (vr->min)
864 && integer_zerop (vr->max);
867 /* Return true if max and min of VR are INTEGER_CST. It's not necessary
868 a singleton. */
870 static inline bool
871 range_int_cst_p (value_range_t *vr)
873 return (vr->type == VR_RANGE
874 && TREE_CODE (vr->max) == INTEGER_CST
875 && TREE_CODE (vr->min) == INTEGER_CST);
878 /* Return true if VR is a INTEGER_CST singleton. */
880 static inline bool
881 range_int_cst_singleton_p (value_range_t *vr)
883 return (range_int_cst_p (vr)
884 && !TREE_OVERFLOW (vr->min)
885 && !TREE_OVERFLOW (vr->max)
886 && tree_int_cst_equal (vr->min, vr->max));
889 /* Return true if value range VR involves at least one symbol. */
891 static inline bool
892 symbolic_range_p (value_range_t *vr)
894 return (!is_gimple_min_invariant (vr->min)
895 || !is_gimple_min_invariant (vr->max));
898 /* Return true if value range VR uses an overflow infinity. */
900 static inline bool
901 overflow_infinity_range_p (value_range_t *vr)
903 return (vr->type == VR_RANGE
904 && (is_overflow_infinity (vr->min)
905 || is_overflow_infinity (vr->max)));
908 /* Return false if we can not make a valid comparison based on VR;
909 this will be the case if it uses an overflow infinity and overflow
910 is not undefined (i.e., -fno-strict-overflow is in effect).
911 Otherwise return true, and set *STRICT_OVERFLOW_P to true if VR
912 uses an overflow infinity. */
914 static bool
915 usable_range_p (value_range_t *vr, bool *strict_overflow_p)
917 gcc_assert (vr->type == VR_RANGE);
918 if (is_overflow_infinity (vr->min))
920 *strict_overflow_p = true;
921 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (vr->min)))
922 return false;
924 if (is_overflow_infinity (vr->max))
926 *strict_overflow_p = true;
927 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (vr->max)))
928 return false;
930 return true;
934 /* Return true if the result of assignment STMT is know to be non-negative.
935 If the return value is based on the assumption that signed overflow is
936 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
937 *STRICT_OVERFLOW_P.*/
939 static bool
940 gimple_assign_nonnegative_warnv_p (gimple stmt, bool *strict_overflow_p)
942 enum tree_code code = gimple_assign_rhs_code (stmt);
943 switch (get_gimple_rhs_class (code))
945 case GIMPLE_UNARY_RHS:
946 return tree_unary_nonnegative_warnv_p (gimple_assign_rhs_code (stmt),
947 gimple_expr_type (stmt),
948 gimple_assign_rhs1 (stmt),
949 strict_overflow_p);
950 case GIMPLE_BINARY_RHS:
951 return tree_binary_nonnegative_warnv_p (gimple_assign_rhs_code (stmt),
952 gimple_expr_type (stmt),
953 gimple_assign_rhs1 (stmt),
954 gimple_assign_rhs2 (stmt),
955 strict_overflow_p);
956 case GIMPLE_TERNARY_RHS:
957 return false;
958 case GIMPLE_SINGLE_RHS:
959 return tree_single_nonnegative_warnv_p (gimple_assign_rhs1 (stmt),
960 strict_overflow_p);
961 case GIMPLE_INVALID_RHS:
962 gcc_unreachable ();
963 default:
964 gcc_unreachable ();
968 /* Return true if return value of call STMT is know to be non-negative.
969 If the return value is based on the assumption that signed overflow is
970 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
971 *STRICT_OVERFLOW_P.*/
973 static bool
974 gimple_call_nonnegative_warnv_p (gimple stmt, bool *strict_overflow_p)
976 tree arg0 = gimple_call_num_args (stmt) > 0 ?
977 gimple_call_arg (stmt, 0) : NULL_TREE;
978 tree arg1 = gimple_call_num_args (stmt) > 1 ?
979 gimple_call_arg (stmt, 1) : NULL_TREE;
981 return tree_call_nonnegative_warnv_p (gimple_expr_type (stmt),
982 gimple_call_fndecl (stmt),
983 arg0,
984 arg1,
985 strict_overflow_p);
988 /* Return true if STMT is know to to compute a non-negative value.
989 If the return value is based on the assumption that signed overflow is
990 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
991 *STRICT_OVERFLOW_P.*/
993 static bool
994 gimple_stmt_nonnegative_warnv_p (gimple stmt, bool *strict_overflow_p)
996 switch (gimple_code (stmt))
998 case GIMPLE_ASSIGN:
999 return gimple_assign_nonnegative_warnv_p (stmt, strict_overflow_p);
1000 case GIMPLE_CALL:
1001 return gimple_call_nonnegative_warnv_p (stmt, strict_overflow_p);
1002 default:
1003 gcc_unreachable ();
1007 /* Return true if the result of assignment STMT is know to be non-zero.
1008 If the return value is based on the assumption that signed overflow is
1009 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1010 *STRICT_OVERFLOW_P.*/
1012 static bool
1013 gimple_assign_nonzero_warnv_p (gimple stmt, bool *strict_overflow_p)
1015 enum tree_code code = gimple_assign_rhs_code (stmt);
1016 switch (get_gimple_rhs_class (code))
1018 case GIMPLE_UNARY_RHS:
1019 return tree_unary_nonzero_warnv_p (gimple_assign_rhs_code (stmt),
1020 gimple_expr_type (stmt),
1021 gimple_assign_rhs1 (stmt),
1022 strict_overflow_p);
1023 case GIMPLE_BINARY_RHS:
1024 return tree_binary_nonzero_warnv_p (gimple_assign_rhs_code (stmt),
1025 gimple_expr_type (stmt),
1026 gimple_assign_rhs1 (stmt),
1027 gimple_assign_rhs2 (stmt),
1028 strict_overflow_p);
1029 case GIMPLE_TERNARY_RHS:
1030 return false;
1031 case GIMPLE_SINGLE_RHS:
1032 return tree_single_nonzero_warnv_p (gimple_assign_rhs1 (stmt),
1033 strict_overflow_p);
1034 case GIMPLE_INVALID_RHS:
1035 gcc_unreachable ();
1036 default:
1037 gcc_unreachable ();
1041 /* Return true if STMT is know to to compute a non-zero value.
1042 If the return value is based on the assumption that signed overflow is
1043 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1044 *STRICT_OVERFLOW_P.*/
1046 static bool
1047 gimple_stmt_nonzero_warnv_p (gimple stmt, bool *strict_overflow_p)
1049 switch (gimple_code (stmt))
1051 case GIMPLE_ASSIGN:
1052 return gimple_assign_nonzero_warnv_p (stmt, strict_overflow_p);
1053 case GIMPLE_CALL:
1055 tree fndecl = gimple_call_fndecl (stmt);
1056 if (!fndecl) return false;
1057 if (flag_delete_null_pointer_checks && !flag_check_new
1058 && DECL_IS_OPERATOR_NEW (fndecl)
1059 && !TREE_NOTHROW (fndecl))
1060 return true;
1061 return gimple_alloca_call_p (stmt);
1063 default:
1064 gcc_unreachable ();
1068 /* Like tree_expr_nonzero_warnv_p, but this function uses value ranges
1069 obtained so far. */
1071 static bool
1072 vrp_stmt_computes_nonzero (gimple stmt, bool *strict_overflow_p)
1074 if (gimple_stmt_nonzero_warnv_p (stmt, strict_overflow_p))
1075 return true;
1077 /* If we have an expression of the form &X->a, then the expression
1078 is nonnull if X is nonnull. */
1079 if (is_gimple_assign (stmt)
1080 && gimple_assign_rhs_code (stmt) == ADDR_EXPR)
1082 tree expr = gimple_assign_rhs1 (stmt);
1083 tree base = get_base_address (TREE_OPERAND (expr, 0));
1085 if (base != NULL_TREE
1086 && TREE_CODE (base) == MEM_REF
1087 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
1089 value_range_t *vr = get_value_range (TREE_OPERAND (base, 0));
1090 if (range_is_nonnull (vr))
1091 return true;
1095 return false;
1098 /* Returns true if EXPR is a valid value (as expected by compare_values) --
1099 a gimple invariant, or SSA_NAME +- CST. */
1101 static bool
1102 valid_value_p (tree expr)
1104 if (TREE_CODE (expr) == SSA_NAME)
1105 return true;
1107 if (TREE_CODE (expr) == PLUS_EXPR
1108 || TREE_CODE (expr) == MINUS_EXPR)
1109 return (TREE_CODE (TREE_OPERAND (expr, 0)) == SSA_NAME
1110 && TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST);
1112 return is_gimple_min_invariant (expr);
1115 /* Return
1116 1 if VAL < VAL2
1117 0 if !(VAL < VAL2)
1118 -2 if those are incomparable. */
1119 static inline int
1120 operand_less_p (tree val, tree val2)
1122 /* LT is folded faster than GE and others. Inline the common case. */
1123 if (TREE_CODE (val) == INTEGER_CST && TREE_CODE (val2) == INTEGER_CST)
1125 if (TYPE_UNSIGNED (TREE_TYPE (val)))
1126 return INT_CST_LT_UNSIGNED (val, val2);
1127 else
1129 if (INT_CST_LT (val, val2))
1130 return 1;
1133 else
1135 tree tcmp;
1137 fold_defer_overflow_warnings ();
1139 tcmp = fold_binary_to_constant (LT_EXPR, boolean_type_node, val, val2);
1141 fold_undefer_and_ignore_overflow_warnings ();
1143 if (!tcmp
1144 || TREE_CODE (tcmp) != INTEGER_CST)
1145 return -2;
1147 if (!integer_zerop (tcmp))
1148 return 1;
1151 /* val >= val2, not considering overflow infinity. */
1152 if (is_negative_overflow_infinity (val))
1153 return is_negative_overflow_infinity (val2) ? 0 : 1;
1154 else if (is_positive_overflow_infinity (val2))
1155 return is_positive_overflow_infinity (val) ? 0 : 1;
1157 return 0;
1160 /* Compare two values VAL1 and VAL2. Return
1162 -2 if VAL1 and VAL2 cannot be compared at compile-time,
1163 -1 if VAL1 < VAL2,
1164 0 if VAL1 == VAL2,
1165 +1 if VAL1 > VAL2, and
1166 +2 if VAL1 != VAL2
1168 This is similar to tree_int_cst_compare but supports pointer values
1169 and values that cannot be compared at compile time.
1171 If STRICT_OVERFLOW_P is not NULL, then set *STRICT_OVERFLOW_P to
1172 true if the return value is only valid if we assume that signed
1173 overflow is undefined. */
1175 static int
1176 compare_values_warnv (tree val1, tree val2, bool *strict_overflow_p)
1178 if (val1 == val2)
1179 return 0;
1181 /* Below we rely on the fact that VAL1 and VAL2 are both pointers or
1182 both integers. */
1183 gcc_assert (POINTER_TYPE_P (TREE_TYPE (val1))
1184 == POINTER_TYPE_P (TREE_TYPE (val2)));
1185 /* Convert the two values into the same type. This is needed because
1186 sizetype causes sign extension even for unsigned types. */
1187 val2 = fold_convert (TREE_TYPE (val1), val2);
1188 STRIP_USELESS_TYPE_CONVERSION (val2);
1190 if ((TREE_CODE (val1) == SSA_NAME
1191 || TREE_CODE (val1) == PLUS_EXPR
1192 || TREE_CODE (val1) == MINUS_EXPR)
1193 && (TREE_CODE (val2) == SSA_NAME
1194 || TREE_CODE (val2) == PLUS_EXPR
1195 || TREE_CODE (val2) == MINUS_EXPR))
1197 tree n1, c1, n2, c2;
1198 enum tree_code code1, code2;
1200 /* If VAL1 and VAL2 are of the form 'NAME [+-] CST' or 'NAME',
1201 return -1 or +1 accordingly. If VAL1 and VAL2 don't use the
1202 same name, return -2. */
1203 if (TREE_CODE (val1) == SSA_NAME)
1205 code1 = SSA_NAME;
1206 n1 = val1;
1207 c1 = NULL_TREE;
1209 else
1211 code1 = TREE_CODE (val1);
1212 n1 = TREE_OPERAND (val1, 0);
1213 c1 = TREE_OPERAND (val1, 1);
1214 if (tree_int_cst_sgn (c1) == -1)
1216 if (is_negative_overflow_infinity (c1))
1217 return -2;
1218 c1 = fold_unary_to_constant (NEGATE_EXPR, TREE_TYPE (c1), c1);
1219 if (!c1)
1220 return -2;
1221 code1 = code1 == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR;
1225 if (TREE_CODE (val2) == SSA_NAME)
1227 code2 = SSA_NAME;
1228 n2 = val2;
1229 c2 = NULL_TREE;
1231 else
1233 code2 = TREE_CODE (val2);
1234 n2 = TREE_OPERAND (val2, 0);
1235 c2 = TREE_OPERAND (val2, 1);
1236 if (tree_int_cst_sgn (c2) == -1)
1238 if (is_negative_overflow_infinity (c2))
1239 return -2;
1240 c2 = fold_unary_to_constant (NEGATE_EXPR, TREE_TYPE (c2), c2);
1241 if (!c2)
1242 return -2;
1243 code2 = code2 == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR;
1247 /* Both values must use the same name. */
1248 if (n1 != n2)
1249 return -2;
1251 if (code1 == SSA_NAME
1252 && code2 == SSA_NAME)
1253 /* NAME == NAME */
1254 return 0;
1256 /* If overflow is defined we cannot simplify more. */
1257 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (val1)))
1258 return -2;
1260 if (strict_overflow_p != NULL
1261 && (code1 == SSA_NAME || !TREE_NO_WARNING (val1))
1262 && (code2 == SSA_NAME || !TREE_NO_WARNING (val2)))
1263 *strict_overflow_p = true;
1265 if (code1 == SSA_NAME)
1267 if (code2 == PLUS_EXPR)
1268 /* NAME < NAME + CST */
1269 return -1;
1270 else if (code2 == MINUS_EXPR)
1271 /* NAME > NAME - CST */
1272 return 1;
1274 else if (code1 == PLUS_EXPR)
1276 if (code2 == SSA_NAME)
1277 /* NAME + CST > NAME */
1278 return 1;
1279 else if (code2 == PLUS_EXPR)
1280 /* NAME + CST1 > NAME + CST2, if CST1 > CST2 */
1281 return compare_values_warnv (c1, c2, strict_overflow_p);
1282 else if (code2 == MINUS_EXPR)
1283 /* NAME + CST1 > NAME - CST2 */
1284 return 1;
1286 else if (code1 == MINUS_EXPR)
1288 if (code2 == SSA_NAME)
1289 /* NAME - CST < NAME */
1290 return -1;
1291 else if (code2 == PLUS_EXPR)
1292 /* NAME - CST1 < NAME + CST2 */
1293 return -1;
1294 else if (code2 == MINUS_EXPR)
1295 /* NAME - CST1 > NAME - CST2, if CST1 < CST2. Notice that
1296 C1 and C2 are swapped in the call to compare_values. */
1297 return compare_values_warnv (c2, c1, strict_overflow_p);
1300 gcc_unreachable ();
1303 /* We cannot compare non-constants. */
1304 if (!is_gimple_min_invariant (val1) || !is_gimple_min_invariant (val2))
1305 return -2;
1307 if (!POINTER_TYPE_P (TREE_TYPE (val1)))
1309 /* We cannot compare overflowed values, except for overflow
1310 infinities. */
1311 if (TREE_OVERFLOW (val1) || TREE_OVERFLOW (val2))
1313 if (strict_overflow_p != NULL)
1314 *strict_overflow_p = true;
1315 if (is_negative_overflow_infinity (val1))
1316 return is_negative_overflow_infinity (val2) ? 0 : -1;
1317 else if (is_negative_overflow_infinity (val2))
1318 return 1;
1319 else if (is_positive_overflow_infinity (val1))
1320 return is_positive_overflow_infinity (val2) ? 0 : 1;
1321 else if (is_positive_overflow_infinity (val2))
1322 return -1;
1323 return -2;
1326 return tree_int_cst_compare (val1, val2);
1328 else
1330 tree t;
1332 /* First see if VAL1 and VAL2 are not the same. */
1333 if (val1 == val2 || operand_equal_p (val1, val2, 0))
1334 return 0;
1336 /* If VAL1 is a lower address than VAL2, return -1. */
1337 if (operand_less_p (val1, val2) == 1)
1338 return -1;
1340 /* If VAL1 is a higher address than VAL2, return +1. */
1341 if (operand_less_p (val2, val1) == 1)
1342 return 1;
1344 /* If VAL1 is different than VAL2, return +2.
1345 For integer constants we either have already returned -1 or 1
1346 or they are equivalent. We still might succeed in proving
1347 something about non-trivial operands. */
1348 if (TREE_CODE (val1) != INTEGER_CST
1349 || TREE_CODE (val2) != INTEGER_CST)
1351 t = fold_binary_to_constant (NE_EXPR, boolean_type_node, val1, val2);
1352 if (t && integer_onep (t))
1353 return 2;
1356 return -2;
1360 /* Compare values like compare_values_warnv, but treat comparisons of
1361 nonconstants which rely on undefined overflow as incomparable. */
1363 static int
1364 compare_values (tree val1, tree val2)
1366 bool sop;
1367 int ret;
1369 sop = false;
1370 ret = compare_values_warnv (val1, val2, &sop);
1371 if (sop
1372 && (!is_gimple_min_invariant (val1) || !is_gimple_min_invariant (val2)))
1373 ret = -2;
1374 return ret;
1378 /* Return 1 if VAL is inside value range MIN <= VAL <= MAX,
1379 0 if VAL is not inside [MIN, MAX],
1380 -2 if we cannot tell either way.
1382 Benchmark compile/20001226-1.c compilation time after changing this
1383 function. */
1385 static inline int
1386 value_inside_range (tree val, tree min, tree max)
1388 int cmp1, cmp2;
1390 cmp1 = operand_less_p (val, min);
1391 if (cmp1 == -2)
1392 return -2;
1393 if (cmp1 == 1)
1394 return 0;
1396 cmp2 = operand_less_p (max, val);
1397 if (cmp2 == -2)
1398 return -2;
1400 return !cmp2;
1404 /* Return true if value ranges VR0 and VR1 have a non-empty
1405 intersection.
1407 Benchmark compile/20001226-1.c compilation time after changing this
1408 function.
1411 static inline bool
1412 value_ranges_intersect_p (value_range_t *vr0, value_range_t *vr1)
1414 /* The value ranges do not intersect if the maximum of the first range is
1415 less than the minimum of the second range or vice versa.
1416 When those relations are unknown, we can't do any better. */
1417 if (operand_less_p (vr0->max, vr1->min) != 0)
1418 return false;
1419 if (operand_less_p (vr1->max, vr0->min) != 0)
1420 return false;
1421 return true;
1425 /* Return 1 if [MIN, MAX] includes the value zero, 0 if it does not
1426 include the value zero, -2 if we cannot tell. */
1428 static inline int
1429 range_includes_zero_p (tree min, tree max)
1431 tree zero = build_int_cst (TREE_TYPE (min), 0);
1432 return value_inside_range (zero, min, max);
1435 /* Return true if *VR is know to only contain nonnegative values. */
1437 static inline bool
1438 value_range_nonnegative_p (value_range_t *vr)
1440 /* Testing for VR_ANTI_RANGE is not useful here as any anti-range
1441 which would return a useful value should be encoded as a
1442 VR_RANGE. */
1443 if (vr->type == VR_RANGE)
1445 int result = compare_values (vr->min, integer_zero_node);
1446 return (result == 0 || result == 1);
1449 return false;
1452 /* Return true if T, an SSA_NAME, is known to be nonnegative. Return
1453 false otherwise or if no value range information is available. */
1455 bool
1456 ssa_name_nonnegative_p (const_tree t)
1458 value_range_t *vr = get_value_range (t);
1460 if (INTEGRAL_TYPE_P (t)
1461 && TYPE_UNSIGNED (t))
1462 return true;
1464 if (!vr)
1465 return false;
1467 return value_range_nonnegative_p (vr);
1470 /* If *VR has a value rante that is a single constant value return that,
1471 otherwise return NULL_TREE. */
1473 static tree
1474 value_range_constant_singleton (value_range_t *vr)
1476 if (vr->type == VR_RANGE
1477 && operand_equal_p (vr->min, vr->max, 0)
1478 && is_gimple_min_invariant (vr->min))
1479 return vr->min;
1481 return NULL_TREE;
1484 /* If OP has a value range with a single constant value return that,
1485 otherwise return NULL_TREE. This returns OP itself if OP is a
1486 constant. */
1488 static tree
1489 op_with_constant_singleton_value_range (tree op)
1491 if (is_gimple_min_invariant (op))
1492 return op;
1494 if (TREE_CODE (op) != SSA_NAME)
1495 return NULL_TREE;
1497 return value_range_constant_singleton (get_value_range (op));
1500 /* Return true if op is in a boolean [0, 1] value-range. */
1502 static bool
1503 op_with_boolean_value_range_p (tree op)
1505 value_range_t *vr;
1507 if (TYPE_PRECISION (TREE_TYPE (op)) == 1)
1508 return true;
1510 if (integer_zerop (op)
1511 || integer_onep (op))
1512 return true;
1514 if (TREE_CODE (op) != SSA_NAME)
1515 return false;
1517 vr = get_value_range (op);
1518 return (vr->type == VR_RANGE
1519 && integer_zerop (vr->min)
1520 && integer_onep (vr->max));
1523 /* Extract value range information from an ASSERT_EXPR EXPR and store
1524 it in *VR_P. */
1526 static void
1527 extract_range_from_assert (value_range_t *vr_p, tree expr)
1529 tree var, cond, limit, min, max, type;
1530 value_range_t *limit_vr;
1531 enum tree_code cond_code;
1533 var = ASSERT_EXPR_VAR (expr);
1534 cond = ASSERT_EXPR_COND (expr);
1536 gcc_assert (COMPARISON_CLASS_P (cond));
1538 /* Find VAR in the ASSERT_EXPR conditional. */
1539 if (var == TREE_OPERAND (cond, 0)
1540 || TREE_CODE (TREE_OPERAND (cond, 0)) == PLUS_EXPR
1541 || TREE_CODE (TREE_OPERAND (cond, 0)) == NOP_EXPR)
1543 /* If the predicate is of the form VAR COMP LIMIT, then we just
1544 take LIMIT from the RHS and use the same comparison code. */
1545 cond_code = TREE_CODE (cond);
1546 limit = TREE_OPERAND (cond, 1);
1547 cond = TREE_OPERAND (cond, 0);
1549 else
1551 /* If the predicate is of the form LIMIT COMP VAR, then we need
1552 to flip around the comparison code to create the proper range
1553 for VAR. */
1554 cond_code = swap_tree_comparison (TREE_CODE (cond));
1555 limit = TREE_OPERAND (cond, 0);
1556 cond = TREE_OPERAND (cond, 1);
1559 limit = avoid_overflow_infinity (limit);
1561 type = TREE_TYPE (var);
1562 gcc_assert (limit != var);
1564 /* For pointer arithmetic, we only keep track of pointer equality
1565 and inequality. */
1566 if (POINTER_TYPE_P (type) && cond_code != NE_EXPR && cond_code != EQ_EXPR)
1568 set_value_range_to_varying (vr_p);
1569 return;
1572 /* If LIMIT is another SSA name and LIMIT has a range of its own,
1573 try to use LIMIT's range to avoid creating symbolic ranges
1574 unnecessarily. */
1575 limit_vr = (TREE_CODE (limit) == SSA_NAME) ? get_value_range (limit) : NULL;
1577 /* LIMIT's range is only interesting if it has any useful information. */
1578 if (limit_vr
1579 && (limit_vr->type == VR_UNDEFINED
1580 || limit_vr->type == VR_VARYING
1581 || symbolic_range_p (limit_vr)))
1582 limit_vr = NULL;
1584 /* Initially, the new range has the same set of equivalences of
1585 VAR's range. This will be revised before returning the final
1586 value. Since assertions may be chained via mutually exclusive
1587 predicates, we will need to trim the set of equivalences before
1588 we are done. */
1589 gcc_assert (vr_p->equiv == NULL);
1590 add_equivalence (&vr_p->equiv, var);
1592 /* Extract a new range based on the asserted comparison for VAR and
1593 LIMIT's value range. Notice that if LIMIT has an anti-range, we
1594 will only use it for equality comparisons (EQ_EXPR). For any
1595 other kind of assertion, we cannot derive a range from LIMIT's
1596 anti-range that can be used to describe the new range. For
1597 instance, ASSERT_EXPR <x_2, x_2 <= b_4>. If b_4 is ~[2, 10],
1598 then b_4 takes on the ranges [-INF, 1] and [11, +INF]. There is
1599 no single range for x_2 that could describe LE_EXPR, so we might
1600 as well build the range [b_4, +INF] for it.
1601 One special case we handle is extracting a range from a
1602 range test encoded as (unsigned)var + CST <= limit. */
1603 if (TREE_CODE (cond) == NOP_EXPR
1604 || TREE_CODE (cond) == PLUS_EXPR)
1606 if (TREE_CODE (cond) == PLUS_EXPR)
1608 min = fold_build1 (NEGATE_EXPR, TREE_TYPE (TREE_OPERAND (cond, 1)),
1609 TREE_OPERAND (cond, 1));
1610 max = int_const_binop (PLUS_EXPR, limit, min);
1611 cond = TREE_OPERAND (cond, 0);
1613 else
1615 min = build_int_cst (TREE_TYPE (var), 0);
1616 max = limit;
1619 /* Make sure to not set TREE_OVERFLOW on the final type
1620 conversion. We are willingly interpreting large positive
1621 unsigned values as negative singed values here. */
1622 min = force_fit_type_double (TREE_TYPE (var), tree_to_double_int (min),
1623 0, false);
1624 max = force_fit_type_double (TREE_TYPE (var), tree_to_double_int (max),
1625 0, false);
1627 /* We can transform a max, min range to an anti-range or
1628 vice-versa. Use set_and_canonicalize_value_range which does
1629 this for us. */
1630 if (cond_code == LE_EXPR)
1631 set_and_canonicalize_value_range (vr_p, VR_RANGE,
1632 min, max, vr_p->equiv);
1633 else if (cond_code == GT_EXPR)
1634 set_and_canonicalize_value_range (vr_p, VR_ANTI_RANGE,
1635 min, max, vr_p->equiv);
1636 else
1637 gcc_unreachable ();
1639 else if (cond_code == EQ_EXPR)
1641 enum value_range_type range_type;
1643 if (limit_vr)
1645 range_type = limit_vr->type;
1646 min = limit_vr->min;
1647 max = limit_vr->max;
1649 else
1651 range_type = VR_RANGE;
1652 min = limit;
1653 max = limit;
1656 set_value_range (vr_p, range_type, min, max, vr_p->equiv);
1658 /* When asserting the equality VAR == LIMIT and LIMIT is another
1659 SSA name, the new range will also inherit the equivalence set
1660 from LIMIT. */
1661 if (TREE_CODE (limit) == SSA_NAME)
1662 add_equivalence (&vr_p->equiv, limit);
1664 else if (cond_code == NE_EXPR)
1666 /* As described above, when LIMIT's range is an anti-range and
1667 this assertion is an inequality (NE_EXPR), then we cannot
1668 derive anything from the anti-range. For instance, if
1669 LIMIT's range was ~[0, 0], the assertion 'VAR != LIMIT' does
1670 not imply that VAR's range is [0, 0]. So, in the case of
1671 anti-ranges, we just assert the inequality using LIMIT and
1672 not its anti-range.
1674 If LIMIT_VR is a range, we can only use it to build a new
1675 anti-range if LIMIT_VR is a single-valued range. For
1676 instance, if LIMIT_VR is [0, 1], the predicate
1677 VAR != [0, 1] does not mean that VAR's range is ~[0, 1].
1678 Rather, it means that for value 0 VAR should be ~[0, 0]
1679 and for value 1, VAR should be ~[1, 1]. We cannot
1680 represent these ranges.
1682 The only situation in which we can build a valid
1683 anti-range is when LIMIT_VR is a single-valued range
1684 (i.e., LIMIT_VR->MIN == LIMIT_VR->MAX). In that case,
1685 build the anti-range ~[LIMIT_VR->MIN, LIMIT_VR->MAX]. */
1686 if (limit_vr
1687 && limit_vr->type == VR_RANGE
1688 && compare_values (limit_vr->min, limit_vr->max) == 0)
1690 min = limit_vr->min;
1691 max = limit_vr->max;
1693 else
1695 /* In any other case, we cannot use LIMIT's range to build a
1696 valid anti-range. */
1697 min = max = limit;
1700 /* If MIN and MAX cover the whole range for their type, then
1701 just use the original LIMIT. */
1702 if (INTEGRAL_TYPE_P (type)
1703 && vrp_val_is_min (min)
1704 && vrp_val_is_max (max))
1705 min = max = limit;
1707 set_and_canonicalize_value_range (vr_p, VR_ANTI_RANGE,
1708 min, max, vr_p->equiv);
1710 else if (cond_code == LE_EXPR || cond_code == LT_EXPR)
1712 min = TYPE_MIN_VALUE (type);
1714 if (limit_vr == NULL || limit_vr->type == VR_ANTI_RANGE)
1715 max = limit;
1716 else
1718 /* If LIMIT_VR is of the form [N1, N2], we need to build the
1719 range [MIN, N2] for LE_EXPR and [MIN, N2 - 1] for
1720 LT_EXPR. */
1721 max = limit_vr->max;
1724 /* If the maximum value forces us to be out of bounds, simply punt.
1725 It would be pointless to try and do anything more since this
1726 all should be optimized away above us. */
1727 if ((cond_code == LT_EXPR
1728 && compare_values (max, min) == 0)
1729 || (CONSTANT_CLASS_P (max) && TREE_OVERFLOW (max)))
1730 set_value_range_to_varying (vr_p);
1731 else
1733 /* For LT_EXPR, we create the range [MIN, MAX - 1]. */
1734 if (cond_code == LT_EXPR)
1736 if (TYPE_PRECISION (TREE_TYPE (max)) == 1
1737 && !TYPE_UNSIGNED (TREE_TYPE (max)))
1738 max = fold_build2 (PLUS_EXPR, TREE_TYPE (max), max,
1739 build_int_cst (TREE_TYPE (max), -1));
1740 else
1741 max = fold_build2 (MINUS_EXPR, TREE_TYPE (max), max,
1742 build_int_cst (TREE_TYPE (max), 1));
1743 if (EXPR_P (max))
1744 TREE_NO_WARNING (max) = 1;
1747 set_value_range (vr_p, VR_RANGE, min, max, vr_p->equiv);
1750 else if (cond_code == GE_EXPR || cond_code == GT_EXPR)
1752 max = TYPE_MAX_VALUE (type);
1754 if (limit_vr == NULL || limit_vr->type == VR_ANTI_RANGE)
1755 min = limit;
1756 else
1758 /* If LIMIT_VR is of the form [N1, N2], we need to build the
1759 range [N1, MAX] for GE_EXPR and [N1 + 1, MAX] for
1760 GT_EXPR. */
1761 min = limit_vr->min;
1764 /* If the minimum value forces us to be out of bounds, simply punt.
1765 It would be pointless to try and do anything more since this
1766 all should be optimized away above us. */
1767 if ((cond_code == GT_EXPR
1768 && compare_values (min, max) == 0)
1769 || (CONSTANT_CLASS_P (min) && TREE_OVERFLOW (min)))
1770 set_value_range_to_varying (vr_p);
1771 else
1773 /* For GT_EXPR, we create the range [MIN + 1, MAX]. */
1774 if (cond_code == GT_EXPR)
1776 if (TYPE_PRECISION (TREE_TYPE (min)) == 1
1777 && !TYPE_UNSIGNED (TREE_TYPE (min)))
1778 min = fold_build2 (MINUS_EXPR, TREE_TYPE (min), min,
1779 build_int_cst (TREE_TYPE (min), -1));
1780 else
1781 min = fold_build2 (PLUS_EXPR, TREE_TYPE (min), min,
1782 build_int_cst (TREE_TYPE (min), 1));
1783 if (EXPR_P (min))
1784 TREE_NO_WARNING (min) = 1;
1787 set_value_range (vr_p, VR_RANGE, min, max, vr_p->equiv);
1790 else
1791 gcc_unreachable ();
1793 /* Finally intersect the new range with what we already know about var. */
1794 vrp_intersect_ranges (vr_p, get_value_range (var));
1798 /* Extract range information from SSA name VAR and store it in VR. If
1799 VAR has an interesting range, use it. Otherwise, create the
1800 range [VAR, VAR] and return it. This is useful in situations where
1801 we may have conditionals testing values of VARYING names. For
1802 instance,
1804 x_3 = y_5;
1805 if (x_3 > y_5)
1808 Even if y_5 is deemed VARYING, we can determine that x_3 > y_5 is
1809 always false. */
1811 static void
1812 extract_range_from_ssa_name (value_range_t *vr, tree var)
1814 value_range_t *var_vr = get_value_range (var);
1816 if (var_vr->type != VR_UNDEFINED && var_vr->type != VR_VARYING)
1817 copy_value_range (vr, var_vr);
1818 else
1819 set_value_range (vr, VR_RANGE, var, var, NULL);
1821 add_equivalence (&vr->equiv, var);
1825 /* Wrapper around int_const_binop. If the operation overflows and we
1826 are not using wrapping arithmetic, then adjust the result to be
1827 -INF or +INF depending on CODE, VAL1 and VAL2. This can return
1828 NULL_TREE if we need to use an overflow infinity representation but
1829 the type does not support it. */
1831 static tree
1832 vrp_int_const_binop (enum tree_code code, tree val1, tree val2)
1834 tree res;
1836 res = int_const_binop (code, val1, val2);
1838 /* If we are using unsigned arithmetic, operate symbolically
1839 on -INF and +INF as int_const_binop only handles signed overflow. */
1840 if (TYPE_UNSIGNED (TREE_TYPE (val1)))
1842 int checkz = compare_values (res, val1);
1843 bool overflow = false;
1845 /* Ensure that res = val1 [+*] val2 >= val1
1846 or that res = val1 - val2 <= val1. */
1847 if ((code == PLUS_EXPR
1848 && !(checkz == 1 || checkz == 0))
1849 || (code == MINUS_EXPR
1850 && !(checkz == 0 || checkz == -1)))
1852 overflow = true;
1854 /* Checking for multiplication overflow is done by dividing the
1855 output of the multiplication by the first input of the
1856 multiplication. If the result of that division operation is
1857 not equal to the second input of the multiplication, then the
1858 multiplication overflowed. */
1859 else if (code == MULT_EXPR && !integer_zerop (val1))
1861 tree tmp = int_const_binop (TRUNC_DIV_EXPR,
1862 res,
1863 val1);
1864 int check = compare_values (tmp, val2);
1866 if (check != 0)
1867 overflow = true;
1870 if (overflow)
1872 res = copy_node (res);
1873 TREE_OVERFLOW (res) = 1;
1877 else if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (val1)))
1878 /* If the singed operation wraps then int_const_binop has done
1879 everything we want. */
1881 else if ((TREE_OVERFLOW (res)
1882 && !TREE_OVERFLOW (val1)
1883 && !TREE_OVERFLOW (val2))
1884 || is_overflow_infinity (val1)
1885 || is_overflow_infinity (val2))
1887 /* If the operation overflowed but neither VAL1 nor VAL2 are
1888 overflown, return -INF or +INF depending on the operation
1889 and the combination of signs of the operands. */
1890 int sgn1 = tree_int_cst_sgn (val1);
1891 int sgn2 = tree_int_cst_sgn (val2);
1893 if (needs_overflow_infinity (TREE_TYPE (res))
1894 && !supports_overflow_infinity (TREE_TYPE (res)))
1895 return NULL_TREE;
1897 /* We have to punt on adding infinities of different signs,
1898 since we can't tell what the sign of the result should be.
1899 Likewise for subtracting infinities of the same sign. */
1900 if (((code == PLUS_EXPR && sgn1 != sgn2)
1901 || (code == MINUS_EXPR && sgn1 == sgn2))
1902 && is_overflow_infinity (val1)
1903 && is_overflow_infinity (val2))
1904 return NULL_TREE;
1906 /* Don't try to handle division or shifting of infinities. */
1907 if ((code == TRUNC_DIV_EXPR
1908 || code == FLOOR_DIV_EXPR
1909 || code == CEIL_DIV_EXPR
1910 || code == EXACT_DIV_EXPR
1911 || code == ROUND_DIV_EXPR
1912 || code == RSHIFT_EXPR)
1913 && (is_overflow_infinity (val1)
1914 || is_overflow_infinity (val2)))
1915 return NULL_TREE;
1917 /* Notice that we only need to handle the restricted set of
1918 operations handled by extract_range_from_binary_expr.
1919 Among them, only multiplication, addition and subtraction
1920 can yield overflow without overflown operands because we
1921 are working with integral types only... except in the
1922 case VAL1 = -INF and VAL2 = -1 which overflows to +INF
1923 for division too. */
1925 /* For multiplication, the sign of the overflow is given
1926 by the comparison of the signs of the operands. */
1927 if ((code == MULT_EXPR && sgn1 == sgn2)
1928 /* For addition, the operands must be of the same sign
1929 to yield an overflow. Its sign is therefore that
1930 of one of the operands, for example the first. For
1931 infinite operands X + -INF is negative, not positive. */
1932 || (code == PLUS_EXPR
1933 && (sgn1 >= 0
1934 ? !is_negative_overflow_infinity (val2)
1935 : is_positive_overflow_infinity (val2)))
1936 /* For subtraction, non-infinite operands must be of
1937 different signs to yield an overflow. Its sign is
1938 therefore that of the first operand or the opposite of
1939 that of the second operand. A first operand of 0 counts
1940 as positive here, for the corner case 0 - (-INF), which
1941 overflows, but must yield +INF. For infinite operands 0
1942 - INF is negative, not positive. */
1943 || (code == MINUS_EXPR
1944 && (sgn1 >= 0
1945 ? !is_positive_overflow_infinity (val2)
1946 : is_negative_overflow_infinity (val2)))
1947 /* We only get in here with positive shift count, so the
1948 overflow direction is the same as the sign of val1.
1949 Actually rshift does not overflow at all, but we only
1950 handle the case of shifting overflowed -INF and +INF. */
1951 || (code == RSHIFT_EXPR
1952 && sgn1 >= 0)
1953 /* For division, the only case is -INF / -1 = +INF. */
1954 || code == TRUNC_DIV_EXPR
1955 || code == FLOOR_DIV_EXPR
1956 || code == CEIL_DIV_EXPR
1957 || code == EXACT_DIV_EXPR
1958 || code == ROUND_DIV_EXPR)
1959 return (needs_overflow_infinity (TREE_TYPE (res))
1960 ? positive_overflow_infinity (TREE_TYPE (res))
1961 : TYPE_MAX_VALUE (TREE_TYPE (res)));
1962 else
1963 return (needs_overflow_infinity (TREE_TYPE (res))
1964 ? negative_overflow_infinity (TREE_TYPE (res))
1965 : TYPE_MIN_VALUE (TREE_TYPE (res)));
1968 return res;
1972 /* For range VR compute two double_int bitmasks. In *MAY_BE_NONZERO
1973 bitmask if some bit is unset, it means for all numbers in the range
1974 the bit is 0, otherwise it might be 0 or 1. In *MUST_BE_NONZERO
1975 bitmask if some bit is set, it means for all numbers in the range
1976 the bit is 1, otherwise it might be 0 or 1. */
1978 static bool
1979 zero_nonzero_bits_from_vr (value_range_t *vr,
1980 double_int *may_be_nonzero,
1981 double_int *must_be_nonzero)
1983 *may_be_nonzero = double_int_minus_one;
1984 *must_be_nonzero = double_int_zero;
1985 if (!range_int_cst_p (vr)
1986 || TREE_OVERFLOW (vr->min)
1987 || TREE_OVERFLOW (vr->max))
1988 return false;
1990 if (range_int_cst_singleton_p (vr))
1992 *may_be_nonzero = tree_to_double_int (vr->min);
1993 *must_be_nonzero = *may_be_nonzero;
1995 else if (tree_int_cst_sgn (vr->min) >= 0
1996 || tree_int_cst_sgn (vr->max) < 0)
1998 double_int dmin = tree_to_double_int (vr->min);
1999 double_int dmax = tree_to_double_int (vr->max);
2000 double_int xor_mask = dmin ^ dmax;
2001 *may_be_nonzero = dmin | dmax;
2002 *must_be_nonzero = dmin & dmax;
2003 if (xor_mask.high != 0)
2005 unsigned HOST_WIDE_INT mask
2006 = ((unsigned HOST_WIDE_INT) 1
2007 << floor_log2 (xor_mask.high)) - 1;
2008 may_be_nonzero->low = ALL_ONES;
2009 may_be_nonzero->high |= mask;
2010 must_be_nonzero->low = 0;
2011 must_be_nonzero->high &= ~mask;
2013 else if (xor_mask.low != 0)
2015 unsigned HOST_WIDE_INT mask
2016 = ((unsigned HOST_WIDE_INT) 1
2017 << floor_log2 (xor_mask.low)) - 1;
2018 may_be_nonzero->low |= mask;
2019 must_be_nonzero->low &= ~mask;
2023 return true;
2026 /* Create two value-ranges in *VR0 and *VR1 from the anti-range *AR
2027 so that *VR0 U *VR1 == *AR. Returns true if that is possible,
2028 false otherwise. If *AR can be represented with a single range
2029 *VR1 will be VR_UNDEFINED. */
2031 static bool
2032 ranges_from_anti_range (value_range_t *ar,
2033 value_range_t *vr0, value_range_t *vr1)
2035 tree type = TREE_TYPE (ar->min);
2037 vr0->type = VR_UNDEFINED;
2038 vr1->type = VR_UNDEFINED;
2040 if (ar->type != VR_ANTI_RANGE
2041 || TREE_CODE (ar->min) != INTEGER_CST
2042 || TREE_CODE (ar->max) != INTEGER_CST
2043 || !vrp_val_min (type)
2044 || !vrp_val_max (type))
2045 return false;
2047 if (!vrp_val_is_min (ar->min))
2049 vr0->type = VR_RANGE;
2050 vr0->min = vrp_val_min (type);
2051 vr0->max
2052 = double_int_to_tree (type,
2053 tree_to_double_int (ar->min) - double_int_one);
2055 if (!vrp_val_is_max (ar->max))
2057 vr1->type = VR_RANGE;
2058 vr1->min
2059 = double_int_to_tree (type,
2060 tree_to_double_int (ar->max) + double_int_one);
2061 vr1->max = vrp_val_max (type);
2063 if (vr0->type == VR_UNDEFINED)
2065 *vr0 = *vr1;
2066 vr1->type = VR_UNDEFINED;
2069 return vr0->type != VR_UNDEFINED;
2072 /* Helper to extract a value-range *VR for a multiplicative operation
2073 *VR0 CODE *VR1. */
2075 static void
2076 extract_range_from_multiplicative_op_1 (value_range_t *vr,
2077 enum tree_code code,
2078 value_range_t *vr0, value_range_t *vr1)
2080 enum value_range_type type;
2081 tree val[4];
2082 size_t i;
2083 tree min, max;
2084 bool sop;
2085 int cmp;
2087 /* Multiplications, divisions and shifts are a bit tricky to handle,
2088 depending on the mix of signs we have in the two ranges, we
2089 need to operate on different values to get the minimum and
2090 maximum values for the new range. One approach is to figure
2091 out all the variations of range combinations and do the
2092 operations.
2094 However, this involves several calls to compare_values and it
2095 is pretty convoluted. It's simpler to do the 4 operations
2096 (MIN0 OP MIN1, MIN0 OP MAX1, MAX0 OP MIN1 and MAX0 OP MAX0 OP
2097 MAX1) and then figure the smallest and largest values to form
2098 the new range. */
2099 gcc_assert (code == MULT_EXPR
2100 || code == TRUNC_DIV_EXPR
2101 || code == FLOOR_DIV_EXPR
2102 || code == CEIL_DIV_EXPR
2103 || code == EXACT_DIV_EXPR
2104 || code == ROUND_DIV_EXPR
2105 || code == RSHIFT_EXPR
2106 || code == LSHIFT_EXPR);
2107 gcc_assert ((vr0->type == VR_RANGE
2108 || (code == MULT_EXPR && vr0->type == VR_ANTI_RANGE))
2109 && vr0->type == vr1->type);
2111 type = vr0->type;
2113 /* Compute the 4 cross operations. */
2114 sop = false;
2115 val[0] = vrp_int_const_binop (code, vr0->min, vr1->min);
2116 if (val[0] == NULL_TREE)
2117 sop = true;
2119 if (vr1->max == vr1->min)
2120 val[1] = NULL_TREE;
2121 else
2123 val[1] = vrp_int_const_binop (code, vr0->min, vr1->max);
2124 if (val[1] == NULL_TREE)
2125 sop = true;
2128 if (vr0->max == vr0->min)
2129 val[2] = NULL_TREE;
2130 else
2132 val[2] = vrp_int_const_binop (code, vr0->max, vr1->min);
2133 if (val[2] == NULL_TREE)
2134 sop = true;
2137 if (vr0->min == vr0->max || vr1->min == vr1->max)
2138 val[3] = NULL_TREE;
2139 else
2141 val[3] = vrp_int_const_binop (code, vr0->max, vr1->max);
2142 if (val[3] == NULL_TREE)
2143 sop = true;
2146 if (sop)
2148 set_value_range_to_varying (vr);
2149 return;
2152 /* Set MIN to the minimum of VAL[i] and MAX to the maximum
2153 of VAL[i]. */
2154 min = val[0];
2155 max = val[0];
2156 for (i = 1; i < 4; i++)
2158 if (!is_gimple_min_invariant (min)
2159 || (TREE_OVERFLOW (min) && !is_overflow_infinity (min))
2160 || !is_gimple_min_invariant (max)
2161 || (TREE_OVERFLOW (max) && !is_overflow_infinity (max)))
2162 break;
2164 if (val[i])
2166 if (!is_gimple_min_invariant (val[i])
2167 || (TREE_OVERFLOW (val[i])
2168 && !is_overflow_infinity (val[i])))
2170 /* If we found an overflowed value, set MIN and MAX
2171 to it so that we set the resulting range to
2172 VARYING. */
2173 min = max = val[i];
2174 break;
2177 if (compare_values (val[i], min) == -1)
2178 min = val[i];
2180 if (compare_values (val[i], max) == 1)
2181 max = val[i];
2185 /* If either MIN or MAX overflowed, then set the resulting range to
2186 VARYING. But we do accept an overflow infinity
2187 representation. */
2188 if (min == NULL_TREE
2189 || !is_gimple_min_invariant (min)
2190 || (TREE_OVERFLOW (min) && !is_overflow_infinity (min))
2191 || max == NULL_TREE
2192 || !is_gimple_min_invariant (max)
2193 || (TREE_OVERFLOW (max) && !is_overflow_infinity (max)))
2195 set_value_range_to_varying (vr);
2196 return;
2199 /* We punt if:
2200 1) [-INF, +INF]
2201 2) [-INF, +-INF(OVF)]
2202 3) [+-INF(OVF), +INF]
2203 4) [+-INF(OVF), +-INF(OVF)]
2204 We learn nothing when we have INF and INF(OVF) on both sides.
2205 Note that we do accept [-INF, -INF] and [+INF, +INF] without
2206 overflow. */
2207 if ((vrp_val_is_min (min) || is_overflow_infinity (min))
2208 && (vrp_val_is_max (max) || is_overflow_infinity (max)))
2210 set_value_range_to_varying (vr);
2211 return;
2214 cmp = compare_values (min, max);
2215 if (cmp == -2 || cmp == 1)
2217 /* If the new range has its limits swapped around (MIN > MAX),
2218 then the operation caused one of them to wrap around, mark
2219 the new range VARYING. */
2220 set_value_range_to_varying (vr);
2222 else
2223 set_value_range (vr, type, min, max, NULL);
2226 /* Some quadruple precision helpers. */
2227 static int
2228 quad_int_cmp (double_int l0, double_int h0,
2229 double_int l1, double_int h1, bool uns)
2231 int c = h0.cmp (h1, uns);
2232 if (c != 0) return c;
2233 return l0.ucmp (l1);
2236 static void
2237 quad_int_pair_sort (double_int *l0, double_int *h0,
2238 double_int *l1, double_int *h1, bool uns)
2240 if (quad_int_cmp (*l0, *h0, *l1, *h1, uns) > 0)
2242 double_int tmp;
2243 tmp = *l0; *l0 = *l1; *l1 = tmp;
2244 tmp = *h0; *h0 = *h1; *h1 = tmp;
2248 /* Extract range information from a binary operation CODE based on
2249 the ranges of each of its operands, *VR0 and *VR1 with resulting
2250 type EXPR_TYPE. The resulting range is stored in *VR. */
2252 static void
2253 extract_range_from_binary_expr_1 (value_range_t *vr,
2254 enum tree_code code, tree expr_type,
2255 value_range_t *vr0_, value_range_t *vr1_)
2257 value_range_t vr0 = *vr0_, vr1 = *vr1_;
2258 value_range_t vrtem0 = VR_INITIALIZER, vrtem1 = VR_INITIALIZER;
2259 enum value_range_type type;
2260 tree min = NULL_TREE, max = NULL_TREE;
2261 int cmp;
2263 if (!INTEGRAL_TYPE_P (expr_type)
2264 && !POINTER_TYPE_P (expr_type))
2266 set_value_range_to_varying (vr);
2267 return;
2270 /* Not all binary expressions can be applied to ranges in a
2271 meaningful way. Handle only arithmetic operations. */
2272 if (code != PLUS_EXPR
2273 && code != MINUS_EXPR
2274 && code != POINTER_PLUS_EXPR
2275 && code != MULT_EXPR
2276 && code != TRUNC_DIV_EXPR
2277 && code != FLOOR_DIV_EXPR
2278 && code != CEIL_DIV_EXPR
2279 && code != EXACT_DIV_EXPR
2280 && code != ROUND_DIV_EXPR
2281 && code != TRUNC_MOD_EXPR
2282 && code != RSHIFT_EXPR
2283 && code != LSHIFT_EXPR
2284 && code != MIN_EXPR
2285 && code != MAX_EXPR
2286 && code != BIT_AND_EXPR
2287 && code != BIT_IOR_EXPR
2288 && code != BIT_XOR_EXPR)
2290 set_value_range_to_varying (vr);
2291 return;
2294 /* If both ranges are UNDEFINED, so is the result. */
2295 if (vr0.type == VR_UNDEFINED && vr1.type == VR_UNDEFINED)
2297 set_value_range_to_undefined (vr);
2298 return;
2300 /* If one of the ranges is UNDEFINED drop it to VARYING for the following
2301 code. At some point we may want to special-case operations that
2302 have UNDEFINED result for all or some value-ranges of the not UNDEFINED
2303 operand. */
2304 else if (vr0.type == VR_UNDEFINED)
2305 set_value_range_to_varying (&vr0);
2306 else if (vr1.type == VR_UNDEFINED)
2307 set_value_range_to_varying (&vr1);
2309 /* Now canonicalize anti-ranges to ranges when they are not symbolic
2310 and express ~[] op X as ([]' op X) U ([]'' op X). */
2311 if (vr0.type == VR_ANTI_RANGE
2312 && ranges_from_anti_range (&vr0, &vrtem0, &vrtem1))
2314 extract_range_from_binary_expr_1 (vr, code, expr_type, &vrtem0, vr1_);
2315 if (vrtem1.type != VR_UNDEFINED)
2317 value_range_t vrres = VR_INITIALIZER;
2318 extract_range_from_binary_expr_1 (&vrres, code, expr_type,
2319 &vrtem1, vr1_);
2320 vrp_meet (vr, &vrres);
2322 return;
2324 /* Likewise for X op ~[]. */
2325 if (vr1.type == VR_ANTI_RANGE
2326 && ranges_from_anti_range (&vr1, &vrtem0, &vrtem1))
2328 extract_range_from_binary_expr_1 (vr, code, expr_type, vr0_, &vrtem0);
2329 if (vrtem1.type != VR_UNDEFINED)
2331 value_range_t vrres = VR_INITIALIZER;
2332 extract_range_from_binary_expr_1 (&vrres, code, expr_type,
2333 vr0_, &vrtem1);
2334 vrp_meet (vr, &vrres);
2336 return;
2339 /* The type of the resulting value range defaults to VR0.TYPE. */
2340 type = vr0.type;
2342 /* Refuse to operate on VARYING ranges, ranges of different kinds
2343 and symbolic ranges. As an exception, we allow BIT_AND_EXPR
2344 because we may be able to derive a useful range even if one of
2345 the operands is VR_VARYING or symbolic range. Similarly for
2346 divisions. TODO, we may be able to derive anti-ranges in
2347 some cases. */
2348 if (code != BIT_AND_EXPR
2349 && code != BIT_IOR_EXPR
2350 && code != TRUNC_DIV_EXPR
2351 && code != FLOOR_DIV_EXPR
2352 && code != CEIL_DIV_EXPR
2353 && code != EXACT_DIV_EXPR
2354 && code != ROUND_DIV_EXPR
2355 && code != TRUNC_MOD_EXPR
2356 && code != MIN_EXPR
2357 && code != MAX_EXPR
2358 && (vr0.type == VR_VARYING
2359 || vr1.type == VR_VARYING
2360 || vr0.type != vr1.type
2361 || symbolic_range_p (&vr0)
2362 || symbolic_range_p (&vr1)))
2364 set_value_range_to_varying (vr);
2365 return;
2368 /* Now evaluate the expression to determine the new range. */
2369 if (POINTER_TYPE_P (expr_type))
2371 if (code == MIN_EXPR || code == MAX_EXPR)
2373 /* For MIN/MAX expressions with pointers, we only care about
2374 nullness, if both are non null, then the result is nonnull.
2375 If both are null, then the result is null. Otherwise they
2376 are varying. */
2377 if (range_is_nonnull (&vr0) && range_is_nonnull (&vr1))
2378 set_value_range_to_nonnull (vr, expr_type);
2379 else if (range_is_null (&vr0) && range_is_null (&vr1))
2380 set_value_range_to_null (vr, expr_type);
2381 else
2382 set_value_range_to_varying (vr);
2384 else if (code == POINTER_PLUS_EXPR)
2386 /* For pointer types, we are really only interested in asserting
2387 whether the expression evaluates to non-NULL. */
2388 if (range_is_nonnull (&vr0) || range_is_nonnull (&vr1))
2389 set_value_range_to_nonnull (vr, expr_type);
2390 else if (range_is_null (&vr0) && range_is_null (&vr1))
2391 set_value_range_to_null (vr, expr_type);
2392 else
2393 set_value_range_to_varying (vr);
2395 else if (code == BIT_AND_EXPR)
2397 /* For pointer types, we are really only interested in asserting
2398 whether the expression evaluates to non-NULL. */
2399 if (range_is_nonnull (&vr0) && range_is_nonnull (&vr1))
2400 set_value_range_to_nonnull (vr, expr_type);
2401 else if (range_is_null (&vr0) || range_is_null (&vr1))
2402 set_value_range_to_null (vr, expr_type);
2403 else
2404 set_value_range_to_varying (vr);
2406 else
2407 set_value_range_to_varying (vr);
2409 return;
2412 /* For integer ranges, apply the operation to each end of the
2413 range and see what we end up with. */
2414 if (code == PLUS_EXPR || code == MINUS_EXPR)
2416 /* If we have a PLUS_EXPR with two VR_RANGE integer constant
2417 ranges compute the precise range for such case if possible. */
2418 if (range_int_cst_p (&vr0)
2419 && range_int_cst_p (&vr1)
2420 /* We need as many bits as the possibly unsigned inputs. */
2421 && TYPE_PRECISION (expr_type) <= HOST_BITS_PER_DOUBLE_INT)
2423 double_int min0 = tree_to_double_int (vr0.min);
2424 double_int max0 = tree_to_double_int (vr0.max);
2425 double_int min1 = tree_to_double_int (vr1.min);
2426 double_int max1 = tree_to_double_int (vr1.max);
2427 bool uns = TYPE_UNSIGNED (expr_type);
2428 double_int type_min
2429 = double_int::min_value (TYPE_PRECISION (expr_type), uns);
2430 double_int type_max
2431 = double_int::max_value (TYPE_PRECISION (expr_type), uns);
2432 double_int dmin, dmax;
2433 int min_ovf = 0;
2434 int max_ovf = 0;
2436 if (code == PLUS_EXPR)
2438 dmin = min0 + min1;
2439 dmax = max0 + max1;
2441 /* Check for overflow in double_int. */
2442 if (min1.cmp (double_int_zero, uns) != dmin.cmp (min0, uns))
2443 min_ovf = min0.cmp (dmin, uns);
2444 if (max1.cmp (double_int_zero, uns) != dmax.cmp (max0, uns))
2445 max_ovf = max0.cmp (dmax, uns);
2447 else /* if (code == MINUS_EXPR) */
2449 dmin = min0 - max1;
2450 dmax = max0 - min1;
2452 if (double_int_zero.cmp (max1, uns) != dmin.cmp (min0, uns))
2453 min_ovf = min0.cmp (max1, uns);
2454 if (double_int_zero.cmp (min1, uns) != dmax.cmp (max0, uns))
2455 max_ovf = max0.cmp (min1, uns);
2458 /* For non-wrapping arithmetic look at possibly smaller
2459 value-ranges of the type. */
2460 if (!TYPE_OVERFLOW_WRAPS (expr_type))
2462 if (vrp_val_min (expr_type))
2463 type_min = tree_to_double_int (vrp_val_min (expr_type));
2464 if (vrp_val_max (expr_type))
2465 type_max = tree_to_double_int (vrp_val_max (expr_type));
2468 /* Check for type overflow. */
2469 if (min_ovf == 0)
2471 if (dmin.cmp (type_min, uns) == -1)
2472 min_ovf = -1;
2473 else if (dmin.cmp (type_max, uns) == 1)
2474 min_ovf = 1;
2476 if (max_ovf == 0)
2478 if (dmax.cmp (type_min, uns) == -1)
2479 max_ovf = -1;
2480 else if (dmax.cmp (type_max, uns) == 1)
2481 max_ovf = 1;
2484 if (TYPE_OVERFLOW_WRAPS (expr_type))
2486 /* If overflow wraps, truncate the values and adjust the
2487 range kind and bounds appropriately. */
2488 double_int tmin
2489 = dmin.ext (TYPE_PRECISION (expr_type), uns);
2490 double_int tmax
2491 = dmax.ext (TYPE_PRECISION (expr_type), uns);
2492 if (min_ovf == max_ovf)
2494 /* No overflow or both overflow or underflow. The
2495 range kind stays VR_RANGE. */
2496 min = double_int_to_tree (expr_type, tmin);
2497 max = double_int_to_tree (expr_type, tmax);
2499 else if (min_ovf == -1
2500 && max_ovf == 1)
2502 /* Underflow and overflow, drop to VR_VARYING. */
2503 set_value_range_to_varying (vr);
2504 return;
2506 else
2508 /* Min underflow or max overflow. The range kind
2509 changes to VR_ANTI_RANGE. */
2510 bool covers = false;
2511 double_int tem = tmin;
2512 gcc_assert ((min_ovf == -1 && max_ovf == 0)
2513 || (max_ovf == 1 && min_ovf == 0));
2514 type = VR_ANTI_RANGE;
2515 tmin = tmax + double_int_one;
2516 if (tmin.cmp (tmax, uns) < 0)
2517 covers = true;
2518 tmax = tem + double_int_minus_one;
2519 if (tmax.cmp (tem, uns) > 0)
2520 covers = true;
2521 /* If the anti-range would cover nothing, drop to varying.
2522 Likewise if the anti-range bounds are outside of the
2523 types values. */
2524 if (covers || tmin.cmp (tmax, uns) > 0)
2526 set_value_range_to_varying (vr);
2527 return;
2529 min = double_int_to_tree (expr_type, tmin);
2530 max = double_int_to_tree (expr_type, tmax);
2533 else
2535 /* If overflow does not wrap, saturate to the types min/max
2536 value. */
2537 if (min_ovf == -1)
2539 if (needs_overflow_infinity (expr_type)
2540 && supports_overflow_infinity (expr_type))
2541 min = negative_overflow_infinity (expr_type);
2542 else
2543 min = double_int_to_tree (expr_type, type_min);
2545 else if (min_ovf == 1)
2547 if (needs_overflow_infinity (expr_type)
2548 && supports_overflow_infinity (expr_type))
2549 min = positive_overflow_infinity (expr_type);
2550 else
2551 min = double_int_to_tree (expr_type, type_max);
2553 else
2554 min = double_int_to_tree (expr_type, dmin);
2556 if (max_ovf == -1)
2558 if (needs_overflow_infinity (expr_type)
2559 && supports_overflow_infinity (expr_type))
2560 max = negative_overflow_infinity (expr_type);
2561 else
2562 max = double_int_to_tree (expr_type, type_min);
2564 else if (max_ovf == 1)
2566 if (needs_overflow_infinity (expr_type)
2567 && supports_overflow_infinity (expr_type))
2568 max = positive_overflow_infinity (expr_type);
2569 else
2570 max = double_int_to_tree (expr_type, type_max);
2572 else
2573 max = double_int_to_tree (expr_type, dmax);
2575 if (needs_overflow_infinity (expr_type)
2576 && supports_overflow_infinity (expr_type))
2578 if (is_negative_overflow_infinity (vr0.min)
2579 || (code == PLUS_EXPR
2580 ? is_negative_overflow_infinity (vr1.min)
2581 : is_positive_overflow_infinity (vr1.max)))
2582 min = negative_overflow_infinity (expr_type);
2583 if (is_positive_overflow_infinity (vr0.max)
2584 || (code == PLUS_EXPR
2585 ? is_positive_overflow_infinity (vr1.max)
2586 : is_negative_overflow_infinity (vr1.min)))
2587 max = positive_overflow_infinity (expr_type);
2590 else
2592 /* For other cases, for example if we have a PLUS_EXPR with two
2593 VR_ANTI_RANGEs, drop to VR_VARYING. It would take more effort
2594 to compute a precise range for such a case.
2595 ??? General even mixed range kind operations can be expressed
2596 by for example transforming ~[3, 5] + [1, 2] to range-only
2597 operations and a union primitive:
2598 [-INF, 2] + [1, 2] U [5, +INF] + [1, 2]
2599 [-INF+1, 4] U [6, +INF(OVF)]
2600 though usually the union is not exactly representable with
2601 a single range or anti-range as the above is
2602 [-INF+1, +INF(OVF)] intersected with ~[5, 5]
2603 but one could use a scheme similar to equivalences for this. */
2604 set_value_range_to_varying (vr);
2605 return;
2608 else if (code == MIN_EXPR
2609 || code == MAX_EXPR)
2611 if (vr0.type == VR_RANGE
2612 && !symbolic_range_p (&vr0))
2614 type = VR_RANGE;
2615 if (vr1.type == VR_RANGE
2616 && !symbolic_range_p (&vr1))
2618 /* For operations that make the resulting range directly
2619 proportional to the original ranges, apply the operation to
2620 the same end of each range. */
2621 min = vrp_int_const_binop (code, vr0.min, vr1.min);
2622 max = vrp_int_const_binop (code, vr0.max, vr1.max);
2624 else if (code == MIN_EXPR)
2626 min = vrp_val_min (expr_type);
2627 max = vr0.max;
2629 else if (code == MAX_EXPR)
2631 min = vr0.min;
2632 max = vrp_val_max (expr_type);
2635 else if (vr1.type == VR_RANGE
2636 && !symbolic_range_p (&vr1))
2638 type = VR_RANGE;
2639 if (code == MIN_EXPR)
2641 min = vrp_val_min (expr_type);
2642 max = vr1.max;
2644 else if (code == MAX_EXPR)
2646 min = vr1.min;
2647 max = vrp_val_max (expr_type);
2650 else
2652 set_value_range_to_varying (vr);
2653 return;
2656 else if (code == MULT_EXPR)
2658 /* Fancy code so that with unsigned, [-3,-1]*[-3,-1] does not
2659 drop to varying. */
2660 if (range_int_cst_p (&vr0)
2661 && range_int_cst_p (&vr1)
2662 && TYPE_OVERFLOW_WRAPS (expr_type))
2664 double_int min0, max0, min1, max1, sizem1, size;
2665 double_int prod0l, prod0h, prod1l, prod1h,
2666 prod2l, prod2h, prod3l, prod3h;
2667 bool uns0, uns1, uns;
2669 sizem1 = double_int::max_value (TYPE_PRECISION (expr_type), true);
2670 size = sizem1 + double_int_one;
2672 min0 = tree_to_double_int (vr0.min);
2673 max0 = tree_to_double_int (vr0.max);
2674 min1 = tree_to_double_int (vr1.min);
2675 max1 = tree_to_double_int (vr1.max);
2677 uns0 = TYPE_UNSIGNED (expr_type);
2678 uns1 = uns0;
2680 /* Canonicalize the intervals. */
2681 if (TYPE_UNSIGNED (expr_type))
2683 double_int min2 = size - min0;
2684 if (!min2.is_zero () && min2.cmp (max0, true) < 0)
2686 min0 = -min2;
2687 max0 -= size;
2688 uns0 = false;
2691 min2 = size - min1;
2692 if (!min2.is_zero () && min2.cmp (max1, true) < 0)
2694 min1 = -min2;
2695 max1 -= size;
2696 uns1 = false;
2699 uns = uns0 & uns1;
2701 bool overflow;
2702 prod0l = min0.wide_mul_with_sign (min1, true, &prod0h, &overflow);
2703 if (!uns0 && min0.is_negative ())
2704 prod0h -= min1;
2705 if (!uns1 && min1.is_negative ())
2706 prod0h -= min0;
2708 prod1l = min0.wide_mul_with_sign (max1, true, &prod1h, &overflow);
2709 if (!uns0 && min0.is_negative ())
2710 prod1h -= max1;
2711 if (!uns1 && max1.is_negative ())
2712 prod1h -= min0;
2714 prod2l = max0.wide_mul_with_sign (min1, true, &prod2h, &overflow);
2715 if (!uns0 && max0.is_negative ())
2716 prod2h -= min1;
2717 if (!uns1 && min1.is_negative ())
2718 prod2h -= max0;
2720 prod3l = max0.wide_mul_with_sign (max1, true, &prod3h, &overflow);
2721 if (!uns0 && max0.is_negative ())
2722 prod3h -= max1;
2723 if (!uns1 && max1.is_negative ())
2724 prod3h -= max0;
2726 /* Sort the 4 products. */
2727 quad_int_pair_sort (&prod0l, &prod0h, &prod3l, &prod3h, uns);
2728 quad_int_pair_sort (&prod1l, &prod1h, &prod2l, &prod2h, uns);
2729 quad_int_pair_sort (&prod0l, &prod0h, &prod1l, &prod1h, uns);
2730 quad_int_pair_sort (&prod2l, &prod2h, &prod3l, &prod3h, uns);
2732 /* Max - min. */
2733 if (prod0l.is_zero ())
2735 prod1l = double_int_zero;
2736 prod1h = -prod0h;
2738 else
2740 prod1l = -prod0l;
2741 prod1h = ~prod0h;
2743 prod2l = prod3l + prod1l;
2744 prod2h = prod3h + prod1h;
2745 if (prod2l.ult (prod3l))
2746 prod2h += double_int_one; /* carry */
2748 if (!prod2h.is_zero ()
2749 || prod2l.cmp (sizem1, true) >= 0)
2751 /* the range covers all values. */
2752 set_value_range_to_varying (vr);
2753 return;
2756 /* The following should handle the wrapping and selecting
2757 VR_ANTI_RANGE for us. */
2758 min = double_int_to_tree (expr_type, prod0l);
2759 max = double_int_to_tree (expr_type, prod3l);
2760 set_and_canonicalize_value_range (vr, VR_RANGE, min, max, NULL);
2761 return;
2764 /* If we have an unsigned MULT_EXPR with two VR_ANTI_RANGEs,
2765 drop to VR_VARYING. It would take more effort to compute a
2766 precise range for such a case. For example, if we have
2767 op0 == 65536 and op1 == 65536 with their ranges both being
2768 ~[0,0] on a 32-bit machine, we would have op0 * op1 == 0, so
2769 we cannot claim that the product is in ~[0,0]. Note that we
2770 are guaranteed to have vr0.type == vr1.type at this
2771 point. */
2772 if (vr0.type == VR_ANTI_RANGE
2773 && !TYPE_OVERFLOW_UNDEFINED (expr_type))
2775 set_value_range_to_varying (vr);
2776 return;
2779 extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1);
2780 return;
2782 else if (code == RSHIFT_EXPR
2783 || code == LSHIFT_EXPR)
2785 /* If we have a RSHIFT_EXPR with any shift values outside [0..prec-1],
2786 then drop to VR_VARYING. Outside of this range we get undefined
2787 behavior from the shift operation. We cannot even trust
2788 SHIFT_COUNT_TRUNCATED at this stage, because that applies to rtl
2789 shifts, and the operation at the tree level may be widened. */
2790 if (range_int_cst_p (&vr1)
2791 && compare_tree_int (vr1.min, 0) >= 0
2792 && compare_tree_int (vr1.max, TYPE_PRECISION (expr_type)) == -1)
2794 if (code == RSHIFT_EXPR)
2796 extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1);
2797 return;
2799 /* We can map lshifts by constants to MULT_EXPR handling. */
2800 else if (code == LSHIFT_EXPR
2801 && range_int_cst_singleton_p (&vr1))
2803 bool saved_flag_wrapv;
2804 value_range_t vr1p = VR_INITIALIZER;
2805 vr1p.type = VR_RANGE;
2806 vr1p.min
2807 = double_int_to_tree (expr_type,
2808 double_int_one
2809 .llshift (TREE_INT_CST_LOW (vr1.min),
2810 TYPE_PRECISION (expr_type)));
2811 vr1p.max = vr1p.min;
2812 /* We have to use a wrapping multiply though as signed overflow
2813 on lshifts is implementation defined in C89. */
2814 saved_flag_wrapv = flag_wrapv;
2815 flag_wrapv = 1;
2816 extract_range_from_binary_expr_1 (vr, MULT_EXPR, expr_type,
2817 &vr0, &vr1p);
2818 flag_wrapv = saved_flag_wrapv;
2819 return;
2821 else if (code == LSHIFT_EXPR
2822 && range_int_cst_p (&vr0))
2824 int prec = TYPE_PRECISION (expr_type);
2825 int overflow_pos = prec;
2826 int bound_shift;
2827 double_int bound, complement, low_bound, high_bound;
2828 bool uns = TYPE_UNSIGNED (expr_type);
2829 bool in_bounds = false;
2831 if (!uns)
2832 overflow_pos -= 1;
2834 bound_shift = overflow_pos - TREE_INT_CST_LOW (vr1.max);
2835 /* If bound_shift == HOST_BITS_PER_DOUBLE_INT, the llshift can
2836 overflow. However, for that to happen, vr1.max needs to be
2837 zero, which means vr1 is a singleton range of zero, which
2838 means it should be handled by the previous LSHIFT_EXPR
2839 if-clause. */
2840 bound = double_int_one.llshift (bound_shift, prec);
2841 complement = ~(bound - double_int_one);
2843 if (uns)
2845 low_bound = bound.zext (prec);
2846 high_bound = complement.zext (prec);
2847 if (tree_to_double_int (vr0.max).ult (low_bound))
2849 /* [5, 6] << [1, 2] == [10, 24]. */
2850 /* We're shifting out only zeroes, the value increases
2851 monotonically. */
2852 in_bounds = true;
2854 else if (high_bound.ult (tree_to_double_int (vr0.min)))
2856 /* [0xffffff00, 0xffffffff] << [1, 2]
2857 == [0xfffffc00, 0xfffffffe]. */
2858 /* We're shifting out only ones, the value decreases
2859 monotonically. */
2860 in_bounds = true;
2863 else
2865 /* [-1, 1] << [1, 2] == [-4, 4]. */
2866 low_bound = complement.sext (prec);
2867 high_bound = bound;
2868 if (tree_to_double_int (vr0.max).slt (high_bound)
2869 && low_bound.slt (tree_to_double_int (vr0.min)))
2871 /* For non-negative numbers, we're shifting out only
2872 zeroes, the value increases monotonically.
2873 For negative numbers, we're shifting out only ones, the
2874 value decreases monotomically. */
2875 in_bounds = true;
2879 if (in_bounds)
2881 extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1);
2882 return;
2886 set_value_range_to_varying (vr);
2887 return;
2889 else if (code == TRUNC_DIV_EXPR
2890 || code == FLOOR_DIV_EXPR
2891 || code == CEIL_DIV_EXPR
2892 || code == EXACT_DIV_EXPR
2893 || code == ROUND_DIV_EXPR)
2895 if (vr0.type != VR_RANGE || symbolic_range_p (&vr0))
2897 /* For division, if op1 has VR_RANGE but op0 does not, something
2898 can be deduced just from that range. Say [min, max] / [4, max]
2899 gives [min / 4, max / 4] range. */
2900 if (vr1.type == VR_RANGE
2901 && !symbolic_range_p (&vr1)
2902 && range_includes_zero_p (vr1.min, vr1.max) == 0)
2904 vr0.type = type = VR_RANGE;
2905 vr0.min = vrp_val_min (expr_type);
2906 vr0.max = vrp_val_max (expr_type);
2908 else
2910 set_value_range_to_varying (vr);
2911 return;
2915 /* For divisions, if flag_non_call_exceptions is true, we must
2916 not eliminate a division by zero. */
2917 if (cfun->can_throw_non_call_exceptions
2918 && (vr1.type != VR_RANGE
2919 || range_includes_zero_p (vr1.min, vr1.max) != 0))
2921 set_value_range_to_varying (vr);
2922 return;
2925 /* For divisions, if op0 is VR_RANGE, we can deduce a range
2926 even if op1 is VR_VARYING, VR_ANTI_RANGE, symbolic or can
2927 include 0. */
2928 if (vr0.type == VR_RANGE
2929 && (vr1.type != VR_RANGE
2930 || range_includes_zero_p (vr1.min, vr1.max) != 0))
2932 tree zero = build_int_cst (TREE_TYPE (vr0.min), 0);
2933 int cmp;
2935 min = NULL_TREE;
2936 max = NULL_TREE;
2937 if (TYPE_UNSIGNED (expr_type)
2938 || value_range_nonnegative_p (&vr1))
2940 /* For unsigned division or when divisor is known
2941 to be non-negative, the range has to cover
2942 all numbers from 0 to max for positive max
2943 and all numbers from min to 0 for negative min. */
2944 cmp = compare_values (vr0.max, zero);
2945 if (cmp == -1)
2946 max = zero;
2947 else if (cmp == 0 || cmp == 1)
2948 max = vr0.max;
2949 else
2950 type = VR_VARYING;
2951 cmp = compare_values (vr0.min, zero);
2952 if (cmp == 1)
2953 min = zero;
2954 else if (cmp == 0 || cmp == -1)
2955 min = vr0.min;
2956 else
2957 type = VR_VARYING;
2959 else
2961 /* Otherwise the range is -max .. max or min .. -min
2962 depending on which bound is bigger in absolute value,
2963 as the division can change the sign. */
2964 abs_extent_range (vr, vr0.min, vr0.max);
2965 return;
2967 if (type == VR_VARYING)
2969 set_value_range_to_varying (vr);
2970 return;
2973 else
2975 extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1);
2976 return;
2979 else if (code == TRUNC_MOD_EXPR)
2981 if (vr1.type != VR_RANGE
2982 || range_includes_zero_p (vr1.min, vr1.max) != 0
2983 || vrp_val_is_min (vr1.min))
2985 set_value_range_to_varying (vr);
2986 return;
2988 type = VR_RANGE;
2989 /* Compute MAX <|vr1.min|, |vr1.max|> - 1. */
2990 max = fold_unary_to_constant (ABS_EXPR, expr_type, vr1.min);
2991 if (tree_int_cst_lt (max, vr1.max))
2992 max = vr1.max;
2993 max = int_const_binop (MINUS_EXPR, max, integer_one_node);
2994 /* If the dividend is non-negative the modulus will be
2995 non-negative as well. */
2996 if (TYPE_UNSIGNED (expr_type)
2997 || value_range_nonnegative_p (&vr0))
2998 min = build_int_cst (TREE_TYPE (max), 0);
2999 else
3000 min = fold_unary_to_constant (NEGATE_EXPR, expr_type, max);
3002 else if (code == BIT_AND_EXPR || code == BIT_IOR_EXPR || code == BIT_XOR_EXPR)
3004 bool int_cst_range0, int_cst_range1;
3005 double_int may_be_nonzero0, may_be_nonzero1;
3006 double_int must_be_nonzero0, must_be_nonzero1;
3008 int_cst_range0 = zero_nonzero_bits_from_vr (&vr0, &may_be_nonzero0,
3009 &must_be_nonzero0);
3010 int_cst_range1 = zero_nonzero_bits_from_vr (&vr1, &may_be_nonzero1,
3011 &must_be_nonzero1);
3013 type = VR_RANGE;
3014 if (code == BIT_AND_EXPR)
3016 double_int dmax;
3017 min = double_int_to_tree (expr_type,
3018 must_be_nonzero0 & must_be_nonzero1);
3019 dmax = may_be_nonzero0 & may_be_nonzero1;
3020 /* If both input ranges contain only negative values we can
3021 truncate the result range maximum to the minimum of the
3022 input range maxima. */
3023 if (int_cst_range0 && int_cst_range1
3024 && tree_int_cst_sgn (vr0.max) < 0
3025 && tree_int_cst_sgn (vr1.max) < 0)
3027 dmax = dmax.min (tree_to_double_int (vr0.max),
3028 TYPE_UNSIGNED (expr_type));
3029 dmax = dmax.min (tree_to_double_int (vr1.max),
3030 TYPE_UNSIGNED (expr_type));
3032 /* If either input range contains only non-negative values
3033 we can truncate the result range maximum to the respective
3034 maximum of the input range. */
3035 if (int_cst_range0 && tree_int_cst_sgn (vr0.min) >= 0)
3036 dmax = dmax.min (tree_to_double_int (vr0.max),
3037 TYPE_UNSIGNED (expr_type));
3038 if (int_cst_range1 && tree_int_cst_sgn (vr1.min) >= 0)
3039 dmax = dmax.min (tree_to_double_int (vr1.max),
3040 TYPE_UNSIGNED (expr_type));
3041 max = double_int_to_tree (expr_type, dmax);
3043 else if (code == BIT_IOR_EXPR)
3045 double_int dmin;
3046 max = double_int_to_tree (expr_type,
3047 may_be_nonzero0 | may_be_nonzero1);
3048 dmin = must_be_nonzero0 | must_be_nonzero1;
3049 /* If the input ranges contain only positive values we can
3050 truncate the minimum of the result range to the maximum
3051 of the input range minima. */
3052 if (int_cst_range0 && int_cst_range1
3053 && tree_int_cst_sgn (vr0.min) >= 0
3054 && tree_int_cst_sgn (vr1.min) >= 0)
3056 dmin = dmin.max (tree_to_double_int (vr0.min),
3057 TYPE_UNSIGNED (expr_type));
3058 dmin = dmin.max (tree_to_double_int (vr1.min),
3059 TYPE_UNSIGNED (expr_type));
3061 /* If either input range contains only negative values
3062 we can truncate the minimum of the result range to the
3063 respective minimum range. */
3064 if (int_cst_range0 && tree_int_cst_sgn (vr0.max) < 0)
3065 dmin = dmin.max (tree_to_double_int (vr0.min),
3066 TYPE_UNSIGNED (expr_type));
3067 if (int_cst_range1 && tree_int_cst_sgn (vr1.max) < 0)
3068 dmin = dmin.max (tree_to_double_int (vr1.min),
3069 TYPE_UNSIGNED (expr_type));
3070 min = double_int_to_tree (expr_type, dmin);
3072 else if (code == BIT_XOR_EXPR)
3074 double_int result_zero_bits, result_one_bits;
3075 result_zero_bits = (must_be_nonzero0 & must_be_nonzero1)
3076 | ~(may_be_nonzero0 | may_be_nonzero1);
3077 result_one_bits = must_be_nonzero0.and_not (may_be_nonzero1)
3078 | must_be_nonzero1.and_not (may_be_nonzero0);
3079 max = double_int_to_tree (expr_type, ~result_zero_bits);
3080 min = double_int_to_tree (expr_type, result_one_bits);
3081 /* If the range has all positive or all negative values the
3082 result is better than VARYING. */
3083 if (tree_int_cst_sgn (min) < 0
3084 || tree_int_cst_sgn (max) >= 0)
3086 else
3087 max = min = NULL_TREE;
3090 else
3091 gcc_unreachable ();
3093 /* If either MIN or MAX overflowed, then set the resulting range to
3094 VARYING. But we do accept an overflow infinity
3095 representation. */
3096 if (min == NULL_TREE
3097 || !is_gimple_min_invariant (min)
3098 || (TREE_OVERFLOW (min) && !is_overflow_infinity (min))
3099 || max == NULL_TREE
3100 || !is_gimple_min_invariant (max)
3101 || (TREE_OVERFLOW (max) && !is_overflow_infinity (max)))
3103 set_value_range_to_varying (vr);
3104 return;
3107 /* We punt if:
3108 1) [-INF, +INF]
3109 2) [-INF, +-INF(OVF)]
3110 3) [+-INF(OVF), +INF]
3111 4) [+-INF(OVF), +-INF(OVF)]
3112 We learn nothing when we have INF and INF(OVF) on both sides.
3113 Note that we do accept [-INF, -INF] and [+INF, +INF] without
3114 overflow. */
3115 if ((vrp_val_is_min (min) || is_overflow_infinity (min))
3116 && (vrp_val_is_max (max) || is_overflow_infinity (max)))
3118 set_value_range_to_varying (vr);
3119 return;
3122 cmp = compare_values (min, max);
3123 if (cmp == -2 || cmp == 1)
3125 /* If the new range has its limits swapped around (MIN > MAX),
3126 then the operation caused one of them to wrap around, mark
3127 the new range VARYING. */
3128 set_value_range_to_varying (vr);
3130 else
3131 set_value_range (vr, type, min, max, NULL);
3134 /* Extract range information from a binary expression OP0 CODE OP1 based on
3135 the ranges of each of its operands with resulting type EXPR_TYPE.
3136 The resulting range is stored in *VR. */
3138 static void
3139 extract_range_from_binary_expr (value_range_t *vr,
3140 enum tree_code code,
3141 tree expr_type, tree op0, tree op1)
3143 value_range_t vr0 = VR_INITIALIZER;
3144 value_range_t vr1 = VR_INITIALIZER;
3146 /* Get value ranges for each operand. For constant operands, create
3147 a new value range with the operand to simplify processing. */
3148 if (TREE_CODE (op0) == SSA_NAME)
3149 vr0 = *(get_value_range (op0));
3150 else if (is_gimple_min_invariant (op0))
3151 set_value_range_to_value (&vr0, op0, NULL);
3152 else
3153 set_value_range_to_varying (&vr0);
3155 if (TREE_CODE (op1) == SSA_NAME)
3156 vr1 = *(get_value_range (op1));
3157 else if (is_gimple_min_invariant (op1))
3158 set_value_range_to_value (&vr1, op1, NULL);
3159 else
3160 set_value_range_to_varying (&vr1);
3162 extract_range_from_binary_expr_1 (vr, code, expr_type, &vr0, &vr1);
3165 /* Extract range information from a unary operation CODE based on
3166 the range of its operand *VR0 with type OP0_TYPE with resulting type TYPE.
3167 The The resulting range is stored in *VR. */
3169 static void
3170 extract_range_from_unary_expr_1 (value_range_t *vr,
3171 enum tree_code code, tree type,
3172 value_range_t *vr0_, tree op0_type)
3174 value_range_t vr0 = *vr0_, vrtem0 = VR_INITIALIZER, vrtem1 = VR_INITIALIZER;
3176 /* VRP only operates on integral and pointer types. */
3177 if (!(INTEGRAL_TYPE_P (op0_type)
3178 || POINTER_TYPE_P (op0_type))
3179 || !(INTEGRAL_TYPE_P (type)
3180 || POINTER_TYPE_P (type)))
3182 set_value_range_to_varying (vr);
3183 return;
3186 /* If VR0 is UNDEFINED, so is the result. */
3187 if (vr0.type == VR_UNDEFINED)
3189 set_value_range_to_undefined (vr);
3190 return;
3193 /* Handle operations that we express in terms of others. */
3194 if (code == PAREN_EXPR)
3196 /* PAREN_EXPR is a simple copy. */
3197 copy_value_range (vr, &vr0);
3198 return;
3200 else if (code == NEGATE_EXPR)
3202 /* -X is simply 0 - X, so re-use existing code that also handles
3203 anti-ranges fine. */
3204 value_range_t zero = VR_INITIALIZER;
3205 set_value_range_to_value (&zero, build_int_cst (type, 0), NULL);
3206 extract_range_from_binary_expr_1 (vr, MINUS_EXPR, type, &zero, &vr0);
3207 return;
3209 else if (code == BIT_NOT_EXPR)
3211 /* ~X is simply -1 - X, so re-use existing code that also handles
3212 anti-ranges fine. */
3213 value_range_t minusone = VR_INITIALIZER;
3214 set_value_range_to_value (&minusone, build_int_cst (type, -1), NULL);
3215 extract_range_from_binary_expr_1 (vr, MINUS_EXPR,
3216 type, &minusone, &vr0);
3217 return;
3220 /* Now canonicalize anti-ranges to ranges when they are not symbolic
3221 and express op ~[] as (op []') U (op []''). */
3222 if (vr0.type == VR_ANTI_RANGE
3223 && ranges_from_anti_range (&vr0, &vrtem0, &vrtem1))
3225 extract_range_from_unary_expr_1 (vr, code, type, &vrtem0, op0_type);
3226 if (vrtem1.type != VR_UNDEFINED)
3228 value_range_t vrres = VR_INITIALIZER;
3229 extract_range_from_unary_expr_1 (&vrres, code, type,
3230 &vrtem1, op0_type);
3231 vrp_meet (vr, &vrres);
3233 return;
3236 if (CONVERT_EXPR_CODE_P (code))
3238 tree inner_type = op0_type;
3239 tree outer_type = type;
3241 /* If the expression evaluates to a pointer, we are only interested in
3242 determining if it evaluates to NULL [0, 0] or non-NULL (~[0, 0]). */
3243 if (POINTER_TYPE_P (type))
3245 if (range_is_nonnull (&vr0))
3246 set_value_range_to_nonnull (vr, type);
3247 else if (range_is_null (&vr0))
3248 set_value_range_to_null (vr, type);
3249 else
3250 set_value_range_to_varying (vr);
3251 return;
3254 /* If VR0 is varying and we increase the type precision, assume
3255 a full range for the following transformation. */
3256 if (vr0.type == VR_VARYING
3257 && INTEGRAL_TYPE_P (inner_type)
3258 && TYPE_PRECISION (inner_type) < TYPE_PRECISION (outer_type))
3260 vr0.type = VR_RANGE;
3261 vr0.min = TYPE_MIN_VALUE (inner_type);
3262 vr0.max = TYPE_MAX_VALUE (inner_type);
3265 /* If VR0 is a constant range or anti-range and the conversion is
3266 not truncating we can convert the min and max values and
3267 canonicalize the resulting range. Otherwise we can do the
3268 conversion if the size of the range is less than what the
3269 precision of the target type can represent and the range is
3270 not an anti-range. */
3271 if ((vr0.type == VR_RANGE
3272 || vr0.type == VR_ANTI_RANGE)
3273 && TREE_CODE (vr0.min) == INTEGER_CST
3274 && TREE_CODE (vr0.max) == INTEGER_CST
3275 && (!is_overflow_infinity (vr0.min)
3276 || (vr0.type == VR_RANGE
3277 && TYPE_PRECISION (outer_type) > TYPE_PRECISION (inner_type)
3278 && needs_overflow_infinity (outer_type)
3279 && supports_overflow_infinity (outer_type)))
3280 && (!is_overflow_infinity (vr0.max)
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 && (TYPE_PRECISION (outer_type) >= TYPE_PRECISION (inner_type)
3286 || (vr0.type == VR_RANGE
3287 && integer_zerop (int_const_binop (RSHIFT_EXPR,
3288 int_const_binop (MINUS_EXPR, vr0.max, vr0.min),
3289 size_int (TYPE_PRECISION (outer_type)))))))
3291 tree new_min, new_max;
3292 if (is_overflow_infinity (vr0.min))
3293 new_min = negative_overflow_infinity (outer_type);
3294 else
3295 new_min = force_fit_type_double (outer_type,
3296 tree_to_double_int (vr0.min),
3297 0, false);
3298 if (is_overflow_infinity (vr0.max))
3299 new_max = positive_overflow_infinity (outer_type);
3300 else
3301 new_max = force_fit_type_double (outer_type,
3302 tree_to_double_int (vr0.max),
3303 0, false);
3304 set_and_canonicalize_value_range (vr, vr0.type,
3305 new_min, new_max, NULL);
3306 return;
3309 set_value_range_to_varying (vr);
3310 return;
3312 else if (code == ABS_EXPR)
3314 tree min, max;
3315 int cmp;
3317 /* Pass through vr0 in the easy cases. */
3318 if (TYPE_UNSIGNED (type)
3319 || value_range_nonnegative_p (&vr0))
3321 copy_value_range (vr, &vr0);
3322 return;
3325 /* For the remaining varying or symbolic ranges we can't do anything
3326 useful. */
3327 if (vr0.type == VR_VARYING
3328 || symbolic_range_p (&vr0))
3330 set_value_range_to_varying (vr);
3331 return;
3334 /* -TYPE_MIN_VALUE = TYPE_MIN_VALUE with flag_wrapv so we can't get a
3335 useful range. */
3336 if (!TYPE_OVERFLOW_UNDEFINED (type)
3337 && ((vr0.type == VR_RANGE
3338 && vrp_val_is_min (vr0.min))
3339 || (vr0.type == VR_ANTI_RANGE
3340 && !vrp_val_is_min (vr0.min))))
3342 set_value_range_to_varying (vr);
3343 return;
3346 /* ABS_EXPR may flip the range around, if the original range
3347 included negative values. */
3348 if (is_overflow_infinity (vr0.min))
3349 min = positive_overflow_infinity (type);
3350 else if (!vrp_val_is_min (vr0.min))
3351 min = fold_unary_to_constant (code, type, vr0.min);
3352 else if (!needs_overflow_infinity (type))
3353 min = TYPE_MAX_VALUE (type);
3354 else if (supports_overflow_infinity (type))
3355 min = positive_overflow_infinity (type);
3356 else
3358 set_value_range_to_varying (vr);
3359 return;
3362 if (is_overflow_infinity (vr0.max))
3363 max = positive_overflow_infinity (type);
3364 else if (!vrp_val_is_min (vr0.max))
3365 max = fold_unary_to_constant (code, type, vr0.max);
3366 else if (!needs_overflow_infinity (type))
3367 max = TYPE_MAX_VALUE (type);
3368 else if (supports_overflow_infinity (type)
3369 /* We shouldn't generate [+INF, +INF] as set_value_range
3370 doesn't like this and ICEs. */
3371 && !is_positive_overflow_infinity (min))
3372 max = positive_overflow_infinity (type);
3373 else
3375 set_value_range_to_varying (vr);
3376 return;
3379 cmp = compare_values (min, max);
3381 /* If a VR_ANTI_RANGEs contains zero, then we have
3382 ~[-INF, min(MIN, MAX)]. */
3383 if (vr0.type == VR_ANTI_RANGE)
3385 if (range_includes_zero_p (vr0.min, vr0.max) == 1)
3387 /* Take the lower of the two values. */
3388 if (cmp != 1)
3389 max = min;
3391 /* Create ~[-INF, min (abs(MIN), abs(MAX))]
3392 or ~[-INF + 1, min (abs(MIN), abs(MAX))] when
3393 flag_wrapv is set and the original anti-range doesn't include
3394 TYPE_MIN_VALUE, remember -TYPE_MIN_VALUE = TYPE_MIN_VALUE. */
3395 if (TYPE_OVERFLOW_WRAPS (type))
3397 tree type_min_value = TYPE_MIN_VALUE (type);
3399 min = (vr0.min != type_min_value
3400 ? int_const_binop (PLUS_EXPR, type_min_value,
3401 integer_one_node)
3402 : type_min_value);
3404 else
3406 if (overflow_infinity_range_p (&vr0))
3407 min = negative_overflow_infinity (type);
3408 else
3409 min = TYPE_MIN_VALUE (type);
3412 else
3414 /* All else has failed, so create the range [0, INF], even for
3415 flag_wrapv since TYPE_MIN_VALUE is in the original
3416 anti-range. */
3417 vr0.type = VR_RANGE;
3418 min = build_int_cst (type, 0);
3419 if (needs_overflow_infinity (type))
3421 if (supports_overflow_infinity (type))
3422 max = positive_overflow_infinity (type);
3423 else
3425 set_value_range_to_varying (vr);
3426 return;
3429 else
3430 max = TYPE_MAX_VALUE (type);
3434 /* If the range contains zero then we know that the minimum value in the
3435 range will be zero. */
3436 else if (range_includes_zero_p (vr0.min, vr0.max) == 1)
3438 if (cmp == 1)
3439 max = min;
3440 min = build_int_cst (type, 0);
3442 else
3444 /* If the range was reversed, swap MIN and MAX. */
3445 if (cmp == 1)
3447 tree t = min;
3448 min = max;
3449 max = t;
3453 cmp = compare_values (min, max);
3454 if (cmp == -2 || cmp == 1)
3456 /* If the new range has its limits swapped around (MIN > MAX),
3457 then the operation caused one of them to wrap around, mark
3458 the new range VARYING. */
3459 set_value_range_to_varying (vr);
3461 else
3462 set_value_range (vr, vr0.type, min, max, NULL);
3463 return;
3466 /* For unhandled operations fall back to varying. */
3467 set_value_range_to_varying (vr);
3468 return;
3472 /* Extract range information from a unary expression CODE OP0 based on
3473 the range of its operand with resulting type TYPE.
3474 The resulting range is stored in *VR. */
3476 static void
3477 extract_range_from_unary_expr (value_range_t *vr, enum tree_code code,
3478 tree type, tree op0)
3480 value_range_t vr0 = VR_INITIALIZER;
3482 /* Get value ranges for the operand. For constant operands, create
3483 a new value range with the operand to simplify processing. */
3484 if (TREE_CODE (op0) == SSA_NAME)
3485 vr0 = *(get_value_range (op0));
3486 else if (is_gimple_min_invariant (op0))
3487 set_value_range_to_value (&vr0, op0, NULL);
3488 else
3489 set_value_range_to_varying (&vr0);
3491 extract_range_from_unary_expr_1 (vr, code, type, &vr0, TREE_TYPE (op0));
3495 /* Extract range information from a conditional expression STMT based on
3496 the ranges of each of its operands and the expression code. */
3498 static void
3499 extract_range_from_cond_expr (value_range_t *vr, gimple stmt)
3501 tree op0, op1;
3502 value_range_t vr0 = VR_INITIALIZER;
3503 value_range_t vr1 = VR_INITIALIZER;
3505 /* Get value ranges for each operand. For constant operands, create
3506 a new value range with the operand to simplify processing. */
3507 op0 = gimple_assign_rhs2 (stmt);
3508 if (TREE_CODE (op0) == SSA_NAME)
3509 vr0 = *(get_value_range (op0));
3510 else if (is_gimple_min_invariant (op0))
3511 set_value_range_to_value (&vr0, op0, NULL);
3512 else
3513 set_value_range_to_varying (&vr0);
3515 op1 = gimple_assign_rhs3 (stmt);
3516 if (TREE_CODE (op1) == SSA_NAME)
3517 vr1 = *(get_value_range (op1));
3518 else if (is_gimple_min_invariant (op1))
3519 set_value_range_to_value (&vr1, op1, NULL);
3520 else
3521 set_value_range_to_varying (&vr1);
3523 /* The resulting value range is the union of the operand ranges */
3524 copy_value_range (vr, &vr0);
3525 vrp_meet (vr, &vr1);
3529 /* Extract range information from a comparison expression EXPR based
3530 on the range of its operand and the expression code. */
3532 static void
3533 extract_range_from_comparison (value_range_t *vr, enum tree_code code,
3534 tree type, tree op0, tree op1)
3536 bool sop = false;
3537 tree val;
3539 val = vrp_evaluate_conditional_warnv_with_ops (code, op0, op1, false, &sop,
3540 NULL);
3542 /* A disadvantage of using a special infinity as an overflow
3543 representation is that we lose the ability to record overflow
3544 when we don't have an infinity. So we have to ignore a result
3545 which relies on overflow. */
3547 if (val && !is_overflow_infinity (val) && !sop)
3549 /* Since this expression was found on the RHS of an assignment,
3550 its type may be different from _Bool. Convert VAL to EXPR's
3551 type. */
3552 val = fold_convert (type, val);
3553 if (is_gimple_min_invariant (val))
3554 set_value_range_to_value (vr, val, vr->equiv);
3555 else
3556 set_value_range (vr, VR_RANGE, val, val, vr->equiv);
3558 else
3559 /* The result of a comparison is always true or false. */
3560 set_value_range_to_truthvalue (vr, type);
3563 /* Try to derive a nonnegative or nonzero range out of STMT relying
3564 primarily on generic routines in fold in conjunction with range data.
3565 Store the result in *VR */
3567 static void
3568 extract_range_basic (value_range_t *vr, gimple stmt)
3570 bool sop = false;
3571 tree type = gimple_expr_type (stmt);
3573 if (gimple_call_builtin_p (stmt, BUILT_IN_NORMAL))
3575 tree fndecl = gimple_call_fndecl (stmt), arg;
3576 int mini, maxi, zerov = 0, prec;
3578 switch (DECL_FUNCTION_CODE (fndecl))
3580 case BUILT_IN_CONSTANT_P:
3581 /* If the call is __builtin_constant_p and the argument is a
3582 function parameter resolve it to false. This avoids bogus
3583 array bound warnings.
3584 ??? We could do this as early as inlining is finished. */
3585 arg = gimple_call_arg (stmt, 0);
3586 if (TREE_CODE (arg) == SSA_NAME
3587 && SSA_NAME_IS_DEFAULT_DEF (arg)
3588 && TREE_CODE (SSA_NAME_VAR (arg)) == PARM_DECL)
3590 set_value_range_to_null (vr, type);
3591 return;
3593 break;
3594 /* Both __builtin_ffs* and __builtin_popcount return
3595 [0, prec]. */
3596 CASE_INT_FN (BUILT_IN_FFS):
3597 CASE_INT_FN (BUILT_IN_POPCOUNT):
3598 arg = gimple_call_arg (stmt, 0);
3599 prec = TYPE_PRECISION (TREE_TYPE (arg));
3600 mini = 0;
3601 maxi = prec;
3602 if (TREE_CODE (arg) == SSA_NAME)
3604 value_range_t *vr0 = get_value_range (arg);
3605 /* If arg is non-zero, then ffs or popcount
3606 are non-zero. */
3607 if (((vr0->type == VR_RANGE
3608 && integer_nonzerop (vr0->min))
3609 || (vr0->type == VR_ANTI_RANGE
3610 && integer_zerop (vr0->min)))
3611 && !TREE_OVERFLOW (vr0->min))
3612 mini = 1;
3613 /* If some high bits are known to be zero,
3614 we can decrease the maximum. */
3615 if (vr0->type == VR_RANGE
3616 && TREE_CODE (vr0->max) == INTEGER_CST
3617 && !TREE_OVERFLOW (vr0->max))
3618 maxi = tree_floor_log2 (vr0->max) + 1;
3620 goto bitop_builtin;
3621 /* __builtin_parity* returns [0, 1]. */
3622 CASE_INT_FN (BUILT_IN_PARITY):
3623 mini = 0;
3624 maxi = 1;
3625 goto bitop_builtin;
3626 /* __builtin_c[lt]z* return [0, prec-1], except for
3627 when the argument is 0, but that is undefined behavior.
3628 On many targets where the CLZ RTL or optab value is defined
3629 for 0 the value is prec, so include that in the range
3630 by default. */
3631 CASE_INT_FN (BUILT_IN_CLZ):
3632 arg = gimple_call_arg (stmt, 0);
3633 prec = TYPE_PRECISION (TREE_TYPE (arg));
3634 mini = 0;
3635 maxi = prec;
3636 if (optab_handler (clz_optab, TYPE_MODE (TREE_TYPE (arg)))
3637 != CODE_FOR_nothing
3638 && CLZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (TREE_TYPE (arg)),
3639 zerov)
3640 /* Handle only the single common value. */
3641 && zerov != prec)
3642 /* Magic value to give up, unless vr0 proves
3643 arg is non-zero. */
3644 mini = -2;
3645 if (TREE_CODE (arg) == SSA_NAME)
3647 value_range_t *vr0 = get_value_range (arg);
3648 /* From clz of VR_RANGE minimum we can compute
3649 result maximum. */
3650 if (vr0->type == VR_RANGE
3651 && TREE_CODE (vr0->min) == INTEGER_CST
3652 && !TREE_OVERFLOW (vr0->min))
3654 maxi = prec - 1 - tree_floor_log2 (vr0->min);
3655 if (maxi != prec)
3656 mini = 0;
3658 else if (vr0->type == VR_ANTI_RANGE
3659 && integer_zerop (vr0->min)
3660 && !TREE_OVERFLOW (vr0->min))
3662 maxi = prec - 1;
3663 mini = 0;
3665 if (mini == -2)
3666 break;
3667 /* From clz of VR_RANGE maximum we can compute
3668 result minimum. */
3669 if (vr0->type == VR_RANGE
3670 && TREE_CODE (vr0->max) == INTEGER_CST
3671 && !TREE_OVERFLOW (vr0->max))
3673 mini = prec - 1 - tree_floor_log2 (vr0->max);
3674 if (mini == prec)
3675 break;
3678 if (mini == -2)
3679 break;
3680 goto bitop_builtin;
3681 /* __builtin_ctz* return [0, prec-1], except for
3682 when the argument is 0, but that is undefined behavior.
3683 If there is a ctz optab for this mode and
3684 CTZ_DEFINED_VALUE_AT_ZERO, include that in the range,
3685 otherwise just assume 0 won't be seen. */
3686 CASE_INT_FN (BUILT_IN_CTZ):
3687 arg = gimple_call_arg (stmt, 0);
3688 prec = TYPE_PRECISION (TREE_TYPE (arg));
3689 mini = 0;
3690 maxi = prec - 1;
3691 if (optab_handler (ctz_optab, TYPE_MODE (TREE_TYPE (arg)))
3692 != CODE_FOR_nothing
3693 && CTZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (TREE_TYPE (arg)),
3694 zerov))
3696 /* Handle only the two common values. */
3697 if (zerov == -1)
3698 mini = -1;
3699 else if (zerov == prec)
3700 maxi = prec;
3701 else
3702 /* Magic value to give up, unless vr0 proves
3703 arg is non-zero. */
3704 mini = -2;
3706 if (TREE_CODE (arg) == SSA_NAME)
3708 value_range_t *vr0 = get_value_range (arg);
3709 /* If arg is non-zero, then use [0, prec - 1]. */
3710 if (((vr0->type == VR_RANGE
3711 && integer_nonzerop (vr0->min))
3712 || (vr0->type == VR_ANTI_RANGE
3713 && integer_zerop (vr0->min)))
3714 && !TREE_OVERFLOW (vr0->min))
3716 mini = 0;
3717 maxi = prec - 1;
3719 /* If some high bits are known to be zero,
3720 we can decrease the result maximum. */
3721 if (vr0->type == VR_RANGE
3722 && TREE_CODE (vr0->max) == INTEGER_CST
3723 && !TREE_OVERFLOW (vr0->max))
3725 maxi = tree_floor_log2 (vr0->max);
3726 /* For vr0 [0, 0] give up. */
3727 if (maxi == -1)
3728 break;
3731 if (mini == -2)
3732 break;
3733 goto bitop_builtin;
3734 /* __builtin_clrsb* returns [0, prec-1]. */
3735 CASE_INT_FN (BUILT_IN_CLRSB):
3736 arg = gimple_call_arg (stmt, 0);
3737 prec = TYPE_PRECISION (TREE_TYPE (arg));
3738 mini = 0;
3739 maxi = prec - 1;
3740 goto bitop_builtin;
3741 bitop_builtin:
3742 set_value_range (vr, VR_RANGE, build_int_cst (type, mini),
3743 build_int_cst (type, maxi), NULL);
3744 return;
3745 default:
3746 break;
3749 if (INTEGRAL_TYPE_P (type)
3750 && gimple_stmt_nonnegative_warnv_p (stmt, &sop))
3751 set_value_range_to_nonnegative (vr, type,
3752 sop || stmt_overflow_infinity (stmt));
3753 else if (vrp_stmt_computes_nonzero (stmt, &sop)
3754 && !sop)
3755 set_value_range_to_nonnull (vr, type);
3756 else
3757 set_value_range_to_varying (vr);
3761 /* Try to compute a useful range out of assignment STMT and store it
3762 in *VR. */
3764 static void
3765 extract_range_from_assignment (value_range_t *vr, gimple stmt)
3767 enum tree_code code = gimple_assign_rhs_code (stmt);
3769 if (code == ASSERT_EXPR)
3770 extract_range_from_assert (vr, gimple_assign_rhs1 (stmt));
3771 else if (code == SSA_NAME)
3772 extract_range_from_ssa_name (vr, gimple_assign_rhs1 (stmt));
3773 else if (TREE_CODE_CLASS (code) == tcc_binary)
3774 extract_range_from_binary_expr (vr, gimple_assign_rhs_code (stmt),
3775 gimple_expr_type (stmt),
3776 gimple_assign_rhs1 (stmt),
3777 gimple_assign_rhs2 (stmt));
3778 else if (TREE_CODE_CLASS (code) == tcc_unary)
3779 extract_range_from_unary_expr (vr, gimple_assign_rhs_code (stmt),
3780 gimple_expr_type (stmt),
3781 gimple_assign_rhs1 (stmt));
3782 else if (code == COND_EXPR)
3783 extract_range_from_cond_expr (vr, stmt);
3784 else if (TREE_CODE_CLASS (code) == tcc_comparison)
3785 extract_range_from_comparison (vr, gimple_assign_rhs_code (stmt),
3786 gimple_expr_type (stmt),
3787 gimple_assign_rhs1 (stmt),
3788 gimple_assign_rhs2 (stmt));
3789 else if (get_gimple_rhs_class (code) == GIMPLE_SINGLE_RHS
3790 && is_gimple_min_invariant (gimple_assign_rhs1 (stmt)))
3791 set_value_range_to_value (vr, gimple_assign_rhs1 (stmt), NULL);
3792 else
3793 set_value_range_to_varying (vr);
3795 if (vr->type == VR_VARYING)
3796 extract_range_basic (vr, stmt);
3799 /* Given a range VR, a LOOP and a variable VAR, determine whether it
3800 would be profitable to adjust VR using scalar evolution information
3801 for VAR. If so, update VR with the new limits. */
3803 static void
3804 adjust_range_with_scev (value_range_t *vr, struct loop *loop,
3805 gimple stmt, tree var)
3807 tree init, step, chrec, tmin, tmax, min, max, type, tem;
3808 enum ev_direction dir;
3810 /* TODO. Don't adjust anti-ranges. An anti-range may provide
3811 better opportunities than a regular range, but I'm not sure. */
3812 if (vr->type == VR_ANTI_RANGE)
3813 return;
3815 chrec = instantiate_parameters (loop, analyze_scalar_evolution (loop, var));
3817 /* Like in PR19590, scev can return a constant function. */
3818 if (is_gimple_min_invariant (chrec))
3820 set_value_range_to_value (vr, chrec, vr->equiv);
3821 return;
3824 if (TREE_CODE (chrec) != POLYNOMIAL_CHREC)
3825 return;
3827 init = initial_condition_in_loop_num (chrec, loop->num);
3828 tem = op_with_constant_singleton_value_range (init);
3829 if (tem)
3830 init = tem;
3831 step = evolution_part_in_loop_num (chrec, loop->num);
3832 tem = op_with_constant_singleton_value_range (step);
3833 if (tem)
3834 step = tem;
3836 /* If STEP is symbolic, we can't know whether INIT will be the
3837 minimum or maximum value in the range. Also, unless INIT is
3838 a simple expression, compare_values and possibly other functions
3839 in tree-vrp won't be able to handle it. */
3840 if (step == NULL_TREE
3841 || !is_gimple_min_invariant (step)
3842 || !valid_value_p (init))
3843 return;
3845 dir = scev_direction (chrec);
3846 if (/* Do not adjust ranges if we do not know whether the iv increases
3847 or decreases, ... */
3848 dir == EV_DIR_UNKNOWN
3849 /* ... or if it may wrap. */
3850 || scev_probably_wraps_p (init, step, stmt, get_chrec_loop (chrec),
3851 true))
3852 return;
3854 /* We use TYPE_MIN_VALUE and TYPE_MAX_VALUE here instead of
3855 negative_overflow_infinity and positive_overflow_infinity,
3856 because we have concluded that the loop probably does not
3857 wrap. */
3859 type = TREE_TYPE (var);
3860 if (POINTER_TYPE_P (type) || !TYPE_MIN_VALUE (type))
3861 tmin = lower_bound_in_type (type, type);
3862 else
3863 tmin = TYPE_MIN_VALUE (type);
3864 if (POINTER_TYPE_P (type) || !TYPE_MAX_VALUE (type))
3865 tmax = upper_bound_in_type (type, type);
3866 else
3867 tmax = TYPE_MAX_VALUE (type);
3869 /* Try to use estimated number of iterations for the loop to constrain the
3870 final value in the evolution. */
3871 if (TREE_CODE (step) == INTEGER_CST
3872 && is_gimple_val (init)
3873 && (TREE_CODE (init) != SSA_NAME
3874 || get_value_range (init)->type == VR_RANGE))
3876 double_int nit;
3878 /* We are only entering here for loop header PHI nodes, so using
3879 the number of latch executions is the correct thing to use. */
3880 if (max_loop_iterations (loop, &nit))
3882 value_range_t maxvr = VR_INITIALIZER;
3883 double_int dtmp;
3884 bool unsigned_p = TYPE_UNSIGNED (TREE_TYPE (step));
3885 bool overflow = false;
3887 dtmp = tree_to_double_int (step)
3888 .mul_with_sign (nit, unsigned_p, &overflow);
3889 /* If the multiplication overflowed we can't do a meaningful
3890 adjustment. Likewise if the result doesn't fit in the type
3891 of the induction variable. For a signed type we have to
3892 check whether the result has the expected signedness which
3893 is that of the step as number of iterations is unsigned. */
3894 if (!overflow
3895 && double_int_fits_to_tree_p (TREE_TYPE (init), dtmp)
3896 && (unsigned_p
3897 || ((dtmp.high ^ TREE_INT_CST_HIGH (step)) >= 0)))
3899 tem = double_int_to_tree (TREE_TYPE (init), dtmp);
3900 extract_range_from_binary_expr (&maxvr, PLUS_EXPR,
3901 TREE_TYPE (init), init, tem);
3902 /* Likewise if the addition did. */
3903 if (maxvr.type == VR_RANGE)
3905 tmin = maxvr.min;
3906 tmax = maxvr.max;
3912 if (vr->type == VR_VARYING || vr->type == VR_UNDEFINED)
3914 min = tmin;
3915 max = tmax;
3917 /* For VARYING or UNDEFINED ranges, just about anything we get
3918 from scalar evolutions should be better. */
3920 if (dir == EV_DIR_DECREASES)
3921 max = init;
3922 else
3923 min = init;
3925 /* If we would create an invalid range, then just assume we
3926 know absolutely nothing. This may be over-conservative,
3927 but it's clearly safe, and should happen only in unreachable
3928 parts of code, or for invalid programs. */
3929 if (compare_values (min, max) == 1)
3930 return;
3932 set_value_range (vr, VR_RANGE, min, max, vr->equiv);
3934 else if (vr->type == VR_RANGE)
3936 min = vr->min;
3937 max = vr->max;
3939 if (dir == EV_DIR_DECREASES)
3941 /* INIT is the maximum value. If INIT is lower than VR->MAX
3942 but no smaller than VR->MIN, set VR->MAX to INIT. */
3943 if (compare_values (init, max) == -1)
3944 max = init;
3946 /* According to the loop information, the variable does not
3947 overflow. If we think it does, probably because of an
3948 overflow due to arithmetic on a different INF value,
3949 reset now. */
3950 if (is_negative_overflow_infinity (min)
3951 || compare_values (min, tmin) == -1)
3952 min = tmin;
3955 else
3957 /* If INIT is bigger than VR->MIN, set VR->MIN to INIT. */
3958 if (compare_values (init, min) == 1)
3959 min = init;
3961 if (is_positive_overflow_infinity (max)
3962 || compare_values (tmax, max) == -1)
3963 max = tmax;
3966 /* If we just created an invalid range with the minimum
3967 greater than the maximum, we fail conservatively.
3968 This should happen only in unreachable
3969 parts of code, or for invalid programs. */
3970 if (compare_values (min, max) == 1)
3971 return;
3973 set_value_range (vr, VR_RANGE, min, max, vr->equiv);
3977 /* Return true if VAR may overflow at STMT. This checks any available
3978 loop information to see if we can determine that VAR does not
3979 overflow. */
3981 static bool
3982 vrp_var_may_overflow (tree var, gimple stmt)
3984 struct loop *l;
3985 tree chrec, init, step;
3987 if (current_loops == NULL)
3988 return true;
3990 l = loop_containing_stmt (stmt);
3991 if (l == NULL
3992 || !loop_outer (l))
3993 return true;
3995 chrec = instantiate_parameters (l, analyze_scalar_evolution (l, var));
3996 if (TREE_CODE (chrec) != POLYNOMIAL_CHREC)
3997 return true;
3999 init = initial_condition_in_loop_num (chrec, l->num);
4000 step = evolution_part_in_loop_num (chrec, l->num);
4002 if (step == NULL_TREE
4003 || !is_gimple_min_invariant (step)
4004 || !valid_value_p (init))
4005 return true;
4007 /* If we get here, we know something useful about VAR based on the
4008 loop information. If it wraps, it may overflow. */
4010 if (scev_probably_wraps_p (init, step, stmt, get_chrec_loop (chrec),
4011 true))
4012 return true;
4014 if (dump_file && (dump_flags & TDF_DETAILS) != 0)
4016 print_generic_expr (dump_file, var, 0);
4017 fprintf (dump_file, ": loop information indicates does not overflow\n");
4020 return false;
4024 /* Given two numeric value ranges VR0, VR1 and a comparison code COMP:
4026 - Return BOOLEAN_TRUE_NODE if VR0 COMP VR1 always returns true for
4027 all the values in the ranges.
4029 - Return BOOLEAN_FALSE_NODE if the comparison always returns false.
4031 - Return NULL_TREE if it is not always possible to determine the
4032 value of the comparison.
4034 Also set *STRICT_OVERFLOW_P to indicate whether a range with an
4035 overflow infinity was used in the test. */
4038 static tree
4039 compare_ranges (enum tree_code comp, value_range_t *vr0, value_range_t *vr1,
4040 bool *strict_overflow_p)
4042 /* VARYING or UNDEFINED ranges cannot be compared. */
4043 if (vr0->type == VR_VARYING
4044 || vr0->type == VR_UNDEFINED
4045 || vr1->type == VR_VARYING
4046 || vr1->type == VR_UNDEFINED)
4047 return NULL_TREE;
4049 /* Anti-ranges need to be handled separately. */
4050 if (vr0->type == VR_ANTI_RANGE || vr1->type == VR_ANTI_RANGE)
4052 /* If both are anti-ranges, then we cannot compute any
4053 comparison. */
4054 if (vr0->type == VR_ANTI_RANGE && vr1->type == VR_ANTI_RANGE)
4055 return NULL_TREE;
4057 /* These comparisons are never statically computable. */
4058 if (comp == GT_EXPR
4059 || comp == GE_EXPR
4060 || comp == LT_EXPR
4061 || comp == LE_EXPR)
4062 return NULL_TREE;
4064 /* Equality can be computed only between a range and an
4065 anti-range. ~[VAL1, VAL2] == [VAL1, VAL2] is always false. */
4066 if (vr0->type == VR_RANGE)
4068 /* To simplify processing, make VR0 the anti-range. */
4069 value_range_t *tmp = vr0;
4070 vr0 = vr1;
4071 vr1 = tmp;
4074 gcc_assert (comp == NE_EXPR || comp == EQ_EXPR);
4076 if (compare_values_warnv (vr0->min, vr1->min, strict_overflow_p) == 0
4077 && compare_values_warnv (vr0->max, vr1->max, strict_overflow_p) == 0)
4078 return (comp == NE_EXPR) ? boolean_true_node : boolean_false_node;
4080 return NULL_TREE;
4083 if (!usable_range_p (vr0, strict_overflow_p)
4084 || !usable_range_p (vr1, strict_overflow_p))
4085 return NULL_TREE;
4087 /* Simplify processing. If COMP is GT_EXPR or GE_EXPR, switch the
4088 operands around and change the comparison code. */
4089 if (comp == GT_EXPR || comp == GE_EXPR)
4091 value_range_t *tmp;
4092 comp = (comp == GT_EXPR) ? LT_EXPR : LE_EXPR;
4093 tmp = vr0;
4094 vr0 = vr1;
4095 vr1 = tmp;
4098 if (comp == EQ_EXPR)
4100 /* Equality may only be computed if both ranges represent
4101 exactly one value. */
4102 if (compare_values_warnv (vr0->min, vr0->max, strict_overflow_p) == 0
4103 && compare_values_warnv (vr1->min, vr1->max, strict_overflow_p) == 0)
4105 int cmp_min = compare_values_warnv (vr0->min, vr1->min,
4106 strict_overflow_p);
4107 int cmp_max = compare_values_warnv (vr0->max, vr1->max,
4108 strict_overflow_p);
4109 if (cmp_min == 0 && cmp_max == 0)
4110 return boolean_true_node;
4111 else if (cmp_min != -2 && cmp_max != -2)
4112 return boolean_false_node;
4114 /* If [V0_MIN, V1_MAX] < [V1_MIN, V1_MAX] then V0 != V1. */
4115 else if (compare_values_warnv (vr0->min, vr1->max,
4116 strict_overflow_p) == 1
4117 || compare_values_warnv (vr1->min, vr0->max,
4118 strict_overflow_p) == 1)
4119 return boolean_false_node;
4121 return NULL_TREE;
4123 else if (comp == NE_EXPR)
4125 int cmp1, cmp2;
4127 /* If VR0 is completely to the left or completely to the right
4128 of VR1, they are always different. Notice that we need to
4129 make sure that both comparisons yield similar results to
4130 avoid comparing values that cannot be compared at
4131 compile-time. */
4132 cmp1 = compare_values_warnv (vr0->max, vr1->min, strict_overflow_p);
4133 cmp2 = compare_values_warnv (vr0->min, vr1->max, strict_overflow_p);
4134 if ((cmp1 == -1 && cmp2 == -1) || (cmp1 == 1 && cmp2 == 1))
4135 return boolean_true_node;
4137 /* If VR0 and VR1 represent a single value and are identical,
4138 return false. */
4139 else if (compare_values_warnv (vr0->min, vr0->max,
4140 strict_overflow_p) == 0
4141 && compare_values_warnv (vr1->min, vr1->max,
4142 strict_overflow_p) == 0
4143 && compare_values_warnv (vr0->min, vr1->min,
4144 strict_overflow_p) == 0
4145 && compare_values_warnv (vr0->max, vr1->max,
4146 strict_overflow_p) == 0)
4147 return boolean_false_node;
4149 /* Otherwise, they may or may not be different. */
4150 else
4151 return NULL_TREE;
4153 else if (comp == LT_EXPR || comp == LE_EXPR)
4155 int tst;
4157 /* If VR0 is to the left of VR1, return true. */
4158 tst = compare_values_warnv (vr0->max, vr1->min, strict_overflow_p);
4159 if ((comp == LT_EXPR && tst == -1)
4160 || (comp == LE_EXPR && (tst == -1 || tst == 0)))
4162 if (overflow_infinity_range_p (vr0)
4163 || overflow_infinity_range_p (vr1))
4164 *strict_overflow_p = true;
4165 return boolean_true_node;
4168 /* If VR0 is to the right of VR1, return false. */
4169 tst = compare_values_warnv (vr0->min, vr1->max, strict_overflow_p);
4170 if ((comp == LT_EXPR && (tst == 0 || tst == 1))
4171 || (comp == LE_EXPR && tst == 1))
4173 if (overflow_infinity_range_p (vr0)
4174 || overflow_infinity_range_p (vr1))
4175 *strict_overflow_p = true;
4176 return boolean_false_node;
4179 /* Otherwise, we don't know. */
4180 return NULL_TREE;
4183 gcc_unreachable ();
4187 /* Given a value range VR, a value VAL and a comparison code COMP, return
4188 BOOLEAN_TRUE_NODE if VR COMP VAL always returns true for all the
4189 values in VR. Return BOOLEAN_FALSE_NODE if the comparison
4190 always returns false. Return NULL_TREE if it is not always
4191 possible to determine the value of the comparison. Also set
4192 *STRICT_OVERFLOW_P to indicate whether a range with an overflow
4193 infinity was used in the test. */
4195 static tree
4196 compare_range_with_value (enum tree_code comp, value_range_t *vr, tree val,
4197 bool *strict_overflow_p)
4199 if (vr->type == VR_VARYING || vr->type == VR_UNDEFINED)
4200 return NULL_TREE;
4202 /* Anti-ranges need to be handled separately. */
4203 if (vr->type == VR_ANTI_RANGE)
4205 /* For anti-ranges, the only predicates that we can compute at
4206 compile time are equality and inequality. */
4207 if (comp == GT_EXPR
4208 || comp == GE_EXPR
4209 || comp == LT_EXPR
4210 || comp == LE_EXPR)
4211 return NULL_TREE;
4213 /* ~[VAL_1, VAL_2] OP VAL is known if VAL_1 <= VAL <= VAL_2. */
4214 if (value_inside_range (val, vr->min, vr->max) == 1)
4215 return (comp == NE_EXPR) ? boolean_true_node : boolean_false_node;
4217 return NULL_TREE;
4220 if (!usable_range_p (vr, strict_overflow_p))
4221 return NULL_TREE;
4223 if (comp == EQ_EXPR)
4225 /* EQ_EXPR may only be computed if VR represents exactly
4226 one value. */
4227 if (compare_values_warnv (vr->min, vr->max, strict_overflow_p) == 0)
4229 int cmp = compare_values_warnv (vr->min, val, strict_overflow_p);
4230 if (cmp == 0)
4231 return boolean_true_node;
4232 else if (cmp == -1 || cmp == 1 || cmp == 2)
4233 return boolean_false_node;
4235 else if (compare_values_warnv (val, vr->min, strict_overflow_p) == -1
4236 || compare_values_warnv (vr->max, val, strict_overflow_p) == -1)
4237 return boolean_false_node;
4239 return NULL_TREE;
4241 else if (comp == NE_EXPR)
4243 /* If VAL is not inside VR, then they are always different. */
4244 if (compare_values_warnv (vr->max, val, strict_overflow_p) == -1
4245 || compare_values_warnv (vr->min, val, strict_overflow_p) == 1)
4246 return boolean_true_node;
4248 /* If VR represents exactly one value equal to VAL, then return
4249 false. */
4250 if (compare_values_warnv (vr->min, vr->max, strict_overflow_p) == 0
4251 && compare_values_warnv (vr->min, val, strict_overflow_p) == 0)
4252 return boolean_false_node;
4254 /* Otherwise, they may or may not be different. */
4255 return NULL_TREE;
4257 else if (comp == LT_EXPR || comp == LE_EXPR)
4259 int tst;
4261 /* If VR is to the left of VAL, return true. */
4262 tst = compare_values_warnv (vr->max, val, strict_overflow_p);
4263 if ((comp == LT_EXPR && tst == -1)
4264 || (comp == LE_EXPR && (tst == -1 || tst == 0)))
4266 if (overflow_infinity_range_p (vr))
4267 *strict_overflow_p = true;
4268 return boolean_true_node;
4271 /* If VR is to the right of VAL, return false. */
4272 tst = compare_values_warnv (vr->min, val, strict_overflow_p);
4273 if ((comp == LT_EXPR && (tst == 0 || tst == 1))
4274 || (comp == LE_EXPR && tst == 1))
4276 if (overflow_infinity_range_p (vr))
4277 *strict_overflow_p = true;
4278 return boolean_false_node;
4281 /* Otherwise, we don't know. */
4282 return NULL_TREE;
4284 else if (comp == GT_EXPR || comp == GE_EXPR)
4286 int tst;
4288 /* If VR is to the right of VAL, return true. */
4289 tst = compare_values_warnv (vr->min, val, strict_overflow_p);
4290 if ((comp == GT_EXPR && tst == 1)
4291 || (comp == GE_EXPR && (tst == 0 || tst == 1)))
4293 if (overflow_infinity_range_p (vr))
4294 *strict_overflow_p = true;
4295 return boolean_true_node;
4298 /* If VR is to the left of VAL, return false. */
4299 tst = compare_values_warnv (vr->max, val, strict_overflow_p);
4300 if ((comp == GT_EXPR && (tst == -1 || tst == 0))
4301 || (comp == GE_EXPR && tst == -1))
4303 if (overflow_infinity_range_p (vr))
4304 *strict_overflow_p = true;
4305 return boolean_false_node;
4308 /* Otherwise, we don't know. */
4309 return NULL_TREE;
4312 gcc_unreachable ();
4316 /* Debugging dumps. */
4318 void dump_value_range (FILE *, value_range_t *);
4319 void debug_value_range (value_range_t *);
4320 void dump_all_value_ranges (FILE *);
4321 void debug_all_value_ranges (void);
4322 void dump_vr_equiv (FILE *, bitmap);
4323 void debug_vr_equiv (bitmap);
4326 /* Dump value range VR to FILE. */
4328 void
4329 dump_value_range (FILE *file, value_range_t *vr)
4331 if (vr == NULL)
4332 fprintf (file, "[]");
4333 else if (vr->type == VR_UNDEFINED)
4334 fprintf (file, "UNDEFINED");
4335 else if (vr->type == VR_RANGE || vr->type == VR_ANTI_RANGE)
4337 tree type = TREE_TYPE (vr->min);
4339 fprintf (file, "%s[", (vr->type == VR_ANTI_RANGE) ? "~" : "");
4341 if (is_negative_overflow_infinity (vr->min))
4342 fprintf (file, "-INF(OVF)");
4343 else if (INTEGRAL_TYPE_P (type)
4344 && !TYPE_UNSIGNED (type)
4345 && vrp_val_is_min (vr->min))
4346 fprintf (file, "-INF");
4347 else
4348 print_generic_expr (file, vr->min, 0);
4350 fprintf (file, ", ");
4352 if (is_positive_overflow_infinity (vr->max))
4353 fprintf (file, "+INF(OVF)");
4354 else if (INTEGRAL_TYPE_P (type)
4355 && vrp_val_is_max (vr->max))
4356 fprintf (file, "+INF");
4357 else
4358 print_generic_expr (file, vr->max, 0);
4360 fprintf (file, "]");
4362 if (vr->equiv)
4364 bitmap_iterator bi;
4365 unsigned i, c = 0;
4367 fprintf (file, " EQUIVALENCES: { ");
4369 EXECUTE_IF_SET_IN_BITMAP (vr->equiv, 0, i, bi)
4371 print_generic_expr (file, ssa_name (i), 0);
4372 fprintf (file, " ");
4373 c++;
4376 fprintf (file, "} (%u elements)", c);
4379 else if (vr->type == VR_VARYING)
4380 fprintf (file, "VARYING");
4381 else
4382 fprintf (file, "INVALID RANGE");
4386 /* Dump value range VR to stderr. */
4388 DEBUG_FUNCTION void
4389 debug_value_range (value_range_t *vr)
4391 dump_value_range (stderr, vr);
4392 fprintf (stderr, "\n");
4396 /* Dump value ranges of all SSA_NAMEs to FILE. */
4398 void
4399 dump_all_value_ranges (FILE *file)
4401 size_t i;
4403 for (i = 0; i < num_vr_values; i++)
4405 if (vr_value[i])
4407 print_generic_expr (file, ssa_name (i), 0);
4408 fprintf (file, ": ");
4409 dump_value_range (file, vr_value[i]);
4410 fprintf (file, "\n");
4414 fprintf (file, "\n");
4418 /* Dump all value ranges to stderr. */
4420 DEBUG_FUNCTION void
4421 debug_all_value_ranges (void)
4423 dump_all_value_ranges (stderr);
4427 /* Given a COND_EXPR COND of the form 'V OP W', and an SSA name V,
4428 create a new SSA name N and return the assertion assignment
4429 'V = ASSERT_EXPR <V, V OP W>'. */
4431 static gimple
4432 build_assert_expr_for (tree cond, tree v)
4434 tree a;
4435 gimple assertion;
4437 gcc_assert (TREE_CODE (v) == SSA_NAME
4438 && COMPARISON_CLASS_P (cond));
4440 a = build2 (ASSERT_EXPR, TREE_TYPE (v), v, cond);
4441 assertion = gimple_build_assign (NULL_TREE, a);
4443 /* The new ASSERT_EXPR, creates a new SSA name that replaces the
4444 operand of the ASSERT_EXPR. Create it so the new name and the old one
4445 are registered in the replacement table so that we can fix the SSA web
4446 after adding all the ASSERT_EXPRs. */
4447 create_new_def_for (v, assertion, NULL);
4449 return assertion;
4453 /* Return false if EXPR is a predicate expression involving floating
4454 point values. */
4456 static inline bool
4457 fp_predicate (gimple stmt)
4459 GIMPLE_CHECK (stmt, GIMPLE_COND);
4461 return FLOAT_TYPE_P (TREE_TYPE (gimple_cond_lhs (stmt)));
4465 /* If OP can be inferred to be non-zero after STMT executes, return true. */
4467 static bool
4468 infer_nonnull_range (gimple stmt, tree op)
4470 /* We can only assume that a pointer dereference will yield
4471 non-NULL if -fdelete-null-pointer-checks is enabled. */
4472 if (!flag_delete_null_pointer_checks
4473 || !POINTER_TYPE_P (TREE_TYPE (op))
4474 || gimple_code (stmt) == GIMPLE_ASM)
4475 return false;
4477 unsigned num_uses, num_loads, num_stores;
4479 count_uses_and_derefs (op, stmt, &num_uses, &num_loads, &num_stores);
4480 if (num_loads + num_stores > 0)
4481 return true;
4483 if (gimple_code (stmt) == GIMPLE_CALL)
4485 tree fntype = gimple_call_fntype (stmt);
4486 tree attrs = TYPE_ATTRIBUTES (fntype);
4487 for (; attrs; attrs = TREE_CHAIN (attrs))
4489 attrs = lookup_attribute ("nonnull", attrs);
4491 /* If "nonnull" wasn't specified, we know nothing about
4492 the argument. */
4493 if (attrs == NULL_TREE)
4494 return false;
4496 /* If "nonnull" applies to all the arguments, then ARG
4497 is non-null. */
4498 if (TREE_VALUE (attrs) == NULL_TREE)
4499 return true;
4501 /* Now see if op appears in the nonnull list. */
4502 for (tree t = TREE_VALUE (attrs); t; t = TREE_CHAIN (t))
4504 int idx = TREE_INT_CST_LOW (TREE_VALUE (t)) - 1;
4505 tree arg = gimple_call_arg (stmt, idx);
4506 if (op == arg)
4507 return true;
4512 return false;
4515 /* If the range of values taken by OP can be inferred after STMT executes,
4516 return the comparison code (COMP_CODE_P) and value (VAL_P) that
4517 describes the inferred range. Return true if a range could be
4518 inferred. */
4520 static bool
4521 infer_value_range (gimple stmt, tree op, enum tree_code *comp_code_p, tree *val_p)
4523 *val_p = NULL_TREE;
4524 *comp_code_p = ERROR_MARK;
4526 /* Do not attempt to infer anything in names that flow through
4527 abnormal edges. */
4528 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op))
4529 return false;
4531 /* Similarly, don't infer anything from statements that may throw
4532 exceptions. ??? Relax this requirement? */
4533 if (stmt_could_throw_p (stmt))
4534 return false;
4536 /* If STMT is the last statement of a basic block with no
4537 successors, there is no point inferring anything about any of its
4538 operands. We would not be able to find a proper insertion point
4539 for the assertion, anyway. */
4540 if (stmt_ends_bb_p (stmt) && EDGE_COUNT (gimple_bb (stmt)->succs) == 0)
4541 return false;
4543 if (infer_nonnull_range (stmt, op))
4545 *val_p = build_int_cst (TREE_TYPE (op), 0);
4546 *comp_code_p = NE_EXPR;
4547 return true;
4550 return false;
4554 void dump_asserts_for (FILE *, tree);
4555 void debug_asserts_for (tree);
4556 void dump_all_asserts (FILE *);
4557 void debug_all_asserts (void);
4559 /* Dump all the registered assertions for NAME to FILE. */
4561 void
4562 dump_asserts_for (FILE *file, tree name)
4564 assert_locus_t loc;
4566 fprintf (file, "Assertions to be inserted for ");
4567 print_generic_expr (file, name, 0);
4568 fprintf (file, "\n");
4570 loc = asserts_for[SSA_NAME_VERSION (name)];
4571 while (loc)
4573 fprintf (file, "\t");
4574 print_gimple_stmt (file, gsi_stmt (loc->si), 0, 0);
4575 fprintf (file, "\n\tBB #%d", loc->bb->index);
4576 if (loc->e)
4578 fprintf (file, "\n\tEDGE %d->%d", loc->e->src->index,
4579 loc->e->dest->index);
4580 dump_edge_info (file, loc->e, dump_flags, 0);
4582 fprintf (file, "\n\tPREDICATE: ");
4583 print_generic_expr (file, name, 0);
4584 fprintf (file, " %s ", tree_code_name[(int)loc->comp_code]);
4585 print_generic_expr (file, loc->val, 0);
4586 fprintf (file, "\n\n");
4587 loc = loc->next;
4590 fprintf (file, "\n");
4594 /* Dump all the registered assertions for NAME to stderr. */
4596 DEBUG_FUNCTION void
4597 debug_asserts_for (tree name)
4599 dump_asserts_for (stderr, name);
4603 /* Dump all the registered assertions for all the names to FILE. */
4605 void
4606 dump_all_asserts (FILE *file)
4608 unsigned i;
4609 bitmap_iterator bi;
4611 fprintf (file, "\nASSERT_EXPRs to be inserted\n\n");
4612 EXECUTE_IF_SET_IN_BITMAP (need_assert_for, 0, i, bi)
4613 dump_asserts_for (file, ssa_name (i));
4614 fprintf (file, "\n");
4618 /* Dump all the registered assertions for all the names to stderr. */
4620 DEBUG_FUNCTION void
4621 debug_all_asserts (void)
4623 dump_all_asserts (stderr);
4627 /* If NAME doesn't have an ASSERT_EXPR registered for asserting
4628 'EXPR COMP_CODE VAL' at a location that dominates block BB or
4629 E->DEST, then register this location as a possible insertion point
4630 for ASSERT_EXPR <NAME, EXPR COMP_CODE VAL>.
4632 BB, E and SI provide the exact insertion point for the new
4633 ASSERT_EXPR. If BB is NULL, then the ASSERT_EXPR is to be inserted
4634 on edge E. Otherwise, if E is NULL, the ASSERT_EXPR is inserted on
4635 BB. If SI points to a COND_EXPR or a SWITCH_EXPR statement, then E
4636 must not be NULL. */
4638 static void
4639 register_new_assert_for (tree name, tree expr,
4640 enum tree_code comp_code,
4641 tree val,
4642 basic_block bb,
4643 edge e,
4644 gimple_stmt_iterator si)
4646 assert_locus_t n, loc, last_loc;
4647 basic_block dest_bb;
4649 gcc_checking_assert (bb == NULL || e == NULL);
4651 if (e == NULL)
4652 gcc_checking_assert (gimple_code (gsi_stmt (si)) != GIMPLE_COND
4653 && gimple_code (gsi_stmt (si)) != GIMPLE_SWITCH);
4655 /* Never build an assert comparing against an integer constant with
4656 TREE_OVERFLOW set. This confuses our undefined overflow warning
4657 machinery. */
4658 if (TREE_CODE (val) == INTEGER_CST
4659 && TREE_OVERFLOW (val))
4660 val = build_int_cst_wide (TREE_TYPE (val),
4661 TREE_INT_CST_LOW (val), TREE_INT_CST_HIGH (val));
4663 /* The new assertion A will be inserted at BB or E. We need to
4664 determine if the new location is dominated by a previously
4665 registered location for A. If we are doing an edge insertion,
4666 assume that A will be inserted at E->DEST. Note that this is not
4667 necessarily true.
4669 If E is a critical edge, it will be split. But even if E is
4670 split, the new block will dominate the same set of blocks that
4671 E->DEST dominates.
4673 The reverse, however, is not true, blocks dominated by E->DEST
4674 will not be dominated by the new block created to split E. So,
4675 if the insertion location is on a critical edge, we will not use
4676 the new location to move another assertion previously registered
4677 at a block dominated by E->DEST. */
4678 dest_bb = (bb) ? bb : e->dest;
4680 /* If NAME already has an ASSERT_EXPR registered for COMP_CODE and
4681 VAL at a block dominating DEST_BB, then we don't need to insert a new
4682 one. Similarly, if the same assertion already exists at a block
4683 dominated by DEST_BB and the new location is not on a critical
4684 edge, then update the existing location for the assertion (i.e.,
4685 move the assertion up in the dominance tree).
4687 Note, this is implemented as a simple linked list because there
4688 should not be more than a handful of assertions registered per
4689 name. If this becomes a performance problem, a table hashed by
4690 COMP_CODE and VAL could be implemented. */
4691 loc = asserts_for[SSA_NAME_VERSION (name)];
4692 last_loc = loc;
4693 while (loc)
4695 if (loc->comp_code == comp_code
4696 && (loc->val == val
4697 || operand_equal_p (loc->val, val, 0))
4698 && (loc->expr == expr
4699 || operand_equal_p (loc->expr, expr, 0)))
4701 /* If E is not a critical edge and DEST_BB
4702 dominates the existing location for the assertion, move
4703 the assertion up in the dominance tree by updating its
4704 location information. */
4705 if ((e == NULL || !EDGE_CRITICAL_P (e))
4706 && dominated_by_p (CDI_DOMINATORS, loc->bb, dest_bb))
4708 loc->bb = dest_bb;
4709 loc->e = e;
4710 loc->si = si;
4711 return;
4715 /* Update the last node of the list and move to the next one. */
4716 last_loc = loc;
4717 loc = loc->next;
4720 /* If we didn't find an assertion already registered for
4721 NAME COMP_CODE VAL, add a new one at the end of the list of
4722 assertions associated with NAME. */
4723 n = XNEW (struct assert_locus_d);
4724 n->bb = dest_bb;
4725 n->e = e;
4726 n->si = si;
4727 n->comp_code = comp_code;
4728 n->val = val;
4729 n->expr = expr;
4730 n->next = NULL;
4732 if (last_loc)
4733 last_loc->next = n;
4734 else
4735 asserts_for[SSA_NAME_VERSION (name)] = n;
4737 bitmap_set_bit (need_assert_for, SSA_NAME_VERSION (name));
4740 /* (COND_OP0 COND_CODE COND_OP1) is a predicate which uses NAME.
4741 Extract a suitable test code and value and store them into *CODE_P and
4742 *VAL_P so the predicate is normalized to NAME *CODE_P *VAL_P.
4744 If no extraction was possible, return FALSE, otherwise return TRUE.
4746 If INVERT is true, then we invert the result stored into *CODE_P. */
4748 static bool
4749 extract_code_and_val_from_cond_with_ops (tree name, enum tree_code cond_code,
4750 tree cond_op0, tree cond_op1,
4751 bool invert, enum tree_code *code_p,
4752 tree *val_p)
4754 enum tree_code comp_code;
4755 tree val;
4757 /* Otherwise, we have a comparison of the form NAME COMP VAL
4758 or VAL COMP NAME. */
4759 if (name == cond_op1)
4761 /* If the predicate is of the form VAL COMP NAME, flip
4762 COMP around because we need to register NAME as the
4763 first operand in the predicate. */
4764 comp_code = swap_tree_comparison (cond_code);
4765 val = cond_op0;
4767 else
4769 /* The comparison is of the form NAME COMP VAL, so the
4770 comparison code remains unchanged. */
4771 comp_code = cond_code;
4772 val = cond_op1;
4775 /* Invert the comparison code as necessary. */
4776 if (invert)
4777 comp_code = invert_tree_comparison (comp_code, 0);
4779 /* VRP does not handle float types. */
4780 if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (val)))
4781 return false;
4783 /* Do not register always-false predicates.
4784 FIXME: this works around a limitation in fold() when dealing with
4785 enumerations. Given 'enum { N1, N2 } x;', fold will not
4786 fold 'if (x > N2)' to 'if (0)'. */
4787 if ((comp_code == GT_EXPR || comp_code == LT_EXPR)
4788 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
4790 tree min = TYPE_MIN_VALUE (TREE_TYPE (val));
4791 tree max = TYPE_MAX_VALUE (TREE_TYPE (val));
4793 if (comp_code == GT_EXPR
4794 && (!max
4795 || compare_values (val, max) == 0))
4796 return false;
4798 if (comp_code == LT_EXPR
4799 && (!min
4800 || compare_values (val, min) == 0))
4801 return false;
4803 *code_p = comp_code;
4804 *val_p = val;
4805 return true;
4808 /* Find out smallest RES where RES > VAL && (RES & MASK) == RES, if any
4809 (otherwise return VAL). VAL and MASK must be zero-extended for
4810 precision PREC. If SGNBIT is non-zero, first xor VAL with SGNBIT
4811 (to transform signed values into unsigned) and at the end xor
4812 SGNBIT back. */
4814 static double_int
4815 masked_increment (double_int val, double_int mask, double_int sgnbit,
4816 unsigned int prec)
4818 double_int bit = double_int_one, res;
4819 unsigned int i;
4821 val ^= sgnbit;
4822 for (i = 0; i < prec; i++, bit += bit)
4824 res = mask;
4825 if ((res & bit).is_zero ())
4826 continue;
4827 res = bit - double_int_one;
4828 res = (val + bit).and_not (res);
4829 res &= mask;
4830 if (res.ugt (val))
4831 return res ^ sgnbit;
4833 return val ^ sgnbit;
4836 /* Try to register an edge assertion for SSA name NAME on edge E for
4837 the condition COND contributing to the conditional jump pointed to by BSI.
4838 Invert the condition COND if INVERT is true.
4839 Return true if an assertion for NAME could be registered. */
4841 static bool
4842 register_edge_assert_for_2 (tree name, edge e, gimple_stmt_iterator bsi,
4843 enum tree_code cond_code,
4844 tree cond_op0, tree cond_op1, bool invert)
4846 tree val;
4847 enum tree_code comp_code;
4848 bool retval = false;
4850 if (!extract_code_and_val_from_cond_with_ops (name, cond_code,
4851 cond_op0,
4852 cond_op1,
4853 invert, &comp_code, &val))
4854 return false;
4856 /* Only register an ASSERT_EXPR if NAME was found in the sub-graph
4857 reachable from E. */
4858 if (live_on_edge (e, name)
4859 && !has_single_use (name))
4861 register_new_assert_for (name, name, comp_code, val, NULL, e, bsi);
4862 retval = true;
4865 /* In the case of NAME <= CST and NAME being defined as
4866 NAME = (unsigned) NAME2 + CST2 we can assert NAME2 >= -CST2
4867 and NAME2 <= CST - CST2. We can do the same for NAME > CST.
4868 This catches range and anti-range tests. */
4869 if ((comp_code == LE_EXPR
4870 || comp_code == GT_EXPR)
4871 && TREE_CODE (val) == INTEGER_CST
4872 && TYPE_UNSIGNED (TREE_TYPE (val)))
4874 gimple def_stmt = SSA_NAME_DEF_STMT (name);
4875 tree cst2 = NULL_TREE, name2 = NULL_TREE, name3 = NULL_TREE;
4877 /* Extract CST2 from the (optional) addition. */
4878 if (is_gimple_assign (def_stmt)
4879 && gimple_assign_rhs_code (def_stmt) == PLUS_EXPR)
4881 name2 = gimple_assign_rhs1 (def_stmt);
4882 cst2 = gimple_assign_rhs2 (def_stmt);
4883 if (TREE_CODE (name2) == SSA_NAME
4884 && TREE_CODE (cst2) == INTEGER_CST)
4885 def_stmt = SSA_NAME_DEF_STMT (name2);
4888 /* Extract NAME2 from the (optional) sign-changing cast. */
4889 if (gimple_assign_cast_p (def_stmt))
4891 if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt))
4892 && ! TYPE_UNSIGNED (TREE_TYPE (gimple_assign_rhs1 (def_stmt)))
4893 && (TYPE_PRECISION (gimple_expr_type (def_stmt))
4894 == TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (def_stmt)))))
4895 name3 = gimple_assign_rhs1 (def_stmt);
4898 /* If name3 is used later, create an ASSERT_EXPR for it. */
4899 if (name3 != NULL_TREE
4900 && TREE_CODE (name3) == SSA_NAME
4901 && (cst2 == NULL_TREE
4902 || TREE_CODE (cst2) == INTEGER_CST)
4903 && INTEGRAL_TYPE_P (TREE_TYPE (name3))
4904 && live_on_edge (e, name3)
4905 && !has_single_use (name3))
4907 tree tmp;
4909 /* Build an expression for the range test. */
4910 tmp = build1 (NOP_EXPR, TREE_TYPE (name), name3);
4911 if (cst2 != NULL_TREE)
4912 tmp = build2 (PLUS_EXPR, TREE_TYPE (name), tmp, cst2);
4914 if (dump_file)
4916 fprintf (dump_file, "Adding assert for ");
4917 print_generic_expr (dump_file, name3, 0);
4918 fprintf (dump_file, " from ");
4919 print_generic_expr (dump_file, tmp, 0);
4920 fprintf (dump_file, "\n");
4923 register_new_assert_for (name3, tmp, comp_code, val, NULL, e, bsi);
4925 retval = true;
4928 /* If name2 is used later, create an ASSERT_EXPR for it. */
4929 if (name2 != NULL_TREE
4930 && TREE_CODE (name2) == SSA_NAME
4931 && TREE_CODE (cst2) == INTEGER_CST
4932 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
4933 && live_on_edge (e, name2)
4934 && !has_single_use (name2))
4936 tree tmp;
4938 /* Build an expression for the range test. */
4939 tmp = name2;
4940 if (TREE_TYPE (name) != TREE_TYPE (name2))
4941 tmp = build1 (NOP_EXPR, TREE_TYPE (name), tmp);
4942 if (cst2 != NULL_TREE)
4943 tmp = build2 (PLUS_EXPR, TREE_TYPE (name), tmp, cst2);
4945 if (dump_file)
4947 fprintf (dump_file, "Adding assert for ");
4948 print_generic_expr (dump_file, name2, 0);
4949 fprintf (dump_file, " from ");
4950 print_generic_expr (dump_file, tmp, 0);
4951 fprintf (dump_file, "\n");
4954 register_new_assert_for (name2, tmp, comp_code, val, NULL, e, bsi);
4956 retval = true;
4960 /* In the case of post-in/decrement tests like if (i++) ... and uses
4961 of the in/decremented value on the edge the extra name we want to
4962 assert for is not on the def chain of the name compared. Instead
4963 it is in the set of use stmts. */
4964 if ((comp_code == NE_EXPR
4965 || comp_code == EQ_EXPR)
4966 && TREE_CODE (val) == INTEGER_CST)
4968 imm_use_iterator ui;
4969 gimple use_stmt;
4970 FOR_EACH_IMM_USE_STMT (use_stmt, ui, name)
4972 /* Cut off to use-stmts that are in the predecessor. */
4973 if (gimple_bb (use_stmt) != e->src)
4974 continue;
4976 if (!is_gimple_assign (use_stmt))
4977 continue;
4979 enum tree_code code = gimple_assign_rhs_code (use_stmt);
4980 if (code != PLUS_EXPR
4981 && code != MINUS_EXPR)
4982 continue;
4984 tree cst = gimple_assign_rhs2 (use_stmt);
4985 if (TREE_CODE (cst) != INTEGER_CST)
4986 continue;
4988 tree name2 = gimple_assign_lhs (use_stmt);
4989 if (live_on_edge (e, name2))
4991 cst = int_const_binop (code, val, cst);
4992 register_new_assert_for (name2, name2, comp_code, cst,
4993 NULL, e, bsi);
4994 retval = true;
4999 if (TREE_CODE_CLASS (comp_code) == tcc_comparison
5000 && TREE_CODE (val) == INTEGER_CST)
5002 gimple def_stmt = SSA_NAME_DEF_STMT (name);
5003 tree name2 = NULL_TREE, names[2], cst2 = NULL_TREE;
5004 tree val2 = NULL_TREE;
5005 double_int mask = double_int_zero;
5006 unsigned int prec = TYPE_PRECISION (TREE_TYPE (val));
5007 unsigned int nprec = prec;
5008 enum tree_code rhs_code = ERROR_MARK;
5010 if (is_gimple_assign (def_stmt))
5011 rhs_code = gimple_assign_rhs_code (def_stmt);
5013 /* Add asserts for NAME cmp CST and NAME being defined
5014 as NAME = (int) NAME2. */
5015 if (!TYPE_UNSIGNED (TREE_TYPE (val))
5016 && (comp_code == LE_EXPR || comp_code == LT_EXPR
5017 || comp_code == GT_EXPR || comp_code == GE_EXPR)
5018 && gimple_assign_cast_p (def_stmt))
5020 name2 = gimple_assign_rhs1 (def_stmt);
5021 if (CONVERT_EXPR_CODE_P (rhs_code)
5022 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
5023 && TYPE_UNSIGNED (TREE_TYPE (name2))
5024 && prec == TYPE_PRECISION (TREE_TYPE (name2))
5025 && (comp_code == LE_EXPR || comp_code == GT_EXPR
5026 || !tree_int_cst_equal (val,
5027 TYPE_MIN_VALUE (TREE_TYPE (val))))
5028 && live_on_edge (e, name2)
5029 && !has_single_use (name2))
5031 tree tmp, cst;
5032 enum tree_code new_comp_code = comp_code;
5034 cst = fold_convert (TREE_TYPE (name2),
5035 TYPE_MIN_VALUE (TREE_TYPE (val)));
5036 /* Build an expression for the range test. */
5037 tmp = build2 (PLUS_EXPR, TREE_TYPE (name2), name2, cst);
5038 cst = fold_build2 (PLUS_EXPR, TREE_TYPE (name2), cst,
5039 fold_convert (TREE_TYPE (name2), val));
5040 if (comp_code == LT_EXPR || comp_code == GE_EXPR)
5042 new_comp_code = comp_code == LT_EXPR ? LE_EXPR : GT_EXPR;
5043 cst = fold_build2 (MINUS_EXPR, TREE_TYPE (name2), cst,
5044 build_int_cst (TREE_TYPE (name2), 1));
5047 if (dump_file)
5049 fprintf (dump_file, "Adding assert for ");
5050 print_generic_expr (dump_file, name2, 0);
5051 fprintf (dump_file, " from ");
5052 print_generic_expr (dump_file, tmp, 0);
5053 fprintf (dump_file, "\n");
5056 register_new_assert_for (name2, tmp, new_comp_code, cst, NULL,
5057 e, bsi);
5059 retval = true;
5063 /* Add asserts for NAME cmp CST and NAME being defined as
5064 NAME = NAME2 >> CST2.
5066 Extract CST2 from the right shift. */
5067 if (rhs_code == RSHIFT_EXPR)
5069 name2 = gimple_assign_rhs1 (def_stmt);
5070 cst2 = gimple_assign_rhs2 (def_stmt);
5071 if (TREE_CODE (name2) == SSA_NAME
5072 && host_integerp (cst2, 1)
5073 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
5074 && IN_RANGE (tree_low_cst (cst2, 1), 1, prec - 1)
5075 && prec <= HOST_BITS_PER_DOUBLE_INT
5076 && prec == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (val)))
5077 && live_on_edge (e, name2)
5078 && !has_single_use (name2))
5080 mask = double_int::mask (tree_low_cst (cst2, 1));
5081 val2 = fold_binary (LSHIFT_EXPR, TREE_TYPE (val), val, cst2);
5084 if (val2 != NULL_TREE
5085 && TREE_CODE (val2) == INTEGER_CST
5086 && simple_cst_equal (fold_build2 (RSHIFT_EXPR,
5087 TREE_TYPE (val),
5088 val2, cst2), val))
5090 enum tree_code new_comp_code = comp_code;
5091 tree tmp, new_val;
5093 tmp = name2;
5094 if (comp_code == EQ_EXPR || comp_code == NE_EXPR)
5096 if (!TYPE_UNSIGNED (TREE_TYPE (val)))
5098 tree type = build_nonstandard_integer_type (prec, 1);
5099 tmp = build1 (NOP_EXPR, type, name2);
5100 val2 = fold_convert (type, val2);
5102 tmp = fold_build2 (MINUS_EXPR, TREE_TYPE (tmp), tmp, val2);
5103 new_val = double_int_to_tree (TREE_TYPE (tmp), mask);
5104 new_comp_code = comp_code == EQ_EXPR ? LE_EXPR : GT_EXPR;
5106 else if (comp_code == LT_EXPR || comp_code == GE_EXPR)
5108 double_int minval
5109 = double_int::min_value (prec, TYPE_UNSIGNED (TREE_TYPE (val)));
5110 new_val = val2;
5111 if (minval == tree_to_double_int (new_val))
5112 new_val = NULL_TREE;
5114 else
5116 double_int maxval
5117 = double_int::max_value (prec, TYPE_UNSIGNED (TREE_TYPE (val)));
5118 mask |= tree_to_double_int (val2);
5119 if (mask == maxval)
5120 new_val = NULL_TREE;
5121 else
5122 new_val = double_int_to_tree (TREE_TYPE (val2), mask);
5125 if (new_val)
5127 if (dump_file)
5129 fprintf (dump_file, "Adding assert for ");
5130 print_generic_expr (dump_file, name2, 0);
5131 fprintf (dump_file, " from ");
5132 print_generic_expr (dump_file, tmp, 0);
5133 fprintf (dump_file, "\n");
5136 register_new_assert_for (name2, tmp, new_comp_code, new_val,
5137 NULL, e, bsi);
5138 retval = true;
5142 /* Add asserts for NAME cmp CST and NAME being defined as
5143 NAME = NAME2 & CST2.
5145 Extract CST2 from the and.
5147 Also handle
5148 NAME = (unsigned) NAME2;
5149 casts where NAME's type is unsigned and has smaller precision
5150 than NAME2's type as if it was NAME = NAME2 & MASK. */
5151 names[0] = NULL_TREE;
5152 names[1] = NULL_TREE;
5153 cst2 = NULL_TREE;
5154 if (rhs_code == BIT_AND_EXPR
5155 || (CONVERT_EXPR_CODE_P (rhs_code)
5156 && TREE_CODE (TREE_TYPE (val)) == INTEGER_TYPE
5157 && TYPE_UNSIGNED (TREE_TYPE (val))
5158 && TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (def_stmt)))
5159 > prec
5160 && !retval))
5162 name2 = gimple_assign_rhs1 (def_stmt);
5163 if (rhs_code == BIT_AND_EXPR)
5164 cst2 = gimple_assign_rhs2 (def_stmt);
5165 else
5167 cst2 = TYPE_MAX_VALUE (TREE_TYPE (val));
5168 nprec = TYPE_PRECISION (TREE_TYPE (name2));
5170 if (TREE_CODE (name2) == SSA_NAME
5171 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
5172 && TREE_CODE (cst2) == INTEGER_CST
5173 && !integer_zerop (cst2)
5174 && nprec <= HOST_BITS_PER_DOUBLE_INT
5175 && (nprec > 1
5176 || TYPE_UNSIGNED (TREE_TYPE (val))))
5178 gimple def_stmt2 = SSA_NAME_DEF_STMT (name2);
5179 if (gimple_assign_cast_p (def_stmt2))
5181 names[1] = gimple_assign_rhs1 (def_stmt2);
5182 if (!CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt2))
5183 || !INTEGRAL_TYPE_P (TREE_TYPE (names[1]))
5184 || (TYPE_PRECISION (TREE_TYPE (name2))
5185 != TYPE_PRECISION (TREE_TYPE (names[1])))
5186 || !live_on_edge (e, names[1])
5187 || has_single_use (names[1]))
5188 names[1] = NULL_TREE;
5190 if (live_on_edge (e, name2)
5191 && !has_single_use (name2))
5192 names[0] = name2;
5195 if (names[0] || names[1])
5197 double_int minv, maxv = double_int_zero, valv, cst2v;
5198 double_int tem, sgnbit;
5199 bool valid_p = false, valn = false, cst2n = false;
5200 enum tree_code ccode = comp_code;
5202 valv = tree_to_double_int (val).zext (nprec);
5203 cst2v = tree_to_double_int (cst2).zext (nprec);
5204 if (!TYPE_UNSIGNED (TREE_TYPE (val)))
5206 valn = valv.sext (nprec).is_negative ();
5207 cst2n = cst2v.sext (nprec).is_negative ();
5209 /* If CST2 doesn't have most significant bit set,
5210 but VAL is negative, we have comparison like
5211 if ((x & 0x123) > -4) (always true). Just give up. */
5212 if (!cst2n && valn)
5213 ccode = ERROR_MARK;
5214 if (cst2n)
5215 sgnbit = double_int_one.llshift (nprec - 1, nprec).zext (nprec);
5216 else
5217 sgnbit = double_int_zero;
5218 minv = valv & cst2v;
5219 switch (ccode)
5221 case EQ_EXPR:
5222 /* Minimum unsigned value for equality is VAL & CST2
5223 (should be equal to VAL, otherwise we probably should
5224 have folded the comparison into false) and
5225 maximum unsigned value is VAL | ~CST2. */
5226 maxv = valv | ~cst2v;
5227 maxv = maxv.zext (nprec);
5228 valid_p = true;
5229 break;
5230 case NE_EXPR:
5231 tem = valv | ~cst2v;
5232 tem = tem.zext (nprec);
5233 /* If VAL is 0, handle (X & CST2) != 0 as (X & CST2) > 0U. */
5234 if (valv.is_zero ())
5236 cst2n = false;
5237 sgnbit = double_int_zero;
5238 goto gt_expr;
5240 /* If (VAL | ~CST2) is all ones, handle it as
5241 (X & CST2) < VAL. */
5242 if (tem == double_int::mask (nprec))
5244 cst2n = false;
5245 valn = false;
5246 sgnbit = double_int_zero;
5247 goto lt_expr;
5249 if (!cst2n
5250 && cst2v.sext (nprec).is_negative ())
5251 sgnbit
5252 = double_int_one.llshift (nprec - 1, nprec).zext (nprec);
5253 if (!sgnbit.is_zero ())
5255 if (valv == sgnbit)
5257 cst2n = true;
5258 valn = true;
5259 goto gt_expr;
5261 if (tem == double_int::mask (nprec - 1))
5263 cst2n = true;
5264 goto lt_expr;
5266 if (!cst2n)
5267 sgnbit = double_int_zero;
5269 break;
5270 case GE_EXPR:
5271 /* Minimum unsigned value for >= if (VAL & CST2) == VAL
5272 is VAL and maximum unsigned value is ~0. For signed
5273 comparison, if CST2 doesn't have most significant bit
5274 set, handle it similarly. If CST2 has MSB set,
5275 the minimum is the same, and maximum is ~0U/2. */
5276 if (minv != valv)
5278 /* If (VAL & CST2) != VAL, X & CST2 can't be equal to
5279 VAL. */
5280 minv = masked_increment (valv, cst2v, sgnbit, nprec);
5281 if (minv == valv)
5282 break;
5284 maxv = double_int::mask (nprec - (cst2n ? 1 : 0));
5285 valid_p = true;
5286 break;
5287 case GT_EXPR:
5288 gt_expr:
5289 /* Find out smallest MINV where MINV > VAL
5290 && (MINV & CST2) == MINV, if any. If VAL is signed and
5291 CST2 has MSB set, compute it biased by 1 << (nprec - 1). */
5292 minv = masked_increment (valv, cst2v, sgnbit, nprec);
5293 if (minv == valv)
5294 break;
5295 maxv = double_int::mask (nprec - (cst2n ? 1 : 0));
5296 valid_p = true;
5297 break;
5298 case LE_EXPR:
5299 /* Minimum unsigned value for <= is 0 and maximum
5300 unsigned value is VAL | ~CST2 if (VAL & CST2) == VAL.
5301 Otherwise, find smallest VAL2 where VAL2 > VAL
5302 && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
5303 as maximum.
5304 For signed comparison, if CST2 doesn't have most
5305 significant bit set, handle it similarly. If CST2 has
5306 MSB set, the maximum is the same and minimum is INT_MIN. */
5307 if (minv == valv)
5308 maxv = valv;
5309 else
5311 maxv = masked_increment (valv, cst2v, sgnbit, nprec);
5312 if (maxv == valv)
5313 break;
5314 maxv -= double_int_one;
5316 maxv |= ~cst2v;
5317 maxv = maxv.zext (nprec);
5318 minv = sgnbit;
5319 valid_p = true;
5320 break;
5321 case LT_EXPR:
5322 lt_expr:
5323 /* Minimum unsigned value for < is 0 and maximum
5324 unsigned value is (VAL-1) | ~CST2 if (VAL & CST2) == VAL.
5325 Otherwise, find smallest VAL2 where VAL2 > VAL
5326 && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
5327 as maximum.
5328 For signed comparison, if CST2 doesn't have most
5329 significant bit set, handle it similarly. If CST2 has
5330 MSB set, the maximum is the same and minimum is INT_MIN. */
5331 if (minv == valv)
5333 if (valv == sgnbit)
5334 break;
5335 maxv = valv;
5337 else
5339 maxv = masked_increment (valv, cst2v, sgnbit, nprec);
5340 if (maxv == valv)
5341 break;
5343 maxv -= double_int_one;
5344 maxv |= ~cst2v;
5345 maxv = maxv.zext (nprec);
5346 minv = sgnbit;
5347 valid_p = true;
5348 break;
5349 default:
5350 break;
5352 if (valid_p
5353 && (maxv - minv).zext (nprec) != double_int::mask (nprec))
5355 tree tmp, new_val, type;
5356 int i;
5358 for (i = 0; i < 2; i++)
5359 if (names[i])
5361 double_int maxv2 = maxv;
5362 tmp = names[i];
5363 type = TREE_TYPE (names[i]);
5364 if (!TYPE_UNSIGNED (type))
5366 type = build_nonstandard_integer_type (nprec, 1);
5367 tmp = build1 (NOP_EXPR, type, names[i]);
5369 if (!minv.is_zero ())
5371 tmp = build2 (PLUS_EXPR, type, tmp,
5372 double_int_to_tree (type, -minv));
5373 maxv2 = maxv - minv;
5375 new_val = double_int_to_tree (type, maxv2);
5377 if (dump_file)
5379 fprintf (dump_file, "Adding assert for ");
5380 print_generic_expr (dump_file, names[i], 0);
5381 fprintf (dump_file, " from ");
5382 print_generic_expr (dump_file, tmp, 0);
5383 fprintf (dump_file, "\n");
5386 register_new_assert_for (names[i], tmp, LE_EXPR,
5387 new_val, NULL, e, bsi);
5388 retval = true;
5394 return retval;
5397 /* OP is an operand of a truth value expression which is known to have
5398 a particular value. Register any asserts for OP and for any
5399 operands in OP's defining statement.
5401 If CODE is EQ_EXPR, then we want to register OP is zero (false),
5402 if CODE is NE_EXPR, then we want to register OP is nonzero (true). */
5404 static bool
5405 register_edge_assert_for_1 (tree op, enum tree_code code,
5406 edge e, gimple_stmt_iterator bsi)
5408 bool retval = false;
5409 gimple op_def;
5410 tree val;
5411 enum tree_code rhs_code;
5413 /* We only care about SSA_NAMEs. */
5414 if (TREE_CODE (op) != SSA_NAME)
5415 return false;
5417 /* We know that OP will have a zero or nonzero value. If OP is used
5418 more than once go ahead and register an assert for OP.
5420 The FOUND_IN_SUBGRAPH support is not helpful in this situation as
5421 it will always be set for OP (because OP is used in a COND_EXPR in
5422 the subgraph). */
5423 if (!has_single_use (op))
5425 val = build_int_cst (TREE_TYPE (op), 0);
5426 register_new_assert_for (op, op, code, val, NULL, e, bsi);
5427 retval = true;
5430 /* Now look at how OP is set. If it's set from a comparison,
5431 a truth operation or some bit operations, then we may be able
5432 to register information about the operands of that assignment. */
5433 op_def = SSA_NAME_DEF_STMT (op);
5434 if (gimple_code (op_def) != GIMPLE_ASSIGN)
5435 return retval;
5437 rhs_code = gimple_assign_rhs_code (op_def);
5439 if (TREE_CODE_CLASS (rhs_code) == tcc_comparison)
5441 bool invert = (code == EQ_EXPR ? true : false);
5442 tree op0 = gimple_assign_rhs1 (op_def);
5443 tree op1 = gimple_assign_rhs2 (op_def);
5445 if (TREE_CODE (op0) == SSA_NAME)
5446 retval |= register_edge_assert_for_2 (op0, e, bsi, rhs_code, op0, op1,
5447 invert);
5448 if (TREE_CODE (op1) == SSA_NAME)
5449 retval |= register_edge_assert_for_2 (op1, e, bsi, rhs_code, op0, op1,
5450 invert);
5452 else if ((code == NE_EXPR
5453 && gimple_assign_rhs_code (op_def) == BIT_AND_EXPR)
5454 || (code == EQ_EXPR
5455 && gimple_assign_rhs_code (op_def) == BIT_IOR_EXPR))
5457 /* Recurse on each operand. */
5458 tree op0 = gimple_assign_rhs1 (op_def);
5459 tree op1 = gimple_assign_rhs2 (op_def);
5460 if (TREE_CODE (op0) == SSA_NAME
5461 && has_single_use (op0))
5462 retval |= register_edge_assert_for_1 (op0, code, e, bsi);
5463 if (TREE_CODE (op1) == SSA_NAME
5464 && has_single_use (op1))
5465 retval |= register_edge_assert_for_1 (op1, code, e, bsi);
5467 else if (gimple_assign_rhs_code (op_def) == BIT_NOT_EXPR
5468 && TYPE_PRECISION (TREE_TYPE (gimple_assign_lhs (op_def))) == 1)
5470 /* Recurse, flipping CODE. */
5471 code = invert_tree_comparison (code, false);
5472 retval |= register_edge_assert_for_1 (gimple_assign_rhs1 (op_def),
5473 code, e, bsi);
5475 else if (gimple_assign_rhs_code (op_def) == SSA_NAME)
5477 /* Recurse through the copy. */
5478 retval |= register_edge_assert_for_1 (gimple_assign_rhs1 (op_def),
5479 code, e, bsi);
5481 else if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (op_def)))
5483 /* Recurse through the type conversion. */
5484 retval |= register_edge_assert_for_1 (gimple_assign_rhs1 (op_def),
5485 code, e, bsi);
5488 return retval;
5491 /* Try to register an edge assertion for SSA name NAME on edge E for
5492 the condition COND contributing to the conditional jump pointed to by SI.
5493 Return true if an assertion for NAME could be registered. */
5495 static bool
5496 register_edge_assert_for (tree name, edge e, gimple_stmt_iterator si,
5497 enum tree_code cond_code, tree cond_op0,
5498 tree cond_op1)
5500 tree val;
5501 enum tree_code comp_code;
5502 bool retval = false;
5503 bool is_else_edge = (e->flags & EDGE_FALSE_VALUE) != 0;
5505 /* Do not attempt to infer anything in names that flow through
5506 abnormal edges. */
5507 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name))
5508 return false;
5510 if (!extract_code_and_val_from_cond_with_ops (name, cond_code,
5511 cond_op0, cond_op1,
5512 is_else_edge,
5513 &comp_code, &val))
5514 return false;
5516 /* Register ASSERT_EXPRs for name. */
5517 retval |= register_edge_assert_for_2 (name, e, si, cond_code, cond_op0,
5518 cond_op1, is_else_edge);
5521 /* If COND is effectively an equality test of an SSA_NAME against
5522 the value zero or one, then we may be able to assert values
5523 for SSA_NAMEs which flow into COND. */
5525 /* In the case of NAME == 1 or NAME != 0, for BIT_AND_EXPR defining
5526 statement of NAME we can assert both operands of the BIT_AND_EXPR
5527 have nonzero value. */
5528 if (((comp_code == EQ_EXPR && integer_onep (val))
5529 || (comp_code == NE_EXPR && integer_zerop (val))))
5531 gimple def_stmt = SSA_NAME_DEF_STMT (name);
5533 if (is_gimple_assign (def_stmt)
5534 && gimple_assign_rhs_code (def_stmt) == BIT_AND_EXPR)
5536 tree op0 = gimple_assign_rhs1 (def_stmt);
5537 tree op1 = gimple_assign_rhs2 (def_stmt);
5538 retval |= register_edge_assert_for_1 (op0, NE_EXPR, e, si);
5539 retval |= register_edge_assert_for_1 (op1, NE_EXPR, e, si);
5543 /* In the case of NAME == 0 or NAME != 1, for BIT_IOR_EXPR defining
5544 statement of NAME we can assert both operands of the BIT_IOR_EXPR
5545 have zero value. */
5546 if (((comp_code == EQ_EXPR && integer_zerop (val))
5547 || (comp_code == NE_EXPR && integer_onep (val))))
5549 gimple def_stmt = SSA_NAME_DEF_STMT (name);
5551 /* For BIT_IOR_EXPR only if NAME == 0 both operands have
5552 necessarily zero value, or if type-precision is one. */
5553 if (is_gimple_assign (def_stmt)
5554 && (gimple_assign_rhs_code (def_stmt) == BIT_IOR_EXPR
5555 && (TYPE_PRECISION (TREE_TYPE (name)) == 1
5556 || comp_code == EQ_EXPR)))
5558 tree op0 = gimple_assign_rhs1 (def_stmt);
5559 tree op1 = gimple_assign_rhs2 (def_stmt);
5560 retval |= register_edge_assert_for_1 (op0, EQ_EXPR, e, si);
5561 retval |= register_edge_assert_for_1 (op1, EQ_EXPR, e, si);
5565 return retval;
5569 /* Determine whether the outgoing edges of BB should receive an
5570 ASSERT_EXPR for each of the operands of BB's LAST statement.
5571 The last statement of BB must be a COND_EXPR.
5573 If any of the sub-graphs rooted at BB have an interesting use of
5574 the predicate operands, an assert location node is added to the
5575 list of assertions for the corresponding operands. */
5577 static bool
5578 find_conditional_asserts (basic_block bb, gimple last)
5580 bool need_assert;
5581 gimple_stmt_iterator bsi;
5582 tree op;
5583 edge_iterator ei;
5584 edge e;
5585 ssa_op_iter iter;
5587 need_assert = false;
5588 bsi = gsi_for_stmt (last);
5590 /* Look for uses of the operands in each of the sub-graphs
5591 rooted at BB. We need to check each of the outgoing edges
5592 separately, so that we know what kind of ASSERT_EXPR to
5593 insert. */
5594 FOR_EACH_EDGE (e, ei, bb->succs)
5596 if (e->dest == bb)
5597 continue;
5599 /* Register the necessary assertions for each operand in the
5600 conditional predicate. */
5601 FOR_EACH_SSA_TREE_OPERAND (op, last, iter, SSA_OP_USE)
5603 need_assert |= register_edge_assert_for (op, e, bsi,
5604 gimple_cond_code (last),
5605 gimple_cond_lhs (last),
5606 gimple_cond_rhs (last));
5610 return need_assert;
5613 struct case_info
5615 tree expr;
5616 basic_block bb;
5619 /* Compare two case labels sorting first by the destination bb index
5620 and then by the case value. */
5622 static int
5623 compare_case_labels (const void *p1, const void *p2)
5625 const struct case_info *ci1 = (const struct case_info *) p1;
5626 const struct case_info *ci2 = (const struct case_info *) p2;
5627 int idx1 = ci1->bb->index;
5628 int idx2 = ci2->bb->index;
5630 if (idx1 < idx2)
5631 return -1;
5632 else if (idx1 == idx2)
5634 /* Make sure the default label is first in a group. */
5635 if (!CASE_LOW (ci1->expr))
5636 return -1;
5637 else if (!CASE_LOW (ci2->expr))
5638 return 1;
5639 else
5640 return tree_int_cst_compare (CASE_LOW (ci1->expr),
5641 CASE_LOW (ci2->expr));
5643 else
5644 return 1;
5647 /* Determine whether the outgoing edges of BB should receive an
5648 ASSERT_EXPR for each of the operands of BB's LAST statement.
5649 The last statement of BB must be a SWITCH_EXPR.
5651 If any of the sub-graphs rooted at BB have an interesting use of
5652 the predicate operands, an assert location node is added to the
5653 list of assertions for the corresponding operands. */
5655 static bool
5656 find_switch_asserts (basic_block bb, gimple last)
5658 bool need_assert;
5659 gimple_stmt_iterator bsi;
5660 tree op;
5661 edge e;
5662 struct case_info *ci;
5663 size_t n = gimple_switch_num_labels (last);
5664 #if GCC_VERSION >= 4000
5665 unsigned int idx;
5666 #else
5667 /* Work around GCC 3.4 bug (PR 37086). */
5668 volatile unsigned int idx;
5669 #endif
5671 need_assert = false;
5672 bsi = gsi_for_stmt (last);
5673 op = gimple_switch_index (last);
5674 if (TREE_CODE (op) != SSA_NAME)
5675 return false;
5677 /* Build a vector of case labels sorted by destination label. */
5678 ci = XNEWVEC (struct case_info, n);
5679 for (idx = 0; idx < n; ++idx)
5681 ci[idx].expr = gimple_switch_label (last, idx);
5682 ci[idx].bb = label_to_block (CASE_LABEL (ci[idx].expr));
5684 qsort (ci, n, sizeof (struct case_info), compare_case_labels);
5686 for (idx = 0; idx < n; ++idx)
5688 tree min, max;
5689 tree cl = ci[idx].expr;
5690 basic_block cbb = ci[idx].bb;
5692 min = CASE_LOW (cl);
5693 max = CASE_HIGH (cl);
5695 /* If there are multiple case labels with the same destination
5696 we need to combine them to a single value range for the edge. */
5697 if (idx + 1 < n && cbb == ci[idx + 1].bb)
5699 /* Skip labels until the last of the group. */
5700 do {
5701 ++idx;
5702 } while (idx < n && cbb == ci[idx].bb);
5703 --idx;
5705 /* Pick up the maximum of the case label range. */
5706 if (CASE_HIGH (ci[idx].expr))
5707 max = CASE_HIGH (ci[idx].expr);
5708 else
5709 max = CASE_LOW (ci[idx].expr);
5712 /* Nothing to do if the range includes the default label until we
5713 can register anti-ranges. */
5714 if (min == NULL_TREE)
5715 continue;
5717 /* Find the edge to register the assert expr on. */
5718 e = find_edge (bb, cbb);
5720 /* Register the necessary assertions for the operand in the
5721 SWITCH_EXPR. */
5722 need_assert |= register_edge_assert_for (op, e, bsi,
5723 max ? GE_EXPR : EQ_EXPR,
5725 fold_convert (TREE_TYPE (op),
5726 min));
5727 if (max)
5729 need_assert |= register_edge_assert_for (op, e, bsi, LE_EXPR,
5731 fold_convert (TREE_TYPE (op),
5732 max));
5736 XDELETEVEC (ci);
5737 return need_assert;
5741 /* Traverse all the statements in block BB looking for statements that
5742 may generate useful assertions for the SSA names in their operand.
5743 If a statement produces a useful assertion A for name N_i, then the
5744 list of assertions already generated for N_i is scanned to
5745 determine if A is actually needed.
5747 If N_i already had the assertion A at a location dominating the
5748 current location, then nothing needs to be done. Otherwise, the
5749 new location for A is recorded instead.
5751 1- For every statement S in BB, all the variables used by S are
5752 added to bitmap FOUND_IN_SUBGRAPH.
5754 2- If statement S uses an operand N in a way that exposes a known
5755 value range for N, then if N was not already generated by an
5756 ASSERT_EXPR, create a new assert location for N. For instance,
5757 if N is a pointer and the statement dereferences it, we can
5758 assume that N is not NULL.
5760 3- COND_EXPRs are a special case of #2. We can derive range
5761 information from the predicate but need to insert different
5762 ASSERT_EXPRs for each of the sub-graphs rooted at the
5763 conditional block. If the last statement of BB is a conditional
5764 expression of the form 'X op Y', then
5766 a) Remove X and Y from the set FOUND_IN_SUBGRAPH.
5768 b) If the conditional is the only entry point to the sub-graph
5769 corresponding to the THEN_CLAUSE, recurse into it. On
5770 return, if X and/or Y are marked in FOUND_IN_SUBGRAPH, then
5771 an ASSERT_EXPR is added for the corresponding variable.
5773 c) Repeat step (b) on the ELSE_CLAUSE.
5775 d) Mark X and Y in FOUND_IN_SUBGRAPH.
5777 For instance,
5779 if (a == 9)
5780 b = a;
5781 else
5782 b = c + 1;
5784 In this case, an assertion on the THEN clause is useful to
5785 determine that 'a' is always 9 on that edge. However, an assertion
5786 on the ELSE clause would be unnecessary.
5788 4- If BB does not end in a conditional expression, then we recurse
5789 into BB's dominator children.
5791 At the end of the recursive traversal, every SSA name will have a
5792 list of locations where ASSERT_EXPRs should be added. When a new
5793 location for name N is found, it is registered by calling
5794 register_new_assert_for. That function keeps track of all the
5795 registered assertions to prevent adding unnecessary assertions.
5796 For instance, if a pointer P_4 is dereferenced more than once in a
5797 dominator tree, only the location dominating all the dereference of
5798 P_4 will receive an ASSERT_EXPR.
5800 If this function returns true, then it means that there are names
5801 for which we need to generate ASSERT_EXPRs. Those assertions are
5802 inserted by process_assert_insertions. */
5804 static bool
5805 find_assert_locations_1 (basic_block bb, sbitmap live)
5807 gimple_stmt_iterator si;
5808 gimple last;
5809 bool need_assert;
5811 need_assert = false;
5812 last = last_stmt (bb);
5814 /* If BB's last statement is a conditional statement involving integer
5815 operands, determine if we need to add ASSERT_EXPRs. */
5816 if (last
5817 && gimple_code (last) == GIMPLE_COND
5818 && !fp_predicate (last)
5819 && !ZERO_SSA_OPERANDS (last, SSA_OP_USE))
5820 need_assert |= find_conditional_asserts (bb, last);
5822 /* If BB's last statement is a switch statement involving integer
5823 operands, determine if we need to add ASSERT_EXPRs. */
5824 if (last
5825 && gimple_code (last) == GIMPLE_SWITCH
5826 && !ZERO_SSA_OPERANDS (last, SSA_OP_USE))
5827 need_assert |= find_switch_asserts (bb, last);
5829 /* Traverse all the statements in BB marking used names and looking
5830 for statements that may infer assertions for their used operands. */
5831 for (si = gsi_last_bb (bb); !gsi_end_p (si); gsi_prev (&si))
5833 gimple stmt;
5834 tree op;
5835 ssa_op_iter i;
5837 stmt = gsi_stmt (si);
5839 if (is_gimple_debug (stmt))
5840 continue;
5842 /* See if we can derive an assertion for any of STMT's operands. */
5843 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_USE)
5845 tree value;
5846 enum tree_code comp_code;
5848 /* If op is not live beyond this stmt, do not bother to insert
5849 asserts for it. */
5850 if (!bitmap_bit_p (live, SSA_NAME_VERSION (op)))
5851 continue;
5853 /* If OP is used in such a way that we can infer a value
5854 range for it, and we don't find a previous assertion for
5855 it, create a new assertion location node for OP. */
5856 if (infer_value_range (stmt, op, &comp_code, &value))
5858 /* If we are able to infer a nonzero value range for OP,
5859 then walk backwards through the use-def chain to see if OP
5860 was set via a typecast.
5862 If so, then we can also infer a nonzero value range
5863 for the operand of the NOP_EXPR. */
5864 if (comp_code == NE_EXPR && integer_zerop (value))
5866 tree t = op;
5867 gimple def_stmt = SSA_NAME_DEF_STMT (t);
5869 while (is_gimple_assign (def_stmt)
5870 && gimple_assign_rhs_code (def_stmt) == NOP_EXPR
5871 && TREE_CODE
5872 (gimple_assign_rhs1 (def_stmt)) == SSA_NAME
5873 && POINTER_TYPE_P
5874 (TREE_TYPE (gimple_assign_rhs1 (def_stmt))))
5876 t = gimple_assign_rhs1 (def_stmt);
5877 def_stmt = SSA_NAME_DEF_STMT (t);
5879 /* Note we want to register the assert for the
5880 operand of the NOP_EXPR after SI, not after the
5881 conversion. */
5882 if (! has_single_use (t))
5884 register_new_assert_for (t, t, comp_code, value,
5885 bb, NULL, si);
5886 need_assert = true;
5891 register_new_assert_for (op, op, comp_code, value, bb, NULL, si);
5892 need_assert = true;
5896 /* Update live. */
5897 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_USE)
5898 bitmap_set_bit (live, SSA_NAME_VERSION (op));
5899 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_DEF)
5900 bitmap_clear_bit (live, SSA_NAME_VERSION (op));
5903 /* Traverse all PHI nodes in BB, updating live. */
5904 for (si = gsi_start_phis (bb); !gsi_end_p (si); gsi_next (&si))
5906 use_operand_p arg_p;
5907 ssa_op_iter i;
5908 gimple phi = gsi_stmt (si);
5909 tree res = gimple_phi_result (phi);
5911 if (virtual_operand_p (res))
5912 continue;
5914 FOR_EACH_PHI_ARG (arg_p, phi, i, SSA_OP_USE)
5916 tree arg = USE_FROM_PTR (arg_p);
5917 if (TREE_CODE (arg) == SSA_NAME)
5918 bitmap_set_bit (live, SSA_NAME_VERSION (arg));
5921 bitmap_clear_bit (live, SSA_NAME_VERSION (res));
5924 return need_assert;
5927 /* Do an RPO walk over the function computing SSA name liveness
5928 on-the-fly and deciding on assert expressions to insert.
5929 Returns true if there are assert expressions to be inserted. */
5931 static bool
5932 find_assert_locations (void)
5934 int *rpo = XNEWVEC (int, last_basic_block);
5935 int *bb_rpo = XNEWVEC (int, last_basic_block);
5936 int *last_rpo = XCNEWVEC (int, last_basic_block);
5937 int rpo_cnt, i;
5938 bool need_asserts;
5940 live = XCNEWVEC (sbitmap, last_basic_block);
5941 rpo_cnt = pre_and_rev_post_order_compute (NULL, rpo, false);
5942 for (i = 0; i < rpo_cnt; ++i)
5943 bb_rpo[rpo[i]] = i;
5945 need_asserts = false;
5946 for (i = rpo_cnt - 1; i >= 0; --i)
5948 basic_block bb = BASIC_BLOCK (rpo[i]);
5949 edge e;
5950 edge_iterator ei;
5952 if (!live[rpo[i]])
5954 live[rpo[i]] = sbitmap_alloc (num_ssa_names);
5955 bitmap_clear (live[rpo[i]]);
5958 /* Process BB and update the live information with uses in
5959 this block. */
5960 need_asserts |= find_assert_locations_1 (bb, live[rpo[i]]);
5962 /* Merge liveness into the predecessor blocks and free it. */
5963 if (!bitmap_empty_p (live[rpo[i]]))
5965 int pred_rpo = i;
5966 FOR_EACH_EDGE (e, ei, bb->preds)
5968 int pred = e->src->index;
5969 if ((e->flags & EDGE_DFS_BACK) || pred == ENTRY_BLOCK)
5970 continue;
5972 if (!live[pred])
5974 live[pred] = sbitmap_alloc (num_ssa_names);
5975 bitmap_clear (live[pred]);
5977 bitmap_ior (live[pred], live[pred], live[rpo[i]]);
5979 if (bb_rpo[pred] < pred_rpo)
5980 pred_rpo = bb_rpo[pred];
5983 /* Record the RPO number of the last visited block that needs
5984 live information from this block. */
5985 last_rpo[rpo[i]] = pred_rpo;
5987 else
5989 sbitmap_free (live[rpo[i]]);
5990 live[rpo[i]] = NULL;
5993 /* We can free all successors live bitmaps if all their
5994 predecessors have been visited already. */
5995 FOR_EACH_EDGE (e, ei, bb->succs)
5996 if (last_rpo[e->dest->index] == i
5997 && live[e->dest->index])
5999 sbitmap_free (live[e->dest->index]);
6000 live[e->dest->index] = NULL;
6004 XDELETEVEC (rpo);
6005 XDELETEVEC (bb_rpo);
6006 XDELETEVEC (last_rpo);
6007 for (i = 0; i < last_basic_block; ++i)
6008 if (live[i])
6009 sbitmap_free (live[i]);
6010 XDELETEVEC (live);
6012 return need_asserts;
6015 /* Create an ASSERT_EXPR for NAME and insert it in the location
6016 indicated by LOC. Return true if we made any edge insertions. */
6018 static bool
6019 process_assert_insertions_for (tree name, assert_locus_t loc)
6021 /* Build the comparison expression NAME_i COMP_CODE VAL. */
6022 gimple stmt;
6023 tree cond;
6024 gimple assert_stmt;
6025 edge_iterator ei;
6026 edge e;
6028 /* If we have X <=> X do not insert an assert expr for that. */
6029 if (loc->expr == loc->val)
6030 return false;
6032 cond = build2 (loc->comp_code, boolean_type_node, loc->expr, loc->val);
6033 assert_stmt = build_assert_expr_for (cond, name);
6034 if (loc->e)
6036 /* We have been asked to insert the assertion on an edge. This
6037 is used only by COND_EXPR and SWITCH_EXPR assertions. */
6038 gcc_checking_assert (gimple_code (gsi_stmt (loc->si)) == GIMPLE_COND
6039 || (gimple_code (gsi_stmt (loc->si))
6040 == GIMPLE_SWITCH));
6042 gsi_insert_on_edge (loc->e, assert_stmt);
6043 return true;
6046 /* Otherwise, we can insert right after LOC->SI iff the
6047 statement must not be the last statement in the block. */
6048 stmt = gsi_stmt (loc->si);
6049 if (!stmt_ends_bb_p (stmt))
6051 gsi_insert_after (&loc->si, assert_stmt, GSI_SAME_STMT);
6052 return false;
6055 /* If STMT must be the last statement in BB, we can only insert new
6056 assertions on the non-abnormal edge out of BB. Note that since
6057 STMT is not control flow, there may only be one non-abnormal edge
6058 out of BB. */
6059 FOR_EACH_EDGE (e, ei, loc->bb->succs)
6060 if (!(e->flags & EDGE_ABNORMAL))
6062 gsi_insert_on_edge (e, assert_stmt);
6063 return true;
6066 gcc_unreachable ();
6070 /* Process all the insertions registered for every name N_i registered
6071 in NEED_ASSERT_FOR. The list of assertions to be inserted are
6072 found in ASSERTS_FOR[i]. */
6074 static void
6075 process_assert_insertions (void)
6077 unsigned i;
6078 bitmap_iterator bi;
6079 bool update_edges_p = false;
6080 int num_asserts = 0;
6082 if (dump_file && (dump_flags & TDF_DETAILS))
6083 dump_all_asserts (dump_file);
6085 EXECUTE_IF_SET_IN_BITMAP (need_assert_for, 0, i, bi)
6087 assert_locus_t loc = asserts_for[i];
6088 gcc_assert (loc);
6090 while (loc)
6092 assert_locus_t next = loc->next;
6093 update_edges_p |= process_assert_insertions_for (ssa_name (i), loc);
6094 free (loc);
6095 loc = next;
6096 num_asserts++;
6100 if (update_edges_p)
6101 gsi_commit_edge_inserts ();
6103 statistics_counter_event (cfun, "Number of ASSERT_EXPR expressions inserted",
6104 num_asserts);
6108 /* Traverse the flowgraph looking for conditional jumps to insert range
6109 expressions. These range expressions are meant to provide information
6110 to optimizations that need to reason in terms of value ranges. They
6111 will not be expanded into RTL. For instance, given:
6113 x = ...
6114 y = ...
6115 if (x < y)
6116 y = x - 2;
6117 else
6118 x = y + 3;
6120 this pass will transform the code into:
6122 x = ...
6123 y = ...
6124 if (x < y)
6126 x = ASSERT_EXPR <x, x < y>
6127 y = x - 2
6129 else
6131 y = ASSERT_EXPR <y, x <= y>
6132 x = y + 3
6135 The idea is that once copy and constant propagation have run, other
6136 optimizations will be able to determine what ranges of values can 'x'
6137 take in different paths of the code, simply by checking the reaching
6138 definition of 'x'. */
6140 static void
6141 insert_range_assertions (void)
6143 need_assert_for = BITMAP_ALLOC (NULL);
6144 asserts_for = XCNEWVEC (assert_locus_t, num_ssa_names);
6146 calculate_dominance_info (CDI_DOMINATORS);
6148 if (find_assert_locations ())
6150 process_assert_insertions ();
6151 update_ssa (TODO_update_ssa_no_phi);
6154 if (dump_file && (dump_flags & TDF_DETAILS))
6156 fprintf (dump_file, "\nSSA form after inserting ASSERT_EXPRs\n");
6157 dump_function_to_file (current_function_decl, dump_file, dump_flags);
6160 free (asserts_for);
6161 BITMAP_FREE (need_assert_for);
6164 /* Checks one ARRAY_REF in REF, located at LOCUS. Ignores flexible arrays
6165 and "struct" hacks. If VRP can determine that the
6166 array subscript is a constant, check if it is outside valid
6167 range. If the array subscript is a RANGE, warn if it is
6168 non-overlapping with valid range.
6169 IGNORE_OFF_BY_ONE is true if the ARRAY_REF is inside a ADDR_EXPR. */
6171 static void
6172 check_array_ref (location_t location, tree ref, bool ignore_off_by_one)
6174 value_range_t* vr = NULL;
6175 tree low_sub, up_sub;
6176 tree low_bound, up_bound, up_bound_p1;
6177 tree base;
6179 if (TREE_NO_WARNING (ref))
6180 return;
6182 low_sub = up_sub = TREE_OPERAND (ref, 1);
6183 up_bound = array_ref_up_bound (ref);
6185 /* Can not check flexible arrays. */
6186 if (!up_bound
6187 || TREE_CODE (up_bound) != INTEGER_CST)
6188 return;
6190 /* Accesses to trailing arrays via pointers may access storage
6191 beyond the types array bounds. */
6192 base = get_base_address (ref);
6193 if (base && TREE_CODE (base) == MEM_REF)
6195 tree cref, next = NULL_TREE;
6197 if (TREE_CODE (TREE_OPERAND (ref, 0)) != COMPONENT_REF)
6198 return;
6200 cref = TREE_OPERAND (ref, 0);
6201 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (cref, 0))) == RECORD_TYPE)
6202 for (next = DECL_CHAIN (TREE_OPERAND (cref, 1));
6203 next && TREE_CODE (next) != FIELD_DECL;
6204 next = DECL_CHAIN (next))
6207 /* If this is the last field in a struct type or a field in a
6208 union type do not warn. */
6209 if (!next)
6210 return;
6213 low_bound = array_ref_low_bound (ref);
6214 up_bound_p1 = int_const_binop (PLUS_EXPR, up_bound, integer_one_node);
6216 if (TREE_CODE (low_sub) == SSA_NAME)
6218 vr = get_value_range (low_sub);
6219 if (vr->type == VR_RANGE || vr->type == VR_ANTI_RANGE)
6221 low_sub = vr->type == VR_RANGE ? vr->max : vr->min;
6222 up_sub = vr->type == VR_RANGE ? vr->min : vr->max;
6226 if (vr && vr->type == VR_ANTI_RANGE)
6228 if (TREE_CODE (up_sub) == INTEGER_CST
6229 && tree_int_cst_lt (up_bound, up_sub)
6230 && TREE_CODE (low_sub) == INTEGER_CST
6231 && tree_int_cst_lt (low_sub, low_bound))
6233 warning_at (location, OPT_Warray_bounds,
6234 "array subscript is outside array bounds");
6235 TREE_NO_WARNING (ref) = 1;
6238 else if (TREE_CODE (up_sub) == INTEGER_CST
6239 && (ignore_off_by_one
6240 ? (tree_int_cst_lt (up_bound, up_sub)
6241 && !tree_int_cst_equal (up_bound_p1, up_sub))
6242 : (tree_int_cst_lt (up_bound, up_sub)
6243 || tree_int_cst_equal (up_bound_p1, up_sub))))
6245 if (dump_file && (dump_flags & TDF_DETAILS))
6247 fprintf (dump_file, "Array bound warning for ");
6248 dump_generic_expr (MSG_NOTE, TDF_SLIM, ref);
6249 fprintf (dump_file, "\n");
6251 warning_at (location, OPT_Warray_bounds,
6252 "array subscript is above array bounds");
6253 TREE_NO_WARNING (ref) = 1;
6255 else if (TREE_CODE (low_sub) == INTEGER_CST
6256 && tree_int_cst_lt (low_sub, low_bound))
6258 if (dump_file && (dump_flags & TDF_DETAILS))
6260 fprintf (dump_file, "Array bound warning for ");
6261 dump_generic_expr (MSG_NOTE, TDF_SLIM, ref);
6262 fprintf (dump_file, "\n");
6264 warning_at (location, OPT_Warray_bounds,
6265 "array subscript is below array bounds");
6266 TREE_NO_WARNING (ref) = 1;
6270 /* Searches if the expr T, located at LOCATION computes
6271 address of an ARRAY_REF, and call check_array_ref on it. */
6273 static void
6274 search_for_addr_array (tree t, location_t location)
6276 while (TREE_CODE (t) == SSA_NAME)
6278 gimple g = SSA_NAME_DEF_STMT (t);
6280 if (gimple_code (g) != GIMPLE_ASSIGN)
6281 return;
6283 if (get_gimple_rhs_class (gimple_assign_rhs_code (g))
6284 != GIMPLE_SINGLE_RHS)
6285 return;
6287 t = gimple_assign_rhs1 (g);
6291 /* We are only interested in addresses of ARRAY_REF's. */
6292 if (TREE_CODE (t) != ADDR_EXPR)
6293 return;
6295 /* Check each ARRAY_REFs in the reference chain. */
6298 if (TREE_CODE (t) == ARRAY_REF)
6299 check_array_ref (location, t, true /*ignore_off_by_one*/);
6301 t = TREE_OPERAND (t, 0);
6303 while (handled_component_p (t));
6305 if (TREE_CODE (t) == MEM_REF
6306 && TREE_CODE (TREE_OPERAND (t, 0)) == ADDR_EXPR
6307 && !TREE_NO_WARNING (t))
6309 tree tem = TREE_OPERAND (TREE_OPERAND (t, 0), 0);
6310 tree low_bound, up_bound, el_sz;
6311 double_int idx;
6312 if (TREE_CODE (TREE_TYPE (tem)) != ARRAY_TYPE
6313 || TREE_CODE (TREE_TYPE (TREE_TYPE (tem))) == ARRAY_TYPE
6314 || !TYPE_DOMAIN (TREE_TYPE (tem)))
6315 return;
6317 low_bound = TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (tem)));
6318 up_bound = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (tem)));
6319 el_sz = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (tem)));
6320 if (!low_bound
6321 || TREE_CODE (low_bound) != INTEGER_CST
6322 || !up_bound
6323 || TREE_CODE (up_bound) != INTEGER_CST
6324 || !el_sz
6325 || TREE_CODE (el_sz) != INTEGER_CST)
6326 return;
6328 idx = mem_ref_offset (t);
6329 idx = idx.sdiv (tree_to_double_int (el_sz), TRUNC_DIV_EXPR);
6330 if (idx.slt (double_int_zero))
6332 if (dump_file && (dump_flags & TDF_DETAILS))
6334 fprintf (dump_file, "Array bound warning for ");
6335 dump_generic_expr (MSG_NOTE, TDF_SLIM, t);
6336 fprintf (dump_file, "\n");
6338 warning_at (location, OPT_Warray_bounds,
6339 "array subscript is below array bounds");
6340 TREE_NO_WARNING (t) = 1;
6342 else if (idx.sgt (tree_to_double_int (up_bound)
6343 - tree_to_double_int (low_bound)
6344 + double_int_one))
6346 if (dump_file && (dump_flags & TDF_DETAILS))
6348 fprintf (dump_file, "Array bound warning for ");
6349 dump_generic_expr (MSG_NOTE, TDF_SLIM, t);
6350 fprintf (dump_file, "\n");
6352 warning_at (location, OPT_Warray_bounds,
6353 "array subscript is above array bounds");
6354 TREE_NO_WARNING (t) = 1;
6359 /* walk_tree() callback that checks if *TP is
6360 an ARRAY_REF inside an ADDR_EXPR (in which an array
6361 subscript one outside the valid range is allowed). Call
6362 check_array_ref for each ARRAY_REF found. The location is
6363 passed in DATA. */
6365 static tree
6366 check_array_bounds (tree *tp, int *walk_subtree, void *data)
6368 tree t = *tp;
6369 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
6370 location_t location;
6372 if (EXPR_HAS_LOCATION (t))
6373 location = EXPR_LOCATION (t);
6374 else
6376 location_t *locp = (location_t *) wi->info;
6377 location = *locp;
6380 *walk_subtree = TRUE;
6382 if (TREE_CODE (t) == ARRAY_REF)
6383 check_array_ref (location, t, false /*ignore_off_by_one*/);
6385 if (TREE_CODE (t) == MEM_REF
6386 || (TREE_CODE (t) == RETURN_EXPR && TREE_OPERAND (t, 0)))
6387 search_for_addr_array (TREE_OPERAND (t, 0), location);
6389 if (TREE_CODE (t) == ADDR_EXPR)
6390 *walk_subtree = FALSE;
6392 return NULL_TREE;
6395 /* Walk over all statements of all reachable BBs and call check_array_bounds
6396 on them. */
6398 static void
6399 check_all_array_refs (void)
6401 basic_block bb;
6402 gimple_stmt_iterator si;
6404 FOR_EACH_BB (bb)
6406 edge_iterator ei;
6407 edge e;
6408 bool executable = false;
6410 /* Skip blocks that were found to be unreachable. */
6411 FOR_EACH_EDGE (e, ei, bb->preds)
6412 executable |= !!(e->flags & EDGE_EXECUTABLE);
6413 if (!executable)
6414 continue;
6416 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
6418 gimple stmt = gsi_stmt (si);
6419 struct walk_stmt_info wi;
6420 if (!gimple_has_location (stmt))
6421 continue;
6423 if (is_gimple_call (stmt))
6425 size_t i;
6426 size_t n = gimple_call_num_args (stmt);
6427 for (i = 0; i < n; i++)
6429 tree arg = gimple_call_arg (stmt, i);
6430 search_for_addr_array (arg, gimple_location (stmt));
6433 else
6435 memset (&wi, 0, sizeof (wi));
6436 wi.info = CONST_CAST (void *, (const void *)
6437 gimple_location_ptr (stmt));
6439 walk_gimple_op (gsi_stmt (si),
6440 check_array_bounds,
6441 &wi);
6447 /* Convert range assertion expressions into the implied copies and
6448 copy propagate away the copies. Doing the trivial copy propagation
6449 here avoids the need to run the full copy propagation pass after
6450 VRP.
6452 FIXME, this will eventually lead to copy propagation removing the
6453 names that had useful range information attached to them. For
6454 instance, if we had the assertion N_i = ASSERT_EXPR <N_j, N_j > 3>,
6455 then N_i will have the range [3, +INF].
6457 However, by converting the assertion into the implied copy
6458 operation N_i = N_j, we will then copy-propagate N_j into the uses
6459 of N_i and lose the range information. We may want to hold on to
6460 ASSERT_EXPRs a little while longer as the ranges could be used in
6461 things like jump threading.
6463 The problem with keeping ASSERT_EXPRs around is that passes after
6464 VRP need to handle them appropriately.
6466 Another approach would be to make the range information a first
6467 class property of the SSA_NAME so that it can be queried from
6468 any pass. This is made somewhat more complex by the need for
6469 multiple ranges to be associated with one SSA_NAME. */
6471 static void
6472 remove_range_assertions (void)
6474 basic_block bb;
6475 gimple_stmt_iterator si;
6477 /* Note that the BSI iterator bump happens at the bottom of the
6478 loop and no bump is necessary if we're removing the statement
6479 referenced by the current BSI. */
6480 FOR_EACH_BB (bb)
6481 for (si = gsi_start_bb (bb); !gsi_end_p (si);)
6483 gimple stmt = gsi_stmt (si);
6484 gimple use_stmt;
6486 if (is_gimple_assign (stmt)
6487 && gimple_assign_rhs_code (stmt) == ASSERT_EXPR)
6489 tree rhs = gimple_assign_rhs1 (stmt);
6490 tree var;
6491 tree cond = fold (ASSERT_EXPR_COND (rhs));
6492 use_operand_p use_p;
6493 imm_use_iterator iter;
6495 gcc_assert (cond != boolean_false_node);
6497 /* Propagate the RHS into every use of the LHS. */
6498 var = ASSERT_EXPR_VAR (rhs);
6499 FOR_EACH_IMM_USE_STMT (use_stmt, iter,
6500 gimple_assign_lhs (stmt))
6501 FOR_EACH_IMM_USE_ON_STMT (use_p, iter)
6503 SET_USE (use_p, var);
6504 gcc_assert (TREE_CODE (var) == SSA_NAME);
6507 /* And finally, remove the copy, it is not needed. */
6508 gsi_remove (&si, true);
6509 release_defs (stmt);
6511 else
6512 gsi_next (&si);
6517 /* Return true if STMT is interesting for VRP. */
6519 static bool
6520 stmt_interesting_for_vrp (gimple stmt)
6522 if (gimple_code (stmt) == GIMPLE_PHI)
6524 tree res = gimple_phi_result (stmt);
6525 return (!virtual_operand_p (res)
6526 && (INTEGRAL_TYPE_P (TREE_TYPE (res))
6527 || POINTER_TYPE_P (TREE_TYPE (res))));
6529 else if (is_gimple_assign (stmt) || is_gimple_call (stmt))
6531 tree lhs = gimple_get_lhs (stmt);
6533 /* In general, assignments with virtual operands are not useful
6534 for deriving ranges, with the obvious exception of calls to
6535 builtin functions. */
6536 if (lhs && TREE_CODE (lhs) == SSA_NAME
6537 && (INTEGRAL_TYPE_P (TREE_TYPE (lhs))
6538 || POINTER_TYPE_P (TREE_TYPE (lhs)))
6539 && ((is_gimple_call (stmt)
6540 && gimple_call_fndecl (stmt) != NULL_TREE
6541 && (DECL_BUILT_IN (gimple_call_fndecl (stmt))
6542 || DECL_IS_OPERATOR_NEW (gimple_call_fndecl (stmt))))
6543 || !gimple_vuse (stmt)))
6544 return true;
6546 else if (gimple_code (stmt) == GIMPLE_COND
6547 || gimple_code (stmt) == GIMPLE_SWITCH)
6548 return true;
6550 return false;
6554 /* Initialize local data structures for VRP. */
6556 static void
6557 vrp_initialize (void)
6559 basic_block bb;
6561 values_propagated = false;
6562 num_vr_values = num_ssa_names;
6563 vr_value = XCNEWVEC (value_range_t *, num_vr_values);
6564 vr_phi_edge_counts = XCNEWVEC (int, num_ssa_names);
6566 FOR_EACH_BB (bb)
6568 gimple_stmt_iterator si;
6570 for (si = gsi_start_phis (bb); !gsi_end_p (si); gsi_next (&si))
6572 gimple phi = gsi_stmt (si);
6573 if (!stmt_interesting_for_vrp (phi))
6575 tree lhs = PHI_RESULT (phi);
6576 set_value_range_to_varying (get_value_range (lhs));
6577 prop_set_simulate_again (phi, false);
6579 else
6580 prop_set_simulate_again (phi, true);
6583 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
6585 gimple stmt = gsi_stmt (si);
6587 /* If the statement is a control insn, then we do not
6588 want to avoid simulating the statement once. Failure
6589 to do so means that those edges will never get added. */
6590 if (stmt_ends_bb_p (stmt))
6591 prop_set_simulate_again (stmt, true);
6592 else if (!stmt_interesting_for_vrp (stmt))
6594 ssa_op_iter i;
6595 tree def;
6596 FOR_EACH_SSA_TREE_OPERAND (def, stmt, i, SSA_OP_DEF)
6597 set_value_range_to_varying (get_value_range (def));
6598 prop_set_simulate_again (stmt, false);
6600 else
6601 prop_set_simulate_again (stmt, true);
6606 /* Return the singleton value-range for NAME or NAME. */
6608 static inline tree
6609 vrp_valueize (tree name)
6611 if (TREE_CODE (name) == SSA_NAME)
6613 value_range_t *vr = get_value_range (name);
6614 if (vr->type == VR_RANGE
6615 && (vr->min == vr->max
6616 || operand_equal_p (vr->min, vr->max, 0)))
6617 return vr->min;
6619 return name;
6622 /* Visit assignment STMT. If it produces an interesting range, record
6623 the SSA name in *OUTPUT_P. */
6625 static enum ssa_prop_result
6626 vrp_visit_assignment_or_call (gimple stmt, tree *output_p)
6628 tree def, lhs;
6629 ssa_op_iter iter;
6630 enum gimple_code code = gimple_code (stmt);
6631 lhs = gimple_get_lhs (stmt);
6633 /* We only keep track of ranges in integral and pointer types. */
6634 if (TREE_CODE (lhs) == SSA_NAME
6635 && ((INTEGRAL_TYPE_P (TREE_TYPE (lhs))
6636 /* It is valid to have NULL MIN/MAX values on a type. See
6637 build_range_type. */
6638 && TYPE_MIN_VALUE (TREE_TYPE (lhs))
6639 && TYPE_MAX_VALUE (TREE_TYPE (lhs)))
6640 || POINTER_TYPE_P (TREE_TYPE (lhs))))
6642 value_range_t new_vr = VR_INITIALIZER;
6644 /* Try folding the statement to a constant first. */
6645 tree tem = gimple_fold_stmt_to_constant (stmt, vrp_valueize);
6646 if (tem && !is_overflow_infinity (tem))
6647 set_value_range (&new_vr, VR_RANGE, tem, tem, NULL);
6648 /* Then dispatch to value-range extracting functions. */
6649 else if (code == GIMPLE_CALL)
6650 extract_range_basic (&new_vr, stmt);
6651 else
6652 extract_range_from_assignment (&new_vr, stmt);
6654 if (update_value_range (lhs, &new_vr))
6656 *output_p = lhs;
6658 if (dump_file && (dump_flags & TDF_DETAILS))
6660 fprintf (dump_file, "Found new range for ");
6661 print_generic_expr (dump_file, lhs, 0);
6662 fprintf (dump_file, ": ");
6663 dump_value_range (dump_file, &new_vr);
6664 fprintf (dump_file, "\n\n");
6667 if (new_vr.type == VR_VARYING)
6668 return SSA_PROP_VARYING;
6670 return SSA_PROP_INTERESTING;
6673 return SSA_PROP_NOT_INTERESTING;
6676 /* Every other statement produces no useful ranges. */
6677 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_DEF)
6678 set_value_range_to_varying (get_value_range (def));
6680 return SSA_PROP_VARYING;
6683 /* Helper that gets the value range of the SSA_NAME with version I
6684 or a symbolic range containing the SSA_NAME only if the value range
6685 is varying or undefined. */
6687 static inline value_range_t
6688 get_vr_for_comparison (int i)
6690 value_range_t vr = *get_value_range (ssa_name (i));
6692 /* If name N_i does not have a valid range, use N_i as its own
6693 range. This allows us to compare against names that may
6694 have N_i in their ranges. */
6695 if (vr.type == VR_VARYING || vr.type == VR_UNDEFINED)
6697 vr.type = VR_RANGE;
6698 vr.min = ssa_name (i);
6699 vr.max = ssa_name (i);
6702 return vr;
6705 /* Compare all the value ranges for names equivalent to VAR with VAL
6706 using comparison code COMP. Return the same value returned by
6707 compare_range_with_value, including the setting of
6708 *STRICT_OVERFLOW_P. */
6710 static tree
6711 compare_name_with_value (enum tree_code comp, tree var, tree val,
6712 bool *strict_overflow_p)
6714 bitmap_iterator bi;
6715 unsigned i;
6716 bitmap e;
6717 tree retval, t;
6718 int used_strict_overflow;
6719 bool sop;
6720 value_range_t equiv_vr;
6722 /* Get the set of equivalences for VAR. */
6723 e = get_value_range (var)->equiv;
6725 /* Start at -1. Set it to 0 if we do a comparison without relying
6726 on overflow, or 1 if all comparisons rely on overflow. */
6727 used_strict_overflow = -1;
6729 /* Compare vars' value range with val. */
6730 equiv_vr = get_vr_for_comparison (SSA_NAME_VERSION (var));
6731 sop = false;
6732 retval = compare_range_with_value (comp, &equiv_vr, val, &sop);
6733 if (retval)
6734 used_strict_overflow = sop ? 1 : 0;
6736 /* If the equiv set is empty we have done all work we need to do. */
6737 if (e == NULL)
6739 if (retval
6740 && used_strict_overflow > 0)
6741 *strict_overflow_p = true;
6742 return retval;
6745 EXECUTE_IF_SET_IN_BITMAP (e, 0, i, bi)
6747 equiv_vr = get_vr_for_comparison (i);
6748 sop = false;
6749 t = compare_range_with_value (comp, &equiv_vr, val, &sop);
6750 if (t)
6752 /* If we get different answers from different members
6753 of the equivalence set this check must be in a dead
6754 code region. Folding it to a trap representation
6755 would be correct here. For now just return don't-know. */
6756 if (retval != NULL
6757 && t != retval)
6759 retval = NULL_TREE;
6760 break;
6762 retval = t;
6764 if (!sop)
6765 used_strict_overflow = 0;
6766 else if (used_strict_overflow < 0)
6767 used_strict_overflow = 1;
6771 if (retval
6772 && used_strict_overflow > 0)
6773 *strict_overflow_p = true;
6775 return retval;
6779 /* Given a comparison code COMP and names N1 and N2, compare all the
6780 ranges equivalent to N1 against all the ranges equivalent to N2
6781 to determine the value of N1 COMP N2. Return the same value
6782 returned by compare_ranges. Set *STRICT_OVERFLOW_P to indicate
6783 whether we relied on an overflow infinity in the comparison. */
6786 static tree
6787 compare_names (enum tree_code comp, tree n1, tree n2,
6788 bool *strict_overflow_p)
6790 tree t, retval;
6791 bitmap e1, e2;
6792 bitmap_iterator bi1, bi2;
6793 unsigned i1, i2;
6794 int used_strict_overflow;
6795 static bitmap_obstack *s_obstack = NULL;
6796 static bitmap s_e1 = NULL, s_e2 = NULL;
6798 /* Compare the ranges of every name equivalent to N1 against the
6799 ranges of every name equivalent to N2. */
6800 e1 = get_value_range (n1)->equiv;
6801 e2 = get_value_range (n2)->equiv;
6803 /* Use the fake bitmaps if e1 or e2 are not available. */
6804 if (s_obstack == NULL)
6806 s_obstack = XNEW (bitmap_obstack);
6807 bitmap_obstack_initialize (s_obstack);
6808 s_e1 = BITMAP_ALLOC (s_obstack);
6809 s_e2 = BITMAP_ALLOC (s_obstack);
6811 if (e1 == NULL)
6812 e1 = s_e1;
6813 if (e2 == NULL)
6814 e2 = s_e2;
6816 /* Add N1 and N2 to their own set of equivalences to avoid
6817 duplicating the body of the loop just to check N1 and N2
6818 ranges. */
6819 bitmap_set_bit (e1, SSA_NAME_VERSION (n1));
6820 bitmap_set_bit (e2, SSA_NAME_VERSION (n2));
6822 /* If the equivalence sets have a common intersection, then the two
6823 names can be compared without checking their ranges. */
6824 if (bitmap_intersect_p (e1, e2))
6826 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
6827 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
6829 return (comp == EQ_EXPR || comp == GE_EXPR || comp == LE_EXPR)
6830 ? boolean_true_node
6831 : boolean_false_node;
6834 /* Start at -1. Set it to 0 if we do a comparison without relying
6835 on overflow, or 1 if all comparisons rely on overflow. */
6836 used_strict_overflow = -1;
6838 /* Otherwise, compare all the equivalent ranges. First, add N1 and
6839 N2 to their own set of equivalences to avoid duplicating the body
6840 of the loop just to check N1 and N2 ranges. */
6841 EXECUTE_IF_SET_IN_BITMAP (e1, 0, i1, bi1)
6843 value_range_t vr1 = get_vr_for_comparison (i1);
6845 t = retval = NULL_TREE;
6846 EXECUTE_IF_SET_IN_BITMAP (e2, 0, i2, bi2)
6848 bool sop = false;
6850 value_range_t vr2 = get_vr_for_comparison (i2);
6852 t = compare_ranges (comp, &vr1, &vr2, &sop);
6853 if (t)
6855 /* If we get different answers from different members
6856 of the equivalence set this check must be in a dead
6857 code region. Folding it to a trap representation
6858 would be correct here. For now just return don't-know. */
6859 if (retval != NULL
6860 && t != retval)
6862 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
6863 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
6864 return NULL_TREE;
6866 retval = t;
6868 if (!sop)
6869 used_strict_overflow = 0;
6870 else if (used_strict_overflow < 0)
6871 used_strict_overflow = 1;
6875 if (retval)
6877 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
6878 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
6879 if (used_strict_overflow > 0)
6880 *strict_overflow_p = true;
6881 return retval;
6885 /* None of the equivalent ranges are useful in computing this
6886 comparison. */
6887 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
6888 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
6889 return NULL_TREE;
6892 /* Helper function for vrp_evaluate_conditional_warnv. */
6894 static tree
6895 vrp_evaluate_conditional_warnv_with_ops_using_ranges (enum tree_code code,
6896 tree op0, tree op1,
6897 bool * strict_overflow_p)
6899 value_range_t *vr0, *vr1;
6901 vr0 = (TREE_CODE (op0) == SSA_NAME) ? get_value_range (op0) : NULL;
6902 vr1 = (TREE_CODE (op1) == SSA_NAME) ? get_value_range (op1) : NULL;
6904 if (vr0 && vr1)
6905 return compare_ranges (code, vr0, vr1, strict_overflow_p);
6906 else if (vr0 && vr1 == NULL)
6907 return compare_range_with_value (code, vr0, op1, strict_overflow_p);
6908 else if (vr0 == NULL && vr1)
6909 return (compare_range_with_value
6910 (swap_tree_comparison (code), vr1, op0, strict_overflow_p));
6911 return NULL;
6914 /* Helper function for vrp_evaluate_conditional_warnv. */
6916 static tree
6917 vrp_evaluate_conditional_warnv_with_ops (enum tree_code code, tree op0,
6918 tree op1, bool use_equiv_p,
6919 bool *strict_overflow_p, bool *only_ranges)
6921 tree ret;
6922 if (only_ranges)
6923 *only_ranges = true;
6925 /* We only deal with integral and pointer types. */
6926 if (!INTEGRAL_TYPE_P (TREE_TYPE (op0))
6927 && !POINTER_TYPE_P (TREE_TYPE (op0)))
6928 return NULL_TREE;
6930 if (use_equiv_p)
6932 if (only_ranges
6933 && (ret = vrp_evaluate_conditional_warnv_with_ops_using_ranges
6934 (code, op0, op1, strict_overflow_p)))
6935 return ret;
6936 *only_ranges = false;
6937 if (TREE_CODE (op0) == SSA_NAME && TREE_CODE (op1) == SSA_NAME)
6938 return compare_names (code, op0, op1, strict_overflow_p);
6939 else if (TREE_CODE (op0) == SSA_NAME)
6940 return compare_name_with_value (code, op0, op1, strict_overflow_p);
6941 else if (TREE_CODE (op1) == SSA_NAME)
6942 return (compare_name_with_value
6943 (swap_tree_comparison (code), op1, op0, strict_overflow_p));
6945 else
6946 return vrp_evaluate_conditional_warnv_with_ops_using_ranges (code, op0, op1,
6947 strict_overflow_p);
6948 return NULL_TREE;
6951 /* Given (CODE OP0 OP1) within STMT, try to simplify it based on value range
6952 information. Return NULL if the conditional can not be evaluated.
6953 The ranges of all the names equivalent with the operands in COND
6954 will be used when trying to compute the value. If the result is
6955 based on undefined signed overflow, issue a warning if
6956 appropriate. */
6958 static tree
6959 vrp_evaluate_conditional (enum tree_code code, tree op0, tree op1, gimple stmt)
6961 bool sop;
6962 tree ret;
6963 bool only_ranges;
6965 /* Some passes and foldings leak constants with overflow flag set
6966 into the IL. Avoid doing wrong things with these and bail out. */
6967 if ((TREE_CODE (op0) == INTEGER_CST
6968 && TREE_OVERFLOW (op0))
6969 || (TREE_CODE (op1) == INTEGER_CST
6970 && TREE_OVERFLOW (op1)))
6971 return NULL_TREE;
6973 sop = false;
6974 ret = vrp_evaluate_conditional_warnv_with_ops (code, op0, op1, true, &sop,
6975 &only_ranges);
6977 if (ret && sop)
6979 enum warn_strict_overflow_code wc;
6980 const char* warnmsg;
6982 if (is_gimple_min_invariant (ret))
6984 wc = WARN_STRICT_OVERFLOW_CONDITIONAL;
6985 warnmsg = G_("assuming signed overflow does not occur when "
6986 "simplifying conditional to constant");
6988 else
6990 wc = WARN_STRICT_OVERFLOW_COMPARISON;
6991 warnmsg = G_("assuming signed overflow does not occur when "
6992 "simplifying conditional");
6995 if (issue_strict_overflow_warning (wc))
6997 location_t location;
6999 if (!gimple_has_location (stmt))
7000 location = input_location;
7001 else
7002 location = gimple_location (stmt);
7003 warning_at (location, OPT_Wstrict_overflow, "%s", warnmsg);
7007 if (warn_type_limits
7008 && ret && only_ranges
7009 && TREE_CODE_CLASS (code) == tcc_comparison
7010 && TREE_CODE (op0) == SSA_NAME)
7012 /* If the comparison is being folded and the operand on the LHS
7013 is being compared against a constant value that is outside of
7014 the natural range of OP0's type, then the predicate will
7015 always fold regardless of the value of OP0. If -Wtype-limits
7016 was specified, emit a warning. */
7017 tree type = TREE_TYPE (op0);
7018 value_range_t *vr0 = get_value_range (op0);
7020 if (vr0->type != VR_VARYING
7021 && INTEGRAL_TYPE_P (type)
7022 && vrp_val_is_min (vr0->min)
7023 && vrp_val_is_max (vr0->max)
7024 && is_gimple_min_invariant (op1))
7026 location_t location;
7028 if (!gimple_has_location (stmt))
7029 location = input_location;
7030 else
7031 location = gimple_location (stmt);
7033 warning_at (location, OPT_Wtype_limits,
7034 integer_zerop (ret)
7035 ? G_("comparison always false "
7036 "due to limited range of data type")
7037 : G_("comparison always true "
7038 "due to limited range of data type"));
7042 return ret;
7046 /* Visit conditional statement STMT. If we can determine which edge
7047 will be taken out of STMT's basic block, record it in
7048 *TAKEN_EDGE_P and return SSA_PROP_INTERESTING. Otherwise, return
7049 SSA_PROP_VARYING. */
7051 static enum ssa_prop_result
7052 vrp_visit_cond_stmt (gimple stmt, edge *taken_edge_p)
7054 tree val;
7055 bool sop;
7057 *taken_edge_p = NULL;
7059 if (dump_file && (dump_flags & TDF_DETAILS))
7061 tree use;
7062 ssa_op_iter i;
7064 fprintf (dump_file, "\nVisiting conditional with predicate: ");
7065 print_gimple_stmt (dump_file, stmt, 0, 0);
7066 fprintf (dump_file, "\nWith known ranges\n");
7068 FOR_EACH_SSA_TREE_OPERAND (use, stmt, i, SSA_OP_USE)
7070 fprintf (dump_file, "\t");
7071 print_generic_expr (dump_file, use, 0);
7072 fprintf (dump_file, ": ");
7073 dump_value_range (dump_file, vr_value[SSA_NAME_VERSION (use)]);
7076 fprintf (dump_file, "\n");
7079 /* Compute the value of the predicate COND by checking the known
7080 ranges of each of its operands.
7082 Note that we cannot evaluate all the equivalent ranges here
7083 because those ranges may not yet be final and with the current
7084 propagation strategy, we cannot determine when the value ranges
7085 of the names in the equivalence set have changed.
7087 For instance, given the following code fragment
7089 i_5 = PHI <8, i_13>
7091 i_14 = ASSERT_EXPR <i_5, i_5 != 0>
7092 if (i_14 == 1)
7095 Assume that on the first visit to i_14, i_5 has the temporary
7096 range [8, 8] because the second argument to the PHI function is
7097 not yet executable. We derive the range ~[0, 0] for i_14 and the
7098 equivalence set { i_5 }. So, when we visit 'if (i_14 == 1)' for
7099 the first time, since i_14 is equivalent to the range [8, 8], we
7100 determine that the predicate is always false.
7102 On the next round of propagation, i_13 is determined to be
7103 VARYING, which causes i_5 to drop down to VARYING. So, another
7104 visit to i_14 is scheduled. In this second visit, we compute the
7105 exact same range and equivalence set for i_14, namely ~[0, 0] and
7106 { i_5 }. But we did not have the previous range for i_5
7107 registered, so vrp_visit_assignment thinks that the range for
7108 i_14 has not changed. Therefore, the predicate 'if (i_14 == 1)'
7109 is not visited again, which stops propagation from visiting
7110 statements in the THEN clause of that if().
7112 To properly fix this we would need to keep the previous range
7113 value for the names in the equivalence set. This way we would've
7114 discovered that from one visit to the other i_5 changed from
7115 range [8, 8] to VR_VARYING.
7117 However, fixing this apparent limitation may not be worth the
7118 additional checking. Testing on several code bases (GCC, DLV,
7119 MICO, TRAMP3D and SPEC2000) showed that doing this results in
7120 4 more predicates folded in SPEC. */
7121 sop = false;
7123 val = vrp_evaluate_conditional_warnv_with_ops (gimple_cond_code (stmt),
7124 gimple_cond_lhs (stmt),
7125 gimple_cond_rhs (stmt),
7126 false, &sop, NULL);
7127 if (val)
7129 if (!sop)
7130 *taken_edge_p = find_taken_edge (gimple_bb (stmt), val);
7131 else
7133 if (dump_file && (dump_flags & TDF_DETAILS))
7134 fprintf (dump_file,
7135 "\nIgnoring predicate evaluation because "
7136 "it assumes that signed overflow is undefined");
7137 val = NULL_TREE;
7141 if (dump_file && (dump_flags & TDF_DETAILS))
7143 fprintf (dump_file, "\nPredicate evaluates to: ");
7144 if (val == NULL_TREE)
7145 fprintf (dump_file, "DON'T KNOW\n");
7146 else
7147 print_generic_stmt (dump_file, val, 0);
7150 return (*taken_edge_p) ? SSA_PROP_INTERESTING : SSA_PROP_VARYING;
7153 /* Searches the case label vector VEC for the index *IDX of the CASE_LABEL
7154 that includes the value VAL. The search is restricted to the range
7155 [START_IDX, n - 1] where n is the size of VEC.
7157 If there is a CASE_LABEL for VAL, its index is placed in IDX and true is
7158 returned.
7160 If there is no CASE_LABEL for VAL and there is one that is larger than VAL,
7161 it is placed in IDX and false is returned.
7163 If VAL is larger than any CASE_LABEL, n is placed on IDX and false is
7164 returned. */
7166 static bool
7167 find_case_label_index (gimple stmt, size_t start_idx, tree val, size_t *idx)
7169 size_t n = gimple_switch_num_labels (stmt);
7170 size_t low, high;
7172 /* Find case label for minimum of the value range or the next one.
7173 At each iteration we are searching in [low, high - 1]. */
7175 for (low = start_idx, high = n; high != low; )
7177 tree t;
7178 int cmp;
7179 /* Note that i != high, so we never ask for n. */
7180 size_t i = (high + low) / 2;
7181 t = gimple_switch_label (stmt, i);
7183 /* Cache the result of comparing CASE_LOW and val. */
7184 cmp = tree_int_cst_compare (CASE_LOW (t), val);
7186 if (cmp == 0)
7188 /* Ranges cannot be empty. */
7189 *idx = i;
7190 return true;
7192 else if (cmp > 0)
7193 high = i;
7194 else
7196 low = i + 1;
7197 if (CASE_HIGH (t) != NULL
7198 && tree_int_cst_compare (CASE_HIGH (t), val) >= 0)
7200 *idx = i;
7201 return true;
7206 *idx = high;
7207 return false;
7210 /* Searches the case label vector VEC for the range of CASE_LABELs that is used
7211 for values between MIN and MAX. The first index is placed in MIN_IDX. The
7212 last index is placed in MAX_IDX. If the range of CASE_LABELs is empty
7213 then MAX_IDX < MIN_IDX.
7214 Returns true if the default label is not needed. */
7216 static bool
7217 find_case_label_range (gimple stmt, tree min, tree max, size_t *min_idx,
7218 size_t *max_idx)
7220 size_t i, j;
7221 bool min_take_default = !find_case_label_index (stmt, 1, min, &i);
7222 bool max_take_default = !find_case_label_index (stmt, i, max, &j);
7224 if (i == j
7225 && min_take_default
7226 && max_take_default)
7228 /* Only the default case label reached.
7229 Return an empty range. */
7230 *min_idx = 1;
7231 *max_idx = 0;
7232 return false;
7234 else
7236 bool take_default = min_take_default || max_take_default;
7237 tree low, high;
7238 size_t k;
7240 if (max_take_default)
7241 j--;
7243 /* If the case label range is continuous, we do not need
7244 the default case label. Verify that. */
7245 high = CASE_LOW (gimple_switch_label (stmt, i));
7246 if (CASE_HIGH (gimple_switch_label (stmt, i)))
7247 high = CASE_HIGH (gimple_switch_label (stmt, i));
7248 for (k = i + 1; k <= j; ++k)
7250 low = CASE_LOW (gimple_switch_label (stmt, k));
7251 if (!integer_onep (int_const_binop (MINUS_EXPR, low, high)))
7253 take_default = true;
7254 break;
7256 high = low;
7257 if (CASE_HIGH (gimple_switch_label (stmt, k)))
7258 high = CASE_HIGH (gimple_switch_label (stmt, k));
7261 *min_idx = i;
7262 *max_idx = j;
7263 return !take_default;
7267 /* Searches the case label vector VEC for the ranges of CASE_LABELs that are
7268 used in range VR. The indices are placed in MIN_IDX1, MAX_IDX, MIN_IDX2 and
7269 MAX_IDX2. If the ranges of CASE_LABELs are empty then MAX_IDX1 < MIN_IDX1.
7270 Returns true if the default label is not needed. */
7272 static bool
7273 find_case_label_ranges (gimple stmt, value_range_t *vr, size_t *min_idx1,
7274 size_t *max_idx1, size_t *min_idx2,
7275 size_t *max_idx2)
7277 size_t i, j, k, l;
7278 unsigned int n = gimple_switch_num_labels (stmt);
7279 bool take_default;
7280 tree case_low, case_high;
7281 tree min = vr->min, max = vr->max;
7283 gcc_checking_assert (vr->type == VR_RANGE || vr->type == VR_ANTI_RANGE);
7285 take_default = !find_case_label_range (stmt, min, max, &i, &j);
7287 /* Set second range to emtpy. */
7288 *min_idx2 = 1;
7289 *max_idx2 = 0;
7291 if (vr->type == VR_RANGE)
7293 *min_idx1 = i;
7294 *max_idx1 = j;
7295 return !take_default;
7298 /* Set first range to all case labels. */
7299 *min_idx1 = 1;
7300 *max_idx1 = n - 1;
7302 if (i > j)
7303 return false;
7305 /* Make sure all the values of case labels [i , j] are contained in
7306 range [MIN, MAX]. */
7307 case_low = CASE_LOW (gimple_switch_label (stmt, i));
7308 case_high = CASE_HIGH (gimple_switch_label (stmt, j));
7309 if (tree_int_cst_compare (case_low, min) < 0)
7310 i += 1;
7311 if (case_high != NULL_TREE
7312 && tree_int_cst_compare (max, case_high) < 0)
7313 j -= 1;
7315 if (i > j)
7316 return false;
7318 /* If the range spans case labels [i, j], the corresponding anti-range spans
7319 the labels [1, i - 1] and [j + 1, n - 1]. */
7320 k = j + 1;
7321 l = n - 1;
7322 if (k > l)
7324 k = 1;
7325 l = 0;
7328 j = i - 1;
7329 i = 1;
7330 if (i > j)
7332 i = k;
7333 j = l;
7334 k = 1;
7335 l = 0;
7338 *min_idx1 = i;
7339 *max_idx1 = j;
7340 *min_idx2 = k;
7341 *max_idx2 = l;
7342 return false;
7345 /* Visit switch statement STMT. If we can determine which edge
7346 will be taken out of STMT's basic block, record it in
7347 *TAKEN_EDGE_P and return SSA_PROP_INTERESTING. Otherwise, return
7348 SSA_PROP_VARYING. */
7350 static enum ssa_prop_result
7351 vrp_visit_switch_stmt (gimple stmt, edge *taken_edge_p)
7353 tree op, val;
7354 value_range_t *vr;
7355 size_t i = 0, j = 0, k, l;
7356 bool take_default;
7358 *taken_edge_p = NULL;
7359 op = gimple_switch_index (stmt);
7360 if (TREE_CODE (op) != SSA_NAME)
7361 return SSA_PROP_VARYING;
7363 vr = get_value_range (op);
7364 if (dump_file && (dump_flags & TDF_DETAILS))
7366 fprintf (dump_file, "\nVisiting switch expression with operand ");
7367 print_generic_expr (dump_file, op, 0);
7368 fprintf (dump_file, " with known range ");
7369 dump_value_range (dump_file, vr);
7370 fprintf (dump_file, "\n");
7373 if ((vr->type != VR_RANGE
7374 && vr->type != VR_ANTI_RANGE)
7375 || symbolic_range_p (vr))
7376 return SSA_PROP_VARYING;
7378 /* Find the single edge that is taken from the switch expression. */
7379 take_default = !find_case_label_ranges (stmt, vr, &i, &j, &k, &l);
7381 /* Check if the range spans no CASE_LABEL. If so, we only reach the default
7382 label */
7383 if (j < i)
7385 gcc_assert (take_default);
7386 val = gimple_switch_default_label (stmt);
7388 else
7390 /* Check if labels with index i to j and maybe the default label
7391 are all reaching the same label. */
7393 val = gimple_switch_label (stmt, i);
7394 if (take_default
7395 && CASE_LABEL (gimple_switch_default_label (stmt))
7396 != CASE_LABEL (val))
7398 if (dump_file && (dump_flags & TDF_DETAILS))
7399 fprintf (dump_file, " not a single destination for this "
7400 "range\n");
7401 return SSA_PROP_VARYING;
7403 for (++i; i <= j; ++i)
7405 if (CASE_LABEL (gimple_switch_label (stmt, i)) != CASE_LABEL (val))
7407 if (dump_file && (dump_flags & TDF_DETAILS))
7408 fprintf (dump_file, " not a single destination for this "
7409 "range\n");
7410 return SSA_PROP_VARYING;
7413 for (; k <= l; ++k)
7415 if (CASE_LABEL (gimple_switch_label (stmt, k)) != CASE_LABEL (val))
7417 if (dump_file && (dump_flags & TDF_DETAILS))
7418 fprintf (dump_file, " not a single destination for this "
7419 "range\n");
7420 return SSA_PROP_VARYING;
7425 *taken_edge_p = find_edge (gimple_bb (stmt),
7426 label_to_block (CASE_LABEL (val)));
7428 if (dump_file && (dump_flags & TDF_DETAILS))
7430 fprintf (dump_file, " will take edge to ");
7431 print_generic_stmt (dump_file, CASE_LABEL (val), 0);
7434 return SSA_PROP_INTERESTING;
7438 /* Evaluate statement STMT. If the statement produces a useful range,
7439 return SSA_PROP_INTERESTING and record the SSA name with the
7440 interesting range into *OUTPUT_P.
7442 If STMT is a conditional branch and we can determine its truth
7443 value, the taken edge is recorded in *TAKEN_EDGE_P.
7445 If STMT produces a varying value, return SSA_PROP_VARYING. */
7447 static enum ssa_prop_result
7448 vrp_visit_stmt (gimple stmt, edge *taken_edge_p, tree *output_p)
7450 tree def;
7451 ssa_op_iter iter;
7453 if (dump_file && (dump_flags & TDF_DETAILS))
7455 fprintf (dump_file, "\nVisiting statement:\n");
7456 print_gimple_stmt (dump_file, stmt, 0, dump_flags);
7457 fprintf (dump_file, "\n");
7460 if (!stmt_interesting_for_vrp (stmt))
7461 gcc_assert (stmt_ends_bb_p (stmt));
7462 else if (is_gimple_assign (stmt) || is_gimple_call (stmt))
7463 return vrp_visit_assignment_or_call (stmt, output_p);
7464 else if (gimple_code (stmt) == GIMPLE_COND)
7465 return vrp_visit_cond_stmt (stmt, taken_edge_p);
7466 else if (gimple_code (stmt) == GIMPLE_SWITCH)
7467 return vrp_visit_switch_stmt (stmt, taken_edge_p);
7469 /* All other statements produce nothing of interest for VRP, so mark
7470 their outputs varying and prevent further simulation. */
7471 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_DEF)
7472 set_value_range_to_varying (get_value_range (def));
7474 return SSA_PROP_VARYING;
7477 /* Union the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
7478 { VR1TYPE, VR0MIN, VR0MAX } and store the result
7479 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
7480 possible such range. The resulting range is not canonicalized. */
7482 static void
7483 union_ranges (enum value_range_type *vr0type,
7484 tree *vr0min, tree *vr0max,
7485 enum value_range_type vr1type,
7486 tree vr1min, tree vr1max)
7488 bool mineq = operand_equal_p (*vr0min, vr1min, 0);
7489 bool maxeq = operand_equal_p (*vr0max, vr1max, 0);
7491 /* [] is vr0, () is vr1 in the following classification comments. */
7492 if (mineq && maxeq)
7494 /* [( )] */
7495 if (*vr0type == vr1type)
7496 /* Nothing to do for equal ranges. */
7498 else if ((*vr0type == VR_RANGE
7499 && vr1type == VR_ANTI_RANGE)
7500 || (*vr0type == VR_ANTI_RANGE
7501 && vr1type == VR_RANGE))
7503 /* For anti-range with range union the result is varying. */
7504 goto give_up;
7506 else
7507 gcc_unreachable ();
7509 else if (operand_less_p (*vr0max, vr1min) == 1
7510 || operand_less_p (vr1max, *vr0min) == 1)
7512 /* [ ] ( ) or ( ) [ ]
7513 If the ranges have an empty intersection, result of the union
7514 operation is the anti-range or if both are anti-ranges
7515 it covers all. */
7516 if (*vr0type == VR_ANTI_RANGE
7517 && vr1type == VR_ANTI_RANGE)
7518 goto give_up;
7519 else if (*vr0type == VR_ANTI_RANGE
7520 && vr1type == VR_RANGE)
7522 else if (*vr0type == VR_RANGE
7523 && vr1type == VR_ANTI_RANGE)
7525 *vr0type = vr1type;
7526 *vr0min = vr1min;
7527 *vr0max = vr1max;
7529 else if (*vr0type == VR_RANGE
7530 && vr1type == VR_RANGE)
7532 /* The result is the convex hull of both ranges. */
7533 if (operand_less_p (*vr0max, vr1min) == 1)
7535 /* If the result can be an anti-range, create one. */
7536 if (TREE_CODE (*vr0max) == INTEGER_CST
7537 && TREE_CODE (vr1min) == INTEGER_CST
7538 && vrp_val_is_min (*vr0min)
7539 && vrp_val_is_max (vr1max))
7541 tree min = int_const_binop (PLUS_EXPR,
7542 *vr0max, integer_one_node);
7543 tree max = int_const_binop (MINUS_EXPR,
7544 vr1min, integer_one_node);
7545 if (!operand_less_p (max, min))
7547 *vr0type = VR_ANTI_RANGE;
7548 *vr0min = min;
7549 *vr0max = max;
7551 else
7552 *vr0max = vr1max;
7554 else
7555 *vr0max = vr1max;
7557 else
7559 /* If the result can be an anti-range, create one. */
7560 if (TREE_CODE (vr1max) == INTEGER_CST
7561 && TREE_CODE (*vr0min) == INTEGER_CST
7562 && vrp_val_is_min (vr1min)
7563 && vrp_val_is_max (*vr0max))
7565 tree min = int_const_binop (PLUS_EXPR,
7566 vr1max, integer_one_node);
7567 tree max = int_const_binop (MINUS_EXPR,
7568 *vr0min, integer_one_node);
7569 if (!operand_less_p (max, min))
7571 *vr0type = VR_ANTI_RANGE;
7572 *vr0min = min;
7573 *vr0max = max;
7575 else
7576 *vr0min = vr1min;
7578 else
7579 *vr0min = vr1min;
7582 else
7583 gcc_unreachable ();
7585 else if ((maxeq || operand_less_p (vr1max, *vr0max) == 1)
7586 && (mineq || operand_less_p (*vr0min, vr1min) == 1))
7588 /* [ ( ) ] or [( ) ] or [ ( )] */
7589 if (*vr0type == VR_RANGE
7590 && vr1type == VR_RANGE)
7592 else if (*vr0type == VR_ANTI_RANGE
7593 && vr1type == VR_ANTI_RANGE)
7595 *vr0type = vr1type;
7596 *vr0min = vr1min;
7597 *vr0max = vr1max;
7599 else if (*vr0type == VR_ANTI_RANGE
7600 && vr1type == VR_RANGE)
7602 /* Arbitrarily choose the right or left gap. */
7603 if (!mineq && TREE_CODE (vr1min) == INTEGER_CST)
7604 *vr0max = int_const_binop (MINUS_EXPR, vr1min, integer_one_node);
7605 else if (!maxeq && TREE_CODE (vr1max) == INTEGER_CST)
7606 *vr0min = int_const_binop (PLUS_EXPR, vr1max, integer_one_node);
7607 else
7608 goto give_up;
7610 else if (*vr0type == VR_RANGE
7611 && vr1type == VR_ANTI_RANGE)
7612 /* The result covers everything. */
7613 goto give_up;
7614 else
7615 gcc_unreachable ();
7617 else if ((maxeq || operand_less_p (*vr0max, vr1max) == 1)
7618 && (mineq || operand_less_p (vr1min, *vr0min) == 1))
7620 /* ( [ ] ) or ([ ] ) or ( [ ]) */
7621 if (*vr0type == VR_RANGE
7622 && vr1type == VR_RANGE)
7624 *vr0type = vr1type;
7625 *vr0min = vr1min;
7626 *vr0max = vr1max;
7628 else if (*vr0type == VR_ANTI_RANGE
7629 && vr1type == VR_ANTI_RANGE)
7631 else if (*vr0type == VR_RANGE
7632 && vr1type == VR_ANTI_RANGE)
7634 *vr0type = VR_ANTI_RANGE;
7635 if (!mineq && TREE_CODE (*vr0min) == INTEGER_CST)
7637 *vr0max = int_const_binop (MINUS_EXPR, *vr0min, integer_one_node);
7638 *vr0min = vr1min;
7640 else if (!maxeq && TREE_CODE (*vr0max) == INTEGER_CST)
7642 *vr0min = int_const_binop (PLUS_EXPR, *vr0max, integer_one_node);
7643 *vr0max = vr1max;
7645 else
7646 goto give_up;
7648 else if (*vr0type == VR_ANTI_RANGE
7649 && vr1type == VR_RANGE)
7650 /* The result covers everything. */
7651 goto give_up;
7652 else
7653 gcc_unreachable ();
7655 else if ((operand_less_p (vr1min, *vr0max) == 1
7656 || operand_equal_p (vr1min, *vr0max, 0))
7657 && operand_less_p (*vr0min, vr1min) == 1)
7659 /* [ ( ] ) or [ ]( ) */
7660 if (*vr0type == VR_RANGE
7661 && vr1type == VR_RANGE)
7662 *vr0max = vr1max;
7663 else if (*vr0type == VR_ANTI_RANGE
7664 && vr1type == VR_ANTI_RANGE)
7665 *vr0min = vr1min;
7666 else if (*vr0type == VR_ANTI_RANGE
7667 && vr1type == VR_RANGE)
7669 if (TREE_CODE (vr1min) == INTEGER_CST)
7670 *vr0max = int_const_binop (MINUS_EXPR, vr1min, integer_one_node);
7671 else
7672 goto give_up;
7674 else if (*vr0type == VR_RANGE
7675 && vr1type == VR_ANTI_RANGE)
7677 if (TREE_CODE (*vr0max) == INTEGER_CST)
7679 *vr0type = vr1type;
7680 *vr0min = int_const_binop (PLUS_EXPR, *vr0max, integer_one_node);
7681 *vr0max = vr1max;
7683 else
7684 goto give_up;
7686 else
7687 gcc_unreachable ();
7689 else if ((operand_less_p (*vr0min, vr1max) == 1
7690 || operand_equal_p (*vr0min, vr1max, 0))
7691 && operand_less_p (vr1min, *vr0min) == 1)
7693 /* ( [ ) ] or ( )[ ] */
7694 if (*vr0type == VR_RANGE
7695 && vr1type == VR_RANGE)
7696 *vr0min = vr1min;
7697 else if (*vr0type == VR_ANTI_RANGE
7698 && vr1type == VR_ANTI_RANGE)
7699 *vr0max = vr1max;
7700 else if (*vr0type == VR_ANTI_RANGE
7701 && vr1type == VR_RANGE)
7703 if (TREE_CODE (vr1max) == INTEGER_CST)
7704 *vr0min = int_const_binop (PLUS_EXPR, vr1max, integer_one_node);
7705 else
7706 goto give_up;
7708 else if (*vr0type == VR_RANGE
7709 && vr1type == VR_ANTI_RANGE)
7711 if (TREE_CODE (*vr0min) == INTEGER_CST)
7713 *vr0type = vr1type;
7714 *vr0min = vr1min;
7715 *vr0max = int_const_binop (MINUS_EXPR, *vr0min, integer_one_node);
7717 else
7718 goto give_up;
7720 else
7721 gcc_unreachable ();
7723 else
7724 goto give_up;
7726 return;
7728 give_up:
7729 *vr0type = VR_VARYING;
7730 *vr0min = NULL_TREE;
7731 *vr0max = NULL_TREE;
7734 /* Intersect the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
7735 { VR1TYPE, VR0MIN, VR0MAX } and store the result
7736 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
7737 possible such range. The resulting range is not canonicalized. */
7739 static void
7740 intersect_ranges (enum value_range_type *vr0type,
7741 tree *vr0min, tree *vr0max,
7742 enum value_range_type vr1type,
7743 tree vr1min, tree vr1max)
7745 bool mineq = operand_equal_p (*vr0min, vr1min, 0);
7746 bool maxeq = operand_equal_p (*vr0max, vr1max, 0);
7748 /* [] is vr0, () is vr1 in the following classification comments. */
7749 if (mineq && maxeq)
7751 /* [( )] */
7752 if (*vr0type == vr1type)
7753 /* Nothing to do for equal ranges. */
7755 else if ((*vr0type == VR_RANGE
7756 && vr1type == VR_ANTI_RANGE)
7757 || (*vr0type == VR_ANTI_RANGE
7758 && vr1type == VR_RANGE))
7760 /* For anti-range with range intersection the result is empty. */
7761 *vr0type = VR_UNDEFINED;
7762 *vr0min = NULL_TREE;
7763 *vr0max = NULL_TREE;
7765 else
7766 gcc_unreachable ();
7768 else if (operand_less_p (*vr0max, vr1min) == 1
7769 || operand_less_p (vr1max, *vr0min) == 1)
7771 /* [ ] ( ) or ( ) [ ]
7772 If the ranges have an empty intersection, the result of the
7773 intersect operation is the range for intersecting an
7774 anti-range with a range or empty when intersecting two ranges. */
7775 if (*vr0type == VR_RANGE
7776 && vr1type == VR_ANTI_RANGE)
7778 else if (*vr0type == VR_ANTI_RANGE
7779 && vr1type == VR_RANGE)
7781 *vr0type = vr1type;
7782 *vr0min = vr1min;
7783 *vr0max = vr1max;
7785 else if (*vr0type == VR_RANGE
7786 && vr1type == VR_RANGE)
7788 *vr0type = VR_UNDEFINED;
7789 *vr0min = NULL_TREE;
7790 *vr0max = NULL_TREE;
7792 else if (*vr0type == VR_ANTI_RANGE
7793 && vr1type == VR_ANTI_RANGE)
7795 /* If the anti-ranges are adjacent to each other merge them. */
7796 if (TREE_CODE (*vr0max) == INTEGER_CST
7797 && TREE_CODE (vr1min) == INTEGER_CST
7798 && operand_less_p (*vr0max, vr1min) == 1
7799 && integer_onep (int_const_binop (MINUS_EXPR,
7800 vr1min, *vr0max)))
7801 *vr0max = vr1max;
7802 else if (TREE_CODE (vr1max) == INTEGER_CST
7803 && TREE_CODE (*vr0min) == INTEGER_CST
7804 && operand_less_p (vr1max, *vr0min) == 1
7805 && integer_onep (int_const_binop (MINUS_EXPR,
7806 *vr0min, vr1max)))
7807 *vr0min = vr1min;
7808 /* Else arbitrarily take VR0. */
7811 else if ((maxeq || operand_less_p (vr1max, *vr0max) == 1)
7812 && (mineq || operand_less_p (*vr0min, vr1min) == 1))
7814 /* [ ( ) ] or [( ) ] or [ ( )] */
7815 if (*vr0type == VR_RANGE
7816 && vr1type == VR_RANGE)
7818 /* If both are ranges the result is the inner one. */
7819 *vr0type = vr1type;
7820 *vr0min = vr1min;
7821 *vr0max = vr1max;
7823 else if (*vr0type == VR_RANGE
7824 && vr1type == VR_ANTI_RANGE)
7826 /* Choose the right gap if the left one is empty. */
7827 if (mineq)
7829 if (TREE_CODE (vr1max) == INTEGER_CST)
7830 *vr0min = int_const_binop (PLUS_EXPR, vr1max, integer_one_node);
7831 else
7832 *vr0min = vr1max;
7834 /* Choose the left gap if the right one is empty. */
7835 else if (maxeq)
7837 if (TREE_CODE (vr1min) == INTEGER_CST)
7838 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
7839 integer_one_node);
7840 else
7841 *vr0max = vr1min;
7843 /* Choose the anti-range if the range is effectively varying. */
7844 else if (vrp_val_is_min (*vr0min)
7845 && vrp_val_is_max (*vr0max))
7847 *vr0type = vr1type;
7848 *vr0min = vr1min;
7849 *vr0max = vr1max;
7851 /* Else choose the range. */
7853 else if (*vr0type == VR_ANTI_RANGE
7854 && vr1type == VR_ANTI_RANGE)
7855 /* If both are anti-ranges the result is the outer one. */
7857 else if (*vr0type == VR_ANTI_RANGE
7858 && vr1type == VR_RANGE)
7860 /* The intersection is empty. */
7861 *vr0type = VR_UNDEFINED;
7862 *vr0min = NULL_TREE;
7863 *vr0max = NULL_TREE;
7865 else
7866 gcc_unreachable ();
7868 else if ((maxeq || operand_less_p (*vr0max, vr1max) == 1)
7869 && (mineq || operand_less_p (vr1min, *vr0min) == 1))
7871 /* ( [ ] ) or ([ ] ) or ( [ ]) */
7872 if (*vr0type == VR_RANGE
7873 && vr1type == VR_RANGE)
7874 /* Choose the inner range. */
7876 else if (*vr0type == VR_ANTI_RANGE
7877 && vr1type == VR_RANGE)
7879 /* Choose the right gap if the left is empty. */
7880 if (mineq)
7882 *vr0type = VR_RANGE;
7883 if (TREE_CODE (*vr0max) == INTEGER_CST)
7884 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
7885 integer_one_node);
7886 else
7887 *vr0min = *vr0max;
7888 *vr0max = vr1max;
7890 /* Choose the left gap if the right is empty. */
7891 else if (maxeq)
7893 *vr0type = VR_RANGE;
7894 if (TREE_CODE (*vr0min) == INTEGER_CST)
7895 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
7896 integer_one_node);
7897 else
7898 *vr0max = *vr0min;
7899 *vr0min = vr1min;
7901 /* Choose the anti-range if the range is effectively varying. */
7902 else if (vrp_val_is_min (vr1min)
7903 && vrp_val_is_max (vr1max))
7905 /* Else choose the range. */
7906 else
7908 *vr0type = vr1type;
7909 *vr0min = vr1min;
7910 *vr0max = vr1max;
7913 else if (*vr0type == VR_ANTI_RANGE
7914 && vr1type == VR_ANTI_RANGE)
7916 /* If both are anti-ranges the result is the outer one. */
7917 *vr0type = vr1type;
7918 *vr0min = vr1min;
7919 *vr0max = vr1max;
7921 else if (vr1type == VR_ANTI_RANGE
7922 && *vr0type == VR_RANGE)
7924 /* The intersection is empty. */
7925 *vr0type = VR_UNDEFINED;
7926 *vr0min = NULL_TREE;
7927 *vr0max = NULL_TREE;
7929 else
7930 gcc_unreachable ();
7932 else if ((operand_less_p (vr1min, *vr0max) == 1
7933 || operand_equal_p (vr1min, *vr0max, 0))
7934 && operand_less_p (*vr0min, vr1min) == 1)
7936 /* [ ( ] ) or [ ]( ) */
7937 if (*vr0type == VR_ANTI_RANGE
7938 && vr1type == VR_ANTI_RANGE)
7939 *vr0max = vr1max;
7940 else if (*vr0type == VR_RANGE
7941 && vr1type == VR_RANGE)
7942 *vr0min = vr1min;
7943 else if (*vr0type == VR_RANGE
7944 && vr1type == VR_ANTI_RANGE)
7946 if (TREE_CODE (vr1min) == INTEGER_CST)
7947 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
7948 integer_one_node);
7949 else
7950 *vr0max = vr1min;
7952 else if (*vr0type == VR_ANTI_RANGE
7953 && vr1type == VR_RANGE)
7955 *vr0type = VR_RANGE;
7956 if (TREE_CODE (*vr0max) == INTEGER_CST)
7957 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
7958 integer_one_node);
7959 else
7960 *vr0min = *vr0max;
7961 *vr0max = vr1max;
7963 else
7964 gcc_unreachable ();
7966 else if ((operand_less_p (*vr0min, vr1max) == 1
7967 || operand_equal_p (*vr0min, vr1max, 0))
7968 && operand_less_p (vr1min, *vr0min) == 1)
7970 /* ( [ ) ] or ( )[ ] */
7971 if (*vr0type == VR_ANTI_RANGE
7972 && vr1type == VR_ANTI_RANGE)
7973 *vr0min = vr1min;
7974 else if (*vr0type == VR_RANGE
7975 && vr1type == VR_RANGE)
7976 *vr0max = vr1max;
7977 else if (*vr0type == VR_RANGE
7978 && vr1type == VR_ANTI_RANGE)
7980 if (TREE_CODE (vr1max) == INTEGER_CST)
7981 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
7982 integer_one_node);
7983 else
7984 *vr0min = vr1max;
7986 else if (*vr0type == VR_ANTI_RANGE
7987 && vr1type == VR_RANGE)
7989 *vr0type = VR_RANGE;
7990 if (TREE_CODE (*vr0min) == INTEGER_CST)
7991 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
7992 integer_one_node);
7993 else
7994 *vr0max = *vr0min;
7995 *vr0min = vr1min;
7997 else
7998 gcc_unreachable ();
8001 /* As a fallback simply use { *VRTYPE, *VR0MIN, *VR0MAX } as
8002 result for the intersection. That's always a conservative
8003 correct estimate. */
8005 return;
8009 /* Intersect the two value-ranges *VR0 and *VR1 and store the result
8010 in *VR0. This may not be the smallest possible such range. */
8012 static void
8013 vrp_intersect_ranges_1 (value_range_t *vr0, value_range_t *vr1)
8015 value_range_t saved;
8017 /* If either range is VR_VARYING the other one wins. */
8018 if (vr1->type == VR_VARYING)
8019 return;
8020 if (vr0->type == VR_VARYING)
8022 copy_value_range (vr0, vr1);
8023 return;
8026 /* When either range is VR_UNDEFINED the resulting range is
8027 VR_UNDEFINED, too. */
8028 if (vr0->type == VR_UNDEFINED)
8029 return;
8030 if (vr1->type == VR_UNDEFINED)
8032 set_value_range_to_undefined (vr0);
8033 return;
8036 /* Save the original vr0 so we can return it as conservative intersection
8037 result when our worker turns things to varying. */
8038 saved = *vr0;
8039 intersect_ranges (&vr0->type, &vr0->min, &vr0->max,
8040 vr1->type, vr1->min, vr1->max);
8041 /* Make sure to canonicalize the result though as the inversion of a
8042 VR_RANGE can still be a VR_RANGE. */
8043 set_and_canonicalize_value_range (vr0, vr0->type,
8044 vr0->min, vr0->max, vr0->equiv);
8045 /* If that failed, use the saved original VR0. */
8046 if (vr0->type == VR_VARYING)
8048 *vr0 = saved;
8049 return;
8051 /* If the result is VR_UNDEFINED there is no need to mess with
8052 the equivalencies. */
8053 if (vr0->type == VR_UNDEFINED)
8054 return;
8056 /* The resulting set of equivalences for range intersection is the union of
8057 the two sets. */
8058 if (vr0->equiv && vr1->equiv && vr0->equiv != vr1->equiv)
8059 bitmap_ior_into (vr0->equiv, vr1->equiv);
8060 else if (vr1->equiv && !vr0->equiv)
8061 bitmap_copy (vr0->equiv, vr1->equiv);
8064 static void
8065 vrp_intersect_ranges (value_range_t *vr0, value_range_t *vr1)
8067 if (dump_file && (dump_flags & TDF_DETAILS))
8069 fprintf (dump_file, "Intersecting\n ");
8070 dump_value_range (dump_file, vr0);
8071 fprintf (dump_file, "\nand\n ");
8072 dump_value_range (dump_file, vr1);
8073 fprintf (dump_file, "\n");
8075 vrp_intersect_ranges_1 (vr0, vr1);
8076 if (dump_file && (dump_flags & TDF_DETAILS))
8078 fprintf (dump_file, "to\n ");
8079 dump_value_range (dump_file, vr0);
8080 fprintf (dump_file, "\n");
8084 /* Meet operation for value ranges. Given two value ranges VR0 and
8085 VR1, store in VR0 a range that contains both VR0 and VR1. This
8086 may not be the smallest possible such range. */
8088 static void
8089 vrp_meet_1 (value_range_t *vr0, value_range_t *vr1)
8091 value_range_t saved;
8093 if (vr0->type == VR_UNDEFINED)
8095 set_value_range (vr0, vr1->type, vr1->min, vr1->max, vr1->equiv);
8096 return;
8099 if (vr1->type == VR_UNDEFINED)
8101 /* VR0 already has the resulting range. */
8102 return;
8105 if (vr0->type == VR_VARYING)
8107 /* Nothing to do. VR0 already has the resulting range. */
8108 return;
8111 if (vr1->type == VR_VARYING)
8113 set_value_range_to_varying (vr0);
8114 return;
8117 saved = *vr0;
8118 union_ranges (&vr0->type, &vr0->min, &vr0->max,
8119 vr1->type, vr1->min, vr1->max);
8120 if (vr0->type == VR_VARYING)
8122 /* Failed to find an efficient meet. Before giving up and setting
8123 the result to VARYING, see if we can at least derive a useful
8124 anti-range. FIXME, all this nonsense about distinguishing
8125 anti-ranges from ranges is necessary because of the odd
8126 semantics of range_includes_zero_p and friends. */
8127 if (((saved.type == VR_RANGE
8128 && range_includes_zero_p (saved.min, saved.max) == 0)
8129 || (saved.type == VR_ANTI_RANGE
8130 && range_includes_zero_p (saved.min, saved.max) == 1))
8131 && ((vr1->type == VR_RANGE
8132 && range_includes_zero_p (vr1->min, vr1->max) == 0)
8133 || (vr1->type == VR_ANTI_RANGE
8134 && range_includes_zero_p (vr1->min, vr1->max) == 1)))
8136 set_value_range_to_nonnull (vr0, TREE_TYPE (saved.min));
8138 /* Since this meet operation did not result from the meeting of
8139 two equivalent names, VR0 cannot have any equivalences. */
8140 if (vr0->equiv)
8141 bitmap_clear (vr0->equiv);
8142 return;
8145 set_value_range_to_varying (vr0);
8146 return;
8148 set_and_canonicalize_value_range (vr0, vr0->type, vr0->min, vr0->max,
8149 vr0->equiv);
8150 if (vr0->type == VR_VARYING)
8151 return;
8153 /* The resulting set of equivalences is always the intersection of
8154 the two sets. */
8155 if (vr0->equiv && vr1->equiv && vr0->equiv != vr1->equiv)
8156 bitmap_and_into (vr0->equiv, vr1->equiv);
8157 else if (vr0->equiv && !vr1->equiv)
8158 bitmap_clear (vr0->equiv);
8161 static void
8162 vrp_meet (value_range_t *vr0, value_range_t *vr1)
8164 if (dump_file && (dump_flags & TDF_DETAILS))
8166 fprintf (dump_file, "Meeting\n ");
8167 dump_value_range (dump_file, vr0);
8168 fprintf (dump_file, "\nand\n ");
8169 dump_value_range (dump_file, vr1);
8170 fprintf (dump_file, "\n");
8172 vrp_meet_1 (vr0, vr1);
8173 if (dump_file && (dump_flags & TDF_DETAILS))
8175 fprintf (dump_file, "to\n ");
8176 dump_value_range (dump_file, vr0);
8177 fprintf (dump_file, "\n");
8182 /* Visit all arguments for PHI node PHI that flow through executable
8183 edges. If a valid value range can be derived from all the incoming
8184 value ranges, set a new range for the LHS of PHI. */
8186 static enum ssa_prop_result
8187 vrp_visit_phi_node (gimple phi)
8189 size_t i;
8190 tree lhs = PHI_RESULT (phi);
8191 value_range_t *lhs_vr = get_value_range (lhs);
8192 value_range_t vr_result = VR_INITIALIZER;
8193 bool first = true;
8194 int edges, old_edges;
8195 struct loop *l;
8197 if (dump_file && (dump_flags & TDF_DETAILS))
8199 fprintf (dump_file, "\nVisiting PHI node: ");
8200 print_gimple_stmt (dump_file, phi, 0, dump_flags);
8203 edges = 0;
8204 for (i = 0; i < gimple_phi_num_args (phi); i++)
8206 edge e = gimple_phi_arg_edge (phi, i);
8208 if (dump_file && (dump_flags & TDF_DETAILS))
8210 fprintf (dump_file,
8211 "\n Argument #%d (%d -> %d %sexecutable)\n",
8212 (int) i, e->src->index, e->dest->index,
8213 (e->flags & EDGE_EXECUTABLE) ? "" : "not ");
8216 if (e->flags & EDGE_EXECUTABLE)
8218 tree arg = PHI_ARG_DEF (phi, i);
8219 value_range_t vr_arg;
8221 ++edges;
8223 if (TREE_CODE (arg) == SSA_NAME)
8225 vr_arg = *(get_value_range (arg));
8226 /* Do not allow equivalences or symbolic ranges to leak in from
8227 backedges. That creates invalid equivalencies.
8228 See PR53465 and PR54767. */
8229 if (e->flags & EDGE_DFS_BACK
8230 && (vr_arg.type == VR_RANGE
8231 || vr_arg.type == VR_ANTI_RANGE))
8233 vr_arg.equiv = NULL;
8234 if (symbolic_range_p (&vr_arg))
8236 vr_arg.type = VR_VARYING;
8237 vr_arg.min = NULL_TREE;
8238 vr_arg.max = NULL_TREE;
8242 else
8244 if (is_overflow_infinity (arg))
8246 arg = copy_node (arg);
8247 TREE_OVERFLOW (arg) = 0;
8250 vr_arg.type = VR_RANGE;
8251 vr_arg.min = arg;
8252 vr_arg.max = arg;
8253 vr_arg.equiv = NULL;
8256 if (dump_file && (dump_flags & TDF_DETAILS))
8258 fprintf (dump_file, "\t");
8259 print_generic_expr (dump_file, arg, dump_flags);
8260 fprintf (dump_file, "\n\tValue: ");
8261 dump_value_range (dump_file, &vr_arg);
8262 fprintf (dump_file, "\n");
8265 if (first)
8266 copy_value_range (&vr_result, &vr_arg);
8267 else
8268 vrp_meet (&vr_result, &vr_arg);
8269 first = false;
8271 if (vr_result.type == VR_VARYING)
8272 break;
8276 if (vr_result.type == VR_VARYING)
8277 goto varying;
8278 else if (vr_result.type == VR_UNDEFINED)
8279 goto update_range;
8281 old_edges = vr_phi_edge_counts[SSA_NAME_VERSION (lhs)];
8282 vr_phi_edge_counts[SSA_NAME_VERSION (lhs)] = edges;
8284 /* To prevent infinite iterations in the algorithm, derive ranges
8285 when the new value is slightly bigger or smaller than the
8286 previous one. We don't do this if we have seen a new executable
8287 edge; this helps us avoid an overflow infinity for conditionals
8288 which are not in a loop. If the old value-range was VR_UNDEFINED
8289 use the updated range and iterate one more time. */
8290 if (edges > 0
8291 && gimple_phi_num_args (phi) > 1
8292 && edges == old_edges
8293 && lhs_vr->type != VR_UNDEFINED)
8295 int cmp_min = compare_values (lhs_vr->min, vr_result.min);
8296 int cmp_max = compare_values (lhs_vr->max, vr_result.max);
8298 /* For non VR_RANGE or for pointers fall back to varying if
8299 the range changed. */
8300 if ((lhs_vr->type != VR_RANGE || vr_result.type != VR_RANGE
8301 || POINTER_TYPE_P (TREE_TYPE (lhs)))
8302 && (cmp_min != 0 || cmp_max != 0))
8303 goto varying;
8305 /* If the new minimum is smaller or larger than the previous
8306 one, go all the way to -INF. In the first case, to avoid
8307 iterating millions of times to reach -INF, and in the
8308 other case to avoid infinite bouncing between different
8309 minimums. */
8310 if (cmp_min > 0 || cmp_min < 0)
8312 if (!needs_overflow_infinity (TREE_TYPE (vr_result.min))
8313 || !vrp_var_may_overflow (lhs, phi))
8314 vr_result.min = TYPE_MIN_VALUE (TREE_TYPE (vr_result.min));
8315 else if (supports_overflow_infinity (TREE_TYPE (vr_result.min)))
8316 vr_result.min =
8317 negative_overflow_infinity (TREE_TYPE (vr_result.min));
8320 /* Similarly, if the new maximum is smaller or larger than
8321 the previous one, go all the way to +INF. */
8322 if (cmp_max < 0 || cmp_max > 0)
8324 if (!needs_overflow_infinity (TREE_TYPE (vr_result.max))
8325 || !vrp_var_may_overflow (lhs, phi))
8326 vr_result.max = TYPE_MAX_VALUE (TREE_TYPE (vr_result.max));
8327 else if (supports_overflow_infinity (TREE_TYPE (vr_result.max)))
8328 vr_result.max =
8329 positive_overflow_infinity (TREE_TYPE (vr_result.max));
8332 /* If we dropped either bound to +-INF then if this is a loop
8333 PHI node SCEV may known more about its value-range. */
8334 if ((cmp_min > 0 || cmp_min < 0
8335 || cmp_max < 0 || cmp_max > 0)
8336 && current_loops
8337 && (l = loop_containing_stmt (phi))
8338 && l->header == gimple_bb (phi))
8339 adjust_range_with_scev (&vr_result, l, phi, lhs);
8341 /* If we will end up with a (-INF, +INF) range, set it to
8342 VARYING. Same if the previous max value was invalid for
8343 the type and we end up with vr_result.min > vr_result.max. */
8344 if ((vrp_val_is_max (vr_result.max)
8345 && vrp_val_is_min (vr_result.min))
8346 || compare_values (vr_result.min,
8347 vr_result.max) > 0)
8348 goto varying;
8351 /* If the new range is different than the previous value, keep
8352 iterating. */
8353 update_range:
8354 if (update_value_range (lhs, &vr_result))
8356 if (dump_file && (dump_flags & TDF_DETAILS))
8358 fprintf (dump_file, "Found new range for ");
8359 print_generic_expr (dump_file, lhs, 0);
8360 fprintf (dump_file, ": ");
8361 dump_value_range (dump_file, &vr_result);
8362 fprintf (dump_file, "\n\n");
8365 return SSA_PROP_INTERESTING;
8368 /* Nothing changed, don't add outgoing edges. */
8369 return SSA_PROP_NOT_INTERESTING;
8371 /* No match found. Set the LHS to VARYING. */
8372 varying:
8373 set_value_range_to_varying (lhs_vr);
8374 return SSA_PROP_VARYING;
8377 /* Simplify boolean operations if the source is known
8378 to be already a boolean. */
8379 static bool
8380 simplify_truth_ops_using_ranges (gimple_stmt_iterator *gsi, gimple stmt)
8382 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
8383 tree lhs, op0, op1;
8384 bool need_conversion;
8386 /* We handle only !=/== case here. */
8387 gcc_assert (rhs_code == EQ_EXPR || rhs_code == NE_EXPR);
8389 op0 = gimple_assign_rhs1 (stmt);
8390 if (!op_with_boolean_value_range_p (op0))
8391 return false;
8393 op1 = gimple_assign_rhs2 (stmt);
8394 if (!op_with_boolean_value_range_p (op1))
8395 return false;
8397 /* Reduce number of cases to handle to NE_EXPR. As there is no
8398 BIT_XNOR_EXPR we cannot replace A == B with a single statement. */
8399 if (rhs_code == EQ_EXPR)
8401 if (TREE_CODE (op1) == INTEGER_CST)
8402 op1 = int_const_binop (BIT_XOR_EXPR, op1, integer_one_node);
8403 else
8404 return false;
8407 lhs = gimple_assign_lhs (stmt);
8408 need_conversion
8409 = !useless_type_conversion_p (TREE_TYPE (lhs), TREE_TYPE (op0));
8411 /* Make sure to not sign-extend a 1-bit 1 when converting the result. */
8412 if (need_conversion
8413 && !TYPE_UNSIGNED (TREE_TYPE (op0))
8414 && TYPE_PRECISION (TREE_TYPE (op0)) == 1
8415 && TYPE_PRECISION (TREE_TYPE (lhs)) > 1)
8416 return false;
8418 /* For A != 0 we can substitute A itself. */
8419 if (integer_zerop (op1))
8420 gimple_assign_set_rhs_with_ops (gsi,
8421 need_conversion
8422 ? NOP_EXPR : TREE_CODE (op0),
8423 op0, NULL_TREE);
8424 /* For A != B we substitute A ^ B. Either with conversion. */
8425 else if (need_conversion)
8427 tree tem = make_ssa_name (TREE_TYPE (op0), NULL);
8428 gimple newop = gimple_build_assign_with_ops (BIT_XOR_EXPR, tem, op0, op1);
8429 gsi_insert_before (gsi, newop, GSI_SAME_STMT);
8430 gimple_assign_set_rhs_with_ops (gsi, NOP_EXPR, tem, NULL_TREE);
8432 /* Or without. */
8433 else
8434 gimple_assign_set_rhs_with_ops (gsi, BIT_XOR_EXPR, op0, op1);
8435 update_stmt (gsi_stmt (*gsi));
8437 return true;
8440 /* Simplify a division or modulo operator to a right shift or
8441 bitwise and if the first operand is unsigned or is greater
8442 than zero and the second operand is an exact power of two. */
8444 static bool
8445 simplify_div_or_mod_using_ranges (gimple stmt)
8447 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
8448 tree val = NULL;
8449 tree op0 = gimple_assign_rhs1 (stmt);
8450 tree op1 = gimple_assign_rhs2 (stmt);
8451 value_range_t *vr = get_value_range (gimple_assign_rhs1 (stmt));
8453 if (TYPE_UNSIGNED (TREE_TYPE (op0)))
8455 val = integer_one_node;
8457 else
8459 bool sop = false;
8461 val = compare_range_with_value (GE_EXPR, vr, integer_zero_node, &sop);
8463 if (val
8464 && sop
8465 && integer_onep (val)
8466 && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC))
8468 location_t location;
8470 if (!gimple_has_location (stmt))
8471 location = input_location;
8472 else
8473 location = gimple_location (stmt);
8474 warning_at (location, OPT_Wstrict_overflow,
8475 "assuming signed overflow does not occur when "
8476 "simplifying %</%> or %<%%%> to %<>>%> or %<&%>");
8480 if (val && integer_onep (val))
8482 tree t;
8484 if (rhs_code == TRUNC_DIV_EXPR)
8486 t = build_int_cst (integer_type_node, tree_log2 (op1));
8487 gimple_assign_set_rhs_code (stmt, RSHIFT_EXPR);
8488 gimple_assign_set_rhs1 (stmt, op0);
8489 gimple_assign_set_rhs2 (stmt, t);
8491 else
8493 t = build_int_cst (TREE_TYPE (op1), 1);
8494 t = int_const_binop (MINUS_EXPR, op1, t);
8495 t = fold_convert (TREE_TYPE (op0), t);
8497 gimple_assign_set_rhs_code (stmt, BIT_AND_EXPR);
8498 gimple_assign_set_rhs1 (stmt, op0);
8499 gimple_assign_set_rhs2 (stmt, t);
8502 update_stmt (stmt);
8503 return true;
8506 return false;
8509 /* If the operand to an ABS_EXPR is >= 0, then eliminate the
8510 ABS_EXPR. If the operand is <= 0, then simplify the
8511 ABS_EXPR into a NEGATE_EXPR. */
8513 static bool
8514 simplify_abs_using_ranges (gimple stmt)
8516 tree val = NULL;
8517 tree op = gimple_assign_rhs1 (stmt);
8518 tree type = TREE_TYPE (op);
8519 value_range_t *vr = get_value_range (op);
8521 if (TYPE_UNSIGNED (type))
8523 val = integer_zero_node;
8525 else if (vr)
8527 bool sop = false;
8529 val = compare_range_with_value (LE_EXPR, vr, integer_zero_node, &sop);
8530 if (!val)
8532 sop = false;
8533 val = compare_range_with_value (GE_EXPR, vr, integer_zero_node,
8534 &sop);
8536 if (val)
8538 if (integer_zerop (val))
8539 val = integer_one_node;
8540 else if (integer_onep (val))
8541 val = integer_zero_node;
8545 if (val
8546 && (integer_onep (val) || integer_zerop (val)))
8548 if (sop && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC))
8550 location_t location;
8552 if (!gimple_has_location (stmt))
8553 location = input_location;
8554 else
8555 location = gimple_location (stmt);
8556 warning_at (location, OPT_Wstrict_overflow,
8557 "assuming signed overflow does not occur when "
8558 "simplifying %<abs (X)%> to %<X%> or %<-X%>");
8561 gimple_assign_set_rhs1 (stmt, op);
8562 if (integer_onep (val))
8563 gimple_assign_set_rhs_code (stmt, NEGATE_EXPR);
8564 else
8565 gimple_assign_set_rhs_code (stmt, SSA_NAME);
8566 update_stmt (stmt);
8567 return true;
8571 return false;
8574 /* Optimize away redundant BIT_AND_EXPR and BIT_IOR_EXPR.
8575 If all the bits that are being cleared by & are already
8576 known to be zero from VR, or all the bits that are being
8577 set by | are already known to be one from VR, the bit
8578 operation is redundant. */
8580 static bool
8581 simplify_bit_ops_using_ranges (gimple_stmt_iterator *gsi, gimple stmt)
8583 tree op0 = gimple_assign_rhs1 (stmt);
8584 tree op1 = gimple_assign_rhs2 (stmt);
8585 tree op = NULL_TREE;
8586 value_range_t vr0 = VR_INITIALIZER;
8587 value_range_t vr1 = VR_INITIALIZER;
8588 double_int may_be_nonzero0, may_be_nonzero1;
8589 double_int must_be_nonzero0, must_be_nonzero1;
8590 double_int mask;
8592 if (TREE_CODE (op0) == SSA_NAME)
8593 vr0 = *(get_value_range (op0));
8594 else if (is_gimple_min_invariant (op0))
8595 set_value_range_to_value (&vr0, op0, NULL);
8596 else
8597 return false;
8599 if (TREE_CODE (op1) == SSA_NAME)
8600 vr1 = *(get_value_range (op1));
8601 else if (is_gimple_min_invariant (op1))
8602 set_value_range_to_value (&vr1, op1, NULL);
8603 else
8604 return false;
8606 if (!zero_nonzero_bits_from_vr (&vr0, &may_be_nonzero0, &must_be_nonzero0))
8607 return false;
8608 if (!zero_nonzero_bits_from_vr (&vr1, &may_be_nonzero1, &must_be_nonzero1))
8609 return false;
8611 switch (gimple_assign_rhs_code (stmt))
8613 case BIT_AND_EXPR:
8614 mask = may_be_nonzero0.and_not (must_be_nonzero1);
8615 if (mask.is_zero ())
8617 op = op0;
8618 break;
8620 mask = may_be_nonzero1.and_not (must_be_nonzero0);
8621 if (mask.is_zero ())
8623 op = op1;
8624 break;
8626 break;
8627 case BIT_IOR_EXPR:
8628 mask = may_be_nonzero0.and_not (must_be_nonzero1);
8629 if (mask.is_zero ())
8631 op = op1;
8632 break;
8634 mask = may_be_nonzero1.and_not (must_be_nonzero0);
8635 if (mask.is_zero ())
8637 op = op0;
8638 break;
8640 break;
8641 default:
8642 gcc_unreachable ();
8645 if (op == NULL_TREE)
8646 return false;
8648 gimple_assign_set_rhs_with_ops (gsi, TREE_CODE (op), op, NULL);
8649 update_stmt (gsi_stmt (*gsi));
8650 return true;
8653 /* We are comparing trees OP0 and OP1 using COND_CODE. OP0 has
8654 a known value range VR.
8656 If there is one and only one value which will satisfy the
8657 conditional, then return that value. Else return NULL. */
8659 static tree
8660 test_for_singularity (enum tree_code cond_code, tree op0,
8661 tree op1, value_range_t *vr)
8663 tree min = NULL;
8664 tree max = NULL;
8666 /* Extract minimum/maximum values which satisfy the
8667 the conditional as it was written. */
8668 if (cond_code == LE_EXPR || cond_code == LT_EXPR)
8670 /* This should not be negative infinity; there is no overflow
8671 here. */
8672 min = TYPE_MIN_VALUE (TREE_TYPE (op0));
8674 max = op1;
8675 if (cond_code == LT_EXPR && !is_overflow_infinity (max))
8677 tree one = build_int_cst (TREE_TYPE (op0), 1);
8678 max = fold_build2 (MINUS_EXPR, TREE_TYPE (op0), max, one);
8679 if (EXPR_P (max))
8680 TREE_NO_WARNING (max) = 1;
8683 else if (cond_code == GE_EXPR || cond_code == GT_EXPR)
8685 /* This should not be positive infinity; there is no overflow
8686 here. */
8687 max = TYPE_MAX_VALUE (TREE_TYPE (op0));
8689 min = op1;
8690 if (cond_code == GT_EXPR && !is_overflow_infinity (min))
8692 tree one = build_int_cst (TREE_TYPE (op0), 1);
8693 min = fold_build2 (PLUS_EXPR, TREE_TYPE (op0), min, one);
8694 if (EXPR_P (min))
8695 TREE_NO_WARNING (min) = 1;
8699 /* Now refine the minimum and maximum values using any
8700 value range information we have for op0. */
8701 if (min && max)
8703 if (compare_values (vr->min, min) == 1)
8704 min = vr->min;
8705 if (compare_values (vr->max, max) == -1)
8706 max = vr->max;
8708 /* If the new min/max values have converged to a single value,
8709 then there is only one value which can satisfy the condition,
8710 return that value. */
8711 if (operand_equal_p (min, max, 0) && is_gimple_min_invariant (min))
8712 return min;
8714 return NULL;
8717 /* Return whether the value range *VR fits in an integer type specified
8718 by PRECISION and UNSIGNED_P. */
8720 static bool
8721 range_fits_type_p (value_range_t *vr, unsigned precision, bool unsigned_p)
8723 tree src_type;
8724 unsigned src_precision;
8725 double_int tem;
8727 /* We can only handle integral and pointer types. */
8728 src_type = TREE_TYPE (vr->min);
8729 if (!INTEGRAL_TYPE_P (src_type)
8730 && !POINTER_TYPE_P (src_type))
8731 return false;
8733 /* An extension is fine unless VR is signed and unsigned_p,
8734 and so is an identity transform. */
8735 src_precision = TYPE_PRECISION (TREE_TYPE (vr->min));
8736 if ((src_precision < precision
8737 && !(unsigned_p && !TYPE_UNSIGNED (src_type)))
8738 || (src_precision == precision
8739 && TYPE_UNSIGNED (src_type) == unsigned_p))
8740 return true;
8742 /* Now we can only handle ranges with constant bounds. */
8743 if (vr->type != VR_RANGE
8744 || TREE_CODE (vr->min) != INTEGER_CST
8745 || TREE_CODE (vr->max) != INTEGER_CST)
8746 return false;
8748 /* For sign changes, the MSB of the double_int has to be clear.
8749 An unsigned value with its MSB set cannot be represented by
8750 a signed double_int, while a negative value cannot be represented
8751 by an unsigned double_int. */
8752 if (TYPE_UNSIGNED (src_type) != unsigned_p
8753 && (TREE_INT_CST_HIGH (vr->min) | TREE_INT_CST_HIGH (vr->max)) < 0)
8754 return false;
8756 /* Then we can perform the conversion on both ends and compare
8757 the result for equality. */
8758 tem = tree_to_double_int (vr->min).ext (precision, unsigned_p);
8759 if (tree_to_double_int (vr->min) != tem)
8760 return false;
8761 tem = tree_to_double_int (vr->max).ext (precision, unsigned_p);
8762 if (tree_to_double_int (vr->max) != tem)
8763 return false;
8765 return true;
8768 /* Simplify a conditional using a relational operator to an equality
8769 test if the range information indicates only one value can satisfy
8770 the original conditional. */
8772 static bool
8773 simplify_cond_using_ranges (gimple stmt)
8775 tree op0 = gimple_cond_lhs (stmt);
8776 tree op1 = gimple_cond_rhs (stmt);
8777 enum tree_code cond_code = gimple_cond_code (stmt);
8779 if (cond_code != NE_EXPR
8780 && cond_code != EQ_EXPR
8781 && TREE_CODE (op0) == SSA_NAME
8782 && INTEGRAL_TYPE_P (TREE_TYPE (op0))
8783 && is_gimple_min_invariant (op1))
8785 value_range_t *vr = get_value_range (op0);
8787 /* If we have range information for OP0, then we might be
8788 able to simplify this conditional. */
8789 if (vr->type == VR_RANGE)
8791 tree new_tree = test_for_singularity (cond_code, op0, op1, vr);
8793 if (new_tree)
8795 if (dump_file)
8797 fprintf (dump_file, "Simplified relational ");
8798 print_gimple_stmt (dump_file, stmt, 0, 0);
8799 fprintf (dump_file, " into ");
8802 gimple_cond_set_code (stmt, EQ_EXPR);
8803 gimple_cond_set_lhs (stmt, op0);
8804 gimple_cond_set_rhs (stmt, new_tree);
8806 update_stmt (stmt);
8808 if (dump_file)
8810 print_gimple_stmt (dump_file, stmt, 0, 0);
8811 fprintf (dump_file, "\n");
8814 return true;
8817 /* Try again after inverting the condition. We only deal
8818 with integral types here, so no need to worry about
8819 issues with inverting FP comparisons. */
8820 cond_code = invert_tree_comparison (cond_code, false);
8821 new_tree = test_for_singularity (cond_code, op0, op1, vr);
8823 if (new_tree)
8825 if (dump_file)
8827 fprintf (dump_file, "Simplified relational ");
8828 print_gimple_stmt (dump_file, stmt, 0, 0);
8829 fprintf (dump_file, " into ");
8832 gimple_cond_set_code (stmt, NE_EXPR);
8833 gimple_cond_set_lhs (stmt, op0);
8834 gimple_cond_set_rhs (stmt, new_tree);
8836 update_stmt (stmt);
8838 if (dump_file)
8840 print_gimple_stmt (dump_file, stmt, 0, 0);
8841 fprintf (dump_file, "\n");
8844 return true;
8849 /* If we have a comparison of an SSA_NAME (OP0) against a constant,
8850 see if OP0 was set by a type conversion where the source of
8851 the conversion is another SSA_NAME with a range that fits
8852 into the range of OP0's type.
8854 If so, the conversion is redundant as the earlier SSA_NAME can be
8855 used for the comparison directly if we just massage the constant in the
8856 comparison. */
8857 if (TREE_CODE (op0) == SSA_NAME
8858 && TREE_CODE (op1) == INTEGER_CST)
8860 gimple def_stmt = SSA_NAME_DEF_STMT (op0);
8861 tree innerop;
8863 if (!is_gimple_assign (def_stmt)
8864 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt)))
8865 return false;
8867 innerop = gimple_assign_rhs1 (def_stmt);
8869 if (TREE_CODE (innerop) == SSA_NAME
8870 && !POINTER_TYPE_P (TREE_TYPE (innerop)))
8872 value_range_t *vr = get_value_range (innerop);
8874 if (range_int_cst_p (vr)
8875 && range_fits_type_p (vr,
8876 TYPE_PRECISION (TREE_TYPE (op0)),
8877 TYPE_UNSIGNED (TREE_TYPE (op0)))
8878 && int_fits_type_p (op1, TREE_TYPE (innerop))
8879 /* The range must not have overflowed, or if it did overflow
8880 we must not be wrapping/trapping overflow and optimizing
8881 with strict overflow semantics. */
8882 && ((!is_negative_overflow_infinity (vr->min)
8883 && !is_positive_overflow_infinity (vr->max))
8884 || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (innerop))))
8886 /* If the range overflowed and the user has asked for warnings
8887 when strict overflow semantics were used to optimize code,
8888 issue an appropriate warning. */
8889 if ((is_negative_overflow_infinity (vr->min)
8890 || is_positive_overflow_infinity (vr->max))
8891 && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_CONDITIONAL))
8893 location_t location;
8895 if (!gimple_has_location (stmt))
8896 location = input_location;
8897 else
8898 location = gimple_location (stmt);
8899 warning_at (location, OPT_Wstrict_overflow,
8900 "assuming signed overflow does not occur when "
8901 "simplifying conditional");
8904 tree newconst = fold_convert (TREE_TYPE (innerop), op1);
8905 gimple_cond_set_lhs (stmt, innerop);
8906 gimple_cond_set_rhs (stmt, newconst);
8907 return true;
8912 return false;
8915 /* Simplify a switch statement using the value range of the switch
8916 argument. */
8918 static bool
8919 simplify_switch_using_ranges (gimple stmt)
8921 tree op = gimple_switch_index (stmt);
8922 value_range_t *vr;
8923 bool take_default;
8924 edge e;
8925 edge_iterator ei;
8926 size_t i = 0, j = 0, n, n2;
8927 tree vec2;
8928 switch_update su;
8929 size_t k = 1, l = 0;
8931 if (TREE_CODE (op) == SSA_NAME)
8933 vr = get_value_range (op);
8935 /* We can only handle integer ranges. */
8936 if ((vr->type != VR_RANGE
8937 && vr->type != VR_ANTI_RANGE)
8938 || symbolic_range_p (vr))
8939 return false;
8941 /* Find case label for min/max of the value range. */
8942 take_default = !find_case_label_ranges (stmt, vr, &i, &j, &k, &l);
8944 else if (TREE_CODE (op) == INTEGER_CST)
8946 take_default = !find_case_label_index (stmt, 1, op, &i);
8947 if (take_default)
8949 i = 1;
8950 j = 0;
8952 else
8954 j = i;
8957 else
8958 return false;
8960 n = gimple_switch_num_labels (stmt);
8962 /* Bail out if this is just all edges taken. */
8963 if (i == 1
8964 && j == n - 1
8965 && take_default)
8966 return false;
8968 /* Build a new vector of taken case labels. */
8969 vec2 = make_tree_vec (j - i + 1 + l - k + 1 + (int)take_default);
8970 n2 = 0;
8972 /* Add the default edge, if necessary. */
8973 if (take_default)
8974 TREE_VEC_ELT (vec2, n2++) = gimple_switch_default_label (stmt);
8976 for (; i <= j; ++i, ++n2)
8977 TREE_VEC_ELT (vec2, n2) = gimple_switch_label (stmt, i);
8979 for (; k <= l; ++k, ++n2)
8980 TREE_VEC_ELT (vec2, n2) = gimple_switch_label (stmt, k);
8982 /* Mark needed edges. */
8983 for (i = 0; i < n2; ++i)
8985 e = find_edge (gimple_bb (stmt),
8986 label_to_block (CASE_LABEL (TREE_VEC_ELT (vec2, i))));
8987 e->aux = (void *)-1;
8990 /* Queue not needed edges for later removal. */
8991 FOR_EACH_EDGE (e, ei, gimple_bb (stmt)->succs)
8993 if (e->aux == (void *)-1)
8995 e->aux = NULL;
8996 continue;
8999 if (dump_file && (dump_flags & TDF_DETAILS))
9001 fprintf (dump_file, "removing unreachable case label\n");
9003 to_remove_edges.safe_push (e);
9004 e->flags &= ~EDGE_EXECUTABLE;
9007 /* And queue an update for the stmt. */
9008 su.stmt = stmt;
9009 su.vec = vec2;
9010 to_update_switch_stmts.safe_push (su);
9011 return false;
9014 /* Simplify an integral conversion from an SSA name in STMT. */
9016 static bool
9017 simplify_conversion_using_ranges (gimple stmt)
9019 tree innerop, middleop, finaltype;
9020 gimple def_stmt;
9021 value_range_t *innervr;
9022 bool inner_unsigned_p, middle_unsigned_p, final_unsigned_p;
9023 unsigned inner_prec, middle_prec, final_prec;
9024 double_int innermin, innermed, innermax, middlemin, middlemed, middlemax;
9026 finaltype = TREE_TYPE (gimple_assign_lhs (stmt));
9027 if (!INTEGRAL_TYPE_P (finaltype))
9028 return false;
9029 middleop = gimple_assign_rhs1 (stmt);
9030 def_stmt = SSA_NAME_DEF_STMT (middleop);
9031 if (!is_gimple_assign (def_stmt)
9032 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt)))
9033 return false;
9034 innerop = gimple_assign_rhs1 (def_stmt);
9035 if (TREE_CODE (innerop) != SSA_NAME
9036 || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (innerop))
9037 return false;
9039 /* Get the value-range of the inner operand. */
9040 innervr = get_value_range (innerop);
9041 if (innervr->type != VR_RANGE
9042 || TREE_CODE (innervr->min) != INTEGER_CST
9043 || TREE_CODE (innervr->max) != INTEGER_CST)
9044 return false;
9046 /* Simulate the conversion chain to check if the result is equal if
9047 the middle conversion is removed. */
9048 innermin = tree_to_double_int (innervr->min);
9049 innermax = tree_to_double_int (innervr->max);
9051 inner_prec = TYPE_PRECISION (TREE_TYPE (innerop));
9052 middle_prec = TYPE_PRECISION (TREE_TYPE (middleop));
9053 final_prec = TYPE_PRECISION (finaltype);
9055 /* If the first conversion is not injective, the second must not
9056 be widening. */
9057 if ((innermax - innermin).ugt (double_int::mask (middle_prec))
9058 && middle_prec < final_prec)
9059 return false;
9060 /* We also want a medium value so that we can track the effect that
9061 narrowing conversions with sign change have. */
9062 inner_unsigned_p = TYPE_UNSIGNED (TREE_TYPE (innerop));
9063 if (inner_unsigned_p)
9064 innermed = double_int::mask (inner_prec).lrshift (1, inner_prec);
9065 else
9066 innermed = double_int_zero;
9067 if (innermin.cmp (innermed, inner_unsigned_p) >= 0
9068 || innermed.cmp (innermax, inner_unsigned_p) >= 0)
9069 innermed = innermin;
9071 middle_unsigned_p = TYPE_UNSIGNED (TREE_TYPE (middleop));
9072 middlemin = innermin.ext (middle_prec, middle_unsigned_p);
9073 middlemed = innermed.ext (middle_prec, middle_unsigned_p);
9074 middlemax = innermax.ext (middle_prec, middle_unsigned_p);
9076 /* Require that the final conversion applied to both the original
9077 and the intermediate range produces the same result. */
9078 final_unsigned_p = TYPE_UNSIGNED (finaltype);
9079 if (middlemin.ext (final_prec, final_unsigned_p)
9080 != innermin.ext (final_prec, final_unsigned_p)
9081 || middlemed.ext (final_prec, final_unsigned_p)
9082 != innermed.ext (final_prec, final_unsigned_p)
9083 || middlemax.ext (final_prec, final_unsigned_p)
9084 != innermax.ext (final_prec, final_unsigned_p))
9085 return false;
9087 gimple_assign_set_rhs1 (stmt, innerop);
9088 update_stmt (stmt);
9089 return true;
9092 /* Simplify a conversion from integral SSA name to float in STMT. */
9094 static bool
9095 simplify_float_conversion_using_ranges (gimple_stmt_iterator *gsi, gimple stmt)
9097 tree rhs1 = gimple_assign_rhs1 (stmt);
9098 value_range_t *vr = get_value_range (rhs1);
9099 enum machine_mode fltmode = TYPE_MODE (TREE_TYPE (gimple_assign_lhs (stmt)));
9100 enum machine_mode mode;
9101 tree tem;
9102 gimple conv;
9104 /* We can only handle constant ranges. */
9105 if (vr->type != VR_RANGE
9106 || TREE_CODE (vr->min) != INTEGER_CST
9107 || TREE_CODE (vr->max) != INTEGER_CST)
9108 return false;
9110 /* First check if we can use a signed type in place of an unsigned. */
9111 if (TYPE_UNSIGNED (TREE_TYPE (rhs1))
9112 && (can_float_p (fltmode, TYPE_MODE (TREE_TYPE (rhs1)), 0)
9113 != CODE_FOR_nothing)
9114 && range_fits_type_p (vr, GET_MODE_PRECISION
9115 (TYPE_MODE (TREE_TYPE (rhs1))), 0))
9116 mode = TYPE_MODE (TREE_TYPE (rhs1));
9117 /* If we can do the conversion in the current input mode do nothing. */
9118 else if (can_float_p (fltmode, TYPE_MODE (TREE_TYPE (rhs1)),
9119 TYPE_UNSIGNED (TREE_TYPE (rhs1))) != CODE_FOR_nothing)
9120 return false;
9121 /* Otherwise search for a mode we can use, starting from the narrowest
9122 integer mode available. */
9123 else
9125 mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
9128 /* If we cannot do a signed conversion to float from mode
9129 or if the value-range does not fit in the signed type
9130 try with a wider mode. */
9131 if (can_float_p (fltmode, mode, 0) != CODE_FOR_nothing
9132 && range_fits_type_p (vr, GET_MODE_PRECISION (mode), 0))
9133 break;
9135 mode = GET_MODE_WIDER_MODE (mode);
9136 /* But do not widen the input. Instead leave that to the
9137 optabs expansion code. */
9138 if (GET_MODE_PRECISION (mode) > TYPE_PRECISION (TREE_TYPE (rhs1)))
9139 return false;
9141 while (mode != VOIDmode);
9142 if (mode == VOIDmode)
9143 return false;
9146 /* It works, insert a truncation or sign-change before the
9147 float conversion. */
9148 tem = make_ssa_name (build_nonstandard_integer_type
9149 (GET_MODE_PRECISION (mode), 0), NULL);
9150 conv = gimple_build_assign_with_ops (NOP_EXPR, tem, rhs1, NULL_TREE);
9151 gsi_insert_before (gsi, conv, GSI_SAME_STMT);
9152 gimple_assign_set_rhs1 (stmt, tem);
9153 update_stmt (stmt);
9155 return true;
9158 /* Simplify STMT using ranges if possible. */
9160 static bool
9161 simplify_stmt_using_ranges (gimple_stmt_iterator *gsi)
9163 gimple stmt = gsi_stmt (*gsi);
9164 if (is_gimple_assign (stmt))
9166 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
9167 tree rhs1 = gimple_assign_rhs1 (stmt);
9169 switch (rhs_code)
9171 case EQ_EXPR:
9172 case NE_EXPR:
9173 /* Transform EQ_EXPR, NE_EXPR into BIT_XOR_EXPR or identity
9174 if the RHS is zero or one, and the LHS are known to be boolean
9175 values. */
9176 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
9177 return simplify_truth_ops_using_ranges (gsi, stmt);
9178 break;
9180 /* Transform TRUNC_DIV_EXPR and TRUNC_MOD_EXPR into RSHIFT_EXPR
9181 and BIT_AND_EXPR respectively if the first operand is greater
9182 than zero and the second operand is an exact power of two. */
9183 case TRUNC_DIV_EXPR:
9184 case TRUNC_MOD_EXPR:
9185 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1))
9186 && integer_pow2p (gimple_assign_rhs2 (stmt)))
9187 return simplify_div_or_mod_using_ranges (stmt);
9188 break;
9190 /* Transform ABS (X) into X or -X as appropriate. */
9191 case ABS_EXPR:
9192 if (TREE_CODE (rhs1) == SSA_NAME
9193 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
9194 return simplify_abs_using_ranges (stmt);
9195 break;
9197 case BIT_AND_EXPR:
9198 case BIT_IOR_EXPR:
9199 /* Optimize away BIT_AND_EXPR and BIT_IOR_EXPR
9200 if all the bits being cleared are already cleared or
9201 all the bits being set are already set. */
9202 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
9203 return simplify_bit_ops_using_ranges (gsi, stmt);
9204 break;
9206 CASE_CONVERT:
9207 if (TREE_CODE (rhs1) == SSA_NAME
9208 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
9209 return simplify_conversion_using_ranges (stmt);
9210 break;
9212 case FLOAT_EXPR:
9213 if (TREE_CODE (rhs1) == SSA_NAME
9214 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
9215 return simplify_float_conversion_using_ranges (gsi, stmt);
9216 break;
9218 default:
9219 break;
9222 else if (gimple_code (stmt) == GIMPLE_COND)
9223 return simplify_cond_using_ranges (stmt);
9224 else if (gimple_code (stmt) == GIMPLE_SWITCH)
9225 return simplify_switch_using_ranges (stmt);
9227 return false;
9230 /* If the statement pointed by SI has a predicate whose value can be
9231 computed using the value range information computed by VRP, compute
9232 its value and return true. Otherwise, return false. */
9234 static bool
9235 fold_predicate_in (gimple_stmt_iterator *si)
9237 bool assignment_p = false;
9238 tree val;
9239 gimple stmt = gsi_stmt (*si);
9241 if (is_gimple_assign (stmt)
9242 && TREE_CODE_CLASS (gimple_assign_rhs_code (stmt)) == tcc_comparison)
9244 assignment_p = true;
9245 val = vrp_evaluate_conditional (gimple_assign_rhs_code (stmt),
9246 gimple_assign_rhs1 (stmt),
9247 gimple_assign_rhs2 (stmt),
9248 stmt);
9250 else if (gimple_code (stmt) == GIMPLE_COND)
9251 val = vrp_evaluate_conditional (gimple_cond_code (stmt),
9252 gimple_cond_lhs (stmt),
9253 gimple_cond_rhs (stmt),
9254 stmt);
9255 else
9256 return false;
9258 if (val)
9260 if (assignment_p)
9261 val = fold_convert (gimple_expr_type (stmt), val);
9263 if (dump_file)
9265 fprintf (dump_file, "Folding predicate ");
9266 print_gimple_expr (dump_file, stmt, 0, 0);
9267 fprintf (dump_file, " to ");
9268 print_generic_expr (dump_file, val, 0);
9269 fprintf (dump_file, "\n");
9272 if (is_gimple_assign (stmt))
9273 gimple_assign_set_rhs_from_tree (si, val);
9274 else
9276 gcc_assert (gimple_code (stmt) == GIMPLE_COND);
9277 if (integer_zerop (val))
9278 gimple_cond_make_false (stmt);
9279 else if (integer_onep (val))
9280 gimple_cond_make_true (stmt);
9281 else
9282 gcc_unreachable ();
9285 return true;
9288 return false;
9291 /* Callback for substitute_and_fold folding the stmt at *SI. */
9293 static bool
9294 vrp_fold_stmt (gimple_stmt_iterator *si)
9296 if (fold_predicate_in (si))
9297 return true;
9299 return simplify_stmt_using_ranges (si);
9302 /* Stack of dest,src equivalency pairs that need to be restored after
9303 each attempt to thread a block's incoming edge to an outgoing edge.
9305 A NULL entry is used to mark the end of pairs which need to be
9306 restored. */
9307 static vec<tree> equiv_stack;
9309 /* A trivial wrapper so that we can present the generic jump threading
9310 code with a simple API for simplifying statements. STMT is the
9311 statement we want to simplify, WITHIN_STMT provides the location
9312 for any overflow warnings. */
9314 static tree
9315 simplify_stmt_for_jump_threading (gimple stmt, gimple within_stmt)
9317 if (gimple_code (stmt) == GIMPLE_COND)
9318 return vrp_evaluate_conditional (gimple_cond_code (stmt),
9319 gimple_cond_lhs (stmt),
9320 gimple_cond_rhs (stmt), within_stmt);
9322 if (gimple_code (stmt) == GIMPLE_ASSIGN)
9324 value_range_t new_vr = VR_INITIALIZER;
9325 tree lhs = gimple_assign_lhs (stmt);
9327 if (TREE_CODE (lhs) == SSA_NAME
9328 && (INTEGRAL_TYPE_P (TREE_TYPE (lhs))
9329 || POINTER_TYPE_P (TREE_TYPE (lhs))))
9331 extract_range_from_assignment (&new_vr, stmt);
9332 if (range_int_cst_singleton_p (&new_vr))
9333 return new_vr.min;
9337 return NULL_TREE;
9340 /* Blocks which have more than one predecessor and more than
9341 one successor present jump threading opportunities, i.e.,
9342 when the block is reached from a specific predecessor, we
9343 may be able to determine which of the outgoing edges will
9344 be traversed. When this optimization applies, we are able
9345 to avoid conditionals at runtime and we may expose secondary
9346 optimization opportunities.
9348 This routine is effectively a driver for the generic jump
9349 threading code. It basically just presents the generic code
9350 with edges that may be suitable for jump threading.
9352 Unlike DOM, we do not iterate VRP if jump threading was successful.
9353 While iterating may expose new opportunities for VRP, it is expected
9354 those opportunities would be very limited and the compile time cost
9355 to expose those opportunities would be significant.
9357 As jump threading opportunities are discovered, they are registered
9358 for later realization. */
9360 static void
9361 identify_jump_threads (void)
9363 basic_block bb;
9364 gimple dummy;
9365 int i;
9366 edge e;
9368 /* Ugh. When substituting values earlier in this pass we can
9369 wipe the dominance information. So rebuild the dominator
9370 information as we need it within the jump threading code. */
9371 calculate_dominance_info (CDI_DOMINATORS);
9373 /* We do not allow VRP information to be used for jump threading
9374 across a back edge in the CFG. Otherwise it becomes too
9375 difficult to avoid eliminating loop exit tests. Of course
9376 EDGE_DFS_BACK is not accurate at this time so we have to
9377 recompute it. */
9378 mark_dfs_back_edges ();
9380 /* Do not thread across edges we are about to remove. Just marking
9381 them as EDGE_DFS_BACK will do. */
9382 FOR_EACH_VEC_ELT (to_remove_edges, i, e)
9383 e->flags |= EDGE_DFS_BACK;
9385 /* Allocate our unwinder stack to unwind any temporary equivalences
9386 that might be recorded. */
9387 equiv_stack.create (20);
9389 /* To avoid lots of silly node creation, we create a single
9390 conditional and just modify it in-place when attempting to
9391 thread jumps. */
9392 dummy = gimple_build_cond (EQ_EXPR,
9393 integer_zero_node, integer_zero_node,
9394 NULL, NULL);
9396 /* Walk through all the blocks finding those which present a
9397 potential jump threading opportunity. We could set this up
9398 as a dominator walker and record data during the walk, but
9399 I doubt it's worth the effort for the classes of jump
9400 threading opportunities we are trying to identify at this
9401 point in compilation. */
9402 FOR_EACH_BB (bb)
9404 gimple last;
9406 /* If the generic jump threading code does not find this block
9407 interesting, then there is nothing to do. */
9408 if (! potentially_threadable_block (bb))
9409 continue;
9411 /* We only care about blocks ending in a COND_EXPR. While there
9412 may be some value in handling SWITCH_EXPR here, I doubt it's
9413 terribly important. */
9414 last = gsi_stmt (gsi_last_bb (bb));
9416 /* We're basically looking for a switch or any kind of conditional with
9417 integral or pointer type arguments. Note the type of the second
9418 argument will be the same as the first argument, so no need to
9419 check it explicitly. */
9420 if (gimple_code (last) == GIMPLE_SWITCH
9421 || (gimple_code (last) == GIMPLE_COND
9422 && TREE_CODE (gimple_cond_lhs (last)) == SSA_NAME
9423 && (INTEGRAL_TYPE_P (TREE_TYPE (gimple_cond_lhs (last)))
9424 || POINTER_TYPE_P (TREE_TYPE (gimple_cond_lhs (last))))
9425 && (TREE_CODE (gimple_cond_rhs (last)) == SSA_NAME
9426 || is_gimple_min_invariant (gimple_cond_rhs (last)))))
9428 edge_iterator ei;
9430 /* We've got a block with multiple predecessors and multiple
9431 successors which also ends in a suitable conditional or
9432 switch statement. For each predecessor, see if we can thread
9433 it to a specific successor. */
9434 FOR_EACH_EDGE (e, ei, bb->preds)
9436 /* Do not thread across back edges or abnormal edges
9437 in the CFG. */
9438 if (e->flags & (EDGE_DFS_BACK | EDGE_COMPLEX))
9439 continue;
9441 thread_across_edge (dummy, e, true, &equiv_stack,
9442 simplify_stmt_for_jump_threading);
9447 /* We do not actually update the CFG or SSA graphs at this point as
9448 ASSERT_EXPRs are still in the IL and cfg cleanup code does not yet
9449 handle ASSERT_EXPRs gracefully. */
9452 /* We identified all the jump threading opportunities earlier, but could
9453 not transform the CFG at that time. This routine transforms the
9454 CFG and arranges for the dominator tree to be rebuilt if necessary.
9456 Note the SSA graph update will occur during the normal TODO
9457 processing by the pass manager. */
9458 static void
9459 finalize_jump_threads (void)
9461 thread_through_all_blocks (false);
9462 equiv_stack.release ();
9466 /* Traverse all the blocks folding conditionals with known ranges. */
9468 static void
9469 vrp_finalize (void)
9471 size_t i;
9473 values_propagated = true;
9475 if (dump_file)
9477 fprintf (dump_file, "\nValue ranges after VRP:\n\n");
9478 dump_all_value_ranges (dump_file);
9479 fprintf (dump_file, "\n");
9482 substitute_and_fold (op_with_constant_singleton_value_range,
9483 vrp_fold_stmt, false);
9485 if (warn_array_bounds)
9486 check_all_array_refs ();
9488 /* We must identify jump threading opportunities before we release
9489 the datastructures built by VRP. */
9490 identify_jump_threads ();
9492 /* Set value range to non pointer SSA_NAMEs. */
9493 for (i = 0; i < num_vr_values; i++)
9494 if (vr_value[i])
9496 tree name = ssa_name (i);
9498 if (!name
9499 || POINTER_TYPE_P (TREE_TYPE (name))
9500 || (vr_value[i]->type == VR_VARYING)
9501 || (vr_value[i]->type == VR_UNDEFINED))
9502 continue;
9504 if ((TREE_CODE (vr_value[i]->min) == INTEGER_CST)
9505 && (TREE_CODE (vr_value[i]->max) == INTEGER_CST))
9507 if (vr_value[i]->type == VR_RANGE)
9508 set_range_info (name,
9509 tree_to_double_int (vr_value[i]->min),
9510 tree_to_double_int (vr_value[i]->max));
9511 else if (vr_value[i]->type == VR_ANTI_RANGE)
9513 /* VR_ANTI_RANGE ~[min, max] is encoded compactly as
9514 [max + 1, min - 1] without additional attributes.
9515 When min value > max value, we know that it is
9516 VR_ANTI_RANGE; it is VR_RANGE otherwise. */
9518 /* ~[0,0] anti-range is represented as
9519 range. */
9520 if (TYPE_UNSIGNED (TREE_TYPE (name))
9521 && integer_zerop (vr_value[i]->min)
9522 && integer_zerop (vr_value[i]->max))
9523 set_range_info (name,
9524 double_int_one,
9525 double_int::max_value
9526 (TYPE_PRECISION (TREE_TYPE (name)), true));
9527 else
9528 set_range_info (name,
9529 tree_to_double_int (vr_value[i]->max)
9530 + double_int_one,
9531 tree_to_double_int (vr_value[i]->min)
9532 - double_int_one);
9537 /* Free allocated memory. */
9538 for (i = 0; i < num_vr_values; i++)
9539 if (vr_value[i])
9541 BITMAP_FREE (vr_value[i]->equiv);
9542 free (vr_value[i]);
9545 free (vr_value);
9546 free (vr_phi_edge_counts);
9548 /* So that we can distinguish between VRP data being available
9549 and not available. */
9550 vr_value = NULL;
9551 vr_phi_edge_counts = NULL;
9555 /* Main entry point to VRP (Value Range Propagation). This pass is
9556 loosely based on J. R. C. Patterson, ``Accurate Static Branch
9557 Prediction by Value Range Propagation,'' in SIGPLAN Conference on
9558 Programming Language Design and Implementation, pp. 67-78, 1995.
9559 Also available at http://citeseer.ist.psu.edu/patterson95accurate.html
9561 This is essentially an SSA-CCP pass modified to deal with ranges
9562 instead of constants.
9564 While propagating ranges, we may find that two or more SSA name
9565 have equivalent, though distinct ranges. For instance,
9567 1 x_9 = p_3->a;
9568 2 p_4 = ASSERT_EXPR <p_3, p_3 != 0>
9569 3 if (p_4 == q_2)
9570 4 p_5 = ASSERT_EXPR <p_4, p_4 == q_2>;
9571 5 endif
9572 6 if (q_2)
9574 In the code above, pointer p_5 has range [q_2, q_2], but from the
9575 code we can also determine that p_5 cannot be NULL and, if q_2 had
9576 a non-varying range, p_5's range should also be compatible with it.
9578 These equivalences are created by two expressions: ASSERT_EXPR and
9579 copy operations. Since p_5 is an assertion on p_4, and p_4 was the
9580 result of another assertion, then we can use the fact that p_5 and
9581 p_4 are equivalent when evaluating p_5's range.
9583 Together with value ranges, we also propagate these equivalences
9584 between names so that we can take advantage of information from
9585 multiple ranges when doing final replacement. Note that this
9586 equivalency relation is transitive but not symmetric.
9588 In the example above, p_5 is equivalent to p_4, q_2 and p_3, but we
9589 cannot assert that q_2 is equivalent to p_5 because q_2 may be used
9590 in contexts where that assertion does not hold (e.g., in line 6).
9592 TODO, the main difference between this pass and Patterson's is that
9593 we do not propagate edge probabilities. We only compute whether
9594 edges can be taken or not. That is, instead of having a spectrum
9595 of jump probabilities between 0 and 1, we only deal with 0, 1 and
9596 DON'T KNOW. In the future, it may be worthwhile to propagate
9597 probabilities to aid branch prediction. */
9599 static unsigned int
9600 execute_vrp (void)
9602 int i;
9603 edge e;
9604 switch_update *su;
9606 loop_optimizer_init (LOOPS_NORMAL | LOOPS_HAVE_RECORDED_EXITS);
9607 rewrite_into_loop_closed_ssa (NULL, TODO_update_ssa);
9608 scev_initialize ();
9610 /* ??? This ends up using stale EDGE_DFS_BACK for liveness computation.
9611 Inserting assertions may split edges which will invalidate
9612 EDGE_DFS_BACK. */
9613 insert_range_assertions ();
9615 to_remove_edges.create (10);
9616 to_update_switch_stmts.create (5);
9617 threadedge_initialize_values ();
9619 /* For visiting PHI nodes we need EDGE_DFS_BACK computed. */
9620 mark_dfs_back_edges ();
9622 vrp_initialize ();
9623 ssa_propagate (vrp_visit_stmt, vrp_visit_phi_node);
9624 vrp_finalize ();
9626 free_numbers_of_iterations_estimates ();
9628 /* ASSERT_EXPRs must be removed before finalizing jump threads
9629 as finalizing jump threads calls the CFG cleanup code which
9630 does not properly handle ASSERT_EXPRs. */
9631 remove_range_assertions ();
9633 /* If we exposed any new variables, go ahead and put them into
9634 SSA form now, before we handle jump threading. This simplifies
9635 interactions between rewriting of _DECL nodes into SSA form
9636 and rewriting SSA_NAME nodes into SSA form after block
9637 duplication and CFG manipulation. */
9638 update_ssa (TODO_update_ssa);
9640 finalize_jump_threads ();
9642 /* Remove dead edges from SWITCH_EXPR optimization. This leaves the
9643 CFG in a broken state and requires a cfg_cleanup run. */
9644 FOR_EACH_VEC_ELT (to_remove_edges, i, e)
9645 remove_edge (e);
9646 /* Update SWITCH_EXPR case label vector. */
9647 FOR_EACH_VEC_ELT (to_update_switch_stmts, i, su)
9649 size_t j;
9650 size_t n = TREE_VEC_LENGTH (su->vec);
9651 tree label;
9652 gimple_switch_set_num_labels (su->stmt, n);
9653 for (j = 0; j < n; j++)
9654 gimple_switch_set_label (su->stmt, j, TREE_VEC_ELT (su->vec, j));
9655 /* As we may have replaced the default label with a regular one
9656 make sure to make it a real default label again. This ensures
9657 optimal expansion. */
9658 label = gimple_switch_label (su->stmt, 0);
9659 CASE_LOW (label) = NULL_TREE;
9660 CASE_HIGH (label) = NULL_TREE;
9663 if (to_remove_edges.length () > 0)
9665 free_dominance_info (CDI_DOMINATORS);
9666 if (current_loops)
9667 loops_state_set (LOOPS_NEED_FIXUP);
9670 to_remove_edges.release ();
9671 to_update_switch_stmts.release ();
9672 threadedge_finalize_values ();
9674 scev_finalize ();
9675 loop_optimizer_finalize ();
9676 return 0;
9679 static bool
9680 gate_vrp (void)
9682 return flag_tree_vrp != 0;
9685 namespace {
9687 const pass_data pass_data_vrp =
9689 GIMPLE_PASS, /* type */
9690 "vrp", /* name */
9691 OPTGROUP_NONE, /* optinfo_flags */
9692 true, /* has_gate */
9693 true, /* has_execute */
9694 TV_TREE_VRP, /* tv_id */
9695 PROP_ssa, /* properties_required */
9696 0, /* properties_provided */
9697 0, /* properties_destroyed */
9698 0, /* todo_flags_start */
9699 ( TODO_cleanup_cfg | TODO_update_ssa
9700 | TODO_verify_ssa
9701 | TODO_verify_flow ), /* todo_flags_finish */
9704 class pass_vrp : public gimple_opt_pass
9706 public:
9707 pass_vrp (gcc::context *ctxt)
9708 : gimple_opt_pass (pass_data_vrp, ctxt)
9711 /* opt_pass methods: */
9712 opt_pass * clone () { return new pass_vrp (m_ctxt); }
9713 bool gate () { return gate_vrp (); }
9714 unsigned int execute () { return execute_vrp (); }
9716 }; // class pass_vrp
9718 } // anon namespace
9720 gimple_opt_pass *
9721 make_pass_vrp (gcc::context *ctxt)
9723 return new pass_vrp (ctxt);