rs6000-c.c (altivec_resolve_overloaded_builtin): Reformat two multi-line strings.
[official-gcc.git] / gcc / tree-vrp.c
blob06364b70f3317acc4cc9c0510b003ea36ba4946c
1 /* Support routines for Value Range Propagation (VRP).
2 Copyright (C) 2005-2016 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 "backend.h"
25 #include "insn-codes.h"
26 #include "rtl.h"
27 #include "tree.h"
28 #include "gimple.h"
29 #include "cfghooks.h"
30 #include "tree-pass.h"
31 #include "ssa.h"
32 #include "optabs-tree.h"
33 #include "gimple-pretty-print.h"
34 #include "diagnostic-core.h"
35 #include "flags.h"
36 #include "fold-const.h"
37 #include "stor-layout.h"
38 #include "calls.h"
39 #include "cfganal.h"
40 #include "gimple-fold.h"
41 #include "tree-eh.h"
42 #include "gimple-iterator.h"
43 #include "gimple-walk.h"
44 #include "tree-cfg.h"
45 #include "tree-ssa-loop-manip.h"
46 #include "tree-ssa-loop-niter.h"
47 #include "tree-ssa-loop.h"
48 #include "tree-into-ssa.h"
49 #include "tree-ssa.h"
50 #include "intl.h"
51 #include "cfgloop.h"
52 #include "tree-scalar-evolution.h"
53 #include "tree-ssa-propagate.h"
54 #include "tree-chrec.h"
55 #include "tree-ssa-threadupdate.h"
56 #include "tree-ssa-scopedtables.h"
57 #include "tree-ssa-threadedge.h"
58 #include "omp-low.h"
59 #include "target.h"
60 #include "case-cfn-macros.h"
62 /* Range of values that can be associated with an SSA_NAME after VRP
63 has executed. */
64 struct value_range
66 /* Lattice value represented by this range. */
67 enum value_range_type type;
69 /* Minimum and maximum values represented by this range. These
70 values should be interpreted as follows:
72 - If TYPE is VR_UNDEFINED or VR_VARYING then MIN and MAX must
73 be NULL.
75 - If TYPE == VR_RANGE then MIN holds the minimum value and
76 MAX holds the maximum value of the range [MIN, MAX].
78 - If TYPE == ANTI_RANGE the variable is known to NOT
79 take any values in the range [MIN, MAX]. */
80 tree min;
81 tree max;
83 /* Set of SSA names whose value ranges are equivalent to this one.
84 This set is only valid when TYPE is VR_RANGE or VR_ANTI_RANGE. */
85 bitmap equiv;
88 #define VR_INITIALIZER { VR_UNDEFINED, NULL_TREE, NULL_TREE, NULL }
90 /* Set of SSA names found live during the RPO traversal of the function
91 for still active basic-blocks. */
92 static sbitmap *live;
94 /* Return true if the SSA name NAME is live on the edge E. */
96 static bool
97 live_on_edge (edge e, tree name)
99 return (live[e->dest->index]
100 && bitmap_bit_p (live[e->dest->index], SSA_NAME_VERSION (name)));
103 /* Local functions. */
104 static int compare_values (tree val1, tree val2);
105 static int compare_values_warnv (tree val1, tree val2, bool *);
106 static void vrp_meet (value_range *, value_range *);
107 static void vrp_intersect_ranges (value_range *, value_range *);
108 static tree vrp_evaluate_conditional_warnv_with_ops (enum tree_code,
109 tree, tree, bool, bool *,
110 bool *);
112 /* Location information for ASSERT_EXPRs. Each instance of this
113 structure describes an ASSERT_EXPR for an SSA name. Since a single
114 SSA name may have more than one assertion associated with it, these
115 locations are kept in a linked list attached to the corresponding
116 SSA name. */
117 struct assert_locus
119 /* Basic block where the assertion would be inserted. */
120 basic_block bb;
122 /* Some assertions need to be inserted on an edge (e.g., assertions
123 generated by COND_EXPRs). In those cases, BB will be NULL. */
124 edge e;
126 /* Pointer to the statement that generated this assertion. */
127 gimple_stmt_iterator si;
129 /* Predicate code for the ASSERT_EXPR. Must be COMPARISON_CLASS_P. */
130 enum tree_code comp_code;
132 /* Value being compared against. */
133 tree val;
135 /* Expression to compare. */
136 tree expr;
138 /* Next node in the linked list. */
139 assert_locus *next;
142 /* If bit I is present, it means that SSA name N_i has a list of
143 assertions that should be inserted in the IL. */
144 static bitmap need_assert_for;
146 /* Array of locations lists where to insert assertions. ASSERTS_FOR[I]
147 holds a list of ASSERT_LOCUS_T nodes that describe where
148 ASSERT_EXPRs for SSA name N_I should be inserted. */
149 static assert_locus **asserts_for;
151 /* Value range array. After propagation, VR_VALUE[I] holds the range
152 of values that SSA name N_I may take. */
153 static unsigned num_vr_values;
154 static value_range **vr_value;
155 static bool values_propagated;
157 /* For a PHI node which sets SSA name N_I, VR_COUNTS[I] holds the
158 number of executable edges we saw the last time we visited the
159 node. */
160 static int *vr_phi_edge_counts;
162 struct switch_update {
163 gswitch *stmt;
164 tree vec;
167 static vec<edge> to_remove_edges;
168 static vec<switch_update> to_update_switch_stmts;
171 /* Return the maximum value for TYPE. */
173 static inline tree
174 vrp_val_max (const_tree type)
176 if (!INTEGRAL_TYPE_P (type))
177 return NULL_TREE;
179 return TYPE_MAX_VALUE (type);
182 /* Return the minimum value for TYPE. */
184 static inline tree
185 vrp_val_min (const_tree type)
187 if (!INTEGRAL_TYPE_P (type))
188 return NULL_TREE;
190 return TYPE_MIN_VALUE (type);
193 /* Return whether VAL is equal to the maximum value of its type. This
194 will be true for a positive overflow infinity. We can't do a
195 simple equality comparison with TYPE_MAX_VALUE because C typedefs
196 and Ada subtypes can produce types whose TYPE_MAX_VALUE is not ==
197 to the integer constant with the same value in the type. */
199 static inline bool
200 vrp_val_is_max (const_tree val)
202 tree type_max = vrp_val_max (TREE_TYPE (val));
203 return (val == type_max
204 || (type_max != NULL_TREE
205 && operand_equal_p (val, type_max, 0)));
208 /* Return whether VAL is equal to the minimum value of its type. This
209 will be true for a negative overflow infinity. */
211 static inline bool
212 vrp_val_is_min (const_tree val)
214 tree type_min = vrp_val_min (TREE_TYPE (val));
215 return (val == type_min
216 || (type_min != NULL_TREE
217 && operand_equal_p (val, type_min, 0)));
221 /* Return whether TYPE should use an overflow infinity distinct from
222 TYPE_{MIN,MAX}_VALUE. We use an overflow infinity value to
223 represent a signed overflow during VRP computations. An infinity
224 is distinct from a half-range, which will go from some number to
225 TYPE_{MIN,MAX}_VALUE. */
227 static inline bool
228 needs_overflow_infinity (const_tree type)
230 return INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_WRAPS (type);
233 /* Return whether TYPE can support our overflow infinity
234 representation: we use the TREE_OVERFLOW flag, which only exists
235 for constants. If TYPE doesn't support this, we don't optimize
236 cases which would require signed overflow--we drop them to
237 VARYING. */
239 static inline bool
240 supports_overflow_infinity (const_tree type)
242 tree min = vrp_val_min (type), max = vrp_val_max (type);
243 gcc_checking_assert (needs_overflow_infinity (type));
244 return (min != NULL_TREE
245 && CONSTANT_CLASS_P (min)
246 && max != NULL_TREE
247 && CONSTANT_CLASS_P (max));
250 /* VAL is the maximum or minimum value of a type. Return a
251 corresponding overflow infinity. */
253 static inline tree
254 make_overflow_infinity (tree val)
256 gcc_checking_assert (val != NULL_TREE && CONSTANT_CLASS_P (val));
257 val = copy_node (val);
258 TREE_OVERFLOW (val) = 1;
259 return val;
262 /* Return a negative overflow infinity for TYPE. */
264 static inline tree
265 negative_overflow_infinity (tree type)
267 gcc_checking_assert (supports_overflow_infinity (type));
268 return make_overflow_infinity (vrp_val_min (type));
271 /* Return a positive overflow infinity for TYPE. */
273 static inline tree
274 positive_overflow_infinity (tree type)
276 gcc_checking_assert (supports_overflow_infinity (type));
277 return make_overflow_infinity (vrp_val_max (type));
280 /* Return whether VAL is a negative overflow infinity. */
282 static inline bool
283 is_negative_overflow_infinity (const_tree val)
285 return (TREE_OVERFLOW_P (val)
286 && needs_overflow_infinity (TREE_TYPE (val))
287 && vrp_val_is_min (val));
290 /* Return whether VAL is a positive overflow infinity. */
292 static inline bool
293 is_positive_overflow_infinity (const_tree val)
295 return (TREE_OVERFLOW_P (val)
296 && needs_overflow_infinity (TREE_TYPE (val))
297 && vrp_val_is_max (val));
300 /* Return whether VAL is a positive or negative overflow infinity. */
302 static inline bool
303 is_overflow_infinity (const_tree val)
305 return (TREE_OVERFLOW_P (val)
306 && needs_overflow_infinity (TREE_TYPE (val))
307 && (vrp_val_is_min (val) || vrp_val_is_max (val)));
310 /* Return whether STMT has a constant rhs that is_overflow_infinity. */
312 static inline bool
313 stmt_overflow_infinity (gimple *stmt)
315 if (is_gimple_assign (stmt)
316 && get_gimple_rhs_class (gimple_assign_rhs_code (stmt)) ==
317 GIMPLE_SINGLE_RHS)
318 return is_overflow_infinity (gimple_assign_rhs1 (stmt));
319 return false;
322 /* If VAL is now an overflow infinity, return VAL. Otherwise, return
323 the same value with TREE_OVERFLOW clear. This can be used to avoid
324 confusing a regular value with an overflow value. */
326 static inline tree
327 avoid_overflow_infinity (tree val)
329 if (!is_overflow_infinity (val))
330 return val;
332 if (vrp_val_is_max (val))
333 return vrp_val_max (TREE_TYPE (val));
334 else
336 gcc_checking_assert (vrp_val_is_min (val));
337 return vrp_val_min (TREE_TYPE (val));
342 /* Set value range VR to VR_UNDEFINED. */
344 static inline void
345 set_value_range_to_undefined (value_range *vr)
347 vr->type = VR_UNDEFINED;
348 vr->min = vr->max = NULL_TREE;
349 if (vr->equiv)
350 bitmap_clear (vr->equiv);
354 /* Set value range VR to VR_VARYING. */
356 static inline void
357 set_value_range_to_varying (value_range *vr)
359 vr->type = VR_VARYING;
360 vr->min = vr->max = NULL_TREE;
361 if (vr->equiv)
362 bitmap_clear (vr->equiv);
366 /* Set value range VR to {T, MIN, MAX, EQUIV}. */
368 static void
369 set_value_range (value_range *vr, enum value_range_type t, tree min,
370 tree max, bitmap equiv)
372 /* Check the validity of the range. */
373 if (flag_checking
374 && (t == VR_RANGE || t == VR_ANTI_RANGE))
376 int cmp;
378 gcc_assert (min && max);
380 gcc_assert ((!TREE_OVERFLOW_P (min) || is_overflow_infinity (min))
381 && (!TREE_OVERFLOW_P (max) || is_overflow_infinity (max)));
383 if (INTEGRAL_TYPE_P (TREE_TYPE (min)) && t == VR_ANTI_RANGE)
384 gcc_assert (!vrp_val_is_min (min) || !vrp_val_is_max (max));
386 cmp = compare_values (min, max);
387 gcc_assert (cmp == 0 || cmp == -1 || cmp == -2);
389 if (needs_overflow_infinity (TREE_TYPE (min)))
390 gcc_assert (!is_overflow_infinity (min)
391 || !is_overflow_infinity (max));
394 if (flag_checking
395 && (t == VR_UNDEFINED || t == VR_VARYING))
397 gcc_assert (min == NULL_TREE && max == NULL_TREE);
398 gcc_assert (equiv == NULL || bitmap_empty_p (equiv));
401 vr->type = t;
402 vr->min = min;
403 vr->max = max;
405 /* Since updating the equivalence set involves deep copying the
406 bitmaps, only do it if absolutely necessary. */
407 if (vr->equiv == NULL
408 && equiv != NULL)
409 vr->equiv = BITMAP_ALLOC (NULL);
411 if (equiv != vr->equiv)
413 if (equiv && !bitmap_empty_p (equiv))
414 bitmap_copy (vr->equiv, equiv);
415 else
416 bitmap_clear (vr->equiv);
421 /* Set value range VR to the canonical form of {T, MIN, MAX, EQUIV}.
422 This means adjusting T, MIN and MAX representing the case of a
423 wrapping range with MAX < MIN covering [MIN, type_max] U [type_min, MAX]
424 as anti-rage ~[MAX+1, MIN-1]. Likewise for wrapping anti-ranges.
425 In corner cases where MAX+1 or MIN-1 wraps this will fall back
426 to varying.
427 This routine exists to ease canonicalization in the case where we
428 extract ranges from var + CST op limit. */
430 static void
431 set_and_canonicalize_value_range (value_range *vr, enum value_range_type t,
432 tree min, tree max, bitmap equiv)
434 /* Use the canonical setters for VR_UNDEFINED and VR_VARYING. */
435 if (t == VR_UNDEFINED)
437 set_value_range_to_undefined (vr);
438 return;
440 else if (t == VR_VARYING)
442 set_value_range_to_varying (vr);
443 return;
446 /* Nothing to canonicalize for symbolic ranges. */
447 if (TREE_CODE (min) != INTEGER_CST
448 || TREE_CODE (max) != INTEGER_CST)
450 set_value_range (vr, t, min, max, equiv);
451 return;
454 /* Wrong order for min and max, to swap them and the VR type we need
455 to adjust them. */
456 if (tree_int_cst_lt (max, min))
458 tree one, tmp;
460 /* For one bit precision if max < min, then the swapped
461 range covers all values, so for VR_RANGE it is varying and
462 for VR_ANTI_RANGE empty range, so drop to varying as well. */
463 if (TYPE_PRECISION (TREE_TYPE (min)) == 1)
465 set_value_range_to_varying (vr);
466 return;
469 one = build_int_cst (TREE_TYPE (min), 1);
470 tmp = int_const_binop (PLUS_EXPR, max, one);
471 max = int_const_binop (MINUS_EXPR, min, one);
472 min = tmp;
474 /* There's one corner case, if we had [C+1, C] before we now have
475 that again. But this represents an empty value range, so drop
476 to varying in this case. */
477 if (tree_int_cst_lt (max, min))
479 set_value_range_to_varying (vr);
480 return;
483 t = t == VR_RANGE ? VR_ANTI_RANGE : VR_RANGE;
486 /* Anti-ranges that can be represented as ranges should be so. */
487 if (t == VR_ANTI_RANGE)
489 bool is_min = vrp_val_is_min (min);
490 bool is_max = vrp_val_is_max (max);
492 if (is_min && is_max)
494 /* We cannot deal with empty ranges, drop to varying.
495 ??? This could be VR_UNDEFINED instead. */
496 set_value_range_to_varying (vr);
497 return;
499 else if (TYPE_PRECISION (TREE_TYPE (min)) == 1
500 && (is_min || is_max))
502 /* Non-empty boolean ranges can always be represented
503 as a singleton range. */
504 if (is_min)
505 min = max = vrp_val_max (TREE_TYPE (min));
506 else
507 min = max = vrp_val_min (TREE_TYPE (min));
508 t = VR_RANGE;
510 else if (is_min
511 /* As a special exception preserve non-null ranges. */
512 && !(TYPE_UNSIGNED (TREE_TYPE (min))
513 && integer_zerop (max)))
515 tree one = build_int_cst (TREE_TYPE (max), 1);
516 min = int_const_binop (PLUS_EXPR, max, one);
517 max = vrp_val_max (TREE_TYPE (max));
518 t = VR_RANGE;
520 else if (is_max)
522 tree one = build_int_cst (TREE_TYPE (min), 1);
523 max = int_const_binop (MINUS_EXPR, min, one);
524 min = vrp_val_min (TREE_TYPE (min));
525 t = VR_RANGE;
529 /* Drop [-INF(OVF), +INF(OVF)] to varying. */
530 if (needs_overflow_infinity (TREE_TYPE (min))
531 && is_overflow_infinity (min)
532 && is_overflow_infinity (max))
534 set_value_range_to_varying (vr);
535 return;
538 set_value_range (vr, t, min, max, equiv);
541 /* Copy value range FROM into value range TO. */
543 static inline void
544 copy_value_range (value_range *to, value_range *from)
546 set_value_range (to, from->type, from->min, from->max, from->equiv);
549 /* Set value range VR to a single value. This function is only called
550 with values we get from statements, and exists to clear the
551 TREE_OVERFLOW flag so that we don't think we have an overflow
552 infinity when we shouldn't. */
554 static inline void
555 set_value_range_to_value (value_range *vr, tree val, bitmap equiv)
557 gcc_assert (is_gimple_min_invariant (val));
558 if (TREE_OVERFLOW_P (val))
559 val = drop_tree_overflow (val);
560 set_value_range (vr, VR_RANGE, val, val, equiv);
563 /* Set value range VR to a non-negative range of type TYPE.
564 OVERFLOW_INFINITY indicates whether to use an overflow infinity
565 rather than TYPE_MAX_VALUE; this should be true if we determine
566 that the range is nonnegative based on the assumption that signed
567 overflow does not occur. */
569 static inline void
570 set_value_range_to_nonnegative (value_range *vr, tree type,
571 bool overflow_infinity)
573 tree zero;
575 if (overflow_infinity && !supports_overflow_infinity (type))
577 set_value_range_to_varying (vr);
578 return;
581 zero = build_int_cst (type, 0);
582 set_value_range (vr, VR_RANGE, zero,
583 (overflow_infinity
584 ? positive_overflow_infinity (type)
585 : TYPE_MAX_VALUE (type)),
586 vr->equiv);
589 /* Set value range VR to a non-NULL range of type TYPE. */
591 static inline void
592 set_value_range_to_nonnull (value_range *vr, tree type)
594 tree zero = build_int_cst (type, 0);
595 set_value_range (vr, VR_ANTI_RANGE, zero, zero, vr->equiv);
599 /* Set value range VR to a NULL range of type TYPE. */
601 static inline void
602 set_value_range_to_null (value_range *vr, tree type)
604 set_value_range_to_value (vr, build_int_cst (type, 0), vr->equiv);
608 /* Set value range VR to a range of a truthvalue of type TYPE. */
610 static inline void
611 set_value_range_to_truthvalue (value_range *vr, tree type)
613 if (TYPE_PRECISION (type) == 1)
614 set_value_range_to_varying (vr);
615 else
616 set_value_range (vr, VR_RANGE,
617 build_int_cst (type, 0), build_int_cst (type, 1),
618 vr->equiv);
622 /* If abs (min) < abs (max), set VR to [-max, max], if
623 abs (min) >= abs (max), set VR to [-min, min]. */
625 static void
626 abs_extent_range (value_range *vr, tree min, tree max)
628 int cmp;
630 gcc_assert (TREE_CODE (min) == INTEGER_CST);
631 gcc_assert (TREE_CODE (max) == INTEGER_CST);
632 gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (min)));
633 gcc_assert (!TYPE_UNSIGNED (TREE_TYPE (min)));
634 min = fold_unary (ABS_EXPR, TREE_TYPE (min), min);
635 max = fold_unary (ABS_EXPR, TREE_TYPE (max), max);
636 if (TREE_OVERFLOW (min) || TREE_OVERFLOW (max))
638 set_value_range_to_varying (vr);
639 return;
641 cmp = compare_values (min, max);
642 if (cmp == -1)
643 min = fold_unary (NEGATE_EXPR, TREE_TYPE (min), max);
644 else if (cmp == 0 || cmp == 1)
646 max = min;
647 min = fold_unary (NEGATE_EXPR, TREE_TYPE (min), min);
649 else
651 set_value_range_to_varying (vr);
652 return;
654 set_and_canonicalize_value_range (vr, VR_RANGE, min, max, NULL);
658 /* Return value range information for VAR.
660 If we have no values ranges recorded (ie, VRP is not running), then
661 return NULL. Otherwise create an empty range if none existed for VAR. */
663 static value_range *
664 get_value_range (const_tree var)
666 static const value_range vr_const_varying
667 = { VR_VARYING, NULL_TREE, NULL_TREE, NULL };
668 value_range *vr;
669 tree sym;
670 unsigned ver = SSA_NAME_VERSION (var);
672 /* If we have no recorded ranges, then return NULL. */
673 if (! vr_value)
674 return NULL;
676 /* If we query the range for a new SSA name return an unmodifiable VARYING.
677 We should get here at most from the substitute-and-fold stage which
678 will never try to change values. */
679 if (ver >= num_vr_values)
680 return CONST_CAST (value_range *, &vr_const_varying);
682 vr = vr_value[ver];
683 if (vr)
684 return vr;
686 /* After propagation finished do not allocate new value-ranges. */
687 if (values_propagated)
688 return CONST_CAST (value_range *, &vr_const_varying);
690 /* Create a default value range. */
691 vr_value[ver] = vr = XCNEW (value_range);
693 /* Defer allocating the equivalence set. */
694 vr->equiv = NULL;
696 /* If VAR is a default definition of a parameter, the variable can
697 take any value in VAR's type. */
698 if (SSA_NAME_IS_DEFAULT_DEF (var))
700 sym = SSA_NAME_VAR (var);
701 if (TREE_CODE (sym) == PARM_DECL)
703 /* Try to use the "nonnull" attribute to create ~[0, 0]
704 anti-ranges for pointers. Note that this is only valid with
705 default definitions of PARM_DECLs. */
706 if (POINTER_TYPE_P (TREE_TYPE (sym))
707 && nonnull_arg_p (sym))
708 set_value_range_to_nonnull (vr, TREE_TYPE (sym));
709 else
710 set_value_range_to_varying (vr);
712 else if (TREE_CODE (sym) == RESULT_DECL
713 && DECL_BY_REFERENCE (sym))
714 set_value_range_to_nonnull (vr, TREE_TYPE (sym));
717 return vr;
720 /* Return true, if VAL1 and VAL2 are equal values for VRP purposes. */
722 static inline bool
723 vrp_operand_equal_p (const_tree val1, const_tree val2)
725 if (val1 == val2)
726 return true;
727 if (!val1 || !val2 || !operand_equal_p (val1, val2, 0))
728 return false;
729 return is_overflow_infinity (val1) == is_overflow_infinity (val2);
732 /* Return true, if the bitmaps B1 and B2 are equal. */
734 static inline bool
735 vrp_bitmap_equal_p (const_bitmap b1, const_bitmap b2)
737 return (b1 == b2
738 || ((!b1 || bitmap_empty_p (b1))
739 && (!b2 || bitmap_empty_p (b2)))
740 || (b1 && b2
741 && bitmap_equal_p (b1, b2)));
744 /* Update the value range and equivalence set for variable VAR to
745 NEW_VR. Return true if NEW_VR is different from VAR's previous
746 value.
748 NOTE: This function assumes that NEW_VR is a temporary value range
749 object created for the sole purpose of updating VAR's range. The
750 storage used by the equivalence set from NEW_VR will be freed by
751 this function. Do not call update_value_range when NEW_VR
752 is the range object associated with another SSA name. */
754 static inline bool
755 update_value_range (const_tree var, value_range *new_vr)
757 value_range *old_vr;
758 bool is_new;
760 /* If there is a value-range on the SSA name from earlier analysis
761 factor that in. */
762 if (INTEGRAL_TYPE_P (TREE_TYPE (var)))
764 wide_int min, max;
765 value_range_type rtype = get_range_info (var, &min, &max);
766 if (rtype == VR_RANGE || rtype == VR_ANTI_RANGE)
768 value_range nr;
769 nr.type = rtype;
770 nr.min = wide_int_to_tree (TREE_TYPE (var), min);
771 nr.max = wide_int_to_tree (TREE_TYPE (var), max);
772 nr.equiv = NULL;
773 vrp_intersect_ranges (new_vr, &nr);
777 /* Update the value range, if necessary. */
778 old_vr = get_value_range (var);
779 is_new = old_vr->type != new_vr->type
780 || !vrp_operand_equal_p (old_vr->min, new_vr->min)
781 || !vrp_operand_equal_p (old_vr->max, new_vr->max)
782 || !vrp_bitmap_equal_p (old_vr->equiv, new_vr->equiv);
784 if (is_new)
786 /* Do not allow transitions up the lattice. The following
787 is slightly more awkward than just new_vr->type < old_vr->type
788 because VR_RANGE and VR_ANTI_RANGE need to be considered
789 the same. We may not have is_new when transitioning to
790 UNDEFINED. If old_vr->type is VARYING, we shouldn't be
791 called. */
792 if (new_vr->type == VR_UNDEFINED)
794 BITMAP_FREE (new_vr->equiv);
795 set_value_range_to_varying (old_vr);
796 set_value_range_to_varying (new_vr);
797 return true;
799 else
800 set_value_range (old_vr, new_vr->type, new_vr->min, new_vr->max,
801 new_vr->equiv);
804 BITMAP_FREE (new_vr->equiv);
806 return is_new;
810 /* Add VAR and VAR's equivalence set to EQUIV. This is the central
811 point where equivalence processing can be turned on/off. */
813 static void
814 add_equivalence (bitmap *equiv, const_tree var)
816 unsigned ver = SSA_NAME_VERSION (var);
817 value_range *vr = vr_value[ver];
819 if (*equiv == NULL)
820 *equiv = BITMAP_ALLOC (NULL);
821 bitmap_set_bit (*equiv, ver);
822 if (vr && vr->equiv)
823 bitmap_ior_into (*equiv, vr->equiv);
827 /* Return true if VR is ~[0, 0]. */
829 static inline bool
830 range_is_nonnull (value_range *vr)
832 return vr->type == VR_ANTI_RANGE
833 && integer_zerop (vr->min)
834 && integer_zerop (vr->max);
838 /* Return true if VR is [0, 0]. */
840 static inline bool
841 range_is_null (value_range *vr)
843 return vr->type == VR_RANGE
844 && integer_zerop (vr->min)
845 && integer_zerop (vr->max);
848 /* Return true if max and min of VR are INTEGER_CST. It's not necessary
849 a singleton. */
851 static inline bool
852 range_int_cst_p (value_range *vr)
854 return (vr->type == VR_RANGE
855 && TREE_CODE (vr->max) == INTEGER_CST
856 && TREE_CODE (vr->min) == INTEGER_CST);
859 /* Return true if VR is a INTEGER_CST singleton. */
861 static inline bool
862 range_int_cst_singleton_p (value_range *vr)
864 return (range_int_cst_p (vr)
865 && !is_overflow_infinity (vr->min)
866 && !is_overflow_infinity (vr->max)
867 && tree_int_cst_equal (vr->min, vr->max));
870 /* Return true if value range VR involves at least one symbol. */
872 static inline bool
873 symbolic_range_p (value_range *vr)
875 return (!is_gimple_min_invariant (vr->min)
876 || !is_gimple_min_invariant (vr->max));
879 /* Return the single symbol (an SSA_NAME) contained in T if any, or NULL_TREE
880 otherwise. We only handle additive operations and set NEG to true if the
881 symbol is negated and INV to the invariant part, if any. */
883 static tree
884 get_single_symbol (tree t, bool *neg, tree *inv)
886 bool neg_;
887 tree inv_;
889 if (TREE_CODE (t) == PLUS_EXPR
890 || TREE_CODE (t) == POINTER_PLUS_EXPR
891 || TREE_CODE (t) == MINUS_EXPR)
893 if (is_gimple_min_invariant (TREE_OPERAND (t, 0)))
895 neg_ = (TREE_CODE (t) == MINUS_EXPR);
896 inv_ = TREE_OPERAND (t, 0);
897 t = TREE_OPERAND (t, 1);
899 else if (is_gimple_min_invariant (TREE_OPERAND (t, 1)))
901 neg_ = false;
902 inv_ = TREE_OPERAND (t, 1);
903 t = TREE_OPERAND (t, 0);
905 else
906 return NULL_TREE;
908 else
910 neg_ = false;
911 inv_ = NULL_TREE;
914 if (TREE_CODE (t) == NEGATE_EXPR)
916 t = TREE_OPERAND (t, 0);
917 neg_ = !neg_;
920 if (TREE_CODE (t) != SSA_NAME)
921 return NULL_TREE;
923 *neg = neg_;
924 *inv = inv_;
925 return t;
928 /* The reverse operation: build a symbolic expression with TYPE
929 from symbol SYM, negated according to NEG, and invariant INV. */
931 static tree
932 build_symbolic_expr (tree type, tree sym, bool neg, tree inv)
934 const bool pointer_p = POINTER_TYPE_P (type);
935 tree t = sym;
937 if (neg)
938 t = build1 (NEGATE_EXPR, type, t);
940 if (integer_zerop (inv))
941 return t;
943 return build2 (pointer_p ? POINTER_PLUS_EXPR : PLUS_EXPR, type, t, inv);
946 /* Return true if value range VR involves exactly one symbol SYM. */
948 static bool
949 symbolic_range_based_on_p (value_range *vr, const_tree sym)
951 bool neg, min_has_symbol, max_has_symbol;
952 tree inv;
954 if (is_gimple_min_invariant (vr->min))
955 min_has_symbol = false;
956 else if (get_single_symbol (vr->min, &neg, &inv) == sym)
957 min_has_symbol = true;
958 else
959 return false;
961 if (is_gimple_min_invariant (vr->max))
962 max_has_symbol = false;
963 else if (get_single_symbol (vr->max, &neg, &inv) == sym)
964 max_has_symbol = true;
965 else
966 return false;
968 return (min_has_symbol || max_has_symbol);
971 /* Return true if value range VR uses an overflow infinity. */
973 static inline bool
974 overflow_infinity_range_p (value_range *vr)
976 return (vr->type == VR_RANGE
977 && (is_overflow_infinity (vr->min)
978 || is_overflow_infinity (vr->max)));
981 /* Return false if we can not make a valid comparison based on VR;
982 this will be the case if it uses an overflow infinity and overflow
983 is not undefined (i.e., -fno-strict-overflow is in effect).
984 Otherwise return true, and set *STRICT_OVERFLOW_P to true if VR
985 uses an overflow infinity. */
987 static bool
988 usable_range_p (value_range *vr, bool *strict_overflow_p)
990 gcc_assert (vr->type == VR_RANGE);
991 if (is_overflow_infinity (vr->min))
993 *strict_overflow_p = true;
994 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (vr->min)))
995 return false;
997 if (is_overflow_infinity (vr->max))
999 *strict_overflow_p = true;
1000 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (vr->max)))
1001 return false;
1003 return true;
1006 /* Return true if the result of assignment STMT is know to be non-zero.
1007 If the return value is based on the assumption that signed overflow is
1008 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1009 *STRICT_OVERFLOW_P.*/
1011 static bool
1012 gimple_assign_nonzero_warnv_p (gimple *stmt, bool *strict_overflow_p)
1014 enum tree_code code = gimple_assign_rhs_code (stmt);
1015 switch (get_gimple_rhs_class (code))
1017 case GIMPLE_UNARY_RHS:
1018 return tree_unary_nonzero_warnv_p (gimple_assign_rhs_code (stmt),
1019 gimple_expr_type (stmt),
1020 gimple_assign_rhs1 (stmt),
1021 strict_overflow_p);
1022 case GIMPLE_BINARY_RHS:
1023 return tree_binary_nonzero_warnv_p (gimple_assign_rhs_code (stmt),
1024 gimple_expr_type (stmt),
1025 gimple_assign_rhs1 (stmt),
1026 gimple_assign_rhs2 (stmt),
1027 strict_overflow_p);
1028 case GIMPLE_TERNARY_RHS:
1029 return false;
1030 case GIMPLE_SINGLE_RHS:
1031 return tree_single_nonzero_warnv_p (gimple_assign_rhs1 (stmt),
1032 strict_overflow_p);
1033 case GIMPLE_INVALID_RHS:
1034 gcc_unreachable ();
1035 default:
1036 gcc_unreachable ();
1040 /* Return true if STMT is known to compute a non-zero value.
1041 If the return value is based on the assumption that signed overflow is
1042 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1043 *STRICT_OVERFLOW_P.*/
1045 static bool
1046 gimple_stmt_nonzero_warnv_p (gimple *stmt, bool *strict_overflow_p)
1048 switch (gimple_code (stmt))
1050 case GIMPLE_ASSIGN:
1051 return gimple_assign_nonzero_warnv_p (stmt, strict_overflow_p);
1052 case GIMPLE_CALL:
1054 tree fndecl = gimple_call_fndecl (stmt);
1055 if (!fndecl) return false;
1056 if (flag_delete_null_pointer_checks && !flag_check_new
1057 && DECL_IS_OPERATOR_NEW (fndecl)
1058 && !TREE_NOTHROW (fndecl))
1059 return true;
1060 /* References are always non-NULL. */
1061 if (flag_delete_null_pointer_checks
1062 && TREE_CODE (TREE_TYPE (fndecl)) == REFERENCE_TYPE)
1063 return true;
1064 if (flag_delete_null_pointer_checks &&
1065 lookup_attribute ("returns_nonnull",
1066 TYPE_ATTRIBUTES (gimple_call_fntype (stmt))))
1067 return true;
1068 return gimple_alloca_call_p (stmt);
1070 default:
1071 gcc_unreachable ();
1075 /* Like tree_expr_nonzero_warnv_p, but this function uses value ranges
1076 obtained so far. */
1078 static bool
1079 vrp_stmt_computes_nonzero (gimple *stmt, bool *strict_overflow_p)
1081 if (gimple_stmt_nonzero_warnv_p (stmt, strict_overflow_p))
1082 return true;
1084 /* If we have an expression of the form &X->a, then the expression
1085 is nonnull if X is nonnull. */
1086 if (is_gimple_assign (stmt)
1087 && gimple_assign_rhs_code (stmt) == ADDR_EXPR)
1089 tree expr = gimple_assign_rhs1 (stmt);
1090 tree base = get_base_address (TREE_OPERAND (expr, 0));
1092 if (base != NULL_TREE
1093 && TREE_CODE (base) == MEM_REF
1094 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
1096 value_range *vr = get_value_range (TREE_OPERAND (base, 0));
1097 if (range_is_nonnull (vr))
1098 return true;
1102 return false;
1105 /* Returns true if EXPR is a valid value (as expected by compare_values) --
1106 a gimple invariant, or SSA_NAME +- CST. */
1108 static bool
1109 valid_value_p (tree expr)
1111 if (TREE_CODE (expr) == SSA_NAME)
1112 return true;
1114 if (TREE_CODE (expr) == PLUS_EXPR
1115 || TREE_CODE (expr) == MINUS_EXPR)
1116 return (TREE_CODE (TREE_OPERAND (expr, 0)) == SSA_NAME
1117 && TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST);
1119 return is_gimple_min_invariant (expr);
1122 /* Return
1123 1 if VAL < VAL2
1124 0 if !(VAL < VAL2)
1125 -2 if those are incomparable. */
1126 static inline int
1127 operand_less_p (tree val, tree val2)
1129 /* LT is folded faster than GE and others. Inline the common case. */
1130 if (TREE_CODE (val) == INTEGER_CST && TREE_CODE (val2) == INTEGER_CST)
1131 return tree_int_cst_lt (val, val2);
1132 else
1134 tree tcmp;
1136 fold_defer_overflow_warnings ();
1138 tcmp = fold_binary_to_constant (LT_EXPR, boolean_type_node, val, val2);
1140 fold_undefer_and_ignore_overflow_warnings ();
1142 if (!tcmp
1143 || TREE_CODE (tcmp) != INTEGER_CST)
1144 return -2;
1146 if (!integer_zerop (tcmp))
1147 return 1;
1150 /* val >= val2, not considering overflow infinity. */
1151 if (is_negative_overflow_infinity (val))
1152 return is_negative_overflow_infinity (val2) ? 0 : 1;
1153 else if (is_positive_overflow_infinity (val2))
1154 return is_positive_overflow_infinity (val) ? 0 : 1;
1156 return 0;
1159 /* Compare two values VAL1 and VAL2. Return
1161 -2 if VAL1 and VAL2 cannot be compared at compile-time,
1162 -1 if VAL1 < VAL2,
1163 0 if VAL1 == VAL2,
1164 +1 if VAL1 > VAL2, and
1165 +2 if VAL1 != VAL2
1167 This is similar to tree_int_cst_compare but supports pointer values
1168 and values that cannot be compared at compile time.
1170 If STRICT_OVERFLOW_P is not NULL, then set *STRICT_OVERFLOW_P to
1171 true if the return value is only valid if we assume that signed
1172 overflow is undefined. */
1174 static int
1175 compare_values_warnv (tree val1, tree val2, bool *strict_overflow_p)
1177 if (val1 == val2)
1178 return 0;
1180 /* Below we rely on the fact that VAL1 and VAL2 are both pointers or
1181 both integers. */
1182 gcc_assert (POINTER_TYPE_P (TREE_TYPE (val1))
1183 == POINTER_TYPE_P (TREE_TYPE (val2)));
1185 /* Convert the two values into the same type. This is needed because
1186 sizetype causes sign extension even for unsigned types. */
1187 val2 = fold_convert (TREE_TYPE (val1), val2);
1188 STRIP_USELESS_TYPE_CONVERSION (val2);
1190 const bool overflow_undefined
1191 = INTEGRAL_TYPE_P (TREE_TYPE (val1))
1192 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (val1));
1193 tree inv1, inv2;
1194 bool neg1, neg2;
1195 tree sym1 = get_single_symbol (val1, &neg1, &inv1);
1196 tree sym2 = get_single_symbol (val2, &neg2, &inv2);
1198 /* If VAL1 and VAL2 are of the form '[-]NAME [+ CST]', return -1 or +1
1199 accordingly. If VAL1 and VAL2 don't use the same name, return -2. */
1200 if (sym1 && sym2)
1202 /* Both values must use the same name with the same sign. */
1203 if (sym1 != sym2 || neg1 != neg2)
1204 return -2;
1206 /* [-]NAME + CST == [-]NAME + CST. */
1207 if (inv1 == inv2)
1208 return 0;
1210 /* If overflow is defined we cannot simplify more. */
1211 if (!overflow_undefined)
1212 return -2;
1214 if (strict_overflow_p != NULL
1215 && (!inv1 || !TREE_NO_WARNING (val1))
1216 && (!inv2 || !TREE_NO_WARNING (val2)))
1217 *strict_overflow_p = true;
1219 if (!inv1)
1220 inv1 = build_int_cst (TREE_TYPE (val1), 0);
1221 if (!inv2)
1222 inv2 = build_int_cst (TREE_TYPE (val2), 0);
1224 return compare_values_warnv (inv1, inv2, strict_overflow_p);
1227 const bool cst1 = is_gimple_min_invariant (val1);
1228 const bool cst2 = is_gimple_min_invariant (val2);
1230 /* If one is of the form '[-]NAME + CST' and the other is constant, then
1231 it might be possible to say something depending on the constants. */
1232 if ((sym1 && inv1 && cst2) || (sym2 && inv2 && cst1))
1234 if (!overflow_undefined)
1235 return -2;
1237 if (strict_overflow_p != NULL
1238 && (!sym1 || !TREE_NO_WARNING (val1))
1239 && (!sym2 || !TREE_NO_WARNING (val2)))
1240 *strict_overflow_p = true;
1242 const signop sgn = TYPE_SIGN (TREE_TYPE (val1));
1243 tree cst = cst1 ? val1 : val2;
1244 tree inv = cst1 ? inv2 : inv1;
1246 /* Compute the difference between the constants. If it overflows or
1247 underflows, this means that we can trivially compare the NAME with
1248 it and, consequently, the two values with each other. */
1249 wide_int diff = wi::sub (cst, inv);
1250 if (wi::cmp (0, inv, sgn) != wi::cmp (diff, cst, sgn))
1252 const int res = wi::cmp (cst, inv, sgn);
1253 return cst1 ? res : -res;
1256 return -2;
1259 /* We cannot say anything more for non-constants. */
1260 if (!cst1 || !cst2)
1261 return -2;
1263 if (!POINTER_TYPE_P (TREE_TYPE (val1)))
1265 /* We cannot compare overflowed values, except for overflow
1266 infinities. */
1267 if (TREE_OVERFLOW (val1) || TREE_OVERFLOW (val2))
1269 if (strict_overflow_p != NULL)
1270 *strict_overflow_p = true;
1271 if (is_negative_overflow_infinity (val1))
1272 return is_negative_overflow_infinity (val2) ? 0 : -1;
1273 else if (is_negative_overflow_infinity (val2))
1274 return 1;
1275 else if (is_positive_overflow_infinity (val1))
1276 return is_positive_overflow_infinity (val2) ? 0 : 1;
1277 else if (is_positive_overflow_infinity (val2))
1278 return -1;
1279 return -2;
1282 return tree_int_cst_compare (val1, val2);
1284 else
1286 tree t;
1288 /* First see if VAL1 and VAL2 are not the same. */
1289 if (val1 == val2 || operand_equal_p (val1, val2, 0))
1290 return 0;
1292 /* If VAL1 is a lower address than VAL2, return -1. */
1293 if (operand_less_p (val1, val2) == 1)
1294 return -1;
1296 /* If VAL1 is a higher address than VAL2, return +1. */
1297 if (operand_less_p (val2, val1) == 1)
1298 return 1;
1300 /* If VAL1 is different than VAL2, return +2.
1301 For integer constants we either have already returned -1 or 1
1302 or they are equivalent. We still might succeed in proving
1303 something about non-trivial operands. */
1304 if (TREE_CODE (val1) != INTEGER_CST
1305 || TREE_CODE (val2) != INTEGER_CST)
1307 t = fold_binary_to_constant (NE_EXPR, boolean_type_node, val1, val2);
1308 if (t && integer_onep (t))
1309 return 2;
1312 return -2;
1316 /* Compare values like compare_values_warnv, but treat comparisons of
1317 nonconstants which rely on undefined overflow as incomparable. */
1319 static int
1320 compare_values (tree val1, tree val2)
1322 bool sop;
1323 int ret;
1325 sop = false;
1326 ret = compare_values_warnv (val1, val2, &sop);
1327 if (sop
1328 && (!is_gimple_min_invariant (val1) || !is_gimple_min_invariant (val2)))
1329 ret = -2;
1330 return ret;
1334 /* Return 1 if VAL is inside value range MIN <= VAL <= MAX,
1335 0 if VAL is not inside [MIN, MAX],
1336 -2 if we cannot tell either way.
1338 Benchmark compile/20001226-1.c compilation time after changing this
1339 function. */
1341 static inline int
1342 value_inside_range (tree val, tree min, tree max)
1344 int cmp1, cmp2;
1346 cmp1 = operand_less_p (val, min);
1347 if (cmp1 == -2)
1348 return -2;
1349 if (cmp1 == 1)
1350 return 0;
1352 cmp2 = operand_less_p (max, val);
1353 if (cmp2 == -2)
1354 return -2;
1356 return !cmp2;
1360 /* Return true if value ranges VR0 and VR1 have a non-empty
1361 intersection.
1363 Benchmark compile/20001226-1.c compilation time after changing this
1364 function.
1367 static inline bool
1368 value_ranges_intersect_p (value_range *vr0, value_range *vr1)
1370 /* The value ranges do not intersect if the maximum of the first range is
1371 less than the minimum of the second range or vice versa.
1372 When those relations are unknown, we can't do any better. */
1373 if (operand_less_p (vr0->max, vr1->min) != 0)
1374 return false;
1375 if (operand_less_p (vr1->max, vr0->min) != 0)
1376 return false;
1377 return true;
1381 /* Return 1 if [MIN, MAX] includes the value zero, 0 if it does not
1382 include the value zero, -2 if we cannot tell. */
1384 static inline int
1385 range_includes_zero_p (tree min, tree max)
1387 tree zero = build_int_cst (TREE_TYPE (min), 0);
1388 return value_inside_range (zero, min, max);
1391 /* Return true if *VR is know to only contain nonnegative values. */
1393 static inline bool
1394 value_range_nonnegative_p (value_range *vr)
1396 /* Testing for VR_ANTI_RANGE is not useful here as any anti-range
1397 which would return a useful value should be encoded as a
1398 VR_RANGE. */
1399 if (vr->type == VR_RANGE)
1401 int result = compare_values (vr->min, integer_zero_node);
1402 return (result == 0 || result == 1);
1405 return false;
1408 /* If *VR has a value rante that is a single constant value return that,
1409 otherwise return NULL_TREE. */
1411 static tree
1412 value_range_constant_singleton (value_range *vr)
1414 if (vr->type == VR_RANGE
1415 && operand_equal_p (vr->min, vr->max, 0)
1416 && is_gimple_min_invariant (vr->min))
1417 return vr->min;
1419 return NULL_TREE;
1422 /* If OP has a value range with a single constant value return that,
1423 otherwise return NULL_TREE. This returns OP itself if OP is a
1424 constant. */
1426 static tree
1427 op_with_constant_singleton_value_range (tree op)
1429 if (is_gimple_min_invariant (op))
1430 return op;
1432 if (TREE_CODE (op) != SSA_NAME)
1433 return NULL_TREE;
1435 return value_range_constant_singleton (get_value_range (op));
1438 /* Return true if op is in a boolean [0, 1] value-range. */
1440 static bool
1441 op_with_boolean_value_range_p (tree op)
1443 value_range *vr;
1445 if (TYPE_PRECISION (TREE_TYPE (op)) == 1)
1446 return true;
1448 if (integer_zerop (op)
1449 || integer_onep (op))
1450 return true;
1452 if (TREE_CODE (op) != SSA_NAME)
1453 return false;
1455 vr = get_value_range (op);
1456 return (vr->type == VR_RANGE
1457 && integer_zerop (vr->min)
1458 && integer_onep (vr->max));
1461 /* Extract value range information from an ASSERT_EXPR EXPR and store
1462 it in *VR_P. */
1464 static void
1465 extract_range_from_assert (value_range *vr_p, tree expr)
1467 tree var, cond, limit, min, max, type;
1468 value_range *limit_vr;
1469 enum tree_code cond_code;
1471 var = ASSERT_EXPR_VAR (expr);
1472 cond = ASSERT_EXPR_COND (expr);
1474 gcc_assert (COMPARISON_CLASS_P (cond));
1476 /* Find VAR in the ASSERT_EXPR conditional. */
1477 if (var == TREE_OPERAND (cond, 0)
1478 || TREE_CODE (TREE_OPERAND (cond, 0)) == PLUS_EXPR
1479 || TREE_CODE (TREE_OPERAND (cond, 0)) == NOP_EXPR)
1481 /* If the predicate is of the form VAR COMP LIMIT, then we just
1482 take LIMIT from the RHS and use the same comparison code. */
1483 cond_code = TREE_CODE (cond);
1484 limit = TREE_OPERAND (cond, 1);
1485 cond = TREE_OPERAND (cond, 0);
1487 else
1489 /* If the predicate is of the form LIMIT COMP VAR, then we need
1490 to flip around the comparison code to create the proper range
1491 for VAR. */
1492 cond_code = swap_tree_comparison (TREE_CODE (cond));
1493 limit = TREE_OPERAND (cond, 0);
1494 cond = TREE_OPERAND (cond, 1);
1497 limit = avoid_overflow_infinity (limit);
1499 type = TREE_TYPE (var);
1500 gcc_assert (limit != var);
1502 /* For pointer arithmetic, we only keep track of pointer equality
1503 and inequality. */
1504 if (POINTER_TYPE_P (type) && cond_code != NE_EXPR && cond_code != EQ_EXPR)
1506 set_value_range_to_varying (vr_p);
1507 return;
1510 /* If LIMIT is another SSA name and LIMIT has a range of its own,
1511 try to use LIMIT's range to avoid creating symbolic ranges
1512 unnecessarily. */
1513 limit_vr = (TREE_CODE (limit) == SSA_NAME) ? get_value_range (limit) : NULL;
1515 /* LIMIT's range is only interesting if it has any useful information. */
1516 if (! limit_vr
1517 || limit_vr->type == VR_UNDEFINED
1518 || limit_vr->type == VR_VARYING
1519 || (symbolic_range_p (limit_vr)
1520 && ! (limit_vr->type == VR_RANGE
1521 && (limit_vr->min == limit_vr->max
1522 || operand_equal_p (limit_vr->min, limit_vr->max, 0)))))
1523 limit_vr = NULL;
1525 /* Initially, the new range has the same set of equivalences of
1526 VAR's range. This will be revised before returning the final
1527 value. Since assertions may be chained via mutually exclusive
1528 predicates, we will need to trim the set of equivalences before
1529 we are done. */
1530 gcc_assert (vr_p->equiv == NULL);
1531 add_equivalence (&vr_p->equiv, var);
1533 /* Extract a new range based on the asserted comparison for VAR and
1534 LIMIT's value range. Notice that if LIMIT has an anti-range, we
1535 will only use it for equality comparisons (EQ_EXPR). For any
1536 other kind of assertion, we cannot derive a range from LIMIT's
1537 anti-range that can be used to describe the new range. For
1538 instance, ASSERT_EXPR <x_2, x_2 <= b_4>. If b_4 is ~[2, 10],
1539 then b_4 takes on the ranges [-INF, 1] and [11, +INF]. There is
1540 no single range for x_2 that could describe LE_EXPR, so we might
1541 as well build the range [b_4, +INF] for it.
1542 One special case we handle is extracting a range from a
1543 range test encoded as (unsigned)var + CST <= limit. */
1544 if (TREE_CODE (cond) == NOP_EXPR
1545 || TREE_CODE (cond) == PLUS_EXPR)
1547 if (TREE_CODE (cond) == PLUS_EXPR)
1549 min = fold_build1 (NEGATE_EXPR, TREE_TYPE (TREE_OPERAND (cond, 1)),
1550 TREE_OPERAND (cond, 1));
1551 max = int_const_binop (PLUS_EXPR, limit, min);
1552 cond = TREE_OPERAND (cond, 0);
1554 else
1556 min = build_int_cst (TREE_TYPE (var), 0);
1557 max = limit;
1560 /* Make sure to not set TREE_OVERFLOW on the final type
1561 conversion. We are willingly interpreting large positive
1562 unsigned values as negative signed values here. */
1563 min = force_fit_type (TREE_TYPE (var), wi::to_widest (min), 0, false);
1564 max = force_fit_type (TREE_TYPE (var), wi::to_widest (max), 0, false);
1566 /* We can transform a max, min range to an anti-range or
1567 vice-versa. Use set_and_canonicalize_value_range which does
1568 this for us. */
1569 if (cond_code == LE_EXPR)
1570 set_and_canonicalize_value_range (vr_p, VR_RANGE,
1571 min, max, vr_p->equiv);
1572 else if (cond_code == GT_EXPR)
1573 set_and_canonicalize_value_range (vr_p, VR_ANTI_RANGE,
1574 min, max, vr_p->equiv);
1575 else
1576 gcc_unreachable ();
1578 else if (cond_code == EQ_EXPR)
1580 enum value_range_type range_type;
1582 if (limit_vr)
1584 range_type = limit_vr->type;
1585 min = limit_vr->min;
1586 max = limit_vr->max;
1588 else
1590 range_type = VR_RANGE;
1591 min = limit;
1592 max = limit;
1595 set_value_range (vr_p, range_type, min, max, vr_p->equiv);
1597 /* When asserting the equality VAR == LIMIT and LIMIT is another
1598 SSA name, the new range will also inherit the equivalence set
1599 from LIMIT. */
1600 if (TREE_CODE (limit) == SSA_NAME)
1601 add_equivalence (&vr_p->equiv, limit);
1603 else if (cond_code == NE_EXPR)
1605 /* As described above, when LIMIT's range is an anti-range and
1606 this assertion is an inequality (NE_EXPR), then we cannot
1607 derive anything from the anti-range. For instance, if
1608 LIMIT's range was ~[0, 0], the assertion 'VAR != LIMIT' does
1609 not imply that VAR's range is [0, 0]. So, in the case of
1610 anti-ranges, we just assert the inequality using LIMIT and
1611 not its anti-range.
1613 If LIMIT_VR is a range, we can only use it to build a new
1614 anti-range if LIMIT_VR is a single-valued range. For
1615 instance, if LIMIT_VR is [0, 1], the predicate
1616 VAR != [0, 1] does not mean that VAR's range is ~[0, 1].
1617 Rather, it means that for value 0 VAR should be ~[0, 0]
1618 and for value 1, VAR should be ~[1, 1]. We cannot
1619 represent these ranges.
1621 The only situation in which we can build a valid
1622 anti-range is when LIMIT_VR is a single-valued range
1623 (i.e., LIMIT_VR->MIN == LIMIT_VR->MAX). In that case,
1624 build the anti-range ~[LIMIT_VR->MIN, LIMIT_VR->MAX]. */
1625 if (limit_vr
1626 && limit_vr->type == VR_RANGE
1627 && compare_values (limit_vr->min, limit_vr->max) == 0)
1629 min = limit_vr->min;
1630 max = limit_vr->max;
1632 else
1634 /* In any other case, we cannot use LIMIT's range to build a
1635 valid anti-range. */
1636 min = max = limit;
1639 /* If MIN and MAX cover the whole range for their type, then
1640 just use the original LIMIT. */
1641 if (INTEGRAL_TYPE_P (type)
1642 && vrp_val_is_min (min)
1643 && vrp_val_is_max (max))
1644 min = max = limit;
1646 set_and_canonicalize_value_range (vr_p, VR_ANTI_RANGE,
1647 min, max, vr_p->equiv);
1649 else if (cond_code == LE_EXPR || cond_code == LT_EXPR)
1651 min = TYPE_MIN_VALUE (type);
1653 if (limit_vr == NULL || limit_vr->type == VR_ANTI_RANGE)
1654 max = limit;
1655 else
1657 /* If LIMIT_VR is of the form [N1, N2], we need to build the
1658 range [MIN, N2] for LE_EXPR and [MIN, N2 - 1] for
1659 LT_EXPR. */
1660 max = limit_vr->max;
1663 /* If the maximum value forces us to be out of bounds, simply punt.
1664 It would be pointless to try and do anything more since this
1665 all should be optimized away above us. */
1666 if ((cond_code == LT_EXPR
1667 && compare_values (max, min) == 0)
1668 || is_overflow_infinity (max))
1669 set_value_range_to_varying (vr_p);
1670 else
1672 /* For LT_EXPR, we create the range [MIN, MAX - 1]. */
1673 if (cond_code == LT_EXPR)
1675 if (TYPE_PRECISION (TREE_TYPE (max)) == 1
1676 && !TYPE_UNSIGNED (TREE_TYPE (max)))
1677 max = fold_build2 (PLUS_EXPR, TREE_TYPE (max), max,
1678 build_int_cst (TREE_TYPE (max), -1));
1679 else
1680 max = fold_build2 (MINUS_EXPR, TREE_TYPE (max), max,
1681 build_int_cst (TREE_TYPE (max), 1));
1682 if (EXPR_P (max))
1683 TREE_NO_WARNING (max) = 1;
1686 set_value_range (vr_p, VR_RANGE, min, max, vr_p->equiv);
1689 else if (cond_code == GE_EXPR || cond_code == GT_EXPR)
1691 max = TYPE_MAX_VALUE (type);
1693 if (limit_vr == NULL || limit_vr->type == VR_ANTI_RANGE)
1694 min = limit;
1695 else
1697 /* If LIMIT_VR is of the form [N1, N2], we need to build the
1698 range [N1, MAX] for GE_EXPR and [N1 + 1, MAX] for
1699 GT_EXPR. */
1700 min = limit_vr->min;
1703 /* If the minimum value forces us to be out of bounds, simply punt.
1704 It would be pointless to try and do anything more since this
1705 all should be optimized away above us. */
1706 if ((cond_code == GT_EXPR
1707 && compare_values (min, max) == 0)
1708 || is_overflow_infinity (min))
1709 set_value_range_to_varying (vr_p);
1710 else
1712 /* For GT_EXPR, we create the range [MIN + 1, MAX]. */
1713 if (cond_code == GT_EXPR)
1715 if (TYPE_PRECISION (TREE_TYPE (min)) == 1
1716 && !TYPE_UNSIGNED (TREE_TYPE (min)))
1717 min = fold_build2 (MINUS_EXPR, TREE_TYPE (min), min,
1718 build_int_cst (TREE_TYPE (min), -1));
1719 else
1720 min = fold_build2 (PLUS_EXPR, TREE_TYPE (min), min,
1721 build_int_cst (TREE_TYPE (min), 1));
1722 if (EXPR_P (min))
1723 TREE_NO_WARNING (min) = 1;
1726 set_value_range (vr_p, VR_RANGE, min, max, vr_p->equiv);
1729 else
1730 gcc_unreachable ();
1732 /* Finally intersect the new range with what we already know about var. */
1733 vrp_intersect_ranges (vr_p, get_value_range (var));
1737 /* Extract range information from SSA name VAR and store it in VR. If
1738 VAR has an interesting range, use it. Otherwise, create the
1739 range [VAR, VAR] and return it. This is useful in situations where
1740 we may have conditionals testing values of VARYING names. For
1741 instance,
1743 x_3 = y_5;
1744 if (x_3 > y_5)
1747 Even if y_5 is deemed VARYING, we can determine that x_3 > y_5 is
1748 always false. */
1750 static void
1751 extract_range_from_ssa_name (value_range *vr, tree var)
1753 value_range *var_vr = get_value_range (var);
1755 if (var_vr->type != VR_VARYING)
1756 copy_value_range (vr, var_vr);
1757 else
1758 set_value_range (vr, VR_RANGE, var, var, NULL);
1760 add_equivalence (&vr->equiv, var);
1764 /* Wrapper around int_const_binop. If the operation overflows and we
1765 are not using wrapping arithmetic, then adjust the result to be
1766 -INF or +INF depending on CODE, VAL1 and VAL2. This can return
1767 NULL_TREE if we need to use an overflow infinity representation but
1768 the type does not support it. */
1770 static tree
1771 vrp_int_const_binop (enum tree_code code, tree val1, tree val2)
1773 tree res;
1775 res = int_const_binop (code, val1, val2);
1777 /* If we are using unsigned arithmetic, operate symbolically
1778 on -INF and +INF as int_const_binop only handles signed overflow. */
1779 if (TYPE_UNSIGNED (TREE_TYPE (val1)))
1781 int checkz = compare_values (res, val1);
1782 bool overflow = false;
1784 /* Ensure that res = val1 [+*] val2 >= val1
1785 or that res = val1 - val2 <= val1. */
1786 if ((code == PLUS_EXPR
1787 && !(checkz == 1 || checkz == 0))
1788 || (code == MINUS_EXPR
1789 && !(checkz == 0 || checkz == -1)))
1791 overflow = true;
1793 /* Checking for multiplication overflow is done by dividing the
1794 output of the multiplication by the first input of the
1795 multiplication. If the result of that division operation is
1796 not equal to the second input of the multiplication, then the
1797 multiplication overflowed. */
1798 else if (code == MULT_EXPR && !integer_zerop (val1))
1800 tree tmp = int_const_binop (TRUNC_DIV_EXPR,
1801 res,
1802 val1);
1803 int check = compare_values (tmp, val2);
1805 if (check != 0)
1806 overflow = true;
1809 if (overflow)
1811 res = copy_node (res);
1812 TREE_OVERFLOW (res) = 1;
1816 else if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (val1)))
1817 /* If the singed operation wraps then int_const_binop has done
1818 everything we want. */
1820 /* Signed division of -1/0 overflows and by the time it gets here
1821 returns NULL_TREE. */
1822 else if (!res)
1823 return NULL_TREE;
1824 else if ((TREE_OVERFLOW (res)
1825 && !TREE_OVERFLOW (val1)
1826 && !TREE_OVERFLOW (val2))
1827 || is_overflow_infinity (val1)
1828 || is_overflow_infinity (val2))
1830 /* If the operation overflowed but neither VAL1 nor VAL2 are
1831 overflown, return -INF or +INF depending on the operation
1832 and the combination of signs of the operands. */
1833 int sgn1 = tree_int_cst_sgn (val1);
1834 int sgn2 = tree_int_cst_sgn (val2);
1836 if (needs_overflow_infinity (TREE_TYPE (res))
1837 && !supports_overflow_infinity (TREE_TYPE (res)))
1838 return NULL_TREE;
1840 /* We have to punt on adding infinities of different signs,
1841 since we can't tell what the sign of the result should be.
1842 Likewise for subtracting infinities of the same sign. */
1843 if (((code == PLUS_EXPR && sgn1 != sgn2)
1844 || (code == MINUS_EXPR && sgn1 == sgn2))
1845 && is_overflow_infinity (val1)
1846 && is_overflow_infinity (val2))
1847 return NULL_TREE;
1849 /* Don't try to handle division or shifting of infinities. */
1850 if ((code == TRUNC_DIV_EXPR
1851 || code == FLOOR_DIV_EXPR
1852 || code == CEIL_DIV_EXPR
1853 || code == EXACT_DIV_EXPR
1854 || code == ROUND_DIV_EXPR
1855 || code == RSHIFT_EXPR)
1856 && (is_overflow_infinity (val1)
1857 || is_overflow_infinity (val2)))
1858 return NULL_TREE;
1860 /* Notice that we only need to handle the restricted set of
1861 operations handled by extract_range_from_binary_expr.
1862 Among them, only multiplication, addition and subtraction
1863 can yield overflow without overflown operands because we
1864 are working with integral types only... except in the
1865 case VAL1 = -INF and VAL2 = -1 which overflows to +INF
1866 for division too. */
1868 /* For multiplication, the sign of the overflow is given
1869 by the comparison of the signs of the operands. */
1870 if ((code == MULT_EXPR && sgn1 == sgn2)
1871 /* For addition, the operands must be of the same sign
1872 to yield an overflow. Its sign is therefore that
1873 of one of the operands, for example the first. For
1874 infinite operands X + -INF is negative, not positive. */
1875 || (code == PLUS_EXPR
1876 && (sgn1 >= 0
1877 ? !is_negative_overflow_infinity (val2)
1878 : is_positive_overflow_infinity (val2)))
1879 /* For subtraction, non-infinite operands must be of
1880 different signs to yield an overflow. Its sign is
1881 therefore that of the first operand or the opposite of
1882 that of the second operand. A first operand of 0 counts
1883 as positive here, for the corner case 0 - (-INF), which
1884 overflows, but must yield +INF. For infinite operands 0
1885 - INF is negative, not positive. */
1886 || (code == MINUS_EXPR
1887 && (sgn1 >= 0
1888 ? !is_positive_overflow_infinity (val2)
1889 : is_negative_overflow_infinity (val2)))
1890 /* We only get in here with positive shift count, so the
1891 overflow direction is the same as the sign of val1.
1892 Actually rshift does not overflow at all, but we only
1893 handle the case of shifting overflowed -INF and +INF. */
1894 || (code == RSHIFT_EXPR
1895 && sgn1 >= 0)
1896 /* For division, the only case is -INF / -1 = +INF. */
1897 || code == TRUNC_DIV_EXPR
1898 || code == FLOOR_DIV_EXPR
1899 || code == CEIL_DIV_EXPR
1900 || code == EXACT_DIV_EXPR
1901 || code == ROUND_DIV_EXPR)
1902 return (needs_overflow_infinity (TREE_TYPE (res))
1903 ? positive_overflow_infinity (TREE_TYPE (res))
1904 : TYPE_MAX_VALUE (TREE_TYPE (res)));
1905 else
1906 return (needs_overflow_infinity (TREE_TYPE (res))
1907 ? negative_overflow_infinity (TREE_TYPE (res))
1908 : TYPE_MIN_VALUE (TREE_TYPE (res)));
1911 return res;
1915 /* For range VR compute two wide_int bitmasks. In *MAY_BE_NONZERO
1916 bitmask if some bit is unset, it means for all numbers in the range
1917 the bit is 0, otherwise it might be 0 or 1. In *MUST_BE_NONZERO
1918 bitmask if some bit is set, it means for all numbers in the range
1919 the bit is 1, otherwise it might be 0 or 1. */
1921 static bool
1922 zero_nonzero_bits_from_vr (const tree expr_type,
1923 value_range *vr,
1924 wide_int *may_be_nonzero,
1925 wide_int *must_be_nonzero)
1927 *may_be_nonzero = wi::minus_one (TYPE_PRECISION (expr_type));
1928 *must_be_nonzero = wi::zero (TYPE_PRECISION (expr_type));
1929 if (!range_int_cst_p (vr)
1930 || is_overflow_infinity (vr->min)
1931 || is_overflow_infinity (vr->max))
1932 return false;
1934 if (range_int_cst_singleton_p (vr))
1936 *may_be_nonzero = vr->min;
1937 *must_be_nonzero = *may_be_nonzero;
1939 else if (tree_int_cst_sgn (vr->min) >= 0
1940 || tree_int_cst_sgn (vr->max) < 0)
1942 wide_int xor_mask = wi::bit_xor (vr->min, vr->max);
1943 *may_be_nonzero = wi::bit_or (vr->min, vr->max);
1944 *must_be_nonzero = wi::bit_and (vr->min, vr->max);
1945 if (xor_mask != 0)
1947 wide_int mask = wi::mask (wi::floor_log2 (xor_mask), false,
1948 may_be_nonzero->get_precision ());
1949 *may_be_nonzero = *may_be_nonzero | mask;
1950 *must_be_nonzero = must_be_nonzero->and_not (mask);
1954 return true;
1957 /* Create two value-ranges in *VR0 and *VR1 from the anti-range *AR
1958 so that *VR0 U *VR1 == *AR. Returns true if that is possible,
1959 false otherwise. If *AR can be represented with a single range
1960 *VR1 will be VR_UNDEFINED. */
1962 static bool
1963 ranges_from_anti_range (value_range *ar,
1964 value_range *vr0, value_range *vr1)
1966 tree type = TREE_TYPE (ar->min);
1968 vr0->type = VR_UNDEFINED;
1969 vr1->type = VR_UNDEFINED;
1971 if (ar->type != VR_ANTI_RANGE
1972 || TREE_CODE (ar->min) != INTEGER_CST
1973 || TREE_CODE (ar->max) != INTEGER_CST
1974 || !vrp_val_min (type)
1975 || !vrp_val_max (type))
1976 return false;
1978 if (!vrp_val_is_min (ar->min))
1980 vr0->type = VR_RANGE;
1981 vr0->min = vrp_val_min (type);
1982 vr0->max = wide_int_to_tree (type, wi::sub (ar->min, 1));
1984 if (!vrp_val_is_max (ar->max))
1986 vr1->type = VR_RANGE;
1987 vr1->min = wide_int_to_tree (type, wi::add (ar->max, 1));
1988 vr1->max = vrp_val_max (type);
1990 if (vr0->type == VR_UNDEFINED)
1992 *vr0 = *vr1;
1993 vr1->type = VR_UNDEFINED;
1996 return vr0->type != VR_UNDEFINED;
1999 /* Helper to extract a value-range *VR for a multiplicative operation
2000 *VR0 CODE *VR1. */
2002 static void
2003 extract_range_from_multiplicative_op_1 (value_range *vr,
2004 enum tree_code code,
2005 value_range *vr0, value_range *vr1)
2007 enum value_range_type type;
2008 tree val[4];
2009 size_t i;
2010 tree min, max;
2011 bool sop;
2012 int cmp;
2014 /* Multiplications, divisions and shifts are a bit tricky to handle,
2015 depending on the mix of signs we have in the two ranges, we
2016 need to operate on different values to get the minimum and
2017 maximum values for the new range. One approach is to figure
2018 out all the variations of range combinations and do the
2019 operations.
2021 However, this involves several calls to compare_values and it
2022 is pretty convoluted. It's simpler to do the 4 operations
2023 (MIN0 OP MIN1, MIN0 OP MAX1, MAX0 OP MIN1 and MAX0 OP MAX0 OP
2024 MAX1) and then figure the smallest and largest values to form
2025 the new range. */
2026 gcc_assert (code == MULT_EXPR
2027 || code == TRUNC_DIV_EXPR
2028 || code == FLOOR_DIV_EXPR
2029 || code == CEIL_DIV_EXPR
2030 || code == EXACT_DIV_EXPR
2031 || code == ROUND_DIV_EXPR
2032 || code == RSHIFT_EXPR
2033 || code == LSHIFT_EXPR);
2034 gcc_assert ((vr0->type == VR_RANGE
2035 || (code == MULT_EXPR && vr0->type == VR_ANTI_RANGE))
2036 && vr0->type == vr1->type);
2038 type = vr0->type;
2040 /* Compute the 4 cross operations. */
2041 sop = false;
2042 val[0] = vrp_int_const_binop (code, vr0->min, vr1->min);
2043 if (val[0] == NULL_TREE)
2044 sop = true;
2046 if (vr1->max == vr1->min)
2047 val[1] = NULL_TREE;
2048 else
2050 val[1] = vrp_int_const_binop (code, vr0->min, vr1->max);
2051 if (val[1] == NULL_TREE)
2052 sop = true;
2055 if (vr0->max == vr0->min)
2056 val[2] = NULL_TREE;
2057 else
2059 val[2] = vrp_int_const_binop (code, vr0->max, vr1->min);
2060 if (val[2] == NULL_TREE)
2061 sop = true;
2064 if (vr0->min == vr0->max || vr1->min == vr1->max)
2065 val[3] = NULL_TREE;
2066 else
2068 val[3] = vrp_int_const_binop (code, vr0->max, vr1->max);
2069 if (val[3] == NULL_TREE)
2070 sop = true;
2073 if (sop)
2075 set_value_range_to_varying (vr);
2076 return;
2079 /* Set MIN to the minimum of VAL[i] and MAX to the maximum
2080 of VAL[i]. */
2081 min = val[0];
2082 max = val[0];
2083 for (i = 1; i < 4; i++)
2085 if (!is_gimple_min_invariant (min)
2086 || (TREE_OVERFLOW (min) && !is_overflow_infinity (min))
2087 || !is_gimple_min_invariant (max)
2088 || (TREE_OVERFLOW (max) && !is_overflow_infinity (max)))
2089 break;
2091 if (val[i])
2093 if (!is_gimple_min_invariant (val[i])
2094 || (TREE_OVERFLOW (val[i])
2095 && !is_overflow_infinity (val[i])))
2097 /* If we found an overflowed value, set MIN and MAX
2098 to it so that we set the resulting range to
2099 VARYING. */
2100 min = max = val[i];
2101 break;
2104 if (compare_values (val[i], min) == -1)
2105 min = val[i];
2107 if (compare_values (val[i], max) == 1)
2108 max = val[i];
2112 /* If either MIN or MAX overflowed, then set the resulting range to
2113 VARYING. But we do accept an overflow infinity
2114 representation. */
2115 if (min == NULL_TREE
2116 || !is_gimple_min_invariant (min)
2117 || (TREE_OVERFLOW (min) && !is_overflow_infinity (min))
2118 || max == NULL_TREE
2119 || !is_gimple_min_invariant (max)
2120 || (TREE_OVERFLOW (max) && !is_overflow_infinity (max)))
2122 set_value_range_to_varying (vr);
2123 return;
2126 /* We punt if:
2127 1) [-INF, +INF]
2128 2) [-INF, +-INF(OVF)]
2129 3) [+-INF(OVF), +INF]
2130 4) [+-INF(OVF), +-INF(OVF)]
2131 We learn nothing when we have INF and INF(OVF) on both sides.
2132 Note that we do accept [-INF, -INF] and [+INF, +INF] without
2133 overflow. */
2134 if ((vrp_val_is_min (min) || is_overflow_infinity (min))
2135 && (vrp_val_is_max (max) || is_overflow_infinity (max)))
2137 set_value_range_to_varying (vr);
2138 return;
2141 cmp = compare_values (min, max);
2142 if (cmp == -2 || cmp == 1)
2144 /* If the new range has its limits swapped around (MIN > MAX),
2145 then the operation caused one of them to wrap around, mark
2146 the new range VARYING. */
2147 set_value_range_to_varying (vr);
2149 else
2150 set_value_range (vr, type, min, max, NULL);
2153 /* Extract range information from a binary operation CODE based on
2154 the ranges of each of its operands *VR0 and *VR1 with resulting
2155 type EXPR_TYPE. The resulting range is stored in *VR. */
2157 static void
2158 extract_range_from_binary_expr_1 (value_range *vr,
2159 enum tree_code code, tree expr_type,
2160 value_range *vr0_, value_range *vr1_)
2162 value_range vr0 = *vr0_, vr1 = *vr1_;
2163 value_range vrtem0 = VR_INITIALIZER, vrtem1 = VR_INITIALIZER;
2164 enum value_range_type type;
2165 tree min = NULL_TREE, max = NULL_TREE;
2166 int cmp;
2168 if (!INTEGRAL_TYPE_P (expr_type)
2169 && !POINTER_TYPE_P (expr_type))
2171 set_value_range_to_varying (vr);
2172 return;
2175 /* Not all binary expressions can be applied to ranges in a
2176 meaningful way. Handle only arithmetic operations. */
2177 if (code != PLUS_EXPR
2178 && code != MINUS_EXPR
2179 && code != POINTER_PLUS_EXPR
2180 && code != MULT_EXPR
2181 && code != TRUNC_DIV_EXPR
2182 && code != FLOOR_DIV_EXPR
2183 && code != CEIL_DIV_EXPR
2184 && code != EXACT_DIV_EXPR
2185 && code != ROUND_DIV_EXPR
2186 && code != TRUNC_MOD_EXPR
2187 && code != RSHIFT_EXPR
2188 && code != LSHIFT_EXPR
2189 && code != MIN_EXPR
2190 && code != MAX_EXPR
2191 && code != BIT_AND_EXPR
2192 && code != BIT_IOR_EXPR
2193 && code != BIT_XOR_EXPR)
2195 set_value_range_to_varying (vr);
2196 return;
2199 /* If both ranges are UNDEFINED, so is the result. */
2200 if (vr0.type == VR_UNDEFINED && vr1.type == VR_UNDEFINED)
2202 set_value_range_to_undefined (vr);
2203 return;
2205 /* If one of the ranges is UNDEFINED drop it to VARYING for the following
2206 code. At some point we may want to special-case operations that
2207 have UNDEFINED result for all or some value-ranges of the not UNDEFINED
2208 operand. */
2209 else if (vr0.type == VR_UNDEFINED)
2210 set_value_range_to_varying (&vr0);
2211 else if (vr1.type == VR_UNDEFINED)
2212 set_value_range_to_varying (&vr1);
2214 /* Now canonicalize anti-ranges to ranges when they are not symbolic
2215 and express ~[] op X as ([]' op X) U ([]'' op X). */
2216 if (vr0.type == VR_ANTI_RANGE
2217 && ranges_from_anti_range (&vr0, &vrtem0, &vrtem1))
2219 extract_range_from_binary_expr_1 (vr, code, expr_type, &vrtem0, vr1_);
2220 if (vrtem1.type != VR_UNDEFINED)
2222 value_range vrres = VR_INITIALIZER;
2223 extract_range_from_binary_expr_1 (&vrres, code, expr_type,
2224 &vrtem1, vr1_);
2225 vrp_meet (vr, &vrres);
2227 return;
2229 /* Likewise for X op ~[]. */
2230 if (vr1.type == VR_ANTI_RANGE
2231 && ranges_from_anti_range (&vr1, &vrtem0, &vrtem1))
2233 extract_range_from_binary_expr_1 (vr, code, expr_type, vr0_, &vrtem0);
2234 if (vrtem1.type != VR_UNDEFINED)
2236 value_range vrres = VR_INITIALIZER;
2237 extract_range_from_binary_expr_1 (&vrres, code, expr_type,
2238 vr0_, &vrtem1);
2239 vrp_meet (vr, &vrres);
2241 return;
2244 /* The type of the resulting value range defaults to VR0.TYPE. */
2245 type = vr0.type;
2247 /* Refuse to operate on VARYING ranges, ranges of different kinds
2248 and symbolic ranges. As an exception, we allow BIT_{AND,IOR}
2249 because we may be able to derive a useful range even if one of
2250 the operands is VR_VARYING or symbolic range. Similarly for
2251 divisions, MIN/MAX and PLUS/MINUS.
2253 TODO, we may be able to derive anti-ranges in some cases. */
2254 if (code != BIT_AND_EXPR
2255 && code != BIT_IOR_EXPR
2256 && code != TRUNC_DIV_EXPR
2257 && code != FLOOR_DIV_EXPR
2258 && code != CEIL_DIV_EXPR
2259 && code != EXACT_DIV_EXPR
2260 && code != ROUND_DIV_EXPR
2261 && code != TRUNC_MOD_EXPR
2262 && code != MIN_EXPR
2263 && code != MAX_EXPR
2264 && code != PLUS_EXPR
2265 && code != MINUS_EXPR
2266 && code != RSHIFT_EXPR
2267 && (vr0.type == VR_VARYING
2268 || vr1.type == VR_VARYING
2269 || vr0.type != vr1.type
2270 || symbolic_range_p (&vr0)
2271 || symbolic_range_p (&vr1)))
2273 set_value_range_to_varying (vr);
2274 return;
2277 /* Now evaluate the expression to determine the new range. */
2278 if (POINTER_TYPE_P (expr_type))
2280 if (code == MIN_EXPR || code == MAX_EXPR)
2282 /* For MIN/MAX expressions with pointers, we only care about
2283 nullness, if both are non null, then the result is nonnull.
2284 If both are null, then the result is null. Otherwise they
2285 are varying. */
2286 if (range_is_nonnull (&vr0) && range_is_nonnull (&vr1))
2287 set_value_range_to_nonnull (vr, expr_type);
2288 else if (range_is_null (&vr0) && range_is_null (&vr1))
2289 set_value_range_to_null (vr, expr_type);
2290 else
2291 set_value_range_to_varying (vr);
2293 else if (code == POINTER_PLUS_EXPR)
2295 /* For pointer types, we are really only interested in asserting
2296 whether the expression evaluates to non-NULL. */
2297 if (range_is_nonnull (&vr0) || range_is_nonnull (&vr1))
2298 set_value_range_to_nonnull (vr, expr_type);
2299 else if (range_is_null (&vr0) && range_is_null (&vr1))
2300 set_value_range_to_null (vr, expr_type);
2301 else
2302 set_value_range_to_varying (vr);
2304 else if (code == BIT_AND_EXPR)
2306 /* For pointer types, we are really only interested in asserting
2307 whether the expression evaluates to non-NULL. */
2308 if (range_is_nonnull (&vr0) && range_is_nonnull (&vr1))
2309 set_value_range_to_nonnull (vr, expr_type);
2310 else if (range_is_null (&vr0) || range_is_null (&vr1))
2311 set_value_range_to_null (vr, expr_type);
2312 else
2313 set_value_range_to_varying (vr);
2315 else
2316 set_value_range_to_varying (vr);
2318 return;
2321 /* For integer ranges, apply the operation to each end of the
2322 range and see what we end up with. */
2323 if (code == PLUS_EXPR || code == MINUS_EXPR)
2325 const bool minus_p = (code == MINUS_EXPR);
2326 tree min_op0 = vr0.min;
2327 tree min_op1 = minus_p ? vr1.max : vr1.min;
2328 tree max_op0 = vr0.max;
2329 tree max_op1 = minus_p ? vr1.min : vr1.max;
2330 tree sym_min_op0 = NULL_TREE;
2331 tree sym_min_op1 = NULL_TREE;
2332 tree sym_max_op0 = NULL_TREE;
2333 tree sym_max_op1 = NULL_TREE;
2334 bool neg_min_op0, neg_min_op1, neg_max_op0, neg_max_op1;
2336 /* If we have a PLUS or MINUS with two VR_RANGEs, either constant or
2337 single-symbolic ranges, try to compute the precise resulting range,
2338 but only if we know that this resulting range will also be constant
2339 or single-symbolic. */
2340 if (vr0.type == VR_RANGE && vr1.type == VR_RANGE
2341 && (TREE_CODE (min_op0) == INTEGER_CST
2342 || (sym_min_op0
2343 = get_single_symbol (min_op0, &neg_min_op0, &min_op0)))
2344 && (TREE_CODE (min_op1) == INTEGER_CST
2345 || (sym_min_op1
2346 = get_single_symbol (min_op1, &neg_min_op1, &min_op1)))
2347 && (!(sym_min_op0 && sym_min_op1)
2348 || (sym_min_op0 == sym_min_op1
2349 && neg_min_op0 == (minus_p ? neg_min_op1 : !neg_min_op1)))
2350 && (TREE_CODE (max_op0) == INTEGER_CST
2351 || (sym_max_op0
2352 = get_single_symbol (max_op0, &neg_max_op0, &max_op0)))
2353 && (TREE_CODE (max_op1) == INTEGER_CST
2354 || (sym_max_op1
2355 = get_single_symbol (max_op1, &neg_max_op1, &max_op1)))
2356 && (!(sym_max_op0 && sym_max_op1)
2357 || (sym_max_op0 == sym_max_op1
2358 && neg_max_op0 == (minus_p ? neg_max_op1 : !neg_max_op1))))
2360 const signop sgn = TYPE_SIGN (expr_type);
2361 const unsigned int prec = TYPE_PRECISION (expr_type);
2362 wide_int type_min, type_max, wmin, wmax;
2363 int min_ovf = 0;
2364 int max_ovf = 0;
2366 /* Get the lower and upper bounds of the type. */
2367 if (TYPE_OVERFLOW_WRAPS (expr_type))
2369 type_min = wi::min_value (prec, sgn);
2370 type_max = wi::max_value (prec, sgn);
2372 else
2374 type_min = vrp_val_min (expr_type);
2375 type_max = vrp_val_max (expr_type);
2378 /* Combine the lower bounds, if any. */
2379 if (min_op0 && min_op1)
2381 if (minus_p)
2383 wmin = wi::sub (min_op0, min_op1);
2385 /* Check for overflow. */
2386 if (wi::cmp (0, min_op1, sgn)
2387 != wi::cmp (wmin, min_op0, sgn))
2388 min_ovf = wi::cmp (min_op0, min_op1, sgn);
2390 else
2392 wmin = wi::add (min_op0, min_op1);
2394 /* Check for overflow. */
2395 if (wi::cmp (min_op1, 0, sgn)
2396 != wi::cmp (wmin, min_op0, sgn))
2397 min_ovf = wi::cmp (min_op0, wmin, sgn);
2400 else if (min_op0)
2401 wmin = min_op0;
2402 else if (min_op1)
2403 wmin = minus_p ? wi::neg (min_op1) : min_op1;
2404 else
2405 wmin = wi::shwi (0, prec);
2407 /* Combine the upper bounds, if any. */
2408 if (max_op0 && max_op1)
2410 if (minus_p)
2412 wmax = wi::sub (max_op0, max_op1);
2414 /* Check for overflow. */
2415 if (wi::cmp (0, max_op1, sgn)
2416 != wi::cmp (wmax, max_op0, sgn))
2417 max_ovf = wi::cmp (max_op0, max_op1, sgn);
2419 else
2421 wmax = wi::add (max_op0, max_op1);
2423 if (wi::cmp (max_op1, 0, sgn)
2424 != wi::cmp (wmax, max_op0, sgn))
2425 max_ovf = wi::cmp (max_op0, wmax, sgn);
2428 else if (max_op0)
2429 wmax = max_op0;
2430 else if (max_op1)
2431 wmax = minus_p ? wi::neg (max_op1) : max_op1;
2432 else
2433 wmax = wi::shwi (0, prec);
2435 /* Check for type overflow. */
2436 if (min_ovf == 0)
2438 if (wi::cmp (wmin, type_min, sgn) == -1)
2439 min_ovf = -1;
2440 else if (wi::cmp (wmin, type_max, sgn) == 1)
2441 min_ovf = 1;
2443 if (max_ovf == 0)
2445 if (wi::cmp (wmax, type_min, sgn) == -1)
2446 max_ovf = -1;
2447 else if (wi::cmp (wmax, type_max, sgn) == 1)
2448 max_ovf = 1;
2451 /* If we have overflow for the constant part and the resulting
2452 range will be symbolic, drop to VR_VARYING. */
2453 if ((min_ovf && sym_min_op0 != sym_min_op1)
2454 || (max_ovf && sym_max_op0 != sym_max_op1))
2456 set_value_range_to_varying (vr);
2457 return;
2460 if (TYPE_OVERFLOW_WRAPS (expr_type))
2462 /* If overflow wraps, truncate the values and adjust the
2463 range kind and bounds appropriately. */
2464 wide_int tmin = wide_int::from (wmin, prec, sgn);
2465 wide_int tmax = wide_int::from (wmax, prec, sgn);
2466 if (min_ovf == max_ovf)
2468 /* No overflow or both overflow or underflow. The
2469 range kind stays VR_RANGE. */
2470 min = wide_int_to_tree (expr_type, tmin);
2471 max = wide_int_to_tree (expr_type, tmax);
2473 else if ((min_ovf == -1 && max_ovf == 0)
2474 || (max_ovf == 1 && min_ovf == 0))
2476 /* Min underflow or max overflow. The range kind
2477 changes to VR_ANTI_RANGE. */
2478 bool covers = false;
2479 wide_int tem = tmin;
2480 type = VR_ANTI_RANGE;
2481 tmin = tmax + 1;
2482 if (wi::cmp (tmin, tmax, sgn) < 0)
2483 covers = true;
2484 tmax = tem - 1;
2485 if (wi::cmp (tmax, tem, sgn) > 0)
2486 covers = true;
2487 /* If the anti-range would cover nothing, drop to varying.
2488 Likewise if the anti-range bounds are outside of the
2489 types values. */
2490 if (covers || wi::cmp (tmin, tmax, sgn) > 0)
2492 set_value_range_to_varying (vr);
2493 return;
2495 min = wide_int_to_tree (expr_type, tmin);
2496 max = wide_int_to_tree (expr_type, tmax);
2498 else
2500 /* Other underflow and/or overflow, drop to VR_VARYING. */
2501 set_value_range_to_varying (vr);
2502 return;
2505 else
2507 /* If overflow does not wrap, saturate to the types min/max
2508 value. */
2509 if (min_ovf == -1)
2511 if (needs_overflow_infinity (expr_type)
2512 && supports_overflow_infinity (expr_type))
2513 min = negative_overflow_infinity (expr_type);
2514 else
2515 min = wide_int_to_tree (expr_type, type_min);
2517 else if (min_ovf == 1)
2519 if (needs_overflow_infinity (expr_type)
2520 && supports_overflow_infinity (expr_type))
2521 min = positive_overflow_infinity (expr_type);
2522 else
2523 min = wide_int_to_tree (expr_type, type_max);
2525 else
2526 min = wide_int_to_tree (expr_type, wmin);
2528 if (max_ovf == -1)
2530 if (needs_overflow_infinity (expr_type)
2531 && supports_overflow_infinity (expr_type))
2532 max = negative_overflow_infinity (expr_type);
2533 else
2534 max = wide_int_to_tree (expr_type, type_min);
2536 else if (max_ovf == 1)
2538 if (needs_overflow_infinity (expr_type)
2539 && supports_overflow_infinity (expr_type))
2540 max = positive_overflow_infinity (expr_type);
2541 else
2542 max = wide_int_to_tree (expr_type, type_max);
2544 else
2545 max = wide_int_to_tree (expr_type, wmax);
2548 if (needs_overflow_infinity (expr_type)
2549 && supports_overflow_infinity (expr_type))
2551 if ((min_op0 && is_negative_overflow_infinity (min_op0))
2552 || (min_op1
2553 && (minus_p
2554 ? is_positive_overflow_infinity (min_op1)
2555 : is_negative_overflow_infinity (min_op1))))
2556 min = negative_overflow_infinity (expr_type);
2557 if ((max_op0 && is_positive_overflow_infinity (max_op0))
2558 || (max_op1
2559 && (minus_p
2560 ? is_negative_overflow_infinity (max_op1)
2561 : is_positive_overflow_infinity (max_op1))))
2562 max = positive_overflow_infinity (expr_type);
2565 /* If the result lower bound is constant, we're done;
2566 otherwise, build the symbolic lower bound. */
2567 if (sym_min_op0 == sym_min_op1)
2569 else if (sym_min_op0)
2570 min = build_symbolic_expr (expr_type, sym_min_op0,
2571 neg_min_op0, min);
2572 else if (sym_min_op1)
2573 min = build_symbolic_expr (expr_type, sym_min_op1,
2574 neg_min_op1 ^ minus_p, min);
2576 /* Likewise for the upper bound. */
2577 if (sym_max_op0 == sym_max_op1)
2579 else if (sym_max_op0)
2580 max = build_symbolic_expr (expr_type, sym_max_op0,
2581 neg_max_op0, max);
2582 else if (sym_max_op1)
2583 max = build_symbolic_expr (expr_type, sym_max_op1,
2584 neg_max_op1 ^ minus_p, max);
2586 else
2588 /* For other cases, for example if we have a PLUS_EXPR with two
2589 VR_ANTI_RANGEs, drop to VR_VARYING. It would take more effort
2590 to compute a precise range for such a case.
2591 ??? General even mixed range kind operations can be expressed
2592 by for example transforming ~[3, 5] + [1, 2] to range-only
2593 operations and a union primitive:
2594 [-INF, 2] + [1, 2] U [5, +INF] + [1, 2]
2595 [-INF+1, 4] U [6, +INF(OVF)]
2596 though usually the union is not exactly representable with
2597 a single range or anti-range as the above is
2598 [-INF+1, +INF(OVF)] intersected with ~[5, 5]
2599 but one could use a scheme similar to equivalences for this. */
2600 set_value_range_to_varying (vr);
2601 return;
2604 else if (code == MIN_EXPR
2605 || code == MAX_EXPR)
2607 if (vr0.type == VR_RANGE
2608 && !symbolic_range_p (&vr0))
2610 type = VR_RANGE;
2611 if (vr1.type == VR_RANGE
2612 && !symbolic_range_p (&vr1))
2614 /* For operations that make the resulting range directly
2615 proportional to the original ranges, apply the operation to
2616 the same end of each range. */
2617 min = vrp_int_const_binop (code, vr0.min, vr1.min);
2618 max = vrp_int_const_binop (code, vr0.max, vr1.max);
2620 else if (code == MIN_EXPR)
2622 min = vrp_val_min (expr_type);
2623 max = vr0.max;
2625 else if (code == MAX_EXPR)
2627 min = vr0.min;
2628 max = vrp_val_max (expr_type);
2631 else if (vr1.type == VR_RANGE
2632 && !symbolic_range_p (&vr1))
2634 type = VR_RANGE;
2635 if (code == MIN_EXPR)
2637 min = vrp_val_min (expr_type);
2638 max = vr1.max;
2640 else if (code == MAX_EXPR)
2642 min = vr1.min;
2643 max = vrp_val_max (expr_type);
2646 else
2648 set_value_range_to_varying (vr);
2649 return;
2652 else if (code == MULT_EXPR)
2654 /* Fancy code so that with unsigned, [-3,-1]*[-3,-1] does not
2655 drop to varying. This test requires 2*prec bits if both
2656 operands are signed and 2*prec + 2 bits if either is not. */
2658 signop sign = TYPE_SIGN (expr_type);
2659 unsigned int prec = TYPE_PRECISION (expr_type);
2661 if (range_int_cst_p (&vr0)
2662 && range_int_cst_p (&vr1)
2663 && TYPE_OVERFLOW_WRAPS (expr_type))
2665 typedef FIXED_WIDE_INT (WIDE_INT_MAX_PRECISION * 2) vrp_int;
2666 typedef generic_wide_int
2667 <wi::extended_tree <WIDE_INT_MAX_PRECISION * 2> > vrp_int_cst;
2668 vrp_int sizem1 = wi::mask <vrp_int> (prec, false);
2669 vrp_int size = sizem1 + 1;
2671 /* Extend the values using the sign of the result to PREC2.
2672 From here on out, everthing is just signed math no matter
2673 what the input types were. */
2674 vrp_int min0 = vrp_int_cst (vr0.min);
2675 vrp_int max0 = vrp_int_cst (vr0.max);
2676 vrp_int min1 = vrp_int_cst (vr1.min);
2677 vrp_int max1 = vrp_int_cst (vr1.max);
2678 /* Canonicalize the intervals. */
2679 if (sign == UNSIGNED)
2681 if (wi::ltu_p (size, min0 + max0))
2683 min0 -= size;
2684 max0 -= size;
2687 if (wi::ltu_p (size, min1 + max1))
2689 min1 -= size;
2690 max1 -= size;
2694 vrp_int prod0 = min0 * min1;
2695 vrp_int prod1 = min0 * max1;
2696 vrp_int prod2 = max0 * min1;
2697 vrp_int prod3 = max0 * max1;
2699 /* Sort the 4 products so that min is in prod0 and max is in
2700 prod3. */
2701 /* min0min1 > max0max1 */
2702 if (prod0 > prod3)
2703 std::swap (prod0, prod3);
2705 /* min0max1 > max0min1 */
2706 if (prod1 > prod2)
2707 std::swap (prod1, prod2);
2709 if (prod0 > prod1)
2710 std::swap (prod0, prod1);
2712 if (prod2 > prod3)
2713 std::swap (prod2, prod3);
2715 /* diff = max - min. */
2716 prod2 = prod3 - prod0;
2717 if (wi::geu_p (prod2, sizem1))
2719 /* the range covers all values. */
2720 set_value_range_to_varying (vr);
2721 return;
2724 /* The following should handle the wrapping and selecting
2725 VR_ANTI_RANGE for us. */
2726 min = wide_int_to_tree (expr_type, prod0);
2727 max = wide_int_to_tree (expr_type, prod3);
2728 set_and_canonicalize_value_range (vr, VR_RANGE, min, max, NULL);
2729 return;
2732 /* If we have an unsigned MULT_EXPR with two VR_ANTI_RANGEs,
2733 drop to VR_VARYING. It would take more effort to compute a
2734 precise range for such a case. For example, if we have
2735 op0 == 65536 and op1 == 65536 with their ranges both being
2736 ~[0,0] on a 32-bit machine, we would have op0 * op1 == 0, so
2737 we cannot claim that the product is in ~[0,0]. Note that we
2738 are guaranteed to have vr0.type == vr1.type at this
2739 point. */
2740 if (vr0.type == VR_ANTI_RANGE
2741 && !TYPE_OVERFLOW_UNDEFINED (expr_type))
2743 set_value_range_to_varying (vr);
2744 return;
2747 extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1);
2748 return;
2750 else if (code == RSHIFT_EXPR
2751 || code == LSHIFT_EXPR)
2753 /* If we have a RSHIFT_EXPR with any shift values outside [0..prec-1],
2754 then drop to VR_VARYING. Outside of this range we get undefined
2755 behavior from the shift operation. We cannot even trust
2756 SHIFT_COUNT_TRUNCATED at this stage, because that applies to rtl
2757 shifts, and the operation at the tree level may be widened. */
2758 if (range_int_cst_p (&vr1)
2759 && compare_tree_int (vr1.min, 0) >= 0
2760 && compare_tree_int (vr1.max, TYPE_PRECISION (expr_type)) == -1)
2762 if (code == RSHIFT_EXPR)
2764 /* Even if vr0 is VARYING or otherwise not usable, we can derive
2765 useful ranges just from the shift count. E.g.
2766 x >> 63 for signed 64-bit x is always [-1, 0]. */
2767 if (vr0.type != VR_RANGE || symbolic_range_p (&vr0))
2769 vr0.type = type = VR_RANGE;
2770 vr0.min = vrp_val_min (expr_type);
2771 vr0.max = vrp_val_max (expr_type);
2773 extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1);
2774 return;
2776 /* We can map lshifts by constants to MULT_EXPR handling. */
2777 else if (code == LSHIFT_EXPR
2778 && range_int_cst_singleton_p (&vr1))
2780 bool saved_flag_wrapv;
2781 value_range vr1p = VR_INITIALIZER;
2782 vr1p.type = VR_RANGE;
2783 vr1p.min = (wide_int_to_tree
2784 (expr_type,
2785 wi::set_bit_in_zero (tree_to_shwi (vr1.min),
2786 TYPE_PRECISION (expr_type))));
2787 vr1p.max = vr1p.min;
2788 /* We have to use a wrapping multiply though as signed overflow
2789 on lshifts is implementation defined in C89. */
2790 saved_flag_wrapv = flag_wrapv;
2791 flag_wrapv = 1;
2792 extract_range_from_binary_expr_1 (vr, MULT_EXPR, expr_type,
2793 &vr0, &vr1p);
2794 flag_wrapv = saved_flag_wrapv;
2795 return;
2797 else if (code == LSHIFT_EXPR
2798 && range_int_cst_p (&vr0))
2800 int prec = TYPE_PRECISION (expr_type);
2801 int overflow_pos = prec;
2802 int bound_shift;
2803 wide_int low_bound, high_bound;
2804 bool uns = TYPE_UNSIGNED (expr_type);
2805 bool in_bounds = false;
2807 if (!uns)
2808 overflow_pos -= 1;
2810 bound_shift = overflow_pos - tree_to_shwi (vr1.max);
2811 /* If bound_shift == HOST_BITS_PER_WIDE_INT, the llshift can
2812 overflow. However, for that to happen, vr1.max needs to be
2813 zero, which means vr1 is a singleton range of zero, which
2814 means it should be handled by the previous LSHIFT_EXPR
2815 if-clause. */
2816 wide_int bound = wi::set_bit_in_zero (bound_shift, prec);
2817 wide_int complement = ~(bound - 1);
2819 if (uns)
2821 low_bound = bound;
2822 high_bound = complement;
2823 if (wi::ltu_p (vr0.max, low_bound))
2825 /* [5, 6] << [1, 2] == [10, 24]. */
2826 /* We're shifting out only zeroes, the value increases
2827 monotonically. */
2828 in_bounds = true;
2830 else if (wi::ltu_p (high_bound, vr0.min))
2832 /* [0xffffff00, 0xffffffff] << [1, 2]
2833 == [0xfffffc00, 0xfffffffe]. */
2834 /* We're shifting out only ones, the value decreases
2835 monotonically. */
2836 in_bounds = true;
2839 else
2841 /* [-1, 1] << [1, 2] == [-4, 4]. */
2842 low_bound = complement;
2843 high_bound = bound;
2844 if (wi::lts_p (vr0.max, high_bound)
2845 && wi::lts_p (low_bound, vr0.min))
2847 /* For non-negative numbers, we're shifting out only
2848 zeroes, the value increases monotonically.
2849 For negative numbers, we're shifting out only ones, the
2850 value decreases monotomically. */
2851 in_bounds = true;
2855 if (in_bounds)
2857 extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1);
2858 return;
2862 set_value_range_to_varying (vr);
2863 return;
2865 else if (code == TRUNC_DIV_EXPR
2866 || code == FLOOR_DIV_EXPR
2867 || code == CEIL_DIV_EXPR
2868 || code == EXACT_DIV_EXPR
2869 || code == ROUND_DIV_EXPR)
2871 if (vr0.type != VR_RANGE || symbolic_range_p (&vr0))
2873 /* For division, if op1 has VR_RANGE but op0 does not, something
2874 can be deduced just from that range. Say [min, max] / [4, max]
2875 gives [min / 4, max / 4] range. */
2876 if (vr1.type == VR_RANGE
2877 && !symbolic_range_p (&vr1)
2878 && range_includes_zero_p (vr1.min, vr1.max) == 0)
2880 vr0.type = type = VR_RANGE;
2881 vr0.min = vrp_val_min (expr_type);
2882 vr0.max = vrp_val_max (expr_type);
2884 else
2886 set_value_range_to_varying (vr);
2887 return;
2891 /* For divisions, if flag_non_call_exceptions is true, we must
2892 not eliminate a division by zero. */
2893 if (cfun->can_throw_non_call_exceptions
2894 && (vr1.type != VR_RANGE
2895 || range_includes_zero_p (vr1.min, vr1.max) != 0))
2897 set_value_range_to_varying (vr);
2898 return;
2901 /* For divisions, if op0 is VR_RANGE, we can deduce a range
2902 even if op1 is VR_VARYING, VR_ANTI_RANGE, symbolic or can
2903 include 0. */
2904 if (vr0.type == VR_RANGE
2905 && (vr1.type != VR_RANGE
2906 || range_includes_zero_p (vr1.min, vr1.max) != 0))
2908 tree zero = build_int_cst (TREE_TYPE (vr0.min), 0);
2909 int cmp;
2911 min = NULL_TREE;
2912 max = NULL_TREE;
2913 if (TYPE_UNSIGNED (expr_type)
2914 || value_range_nonnegative_p (&vr1))
2916 /* For unsigned division or when divisor is known
2917 to be non-negative, the range has to cover
2918 all numbers from 0 to max for positive max
2919 and all numbers from min to 0 for negative min. */
2920 cmp = compare_values (vr0.max, zero);
2921 if (cmp == -1)
2923 /* When vr0.max < 0, vr1.min != 0 and value
2924 ranges for dividend and divisor are available. */
2925 if (vr1.type == VR_RANGE
2926 && !symbolic_range_p (&vr0)
2927 && !symbolic_range_p (&vr1)
2928 && compare_values (vr1.min, zero) != 0)
2929 max = int_const_binop (code, vr0.max, vr1.min);
2930 else
2931 max = zero;
2933 else if (cmp == 0 || cmp == 1)
2934 max = vr0.max;
2935 else
2936 type = VR_VARYING;
2937 cmp = compare_values (vr0.min, zero);
2938 if (cmp == 1)
2940 /* For unsigned division when value ranges for dividend
2941 and divisor are available. */
2942 if (vr1.type == VR_RANGE
2943 && !symbolic_range_p (&vr0)
2944 && !symbolic_range_p (&vr1)
2945 && compare_values (vr1.max, zero) != 0)
2946 min = int_const_binop (code, vr0.min, vr1.max);
2947 else
2948 min = zero;
2950 else if (cmp == 0 || cmp == -1)
2951 min = vr0.min;
2952 else
2953 type = VR_VARYING;
2955 else
2957 /* Otherwise the range is -max .. max or min .. -min
2958 depending on which bound is bigger in absolute value,
2959 as the division can change the sign. */
2960 abs_extent_range (vr, vr0.min, vr0.max);
2961 return;
2963 if (type == VR_VARYING)
2965 set_value_range_to_varying (vr);
2966 return;
2969 else if (!symbolic_range_p (&vr0) && !symbolic_range_p (&vr1))
2971 extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1);
2972 return;
2975 else if (code == TRUNC_MOD_EXPR)
2977 if (range_is_null (&vr1))
2979 set_value_range_to_undefined (vr);
2980 return;
2982 /* ABS (A % B) < ABS (B) and either
2983 0 <= A % B <= A or A <= A % B <= 0. */
2984 type = VR_RANGE;
2985 signop sgn = TYPE_SIGN (expr_type);
2986 unsigned int prec = TYPE_PRECISION (expr_type);
2987 wide_int wmin, wmax, tmp;
2988 wide_int zero = wi::zero (prec);
2989 wide_int one = wi::one (prec);
2990 if (vr1.type == VR_RANGE && !symbolic_range_p (&vr1))
2992 wmax = wi::sub (vr1.max, one);
2993 if (sgn == SIGNED)
2995 tmp = wi::sub (wi::minus_one (prec), vr1.min);
2996 wmax = wi::smax (wmax, tmp);
2999 else
3001 wmax = wi::max_value (prec, sgn);
3002 /* X % INT_MIN may be INT_MAX. */
3003 if (sgn == UNSIGNED)
3004 wmax = wmax - one;
3007 if (sgn == UNSIGNED)
3008 wmin = zero;
3009 else
3011 wmin = -wmax;
3012 if (vr0.type == VR_RANGE && TREE_CODE (vr0.min) == INTEGER_CST)
3014 tmp = vr0.min;
3015 if (wi::gts_p (tmp, zero))
3016 tmp = zero;
3017 wmin = wi::smax (wmin, tmp);
3021 if (vr0.type == VR_RANGE && TREE_CODE (vr0.max) == INTEGER_CST)
3023 tmp = vr0.max;
3024 if (sgn == SIGNED && wi::neg_p (tmp))
3025 tmp = zero;
3026 wmax = wi::min (wmax, tmp, sgn);
3029 min = wide_int_to_tree (expr_type, wmin);
3030 max = wide_int_to_tree (expr_type, wmax);
3032 else if (code == BIT_AND_EXPR || code == BIT_IOR_EXPR || code == BIT_XOR_EXPR)
3034 bool int_cst_range0, int_cst_range1;
3035 wide_int may_be_nonzero0, may_be_nonzero1;
3036 wide_int must_be_nonzero0, must_be_nonzero1;
3038 int_cst_range0 = zero_nonzero_bits_from_vr (expr_type, &vr0,
3039 &may_be_nonzero0,
3040 &must_be_nonzero0);
3041 int_cst_range1 = zero_nonzero_bits_from_vr (expr_type, &vr1,
3042 &may_be_nonzero1,
3043 &must_be_nonzero1);
3045 type = VR_RANGE;
3046 if (code == BIT_AND_EXPR)
3048 min = wide_int_to_tree (expr_type,
3049 must_be_nonzero0 & must_be_nonzero1);
3050 wide_int wmax = may_be_nonzero0 & may_be_nonzero1;
3051 /* If both input ranges contain only negative values we can
3052 truncate the result range maximum to the minimum of the
3053 input range maxima. */
3054 if (int_cst_range0 && int_cst_range1
3055 && tree_int_cst_sgn (vr0.max) < 0
3056 && tree_int_cst_sgn (vr1.max) < 0)
3058 wmax = wi::min (wmax, vr0.max, TYPE_SIGN (expr_type));
3059 wmax = wi::min (wmax, vr1.max, TYPE_SIGN (expr_type));
3061 /* If either input range contains only non-negative values
3062 we can truncate the result range maximum to the respective
3063 maximum of the input range. */
3064 if (int_cst_range0 && tree_int_cst_sgn (vr0.min) >= 0)
3065 wmax = wi::min (wmax, vr0.max, TYPE_SIGN (expr_type));
3066 if (int_cst_range1 && tree_int_cst_sgn (vr1.min) >= 0)
3067 wmax = wi::min (wmax, vr1.max, TYPE_SIGN (expr_type));
3068 max = wide_int_to_tree (expr_type, wmax);
3070 else if (code == BIT_IOR_EXPR)
3072 max = wide_int_to_tree (expr_type,
3073 may_be_nonzero0 | may_be_nonzero1);
3074 wide_int wmin = must_be_nonzero0 | must_be_nonzero1;
3075 /* If the input ranges contain only positive values we can
3076 truncate the minimum of the result range to the maximum
3077 of the input range minima. */
3078 if (int_cst_range0 && int_cst_range1
3079 && tree_int_cst_sgn (vr0.min) >= 0
3080 && tree_int_cst_sgn (vr1.min) >= 0)
3082 wmin = wi::max (wmin, vr0.min, TYPE_SIGN (expr_type));
3083 wmin = wi::max (wmin, vr1.min, TYPE_SIGN (expr_type));
3085 /* If either input range contains only negative values
3086 we can truncate the minimum of the result range to the
3087 respective minimum range. */
3088 if (int_cst_range0 && tree_int_cst_sgn (vr0.max) < 0)
3089 wmin = wi::max (wmin, vr0.min, TYPE_SIGN (expr_type));
3090 if (int_cst_range1 && tree_int_cst_sgn (vr1.max) < 0)
3091 wmin = wi::max (wmin, vr1.min, TYPE_SIGN (expr_type));
3092 min = wide_int_to_tree (expr_type, wmin);
3094 else if (code == BIT_XOR_EXPR)
3096 wide_int result_zero_bits = ((must_be_nonzero0 & must_be_nonzero1)
3097 | ~(may_be_nonzero0 | may_be_nonzero1));
3098 wide_int result_one_bits
3099 = (must_be_nonzero0.and_not (may_be_nonzero1)
3100 | must_be_nonzero1.and_not (may_be_nonzero0));
3101 max = wide_int_to_tree (expr_type, ~result_zero_bits);
3102 min = wide_int_to_tree (expr_type, result_one_bits);
3103 /* If the range has all positive or all negative values the
3104 result is better than VARYING. */
3105 if (tree_int_cst_sgn (min) < 0
3106 || tree_int_cst_sgn (max) >= 0)
3108 else
3109 max = min = NULL_TREE;
3112 else
3113 gcc_unreachable ();
3115 /* If either MIN or MAX overflowed, then set the resulting range to
3116 VARYING. But we do accept an overflow infinity representation. */
3117 if (min == NULL_TREE
3118 || (TREE_OVERFLOW_P (min) && !is_overflow_infinity (min))
3119 || max == NULL_TREE
3120 || (TREE_OVERFLOW_P (max) && !is_overflow_infinity (max)))
3122 set_value_range_to_varying (vr);
3123 return;
3126 /* We punt if:
3127 1) [-INF, +INF]
3128 2) [-INF, +-INF(OVF)]
3129 3) [+-INF(OVF), +INF]
3130 4) [+-INF(OVF), +-INF(OVF)]
3131 We learn nothing when we have INF and INF(OVF) on both sides.
3132 Note that we do accept [-INF, -INF] and [+INF, +INF] without
3133 overflow. */
3134 if ((vrp_val_is_min (min) || is_overflow_infinity (min))
3135 && (vrp_val_is_max (max) || is_overflow_infinity (max)))
3137 set_value_range_to_varying (vr);
3138 return;
3141 cmp = compare_values (min, max);
3142 if (cmp == -2 || cmp == 1)
3144 /* If the new range has its limits swapped around (MIN > MAX),
3145 then the operation caused one of them to wrap around, mark
3146 the new range VARYING. */
3147 set_value_range_to_varying (vr);
3149 else
3150 set_value_range (vr, type, min, max, NULL);
3153 /* Extract range information from a binary expression OP0 CODE OP1 based on
3154 the ranges of each of its operands with resulting type EXPR_TYPE.
3155 The resulting range is stored in *VR. */
3157 static void
3158 extract_range_from_binary_expr (value_range *vr,
3159 enum tree_code code,
3160 tree expr_type, tree op0, tree op1)
3162 value_range vr0 = VR_INITIALIZER;
3163 value_range vr1 = VR_INITIALIZER;
3165 /* Get value ranges for each operand. For constant operands, create
3166 a new value range with the operand to simplify processing. */
3167 if (TREE_CODE (op0) == SSA_NAME)
3168 vr0 = *(get_value_range (op0));
3169 else if (is_gimple_min_invariant (op0))
3170 set_value_range_to_value (&vr0, op0, NULL);
3171 else
3172 set_value_range_to_varying (&vr0);
3174 if (TREE_CODE (op1) == SSA_NAME)
3175 vr1 = *(get_value_range (op1));
3176 else if (is_gimple_min_invariant (op1))
3177 set_value_range_to_value (&vr1, op1, NULL);
3178 else
3179 set_value_range_to_varying (&vr1);
3181 extract_range_from_binary_expr_1 (vr, code, expr_type, &vr0, &vr1);
3183 /* Try harder for PLUS and MINUS if the range of one operand is symbolic
3184 and based on the other operand, for example if it was deduced from a
3185 symbolic comparison. When a bound of the range of the first operand
3186 is invariant, we set the corresponding bound of the new range to INF
3187 in order to avoid recursing on the range of the second operand. */
3188 if (vr->type == VR_VARYING
3189 && (code == PLUS_EXPR || code == MINUS_EXPR)
3190 && TREE_CODE (op1) == SSA_NAME
3191 && vr0.type == VR_RANGE
3192 && symbolic_range_based_on_p (&vr0, op1))
3194 const bool minus_p = (code == MINUS_EXPR);
3195 value_range n_vr1 = VR_INITIALIZER;
3197 /* Try with VR0 and [-INF, OP1]. */
3198 if (is_gimple_min_invariant (minus_p ? vr0.max : vr0.min))
3199 set_value_range (&n_vr1, VR_RANGE, vrp_val_min (expr_type), op1, NULL);
3201 /* Try with VR0 and [OP1, +INF]. */
3202 else if (is_gimple_min_invariant (minus_p ? vr0.min : vr0.max))
3203 set_value_range (&n_vr1, VR_RANGE, op1, vrp_val_max (expr_type), NULL);
3205 /* Try with VR0 and [OP1, OP1]. */
3206 else
3207 set_value_range (&n_vr1, VR_RANGE, op1, op1, NULL);
3209 extract_range_from_binary_expr_1 (vr, code, expr_type, &vr0, &n_vr1);
3212 if (vr->type == VR_VARYING
3213 && (code == PLUS_EXPR || code == MINUS_EXPR)
3214 && TREE_CODE (op0) == SSA_NAME
3215 && vr1.type == VR_RANGE
3216 && symbolic_range_based_on_p (&vr1, op0))
3218 const bool minus_p = (code == MINUS_EXPR);
3219 value_range n_vr0 = VR_INITIALIZER;
3221 /* Try with [-INF, OP0] and VR1. */
3222 if (is_gimple_min_invariant (minus_p ? vr1.max : vr1.min))
3223 set_value_range (&n_vr0, VR_RANGE, vrp_val_min (expr_type), op0, NULL);
3225 /* Try with [OP0, +INF] and VR1. */
3226 else if (is_gimple_min_invariant (minus_p ? vr1.min : vr1.max))
3227 set_value_range (&n_vr0, VR_RANGE, op0, vrp_val_max (expr_type), NULL);
3229 /* Try with [OP0, OP0] and VR1. */
3230 else
3231 set_value_range (&n_vr0, VR_RANGE, op0, op0, NULL);
3233 extract_range_from_binary_expr_1 (vr, code, expr_type, &n_vr0, &vr1);
3237 /* Extract range information from a unary operation CODE based on
3238 the range of its operand *VR0 with type OP0_TYPE with resulting type TYPE.
3239 The resulting range is stored in *VR. */
3241 static void
3242 extract_range_from_unary_expr_1 (value_range *vr,
3243 enum tree_code code, tree type,
3244 value_range *vr0_, tree op0_type)
3246 value_range vr0 = *vr0_, vrtem0 = VR_INITIALIZER, vrtem1 = VR_INITIALIZER;
3248 /* VRP only operates on integral and pointer types. */
3249 if (!(INTEGRAL_TYPE_P (op0_type)
3250 || POINTER_TYPE_P (op0_type))
3251 || !(INTEGRAL_TYPE_P (type)
3252 || POINTER_TYPE_P (type)))
3254 set_value_range_to_varying (vr);
3255 return;
3258 /* If VR0 is UNDEFINED, so is the result. */
3259 if (vr0.type == VR_UNDEFINED)
3261 set_value_range_to_undefined (vr);
3262 return;
3265 /* Handle operations that we express in terms of others. */
3266 if (code == PAREN_EXPR || code == OBJ_TYPE_REF)
3268 /* PAREN_EXPR and OBJ_TYPE_REF are simple copies. */
3269 copy_value_range (vr, &vr0);
3270 return;
3272 else if (code == NEGATE_EXPR)
3274 /* -X is simply 0 - X, so re-use existing code that also handles
3275 anti-ranges fine. */
3276 value_range zero = VR_INITIALIZER;
3277 set_value_range_to_value (&zero, build_int_cst (type, 0), NULL);
3278 extract_range_from_binary_expr_1 (vr, MINUS_EXPR, type, &zero, &vr0);
3279 return;
3281 else if (code == BIT_NOT_EXPR)
3283 /* ~X is simply -1 - X, so re-use existing code that also handles
3284 anti-ranges fine. */
3285 value_range minusone = VR_INITIALIZER;
3286 set_value_range_to_value (&minusone, build_int_cst (type, -1), NULL);
3287 extract_range_from_binary_expr_1 (vr, MINUS_EXPR,
3288 type, &minusone, &vr0);
3289 return;
3292 /* Now canonicalize anti-ranges to ranges when they are not symbolic
3293 and express op ~[] as (op []') U (op []''). */
3294 if (vr0.type == VR_ANTI_RANGE
3295 && ranges_from_anti_range (&vr0, &vrtem0, &vrtem1))
3297 extract_range_from_unary_expr_1 (vr, code, type, &vrtem0, op0_type);
3298 if (vrtem1.type != VR_UNDEFINED)
3300 value_range vrres = VR_INITIALIZER;
3301 extract_range_from_unary_expr_1 (&vrres, code, type,
3302 &vrtem1, op0_type);
3303 vrp_meet (vr, &vrres);
3305 return;
3308 if (CONVERT_EXPR_CODE_P (code))
3310 tree inner_type = op0_type;
3311 tree outer_type = type;
3313 /* If the expression evaluates to a pointer, we are only interested in
3314 determining if it evaluates to NULL [0, 0] or non-NULL (~[0, 0]). */
3315 if (POINTER_TYPE_P (type))
3317 if (range_is_nonnull (&vr0))
3318 set_value_range_to_nonnull (vr, type);
3319 else if (range_is_null (&vr0))
3320 set_value_range_to_null (vr, type);
3321 else
3322 set_value_range_to_varying (vr);
3323 return;
3326 /* If VR0 is varying and we increase the type precision, assume
3327 a full range for the following transformation. */
3328 if (vr0.type == VR_VARYING
3329 && INTEGRAL_TYPE_P (inner_type)
3330 && TYPE_PRECISION (inner_type) < TYPE_PRECISION (outer_type))
3332 vr0.type = VR_RANGE;
3333 vr0.min = TYPE_MIN_VALUE (inner_type);
3334 vr0.max = TYPE_MAX_VALUE (inner_type);
3337 /* If VR0 is a constant range or anti-range and the conversion is
3338 not truncating we can convert the min and max values and
3339 canonicalize the resulting range. Otherwise we can do the
3340 conversion if the size of the range is less than what the
3341 precision of the target type can represent and the range is
3342 not an anti-range. */
3343 if ((vr0.type == VR_RANGE
3344 || vr0.type == VR_ANTI_RANGE)
3345 && TREE_CODE (vr0.min) == INTEGER_CST
3346 && TREE_CODE (vr0.max) == INTEGER_CST
3347 && (!is_overflow_infinity (vr0.min)
3348 || (vr0.type == VR_RANGE
3349 && TYPE_PRECISION (outer_type) > TYPE_PRECISION (inner_type)
3350 && needs_overflow_infinity (outer_type)
3351 && supports_overflow_infinity (outer_type)))
3352 && (!is_overflow_infinity (vr0.max)
3353 || (vr0.type == VR_RANGE
3354 && TYPE_PRECISION (outer_type) > TYPE_PRECISION (inner_type)
3355 && needs_overflow_infinity (outer_type)
3356 && supports_overflow_infinity (outer_type)))
3357 && (TYPE_PRECISION (outer_type) >= TYPE_PRECISION (inner_type)
3358 || (vr0.type == VR_RANGE
3359 && integer_zerop (int_const_binop (RSHIFT_EXPR,
3360 int_const_binop (MINUS_EXPR, vr0.max, vr0.min),
3361 size_int (TYPE_PRECISION (outer_type)))))))
3363 tree new_min, new_max;
3364 if (is_overflow_infinity (vr0.min))
3365 new_min = negative_overflow_infinity (outer_type);
3366 else
3367 new_min = force_fit_type (outer_type, wi::to_widest (vr0.min),
3368 0, false);
3369 if (is_overflow_infinity (vr0.max))
3370 new_max = positive_overflow_infinity (outer_type);
3371 else
3372 new_max = force_fit_type (outer_type, wi::to_widest (vr0.max),
3373 0, false);
3374 set_and_canonicalize_value_range (vr, vr0.type,
3375 new_min, new_max, NULL);
3376 return;
3379 set_value_range_to_varying (vr);
3380 return;
3382 else if (code == ABS_EXPR)
3384 tree min, max;
3385 int cmp;
3387 /* Pass through vr0 in the easy cases. */
3388 if (TYPE_UNSIGNED (type)
3389 || value_range_nonnegative_p (&vr0))
3391 copy_value_range (vr, &vr0);
3392 return;
3395 /* For the remaining varying or symbolic ranges we can't do anything
3396 useful. */
3397 if (vr0.type == VR_VARYING
3398 || symbolic_range_p (&vr0))
3400 set_value_range_to_varying (vr);
3401 return;
3404 /* -TYPE_MIN_VALUE = TYPE_MIN_VALUE with flag_wrapv so we can't get a
3405 useful range. */
3406 if (!TYPE_OVERFLOW_UNDEFINED (type)
3407 && ((vr0.type == VR_RANGE
3408 && vrp_val_is_min (vr0.min))
3409 || (vr0.type == VR_ANTI_RANGE
3410 && !vrp_val_is_min (vr0.min))))
3412 set_value_range_to_varying (vr);
3413 return;
3416 /* ABS_EXPR may flip the range around, if the original range
3417 included negative values. */
3418 if (is_overflow_infinity (vr0.min))
3419 min = positive_overflow_infinity (type);
3420 else if (!vrp_val_is_min (vr0.min))
3421 min = fold_unary_to_constant (code, type, vr0.min);
3422 else if (!needs_overflow_infinity (type))
3423 min = TYPE_MAX_VALUE (type);
3424 else if (supports_overflow_infinity (type))
3425 min = positive_overflow_infinity (type);
3426 else
3428 set_value_range_to_varying (vr);
3429 return;
3432 if (is_overflow_infinity (vr0.max))
3433 max = positive_overflow_infinity (type);
3434 else if (!vrp_val_is_min (vr0.max))
3435 max = fold_unary_to_constant (code, type, vr0.max);
3436 else if (!needs_overflow_infinity (type))
3437 max = TYPE_MAX_VALUE (type);
3438 else if (supports_overflow_infinity (type)
3439 /* We shouldn't generate [+INF, +INF] as set_value_range
3440 doesn't like this and ICEs. */
3441 && !is_positive_overflow_infinity (min))
3442 max = positive_overflow_infinity (type);
3443 else
3445 set_value_range_to_varying (vr);
3446 return;
3449 cmp = compare_values (min, max);
3451 /* If a VR_ANTI_RANGEs contains zero, then we have
3452 ~[-INF, min(MIN, MAX)]. */
3453 if (vr0.type == VR_ANTI_RANGE)
3455 if (range_includes_zero_p (vr0.min, vr0.max) == 1)
3457 /* Take the lower of the two values. */
3458 if (cmp != 1)
3459 max = min;
3461 /* Create ~[-INF, min (abs(MIN), abs(MAX))]
3462 or ~[-INF + 1, min (abs(MIN), abs(MAX))] when
3463 flag_wrapv is set and the original anti-range doesn't include
3464 TYPE_MIN_VALUE, remember -TYPE_MIN_VALUE = TYPE_MIN_VALUE. */
3465 if (TYPE_OVERFLOW_WRAPS (type))
3467 tree type_min_value = TYPE_MIN_VALUE (type);
3469 min = (vr0.min != type_min_value
3470 ? int_const_binop (PLUS_EXPR, type_min_value,
3471 build_int_cst (TREE_TYPE (type_min_value), 1))
3472 : type_min_value);
3474 else
3476 if (overflow_infinity_range_p (&vr0))
3477 min = negative_overflow_infinity (type);
3478 else
3479 min = TYPE_MIN_VALUE (type);
3482 else
3484 /* All else has failed, so create the range [0, INF], even for
3485 flag_wrapv since TYPE_MIN_VALUE is in the original
3486 anti-range. */
3487 vr0.type = VR_RANGE;
3488 min = build_int_cst (type, 0);
3489 if (needs_overflow_infinity (type))
3491 if (supports_overflow_infinity (type))
3492 max = positive_overflow_infinity (type);
3493 else
3495 set_value_range_to_varying (vr);
3496 return;
3499 else
3500 max = TYPE_MAX_VALUE (type);
3504 /* If the range contains zero then we know that the minimum value in the
3505 range will be zero. */
3506 else if (range_includes_zero_p (vr0.min, vr0.max) == 1)
3508 if (cmp == 1)
3509 max = min;
3510 min = build_int_cst (type, 0);
3512 else
3514 /* If the range was reversed, swap MIN and MAX. */
3515 if (cmp == 1)
3516 std::swap (min, max);
3519 cmp = compare_values (min, max);
3520 if (cmp == -2 || cmp == 1)
3522 /* If the new range has its limits swapped around (MIN > MAX),
3523 then the operation caused one of them to wrap around, mark
3524 the new range VARYING. */
3525 set_value_range_to_varying (vr);
3527 else
3528 set_value_range (vr, vr0.type, min, max, NULL);
3529 return;
3532 /* For unhandled operations fall back to varying. */
3533 set_value_range_to_varying (vr);
3534 return;
3538 /* Extract range information from a unary expression CODE OP0 based on
3539 the range of its operand with resulting type TYPE.
3540 The resulting range is stored in *VR. */
3542 static void
3543 extract_range_from_unary_expr (value_range *vr, enum tree_code code,
3544 tree type, tree op0)
3546 value_range vr0 = VR_INITIALIZER;
3548 /* Get value ranges for the operand. For constant operands, create
3549 a new value range with the operand to simplify processing. */
3550 if (TREE_CODE (op0) == SSA_NAME)
3551 vr0 = *(get_value_range (op0));
3552 else if (is_gimple_min_invariant (op0))
3553 set_value_range_to_value (&vr0, op0, NULL);
3554 else
3555 set_value_range_to_varying (&vr0);
3557 extract_range_from_unary_expr_1 (vr, code, type, &vr0, TREE_TYPE (op0));
3561 /* Extract range information from a conditional expression STMT based on
3562 the ranges of each of its operands and the expression code. */
3564 static void
3565 extract_range_from_cond_expr (value_range *vr, gassign *stmt)
3567 tree op0, op1;
3568 value_range vr0 = VR_INITIALIZER;
3569 value_range vr1 = VR_INITIALIZER;
3571 /* Get value ranges for each operand. For constant operands, create
3572 a new value range with the operand to simplify processing. */
3573 op0 = gimple_assign_rhs2 (stmt);
3574 if (TREE_CODE (op0) == SSA_NAME)
3575 vr0 = *(get_value_range (op0));
3576 else if (is_gimple_min_invariant (op0))
3577 set_value_range_to_value (&vr0, op0, NULL);
3578 else
3579 set_value_range_to_varying (&vr0);
3581 op1 = gimple_assign_rhs3 (stmt);
3582 if (TREE_CODE (op1) == SSA_NAME)
3583 vr1 = *(get_value_range (op1));
3584 else if (is_gimple_min_invariant (op1))
3585 set_value_range_to_value (&vr1, op1, NULL);
3586 else
3587 set_value_range_to_varying (&vr1);
3589 /* The resulting value range is the union of the operand ranges */
3590 copy_value_range (vr, &vr0);
3591 vrp_meet (vr, &vr1);
3595 /* Extract range information from a comparison expression EXPR based
3596 on the range of its operand and the expression code. */
3598 static void
3599 extract_range_from_comparison (value_range *vr, enum tree_code code,
3600 tree type, tree op0, tree op1)
3602 bool sop = false;
3603 tree val;
3605 val = vrp_evaluate_conditional_warnv_with_ops (code, op0, op1, false, &sop,
3606 NULL);
3608 /* A disadvantage of using a special infinity as an overflow
3609 representation is that we lose the ability to record overflow
3610 when we don't have an infinity. So we have to ignore a result
3611 which relies on overflow. */
3613 if (val && !is_overflow_infinity (val) && !sop)
3615 /* Since this expression was found on the RHS of an assignment,
3616 its type may be different from _Bool. Convert VAL to EXPR's
3617 type. */
3618 val = fold_convert (type, val);
3619 if (is_gimple_min_invariant (val))
3620 set_value_range_to_value (vr, val, vr->equiv);
3621 else
3622 set_value_range (vr, VR_RANGE, val, val, vr->equiv);
3624 else
3625 /* The result of a comparison is always true or false. */
3626 set_value_range_to_truthvalue (vr, type);
3629 /* Helper function for simplify_internal_call_using_ranges and
3630 extract_range_basic. Return true if OP0 SUBCODE OP1 for
3631 SUBCODE {PLUS,MINUS,MULT}_EXPR is known to never overflow or
3632 always overflow. Set *OVF to true if it is known to always
3633 overflow. */
3635 static bool
3636 check_for_binary_op_overflow (enum tree_code subcode, tree type,
3637 tree op0, tree op1, bool *ovf)
3639 value_range vr0 = VR_INITIALIZER;
3640 value_range vr1 = VR_INITIALIZER;
3641 if (TREE_CODE (op0) == SSA_NAME)
3642 vr0 = *get_value_range (op0);
3643 else if (TREE_CODE (op0) == INTEGER_CST)
3644 set_value_range_to_value (&vr0, op0, NULL);
3645 else
3646 set_value_range_to_varying (&vr0);
3648 if (TREE_CODE (op1) == SSA_NAME)
3649 vr1 = *get_value_range (op1);
3650 else if (TREE_CODE (op1) == INTEGER_CST)
3651 set_value_range_to_value (&vr1, op1, NULL);
3652 else
3653 set_value_range_to_varying (&vr1);
3655 if (!range_int_cst_p (&vr0)
3656 || TREE_OVERFLOW (vr0.min)
3657 || TREE_OVERFLOW (vr0.max))
3659 vr0.min = vrp_val_min (TREE_TYPE (op0));
3660 vr0.max = vrp_val_max (TREE_TYPE (op0));
3662 if (!range_int_cst_p (&vr1)
3663 || TREE_OVERFLOW (vr1.min)
3664 || TREE_OVERFLOW (vr1.max))
3666 vr1.min = vrp_val_min (TREE_TYPE (op1));
3667 vr1.max = vrp_val_max (TREE_TYPE (op1));
3669 *ovf = arith_overflowed_p (subcode, type, vr0.min,
3670 subcode == MINUS_EXPR ? vr1.max : vr1.min);
3671 if (arith_overflowed_p (subcode, type, vr0.max,
3672 subcode == MINUS_EXPR ? vr1.min : vr1.max) != *ovf)
3673 return false;
3674 if (subcode == MULT_EXPR)
3676 if (arith_overflowed_p (subcode, type, vr0.min, vr1.max) != *ovf
3677 || arith_overflowed_p (subcode, type, vr0.max, vr1.min) != *ovf)
3678 return false;
3680 if (*ovf)
3682 /* So far we found that there is an overflow on the boundaries.
3683 That doesn't prove that there is an overflow even for all values
3684 in between the boundaries. For that compute widest_int range
3685 of the result and see if it doesn't overlap the range of
3686 type. */
3687 widest_int wmin, wmax;
3688 widest_int w[4];
3689 int i;
3690 w[0] = wi::to_widest (vr0.min);
3691 w[1] = wi::to_widest (vr0.max);
3692 w[2] = wi::to_widest (vr1.min);
3693 w[3] = wi::to_widest (vr1.max);
3694 for (i = 0; i < 4; i++)
3696 widest_int wt;
3697 switch (subcode)
3699 case PLUS_EXPR:
3700 wt = wi::add (w[i & 1], w[2 + (i & 2) / 2]);
3701 break;
3702 case MINUS_EXPR:
3703 wt = wi::sub (w[i & 1], w[2 + (i & 2) / 2]);
3704 break;
3705 case MULT_EXPR:
3706 wt = wi::mul (w[i & 1], w[2 + (i & 2) / 2]);
3707 break;
3708 default:
3709 gcc_unreachable ();
3711 if (i == 0)
3713 wmin = wt;
3714 wmax = wt;
3716 else
3718 wmin = wi::smin (wmin, wt);
3719 wmax = wi::smax (wmax, wt);
3722 /* The result of op0 CODE op1 is known to be in range
3723 [wmin, wmax]. */
3724 widest_int wtmin = wi::to_widest (vrp_val_min (type));
3725 widest_int wtmax = wi::to_widest (vrp_val_max (type));
3726 /* If all values in [wmin, wmax] are smaller than
3727 [wtmin, wtmax] or all are larger than [wtmin, wtmax],
3728 the arithmetic operation will always overflow. */
3729 if (wmax < wtmin || wmin > wtmax)
3730 return true;
3731 return false;
3733 return true;
3736 /* Try to derive a nonnegative or nonzero range out of STMT relying
3737 primarily on generic routines in fold in conjunction with range data.
3738 Store the result in *VR */
3740 static void
3741 extract_range_basic (value_range *vr, gimple *stmt)
3743 bool sop = false;
3744 tree type = gimple_expr_type (stmt);
3746 if (is_gimple_call (stmt))
3748 tree arg;
3749 int mini, maxi, zerov = 0, prec;
3750 enum tree_code subcode = ERROR_MARK;
3751 combined_fn cfn = gimple_call_combined_fn (stmt);
3753 switch (cfn)
3755 case CFN_BUILT_IN_CONSTANT_P:
3756 /* If the call is __builtin_constant_p and the argument is a
3757 function parameter resolve it to false. This avoids bogus
3758 array bound warnings.
3759 ??? We could do this as early as inlining is finished. */
3760 arg = gimple_call_arg (stmt, 0);
3761 if (TREE_CODE (arg) == SSA_NAME
3762 && SSA_NAME_IS_DEFAULT_DEF (arg)
3763 && TREE_CODE (SSA_NAME_VAR (arg)) == PARM_DECL)
3765 set_value_range_to_null (vr, type);
3766 return;
3768 break;
3769 /* Both __builtin_ffs* and __builtin_popcount return
3770 [0, prec]. */
3771 CASE_CFN_FFS:
3772 CASE_CFN_POPCOUNT:
3773 arg = gimple_call_arg (stmt, 0);
3774 prec = TYPE_PRECISION (TREE_TYPE (arg));
3775 mini = 0;
3776 maxi = prec;
3777 if (TREE_CODE (arg) == SSA_NAME)
3779 value_range *vr0 = get_value_range (arg);
3780 /* If arg is non-zero, then ffs or popcount
3781 are non-zero. */
3782 if (((vr0->type == VR_RANGE
3783 && range_includes_zero_p (vr0->min, vr0->max) == 0)
3784 || (vr0->type == VR_ANTI_RANGE
3785 && range_includes_zero_p (vr0->min, vr0->max) == 1))
3786 && !is_overflow_infinity (vr0->min)
3787 && !is_overflow_infinity (vr0->max))
3788 mini = 1;
3789 /* If some high bits are known to be zero,
3790 we can decrease the maximum. */
3791 if (vr0->type == VR_RANGE
3792 && TREE_CODE (vr0->max) == INTEGER_CST
3793 && !operand_less_p (vr0->min,
3794 build_zero_cst (TREE_TYPE (vr0->min)))
3795 && !is_overflow_infinity (vr0->max))
3796 maxi = tree_floor_log2 (vr0->max) + 1;
3798 goto bitop_builtin;
3799 /* __builtin_parity* returns [0, 1]. */
3800 CASE_CFN_PARITY:
3801 mini = 0;
3802 maxi = 1;
3803 goto bitop_builtin;
3804 /* __builtin_c[lt]z* return [0, prec-1], except for
3805 when the argument is 0, but that is undefined behavior.
3806 On many targets where the CLZ RTL or optab value is defined
3807 for 0 the value is prec, so include that in the range
3808 by default. */
3809 CASE_CFN_CLZ:
3810 arg = gimple_call_arg (stmt, 0);
3811 prec = TYPE_PRECISION (TREE_TYPE (arg));
3812 mini = 0;
3813 maxi = prec;
3814 if (optab_handler (clz_optab, TYPE_MODE (TREE_TYPE (arg)))
3815 != CODE_FOR_nothing
3816 && CLZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (TREE_TYPE (arg)),
3817 zerov)
3818 /* Handle only the single common value. */
3819 && zerov != prec)
3820 /* Magic value to give up, unless vr0 proves
3821 arg is non-zero. */
3822 mini = -2;
3823 if (TREE_CODE (arg) == SSA_NAME)
3825 value_range *vr0 = get_value_range (arg);
3826 /* From clz of VR_RANGE minimum we can compute
3827 result maximum. */
3828 if (vr0->type == VR_RANGE
3829 && TREE_CODE (vr0->min) == INTEGER_CST
3830 && !is_overflow_infinity (vr0->min))
3832 maxi = prec - 1 - tree_floor_log2 (vr0->min);
3833 if (maxi != prec)
3834 mini = 0;
3836 else if (vr0->type == VR_ANTI_RANGE
3837 && integer_zerop (vr0->min)
3838 && !is_overflow_infinity (vr0->min))
3840 maxi = prec - 1;
3841 mini = 0;
3843 if (mini == -2)
3844 break;
3845 /* From clz of VR_RANGE maximum we can compute
3846 result minimum. */
3847 if (vr0->type == VR_RANGE
3848 && TREE_CODE (vr0->max) == INTEGER_CST
3849 && !is_overflow_infinity (vr0->max))
3851 mini = prec - 1 - tree_floor_log2 (vr0->max);
3852 if (mini == prec)
3853 break;
3856 if (mini == -2)
3857 break;
3858 goto bitop_builtin;
3859 /* __builtin_ctz* return [0, prec-1], except for
3860 when the argument is 0, but that is undefined behavior.
3861 If there is a ctz optab for this mode and
3862 CTZ_DEFINED_VALUE_AT_ZERO, include that in the range,
3863 otherwise just assume 0 won't be seen. */
3864 CASE_CFN_CTZ:
3865 arg = gimple_call_arg (stmt, 0);
3866 prec = TYPE_PRECISION (TREE_TYPE (arg));
3867 mini = 0;
3868 maxi = prec - 1;
3869 if (optab_handler (ctz_optab, TYPE_MODE (TREE_TYPE (arg)))
3870 != CODE_FOR_nothing
3871 && CTZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (TREE_TYPE (arg)),
3872 zerov))
3874 /* Handle only the two common values. */
3875 if (zerov == -1)
3876 mini = -1;
3877 else if (zerov == prec)
3878 maxi = prec;
3879 else
3880 /* Magic value to give up, unless vr0 proves
3881 arg is non-zero. */
3882 mini = -2;
3884 if (TREE_CODE (arg) == SSA_NAME)
3886 value_range *vr0 = get_value_range (arg);
3887 /* If arg is non-zero, then use [0, prec - 1]. */
3888 if (((vr0->type == VR_RANGE
3889 && integer_nonzerop (vr0->min))
3890 || (vr0->type == VR_ANTI_RANGE
3891 && integer_zerop (vr0->min)))
3892 && !is_overflow_infinity (vr0->min))
3894 mini = 0;
3895 maxi = prec - 1;
3897 /* If some high bits are known to be zero,
3898 we can decrease the result maximum. */
3899 if (vr0->type == VR_RANGE
3900 && TREE_CODE (vr0->max) == INTEGER_CST
3901 && !is_overflow_infinity (vr0->max))
3903 maxi = tree_floor_log2 (vr0->max);
3904 /* For vr0 [0, 0] give up. */
3905 if (maxi == -1)
3906 break;
3909 if (mini == -2)
3910 break;
3911 goto bitop_builtin;
3912 /* __builtin_clrsb* returns [0, prec-1]. */
3913 CASE_CFN_CLRSB:
3914 arg = gimple_call_arg (stmt, 0);
3915 prec = TYPE_PRECISION (TREE_TYPE (arg));
3916 mini = 0;
3917 maxi = prec - 1;
3918 goto bitop_builtin;
3919 bitop_builtin:
3920 set_value_range (vr, VR_RANGE, build_int_cst (type, mini),
3921 build_int_cst (type, maxi), NULL);
3922 return;
3923 case CFN_UBSAN_CHECK_ADD:
3924 subcode = PLUS_EXPR;
3925 break;
3926 case CFN_UBSAN_CHECK_SUB:
3927 subcode = MINUS_EXPR;
3928 break;
3929 case CFN_UBSAN_CHECK_MUL:
3930 subcode = MULT_EXPR;
3931 break;
3932 case CFN_GOACC_DIM_SIZE:
3933 case CFN_GOACC_DIM_POS:
3934 /* Optimizing these two internal functions helps the loop
3935 optimizer eliminate outer comparisons. Size is [1,N]
3936 and pos is [0,N-1]. */
3938 bool is_pos = cfn == CFN_GOACC_DIM_POS;
3939 int axis = get_oacc_ifn_dim_arg (stmt);
3940 int size = get_oacc_fn_dim_size (current_function_decl, axis);
3942 if (!size)
3943 /* If it's dynamic, the backend might know a hardware
3944 limitation. */
3945 size = targetm.goacc.dim_limit (axis);
3947 tree type = TREE_TYPE (gimple_call_lhs (stmt));
3948 set_value_range (vr, VR_RANGE,
3949 build_int_cst (type, is_pos ? 0 : 1),
3950 size ? build_int_cst (type, size - is_pos)
3951 : vrp_val_max (type), NULL);
3953 return;
3954 default:
3955 break;
3957 if (subcode != ERROR_MARK)
3959 bool saved_flag_wrapv = flag_wrapv;
3960 /* Pretend the arithmetics is wrapping. If there is
3961 any overflow, we'll complain, but will actually do
3962 wrapping operation. */
3963 flag_wrapv = 1;
3964 extract_range_from_binary_expr (vr, subcode, type,
3965 gimple_call_arg (stmt, 0),
3966 gimple_call_arg (stmt, 1));
3967 flag_wrapv = saved_flag_wrapv;
3969 /* If for both arguments vrp_valueize returned non-NULL,
3970 this should have been already folded and if not, it
3971 wasn't folded because of overflow. Avoid removing the
3972 UBSAN_CHECK_* calls in that case. */
3973 if (vr->type == VR_RANGE
3974 && (vr->min == vr->max
3975 || operand_equal_p (vr->min, vr->max, 0)))
3976 set_value_range_to_varying (vr);
3977 return;
3980 /* Handle extraction of the two results (result of arithmetics and
3981 a flag whether arithmetics overflowed) from {ADD,SUB,MUL}_OVERFLOW
3982 internal function. */
3983 else if (is_gimple_assign (stmt)
3984 && (gimple_assign_rhs_code (stmt) == REALPART_EXPR
3985 || gimple_assign_rhs_code (stmt) == IMAGPART_EXPR)
3986 && INTEGRAL_TYPE_P (type))
3988 enum tree_code code = gimple_assign_rhs_code (stmt);
3989 tree op = gimple_assign_rhs1 (stmt);
3990 if (TREE_CODE (op) == code && TREE_CODE (TREE_OPERAND (op, 0)) == SSA_NAME)
3992 gimple *g = SSA_NAME_DEF_STMT (TREE_OPERAND (op, 0));
3993 if (is_gimple_call (g) && gimple_call_internal_p (g))
3995 enum tree_code subcode = ERROR_MARK;
3996 switch (gimple_call_internal_fn (g))
3998 case IFN_ADD_OVERFLOW:
3999 subcode = PLUS_EXPR;
4000 break;
4001 case IFN_SUB_OVERFLOW:
4002 subcode = MINUS_EXPR;
4003 break;
4004 case IFN_MUL_OVERFLOW:
4005 subcode = MULT_EXPR;
4006 break;
4007 default:
4008 break;
4010 if (subcode != ERROR_MARK)
4012 tree op0 = gimple_call_arg (g, 0);
4013 tree op1 = gimple_call_arg (g, 1);
4014 if (code == IMAGPART_EXPR)
4016 bool ovf = false;
4017 if (check_for_binary_op_overflow (subcode, type,
4018 op0, op1, &ovf))
4019 set_value_range_to_value (vr,
4020 build_int_cst (type, ovf),
4021 NULL);
4022 else if (TYPE_PRECISION (type) == 1
4023 && !TYPE_UNSIGNED (type))
4024 set_value_range_to_varying (vr);
4025 else
4026 set_value_range (vr, VR_RANGE, build_int_cst (type, 0),
4027 build_int_cst (type, 1), NULL);
4029 else if (types_compatible_p (type, TREE_TYPE (op0))
4030 && types_compatible_p (type, TREE_TYPE (op1)))
4032 bool saved_flag_wrapv = flag_wrapv;
4033 /* Pretend the arithmetics is wrapping. If there is
4034 any overflow, IMAGPART_EXPR will be set. */
4035 flag_wrapv = 1;
4036 extract_range_from_binary_expr (vr, subcode, type,
4037 op0, op1);
4038 flag_wrapv = saved_flag_wrapv;
4040 else
4042 value_range vr0 = VR_INITIALIZER;
4043 value_range vr1 = VR_INITIALIZER;
4044 bool saved_flag_wrapv = flag_wrapv;
4045 /* Pretend the arithmetics is wrapping. If there is
4046 any overflow, IMAGPART_EXPR will be set. */
4047 flag_wrapv = 1;
4048 extract_range_from_unary_expr (&vr0, NOP_EXPR,
4049 type, op0);
4050 extract_range_from_unary_expr (&vr1, NOP_EXPR,
4051 type, op1);
4052 extract_range_from_binary_expr_1 (vr, subcode, type,
4053 &vr0, &vr1);
4054 flag_wrapv = saved_flag_wrapv;
4056 return;
4061 if (INTEGRAL_TYPE_P (type)
4062 && gimple_stmt_nonnegative_warnv_p (stmt, &sop))
4063 set_value_range_to_nonnegative (vr, type,
4064 sop || stmt_overflow_infinity (stmt));
4065 else if (vrp_stmt_computes_nonzero (stmt, &sop)
4066 && !sop)
4067 set_value_range_to_nonnull (vr, type);
4068 else
4069 set_value_range_to_varying (vr);
4073 /* Try to compute a useful range out of assignment STMT and store it
4074 in *VR. */
4076 static void
4077 extract_range_from_assignment (value_range *vr, gassign *stmt)
4079 enum tree_code code = gimple_assign_rhs_code (stmt);
4081 if (code == ASSERT_EXPR)
4082 extract_range_from_assert (vr, gimple_assign_rhs1 (stmt));
4083 else if (code == SSA_NAME)
4084 extract_range_from_ssa_name (vr, gimple_assign_rhs1 (stmt));
4085 else if (TREE_CODE_CLASS (code) == tcc_binary)
4086 extract_range_from_binary_expr (vr, gimple_assign_rhs_code (stmt),
4087 gimple_expr_type (stmt),
4088 gimple_assign_rhs1 (stmt),
4089 gimple_assign_rhs2 (stmt));
4090 else if (TREE_CODE_CLASS (code) == tcc_unary)
4091 extract_range_from_unary_expr (vr, gimple_assign_rhs_code (stmt),
4092 gimple_expr_type (stmt),
4093 gimple_assign_rhs1 (stmt));
4094 else if (code == COND_EXPR)
4095 extract_range_from_cond_expr (vr, stmt);
4096 else if (TREE_CODE_CLASS (code) == tcc_comparison)
4097 extract_range_from_comparison (vr, gimple_assign_rhs_code (stmt),
4098 gimple_expr_type (stmt),
4099 gimple_assign_rhs1 (stmt),
4100 gimple_assign_rhs2 (stmt));
4101 else if (get_gimple_rhs_class (code) == GIMPLE_SINGLE_RHS
4102 && is_gimple_min_invariant (gimple_assign_rhs1 (stmt)))
4103 set_value_range_to_value (vr, gimple_assign_rhs1 (stmt), NULL);
4104 else
4105 set_value_range_to_varying (vr);
4107 if (vr->type == VR_VARYING)
4108 extract_range_basic (vr, stmt);
4111 /* Given a range VR, a LOOP and a variable VAR, determine whether it
4112 would be profitable to adjust VR using scalar evolution information
4113 for VAR. If so, update VR with the new limits. */
4115 static void
4116 adjust_range_with_scev (value_range *vr, struct loop *loop,
4117 gimple *stmt, tree var)
4119 tree init, step, chrec, tmin, tmax, min, max, type, tem;
4120 enum ev_direction dir;
4122 /* TODO. Don't adjust anti-ranges. An anti-range may provide
4123 better opportunities than a regular range, but I'm not sure. */
4124 if (vr->type == VR_ANTI_RANGE)
4125 return;
4127 chrec = instantiate_parameters (loop, analyze_scalar_evolution (loop, var));
4129 /* Like in PR19590, scev can return a constant function. */
4130 if (is_gimple_min_invariant (chrec))
4132 set_value_range_to_value (vr, chrec, vr->equiv);
4133 return;
4136 if (TREE_CODE (chrec) != POLYNOMIAL_CHREC)
4137 return;
4139 init = initial_condition_in_loop_num (chrec, loop->num);
4140 tem = op_with_constant_singleton_value_range (init);
4141 if (tem)
4142 init = tem;
4143 step = evolution_part_in_loop_num (chrec, loop->num);
4144 tem = op_with_constant_singleton_value_range (step);
4145 if (tem)
4146 step = tem;
4148 /* If STEP is symbolic, we can't know whether INIT will be the
4149 minimum or maximum value in the range. Also, unless INIT is
4150 a simple expression, compare_values and possibly other functions
4151 in tree-vrp won't be able to handle it. */
4152 if (step == NULL_TREE
4153 || !is_gimple_min_invariant (step)
4154 || !valid_value_p (init))
4155 return;
4157 dir = scev_direction (chrec);
4158 if (/* Do not adjust ranges if we do not know whether the iv increases
4159 or decreases, ... */
4160 dir == EV_DIR_UNKNOWN
4161 /* ... or if it may wrap. */
4162 || scev_probably_wraps_p (NULL_TREE, init, step, stmt,
4163 get_chrec_loop (chrec), true))
4164 return;
4166 /* We use TYPE_MIN_VALUE and TYPE_MAX_VALUE here instead of
4167 negative_overflow_infinity and positive_overflow_infinity,
4168 because we have concluded that the loop probably does not
4169 wrap. */
4171 type = TREE_TYPE (var);
4172 if (POINTER_TYPE_P (type) || !TYPE_MIN_VALUE (type))
4173 tmin = lower_bound_in_type (type, type);
4174 else
4175 tmin = TYPE_MIN_VALUE (type);
4176 if (POINTER_TYPE_P (type) || !TYPE_MAX_VALUE (type))
4177 tmax = upper_bound_in_type (type, type);
4178 else
4179 tmax = TYPE_MAX_VALUE (type);
4181 /* Try to use estimated number of iterations for the loop to constrain the
4182 final value in the evolution. */
4183 if (TREE_CODE (step) == INTEGER_CST
4184 && is_gimple_val (init)
4185 && (TREE_CODE (init) != SSA_NAME
4186 || get_value_range (init)->type == VR_RANGE))
4188 widest_int nit;
4190 /* We are only entering here for loop header PHI nodes, so using
4191 the number of latch executions is the correct thing to use. */
4192 if (max_loop_iterations (loop, &nit))
4194 value_range maxvr = VR_INITIALIZER;
4195 signop sgn = TYPE_SIGN (TREE_TYPE (step));
4196 bool overflow;
4198 widest_int wtmp = wi::mul (wi::to_widest (step), nit, sgn,
4199 &overflow);
4200 /* If the multiplication overflowed we can't do a meaningful
4201 adjustment. Likewise if the result doesn't fit in the type
4202 of the induction variable. For a signed type we have to
4203 check whether the result has the expected signedness which
4204 is that of the step as number of iterations is unsigned. */
4205 if (!overflow
4206 && wi::fits_to_tree_p (wtmp, TREE_TYPE (init))
4207 && (sgn == UNSIGNED
4208 || wi::gts_p (wtmp, 0) == wi::gts_p (step, 0)))
4210 tem = wide_int_to_tree (TREE_TYPE (init), wtmp);
4211 extract_range_from_binary_expr (&maxvr, PLUS_EXPR,
4212 TREE_TYPE (init), init, tem);
4213 /* Likewise if the addition did. */
4214 if (maxvr.type == VR_RANGE)
4216 value_range initvr = VR_INITIALIZER;
4218 if (TREE_CODE (init) == SSA_NAME)
4219 initvr = *(get_value_range (init));
4220 else if (is_gimple_min_invariant (init))
4221 set_value_range_to_value (&initvr, init, NULL);
4222 else
4223 return;
4225 /* Check if init + nit * step overflows. Though we checked
4226 scev {init, step}_loop doesn't wrap, it is not enough
4227 because the loop may exit immediately. Overflow could
4228 happen in the plus expression in this case. */
4229 if ((dir == EV_DIR_DECREASES
4230 && (is_negative_overflow_infinity (maxvr.min)
4231 || compare_values (maxvr.min, initvr.min) != -1))
4232 || (dir == EV_DIR_GROWS
4233 && (is_positive_overflow_infinity (maxvr.max)
4234 || compare_values (maxvr.max, initvr.max) != 1)))
4235 return;
4237 tmin = maxvr.min;
4238 tmax = maxvr.max;
4244 if (vr->type == VR_VARYING || vr->type == VR_UNDEFINED)
4246 min = tmin;
4247 max = tmax;
4249 /* For VARYING or UNDEFINED ranges, just about anything we get
4250 from scalar evolutions should be better. */
4252 if (dir == EV_DIR_DECREASES)
4253 max = init;
4254 else
4255 min = init;
4257 else if (vr->type == VR_RANGE)
4259 min = vr->min;
4260 max = vr->max;
4262 if (dir == EV_DIR_DECREASES)
4264 /* INIT is the maximum value. If INIT is lower than VR->MAX
4265 but no smaller than VR->MIN, set VR->MAX to INIT. */
4266 if (compare_values (init, max) == -1)
4267 max = init;
4269 /* According to the loop information, the variable does not
4270 overflow. If we think it does, probably because of an
4271 overflow due to arithmetic on a different INF value,
4272 reset now. */
4273 if (is_negative_overflow_infinity (min)
4274 || compare_values (min, tmin) == -1)
4275 min = tmin;
4278 else
4280 /* If INIT is bigger than VR->MIN, set VR->MIN to INIT. */
4281 if (compare_values (init, min) == 1)
4282 min = init;
4284 if (is_positive_overflow_infinity (max)
4285 || compare_values (tmax, max) == -1)
4286 max = tmax;
4289 else
4290 return;
4292 /* If we just created an invalid range with the minimum
4293 greater than the maximum, we fail conservatively.
4294 This should happen only in unreachable
4295 parts of code, or for invalid programs. */
4296 if (compare_values (min, max) == 1
4297 || (is_negative_overflow_infinity (min)
4298 && is_positive_overflow_infinity (max)))
4299 return;
4301 /* Even for valid range info, sometimes overflow flag will leak in.
4302 As GIMPLE IL should have no constants with TREE_OVERFLOW set, we
4303 drop them except for +-overflow_infinity which still need special
4304 handling in vrp pass. */
4305 if (TREE_OVERFLOW_P (min)
4306 && ! is_negative_overflow_infinity (min))
4307 min = drop_tree_overflow (min);
4308 if (TREE_OVERFLOW_P (max)
4309 && ! is_positive_overflow_infinity (max))
4310 max = drop_tree_overflow (max);
4312 set_value_range (vr, VR_RANGE, min, max, vr->equiv);
4316 /* Given two numeric value ranges VR0, VR1 and a comparison code COMP:
4318 - Return BOOLEAN_TRUE_NODE if VR0 COMP VR1 always returns true for
4319 all the values in the ranges.
4321 - Return BOOLEAN_FALSE_NODE if the comparison always returns false.
4323 - Return NULL_TREE if it is not always possible to determine the
4324 value of the comparison.
4326 Also set *STRICT_OVERFLOW_P to indicate whether a range with an
4327 overflow infinity was used in the test. */
4330 static tree
4331 compare_ranges (enum tree_code comp, value_range *vr0, value_range *vr1,
4332 bool *strict_overflow_p)
4334 /* VARYING or UNDEFINED ranges cannot be compared. */
4335 if (vr0->type == VR_VARYING
4336 || vr0->type == VR_UNDEFINED
4337 || vr1->type == VR_VARYING
4338 || vr1->type == VR_UNDEFINED)
4339 return NULL_TREE;
4341 /* Anti-ranges need to be handled separately. */
4342 if (vr0->type == VR_ANTI_RANGE || vr1->type == VR_ANTI_RANGE)
4344 /* If both are anti-ranges, then we cannot compute any
4345 comparison. */
4346 if (vr0->type == VR_ANTI_RANGE && vr1->type == VR_ANTI_RANGE)
4347 return NULL_TREE;
4349 /* These comparisons are never statically computable. */
4350 if (comp == GT_EXPR
4351 || comp == GE_EXPR
4352 || comp == LT_EXPR
4353 || comp == LE_EXPR)
4354 return NULL_TREE;
4356 /* Equality can be computed only between a range and an
4357 anti-range. ~[VAL1, VAL2] == [VAL1, VAL2] is always false. */
4358 if (vr0->type == VR_RANGE)
4360 /* To simplify processing, make VR0 the anti-range. */
4361 value_range *tmp = vr0;
4362 vr0 = vr1;
4363 vr1 = tmp;
4366 gcc_assert (comp == NE_EXPR || comp == EQ_EXPR);
4368 if (compare_values_warnv (vr0->min, vr1->min, strict_overflow_p) == 0
4369 && compare_values_warnv (vr0->max, vr1->max, strict_overflow_p) == 0)
4370 return (comp == NE_EXPR) ? boolean_true_node : boolean_false_node;
4372 return NULL_TREE;
4375 if (!usable_range_p (vr0, strict_overflow_p)
4376 || !usable_range_p (vr1, strict_overflow_p))
4377 return NULL_TREE;
4379 /* Simplify processing. If COMP is GT_EXPR or GE_EXPR, switch the
4380 operands around and change the comparison code. */
4381 if (comp == GT_EXPR || comp == GE_EXPR)
4383 comp = (comp == GT_EXPR) ? LT_EXPR : LE_EXPR;
4384 std::swap (vr0, vr1);
4387 if (comp == EQ_EXPR)
4389 /* Equality may only be computed if both ranges represent
4390 exactly one value. */
4391 if (compare_values_warnv (vr0->min, vr0->max, strict_overflow_p) == 0
4392 && compare_values_warnv (vr1->min, vr1->max, strict_overflow_p) == 0)
4394 int cmp_min = compare_values_warnv (vr0->min, vr1->min,
4395 strict_overflow_p);
4396 int cmp_max = compare_values_warnv (vr0->max, vr1->max,
4397 strict_overflow_p);
4398 if (cmp_min == 0 && cmp_max == 0)
4399 return boolean_true_node;
4400 else if (cmp_min != -2 && cmp_max != -2)
4401 return boolean_false_node;
4403 /* If [V0_MIN, V1_MAX] < [V1_MIN, V1_MAX] then V0 != V1. */
4404 else if (compare_values_warnv (vr0->min, vr1->max,
4405 strict_overflow_p) == 1
4406 || compare_values_warnv (vr1->min, vr0->max,
4407 strict_overflow_p) == 1)
4408 return boolean_false_node;
4410 return NULL_TREE;
4412 else if (comp == NE_EXPR)
4414 int cmp1, cmp2;
4416 /* If VR0 is completely to the left or completely to the right
4417 of VR1, they are always different. Notice that we need to
4418 make sure that both comparisons yield similar results to
4419 avoid comparing values that cannot be compared at
4420 compile-time. */
4421 cmp1 = compare_values_warnv (vr0->max, vr1->min, strict_overflow_p);
4422 cmp2 = compare_values_warnv (vr0->min, vr1->max, strict_overflow_p);
4423 if ((cmp1 == -1 && cmp2 == -1) || (cmp1 == 1 && cmp2 == 1))
4424 return boolean_true_node;
4426 /* If VR0 and VR1 represent a single value and are identical,
4427 return false. */
4428 else if (compare_values_warnv (vr0->min, vr0->max,
4429 strict_overflow_p) == 0
4430 && compare_values_warnv (vr1->min, vr1->max,
4431 strict_overflow_p) == 0
4432 && compare_values_warnv (vr0->min, vr1->min,
4433 strict_overflow_p) == 0
4434 && compare_values_warnv (vr0->max, vr1->max,
4435 strict_overflow_p) == 0)
4436 return boolean_false_node;
4438 /* Otherwise, they may or may not be different. */
4439 else
4440 return NULL_TREE;
4442 else if (comp == LT_EXPR || comp == LE_EXPR)
4444 int tst;
4446 /* If VR0 is to the left of VR1, return true. */
4447 tst = compare_values_warnv (vr0->max, vr1->min, strict_overflow_p);
4448 if ((comp == LT_EXPR && tst == -1)
4449 || (comp == LE_EXPR && (tst == -1 || tst == 0)))
4451 if (overflow_infinity_range_p (vr0)
4452 || overflow_infinity_range_p (vr1))
4453 *strict_overflow_p = true;
4454 return boolean_true_node;
4457 /* If VR0 is to the right of VR1, return false. */
4458 tst = compare_values_warnv (vr0->min, vr1->max, strict_overflow_p);
4459 if ((comp == LT_EXPR && (tst == 0 || tst == 1))
4460 || (comp == LE_EXPR && tst == 1))
4462 if (overflow_infinity_range_p (vr0)
4463 || overflow_infinity_range_p (vr1))
4464 *strict_overflow_p = true;
4465 return boolean_false_node;
4468 /* Otherwise, we don't know. */
4469 return NULL_TREE;
4472 gcc_unreachable ();
4476 /* Given a value range VR, a value VAL and a comparison code COMP, return
4477 BOOLEAN_TRUE_NODE if VR COMP VAL always returns true for all the
4478 values in VR. Return BOOLEAN_FALSE_NODE if the comparison
4479 always returns false. Return NULL_TREE if it is not always
4480 possible to determine the value of the comparison. Also set
4481 *STRICT_OVERFLOW_P to indicate whether a range with an overflow
4482 infinity was used in the test. */
4484 static tree
4485 compare_range_with_value (enum tree_code comp, value_range *vr, tree val,
4486 bool *strict_overflow_p)
4488 if (vr->type == VR_VARYING || vr->type == VR_UNDEFINED)
4489 return NULL_TREE;
4491 /* Anti-ranges need to be handled separately. */
4492 if (vr->type == VR_ANTI_RANGE)
4494 /* For anti-ranges, the only predicates that we can compute at
4495 compile time are equality and inequality. */
4496 if (comp == GT_EXPR
4497 || comp == GE_EXPR
4498 || comp == LT_EXPR
4499 || comp == LE_EXPR)
4500 return NULL_TREE;
4502 /* ~[VAL_1, VAL_2] OP VAL is known if VAL_1 <= VAL <= VAL_2. */
4503 if (value_inside_range (val, vr->min, vr->max) == 1)
4504 return (comp == NE_EXPR) ? boolean_true_node : boolean_false_node;
4506 return NULL_TREE;
4509 if (!usable_range_p (vr, strict_overflow_p))
4510 return NULL_TREE;
4512 if (comp == EQ_EXPR)
4514 /* EQ_EXPR may only be computed if VR represents exactly
4515 one value. */
4516 if (compare_values_warnv (vr->min, vr->max, strict_overflow_p) == 0)
4518 int cmp = compare_values_warnv (vr->min, val, strict_overflow_p);
4519 if (cmp == 0)
4520 return boolean_true_node;
4521 else if (cmp == -1 || cmp == 1 || cmp == 2)
4522 return boolean_false_node;
4524 else if (compare_values_warnv (val, vr->min, strict_overflow_p) == -1
4525 || compare_values_warnv (vr->max, val, strict_overflow_p) == -1)
4526 return boolean_false_node;
4528 return NULL_TREE;
4530 else if (comp == NE_EXPR)
4532 /* If VAL is not inside VR, then they are always different. */
4533 if (compare_values_warnv (vr->max, val, strict_overflow_p) == -1
4534 || compare_values_warnv (vr->min, val, strict_overflow_p) == 1)
4535 return boolean_true_node;
4537 /* If VR represents exactly one value equal to VAL, then return
4538 false. */
4539 if (compare_values_warnv (vr->min, vr->max, strict_overflow_p) == 0
4540 && compare_values_warnv (vr->min, val, strict_overflow_p) == 0)
4541 return boolean_false_node;
4543 /* Otherwise, they may or may not be different. */
4544 return NULL_TREE;
4546 else if (comp == LT_EXPR || comp == LE_EXPR)
4548 int tst;
4550 /* If VR is to the left of VAL, return true. */
4551 tst = compare_values_warnv (vr->max, val, strict_overflow_p);
4552 if ((comp == LT_EXPR && tst == -1)
4553 || (comp == LE_EXPR && (tst == -1 || tst == 0)))
4555 if (overflow_infinity_range_p (vr))
4556 *strict_overflow_p = true;
4557 return boolean_true_node;
4560 /* If VR is to the right of VAL, return false. */
4561 tst = compare_values_warnv (vr->min, val, strict_overflow_p);
4562 if ((comp == LT_EXPR && (tst == 0 || tst == 1))
4563 || (comp == LE_EXPR && tst == 1))
4565 if (overflow_infinity_range_p (vr))
4566 *strict_overflow_p = true;
4567 return boolean_false_node;
4570 /* Otherwise, we don't know. */
4571 return NULL_TREE;
4573 else if (comp == GT_EXPR || comp == GE_EXPR)
4575 int tst;
4577 /* If VR is to the right of VAL, return true. */
4578 tst = compare_values_warnv (vr->min, val, strict_overflow_p);
4579 if ((comp == GT_EXPR && tst == 1)
4580 || (comp == GE_EXPR && (tst == 0 || tst == 1)))
4582 if (overflow_infinity_range_p (vr))
4583 *strict_overflow_p = true;
4584 return boolean_true_node;
4587 /* If VR is to the left of VAL, return false. */
4588 tst = compare_values_warnv (vr->max, val, strict_overflow_p);
4589 if ((comp == GT_EXPR && (tst == -1 || tst == 0))
4590 || (comp == GE_EXPR && tst == -1))
4592 if (overflow_infinity_range_p (vr))
4593 *strict_overflow_p = true;
4594 return boolean_false_node;
4597 /* Otherwise, we don't know. */
4598 return NULL_TREE;
4601 gcc_unreachable ();
4605 /* Debugging dumps. */
4607 void dump_value_range (FILE *, value_range *);
4608 void debug_value_range (value_range *);
4609 void dump_all_value_ranges (FILE *);
4610 void debug_all_value_ranges (void);
4611 void dump_vr_equiv (FILE *, bitmap);
4612 void debug_vr_equiv (bitmap);
4615 /* Dump value range VR to FILE. */
4617 void
4618 dump_value_range (FILE *file, value_range *vr)
4620 if (vr == NULL)
4621 fprintf (file, "[]");
4622 else if (vr->type == VR_UNDEFINED)
4623 fprintf (file, "UNDEFINED");
4624 else if (vr->type == VR_RANGE || vr->type == VR_ANTI_RANGE)
4626 tree type = TREE_TYPE (vr->min);
4628 fprintf (file, "%s[", (vr->type == VR_ANTI_RANGE) ? "~" : "");
4630 if (is_negative_overflow_infinity (vr->min))
4631 fprintf (file, "-INF(OVF)");
4632 else if (INTEGRAL_TYPE_P (type)
4633 && !TYPE_UNSIGNED (type)
4634 && vrp_val_is_min (vr->min))
4635 fprintf (file, "-INF");
4636 else
4637 print_generic_expr (file, vr->min, 0);
4639 fprintf (file, ", ");
4641 if (is_positive_overflow_infinity (vr->max))
4642 fprintf (file, "+INF(OVF)");
4643 else if (INTEGRAL_TYPE_P (type)
4644 && vrp_val_is_max (vr->max))
4645 fprintf (file, "+INF");
4646 else
4647 print_generic_expr (file, vr->max, 0);
4649 fprintf (file, "]");
4651 if (vr->equiv)
4653 bitmap_iterator bi;
4654 unsigned i, c = 0;
4656 fprintf (file, " EQUIVALENCES: { ");
4658 EXECUTE_IF_SET_IN_BITMAP (vr->equiv, 0, i, bi)
4660 print_generic_expr (file, ssa_name (i), 0);
4661 fprintf (file, " ");
4662 c++;
4665 fprintf (file, "} (%u elements)", c);
4668 else if (vr->type == VR_VARYING)
4669 fprintf (file, "VARYING");
4670 else
4671 fprintf (file, "INVALID RANGE");
4675 /* Dump value range VR to stderr. */
4677 DEBUG_FUNCTION void
4678 debug_value_range (value_range *vr)
4680 dump_value_range (stderr, vr);
4681 fprintf (stderr, "\n");
4685 /* Dump value ranges of all SSA_NAMEs to FILE. */
4687 void
4688 dump_all_value_ranges (FILE *file)
4690 size_t i;
4692 for (i = 0; i < num_vr_values; i++)
4694 if (vr_value[i])
4696 print_generic_expr (file, ssa_name (i), 0);
4697 fprintf (file, ": ");
4698 dump_value_range (file, vr_value[i]);
4699 fprintf (file, "\n");
4703 fprintf (file, "\n");
4707 /* Dump all value ranges to stderr. */
4709 DEBUG_FUNCTION void
4710 debug_all_value_ranges (void)
4712 dump_all_value_ranges (stderr);
4716 /* Given a COND_EXPR COND of the form 'V OP W', and an SSA name V,
4717 create a new SSA name N and return the assertion assignment
4718 'N = ASSERT_EXPR <V, V OP W>'. */
4720 static gimple *
4721 build_assert_expr_for (tree cond, tree v)
4723 tree a;
4724 gassign *assertion;
4726 gcc_assert (TREE_CODE (v) == SSA_NAME
4727 && COMPARISON_CLASS_P (cond));
4729 a = build2 (ASSERT_EXPR, TREE_TYPE (v), v, cond);
4730 assertion = gimple_build_assign (NULL_TREE, a);
4732 /* The new ASSERT_EXPR, creates a new SSA name that replaces the
4733 operand of the ASSERT_EXPR. Create it so the new name and the old one
4734 are registered in the replacement table so that we can fix the SSA web
4735 after adding all the ASSERT_EXPRs. */
4736 create_new_def_for (v, assertion, NULL);
4738 return assertion;
4742 /* Return false if EXPR is a predicate expression involving floating
4743 point values. */
4745 static inline bool
4746 fp_predicate (gimple *stmt)
4748 GIMPLE_CHECK (stmt, GIMPLE_COND);
4750 return FLOAT_TYPE_P (TREE_TYPE (gimple_cond_lhs (stmt)));
4753 /* If the range of values taken by OP can be inferred after STMT executes,
4754 return the comparison code (COMP_CODE_P) and value (VAL_P) that
4755 describes the inferred range. Return true if a range could be
4756 inferred. */
4758 static bool
4759 infer_value_range (gimple *stmt, tree op, tree_code *comp_code_p, tree *val_p)
4761 *val_p = NULL_TREE;
4762 *comp_code_p = ERROR_MARK;
4764 /* Do not attempt to infer anything in names that flow through
4765 abnormal edges. */
4766 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op))
4767 return false;
4769 /* Similarly, don't infer anything from statements that may throw
4770 exceptions. ??? Relax this requirement? */
4771 if (stmt_could_throw_p (stmt))
4772 return false;
4774 /* If STMT is the last statement of a basic block with no normal
4775 successors, there is no point inferring anything about any of its
4776 operands. We would not be able to find a proper insertion point
4777 for the assertion, anyway. */
4778 if (stmt_ends_bb_p (stmt))
4780 edge_iterator ei;
4781 edge e;
4783 FOR_EACH_EDGE (e, ei, gimple_bb (stmt)->succs)
4784 if (!(e->flags & EDGE_ABNORMAL))
4785 break;
4786 if (e == NULL)
4787 return false;
4790 if (infer_nonnull_range (stmt, op))
4792 *val_p = build_int_cst (TREE_TYPE (op), 0);
4793 *comp_code_p = NE_EXPR;
4794 return true;
4797 return false;
4801 void dump_asserts_for (FILE *, tree);
4802 void debug_asserts_for (tree);
4803 void dump_all_asserts (FILE *);
4804 void debug_all_asserts (void);
4806 /* Dump all the registered assertions for NAME to FILE. */
4808 void
4809 dump_asserts_for (FILE *file, tree name)
4811 assert_locus *loc;
4813 fprintf (file, "Assertions to be inserted for ");
4814 print_generic_expr (file, name, 0);
4815 fprintf (file, "\n");
4817 loc = asserts_for[SSA_NAME_VERSION (name)];
4818 while (loc)
4820 fprintf (file, "\t");
4821 print_gimple_stmt (file, gsi_stmt (loc->si), 0, 0);
4822 fprintf (file, "\n\tBB #%d", loc->bb->index);
4823 if (loc->e)
4825 fprintf (file, "\n\tEDGE %d->%d", loc->e->src->index,
4826 loc->e->dest->index);
4827 dump_edge_info (file, loc->e, dump_flags, 0);
4829 fprintf (file, "\n\tPREDICATE: ");
4830 print_generic_expr (file, name, 0);
4831 fprintf (file, " %s ", get_tree_code_name (loc->comp_code));
4832 print_generic_expr (file, loc->val, 0);
4833 fprintf (file, "\n\n");
4834 loc = loc->next;
4837 fprintf (file, "\n");
4841 /* Dump all the registered assertions for NAME to stderr. */
4843 DEBUG_FUNCTION void
4844 debug_asserts_for (tree name)
4846 dump_asserts_for (stderr, name);
4850 /* Dump all the registered assertions for all the names to FILE. */
4852 void
4853 dump_all_asserts (FILE *file)
4855 unsigned i;
4856 bitmap_iterator bi;
4858 fprintf (file, "\nASSERT_EXPRs to be inserted\n\n");
4859 EXECUTE_IF_SET_IN_BITMAP (need_assert_for, 0, i, bi)
4860 dump_asserts_for (file, ssa_name (i));
4861 fprintf (file, "\n");
4865 /* Dump all the registered assertions for all the names to stderr. */
4867 DEBUG_FUNCTION void
4868 debug_all_asserts (void)
4870 dump_all_asserts (stderr);
4874 /* If NAME doesn't have an ASSERT_EXPR registered for asserting
4875 'EXPR COMP_CODE VAL' at a location that dominates block BB or
4876 E->DEST, then register this location as a possible insertion point
4877 for ASSERT_EXPR <NAME, EXPR COMP_CODE VAL>.
4879 BB, E and SI provide the exact insertion point for the new
4880 ASSERT_EXPR. If BB is NULL, then the ASSERT_EXPR is to be inserted
4881 on edge E. Otherwise, if E is NULL, the ASSERT_EXPR is inserted on
4882 BB. If SI points to a COND_EXPR or a SWITCH_EXPR statement, then E
4883 must not be NULL. */
4885 static void
4886 register_new_assert_for (tree name, tree expr,
4887 enum tree_code comp_code,
4888 tree val,
4889 basic_block bb,
4890 edge e,
4891 gimple_stmt_iterator si)
4893 assert_locus *n, *loc, *last_loc;
4894 basic_block dest_bb;
4896 gcc_checking_assert (bb == NULL || e == NULL);
4898 if (e == NULL)
4899 gcc_checking_assert (gimple_code (gsi_stmt (si)) != GIMPLE_COND
4900 && gimple_code (gsi_stmt (si)) != GIMPLE_SWITCH);
4902 /* Never build an assert comparing against an integer constant with
4903 TREE_OVERFLOW set. This confuses our undefined overflow warning
4904 machinery. */
4905 if (TREE_OVERFLOW_P (val))
4906 val = drop_tree_overflow (val);
4908 /* The new assertion A will be inserted at BB or E. We need to
4909 determine if the new location is dominated by a previously
4910 registered location for A. If we are doing an edge insertion,
4911 assume that A will be inserted at E->DEST. Note that this is not
4912 necessarily true.
4914 If E is a critical edge, it will be split. But even if E is
4915 split, the new block will dominate the same set of blocks that
4916 E->DEST dominates.
4918 The reverse, however, is not true, blocks dominated by E->DEST
4919 will not be dominated by the new block created to split E. So,
4920 if the insertion location is on a critical edge, we will not use
4921 the new location to move another assertion previously registered
4922 at a block dominated by E->DEST. */
4923 dest_bb = (bb) ? bb : e->dest;
4925 /* If NAME already has an ASSERT_EXPR registered for COMP_CODE and
4926 VAL at a block dominating DEST_BB, then we don't need to insert a new
4927 one. Similarly, if the same assertion already exists at a block
4928 dominated by DEST_BB and the new location is not on a critical
4929 edge, then update the existing location for the assertion (i.e.,
4930 move the assertion up in the dominance tree).
4932 Note, this is implemented as a simple linked list because there
4933 should not be more than a handful of assertions registered per
4934 name. If this becomes a performance problem, a table hashed by
4935 COMP_CODE and VAL could be implemented. */
4936 loc = asserts_for[SSA_NAME_VERSION (name)];
4937 last_loc = loc;
4938 while (loc)
4940 if (loc->comp_code == comp_code
4941 && (loc->val == val
4942 || operand_equal_p (loc->val, val, 0))
4943 && (loc->expr == expr
4944 || operand_equal_p (loc->expr, expr, 0)))
4946 /* If E is not a critical edge and DEST_BB
4947 dominates the existing location for the assertion, move
4948 the assertion up in the dominance tree by updating its
4949 location information. */
4950 if ((e == NULL || !EDGE_CRITICAL_P (e))
4951 && dominated_by_p (CDI_DOMINATORS, loc->bb, dest_bb))
4953 loc->bb = dest_bb;
4954 loc->e = e;
4955 loc->si = si;
4956 return;
4960 /* Update the last node of the list and move to the next one. */
4961 last_loc = loc;
4962 loc = loc->next;
4965 /* If we didn't find an assertion already registered for
4966 NAME COMP_CODE VAL, add a new one at the end of the list of
4967 assertions associated with NAME. */
4968 n = XNEW (struct assert_locus);
4969 n->bb = dest_bb;
4970 n->e = e;
4971 n->si = si;
4972 n->comp_code = comp_code;
4973 n->val = val;
4974 n->expr = expr;
4975 n->next = NULL;
4977 if (last_loc)
4978 last_loc->next = n;
4979 else
4980 asserts_for[SSA_NAME_VERSION (name)] = n;
4982 bitmap_set_bit (need_assert_for, SSA_NAME_VERSION (name));
4985 /* (COND_OP0 COND_CODE COND_OP1) is a predicate which uses NAME.
4986 Extract a suitable test code and value and store them into *CODE_P and
4987 *VAL_P so the predicate is normalized to NAME *CODE_P *VAL_P.
4989 If no extraction was possible, return FALSE, otherwise return TRUE.
4991 If INVERT is true, then we invert the result stored into *CODE_P. */
4993 static bool
4994 extract_code_and_val_from_cond_with_ops (tree name, enum tree_code cond_code,
4995 tree cond_op0, tree cond_op1,
4996 bool invert, enum tree_code *code_p,
4997 tree *val_p)
4999 enum tree_code comp_code;
5000 tree val;
5002 /* Otherwise, we have a comparison of the form NAME COMP VAL
5003 or VAL COMP NAME. */
5004 if (name == cond_op1)
5006 /* If the predicate is of the form VAL COMP NAME, flip
5007 COMP around because we need to register NAME as the
5008 first operand in the predicate. */
5009 comp_code = swap_tree_comparison (cond_code);
5010 val = cond_op0;
5012 else
5014 /* The comparison is of the form NAME COMP VAL, so the
5015 comparison code remains unchanged. */
5016 comp_code = cond_code;
5017 val = cond_op1;
5020 /* Invert the comparison code as necessary. */
5021 if (invert)
5022 comp_code = invert_tree_comparison (comp_code, 0);
5024 /* VRP only handles integral and pointer types. */
5025 if (! INTEGRAL_TYPE_P (TREE_TYPE (val))
5026 && ! POINTER_TYPE_P (TREE_TYPE (val)))
5027 return false;
5029 /* Do not register always-false predicates.
5030 FIXME: this works around a limitation in fold() when dealing with
5031 enumerations. Given 'enum { N1, N2 } x;', fold will not
5032 fold 'if (x > N2)' to 'if (0)'. */
5033 if ((comp_code == GT_EXPR || comp_code == LT_EXPR)
5034 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
5036 tree min = TYPE_MIN_VALUE (TREE_TYPE (val));
5037 tree max = TYPE_MAX_VALUE (TREE_TYPE (val));
5039 if (comp_code == GT_EXPR
5040 && (!max
5041 || compare_values (val, max) == 0))
5042 return false;
5044 if (comp_code == LT_EXPR
5045 && (!min
5046 || compare_values (val, min) == 0))
5047 return false;
5049 *code_p = comp_code;
5050 *val_p = val;
5051 return true;
5054 /* Find out smallest RES where RES > VAL && (RES & MASK) == RES, if any
5055 (otherwise return VAL). VAL and MASK must be zero-extended for
5056 precision PREC. If SGNBIT is non-zero, first xor VAL with SGNBIT
5057 (to transform signed values into unsigned) and at the end xor
5058 SGNBIT back. */
5060 static wide_int
5061 masked_increment (const wide_int &val_in, const wide_int &mask,
5062 const wide_int &sgnbit, unsigned int prec)
5064 wide_int bit = wi::one (prec), res;
5065 unsigned int i;
5067 wide_int val = val_in ^ sgnbit;
5068 for (i = 0; i < prec; i++, bit += bit)
5070 res = mask;
5071 if ((res & bit) == 0)
5072 continue;
5073 res = bit - 1;
5074 res = (val + bit).and_not (res);
5075 res &= mask;
5076 if (wi::gtu_p (res, val))
5077 return res ^ sgnbit;
5079 return val ^ sgnbit;
5082 /* Try to register an edge assertion for SSA name NAME on edge E for
5083 the condition COND contributing to the conditional jump pointed to by BSI.
5084 Invert the condition COND if INVERT is true. */
5086 static void
5087 register_edge_assert_for_2 (tree name, edge e, gimple_stmt_iterator bsi,
5088 enum tree_code cond_code,
5089 tree cond_op0, tree cond_op1, bool invert)
5091 tree val;
5092 enum tree_code comp_code;
5094 if (!extract_code_and_val_from_cond_with_ops (name, cond_code,
5095 cond_op0,
5096 cond_op1,
5097 invert, &comp_code, &val))
5098 return;
5100 /* Only register an ASSERT_EXPR if NAME was found in the sub-graph
5101 reachable from E. */
5102 if (live_on_edge (e, name))
5103 register_new_assert_for (name, name, comp_code, val, NULL, e, bsi);
5105 /* In the case of NAME <= CST and NAME being defined as
5106 NAME = (unsigned) NAME2 + CST2 we can assert NAME2 >= -CST2
5107 and NAME2 <= CST - CST2. We can do the same for NAME > CST.
5108 This catches range and anti-range tests. */
5109 if ((comp_code == LE_EXPR
5110 || comp_code == GT_EXPR)
5111 && TREE_CODE (val) == INTEGER_CST
5112 && TYPE_UNSIGNED (TREE_TYPE (val)))
5114 gimple *def_stmt = SSA_NAME_DEF_STMT (name);
5115 tree cst2 = NULL_TREE, name2 = NULL_TREE, name3 = NULL_TREE;
5117 /* Extract CST2 from the (optional) addition. */
5118 if (is_gimple_assign (def_stmt)
5119 && gimple_assign_rhs_code (def_stmt) == PLUS_EXPR)
5121 name2 = gimple_assign_rhs1 (def_stmt);
5122 cst2 = gimple_assign_rhs2 (def_stmt);
5123 if (TREE_CODE (name2) == SSA_NAME
5124 && TREE_CODE (cst2) == INTEGER_CST)
5125 def_stmt = SSA_NAME_DEF_STMT (name2);
5128 /* Extract NAME2 from the (optional) sign-changing cast. */
5129 if (gimple_assign_cast_p (def_stmt))
5131 if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt))
5132 && ! TYPE_UNSIGNED (TREE_TYPE (gimple_assign_rhs1 (def_stmt)))
5133 && (TYPE_PRECISION (gimple_expr_type (def_stmt))
5134 == TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (def_stmt)))))
5135 name3 = gimple_assign_rhs1 (def_stmt);
5138 /* If name3 is used later, create an ASSERT_EXPR for it. */
5139 if (name3 != NULL_TREE
5140 && TREE_CODE (name3) == SSA_NAME
5141 && (cst2 == NULL_TREE
5142 || TREE_CODE (cst2) == INTEGER_CST)
5143 && INTEGRAL_TYPE_P (TREE_TYPE (name3))
5144 && live_on_edge (e, name3))
5146 tree tmp;
5148 /* Build an expression for the range test. */
5149 tmp = build1 (NOP_EXPR, TREE_TYPE (name), name3);
5150 if (cst2 != NULL_TREE)
5151 tmp = build2 (PLUS_EXPR, TREE_TYPE (name), tmp, cst2);
5153 if (dump_file)
5155 fprintf (dump_file, "Adding assert for ");
5156 print_generic_expr (dump_file, name3, 0);
5157 fprintf (dump_file, " from ");
5158 print_generic_expr (dump_file, tmp, 0);
5159 fprintf (dump_file, "\n");
5162 register_new_assert_for (name3, tmp, comp_code, val, NULL, e, bsi);
5165 /* If name2 is used later, create an ASSERT_EXPR for it. */
5166 if (name2 != NULL_TREE
5167 && TREE_CODE (name2) == SSA_NAME
5168 && TREE_CODE (cst2) == INTEGER_CST
5169 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
5170 && live_on_edge (e, name2))
5172 tree tmp;
5174 /* Build an expression for the range test. */
5175 tmp = name2;
5176 if (TREE_TYPE (name) != TREE_TYPE (name2))
5177 tmp = build1 (NOP_EXPR, TREE_TYPE (name), tmp);
5178 if (cst2 != NULL_TREE)
5179 tmp = build2 (PLUS_EXPR, TREE_TYPE (name), tmp, cst2);
5181 if (dump_file)
5183 fprintf (dump_file, "Adding assert for ");
5184 print_generic_expr (dump_file, name2, 0);
5185 fprintf (dump_file, " from ");
5186 print_generic_expr (dump_file, tmp, 0);
5187 fprintf (dump_file, "\n");
5190 register_new_assert_for (name2, tmp, comp_code, val, NULL, e, bsi);
5194 /* In the case of post-in/decrement tests like if (i++) ... and uses
5195 of the in/decremented value on the edge the extra name we want to
5196 assert for is not on the def chain of the name compared. Instead
5197 it is in the set of use stmts.
5198 Similar cases happen for conversions that were simplified through
5199 fold_{sign_changed,widened}_comparison. */
5200 if ((comp_code == NE_EXPR
5201 || comp_code == EQ_EXPR)
5202 && TREE_CODE (val) == INTEGER_CST)
5204 imm_use_iterator ui;
5205 gimple *use_stmt;
5206 FOR_EACH_IMM_USE_STMT (use_stmt, ui, name)
5208 if (!is_gimple_assign (use_stmt))
5209 continue;
5211 /* Cut off to use-stmts that are dominating the predecessor. */
5212 if (!dominated_by_p (CDI_DOMINATORS, e->src, gimple_bb (use_stmt)))
5213 continue;
5215 tree name2 = gimple_assign_lhs (use_stmt);
5216 if (TREE_CODE (name2) != SSA_NAME
5217 || !live_on_edge (e, name2))
5218 continue;
5220 enum tree_code code = gimple_assign_rhs_code (use_stmt);
5221 tree cst;
5222 if (code == PLUS_EXPR
5223 || code == MINUS_EXPR)
5225 cst = gimple_assign_rhs2 (use_stmt);
5226 if (TREE_CODE (cst) != INTEGER_CST)
5227 continue;
5228 cst = int_const_binop (code, val, cst);
5230 else if (CONVERT_EXPR_CODE_P (code))
5232 /* For truncating conversions we cannot record
5233 an inequality. */
5234 if (comp_code == NE_EXPR
5235 && (TYPE_PRECISION (TREE_TYPE (name2))
5236 < TYPE_PRECISION (TREE_TYPE (name))))
5237 continue;
5238 cst = fold_convert (TREE_TYPE (name2), val);
5240 else
5241 continue;
5243 if (TREE_OVERFLOW_P (cst))
5244 cst = drop_tree_overflow (cst);
5245 register_new_assert_for (name2, name2, comp_code, cst,
5246 NULL, e, bsi);
5250 if (TREE_CODE_CLASS (comp_code) == tcc_comparison
5251 && TREE_CODE (val) == INTEGER_CST)
5253 gimple *def_stmt = SSA_NAME_DEF_STMT (name);
5254 tree name2 = NULL_TREE, names[2], cst2 = NULL_TREE;
5255 tree val2 = NULL_TREE;
5256 unsigned int prec = TYPE_PRECISION (TREE_TYPE (val));
5257 wide_int mask = wi::zero (prec);
5258 unsigned int nprec = prec;
5259 enum tree_code rhs_code = ERROR_MARK;
5261 if (is_gimple_assign (def_stmt))
5262 rhs_code = gimple_assign_rhs_code (def_stmt);
5264 /* In the case of NAME != CST1 where NAME = A +- CST2 we can
5265 assert that A != CST1 -+ CST2. */
5266 if ((comp_code == EQ_EXPR || comp_code == NE_EXPR)
5267 && (rhs_code == PLUS_EXPR || rhs_code == MINUS_EXPR))
5269 tree op0 = gimple_assign_rhs1 (def_stmt);
5270 tree op1 = gimple_assign_rhs2 (def_stmt);
5271 if (TREE_CODE (op0) == SSA_NAME
5272 && TREE_CODE (op1) == INTEGER_CST
5273 && live_on_edge (e, op0))
5275 enum tree_code reverse_op = (rhs_code == PLUS_EXPR
5276 ? MINUS_EXPR : PLUS_EXPR);
5277 op1 = int_const_binop (reverse_op, val, op1);
5278 if (TREE_OVERFLOW (op1))
5279 op1 = drop_tree_overflow (op1);
5280 register_new_assert_for (op0, op0, comp_code, op1, NULL, e, bsi);
5284 /* Add asserts for NAME cmp CST and NAME being defined
5285 as NAME = (int) NAME2. */
5286 if (!TYPE_UNSIGNED (TREE_TYPE (val))
5287 && (comp_code == LE_EXPR || comp_code == LT_EXPR
5288 || comp_code == GT_EXPR || comp_code == GE_EXPR)
5289 && gimple_assign_cast_p (def_stmt))
5291 name2 = gimple_assign_rhs1 (def_stmt);
5292 if (CONVERT_EXPR_CODE_P (rhs_code)
5293 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
5294 && TYPE_UNSIGNED (TREE_TYPE (name2))
5295 && prec == TYPE_PRECISION (TREE_TYPE (name2))
5296 && (comp_code == LE_EXPR || comp_code == GT_EXPR
5297 || !tree_int_cst_equal (val,
5298 TYPE_MIN_VALUE (TREE_TYPE (val))))
5299 && live_on_edge (e, name2))
5301 tree tmp, cst;
5302 enum tree_code new_comp_code = comp_code;
5304 cst = fold_convert (TREE_TYPE (name2),
5305 TYPE_MIN_VALUE (TREE_TYPE (val)));
5306 /* Build an expression for the range test. */
5307 tmp = build2 (PLUS_EXPR, TREE_TYPE (name2), name2, cst);
5308 cst = fold_build2 (PLUS_EXPR, TREE_TYPE (name2), cst,
5309 fold_convert (TREE_TYPE (name2), val));
5310 if (comp_code == LT_EXPR || comp_code == GE_EXPR)
5312 new_comp_code = comp_code == LT_EXPR ? LE_EXPR : GT_EXPR;
5313 cst = fold_build2 (MINUS_EXPR, TREE_TYPE (name2), cst,
5314 build_int_cst (TREE_TYPE (name2), 1));
5317 if (dump_file)
5319 fprintf (dump_file, "Adding assert for ");
5320 print_generic_expr (dump_file, name2, 0);
5321 fprintf (dump_file, " from ");
5322 print_generic_expr (dump_file, tmp, 0);
5323 fprintf (dump_file, "\n");
5326 register_new_assert_for (name2, tmp, new_comp_code, cst, NULL,
5327 e, bsi);
5331 /* Add asserts for NAME cmp CST and NAME being defined as
5332 NAME = NAME2 >> CST2.
5334 Extract CST2 from the right shift. */
5335 if (rhs_code == RSHIFT_EXPR)
5337 name2 = gimple_assign_rhs1 (def_stmt);
5338 cst2 = gimple_assign_rhs2 (def_stmt);
5339 if (TREE_CODE (name2) == SSA_NAME
5340 && tree_fits_uhwi_p (cst2)
5341 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
5342 && IN_RANGE (tree_to_uhwi (cst2), 1, prec - 1)
5343 && prec == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (val)))
5344 && live_on_edge (e, name2))
5346 mask = wi::mask (tree_to_uhwi (cst2), false, prec);
5347 val2 = fold_binary (LSHIFT_EXPR, TREE_TYPE (val), val, cst2);
5350 if (val2 != NULL_TREE
5351 && TREE_CODE (val2) == INTEGER_CST
5352 && simple_cst_equal (fold_build2 (RSHIFT_EXPR,
5353 TREE_TYPE (val),
5354 val2, cst2), val))
5356 enum tree_code new_comp_code = comp_code;
5357 tree tmp, new_val;
5359 tmp = name2;
5360 if (comp_code == EQ_EXPR || comp_code == NE_EXPR)
5362 if (!TYPE_UNSIGNED (TREE_TYPE (val)))
5364 tree type = build_nonstandard_integer_type (prec, 1);
5365 tmp = build1 (NOP_EXPR, type, name2);
5366 val2 = fold_convert (type, val2);
5368 tmp = fold_build2 (MINUS_EXPR, TREE_TYPE (tmp), tmp, val2);
5369 new_val = wide_int_to_tree (TREE_TYPE (tmp), mask);
5370 new_comp_code = comp_code == EQ_EXPR ? LE_EXPR : GT_EXPR;
5372 else if (comp_code == LT_EXPR || comp_code == GE_EXPR)
5374 wide_int minval
5375 = wi::min_value (prec, TYPE_SIGN (TREE_TYPE (val)));
5376 new_val = val2;
5377 if (minval == new_val)
5378 new_val = NULL_TREE;
5380 else
5382 wide_int maxval
5383 = wi::max_value (prec, TYPE_SIGN (TREE_TYPE (val)));
5384 mask |= val2;
5385 if (mask == maxval)
5386 new_val = NULL_TREE;
5387 else
5388 new_val = wide_int_to_tree (TREE_TYPE (val2), mask);
5391 if (new_val)
5393 if (dump_file)
5395 fprintf (dump_file, "Adding assert for ");
5396 print_generic_expr (dump_file, name2, 0);
5397 fprintf (dump_file, " from ");
5398 print_generic_expr (dump_file, tmp, 0);
5399 fprintf (dump_file, "\n");
5402 register_new_assert_for (name2, tmp, new_comp_code, new_val,
5403 NULL, e, bsi);
5407 /* Add asserts for NAME cmp CST and NAME being defined as
5408 NAME = NAME2 & CST2.
5410 Extract CST2 from the and.
5412 Also handle
5413 NAME = (unsigned) NAME2;
5414 casts where NAME's type is unsigned and has smaller precision
5415 than NAME2's type as if it was NAME = NAME2 & MASK. */
5416 names[0] = NULL_TREE;
5417 names[1] = NULL_TREE;
5418 cst2 = NULL_TREE;
5419 if (rhs_code == BIT_AND_EXPR
5420 || (CONVERT_EXPR_CODE_P (rhs_code)
5421 && INTEGRAL_TYPE_P (TREE_TYPE (val))
5422 && TYPE_UNSIGNED (TREE_TYPE (val))
5423 && TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (def_stmt)))
5424 > prec))
5426 name2 = gimple_assign_rhs1 (def_stmt);
5427 if (rhs_code == BIT_AND_EXPR)
5428 cst2 = gimple_assign_rhs2 (def_stmt);
5429 else
5431 cst2 = TYPE_MAX_VALUE (TREE_TYPE (val));
5432 nprec = TYPE_PRECISION (TREE_TYPE (name2));
5434 if (TREE_CODE (name2) == SSA_NAME
5435 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
5436 && TREE_CODE (cst2) == INTEGER_CST
5437 && !integer_zerop (cst2)
5438 && (nprec > 1
5439 || TYPE_UNSIGNED (TREE_TYPE (val))))
5441 gimple *def_stmt2 = SSA_NAME_DEF_STMT (name2);
5442 if (gimple_assign_cast_p (def_stmt2))
5444 names[1] = gimple_assign_rhs1 (def_stmt2);
5445 if (!CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt2))
5446 || !INTEGRAL_TYPE_P (TREE_TYPE (names[1]))
5447 || (TYPE_PRECISION (TREE_TYPE (name2))
5448 != TYPE_PRECISION (TREE_TYPE (names[1])))
5449 || !live_on_edge (e, names[1]))
5450 names[1] = NULL_TREE;
5452 if (live_on_edge (e, name2))
5453 names[0] = name2;
5456 if (names[0] || names[1])
5458 wide_int minv, maxv, valv, cst2v;
5459 wide_int tem, sgnbit;
5460 bool valid_p = false, valn, cst2n;
5461 enum tree_code ccode = comp_code;
5463 valv = wide_int::from (val, nprec, UNSIGNED);
5464 cst2v = wide_int::from (cst2, nprec, UNSIGNED);
5465 valn = wi::neg_p (valv, TYPE_SIGN (TREE_TYPE (val)));
5466 cst2n = wi::neg_p (cst2v, TYPE_SIGN (TREE_TYPE (val)));
5467 /* If CST2 doesn't have most significant bit set,
5468 but VAL is negative, we have comparison like
5469 if ((x & 0x123) > -4) (always true). Just give up. */
5470 if (!cst2n && valn)
5471 ccode = ERROR_MARK;
5472 if (cst2n)
5473 sgnbit = wi::set_bit_in_zero (nprec - 1, nprec);
5474 else
5475 sgnbit = wi::zero (nprec);
5476 minv = valv & cst2v;
5477 switch (ccode)
5479 case EQ_EXPR:
5480 /* Minimum unsigned value for equality is VAL & CST2
5481 (should be equal to VAL, otherwise we probably should
5482 have folded the comparison into false) and
5483 maximum unsigned value is VAL | ~CST2. */
5484 maxv = valv | ~cst2v;
5485 valid_p = true;
5486 break;
5488 case NE_EXPR:
5489 tem = valv | ~cst2v;
5490 /* If VAL is 0, handle (X & CST2) != 0 as (X & CST2) > 0U. */
5491 if (valv == 0)
5493 cst2n = false;
5494 sgnbit = wi::zero (nprec);
5495 goto gt_expr;
5497 /* If (VAL | ~CST2) is all ones, handle it as
5498 (X & CST2) < VAL. */
5499 if (tem == -1)
5501 cst2n = false;
5502 valn = false;
5503 sgnbit = wi::zero (nprec);
5504 goto lt_expr;
5506 if (!cst2n && wi::neg_p (cst2v))
5507 sgnbit = wi::set_bit_in_zero (nprec - 1, nprec);
5508 if (sgnbit != 0)
5510 if (valv == sgnbit)
5512 cst2n = true;
5513 valn = true;
5514 goto gt_expr;
5516 if (tem == wi::mask (nprec - 1, false, nprec))
5518 cst2n = true;
5519 goto lt_expr;
5521 if (!cst2n)
5522 sgnbit = wi::zero (nprec);
5524 break;
5526 case GE_EXPR:
5527 /* Minimum unsigned value for >= if (VAL & CST2) == VAL
5528 is VAL and maximum unsigned value is ~0. For signed
5529 comparison, if CST2 doesn't have most significant bit
5530 set, handle it similarly. If CST2 has MSB set,
5531 the minimum is the same, and maximum is ~0U/2. */
5532 if (minv != valv)
5534 /* If (VAL & CST2) != VAL, X & CST2 can't be equal to
5535 VAL. */
5536 minv = masked_increment (valv, cst2v, sgnbit, nprec);
5537 if (minv == valv)
5538 break;
5540 maxv = wi::mask (nprec - (cst2n ? 1 : 0), false, nprec);
5541 valid_p = true;
5542 break;
5544 case GT_EXPR:
5545 gt_expr:
5546 /* Find out smallest MINV where MINV > VAL
5547 && (MINV & CST2) == MINV, if any. If VAL is signed and
5548 CST2 has MSB set, compute it biased by 1 << (nprec - 1). */
5549 minv = masked_increment (valv, cst2v, sgnbit, nprec);
5550 if (minv == valv)
5551 break;
5552 maxv = wi::mask (nprec - (cst2n ? 1 : 0), false, nprec);
5553 valid_p = true;
5554 break;
5556 case LE_EXPR:
5557 /* Minimum unsigned value for <= is 0 and maximum
5558 unsigned value is VAL | ~CST2 if (VAL & CST2) == VAL.
5559 Otherwise, find smallest VAL2 where VAL2 > VAL
5560 && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
5561 as maximum.
5562 For signed comparison, if CST2 doesn't have most
5563 significant bit set, handle it similarly. If CST2 has
5564 MSB set, the maximum is the same and minimum is INT_MIN. */
5565 if (minv == valv)
5566 maxv = valv;
5567 else
5569 maxv = masked_increment (valv, cst2v, sgnbit, nprec);
5570 if (maxv == valv)
5571 break;
5572 maxv -= 1;
5574 maxv |= ~cst2v;
5575 minv = sgnbit;
5576 valid_p = true;
5577 break;
5579 case LT_EXPR:
5580 lt_expr:
5581 /* Minimum unsigned value for < is 0 and maximum
5582 unsigned value is (VAL-1) | ~CST2 if (VAL & CST2) == VAL.
5583 Otherwise, find smallest VAL2 where VAL2 > VAL
5584 && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
5585 as maximum.
5586 For signed comparison, if CST2 doesn't have most
5587 significant bit set, handle it similarly. If CST2 has
5588 MSB set, the maximum is the same and minimum is INT_MIN. */
5589 if (minv == valv)
5591 if (valv == sgnbit)
5592 break;
5593 maxv = valv;
5595 else
5597 maxv = masked_increment (valv, cst2v, sgnbit, nprec);
5598 if (maxv == valv)
5599 break;
5601 maxv -= 1;
5602 maxv |= ~cst2v;
5603 minv = sgnbit;
5604 valid_p = true;
5605 break;
5607 default:
5608 break;
5610 if (valid_p
5611 && (maxv - minv) != -1)
5613 tree tmp, new_val, type;
5614 int i;
5616 for (i = 0; i < 2; i++)
5617 if (names[i])
5619 wide_int maxv2 = maxv;
5620 tmp = names[i];
5621 type = TREE_TYPE (names[i]);
5622 if (!TYPE_UNSIGNED (type))
5624 type = build_nonstandard_integer_type (nprec, 1);
5625 tmp = build1 (NOP_EXPR, type, names[i]);
5627 if (minv != 0)
5629 tmp = build2 (PLUS_EXPR, type, tmp,
5630 wide_int_to_tree (type, -minv));
5631 maxv2 = maxv - minv;
5633 new_val = wide_int_to_tree (type, maxv2);
5635 if (dump_file)
5637 fprintf (dump_file, "Adding assert for ");
5638 print_generic_expr (dump_file, names[i], 0);
5639 fprintf (dump_file, " from ");
5640 print_generic_expr (dump_file, tmp, 0);
5641 fprintf (dump_file, "\n");
5644 register_new_assert_for (names[i], tmp, LE_EXPR,
5645 new_val, NULL, e, bsi);
5652 /* OP is an operand of a truth value expression which is known to have
5653 a particular value. Register any asserts for OP and for any
5654 operands in OP's defining statement.
5656 If CODE is EQ_EXPR, then we want to register OP is zero (false),
5657 if CODE is NE_EXPR, then we want to register OP is nonzero (true). */
5659 static void
5660 register_edge_assert_for_1 (tree op, enum tree_code code,
5661 edge e, gimple_stmt_iterator bsi)
5663 gimple *op_def;
5664 tree val;
5665 enum tree_code rhs_code;
5667 /* We only care about SSA_NAMEs. */
5668 if (TREE_CODE (op) != SSA_NAME)
5669 return;
5671 /* We know that OP will have a zero or nonzero value. If OP is used
5672 more than once go ahead and register an assert for OP. */
5673 if (live_on_edge (e, op))
5675 val = build_int_cst (TREE_TYPE (op), 0);
5676 register_new_assert_for (op, op, code, val, NULL, e, bsi);
5679 /* Now look at how OP is set. If it's set from a comparison,
5680 a truth operation or some bit operations, then we may be able
5681 to register information about the operands of that assignment. */
5682 op_def = SSA_NAME_DEF_STMT (op);
5683 if (gimple_code (op_def) != GIMPLE_ASSIGN)
5684 return;
5686 rhs_code = gimple_assign_rhs_code (op_def);
5688 if (TREE_CODE_CLASS (rhs_code) == tcc_comparison)
5690 bool invert = (code == EQ_EXPR ? true : false);
5691 tree op0 = gimple_assign_rhs1 (op_def);
5692 tree op1 = gimple_assign_rhs2 (op_def);
5694 if (TREE_CODE (op0) == SSA_NAME)
5695 register_edge_assert_for_2 (op0, e, bsi, rhs_code, op0, op1, invert);
5696 if (TREE_CODE (op1) == SSA_NAME)
5697 register_edge_assert_for_2 (op1, e, bsi, rhs_code, op0, op1, invert);
5699 else if ((code == NE_EXPR
5700 && gimple_assign_rhs_code (op_def) == BIT_AND_EXPR)
5701 || (code == EQ_EXPR
5702 && gimple_assign_rhs_code (op_def) == BIT_IOR_EXPR))
5704 /* Recurse on each operand. */
5705 tree op0 = gimple_assign_rhs1 (op_def);
5706 tree op1 = gimple_assign_rhs2 (op_def);
5707 if (TREE_CODE (op0) == SSA_NAME
5708 && has_single_use (op0))
5709 register_edge_assert_for_1 (op0, code, e, bsi);
5710 if (TREE_CODE (op1) == SSA_NAME
5711 && has_single_use (op1))
5712 register_edge_assert_for_1 (op1, code, e, bsi);
5714 else if (gimple_assign_rhs_code (op_def) == BIT_NOT_EXPR
5715 && TYPE_PRECISION (TREE_TYPE (gimple_assign_lhs (op_def))) == 1)
5717 /* Recurse, flipping CODE. */
5718 code = invert_tree_comparison (code, false);
5719 register_edge_assert_for_1 (gimple_assign_rhs1 (op_def), code, e, bsi);
5721 else if (gimple_assign_rhs_code (op_def) == SSA_NAME)
5723 /* Recurse through the copy. */
5724 register_edge_assert_for_1 (gimple_assign_rhs1 (op_def), code, e, bsi);
5726 else if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (op_def)))
5728 /* Recurse through the type conversion, unless it is a narrowing
5729 conversion or conversion from non-integral type. */
5730 tree rhs = gimple_assign_rhs1 (op_def);
5731 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs))
5732 && (TYPE_PRECISION (TREE_TYPE (rhs))
5733 <= TYPE_PRECISION (TREE_TYPE (op))))
5734 register_edge_assert_for_1 (rhs, code, e, bsi);
5738 /* Try to register an edge assertion for SSA name NAME on edge E for
5739 the condition COND contributing to the conditional jump pointed to by
5740 SI. */
5742 static void
5743 register_edge_assert_for (tree name, edge e, gimple_stmt_iterator si,
5744 enum tree_code cond_code, tree cond_op0,
5745 tree cond_op1)
5747 tree val;
5748 enum tree_code comp_code;
5749 bool is_else_edge = (e->flags & EDGE_FALSE_VALUE) != 0;
5751 /* Do not attempt to infer anything in names that flow through
5752 abnormal edges. */
5753 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name))
5754 return;
5756 if (!extract_code_and_val_from_cond_with_ops (name, cond_code,
5757 cond_op0, cond_op1,
5758 is_else_edge,
5759 &comp_code, &val))
5760 return;
5762 /* Register ASSERT_EXPRs for name. */
5763 register_edge_assert_for_2 (name, e, si, cond_code, cond_op0,
5764 cond_op1, is_else_edge);
5767 /* If COND is effectively an equality test of an SSA_NAME against
5768 the value zero or one, then we may be able to assert values
5769 for SSA_NAMEs which flow into COND. */
5771 /* In the case of NAME == 1 or NAME != 0, for BIT_AND_EXPR defining
5772 statement of NAME we can assert both operands of the BIT_AND_EXPR
5773 have nonzero value. */
5774 if (((comp_code == EQ_EXPR && integer_onep (val))
5775 || (comp_code == NE_EXPR && integer_zerop (val))))
5777 gimple *def_stmt = SSA_NAME_DEF_STMT (name);
5779 if (is_gimple_assign (def_stmt)
5780 && gimple_assign_rhs_code (def_stmt) == BIT_AND_EXPR)
5782 tree op0 = gimple_assign_rhs1 (def_stmt);
5783 tree op1 = gimple_assign_rhs2 (def_stmt);
5784 register_edge_assert_for_1 (op0, NE_EXPR, e, si);
5785 register_edge_assert_for_1 (op1, NE_EXPR, e, si);
5789 /* In the case of NAME == 0 or NAME != 1, for BIT_IOR_EXPR defining
5790 statement of NAME we can assert both operands of the BIT_IOR_EXPR
5791 have zero value. */
5792 if (((comp_code == EQ_EXPR && integer_zerop (val))
5793 || (comp_code == NE_EXPR && integer_onep (val))))
5795 gimple *def_stmt = SSA_NAME_DEF_STMT (name);
5797 /* For BIT_IOR_EXPR only if NAME == 0 both operands have
5798 necessarily zero value, or if type-precision is one. */
5799 if (is_gimple_assign (def_stmt)
5800 && (gimple_assign_rhs_code (def_stmt) == BIT_IOR_EXPR
5801 && (TYPE_PRECISION (TREE_TYPE (name)) == 1
5802 || comp_code == EQ_EXPR)))
5804 tree op0 = gimple_assign_rhs1 (def_stmt);
5805 tree op1 = gimple_assign_rhs2 (def_stmt);
5806 register_edge_assert_for_1 (op0, EQ_EXPR, e, si);
5807 register_edge_assert_for_1 (op1, EQ_EXPR, e, si);
5813 /* Determine whether the outgoing edges of BB should receive an
5814 ASSERT_EXPR for each of the operands of BB's LAST statement.
5815 The last statement of BB must be a COND_EXPR.
5817 If any of the sub-graphs rooted at BB have an interesting use of
5818 the predicate operands, an assert location node is added to the
5819 list of assertions for the corresponding operands. */
5821 static void
5822 find_conditional_asserts (basic_block bb, gcond *last)
5824 gimple_stmt_iterator bsi;
5825 tree op;
5826 edge_iterator ei;
5827 edge e;
5828 ssa_op_iter iter;
5830 bsi = gsi_for_stmt (last);
5832 /* Look for uses of the operands in each of the sub-graphs
5833 rooted at BB. We need to check each of the outgoing edges
5834 separately, so that we know what kind of ASSERT_EXPR to
5835 insert. */
5836 FOR_EACH_EDGE (e, ei, bb->succs)
5838 if (e->dest == bb)
5839 continue;
5841 /* Register the necessary assertions for each operand in the
5842 conditional predicate. */
5843 FOR_EACH_SSA_TREE_OPERAND (op, last, iter, SSA_OP_USE)
5844 register_edge_assert_for (op, e, bsi,
5845 gimple_cond_code (last),
5846 gimple_cond_lhs (last),
5847 gimple_cond_rhs (last));
5851 struct case_info
5853 tree expr;
5854 basic_block bb;
5857 /* Compare two case labels sorting first by the destination bb index
5858 and then by the case value. */
5860 static int
5861 compare_case_labels (const void *p1, const void *p2)
5863 const struct case_info *ci1 = (const struct case_info *) p1;
5864 const struct case_info *ci2 = (const struct case_info *) p2;
5865 int idx1 = ci1->bb->index;
5866 int idx2 = ci2->bb->index;
5868 if (idx1 < idx2)
5869 return -1;
5870 else if (idx1 == idx2)
5872 /* Make sure the default label is first in a group. */
5873 if (!CASE_LOW (ci1->expr))
5874 return -1;
5875 else if (!CASE_LOW (ci2->expr))
5876 return 1;
5877 else
5878 return tree_int_cst_compare (CASE_LOW (ci1->expr),
5879 CASE_LOW (ci2->expr));
5881 else
5882 return 1;
5885 /* Determine whether the outgoing edges of BB should receive an
5886 ASSERT_EXPR for each of the operands of BB's LAST statement.
5887 The last statement of BB must be a SWITCH_EXPR.
5889 If any of the sub-graphs rooted at BB have an interesting use of
5890 the predicate operands, an assert location node is added to the
5891 list of assertions for the corresponding operands. */
5893 static void
5894 find_switch_asserts (basic_block bb, gswitch *last)
5896 gimple_stmt_iterator bsi;
5897 tree op;
5898 edge e;
5899 struct case_info *ci;
5900 size_t n = gimple_switch_num_labels (last);
5901 #if GCC_VERSION >= 4000
5902 unsigned int idx;
5903 #else
5904 /* Work around GCC 3.4 bug (PR 37086). */
5905 volatile unsigned int idx;
5906 #endif
5908 bsi = gsi_for_stmt (last);
5909 op = gimple_switch_index (last);
5910 if (TREE_CODE (op) != SSA_NAME)
5911 return;
5913 /* Build a vector of case labels sorted by destination label. */
5914 ci = XNEWVEC (struct case_info, n);
5915 for (idx = 0; idx < n; ++idx)
5917 ci[idx].expr = gimple_switch_label (last, idx);
5918 ci[idx].bb = label_to_block (CASE_LABEL (ci[idx].expr));
5920 qsort (ci, n, sizeof (struct case_info), compare_case_labels);
5922 for (idx = 0; idx < n; ++idx)
5924 tree min, max;
5925 tree cl = ci[idx].expr;
5926 basic_block cbb = ci[idx].bb;
5928 min = CASE_LOW (cl);
5929 max = CASE_HIGH (cl);
5931 /* If there are multiple case labels with the same destination
5932 we need to combine them to a single value range for the edge. */
5933 if (idx + 1 < n && cbb == ci[idx + 1].bb)
5935 /* Skip labels until the last of the group. */
5936 do {
5937 ++idx;
5938 } while (idx < n && cbb == ci[idx].bb);
5939 --idx;
5941 /* Pick up the maximum of the case label range. */
5942 if (CASE_HIGH (ci[idx].expr))
5943 max = CASE_HIGH (ci[idx].expr);
5944 else
5945 max = CASE_LOW (ci[idx].expr);
5948 /* Nothing to do if the range includes the default label until we
5949 can register anti-ranges. */
5950 if (min == NULL_TREE)
5951 continue;
5953 /* Find the edge to register the assert expr on. */
5954 e = find_edge (bb, cbb);
5956 /* Register the necessary assertions for the operand in the
5957 SWITCH_EXPR. */
5958 register_edge_assert_for (op, e, bsi,
5959 max ? GE_EXPR : EQ_EXPR,
5960 op, fold_convert (TREE_TYPE (op), min));
5961 if (max)
5962 register_edge_assert_for (op, e, bsi, LE_EXPR, op,
5963 fold_convert (TREE_TYPE (op), max));
5966 XDELETEVEC (ci);
5970 /* Traverse all the statements in block BB looking for statements that
5971 may generate useful assertions for the SSA names in their operand.
5972 If a statement produces a useful assertion A for name N_i, then the
5973 list of assertions already generated for N_i is scanned to
5974 determine if A is actually needed.
5976 If N_i already had the assertion A at a location dominating the
5977 current location, then nothing needs to be done. Otherwise, the
5978 new location for A is recorded instead.
5980 1- For every statement S in BB, all the variables used by S are
5981 added to bitmap FOUND_IN_SUBGRAPH.
5983 2- If statement S uses an operand N in a way that exposes a known
5984 value range for N, then if N was not already generated by an
5985 ASSERT_EXPR, create a new assert location for N. For instance,
5986 if N is a pointer and the statement dereferences it, we can
5987 assume that N is not NULL.
5989 3- COND_EXPRs are a special case of #2. We can derive range
5990 information from the predicate but need to insert different
5991 ASSERT_EXPRs for each of the sub-graphs rooted at the
5992 conditional block. If the last statement of BB is a conditional
5993 expression of the form 'X op Y', then
5995 a) Remove X and Y from the set FOUND_IN_SUBGRAPH.
5997 b) If the conditional is the only entry point to the sub-graph
5998 corresponding to the THEN_CLAUSE, recurse into it. On
5999 return, if X and/or Y are marked in FOUND_IN_SUBGRAPH, then
6000 an ASSERT_EXPR is added for the corresponding variable.
6002 c) Repeat step (b) on the ELSE_CLAUSE.
6004 d) Mark X and Y in FOUND_IN_SUBGRAPH.
6006 For instance,
6008 if (a == 9)
6009 b = a;
6010 else
6011 b = c + 1;
6013 In this case, an assertion on the THEN clause is useful to
6014 determine that 'a' is always 9 on that edge. However, an assertion
6015 on the ELSE clause would be unnecessary.
6017 4- If BB does not end in a conditional expression, then we recurse
6018 into BB's dominator children.
6020 At the end of the recursive traversal, every SSA name will have a
6021 list of locations where ASSERT_EXPRs should be added. When a new
6022 location for name N is found, it is registered by calling
6023 register_new_assert_for. That function keeps track of all the
6024 registered assertions to prevent adding unnecessary assertions.
6025 For instance, if a pointer P_4 is dereferenced more than once in a
6026 dominator tree, only the location dominating all the dereference of
6027 P_4 will receive an ASSERT_EXPR. */
6029 static void
6030 find_assert_locations_1 (basic_block bb, sbitmap live)
6032 gimple *last;
6034 last = last_stmt (bb);
6036 /* If BB's last statement is a conditional statement involving integer
6037 operands, determine if we need to add ASSERT_EXPRs. */
6038 if (last
6039 && gimple_code (last) == GIMPLE_COND
6040 && !fp_predicate (last)
6041 && !ZERO_SSA_OPERANDS (last, SSA_OP_USE))
6042 find_conditional_asserts (bb, as_a <gcond *> (last));
6044 /* If BB's last statement is a switch statement involving integer
6045 operands, determine if we need to add ASSERT_EXPRs. */
6046 if (last
6047 && gimple_code (last) == GIMPLE_SWITCH
6048 && !ZERO_SSA_OPERANDS (last, SSA_OP_USE))
6049 find_switch_asserts (bb, as_a <gswitch *> (last));
6051 /* Traverse all the statements in BB marking used names and looking
6052 for statements that may infer assertions for their used operands. */
6053 for (gimple_stmt_iterator si = gsi_last_bb (bb); !gsi_end_p (si);
6054 gsi_prev (&si))
6056 gimple *stmt;
6057 tree op;
6058 ssa_op_iter i;
6060 stmt = gsi_stmt (si);
6062 if (is_gimple_debug (stmt))
6063 continue;
6065 /* See if we can derive an assertion for any of STMT's operands. */
6066 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_USE)
6068 tree value;
6069 enum tree_code comp_code;
6071 /* If op is not live beyond this stmt, do not bother to insert
6072 asserts for it. */
6073 if (!bitmap_bit_p (live, SSA_NAME_VERSION (op)))
6074 continue;
6076 /* If OP is used in such a way that we can infer a value
6077 range for it, and we don't find a previous assertion for
6078 it, create a new assertion location node for OP. */
6079 if (infer_value_range (stmt, op, &comp_code, &value))
6081 /* If we are able to infer a nonzero value range for OP,
6082 then walk backwards through the use-def chain to see if OP
6083 was set via a typecast.
6085 If so, then we can also infer a nonzero value range
6086 for the operand of the NOP_EXPR. */
6087 if (comp_code == NE_EXPR && integer_zerop (value))
6089 tree t = op;
6090 gimple *def_stmt = SSA_NAME_DEF_STMT (t);
6092 while (is_gimple_assign (def_stmt)
6093 && CONVERT_EXPR_CODE_P
6094 (gimple_assign_rhs_code (def_stmt))
6095 && TREE_CODE
6096 (gimple_assign_rhs1 (def_stmt)) == SSA_NAME
6097 && POINTER_TYPE_P
6098 (TREE_TYPE (gimple_assign_rhs1 (def_stmt))))
6100 t = gimple_assign_rhs1 (def_stmt);
6101 def_stmt = SSA_NAME_DEF_STMT (t);
6103 /* Note we want to register the assert for the
6104 operand of the NOP_EXPR after SI, not after the
6105 conversion. */
6106 if (bitmap_bit_p (live, SSA_NAME_VERSION (t)))
6107 register_new_assert_for (t, t, comp_code, value,
6108 bb, NULL, si);
6112 register_new_assert_for (op, op, comp_code, value, bb, NULL, si);
6116 /* Update live. */
6117 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_USE)
6118 bitmap_set_bit (live, SSA_NAME_VERSION (op));
6119 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_DEF)
6120 bitmap_clear_bit (live, SSA_NAME_VERSION (op));
6123 /* Traverse all PHI nodes in BB, updating live. */
6124 for (gphi_iterator si = gsi_start_phis (bb); !gsi_end_p (si);
6125 gsi_next (&si))
6127 use_operand_p arg_p;
6128 ssa_op_iter i;
6129 gphi *phi = si.phi ();
6130 tree res = gimple_phi_result (phi);
6132 if (virtual_operand_p (res))
6133 continue;
6135 FOR_EACH_PHI_ARG (arg_p, phi, i, SSA_OP_USE)
6137 tree arg = USE_FROM_PTR (arg_p);
6138 if (TREE_CODE (arg) == SSA_NAME)
6139 bitmap_set_bit (live, SSA_NAME_VERSION (arg));
6142 bitmap_clear_bit (live, SSA_NAME_VERSION (res));
6146 /* Do an RPO walk over the function computing SSA name liveness
6147 on-the-fly and deciding on assert expressions to insert. */
6149 static void
6150 find_assert_locations (void)
6152 int *rpo = XNEWVEC (int, last_basic_block_for_fn (cfun));
6153 int *bb_rpo = XNEWVEC (int, last_basic_block_for_fn (cfun));
6154 int *last_rpo = XCNEWVEC (int, last_basic_block_for_fn (cfun));
6155 int rpo_cnt, i;
6157 live = XCNEWVEC (sbitmap, last_basic_block_for_fn (cfun));
6158 rpo_cnt = pre_and_rev_post_order_compute (NULL, rpo, false);
6159 for (i = 0; i < rpo_cnt; ++i)
6160 bb_rpo[rpo[i]] = i;
6162 /* Pre-seed loop latch liveness from loop header PHI nodes. Due to
6163 the order we compute liveness and insert asserts we otherwise
6164 fail to insert asserts into the loop latch. */
6165 loop_p loop;
6166 FOR_EACH_LOOP (loop, 0)
6168 i = loop->latch->index;
6169 unsigned int j = single_succ_edge (loop->latch)->dest_idx;
6170 for (gphi_iterator gsi = gsi_start_phis (loop->header);
6171 !gsi_end_p (gsi); gsi_next (&gsi))
6173 gphi *phi = gsi.phi ();
6174 if (virtual_operand_p (gimple_phi_result (phi)))
6175 continue;
6176 tree arg = gimple_phi_arg_def (phi, j);
6177 if (TREE_CODE (arg) == SSA_NAME)
6179 if (live[i] == NULL)
6181 live[i] = sbitmap_alloc (num_ssa_names);
6182 bitmap_clear (live[i]);
6184 bitmap_set_bit (live[i], SSA_NAME_VERSION (arg));
6189 for (i = rpo_cnt - 1; i >= 0; --i)
6191 basic_block bb = BASIC_BLOCK_FOR_FN (cfun, rpo[i]);
6192 edge e;
6193 edge_iterator ei;
6195 if (!live[rpo[i]])
6197 live[rpo[i]] = sbitmap_alloc (num_ssa_names);
6198 bitmap_clear (live[rpo[i]]);
6201 /* Process BB and update the live information with uses in
6202 this block. */
6203 find_assert_locations_1 (bb, live[rpo[i]]);
6205 /* Merge liveness into the predecessor blocks and free it. */
6206 if (!bitmap_empty_p (live[rpo[i]]))
6208 int pred_rpo = i;
6209 FOR_EACH_EDGE (e, ei, bb->preds)
6211 int pred = e->src->index;
6212 if ((e->flags & EDGE_DFS_BACK) || pred == ENTRY_BLOCK)
6213 continue;
6215 if (!live[pred])
6217 live[pred] = sbitmap_alloc (num_ssa_names);
6218 bitmap_clear (live[pred]);
6220 bitmap_ior (live[pred], live[pred], live[rpo[i]]);
6222 if (bb_rpo[pred] < pred_rpo)
6223 pred_rpo = bb_rpo[pred];
6226 /* Record the RPO number of the last visited block that needs
6227 live information from this block. */
6228 last_rpo[rpo[i]] = pred_rpo;
6230 else
6232 sbitmap_free (live[rpo[i]]);
6233 live[rpo[i]] = NULL;
6236 /* We can free all successors live bitmaps if all their
6237 predecessors have been visited already. */
6238 FOR_EACH_EDGE (e, ei, bb->succs)
6239 if (last_rpo[e->dest->index] == i
6240 && live[e->dest->index])
6242 sbitmap_free (live[e->dest->index]);
6243 live[e->dest->index] = NULL;
6247 XDELETEVEC (rpo);
6248 XDELETEVEC (bb_rpo);
6249 XDELETEVEC (last_rpo);
6250 for (i = 0; i < last_basic_block_for_fn (cfun); ++i)
6251 if (live[i])
6252 sbitmap_free (live[i]);
6253 XDELETEVEC (live);
6256 /* Create an ASSERT_EXPR for NAME and insert it in the location
6257 indicated by LOC. Return true if we made any edge insertions. */
6259 static bool
6260 process_assert_insertions_for (tree name, assert_locus *loc)
6262 /* Build the comparison expression NAME_i COMP_CODE VAL. */
6263 gimple *stmt;
6264 tree cond;
6265 gimple *assert_stmt;
6266 edge_iterator ei;
6267 edge e;
6269 /* If we have X <=> X do not insert an assert expr for that. */
6270 if (loc->expr == loc->val)
6271 return false;
6273 cond = build2 (loc->comp_code, boolean_type_node, loc->expr, loc->val);
6274 assert_stmt = build_assert_expr_for (cond, name);
6275 if (loc->e)
6277 /* We have been asked to insert the assertion on an edge. This
6278 is used only by COND_EXPR and SWITCH_EXPR assertions. */
6279 gcc_checking_assert (gimple_code (gsi_stmt (loc->si)) == GIMPLE_COND
6280 || (gimple_code (gsi_stmt (loc->si))
6281 == GIMPLE_SWITCH));
6283 gsi_insert_on_edge (loc->e, assert_stmt);
6284 return true;
6287 /* Otherwise, we can insert right after LOC->SI iff the
6288 statement must not be the last statement in the block. */
6289 stmt = gsi_stmt (loc->si);
6290 if (!stmt_ends_bb_p (stmt))
6292 gsi_insert_after (&loc->si, assert_stmt, GSI_SAME_STMT);
6293 return false;
6296 /* If STMT must be the last statement in BB, we can only insert new
6297 assertions on the non-abnormal edge out of BB. Note that since
6298 STMT is not control flow, there may only be one non-abnormal edge
6299 out of BB. */
6300 FOR_EACH_EDGE (e, ei, loc->bb->succs)
6301 if (!(e->flags & EDGE_ABNORMAL))
6303 gsi_insert_on_edge (e, assert_stmt);
6304 return true;
6307 gcc_unreachable ();
6311 /* Process all the insertions registered for every name N_i registered
6312 in NEED_ASSERT_FOR. The list of assertions to be inserted are
6313 found in ASSERTS_FOR[i]. */
6315 static void
6316 process_assert_insertions (void)
6318 unsigned i;
6319 bitmap_iterator bi;
6320 bool update_edges_p = false;
6321 int num_asserts = 0;
6323 if (dump_file && (dump_flags & TDF_DETAILS))
6324 dump_all_asserts (dump_file);
6326 EXECUTE_IF_SET_IN_BITMAP (need_assert_for, 0, i, bi)
6328 assert_locus *loc = asserts_for[i];
6329 gcc_assert (loc);
6331 while (loc)
6333 assert_locus *next = loc->next;
6334 update_edges_p |= process_assert_insertions_for (ssa_name (i), loc);
6335 free (loc);
6336 loc = next;
6337 num_asserts++;
6341 if (update_edges_p)
6342 gsi_commit_edge_inserts ();
6344 statistics_counter_event (cfun, "Number of ASSERT_EXPR expressions inserted",
6345 num_asserts);
6349 /* Traverse the flowgraph looking for conditional jumps to insert range
6350 expressions. These range expressions are meant to provide information
6351 to optimizations that need to reason in terms of value ranges. They
6352 will not be expanded into RTL. For instance, given:
6354 x = ...
6355 y = ...
6356 if (x < y)
6357 y = x - 2;
6358 else
6359 x = y + 3;
6361 this pass will transform the code into:
6363 x = ...
6364 y = ...
6365 if (x < y)
6367 x = ASSERT_EXPR <x, x < y>
6368 y = x - 2
6370 else
6372 y = ASSERT_EXPR <y, x >= y>
6373 x = y + 3
6376 The idea is that once copy and constant propagation have run, other
6377 optimizations will be able to determine what ranges of values can 'x'
6378 take in different paths of the code, simply by checking the reaching
6379 definition of 'x'. */
6381 static void
6382 insert_range_assertions (void)
6384 need_assert_for = BITMAP_ALLOC (NULL);
6385 asserts_for = XCNEWVEC (assert_locus *, num_ssa_names);
6387 calculate_dominance_info (CDI_DOMINATORS);
6389 find_assert_locations ();
6390 if (!bitmap_empty_p (need_assert_for))
6392 process_assert_insertions ();
6393 update_ssa (TODO_update_ssa_no_phi);
6396 if (dump_file && (dump_flags & TDF_DETAILS))
6398 fprintf (dump_file, "\nSSA form after inserting ASSERT_EXPRs\n");
6399 dump_function_to_file (current_function_decl, dump_file, dump_flags);
6402 free (asserts_for);
6403 BITMAP_FREE (need_assert_for);
6406 /* Checks one ARRAY_REF in REF, located at LOCUS. Ignores flexible arrays
6407 and "struct" hacks. If VRP can determine that the
6408 array subscript is a constant, check if it is outside valid
6409 range. If the array subscript is a RANGE, warn if it is
6410 non-overlapping with valid range.
6411 IGNORE_OFF_BY_ONE is true if the ARRAY_REF is inside a ADDR_EXPR. */
6413 static void
6414 check_array_ref (location_t location, tree ref, bool ignore_off_by_one)
6416 value_range *vr = NULL;
6417 tree low_sub, up_sub;
6418 tree low_bound, up_bound, up_bound_p1;
6420 if (TREE_NO_WARNING (ref))
6421 return;
6423 low_sub = up_sub = TREE_OPERAND (ref, 1);
6424 up_bound = array_ref_up_bound (ref);
6426 /* Can not check flexible arrays. */
6427 if (!up_bound
6428 || TREE_CODE (up_bound) != INTEGER_CST)
6429 return;
6431 /* Accesses to trailing arrays via pointers may access storage
6432 beyond the types array bounds. */
6433 if (warn_array_bounds < 2
6434 && array_at_struct_end_p (ref))
6435 return;
6437 low_bound = array_ref_low_bound (ref);
6438 up_bound_p1 = int_const_binop (PLUS_EXPR, up_bound,
6439 build_int_cst (TREE_TYPE (up_bound), 1));
6441 /* Empty array. */
6442 if (tree_int_cst_equal (low_bound, up_bound_p1))
6444 warning_at (location, OPT_Warray_bounds,
6445 "array subscript is above array bounds");
6446 TREE_NO_WARNING (ref) = 1;
6449 if (TREE_CODE (low_sub) == SSA_NAME)
6451 vr = get_value_range (low_sub);
6452 if (vr->type == VR_RANGE || vr->type == VR_ANTI_RANGE)
6454 low_sub = vr->type == VR_RANGE ? vr->max : vr->min;
6455 up_sub = vr->type == VR_RANGE ? vr->min : vr->max;
6459 if (vr && vr->type == VR_ANTI_RANGE)
6461 if (TREE_CODE (up_sub) == INTEGER_CST
6462 && (ignore_off_by_one
6463 ? tree_int_cst_lt (up_bound, up_sub)
6464 : tree_int_cst_le (up_bound, up_sub))
6465 && TREE_CODE (low_sub) == INTEGER_CST
6466 && tree_int_cst_le (low_sub, low_bound))
6468 warning_at (location, OPT_Warray_bounds,
6469 "array subscript is outside array bounds");
6470 TREE_NO_WARNING (ref) = 1;
6473 else if (TREE_CODE (up_sub) == INTEGER_CST
6474 && (ignore_off_by_one
6475 ? !tree_int_cst_le (up_sub, up_bound_p1)
6476 : !tree_int_cst_le (up_sub, up_bound)))
6478 if (dump_file && (dump_flags & TDF_DETAILS))
6480 fprintf (dump_file, "Array bound warning for ");
6481 dump_generic_expr (MSG_NOTE, TDF_SLIM, ref);
6482 fprintf (dump_file, "\n");
6484 warning_at (location, OPT_Warray_bounds,
6485 "array subscript is above array bounds");
6486 TREE_NO_WARNING (ref) = 1;
6488 else if (TREE_CODE (low_sub) == INTEGER_CST
6489 && tree_int_cst_lt (low_sub, low_bound))
6491 if (dump_file && (dump_flags & TDF_DETAILS))
6493 fprintf (dump_file, "Array bound warning for ");
6494 dump_generic_expr (MSG_NOTE, TDF_SLIM, ref);
6495 fprintf (dump_file, "\n");
6497 warning_at (location, OPT_Warray_bounds,
6498 "array subscript is below array bounds");
6499 TREE_NO_WARNING (ref) = 1;
6503 /* Searches if the expr T, located at LOCATION computes
6504 address of an ARRAY_REF, and call check_array_ref on it. */
6506 static void
6507 search_for_addr_array (tree t, location_t location)
6509 /* Check each ARRAY_REFs in the reference chain. */
6512 if (TREE_CODE (t) == ARRAY_REF)
6513 check_array_ref (location, t, true /*ignore_off_by_one*/);
6515 t = TREE_OPERAND (t, 0);
6517 while (handled_component_p (t));
6519 if (TREE_CODE (t) == MEM_REF
6520 && TREE_CODE (TREE_OPERAND (t, 0)) == ADDR_EXPR
6521 && !TREE_NO_WARNING (t))
6523 tree tem = TREE_OPERAND (TREE_OPERAND (t, 0), 0);
6524 tree low_bound, up_bound, el_sz;
6525 offset_int idx;
6526 if (TREE_CODE (TREE_TYPE (tem)) != ARRAY_TYPE
6527 || TREE_CODE (TREE_TYPE (TREE_TYPE (tem))) == ARRAY_TYPE
6528 || !TYPE_DOMAIN (TREE_TYPE (tem)))
6529 return;
6531 low_bound = TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (tem)));
6532 up_bound = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (tem)));
6533 el_sz = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (tem)));
6534 if (!low_bound
6535 || TREE_CODE (low_bound) != INTEGER_CST
6536 || !up_bound
6537 || TREE_CODE (up_bound) != INTEGER_CST
6538 || !el_sz
6539 || TREE_CODE (el_sz) != INTEGER_CST)
6540 return;
6542 idx = mem_ref_offset (t);
6543 idx = wi::sdiv_trunc (idx, wi::to_offset (el_sz));
6544 if (idx < 0)
6546 if (dump_file && (dump_flags & TDF_DETAILS))
6548 fprintf (dump_file, "Array bound warning for ");
6549 dump_generic_expr (MSG_NOTE, TDF_SLIM, t);
6550 fprintf (dump_file, "\n");
6552 warning_at (location, OPT_Warray_bounds,
6553 "array subscript is below array bounds");
6554 TREE_NO_WARNING (t) = 1;
6556 else if (idx > (wi::to_offset (up_bound)
6557 - wi::to_offset (low_bound) + 1))
6559 if (dump_file && (dump_flags & TDF_DETAILS))
6561 fprintf (dump_file, "Array bound warning for ");
6562 dump_generic_expr (MSG_NOTE, TDF_SLIM, t);
6563 fprintf (dump_file, "\n");
6565 warning_at (location, OPT_Warray_bounds,
6566 "array subscript is above array bounds");
6567 TREE_NO_WARNING (t) = 1;
6572 /* walk_tree() callback that checks if *TP is
6573 an ARRAY_REF inside an ADDR_EXPR (in which an array
6574 subscript one outside the valid range is allowed). Call
6575 check_array_ref for each ARRAY_REF found. The location is
6576 passed in DATA. */
6578 static tree
6579 check_array_bounds (tree *tp, int *walk_subtree, void *data)
6581 tree t = *tp;
6582 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
6583 location_t location;
6585 if (EXPR_HAS_LOCATION (t))
6586 location = EXPR_LOCATION (t);
6587 else
6589 location_t *locp = (location_t *) wi->info;
6590 location = *locp;
6593 *walk_subtree = TRUE;
6595 if (TREE_CODE (t) == ARRAY_REF)
6596 check_array_ref (location, t, false /*ignore_off_by_one*/);
6598 else if (TREE_CODE (t) == ADDR_EXPR)
6600 search_for_addr_array (t, location);
6601 *walk_subtree = FALSE;
6604 return NULL_TREE;
6607 /* Walk over all statements of all reachable BBs and call check_array_bounds
6608 on them. */
6610 static void
6611 check_all_array_refs (void)
6613 basic_block bb;
6614 gimple_stmt_iterator si;
6616 FOR_EACH_BB_FN (bb, cfun)
6618 edge_iterator ei;
6619 edge e;
6620 bool executable = false;
6622 /* Skip blocks that were found to be unreachable. */
6623 FOR_EACH_EDGE (e, ei, bb->preds)
6624 executable |= !!(e->flags & EDGE_EXECUTABLE);
6625 if (!executable)
6626 continue;
6628 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
6630 gimple *stmt = gsi_stmt (si);
6631 struct walk_stmt_info wi;
6632 if (!gimple_has_location (stmt)
6633 || is_gimple_debug (stmt))
6634 continue;
6636 memset (&wi, 0, sizeof (wi));
6638 location_t loc = gimple_location (stmt);
6639 wi.info = &loc;
6641 walk_gimple_op (gsi_stmt (si),
6642 check_array_bounds,
6643 &wi);
6648 /* Return true if all imm uses of VAR are either in STMT, or
6649 feed (optionally through a chain of single imm uses) GIMPLE_COND
6650 in basic block COND_BB. */
6652 static bool
6653 all_imm_uses_in_stmt_or_feed_cond (tree var, gimple *stmt, basic_block cond_bb)
6655 use_operand_p use_p, use2_p;
6656 imm_use_iterator iter;
6658 FOR_EACH_IMM_USE_FAST (use_p, iter, var)
6659 if (USE_STMT (use_p) != stmt)
6661 gimple *use_stmt = USE_STMT (use_p), *use_stmt2;
6662 if (is_gimple_debug (use_stmt))
6663 continue;
6664 while (is_gimple_assign (use_stmt)
6665 && TREE_CODE (gimple_assign_lhs (use_stmt)) == SSA_NAME
6666 && single_imm_use (gimple_assign_lhs (use_stmt),
6667 &use2_p, &use_stmt2))
6668 use_stmt = use_stmt2;
6669 if (gimple_code (use_stmt) != GIMPLE_COND
6670 || gimple_bb (use_stmt) != cond_bb)
6671 return false;
6673 return true;
6676 /* Handle
6677 _4 = x_3 & 31;
6678 if (_4 != 0)
6679 goto <bb 6>;
6680 else
6681 goto <bb 7>;
6682 <bb 6>:
6683 __builtin_unreachable ();
6684 <bb 7>:
6685 x_5 = ASSERT_EXPR <x_3, ...>;
6686 If x_3 has no other immediate uses (checked by caller),
6687 var is the x_3 var from ASSERT_EXPR, we can clear low 5 bits
6688 from the non-zero bitmask. */
6690 static void
6691 maybe_set_nonzero_bits (basic_block bb, tree var)
6693 edge e = single_pred_edge (bb);
6694 basic_block cond_bb = e->src;
6695 gimple *stmt = last_stmt (cond_bb);
6696 tree cst;
6698 if (stmt == NULL
6699 || gimple_code (stmt) != GIMPLE_COND
6700 || gimple_cond_code (stmt) != ((e->flags & EDGE_TRUE_VALUE)
6701 ? EQ_EXPR : NE_EXPR)
6702 || TREE_CODE (gimple_cond_lhs (stmt)) != SSA_NAME
6703 || !integer_zerop (gimple_cond_rhs (stmt)))
6704 return;
6706 stmt = SSA_NAME_DEF_STMT (gimple_cond_lhs (stmt));
6707 if (!is_gimple_assign (stmt)
6708 || gimple_assign_rhs_code (stmt) != BIT_AND_EXPR
6709 || TREE_CODE (gimple_assign_rhs2 (stmt)) != INTEGER_CST)
6710 return;
6711 if (gimple_assign_rhs1 (stmt) != var)
6713 gimple *stmt2;
6715 if (TREE_CODE (gimple_assign_rhs1 (stmt)) != SSA_NAME)
6716 return;
6717 stmt2 = SSA_NAME_DEF_STMT (gimple_assign_rhs1 (stmt));
6718 if (!gimple_assign_cast_p (stmt2)
6719 || gimple_assign_rhs1 (stmt2) != var
6720 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (stmt2))
6721 || (TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (stmt)))
6722 != TYPE_PRECISION (TREE_TYPE (var))))
6723 return;
6725 cst = gimple_assign_rhs2 (stmt);
6726 set_nonzero_bits (var, wi::bit_and_not (get_nonzero_bits (var), cst));
6729 /* Convert range assertion expressions into the implied copies and
6730 copy propagate away the copies. Doing the trivial copy propagation
6731 here avoids the need to run the full copy propagation pass after
6732 VRP.
6734 FIXME, this will eventually lead to copy propagation removing the
6735 names that had useful range information attached to them. For
6736 instance, if we had the assertion N_i = ASSERT_EXPR <N_j, N_j > 3>,
6737 then N_i will have the range [3, +INF].
6739 However, by converting the assertion into the implied copy
6740 operation N_i = N_j, we will then copy-propagate N_j into the uses
6741 of N_i and lose the range information. We may want to hold on to
6742 ASSERT_EXPRs a little while longer as the ranges could be used in
6743 things like jump threading.
6745 The problem with keeping ASSERT_EXPRs around is that passes after
6746 VRP need to handle them appropriately.
6748 Another approach would be to make the range information a first
6749 class property of the SSA_NAME so that it can be queried from
6750 any pass. This is made somewhat more complex by the need for
6751 multiple ranges to be associated with one SSA_NAME. */
6753 static void
6754 remove_range_assertions (void)
6756 basic_block bb;
6757 gimple_stmt_iterator si;
6758 /* 1 if looking at ASSERT_EXPRs immediately at the beginning of
6759 a basic block preceeded by GIMPLE_COND branching to it and
6760 __builtin_trap, -1 if not yet checked, 0 otherwise. */
6761 int is_unreachable;
6763 /* Note that the BSI iterator bump happens at the bottom of the
6764 loop and no bump is necessary if we're removing the statement
6765 referenced by the current BSI. */
6766 FOR_EACH_BB_FN (bb, cfun)
6767 for (si = gsi_after_labels (bb), is_unreachable = -1; !gsi_end_p (si);)
6769 gimple *stmt = gsi_stmt (si);
6770 gimple *use_stmt;
6772 if (is_gimple_assign (stmt)
6773 && gimple_assign_rhs_code (stmt) == ASSERT_EXPR)
6775 tree lhs = gimple_assign_lhs (stmt);
6776 tree rhs = gimple_assign_rhs1 (stmt);
6777 tree var;
6778 use_operand_p use_p;
6779 imm_use_iterator iter;
6781 var = ASSERT_EXPR_VAR (rhs);
6782 gcc_assert (TREE_CODE (var) == SSA_NAME);
6784 if (!POINTER_TYPE_P (TREE_TYPE (lhs))
6785 && SSA_NAME_RANGE_INFO (lhs))
6787 if (is_unreachable == -1)
6789 is_unreachable = 0;
6790 if (single_pred_p (bb)
6791 && assert_unreachable_fallthru_edge_p
6792 (single_pred_edge (bb)))
6793 is_unreachable = 1;
6795 /* Handle
6796 if (x_7 >= 10 && x_7 < 20)
6797 __builtin_unreachable ();
6798 x_8 = ASSERT_EXPR <x_7, ...>;
6799 if the only uses of x_7 are in the ASSERT_EXPR and
6800 in the condition. In that case, we can copy the
6801 range info from x_8 computed in this pass also
6802 for x_7. */
6803 if (is_unreachable
6804 && all_imm_uses_in_stmt_or_feed_cond (var, stmt,
6805 single_pred (bb)))
6807 set_range_info (var, SSA_NAME_RANGE_TYPE (lhs),
6808 SSA_NAME_RANGE_INFO (lhs)->get_min (),
6809 SSA_NAME_RANGE_INFO (lhs)->get_max ());
6810 maybe_set_nonzero_bits (bb, var);
6814 /* Propagate the RHS into every use of the LHS. */
6815 FOR_EACH_IMM_USE_STMT (use_stmt, iter, lhs)
6816 FOR_EACH_IMM_USE_ON_STMT (use_p, iter)
6817 SET_USE (use_p, var);
6819 /* And finally, remove the copy, it is not needed. */
6820 gsi_remove (&si, true);
6821 release_defs (stmt);
6823 else
6825 if (!is_gimple_debug (gsi_stmt (si)))
6826 is_unreachable = 0;
6827 gsi_next (&si);
6833 /* Return true if STMT is interesting for VRP. */
6835 static bool
6836 stmt_interesting_for_vrp (gimple *stmt)
6838 if (gimple_code (stmt) == GIMPLE_PHI)
6840 tree res = gimple_phi_result (stmt);
6841 return (!virtual_operand_p (res)
6842 && (INTEGRAL_TYPE_P (TREE_TYPE (res))
6843 || POINTER_TYPE_P (TREE_TYPE (res))));
6845 else if (is_gimple_assign (stmt) || is_gimple_call (stmt))
6847 tree lhs = gimple_get_lhs (stmt);
6849 /* In general, assignments with virtual operands are not useful
6850 for deriving ranges, with the obvious exception of calls to
6851 builtin functions. */
6852 if (lhs && TREE_CODE (lhs) == SSA_NAME
6853 && (INTEGRAL_TYPE_P (TREE_TYPE (lhs))
6854 || POINTER_TYPE_P (TREE_TYPE (lhs)))
6855 && (is_gimple_call (stmt)
6856 || !gimple_vuse (stmt)))
6857 return true;
6858 else if (is_gimple_call (stmt) && gimple_call_internal_p (stmt))
6859 switch (gimple_call_internal_fn (stmt))
6861 case IFN_ADD_OVERFLOW:
6862 case IFN_SUB_OVERFLOW:
6863 case IFN_MUL_OVERFLOW:
6864 /* These internal calls return _Complex integer type,
6865 but are interesting to VRP nevertheless. */
6866 if (lhs && TREE_CODE (lhs) == SSA_NAME)
6867 return true;
6868 break;
6869 default:
6870 break;
6873 else if (gimple_code (stmt) == GIMPLE_COND
6874 || gimple_code (stmt) == GIMPLE_SWITCH)
6875 return true;
6877 return false;
6881 /* Initialize local data structures for VRP. */
6883 static void
6884 vrp_initialize (void)
6886 basic_block bb;
6888 values_propagated = false;
6889 num_vr_values = num_ssa_names;
6890 vr_value = XCNEWVEC (value_range *, num_vr_values);
6891 vr_phi_edge_counts = XCNEWVEC (int, num_ssa_names);
6893 FOR_EACH_BB_FN (bb, cfun)
6895 for (gphi_iterator si = gsi_start_phis (bb); !gsi_end_p (si);
6896 gsi_next (&si))
6898 gphi *phi = si.phi ();
6899 if (!stmt_interesting_for_vrp (phi))
6901 tree lhs = PHI_RESULT (phi);
6902 set_value_range_to_varying (get_value_range (lhs));
6903 prop_set_simulate_again (phi, false);
6905 else
6906 prop_set_simulate_again (phi, true);
6909 for (gimple_stmt_iterator si = gsi_start_bb (bb); !gsi_end_p (si);
6910 gsi_next (&si))
6912 gimple *stmt = gsi_stmt (si);
6914 /* If the statement is a control insn, then we do not
6915 want to avoid simulating the statement once. Failure
6916 to do so means that those edges will never get added. */
6917 if (stmt_ends_bb_p (stmt))
6918 prop_set_simulate_again (stmt, true);
6919 else if (!stmt_interesting_for_vrp (stmt))
6921 ssa_op_iter i;
6922 tree def;
6923 FOR_EACH_SSA_TREE_OPERAND (def, stmt, i, SSA_OP_DEF)
6924 set_value_range_to_varying (get_value_range (def));
6925 prop_set_simulate_again (stmt, false);
6927 else
6928 prop_set_simulate_again (stmt, true);
6933 /* Return the singleton value-range for NAME or NAME. */
6935 static inline tree
6936 vrp_valueize (tree name)
6938 if (TREE_CODE (name) == SSA_NAME)
6940 value_range *vr = get_value_range (name);
6941 if (vr->type == VR_RANGE
6942 && (vr->min == vr->max
6943 || operand_equal_p (vr->min, vr->max, 0)))
6944 return vr->min;
6946 return name;
6949 /* Return the singleton value-range for NAME if that is a constant
6950 but signal to not follow SSA edges. */
6952 static inline tree
6953 vrp_valueize_1 (tree name)
6955 if (TREE_CODE (name) == SSA_NAME)
6957 /* If the definition may be simulated again we cannot follow
6958 this SSA edge as the SSA propagator does not necessarily
6959 re-visit the use. */
6960 gimple *def_stmt = SSA_NAME_DEF_STMT (name);
6961 if (!gimple_nop_p (def_stmt)
6962 && prop_simulate_again_p (def_stmt))
6963 return NULL_TREE;
6964 value_range *vr = get_value_range (name);
6965 if (range_int_cst_singleton_p (vr))
6966 return vr->min;
6968 return name;
6971 /* Visit assignment STMT. If it produces an interesting range, record
6972 the SSA name in *OUTPUT_P. */
6974 static enum ssa_prop_result
6975 vrp_visit_assignment_or_call (gimple *stmt, tree *output_p)
6977 tree def, lhs;
6978 ssa_op_iter iter;
6979 enum gimple_code code = gimple_code (stmt);
6980 lhs = gimple_get_lhs (stmt);
6982 /* We only keep track of ranges in integral and pointer types. */
6983 if (TREE_CODE (lhs) == SSA_NAME
6984 && ((INTEGRAL_TYPE_P (TREE_TYPE (lhs))
6985 /* It is valid to have NULL MIN/MAX values on a type. See
6986 build_range_type. */
6987 && TYPE_MIN_VALUE (TREE_TYPE (lhs))
6988 && TYPE_MAX_VALUE (TREE_TYPE (lhs)))
6989 || POINTER_TYPE_P (TREE_TYPE (lhs))))
6991 value_range new_vr = VR_INITIALIZER;
6993 /* Try folding the statement to a constant first. */
6994 tree tem = gimple_fold_stmt_to_constant_1 (stmt, vrp_valueize,
6995 vrp_valueize_1);
6996 if (tem && is_gimple_min_invariant (tem))
6997 set_value_range_to_value (&new_vr, tem, NULL);
6998 /* Then dispatch to value-range extracting functions. */
6999 else if (code == GIMPLE_CALL)
7000 extract_range_basic (&new_vr, stmt);
7001 else
7002 extract_range_from_assignment (&new_vr, as_a <gassign *> (stmt));
7004 if (update_value_range (lhs, &new_vr))
7006 *output_p = lhs;
7008 if (dump_file && (dump_flags & TDF_DETAILS))
7010 fprintf (dump_file, "Found new range for ");
7011 print_generic_expr (dump_file, lhs, 0);
7012 fprintf (dump_file, ": ");
7013 dump_value_range (dump_file, &new_vr);
7014 fprintf (dump_file, "\n");
7017 if (new_vr.type == VR_VARYING)
7018 return SSA_PROP_VARYING;
7020 return SSA_PROP_INTERESTING;
7023 return SSA_PROP_NOT_INTERESTING;
7025 else if (is_gimple_call (stmt) && gimple_call_internal_p (stmt))
7026 switch (gimple_call_internal_fn (stmt))
7028 case IFN_ADD_OVERFLOW:
7029 case IFN_SUB_OVERFLOW:
7030 case IFN_MUL_OVERFLOW:
7031 /* These internal calls return _Complex integer type,
7032 which VRP does not track, but the immediate uses
7033 thereof might be interesting. */
7034 if (lhs && TREE_CODE (lhs) == SSA_NAME)
7036 imm_use_iterator iter;
7037 use_operand_p use_p;
7038 enum ssa_prop_result res = SSA_PROP_VARYING;
7040 set_value_range_to_varying (get_value_range (lhs));
7042 FOR_EACH_IMM_USE_FAST (use_p, iter, lhs)
7044 gimple *use_stmt = USE_STMT (use_p);
7045 if (!is_gimple_assign (use_stmt))
7046 continue;
7047 enum tree_code rhs_code = gimple_assign_rhs_code (use_stmt);
7048 if (rhs_code != REALPART_EXPR && rhs_code != IMAGPART_EXPR)
7049 continue;
7050 tree rhs1 = gimple_assign_rhs1 (use_stmt);
7051 tree use_lhs = gimple_assign_lhs (use_stmt);
7052 if (TREE_CODE (rhs1) != rhs_code
7053 || TREE_OPERAND (rhs1, 0) != lhs
7054 || TREE_CODE (use_lhs) != SSA_NAME
7055 || !stmt_interesting_for_vrp (use_stmt)
7056 || (!INTEGRAL_TYPE_P (TREE_TYPE (use_lhs))
7057 || !TYPE_MIN_VALUE (TREE_TYPE (use_lhs))
7058 || !TYPE_MAX_VALUE (TREE_TYPE (use_lhs))))
7059 continue;
7061 /* If there is a change in the value range for any of the
7062 REALPART_EXPR/IMAGPART_EXPR immediate uses, return
7063 SSA_PROP_INTERESTING. If there are any REALPART_EXPR
7064 or IMAGPART_EXPR immediate uses, but none of them have
7065 a change in their value ranges, return
7066 SSA_PROP_NOT_INTERESTING. If there are no
7067 {REAL,IMAG}PART_EXPR uses at all,
7068 return SSA_PROP_VARYING. */
7069 value_range new_vr = VR_INITIALIZER;
7070 extract_range_basic (&new_vr, use_stmt);
7071 value_range *old_vr = get_value_range (use_lhs);
7072 if (old_vr->type != new_vr.type
7073 || !vrp_operand_equal_p (old_vr->min, new_vr.min)
7074 || !vrp_operand_equal_p (old_vr->max, new_vr.max)
7075 || !vrp_bitmap_equal_p (old_vr->equiv, new_vr.equiv))
7076 res = SSA_PROP_INTERESTING;
7077 else
7078 res = SSA_PROP_NOT_INTERESTING;
7079 BITMAP_FREE (new_vr.equiv);
7080 if (res == SSA_PROP_INTERESTING)
7082 *output_p = lhs;
7083 return res;
7087 return res;
7089 break;
7090 default:
7091 break;
7094 /* Every other statement produces no useful ranges. */
7095 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_DEF)
7096 set_value_range_to_varying (get_value_range (def));
7098 return SSA_PROP_VARYING;
7101 /* Helper that gets the value range of the SSA_NAME with version I
7102 or a symbolic range containing the SSA_NAME only if the value range
7103 is varying or undefined. */
7105 static inline value_range
7106 get_vr_for_comparison (int i)
7108 value_range vr = *get_value_range (ssa_name (i));
7110 /* If name N_i does not have a valid range, use N_i as its own
7111 range. This allows us to compare against names that may
7112 have N_i in their ranges. */
7113 if (vr.type == VR_VARYING || vr.type == VR_UNDEFINED)
7115 vr.type = VR_RANGE;
7116 vr.min = ssa_name (i);
7117 vr.max = ssa_name (i);
7120 return vr;
7123 /* Compare all the value ranges for names equivalent to VAR with VAL
7124 using comparison code COMP. Return the same value returned by
7125 compare_range_with_value, including the setting of
7126 *STRICT_OVERFLOW_P. */
7128 static tree
7129 compare_name_with_value (enum tree_code comp, tree var, tree val,
7130 bool *strict_overflow_p, bool use_equiv_p)
7132 bitmap_iterator bi;
7133 unsigned i;
7134 bitmap e;
7135 tree retval, t;
7136 int used_strict_overflow;
7137 bool sop;
7138 value_range equiv_vr;
7140 /* Get the set of equivalences for VAR. */
7141 e = get_value_range (var)->equiv;
7143 /* Start at -1. Set it to 0 if we do a comparison without relying
7144 on overflow, or 1 if all comparisons rely on overflow. */
7145 used_strict_overflow = -1;
7147 /* Compare vars' value range with val. */
7148 equiv_vr = get_vr_for_comparison (SSA_NAME_VERSION (var));
7149 sop = false;
7150 retval = compare_range_with_value (comp, &equiv_vr, val, &sop);
7151 if (retval)
7152 used_strict_overflow = sop ? 1 : 0;
7154 /* If the equiv set is empty we have done all work we need to do. */
7155 if (e == NULL)
7157 if (retval
7158 && used_strict_overflow > 0)
7159 *strict_overflow_p = true;
7160 return retval;
7163 EXECUTE_IF_SET_IN_BITMAP (e, 0, i, bi)
7165 if (! use_equiv_p
7166 && ! SSA_NAME_IS_DEFAULT_DEF (ssa_name (i))
7167 && prop_simulate_again_p (SSA_NAME_DEF_STMT (ssa_name (i))))
7168 continue;
7170 equiv_vr = get_vr_for_comparison (i);
7171 sop = false;
7172 t = compare_range_with_value (comp, &equiv_vr, val, &sop);
7173 if (t)
7175 /* If we get different answers from different members
7176 of the equivalence set this check must be in a dead
7177 code region. Folding it to a trap representation
7178 would be correct here. For now just return don't-know. */
7179 if (retval != NULL
7180 && t != retval)
7182 retval = NULL_TREE;
7183 break;
7185 retval = t;
7187 if (!sop)
7188 used_strict_overflow = 0;
7189 else if (used_strict_overflow < 0)
7190 used_strict_overflow = 1;
7194 if (retval
7195 && used_strict_overflow > 0)
7196 *strict_overflow_p = true;
7198 return retval;
7202 /* Given a comparison code COMP and names N1 and N2, compare all the
7203 ranges equivalent to N1 against all the ranges equivalent to N2
7204 to determine the value of N1 COMP N2. Return the same value
7205 returned by compare_ranges. Set *STRICT_OVERFLOW_P to indicate
7206 whether we relied on an overflow infinity in the comparison. */
7209 static tree
7210 compare_names (enum tree_code comp, tree n1, tree n2,
7211 bool *strict_overflow_p)
7213 tree t, retval;
7214 bitmap e1, e2;
7215 bitmap_iterator bi1, bi2;
7216 unsigned i1, i2;
7217 int used_strict_overflow;
7218 static bitmap_obstack *s_obstack = NULL;
7219 static bitmap s_e1 = NULL, s_e2 = NULL;
7221 /* Compare the ranges of every name equivalent to N1 against the
7222 ranges of every name equivalent to N2. */
7223 e1 = get_value_range (n1)->equiv;
7224 e2 = get_value_range (n2)->equiv;
7226 /* Use the fake bitmaps if e1 or e2 are not available. */
7227 if (s_obstack == NULL)
7229 s_obstack = XNEW (bitmap_obstack);
7230 bitmap_obstack_initialize (s_obstack);
7231 s_e1 = BITMAP_ALLOC (s_obstack);
7232 s_e2 = BITMAP_ALLOC (s_obstack);
7234 if (e1 == NULL)
7235 e1 = s_e1;
7236 if (e2 == NULL)
7237 e2 = s_e2;
7239 /* Add N1 and N2 to their own set of equivalences to avoid
7240 duplicating the body of the loop just to check N1 and N2
7241 ranges. */
7242 bitmap_set_bit (e1, SSA_NAME_VERSION (n1));
7243 bitmap_set_bit (e2, SSA_NAME_VERSION (n2));
7245 /* If the equivalence sets have a common intersection, then the two
7246 names can be compared without checking their ranges. */
7247 if (bitmap_intersect_p (e1, e2))
7249 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
7250 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
7252 return (comp == EQ_EXPR || comp == GE_EXPR || comp == LE_EXPR)
7253 ? boolean_true_node
7254 : boolean_false_node;
7257 /* Start at -1. Set it to 0 if we do a comparison without relying
7258 on overflow, or 1 if all comparisons rely on overflow. */
7259 used_strict_overflow = -1;
7261 /* Otherwise, compare all the equivalent ranges. First, add N1 and
7262 N2 to their own set of equivalences to avoid duplicating the body
7263 of the loop just to check N1 and N2 ranges. */
7264 EXECUTE_IF_SET_IN_BITMAP (e1, 0, i1, bi1)
7266 value_range vr1 = get_vr_for_comparison (i1);
7268 t = retval = NULL_TREE;
7269 EXECUTE_IF_SET_IN_BITMAP (e2, 0, i2, bi2)
7271 bool sop = false;
7273 value_range vr2 = get_vr_for_comparison (i2);
7275 t = compare_ranges (comp, &vr1, &vr2, &sop);
7276 if (t)
7278 /* If we get different answers from different members
7279 of the equivalence set this check must be in a dead
7280 code region. Folding it to a trap representation
7281 would be correct here. For now just return don't-know. */
7282 if (retval != NULL
7283 && t != retval)
7285 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
7286 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
7287 return NULL_TREE;
7289 retval = t;
7291 if (!sop)
7292 used_strict_overflow = 0;
7293 else if (used_strict_overflow < 0)
7294 used_strict_overflow = 1;
7298 if (retval)
7300 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
7301 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
7302 if (used_strict_overflow > 0)
7303 *strict_overflow_p = true;
7304 return retval;
7308 /* None of the equivalent ranges are useful in computing this
7309 comparison. */
7310 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
7311 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
7312 return NULL_TREE;
7315 /* Helper function for vrp_evaluate_conditional_warnv & other
7316 optimizers. */
7318 static tree
7319 vrp_evaluate_conditional_warnv_with_ops_using_ranges (enum tree_code code,
7320 tree op0, tree op1,
7321 bool * strict_overflow_p)
7323 value_range *vr0, *vr1;
7325 vr0 = (TREE_CODE (op0) == SSA_NAME) ? get_value_range (op0) : NULL;
7326 vr1 = (TREE_CODE (op1) == SSA_NAME) ? get_value_range (op1) : NULL;
7328 tree res = NULL_TREE;
7329 if (vr0 && vr1)
7330 res = compare_ranges (code, vr0, vr1, strict_overflow_p);
7331 if (!res && vr0)
7332 res = compare_range_with_value (code, vr0, op1, strict_overflow_p);
7333 if (!res && vr1)
7334 res = (compare_range_with_value
7335 (swap_tree_comparison (code), vr1, op0, strict_overflow_p));
7336 return res;
7339 /* Helper function for vrp_evaluate_conditional_warnv. */
7341 static tree
7342 vrp_evaluate_conditional_warnv_with_ops (enum tree_code code, tree op0,
7343 tree op1, bool use_equiv_p,
7344 bool *strict_overflow_p, bool *only_ranges)
7346 tree ret;
7347 if (only_ranges)
7348 *only_ranges = true;
7350 /* We only deal with integral and pointer types. */
7351 if (!INTEGRAL_TYPE_P (TREE_TYPE (op0))
7352 && !POINTER_TYPE_P (TREE_TYPE (op0)))
7353 return NULL_TREE;
7355 if ((ret = vrp_evaluate_conditional_warnv_with_ops_using_ranges
7356 (code, op0, op1, strict_overflow_p)))
7357 return ret;
7358 if (only_ranges)
7359 *only_ranges = false;
7360 /* Do not use compare_names during propagation, it's quadratic. */
7361 if (TREE_CODE (op0) == SSA_NAME && TREE_CODE (op1) == SSA_NAME
7362 && use_equiv_p)
7363 return compare_names (code, op0, op1, strict_overflow_p);
7364 else if (TREE_CODE (op0) == SSA_NAME)
7365 return compare_name_with_value (code, op0, op1,
7366 strict_overflow_p, use_equiv_p);
7367 else if (TREE_CODE (op1) == SSA_NAME)
7368 return compare_name_with_value (swap_tree_comparison (code), op1, op0,
7369 strict_overflow_p, use_equiv_p);
7370 return NULL_TREE;
7373 /* Given (CODE OP0 OP1) within STMT, try to simplify it based on value range
7374 information. Return NULL if the conditional can not be evaluated.
7375 The ranges of all the names equivalent with the operands in COND
7376 will be used when trying to compute the value. If the result is
7377 based on undefined signed overflow, issue a warning if
7378 appropriate. */
7380 static tree
7381 vrp_evaluate_conditional (tree_code code, tree op0, tree op1, gimple *stmt)
7383 bool sop;
7384 tree ret;
7385 bool only_ranges;
7387 /* Some passes and foldings leak constants with overflow flag set
7388 into the IL. Avoid doing wrong things with these and bail out. */
7389 if ((TREE_CODE (op0) == INTEGER_CST
7390 && TREE_OVERFLOW (op0))
7391 || (TREE_CODE (op1) == INTEGER_CST
7392 && TREE_OVERFLOW (op1)))
7393 return NULL_TREE;
7395 sop = false;
7396 ret = vrp_evaluate_conditional_warnv_with_ops (code, op0, op1, true, &sop,
7397 &only_ranges);
7399 if (ret && sop)
7401 enum warn_strict_overflow_code wc;
7402 const char* warnmsg;
7404 if (is_gimple_min_invariant (ret))
7406 wc = WARN_STRICT_OVERFLOW_CONDITIONAL;
7407 warnmsg = G_("assuming signed overflow does not occur when "
7408 "simplifying conditional to constant");
7410 else
7412 wc = WARN_STRICT_OVERFLOW_COMPARISON;
7413 warnmsg = G_("assuming signed overflow does not occur when "
7414 "simplifying conditional");
7417 if (issue_strict_overflow_warning (wc))
7419 location_t location;
7421 if (!gimple_has_location (stmt))
7422 location = input_location;
7423 else
7424 location = gimple_location (stmt);
7425 warning_at (location, OPT_Wstrict_overflow, "%s", warnmsg);
7429 if (warn_type_limits
7430 && ret && only_ranges
7431 && TREE_CODE_CLASS (code) == tcc_comparison
7432 && TREE_CODE (op0) == SSA_NAME)
7434 /* If the comparison is being folded and the operand on the LHS
7435 is being compared against a constant value that is outside of
7436 the natural range of OP0's type, then the predicate will
7437 always fold regardless of the value of OP0. If -Wtype-limits
7438 was specified, emit a warning. */
7439 tree type = TREE_TYPE (op0);
7440 value_range *vr0 = get_value_range (op0);
7442 if (vr0->type == VR_RANGE
7443 && INTEGRAL_TYPE_P (type)
7444 && vrp_val_is_min (vr0->min)
7445 && vrp_val_is_max (vr0->max)
7446 && is_gimple_min_invariant (op1))
7448 location_t location;
7450 if (!gimple_has_location (stmt))
7451 location = input_location;
7452 else
7453 location = gimple_location (stmt);
7455 warning_at (location, OPT_Wtype_limits,
7456 integer_zerop (ret)
7457 ? G_("comparison always false "
7458 "due to limited range of data type")
7459 : G_("comparison always true "
7460 "due to limited range of data type"));
7464 return ret;
7468 /* Visit conditional statement STMT. If we can determine which edge
7469 will be taken out of STMT's basic block, record it in
7470 *TAKEN_EDGE_P and return SSA_PROP_INTERESTING. Otherwise, return
7471 SSA_PROP_VARYING. */
7473 static enum ssa_prop_result
7474 vrp_visit_cond_stmt (gcond *stmt, edge *taken_edge_p)
7476 tree val;
7477 bool sop;
7479 *taken_edge_p = NULL;
7481 if (dump_file && (dump_flags & TDF_DETAILS))
7483 tree use;
7484 ssa_op_iter i;
7486 fprintf (dump_file, "\nVisiting conditional with predicate: ");
7487 print_gimple_stmt (dump_file, stmt, 0, 0);
7488 fprintf (dump_file, "\nWith known ranges\n");
7490 FOR_EACH_SSA_TREE_OPERAND (use, stmt, i, SSA_OP_USE)
7492 fprintf (dump_file, "\t");
7493 print_generic_expr (dump_file, use, 0);
7494 fprintf (dump_file, ": ");
7495 dump_value_range (dump_file, vr_value[SSA_NAME_VERSION (use)]);
7498 fprintf (dump_file, "\n");
7501 /* Compute the value of the predicate COND by checking the known
7502 ranges of each of its operands.
7504 Note that we cannot evaluate all the equivalent ranges here
7505 because those ranges may not yet be final and with the current
7506 propagation strategy, we cannot determine when the value ranges
7507 of the names in the equivalence set have changed.
7509 For instance, given the following code fragment
7511 i_5 = PHI <8, i_13>
7513 i_14 = ASSERT_EXPR <i_5, i_5 != 0>
7514 if (i_14 == 1)
7517 Assume that on the first visit to i_14, i_5 has the temporary
7518 range [8, 8] because the second argument to the PHI function is
7519 not yet executable. We derive the range ~[0, 0] for i_14 and the
7520 equivalence set { i_5 }. So, when we visit 'if (i_14 == 1)' for
7521 the first time, since i_14 is equivalent to the range [8, 8], we
7522 determine that the predicate is always false.
7524 On the next round of propagation, i_13 is determined to be
7525 VARYING, which causes i_5 to drop down to VARYING. So, another
7526 visit to i_14 is scheduled. In this second visit, we compute the
7527 exact same range and equivalence set for i_14, namely ~[0, 0] and
7528 { i_5 }. But we did not have the previous range for i_5
7529 registered, so vrp_visit_assignment thinks that the range for
7530 i_14 has not changed. Therefore, the predicate 'if (i_14 == 1)'
7531 is not visited again, which stops propagation from visiting
7532 statements in the THEN clause of that if().
7534 To properly fix this we would need to keep the previous range
7535 value for the names in the equivalence set. This way we would've
7536 discovered that from one visit to the other i_5 changed from
7537 range [8, 8] to VR_VARYING.
7539 However, fixing this apparent limitation may not be worth the
7540 additional checking. Testing on several code bases (GCC, DLV,
7541 MICO, TRAMP3D and SPEC2000) showed that doing this results in
7542 4 more predicates folded in SPEC. */
7543 sop = false;
7545 val = vrp_evaluate_conditional_warnv_with_ops (gimple_cond_code (stmt),
7546 gimple_cond_lhs (stmt),
7547 gimple_cond_rhs (stmt),
7548 false, &sop, NULL);
7549 if (val)
7551 if (!sop)
7552 *taken_edge_p = find_taken_edge (gimple_bb (stmt), val);
7553 else
7555 if (dump_file && (dump_flags & TDF_DETAILS))
7556 fprintf (dump_file,
7557 "\nIgnoring predicate evaluation because "
7558 "it assumes that signed overflow is undefined");
7559 val = NULL_TREE;
7563 if (dump_file && (dump_flags & TDF_DETAILS))
7565 fprintf (dump_file, "\nPredicate evaluates to: ");
7566 if (val == NULL_TREE)
7567 fprintf (dump_file, "DON'T KNOW\n");
7568 else
7569 print_generic_stmt (dump_file, val, 0);
7572 return (*taken_edge_p) ? SSA_PROP_INTERESTING : SSA_PROP_VARYING;
7575 /* Searches the case label vector VEC for the index *IDX of the CASE_LABEL
7576 that includes the value VAL. The search is restricted to the range
7577 [START_IDX, n - 1] where n is the size of VEC.
7579 If there is a CASE_LABEL for VAL, its index is placed in IDX and true is
7580 returned.
7582 If there is no CASE_LABEL for VAL and there is one that is larger than VAL,
7583 it is placed in IDX and false is returned.
7585 If VAL is larger than any CASE_LABEL, n is placed on IDX and false is
7586 returned. */
7588 static bool
7589 find_case_label_index (gswitch *stmt, size_t start_idx, tree val, size_t *idx)
7591 size_t n = gimple_switch_num_labels (stmt);
7592 size_t low, high;
7594 /* Find case label for minimum of the value range or the next one.
7595 At each iteration we are searching in [low, high - 1]. */
7597 for (low = start_idx, high = n; high != low; )
7599 tree t;
7600 int cmp;
7601 /* Note that i != high, so we never ask for n. */
7602 size_t i = (high + low) / 2;
7603 t = gimple_switch_label (stmt, i);
7605 /* Cache the result of comparing CASE_LOW and val. */
7606 cmp = tree_int_cst_compare (CASE_LOW (t), val);
7608 if (cmp == 0)
7610 /* Ranges cannot be empty. */
7611 *idx = i;
7612 return true;
7614 else if (cmp > 0)
7615 high = i;
7616 else
7618 low = i + 1;
7619 if (CASE_HIGH (t) != NULL
7620 && tree_int_cst_compare (CASE_HIGH (t), val) >= 0)
7622 *idx = i;
7623 return true;
7628 *idx = high;
7629 return false;
7632 /* Searches the case label vector VEC for the range of CASE_LABELs that is used
7633 for values between MIN and MAX. The first index is placed in MIN_IDX. The
7634 last index is placed in MAX_IDX. If the range of CASE_LABELs is empty
7635 then MAX_IDX < MIN_IDX.
7636 Returns true if the default label is not needed. */
7638 static bool
7639 find_case_label_range (gswitch *stmt, tree min, tree max, size_t *min_idx,
7640 size_t *max_idx)
7642 size_t i, j;
7643 bool min_take_default = !find_case_label_index (stmt, 1, min, &i);
7644 bool max_take_default = !find_case_label_index (stmt, i, max, &j);
7646 if (i == j
7647 && min_take_default
7648 && max_take_default)
7650 /* Only the default case label reached.
7651 Return an empty range. */
7652 *min_idx = 1;
7653 *max_idx = 0;
7654 return false;
7656 else
7658 bool take_default = min_take_default || max_take_default;
7659 tree low, high;
7660 size_t k;
7662 if (max_take_default)
7663 j--;
7665 /* If the case label range is continuous, we do not need
7666 the default case label. Verify that. */
7667 high = CASE_LOW (gimple_switch_label (stmt, i));
7668 if (CASE_HIGH (gimple_switch_label (stmt, i)))
7669 high = CASE_HIGH (gimple_switch_label (stmt, i));
7670 for (k = i + 1; k <= j; ++k)
7672 low = CASE_LOW (gimple_switch_label (stmt, k));
7673 if (!integer_onep (int_const_binop (MINUS_EXPR, low, high)))
7675 take_default = true;
7676 break;
7678 high = low;
7679 if (CASE_HIGH (gimple_switch_label (stmt, k)))
7680 high = CASE_HIGH (gimple_switch_label (stmt, k));
7683 *min_idx = i;
7684 *max_idx = j;
7685 return !take_default;
7689 /* Searches the case label vector VEC for the ranges of CASE_LABELs that are
7690 used in range VR. The indices are placed in MIN_IDX1, MAX_IDX, MIN_IDX2 and
7691 MAX_IDX2. If the ranges of CASE_LABELs are empty then MAX_IDX1 < MIN_IDX1.
7692 Returns true if the default label is not needed. */
7694 static bool
7695 find_case_label_ranges (gswitch *stmt, value_range *vr, size_t *min_idx1,
7696 size_t *max_idx1, size_t *min_idx2,
7697 size_t *max_idx2)
7699 size_t i, j, k, l;
7700 unsigned int n = gimple_switch_num_labels (stmt);
7701 bool take_default;
7702 tree case_low, case_high;
7703 tree min = vr->min, max = vr->max;
7705 gcc_checking_assert (vr->type == VR_RANGE || vr->type == VR_ANTI_RANGE);
7707 take_default = !find_case_label_range (stmt, min, max, &i, &j);
7709 /* Set second range to emtpy. */
7710 *min_idx2 = 1;
7711 *max_idx2 = 0;
7713 if (vr->type == VR_RANGE)
7715 *min_idx1 = i;
7716 *max_idx1 = j;
7717 return !take_default;
7720 /* Set first range to all case labels. */
7721 *min_idx1 = 1;
7722 *max_idx1 = n - 1;
7724 if (i > j)
7725 return false;
7727 /* Make sure all the values of case labels [i , j] are contained in
7728 range [MIN, MAX]. */
7729 case_low = CASE_LOW (gimple_switch_label (stmt, i));
7730 case_high = CASE_HIGH (gimple_switch_label (stmt, j));
7731 if (tree_int_cst_compare (case_low, min) < 0)
7732 i += 1;
7733 if (case_high != NULL_TREE
7734 && tree_int_cst_compare (max, case_high) < 0)
7735 j -= 1;
7737 if (i > j)
7738 return false;
7740 /* If the range spans case labels [i, j], the corresponding anti-range spans
7741 the labels [1, i - 1] and [j + 1, n - 1]. */
7742 k = j + 1;
7743 l = n - 1;
7744 if (k > l)
7746 k = 1;
7747 l = 0;
7750 j = i - 1;
7751 i = 1;
7752 if (i > j)
7754 i = k;
7755 j = l;
7756 k = 1;
7757 l = 0;
7760 *min_idx1 = i;
7761 *max_idx1 = j;
7762 *min_idx2 = k;
7763 *max_idx2 = l;
7764 return false;
7767 /* Visit switch statement STMT. If we can determine which edge
7768 will be taken out of STMT's basic block, record it in
7769 *TAKEN_EDGE_P and return SSA_PROP_INTERESTING. Otherwise, return
7770 SSA_PROP_VARYING. */
7772 static enum ssa_prop_result
7773 vrp_visit_switch_stmt (gswitch *stmt, edge *taken_edge_p)
7775 tree op, val;
7776 value_range *vr;
7777 size_t i = 0, j = 0, k, l;
7778 bool take_default;
7780 *taken_edge_p = NULL;
7781 op = gimple_switch_index (stmt);
7782 if (TREE_CODE (op) != SSA_NAME)
7783 return SSA_PROP_VARYING;
7785 vr = get_value_range (op);
7786 if (dump_file && (dump_flags & TDF_DETAILS))
7788 fprintf (dump_file, "\nVisiting switch expression with operand ");
7789 print_generic_expr (dump_file, op, 0);
7790 fprintf (dump_file, " with known range ");
7791 dump_value_range (dump_file, vr);
7792 fprintf (dump_file, "\n");
7795 if ((vr->type != VR_RANGE
7796 && vr->type != VR_ANTI_RANGE)
7797 || symbolic_range_p (vr))
7798 return SSA_PROP_VARYING;
7800 /* Find the single edge that is taken from the switch expression. */
7801 take_default = !find_case_label_ranges (stmt, vr, &i, &j, &k, &l);
7803 /* Check if the range spans no CASE_LABEL. If so, we only reach the default
7804 label */
7805 if (j < i)
7807 gcc_assert (take_default);
7808 val = gimple_switch_default_label (stmt);
7810 else
7812 /* Check if labels with index i to j and maybe the default label
7813 are all reaching the same label. */
7815 val = gimple_switch_label (stmt, i);
7816 if (take_default
7817 && CASE_LABEL (gimple_switch_default_label (stmt))
7818 != CASE_LABEL (val))
7820 if (dump_file && (dump_flags & TDF_DETAILS))
7821 fprintf (dump_file, " not a single destination for this "
7822 "range\n");
7823 return SSA_PROP_VARYING;
7825 for (++i; i <= j; ++i)
7827 if (CASE_LABEL (gimple_switch_label (stmt, i)) != CASE_LABEL (val))
7829 if (dump_file && (dump_flags & TDF_DETAILS))
7830 fprintf (dump_file, " not a single destination for this "
7831 "range\n");
7832 return SSA_PROP_VARYING;
7835 for (; k <= l; ++k)
7837 if (CASE_LABEL (gimple_switch_label (stmt, k)) != CASE_LABEL (val))
7839 if (dump_file && (dump_flags & TDF_DETAILS))
7840 fprintf (dump_file, " not a single destination for this "
7841 "range\n");
7842 return SSA_PROP_VARYING;
7847 *taken_edge_p = find_edge (gimple_bb (stmt),
7848 label_to_block (CASE_LABEL (val)));
7850 if (dump_file && (dump_flags & TDF_DETAILS))
7852 fprintf (dump_file, " will take edge to ");
7853 print_generic_stmt (dump_file, CASE_LABEL (val), 0);
7856 return SSA_PROP_INTERESTING;
7860 /* Evaluate statement STMT. If the statement produces a useful range,
7861 return SSA_PROP_INTERESTING and record the SSA name with the
7862 interesting range into *OUTPUT_P.
7864 If STMT is a conditional branch and we can determine its truth
7865 value, the taken edge is recorded in *TAKEN_EDGE_P.
7867 If STMT produces a varying value, return SSA_PROP_VARYING. */
7869 static enum ssa_prop_result
7870 vrp_visit_stmt (gimple *stmt, edge *taken_edge_p, tree *output_p)
7872 tree def;
7873 ssa_op_iter iter;
7875 if (dump_file && (dump_flags & TDF_DETAILS))
7877 fprintf (dump_file, "\nVisiting statement:\n");
7878 print_gimple_stmt (dump_file, stmt, 0, dump_flags);
7881 if (!stmt_interesting_for_vrp (stmt))
7882 gcc_assert (stmt_ends_bb_p (stmt));
7883 else if (is_gimple_assign (stmt) || is_gimple_call (stmt))
7884 return vrp_visit_assignment_or_call (stmt, output_p);
7885 else if (gimple_code (stmt) == GIMPLE_COND)
7886 return vrp_visit_cond_stmt (as_a <gcond *> (stmt), taken_edge_p);
7887 else if (gimple_code (stmt) == GIMPLE_SWITCH)
7888 return vrp_visit_switch_stmt (as_a <gswitch *> (stmt), taken_edge_p);
7890 /* All other statements produce nothing of interest for VRP, so mark
7891 their outputs varying and prevent further simulation. */
7892 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_DEF)
7893 set_value_range_to_varying (get_value_range (def));
7895 return SSA_PROP_VARYING;
7898 /* Union the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
7899 { VR1TYPE, VR0MIN, VR0MAX } and store the result
7900 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
7901 possible such range. The resulting range is not canonicalized. */
7903 static void
7904 union_ranges (enum value_range_type *vr0type,
7905 tree *vr0min, tree *vr0max,
7906 enum value_range_type vr1type,
7907 tree vr1min, tree vr1max)
7909 bool mineq = operand_equal_p (*vr0min, vr1min, 0);
7910 bool maxeq = operand_equal_p (*vr0max, vr1max, 0);
7912 /* [] is vr0, () is vr1 in the following classification comments. */
7913 if (mineq && maxeq)
7915 /* [( )] */
7916 if (*vr0type == vr1type)
7917 /* Nothing to do for equal ranges. */
7919 else if ((*vr0type == VR_RANGE
7920 && vr1type == VR_ANTI_RANGE)
7921 || (*vr0type == VR_ANTI_RANGE
7922 && vr1type == VR_RANGE))
7924 /* For anti-range with range union the result is varying. */
7925 goto give_up;
7927 else
7928 gcc_unreachable ();
7930 else if (operand_less_p (*vr0max, vr1min) == 1
7931 || operand_less_p (vr1max, *vr0min) == 1)
7933 /* [ ] ( ) or ( ) [ ]
7934 If the ranges have an empty intersection, result of the union
7935 operation is the anti-range or if both are anti-ranges
7936 it covers all. */
7937 if (*vr0type == VR_ANTI_RANGE
7938 && vr1type == VR_ANTI_RANGE)
7939 goto give_up;
7940 else if (*vr0type == VR_ANTI_RANGE
7941 && vr1type == VR_RANGE)
7943 else if (*vr0type == VR_RANGE
7944 && vr1type == VR_ANTI_RANGE)
7946 *vr0type = vr1type;
7947 *vr0min = vr1min;
7948 *vr0max = vr1max;
7950 else if (*vr0type == VR_RANGE
7951 && vr1type == VR_RANGE)
7953 /* The result is the convex hull of both ranges. */
7954 if (operand_less_p (*vr0max, vr1min) == 1)
7956 /* If the result can be an anti-range, create one. */
7957 if (TREE_CODE (*vr0max) == INTEGER_CST
7958 && TREE_CODE (vr1min) == INTEGER_CST
7959 && vrp_val_is_min (*vr0min)
7960 && vrp_val_is_max (vr1max))
7962 tree min = int_const_binop (PLUS_EXPR,
7963 *vr0max,
7964 build_int_cst (TREE_TYPE (*vr0max), 1));
7965 tree max = int_const_binop (MINUS_EXPR,
7966 vr1min,
7967 build_int_cst (TREE_TYPE (vr1min), 1));
7968 if (!operand_less_p (max, min))
7970 *vr0type = VR_ANTI_RANGE;
7971 *vr0min = min;
7972 *vr0max = max;
7974 else
7975 *vr0max = vr1max;
7977 else
7978 *vr0max = vr1max;
7980 else
7982 /* If the result can be an anti-range, create one. */
7983 if (TREE_CODE (vr1max) == INTEGER_CST
7984 && TREE_CODE (*vr0min) == INTEGER_CST
7985 && vrp_val_is_min (vr1min)
7986 && vrp_val_is_max (*vr0max))
7988 tree min = int_const_binop (PLUS_EXPR,
7989 vr1max,
7990 build_int_cst (TREE_TYPE (vr1max), 1));
7991 tree max = int_const_binop (MINUS_EXPR,
7992 *vr0min,
7993 build_int_cst (TREE_TYPE (*vr0min), 1));
7994 if (!operand_less_p (max, min))
7996 *vr0type = VR_ANTI_RANGE;
7997 *vr0min = min;
7998 *vr0max = max;
8000 else
8001 *vr0min = vr1min;
8003 else
8004 *vr0min = vr1min;
8007 else
8008 gcc_unreachable ();
8010 else if ((maxeq || operand_less_p (vr1max, *vr0max) == 1)
8011 && (mineq || operand_less_p (*vr0min, vr1min) == 1))
8013 /* [ ( ) ] or [( ) ] or [ ( )] */
8014 if (*vr0type == VR_RANGE
8015 && vr1type == VR_RANGE)
8017 else if (*vr0type == VR_ANTI_RANGE
8018 && vr1type == VR_ANTI_RANGE)
8020 *vr0type = vr1type;
8021 *vr0min = vr1min;
8022 *vr0max = vr1max;
8024 else if (*vr0type == VR_ANTI_RANGE
8025 && vr1type == VR_RANGE)
8027 /* Arbitrarily choose the right or left gap. */
8028 if (!mineq && TREE_CODE (vr1min) == INTEGER_CST)
8029 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
8030 build_int_cst (TREE_TYPE (vr1min), 1));
8031 else if (!maxeq && TREE_CODE (vr1max) == INTEGER_CST)
8032 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
8033 build_int_cst (TREE_TYPE (vr1max), 1));
8034 else
8035 goto give_up;
8037 else if (*vr0type == VR_RANGE
8038 && vr1type == VR_ANTI_RANGE)
8039 /* The result covers everything. */
8040 goto give_up;
8041 else
8042 gcc_unreachable ();
8044 else if ((maxeq || operand_less_p (*vr0max, vr1max) == 1)
8045 && (mineq || operand_less_p (vr1min, *vr0min) == 1))
8047 /* ( [ ] ) or ([ ] ) or ( [ ]) */
8048 if (*vr0type == VR_RANGE
8049 && vr1type == VR_RANGE)
8051 *vr0type = vr1type;
8052 *vr0min = vr1min;
8053 *vr0max = vr1max;
8055 else if (*vr0type == VR_ANTI_RANGE
8056 && vr1type == VR_ANTI_RANGE)
8058 else if (*vr0type == VR_RANGE
8059 && vr1type == VR_ANTI_RANGE)
8061 *vr0type = VR_ANTI_RANGE;
8062 if (!mineq && TREE_CODE (*vr0min) == INTEGER_CST)
8064 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
8065 build_int_cst (TREE_TYPE (*vr0min), 1));
8066 *vr0min = vr1min;
8068 else if (!maxeq && TREE_CODE (*vr0max) == INTEGER_CST)
8070 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
8071 build_int_cst (TREE_TYPE (*vr0max), 1));
8072 *vr0max = vr1max;
8074 else
8075 goto give_up;
8077 else if (*vr0type == VR_ANTI_RANGE
8078 && vr1type == VR_RANGE)
8079 /* The result covers everything. */
8080 goto give_up;
8081 else
8082 gcc_unreachable ();
8084 else if ((operand_less_p (vr1min, *vr0max) == 1
8085 || operand_equal_p (vr1min, *vr0max, 0))
8086 && operand_less_p (*vr0min, vr1min) == 1
8087 && operand_less_p (*vr0max, vr1max) == 1)
8089 /* [ ( ] ) or [ ]( ) */
8090 if (*vr0type == VR_RANGE
8091 && vr1type == VR_RANGE)
8092 *vr0max = vr1max;
8093 else if (*vr0type == VR_ANTI_RANGE
8094 && vr1type == VR_ANTI_RANGE)
8095 *vr0min = vr1min;
8096 else if (*vr0type == VR_ANTI_RANGE
8097 && vr1type == VR_RANGE)
8099 if (TREE_CODE (vr1min) == INTEGER_CST)
8100 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
8101 build_int_cst (TREE_TYPE (vr1min), 1));
8102 else
8103 goto give_up;
8105 else if (*vr0type == VR_RANGE
8106 && vr1type == VR_ANTI_RANGE)
8108 if (TREE_CODE (*vr0max) == INTEGER_CST)
8110 *vr0type = vr1type;
8111 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
8112 build_int_cst (TREE_TYPE (*vr0max), 1));
8113 *vr0max = vr1max;
8115 else
8116 goto give_up;
8118 else
8119 gcc_unreachable ();
8121 else if ((operand_less_p (*vr0min, vr1max) == 1
8122 || operand_equal_p (*vr0min, vr1max, 0))
8123 && operand_less_p (vr1min, *vr0min) == 1
8124 && operand_less_p (vr1max, *vr0max) == 1)
8126 /* ( [ ) ] or ( )[ ] */
8127 if (*vr0type == VR_RANGE
8128 && vr1type == VR_RANGE)
8129 *vr0min = vr1min;
8130 else if (*vr0type == VR_ANTI_RANGE
8131 && vr1type == VR_ANTI_RANGE)
8132 *vr0max = vr1max;
8133 else if (*vr0type == VR_ANTI_RANGE
8134 && vr1type == VR_RANGE)
8136 if (TREE_CODE (vr1max) == INTEGER_CST)
8137 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
8138 build_int_cst (TREE_TYPE (vr1max), 1));
8139 else
8140 goto give_up;
8142 else if (*vr0type == VR_RANGE
8143 && vr1type == VR_ANTI_RANGE)
8145 if (TREE_CODE (*vr0min) == INTEGER_CST)
8147 *vr0type = vr1type;
8148 *vr0min = vr1min;
8149 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
8150 build_int_cst (TREE_TYPE (*vr0min), 1));
8152 else
8153 goto give_up;
8155 else
8156 gcc_unreachable ();
8158 else
8159 goto give_up;
8161 return;
8163 give_up:
8164 *vr0type = VR_VARYING;
8165 *vr0min = NULL_TREE;
8166 *vr0max = NULL_TREE;
8169 /* Intersect the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
8170 { VR1TYPE, VR0MIN, VR0MAX } and store the result
8171 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
8172 possible such range. The resulting range is not canonicalized. */
8174 static void
8175 intersect_ranges (enum value_range_type *vr0type,
8176 tree *vr0min, tree *vr0max,
8177 enum value_range_type vr1type,
8178 tree vr1min, tree vr1max)
8180 bool mineq = operand_equal_p (*vr0min, vr1min, 0);
8181 bool maxeq = operand_equal_p (*vr0max, vr1max, 0);
8183 /* [] is vr0, () is vr1 in the following classification comments. */
8184 if (mineq && maxeq)
8186 /* [( )] */
8187 if (*vr0type == vr1type)
8188 /* Nothing to do for equal ranges. */
8190 else if ((*vr0type == VR_RANGE
8191 && vr1type == VR_ANTI_RANGE)
8192 || (*vr0type == VR_ANTI_RANGE
8193 && vr1type == VR_RANGE))
8195 /* For anti-range with range intersection the result is empty. */
8196 *vr0type = VR_UNDEFINED;
8197 *vr0min = NULL_TREE;
8198 *vr0max = NULL_TREE;
8200 else
8201 gcc_unreachable ();
8203 else if (operand_less_p (*vr0max, vr1min) == 1
8204 || operand_less_p (vr1max, *vr0min) == 1)
8206 /* [ ] ( ) or ( ) [ ]
8207 If the ranges have an empty intersection, the result of the
8208 intersect operation is the range for intersecting an
8209 anti-range with a range or empty when intersecting two ranges. */
8210 if (*vr0type == VR_RANGE
8211 && vr1type == VR_ANTI_RANGE)
8213 else if (*vr0type == VR_ANTI_RANGE
8214 && vr1type == VR_RANGE)
8216 *vr0type = vr1type;
8217 *vr0min = vr1min;
8218 *vr0max = vr1max;
8220 else if (*vr0type == VR_RANGE
8221 && vr1type == VR_RANGE)
8223 *vr0type = VR_UNDEFINED;
8224 *vr0min = NULL_TREE;
8225 *vr0max = NULL_TREE;
8227 else if (*vr0type == VR_ANTI_RANGE
8228 && vr1type == VR_ANTI_RANGE)
8230 /* If the anti-ranges are adjacent to each other merge them. */
8231 if (TREE_CODE (*vr0max) == INTEGER_CST
8232 && TREE_CODE (vr1min) == INTEGER_CST
8233 && operand_less_p (*vr0max, vr1min) == 1
8234 && integer_onep (int_const_binop (MINUS_EXPR,
8235 vr1min, *vr0max)))
8236 *vr0max = vr1max;
8237 else if (TREE_CODE (vr1max) == INTEGER_CST
8238 && TREE_CODE (*vr0min) == INTEGER_CST
8239 && operand_less_p (vr1max, *vr0min) == 1
8240 && integer_onep (int_const_binop (MINUS_EXPR,
8241 *vr0min, vr1max)))
8242 *vr0min = vr1min;
8243 /* Else arbitrarily take VR0. */
8246 else if ((maxeq || operand_less_p (vr1max, *vr0max) == 1)
8247 && (mineq || operand_less_p (*vr0min, vr1min) == 1))
8249 /* [ ( ) ] or [( ) ] or [ ( )] */
8250 if (*vr0type == VR_RANGE
8251 && vr1type == VR_RANGE)
8253 /* If both are ranges the result is the inner one. */
8254 *vr0type = vr1type;
8255 *vr0min = vr1min;
8256 *vr0max = vr1max;
8258 else if (*vr0type == VR_RANGE
8259 && vr1type == VR_ANTI_RANGE)
8261 /* Choose the right gap if the left one is empty. */
8262 if (mineq)
8264 if (TREE_CODE (vr1max) == INTEGER_CST)
8265 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
8266 build_int_cst (TREE_TYPE (vr1max), 1));
8267 else
8268 *vr0min = vr1max;
8270 /* Choose the left gap if the right one is empty. */
8271 else if (maxeq)
8273 if (TREE_CODE (vr1min) == INTEGER_CST)
8274 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
8275 build_int_cst (TREE_TYPE (vr1min), 1));
8276 else
8277 *vr0max = vr1min;
8279 /* Choose the anti-range if the range is effectively varying. */
8280 else if (vrp_val_is_min (*vr0min)
8281 && vrp_val_is_max (*vr0max))
8283 *vr0type = vr1type;
8284 *vr0min = vr1min;
8285 *vr0max = vr1max;
8287 /* Else choose the range. */
8289 else if (*vr0type == VR_ANTI_RANGE
8290 && vr1type == VR_ANTI_RANGE)
8291 /* If both are anti-ranges the result is the outer one. */
8293 else if (*vr0type == VR_ANTI_RANGE
8294 && vr1type == VR_RANGE)
8296 /* The intersection is empty. */
8297 *vr0type = VR_UNDEFINED;
8298 *vr0min = NULL_TREE;
8299 *vr0max = NULL_TREE;
8301 else
8302 gcc_unreachable ();
8304 else if ((maxeq || operand_less_p (*vr0max, vr1max) == 1)
8305 && (mineq || operand_less_p (vr1min, *vr0min) == 1))
8307 /* ( [ ] ) or ([ ] ) or ( [ ]) */
8308 if (*vr0type == VR_RANGE
8309 && vr1type == VR_RANGE)
8310 /* Choose the inner range. */
8312 else if (*vr0type == VR_ANTI_RANGE
8313 && vr1type == VR_RANGE)
8315 /* Choose the right gap if the left is empty. */
8316 if (mineq)
8318 *vr0type = VR_RANGE;
8319 if (TREE_CODE (*vr0max) == INTEGER_CST)
8320 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
8321 build_int_cst (TREE_TYPE (*vr0max), 1));
8322 else
8323 *vr0min = *vr0max;
8324 *vr0max = vr1max;
8326 /* Choose the left gap if the right is empty. */
8327 else if (maxeq)
8329 *vr0type = VR_RANGE;
8330 if (TREE_CODE (*vr0min) == INTEGER_CST)
8331 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
8332 build_int_cst (TREE_TYPE (*vr0min), 1));
8333 else
8334 *vr0max = *vr0min;
8335 *vr0min = vr1min;
8337 /* Choose the anti-range if the range is effectively varying. */
8338 else if (vrp_val_is_min (vr1min)
8339 && vrp_val_is_max (vr1max))
8341 /* Else choose the range. */
8342 else
8344 *vr0type = vr1type;
8345 *vr0min = vr1min;
8346 *vr0max = vr1max;
8349 else if (*vr0type == VR_ANTI_RANGE
8350 && vr1type == VR_ANTI_RANGE)
8352 /* If both are anti-ranges the result is the outer one. */
8353 *vr0type = vr1type;
8354 *vr0min = vr1min;
8355 *vr0max = vr1max;
8357 else if (vr1type == VR_ANTI_RANGE
8358 && *vr0type == VR_RANGE)
8360 /* The intersection is empty. */
8361 *vr0type = VR_UNDEFINED;
8362 *vr0min = NULL_TREE;
8363 *vr0max = NULL_TREE;
8365 else
8366 gcc_unreachable ();
8368 else if ((operand_less_p (vr1min, *vr0max) == 1
8369 || operand_equal_p (vr1min, *vr0max, 0))
8370 && operand_less_p (*vr0min, vr1min) == 1)
8372 /* [ ( ] ) or [ ]( ) */
8373 if (*vr0type == VR_ANTI_RANGE
8374 && vr1type == VR_ANTI_RANGE)
8375 *vr0max = vr1max;
8376 else if (*vr0type == VR_RANGE
8377 && vr1type == VR_RANGE)
8378 *vr0min = vr1min;
8379 else if (*vr0type == VR_RANGE
8380 && vr1type == VR_ANTI_RANGE)
8382 if (TREE_CODE (vr1min) == INTEGER_CST)
8383 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
8384 build_int_cst (TREE_TYPE (vr1min), 1));
8385 else
8386 *vr0max = vr1min;
8388 else if (*vr0type == VR_ANTI_RANGE
8389 && vr1type == VR_RANGE)
8391 *vr0type = VR_RANGE;
8392 if (TREE_CODE (*vr0max) == INTEGER_CST)
8393 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
8394 build_int_cst (TREE_TYPE (*vr0max), 1));
8395 else
8396 *vr0min = *vr0max;
8397 *vr0max = vr1max;
8399 else
8400 gcc_unreachable ();
8402 else if ((operand_less_p (*vr0min, vr1max) == 1
8403 || operand_equal_p (*vr0min, vr1max, 0))
8404 && operand_less_p (vr1min, *vr0min) == 1)
8406 /* ( [ ) ] or ( )[ ] */
8407 if (*vr0type == VR_ANTI_RANGE
8408 && vr1type == VR_ANTI_RANGE)
8409 *vr0min = vr1min;
8410 else if (*vr0type == VR_RANGE
8411 && vr1type == VR_RANGE)
8412 *vr0max = vr1max;
8413 else if (*vr0type == VR_RANGE
8414 && vr1type == VR_ANTI_RANGE)
8416 if (TREE_CODE (vr1max) == INTEGER_CST)
8417 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
8418 build_int_cst (TREE_TYPE (vr1max), 1));
8419 else
8420 *vr0min = vr1max;
8422 else if (*vr0type == VR_ANTI_RANGE
8423 && vr1type == VR_RANGE)
8425 *vr0type = VR_RANGE;
8426 if (TREE_CODE (*vr0min) == INTEGER_CST)
8427 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
8428 build_int_cst (TREE_TYPE (*vr0min), 1));
8429 else
8430 *vr0max = *vr0min;
8431 *vr0min = vr1min;
8433 else
8434 gcc_unreachable ();
8437 /* As a fallback simply use { *VRTYPE, *VR0MIN, *VR0MAX } as
8438 result for the intersection. That's always a conservative
8439 correct estimate. */
8441 return;
8445 /* Intersect the two value-ranges *VR0 and *VR1 and store the result
8446 in *VR0. This may not be the smallest possible such range. */
8448 static void
8449 vrp_intersect_ranges_1 (value_range *vr0, value_range *vr1)
8451 value_range saved;
8453 /* If either range is VR_VARYING the other one wins. */
8454 if (vr1->type == VR_VARYING)
8455 return;
8456 if (vr0->type == VR_VARYING)
8458 copy_value_range (vr0, vr1);
8459 return;
8462 /* When either range is VR_UNDEFINED the resulting range is
8463 VR_UNDEFINED, too. */
8464 if (vr0->type == VR_UNDEFINED)
8465 return;
8466 if (vr1->type == VR_UNDEFINED)
8468 set_value_range_to_undefined (vr0);
8469 return;
8472 /* Save the original vr0 so we can return it as conservative intersection
8473 result when our worker turns things to varying. */
8474 saved = *vr0;
8475 intersect_ranges (&vr0->type, &vr0->min, &vr0->max,
8476 vr1->type, vr1->min, vr1->max);
8477 /* Make sure to canonicalize the result though as the inversion of a
8478 VR_RANGE can still be a VR_RANGE. */
8479 set_and_canonicalize_value_range (vr0, vr0->type,
8480 vr0->min, vr0->max, vr0->equiv);
8481 /* If that failed, use the saved original VR0. */
8482 if (vr0->type == VR_VARYING)
8484 *vr0 = saved;
8485 return;
8487 /* If the result is VR_UNDEFINED there is no need to mess with
8488 the equivalencies. */
8489 if (vr0->type == VR_UNDEFINED)
8490 return;
8492 /* The resulting set of equivalences for range intersection is the union of
8493 the two sets. */
8494 if (vr0->equiv && vr1->equiv && vr0->equiv != vr1->equiv)
8495 bitmap_ior_into (vr0->equiv, vr1->equiv);
8496 else if (vr1->equiv && !vr0->equiv)
8497 bitmap_copy (vr0->equiv, vr1->equiv);
8500 static void
8501 vrp_intersect_ranges (value_range *vr0, value_range *vr1)
8503 if (dump_file && (dump_flags & TDF_DETAILS))
8505 fprintf (dump_file, "Intersecting\n ");
8506 dump_value_range (dump_file, vr0);
8507 fprintf (dump_file, "\nand\n ");
8508 dump_value_range (dump_file, vr1);
8509 fprintf (dump_file, "\n");
8511 vrp_intersect_ranges_1 (vr0, vr1);
8512 if (dump_file && (dump_flags & TDF_DETAILS))
8514 fprintf (dump_file, "to\n ");
8515 dump_value_range (dump_file, vr0);
8516 fprintf (dump_file, "\n");
8520 /* Meet operation for value ranges. Given two value ranges VR0 and
8521 VR1, store in VR0 a range that contains both VR0 and VR1. This
8522 may not be the smallest possible such range. */
8524 static void
8525 vrp_meet_1 (value_range *vr0, value_range *vr1)
8527 value_range saved;
8529 if (vr0->type == VR_UNDEFINED)
8531 set_value_range (vr0, vr1->type, vr1->min, vr1->max, vr1->equiv);
8532 return;
8535 if (vr1->type == VR_UNDEFINED)
8537 /* VR0 already has the resulting range. */
8538 return;
8541 if (vr0->type == VR_VARYING)
8543 /* Nothing to do. VR0 already has the resulting range. */
8544 return;
8547 if (vr1->type == VR_VARYING)
8549 set_value_range_to_varying (vr0);
8550 return;
8553 saved = *vr0;
8554 union_ranges (&vr0->type, &vr0->min, &vr0->max,
8555 vr1->type, vr1->min, vr1->max);
8556 if (vr0->type == VR_VARYING)
8558 /* Failed to find an efficient meet. Before giving up and setting
8559 the result to VARYING, see if we can at least derive a useful
8560 anti-range. FIXME, all this nonsense about distinguishing
8561 anti-ranges from ranges is necessary because of the odd
8562 semantics of range_includes_zero_p and friends. */
8563 if (((saved.type == VR_RANGE
8564 && range_includes_zero_p (saved.min, saved.max) == 0)
8565 || (saved.type == VR_ANTI_RANGE
8566 && range_includes_zero_p (saved.min, saved.max) == 1))
8567 && ((vr1->type == VR_RANGE
8568 && range_includes_zero_p (vr1->min, vr1->max) == 0)
8569 || (vr1->type == VR_ANTI_RANGE
8570 && range_includes_zero_p (vr1->min, vr1->max) == 1)))
8572 set_value_range_to_nonnull (vr0, TREE_TYPE (saved.min));
8574 /* Since this meet operation did not result from the meeting of
8575 two equivalent names, VR0 cannot have any equivalences. */
8576 if (vr0->equiv)
8577 bitmap_clear (vr0->equiv);
8578 return;
8581 set_value_range_to_varying (vr0);
8582 return;
8584 set_and_canonicalize_value_range (vr0, vr0->type, vr0->min, vr0->max,
8585 vr0->equiv);
8586 if (vr0->type == VR_VARYING)
8587 return;
8589 /* The resulting set of equivalences is always the intersection of
8590 the two sets. */
8591 if (vr0->equiv && vr1->equiv && vr0->equiv != vr1->equiv)
8592 bitmap_and_into (vr0->equiv, vr1->equiv);
8593 else if (vr0->equiv && !vr1->equiv)
8594 bitmap_clear (vr0->equiv);
8597 static void
8598 vrp_meet (value_range *vr0, value_range *vr1)
8600 if (dump_file && (dump_flags & TDF_DETAILS))
8602 fprintf (dump_file, "Meeting\n ");
8603 dump_value_range (dump_file, vr0);
8604 fprintf (dump_file, "\nand\n ");
8605 dump_value_range (dump_file, vr1);
8606 fprintf (dump_file, "\n");
8608 vrp_meet_1 (vr0, vr1);
8609 if (dump_file && (dump_flags & TDF_DETAILS))
8611 fprintf (dump_file, "to\n ");
8612 dump_value_range (dump_file, vr0);
8613 fprintf (dump_file, "\n");
8618 /* Visit all arguments for PHI node PHI that flow through executable
8619 edges. If a valid value range can be derived from all the incoming
8620 value ranges, set a new range for the LHS of PHI. */
8622 static enum ssa_prop_result
8623 vrp_visit_phi_node (gphi *phi)
8625 size_t i;
8626 tree lhs = PHI_RESULT (phi);
8627 value_range *lhs_vr = get_value_range (lhs);
8628 value_range vr_result = VR_INITIALIZER;
8629 bool first = true;
8630 int edges, old_edges;
8631 struct loop *l;
8633 if (dump_file && (dump_flags & TDF_DETAILS))
8635 fprintf (dump_file, "\nVisiting PHI node: ");
8636 print_gimple_stmt (dump_file, phi, 0, dump_flags);
8639 edges = 0;
8640 for (i = 0; i < gimple_phi_num_args (phi); i++)
8642 edge e = gimple_phi_arg_edge (phi, i);
8644 if (dump_file && (dump_flags & TDF_DETAILS))
8646 fprintf (dump_file,
8647 " Argument #%d (%d -> %d %sexecutable)\n",
8648 (int) i, e->src->index, e->dest->index,
8649 (e->flags & EDGE_EXECUTABLE) ? "" : "not ");
8652 if (e->flags & EDGE_EXECUTABLE)
8654 tree arg = PHI_ARG_DEF (phi, i);
8655 value_range vr_arg;
8657 ++edges;
8659 if (TREE_CODE (arg) == SSA_NAME)
8661 vr_arg = *(get_value_range (arg));
8662 /* Do not allow equivalences or symbolic ranges to leak in from
8663 backedges. That creates invalid equivalencies.
8664 See PR53465 and PR54767. */
8665 if (e->flags & EDGE_DFS_BACK)
8667 if (vr_arg.type == VR_RANGE
8668 || vr_arg.type == VR_ANTI_RANGE)
8670 vr_arg.equiv = NULL;
8671 if (symbolic_range_p (&vr_arg))
8673 vr_arg.type = VR_VARYING;
8674 vr_arg.min = NULL_TREE;
8675 vr_arg.max = NULL_TREE;
8679 else
8681 /* If the non-backedge arguments range is VR_VARYING then
8682 we can still try recording a simple equivalence. */
8683 if (vr_arg.type == VR_VARYING)
8685 vr_arg.type = VR_RANGE;
8686 vr_arg.min = arg;
8687 vr_arg.max = arg;
8688 vr_arg.equiv = NULL;
8692 else
8694 if (TREE_OVERFLOW_P (arg))
8695 arg = drop_tree_overflow (arg);
8697 vr_arg.type = VR_RANGE;
8698 vr_arg.min = arg;
8699 vr_arg.max = arg;
8700 vr_arg.equiv = NULL;
8703 if (dump_file && (dump_flags & TDF_DETAILS))
8705 fprintf (dump_file, "\t");
8706 print_generic_expr (dump_file, arg, dump_flags);
8707 fprintf (dump_file, ": ");
8708 dump_value_range (dump_file, &vr_arg);
8709 fprintf (dump_file, "\n");
8712 if (first)
8713 copy_value_range (&vr_result, &vr_arg);
8714 else
8715 vrp_meet (&vr_result, &vr_arg);
8716 first = false;
8718 if (vr_result.type == VR_VARYING)
8719 break;
8723 if (vr_result.type == VR_VARYING)
8724 goto varying;
8725 else if (vr_result.type == VR_UNDEFINED)
8726 goto update_range;
8728 old_edges = vr_phi_edge_counts[SSA_NAME_VERSION (lhs)];
8729 vr_phi_edge_counts[SSA_NAME_VERSION (lhs)] = edges;
8731 /* To prevent infinite iterations in the algorithm, derive ranges
8732 when the new value is slightly bigger or smaller than the
8733 previous one. We don't do this if we have seen a new executable
8734 edge; this helps us avoid an overflow infinity for conditionals
8735 which are not in a loop. If the old value-range was VR_UNDEFINED
8736 use the updated range and iterate one more time. */
8737 if (edges > 0
8738 && gimple_phi_num_args (phi) > 1
8739 && edges == old_edges
8740 && lhs_vr->type != VR_UNDEFINED)
8742 /* Compare old and new ranges, fall back to varying if the
8743 values are not comparable. */
8744 int cmp_min = compare_values (lhs_vr->min, vr_result.min);
8745 if (cmp_min == -2)
8746 goto varying;
8747 int cmp_max = compare_values (lhs_vr->max, vr_result.max);
8748 if (cmp_max == -2)
8749 goto varying;
8751 /* For non VR_RANGE or for pointers fall back to varying if
8752 the range changed. */
8753 if ((lhs_vr->type != VR_RANGE || vr_result.type != VR_RANGE
8754 || POINTER_TYPE_P (TREE_TYPE (lhs)))
8755 && (cmp_min != 0 || cmp_max != 0))
8756 goto varying;
8758 /* If the new minimum is larger than the previous one
8759 retain the old value. If the new minimum value is smaller
8760 than the previous one and not -INF go all the way to -INF + 1.
8761 In the first case, to avoid infinite bouncing between different
8762 minimums, and in the other case to avoid iterating millions of
8763 times to reach -INF. Going to -INF + 1 also lets the following
8764 iteration compute whether there will be any overflow, at the
8765 expense of one additional iteration. */
8766 if (cmp_min < 0)
8767 vr_result.min = lhs_vr->min;
8768 else if (cmp_min > 0
8769 && !vrp_val_is_min (vr_result.min))
8770 vr_result.min
8771 = int_const_binop (PLUS_EXPR,
8772 vrp_val_min (TREE_TYPE (vr_result.min)),
8773 build_int_cst (TREE_TYPE (vr_result.min), 1));
8775 /* Similarly for the maximum value. */
8776 if (cmp_max > 0)
8777 vr_result.max = lhs_vr->max;
8778 else if (cmp_max < 0
8779 && !vrp_val_is_max (vr_result.max))
8780 vr_result.max
8781 = int_const_binop (MINUS_EXPR,
8782 vrp_val_max (TREE_TYPE (vr_result.min)),
8783 build_int_cst (TREE_TYPE (vr_result.min), 1));
8785 /* If we dropped either bound to +-INF then if this is a loop
8786 PHI node SCEV may known more about its value-range. */
8787 if (cmp_min > 0 || cmp_min < 0
8788 || cmp_max < 0 || cmp_max > 0)
8789 goto scev_check;
8791 goto infinite_check;
8794 /* If the new range is different than the previous value, keep
8795 iterating. */
8796 update_range:
8797 if (update_value_range (lhs, &vr_result))
8799 if (dump_file && (dump_flags & TDF_DETAILS))
8801 fprintf (dump_file, "Found new range for ");
8802 print_generic_expr (dump_file, lhs, 0);
8803 fprintf (dump_file, ": ");
8804 dump_value_range (dump_file, &vr_result);
8805 fprintf (dump_file, "\n");
8808 if (vr_result.type == VR_VARYING)
8809 return SSA_PROP_VARYING;
8811 return SSA_PROP_INTERESTING;
8814 /* Nothing changed, don't add outgoing edges. */
8815 return SSA_PROP_NOT_INTERESTING;
8817 varying:
8818 set_value_range_to_varying (&vr_result);
8820 scev_check:
8821 /* If this is a loop PHI node SCEV may known more about its value-range.
8822 scev_check can be reached from two paths, one is a fall through from above
8823 "varying" label, the other is direct goto from code block which tries to
8824 avoid infinite simulation. */
8825 if ((l = loop_containing_stmt (phi))
8826 && l->header == gimple_bb (phi))
8827 adjust_range_with_scev (&vr_result, l, phi, lhs);
8829 infinite_check:
8830 /* If we will end up with a (-INF, +INF) range, set it to
8831 VARYING. Same if the previous max value was invalid for
8832 the type and we end up with vr_result.min > vr_result.max. */
8833 if ((vr_result.type == VR_RANGE || vr_result.type == VR_ANTI_RANGE)
8834 && !((vrp_val_is_max (vr_result.max) && vrp_val_is_min (vr_result.min))
8835 || compare_values (vr_result.min, vr_result.max) > 0))
8836 goto update_range;
8838 /* No match found. Set the LHS to VARYING. */
8839 set_value_range_to_varying (lhs_vr);
8840 return SSA_PROP_VARYING;
8843 /* Simplify boolean operations if the source is known
8844 to be already a boolean. */
8845 static bool
8846 simplify_truth_ops_using_ranges (gimple_stmt_iterator *gsi, gimple *stmt)
8848 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
8849 tree lhs, op0, op1;
8850 bool need_conversion;
8852 /* We handle only !=/== case here. */
8853 gcc_assert (rhs_code == EQ_EXPR || rhs_code == NE_EXPR);
8855 op0 = gimple_assign_rhs1 (stmt);
8856 if (!op_with_boolean_value_range_p (op0))
8857 return false;
8859 op1 = gimple_assign_rhs2 (stmt);
8860 if (!op_with_boolean_value_range_p (op1))
8861 return false;
8863 /* Reduce number of cases to handle to NE_EXPR. As there is no
8864 BIT_XNOR_EXPR we cannot replace A == B with a single statement. */
8865 if (rhs_code == EQ_EXPR)
8867 if (TREE_CODE (op1) == INTEGER_CST)
8868 op1 = int_const_binop (BIT_XOR_EXPR, op1,
8869 build_int_cst (TREE_TYPE (op1), 1));
8870 else
8871 return false;
8874 lhs = gimple_assign_lhs (stmt);
8875 need_conversion
8876 = !useless_type_conversion_p (TREE_TYPE (lhs), TREE_TYPE (op0));
8878 /* Make sure to not sign-extend a 1-bit 1 when converting the result. */
8879 if (need_conversion
8880 && !TYPE_UNSIGNED (TREE_TYPE (op0))
8881 && TYPE_PRECISION (TREE_TYPE (op0)) == 1
8882 && TYPE_PRECISION (TREE_TYPE (lhs)) > 1)
8883 return false;
8885 /* For A != 0 we can substitute A itself. */
8886 if (integer_zerop (op1))
8887 gimple_assign_set_rhs_with_ops (gsi,
8888 need_conversion
8889 ? NOP_EXPR : TREE_CODE (op0), op0);
8890 /* For A != B we substitute A ^ B. Either with conversion. */
8891 else if (need_conversion)
8893 tree tem = make_ssa_name (TREE_TYPE (op0));
8894 gassign *newop
8895 = gimple_build_assign (tem, BIT_XOR_EXPR, op0, op1);
8896 gsi_insert_before (gsi, newop, GSI_SAME_STMT);
8897 if (INTEGRAL_TYPE_P (TREE_TYPE (tem))
8898 && TYPE_PRECISION (TREE_TYPE (tem)) > 1)
8899 set_range_info (tem, VR_RANGE,
8900 wi::zero (TYPE_PRECISION (TREE_TYPE (tem))),
8901 wi::one (TYPE_PRECISION (TREE_TYPE (tem))));
8902 gimple_assign_set_rhs_with_ops (gsi, NOP_EXPR, tem);
8904 /* Or without. */
8905 else
8906 gimple_assign_set_rhs_with_ops (gsi, BIT_XOR_EXPR, op0, op1);
8907 update_stmt (gsi_stmt (*gsi));
8909 return true;
8912 /* Simplify a division or modulo operator to a right shift or
8913 bitwise and if the first operand is unsigned or is greater
8914 than zero and the second operand is an exact power of two.
8915 For TRUNC_MOD_EXPR op0 % op1 with constant op1, optimize it
8916 into just op0 if op0's range is known to be a subset of
8917 [-op1 + 1, op1 - 1] for signed and [0, op1 - 1] for unsigned
8918 modulo. */
8920 static bool
8921 simplify_div_or_mod_using_ranges (gimple_stmt_iterator *gsi, gimple *stmt)
8923 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
8924 tree val = NULL;
8925 tree op0 = gimple_assign_rhs1 (stmt);
8926 tree op1 = gimple_assign_rhs2 (stmt);
8927 value_range *vr = get_value_range (op0);
8929 if (rhs_code == TRUNC_MOD_EXPR
8930 && TREE_CODE (op1) == INTEGER_CST
8931 && tree_int_cst_sgn (op1) == 1
8932 && range_int_cst_p (vr)
8933 && tree_int_cst_lt (vr->max, op1))
8935 if (TYPE_UNSIGNED (TREE_TYPE (op0))
8936 || tree_int_cst_sgn (vr->min) >= 0
8937 || tree_int_cst_lt (fold_unary (NEGATE_EXPR, TREE_TYPE (op1), op1),
8938 vr->min))
8940 /* If op0 already has the range op0 % op1 has,
8941 then TRUNC_MOD_EXPR won't change anything. */
8942 gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
8943 gimple_assign_set_rhs_from_tree (&gsi, op0);
8944 update_stmt (stmt);
8945 return true;
8949 if (!integer_pow2p (op1))
8951 /* X % -Y can be only optimized into X % Y either if
8952 X is not INT_MIN, or Y is not -1. Fold it now, as after
8953 remove_range_assertions the range info might be not available
8954 anymore. */
8955 if (rhs_code == TRUNC_MOD_EXPR
8956 && fold_stmt (gsi, follow_single_use_edges))
8957 return true;
8958 return false;
8961 if (TYPE_UNSIGNED (TREE_TYPE (op0)))
8962 val = integer_one_node;
8963 else
8965 bool sop = false;
8967 val = compare_range_with_value (GE_EXPR, vr, integer_zero_node, &sop);
8969 if (val
8970 && sop
8971 && integer_onep (val)
8972 && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC))
8974 location_t location;
8976 if (!gimple_has_location (stmt))
8977 location = input_location;
8978 else
8979 location = gimple_location (stmt);
8980 warning_at (location, OPT_Wstrict_overflow,
8981 "assuming signed overflow does not occur when "
8982 "simplifying %</%> or %<%%%> to %<>>%> or %<&%>");
8986 if (val && integer_onep (val))
8988 tree t;
8990 if (rhs_code == TRUNC_DIV_EXPR)
8992 t = build_int_cst (integer_type_node, tree_log2 (op1));
8993 gimple_assign_set_rhs_code (stmt, RSHIFT_EXPR);
8994 gimple_assign_set_rhs1 (stmt, op0);
8995 gimple_assign_set_rhs2 (stmt, t);
8997 else
8999 t = build_int_cst (TREE_TYPE (op1), 1);
9000 t = int_const_binop (MINUS_EXPR, op1, t);
9001 t = fold_convert (TREE_TYPE (op0), t);
9003 gimple_assign_set_rhs_code (stmt, BIT_AND_EXPR);
9004 gimple_assign_set_rhs1 (stmt, op0);
9005 gimple_assign_set_rhs2 (stmt, t);
9008 update_stmt (stmt);
9009 return true;
9012 return false;
9015 /* Simplify a min or max if the ranges of the two operands are
9016 disjoint. Return true if we do simplify. */
9018 static bool
9019 simplify_min_or_max_using_ranges (gimple *stmt)
9021 tree op0 = gimple_assign_rhs1 (stmt);
9022 tree op1 = gimple_assign_rhs2 (stmt);
9023 bool sop = false;
9024 tree val;
9026 val = (vrp_evaluate_conditional_warnv_with_ops_using_ranges
9027 (LE_EXPR, op0, op1, &sop));
9028 if (!val)
9030 sop = false;
9031 val = (vrp_evaluate_conditional_warnv_with_ops_using_ranges
9032 (LT_EXPR, op0, op1, &sop));
9035 if (val)
9037 if (sop && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC))
9039 location_t location;
9041 if (!gimple_has_location (stmt))
9042 location = input_location;
9043 else
9044 location = gimple_location (stmt);
9045 warning_at (location, OPT_Wstrict_overflow,
9046 "assuming signed overflow does not occur when "
9047 "simplifying %<min/max (X,Y)%> to %<X%> or %<Y%>");
9050 /* VAL == TRUE -> OP0 < or <= op1
9051 VAL == FALSE -> OP0 > or >= op1. */
9052 tree res = ((gimple_assign_rhs_code (stmt) == MAX_EXPR)
9053 == integer_zerop (val)) ? op0 : op1;
9054 gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
9055 gimple_assign_set_rhs_from_tree (&gsi, res);
9056 update_stmt (stmt);
9057 return true;
9060 return false;
9063 /* If the operand to an ABS_EXPR is >= 0, then eliminate the
9064 ABS_EXPR. If the operand is <= 0, then simplify the
9065 ABS_EXPR into a NEGATE_EXPR. */
9067 static bool
9068 simplify_abs_using_ranges (gimple *stmt)
9070 tree op = gimple_assign_rhs1 (stmt);
9071 value_range *vr = get_value_range (op);
9073 if (vr)
9075 tree val = NULL;
9076 bool sop = false;
9078 val = compare_range_with_value (LE_EXPR, vr, integer_zero_node, &sop);
9079 if (!val)
9081 /* The range is neither <= 0 nor > 0. Now see if it is
9082 either < 0 or >= 0. */
9083 sop = false;
9084 val = compare_range_with_value (LT_EXPR, vr, integer_zero_node,
9085 &sop);
9088 if (val)
9090 if (sop && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC))
9092 location_t location;
9094 if (!gimple_has_location (stmt))
9095 location = input_location;
9096 else
9097 location = gimple_location (stmt);
9098 warning_at (location, OPT_Wstrict_overflow,
9099 "assuming signed overflow does not occur when "
9100 "simplifying %<abs (X)%> to %<X%> or %<-X%>");
9103 gimple_assign_set_rhs1 (stmt, op);
9104 if (integer_zerop (val))
9105 gimple_assign_set_rhs_code (stmt, SSA_NAME);
9106 else
9107 gimple_assign_set_rhs_code (stmt, NEGATE_EXPR);
9108 update_stmt (stmt);
9109 return true;
9113 return false;
9116 /* Optimize away redundant BIT_AND_EXPR and BIT_IOR_EXPR.
9117 If all the bits that are being cleared by & are already
9118 known to be zero from VR, or all the bits that are being
9119 set by | are already known to be one from VR, the bit
9120 operation is redundant. */
9122 static bool
9123 simplify_bit_ops_using_ranges (gimple_stmt_iterator *gsi, gimple *stmt)
9125 tree op0 = gimple_assign_rhs1 (stmt);
9126 tree op1 = gimple_assign_rhs2 (stmt);
9127 tree op = NULL_TREE;
9128 value_range vr0 = VR_INITIALIZER;
9129 value_range vr1 = VR_INITIALIZER;
9130 wide_int may_be_nonzero0, may_be_nonzero1;
9131 wide_int must_be_nonzero0, must_be_nonzero1;
9132 wide_int mask;
9134 if (TREE_CODE (op0) == SSA_NAME)
9135 vr0 = *(get_value_range (op0));
9136 else if (is_gimple_min_invariant (op0))
9137 set_value_range_to_value (&vr0, op0, NULL);
9138 else
9139 return false;
9141 if (TREE_CODE (op1) == SSA_NAME)
9142 vr1 = *(get_value_range (op1));
9143 else if (is_gimple_min_invariant (op1))
9144 set_value_range_to_value (&vr1, op1, NULL);
9145 else
9146 return false;
9148 if (!zero_nonzero_bits_from_vr (TREE_TYPE (op0), &vr0, &may_be_nonzero0,
9149 &must_be_nonzero0))
9150 return false;
9151 if (!zero_nonzero_bits_from_vr (TREE_TYPE (op1), &vr1, &may_be_nonzero1,
9152 &must_be_nonzero1))
9153 return false;
9155 switch (gimple_assign_rhs_code (stmt))
9157 case BIT_AND_EXPR:
9158 mask = may_be_nonzero0.and_not (must_be_nonzero1);
9159 if (mask == 0)
9161 op = op0;
9162 break;
9164 mask = may_be_nonzero1.and_not (must_be_nonzero0);
9165 if (mask == 0)
9167 op = op1;
9168 break;
9170 break;
9171 case BIT_IOR_EXPR:
9172 mask = may_be_nonzero0.and_not (must_be_nonzero1);
9173 if (mask == 0)
9175 op = op1;
9176 break;
9178 mask = may_be_nonzero1.and_not (must_be_nonzero0);
9179 if (mask == 0)
9181 op = op0;
9182 break;
9184 break;
9185 default:
9186 gcc_unreachable ();
9189 if (op == NULL_TREE)
9190 return false;
9192 gimple_assign_set_rhs_with_ops (gsi, TREE_CODE (op), op);
9193 update_stmt (gsi_stmt (*gsi));
9194 return true;
9197 /* We are comparing trees OP0 and OP1 using COND_CODE. OP0 has
9198 a known value range VR.
9200 If there is one and only one value which will satisfy the
9201 conditional, then return that value. Else return NULL.
9203 If signed overflow must be undefined for the value to satisfy
9204 the conditional, then set *STRICT_OVERFLOW_P to true. */
9206 static tree
9207 test_for_singularity (enum tree_code cond_code, tree op0,
9208 tree op1, value_range *vr,
9209 bool *strict_overflow_p)
9211 tree min = NULL;
9212 tree max = NULL;
9214 /* Extract minimum/maximum values which satisfy the conditional as it was
9215 written. */
9216 if (cond_code == LE_EXPR || cond_code == LT_EXPR)
9218 /* This should not be negative infinity; there is no overflow
9219 here. */
9220 min = TYPE_MIN_VALUE (TREE_TYPE (op0));
9222 max = op1;
9223 if (cond_code == LT_EXPR && !is_overflow_infinity (max))
9225 tree one = build_int_cst (TREE_TYPE (op0), 1);
9226 max = fold_build2 (MINUS_EXPR, TREE_TYPE (op0), max, one);
9227 if (EXPR_P (max))
9228 TREE_NO_WARNING (max) = 1;
9231 else if (cond_code == GE_EXPR || cond_code == GT_EXPR)
9233 /* This should not be positive infinity; there is no overflow
9234 here. */
9235 max = TYPE_MAX_VALUE (TREE_TYPE (op0));
9237 min = op1;
9238 if (cond_code == GT_EXPR && !is_overflow_infinity (min))
9240 tree one = build_int_cst (TREE_TYPE (op0), 1);
9241 min = fold_build2 (PLUS_EXPR, TREE_TYPE (op0), min, one);
9242 if (EXPR_P (min))
9243 TREE_NO_WARNING (min) = 1;
9247 /* Now refine the minimum and maximum values using any
9248 value range information we have for op0. */
9249 if (min && max)
9251 if (compare_values (vr->min, min) == 1)
9252 min = vr->min;
9253 if (compare_values (vr->max, max) == -1)
9254 max = vr->max;
9256 /* If the new min/max values have converged to a single value,
9257 then there is only one value which can satisfy the condition,
9258 return that value. */
9259 if (operand_equal_p (min, max, 0) && is_gimple_min_invariant (min))
9261 if ((cond_code == LE_EXPR || cond_code == LT_EXPR)
9262 && is_overflow_infinity (vr->max))
9263 *strict_overflow_p = true;
9264 if ((cond_code == GE_EXPR || cond_code == GT_EXPR)
9265 && is_overflow_infinity (vr->min))
9266 *strict_overflow_p = true;
9268 return min;
9271 return NULL;
9274 /* Return whether the value range *VR fits in an integer type specified
9275 by PRECISION and UNSIGNED_P. */
9277 static bool
9278 range_fits_type_p (value_range *vr, unsigned dest_precision, signop dest_sgn)
9280 tree src_type;
9281 unsigned src_precision;
9282 widest_int tem;
9283 signop src_sgn;
9285 /* We can only handle integral and pointer types. */
9286 src_type = TREE_TYPE (vr->min);
9287 if (!INTEGRAL_TYPE_P (src_type)
9288 && !POINTER_TYPE_P (src_type))
9289 return false;
9291 /* An extension is fine unless VR is SIGNED and dest_sgn is UNSIGNED,
9292 and so is an identity transform. */
9293 src_precision = TYPE_PRECISION (TREE_TYPE (vr->min));
9294 src_sgn = TYPE_SIGN (src_type);
9295 if ((src_precision < dest_precision
9296 && !(dest_sgn == UNSIGNED && src_sgn == SIGNED))
9297 || (src_precision == dest_precision && src_sgn == dest_sgn))
9298 return true;
9300 /* Now we can only handle ranges with constant bounds. */
9301 if (vr->type != VR_RANGE
9302 || TREE_CODE (vr->min) != INTEGER_CST
9303 || TREE_CODE (vr->max) != INTEGER_CST)
9304 return false;
9306 /* For sign changes, the MSB of the wide_int has to be clear.
9307 An unsigned value with its MSB set cannot be represented by
9308 a signed wide_int, while a negative value cannot be represented
9309 by an unsigned wide_int. */
9310 if (src_sgn != dest_sgn
9311 && (wi::lts_p (vr->min, 0) || wi::lts_p (vr->max, 0)))
9312 return false;
9314 /* Then we can perform the conversion on both ends and compare
9315 the result for equality. */
9316 tem = wi::ext (wi::to_widest (vr->min), dest_precision, dest_sgn);
9317 if (tem != wi::to_widest (vr->min))
9318 return false;
9319 tem = wi::ext (wi::to_widest (vr->max), dest_precision, dest_sgn);
9320 if (tem != wi::to_widest (vr->max))
9321 return false;
9323 return true;
9326 /* Simplify a conditional using a relational operator to an equality
9327 test if the range information indicates only one value can satisfy
9328 the original conditional. */
9330 static bool
9331 simplify_cond_using_ranges (gcond *stmt)
9333 tree op0 = gimple_cond_lhs (stmt);
9334 tree op1 = gimple_cond_rhs (stmt);
9335 enum tree_code cond_code = gimple_cond_code (stmt);
9337 if (cond_code != NE_EXPR
9338 && cond_code != EQ_EXPR
9339 && TREE_CODE (op0) == SSA_NAME
9340 && INTEGRAL_TYPE_P (TREE_TYPE (op0))
9341 && is_gimple_min_invariant (op1))
9343 value_range *vr = get_value_range (op0);
9345 /* If we have range information for OP0, then we might be
9346 able to simplify this conditional. */
9347 if (vr->type == VR_RANGE)
9349 enum warn_strict_overflow_code wc = WARN_STRICT_OVERFLOW_COMPARISON;
9350 bool sop = false;
9351 tree new_tree = test_for_singularity (cond_code, op0, op1, vr, &sop);
9353 if (new_tree
9354 && (!sop || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0))))
9356 if (dump_file)
9358 fprintf (dump_file, "Simplified relational ");
9359 print_gimple_stmt (dump_file, stmt, 0, 0);
9360 fprintf (dump_file, " into ");
9363 gimple_cond_set_code (stmt, EQ_EXPR);
9364 gimple_cond_set_lhs (stmt, op0);
9365 gimple_cond_set_rhs (stmt, new_tree);
9367 update_stmt (stmt);
9369 if (dump_file)
9371 print_gimple_stmt (dump_file, stmt, 0, 0);
9372 fprintf (dump_file, "\n");
9375 if (sop && issue_strict_overflow_warning (wc))
9377 location_t location = input_location;
9378 if (gimple_has_location (stmt))
9379 location = gimple_location (stmt);
9381 warning_at (location, OPT_Wstrict_overflow,
9382 "assuming signed overflow does not occur when "
9383 "simplifying conditional");
9386 return true;
9389 /* Try again after inverting the condition. We only deal
9390 with integral types here, so no need to worry about
9391 issues with inverting FP comparisons. */
9392 sop = false;
9393 new_tree = test_for_singularity
9394 (invert_tree_comparison (cond_code, false),
9395 op0, op1, vr, &sop);
9397 if (new_tree
9398 && (!sop || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0))))
9400 if (dump_file)
9402 fprintf (dump_file, "Simplified relational ");
9403 print_gimple_stmt (dump_file, stmt, 0, 0);
9404 fprintf (dump_file, " into ");
9407 gimple_cond_set_code (stmt, NE_EXPR);
9408 gimple_cond_set_lhs (stmt, op0);
9409 gimple_cond_set_rhs (stmt, new_tree);
9411 update_stmt (stmt);
9413 if (dump_file)
9415 print_gimple_stmt (dump_file, stmt, 0, 0);
9416 fprintf (dump_file, "\n");
9419 if (sop && issue_strict_overflow_warning (wc))
9421 location_t location = input_location;
9422 if (gimple_has_location (stmt))
9423 location = gimple_location (stmt);
9425 warning_at (location, OPT_Wstrict_overflow,
9426 "assuming signed overflow does not occur when "
9427 "simplifying conditional");
9430 return true;
9435 /* If we have a comparison of an SSA_NAME (OP0) against a constant,
9436 see if OP0 was set by a type conversion where the source of
9437 the conversion is another SSA_NAME with a range that fits
9438 into the range of OP0's type.
9440 If so, the conversion is redundant as the earlier SSA_NAME can be
9441 used for the comparison directly if we just massage the constant in the
9442 comparison. */
9443 if (TREE_CODE (op0) == SSA_NAME
9444 && TREE_CODE (op1) == INTEGER_CST)
9446 gimple *def_stmt = SSA_NAME_DEF_STMT (op0);
9447 tree innerop;
9449 if (!is_gimple_assign (def_stmt)
9450 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt)))
9451 return false;
9453 innerop = gimple_assign_rhs1 (def_stmt);
9455 if (TREE_CODE (innerop) == SSA_NAME
9456 && !POINTER_TYPE_P (TREE_TYPE (innerop))
9457 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (innerop)
9458 && desired_pro_or_demotion_p (TREE_TYPE (innerop), TREE_TYPE (op0)))
9460 value_range *vr = get_value_range (innerop);
9462 if (range_int_cst_p (vr)
9463 && range_fits_type_p (vr,
9464 TYPE_PRECISION (TREE_TYPE (op0)),
9465 TYPE_SIGN (TREE_TYPE (op0)))
9466 && int_fits_type_p (op1, TREE_TYPE (innerop))
9467 /* The range must not have overflowed, or if it did overflow
9468 we must not be wrapping/trapping overflow and optimizing
9469 with strict overflow semantics. */
9470 && ((!is_negative_overflow_infinity (vr->min)
9471 && !is_positive_overflow_infinity (vr->max))
9472 || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (innerop))))
9474 /* If the range overflowed and the user has asked for warnings
9475 when strict overflow semantics were used to optimize code,
9476 issue an appropriate warning. */
9477 if (cond_code != EQ_EXPR && cond_code != NE_EXPR
9478 && (is_negative_overflow_infinity (vr->min)
9479 || is_positive_overflow_infinity (vr->max))
9480 && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_CONDITIONAL))
9482 location_t location;
9484 if (!gimple_has_location (stmt))
9485 location = input_location;
9486 else
9487 location = gimple_location (stmt);
9488 warning_at (location, OPT_Wstrict_overflow,
9489 "assuming signed overflow does not occur when "
9490 "simplifying conditional");
9493 tree newconst = fold_convert (TREE_TYPE (innerop), op1);
9494 gimple_cond_set_lhs (stmt, innerop);
9495 gimple_cond_set_rhs (stmt, newconst);
9496 return true;
9501 return false;
9504 /* Simplify a switch statement using the value range of the switch
9505 argument. */
9507 static bool
9508 simplify_switch_using_ranges (gswitch *stmt)
9510 tree op = gimple_switch_index (stmt);
9511 value_range *vr;
9512 bool take_default;
9513 edge e;
9514 edge_iterator ei;
9515 size_t i = 0, j = 0, n, n2;
9516 tree vec2;
9517 switch_update su;
9518 size_t k = 1, l = 0;
9520 if (TREE_CODE (op) == SSA_NAME)
9522 vr = get_value_range (op);
9524 /* We can only handle integer ranges. */
9525 if ((vr->type != VR_RANGE
9526 && vr->type != VR_ANTI_RANGE)
9527 || symbolic_range_p (vr))
9528 return false;
9530 /* Find case label for min/max of the value range. */
9531 take_default = !find_case_label_ranges (stmt, vr, &i, &j, &k, &l);
9533 else if (TREE_CODE (op) == INTEGER_CST)
9535 take_default = !find_case_label_index (stmt, 1, op, &i);
9536 if (take_default)
9538 i = 1;
9539 j = 0;
9541 else
9543 j = i;
9546 else
9547 return false;
9549 n = gimple_switch_num_labels (stmt);
9551 /* Bail out if this is just all edges taken. */
9552 if (i == 1
9553 && j == n - 1
9554 && take_default)
9555 return false;
9557 /* Build a new vector of taken case labels. */
9558 vec2 = make_tree_vec (j - i + 1 + l - k + 1 + (int)take_default);
9559 n2 = 0;
9561 /* Add the default edge, if necessary. */
9562 if (take_default)
9563 TREE_VEC_ELT (vec2, n2++) = gimple_switch_default_label (stmt);
9565 for (; i <= j; ++i, ++n2)
9566 TREE_VEC_ELT (vec2, n2) = gimple_switch_label (stmt, i);
9568 for (; k <= l; ++k, ++n2)
9569 TREE_VEC_ELT (vec2, n2) = gimple_switch_label (stmt, k);
9571 /* Mark needed edges. */
9572 for (i = 0; i < n2; ++i)
9574 e = find_edge (gimple_bb (stmt),
9575 label_to_block (CASE_LABEL (TREE_VEC_ELT (vec2, i))));
9576 e->aux = (void *)-1;
9579 /* Queue not needed edges for later removal. */
9580 FOR_EACH_EDGE (e, ei, gimple_bb (stmt)->succs)
9582 if (e->aux == (void *)-1)
9584 e->aux = NULL;
9585 continue;
9588 if (dump_file && (dump_flags & TDF_DETAILS))
9590 fprintf (dump_file, "removing unreachable case label\n");
9592 to_remove_edges.safe_push (e);
9593 e->flags &= ~EDGE_EXECUTABLE;
9596 /* And queue an update for the stmt. */
9597 su.stmt = stmt;
9598 su.vec = vec2;
9599 to_update_switch_stmts.safe_push (su);
9600 return false;
9603 /* Simplify an integral conversion from an SSA name in STMT. */
9605 static bool
9606 simplify_conversion_using_ranges (gimple *stmt)
9608 tree innerop, middleop, finaltype;
9609 gimple *def_stmt;
9610 signop inner_sgn, middle_sgn, final_sgn;
9611 unsigned inner_prec, middle_prec, final_prec;
9612 widest_int innermin, innermed, innermax, middlemin, middlemed, middlemax;
9614 finaltype = TREE_TYPE (gimple_assign_lhs (stmt));
9615 if (!INTEGRAL_TYPE_P (finaltype))
9616 return false;
9617 middleop = gimple_assign_rhs1 (stmt);
9618 def_stmt = SSA_NAME_DEF_STMT (middleop);
9619 if (!is_gimple_assign (def_stmt)
9620 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt)))
9621 return false;
9622 innerop = gimple_assign_rhs1 (def_stmt);
9623 if (TREE_CODE (innerop) != SSA_NAME
9624 || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (innerop))
9625 return false;
9627 /* Get the value-range of the inner operand. Use get_range_info in
9628 case innerop was created during substitute-and-fold. */
9629 wide_int imin, imax;
9630 if (!INTEGRAL_TYPE_P (TREE_TYPE (innerop))
9631 || get_range_info (innerop, &imin, &imax) != VR_RANGE)
9632 return false;
9633 innermin = widest_int::from (imin, TYPE_SIGN (TREE_TYPE (innerop)));
9634 innermax = widest_int::from (imax, TYPE_SIGN (TREE_TYPE (innerop)));
9636 /* Simulate the conversion chain to check if the result is equal if
9637 the middle conversion is removed. */
9638 inner_prec = TYPE_PRECISION (TREE_TYPE (innerop));
9639 middle_prec = TYPE_PRECISION (TREE_TYPE (middleop));
9640 final_prec = TYPE_PRECISION (finaltype);
9642 /* If the first conversion is not injective, the second must not
9643 be widening. */
9644 if (wi::gtu_p (innermax - innermin,
9645 wi::mask <widest_int> (middle_prec, false))
9646 && middle_prec < final_prec)
9647 return false;
9648 /* We also want a medium value so that we can track the effect that
9649 narrowing conversions with sign change have. */
9650 inner_sgn = TYPE_SIGN (TREE_TYPE (innerop));
9651 if (inner_sgn == UNSIGNED)
9652 innermed = wi::shifted_mask <widest_int> (1, inner_prec - 1, false);
9653 else
9654 innermed = 0;
9655 if (wi::cmp (innermin, innermed, inner_sgn) >= 0
9656 || wi::cmp (innermed, innermax, inner_sgn) >= 0)
9657 innermed = innermin;
9659 middle_sgn = TYPE_SIGN (TREE_TYPE (middleop));
9660 middlemin = wi::ext (innermin, middle_prec, middle_sgn);
9661 middlemed = wi::ext (innermed, middle_prec, middle_sgn);
9662 middlemax = wi::ext (innermax, middle_prec, middle_sgn);
9664 /* Require that the final conversion applied to both the original
9665 and the intermediate range produces the same result. */
9666 final_sgn = TYPE_SIGN (finaltype);
9667 if (wi::ext (middlemin, final_prec, final_sgn)
9668 != wi::ext (innermin, final_prec, final_sgn)
9669 || wi::ext (middlemed, final_prec, final_sgn)
9670 != wi::ext (innermed, final_prec, final_sgn)
9671 || wi::ext (middlemax, final_prec, final_sgn)
9672 != wi::ext (innermax, final_prec, final_sgn))
9673 return false;
9675 gimple_assign_set_rhs1 (stmt, innerop);
9676 update_stmt (stmt);
9677 return true;
9680 /* Simplify a conversion from integral SSA name to float in STMT. */
9682 static bool
9683 simplify_float_conversion_using_ranges (gimple_stmt_iterator *gsi,
9684 gimple *stmt)
9686 tree rhs1 = gimple_assign_rhs1 (stmt);
9687 value_range *vr = get_value_range (rhs1);
9688 machine_mode fltmode = TYPE_MODE (TREE_TYPE (gimple_assign_lhs (stmt)));
9689 machine_mode mode;
9690 tree tem;
9691 gassign *conv;
9693 /* We can only handle constant ranges. */
9694 if (vr->type != VR_RANGE
9695 || TREE_CODE (vr->min) != INTEGER_CST
9696 || TREE_CODE (vr->max) != INTEGER_CST)
9697 return false;
9699 /* First check if we can use a signed type in place of an unsigned. */
9700 if (TYPE_UNSIGNED (TREE_TYPE (rhs1))
9701 && (can_float_p (fltmode, TYPE_MODE (TREE_TYPE (rhs1)), 0)
9702 != CODE_FOR_nothing)
9703 && range_fits_type_p (vr, TYPE_PRECISION (TREE_TYPE (rhs1)), SIGNED))
9704 mode = TYPE_MODE (TREE_TYPE (rhs1));
9705 /* If we can do the conversion in the current input mode do nothing. */
9706 else if (can_float_p (fltmode, TYPE_MODE (TREE_TYPE (rhs1)),
9707 TYPE_UNSIGNED (TREE_TYPE (rhs1))) != CODE_FOR_nothing)
9708 return false;
9709 /* Otherwise search for a mode we can use, starting from the narrowest
9710 integer mode available. */
9711 else
9713 mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
9716 /* If we cannot do a signed conversion to float from mode
9717 or if the value-range does not fit in the signed type
9718 try with a wider mode. */
9719 if (can_float_p (fltmode, mode, 0) != CODE_FOR_nothing
9720 && range_fits_type_p (vr, GET_MODE_PRECISION (mode), SIGNED))
9721 break;
9723 mode = GET_MODE_WIDER_MODE (mode);
9724 /* But do not widen the input. Instead leave that to the
9725 optabs expansion code. */
9726 if (GET_MODE_PRECISION (mode) > TYPE_PRECISION (TREE_TYPE (rhs1)))
9727 return false;
9729 while (mode != VOIDmode);
9730 if (mode == VOIDmode)
9731 return false;
9734 /* It works, insert a truncation or sign-change before the
9735 float conversion. */
9736 tem = make_ssa_name (build_nonstandard_integer_type
9737 (GET_MODE_PRECISION (mode), 0));
9738 conv = gimple_build_assign (tem, NOP_EXPR, rhs1);
9739 gsi_insert_before (gsi, conv, GSI_SAME_STMT);
9740 gimple_assign_set_rhs1 (stmt, tem);
9741 update_stmt (stmt);
9743 return true;
9746 /* Simplify an internal fn call using ranges if possible. */
9748 static bool
9749 simplify_internal_call_using_ranges (gimple_stmt_iterator *gsi, gimple *stmt)
9751 enum tree_code subcode;
9752 bool is_ubsan = false;
9753 bool ovf = false;
9754 switch (gimple_call_internal_fn (stmt))
9756 case IFN_UBSAN_CHECK_ADD:
9757 subcode = PLUS_EXPR;
9758 is_ubsan = true;
9759 break;
9760 case IFN_UBSAN_CHECK_SUB:
9761 subcode = MINUS_EXPR;
9762 is_ubsan = true;
9763 break;
9764 case IFN_UBSAN_CHECK_MUL:
9765 subcode = MULT_EXPR;
9766 is_ubsan = true;
9767 break;
9768 case IFN_ADD_OVERFLOW:
9769 subcode = PLUS_EXPR;
9770 break;
9771 case IFN_SUB_OVERFLOW:
9772 subcode = MINUS_EXPR;
9773 break;
9774 case IFN_MUL_OVERFLOW:
9775 subcode = MULT_EXPR;
9776 break;
9777 default:
9778 return false;
9781 tree op0 = gimple_call_arg (stmt, 0);
9782 tree op1 = gimple_call_arg (stmt, 1);
9783 tree type;
9784 if (is_ubsan)
9785 type = TREE_TYPE (op0);
9786 else if (gimple_call_lhs (stmt) == NULL_TREE)
9787 return false;
9788 else
9789 type = TREE_TYPE (TREE_TYPE (gimple_call_lhs (stmt)));
9790 if (!check_for_binary_op_overflow (subcode, type, op0, op1, &ovf)
9791 || (is_ubsan && ovf))
9792 return false;
9794 gimple *g;
9795 location_t loc = gimple_location (stmt);
9796 if (is_ubsan)
9797 g = gimple_build_assign (gimple_call_lhs (stmt), subcode, op0, op1);
9798 else
9800 int prec = TYPE_PRECISION (type);
9801 tree utype = type;
9802 if (ovf
9803 || !useless_type_conversion_p (type, TREE_TYPE (op0))
9804 || !useless_type_conversion_p (type, TREE_TYPE (op1)))
9805 utype = build_nonstandard_integer_type (prec, 1);
9806 if (TREE_CODE (op0) == INTEGER_CST)
9807 op0 = fold_convert (utype, op0);
9808 else if (!useless_type_conversion_p (utype, TREE_TYPE (op0)))
9810 g = gimple_build_assign (make_ssa_name (utype), NOP_EXPR, op0);
9811 gimple_set_location (g, loc);
9812 gsi_insert_before (gsi, g, GSI_SAME_STMT);
9813 op0 = gimple_assign_lhs (g);
9815 if (TREE_CODE (op1) == INTEGER_CST)
9816 op1 = fold_convert (utype, op1);
9817 else if (!useless_type_conversion_p (utype, TREE_TYPE (op1)))
9819 g = gimple_build_assign (make_ssa_name (utype), NOP_EXPR, op1);
9820 gimple_set_location (g, loc);
9821 gsi_insert_before (gsi, g, GSI_SAME_STMT);
9822 op1 = gimple_assign_lhs (g);
9824 g = gimple_build_assign (make_ssa_name (utype), subcode, op0, op1);
9825 gimple_set_location (g, loc);
9826 gsi_insert_before (gsi, g, GSI_SAME_STMT);
9827 if (utype != type)
9829 g = gimple_build_assign (make_ssa_name (type), NOP_EXPR,
9830 gimple_assign_lhs (g));
9831 gimple_set_location (g, loc);
9832 gsi_insert_before (gsi, g, GSI_SAME_STMT);
9834 g = gimple_build_assign (gimple_call_lhs (stmt), COMPLEX_EXPR,
9835 gimple_assign_lhs (g),
9836 build_int_cst (type, ovf));
9838 gimple_set_location (g, loc);
9839 gsi_replace (gsi, g, false);
9840 return true;
9843 /* Simplify STMT using ranges if possible. */
9845 static bool
9846 simplify_stmt_using_ranges (gimple_stmt_iterator *gsi)
9848 gimple *stmt = gsi_stmt (*gsi);
9849 if (is_gimple_assign (stmt))
9851 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
9852 tree rhs1 = gimple_assign_rhs1 (stmt);
9854 switch (rhs_code)
9856 case EQ_EXPR:
9857 case NE_EXPR:
9858 /* Transform EQ_EXPR, NE_EXPR into BIT_XOR_EXPR or identity
9859 if the RHS is zero or one, and the LHS are known to be boolean
9860 values. */
9861 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
9862 return simplify_truth_ops_using_ranges (gsi, stmt);
9863 break;
9865 /* Transform TRUNC_DIV_EXPR and TRUNC_MOD_EXPR into RSHIFT_EXPR
9866 and BIT_AND_EXPR respectively if the first operand is greater
9867 than zero and the second operand is an exact power of two.
9868 Also optimize TRUNC_MOD_EXPR away if the second operand is
9869 constant and the first operand already has the right value
9870 range. */
9871 case TRUNC_DIV_EXPR:
9872 case TRUNC_MOD_EXPR:
9873 if (TREE_CODE (rhs1) == SSA_NAME
9874 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
9875 return simplify_div_or_mod_using_ranges (gsi, stmt);
9876 break;
9878 /* Transform ABS (X) into X or -X as appropriate. */
9879 case ABS_EXPR:
9880 if (TREE_CODE (rhs1) == SSA_NAME
9881 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
9882 return simplify_abs_using_ranges (stmt);
9883 break;
9885 case BIT_AND_EXPR:
9886 case BIT_IOR_EXPR:
9887 /* Optimize away BIT_AND_EXPR and BIT_IOR_EXPR
9888 if all the bits being cleared are already cleared or
9889 all the bits being set are already set. */
9890 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
9891 return simplify_bit_ops_using_ranges (gsi, stmt);
9892 break;
9894 CASE_CONVERT:
9895 if (TREE_CODE (rhs1) == SSA_NAME
9896 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
9897 return simplify_conversion_using_ranges (stmt);
9898 break;
9900 case FLOAT_EXPR:
9901 if (TREE_CODE (rhs1) == SSA_NAME
9902 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
9903 return simplify_float_conversion_using_ranges (gsi, stmt);
9904 break;
9906 case MIN_EXPR:
9907 case MAX_EXPR:
9908 return simplify_min_or_max_using_ranges (stmt);
9909 break;
9911 default:
9912 break;
9915 else if (gimple_code (stmt) == GIMPLE_COND)
9916 return simplify_cond_using_ranges (as_a <gcond *> (stmt));
9917 else if (gimple_code (stmt) == GIMPLE_SWITCH)
9918 return simplify_switch_using_ranges (as_a <gswitch *> (stmt));
9919 else if (is_gimple_call (stmt)
9920 && gimple_call_internal_p (stmt))
9921 return simplify_internal_call_using_ranges (gsi, stmt);
9923 return false;
9926 /* If the statement pointed by SI has a predicate whose value can be
9927 computed using the value range information computed by VRP, compute
9928 its value and return true. Otherwise, return false. */
9930 static bool
9931 fold_predicate_in (gimple_stmt_iterator *si)
9933 bool assignment_p = false;
9934 tree val;
9935 gimple *stmt = gsi_stmt (*si);
9937 if (is_gimple_assign (stmt)
9938 && TREE_CODE_CLASS (gimple_assign_rhs_code (stmt)) == tcc_comparison)
9940 assignment_p = true;
9941 val = vrp_evaluate_conditional (gimple_assign_rhs_code (stmt),
9942 gimple_assign_rhs1 (stmt),
9943 gimple_assign_rhs2 (stmt),
9944 stmt);
9946 else if (gcond *cond_stmt = dyn_cast <gcond *> (stmt))
9947 val = vrp_evaluate_conditional (gimple_cond_code (cond_stmt),
9948 gimple_cond_lhs (cond_stmt),
9949 gimple_cond_rhs (cond_stmt),
9950 stmt);
9951 else
9952 return false;
9954 if (val)
9956 if (assignment_p)
9957 val = fold_convert (gimple_expr_type (stmt), val);
9959 if (dump_file)
9961 fprintf (dump_file, "Folding predicate ");
9962 print_gimple_expr (dump_file, stmt, 0, 0);
9963 fprintf (dump_file, " to ");
9964 print_generic_expr (dump_file, val, 0);
9965 fprintf (dump_file, "\n");
9968 if (is_gimple_assign (stmt))
9969 gimple_assign_set_rhs_from_tree (si, val);
9970 else
9972 gcc_assert (gimple_code (stmt) == GIMPLE_COND);
9973 gcond *cond_stmt = as_a <gcond *> (stmt);
9974 if (integer_zerop (val))
9975 gimple_cond_make_false (cond_stmt);
9976 else if (integer_onep (val))
9977 gimple_cond_make_true (cond_stmt);
9978 else
9979 gcc_unreachable ();
9982 return true;
9985 return false;
9988 /* Callback for substitute_and_fold folding the stmt at *SI. */
9990 static bool
9991 vrp_fold_stmt (gimple_stmt_iterator *si)
9993 if (fold_predicate_in (si))
9994 return true;
9996 return simplify_stmt_using_ranges (si);
9999 /* Unwindable const/copy equivalences. */
10000 const_and_copies *equiv_stack;
10002 /* A trivial wrapper so that we can present the generic jump threading
10003 code with a simple API for simplifying statements. STMT is the
10004 statement we want to simplify, WITHIN_STMT provides the location
10005 for any overflow warnings. */
10007 static tree
10008 simplify_stmt_for_jump_threading (gimple *stmt, gimple *within_stmt,
10009 class avail_exprs_stack *avail_exprs_stack ATTRIBUTE_UNUSED)
10011 if (gcond *cond_stmt = dyn_cast <gcond *> (stmt))
10012 return vrp_evaluate_conditional (gimple_cond_code (cond_stmt),
10013 gimple_cond_lhs (cond_stmt),
10014 gimple_cond_rhs (cond_stmt),
10015 within_stmt);
10017 if (gassign *assign_stmt = dyn_cast <gassign *> (stmt))
10019 value_range new_vr = VR_INITIALIZER;
10020 tree lhs = gimple_assign_lhs (assign_stmt);
10022 if (TREE_CODE (lhs) == SSA_NAME
10023 && (INTEGRAL_TYPE_P (TREE_TYPE (lhs))
10024 || POINTER_TYPE_P (TREE_TYPE (lhs))))
10026 extract_range_from_assignment (&new_vr, assign_stmt);
10027 if (range_int_cst_singleton_p (&new_vr))
10028 return new_vr.min;
10032 return NULL_TREE;
10035 /* Blocks which have more than one predecessor and more than
10036 one successor present jump threading opportunities, i.e.,
10037 when the block is reached from a specific predecessor, we
10038 may be able to determine which of the outgoing edges will
10039 be traversed. When this optimization applies, we are able
10040 to avoid conditionals at runtime and we may expose secondary
10041 optimization opportunities.
10043 This routine is effectively a driver for the generic jump
10044 threading code. It basically just presents the generic code
10045 with edges that may be suitable for jump threading.
10047 Unlike DOM, we do not iterate VRP if jump threading was successful.
10048 While iterating may expose new opportunities for VRP, it is expected
10049 those opportunities would be very limited and the compile time cost
10050 to expose those opportunities would be significant.
10052 As jump threading opportunities are discovered, they are registered
10053 for later realization. */
10055 static void
10056 identify_jump_threads (void)
10058 basic_block bb;
10059 gcond *dummy;
10060 int i;
10061 edge e;
10063 /* Ugh. When substituting values earlier in this pass we can
10064 wipe the dominance information. So rebuild the dominator
10065 information as we need it within the jump threading code. */
10066 calculate_dominance_info (CDI_DOMINATORS);
10068 /* We do not allow VRP information to be used for jump threading
10069 across a back edge in the CFG. Otherwise it becomes too
10070 difficult to avoid eliminating loop exit tests. Of course
10071 EDGE_DFS_BACK is not accurate at this time so we have to
10072 recompute it. */
10073 mark_dfs_back_edges ();
10075 /* Do not thread across edges we are about to remove. Just marking
10076 them as EDGE_IGNORE will do. */
10077 FOR_EACH_VEC_ELT (to_remove_edges, i, e)
10078 e->flags |= EDGE_IGNORE;
10080 /* Allocate our unwinder stack to unwind any temporary equivalences
10081 that might be recorded. */
10082 equiv_stack = new const_and_copies ();
10084 /* To avoid lots of silly node creation, we create a single
10085 conditional and just modify it in-place when attempting to
10086 thread jumps. */
10087 dummy = gimple_build_cond (EQ_EXPR,
10088 integer_zero_node, integer_zero_node,
10089 NULL, NULL);
10091 /* Walk through all the blocks finding those which present a
10092 potential jump threading opportunity. We could set this up
10093 as a dominator walker and record data during the walk, but
10094 I doubt it's worth the effort for the classes of jump
10095 threading opportunities we are trying to identify at this
10096 point in compilation. */
10097 FOR_EACH_BB_FN (bb, cfun)
10099 gimple *last;
10101 /* If the generic jump threading code does not find this block
10102 interesting, then there is nothing to do. */
10103 if (! potentially_threadable_block (bb))
10104 continue;
10106 last = last_stmt (bb);
10108 /* We're basically looking for a switch or any kind of conditional with
10109 integral or pointer type arguments. Note the type of the second
10110 argument will be the same as the first argument, so no need to
10111 check it explicitly.
10113 We also handle the case where there are no statements in the
10114 block. This come up with forwarder blocks that are not
10115 optimized away because they lead to a loop header. But we do
10116 want to thread through them as we can sometimes thread to the
10117 loop exit which is obviously profitable. */
10118 if (!last
10119 || gimple_code (last) == GIMPLE_SWITCH
10120 || (gimple_code (last) == GIMPLE_COND
10121 && TREE_CODE (gimple_cond_lhs (last)) == SSA_NAME
10122 && (INTEGRAL_TYPE_P (TREE_TYPE (gimple_cond_lhs (last)))
10123 || POINTER_TYPE_P (TREE_TYPE (gimple_cond_lhs (last))))
10124 && (TREE_CODE (gimple_cond_rhs (last)) == SSA_NAME
10125 || is_gimple_min_invariant (gimple_cond_rhs (last)))))
10127 edge_iterator ei;
10129 /* We've got a block with multiple predecessors and multiple
10130 successors which also ends in a suitable conditional or
10131 switch statement. For each predecessor, see if we can thread
10132 it to a specific successor. */
10133 FOR_EACH_EDGE (e, ei, bb->preds)
10135 /* Do not thread across edges marked to ignoreor abnormal
10136 edges in the CFG. */
10137 if (e->flags & (EDGE_IGNORE | EDGE_COMPLEX))
10138 continue;
10140 thread_across_edge (dummy, e, true, equiv_stack, NULL,
10141 simplify_stmt_for_jump_threading);
10146 /* Clear EDGE_IGNORE. */
10147 FOR_EACH_VEC_ELT (to_remove_edges, i, e)
10148 e->flags &= ~EDGE_IGNORE;
10150 /* We do not actually update the CFG or SSA graphs at this point as
10151 ASSERT_EXPRs are still in the IL and cfg cleanup code does not yet
10152 handle ASSERT_EXPRs gracefully. */
10155 /* We identified all the jump threading opportunities earlier, but could
10156 not transform the CFG at that time. This routine transforms the
10157 CFG and arranges for the dominator tree to be rebuilt if necessary.
10159 Note the SSA graph update will occur during the normal TODO
10160 processing by the pass manager. */
10161 static void
10162 finalize_jump_threads (void)
10164 thread_through_all_blocks (false);
10165 delete equiv_stack;
10169 /* Traverse all the blocks folding conditionals with known ranges. */
10171 static void
10172 vrp_finalize (bool warn_array_bounds_p)
10174 size_t i;
10176 values_propagated = true;
10178 if (dump_file)
10180 fprintf (dump_file, "\nValue ranges after VRP:\n\n");
10181 dump_all_value_ranges (dump_file);
10182 fprintf (dump_file, "\n");
10185 /* Set value range to non pointer SSA_NAMEs. */
10186 for (i = 0; i < num_vr_values; i++)
10187 if (vr_value[i])
10189 tree name = ssa_name (i);
10191 if (!name
10192 || POINTER_TYPE_P (TREE_TYPE (name))
10193 || (vr_value[i]->type == VR_VARYING)
10194 || (vr_value[i]->type == VR_UNDEFINED))
10195 continue;
10197 if ((TREE_CODE (vr_value[i]->min) == INTEGER_CST)
10198 && (TREE_CODE (vr_value[i]->max) == INTEGER_CST)
10199 && (vr_value[i]->type == VR_RANGE
10200 || vr_value[i]->type == VR_ANTI_RANGE))
10201 set_range_info (name, vr_value[i]->type, vr_value[i]->min,
10202 vr_value[i]->max);
10205 substitute_and_fold (op_with_constant_singleton_value_range,
10206 vrp_fold_stmt, false);
10208 if (warn_array_bounds && warn_array_bounds_p)
10209 check_all_array_refs ();
10211 /* We must identify jump threading opportunities before we release
10212 the datastructures built by VRP. */
10213 identify_jump_threads ();
10215 /* Free allocated memory. */
10216 for (i = 0; i < num_vr_values; i++)
10217 if (vr_value[i])
10219 BITMAP_FREE (vr_value[i]->equiv);
10220 free (vr_value[i]);
10223 free (vr_value);
10224 free (vr_phi_edge_counts);
10226 /* So that we can distinguish between VRP data being available
10227 and not available. */
10228 vr_value = NULL;
10229 vr_phi_edge_counts = NULL;
10233 /* Main entry point to VRP (Value Range Propagation). This pass is
10234 loosely based on J. R. C. Patterson, ``Accurate Static Branch
10235 Prediction by Value Range Propagation,'' in SIGPLAN Conference on
10236 Programming Language Design and Implementation, pp. 67-78, 1995.
10237 Also available at http://citeseer.ist.psu.edu/patterson95accurate.html
10239 This is essentially an SSA-CCP pass modified to deal with ranges
10240 instead of constants.
10242 While propagating ranges, we may find that two or more SSA name
10243 have equivalent, though distinct ranges. For instance,
10245 1 x_9 = p_3->a;
10246 2 p_4 = ASSERT_EXPR <p_3, p_3 != 0>
10247 3 if (p_4 == q_2)
10248 4 p_5 = ASSERT_EXPR <p_4, p_4 == q_2>;
10249 5 endif
10250 6 if (q_2)
10252 In the code above, pointer p_5 has range [q_2, q_2], but from the
10253 code we can also determine that p_5 cannot be NULL and, if q_2 had
10254 a non-varying range, p_5's range should also be compatible with it.
10256 These equivalences are created by two expressions: ASSERT_EXPR and
10257 copy operations. Since p_5 is an assertion on p_4, and p_4 was the
10258 result of another assertion, then we can use the fact that p_5 and
10259 p_4 are equivalent when evaluating p_5's range.
10261 Together with value ranges, we also propagate these equivalences
10262 between names so that we can take advantage of information from
10263 multiple ranges when doing final replacement. Note that this
10264 equivalency relation is transitive but not symmetric.
10266 In the example above, p_5 is equivalent to p_4, q_2 and p_3, but we
10267 cannot assert that q_2 is equivalent to p_5 because q_2 may be used
10268 in contexts where that assertion does not hold (e.g., in line 6).
10270 TODO, the main difference between this pass and Patterson's is that
10271 we do not propagate edge probabilities. We only compute whether
10272 edges can be taken or not. That is, instead of having a spectrum
10273 of jump probabilities between 0 and 1, we only deal with 0, 1 and
10274 DON'T KNOW. In the future, it may be worthwhile to propagate
10275 probabilities to aid branch prediction. */
10277 static unsigned int
10278 execute_vrp (bool warn_array_bounds_p)
10280 int i;
10281 edge e;
10282 switch_update *su;
10284 loop_optimizer_init (LOOPS_NORMAL | LOOPS_HAVE_RECORDED_EXITS);
10285 rewrite_into_loop_closed_ssa (NULL, TODO_update_ssa);
10286 scev_initialize ();
10288 /* ??? This ends up using stale EDGE_DFS_BACK for liveness computation.
10289 Inserting assertions may split edges which will invalidate
10290 EDGE_DFS_BACK. */
10291 insert_range_assertions ();
10293 to_remove_edges.create (10);
10294 to_update_switch_stmts.create (5);
10295 threadedge_initialize_values ();
10297 /* For visiting PHI nodes we need EDGE_DFS_BACK computed. */
10298 mark_dfs_back_edges ();
10300 vrp_initialize ();
10301 ssa_propagate (vrp_visit_stmt, vrp_visit_phi_node);
10302 vrp_finalize (warn_array_bounds_p);
10304 free_numbers_of_iterations_estimates (cfun);
10306 /* ASSERT_EXPRs must be removed before finalizing jump threads
10307 as finalizing jump threads calls the CFG cleanup code which
10308 does not properly handle ASSERT_EXPRs. */
10309 remove_range_assertions ();
10311 /* If we exposed any new variables, go ahead and put them into
10312 SSA form now, before we handle jump threading. This simplifies
10313 interactions between rewriting of _DECL nodes into SSA form
10314 and rewriting SSA_NAME nodes into SSA form after block
10315 duplication and CFG manipulation. */
10316 update_ssa (TODO_update_ssa);
10318 finalize_jump_threads ();
10320 /* Remove dead edges from SWITCH_EXPR optimization. This leaves the
10321 CFG in a broken state and requires a cfg_cleanup run. */
10322 FOR_EACH_VEC_ELT (to_remove_edges, i, e)
10323 remove_edge (e);
10324 /* Update SWITCH_EXPR case label vector. */
10325 FOR_EACH_VEC_ELT (to_update_switch_stmts, i, su)
10327 size_t j;
10328 size_t n = TREE_VEC_LENGTH (su->vec);
10329 tree label;
10330 gimple_switch_set_num_labels (su->stmt, n);
10331 for (j = 0; j < n; j++)
10332 gimple_switch_set_label (su->stmt, j, TREE_VEC_ELT (su->vec, j));
10333 /* As we may have replaced the default label with a regular one
10334 make sure to make it a real default label again. This ensures
10335 optimal expansion. */
10336 label = gimple_switch_label (su->stmt, 0);
10337 CASE_LOW (label) = NULL_TREE;
10338 CASE_HIGH (label) = NULL_TREE;
10341 if (to_remove_edges.length () > 0)
10343 free_dominance_info (CDI_DOMINATORS);
10344 loops_state_set (LOOPS_NEED_FIXUP);
10347 to_remove_edges.release ();
10348 to_update_switch_stmts.release ();
10349 threadedge_finalize_values ();
10351 scev_finalize ();
10352 loop_optimizer_finalize ();
10353 return 0;
10356 namespace {
10358 const pass_data pass_data_vrp =
10360 GIMPLE_PASS, /* type */
10361 "vrp", /* name */
10362 OPTGROUP_NONE, /* optinfo_flags */
10363 TV_TREE_VRP, /* tv_id */
10364 PROP_ssa, /* properties_required */
10365 0, /* properties_provided */
10366 0, /* properties_destroyed */
10367 0, /* todo_flags_start */
10368 ( TODO_cleanup_cfg | TODO_update_ssa ), /* todo_flags_finish */
10371 class pass_vrp : public gimple_opt_pass
10373 public:
10374 pass_vrp (gcc::context *ctxt)
10375 : gimple_opt_pass (pass_data_vrp, ctxt), warn_array_bounds_p (false)
10378 /* opt_pass methods: */
10379 opt_pass * clone () { return new pass_vrp (m_ctxt); }
10380 void set_pass_param (unsigned int n, bool param)
10382 gcc_assert (n == 0);
10383 warn_array_bounds_p = param;
10385 virtual bool gate (function *) { return flag_tree_vrp != 0; }
10386 virtual unsigned int execute (function *)
10387 { return execute_vrp (warn_array_bounds_p); }
10389 private:
10390 bool warn_array_bounds_p;
10391 }; // class pass_vrp
10393 } // anon namespace
10395 gimple_opt_pass *
10396 make_pass_vrp (gcc::context *ctxt)
10398 return new pass_vrp (ctxt);