Make std::vector<bool> meet C++11 allocator requirements.
[official-gcc.git] / gcc / tree-vrp.c
blob2264242b9ad1cdb29be5da04b431bbb1f5ee3c0a
1 /* Support routines for Value Range Propagation (VRP).
2 Copyright (C) 2005-2014 Free Software Foundation, Inc.
3 Contributed by Diego Novillo <dnovillo@redhat.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "flags.h"
26 #include "tree.h"
27 #include "stor-layout.h"
28 #include "calls.h"
29 #include "predict.h"
30 #include "vec.h"
31 #include "hashtab.h"
32 #include "hash-set.h"
33 #include "machmode.h"
34 #include "hard-reg-set.h"
35 #include "input.h"
36 #include "function.h"
37 #include "dominance.h"
38 #include "cfg.h"
39 #include "cfganal.h"
40 #include "basic-block.h"
41 #include "tree-ssa-alias.h"
42 #include "internal-fn.h"
43 #include "gimple-fold.h"
44 #include "tree-eh.h"
45 #include "gimple-expr.h"
46 #include "is-a.h"
47 #include "gimple.h"
48 #include "gimple-iterator.h"
49 #include "gimple-walk.h"
50 #include "gimple-ssa.h"
51 #include "tree-cfg.h"
52 #include "tree-phinodes.h"
53 #include "ssa-iterators.h"
54 #include "stringpool.h"
55 #include "tree-ssanames.h"
56 #include "tree-ssa-loop-manip.h"
57 #include "tree-ssa-loop-niter.h"
58 #include "tree-ssa-loop.h"
59 #include "tree-into-ssa.h"
60 #include "tree-ssa.h"
61 #include "tree-pass.h"
62 #include "tree-dump.h"
63 #include "gimple-pretty-print.h"
64 #include "diagnostic-core.h"
65 #include "intl.h"
66 #include "cfgloop.h"
67 #include "tree-scalar-evolution.h"
68 #include "tree-ssa-propagate.h"
69 #include "tree-chrec.h"
70 #include "tree-ssa-threadupdate.h"
71 #include "expr.h"
72 #include "optabs.h"
73 #include "tree-ssa-threadedge.h"
74 #include "wide-int.h"
78 /* Range of values that can be associated with an SSA_NAME after VRP
79 has executed. */
80 struct value_range_d
82 /* Lattice value represented by this range. */
83 enum value_range_type type;
85 /* Minimum and maximum values represented by this range. These
86 values should be interpreted as follows:
88 - If TYPE is VR_UNDEFINED or VR_VARYING then MIN and MAX must
89 be NULL.
91 - If TYPE == VR_RANGE then MIN holds the minimum value and
92 MAX holds the maximum value of the range [MIN, MAX].
94 - If TYPE == ANTI_RANGE the variable is known to NOT
95 take any values in the range [MIN, MAX]. */
96 tree min;
97 tree max;
99 /* Set of SSA names whose value ranges are equivalent to this one.
100 This set is only valid when TYPE is VR_RANGE or VR_ANTI_RANGE. */
101 bitmap equiv;
104 typedef struct value_range_d value_range_t;
106 #define VR_INITIALIZER { VR_UNDEFINED, NULL_TREE, NULL_TREE, NULL }
108 /* Set of SSA names found live during the RPO traversal of the function
109 for still active basic-blocks. */
110 static sbitmap *live;
112 /* Return true if the SSA name NAME is live on the edge E. */
114 static bool
115 live_on_edge (edge e, tree name)
117 return (live[e->dest->index]
118 && bitmap_bit_p (live[e->dest->index], SSA_NAME_VERSION (name)));
121 /* Local functions. */
122 static int compare_values (tree val1, tree val2);
123 static int compare_values_warnv (tree val1, tree val2, bool *);
124 static void vrp_meet (value_range_t *, value_range_t *);
125 static void vrp_intersect_ranges (value_range_t *, value_range_t *);
126 static tree vrp_evaluate_conditional_warnv_with_ops (enum tree_code,
127 tree, tree, bool, bool *,
128 bool *);
130 /* Location information for ASSERT_EXPRs. Each instance of this
131 structure describes an ASSERT_EXPR for an SSA name. Since a single
132 SSA name may have more than one assertion associated with it, these
133 locations are kept in a linked list attached to the corresponding
134 SSA name. */
135 struct assert_locus_d
137 /* Basic block where the assertion would be inserted. */
138 basic_block bb;
140 /* Some assertions need to be inserted on an edge (e.g., assertions
141 generated by COND_EXPRs). In those cases, BB will be NULL. */
142 edge e;
144 /* Pointer to the statement that generated this assertion. */
145 gimple_stmt_iterator si;
147 /* Predicate code for the ASSERT_EXPR. Must be COMPARISON_CLASS_P. */
148 enum tree_code comp_code;
150 /* Value being compared against. */
151 tree val;
153 /* Expression to compare. */
154 tree expr;
156 /* Next node in the linked list. */
157 struct assert_locus_d *next;
160 typedef struct assert_locus_d *assert_locus_t;
162 /* If bit I is present, it means that SSA name N_i has a list of
163 assertions that should be inserted in the IL. */
164 static bitmap need_assert_for;
166 /* Array of locations lists where to insert assertions. ASSERTS_FOR[I]
167 holds a list of ASSERT_LOCUS_T nodes that describe where
168 ASSERT_EXPRs for SSA name N_I should be inserted. */
169 static assert_locus_t *asserts_for;
171 /* Value range array. After propagation, VR_VALUE[I] holds the range
172 of values that SSA name N_I may take. */
173 static unsigned num_vr_values;
174 static value_range_t **vr_value;
175 static bool values_propagated;
177 /* For a PHI node which sets SSA name N_I, VR_COUNTS[I] holds the
178 number of executable edges we saw the last time we visited the
179 node. */
180 static int *vr_phi_edge_counts;
182 typedef struct {
183 gimple stmt;
184 tree vec;
185 } switch_update;
187 static vec<edge> to_remove_edges;
188 static vec<switch_update> to_update_switch_stmts;
191 /* Return the maximum value for TYPE. */
193 static inline tree
194 vrp_val_max (const_tree type)
196 if (!INTEGRAL_TYPE_P (type))
197 return NULL_TREE;
199 return TYPE_MAX_VALUE (type);
202 /* Return the minimum value for TYPE. */
204 static inline tree
205 vrp_val_min (const_tree type)
207 if (!INTEGRAL_TYPE_P (type))
208 return NULL_TREE;
210 return TYPE_MIN_VALUE (type);
213 /* Return whether VAL is equal to the maximum value of its type. This
214 will be true for a positive overflow infinity. We can't do a
215 simple equality comparison with TYPE_MAX_VALUE because C typedefs
216 and Ada subtypes can produce types whose TYPE_MAX_VALUE is not ==
217 to the integer constant with the same value in the type. */
219 static inline bool
220 vrp_val_is_max (const_tree val)
222 tree type_max = vrp_val_max (TREE_TYPE (val));
223 return (val == type_max
224 || (type_max != NULL_TREE
225 && operand_equal_p (val, type_max, 0)));
228 /* Return whether VAL is equal to the minimum value of its type. This
229 will be true for a negative overflow infinity. */
231 static inline bool
232 vrp_val_is_min (const_tree val)
234 tree type_min = vrp_val_min (TREE_TYPE (val));
235 return (val == type_min
236 || (type_min != NULL_TREE
237 && operand_equal_p (val, type_min, 0)));
241 /* Return whether TYPE should use an overflow infinity distinct from
242 TYPE_{MIN,MAX}_VALUE. We use an overflow infinity value to
243 represent a signed overflow during VRP computations. An infinity
244 is distinct from a half-range, which will go from some number to
245 TYPE_{MIN,MAX}_VALUE. */
247 static inline bool
248 needs_overflow_infinity (const_tree type)
250 return INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_WRAPS (type);
253 /* Return whether TYPE can support our overflow infinity
254 representation: we use the TREE_OVERFLOW flag, which only exists
255 for constants. If TYPE doesn't support this, we don't optimize
256 cases which would require signed overflow--we drop them to
257 VARYING. */
259 static inline bool
260 supports_overflow_infinity (const_tree type)
262 tree min = vrp_val_min (type), max = vrp_val_max (type);
263 #ifdef ENABLE_CHECKING
264 gcc_assert (needs_overflow_infinity (type));
265 #endif
266 return (min != NULL_TREE
267 && CONSTANT_CLASS_P (min)
268 && max != NULL_TREE
269 && CONSTANT_CLASS_P (max));
272 /* VAL is the maximum or minimum value of a type. Return a
273 corresponding overflow infinity. */
275 static inline tree
276 make_overflow_infinity (tree val)
278 gcc_checking_assert (val != NULL_TREE && CONSTANT_CLASS_P (val));
279 val = copy_node (val);
280 TREE_OVERFLOW (val) = 1;
281 return val;
284 /* Return a negative overflow infinity for TYPE. */
286 static inline tree
287 negative_overflow_infinity (tree type)
289 gcc_checking_assert (supports_overflow_infinity (type));
290 return make_overflow_infinity (vrp_val_min (type));
293 /* Return a positive overflow infinity for TYPE. */
295 static inline tree
296 positive_overflow_infinity (tree type)
298 gcc_checking_assert (supports_overflow_infinity (type));
299 return make_overflow_infinity (vrp_val_max (type));
302 /* Return whether VAL is a negative overflow infinity. */
304 static inline bool
305 is_negative_overflow_infinity (const_tree val)
307 return (TREE_OVERFLOW_P (val)
308 && needs_overflow_infinity (TREE_TYPE (val))
309 && vrp_val_is_min (val));
312 /* Return whether VAL is a positive overflow infinity. */
314 static inline bool
315 is_positive_overflow_infinity (const_tree val)
317 return (TREE_OVERFLOW_P (val)
318 && needs_overflow_infinity (TREE_TYPE (val))
319 && vrp_val_is_max (val));
322 /* Return whether VAL is a positive or negative overflow infinity. */
324 static inline bool
325 is_overflow_infinity (const_tree val)
327 return (TREE_OVERFLOW_P (val)
328 && needs_overflow_infinity (TREE_TYPE (val))
329 && (vrp_val_is_min (val) || vrp_val_is_max (val)));
332 /* Return whether STMT has a constant rhs that is_overflow_infinity. */
334 static inline bool
335 stmt_overflow_infinity (gimple stmt)
337 if (is_gimple_assign (stmt)
338 && get_gimple_rhs_class (gimple_assign_rhs_code (stmt)) ==
339 GIMPLE_SINGLE_RHS)
340 return is_overflow_infinity (gimple_assign_rhs1 (stmt));
341 return false;
344 /* If VAL is now an overflow infinity, return VAL. Otherwise, return
345 the same value with TREE_OVERFLOW clear. This can be used to avoid
346 confusing a regular value with an overflow value. */
348 static inline tree
349 avoid_overflow_infinity (tree val)
351 if (!is_overflow_infinity (val))
352 return val;
354 if (vrp_val_is_max (val))
355 return vrp_val_max (TREE_TYPE (val));
356 else
358 gcc_checking_assert (vrp_val_is_min (val));
359 return vrp_val_min (TREE_TYPE (val));
364 /* Return true if ARG is marked with the nonnull attribute in the
365 current function signature. */
367 static bool
368 nonnull_arg_p (const_tree arg)
370 tree t, attrs, fntype;
371 unsigned HOST_WIDE_INT arg_num;
373 gcc_assert (TREE_CODE (arg) == PARM_DECL && POINTER_TYPE_P (TREE_TYPE (arg)));
375 /* The static chain decl is always non null. */
376 if (arg == cfun->static_chain_decl)
377 return true;
379 fntype = TREE_TYPE (current_function_decl);
380 for (attrs = TYPE_ATTRIBUTES (fntype); attrs; attrs = TREE_CHAIN (attrs))
382 attrs = lookup_attribute ("nonnull", attrs);
384 /* If "nonnull" wasn't specified, we know nothing about the argument. */
385 if (attrs == NULL_TREE)
386 return false;
388 /* If "nonnull" applies to all the arguments, then ARG is non-null. */
389 if (TREE_VALUE (attrs) == NULL_TREE)
390 return true;
392 /* Get the position number for ARG in the function signature. */
393 for (arg_num = 1, t = DECL_ARGUMENTS (current_function_decl);
395 t = DECL_CHAIN (t), arg_num++)
397 if (t == arg)
398 break;
401 gcc_assert (t == arg);
403 /* Now see if ARG_NUM is mentioned in the nonnull list. */
404 for (t = TREE_VALUE (attrs); t; t = TREE_CHAIN (t))
406 if (compare_tree_int (TREE_VALUE (t), arg_num) == 0)
407 return true;
411 return false;
415 /* Set value range VR to VR_UNDEFINED. */
417 static inline void
418 set_value_range_to_undefined (value_range_t *vr)
420 vr->type = VR_UNDEFINED;
421 vr->min = vr->max = NULL_TREE;
422 if (vr->equiv)
423 bitmap_clear (vr->equiv);
427 /* Set value range VR to VR_VARYING. */
429 static inline void
430 set_value_range_to_varying (value_range_t *vr)
432 vr->type = VR_VARYING;
433 vr->min = vr->max = NULL_TREE;
434 if (vr->equiv)
435 bitmap_clear (vr->equiv);
439 /* Set value range VR to {T, MIN, MAX, EQUIV}. */
441 static void
442 set_value_range (value_range_t *vr, enum value_range_type t, tree min,
443 tree max, bitmap equiv)
445 #if defined ENABLE_CHECKING
446 /* Check the validity of the range. */
447 if (t == VR_RANGE || t == VR_ANTI_RANGE)
449 int cmp;
451 gcc_assert (min && max);
453 gcc_assert ((!TREE_OVERFLOW_P (min) || is_overflow_infinity (min))
454 && (!TREE_OVERFLOW_P (max) || is_overflow_infinity (max)));
456 if (INTEGRAL_TYPE_P (TREE_TYPE (min)) && t == VR_ANTI_RANGE)
457 gcc_assert (!vrp_val_is_min (min) || !vrp_val_is_max (max));
459 cmp = compare_values (min, max);
460 gcc_assert (cmp == 0 || cmp == -1 || cmp == -2);
462 if (needs_overflow_infinity (TREE_TYPE (min)))
463 gcc_assert (!is_overflow_infinity (min)
464 || !is_overflow_infinity (max));
467 if (t == VR_UNDEFINED || t == VR_VARYING)
468 gcc_assert (min == NULL_TREE && max == NULL_TREE);
470 if (t == VR_UNDEFINED || t == VR_VARYING)
471 gcc_assert (equiv == NULL || bitmap_empty_p (equiv));
472 #endif
474 vr->type = t;
475 vr->min = min;
476 vr->max = max;
478 /* Since updating the equivalence set involves deep copying the
479 bitmaps, only do it if absolutely necessary. */
480 if (vr->equiv == NULL
481 && equiv != NULL)
482 vr->equiv = BITMAP_ALLOC (NULL);
484 if (equiv != vr->equiv)
486 if (equiv && !bitmap_empty_p (equiv))
487 bitmap_copy (vr->equiv, equiv);
488 else
489 bitmap_clear (vr->equiv);
494 /* Set value range VR to the canonical form of {T, MIN, MAX, EQUIV}.
495 This means adjusting T, MIN and MAX representing the case of a
496 wrapping range with MAX < MIN covering [MIN, type_max] U [type_min, MAX]
497 as anti-rage ~[MAX+1, MIN-1]. Likewise for wrapping anti-ranges.
498 In corner cases where MAX+1 or MIN-1 wraps this will fall back
499 to varying.
500 This routine exists to ease canonicalization in the case where we
501 extract ranges from var + CST op limit. */
503 static void
504 set_and_canonicalize_value_range (value_range_t *vr, enum value_range_type t,
505 tree min, tree max, bitmap equiv)
507 /* Use the canonical setters for VR_UNDEFINED and VR_VARYING. */
508 if (t == VR_UNDEFINED)
510 set_value_range_to_undefined (vr);
511 return;
513 else if (t == VR_VARYING)
515 set_value_range_to_varying (vr);
516 return;
519 /* Nothing to canonicalize for symbolic ranges. */
520 if (TREE_CODE (min) != INTEGER_CST
521 || TREE_CODE (max) != INTEGER_CST)
523 set_value_range (vr, t, min, max, equiv);
524 return;
527 /* Wrong order for min and max, to swap them and the VR type we need
528 to adjust them. */
529 if (tree_int_cst_lt (max, min))
531 tree one, tmp;
533 /* For one bit precision if max < min, then the swapped
534 range covers all values, so for VR_RANGE it is varying and
535 for VR_ANTI_RANGE empty range, so drop to varying as well. */
536 if (TYPE_PRECISION (TREE_TYPE (min)) == 1)
538 set_value_range_to_varying (vr);
539 return;
542 one = build_int_cst (TREE_TYPE (min), 1);
543 tmp = int_const_binop (PLUS_EXPR, max, one);
544 max = int_const_binop (MINUS_EXPR, min, one);
545 min = tmp;
547 /* There's one corner case, if we had [C+1, C] before we now have
548 that again. But this represents an empty value range, so drop
549 to varying in this case. */
550 if (tree_int_cst_lt (max, min))
552 set_value_range_to_varying (vr);
553 return;
556 t = t == VR_RANGE ? VR_ANTI_RANGE : VR_RANGE;
559 /* Anti-ranges that can be represented as ranges should be so. */
560 if (t == VR_ANTI_RANGE)
562 bool is_min = vrp_val_is_min (min);
563 bool is_max = vrp_val_is_max (max);
565 if (is_min && is_max)
567 /* We cannot deal with empty ranges, drop to varying.
568 ??? This could be VR_UNDEFINED instead. */
569 set_value_range_to_varying (vr);
570 return;
572 else if (TYPE_PRECISION (TREE_TYPE (min)) == 1
573 && (is_min || is_max))
575 /* Non-empty boolean ranges can always be represented
576 as a singleton range. */
577 if (is_min)
578 min = max = vrp_val_max (TREE_TYPE (min));
579 else
580 min = max = vrp_val_min (TREE_TYPE (min));
581 t = VR_RANGE;
583 else if (is_min
584 /* As a special exception preserve non-null ranges. */
585 && !(TYPE_UNSIGNED (TREE_TYPE (min))
586 && integer_zerop (max)))
588 tree one = build_int_cst (TREE_TYPE (max), 1);
589 min = int_const_binop (PLUS_EXPR, max, one);
590 max = vrp_val_max (TREE_TYPE (max));
591 t = VR_RANGE;
593 else if (is_max)
595 tree one = build_int_cst (TREE_TYPE (min), 1);
596 max = int_const_binop (MINUS_EXPR, min, one);
597 min = vrp_val_min (TREE_TYPE (min));
598 t = VR_RANGE;
602 /* Drop [-INF(OVF), +INF(OVF)] to varying. */
603 if (needs_overflow_infinity (TREE_TYPE (min))
604 && is_overflow_infinity (min)
605 && is_overflow_infinity (max))
607 set_value_range_to_varying (vr);
608 return;
611 set_value_range (vr, t, min, max, equiv);
614 /* Copy value range FROM into value range TO. */
616 static inline void
617 copy_value_range (value_range_t *to, value_range_t *from)
619 set_value_range (to, from->type, from->min, from->max, from->equiv);
622 /* Set value range VR to a single value. This function is only called
623 with values we get from statements, and exists to clear the
624 TREE_OVERFLOW flag so that we don't think we have an overflow
625 infinity when we shouldn't. */
627 static inline void
628 set_value_range_to_value (value_range_t *vr, tree val, bitmap equiv)
630 gcc_assert (is_gimple_min_invariant (val));
631 if (TREE_OVERFLOW_P (val))
632 val = drop_tree_overflow (val);
633 set_value_range (vr, VR_RANGE, val, val, equiv);
636 /* Set value range VR to a non-negative range of type TYPE.
637 OVERFLOW_INFINITY indicates whether to use an overflow infinity
638 rather than TYPE_MAX_VALUE; this should be true if we determine
639 that the range is nonnegative based on the assumption that signed
640 overflow does not occur. */
642 static inline void
643 set_value_range_to_nonnegative (value_range_t *vr, tree type,
644 bool overflow_infinity)
646 tree zero;
648 if (overflow_infinity && !supports_overflow_infinity (type))
650 set_value_range_to_varying (vr);
651 return;
654 zero = build_int_cst (type, 0);
655 set_value_range (vr, VR_RANGE, zero,
656 (overflow_infinity
657 ? positive_overflow_infinity (type)
658 : TYPE_MAX_VALUE (type)),
659 vr->equiv);
662 /* Set value range VR to a non-NULL range of type TYPE. */
664 static inline void
665 set_value_range_to_nonnull (value_range_t *vr, tree type)
667 tree zero = build_int_cst (type, 0);
668 set_value_range (vr, VR_ANTI_RANGE, zero, zero, vr->equiv);
672 /* Set value range VR to a NULL range of type TYPE. */
674 static inline void
675 set_value_range_to_null (value_range_t *vr, tree type)
677 set_value_range_to_value (vr, build_int_cst (type, 0), vr->equiv);
681 /* Set value range VR to a range of a truthvalue of type TYPE. */
683 static inline void
684 set_value_range_to_truthvalue (value_range_t *vr, tree type)
686 if (TYPE_PRECISION (type) == 1)
687 set_value_range_to_varying (vr);
688 else
689 set_value_range (vr, VR_RANGE,
690 build_int_cst (type, 0), build_int_cst (type, 1),
691 vr->equiv);
695 /* If abs (min) < abs (max), set VR to [-max, max], if
696 abs (min) >= abs (max), set VR to [-min, min]. */
698 static void
699 abs_extent_range (value_range_t *vr, tree min, tree max)
701 int cmp;
703 gcc_assert (TREE_CODE (min) == INTEGER_CST);
704 gcc_assert (TREE_CODE (max) == INTEGER_CST);
705 gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (min)));
706 gcc_assert (!TYPE_UNSIGNED (TREE_TYPE (min)));
707 min = fold_unary (ABS_EXPR, TREE_TYPE (min), min);
708 max = fold_unary (ABS_EXPR, TREE_TYPE (max), max);
709 if (TREE_OVERFLOW (min) || TREE_OVERFLOW (max))
711 set_value_range_to_varying (vr);
712 return;
714 cmp = compare_values (min, max);
715 if (cmp == -1)
716 min = fold_unary (NEGATE_EXPR, TREE_TYPE (min), max);
717 else if (cmp == 0 || cmp == 1)
719 max = min;
720 min = fold_unary (NEGATE_EXPR, TREE_TYPE (min), min);
722 else
724 set_value_range_to_varying (vr);
725 return;
727 set_and_canonicalize_value_range (vr, VR_RANGE, min, max, NULL);
731 /* Return value range information for VAR.
733 If we have no values ranges recorded (ie, VRP is not running), then
734 return NULL. Otherwise create an empty range if none existed for VAR. */
736 static value_range_t *
737 get_value_range (const_tree var)
739 static const struct value_range_d vr_const_varying
740 = { VR_VARYING, NULL_TREE, NULL_TREE, NULL };
741 value_range_t *vr;
742 tree sym;
743 unsigned ver = SSA_NAME_VERSION (var);
745 /* If we have no recorded ranges, then return NULL. */
746 if (! vr_value)
747 return NULL;
749 /* If we query the range for a new SSA name return an unmodifiable VARYING.
750 We should get here at most from the substitute-and-fold stage which
751 will never try to change values. */
752 if (ver >= num_vr_values)
753 return CONST_CAST (value_range_t *, &vr_const_varying);
755 vr = vr_value[ver];
756 if (vr)
757 return vr;
759 /* After propagation finished do not allocate new value-ranges. */
760 if (values_propagated)
761 return CONST_CAST (value_range_t *, &vr_const_varying);
763 /* Create a default value range. */
764 vr_value[ver] = vr = XCNEW (value_range_t);
766 /* Defer allocating the equivalence set. */
767 vr->equiv = NULL;
769 /* If VAR is a default definition of a parameter, the variable can
770 take any value in VAR's type. */
771 if (SSA_NAME_IS_DEFAULT_DEF (var))
773 sym = SSA_NAME_VAR (var);
774 if (TREE_CODE (sym) == PARM_DECL)
776 /* Try to use the "nonnull" attribute to create ~[0, 0]
777 anti-ranges for pointers. Note that this is only valid with
778 default definitions of PARM_DECLs. */
779 if (POINTER_TYPE_P (TREE_TYPE (sym))
780 && nonnull_arg_p (sym))
781 set_value_range_to_nonnull (vr, TREE_TYPE (sym));
782 else
783 set_value_range_to_varying (vr);
785 else if (TREE_CODE (sym) == RESULT_DECL
786 && DECL_BY_REFERENCE (sym))
787 set_value_range_to_nonnull (vr, TREE_TYPE (sym));
790 return vr;
793 /* Return true, if VAL1 and VAL2 are equal values for VRP purposes. */
795 static inline bool
796 vrp_operand_equal_p (const_tree val1, const_tree val2)
798 if (val1 == val2)
799 return true;
800 if (!val1 || !val2 || !operand_equal_p (val1, val2, 0))
801 return false;
802 return is_overflow_infinity (val1) == is_overflow_infinity (val2);
805 /* Return true, if the bitmaps B1 and B2 are equal. */
807 static inline bool
808 vrp_bitmap_equal_p (const_bitmap b1, const_bitmap b2)
810 return (b1 == b2
811 || ((!b1 || bitmap_empty_p (b1))
812 && (!b2 || bitmap_empty_p (b2)))
813 || (b1 && b2
814 && bitmap_equal_p (b1, b2)));
817 /* Update the value range and equivalence set for variable VAR to
818 NEW_VR. Return true if NEW_VR is different from VAR's previous
819 value.
821 NOTE: This function assumes that NEW_VR is a temporary value range
822 object created for the sole purpose of updating VAR's range. The
823 storage used by the equivalence set from NEW_VR will be freed by
824 this function. Do not call update_value_range when NEW_VR
825 is the range object associated with another SSA name. */
827 static inline bool
828 update_value_range (const_tree var, value_range_t *new_vr)
830 value_range_t *old_vr;
831 bool is_new;
833 /* Update the value range, if necessary. */
834 old_vr = get_value_range (var);
835 is_new = old_vr->type != new_vr->type
836 || !vrp_operand_equal_p (old_vr->min, new_vr->min)
837 || !vrp_operand_equal_p (old_vr->max, new_vr->max)
838 || !vrp_bitmap_equal_p (old_vr->equiv, new_vr->equiv);
840 if (is_new)
842 /* Do not allow transitions up the lattice. The following
843 is slightly more awkward than just new_vr->type < old_vr->type
844 because VR_RANGE and VR_ANTI_RANGE need to be considered
845 the same. We may not have is_new when transitioning to
846 UNDEFINED or from VARYING. */
847 if (new_vr->type == VR_UNDEFINED
848 || old_vr->type == VR_VARYING)
849 set_value_range_to_varying (old_vr);
850 else
851 set_value_range (old_vr, new_vr->type, new_vr->min, new_vr->max,
852 new_vr->equiv);
855 BITMAP_FREE (new_vr->equiv);
857 return is_new;
861 /* Add VAR and VAR's equivalence set to EQUIV. This is the central
862 point where equivalence processing can be turned on/off. */
864 static void
865 add_equivalence (bitmap *equiv, const_tree var)
867 unsigned ver = SSA_NAME_VERSION (var);
868 value_range_t *vr = vr_value[ver];
870 if (*equiv == NULL)
871 *equiv = BITMAP_ALLOC (NULL);
872 bitmap_set_bit (*equiv, ver);
873 if (vr && vr->equiv)
874 bitmap_ior_into (*equiv, vr->equiv);
878 /* Return true if VR is ~[0, 0]. */
880 static inline bool
881 range_is_nonnull (value_range_t *vr)
883 return vr->type == VR_ANTI_RANGE
884 && integer_zerop (vr->min)
885 && integer_zerop (vr->max);
889 /* Return true if VR is [0, 0]. */
891 static inline bool
892 range_is_null (value_range_t *vr)
894 return vr->type == VR_RANGE
895 && integer_zerop (vr->min)
896 && integer_zerop (vr->max);
899 /* Return true if max and min of VR are INTEGER_CST. It's not necessary
900 a singleton. */
902 static inline bool
903 range_int_cst_p (value_range_t *vr)
905 return (vr->type == VR_RANGE
906 && TREE_CODE (vr->max) == INTEGER_CST
907 && TREE_CODE (vr->min) == INTEGER_CST);
910 /* Return true if VR is a INTEGER_CST singleton. */
912 static inline bool
913 range_int_cst_singleton_p (value_range_t *vr)
915 return (range_int_cst_p (vr)
916 && !is_overflow_infinity (vr->min)
917 && !is_overflow_infinity (vr->max)
918 && tree_int_cst_equal (vr->min, vr->max));
921 /* Return true if value range VR involves at least one symbol. */
923 static inline bool
924 symbolic_range_p (value_range_t *vr)
926 return (!is_gimple_min_invariant (vr->min)
927 || !is_gimple_min_invariant (vr->max));
930 /* Return the single symbol (an SSA_NAME) contained in T if any, or NULL_TREE
931 otherwise. We only handle additive operations and set NEG to true if the
932 symbol is negated and INV to the invariant part, if any. */
934 static tree
935 get_single_symbol (tree t, bool *neg, tree *inv)
937 bool neg_;
938 tree inv_;
940 if (TREE_CODE (t) == PLUS_EXPR
941 || TREE_CODE (t) == POINTER_PLUS_EXPR
942 || TREE_CODE (t) == MINUS_EXPR)
944 if (is_gimple_min_invariant (TREE_OPERAND (t, 0)))
946 neg_ = (TREE_CODE (t) == MINUS_EXPR);
947 inv_ = TREE_OPERAND (t, 0);
948 t = TREE_OPERAND (t, 1);
950 else if (is_gimple_min_invariant (TREE_OPERAND (t, 1)))
952 neg_ = false;
953 inv_ = TREE_OPERAND (t, 1);
954 t = TREE_OPERAND (t, 0);
956 else
957 return NULL_TREE;
959 else
961 neg_ = false;
962 inv_ = NULL_TREE;
965 if (TREE_CODE (t) == NEGATE_EXPR)
967 t = TREE_OPERAND (t, 0);
968 neg_ = !neg_;
971 if (TREE_CODE (t) != SSA_NAME)
972 return NULL_TREE;
974 *neg = neg_;
975 *inv = inv_;
976 return t;
979 /* The reverse operation: build a symbolic expression with TYPE
980 from symbol SYM, negated according to NEG, and invariant INV. */
982 static tree
983 build_symbolic_expr (tree type, tree sym, bool neg, tree inv)
985 const bool pointer_p = POINTER_TYPE_P (type);
986 tree t = sym;
988 if (neg)
989 t = build1 (NEGATE_EXPR, type, t);
991 if (integer_zerop (inv))
992 return t;
994 return build2 (pointer_p ? POINTER_PLUS_EXPR : PLUS_EXPR, type, t, inv);
997 /* Return true if value range VR involves exactly one symbol SYM. */
999 static bool
1000 symbolic_range_based_on_p (value_range_t *vr, const_tree sym)
1002 bool neg, min_has_symbol, max_has_symbol;
1003 tree inv;
1005 if (is_gimple_min_invariant (vr->min))
1006 min_has_symbol = false;
1007 else if (get_single_symbol (vr->min, &neg, &inv) == sym)
1008 min_has_symbol = true;
1009 else
1010 return false;
1012 if (is_gimple_min_invariant (vr->max))
1013 max_has_symbol = false;
1014 else if (get_single_symbol (vr->max, &neg, &inv) == sym)
1015 max_has_symbol = true;
1016 else
1017 return false;
1019 return (min_has_symbol || max_has_symbol);
1022 /* Return true if value range VR uses an overflow infinity. */
1024 static inline bool
1025 overflow_infinity_range_p (value_range_t *vr)
1027 return (vr->type == VR_RANGE
1028 && (is_overflow_infinity (vr->min)
1029 || is_overflow_infinity (vr->max)));
1032 /* Return false if we can not make a valid comparison based on VR;
1033 this will be the case if it uses an overflow infinity and overflow
1034 is not undefined (i.e., -fno-strict-overflow is in effect).
1035 Otherwise return true, and set *STRICT_OVERFLOW_P to true if VR
1036 uses an overflow infinity. */
1038 static bool
1039 usable_range_p (value_range_t *vr, bool *strict_overflow_p)
1041 gcc_assert (vr->type == VR_RANGE);
1042 if (is_overflow_infinity (vr->min))
1044 *strict_overflow_p = true;
1045 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (vr->min)))
1046 return false;
1048 if (is_overflow_infinity (vr->max))
1050 *strict_overflow_p = true;
1051 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (vr->max)))
1052 return false;
1054 return true;
1058 /* Return true if the result of assignment STMT is know to be non-negative.
1059 If the return value is based on the assumption that signed overflow is
1060 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1061 *STRICT_OVERFLOW_P.*/
1063 static bool
1064 gimple_assign_nonnegative_warnv_p (gimple stmt, bool *strict_overflow_p)
1066 enum tree_code code = gimple_assign_rhs_code (stmt);
1067 switch (get_gimple_rhs_class (code))
1069 case GIMPLE_UNARY_RHS:
1070 return tree_unary_nonnegative_warnv_p (gimple_assign_rhs_code (stmt),
1071 gimple_expr_type (stmt),
1072 gimple_assign_rhs1 (stmt),
1073 strict_overflow_p);
1074 case GIMPLE_BINARY_RHS:
1075 return tree_binary_nonnegative_warnv_p (gimple_assign_rhs_code (stmt),
1076 gimple_expr_type (stmt),
1077 gimple_assign_rhs1 (stmt),
1078 gimple_assign_rhs2 (stmt),
1079 strict_overflow_p);
1080 case GIMPLE_TERNARY_RHS:
1081 return false;
1082 case GIMPLE_SINGLE_RHS:
1083 return tree_single_nonnegative_warnv_p (gimple_assign_rhs1 (stmt),
1084 strict_overflow_p);
1085 case GIMPLE_INVALID_RHS:
1086 gcc_unreachable ();
1087 default:
1088 gcc_unreachable ();
1092 /* Return true if return value of call STMT is know to be non-negative.
1093 If the return value is based on the assumption that signed overflow is
1094 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1095 *STRICT_OVERFLOW_P.*/
1097 static bool
1098 gimple_call_nonnegative_warnv_p (gimple stmt, bool *strict_overflow_p)
1100 tree arg0 = gimple_call_num_args (stmt) > 0 ?
1101 gimple_call_arg (stmt, 0) : NULL_TREE;
1102 tree arg1 = gimple_call_num_args (stmt) > 1 ?
1103 gimple_call_arg (stmt, 1) : NULL_TREE;
1105 return tree_call_nonnegative_warnv_p (gimple_expr_type (stmt),
1106 gimple_call_fndecl (stmt),
1107 arg0,
1108 arg1,
1109 strict_overflow_p);
1112 /* Return true if STMT is know to to compute a non-negative value.
1113 If the return value is based on the assumption that signed overflow is
1114 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1115 *STRICT_OVERFLOW_P.*/
1117 static bool
1118 gimple_stmt_nonnegative_warnv_p (gimple stmt, bool *strict_overflow_p)
1120 switch (gimple_code (stmt))
1122 case GIMPLE_ASSIGN:
1123 return gimple_assign_nonnegative_warnv_p (stmt, strict_overflow_p);
1124 case GIMPLE_CALL:
1125 return gimple_call_nonnegative_warnv_p (stmt, strict_overflow_p);
1126 default:
1127 gcc_unreachable ();
1131 /* Return true if the result of assignment STMT is know to be non-zero.
1132 If the return value is based on the assumption that signed overflow is
1133 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1134 *STRICT_OVERFLOW_P.*/
1136 static bool
1137 gimple_assign_nonzero_warnv_p (gimple stmt, bool *strict_overflow_p)
1139 enum tree_code code = gimple_assign_rhs_code (stmt);
1140 switch (get_gimple_rhs_class (code))
1142 case GIMPLE_UNARY_RHS:
1143 return tree_unary_nonzero_warnv_p (gimple_assign_rhs_code (stmt),
1144 gimple_expr_type (stmt),
1145 gimple_assign_rhs1 (stmt),
1146 strict_overflow_p);
1147 case GIMPLE_BINARY_RHS:
1148 return tree_binary_nonzero_warnv_p (gimple_assign_rhs_code (stmt),
1149 gimple_expr_type (stmt),
1150 gimple_assign_rhs1 (stmt),
1151 gimple_assign_rhs2 (stmt),
1152 strict_overflow_p);
1153 case GIMPLE_TERNARY_RHS:
1154 return false;
1155 case GIMPLE_SINGLE_RHS:
1156 return tree_single_nonzero_warnv_p (gimple_assign_rhs1 (stmt),
1157 strict_overflow_p);
1158 case GIMPLE_INVALID_RHS:
1159 gcc_unreachable ();
1160 default:
1161 gcc_unreachable ();
1165 /* Return true if STMT is known to compute a non-zero value.
1166 If the return value is based on the assumption that signed overflow is
1167 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1168 *STRICT_OVERFLOW_P.*/
1170 static bool
1171 gimple_stmt_nonzero_warnv_p (gimple stmt, bool *strict_overflow_p)
1173 switch (gimple_code (stmt))
1175 case GIMPLE_ASSIGN:
1176 return gimple_assign_nonzero_warnv_p (stmt, strict_overflow_p);
1177 case GIMPLE_CALL:
1179 tree fndecl = gimple_call_fndecl (stmt);
1180 if (!fndecl) return false;
1181 if (flag_delete_null_pointer_checks && !flag_check_new
1182 && DECL_IS_OPERATOR_NEW (fndecl)
1183 && !TREE_NOTHROW (fndecl))
1184 return true;
1185 if (flag_delete_null_pointer_checks &&
1186 lookup_attribute ("returns_nonnull",
1187 TYPE_ATTRIBUTES (gimple_call_fntype (stmt))))
1188 return true;
1189 return gimple_alloca_call_p (stmt);
1191 default:
1192 gcc_unreachable ();
1196 /* Like tree_expr_nonzero_warnv_p, but this function uses value ranges
1197 obtained so far. */
1199 static bool
1200 vrp_stmt_computes_nonzero (gimple stmt, bool *strict_overflow_p)
1202 if (gimple_stmt_nonzero_warnv_p (stmt, strict_overflow_p))
1203 return true;
1205 /* If we have an expression of the form &X->a, then the expression
1206 is nonnull if X is nonnull. */
1207 if (is_gimple_assign (stmt)
1208 && gimple_assign_rhs_code (stmt) == ADDR_EXPR)
1210 tree expr = gimple_assign_rhs1 (stmt);
1211 tree base = get_base_address (TREE_OPERAND (expr, 0));
1213 if (base != NULL_TREE
1214 && TREE_CODE (base) == MEM_REF
1215 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
1217 value_range_t *vr = get_value_range (TREE_OPERAND (base, 0));
1218 if (range_is_nonnull (vr))
1219 return true;
1223 return false;
1226 /* Returns true if EXPR is a valid value (as expected by compare_values) --
1227 a gimple invariant, or SSA_NAME +- CST. */
1229 static bool
1230 valid_value_p (tree expr)
1232 if (TREE_CODE (expr) == SSA_NAME)
1233 return true;
1235 if (TREE_CODE (expr) == PLUS_EXPR
1236 || TREE_CODE (expr) == MINUS_EXPR)
1237 return (TREE_CODE (TREE_OPERAND (expr, 0)) == SSA_NAME
1238 && TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST);
1240 return is_gimple_min_invariant (expr);
1243 /* Return
1244 1 if VAL < VAL2
1245 0 if !(VAL < VAL2)
1246 -2 if those are incomparable. */
1247 static inline int
1248 operand_less_p (tree val, tree val2)
1250 /* LT is folded faster than GE and others. Inline the common case. */
1251 if (TREE_CODE (val) == INTEGER_CST && TREE_CODE (val2) == INTEGER_CST)
1252 return tree_int_cst_lt (val, val2);
1253 else
1255 tree tcmp;
1257 fold_defer_overflow_warnings ();
1259 tcmp = fold_binary_to_constant (LT_EXPR, boolean_type_node, val, val2);
1261 fold_undefer_and_ignore_overflow_warnings ();
1263 if (!tcmp
1264 || TREE_CODE (tcmp) != INTEGER_CST)
1265 return -2;
1267 if (!integer_zerop (tcmp))
1268 return 1;
1271 /* val >= val2, not considering overflow infinity. */
1272 if (is_negative_overflow_infinity (val))
1273 return is_negative_overflow_infinity (val2) ? 0 : 1;
1274 else if (is_positive_overflow_infinity (val2))
1275 return is_positive_overflow_infinity (val) ? 0 : 1;
1277 return 0;
1280 /* Compare two values VAL1 and VAL2. Return
1282 -2 if VAL1 and VAL2 cannot be compared at compile-time,
1283 -1 if VAL1 < VAL2,
1284 0 if VAL1 == VAL2,
1285 +1 if VAL1 > VAL2, and
1286 +2 if VAL1 != VAL2
1288 This is similar to tree_int_cst_compare but supports pointer values
1289 and values that cannot be compared at compile time.
1291 If STRICT_OVERFLOW_P is not NULL, then set *STRICT_OVERFLOW_P to
1292 true if the return value is only valid if we assume that signed
1293 overflow is undefined. */
1295 static int
1296 compare_values_warnv (tree val1, tree val2, bool *strict_overflow_p)
1298 if (val1 == val2)
1299 return 0;
1301 /* Below we rely on the fact that VAL1 and VAL2 are both pointers or
1302 both integers. */
1303 gcc_assert (POINTER_TYPE_P (TREE_TYPE (val1))
1304 == POINTER_TYPE_P (TREE_TYPE (val2)));
1306 /* Convert the two values into the same type. This is needed because
1307 sizetype causes sign extension even for unsigned types. */
1308 val2 = fold_convert (TREE_TYPE (val1), val2);
1309 STRIP_USELESS_TYPE_CONVERSION (val2);
1311 if ((TREE_CODE (val1) == SSA_NAME
1312 || (TREE_CODE (val1) == NEGATE_EXPR
1313 && TREE_CODE (TREE_OPERAND (val1, 0)) == SSA_NAME)
1314 || TREE_CODE (val1) == PLUS_EXPR
1315 || TREE_CODE (val1) == MINUS_EXPR)
1316 && (TREE_CODE (val2) == SSA_NAME
1317 || (TREE_CODE (val2) == NEGATE_EXPR
1318 && TREE_CODE (TREE_OPERAND (val2, 0)) == SSA_NAME)
1319 || TREE_CODE (val2) == PLUS_EXPR
1320 || TREE_CODE (val2) == MINUS_EXPR))
1322 tree n1, c1, n2, c2;
1323 enum tree_code code1, code2;
1325 /* If VAL1 and VAL2 are of the form '[-]NAME [+-] CST' or 'NAME',
1326 return -1 or +1 accordingly. If VAL1 and VAL2 don't use the
1327 same name, return -2. */
1328 if (TREE_CODE (val1) == SSA_NAME || TREE_CODE (val1) == NEGATE_EXPR)
1330 code1 = SSA_NAME;
1331 n1 = val1;
1332 c1 = NULL_TREE;
1334 else
1336 code1 = TREE_CODE (val1);
1337 n1 = TREE_OPERAND (val1, 0);
1338 c1 = TREE_OPERAND (val1, 1);
1339 if (tree_int_cst_sgn (c1) == -1)
1341 if (is_negative_overflow_infinity (c1))
1342 return -2;
1343 c1 = fold_unary_to_constant (NEGATE_EXPR, TREE_TYPE (c1), c1);
1344 if (!c1)
1345 return -2;
1346 code1 = code1 == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR;
1350 if (TREE_CODE (val2) == SSA_NAME || TREE_CODE (val2) == NEGATE_EXPR)
1352 code2 = SSA_NAME;
1353 n2 = val2;
1354 c2 = NULL_TREE;
1356 else
1358 code2 = TREE_CODE (val2);
1359 n2 = TREE_OPERAND (val2, 0);
1360 c2 = TREE_OPERAND (val2, 1);
1361 if (tree_int_cst_sgn (c2) == -1)
1363 if (is_negative_overflow_infinity (c2))
1364 return -2;
1365 c2 = fold_unary_to_constant (NEGATE_EXPR, TREE_TYPE (c2), c2);
1366 if (!c2)
1367 return -2;
1368 code2 = code2 == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR;
1372 /* Both values must use the same name. */
1373 if (TREE_CODE (n1) == NEGATE_EXPR && TREE_CODE (n2) == NEGATE_EXPR)
1375 n1 = TREE_OPERAND (n1, 0);
1376 n2 = TREE_OPERAND (n2, 0);
1378 if (n1 != n2)
1379 return -2;
1381 if (code1 == SSA_NAME && code2 == SSA_NAME)
1382 /* NAME == NAME */
1383 return 0;
1385 /* If overflow is defined we cannot simplify more. */
1386 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (val1)))
1387 return -2;
1389 if (strict_overflow_p != NULL
1390 && (code1 == SSA_NAME || !TREE_NO_WARNING (val1))
1391 && (code2 == SSA_NAME || !TREE_NO_WARNING (val2)))
1392 *strict_overflow_p = true;
1394 if (code1 == SSA_NAME)
1396 if (code2 == PLUS_EXPR)
1397 /* NAME < NAME + CST */
1398 return -1;
1399 else if (code2 == MINUS_EXPR)
1400 /* NAME > NAME - CST */
1401 return 1;
1403 else if (code1 == PLUS_EXPR)
1405 if (code2 == SSA_NAME)
1406 /* NAME + CST > NAME */
1407 return 1;
1408 else if (code2 == PLUS_EXPR)
1409 /* NAME + CST1 > NAME + CST2, if CST1 > CST2 */
1410 return compare_values_warnv (c1, c2, strict_overflow_p);
1411 else if (code2 == MINUS_EXPR)
1412 /* NAME + CST1 > NAME - CST2 */
1413 return 1;
1415 else if (code1 == MINUS_EXPR)
1417 if (code2 == SSA_NAME)
1418 /* NAME - CST < NAME */
1419 return -1;
1420 else if (code2 == PLUS_EXPR)
1421 /* NAME - CST1 < NAME + CST2 */
1422 return -1;
1423 else if (code2 == MINUS_EXPR)
1424 /* NAME - CST1 > NAME - CST2, if CST1 < CST2. Notice that
1425 C1 and C2 are swapped in the call to compare_values. */
1426 return compare_values_warnv (c2, c1, strict_overflow_p);
1429 gcc_unreachable ();
1432 /* We cannot compare non-constants. */
1433 if (!is_gimple_min_invariant (val1) || !is_gimple_min_invariant (val2))
1434 return -2;
1436 if (!POINTER_TYPE_P (TREE_TYPE (val1)))
1438 /* We cannot compare overflowed values, except for overflow
1439 infinities. */
1440 if (TREE_OVERFLOW (val1) || TREE_OVERFLOW (val2))
1442 if (strict_overflow_p != NULL)
1443 *strict_overflow_p = true;
1444 if (is_negative_overflow_infinity (val1))
1445 return is_negative_overflow_infinity (val2) ? 0 : -1;
1446 else if (is_negative_overflow_infinity (val2))
1447 return 1;
1448 else if (is_positive_overflow_infinity (val1))
1449 return is_positive_overflow_infinity (val2) ? 0 : 1;
1450 else if (is_positive_overflow_infinity (val2))
1451 return -1;
1452 return -2;
1455 return tree_int_cst_compare (val1, val2);
1457 else
1459 tree t;
1461 /* First see if VAL1 and VAL2 are not the same. */
1462 if (val1 == val2 || operand_equal_p (val1, val2, 0))
1463 return 0;
1465 /* If VAL1 is a lower address than VAL2, return -1. */
1466 if (operand_less_p (val1, val2) == 1)
1467 return -1;
1469 /* If VAL1 is a higher address than VAL2, return +1. */
1470 if (operand_less_p (val2, val1) == 1)
1471 return 1;
1473 /* If VAL1 is different than VAL2, return +2.
1474 For integer constants we either have already returned -1 or 1
1475 or they are equivalent. We still might succeed in proving
1476 something about non-trivial operands. */
1477 if (TREE_CODE (val1) != INTEGER_CST
1478 || TREE_CODE (val2) != INTEGER_CST)
1480 t = fold_binary_to_constant (NE_EXPR, boolean_type_node, val1, val2);
1481 if (t && integer_onep (t))
1482 return 2;
1485 return -2;
1489 /* Compare values like compare_values_warnv, but treat comparisons of
1490 nonconstants which rely on undefined overflow as incomparable. */
1492 static int
1493 compare_values (tree val1, tree val2)
1495 bool sop;
1496 int ret;
1498 sop = false;
1499 ret = compare_values_warnv (val1, val2, &sop);
1500 if (sop
1501 && (!is_gimple_min_invariant (val1) || !is_gimple_min_invariant (val2)))
1502 ret = -2;
1503 return ret;
1507 /* Return 1 if VAL is inside value range MIN <= VAL <= MAX,
1508 0 if VAL is not inside [MIN, MAX],
1509 -2 if we cannot tell either way.
1511 Benchmark compile/20001226-1.c compilation time after changing this
1512 function. */
1514 static inline int
1515 value_inside_range (tree val, tree min, tree max)
1517 int cmp1, cmp2;
1519 cmp1 = operand_less_p (val, min);
1520 if (cmp1 == -2)
1521 return -2;
1522 if (cmp1 == 1)
1523 return 0;
1525 cmp2 = operand_less_p (max, val);
1526 if (cmp2 == -2)
1527 return -2;
1529 return !cmp2;
1533 /* Return true if value ranges VR0 and VR1 have a non-empty
1534 intersection.
1536 Benchmark compile/20001226-1.c compilation time after changing this
1537 function.
1540 static inline bool
1541 value_ranges_intersect_p (value_range_t *vr0, value_range_t *vr1)
1543 /* The value ranges do not intersect if the maximum of the first range is
1544 less than the minimum of the second range or vice versa.
1545 When those relations are unknown, we can't do any better. */
1546 if (operand_less_p (vr0->max, vr1->min) != 0)
1547 return false;
1548 if (operand_less_p (vr1->max, vr0->min) != 0)
1549 return false;
1550 return true;
1554 /* Return 1 if [MIN, MAX] includes the value zero, 0 if it does not
1555 include the value zero, -2 if we cannot tell. */
1557 static inline int
1558 range_includes_zero_p (tree min, tree max)
1560 tree zero = build_int_cst (TREE_TYPE (min), 0);
1561 return value_inside_range (zero, min, max);
1564 /* Return true if *VR is know to only contain nonnegative values. */
1566 static inline bool
1567 value_range_nonnegative_p (value_range_t *vr)
1569 /* Testing for VR_ANTI_RANGE is not useful here as any anti-range
1570 which would return a useful value should be encoded as a
1571 VR_RANGE. */
1572 if (vr->type == VR_RANGE)
1574 int result = compare_values (vr->min, integer_zero_node);
1575 return (result == 0 || result == 1);
1578 return false;
1581 /* If *VR has a value rante that is a single constant value return that,
1582 otherwise return NULL_TREE. */
1584 static tree
1585 value_range_constant_singleton (value_range_t *vr)
1587 if (vr->type == VR_RANGE
1588 && operand_equal_p (vr->min, vr->max, 0)
1589 && is_gimple_min_invariant (vr->min))
1590 return vr->min;
1592 return NULL_TREE;
1595 /* If OP has a value range with a single constant value return that,
1596 otherwise return NULL_TREE. This returns OP itself if OP is a
1597 constant. */
1599 static tree
1600 op_with_constant_singleton_value_range (tree op)
1602 if (is_gimple_min_invariant (op))
1603 return op;
1605 if (TREE_CODE (op) != SSA_NAME)
1606 return NULL_TREE;
1608 return value_range_constant_singleton (get_value_range (op));
1611 /* Return true if op is in a boolean [0, 1] value-range. */
1613 static bool
1614 op_with_boolean_value_range_p (tree op)
1616 value_range_t *vr;
1618 if (TYPE_PRECISION (TREE_TYPE (op)) == 1)
1619 return true;
1621 if (integer_zerop (op)
1622 || integer_onep (op))
1623 return true;
1625 if (TREE_CODE (op) != SSA_NAME)
1626 return false;
1628 vr = get_value_range (op);
1629 return (vr->type == VR_RANGE
1630 && integer_zerop (vr->min)
1631 && integer_onep (vr->max));
1634 /* Extract value range information from an ASSERT_EXPR EXPR and store
1635 it in *VR_P. */
1637 static void
1638 extract_range_from_assert (value_range_t *vr_p, tree expr)
1640 tree var, cond, limit, min, max, type;
1641 value_range_t *limit_vr;
1642 enum tree_code cond_code;
1644 var = ASSERT_EXPR_VAR (expr);
1645 cond = ASSERT_EXPR_COND (expr);
1647 gcc_assert (COMPARISON_CLASS_P (cond));
1649 /* Find VAR in the ASSERT_EXPR conditional. */
1650 if (var == TREE_OPERAND (cond, 0)
1651 || TREE_CODE (TREE_OPERAND (cond, 0)) == PLUS_EXPR
1652 || TREE_CODE (TREE_OPERAND (cond, 0)) == NOP_EXPR)
1654 /* If the predicate is of the form VAR COMP LIMIT, then we just
1655 take LIMIT from the RHS and use the same comparison code. */
1656 cond_code = TREE_CODE (cond);
1657 limit = TREE_OPERAND (cond, 1);
1658 cond = TREE_OPERAND (cond, 0);
1660 else
1662 /* If the predicate is of the form LIMIT COMP VAR, then we need
1663 to flip around the comparison code to create the proper range
1664 for VAR. */
1665 cond_code = swap_tree_comparison (TREE_CODE (cond));
1666 limit = TREE_OPERAND (cond, 0);
1667 cond = TREE_OPERAND (cond, 1);
1670 limit = avoid_overflow_infinity (limit);
1672 type = TREE_TYPE (var);
1673 gcc_assert (limit != var);
1675 /* For pointer arithmetic, we only keep track of pointer equality
1676 and inequality. */
1677 if (POINTER_TYPE_P (type) && cond_code != NE_EXPR && cond_code != EQ_EXPR)
1679 set_value_range_to_varying (vr_p);
1680 return;
1683 /* If LIMIT is another SSA name and LIMIT has a range of its own,
1684 try to use LIMIT's range to avoid creating symbolic ranges
1685 unnecessarily. */
1686 limit_vr = (TREE_CODE (limit) == SSA_NAME) ? get_value_range (limit) : NULL;
1688 /* LIMIT's range is only interesting if it has any useful information. */
1689 if (limit_vr
1690 && (limit_vr->type == VR_UNDEFINED
1691 || limit_vr->type == VR_VARYING
1692 || symbolic_range_p (limit_vr)))
1693 limit_vr = NULL;
1695 /* Initially, the new range has the same set of equivalences of
1696 VAR's range. This will be revised before returning the final
1697 value. Since assertions may be chained via mutually exclusive
1698 predicates, we will need to trim the set of equivalences before
1699 we are done. */
1700 gcc_assert (vr_p->equiv == NULL);
1701 add_equivalence (&vr_p->equiv, var);
1703 /* Extract a new range based on the asserted comparison for VAR and
1704 LIMIT's value range. Notice that if LIMIT has an anti-range, we
1705 will only use it for equality comparisons (EQ_EXPR). For any
1706 other kind of assertion, we cannot derive a range from LIMIT's
1707 anti-range that can be used to describe the new range. For
1708 instance, ASSERT_EXPR <x_2, x_2 <= b_4>. If b_4 is ~[2, 10],
1709 then b_4 takes on the ranges [-INF, 1] and [11, +INF]. There is
1710 no single range for x_2 that could describe LE_EXPR, so we might
1711 as well build the range [b_4, +INF] for it.
1712 One special case we handle is extracting a range from a
1713 range test encoded as (unsigned)var + CST <= limit. */
1714 if (TREE_CODE (cond) == NOP_EXPR
1715 || TREE_CODE (cond) == PLUS_EXPR)
1717 if (TREE_CODE (cond) == PLUS_EXPR)
1719 min = fold_build1 (NEGATE_EXPR, TREE_TYPE (TREE_OPERAND (cond, 1)),
1720 TREE_OPERAND (cond, 1));
1721 max = int_const_binop (PLUS_EXPR, limit, min);
1722 cond = TREE_OPERAND (cond, 0);
1724 else
1726 min = build_int_cst (TREE_TYPE (var), 0);
1727 max = limit;
1730 /* Make sure to not set TREE_OVERFLOW on the final type
1731 conversion. We are willingly interpreting large positive
1732 unsigned values as negative signed values here. */
1733 min = force_fit_type (TREE_TYPE (var), wi::to_widest (min), 0, false);
1734 max = force_fit_type (TREE_TYPE (var), wi::to_widest (max), 0, false);
1736 /* We can transform a max, min range to an anti-range or
1737 vice-versa. Use set_and_canonicalize_value_range which does
1738 this for us. */
1739 if (cond_code == LE_EXPR)
1740 set_and_canonicalize_value_range (vr_p, VR_RANGE,
1741 min, max, vr_p->equiv);
1742 else if (cond_code == GT_EXPR)
1743 set_and_canonicalize_value_range (vr_p, VR_ANTI_RANGE,
1744 min, max, vr_p->equiv);
1745 else
1746 gcc_unreachable ();
1748 else if (cond_code == EQ_EXPR)
1750 enum value_range_type range_type;
1752 if (limit_vr)
1754 range_type = limit_vr->type;
1755 min = limit_vr->min;
1756 max = limit_vr->max;
1758 else
1760 range_type = VR_RANGE;
1761 min = limit;
1762 max = limit;
1765 set_value_range (vr_p, range_type, min, max, vr_p->equiv);
1767 /* When asserting the equality VAR == LIMIT and LIMIT is another
1768 SSA name, the new range will also inherit the equivalence set
1769 from LIMIT. */
1770 if (TREE_CODE (limit) == SSA_NAME)
1771 add_equivalence (&vr_p->equiv, limit);
1773 else if (cond_code == NE_EXPR)
1775 /* As described above, when LIMIT's range is an anti-range and
1776 this assertion is an inequality (NE_EXPR), then we cannot
1777 derive anything from the anti-range. For instance, if
1778 LIMIT's range was ~[0, 0], the assertion 'VAR != LIMIT' does
1779 not imply that VAR's range is [0, 0]. So, in the case of
1780 anti-ranges, we just assert the inequality using LIMIT and
1781 not its anti-range.
1783 If LIMIT_VR is a range, we can only use it to build a new
1784 anti-range if LIMIT_VR is a single-valued range. For
1785 instance, if LIMIT_VR is [0, 1], the predicate
1786 VAR != [0, 1] does not mean that VAR's range is ~[0, 1].
1787 Rather, it means that for value 0 VAR should be ~[0, 0]
1788 and for value 1, VAR should be ~[1, 1]. We cannot
1789 represent these ranges.
1791 The only situation in which we can build a valid
1792 anti-range is when LIMIT_VR is a single-valued range
1793 (i.e., LIMIT_VR->MIN == LIMIT_VR->MAX). In that case,
1794 build the anti-range ~[LIMIT_VR->MIN, LIMIT_VR->MAX]. */
1795 if (limit_vr
1796 && limit_vr->type == VR_RANGE
1797 && compare_values (limit_vr->min, limit_vr->max) == 0)
1799 min = limit_vr->min;
1800 max = limit_vr->max;
1802 else
1804 /* In any other case, we cannot use LIMIT's range to build a
1805 valid anti-range. */
1806 min = max = limit;
1809 /* If MIN and MAX cover the whole range for their type, then
1810 just use the original LIMIT. */
1811 if (INTEGRAL_TYPE_P (type)
1812 && vrp_val_is_min (min)
1813 && vrp_val_is_max (max))
1814 min = max = limit;
1816 set_and_canonicalize_value_range (vr_p, VR_ANTI_RANGE,
1817 min, max, vr_p->equiv);
1819 else if (cond_code == LE_EXPR || cond_code == LT_EXPR)
1821 min = TYPE_MIN_VALUE (type);
1823 if (limit_vr == NULL || limit_vr->type == VR_ANTI_RANGE)
1824 max = limit;
1825 else
1827 /* If LIMIT_VR is of the form [N1, N2], we need to build the
1828 range [MIN, N2] for LE_EXPR and [MIN, N2 - 1] for
1829 LT_EXPR. */
1830 max = limit_vr->max;
1833 /* If the maximum value forces us to be out of bounds, simply punt.
1834 It would be pointless to try and do anything more since this
1835 all should be optimized away above us. */
1836 if ((cond_code == LT_EXPR
1837 && compare_values (max, min) == 0)
1838 || is_overflow_infinity (max))
1839 set_value_range_to_varying (vr_p);
1840 else
1842 /* For LT_EXPR, we create the range [MIN, MAX - 1]. */
1843 if (cond_code == LT_EXPR)
1845 if (TYPE_PRECISION (TREE_TYPE (max)) == 1
1846 && !TYPE_UNSIGNED (TREE_TYPE (max)))
1847 max = fold_build2 (PLUS_EXPR, TREE_TYPE (max), max,
1848 build_int_cst (TREE_TYPE (max), -1));
1849 else
1850 max = fold_build2 (MINUS_EXPR, TREE_TYPE (max), max,
1851 build_int_cst (TREE_TYPE (max), 1));
1852 if (EXPR_P (max))
1853 TREE_NO_WARNING (max) = 1;
1856 set_value_range (vr_p, VR_RANGE, min, max, vr_p->equiv);
1859 else if (cond_code == GE_EXPR || cond_code == GT_EXPR)
1861 max = TYPE_MAX_VALUE (type);
1863 if (limit_vr == NULL || limit_vr->type == VR_ANTI_RANGE)
1864 min = limit;
1865 else
1867 /* If LIMIT_VR is of the form [N1, N2], we need to build the
1868 range [N1, MAX] for GE_EXPR and [N1 + 1, MAX] for
1869 GT_EXPR. */
1870 min = limit_vr->min;
1873 /* If the minimum value forces us to be out of bounds, simply punt.
1874 It would be pointless to try and do anything more since this
1875 all should be optimized away above us. */
1876 if ((cond_code == GT_EXPR
1877 && compare_values (min, max) == 0)
1878 || is_overflow_infinity (min))
1879 set_value_range_to_varying (vr_p);
1880 else
1882 /* For GT_EXPR, we create the range [MIN + 1, MAX]. */
1883 if (cond_code == GT_EXPR)
1885 if (TYPE_PRECISION (TREE_TYPE (min)) == 1
1886 && !TYPE_UNSIGNED (TREE_TYPE (min)))
1887 min = fold_build2 (MINUS_EXPR, TREE_TYPE (min), min,
1888 build_int_cst (TREE_TYPE (min), -1));
1889 else
1890 min = fold_build2 (PLUS_EXPR, TREE_TYPE (min), min,
1891 build_int_cst (TREE_TYPE (min), 1));
1892 if (EXPR_P (min))
1893 TREE_NO_WARNING (min) = 1;
1896 set_value_range (vr_p, VR_RANGE, min, max, vr_p->equiv);
1899 else
1900 gcc_unreachable ();
1902 /* Finally intersect the new range with what we already know about var. */
1903 vrp_intersect_ranges (vr_p, get_value_range (var));
1907 /* Extract range information from SSA name VAR and store it in VR. If
1908 VAR has an interesting range, use it. Otherwise, create the
1909 range [VAR, VAR] and return it. This is useful in situations where
1910 we may have conditionals testing values of VARYING names. For
1911 instance,
1913 x_3 = y_5;
1914 if (x_3 > y_5)
1917 Even if y_5 is deemed VARYING, we can determine that x_3 > y_5 is
1918 always false. */
1920 static void
1921 extract_range_from_ssa_name (value_range_t *vr, tree var)
1923 value_range_t *var_vr = get_value_range (var);
1925 if (var_vr->type != VR_VARYING)
1926 copy_value_range (vr, var_vr);
1927 else
1928 set_value_range (vr, VR_RANGE, var, var, NULL);
1930 add_equivalence (&vr->equiv, var);
1934 /* Wrapper around int_const_binop. If the operation overflows and we
1935 are not using wrapping arithmetic, then adjust the result to be
1936 -INF or +INF depending on CODE, VAL1 and VAL2. This can return
1937 NULL_TREE if we need to use an overflow infinity representation but
1938 the type does not support it. */
1940 static tree
1941 vrp_int_const_binop (enum tree_code code, tree val1, tree val2)
1943 tree res;
1945 res = int_const_binop (code, val1, val2);
1947 /* If we are using unsigned arithmetic, operate symbolically
1948 on -INF and +INF as int_const_binop only handles signed overflow. */
1949 if (TYPE_UNSIGNED (TREE_TYPE (val1)))
1951 int checkz = compare_values (res, val1);
1952 bool overflow = false;
1954 /* Ensure that res = val1 [+*] val2 >= val1
1955 or that res = val1 - val2 <= val1. */
1956 if ((code == PLUS_EXPR
1957 && !(checkz == 1 || checkz == 0))
1958 || (code == MINUS_EXPR
1959 && !(checkz == 0 || checkz == -1)))
1961 overflow = true;
1963 /* Checking for multiplication overflow is done by dividing the
1964 output of the multiplication by the first input of the
1965 multiplication. If the result of that division operation is
1966 not equal to the second input of the multiplication, then the
1967 multiplication overflowed. */
1968 else if (code == MULT_EXPR && !integer_zerop (val1))
1970 tree tmp = int_const_binop (TRUNC_DIV_EXPR,
1971 res,
1972 val1);
1973 int check = compare_values (tmp, val2);
1975 if (check != 0)
1976 overflow = true;
1979 if (overflow)
1981 res = copy_node (res);
1982 TREE_OVERFLOW (res) = 1;
1986 else if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (val1)))
1987 /* If the singed operation wraps then int_const_binop has done
1988 everything we want. */
1990 /* Signed division of -1/0 overflows and by the time it gets here
1991 returns NULL_TREE. */
1992 else if (!res)
1993 return NULL_TREE;
1994 else if ((TREE_OVERFLOW (res)
1995 && !TREE_OVERFLOW (val1)
1996 && !TREE_OVERFLOW (val2))
1997 || is_overflow_infinity (val1)
1998 || is_overflow_infinity (val2))
2000 /* If the operation overflowed but neither VAL1 nor VAL2 are
2001 overflown, return -INF or +INF depending on the operation
2002 and the combination of signs of the operands. */
2003 int sgn1 = tree_int_cst_sgn (val1);
2004 int sgn2 = tree_int_cst_sgn (val2);
2006 if (needs_overflow_infinity (TREE_TYPE (res))
2007 && !supports_overflow_infinity (TREE_TYPE (res)))
2008 return NULL_TREE;
2010 /* We have to punt on adding infinities of different signs,
2011 since we can't tell what the sign of the result should be.
2012 Likewise for subtracting infinities of the same sign. */
2013 if (((code == PLUS_EXPR && sgn1 != sgn2)
2014 || (code == MINUS_EXPR && sgn1 == sgn2))
2015 && is_overflow_infinity (val1)
2016 && is_overflow_infinity (val2))
2017 return NULL_TREE;
2019 /* Don't try to handle division or shifting of infinities. */
2020 if ((code == TRUNC_DIV_EXPR
2021 || code == FLOOR_DIV_EXPR
2022 || code == CEIL_DIV_EXPR
2023 || code == EXACT_DIV_EXPR
2024 || code == ROUND_DIV_EXPR
2025 || code == RSHIFT_EXPR)
2026 && (is_overflow_infinity (val1)
2027 || is_overflow_infinity (val2)))
2028 return NULL_TREE;
2030 /* Notice that we only need to handle the restricted set of
2031 operations handled by extract_range_from_binary_expr.
2032 Among them, only multiplication, addition and subtraction
2033 can yield overflow without overflown operands because we
2034 are working with integral types only... except in the
2035 case VAL1 = -INF and VAL2 = -1 which overflows to +INF
2036 for division too. */
2038 /* For multiplication, the sign of the overflow is given
2039 by the comparison of the signs of the operands. */
2040 if ((code == MULT_EXPR && sgn1 == sgn2)
2041 /* For addition, the operands must be of the same sign
2042 to yield an overflow. Its sign is therefore that
2043 of one of the operands, for example the first. For
2044 infinite operands X + -INF is negative, not positive. */
2045 || (code == PLUS_EXPR
2046 && (sgn1 >= 0
2047 ? !is_negative_overflow_infinity (val2)
2048 : is_positive_overflow_infinity (val2)))
2049 /* For subtraction, non-infinite operands must be of
2050 different signs to yield an overflow. Its sign is
2051 therefore that of the first operand or the opposite of
2052 that of the second operand. A first operand of 0 counts
2053 as positive here, for the corner case 0 - (-INF), which
2054 overflows, but must yield +INF. For infinite operands 0
2055 - INF is negative, not positive. */
2056 || (code == MINUS_EXPR
2057 && (sgn1 >= 0
2058 ? !is_positive_overflow_infinity (val2)
2059 : is_negative_overflow_infinity (val2)))
2060 /* We only get in here with positive shift count, so the
2061 overflow direction is the same as the sign of val1.
2062 Actually rshift does not overflow at all, but we only
2063 handle the case of shifting overflowed -INF and +INF. */
2064 || (code == RSHIFT_EXPR
2065 && sgn1 >= 0)
2066 /* For division, the only case is -INF / -1 = +INF. */
2067 || code == TRUNC_DIV_EXPR
2068 || code == FLOOR_DIV_EXPR
2069 || code == CEIL_DIV_EXPR
2070 || code == EXACT_DIV_EXPR
2071 || code == ROUND_DIV_EXPR)
2072 return (needs_overflow_infinity (TREE_TYPE (res))
2073 ? positive_overflow_infinity (TREE_TYPE (res))
2074 : TYPE_MAX_VALUE (TREE_TYPE (res)));
2075 else
2076 return (needs_overflow_infinity (TREE_TYPE (res))
2077 ? negative_overflow_infinity (TREE_TYPE (res))
2078 : TYPE_MIN_VALUE (TREE_TYPE (res)));
2081 return res;
2085 /* For range VR compute two wide_int bitmasks. In *MAY_BE_NONZERO
2086 bitmask if some bit is unset, it means for all numbers in the range
2087 the bit is 0, otherwise it might be 0 or 1. In *MUST_BE_NONZERO
2088 bitmask if some bit is set, it means for all numbers in the range
2089 the bit is 1, otherwise it might be 0 or 1. */
2091 static bool
2092 zero_nonzero_bits_from_vr (const tree expr_type,
2093 value_range_t *vr,
2094 wide_int *may_be_nonzero,
2095 wide_int *must_be_nonzero)
2097 *may_be_nonzero = wi::minus_one (TYPE_PRECISION (expr_type));
2098 *must_be_nonzero = wi::zero (TYPE_PRECISION (expr_type));
2099 if (!range_int_cst_p (vr)
2100 || is_overflow_infinity (vr->min)
2101 || is_overflow_infinity (vr->max))
2102 return false;
2104 if (range_int_cst_singleton_p (vr))
2106 *may_be_nonzero = vr->min;
2107 *must_be_nonzero = *may_be_nonzero;
2109 else if (tree_int_cst_sgn (vr->min) >= 0
2110 || tree_int_cst_sgn (vr->max) < 0)
2112 wide_int xor_mask = wi::bit_xor (vr->min, vr->max);
2113 *may_be_nonzero = wi::bit_or (vr->min, vr->max);
2114 *must_be_nonzero = wi::bit_and (vr->min, vr->max);
2115 if (xor_mask != 0)
2117 wide_int mask = wi::mask (wi::floor_log2 (xor_mask), false,
2118 may_be_nonzero->get_precision ());
2119 *may_be_nonzero = *may_be_nonzero | mask;
2120 *must_be_nonzero = must_be_nonzero->and_not (mask);
2124 return true;
2127 /* Create two value-ranges in *VR0 and *VR1 from the anti-range *AR
2128 so that *VR0 U *VR1 == *AR. Returns true if that is possible,
2129 false otherwise. If *AR can be represented with a single range
2130 *VR1 will be VR_UNDEFINED. */
2132 static bool
2133 ranges_from_anti_range (value_range_t *ar,
2134 value_range_t *vr0, value_range_t *vr1)
2136 tree type = TREE_TYPE (ar->min);
2138 vr0->type = VR_UNDEFINED;
2139 vr1->type = VR_UNDEFINED;
2141 if (ar->type != VR_ANTI_RANGE
2142 || TREE_CODE (ar->min) != INTEGER_CST
2143 || TREE_CODE (ar->max) != INTEGER_CST
2144 || !vrp_val_min (type)
2145 || !vrp_val_max (type))
2146 return false;
2148 if (!vrp_val_is_min (ar->min))
2150 vr0->type = VR_RANGE;
2151 vr0->min = vrp_val_min (type);
2152 vr0->max = wide_int_to_tree (type, wi::sub (ar->min, 1));
2154 if (!vrp_val_is_max (ar->max))
2156 vr1->type = VR_RANGE;
2157 vr1->min = wide_int_to_tree (type, wi::add (ar->max, 1));
2158 vr1->max = vrp_val_max (type);
2160 if (vr0->type == VR_UNDEFINED)
2162 *vr0 = *vr1;
2163 vr1->type = VR_UNDEFINED;
2166 return vr0->type != VR_UNDEFINED;
2169 /* Helper to extract a value-range *VR for a multiplicative operation
2170 *VR0 CODE *VR1. */
2172 static void
2173 extract_range_from_multiplicative_op_1 (value_range_t *vr,
2174 enum tree_code code,
2175 value_range_t *vr0, value_range_t *vr1)
2177 enum value_range_type type;
2178 tree val[4];
2179 size_t i;
2180 tree min, max;
2181 bool sop;
2182 int cmp;
2184 /* Multiplications, divisions and shifts are a bit tricky to handle,
2185 depending on the mix of signs we have in the two ranges, we
2186 need to operate on different values to get the minimum and
2187 maximum values for the new range. One approach is to figure
2188 out all the variations of range combinations and do the
2189 operations.
2191 However, this involves several calls to compare_values and it
2192 is pretty convoluted. It's simpler to do the 4 operations
2193 (MIN0 OP MIN1, MIN0 OP MAX1, MAX0 OP MIN1 and MAX0 OP MAX0 OP
2194 MAX1) and then figure the smallest and largest values to form
2195 the new range. */
2196 gcc_assert (code == MULT_EXPR
2197 || code == TRUNC_DIV_EXPR
2198 || code == FLOOR_DIV_EXPR
2199 || code == CEIL_DIV_EXPR
2200 || code == EXACT_DIV_EXPR
2201 || code == ROUND_DIV_EXPR
2202 || code == RSHIFT_EXPR
2203 || code == LSHIFT_EXPR);
2204 gcc_assert ((vr0->type == VR_RANGE
2205 || (code == MULT_EXPR && vr0->type == VR_ANTI_RANGE))
2206 && vr0->type == vr1->type);
2208 type = vr0->type;
2210 /* Compute the 4 cross operations. */
2211 sop = false;
2212 val[0] = vrp_int_const_binop (code, vr0->min, vr1->min);
2213 if (val[0] == NULL_TREE)
2214 sop = true;
2216 if (vr1->max == vr1->min)
2217 val[1] = NULL_TREE;
2218 else
2220 val[1] = vrp_int_const_binop (code, vr0->min, vr1->max);
2221 if (val[1] == NULL_TREE)
2222 sop = true;
2225 if (vr0->max == vr0->min)
2226 val[2] = NULL_TREE;
2227 else
2229 val[2] = vrp_int_const_binop (code, vr0->max, vr1->min);
2230 if (val[2] == NULL_TREE)
2231 sop = true;
2234 if (vr0->min == vr0->max || vr1->min == vr1->max)
2235 val[3] = NULL_TREE;
2236 else
2238 val[3] = vrp_int_const_binop (code, vr0->max, vr1->max);
2239 if (val[3] == NULL_TREE)
2240 sop = true;
2243 if (sop)
2245 set_value_range_to_varying (vr);
2246 return;
2249 /* Set MIN to the minimum of VAL[i] and MAX to the maximum
2250 of VAL[i]. */
2251 min = val[0];
2252 max = val[0];
2253 for (i = 1; i < 4; i++)
2255 if (!is_gimple_min_invariant (min)
2256 || (TREE_OVERFLOW (min) && !is_overflow_infinity (min))
2257 || !is_gimple_min_invariant (max)
2258 || (TREE_OVERFLOW (max) && !is_overflow_infinity (max)))
2259 break;
2261 if (val[i])
2263 if (!is_gimple_min_invariant (val[i])
2264 || (TREE_OVERFLOW (val[i])
2265 && !is_overflow_infinity (val[i])))
2267 /* If we found an overflowed value, set MIN and MAX
2268 to it so that we set the resulting range to
2269 VARYING. */
2270 min = max = val[i];
2271 break;
2274 if (compare_values (val[i], min) == -1)
2275 min = val[i];
2277 if (compare_values (val[i], max) == 1)
2278 max = val[i];
2282 /* If either MIN or MAX overflowed, then set the resulting range to
2283 VARYING. But we do accept an overflow infinity
2284 representation. */
2285 if (min == NULL_TREE
2286 || !is_gimple_min_invariant (min)
2287 || (TREE_OVERFLOW (min) && !is_overflow_infinity (min))
2288 || max == NULL_TREE
2289 || !is_gimple_min_invariant (max)
2290 || (TREE_OVERFLOW (max) && !is_overflow_infinity (max)))
2292 set_value_range_to_varying (vr);
2293 return;
2296 /* We punt if:
2297 1) [-INF, +INF]
2298 2) [-INF, +-INF(OVF)]
2299 3) [+-INF(OVF), +INF]
2300 4) [+-INF(OVF), +-INF(OVF)]
2301 We learn nothing when we have INF and INF(OVF) on both sides.
2302 Note that we do accept [-INF, -INF] and [+INF, +INF] without
2303 overflow. */
2304 if ((vrp_val_is_min (min) || is_overflow_infinity (min))
2305 && (vrp_val_is_max (max) || is_overflow_infinity (max)))
2307 set_value_range_to_varying (vr);
2308 return;
2311 cmp = compare_values (min, max);
2312 if (cmp == -2 || cmp == 1)
2314 /* If the new range has its limits swapped around (MIN > MAX),
2315 then the operation caused one of them to wrap around, mark
2316 the new range VARYING. */
2317 set_value_range_to_varying (vr);
2319 else
2320 set_value_range (vr, type, min, max, NULL);
2323 /* Extract range information from a binary operation CODE based on
2324 the ranges of each of its operands *VR0 and *VR1 with resulting
2325 type EXPR_TYPE. The resulting range is stored in *VR. */
2327 static void
2328 extract_range_from_binary_expr_1 (value_range_t *vr,
2329 enum tree_code code, tree expr_type,
2330 value_range_t *vr0_, value_range_t *vr1_)
2332 value_range_t vr0 = *vr0_, vr1 = *vr1_;
2333 value_range_t vrtem0 = VR_INITIALIZER, vrtem1 = VR_INITIALIZER;
2334 enum value_range_type type;
2335 tree min = NULL_TREE, max = NULL_TREE;
2336 int cmp;
2338 if (!INTEGRAL_TYPE_P (expr_type)
2339 && !POINTER_TYPE_P (expr_type))
2341 set_value_range_to_varying (vr);
2342 return;
2345 /* Not all binary expressions can be applied to ranges in a
2346 meaningful way. Handle only arithmetic operations. */
2347 if (code != PLUS_EXPR
2348 && code != MINUS_EXPR
2349 && code != POINTER_PLUS_EXPR
2350 && code != MULT_EXPR
2351 && code != TRUNC_DIV_EXPR
2352 && code != FLOOR_DIV_EXPR
2353 && code != CEIL_DIV_EXPR
2354 && code != EXACT_DIV_EXPR
2355 && code != ROUND_DIV_EXPR
2356 && code != TRUNC_MOD_EXPR
2357 && code != RSHIFT_EXPR
2358 && code != LSHIFT_EXPR
2359 && code != MIN_EXPR
2360 && code != MAX_EXPR
2361 && code != BIT_AND_EXPR
2362 && code != BIT_IOR_EXPR
2363 && code != BIT_XOR_EXPR)
2365 set_value_range_to_varying (vr);
2366 return;
2369 /* If both ranges are UNDEFINED, so is the result. */
2370 if (vr0.type == VR_UNDEFINED && vr1.type == VR_UNDEFINED)
2372 set_value_range_to_undefined (vr);
2373 return;
2375 /* If one of the ranges is UNDEFINED drop it to VARYING for the following
2376 code. At some point we may want to special-case operations that
2377 have UNDEFINED result for all or some value-ranges of the not UNDEFINED
2378 operand. */
2379 else if (vr0.type == VR_UNDEFINED)
2380 set_value_range_to_varying (&vr0);
2381 else if (vr1.type == VR_UNDEFINED)
2382 set_value_range_to_varying (&vr1);
2384 /* Now canonicalize anti-ranges to ranges when they are not symbolic
2385 and express ~[] op X as ([]' op X) U ([]'' op X). */
2386 if (vr0.type == VR_ANTI_RANGE
2387 && ranges_from_anti_range (&vr0, &vrtem0, &vrtem1))
2389 extract_range_from_binary_expr_1 (vr, code, expr_type, &vrtem0, vr1_);
2390 if (vrtem1.type != VR_UNDEFINED)
2392 value_range_t vrres = VR_INITIALIZER;
2393 extract_range_from_binary_expr_1 (&vrres, code, expr_type,
2394 &vrtem1, vr1_);
2395 vrp_meet (vr, &vrres);
2397 return;
2399 /* Likewise for X op ~[]. */
2400 if (vr1.type == VR_ANTI_RANGE
2401 && ranges_from_anti_range (&vr1, &vrtem0, &vrtem1))
2403 extract_range_from_binary_expr_1 (vr, code, expr_type, vr0_, &vrtem0);
2404 if (vrtem1.type != VR_UNDEFINED)
2406 value_range_t vrres = VR_INITIALIZER;
2407 extract_range_from_binary_expr_1 (&vrres, code, expr_type,
2408 vr0_, &vrtem1);
2409 vrp_meet (vr, &vrres);
2411 return;
2414 /* The type of the resulting value range defaults to VR0.TYPE. */
2415 type = vr0.type;
2417 /* Refuse to operate on VARYING ranges, ranges of different kinds
2418 and symbolic ranges. As an exception, we allow BIT_{AND,IOR}
2419 because we may be able to derive a useful range even if one of
2420 the operands is VR_VARYING or symbolic range. Similarly for
2421 divisions, MIN/MAX and PLUS/MINUS.
2423 TODO, we may be able to derive anti-ranges in some cases. */
2424 if (code != BIT_AND_EXPR
2425 && code != BIT_IOR_EXPR
2426 && code != TRUNC_DIV_EXPR
2427 && code != FLOOR_DIV_EXPR
2428 && code != CEIL_DIV_EXPR
2429 && code != EXACT_DIV_EXPR
2430 && code != ROUND_DIV_EXPR
2431 && code != TRUNC_MOD_EXPR
2432 && code != MIN_EXPR
2433 && code != MAX_EXPR
2434 && code != PLUS_EXPR
2435 && code != MINUS_EXPR
2436 && (vr0.type == VR_VARYING
2437 || vr1.type == VR_VARYING
2438 || vr0.type != vr1.type
2439 || symbolic_range_p (&vr0)
2440 || symbolic_range_p (&vr1)))
2442 set_value_range_to_varying (vr);
2443 return;
2446 /* Now evaluate the expression to determine the new range. */
2447 if (POINTER_TYPE_P (expr_type))
2449 if (code == MIN_EXPR || code == MAX_EXPR)
2451 /* For MIN/MAX expressions with pointers, we only care about
2452 nullness, if both are non null, then the result is nonnull.
2453 If both are null, then the result is null. Otherwise they
2454 are varying. */
2455 if (range_is_nonnull (&vr0) && range_is_nonnull (&vr1))
2456 set_value_range_to_nonnull (vr, expr_type);
2457 else if (range_is_null (&vr0) && range_is_null (&vr1))
2458 set_value_range_to_null (vr, expr_type);
2459 else
2460 set_value_range_to_varying (vr);
2462 else if (code == POINTER_PLUS_EXPR)
2464 /* For pointer types, we are really only interested in asserting
2465 whether the expression evaluates to non-NULL. */
2466 if (range_is_nonnull (&vr0) || range_is_nonnull (&vr1))
2467 set_value_range_to_nonnull (vr, expr_type);
2468 else if (range_is_null (&vr0) && range_is_null (&vr1))
2469 set_value_range_to_null (vr, expr_type);
2470 else
2471 set_value_range_to_varying (vr);
2473 else if (code == BIT_AND_EXPR)
2475 /* For pointer types, we are really only interested in asserting
2476 whether the expression evaluates to non-NULL. */
2477 if (range_is_nonnull (&vr0) && range_is_nonnull (&vr1))
2478 set_value_range_to_nonnull (vr, expr_type);
2479 else if (range_is_null (&vr0) || range_is_null (&vr1))
2480 set_value_range_to_null (vr, expr_type);
2481 else
2482 set_value_range_to_varying (vr);
2484 else
2485 set_value_range_to_varying (vr);
2487 return;
2490 /* For integer ranges, apply the operation to each end of the
2491 range and see what we end up with. */
2492 if (code == PLUS_EXPR || code == MINUS_EXPR)
2494 const bool minus_p = (code == MINUS_EXPR);
2495 tree min_op0 = vr0.min;
2496 tree min_op1 = minus_p ? vr1.max : vr1.min;
2497 tree max_op0 = vr0.max;
2498 tree max_op1 = minus_p ? vr1.min : vr1.max;
2499 tree sym_min_op0 = NULL_TREE;
2500 tree sym_min_op1 = NULL_TREE;
2501 tree sym_max_op0 = NULL_TREE;
2502 tree sym_max_op1 = NULL_TREE;
2503 bool neg_min_op0, neg_min_op1, neg_max_op0, neg_max_op1;
2505 /* If we have a PLUS or MINUS with two VR_RANGEs, either constant or
2506 single-symbolic ranges, try to compute the precise resulting range,
2507 but only if we know that this resulting range will also be constant
2508 or single-symbolic. */
2509 if (vr0.type == VR_RANGE && vr1.type == VR_RANGE
2510 && (TREE_CODE (min_op0) == INTEGER_CST
2511 || (sym_min_op0
2512 = get_single_symbol (min_op0, &neg_min_op0, &min_op0)))
2513 && (TREE_CODE (min_op1) == INTEGER_CST
2514 || (sym_min_op1
2515 = get_single_symbol (min_op1, &neg_min_op1, &min_op1)))
2516 && (!(sym_min_op0 && sym_min_op1)
2517 || (sym_min_op0 == sym_min_op1
2518 && neg_min_op0 == (minus_p ? neg_min_op1 : !neg_min_op1)))
2519 && (TREE_CODE (max_op0) == INTEGER_CST
2520 || (sym_max_op0
2521 = get_single_symbol (max_op0, &neg_max_op0, &max_op0)))
2522 && (TREE_CODE (max_op1) == INTEGER_CST
2523 || (sym_max_op1
2524 = get_single_symbol (max_op1, &neg_max_op1, &max_op1)))
2525 && (!(sym_max_op0 && sym_max_op1)
2526 || (sym_max_op0 == sym_max_op1
2527 && neg_max_op0 == (minus_p ? neg_max_op1 : !neg_max_op1))))
2529 const signop sgn = TYPE_SIGN (expr_type);
2530 const unsigned int prec = TYPE_PRECISION (expr_type);
2531 wide_int type_min, type_max, wmin, wmax;
2532 int min_ovf = 0;
2533 int max_ovf = 0;
2535 /* Get the lower and upper bounds of the type. */
2536 if (TYPE_OVERFLOW_WRAPS (expr_type))
2538 type_min = wi::min_value (prec, sgn);
2539 type_max = wi::max_value (prec, sgn);
2541 else
2543 type_min = vrp_val_min (expr_type);
2544 type_max = vrp_val_max (expr_type);
2547 /* Combine the lower bounds, if any. */
2548 if (min_op0 && min_op1)
2550 if (minus_p)
2552 wmin = wi::sub (min_op0, min_op1);
2554 /* Check for overflow. */
2555 if (wi::cmp (0, min_op1, sgn)
2556 != wi::cmp (wmin, min_op0, sgn))
2557 min_ovf = wi::cmp (min_op0, min_op1, sgn);
2559 else
2561 wmin = wi::add (min_op0, min_op1);
2563 /* Check for overflow. */
2564 if (wi::cmp (min_op1, 0, sgn)
2565 != wi::cmp (wmin, min_op0, sgn))
2566 min_ovf = wi::cmp (min_op0, wmin, sgn);
2569 else if (min_op0)
2570 wmin = min_op0;
2571 else if (min_op1)
2572 wmin = minus_p ? wi::neg (min_op1) : min_op1;
2573 else
2574 wmin = wi::shwi (0, prec);
2576 /* Combine the upper bounds, if any. */
2577 if (max_op0 && max_op1)
2579 if (minus_p)
2581 wmax = wi::sub (max_op0, max_op1);
2583 /* Check for overflow. */
2584 if (wi::cmp (0, max_op1, sgn)
2585 != wi::cmp (wmax, max_op0, sgn))
2586 max_ovf = wi::cmp (max_op0, max_op1, sgn);
2588 else
2590 wmax = wi::add (max_op0, max_op1);
2592 if (wi::cmp (max_op1, 0, sgn)
2593 != wi::cmp (wmax, max_op0, sgn))
2594 max_ovf = wi::cmp (max_op0, wmax, sgn);
2597 else if (max_op0)
2598 wmax = max_op0;
2599 else if (max_op1)
2600 wmax = minus_p ? wi::neg (max_op1) : max_op1;
2601 else
2602 wmax = wi::shwi (0, prec);
2604 /* Check for type overflow. */
2605 if (min_ovf == 0)
2607 if (wi::cmp (wmin, type_min, sgn) == -1)
2608 min_ovf = -1;
2609 else if (wi::cmp (wmin, type_max, sgn) == 1)
2610 min_ovf = 1;
2612 if (max_ovf == 0)
2614 if (wi::cmp (wmax, type_min, sgn) == -1)
2615 max_ovf = -1;
2616 else if (wi::cmp (wmax, type_max, sgn) == 1)
2617 max_ovf = 1;
2620 /* If we have overflow for the constant part and the resulting
2621 range will be symbolic, drop to VR_VARYING. */
2622 if ((min_ovf && sym_min_op0 != sym_min_op1)
2623 || (max_ovf && sym_max_op0 != sym_max_op1))
2625 set_value_range_to_varying (vr);
2626 return;
2629 if (TYPE_OVERFLOW_WRAPS (expr_type))
2631 /* If overflow wraps, truncate the values and adjust the
2632 range kind and bounds appropriately. */
2633 wide_int tmin = wide_int::from (wmin, prec, sgn);
2634 wide_int tmax = wide_int::from (wmax, prec, sgn);
2635 if (min_ovf == max_ovf)
2637 /* No overflow or both overflow or underflow. The
2638 range kind stays VR_RANGE. */
2639 min = wide_int_to_tree (expr_type, tmin);
2640 max = wide_int_to_tree (expr_type, tmax);
2642 else if (min_ovf == -1 && max_ovf == 1)
2644 /* Underflow and overflow, drop to VR_VARYING. */
2645 set_value_range_to_varying (vr);
2646 return;
2648 else
2650 /* Min underflow or max overflow. The range kind
2651 changes to VR_ANTI_RANGE. */
2652 bool covers = false;
2653 wide_int tem = tmin;
2654 gcc_assert ((min_ovf == -1 && max_ovf == 0)
2655 || (max_ovf == 1 && min_ovf == 0));
2656 type = VR_ANTI_RANGE;
2657 tmin = tmax + 1;
2658 if (wi::cmp (tmin, tmax, sgn) < 0)
2659 covers = true;
2660 tmax = tem - 1;
2661 if (wi::cmp (tmax, tem, sgn) > 0)
2662 covers = true;
2663 /* If the anti-range would cover nothing, drop to varying.
2664 Likewise if the anti-range bounds are outside of the
2665 types values. */
2666 if (covers || wi::cmp (tmin, tmax, sgn) > 0)
2668 set_value_range_to_varying (vr);
2669 return;
2671 min = wide_int_to_tree (expr_type, tmin);
2672 max = wide_int_to_tree (expr_type, tmax);
2675 else
2677 /* If overflow does not wrap, saturate to the types min/max
2678 value. */
2679 if (min_ovf == -1)
2681 if (needs_overflow_infinity (expr_type)
2682 && supports_overflow_infinity (expr_type))
2683 min = negative_overflow_infinity (expr_type);
2684 else
2685 min = wide_int_to_tree (expr_type, type_min);
2687 else if (min_ovf == 1)
2689 if (needs_overflow_infinity (expr_type)
2690 && supports_overflow_infinity (expr_type))
2691 min = positive_overflow_infinity (expr_type);
2692 else
2693 min = wide_int_to_tree (expr_type, type_max);
2695 else
2696 min = wide_int_to_tree (expr_type, wmin);
2698 if (max_ovf == -1)
2700 if (needs_overflow_infinity (expr_type)
2701 && supports_overflow_infinity (expr_type))
2702 max = negative_overflow_infinity (expr_type);
2703 else
2704 max = wide_int_to_tree (expr_type, type_min);
2706 else if (max_ovf == 1)
2708 if (needs_overflow_infinity (expr_type)
2709 && supports_overflow_infinity (expr_type))
2710 max = positive_overflow_infinity (expr_type);
2711 else
2712 max = wide_int_to_tree (expr_type, type_max);
2714 else
2715 max = wide_int_to_tree (expr_type, wmax);
2718 if (needs_overflow_infinity (expr_type)
2719 && supports_overflow_infinity (expr_type))
2721 if ((min_op0 && is_negative_overflow_infinity (min_op0))
2722 || (min_op1
2723 && (minus_p
2724 ? is_positive_overflow_infinity (min_op1)
2725 : is_negative_overflow_infinity (min_op1))))
2726 min = negative_overflow_infinity (expr_type);
2727 if ((max_op0 && is_positive_overflow_infinity (max_op0))
2728 || (max_op1
2729 && (minus_p
2730 ? is_negative_overflow_infinity (max_op1)
2731 : is_positive_overflow_infinity (max_op1))))
2732 max = positive_overflow_infinity (expr_type);
2735 /* If the result lower bound is constant, we're done;
2736 otherwise, build the symbolic lower bound. */
2737 if (sym_min_op0 == sym_min_op1)
2739 else if (sym_min_op0)
2740 min = build_symbolic_expr (expr_type, sym_min_op0,
2741 neg_min_op0, min);
2742 else if (sym_min_op1)
2743 min = build_symbolic_expr (expr_type, sym_min_op1,
2744 neg_min_op1 ^ minus_p, min);
2746 /* Likewise for the upper bound. */
2747 if (sym_max_op0 == sym_max_op1)
2749 else if (sym_max_op0)
2750 max = build_symbolic_expr (expr_type, sym_max_op0,
2751 neg_max_op0, max);
2752 else if (sym_max_op1)
2753 max = build_symbolic_expr (expr_type, sym_max_op1,
2754 neg_max_op1 ^ minus_p, max);
2756 else
2758 /* For other cases, for example if we have a PLUS_EXPR with two
2759 VR_ANTI_RANGEs, drop to VR_VARYING. It would take more effort
2760 to compute a precise range for such a case.
2761 ??? General even mixed range kind operations can be expressed
2762 by for example transforming ~[3, 5] + [1, 2] to range-only
2763 operations and a union primitive:
2764 [-INF, 2] + [1, 2] U [5, +INF] + [1, 2]
2765 [-INF+1, 4] U [6, +INF(OVF)]
2766 though usually the union is not exactly representable with
2767 a single range or anti-range as the above is
2768 [-INF+1, +INF(OVF)] intersected with ~[5, 5]
2769 but one could use a scheme similar to equivalences for this. */
2770 set_value_range_to_varying (vr);
2771 return;
2774 else if (code == MIN_EXPR
2775 || code == MAX_EXPR)
2777 if (vr0.type == VR_RANGE
2778 && !symbolic_range_p (&vr0))
2780 type = VR_RANGE;
2781 if (vr1.type == VR_RANGE
2782 && !symbolic_range_p (&vr1))
2784 /* For operations that make the resulting range directly
2785 proportional to the original ranges, apply the operation to
2786 the same end of each range. */
2787 min = vrp_int_const_binop (code, vr0.min, vr1.min);
2788 max = vrp_int_const_binop (code, vr0.max, vr1.max);
2790 else if (code == MIN_EXPR)
2792 min = vrp_val_min (expr_type);
2793 max = vr0.max;
2795 else if (code == MAX_EXPR)
2797 min = vr0.min;
2798 max = vrp_val_max (expr_type);
2801 else if (vr1.type == VR_RANGE
2802 && !symbolic_range_p (&vr1))
2804 type = VR_RANGE;
2805 if (code == MIN_EXPR)
2807 min = vrp_val_min (expr_type);
2808 max = vr1.max;
2810 else if (code == MAX_EXPR)
2812 min = vr1.min;
2813 max = vrp_val_max (expr_type);
2816 else
2818 set_value_range_to_varying (vr);
2819 return;
2822 else if (code == MULT_EXPR)
2824 /* Fancy code so that with unsigned, [-3,-1]*[-3,-1] does not
2825 drop to varying. This test requires 2*prec bits if both
2826 operands are signed and 2*prec + 2 bits if either is not. */
2828 signop sign = TYPE_SIGN (expr_type);
2829 unsigned int prec = TYPE_PRECISION (expr_type);
2831 if (range_int_cst_p (&vr0)
2832 && range_int_cst_p (&vr1)
2833 && TYPE_OVERFLOW_WRAPS (expr_type))
2835 typedef FIXED_WIDE_INT (WIDE_INT_MAX_PRECISION * 2) vrp_int;
2836 typedef generic_wide_int
2837 <wi::extended_tree <WIDE_INT_MAX_PRECISION * 2> > vrp_int_cst;
2838 vrp_int sizem1 = wi::mask <vrp_int> (prec, false);
2839 vrp_int size = sizem1 + 1;
2841 /* Extend the values using the sign of the result to PREC2.
2842 From here on out, everthing is just signed math no matter
2843 what the input types were. */
2844 vrp_int min0 = vrp_int_cst (vr0.min);
2845 vrp_int max0 = vrp_int_cst (vr0.max);
2846 vrp_int min1 = vrp_int_cst (vr1.min);
2847 vrp_int max1 = vrp_int_cst (vr1.max);
2848 /* Canonicalize the intervals. */
2849 if (sign == UNSIGNED)
2851 if (wi::ltu_p (size, min0 + max0))
2853 min0 -= size;
2854 max0 -= size;
2857 if (wi::ltu_p (size, min1 + max1))
2859 min1 -= size;
2860 max1 -= size;
2864 vrp_int prod0 = min0 * min1;
2865 vrp_int prod1 = min0 * max1;
2866 vrp_int prod2 = max0 * min1;
2867 vrp_int prod3 = max0 * max1;
2869 /* Sort the 4 products so that min is in prod0 and max is in
2870 prod3. */
2871 /* min0min1 > max0max1 */
2872 if (wi::gts_p (prod0, prod3))
2874 vrp_int tmp = prod3;
2875 prod3 = prod0;
2876 prod0 = tmp;
2879 /* min0max1 > max0min1 */
2880 if (wi::gts_p (prod1, prod2))
2882 vrp_int tmp = prod2;
2883 prod2 = prod1;
2884 prod1 = tmp;
2887 if (wi::gts_p (prod0, prod1))
2889 vrp_int tmp = prod1;
2890 prod1 = prod0;
2891 prod0 = tmp;
2894 if (wi::gts_p (prod2, prod3))
2896 vrp_int tmp = prod3;
2897 prod3 = prod2;
2898 prod2 = tmp;
2901 /* diff = max - min. */
2902 prod2 = prod3 - prod0;
2903 if (wi::geu_p (prod2, sizem1))
2905 /* the range covers all values. */
2906 set_value_range_to_varying (vr);
2907 return;
2910 /* The following should handle the wrapping and selecting
2911 VR_ANTI_RANGE for us. */
2912 min = wide_int_to_tree (expr_type, prod0);
2913 max = wide_int_to_tree (expr_type, prod3);
2914 set_and_canonicalize_value_range (vr, VR_RANGE, min, max, NULL);
2915 return;
2918 /* If we have an unsigned MULT_EXPR with two VR_ANTI_RANGEs,
2919 drop to VR_VARYING. It would take more effort to compute a
2920 precise range for such a case. For example, if we have
2921 op0 == 65536 and op1 == 65536 with their ranges both being
2922 ~[0,0] on a 32-bit machine, we would have op0 * op1 == 0, so
2923 we cannot claim that the product is in ~[0,0]. Note that we
2924 are guaranteed to have vr0.type == vr1.type at this
2925 point. */
2926 if (vr0.type == VR_ANTI_RANGE
2927 && !TYPE_OVERFLOW_UNDEFINED (expr_type))
2929 set_value_range_to_varying (vr);
2930 return;
2933 extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1);
2934 return;
2936 else if (code == RSHIFT_EXPR
2937 || code == LSHIFT_EXPR)
2939 /* If we have a RSHIFT_EXPR with any shift values outside [0..prec-1],
2940 then drop to VR_VARYING. Outside of this range we get undefined
2941 behavior from the shift operation. We cannot even trust
2942 SHIFT_COUNT_TRUNCATED at this stage, because that applies to rtl
2943 shifts, and the operation at the tree level may be widened. */
2944 if (range_int_cst_p (&vr1)
2945 && compare_tree_int (vr1.min, 0) >= 0
2946 && compare_tree_int (vr1.max, TYPE_PRECISION (expr_type)) == -1)
2948 if (code == RSHIFT_EXPR)
2950 extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1);
2951 return;
2953 /* We can map lshifts by constants to MULT_EXPR handling. */
2954 else if (code == LSHIFT_EXPR
2955 && range_int_cst_singleton_p (&vr1))
2957 bool saved_flag_wrapv;
2958 value_range_t vr1p = VR_INITIALIZER;
2959 vr1p.type = VR_RANGE;
2960 vr1p.min = (wide_int_to_tree
2961 (expr_type,
2962 wi::set_bit_in_zero (tree_to_shwi (vr1.min),
2963 TYPE_PRECISION (expr_type))));
2964 vr1p.max = vr1p.min;
2965 /* We have to use a wrapping multiply though as signed overflow
2966 on lshifts is implementation defined in C89. */
2967 saved_flag_wrapv = flag_wrapv;
2968 flag_wrapv = 1;
2969 extract_range_from_binary_expr_1 (vr, MULT_EXPR, expr_type,
2970 &vr0, &vr1p);
2971 flag_wrapv = saved_flag_wrapv;
2972 return;
2974 else if (code == LSHIFT_EXPR
2975 && range_int_cst_p (&vr0))
2977 int prec = TYPE_PRECISION (expr_type);
2978 int overflow_pos = prec;
2979 int bound_shift;
2980 wide_int low_bound, high_bound;
2981 bool uns = TYPE_UNSIGNED (expr_type);
2982 bool in_bounds = false;
2984 if (!uns)
2985 overflow_pos -= 1;
2987 bound_shift = overflow_pos - tree_to_shwi (vr1.max);
2988 /* If bound_shift == HOST_BITS_PER_WIDE_INT, the llshift can
2989 overflow. However, for that to happen, vr1.max needs to be
2990 zero, which means vr1 is a singleton range of zero, which
2991 means it should be handled by the previous LSHIFT_EXPR
2992 if-clause. */
2993 wide_int bound = wi::set_bit_in_zero (bound_shift, prec);
2994 wide_int complement = ~(bound - 1);
2996 if (uns)
2998 low_bound = bound;
2999 high_bound = complement;
3000 if (wi::ltu_p (vr0.max, low_bound))
3002 /* [5, 6] << [1, 2] == [10, 24]. */
3003 /* We're shifting out only zeroes, the value increases
3004 monotonically. */
3005 in_bounds = true;
3007 else if (wi::ltu_p (high_bound, vr0.min))
3009 /* [0xffffff00, 0xffffffff] << [1, 2]
3010 == [0xfffffc00, 0xfffffffe]. */
3011 /* We're shifting out only ones, the value decreases
3012 monotonically. */
3013 in_bounds = true;
3016 else
3018 /* [-1, 1] << [1, 2] == [-4, 4]. */
3019 low_bound = complement;
3020 high_bound = bound;
3021 if (wi::lts_p (vr0.max, high_bound)
3022 && wi::lts_p (low_bound, vr0.min))
3024 /* For non-negative numbers, we're shifting out only
3025 zeroes, the value increases monotonically.
3026 For negative numbers, we're shifting out only ones, the
3027 value decreases monotomically. */
3028 in_bounds = true;
3032 if (in_bounds)
3034 extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1);
3035 return;
3039 set_value_range_to_varying (vr);
3040 return;
3042 else if (code == TRUNC_DIV_EXPR
3043 || code == FLOOR_DIV_EXPR
3044 || code == CEIL_DIV_EXPR
3045 || code == EXACT_DIV_EXPR
3046 || code == ROUND_DIV_EXPR)
3048 if (vr0.type != VR_RANGE || symbolic_range_p (&vr0))
3050 /* For division, if op1 has VR_RANGE but op0 does not, something
3051 can be deduced just from that range. Say [min, max] / [4, max]
3052 gives [min / 4, max / 4] range. */
3053 if (vr1.type == VR_RANGE
3054 && !symbolic_range_p (&vr1)
3055 && range_includes_zero_p (vr1.min, vr1.max) == 0)
3057 vr0.type = type = VR_RANGE;
3058 vr0.min = vrp_val_min (expr_type);
3059 vr0.max = vrp_val_max (expr_type);
3061 else
3063 set_value_range_to_varying (vr);
3064 return;
3068 /* For divisions, if flag_non_call_exceptions is true, we must
3069 not eliminate a division by zero. */
3070 if (cfun->can_throw_non_call_exceptions
3071 && (vr1.type != VR_RANGE
3072 || range_includes_zero_p (vr1.min, vr1.max) != 0))
3074 set_value_range_to_varying (vr);
3075 return;
3078 /* For divisions, if op0 is VR_RANGE, we can deduce a range
3079 even if op1 is VR_VARYING, VR_ANTI_RANGE, symbolic or can
3080 include 0. */
3081 if (vr0.type == VR_RANGE
3082 && (vr1.type != VR_RANGE
3083 || range_includes_zero_p (vr1.min, vr1.max) != 0))
3085 tree zero = build_int_cst (TREE_TYPE (vr0.min), 0);
3086 int cmp;
3088 min = NULL_TREE;
3089 max = NULL_TREE;
3090 if (TYPE_UNSIGNED (expr_type)
3091 || value_range_nonnegative_p (&vr1))
3093 /* For unsigned division or when divisor is known
3094 to be non-negative, the range has to cover
3095 all numbers from 0 to max for positive max
3096 and all numbers from min to 0 for negative min. */
3097 cmp = compare_values (vr0.max, zero);
3098 if (cmp == -1)
3099 max = zero;
3100 else if (cmp == 0 || cmp == 1)
3101 max = vr0.max;
3102 else
3103 type = VR_VARYING;
3104 cmp = compare_values (vr0.min, zero);
3105 if (cmp == 1)
3106 min = zero;
3107 else if (cmp == 0 || cmp == -1)
3108 min = vr0.min;
3109 else
3110 type = VR_VARYING;
3112 else
3114 /* Otherwise the range is -max .. max or min .. -min
3115 depending on which bound is bigger in absolute value,
3116 as the division can change the sign. */
3117 abs_extent_range (vr, vr0.min, vr0.max);
3118 return;
3120 if (type == VR_VARYING)
3122 set_value_range_to_varying (vr);
3123 return;
3126 else
3128 extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1);
3129 return;
3132 else if (code == TRUNC_MOD_EXPR)
3134 if (vr1.type != VR_RANGE
3135 || range_includes_zero_p (vr1.min, vr1.max) != 0
3136 || vrp_val_is_min (vr1.min))
3138 set_value_range_to_varying (vr);
3139 return;
3141 type = VR_RANGE;
3142 /* Compute MAX <|vr1.min|, |vr1.max|> - 1. */
3143 max = fold_unary_to_constant (ABS_EXPR, expr_type, vr1.min);
3144 if (tree_int_cst_lt (max, vr1.max))
3145 max = vr1.max;
3146 max = int_const_binop (MINUS_EXPR, max, build_int_cst (TREE_TYPE (max), 1));
3147 /* If the dividend is non-negative the modulus will be
3148 non-negative as well. */
3149 if (TYPE_UNSIGNED (expr_type)
3150 || value_range_nonnegative_p (&vr0))
3151 min = build_int_cst (TREE_TYPE (max), 0);
3152 else
3153 min = fold_unary_to_constant (NEGATE_EXPR, expr_type, max);
3155 else if (code == BIT_AND_EXPR || code == BIT_IOR_EXPR || code == BIT_XOR_EXPR)
3157 bool int_cst_range0, int_cst_range1;
3158 wide_int may_be_nonzero0, may_be_nonzero1;
3159 wide_int must_be_nonzero0, must_be_nonzero1;
3161 int_cst_range0 = zero_nonzero_bits_from_vr (expr_type, &vr0,
3162 &may_be_nonzero0,
3163 &must_be_nonzero0);
3164 int_cst_range1 = zero_nonzero_bits_from_vr (expr_type, &vr1,
3165 &may_be_nonzero1,
3166 &must_be_nonzero1);
3168 type = VR_RANGE;
3169 if (code == BIT_AND_EXPR)
3171 min = wide_int_to_tree (expr_type,
3172 must_be_nonzero0 & must_be_nonzero1);
3173 wide_int wmax = may_be_nonzero0 & may_be_nonzero1;
3174 /* If both input ranges contain only negative values we can
3175 truncate the result range maximum to the minimum of the
3176 input range maxima. */
3177 if (int_cst_range0 && int_cst_range1
3178 && tree_int_cst_sgn (vr0.max) < 0
3179 && tree_int_cst_sgn (vr1.max) < 0)
3181 wmax = wi::min (wmax, vr0.max, TYPE_SIGN (expr_type));
3182 wmax = wi::min (wmax, vr1.max, TYPE_SIGN (expr_type));
3184 /* If either input range contains only non-negative values
3185 we can truncate the result range maximum to the respective
3186 maximum of the input range. */
3187 if (int_cst_range0 && tree_int_cst_sgn (vr0.min) >= 0)
3188 wmax = wi::min (wmax, vr0.max, TYPE_SIGN (expr_type));
3189 if (int_cst_range1 && tree_int_cst_sgn (vr1.min) >= 0)
3190 wmax = wi::min (wmax, vr1.max, TYPE_SIGN (expr_type));
3191 max = wide_int_to_tree (expr_type, wmax);
3193 else if (code == BIT_IOR_EXPR)
3195 max = wide_int_to_tree (expr_type,
3196 may_be_nonzero0 | may_be_nonzero1);
3197 wide_int wmin = must_be_nonzero0 | must_be_nonzero1;
3198 /* If the input ranges contain only positive values we can
3199 truncate the minimum of the result range to the maximum
3200 of the input range minima. */
3201 if (int_cst_range0 && int_cst_range1
3202 && tree_int_cst_sgn (vr0.min) >= 0
3203 && tree_int_cst_sgn (vr1.min) >= 0)
3205 wmin = wi::max (wmin, vr0.min, TYPE_SIGN (expr_type));
3206 wmin = wi::max (wmin, vr1.min, TYPE_SIGN (expr_type));
3208 /* If either input range contains only negative values
3209 we can truncate the minimum of the result range to the
3210 respective minimum range. */
3211 if (int_cst_range0 && tree_int_cst_sgn (vr0.max) < 0)
3212 wmin = wi::max (wmin, vr0.min, TYPE_SIGN (expr_type));
3213 if (int_cst_range1 && tree_int_cst_sgn (vr1.max) < 0)
3214 wmin = wi::max (wmin, vr1.min, TYPE_SIGN (expr_type));
3215 min = wide_int_to_tree (expr_type, wmin);
3217 else if (code == BIT_XOR_EXPR)
3219 wide_int result_zero_bits = ((must_be_nonzero0 & must_be_nonzero1)
3220 | ~(may_be_nonzero0 | may_be_nonzero1));
3221 wide_int result_one_bits
3222 = (must_be_nonzero0.and_not (may_be_nonzero1)
3223 | must_be_nonzero1.and_not (may_be_nonzero0));
3224 max = wide_int_to_tree (expr_type, ~result_zero_bits);
3225 min = wide_int_to_tree (expr_type, result_one_bits);
3226 /* If the range has all positive or all negative values the
3227 result is better than VARYING. */
3228 if (tree_int_cst_sgn (min) < 0
3229 || tree_int_cst_sgn (max) >= 0)
3231 else
3232 max = min = NULL_TREE;
3235 else
3236 gcc_unreachable ();
3238 /* If either MIN or MAX overflowed, then set the resulting range to
3239 VARYING. But we do accept an overflow infinity representation. */
3240 if (min == NULL_TREE
3241 || (TREE_OVERFLOW_P (min) && !is_overflow_infinity (min))
3242 || max == NULL_TREE
3243 || (TREE_OVERFLOW_P (max) && !is_overflow_infinity (max)))
3245 set_value_range_to_varying (vr);
3246 return;
3249 /* We punt if:
3250 1) [-INF, +INF]
3251 2) [-INF, +-INF(OVF)]
3252 3) [+-INF(OVF), +INF]
3253 4) [+-INF(OVF), +-INF(OVF)]
3254 We learn nothing when we have INF and INF(OVF) on both sides.
3255 Note that we do accept [-INF, -INF] and [+INF, +INF] without
3256 overflow. */
3257 if ((vrp_val_is_min (min) || is_overflow_infinity (min))
3258 && (vrp_val_is_max (max) || is_overflow_infinity (max)))
3260 set_value_range_to_varying (vr);
3261 return;
3264 cmp = compare_values (min, max);
3265 if (cmp == -2 || cmp == 1)
3267 /* If the new range has its limits swapped around (MIN > MAX),
3268 then the operation caused one of them to wrap around, mark
3269 the new range VARYING. */
3270 set_value_range_to_varying (vr);
3272 else
3273 set_value_range (vr, type, min, max, NULL);
3276 /* Extract range information from a binary expression OP0 CODE OP1 based on
3277 the ranges of each of its operands with resulting type EXPR_TYPE.
3278 The resulting range is stored in *VR. */
3280 static void
3281 extract_range_from_binary_expr (value_range_t *vr,
3282 enum tree_code code,
3283 tree expr_type, tree op0, tree op1)
3285 value_range_t vr0 = VR_INITIALIZER;
3286 value_range_t vr1 = VR_INITIALIZER;
3288 /* Get value ranges for each operand. For constant operands, create
3289 a new value range with the operand to simplify processing. */
3290 if (TREE_CODE (op0) == SSA_NAME)
3291 vr0 = *(get_value_range (op0));
3292 else if (is_gimple_min_invariant (op0))
3293 set_value_range_to_value (&vr0, op0, NULL);
3294 else
3295 set_value_range_to_varying (&vr0);
3297 if (TREE_CODE (op1) == SSA_NAME)
3298 vr1 = *(get_value_range (op1));
3299 else if (is_gimple_min_invariant (op1))
3300 set_value_range_to_value (&vr1, op1, NULL);
3301 else
3302 set_value_range_to_varying (&vr1);
3304 extract_range_from_binary_expr_1 (vr, code, expr_type, &vr0, &vr1);
3306 /* Try harder for PLUS and MINUS if the range of one operand is symbolic
3307 and based on the other operand, for example if it was deduced from a
3308 symbolic comparison. When a bound of the range of the first operand
3309 is invariant, we set the corresponding bound of the new range to INF
3310 in order to avoid recursing on the range of the second operand. */
3311 if (vr->type == VR_VARYING
3312 && (code == PLUS_EXPR || code == MINUS_EXPR)
3313 && TREE_CODE (op1) == SSA_NAME
3314 && vr0.type == VR_RANGE
3315 && symbolic_range_based_on_p (&vr0, op1))
3317 const bool minus_p = (code == MINUS_EXPR);
3318 value_range_t n_vr1 = VR_INITIALIZER;
3320 /* Try with VR0 and [-INF, OP1]. */
3321 if (is_gimple_min_invariant (minus_p ? vr0.max : vr0.min))
3322 set_value_range (&n_vr1, VR_RANGE, vrp_val_min (expr_type), op1, NULL);
3324 /* Try with VR0 and [OP1, +INF]. */
3325 else if (is_gimple_min_invariant (minus_p ? vr0.min : vr0.max))
3326 set_value_range (&n_vr1, VR_RANGE, op1, vrp_val_max (expr_type), NULL);
3328 /* Try with VR0 and [OP1, OP1]. */
3329 else
3330 set_value_range (&n_vr1, VR_RANGE, op1, op1, NULL);
3332 extract_range_from_binary_expr_1 (vr, code, expr_type, &vr0, &n_vr1);
3335 if (vr->type == VR_VARYING
3336 && (code == PLUS_EXPR || code == MINUS_EXPR)
3337 && TREE_CODE (op0) == SSA_NAME
3338 && vr1.type == VR_RANGE
3339 && symbolic_range_based_on_p (&vr1, op0))
3341 const bool minus_p = (code == MINUS_EXPR);
3342 value_range_t n_vr0 = VR_INITIALIZER;
3344 /* Try with [-INF, OP0] and VR1. */
3345 if (is_gimple_min_invariant (minus_p ? vr1.max : vr1.min))
3346 set_value_range (&n_vr0, VR_RANGE, vrp_val_min (expr_type), op0, NULL);
3348 /* Try with [OP0, +INF] and VR1. */
3349 else if (is_gimple_min_invariant (minus_p ? vr1.min : vr1.max))
3350 set_value_range (&n_vr0, VR_RANGE, op0, vrp_val_max (expr_type), NULL);
3352 /* Try with [OP0, OP0] and VR1. */
3353 else
3354 set_value_range (&n_vr0, VR_RANGE, op0, op0, NULL);
3356 extract_range_from_binary_expr_1 (vr, code, expr_type, &n_vr0, &vr1);
3360 /* Extract range information from a unary operation CODE based on
3361 the range of its operand *VR0 with type OP0_TYPE with resulting type TYPE.
3362 The The resulting range is stored in *VR. */
3364 static void
3365 extract_range_from_unary_expr_1 (value_range_t *vr,
3366 enum tree_code code, tree type,
3367 value_range_t *vr0_, tree op0_type)
3369 value_range_t vr0 = *vr0_, vrtem0 = VR_INITIALIZER, vrtem1 = VR_INITIALIZER;
3371 /* VRP only operates on integral and pointer types. */
3372 if (!(INTEGRAL_TYPE_P (op0_type)
3373 || POINTER_TYPE_P (op0_type))
3374 || !(INTEGRAL_TYPE_P (type)
3375 || POINTER_TYPE_P (type)))
3377 set_value_range_to_varying (vr);
3378 return;
3381 /* If VR0 is UNDEFINED, so is the result. */
3382 if (vr0.type == VR_UNDEFINED)
3384 set_value_range_to_undefined (vr);
3385 return;
3388 /* Handle operations that we express in terms of others. */
3389 if (code == PAREN_EXPR || code == OBJ_TYPE_REF)
3391 /* PAREN_EXPR and OBJ_TYPE_REF are simple copies. */
3392 copy_value_range (vr, &vr0);
3393 return;
3395 else if (code == NEGATE_EXPR)
3397 /* -X is simply 0 - X, so re-use existing code that also handles
3398 anti-ranges fine. */
3399 value_range_t zero = VR_INITIALIZER;
3400 set_value_range_to_value (&zero, build_int_cst (type, 0), NULL);
3401 extract_range_from_binary_expr_1 (vr, MINUS_EXPR, type, &zero, &vr0);
3402 return;
3404 else if (code == BIT_NOT_EXPR)
3406 /* ~X is simply -1 - X, so re-use existing code that also handles
3407 anti-ranges fine. */
3408 value_range_t minusone = VR_INITIALIZER;
3409 set_value_range_to_value (&minusone, build_int_cst (type, -1), NULL);
3410 extract_range_from_binary_expr_1 (vr, MINUS_EXPR,
3411 type, &minusone, &vr0);
3412 return;
3415 /* Now canonicalize anti-ranges to ranges when they are not symbolic
3416 and express op ~[] as (op []') U (op []''). */
3417 if (vr0.type == VR_ANTI_RANGE
3418 && ranges_from_anti_range (&vr0, &vrtem0, &vrtem1))
3420 extract_range_from_unary_expr_1 (vr, code, type, &vrtem0, op0_type);
3421 if (vrtem1.type != VR_UNDEFINED)
3423 value_range_t vrres = VR_INITIALIZER;
3424 extract_range_from_unary_expr_1 (&vrres, code, type,
3425 &vrtem1, op0_type);
3426 vrp_meet (vr, &vrres);
3428 return;
3431 if (CONVERT_EXPR_CODE_P (code))
3433 tree inner_type = op0_type;
3434 tree outer_type = type;
3436 /* If the expression evaluates to a pointer, we are only interested in
3437 determining if it evaluates to NULL [0, 0] or non-NULL (~[0, 0]). */
3438 if (POINTER_TYPE_P (type))
3440 if (range_is_nonnull (&vr0))
3441 set_value_range_to_nonnull (vr, type);
3442 else if (range_is_null (&vr0))
3443 set_value_range_to_null (vr, type);
3444 else
3445 set_value_range_to_varying (vr);
3446 return;
3449 /* If VR0 is varying and we increase the type precision, assume
3450 a full range for the following transformation. */
3451 if (vr0.type == VR_VARYING
3452 && INTEGRAL_TYPE_P (inner_type)
3453 && TYPE_PRECISION (inner_type) < TYPE_PRECISION (outer_type))
3455 vr0.type = VR_RANGE;
3456 vr0.min = TYPE_MIN_VALUE (inner_type);
3457 vr0.max = TYPE_MAX_VALUE (inner_type);
3460 /* If VR0 is a constant range or anti-range and the conversion is
3461 not truncating we can convert the min and max values and
3462 canonicalize the resulting range. Otherwise we can do the
3463 conversion if the size of the range is less than what the
3464 precision of the target type can represent and the range is
3465 not an anti-range. */
3466 if ((vr0.type == VR_RANGE
3467 || vr0.type == VR_ANTI_RANGE)
3468 && TREE_CODE (vr0.min) == INTEGER_CST
3469 && TREE_CODE (vr0.max) == INTEGER_CST
3470 && (!is_overflow_infinity (vr0.min)
3471 || (vr0.type == VR_RANGE
3472 && TYPE_PRECISION (outer_type) > TYPE_PRECISION (inner_type)
3473 && needs_overflow_infinity (outer_type)
3474 && supports_overflow_infinity (outer_type)))
3475 && (!is_overflow_infinity (vr0.max)
3476 || (vr0.type == VR_RANGE
3477 && TYPE_PRECISION (outer_type) > TYPE_PRECISION (inner_type)
3478 && needs_overflow_infinity (outer_type)
3479 && supports_overflow_infinity (outer_type)))
3480 && (TYPE_PRECISION (outer_type) >= TYPE_PRECISION (inner_type)
3481 || (vr0.type == VR_RANGE
3482 && integer_zerop (int_const_binop (RSHIFT_EXPR,
3483 int_const_binop (MINUS_EXPR, vr0.max, vr0.min),
3484 size_int (TYPE_PRECISION (outer_type)))))))
3486 tree new_min, new_max;
3487 if (is_overflow_infinity (vr0.min))
3488 new_min = negative_overflow_infinity (outer_type);
3489 else
3490 new_min = force_fit_type (outer_type, wi::to_widest (vr0.min),
3491 0, false);
3492 if (is_overflow_infinity (vr0.max))
3493 new_max = positive_overflow_infinity (outer_type);
3494 else
3495 new_max = force_fit_type (outer_type, wi::to_widest (vr0.max),
3496 0, false);
3497 set_and_canonicalize_value_range (vr, vr0.type,
3498 new_min, new_max, NULL);
3499 return;
3502 set_value_range_to_varying (vr);
3503 return;
3505 else if (code == ABS_EXPR)
3507 tree min, max;
3508 int cmp;
3510 /* Pass through vr0 in the easy cases. */
3511 if (TYPE_UNSIGNED (type)
3512 || value_range_nonnegative_p (&vr0))
3514 copy_value_range (vr, &vr0);
3515 return;
3518 /* For the remaining varying or symbolic ranges we can't do anything
3519 useful. */
3520 if (vr0.type == VR_VARYING
3521 || symbolic_range_p (&vr0))
3523 set_value_range_to_varying (vr);
3524 return;
3527 /* -TYPE_MIN_VALUE = TYPE_MIN_VALUE with flag_wrapv so we can't get a
3528 useful range. */
3529 if (!TYPE_OVERFLOW_UNDEFINED (type)
3530 && ((vr0.type == VR_RANGE
3531 && vrp_val_is_min (vr0.min))
3532 || (vr0.type == VR_ANTI_RANGE
3533 && !vrp_val_is_min (vr0.min))))
3535 set_value_range_to_varying (vr);
3536 return;
3539 /* ABS_EXPR may flip the range around, if the original range
3540 included negative values. */
3541 if (is_overflow_infinity (vr0.min))
3542 min = positive_overflow_infinity (type);
3543 else if (!vrp_val_is_min (vr0.min))
3544 min = fold_unary_to_constant (code, type, vr0.min);
3545 else if (!needs_overflow_infinity (type))
3546 min = TYPE_MAX_VALUE (type);
3547 else if (supports_overflow_infinity (type))
3548 min = positive_overflow_infinity (type);
3549 else
3551 set_value_range_to_varying (vr);
3552 return;
3555 if (is_overflow_infinity (vr0.max))
3556 max = positive_overflow_infinity (type);
3557 else if (!vrp_val_is_min (vr0.max))
3558 max = fold_unary_to_constant (code, type, vr0.max);
3559 else if (!needs_overflow_infinity (type))
3560 max = TYPE_MAX_VALUE (type);
3561 else if (supports_overflow_infinity (type)
3562 /* We shouldn't generate [+INF, +INF] as set_value_range
3563 doesn't like this and ICEs. */
3564 && !is_positive_overflow_infinity (min))
3565 max = positive_overflow_infinity (type);
3566 else
3568 set_value_range_to_varying (vr);
3569 return;
3572 cmp = compare_values (min, max);
3574 /* If a VR_ANTI_RANGEs contains zero, then we have
3575 ~[-INF, min(MIN, MAX)]. */
3576 if (vr0.type == VR_ANTI_RANGE)
3578 if (range_includes_zero_p (vr0.min, vr0.max) == 1)
3580 /* Take the lower of the two values. */
3581 if (cmp != 1)
3582 max = min;
3584 /* Create ~[-INF, min (abs(MIN), abs(MAX))]
3585 or ~[-INF + 1, min (abs(MIN), abs(MAX))] when
3586 flag_wrapv is set and the original anti-range doesn't include
3587 TYPE_MIN_VALUE, remember -TYPE_MIN_VALUE = TYPE_MIN_VALUE. */
3588 if (TYPE_OVERFLOW_WRAPS (type))
3590 tree type_min_value = TYPE_MIN_VALUE (type);
3592 min = (vr0.min != type_min_value
3593 ? int_const_binop (PLUS_EXPR, type_min_value,
3594 build_int_cst (TREE_TYPE (type_min_value), 1))
3595 : type_min_value);
3597 else
3599 if (overflow_infinity_range_p (&vr0))
3600 min = negative_overflow_infinity (type);
3601 else
3602 min = TYPE_MIN_VALUE (type);
3605 else
3607 /* All else has failed, so create the range [0, INF], even for
3608 flag_wrapv since TYPE_MIN_VALUE is in the original
3609 anti-range. */
3610 vr0.type = VR_RANGE;
3611 min = build_int_cst (type, 0);
3612 if (needs_overflow_infinity (type))
3614 if (supports_overflow_infinity (type))
3615 max = positive_overflow_infinity (type);
3616 else
3618 set_value_range_to_varying (vr);
3619 return;
3622 else
3623 max = TYPE_MAX_VALUE (type);
3627 /* If the range contains zero then we know that the minimum value in the
3628 range will be zero. */
3629 else if (range_includes_zero_p (vr0.min, vr0.max) == 1)
3631 if (cmp == 1)
3632 max = min;
3633 min = build_int_cst (type, 0);
3635 else
3637 /* If the range was reversed, swap MIN and MAX. */
3638 if (cmp == 1)
3640 tree t = min;
3641 min = max;
3642 max = t;
3646 cmp = compare_values (min, max);
3647 if (cmp == -2 || cmp == 1)
3649 /* If the new range has its limits swapped around (MIN > MAX),
3650 then the operation caused one of them to wrap around, mark
3651 the new range VARYING. */
3652 set_value_range_to_varying (vr);
3654 else
3655 set_value_range (vr, vr0.type, min, max, NULL);
3656 return;
3659 /* For unhandled operations fall back to varying. */
3660 set_value_range_to_varying (vr);
3661 return;
3665 /* Extract range information from a unary expression CODE OP0 based on
3666 the range of its operand with resulting type TYPE.
3667 The resulting range is stored in *VR. */
3669 static void
3670 extract_range_from_unary_expr (value_range_t *vr, enum tree_code code,
3671 tree type, tree op0)
3673 value_range_t vr0 = VR_INITIALIZER;
3675 /* Get value ranges for the operand. For constant operands, create
3676 a new value range with the operand to simplify processing. */
3677 if (TREE_CODE (op0) == SSA_NAME)
3678 vr0 = *(get_value_range (op0));
3679 else if (is_gimple_min_invariant (op0))
3680 set_value_range_to_value (&vr0, op0, NULL);
3681 else
3682 set_value_range_to_varying (&vr0);
3684 extract_range_from_unary_expr_1 (vr, code, type, &vr0, TREE_TYPE (op0));
3688 /* Extract range information from a conditional expression STMT based on
3689 the ranges of each of its operands and the expression code. */
3691 static void
3692 extract_range_from_cond_expr (value_range_t *vr, gimple stmt)
3694 tree op0, op1;
3695 value_range_t vr0 = VR_INITIALIZER;
3696 value_range_t vr1 = VR_INITIALIZER;
3698 /* Get value ranges for each operand. For constant operands, create
3699 a new value range with the operand to simplify processing. */
3700 op0 = gimple_assign_rhs2 (stmt);
3701 if (TREE_CODE (op0) == SSA_NAME)
3702 vr0 = *(get_value_range (op0));
3703 else if (is_gimple_min_invariant (op0))
3704 set_value_range_to_value (&vr0, op0, NULL);
3705 else
3706 set_value_range_to_varying (&vr0);
3708 op1 = gimple_assign_rhs3 (stmt);
3709 if (TREE_CODE (op1) == SSA_NAME)
3710 vr1 = *(get_value_range (op1));
3711 else if (is_gimple_min_invariant (op1))
3712 set_value_range_to_value (&vr1, op1, NULL);
3713 else
3714 set_value_range_to_varying (&vr1);
3716 /* The resulting value range is the union of the operand ranges */
3717 copy_value_range (vr, &vr0);
3718 vrp_meet (vr, &vr1);
3722 /* Extract range information from a comparison expression EXPR based
3723 on the range of its operand and the expression code. */
3725 static void
3726 extract_range_from_comparison (value_range_t *vr, enum tree_code code,
3727 tree type, tree op0, tree op1)
3729 bool sop = false;
3730 tree val;
3732 val = vrp_evaluate_conditional_warnv_with_ops (code, op0, op1, false, &sop,
3733 NULL);
3735 /* A disadvantage of using a special infinity as an overflow
3736 representation is that we lose the ability to record overflow
3737 when we don't have an infinity. So we have to ignore a result
3738 which relies on overflow. */
3740 if (val && !is_overflow_infinity (val) && !sop)
3742 /* Since this expression was found on the RHS of an assignment,
3743 its type may be different from _Bool. Convert VAL to EXPR's
3744 type. */
3745 val = fold_convert (type, val);
3746 if (is_gimple_min_invariant (val))
3747 set_value_range_to_value (vr, val, vr->equiv);
3748 else
3749 set_value_range (vr, VR_RANGE, val, val, vr->equiv);
3751 else
3752 /* The result of a comparison is always true or false. */
3753 set_value_range_to_truthvalue (vr, type);
3756 /* Try to derive a nonnegative or nonzero range out of STMT relying
3757 primarily on generic routines in fold in conjunction with range data.
3758 Store the result in *VR */
3760 static void
3761 extract_range_basic (value_range_t *vr, gimple stmt)
3763 bool sop = false;
3764 tree type = gimple_expr_type (stmt);
3766 if (gimple_call_builtin_p (stmt, BUILT_IN_NORMAL))
3768 tree fndecl = gimple_call_fndecl (stmt), arg;
3769 int mini, maxi, zerov = 0, prec;
3771 switch (DECL_FUNCTION_CODE (fndecl))
3773 case BUILT_IN_CONSTANT_P:
3774 /* If the call is __builtin_constant_p and the argument is a
3775 function parameter resolve it to false. This avoids bogus
3776 array bound warnings.
3777 ??? We could do this as early as inlining is finished. */
3778 arg = gimple_call_arg (stmt, 0);
3779 if (TREE_CODE (arg) == SSA_NAME
3780 && SSA_NAME_IS_DEFAULT_DEF (arg)
3781 && TREE_CODE (SSA_NAME_VAR (arg)) == PARM_DECL)
3783 set_value_range_to_null (vr, type);
3784 return;
3786 break;
3787 /* Both __builtin_ffs* and __builtin_popcount return
3788 [0, prec]. */
3789 CASE_INT_FN (BUILT_IN_FFS):
3790 CASE_INT_FN (BUILT_IN_POPCOUNT):
3791 arg = gimple_call_arg (stmt, 0);
3792 prec = TYPE_PRECISION (TREE_TYPE (arg));
3793 mini = 0;
3794 maxi = prec;
3795 if (TREE_CODE (arg) == SSA_NAME)
3797 value_range_t *vr0 = get_value_range (arg);
3798 /* If arg is non-zero, then ffs or popcount
3799 are non-zero. */
3800 if (((vr0->type == VR_RANGE
3801 && range_includes_zero_p (vr0->min, vr0->max) == 0)
3802 || (vr0->type == VR_ANTI_RANGE
3803 && range_includes_zero_p (vr0->min, vr0->max) == 1))
3804 && !is_overflow_infinity (vr0->min)
3805 && !is_overflow_infinity (vr0->max))
3806 mini = 1;
3807 /* If some high bits are known to be zero,
3808 we can decrease the maximum. */
3809 if (vr0->type == VR_RANGE
3810 && TREE_CODE (vr0->max) == INTEGER_CST
3811 && !operand_less_p (vr0->min,
3812 build_zero_cst (TREE_TYPE (vr0->min)))
3813 && !is_overflow_infinity (vr0->max))
3814 maxi = tree_floor_log2 (vr0->max) + 1;
3816 goto bitop_builtin;
3817 /* __builtin_parity* returns [0, 1]. */
3818 CASE_INT_FN (BUILT_IN_PARITY):
3819 mini = 0;
3820 maxi = 1;
3821 goto bitop_builtin;
3822 /* __builtin_c[lt]z* return [0, prec-1], except for
3823 when the argument is 0, but that is undefined behavior.
3824 On many targets where the CLZ RTL or optab value is defined
3825 for 0 the value is prec, so include that in the range
3826 by default. */
3827 CASE_INT_FN (BUILT_IN_CLZ):
3828 arg = gimple_call_arg (stmt, 0);
3829 prec = TYPE_PRECISION (TREE_TYPE (arg));
3830 mini = 0;
3831 maxi = prec;
3832 if (optab_handler (clz_optab, TYPE_MODE (TREE_TYPE (arg)))
3833 != CODE_FOR_nothing
3834 && CLZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (TREE_TYPE (arg)),
3835 zerov)
3836 /* Handle only the single common value. */
3837 && zerov != prec)
3838 /* Magic value to give up, unless vr0 proves
3839 arg is non-zero. */
3840 mini = -2;
3841 if (TREE_CODE (arg) == SSA_NAME)
3843 value_range_t *vr0 = get_value_range (arg);
3844 /* From clz of VR_RANGE minimum we can compute
3845 result maximum. */
3846 if (vr0->type == VR_RANGE
3847 && TREE_CODE (vr0->min) == INTEGER_CST
3848 && !is_overflow_infinity (vr0->min))
3850 maxi = prec - 1 - tree_floor_log2 (vr0->min);
3851 if (maxi != prec)
3852 mini = 0;
3854 else if (vr0->type == VR_ANTI_RANGE
3855 && integer_zerop (vr0->min)
3856 && !is_overflow_infinity (vr0->min))
3858 maxi = prec - 1;
3859 mini = 0;
3861 if (mini == -2)
3862 break;
3863 /* From clz of VR_RANGE maximum we can compute
3864 result minimum. */
3865 if (vr0->type == VR_RANGE
3866 && TREE_CODE (vr0->max) == INTEGER_CST
3867 && !is_overflow_infinity (vr0->max))
3869 mini = prec - 1 - tree_floor_log2 (vr0->max);
3870 if (mini == prec)
3871 break;
3874 if (mini == -2)
3875 break;
3876 goto bitop_builtin;
3877 /* __builtin_ctz* return [0, prec-1], except for
3878 when the argument is 0, but that is undefined behavior.
3879 If there is a ctz optab for this mode and
3880 CTZ_DEFINED_VALUE_AT_ZERO, include that in the range,
3881 otherwise just assume 0 won't be seen. */
3882 CASE_INT_FN (BUILT_IN_CTZ):
3883 arg = gimple_call_arg (stmt, 0);
3884 prec = TYPE_PRECISION (TREE_TYPE (arg));
3885 mini = 0;
3886 maxi = prec - 1;
3887 if (optab_handler (ctz_optab, TYPE_MODE (TREE_TYPE (arg)))
3888 != CODE_FOR_nothing
3889 && CTZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (TREE_TYPE (arg)),
3890 zerov))
3892 /* Handle only the two common values. */
3893 if (zerov == -1)
3894 mini = -1;
3895 else if (zerov == prec)
3896 maxi = prec;
3897 else
3898 /* Magic value to give up, unless vr0 proves
3899 arg is non-zero. */
3900 mini = -2;
3902 if (TREE_CODE (arg) == SSA_NAME)
3904 value_range_t *vr0 = get_value_range (arg);
3905 /* If arg is non-zero, then use [0, prec - 1]. */
3906 if (((vr0->type == VR_RANGE
3907 && integer_nonzerop (vr0->min))
3908 || (vr0->type == VR_ANTI_RANGE
3909 && integer_zerop (vr0->min)))
3910 && !is_overflow_infinity (vr0->min))
3912 mini = 0;
3913 maxi = prec - 1;
3915 /* If some high bits are known to be zero,
3916 we can decrease the result maximum. */
3917 if (vr0->type == VR_RANGE
3918 && TREE_CODE (vr0->max) == INTEGER_CST
3919 && !is_overflow_infinity (vr0->max))
3921 maxi = tree_floor_log2 (vr0->max);
3922 /* For vr0 [0, 0] give up. */
3923 if (maxi == -1)
3924 break;
3927 if (mini == -2)
3928 break;
3929 goto bitop_builtin;
3930 /* __builtin_clrsb* returns [0, prec-1]. */
3931 CASE_INT_FN (BUILT_IN_CLRSB):
3932 arg = gimple_call_arg (stmt, 0);
3933 prec = TYPE_PRECISION (TREE_TYPE (arg));
3934 mini = 0;
3935 maxi = prec - 1;
3936 goto bitop_builtin;
3937 bitop_builtin:
3938 set_value_range (vr, VR_RANGE, build_int_cst (type, mini),
3939 build_int_cst (type, maxi), NULL);
3940 return;
3941 default:
3942 break;
3945 else if (is_gimple_call (stmt)
3946 && gimple_call_internal_p (stmt))
3948 enum tree_code subcode = ERROR_MARK;
3949 switch (gimple_call_internal_fn (stmt))
3951 case IFN_UBSAN_CHECK_ADD:
3952 subcode = PLUS_EXPR;
3953 break;
3954 case IFN_UBSAN_CHECK_SUB:
3955 subcode = MINUS_EXPR;
3956 break;
3957 case IFN_UBSAN_CHECK_MUL:
3958 subcode = MULT_EXPR;
3959 break;
3960 default:
3961 break;
3963 if (subcode != ERROR_MARK)
3965 bool saved_flag_wrapv = flag_wrapv;
3966 /* Pretend the arithmetics is wrapping. If there is
3967 any overflow, we'll complain, but will actually do
3968 wrapping operation. */
3969 flag_wrapv = 1;
3970 extract_range_from_binary_expr (vr, subcode, type,
3971 gimple_call_arg (stmt, 0),
3972 gimple_call_arg (stmt, 1));
3973 flag_wrapv = saved_flag_wrapv;
3975 /* If for both arguments vrp_valueize returned non-NULL,
3976 this should have been already folded and if not, it
3977 wasn't folded because of overflow. Avoid removing the
3978 UBSAN_CHECK_* calls in that case. */
3979 if (vr->type == VR_RANGE
3980 && (vr->min == vr->max
3981 || operand_equal_p (vr->min, vr->max, 0)))
3982 set_value_range_to_varying (vr);
3983 return;
3986 if (INTEGRAL_TYPE_P (type)
3987 && gimple_stmt_nonnegative_warnv_p (stmt, &sop))
3988 set_value_range_to_nonnegative (vr, type,
3989 sop || stmt_overflow_infinity (stmt));
3990 else if (vrp_stmt_computes_nonzero (stmt, &sop)
3991 && !sop)
3992 set_value_range_to_nonnull (vr, type);
3993 else
3994 set_value_range_to_varying (vr);
3998 /* Try to compute a useful range out of assignment STMT and store it
3999 in *VR. */
4001 static void
4002 extract_range_from_assignment (value_range_t *vr, gimple stmt)
4004 enum tree_code code = gimple_assign_rhs_code (stmt);
4006 if (code == ASSERT_EXPR)
4007 extract_range_from_assert (vr, gimple_assign_rhs1 (stmt));
4008 else if (code == SSA_NAME)
4009 extract_range_from_ssa_name (vr, gimple_assign_rhs1 (stmt));
4010 else if (TREE_CODE_CLASS (code) == tcc_binary)
4011 extract_range_from_binary_expr (vr, gimple_assign_rhs_code (stmt),
4012 gimple_expr_type (stmt),
4013 gimple_assign_rhs1 (stmt),
4014 gimple_assign_rhs2 (stmt));
4015 else if (TREE_CODE_CLASS (code) == tcc_unary)
4016 extract_range_from_unary_expr (vr, gimple_assign_rhs_code (stmt),
4017 gimple_expr_type (stmt),
4018 gimple_assign_rhs1 (stmt));
4019 else if (code == COND_EXPR)
4020 extract_range_from_cond_expr (vr, stmt);
4021 else if (TREE_CODE_CLASS (code) == tcc_comparison)
4022 extract_range_from_comparison (vr, gimple_assign_rhs_code (stmt),
4023 gimple_expr_type (stmt),
4024 gimple_assign_rhs1 (stmt),
4025 gimple_assign_rhs2 (stmt));
4026 else if (get_gimple_rhs_class (code) == GIMPLE_SINGLE_RHS
4027 && is_gimple_min_invariant (gimple_assign_rhs1 (stmt)))
4028 set_value_range_to_value (vr, gimple_assign_rhs1 (stmt), NULL);
4029 else
4030 set_value_range_to_varying (vr);
4032 if (vr->type == VR_VARYING)
4033 extract_range_basic (vr, stmt);
4036 /* Given a range VR, a LOOP and a variable VAR, determine whether it
4037 would be profitable to adjust VR using scalar evolution information
4038 for VAR. If so, update VR with the new limits. */
4040 static void
4041 adjust_range_with_scev (value_range_t *vr, struct loop *loop,
4042 gimple stmt, tree var)
4044 tree init, step, chrec, tmin, tmax, min, max, type, tem;
4045 enum ev_direction dir;
4047 /* TODO. Don't adjust anti-ranges. An anti-range may provide
4048 better opportunities than a regular range, but I'm not sure. */
4049 if (vr->type == VR_ANTI_RANGE)
4050 return;
4052 chrec = instantiate_parameters (loop, analyze_scalar_evolution (loop, var));
4054 /* Like in PR19590, scev can return a constant function. */
4055 if (is_gimple_min_invariant (chrec))
4057 set_value_range_to_value (vr, chrec, vr->equiv);
4058 return;
4061 if (TREE_CODE (chrec) != POLYNOMIAL_CHREC)
4062 return;
4064 init = initial_condition_in_loop_num (chrec, loop->num);
4065 tem = op_with_constant_singleton_value_range (init);
4066 if (tem)
4067 init = tem;
4068 step = evolution_part_in_loop_num (chrec, loop->num);
4069 tem = op_with_constant_singleton_value_range (step);
4070 if (tem)
4071 step = tem;
4073 /* If STEP is symbolic, we can't know whether INIT will be the
4074 minimum or maximum value in the range. Also, unless INIT is
4075 a simple expression, compare_values and possibly other functions
4076 in tree-vrp won't be able to handle it. */
4077 if (step == NULL_TREE
4078 || !is_gimple_min_invariant (step)
4079 || !valid_value_p (init))
4080 return;
4082 dir = scev_direction (chrec);
4083 if (/* Do not adjust ranges if we do not know whether the iv increases
4084 or decreases, ... */
4085 dir == EV_DIR_UNKNOWN
4086 /* ... or if it may wrap. */
4087 || scev_probably_wraps_p (init, step, stmt, get_chrec_loop (chrec),
4088 true))
4089 return;
4091 /* We use TYPE_MIN_VALUE and TYPE_MAX_VALUE here instead of
4092 negative_overflow_infinity and positive_overflow_infinity,
4093 because we have concluded that the loop probably does not
4094 wrap. */
4096 type = TREE_TYPE (var);
4097 if (POINTER_TYPE_P (type) || !TYPE_MIN_VALUE (type))
4098 tmin = lower_bound_in_type (type, type);
4099 else
4100 tmin = TYPE_MIN_VALUE (type);
4101 if (POINTER_TYPE_P (type) || !TYPE_MAX_VALUE (type))
4102 tmax = upper_bound_in_type (type, type);
4103 else
4104 tmax = TYPE_MAX_VALUE (type);
4106 /* Try to use estimated number of iterations for the loop to constrain the
4107 final value in the evolution. */
4108 if (TREE_CODE (step) == INTEGER_CST
4109 && is_gimple_val (init)
4110 && (TREE_CODE (init) != SSA_NAME
4111 || get_value_range (init)->type == VR_RANGE))
4113 widest_int nit;
4115 /* We are only entering here for loop header PHI nodes, so using
4116 the number of latch executions is the correct thing to use. */
4117 if (max_loop_iterations (loop, &nit))
4119 value_range_t maxvr = VR_INITIALIZER;
4120 signop sgn = TYPE_SIGN (TREE_TYPE (step));
4121 bool overflow;
4123 widest_int wtmp = wi::mul (wi::to_widest (step), nit, sgn,
4124 &overflow);
4125 /* If the multiplication overflowed we can't do a meaningful
4126 adjustment. Likewise if the result doesn't fit in the type
4127 of the induction variable. For a signed type we have to
4128 check whether the result has the expected signedness which
4129 is that of the step as number of iterations is unsigned. */
4130 if (!overflow
4131 && wi::fits_to_tree_p (wtmp, TREE_TYPE (init))
4132 && (sgn == UNSIGNED
4133 || wi::gts_p (wtmp, 0) == wi::gts_p (step, 0)))
4135 tem = wide_int_to_tree (TREE_TYPE (init), wtmp);
4136 extract_range_from_binary_expr (&maxvr, PLUS_EXPR,
4137 TREE_TYPE (init), init, tem);
4138 /* Likewise if the addition did. */
4139 if (maxvr.type == VR_RANGE)
4141 tmin = maxvr.min;
4142 tmax = maxvr.max;
4148 if (vr->type == VR_VARYING || vr->type == VR_UNDEFINED)
4150 min = tmin;
4151 max = tmax;
4153 /* For VARYING or UNDEFINED ranges, just about anything we get
4154 from scalar evolutions should be better. */
4156 if (dir == EV_DIR_DECREASES)
4157 max = init;
4158 else
4159 min = init;
4161 else if (vr->type == VR_RANGE)
4163 min = vr->min;
4164 max = vr->max;
4166 if (dir == EV_DIR_DECREASES)
4168 /* INIT is the maximum value. If INIT is lower than VR->MAX
4169 but no smaller than VR->MIN, set VR->MAX to INIT. */
4170 if (compare_values (init, max) == -1)
4171 max = init;
4173 /* According to the loop information, the variable does not
4174 overflow. If we think it does, probably because of an
4175 overflow due to arithmetic on a different INF value,
4176 reset now. */
4177 if (is_negative_overflow_infinity (min)
4178 || compare_values (min, tmin) == -1)
4179 min = tmin;
4182 else
4184 /* If INIT is bigger than VR->MIN, set VR->MIN to INIT. */
4185 if (compare_values (init, min) == 1)
4186 min = init;
4188 if (is_positive_overflow_infinity (max)
4189 || compare_values (tmax, max) == -1)
4190 max = tmax;
4193 else
4194 return;
4196 /* If we just created an invalid range with the minimum
4197 greater than the maximum, we fail conservatively.
4198 This should happen only in unreachable
4199 parts of code, or for invalid programs. */
4200 if (compare_values (min, max) == 1
4201 || (is_negative_overflow_infinity (min)
4202 && is_positive_overflow_infinity (max)))
4203 return;
4205 set_value_range (vr, VR_RANGE, min, max, vr->equiv);
4209 /* Given two numeric value ranges VR0, VR1 and a comparison code COMP:
4211 - Return BOOLEAN_TRUE_NODE if VR0 COMP VR1 always returns true for
4212 all the values in the ranges.
4214 - Return BOOLEAN_FALSE_NODE if the comparison always returns false.
4216 - Return NULL_TREE if it is not always possible to determine the
4217 value of the comparison.
4219 Also set *STRICT_OVERFLOW_P to indicate whether a range with an
4220 overflow infinity was used in the test. */
4223 static tree
4224 compare_ranges (enum tree_code comp, value_range_t *vr0, value_range_t *vr1,
4225 bool *strict_overflow_p)
4227 /* VARYING or UNDEFINED ranges cannot be compared. */
4228 if (vr0->type == VR_VARYING
4229 || vr0->type == VR_UNDEFINED
4230 || vr1->type == VR_VARYING
4231 || vr1->type == VR_UNDEFINED)
4232 return NULL_TREE;
4234 /* Anti-ranges need to be handled separately. */
4235 if (vr0->type == VR_ANTI_RANGE || vr1->type == VR_ANTI_RANGE)
4237 /* If both are anti-ranges, then we cannot compute any
4238 comparison. */
4239 if (vr0->type == VR_ANTI_RANGE && vr1->type == VR_ANTI_RANGE)
4240 return NULL_TREE;
4242 /* These comparisons are never statically computable. */
4243 if (comp == GT_EXPR
4244 || comp == GE_EXPR
4245 || comp == LT_EXPR
4246 || comp == LE_EXPR)
4247 return NULL_TREE;
4249 /* Equality can be computed only between a range and an
4250 anti-range. ~[VAL1, VAL2] == [VAL1, VAL2] is always false. */
4251 if (vr0->type == VR_RANGE)
4253 /* To simplify processing, make VR0 the anti-range. */
4254 value_range_t *tmp = vr0;
4255 vr0 = vr1;
4256 vr1 = tmp;
4259 gcc_assert (comp == NE_EXPR || comp == EQ_EXPR);
4261 if (compare_values_warnv (vr0->min, vr1->min, strict_overflow_p) == 0
4262 && compare_values_warnv (vr0->max, vr1->max, strict_overflow_p) == 0)
4263 return (comp == NE_EXPR) ? boolean_true_node : boolean_false_node;
4265 return NULL_TREE;
4268 if (!usable_range_p (vr0, strict_overflow_p)
4269 || !usable_range_p (vr1, strict_overflow_p))
4270 return NULL_TREE;
4272 /* Simplify processing. If COMP is GT_EXPR or GE_EXPR, switch the
4273 operands around and change the comparison code. */
4274 if (comp == GT_EXPR || comp == GE_EXPR)
4276 value_range_t *tmp;
4277 comp = (comp == GT_EXPR) ? LT_EXPR : LE_EXPR;
4278 tmp = vr0;
4279 vr0 = vr1;
4280 vr1 = tmp;
4283 if (comp == EQ_EXPR)
4285 /* Equality may only be computed if both ranges represent
4286 exactly one value. */
4287 if (compare_values_warnv (vr0->min, vr0->max, strict_overflow_p) == 0
4288 && compare_values_warnv (vr1->min, vr1->max, strict_overflow_p) == 0)
4290 int cmp_min = compare_values_warnv (vr0->min, vr1->min,
4291 strict_overflow_p);
4292 int cmp_max = compare_values_warnv (vr0->max, vr1->max,
4293 strict_overflow_p);
4294 if (cmp_min == 0 && cmp_max == 0)
4295 return boolean_true_node;
4296 else if (cmp_min != -2 && cmp_max != -2)
4297 return boolean_false_node;
4299 /* If [V0_MIN, V1_MAX] < [V1_MIN, V1_MAX] then V0 != V1. */
4300 else if (compare_values_warnv (vr0->min, vr1->max,
4301 strict_overflow_p) == 1
4302 || compare_values_warnv (vr1->min, vr0->max,
4303 strict_overflow_p) == 1)
4304 return boolean_false_node;
4306 return NULL_TREE;
4308 else if (comp == NE_EXPR)
4310 int cmp1, cmp2;
4312 /* If VR0 is completely to the left or completely to the right
4313 of VR1, they are always different. Notice that we need to
4314 make sure that both comparisons yield similar results to
4315 avoid comparing values that cannot be compared at
4316 compile-time. */
4317 cmp1 = compare_values_warnv (vr0->max, vr1->min, strict_overflow_p);
4318 cmp2 = compare_values_warnv (vr0->min, vr1->max, strict_overflow_p);
4319 if ((cmp1 == -1 && cmp2 == -1) || (cmp1 == 1 && cmp2 == 1))
4320 return boolean_true_node;
4322 /* If VR0 and VR1 represent a single value and are identical,
4323 return false. */
4324 else if (compare_values_warnv (vr0->min, vr0->max,
4325 strict_overflow_p) == 0
4326 && compare_values_warnv (vr1->min, vr1->max,
4327 strict_overflow_p) == 0
4328 && compare_values_warnv (vr0->min, vr1->min,
4329 strict_overflow_p) == 0
4330 && compare_values_warnv (vr0->max, vr1->max,
4331 strict_overflow_p) == 0)
4332 return boolean_false_node;
4334 /* Otherwise, they may or may not be different. */
4335 else
4336 return NULL_TREE;
4338 else if (comp == LT_EXPR || comp == LE_EXPR)
4340 int tst;
4342 /* If VR0 is to the left of VR1, return true. */
4343 tst = compare_values_warnv (vr0->max, vr1->min, strict_overflow_p);
4344 if ((comp == LT_EXPR && tst == -1)
4345 || (comp == LE_EXPR && (tst == -1 || tst == 0)))
4347 if (overflow_infinity_range_p (vr0)
4348 || overflow_infinity_range_p (vr1))
4349 *strict_overflow_p = true;
4350 return boolean_true_node;
4353 /* If VR0 is to the right of VR1, return false. */
4354 tst = compare_values_warnv (vr0->min, vr1->max, strict_overflow_p);
4355 if ((comp == LT_EXPR && (tst == 0 || tst == 1))
4356 || (comp == LE_EXPR && tst == 1))
4358 if (overflow_infinity_range_p (vr0)
4359 || overflow_infinity_range_p (vr1))
4360 *strict_overflow_p = true;
4361 return boolean_false_node;
4364 /* Otherwise, we don't know. */
4365 return NULL_TREE;
4368 gcc_unreachable ();
4372 /* Given a value range VR, a value VAL and a comparison code COMP, return
4373 BOOLEAN_TRUE_NODE if VR COMP VAL always returns true for all the
4374 values in VR. Return BOOLEAN_FALSE_NODE if the comparison
4375 always returns false. Return NULL_TREE if it is not always
4376 possible to determine the value of the comparison. Also set
4377 *STRICT_OVERFLOW_P to indicate whether a range with an overflow
4378 infinity was used in the test. */
4380 static tree
4381 compare_range_with_value (enum tree_code comp, value_range_t *vr, tree val,
4382 bool *strict_overflow_p)
4384 if (vr->type == VR_VARYING || vr->type == VR_UNDEFINED)
4385 return NULL_TREE;
4387 /* Anti-ranges need to be handled separately. */
4388 if (vr->type == VR_ANTI_RANGE)
4390 /* For anti-ranges, the only predicates that we can compute at
4391 compile time are equality and inequality. */
4392 if (comp == GT_EXPR
4393 || comp == GE_EXPR
4394 || comp == LT_EXPR
4395 || comp == LE_EXPR)
4396 return NULL_TREE;
4398 /* ~[VAL_1, VAL_2] OP VAL is known if VAL_1 <= VAL <= VAL_2. */
4399 if (value_inside_range (val, vr->min, vr->max) == 1)
4400 return (comp == NE_EXPR) ? boolean_true_node : boolean_false_node;
4402 return NULL_TREE;
4405 if (!usable_range_p (vr, strict_overflow_p))
4406 return NULL_TREE;
4408 if (comp == EQ_EXPR)
4410 /* EQ_EXPR may only be computed if VR represents exactly
4411 one value. */
4412 if (compare_values_warnv (vr->min, vr->max, strict_overflow_p) == 0)
4414 int cmp = compare_values_warnv (vr->min, val, strict_overflow_p);
4415 if (cmp == 0)
4416 return boolean_true_node;
4417 else if (cmp == -1 || cmp == 1 || cmp == 2)
4418 return boolean_false_node;
4420 else if (compare_values_warnv (val, vr->min, strict_overflow_p) == -1
4421 || compare_values_warnv (vr->max, val, strict_overflow_p) == -1)
4422 return boolean_false_node;
4424 return NULL_TREE;
4426 else if (comp == NE_EXPR)
4428 /* If VAL is not inside VR, then they are always different. */
4429 if (compare_values_warnv (vr->max, val, strict_overflow_p) == -1
4430 || compare_values_warnv (vr->min, val, strict_overflow_p) == 1)
4431 return boolean_true_node;
4433 /* If VR represents exactly one value equal to VAL, then return
4434 false. */
4435 if (compare_values_warnv (vr->min, vr->max, strict_overflow_p) == 0
4436 && compare_values_warnv (vr->min, val, strict_overflow_p) == 0)
4437 return boolean_false_node;
4439 /* Otherwise, they may or may not be different. */
4440 return NULL_TREE;
4442 else if (comp == LT_EXPR || comp == LE_EXPR)
4444 int tst;
4446 /* If VR is to the left of VAL, return true. */
4447 tst = compare_values_warnv (vr->max, val, strict_overflow_p);
4448 if ((comp == LT_EXPR && tst == -1)
4449 || (comp == LE_EXPR && (tst == -1 || tst == 0)))
4451 if (overflow_infinity_range_p (vr))
4452 *strict_overflow_p = true;
4453 return boolean_true_node;
4456 /* If VR is to the right of VAL, return false. */
4457 tst = compare_values_warnv (vr->min, val, strict_overflow_p);
4458 if ((comp == LT_EXPR && (tst == 0 || tst == 1))
4459 || (comp == LE_EXPR && tst == 1))
4461 if (overflow_infinity_range_p (vr))
4462 *strict_overflow_p = true;
4463 return boolean_false_node;
4466 /* Otherwise, we don't know. */
4467 return NULL_TREE;
4469 else if (comp == GT_EXPR || comp == GE_EXPR)
4471 int tst;
4473 /* If VR is to the right of VAL, return true. */
4474 tst = compare_values_warnv (vr->min, val, strict_overflow_p);
4475 if ((comp == GT_EXPR && tst == 1)
4476 || (comp == GE_EXPR && (tst == 0 || tst == 1)))
4478 if (overflow_infinity_range_p (vr))
4479 *strict_overflow_p = true;
4480 return boolean_true_node;
4483 /* If VR is to the left of VAL, return false. */
4484 tst = compare_values_warnv (vr->max, val, strict_overflow_p);
4485 if ((comp == GT_EXPR && (tst == -1 || tst == 0))
4486 || (comp == GE_EXPR && tst == -1))
4488 if (overflow_infinity_range_p (vr))
4489 *strict_overflow_p = true;
4490 return boolean_false_node;
4493 /* Otherwise, we don't know. */
4494 return NULL_TREE;
4497 gcc_unreachable ();
4501 /* Debugging dumps. */
4503 void dump_value_range (FILE *, value_range_t *);
4504 void debug_value_range (value_range_t *);
4505 void dump_all_value_ranges (FILE *);
4506 void debug_all_value_ranges (void);
4507 void dump_vr_equiv (FILE *, bitmap);
4508 void debug_vr_equiv (bitmap);
4511 /* Dump value range VR to FILE. */
4513 void
4514 dump_value_range (FILE *file, value_range_t *vr)
4516 if (vr == NULL)
4517 fprintf (file, "[]");
4518 else if (vr->type == VR_UNDEFINED)
4519 fprintf (file, "UNDEFINED");
4520 else if (vr->type == VR_RANGE || vr->type == VR_ANTI_RANGE)
4522 tree type = TREE_TYPE (vr->min);
4524 fprintf (file, "%s[", (vr->type == VR_ANTI_RANGE) ? "~" : "");
4526 if (is_negative_overflow_infinity (vr->min))
4527 fprintf (file, "-INF(OVF)");
4528 else if (INTEGRAL_TYPE_P (type)
4529 && !TYPE_UNSIGNED (type)
4530 && vrp_val_is_min (vr->min))
4531 fprintf (file, "-INF");
4532 else
4533 print_generic_expr (file, vr->min, 0);
4535 fprintf (file, ", ");
4537 if (is_positive_overflow_infinity (vr->max))
4538 fprintf (file, "+INF(OVF)");
4539 else if (INTEGRAL_TYPE_P (type)
4540 && vrp_val_is_max (vr->max))
4541 fprintf (file, "+INF");
4542 else
4543 print_generic_expr (file, vr->max, 0);
4545 fprintf (file, "]");
4547 if (vr->equiv)
4549 bitmap_iterator bi;
4550 unsigned i, c = 0;
4552 fprintf (file, " EQUIVALENCES: { ");
4554 EXECUTE_IF_SET_IN_BITMAP (vr->equiv, 0, i, bi)
4556 print_generic_expr (file, ssa_name (i), 0);
4557 fprintf (file, " ");
4558 c++;
4561 fprintf (file, "} (%u elements)", c);
4564 else if (vr->type == VR_VARYING)
4565 fprintf (file, "VARYING");
4566 else
4567 fprintf (file, "INVALID RANGE");
4571 /* Dump value range VR to stderr. */
4573 DEBUG_FUNCTION void
4574 debug_value_range (value_range_t *vr)
4576 dump_value_range (stderr, vr);
4577 fprintf (stderr, "\n");
4581 /* Dump value ranges of all SSA_NAMEs to FILE. */
4583 void
4584 dump_all_value_ranges (FILE *file)
4586 size_t i;
4588 for (i = 0; i < num_vr_values; i++)
4590 if (vr_value[i])
4592 print_generic_expr (file, ssa_name (i), 0);
4593 fprintf (file, ": ");
4594 dump_value_range (file, vr_value[i]);
4595 fprintf (file, "\n");
4599 fprintf (file, "\n");
4603 /* Dump all value ranges to stderr. */
4605 DEBUG_FUNCTION void
4606 debug_all_value_ranges (void)
4608 dump_all_value_ranges (stderr);
4612 /* Given a COND_EXPR COND of the form 'V OP W', and an SSA name V,
4613 create a new SSA name N and return the assertion assignment
4614 'N = ASSERT_EXPR <V, V OP W>'. */
4616 static gimple
4617 build_assert_expr_for (tree cond, tree v)
4619 tree a;
4620 gimple assertion;
4622 gcc_assert (TREE_CODE (v) == SSA_NAME
4623 && COMPARISON_CLASS_P (cond));
4625 a = build2 (ASSERT_EXPR, TREE_TYPE (v), v, cond);
4626 assertion = gimple_build_assign (NULL_TREE, a);
4628 /* The new ASSERT_EXPR, creates a new SSA name that replaces the
4629 operand of the ASSERT_EXPR. Create it so the new name and the old one
4630 are registered in the replacement table so that we can fix the SSA web
4631 after adding all the ASSERT_EXPRs. */
4632 create_new_def_for (v, assertion, NULL);
4634 return assertion;
4638 /* Return false if EXPR is a predicate expression involving floating
4639 point values. */
4641 static inline bool
4642 fp_predicate (gimple stmt)
4644 GIMPLE_CHECK (stmt, GIMPLE_COND);
4646 return FLOAT_TYPE_P (TREE_TYPE (gimple_cond_lhs (stmt)));
4649 /* If the range of values taken by OP can be inferred after STMT executes,
4650 return the comparison code (COMP_CODE_P) and value (VAL_P) that
4651 describes the inferred range. Return true if a range could be
4652 inferred. */
4654 static bool
4655 infer_value_range (gimple stmt, tree op, enum tree_code *comp_code_p, tree *val_p)
4657 *val_p = NULL_TREE;
4658 *comp_code_p = ERROR_MARK;
4660 /* Do not attempt to infer anything in names that flow through
4661 abnormal edges. */
4662 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op))
4663 return false;
4665 /* Similarly, don't infer anything from statements that may throw
4666 exceptions. ??? Relax this requirement? */
4667 if (stmt_could_throw_p (stmt))
4668 return false;
4670 /* If STMT is the last statement of a basic block with no normal
4671 successors, there is no point inferring anything about any of its
4672 operands. We would not be able to find a proper insertion point
4673 for the assertion, anyway. */
4674 if (stmt_ends_bb_p (stmt))
4676 edge_iterator ei;
4677 edge e;
4679 FOR_EACH_EDGE (e, ei, gimple_bb (stmt)->succs)
4680 if (!(e->flags & EDGE_ABNORMAL))
4681 break;
4682 if (e == NULL)
4683 return false;
4686 if (infer_nonnull_range (stmt, op, true, true))
4688 *val_p = build_int_cst (TREE_TYPE (op), 0);
4689 *comp_code_p = NE_EXPR;
4690 return true;
4693 return false;
4697 void dump_asserts_for (FILE *, tree);
4698 void debug_asserts_for (tree);
4699 void dump_all_asserts (FILE *);
4700 void debug_all_asserts (void);
4702 /* Dump all the registered assertions for NAME to FILE. */
4704 void
4705 dump_asserts_for (FILE *file, tree name)
4707 assert_locus_t loc;
4709 fprintf (file, "Assertions to be inserted for ");
4710 print_generic_expr (file, name, 0);
4711 fprintf (file, "\n");
4713 loc = asserts_for[SSA_NAME_VERSION (name)];
4714 while (loc)
4716 fprintf (file, "\t");
4717 print_gimple_stmt (file, gsi_stmt (loc->si), 0, 0);
4718 fprintf (file, "\n\tBB #%d", loc->bb->index);
4719 if (loc->e)
4721 fprintf (file, "\n\tEDGE %d->%d", loc->e->src->index,
4722 loc->e->dest->index);
4723 dump_edge_info (file, loc->e, dump_flags, 0);
4725 fprintf (file, "\n\tPREDICATE: ");
4726 print_generic_expr (file, name, 0);
4727 fprintf (file, " %s ", get_tree_code_name (loc->comp_code));
4728 print_generic_expr (file, loc->val, 0);
4729 fprintf (file, "\n\n");
4730 loc = loc->next;
4733 fprintf (file, "\n");
4737 /* Dump all the registered assertions for NAME to stderr. */
4739 DEBUG_FUNCTION void
4740 debug_asserts_for (tree name)
4742 dump_asserts_for (stderr, name);
4746 /* Dump all the registered assertions for all the names to FILE. */
4748 void
4749 dump_all_asserts (FILE *file)
4751 unsigned i;
4752 bitmap_iterator bi;
4754 fprintf (file, "\nASSERT_EXPRs to be inserted\n\n");
4755 EXECUTE_IF_SET_IN_BITMAP (need_assert_for, 0, i, bi)
4756 dump_asserts_for (file, ssa_name (i));
4757 fprintf (file, "\n");
4761 /* Dump all the registered assertions for all the names to stderr. */
4763 DEBUG_FUNCTION void
4764 debug_all_asserts (void)
4766 dump_all_asserts (stderr);
4770 /* If NAME doesn't have an ASSERT_EXPR registered for asserting
4771 'EXPR COMP_CODE VAL' at a location that dominates block BB or
4772 E->DEST, then register this location as a possible insertion point
4773 for ASSERT_EXPR <NAME, EXPR COMP_CODE VAL>.
4775 BB, E and SI provide the exact insertion point for the new
4776 ASSERT_EXPR. If BB is NULL, then the ASSERT_EXPR is to be inserted
4777 on edge E. Otherwise, if E is NULL, the ASSERT_EXPR is inserted on
4778 BB. If SI points to a COND_EXPR or a SWITCH_EXPR statement, then E
4779 must not be NULL. */
4781 static void
4782 register_new_assert_for (tree name, tree expr,
4783 enum tree_code comp_code,
4784 tree val,
4785 basic_block bb,
4786 edge e,
4787 gimple_stmt_iterator si)
4789 assert_locus_t n, loc, last_loc;
4790 basic_block dest_bb;
4792 gcc_checking_assert (bb == NULL || e == NULL);
4794 if (e == NULL)
4795 gcc_checking_assert (gimple_code (gsi_stmt (si)) != GIMPLE_COND
4796 && gimple_code (gsi_stmt (si)) != GIMPLE_SWITCH);
4798 /* Never build an assert comparing against an integer constant with
4799 TREE_OVERFLOW set. This confuses our undefined overflow warning
4800 machinery. */
4801 if (TREE_OVERFLOW_P (val))
4802 val = drop_tree_overflow (val);
4804 /* The new assertion A will be inserted at BB or E. We need to
4805 determine if the new location is dominated by a previously
4806 registered location for A. If we are doing an edge insertion,
4807 assume that A will be inserted at E->DEST. Note that this is not
4808 necessarily true.
4810 If E is a critical edge, it will be split. But even if E is
4811 split, the new block will dominate the same set of blocks that
4812 E->DEST dominates.
4814 The reverse, however, is not true, blocks dominated by E->DEST
4815 will not be dominated by the new block created to split E. So,
4816 if the insertion location is on a critical edge, we will not use
4817 the new location to move another assertion previously registered
4818 at a block dominated by E->DEST. */
4819 dest_bb = (bb) ? bb : e->dest;
4821 /* If NAME already has an ASSERT_EXPR registered for COMP_CODE and
4822 VAL at a block dominating DEST_BB, then we don't need to insert a new
4823 one. Similarly, if the same assertion already exists at a block
4824 dominated by DEST_BB and the new location is not on a critical
4825 edge, then update the existing location for the assertion (i.e.,
4826 move the assertion up in the dominance tree).
4828 Note, this is implemented as a simple linked list because there
4829 should not be more than a handful of assertions registered per
4830 name. If this becomes a performance problem, a table hashed by
4831 COMP_CODE and VAL could be implemented. */
4832 loc = asserts_for[SSA_NAME_VERSION (name)];
4833 last_loc = loc;
4834 while (loc)
4836 if (loc->comp_code == comp_code
4837 && (loc->val == val
4838 || operand_equal_p (loc->val, val, 0))
4839 && (loc->expr == expr
4840 || operand_equal_p (loc->expr, expr, 0)))
4842 /* If E is not a critical edge and DEST_BB
4843 dominates the existing location for the assertion, move
4844 the assertion up in the dominance tree by updating its
4845 location information. */
4846 if ((e == NULL || !EDGE_CRITICAL_P (e))
4847 && dominated_by_p (CDI_DOMINATORS, loc->bb, dest_bb))
4849 loc->bb = dest_bb;
4850 loc->e = e;
4851 loc->si = si;
4852 return;
4856 /* Update the last node of the list and move to the next one. */
4857 last_loc = loc;
4858 loc = loc->next;
4861 /* If we didn't find an assertion already registered for
4862 NAME COMP_CODE VAL, add a new one at the end of the list of
4863 assertions associated with NAME. */
4864 n = XNEW (struct assert_locus_d);
4865 n->bb = dest_bb;
4866 n->e = e;
4867 n->si = si;
4868 n->comp_code = comp_code;
4869 n->val = val;
4870 n->expr = expr;
4871 n->next = NULL;
4873 if (last_loc)
4874 last_loc->next = n;
4875 else
4876 asserts_for[SSA_NAME_VERSION (name)] = n;
4878 bitmap_set_bit (need_assert_for, SSA_NAME_VERSION (name));
4881 /* (COND_OP0 COND_CODE COND_OP1) is a predicate which uses NAME.
4882 Extract a suitable test code and value and store them into *CODE_P and
4883 *VAL_P so the predicate is normalized to NAME *CODE_P *VAL_P.
4885 If no extraction was possible, return FALSE, otherwise return TRUE.
4887 If INVERT is true, then we invert the result stored into *CODE_P. */
4889 static bool
4890 extract_code_and_val_from_cond_with_ops (tree name, enum tree_code cond_code,
4891 tree cond_op0, tree cond_op1,
4892 bool invert, enum tree_code *code_p,
4893 tree *val_p)
4895 enum tree_code comp_code;
4896 tree val;
4898 /* Otherwise, we have a comparison of the form NAME COMP VAL
4899 or VAL COMP NAME. */
4900 if (name == cond_op1)
4902 /* If the predicate is of the form VAL COMP NAME, flip
4903 COMP around because we need to register NAME as the
4904 first operand in the predicate. */
4905 comp_code = swap_tree_comparison (cond_code);
4906 val = cond_op0;
4908 else
4910 /* The comparison is of the form NAME COMP VAL, so the
4911 comparison code remains unchanged. */
4912 comp_code = cond_code;
4913 val = cond_op1;
4916 /* Invert the comparison code as necessary. */
4917 if (invert)
4918 comp_code = invert_tree_comparison (comp_code, 0);
4920 /* VRP does not handle float types. */
4921 if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (val)))
4922 return false;
4924 /* Do not register always-false predicates.
4925 FIXME: this works around a limitation in fold() when dealing with
4926 enumerations. Given 'enum { N1, N2 } x;', fold will not
4927 fold 'if (x > N2)' to 'if (0)'. */
4928 if ((comp_code == GT_EXPR || comp_code == LT_EXPR)
4929 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
4931 tree min = TYPE_MIN_VALUE (TREE_TYPE (val));
4932 tree max = TYPE_MAX_VALUE (TREE_TYPE (val));
4934 if (comp_code == GT_EXPR
4935 && (!max
4936 || compare_values (val, max) == 0))
4937 return false;
4939 if (comp_code == LT_EXPR
4940 && (!min
4941 || compare_values (val, min) == 0))
4942 return false;
4944 *code_p = comp_code;
4945 *val_p = val;
4946 return true;
4949 /* Find out smallest RES where RES > VAL && (RES & MASK) == RES, if any
4950 (otherwise return VAL). VAL and MASK must be zero-extended for
4951 precision PREC. If SGNBIT is non-zero, first xor VAL with SGNBIT
4952 (to transform signed values into unsigned) and at the end xor
4953 SGNBIT back. */
4955 static wide_int
4956 masked_increment (const wide_int &val_in, const wide_int &mask,
4957 const wide_int &sgnbit, unsigned int prec)
4959 wide_int bit = wi::one (prec), res;
4960 unsigned int i;
4962 wide_int val = val_in ^ sgnbit;
4963 for (i = 0; i < prec; i++, bit += bit)
4965 res = mask;
4966 if ((res & bit) == 0)
4967 continue;
4968 res = bit - 1;
4969 res = (val + bit).and_not (res);
4970 res &= mask;
4971 if (wi::gtu_p (res, val))
4972 return res ^ sgnbit;
4974 return val ^ sgnbit;
4977 /* Try to register an edge assertion for SSA name NAME on edge E for
4978 the condition COND contributing to the conditional jump pointed to by BSI.
4979 Invert the condition COND if INVERT is true.
4980 Return true if an assertion for NAME could be registered. */
4982 static bool
4983 register_edge_assert_for_2 (tree name, edge e, gimple_stmt_iterator bsi,
4984 enum tree_code cond_code,
4985 tree cond_op0, tree cond_op1, bool invert)
4987 tree val;
4988 enum tree_code comp_code;
4989 bool retval = false;
4991 if (!extract_code_and_val_from_cond_with_ops (name, cond_code,
4992 cond_op0,
4993 cond_op1,
4994 invert, &comp_code, &val))
4995 return false;
4997 /* Only register an ASSERT_EXPR if NAME was found in the sub-graph
4998 reachable from E. */
4999 if (live_on_edge (e, name)
5000 && !has_single_use (name))
5002 register_new_assert_for (name, name, comp_code, val, NULL, e, bsi);
5003 retval = true;
5006 /* In the case of NAME <= CST and NAME being defined as
5007 NAME = (unsigned) NAME2 + CST2 we can assert NAME2 >= -CST2
5008 and NAME2 <= CST - CST2. We can do the same for NAME > CST.
5009 This catches range and anti-range tests. */
5010 if ((comp_code == LE_EXPR
5011 || comp_code == GT_EXPR)
5012 && TREE_CODE (val) == INTEGER_CST
5013 && TYPE_UNSIGNED (TREE_TYPE (val)))
5015 gimple def_stmt = SSA_NAME_DEF_STMT (name);
5016 tree cst2 = NULL_TREE, name2 = NULL_TREE, name3 = NULL_TREE;
5018 /* Extract CST2 from the (optional) addition. */
5019 if (is_gimple_assign (def_stmt)
5020 && gimple_assign_rhs_code (def_stmt) == PLUS_EXPR)
5022 name2 = gimple_assign_rhs1 (def_stmt);
5023 cst2 = gimple_assign_rhs2 (def_stmt);
5024 if (TREE_CODE (name2) == SSA_NAME
5025 && TREE_CODE (cst2) == INTEGER_CST)
5026 def_stmt = SSA_NAME_DEF_STMT (name2);
5029 /* Extract NAME2 from the (optional) sign-changing cast. */
5030 if (gimple_assign_cast_p (def_stmt))
5032 if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt))
5033 && ! TYPE_UNSIGNED (TREE_TYPE (gimple_assign_rhs1 (def_stmt)))
5034 && (TYPE_PRECISION (gimple_expr_type (def_stmt))
5035 == TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (def_stmt)))))
5036 name3 = gimple_assign_rhs1 (def_stmt);
5039 /* If name3 is used later, create an ASSERT_EXPR for it. */
5040 if (name3 != NULL_TREE
5041 && TREE_CODE (name3) == SSA_NAME
5042 && (cst2 == NULL_TREE
5043 || TREE_CODE (cst2) == INTEGER_CST)
5044 && INTEGRAL_TYPE_P (TREE_TYPE (name3))
5045 && live_on_edge (e, name3)
5046 && !has_single_use (name3))
5048 tree tmp;
5050 /* Build an expression for the range test. */
5051 tmp = build1 (NOP_EXPR, TREE_TYPE (name), name3);
5052 if (cst2 != NULL_TREE)
5053 tmp = build2 (PLUS_EXPR, TREE_TYPE (name), tmp, cst2);
5055 if (dump_file)
5057 fprintf (dump_file, "Adding assert for ");
5058 print_generic_expr (dump_file, name3, 0);
5059 fprintf (dump_file, " from ");
5060 print_generic_expr (dump_file, tmp, 0);
5061 fprintf (dump_file, "\n");
5064 register_new_assert_for (name3, tmp, comp_code, val, NULL, e, bsi);
5066 retval = true;
5069 /* If name2 is used later, create an ASSERT_EXPR for it. */
5070 if (name2 != NULL_TREE
5071 && TREE_CODE (name2) == SSA_NAME
5072 && TREE_CODE (cst2) == INTEGER_CST
5073 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
5074 && live_on_edge (e, name2)
5075 && !has_single_use (name2))
5077 tree tmp;
5079 /* Build an expression for the range test. */
5080 tmp = name2;
5081 if (TREE_TYPE (name) != TREE_TYPE (name2))
5082 tmp = build1 (NOP_EXPR, TREE_TYPE (name), tmp);
5083 if (cst2 != NULL_TREE)
5084 tmp = build2 (PLUS_EXPR, TREE_TYPE (name), tmp, cst2);
5086 if (dump_file)
5088 fprintf (dump_file, "Adding assert for ");
5089 print_generic_expr (dump_file, name2, 0);
5090 fprintf (dump_file, " from ");
5091 print_generic_expr (dump_file, tmp, 0);
5092 fprintf (dump_file, "\n");
5095 register_new_assert_for (name2, tmp, comp_code, val, NULL, e, bsi);
5097 retval = true;
5101 /* In the case of post-in/decrement tests like if (i++) ... and uses
5102 of the in/decremented value on the edge the extra name we want to
5103 assert for is not on the def chain of the name compared. Instead
5104 it is in the set of use stmts. */
5105 if ((comp_code == NE_EXPR
5106 || comp_code == EQ_EXPR)
5107 && TREE_CODE (val) == INTEGER_CST)
5109 imm_use_iterator ui;
5110 gimple use_stmt;
5111 FOR_EACH_IMM_USE_STMT (use_stmt, ui, name)
5113 /* Cut off to use-stmts that are in the predecessor. */
5114 if (gimple_bb (use_stmt) != e->src)
5115 continue;
5117 if (!is_gimple_assign (use_stmt))
5118 continue;
5120 enum tree_code code = gimple_assign_rhs_code (use_stmt);
5121 if (code != PLUS_EXPR
5122 && code != MINUS_EXPR)
5123 continue;
5125 tree cst = gimple_assign_rhs2 (use_stmt);
5126 if (TREE_CODE (cst) != INTEGER_CST)
5127 continue;
5129 tree name2 = gimple_assign_lhs (use_stmt);
5130 if (live_on_edge (e, name2))
5132 cst = int_const_binop (code, val, cst);
5133 register_new_assert_for (name2, name2, comp_code, cst,
5134 NULL, e, bsi);
5135 retval = true;
5140 if (TREE_CODE_CLASS (comp_code) == tcc_comparison
5141 && TREE_CODE (val) == INTEGER_CST)
5143 gimple def_stmt = SSA_NAME_DEF_STMT (name);
5144 tree name2 = NULL_TREE, names[2], cst2 = NULL_TREE;
5145 tree val2 = NULL_TREE;
5146 unsigned int prec = TYPE_PRECISION (TREE_TYPE (val));
5147 wide_int mask = wi::zero (prec);
5148 unsigned int nprec = prec;
5149 enum tree_code rhs_code = ERROR_MARK;
5151 if (is_gimple_assign (def_stmt))
5152 rhs_code = gimple_assign_rhs_code (def_stmt);
5154 /* Add asserts for NAME cmp CST and NAME being defined
5155 as NAME = (int) NAME2. */
5156 if (!TYPE_UNSIGNED (TREE_TYPE (val))
5157 && (comp_code == LE_EXPR || comp_code == LT_EXPR
5158 || comp_code == GT_EXPR || comp_code == GE_EXPR)
5159 && gimple_assign_cast_p (def_stmt))
5161 name2 = gimple_assign_rhs1 (def_stmt);
5162 if (CONVERT_EXPR_CODE_P (rhs_code)
5163 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
5164 && TYPE_UNSIGNED (TREE_TYPE (name2))
5165 && prec == TYPE_PRECISION (TREE_TYPE (name2))
5166 && (comp_code == LE_EXPR || comp_code == GT_EXPR
5167 || !tree_int_cst_equal (val,
5168 TYPE_MIN_VALUE (TREE_TYPE (val))))
5169 && live_on_edge (e, name2)
5170 && !has_single_use (name2))
5172 tree tmp, cst;
5173 enum tree_code new_comp_code = comp_code;
5175 cst = fold_convert (TREE_TYPE (name2),
5176 TYPE_MIN_VALUE (TREE_TYPE (val)));
5177 /* Build an expression for the range test. */
5178 tmp = build2 (PLUS_EXPR, TREE_TYPE (name2), name2, cst);
5179 cst = fold_build2 (PLUS_EXPR, TREE_TYPE (name2), cst,
5180 fold_convert (TREE_TYPE (name2), val));
5181 if (comp_code == LT_EXPR || comp_code == GE_EXPR)
5183 new_comp_code = comp_code == LT_EXPR ? LE_EXPR : GT_EXPR;
5184 cst = fold_build2 (MINUS_EXPR, TREE_TYPE (name2), cst,
5185 build_int_cst (TREE_TYPE (name2), 1));
5188 if (dump_file)
5190 fprintf (dump_file, "Adding assert for ");
5191 print_generic_expr (dump_file, name2, 0);
5192 fprintf (dump_file, " from ");
5193 print_generic_expr (dump_file, tmp, 0);
5194 fprintf (dump_file, "\n");
5197 register_new_assert_for (name2, tmp, new_comp_code, cst, NULL,
5198 e, bsi);
5200 retval = true;
5204 /* Add asserts for NAME cmp CST and NAME being defined as
5205 NAME = NAME2 >> CST2.
5207 Extract CST2 from the right shift. */
5208 if (rhs_code == RSHIFT_EXPR)
5210 name2 = gimple_assign_rhs1 (def_stmt);
5211 cst2 = gimple_assign_rhs2 (def_stmt);
5212 if (TREE_CODE (name2) == SSA_NAME
5213 && tree_fits_uhwi_p (cst2)
5214 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
5215 && IN_RANGE (tree_to_uhwi (cst2), 1, prec - 1)
5216 && prec == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (val)))
5217 && live_on_edge (e, name2)
5218 && !has_single_use (name2))
5220 mask = wi::mask (tree_to_uhwi (cst2), false, prec);
5221 val2 = fold_binary (LSHIFT_EXPR, TREE_TYPE (val), val, cst2);
5224 if (val2 != NULL_TREE
5225 && TREE_CODE (val2) == INTEGER_CST
5226 && simple_cst_equal (fold_build2 (RSHIFT_EXPR,
5227 TREE_TYPE (val),
5228 val2, cst2), val))
5230 enum tree_code new_comp_code = comp_code;
5231 tree tmp, new_val;
5233 tmp = name2;
5234 if (comp_code == EQ_EXPR || comp_code == NE_EXPR)
5236 if (!TYPE_UNSIGNED (TREE_TYPE (val)))
5238 tree type = build_nonstandard_integer_type (prec, 1);
5239 tmp = build1 (NOP_EXPR, type, name2);
5240 val2 = fold_convert (type, val2);
5242 tmp = fold_build2 (MINUS_EXPR, TREE_TYPE (tmp), tmp, val2);
5243 new_val = wide_int_to_tree (TREE_TYPE (tmp), mask);
5244 new_comp_code = comp_code == EQ_EXPR ? LE_EXPR : GT_EXPR;
5246 else if (comp_code == LT_EXPR || comp_code == GE_EXPR)
5248 wide_int minval
5249 = wi::min_value (prec, TYPE_SIGN (TREE_TYPE (val)));
5250 new_val = val2;
5251 if (minval == new_val)
5252 new_val = NULL_TREE;
5254 else
5256 wide_int maxval
5257 = wi::max_value (prec, TYPE_SIGN (TREE_TYPE (val)));
5258 mask |= val2;
5259 if (mask == maxval)
5260 new_val = NULL_TREE;
5261 else
5262 new_val = wide_int_to_tree (TREE_TYPE (val2), mask);
5265 if (new_val)
5267 if (dump_file)
5269 fprintf (dump_file, "Adding assert for ");
5270 print_generic_expr (dump_file, name2, 0);
5271 fprintf (dump_file, " from ");
5272 print_generic_expr (dump_file, tmp, 0);
5273 fprintf (dump_file, "\n");
5276 register_new_assert_for (name2, tmp, new_comp_code, new_val,
5277 NULL, e, bsi);
5278 retval = true;
5282 /* Add asserts for NAME cmp CST and NAME being defined as
5283 NAME = NAME2 & CST2.
5285 Extract CST2 from the and.
5287 Also handle
5288 NAME = (unsigned) NAME2;
5289 casts where NAME's type is unsigned and has smaller precision
5290 than NAME2's type as if it was NAME = NAME2 & MASK. */
5291 names[0] = NULL_TREE;
5292 names[1] = NULL_TREE;
5293 cst2 = NULL_TREE;
5294 if (rhs_code == BIT_AND_EXPR
5295 || (CONVERT_EXPR_CODE_P (rhs_code)
5296 && TREE_CODE (TREE_TYPE (val)) == INTEGER_TYPE
5297 && TYPE_UNSIGNED (TREE_TYPE (val))
5298 && TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (def_stmt)))
5299 > prec
5300 && !retval))
5302 name2 = gimple_assign_rhs1 (def_stmt);
5303 if (rhs_code == BIT_AND_EXPR)
5304 cst2 = gimple_assign_rhs2 (def_stmt);
5305 else
5307 cst2 = TYPE_MAX_VALUE (TREE_TYPE (val));
5308 nprec = TYPE_PRECISION (TREE_TYPE (name2));
5310 if (TREE_CODE (name2) == SSA_NAME
5311 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
5312 && TREE_CODE (cst2) == INTEGER_CST
5313 && !integer_zerop (cst2)
5314 && (nprec > 1
5315 || TYPE_UNSIGNED (TREE_TYPE (val))))
5317 gimple def_stmt2 = SSA_NAME_DEF_STMT (name2);
5318 if (gimple_assign_cast_p (def_stmt2))
5320 names[1] = gimple_assign_rhs1 (def_stmt2);
5321 if (!CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt2))
5322 || !INTEGRAL_TYPE_P (TREE_TYPE (names[1]))
5323 || (TYPE_PRECISION (TREE_TYPE (name2))
5324 != TYPE_PRECISION (TREE_TYPE (names[1])))
5325 || !live_on_edge (e, names[1])
5326 || has_single_use (names[1]))
5327 names[1] = NULL_TREE;
5329 if (live_on_edge (e, name2)
5330 && !has_single_use (name2))
5331 names[0] = name2;
5334 if (names[0] || names[1])
5336 wide_int minv, maxv, valv, cst2v;
5337 wide_int tem, sgnbit;
5338 bool valid_p = false, valn, cst2n;
5339 enum tree_code ccode = comp_code;
5341 valv = wide_int::from (val, nprec, UNSIGNED);
5342 cst2v = wide_int::from (cst2, nprec, UNSIGNED);
5343 valn = wi::neg_p (valv, TYPE_SIGN (TREE_TYPE (val)));
5344 cst2n = wi::neg_p (cst2v, TYPE_SIGN (TREE_TYPE (val)));
5345 /* If CST2 doesn't have most significant bit set,
5346 but VAL is negative, we have comparison like
5347 if ((x & 0x123) > -4) (always true). Just give up. */
5348 if (!cst2n && valn)
5349 ccode = ERROR_MARK;
5350 if (cst2n)
5351 sgnbit = wi::set_bit_in_zero (nprec - 1, nprec);
5352 else
5353 sgnbit = wi::zero (nprec);
5354 minv = valv & cst2v;
5355 switch (ccode)
5357 case EQ_EXPR:
5358 /* Minimum unsigned value for equality is VAL & CST2
5359 (should be equal to VAL, otherwise we probably should
5360 have folded the comparison into false) and
5361 maximum unsigned value is VAL | ~CST2. */
5362 maxv = valv | ~cst2v;
5363 valid_p = true;
5364 break;
5366 case NE_EXPR:
5367 tem = valv | ~cst2v;
5368 /* If VAL is 0, handle (X & CST2) != 0 as (X & CST2) > 0U. */
5369 if (valv == 0)
5371 cst2n = false;
5372 sgnbit = wi::zero (nprec);
5373 goto gt_expr;
5375 /* If (VAL | ~CST2) is all ones, handle it as
5376 (X & CST2) < VAL. */
5377 if (tem == -1)
5379 cst2n = false;
5380 valn = false;
5381 sgnbit = wi::zero (nprec);
5382 goto lt_expr;
5384 if (!cst2n && wi::neg_p (cst2v))
5385 sgnbit = wi::set_bit_in_zero (nprec - 1, nprec);
5386 if (sgnbit != 0)
5388 if (valv == sgnbit)
5390 cst2n = true;
5391 valn = true;
5392 goto gt_expr;
5394 if (tem == wi::mask (nprec - 1, false, nprec))
5396 cst2n = true;
5397 goto lt_expr;
5399 if (!cst2n)
5400 sgnbit = wi::zero (nprec);
5402 break;
5404 case GE_EXPR:
5405 /* Minimum unsigned value for >= if (VAL & CST2) == VAL
5406 is VAL and maximum unsigned value is ~0. For signed
5407 comparison, if CST2 doesn't have most significant bit
5408 set, handle it similarly. If CST2 has MSB set,
5409 the minimum is the same, and maximum is ~0U/2. */
5410 if (minv != valv)
5412 /* If (VAL & CST2) != VAL, X & CST2 can't be equal to
5413 VAL. */
5414 minv = masked_increment (valv, cst2v, sgnbit, nprec);
5415 if (minv == valv)
5416 break;
5418 maxv = wi::mask (nprec - (cst2n ? 1 : 0), false, nprec);
5419 valid_p = true;
5420 break;
5422 case GT_EXPR:
5423 gt_expr:
5424 /* Find out smallest MINV where MINV > VAL
5425 && (MINV & CST2) == MINV, if any. If VAL is signed and
5426 CST2 has MSB set, compute it biased by 1 << (nprec - 1). */
5427 minv = masked_increment (valv, cst2v, sgnbit, nprec);
5428 if (minv == valv)
5429 break;
5430 maxv = wi::mask (nprec - (cst2n ? 1 : 0), false, nprec);
5431 valid_p = true;
5432 break;
5434 case LE_EXPR:
5435 /* Minimum unsigned value for <= is 0 and maximum
5436 unsigned value is VAL | ~CST2 if (VAL & CST2) == VAL.
5437 Otherwise, find smallest VAL2 where VAL2 > VAL
5438 && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
5439 as maximum.
5440 For signed comparison, if CST2 doesn't have most
5441 significant bit set, handle it similarly. If CST2 has
5442 MSB set, the maximum is the same and minimum is INT_MIN. */
5443 if (minv == valv)
5444 maxv = valv;
5445 else
5447 maxv = masked_increment (valv, cst2v, sgnbit, nprec);
5448 if (maxv == valv)
5449 break;
5450 maxv -= 1;
5452 maxv |= ~cst2v;
5453 minv = sgnbit;
5454 valid_p = true;
5455 break;
5457 case LT_EXPR:
5458 lt_expr:
5459 /* Minimum unsigned value for < is 0 and maximum
5460 unsigned value is (VAL-1) | ~CST2 if (VAL & CST2) == VAL.
5461 Otherwise, find smallest VAL2 where VAL2 > VAL
5462 && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
5463 as maximum.
5464 For signed comparison, if CST2 doesn't have most
5465 significant bit set, handle it similarly. If CST2 has
5466 MSB set, the maximum is the same and minimum is INT_MIN. */
5467 if (minv == valv)
5469 if (valv == sgnbit)
5470 break;
5471 maxv = valv;
5473 else
5475 maxv = masked_increment (valv, cst2v, sgnbit, nprec);
5476 if (maxv == valv)
5477 break;
5479 maxv -= 1;
5480 maxv |= ~cst2v;
5481 minv = sgnbit;
5482 valid_p = true;
5483 break;
5485 default:
5486 break;
5488 if (valid_p
5489 && (maxv - minv) != -1)
5491 tree tmp, new_val, type;
5492 int i;
5494 for (i = 0; i < 2; i++)
5495 if (names[i])
5497 wide_int maxv2 = maxv;
5498 tmp = names[i];
5499 type = TREE_TYPE (names[i]);
5500 if (!TYPE_UNSIGNED (type))
5502 type = build_nonstandard_integer_type (nprec, 1);
5503 tmp = build1 (NOP_EXPR, type, names[i]);
5505 if (minv != 0)
5507 tmp = build2 (PLUS_EXPR, type, tmp,
5508 wide_int_to_tree (type, -minv));
5509 maxv2 = maxv - minv;
5511 new_val = wide_int_to_tree (type, maxv2);
5513 if (dump_file)
5515 fprintf (dump_file, "Adding assert for ");
5516 print_generic_expr (dump_file, names[i], 0);
5517 fprintf (dump_file, " from ");
5518 print_generic_expr (dump_file, tmp, 0);
5519 fprintf (dump_file, "\n");
5522 register_new_assert_for (names[i], tmp, LE_EXPR,
5523 new_val, NULL, e, bsi);
5524 retval = true;
5530 return retval;
5533 /* OP is an operand of a truth value expression which is known to have
5534 a particular value. Register any asserts for OP and for any
5535 operands in OP's defining statement.
5537 If CODE is EQ_EXPR, then we want to register OP is zero (false),
5538 if CODE is NE_EXPR, then we want to register OP is nonzero (true). */
5540 static bool
5541 register_edge_assert_for_1 (tree op, enum tree_code code,
5542 edge e, gimple_stmt_iterator bsi)
5544 bool retval = false;
5545 gimple op_def;
5546 tree val;
5547 enum tree_code rhs_code;
5549 /* We only care about SSA_NAMEs. */
5550 if (TREE_CODE (op) != SSA_NAME)
5551 return false;
5553 /* We know that OP will have a zero or nonzero value. If OP is used
5554 more than once go ahead and register an assert for OP. */
5555 if (live_on_edge (e, op)
5556 && !has_single_use (op))
5558 val = build_int_cst (TREE_TYPE (op), 0);
5559 register_new_assert_for (op, op, code, val, NULL, e, bsi);
5560 retval = true;
5563 /* Now look at how OP is set. If it's set from a comparison,
5564 a truth operation or some bit operations, then we may be able
5565 to register information about the operands of that assignment. */
5566 op_def = SSA_NAME_DEF_STMT (op);
5567 if (gimple_code (op_def) != GIMPLE_ASSIGN)
5568 return retval;
5570 rhs_code = gimple_assign_rhs_code (op_def);
5572 if (TREE_CODE_CLASS (rhs_code) == tcc_comparison)
5574 bool invert = (code == EQ_EXPR ? true : false);
5575 tree op0 = gimple_assign_rhs1 (op_def);
5576 tree op1 = gimple_assign_rhs2 (op_def);
5578 if (TREE_CODE (op0) == SSA_NAME)
5579 retval |= register_edge_assert_for_2 (op0, e, bsi, rhs_code, op0, op1,
5580 invert);
5581 if (TREE_CODE (op1) == SSA_NAME)
5582 retval |= register_edge_assert_for_2 (op1, e, bsi, rhs_code, op0, op1,
5583 invert);
5585 else if ((code == NE_EXPR
5586 && gimple_assign_rhs_code (op_def) == BIT_AND_EXPR)
5587 || (code == EQ_EXPR
5588 && gimple_assign_rhs_code (op_def) == BIT_IOR_EXPR))
5590 /* Recurse on each operand. */
5591 tree op0 = gimple_assign_rhs1 (op_def);
5592 tree op1 = gimple_assign_rhs2 (op_def);
5593 if (TREE_CODE (op0) == SSA_NAME
5594 && has_single_use (op0))
5595 retval |= register_edge_assert_for_1 (op0, code, e, bsi);
5596 if (TREE_CODE (op1) == SSA_NAME
5597 && has_single_use (op1))
5598 retval |= register_edge_assert_for_1 (op1, code, e, bsi);
5600 else if (gimple_assign_rhs_code (op_def) == BIT_NOT_EXPR
5601 && TYPE_PRECISION (TREE_TYPE (gimple_assign_lhs (op_def))) == 1)
5603 /* Recurse, flipping CODE. */
5604 code = invert_tree_comparison (code, false);
5605 retval |= register_edge_assert_for_1 (gimple_assign_rhs1 (op_def),
5606 code, e, bsi);
5608 else if (gimple_assign_rhs_code (op_def) == SSA_NAME)
5610 /* Recurse through the copy. */
5611 retval |= register_edge_assert_for_1 (gimple_assign_rhs1 (op_def),
5612 code, e, bsi);
5614 else if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (op_def)))
5616 /* Recurse through the type conversion, unless it is a narrowing
5617 conversion or conversion from non-integral type. */
5618 tree rhs = gimple_assign_rhs1 (op_def);
5619 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs))
5620 && (TYPE_PRECISION (TREE_TYPE (rhs))
5621 <= TYPE_PRECISION (TREE_TYPE (op))))
5622 retval |= register_edge_assert_for_1 (rhs, code, e, bsi);
5625 return retval;
5628 /* Try to register an edge assertion for SSA name NAME on edge E for
5629 the condition COND contributing to the conditional jump pointed to by SI.
5630 Return true if an assertion for NAME could be registered. */
5632 static bool
5633 register_edge_assert_for (tree name, edge e, gimple_stmt_iterator si,
5634 enum tree_code cond_code, tree cond_op0,
5635 tree cond_op1)
5637 tree val;
5638 enum tree_code comp_code;
5639 bool retval = false;
5640 bool is_else_edge = (e->flags & EDGE_FALSE_VALUE) != 0;
5642 /* Do not attempt to infer anything in names that flow through
5643 abnormal edges. */
5644 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name))
5645 return false;
5647 if (!extract_code_and_val_from_cond_with_ops (name, cond_code,
5648 cond_op0, cond_op1,
5649 is_else_edge,
5650 &comp_code, &val))
5651 return false;
5653 /* Register ASSERT_EXPRs for name. */
5654 retval |= register_edge_assert_for_2 (name, e, si, cond_code, cond_op0,
5655 cond_op1, is_else_edge);
5658 /* If COND is effectively an equality test of an SSA_NAME against
5659 the value zero or one, then we may be able to assert values
5660 for SSA_NAMEs which flow into COND. */
5662 /* In the case of NAME == 1 or NAME != 0, for BIT_AND_EXPR defining
5663 statement of NAME we can assert both operands of the BIT_AND_EXPR
5664 have nonzero value. */
5665 if (((comp_code == EQ_EXPR && integer_onep (val))
5666 || (comp_code == NE_EXPR && integer_zerop (val))))
5668 gimple def_stmt = SSA_NAME_DEF_STMT (name);
5670 if (is_gimple_assign (def_stmt)
5671 && gimple_assign_rhs_code (def_stmt) == BIT_AND_EXPR)
5673 tree op0 = gimple_assign_rhs1 (def_stmt);
5674 tree op1 = gimple_assign_rhs2 (def_stmt);
5675 retval |= register_edge_assert_for_1 (op0, NE_EXPR, e, si);
5676 retval |= register_edge_assert_for_1 (op1, NE_EXPR, e, si);
5680 /* In the case of NAME == 0 or NAME != 1, for BIT_IOR_EXPR defining
5681 statement of NAME we can assert both operands of the BIT_IOR_EXPR
5682 have zero value. */
5683 if (((comp_code == EQ_EXPR && integer_zerop (val))
5684 || (comp_code == NE_EXPR && integer_onep (val))))
5686 gimple def_stmt = SSA_NAME_DEF_STMT (name);
5688 /* For BIT_IOR_EXPR only if NAME == 0 both operands have
5689 necessarily zero value, or if type-precision is one. */
5690 if (is_gimple_assign (def_stmt)
5691 && (gimple_assign_rhs_code (def_stmt) == BIT_IOR_EXPR
5692 && (TYPE_PRECISION (TREE_TYPE (name)) == 1
5693 || comp_code == EQ_EXPR)))
5695 tree op0 = gimple_assign_rhs1 (def_stmt);
5696 tree op1 = gimple_assign_rhs2 (def_stmt);
5697 retval |= register_edge_assert_for_1 (op0, EQ_EXPR, e, si);
5698 retval |= register_edge_assert_for_1 (op1, EQ_EXPR, e, si);
5702 return retval;
5706 /* Determine whether the outgoing edges of BB should receive an
5707 ASSERT_EXPR for each of the operands of BB's LAST statement.
5708 The last statement of BB must be a COND_EXPR.
5710 If any of the sub-graphs rooted at BB have an interesting use of
5711 the predicate operands, an assert location node is added to the
5712 list of assertions for the corresponding operands. */
5714 static bool
5715 find_conditional_asserts (basic_block bb, gimple last)
5717 bool need_assert;
5718 gimple_stmt_iterator bsi;
5719 tree op;
5720 edge_iterator ei;
5721 edge e;
5722 ssa_op_iter iter;
5724 need_assert = false;
5725 bsi = gsi_for_stmt (last);
5727 /* Look for uses of the operands in each of the sub-graphs
5728 rooted at BB. We need to check each of the outgoing edges
5729 separately, so that we know what kind of ASSERT_EXPR to
5730 insert. */
5731 FOR_EACH_EDGE (e, ei, bb->succs)
5733 if (e->dest == bb)
5734 continue;
5736 /* Register the necessary assertions for each operand in the
5737 conditional predicate. */
5738 FOR_EACH_SSA_TREE_OPERAND (op, last, iter, SSA_OP_USE)
5740 need_assert |= register_edge_assert_for (op, e, bsi,
5741 gimple_cond_code (last),
5742 gimple_cond_lhs (last),
5743 gimple_cond_rhs (last));
5747 return need_assert;
5750 struct case_info
5752 tree expr;
5753 basic_block bb;
5756 /* Compare two case labels sorting first by the destination bb index
5757 and then by the case value. */
5759 static int
5760 compare_case_labels (const void *p1, const void *p2)
5762 const struct case_info *ci1 = (const struct case_info *) p1;
5763 const struct case_info *ci2 = (const struct case_info *) p2;
5764 int idx1 = ci1->bb->index;
5765 int idx2 = ci2->bb->index;
5767 if (idx1 < idx2)
5768 return -1;
5769 else if (idx1 == idx2)
5771 /* Make sure the default label is first in a group. */
5772 if (!CASE_LOW (ci1->expr))
5773 return -1;
5774 else if (!CASE_LOW (ci2->expr))
5775 return 1;
5776 else
5777 return tree_int_cst_compare (CASE_LOW (ci1->expr),
5778 CASE_LOW (ci2->expr));
5780 else
5781 return 1;
5784 /* Determine whether the outgoing edges of BB should receive an
5785 ASSERT_EXPR for each of the operands of BB's LAST statement.
5786 The last statement of BB must be a SWITCH_EXPR.
5788 If any of the sub-graphs rooted at BB have an interesting use of
5789 the predicate operands, an assert location node is added to the
5790 list of assertions for the corresponding operands. */
5792 static bool
5793 find_switch_asserts (basic_block bb, gimple last)
5795 bool need_assert;
5796 gimple_stmt_iterator bsi;
5797 tree op;
5798 edge e;
5799 struct case_info *ci;
5800 size_t n = gimple_switch_num_labels (last);
5801 #if GCC_VERSION >= 4000
5802 unsigned int idx;
5803 #else
5804 /* Work around GCC 3.4 bug (PR 37086). */
5805 volatile unsigned int idx;
5806 #endif
5808 need_assert = false;
5809 bsi = gsi_for_stmt (last);
5810 op = gimple_switch_index (last);
5811 if (TREE_CODE (op) != SSA_NAME)
5812 return false;
5814 /* Build a vector of case labels sorted by destination label. */
5815 ci = XNEWVEC (struct case_info, n);
5816 for (idx = 0; idx < n; ++idx)
5818 ci[idx].expr = gimple_switch_label (last, idx);
5819 ci[idx].bb = label_to_block (CASE_LABEL (ci[idx].expr));
5821 qsort (ci, n, sizeof (struct case_info), compare_case_labels);
5823 for (idx = 0; idx < n; ++idx)
5825 tree min, max;
5826 tree cl = ci[idx].expr;
5827 basic_block cbb = ci[idx].bb;
5829 min = CASE_LOW (cl);
5830 max = CASE_HIGH (cl);
5832 /* If there are multiple case labels with the same destination
5833 we need to combine them to a single value range for the edge. */
5834 if (idx + 1 < n && cbb == ci[idx + 1].bb)
5836 /* Skip labels until the last of the group. */
5837 do {
5838 ++idx;
5839 } while (idx < n && cbb == ci[idx].bb);
5840 --idx;
5842 /* Pick up the maximum of the case label range. */
5843 if (CASE_HIGH (ci[idx].expr))
5844 max = CASE_HIGH (ci[idx].expr);
5845 else
5846 max = CASE_LOW (ci[idx].expr);
5849 /* Nothing to do if the range includes the default label until we
5850 can register anti-ranges. */
5851 if (min == NULL_TREE)
5852 continue;
5854 /* Find the edge to register the assert expr on. */
5855 e = find_edge (bb, cbb);
5857 /* Register the necessary assertions for the operand in the
5858 SWITCH_EXPR. */
5859 need_assert |= register_edge_assert_for (op, e, bsi,
5860 max ? GE_EXPR : EQ_EXPR,
5862 fold_convert (TREE_TYPE (op),
5863 min));
5864 if (max)
5866 need_assert |= register_edge_assert_for (op, e, bsi, LE_EXPR,
5868 fold_convert (TREE_TYPE (op),
5869 max));
5873 XDELETEVEC (ci);
5874 return need_assert;
5878 /* Traverse all the statements in block BB looking for statements that
5879 may generate useful assertions for the SSA names in their operand.
5880 If a statement produces a useful assertion A for name N_i, then the
5881 list of assertions already generated for N_i is scanned to
5882 determine if A is actually needed.
5884 If N_i already had the assertion A at a location dominating the
5885 current location, then nothing needs to be done. Otherwise, the
5886 new location for A is recorded instead.
5888 1- For every statement S in BB, all the variables used by S are
5889 added to bitmap FOUND_IN_SUBGRAPH.
5891 2- If statement S uses an operand N in a way that exposes a known
5892 value range for N, then if N was not already generated by an
5893 ASSERT_EXPR, create a new assert location for N. For instance,
5894 if N is a pointer and the statement dereferences it, we can
5895 assume that N is not NULL.
5897 3- COND_EXPRs are a special case of #2. We can derive range
5898 information from the predicate but need to insert different
5899 ASSERT_EXPRs for each of the sub-graphs rooted at the
5900 conditional block. If the last statement of BB is a conditional
5901 expression of the form 'X op Y', then
5903 a) Remove X and Y from the set FOUND_IN_SUBGRAPH.
5905 b) If the conditional is the only entry point to the sub-graph
5906 corresponding to the THEN_CLAUSE, recurse into it. On
5907 return, if X and/or Y are marked in FOUND_IN_SUBGRAPH, then
5908 an ASSERT_EXPR is added for the corresponding variable.
5910 c) Repeat step (b) on the ELSE_CLAUSE.
5912 d) Mark X and Y in FOUND_IN_SUBGRAPH.
5914 For instance,
5916 if (a == 9)
5917 b = a;
5918 else
5919 b = c + 1;
5921 In this case, an assertion on the THEN clause is useful to
5922 determine that 'a' is always 9 on that edge. However, an assertion
5923 on the ELSE clause would be unnecessary.
5925 4- If BB does not end in a conditional expression, then we recurse
5926 into BB's dominator children.
5928 At the end of the recursive traversal, every SSA name will have a
5929 list of locations where ASSERT_EXPRs should be added. When a new
5930 location for name N is found, it is registered by calling
5931 register_new_assert_for. That function keeps track of all the
5932 registered assertions to prevent adding unnecessary assertions.
5933 For instance, if a pointer P_4 is dereferenced more than once in a
5934 dominator tree, only the location dominating all the dereference of
5935 P_4 will receive an ASSERT_EXPR.
5937 If this function returns true, then it means that there are names
5938 for which we need to generate ASSERT_EXPRs. Those assertions are
5939 inserted by process_assert_insertions. */
5941 static bool
5942 find_assert_locations_1 (basic_block bb, sbitmap live)
5944 gimple_stmt_iterator si;
5945 gimple last;
5946 bool need_assert;
5948 need_assert = false;
5949 last = last_stmt (bb);
5951 /* If BB's last statement is a conditional statement involving integer
5952 operands, determine if we need to add ASSERT_EXPRs. */
5953 if (last
5954 && gimple_code (last) == GIMPLE_COND
5955 && !fp_predicate (last)
5956 && !ZERO_SSA_OPERANDS (last, SSA_OP_USE))
5957 need_assert |= find_conditional_asserts (bb, last);
5959 /* If BB's last statement is a switch statement involving integer
5960 operands, determine if we need to add ASSERT_EXPRs. */
5961 if (last
5962 && gimple_code (last) == GIMPLE_SWITCH
5963 && !ZERO_SSA_OPERANDS (last, SSA_OP_USE))
5964 need_assert |= find_switch_asserts (bb, last);
5966 /* Traverse all the statements in BB marking used names and looking
5967 for statements that may infer assertions for their used operands. */
5968 for (si = gsi_last_bb (bb); !gsi_end_p (si); gsi_prev (&si))
5970 gimple stmt;
5971 tree op;
5972 ssa_op_iter i;
5974 stmt = gsi_stmt (si);
5976 if (is_gimple_debug (stmt))
5977 continue;
5979 /* See if we can derive an assertion for any of STMT's operands. */
5980 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_USE)
5982 tree value;
5983 enum tree_code comp_code;
5985 /* If op is not live beyond this stmt, do not bother to insert
5986 asserts for it. */
5987 if (!bitmap_bit_p (live, SSA_NAME_VERSION (op)))
5988 continue;
5990 /* If OP is used in such a way that we can infer a value
5991 range for it, and we don't find a previous assertion for
5992 it, create a new assertion location node for OP. */
5993 if (infer_value_range (stmt, op, &comp_code, &value))
5995 /* If we are able to infer a nonzero value range for OP,
5996 then walk backwards through the use-def chain to see if OP
5997 was set via a typecast.
5999 If so, then we can also infer a nonzero value range
6000 for the operand of the NOP_EXPR. */
6001 if (comp_code == NE_EXPR && integer_zerop (value))
6003 tree t = op;
6004 gimple def_stmt = SSA_NAME_DEF_STMT (t);
6006 while (is_gimple_assign (def_stmt)
6007 && CONVERT_EXPR_CODE_P
6008 (gimple_assign_rhs_code (def_stmt))
6009 && TREE_CODE
6010 (gimple_assign_rhs1 (def_stmt)) == SSA_NAME
6011 && POINTER_TYPE_P
6012 (TREE_TYPE (gimple_assign_rhs1 (def_stmt))))
6014 t = gimple_assign_rhs1 (def_stmt);
6015 def_stmt = SSA_NAME_DEF_STMT (t);
6017 /* Note we want to register the assert for the
6018 operand of the NOP_EXPR after SI, not after the
6019 conversion. */
6020 if (! has_single_use (t))
6022 register_new_assert_for (t, t, comp_code, value,
6023 bb, NULL, si);
6024 need_assert = true;
6029 register_new_assert_for (op, op, comp_code, value, bb, NULL, si);
6030 need_assert = true;
6034 /* Update live. */
6035 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_USE)
6036 bitmap_set_bit (live, SSA_NAME_VERSION (op));
6037 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_DEF)
6038 bitmap_clear_bit (live, SSA_NAME_VERSION (op));
6041 /* Traverse all PHI nodes in BB, updating live. */
6042 for (si = gsi_start_phis (bb); !gsi_end_p (si); gsi_next (&si))
6044 use_operand_p arg_p;
6045 ssa_op_iter i;
6046 gimple phi = gsi_stmt (si);
6047 tree res = gimple_phi_result (phi);
6049 if (virtual_operand_p (res))
6050 continue;
6052 FOR_EACH_PHI_ARG (arg_p, phi, i, SSA_OP_USE)
6054 tree arg = USE_FROM_PTR (arg_p);
6055 if (TREE_CODE (arg) == SSA_NAME)
6056 bitmap_set_bit (live, SSA_NAME_VERSION (arg));
6059 bitmap_clear_bit (live, SSA_NAME_VERSION (res));
6062 return need_assert;
6065 /* Do an RPO walk over the function computing SSA name liveness
6066 on-the-fly and deciding on assert expressions to insert.
6067 Returns true if there are assert expressions to be inserted. */
6069 static bool
6070 find_assert_locations (void)
6072 int *rpo = XNEWVEC (int, last_basic_block_for_fn (cfun));
6073 int *bb_rpo = XNEWVEC (int, last_basic_block_for_fn (cfun));
6074 int *last_rpo = XCNEWVEC (int, last_basic_block_for_fn (cfun));
6075 int rpo_cnt, i;
6076 bool need_asserts;
6078 live = XCNEWVEC (sbitmap, last_basic_block_for_fn (cfun));
6079 rpo_cnt = pre_and_rev_post_order_compute (NULL, rpo, false);
6080 for (i = 0; i < rpo_cnt; ++i)
6081 bb_rpo[rpo[i]] = i;
6083 /* Pre-seed loop latch liveness from loop header PHI nodes. Due to
6084 the order we compute liveness and insert asserts we otherwise
6085 fail to insert asserts into the loop latch. */
6086 loop_p loop;
6087 FOR_EACH_LOOP (loop, 0)
6089 i = loop->latch->index;
6090 unsigned int j = single_succ_edge (loop->latch)->dest_idx;
6091 for (gimple_stmt_iterator gsi = gsi_start_phis (loop->header);
6092 !gsi_end_p (gsi); gsi_next (&gsi))
6094 gimple phi = gsi_stmt (gsi);
6095 if (virtual_operand_p (gimple_phi_result (phi)))
6096 continue;
6097 tree arg = gimple_phi_arg_def (phi, j);
6098 if (TREE_CODE (arg) == SSA_NAME)
6100 if (live[i] == NULL)
6102 live[i] = sbitmap_alloc (num_ssa_names);
6103 bitmap_clear (live[i]);
6105 bitmap_set_bit (live[i], SSA_NAME_VERSION (arg));
6110 need_asserts = false;
6111 for (i = rpo_cnt - 1; i >= 0; --i)
6113 basic_block bb = BASIC_BLOCK_FOR_FN (cfun, rpo[i]);
6114 edge e;
6115 edge_iterator ei;
6117 if (!live[rpo[i]])
6119 live[rpo[i]] = sbitmap_alloc (num_ssa_names);
6120 bitmap_clear (live[rpo[i]]);
6123 /* Process BB and update the live information with uses in
6124 this block. */
6125 need_asserts |= find_assert_locations_1 (bb, live[rpo[i]]);
6127 /* Merge liveness into the predecessor blocks and free it. */
6128 if (!bitmap_empty_p (live[rpo[i]]))
6130 int pred_rpo = i;
6131 FOR_EACH_EDGE (e, ei, bb->preds)
6133 int pred = e->src->index;
6134 if ((e->flags & EDGE_DFS_BACK) || pred == ENTRY_BLOCK)
6135 continue;
6137 if (!live[pred])
6139 live[pred] = sbitmap_alloc (num_ssa_names);
6140 bitmap_clear (live[pred]);
6142 bitmap_ior (live[pred], live[pred], live[rpo[i]]);
6144 if (bb_rpo[pred] < pred_rpo)
6145 pred_rpo = bb_rpo[pred];
6148 /* Record the RPO number of the last visited block that needs
6149 live information from this block. */
6150 last_rpo[rpo[i]] = pred_rpo;
6152 else
6154 sbitmap_free (live[rpo[i]]);
6155 live[rpo[i]] = NULL;
6158 /* We can free all successors live bitmaps if all their
6159 predecessors have been visited already. */
6160 FOR_EACH_EDGE (e, ei, bb->succs)
6161 if (last_rpo[e->dest->index] == i
6162 && live[e->dest->index])
6164 sbitmap_free (live[e->dest->index]);
6165 live[e->dest->index] = NULL;
6169 XDELETEVEC (rpo);
6170 XDELETEVEC (bb_rpo);
6171 XDELETEVEC (last_rpo);
6172 for (i = 0; i < last_basic_block_for_fn (cfun); ++i)
6173 if (live[i])
6174 sbitmap_free (live[i]);
6175 XDELETEVEC (live);
6177 return need_asserts;
6180 /* Create an ASSERT_EXPR for NAME and insert it in the location
6181 indicated by LOC. Return true if we made any edge insertions. */
6183 static bool
6184 process_assert_insertions_for (tree name, assert_locus_t loc)
6186 /* Build the comparison expression NAME_i COMP_CODE VAL. */
6187 gimple stmt;
6188 tree cond;
6189 gimple assert_stmt;
6190 edge_iterator ei;
6191 edge e;
6193 /* If we have X <=> X do not insert an assert expr for that. */
6194 if (loc->expr == loc->val)
6195 return false;
6197 cond = build2 (loc->comp_code, boolean_type_node, loc->expr, loc->val);
6198 assert_stmt = build_assert_expr_for (cond, name);
6199 if (loc->e)
6201 /* We have been asked to insert the assertion on an edge. This
6202 is used only by COND_EXPR and SWITCH_EXPR assertions. */
6203 gcc_checking_assert (gimple_code (gsi_stmt (loc->si)) == GIMPLE_COND
6204 || (gimple_code (gsi_stmt (loc->si))
6205 == GIMPLE_SWITCH));
6207 gsi_insert_on_edge (loc->e, assert_stmt);
6208 return true;
6211 /* Otherwise, we can insert right after LOC->SI iff the
6212 statement must not be the last statement in the block. */
6213 stmt = gsi_stmt (loc->si);
6214 if (!stmt_ends_bb_p (stmt))
6216 gsi_insert_after (&loc->si, assert_stmt, GSI_SAME_STMT);
6217 return false;
6220 /* If STMT must be the last statement in BB, we can only insert new
6221 assertions on the non-abnormal edge out of BB. Note that since
6222 STMT is not control flow, there may only be one non-abnormal edge
6223 out of BB. */
6224 FOR_EACH_EDGE (e, ei, loc->bb->succs)
6225 if (!(e->flags & EDGE_ABNORMAL))
6227 gsi_insert_on_edge (e, assert_stmt);
6228 return true;
6231 gcc_unreachable ();
6235 /* Process all the insertions registered for every name N_i registered
6236 in NEED_ASSERT_FOR. The list of assertions to be inserted are
6237 found in ASSERTS_FOR[i]. */
6239 static void
6240 process_assert_insertions (void)
6242 unsigned i;
6243 bitmap_iterator bi;
6244 bool update_edges_p = false;
6245 int num_asserts = 0;
6247 if (dump_file && (dump_flags & TDF_DETAILS))
6248 dump_all_asserts (dump_file);
6250 EXECUTE_IF_SET_IN_BITMAP (need_assert_for, 0, i, bi)
6252 assert_locus_t loc = asserts_for[i];
6253 gcc_assert (loc);
6255 while (loc)
6257 assert_locus_t next = loc->next;
6258 update_edges_p |= process_assert_insertions_for (ssa_name (i), loc);
6259 free (loc);
6260 loc = next;
6261 num_asserts++;
6265 if (update_edges_p)
6266 gsi_commit_edge_inserts ();
6268 statistics_counter_event (cfun, "Number of ASSERT_EXPR expressions inserted",
6269 num_asserts);
6273 /* Traverse the flowgraph looking for conditional jumps to insert range
6274 expressions. These range expressions are meant to provide information
6275 to optimizations that need to reason in terms of value ranges. They
6276 will not be expanded into RTL. For instance, given:
6278 x = ...
6279 y = ...
6280 if (x < y)
6281 y = x - 2;
6282 else
6283 x = y + 3;
6285 this pass will transform the code into:
6287 x = ...
6288 y = ...
6289 if (x < y)
6291 x = ASSERT_EXPR <x, x < y>
6292 y = x - 2
6294 else
6296 y = ASSERT_EXPR <y, x >= y>
6297 x = y + 3
6300 The idea is that once copy and constant propagation have run, other
6301 optimizations will be able to determine what ranges of values can 'x'
6302 take in different paths of the code, simply by checking the reaching
6303 definition of 'x'. */
6305 static void
6306 insert_range_assertions (void)
6308 need_assert_for = BITMAP_ALLOC (NULL);
6309 asserts_for = XCNEWVEC (assert_locus_t, num_ssa_names);
6311 calculate_dominance_info (CDI_DOMINATORS);
6313 if (find_assert_locations ())
6315 process_assert_insertions ();
6316 update_ssa (TODO_update_ssa_no_phi);
6319 if (dump_file && (dump_flags & TDF_DETAILS))
6321 fprintf (dump_file, "\nSSA form after inserting ASSERT_EXPRs\n");
6322 dump_function_to_file (current_function_decl, dump_file, dump_flags);
6325 free (asserts_for);
6326 BITMAP_FREE (need_assert_for);
6329 /* Checks one ARRAY_REF in REF, located at LOCUS. Ignores flexible arrays
6330 and "struct" hacks. If VRP can determine that the
6331 array subscript is a constant, check if it is outside valid
6332 range. If the array subscript is a RANGE, warn if it is
6333 non-overlapping with valid range.
6334 IGNORE_OFF_BY_ONE is true if the ARRAY_REF is inside a ADDR_EXPR. */
6336 static void
6337 check_array_ref (location_t location, tree ref, bool ignore_off_by_one)
6339 value_range_t* vr = NULL;
6340 tree low_sub, up_sub;
6341 tree low_bound, up_bound, up_bound_p1;
6342 tree base;
6344 if (TREE_NO_WARNING (ref))
6345 return;
6347 low_sub = up_sub = TREE_OPERAND (ref, 1);
6348 up_bound = array_ref_up_bound (ref);
6350 /* Can not check flexible arrays. */
6351 if (!up_bound
6352 || TREE_CODE (up_bound) != INTEGER_CST)
6353 return;
6355 /* Accesses to trailing arrays via pointers may access storage
6356 beyond the types array bounds. */
6357 base = get_base_address (ref);
6358 if (base && TREE_CODE (base) == MEM_REF)
6360 tree cref, next = NULL_TREE;
6362 if (TREE_CODE (TREE_OPERAND (ref, 0)) != COMPONENT_REF)
6363 return;
6365 cref = TREE_OPERAND (ref, 0);
6366 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (cref, 0))) == RECORD_TYPE)
6367 for (next = DECL_CHAIN (TREE_OPERAND (cref, 1));
6368 next && TREE_CODE (next) != FIELD_DECL;
6369 next = DECL_CHAIN (next))
6372 /* If this is the last field in a struct type or a field in a
6373 union type do not warn. */
6374 if (!next)
6375 return;
6378 low_bound = array_ref_low_bound (ref);
6379 up_bound_p1 = int_const_binop (PLUS_EXPR, up_bound,
6380 build_int_cst (TREE_TYPE (up_bound), 1));
6382 if (TREE_CODE (low_sub) == SSA_NAME)
6384 vr = get_value_range (low_sub);
6385 if (vr->type == VR_RANGE || vr->type == VR_ANTI_RANGE)
6387 low_sub = vr->type == VR_RANGE ? vr->max : vr->min;
6388 up_sub = vr->type == VR_RANGE ? vr->min : vr->max;
6392 if (vr && vr->type == VR_ANTI_RANGE)
6394 if (TREE_CODE (up_sub) == INTEGER_CST
6395 && tree_int_cst_lt (up_bound, up_sub)
6396 && TREE_CODE (low_sub) == INTEGER_CST
6397 && tree_int_cst_lt (low_sub, low_bound))
6399 warning_at (location, OPT_Warray_bounds,
6400 "array subscript is outside array bounds");
6401 TREE_NO_WARNING (ref) = 1;
6404 else if (TREE_CODE (up_sub) == INTEGER_CST
6405 && (ignore_off_by_one
6406 ? (tree_int_cst_lt (up_bound, up_sub)
6407 && !tree_int_cst_equal (up_bound_p1, up_sub))
6408 : (tree_int_cst_lt (up_bound, up_sub)
6409 || tree_int_cst_equal (up_bound_p1, up_sub))))
6411 if (dump_file && (dump_flags & TDF_DETAILS))
6413 fprintf (dump_file, "Array bound warning for ");
6414 dump_generic_expr (MSG_NOTE, TDF_SLIM, ref);
6415 fprintf (dump_file, "\n");
6417 warning_at (location, OPT_Warray_bounds,
6418 "array subscript is above array bounds");
6419 TREE_NO_WARNING (ref) = 1;
6421 else if (TREE_CODE (low_sub) == INTEGER_CST
6422 && tree_int_cst_lt (low_sub, low_bound))
6424 if (dump_file && (dump_flags & TDF_DETAILS))
6426 fprintf (dump_file, "Array bound warning for ");
6427 dump_generic_expr (MSG_NOTE, TDF_SLIM, ref);
6428 fprintf (dump_file, "\n");
6430 warning_at (location, OPT_Warray_bounds,
6431 "array subscript is below array bounds");
6432 TREE_NO_WARNING (ref) = 1;
6436 /* Searches if the expr T, located at LOCATION computes
6437 address of an ARRAY_REF, and call check_array_ref on it. */
6439 static void
6440 search_for_addr_array (tree t, location_t location)
6442 while (TREE_CODE (t) == SSA_NAME)
6444 gimple g = SSA_NAME_DEF_STMT (t);
6446 if (gimple_code (g) != GIMPLE_ASSIGN)
6447 return;
6449 if (get_gimple_rhs_class (gimple_assign_rhs_code (g))
6450 != GIMPLE_SINGLE_RHS)
6451 return;
6453 t = gimple_assign_rhs1 (g);
6457 /* We are only interested in addresses of ARRAY_REF's. */
6458 if (TREE_CODE (t) != ADDR_EXPR)
6459 return;
6461 /* Check each ARRAY_REFs in the reference chain. */
6464 if (TREE_CODE (t) == ARRAY_REF)
6465 check_array_ref (location, t, true /*ignore_off_by_one*/);
6467 t = TREE_OPERAND (t, 0);
6469 while (handled_component_p (t));
6471 if (TREE_CODE (t) == MEM_REF
6472 && TREE_CODE (TREE_OPERAND (t, 0)) == ADDR_EXPR
6473 && !TREE_NO_WARNING (t))
6475 tree tem = TREE_OPERAND (TREE_OPERAND (t, 0), 0);
6476 tree low_bound, up_bound, el_sz;
6477 offset_int idx;
6478 if (TREE_CODE (TREE_TYPE (tem)) != ARRAY_TYPE
6479 || TREE_CODE (TREE_TYPE (TREE_TYPE (tem))) == ARRAY_TYPE
6480 || !TYPE_DOMAIN (TREE_TYPE (tem)))
6481 return;
6483 low_bound = TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (tem)));
6484 up_bound = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (tem)));
6485 el_sz = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (tem)));
6486 if (!low_bound
6487 || TREE_CODE (low_bound) != INTEGER_CST
6488 || !up_bound
6489 || TREE_CODE (up_bound) != INTEGER_CST
6490 || !el_sz
6491 || TREE_CODE (el_sz) != INTEGER_CST)
6492 return;
6494 idx = mem_ref_offset (t);
6495 idx = wi::sdiv_trunc (idx, wi::to_offset (el_sz));
6496 if (wi::lts_p (idx, 0))
6498 if (dump_file && (dump_flags & TDF_DETAILS))
6500 fprintf (dump_file, "Array bound warning for ");
6501 dump_generic_expr (MSG_NOTE, TDF_SLIM, t);
6502 fprintf (dump_file, "\n");
6504 warning_at (location, OPT_Warray_bounds,
6505 "array subscript is below array bounds");
6506 TREE_NO_WARNING (t) = 1;
6508 else if (wi::gts_p (idx, (wi::to_offset (up_bound)
6509 - wi::to_offset (low_bound) + 1)))
6511 if (dump_file && (dump_flags & TDF_DETAILS))
6513 fprintf (dump_file, "Array bound warning for ");
6514 dump_generic_expr (MSG_NOTE, TDF_SLIM, t);
6515 fprintf (dump_file, "\n");
6517 warning_at (location, OPT_Warray_bounds,
6518 "array subscript is above array bounds");
6519 TREE_NO_WARNING (t) = 1;
6524 /* walk_tree() callback that checks if *TP is
6525 an ARRAY_REF inside an ADDR_EXPR (in which an array
6526 subscript one outside the valid range is allowed). Call
6527 check_array_ref for each ARRAY_REF found. The location is
6528 passed in DATA. */
6530 static tree
6531 check_array_bounds (tree *tp, int *walk_subtree, void *data)
6533 tree t = *tp;
6534 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
6535 location_t location;
6537 if (EXPR_HAS_LOCATION (t))
6538 location = EXPR_LOCATION (t);
6539 else
6541 location_t *locp = (location_t *) wi->info;
6542 location = *locp;
6545 *walk_subtree = TRUE;
6547 if (TREE_CODE (t) == ARRAY_REF)
6548 check_array_ref (location, t, false /*ignore_off_by_one*/);
6550 if (TREE_CODE (t) == MEM_REF
6551 || (TREE_CODE (t) == RETURN_EXPR && TREE_OPERAND (t, 0)))
6552 search_for_addr_array (TREE_OPERAND (t, 0), location);
6554 if (TREE_CODE (t) == ADDR_EXPR)
6555 *walk_subtree = FALSE;
6557 return NULL_TREE;
6560 /* Walk over all statements of all reachable BBs and call check_array_bounds
6561 on them. */
6563 static void
6564 check_all_array_refs (void)
6566 basic_block bb;
6567 gimple_stmt_iterator si;
6569 FOR_EACH_BB_FN (bb, cfun)
6571 edge_iterator ei;
6572 edge e;
6573 bool executable = false;
6575 /* Skip blocks that were found to be unreachable. */
6576 FOR_EACH_EDGE (e, ei, bb->preds)
6577 executable |= !!(e->flags & EDGE_EXECUTABLE);
6578 if (!executable)
6579 continue;
6581 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
6583 gimple stmt = gsi_stmt (si);
6584 struct walk_stmt_info wi;
6585 if (!gimple_has_location (stmt))
6586 continue;
6588 if (is_gimple_call (stmt))
6590 size_t i;
6591 size_t n = gimple_call_num_args (stmt);
6592 for (i = 0; i < n; i++)
6594 tree arg = gimple_call_arg (stmt, i);
6595 search_for_addr_array (arg, gimple_location (stmt));
6598 else
6600 memset (&wi, 0, sizeof (wi));
6601 wi.info = CONST_CAST (void *, (const void *)
6602 gimple_location_ptr (stmt));
6604 walk_gimple_op (gsi_stmt (si),
6605 check_array_bounds,
6606 &wi);
6612 /* Return true if all imm uses of VAR are either in STMT, or
6613 feed (optionally through a chain of single imm uses) GIMPLE_COND
6614 in basic block COND_BB. */
6616 static bool
6617 all_imm_uses_in_stmt_or_feed_cond (tree var, gimple stmt, basic_block cond_bb)
6619 use_operand_p use_p, use2_p;
6620 imm_use_iterator iter;
6622 FOR_EACH_IMM_USE_FAST (use_p, iter, var)
6623 if (USE_STMT (use_p) != stmt)
6625 gimple use_stmt = USE_STMT (use_p), use_stmt2;
6626 if (is_gimple_debug (use_stmt))
6627 continue;
6628 while (is_gimple_assign (use_stmt)
6629 && TREE_CODE (gimple_assign_lhs (use_stmt)) == SSA_NAME
6630 && single_imm_use (gimple_assign_lhs (use_stmt),
6631 &use2_p, &use_stmt2))
6632 use_stmt = use_stmt2;
6633 if (gimple_code (use_stmt) != GIMPLE_COND
6634 || gimple_bb (use_stmt) != cond_bb)
6635 return false;
6637 return true;
6640 /* Handle
6641 _4 = x_3 & 31;
6642 if (_4 != 0)
6643 goto <bb 6>;
6644 else
6645 goto <bb 7>;
6646 <bb 6>:
6647 __builtin_unreachable ();
6648 <bb 7>:
6649 x_5 = ASSERT_EXPR <x_3, ...>;
6650 If x_3 has no other immediate uses (checked by caller),
6651 var is the x_3 var from ASSERT_EXPR, we can clear low 5 bits
6652 from the non-zero bitmask. */
6654 static void
6655 maybe_set_nonzero_bits (basic_block bb, tree var)
6657 edge e = single_pred_edge (bb);
6658 basic_block cond_bb = e->src;
6659 gimple stmt = last_stmt (cond_bb);
6660 tree cst;
6662 if (stmt == NULL
6663 || gimple_code (stmt) != GIMPLE_COND
6664 || gimple_cond_code (stmt) != ((e->flags & EDGE_TRUE_VALUE)
6665 ? EQ_EXPR : NE_EXPR)
6666 || TREE_CODE (gimple_cond_lhs (stmt)) != SSA_NAME
6667 || !integer_zerop (gimple_cond_rhs (stmt)))
6668 return;
6670 stmt = SSA_NAME_DEF_STMT (gimple_cond_lhs (stmt));
6671 if (!is_gimple_assign (stmt)
6672 || gimple_assign_rhs_code (stmt) != BIT_AND_EXPR
6673 || TREE_CODE (gimple_assign_rhs2 (stmt)) != INTEGER_CST)
6674 return;
6675 if (gimple_assign_rhs1 (stmt) != var)
6677 gimple stmt2;
6679 if (TREE_CODE (gimple_assign_rhs1 (stmt)) != SSA_NAME)
6680 return;
6681 stmt2 = SSA_NAME_DEF_STMT (gimple_assign_rhs1 (stmt));
6682 if (!gimple_assign_cast_p (stmt2)
6683 || gimple_assign_rhs1 (stmt2) != var
6684 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (stmt2))
6685 || (TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (stmt)))
6686 != TYPE_PRECISION (TREE_TYPE (var))))
6687 return;
6689 cst = gimple_assign_rhs2 (stmt);
6690 set_nonzero_bits (var, wi::bit_and_not (get_nonzero_bits (var), cst));
6693 /* Convert range assertion expressions into the implied copies and
6694 copy propagate away the copies. Doing the trivial copy propagation
6695 here avoids the need to run the full copy propagation pass after
6696 VRP.
6698 FIXME, this will eventually lead to copy propagation removing the
6699 names that had useful range information attached to them. For
6700 instance, if we had the assertion N_i = ASSERT_EXPR <N_j, N_j > 3>,
6701 then N_i will have the range [3, +INF].
6703 However, by converting the assertion into the implied copy
6704 operation N_i = N_j, we will then copy-propagate N_j into the uses
6705 of N_i and lose the range information. We may want to hold on to
6706 ASSERT_EXPRs a little while longer as the ranges could be used in
6707 things like jump threading.
6709 The problem with keeping ASSERT_EXPRs around is that passes after
6710 VRP need to handle them appropriately.
6712 Another approach would be to make the range information a first
6713 class property of the SSA_NAME so that it can be queried from
6714 any pass. This is made somewhat more complex by the need for
6715 multiple ranges to be associated with one SSA_NAME. */
6717 static void
6718 remove_range_assertions (void)
6720 basic_block bb;
6721 gimple_stmt_iterator si;
6722 /* 1 if looking at ASSERT_EXPRs immediately at the beginning of
6723 a basic block preceeded by GIMPLE_COND branching to it and
6724 __builtin_trap, -1 if not yet checked, 0 otherwise. */
6725 int is_unreachable;
6727 /* Note that the BSI iterator bump happens at the bottom of the
6728 loop and no bump is necessary if we're removing the statement
6729 referenced by the current BSI. */
6730 FOR_EACH_BB_FN (bb, cfun)
6731 for (si = gsi_after_labels (bb), is_unreachable = -1; !gsi_end_p (si);)
6733 gimple stmt = gsi_stmt (si);
6734 gimple use_stmt;
6736 if (is_gimple_assign (stmt)
6737 && gimple_assign_rhs_code (stmt) == ASSERT_EXPR)
6739 tree lhs = gimple_assign_lhs (stmt);
6740 tree rhs = gimple_assign_rhs1 (stmt);
6741 tree var;
6742 tree cond = fold (ASSERT_EXPR_COND (rhs));
6743 use_operand_p use_p;
6744 imm_use_iterator iter;
6746 gcc_assert (cond != boolean_false_node);
6748 var = ASSERT_EXPR_VAR (rhs);
6749 gcc_assert (TREE_CODE (var) == SSA_NAME);
6751 if (!POINTER_TYPE_P (TREE_TYPE (lhs))
6752 && SSA_NAME_RANGE_INFO (lhs))
6754 if (is_unreachable == -1)
6756 is_unreachable = 0;
6757 if (single_pred_p (bb)
6758 && assert_unreachable_fallthru_edge_p
6759 (single_pred_edge (bb)))
6760 is_unreachable = 1;
6762 /* Handle
6763 if (x_7 >= 10 && x_7 < 20)
6764 __builtin_unreachable ();
6765 x_8 = ASSERT_EXPR <x_7, ...>;
6766 if the only uses of x_7 are in the ASSERT_EXPR and
6767 in the condition. In that case, we can copy the
6768 range info from x_8 computed in this pass also
6769 for x_7. */
6770 if (is_unreachable
6771 && all_imm_uses_in_stmt_or_feed_cond (var, stmt,
6772 single_pred (bb)))
6774 set_range_info (var, SSA_NAME_RANGE_TYPE (lhs),
6775 SSA_NAME_RANGE_INFO (lhs)->get_min (),
6776 SSA_NAME_RANGE_INFO (lhs)->get_max ());
6777 maybe_set_nonzero_bits (bb, var);
6781 /* Propagate the RHS into every use of the LHS. */
6782 FOR_EACH_IMM_USE_STMT (use_stmt, iter, lhs)
6783 FOR_EACH_IMM_USE_ON_STMT (use_p, iter)
6784 SET_USE (use_p, var);
6786 /* And finally, remove the copy, it is not needed. */
6787 gsi_remove (&si, true);
6788 release_defs (stmt);
6790 else
6792 if (!is_gimple_debug (gsi_stmt (si)))
6793 is_unreachable = 0;
6794 gsi_next (&si);
6800 /* Return true if STMT is interesting for VRP. */
6802 static bool
6803 stmt_interesting_for_vrp (gimple stmt)
6805 if (gimple_code (stmt) == GIMPLE_PHI)
6807 tree res = gimple_phi_result (stmt);
6808 return (!virtual_operand_p (res)
6809 && (INTEGRAL_TYPE_P (TREE_TYPE (res))
6810 || POINTER_TYPE_P (TREE_TYPE (res))));
6812 else if (is_gimple_assign (stmt) || is_gimple_call (stmt))
6814 tree lhs = gimple_get_lhs (stmt);
6816 /* In general, assignments with virtual operands are not useful
6817 for deriving ranges, with the obvious exception of calls to
6818 builtin functions. */
6819 if (lhs && TREE_CODE (lhs) == SSA_NAME
6820 && (INTEGRAL_TYPE_P (TREE_TYPE (lhs))
6821 || POINTER_TYPE_P (TREE_TYPE (lhs)))
6822 && (is_gimple_call (stmt)
6823 || !gimple_vuse (stmt)))
6824 return true;
6826 else if (gimple_code (stmt) == GIMPLE_COND
6827 || gimple_code (stmt) == GIMPLE_SWITCH)
6828 return true;
6830 return false;
6834 /* Initialize local data structures for VRP. */
6836 static void
6837 vrp_initialize (void)
6839 basic_block bb;
6841 values_propagated = false;
6842 num_vr_values = num_ssa_names;
6843 vr_value = XCNEWVEC (value_range_t *, num_vr_values);
6844 vr_phi_edge_counts = XCNEWVEC (int, num_ssa_names);
6846 FOR_EACH_BB_FN (bb, cfun)
6848 gimple_stmt_iterator si;
6850 for (si = gsi_start_phis (bb); !gsi_end_p (si); gsi_next (&si))
6852 gimple phi = gsi_stmt (si);
6853 if (!stmt_interesting_for_vrp (phi))
6855 tree lhs = PHI_RESULT (phi);
6856 set_value_range_to_varying (get_value_range (lhs));
6857 prop_set_simulate_again (phi, false);
6859 else
6860 prop_set_simulate_again (phi, true);
6863 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
6865 gimple stmt = gsi_stmt (si);
6867 /* If the statement is a control insn, then we do not
6868 want to avoid simulating the statement once. Failure
6869 to do so means that those edges will never get added. */
6870 if (stmt_ends_bb_p (stmt))
6871 prop_set_simulate_again (stmt, true);
6872 else if (!stmt_interesting_for_vrp (stmt))
6874 ssa_op_iter i;
6875 tree def;
6876 FOR_EACH_SSA_TREE_OPERAND (def, stmt, i, SSA_OP_DEF)
6877 set_value_range_to_varying (get_value_range (def));
6878 prop_set_simulate_again (stmt, false);
6880 else
6881 prop_set_simulate_again (stmt, true);
6886 /* Return the singleton value-range for NAME or NAME. */
6888 static inline tree
6889 vrp_valueize (tree name)
6891 if (TREE_CODE (name) == SSA_NAME)
6893 value_range_t *vr = get_value_range (name);
6894 if (vr->type == VR_RANGE
6895 && (vr->min == vr->max
6896 || operand_equal_p (vr->min, vr->max, 0)))
6897 return vr->min;
6899 return name;
6902 /* Visit assignment STMT. If it produces an interesting range, record
6903 the SSA name in *OUTPUT_P. */
6905 static enum ssa_prop_result
6906 vrp_visit_assignment_or_call (gimple stmt, tree *output_p)
6908 tree def, lhs;
6909 ssa_op_iter iter;
6910 enum gimple_code code = gimple_code (stmt);
6911 lhs = gimple_get_lhs (stmt);
6913 /* We only keep track of ranges in integral and pointer types. */
6914 if (TREE_CODE (lhs) == SSA_NAME
6915 && ((INTEGRAL_TYPE_P (TREE_TYPE (lhs))
6916 /* It is valid to have NULL MIN/MAX values on a type. See
6917 build_range_type. */
6918 && TYPE_MIN_VALUE (TREE_TYPE (lhs))
6919 && TYPE_MAX_VALUE (TREE_TYPE (lhs)))
6920 || POINTER_TYPE_P (TREE_TYPE (lhs))))
6922 value_range_t new_vr = VR_INITIALIZER;
6924 /* Try folding the statement to a constant first. */
6925 tree tem = gimple_fold_stmt_to_constant (stmt, vrp_valueize);
6926 if (tem)
6927 set_value_range_to_value (&new_vr, tem, NULL);
6928 /* Then dispatch to value-range extracting functions. */
6929 else if (code == GIMPLE_CALL)
6930 extract_range_basic (&new_vr, stmt);
6931 else
6932 extract_range_from_assignment (&new_vr, stmt);
6934 if (update_value_range (lhs, &new_vr))
6936 *output_p = lhs;
6938 if (dump_file && (dump_flags & TDF_DETAILS))
6940 fprintf (dump_file, "Found new range for ");
6941 print_generic_expr (dump_file, lhs, 0);
6942 fprintf (dump_file, ": ");
6943 dump_value_range (dump_file, &new_vr);
6944 fprintf (dump_file, "\n");
6947 if (new_vr.type == VR_VARYING)
6948 return SSA_PROP_VARYING;
6950 return SSA_PROP_INTERESTING;
6953 return SSA_PROP_NOT_INTERESTING;
6956 /* Every other statement produces no useful ranges. */
6957 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_DEF)
6958 set_value_range_to_varying (get_value_range (def));
6960 return SSA_PROP_VARYING;
6963 /* Helper that gets the value range of the SSA_NAME with version I
6964 or a symbolic range containing the SSA_NAME only if the value range
6965 is varying or undefined. */
6967 static inline value_range_t
6968 get_vr_for_comparison (int i)
6970 value_range_t vr = *get_value_range (ssa_name (i));
6972 /* If name N_i does not have a valid range, use N_i as its own
6973 range. This allows us to compare against names that may
6974 have N_i in their ranges. */
6975 if (vr.type == VR_VARYING || vr.type == VR_UNDEFINED)
6977 vr.type = VR_RANGE;
6978 vr.min = ssa_name (i);
6979 vr.max = ssa_name (i);
6982 return vr;
6985 /* Compare all the value ranges for names equivalent to VAR with VAL
6986 using comparison code COMP. Return the same value returned by
6987 compare_range_with_value, including the setting of
6988 *STRICT_OVERFLOW_P. */
6990 static tree
6991 compare_name_with_value (enum tree_code comp, tree var, tree val,
6992 bool *strict_overflow_p)
6994 bitmap_iterator bi;
6995 unsigned i;
6996 bitmap e;
6997 tree retval, t;
6998 int used_strict_overflow;
6999 bool sop;
7000 value_range_t equiv_vr;
7002 /* Get the set of equivalences for VAR. */
7003 e = get_value_range (var)->equiv;
7005 /* Start at -1. Set it to 0 if we do a comparison without relying
7006 on overflow, or 1 if all comparisons rely on overflow. */
7007 used_strict_overflow = -1;
7009 /* Compare vars' value range with val. */
7010 equiv_vr = get_vr_for_comparison (SSA_NAME_VERSION (var));
7011 sop = false;
7012 retval = compare_range_with_value (comp, &equiv_vr, val, &sop);
7013 if (retval)
7014 used_strict_overflow = sop ? 1 : 0;
7016 /* If the equiv set is empty we have done all work we need to do. */
7017 if (e == NULL)
7019 if (retval
7020 && used_strict_overflow > 0)
7021 *strict_overflow_p = true;
7022 return retval;
7025 EXECUTE_IF_SET_IN_BITMAP (e, 0, i, bi)
7027 equiv_vr = get_vr_for_comparison (i);
7028 sop = false;
7029 t = compare_range_with_value (comp, &equiv_vr, val, &sop);
7030 if (t)
7032 /* If we get different answers from different members
7033 of the equivalence set this check must be in a dead
7034 code region. Folding it to a trap representation
7035 would be correct here. For now just return don't-know. */
7036 if (retval != NULL
7037 && t != retval)
7039 retval = NULL_TREE;
7040 break;
7042 retval = t;
7044 if (!sop)
7045 used_strict_overflow = 0;
7046 else if (used_strict_overflow < 0)
7047 used_strict_overflow = 1;
7051 if (retval
7052 && used_strict_overflow > 0)
7053 *strict_overflow_p = true;
7055 return retval;
7059 /* Given a comparison code COMP and names N1 and N2, compare all the
7060 ranges equivalent to N1 against all the ranges equivalent to N2
7061 to determine the value of N1 COMP N2. Return the same value
7062 returned by compare_ranges. Set *STRICT_OVERFLOW_P to indicate
7063 whether we relied on an overflow infinity in the comparison. */
7066 static tree
7067 compare_names (enum tree_code comp, tree n1, tree n2,
7068 bool *strict_overflow_p)
7070 tree t, retval;
7071 bitmap e1, e2;
7072 bitmap_iterator bi1, bi2;
7073 unsigned i1, i2;
7074 int used_strict_overflow;
7075 static bitmap_obstack *s_obstack = NULL;
7076 static bitmap s_e1 = NULL, s_e2 = NULL;
7078 /* Compare the ranges of every name equivalent to N1 against the
7079 ranges of every name equivalent to N2. */
7080 e1 = get_value_range (n1)->equiv;
7081 e2 = get_value_range (n2)->equiv;
7083 /* Use the fake bitmaps if e1 or e2 are not available. */
7084 if (s_obstack == NULL)
7086 s_obstack = XNEW (bitmap_obstack);
7087 bitmap_obstack_initialize (s_obstack);
7088 s_e1 = BITMAP_ALLOC (s_obstack);
7089 s_e2 = BITMAP_ALLOC (s_obstack);
7091 if (e1 == NULL)
7092 e1 = s_e1;
7093 if (e2 == NULL)
7094 e2 = s_e2;
7096 /* Add N1 and N2 to their own set of equivalences to avoid
7097 duplicating the body of the loop just to check N1 and N2
7098 ranges. */
7099 bitmap_set_bit (e1, SSA_NAME_VERSION (n1));
7100 bitmap_set_bit (e2, SSA_NAME_VERSION (n2));
7102 /* If the equivalence sets have a common intersection, then the two
7103 names can be compared without checking their ranges. */
7104 if (bitmap_intersect_p (e1, e2))
7106 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
7107 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
7109 return (comp == EQ_EXPR || comp == GE_EXPR || comp == LE_EXPR)
7110 ? boolean_true_node
7111 : boolean_false_node;
7114 /* Start at -1. Set it to 0 if we do a comparison without relying
7115 on overflow, or 1 if all comparisons rely on overflow. */
7116 used_strict_overflow = -1;
7118 /* Otherwise, compare all the equivalent ranges. First, add N1 and
7119 N2 to their own set of equivalences to avoid duplicating the body
7120 of the loop just to check N1 and N2 ranges. */
7121 EXECUTE_IF_SET_IN_BITMAP (e1, 0, i1, bi1)
7123 value_range_t vr1 = get_vr_for_comparison (i1);
7125 t = retval = NULL_TREE;
7126 EXECUTE_IF_SET_IN_BITMAP (e2, 0, i2, bi2)
7128 bool sop = false;
7130 value_range_t vr2 = get_vr_for_comparison (i2);
7132 t = compare_ranges (comp, &vr1, &vr2, &sop);
7133 if (t)
7135 /* If we get different answers from different members
7136 of the equivalence set this check must be in a dead
7137 code region. Folding it to a trap representation
7138 would be correct here. For now just return don't-know. */
7139 if (retval != NULL
7140 && t != retval)
7142 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
7143 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
7144 return NULL_TREE;
7146 retval = t;
7148 if (!sop)
7149 used_strict_overflow = 0;
7150 else if (used_strict_overflow < 0)
7151 used_strict_overflow = 1;
7155 if (retval)
7157 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
7158 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
7159 if (used_strict_overflow > 0)
7160 *strict_overflow_p = true;
7161 return retval;
7165 /* None of the equivalent ranges are useful in computing this
7166 comparison. */
7167 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
7168 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
7169 return NULL_TREE;
7172 /* Helper function for vrp_evaluate_conditional_warnv. */
7174 static tree
7175 vrp_evaluate_conditional_warnv_with_ops_using_ranges (enum tree_code code,
7176 tree op0, tree op1,
7177 bool * strict_overflow_p)
7179 value_range_t *vr0, *vr1;
7181 vr0 = (TREE_CODE (op0) == SSA_NAME) ? get_value_range (op0) : NULL;
7182 vr1 = (TREE_CODE (op1) == SSA_NAME) ? get_value_range (op1) : NULL;
7184 tree res = NULL_TREE;
7185 if (vr0 && vr1)
7186 res = compare_ranges (code, vr0, vr1, strict_overflow_p);
7187 if (!res && vr0)
7188 res = compare_range_with_value (code, vr0, op1, strict_overflow_p);
7189 if (!res && vr1)
7190 res = (compare_range_with_value
7191 (swap_tree_comparison (code), vr1, op0, strict_overflow_p));
7192 return res;
7195 /* Helper function for vrp_evaluate_conditional_warnv. */
7197 static tree
7198 vrp_evaluate_conditional_warnv_with_ops (enum tree_code code, tree op0,
7199 tree op1, bool use_equiv_p,
7200 bool *strict_overflow_p, bool *only_ranges)
7202 tree ret;
7203 if (only_ranges)
7204 *only_ranges = true;
7206 /* We only deal with integral and pointer types. */
7207 if (!INTEGRAL_TYPE_P (TREE_TYPE (op0))
7208 && !POINTER_TYPE_P (TREE_TYPE (op0)))
7209 return NULL_TREE;
7211 if (use_equiv_p)
7213 if (only_ranges
7214 && (ret = vrp_evaluate_conditional_warnv_with_ops_using_ranges
7215 (code, op0, op1, strict_overflow_p)))
7216 return ret;
7217 *only_ranges = false;
7218 if (TREE_CODE (op0) == SSA_NAME && TREE_CODE (op1) == SSA_NAME)
7219 return compare_names (code, op0, op1, strict_overflow_p);
7220 else if (TREE_CODE (op0) == SSA_NAME)
7221 return compare_name_with_value (code, op0, op1, strict_overflow_p);
7222 else if (TREE_CODE (op1) == SSA_NAME)
7223 return (compare_name_with_value
7224 (swap_tree_comparison (code), op1, op0, strict_overflow_p));
7226 else
7227 return vrp_evaluate_conditional_warnv_with_ops_using_ranges (code, op0, op1,
7228 strict_overflow_p);
7229 return NULL_TREE;
7232 /* Given (CODE OP0 OP1) within STMT, try to simplify it based on value range
7233 information. Return NULL if the conditional can not be evaluated.
7234 The ranges of all the names equivalent with the operands in COND
7235 will be used when trying to compute the value. If the result is
7236 based on undefined signed overflow, issue a warning if
7237 appropriate. */
7239 static tree
7240 vrp_evaluate_conditional (enum tree_code code, tree op0, tree op1, gimple stmt)
7242 bool sop;
7243 tree ret;
7244 bool only_ranges;
7246 /* Some passes and foldings leak constants with overflow flag set
7247 into the IL. Avoid doing wrong things with these and bail out. */
7248 if ((TREE_CODE (op0) == INTEGER_CST
7249 && TREE_OVERFLOW (op0))
7250 || (TREE_CODE (op1) == INTEGER_CST
7251 && TREE_OVERFLOW (op1)))
7252 return NULL_TREE;
7254 sop = false;
7255 ret = vrp_evaluate_conditional_warnv_with_ops (code, op0, op1, true, &sop,
7256 &only_ranges);
7258 if (ret && sop)
7260 enum warn_strict_overflow_code wc;
7261 const char* warnmsg;
7263 if (is_gimple_min_invariant (ret))
7265 wc = WARN_STRICT_OVERFLOW_CONDITIONAL;
7266 warnmsg = G_("assuming signed overflow does not occur when "
7267 "simplifying conditional to constant");
7269 else
7271 wc = WARN_STRICT_OVERFLOW_COMPARISON;
7272 warnmsg = G_("assuming signed overflow does not occur when "
7273 "simplifying conditional");
7276 if (issue_strict_overflow_warning (wc))
7278 location_t location;
7280 if (!gimple_has_location (stmt))
7281 location = input_location;
7282 else
7283 location = gimple_location (stmt);
7284 warning_at (location, OPT_Wstrict_overflow, "%s", warnmsg);
7288 if (warn_type_limits
7289 && ret && only_ranges
7290 && TREE_CODE_CLASS (code) == tcc_comparison
7291 && TREE_CODE (op0) == SSA_NAME)
7293 /* If the comparison is being folded and the operand on the LHS
7294 is being compared against a constant value that is outside of
7295 the natural range of OP0's type, then the predicate will
7296 always fold regardless of the value of OP0. If -Wtype-limits
7297 was specified, emit a warning. */
7298 tree type = TREE_TYPE (op0);
7299 value_range_t *vr0 = get_value_range (op0);
7301 if (vr0->type != VR_VARYING
7302 && INTEGRAL_TYPE_P (type)
7303 && vrp_val_is_min (vr0->min)
7304 && vrp_val_is_max (vr0->max)
7305 && is_gimple_min_invariant (op1))
7307 location_t location;
7309 if (!gimple_has_location (stmt))
7310 location = input_location;
7311 else
7312 location = gimple_location (stmt);
7314 warning_at (location, OPT_Wtype_limits,
7315 integer_zerop (ret)
7316 ? G_("comparison always false "
7317 "due to limited range of data type")
7318 : G_("comparison always true "
7319 "due to limited range of data type"));
7323 return ret;
7327 /* Visit conditional statement STMT. If we can determine which edge
7328 will be taken out of STMT's basic block, record it in
7329 *TAKEN_EDGE_P and return SSA_PROP_INTERESTING. Otherwise, return
7330 SSA_PROP_VARYING. */
7332 static enum ssa_prop_result
7333 vrp_visit_cond_stmt (gimple stmt, edge *taken_edge_p)
7335 tree val;
7336 bool sop;
7338 *taken_edge_p = NULL;
7340 if (dump_file && (dump_flags & TDF_DETAILS))
7342 tree use;
7343 ssa_op_iter i;
7345 fprintf (dump_file, "\nVisiting conditional with predicate: ");
7346 print_gimple_stmt (dump_file, stmt, 0, 0);
7347 fprintf (dump_file, "\nWith known ranges\n");
7349 FOR_EACH_SSA_TREE_OPERAND (use, stmt, i, SSA_OP_USE)
7351 fprintf (dump_file, "\t");
7352 print_generic_expr (dump_file, use, 0);
7353 fprintf (dump_file, ": ");
7354 dump_value_range (dump_file, vr_value[SSA_NAME_VERSION (use)]);
7357 fprintf (dump_file, "\n");
7360 /* Compute the value of the predicate COND by checking the known
7361 ranges of each of its operands.
7363 Note that we cannot evaluate all the equivalent ranges here
7364 because those ranges may not yet be final and with the current
7365 propagation strategy, we cannot determine when the value ranges
7366 of the names in the equivalence set have changed.
7368 For instance, given the following code fragment
7370 i_5 = PHI <8, i_13>
7372 i_14 = ASSERT_EXPR <i_5, i_5 != 0>
7373 if (i_14 == 1)
7376 Assume that on the first visit to i_14, i_5 has the temporary
7377 range [8, 8] because the second argument to the PHI function is
7378 not yet executable. We derive the range ~[0, 0] for i_14 and the
7379 equivalence set { i_5 }. So, when we visit 'if (i_14 == 1)' for
7380 the first time, since i_14 is equivalent to the range [8, 8], we
7381 determine that the predicate is always false.
7383 On the next round of propagation, i_13 is determined to be
7384 VARYING, which causes i_5 to drop down to VARYING. So, another
7385 visit to i_14 is scheduled. In this second visit, we compute the
7386 exact same range and equivalence set for i_14, namely ~[0, 0] and
7387 { i_5 }. But we did not have the previous range for i_5
7388 registered, so vrp_visit_assignment thinks that the range for
7389 i_14 has not changed. Therefore, the predicate 'if (i_14 == 1)'
7390 is not visited again, which stops propagation from visiting
7391 statements in the THEN clause of that if().
7393 To properly fix this we would need to keep the previous range
7394 value for the names in the equivalence set. This way we would've
7395 discovered that from one visit to the other i_5 changed from
7396 range [8, 8] to VR_VARYING.
7398 However, fixing this apparent limitation may not be worth the
7399 additional checking. Testing on several code bases (GCC, DLV,
7400 MICO, TRAMP3D and SPEC2000) showed that doing this results in
7401 4 more predicates folded in SPEC. */
7402 sop = false;
7404 val = vrp_evaluate_conditional_warnv_with_ops (gimple_cond_code (stmt),
7405 gimple_cond_lhs (stmt),
7406 gimple_cond_rhs (stmt),
7407 false, &sop, NULL);
7408 if (val)
7410 if (!sop)
7411 *taken_edge_p = find_taken_edge (gimple_bb (stmt), val);
7412 else
7414 if (dump_file && (dump_flags & TDF_DETAILS))
7415 fprintf (dump_file,
7416 "\nIgnoring predicate evaluation because "
7417 "it assumes that signed overflow is undefined");
7418 val = NULL_TREE;
7422 if (dump_file && (dump_flags & TDF_DETAILS))
7424 fprintf (dump_file, "\nPredicate evaluates to: ");
7425 if (val == NULL_TREE)
7426 fprintf (dump_file, "DON'T KNOW\n");
7427 else
7428 print_generic_stmt (dump_file, val, 0);
7431 return (*taken_edge_p) ? SSA_PROP_INTERESTING : SSA_PROP_VARYING;
7434 /* Searches the case label vector VEC for the index *IDX of the CASE_LABEL
7435 that includes the value VAL. The search is restricted to the range
7436 [START_IDX, n - 1] where n is the size of VEC.
7438 If there is a CASE_LABEL for VAL, its index is placed in IDX and true is
7439 returned.
7441 If there is no CASE_LABEL for VAL and there is one that is larger than VAL,
7442 it is placed in IDX and false is returned.
7444 If VAL is larger than any CASE_LABEL, n is placed on IDX and false is
7445 returned. */
7447 static bool
7448 find_case_label_index (gimple stmt, size_t start_idx, tree val, size_t *idx)
7450 size_t n = gimple_switch_num_labels (stmt);
7451 size_t low, high;
7453 /* Find case label for minimum of the value range or the next one.
7454 At each iteration we are searching in [low, high - 1]. */
7456 for (low = start_idx, high = n; high != low; )
7458 tree t;
7459 int cmp;
7460 /* Note that i != high, so we never ask for n. */
7461 size_t i = (high + low) / 2;
7462 t = gimple_switch_label (stmt, i);
7464 /* Cache the result of comparing CASE_LOW and val. */
7465 cmp = tree_int_cst_compare (CASE_LOW (t), val);
7467 if (cmp == 0)
7469 /* Ranges cannot be empty. */
7470 *idx = i;
7471 return true;
7473 else if (cmp > 0)
7474 high = i;
7475 else
7477 low = i + 1;
7478 if (CASE_HIGH (t) != NULL
7479 && tree_int_cst_compare (CASE_HIGH (t), val) >= 0)
7481 *idx = i;
7482 return true;
7487 *idx = high;
7488 return false;
7491 /* Searches the case label vector VEC for the range of CASE_LABELs that is used
7492 for values between MIN and MAX. The first index is placed in MIN_IDX. The
7493 last index is placed in MAX_IDX. If the range of CASE_LABELs is empty
7494 then MAX_IDX < MIN_IDX.
7495 Returns true if the default label is not needed. */
7497 static bool
7498 find_case_label_range (gimple stmt, tree min, tree max, size_t *min_idx,
7499 size_t *max_idx)
7501 size_t i, j;
7502 bool min_take_default = !find_case_label_index (stmt, 1, min, &i);
7503 bool max_take_default = !find_case_label_index (stmt, i, max, &j);
7505 if (i == j
7506 && min_take_default
7507 && max_take_default)
7509 /* Only the default case label reached.
7510 Return an empty range. */
7511 *min_idx = 1;
7512 *max_idx = 0;
7513 return false;
7515 else
7517 bool take_default = min_take_default || max_take_default;
7518 tree low, high;
7519 size_t k;
7521 if (max_take_default)
7522 j--;
7524 /* If the case label range is continuous, we do not need
7525 the default case label. Verify that. */
7526 high = CASE_LOW (gimple_switch_label (stmt, i));
7527 if (CASE_HIGH (gimple_switch_label (stmt, i)))
7528 high = CASE_HIGH (gimple_switch_label (stmt, i));
7529 for (k = i + 1; k <= j; ++k)
7531 low = CASE_LOW (gimple_switch_label (stmt, k));
7532 if (!integer_onep (int_const_binop (MINUS_EXPR, low, high)))
7534 take_default = true;
7535 break;
7537 high = low;
7538 if (CASE_HIGH (gimple_switch_label (stmt, k)))
7539 high = CASE_HIGH (gimple_switch_label (stmt, k));
7542 *min_idx = i;
7543 *max_idx = j;
7544 return !take_default;
7548 /* Searches the case label vector VEC for the ranges of CASE_LABELs that are
7549 used in range VR. The indices are placed in MIN_IDX1, MAX_IDX, MIN_IDX2 and
7550 MAX_IDX2. If the ranges of CASE_LABELs are empty then MAX_IDX1 < MIN_IDX1.
7551 Returns true if the default label is not needed. */
7553 static bool
7554 find_case_label_ranges (gimple stmt, value_range_t *vr, size_t *min_idx1,
7555 size_t *max_idx1, size_t *min_idx2,
7556 size_t *max_idx2)
7558 size_t i, j, k, l;
7559 unsigned int n = gimple_switch_num_labels (stmt);
7560 bool take_default;
7561 tree case_low, case_high;
7562 tree min = vr->min, max = vr->max;
7564 gcc_checking_assert (vr->type == VR_RANGE || vr->type == VR_ANTI_RANGE);
7566 take_default = !find_case_label_range (stmt, min, max, &i, &j);
7568 /* Set second range to emtpy. */
7569 *min_idx2 = 1;
7570 *max_idx2 = 0;
7572 if (vr->type == VR_RANGE)
7574 *min_idx1 = i;
7575 *max_idx1 = j;
7576 return !take_default;
7579 /* Set first range to all case labels. */
7580 *min_idx1 = 1;
7581 *max_idx1 = n - 1;
7583 if (i > j)
7584 return false;
7586 /* Make sure all the values of case labels [i , j] are contained in
7587 range [MIN, MAX]. */
7588 case_low = CASE_LOW (gimple_switch_label (stmt, i));
7589 case_high = CASE_HIGH (gimple_switch_label (stmt, j));
7590 if (tree_int_cst_compare (case_low, min) < 0)
7591 i += 1;
7592 if (case_high != NULL_TREE
7593 && tree_int_cst_compare (max, case_high) < 0)
7594 j -= 1;
7596 if (i > j)
7597 return false;
7599 /* If the range spans case labels [i, j], the corresponding anti-range spans
7600 the labels [1, i - 1] and [j + 1, n - 1]. */
7601 k = j + 1;
7602 l = n - 1;
7603 if (k > l)
7605 k = 1;
7606 l = 0;
7609 j = i - 1;
7610 i = 1;
7611 if (i > j)
7613 i = k;
7614 j = l;
7615 k = 1;
7616 l = 0;
7619 *min_idx1 = i;
7620 *max_idx1 = j;
7621 *min_idx2 = k;
7622 *max_idx2 = l;
7623 return false;
7626 /* Visit switch statement STMT. If we can determine which edge
7627 will be taken out of STMT's basic block, record it in
7628 *TAKEN_EDGE_P and return SSA_PROP_INTERESTING. Otherwise, return
7629 SSA_PROP_VARYING. */
7631 static enum ssa_prop_result
7632 vrp_visit_switch_stmt (gimple stmt, edge *taken_edge_p)
7634 tree op, val;
7635 value_range_t *vr;
7636 size_t i = 0, j = 0, k, l;
7637 bool take_default;
7639 *taken_edge_p = NULL;
7640 op = gimple_switch_index (stmt);
7641 if (TREE_CODE (op) != SSA_NAME)
7642 return SSA_PROP_VARYING;
7644 vr = get_value_range (op);
7645 if (dump_file && (dump_flags & TDF_DETAILS))
7647 fprintf (dump_file, "\nVisiting switch expression with operand ");
7648 print_generic_expr (dump_file, op, 0);
7649 fprintf (dump_file, " with known range ");
7650 dump_value_range (dump_file, vr);
7651 fprintf (dump_file, "\n");
7654 if ((vr->type != VR_RANGE
7655 && vr->type != VR_ANTI_RANGE)
7656 || symbolic_range_p (vr))
7657 return SSA_PROP_VARYING;
7659 /* Find the single edge that is taken from the switch expression. */
7660 take_default = !find_case_label_ranges (stmt, vr, &i, &j, &k, &l);
7662 /* Check if the range spans no CASE_LABEL. If so, we only reach the default
7663 label */
7664 if (j < i)
7666 gcc_assert (take_default);
7667 val = gimple_switch_default_label (stmt);
7669 else
7671 /* Check if labels with index i to j and maybe the default label
7672 are all reaching the same label. */
7674 val = gimple_switch_label (stmt, i);
7675 if (take_default
7676 && CASE_LABEL (gimple_switch_default_label (stmt))
7677 != CASE_LABEL (val))
7679 if (dump_file && (dump_flags & TDF_DETAILS))
7680 fprintf (dump_file, " not a single destination for this "
7681 "range\n");
7682 return SSA_PROP_VARYING;
7684 for (++i; i <= j; ++i)
7686 if (CASE_LABEL (gimple_switch_label (stmt, i)) != CASE_LABEL (val))
7688 if (dump_file && (dump_flags & TDF_DETAILS))
7689 fprintf (dump_file, " not a single destination for this "
7690 "range\n");
7691 return SSA_PROP_VARYING;
7694 for (; k <= l; ++k)
7696 if (CASE_LABEL (gimple_switch_label (stmt, k)) != CASE_LABEL (val))
7698 if (dump_file && (dump_flags & TDF_DETAILS))
7699 fprintf (dump_file, " not a single destination for this "
7700 "range\n");
7701 return SSA_PROP_VARYING;
7706 *taken_edge_p = find_edge (gimple_bb (stmt),
7707 label_to_block (CASE_LABEL (val)));
7709 if (dump_file && (dump_flags & TDF_DETAILS))
7711 fprintf (dump_file, " will take edge to ");
7712 print_generic_stmt (dump_file, CASE_LABEL (val), 0);
7715 return SSA_PROP_INTERESTING;
7719 /* Evaluate statement STMT. If the statement produces a useful range,
7720 return SSA_PROP_INTERESTING and record the SSA name with the
7721 interesting range into *OUTPUT_P.
7723 If STMT is a conditional branch and we can determine its truth
7724 value, the taken edge is recorded in *TAKEN_EDGE_P.
7726 If STMT produces a varying value, return SSA_PROP_VARYING. */
7728 static enum ssa_prop_result
7729 vrp_visit_stmt (gimple stmt, edge *taken_edge_p, tree *output_p)
7731 tree def;
7732 ssa_op_iter iter;
7734 if (dump_file && (dump_flags & TDF_DETAILS))
7736 fprintf (dump_file, "\nVisiting statement:\n");
7737 print_gimple_stmt (dump_file, stmt, 0, dump_flags);
7740 if (!stmt_interesting_for_vrp (stmt))
7741 gcc_assert (stmt_ends_bb_p (stmt));
7742 else if (is_gimple_assign (stmt) || is_gimple_call (stmt))
7743 return vrp_visit_assignment_or_call (stmt, output_p);
7744 else if (gimple_code (stmt) == GIMPLE_COND)
7745 return vrp_visit_cond_stmt (stmt, taken_edge_p);
7746 else if (gimple_code (stmt) == GIMPLE_SWITCH)
7747 return vrp_visit_switch_stmt (stmt, taken_edge_p);
7749 /* All other statements produce nothing of interest for VRP, so mark
7750 their outputs varying and prevent further simulation. */
7751 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_DEF)
7752 set_value_range_to_varying (get_value_range (def));
7754 return SSA_PROP_VARYING;
7757 /* Union the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
7758 { VR1TYPE, VR0MIN, VR0MAX } and store the result
7759 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
7760 possible such range. The resulting range is not canonicalized. */
7762 static void
7763 union_ranges (enum value_range_type *vr0type,
7764 tree *vr0min, tree *vr0max,
7765 enum value_range_type vr1type,
7766 tree vr1min, tree vr1max)
7768 bool mineq = operand_equal_p (*vr0min, vr1min, 0);
7769 bool maxeq = operand_equal_p (*vr0max, vr1max, 0);
7771 /* [] is vr0, () is vr1 in the following classification comments. */
7772 if (mineq && maxeq)
7774 /* [( )] */
7775 if (*vr0type == vr1type)
7776 /* Nothing to do for equal ranges. */
7778 else if ((*vr0type == VR_RANGE
7779 && vr1type == VR_ANTI_RANGE)
7780 || (*vr0type == VR_ANTI_RANGE
7781 && vr1type == VR_RANGE))
7783 /* For anti-range with range union the result is varying. */
7784 goto give_up;
7786 else
7787 gcc_unreachable ();
7789 else if (operand_less_p (*vr0max, vr1min) == 1
7790 || operand_less_p (vr1max, *vr0min) == 1)
7792 /* [ ] ( ) or ( ) [ ]
7793 If the ranges have an empty intersection, result of the union
7794 operation is the anti-range or if both are anti-ranges
7795 it covers all. */
7796 if (*vr0type == VR_ANTI_RANGE
7797 && vr1type == VR_ANTI_RANGE)
7798 goto give_up;
7799 else if (*vr0type == VR_ANTI_RANGE
7800 && vr1type == VR_RANGE)
7802 else if (*vr0type == VR_RANGE
7803 && vr1type == VR_ANTI_RANGE)
7805 *vr0type = vr1type;
7806 *vr0min = vr1min;
7807 *vr0max = vr1max;
7809 else if (*vr0type == VR_RANGE
7810 && vr1type == VR_RANGE)
7812 /* The result is the convex hull of both ranges. */
7813 if (operand_less_p (*vr0max, vr1min) == 1)
7815 /* If the result can be an anti-range, create one. */
7816 if (TREE_CODE (*vr0max) == INTEGER_CST
7817 && TREE_CODE (vr1min) == INTEGER_CST
7818 && vrp_val_is_min (*vr0min)
7819 && vrp_val_is_max (vr1max))
7821 tree min = int_const_binop (PLUS_EXPR,
7822 *vr0max,
7823 build_int_cst (TREE_TYPE (*vr0max), 1));
7824 tree max = int_const_binop (MINUS_EXPR,
7825 vr1min,
7826 build_int_cst (TREE_TYPE (vr1min), 1));
7827 if (!operand_less_p (max, min))
7829 *vr0type = VR_ANTI_RANGE;
7830 *vr0min = min;
7831 *vr0max = max;
7833 else
7834 *vr0max = vr1max;
7836 else
7837 *vr0max = vr1max;
7839 else
7841 /* If the result can be an anti-range, create one. */
7842 if (TREE_CODE (vr1max) == INTEGER_CST
7843 && TREE_CODE (*vr0min) == INTEGER_CST
7844 && vrp_val_is_min (vr1min)
7845 && vrp_val_is_max (*vr0max))
7847 tree min = int_const_binop (PLUS_EXPR,
7848 vr1max,
7849 build_int_cst (TREE_TYPE (vr1max), 1));
7850 tree max = int_const_binop (MINUS_EXPR,
7851 *vr0min,
7852 build_int_cst (TREE_TYPE (*vr0min), 1));
7853 if (!operand_less_p (max, min))
7855 *vr0type = VR_ANTI_RANGE;
7856 *vr0min = min;
7857 *vr0max = max;
7859 else
7860 *vr0min = vr1min;
7862 else
7863 *vr0min = vr1min;
7866 else
7867 gcc_unreachable ();
7869 else if ((maxeq || operand_less_p (vr1max, *vr0max) == 1)
7870 && (mineq || operand_less_p (*vr0min, vr1min) == 1))
7872 /* [ ( ) ] or [( ) ] or [ ( )] */
7873 if (*vr0type == VR_RANGE
7874 && vr1type == VR_RANGE)
7876 else if (*vr0type == VR_ANTI_RANGE
7877 && vr1type == VR_ANTI_RANGE)
7879 *vr0type = vr1type;
7880 *vr0min = vr1min;
7881 *vr0max = vr1max;
7883 else if (*vr0type == VR_ANTI_RANGE
7884 && vr1type == VR_RANGE)
7886 /* Arbitrarily choose the right or left gap. */
7887 if (!mineq && TREE_CODE (vr1min) == INTEGER_CST)
7888 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
7889 build_int_cst (TREE_TYPE (vr1min), 1));
7890 else if (!maxeq && TREE_CODE (vr1max) == INTEGER_CST)
7891 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
7892 build_int_cst (TREE_TYPE (vr1max), 1));
7893 else
7894 goto give_up;
7896 else if (*vr0type == VR_RANGE
7897 && vr1type == VR_ANTI_RANGE)
7898 /* The result covers everything. */
7899 goto give_up;
7900 else
7901 gcc_unreachable ();
7903 else if ((maxeq || operand_less_p (*vr0max, vr1max) == 1)
7904 && (mineq || operand_less_p (vr1min, *vr0min) == 1))
7906 /* ( [ ] ) or ([ ] ) or ( [ ]) */
7907 if (*vr0type == VR_RANGE
7908 && vr1type == VR_RANGE)
7910 *vr0type = vr1type;
7911 *vr0min = vr1min;
7912 *vr0max = vr1max;
7914 else if (*vr0type == VR_ANTI_RANGE
7915 && vr1type == VR_ANTI_RANGE)
7917 else if (*vr0type == VR_RANGE
7918 && vr1type == VR_ANTI_RANGE)
7920 *vr0type = VR_ANTI_RANGE;
7921 if (!mineq && TREE_CODE (*vr0min) == INTEGER_CST)
7923 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
7924 build_int_cst (TREE_TYPE (*vr0min), 1));
7925 *vr0min = vr1min;
7927 else if (!maxeq && TREE_CODE (*vr0max) == INTEGER_CST)
7929 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
7930 build_int_cst (TREE_TYPE (*vr0max), 1));
7931 *vr0max = vr1max;
7933 else
7934 goto give_up;
7936 else if (*vr0type == VR_ANTI_RANGE
7937 && vr1type == VR_RANGE)
7938 /* The result covers everything. */
7939 goto give_up;
7940 else
7941 gcc_unreachable ();
7943 else if ((operand_less_p (vr1min, *vr0max) == 1
7944 || operand_equal_p (vr1min, *vr0max, 0))
7945 && operand_less_p (*vr0min, vr1min) == 1
7946 && operand_less_p (*vr0max, vr1max) == 1)
7948 /* [ ( ] ) or [ ]( ) */
7949 if (*vr0type == VR_RANGE
7950 && vr1type == VR_RANGE)
7951 *vr0max = vr1max;
7952 else if (*vr0type == VR_ANTI_RANGE
7953 && vr1type == VR_ANTI_RANGE)
7954 *vr0min = vr1min;
7955 else if (*vr0type == VR_ANTI_RANGE
7956 && vr1type == VR_RANGE)
7958 if (TREE_CODE (vr1min) == INTEGER_CST)
7959 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
7960 build_int_cst (TREE_TYPE (vr1min), 1));
7961 else
7962 goto give_up;
7964 else if (*vr0type == VR_RANGE
7965 && vr1type == VR_ANTI_RANGE)
7967 if (TREE_CODE (*vr0max) == INTEGER_CST)
7969 *vr0type = vr1type;
7970 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
7971 build_int_cst (TREE_TYPE (*vr0max), 1));
7972 *vr0max = vr1max;
7974 else
7975 goto give_up;
7977 else
7978 gcc_unreachable ();
7980 else if ((operand_less_p (*vr0min, vr1max) == 1
7981 || operand_equal_p (*vr0min, vr1max, 0))
7982 && operand_less_p (vr1min, *vr0min) == 1
7983 && operand_less_p (vr1max, *vr0max) == 1)
7985 /* ( [ ) ] or ( )[ ] */
7986 if (*vr0type == VR_RANGE
7987 && vr1type == VR_RANGE)
7988 *vr0min = vr1min;
7989 else if (*vr0type == VR_ANTI_RANGE
7990 && vr1type == VR_ANTI_RANGE)
7991 *vr0max = vr1max;
7992 else if (*vr0type == VR_ANTI_RANGE
7993 && vr1type == VR_RANGE)
7995 if (TREE_CODE (vr1max) == INTEGER_CST)
7996 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
7997 build_int_cst (TREE_TYPE (vr1max), 1));
7998 else
7999 goto give_up;
8001 else if (*vr0type == VR_RANGE
8002 && vr1type == VR_ANTI_RANGE)
8004 if (TREE_CODE (*vr0min) == INTEGER_CST)
8006 *vr0type = vr1type;
8007 *vr0min = vr1min;
8008 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
8009 build_int_cst (TREE_TYPE (*vr0min), 1));
8011 else
8012 goto give_up;
8014 else
8015 gcc_unreachable ();
8017 else
8018 goto give_up;
8020 return;
8022 give_up:
8023 *vr0type = VR_VARYING;
8024 *vr0min = NULL_TREE;
8025 *vr0max = NULL_TREE;
8028 /* Intersect the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
8029 { VR1TYPE, VR0MIN, VR0MAX } and store the result
8030 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
8031 possible such range. The resulting range is not canonicalized. */
8033 static void
8034 intersect_ranges (enum value_range_type *vr0type,
8035 tree *vr0min, tree *vr0max,
8036 enum value_range_type vr1type,
8037 tree vr1min, tree vr1max)
8039 bool mineq = operand_equal_p (*vr0min, vr1min, 0);
8040 bool maxeq = operand_equal_p (*vr0max, vr1max, 0);
8042 /* [] is vr0, () is vr1 in the following classification comments. */
8043 if (mineq && maxeq)
8045 /* [( )] */
8046 if (*vr0type == vr1type)
8047 /* Nothing to do for equal ranges. */
8049 else if ((*vr0type == VR_RANGE
8050 && vr1type == VR_ANTI_RANGE)
8051 || (*vr0type == VR_ANTI_RANGE
8052 && vr1type == VR_RANGE))
8054 /* For anti-range with range intersection the result is empty. */
8055 *vr0type = VR_UNDEFINED;
8056 *vr0min = NULL_TREE;
8057 *vr0max = NULL_TREE;
8059 else
8060 gcc_unreachable ();
8062 else if (operand_less_p (*vr0max, vr1min) == 1
8063 || operand_less_p (vr1max, *vr0min) == 1)
8065 /* [ ] ( ) or ( ) [ ]
8066 If the ranges have an empty intersection, the result of the
8067 intersect operation is the range for intersecting an
8068 anti-range with a range or empty when intersecting two ranges. */
8069 if (*vr0type == VR_RANGE
8070 && vr1type == VR_ANTI_RANGE)
8072 else if (*vr0type == VR_ANTI_RANGE
8073 && vr1type == VR_RANGE)
8075 *vr0type = vr1type;
8076 *vr0min = vr1min;
8077 *vr0max = vr1max;
8079 else if (*vr0type == VR_RANGE
8080 && vr1type == VR_RANGE)
8082 *vr0type = VR_UNDEFINED;
8083 *vr0min = NULL_TREE;
8084 *vr0max = NULL_TREE;
8086 else if (*vr0type == VR_ANTI_RANGE
8087 && vr1type == VR_ANTI_RANGE)
8089 /* If the anti-ranges are adjacent to each other merge them. */
8090 if (TREE_CODE (*vr0max) == INTEGER_CST
8091 && TREE_CODE (vr1min) == INTEGER_CST
8092 && operand_less_p (*vr0max, vr1min) == 1
8093 && integer_onep (int_const_binop (MINUS_EXPR,
8094 vr1min, *vr0max)))
8095 *vr0max = vr1max;
8096 else if (TREE_CODE (vr1max) == INTEGER_CST
8097 && TREE_CODE (*vr0min) == INTEGER_CST
8098 && operand_less_p (vr1max, *vr0min) == 1
8099 && integer_onep (int_const_binop (MINUS_EXPR,
8100 *vr0min, vr1max)))
8101 *vr0min = vr1min;
8102 /* Else arbitrarily take VR0. */
8105 else if ((maxeq || operand_less_p (vr1max, *vr0max) == 1)
8106 && (mineq || operand_less_p (*vr0min, vr1min) == 1))
8108 /* [ ( ) ] or [( ) ] or [ ( )] */
8109 if (*vr0type == VR_RANGE
8110 && vr1type == VR_RANGE)
8112 /* If both are ranges the result is the inner one. */
8113 *vr0type = vr1type;
8114 *vr0min = vr1min;
8115 *vr0max = vr1max;
8117 else if (*vr0type == VR_RANGE
8118 && vr1type == VR_ANTI_RANGE)
8120 /* Choose the right gap if the left one is empty. */
8121 if (mineq)
8123 if (TREE_CODE (vr1max) == INTEGER_CST)
8124 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
8125 build_int_cst (TREE_TYPE (vr1max), 1));
8126 else
8127 *vr0min = vr1max;
8129 /* Choose the left gap if the right one is empty. */
8130 else if (maxeq)
8132 if (TREE_CODE (vr1min) == INTEGER_CST)
8133 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
8134 build_int_cst (TREE_TYPE (vr1min), 1));
8135 else
8136 *vr0max = vr1min;
8138 /* Choose the anti-range if the range is effectively varying. */
8139 else if (vrp_val_is_min (*vr0min)
8140 && vrp_val_is_max (*vr0max))
8142 *vr0type = vr1type;
8143 *vr0min = vr1min;
8144 *vr0max = vr1max;
8146 /* Else choose the range. */
8148 else if (*vr0type == VR_ANTI_RANGE
8149 && vr1type == VR_ANTI_RANGE)
8150 /* If both are anti-ranges the result is the outer one. */
8152 else if (*vr0type == VR_ANTI_RANGE
8153 && vr1type == VR_RANGE)
8155 /* The intersection is empty. */
8156 *vr0type = VR_UNDEFINED;
8157 *vr0min = NULL_TREE;
8158 *vr0max = NULL_TREE;
8160 else
8161 gcc_unreachable ();
8163 else if ((maxeq || operand_less_p (*vr0max, vr1max) == 1)
8164 && (mineq || operand_less_p (vr1min, *vr0min) == 1))
8166 /* ( [ ] ) or ([ ] ) or ( [ ]) */
8167 if (*vr0type == VR_RANGE
8168 && vr1type == VR_RANGE)
8169 /* Choose the inner range. */
8171 else if (*vr0type == VR_ANTI_RANGE
8172 && vr1type == VR_RANGE)
8174 /* Choose the right gap if the left is empty. */
8175 if (mineq)
8177 *vr0type = VR_RANGE;
8178 if (TREE_CODE (*vr0max) == INTEGER_CST)
8179 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
8180 build_int_cst (TREE_TYPE (*vr0max), 1));
8181 else
8182 *vr0min = *vr0max;
8183 *vr0max = vr1max;
8185 /* Choose the left gap if the right is empty. */
8186 else if (maxeq)
8188 *vr0type = VR_RANGE;
8189 if (TREE_CODE (*vr0min) == INTEGER_CST)
8190 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
8191 build_int_cst (TREE_TYPE (*vr0min), 1));
8192 else
8193 *vr0max = *vr0min;
8194 *vr0min = vr1min;
8196 /* Choose the anti-range if the range is effectively varying. */
8197 else if (vrp_val_is_min (vr1min)
8198 && vrp_val_is_max (vr1max))
8200 /* Else choose the range. */
8201 else
8203 *vr0type = vr1type;
8204 *vr0min = vr1min;
8205 *vr0max = vr1max;
8208 else if (*vr0type == VR_ANTI_RANGE
8209 && vr1type == VR_ANTI_RANGE)
8211 /* If both are anti-ranges the result is the outer one. */
8212 *vr0type = vr1type;
8213 *vr0min = vr1min;
8214 *vr0max = vr1max;
8216 else if (vr1type == VR_ANTI_RANGE
8217 && *vr0type == VR_RANGE)
8219 /* The intersection is empty. */
8220 *vr0type = VR_UNDEFINED;
8221 *vr0min = NULL_TREE;
8222 *vr0max = NULL_TREE;
8224 else
8225 gcc_unreachable ();
8227 else if ((operand_less_p (vr1min, *vr0max) == 1
8228 || operand_equal_p (vr1min, *vr0max, 0))
8229 && operand_less_p (*vr0min, vr1min) == 1)
8231 /* [ ( ] ) or [ ]( ) */
8232 if (*vr0type == VR_ANTI_RANGE
8233 && vr1type == VR_ANTI_RANGE)
8234 *vr0max = vr1max;
8235 else if (*vr0type == VR_RANGE
8236 && vr1type == VR_RANGE)
8237 *vr0min = vr1min;
8238 else if (*vr0type == VR_RANGE
8239 && vr1type == VR_ANTI_RANGE)
8241 if (TREE_CODE (vr1min) == INTEGER_CST)
8242 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
8243 build_int_cst (TREE_TYPE (vr1min), 1));
8244 else
8245 *vr0max = vr1min;
8247 else if (*vr0type == VR_ANTI_RANGE
8248 && vr1type == VR_RANGE)
8250 *vr0type = VR_RANGE;
8251 if (TREE_CODE (*vr0max) == INTEGER_CST)
8252 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
8253 build_int_cst (TREE_TYPE (*vr0max), 1));
8254 else
8255 *vr0min = *vr0max;
8256 *vr0max = vr1max;
8258 else
8259 gcc_unreachable ();
8261 else if ((operand_less_p (*vr0min, vr1max) == 1
8262 || operand_equal_p (*vr0min, vr1max, 0))
8263 && operand_less_p (vr1min, *vr0min) == 1)
8265 /* ( [ ) ] or ( )[ ] */
8266 if (*vr0type == VR_ANTI_RANGE
8267 && vr1type == VR_ANTI_RANGE)
8268 *vr0min = vr1min;
8269 else if (*vr0type == VR_RANGE
8270 && vr1type == VR_RANGE)
8271 *vr0max = vr1max;
8272 else if (*vr0type == VR_RANGE
8273 && vr1type == VR_ANTI_RANGE)
8275 if (TREE_CODE (vr1max) == INTEGER_CST)
8276 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
8277 build_int_cst (TREE_TYPE (vr1max), 1));
8278 else
8279 *vr0min = vr1max;
8281 else if (*vr0type == VR_ANTI_RANGE
8282 && vr1type == VR_RANGE)
8284 *vr0type = VR_RANGE;
8285 if (TREE_CODE (*vr0min) == INTEGER_CST)
8286 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
8287 build_int_cst (TREE_TYPE (*vr0min), 1));
8288 else
8289 *vr0max = *vr0min;
8290 *vr0min = vr1min;
8292 else
8293 gcc_unreachable ();
8296 /* As a fallback simply use { *VRTYPE, *VR0MIN, *VR0MAX } as
8297 result for the intersection. That's always a conservative
8298 correct estimate. */
8300 return;
8304 /* Intersect the two value-ranges *VR0 and *VR1 and store the result
8305 in *VR0. This may not be the smallest possible such range. */
8307 static void
8308 vrp_intersect_ranges_1 (value_range_t *vr0, value_range_t *vr1)
8310 value_range_t saved;
8312 /* If either range is VR_VARYING the other one wins. */
8313 if (vr1->type == VR_VARYING)
8314 return;
8315 if (vr0->type == VR_VARYING)
8317 copy_value_range (vr0, vr1);
8318 return;
8321 /* When either range is VR_UNDEFINED the resulting range is
8322 VR_UNDEFINED, too. */
8323 if (vr0->type == VR_UNDEFINED)
8324 return;
8325 if (vr1->type == VR_UNDEFINED)
8327 set_value_range_to_undefined (vr0);
8328 return;
8331 /* Save the original vr0 so we can return it as conservative intersection
8332 result when our worker turns things to varying. */
8333 saved = *vr0;
8334 intersect_ranges (&vr0->type, &vr0->min, &vr0->max,
8335 vr1->type, vr1->min, vr1->max);
8336 /* Make sure to canonicalize the result though as the inversion of a
8337 VR_RANGE can still be a VR_RANGE. */
8338 set_and_canonicalize_value_range (vr0, vr0->type,
8339 vr0->min, vr0->max, vr0->equiv);
8340 /* If that failed, use the saved original VR0. */
8341 if (vr0->type == VR_VARYING)
8343 *vr0 = saved;
8344 return;
8346 /* If the result is VR_UNDEFINED there is no need to mess with
8347 the equivalencies. */
8348 if (vr0->type == VR_UNDEFINED)
8349 return;
8351 /* The resulting set of equivalences for range intersection is the union of
8352 the two sets. */
8353 if (vr0->equiv && vr1->equiv && vr0->equiv != vr1->equiv)
8354 bitmap_ior_into (vr0->equiv, vr1->equiv);
8355 else if (vr1->equiv && !vr0->equiv)
8356 bitmap_copy (vr0->equiv, vr1->equiv);
8359 static void
8360 vrp_intersect_ranges (value_range_t *vr0, value_range_t *vr1)
8362 if (dump_file && (dump_flags & TDF_DETAILS))
8364 fprintf (dump_file, "Intersecting\n ");
8365 dump_value_range (dump_file, vr0);
8366 fprintf (dump_file, "\nand\n ");
8367 dump_value_range (dump_file, vr1);
8368 fprintf (dump_file, "\n");
8370 vrp_intersect_ranges_1 (vr0, vr1);
8371 if (dump_file && (dump_flags & TDF_DETAILS))
8373 fprintf (dump_file, "to\n ");
8374 dump_value_range (dump_file, vr0);
8375 fprintf (dump_file, "\n");
8379 /* Meet operation for value ranges. Given two value ranges VR0 and
8380 VR1, store in VR0 a range that contains both VR0 and VR1. This
8381 may not be the smallest possible such range. */
8383 static void
8384 vrp_meet_1 (value_range_t *vr0, value_range_t *vr1)
8386 value_range_t saved;
8388 if (vr0->type == VR_UNDEFINED)
8390 set_value_range (vr0, vr1->type, vr1->min, vr1->max, vr1->equiv);
8391 return;
8394 if (vr1->type == VR_UNDEFINED)
8396 /* VR0 already has the resulting range. */
8397 return;
8400 if (vr0->type == VR_VARYING)
8402 /* Nothing to do. VR0 already has the resulting range. */
8403 return;
8406 if (vr1->type == VR_VARYING)
8408 set_value_range_to_varying (vr0);
8409 return;
8412 saved = *vr0;
8413 union_ranges (&vr0->type, &vr0->min, &vr0->max,
8414 vr1->type, vr1->min, vr1->max);
8415 if (vr0->type == VR_VARYING)
8417 /* Failed to find an efficient meet. Before giving up and setting
8418 the result to VARYING, see if we can at least derive a useful
8419 anti-range. FIXME, all this nonsense about distinguishing
8420 anti-ranges from ranges is necessary because of the odd
8421 semantics of range_includes_zero_p and friends. */
8422 if (((saved.type == VR_RANGE
8423 && range_includes_zero_p (saved.min, saved.max) == 0)
8424 || (saved.type == VR_ANTI_RANGE
8425 && range_includes_zero_p (saved.min, saved.max) == 1))
8426 && ((vr1->type == VR_RANGE
8427 && range_includes_zero_p (vr1->min, vr1->max) == 0)
8428 || (vr1->type == VR_ANTI_RANGE
8429 && range_includes_zero_p (vr1->min, vr1->max) == 1)))
8431 set_value_range_to_nonnull (vr0, TREE_TYPE (saved.min));
8433 /* Since this meet operation did not result from the meeting of
8434 two equivalent names, VR0 cannot have any equivalences. */
8435 if (vr0->equiv)
8436 bitmap_clear (vr0->equiv);
8437 return;
8440 set_value_range_to_varying (vr0);
8441 return;
8443 set_and_canonicalize_value_range (vr0, vr0->type, vr0->min, vr0->max,
8444 vr0->equiv);
8445 if (vr0->type == VR_VARYING)
8446 return;
8448 /* The resulting set of equivalences is always the intersection of
8449 the two sets. */
8450 if (vr0->equiv && vr1->equiv && vr0->equiv != vr1->equiv)
8451 bitmap_and_into (vr0->equiv, vr1->equiv);
8452 else if (vr0->equiv && !vr1->equiv)
8453 bitmap_clear (vr0->equiv);
8456 static void
8457 vrp_meet (value_range_t *vr0, value_range_t *vr1)
8459 if (dump_file && (dump_flags & TDF_DETAILS))
8461 fprintf (dump_file, "Meeting\n ");
8462 dump_value_range (dump_file, vr0);
8463 fprintf (dump_file, "\nand\n ");
8464 dump_value_range (dump_file, vr1);
8465 fprintf (dump_file, "\n");
8467 vrp_meet_1 (vr0, vr1);
8468 if (dump_file && (dump_flags & TDF_DETAILS))
8470 fprintf (dump_file, "to\n ");
8471 dump_value_range (dump_file, vr0);
8472 fprintf (dump_file, "\n");
8477 /* Visit all arguments for PHI node PHI that flow through executable
8478 edges. If a valid value range can be derived from all the incoming
8479 value ranges, set a new range for the LHS of PHI. */
8481 static enum ssa_prop_result
8482 vrp_visit_phi_node (gimple phi)
8484 size_t i;
8485 tree lhs = PHI_RESULT (phi);
8486 value_range_t *lhs_vr = get_value_range (lhs);
8487 value_range_t vr_result = VR_INITIALIZER;
8488 bool first = true;
8489 int edges, old_edges;
8490 struct loop *l;
8492 if (dump_file && (dump_flags & TDF_DETAILS))
8494 fprintf (dump_file, "\nVisiting PHI node: ");
8495 print_gimple_stmt (dump_file, phi, 0, dump_flags);
8498 edges = 0;
8499 for (i = 0; i < gimple_phi_num_args (phi); i++)
8501 edge e = gimple_phi_arg_edge (phi, i);
8503 if (dump_file && (dump_flags & TDF_DETAILS))
8505 fprintf (dump_file,
8506 " Argument #%d (%d -> %d %sexecutable)\n",
8507 (int) i, e->src->index, e->dest->index,
8508 (e->flags & EDGE_EXECUTABLE) ? "" : "not ");
8511 if (e->flags & EDGE_EXECUTABLE)
8513 tree arg = PHI_ARG_DEF (phi, i);
8514 value_range_t vr_arg;
8516 ++edges;
8518 if (TREE_CODE (arg) == SSA_NAME)
8520 vr_arg = *(get_value_range (arg));
8521 /* Do not allow equivalences or symbolic ranges to leak in from
8522 backedges. That creates invalid equivalencies.
8523 See PR53465 and PR54767. */
8524 if (e->flags & EDGE_DFS_BACK)
8526 if (vr_arg.type == VR_RANGE
8527 || vr_arg.type == VR_ANTI_RANGE)
8529 vr_arg.equiv = NULL;
8530 if (symbolic_range_p (&vr_arg))
8532 vr_arg.type = VR_VARYING;
8533 vr_arg.min = NULL_TREE;
8534 vr_arg.max = NULL_TREE;
8538 else
8540 /* If the non-backedge arguments range is VR_VARYING then
8541 we can still try recording a simple equivalence. */
8542 if (vr_arg.type == VR_VARYING)
8544 vr_arg.type = VR_RANGE;
8545 vr_arg.min = arg;
8546 vr_arg.max = arg;
8547 vr_arg.equiv = NULL;
8551 else
8553 if (TREE_OVERFLOW_P (arg))
8554 arg = drop_tree_overflow (arg);
8556 vr_arg.type = VR_RANGE;
8557 vr_arg.min = arg;
8558 vr_arg.max = arg;
8559 vr_arg.equiv = NULL;
8562 if (dump_file && (dump_flags & TDF_DETAILS))
8564 fprintf (dump_file, "\t");
8565 print_generic_expr (dump_file, arg, dump_flags);
8566 fprintf (dump_file, ": ");
8567 dump_value_range (dump_file, &vr_arg);
8568 fprintf (dump_file, "\n");
8571 if (first)
8572 copy_value_range (&vr_result, &vr_arg);
8573 else
8574 vrp_meet (&vr_result, &vr_arg);
8575 first = false;
8577 if (vr_result.type == VR_VARYING)
8578 break;
8582 if (vr_result.type == VR_VARYING)
8583 goto varying;
8584 else if (vr_result.type == VR_UNDEFINED)
8585 goto update_range;
8587 old_edges = vr_phi_edge_counts[SSA_NAME_VERSION (lhs)];
8588 vr_phi_edge_counts[SSA_NAME_VERSION (lhs)] = edges;
8590 /* To prevent infinite iterations in the algorithm, derive ranges
8591 when the new value is slightly bigger or smaller than the
8592 previous one. We don't do this if we have seen a new executable
8593 edge; this helps us avoid an overflow infinity for conditionals
8594 which are not in a loop. If the old value-range was VR_UNDEFINED
8595 use the updated range and iterate one more time. */
8596 if (edges > 0
8597 && gimple_phi_num_args (phi) > 1
8598 && edges == old_edges
8599 && lhs_vr->type != VR_UNDEFINED)
8601 /* Compare old and new ranges, fall back to varying if the
8602 values are not comparable. */
8603 int cmp_min = compare_values (lhs_vr->min, vr_result.min);
8604 if (cmp_min == -2)
8605 goto varying;
8606 int cmp_max = compare_values (lhs_vr->max, vr_result.max);
8607 if (cmp_max == -2)
8608 goto varying;
8610 /* For non VR_RANGE or for pointers fall back to varying if
8611 the range changed. */
8612 if ((lhs_vr->type != VR_RANGE || vr_result.type != VR_RANGE
8613 || POINTER_TYPE_P (TREE_TYPE (lhs)))
8614 && (cmp_min != 0 || cmp_max != 0))
8615 goto varying;
8617 /* If the new minimum is larger than than the previous one
8618 retain the old value. If the new minimum value is smaller
8619 than the previous one and not -INF go all the way to -INF + 1.
8620 In the first case, to avoid infinite bouncing between different
8621 minimums, and in the other case to avoid iterating millions of
8622 times to reach -INF. Going to -INF + 1 also lets the following
8623 iteration compute whether there will be any overflow, at the
8624 expense of one additional iteration. */
8625 if (cmp_min < 0)
8626 vr_result.min = lhs_vr->min;
8627 else if (cmp_min > 0
8628 && !vrp_val_is_min (vr_result.min))
8629 vr_result.min
8630 = int_const_binop (PLUS_EXPR,
8631 vrp_val_min (TREE_TYPE (vr_result.min)),
8632 build_int_cst (TREE_TYPE (vr_result.min), 1));
8634 /* Similarly for the maximum value. */
8635 if (cmp_max > 0)
8636 vr_result.max = lhs_vr->max;
8637 else if (cmp_max < 0
8638 && !vrp_val_is_max (vr_result.max))
8639 vr_result.max
8640 = int_const_binop (MINUS_EXPR,
8641 vrp_val_max (TREE_TYPE (vr_result.min)),
8642 build_int_cst (TREE_TYPE (vr_result.min), 1));
8644 /* If we dropped either bound to +-INF then if this is a loop
8645 PHI node SCEV may known more about its value-range. */
8646 if ((cmp_min > 0 || cmp_min < 0
8647 || cmp_max < 0 || cmp_max > 0)
8648 && (l = loop_containing_stmt (phi))
8649 && l->header == gimple_bb (phi))
8650 adjust_range_with_scev (&vr_result, l, phi, lhs);
8652 /* If we will end up with a (-INF, +INF) range, set it to
8653 VARYING. Same if the previous max value was invalid for
8654 the type and we end up with vr_result.min > vr_result.max. */
8655 if ((vrp_val_is_max (vr_result.max)
8656 && vrp_val_is_min (vr_result.min))
8657 || compare_values (vr_result.min,
8658 vr_result.max) > 0)
8659 goto varying;
8662 /* If the new range is different than the previous value, keep
8663 iterating. */
8664 update_range:
8665 if (update_value_range (lhs, &vr_result))
8667 if (dump_file && (dump_flags & TDF_DETAILS))
8669 fprintf (dump_file, "Found new range for ");
8670 print_generic_expr (dump_file, lhs, 0);
8671 fprintf (dump_file, ": ");
8672 dump_value_range (dump_file, &vr_result);
8673 fprintf (dump_file, "\n");
8676 return SSA_PROP_INTERESTING;
8679 /* Nothing changed, don't add outgoing edges. */
8680 return SSA_PROP_NOT_INTERESTING;
8682 /* No match found. Set the LHS to VARYING. */
8683 varying:
8684 set_value_range_to_varying (lhs_vr);
8685 return SSA_PROP_VARYING;
8688 /* Simplify boolean operations if the source is known
8689 to be already a boolean. */
8690 static bool
8691 simplify_truth_ops_using_ranges (gimple_stmt_iterator *gsi, gimple stmt)
8693 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
8694 tree lhs, op0, op1;
8695 bool need_conversion;
8697 /* We handle only !=/== case here. */
8698 gcc_assert (rhs_code == EQ_EXPR || rhs_code == NE_EXPR);
8700 op0 = gimple_assign_rhs1 (stmt);
8701 if (!op_with_boolean_value_range_p (op0))
8702 return false;
8704 op1 = gimple_assign_rhs2 (stmt);
8705 if (!op_with_boolean_value_range_p (op1))
8706 return false;
8708 /* Reduce number of cases to handle to NE_EXPR. As there is no
8709 BIT_XNOR_EXPR we cannot replace A == B with a single statement. */
8710 if (rhs_code == EQ_EXPR)
8712 if (TREE_CODE (op1) == INTEGER_CST)
8713 op1 = int_const_binop (BIT_XOR_EXPR, op1,
8714 build_int_cst (TREE_TYPE (op1), 1));
8715 else
8716 return false;
8719 lhs = gimple_assign_lhs (stmt);
8720 need_conversion
8721 = !useless_type_conversion_p (TREE_TYPE (lhs), TREE_TYPE (op0));
8723 /* Make sure to not sign-extend a 1-bit 1 when converting the result. */
8724 if (need_conversion
8725 && !TYPE_UNSIGNED (TREE_TYPE (op0))
8726 && TYPE_PRECISION (TREE_TYPE (op0)) == 1
8727 && TYPE_PRECISION (TREE_TYPE (lhs)) > 1)
8728 return false;
8730 /* For A != 0 we can substitute A itself. */
8731 if (integer_zerop (op1))
8732 gimple_assign_set_rhs_with_ops (gsi,
8733 need_conversion
8734 ? NOP_EXPR : TREE_CODE (op0),
8735 op0, NULL_TREE);
8736 /* For A != B we substitute A ^ B. Either with conversion. */
8737 else if (need_conversion)
8739 tree tem = make_ssa_name (TREE_TYPE (op0), NULL);
8740 gimple newop = gimple_build_assign_with_ops (BIT_XOR_EXPR, tem, op0, op1);
8741 gsi_insert_before (gsi, newop, GSI_SAME_STMT);
8742 gimple_assign_set_rhs_with_ops (gsi, NOP_EXPR, tem, NULL_TREE);
8744 /* Or without. */
8745 else
8746 gimple_assign_set_rhs_with_ops (gsi, BIT_XOR_EXPR, op0, op1);
8747 update_stmt (gsi_stmt (*gsi));
8749 return true;
8752 /* Simplify a division or modulo operator to a right shift or
8753 bitwise and if the first operand is unsigned or is greater
8754 than zero and the second operand is an exact power of two. */
8756 static bool
8757 simplify_div_or_mod_using_ranges (gimple stmt)
8759 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
8760 tree val = NULL;
8761 tree op0 = gimple_assign_rhs1 (stmt);
8762 tree op1 = gimple_assign_rhs2 (stmt);
8763 value_range_t *vr = get_value_range (gimple_assign_rhs1 (stmt));
8765 if (TYPE_UNSIGNED (TREE_TYPE (op0)))
8767 val = integer_one_node;
8769 else
8771 bool sop = false;
8773 val = compare_range_with_value (GE_EXPR, vr, integer_zero_node, &sop);
8775 if (val
8776 && sop
8777 && integer_onep (val)
8778 && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC))
8780 location_t location;
8782 if (!gimple_has_location (stmt))
8783 location = input_location;
8784 else
8785 location = gimple_location (stmt);
8786 warning_at (location, OPT_Wstrict_overflow,
8787 "assuming signed overflow does not occur when "
8788 "simplifying %</%> or %<%%%> to %<>>%> or %<&%>");
8792 if (val && integer_onep (val))
8794 tree t;
8796 if (rhs_code == TRUNC_DIV_EXPR)
8798 t = build_int_cst (integer_type_node, tree_log2 (op1));
8799 gimple_assign_set_rhs_code (stmt, RSHIFT_EXPR);
8800 gimple_assign_set_rhs1 (stmt, op0);
8801 gimple_assign_set_rhs2 (stmt, t);
8803 else
8805 t = build_int_cst (TREE_TYPE (op1), 1);
8806 t = int_const_binop (MINUS_EXPR, op1, t);
8807 t = fold_convert (TREE_TYPE (op0), t);
8809 gimple_assign_set_rhs_code (stmt, BIT_AND_EXPR);
8810 gimple_assign_set_rhs1 (stmt, op0);
8811 gimple_assign_set_rhs2 (stmt, t);
8814 update_stmt (stmt);
8815 return true;
8818 return false;
8821 /* If the operand to an ABS_EXPR is >= 0, then eliminate the
8822 ABS_EXPR. If the operand is <= 0, then simplify the
8823 ABS_EXPR into a NEGATE_EXPR. */
8825 static bool
8826 simplify_abs_using_ranges (gimple stmt)
8828 tree val = NULL;
8829 tree op = gimple_assign_rhs1 (stmt);
8830 tree type = TREE_TYPE (op);
8831 value_range_t *vr = get_value_range (op);
8833 if (TYPE_UNSIGNED (type))
8835 val = integer_zero_node;
8837 else if (vr)
8839 bool sop = false;
8841 val = compare_range_with_value (LE_EXPR, vr, integer_zero_node, &sop);
8842 if (!val)
8844 sop = false;
8845 val = compare_range_with_value (GE_EXPR, vr, integer_zero_node,
8846 &sop);
8848 if (val)
8850 if (integer_zerop (val))
8851 val = integer_one_node;
8852 else if (integer_onep (val))
8853 val = integer_zero_node;
8857 if (val
8858 && (integer_onep (val) || integer_zerop (val)))
8860 if (sop && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC))
8862 location_t location;
8864 if (!gimple_has_location (stmt))
8865 location = input_location;
8866 else
8867 location = gimple_location (stmt);
8868 warning_at (location, OPT_Wstrict_overflow,
8869 "assuming signed overflow does not occur when "
8870 "simplifying %<abs (X)%> to %<X%> or %<-X%>");
8873 gimple_assign_set_rhs1 (stmt, op);
8874 if (integer_onep (val))
8875 gimple_assign_set_rhs_code (stmt, NEGATE_EXPR);
8876 else
8877 gimple_assign_set_rhs_code (stmt, SSA_NAME);
8878 update_stmt (stmt);
8879 return true;
8883 return false;
8886 /* Optimize away redundant BIT_AND_EXPR and BIT_IOR_EXPR.
8887 If all the bits that are being cleared by & are already
8888 known to be zero from VR, or all the bits that are being
8889 set by | are already known to be one from VR, the bit
8890 operation is redundant. */
8892 static bool
8893 simplify_bit_ops_using_ranges (gimple_stmt_iterator *gsi, gimple stmt)
8895 tree op0 = gimple_assign_rhs1 (stmt);
8896 tree op1 = gimple_assign_rhs2 (stmt);
8897 tree op = NULL_TREE;
8898 value_range_t vr0 = VR_INITIALIZER;
8899 value_range_t vr1 = VR_INITIALIZER;
8900 wide_int may_be_nonzero0, may_be_nonzero1;
8901 wide_int must_be_nonzero0, must_be_nonzero1;
8902 wide_int mask;
8904 if (TREE_CODE (op0) == SSA_NAME)
8905 vr0 = *(get_value_range (op0));
8906 else if (is_gimple_min_invariant (op0))
8907 set_value_range_to_value (&vr0, op0, NULL);
8908 else
8909 return false;
8911 if (TREE_CODE (op1) == SSA_NAME)
8912 vr1 = *(get_value_range (op1));
8913 else if (is_gimple_min_invariant (op1))
8914 set_value_range_to_value (&vr1, op1, NULL);
8915 else
8916 return false;
8918 if (!zero_nonzero_bits_from_vr (TREE_TYPE (op0), &vr0, &may_be_nonzero0,
8919 &must_be_nonzero0))
8920 return false;
8921 if (!zero_nonzero_bits_from_vr (TREE_TYPE (op1), &vr1, &may_be_nonzero1,
8922 &must_be_nonzero1))
8923 return false;
8925 switch (gimple_assign_rhs_code (stmt))
8927 case BIT_AND_EXPR:
8928 mask = may_be_nonzero0.and_not (must_be_nonzero1);
8929 if (mask == 0)
8931 op = op0;
8932 break;
8934 mask = may_be_nonzero1.and_not (must_be_nonzero0);
8935 if (mask == 0)
8937 op = op1;
8938 break;
8940 break;
8941 case BIT_IOR_EXPR:
8942 mask = may_be_nonzero0.and_not (must_be_nonzero1);
8943 if (mask == 0)
8945 op = op1;
8946 break;
8948 mask = may_be_nonzero1.and_not (must_be_nonzero0);
8949 if (mask == 0)
8951 op = op0;
8952 break;
8954 break;
8955 default:
8956 gcc_unreachable ();
8959 if (op == NULL_TREE)
8960 return false;
8962 gimple_assign_set_rhs_with_ops (gsi, TREE_CODE (op), op, NULL);
8963 update_stmt (gsi_stmt (*gsi));
8964 return true;
8967 /* We are comparing trees OP0 and OP1 using COND_CODE. OP0 has
8968 a known value range VR.
8970 If there is one and only one value which will satisfy the
8971 conditional, then return that value. Else return NULL. */
8973 static tree
8974 test_for_singularity (enum tree_code cond_code, tree op0,
8975 tree op1, value_range_t *vr)
8977 tree min = NULL;
8978 tree max = NULL;
8980 /* Extract minimum/maximum values which satisfy the
8981 the conditional as it was written. */
8982 if (cond_code == LE_EXPR || cond_code == LT_EXPR)
8984 /* This should not be negative infinity; there is no overflow
8985 here. */
8986 min = TYPE_MIN_VALUE (TREE_TYPE (op0));
8988 max = op1;
8989 if (cond_code == LT_EXPR && !is_overflow_infinity (max))
8991 tree one = build_int_cst (TREE_TYPE (op0), 1);
8992 max = fold_build2 (MINUS_EXPR, TREE_TYPE (op0), max, one);
8993 if (EXPR_P (max))
8994 TREE_NO_WARNING (max) = 1;
8997 else if (cond_code == GE_EXPR || cond_code == GT_EXPR)
8999 /* This should not be positive infinity; there is no overflow
9000 here. */
9001 max = TYPE_MAX_VALUE (TREE_TYPE (op0));
9003 min = op1;
9004 if (cond_code == GT_EXPR && !is_overflow_infinity (min))
9006 tree one = build_int_cst (TREE_TYPE (op0), 1);
9007 min = fold_build2 (PLUS_EXPR, TREE_TYPE (op0), min, one);
9008 if (EXPR_P (min))
9009 TREE_NO_WARNING (min) = 1;
9013 /* Now refine the minimum and maximum values using any
9014 value range information we have for op0. */
9015 if (min && max)
9017 if (compare_values (vr->min, min) == 1)
9018 min = vr->min;
9019 if (compare_values (vr->max, max) == -1)
9020 max = vr->max;
9022 /* If the new min/max values have converged to a single value,
9023 then there is only one value which can satisfy the condition,
9024 return that value. */
9025 if (operand_equal_p (min, max, 0) && is_gimple_min_invariant (min))
9026 return min;
9028 return NULL;
9031 /* Return whether the value range *VR fits in an integer type specified
9032 by PRECISION and UNSIGNED_P. */
9034 static bool
9035 range_fits_type_p (value_range_t *vr, unsigned dest_precision, signop dest_sgn)
9037 tree src_type;
9038 unsigned src_precision;
9039 widest_int tem;
9040 signop src_sgn;
9042 /* We can only handle integral and pointer types. */
9043 src_type = TREE_TYPE (vr->min);
9044 if (!INTEGRAL_TYPE_P (src_type)
9045 && !POINTER_TYPE_P (src_type))
9046 return false;
9048 /* An extension is fine unless VR is SIGNED and dest_sgn is UNSIGNED,
9049 and so is an identity transform. */
9050 src_precision = TYPE_PRECISION (TREE_TYPE (vr->min));
9051 src_sgn = TYPE_SIGN (src_type);
9052 if ((src_precision < dest_precision
9053 && !(dest_sgn == UNSIGNED && src_sgn == SIGNED))
9054 || (src_precision == dest_precision && src_sgn == dest_sgn))
9055 return true;
9057 /* Now we can only handle ranges with constant bounds. */
9058 if (vr->type != VR_RANGE
9059 || TREE_CODE (vr->min) != INTEGER_CST
9060 || TREE_CODE (vr->max) != INTEGER_CST)
9061 return false;
9063 /* For sign changes, the MSB of the wide_int has to be clear.
9064 An unsigned value with its MSB set cannot be represented by
9065 a signed wide_int, while a negative value cannot be represented
9066 by an unsigned wide_int. */
9067 if (src_sgn != dest_sgn
9068 && (wi::lts_p (vr->min, 0) || wi::lts_p (vr->max, 0)))
9069 return false;
9071 /* Then we can perform the conversion on both ends and compare
9072 the result for equality. */
9073 tem = wi::ext (wi::to_widest (vr->min), dest_precision, dest_sgn);
9074 if (tem != wi::to_widest (vr->min))
9075 return false;
9076 tem = wi::ext (wi::to_widest (vr->max), dest_precision, dest_sgn);
9077 if (tem != wi::to_widest (vr->max))
9078 return false;
9080 return true;
9083 /* Simplify a conditional using a relational operator to an equality
9084 test if the range information indicates only one value can satisfy
9085 the original conditional. */
9087 static bool
9088 simplify_cond_using_ranges (gimple stmt)
9090 tree op0 = gimple_cond_lhs (stmt);
9091 tree op1 = gimple_cond_rhs (stmt);
9092 enum tree_code cond_code = gimple_cond_code (stmt);
9094 if (cond_code != NE_EXPR
9095 && cond_code != EQ_EXPR
9096 && TREE_CODE (op0) == SSA_NAME
9097 && INTEGRAL_TYPE_P (TREE_TYPE (op0))
9098 && is_gimple_min_invariant (op1))
9100 value_range_t *vr = get_value_range (op0);
9102 /* If we have range information for OP0, then we might be
9103 able to simplify this conditional. */
9104 if (vr->type == VR_RANGE)
9106 tree new_tree = test_for_singularity (cond_code, op0, op1, vr);
9108 if (new_tree)
9110 if (dump_file)
9112 fprintf (dump_file, "Simplified relational ");
9113 print_gimple_stmt (dump_file, stmt, 0, 0);
9114 fprintf (dump_file, " into ");
9117 gimple_cond_set_code (stmt, EQ_EXPR);
9118 gimple_cond_set_lhs (stmt, op0);
9119 gimple_cond_set_rhs (stmt, new_tree);
9121 update_stmt (stmt);
9123 if (dump_file)
9125 print_gimple_stmt (dump_file, stmt, 0, 0);
9126 fprintf (dump_file, "\n");
9129 return true;
9132 /* Try again after inverting the condition. We only deal
9133 with integral types here, so no need to worry about
9134 issues with inverting FP comparisons. */
9135 cond_code = invert_tree_comparison (cond_code, false);
9136 new_tree = test_for_singularity (cond_code, op0, op1, vr);
9138 if (new_tree)
9140 if (dump_file)
9142 fprintf (dump_file, "Simplified relational ");
9143 print_gimple_stmt (dump_file, stmt, 0, 0);
9144 fprintf (dump_file, " into ");
9147 gimple_cond_set_code (stmt, NE_EXPR);
9148 gimple_cond_set_lhs (stmt, op0);
9149 gimple_cond_set_rhs (stmt, new_tree);
9151 update_stmt (stmt);
9153 if (dump_file)
9155 print_gimple_stmt (dump_file, stmt, 0, 0);
9156 fprintf (dump_file, "\n");
9159 return true;
9164 /* If we have a comparison of an SSA_NAME (OP0) against a constant,
9165 see if OP0 was set by a type conversion where the source of
9166 the conversion is another SSA_NAME with a range that fits
9167 into the range of OP0's type.
9169 If so, the conversion is redundant as the earlier SSA_NAME can be
9170 used for the comparison directly if we just massage the constant in the
9171 comparison. */
9172 if (TREE_CODE (op0) == SSA_NAME
9173 && TREE_CODE (op1) == INTEGER_CST)
9175 gimple def_stmt = SSA_NAME_DEF_STMT (op0);
9176 tree innerop;
9178 if (!is_gimple_assign (def_stmt)
9179 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt)))
9180 return false;
9182 innerop = gimple_assign_rhs1 (def_stmt);
9184 if (TREE_CODE (innerop) == SSA_NAME
9185 && !POINTER_TYPE_P (TREE_TYPE (innerop)))
9187 value_range_t *vr = get_value_range (innerop);
9189 if (range_int_cst_p (vr)
9190 && range_fits_type_p (vr,
9191 TYPE_PRECISION (TREE_TYPE (op0)),
9192 TYPE_SIGN (TREE_TYPE (op0)))
9193 && int_fits_type_p (op1, TREE_TYPE (innerop))
9194 /* The range must not have overflowed, or if it did overflow
9195 we must not be wrapping/trapping overflow and optimizing
9196 with strict overflow semantics. */
9197 && ((!is_negative_overflow_infinity (vr->min)
9198 && !is_positive_overflow_infinity (vr->max))
9199 || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (innerop))))
9201 /* If the range overflowed and the user has asked for warnings
9202 when strict overflow semantics were used to optimize code,
9203 issue an appropriate warning. */
9204 if (cond_code != EQ_EXPR && cond_code != NE_EXPR
9205 && (is_negative_overflow_infinity (vr->min)
9206 || is_positive_overflow_infinity (vr->max))
9207 && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_CONDITIONAL))
9209 location_t location;
9211 if (!gimple_has_location (stmt))
9212 location = input_location;
9213 else
9214 location = gimple_location (stmt);
9215 warning_at (location, OPT_Wstrict_overflow,
9216 "assuming signed overflow does not occur when "
9217 "simplifying conditional");
9220 tree newconst = fold_convert (TREE_TYPE (innerop), op1);
9221 gimple_cond_set_lhs (stmt, innerop);
9222 gimple_cond_set_rhs (stmt, newconst);
9223 return true;
9228 return false;
9231 /* Simplify a switch statement using the value range of the switch
9232 argument. */
9234 static bool
9235 simplify_switch_using_ranges (gimple stmt)
9237 tree op = gimple_switch_index (stmt);
9238 value_range_t *vr;
9239 bool take_default;
9240 edge e;
9241 edge_iterator ei;
9242 size_t i = 0, j = 0, n, n2;
9243 tree vec2;
9244 switch_update su;
9245 size_t k = 1, l = 0;
9247 if (TREE_CODE (op) == SSA_NAME)
9249 vr = get_value_range (op);
9251 /* We can only handle integer ranges. */
9252 if ((vr->type != VR_RANGE
9253 && vr->type != VR_ANTI_RANGE)
9254 || symbolic_range_p (vr))
9255 return false;
9257 /* Find case label for min/max of the value range. */
9258 take_default = !find_case_label_ranges (stmt, vr, &i, &j, &k, &l);
9260 else if (TREE_CODE (op) == INTEGER_CST)
9262 take_default = !find_case_label_index (stmt, 1, op, &i);
9263 if (take_default)
9265 i = 1;
9266 j = 0;
9268 else
9270 j = i;
9273 else
9274 return false;
9276 n = gimple_switch_num_labels (stmt);
9278 /* Bail out if this is just all edges taken. */
9279 if (i == 1
9280 && j == n - 1
9281 && take_default)
9282 return false;
9284 /* Build a new vector of taken case labels. */
9285 vec2 = make_tree_vec (j - i + 1 + l - k + 1 + (int)take_default);
9286 n2 = 0;
9288 /* Add the default edge, if necessary. */
9289 if (take_default)
9290 TREE_VEC_ELT (vec2, n2++) = gimple_switch_default_label (stmt);
9292 for (; i <= j; ++i, ++n2)
9293 TREE_VEC_ELT (vec2, n2) = gimple_switch_label (stmt, i);
9295 for (; k <= l; ++k, ++n2)
9296 TREE_VEC_ELT (vec2, n2) = gimple_switch_label (stmt, k);
9298 /* Mark needed edges. */
9299 for (i = 0; i < n2; ++i)
9301 e = find_edge (gimple_bb (stmt),
9302 label_to_block (CASE_LABEL (TREE_VEC_ELT (vec2, i))));
9303 e->aux = (void *)-1;
9306 /* Queue not needed edges for later removal. */
9307 FOR_EACH_EDGE (e, ei, gimple_bb (stmt)->succs)
9309 if (e->aux == (void *)-1)
9311 e->aux = NULL;
9312 continue;
9315 if (dump_file && (dump_flags & TDF_DETAILS))
9317 fprintf (dump_file, "removing unreachable case label\n");
9319 to_remove_edges.safe_push (e);
9320 e->flags &= ~EDGE_EXECUTABLE;
9323 /* And queue an update for the stmt. */
9324 su.stmt = stmt;
9325 su.vec = vec2;
9326 to_update_switch_stmts.safe_push (su);
9327 return false;
9330 /* Simplify an integral conversion from an SSA name in STMT. */
9332 static bool
9333 simplify_conversion_using_ranges (gimple stmt)
9335 tree innerop, middleop, finaltype;
9336 gimple def_stmt;
9337 value_range_t *innervr;
9338 signop inner_sgn, middle_sgn, final_sgn;
9339 unsigned inner_prec, middle_prec, final_prec;
9340 widest_int innermin, innermed, innermax, middlemin, middlemed, middlemax;
9342 finaltype = TREE_TYPE (gimple_assign_lhs (stmt));
9343 if (!INTEGRAL_TYPE_P (finaltype))
9344 return false;
9345 middleop = gimple_assign_rhs1 (stmt);
9346 def_stmt = SSA_NAME_DEF_STMT (middleop);
9347 if (!is_gimple_assign (def_stmt)
9348 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt)))
9349 return false;
9350 innerop = gimple_assign_rhs1 (def_stmt);
9351 if (TREE_CODE (innerop) != SSA_NAME
9352 || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (innerop))
9353 return false;
9355 /* Get the value-range of the inner operand. */
9356 innervr = get_value_range (innerop);
9357 if (innervr->type != VR_RANGE
9358 || TREE_CODE (innervr->min) != INTEGER_CST
9359 || TREE_CODE (innervr->max) != INTEGER_CST)
9360 return false;
9362 /* Simulate the conversion chain to check if the result is equal if
9363 the middle conversion is removed. */
9364 innermin = wi::to_widest (innervr->min);
9365 innermax = wi::to_widest (innervr->max);
9367 inner_prec = TYPE_PRECISION (TREE_TYPE (innerop));
9368 middle_prec = TYPE_PRECISION (TREE_TYPE (middleop));
9369 final_prec = TYPE_PRECISION (finaltype);
9371 /* If the first conversion is not injective, the second must not
9372 be widening. */
9373 if (wi::gtu_p (innermax - innermin,
9374 wi::mask <widest_int> (middle_prec, false))
9375 && middle_prec < final_prec)
9376 return false;
9377 /* We also want a medium value so that we can track the effect that
9378 narrowing conversions with sign change have. */
9379 inner_sgn = TYPE_SIGN (TREE_TYPE (innerop));
9380 if (inner_sgn == UNSIGNED)
9381 innermed = wi::shifted_mask <widest_int> (1, inner_prec - 1, false);
9382 else
9383 innermed = 0;
9384 if (wi::cmp (innermin, innermed, inner_sgn) >= 0
9385 || wi::cmp (innermed, innermax, inner_sgn) >= 0)
9386 innermed = innermin;
9388 middle_sgn = TYPE_SIGN (TREE_TYPE (middleop));
9389 middlemin = wi::ext (innermin, middle_prec, middle_sgn);
9390 middlemed = wi::ext (innermed, middle_prec, middle_sgn);
9391 middlemax = wi::ext (innermax, middle_prec, middle_sgn);
9393 /* Require that the final conversion applied to both the original
9394 and the intermediate range produces the same result. */
9395 final_sgn = TYPE_SIGN (finaltype);
9396 if (wi::ext (middlemin, final_prec, final_sgn)
9397 != wi::ext (innermin, final_prec, final_sgn)
9398 || wi::ext (middlemed, final_prec, final_sgn)
9399 != wi::ext (innermed, final_prec, final_sgn)
9400 || wi::ext (middlemax, final_prec, final_sgn)
9401 != wi::ext (innermax, final_prec, final_sgn))
9402 return false;
9404 gimple_assign_set_rhs1 (stmt, innerop);
9405 update_stmt (stmt);
9406 return true;
9409 /* Simplify a conversion from integral SSA name to float in STMT. */
9411 static bool
9412 simplify_float_conversion_using_ranges (gimple_stmt_iterator *gsi, gimple stmt)
9414 tree rhs1 = gimple_assign_rhs1 (stmt);
9415 value_range_t *vr = get_value_range (rhs1);
9416 machine_mode fltmode = TYPE_MODE (TREE_TYPE (gimple_assign_lhs (stmt)));
9417 machine_mode mode;
9418 tree tem;
9419 gimple conv;
9421 /* We can only handle constant ranges. */
9422 if (vr->type != VR_RANGE
9423 || TREE_CODE (vr->min) != INTEGER_CST
9424 || TREE_CODE (vr->max) != INTEGER_CST)
9425 return false;
9427 /* First check if we can use a signed type in place of an unsigned. */
9428 if (TYPE_UNSIGNED (TREE_TYPE (rhs1))
9429 && (can_float_p (fltmode, TYPE_MODE (TREE_TYPE (rhs1)), 0)
9430 != CODE_FOR_nothing)
9431 && range_fits_type_p (vr, TYPE_PRECISION (TREE_TYPE (rhs1)), SIGNED))
9432 mode = TYPE_MODE (TREE_TYPE (rhs1));
9433 /* If we can do the conversion in the current input mode do nothing. */
9434 else if (can_float_p (fltmode, TYPE_MODE (TREE_TYPE (rhs1)),
9435 TYPE_UNSIGNED (TREE_TYPE (rhs1))) != CODE_FOR_nothing)
9436 return false;
9437 /* Otherwise search for a mode we can use, starting from the narrowest
9438 integer mode available. */
9439 else
9441 mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
9444 /* If we cannot do a signed conversion to float from mode
9445 or if the value-range does not fit in the signed type
9446 try with a wider mode. */
9447 if (can_float_p (fltmode, mode, 0) != CODE_FOR_nothing
9448 && range_fits_type_p (vr, GET_MODE_PRECISION (mode), SIGNED))
9449 break;
9451 mode = GET_MODE_WIDER_MODE (mode);
9452 /* But do not widen the input. Instead leave that to the
9453 optabs expansion code. */
9454 if (GET_MODE_PRECISION (mode) > TYPE_PRECISION (TREE_TYPE (rhs1)))
9455 return false;
9457 while (mode != VOIDmode);
9458 if (mode == VOIDmode)
9459 return false;
9462 /* It works, insert a truncation or sign-change before the
9463 float conversion. */
9464 tem = make_ssa_name (build_nonstandard_integer_type
9465 (GET_MODE_PRECISION (mode), 0), NULL);
9466 conv = gimple_build_assign_with_ops (NOP_EXPR, tem, rhs1, NULL_TREE);
9467 gsi_insert_before (gsi, conv, GSI_SAME_STMT);
9468 gimple_assign_set_rhs1 (stmt, tem);
9469 update_stmt (stmt);
9471 return true;
9474 /* Simplify an internal fn call using ranges if possible. */
9476 static bool
9477 simplify_internal_call_using_ranges (gimple_stmt_iterator *gsi, gimple stmt)
9479 enum tree_code subcode;
9480 switch (gimple_call_internal_fn (stmt))
9482 case IFN_UBSAN_CHECK_ADD:
9483 subcode = PLUS_EXPR;
9484 break;
9485 case IFN_UBSAN_CHECK_SUB:
9486 subcode = MINUS_EXPR;
9487 break;
9488 case IFN_UBSAN_CHECK_MUL:
9489 subcode = MULT_EXPR;
9490 break;
9491 default:
9492 return false;
9495 value_range_t vr0 = VR_INITIALIZER;
9496 value_range_t vr1 = VR_INITIALIZER;
9497 tree op0 = gimple_call_arg (stmt, 0);
9498 tree op1 = gimple_call_arg (stmt, 1);
9500 if (TREE_CODE (op0) == SSA_NAME)
9501 vr0 = *get_value_range (op0);
9502 else if (TREE_CODE (op0) == INTEGER_CST)
9503 set_value_range_to_value (&vr0, op0, NULL);
9504 else
9505 set_value_range_to_varying (&vr0);
9507 if (TREE_CODE (op1) == SSA_NAME)
9508 vr1 = *get_value_range (op1);
9509 else if (TREE_CODE (op1) == INTEGER_CST)
9510 set_value_range_to_value (&vr1, op1, NULL);
9511 else
9512 set_value_range_to_varying (&vr1);
9514 if (!range_int_cst_p (&vr0))
9516 /* If one range is VR_ANTI_RANGE, VR_VARYING etc.,
9517 optimize at least x = y + 0; x = y - 0; x = y * 0;
9518 and x = y * 1; which never overflow. */
9519 if (!range_int_cst_p (&vr1))
9520 return false;
9521 if (tree_int_cst_sgn (vr1.min) == -1)
9522 return false;
9523 if (compare_tree_int (vr1.max, subcode == MULT_EXPR) == 1)
9524 return false;
9526 else if (!range_int_cst_p (&vr1))
9528 /* If one range is VR_ANTI_RANGE, VR_VARYING etc.,
9529 optimize at least x = 0 + y; x = 0 * y; and x = 1 * y;
9530 which never overflow. */
9531 if (subcode == MINUS_EXPR)
9532 return false;
9533 if (!range_int_cst_p (&vr0))
9534 return false;
9535 if (tree_int_cst_sgn (vr0.min) == -1)
9536 return false;
9537 if (compare_tree_int (vr0.max, subcode == MULT_EXPR) == 1)
9538 return false;
9540 else
9542 tree r1 = int_const_binop (subcode, vr0.min,
9543 subcode == MINUS_EXPR ? vr1.max : vr1.min);
9544 tree r2 = int_const_binop (subcode, vr0.max,
9545 subcode == MINUS_EXPR ? vr1.min : vr1.max);
9546 if (r1 == NULL_TREE || TREE_OVERFLOW (r1)
9547 || r2 == NULL_TREE || TREE_OVERFLOW (r2))
9548 return false;
9549 if (subcode == MULT_EXPR)
9551 tree r3 = int_const_binop (subcode, vr0.min, vr1.max);
9552 tree r4 = int_const_binop (subcode, vr0.max, vr1.min);
9553 if (r3 == NULL_TREE || TREE_OVERFLOW (r3)
9554 || r4 == NULL_TREE || TREE_OVERFLOW (r4))
9555 return false;
9559 gimple g = gimple_build_assign_with_ops (subcode, gimple_call_lhs (stmt),
9560 op0, op1);
9561 gsi_replace (gsi, g, false);
9562 return true;
9565 /* Simplify STMT using ranges if possible. */
9567 static bool
9568 simplify_stmt_using_ranges (gimple_stmt_iterator *gsi)
9570 gimple stmt = gsi_stmt (*gsi);
9571 if (is_gimple_assign (stmt))
9573 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
9574 tree rhs1 = gimple_assign_rhs1 (stmt);
9576 switch (rhs_code)
9578 case EQ_EXPR:
9579 case NE_EXPR:
9580 /* Transform EQ_EXPR, NE_EXPR into BIT_XOR_EXPR or identity
9581 if the RHS is zero or one, and the LHS are known to be boolean
9582 values. */
9583 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
9584 return simplify_truth_ops_using_ranges (gsi, stmt);
9585 break;
9587 /* Transform TRUNC_DIV_EXPR and TRUNC_MOD_EXPR into RSHIFT_EXPR
9588 and BIT_AND_EXPR respectively if the first operand is greater
9589 than zero and the second operand is an exact power of two. */
9590 case TRUNC_DIV_EXPR:
9591 case TRUNC_MOD_EXPR:
9592 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1))
9593 && integer_pow2p (gimple_assign_rhs2 (stmt)))
9594 return simplify_div_or_mod_using_ranges (stmt);
9595 break;
9597 /* Transform ABS (X) into X or -X as appropriate. */
9598 case ABS_EXPR:
9599 if (TREE_CODE (rhs1) == SSA_NAME
9600 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
9601 return simplify_abs_using_ranges (stmt);
9602 break;
9604 case BIT_AND_EXPR:
9605 case BIT_IOR_EXPR:
9606 /* Optimize away BIT_AND_EXPR and BIT_IOR_EXPR
9607 if all the bits being cleared are already cleared or
9608 all the bits being set are already set. */
9609 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
9610 return simplify_bit_ops_using_ranges (gsi, stmt);
9611 break;
9613 CASE_CONVERT:
9614 if (TREE_CODE (rhs1) == SSA_NAME
9615 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
9616 return simplify_conversion_using_ranges (stmt);
9617 break;
9619 case FLOAT_EXPR:
9620 if (TREE_CODE (rhs1) == SSA_NAME
9621 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
9622 return simplify_float_conversion_using_ranges (gsi, stmt);
9623 break;
9625 default:
9626 break;
9629 else if (gimple_code (stmt) == GIMPLE_COND)
9630 return simplify_cond_using_ranges (stmt);
9631 else if (gimple_code (stmt) == GIMPLE_SWITCH)
9632 return simplify_switch_using_ranges (stmt);
9633 else if (is_gimple_call (stmt)
9634 && gimple_call_internal_p (stmt))
9635 return simplify_internal_call_using_ranges (gsi, stmt);
9637 return false;
9640 /* If the statement pointed by SI has a predicate whose value can be
9641 computed using the value range information computed by VRP, compute
9642 its value and return true. Otherwise, return false. */
9644 static bool
9645 fold_predicate_in (gimple_stmt_iterator *si)
9647 bool assignment_p = false;
9648 tree val;
9649 gimple stmt = gsi_stmt (*si);
9651 if (is_gimple_assign (stmt)
9652 && TREE_CODE_CLASS (gimple_assign_rhs_code (stmt)) == tcc_comparison)
9654 assignment_p = true;
9655 val = vrp_evaluate_conditional (gimple_assign_rhs_code (stmt),
9656 gimple_assign_rhs1 (stmt),
9657 gimple_assign_rhs2 (stmt),
9658 stmt);
9660 else if (gimple_code (stmt) == GIMPLE_COND)
9661 val = vrp_evaluate_conditional (gimple_cond_code (stmt),
9662 gimple_cond_lhs (stmt),
9663 gimple_cond_rhs (stmt),
9664 stmt);
9665 else
9666 return false;
9668 if (val)
9670 if (assignment_p)
9671 val = fold_convert (gimple_expr_type (stmt), val);
9673 if (dump_file)
9675 fprintf (dump_file, "Folding predicate ");
9676 print_gimple_expr (dump_file, stmt, 0, 0);
9677 fprintf (dump_file, " to ");
9678 print_generic_expr (dump_file, val, 0);
9679 fprintf (dump_file, "\n");
9682 if (is_gimple_assign (stmt))
9683 gimple_assign_set_rhs_from_tree (si, val);
9684 else
9686 gcc_assert (gimple_code (stmt) == GIMPLE_COND);
9687 if (integer_zerop (val))
9688 gimple_cond_make_false (stmt);
9689 else if (integer_onep (val))
9690 gimple_cond_make_true (stmt);
9691 else
9692 gcc_unreachable ();
9695 return true;
9698 return false;
9701 /* Callback for substitute_and_fold folding the stmt at *SI. */
9703 static bool
9704 vrp_fold_stmt (gimple_stmt_iterator *si)
9706 if (fold_predicate_in (si))
9707 return true;
9709 return simplify_stmt_using_ranges (si);
9712 /* Stack of dest,src equivalency pairs that need to be restored after
9713 each attempt to thread a block's incoming edge to an outgoing edge.
9715 A NULL entry is used to mark the end of pairs which need to be
9716 restored. */
9717 static vec<tree> equiv_stack;
9719 /* A trivial wrapper so that we can present the generic jump threading
9720 code with a simple API for simplifying statements. STMT is the
9721 statement we want to simplify, WITHIN_STMT provides the location
9722 for any overflow warnings. */
9724 static tree
9725 simplify_stmt_for_jump_threading (gimple stmt, gimple within_stmt)
9727 if (gimple_code (stmt) == GIMPLE_COND)
9728 return vrp_evaluate_conditional (gimple_cond_code (stmt),
9729 gimple_cond_lhs (stmt),
9730 gimple_cond_rhs (stmt), within_stmt);
9732 if (gimple_code (stmt) == GIMPLE_ASSIGN)
9734 value_range_t new_vr = VR_INITIALIZER;
9735 tree lhs = gimple_assign_lhs (stmt);
9737 if (TREE_CODE (lhs) == SSA_NAME
9738 && (INTEGRAL_TYPE_P (TREE_TYPE (lhs))
9739 || POINTER_TYPE_P (TREE_TYPE (lhs))))
9741 extract_range_from_assignment (&new_vr, stmt);
9742 if (range_int_cst_singleton_p (&new_vr))
9743 return new_vr.min;
9747 return NULL_TREE;
9750 /* Blocks which have more than one predecessor and more than
9751 one successor present jump threading opportunities, i.e.,
9752 when the block is reached from a specific predecessor, we
9753 may be able to determine which of the outgoing edges will
9754 be traversed. When this optimization applies, we are able
9755 to avoid conditionals at runtime and we may expose secondary
9756 optimization opportunities.
9758 This routine is effectively a driver for the generic jump
9759 threading code. It basically just presents the generic code
9760 with edges that may be suitable for jump threading.
9762 Unlike DOM, we do not iterate VRP if jump threading was successful.
9763 While iterating may expose new opportunities for VRP, it is expected
9764 those opportunities would be very limited and the compile time cost
9765 to expose those opportunities would be significant.
9767 As jump threading opportunities are discovered, they are registered
9768 for later realization. */
9770 static void
9771 identify_jump_threads (void)
9773 basic_block bb;
9774 gimple dummy;
9775 int i;
9776 edge e;
9778 /* Ugh. When substituting values earlier in this pass we can
9779 wipe the dominance information. So rebuild the dominator
9780 information as we need it within the jump threading code. */
9781 calculate_dominance_info (CDI_DOMINATORS);
9783 /* We do not allow VRP information to be used for jump threading
9784 across a back edge in the CFG. Otherwise it becomes too
9785 difficult to avoid eliminating loop exit tests. Of course
9786 EDGE_DFS_BACK is not accurate at this time so we have to
9787 recompute it. */
9788 mark_dfs_back_edges ();
9790 /* Do not thread across edges we are about to remove. Just marking
9791 them as EDGE_DFS_BACK will do. */
9792 FOR_EACH_VEC_ELT (to_remove_edges, i, e)
9793 e->flags |= EDGE_DFS_BACK;
9795 /* Allocate our unwinder stack to unwind any temporary equivalences
9796 that might be recorded. */
9797 equiv_stack.create (20);
9799 /* To avoid lots of silly node creation, we create a single
9800 conditional and just modify it in-place when attempting to
9801 thread jumps. */
9802 dummy = gimple_build_cond (EQ_EXPR,
9803 integer_zero_node, integer_zero_node,
9804 NULL, NULL);
9806 /* Walk through all the blocks finding those which present a
9807 potential jump threading opportunity. We could set this up
9808 as a dominator walker and record data during the walk, but
9809 I doubt it's worth the effort for the classes of jump
9810 threading opportunities we are trying to identify at this
9811 point in compilation. */
9812 FOR_EACH_BB_FN (bb, cfun)
9814 gimple last;
9816 /* If the generic jump threading code does not find this block
9817 interesting, then there is nothing to do. */
9818 if (! potentially_threadable_block (bb))
9819 continue;
9821 /* We only care about blocks ending in a COND_EXPR. While there
9822 may be some value in handling SWITCH_EXPR here, I doubt it's
9823 terribly important. */
9824 last = gsi_stmt (gsi_last_bb (bb));
9826 /* We're basically looking for a switch or any kind of conditional with
9827 integral or pointer type arguments. Note the type of the second
9828 argument will be the same as the first argument, so no need to
9829 check it explicitly. */
9830 if (gimple_code (last) == GIMPLE_SWITCH
9831 || (gimple_code (last) == GIMPLE_COND
9832 && TREE_CODE (gimple_cond_lhs (last)) == SSA_NAME
9833 && (INTEGRAL_TYPE_P (TREE_TYPE (gimple_cond_lhs (last)))
9834 || POINTER_TYPE_P (TREE_TYPE (gimple_cond_lhs (last))))
9835 && (TREE_CODE (gimple_cond_rhs (last)) == SSA_NAME
9836 || is_gimple_min_invariant (gimple_cond_rhs (last)))))
9838 edge_iterator ei;
9840 /* We've got a block with multiple predecessors and multiple
9841 successors which also ends in a suitable conditional or
9842 switch statement. For each predecessor, see if we can thread
9843 it to a specific successor. */
9844 FOR_EACH_EDGE (e, ei, bb->preds)
9846 /* Do not thread across back edges or abnormal edges
9847 in the CFG. */
9848 if (e->flags & (EDGE_DFS_BACK | EDGE_COMPLEX))
9849 continue;
9851 thread_across_edge (dummy, e, true, &equiv_stack,
9852 simplify_stmt_for_jump_threading);
9857 /* We do not actually update the CFG or SSA graphs at this point as
9858 ASSERT_EXPRs are still in the IL and cfg cleanup code does not yet
9859 handle ASSERT_EXPRs gracefully. */
9862 /* We identified all the jump threading opportunities earlier, but could
9863 not transform the CFG at that time. This routine transforms the
9864 CFG and arranges for the dominator tree to be rebuilt if necessary.
9866 Note the SSA graph update will occur during the normal TODO
9867 processing by the pass manager. */
9868 static void
9869 finalize_jump_threads (void)
9871 thread_through_all_blocks (false);
9872 equiv_stack.release ();
9876 /* Traverse all the blocks folding conditionals with known ranges. */
9878 static void
9879 vrp_finalize (void)
9881 size_t i;
9883 values_propagated = true;
9885 if (dump_file)
9887 fprintf (dump_file, "\nValue ranges after VRP:\n\n");
9888 dump_all_value_ranges (dump_file);
9889 fprintf (dump_file, "\n");
9892 substitute_and_fold (op_with_constant_singleton_value_range,
9893 vrp_fold_stmt, false);
9895 if (warn_array_bounds)
9896 check_all_array_refs ();
9898 /* We must identify jump threading opportunities before we release
9899 the datastructures built by VRP. */
9900 identify_jump_threads ();
9902 /* Set value range to non pointer SSA_NAMEs. */
9903 for (i = 0; i < num_vr_values; i++)
9904 if (vr_value[i])
9906 tree name = ssa_name (i);
9908 if (!name
9909 || POINTER_TYPE_P (TREE_TYPE (name))
9910 || (vr_value[i]->type == VR_VARYING)
9911 || (vr_value[i]->type == VR_UNDEFINED))
9912 continue;
9914 if ((TREE_CODE (vr_value[i]->min) == INTEGER_CST)
9915 && (TREE_CODE (vr_value[i]->max) == INTEGER_CST)
9916 && (vr_value[i]->type == VR_RANGE
9917 || vr_value[i]->type == VR_ANTI_RANGE))
9918 set_range_info (name, vr_value[i]->type, vr_value[i]->min,
9919 vr_value[i]->max);
9922 /* Free allocated memory. */
9923 for (i = 0; i < num_vr_values; i++)
9924 if (vr_value[i])
9926 BITMAP_FREE (vr_value[i]->equiv);
9927 free (vr_value[i]);
9930 free (vr_value);
9931 free (vr_phi_edge_counts);
9933 /* So that we can distinguish between VRP data being available
9934 and not available. */
9935 vr_value = NULL;
9936 vr_phi_edge_counts = NULL;
9940 /* Main entry point to VRP (Value Range Propagation). This pass is
9941 loosely based on J. R. C. Patterson, ``Accurate Static Branch
9942 Prediction by Value Range Propagation,'' in SIGPLAN Conference on
9943 Programming Language Design and Implementation, pp. 67-78, 1995.
9944 Also available at http://citeseer.ist.psu.edu/patterson95accurate.html
9946 This is essentially an SSA-CCP pass modified to deal with ranges
9947 instead of constants.
9949 While propagating ranges, we may find that two or more SSA name
9950 have equivalent, though distinct ranges. For instance,
9952 1 x_9 = p_3->a;
9953 2 p_4 = ASSERT_EXPR <p_3, p_3 != 0>
9954 3 if (p_4 == q_2)
9955 4 p_5 = ASSERT_EXPR <p_4, p_4 == q_2>;
9956 5 endif
9957 6 if (q_2)
9959 In the code above, pointer p_5 has range [q_2, q_2], but from the
9960 code we can also determine that p_5 cannot be NULL and, if q_2 had
9961 a non-varying range, p_5's range should also be compatible with it.
9963 These equivalences are created by two expressions: ASSERT_EXPR and
9964 copy operations. Since p_5 is an assertion on p_4, and p_4 was the
9965 result of another assertion, then we can use the fact that p_5 and
9966 p_4 are equivalent when evaluating p_5's range.
9968 Together with value ranges, we also propagate these equivalences
9969 between names so that we can take advantage of information from
9970 multiple ranges when doing final replacement. Note that this
9971 equivalency relation is transitive but not symmetric.
9973 In the example above, p_5 is equivalent to p_4, q_2 and p_3, but we
9974 cannot assert that q_2 is equivalent to p_5 because q_2 may be used
9975 in contexts where that assertion does not hold (e.g., in line 6).
9977 TODO, the main difference between this pass and Patterson's is that
9978 we do not propagate edge probabilities. We only compute whether
9979 edges can be taken or not. That is, instead of having a spectrum
9980 of jump probabilities between 0 and 1, we only deal with 0, 1 and
9981 DON'T KNOW. In the future, it may be worthwhile to propagate
9982 probabilities to aid branch prediction. */
9984 static unsigned int
9985 execute_vrp (void)
9987 int i;
9988 edge e;
9989 switch_update *su;
9991 loop_optimizer_init (LOOPS_NORMAL | LOOPS_HAVE_RECORDED_EXITS);
9992 rewrite_into_loop_closed_ssa (NULL, TODO_update_ssa);
9993 scev_initialize ();
9995 /* ??? This ends up using stale EDGE_DFS_BACK for liveness computation.
9996 Inserting assertions may split edges which will invalidate
9997 EDGE_DFS_BACK. */
9998 insert_range_assertions ();
10000 to_remove_edges.create (10);
10001 to_update_switch_stmts.create (5);
10002 threadedge_initialize_values ();
10004 /* For visiting PHI nodes we need EDGE_DFS_BACK computed. */
10005 mark_dfs_back_edges ();
10007 vrp_initialize ();
10008 ssa_propagate (vrp_visit_stmt, vrp_visit_phi_node);
10009 vrp_finalize ();
10011 free_numbers_of_iterations_estimates ();
10013 /* ASSERT_EXPRs must be removed before finalizing jump threads
10014 as finalizing jump threads calls the CFG cleanup code which
10015 does not properly handle ASSERT_EXPRs. */
10016 remove_range_assertions ();
10018 /* If we exposed any new variables, go ahead and put them into
10019 SSA form now, before we handle jump threading. This simplifies
10020 interactions between rewriting of _DECL nodes into SSA form
10021 and rewriting SSA_NAME nodes into SSA form after block
10022 duplication and CFG manipulation. */
10023 update_ssa (TODO_update_ssa);
10025 finalize_jump_threads ();
10027 /* Remove dead edges from SWITCH_EXPR optimization. This leaves the
10028 CFG in a broken state and requires a cfg_cleanup run. */
10029 FOR_EACH_VEC_ELT (to_remove_edges, i, e)
10030 remove_edge (e);
10031 /* Update SWITCH_EXPR case label vector. */
10032 FOR_EACH_VEC_ELT (to_update_switch_stmts, i, su)
10034 size_t j;
10035 size_t n = TREE_VEC_LENGTH (su->vec);
10036 tree label;
10037 gimple_switch_set_num_labels (su->stmt, n);
10038 for (j = 0; j < n; j++)
10039 gimple_switch_set_label (su->stmt, j, TREE_VEC_ELT (su->vec, j));
10040 /* As we may have replaced the default label with a regular one
10041 make sure to make it a real default label again. This ensures
10042 optimal expansion. */
10043 label = gimple_switch_label (su->stmt, 0);
10044 CASE_LOW (label) = NULL_TREE;
10045 CASE_HIGH (label) = NULL_TREE;
10048 if (to_remove_edges.length () > 0)
10050 free_dominance_info (CDI_DOMINATORS);
10051 loops_state_set (LOOPS_NEED_FIXUP);
10054 to_remove_edges.release ();
10055 to_update_switch_stmts.release ();
10056 threadedge_finalize_values ();
10058 scev_finalize ();
10059 loop_optimizer_finalize ();
10060 return 0;
10063 namespace {
10065 const pass_data pass_data_vrp =
10067 GIMPLE_PASS, /* type */
10068 "vrp", /* name */
10069 OPTGROUP_NONE, /* optinfo_flags */
10070 TV_TREE_VRP, /* tv_id */
10071 PROP_ssa, /* properties_required */
10072 0, /* properties_provided */
10073 0, /* properties_destroyed */
10074 0, /* todo_flags_start */
10075 ( TODO_cleanup_cfg | TODO_update_ssa ), /* todo_flags_finish */
10078 class pass_vrp : public gimple_opt_pass
10080 public:
10081 pass_vrp (gcc::context *ctxt)
10082 : gimple_opt_pass (pass_data_vrp, ctxt)
10085 /* opt_pass methods: */
10086 opt_pass * clone () { return new pass_vrp (m_ctxt); }
10087 virtual bool gate (function *) { return flag_tree_vrp != 0; }
10088 virtual unsigned int execute (function *) { return execute_vrp (); }
10090 }; // class pass_vrp
10092 } // anon namespace
10094 gimple_opt_pass *
10095 make_pass_vrp (gcc::context *ctxt)
10097 return new pass_vrp (ctxt);