* config/rs6000/rs6000.c (rs6000_option_override_internal): Do not
[official-gcc.git] / gcc / tree-vrp.c
blobb728dddb87113bafa5b93ffeb1ad60eed56c0dd5
1 /* Support routines for Value Range Propagation (VRP).
2 Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
3 Free Software Foundation, Inc.
4 Contributed by Diego Novillo <dnovillo@redhat.com>.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "ggc.h"
27 #include "flags.h"
28 #include "tree.h"
29 #include "basic-block.h"
30 #include "tree-flow.h"
31 #include "tree-pass.h"
32 #include "tree-dump.h"
33 #include "gimple-pretty-print.h"
34 #include "diagnostic-core.h"
35 #include "intl.h"
36 #include "cfgloop.h"
37 #include "tree-scalar-evolution.h"
38 #include "tree-ssa-propagate.h"
39 #include "tree-chrec.h"
40 #include "gimple-fold.h"
41 #include "expr.h"
42 #include "optabs.h"
45 /* Type of value ranges. See value_range_d for a description of these
46 types. */
47 enum value_range_type { VR_UNDEFINED, VR_RANGE, VR_ANTI_RANGE, VR_VARYING };
49 /* Range of values that can be associated with an SSA_NAME after VRP
50 has executed. */
51 struct value_range_d
53 /* Lattice value represented by this range. */
54 enum value_range_type type;
56 /* Minimum and maximum values represented by this range. These
57 values should be interpreted as follows:
59 - If TYPE is VR_UNDEFINED or VR_VARYING then MIN and MAX must
60 be NULL.
62 - If TYPE == VR_RANGE then MIN holds the minimum value and
63 MAX holds the maximum value of the range [MIN, MAX].
65 - If TYPE == ANTI_RANGE the variable is known to NOT
66 take any values in the range [MIN, MAX]. */
67 tree min;
68 tree max;
70 /* Set of SSA names whose value ranges are equivalent to this one.
71 This set is only valid when TYPE is VR_RANGE or VR_ANTI_RANGE. */
72 bitmap equiv;
75 typedef struct value_range_d value_range_t;
77 #define VR_INITIALIZER { VR_UNDEFINED, NULL_TREE, NULL_TREE, NULL }
79 /* Set of SSA names found live during the RPO traversal of the function
80 for still active basic-blocks. */
81 static sbitmap *live;
83 /* Return true if the SSA name NAME is live on the edge E. */
85 static bool
86 live_on_edge (edge e, tree name)
88 return (live[e->dest->index]
89 && TEST_BIT (live[e->dest->index], SSA_NAME_VERSION (name)));
92 /* Local functions. */
93 static int compare_values (tree val1, tree val2);
94 static int compare_values_warnv (tree val1, tree val2, bool *);
95 static void vrp_meet (value_range_t *, value_range_t *);
96 static void vrp_intersect_ranges (value_range_t *, value_range_t *);
97 static tree vrp_evaluate_conditional_warnv_with_ops (enum tree_code,
98 tree, tree, bool, bool *,
99 bool *);
101 /* Location information for ASSERT_EXPRs. Each instance of this
102 structure describes an ASSERT_EXPR for an SSA name. Since a single
103 SSA name may have more than one assertion associated with it, these
104 locations are kept in a linked list attached to the corresponding
105 SSA name. */
106 struct assert_locus_d
108 /* Basic block where the assertion would be inserted. */
109 basic_block bb;
111 /* Some assertions need to be inserted on an edge (e.g., assertions
112 generated by COND_EXPRs). In those cases, BB will be NULL. */
113 edge e;
115 /* Pointer to the statement that generated this assertion. */
116 gimple_stmt_iterator si;
118 /* Predicate code for the ASSERT_EXPR. Must be COMPARISON_CLASS_P. */
119 enum tree_code comp_code;
121 /* Value being compared against. */
122 tree val;
124 /* Expression to compare. */
125 tree expr;
127 /* Next node in the linked list. */
128 struct assert_locus_d *next;
131 typedef struct assert_locus_d *assert_locus_t;
133 /* If bit I is present, it means that SSA name N_i has a list of
134 assertions that should be inserted in the IL. */
135 static bitmap need_assert_for;
137 /* Array of locations lists where to insert assertions. ASSERTS_FOR[I]
138 holds a list of ASSERT_LOCUS_T nodes that describe where
139 ASSERT_EXPRs for SSA name N_I should be inserted. */
140 static assert_locus_t *asserts_for;
142 /* Value range array. After propagation, VR_VALUE[I] holds the range
143 of values that SSA name N_I may take. */
144 static unsigned num_vr_values;
145 static value_range_t **vr_value;
146 static bool values_propagated;
148 /* For a PHI node which sets SSA name N_I, VR_COUNTS[I] holds the
149 number of executable edges we saw the last time we visited the
150 node. */
151 static int *vr_phi_edge_counts;
153 typedef struct {
154 gimple stmt;
155 tree vec;
156 } switch_update;
158 static VEC (edge, heap) *to_remove_edges;
159 DEF_VEC_O(switch_update);
160 DEF_VEC_ALLOC_O(switch_update, heap);
161 static VEC (switch_update, heap) *to_update_switch_stmts;
164 /* Return the maximum value for TYPE. */
166 static inline tree
167 vrp_val_max (const_tree type)
169 if (!INTEGRAL_TYPE_P (type))
170 return NULL_TREE;
172 return TYPE_MAX_VALUE (type);
175 /* Return the minimum value for TYPE. */
177 static inline tree
178 vrp_val_min (const_tree type)
180 if (!INTEGRAL_TYPE_P (type))
181 return NULL_TREE;
183 return TYPE_MIN_VALUE (type);
186 /* Return whether VAL is equal to the maximum value of its type. This
187 will be true for a positive overflow infinity. We can't do a
188 simple equality comparison with TYPE_MAX_VALUE because C typedefs
189 and Ada subtypes can produce types whose TYPE_MAX_VALUE is not ==
190 to the integer constant with the same value in the type. */
192 static inline bool
193 vrp_val_is_max (const_tree val)
195 tree type_max = vrp_val_max (TREE_TYPE (val));
196 return (val == type_max
197 || (type_max != NULL_TREE
198 && operand_equal_p (val, type_max, 0)));
201 /* Return whether VAL is equal to the minimum value of its type. This
202 will be true for a negative overflow infinity. */
204 static inline bool
205 vrp_val_is_min (const_tree val)
207 tree type_min = vrp_val_min (TREE_TYPE (val));
208 return (val == type_min
209 || (type_min != NULL_TREE
210 && operand_equal_p (val, type_min, 0)));
214 /* Return whether TYPE should use an overflow infinity distinct from
215 TYPE_{MIN,MAX}_VALUE. We use an overflow infinity value to
216 represent a signed overflow during VRP computations. An infinity
217 is distinct from a half-range, which will go from some number to
218 TYPE_{MIN,MAX}_VALUE. */
220 static inline bool
221 needs_overflow_infinity (const_tree type)
223 return INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_WRAPS (type);
226 /* Return whether TYPE can support our overflow infinity
227 representation: we use the TREE_OVERFLOW flag, which only exists
228 for constants. If TYPE doesn't support this, we don't optimize
229 cases which would require signed overflow--we drop them to
230 VARYING. */
232 static inline bool
233 supports_overflow_infinity (const_tree type)
235 tree min = vrp_val_min (type), max = vrp_val_max (type);
236 #ifdef ENABLE_CHECKING
237 gcc_assert (needs_overflow_infinity (type));
238 #endif
239 return (min != NULL_TREE
240 && CONSTANT_CLASS_P (min)
241 && max != NULL_TREE
242 && CONSTANT_CLASS_P (max));
245 /* VAL is the maximum or minimum value of a type. Return a
246 corresponding overflow infinity. */
248 static inline tree
249 make_overflow_infinity (tree val)
251 gcc_checking_assert (val != NULL_TREE && CONSTANT_CLASS_P (val));
252 val = copy_node (val);
253 TREE_OVERFLOW (val) = 1;
254 return val;
257 /* Return a negative overflow infinity for TYPE. */
259 static inline tree
260 negative_overflow_infinity (tree type)
262 gcc_checking_assert (supports_overflow_infinity (type));
263 return make_overflow_infinity (vrp_val_min (type));
266 /* Return a positive overflow infinity for TYPE. */
268 static inline tree
269 positive_overflow_infinity (tree type)
271 gcc_checking_assert (supports_overflow_infinity (type));
272 return make_overflow_infinity (vrp_val_max (type));
275 /* Return whether VAL is a negative overflow infinity. */
277 static inline bool
278 is_negative_overflow_infinity (const_tree val)
280 return (needs_overflow_infinity (TREE_TYPE (val))
281 && CONSTANT_CLASS_P (val)
282 && TREE_OVERFLOW (val)
283 && vrp_val_is_min (val));
286 /* Return whether VAL is a positive overflow infinity. */
288 static inline bool
289 is_positive_overflow_infinity (const_tree val)
291 return (needs_overflow_infinity (TREE_TYPE (val))
292 && CONSTANT_CLASS_P (val)
293 && TREE_OVERFLOW (val)
294 && vrp_val_is_max (val));
297 /* Return whether VAL is a positive or negative overflow infinity. */
299 static inline bool
300 is_overflow_infinity (const_tree val)
302 return (needs_overflow_infinity (TREE_TYPE (val))
303 && CONSTANT_CLASS_P (val)
304 && TREE_OVERFLOW (val)
305 && (vrp_val_is_min (val) || vrp_val_is_max (val)));
308 /* Return whether STMT has a constant rhs that is_overflow_infinity. */
310 static inline bool
311 stmt_overflow_infinity (gimple stmt)
313 if (is_gimple_assign (stmt)
314 && get_gimple_rhs_class (gimple_assign_rhs_code (stmt)) ==
315 GIMPLE_SINGLE_RHS)
316 return is_overflow_infinity (gimple_assign_rhs1 (stmt));
317 return false;
320 /* If VAL is now an overflow infinity, return VAL. Otherwise, return
321 the same value with TREE_OVERFLOW clear. This can be used to avoid
322 confusing a regular value with an overflow value. */
324 static inline tree
325 avoid_overflow_infinity (tree val)
327 if (!is_overflow_infinity (val))
328 return val;
330 if (vrp_val_is_max (val))
331 return vrp_val_max (TREE_TYPE (val));
332 else
334 gcc_checking_assert (vrp_val_is_min (val));
335 return vrp_val_min (TREE_TYPE (val));
340 /* Return true if ARG is marked with the nonnull attribute in the
341 current function signature. */
343 static bool
344 nonnull_arg_p (const_tree arg)
346 tree t, attrs, fntype;
347 unsigned HOST_WIDE_INT arg_num;
349 gcc_assert (TREE_CODE (arg) == PARM_DECL && POINTER_TYPE_P (TREE_TYPE (arg)));
351 /* The static chain decl is always non null. */
352 if (arg == cfun->static_chain_decl)
353 return true;
355 fntype = TREE_TYPE (current_function_decl);
356 for (attrs = TYPE_ATTRIBUTES (fntype); attrs; attrs = TREE_CHAIN (attrs))
358 attrs = lookup_attribute ("nonnull", attrs);
360 /* If "nonnull" wasn't specified, we know nothing about the argument. */
361 if (attrs == NULL_TREE)
362 return false;
364 /* If "nonnull" applies to all the arguments, then ARG is non-null. */
365 if (TREE_VALUE (attrs) == NULL_TREE)
366 return true;
368 /* Get the position number for ARG in the function signature. */
369 for (arg_num = 1, t = DECL_ARGUMENTS (current_function_decl);
371 t = DECL_CHAIN (t), arg_num++)
373 if (t == arg)
374 break;
377 gcc_assert (t == arg);
379 /* Now see if ARG_NUM is mentioned in the nonnull list. */
380 for (t = TREE_VALUE (attrs); t; t = TREE_CHAIN (t))
382 if (compare_tree_int (TREE_VALUE (t), arg_num) == 0)
383 return true;
387 return false;
391 /* Set value range VR to VR_UNDEFINED. */
393 static inline void
394 set_value_range_to_undefined (value_range_t *vr)
396 vr->type = VR_UNDEFINED;
397 vr->min = vr->max = NULL_TREE;
398 if (vr->equiv)
399 bitmap_clear (vr->equiv);
403 /* Set value range VR to VR_VARYING. */
405 static inline void
406 set_value_range_to_varying (value_range_t *vr)
408 vr->type = VR_VARYING;
409 vr->min = vr->max = NULL_TREE;
410 if (vr->equiv)
411 bitmap_clear (vr->equiv);
415 /* Set value range VR to {T, MIN, MAX, EQUIV}. */
417 static void
418 set_value_range (value_range_t *vr, enum value_range_type t, tree min,
419 tree max, bitmap equiv)
421 #if defined ENABLE_CHECKING
422 /* Check the validity of the range. */
423 if (t == VR_RANGE || t == VR_ANTI_RANGE)
425 int cmp;
427 gcc_assert (min && max);
429 if (INTEGRAL_TYPE_P (TREE_TYPE (min)) && t == VR_ANTI_RANGE)
430 gcc_assert (!vrp_val_is_min (min) || !vrp_val_is_max (max));
432 cmp = compare_values (min, max);
433 gcc_assert (cmp == 0 || cmp == -1 || cmp == -2);
435 if (needs_overflow_infinity (TREE_TYPE (min)))
436 gcc_assert (!is_overflow_infinity (min)
437 || !is_overflow_infinity (max));
440 if (t == VR_UNDEFINED || t == VR_VARYING)
441 gcc_assert (min == NULL_TREE && max == NULL_TREE);
443 if (t == VR_UNDEFINED || t == VR_VARYING)
444 gcc_assert (equiv == NULL || bitmap_empty_p (equiv));
445 #endif
447 vr->type = t;
448 vr->min = min;
449 vr->max = max;
451 /* Since updating the equivalence set involves deep copying the
452 bitmaps, only do it if absolutely necessary. */
453 if (vr->equiv == NULL
454 && equiv != NULL)
455 vr->equiv = BITMAP_ALLOC (NULL);
457 if (equiv != vr->equiv)
459 if (equiv && !bitmap_empty_p (equiv))
460 bitmap_copy (vr->equiv, equiv);
461 else
462 bitmap_clear (vr->equiv);
467 /* Set value range VR to the canonical form of {T, MIN, MAX, EQUIV}.
468 This means adjusting T, MIN and MAX representing the case of a
469 wrapping range with MAX < MIN covering [MIN, type_max] U [type_min, MAX]
470 as anti-rage ~[MAX+1, MIN-1]. Likewise for wrapping anti-ranges.
471 In corner cases where MAX+1 or MIN-1 wraps this will fall back
472 to varying.
473 This routine exists to ease canonicalization in the case where we
474 extract ranges from var + CST op limit. */
476 static void
477 set_and_canonicalize_value_range (value_range_t *vr, enum value_range_type t,
478 tree min, tree max, bitmap equiv)
480 /* Use the canonical setters for VR_UNDEFINED and VR_VARYING. */
481 if (t == VR_UNDEFINED)
483 set_value_range_to_undefined (vr);
484 return;
486 else if (t == VR_VARYING)
488 set_value_range_to_varying (vr);
489 return;
492 /* Nothing to canonicalize for symbolic ranges. */
493 if (TREE_CODE (min) != INTEGER_CST
494 || TREE_CODE (max) != INTEGER_CST)
496 set_value_range (vr, t, min, max, equiv);
497 return;
500 /* Wrong order for min and max, to swap them and the VR type we need
501 to adjust them. */
502 if (tree_int_cst_lt (max, min))
504 tree one, tmp;
506 /* For one bit precision if max < min, then the swapped
507 range covers all values, so for VR_RANGE it is varying and
508 for VR_ANTI_RANGE empty range, so drop to varying as well. */
509 if (TYPE_PRECISION (TREE_TYPE (min)) == 1)
511 set_value_range_to_varying (vr);
512 return;
515 one = build_int_cst (TREE_TYPE (min), 1);
516 tmp = int_const_binop (PLUS_EXPR, max, one);
517 max = int_const_binop (MINUS_EXPR, min, one);
518 min = tmp;
520 /* There's one corner case, if we had [C+1, C] before we now have
521 that again. But this represents an empty value range, so drop
522 to varying in this case. */
523 if (tree_int_cst_lt (max, min))
525 set_value_range_to_varying (vr);
526 return;
529 t = t == VR_RANGE ? VR_ANTI_RANGE : VR_RANGE;
532 /* Anti-ranges that can be represented as ranges should be so. */
533 if (t == VR_ANTI_RANGE)
535 bool is_min = vrp_val_is_min (min);
536 bool is_max = vrp_val_is_max (max);
538 if (is_min && is_max)
540 /* We cannot deal with empty ranges, drop to varying.
541 ??? This could be VR_UNDEFINED instead. */
542 set_value_range_to_varying (vr);
543 return;
545 else if (TYPE_PRECISION (TREE_TYPE (min)) == 1
546 && !TYPE_UNSIGNED (TREE_TYPE (min))
547 && (is_min || is_max))
549 /* For signed 1-bit precision, one is not in-range and
550 thus adding/subtracting it would result in overflows. */
551 if (operand_equal_p (min, max, 0))
553 min = max = is_min ? vrp_val_max (TREE_TYPE (min))
554 : vrp_val_min (TREE_TYPE (min));
555 t = VR_RANGE;
557 else
559 set_value_range_to_varying (vr);
560 return;
563 else if (is_min
564 /* As a special exception preserve non-null ranges. */
565 && !(TYPE_UNSIGNED (TREE_TYPE (min))
566 && integer_zerop (max)))
568 tree one = build_int_cst (TREE_TYPE (max), 1);
569 min = int_const_binop (PLUS_EXPR, max, one);
570 max = vrp_val_max (TREE_TYPE (max));
571 t = VR_RANGE;
573 else if (is_max)
575 tree one = build_int_cst (TREE_TYPE (min), 1);
576 max = int_const_binop (MINUS_EXPR, min, one);
577 min = vrp_val_min (TREE_TYPE (min));
578 t = VR_RANGE;
582 /* Drop [-INF(OVF), +INF(OVF)] to varying. */
583 if (needs_overflow_infinity (TREE_TYPE (min))
584 && is_overflow_infinity (min)
585 && is_overflow_infinity (max))
587 set_value_range_to_varying (vr);
588 return;
591 set_value_range (vr, t, min, max, equiv);
594 /* Copy value range FROM into value range TO. */
596 static inline void
597 copy_value_range (value_range_t *to, value_range_t *from)
599 set_value_range (to, from->type, from->min, from->max, from->equiv);
602 /* Set value range VR to a single value. This function is only called
603 with values we get from statements, and exists to clear the
604 TREE_OVERFLOW flag so that we don't think we have an overflow
605 infinity when we shouldn't. */
607 static inline void
608 set_value_range_to_value (value_range_t *vr, tree val, bitmap equiv)
610 gcc_assert (is_gimple_min_invariant (val));
611 val = avoid_overflow_infinity (val);
612 set_value_range (vr, VR_RANGE, val, val, equiv);
615 /* Set value range VR to a non-negative range of type TYPE.
616 OVERFLOW_INFINITY indicates whether to use an overflow infinity
617 rather than TYPE_MAX_VALUE; this should be true if we determine
618 that the range is nonnegative based on the assumption that signed
619 overflow does not occur. */
621 static inline void
622 set_value_range_to_nonnegative (value_range_t *vr, tree type,
623 bool overflow_infinity)
625 tree zero;
627 if (overflow_infinity && !supports_overflow_infinity (type))
629 set_value_range_to_varying (vr);
630 return;
633 zero = build_int_cst (type, 0);
634 set_value_range (vr, VR_RANGE, zero,
635 (overflow_infinity
636 ? positive_overflow_infinity (type)
637 : TYPE_MAX_VALUE (type)),
638 vr->equiv);
641 /* Set value range VR to a non-NULL range of type TYPE. */
643 static inline void
644 set_value_range_to_nonnull (value_range_t *vr, tree type)
646 tree zero = build_int_cst (type, 0);
647 set_value_range (vr, VR_ANTI_RANGE, zero, zero, vr->equiv);
651 /* Set value range VR to a NULL range of type TYPE. */
653 static inline void
654 set_value_range_to_null (value_range_t *vr, tree type)
656 set_value_range_to_value (vr, build_int_cst (type, 0), vr->equiv);
660 /* Set value range VR to a range of a truthvalue of type TYPE. */
662 static inline void
663 set_value_range_to_truthvalue (value_range_t *vr, tree type)
665 if (TYPE_PRECISION (type) == 1)
666 set_value_range_to_varying (vr);
667 else
668 set_value_range (vr, VR_RANGE,
669 build_int_cst (type, 0), build_int_cst (type, 1),
670 vr->equiv);
674 /* If abs (min) < abs (max), set VR to [-max, max], if
675 abs (min) >= abs (max), set VR to [-min, min]. */
677 static void
678 abs_extent_range (value_range_t *vr, tree min, tree max)
680 int cmp;
682 gcc_assert (TREE_CODE (min) == INTEGER_CST);
683 gcc_assert (TREE_CODE (max) == INTEGER_CST);
684 gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (min)));
685 gcc_assert (!TYPE_UNSIGNED (TREE_TYPE (min)));
686 min = fold_unary (ABS_EXPR, TREE_TYPE (min), min);
687 max = fold_unary (ABS_EXPR, TREE_TYPE (max), max);
688 if (TREE_OVERFLOW (min) || TREE_OVERFLOW (max))
690 set_value_range_to_varying (vr);
691 return;
693 cmp = compare_values (min, max);
694 if (cmp == -1)
695 min = fold_unary (NEGATE_EXPR, TREE_TYPE (min), max);
696 else if (cmp == 0 || cmp == 1)
698 max = min;
699 min = fold_unary (NEGATE_EXPR, TREE_TYPE (min), min);
701 else
703 set_value_range_to_varying (vr);
704 return;
706 set_and_canonicalize_value_range (vr, VR_RANGE, min, max, NULL);
710 /* Return value range information for VAR.
712 If we have no values ranges recorded (ie, VRP is not running), then
713 return NULL. Otherwise create an empty range if none existed for VAR. */
715 static value_range_t *
716 get_value_range (const_tree var)
718 static const struct value_range_d vr_const_varying
719 = { VR_VARYING, NULL_TREE, NULL_TREE, NULL };
720 value_range_t *vr;
721 tree sym;
722 unsigned ver = SSA_NAME_VERSION (var);
724 /* If we have no recorded ranges, then return NULL. */
725 if (! vr_value)
726 return NULL;
728 /* If we query the range for a new SSA name return an unmodifiable VARYING.
729 We should get here at most from the substitute-and-fold stage which
730 will never try to change values. */
731 if (ver >= num_vr_values)
732 return CONST_CAST (value_range_t *, &vr_const_varying);
734 vr = vr_value[ver];
735 if (vr)
736 return vr;
738 /* After propagation finished do not allocate new value-ranges. */
739 if (values_propagated)
740 return CONST_CAST (value_range_t *, &vr_const_varying);
742 /* Create a default value range. */
743 vr_value[ver] = vr = XCNEW (value_range_t);
745 /* Defer allocating the equivalence set. */
746 vr->equiv = NULL;
748 /* If VAR is a default definition of a parameter, the variable can
749 take any value in VAR's type. */
750 if (SSA_NAME_IS_DEFAULT_DEF (var))
752 sym = SSA_NAME_VAR (var);
753 if (TREE_CODE (sym) == PARM_DECL)
755 /* Try to use the "nonnull" attribute to create ~[0, 0]
756 anti-ranges for pointers. Note that this is only valid with
757 default definitions of PARM_DECLs. */
758 if (POINTER_TYPE_P (TREE_TYPE (sym))
759 && nonnull_arg_p (sym))
760 set_value_range_to_nonnull (vr, TREE_TYPE (sym));
761 else
762 set_value_range_to_varying (vr);
764 else if (TREE_CODE (sym) == RESULT_DECL
765 && DECL_BY_REFERENCE (sym))
766 set_value_range_to_nonnull (vr, TREE_TYPE (sym));
769 return vr;
772 /* Return true, if VAL1 and VAL2 are equal values for VRP purposes. */
774 static inline bool
775 vrp_operand_equal_p (const_tree val1, const_tree val2)
777 if (val1 == val2)
778 return true;
779 if (!val1 || !val2 || !operand_equal_p (val1, val2, 0))
780 return false;
781 if (is_overflow_infinity (val1))
782 return is_overflow_infinity (val2);
783 return true;
786 /* Return true, if the bitmaps B1 and B2 are equal. */
788 static inline bool
789 vrp_bitmap_equal_p (const_bitmap b1, const_bitmap b2)
791 return (b1 == b2
792 || ((!b1 || bitmap_empty_p (b1))
793 && (!b2 || bitmap_empty_p (b2)))
794 || (b1 && b2
795 && bitmap_equal_p (b1, b2)));
798 /* Update the value range and equivalence set for variable VAR to
799 NEW_VR. Return true if NEW_VR is different from VAR's previous
800 value.
802 NOTE: This function assumes that NEW_VR is a temporary value range
803 object created for the sole purpose of updating VAR's range. The
804 storage used by the equivalence set from NEW_VR will be freed by
805 this function. Do not call update_value_range when NEW_VR
806 is the range object associated with another SSA name. */
808 static inline bool
809 update_value_range (const_tree var, value_range_t *new_vr)
811 value_range_t *old_vr;
812 bool is_new;
814 /* Update the value range, if necessary. */
815 old_vr = get_value_range (var);
816 is_new = old_vr->type != new_vr->type
817 || !vrp_operand_equal_p (old_vr->min, new_vr->min)
818 || !vrp_operand_equal_p (old_vr->max, new_vr->max)
819 || !vrp_bitmap_equal_p (old_vr->equiv, new_vr->equiv);
821 if (is_new)
822 set_value_range (old_vr, new_vr->type, new_vr->min, new_vr->max,
823 new_vr->equiv);
825 BITMAP_FREE (new_vr->equiv);
827 return is_new;
831 /* Add VAR and VAR's equivalence set to EQUIV. This is the central
832 point where equivalence processing can be turned on/off. */
834 static void
835 add_equivalence (bitmap *equiv, const_tree var)
837 unsigned ver = SSA_NAME_VERSION (var);
838 value_range_t *vr = vr_value[ver];
840 if (*equiv == NULL)
841 *equiv = BITMAP_ALLOC (NULL);
842 bitmap_set_bit (*equiv, ver);
843 if (vr && vr->equiv)
844 bitmap_ior_into (*equiv, vr->equiv);
848 /* Return true if VR is ~[0, 0]. */
850 static inline bool
851 range_is_nonnull (value_range_t *vr)
853 return vr->type == VR_ANTI_RANGE
854 && integer_zerop (vr->min)
855 && integer_zerop (vr->max);
859 /* Return true if VR is [0, 0]. */
861 static inline bool
862 range_is_null (value_range_t *vr)
864 return vr->type == VR_RANGE
865 && integer_zerop (vr->min)
866 && integer_zerop (vr->max);
869 /* Return true if max and min of VR are INTEGER_CST. It's not necessary
870 a singleton. */
872 static inline bool
873 range_int_cst_p (value_range_t *vr)
875 return (vr->type == VR_RANGE
876 && TREE_CODE (vr->max) == INTEGER_CST
877 && TREE_CODE (vr->min) == INTEGER_CST);
880 /* Return true if VR is a INTEGER_CST singleton. */
882 static inline bool
883 range_int_cst_singleton_p (value_range_t *vr)
885 return (range_int_cst_p (vr)
886 && !TREE_OVERFLOW (vr->min)
887 && !TREE_OVERFLOW (vr->max)
888 && tree_int_cst_equal (vr->min, vr->max));
891 /* Return true if value range VR involves at least one symbol. */
893 static inline bool
894 symbolic_range_p (value_range_t *vr)
896 return (!is_gimple_min_invariant (vr->min)
897 || !is_gimple_min_invariant (vr->max));
900 /* Return true if value range VR uses an overflow infinity. */
902 static inline bool
903 overflow_infinity_range_p (value_range_t *vr)
905 return (vr->type == VR_RANGE
906 && (is_overflow_infinity (vr->min)
907 || is_overflow_infinity (vr->max)));
910 /* Return false if we can not make a valid comparison based on VR;
911 this will be the case if it uses an overflow infinity and overflow
912 is not undefined (i.e., -fno-strict-overflow is in effect).
913 Otherwise return true, and set *STRICT_OVERFLOW_P to true if VR
914 uses an overflow infinity. */
916 static bool
917 usable_range_p (value_range_t *vr, bool *strict_overflow_p)
919 gcc_assert (vr->type == VR_RANGE);
920 if (is_overflow_infinity (vr->min))
922 *strict_overflow_p = true;
923 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (vr->min)))
924 return false;
926 if (is_overflow_infinity (vr->max))
928 *strict_overflow_p = true;
929 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (vr->max)))
930 return false;
932 return true;
936 /* Return true if the result of assignment STMT is know to be non-negative.
937 If the return value is based on the assumption that signed overflow is
938 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
939 *STRICT_OVERFLOW_P.*/
941 static bool
942 gimple_assign_nonnegative_warnv_p (gimple stmt, bool *strict_overflow_p)
944 enum tree_code code = gimple_assign_rhs_code (stmt);
945 switch (get_gimple_rhs_class (code))
947 case GIMPLE_UNARY_RHS:
948 return tree_unary_nonnegative_warnv_p (gimple_assign_rhs_code (stmt),
949 gimple_expr_type (stmt),
950 gimple_assign_rhs1 (stmt),
951 strict_overflow_p);
952 case GIMPLE_BINARY_RHS:
953 return tree_binary_nonnegative_warnv_p (gimple_assign_rhs_code (stmt),
954 gimple_expr_type (stmt),
955 gimple_assign_rhs1 (stmt),
956 gimple_assign_rhs2 (stmt),
957 strict_overflow_p);
958 case GIMPLE_TERNARY_RHS:
959 return false;
960 case GIMPLE_SINGLE_RHS:
961 return tree_single_nonnegative_warnv_p (gimple_assign_rhs1 (stmt),
962 strict_overflow_p);
963 case GIMPLE_INVALID_RHS:
964 gcc_unreachable ();
965 default:
966 gcc_unreachable ();
970 /* Return true if return value of call STMT is know to be non-negative.
971 If the return value is based on the assumption that signed overflow is
972 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
973 *STRICT_OVERFLOW_P.*/
975 static bool
976 gimple_call_nonnegative_warnv_p (gimple stmt, bool *strict_overflow_p)
978 tree arg0 = gimple_call_num_args (stmt) > 0 ?
979 gimple_call_arg (stmt, 0) : NULL_TREE;
980 tree arg1 = gimple_call_num_args (stmt) > 1 ?
981 gimple_call_arg (stmt, 1) : NULL_TREE;
983 return tree_call_nonnegative_warnv_p (gimple_expr_type (stmt),
984 gimple_call_fndecl (stmt),
985 arg0,
986 arg1,
987 strict_overflow_p);
990 /* Return true if STMT is know to to compute a non-negative value.
991 If the return value is based on the assumption that signed overflow is
992 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
993 *STRICT_OVERFLOW_P.*/
995 static bool
996 gimple_stmt_nonnegative_warnv_p (gimple stmt, bool *strict_overflow_p)
998 switch (gimple_code (stmt))
1000 case GIMPLE_ASSIGN:
1001 return gimple_assign_nonnegative_warnv_p (stmt, strict_overflow_p);
1002 case GIMPLE_CALL:
1003 return gimple_call_nonnegative_warnv_p (stmt, strict_overflow_p);
1004 default:
1005 gcc_unreachable ();
1009 /* Return true if the result of assignment STMT is know to be non-zero.
1010 If the return value is based on the assumption that signed overflow is
1011 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1012 *STRICT_OVERFLOW_P.*/
1014 static bool
1015 gimple_assign_nonzero_warnv_p (gimple stmt, bool *strict_overflow_p)
1017 enum tree_code code = gimple_assign_rhs_code (stmt);
1018 switch (get_gimple_rhs_class (code))
1020 case GIMPLE_UNARY_RHS:
1021 return tree_unary_nonzero_warnv_p (gimple_assign_rhs_code (stmt),
1022 gimple_expr_type (stmt),
1023 gimple_assign_rhs1 (stmt),
1024 strict_overflow_p);
1025 case GIMPLE_BINARY_RHS:
1026 return tree_binary_nonzero_warnv_p (gimple_assign_rhs_code (stmt),
1027 gimple_expr_type (stmt),
1028 gimple_assign_rhs1 (stmt),
1029 gimple_assign_rhs2 (stmt),
1030 strict_overflow_p);
1031 case GIMPLE_TERNARY_RHS:
1032 return false;
1033 case GIMPLE_SINGLE_RHS:
1034 return tree_single_nonzero_warnv_p (gimple_assign_rhs1 (stmt),
1035 strict_overflow_p);
1036 case GIMPLE_INVALID_RHS:
1037 gcc_unreachable ();
1038 default:
1039 gcc_unreachable ();
1043 /* Return true if STMT is know to to compute a non-zero value.
1044 If the return value is based on the assumption that signed overflow is
1045 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1046 *STRICT_OVERFLOW_P.*/
1048 static bool
1049 gimple_stmt_nonzero_warnv_p (gimple stmt, bool *strict_overflow_p)
1051 switch (gimple_code (stmt))
1053 case GIMPLE_ASSIGN:
1054 return gimple_assign_nonzero_warnv_p (stmt, strict_overflow_p);
1055 case GIMPLE_CALL:
1056 return gimple_alloca_call_p (stmt);
1057 default:
1058 gcc_unreachable ();
1062 /* Like tree_expr_nonzero_warnv_p, but this function uses value ranges
1063 obtained so far. */
1065 static bool
1066 vrp_stmt_computes_nonzero (gimple stmt, bool *strict_overflow_p)
1068 if (gimple_stmt_nonzero_warnv_p (stmt, strict_overflow_p))
1069 return true;
1071 /* If we have an expression of the form &X->a, then the expression
1072 is nonnull if X is nonnull. */
1073 if (is_gimple_assign (stmt)
1074 && gimple_assign_rhs_code (stmt) == ADDR_EXPR)
1076 tree expr = gimple_assign_rhs1 (stmt);
1077 tree base = get_base_address (TREE_OPERAND (expr, 0));
1079 if (base != NULL_TREE
1080 && TREE_CODE (base) == MEM_REF
1081 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
1083 value_range_t *vr = get_value_range (TREE_OPERAND (base, 0));
1084 if (range_is_nonnull (vr))
1085 return true;
1089 return false;
1092 /* Returns true if EXPR is a valid value (as expected by compare_values) --
1093 a gimple invariant, or SSA_NAME +- CST. */
1095 static bool
1096 valid_value_p (tree expr)
1098 if (TREE_CODE (expr) == SSA_NAME)
1099 return true;
1101 if (TREE_CODE (expr) == PLUS_EXPR
1102 || TREE_CODE (expr) == MINUS_EXPR)
1103 return (TREE_CODE (TREE_OPERAND (expr, 0)) == SSA_NAME
1104 && TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST);
1106 return is_gimple_min_invariant (expr);
1109 /* Return
1110 1 if VAL < VAL2
1111 0 if !(VAL < VAL2)
1112 -2 if those are incomparable. */
1113 static inline int
1114 operand_less_p (tree val, tree val2)
1116 /* LT is folded faster than GE and others. Inline the common case. */
1117 if (TREE_CODE (val) == INTEGER_CST && TREE_CODE (val2) == INTEGER_CST)
1119 if (TYPE_UNSIGNED (TREE_TYPE (val)))
1120 return INT_CST_LT_UNSIGNED (val, val2);
1121 else
1123 if (INT_CST_LT (val, val2))
1124 return 1;
1127 else
1129 tree tcmp;
1131 fold_defer_overflow_warnings ();
1133 tcmp = fold_binary_to_constant (LT_EXPR, boolean_type_node, val, val2);
1135 fold_undefer_and_ignore_overflow_warnings ();
1137 if (!tcmp
1138 || TREE_CODE (tcmp) != INTEGER_CST)
1139 return -2;
1141 if (!integer_zerop (tcmp))
1142 return 1;
1145 /* val >= val2, not considering overflow infinity. */
1146 if (is_negative_overflow_infinity (val))
1147 return is_negative_overflow_infinity (val2) ? 0 : 1;
1148 else if (is_positive_overflow_infinity (val2))
1149 return is_positive_overflow_infinity (val) ? 0 : 1;
1151 return 0;
1154 /* Compare two values VAL1 and VAL2. Return
1156 -2 if VAL1 and VAL2 cannot be compared at compile-time,
1157 -1 if VAL1 < VAL2,
1158 0 if VAL1 == VAL2,
1159 +1 if VAL1 > VAL2, and
1160 +2 if VAL1 != VAL2
1162 This is similar to tree_int_cst_compare but supports pointer values
1163 and values that cannot be compared at compile time.
1165 If STRICT_OVERFLOW_P is not NULL, then set *STRICT_OVERFLOW_P to
1166 true if the return value is only valid if we assume that signed
1167 overflow is undefined. */
1169 static int
1170 compare_values_warnv (tree val1, tree val2, bool *strict_overflow_p)
1172 if (val1 == val2)
1173 return 0;
1175 /* Below we rely on the fact that VAL1 and VAL2 are both pointers or
1176 both integers. */
1177 gcc_assert (POINTER_TYPE_P (TREE_TYPE (val1))
1178 == POINTER_TYPE_P (TREE_TYPE (val2)));
1179 /* Convert the two values into the same type. This is needed because
1180 sizetype causes sign extension even for unsigned types. */
1181 val2 = fold_convert (TREE_TYPE (val1), val2);
1182 STRIP_USELESS_TYPE_CONVERSION (val2);
1184 if ((TREE_CODE (val1) == SSA_NAME
1185 || TREE_CODE (val1) == PLUS_EXPR
1186 || TREE_CODE (val1) == MINUS_EXPR)
1187 && (TREE_CODE (val2) == SSA_NAME
1188 || TREE_CODE (val2) == PLUS_EXPR
1189 || TREE_CODE (val2) == MINUS_EXPR))
1191 tree n1, c1, n2, c2;
1192 enum tree_code code1, code2;
1194 /* If VAL1 and VAL2 are of the form 'NAME [+-] CST' or 'NAME',
1195 return -1 or +1 accordingly. If VAL1 and VAL2 don't use the
1196 same name, return -2. */
1197 if (TREE_CODE (val1) == SSA_NAME)
1199 code1 = SSA_NAME;
1200 n1 = val1;
1201 c1 = NULL_TREE;
1203 else
1205 code1 = TREE_CODE (val1);
1206 n1 = TREE_OPERAND (val1, 0);
1207 c1 = TREE_OPERAND (val1, 1);
1208 if (tree_int_cst_sgn (c1) == -1)
1210 if (is_negative_overflow_infinity (c1))
1211 return -2;
1212 c1 = fold_unary_to_constant (NEGATE_EXPR, TREE_TYPE (c1), c1);
1213 if (!c1)
1214 return -2;
1215 code1 = code1 == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR;
1219 if (TREE_CODE (val2) == SSA_NAME)
1221 code2 = SSA_NAME;
1222 n2 = val2;
1223 c2 = NULL_TREE;
1225 else
1227 code2 = TREE_CODE (val2);
1228 n2 = TREE_OPERAND (val2, 0);
1229 c2 = TREE_OPERAND (val2, 1);
1230 if (tree_int_cst_sgn (c2) == -1)
1232 if (is_negative_overflow_infinity (c2))
1233 return -2;
1234 c2 = fold_unary_to_constant (NEGATE_EXPR, TREE_TYPE (c2), c2);
1235 if (!c2)
1236 return -2;
1237 code2 = code2 == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR;
1241 /* Both values must use the same name. */
1242 if (n1 != n2)
1243 return -2;
1245 if (code1 == SSA_NAME
1246 && code2 == SSA_NAME)
1247 /* NAME == NAME */
1248 return 0;
1250 /* If overflow is defined we cannot simplify more. */
1251 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (val1)))
1252 return -2;
1254 if (strict_overflow_p != NULL
1255 && (code1 == SSA_NAME || !TREE_NO_WARNING (val1))
1256 && (code2 == SSA_NAME || !TREE_NO_WARNING (val2)))
1257 *strict_overflow_p = true;
1259 if (code1 == SSA_NAME)
1261 if (code2 == PLUS_EXPR)
1262 /* NAME < NAME + CST */
1263 return -1;
1264 else if (code2 == MINUS_EXPR)
1265 /* NAME > NAME - CST */
1266 return 1;
1268 else if (code1 == PLUS_EXPR)
1270 if (code2 == SSA_NAME)
1271 /* NAME + CST > NAME */
1272 return 1;
1273 else if (code2 == PLUS_EXPR)
1274 /* NAME + CST1 > NAME + CST2, if CST1 > CST2 */
1275 return compare_values_warnv (c1, c2, strict_overflow_p);
1276 else if (code2 == MINUS_EXPR)
1277 /* NAME + CST1 > NAME - CST2 */
1278 return 1;
1280 else if (code1 == MINUS_EXPR)
1282 if (code2 == SSA_NAME)
1283 /* NAME - CST < NAME */
1284 return -1;
1285 else if (code2 == PLUS_EXPR)
1286 /* NAME - CST1 < NAME + CST2 */
1287 return -1;
1288 else if (code2 == MINUS_EXPR)
1289 /* NAME - CST1 > NAME - CST2, if CST1 < CST2. Notice that
1290 C1 and C2 are swapped in the call to compare_values. */
1291 return compare_values_warnv (c2, c1, strict_overflow_p);
1294 gcc_unreachable ();
1297 /* We cannot compare non-constants. */
1298 if (!is_gimple_min_invariant (val1) || !is_gimple_min_invariant (val2))
1299 return -2;
1301 if (!POINTER_TYPE_P (TREE_TYPE (val1)))
1303 /* We cannot compare overflowed values, except for overflow
1304 infinities. */
1305 if (TREE_OVERFLOW (val1) || TREE_OVERFLOW (val2))
1307 if (strict_overflow_p != NULL)
1308 *strict_overflow_p = true;
1309 if (is_negative_overflow_infinity (val1))
1310 return is_negative_overflow_infinity (val2) ? 0 : -1;
1311 else if (is_negative_overflow_infinity (val2))
1312 return 1;
1313 else if (is_positive_overflow_infinity (val1))
1314 return is_positive_overflow_infinity (val2) ? 0 : 1;
1315 else if (is_positive_overflow_infinity (val2))
1316 return -1;
1317 return -2;
1320 return tree_int_cst_compare (val1, val2);
1322 else
1324 tree t;
1326 /* First see if VAL1 and VAL2 are not the same. */
1327 if (val1 == val2 || operand_equal_p (val1, val2, 0))
1328 return 0;
1330 /* If VAL1 is a lower address than VAL2, return -1. */
1331 if (operand_less_p (val1, val2) == 1)
1332 return -1;
1334 /* If VAL1 is a higher address than VAL2, return +1. */
1335 if (operand_less_p (val2, val1) == 1)
1336 return 1;
1338 /* If VAL1 is different than VAL2, return +2.
1339 For integer constants we either have already returned -1 or 1
1340 or they are equivalent. We still might succeed in proving
1341 something about non-trivial operands. */
1342 if (TREE_CODE (val1) != INTEGER_CST
1343 || TREE_CODE (val2) != INTEGER_CST)
1345 t = fold_binary_to_constant (NE_EXPR, boolean_type_node, val1, val2);
1346 if (t && integer_onep (t))
1347 return 2;
1350 return -2;
1354 /* Compare values like compare_values_warnv, but treat comparisons of
1355 nonconstants which rely on undefined overflow as incomparable. */
1357 static int
1358 compare_values (tree val1, tree val2)
1360 bool sop;
1361 int ret;
1363 sop = false;
1364 ret = compare_values_warnv (val1, val2, &sop);
1365 if (sop
1366 && (!is_gimple_min_invariant (val1) || !is_gimple_min_invariant (val2)))
1367 ret = -2;
1368 return ret;
1372 /* Return 1 if VAL is inside value range MIN <= VAL <= MAX,
1373 0 if VAL is not inside [MIN, MAX],
1374 -2 if we cannot tell either way.
1376 Benchmark compile/20001226-1.c compilation time after changing this
1377 function. */
1379 static inline int
1380 value_inside_range (tree val, tree min, tree max)
1382 int cmp1, cmp2;
1384 cmp1 = operand_less_p (val, min);
1385 if (cmp1 == -2)
1386 return -2;
1387 if (cmp1 == 1)
1388 return 0;
1390 cmp2 = operand_less_p (max, val);
1391 if (cmp2 == -2)
1392 return -2;
1394 return !cmp2;
1398 /* Return true if value ranges VR0 and VR1 have a non-empty
1399 intersection.
1401 Benchmark compile/20001226-1.c compilation time after changing this
1402 function.
1405 static inline bool
1406 value_ranges_intersect_p (value_range_t *vr0, value_range_t *vr1)
1408 /* The value ranges do not intersect if the maximum of the first range is
1409 less than the minimum of the second range or vice versa.
1410 When those relations are unknown, we can't do any better. */
1411 if (operand_less_p (vr0->max, vr1->min) != 0)
1412 return false;
1413 if (operand_less_p (vr1->max, vr0->min) != 0)
1414 return false;
1415 return true;
1419 /* Return 1 if [MIN, MAX] includes the value zero, 0 if it does not
1420 include the value zero, -2 if we cannot tell. */
1422 static inline int
1423 range_includes_zero_p (tree min, tree max)
1425 tree zero = build_int_cst (TREE_TYPE (min), 0);
1426 return value_inside_range (zero, min, max);
1429 /* Return true if *VR is know to only contain nonnegative values. */
1431 static inline bool
1432 value_range_nonnegative_p (value_range_t *vr)
1434 /* Testing for VR_ANTI_RANGE is not useful here as any anti-range
1435 which would return a useful value should be encoded as a
1436 VR_RANGE. */
1437 if (vr->type == VR_RANGE)
1439 int result = compare_values (vr->min, integer_zero_node);
1440 return (result == 0 || result == 1);
1443 return false;
1446 /* Return true if T, an SSA_NAME, is known to be nonnegative. Return
1447 false otherwise or if no value range information is available. */
1449 bool
1450 ssa_name_nonnegative_p (const_tree t)
1452 value_range_t *vr = get_value_range (t);
1454 if (INTEGRAL_TYPE_P (t)
1455 && TYPE_UNSIGNED (t))
1456 return true;
1458 if (!vr)
1459 return false;
1461 return value_range_nonnegative_p (vr);
1464 /* If *VR has a value rante that is a single constant value return that,
1465 otherwise return NULL_TREE. */
1467 static tree
1468 value_range_constant_singleton (value_range_t *vr)
1470 if (vr->type == VR_RANGE
1471 && operand_equal_p (vr->min, vr->max, 0)
1472 && is_gimple_min_invariant (vr->min))
1473 return vr->min;
1475 return NULL_TREE;
1478 /* If OP has a value range with a single constant value return that,
1479 otherwise return NULL_TREE. This returns OP itself if OP is a
1480 constant. */
1482 static tree
1483 op_with_constant_singleton_value_range (tree op)
1485 if (is_gimple_min_invariant (op))
1486 return op;
1488 if (TREE_CODE (op) != SSA_NAME)
1489 return NULL_TREE;
1491 return value_range_constant_singleton (get_value_range (op));
1494 /* Return true if op is in a boolean [0, 1] value-range. */
1496 static bool
1497 op_with_boolean_value_range_p (tree op)
1499 value_range_t *vr;
1501 if (TYPE_PRECISION (TREE_TYPE (op)) == 1)
1502 return true;
1504 if (integer_zerop (op)
1505 || integer_onep (op))
1506 return true;
1508 if (TREE_CODE (op) != SSA_NAME)
1509 return false;
1511 vr = get_value_range (op);
1512 return (vr->type == VR_RANGE
1513 && integer_zerop (vr->min)
1514 && integer_onep (vr->max));
1517 /* Extract value range information from an ASSERT_EXPR EXPR and store
1518 it in *VR_P. */
1520 static void
1521 extract_range_from_assert (value_range_t *vr_p, tree expr)
1523 tree var, cond, limit, min, max, type;
1524 value_range_t *limit_vr;
1525 enum tree_code cond_code;
1527 var = ASSERT_EXPR_VAR (expr);
1528 cond = ASSERT_EXPR_COND (expr);
1530 gcc_assert (COMPARISON_CLASS_P (cond));
1532 /* Find VAR in the ASSERT_EXPR conditional. */
1533 if (var == TREE_OPERAND (cond, 0)
1534 || TREE_CODE (TREE_OPERAND (cond, 0)) == PLUS_EXPR
1535 || TREE_CODE (TREE_OPERAND (cond, 0)) == NOP_EXPR)
1537 /* If the predicate is of the form VAR COMP LIMIT, then we just
1538 take LIMIT from the RHS and use the same comparison code. */
1539 cond_code = TREE_CODE (cond);
1540 limit = TREE_OPERAND (cond, 1);
1541 cond = TREE_OPERAND (cond, 0);
1543 else
1545 /* If the predicate is of the form LIMIT COMP VAR, then we need
1546 to flip around the comparison code to create the proper range
1547 for VAR. */
1548 cond_code = swap_tree_comparison (TREE_CODE (cond));
1549 limit = TREE_OPERAND (cond, 0);
1550 cond = TREE_OPERAND (cond, 1);
1553 limit = avoid_overflow_infinity (limit);
1555 type = TREE_TYPE (var);
1556 gcc_assert (limit != var);
1558 /* For pointer arithmetic, we only keep track of pointer equality
1559 and inequality. */
1560 if (POINTER_TYPE_P (type) && cond_code != NE_EXPR && cond_code != EQ_EXPR)
1562 set_value_range_to_varying (vr_p);
1563 return;
1566 /* If LIMIT is another SSA name and LIMIT has a range of its own,
1567 try to use LIMIT's range to avoid creating symbolic ranges
1568 unnecessarily. */
1569 limit_vr = (TREE_CODE (limit) == SSA_NAME) ? get_value_range (limit) : NULL;
1571 /* LIMIT's range is only interesting if it has any useful information. */
1572 if (limit_vr
1573 && (limit_vr->type == VR_UNDEFINED
1574 || limit_vr->type == VR_VARYING
1575 || symbolic_range_p (limit_vr)))
1576 limit_vr = NULL;
1578 /* Initially, the new range has the same set of equivalences of
1579 VAR's range. This will be revised before returning the final
1580 value. Since assertions may be chained via mutually exclusive
1581 predicates, we will need to trim the set of equivalences before
1582 we are done. */
1583 gcc_assert (vr_p->equiv == NULL);
1584 add_equivalence (&vr_p->equiv, var);
1586 /* Extract a new range based on the asserted comparison for VAR and
1587 LIMIT's value range. Notice that if LIMIT has an anti-range, we
1588 will only use it for equality comparisons (EQ_EXPR). For any
1589 other kind of assertion, we cannot derive a range from LIMIT's
1590 anti-range that can be used to describe the new range. For
1591 instance, ASSERT_EXPR <x_2, x_2 <= b_4>. If b_4 is ~[2, 10],
1592 then b_4 takes on the ranges [-INF, 1] and [11, +INF]. There is
1593 no single range for x_2 that could describe LE_EXPR, so we might
1594 as well build the range [b_4, +INF] for it.
1595 One special case we handle is extracting a range from a
1596 range test encoded as (unsigned)var + CST <= limit. */
1597 if (TREE_CODE (cond) == NOP_EXPR
1598 || TREE_CODE (cond) == PLUS_EXPR)
1600 if (TREE_CODE (cond) == PLUS_EXPR)
1602 min = fold_build1 (NEGATE_EXPR, TREE_TYPE (TREE_OPERAND (cond, 1)),
1603 TREE_OPERAND (cond, 1));
1604 max = int_const_binop (PLUS_EXPR, limit, min);
1605 cond = TREE_OPERAND (cond, 0);
1607 else
1609 min = build_int_cst (TREE_TYPE (var), 0);
1610 max = limit;
1613 /* Make sure to not set TREE_OVERFLOW on the final type
1614 conversion. We are willingly interpreting large positive
1615 unsigned values as negative singed values here. */
1616 min = force_fit_type_double (TREE_TYPE (var), tree_to_double_int (min),
1617 0, false);
1618 max = force_fit_type_double (TREE_TYPE (var), tree_to_double_int (max),
1619 0, false);
1621 /* We can transform a max, min range to an anti-range or
1622 vice-versa. Use set_and_canonicalize_value_range which does
1623 this for us. */
1624 if (cond_code == LE_EXPR)
1625 set_and_canonicalize_value_range (vr_p, VR_RANGE,
1626 min, max, vr_p->equiv);
1627 else if (cond_code == GT_EXPR)
1628 set_and_canonicalize_value_range (vr_p, VR_ANTI_RANGE,
1629 min, max, vr_p->equiv);
1630 else
1631 gcc_unreachable ();
1633 else if (cond_code == EQ_EXPR)
1635 enum value_range_type range_type;
1637 if (limit_vr)
1639 range_type = limit_vr->type;
1640 min = limit_vr->min;
1641 max = limit_vr->max;
1643 else
1645 range_type = VR_RANGE;
1646 min = limit;
1647 max = limit;
1650 set_value_range (vr_p, range_type, min, max, vr_p->equiv);
1652 /* When asserting the equality VAR == LIMIT and LIMIT is another
1653 SSA name, the new range will also inherit the equivalence set
1654 from LIMIT. */
1655 if (TREE_CODE (limit) == SSA_NAME)
1656 add_equivalence (&vr_p->equiv, limit);
1658 else if (cond_code == NE_EXPR)
1660 /* As described above, when LIMIT's range is an anti-range and
1661 this assertion is an inequality (NE_EXPR), then we cannot
1662 derive anything from the anti-range. For instance, if
1663 LIMIT's range was ~[0, 0], the assertion 'VAR != LIMIT' does
1664 not imply that VAR's range is [0, 0]. So, in the case of
1665 anti-ranges, we just assert the inequality using LIMIT and
1666 not its anti-range.
1668 If LIMIT_VR is a range, we can only use it to build a new
1669 anti-range if LIMIT_VR is a single-valued range. For
1670 instance, if LIMIT_VR is [0, 1], the predicate
1671 VAR != [0, 1] does not mean that VAR's range is ~[0, 1].
1672 Rather, it means that for value 0 VAR should be ~[0, 0]
1673 and for value 1, VAR should be ~[1, 1]. We cannot
1674 represent these ranges.
1676 The only situation in which we can build a valid
1677 anti-range is when LIMIT_VR is a single-valued range
1678 (i.e., LIMIT_VR->MIN == LIMIT_VR->MAX). In that case,
1679 build the anti-range ~[LIMIT_VR->MIN, LIMIT_VR->MAX]. */
1680 if (limit_vr
1681 && limit_vr->type == VR_RANGE
1682 && compare_values (limit_vr->min, limit_vr->max) == 0)
1684 min = limit_vr->min;
1685 max = limit_vr->max;
1687 else
1689 /* In any other case, we cannot use LIMIT's range to build a
1690 valid anti-range. */
1691 min = max = limit;
1694 /* If MIN and MAX cover the whole range for their type, then
1695 just use the original LIMIT. */
1696 if (INTEGRAL_TYPE_P (type)
1697 && vrp_val_is_min (min)
1698 && vrp_val_is_max (max))
1699 min = max = limit;
1701 set_value_range (vr_p, VR_ANTI_RANGE, min, max, vr_p->equiv);
1703 else if (cond_code == LE_EXPR || cond_code == LT_EXPR)
1705 min = TYPE_MIN_VALUE (type);
1707 if (limit_vr == NULL || limit_vr->type == VR_ANTI_RANGE)
1708 max = limit;
1709 else
1711 /* If LIMIT_VR is of the form [N1, N2], we need to build the
1712 range [MIN, N2] for LE_EXPR and [MIN, N2 - 1] for
1713 LT_EXPR. */
1714 max = limit_vr->max;
1717 /* If the maximum value forces us to be out of bounds, simply punt.
1718 It would be pointless to try and do anything more since this
1719 all should be optimized away above us. */
1720 if ((cond_code == LT_EXPR
1721 && compare_values (max, min) == 0)
1722 || (CONSTANT_CLASS_P (max) && TREE_OVERFLOW (max)))
1723 set_value_range_to_varying (vr_p);
1724 else
1726 /* For LT_EXPR, we create the range [MIN, MAX - 1]. */
1727 if (cond_code == LT_EXPR)
1729 if (TYPE_PRECISION (TREE_TYPE (max)) == 1
1730 && !TYPE_UNSIGNED (TREE_TYPE (max)))
1731 max = fold_build2 (PLUS_EXPR, TREE_TYPE (max), max,
1732 build_int_cst (TREE_TYPE (max), -1));
1733 else
1734 max = fold_build2 (MINUS_EXPR, TREE_TYPE (max), max,
1735 build_int_cst (TREE_TYPE (max), 1));
1736 if (EXPR_P (max))
1737 TREE_NO_WARNING (max) = 1;
1740 set_value_range (vr_p, VR_RANGE, min, max, vr_p->equiv);
1743 else if (cond_code == GE_EXPR || cond_code == GT_EXPR)
1745 max = TYPE_MAX_VALUE (type);
1747 if (limit_vr == NULL || limit_vr->type == VR_ANTI_RANGE)
1748 min = limit;
1749 else
1751 /* If LIMIT_VR is of the form [N1, N2], we need to build the
1752 range [N1, MAX] for GE_EXPR and [N1 + 1, MAX] for
1753 GT_EXPR. */
1754 min = limit_vr->min;
1757 /* If the minimum value forces us to be out of bounds, simply punt.
1758 It would be pointless to try and do anything more since this
1759 all should be optimized away above us. */
1760 if ((cond_code == GT_EXPR
1761 && compare_values (min, max) == 0)
1762 || (CONSTANT_CLASS_P (min) && TREE_OVERFLOW (min)))
1763 set_value_range_to_varying (vr_p);
1764 else
1766 /* For GT_EXPR, we create the range [MIN + 1, MAX]. */
1767 if (cond_code == GT_EXPR)
1769 if (TYPE_PRECISION (TREE_TYPE (min)) == 1
1770 && !TYPE_UNSIGNED (TREE_TYPE (min)))
1771 min = fold_build2 (MINUS_EXPR, TREE_TYPE (min), min,
1772 build_int_cst (TREE_TYPE (min), -1));
1773 else
1774 min = fold_build2 (PLUS_EXPR, TREE_TYPE (min), min,
1775 build_int_cst (TREE_TYPE (min), 1));
1776 if (EXPR_P (min))
1777 TREE_NO_WARNING (min) = 1;
1780 set_value_range (vr_p, VR_RANGE, min, max, vr_p->equiv);
1783 else
1784 gcc_unreachable ();
1786 /* Finally intersect the new range with what we already know about var. */
1787 vrp_intersect_ranges (vr_p, get_value_range (var));
1791 /* Extract range information from SSA name VAR and store it in VR. If
1792 VAR has an interesting range, use it. Otherwise, create the
1793 range [VAR, VAR] and return it. This is useful in situations where
1794 we may have conditionals testing values of VARYING names. For
1795 instance,
1797 x_3 = y_5;
1798 if (x_3 > y_5)
1801 Even if y_5 is deemed VARYING, we can determine that x_3 > y_5 is
1802 always false. */
1804 static void
1805 extract_range_from_ssa_name (value_range_t *vr, tree var)
1807 value_range_t *var_vr = get_value_range (var);
1809 if (var_vr->type != VR_UNDEFINED && var_vr->type != VR_VARYING)
1810 copy_value_range (vr, var_vr);
1811 else
1812 set_value_range (vr, VR_RANGE, var, var, NULL);
1814 add_equivalence (&vr->equiv, var);
1818 /* Wrapper around int_const_binop. If the operation overflows and we
1819 are not using wrapping arithmetic, then adjust the result to be
1820 -INF or +INF depending on CODE, VAL1 and VAL2. This can return
1821 NULL_TREE if we need to use an overflow infinity representation but
1822 the type does not support it. */
1824 static tree
1825 vrp_int_const_binop (enum tree_code code, tree val1, tree val2)
1827 tree res;
1829 res = int_const_binop (code, val1, val2);
1831 /* If we are using unsigned arithmetic, operate symbolically
1832 on -INF and +INF as int_const_binop only handles signed overflow. */
1833 if (TYPE_UNSIGNED (TREE_TYPE (val1)))
1835 int checkz = compare_values (res, val1);
1836 bool overflow = false;
1838 /* Ensure that res = val1 [+*] val2 >= val1
1839 or that res = val1 - val2 <= val1. */
1840 if ((code == PLUS_EXPR
1841 && !(checkz == 1 || checkz == 0))
1842 || (code == MINUS_EXPR
1843 && !(checkz == 0 || checkz == -1)))
1845 overflow = true;
1847 /* Checking for multiplication overflow is done by dividing the
1848 output of the multiplication by the first input of the
1849 multiplication. If the result of that division operation is
1850 not equal to the second input of the multiplication, then the
1851 multiplication overflowed. */
1852 else if (code == MULT_EXPR && !integer_zerop (val1))
1854 tree tmp = int_const_binop (TRUNC_DIV_EXPR,
1855 res,
1856 val1);
1857 int check = compare_values (tmp, val2);
1859 if (check != 0)
1860 overflow = true;
1863 if (overflow)
1865 res = copy_node (res);
1866 TREE_OVERFLOW (res) = 1;
1870 else if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (val1)))
1871 /* If the singed operation wraps then int_const_binop has done
1872 everything we want. */
1874 else if ((TREE_OVERFLOW (res)
1875 && !TREE_OVERFLOW (val1)
1876 && !TREE_OVERFLOW (val2))
1877 || is_overflow_infinity (val1)
1878 || is_overflow_infinity (val2))
1880 /* If the operation overflowed but neither VAL1 nor VAL2 are
1881 overflown, return -INF or +INF depending on the operation
1882 and the combination of signs of the operands. */
1883 int sgn1 = tree_int_cst_sgn (val1);
1884 int sgn2 = tree_int_cst_sgn (val2);
1886 if (needs_overflow_infinity (TREE_TYPE (res))
1887 && !supports_overflow_infinity (TREE_TYPE (res)))
1888 return NULL_TREE;
1890 /* We have to punt on adding infinities of different signs,
1891 since we can't tell what the sign of the result should be.
1892 Likewise for subtracting infinities of the same sign. */
1893 if (((code == PLUS_EXPR && sgn1 != sgn2)
1894 || (code == MINUS_EXPR && sgn1 == sgn2))
1895 && is_overflow_infinity (val1)
1896 && is_overflow_infinity (val2))
1897 return NULL_TREE;
1899 /* Don't try to handle division or shifting of infinities. */
1900 if ((code == TRUNC_DIV_EXPR
1901 || code == FLOOR_DIV_EXPR
1902 || code == CEIL_DIV_EXPR
1903 || code == EXACT_DIV_EXPR
1904 || code == ROUND_DIV_EXPR
1905 || code == RSHIFT_EXPR)
1906 && (is_overflow_infinity (val1)
1907 || is_overflow_infinity (val2)))
1908 return NULL_TREE;
1910 /* Notice that we only need to handle the restricted set of
1911 operations handled by extract_range_from_binary_expr.
1912 Among them, only multiplication, addition and subtraction
1913 can yield overflow without overflown operands because we
1914 are working with integral types only... except in the
1915 case VAL1 = -INF and VAL2 = -1 which overflows to +INF
1916 for division too. */
1918 /* For multiplication, the sign of the overflow is given
1919 by the comparison of the signs of the operands. */
1920 if ((code == MULT_EXPR && sgn1 == sgn2)
1921 /* For addition, the operands must be of the same sign
1922 to yield an overflow. Its sign is therefore that
1923 of one of the operands, for example the first. For
1924 infinite operands X + -INF is negative, not positive. */
1925 || (code == PLUS_EXPR
1926 && (sgn1 >= 0
1927 ? !is_negative_overflow_infinity (val2)
1928 : is_positive_overflow_infinity (val2)))
1929 /* For subtraction, non-infinite operands must be of
1930 different signs to yield an overflow. Its sign is
1931 therefore that of the first operand or the opposite of
1932 that of the second operand. A first operand of 0 counts
1933 as positive here, for the corner case 0 - (-INF), which
1934 overflows, but must yield +INF. For infinite operands 0
1935 - INF is negative, not positive. */
1936 || (code == MINUS_EXPR
1937 && (sgn1 >= 0
1938 ? !is_positive_overflow_infinity (val2)
1939 : is_negative_overflow_infinity (val2)))
1940 /* We only get in here with positive shift count, so the
1941 overflow direction is the same as the sign of val1.
1942 Actually rshift does not overflow at all, but we only
1943 handle the case of shifting overflowed -INF and +INF. */
1944 || (code == RSHIFT_EXPR
1945 && sgn1 >= 0)
1946 /* For division, the only case is -INF / -1 = +INF. */
1947 || code == TRUNC_DIV_EXPR
1948 || code == FLOOR_DIV_EXPR
1949 || code == CEIL_DIV_EXPR
1950 || code == EXACT_DIV_EXPR
1951 || code == ROUND_DIV_EXPR)
1952 return (needs_overflow_infinity (TREE_TYPE (res))
1953 ? positive_overflow_infinity (TREE_TYPE (res))
1954 : TYPE_MAX_VALUE (TREE_TYPE (res)));
1955 else
1956 return (needs_overflow_infinity (TREE_TYPE (res))
1957 ? negative_overflow_infinity (TREE_TYPE (res))
1958 : TYPE_MIN_VALUE (TREE_TYPE (res)));
1961 return res;
1965 /* For range VR compute two double_int bitmasks. In *MAY_BE_NONZERO
1966 bitmask if some bit is unset, it means for all numbers in the range
1967 the bit is 0, otherwise it might be 0 or 1. In *MUST_BE_NONZERO
1968 bitmask if some bit is set, it means for all numbers in the range
1969 the bit is 1, otherwise it might be 0 or 1. */
1971 static bool
1972 zero_nonzero_bits_from_vr (value_range_t *vr,
1973 double_int *may_be_nonzero,
1974 double_int *must_be_nonzero)
1976 *may_be_nonzero = double_int_minus_one;
1977 *must_be_nonzero = double_int_zero;
1978 if (!range_int_cst_p (vr)
1979 || TREE_OVERFLOW (vr->min)
1980 || TREE_OVERFLOW (vr->max))
1981 return false;
1983 if (range_int_cst_singleton_p (vr))
1985 *may_be_nonzero = tree_to_double_int (vr->min);
1986 *must_be_nonzero = *may_be_nonzero;
1988 else if (tree_int_cst_sgn (vr->min) >= 0
1989 || tree_int_cst_sgn (vr->max) < 0)
1991 double_int dmin = tree_to_double_int (vr->min);
1992 double_int dmax = tree_to_double_int (vr->max);
1993 double_int xor_mask = dmin ^ dmax;
1994 *may_be_nonzero = dmin | dmax;
1995 *must_be_nonzero = dmin & dmax;
1996 if (xor_mask.high != 0)
1998 unsigned HOST_WIDE_INT mask
1999 = ((unsigned HOST_WIDE_INT) 1
2000 << floor_log2 (xor_mask.high)) - 1;
2001 may_be_nonzero->low = ALL_ONES;
2002 may_be_nonzero->high |= mask;
2003 must_be_nonzero->low = 0;
2004 must_be_nonzero->high &= ~mask;
2006 else if (xor_mask.low != 0)
2008 unsigned HOST_WIDE_INT mask
2009 = ((unsigned HOST_WIDE_INT) 1
2010 << floor_log2 (xor_mask.low)) - 1;
2011 may_be_nonzero->low |= mask;
2012 must_be_nonzero->low &= ~mask;
2016 return true;
2019 /* Create two value-ranges in *VR0 and *VR1 from the anti-range *AR
2020 so that *VR0 U *VR1 == *AR. Returns true if that is possible,
2021 false otherwise. If *AR can be represented with a single range
2022 *VR1 will be VR_UNDEFINED. */
2024 static bool
2025 ranges_from_anti_range (value_range_t *ar,
2026 value_range_t *vr0, value_range_t *vr1)
2028 tree type = TREE_TYPE (ar->min);
2030 vr0->type = VR_UNDEFINED;
2031 vr1->type = VR_UNDEFINED;
2033 if (ar->type != VR_ANTI_RANGE
2034 || TREE_CODE (ar->min) != INTEGER_CST
2035 || TREE_CODE (ar->max) != INTEGER_CST
2036 || !vrp_val_min (type)
2037 || !vrp_val_max (type))
2038 return false;
2040 if (!vrp_val_is_min (ar->min))
2042 vr0->type = VR_RANGE;
2043 vr0->min = vrp_val_min (type);
2044 vr0->max
2045 = double_int_to_tree (type,
2046 tree_to_double_int (ar->min) - double_int_one);
2048 if (!vrp_val_is_max (ar->max))
2050 vr1->type = VR_RANGE;
2051 vr1->min
2052 = double_int_to_tree (type,
2053 tree_to_double_int (ar->max) + double_int_one);
2054 vr1->max = vrp_val_max (type);
2056 if (vr0->type == VR_UNDEFINED)
2058 *vr0 = *vr1;
2059 vr1->type = VR_UNDEFINED;
2062 return vr0->type != VR_UNDEFINED;
2065 /* Helper to extract a value-range *VR for a multiplicative operation
2066 *VR0 CODE *VR1. */
2068 static void
2069 extract_range_from_multiplicative_op_1 (value_range_t *vr,
2070 enum tree_code code,
2071 value_range_t *vr0, value_range_t *vr1)
2073 enum value_range_type type;
2074 tree val[4];
2075 size_t i;
2076 tree min, max;
2077 bool sop;
2078 int cmp;
2080 /* Multiplications, divisions and shifts are a bit tricky to handle,
2081 depending on the mix of signs we have in the two ranges, we
2082 need to operate on different values to get the minimum and
2083 maximum values for the new range. One approach is to figure
2084 out all the variations of range combinations and do the
2085 operations.
2087 However, this involves several calls to compare_values and it
2088 is pretty convoluted. It's simpler to do the 4 operations
2089 (MIN0 OP MIN1, MIN0 OP MAX1, MAX0 OP MIN1 and MAX0 OP MAX0 OP
2090 MAX1) and then figure the smallest and largest values to form
2091 the new range. */
2092 gcc_assert (code == MULT_EXPR
2093 || code == TRUNC_DIV_EXPR
2094 || code == FLOOR_DIV_EXPR
2095 || code == CEIL_DIV_EXPR
2096 || code == EXACT_DIV_EXPR
2097 || code == ROUND_DIV_EXPR
2098 || code == RSHIFT_EXPR
2099 || code == LSHIFT_EXPR);
2100 gcc_assert ((vr0->type == VR_RANGE
2101 || (code == MULT_EXPR && vr0->type == VR_ANTI_RANGE))
2102 && vr0->type == vr1->type);
2104 type = vr0->type;
2106 /* Compute the 4 cross operations. */
2107 sop = false;
2108 val[0] = vrp_int_const_binop (code, vr0->min, vr1->min);
2109 if (val[0] == NULL_TREE)
2110 sop = true;
2112 if (vr1->max == vr1->min)
2113 val[1] = NULL_TREE;
2114 else
2116 val[1] = vrp_int_const_binop (code, vr0->min, vr1->max);
2117 if (val[1] == NULL_TREE)
2118 sop = true;
2121 if (vr0->max == vr0->min)
2122 val[2] = NULL_TREE;
2123 else
2125 val[2] = vrp_int_const_binop (code, vr0->max, vr1->min);
2126 if (val[2] == NULL_TREE)
2127 sop = true;
2130 if (vr0->min == vr0->max || vr1->min == vr1->max)
2131 val[3] = NULL_TREE;
2132 else
2134 val[3] = vrp_int_const_binop (code, vr0->max, vr1->max);
2135 if (val[3] == NULL_TREE)
2136 sop = true;
2139 if (sop)
2141 set_value_range_to_varying (vr);
2142 return;
2145 /* Set MIN to the minimum of VAL[i] and MAX to the maximum
2146 of VAL[i]. */
2147 min = val[0];
2148 max = val[0];
2149 for (i = 1; i < 4; i++)
2151 if (!is_gimple_min_invariant (min)
2152 || (TREE_OVERFLOW (min) && !is_overflow_infinity (min))
2153 || !is_gimple_min_invariant (max)
2154 || (TREE_OVERFLOW (max) && !is_overflow_infinity (max)))
2155 break;
2157 if (val[i])
2159 if (!is_gimple_min_invariant (val[i])
2160 || (TREE_OVERFLOW (val[i])
2161 && !is_overflow_infinity (val[i])))
2163 /* If we found an overflowed value, set MIN and MAX
2164 to it so that we set the resulting range to
2165 VARYING. */
2166 min = max = val[i];
2167 break;
2170 if (compare_values (val[i], min) == -1)
2171 min = val[i];
2173 if (compare_values (val[i], max) == 1)
2174 max = val[i];
2178 /* If either MIN or MAX overflowed, then set the resulting range to
2179 VARYING. But we do accept an overflow infinity
2180 representation. */
2181 if (min == NULL_TREE
2182 || !is_gimple_min_invariant (min)
2183 || (TREE_OVERFLOW (min) && !is_overflow_infinity (min))
2184 || max == NULL_TREE
2185 || !is_gimple_min_invariant (max)
2186 || (TREE_OVERFLOW (max) && !is_overflow_infinity (max)))
2188 set_value_range_to_varying (vr);
2189 return;
2192 /* We punt if:
2193 1) [-INF, +INF]
2194 2) [-INF, +-INF(OVF)]
2195 3) [+-INF(OVF), +INF]
2196 4) [+-INF(OVF), +-INF(OVF)]
2197 We learn nothing when we have INF and INF(OVF) on both sides.
2198 Note that we do accept [-INF, -INF] and [+INF, +INF] without
2199 overflow. */
2200 if ((vrp_val_is_min (min) || is_overflow_infinity (min))
2201 && (vrp_val_is_max (max) || is_overflow_infinity (max)))
2203 set_value_range_to_varying (vr);
2204 return;
2207 cmp = compare_values (min, max);
2208 if (cmp == -2 || cmp == 1)
2210 /* If the new range has its limits swapped around (MIN > MAX),
2211 then the operation caused one of them to wrap around, mark
2212 the new range VARYING. */
2213 set_value_range_to_varying (vr);
2215 else
2216 set_value_range (vr, type, min, max, NULL);
2219 /* Some quadruple precision helpers. */
2220 static int
2221 quad_int_cmp (double_int l0, double_int h0,
2222 double_int l1, double_int h1, bool uns)
2224 int c = h0.cmp (h1, uns);
2225 if (c != 0) return c;
2226 return l0.ucmp (l1);
2229 static void
2230 quad_int_pair_sort (double_int *l0, double_int *h0,
2231 double_int *l1, double_int *h1, bool uns)
2233 if (quad_int_cmp (*l0, *h0, *l1, *h1, uns) > 0)
2235 double_int tmp;
2236 tmp = *l0; *l0 = *l1; *l1 = tmp;
2237 tmp = *h0; *h0 = *h1; *h1 = tmp;
2241 /* Extract range information from a binary operation CODE based on
2242 the ranges of each of its operands, *VR0 and *VR1 with resulting
2243 type EXPR_TYPE. The resulting range is stored in *VR. */
2245 static void
2246 extract_range_from_binary_expr_1 (value_range_t *vr,
2247 enum tree_code code, tree expr_type,
2248 value_range_t *vr0_, value_range_t *vr1_)
2250 value_range_t vr0 = *vr0_, vr1 = *vr1_;
2251 value_range_t vrtem0 = VR_INITIALIZER, vrtem1 = VR_INITIALIZER;
2252 enum value_range_type type;
2253 tree min = NULL_TREE, max = NULL_TREE;
2254 int cmp;
2256 if (!INTEGRAL_TYPE_P (expr_type)
2257 && !POINTER_TYPE_P (expr_type))
2259 set_value_range_to_varying (vr);
2260 return;
2263 /* Not all binary expressions can be applied to ranges in a
2264 meaningful way. Handle only arithmetic operations. */
2265 if (code != PLUS_EXPR
2266 && code != MINUS_EXPR
2267 && code != POINTER_PLUS_EXPR
2268 && code != MULT_EXPR
2269 && code != TRUNC_DIV_EXPR
2270 && code != FLOOR_DIV_EXPR
2271 && code != CEIL_DIV_EXPR
2272 && code != EXACT_DIV_EXPR
2273 && code != ROUND_DIV_EXPR
2274 && code != TRUNC_MOD_EXPR
2275 && code != RSHIFT_EXPR
2276 && code != LSHIFT_EXPR
2277 && code != MIN_EXPR
2278 && code != MAX_EXPR
2279 && code != BIT_AND_EXPR
2280 && code != BIT_IOR_EXPR
2281 && code != BIT_XOR_EXPR)
2283 set_value_range_to_varying (vr);
2284 return;
2287 /* If both ranges are UNDEFINED, so is the result. */
2288 if (vr0.type == VR_UNDEFINED && vr1.type == VR_UNDEFINED)
2290 set_value_range_to_undefined (vr);
2291 return;
2293 /* If one of the ranges is UNDEFINED drop it to VARYING for the following
2294 code. At some point we may want to special-case operations that
2295 have UNDEFINED result for all or some value-ranges of the not UNDEFINED
2296 operand. */
2297 else if (vr0.type == VR_UNDEFINED)
2298 set_value_range_to_varying (&vr0);
2299 else if (vr1.type == VR_UNDEFINED)
2300 set_value_range_to_varying (&vr1);
2302 /* Now canonicalize anti-ranges to ranges when they are not symbolic
2303 and express ~[] op X as ([]' op X) U ([]'' op X). */
2304 if (vr0.type == VR_ANTI_RANGE
2305 && ranges_from_anti_range (&vr0, &vrtem0, &vrtem1))
2307 extract_range_from_binary_expr_1 (vr, code, expr_type, &vrtem0, vr1_);
2308 if (vrtem1.type != VR_UNDEFINED)
2310 value_range_t vrres = VR_INITIALIZER;
2311 extract_range_from_binary_expr_1 (&vrres, code, expr_type,
2312 &vrtem1, vr1_);
2313 vrp_meet (vr, &vrres);
2315 return;
2317 /* Likewise for X op ~[]. */
2318 if (vr1.type == VR_ANTI_RANGE
2319 && ranges_from_anti_range (&vr1, &vrtem0, &vrtem1))
2321 extract_range_from_binary_expr_1 (vr, code, expr_type, vr0_, &vrtem0);
2322 if (vrtem1.type != VR_UNDEFINED)
2324 value_range_t vrres = VR_INITIALIZER;
2325 extract_range_from_binary_expr_1 (&vrres, code, expr_type,
2326 vr0_, &vrtem1);
2327 vrp_meet (vr, &vrres);
2329 return;
2332 /* The type of the resulting value range defaults to VR0.TYPE. */
2333 type = vr0.type;
2335 /* Refuse to operate on VARYING ranges, ranges of different kinds
2336 and symbolic ranges. As an exception, we allow BIT_AND_EXPR
2337 because we may be able to derive a useful range even if one of
2338 the operands is VR_VARYING or symbolic range. Similarly for
2339 divisions. TODO, we may be able to derive anti-ranges in
2340 some cases. */
2341 if (code != BIT_AND_EXPR
2342 && code != BIT_IOR_EXPR
2343 && code != TRUNC_DIV_EXPR
2344 && code != FLOOR_DIV_EXPR
2345 && code != CEIL_DIV_EXPR
2346 && code != EXACT_DIV_EXPR
2347 && code != ROUND_DIV_EXPR
2348 && code != TRUNC_MOD_EXPR
2349 && (vr0.type == VR_VARYING
2350 || vr1.type == VR_VARYING
2351 || vr0.type != vr1.type
2352 || symbolic_range_p (&vr0)
2353 || symbolic_range_p (&vr1)))
2355 set_value_range_to_varying (vr);
2356 return;
2359 /* Now evaluate the expression to determine the new range. */
2360 if (POINTER_TYPE_P (expr_type))
2362 if (code == MIN_EXPR || code == MAX_EXPR)
2364 /* For MIN/MAX expressions with pointers, we only care about
2365 nullness, if both are non null, then the result is nonnull.
2366 If both are null, then the result is null. Otherwise they
2367 are varying. */
2368 if (range_is_nonnull (&vr0) && range_is_nonnull (&vr1))
2369 set_value_range_to_nonnull (vr, expr_type);
2370 else if (range_is_null (&vr0) && range_is_null (&vr1))
2371 set_value_range_to_null (vr, expr_type);
2372 else
2373 set_value_range_to_varying (vr);
2375 else if (code == POINTER_PLUS_EXPR)
2377 /* For pointer types, we are really only interested in asserting
2378 whether the expression evaluates to non-NULL. */
2379 if (range_is_nonnull (&vr0) || range_is_nonnull (&vr1))
2380 set_value_range_to_nonnull (vr, expr_type);
2381 else if (range_is_null (&vr0) && range_is_null (&vr1))
2382 set_value_range_to_null (vr, expr_type);
2383 else
2384 set_value_range_to_varying (vr);
2386 else if (code == BIT_AND_EXPR)
2388 /* For pointer types, we are really only interested in asserting
2389 whether the expression evaluates to non-NULL. */
2390 if (range_is_nonnull (&vr0) && range_is_nonnull (&vr1))
2391 set_value_range_to_nonnull (vr, expr_type);
2392 else if (range_is_null (&vr0) || range_is_null (&vr1))
2393 set_value_range_to_null (vr, expr_type);
2394 else
2395 set_value_range_to_varying (vr);
2397 else
2398 set_value_range_to_varying (vr);
2400 return;
2403 /* For integer ranges, apply the operation to each end of the
2404 range and see what we end up with. */
2405 if (code == PLUS_EXPR || code == MINUS_EXPR)
2407 /* If we have a PLUS_EXPR with two VR_RANGE integer constant
2408 ranges compute the precise range for such case if possible. */
2409 if (range_int_cst_p (&vr0)
2410 && range_int_cst_p (&vr1)
2411 /* We need as many bits as the possibly unsigned inputs. */
2412 && TYPE_PRECISION (expr_type) <= HOST_BITS_PER_DOUBLE_INT)
2414 double_int min0 = tree_to_double_int (vr0.min);
2415 double_int max0 = tree_to_double_int (vr0.max);
2416 double_int min1 = tree_to_double_int (vr1.min);
2417 double_int max1 = tree_to_double_int (vr1.max);
2418 bool uns = TYPE_UNSIGNED (expr_type);
2419 double_int type_min
2420 = double_int::min_value (TYPE_PRECISION (expr_type), uns);
2421 double_int type_max
2422 = double_int::max_value (TYPE_PRECISION (expr_type), uns);
2423 double_int dmin, dmax;
2424 int min_ovf = 0;
2425 int max_ovf = 0;
2427 if (code == PLUS_EXPR)
2429 dmin = min0 + min1;
2430 dmax = max0 + max1;
2432 /* Check for overflow in double_int. */
2433 if (min1.cmp (double_int_zero, uns) != dmin.cmp (min0, uns))
2434 min_ovf = min0.cmp (dmin, uns);
2435 if (max1.cmp (double_int_zero, uns) != dmax.cmp (max0, uns))
2436 max_ovf = max0.cmp (dmax, uns);
2438 else /* if (code == MINUS_EXPR) */
2440 dmin = min0 - max1;
2441 dmax = max0 - min1;
2443 if (double_int_zero.cmp (max1, uns) != dmin.cmp (min0, uns))
2444 min_ovf = min0.cmp (max1, uns);
2445 if (double_int_zero.cmp (min1, uns) != dmax.cmp (max0, uns))
2446 max_ovf = max0.cmp (min1, uns);
2449 /* For non-wrapping arithmetic look at possibly smaller
2450 value-ranges of the type. */
2451 if (!TYPE_OVERFLOW_WRAPS (expr_type))
2453 if (vrp_val_min (expr_type))
2454 type_min = tree_to_double_int (vrp_val_min (expr_type));
2455 if (vrp_val_max (expr_type))
2456 type_max = tree_to_double_int (vrp_val_max (expr_type));
2459 /* Check for type overflow. */
2460 if (min_ovf == 0)
2462 if (dmin.cmp (type_min, uns) == -1)
2463 min_ovf = -1;
2464 else if (dmin.cmp (type_max, uns) == 1)
2465 min_ovf = 1;
2467 if (max_ovf == 0)
2469 if (dmax.cmp (type_min, uns) == -1)
2470 max_ovf = -1;
2471 else if (dmax.cmp (type_max, uns) == 1)
2472 max_ovf = 1;
2475 if (TYPE_OVERFLOW_WRAPS (expr_type))
2477 /* If overflow wraps, truncate the values and adjust the
2478 range kind and bounds appropriately. */
2479 double_int tmin
2480 = dmin.ext (TYPE_PRECISION (expr_type), uns);
2481 double_int tmax
2482 = dmax.ext (TYPE_PRECISION (expr_type), uns);
2483 if (min_ovf == max_ovf)
2485 /* No overflow or both overflow or underflow. The
2486 range kind stays VR_RANGE. */
2487 min = double_int_to_tree (expr_type, tmin);
2488 max = double_int_to_tree (expr_type, tmax);
2490 else if (min_ovf == -1
2491 && max_ovf == 1)
2493 /* Underflow and overflow, drop to VR_VARYING. */
2494 set_value_range_to_varying (vr);
2495 return;
2497 else
2499 /* Min underflow or max overflow. The range kind
2500 changes to VR_ANTI_RANGE. */
2501 bool covers = false;
2502 double_int tem = tmin;
2503 gcc_assert ((min_ovf == -1 && max_ovf == 0)
2504 || (max_ovf == 1 && min_ovf == 0));
2505 type = VR_ANTI_RANGE;
2506 tmin = tmax + double_int_one;
2507 if (tmin.cmp (tmax, uns) < 0)
2508 covers = true;
2509 tmax = tem + double_int_minus_one;
2510 if (tmax.cmp (tem, uns) > 0)
2511 covers = true;
2512 /* If the anti-range would cover nothing, drop to varying.
2513 Likewise if the anti-range bounds are outside of the
2514 types values. */
2515 if (covers || tmin.cmp (tmax, uns) > 0)
2517 set_value_range_to_varying (vr);
2518 return;
2520 min = double_int_to_tree (expr_type, tmin);
2521 max = double_int_to_tree (expr_type, tmax);
2524 else
2526 /* If overflow does not wrap, saturate to the types min/max
2527 value. */
2528 if (min_ovf == -1)
2530 if (needs_overflow_infinity (expr_type)
2531 && supports_overflow_infinity (expr_type))
2532 min = negative_overflow_infinity (expr_type);
2533 else
2534 min = double_int_to_tree (expr_type, type_min);
2536 else if (min_ovf == 1)
2538 if (needs_overflow_infinity (expr_type)
2539 && supports_overflow_infinity (expr_type))
2540 min = positive_overflow_infinity (expr_type);
2541 else
2542 min = double_int_to_tree (expr_type, type_max);
2544 else
2545 min = double_int_to_tree (expr_type, dmin);
2547 if (max_ovf == -1)
2549 if (needs_overflow_infinity (expr_type)
2550 && supports_overflow_infinity (expr_type))
2551 max = negative_overflow_infinity (expr_type);
2552 else
2553 max = double_int_to_tree (expr_type, type_min);
2555 else if (max_ovf == 1)
2557 if (needs_overflow_infinity (expr_type)
2558 && supports_overflow_infinity (expr_type))
2559 max = positive_overflow_infinity (expr_type);
2560 else
2561 max = double_int_to_tree (expr_type, type_max);
2563 else
2564 max = double_int_to_tree (expr_type, dmax);
2566 if (needs_overflow_infinity (expr_type)
2567 && supports_overflow_infinity (expr_type))
2569 if (is_negative_overflow_infinity (vr0.min)
2570 || (code == PLUS_EXPR
2571 ? is_negative_overflow_infinity (vr1.min)
2572 : is_positive_overflow_infinity (vr1.max)))
2573 min = negative_overflow_infinity (expr_type);
2574 if (is_positive_overflow_infinity (vr0.max)
2575 || (code == PLUS_EXPR
2576 ? is_positive_overflow_infinity (vr1.max)
2577 : is_negative_overflow_infinity (vr1.min)))
2578 max = positive_overflow_infinity (expr_type);
2581 else
2583 /* For other cases, for example if we have a PLUS_EXPR with two
2584 VR_ANTI_RANGEs, drop to VR_VARYING. It would take more effort
2585 to compute a precise range for such a case.
2586 ??? General even mixed range kind operations can be expressed
2587 by for example transforming ~[3, 5] + [1, 2] to range-only
2588 operations and a union primitive:
2589 [-INF, 2] + [1, 2] U [5, +INF] + [1, 2]
2590 [-INF+1, 4] U [6, +INF(OVF)]
2591 though usually the union is not exactly representable with
2592 a single range or anti-range as the above is
2593 [-INF+1, +INF(OVF)] intersected with ~[5, 5]
2594 but one could use a scheme similar to equivalences for this. */
2595 set_value_range_to_varying (vr);
2596 return;
2599 else if (code == MIN_EXPR
2600 || code == MAX_EXPR)
2602 if (vr0.type == VR_ANTI_RANGE)
2604 /* For MIN_EXPR and MAX_EXPR with two VR_ANTI_RANGEs,
2605 the resulting VR_ANTI_RANGE is the same - intersection
2606 of the two ranges. */
2607 min = vrp_int_const_binop (MAX_EXPR, vr0.min, vr1.min);
2608 max = vrp_int_const_binop (MIN_EXPR, vr0.max, vr1.max);
2610 else
2612 /* For operations that make the resulting range directly
2613 proportional to the original ranges, apply the operation to
2614 the same end of each range. */
2615 min = vrp_int_const_binop (code, vr0.min, vr1.min);
2616 max = vrp_int_const_binop (code, vr0.max, vr1.max);
2619 else if (code == MULT_EXPR)
2621 /* Fancy code so that with unsigned, [-3,-1]*[-3,-1] does not
2622 drop to varying. */
2623 if (range_int_cst_p (&vr0)
2624 && range_int_cst_p (&vr1)
2625 && TYPE_OVERFLOW_WRAPS (expr_type))
2627 double_int min0, max0, min1, max1, sizem1, size;
2628 double_int prod0l, prod0h, prod1l, prod1h,
2629 prod2l, prod2h, prod3l, prod3h;
2630 bool uns0, uns1, uns;
2632 sizem1 = double_int::max_value (TYPE_PRECISION (expr_type), true);
2633 size = sizem1 + double_int_one;
2635 min0 = tree_to_double_int (vr0.min);
2636 max0 = tree_to_double_int (vr0.max);
2637 min1 = tree_to_double_int (vr1.min);
2638 max1 = tree_to_double_int (vr1.max);
2640 uns0 = TYPE_UNSIGNED (expr_type);
2641 uns1 = uns0;
2643 /* Canonicalize the intervals. */
2644 if (TYPE_UNSIGNED (expr_type))
2646 double_int min2 = size - min0;
2647 if (min2.cmp (max0, true) < 0)
2649 min0 = -min2;
2650 max0 -= size;
2651 uns0 = false;
2654 min2 = size - min1;
2655 if (min2.cmp (max1, true) < 0)
2657 min1 = -min2;
2658 max1 -= size;
2659 uns1 = false;
2662 uns = uns0 & uns1;
2664 bool overflow;
2665 prod0l = min0.wide_mul_with_sign (min1, true, &prod0h, &overflow);
2666 if (!uns0 && min0.is_negative ())
2667 prod0h -= min1;
2668 if (!uns1 && min1.is_negative ())
2669 prod0h -= min0;
2671 prod1l = min0.wide_mul_with_sign (max1, true, &prod1h, &overflow);
2672 if (!uns0 && min0.is_negative ())
2673 prod1h -= max1;
2674 if (!uns1 && max1.is_negative ())
2675 prod1h -= min0;
2677 prod2l = max0.wide_mul_with_sign (min1, true, &prod2h, &overflow);
2678 if (!uns0 && max0.is_negative ())
2679 prod2h -= min1;
2680 if (!uns1 && min1.is_negative ())
2681 prod2h -= max0;
2683 prod3l = max0.wide_mul_with_sign (max1, true, &prod3h, &overflow);
2684 if (!uns0 && max0.is_negative ())
2685 prod3h -= max1;
2686 if (!uns1 && max1.is_negative ())
2687 prod3h -= max0;
2689 /* Sort the 4 products. */
2690 quad_int_pair_sort (&prod0l, &prod0h, &prod3l, &prod3h, uns);
2691 quad_int_pair_sort (&prod1l, &prod1h, &prod2l, &prod2h, uns);
2692 quad_int_pair_sort (&prod0l, &prod0h, &prod1l, &prod1h, uns);
2693 quad_int_pair_sort (&prod2l, &prod2h, &prod3l, &prod3h, uns);
2695 /* Max - min. */
2696 if (prod0l.is_zero ())
2698 prod1l = double_int_zero;
2699 prod1h = -prod0h;
2701 else
2703 prod1l = -prod0l;
2704 prod1h = ~prod0h;
2706 prod2l = prod3l + prod1l;
2707 prod2h = prod3h + prod1h;
2708 if (prod2l.ult (prod3l))
2709 prod2h += double_int_one; /* carry */
2711 if (!prod2h.is_zero ()
2712 || prod2l.cmp (sizem1, true) >= 0)
2714 /* the range covers all values. */
2715 set_value_range_to_varying (vr);
2716 return;
2719 /* The following should handle the wrapping and selecting
2720 VR_ANTI_RANGE for us. */
2721 min = double_int_to_tree (expr_type, prod0l);
2722 max = double_int_to_tree (expr_type, prod3l);
2723 set_and_canonicalize_value_range (vr, VR_RANGE, min, max, NULL);
2724 return;
2727 /* If we have an unsigned MULT_EXPR with two VR_ANTI_RANGEs,
2728 drop to VR_VARYING. It would take more effort to compute a
2729 precise range for such a case. For example, if we have
2730 op0 == 65536 and op1 == 65536 with their ranges both being
2731 ~[0,0] on a 32-bit machine, we would have op0 * op1 == 0, so
2732 we cannot claim that the product is in ~[0,0]. Note that we
2733 are guaranteed to have vr0.type == vr1.type at this
2734 point. */
2735 if (vr0.type == VR_ANTI_RANGE
2736 && !TYPE_OVERFLOW_UNDEFINED (expr_type))
2738 set_value_range_to_varying (vr);
2739 return;
2742 extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1);
2743 return;
2745 else if (code == RSHIFT_EXPR
2746 || code == LSHIFT_EXPR)
2748 /* If we have a RSHIFT_EXPR with any shift values outside [0..prec-1],
2749 then drop to VR_VARYING. Outside of this range we get undefined
2750 behavior from the shift operation. We cannot even trust
2751 SHIFT_COUNT_TRUNCATED at this stage, because that applies to rtl
2752 shifts, and the operation at the tree level may be widened. */
2753 if (range_int_cst_p (&vr1)
2754 && compare_tree_int (vr1.min, 0) >= 0
2755 && compare_tree_int (vr1.max, TYPE_PRECISION (expr_type)) == -1)
2757 if (code == RSHIFT_EXPR)
2759 extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1);
2760 return;
2762 /* We can map lshifts by constants to MULT_EXPR handling. */
2763 else if (code == LSHIFT_EXPR
2764 && range_int_cst_singleton_p (&vr1))
2766 bool saved_flag_wrapv;
2767 value_range_t vr1p = VR_INITIALIZER;
2768 vr1p.type = VR_RANGE;
2769 vr1p.min
2770 = double_int_to_tree (expr_type,
2771 double_int_one
2772 .llshift (TREE_INT_CST_LOW (vr1.min),
2773 TYPE_PRECISION (expr_type)));
2774 vr1p.max = vr1p.min;
2775 /* We have to use a wrapping multiply though as signed overflow
2776 on lshifts is implementation defined in C89. */
2777 saved_flag_wrapv = flag_wrapv;
2778 flag_wrapv = 1;
2779 extract_range_from_binary_expr_1 (vr, MULT_EXPR, expr_type,
2780 &vr0, &vr1p);
2781 flag_wrapv = saved_flag_wrapv;
2782 return;
2784 else if (code == LSHIFT_EXPR
2785 && range_int_cst_p (&vr0))
2787 int prec = TYPE_PRECISION (expr_type);
2788 int overflow_pos = prec;
2789 int bound_shift;
2790 double_int bound, complement, low_bound, high_bound;
2791 bool uns = TYPE_UNSIGNED (expr_type);
2792 bool in_bounds = false;
2794 if (!uns)
2795 overflow_pos -= 1;
2797 bound_shift = overflow_pos - TREE_INT_CST_LOW (vr1.max);
2798 /* If bound_shift == HOST_BITS_PER_DOUBLE_INT, the llshift can
2799 overflow. However, for that to happen, vr1.max needs to be
2800 zero, which means vr1 is a singleton range of zero, which
2801 means it should be handled by the previous LSHIFT_EXPR
2802 if-clause. */
2803 bound = double_int_one.llshift (bound_shift, prec);
2804 complement = ~(bound - double_int_one);
2806 if (uns)
2808 low_bound = bound;
2809 high_bound = complement.zext (prec);
2810 if (tree_to_double_int (vr0.max).ult (low_bound))
2812 /* [5, 6] << [1, 2] == [10, 24]. */
2813 /* We're shifting out only zeroes, the value increases
2814 monotonically. */
2815 in_bounds = true;
2817 else if (high_bound.ult (tree_to_double_int (vr0.min)))
2819 /* [0xffffff00, 0xffffffff] << [1, 2]
2820 == [0xfffffc00, 0xfffffffe]. */
2821 /* We're shifting out only ones, the value decreases
2822 monotonically. */
2823 in_bounds = true;
2826 else
2828 /* [-1, 1] << [1, 2] == [-4, 4]. */
2829 low_bound = complement.sext (prec);
2830 high_bound = bound;
2831 if (tree_to_double_int (vr0.max).slt (high_bound)
2832 && low_bound.slt (tree_to_double_int (vr0.min)))
2834 /* For non-negative numbers, we're shifting out only
2835 zeroes, the value increases monotonically.
2836 For negative numbers, we're shifting out only ones, the
2837 value decreases monotomically. */
2838 in_bounds = true;
2842 if (in_bounds)
2844 extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1);
2845 return;
2849 set_value_range_to_varying (vr);
2850 return;
2852 else if (code == TRUNC_DIV_EXPR
2853 || code == FLOOR_DIV_EXPR
2854 || code == CEIL_DIV_EXPR
2855 || code == EXACT_DIV_EXPR
2856 || code == ROUND_DIV_EXPR)
2858 if (vr0.type != VR_RANGE || symbolic_range_p (&vr0))
2860 /* For division, if op1 has VR_RANGE but op0 does not, something
2861 can be deduced just from that range. Say [min, max] / [4, max]
2862 gives [min / 4, max / 4] range. */
2863 if (vr1.type == VR_RANGE
2864 && !symbolic_range_p (&vr1)
2865 && range_includes_zero_p (vr1.min, vr1.max) == 0)
2867 vr0.type = type = VR_RANGE;
2868 vr0.min = vrp_val_min (expr_type);
2869 vr0.max = vrp_val_max (expr_type);
2871 else
2873 set_value_range_to_varying (vr);
2874 return;
2878 /* For divisions, if flag_non_call_exceptions is true, we must
2879 not eliminate a division by zero. */
2880 if (cfun->can_throw_non_call_exceptions
2881 && (vr1.type != VR_RANGE
2882 || range_includes_zero_p (vr1.min, vr1.max) != 0))
2884 set_value_range_to_varying (vr);
2885 return;
2888 /* For divisions, if op0 is VR_RANGE, we can deduce a range
2889 even if op1 is VR_VARYING, VR_ANTI_RANGE, symbolic or can
2890 include 0. */
2891 if (vr0.type == VR_RANGE
2892 && (vr1.type != VR_RANGE
2893 || range_includes_zero_p (vr1.min, vr1.max) != 0))
2895 tree zero = build_int_cst (TREE_TYPE (vr0.min), 0);
2896 int cmp;
2898 min = NULL_TREE;
2899 max = NULL_TREE;
2900 if (TYPE_UNSIGNED (expr_type)
2901 || value_range_nonnegative_p (&vr1))
2903 /* For unsigned division or when divisor is known
2904 to be non-negative, the range has to cover
2905 all numbers from 0 to max for positive max
2906 and all numbers from min to 0 for negative min. */
2907 cmp = compare_values (vr0.max, zero);
2908 if (cmp == -1)
2909 max = zero;
2910 else if (cmp == 0 || cmp == 1)
2911 max = vr0.max;
2912 else
2913 type = VR_VARYING;
2914 cmp = compare_values (vr0.min, zero);
2915 if (cmp == 1)
2916 min = zero;
2917 else if (cmp == 0 || cmp == -1)
2918 min = vr0.min;
2919 else
2920 type = VR_VARYING;
2922 else
2924 /* Otherwise the range is -max .. max or min .. -min
2925 depending on which bound is bigger in absolute value,
2926 as the division can change the sign. */
2927 abs_extent_range (vr, vr0.min, vr0.max);
2928 return;
2930 if (type == VR_VARYING)
2932 set_value_range_to_varying (vr);
2933 return;
2936 else
2938 extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1);
2939 return;
2942 else if (code == TRUNC_MOD_EXPR)
2944 if (vr1.type != VR_RANGE
2945 || range_includes_zero_p (vr1.min, vr1.max) != 0
2946 || vrp_val_is_min (vr1.min))
2948 set_value_range_to_varying (vr);
2949 return;
2951 type = VR_RANGE;
2952 /* Compute MAX <|vr1.min|, |vr1.max|> - 1. */
2953 max = fold_unary_to_constant (ABS_EXPR, expr_type, vr1.min);
2954 if (tree_int_cst_lt (max, vr1.max))
2955 max = vr1.max;
2956 max = int_const_binop (MINUS_EXPR, max, integer_one_node);
2957 /* If the dividend is non-negative the modulus will be
2958 non-negative as well. */
2959 if (TYPE_UNSIGNED (expr_type)
2960 || value_range_nonnegative_p (&vr0))
2961 min = build_int_cst (TREE_TYPE (max), 0);
2962 else
2963 min = fold_unary_to_constant (NEGATE_EXPR, expr_type, max);
2965 else if (code == BIT_AND_EXPR || code == BIT_IOR_EXPR || code == BIT_XOR_EXPR)
2967 bool int_cst_range0, int_cst_range1;
2968 double_int may_be_nonzero0, may_be_nonzero1;
2969 double_int must_be_nonzero0, must_be_nonzero1;
2971 int_cst_range0 = zero_nonzero_bits_from_vr (&vr0, &may_be_nonzero0,
2972 &must_be_nonzero0);
2973 int_cst_range1 = zero_nonzero_bits_from_vr (&vr1, &may_be_nonzero1,
2974 &must_be_nonzero1);
2976 type = VR_RANGE;
2977 if (code == BIT_AND_EXPR)
2979 double_int dmax;
2980 min = double_int_to_tree (expr_type,
2981 must_be_nonzero0 & must_be_nonzero1);
2982 dmax = may_be_nonzero0 & may_be_nonzero1;
2983 /* If both input ranges contain only negative values we can
2984 truncate the result range maximum to the minimum of the
2985 input range maxima. */
2986 if (int_cst_range0 && int_cst_range1
2987 && tree_int_cst_sgn (vr0.max) < 0
2988 && tree_int_cst_sgn (vr1.max) < 0)
2990 dmax = dmax.min (tree_to_double_int (vr0.max),
2991 TYPE_UNSIGNED (expr_type));
2992 dmax = dmax.min (tree_to_double_int (vr1.max),
2993 TYPE_UNSIGNED (expr_type));
2995 /* If either input range contains only non-negative values
2996 we can truncate the result range maximum to the respective
2997 maximum of the input range. */
2998 if (int_cst_range0 && tree_int_cst_sgn (vr0.min) >= 0)
2999 dmax = dmax.min (tree_to_double_int (vr0.max),
3000 TYPE_UNSIGNED (expr_type));
3001 if (int_cst_range1 && tree_int_cst_sgn (vr1.min) >= 0)
3002 dmax = dmax.min (tree_to_double_int (vr1.max),
3003 TYPE_UNSIGNED (expr_type));
3004 max = double_int_to_tree (expr_type, dmax);
3006 else if (code == BIT_IOR_EXPR)
3008 double_int dmin;
3009 max = double_int_to_tree (expr_type,
3010 may_be_nonzero0 | may_be_nonzero1);
3011 dmin = must_be_nonzero0 | must_be_nonzero1;
3012 /* If the input ranges contain only positive values we can
3013 truncate the minimum of the result range to the maximum
3014 of the input range minima. */
3015 if (int_cst_range0 && int_cst_range1
3016 && tree_int_cst_sgn (vr0.min) >= 0
3017 && tree_int_cst_sgn (vr1.min) >= 0)
3019 dmin = dmin.max (tree_to_double_int (vr0.min),
3020 TYPE_UNSIGNED (expr_type));
3021 dmin = dmin.max (tree_to_double_int (vr1.min),
3022 TYPE_UNSIGNED (expr_type));
3024 /* If either input range contains only negative values
3025 we can truncate the minimum of the result range to the
3026 respective minimum range. */
3027 if (int_cst_range0 && tree_int_cst_sgn (vr0.max) < 0)
3028 dmin = dmin.max (tree_to_double_int (vr0.min),
3029 TYPE_UNSIGNED (expr_type));
3030 if (int_cst_range1 && tree_int_cst_sgn (vr1.max) < 0)
3031 dmin = dmin.max (tree_to_double_int (vr1.min),
3032 TYPE_UNSIGNED (expr_type));
3033 min = double_int_to_tree (expr_type, dmin);
3035 else if (code == BIT_XOR_EXPR)
3037 double_int result_zero_bits, result_one_bits;
3038 result_zero_bits = (must_be_nonzero0 & must_be_nonzero1)
3039 | ~(may_be_nonzero0 | may_be_nonzero1);
3040 result_one_bits = must_be_nonzero0.and_not (may_be_nonzero1)
3041 | must_be_nonzero1.and_not (may_be_nonzero0);
3042 max = double_int_to_tree (expr_type, ~result_zero_bits);
3043 min = double_int_to_tree (expr_type, result_one_bits);
3044 /* If the range has all positive or all negative values the
3045 result is better than VARYING. */
3046 if (tree_int_cst_sgn (min) < 0
3047 || tree_int_cst_sgn (max) >= 0)
3049 else
3050 max = min = NULL_TREE;
3053 else
3054 gcc_unreachable ();
3056 /* If either MIN or MAX overflowed, then set the resulting range to
3057 VARYING. But we do accept an overflow infinity
3058 representation. */
3059 if (min == NULL_TREE
3060 || !is_gimple_min_invariant (min)
3061 || (TREE_OVERFLOW (min) && !is_overflow_infinity (min))
3062 || max == NULL_TREE
3063 || !is_gimple_min_invariant (max)
3064 || (TREE_OVERFLOW (max) && !is_overflow_infinity (max)))
3066 set_value_range_to_varying (vr);
3067 return;
3070 /* We punt if:
3071 1) [-INF, +INF]
3072 2) [-INF, +-INF(OVF)]
3073 3) [+-INF(OVF), +INF]
3074 4) [+-INF(OVF), +-INF(OVF)]
3075 We learn nothing when we have INF and INF(OVF) on both sides.
3076 Note that we do accept [-INF, -INF] and [+INF, +INF] without
3077 overflow. */
3078 if ((vrp_val_is_min (min) || is_overflow_infinity (min))
3079 && (vrp_val_is_max (max) || is_overflow_infinity (max)))
3081 set_value_range_to_varying (vr);
3082 return;
3085 cmp = compare_values (min, max);
3086 if (cmp == -2 || cmp == 1)
3088 /* If the new range has its limits swapped around (MIN > MAX),
3089 then the operation caused one of them to wrap around, mark
3090 the new range VARYING. */
3091 set_value_range_to_varying (vr);
3093 else
3094 set_value_range (vr, type, min, max, NULL);
3097 /* Extract range information from a binary expression OP0 CODE OP1 based on
3098 the ranges of each of its operands with resulting type EXPR_TYPE.
3099 The resulting range is stored in *VR. */
3101 static void
3102 extract_range_from_binary_expr (value_range_t *vr,
3103 enum tree_code code,
3104 tree expr_type, tree op0, tree op1)
3106 value_range_t vr0 = VR_INITIALIZER;
3107 value_range_t vr1 = VR_INITIALIZER;
3109 /* Get value ranges for each operand. For constant operands, create
3110 a new value range with the operand to simplify processing. */
3111 if (TREE_CODE (op0) == SSA_NAME)
3112 vr0 = *(get_value_range (op0));
3113 else if (is_gimple_min_invariant (op0))
3114 set_value_range_to_value (&vr0, op0, NULL);
3115 else
3116 set_value_range_to_varying (&vr0);
3118 if (TREE_CODE (op1) == SSA_NAME)
3119 vr1 = *(get_value_range (op1));
3120 else if (is_gimple_min_invariant (op1))
3121 set_value_range_to_value (&vr1, op1, NULL);
3122 else
3123 set_value_range_to_varying (&vr1);
3125 extract_range_from_binary_expr_1 (vr, code, expr_type, &vr0, &vr1);
3128 /* Extract range information from a unary operation CODE based on
3129 the range of its operand *VR0 with type OP0_TYPE with resulting type TYPE.
3130 The The resulting range is stored in *VR. */
3132 static void
3133 extract_range_from_unary_expr_1 (value_range_t *vr,
3134 enum tree_code code, tree type,
3135 value_range_t *vr0_, tree op0_type)
3137 value_range_t vr0 = *vr0_, vrtem0 = VR_INITIALIZER, vrtem1 = VR_INITIALIZER;
3139 /* VRP only operates on integral and pointer types. */
3140 if (!(INTEGRAL_TYPE_P (op0_type)
3141 || POINTER_TYPE_P (op0_type))
3142 || !(INTEGRAL_TYPE_P (type)
3143 || POINTER_TYPE_P (type)))
3145 set_value_range_to_varying (vr);
3146 return;
3149 /* If VR0 is UNDEFINED, so is the result. */
3150 if (vr0.type == VR_UNDEFINED)
3152 set_value_range_to_undefined (vr);
3153 return;
3156 /* Handle operations that we express in terms of others. */
3157 if (code == PAREN_EXPR)
3159 /* PAREN_EXPR is a simple copy. */
3160 copy_value_range (vr, &vr0);
3161 return;
3163 else if (code == NEGATE_EXPR)
3165 /* -X is simply 0 - X, so re-use existing code that also handles
3166 anti-ranges fine. */
3167 value_range_t zero = VR_INITIALIZER;
3168 set_value_range_to_value (&zero, build_int_cst (type, 0), NULL);
3169 extract_range_from_binary_expr_1 (vr, MINUS_EXPR, type, &zero, &vr0);
3170 return;
3172 else if (code == BIT_NOT_EXPR)
3174 /* ~X is simply -1 - X, so re-use existing code that also handles
3175 anti-ranges fine. */
3176 value_range_t minusone = VR_INITIALIZER;
3177 set_value_range_to_value (&minusone, build_int_cst (type, -1), NULL);
3178 extract_range_from_binary_expr_1 (vr, MINUS_EXPR,
3179 type, &minusone, &vr0);
3180 return;
3183 /* Now canonicalize anti-ranges to ranges when they are not symbolic
3184 and express op ~[] as (op []') U (op []''). */
3185 if (vr0.type == VR_ANTI_RANGE
3186 && ranges_from_anti_range (&vr0, &vrtem0, &vrtem1))
3188 extract_range_from_unary_expr_1 (vr, code, type, &vrtem0, op0_type);
3189 if (vrtem1.type != VR_UNDEFINED)
3191 value_range_t vrres = VR_INITIALIZER;
3192 extract_range_from_unary_expr_1 (&vrres, code, type,
3193 &vrtem1, op0_type);
3194 vrp_meet (vr, &vrres);
3196 return;
3199 if (CONVERT_EXPR_CODE_P (code))
3201 tree inner_type = op0_type;
3202 tree outer_type = type;
3204 /* If the expression evaluates to a pointer, we are only interested in
3205 determining if it evaluates to NULL [0, 0] or non-NULL (~[0, 0]). */
3206 if (POINTER_TYPE_P (type))
3208 if (range_is_nonnull (&vr0))
3209 set_value_range_to_nonnull (vr, type);
3210 else if (range_is_null (&vr0))
3211 set_value_range_to_null (vr, type);
3212 else
3213 set_value_range_to_varying (vr);
3214 return;
3217 /* If VR0 is varying and we increase the type precision, assume
3218 a full range for the following transformation. */
3219 if (vr0.type == VR_VARYING
3220 && INTEGRAL_TYPE_P (inner_type)
3221 && TYPE_PRECISION (inner_type) < TYPE_PRECISION (outer_type))
3223 vr0.type = VR_RANGE;
3224 vr0.min = TYPE_MIN_VALUE (inner_type);
3225 vr0.max = TYPE_MAX_VALUE (inner_type);
3228 /* If VR0 is a constant range or anti-range and the conversion is
3229 not truncating we can convert the min and max values and
3230 canonicalize the resulting range. Otherwise we can do the
3231 conversion if the size of the range is less than what the
3232 precision of the target type can represent and the range is
3233 not an anti-range. */
3234 if ((vr0.type == VR_RANGE
3235 || vr0.type == VR_ANTI_RANGE)
3236 && TREE_CODE (vr0.min) == INTEGER_CST
3237 && TREE_CODE (vr0.max) == INTEGER_CST
3238 && (!is_overflow_infinity (vr0.min)
3239 || (vr0.type == VR_RANGE
3240 && TYPE_PRECISION (outer_type) > TYPE_PRECISION (inner_type)
3241 && needs_overflow_infinity (outer_type)
3242 && supports_overflow_infinity (outer_type)))
3243 && (!is_overflow_infinity (vr0.max)
3244 || (vr0.type == VR_RANGE
3245 && TYPE_PRECISION (outer_type) > TYPE_PRECISION (inner_type)
3246 && needs_overflow_infinity (outer_type)
3247 && supports_overflow_infinity (outer_type)))
3248 && (TYPE_PRECISION (outer_type) >= TYPE_PRECISION (inner_type)
3249 || (vr0.type == VR_RANGE
3250 && integer_zerop (int_const_binop (RSHIFT_EXPR,
3251 int_const_binop (MINUS_EXPR, vr0.max, vr0.min),
3252 size_int (TYPE_PRECISION (outer_type)))))))
3254 tree new_min, new_max;
3255 if (is_overflow_infinity (vr0.min))
3256 new_min = negative_overflow_infinity (outer_type);
3257 else
3258 new_min = force_fit_type_double (outer_type,
3259 tree_to_double_int (vr0.min),
3260 0, false);
3261 if (is_overflow_infinity (vr0.max))
3262 new_max = positive_overflow_infinity (outer_type);
3263 else
3264 new_max = force_fit_type_double (outer_type,
3265 tree_to_double_int (vr0.max),
3266 0, false);
3267 set_and_canonicalize_value_range (vr, vr0.type,
3268 new_min, new_max, NULL);
3269 return;
3272 set_value_range_to_varying (vr);
3273 return;
3275 else if (code == ABS_EXPR)
3277 tree min, max;
3278 int cmp;
3280 /* Pass through vr0 in the easy cases. */
3281 if (TYPE_UNSIGNED (type)
3282 || value_range_nonnegative_p (&vr0))
3284 copy_value_range (vr, &vr0);
3285 return;
3288 /* For the remaining varying or symbolic ranges we can't do anything
3289 useful. */
3290 if (vr0.type == VR_VARYING
3291 || symbolic_range_p (&vr0))
3293 set_value_range_to_varying (vr);
3294 return;
3297 /* -TYPE_MIN_VALUE = TYPE_MIN_VALUE with flag_wrapv so we can't get a
3298 useful range. */
3299 if (!TYPE_OVERFLOW_UNDEFINED (type)
3300 && ((vr0.type == VR_RANGE
3301 && vrp_val_is_min (vr0.min))
3302 || (vr0.type == VR_ANTI_RANGE
3303 && !vrp_val_is_min (vr0.min))))
3305 set_value_range_to_varying (vr);
3306 return;
3309 /* ABS_EXPR may flip the range around, if the original range
3310 included negative values. */
3311 if (is_overflow_infinity (vr0.min))
3312 min = positive_overflow_infinity (type);
3313 else if (!vrp_val_is_min (vr0.min))
3314 min = fold_unary_to_constant (code, type, vr0.min);
3315 else if (!needs_overflow_infinity (type))
3316 min = TYPE_MAX_VALUE (type);
3317 else if (supports_overflow_infinity (type))
3318 min = positive_overflow_infinity (type);
3319 else
3321 set_value_range_to_varying (vr);
3322 return;
3325 if (is_overflow_infinity (vr0.max))
3326 max = positive_overflow_infinity (type);
3327 else if (!vrp_val_is_min (vr0.max))
3328 max = fold_unary_to_constant (code, type, vr0.max);
3329 else if (!needs_overflow_infinity (type))
3330 max = TYPE_MAX_VALUE (type);
3331 else if (supports_overflow_infinity (type)
3332 /* We shouldn't generate [+INF, +INF] as set_value_range
3333 doesn't like this and ICEs. */
3334 && !is_positive_overflow_infinity (min))
3335 max = positive_overflow_infinity (type);
3336 else
3338 set_value_range_to_varying (vr);
3339 return;
3342 cmp = compare_values (min, max);
3344 /* If a VR_ANTI_RANGEs contains zero, then we have
3345 ~[-INF, min(MIN, MAX)]. */
3346 if (vr0.type == VR_ANTI_RANGE)
3348 if (range_includes_zero_p (vr0.min, vr0.max) == 1)
3350 /* Take the lower of the two values. */
3351 if (cmp != 1)
3352 max = min;
3354 /* Create ~[-INF, min (abs(MIN), abs(MAX))]
3355 or ~[-INF + 1, min (abs(MIN), abs(MAX))] when
3356 flag_wrapv is set and the original anti-range doesn't include
3357 TYPE_MIN_VALUE, remember -TYPE_MIN_VALUE = TYPE_MIN_VALUE. */
3358 if (TYPE_OVERFLOW_WRAPS (type))
3360 tree type_min_value = TYPE_MIN_VALUE (type);
3362 min = (vr0.min != type_min_value
3363 ? int_const_binop (PLUS_EXPR, type_min_value,
3364 integer_one_node)
3365 : type_min_value);
3367 else
3369 if (overflow_infinity_range_p (&vr0))
3370 min = negative_overflow_infinity (type);
3371 else
3372 min = TYPE_MIN_VALUE (type);
3375 else
3377 /* All else has failed, so create the range [0, INF], even for
3378 flag_wrapv since TYPE_MIN_VALUE is in the original
3379 anti-range. */
3380 vr0.type = VR_RANGE;
3381 min = build_int_cst (type, 0);
3382 if (needs_overflow_infinity (type))
3384 if (supports_overflow_infinity (type))
3385 max = positive_overflow_infinity (type);
3386 else
3388 set_value_range_to_varying (vr);
3389 return;
3392 else
3393 max = TYPE_MAX_VALUE (type);
3397 /* If the range contains zero then we know that the minimum value in the
3398 range will be zero. */
3399 else if (range_includes_zero_p (vr0.min, vr0.max) == 1)
3401 if (cmp == 1)
3402 max = min;
3403 min = build_int_cst (type, 0);
3405 else
3407 /* If the range was reversed, swap MIN and MAX. */
3408 if (cmp == 1)
3410 tree t = min;
3411 min = max;
3412 max = t;
3416 cmp = compare_values (min, max);
3417 if (cmp == -2 || cmp == 1)
3419 /* If the new range has its limits swapped around (MIN > MAX),
3420 then the operation caused one of them to wrap around, mark
3421 the new range VARYING. */
3422 set_value_range_to_varying (vr);
3424 else
3425 set_value_range (vr, vr0.type, min, max, NULL);
3426 return;
3429 /* For unhandled operations fall back to varying. */
3430 set_value_range_to_varying (vr);
3431 return;
3435 /* Extract range information from a unary expression CODE OP0 based on
3436 the range of its operand with resulting type TYPE.
3437 The resulting range is stored in *VR. */
3439 static void
3440 extract_range_from_unary_expr (value_range_t *vr, enum tree_code code,
3441 tree type, tree op0)
3443 value_range_t vr0 = VR_INITIALIZER;
3445 /* Get value ranges for the operand. For constant operands, create
3446 a new value range with the operand to simplify processing. */
3447 if (TREE_CODE (op0) == SSA_NAME)
3448 vr0 = *(get_value_range (op0));
3449 else if (is_gimple_min_invariant (op0))
3450 set_value_range_to_value (&vr0, op0, NULL);
3451 else
3452 set_value_range_to_varying (&vr0);
3454 extract_range_from_unary_expr_1 (vr, code, type, &vr0, TREE_TYPE (op0));
3458 /* Extract range information from a conditional expression STMT based on
3459 the ranges of each of its operands and the expression code. */
3461 static void
3462 extract_range_from_cond_expr (value_range_t *vr, gimple stmt)
3464 tree op0, op1;
3465 value_range_t vr0 = VR_INITIALIZER;
3466 value_range_t vr1 = VR_INITIALIZER;
3468 /* Get value ranges for each operand. For constant operands, create
3469 a new value range with the operand to simplify processing. */
3470 op0 = gimple_assign_rhs2 (stmt);
3471 if (TREE_CODE (op0) == SSA_NAME)
3472 vr0 = *(get_value_range (op0));
3473 else if (is_gimple_min_invariant (op0))
3474 set_value_range_to_value (&vr0, op0, NULL);
3475 else
3476 set_value_range_to_varying (&vr0);
3478 op1 = gimple_assign_rhs3 (stmt);
3479 if (TREE_CODE (op1) == SSA_NAME)
3480 vr1 = *(get_value_range (op1));
3481 else if (is_gimple_min_invariant (op1))
3482 set_value_range_to_value (&vr1, op1, NULL);
3483 else
3484 set_value_range_to_varying (&vr1);
3486 /* The resulting value range is the union of the operand ranges */
3487 copy_value_range (vr, &vr0);
3488 vrp_meet (vr, &vr1);
3492 /* Extract range information from a comparison expression EXPR based
3493 on the range of its operand and the expression code. */
3495 static void
3496 extract_range_from_comparison (value_range_t *vr, enum tree_code code,
3497 tree type, tree op0, tree op1)
3499 bool sop = false;
3500 tree val;
3502 val = vrp_evaluate_conditional_warnv_with_ops (code, op0, op1, false, &sop,
3503 NULL);
3505 /* A disadvantage of using a special infinity as an overflow
3506 representation is that we lose the ability to record overflow
3507 when we don't have an infinity. So we have to ignore a result
3508 which relies on overflow. */
3510 if (val && !is_overflow_infinity (val) && !sop)
3512 /* Since this expression was found on the RHS of an assignment,
3513 its type may be different from _Bool. Convert VAL to EXPR's
3514 type. */
3515 val = fold_convert (type, val);
3516 if (is_gimple_min_invariant (val))
3517 set_value_range_to_value (vr, val, vr->equiv);
3518 else
3519 set_value_range (vr, VR_RANGE, val, val, vr->equiv);
3521 else
3522 /* The result of a comparison is always true or false. */
3523 set_value_range_to_truthvalue (vr, type);
3526 /* Try to derive a nonnegative or nonzero range out of STMT relying
3527 primarily on generic routines in fold in conjunction with range data.
3528 Store the result in *VR */
3530 static void
3531 extract_range_basic (value_range_t *vr, gimple stmt)
3533 bool sop = false;
3534 tree type = gimple_expr_type (stmt);
3536 if (INTEGRAL_TYPE_P (type)
3537 && gimple_stmt_nonnegative_warnv_p (stmt, &sop))
3538 set_value_range_to_nonnegative (vr, type,
3539 sop || stmt_overflow_infinity (stmt));
3540 else if (vrp_stmt_computes_nonzero (stmt, &sop)
3541 && !sop)
3542 set_value_range_to_nonnull (vr, type);
3543 else
3544 set_value_range_to_varying (vr);
3548 /* Try to compute a useful range out of assignment STMT and store it
3549 in *VR. */
3551 static void
3552 extract_range_from_assignment (value_range_t *vr, gimple stmt)
3554 enum tree_code code = gimple_assign_rhs_code (stmt);
3556 if (code == ASSERT_EXPR)
3557 extract_range_from_assert (vr, gimple_assign_rhs1 (stmt));
3558 else if (code == SSA_NAME)
3559 extract_range_from_ssa_name (vr, gimple_assign_rhs1 (stmt));
3560 else if (TREE_CODE_CLASS (code) == tcc_binary)
3561 extract_range_from_binary_expr (vr, gimple_assign_rhs_code (stmt),
3562 gimple_expr_type (stmt),
3563 gimple_assign_rhs1 (stmt),
3564 gimple_assign_rhs2 (stmt));
3565 else if (TREE_CODE_CLASS (code) == tcc_unary)
3566 extract_range_from_unary_expr (vr, gimple_assign_rhs_code (stmt),
3567 gimple_expr_type (stmt),
3568 gimple_assign_rhs1 (stmt));
3569 else if (code == COND_EXPR)
3570 extract_range_from_cond_expr (vr, stmt);
3571 else if (TREE_CODE_CLASS (code) == tcc_comparison)
3572 extract_range_from_comparison (vr, gimple_assign_rhs_code (stmt),
3573 gimple_expr_type (stmt),
3574 gimple_assign_rhs1 (stmt),
3575 gimple_assign_rhs2 (stmt));
3576 else if (get_gimple_rhs_class (code) == GIMPLE_SINGLE_RHS
3577 && is_gimple_min_invariant (gimple_assign_rhs1 (stmt)))
3578 set_value_range_to_value (vr, gimple_assign_rhs1 (stmt), NULL);
3579 else
3580 set_value_range_to_varying (vr);
3582 if (vr->type == VR_VARYING)
3583 extract_range_basic (vr, stmt);
3586 /* Given a range VR, a LOOP and a variable VAR, determine whether it
3587 would be profitable to adjust VR using scalar evolution information
3588 for VAR. If so, update VR with the new limits. */
3590 static void
3591 adjust_range_with_scev (value_range_t *vr, struct loop *loop,
3592 gimple stmt, tree var)
3594 tree init, step, chrec, tmin, tmax, min, max, type, tem;
3595 enum ev_direction dir;
3597 /* TODO. Don't adjust anti-ranges. An anti-range may provide
3598 better opportunities than a regular range, but I'm not sure. */
3599 if (vr->type == VR_ANTI_RANGE)
3600 return;
3602 chrec = instantiate_parameters (loop, analyze_scalar_evolution (loop, var));
3604 /* Like in PR19590, scev can return a constant function. */
3605 if (is_gimple_min_invariant (chrec))
3607 set_value_range_to_value (vr, chrec, vr->equiv);
3608 return;
3611 if (TREE_CODE (chrec) != POLYNOMIAL_CHREC)
3612 return;
3614 init = initial_condition_in_loop_num (chrec, loop->num);
3615 tem = op_with_constant_singleton_value_range (init);
3616 if (tem)
3617 init = tem;
3618 step = evolution_part_in_loop_num (chrec, loop->num);
3619 tem = op_with_constant_singleton_value_range (step);
3620 if (tem)
3621 step = tem;
3623 /* If STEP is symbolic, we can't know whether INIT will be the
3624 minimum or maximum value in the range. Also, unless INIT is
3625 a simple expression, compare_values and possibly other functions
3626 in tree-vrp won't be able to handle it. */
3627 if (step == NULL_TREE
3628 || !is_gimple_min_invariant (step)
3629 || !valid_value_p (init))
3630 return;
3632 dir = scev_direction (chrec);
3633 if (/* Do not adjust ranges if we do not know whether the iv increases
3634 or decreases, ... */
3635 dir == EV_DIR_UNKNOWN
3636 /* ... or if it may wrap. */
3637 || scev_probably_wraps_p (init, step, stmt, get_chrec_loop (chrec),
3638 true))
3639 return;
3641 /* We use TYPE_MIN_VALUE and TYPE_MAX_VALUE here instead of
3642 negative_overflow_infinity and positive_overflow_infinity,
3643 because we have concluded that the loop probably does not
3644 wrap. */
3646 type = TREE_TYPE (var);
3647 if (POINTER_TYPE_P (type) || !TYPE_MIN_VALUE (type))
3648 tmin = lower_bound_in_type (type, type);
3649 else
3650 tmin = TYPE_MIN_VALUE (type);
3651 if (POINTER_TYPE_P (type) || !TYPE_MAX_VALUE (type))
3652 tmax = upper_bound_in_type (type, type);
3653 else
3654 tmax = TYPE_MAX_VALUE (type);
3656 /* Try to use estimated number of iterations for the loop to constrain the
3657 final value in the evolution. */
3658 if (TREE_CODE (step) == INTEGER_CST
3659 && is_gimple_val (init)
3660 && (TREE_CODE (init) != SSA_NAME
3661 || get_value_range (init)->type == VR_RANGE))
3663 double_int nit;
3665 /* We are only entering here for loop header PHI nodes, so using
3666 the number of latch executions is the correct thing to use. */
3667 if (max_loop_iterations (loop, &nit))
3669 value_range_t maxvr = VR_INITIALIZER;
3670 double_int dtmp;
3671 bool unsigned_p = TYPE_UNSIGNED (TREE_TYPE (step));
3672 bool overflow = false;
3674 dtmp = tree_to_double_int (step)
3675 .mul_with_sign (nit, unsigned_p, &overflow);
3676 /* If the multiplication overflowed we can't do a meaningful
3677 adjustment. Likewise if the result doesn't fit in the type
3678 of the induction variable. For a signed type we have to
3679 check whether the result has the expected signedness which
3680 is that of the step as number of iterations is unsigned. */
3681 if (!overflow
3682 && double_int_fits_to_tree_p (TREE_TYPE (init), dtmp)
3683 && (unsigned_p
3684 || ((dtmp.high ^ TREE_INT_CST_HIGH (step)) >= 0)))
3686 tem = double_int_to_tree (TREE_TYPE (init), dtmp);
3687 extract_range_from_binary_expr (&maxvr, PLUS_EXPR,
3688 TREE_TYPE (init), init, tem);
3689 /* Likewise if the addition did. */
3690 if (maxvr.type == VR_RANGE)
3692 tmin = maxvr.min;
3693 tmax = maxvr.max;
3699 if (vr->type == VR_VARYING || vr->type == VR_UNDEFINED)
3701 min = tmin;
3702 max = tmax;
3704 /* For VARYING or UNDEFINED ranges, just about anything we get
3705 from scalar evolutions should be better. */
3707 if (dir == EV_DIR_DECREASES)
3708 max = init;
3709 else
3710 min = init;
3712 /* If we would create an invalid range, then just assume we
3713 know absolutely nothing. This may be over-conservative,
3714 but it's clearly safe, and should happen only in unreachable
3715 parts of code, or for invalid programs. */
3716 if (compare_values (min, max) == 1)
3717 return;
3719 set_value_range (vr, VR_RANGE, min, max, vr->equiv);
3721 else if (vr->type == VR_RANGE)
3723 min = vr->min;
3724 max = vr->max;
3726 if (dir == EV_DIR_DECREASES)
3728 /* INIT is the maximum value. If INIT is lower than VR->MAX
3729 but no smaller than VR->MIN, set VR->MAX to INIT. */
3730 if (compare_values (init, max) == -1)
3731 max = init;
3733 /* According to the loop information, the variable does not
3734 overflow. If we think it does, probably because of an
3735 overflow due to arithmetic on a different INF value,
3736 reset now. */
3737 if (is_negative_overflow_infinity (min)
3738 || compare_values (min, tmin) == -1)
3739 min = tmin;
3742 else
3744 /* If INIT is bigger than VR->MIN, set VR->MIN to INIT. */
3745 if (compare_values (init, min) == 1)
3746 min = init;
3748 if (is_positive_overflow_infinity (max)
3749 || compare_values (tmax, max) == -1)
3750 max = tmax;
3753 /* If we just created an invalid range with the minimum
3754 greater than the maximum, we fail conservatively.
3755 This should happen only in unreachable
3756 parts of code, or for invalid programs. */
3757 if (compare_values (min, max) == 1)
3758 return;
3760 set_value_range (vr, VR_RANGE, min, max, vr->equiv);
3764 /* Return true if VAR may overflow at STMT. This checks any available
3765 loop information to see if we can determine that VAR does not
3766 overflow. */
3768 static bool
3769 vrp_var_may_overflow (tree var, gimple stmt)
3771 struct loop *l;
3772 tree chrec, init, step;
3774 if (current_loops == NULL)
3775 return true;
3777 l = loop_containing_stmt (stmt);
3778 if (l == NULL
3779 || !loop_outer (l))
3780 return true;
3782 chrec = instantiate_parameters (l, analyze_scalar_evolution (l, var));
3783 if (TREE_CODE (chrec) != POLYNOMIAL_CHREC)
3784 return true;
3786 init = initial_condition_in_loop_num (chrec, l->num);
3787 step = evolution_part_in_loop_num (chrec, l->num);
3789 if (step == NULL_TREE
3790 || !is_gimple_min_invariant (step)
3791 || !valid_value_p (init))
3792 return true;
3794 /* If we get here, we know something useful about VAR based on the
3795 loop information. If it wraps, it may overflow. */
3797 if (scev_probably_wraps_p (init, step, stmt, get_chrec_loop (chrec),
3798 true))
3799 return true;
3801 if (dump_file && (dump_flags & TDF_DETAILS) != 0)
3803 print_generic_expr (dump_file, var, 0);
3804 fprintf (dump_file, ": loop information indicates does not overflow\n");
3807 return false;
3811 /* Given two numeric value ranges VR0, VR1 and a comparison code COMP:
3813 - Return BOOLEAN_TRUE_NODE if VR0 COMP VR1 always returns true for
3814 all the values in the ranges.
3816 - Return BOOLEAN_FALSE_NODE if the comparison always returns false.
3818 - Return NULL_TREE if it is not always possible to determine the
3819 value of the comparison.
3821 Also set *STRICT_OVERFLOW_P to indicate whether a range with an
3822 overflow infinity was used in the test. */
3825 static tree
3826 compare_ranges (enum tree_code comp, value_range_t *vr0, value_range_t *vr1,
3827 bool *strict_overflow_p)
3829 /* VARYING or UNDEFINED ranges cannot be compared. */
3830 if (vr0->type == VR_VARYING
3831 || vr0->type == VR_UNDEFINED
3832 || vr1->type == VR_VARYING
3833 || vr1->type == VR_UNDEFINED)
3834 return NULL_TREE;
3836 /* Anti-ranges need to be handled separately. */
3837 if (vr0->type == VR_ANTI_RANGE || vr1->type == VR_ANTI_RANGE)
3839 /* If both are anti-ranges, then we cannot compute any
3840 comparison. */
3841 if (vr0->type == VR_ANTI_RANGE && vr1->type == VR_ANTI_RANGE)
3842 return NULL_TREE;
3844 /* These comparisons are never statically computable. */
3845 if (comp == GT_EXPR
3846 || comp == GE_EXPR
3847 || comp == LT_EXPR
3848 || comp == LE_EXPR)
3849 return NULL_TREE;
3851 /* Equality can be computed only between a range and an
3852 anti-range. ~[VAL1, VAL2] == [VAL1, VAL2] is always false. */
3853 if (vr0->type == VR_RANGE)
3855 /* To simplify processing, make VR0 the anti-range. */
3856 value_range_t *tmp = vr0;
3857 vr0 = vr1;
3858 vr1 = tmp;
3861 gcc_assert (comp == NE_EXPR || comp == EQ_EXPR);
3863 if (compare_values_warnv (vr0->min, vr1->min, strict_overflow_p) == 0
3864 && compare_values_warnv (vr0->max, vr1->max, strict_overflow_p) == 0)
3865 return (comp == NE_EXPR) ? boolean_true_node : boolean_false_node;
3867 return NULL_TREE;
3870 if (!usable_range_p (vr0, strict_overflow_p)
3871 || !usable_range_p (vr1, strict_overflow_p))
3872 return NULL_TREE;
3874 /* Simplify processing. If COMP is GT_EXPR or GE_EXPR, switch the
3875 operands around and change the comparison code. */
3876 if (comp == GT_EXPR || comp == GE_EXPR)
3878 value_range_t *tmp;
3879 comp = (comp == GT_EXPR) ? LT_EXPR : LE_EXPR;
3880 tmp = vr0;
3881 vr0 = vr1;
3882 vr1 = tmp;
3885 if (comp == EQ_EXPR)
3887 /* Equality may only be computed if both ranges represent
3888 exactly one value. */
3889 if (compare_values_warnv (vr0->min, vr0->max, strict_overflow_p) == 0
3890 && compare_values_warnv (vr1->min, vr1->max, strict_overflow_p) == 0)
3892 int cmp_min = compare_values_warnv (vr0->min, vr1->min,
3893 strict_overflow_p);
3894 int cmp_max = compare_values_warnv (vr0->max, vr1->max,
3895 strict_overflow_p);
3896 if (cmp_min == 0 && cmp_max == 0)
3897 return boolean_true_node;
3898 else if (cmp_min != -2 && cmp_max != -2)
3899 return boolean_false_node;
3901 /* If [V0_MIN, V1_MAX] < [V1_MIN, V1_MAX] then V0 != V1. */
3902 else if (compare_values_warnv (vr0->min, vr1->max,
3903 strict_overflow_p) == 1
3904 || compare_values_warnv (vr1->min, vr0->max,
3905 strict_overflow_p) == 1)
3906 return boolean_false_node;
3908 return NULL_TREE;
3910 else if (comp == NE_EXPR)
3912 int cmp1, cmp2;
3914 /* If VR0 is completely to the left or completely to the right
3915 of VR1, they are always different. Notice that we need to
3916 make sure that both comparisons yield similar results to
3917 avoid comparing values that cannot be compared at
3918 compile-time. */
3919 cmp1 = compare_values_warnv (vr0->max, vr1->min, strict_overflow_p);
3920 cmp2 = compare_values_warnv (vr0->min, vr1->max, strict_overflow_p);
3921 if ((cmp1 == -1 && cmp2 == -1) || (cmp1 == 1 && cmp2 == 1))
3922 return boolean_true_node;
3924 /* If VR0 and VR1 represent a single value and are identical,
3925 return false. */
3926 else if (compare_values_warnv (vr0->min, vr0->max,
3927 strict_overflow_p) == 0
3928 && compare_values_warnv (vr1->min, vr1->max,
3929 strict_overflow_p) == 0
3930 && compare_values_warnv (vr0->min, vr1->min,
3931 strict_overflow_p) == 0
3932 && compare_values_warnv (vr0->max, vr1->max,
3933 strict_overflow_p) == 0)
3934 return boolean_false_node;
3936 /* Otherwise, they may or may not be different. */
3937 else
3938 return NULL_TREE;
3940 else if (comp == LT_EXPR || comp == LE_EXPR)
3942 int tst;
3944 /* If VR0 is to the left of VR1, return true. */
3945 tst = compare_values_warnv (vr0->max, vr1->min, strict_overflow_p);
3946 if ((comp == LT_EXPR && tst == -1)
3947 || (comp == LE_EXPR && (tst == -1 || tst == 0)))
3949 if (overflow_infinity_range_p (vr0)
3950 || overflow_infinity_range_p (vr1))
3951 *strict_overflow_p = true;
3952 return boolean_true_node;
3955 /* If VR0 is to the right of VR1, return false. */
3956 tst = compare_values_warnv (vr0->min, vr1->max, strict_overflow_p);
3957 if ((comp == LT_EXPR && (tst == 0 || tst == 1))
3958 || (comp == LE_EXPR && tst == 1))
3960 if (overflow_infinity_range_p (vr0)
3961 || overflow_infinity_range_p (vr1))
3962 *strict_overflow_p = true;
3963 return boolean_false_node;
3966 /* Otherwise, we don't know. */
3967 return NULL_TREE;
3970 gcc_unreachable ();
3974 /* Given a value range VR, a value VAL and a comparison code COMP, return
3975 BOOLEAN_TRUE_NODE if VR COMP VAL always returns true for all the
3976 values in VR. Return BOOLEAN_FALSE_NODE if the comparison
3977 always returns false. Return NULL_TREE if it is not always
3978 possible to determine the value of the comparison. Also set
3979 *STRICT_OVERFLOW_P to indicate whether a range with an overflow
3980 infinity was used in the test. */
3982 static tree
3983 compare_range_with_value (enum tree_code comp, value_range_t *vr, tree val,
3984 bool *strict_overflow_p)
3986 if (vr->type == VR_VARYING || vr->type == VR_UNDEFINED)
3987 return NULL_TREE;
3989 /* Anti-ranges need to be handled separately. */
3990 if (vr->type == VR_ANTI_RANGE)
3992 /* For anti-ranges, the only predicates that we can compute at
3993 compile time are equality and inequality. */
3994 if (comp == GT_EXPR
3995 || comp == GE_EXPR
3996 || comp == LT_EXPR
3997 || comp == LE_EXPR)
3998 return NULL_TREE;
4000 /* ~[VAL_1, VAL_2] OP VAL is known if VAL_1 <= VAL <= VAL_2. */
4001 if (value_inside_range (val, vr->min, vr->max) == 1)
4002 return (comp == NE_EXPR) ? boolean_true_node : boolean_false_node;
4004 return NULL_TREE;
4007 if (!usable_range_p (vr, strict_overflow_p))
4008 return NULL_TREE;
4010 if (comp == EQ_EXPR)
4012 /* EQ_EXPR may only be computed if VR represents exactly
4013 one value. */
4014 if (compare_values_warnv (vr->min, vr->max, strict_overflow_p) == 0)
4016 int cmp = compare_values_warnv (vr->min, val, strict_overflow_p);
4017 if (cmp == 0)
4018 return boolean_true_node;
4019 else if (cmp == -1 || cmp == 1 || cmp == 2)
4020 return boolean_false_node;
4022 else if (compare_values_warnv (val, vr->min, strict_overflow_p) == -1
4023 || compare_values_warnv (vr->max, val, strict_overflow_p) == -1)
4024 return boolean_false_node;
4026 return NULL_TREE;
4028 else if (comp == NE_EXPR)
4030 /* If VAL is not inside VR, then they are always different. */
4031 if (compare_values_warnv (vr->max, val, strict_overflow_p) == -1
4032 || compare_values_warnv (vr->min, val, strict_overflow_p) == 1)
4033 return boolean_true_node;
4035 /* If VR represents exactly one value equal to VAL, then return
4036 false. */
4037 if (compare_values_warnv (vr->min, vr->max, strict_overflow_p) == 0
4038 && compare_values_warnv (vr->min, val, strict_overflow_p) == 0)
4039 return boolean_false_node;
4041 /* Otherwise, they may or may not be different. */
4042 return NULL_TREE;
4044 else if (comp == LT_EXPR || comp == LE_EXPR)
4046 int tst;
4048 /* If VR is to the left of VAL, return true. */
4049 tst = compare_values_warnv (vr->max, val, strict_overflow_p);
4050 if ((comp == LT_EXPR && tst == -1)
4051 || (comp == LE_EXPR && (tst == -1 || tst == 0)))
4053 if (overflow_infinity_range_p (vr))
4054 *strict_overflow_p = true;
4055 return boolean_true_node;
4058 /* If VR is to the right of VAL, return false. */
4059 tst = compare_values_warnv (vr->min, val, strict_overflow_p);
4060 if ((comp == LT_EXPR && (tst == 0 || tst == 1))
4061 || (comp == LE_EXPR && tst == 1))
4063 if (overflow_infinity_range_p (vr))
4064 *strict_overflow_p = true;
4065 return boolean_false_node;
4068 /* Otherwise, we don't know. */
4069 return NULL_TREE;
4071 else if (comp == GT_EXPR || comp == GE_EXPR)
4073 int tst;
4075 /* If VR is to the right of VAL, return true. */
4076 tst = compare_values_warnv (vr->min, val, strict_overflow_p);
4077 if ((comp == GT_EXPR && tst == 1)
4078 || (comp == GE_EXPR && (tst == 0 || tst == 1)))
4080 if (overflow_infinity_range_p (vr))
4081 *strict_overflow_p = true;
4082 return boolean_true_node;
4085 /* If VR is to the left of VAL, return false. */
4086 tst = compare_values_warnv (vr->max, val, strict_overflow_p);
4087 if ((comp == GT_EXPR && (tst == -1 || tst == 0))
4088 || (comp == GE_EXPR && tst == -1))
4090 if (overflow_infinity_range_p (vr))
4091 *strict_overflow_p = true;
4092 return boolean_false_node;
4095 /* Otherwise, we don't know. */
4096 return NULL_TREE;
4099 gcc_unreachable ();
4103 /* Debugging dumps. */
4105 void dump_value_range (FILE *, value_range_t *);
4106 void debug_value_range (value_range_t *);
4107 void dump_all_value_ranges (FILE *);
4108 void debug_all_value_ranges (void);
4109 void dump_vr_equiv (FILE *, bitmap);
4110 void debug_vr_equiv (bitmap);
4113 /* Dump value range VR to FILE. */
4115 void
4116 dump_value_range (FILE *file, value_range_t *vr)
4118 if (vr == NULL)
4119 fprintf (file, "[]");
4120 else if (vr->type == VR_UNDEFINED)
4121 fprintf (file, "UNDEFINED");
4122 else if (vr->type == VR_RANGE || vr->type == VR_ANTI_RANGE)
4124 tree type = TREE_TYPE (vr->min);
4126 fprintf (file, "%s[", (vr->type == VR_ANTI_RANGE) ? "~" : "");
4128 if (is_negative_overflow_infinity (vr->min))
4129 fprintf (file, "-INF(OVF)");
4130 else if (INTEGRAL_TYPE_P (type)
4131 && !TYPE_UNSIGNED (type)
4132 && vrp_val_is_min (vr->min))
4133 fprintf (file, "-INF");
4134 else
4135 print_generic_expr (file, vr->min, 0);
4137 fprintf (file, ", ");
4139 if (is_positive_overflow_infinity (vr->max))
4140 fprintf (file, "+INF(OVF)");
4141 else if (INTEGRAL_TYPE_P (type)
4142 && vrp_val_is_max (vr->max))
4143 fprintf (file, "+INF");
4144 else
4145 print_generic_expr (file, vr->max, 0);
4147 fprintf (file, "]");
4149 if (vr->equiv)
4151 bitmap_iterator bi;
4152 unsigned i, c = 0;
4154 fprintf (file, " EQUIVALENCES: { ");
4156 EXECUTE_IF_SET_IN_BITMAP (vr->equiv, 0, i, bi)
4158 print_generic_expr (file, ssa_name (i), 0);
4159 fprintf (file, " ");
4160 c++;
4163 fprintf (file, "} (%u elements)", c);
4166 else if (vr->type == VR_VARYING)
4167 fprintf (file, "VARYING");
4168 else
4169 fprintf (file, "INVALID RANGE");
4173 /* Dump value range VR to stderr. */
4175 DEBUG_FUNCTION void
4176 debug_value_range (value_range_t *vr)
4178 dump_value_range (stderr, vr);
4179 fprintf (stderr, "\n");
4183 /* Dump value ranges of all SSA_NAMEs to FILE. */
4185 void
4186 dump_all_value_ranges (FILE *file)
4188 size_t i;
4190 for (i = 0; i < num_vr_values; i++)
4192 if (vr_value[i])
4194 print_generic_expr (file, ssa_name (i), 0);
4195 fprintf (file, ": ");
4196 dump_value_range (file, vr_value[i]);
4197 fprintf (file, "\n");
4201 fprintf (file, "\n");
4205 /* Dump all value ranges to stderr. */
4207 DEBUG_FUNCTION void
4208 debug_all_value_ranges (void)
4210 dump_all_value_ranges (stderr);
4214 /* Given a COND_EXPR COND of the form 'V OP W', and an SSA name V,
4215 create a new SSA name N and return the assertion assignment
4216 'V = ASSERT_EXPR <V, V OP W>'. */
4218 static gimple
4219 build_assert_expr_for (tree cond, tree v)
4221 tree a;
4222 gimple assertion;
4224 gcc_assert (TREE_CODE (v) == SSA_NAME
4225 && COMPARISON_CLASS_P (cond));
4227 a = build2 (ASSERT_EXPR, TREE_TYPE (v), v, cond);
4228 assertion = gimple_build_assign (NULL_TREE, a);
4230 /* The new ASSERT_EXPR, creates a new SSA name that replaces the
4231 operand of the ASSERT_EXPR. Create it so the new name and the old one
4232 are registered in the replacement table so that we can fix the SSA web
4233 after adding all the ASSERT_EXPRs. */
4234 create_new_def_for (v, assertion, NULL);
4236 return assertion;
4240 /* Return false if EXPR is a predicate expression involving floating
4241 point values. */
4243 static inline bool
4244 fp_predicate (gimple stmt)
4246 GIMPLE_CHECK (stmt, GIMPLE_COND);
4248 return FLOAT_TYPE_P (TREE_TYPE (gimple_cond_lhs (stmt)));
4252 /* If the range of values taken by OP can be inferred after STMT executes,
4253 return the comparison code (COMP_CODE_P) and value (VAL_P) that
4254 describes the inferred range. Return true if a range could be
4255 inferred. */
4257 static bool
4258 infer_value_range (gimple stmt, tree op, enum tree_code *comp_code_p, tree *val_p)
4260 *val_p = NULL_TREE;
4261 *comp_code_p = ERROR_MARK;
4263 /* Do not attempt to infer anything in names that flow through
4264 abnormal edges. */
4265 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op))
4266 return false;
4268 /* Similarly, don't infer anything from statements that may throw
4269 exceptions. */
4270 if (stmt_could_throw_p (stmt))
4271 return false;
4273 /* If STMT is the last statement of a basic block with no
4274 successors, there is no point inferring anything about any of its
4275 operands. We would not be able to find a proper insertion point
4276 for the assertion, anyway. */
4277 if (stmt_ends_bb_p (stmt) && EDGE_COUNT (gimple_bb (stmt)->succs) == 0)
4278 return false;
4280 /* We can only assume that a pointer dereference will yield
4281 non-NULL if -fdelete-null-pointer-checks is enabled. */
4282 if (flag_delete_null_pointer_checks
4283 && POINTER_TYPE_P (TREE_TYPE (op))
4284 && gimple_code (stmt) != GIMPLE_ASM)
4286 unsigned num_uses, num_loads, num_stores;
4288 count_uses_and_derefs (op, stmt, &num_uses, &num_loads, &num_stores);
4289 if (num_loads + num_stores > 0)
4291 *val_p = build_int_cst (TREE_TYPE (op), 0);
4292 *comp_code_p = NE_EXPR;
4293 return true;
4297 return false;
4301 void dump_asserts_for (FILE *, tree);
4302 void debug_asserts_for (tree);
4303 void dump_all_asserts (FILE *);
4304 void debug_all_asserts (void);
4306 /* Dump all the registered assertions for NAME to FILE. */
4308 void
4309 dump_asserts_for (FILE *file, tree name)
4311 assert_locus_t loc;
4313 fprintf (file, "Assertions to be inserted for ");
4314 print_generic_expr (file, name, 0);
4315 fprintf (file, "\n");
4317 loc = asserts_for[SSA_NAME_VERSION (name)];
4318 while (loc)
4320 fprintf (file, "\t");
4321 print_gimple_stmt (file, gsi_stmt (loc->si), 0, 0);
4322 fprintf (file, "\n\tBB #%d", loc->bb->index);
4323 if (loc->e)
4325 fprintf (file, "\n\tEDGE %d->%d", loc->e->src->index,
4326 loc->e->dest->index);
4327 dump_edge_info (file, loc->e, dump_flags, 0);
4329 fprintf (file, "\n\tPREDICATE: ");
4330 print_generic_expr (file, name, 0);
4331 fprintf (file, " %s ", tree_code_name[(int)loc->comp_code]);
4332 print_generic_expr (file, loc->val, 0);
4333 fprintf (file, "\n\n");
4334 loc = loc->next;
4337 fprintf (file, "\n");
4341 /* Dump all the registered assertions for NAME to stderr. */
4343 DEBUG_FUNCTION void
4344 debug_asserts_for (tree name)
4346 dump_asserts_for (stderr, name);
4350 /* Dump all the registered assertions for all the names to FILE. */
4352 void
4353 dump_all_asserts (FILE *file)
4355 unsigned i;
4356 bitmap_iterator bi;
4358 fprintf (file, "\nASSERT_EXPRs to be inserted\n\n");
4359 EXECUTE_IF_SET_IN_BITMAP (need_assert_for, 0, i, bi)
4360 dump_asserts_for (file, ssa_name (i));
4361 fprintf (file, "\n");
4365 /* Dump all the registered assertions for all the names to stderr. */
4367 DEBUG_FUNCTION void
4368 debug_all_asserts (void)
4370 dump_all_asserts (stderr);
4374 /* If NAME doesn't have an ASSERT_EXPR registered for asserting
4375 'EXPR COMP_CODE VAL' at a location that dominates block BB or
4376 E->DEST, then register this location as a possible insertion point
4377 for ASSERT_EXPR <NAME, EXPR COMP_CODE VAL>.
4379 BB, E and SI provide the exact insertion point for the new
4380 ASSERT_EXPR. If BB is NULL, then the ASSERT_EXPR is to be inserted
4381 on edge E. Otherwise, if E is NULL, the ASSERT_EXPR is inserted on
4382 BB. If SI points to a COND_EXPR or a SWITCH_EXPR statement, then E
4383 must not be NULL. */
4385 static void
4386 register_new_assert_for (tree name, tree expr,
4387 enum tree_code comp_code,
4388 tree val,
4389 basic_block bb,
4390 edge e,
4391 gimple_stmt_iterator si)
4393 assert_locus_t n, loc, last_loc;
4394 basic_block dest_bb;
4396 gcc_checking_assert (bb == NULL || e == NULL);
4398 if (e == NULL)
4399 gcc_checking_assert (gimple_code (gsi_stmt (si)) != GIMPLE_COND
4400 && gimple_code (gsi_stmt (si)) != GIMPLE_SWITCH);
4402 /* Never build an assert comparing against an integer constant with
4403 TREE_OVERFLOW set. This confuses our undefined overflow warning
4404 machinery. */
4405 if (TREE_CODE (val) == INTEGER_CST
4406 && TREE_OVERFLOW (val))
4407 val = build_int_cst_wide (TREE_TYPE (val),
4408 TREE_INT_CST_LOW (val), TREE_INT_CST_HIGH (val));
4410 /* The new assertion A will be inserted at BB or E. We need to
4411 determine if the new location is dominated by a previously
4412 registered location for A. If we are doing an edge insertion,
4413 assume that A will be inserted at E->DEST. Note that this is not
4414 necessarily true.
4416 If E is a critical edge, it will be split. But even if E is
4417 split, the new block will dominate the same set of blocks that
4418 E->DEST dominates.
4420 The reverse, however, is not true, blocks dominated by E->DEST
4421 will not be dominated by the new block created to split E. So,
4422 if the insertion location is on a critical edge, we will not use
4423 the new location to move another assertion previously registered
4424 at a block dominated by E->DEST. */
4425 dest_bb = (bb) ? bb : e->dest;
4427 /* If NAME already has an ASSERT_EXPR registered for COMP_CODE and
4428 VAL at a block dominating DEST_BB, then we don't need to insert a new
4429 one. Similarly, if the same assertion already exists at a block
4430 dominated by DEST_BB and the new location is not on a critical
4431 edge, then update the existing location for the assertion (i.e.,
4432 move the assertion up in the dominance tree).
4434 Note, this is implemented as a simple linked list because there
4435 should not be more than a handful of assertions registered per
4436 name. If this becomes a performance problem, a table hashed by
4437 COMP_CODE and VAL could be implemented. */
4438 loc = asserts_for[SSA_NAME_VERSION (name)];
4439 last_loc = loc;
4440 while (loc)
4442 if (loc->comp_code == comp_code
4443 && (loc->val == val
4444 || operand_equal_p (loc->val, val, 0))
4445 && (loc->expr == expr
4446 || operand_equal_p (loc->expr, expr, 0)))
4448 /* If E is not a critical edge and DEST_BB
4449 dominates the existing location for the assertion, move
4450 the assertion up in the dominance tree by updating its
4451 location information. */
4452 if ((e == NULL || !EDGE_CRITICAL_P (e))
4453 && dominated_by_p (CDI_DOMINATORS, loc->bb, dest_bb))
4455 loc->bb = dest_bb;
4456 loc->e = e;
4457 loc->si = si;
4458 return;
4462 /* Update the last node of the list and move to the next one. */
4463 last_loc = loc;
4464 loc = loc->next;
4467 /* If we didn't find an assertion already registered for
4468 NAME COMP_CODE VAL, add a new one at the end of the list of
4469 assertions associated with NAME. */
4470 n = XNEW (struct assert_locus_d);
4471 n->bb = dest_bb;
4472 n->e = e;
4473 n->si = si;
4474 n->comp_code = comp_code;
4475 n->val = val;
4476 n->expr = expr;
4477 n->next = NULL;
4479 if (last_loc)
4480 last_loc->next = n;
4481 else
4482 asserts_for[SSA_NAME_VERSION (name)] = n;
4484 bitmap_set_bit (need_assert_for, SSA_NAME_VERSION (name));
4487 /* (COND_OP0 COND_CODE COND_OP1) is a predicate which uses NAME.
4488 Extract a suitable test code and value and store them into *CODE_P and
4489 *VAL_P so the predicate is normalized to NAME *CODE_P *VAL_P.
4491 If no extraction was possible, return FALSE, otherwise return TRUE.
4493 If INVERT is true, then we invert the result stored into *CODE_P. */
4495 static bool
4496 extract_code_and_val_from_cond_with_ops (tree name, enum tree_code cond_code,
4497 tree cond_op0, tree cond_op1,
4498 bool invert, enum tree_code *code_p,
4499 tree *val_p)
4501 enum tree_code comp_code;
4502 tree val;
4504 /* Otherwise, we have a comparison of the form NAME COMP VAL
4505 or VAL COMP NAME. */
4506 if (name == cond_op1)
4508 /* If the predicate is of the form VAL COMP NAME, flip
4509 COMP around because we need to register NAME as the
4510 first operand in the predicate. */
4511 comp_code = swap_tree_comparison (cond_code);
4512 val = cond_op0;
4514 else
4516 /* The comparison is of the form NAME COMP VAL, so the
4517 comparison code remains unchanged. */
4518 comp_code = cond_code;
4519 val = cond_op1;
4522 /* Invert the comparison code as necessary. */
4523 if (invert)
4524 comp_code = invert_tree_comparison (comp_code, 0);
4526 /* VRP does not handle float types. */
4527 if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (val)))
4528 return false;
4530 /* Do not register always-false predicates.
4531 FIXME: this works around a limitation in fold() when dealing with
4532 enumerations. Given 'enum { N1, N2 } x;', fold will not
4533 fold 'if (x > N2)' to 'if (0)'. */
4534 if ((comp_code == GT_EXPR || comp_code == LT_EXPR)
4535 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
4537 tree min = TYPE_MIN_VALUE (TREE_TYPE (val));
4538 tree max = TYPE_MAX_VALUE (TREE_TYPE (val));
4540 if (comp_code == GT_EXPR
4541 && (!max
4542 || compare_values (val, max) == 0))
4543 return false;
4545 if (comp_code == LT_EXPR
4546 && (!min
4547 || compare_values (val, min) == 0))
4548 return false;
4550 *code_p = comp_code;
4551 *val_p = val;
4552 return true;
4555 /* Find out smallest RES where RES > VAL && (RES & MASK) == RES, if any
4556 (otherwise return VAL). VAL and MASK must be zero-extended for
4557 precision PREC. If SGNBIT is non-zero, first xor VAL with SGNBIT
4558 (to transform signed values into unsigned) and at the end xor
4559 SGNBIT back. */
4561 static double_int
4562 masked_increment (double_int val, double_int mask, double_int sgnbit,
4563 unsigned int prec)
4565 double_int bit = double_int_one, res;
4566 unsigned int i;
4568 val ^= sgnbit;
4569 for (i = 0; i < prec; i++, bit += bit)
4571 res = mask;
4572 if ((res & bit).is_zero ())
4573 continue;
4574 res = bit - double_int_one;
4575 res = (val + bit).and_not (res);
4576 res &= mask;
4577 if (res.ugt (val))
4578 return res ^ sgnbit;
4580 return val ^ sgnbit;
4583 /* Try to register an edge assertion for SSA name NAME on edge E for
4584 the condition COND contributing to the conditional jump pointed to by BSI.
4585 Invert the condition COND if INVERT is true.
4586 Return true if an assertion for NAME could be registered. */
4588 static bool
4589 register_edge_assert_for_2 (tree name, edge e, gimple_stmt_iterator bsi,
4590 enum tree_code cond_code,
4591 tree cond_op0, tree cond_op1, bool invert)
4593 tree val;
4594 enum tree_code comp_code;
4595 bool retval = false;
4597 if (!extract_code_and_val_from_cond_with_ops (name, cond_code,
4598 cond_op0,
4599 cond_op1,
4600 invert, &comp_code, &val))
4601 return false;
4603 /* Only register an ASSERT_EXPR if NAME was found in the sub-graph
4604 reachable from E. */
4605 if (live_on_edge (e, name)
4606 && !has_single_use (name))
4608 register_new_assert_for (name, name, comp_code, val, NULL, e, bsi);
4609 retval = true;
4612 /* In the case of NAME <= CST and NAME being defined as
4613 NAME = (unsigned) NAME2 + CST2 we can assert NAME2 >= -CST2
4614 and NAME2 <= CST - CST2. We can do the same for NAME > CST.
4615 This catches range and anti-range tests. */
4616 if ((comp_code == LE_EXPR
4617 || comp_code == GT_EXPR)
4618 && TREE_CODE (val) == INTEGER_CST
4619 && TYPE_UNSIGNED (TREE_TYPE (val)))
4621 gimple def_stmt = SSA_NAME_DEF_STMT (name);
4622 tree cst2 = NULL_TREE, name2 = NULL_TREE, name3 = NULL_TREE;
4624 /* Extract CST2 from the (optional) addition. */
4625 if (is_gimple_assign (def_stmt)
4626 && gimple_assign_rhs_code (def_stmt) == PLUS_EXPR)
4628 name2 = gimple_assign_rhs1 (def_stmt);
4629 cst2 = gimple_assign_rhs2 (def_stmt);
4630 if (TREE_CODE (name2) == SSA_NAME
4631 && TREE_CODE (cst2) == INTEGER_CST)
4632 def_stmt = SSA_NAME_DEF_STMT (name2);
4635 /* Extract NAME2 from the (optional) sign-changing cast. */
4636 if (gimple_assign_cast_p (def_stmt))
4638 if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt))
4639 && ! TYPE_UNSIGNED (TREE_TYPE (gimple_assign_rhs1 (def_stmt)))
4640 && (TYPE_PRECISION (gimple_expr_type (def_stmt))
4641 == TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (def_stmt)))))
4642 name3 = gimple_assign_rhs1 (def_stmt);
4645 /* If name3 is used later, create an ASSERT_EXPR for it. */
4646 if (name3 != NULL_TREE
4647 && TREE_CODE (name3) == SSA_NAME
4648 && (cst2 == NULL_TREE
4649 || TREE_CODE (cst2) == INTEGER_CST)
4650 && INTEGRAL_TYPE_P (TREE_TYPE (name3))
4651 && live_on_edge (e, name3)
4652 && !has_single_use (name3))
4654 tree tmp;
4656 /* Build an expression for the range test. */
4657 tmp = build1 (NOP_EXPR, TREE_TYPE (name), name3);
4658 if (cst2 != NULL_TREE)
4659 tmp = build2 (PLUS_EXPR, TREE_TYPE (name), tmp, cst2);
4661 if (dump_file)
4663 fprintf (dump_file, "Adding assert for ");
4664 print_generic_expr (dump_file, name3, 0);
4665 fprintf (dump_file, " from ");
4666 print_generic_expr (dump_file, tmp, 0);
4667 fprintf (dump_file, "\n");
4670 register_new_assert_for (name3, tmp, comp_code, val, NULL, e, bsi);
4672 retval = true;
4675 /* If name2 is used later, create an ASSERT_EXPR for it. */
4676 if (name2 != NULL_TREE
4677 && TREE_CODE (name2) == SSA_NAME
4678 && TREE_CODE (cst2) == INTEGER_CST
4679 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
4680 && live_on_edge (e, name2)
4681 && !has_single_use (name2))
4683 tree tmp;
4685 /* Build an expression for the range test. */
4686 tmp = name2;
4687 if (TREE_TYPE (name) != TREE_TYPE (name2))
4688 tmp = build1 (NOP_EXPR, TREE_TYPE (name), tmp);
4689 if (cst2 != NULL_TREE)
4690 tmp = build2 (PLUS_EXPR, TREE_TYPE (name), tmp, cst2);
4692 if (dump_file)
4694 fprintf (dump_file, "Adding assert for ");
4695 print_generic_expr (dump_file, name2, 0);
4696 fprintf (dump_file, " from ");
4697 print_generic_expr (dump_file, tmp, 0);
4698 fprintf (dump_file, "\n");
4701 register_new_assert_for (name2, tmp, comp_code, val, NULL, e, bsi);
4703 retval = true;
4707 if (TREE_CODE_CLASS (comp_code) == tcc_comparison
4708 && TREE_CODE (val) == INTEGER_CST)
4710 gimple def_stmt = SSA_NAME_DEF_STMT (name);
4711 tree name2 = NULL_TREE, names[2], cst2 = NULL_TREE;
4712 tree val2 = NULL_TREE;
4713 double_int mask = double_int_zero;
4714 unsigned int prec = TYPE_PRECISION (TREE_TYPE (val));
4716 /* Add asserts for NAME cmp CST and NAME being defined
4717 as NAME = (int) NAME2. */
4718 if (!TYPE_UNSIGNED (TREE_TYPE (val))
4719 && (comp_code == LE_EXPR || comp_code == LT_EXPR
4720 || comp_code == GT_EXPR || comp_code == GE_EXPR)
4721 && gimple_assign_cast_p (def_stmt))
4723 name2 = gimple_assign_rhs1 (def_stmt);
4724 if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt))
4725 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
4726 && TYPE_UNSIGNED (TREE_TYPE (name2))
4727 && prec == TYPE_PRECISION (TREE_TYPE (name2))
4728 && (comp_code == LE_EXPR || comp_code == GT_EXPR
4729 || !tree_int_cst_equal (val,
4730 TYPE_MIN_VALUE (TREE_TYPE (val))))
4731 && live_on_edge (e, name2)
4732 && !has_single_use (name2))
4734 tree tmp, cst;
4735 enum tree_code new_comp_code = comp_code;
4737 cst = fold_convert (TREE_TYPE (name2),
4738 TYPE_MIN_VALUE (TREE_TYPE (val)));
4739 /* Build an expression for the range test. */
4740 tmp = build2 (PLUS_EXPR, TREE_TYPE (name2), name2, cst);
4741 cst = fold_build2 (PLUS_EXPR, TREE_TYPE (name2), cst,
4742 fold_convert (TREE_TYPE (name2), val));
4743 if (comp_code == LT_EXPR || comp_code == GE_EXPR)
4745 new_comp_code = comp_code == LT_EXPR ? LE_EXPR : GT_EXPR;
4746 cst = fold_build2 (MINUS_EXPR, TREE_TYPE (name2), cst,
4747 build_int_cst (TREE_TYPE (name2), 1));
4750 if (dump_file)
4752 fprintf (dump_file, "Adding assert for ");
4753 print_generic_expr (dump_file, name2, 0);
4754 fprintf (dump_file, " from ");
4755 print_generic_expr (dump_file, tmp, 0);
4756 fprintf (dump_file, "\n");
4759 register_new_assert_for (name2, tmp, new_comp_code, cst, NULL,
4760 e, bsi);
4762 retval = true;
4766 /* Add asserts for NAME cmp CST and NAME being defined as
4767 NAME = NAME2 >> CST2.
4769 Extract CST2 from the right shift. */
4770 if (is_gimple_assign (def_stmt)
4771 && gimple_assign_rhs_code (def_stmt) == RSHIFT_EXPR)
4773 name2 = gimple_assign_rhs1 (def_stmt);
4774 cst2 = gimple_assign_rhs2 (def_stmt);
4775 if (TREE_CODE (name2) == SSA_NAME
4776 && host_integerp (cst2, 1)
4777 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
4778 && IN_RANGE (tree_low_cst (cst2, 1), 1, prec - 1)
4779 && prec <= HOST_BITS_PER_DOUBLE_INT
4780 && prec == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (val)))
4781 && live_on_edge (e, name2)
4782 && !has_single_use (name2))
4784 mask = double_int::mask (tree_low_cst (cst2, 1));
4785 val2 = fold_binary (LSHIFT_EXPR, TREE_TYPE (val), val, cst2);
4788 if (val2 != NULL_TREE
4789 && TREE_CODE (val2) == INTEGER_CST
4790 && simple_cst_equal (fold_build2 (RSHIFT_EXPR,
4791 TREE_TYPE (val),
4792 val2, cst2), val))
4794 enum tree_code new_comp_code = comp_code;
4795 tree tmp, new_val;
4797 tmp = name2;
4798 if (comp_code == EQ_EXPR || comp_code == NE_EXPR)
4800 if (!TYPE_UNSIGNED (TREE_TYPE (val)))
4802 tree type = build_nonstandard_integer_type (prec, 1);
4803 tmp = build1 (NOP_EXPR, type, name2);
4804 val2 = fold_convert (type, val2);
4806 tmp = fold_build2 (MINUS_EXPR, TREE_TYPE (tmp), tmp, val2);
4807 new_val = double_int_to_tree (TREE_TYPE (tmp), mask);
4808 new_comp_code = comp_code == EQ_EXPR ? LE_EXPR : GT_EXPR;
4810 else if (comp_code == LT_EXPR || comp_code == GE_EXPR)
4811 new_val = val2;
4812 else
4814 double_int maxval
4815 = double_int::max_value (prec, TYPE_UNSIGNED (TREE_TYPE (val)));
4816 mask |= tree_to_double_int (val2);
4817 if (mask == maxval)
4818 new_val = NULL_TREE;
4819 else
4820 new_val = double_int_to_tree (TREE_TYPE (val2), mask);
4823 if (new_val)
4825 if (dump_file)
4827 fprintf (dump_file, "Adding assert for ");
4828 print_generic_expr (dump_file, name2, 0);
4829 fprintf (dump_file, " from ");
4830 print_generic_expr (dump_file, tmp, 0);
4831 fprintf (dump_file, "\n");
4834 register_new_assert_for (name2, tmp, new_comp_code, new_val,
4835 NULL, e, bsi);
4836 retval = true;
4840 /* Add asserts for NAME cmp CST and NAME being defined as
4841 NAME = NAME2 & CST2.
4843 Extract CST2 from the and. */
4844 names[0] = NULL_TREE;
4845 names[1] = NULL_TREE;
4846 cst2 = NULL_TREE;
4847 if (is_gimple_assign (def_stmt)
4848 && gimple_assign_rhs_code (def_stmt) == BIT_AND_EXPR)
4850 name2 = gimple_assign_rhs1 (def_stmt);
4851 cst2 = gimple_assign_rhs2 (def_stmt);
4852 if (TREE_CODE (name2) == SSA_NAME
4853 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
4854 && TREE_CODE (cst2) == INTEGER_CST
4855 && !integer_zerop (cst2)
4856 && prec <= HOST_BITS_PER_DOUBLE_INT
4857 && (prec > 1
4858 || TYPE_UNSIGNED (TREE_TYPE (val))))
4860 gimple def_stmt2 = SSA_NAME_DEF_STMT (name2);
4861 if (gimple_assign_cast_p (def_stmt2))
4863 names[1] = gimple_assign_rhs1 (def_stmt2);
4864 if (!CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt2))
4865 || !INTEGRAL_TYPE_P (TREE_TYPE (names[1]))
4866 || (TYPE_PRECISION (TREE_TYPE (name2))
4867 != TYPE_PRECISION (TREE_TYPE (names[1])))
4868 || !live_on_edge (e, names[1])
4869 || has_single_use (names[1]))
4870 names[1] = NULL_TREE;
4872 if (live_on_edge (e, name2)
4873 && !has_single_use (name2))
4874 names[0] = name2;
4877 if (names[0] || names[1])
4879 double_int minv, maxv = double_int_zero, valv, cst2v;
4880 double_int tem, sgnbit;
4881 bool valid_p = false, valn = false, cst2n = false;
4882 enum tree_code ccode = comp_code;
4884 valv = tree_to_double_int (val).zext (prec);
4885 cst2v = tree_to_double_int (cst2).zext (prec);
4886 if (!TYPE_UNSIGNED (TREE_TYPE (val)))
4888 valn = valv.sext (prec).is_negative ();
4889 cst2n = cst2v.sext (prec).is_negative ();
4891 /* If CST2 doesn't have most significant bit set,
4892 but VAL is negative, we have comparison like
4893 if ((x & 0x123) > -4) (always true). Just give up. */
4894 if (!cst2n && valn)
4895 ccode = ERROR_MARK;
4896 if (cst2n)
4897 sgnbit = double_int_one.llshift (prec - 1, prec).zext (prec);
4898 else
4899 sgnbit = double_int_zero;
4900 minv = valv & cst2v;
4901 switch (ccode)
4903 case EQ_EXPR:
4904 /* Minimum unsigned value for equality is VAL & CST2
4905 (should be equal to VAL, otherwise we probably should
4906 have folded the comparison into false) and
4907 maximum unsigned value is VAL | ~CST2. */
4908 maxv = valv | ~cst2v;
4909 maxv = maxv.zext (prec);
4910 valid_p = true;
4911 break;
4912 case NE_EXPR:
4913 tem = valv | ~cst2v;
4914 tem = tem.zext (prec);
4915 /* If VAL is 0, handle (X & CST2) != 0 as (X & CST2) > 0U. */
4916 if (valv.is_zero ())
4918 cst2n = false;
4919 sgnbit = double_int_zero;
4920 goto gt_expr;
4922 /* If (VAL | ~CST2) is all ones, handle it as
4923 (X & CST2) < VAL. */
4924 if (tem == double_int::mask (prec))
4926 cst2n = false;
4927 valn = false;
4928 sgnbit = double_int_zero;
4929 goto lt_expr;
4931 if (!cst2n
4932 && cst2v.sext (prec).is_negative ())
4933 sgnbit = double_int_one.llshift (prec - 1, prec).zext (prec);
4934 if (!sgnbit.is_zero ())
4936 if (valv == sgnbit)
4938 cst2n = true;
4939 valn = true;
4940 goto gt_expr;
4942 if (tem == double_int::mask (prec - 1))
4944 cst2n = true;
4945 goto lt_expr;
4947 if (!cst2n)
4948 sgnbit = double_int_zero;
4950 break;
4951 case GE_EXPR:
4952 /* Minimum unsigned value for >= if (VAL & CST2) == VAL
4953 is VAL and maximum unsigned value is ~0. For signed
4954 comparison, if CST2 doesn't have most significant bit
4955 set, handle it similarly. If CST2 has MSB set,
4956 the minimum is the same, and maximum is ~0U/2. */
4957 if (minv != valv)
4959 /* If (VAL & CST2) != VAL, X & CST2 can't be equal to
4960 VAL. */
4961 minv = masked_increment (valv, cst2v, sgnbit, prec);
4962 if (minv == valv)
4963 break;
4965 maxv = double_int::mask (prec - (cst2n ? 1 : 0));
4966 valid_p = true;
4967 break;
4968 case GT_EXPR:
4969 gt_expr:
4970 /* Find out smallest MINV where MINV > VAL
4971 && (MINV & CST2) == MINV, if any. If VAL is signed and
4972 CST2 has MSB set, compute it biased by 1 << (prec - 1). */
4973 minv = masked_increment (valv, cst2v, sgnbit, prec);
4974 if (minv == valv)
4975 break;
4976 maxv = double_int::mask (prec - (cst2n ? 1 : 0));
4977 valid_p = true;
4978 break;
4979 case LE_EXPR:
4980 /* Minimum unsigned value for <= is 0 and maximum
4981 unsigned value is VAL | ~CST2 if (VAL & CST2) == VAL.
4982 Otherwise, find smallest VAL2 where VAL2 > VAL
4983 && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
4984 as maximum.
4985 For signed comparison, if CST2 doesn't have most
4986 significant bit set, handle it similarly. If CST2 has
4987 MSB set, the maximum is the same and minimum is INT_MIN. */
4988 if (minv == valv)
4989 maxv = valv;
4990 else
4992 maxv = masked_increment (valv, cst2v, sgnbit, prec);
4993 if (maxv == valv)
4994 break;
4995 maxv -= double_int_one;
4997 maxv |= ~cst2v;
4998 maxv = maxv.zext (prec);
4999 minv = sgnbit;
5000 valid_p = true;
5001 break;
5002 case LT_EXPR:
5003 lt_expr:
5004 /* Minimum unsigned value for < is 0 and maximum
5005 unsigned value is (VAL-1) | ~CST2 if (VAL & CST2) == VAL.
5006 Otherwise, find smallest VAL2 where VAL2 > VAL
5007 && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
5008 as maximum.
5009 For signed comparison, if CST2 doesn't have most
5010 significant bit set, handle it similarly. If CST2 has
5011 MSB set, the maximum is the same and minimum is INT_MIN. */
5012 if (minv == valv)
5014 if (valv == sgnbit)
5015 break;
5016 maxv = valv;
5018 else
5020 maxv = masked_increment (valv, cst2v, sgnbit, prec);
5021 if (maxv == valv)
5022 break;
5024 maxv -= double_int_one;
5025 maxv |= ~cst2v;
5026 maxv = maxv.zext (prec);
5027 minv = sgnbit;
5028 valid_p = true;
5029 break;
5030 default:
5031 break;
5033 if (valid_p
5034 && (maxv - minv).zext (prec) != double_int::mask (prec))
5036 tree tmp, new_val, type;
5037 int i;
5039 for (i = 0; i < 2; i++)
5040 if (names[i])
5042 double_int maxv2 = maxv;
5043 tmp = names[i];
5044 type = TREE_TYPE (names[i]);
5045 if (!TYPE_UNSIGNED (type))
5047 type = build_nonstandard_integer_type (prec, 1);
5048 tmp = build1 (NOP_EXPR, type, names[i]);
5050 if (!minv.is_zero ())
5052 tmp = build2 (PLUS_EXPR, type, tmp,
5053 double_int_to_tree (type, -minv));
5054 maxv2 = maxv - minv;
5056 new_val = double_int_to_tree (type, maxv2);
5058 if (dump_file)
5060 fprintf (dump_file, "Adding assert for ");
5061 print_generic_expr (dump_file, names[i], 0);
5062 fprintf (dump_file, " from ");
5063 print_generic_expr (dump_file, tmp, 0);
5064 fprintf (dump_file, "\n");
5067 register_new_assert_for (names[i], tmp, LE_EXPR,
5068 new_val, NULL, e, bsi);
5069 retval = true;
5075 return retval;
5078 /* OP is an operand of a truth value expression which is known to have
5079 a particular value. Register any asserts for OP and for any
5080 operands in OP's defining statement.
5082 If CODE is EQ_EXPR, then we want to register OP is zero (false),
5083 if CODE is NE_EXPR, then we want to register OP is nonzero (true). */
5085 static bool
5086 register_edge_assert_for_1 (tree op, enum tree_code code,
5087 edge e, gimple_stmt_iterator bsi)
5089 bool retval = false;
5090 gimple op_def;
5091 tree val;
5092 enum tree_code rhs_code;
5094 /* We only care about SSA_NAMEs. */
5095 if (TREE_CODE (op) != SSA_NAME)
5096 return false;
5098 /* We know that OP will have a zero or nonzero value. If OP is used
5099 more than once go ahead and register an assert for OP.
5101 The FOUND_IN_SUBGRAPH support is not helpful in this situation as
5102 it will always be set for OP (because OP is used in a COND_EXPR in
5103 the subgraph). */
5104 if (!has_single_use (op))
5106 val = build_int_cst (TREE_TYPE (op), 0);
5107 register_new_assert_for (op, op, code, val, NULL, e, bsi);
5108 retval = true;
5111 /* Now look at how OP is set. If it's set from a comparison,
5112 a truth operation or some bit operations, then we may be able
5113 to register information about the operands of that assignment. */
5114 op_def = SSA_NAME_DEF_STMT (op);
5115 if (gimple_code (op_def) != GIMPLE_ASSIGN)
5116 return retval;
5118 rhs_code = gimple_assign_rhs_code (op_def);
5120 if (TREE_CODE_CLASS (rhs_code) == tcc_comparison)
5122 bool invert = (code == EQ_EXPR ? true : false);
5123 tree op0 = gimple_assign_rhs1 (op_def);
5124 tree op1 = gimple_assign_rhs2 (op_def);
5126 if (TREE_CODE (op0) == SSA_NAME)
5127 retval |= register_edge_assert_for_2 (op0, e, bsi, rhs_code, op0, op1,
5128 invert);
5129 if (TREE_CODE (op1) == SSA_NAME)
5130 retval |= register_edge_assert_for_2 (op1, e, bsi, rhs_code, op0, op1,
5131 invert);
5133 else if ((code == NE_EXPR
5134 && gimple_assign_rhs_code (op_def) == BIT_AND_EXPR)
5135 || (code == EQ_EXPR
5136 && gimple_assign_rhs_code (op_def) == BIT_IOR_EXPR))
5138 /* Recurse on each operand. */
5139 retval |= register_edge_assert_for_1 (gimple_assign_rhs1 (op_def),
5140 code, e, bsi);
5141 retval |= register_edge_assert_for_1 (gimple_assign_rhs2 (op_def),
5142 code, e, bsi);
5144 else if (gimple_assign_rhs_code (op_def) == BIT_NOT_EXPR
5145 && TYPE_PRECISION (TREE_TYPE (gimple_assign_lhs (op_def))) == 1)
5147 /* Recurse, flipping CODE. */
5148 code = invert_tree_comparison (code, false);
5149 retval |= register_edge_assert_for_1 (gimple_assign_rhs1 (op_def),
5150 code, e, bsi);
5152 else if (gimple_assign_rhs_code (op_def) == SSA_NAME)
5154 /* Recurse through the copy. */
5155 retval |= register_edge_assert_for_1 (gimple_assign_rhs1 (op_def),
5156 code, e, bsi);
5158 else if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (op_def)))
5160 /* Recurse through the type conversion. */
5161 retval |= register_edge_assert_for_1 (gimple_assign_rhs1 (op_def),
5162 code, e, bsi);
5165 return retval;
5168 /* Try to register an edge assertion for SSA name NAME on edge E for
5169 the condition COND contributing to the conditional jump pointed to by SI.
5170 Return true if an assertion for NAME could be registered. */
5172 static bool
5173 register_edge_assert_for (tree name, edge e, gimple_stmt_iterator si,
5174 enum tree_code cond_code, tree cond_op0,
5175 tree cond_op1)
5177 tree val;
5178 enum tree_code comp_code;
5179 bool retval = false;
5180 bool is_else_edge = (e->flags & EDGE_FALSE_VALUE) != 0;
5182 /* Do not attempt to infer anything in names that flow through
5183 abnormal edges. */
5184 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name))
5185 return false;
5187 if (!extract_code_and_val_from_cond_with_ops (name, cond_code,
5188 cond_op0, cond_op1,
5189 is_else_edge,
5190 &comp_code, &val))
5191 return false;
5193 /* Register ASSERT_EXPRs for name. */
5194 retval |= register_edge_assert_for_2 (name, e, si, cond_code, cond_op0,
5195 cond_op1, is_else_edge);
5198 /* If COND is effectively an equality test of an SSA_NAME against
5199 the value zero or one, then we may be able to assert values
5200 for SSA_NAMEs which flow into COND. */
5202 /* In the case of NAME == 1 or NAME != 0, for BIT_AND_EXPR defining
5203 statement of NAME we can assert both operands of the BIT_AND_EXPR
5204 have nonzero value. */
5205 if (((comp_code == EQ_EXPR && integer_onep (val))
5206 || (comp_code == NE_EXPR && integer_zerop (val))))
5208 gimple def_stmt = SSA_NAME_DEF_STMT (name);
5210 if (is_gimple_assign (def_stmt)
5211 && gimple_assign_rhs_code (def_stmt) == BIT_AND_EXPR)
5213 tree op0 = gimple_assign_rhs1 (def_stmt);
5214 tree op1 = gimple_assign_rhs2 (def_stmt);
5215 retval |= register_edge_assert_for_1 (op0, NE_EXPR, e, si);
5216 retval |= register_edge_assert_for_1 (op1, NE_EXPR, e, si);
5220 /* In the case of NAME == 0 or NAME != 1, for BIT_IOR_EXPR defining
5221 statement of NAME we can assert both operands of the BIT_IOR_EXPR
5222 have zero value. */
5223 if (((comp_code == EQ_EXPR && integer_zerop (val))
5224 || (comp_code == NE_EXPR && integer_onep (val))))
5226 gimple def_stmt = SSA_NAME_DEF_STMT (name);
5228 /* For BIT_IOR_EXPR only if NAME == 0 both operands have
5229 necessarily zero value, or if type-precision is one. */
5230 if (is_gimple_assign (def_stmt)
5231 && (gimple_assign_rhs_code (def_stmt) == BIT_IOR_EXPR
5232 && (TYPE_PRECISION (TREE_TYPE (name)) == 1
5233 || comp_code == EQ_EXPR)))
5235 tree op0 = gimple_assign_rhs1 (def_stmt);
5236 tree op1 = gimple_assign_rhs2 (def_stmt);
5237 retval |= register_edge_assert_for_1 (op0, EQ_EXPR, e, si);
5238 retval |= register_edge_assert_for_1 (op1, EQ_EXPR, e, si);
5242 return retval;
5246 /* Determine whether the outgoing edges of BB should receive an
5247 ASSERT_EXPR for each of the operands of BB's LAST statement.
5248 The last statement of BB must be a COND_EXPR.
5250 If any of the sub-graphs rooted at BB have an interesting use of
5251 the predicate operands, an assert location node is added to the
5252 list of assertions for the corresponding operands. */
5254 static bool
5255 find_conditional_asserts (basic_block bb, gimple last)
5257 bool need_assert;
5258 gimple_stmt_iterator bsi;
5259 tree op;
5260 edge_iterator ei;
5261 edge e;
5262 ssa_op_iter iter;
5264 need_assert = false;
5265 bsi = gsi_for_stmt (last);
5267 /* Look for uses of the operands in each of the sub-graphs
5268 rooted at BB. We need to check each of the outgoing edges
5269 separately, so that we know what kind of ASSERT_EXPR to
5270 insert. */
5271 FOR_EACH_EDGE (e, ei, bb->succs)
5273 if (e->dest == bb)
5274 continue;
5276 /* Register the necessary assertions for each operand in the
5277 conditional predicate. */
5278 FOR_EACH_SSA_TREE_OPERAND (op, last, iter, SSA_OP_USE)
5280 need_assert |= register_edge_assert_for (op, e, bsi,
5281 gimple_cond_code (last),
5282 gimple_cond_lhs (last),
5283 gimple_cond_rhs (last));
5287 return need_assert;
5290 struct case_info
5292 tree expr;
5293 basic_block bb;
5296 /* Compare two case labels sorting first by the destination bb index
5297 and then by the case value. */
5299 static int
5300 compare_case_labels (const void *p1, const void *p2)
5302 const struct case_info *ci1 = (const struct case_info *) p1;
5303 const struct case_info *ci2 = (const struct case_info *) p2;
5304 int idx1 = ci1->bb->index;
5305 int idx2 = ci2->bb->index;
5307 if (idx1 < idx2)
5308 return -1;
5309 else if (idx1 == idx2)
5311 /* Make sure the default label is first in a group. */
5312 if (!CASE_LOW (ci1->expr))
5313 return -1;
5314 else if (!CASE_LOW (ci2->expr))
5315 return 1;
5316 else
5317 return tree_int_cst_compare (CASE_LOW (ci1->expr),
5318 CASE_LOW (ci2->expr));
5320 else
5321 return 1;
5324 /* Determine whether the outgoing edges of BB should receive an
5325 ASSERT_EXPR for each of the operands of BB's LAST statement.
5326 The last statement of BB must be a SWITCH_EXPR.
5328 If any of the sub-graphs rooted at BB have an interesting use of
5329 the predicate operands, an assert location node is added to the
5330 list of assertions for the corresponding operands. */
5332 static bool
5333 find_switch_asserts (basic_block bb, gimple last)
5335 bool need_assert;
5336 gimple_stmt_iterator bsi;
5337 tree op;
5338 edge e;
5339 struct case_info *ci;
5340 size_t n = gimple_switch_num_labels (last);
5341 #if GCC_VERSION >= 4000
5342 unsigned int idx;
5343 #else
5344 /* Work around GCC 3.4 bug (PR 37086). */
5345 volatile unsigned int idx;
5346 #endif
5348 need_assert = false;
5349 bsi = gsi_for_stmt (last);
5350 op = gimple_switch_index (last);
5351 if (TREE_CODE (op) != SSA_NAME)
5352 return false;
5354 /* Build a vector of case labels sorted by destination label. */
5355 ci = XNEWVEC (struct case_info, n);
5356 for (idx = 0; idx < n; ++idx)
5358 ci[idx].expr = gimple_switch_label (last, idx);
5359 ci[idx].bb = label_to_block (CASE_LABEL (ci[idx].expr));
5361 qsort (ci, n, sizeof (struct case_info), compare_case_labels);
5363 for (idx = 0; idx < n; ++idx)
5365 tree min, max;
5366 tree cl = ci[idx].expr;
5367 basic_block cbb = ci[idx].bb;
5369 min = CASE_LOW (cl);
5370 max = CASE_HIGH (cl);
5372 /* If there are multiple case labels with the same destination
5373 we need to combine them to a single value range for the edge. */
5374 if (idx + 1 < n && cbb == ci[idx + 1].bb)
5376 /* Skip labels until the last of the group. */
5377 do {
5378 ++idx;
5379 } while (idx < n && cbb == ci[idx].bb);
5380 --idx;
5382 /* Pick up the maximum of the case label range. */
5383 if (CASE_HIGH (ci[idx].expr))
5384 max = CASE_HIGH (ci[idx].expr);
5385 else
5386 max = CASE_LOW (ci[idx].expr);
5389 /* Nothing to do if the range includes the default label until we
5390 can register anti-ranges. */
5391 if (min == NULL_TREE)
5392 continue;
5394 /* Find the edge to register the assert expr on. */
5395 e = find_edge (bb, cbb);
5397 /* Register the necessary assertions for the operand in the
5398 SWITCH_EXPR. */
5399 need_assert |= register_edge_assert_for (op, e, bsi,
5400 max ? GE_EXPR : EQ_EXPR,
5402 fold_convert (TREE_TYPE (op),
5403 min));
5404 if (max)
5406 need_assert |= register_edge_assert_for (op, e, bsi, LE_EXPR,
5408 fold_convert (TREE_TYPE (op),
5409 max));
5413 XDELETEVEC (ci);
5414 return need_assert;
5418 /* Traverse all the statements in block BB looking for statements that
5419 may generate useful assertions for the SSA names in their operand.
5420 If a statement produces a useful assertion A for name N_i, then the
5421 list of assertions already generated for N_i is scanned to
5422 determine if A is actually needed.
5424 If N_i already had the assertion A at a location dominating the
5425 current location, then nothing needs to be done. Otherwise, the
5426 new location for A is recorded instead.
5428 1- For every statement S in BB, all the variables used by S are
5429 added to bitmap FOUND_IN_SUBGRAPH.
5431 2- If statement S uses an operand N in a way that exposes a known
5432 value range for N, then if N was not already generated by an
5433 ASSERT_EXPR, create a new assert location for N. For instance,
5434 if N is a pointer and the statement dereferences it, we can
5435 assume that N is not NULL.
5437 3- COND_EXPRs are a special case of #2. We can derive range
5438 information from the predicate but need to insert different
5439 ASSERT_EXPRs for each of the sub-graphs rooted at the
5440 conditional block. If the last statement of BB is a conditional
5441 expression of the form 'X op Y', then
5443 a) Remove X and Y from the set FOUND_IN_SUBGRAPH.
5445 b) If the conditional is the only entry point to the sub-graph
5446 corresponding to the THEN_CLAUSE, recurse into it. On
5447 return, if X and/or Y are marked in FOUND_IN_SUBGRAPH, then
5448 an ASSERT_EXPR is added for the corresponding variable.
5450 c) Repeat step (b) on the ELSE_CLAUSE.
5452 d) Mark X and Y in FOUND_IN_SUBGRAPH.
5454 For instance,
5456 if (a == 9)
5457 b = a;
5458 else
5459 b = c + 1;
5461 In this case, an assertion on the THEN clause is useful to
5462 determine that 'a' is always 9 on that edge. However, an assertion
5463 on the ELSE clause would be unnecessary.
5465 4- If BB does not end in a conditional expression, then we recurse
5466 into BB's dominator children.
5468 At the end of the recursive traversal, every SSA name will have a
5469 list of locations where ASSERT_EXPRs should be added. When a new
5470 location for name N is found, it is registered by calling
5471 register_new_assert_for. That function keeps track of all the
5472 registered assertions to prevent adding unnecessary assertions.
5473 For instance, if a pointer P_4 is dereferenced more than once in a
5474 dominator tree, only the location dominating all the dereference of
5475 P_4 will receive an ASSERT_EXPR.
5477 If this function returns true, then it means that there are names
5478 for which we need to generate ASSERT_EXPRs. Those assertions are
5479 inserted by process_assert_insertions. */
5481 static bool
5482 find_assert_locations_1 (basic_block bb, sbitmap live)
5484 gimple_stmt_iterator si;
5485 gimple last;
5486 bool need_assert;
5488 need_assert = false;
5489 last = last_stmt (bb);
5491 /* If BB's last statement is a conditional statement involving integer
5492 operands, determine if we need to add ASSERT_EXPRs. */
5493 if (last
5494 && gimple_code (last) == GIMPLE_COND
5495 && !fp_predicate (last)
5496 && !ZERO_SSA_OPERANDS (last, SSA_OP_USE))
5497 need_assert |= find_conditional_asserts (bb, last);
5499 /* If BB's last statement is a switch statement involving integer
5500 operands, determine if we need to add ASSERT_EXPRs. */
5501 if (last
5502 && gimple_code (last) == GIMPLE_SWITCH
5503 && !ZERO_SSA_OPERANDS (last, SSA_OP_USE))
5504 need_assert |= find_switch_asserts (bb, last);
5506 /* Traverse all the statements in BB marking used names and looking
5507 for statements that may infer assertions for their used operands. */
5508 for (si = gsi_last_bb (bb); !gsi_end_p (si); gsi_prev (&si))
5510 gimple stmt;
5511 tree op;
5512 ssa_op_iter i;
5514 stmt = gsi_stmt (si);
5516 if (is_gimple_debug (stmt))
5517 continue;
5519 /* See if we can derive an assertion for any of STMT's operands. */
5520 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_USE)
5522 tree value;
5523 enum tree_code comp_code;
5525 /* If op is not live beyond this stmt, do not bother to insert
5526 asserts for it. */
5527 if (!TEST_BIT (live, SSA_NAME_VERSION (op)))
5528 continue;
5530 /* If OP is used in such a way that we can infer a value
5531 range for it, and we don't find a previous assertion for
5532 it, create a new assertion location node for OP. */
5533 if (infer_value_range (stmt, op, &comp_code, &value))
5535 /* If we are able to infer a nonzero value range for OP,
5536 then walk backwards through the use-def chain to see if OP
5537 was set via a typecast.
5539 If so, then we can also infer a nonzero value range
5540 for the operand of the NOP_EXPR. */
5541 if (comp_code == NE_EXPR && integer_zerop (value))
5543 tree t = op;
5544 gimple def_stmt = SSA_NAME_DEF_STMT (t);
5546 while (is_gimple_assign (def_stmt)
5547 && gimple_assign_rhs_code (def_stmt) == NOP_EXPR
5548 && TREE_CODE
5549 (gimple_assign_rhs1 (def_stmt)) == SSA_NAME
5550 && POINTER_TYPE_P
5551 (TREE_TYPE (gimple_assign_rhs1 (def_stmt))))
5553 t = gimple_assign_rhs1 (def_stmt);
5554 def_stmt = SSA_NAME_DEF_STMT (t);
5556 /* Note we want to register the assert for the
5557 operand of the NOP_EXPR after SI, not after the
5558 conversion. */
5559 if (! has_single_use (t))
5561 register_new_assert_for (t, t, comp_code, value,
5562 bb, NULL, si);
5563 need_assert = true;
5568 register_new_assert_for (op, op, comp_code, value, bb, NULL, si);
5569 need_assert = true;
5573 /* Update live. */
5574 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_USE)
5575 SET_BIT (live, SSA_NAME_VERSION (op));
5576 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_DEF)
5577 RESET_BIT (live, SSA_NAME_VERSION (op));
5580 /* Traverse all PHI nodes in BB, updating live. */
5581 for (si = gsi_start_phis (bb); !gsi_end_p(si); gsi_next (&si))
5583 use_operand_p arg_p;
5584 ssa_op_iter i;
5585 gimple phi = gsi_stmt (si);
5586 tree res = gimple_phi_result (phi);
5588 if (virtual_operand_p (res))
5589 continue;
5591 FOR_EACH_PHI_ARG (arg_p, phi, i, SSA_OP_USE)
5593 tree arg = USE_FROM_PTR (arg_p);
5594 if (TREE_CODE (arg) == SSA_NAME)
5595 SET_BIT (live, SSA_NAME_VERSION (arg));
5598 RESET_BIT (live, SSA_NAME_VERSION (res));
5601 return need_assert;
5604 /* Do an RPO walk over the function computing SSA name liveness
5605 on-the-fly and deciding on assert expressions to insert.
5606 Returns true if there are assert expressions to be inserted. */
5608 static bool
5609 find_assert_locations (void)
5611 int *rpo = XNEWVEC (int, last_basic_block);
5612 int *bb_rpo = XNEWVEC (int, last_basic_block);
5613 int *last_rpo = XCNEWVEC (int, last_basic_block);
5614 int rpo_cnt, i;
5615 bool need_asserts;
5617 live = XCNEWVEC (sbitmap, last_basic_block);
5618 rpo_cnt = pre_and_rev_post_order_compute (NULL, rpo, false);
5619 for (i = 0; i < rpo_cnt; ++i)
5620 bb_rpo[rpo[i]] = i;
5622 need_asserts = false;
5623 for (i = rpo_cnt - 1; i >= 0; --i)
5625 basic_block bb = BASIC_BLOCK (rpo[i]);
5626 edge e;
5627 edge_iterator ei;
5629 if (!live[rpo[i]])
5631 live[rpo[i]] = sbitmap_alloc (num_ssa_names);
5632 sbitmap_zero (live[rpo[i]]);
5635 /* Process BB and update the live information with uses in
5636 this block. */
5637 need_asserts |= find_assert_locations_1 (bb, live[rpo[i]]);
5639 /* Merge liveness into the predecessor blocks and free it. */
5640 if (!sbitmap_empty_p (live[rpo[i]]))
5642 int pred_rpo = i;
5643 FOR_EACH_EDGE (e, ei, bb->preds)
5645 int pred = e->src->index;
5646 if ((e->flags & EDGE_DFS_BACK) || pred == ENTRY_BLOCK)
5647 continue;
5649 if (!live[pred])
5651 live[pred] = sbitmap_alloc (num_ssa_names);
5652 sbitmap_zero (live[pred]);
5654 sbitmap_a_or_b (live[pred], live[pred], live[rpo[i]]);
5656 if (bb_rpo[pred] < pred_rpo)
5657 pred_rpo = bb_rpo[pred];
5660 /* Record the RPO number of the last visited block that needs
5661 live information from this block. */
5662 last_rpo[rpo[i]] = pred_rpo;
5664 else
5666 sbitmap_free (live[rpo[i]]);
5667 live[rpo[i]] = NULL;
5670 /* We can free all successors live bitmaps if all their
5671 predecessors have been visited already. */
5672 FOR_EACH_EDGE (e, ei, bb->succs)
5673 if (last_rpo[e->dest->index] == i
5674 && live[e->dest->index])
5676 sbitmap_free (live[e->dest->index]);
5677 live[e->dest->index] = NULL;
5681 XDELETEVEC (rpo);
5682 XDELETEVEC (bb_rpo);
5683 XDELETEVEC (last_rpo);
5684 for (i = 0; i < last_basic_block; ++i)
5685 if (live[i])
5686 sbitmap_free (live[i]);
5687 XDELETEVEC (live);
5689 return need_asserts;
5692 /* Create an ASSERT_EXPR for NAME and insert it in the location
5693 indicated by LOC. Return true if we made any edge insertions. */
5695 static bool
5696 process_assert_insertions_for (tree name, assert_locus_t loc)
5698 /* Build the comparison expression NAME_i COMP_CODE VAL. */
5699 gimple stmt;
5700 tree cond;
5701 gimple assert_stmt;
5702 edge_iterator ei;
5703 edge e;
5705 /* If we have X <=> X do not insert an assert expr for that. */
5706 if (loc->expr == loc->val)
5707 return false;
5709 cond = build2 (loc->comp_code, boolean_type_node, loc->expr, loc->val);
5710 assert_stmt = build_assert_expr_for (cond, name);
5711 if (loc->e)
5713 /* We have been asked to insert the assertion on an edge. This
5714 is used only by COND_EXPR and SWITCH_EXPR assertions. */
5715 gcc_checking_assert (gimple_code (gsi_stmt (loc->si)) == GIMPLE_COND
5716 || (gimple_code (gsi_stmt (loc->si))
5717 == GIMPLE_SWITCH));
5719 gsi_insert_on_edge (loc->e, assert_stmt);
5720 return true;
5723 /* Otherwise, we can insert right after LOC->SI iff the
5724 statement must not be the last statement in the block. */
5725 stmt = gsi_stmt (loc->si);
5726 if (!stmt_ends_bb_p (stmt))
5728 gsi_insert_after (&loc->si, assert_stmt, GSI_SAME_STMT);
5729 return false;
5732 /* If STMT must be the last statement in BB, we can only insert new
5733 assertions on the non-abnormal edge out of BB. Note that since
5734 STMT is not control flow, there may only be one non-abnormal edge
5735 out of BB. */
5736 FOR_EACH_EDGE (e, ei, loc->bb->succs)
5737 if (!(e->flags & EDGE_ABNORMAL))
5739 gsi_insert_on_edge (e, assert_stmt);
5740 return true;
5743 gcc_unreachable ();
5747 /* Process all the insertions registered for every name N_i registered
5748 in NEED_ASSERT_FOR. The list of assertions to be inserted are
5749 found in ASSERTS_FOR[i]. */
5751 static void
5752 process_assert_insertions (void)
5754 unsigned i;
5755 bitmap_iterator bi;
5756 bool update_edges_p = false;
5757 int num_asserts = 0;
5759 if (dump_file && (dump_flags & TDF_DETAILS))
5760 dump_all_asserts (dump_file);
5762 EXECUTE_IF_SET_IN_BITMAP (need_assert_for, 0, i, bi)
5764 assert_locus_t loc = asserts_for[i];
5765 gcc_assert (loc);
5767 while (loc)
5769 assert_locus_t next = loc->next;
5770 update_edges_p |= process_assert_insertions_for (ssa_name (i), loc);
5771 free (loc);
5772 loc = next;
5773 num_asserts++;
5777 if (update_edges_p)
5778 gsi_commit_edge_inserts ();
5780 statistics_counter_event (cfun, "Number of ASSERT_EXPR expressions inserted",
5781 num_asserts);
5785 /* Traverse the flowgraph looking for conditional jumps to insert range
5786 expressions. These range expressions are meant to provide information
5787 to optimizations that need to reason in terms of value ranges. They
5788 will not be expanded into RTL. For instance, given:
5790 x = ...
5791 y = ...
5792 if (x < y)
5793 y = x - 2;
5794 else
5795 x = y + 3;
5797 this pass will transform the code into:
5799 x = ...
5800 y = ...
5801 if (x < y)
5803 x = ASSERT_EXPR <x, x < y>
5804 y = x - 2
5806 else
5808 y = ASSERT_EXPR <y, x <= y>
5809 x = y + 3
5812 The idea is that once copy and constant propagation have run, other
5813 optimizations will be able to determine what ranges of values can 'x'
5814 take in different paths of the code, simply by checking the reaching
5815 definition of 'x'. */
5817 static void
5818 insert_range_assertions (void)
5820 need_assert_for = BITMAP_ALLOC (NULL);
5821 asserts_for = XCNEWVEC (assert_locus_t, num_ssa_names);
5823 calculate_dominance_info (CDI_DOMINATORS);
5825 if (find_assert_locations ())
5827 process_assert_insertions ();
5828 update_ssa (TODO_update_ssa_no_phi);
5831 if (dump_file && (dump_flags & TDF_DETAILS))
5833 fprintf (dump_file, "\nSSA form after inserting ASSERT_EXPRs\n");
5834 dump_function_to_file (current_function_decl, dump_file, dump_flags);
5837 free (asserts_for);
5838 BITMAP_FREE (need_assert_for);
5841 /* Checks one ARRAY_REF in REF, located at LOCUS. Ignores flexible arrays
5842 and "struct" hacks. If VRP can determine that the
5843 array subscript is a constant, check if it is outside valid
5844 range. If the array subscript is a RANGE, warn if it is
5845 non-overlapping with valid range.
5846 IGNORE_OFF_BY_ONE is true if the ARRAY_REF is inside a ADDR_EXPR. */
5848 static void
5849 check_array_ref (location_t location, tree ref, bool ignore_off_by_one)
5851 value_range_t* vr = NULL;
5852 tree low_sub, up_sub;
5853 tree low_bound, up_bound, up_bound_p1;
5854 tree base;
5856 if (TREE_NO_WARNING (ref))
5857 return;
5859 low_sub = up_sub = TREE_OPERAND (ref, 1);
5860 up_bound = array_ref_up_bound (ref);
5862 /* Can not check flexible arrays. */
5863 if (!up_bound
5864 || TREE_CODE (up_bound) != INTEGER_CST)
5865 return;
5867 /* Accesses to trailing arrays via pointers may access storage
5868 beyond the types array bounds. */
5869 base = get_base_address (ref);
5870 if (base && TREE_CODE (base) == MEM_REF)
5872 tree cref, next = NULL_TREE;
5874 if (TREE_CODE (TREE_OPERAND (ref, 0)) != COMPONENT_REF)
5875 return;
5877 cref = TREE_OPERAND (ref, 0);
5878 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (cref, 0))) == RECORD_TYPE)
5879 for (next = DECL_CHAIN (TREE_OPERAND (cref, 1));
5880 next && TREE_CODE (next) != FIELD_DECL;
5881 next = DECL_CHAIN (next))
5884 /* If this is the last field in a struct type or a field in a
5885 union type do not warn. */
5886 if (!next)
5887 return;
5890 low_bound = array_ref_low_bound (ref);
5891 up_bound_p1 = int_const_binop (PLUS_EXPR, up_bound, integer_one_node);
5893 if (TREE_CODE (low_sub) == SSA_NAME)
5895 vr = get_value_range (low_sub);
5896 if (vr->type == VR_RANGE || vr->type == VR_ANTI_RANGE)
5898 low_sub = vr->type == VR_RANGE ? vr->max : vr->min;
5899 up_sub = vr->type == VR_RANGE ? vr->min : vr->max;
5903 if (vr && vr->type == VR_ANTI_RANGE)
5905 if (TREE_CODE (up_sub) == INTEGER_CST
5906 && tree_int_cst_lt (up_bound, up_sub)
5907 && TREE_CODE (low_sub) == INTEGER_CST
5908 && tree_int_cst_lt (low_sub, low_bound))
5910 warning_at (location, OPT_Warray_bounds,
5911 "array subscript is outside array bounds");
5912 TREE_NO_WARNING (ref) = 1;
5915 else if (TREE_CODE (up_sub) == INTEGER_CST
5916 && (ignore_off_by_one
5917 ? (tree_int_cst_lt (up_bound, up_sub)
5918 && !tree_int_cst_equal (up_bound_p1, up_sub))
5919 : (tree_int_cst_lt (up_bound, up_sub)
5920 || tree_int_cst_equal (up_bound_p1, up_sub))))
5922 warning_at (location, OPT_Warray_bounds,
5923 "array subscript is above array bounds");
5924 TREE_NO_WARNING (ref) = 1;
5926 else if (TREE_CODE (low_sub) == INTEGER_CST
5927 && tree_int_cst_lt (low_sub, low_bound))
5929 warning_at (location, OPT_Warray_bounds,
5930 "array subscript is below array bounds");
5931 TREE_NO_WARNING (ref) = 1;
5935 /* Searches if the expr T, located at LOCATION computes
5936 address of an ARRAY_REF, and call check_array_ref on it. */
5938 static void
5939 search_for_addr_array (tree t, location_t location)
5941 while (TREE_CODE (t) == SSA_NAME)
5943 gimple g = SSA_NAME_DEF_STMT (t);
5945 if (gimple_code (g) != GIMPLE_ASSIGN)
5946 return;
5948 if (get_gimple_rhs_class (gimple_assign_rhs_code (g))
5949 != GIMPLE_SINGLE_RHS)
5950 return;
5952 t = gimple_assign_rhs1 (g);
5956 /* We are only interested in addresses of ARRAY_REF's. */
5957 if (TREE_CODE (t) != ADDR_EXPR)
5958 return;
5960 /* Check each ARRAY_REFs in the reference chain. */
5963 if (TREE_CODE (t) == ARRAY_REF)
5964 check_array_ref (location, t, true /*ignore_off_by_one*/);
5966 t = TREE_OPERAND (t, 0);
5968 while (handled_component_p (t));
5970 if (TREE_CODE (t) == MEM_REF
5971 && TREE_CODE (TREE_OPERAND (t, 0)) == ADDR_EXPR
5972 && !TREE_NO_WARNING (t))
5974 tree tem = TREE_OPERAND (TREE_OPERAND (t, 0), 0);
5975 tree low_bound, up_bound, el_sz;
5976 double_int idx;
5977 if (TREE_CODE (TREE_TYPE (tem)) != ARRAY_TYPE
5978 || TREE_CODE (TREE_TYPE (TREE_TYPE (tem))) == ARRAY_TYPE
5979 || !TYPE_DOMAIN (TREE_TYPE (tem)))
5980 return;
5982 low_bound = TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (tem)));
5983 up_bound = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (tem)));
5984 el_sz = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (tem)));
5985 if (!low_bound
5986 || TREE_CODE (low_bound) != INTEGER_CST
5987 || !up_bound
5988 || TREE_CODE (up_bound) != INTEGER_CST
5989 || !el_sz
5990 || TREE_CODE (el_sz) != INTEGER_CST)
5991 return;
5993 idx = mem_ref_offset (t);
5994 idx = idx.sdiv (tree_to_double_int (el_sz), TRUNC_DIV_EXPR);
5995 if (idx.slt (double_int_zero))
5997 warning_at (location, OPT_Warray_bounds,
5998 "array subscript is below array bounds");
5999 TREE_NO_WARNING (t) = 1;
6001 else if (idx.sgt (tree_to_double_int (up_bound)
6002 - tree_to_double_int (low_bound)
6003 + double_int_one))
6005 warning_at (location, OPT_Warray_bounds,
6006 "array subscript is above array bounds");
6007 TREE_NO_WARNING (t) = 1;
6012 /* walk_tree() callback that checks if *TP is
6013 an ARRAY_REF inside an ADDR_EXPR (in which an array
6014 subscript one outside the valid range is allowed). Call
6015 check_array_ref for each ARRAY_REF found. The location is
6016 passed in DATA. */
6018 static tree
6019 check_array_bounds (tree *tp, int *walk_subtree, void *data)
6021 tree t = *tp;
6022 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
6023 location_t location;
6025 if (EXPR_HAS_LOCATION (t))
6026 location = EXPR_LOCATION (t);
6027 else
6029 location_t *locp = (location_t *) wi->info;
6030 location = *locp;
6033 *walk_subtree = TRUE;
6035 if (TREE_CODE (t) == ARRAY_REF)
6036 check_array_ref (location, t, false /*ignore_off_by_one*/);
6038 if (TREE_CODE (t) == MEM_REF
6039 || (TREE_CODE (t) == RETURN_EXPR && TREE_OPERAND (t, 0)))
6040 search_for_addr_array (TREE_OPERAND (t, 0), location);
6042 if (TREE_CODE (t) == ADDR_EXPR)
6043 *walk_subtree = FALSE;
6045 return NULL_TREE;
6048 /* Walk over all statements of all reachable BBs and call check_array_bounds
6049 on them. */
6051 static void
6052 check_all_array_refs (void)
6054 basic_block bb;
6055 gimple_stmt_iterator si;
6057 FOR_EACH_BB (bb)
6059 edge_iterator ei;
6060 edge e;
6061 bool executable = false;
6063 /* Skip blocks that were found to be unreachable. */
6064 FOR_EACH_EDGE (e, ei, bb->preds)
6065 executable |= !!(e->flags & EDGE_EXECUTABLE);
6066 if (!executable)
6067 continue;
6069 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
6071 gimple stmt = gsi_stmt (si);
6072 struct walk_stmt_info wi;
6073 if (!gimple_has_location (stmt))
6074 continue;
6076 if (is_gimple_call (stmt))
6078 size_t i;
6079 size_t n = gimple_call_num_args (stmt);
6080 for (i = 0; i < n; i++)
6082 tree arg = gimple_call_arg (stmt, i);
6083 search_for_addr_array (arg, gimple_location (stmt));
6086 else
6088 memset (&wi, 0, sizeof (wi));
6089 wi.info = CONST_CAST (void *, (const void *)
6090 gimple_location_ptr (stmt));
6092 walk_gimple_op (gsi_stmt (si),
6093 check_array_bounds,
6094 &wi);
6100 /* Convert range assertion expressions into the implied copies and
6101 copy propagate away the copies. Doing the trivial copy propagation
6102 here avoids the need to run the full copy propagation pass after
6103 VRP.
6105 FIXME, this will eventually lead to copy propagation removing the
6106 names that had useful range information attached to them. For
6107 instance, if we had the assertion N_i = ASSERT_EXPR <N_j, N_j > 3>,
6108 then N_i will have the range [3, +INF].
6110 However, by converting the assertion into the implied copy
6111 operation N_i = N_j, we will then copy-propagate N_j into the uses
6112 of N_i and lose the range information. We may want to hold on to
6113 ASSERT_EXPRs a little while longer as the ranges could be used in
6114 things like jump threading.
6116 The problem with keeping ASSERT_EXPRs around is that passes after
6117 VRP need to handle them appropriately.
6119 Another approach would be to make the range information a first
6120 class property of the SSA_NAME so that it can be queried from
6121 any pass. This is made somewhat more complex by the need for
6122 multiple ranges to be associated with one SSA_NAME. */
6124 static void
6125 remove_range_assertions (void)
6127 basic_block bb;
6128 gimple_stmt_iterator si;
6130 /* Note that the BSI iterator bump happens at the bottom of the
6131 loop and no bump is necessary if we're removing the statement
6132 referenced by the current BSI. */
6133 FOR_EACH_BB (bb)
6134 for (si = gsi_start_bb (bb); !gsi_end_p (si);)
6136 gimple stmt = gsi_stmt (si);
6137 gimple use_stmt;
6139 if (is_gimple_assign (stmt)
6140 && gimple_assign_rhs_code (stmt) == ASSERT_EXPR)
6142 tree rhs = gimple_assign_rhs1 (stmt);
6143 tree var;
6144 tree cond = fold (ASSERT_EXPR_COND (rhs));
6145 use_operand_p use_p;
6146 imm_use_iterator iter;
6148 gcc_assert (cond != boolean_false_node);
6150 /* Propagate the RHS into every use of the LHS. */
6151 var = ASSERT_EXPR_VAR (rhs);
6152 FOR_EACH_IMM_USE_STMT (use_stmt, iter,
6153 gimple_assign_lhs (stmt))
6154 FOR_EACH_IMM_USE_ON_STMT (use_p, iter)
6156 SET_USE (use_p, var);
6157 gcc_assert (TREE_CODE (var) == SSA_NAME);
6160 /* And finally, remove the copy, it is not needed. */
6161 gsi_remove (&si, true);
6162 release_defs (stmt);
6164 else
6165 gsi_next (&si);
6170 /* Return true if STMT is interesting for VRP. */
6172 static bool
6173 stmt_interesting_for_vrp (gimple stmt)
6175 if (gimple_code (stmt) == GIMPLE_PHI)
6177 tree res = gimple_phi_result (stmt);
6178 return (!virtual_operand_p (res)
6179 && (INTEGRAL_TYPE_P (TREE_TYPE (res))
6180 || POINTER_TYPE_P (TREE_TYPE (res))));
6182 else if (is_gimple_assign (stmt) || is_gimple_call (stmt))
6184 tree lhs = gimple_get_lhs (stmt);
6186 /* In general, assignments with virtual operands are not useful
6187 for deriving ranges, with the obvious exception of calls to
6188 builtin functions. */
6189 if (lhs && TREE_CODE (lhs) == SSA_NAME
6190 && (INTEGRAL_TYPE_P (TREE_TYPE (lhs))
6191 || POINTER_TYPE_P (TREE_TYPE (lhs)))
6192 && ((is_gimple_call (stmt)
6193 && gimple_call_fndecl (stmt) != NULL_TREE
6194 && DECL_BUILT_IN (gimple_call_fndecl (stmt)))
6195 || !gimple_vuse (stmt)))
6196 return true;
6198 else if (gimple_code (stmt) == GIMPLE_COND
6199 || gimple_code (stmt) == GIMPLE_SWITCH)
6200 return true;
6202 return false;
6206 /* Initialize local data structures for VRP. */
6208 static void
6209 vrp_initialize (void)
6211 basic_block bb;
6213 values_propagated = false;
6214 num_vr_values = num_ssa_names;
6215 vr_value = XCNEWVEC (value_range_t *, num_vr_values);
6216 vr_phi_edge_counts = XCNEWVEC (int, num_ssa_names);
6218 FOR_EACH_BB (bb)
6220 gimple_stmt_iterator si;
6222 for (si = gsi_start_phis (bb); !gsi_end_p (si); gsi_next (&si))
6224 gimple phi = gsi_stmt (si);
6225 if (!stmt_interesting_for_vrp (phi))
6227 tree lhs = PHI_RESULT (phi);
6228 set_value_range_to_varying (get_value_range (lhs));
6229 prop_set_simulate_again (phi, false);
6231 else
6232 prop_set_simulate_again (phi, true);
6235 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
6237 gimple stmt = gsi_stmt (si);
6239 /* If the statement is a control insn, then we do not
6240 want to avoid simulating the statement once. Failure
6241 to do so means that those edges will never get added. */
6242 if (stmt_ends_bb_p (stmt))
6243 prop_set_simulate_again (stmt, true);
6244 else if (!stmt_interesting_for_vrp (stmt))
6246 ssa_op_iter i;
6247 tree def;
6248 FOR_EACH_SSA_TREE_OPERAND (def, stmt, i, SSA_OP_DEF)
6249 set_value_range_to_varying (get_value_range (def));
6250 prop_set_simulate_again (stmt, false);
6252 else
6253 prop_set_simulate_again (stmt, true);
6258 /* Return the singleton value-range for NAME or NAME. */
6260 static inline tree
6261 vrp_valueize (tree name)
6263 if (TREE_CODE (name) == SSA_NAME)
6265 value_range_t *vr = get_value_range (name);
6266 if (vr->type == VR_RANGE
6267 && (vr->min == vr->max
6268 || operand_equal_p (vr->min, vr->max, 0)))
6269 return vr->min;
6271 return name;
6274 /* Visit assignment STMT. If it produces an interesting range, record
6275 the SSA name in *OUTPUT_P. */
6277 static enum ssa_prop_result
6278 vrp_visit_assignment_or_call (gimple stmt, tree *output_p)
6280 tree def, lhs;
6281 ssa_op_iter iter;
6282 enum gimple_code code = gimple_code (stmt);
6283 lhs = gimple_get_lhs (stmt);
6285 /* We only keep track of ranges in integral and pointer types. */
6286 if (TREE_CODE (lhs) == SSA_NAME
6287 && ((INTEGRAL_TYPE_P (TREE_TYPE (lhs))
6288 /* It is valid to have NULL MIN/MAX values on a type. See
6289 build_range_type. */
6290 && TYPE_MIN_VALUE (TREE_TYPE (lhs))
6291 && TYPE_MAX_VALUE (TREE_TYPE (lhs)))
6292 || POINTER_TYPE_P (TREE_TYPE (lhs))))
6294 value_range_t new_vr = VR_INITIALIZER;
6296 /* Try folding the statement to a constant first. */
6297 tree tem = gimple_fold_stmt_to_constant (stmt, vrp_valueize);
6298 if (tem && !is_overflow_infinity (tem))
6299 set_value_range (&new_vr, VR_RANGE, tem, tem, NULL);
6300 /* Then dispatch to value-range extracting functions. */
6301 else if (code == GIMPLE_CALL)
6302 extract_range_basic (&new_vr, stmt);
6303 else
6304 extract_range_from_assignment (&new_vr, stmt);
6306 if (update_value_range (lhs, &new_vr))
6308 *output_p = lhs;
6310 if (dump_file && (dump_flags & TDF_DETAILS))
6312 fprintf (dump_file, "Found new range for ");
6313 print_generic_expr (dump_file, lhs, 0);
6314 fprintf (dump_file, ": ");
6315 dump_value_range (dump_file, &new_vr);
6316 fprintf (dump_file, "\n\n");
6319 if (new_vr.type == VR_VARYING)
6320 return SSA_PROP_VARYING;
6322 return SSA_PROP_INTERESTING;
6325 return SSA_PROP_NOT_INTERESTING;
6328 /* Every other statement produces no useful ranges. */
6329 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_DEF)
6330 set_value_range_to_varying (get_value_range (def));
6332 return SSA_PROP_VARYING;
6335 /* Helper that gets the value range of the SSA_NAME with version I
6336 or a symbolic range containing the SSA_NAME only if the value range
6337 is varying or undefined. */
6339 static inline value_range_t
6340 get_vr_for_comparison (int i)
6342 value_range_t vr = *get_value_range (ssa_name (i));
6344 /* If name N_i does not have a valid range, use N_i as its own
6345 range. This allows us to compare against names that may
6346 have N_i in their ranges. */
6347 if (vr.type == VR_VARYING || vr.type == VR_UNDEFINED)
6349 vr.type = VR_RANGE;
6350 vr.min = ssa_name (i);
6351 vr.max = ssa_name (i);
6354 return vr;
6357 /* Compare all the value ranges for names equivalent to VAR with VAL
6358 using comparison code COMP. Return the same value returned by
6359 compare_range_with_value, including the setting of
6360 *STRICT_OVERFLOW_P. */
6362 static tree
6363 compare_name_with_value (enum tree_code comp, tree var, tree val,
6364 bool *strict_overflow_p)
6366 bitmap_iterator bi;
6367 unsigned i;
6368 bitmap e;
6369 tree retval, t;
6370 int used_strict_overflow;
6371 bool sop;
6372 value_range_t equiv_vr;
6374 /* Get the set of equivalences for VAR. */
6375 e = get_value_range (var)->equiv;
6377 /* Start at -1. Set it to 0 if we do a comparison without relying
6378 on overflow, or 1 if all comparisons rely on overflow. */
6379 used_strict_overflow = -1;
6381 /* Compare vars' value range with val. */
6382 equiv_vr = get_vr_for_comparison (SSA_NAME_VERSION (var));
6383 sop = false;
6384 retval = compare_range_with_value (comp, &equiv_vr, val, &sop);
6385 if (retval)
6386 used_strict_overflow = sop ? 1 : 0;
6388 /* If the equiv set is empty we have done all work we need to do. */
6389 if (e == NULL)
6391 if (retval
6392 && used_strict_overflow > 0)
6393 *strict_overflow_p = true;
6394 return retval;
6397 EXECUTE_IF_SET_IN_BITMAP (e, 0, i, bi)
6399 equiv_vr = get_vr_for_comparison (i);
6400 sop = false;
6401 t = compare_range_with_value (comp, &equiv_vr, val, &sop);
6402 if (t)
6404 /* If we get different answers from different members
6405 of the equivalence set this check must be in a dead
6406 code region. Folding it to a trap representation
6407 would be correct here. For now just return don't-know. */
6408 if (retval != NULL
6409 && t != retval)
6411 retval = NULL_TREE;
6412 break;
6414 retval = t;
6416 if (!sop)
6417 used_strict_overflow = 0;
6418 else if (used_strict_overflow < 0)
6419 used_strict_overflow = 1;
6423 if (retval
6424 && used_strict_overflow > 0)
6425 *strict_overflow_p = true;
6427 return retval;
6431 /* Given a comparison code COMP and names N1 and N2, compare all the
6432 ranges equivalent to N1 against all the ranges equivalent to N2
6433 to determine the value of N1 COMP N2. Return the same value
6434 returned by compare_ranges. Set *STRICT_OVERFLOW_P to indicate
6435 whether we relied on an overflow infinity in the comparison. */
6438 static tree
6439 compare_names (enum tree_code comp, tree n1, tree n2,
6440 bool *strict_overflow_p)
6442 tree t, retval;
6443 bitmap e1, e2;
6444 bitmap_iterator bi1, bi2;
6445 unsigned i1, i2;
6446 int used_strict_overflow;
6447 static bitmap_obstack *s_obstack = NULL;
6448 static bitmap s_e1 = NULL, s_e2 = NULL;
6450 /* Compare the ranges of every name equivalent to N1 against the
6451 ranges of every name equivalent to N2. */
6452 e1 = get_value_range (n1)->equiv;
6453 e2 = get_value_range (n2)->equiv;
6455 /* Use the fake bitmaps if e1 or e2 are not available. */
6456 if (s_obstack == NULL)
6458 s_obstack = XNEW (bitmap_obstack);
6459 bitmap_obstack_initialize (s_obstack);
6460 s_e1 = BITMAP_ALLOC (s_obstack);
6461 s_e2 = BITMAP_ALLOC (s_obstack);
6463 if (e1 == NULL)
6464 e1 = s_e1;
6465 if (e2 == NULL)
6466 e2 = s_e2;
6468 /* Add N1 and N2 to their own set of equivalences to avoid
6469 duplicating the body of the loop just to check N1 and N2
6470 ranges. */
6471 bitmap_set_bit (e1, SSA_NAME_VERSION (n1));
6472 bitmap_set_bit (e2, SSA_NAME_VERSION (n2));
6474 /* If the equivalence sets have a common intersection, then the two
6475 names can be compared without checking their ranges. */
6476 if (bitmap_intersect_p (e1, e2))
6478 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
6479 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
6481 return (comp == EQ_EXPR || comp == GE_EXPR || comp == LE_EXPR)
6482 ? boolean_true_node
6483 : boolean_false_node;
6486 /* Start at -1. Set it to 0 if we do a comparison without relying
6487 on overflow, or 1 if all comparisons rely on overflow. */
6488 used_strict_overflow = -1;
6490 /* Otherwise, compare all the equivalent ranges. First, add N1 and
6491 N2 to their own set of equivalences to avoid duplicating the body
6492 of the loop just to check N1 and N2 ranges. */
6493 EXECUTE_IF_SET_IN_BITMAP (e1, 0, i1, bi1)
6495 value_range_t vr1 = get_vr_for_comparison (i1);
6497 t = retval = NULL_TREE;
6498 EXECUTE_IF_SET_IN_BITMAP (e2, 0, i2, bi2)
6500 bool sop = false;
6502 value_range_t vr2 = get_vr_for_comparison (i2);
6504 t = compare_ranges (comp, &vr1, &vr2, &sop);
6505 if (t)
6507 /* If we get different answers from different members
6508 of the equivalence set this check must be in a dead
6509 code region. Folding it to a trap representation
6510 would be correct here. For now just return don't-know. */
6511 if (retval != NULL
6512 && t != retval)
6514 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
6515 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
6516 return NULL_TREE;
6518 retval = t;
6520 if (!sop)
6521 used_strict_overflow = 0;
6522 else if (used_strict_overflow < 0)
6523 used_strict_overflow = 1;
6527 if (retval)
6529 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
6530 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
6531 if (used_strict_overflow > 0)
6532 *strict_overflow_p = true;
6533 return retval;
6537 /* None of the equivalent ranges are useful in computing this
6538 comparison. */
6539 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
6540 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
6541 return NULL_TREE;
6544 /* Helper function for vrp_evaluate_conditional_warnv. */
6546 static tree
6547 vrp_evaluate_conditional_warnv_with_ops_using_ranges (enum tree_code code,
6548 tree op0, tree op1,
6549 bool * strict_overflow_p)
6551 value_range_t *vr0, *vr1;
6553 vr0 = (TREE_CODE (op0) == SSA_NAME) ? get_value_range (op0) : NULL;
6554 vr1 = (TREE_CODE (op1) == SSA_NAME) ? get_value_range (op1) : NULL;
6556 if (vr0 && vr1)
6557 return compare_ranges (code, vr0, vr1, strict_overflow_p);
6558 else if (vr0 && vr1 == NULL)
6559 return compare_range_with_value (code, vr0, op1, strict_overflow_p);
6560 else if (vr0 == NULL && vr1)
6561 return (compare_range_with_value
6562 (swap_tree_comparison (code), vr1, op0, strict_overflow_p));
6563 return NULL;
6566 /* Helper function for vrp_evaluate_conditional_warnv. */
6568 static tree
6569 vrp_evaluate_conditional_warnv_with_ops (enum tree_code code, tree op0,
6570 tree op1, bool use_equiv_p,
6571 bool *strict_overflow_p, bool *only_ranges)
6573 tree ret;
6574 if (only_ranges)
6575 *only_ranges = true;
6577 /* We only deal with integral and pointer types. */
6578 if (!INTEGRAL_TYPE_P (TREE_TYPE (op0))
6579 && !POINTER_TYPE_P (TREE_TYPE (op0)))
6580 return NULL_TREE;
6582 if (use_equiv_p)
6584 if (only_ranges
6585 && (ret = vrp_evaluate_conditional_warnv_with_ops_using_ranges
6586 (code, op0, op1, strict_overflow_p)))
6587 return ret;
6588 *only_ranges = false;
6589 if (TREE_CODE (op0) == SSA_NAME && TREE_CODE (op1) == SSA_NAME)
6590 return compare_names (code, op0, op1, strict_overflow_p);
6591 else if (TREE_CODE (op0) == SSA_NAME)
6592 return compare_name_with_value (code, op0, op1, strict_overflow_p);
6593 else if (TREE_CODE (op1) == SSA_NAME)
6594 return (compare_name_with_value
6595 (swap_tree_comparison (code), op1, op0, strict_overflow_p));
6597 else
6598 return vrp_evaluate_conditional_warnv_with_ops_using_ranges (code, op0, op1,
6599 strict_overflow_p);
6600 return NULL_TREE;
6603 /* Given (CODE OP0 OP1) within STMT, try to simplify it based on value range
6604 information. Return NULL if the conditional can not be evaluated.
6605 The ranges of all the names equivalent with the operands in COND
6606 will be used when trying to compute the value. If the result is
6607 based on undefined signed overflow, issue a warning if
6608 appropriate. */
6610 static tree
6611 vrp_evaluate_conditional (enum tree_code code, tree op0, tree op1, gimple stmt)
6613 bool sop;
6614 tree ret;
6615 bool only_ranges;
6617 /* Some passes and foldings leak constants with overflow flag set
6618 into the IL. Avoid doing wrong things with these and bail out. */
6619 if ((TREE_CODE (op0) == INTEGER_CST
6620 && TREE_OVERFLOW (op0))
6621 || (TREE_CODE (op1) == INTEGER_CST
6622 && TREE_OVERFLOW (op1)))
6623 return NULL_TREE;
6625 sop = false;
6626 ret = vrp_evaluate_conditional_warnv_with_ops (code, op0, op1, true, &sop,
6627 &only_ranges);
6629 if (ret && sop)
6631 enum warn_strict_overflow_code wc;
6632 const char* warnmsg;
6634 if (is_gimple_min_invariant (ret))
6636 wc = WARN_STRICT_OVERFLOW_CONDITIONAL;
6637 warnmsg = G_("assuming signed overflow does not occur when "
6638 "simplifying conditional to constant");
6640 else
6642 wc = WARN_STRICT_OVERFLOW_COMPARISON;
6643 warnmsg = G_("assuming signed overflow does not occur when "
6644 "simplifying conditional");
6647 if (issue_strict_overflow_warning (wc))
6649 location_t location;
6651 if (!gimple_has_location (stmt))
6652 location = input_location;
6653 else
6654 location = gimple_location (stmt);
6655 warning_at (location, OPT_Wstrict_overflow, "%s", warnmsg);
6659 if (warn_type_limits
6660 && ret && only_ranges
6661 && TREE_CODE_CLASS (code) == tcc_comparison
6662 && TREE_CODE (op0) == SSA_NAME)
6664 /* If the comparison is being folded and the operand on the LHS
6665 is being compared against a constant value that is outside of
6666 the natural range of OP0's type, then the predicate will
6667 always fold regardless of the value of OP0. If -Wtype-limits
6668 was specified, emit a warning. */
6669 tree type = TREE_TYPE (op0);
6670 value_range_t *vr0 = get_value_range (op0);
6672 if (vr0->type != VR_VARYING
6673 && INTEGRAL_TYPE_P (type)
6674 && vrp_val_is_min (vr0->min)
6675 && vrp_val_is_max (vr0->max)
6676 && is_gimple_min_invariant (op1))
6678 location_t location;
6680 if (!gimple_has_location (stmt))
6681 location = input_location;
6682 else
6683 location = gimple_location (stmt);
6685 warning_at (location, OPT_Wtype_limits,
6686 integer_zerop (ret)
6687 ? G_("comparison always false "
6688 "due to limited range of data type")
6689 : G_("comparison always true "
6690 "due to limited range of data type"));
6694 return ret;
6698 /* Visit conditional statement STMT. If we can determine which edge
6699 will be taken out of STMT's basic block, record it in
6700 *TAKEN_EDGE_P and return SSA_PROP_INTERESTING. Otherwise, return
6701 SSA_PROP_VARYING. */
6703 static enum ssa_prop_result
6704 vrp_visit_cond_stmt (gimple stmt, edge *taken_edge_p)
6706 tree val;
6707 bool sop;
6709 *taken_edge_p = NULL;
6711 if (dump_file && (dump_flags & TDF_DETAILS))
6713 tree use;
6714 ssa_op_iter i;
6716 fprintf (dump_file, "\nVisiting conditional with predicate: ");
6717 print_gimple_stmt (dump_file, stmt, 0, 0);
6718 fprintf (dump_file, "\nWith known ranges\n");
6720 FOR_EACH_SSA_TREE_OPERAND (use, stmt, i, SSA_OP_USE)
6722 fprintf (dump_file, "\t");
6723 print_generic_expr (dump_file, use, 0);
6724 fprintf (dump_file, ": ");
6725 dump_value_range (dump_file, vr_value[SSA_NAME_VERSION (use)]);
6728 fprintf (dump_file, "\n");
6731 /* Compute the value of the predicate COND by checking the known
6732 ranges of each of its operands.
6734 Note that we cannot evaluate all the equivalent ranges here
6735 because those ranges may not yet be final and with the current
6736 propagation strategy, we cannot determine when the value ranges
6737 of the names in the equivalence set have changed.
6739 For instance, given the following code fragment
6741 i_5 = PHI <8, i_13>
6743 i_14 = ASSERT_EXPR <i_5, i_5 != 0>
6744 if (i_14 == 1)
6747 Assume that on the first visit to i_14, i_5 has the temporary
6748 range [8, 8] because the second argument to the PHI function is
6749 not yet executable. We derive the range ~[0, 0] for i_14 and the
6750 equivalence set { i_5 }. So, when we visit 'if (i_14 == 1)' for
6751 the first time, since i_14 is equivalent to the range [8, 8], we
6752 determine that the predicate is always false.
6754 On the next round of propagation, i_13 is determined to be
6755 VARYING, which causes i_5 to drop down to VARYING. So, another
6756 visit to i_14 is scheduled. In this second visit, we compute the
6757 exact same range and equivalence set for i_14, namely ~[0, 0] and
6758 { i_5 }. But we did not have the previous range for i_5
6759 registered, so vrp_visit_assignment thinks that the range for
6760 i_14 has not changed. Therefore, the predicate 'if (i_14 == 1)'
6761 is not visited again, which stops propagation from visiting
6762 statements in the THEN clause of that if().
6764 To properly fix this we would need to keep the previous range
6765 value for the names in the equivalence set. This way we would've
6766 discovered that from one visit to the other i_5 changed from
6767 range [8, 8] to VR_VARYING.
6769 However, fixing this apparent limitation may not be worth the
6770 additional checking. Testing on several code bases (GCC, DLV,
6771 MICO, TRAMP3D and SPEC2000) showed that doing this results in
6772 4 more predicates folded in SPEC. */
6773 sop = false;
6775 val = vrp_evaluate_conditional_warnv_with_ops (gimple_cond_code (stmt),
6776 gimple_cond_lhs (stmt),
6777 gimple_cond_rhs (stmt),
6778 false, &sop, NULL);
6779 if (val)
6781 if (!sop)
6782 *taken_edge_p = find_taken_edge (gimple_bb (stmt), val);
6783 else
6785 if (dump_file && (dump_flags & TDF_DETAILS))
6786 fprintf (dump_file,
6787 "\nIgnoring predicate evaluation because "
6788 "it assumes that signed overflow is undefined");
6789 val = NULL_TREE;
6793 if (dump_file && (dump_flags & TDF_DETAILS))
6795 fprintf (dump_file, "\nPredicate evaluates to: ");
6796 if (val == NULL_TREE)
6797 fprintf (dump_file, "DON'T KNOW\n");
6798 else
6799 print_generic_stmt (dump_file, val, 0);
6802 return (*taken_edge_p) ? SSA_PROP_INTERESTING : SSA_PROP_VARYING;
6805 /* Searches the case label vector VEC for the index *IDX of the CASE_LABEL
6806 that includes the value VAL. The search is restricted to the range
6807 [START_IDX, n - 1] where n is the size of VEC.
6809 If there is a CASE_LABEL for VAL, its index is placed in IDX and true is
6810 returned.
6812 If there is no CASE_LABEL for VAL and there is one that is larger than VAL,
6813 it is placed in IDX and false is returned.
6815 If VAL is larger than any CASE_LABEL, n is placed on IDX and false is
6816 returned. */
6818 static bool
6819 find_case_label_index (gimple stmt, size_t start_idx, tree val, size_t *idx)
6821 size_t n = gimple_switch_num_labels (stmt);
6822 size_t low, high;
6824 /* Find case label for minimum of the value range or the next one.
6825 At each iteration we are searching in [low, high - 1]. */
6827 for (low = start_idx, high = n; high != low; )
6829 tree t;
6830 int cmp;
6831 /* Note that i != high, so we never ask for n. */
6832 size_t i = (high + low) / 2;
6833 t = gimple_switch_label (stmt, i);
6835 /* Cache the result of comparing CASE_LOW and val. */
6836 cmp = tree_int_cst_compare (CASE_LOW (t), val);
6838 if (cmp == 0)
6840 /* Ranges cannot be empty. */
6841 *idx = i;
6842 return true;
6844 else if (cmp > 0)
6845 high = i;
6846 else
6848 low = i + 1;
6849 if (CASE_HIGH (t) != NULL
6850 && tree_int_cst_compare (CASE_HIGH (t), val) >= 0)
6852 *idx = i;
6853 return true;
6858 *idx = high;
6859 return false;
6862 /* Searches the case label vector VEC for the range of CASE_LABELs that is used
6863 for values between MIN and MAX. The first index is placed in MIN_IDX. The
6864 last index is placed in MAX_IDX. If the range of CASE_LABELs is empty
6865 then MAX_IDX < MIN_IDX.
6866 Returns true if the default label is not needed. */
6868 static bool
6869 find_case_label_range (gimple stmt, tree min, tree max, size_t *min_idx,
6870 size_t *max_idx)
6872 size_t i, j;
6873 bool min_take_default = !find_case_label_index (stmt, 1, min, &i);
6874 bool max_take_default = !find_case_label_index (stmt, i, max, &j);
6876 if (i == j
6877 && min_take_default
6878 && max_take_default)
6880 /* Only the default case label reached.
6881 Return an empty range. */
6882 *min_idx = 1;
6883 *max_idx = 0;
6884 return false;
6886 else
6888 bool take_default = min_take_default || max_take_default;
6889 tree low, high;
6890 size_t k;
6892 if (max_take_default)
6893 j--;
6895 /* If the case label range is continuous, we do not need
6896 the default case label. Verify that. */
6897 high = CASE_LOW (gimple_switch_label (stmt, i));
6898 if (CASE_HIGH (gimple_switch_label (stmt, i)))
6899 high = CASE_HIGH (gimple_switch_label (stmt, i));
6900 for (k = i + 1; k <= j; ++k)
6902 low = CASE_LOW (gimple_switch_label (stmt, k));
6903 if (!integer_onep (int_const_binop (MINUS_EXPR, low, high)))
6905 take_default = true;
6906 break;
6908 high = low;
6909 if (CASE_HIGH (gimple_switch_label (stmt, k)))
6910 high = CASE_HIGH (gimple_switch_label (stmt, k));
6913 *min_idx = i;
6914 *max_idx = j;
6915 return !take_default;
6919 /* Searches the case label vector VEC for the ranges of CASE_LABELs that are
6920 used in range VR. The indices are placed in MIN_IDX1, MAX_IDX, MIN_IDX2 and
6921 MAX_IDX2. If the ranges of CASE_LABELs are empty then MAX_IDX1 < MIN_IDX1.
6922 Returns true if the default label is not needed. */
6924 static bool
6925 find_case_label_ranges (gimple stmt, value_range_t *vr, size_t *min_idx1,
6926 size_t *max_idx1, size_t *min_idx2,
6927 size_t *max_idx2)
6929 size_t i, j, k, l;
6930 unsigned int n = gimple_switch_num_labels (stmt);
6931 bool take_default;
6932 tree case_low, case_high;
6933 tree min = vr->min, max = vr->max;
6935 gcc_checking_assert (vr->type == VR_RANGE || vr->type == VR_ANTI_RANGE);
6937 take_default = !find_case_label_range (stmt, min, max, &i, &j);
6939 /* Set second range to emtpy. */
6940 *min_idx2 = 1;
6941 *max_idx2 = 0;
6943 if (vr->type == VR_RANGE)
6945 *min_idx1 = i;
6946 *max_idx1 = j;
6947 return !take_default;
6950 /* Set first range to all case labels. */
6951 *min_idx1 = 1;
6952 *max_idx1 = n - 1;
6954 if (i > j)
6955 return false;
6957 /* Make sure all the values of case labels [i , j] are contained in
6958 range [MIN, MAX]. */
6959 case_low = CASE_LOW (gimple_switch_label (stmt, i));
6960 case_high = CASE_HIGH (gimple_switch_label (stmt, j));
6961 if (tree_int_cst_compare (case_low, min) < 0)
6962 i += 1;
6963 if (case_high != NULL_TREE
6964 && tree_int_cst_compare (max, case_high) < 0)
6965 j -= 1;
6967 if (i > j)
6968 return false;
6970 /* If the range spans case labels [i, j], the corresponding anti-range spans
6971 the labels [1, i - 1] and [j + 1, n - 1]. */
6972 k = j + 1;
6973 l = n - 1;
6974 if (k > l)
6976 k = 1;
6977 l = 0;
6980 j = i - 1;
6981 i = 1;
6982 if (i > j)
6984 i = k;
6985 j = l;
6986 k = 1;
6987 l = 0;
6990 *min_idx1 = i;
6991 *max_idx1 = j;
6992 *min_idx2 = k;
6993 *max_idx2 = l;
6994 return false;
6997 /* Visit switch statement STMT. If we can determine which edge
6998 will be taken out of STMT's basic block, record it in
6999 *TAKEN_EDGE_P and return SSA_PROP_INTERESTING. Otherwise, return
7000 SSA_PROP_VARYING. */
7002 static enum ssa_prop_result
7003 vrp_visit_switch_stmt (gimple stmt, edge *taken_edge_p)
7005 tree op, val;
7006 value_range_t *vr;
7007 size_t i = 0, j = 0, k, l;
7008 bool take_default;
7010 *taken_edge_p = NULL;
7011 op = gimple_switch_index (stmt);
7012 if (TREE_CODE (op) != SSA_NAME)
7013 return SSA_PROP_VARYING;
7015 vr = get_value_range (op);
7016 if (dump_file && (dump_flags & TDF_DETAILS))
7018 fprintf (dump_file, "\nVisiting switch expression with operand ");
7019 print_generic_expr (dump_file, op, 0);
7020 fprintf (dump_file, " with known range ");
7021 dump_value_range (dump_file, vr);
7022 fprintf (dump_file, "\n");
7025 if ((vr->type != VR_RANGE
7026 && vr->type != VR_ANTI_RANGE)
7027 || symbolic_range_p (vr))
7028 return SSA_PROP_VARYING;
7030 /* Find the single edge that is taken from the switch expression. */
7031 take_default = !find_case_label_ranges (stmt, vr, &i, &j, &k, &l);
7033 /* Check if the range spans no CASE_LABEL. If so, we only reach the default
7034 label */
7035 if (j < i)
7037 gcc_assert (take_default);
7038 val = gimple_switch_default_label (stmt);
7040 else
7042 /* Check if labels with index i to j and maybe the default label
7043 are all reaching the same label. */
7045 val = gimple_switch_label (stmt, i);
7046 if (take_default
7047 && CASE_LABEL (gimple_switch_default_label (stmt))
7048 != CASE_LABEL (val))
7050 if (dump_file && (dump_flags & TDF_DETAILS))
7051 fprintf (dump_file, " not a single destination for this "
7052 "range\n");
7053 return SSA_PROP_VARYING;
7055 for (++i; i <= j; ++i)
7057 if (CASE_LABEL (gimple_switch_label (stmt, i)) != CASE_LABEL (val))
7059 if (dump_file && (dump_flags & TDF_DETAILS))
7060 fprintf (dump_file, " not a single destination for this "
7061 "range\n");
7062 return SSA_PROP_VARYING;
7065 for (; k <= l; ++k)
7067 if (CASE_LABEL (gimple_switch_label (stmt, k)) != CASE_LABEL (val))
7069 if (dump_file && (dump_flags & TDF_DETAILS))
7070 fprintf (dump_file, " not a single destination for this "
7071 "range\n");
7072 return SSA_PROP_VARYING;
7077 *taken_edge_p = find_edge (gimple_bb (stmt),
7078 label_to_block (CASE_LABEL (val)));
7080 if (dump_file && (dump_flags & TDF_DETAILS))
7082 fprintf (dump_file, " will take edge to ");
7083 print_generic_stmt (dump_file, CASE_LABEL (val), 0);
7086 return SSA_PROP_INTERESTING;
7090 /* Evaluate statement STMT. If the statement produces a useful range,
7091 return SSA_PROP_INTERESTING and record the SSA name with the
7092 interesting range into *OUTPUT_P.
7094 If STMT is a conditional branch and we can determine its truth
7095 value, the taken edge is recorded in *TAKEN_EDGE_P.
7097 If STMT produces a varying value, return SSA_PROP_VARYING. */
7099 static enum ssa_prop_result
7100 vrp_visit_stmt (gimple stmt, edge *taken_edge_p, tree *output_p)
7102 tree def;
7103 ssa_op_iter iter;
7105 if (dump_file && (dump_flags & TDF_DETAILS))
7107 fprintf (dump_file, "\nVisiting statement:\n");
7108 print_gimple_stmt (dump_file, stmt, 0, dump_flags);
7109 fprintf (dump_file, "\n");
7112 if (!stmt_interesting_for_vrp (stmt))
7113 gcc_assert (stmt_ends_bb_p (stmt));
7114 else if (is_gimple_assign (stmt) || is_gimple_call (stmt))
7116 /* In general, assignments with virtual operands are not useful
7117 for deriving ranges, with the obvious exception of calls to
7118 builtin functions. */
7119 if ((is_gimple_call (stmt)
7120 && gimple_call_fndecl (stmt) != NULL_TREE
7121 && DECL_BUILT_IN (gimple_call_fndecl (stmt)))
7122 || !gimple_vuse (stmt))
7123 return vrp_visit_assignment_or_call (stmt, output_p);
7125 else if (gimple_code (stmt) == GIMPLE_COND)
7126 return vrp_visit_cond_stmt (stmt, taken_edge_p);
7127 else if (gimple_code (stmt) == GIMPLE_SWITCH)
7128 return vrp_visit_switch_stmt (stmt, taken_edge_p);
7130 /* All other statements produce nothing of interest for VRP, so mark
7131 their outputs varying and prevent further simulation. */
7132 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_DEF)
7133 set_value_range_to_varying (get_value_range (def));
7135 return SSA_PROP_VARYING;
7138 /* Union the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
7139 { VR1TYPE, VR0MIN, VR0MAX } and store the result
7140 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
7141 possible such range. The resulting range is not canonicalized. */
7143 static void
7144 union_ranges (enum value_range_type *vr0type,
7145 tree *vr0min, tree *vr0max,
7146 enum value_range_type vr1type,
7147 tree vr1min, tree vr1max)
7149 bool mineq = operand_equal_p (*vr0min, vr1min, 0);
7150 bool maxeq = operand_equal_p (*vr0max, vr1max, 0);
7152 /* [] is vr0, () is vr1 in the following classification comments. */
7153 if (mineq && maxeq)
7155 /* [( )] */
7156 if (*vr0type == vr1type)
7157 /* Nothing to do for equal ranges. */
7159 else if ((*vr0type == VR_RANGE
7160 && vr1type == VR_ANTI_RANGE)
7161 || (*vr0type == VR_ANTI_RANGE
7162 && vr1type == VR_RANGE))
7164 /* For anti-range with range union the result is varying. */
7165 goto give_up;
7167 else
7168 gcc_unreachable ();
7170 else if (operand_less_p (*vr0max, vr1min) == 1
7171 || operand_less_p (vr1max, *vr0min) == 1)
7173 /* [ ] ( ) or ( ) [ ]
7174 If the ranges have an empty intersection, result of the union
7175 operation is the anti-range or if both are anti-ranges
7176 it covers all. */
7177 if (*vr0type == VR_ANTI_RANGE
7178 && vr1type == VR_ANTI_RANGE)
7179 goto give_up;
7180 else if (*vr0type == VR_ANTI_RANGE
7181 && vr1type == VR_RANGE)
7183 else if (*vr0type == VR_RANGE
7184 && vr1type == VR_ANTI_RANGE)
7186 *vr0type = vr1type;
7187 *vr0min = vr1min;
7188 *vr0max = vr1max;
7190 else if (*vr0type == VR_RANGE
7191 && vr1type == VR_RANGE)
7193 /* The result is the convex hull of both ranges. */
7194 if (operand_less_p (*vr0max, vr1min) == 1)
7196 /* If the result can be an anti-range, create one. */
7197 if (TREE_CODE (*vr0max) == INTEGER_CST
7198 && TREE_CODE (vr1min) == INTEGER_CST
7199 && vrp_val_is_min (*vr0min)
7200 && vrp_val_is_max (vr1max))
7202 tree min = int_const_binop (PLUS_EXPR,
7203 *vr0max, integer_one_node);
7204 tree max = int_const_binop (MINUS_EXPR,
7205 vr1min, integer_one_node);
7206 if (!operand_less_p (max, min))
7208 *vr0type = VR_ANTI_RANGE;
7209 *vr0min = min;
7210 *vr0max = max;
7212 else
7213 *vr0max = vr1max;
7215 else
7216 *vr0max = vr1max;
7218 else
7220 /* If the result can be an anti-range, create one. */
7221 if (TREE_CODE (vr1max) == INTEGER_CST
7222 && TREE_CODE (*vr0min) == INTEGER_CST
7223 && vrp_val_is_min (vr1min)
7224 && vrp_val_is_max (*vr0max))
7226 tree min = int_const_binop (PLUS_EXPR,
7227 vr1max, integer_one_node);
7228 tree max = int_const_binop (MINUS_EXPR,
7229 *vr0min, integer_one_node);
7230 if (!operand_less_p (max, min))
7232 *vr0type = VR_ANTI_RANGE;
7233 *vr0min = min;
7234 *vr0max = max;
7236 else
7237 *vr0min = vr1min;
7239 else
7240 *vr0min = vr1min;
7243 else
7244 gcc_unreachable ();
7246 else if ((maxeq || operand_less_p (vr1max, *vr0max) == 1)
7247 && (mineq || operand_less_p (*vr0min, vr1min) == 1))
7249 /* [ ( ) ] or [( ) ] or [ ( )] */
7250 if (*vr0type == VR_RANGE
7251 && vr1type == VR_RANGE)
7253 else if (*vr0type == VR_ANTI_RANGE
7254 && vr1type == VR_ANTI_RANGE)
7256 *vr0type = vr1type;
7257 *vr0min = vr1min;
7258 *vr0max = vr1max;
7260 else if (*vr0type == VR_ANTI_RANGE
7261 && vr1type == VR_RANGE)
7263 /* Arbitrarily choose the right or left gap. */
7264 if (!mineq && TREE_CODE (vr1min) == INTEGER_CST)
7265 *vr0max = int_const_binop (MINUS_EXPR, vr1min, integer_one_node);
7266 else if (!maxeq && TREE_CODE (vr1max) == INTEGER_CST)
7267 *vr0min = int_const_binop (PLUS_EXPR, vr1max, integer_one_node);
7268 else
7269 goto give_up;
7271 else if (*vr0type == VR_RANGE
7272 && vr1type == VR_ANTI_RANGE)
7273 /* The result covers everything. */
7274 goto give_up;
7275 else
7276 gcc_unreachable ();
7278 else if ((maxeq || operand_less_p (*vr0max, vr1max) == 1)
7279 && (mineq || operand_less_p (vr1min, *vr0min) == 1))
7281 /* ( [ ] ) or ([ ] ) or ( [ ]) */
7282 if (*vr0type == VR_RANGE
7283 && vr1type == VR_RANGE)
7285 *vr0type = vr1type;
7286 *vr0min = vr1min;
7287 *vr0max = vr1max;
7289 else if (*vr0type == VR_ANTI_RANGE
7290 && vr1type == VR_ANTI_RANGE)
7292 else if (*vr0type == VR_RANGE
7293 && vr1type == VR_ANTI_RANGE)
7295 *vr0type = VR_ANTI_RANGE;
7296 if (!mineq && TREE_CODE (*vr0min) == INTEGER_CST)
7298 *vr0max = int_const_binop (MINUS_EXPR, *vr0min, integer_one_node);
7299 *vr0min = vr1min;
7301 else if (!maxeq && TREE_CODE (*vr0max) == INTEGER_CST)
7303 *vr0min = int_const_binop (PLUS_EXPR, *vr0max, integer_one_node);
7304 *vr0max = vr1max;
7306 else
7307 goto give_up;
7309 else if (*vr0type == VR_ANTI_RANGE
7310 && vr1type == VR_RANGE)
7311 /* The result covers everything. */
7312 goto give_up;
7313 else
7314 gcc_unreachable ();
7316 else if ((operand_less_p (vr1min, *vr0max) == 1
7317 || operand_equal_p (vr1min, *vr0max, 0))
7318 && operand_less_p (*vr0min, vr1min) == 1)
7320 /* [ ( ] ) or [ ]( ) */
7321 if (*vr0type == VR_RANGE
7322 && vr1type == VR_RANGE)
7323 *vr0max = vr1max;
7324 else if (*vr0type == VR_ANTI_RANGE
7325 && vr1type == VR_ANTI_RANGE)
7326 *vr0min = vr1min;
7327 else if (*vr0type == VR_ANTI_RANGE
7328 && vr1type == VR_RANGE)
7330 if (TREE_CODE (vr1min) == INTEGER_CST)
7331 *vr0max = int_const_binop (MINUS_EXPR, vr1min, integer_one_node);
7332 else
7333 goto give_up;
7335 else if (*vr0type == VR_RANGE
7336 && vr1type == VR_ANTI_RANGE)
7338 if (TREE_CODE (*vr0max) == INTEGER_CST)
7340 *vr0type = vr1type;
7341 *vr0min = int_const_binop (PLUS_EXPR, *vr0max, integer_one_node);
7342 *vr0max = vr1max;
7344 else
7345 goto give_up;
7347 else
7348 gcc_unreachable ();
7350 else if ((operand_less_p (*vr0min, vr1max) == 1
7351 || operand_equal_p (*vr0min, vr1max, 0))
7352 && operand_less_p (vr1min, *vr0min) == 1)
7354 /* ( [ ) ] or ( )[ ] */
7355 if (*vr0type == VR_RANGE
7356 && vr1type == VR_RANGE)
7357 *vr0min = vr1min;
7358 else if (*vr0type == VR_ANTI_RANGE
7359 && vr1type == VR_ANTI_RANGE)
7360 *vr0max = vr1max;
7361 else if (*vr0type == VR_ANTI_RANGE
7362 && vr1type == VR_RANGE)
7364 if (TREE_CODE (vr1max) == INTEGER_CST)
7365 *vr0min = int_const_binop (PLUS_EXPR, vr1max, integer_one_node);
7366 else
7367 goto give_up;
7369 else if (*vr0type == VR_RANGE
7370 && vr1type == VR_ANTI_RANGE)
7372 if (TREE_CODE (*vr0min) == INTEGER_CST)
7374 *vr0type = vr1type;
7375 *vr0min = vr1min;
7376 *vr0max = int_const_binop (MINUS_EXPR, *vr0min, integer_one_node);
7378 else
7379 goto give_up;
7381 else
7382 gcc_unreachable ();
7384 else
7385 goto give_up;
7387 return;
7389 give_up:
7390 *vr0type = VR_VARYING;
7391 *vr0min = NULL_TREE;
7392 *vr0max = NULL_TREE;
7395 /* Intersect the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
7396 { VR1TYPE, VR0MIN, VR0MAX } and store the result
7397 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
7398 possible such range. The resulting range is not canonicalized. */
7400 static void
7401 intersect_ranges (enum value_range_type *vr0type,
7402 tree *vr0min, tree *vr0max,
7403 enum value_range_type vr1type,
7404 tree vr1min, tree vr1max)
7406 bool mineq = operand_equal_p (*vr0min, vr1min, 0);
7407 bool maxeq = operand_equal_p (*vr0max, vr1max, 0);
7409 /* [] is vr0, () is vr1 in the following classification comments. */
7410 if (mineq && maxeq)
7412 /* [( )] */
7413 if (*vr0type == vr1type)
7414 /* Nothing to do for equal ranges. */
7416 else if ((*vr0type == VR_RANGE
7417 && vr1type == VR_ANTI_RANGE)
7418 || (*vr0type == VR_ANTI_RANGE
7419 && vr1type == VR_RANGE))
7421 /* For anti-range with range intersection the result is empty. */
7422 *vr0type = VR_UNDEFINED;
7423 *vr0min = NULL_TREE;
7424 *vr0max = NULL_TREE;
7426 else
7427 gcc_unreachable ();
7429 else if (operand_less_p (*vr0max, vr1min) == 1
7430 || operand_less_p (vr1max, *vr0min) == 1)
7432 /* [ ] ( ) or ( ) [ ]
7433 If the ranges have an empty intersection, the result of the
7434 intersect operation is the range for intersecting an
7435 anti-range with a range or empty when intersecting two ranges. */
7436 if (*vr0type == VR_RANGE
7437 && vr1type == VR_ANTI_RANGE)
7439 else if (*vr0type == VR_ANTI_RANGE
7440 && vr1type == VR_RANGE)
7442 *vr0type = vr1type;
7443 *vr0min = vr1min;
7444 *vr0max = vr1max;
7446 else if (*vr0type == VR_RANGE
7447 && vr1type == VR_RANGE)
7449 *vr0type = VR_UNDEFINED;
7450 *vr0min = NULL_TREE;
7451 *vr0max = NULL_TREE;
7453 else if (*vr0type == VR_ANTI_RANGE
7454 && vr1type == VR_ANTI_RANGE)
7456 /* If the anti-ranges are adjacent to each other merge them. */
7457 if (TREE_CODE (*vr0max) == INTEGER_CST
7458 && TREE_CODE (vr1min) == INTEGER_CST
7459 && operand_less_p (*vr0max, vr1min) == 1
7460 && integer_onep (int_const_binop (MINUS_EXPR,
7461 vr1min, *vr0max)))
7462 *vr0max = vr1max;
7463 else if (TREE_CODE (vr1max) == INTEGER_CST
7464 && TREE_CODE (*vr0min) == INTEGER_CST
7465 && operand_less_p (vr1max, *vr0min) == 1
7466 && integer_onep (int_const_binop (MINUS_EXPR,
7467 *vr0min, vr1max)))
7468 *vr0min = vr1min;
7469 /* Else arbitrarily take VR0. */
7472 else if ((maxeq || operand_less_p (vr1max, *vr0max) == 1)
7473 && (mineq || operand_less_p (*vr0min, vr1min) == 1))
7475 /* [ ( ) ] or [( ) ] or [ ( )] */
7476 if (*vr0type == VR_RANGE
7477 && vr1type == VR_RANGE)
7479 /* If both are ranges the result is the inner one. */
7480 *vr0type = vr1type;
7481 *vr0min = vr1min;
7482 *vr0max = vr1max;
7484 else if (*vr0type == VR_RANGE
7485 && vr1type == VR_ANTI_RANGE)
7487 /* Choose the right gap if the left one is empty. */
7488 if (mineq)
7490 if (TREE_CODE (vr1max) == INTEGER_CST)
7491 *vr0min = int_const_binop (PLUS_EXPR, vr1max, integer_one_node);
7492 else
7493 *vr0min = vr1max;
7495 /* Choose the left gap if the right one is empty. */
7496 else if (maxeq)
7498 if (TREE_CODE (vr1min) == INTEGER_CST)
7499 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
7500 integer_one_node);
7501 else
7502 *vr0max = vr1min;
7504 /* Choose the anti-range if the range is effectively varying. */
7505 else if (vrp_val_is_min (*vr0min)
7506 && vrp_val_is_max (*vr0max))
7508 *vr0type = vr1type;
7509 *vr0min = vr1min;
7510 *vr0max = vr1max;
7512 /* Else choose the range. */
7514 else if (*vr0type == VR_ANTI_RANGE
7515 && vr1type == VR_ANTI_RANGE)
7516 /* If both are anti-ranges the result is the outer one. */
7518 else if (*vr0type == VR_ANTI_RANGE
7519 && vr1type == VR_RANGE)
7521 /* The intersection is empty. */
7522 *vr0type = VR_UNDEFINED;
7523 *vr0min = NULL_TREE;
7524 *vr0max = NULL_TREE;
7526 else
7527 gcc_unreachable ();
7529 else if ((maxeq || operand_less_p (*vr0max, vr1max) == 1)
7530 && (mineq || operand_less_p (vr1min, *vr0min) == 1))
7532 /* ( [ ] ) or ([ ] ) or ( [ ]) */
7533 if (*vr0type == VR_RANGE
7534 && vr1type == VR_RANGE)
7535 /* Choose the inner range. */
7537 else if (*vr0type == VR_ANTI_RANGE
7538 && vr1type == VR_RANGE)
7540 /* Choose the right gap if the left is empty. */
7541 if (mineq)
7543 *vr0type = VR_RANGE;
7544 if (TREE_CODE (*vr0max) == INTEGER_CST)
7545 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
7546 integer_one_node);
7547 else
7548 *vr0min = *vr0max;
7549 *vr0max = vr1max;
7551 /* Choose the left gap if the right is empty. */
7552 else if (maxeq)
7554 *vr0type = VR_RANGE;
7555 if (TREE_CODE (*vr0min) == INTEGER_CST)
7556 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
7557 integer_one_node);
7558 else
7559 *vr0max = *vr0min;
7560 *vr0min = vr1min;
7562 /* Choose the anti-range if the range is effectively varying. */
7563 else if (vrp_val_is_min (vr1min)
7564 && vrp_val_is_max (vr1max))
7566 /* Else choose the range. */
7567 else
7569 *vr0type = vr1type;
7570 *vr0min = vr1min;
7571 *vr0max = vr1max;
7574 else if (*vr0type == VR_ANTI_RANGE
7575 && vr1type == VR_ANTI_RANGE)
7577 /* If both are anti-ranges the result is the outer one. */
7578 *vr0type = vr1type;
7579 *vr0min = vr1min;
7580 *vr0max = vr1max;
7582 else if (vr1type == VR_ANTI_RANGE
7583 && *vr0type == VR_RANGE)
7585 /* The intersection is empty. */
7586 *vr0type = VR_UNDEFINED;
7587 *vr0min = NULL_TREE;
7588 *vr0max = NULL_TREE;
7590 else
7591 gcc_unreachable ();
7593 else if ((operand_less_p (vr1min, *vr0max) == 1
7594 || operand_equal_p (vr1min, *vr0max, 0))
7595 && operand_less_p (*vr0min, vr1min) == 1)
7597 /* [ ( ] ) or [ ]( ) */
7598 if (*vr0type == VR_ANTI_RANGE
7599 && vr1type == VR_ANTI_RANGE)
7600 *vr0max = vr1max;
7601 else if (*vr0type == VR_RANGE
7602 && vr1type == VR_RANGE)
7603 *vr0min = vr1min;
7604 else if (*vr0type == VR_RANGE
7605 && vr1type == VR_ANTI_RANGE)
7607 if (TREE_CODE (vr1min) == INTEGER_CST)
7608 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
7609 integer_one_node);
7610 else
7611 *vr0max = vr1min;
7613 else if (*vr0type == VR_ANTI_RANGE
7614 && vr1type == VR_RANGE)
7616 *vr0type = VR_RANGE;
7617 if (TREE_CODE (*vr0max) == INTEGER_CST)
7618 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
7619 integer_one_node);
7620 else
7621 *vr0min = *vr0max;
7622 *vr0max = vr1max;
7624 else
7625 gcc_unreachable ();
7627 else if ((operand_less_p (*vr0min, vr1max) == 1
7628 || operand_equal_p (*vr0min, vr1max, 0))
7629 && operand_less_p (vr1min, *vr0min) == 1)
7631 /* ( [ ) ] or ( )[ ] */
7632 if (*vr0type == VR_ANTI_RANGE
7633 && vr1type == VR_ANTI_RANGE)
7634 *vr0min = vr1min;
7635 else if (*vr0type == VR_RANGE
7636 && vr1type == VR_RANGE)
7637 *vr0max = vr1max;
7638 else if (*vr0type == VR_RANGE
7639 && vr1type == VR_ANTI_RANGE)
7641 if (TREE_CODE (vr1max) == INTEGER_CST)
7642 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
7643 integer_one_node);
7644 else
7645 *vr0min = vr1max;
7647 else if (*vr0type == VR_ANTI_RANGE
7648 && vr1type == VR_RANGE)
7650 *vr0type = VR_RANGE;
7651 if (TREE_CODE (*vr0min) == INTEGER_CST)
7652 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
7653 integer_one_node);
7654 else
7655 *vr0max = *vr0min;
7656 *vr0min = vr1min;
7658 else
7659 gcc_unreachable ();
7662 /* As a fallback simply use { *VRTYPE, *VR0MIN, *VR0MAX } as
7663 result for the intersection. That's always a conservative
7664 correct estimate. */
7666 return;
7670 /* Intersect the two value-ranges *VR0 and *VR1 and store the result
7671 in *VR0. This may not be the smallest possible such range. */
7673 static void
7674 vrp_intersect_ranges_1 (value_range_t *vr0, value_range_t *vr1)
7676 value_range_t saved;
7678 /* If either range is VR_VARYING the other one wins. */
7679 if (vr1->type == VR_VARYING)
7680 return;
7681 if (vr0->type == VR_VARYING)
7683 copy_value_range (vr0, vr1);
7684 return;
7687 /* When either range is VR_UNDEFINED the resulting range is
7688 VR_UNDEFINED, too. */
7689 if (vr0->type == VR_UNDEFINED)
7690 return;
7691 if (vr1->type == VR_UNDEFINED)
7693 set_value_range_to_undefined (vr0);
7694 return;
7697 /* Save the original vr0 so we can return it as conservative intersection
7698 result when our worker turns things to varying. */
7699 saved = *vr0;
7700 intersect_ranges (&vr0->type, &vr0->min, &vr0->max,
7701 vr1->type, vr1->min, vr1->max);
7702 /* Make sure to canonicalize the result though as the inversion of a
7703 VR_RANGE can still be a VR_RANGE. */
7704 set_and_canonicalize_value_range (vr0, vr0->type,
7705 vr0->min, vr0->max, vr0->equiv);
7706 /* If that failed, use the saved original VR0. */
7707 if (vr0->type == VR_VARYING)
7709 *vr0 = saved;
7710 return;
7712 /* If the result is VR_UNDEFINED there is no need to mess with
7713 the equivalencies. */
7714 if (vr0->type == VR_UNDEFINED)
7715 return;
7717 /* The resulting set of equivalences for range intersection is the union of
7718 the two sets. */
7719 if (vr0->equiv && vr1->equiv && vr0->equiv != vr1->equiv)
7720 bitmap_ior_into (vr0->equiv, vr1->equiv);
7721 else if (vr1->equiv && !vr0->equiv)
7722 bitmap_copy (vr0->equiv, vr1->equiv);
7725 static void
7726 vrp_intersect_ranges (value_range_t *vr0, value_range_t *vr1)
7728 if (dump_file && (dump_flags & TDF_DETAILS))
7730 fprintf (dump_file, "Intersecting\n ");
7731 dump_value_range (dump_file, vr0);
7732 fprintf (dump_file, "\nand\n ");
7733 dump_value_range (dump_file, vr1);
7734 fprintf (dump_file, "\n");
7736 vrp_intersect_ranges_1 (vr0, vr1);
7737 if (dump_file && (dump_flags & TDF_DETAILS))
7739 fprintf (dump_file, "to\n ");
7740 dump_value_range (dump_file, vr0);
7741 fprintf (dump_file, "\n");
7745 /* Meet operation for value ranges. Given two value ranges VR0 and
7746 VR1, store in VR0 a range that contains both VR0 and VR1. This
7747 may not be the smallest possible such range. */
7749 static void
7750 vrp_meet_1 (value_range_t *vr0, value_range_t *vr1)
7752 value_range_t saved;
7754 if (vr0->type == VR_UNDEFINED)
7756 /* Drop equivalences. See PR53465. */
7757 set_value_range (vr0, vr1->type, vr1->min, vr1->max, NULL);
7758 return;
7761 if (vr1->type == VR_UNDEFINED)
7763 /* VR0 already has the resulting range, just drop equivalences.
7764 See PR53465. */
7765 if (vr0->equiv)
7766 bitmap_clear (vr0->equiv);
7767 return;
7770 if (vr0->type == VR_VARYING)
7772 /* Nothing to do. VR0 already has the resulting range. */
7773 return;
7776 if (vr1->type == VR_VARYING)
7778 set_value_range_to_varying (vr0);
7779 return;
7782 saved = *vr0;
7783 union_ranges (&vr0->type, &vr0->min, &vr0->max,
7784 vr1->type, vr1->min, vr1->max);
7785 if (vr0->type == VR_VARYING)
7787 /* Failed to find an efficient meet. Before giving up and setting
7788 the result to VARYING, see if we can at least derive a useful
7789 anti-range. FIXME, all this nonsense about distinguishing
7790 anti-ranges from ranges is necessary because of the odd
7791 semantics of range_includes_zero_p and friends. */
7792 if (((saved.type == VR_RANGE
7793 && range_includes_zero_p (saved.min, saved.max) == 0)
7794 || (saved.type == VR_ANTI_RANGE
7795 && range_includes_zero_p (saved.min, saved.max) == 1))
7796 && ((vr1->type == VR_RANGE
7797 && range_includes_zero_p (vr1->min, vr1->max) == 0)
7798 || (vr1->type == VR_ANTI_RANGE
7799 && range_includes_zero_p (vr1->min, vr1->max) == 1)))
7801 set_value_range_to_nonnull (vr0, TREE_TYPE (saved.min));
7803 /* Since this meet operation did not result from the meeting of
7804 two equivalent names, VR0 cannot have any equivalences. */
7805 if (vr0->equiv)
7806 bitmap_clear (vr0->equiv);
7807 return;
7810 set_value_range_to_varying (vr0);
7811 return;
7813 set_and_canonicalize_value_range (vr0, vr0->type, vr0->min, vr0->max,
7814 vr0->equiv);
7815 if (vr0->type == VR_VARYING)
7816 return;
7818 /* The resulting set of equivalences is always the intersection of
7819 the two sets. */
7820 if (vr0->equiv && vr1->equiv && vr0->equiv != vr1->equiv)
7821 bitmap_and_into (vr0->equiv, vr1->equiv);
7822 else if (vr0->equiv && !vr1->equiv)
7823 bitmap_clear (vr0->equiv);
7826 static void
7827 vrp_meet (value_range_t *vr0, value_range_t *vr1)
7829 if (dump_file && (dump_flags & TDF_DETAILS))
7831 fprintf (dump_file, "Meeting\n ");
7832 dump_value_range (dump_file, vr0);
7833 fprintf (dump_file, "\nand\n ");
7834 dump_value_range (dump_file, vr1);
7835 fprintf (dump_file, "\n");
7837 vrp_meet_1 (vr0, vr1);
7838 if (dump_file && (dump_flags & TDF_DETAILS))
7840 fprintf (dump_file, "to\n ");
7841 dump_value_range (dump_file, vr0);
7842 fprintf (dump_file, "\n");
7847 /* Visit all arguments for PHI node PHI that flow through executable
7848 edges. If a valid value range can be derived from all the incoming
7849 value ranges, set a new range for the LHS of PHI. */
7851 static enum ssa_prop_result
7852 vrp_visit_phi_node (gimple phi)
7854 size_t i;
7855 tree lhs = PHI_RESULT (phi);
7856 value_range_t *lhs_vr = get_value_range (lhs);
7857 value_range_t vr_result = VR_INITIALIZER;
7858 bool first = true;
7859 int edges, old_edges;
7860 struct loop *l;
7862 if (dump_file && (dump_flags & TDF_DETAILS))
7864 fprintf (dump_file, "\nVisiting PHI node: ");
7865 print_gimple_stmt (dump_file, phi, 0, dump_flags);
7868 edges = 0;
7869 for (i = 0; i < gimple_phi_num_args (phi); i++)
7871 edge e = gimple_phi_arg_edge (phi, i);
7873 if (dump_file && (dump_flags & TDF_DETAILS))
7875 fprintf (dump_file,
7876 "\n Argument #%d (%d -> %d %sexecutable)\n",
7877 (int) i, e->src->index, e->dest->index,
7878 (e->flags & EDGE_EXECUTABLE) ? "" : "not ");
7881 if (e->flags & EDGE_EXECUTABLE)
7883 tree arg = PHI_ARG_DEF (phi, i);
7884 value_range_t vr_arg;
7886 ++edges;
7888 if (TREE_CODE (arg) == SSA_NAME)
7890 vr_arg = *(get_value_range (arg));
7892 else
7894 if (is_overflow_infinity (arg))
7896 arg = copy_node (arg);
7897 TREE_OVERFLOW (arg) = 0;
7900 vr_arg.type = VR_RANGE;
7901 vr_arg.min = arg;
7902 vr_arg.max = arg;
7903 vr_arg.equiv = NULL;
7906 if (dump_file && (dump_flags & TDF_DETAILS))
7908 fprintf (dump_file, "\t");
7909 print_generic_expr (dump_file, arg, dump_flags);
7910 fprintf (dump_file, "\n\tValue: ");
7911 dump_value_range (dump_file, &vr_arg);
7912 fprintf (dump_file, "\n");
7915 if (first)
7916 copy_value_range (&vr_result, &vr_arg);
7917 else
7918 vrp_meet (&vr_result, &vr_arg);
7919 first = false;
7921 if (vr_result.type == VR_VARYING)
7922 break;
7926 if (vr_result.type == VR_VARYING)
7927 goto varying;
7928 else if (vr_result.type == VR_UNDEFINED)
7929 goto update_range;
7931 old_edges = vr_phi_edge_counts[SSA_NAME_VERSION (lhs)];
7932 vr_phi_edge_counts[SSA_NAME_VERSION (lhs)] = edges;
7934 /* To prevent infinite iterations in the algorithm, derive ranges
7935 when the new value is slightly bigger or smaller than the
7936 previous one. We don't do this if we have seen a new executable
7937 edge; this helps us avoid an overflow infinity for conditionals
7938 which are not in a loop. If the old value-range was VR_UNDEFINED
7939 use the updated range and iterate one more time. */
7940 if (edges > 0
7941 && gimple_phi_num_args (phi) > 1
7942 && edges == old_edges
7943 && lhs_vr->type != VR_UNDEFINED)
7945 int cmp_min = compare_values (lhs_vr->min, vr_result.min);
7946 int cmp_max = compare_values (lhs_vr->max, vr_result.max);
7948 /* For non VR_RANGE or for pointers fall back to varying if
7949 the range changed. */
7950 if ((lhs_vr->type != VR_RANGE || vr_result.type != VR_RANGE
7951 || POINTER_TYPE_P (TREE_TYPE (lhs)))
7952 && (cmp_min != 0 || cmp_max != 0))
7953 goto varying;
7955 /* If the new minimum is smaller or larger than the previous
7956 one, go all the way to -INF. In the first case, to avoid
7957 iterating millions of times to reach -INF, and in the
7958 other case to avoid infinite bouncing between different
7959 minimums. */
7960 if (cmp_min > 0 || cmp_min < 0)
7962 if (!needs_overflow_infinity (TREE_TYPE (vr_result.min))
7963 || !vrp_var_may_overflow (lhs, phi))
7964 vr_result.min = TYPE_MIN_VALUE (TREE_TYPE (vr_result.min));
7965 else if (supports_overflow_infinity (TREE_TYPE (vr_result.min)))
7966 vr_result.min =
7967 negative_overflow_infinity (TREE_TYPE (vr_result.min));
7970 /* Similarly, if the new maximum is smaller or larger than
7971 the previous one, go all the way to +INF. */
7972 if (cmp_max < 0 || cmp_max > 0)
7974 if (!needs_overflow_infinity (TREE_TYPE (vr_result.max))
7975 || !vrp_var_may_overflow (lhs, phi))
7976 vr_result.max = TYPE_MAX_VALUE (TREE_TYPE (vr_result.max));
7977 else if (supports_overflow_infinity (TREE_TYPE (vr_result.max)))
7978 vr_result.max =
7979 positive_overflow_infinity (TREE_TYPE (vr_result.max));
7982 /* If we dropped either bound to +-INF then if this is a loop
7983 PHI node SCEV may known more about its value-range. */
7984 if ((cmp_min > 0 || cmp_min < 0
7985 || cmp_max < 0 || cmp_max > 0)
7986 && current_loops
7987 && (l = loop_containing_stmt (phi))
7988 && l->header == gimple_bb (phi))
7989 adjust_range_with_scev (&vr_result, l, phi, lhs);
7991 /* If we will end up with a (-INF, +INF) range, set it to
7992 VARYING. Same if the previous max value was invalid for
7993 the type and we end up with vr_result.min > vr_result.max. */
7994 if ((vrp_val_is_max (vr_result.max)
7995 && vrp_val_is_min (vr_result.min))
7996 || compare_values (vr_result.min,
7997 vr_result.max) > 0)
7998 goto varying;
8001 /* If the new range is different than the previous value, keep
8002 iterating. */
8003 update_range:
8004 if (update_value_range (lhs, &vr_result))
8006 if (dump_file && (dump_flags & TDF_DETAILS))
8008 fprintf (dump_file, "Found new range for ");
8009 print_generic_expr (dump_file, lhs, 0);
8010 fprintf (dump_file, ": ");
8011 dump_value_range (dump_file, &vr_result);
8012 fprintf (dump_file, "\n\n");
8015 return SSA_PROP_INTERESTING;
8018 /* Nothing changed, don't add outgoing edges. */
8019 return SSA_PROP_NOT_INTERESTING;
8021 /* No match found. Set the LHS to VARYING. */
8022 varying:
8023 set_value_range_to_varying (lhs_vr);
8024 return SSA_PROP_VARYING;
8027 /* Simplify boolean operations if the source is known
8028 to be already a boolean. */
8029 static bool
8030 simplify_truth_ops_using_ranges (gimple_stmt_iterator *gsi, gimple stmt)
8032 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
8033 tree lhs, op0, op1;
8034 bool need_conversion;
8036 /* We handle only !=/== case here. */
8037 gcc_assert (rhs_code == EQ_EXPR || rhs_code == NE_EXPR);
8039 op0 = gimple_assign_rhs1 (stmt);
8040 if (!op_with_boolean_value_range_p (op0))
8041 return false;
8043 op1 = gimple_assign_rhs2 (stmt);
8044 if (!op_with_boolean_value_range_p (op1))
8045 return false;
8047 /* Reduce number of cases to handle to NE_EXPR. As there is no
8048 BIT_XNOR_EXPR we cannot replace A == B with a single statement. */
8049 if (rhs_code == EQ_EXPR)
8051 if (TREE_CODE (op1) == INTEGER_CST)
8052 op1 = int_const_binop (BIT_XOR_EXPR, op1, integer_one_node);
8053 else
8054 return false;
8057 lhs = gimple_assign_lhs (stmt);
8058 need_conversion
8059 = !useless_type_conversion_p (TREE_TYPE (lhs), TREE_TYPE (op0));
8061 /* Make sure to not sign-extend a 1-bit 1 when converting the result. */
8062 if (need_conversion
8063 && !TYPE_UNSIGNED (TREE_TYPE (op0))
8064 && TYPE_PRECISION (TREE_TYPE (op0)) == 1
8065 && TYPE_PRECISION (TREE_TYPE (lhs)) > 1)
8066 return false;
8068 /* For A != 0 we can substitute A itself. */
8069 if (integer_zerop (op1))
8070 gimple_assign_set_rhs_with_ops (gsi,
8071 need_conversion
8072 ? NOP_EXPR : TREE_CODE (op0),
8073 op0, NULL_TREE);
8074 /* For A != B we substitute A ^ B. Either with conversion. */
8075 else if (need_conversion)
8077 tree tem = make_ssa_name (TREE_TYPE (op0), NULL);
8078 gimple newop = gimple_build_assign_with_ops (BIT_XOR_EXPR, tem, op0, op1);
8079 gsi_insert_before (gsi, newop, GSI_SAME_STMT);
8080 gimple_assign_set_rhs_with_ops (gsi, NOP_EXPR, tem, NULL_TREE);
8082 /* Or without. */
8083 else
8084 gimple_assign_set_rhs_with_ops (gsi, BIT_XOR_EXPR, op0, op1);
8085 update_stmt (gsi_stmt (*gsi));
8087 return true;
8090 /* Simplify a division or modulo operator to a right shift or
8091 bitwise and if the first operand is unsigned or is greater
8092 than zero and the second operand is an exact power of two. */
8094 static bool
8095 simplify_div_or_mod_using_ranges (gimple stmt)
8097 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
8098 tree val = NULL;
8099 tree op0 = gimple_assign_rhs1 (stmt);
8100 tree op1 = gimple_assign_rhs2 (stmt);
8101 value_range_t *vr = get_value_range (gimple_assign_rhs1 (stmt));
8103 if (TYPE_UNSIGNED (TREE_TYPE (op0)))
8105 val = integer_one_node;
8107 else
8109 bool sop = false;
8111 val = compare_range_with_value (GE_EXPR, vr, integer_zero_node, &sop);
8113 if (val
8114 && sop
8115 && integer_onep (val)
8116 && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC))
8118 location_t location;
8120 if (!gimple_has_location (stmt))
8121 location = input_location;
8122 else
8123 location = gimple_location (stmt);
8124 warning_at (location, OPT_Wstrict_overflow,
8125 "assuming signed overflow does not occur when "
8126 "simplifying %</%> or %<%%%> to %<>>%> or %<&%>");
8130 if (val && integer_onep (val))
8132 tree t;
8134 if (rhs_code == TRUNC_DIV_EXPR)
8136 t = build_int_cst (integer_type_node, tree_log2 (op1));
8137 gimple_assign_set_rhs_code (stmt, RSHIFT_EXPR);
8138 gimple_assign_set_rhs1 (stmt, op0);
8139 gimple_assign_set_rhs2 (stmt, t);
8141 else
8143 t = build_int_cst (TREE_TYPE (op1), 1);
8144 t = int_const_binop (MINUS_EXPR, op1, t);
8145 t = fold_convert (TREE_TYPE (op0), t);
8147 gimple_assign_set_rhs_code (stmt, BIT_AND_EXPR);
8148 gimple_assign_set_rhs1 (stmt, op0);
8149 gimple_assign_set_rhs2 (stmt, t);
8152 update_stmt (stmt);
8153 return true;
8156 return false;
8159 /* If the operand to an ABS_EXPR is >= 0, then eliminate the
8160 ABS_EXPR. If the operand is <= 0, then simplify the
8161 ABS_EXPR into a NEGATE_EXPR. */
8163 static bool
8164 simplify_abs_using_ranges (gimple stmt)
8166 tree val = NULL;
8167 tree op = gimple_assign_rhs1 (stmt);
8168 tree type = TREE_TYPE (op);
8169 value_range_t *vr = get_value_range (op);
8171 if (TYPE_UNSIGNED (type))
8173 val = integer_zero_node;
8175 else if (vr)
8177 bool sop = false;
8179 val = compare_range_with_value (LE_EXPR, vr, integer_zero_node, &sop);
8180 if (!val)
8182 sop = false;
8183 val = compare_range_with_value (GE_EXPR, vr, integer_zero_node,
8184 &sop);
8186 if (val)
8188 if (integer_zerop (val))
8189 val = integer_one_node;
8190 else if (integer_onep (val))
8191 val = integer_zero_node;
8195 if (val
8196 && (integer_onep (val) || integer_zerop (val)))
8198 if (sop && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC))
8200 location_t location;
8202 if (!gimple_has_location (stmt))
8203 location = input_location;
8204 else
8205 location = gimple_location (stmt);
8206 warning_at (location, OPT_Wstrict_overflow,
8207 "assuming signed overflow does not occur when "
8208 "simplifying %<abs (X)%> to %<X%> or %<-X%>");
8211 gimple_assign_set_rhs1 (stmt, op);
8212 if (integer_onep (val))
8213 gimple_assign_set_rhs_code (stmt, NEGATE_EXPR);
8214 else
8215 gimple_assign_set_rhs_code (stmt, SSA_NAME);
8216 update_stmt (stmt);
8217 return true;
8221 return false;
8224 /* Optimize away redundant BIT_AND_EXPR and BIT_IOR_EXPR.
8225 If all the bits that are being cleared by & are already
8226 known to be zero from VR, or all the bits that are being
8227 set by | are already known to be one from VR, the bit
8228 operation is redundant. */
8230 static bool
8231 simplify_bit_ops_using_ranges (gimple_stmt_iterator *gsi, gimple stmt)
8233 tree op0 = gimple_assign_rhs1 (stmt);
8234 tree op1 = gimple_assign_rhs2 (stmt);
8235 tree op = NULL_TREE;
8236 value_range_t vr0 = VR_INITIALIZER;
8237 value_range_t vr1 = VR_INITIALIZER;
8238 double_int may_be_nonzero0, may_be_nonzero1;
8239 double_int must_be_nonzero0, must_be_nonzero1;
8240 double_int mask;
8242 if (TREE_CODE (op0) == SSA_NAME)
8243 vr0 = *(get_value_range (op0));
8244 else if (is_gimple_min_invariant (op0))
8245 set_value_range_to_value (&vr0, op0, NULL);
8246 else
8247 return false;
8249 if (TREE_CODE (op1) == SSA_NAME)
8250 vr1 = *(get_value_range (op1));
8251 else if (is_gimple_min_invariant (op1))
8252 set_value_range_to_value (&vr1, op1, NULL);
8253 else
8254 return false;
8256 if (!zero_nonzero_bits_from_vr (&vr0, &may_be_nonzero0, &must_be_nonzero0))
8257 return false;
8258 if (!zero_nonzero_bits_from_vr (&vr1, &may_be_nonzero1, &must_be_nonzero1))
8259 return false;
8261 switch (gimple_assign_rhs_code (stmt))
8263 case BIT_AND_EXPR:
8264 mask = may_be_nonzero0.and_not (must_be_nonzero1);
8265 if (mask.is_zero ())
8267 op = op0;
8268 break;
8270 mask = may_be_nonzero1.and_not (must_be_nonzero0);
8271 if (mask.is_zero ())
8273 op = op1;
8274 break;
8276 break;
8277 case BIT_IOR_EXPR:
8278 mask = may_be_nonzero0.and_not (must_be_nonzero1);
8279 if (mask.is_zero ())
8281 op = op1;
8282 break;
8284 mask = may_be_nonzero1.and_not (must_be_nonzero0);
8285 if (mask.is_zero ())
8287 op = op0;
8288 break;
8290 break;
8291 default:
8292 gcc_unreachable ();
8295 if (op == NULL_TREE)
8296 return false;
8298 gimple_assign_set_rhs_with_ops (gsi, TREE_CODE (op), op, NULL);
8299 update_stmt (gsi_stmt (*gsi));
8300 return true;
8303 /* We are comparing trees OP0 and OP1 using COND_CODE. OP0 has
8304 a known value range VR.
8306 If there is one and only one value which will satisfy the
8307 conditional, then return that value. Else return NULL. */
8309 static tree
8310 test_for_singularity (enum tree_code cond_code, tree op0,
8311 tree op1, value_range_t *vr)
8313 tree min = NULL;
8314 tree max = NULL;
8316 /* Extract minimum/maximum values which satisfy the
8317 the conditional as it was written. */
8318 if (cond_code == LE_EXPR || cond_code == LT_EXPR)
8320 /* This should not be negative infinity; there is no overflow
8321 here. */
8322 min = TYPE_MIN_VALUE (TREE_TYPE (op0));
8324 max = op1;
8325 if (cond_code == LT_EXPR && !is_overflow_infinity (max))
8327 tree one = build_int_cst (TREE_TYPE (op0), 1);
8328 max = fold_build2 (MINUS_EXPR, TREE_TYPE (op0), max, one);
8329 if (EXPR_P (max))
8330 TREE_NO_WARNING (max) = 1;
8333 else if (cond_code == GE_EXPR || cond_code == GT_EXPR)
8335 /* This should not be positive infinity; there is no overflow
8336 here. */
8337 max = TYPE_MAX_VALUE (TREE_TYPE (op0));
8339 min = op1;
8340 if (cond_code == GT_EXPR && !is_overflow_infinity (min))
8342 tree one = build_int_cst (TREE_TYPE (op0), 1);
8343 min = fold_build2 (PLUS_EXPR, TREE_TYPE (op0), min, one);
8344 if (EXPR_P (min))
8345 TREE_NO_WARNING (min) = 1;
8349 /* Now refine the minimum and maximum values using any
8350 value range information we have for op0. */
8351 if (min && max)
8353 if (compare_values (vr->min, min) == 1)
8354 min = vr->min;
8355 if (compare_values (vr->max, max) == -1)
8356 max = vr->max;
8358 /* If the new min/max values have converged to a single value,
8359 then there is only one value which can satisfy the condition,
8360 return that value. */
8361 if (operand_equal_p (min, max, 0) && is_gimple_min_invariant (min))
8362 return min;
8364 return NULL;
8367 /* Simplify a conditional using a relational operator to an equality
8368 test if the range information indicates only one value can satisfy
8369 the original conditional. */
8371 static bool
8372 simplify_cond_using_ranges (gimple stmt)
8374 tree op0 = gimple_cond_lhs (stmt);
8375 tree op1 = gimple_cond_rhs (stmt);
8376 enum tree_code cond_code = gimple_cond_code (stmt);
8378 if (cond_code != NE_EXPR
8379 && cond_code != EQ_EXPR
8380 && TREE_CODE (op0) == SSA_NAME
8381 && INTEGRAL_TYPE_P (TREE_TYPE (op0))
8382 && is_gimple_min_invariant (op1))
8384 value_range_t *vr = get_value_range (op0);
8386 /* If we have range information for OP0, then we might be
8387 able to simplify this conditional. */
8388 if (vr->type == VR_RANGE)
8390 tree new_tree = test_for_singularity (cond_code, op0, op1, vr);
8392 if (new_tree)
8394 if (dump_file)
8396 fprintf (dump_file, "Simplified relational ");
8397 print_gimple_stmt (dump_file, stmt, 0, 0);
8398 fprintf (dump_file, " into ");
8401 gimple_cond_set_code (stmt, EQ_EXPR);
8402 gimple_cond_set_lhs (stmt, op0);
8403 gimple_cond_set_rhs (stmt, new_tree);
8405 update_stmt (stmt);
8407 if (dump_file)
8409 print_gimple_stmt (dump_file, stmt, 0, 0);
8410 fprintf (dump_file, "\n");
8413 return true;
8416 /* Try again after inverting the condition. We only deal
8417 with integral types here, so no need to worry about
8418 issues with inverting FP comparisons. */
8419 cond_code = invert_tree_comparison (cond_code, false);
8420 new_tree = test_for_singularity (cond_code, op0, op1, vr);
8422 if (new_tree)
8424 if (dump_file)
8426 fprintf (dump_file, "Simplified relational ");
8427 print_gimple_stmt (dump_file, stmt, 0, 0);
8428 fprintf (dump_file, " into ");
8431 gimple_cond_set_code (stmt, NE_EXPR);
8432 gimple_cond_set_lhs (stmt, op0);
8433 gimple_cond_set_rhs (stmt, new_tree);
8435 update_stmt (stmt);
8437 if (dump_file)
8439 print_gimple_stmt (dump_file, stmt, 0, 0);
8440 fprintf (dump_file, "\n");
8443 return true;
8448 return false;
8451 /* Simplify a switch statement using the value range of the switch
8452 argument. */
8454 static bool
8455 simplify_switch_using_ranges (gimple stmt)
8457 tree op = gimple_switch_index (stmt);
8458 value_range_t *vr;
8459 bool take_default;
8460 edge e;
8461 edge_iterator ei;
8462 size_t i = 0, j = 0, n, n2;
8463 tree vec2;
8464 switch_update su;
8465 size_t k = 1, l = 0;
8467 if (TREE_CODE (op) == SSA_NAME)
8469 vr = get_value_range (op);
8471 /* We can only handle integer ranges. */
8472 if ((vr->type != VR_RANGE
8473 && vr->type != VR_ANTI_RANGE)
8474 || symbolic_range_p (vr))
8475 return false;
8477 /* Find case label for min/max of the value range. */
8478 take_default = !find_case_label_ranges (stmt, vr, &i, &j, &k, &l);
8480 else if (TREE_CODE (op) == INTEGER_CST)
8482 take_default = !find_case_label_index (stmt, 1, op, &i);
8483 if (take_default)
8485 i = 1;
8486 j = 0;
8488 else
8490 j = i;
8493 else
8494 return false;
8496 n = gimple_switch_num_labels (stmt);
8498 /* Bail out if this is just all edges taken. */
8499 if (i == 1
8500 && j == n - 1
8501 && take_default)
8502 return false;
8504 /* Build a new vector of taken case labels. */
8505 vec2 = make_tree_vec (j - i + 1 + l - k + 1 + (int)take_default);
8506 n2 = 0;
8508 /* Add the default edge, if necessary. */
8509 if (take_default)
8510 TREE_VEC_ELT (vec2, n2++) = gimple_switch_default_label (stmt);
8512 for (; i <= j; ++i, ++n2)
8513 TREE_VEC_ELT (vec2, n2) = gimple_switch_label (stmt, i);
8515 for (; k <= l; ++k, ++n2)
8516 TREE_VEC_ELT (vec2, n2) = gimple_switch_label (stmt, k);
8518 /* Mark needed edges. */
8519 for (i = 0; i < n2; ++i)
8521 e = find_edge (gimple_bb (stmt),
8522 label_to_block (CASE_LABEL (TREE_VEC_ELT (vec2, i))));
8523 e->aux = (void *)-1;
8526 /* Queue not needed edges for later removal. */
8527 FOR_EACH_EDGE (e, ei, gimple_bb (stmt)->succs)
8529 if (e->aux == (void *)-1)
8531 e->aux = NULL;
8532 continue;
8535 if (dump_file && (dump_flags & TDF_DETAILS))
8537 fprintf (dump_file, "removing unreachable case label\n");
8539 VEC_safe_push (edge, heap, to_remove_edges, e);
8540 e->flags &= ~EDGE_EXECUTABLE;
8543 /* And queue an update for the stmt. */
8544 su.stmt = stmt;
8545 su.vec = vec2;
8546 VEC_safe_push (switch_update, heap, to_update_switch_stmts, su);
8547 return false;
8550 /* Simplify an integral conversion from an SSA name in STMT. */
8552 static bool
8553 simplify_conversion_using_ranges (gimple stmt)
8555 tree innerop, middleop, finaltype;
8556 gimple def_stmt;
8557 value_range_t *innervr;
8558 bool inner_unsigned_p, middle_unsigned_p, final_unsigned_p;
8559 unsigned inner_prec, middle_prec, final_prec;
8560 double_int innermin, innermed, innermax, middlemin, middlemed, middlemax;
8562 finaltype = TREE_TYPE (gimple_assign_lhs (stmt));
8563 if (!INTEGRAL_TYPE_P (finaltype))
8564 return false;
8565 middleop = gimple_assign_rhs1 (stmt);
8566 def_stmt = SSA_NAME_DEF_STMT (middleop);
8567 if (!is_gimple_assign (def_stmt)
8568 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt)))
8569 return false;
8570 innerop = gimple_assign_rhs1 (def_stmt);
8571 if (TREE_CODE (innerop) != SSA_NAME)
8572 return false;
8574 /* Get the value-range of the inner operand. */
8575 innervr = get_value_range (innerop);
8576 if (innervr->type != VR_RANGE
8577 || TREE_CODE (innervr->min) != INTEGER_CST
8578 || TREE_CODE (innervr->max) != INTEGER_CST)
8579 return false;
8581 /* Simulate the conversion chain to check if the result is equal if
8582 the middle conversion is removed. */
8583 innermin = tree_to_double_int (innervr->min);
8584 innermax = tree_to_double_int (innervr->max);
8586 inner_prec = TYPE_PRECISION (TREE_TYPE (innerop));
8587 middle_prec = TYPE_PRECISION (TREE_TYPE (middleop));
8588 final_prec = TYPE_PRECISION (finaltype);
8590 /* If the first conversion is not injective, the second must not
8591 be widening. */
8592 if ((innermax - innermin).ugt (double_int::mask (middle_prec))
8593 && middle_prec < final_prec)
8594 return false;
8595 /* We also want a medium value so that we can track the effect that
8596 narrowing conversions with sign change have. */
8597 inner_unsigned_p = TYPE_UNSIGNED (TREE_TYPE (innerop));
8598 if (inner_unsigned_p)
8599 innermed = double_int::mask (inner_prec).lrshift (1, inner_prec);
8600 else
8601 innermed = double_int_zero;
8602 if (innermin.cmp (innermed, inner_unsigned_p) >= 0
8603 || innermed.cmp (innermax, inner_unsigned_p) >= 0)
8604 innermed = innermin;
8606 middle_unsigned_p = TYPE_UNSIGNED (TREE_TYPE (middleop));
8607 middlemin = innermin.ext (middle_prec, middle_unsigned_p);
8608 middlemed = innermed.ext (middle_prec, middle_unsigned_p);
8609 middlemax = innermax.ext (middle_prec, middle_unsigned_p);
8611 /* Require that the final conversion applied to both the original
8612 and the intermediate range produces the same result. */
8613 final_unsigned_p = TYPE_UNSIGNED (finaltype);
8614 if (middlemin.ext (final_prec, final_unsigned_p)
8615 != innermin.ext (final_prec, final_unsigned_p)
8616 || middlemed.ext (final_prec, final_unsigned_p)
8617 != innermed.ext (final_prec, final_unsigned_p)
8618 || middlemax.ext (final_prec, final_unsigned_p)
8619 != innermax.ext (final_prec, final_unsigned_p))
8620 return false;
8622 gimple_assign_set_rhs1 (stmt, innerop);
8623 update_stmt (stmt);
8624 return true;
8627 /* Return whether the value range *VR fits in an integer type specified
8628 by PRECISION and UNSIGNED_P. */
8630 static bool
8631 range_fits_type_p (value_range_t *vr, unsigned precision, bool unsigned_p)
8633 tree src_type;
8634 unsigned src_precision;
8635 double_int tem;
8637 /* We can only handle integral and pointer types. */
8638 src_type = TREE_TYPE (vr->min);
8639 if (!INTEGRAL_TYPE_P (src_type)
8640 && !POINTER_TYPE_P (src_type))
8641 return false;
8643 /* An extension is always fine, so is an identity transform. */
8644 src_precision = TYPE_PRECISION (TREE_TYPE (vr->min));
8645 if (src_precision < precision
8646 || (src_precision == precision
8647 && TYPE_UNSIGNED (src_type) == unsigned_p))
8648 return true;
8650 /* Now we can only handle ranges with constant bounds. */
8651 if (vr->type != VR_RANGE
8652 || TREE_CODE (vr->min) != INTEGER_CST
8653 || TREE_CODE (vr->max) != INTEGER_CST)
8654 return false;
8656 /* For precision-preserving sign-changes the MSB of the double-int
8657 has to be clear. */
8658 if (src_precision == precision
8659 && (TREE_INT_CST_HIGH (vr->min) | TREE_INT_CST_HIGH (vr->max)) < 0)
8660 return false;
8662 /* Then we can perform the conversion on both ends and compare
8663 the result for equality. */
8664 tem = tree_to_double_int (vr->min).ext (precision, unsigned_p);
8665 if (tree_to_double_int (vr->min) != tem)
8666 return false;
8667 tem = tree_to_double_int (vr->max).ext (precision, unsigned_p);
8668 if (tree_to_double_int (vr->max) != tem)
8669 return false;
8671 return true;
8674 /* Simplify a conversion from integral SSA name to float in STMT. */
8676 static bool
8677 simplify_float_conversion_using_ranges (gimple_stmt_iterator *gsi, gimple stmt)
8679 tree rhs1 = gimple_assign_rhs1 (stmt);
8680 value_range_t *vr = get_value_range (rhs1);
8681 enum machine_mode fltmode = TYPE_MODE (TREE_TYPE (gimple_assign_lhs (stmt)));
8682 enum machine_mode mode;
8683 tree tem;
8684 gimple conv;
8686 /* We can only handle constant ranges. */
8687 if (vr->type != VR_RANGE
8688 || TREE_CODE (vr->min) != INTEGER_CST
8689 || TREE_CODE (vr->max) != INTEGER_CST)
8690 return false;
8692 /* First check if we can use a signed type in place of an unsigned. */
8693 if (TYPE_UNSIGNED (TREE_TYPE (rhs1))
8694 && (can_float_p (fltmode, TYPE_MODE (TREE_TYPE (rhs1)), 0)
8695 != CODE_FOR_nothing)
8696 && range_fits_type_p (vr, GET_MODE_PRECISION
8697 (TYPE_MODE (TREE_TYPE (rhs1))), 0))
8698 mode = TYPE_MODE (TREE_TYPE (rhs1));
8699 /* If we can do the conversion in the current input mode do nothing. */
8700 else if (can_float_p (fltmode, TYPE_MODE (TREE_TYPE (rhs1)),
8701 TYPE_UNSIGNED (TREE_TYPE (rhs1))))
8702 return false;
8703 /* Otherwise search for a mode we can use, starting from the narrowest
8704 integer mode available. */
8705 else
8707 mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
8710 /* If we cannot do a signed conversion to float from mode
8711 or if the value-range does not fit in the signed type
8712 try with a wider mode. */
8713 if (can_float_p (fltmode, mode, 0) != CODE_FOR_nothing
8714 && range_fits_type_p (vr, GET_MODE_PRECISION (mode), 0))
8715 break;
8717 mode = GET_MODE_WIDER_MODE (mode);
8718 /* But do not widen the input. Instead leave that to the
8719 optabs expansion code. */
8720 if (GET_MODE_PRECISION (mode) > TYPE_PRECISION (TREE_TYPE (rhs1)))
8721 return false;
8723 while (mode != VOIDmode);
8724 if (mode == VOIDmode)
8725 return false;
8728 /* It works, insert a truncation or sign-change before the
8729 float conversion. */
8730 tem = make_ssa_name (build_nonstandard_integer_type
8731 (GET_MODE_PRECISION (mode), 0), NULL);
8732 conv = gimple_build_assign_with_ops (NOP_EXPR, tem, rhs1, NULL_TREE);
8733 gsi_insert_before (gsi, conv, GSI_SAME_STMT);
8734 gimple_assign_set_rhs1 (stmt, tem);
8735 update_stmt (stmt);
8737 return true;
8740 /* Simplify STMT using ranges if possible. */
8742 static bool
8743 simplify_stmt_using_ranges (gimple_stmt_iterator *gsi)
8745 gimple stmt = gsi_stmt (*gsi);
8746 if (is_gimple_assign (stmt))
8748 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
8749 tree rhs1 = gimple_assign_rhs1 (stmt);
8751 switch (rhs_code)
8753 case EQ_EXPR:
8754 case NE_EXPR:
8755 /* Transform EQ_EXPR, NE_EXPR into BIT_XOR_EXPR or identity
8756 if the RHS is zero or one, and the LHS are known to be boolean
8757 values. */
8758 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
8759 return simplify_truth_ops_using_ranges (gsi, stmt);
8760 break;
8762 /* Transform TRUNC_DIV_EXPR and TRUNC_MOD_EXPR into RSHIFT_EXPR
8763 and BIT_AND_EXPR respectively if the first operand is greater
8764 than zero and the second operand is an exact power of two. */
8765 case TRUNC_DIV_EXPR:
8766 case TRUNC_MOD_EXPR:
8767 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1))
8768 && integer_pow2p (gimple_assign_rhs2 (stmt)))
8769 return simplify_div_or_mod_using_ranges (stmt);
8770 break;
8772 /* Transform ABS (X) into X or -X as appropriate. */
8773 case ABS_EXPR:
8774 if (TREE_CODE (rhs1) == SSA_NAME
8775 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
8776 return simplify_abs_using_ranges (stmt);
8777 break;
8779 case BIT_AND_EXPR:
8780 case BIT_IOR_EXPR:
8781 /* Optimize away BIT_AND_EXPR and BIT_IOR_EXPR
8782 if all the bits being cleared are already cleared or
8783 all the bits being set are already set. */
8784 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
8785 return simplify_bit_ops_using_ranges (gsi, stmt);
8786 break;
8788 CASE_CONVERT:
8789 if (TREE_CODE (rhs1) == SSA_NAME
8790 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
8791 return simplify_conversion_using_ranges (stmt);
8792 break;
8794 case FLOAT_EXPR:
8795 if (TREE_CODE (rhs1) == SSA_NAME
8796 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
8797 return simplify_float_conversion_using_ranges (gsi, stmt);
8798 break;
8800 default:
8801 break;
8804 else if (gimple_code (stmt) == GIMPLE_COND)
8805 return simplify_cond_using_ranges (stmt);
8806 else if (gimple_code (stmt) == GIMPLE_SWITCH)
8807 return simplify_switch_using_ranges (stmt);
8809 return false;
8812 /* If the statement pointed by SI has a predicate whose value can be
8813 computed using the value range information computed by VRP, compute
8814 its value and return true. Otherwise, return false. */
8816 static bool
8817 fold_predicate_in (gimple_stmt_iterator *si)
8819 bool assignment_p = false;
8820 tree val;
8821 gimple stmt = gsi_stmt (*si);
8823 if (is_gimple_assign (stmt)
8824 && TREE_CODE_CLASS (gimple_assign_rhs_code (stmt)) == tcc_comparison)
8826 assignment_p = true;
8827 val = vrp_evaluate_conditional (gimple_assign_rhs_code (stmt),
8828 gimple_assign_rhs1 (stmt),
8829 gimple_assign_rhs2 (stmt),
8830 stmt);
8832 else if (gimple_code (stmt) == GIMPLE_COND)
8833 val = vrp_evaluate_conditional (gimple_cond_code (stmt),
8834 gimple_cond_lhs (stmt),
8835 gimple_cond_rhs (stmt),
8836 stmt);
8837 else
8838 return false;
8840 if (val)
8842 if (assignment_p)
8843 val = fold_convert (gimple_expr_type (stmt), val);
8845 if (dump_file)
8847 fprintf (dump_file, "Folding predicate ");
8848 print_gimple_expr (dump_file, stmt, 0, 0);
8849 fprintf (dump_file, " to ");
8850 print_generic_expr (dump_file, val, 0);
8851 fprintf (dump_file, "\n");
8854 if (is_gimple_assign (stmt))
8855 gimple_assign_set_rhs_from_tree (si, val);
8856 else
8858 gcc_assert (gimple_code (stmt) == GIMPLE_COND);
8859 if (integer_zerop (val))
8860 gimple_cond_make_false (stmt);
8861 else if (integer_onep (val))
8862 gimple_cond_make_true (stmt);
8863 else
8864 gcc_unreachable ();
8867 return true;
8870 return false;
8873 /* Callback for substitute_and_fold folding the stmt at *SI. */
8875 static bool
8876 vrp_fold_stmt (gimple_stmt_iterator *si)
8878 if (fold_predicate_in (si))
8879 return true;
8881 return simplify_stmt_using_ranges (si);
8884 /* Stack of dest,src equivalency pairs that need to be restored after
8885 each attempt to thread a block's incoming edge to an outgoing edge.
8887 A NULL entry is used to mark the end of pairs which need to be
8888 restored. */
8889 static VEC(tree,heap) *equiv_stack;
8891 /* A trivial wrapper so that we can present the generic jump threading
8892 code with a simple API for simplifying statements. STMT is the
8893 statement we want to simplify, WITHIN_STMT provides the location
8894 for any overflow warnings. */
8896 static tree
8897 simplify_stmt_for_jump_threading (gimple stmt, gimple within_stmt)
8899 /* We only use VRP information to simplify conditionals. This is
8900 overly conservative, but it's unclear if doing more would be
8901 worth the compile time cost. */
8902 if (gimple_code (stmt) != GIMPLE_COND)
8903 return NULL;
8905 return vrp_evaluate_conditional (gimple_cond_code (stmt),
8906 gimple_cond_lhs (stmt),
8907 gimple_cond_rhs (stmt), within_stmt);
8910 /* Blocks which have more than one predecessor and more than
8911 one successor present jump threading opportunities, i.e.,
8912 when the block is reached from a specific predecessor, we
8913 may be able to determine which of the outgoing edges will
8914 be traversed. When this optimization applies, we are able
8915 to avoid conditionals at runtime and we may expose secondary
8916 optimization opportunities.
8918 This routine is effectively a driver for the generic jump
8919 threading code. It basically just presents the generic code
8920 with edges that may be suitable for jump threading.
8922 Unlike DOM, we do not iterate VRP if jump threading was successful.
8923 While iterating may expose new opportunities for VRP, it is expected
8924 those opportunities would be very limited and the compile time cost
8925 to expose those opportunities would be significant.
8927 As jump threading opportunities are discovered, they are registered
8928 for later realization. */
8930 static void
8931 identify_jump_threads (void)
8933 basic_block bb;
8934 gimple dummy;
8935 int i;
8936 edge e;
8938 /* Ugh. When substituting values earlier in this pass we can
8939 wipe the dominance information. So rebuild the dominator
8940 information as we need it within the jump threading code. */
8941 calculate_dominance_info (CDI_DOMINATORS);
8943 /* We do not allow VRP information to be used for jump threading
8944 across a back edge in the CFG. Otherwise it becomes too
8945 difficult to avoid eliminating loop exit tests. Of course
8946 EDGE_DFS_BACK is not accurate at this time so we have to
8947 recompute it. */
8948 mark_dfs_back_edges ();
8950 /* Do not thread across edges we are about to remove. Just marking
8951 them as EDGE_DFS_BACK will do. */
8952 FOR_EACH_VEC_ELT (edge, to_remove_edges, i, e)
8953 e->flags |= EDGE_DFS_BACK;
8955 /* Allocate our unwinder stack to unwind any temporary equivalences
8956 that might be recorded. */
8957 equiv_stack = VEC_alloc (tree, heap, 20);
8959 /* To avoid lots of silly node creation, we create a single
8960 conditional and just modify it in-place when attempting to
8961 thread jumps. */
8962 dummy = gimple_build_cond (EQ_EXPR,
8963 integer_zero_node, integer_zero_node,
8964 NULL, NULL);
8966 /* Walk through all the blocks finding those which present a
8967 potential jump threading opportunity. We could set this up
8968 as a dominator walker and record data during the walk, but
8969 I doubt it's worth the effort for the classes of jump
8970 threading opportunities we are trying to identify at this
8971 point in compilation. */
8972 FOR_EACH_BB (bb)
8974 gimple last;
8976 /* If the generic jump threading code does not find this block
8977 interesting, then there is nothing to do. */
8978 if (! potentially_threadable_block (bb))
8979 continue;
8981 /* We only care about blocks ending in a COND_EXPR. While there
8982 may be some value in handling SWITCH_EXPR here, I doubt it's
8983 terribly important. */
8984 last = gsi_stmt (gsi_last_bb (bb));
8986 /* We're basically looking for a switch or any kind of conditional with
8987 integral or pointer type arguments. Note the type of the second
8988 argument will be the same as the first argument, so no need to
8989 check it explicitly. */
8990 if (gimple_code (last) == GIMPLE_SWITCH
8991 || (gimple_code (last) == GIMPLE_COND
8992 && TREE_CODE (gimple_cond_lhs (last)) == SSA_NAME
8993 && (INTEGRAL_TYPE_P (TREE_TYPE (gimple_cond_lhs (last)))
8994 || POINTER_TYPE_P (TREE_TYPE (gimple_cond_lhs (last))))
8995 && (TREE_CODE (gimple_cond_rhs (last)) == SSA_NAME
8996 || is_gimple_min_invariant (gimple_cond_rhs (last)))))
8998 edge_iterator ei;
9000 /* We've got a block with multiple predecessors and multiple
9001 successors which also ends in a suitable conditional or
9002 switch statement. For each predecessor, see if we can thread
9003 it to a specific successor. */
9004 FOR_EACH_EDGE (e, ei, bb->preds)
9006 /* Do not thread across back edges or abnormal edges
9007 in the CFG. */
9008 if (e->flags & (EDGE_DFS_BACK | EDGE_COMPLEX))
9009 continue;
9011 thread_across_edge (dummy, e, true, &equiv_stack,
9012 simplify_stmt_for_jump_threading);
9017 /* We do not actually update the CFG or SSA graphs at this point as
9018 ASSERT_EXPRs are still in the IL and cfg cleanup code does not yet
9019 handle ASSERT_EXPRs gracefully. */
9022 /* We identified all the jump threading opportunities earlier, but could
9023 not transform the CFG at that time. This routine transforms the
9024 CFG and arranges for the dominator tree to be rebuilt if necessary.
9026 Note the SSA graph update will occur during the normal TODO
9027 processing by the pass manager. */
9028 static void
9029 finalize_jump_threads (void)
9031 thread_through_all_blocks (false);
9032 VEC_free (tree, heap, equiv_stack);
9036 /* Traverse all the blocks folding conditionals with known ranges. */
9038 static void
9039 vrp_finalize (void)
9041 size_t i;
9043 values_propagated = true;
9045 if (dump_file)
9047 fprintf (dump_file, "\nValue ranges after VRP:\n\n");
9048 dump_all_value_ranges (dump_file);
9049 fprintf (dump_file, "\n");
9052 substitute_and_fold (op_with_constant_singleton_value_range,
9053 vrp_fold_stmt, false);
9055 if (warn_array_bounds)
9056 check_all_array_refs ();
9058 /* We must identify jump threading opportunities before we release
9059 the datastructures built by VRP. */
9060 identify_jump_threads ();
9062 /* Free allocated memory. */
9063 for (i = 0; i < num_vr_values; i++)
9064 if (vr_value[i])
9066 BITMAP_FREE (vr_value[i]->equiv);
9067 free (vr_value[i]);
9070 free (vr_value);
9071 free (vr_phi_edge_counts);
9073 /* So that we can distinguish between VRP data being available
9074 and not available. */
9075 vr_value = NULL;
9076 vr_phi_edge_counts = NULL;
9080 /* Main entry point to VRP (Value Range Propagation). This pass is
9081 loosely based on J. R. C. Patterson, ``Accurate Static Branch
9082 Prediction by Value Range Propagation,'' in SIGPLAN Conference on
9083 Programming Language Design and Implementation, pp. 67-78, 1995.
9084 Also available at http://citeseer.ist.psu.edu/patterson95accurate.html
9086 This is essentially an SSA-CCP pass modified to deal with ranges
9087 instead of constants.
9089 While propagating ranges, we may find that two or more SSA name
9090 have equivalent, though distinct ranges. For instance,
9092 1 x_9 = p_3->a;
9093 2 p_4 = ASSERT_EXPR <p_3, p_3 != 0>
9094 3 if (p_4 == q_2)
9095 4 p_5 = ASSERT_EXPR <p_4, p_4 == q_2>;
9096 5 endif
9097 6 if (q_2)
9099 In the code above, pointer p_5 has range [q_2, q_2], but from the
9100 code we can also determine that p_5 cannot be NULL and, if q_2 had
9101 a non-varying range, p_5's range should also be compatible with it.
9103 These equivalences are created by two expressions: ASSERT_EXPR and
9104 copy operations. Since p_5 is an assertion on p_4, and p_4 was the
9105 result of another assertion, then we can use the fact that p_5 and
9106 p_4 are equivalent when evaluating p_5's range.
9108 Together with value ranges, we also propagate these equivalences
9109 between names so that we can take advantage of information from
9110 multiple ranges when doing final replacement. Note that this
9111 equivalency relation is transitive but not symmetric.
9113 In the example above, p_5 is equivalent to p_4, q_2 and p_3, but we
9114 cannot assert that q_2 is equivalent to p_5 because q_2 may be used
9115 in contexts where that assertion does not hold (e.g., in line 6).
9117 TODO, the main difference between this pass and Patterson's is that
9118 we do not propagate edge probabilities. We only compute whether
9119 edges can be taken or not. That is, instead of having a spectrum
9120 of jump probabilities between 0 and 1, we only deal with 0, 1 and
9121 DON'T KNOW. In the future, it may be worthwhile to propagate
9122 probabilities to aid branch prediction. */
9124 static unsigned int
9125 execute_vrp (void)
9127 int i;
9128 edge e;
9129 switch_update *su;
9131 loop_optimizer_init (LOOPS_NORMAL | LOOPS_HAVE_RECORDED_EXITS);
9132 rewrite_into_loop_closed_ssa (NULL, TODO_update_ssa);
9133 scev_initialize ();
9135 insert_range_assertions ();
9137 to_remove_edges = VEC_alloc (edge, heap, 10);
9138 to_update_switch_stmts = VEC_alloc (switch_update, heap, 5);
9139 threadedge_initialize_values ();
9141 vrp_initialize ();
9142 ssa_propagate (vrp_visit_stmt, vrp_visit_phi_node);
9143 vrp_finalize ();
9145 free_numbers_of_iterations_estimates ();
9147 /* ASSERT_EXPRs must be removed before finalizing jump threads
9148 as finalizing jump threads calls the CFG cleanup code which
9149 does not properly handle ASSERT_EXPRs. */
9150 remove_range_assertions ();
9152 /* If we exposed any new variables, go ahead and put them into
9153 SSA form now, before we handle jump threading. This simplifies
9154 interactions between rewriting of _DECL nodes into SSA form
9155 and rewriting SSA_NAME nodes into SSA form after block
9156 duplication and CFG manipulation. */
9157 update_ssa (TODO_update_ssa);
9159 finalize_jump_threads ();
9161 /* Remove dead edges from SWITCH_EXPR optimization. This leaves the
9162 CFG in a broken state and requires a cfg_cleanup run. */
9163 FOR_EACH_VEC_ELT (edge, to_remove_edges, i, e)
9164 remove_edge (e);
9165 /* Update SWITCH_EXPR case label vector. */
9166 FOR_EACH_VEC_ELT (switch_update, to_update_switch_stmts, i, su)
9168 size_t j;
9169 size_t n = TREE_VEC_LENGTH (su->vec);
9170 tree label;
9171 gimple_switch_set_num_labels (su->stmt, n);
9172 for (j = 0; j < n; j++)
9173 gimple_switch_set_label (su->stmt, j, TREE_VEC_ELT (su->vec, j));
9174 /* As we may have replaced the default label with a regular one
9175 make sure to make it a real default label again. This ensures
9176 optimal expansion. */
9177 label = gimple_switch_label (su->stmt, 0);
9178 CASE_LOW (label) = NULL_TREE;
9179 CASE_HIGH (label) = NULL_TREE;
9182 if (VEC_length (edge, to_remove_edges) > 0)
9183 free_dominance_info (CDI_DOMINATORS);
9185 VEC_free (edge, heap, to_remove_edges);
9186 VEC_free (switch_update, heap, to_update_switch_stmts);
9187 threadedge_finalize_values ();
9189 scev_finalize ();
9190 loop_optimizer_finalize ();
9191 return 0;
9194 static bool
9195 gate_vrp (void)
9197 return flag_tree_vrp != 0;
9200 struct gimple_opt_pass pass_vrp =
9203 GIMPLE_PASS,
9204 "vrp", /* name */
9205 gate_vrp, /* gate */
9206 execute_vrp, /* execute */
9207 NULL, /* sub */
9208 NULL, /* next */
9209 0, /* static_pass_number */
9210 TV_TREE_VRP, /* tv_id */
9211 PROP_ssa, /* properties_required */
9212 0, /* properties_provided */
9213 0, /* properties_destroyed */
9214 0, /* todo_flags_start */
9215 TODO_cleanup_cfg
9216 | TODO_update_ssa
9217 | TODO_verify_ssa
9218 | TODO_verify_flow
9219 | TODO_ggc_collect /* todo_flags_finish */