[ARM, AArch64] Add missing vrnd{,a,m,n,p,x} tests.
[official-gcc.git] / gcc / tree-vrp.c
blob68f2e905424999e2a639c02ae74b160ebfe8a067
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 = NULL;
1522 /* Initially, the new range has the same set of equivalences of
1523 VAR's range. This will be revised before returning the final
1524 value. Since assertions may be chained via mutually exclusive
1525 predicates, we will need to trim the set of equivalences before
1526 we are done. */
1527 gcc_assert (vr_p->equiv == NULL);
1528 add_equivalence (&vr_p->equiv, var);
1530 /* Extract a new range based on the asserted comparison for VAR and
1531 LIMIT's value range. Notice that if LIMIT has an anti-range, we
1532 will only use it for equality comparisons (EQ_EXPR). For any
1533 other kind of assertion, we cannot derive a range from LIMIT's
1534 anti-range that can be used to describe the new range. For
1535 instance, ASSERT_EXPR <x_2, x_2 <= b_4>. If b_4 is ~[2, 10],
1536 then b_4 takes on the ranges [-INF, 1] and [11, +INF]. There is
1537 no single range for x_2 that could describe LE_EXPR, so we might
1538 as well build the range [b_4, +INF] for it.
1539 One special case we handle is extracting a range from a
1540 range test encoded as (unsigned)var + CST <= limit. */
1541 if (TREE_CODE (cond) == NOP_EXPR
1542 || TREE_CODE (cond) == PLUS_EXPR)
1544 if (TREE_CODE (cond) == PLUS_EXPR)
1546 min = fold_build1 (NEGATE_EXPR, TREE_TYPE (TREE_OPERAND (cond, 1)),
1547 TREE_OPERAND (cond, 1));
1548 max = int_const_binop (PLUS_EXPR, limit, min);
1549 cond = TREE_OPERAND (cond, 0);
1551 else
1553 min = build_int_cst (TREE_TYPE (var), 0);
1554 max = limit;
1557 /* Make sure to not set TREE_OVERFLOW on the final type
1558 conversion. We are willingly interpreting large positive
1559 unsigned values as negative signed values here. */
1560 min = force_fit_type (TREE_TYPE (var), wi::to_widest (min), 0, false);
1561 max = force_fit_type (TREE_TYPE (var), wi::to_widest (max), 0, false);
1563 /* We can transform a max, min range to an anti-range or
1564 vice-versa. Use set_and_canonicalize_value_range which does
1565 this for us. */
1566 if (cond_code == LE_EXPR)
1567 set_and_canonicalize_value_range (vr_p, VR_RANGE,
1568 min, max, vr_p->equiv);
1569 else if (cond_code == GT_EXPR)
1570 set_and_canonicalize_value_range (vr_p, VR_ANTI_RANGE,
1571 min, max, vr_p->equiv);
1572 else
1573 gcc_unreachable ();
1575 else if (cond_code == EQ_EXPR)
1577 enum value_range_type range_type;
1579 if (limit_vr)
1581 range_type = limit_vr->type;
1582 min = limit_vr->min;
1583 max = limit_vr->max;
1585 else
1587 range_type = VR_RANGE;
1588 min = limit;
1589 max = limit;
1592 set_value_range (vr_p, range_type, min, max, vr_p->equiv);
1594 /* When asserting the equality VAR == LIMIT and LIMIT is another
1595 SSA name, the new range will also inherit the equivalence set
1596 from LIMIT. */
1597 if (TREE_CODE (limit) == SSA_NAME)
1598 add_equivalence (&vr_p->equiv, limit);
1600 else if (cond_code == NE_EXPR)
1602 /* As described above, when LIMIT's range is an anti-range and
1603 this assertion is an inequality (NE_EXPR), then we cannot
1604 derive anything from the anti-range. For instance, if
1605 LIMIT's range was ~[0, 0], the assertion 'VAR != LIMIT' does
1606 not imply that VAR's range is [0, 0]. So, in the case of
1607 anti-ranges, we just assert the inequality using LIMIT and
1608 not its anti-range.
1610 If LIMIT_VR is a range, we can only use it to build a new
1611 anti-range if LIMIT_VR is a single-valued range. For
1612 instance, if LIMIT_VR is [0, 1], the predicate
1613 VAR != [0, 1] does not mean that VAR's range is ~[0, 1].
1614 Rather, it means that for value 0 VAR should be ~[0, 0]
1615 and for value 1, VAR should be ~[1, 1]. We cannot
1616 represent these ranges.
1618 The only situation in which we can build a valid
1619 anti-range is when LIMIT_VR is a single-valued range
1620 (i.e., LIMIT_VR->MIN == LIMIT_VR->MAX). In that case,
1621 build the anti-range ~[LIMIT_VR->MIN, LIMIT_VR->MAX]. */
1622 if (limit_vr
1623 && limit_vr->type == VR_RANGE
1624 && compare_values (limit_vr->min, limit_vr->max) == 0)
1626 min = limit_vr->min;
1627 max = limit_vr->max;
1629 else
1631 /* In any other case, we cannot use LIMIT's range to build a
1632 valid anti-range. */
1633 min = max = limit;
1636 /* If MIN and MAX cover the whole range for their type, then
1637 just use the original LIMIT. */
1638 if (INTEGRAL_TYPE_P (type)
1639 && vrp_val_is_min (min)
1640 && vrp_val_is_max (max))
1641 min = max = limit;
1643 set_and_canonicalize_value_range (vr_p, VR_ANTI_RANGE,
1644 min, max, vr_p->equiv);
1646 else if (cond_code == LE_EXPR || cond_code == LT_EXPR)
1648 min = TYPE_MIN_VALUE (type);
1650 if (limit_vr == NULL || limit_vr->type == VR_ANTI_RANGE)
1651 max = limit;
1652 else
1654 /* If LIMIT_VR is of the form [N1, N2], we need to build the
1655 range [MIN, N2] for LE_EXPR and [MIN, N2 - 1] for
1656 LT_EXPR. */
1657 max = limit_vr->max;
1660 /* If the maximum value forces us to be out of bounds, simply punt.
1661 It would be pointless to try and do anything more since this
1662 all should be optimized away above us. */
1663 if ((cond_code == LT_EXPR
1664 && compare_values (max, min) == 0)
1665 || is_overflow_infinity (max))
1666 set_value_range_to_varying (vr_p);
1667 else
1669 /* For LT_EXPR, we create the range [MIN, MAX - 1]. */
1670 if (cond_code == LT_EXPR)
1672 if (TYPE_PRECISION (TREE_TYPE (max)) == 1
1673 && !TYPE_UNSIGNED (TREE_TYPE (max)))
1674 max = fold_build2 (PLUS_EXPR, TREE_TYPE (max), max,
1675 build_int_cst (TREE_TYPE (max), -1));
1676 else
1677 max = fold_build2 (MINUS_EXPR, TREE_TYPE (max), max,
1678 build_int_cst (TREE_TYPE (max), 1));
1679 if (EXPR_P (max))
1680 TREE_NO_WARNING (max) = 1;
1683 set_value_range (vr_p, VR_RANGE, min, max, vr_p->equiv);
1686 else if (cond_code == GE_EXPR || cond_code == GT_EXPR)
1688 max = TYPE_MAX_VALUE (type);
1690 if (limit_vr == NULL || limit_vr->type == VR_ANTI_RANGE)
1691 min = limit;
1692 else
1694 /* If LIMIT_VR is of the form [N1, N2], we need to build the
1695 range [N1, MAX] for GE_EXPR and [N1 + 1, MAX] for
1696 GT_EXPR. */
1697 min = limit_vr->min;
1700 /* If the minimum value forces us to be out of bounds, simply punt.
1701 It would be pointless to try and do anything more since this
1702 all should be optimized away above us. */
1703 if ((cond_code == GT_EXPR
1704 && compare_values (min, max) == 0)
1705 || is_overflow_infinity (min))
1706 set_value_range_to_varying (vr_p);
1707 else
1709 /* For GT_EXPR, we create the range [MIN + 1, MAX]. */
1710 if (cond_code == GT_EXPR)
1712 if (TYPE_PRECISION (TREE_TYPE (min)) == 1
1713 && !TYPE_UNSIGNED (TREE_TYPE (min)))
1714 min = fold_build2 (MINUS_EXPR, TREE_TYPE (min), min,
1715 build_int_cst (TREE_TYPE (min), -1));
1716 else
1717 min = fold_build2 (PLUS_EXPR, TREE_TYPE (min), min,
1718 build_int_cst (TREE_TYPE (min), 1));
1719 if (EXPR_P (min))
1720 TREE_NO_WARNING (min) = 1;
1723 set_value_range (vr_p, VR_RANGE, min, max, vr_p->equiv);
1726 else
1727 gcc_unreachable ();
1729 /* Finally intersect the new range with what we already know about var. */
1730 vrp_intersect_ranges (vr_p, get_value_range (var));
1734 /* Extract range information from SSA name VAR and store it in VR. If
1735 VAR has an interesting range, use it. Otherwise, create the
1736 range [VAR, VAR] and return it. This is useful in situations where
1737 we may have conditionals testing values of VARYING names. For
1738 instance,
1740 x_3 = y_5;
1741 if (x_3 > y_5)
1744 Even if y_5 is deemed VARYING, we can determine that x_3 > y_5 is
1745 always false. */
1747 static void
1748 extract_range_from_ssa_name (value_range *vr, tree var)
1750 value_range *var_vr = get_value_range (var);
1752 if (var_vr->type != VR_VARYING)
1753 copy_value_range (vr, var_vr);
1754 else
1755 set_value_range (vr, VR_RANGE, var, var, NULL);
1757 add_equivalence (&vr->equiv, var);
1761 /* Wrapper around int_const_binop. If the operation overflows and we
1762 are not using wrapping arithmetic, then adjust the result to be
1763 -INF or +INF depending on CODE, VAL1 and VAL2. This can return
1764 NULL_TREE if we need to use an overflow infinity representation but
1765 the type does not support it. */
1767 static tree
1768 vrp_int_const_binop (enum tree_code code, tree val1, tree val2)
1770 tree res;
1772 res = int_const_binop (code, val1, val2);
1774 /* If we are using unsigned arithmetic, operate symbolically
1775 on -INF and +INF as int_const_binop only handles signed overflow. */
1776 if (TYPE_UNSIGNED (TREE_TYPE (val1)))
1778 int checkz = compare_values (res, val1);
1779 bool overflow = false;
1781 /* Ensure that res = val1 [+*] val2 >= val1
1782 or that res = val1 - val2 <= val1. */
1783 if ((code == PLUS_EXPR
1784 && !(checkz == 1 || checkz == 0))
1785 || (code == MINUS_EXPR
1786 && !(checkz == 0 || checkz == -1)))
1788 overflow = true;
1790 /* Checking for multiplication overflow is done by dividing the
1791 output of the multiplication by the first input of the
1792 multiplication. If the result of that division operation is
1793 not equal to the second input of the multiplication, then the
1794 multiplication overflowed. */
1795 else if (code == MULT_EXPR && !integer_zerop (val1))
1797 tree tmp = int_const_binop (TRUNC_DIV_EXPR,
1798 res,
1799 val1);
1800 int check = compare_values (tmp, val2);
1802 if (check != 0)
1803 overflow = true;
1806 if (overflow)
1808 res = copy_node (res);
1809 TREE_OVERFLOW (res) = 1;
1813 else if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (val1)))
1814 /* If the singed operation wraps then int_const_binop has done
1815 everything we want. */
1817 /* Signed division of -1/0 overflows and by the time it gets here
1818 returns NULL_TREE. */
1819 else if (!res)
1820 return NULL_TREE;
1821 else if ((TREE_OVERFLOW (res)
1822 && !TREE_OVERFLOW (val1)
1823 && !TREE_OVERFLOW (val2))
1824 || is_overflow_infinity (val1)
1825 || is_overflow_infinity (val2))
1827 /* If the operation overflowed but neither VAL1 nor VAL2 are
1828 overflown, return -INF or +INF depending on the operation
1829 and the combination of signs of the operands. */
1830 int sgn1 = tree_int_cst_sgn (val1);
1831 int sgn2 = tree_int_cst_sgn (val2);
1833 if (needs_overflow_infinity (TREE_TYPE (res))
1834 && !supports_overflow_infinity (TREE_TYPE (res)))
1835 return NULL_TREE;
1837 /* We have to punt on adding infinities of different signs,
1838 since we can't tell what the sign of the result should be.
1839 Likewise for subtracting infinities of the same sign. */
1840 if (((code == PLUS_EXPR && sgn1 != sgn2)
1841 || (code == MINUS_EXPR && sgn1 == sgn2))
1842 && is_overflow_infinity (val1)
1843 && is_overflow_infinity (val2))
1844 return NULL_TREE;
1846 /* Don't try to handle division or shifting of infinities. */
1847 if ((code == TRUNC_DIV_EXPR
1848 || code == FLOOR_DIV_EXPR
1849 || code == CEIL_DIV_EXPR
1850 || code == EXACT_DIV_EXPR
1851 || code == ROUND_DIV_EXPR
1852 || code == RSHIFT_EXPR)
1853 && (is_overflow_infinity (val1)
1854 || is_overflow_infinity (val2)))
1855 return NULL_TREE;
1857 /* Notice that we only need to handle the restricted set of
1858 operations handled by extract_range_from_binary_expr.
1859 Among them, only multiplication, addition and subtraction
1860 can yield overflow without overflown operands because we
1861 are working with integral types only... except in the
1862 case VAL1 = -INF and VAL2 = -1 which overflows to +INF
1863 for division too. */
1865 /* For multiplication, the sign of the overflow is given
1866 by the comparison of the signs of the operands. */
1867 if ((code == MULT_EXPR && sgn1 == sgn2)
1868 /* For addition, the operands must be of the same sign
1869 to yield an overflow. Its sign is therefore that
1870 of one of the operands, for example the first. For
1871 infinite operands X + -INF is negative, not positive. */
1872 || (code == PLUS_EXPR
1873 && (sgn1 >= 0
1874 ? !is_negative_overflow_infinity (val2)
1875 : is_positive_overflow_infinity (val2)))
1876 /* For subtraction, non-infinite operands must be of
1877 different signs to yield an overflow. Its sign is
1878 therefore that of the first operand or the opposite of
1879 that of the second operand. A first operand of 0 counts
1880 as positive here, for the corner case 0 - (-INF), which
1881 overflows, but must yield +INF. For infinite operands 0
1882 - INF is negative, not positive. */
1883 || (code == MINUS_EXPR
1884 && (sgn1 >= 0
1885 ? !is_positive_overflow_infinity (val2)
1886 : is_negative_overflow_infinity (val2)))
1887 /* We only get in here with positive shift count, so the
1888 overflow direction is the same as the sign of val1.
1889 Actually rshift does not overflow at all, but we only
1890 handle the case of shifting overflowed -INF and +INF. */
1891 || (code == RSHIFT_EXPR
1892 && sgn1 >= 0)
1893 /* For division, the only case is -INF / -1 = +INF. */
1894 || code == TRUNC_DIV_EXPR
1895 || code == FLOOR_DIV_EXPR
1896 || code == CEIL_DIV_EXPR
1897 || code == EXACT_DIV_EXPR
1898 || code == ROUND_DIV_EXPR)
1899 return (needs_overflow_infinity (TREE_TYPE (res))
1900 ? positive_overflow_infinity (TREE_TYPE (res))
1901 : TYPE_MAX_VALUE (TREE_TYPE (res)));
1902 else
1903 return (needs_overflow_infinity (TREE_TYPE (res))
1904 ? negative_overflow_infinity (TREE_TYPE (res))
1905 : TYPE_MIN_VALUE (TREE_TYPE (res)));
1908 return res;
1912 /* For range VR compute two wide_int bitmasks. In *MAY_BE_NONZERO
1913 bitmask if some bit is unset, it means for all numbers in the range
1914 the bit is 0, otherwise it might be 0 or 1. In *MUST_BE_NONZERO
1915 bitmask if some bit is set, it means for all numbers in the range
1916 the bit is 1, otherwise it might be 0 or 1. */
1918 static bool
1919 zero_nonzero_bits_from_vr (const tree expr_type,
1920 value_range *vr,
1921 wide_int *may_be_nonzero,
1922 wide_int *must_be_nonzero)
1924 *may_be_nonzero = wi::minus_one (TYPE_PRECISION (expr_type));
1925 *must_be_nonzero = wi::zero (TYPE_PRECISION (expr_type));
1926 if (!range_int_cst_p (vr)
1927 || is_overflow_infinity (vr->min)
1928 || is_overflow_infinity (vr->max))
1929 return false;
1931 if (range_int_cst_singleton_p (vr))
1933 *may_be_nonzero = vr->min;
1934 *must_be_nonzero = *may_be_nonzero;
1936 else if (tree_int_cst_sgn (vr->min) >= 0
1937 || tree_int_cst_sgn (vr->max) < 0)
1939 wide_int xor_mask = wi::bit_xor (vr->min, vr->max);
1940 *may_be_nonzero = wi::bit_or (vr->min, vr->max);
1941 *must_be_nonzero = wi::bit_and (vr->min, vr->max);
1942 if (xor_mask != 0)
1944 wide_int mask = wi::mask (wi::floor_log2 (xor_mask), false,
1945 may_be_nonzero->get_precision ());
1946 *may_be_nonzero = *may_be_nonzero | mask;
1947 *must_be_nonzero = must_be_nonzero->and_not (mask);
1951 return true;
1954 /* Create two value-ranges in *VR0 and *VR1 from the anti-range *AR
1955 so that *VR0 U *VR1 == *AR. Returns true if that is possible,
1956 false otherwise. If *AR can be represented with a single range
1957 *VR1 will be VR_UNDEFINED. */
1959 static bool
1960 ranges_from_anti_range (value_range *ar,
1961 value_range *vr0, value_range *vr1)
1963 tree type = TREE_TYPE (ar->min);
1965 vr0->type = VR_UNDEFINED;
1966 vr1->type = VR_UNDEFINED;
1968 if (ar->type != VR_ANTI_RANGE
1969 || TREE_CODE (ar->min) != INTEGER_CST
1970 || TREE_CODE (ar->max) != INTEGER_CST
1971 || !vrp_val_min (type)
1972 || !vrp_val_max (type))
1973 return false;
1975 if (!vrp_val_is_min (ar->min))
1977 vr0->type = VR_RANGE;
1978 vr0->min = vrp_val_min (type);
1979 vr0->max = wide_int_to_tree (type, wi::sub (ar->min, 1));
1981 if (!vrp_val_is_max (ar->max))
1983 vr1->type = VR_RANGE;
1984 vr1->min = wide_int_to_tree (type, wi::add (ar->max, 1));
1985 vr1->max = vrp_val_max (type);
1987 if (vr0->type == VR_UNDEFINED)
1989 *vr0 = *vr1;
1990 vr1->type = VR_UNDEFINED;
1993 return vr0->type != VR_UNDEFINED;
1996 /* Helper to extract a value-range *VR for a multiplicative operation
1997 *VR0 CODE *VR1. */
1999 static void
2000 extract_range_from_multiplicative_op_1 (value_range *vr,
2001 enum tree_code code,
2002 value_range *vr0, value_range *vr1)
2004 enum value_range_type type;
2005 tree val[4];
2006 size_t i;
2007 tree min, max;
2008 bool sop;
2009 int cmp;
2011 /* Multiplications, divisions and shifts are a bit tricky to handle,
2012 depending on the mix of signs we have in the two ranges, we
2013 need to operate on different values to get the minimum and
2014 maximum values for the new range. One approach is to figure
2015 out all the variations of range combinations and do the
2016 operations.
2018 However, this involves several calls to compare_values and it
2019 is pretty convoluted. It's simpler to do the 4 operations
2020 (MIN0 OP MIN1, MIN0 OP MAX1, MAX0 OP MIN1 and MAX0 OP MAX0 OP
2021 MAX1) and then figure the smallest and largest values to form
2022 the new range. */
2023 gcc_assert (code == MULT_EXPR
2024 || code == TRUNC_DIV_EXPR
2025 || code == FLOOR_DIV_EXPR
2026 || code == CEIL_DIV_EXPR
2027 || code == EXACT_DIV_EXPR
2028 || code == ROUND_DIV_EXPR
2029 || code == RSHIFT_EXPR
2030 || code == LSHIFT_EXPR);
2031 gcc_assert ((vr0->type == VR_RANGE
2032 || (code == MULT_EXPR && vr0->type == VR_ANTI_RANGE))
2033 && vr0->type == vr1->type);
2035 type = vr0->type;
2037 /* Compute the 4 cross operations. */
2038 sop = false;
2039 val[0] = vrp_int_const_binop (code, vr0->min, vr1->min);
2040 if (val[0] == NULL_TREE)
2041 sop = true;
2043 if (vr1->max == vr1->min)
2044 val[1] = NULL_TREE;
2045 else
2047 val[1] = vrp_int_const_binop (code, vr0->min, vr1->max);
2048 if (val[1] == NULL_TREE)
2049 sop = true;
2052 if (vr0->max == vr0->min)
2053 val[2] = NULL_TREE;
2054 else
2056 val[2] = vrp_int_const_binop (code, vr0->max, vr1->min);
2057 if (val[2] == NULL_TREE)
2058 sop = true;
2061 if (vr0->min == vr0->max || vr1->min == vr1->max)
2062 val[3] = NULL_TREE;
2063 else
2065 val[3] = vrp_int_const_binop (code, vr0->max, vr1->max);
2066 if (val[3] == NULL_TREE)
2067 sop = true;
2070 if (sop)
2072 set_value_range_to_varying (vr);
2073 return;
2076 /* Set MIN to the minimum of VAL[i] and MAX to the maximum
2077 of VAL[i]. */
2078 min = val[0];
2079 max = val[0];
2080 for (i = 1; i < 4; i++)
2082 if (!is_gimple_min_invariant (min)
2083 || (TREE_OVERFLOW (min) && !is_overflow_infinity (min))
2084 || !is_gimple_min_invariant (max)
2085 || (TREE_OVERFLOW (max) && !is_overflow_infinity (max)))
2086 break;
2088 if (val[i])
2090 if (!is_gimple_min_invariant (val[i])
2091 || (TREE_OVERFLOW (val[i])
2092 && !is_overflow_infinity (val[i])))
2094 /* If we found an overflowed value, set MIN and MAX
2095 to it so that we set the resulting range to
2096 VARYING. */
2097 min = max = val[i];
2098 break;
2101 if (compare_values (val[i], min) == -1)
2102 min = val[i];
2104 if (compare_values (val[i], max) == 1)
2105 max = val[i];
2109 /* If either MIN or MAX overflowed, then set the resulting range to
2110 VARYING. But we do accept an overflow infinity
2111 representation. */
2112 if (min == NULL_TREE
2113 || !is_gimple_min_invariant (min)
2114 || (TREE_OVERFLOW (min) && !is_overflow_infinity (min))
2115 || max == NULL_TREE
2116 || !is_gimple_min_invariant (max)
2117 || (TREE_OVERFLOW (max) && !is_overflow_infinity (max)))
2119 set_value_range_to_varying (vr);
2120 return;
2123 /* We punt if:
2124 1) [-INF, +INF]
2125 2) [-INF, +-INF(OVF)]
2126 3) [+-INF(OVF), +INF]
2127 4) [+-INF(OVF), +-INF(OVF)]
2128 We learn nothing when we have INF and INF(OVF) on both sides.
2129 Note that we do accept [-INF, -INF] and [+INF, +INF] without
2130 overflow. */
2131 if ((vrp_val_is_min (min) || is_overflow_infinity (min))
2132 && (vrp_val_is_max (max) || is_overflow_infinity (max)))
2134 set_value_range_to_varying (vr);
2135 return;
2138 cmp = compare_values (min, max);
2139 if (cmp == -2 || cmp == 1)
2141 /* If the new range has its limits swapped around (MIN > MAX),
2142 then the operation caused one of them to wrap around, mark
2143 the new range VARYING. */
2144 set_value_range_to_varying (vr);
2146 else
2147 set_value_range (vr, type, min, max, NULL);
2150 /* Extract range information from a binary operation CODE based on
2151 the ranges of each of its operands *VR0 and *VR1 with resulting
2152 type EXPR_TYPE. The resulting range is stored in *VR. */
2154 static void
2155 extract_range_from_binary_expr_1 (value_range *vr,
2156 enum tree_code code, tree expr_type,
2157 value_range *vr0_, value_range *vr1_)
2159 value_range vr0 = *vr0_, vr1 = *vr1_;
2160 value_range vrtem0 = VR_INITIALIZER, vrtem1 = VR_INITIALIZER;
2161 enum value_range_type type;
2162 tree min = NULL_TREE, max = NULL_TREE;
2163 int cmp;
2165 if (!INTEGRAL_TYPE_P (expr_type)
2166 && !POINTER_TYPE_P (expr_type))
2168 set_value_range_to_varying (vr);
2169 return;
2172 /* Not all binary expressions can be applied to ranges in a
2173 meaningful way. Handle only arithmetic operations. */
2174 if (code != PLUS_EXPR
2175 && code != MINUS_EXPR
2176 && code != POINTER_PLUS_EXPR
2177 && code != MULT_EXPR
2178 && code != TRUNC_DIV_EXPR
2179 && code != FLOOR_DIV_EXPR
2180 && code != CEIL_DIV_EXPR
2181 && code != EXACT_DIV_EXPR
2182 && code != ROUND_DIV_EXPR
2183 && code != TRUNC_MOD_EXPR
2184 && code != RSHIFT_EXPR
2185 && code != LSHIFT_EXPR
2186 && code != MIN_EXPR
2187 && code != MAX_EXPR
2188 && code != BIT_AND_EXPR
2189 && code != BIT_IOR_EXPR
2190 && code != BIT_XOR_EXPR)
2192 set_value_range_to_varying (vr);
2193 return;
2196 /* If both ranges are UNDEFINED, so is the result. */
2197 if (vr0.type == VR_UNDEFINED && vr1.type == VR_UNDEFINED)
2199 set_value_range_to_undefined (vr);
2200 return;
2202 /* If one of the ranges is UNDEFINED drop it to VARYING for the following
2203 code. At some point we may want to special-case operations that
2204 have UNDEFINED result for all or some value-ranges of the not UNDEFINED
2205 operand. */
2206 else if (vr0.type == VR_UNDEFINED)
2207 set_value_range_to_varying (&vr0);
2208 else if (vr1.type == VR_UNDEFINED)
2209 set_value_range_to_varying (&vr1);
2211 /* Now canonicalize anti-ranges to ranges when they are not symbolic
2212 and express ~[] op X as ([]' op X) U ([]'' op X). */
2213 if (vr0.type == VR_ANTI_RANGE
2214 && ranges_from_anti_range (&vr0, &vrtem0, &vrtem1))
2216 extract_range_from_binary_expr_1 (vr, code, expr_type, &vrtem0, vr1_);
2217 if (vrtem1.type != VR_UNDEFINED)
2219 value_range vrres = VR_INITIALIZER;
2220 extract_range_from_binary_expr_1 (&vrres, code, expr_type,
2221 &vrtem1, vr1_);
2222 vrp_meet (vr, &vrres);
2224 return;
2226 /* Likewise for X op ~[]. */
2227 if (vr1.type == VR_ANTI_RANGE
2228 && ranges_from_anti_range (&vr1, &vrtem0, &vrtem1))
2230 extract_range_from_binary_expr_1 (vr, code, expr_type, vr0_, &vrtem0);
2231 if (vrtem1.type != VR_UNDEFINED)
2233 value_range vrres = VR_INITIALIZER;
2234 extract_range_from_binary_expr_1 (&vrres, code, expr_type,
2235 vr0_, &vrtem1);
2236 vrp_meet (vr, &vrres);
2238 return;
2241 /* The type of the resulting value range defaults to VR0.TYPE. */
2242 type = vr0.type;
2244 /* Refuse to operate on VARYING ranges, ranges of different kinds
2245 and symbolic ranges. As an exception, we allow BIT_{AND,IOR}
2246 because we may be able to derive a useful range even if one of
2247 the operands is VR_VARYING or symbolic range. Similarly for
2248 divisions, MIN/MAX and PLUS/MINUS.
2250 TODO, we may be able to derive anti-ranges in some cases. */
2251 if (code != BIT_AND_EXPR
2252 && code != BIT_IOR_EXPR
2253 && code != TRUNC_DIV_EXPR
2254 && code != FLOOR_DIV_EXPR
2255 && code != CEIL_DIV_EXPR
2256 && code != EXACT_DIV_EXPR
2257 && code != ROUND_DIV_EXPR
2258 && code != TRUNC_MOD_EXPR
2259 && code != MIN_EXPR
2260 && code != MAX_EXPR
2261 && code != PLUS_EXPR
2262 && code != MINUS_EXPR
2263 && code != RSHIFT_EXPR
2264 && (vr0.type == VR_VARYING
2265 || vr1.type == VR_VARYING
2266 || vr0.type != vr1.type
2267 || symbolic_range_p (&vr0)
2268 || symbolic_range_p (&vr1)))
2270 set_value_range_to_varying (vr);
2271 return;
2274 /* Now evaluate the expression to determine the new range. */
2275 if (POINTER_TYPE_P (expr_type))
2277 if (code == MIN_EXPR || code == MAX_EXPR)
2279 /* For MIN/MAX expressions with pointers, we only care about
2280 nullness, if both are non null, then the result is nonnull.
2281 If both are null, then the result is null. Otherwise they
2282 are varying. */
2283 if (range_is_nonnull (&vr0) && range_is_nonnull (&vr1))
2284 set_value_range_to_nonnull (vr, expr_type);
2285 else if (range_is_null (&vr0) && range_is_null (&vr1))
2286 set_value_range_to_null (vr, expr_type);
2287 else
2288 set_value_range_to_varying (vr);
2290 else if (code == POINTER_PLUS_EXPR)
2292 /* For pointer types, we are really only interested in asserting
2293 whether the expression evaluates to non-NULL. */
2294 if (range_is_nonnull (&vr0) || range_is_nonnull (&vr1))
2295 set_value_range_to_nonnull (vr, expr_type);
2296 else if (range_is_null (&vr0) && range_is_null (&vr1))
2297 set_value_range_to_null (vr, expr_type);
2298 else
2299 set_value_range_to_varying (vr);
2301 else if (code == BIT_AND_EXPR)
2303 /* For pointer types, we are really only interested in asserting
2304 whether the expression evaluates to non-NULL. */
2305 if (range_is_nonnull (&vr0) && range_is_nonnull (&vr1))
2306 set_value_range_to_nonnull (vr, expr_type);
2307 else if (range_is_null (&vr0) || range_is_null (&vr1))
2308 set_value_range_to_null (vr, expr_type);
2309 else
2310 set_value_range_to_varying (vr);
2312 else
2313 set_value_range_to_varying (vr);
2315 return;
2318 /* For integer ranges, apply the operation to each end of the
2319 range and see what we end up with. */
2320 if (code == PLUS_EXPR || code == MINUS_EXPR)
2322 const bool minus_p = (code == MINUS_EXPR);
2323 tree min_op0 = vr0.min;
2324 tree min_op1 = minus_p ? vr1.max : vr1.min;
2325 tree max_op0 = vr0.max;
2326 tree max_op1 = minus_p ? vr1.min : vr1.max;
2327 tree sym_min_op0 = NULL_TREE;
2328 tree sym_min_op1 = NULL_TREE;
2329 tree sym_max_op0 = NULL_TREE;
2330 tree sym_max_op1 = NULL_TREE;
2331 bool neg_min_op0, neg_min_op1, neg_max_op0, neg_max_op1;
2333 /* If we have a PLUS or MINUS with two VR_RANGEs, either constant or
2334 single-symbolic ranges, try to compute the precise resulting range,
2335 but only if we know that this resulting range will also be constant
2336 or single-symbolic. */
2337 if (vr0.type == VR_RANGE && vr1.type == VR_RANGE
2338 && (TREE_CODE (min_op0) == INTEGER_CST
2339 || (sym_min_op0
2340 = get_single_symbol (min_op0, &neg_min_op0, &min_op0)))
2341 && (TREE_CODE (min_op1) == INTEGER_CST
2342 || (sym_min_op1
2343 = get_single_symbol (min_op1, &neg_min_op1, &min_op1)))
2344 && (!(sym_min_op0 && sym_min_op1)
2345 || (sym_min_op0 == sym_min_op1
2346 && neg_min_op0 == (minus_p ? neg_min_op1 : !neg_min_op1)))
2347 && (TREE_CODE (max_op0) == INTEGER_CST
2348 || (sym_max_op0
2349 = get_single_symbol (max_op0, &neg_max_op0, &max_op0)))
2350 && (TREE_CODE (max_op1) == INTEGER_CST
2351 || (sym_max_op1
2352 = get_single_symbol (max_op1, &neg_max_op1, &max_op1)))
2353 && (!(sym_max_op0 && sym_max_op1)
2354 || (sym_max_op0 == sym_max_op1
2355 && neg_max_op0 == (minus_p ? neg_max_op1 : !neg_max_op1))))
2357 const signop sgn = TYPE_SIGN (expr_type);
2358 const unsigned int prec = TYPE_PRECISION (expr_type);
2359 wide_int type_min, type_max, wmin, wmax;
2360 int min_ovf = 0;
2361 int max_ovf = 0;
2363 /* Get the lower and upper bounds of the type. */
2364 if (TYPE_OVERFLOW_WRAPS (expr_type))
2366 type_min = wi::min_value (prec, sgn);
2367 type_max = wi::max_value (prec, sgn);
2369 else
2371 type_min = vrp_val_min (expr_type);
2372 type_max = vrp_val_max (expr_type);
2375 /* Combine the lower bounds, if any. */
2376 if (min_op0 && min_op1)
2378 if (minus_p)
2380 wmin = wi::sub (min_op0, min_op1);
2382 /* Check for overflow. */
2383 if (wi::cmp (0, min_op1, sgn)
2384 != wi::cmp (wmin, min_op0, sgn))
2385 min_ovf = wi::cmp (min_op0, min_op1, sgn);
2387 else
2389 wmin = wi::add (min_op0, min_op1);
2391 /* Check for overflow. */
2392 if (wi::cmp (min_op1, 0, sgn)
2393 != wi::cmp (wmin, min_op0, sgn))
2394 min_ovf = wi::cmp (min_op0, wmin, sgn);
2397 else if (min_op0)
2398 wmin = min_op0;
2399 else if (min_op1)
2400 wmin = minus_p ? wi::neg (min_op1) : min_op1;
2401 else
2402 wmin = wi::shwi (0, prec);
2404 /* Combine the upper bounds, if any. */
2405 if (max_op0 && max_op1)
2407 if (minus_p)
2409 wmax = wi::sub (max_op0, max_op1);
2411 /* Check for overflow. */
2412 if (wi::cmp (0, max_op1, sgn)
2413 != wi::cmp (wmax, max_op0, sgn))
2414 max_ovf = wi::cmp (max_op0, max_op1, sgn);
2416 else
2418 wmax = wi::add (max_op0, max_op1);
2420 if (wi::cmp (max_op1, 0, sgn)
2421 != wi::cmp (wmax, max_op0, sgn))
2422 max_ovf = wi::cmp (max_op0, wmax, sgn);
2425 else if (max_op0)
2426 wmax = max_op0;
2427 else if (max_op1)
2428 wmax = minus_p ? wi::neg (max_op1) : max_op1;
2429 else
2430 wmax = wi::shwi (0, prec);
2432 /* Check for type overflow. */
2433 if (min_ovf == 0)
2435 if (wi::cmp (wmin, type_min, sgn) == -1)
2436 min_ovf = -1;
2437 else if (wi::cmp (wmin, type_max, sgn) == 1)
2438 min_ovf = 1;
2440 if (max_ovf == 0)
2442 if (wi::cmp (wmax, type_min, sgn) == -1)
2443 max_ovf = -1;
2444 else if (wi::cmp (wmax, type_max, sgn) == 1)
2445 max_ovf = 1;
2448 /* If we have overflow for the constant part and the resulting
2449 range will be symbolic, drop to VR_VARYING. */
2450 if ((min_ovf && sym_min_op0 != sym_min_op1)
2451 || (max_ovf && sym_max_op0 != sym_max_op1))
2453 set_value_range_to_varying (vr);
2454 return;
2457 if (TYPE_OVERFLOW_WRAPS (expr_type))
2459 /* If overflow wraps, truncate the values and adjust the
2460 range kind and bounds appropriately. */
2461 wide_int tmin = wide_int::from (wmin, prec, sgn);
2462 wide_int tmax = wide_int::from (wmax, prec, sgn);
2463 if (min_ovf == max_ovf)
2465 /* No overflow or both overflow or underflow. The
2466 range kind stays VR_RANGE. */
2467 min = wide_int_to_tree (expr_type, tmin);
2468 max = wide_int_to_tree (expr_type, tmax);
2470 else if ((min_ovf == -1 && max_ovf == 0)
2471 || (max_ovf == 1 && min_ovf == 0))
2473 /* Min underflow or max overflow. The range kind
2474 changes to VR_ANTI_RANGE. */
2475 bool covers = false;
2476 wide_int tem = tmin;
2477 type = VR_ANTI_RANGE;
2478 tmin = tmax + 1;
2479 if (wi::cmp (tmin, tmax, sgn) < 0)
2480 covers = true;
2481 tmax = tem - 1;
2482 if (wi::cmp (tmax, tem, sgn) > 0)
2483 covers = true;
2484 /* If the anti-range would cover nothing, drop to varying.
2485 Likewise if the anti-range bounds are outside of the
2486 types values. */
2487 if (covers || wi::cmp (tmin, tmax, sgn) > 0)
2489 set_value_range_to_varying (vr);
2490 return;
2492 min = wide_int_to_tree (expr_type, tmin);
2493 max = wide_int_to_tree (expr_type, tmax);
2495 else
2497 /* Other underflow and/or overflow, drop to VR_VARYING. */
2498 set_value_range_to_varying (vr);
2499 return;
2502 else
2504 /* If overflow does not wrap, saturate to the types min/max
2505 value. */
2506 if (min_ovf == -1)
2508 if (needs_overflow_infinity (expr_type)
2509 && supports_overflow_infinity (expr_type))
2510 min = negative_overflow_infinity (expr_type);
2511 else
2512 min = wide_int_to_tree (expr_type, type_min);
2514 else if (min_ovf == 1)
2516 if (needs_overflow_infinity (expr_type)
2517 && supports_overflow_infinity (expr_type))
2518 min = positive_overflow_infinity (expr_type);
2519 else
2520 min = wide_int_to_tree (expr_type, type_max);
2522 else
2523 min = wide_int_to_tree (expr_type, wmin);
2525 if (max_ovf == -1)
2527 if (needs_overflow_infinity (expr_type)
2528 && supports_overflow_infinity (expr_type))
2529 max = negative_overflow_infinity (expr_type);
2530 else
2531 max = wide_int_to_tree (expr_type, type_min);
2533 else if (max_ovf == 1)
2535 if (needs_overflow_infinity (expr_type)
2536 && supports_overflow_infinity (expr_type))
2537 max = positive_overflow_infinity (expr_type);
2538 else
2539 max = wide_int_to_tree (expr_type, type_max);
2541 else
2542 max = wide_int_to_tree (expr_type, wmax);
2545 if (needs_overflow_infinity (expr_type)
2546 && supports_overflow_infinity (expr_type))
2548 if ((min_op0 && is_negative_overflow_infinity (min_op0))
2549 || (min_op1
2550 && (minus_p
2551 ? is_positive_overflow_infinity (min_op1)
2552 : is_negative_overflow_infinity (min_op1))))
2553 min = negative_overflow_infinity (expr_type);
2554 if ((max_op0 && is_positive_overflow_infinity (max_op0))
2555 || (max_op1
2556 && (minus_p
2557 ? is_negative_overflow_infinity (max_op1)
2558 : is_positive_overflow_infinity (max_op1))))
2559 max = positive_overflow_infinity (expr_type);
2562 /* If the result lower bound is constant, we're done;
2563 otherwise, build the symbolic lower bound. */
2564 if (sym_min_op0 == sym_min_op1)
2566 else if (sym_min_op0)
2567 min = build_symbolic_expr (expr_type, sym_min_op0,
2568 neg_min_op0, min);
2569 else if (sym_min_op1)
2570 min = build_symbolic_expr (expr_type, sym_min_op1,
2571 neg_min_op1 ^ minus_p, min);
2573 /* Likewise for the upper bound. */
2574 if (sym_max_op0 == sym_max_op1)
2576 else if (sym_max_op0)
2577 max = build_symbolic_expr (expr_type, sym_max_op0,
2578 neg_max_op0, max);
2579 else if (sym_max_op1)
2580 max = build_symbolic_expr (expr_type, sym_max_op1,
2581 neg_max_op1 ^ minus_p, max);
2583 else
2585 /* For other cases, for example if we have a PLUS_EXPR with two
2586 VR_ANTI_RANGEs, drop to VR_VARYING. It would take more effort
2587 to compute a precise range for such a case.
2588 ??? General even mixed range kind operations can be expressed
2589 by for example transforming ~[3, 5] + [1, 2] to range-only
2590 operations and a union primitive:
2591 [-INF, 2] + [1, 2] U [5, +INF] + [1, 2]
2592 [-INF+1, 4] U [6, +INF(OVF)]
2593 though usually the union is not exactly representable with
2594 a single range or anti-range as the above is
2595 [-INF+1, +INF(OVF)] intersected with ~[5, 5]
2596 but one could use a scheme similar to equivalences for this. */
2597 set_value_range_to_varying (vr);
2598 return;
2601 else if (code == MIN_EXPR
2602 || code == MAX_EXPR)
2604 if (vr0.type == VR_RANGE
2605 && !symbolic_range_p (&vr0))
2607 type = VR_RANGE;
2608 if (vr1.type == VR_RANGE
2609 && !symbolic_range_p (&vr1))
2611 /* For operations that make the resulting range directly
2612 proportional to the original ranges, apply the operation to
2613 the same end of each range. */
2614 min = vrp_int_const_binop (code, vr0.min, vr1.min);
2615 max = vrp_int_const_binop (code, vr0.max, vr1.max);
2617 else if (code == MIN_EXPR)
2619 min = vrp_val_min (expr_type);
2620 max = vr0.max;
2622 else if (code == MAX_EXPR)
2624 min = vr0.min;
2625 max = vrp_val_max (expr_type);
2628 else if (vr1.type == VR_RANGE
2629 && !symbolic_range_p (&vr1))
2631 type = VR_RANGE;
2632 if (code == MIN_EXPR)
2634 min = vrp_val_min (expr_type);
2635 max = vr1.max;
2637 else if (code == MAX_EXPR)
2639 min = vr1.min;
2640 max = vrp_val_max (expr_type);
2643 else
2645 set_value_range_to_varying (vr);
2646 return;
2649 else if (code == MULT_EXPR)
2651 /* Fancy code so that with unsigned, [-3,-1]*[-3,-1] does not
2652 drop to varying. This test requires 2*prec bits if both
2653 operands are signed and 2*prec + 2 bits if either is not. */
2655 signop sign = TYPE_SIGN (expr_type);
2656 unsigned int prec = TYPE_PRECISION (expr_type);
2658 if (range_int_cst_p (&vr0)
2659 && range_int_cst_p (&vr1)
2660 && TYPE_OVERFLOW_WRAPS (expr_type))
2662 typedef FIXED_WIDE_INT (WIDE_INT_MAX_PRECISION * 2) vrp_int;
2663 typedef generic_wide_int
2664 <wi::extended_tree <WIDE_INT_MAX_PRECISION * 2> > vrp_int_cst;
2665 vrp_int sizem1 = wi::mask <vrp_int> (prec, false);
2666 vrp_int size = sizem1 + 1;
2668 /* Extend the values using the sign of the result to PREC2.
2669 From here on out, everthing is just signed math no matter
2670 what the input types were. */
2671 vrp_int min0 = vrp_int_cst (vr0.min);
2672 vrp_int max0 = vrp_int_cst (vr0.max);
2673 vrp_int min1 = vrp_int_cst (vr1.min);
2674 vrp_int max1 = vrp_int_cst (vr1.max);
2675 /* Canonicalize the intervals. */
2676 if (sign == UNSIGNED)
2678 if (wi::ltu_p (size, min0 + max0))
2680 min0 -= size;
2681 max0 -= size;
2684 if (wi::ltu_p (size, min1 + max1))
2686 min1 -= size;
2687 max1 -= size;
2691 vrp_int prod0 = min0 * min1;
2692 vrp_int prod1 = min0 * max1;
2693 vrp_int prod2 = max0 * min1;
2694 vrp_int prod3 = max0 * max1;
2696 /* Sort the 4 products so that min is in prod0 and max is in
2697 prod3. */
2698 /* min0min1 > max0max1 */
2699 if (prod0 > prod3)
2700 std::swap (prod0, prod3);
2702 /* min0max1 > max0min1 */
2703 if (prod1 > prod2)
2704 std::swap (prod1, prod2);
2706 if (prod0 > prod1)
2707 std::swap (prod0, prod1);
2709 if (prod2 > prod3)
2710 std::swap (prod2, prod3);
2712 /* diff = max - min. */
2713 prod2 = prod3 - prod0;
2714 if (wi::geu_p (prod2, sizem1))
2716 /* the range covers all values. */
2717 set_value_range_to_varying (vr);
2718 return;
2721 /* The following should handle the wrapping and selecting
2722 VR_ANTI_RANGE for us. */
2723 min = wide_int_to_tree (expr_type, prod0);
2724 max = wide_int_to_tree (expr_type, prod3);
2725 set_and_canonicalize_value_range (vr, VR_RANGE, min, max, NULL);
2726 return;
2729 /* If we have an unsigned MULT_EXPR with two VR_ANTI_RANGEs,
2730 drop to VR_VARYING. It would take more effort to compute a
2731 precise range for such a case. For example, if we have
2732 op0 == 65536 and op1 == 65536 with their ranges both being
2733 ~[0,0] on a 32-bit machine, we would have op0 * op1 == 0, so
2734 we cannot claim that the product is in ~[0,0]. Note that we
2735 are guaranteed to have vr0.type == vr1.type at this
2736 point. */
2737 if (vr0.type == VR_ANTI_RANGE
2738 && !TYPE_OVERFLOW_UNDEFINED (expr_type))
2740 set_value_range_to_varying (vr);
2741 return;
2744 extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1);
2745 return;
2747 else if (code == RSHIFT_EXPR
2748 || code == LSHIFT_EXPR)
2750 /* If we have a RSHIFT_EXPR with any shift values outside [0..prec-1],
2751 then drop to VR_VARYING. Outside of this range we get undefined
2752 behavior from the shift operation. We cannot even trust
2753 SHIFT_COUNT_TRUNCATED at this stage, because that applies to rtl
2754 shifts, and the operation at the tree level may be widened. */
2755 if (range_int_cst_p (&vr1)
2756 && compare_tree_int (vr1.min, 0) >= 0
2757 && compare_tree_int (vr1.max, TYPE_PRECISION (expr_type)) == -1)
2759 if (code == RSHIFT_EXPR)
2761 /* Even if vr0 is VARYING or otherwise not usable, we can derive
2762 useful ranges just from the shift count. E.g.
2763 x >> 63 for signed 64-bit x is always [-1, 0]. */
2764 if (vr0.type != VR_RANGE || symbolic_range_p (&vr0))
2766 vr0.type = type = VR_RANGE;
2767 vr0.min = vrp_val_min (expr_type);
2768 vr0.max = vrp_val_max (expr_type);
2770 extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1);
2771 return;
2773 /* We can map lshifts by constants to MULT_EXPR handling. */
2774 else if (code == LSHIFT_EXPR
2775 && range_int_cst_singleton_p (&vr1))
2777 bool saved_flag_wrapv;
2778 value_range vr1p = VR_INITIALIZER;
2779 vr1p.type = VR_RANGE;
2780 vr1p.min = (wide_int_to_tree
2781 (expr_type,
2782 wi::set_bit_in_zero (tree_to_shwi (vr1.min),
2783 TYPE_PRECISION (expr_type))));
2784 vr1p.max = vr1p.min;
2785 /* We have to use a wrapping multiply though as signed overflow
2786 on lshifts is implementation defined in C89. */
2787 saved_flag_wrapv = flag_wrapv;
2788 flag_wrapv = 1;
2789 extract_range_from_binary_expr_1 (vr, MULT_EXPR, expr_type,
2790 &vr0, &vr1p);
2791 flag_wrapv = saved_flag_wrapv;
2792 return;
2794 else if (code == LSHIFT_EXPR
2795 && range_int_cst_p (&vr0))
2797 int prec = TYPE_PRECISION (expr_type);
2798 int overflow_pos = prec;
2799 int bound_shift;
2800 wide_int low_bound, high_bound;
2801 bool uns = TYPE_UNSIGNED (expr_type);
2802 bool in_bounds = false;
2804 if (!uns)
2805 overflow_pos -= 1;
2807 bound_shift = overflow_pos - tree_to_shwi (vr1.max);
2808 /* If bound_shift == HOST_BITS_PER_WIDE_INT, the llshift can
2809 overflow. However, for that to happen, vr1.max needs to be
2810 zero, which means vr1 is a singleton range of zero, which
2811 means it should be handled by the previous LSHIFT_EXPR
2812 if-clause. */
2813 wide_int bound = wi::set_bit_in_zero (bound_shift, prec);
2814 wide_int complement = ~(bound - 1);
2816 if (uns)
2818 low_bound = bound;
2819 high_bound = complement;
2820 if (wi::ltu_p (vr0.max, low_bound))
2822 /* [5, 6] << [1, 2] == [10, 24]. */
2823 /* We're shifting out only zeroes, the value increases
2824 monotonically. */
2825 in_bounds = true;
2827 else if (wi::ltu_p (high_bound, vr0.min))
2829 /* [0xffffff00, 0xffffffff] << [1, 2]
2830 == [0xfffffc00, 0xfffffffe]. */
2831 /* We're shifting out only ones, the value decreases
2832 monotonically. */
2833 in_bounds = true;
2836 else
2838 /* [-1, 1] << [1, 2] == [-4, 4]. */
2839 low_bound = complement;
2840 high_bound = bound;
2841 if (wi::lts_p (vr0.max, high_bound)
2842 && wi::lts_p (low_bound, vr0.min))
2844 /* For non-negative numbers, we're shifting out only
2845 zeroes, the value increases monotonically.
2846 For negative numbers, we're shifting out only ones, the
2847 value decreases monotomically. */
2848 in_bounds = true;
2852 if (in_bounds)
2854 extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1);
2855 return;
2859 set_value_range_to_varying (vr);
2860 return;
2862 else if (code == TRUNC_DIV_EXPR
2863 || code == FLOOR_DIV_EXPR
2864 || code == CEIL_DIV_EXPR
2865 || code == EXACT_DIV_EXPR
2866 || code == ROUND_DIV_EXPR)
2868 if (vr0.type != VR_RANGE || symbolic_range_p (&vr0))
2870 /* For division, if op1 has VR_RANGE but op0 does not, something
2871 can be deduced just from that range. Say [min, max] / [4, max]
2872 gives [min / 4, max / 4] range. */
2873 if (vr1.type == VR_RANGE
2874 && !symbolic_range_p (&vr1)
2875 && range_includes_zero_p (vr1.min, vr1.max) == 0)
2877 vr0.type = type = VR_RANGE;
2878 vr0.min = vrp_val_min (expr_type);
2879 vr0.max = vrp_val_max (expr_type);
2881 else
2883 set_value_range_to_varying (vr);
2884 return;
2888 /* For divisions, if flag_non_call_exceptions is true, we must
2889 not eliminate a division by zero. */
2890 if (cfun->can_throw_non_call_exceptions
2891 && (vr1.type != VR_RANGE
2892 || range_includes_zero_p (vr1.min, vr1.max) != 0))
2894 set_value_range_to_varying (vr);
2895 return;
2898 /* For divisions, if op0 is VR_RANGE, we can deduce a range
2899 even if op1 is VR_VARYING, VR_ANTI_RANGE, symbolic or can
2900 include 0. */
2901 if (vr0.type == VR_RANGE
2902 && (vr1.type != VR_RANGE
2903 || range_includes_zero_p (vr1.min, vr1.max) != 0))
2905 tree zero = build_int_cst (TREE_TYPE (vr0.min), 0);
2906 int cmp;
2908 min = NULL_TREE;
2909 max = NULL_TREE;
2910 if (TYPE_UNSIGNED (expr_type)
2911 || value_range_nonnegative_p (&vr1))
2913 /* For unsigned division or when divisor is known
2914 to be non-negative, the range has to cover
2915 all numbers from 0 to max for positive max
2916 and all numbers from min to 0 for negative min. */
2917 cmp = compare_values (vr0.max, zero);
2918 if (cmp == -1)
2920 /* When vr0.max < 0, vr1.min != 0 and value
2921 ranges for dividend and divisor are available. */
2922 if (vr1.type == VR_RANGE
2923 && !symbolic_range_p (&vr0)
2924 && !symbolic_range_p (&vr1)
2925 && compare_values (vr1.min, zero) != 0)
2926 max = int_const_binop (code, vr0.max, vr1.min);
2927 else
2928 max = zero;
2930 else if (cmp == 0 || cmp == 1)
2931 max = vr0.max;
2932 else
2933 type = VR_VARYING;
2934 cmp = compare_values (vr0.min, zero);
2935 if (cmp == 1)
2937 /* For unsigned division when value ranges for dividend
2938 and divisor are available. */
2939 if (vr1.type == VR_RANGE
2940 && !symbolic_range_p (&vr0)
2941 && !symbolic_range_p (&vr1))
2942 min = int_const_binop (code, vr0.min, vr1.max);
2943 else
2944 min = zero;
2946 else if (cmp == 0 || cmp == -1)
2947 min = vr0.min;
2948 else
2949 type = VR_VARYING;
2951 else
2953 /* Otherwise the range is -max .. max or min .. -min
2954 depending on which bound is bigger in absolute value,
2955 as the division can change the sign. */
2956 abs_extent_range (vr, vr0.min, vr0.max);
2957 return;
2959 if (type == VR_VARYING)
2961 set_value_range_to_varying (vr);
2962 return;
2965 else if (!symbolic_range_p (&vr0) && !symbolic_range_p (&vr1))
2967 extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1);
2968 return;
2971 else if (code == TRUNC_MOD_EXPR)
2973 if (range_is_null (&vr1))
2975 set_value_range_to_undefined (vr);
2976 return;
2978 /* ABS (A % B) < ABS (B) and either
2979 0 <= A % B <= A or A <= A % B <= 0. */
2980 type = VR_RANGE;
2981 signop sgn = TYPE_SIGN (expr_type);
2982 unsigned int prec = TYPE_PRECISION (expr_type);
2983 wide_int wmin, wmax, tmp;
2984 wide_int zero = wi::zero (prec);
2985 wide_int one = wi::one (prec);
2986 if (vr1.type == VR_RANGE && !symbolic_range_p (&vr1))
2988 wmax = wi::sub (vr1.max, one);
2989 if (sgn == SIGNED)
2991 tmp = wi::sub (wi::minus_one (prec), vr1.min);
2992 wmax = wi::smax (wmax, tmp);
2995 else
2997 wmax = wi::max_value (prec, sgn);
2998 /* X % INT_MIN may be INT_MAX. */
2999 if (sgn == UNSIGNED)
3000 wmax = wmax - one;
3003 if (sgn == UNSIGNED)
3004 wmin = zero;
3005 else
3007 wmin = -wmax;
3008 if (vr0.type == VR_RANGE && TREE_CODE (vr0.min) == INTEGER_CST)
3010 tmp = vr0.min;
3011 if (wi::gts_p (tmp, zero))
3012 tmp = zero;
3013 wmin = wi::smax (wmin, tmp);
3017 if (vr0.type == VR_RANGE && TREE_CODE (vr0.max) == INTEGER_CST)
3019 tmp = vr0.max;
3020 if (sgn == SIGNED && wi::neg_p (tmp))
3021 tmp = zero;
3022 wmax = wi::min (wmax, tmp, sgn);
3025 min = wide_int_to_tree (expr_type, wmin);
3026 max = wide_int_to_tree (expr_type, wmax);
3028 else if (code == BIT_AND_EXPR || code == BIT_IOR_EXPR || code == BIT_XOR_EXPR)
3030 bool int_cst_range0, int_cst_range1;
3031 wide_int may_be_nonzero0, may_be_nonzero1;
3032 wide_int must_be_nonzero0, must_be_nonzero1;
3034 int_cst_range0 = zero_nonzero_bits_from_vr (expr_type, &vr0,
3035 &may_be_nonzero0,
3036 &must_be_nonzero0);
3037 int_cst_range1 = zero_nonzero_bits_from_vr (expr_type, &vr1,
3038 &may_be_nonzero1,
3039 &must_be_nonzero1);
3041 type = VR_RANGE;
3042 if (code == BIT_AND_EXPR)
3044 min = wide_int_to_tree (expr_type,
3045 must_be_nonzero0 & must_be_nonzero1);
3046 wide_int wmax = may_be_nonzero0 & may_be_nonzero1;
3047 /* If both input ranges contain only negative values we can
3048 truncate the result range maximum to the minimum of the
3049 input range maxima. */
3050 if (int_cst_range0 && int_cst_range1
3051 && tree_int_cst_sgn (vr0.max) < 0
3052 && tree_int_cst_sgn (vr1.max) < 0)
3054 wmax = wi::min (wmax, vr0.max, TYPE_SIGN (expr_type));
3055 wmax = wi::min (wmax, vr1.max, TYPE_SIGN (expr_type));
3057 /* If either input range contains only non-negative values
3058 we can truncate the result range maximum to the respective
3059 maximum of the input range. */
3060 if (int_cst_range0 && tree_int_cst_sgn (vr0.min) >= 0)
3061 wmax = wi::min (wmax, vr0.max, TYPE_SIGN (expr_type));
3062 if (int_cst_range1 && tree_int_cst_sgn (vr1.min) >= 0)
3063 wmax = wi::min (wmax, vr1.max, TYPE_SIGN (expr_type));
3064 max = wide_int_to_tree (expr_type, wmax);
3066 else if (code == BIT_IOR_EXPR)
3068 max = wide_int_to_tree (expr_type,
3069 may_be_nonzero0 | may_be_nonzero1);
3070 wide_int wmin = must_be_nonzero0 | must_be_nonzero1;
3071 /* If the input ranges contain only positive values we can
3072 truncate the minimum of the result range to the maximum
3073 of the input range minima. */
3074 if (int_cst_range0 && int_cst_range1
3075 && tree_int_cst_sgn (vr0.min) >= 0
3076 && tree_int_cst_sgn (vr1.min) >= 0)
3078 wmin = wi::max (wmin, vr0.min, TYPE_SIGN (expr_type));
3079 wmin = wi::max (wmin, vr1.min, TYPE_SIGN (expr_type));
3081 /* If either input range contains only negative values
3082 we can truncate the minimum of the result range to the
3083 respective minimum range. */
3084 if (int_cst_range0 && tree_int_cst_sgn (vr0.max) < 0)
3085 wmin = wi::max (wmin, vr0.min, TYPE_SIGN (expr_type));
3086 if (int_cst_range1 && tree_int_cst_sgn (vr1.max) < 0)
3087 wmin = wi::max (wmin, vr1.min, TYPE_SIGN (expr_type));
3088 min = wide_int_to_tree (expr_type, wmin);
3090 else if (code == BIT_XOR_EXPR)
3092 wide_int result_zero_bits = ((must_be_nonzero0 & must_be_nonzero1)
3093 | ~(may_be_nonzero0 | may_be_nonzero1));
3094 wide_int result_one_bits
3095 = (must_be_nonzero0.and_not (may_be_nonzero1)
3096 | must_be_nonzero1.and_not (may_be_nonzero0));
3097 max = wide_int_to_tree (expr_type, ~result_zero_bits);
3098 min = wide_int_to_tree (expr_type, result_one_bits);
3099 /* If the range has all positive or all negative values the
3100 result is better than VARYING. */
3101 if (tree_int_cst_sgn (min) < 0
3102 || tree_int_cst_sgn (max) >= 0)
3104 else
3105 max = min = NULL_TREE;
3108 else
3109 gcc_unreachable ();
3111 /* If either MIN or MAX overflowed, then set the resulting range to
3112 VARYING. But we do accept an overflow infinity representation. */
3113 if (min == NULL_TREE
3114 || (TREE_OVERFLOW_P (min) && !is_overflow_infinity (min))
3115 || max == NULL_TREE
3116 || (TREE_OVERFLOW_P (max) && !is_overflow_infinity (max)))
3118 set_value_range_to_varying (vr);
3119 return;
3122 /* We punt if:
3123 1) [-INF, +INF]
3124 2) [-INF, +-INF(OVF)]
3125 3) [+-INF(OVF), +INF]
3126 4) [+-INF(OVF), +-INF(OVF)]
3127 We learn nothing when we have INF and INF(OVF) on both sides.
3128 Note that we do accept [-INF, -INF] and [+INF, +INF] without
3129 overflow. */
3130 if ((vrp_val_is_min (min) || is_overflow_infinity (min))
3131 && (vrp_val_is_max (max) || is_overflow_infinity (max)))
3133 set_value_range_to_varying (vr);
3134 return;
3137 cmp = compare_values (min, max);
3138 if (cmp == -2 || cmp == 1)
3140 /* If the new range has its limits swapped around (MIN > MAX),
3141 then the operation caused one of them to wrap around, mark
3142 the new range VARYING. */
3143 set_value_range_to_varying (vr);
3145 else
3146 set_value_range (vr, type, min, max, NULL);
3149 /* Extract range information from a binary expression OP0 CODE OP1 based on
3150 the ranges of each of its operands with resulting type EXPR_TYPE.
3151 The resulting range is stored in *VR. */
3153 static void
3154 extract_range_from_binary_expr (value_range *vr,
3155 enum tree_code code,
3156 tree expr_type, tree op0, tree op1)
3158 value_range vr0 = VR_INITIALIZER;
3159 value_range vr1 = VR_INITIALIZER;
3161 /* Get value ranges for each operand. For constant operands, create
3162 a new value range with the operand to simplify processing. */
3163 if (TREE_CODE (op0) == SSA_NAME)
3164 vr0 = *(get_value_range (op0));
3165 else if (is_gimple_min_invariant (op0))
3166 set_value_range_to_value (&vr0, op0, NULL);
3167 else
3168 set_value_range_to_varying (&vr0);
3170 if (TREE_CODE (op1) == SSA_NAME)
3171 vr1 = *(get_value_range (op1));
3172 else if (is_gimple_min_invariant (op1))
3173 set_value_range_to_value (&vr1, op1, NULL);
3174 else
3175 set_value_range_to_varying (&vr1);
3177 extract_range_from_binary_expr_1 (vr, code, expr_type, &vr0, &vr1);
3179 /* Try harder for PLUS and MINUS if the range of one operand is symbolic
3180 and based on the other operand, for example if it was deduced from a
3181 symbolic comparison. When a bound of the range of the first operand
3182 is invariant, we set the corresponding bound of the new range to INF
3183 in order to avoid recursing on the range of the second operand. */
3184 if (vr->type == VR_VARYING
3185 && (code == PLUS_EXPR || code == MINUS_EXPR)
3186 && TREE_CODE (op1) == SSA_NAME
3187 && vr0.type == VR_RANGE
3188 && symbolic_range_based_on_p (&vr0, op1))
3190 const bool minus_p = (code == MINUS_EXPR);
3191 value_range n_vr1 = VR_INITIALIZER;
3193 /* Try with VR0 and [-INF, OP1]. */
3194 if (is_gimple_min_invariant (minus_p ? vr0.max : vr0.min))
3195 set_value_range (&n_vr1, VR_RANGE, vrp_val_min (expr_type), op1, NULL);
3197 /* Try with VR0 and [OP1, +INF]. */
3198 else if (is_gimple_min_invariant (minus_p ? vr0.min : vr0.max))
3199 set_value_range (&n_vr1, VR_RANGE, op1, vrp_val_max (expr_type), NULL);
3201 /* Try with VR0 and [OP1, OP1]. */
3202 else
3203 set_value_range (&n_vr1, VR_RANGE, op1, op1, NULL);
3205 extract_range_from_binary_expr_1 (vr, code, expr_type, &vr0, &n_vr1);
3208 if (vr->type == VR_VARYING
3209 && (code == PLUS_EXPR || code == MINUS_EXPR)
3210 && TREE_CODE (op0) == SSA_NAME
3211 && vr1.type == VR_RANGE
3212 && symbolic_range_based_on_p (&vr1, op0))
3214 const bool minus_p = (code == MINUS_EXPR);
3215 value_range n_vr0 = VR_INITIALIZER;
3217 /* Try with [-INF, OP0] and VR1. */
3218 if (is_gimple_min_invariant (minus_p ? vr1.max : vr1.min))
3219 set_value_range (&n_vr0, VR_RANGE, vrp_val_min (expr_type), op0, NULL);
3221 /* Try with [OP0, +INF] and VR1. */
3222 else if (is_gimple_min_invariant (minus_p ? vr1.min : vr1.max))
3223 set_value_range (&n_vr0, VR_RANGE, op0, vrp_val_max (expr_type), NULL);
3225 /* Try with [OP0, OP0] and VR1. */
3226 else
3227 set_value_range (&n_vr0, VR_RANGE, op0, op0, NULL);
3229 extract_range_from_binary_expr_1 (vr, code, expr_type, &n_vr0, &vr1);
3233 /* Extract range information from a unary operation CODE based on
3234 the range of its operand *VR0 with type OP0_TYPE with resulting type TYPE.
3235 The resulting range is stored in *VR. */
3237 static void
3238 extract_range_from_unary_expr_1 (value_range *vr,
3239 enum tree_code code, tree type,
3240 value_range *vr0_, tree op0_type)
3242 value_range vr0 = *vr0_, vrtem0 = VR_INITIALIZER, vrtem1 = VR_INITIALIZER;
3244 /* VRP only operates on integral and pointer types. */
3245 if (!(INTEGRAL_TYPE_P (op0_type)
3246 || POINTER_TYPE_P (op0_type))
3247 || !(INTEGRAL_TYPE_P (type)
3248 || POINTER_TYPE_P (type)))
3250 set_value_range_to_varying (vr);
3251 return;
3254 /* If VR0 is UNDEFINED, so is the result. */
3255 if (vr0.type == VR_UNDEFINED)
3257 set_value_range_to_undefined (vr);
3258 return;
3261 /* Handle operations that we express in terms of others. */
3262 if (code == PAREN_EXPR || code == OBJ_TYPE_REF)
3264 /* PAREN_EXPR and OBJ_TYPE_REF are simple copies. */
3265 copy_value_range (vr, &vr0);
3266 return;
3268 else if (code == NEGATE_EXPR)
3270 /* -X is simply 0 - X, so re-use existing code that also handles
3271 anti-ranges fine. */
3272 value_range zero = VR_INITIALIZER;
3273 set_value_range_to_value (&zero, build_int_cst (type, 0), NULL);
3274 extract_range_from_binary_expr_1 (vr, MINUS_EXPR, type, &zero, &vr0);
3275 return;
3277 else if (code == BIT_NOT_EXPR)
3279 /* ~X is simply -1 - X, so re-use existing code that also handles
3280 anti-ranges fine. */
3281 value_range minusone = VR_INITIALIZER;
3282 set_value_range_to_value (&minusone, build_int_cst (type, -1), NULL);
3283 extract_range_from_binary_expr_1 (vr, MINUS_EXPR,
3284 type, &minusone, &vr0);
3285 return;
3288 /* Now canonicalize anti-ranges to ranges when they are not symbolic
3289 and express op ~[] as (op []') U (op []''). */
3290 if (vr0.type == VR_ANTI_RANGE
3291 && ranges_from_anti_range (&vr0, &vrtem0, &vrtem1))
3293 extract_range_from_unary_expr_1 (vr, code, type, &vrtem0, op0_type);
3294 if (vrtem1.type != VR_UNDEFINED)
3296 value_range vrres = VR_INITIALIZER;
3297 extract_range_from_unary_expr_1 (&vrres, code, type,
3298 &vrtem1, op0_type);
3299 vrp_meet (vr, &vrres);
3301 return;
3304 if (CONVERT_EXPR_CODE_P (code))
3306 tree inner_type = op0_type;
3307 tree outer_type = type;
3309 /* If the expression evaluates to a pointer, we are only interested in
3310 determining if it evaluates to NULL [0, 0] or non-NULL (~[0, 0]). */
3311 if (POINTER_TYPE_P (type))
3313 if (range_is_nonnull (&vr0))
3314 set_value_range_to_nonnull (vr, type);
3315 else if (range_is_null (&vr0))
3316 set_value_range_to_null (vr, type);
3317 else
3318 set_value_range_to_varying (vr);
3319 return;
3322 /* If VR0 is varying and we increase the type precision, assume
3323 a full range for the following transformation. */
3324 if (vr0.type == VR_VARYING
3325 && INTEGRAL_TYPE_P (inner_type)
3326 && TYPE_PRECISION (inner_type) < TYPE_PRECISION (outer_type))
3328 vr0.type = VR_RANGE;
3329 vr0.min = TYPE_MIN_VALUE (inner_type);
3330 vr0.max = TYPE_MAX_VALUE (inner_type);
3333 /* If VR0 is a constant range or anti-range and the conversion is
3334 not truncating we can convert the min and max values and
3335 canonicalize the resulting range. Otherwise we can do the
3336 conversion if the size of the range is less than what the
3337 precision of the target type can represent and the range is
3338 not an anti-range. */
3339 if ((vr0.type == VR_RANGE
3340 || vr0.type == VR_ANTI_RANGE)
3341 && TREE_CODE (vr0.min) == INTEGER_CST
3342 && TREE_CODE (vr0.max) == INTEGER_CST
3343 && (!is_overflow_infinity (vr0.min)
3344 || (vr0.type == VR_RANGE
3345 && TYPE_PRECISION (outer_type) > TYPE_PRECISION (inner_type)
3346 && needs_overflow_infinity (outer_type)
3347 && supports_overflow_infinity (outer_type)))
3348 && (!is_overflow_infinity (vr0.max)
3349 || (vr0.type == VR_RANGE
3350 && TYPE_PRECISION (outer_type) > TYPE_PRECISION (inner_type)
3351 && needs_overflow_infinity (outer_type)
3352 && supports_overflow_infinity (outer_type)))
3353 && (TYPE_PRECISION (outer_type) >= TYPE_PRECISION (inner_type)
3354 || (vr0.type == VR_RANGE
3355 && integer_zerop (int_const_binop (RSHIFT_EXPR,
3356 int_const_binop (MINUS_EXPR, vr0.max, vr0.min),
3357 size_int (TYPE_PRECISION (outer_type)))))))
3359 tree new_min, new_max;
3360 if (is_overflow_infinity (vr0.min))
3361 new_min = negative_overflow_infinity (outer_type);
3362 else
3363 new_min = force_fit_type (outer_type, wi::to_widest (vr0.min),
3364 0, false);
3365 if (is_overflow_infinity (vr0.max))
3366 new_max = positive_overflow_infinity (outer_type);
3367 else
3368 new_max = force_fit_type (outer_type, wi::to_widest (vr0.max),
3369 0, false);
3370 set_and_canonicalize_value_range (vr, vr0.type,
3371 new_min, new_max, NULL);
3372 return;
3375 set_value_range_to_varying (vr);
3376 return;
3378 else if (code == ABS_EXPR)
3380 tree min, max;
3381 int cmp;
3383 /* Pass through vr0 in the easy cases. */
3384 if (TYPE_UNSIGNED (type)
3385 || value_range_nonnegative_p (&vr0))
3387 copy_value_range (vr, &vr0);
3388 return;
3391 /* For the remaining varying or symbolic ranges we can't do anything
3392 useful. */
3393 if (vr0.type == VR_VARYING
3394 || symbolic_range_p (&vr0))
3396 set_value_range_to_varying (vr);
3397 return;
3400 /* -TYPE_MIN_VALUE = TYPE_MIN_VALUE with flag_wrapv so we can't get a
3401 useful range. */
3402 if (!TYPE_OVERFLOW_UNDEFINED (type)
3403 && ((vr0.type == VR_RANGE
3404 && vrp_val_is_min (vr0.min))
3405 || (vr0.type == VR_ANTI_RANGE
3406 && !vrp_val_is_min (vr0.min))))
3408 set_value_range_to_varying (vr);
3409 return;
3412 /* ABS_EXPR may flip the range around, if the original range
3413 included negative values. */
3414 if (is_overflow_infinity (vr0.min))
3415 min = positive_overflow_infinity (type);
3416 else if (!vrp_val_is_min (vr0.min))
3417 min = fold_unary_to_constant (code, type, vr0.min);
3418 else if (!needs_overflow_infinity (type))
3419 min = TYPE_MAX_VALUE (type);
3420 else if (supports_overflow_infinity (type))
3421 min = positive_overflow_infinity (type);
3422 else
3424 set_value_range_to_varying (vr);
3425 return;
3428 if (is_overflow_infinity (vr0.max))
3429 max = positive_overflow_infinity (type);
3430 else if (!vrp_val_is_min (vr0.max))
3431 max = fold_unary_to_constant (code, type, vr0.max);
3432 else if (!needs_overflow_infinity (type))
3433 max = TYPE_MAX_VALUE (type);
3434 else if (supports_overflow_infinity (type)
3435 /* We shouldn't generate [+INF, +INF] as set_value_range
3436 doesn't like this and ICEs. */
3437 && !is_positive_overflow_infinity (min))
3438 max = positive_overflow_infinity (type);
3439 else
3441 set_value_range_to_varying (vr);
3442 return;
3445 cmp = compare_values (min, max);
3447 /* If a VR_ANTI_RANGEs contains zero, then we have
3448 ~[-INF, min(MIN, MAX)]. */
3449 if (vr0.type == VR_ANTI_RANGE)
3451 if (range_includes_zero_p (vr0.min, vr0.max) == 1)
3453 /* Take the lower of the two values. */
3454 if (cmp != 1)
3455 max = min;
3457 /* Create ~[-INF, min (abs(MIN), abs(MAX))]
3458 or ~[-INF + 1, min (abs(MIN), abs(MAX))] when
3459 flag_wrapv is set and the original anti-range doesn't include
3460 TYPE_MIN_VALUE, remember -TYPE_MIN_VALUE = TYPE_MIN_VALUE. */
3461 if (TYPE_OVERFLOW_WRAPS (type))
3463 tree type_min_value = TYPE_MIN_VALUE (type);
3465 min = (vr0.min != type_min_value
3466 ? int_const_binop (PLUS_EXPR, type_min_value,
3467 build_int_cst (TREE_TYPE (type_min_value), 1))
3468 : type_min_value);
3470 else
3472 if (overflow_infinity_range_p (&vr0))
3473 min = negative_overflow_infinity (type);
3474 else
3475 min = TYPE_MIN_VALUE (type);
3478 else
3480 /* All else has failed, so create the range [0, INF], even for
3481 flag_wrapv since TYPE_MIN_VALUE is in the original
3482 anti-range. */
3483 vr0.type = VR_RANGE;
3484 min = build_int_cst (type, 0);
3485 if (needs_overflow_infinity (type))
3487 if (supports_overflow_infinity (type))
3488 max = positive_overflow_infinity (type);
3489 else
3491 set_value_range_to_varying (vr);
3492 return;
3495 else
3496 max = TYPE_MAX_VALUE (type);
3500 /* If the range contains zero then we know that the minimum value in the
3501 range will be zero. */
3502 else if (range_includes_zero_p (vr0.min, vr0.max) == 1)
3504 if (cmp == 1)
3505 max = min;
3506 min = build_int_cst (type, 0);
3508 else
3510 /* If the range was reversed, swap MIN and MAX. */
3511 if (cmp == 1)
3512 std::swap (min, max);
3515 cmp = compare_values (min, max);
3516 if (cmp == -2 || cmp == 1)
3518 /* If the new range has its limits swapped around (MIN > MAX),
3519 then the operation caused one of them to wrap around, mark
3520 the new range VARYING. */
3521 set_value_range_to_varying (vr);
3523 else
3524 set_value_range (vr, vr0.type, min, max, NULL);
3525 return;
3528 /* For unhandled operations fall back to varying. */
3529 set_value_range_to_varying (vr);
3530 return;
3534 /* Extract range information from a unary expression CODE OP0 based on
3535 the range of its operand with resulting type TYPE.
3536 The resulting range is stored in *VR. */
3538 static void
3539 extract_range_from_unary_expr (value_range *vr, enum tree_code code,
3540 tree type, tree op0)
3542 value_range vr0 = VR_INITIALIZER;
3544 /* Get value ranges for the operand. For constant operands, create
3545 a new value range with the operand to simplify processing. */
3546 if (TREE_CODE (op0) == SSA_NAME)
3547 vr0 = *(get_value_range (op0));
3548 else if (is_gimple_min_invariant (op0))
3549 set_value_range_to_value (&vr0, op0, NULL);
3550 else
3551 set_value_range_to_varying (&vr0);
3553 extract_range_from_unary_expr_1 (vr, code, type, &vr0, TREE_TYPE (op0));
3557 /* Extract range information from a conditional expression STMT based on
3558 the ranges of each of its operands and the expression code. */
3560 static void
3561 extract_range_from_cond_expr (value_range *vr, gassign *stmt)
3563 tree op0, op1;
3564 value_range vr0 = VR_INITIALIZER;
3565 value_range vr1 = VR_INITIALIZER;
3567 /* Get value ranges for each operand. For constant operands, create
3568 a new value range with the operand to simplify processing. */
3569 op0 = gimple_assign_rhs2 (stmt);
3570 if (TREE_CODE (op0) == SSA_NAME)
3571 vr0 = *(get_value_range (op0));
3572 else if (is_gimple_min_invariant (op0))
3573 set_value_range_to_value (&vr0, op0, NULL);
3574 else
3575 set_value_range_to_varying (&vr0);
3577 op1 = gimple_assign_rhs3 (stmt);
3578 if (TREE_CODE (op1) == SSA_NAME)
3579 vr1 = *(get_value_range (op1));
3580 else if (is_gimple_min_invariant (op1))
3581 set_value_range_to_value (&vr1, op1, NULL);
3582 else
3583 set_value_range_to_varying (&vr1);
3585 /* The resulting value range is the union of the operand ranges */
3586 copy_value_range (vr, &vr0);
3587 vrp_meet (vr, &vr1);
3591 /* Extract range information from a comparison expression EXPR based
3592 on the range of its operand and the expression code. */
3594 static void
3595 extract_range_from_comparison (value_range *vr, enum tree_code code,
3596 tree type, tree op0, tree op1)
3598 bool sop = false;
3599 tree val;
3601 val = vrp_evaluate_conditional_warnv_with_ops (code, op0, op1, false, &sop,
3602 NULL);
3604 /* A disadvantage of using a special infinity as an overflow
3605 representation is that we lose the ability to record overflow
3606 when we don't have an infinity. So we have to ignore a result
3607 which relies on overflow. */
3609 if (val && !is_overflow_infinity (val) && !sop)
3611 /* Since this expression was found on the RHS of an assignment,
3612 its type may be different from _Bool. Convert VAL to EXPR's
3613 type. */
3614 val = fold_convert (type, val);
3615 if (is_gimple_min_invariant (val))
3616 set_value_range_to_value (vr, val, vr->equiv);
3617 else
3618 set_value_range (vr, VR_RANGE, val, val, vr->equiv);
3620 else
3621 /* The result of a comparison is always true or false. */
3622 set_value_range_to_truthvalue (vr, type);
3625 /* Helper function for simplify_internal_call_using_ranges and
3626 extract_range_basic. Return true if OP0 SUBCODE OP1 for
3627 SUBCODE {PLUS,MINUS,MULT}_EXPR is known to never overflow or
3628 always overflow. Set *OVF to true if it is known to always
3629 overflow. */
3631 static bool
3632 check_for_binary_op_overflow (enum tree_code subcode, tree type,
3633 tree op0, tree op1, bool *ovf)
3635 value_range vr0 = VR_INITIALIZER;
3636 value_range vr1 = VR_INITIALIZER;
3637 if (TREE_CODE (op0) == SSA_NAME)
3638 vr0 = *get_value_range (op0);
3639 else if (TREE_CODE (op0) == INTEGER_CST)
3640 set_value_range_to_value (&vr0, op0, NULL);
3641 else
3642 set_value_range_to_varying (&vr0);
3644 if (TREE_CODE (op1) == SSA_NAME)
3645 vr1 = *get_value_range (op1);
3646 else if (TREE_CODE (op1) == INTEGER_CST)
3647 set_value_range_to_value (&vr1, op1, NULL);
3648 else
3649 set_value_range_to_varying (&vr1);
3651 if (!range_int_cst_p (&vr0)
3652 || TREE_OVERFLOW (vr0.min)
3653 || TREE_OVERFLOW (vr0.max))
3655 vr0.min = vrp_val_min (TREE_TYPE (op0));
3656 vr0.max = vrp_val_max (TREE_TYPE (op0));
3658 if (!range_int_cst_p (&vr1)
3659 || TREE_OVERFLOW (vr1.min)
3660 || TREE_OVERFLOW (vr1.max))
3662 vr1.min = vrp_val_min (TREE_TYPE (op1));
3663 vr1.max = vrp_val_max (TREE_TYPE (op1));
3665 *ovf = arith_overflowed_p (subcode, type, vr0.min,
3666 subcode == MINUS_EXPR ? vr1.max : vr1.min);
3667 if (arith_overflowed_p (subcode, type, vr0.max,
3668 subcode == MINUS_EXPR ? vr1.min : vr1.max) != *ovf)
3669 return false;
3670 if (subcode == MULT_EXPR)
3672 if (arith_overflowed_p (subcode, type, vr0.min, vr1.max) != *ovf
3673 || arith_overflowed_p (subcode, type, vr0.max, vr1.min) != *ovf)
3674 return false;
3676 if (*ovf)
3678 /* So far we found that there is an overflow on the boundaries.
3679 That doesn't prove that there is an overflow even for all values
3680 in between the boundaries. For that compute widest_int range
3681 of the result and see if it doesn't overlap the range of
3682 type. */
3683 widest_int wmin, wmax;
3684 widest_int w[4];
3685 int i;
3686 w[0] = wi::to_widest (vr0.min);
3687 w[1] = wi::to_widest (vr0.max);
3688 w[2] = wi::to_widest (vr1.min);
3689 w[3] = wi::to_widest (vr1.max);
3690 for (i = 0; i < 4; i++)
3692 widest_int wt;
3693 switch (subcode)
3695 case PLUS_EXPR:
3696 wt = wi::add (w[i & 1], w[2 + (i & 2) / 2]);
3697 break;
3698 case MINUS_EXPR:
3699 wt = wi::sub (w[i & 1], w[2 + (i & 2) / 2]);
3700 break;
3701 case MULT_EXPR:
3702 wt = wi::mul (w[i & 1], w[2 + (i & 2) / 2]);
3703 break;
3704 default:
3705 gcc_unreachable ();
3707 if (i == 0)
3709 wmin = wt;
3710 wmax = wt;
3712 else
3714 wmin = wi::smin (wmin, wt);
3715 wmax = wi::smax (wmax, wt);
3718 /* The result of op0 CODE op1 is known to be in range
3719 [wmin, wmax]. */
3720 widest_int wtmin = wi::to_widest (vrp_val_min (type));
3721 widest_int wtmax = wi::to_widest (vrp_val_max (type));
3722 /* If all values in [wmin, wmax] are smaller than
3723 [wtmin, wtmax] or all are larger than [wtmin, wtmax],
3724 the arithmetic operation will always overflow. */
3725 if (wmax < wtmin || wmin > wtmax)
3726 return true;
3727 return false;
3729 return true;
3732 /* Try to derive a nonnegative or nonzero range out of STMT relying
3733 primarily on generic routines in fold in conjunction with range data.
3734 Store the result in *VR */
3736 static void
3737 extract_range_basic (value_range *vr, gimple *stmt)
3739 bool sop = false;
3740 tree type = gimple_expr_type (stmt);
3742 if (is_gimple_call (stmt))
3744 tree arg;
3745 int mini, maxi, zerov = 0, prec;
3746 enum tree_code subcode = ERROR_MARK;
3747 combined_fn cfn = gimple_call_combined_fn (stmt);
3749 switch (cfn)
3751 case CFN_BUILT_IN_CONSTANT_P:
3752 /* If the call is __builtin_constant_p and the argument is a
3753 function parameter resolve it to false. This avoids bogus
3754 array bound warnings.
3755 ??? We could do this as early as inlining is finished. */
3756 arg = gimple_call_arg (stmt, 0);
3757 if (TREE_CODE (arg) == SSA_NAME
3758 && SSA_NAME_IS_DEFAULT_DEF (arg)
3759 && TREE_CODE (SSA_NAME_VAR (arg)) == PARM_DECL)
3761 set_value_range_to_null (vr, type);
3762 return;
3764 break;
3765 /* Both __builtin_ffs* and __builtin_popcount return
3766 [0, prec]. */
3767 CASE_CFN_FFS:
3768 CASE_CFN_POPCOUNT:
3769 arg = gimple_call_arg (stmt, 0);
3770 prec = TYPE_PRECISION (TREE_TYPE (arg));
3771 mini = 0;
3772 maxi = prec;
3773 if (TREE_CODE (arg) == SSA_NAME)
3775 value_range *vr0 = get_value_range (arg);
3776 /* If arg is non-zero, then ffs or popcount
3777 are non-zero. */
3778 if (((vr0->type == VR_RANGE
3779 && range_includes_zero_p (vr0->min, vr0->max) == 0)
3780 || (vr0->type == VR_ANTI_RANGE
3781 && range_includes_zero_p (vr0->min, vr0->max) == 1))
3782 && !is_overflow_infinity (vr0->min)
3783 && !is_overflow_infinity (vr0->max))
3784 mini = 1;
3785 /* If some high bits are known to be zero,
3786 we can decrease the maximum. */
3787 if (vr0->type == VR_RANGE
3788 && TREE_CODE (vr0->max) == INTEGER_CST
3789 && !operand_less_p (vr0->min,
3790 build_zero_cst (TREE_TYPE (vr0->min)))
3791 && !is_overflow_infinity (vr0->max))
3792 maxi = tree_floor_log2 (vr0->max) + 1;
3794 goto bitop_builtin;
3795 /* __builtin_parity* returns [0, 1]. */
3796 CASE_CFN_PARITY:
3797 mini = 0;
3798 maxi = 1;
3799 goto bitop_builtin;
3800 /* __builtin_c[lt]z* return [0, prec-1], except for
3801 when the argument is 0, but that is undefined behavior.
3802 On many targets where the CLZ RTL or optab value is defined
3803 for 0 the value is prec, so include that in the range
3804 by default. */
3805 CASE_CFN_CLZ:
3806 arg = gimple_call_arg (stmt, 0);
3807 prec = TYPE_PRECISION (TREE_TYPE (arg));
3808 mini = 0;
3809 maxi = prec;
3810 if (optab_handler (clz_optab, TYPE_MODE (TREE_TYPE (arg)))
3811 != CODE_FOR_nothing
3812 && CLZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (TREE_TYPE (arg)),
3813 zerov)
3814 /* Handle only the single common value. */
3815 && zerov != prec)
3816 /* Magic value to give up, unless vr0 proves
3817 arg is non-zero. */
3818 mini = -2;
3819 if (TREE_CODE (arg) == SSA_NAME)
3821 value_range *vr0 = get_value_range (arg);
3822 /* From clz of VR_RANGE minimum we can compute
3823 result maximum. */
3824 if (vr0->type == VR_RANGE
3825 && TREE_CODE (vr0->min) == INTEGER_CST
3826 && !is_overflow_infinity (vr0->min))
3828 maxi = prec - 1 - tree_floor_log2 (vr0->min);
3829 if (maxi != prec)
3830 mini = 0;
3832 else if (vr0->type == VR_ANTI_RANGE
3833 && integer_zerop (vr0->min)
3834 && !is_overflow_infinity (vr0->min))
3836 maxi = prec - 1;
3837 mini = 0;
3839 if (mini == -2)
3840 break;
3841 /* From clz of VR_RANGE maximum we can compute
3842 result minimum. */
3843 if (vr0->type == VR_RANGE
3844 && TREE_CODE (vr0->max) == INTEGER_CST
3845 && !is_overflow_infinity (vr0->max))
3847 mini = prec - 1 - tree_floor_log2 (vr0->max);
3848 if (mini == prec)
3849 break;
3852 if (mini == -2)
3853 break;
3854 goto bitop_builtin;
3855 /* __builtin_ctz* return [0, prec-1], except for
3856 when the argument is 0, but that is undefined behavior.
3857 If there is a ctz optab for this mode and
3858 CTZ_DEFINED_VALUE_AT_ZERO, include that in the range,
3859 otherwise just assume 0 won't be seen. */
3860 CASE_CFN_CTZ:
3861 arg = gimple_call_arg (stmt, 0);
3862 prec = TYPE_PRECISION (TREE_TYPE (arg));
3863 mini = 0;
3864 maxi = prec - 1;
3865 if (optab_handler (ctz_optab, TYPE_MODE (TREE_TYPE (arg)))
3866 != CODE_FOR_nothing
3867 && CTZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (TREE_TYPE (arg)),
3868 zerov))
3870 /* Handle only the two common values. */
3871 if (zerov == -1)
3872 mini = -1;
3873 else if (zerov == prec)
3874 maxi = prec;
3875 else
3876 /* Magic value to give up, unless vr0 proves
3877 arg is non-zero. */
3878 mini = -2;
3880 if (TREE_CODE (arg) == SSA_NAME)
3882 value_range *vr0 = get_value_range (arg);
3883 /* If arg is non-zero, then use [0, prec - 1]. */
3884 if (((vr0->type == VR_RANGE
3885 && integer_nonzerop (vr0->min))
3886 || (vr0->type == VR_ANTI_RANGE
3887 && integer_zerop (vr0->min)))
3888 && !is_overflow_infinity (vr0->min))
3890 mini = 0;
3891 maxi = prec - 1;
3893 /* If some high bits are known to be zero,
3894 we can decrease the result maximum. */
3895 if (vr0->type == VR_RANGE
3896 && TREE_CODE (vr0->max) == INTEGER_CST
3897 && !is_overflow_infinity (vr0->max))
3899 maxi = tree_floor_log2 (vr0->max);
3900 /* For vr0 [0, 0] give up. */
3901 if (maxi == -1)
3902 break;
3905 if (mini == -2)
3906 break;
3907 goto bitop_builtin;
3908 /* __builtin_clrsb* returns [0, prec-1]. */
3909 CASE_CFN_CLRSB:
3910 arg = gimple_call_arg (stmt, 0);
3911 prec = TYPE_PRECISION (TREE_TYPE (arg));
3912 mini = 0;
3913 maxi = prec - 1;
3914 goto bitop_builtin;
3915 bitop_builtin:
3916 set_value_range (vr, VR_RANGE, build_int_cst (type, mini),
3917 build_int_cst (type, maxi), NULL);
3918 return;
3919 case CFN_UBSAN_CHECK_ADD:
3920 subcode = PLUS_EXPR;
3921 break;
3922 case CFN_UBSAN_CHECK_SUB:
3923 subcode = MINUS_EXPR;
3924 break;
3925 case CFN_UBSAN_CHECK_MUL:
3926 subcode = MULT_EXPR;
3927 break;
3928 case CFN_GOACC_DIM_SIZE:
3929 case CFN_GOACC_DIM_POS:
3930 /* Optimizing these two internal functions helps the loop
3931 optimizer eliminate outer comparisons. Size is [1,N]
3932 and pos is [0,N-1]. */
3934 bool is_pos = cfn == CFN_GOACC_DIM_POS;
3935 int axis = get_oacc_ifn_dim_arg (stmt);
3936 int size = get_oacc_fn_dim_size (current_function_decl, axis);
3938 if (!size)
3939 /* If it's dynamic, the backend might know a hardware
3940 limitation. */
3941 size = targetm.goacc.dim_limit (axis);
3943 tree type = TREE_TYPE (gimple_call_lhs (stmt));
3944 set_value_range (vr, VR_RANGE,
3945 build_int_cst (type, is_pos ? 0 : 1),
3946 size ? build_int_cst (type, size - is_pos)
3947 : vrp_val_max (type), NULL);
3949 return;
3950 default:
3951 break;
3953 if (subcode != ERROR_MARK)
3955 bool saved_flag_wrapv = flag_wrapv;
3956 /* Pretend the arithmetics is wrapping. If there is
3957 any overflow, we'll complain, but will actually do
3958 wrapping operation. */
3959 flag_wrapv = 1;
3960 extract_range_from_binary_expr (vr, subcode, type,
3961 gimple_call_arg (stmt, 0),
3962 gimple_call_arg (stmt, 1));
3963 flag_wrapv = saved_flag_wrapv;
3965 /* If for both arguments vrp_valueize returned non-NULL,
3966 this should have been already folded and if not, it
3967 wasn't folded because of overflow. Avoid removing the
3968 UBSAN_CHECK_* calls in that case. */
3969 if (vr->type == VR_RANGE
3970 && (vr->min == vr->max
3971 || operand_equal_p (vr->min, vr->max, 0)))
3972 set_value_range_to_varying (vr);
3973 return;
3976 /* Handle extraction of the two results (result of arithmetics and
3977 a flag whether arithmetics overflowed) from {ADD,SUB,MUL}_OVERFLOW
3978 internal function. */
3979 else if (is_gimple_assign (stmt)
3980 && (gimple_assign_rhs_code (stmt) == REALPART_EXPR
3981 || gimple_assign_rhs_code (stmt) == IMAGPART_EXPR)
3982 && INTEGRAL_TYPE_P (type))
3984 enum tree_code code = gimple_assign_rhs_code (stmt);
3985 tree op = gimple_assign_rhs1 (stmt);
3986 if (TREE_CODE (op) == code && TREE_CODE (TREE_OPERAND (op, 0)) == SSA_NAME)
3988 gimple *g = SSA_NAME_DEF_STMT (TREE_OPERAND (op, 0));
3989 if (is_gimple_call (g) && gimple_call_internal_p (g))
3991 enum tree_code subcode = ERROR_MARK;
3992 switch (gimple_call_internal_fn (g))
3994 case IFN_ADD_OVERFLOW:
3995 subcode = PLUS_EXPR;
3996 break;
3997 case IFN_SUB_OVERFLOW:
3998 subcode = MINUS_EXPR;
3999 break;
4000 case IFN_MUL_OVERFLOW:
4001 subcode = MULT_EXPR;
4002 break;
4003 default:
4004 break;
4006 if (subcode != ERROR_MARK)
4008 tree op0 = gimple_call_arg (g, 0);
4009 tree op1 = gimple_call_arg (g, 1);
4010 if (code == IMAGPART_EXPR)
4012 bool ovf = false;
4013 if (check_for_binary_op_overflow (subcode, type,
4014 op0, op1, &ovf))
4015 set_value_range_to_value (vr,
4016 build_int_cst (type, ovf),
4017 NULL);
4018 else
4019 set_value_range (vr, VR_RANGE, build_int_cst (type, 0),
4020 build_int_cst (type, 1), NULL);
4022 else if (types_compatible_p (type, TREE_TYPE (op0))
4023 && types_compatible_p (type, TREE_TYPE (op1)))
4025 bool saved_flag_wrapv = flag_wrapv;
4026 /* Pretend the arithmetics is wrapping. If there is
4027 any overflow, IMAGPART_EXPR will be set. */
4028 flag_wrapv = 1;
4029 extract_range_from_binary_expr (vr, subcode, type,
4030 op0, op1);
4031 flag_wrapv = saved_flag_wrapv;
4033 else
4035 value_range vr0 = VR_INITIALIZER;
4036 value_range vr1 = VR_INITIALIZER;
4037 bool saved_flag_wrapv = flag_wrapv;
4038 /* Pretend the arithmetics is wrapping. If there is
4039 any overflow, IMAGPART_EXPR will be set. */
4040 flag_wrapv = 1;
4041 extract_range_from_unary_expr (&vr0, NOP_EXPR,
4042 type, op0);
4043 extract_range_from_unary_expr (&vr1, NOP_EXPR,
4044 type, op1);
4045 extract_range_from_binary_expr_1 (vr, subcode, type,
4046 &vr0, &vr1);
4047 flag_wrapv = saved_flag_wrapv;
4049 return;
4054 if (INTEGRAL_TYPE_P (type)
4055 && gimple_stmt_nonnegative_warnv_p (stmt, &sop))
4056 set_value_range_to_nonnegative (vr, type,
4057 sop || stmt_overflow_infinity (stmt));
4058 else if (vrp_stmt_computes_nonzero (stmt, &sop)
4059 && !sop)
4060 set_value_range_to_nonnull (vr, type);
4061 else
4062 set_value_range_to_varying (vr);
4066 /* Try to compute a useful range out of assignment STMT and store it
4067 in *VR. */
4069 static void
4070 extract_range_from_assignment (value_range *vr, gassign *stmt)
4072 enum tree_code code = gimple_assign_rhs_code (stmt);
4074 if (code == ASSERT_EXPR)
4075 extract_range_from_assert (vr, gimple_assign_rhs1 (stmt));
4076 else if (code == SSA_NAME)
4077 extract_range_from_ssa_name (vr, gimple_assign_rhs1 (stmt));
4078 else if (TREE_CODE_CLASS (code) == tcc_binary)
4079 extract_range_from_binary_expr (vr, gimple_assign_rhs_code (stmt),
4080 gimple_expr_type (stmt),
4081 gimple_assign_rhs1 (stmt),
4082 gimple_assign_rhs2 (stmt));
4083 else if (TREE_CODE_CLASS (code) == tcc_unary)
4084 extract_range_from_unary_expr (vr, gimple_assign_rhs_code (stmt),
4085 gimple_expr_type (stmt),
4086 gimple_assign_rhs1 (stmt));
4087 else if (code == COND_EXPR)
4088 extract_range_from_cond_expr (vr, stmt);
4089 else if (TREE_CODE_CLASS (code) == tcc_comparison)
4090 extract_range_from_comparison (vr, gimple_assign_rhs_code (stmt),
4091 gimple_expr_type (stmt),
4092 gimple_assign_rhs1 (stmt),
4093 gimple_assign_rhs2 (stmt));
4094 else if (get_gimple_rhs_class (code) == GIMPLE_SINGLE_RHS
4095 && is_gimple_min_invariant (gimple_assign_rhs1 (stmt)))
4096 set_value_range_to_value (vr, gimple_assign_rhs1 (stmt), NULL);
4097 else
4098 set_value_range_to_varying (vr);
4100 if (vr->type == VR_VARYING)
4101 extract_range_basic (vr, stmt);
4104 /* Given a range VR, a LOOP and a variable VAR, determine whether it
4105 would be profitable to adjust VR using scalar evolution information
4106 for VAR. If so, update VR with the new limits. */
4108 static void
4109 adjust_range_with_scev (value_range *vr, struct loop *loop,
4110 gimple *stmt, tree var)
4112 tree init, step, chrec, tmin, tmax, min, max, type, tem;
4113 enum ev_direction dir;
4115 /* TODO. Don't adjust anti-ranges. An anti-range may provide
4116 better opportunities than a regular range, but I'm not sure. */
4117 if (vr->type == VR_ANTI_RANGE)
4118 return;
4120 chrec = instantiate_parameters (loop, analyze_scalar_evolution (loop, var));
4122 /* Like in PR19590, scev can return a constant function. */
4123 if (is_gimple_min_invariant (chrec))
4125 set_value_range_to_value (vr, chrec, vr->equiv);
4126 return;
4129 if (TREE_CODE (chrec) != POLYNOMIAL_CHREC)
4130 return;
4132 init = initial_condition_in_loop_num (chrec, loop->num);
4133 tem = op_with_constant_singleton_value_range (init);
4134 if (tem)
4135 init = tem;
4136 step = evolution_part_in_loop_num (chrec, loop->num);
4137 tem = op_with_constant_singleton_value_range (step);
4138 if (tem)
4139 step = tem;
4141 /* If STEP is symbolic, we can't know whether INIT will be the
4142 minimum or maximum value in the range. Also, unless INIT is
4143 a simple expression, compare_values and possibly other functions
4144 in tree-vrp won't be able to handle it. */
4145 if (step == NULL_TREE
4146 || !is_gimple_min_invariant (step)
4147 || !valid_value_p (init))
4148 return;
4150 dir = scev_direction (chrec);
4151 if (/* Do not adjust ranges if we do not know whether the iv increases
4152 or decreases, ... */
4153 dir == EV_DIR_UNKNOWN
4154 /* ... or if it may wrap. */
4155 || scev_probably_wraps_p (init, step, stmt, get_chrec_loop (chrec),
4156 true))
4157 return;
4159 /* We use TYPE_MIN_VALUE and TYPE_MAX_VALUE here instead of
4160 negative_overflow_infinity and positive_overflow_infinity,
4161 because we have concluded that the loop probably does not
4162 wrap. */
4164 type = TREE_TYPE (var);
4165 if (POINTER_TYPE_P (type) || !TYPE_MIN_VALUE (type))
4166 tmin = lower_bound_in_type (type, type);
4167 else
4168 tmin = TYPE_MIN_VALUE (type);
4169 if (POINTER_TYPE_P (type) || !TYPE_MAX_VALUE (type))
4170 tmax = upper_bound_in_type (type, type);
4171 else
4172 tmax = TYPE_MAX_VALUE (type);
4174 /* Try to use estimated number of iterations for the loop to constrain the
4175 final value in the evolution. */
4176 if (TREE_CODE (step) == INTEGER_CST
4177 && is_gimple_val (init)
4178 && (TREE_CODE (init) != SSA_NAME
4179 || get_value_range (init)->type == VR_RANGE))
4181 widest_int nit;
4183 /* We are only entering here for loop header PHI nodes, so using
4184 the number of latch executions is the correct thing to use. */
4185 if (max_loop_iterations (loop, &nit))
4187 value_range maxvr = VR_INITIALIZER;
4188 signop sgn = TYPE_SIGN (TREE_TYPE (step));
4189 bool overflow;
4191 widest_int wtmp = wi::mul (wi::to_widest (step), nit, sgn,
4192 &overflow);
4193 /* If the multiplication overflowed we can't do a meaningful
4194 adjustment. Likewise if the result doesn't fit in the type
4195 of the induction variable. For a signed type we have to
4196 check whether the result has the expected signedness which
4197 is that of the step as number of iterations is unsigned. */
4198 if (!overflow
4199 && wi::fits_to_tree_p (wtmp, TREE_TYPE (init))
4200 && (sgn == UNSIGNED
4201 || wi::gts_p (wtmp, 0) == wi::gts_p (step, 0)))
4203 tem = wide_int_to_tree (TREE_TYPE (init), wtmp);
4204 extract_range_from_binary_expr (&maxvr, PLUS_EXPR,
4205 TREE_TYPE (init), init, tem);
4206 /* Likewise if the addition did. */
4207 if (maxvr.type == VR_RANGE)
4209 value_range initvr = VR_INITIALIZER;
4211 if (TREE_CODE (init) == SSA_NAME)
4212 initvr = *(get_value_range (init));
4213 else if (is_gimple_min_invariant (init))
4214 set_value_range_to_value (&initvr, init, NULL);
4215 else
4216 return;
4218 /* Check if init + nit * step overflows. Though we checked
4219 scev {init, step}_loop doesn't wrap, it is not enough
4220 because the loop may exit immediately. Overflow could
4221 happen in the plus expression in this case. */
4222 if ((dir == EV_DIR_DECREASES
4223 && (is_negative_overflow_infinity (maxvr.min)
4224 || compare_values (maxvr.min, initvr.min) != -1))
4225 || (dir == EV_DIR_GROWS
4226 && (is_positive_overflow_infinity (maxvr.max)
4227 || compare_values (maxvr.max, initvr.max) != 1)))
4228 return;
4230 tmin = maxvr.min;
4231 tmax = maxvr.max;
4237 if (vr->type == VR_VARYING || vr->type == VR_UNDEFINED)
4239 min = tmin;
4240 max = tmax;
4242 /* For VARYING or UNDEFINED ranges, just about anything we get
4243 from scalar evolutions should be better. */
4245 if (dir == EV_DIR_DECREASES)
4246 max = init;
4247 else
4248 min = init;
4250 else if (vr->type == VR_RANGE)
4252 min = vr->min;
4253 max = vr->max;
4255 if (dir == EV_DIR_DECREASES)
4257 /* INIT is the maximum value. If INIT is lower than VR->MAX
4258 but no smaller than VR->MIN, set VR->MAX to INIT. */
4259 if (compare_values (init, max) == -1)
4260 max = init;
4262 /* According to the loop information, the variable does not
4263 overflow. If we think it does, probably because of an
4264 overflow due to arithmetic on a different INF value,
4265 reset now. */
4266 if (is_negative_overflow_infinity (min)
4267 || compare_values (min, tmin) == -1)
4268 min = tmin;
4271 else
4273 /* If INIT is bigger than VR->MIN, set VR->MIN to INIT. */
4274 if (compare_values (init, min) == 1)
4275 min = init;
4277 if (is_positive_overflow_infinity (max)
4278 || compare_values (tmax, max) == -1)
4279 max = tmax;
4282 else
4283 return;
4285 /* If we just created an invalid range with the minimum
4286 greater than the maximum, we fail conservatively.
4287 This should happen only in unreachable
4288 parts of code, or for invalid programs. */
4289 if (compare_values (min, max) == 1
4290 || (is_negative_overflow_infinity (min)
4291 && is_positive_overflow_infinity (max)))
4292 return;
4294 /* Even for valid range info, sometimes overflow flag will leak in.
4295 As GIMPLE IL should have no constants with TREE_OVERFLOW set, we
4296 drop them except for +-overflow_infinity which still need special
4297 handling in vrp pass. */
4298 if (TREE_OVERFLOW_P (min)
4299 && ! is_negative_overflow_infinity (min))
4300 min = drop_tree_overflow (min);
4301 if (TREE_OVERFLOW_P (max)
4302 && ! is_positive_overflow_infinity (max))
4303 max = drop_tree_overflow (max);
4305 set_value_range (vr, VR_RANGE, min, max, vr->equiv);
4309 /* Given two numeric value ranges VR0, VR1 and a comparison code COMP:
4311 - Return BOOLEAN_TRUE_NODE if VR0 COMP VR1 always returns true for
4312 all the values in the ranges.
4314 - Return BOOLEAN_FALSE_NODE if the comparison always returns false.
4316 - Return NULL_TREE if it is not always possible to determine the
4317 value of the comparison.
4319 Also set *STRICT_OVERFLOW_P to indicate whether a range with an
4320 overflow infinity was used in the test. */
4323 static tree
4324 compare_ranges (enum tree_code comp, value_range *vr0, value_range *vr1,
4325 bool *strict_overflow_p)
4327 /* VARYING or UNDEFINED ranges cannot be compared. */
4328 if (vr0->type == VR_VARYING
4329 || vr0->type == VR_UNDEFINED
4330 || vr1->type == VR_VARYING
4331 || vr1->type == VR_UNDEFINED)
4332 return NULL_TREE;
4334 /* Anti-ranges need to be handled separately. */
4335 if (vr0->type == VR_ANTI_RANGE || vr1->type == VR_ANTI_RANGE)
4337 /* If both are anti-ranges, then we cannot compute any
4338 comparison. */
4339 if (vr0->type == VR_ANTI_RANGE && vr1->type == VR_ANTI_RANGE)
4340 return NULL_TREE;
4342 /* These comparisons are never statically computable. */
4343 if (comp == GT_EXPR
4344 || comp == GE_EXPR
4345 || comp == LT_EXPR
4346 || comp == LE_EXPR)
4347 return NULL_TREE;
4349 /* Equality can be computed only between a range and an
4350 anti-range. ~[VAL1, VAL2] == [VAL1, VAL2] is always false. */
4351 if (vr0->type == VR_RANGE)
4353 /* To simplify processing, make VR0 the anti-range. */
4354 value_range *tmp = vr0;
4355 vr0 = vr1;
4356 vr1 = tmp;
4359 gcc_assert (comp == NE_EXPR || comp == EQ_EXPR);
4361 if (compare_values_warnv (vr0->min, vr1->min, strict_overflow_p) == 0
4362 && compare_values_warnv (vr0->max, vr1->max, strict_overflow_p) == 0)
4363 return (comp == NE_EXPR) ? boolean_true_node : boolean_false_node;
4365 return NULL_TREE;
4368 if (!usable_range_p (vr0, strict_overflow_p)
4369 || !usable_range_p (vr1, strict_overflow_p))
4370 return NULL_TREE;
4372 /* Simplify processing. If COMP is GT_EXPR or GE_EXPR, switch the
4373 operands around and change the comparison code. */
4374 if (comp == GT_EXPR || comp == GE_EXPR)
4376 comp = (comp == GT_EXPR) ? LT_EXPR : LE_EXPR;
4377 std::swap (vr0, vr1);
4380 if (comp == EQ_EXPR)
4382 /* Equality may only be computed if both ranges represent
4383 exactly one value. */
4384 if (compare_values_warnv (vr0->min, vr0->max, strict_overflow_p) == 0
4385 && compare_values_warnv (vr1->min, vr1->max, strict_overflow_p) == 0)
4387 int cmp_min = compare_values_warnv (vr0->min, vr1->min,
4388 strict_overflow_p);
4389 int cmp_max = compare_values_warnv (vr0->max, vr1->max,
4390 strict_overflow_p);
4391 if (cmp_min == 0 && cmp_max == 0)
4392 return boolean_true_node;
4393 else if (cmp_min != -2 && cmp_max != -2)
4394 return boolean_false_node;
4396 /* If [V0_MIN, V1_MAX] < [V1_MIN, V1_MAX] then V0 != V1. */
4397 else if (compare_values_warnv (vr0->min, vr1->max,
4398 strict_overflow_p) == 1
4399 || compare_values_warnv (vr1->min, vr0->max,
4400 strict_overflow_p) == 1)
4401 return boolean_false_node;
4403 return NULL_TREE;
4405 else if (comp == NE_EXPR)
4407 int cmp1, cmp2;
4409 /* If VR0 is completely to the left or completely to the right
4410 of VR1, they are always different. Notice that we need to
4411 make sure that both comparisons yield similar results to
4412 avoid comparing values that cannot be compared at
4413 compile-time. */
4414 cmp1 = compare_values_warnv (vr0->max, vr1->min, strict_overflow_p);
4415 cmp2 = compare_values_warnv (vr0->min, vr1->max, strict_overflow_p);
4416 if ((cmp1 == -1 && cmp2 == -1) || (cmp1 == 1 && cmp2 == 1))
4417 return boolean_true_node;
4419 /* If VR0 and VR1 represent a single value and are identical,
4420 return false. */
4421 else if (compare_values_warnv (vr0->min, vr0->max,
4422 strict_overflow_p) == 0
4423 && compare_values_warnv (vr1->min, vr1->max,
4424 strict_overflow_p) == 0
4425 && compare_values_warnv (vr0->min, vr1->min,
4426 strict_overflow_p) == 0
4427 && compare_values_warnv (vr0->max, vr1->max,
4428 strict_overflow_p) == 0)
4429 return boolean_false_node;
4431 /* Otherwise, they may or may not be different. */
4432 else
4433 return NULL_TREE;
4435 else if (comp == LT_EXPR || comp == LE_EXPR)
4437 int tst;
4439 /* If VR0 is to the left of VR1, return true. */
4440 tst = compare_values_warnv (vr0->max, vr1->min, strict_overflow_p);
4441 if ((comp == LT_EXPR && tst == -1)
4442 || (comp == LE_EXPR && (tst == -1 || tst == 0)))
4444 if (overflow_infinity_range_p (vr0)
4445 || overflow_infinity_range_p (vr1))
4446 *strict_overflow_p = true;
4447 return boolean_true_node;
4450 /* If VR0 is to the right of VR1, return false. */
4451 tst = compare_values_warnv (vr0->min, vr1->max, strict_overflow_p);
4452 if ((comp == LT_EXPR && (tst == 0 || tst == 1))
4453 || (comp == LE_EXPR && tst == 1))
4455 if (overflow_infinity_range_p (vr0)
4456 || overflow_infinity_range_p (vr1))
4457 *strict_overflow_p = true;
4458 return boolean_false_node;
4461 /* Otherwise, we don't know. */
4462 return NULL_TREE;
4465 gcc_unreachable ();
4469 /* Given a value range VR, a value VAL and a comparison code COMP, return
4470 BOOLEAN_TRUE_NODE if VR COMP VAL always returns true for all the
4471 values in VR. Return BOOLEAN_FALSE_NODE if the comparison
4472 always returns false. Return NULL_TREE if it is not always
4473 possible to determine the value of the comparison. Also set
4474 *STRICT_OVERFLOW_P to indicate whether a range with an overflow
4475 infinity was used in the test. */
4477 static tree
4478 compare_range_with_value (enum tree_code comp, value_range *vr, tree val,
4479 bool *strict_overflow_p)
4481 if (vr->type == VR_VARYING || vr->type == VR_UNDEFINED)
4482 return NULL_TREE;
4484 /* Anti-ranges need to be handled separately. */
4485 if (vr->type == VR_ANTI_RANGE)
4487 /* For anti-ranges, the only predicates that we can compute at
4488 compile time are equality and inequality. */
4489 if (comp == GT_EXPR
4490 || comp == GE_EXPR
4491 || comp == LT_EXPR
4492 || comp == LE_EXPR)
4493 return NULL_TREE;
4495 /* ~[VAL_1, VAL_2] OP VAL is known if VAL_1 <= VAL <= VAL_2. */
4496 if (value_inside_range (val, vr->min, vr->max) == 1)
4497 return (comp == NE_EXPR) ? boolean_true_node : boolean_false_node;
4499 return NULL_TREE;
4502 if (!usable_range_p (vr, strict_overflow_p))
4503 return NULL_TREE;
4505 if (comp == EQ_EXPR)
4507 /* EQ_EXPR may only be computed if VR represents exactly
4508 one value. */
4509 if (compare_values_warnv (vr->min, vr->max, strict_overflow_p) == 0)
4511 int cmp = compare_values_warnv (vr->min, val, strict_overflow_p);
4512 if (cmp == 0)
4513 return boolean_true_node;
4514 else if (cmp == -1 || cmp == 1 || cmp == 2)
4515 return boolean_false_node;
4517 else if (compare_values_warnv (val, vr->min, strict_overflow_p) == -1
4518 || compare_values_warnv (vr->max, val, strict_overflow_p) == -1)
4519 return boolean_false_node;
4521 return NULL_TREE;
4523 else if (comp == NE_EXPR)
4525 /* If VAL is not inside VR, then they are always different. */
4526 if (compare_values_warnv (vr->max, val, strict_overflow_p) == -1
4527 || compare_values_warnv (vr->min, val, strict_overflow_p) == 1)
4528 return boolean_true_node;
4530 /* If VR represents exactly one value equal to VAL, then return
4531 false. */
4532 if (compare_values_warnv (vr->min, vr->max, strict_overflow_p) == 0
4533 && compare_values_warnv (vr->min, val, strict_overflow_p) == 0)
4534 return boolean_false_node;
4536 /* Otherwise, they may or may not be different. */
4537 return NULL_TREE;
4539 else if (comp == LT_EXPR || comp == LE_EXPR)
4541 int tst;
4543 /* If VR is to the left of VAL, return true. */
4544 tst = compare_values_warnv (vr->max, val, strict_overflow_p);
4545 if ((comp == LT_EXPR && tst == -1)
4546 || (comp == LE_EXPR && (tst == -1 || tst == 0)))
4548 if (overflow_infinity_range_p (vr))
4549 *strict_overflow_p = true;
4550 return boolean_true_node;
4553 /* If VR is to the right of VAL, return false. */
4554 tst = compare_values_warnv (vr->min, val, strict_overflow_p);
4555 if ((comp == LT_EXPR && (tst == 0 || tst == 1))
4556 || (comp == LE_EXPR && tst == 1))
4558 if (overflow_infinity_range_p (vr))
4559 *strict_overflow_p = true;
4560 return boolean_false_node;
4563 /* Otherwise, we don't know. */
4564 return NULL_TREE;
4566 else if (comp == GT_EXPR || comp == GE_EXPR)
4568 int tst;
4570 /* If VR is to the right of VAL, return true. */
4571 tst = compare_values_warnv (vr->min, val, strict_overflow_p);
4572 if ((comp == GT_EXPR && tst == 1)
4573 || (comp == GE_EXPR && (tst == 0 || tst == 1)))
4575 if (overflow_infinity_range_p (vr))
4576 *strict_overflow_p = true;
4577 return boolean_true_node;
4580 /* If VR is to the left of VAL, return false. */
4581 tst = compare_values_warnv (vr->max, val, strict_overflow_p);
4582 if ((comp == GT_EXPR && (tst == -1 || tst == 0))
4583 || (comp == GE_EXPR && tst == -1))
4585 if (overflow_infinity_range_p (vr))
4586 *strict_overflow_p = true;
4587 return boolean_false_node;
4590 /* Otherwise, we don't know. */
4591 return NULL_TREE;
4594 gcc_unreachable ();
4598 /* Debugging dumps. */
4600 void dump_value_range (FILE *, value_range *);
4601 void debug_value_range (value_range *);
4602 void dump_all_value_ranges (FILE *);
4603 void debug_all_value_ranges (void);
4604 void dump_vr_equiv (FILE *, bitmap);
4605 void debug_vr_equiv (bitmap);
4608 /* Dump value range VR to FILE. */
4610 void
4611 dump_value_range (FILE *file, value_range *vr)
4613 if (vr == NULL)
4614 fprintf (file, "[]");
4615 else if (vr->type == VR_UNDEFINED)
4616 fprintf (file, "UNDEFINED");
4617 else if (vr->type == VR_RANGE || vr->type == VR_ANTI_RANGE)
4619 tree type = TREE_TYPE (vr->min);
4621 fprintf (file, "%s[", (vr->type == VR_ANTI_RANGE) ? "~" : "");
4623 if (is_negative_overflow_infinity (vr->min))
4624 fprintf (file, "-INF(OVF)");
4625 else if (INTEGRAL_TYPE_P (type)
4626 && !TYPE_UNSIGNED (type)
4627 && vrp_val_is_min (vr->min))
4628 fprintf (file, "-INF");
4629 else
4630 print_generic_expr (file, vr->min, 0);
4632 fprintf (file, ", ");
4634 if (is_positive_overflow_infinity (vr->max))
4635 fprintf (file, "+INF(OVF)");
4636 else if (INTEGRAL_TYPE_P (type)
4637 && vrp_val_is_max (vr->max))
4638 fprintf (file, "+INF");
4639 else
4640 print_generic_expr (file, vr->max, 0);
4642 fprintf (file, "]");
4644 if (vr->equiv)
4646 bitmap_iterator bi;
4647 unsigned i, c = 0;
4649 fprintf (file, " EQUIVALENCES: { ");
4651 EXECUTE_IF_SET_IN_BITMAP (vr->equiv, 0, i, bi)
4653 print_generic_expr (file, ssa_name (i), 0);
4654 fprintf (file, " ");
4655 c++;
4658 fprintf (file, "} (%u elements)", c);
4661 else if (vr->type == VR_VARYING)
4662 fprintf (file, "VARYING");
4663 else
4664 fprintf (file, "INVALID RANGE");
4668 /* Dump value range VR to stderr. */
4670 DEBUG_FUNCTION void
4671 debug_value_range (value_range *vr)
4673 dump_value_range (stderr, vr);
4674 fprintf (stderr, "\n");
4678 /* Dump value ranges of all SSA_NAMEs to FILE. */
4680 void
4681 dump_all_value_ranges (FILE *file)
4683 size_t i;
4685 for (i = 0; i < num_vr_values; i++)
4687 if (vr_value[i])
4689 print_generic_expr (file, ssa_name (i), 0);
4690 fprintf (file, ": ");
4691 dump_value_range (file, vr_value[i]);
4692 fprintf (file, "\n");
4696 fprintf (file, "\n");
4700 /* Dump all value ranges to stderr. */
4702 DEBUG_FUNCTION void
4703 debug_all_value_ranges (void)
4705 dump_all_value_ranges (stderr);
4709 /* Given a COND_EXPR COND of the form 'V OP W', and an SSA name V,
4710 create a new SSA name N and return the assertion assignment
4711 'N = ASSERT_EXPR <V, V OP W>'. */
4713 static gimple *
4714 build_assert_expr_for (tree cond, tree v)
4716 tree a;
4717 gassign *assertion;
4719 gcc_assert (TREE_CODE (v) == SSA_NAME
4720 && COMPARISON_CLASS_P (cond));
4722 a = build2 (ASSERT_EXPR, TREE_TYPE (v), v, cond);
4723 assertion = gimple_build_assign (NULL_TREE, a);
4725 /* The new ASSERT_EXPR, creates a new SSA name that replaces the
4726 operand of the ASSERT_EXPR. Create it so the new name and the old one
4727 are registered in the replacement table so that we can fix the SSA web
4728 after adding all the ASSERT_EXPRs. */
4729 create_new_def_for (v, assertion, NULL);
4731 return assertion;
4735 /* Return false if EXPR is a predicate expression involving floating
4736 point values. */
4738 static inline bool
4739 fp_predicate (gimple *stmt)
4741 GIMPLE_CHECK (stmt, GIMPLE_COND);
4743 return FLOAT_TYPE_P (TREE_TYPE (gimple_cond_lhs (stmt)));
4746 /* If the range of values taken by OP can be inferred after STMT executes,
4747 return the comparison code (COMP_CODE_P) and value (VAL_P) that
4748 describes the inferred range. Return true if a range could be
4749 inferred. */
4751 static bool
4752 infer_value_range (gimple *stmt, tree op, tree_code *comp_code_p, tree *val_p)
4754 *val_p = NULL_TREE;
4755 *comp_code_p = ERROR_MARK;
4757 /* Do not attempt to infer anything in names that flow through
4758 abnormal edges. */
4759 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op))
4760 return false;
4762 /* Similarly, don't infer anything from statements that may throw
4763 exceptions. ??? Relax this requirement? */
4764 if (stmt_could_throw_p (stmt))
4765 return false;
4767 /* If STMT is the last statement of a basic block with no normal
4768 successors, there is no point inferring anything about any of its
4769 operands. We would not be able to find a proper insertion point
4770 for the assertion, anyway. */
4771 if (stmt_ends_bb_p (stmt))
4773 edge_iterator ei;
4774 edge e;
4776 FOR_EACH_EDGE (e, ei, gimple_bb (stmt)->succs)
4777 if (!(e->flags & EDGE_ABNORMAL))
4778 break;
4779 if (e == NULL)
4780 return false;
4783 if (infer_nonnull_range (stmt, op))
4785 *val_p = build_int_cst (TREE_TYPE (op), 0);
4786 *comp_code_p = NE_EXPR;
4787 return true;
4790 return false;
4794 void dump_asserts_for (FILE *, tree);
4795 void debug_asserts_for (tree);
4796 void dump_all_asserts (FILE *);
4797 void debug_all_asserts (void);
4799 /* Dump all the registered assertions for NAME to FILE. */
4801 void
4802 dump_asserts_for (FILE *file, tree name)
4804 assert_locus *loc;
4806 fprintf (file, "Assertions to be inserted for ");
4807 print_generic_expr (file, name, 0);
4808 fprintf (file, "\n");
4810 loc = asserts_for[SSA_NAME_VERSION (name)];
4811 while (loc)
4813 fprintf (file, "\t");
4814 print_gimple_stmt (file, gsi_stmt (loc->si), 0, 0);
4815 fprintf (file, "\n\tBB #%d", loc->bb->index);
4816 if (loc->e)
4818 fprintf (file, "\n\tEDGE %d->%d", loc->e->src->index,
4819 loc->e->dest->index);
4820 dump_edge_info (file, loc->e, dump_flags, 0);
4822 fprintf (file, "\n\tPREDICATE: ");
4823 print_generic_expr (file, name, 0);
4824 fprintf (file, " %s ", get_tree_code_name (loc->comp_code));
4825 print_generic_expr (file, loc->val, 0);
4826 fprintf (file, "\n\n");
4827 loc = loc->next;
4830 fprintf (file, "\n");
4834 /* Dump all the registered assertions for NAME to stderr. */
4836 DEBUG_FUNCTION void
4837 debug_asserts_for (tree name)
4839 dump_asserts_for (stderr, name);
4843 /* Dump all the registered assertions for all the names to FILE. */
4845 void
4846 dump_all_asserts (FILE *file)
4848 unsigned i;
4849 bitmap_iterator bi;
4851 fprintf (file, "\nASSERT_EXPRs to be inserted\n\n");
4852 EXECUTE_IF_SET_IN_BITMAP (need_assert_for, 0, i, bi)
4853 dump_asserts_for (file, ssa_name (i));
4854 fprintf (file, "\n");
4858 /* Dump all the registered assertions for all the names to stderr. */
4860 DEBUG_FUNCTION void
4861 debug_all_asserts (void)
4863 dump_all_asserts (stderr);
4867 /* If NAME doesn't have an ASSERT_EXPR registered for asserting
4868 'EXPR COMP_CODE VAL' at a location that dominates block BB or
4869 E->DEST, then register this location as a possible insertion point
4870 for ASSERT_EXPR <NAME, EXPR COMP_CODE VAL>.
4872 BB, E and SI provide the exact insertion point for the new
4873 ASSERT_EXPR. If BB is NULL, then the ASSERT_EXPR is to be inserted
4874 on edge E. Otherwise, if E is NULL, the ASSERT_EXPR is inserted on
4875 BB. If SI points to a COND_EXPR or a SWITCH_EXPR statement, then E
4876 must not be NULL. */
4878 static void
4879 register_new_assert_for (tree name, tree expr,
4880 enum tree_code comp_code,
4881 tree val,
4882 basic_block bb,
4883 edge e,
4884 gimple_stmt_iterator si)
4886 assert_locus *n, *loc, *last_loc;
4887 basic_block dest_bb;
4889 gcc_checking_assert (bb == NULL || e == NULL);
4891 if (e == NULL)
4892 gcc_checking_assert (gimple_code (gsi_stmt (si)) != GIMPLE_COND
4893 && gimple_code (gsi_stmt (si)) != GIMPLE_SWITCH);
4895 /* Never build an assert comparing against an integer constant with
4896 TREE_OVERFLOW set. This confuses our undefined overflow warning
4897 machinery. */
4898 if (TREE_OVERFLOW_P (val))
4899 val = drop_tree_overflow (val);
4901 /* The new assertion A will be inserted at BB or E. We need to
4902 determine if the new location is dominated by a previously
4903 registered location for A. If we are doing an edge insertion,
4904 assume that A will be inserted at E->DEST. Note that this is not
4905 necessarily true.
4907 If E is a critical edge, it will be split. But even if E is
4908 split, the new block will dominate the same set of blocks that
4909 E->DEST dominates.
4911 The reverse, however, is not true, blocks dominated by E->DEST
4912 will not be dominated by the new block created to split E. So,
4913 if the insertion location is on a critical edge, we will not use
4914 the new location to move another assertion previously registered
4915 at a block dominated by E->DEST. */
4916 dest_bb = (bb) ? bb : e->dest;
4918 /* If NAME already has an ASSERT_EXPR registered for COMP_CODE and
4919 VAL at a block dominating DEST_BB, then we don't need to insert a new
4920 one. Similarly, if the same assertion already exists at a block
4921 dominated by DEST_BB and the new location is not on a critical
4922 edge, then update the existing location for the assertion (i.e.,
4923 move the assertion up in the dominance tree).
4925 Note, this is implemented as a simple linked list because there
4926 should not be more than a handful of assertions registered per
4927 name. If this becomes a performance problem, a table hashed by
4928 COMP_CODE and VAL could be implemented. */
4929 loc = asserts_for[SSA_NAME_VERSION (name)];
4930 last_loc = loc;
4931 while (loc)
4933 if (loc->comp_code == comp_code
4934 && (loc->val == val
4935 || operand_equal_p (loc->val, val, 0))
4936 && (loc->expr == expr
4937 || operand_equal_p (loc->expr, expr, 0)))
4939 /* If E is not a critical edge and DEST_BB
4940 dominates the existing location for the assertion, move
4941 the assertion up in the dominance tree by updating its
4942 location information. */
4943 if ((e == NULL || !EDGE_CRITICAL_P (e))
4944 && dominated_by_p (CDI_DOMINATORS, loc->bb, dest_bb))
4946 loc->bb = dest_bb;
4947 loc->e = e;
4948 loc->si = si;
4949 return;
4953 /* Update the last node of the list and move to the next one. */
4954 last_loc = loc;
4955 loc = loc->next;
4958 /* If we didn't find an assertion already registered for
4959 NAME COMP_CODE VAL, add a new one at the end of the list of
4960 assertions associated with NAME. */
4961 n = XNEW (struct assert_locus);
4962 n->bb = dest_bb;
4963 n->e = e;
4964 n->si = si;
4965 n->comp_code = comp_code;
4966 n->val = val;
4967 n->expr = expr;
4968 n->next = NULL;
4970 if (last_loc)
4971 last_loc->next = n;
4972 else
4973 asserts_for[SSA_NAME_VERSION (name)] = n;
4975 bitmap_set_bit (need_assert_for, SSA_NAME_VERSION (name));
4978 /* (COND_OP0 COND_CODE COND_OP1) is a predicate which uses NAME.
4979 Extract a suitable test code and value and store them into *CODE_P and
4980 *VAL_P so the predicate is normalized to NAME *CODE_P *VAL_P.
4982 If no extraction was possible, return FALSE, otherwise return TRUE.
4984 If INVERT is true, then we invert the result stored into *CODE_P. */
4986 static bool
4987 extract_code_and_val_from_cond_with_ops (tree name, enum tree_code cond_code,
4988 tree cond_op0, tree cond_op1,
4989 bool invert, enum tree_code *code_p,
4990 tree *val_p)
4992 enum tree_code comp_code;
4993 tree val;
4995 /* Otherwise, we have a comparison of the form NAME COMP VAL
4996 or VAL COMP NAME. */
4997 if (name == cond_op1)
4999 /* If the predicate is of the form VAL COMP NAME, flip
5000 COMP around because we need to register NAME as the
5001 first operand in the predicate. */
5002 comp_code = swap_tree_comparison (cond_code);
5003 val = cond_op0;
5005 else
5007 /* The comparison is of the form NAME COMP VAL, so the
5008 comparison code remains unchanged. */
5009 comp_code = cond_code;
5010 val = cond_op1;
5013 /* Invert the comparison code as necessary. */
5014 if (invert)
5015 comp_code = invert_tree_comparison (comp_code, 0);
5017 /* VRP only handles integral and pointer types. */
5018 if (! INTEGRAL_TYPE_P (TREE_TYPE (val))
5019 && ! POINTER_TYPE_P (TREE_TYPE (val)))
5020 return false;
5022 /* Do not register always-false predicates.
5023 FIXME: this works around a limitation in fold() when dealing with
5024 enumerations. Given 'enum { N1, N2 } x;', fold will not
5025 fold 'if (x > N2)' to 'if (0)'. */
5026 if ((comp_code == GT_EXPR || comp_code == LT_EXPR)
5027 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
5029 tree min = TYPE_MIN_VALUE (TREE_TYPE (val));
5030 tree max = TYPE_MAX_VALUE (TREE_TYPE (val));
5032 if (comp_code == GT_EXPR
5033 && (!max
5034 || compare_values (val, max) == 0))
5035 return false;
5037 if (comp_code == LT_EXPR
5038 && (!min
5039 || compare_values (val, min) == 0))
5040 return false;
5042 *code_p = comp_code;
5043 *val_p = val;
5044 return true;
5047 /* Find out smallest RES where RES > VAL && (RES & MASK) == RES, if any
5048 (otherwise return VAL). VAL and MASK must be zero-extended for
5049 precision PREC. If SGNBIT is non-zero, first xor VAL with SGNBIT
5050 (to transform signed values into unsigned) and at the end xor
5051 SGNBIT back. */
5053 static wide_int
5054 masked_increment (const wide_int &val_in, const wide_int &mask,
5055 const wide_int &sgnbit, unsigned int prec)
5057 wide_int bit = wi::one (prec), res;
5058 unsigned int i;
5060 wide_int val = val_in ^ sgnbit;
5061 for (i = 0; i < prec; i++, bit += bit)
5063 res = mask;
5064 if ((res & bit) == 0)
5065 continue;
5066 res = bit - 1;
5067 res = (val + bit).and_not (res);
5068 res &= mask;
5069 if (wi::gtu_p (res, val))
5070 return res ^ sgnbit;
5072 return val ^ sgnbit;
5075 /* Try to register an edge assertion for SSA name NAME on edge E for
5076 the condition COND contributing to the conditional jump pointed to by BSI.
5077 Invert the condition COND if INVERT is true. */
5079 static void
5080 register_edge_assert_for_2 (tree name, edge e, gimple_stmt_iterator bsi,
5081 enum tree_code cond_code,
5082 tree cond_op0, tree cond_op1, bool invert)
5084 tree val;
5085 enum tree_code comp_code;
5087 if (!extract_code_and_val_from_cond_with_ops (name, cond_code,
5088 cond_op0,
5089 cond_op1,
5090 invert, &comp_code, &val))
5091 return;
5093 /* Only register an ASSERT_EXPR if NAME was found in the sub-graph
5094 reachable from E. */
5095 if (live_on_edge (e, name))
5096 register_new_assert_for (name, name, comp_code, val, NULL, e, bsi);
5098 /* In the case of NAME <= CST and NAME being defined as
5099 NAME = (unsigned) NAME2 + CST2 we can assert NAME2 >= -CST2
5100 and NAME2 <= CST - CST2. We can do the same for NAME > CST.
5101 This catches range and anti-range tests. */
5102 if ((comp_code == LE_EXPR
5103 || comp_code == GT_EXPR)
5104 && TREE_CODE (val) == INTEGER_CST
5105 && TYPE_UNSIGNED (TREE_TYPE (val)))
5107 gimple *def_stmt = SSA_NAME_DEF_STMT (name);
5108 tree cst2 = NULL_TREE, name2 = NULL_TREE, name3 = NULL_TREE;
5110 /* Extract CST2 from the (optional) addition. */
5111 if (is_gimple_assign (def_stmt)
5112 && gimple_assign_rhs_code (def_stmt) == PLUS_EXPR)
5114 name2 = gimple_assign_rhs1 (def_stmt);
5115 cst2 = gimple_assign_rhs2 (def_stmt);
5116 if (TREE_CODE (name2) == SSA_NAME
5117 && TREE_CODE (cst2) == INTEGER_CST)
5118 def_stmt = SSA_NAME_DEF_STMT (name2);
5121 /* Extract NAME2 from the (optional) sign-changing cast. */
5122 if (gimple_assign_cast_p (def_stmt))
5124 if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt))
5125 && ! TYPE_UNSIGNED (TREE_TYPE (gimple_assign_rhs1 (def_stmt)))
5126 && (TYPE_PRECISION (gimple_expr_type (def_stmt))
5127 == TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (def_stmt)))))
5128 name3 = gimple_assign_rhs1 (def_stmt);
5131 /* If name3 is used later, create an ASSERT_EXPR for it. */
5132 if (name3 != NULL_TREE
5133 && TREE_CODE (name3) == SSA_NAME
5134 && (cst2 == NULL_TREE
5135 || TREE_CODE (cst2) == INTEGER_CST)
5136 && INTEGRAL_TYPE_P (TREE_TYPE (name3))
5137 && live_on_edge (e, name3))
5139 tree tmp;
5141 /* Build an expression for the range test. */
5142 tmp = build1 (NOP_EXPR, TREE_TYPE (name), name3);
5143 if (cst2 != NULL_TREE)
5144 tmp = build2 (PLUS_EXPR, TREE_TYPE (name), tmp, cst2);
5146 if (dump_file)
5148 fprintf (dump_file, "Adding assert for ");
5149 print_generic_expr (dump_file, name3, 0);
5150 fprintf (dump_file, " from ");
5151 print_generic_expr (dump_file, tmp, 0);
5152 fprintf (dump_file, "\n");
5155 register_new_assert_for (name3, tmp, comp_code, val, NULL, e, bsi);
5158 /* If name2 is used later, create an ASSERT_EXPR for it. */
5159 if (name2 != NULL_TREE
5160 && TREE_CODE (name2) == SSA_NAME
5161 && TREE_CODE (cst2) == INTEGER_CST
5162 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
5163 && live_on_edge (e, name2))
5165 tree tmp;
5167 /* Build an expression for the range test. */
5168 tmp = name2;
5169 if (TREE_TYPE (name) != TREE_TYPE (name2))
5170 tmp = build1 (NOP_EXPR, TREE_TYPE (name), tmp);
5171 if (cst2 != NULL_TREE)
5172 tmp = build2 (PLUS_EXPR, TREE_TYPE (name), tmp, cst2);
5174 if (dump_file)
5176 fprintf (dump_file, "Adding assert for ");
5177 print_generic_expr (dump_file, name2, 0);
5178 fprintf (dump_file, " from ");
5179 print_generic_expr (dump_file, tmp, 0);
5180 fprintf (dump_file, "\n");
5183 register_new_assert_for (name2, tmp, comp_code, val, NULL, e, bsi);
5187 /* In the case of post-in/decrement tests like if (i++) ... and uses
5188 of the in/decremented value on the edge the extra name we want to
5189 assert for is not on the def chain of the name compared. Instead
5190 it is in the set of use stmts.
5191 Similar cases happen for conversions that were simplified through
5192 fold_{sign_changed,widened}_comparison. */
5193 if ((comp_code == NE_EXPR
5194 || comp_code == EQ_EXPR)
5195 && TREE_CODE (val) == INTEGER_CST)
5197 imm_use_iterator ui;
5198 gimple *use_stmt;
5199 FOR_EACH_IMM_USE_STMT (use_stmt, ui, name)
5201 if (!is_gimple_assign (use_stmt))
5202 continue;
5204 /* Cut off to use-stmts that are dominating the predecessor. */
5205 if (!dominated_by_p (CDI_DOMINATORS, e->src, gimple_bb (use_stmt)))
5206 continue;
5208 tree name2 = gimple_assign_lhs (use_stmt);
5209 if (TREE_CODE (name2) != SSA_NAME
5210 || !live_on_edge (e, name2))
5211 continue;
5213 enum tree_code code = gimple_assign_rhs_code (use_stmt);
5214 tree cst;
5215 if (code == PLUS_EXPR
5216 || code == MINUS_EXPR)
5218 cst = gimple_assign_rhs2 (use_stmt);
5219 if (TREE_CODE (cst) != INTEGER_CST)
5220 continue;
5221 cst = int_const_binop (code, val, cst);
5223 else if (CONVERT_EXPR_CODE_P (code))
5225 /* For truncating conversions we cannot record
5226 an inequality. */
5227 if (comp_code == NE_EXPR
5228 && (TYPE_PRECISION (TREE_TYPE (name2))
5229 < TYPE_PRECISION (TREE_TYPE (name))))
5230 continue;
5231 cst = fold_convert (TREE_TYPE (name2), val);
5233 else
5234 continue;
5236 if (TREE_OVERFLOW_P (cst))
5237 cst = drop_tree_overflow (cst);
5238 register_new_assert_for (name2, name2, comp_code, cst,
5239 NULL, e, bsi);
5243 if (TREE_CODE_CLASS (comp_code) == tcc_comparison
5244 && TREE_CODE (val) == INTEGER_CST)
5246 gimple *def_stmt = SSA_NAME_DEF_STMT (name);
5247 tree name2 = NULL_TREE, names[2], cst2 = NULL_TREE;
5248 tree val2 = NULL_TREE;
5249 unsigned int prec = TYPE_PRECISION (TREE_TYPE (val));
5250 wide_int mask = wi::zero (prec);
5251 unsigned int nprec = prec;
5252 enum tree_code rhs_code = ERROR_MARK;
5254 if (is_gimple_assign (def_stmt))
5255 rhs_code = gimple_assign_rhs_code (def_stmt);
5257 /* In the case of NAME != CST1 where NAME = A +- CST2 we can
5258 assert that A != CST1 -+ CST2. */
5259 if ((comp_code == EQ_EXPR || comp_code == NE_EXPR)
5260 && (rhs_code == PLUS_EXPR || rhs_code == MINUS_EXPR))
5262 tree op0 = gimple_assign_rhs1 (def_stmt);
5263 tree op1 = gimple_assign_rhs2 (def_stmt);
5264 if (TREE_CODE (op0) == SSA_NAME
5265 && TREE_CODE (op1) == INTEGER_CST
5266 && live_on_edge (e, op0))
5268 enum tree_code reverse_op = (rhs_code == PLUS_EXPR
5269 ? MINUS_EXPR : PLUS_EXPR);
5270 op1 = int_const_binop (reverse_op, val, op1);
5271 if (TREE_OVERFLOW (op1))
5272 op1 = drop_tree_overflow (op1);
5273 register_new_assert_for (op0, op0, comp_code, op1, NULL, e, bsi);
5277 /* Add asserts for NAME cmp CST and NAME being defined
5278 as NAME = (int) NAME2. */
5279 if (!TYPE_UNSIGNED (TREE_TYPE (val))
5280 && (comp_code == LE_EXPR || comp_code == LT_EXPR
5281 || comp_code == GT_EXPR || comp_code == GE_EXPR)
5282 && gimple_assign_cast_p (def_stmt))
5284 name2 = gimple_assign_rhs1 (def_stmt);
5285 if (CONVERT_EXPR_CODE_P (rhs_code)
5286 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
5287 && TYPE_UNSIGNED (TREE_TYPE (name2))
5288 && prec == TYPE_PRECISION (TREE_TYPE (name2))
5289 && (comp_code == LE_EXPR || comp_code == GT_EXPR
5290 || !tree_int_cst_equal (val,
5291 TYPE_MIN_VALUE (TREE_TYPE (val))))
5292 && live_on_edge (e, name2))
5294 tree tmp, cst;
5295 enum tree_code new_comp_code = comp_code;
5297 cst = fold_convert (TREE_TYPE (name2),
5298 TYPE_MIN_VALUE (TREE_TYPE (val)));
5299 /* Build an expression for the range test. */
5300 tmp = build2 (PLUS_EXPR, TREE_TYPE (name2), name2, cst);
5301 cst = fold_build2 (PLUS_EXPR, TREE_TYPE (name2), cst,
5302 fold_convert (TREE_TYPE (name2), val));
5303 if (comp_code == LT_EXPR || comp_code == GE_EXPR)
5305 new_comp_code = comp_code == LT_EXPR ? LE_EXPR : GT_EXPR;
5306 cst = fold_build2 (MINUS_EXPR, TREE_TYPE (name2), cst,
5307 build_int_cst (TREE_TYPE (name2), 1));
5310 if (dump_file)
5312 fprintf (dump_file, "Adding assert for ");
5313 print_generic_expr (dump_file, name2, 0);
5314 fprintf (dump_file, " from ");
5315 print_generic_expr (dump_file, tmp, 0);
5316 fprintf (dump_file, "\n");
5319 register_new_assert_for (name2, tmp, new_comp_code, cst, NULL,
5320 e, bsi);
5324 /* Add asserts for NAME cmp CST and NAME being defined as
5325 NAME = NAME2 >> CST2.
5327 Extract CST2 from the right shift. */
5328 if (rhs_code == RSHIFT_EXPR)
5330 name2 = gimple_assign_rhs1 (def_stmt);
5331 cst2 = gimple_assign_rhs2 (def_stmt);
5332 if (TREE_CODE (name2) == SSA_NAME
5333 && tree_fits_uhwi_p (cst2)
5334 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
5335 && IN_RANGE (tree_to_uhwi (cst2), 1, prec - 1)
5336 && prec == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (val)))
5337 && live_on_edge (e, name2))
5339 mask = wi::mask (tree_to_uhwi (cst2), false, prec);
5340 val2 = fold_binary (LSHIFT_EXPR, TREE_TYPE (val), val, cst2);
5343 if (val2 != NULL_TREE
5344 && TREE_CODE (val2) == INTEGER_CST
5345 && simple_cst_equal (fold_build2 (RSHIFT_EXPR,
5346 TREE_TYPE (val),
5347 val2, cst2), val))
5349 enum tree_code new_comp_code = comp_code;
5350 tree tmp, new_val;
5352 tmp = name2;
5353 if (comp_code == EQ_EXPR || comp_code == NE_EXPR)
5355 if (!TYPE_UNSIGNED (TREE_TYPE (val)))
5357 tree type = build_nonstandard_integer_type (prec, 1);
5358 tmp = build1 (NOP_EXPR, type, name2);
5359 val2 = fold_convert (type, val2);
5361 tmp = fold_build2 (MINUS_EXPR, TREE_TYPE (tmp), tmp, val2);
5362 new_val = wide_int_to_tree (TREE_TYPE (tmp), mask);
5363 new_comp_code = comp_code == EQ_EXPR ? LE_EXPR : GT_EXPR;
5365 else if (comp_code == LT_EXPR || comp_code == GE_EXPR)
5367 wide_int minval
5368 = wi::min_value (prec, TYPE_SIGN (TREE_TYPE (val)));
5369 new_val = val2;
5370 if (minval == new_val)
5371 new_val = NULL_TREE;
5373 else
5375 wide_int maxval
5376 = wi::max_value (prec, TYPE_SIGN (TREE_TYPE (val)));
5377 mask |= val2;
5378 if (mask == maxval)
5379 new_val = NULL_TREE;
5380 else
5381 new_val = wide_int_to_tree (TREE_TYPE (val2), mask);
5384 if (new_val)
5386 if (dump_file)
5388 fprintf (dump_file, "Adding assert for ");
5389 print_generic_expr (dump_file, name2, 0);
5390 fprintf (dump_file, " from ");
5391 print_generic_expr (dump_file, tmp, 0);
5392 fprintf (dump_file, "\n");
5395 register_new_assert_for (name2, tmp, new_comp_code, new_val,
5396 NULL, e, bsi);
5400 /* Add asserts for NAME cmp CST and NAME being defined as
5401 NAME = NAME2 & CST2.
5403 Extract CST2 from the and.
5405 Also handle
5406 NAME = (unsigned) NAME2;
5407 casts where NAME's type is unsigned and has smaller precision
5408 than NAME2's type as if it was NAME = NAME2 & MASK. */
5409 names[0] = NULL_TREE;
5410 names[1] = NULL_TREE;
5411 cst2 = NULL_TREE;
5412 if (rhs_code == BIT_AND_EXPR
5413 || (CONVERT_EXPR_CODE_P (rhs_code)
5414 && INTEGRAL_TYPE_P (TREE_TYPE (val))
5415 && TYPE_UNSIGNED (TREE_TYPE (val))
5416 && TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (def_stmt)))
5417 > prec))
5419 name2 = gimple_assign_rhs1 (def_stmt);
5420 if (rhs_code == BIT_AND_EXPR)
5421 cst2 = gimple_assign_rhs2 (def_stmt);
5422 else
5424 cst2 = TYPE_MAX_VALUE (TREE_TYPE (val));
5425 nprec = TYPE_PRECISION (TREE_TYPE (name2));
5427 if (TREE_CODE (name2) == SSA_NAME
5428 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
5429 && TREE_CODE (cst2) == INTEGER_CST
5430 && !integer_zerop (cst2)
5431 && (nprec > 1
5432 || TYPE_UNSIGNED (TREE_TYPE (val))))
5434 gimple *def_stmt2 = SSA_NAME_DEF_STMT (name2);
5435 if (gimple_assign_cast_p (def_stmt2))
5437 names[1] = gimple_assign_rhs1 (def_stmt2);
5438 if (!CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt2))
5439 || !INTEGRAL_TYPE_P (TREE_TYPE (names[1]))
5440 || (TYPE_PRECISION (TREE_TYPE (name2))
5441 != TYPE_PRECISION (TREE_TYPE (names[1])))
5442 || !live_on_edge (e, names[1]))
5443 names[1] = NULL_TREE;
5445 if (live_on_edge (e, name2))
5446 names[0] = name2;
5449 if (names[0] || names[1])
5451 wide_int minv, maxv, valv, cst2v;
5452 wide_int tem, sgnbit;
5453 bool valid_p = false, valn, cst2n;
5454 enum tree_code ccode = comp_code;
5456 valv = wide_int::from (val, nprec, UNSIGNED);
5457 cst2v = wide_int::from (cst2, nprec, UNSIGNED);
5458 valn = wi::neg_p (valv, TYPE_SIGN (TREE_TYPE (val)));
5459 cst2n = wi::neg_p (cst2v, TYPE_SIGN (TREE_TYPE (val)));
5460 /* If CST2 doesn't have most significant bit set,
5461 but VAL is negative, we have comparison like
5462 if ((x & 0x123) > -4) (always true). Just give up. */
5463 if (!cst2n && valn)
5464 ccode = ERROR_MARK;
5465 if (cst2n)
5466 sgnbit = wi::set_bit_in_zero (nprec - 1, nprec);
5467 else
5468 sgnbit = wi::zero (nprec);
5469 minv = valv & cst2v;
5470 switch (ccode)
5472 case EQ_EXPR:
5473 /* Minimum unsigned value for equality is VAL & CST2
5474 (should be equal to VAL, otherwise we probably should
5475 have folded the comparison into false) and
5476 maximum unsigned value is VAL | ~CST2. */
5477 maxv = valv | ~cst2v;
5478 valid_p = true;
5479 break;
5481 case NE_EXPR:
5482 tem = valv | ~cst2v;
5483 /* If VAL is 0, handle (X & CST2) != 0 as (X & CST2) > 0U. */
5484 if (valv == 0)
5486 cst2n = false;
5487 sgnbit = wi::zero (nprec);
5488 goto gt_expr;
5490 /* If (VAL | ~CST2) is all ones, handle it as
5491 (X & CST2) < VAL. */
5492 if (tem == -1)
5494 cst2n = false;
5495 valn = false;
5496 sgnbit = wi::zero (nprec);
5497 goto lt_expr;
5499 if (!cst2n && wi::neg_p (cst2v))
5500 sgnbit = wi::set_bit_in_zero (nprec - 1, nprec);
5501 if (sgnbit != 0)
5503 if (valv == sgnbit)
5505 cst2n = true;
5506 valn = true;
5507 goto gt_expr;
5509 if (tem == wi::mask (nprec - 1, false, nprec))
5511 cst2n = true;
5512 goto lt_expr;
5514 if (!cst2n)
5515 sgnbit = wi::zero (nprec);
5517 break;
5519 case GE_EXPR:
5520 /* Minimum unsigned value for >= if (VAL & CST2) == VAL
5521 is VAL and maximum unsigned value is ~0. For signed
5522 comparison, if CST2 doesn't have most significant bit
5523 set, handle it similarly. If CST2 has MSB set,
5524 the minimum is the same, and maximum is ~0U/2. */
5525 if (minv != valv)
5527 /* If (VAL & CST2) != VAL, X & CST2 can't be equal to
5528 VAL. */
5529 minv = masked_increment (valv, cst2v, sgnbit, nprec);
5530 if (minv == valv)
5531 break;
5533 maxv = wi::mask (nprec - (cst2n ? 1 : 0), false, nprec);
5534 valid_p = true;
5535 break;
5537 case GT_EXPR:
5538 gt_expr:
5539 /* Find out smallest MINV where MINV > VAL
5540 && (MINV & CST2) == MINV, if any. If VAL is signed and
5541 CST2 has MSB set, compute it biased by 1 << (nprec - 1). */
5542 minv = masked_increment (valv, cst2v, sgnbit, nprec);
5543 if (minv == valv)
5544 break;
5545 maxv = wi::mask (nprec - (cst2n ? 1 : 0), false, nprec);
5546 valid_p = true;
5547 break;
5549 case LE_EXPR:
5550 /* Minimum unsigned value for <= is 0 and maximum
5551 unsigned value is VAL | ~CST2 if (VAL & CST2) == VAL.
5552 Otherwise, find smallest VAL2 where VAL2 > VAL
5553 && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
5554 as maximum.
5555 For signed comparison, if CST2 doesn't have most
5556 significant bit set, handle it similarly. If CST2 has
5557 MSB set, the maximum is the same and minimum is INT_MIN. */
5558 if (minv == valv)
5559 maxv = valv;
5560 else
5562 maxv = masked_increment (valv, cst2v, sgnbit, nprec);
5563 if (maxv == valv)
5564 break;
5565 maxv -= 1;
5567 maxv |= ~cst2v;
5568 minv = sgnbit;
5569 valid_p = true;
5570 break;
5572 case LT_EXPR:
5573 lt_expr:
5574 /* Minimum unsigned value for < is 0 and maximum
5575 unsigned value is (VAL-1) | ~CST2 if (VAL & CST2) == VAL.
5576 Otherwise, find smallest VAL2 where VAL2 > VAL
5577 && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
5578 as maximum.
5579 For signed comparison, if CST2 doesn't have most
5580 significant bit set, handle it similarly. If CST2 has
5581 MSB set, the maximum is the same and minimum is INT_MIN. */
5582 if (minv == valv)
5584 if (valv == sgnbit)
5585 break;
5586 maxv = valv;
5588 else
5590 maxv = masked_increment (valv, cst2v, sgnbit, nprec);
5591 if (maxv == valv)
5592 break;
5594 maxv -= 1;
5595 maxv |= ~cst2v;
5596 minv = sgnbit;
5597 valid_p = true;
5598 break;
5600 default:
5601 break;
5603 if (valid_p
5604 && (maxv - minv) != -1)
5606 tree tmp, new_val, type;
5607 int i;
5609 for (i = 0; i < 2; i++)
5610 if (names[i])
5612 wide_int maxv2 = maxv;
5613 tmp = names[i];
5614 type = TREE_TYPE (names[i]);
5615 if (!TYPE_UNSIGNED (type))
5617 type = build_nonstandard_integer_type (nprec, 1);
5618 tmp = build1 (NOP_EXPR, type, names[i]);
5620 if (minv != 0)
5622 tmp = build2 (PLUS_EXPR, type, tmp,
5623 wide_int_to_tree (type, -minv));
5624 maxv2 = maxv - minv;
5626 new_val = wide_int_to_tree (type, maxv2);
5628 if (dump_file)
5630 fprintf (dump_file, "Adding assert for ");
5631 print_generic_expr (dump_file, names[i], 0);
5632 fprintf (dump_file, " from ");
5633 print_generic_expr (dump_file, tmp, 0);
5634 fprintf (dump_file, "\n");
5637 register_new_assert_for (names[i], tmp, LE_EXPR,
5638 new_val, NULL, e, bsi);
5645 /* OP is an operand of a truth value expression which is known to have
5646 a particular value. Register any asserts for OP and for any
5647 operands in OP's defining statement.
5649 If CODE is EQ_EXPR, then we want to register OP is zero (false),
5650 if CODE is NE_EXPR, then we want to register OP is nonzero (true). */
5652 static void
5653 register_edge_assert_for_1 (tree op, enum tree_code code,
5654 edge e, gimple_stmt_iterator bsi)
5656 gimple *op_def;
5657 tree val;
5658 enum tree_code rhs_code;
5660 /* We only care about SSA_NAMEs. */
5661 if (TREE_CODE (op) != SSA_NAME)
5662 return;
5664 /* We know that OP will have a zero or nonzero value. If OP is used
5665 more than once go ahead and register an assert for OP. */
5666 if (live_on_edge (e, op))
5668 val = build_int_cst (TREE_TYPE (op), 0);
5669 register_new_assert_for (op, op, code, val, NULL, e, bsi);
5672 /* Now look at how OP is set. If it's set from a comparison,
5673 a truth operation or some bit operations, then we may be able
5674 to register information about the operands of that assignment. */
5675 op_def = SSA_NAME_DEF_STMT (op);
5676 if (gimple_code (op_def) != GIMPLE_ASSIGN)
5677 return;
5679 rhs_code = gimple_assign_rhs_code (op_def);
5681 if (TREE_CODE_CLASS (rhs_code) == tcc_comparison)
5683 bool invert = (code == EQ_EXPR ? true : false);
5684 tree op0 = gimple_assign_rhs1 (op_def);
5685 tree op1 = gimple_assign_rhs2 (op_def);
5687 if (TREE_CODE (op0) == SSA_NAME)
5688 register_edge_assert_for_2 (op0, e, bsi, rhs_code, op0, op1, invert);
5689 if (TREE_CODE (op1) == SSA_NAME)
5690 register_edge_assert_for_2 (op1, e, bsi, rhs_code, op0, op1, invert);
5692 else if ((code == NE_EXPR
5693 && gimple_assign_rhs_code (op_def) == BIT_AND_EXPR)
5694 || (code == EQ_EXPR
5695 && gimple_assign_rhs_code (op_def) == BIT_IOR_EXPR))
5697 /* Recurse on each operand. */
5698 tree op0 = gimple_assign_rhs1 (op_def);
5699 tree op1 = gimple_assign_rhs2 (op_def);
5700 if (TREE_CODE (op0) == SSA_NAME
5701 && has_single_use (op0))
5702 register_edge_assert_for_1 (op0, code, e, bsi);
5703 if (TREE_CODE (op1) == SSA_NAME
5704 && has_single_use (op1))
5705 register_edge_assert_for_1 (op1, code, e, bsi);
5707 else if (gimple_assign_rhs_code (op_def) == BIT_NOT_EXPR
5708 && TYPE_PRECISION (TREE_TYPE (gimple_assign_lhs (op_def))) == 1)
5710 /* Recurse, flipping CODE. */
5711 code = invert_tree_comparison (code, false);
5712 register_edge_assert_for_1 (gimple_assign_rhs1 (op_def), code, e, bsi);
5714 else if (gimple_assign_rhs_code (op_def) == SSA_NAME)
5716 /* Recurse through the copy. */
5717 register_edge_assert_for_1 (gimple_assign_rhs1 (op_def), code, e, bsi);
5719 else if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (op_def)))
5721 /* Recurse through the type conversion, unless it is a narrowing
5722 conversion or conversion from non-integral type. */
5723 tree rhs = gimple_assign_rhs1 (op_def);
5724 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs))
5725 && (TYPE_PRECISION (TREE_TYPE (rhs))
5726 <= TYPE_PRECISION (TREE_TYPE (op))))
5727 register_edge_assert_for_1 (rhs, code, e, bsi);
5731 /* Try to register an edge assertion for SSA name NAME on edge E for
5732 the condition COND contributing to the conditional jump pointed to by
5733 SI. */
5735 static void
5736 register_edge_assert_for (tree name, edge e, gimple_stmt_iterator si,
5737 enum tree_code cond_code, tree cond_op0,
5738 tree cond_op1)
5740 tree val;
5741 enum tree_code comp_code;
5742 bool is_else_edge = (e->flags & EDGE_FALSE_VALUE) != 0;
5744 /* Do not attempt to infer anything in names that flow through
5745 abnormal edges. */
5746 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name))
5747 return;
5749 if (!extract_code_and_val_from_cond_with_ops (name, cond_code,
5750 cond_op0, cond_op1,
5751 is_else_edge,
5752 &comp_code, &val))
5753 return;
5755 /* Register ASSERT_EXPRs for name. */
5756 register_edge_assert_for_2 (name, e, si, cond_code, cond_op0,
5757 cond_op1, is_else_edge);
5760 /* If COND is effectively an equality test of an SSA_NAME against
5761 the value zero or one, then we may be able to assert values
5762 for SSA_NAMEs which flow into COND. */
5764 /* In the case of NAME == 1 or NAME != 0, for BIT_AND_EXPR defining
5765 statement of NAME we can assert both operands of the BIT_AND_EXPR
5766 have nonzero value. */
5767 if (((comp_code == EQ_EXPR && integer_onep (val))
5768 || (comp_code == NE_EXPR && integer_zerop (val))))
5770 gimple *def_stmt = SSA_NAME_DEF_STMT (name);
5772 if (is_gimple_assign (def_stmt)
5773 && gimple_assign_rhs_code (def_stmt) == BIT_AND_EXPR)
5775 tree op0 = gimple_assign_rhs1 (def_stmt);
5776 tree op1 = gimple_assign_rhs2 (def_stmt);
5777 register_edge_assert_for_1 (op0, NE_EXPR, e, si);
5778 register_edge_assert_for_1 (op1, NE_EXPR, e, si);
5782 /* In the case of NAME == 0 or NAME != 1, for BIT_IOR_EXPR defining
5783 statement of NAME we can assert both operands of the BIT_IOR_EXPR
5784 have zero value. */
5785 if (((comp_code == EQ_EXPR && integer_zerop (val))
5786 || (comp_code == NE_EXPR && integer_onep (val))))
5788 gimple *def_stmt = SSA_NAME_DEF_STMT (name);
5790 /* For BIT_IOR_EXPR only if NAME == 0 both operands have
5791 necessarily zero value, or if type-precision is one. */
5792 if (is_gimple_assign (def_stmt)
5793 && (gimple_assign_rhs_code (def_stmt) == BIT_IOR_EXPR
5794 && (TYPE_PRECISION (TREE_TYPE (name)) == 1
5795 || comp_code == EQ_EXPR)))
5797 tree op0 = gimple_assign_rhs1 (def_stmt);
5798 tree op1 = gimple_assign_rhs2 (def_stmt);
5799 register_edge_assert_for_1 (op0, EQ_EXPR, e, si);
5800 register_edge_assert_for_1 (op1, EQ_EXPR, e, si);
5806 /* Determine whether the outgoing edges of BB should receive an
5807 ASSERT_EXPR for each of the operands of BB's LAST statement.
5808 The last statement of BB must be a COND_EXPR.
5810 If any of the sub-graphs rooted at BB have an interesting use of
5811 the predicate operands, an assert location node is added to the
5812 list of assertions for the corresponding operands. */
5814 static void
5815 find_conditional_asserts (basic_block bb, gcond *last)
5817 gimple_stmt_iterator bsi;
5818 tree op;
5819 edge_iterator ei;
5820 edge e;
5821 ssa_op_iter iter;
5823 bsi = gsi_for_stmt (last);
5825 /* Look for uses of the operands in each of the sub-graphs
5826 rooted at BB. We need to check each of the outgoing edges
5827 separately, so that we know what kind of ASSERT_EXPR to
5828 insert. */
5829 FOR_EACH_EDGE (e, ei, bb->succs)
5831 if (e->dest == bb)
5832 continue;
5834 /* Register the necessary assertions for each operand in the
5835 conditional predicate. */
5836 FOR_EACH_SSA_TREE_OPERAND (op, last, iter, SSA_OP_USE)
5837 register_edge_assert_for (op, e, bsi,
5838 gimple_cond_code (last),
5839 gimple_cond_lhs (last),
5840 gimple_cond_rhs (last));
5844 struct case_info
5846 tree expr;
5847 basic_block bb;
5850 /* Compare two case labels sorting first by the destination bb index
5851 and then by the case value. */
5853 static int
5854 compare_case_labels (const void *p1, const void *p2)
5856 const struct case_info *ci1 = (const struct case_info *) p1;
5857 const struct case_info *ci2 = (const struct case_info *) p2;
5858 int idx1 = ci1->bb->index;
5859 int idx2 = ci2->bb->index;
5861 if (idx1 < idx2)
5862 return -1;
5863 else if (idx1 == idx2)
5865 /* Make sure the default label is first in a group. */
5866 if (!CASE_LOW (ci1->expr))
5867 return -1;
5868 else if (!CASE_LOW (ci2->expr))
5869 return 1;
5870 else
5871 return tree_int_cst_compare (CASE_LOW (ci1->expr),
5872 CASE_LOW (ci2->expr));
5874 else
5875 return 1;
5878 /* Determine whether the outgoing edges of BB should receive an
5879 ASSERT_EXPR for each of the operands of BB's LAST statement.
5880 The last statement of BB must be a SWITCH_EXPR.
5882 If any of the sub-graphs rooted at BB have an interesting use of
5883 the predicate operands, an assert location node is added to the
5884 list of assertions for the corresponding operands. */
5886 static void
5887 find_switch_asserts (basic_block bb, gswitch *last)
5889 gimple_stmt_iterator bsi;
5890 tree op;
5891 edge e;
5892 struct case_info *ci;
5893 size_t n = gimple_switch_num_labels (last);
5894 #if GCC_VERSION >= 4000
5895 unsigned int idx;
5896 #else
5897 /* Work around GCC 3.4 bug (PR 37086). */
5898 volatile unsigned int idx;
5899 #endif
5901 bsi = gsi_for_stmt (last);
5902 op = gimple_switch_index (last);
5903 if (TREE_CODE (op) != SSA_NAME)
5904 return;
5906 /* Build a vector of case labels sorted by destination label. */
5907 ci = XNEWVEC (struct case_info, n);
5908 for (idx = 0; idx < n; ++idx)
5910 ci[idx].expr = gimple_switch_label (last, idx);
5911 ci[idx].bb = label_to_block (CASE_LABEL (ci[idx].expr));
5913 qsort (ci, n, sizeof (struct case_info), compare_case_labels);
5915 for (idx = 0; idx < n; ++idx)
5917 tree min, max;
5918 tree cl = ci[idx].expr;
5919 basic_block cbb = ci[idx].bb;
5921 min = CASE_LOW (cl);
5922 max = CASE_HIGH (cl);
5924 /* If there are multiple case labels with the same destination
5925 we need to combine them to a single value range for the edge. */
5926 if (idx + 1 < n && cbb == ci[idx + 1].bb)
5928 /* Skip labels until the last of the group. */
5929 do {
5930 ++idx;
5931 } while (idx < n && cbb == ci[idx].bb);
5932 --idx;
5934 /* Pick up the maximum of the case label range. */
5935 if (CASE_HIGH (ci[idx].expr))
5936 max = CASE_HIGH (ci[idx].expr);
5937 else
5938 max = CASE_LOW (ci[idx].expr);
5941 /* Nothing to do if the range includes the default label until we
5942 can register anti-ranges. */
5943 if (min == NULL_TREE)
5944 continue;
5946 /* Find the edge to register the assert expr on. */
5947 e = find_edge (bb, cbb);
5949 /* Register the necessary assertions for the operand in the
5950 SWITCH_EXPR. */
5951 register_edge_assert_for (op, e, bsi,
5952 max ? GE_EXPR : EQ_EXPR,
5953 op, fold_convert (TREE_TYPE (op), min));
5954 if (max)
5955 register_edge_assert_for (op, e, bsi, LE_EXPR, op,
5956 fold_convert (TREE_TYPE (op), max));
5959 XDELETEVEC (ci);
5963 /* Traverse all the statements in block BB looking for statements that
5964 may generate useful assertions for the SSA names in their operand.
5965 If a statement produces a useful assertion A for name N_i, then the
5966 list of assertions already generated for N_i is scanned to
5967 determine if A is actually needed.
5969 If N_i already had the assertion A at a location dominating the
5970 current location, then nothing needs to be done. Otherwise, the
5971 new location for A is recorded instead.
5973 1- For every statement S in BB, all the variables used by S are
5974 added to bitmap FOUND_IN_SUBGRAPH.
5976 2- If statement S uses an operand N in a way that exposes a known
5977 value range for N, then if N was not already generated by an
5978 ASSERT_EXPR, create a new assert location for N. For instance,
5979 if N is a pointer and the statement dereferences it, we can
5980 assume that N is not NULL.
5982 3- COND_EXPRs are a special case of #2. We can derive range
5983 information from the predicate but need to insert different
5984 ASSERT_EXPRs for each of the sub-graphs rooted at the
5985 conditional block. If the last statement of BB is a conditional
5986 expression of the form 'X op Y', then
5988 a) Remove X and Y from the set FOUND_IN_SUBGRAPH.
5990 b) If the conditional is the only entry point to the sub-graph
5991 corresponding to the THEN_CLAUSE, recurse into it. On
5992 return, if X and/or Y are marked in FOUND_IN_SUBGRAPH, then
5993 an ASSERT_EXPR is added for the corresponding variable.
5995 c) Repeat step (b) on the ELSE_CLAUSE.
5997 d) Mark X and Y in FOUND_IN_SUBGRAPH.
5999 For instance,
6001 if (a == 9)
6002 b = a;
6003 else
6004 b = c + 1;
6006 In this case, an assertion on the THEN clause is useful to
6007 determine that 'a' is always 9 on that edge. However, an assertion
6008 on the ELSE clause would be unnecessary.
6010 4- If BB does not end in a conditional expression, then we recurse
6011 into BB's dominator children.
6013 At the end of the recursive traversal, every SSA name will have a
6014 list of locations where ASSERT_EXPRs should be added. When a new
6015 location for name N is found, it is registered by calling
6016 register_new_assert_for. That function keeps track of all the
6017 registered assertions to prevent adding unnecessary assertions.
6018 For instance, if a pointer P_4 is dereferenced more than once in a
6019 dominator tree, only the location dominating all the dereference of
6020 P_4 will receive an ASSERT_EXPR. */
6022 static void
6023 find_assert_locations_1 (basic_block bb, sbitmap live)
6025 gimple *last;
6027 last = last_stmt (bb);
6029 /* If BB's last statement is a conditional statement involving integer
6030 operands, determine if we need to add ASSERT_EXPRs. */
6031 if (last
6032 && gimple_code (last) == GIMPLE_COND
6033 && !fp_predicate (last)
6034 && !ZERO_SSA_OPERANDS (last, SSA_OP_USE))
6035 find_conditional_asserts (bb, as_a <gcond *> (last));
6037 /* If BB's last statement is a switch statement involving integer
6038 operands, determine if we need to add ASSERT_EXPRs. */
6039 if (last
6040 && gimple_code (last) == GIMPLE_SWITCH
6041 && !ZERO_SSA_OPERANDS (last, SSA_OP_USE))
6042 find_switch_asserts (bb, as_a <gswitch *> (last));
6044 /* Traverse all the statements in BB marking used names and looking
6045 for statements that may infer assertions for their used operands. */
6046 for (gimple_stmt_iterator si = gsi_last_bb (bb); !gsi_end_p (si);
6047 gsi_prev (&si))
6049 gimple *stmt;
6050 tree op;
6051 ssa_op_iter i;
6053 stmt = gsi_stmt (si);
6055 if (is_gimple_debug (stmt))
6056 continue;
6058 /* See if we can derive an assertion for any of STMT's operands. */
6059 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_USE)
6061 tree value;
6062 enum tree_code comp_code;
6064 /* If op is not live beyond this stmt, do not bother to insert
6065 asserts for it. */
6066 if (!bitmap_bit_p (live, SSA_NAME_VERSION (op)))
6067 continue;
6069 /* If OP is used in such a way that we can infer a value
6070 range for it, and we don't find a previous assertion for
6071 it, create a new assertion location node for OP. */
6072 if (infer_value_range (stmt, op, &comp_code, &value))
6074 /* If we are able to infer a nonzero value range for OP,
6075 then walk backwards through the use-def chain to see if OP
6076 was set via a typecast.
6078 If so, then we can also infer a nonzero value range
6079 for the operand of the NOP_EXPR. */
6080 if (comp_code == NE_EXPR && integer_zerop (value))
6082 tree t = op;
6083 gimple *def_stmt = SSA_NAME_DEF_STMT (t);
6085 while (is_gimple_assign (def_stmt)
6086 && CONVERT_EXPR_CODE_P
6087 (gimple_assign_rhs_code (def_stmt))
6088 && TREE_CODE
6089 (gimple_assign_rhs1 (def_stmt)) == SSA_NAME
6090 && POINTER_TYPE_P
6091 (TREE_TYPE (gimple_assign_rhs1 (def_stmt))))
6093 t = gimple_assign_rhs1 (def_stmt);
6094 def_stmt = SSA_NAME_DEF_STMT (t);
6096 /* Note we want to register the assert for the
6097 operand of the NOP_EXPR after SI, not after the
6098 conversion. */
6099 if (bitmap_bit_p (live, SSA_NAME_VERSION (t)))
6100 register_new_assert_for (t, t, comp_code, value,
6101 bb, NULL, si);
6105 register_new_assert_for (op, op, comp_code, value, bb, NULL, si);
6109 /* Update live. */
6110 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_USE)
6111 bitmap_set_bit (live, SSA_NAME_VERSION (op));
6112 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_DEF)
6113 bitmap_clear_bit (live, SSA_NAME_VERSION (op));
6116 /* Traverse all PHI nodes in BB, updating live. */
6117 for (gphi_iterator si = gsi_start_phis (bb); !gsi_end_p (si);
6118 gsi_next (&si))
6120 use_operand_p arg_p;
6121 ssa_op_iter i;
6122 gphi *phi = si.phi ();
6123 tree res = gimple_phi_result (phi);
6125 if (virtual_operand_p (res))
6126 continue;
6128 FOR_EACH_PHI_ARG (arg_p, phi, i, SSA_OP_USE)
6130 tree arg = USE_FROM_PTR (arg_p);
6131 if (TREE_CODE (arg) == SSA_NAME)
6132 bitmap_set_bit (live, SSA_NAME_VERSION (arg));
6135 bitmap_clear_bit (live, SSA_NAME_VERSION (res));
6139 /* Do an RPO walk over the function computing SSA name liveness
6140 on-the-fly and deciding on assert expressions to insert. */
6142 static void
6143 find_assert_locations (void)
6145 int *rpo = XNEWVEC (int, last_basic_block_for_fn (cfun));
6146 int *bb_rpo = XNEWVEC (int, last_basic_block_for_fn (cfun));
6147 int *last_rpo = XCNEWVEC (int, last_basic_block_for_fn (cfun));
6148 int rpo_cnt, i;
6150 live = XCNEWVEC (sbitmap, last_basic_block_for_fn (cfun));
6151 rpo_cnt = pre_and_rev_post_order_compute (NULL, rpo, false);
6152 for (i = 0; i < rpo_cnt; ++i)
6153 bb_rpo[rpo[i]] = i;
6155 /* Pre-seed loop latch liveness from loop header PHI nodes. Due to
6156 the order we compute liveness and insert asserts we otherwise
6157 fail to insert asserts into the loop latch. */
6158 loop_p loop;
6159 FOR_EACH_LOOP (loop, 0)
6161 i = loop->latch->index;
6162 unsigned int j = single_succ_edge (loop->latch)->dest_idx;
6163 for (gphi_iterator gsi = gsi_start_phis (loop->header);
6164 !gsi_end_p (gsi); gsi_next (&gsi))
6166 gphi *phi = gsi.phi ();
6167 if (virtual_operand_p (gimple_phi_result (phi)))
6168 continue;
6169 tree arg = gimple_phi_arg_def (phi, j);
6170 if (TREE_CODE (arg) == SSA_NAME)
6172 if (live[i] == NULL)
6174 live[i] = sbitmap_alloc (num_ssa_names);
6175 bitmap_clear (live[i]);
6177 bitmap_set_bit (live[i], SSA_NAME_VERSION (arg));
6182 for (i = rpo_cnt - 1; i >= 0; --i)
6184 basic_block bb = BASIC_BLOCK_FOR_FN (cfun, rpo[i]);
6185 edge e;
6186 edge_iterator ei;
6188 if (!live[rpo[i]])
6190 live[rpo[i]] = sbitmap_alloc (num_ssa_names);
6191 bitmap_clear (live[rpo[i]]);
6194 /* Process BB and update the live information with uses in
6195 this block. */
6196 find_assert_locations_1 (bb, live[rpo[i]]);
6198 /* Merge liveness into the predecessor blocks and free it. */
6199 if (!bitmap_empty_p (live[rpo[i]]))
6201 int pred_rpo = i;
6202 FOR_EACH_EDGE (e, ei, bb->preds)
6204 int pred = e->src->index;
6205 if ((e->flags & EDGE_DFS_BACK) || pred == ENTRY_BLOCK)
6206 continue;
6208 if (!live[pred])
6210 live[pred] = sbitmap_alloc (num_ssa_names);
6211 bitmap_clear (live[pred]);
6213 bitmap_ior (live[pred], live[pred], live[rpo[i]]);
6215 if (bb_rpo[pred] < pred_rpo)
6216 pred_rpo = bb_rpo[pred];
6219 /* Record the RPO number of the last visited block that needs
6220 live information from this block. */
6221 last_rpo[rpo[i]] = pred_rpo;
6223 else
6225 sbitmap_free (live[rpo[i]]);
6226 live[rpo[i]] = NULL;
6229 /* We can free all successors live bitmaps if all their
6230 predecessors have been visited already. */
6231 FOR_EACH_EDGE (e, ei, bb->succs)
6232 if (last_rpo[e->dest->index] == i
6233 && live[e->dest->index])
6235 sbitmap_free (live[e->dest->index]);
6236 live[e->dest->index] = NULL;
6240 XDELETEVEC (rpo);
6241 XDELETEVEC (bb_rpo);
6242 XDELETEVEC (last_rpo);
6243 for (i = 0; i < last_basic_block_for_fn (cfun); ++i)
6244 if (live[i])
6245 sbitmap_free (live[i]);
6246 XDELETEVEC (live);
6249 /* Create an ASSERT_EXPR for NAME and insert it in the location
6250 indicated by LOC. Return true if we made any edge insertions. */
6252 static bool
6253 process_assert_insertions_for (tree name, assert_locus *loc)
6255 /* Build the comparison expression NAME_i COMP_CODE VAL. */
6256 gimple *stmt;
6257 tree cond;
6258 gimple *assert_stmt;
6259 edge_iterator ei;
6260 edge e;
6262 /* If we have X <=> X do not insert an assert expr for that. */
6263 if (loc->expr == loc->val)
6264 return false;
6266 cond = build2 (loc->comp_code, boolean_type_node, loc->expr, loc->val);
6267 assert_stmt = build_assert_expr_for (cond, name);
6268 if (loc->e)
6270 /* We have been asked to insert the assertion on an edge. This
6271 is used only by COND_EXPR and SWITCH_EXPR assertions. */
6272 gcc_checking_assert (gimple_code (gsi_stmt (loc->si)) == GIMPLE_COND
6273 || (gimple_code (gsi_stmt (loc->si))
6274 == GIMPLE_SWITCH));
6276 gsi_insert_on_edge (loc->e, assert_stmt);
6277 return true;
6280 /* Otherwise, we can insert right after LOC->SI iff the
6281 statement must not be the last statement in the block. */
6282 stmt = gsi_stmt (loc->si);
6283 if (!stmt_ends_bb_p (stmt))
6285 gsi_insert_after (&loc->si, assert_stmt, GSI_SAME_STMT);
6286 return false;
6289 /* If STMT must be the last statement in BB, we can only insert new
6290 assertions on the non-abnormal edge out of BB. Note that since
6291 STMT is not control flow, there may only be one non-abnormal edge
6292 out of BB. */
6293 FOR_EACH_EDGE (e, ei, loc->bb->succs)
6294 if (!(e->flags & EDGE_ABNORMAL))
6296 gsi_insert_on_edge (e, assert_stmt);
6297 return true;
6300 gcc_unreachable ();
6304 /* Process all the insertions registered for every name N_i registered
6305 in NEED_ASSERT_FOR. The list of assertions to be inserted are
6306 found in ASSERTS_FOR[i]. */
6308 static void
6309 process_assert_insertions (void)
6311 unsigned i;
6312 bitmap_iterator bi;
6313 bool update_edges_p = false;
6314 int num_asserts = 0;
6316 if (dump_file && (dump_flags & TDF_DETAILS))
6317 dump_all_asserts (dump_file);
6319 EXECUTE_IF_SET_IN_BITMAP (need_assert_for, 0, i, bi)
6321 assert_locus *loc = asserts_for[i];
6322 gcc_assert (loc);
6324 while (loc)
6326 assert_locus *next = loc->next;
6327 update_edges_p |= process_assert_insertions_for (ssa_name (i), loc);
6328 free (loc);
6329 loc = next;
6330 num_asserts++;
6334 if (update_edges_p)
6335 gsi_commit_edge_inserts ();
6337 statistics_counter_event (cfun, "Number of ASSERT_EXPR expressions inserted",
6338 num_asserts);
6342 /* Traverse the flowgraph looking for conditional jumps to insert range
6343 expressions. These range expressions are meant to provide information
6344 to optimizations that need to reason in terms of value ranges. They
6345 will not be expanded into RTL. For instance, given:
6347 x = ...
6348 y = ...
6349 if (x < y)
6350 y = x - 2;
6351 else
6352 x = y + 3;
6354 this pass will transform the code into:
6356 x = ...
6357 y = ...
6358 if (x < y)
6360 x = ASSERT_EXPR <x, x < y>
6361 y = x - 2
6363 else
6365 y = ASSERT_EXPR <y, x >= y>
6366 x = y + 3
6369 The idea is that once copy and constant propagation have run, other
6370 optimizations will be able to determine what ranges of values can 'x'
6371 take in different paths of the code, simply by checking the reaching
6372 definition of 'x'. */
6374 static void
6375 insert_range_assertions (void)
6377 need_assert_for = BITMAP_ALLOC (NULL);
6378 asserts_for = XCNEWVEC (assert_locus *, num_ssa_names);
6380 calculate_dominance_info (CDI_DOMINATORS);
6382 find_assert_locations ();
6383 if (!bitmap_empty_p (need_assert_for))
6385 process_assert_insertions ();
6386 update_ssa (TODO_update_ssa_no_phi);
6389 if (dump_file && (dump_flags & TDF_DETAILS))
6391 fprintf (dump_file, "\nSSA form after inserting ASSERT_EXPRs\n");
6392 dump_function_to_file (current_function_decl, dump_file, dump_flags);
6395 free (asserts_for);
6396 BITMAP_FREE (need_assert_for);
6399 /* Checks one ARRAY_REF in REF, located at LOCUS. Ignores flexible arrays
6400 and "struct" hacks. If VRP can determine that the
6401 array subscript is a constant, check if it is outside valid
6402 range. If the array subscript is a RANGE, warn if it is
6403 non-overlapping with valid range.
6404 IGNORE_OFF_BY_ONE is true if the ARRAY_REF is inside a ADDR_EXPR. */
6406 static void
6407 check_array_ref (location_t location, tree ref, bool ignore_off_by_one)
6409 value_range *vr = NULL;
6410 tree low_sub, up_sub;
6411 tree low_bound, up_bound, up_bound_p1;
6413 if (TREE_NO_WARNING (ref))
6414 return;
6416 low_sub = up_sub = TREE_OPERAND (ref, 1);
6417 up_bound = array_ref_up_bound (ref);
6419 /* Can not check flexible arrays. */
6420 if (!up_bound
6421 || TREE_CODE (up_bound) != INTEGER_CST)
6422 return;
6424 /* Accesses to trailing arrays via pointers may access storage
6425 beyond the types array bounds. */
6426 if (warn_array_bounds < 2
6427 && array_at_struct_end_p (ref))
6428 return;
6430 low_bound = array_ref_low_bound (ref);
6431 up_bound_p1 = int_const_binop (PLUS_EXPR, up_bound,
6432 build_int_cst (TREE_TYPE (up_bound), 1));
6434 /* Empty array. */
6435 if (tree_int_cst_equal (low_bound, up_bound_p1))
6437 warning_at (location, OPT_Warray_bounds,
6438 "array subscript is above array bounds");
6439 TREE_NO_WARNING (ref) = 1;
6442 if (TREE_CODE (low_sub) == SSA_NAME)
6444 vr = get_value_range (low_sub);
6445 if (vr->type == VR_RANGE || vr->type == VR_ANTI_RANGE)
6447 low_sub = vr->type == VR_RANGE ? vr->max : vr->min;
6448 up_sub = vr->type == VR_RANGE ? vr->min : vr->max;
6452 if (vr && vr->type == VR_ANTI_RANGE)
6454 if (TREE_CODE (up_sub) == INTEGER_CST
6455 && (ignore_off_by_one
6456 ? tree_int_cst_lt (up_bound, up_sub)
6457 : tree_int_cst_le (up_bound, up_sub))
6458 && TREE_CODE (low_sub) == INTEGER_CST
6459 && tree_int_cst_le (low_sub, low_bound))
6461 warning_at (location, OPT_Warray_bounds,
6462 "array subscript is outside array bounds");
6463 TREE_NO_WARNING (ref) = 1;
6466 else if (TREE_CODE (up_sub) == INTEGER_CST
6467 && (ignore_off_by_one
6468 ? !tree_int_cst_le (up_sub, up_bound_p1)
6469 : !tree_int_cst_le (up_sub, up_bound)))
6471 if (dump_file && (dump_flags & TDF_DETAILS))
6473 fprintf (dump_file, "Array bound warning for ");
6474 dump_generic_expr (MSG_NOTE, TDF_SLIM, ref);
6475 fprintf (dump_file, "\n");
6477 warning_at (location, OPT_Warray_bounds,
6478 "array subscript is above array bounds");
6479 TREE_NO_WARNING (ref) = 1;
6481 else if (TREE_CODE (low_sub) == INTEGER_CST
6482 && tree_int_cst_lt (low_sub, low_bound))
6484 if (dump_file && (dump_flags & TDF_DETAILS))
6486 fprintf (dump_file, "Array bound warning for ");
6487 dump_generic_expr (MSG_NOTE, TDF_SLIM, ref);
6488 fprintf (dump_file, "\n");
6490 warning_at (location, OPT_Warray_bounds,
6491 "array subscript is below array bounds");
6492 TREE_NO_WARNING (ref) = 1;
6496 /* Searches if the expr T, located at LOCATION computes
6497 address of an ARRAY_REF, and call check_array_ref on it. */
6499 static void
6500 search_for_addr_array (tree t, location_t location)
6502 /* Check each ARRAY_REFs in the reference chain. */
6505 if (TREE_CODE (t) == ARRAY_REF)
6506 check_array_ref (location, t, true /*ignore_off_by_one*/);
6508 t = TREE_OPERAND (t, 0);
6510 while (handled_component_p (t));
6512 if (TREE_CODE (t) == MEM_REF
6513 && TREE_CODE (TREE_OPERAND (t, 0)) == ADDR_EXPR
6514 && !TREE_NO_WARNING (t))
6516 tree tem = TREE_OPERAND (TREE_OPERAND (t, 0), 0);
6517 tree low_bound, up_bound, el_sz;
6518 offset_int idx;
6519 if (TREE_CODE (TREE_TYPE (tem)) != ARRAY_TYPE
6520 || TREE_CODE (TREE_TYPE (TREE_TYPE (tem))) == ARRAY_TYPE
6521 || !TYPE_DOMAIN (TREE_TYPE (tem)))
6522 return;
6524 low_bound = TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (tem)));
6525 up_bound = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (tem)));
6526 el_sz = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (tem)));
6527 if (!low_bound
6528 || TREE_CODE (low_bound) != INTEGER_CST
6529 || !up_bound
6530 || TREE_CODE (up_bound) != INTEGER_CST
6531 || !el_sz
6532 || TREE_CODE (el_sz) != INTEGER_CST)
6533 return;
6535 idx = mem_ref_offset (t);
6536 idx = wi::sdiv_trunc (idx, wi::to_offset (el_sz));
6537 if (idx < 0)
6539 if (dump_file && (dump_flags & TDF_DETAILS))
6541 fprintf (dump_file, "Array bound warning for ");
6542 dump_generic_expr (MSG_NOTE, TDF_SLIM, t);
6543 fprintf (dump_file, "\n");
6545 warning_at (location, OPT_Warray_bounds,
6546 "array subscript is below array bounds");
6547 TREE_NO_WARNING (t) = 1;
6549 else if (idx > (wi::to_offset (up_bound)
6550 - wi::to_offset (low_bound) + 1))
6552 if (dump_file && (dump_flags & TDF_DETAILS))
6554 fprintf (dump_file, "Array bound warning for ");
6555 dump_generic_expr (MSG_NOTE, TDF_SLIM, t);
6556 fprintf (dump_file, "\n");
6558 warning_at (location, OPT_Warray_bounds,
6559 "array subscript is above array bounds");
6560 TREE_NO_WARNING (t) = 1;
6565 /* walk_tree() callback that checks if *TP is
6566 an ARRAY_REF inside an ADDR_EXPR (in which an array
6567 subscript one outside the valid range is allowed). Call
6568 check_array_ref for each ARRAY_REF found. The location is
6569 passed in DATA. */
6571 static tree
6572 check_array_bounds (tree *tp, int *walk_subtree, void *data)
6574 tree t = *tp;
6575 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
6576 location_t location;
6578 if (EXPR_HAS_LOCATION (t))
6579 location = EXPR_LOCATION (t);
6580 else
6582 location_t *locp = (location_t *) wi->info;
6583 location = *locp;
6586 *walk_subtree = TRUE;
6588 if (TREE_CODE (t) == ARRAY_REF)
6589 check_array_ref (location, t, false /*ignore_off_by_one*/);
6591 else if (TREE_CODE (t) == ADDR_EXPR)
6593 search_for_addr_array (t, location);
6594 *walk_subtree = FALSE;
6597 return NULL_TREE;
6600 /* Walk over all statements of all reachable BBs and call check_array_bounds
6601 on them. */
6603 static void
6604 check_all_array_refs (void)
6606 basic_block bb;
6607 gimple_stmt_iterator si;
6609 FOR_EACH_BB_FN (bb, cfun)
6611 edge_iterator ei;
6612 edge e;
6613 bool executable = false;
6615 /* Skip blocks that were found to be unreachable. */
6616 FOR_EACH_EDGE (e, ei, bb->preds)
6617 executable |= !!(e->flags & EDGE_EXECUTABLE);
6618 if (!executable)
6619 continue;
6621 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
6623 gimple *stmt = gsi_stmt (si);
6624 struct walk_stmt_info wi;
6625 if (!gimple_has_location (stmt)
6626 || is_gimple_debug (stmt))
6627 continue;
6629 memset (&wi, 0, sizeof (wi));
6631 location_t loc = gimple_location (stmt);
6632 wi.info = &loc;
6634 walk_gimple_op (gsi_stmt (si),
6635 check_array_bounds,
6636 &wi);
6641 /* Return true if all imm uses of VAR are either in STMT, or
6642 feed (optionally through a chain of single imm uses) GIMPLE_COND
6643 in basic block COND_BB. */
6645 static bool
6646 all_imm_uses_in_stmt_or_feed_cond (tree var, gimple *stmt, basic_block cond_bb)
6648 use_operand_p use_p, use2_p;
6649 imm_use_iterator iter;
6651 FOR_EACH_IMM_USE_FAST (use_p, iter, var)
6652 if (USE_STMT (use_p) != stmt)
6654 gimple *use_stmt = USE_STMT (use_p), *use_stmt2;
6655 if (is_gimple_debug (use_stmt))
6656 continue;
6657 while (is_gimple_assign (use_stmt)
6658 && TREE_CODE (gimple_assign_lhs (use_stmt)) == SSA_NAME
6659 && single_imm_use (gimple_assign_lhs (use_stmt),
6660 &use2_p, &use_stmt2))
6661 use_stmt = use_stmt2;
6662 if (gimple_code (use_stmt) != GIMPLE_COND
6663 || gimple_bb (use_stmt) != cond_bb)
6664 return false;
6666 return true;
6669 /* Handle
6670 _4 = x_3 & 31;
6671 if (_4 != 0)
6672 goto <bb 6>;
6673 else
6674 goto <bb 7>;
6675 <bb 6>:
6676 __builtin_unreachable ();
6677 <bb 7>:
6678 x_5 = ASSERT_EXPR <x_3, ...>;
6679 If x_3 has no other immediate uses (checked by caller),
6680 var is the x_3 var from ASSERT_EXPR, we can clear low 5 bits
6681 from the non-zero bitmask. */
6683 static void
6684 maybe_set_nonzero_bits (basic_block bb, tree var)
6686 edge e = single_pred_edge (bb);
6687 basic_block cond_bb = e->src;
6688 gimple *stmt = last_stmt (cond_bb);
6689 tree cst;
6691 if (stmt == NULL
6692 || gimple_code (stmt) != GIMPLE_COND
6693 || gimple_cond_code (stmt) != ((e->flags & EDGE_TRUE_VALUE)
6694 ? EQ_EXPR : NE_EXPR)
6695 || TREE_CODE (gimple_cond_lhs (stmt)) != SSA_NAME
6696 || !integer_zerop (gimple_cond_rhs (stmt)))
6697 return;
6699 stmt = SSA_NAME_DEF_STMT (gimple_cond_lhs (stmt));
6700 if (!is_gimple_assign (stmt)
6701 || gimple_assign_rhs_code (stmt) != BIT_AND_EXPR
6702 || TREE_CODE (gimple_assign_rhs2 (stmt)) != INTEGER_CST)
6703 return;
6704 if (gimple_assign_rhs1 (stmt) != var)
6706 gimple *stmt2;
6708 if (TREE_CODE (gimple_assign_rhs1 (stmt)) != SSA_NAME)
6709 return;
6710 stmt2 = SSA_NAME_DEF_STMT (gimple_assign_rhs1 (stmt));
6711 if (!gimple_assign_cast_p (stmt2)
6712 || gimple_assign_rhs1 (stmt2) != var
6713 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (stmt2))
6714 || (TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (stmt)))
6715 != TYPE_PRECISION (TREE_TYPE (var))))
6716 return;
6718 cst = gimple_assign_rhs2 (stmt);
6719 set_nonzero_bits (var, wi::bit_and_not (get_nonzero_bits (var), cst));
6722 /* Convert range assertion expressions into the implied copies and
6723 copy propagate away the copies. Doing the trivial copy propagation
6724 here avoids the need to run the full copy propagation pass after
6725 VRP.
6727 FIXME, this will eventually lead to copy propagation removing the
6728 names that had useful range information attached to them. For
6729 instance, if we had the assertion N_i = ASSERT_EXPR <N_j, N_j > 3>,
6730 then N_i will have the range [3, +INF].
6732 However, by converting the assertion into the implied copy
6733 operation N_i = N_j, we will then copy-propagate N_j into the uses
6734 of N_i and lose the range information. We may want to hold on to
6735 ASSERT_EXPRs a little while longer as the ranges could be used in
6736 things like jump threading.
6738 The problem with keeping ASSERT_EXPRs around is that passes after
6739 VRP need to handle them appropriately.
6741 Another approach would be to make the range information a first
6742 class property of the SSA_NAME so that it can be queried from
6743 any pass. This is made somewhat more complex by the need for
6744 multiple ranges to be associated with one SSA_NAME. */
6746 static void
6747 remove_range_assertions (void)
6749 basic_block bb;
6750 gimple_stmt_iterator si;
6751 /* 1 if looking at ASSERT_EXPRs immediately at the beginning of
6752 a basic block preceeded by GIMPLE_COND branching to it and
6753 __builtin_trap, -1 if not yet checked, 0 otherwise. */
6754 int is_unreachable;
6756 /* Note that the BSI iterator bump happens at the bottom of the
6757 loop and no bump is necessary if we're removing the statement
6758 referenced by the current BSI. */
6759 FOR_EACH_BB_FN (bb, cfun)
6760 for (si = gsi_after_labels (bb), is_unreachable = -1; !gsi_end_p (si);)
6762 gimple *stmt = gsi_stmt (si);
6763 gimple *use_stmt;
6765 if (is_gimple_assign (stmt)
6766 && gimple_assign_rhs_code (stmt) == ASSERT_EXPR)
6768 tree lhs = gimple_assign_lhs (stmt);
6769 tree rhs = gimple_assign_rhs1 (stmt);
6770 tree var;
6771 use_operand_p use_p;
6772 imm_use_iterator iter;
6774 var = ASSERT_EXPR_VAR (rhs);
6775 gcc_assert (TREE_CODE (var) == SSA_NAME);
6777 if (!POINTER_TYPE_P (TREE_TYPE (lhs))
6778 && SSA_NAME_RANGE_INFO (lhs))
6780 if (is_unreachable == -1)
6782 is_unreachable = 0;
6783 if (single_pred_p (bb)
6784 && assert_unreachable_fallthru_edge_p
6785 (single_pred_edge (bb)))
6786 is_unreachable = 1;
6788 /* Handle
6789 if (x_7 >= 10 && x_7 < 20)
6790 __builtin_unreachable ();
6791 x_8 = ASSERT_EXPR <x_7, ...>;
6792 if the only uses of x_7 are in the ASSERT_EXPR and
6793 in the condition. In that case, we can copy the
6794 range info from x_8 computed in this pass also
6795 for x_7. */
6796 if (is_unreachable
6797 && all_imm_uses_in_stmt_or_feed_cond (var, stmt,
6798 single_pred (bb)))
6800 set_range_info (var, SSA_NAME_RANGE_TYPE (lhs),
6801 SSA_NAME_RANGE_INFO (lhs)->get_min (),
6802 SSA_NAME_RANGE_INFO (lhs)->get_max ());
6803 maybe_set_nonzero_bits (bb, var);
6807 /* Propagate the RHS into every use of the LHS. */
6808 FOR_EACH_IMM_USE_STMT (use_stmt, iter, lhs)
6809 FOR_EACH_IMM_USE_ON_STMT (use_p, iter)
6810 SET_USE (use_p, var);
6812 /* And finally, remove the copy, it is not needed. */
6813 gsi_remove (&si, true);
6814 release_defs (stmt);
6816 else
6818 if (!is_gimple_debug (gsi_stmt (si)))
6819 is_unreachable = 0;
6820 gsi_next (&si);
6826 /* Return true if STMT is interesting for VRP. */
6828 static bool
6829 stmt_interesting_for_vrp (gimple *stmt)
6831 if (gimple_code (stmt) == GIMPLE_PHI)
6833 tree res = gimple_phi_result (stmt);
6834 return (!virtual_operand_p (res)
6835 && (INTEGRAL_TYPE_P (TREE_TYPE (res))
6836 || POINTER_TYPE_P (TREE_TYPE (res))));
6838 else if (is_gimple_assign (stmt) || is_gimple_call (stmt))
6840 tree lhs = gimple_get_lhs (stmt);
6842 /* In general, assignments with virtual operands are not useful
6843 for deriving ranges, with the obvious exception of calls to
6844 builtin functions. */
6845 if (lhs && TREE_CODE (lhs) == SSA_NAME
6846 && (INTEGRAL_TYPE_P (TREE_TYPE (lhs))
6847 || POINTER_TYPE_P (TREE_TYPE (lhs)))
6848 && (is_gimple_call (stmt)
6849 || !gimple_vuse (stmt)))
6850 return true;
6851 else if (is_gimple_call (stmt) && gimple_call_internal_p (stmt))
6852 switch (gimple_call_internal_fn (stmt))
6854 case IFN_ADD_OVERFLOW:
6855 case IFN_SUB_OVERFLOW:
6856 case IFN_MUL_OVERFLOW:
6857 /* These internal calls return _Complex integer type,
6858 but are interesting to VRP nevertheless. */
6859 if (lhs && TREE_CODE (lhs) == SSA_NAME)
6860 return true;
6861 break;
6862 default:
6863 break;
6866 else if (gimple_code (stmt) == GIMPLE_COND
6867 || gimple_code (stmt) == GIMPLE_SWITCH)
6868 return true;
6870 return false;
6874 /* Initialize local data structures for VRP. */
6876 static void
6877 vrp_initialize (void)
6879 basic_block bb;
6881 values_propagated = false;
6882 num_vr_values = num_ssa_names;
6883 vr_value = XCNEWVEC (value_range *, num_vr_values);
6884 vr_phi_edge_counts = XCNEWVEC (int, num_ssa_names);
6886 FOR_EACH_BB_FN (bb, cfun)
6888 for (gphi_iterator si = gsi_start_phis (bb); !gsi_end_p (si);
6889 gsi_next (&si))
6891 gphi *phi = si.phi ();
6892 if (!stmt_interesting_for_vrp (phi))
6894 tree lhs = PHI_RESULT (phi);
6895 set_value_range_to_varying (get_value_range (lhs));
6896 prop_set_simulate_again (phi, false);
6898 else
6899 prop_set_simulate_again (phi, true);
6902 for (gimple_stmt_iterator si = gsi_start_bb (bb); !gsi_end_p (si);
6903 gsi_next (&si))
6905 gimple *stmt = gsi_stmt (si);
6907 /* If the statement is a control insn, then we do not
6908 want to avoid simulating the statement once. Failure
6909 to do so means that those edges will never get added. */
6910 if (stmt_ends_bb_p (stmt))
6911 prop_set_simulate_again (stmt, true);
6912 else if (!stmt_interesting_for_vrp (stmt))
6914 ssa_op_iter i;
6915 tree def;
6916 FOR_EACH_SSA_TREE_OPERAND (def, stmt, i, SSA_OP_DEF)
6917 set_value_range_to_varying (get_value_range (def));
6918 prop_set_simulate_again (stmt, false);
6920 else
6921 prop_set_simulate_again (stmt, true);
6926 /* Return the singleton value-range for NAME or NAME. */
6928 static inline tree
6929 vrp_valueize (tree name)
6931 if (TREE_CODE (name) == SSA_NAME)
6933 value_range *vr = get_value_range (name);
6934 if (vr->type == VR_RANGE
6935 && (vr->min == vr->max
6936 || operand_equal_p (vr->min, vr->max, 0)))
6937 return vr->min;
6939 return name;
6942 /* Return the singleton value-range for NAME if that is a constant
6943 but signal to not follow SSA edges. */
6945 static inline tree
6946 vrp_valueize_1 (tree name)
6948 if (TREE_CODE (name) == SSA_NAME)
6950 /* If the definition may be simulated again we cannot follow
6951 this SSA edge as the SSA propagator does not necessarily
6952 re-visit the use. */
6953 gimple *def_stmt = SSA_NAME_DEF_STMT (name);
6954 if (!gimple_nop_p (def_stmt)
6955 && prop_simulate_again_p (def_stmt))
6956 return NULL_TREE;
6957 value_range *vr = get_value_range (name);
6958 if (range_int_cst_singleton_p (vr))
6959 return vr->min;
6961 return name;
6964 /* Visit assignment STMT. If it produces an interesting range, record
6965 the SSA name in *OUTPUT_P. */
6967 static enum ssa_prop_result
6968 vrp_visit_assignment_or_call (gimple *stmt, tree *output_p)
6970 tree def, lhs;
6971 ssa_op_iter iter;
6972 enum gimple_code code = gimple_code (stmt);
6973 lhs = gimple_get_lhs (stmt);
6975 /* We only keep track of ranges in integral and pointer types. */
6976 if (TREE_CODE (lhs) == SSA_NAME
6977 && ((INTEGRAL_TYPE_P (TREE_TYPE (lhs))
6978 /* It is valid to have NULL MIN/MAX values on a type. See
6979 build_range_type. */
6980 && TYPE_MIN_VALUE (TREE_TYPE (lhs))
6981 && TYPE_MAX_VALUE (TREE_TYPE (lhs)))
6982 || POINTER_TYPE_P (TREE_TYPE (lhs))))
6984 value_range new_vr = VR_INITIALIZER;
6986 /* Try folding the statement to a constant first. */
6987 tree tem = gimple_fold_stmt_to_constant_1 (stmt, vrp_valueize,
6988 vrp_valueize_1);
6989 if (tem && is_gimple_min_invariant (tem))
6990 set_value_range_to_value (&new_vr, tem, NULL);
6991 /* Then dispatch to value-range extracting functions. */
6992 else if (code == GIMPLE_CALL)
6993 extract_range_basic (&new_vr, stmt);
6994 else
6995 extract_range_from_assignment (&new_vr, as_a <gassign *> (stmt));
6997 if (update_value_range (lhs, &new_vr))
6999 *output_p = lhs;
7001 if (dump_file && (dump_flags & TDF_DETAILS))
7003 fprintf (dump_file, "Found new range for ");
7004 print_generic_expr (dump_file, lhs, 0);
7005 fprintf (dump_file, ": ");
7006 dump_value_range (dump_file, &new_vr);
7007 fprintf (dump_file, "\n");
7010 if (new_vr.type == VR_VARYING)
7011 return SSA_PROP_VARYING;
7013 return SSA_PROP_INTERESTING;
7016 return SSA_PROP_NOT_INTERESTING;
7018 else if (is_gimple_call (stmt) && gimple_call_internal_p (stmt))
7019 switch (gimple_call_internal_fn (stmt))
7021 case IFN_ADD_OVERFLOW:
7022 case IFN_SUB_OVERFLOW:
7023 case IFN_MUL_OVERFLOW:
7024 /* These internal calls return _Complex integer type,
7025 which VRP does not track, but the immediate uses
7026 thereof might be interesting. */
7027 if (lhs && TREE_CODE (lhs) == SSA_NAME)
7029 imm_use_iterator iter;
7030 use_operand_p use_p;
7031 enum ssa_prop_result res = SSA_PROP_VARYING;
7033 set_value_range_to_varying (get_value_range (lhs));
7035 FOR_EACH_IMM_USE_FAST (use_p, iter, lhs)
7037 gimple *use_stmt = USE_STMT (use_p);
7038 if (!is_gimple_assign (use_stmt))
7039 continue;
7040 enum tree_code rhs_code = gimple_assign_rhs_code (use_stmt);
7041 if (rhs_code != REALPART_EXPR && rhs_code != IMAGPART_EXPR)
7042 continue;
7043 tree rhs1 = gimple_assign_rhs1 (use_stmt);
7044 tree use_lhs = gimple_assign_lhs (use_stmt);
7045 if (TREE_CODE (rhs1) != rhs_code
7046 || TREE_OPERAND (rhs1, 0) != lhs
7047 || TREE_CODE (use_lhs) != SSA_NAME
7048 || !stmt_interesting_for_vrp (use_stmt)
7049 || (!INTEGRAL_TYPE_P (TREE_TYPE (use_lhs))
7050 || !TYPE_MIN_VALUE (TREE_TYPE (use_lhs))
7051 || !TYPE_MAX_VALUE (TREE_TYPE (use_lhs))))
7052 continue;
7054 /* If there is a change in the value range for any of the
7055 REALPART_EXPR/IMAGPART_EXPR immediate uses, return
7056 SSA_PROP_INTERESTING. If there are any REALPART_EXPR
7057 or IMAGPART_EXPR immediate uses, but none of them have
7058 a change in their value ranges, return
7059 SSA_PROP_NOT_INTERESTING. If there are no
7060 {REAL,IMAG}PART_EXPR uses at all,
7061 return SSA_PROP_VARYING. */
7062 value_range new_vr = VR_INITIALIZER;
7063 extract_range_basic (&new_vr, use_stmt);
7064 value_range *old_vr = get_value_range (use_lhs);
7065 if (old_vr->type != new_vr.type
7066 || !vrp_operand_equal_p (old_vr->min, new_vr.min)
7067 || !vrp_operand_equal_p (old_vr->max, new_vr.max)
7068 || !vrp_bitmap_equal_p (old_vr->equiv, new_vr.equiv))
7069 res = SSA_PROP_INTERESTING;
7070 else
7071 res = SSA_PROP_NOT_INTERESTING;
7072 BITMAP_FREE (new_vr.equiv);
7073 if (res == SSA_PROP_INTERESTING)
7075 *output_p = lhs;
7076 return res;
7080 return res;
7082 break;
7083 default:
7084 break;
7087 /* Every other statement produces no useful ranges. */
7088 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_DEF)
7089 set_value_range_to_varying (get_value_range (def));
7091 return SSA_PROP_VARYING;
7094 /* Helper that gets the value range of the SSA_NAME with version I
7095 or a symbolic range containing the SSA_NAME only if the value range
7096 is varying or undefined. */
7098 static inline value_range
7099 get_vr_for_comparison (int i)
7101 value_range vr = *get_value_range (ssa_name (i));
7103 /* If name N_i does not have a valid range, use N_i as its own
7104 range. This allows us to compare against names that may
7105 have N_i in their ranges. */
7106 if (vr.type == VR_VARYING || vr.type == VR_UNDEFINED)
7108 vr.type = VR_RANGE;
7109 vr.min = ssa_name (i);
7110 vr.max = ssa_name (i);
7113 return vr;
7116 /* Compare all the value ranges for names equivalent to VAR with VAL
7117 using comparison code COMP. Return the same value returned by
7118 compare_range_with_value, including the setting of
7119 *STRICT_OVERFLOW_P. */
7121 static tree
7122 compare_name_with_value (enum tree_code comp, tree var, tree val,
7123 bool *strict_overflow_p, bool use_equiv_p)
7125 bitmap_iterator bi;
7126 unsigned i;
7127 bitmap e;
7128 tree retval, t;
7129 int used_strict_overflow;
7130 bool sop;
7131 value_range equiv_vr;
7133 /* Get the set of equivalences for VAR. */
7134 e = get_value_range (var)->equiv;
7136 /* Start at -1. Set it to 0 if we do a comparison without relying
7137 on overflow, or 1 if all comparisons rely on overflow. */
7138 used_strict_overflow = -1;
7140 /* Compare vars' value range with val. */
7141 equiv_vr = get_vr_for_comparison (SSA_NAME_VERSION (var));
7142 sop = false;
7143 retval = compare_range_with_value (comp, &equiv_vr, val, &sop);
7144 if (retval)
7145 used_strict_overflow = sop ? 1 : 0;
7147 /* If the equiv set is empty we have done all work we need to do. */
7148 if (e == NULL)
7150 if (retval
7151 && used_strict_overflow > 0)
7152 *strict_overflow_p = true;
7153 return retval;
7156 EXECUTE_IF_SET_IN_BITMAP (e, 0, i, bi)
7158 if (! use_equiv_p
7159 && ! SSA_NAME_IS_DEFAULT_DEF (ssa_name (i))
7160 && prop_simulate_again_p (SSA_NAME_DEF_STMT (ssa_name (i))))
7161 continue;
7163 equiv_vr = get_vr_for_comparison (i);
7164 sop = false;
7165 t = compare_range_with_value (comp, &equiv_vr, val, &sop);
7166 if (t)
7168 /* If we get different answers from different members
7169 of the equivalence set this check must be in a dead
7170 code region. Folding it to a trap representation
7171 would be correct here. For now just return don't-know. */
7172 if (retval != NULL
7173 && t != retval)
7175 retval = NULL_TREE;
7176 break;
7178 retval = t;
7180 if (!sop)
7181 used_strict_overflow = 0;
7182 else if (used_strict_overflow < 0)
7183 used_strict_overflow = 1;
7187 if (retval
7188 && used_strict_overflow > 0)
7189 *strict_overflow_p = true;
7191 return retval;
7195 /* Given a comparison code COMP and names N1 and N2, compare all the
7196 ranges equivalent to N1 against all the ranges equivalent to N2
7197 to determine the value of N1 COMP N2. Return the same value
7198 returned by compare_ranges. Set *STRICT_OVERFLOW_P to indicate
7199 whether we relied on an overflow infinity in the comparison. */
7202 static tree
7203 compare_names (enum tree_code comp, tree n1, tree n2,
7204 bool *strict_overflow_p)
7206 tree t, retval;
7207 bitmap e1, e2;
7208 bitmap_iterator bi1, bi2;
7209 unsigned i1, i2;
7210 int used_strict_overflow;
7211 static bitmap_obstack *s_obstack = NULL;
7212 static bitmap s_e1 = NULL, s_e2 = NULL;
7214 /* Compare the ranges of every name equivalent to N1 against the
7215 ranges of every name equivalent to N2. */
7216 e1 = get_value_range (n1)->equiv;
7217 e2 = get_value_range (n2)->equiv;
7219 /* Use the fake bitmaps if e1 or e2 are not available. */
7220 if (s_obstack == NULL)
7222 s_obstack = XNEW (bitmap_obstack);
7223 bitmap_obstack_initialize (s_obstack);
7224 s_e1 = BITMAP_ALLOC (s_obstack);
7225 s_e2 = BITMAP_ALLOC (s_obstack);
7227 if (e1 == NULL)
7228 e1 = s_e1;
7229 if (e2 == NULL)
7230 e2 = s_e2;
7232 /* Add N1 and N2 to their own set of equivalences to avoid
7233 duplicating the body of the loop just to check N1 and N2
7234 ranges. */
7235 bitmap_set_bit (e1, SSA_NAME_VERSION (n1));
7236 bitmap_set_bit (e2, SSA_NAME_VERSION (n2));
7238 /* If the equivalence sets have a common intersection, then the two
7239 names can be compared without checking their ranges. */
7240 if (bitmap_intersect_p (e1, e2))
7242 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
7243 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
7245 return (comp == EQ_EXPR || comp == GE_EXPR || comp == LE_EXPR)
7246 ? boolean_true_node
7247 : boolean_false_node;
7250 /* Start at -1. Set it to 0 if we do a comparison without relying
7251 on overflow, or 1 if all comparisons rely on overflow. */
7252 used_strict_overflow = -1;
7254 /* Otherwise, compare all the equivalent ranges. First, add N1 and
7255 N2 to their own set of equivalences to avoid duplicating the body
7256 of the loop just to check N1 and N2 ranges. */
7257 EXECUTE_IF_SET_IN_BITMAP (e1, 0, i1, bi1)
7259 value_range vr1 = get_vr_for_comparison (i1);
7261 t = retval = NULL_TREE;
7262 EXECUTE_IF_SET_IN_BITMAP (e2, 0, i2, bi2)
7264 bool sop = false;
7266 value_range vr2 = get_vr_for_comparison (i2);
7268 t = compare_ranges (comp, &vr1, &vr2, &sop);
7269 if (t)
7271 /* If we get different answers from different members
7272 of the equivalence set this check must be in a dead
7273 code region. Folding it to a trap representation
7274 would be correct here. For now just return don't-know. */
7275 if (retval != NULL
7276 && t != retval)
7278 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
7279 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
7280 return NULL_TREE;
7282 retval = t;
7284 if (!sop)
7285 used_strict_overflow = 0;
7286 else if (used_strict_overflow < 0)
7287 used_strict_overflow = 1;
7291 if (retval)
7293 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
7294 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
7295 if (used_strict_overflow > 0)
7296 *strict_overflow_p = true;
7297 return retval;
7301 /* None of the equivalent ranges are useful in computing this
7302 comparison. */
7303 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
7304 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
7305 return NULL_TREE;
7308 /* Helper function for vrp_evaluate_conditional_warnv & other
7309 optimizers. */
7311 static tree
7312 vrp_evaluate_conditional_warnv_with_ops_using_ranges (enum tree_code code,
7313 tree op0, tree op1,
7314 bool * strict_overflow_p)
7316 value_range *vr0, *vr1;
7318 vr0 = (TREE_CODE (op0) == SSA_NAME) ? get_value_range (op0) : NULL;
7319 vr1 = (TREE_CODE (op1) == SSA_NAME) ? get_value_range (op1) : NULL;
7321 tree res = NULL_TREE;
7322 if (vr0 && vr1)
7323 res = compare_ranges (code, vr0, vr1, strict_overflow_p);
7324 if (!res && vr0)
7325 res = compare_range_with_value (code, vr0, op1, strict_overflow_p);
7326 if (!res && vr1)
7327 res = (compare_range_with_value
7328 (swap_tree_comparison (code), vr1, op0, strict_overflow_p));
7329 return res;
7332 /* Helper function for vrp_evaluate_conditional_warnv. */
7334 static tree
7335 vrp_evaluate_conditional_warnv_with_ops (enum tree_code code, tree op0,
7336 tree op1, bool use_equiv_p,
7337 bool *strict_overflow_p, bool *only_ranges)
7339 tree ret;
7340 if (only_ranges)
7341 *only_ranges = true;
7343 /* We only deal with integral and pointer types. */
7344 if (!INTEGRAL_TYPE_P (TREE_TYPE (op0))
7345 && !POINTER_TYPE_P (TREE_TYPE (op0)))
7346 return NULL_TREE;
7348 if ((ret = vrp_evaluate_conditional_warnv_with_ops_using_ranges
7349 (code, op0, op1, strict_overflow_p)))
7350 return ret;
7351 if (only_ranges)
7352 *only_ranges = false;
7353 /* Do not use compare_names during propagation, it's quadratic. */
7354 if (TREE_CODE (op0) == SSA_NAME && TREE_CODE (op1) == SSA_NAME
7355 && use_equiv_p)
7356 return compare_names (code, op0, op1, strict_overflow_p);
7357 else if (TREE_CODE (op0) == SSA_NAME)
7358 return compare_name_with_value (code, op0, op1,
7359 strict_overflow_p, use_equiv_p);
7360 else if (TREE_CODE (op1) == SSA_NAME)
7361 return compare_name_with_value (swap_tree_comparison (code), op1, op0,
7362 strict_overflow_p, use_equiv_p);
7363 return NULL_TREE;
7366 /* Given (CODE OP0 OP1) within STMT, try to simplify it based on value range
7367 information. Return NULL if the conditional can not be evaluated.
7368 The ranges of all the names equivalent with the operands in COND
7369 will be used when trying to compute the value. If the result is
7370 based on undefined signed overflow, issue a warning if
7371 appropriate. */
7373 static tree
7374 vrp_evaluate_conditional (tree_code code, tree op0, tree op1, gimple *stmt)
7376 bool sop;
7377 tree ret;
7378 bool only_ranges;
7380 /* Some passes and foldings leak constants with overflow flag set
7381 into the IL. Avoid doing wrong things with these and bail out. */
7382 if ((TREE_CODE (op0) == INTEGER_CST
7383 && TREE_OVERFLOW (op0))
7384 || (TREE_CODE (op1) == INTEGER_CST
7385 && TREE_OVERFLOW (op1)))
7386 return NULL_TREE;
7388 sop = false;
7389 ret = vrp_evaluate_conditional_warnv_with_ops (code, op0, op1, true, &sop,
7390 &only_ranges);
7392 if (ret && sop)
7394 enum warn_strict_overflow_code wc;
7395 const char* warnmsg;
7397 if (is_gimple_min_invariant (ret))
7399 wc = WARN_STRICT_OVERFLOW_CONDITIONAL;
7400 warnmsg = G_("assuming signed overflow does not occur when "
7401 "simplifying conditional to constant");
7403 else
7405 wc = WARN_STRICT_OVERFLOW_COMPARISON;
7406 warnmsg = G_("assuming signed overflow does not occur when "
7407 "simplifying conditional");
7410 if (issue_strict_overflow_warning (wc))
7412 location_t location;
7414 if (!gimple_has_location (stmt))
7415 location = input_location;
7416 else
7417 location = gimple_location (stmt);
7418 warning_at (location, OPT_Wstrict_overflow, "%s", warnmsg);
7422 if (warn_type_limits
7423 && ret && only_ranges
7424 && TREE_CODE_CLASS (code) == tcc_comparison
7425 && TREE_CODE (op0) == SSA_NAME)
7427 /* If the comparison is being folded and the operand on the LHS
7428 is being compared against a constant value that is outside of
7429 the natural range of OP0's type, then the predicate will
7430 always fold regardless of the value of OP0. If -Wtype-limits
7431 was specified, emit a warning. */
7432 tree type = TREE_TYPE (op0);
7433 value_range *vr0 = get_value_range (op0);
7435 if (vr0->type == VR_RANGE
7436 && INTEGRAL_TYPE_P (type)
7437 && vrp_val_is_min (vr0->min)
7438 && vrp_val_is_max (vr0->max)
7439 && is_gimple_min_invariant (op1))
7441 location_t location;
7443 if (!gimple_has_location (stmt))
7444 location = input_location;
7445 else
7446 location = gimple_location (stmt);
7448 warning_at (location, OPT_Wtype_limits,
7449 integer_zerop (ret)
7450 ? G_("comparison always false "
7451 "due to limited range of data type")
7452 : G_("comparison always true "
7453 "due to limited range of data type"));
7457 return ret;
7461 /* Visit conditional statement STMT. If we can determine which edge
7462 will be taken out of STMT's basic block, record it in
7463 *TAKEN_EDGE_P and return SSA_PROP_INTERESTING. Otherwise, return
7464 SSA_PROP_VARYING. */
7466 static enum ssa_prop_result
7467 vrp_visit_cond_stmt (gcond *stmt, edge *taken_edge_p)
7469 tree val;
7470 bool sop;
7472 *taken_edge_p = NULL;
7474 if (dump_file && (dump_flags & TDF_DETAILS))
7476 tree use;
7477 ssa_op_iter i;
7479 fprintf (dump_file, "\nVisiting conditional with predicate: ");
7480 print_gimple_stmt (dump_file, stmt, 0, 0);
7481 fprintf (dump_file, "\nWith known ranges\n");
7483 FOR_EACH_SSA_TREE_OPERAND (use, stmt, i, SSA_OP_USE)
7485 fprintf (dump_file, "\t");
7486 print_generic_expr (dump_file, use, 0);
7487 fprintf (dump_file, ": ");
7488 dump_value_range (dump_file, vr_value[SSA_NAME_VERSION (use)]);
7491 fprintf (dump_file, "\n");
7494 /* Compute the value of the predicate COND by checking the known
7495 ranges of each of its operands.
7497 Note that we cannot evaluate all the equivalent ranges here
7498 because those ranges may not yet be final and with the current
7499 propagation strategy, we cannot determine when the value ranges
7500 of the names in the equivalence set have changed.
7502 For instance, given the following code fragment
7504 i_5 = PHI <8, i_13>
7506 i_14 = ASSERT_EXPR <i_5, i_5 != 0>
7507 if (i_14 == 1)
7510 Assume that on the first visit to i_14, i_5 has the temporary
7511 range [8, 8] because the second argument to the PHI function is
7512 not yet executable. We derive the range ~[0, 0] for i_14 and the
7513 equivalence set { i_5 }. So, when we visit 'if (i_14 == 1)' for
7514 the first time, since i_14 is equivalent to the range [8, 8], we
7515 determine that the predicate is always false.
7517 On the next round of propagation, i_13 is determined to be
7518 VARYING, which causes i_5 to drop down to VARYING. So, another
7519 visit to i_14 is scheduled. In this second visit, we compute the
7520 exact same range and equivalence set for i_14, namely ~[0, 0] and
7521 { i_5 }. But we did not have the previous range for i_5
7522 registered, so vrp_visit_assignment thinks that the range for
7523 i_14 has not changed. Therefore, the predicate 'if (i_14 == 1)'
7524 is not visited again, which stops propagation from visiting
7525 statements in the THEN clause of that if().
7527 To properly fix this we would need to keep the previous range
7528 value for the names in the equivalence set. This way we would've
7529 discovered that from one visit to the other i_5 changed from
7530 range [8, 8] to VR_VARYING.
7532 However, fixing this apparent limitation may not be worth the
7533 additional checking. Testing on several code bases (GCC, DLV,
7534 MICO, TRAMP3D and SPEC2000) showed that doing this results in
7535 4 more predicates folded in SPEC. */
7536 sop = false;
7538 val = vrp_evaluate_conditional_warnv_with_ops (gimple_cond_code (stmt),
7539 gimple_cond_lhs (stmt),
7540 gimple_cond_rhs (stmt),
7541 false, &sop, NULL);
7542 if (val)
7544 if (!sop)
7545 *taken_edge_p = find_taken_edge (gimple_bb (stmt), val);
7546 else
7548 if (dump_file && (dump_flags & TDF_DETAILS))
7549 fprintf (dump_file,
7550 "\nIgnoring predicate evaluation because "
7551 "it assumes that signed overflow is undefined");
7552 val = NULL_TREE;
7556 if (dump_file && (dump_flags & TDF_DETAILS))
7558 fprintf (dump_file, "\nPredicate evaluates to: ");
7559 if (val == NULL_TREE)
7560 fprintf (dump_file, "DON'T KNOW\n");
7561 else
7562 print_generic_stmt (dump_file, val, 0);
7565 return (*taken_edge_p) ? SSA_PROP_INTERESTING : SSA_PROP_VARYING;
7568 /* Searches the case label vector VEC for the index *IDX of the CASE_LABEL
7569 that includes the value VAL. The search is restricted to the range
7570 [START_IDX, n - 1] where n is the size of VEC.
7572 If there is a CASE_LABEL for VAL, its index is placed in IDX and true is
7573 returned.
7575 If there is no CASE_LABEL for VAL and there is one that is larger than VAL,
7576 it is placed in IDX and false is returned.
7578 If VAL is larger than any CASE_LABEL, n is placed on IDX and false is
7579 returned. */
7581 static bool
7582 find_case_label_index (gswitch *stmt, size_t start_idx, tree val, size_t *idx)
7584 size_t n = gimple_switch_num_labels (stmt);
7585 size_t low, high;
7587 /* Find case label for minimum of the value range or the next one.
7588 At each iteration we are searching in [low, high - 1]. */
7590 for (low = start_idx, high = n; high != low; )
7592 tree t;
7593 int cmp;
7594 /* Note that i != high, so we never ask for n. */
7595 size_t i = (high + low) / 2;
7596 t = gimple_switch_label (stmt, i);
7598 /* Cache the result of comparing CASE_LOW and val. */
7599 cmp = tree_int_cst_compare (CASE_LOW (t), val);
7601 if (cmp == 0)
7603 /* Ranges cannot be empty. */
7604 *idx = i;
7605 return true;
7607 else if (cmp > 0)
7608 high = i;
7609 else
7611 low = i + 1;
7612 if (CASE_HIGH (t) != NULL
7613 && tree_int_cst_compare (CASE_HIGH (t), val) >= 0)
7615 *idx = i;
7616 return true;
7621 *idx = high;
7622 return false;
7625 /* Searches the case label vector VEC for the range of CASE_LABELs that is used
7626 for values between MIN and MAX. The first index is placed in MIN_IDX. The
7627 last index is placed in MAX_IDX. If the range of CASE_LABELs is empty
7628 then MAX_IDX < MIN_IDX.
7629 Returns true if the default label is not needed. */
7631 static bool
7632 find_case_label_range (gswitch *stmt, tree min, tree max, size_t *min_idx,
7633 size_t *max_idx)
7635 size_t i, j;
7636 bool min_take_default = !find_case_label_index (stmt, 1, min, &i);
7637 bool max_take_default = !find_case_label_index (stmt, i, max, &j);
7639 if (i == j
7640 && min_take_default
7641 && max_take_default)
7643 /* Only the default case label reached.
7644 Return an empty range. */
7645 *min_idx = 1;
7646 *max_idx = 0;
7647 return false;
7649 else
7651 bool take_default = min_take_default || max_take_default;
7652 tree low, high;
7653 size_t k;
7655 if (max_take_default)
7656 j--;
7658 /* If the case label range is continuous, we do not need
7659 the default case label. Verify that. */
7660 high = CASE_LOW (gimple_switch_label (stmt, i));
7661 if (CASE_HIGH (gimple_switch_label (stmt, i)))
7662 high = CASE_HIGH (gimple_switch_label (stmt, i));
7663 for (k = i + 1; k <= j; ++k)
7665 low = CASE_LOW (gimple_switch_label (stmt, k));
7666 if (!integer_onep (int_const_binop (MINUS_EXPR, low, high)))
7668 take_default = true;
7669 break;
7671 high = low;
7672 if (CASE_HIGH (gimple_switch_label (stmt, k)))
7673 high = CASE_HIGH (gimple_switch_label (stmt, k));
7676 *min_idx = i;
7677 *max_idx = j;
7678 return !take_default;
7682 /* Searches the case label vector VEC for the ranges of CASE_LABELs that are
7683 used in range VR. The indices are placed in MIN_IDX1, MAX_IDX, MIN_IDX2 and
7684 MAX_IDX2. If the ranges of CASE_LABELs are empty then MAX_IDX1 < MIN_IDX1.
7685 Returns true if the default label is not needed. */
7687 static bool
7688 find_case_label_ranges (gswitch *stmt, value_range *vr, size_t *min_idx1,
7689 size_t *max_idx1, size_t *min_idx2,
7690 size_t *max_idx2)
7692 size_t i, j, k, l;
7693 unsigned int n = gimple_switch_num_labels (stmt);
7694 bool take_default;
7695 tree case_low, case_high;
7696 tree min = vr->min, max = vr->max;
7698 gcc_checking_assert (vr->type == VR_RANGE || vr->type == VR_ANTI_RANGE);
7700 take_default = !find_case_label_range (stmt, min, max, &i, &j);
7702 /* Set second range to emtpy. */
7703 *min_idx2 = 1;
7704 *max_idx2 = 0;
7706 if (vr->type == VR_RANGE)
7708 *min_idx1 = i;
7709 *max_idx1 = j;
7710 return !take_default;
7713 /* Set first range to all case labels. */
7714 *min_idx1 = 1;
7715 *max_idx1 = n - 1;
7717 if (i > j)
7718 return false;
7720 /* Make sure all the values of case labels [i , j] are contained in
7721 range [MIN, MAX]. */
7722 case_low = CASE_LOW (gimple_switch_label (stmt, i));
7723 case_high = CASE_HIGH (gimple_switch_label (stmt, j));
7724 if (tree_int_cst_compare (case_low, min) < 0)
7725 i += 1;
7726 if (case_high != NULL_TREE
7727 && tree_int_cst_compare (max, case_high) < 0)
7728 j -= 1;
7730 if (i > j)
7731 return false;
7733 /* If the range spans case labels [i, j], the corresponding anti-range spans
7734 the labels [1, i - 1] and [j + 1, n - 1]. */
7735 k = j + 1;
7736 l = n - 1;
7737 if (k > l)
7739 k = 1;
7740 l = 0;
7743 j = i - 1;
7744 i = 1;
7745 if (i > j)
7747 i = k;
7748 j = l;
7749 k = 1;
7750 l = 0;
7753 *min_idx1 = i;
7754 *max_idx1 = j;
7755 *min_idx2 = k;
7756 *max_idx2 = l;
7757 return false;
7760 /* Visit switch statement STMT. If we can determine which edge
7761 will be taken out of STMT's basic block, record it in
7762 *TAKEN_EDGE_P and return SSA_PROP_INTERESTING. Otherwise, return
7763 SSA_PROP_VARYING. */
7765 static enum ssa_prop_result
7766 vrp_visit_switch_stmt (gswitch *stmt, edge *taken_edge_p)
7768 tree op, val;
7769 value_range *vr;
7770 size_t i = 0, j = 0, k, l;
7771 bool take_default;
7773 *taken_edge_p = NULL;
7774 op = gimple_switch_index (stmt);
7775 if (TREE_CODE (op) != SSA_NAME)
7776 return SSA_PROP_VARYING;
7778 vr = get_value_range (op);
7779 if (dump_file && (dump_flags & TDF_DETAILS))
7781 fprintf (dump_file, "\nVisiting switch expression with operand ");
7782 print_generic_expr (dump_file, op, 0);
7783 fprintf (dump_file, " with known range ");
7784 dump_value_range (dump_file, vr);
7785 fprintf (dump_file, "\n");
7788 if ((vr->type != VR_RANGE
7789 && vr->type != VR_ANTI_RANGE)
7790 || symbolic_range_p (vr))
7791 return SSA_PROP_VARYING;
7793 /* Find the single edge that is taken from the switch expression. */
7794 take_default = !find_case_label_ranges (stmt, vr, &i, &j, &k, &l);
7796 /* Check if the range spans no CASE_LABEL. If so, we only reach the default
7797 label */
7798 if (j < i)
7800 gcc_assert (take_default);
7801 val = gimple_switch_default_label (stmt);
7803 else
7805 /* Check if labels with index i to j and maybe the default label
7806 are all reaching the same label. */
7808 val = gimple_switch_label (stmt, i);
7809 if (take_default
7810 && CASE_LABEL (gimple_switch_default_label (stmt))
7811 != CASE_LABEL (val))
7813 if (dump_file && (dump_flags & TDF_DETAILS))
7814 fprintf (dump_file, " not a single destination for this "
7815 "range\n");
7816 return SSA_PROP_VARYING;
7818 for (++i; i <= j; ++i)
7820 if (CASE_LABEL (gimple_switch_label (stmt, i)) != CASE_LABEL (val))
7822 if (dump_file && (dump_flags & TDF_DETAILS))
7823 fprintf (dump_file, " not a single destination for this "
7824 "range\n");
7825 return SSA_PROP_VARYING;
7828 for (; k <= l; ++k)
7830 if (CASE_LABEL (gimple_switch_label (stmt, k)) != CASE_LABEL (val))
7832 if (dump_file && (dump_flags & TDF_DETAILS))
7833 fprintf (dump_file, " not a single destination for this "
7834 "range\n");
7835 return SSA_PROP_VARYING;
7840 *taken_edge_p = find_edge (gimple_bb (stmt),
7841 label_to_block (CASE_LABEL (val)));
7843 if (dump_file && (dump_flags & TDF_DETAILS))
7845 fprintf (dump_file, " will take edge to ");
7846 print_generic_stmt (dump_file, CASE_LABEL (val), 0);
7849 return SSA_PROP_INTERESTING;
7853 /* Evaluate statement STMT. If the statement produces a useful range,
7854 return SSA_PROP_INTERESTING and record the SSA name with the
7855 interesting range into *OUTPUT_P.
7857 If STMT is a conditional branch and we can determine its truth
7858 value, the taken edge is recorded in *TAKEN_EDGE_P.
7860 If STMT produces a varying value, return SSA_PROP_VARYING. */
7862 static enum ssa_prop_result
7863 vrp_visit_stmt (gimple *stmt, edge *taken_edge_p, tree *output_p)
7865 tree def;
7866 ssa_op_iter iter;
7868 if (dump_file && (dump_flags & TDF_DETAILS))
7870 fprintf (dump_file, "\nVisiting statement:\n");
7871 print_gimple_stmt (dump_file, stmt, 0, dump_flags);
7874 if (!stmt_interesting_for_vrp (stmt))
7875 gcc_assert (stmt_ends_bb_p (stmt));
7876 else if (is_gimple_assign (stmt) || is_gimple_call (stmt))
7877 return vrp_visit_assignment_or_call (stmt, output_p);
7878 else if (gimple_code (stmt) == GIMPLE_COND)
7879 return vrp_visit_cond_stmt (as_a <gcond *> (stmt), taken_edge_p);
7880 else if (gimple_code (stmt) == GIMPLE_SWITCH)
7881 return vrp_visit_switch_stmt (as_a <gswitch *> (stmt), taken_edge_p);
7883 /* All other statements produce nothing of interest for VRP, so mark
7884 their outputs varying and prevent further simulation. */
7885 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_DEF)
7886 set_value_range_to_varying (get_value_range (def));
7888 return SSA_PROP_VARYING;
7891 /* Union the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
7892 { VR1TYPE, VR0MIN, VR0MAX } and store the result
7893 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
7894 possible such range. The resulting range is not canonicalized. */
7896 static void
7897 union_ranges (enum value_range_type *vr0type,
7898 tree *vr0min, tree *vr0max,
7899 enum value_range_type vr1type,
7900 tree vr1min, tree vr1max)
7902 bool mineq = operand_equal_p (*vr0min, vr1min, 0);
7903 bool maxeq = operand_equal_p (*vr0max, vr1max, 0);
7905 /* [] is vr0, () is vr1 in the following classification comments. */
7906 if (mineq && maxeq)
7908 /* [( )] */
7909 if (*vr0type == vr1type)
7910 /* Nothing to do for equal ranges. */
7912 else if ((*vr0type == VR_RANGE
7913 && vr1type == VR_ANTI_RANGE)
7914 || (*vr0type == VR_ANTI_RANGE
7915 && vr1type == VR_RANGE))
7917 /* For anti-range with range union the result is varying. */
7918 goto give_up;
7920 else
7921 gcc_unreachable ();
7923 else if (operand_less_p (*vr0max, vr1min) == 1
7924 || operand_less_p (vr1max, *vr0min) == 1)
7926 /* [ ] ( ) or ( ) [ ]
7927 If the ranges have an empty intersection, result of the union
7928 operation is the anti-range or if both are anti-ranges
7929 it covers all. */
7930 if (*vr0type == VR_ANTI_RANGE
7931 && vr1type == VR_ANTI_RANGE)
7932 goto give_up;
7933 else if (*vr0type == VR_ANTI_RANGE
7934 && vr1type == VR_RANGE)
7936 else if (*vr0type == VR_RANGE
7937 && vr1type == VR_ANTI_RANGE)
7939 *vr0type = vr1type;
7940 *vr0min = vr1min;
7941 *vr0max = vr1max;
7943 else if (*vr0type == VR_RANGE
7944 && vr1type == VR_RANGE)
7946 /* The result is the convex hull of both ranges. */
7947 if (operand_less_p (*vr0max, vr1min) == 1)
7949 /* If the result can be an anti-range, create one. */
7950 if (TREE_CODE (*vr0max) == INTEGER_CST
7951 && TREE_CODE (vr1min) == INTEGER_CST
7952 && vrp_val_is_min (*vr0min)
7953 && vrp_val_is_max (vr1max))
7955 tree min = int_const_binop (PLUS_EXPR,
7956 *vr0max,
7957 build_int_cst (TREE_TYPE (*vr0max), 1));
7958 tree max = int_const_binop (MINUS_EXPR,
7959 vr1min,
7960 build_int_cst (TREE_TYPE (vr1min), 1));
7961 if (!operand_less_p (max, min))
7963 *vr0type = VR_ANTI_RANGE;
7964 *vr0min = min;
7965 *vr0max = max;
7967 else
7968 *vr0max = vr1max;
7970 else
7971 *vr0max = vr1max;
7973 else
7975 /* If the result can be an anti-range, create one. */
7976 if (TREE_CODE (vr1max) == INTEGER_CST
7977 && TREE_CODE (*vr0min) == INTEGER_CST
7978 && vrp_val_is_min (vr1min)
7979 && vrp_val_is_max (*vr0max))
7981 tree min = int_const_binop (PLUS_EXPR,
7982 vr1max,
7983 build_int_cst (TREE_TYPE (vr1max), 1));
7984 tree max = int_const_binop (MINUS_EXPR,
7985 *vr0min,
7986 build_int_cst (TREE_TYPE (*vr0min), 1));
7987 if (!operand_less_p (max, min))
7989 *vr0type = VR_ANTI_RANGE;
7990 *vr0min = min;
7991 *vr0max = max;
7993 else
7994 *vr0min = vr1min;
7996 else
7997 *vr0min = vr1min;
8000 else
8001 gcc_unreachable ();
8003 else if ((maxeq || operand_less_p (vr1max, *vr0max) == 1)
8004 && (mineq || operand_less_p (*vr0min, vr1min) == 1))
8006 /* [ ( ) ] or [( ) ] or [ ( )] */
8007 if (*vr0type == VR_RANGE
8008 && vr1type == VR_RANGE)
8010 else if (*vr0type == VR_ANTI_RANGE
8011 && vr1type == VR_ANTI_RANGE)
8013 *vr0type = vr1type;
8014 *vr0min = vr1min;
8015 *vr0max = vr1max;
8017 else if (*vr0type == VR_ANTI_RANGE
8018 && vr1type == VR_RANGE)
8020 /* Arbitrarily choose the right or left gap. */
8021 if (!mineq && TREE_CODE (vr1min) == INTEGER_CST)
8022 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
8023 build_int_cst (TREE_TYPE (vr1min), 1));
8024 else if (!maxeq && TREE_CODE (vr1max) == INTEGER_CST)
8025 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
8026 build_int_cst (TREE_TYPE (vr1max), 1));
8027 else
8028 goto give_up;
8030 else if (*vr0type == VR_RANGE
8031 && vr1type == VR_ANTI_RANGE)
8032 /* The result covers everything. */
8033 goto give_up;
8034 else
8035 gcc_unreachable ();
8037 else if ((maxeq || operand_less_p (*vr0max, vr1max) == 1)
8038 && (mineq || operand_less_p (vr1min, *vr0min) == 1))
8040 /* ( [ ] ) or ([ ] ) or ( [ ]) */
8041 if (*vr0type == VR_RANGE
8042 && vr1type == VR_RANGE)
8044 *vr0type = vr1type;
8045 *vr0min = vr1min;
8046 *vr0max = vr1max;
8048 else if (*vr0type == VR_ANTI_RANGE
8049 && vr1type == VR_ANTI_RANGE)
8051 else if (*vr0type == VR_RANGE
8052 && vr1type == VR_ANTI_RANGE)
8054 *vr0type = VR_ANTI_RANGE;
8055 if (!mineq && TREE_CODE (*vr0min) == INTEGER_CST)
8057 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
8058 build_int_cst (TREE_TYPE (*vr0min), 1));
8059 *vr0min = vr1min;
8061 else if (!maxeq && TREE_CODE (*vr0max) == INTEGER_CST)
8063 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
8064 build_int_cst (TREE_TYPE (*vr0max), 1));
8065 *vr0max = vr1max;
8067 else
8068 goto give_up;
8070 else if (*vr0type == VR_ANTI_RANGE
8071 && vr1type == VR_RANGE)
8072 /* The result covers everything. */
8073 goto give_up;
8074 else
8075 gcc_unreachable ();
8077 else if ((operand_less_p (vr1min, *vr0max) == 1
8078 || operand_equal_p (vr1min, *vr0max, 0))
8079 && operand_less_p (*vr0min, vr1min) == 1
8080 && operand_less_p (*vr0max, vr1max) == 1)
8082 /* [ ( ] ) or [ ]( ) */
8083 if (*vr0type == VR_RANGE
8084 && vr1type == VR_RANGE)
8085 *vr0max = vr1max;
8086 else if (*vr0type == VR_ANTI_RANGE
8087 && vr1type == VR_ANTI_RANGE)
8088 *vr0min = vr1min;
8089 else if (*vr0type == VR_ANTI_RANGE
8090 && vr1type == VR_RANGE)
8092 if (TREE_CODE (vr1min) == INTEGER_CST)
8093 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
8094 build_int_cst (TREE_TYPE (vr1min), 1));
8095 else
8096 goto give_up;
8098 else if (*vr0type == VR_RANGE
8099 && vr1type == VR_ANTI_RANGE)
8101 if (TREE_CODE (*vr0max) == INTEGER_CST)
8103 *vr0type = vr1type;
8104 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
8105 build_int_cst (TREE_TYPE (*vr0max), 1));
8106 *vr0max = vr1max;
8108 else
8109 goto give_up;
8111 else
8112 gcc_unreachable ();
8114 else if ((operand_less_p (*vr0min, vr1max) == 1
8115 || operand_equal_p (*vr0min, vr1max, 0))
8116 && operand_less_p (vr1min, *vr0min) == 1
8117 && operand_less_p (vr1max, *vr0max) == 1)
8119 /* ( [ ) ] or ( )[ ] */
8120 if (*vr0type == VR_RANGE
8121 && vr1type == VR_RANGE)
8122 *vr0min = vr1min;
8123 else if (*vr0type == VR_ANTI_RANGE
8124 && vr1type == VR_ANTI_RANGE)
8125 *vr0max = vr1max;
8126 else if (*vr0type == VR_ANTI_RANGE
8127 && vr1type == VR_RANGE)
8129 if (TREE_CODE (vr1max) == INTEGER_CST)
8130 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
8131 build_int_cst (TREE_TYPE (vr1max), 1));
8132 else
8133 goto give_up;
8135 else if (*vr0type == VR_RANGE
8136 && vr1type == VR_ANTI_RANGE)
8138 if (TREE_CODE (*vr0min) == INTEGER_CST)
8140 *vr0type = vr1type;
8141 *vr0min = vr1min;
8142 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
8143 build_int_cst (TREE_TYPE (*vr0min), 1));
8145 else
8146 goto give_up;
8148 else
8149 gcc_unreachable ();
8151 else
8152 goto give_up;
8154 return;
8156 give_up:
8157 *vr0type = VR_VARYING;
8158 *vr0min = NULL_TREE;
8159 *vr0max = NULL_TREE;
8162 /* Intersect the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
8163 { VR1TYPE, VR0MIN, VR0MAX } and store the result
8164 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
8165 possible such range. The resulting range is not canonicalized. */
8167 static void
8168 intersect_ranges (enum value_range_type *vr0type,
8169 tree *vr0min, tree *vr0max,
8170 enum value_range_type vr1type,
8171 tree vr1min, tree vr1max)
8173 bool mineq = operand_equal_p (*vr0min, vr1min, 0);
8174 bool maxeq = operand_equal_p (*vr0max, vr1max, 0);
8176 /* [] is vr0, () is vr1 in the following classification comments. */
8177 if (mineq && maxeq)
8179 /* [( )] */
8180 if (*vr0type == vr1type)
8181 /* Nothing to do for equal ranges. */
8183 else if ((*vr0type == VR_RANGE
8184 && vr1type == VR_ANTI_RANGE)
8185 || (*vr0type == VR_ANTI_RANGE
8186 && vr1type == VR_RANGE))
8188 /* For anti-range with range intersection the result is empty. */
8189 *vr0type = VR_UNDEFINED;
8190 *vr0min = NULL_TREE;
8191 *vr0max = NULL_TREE;
8193 else
8194 gcc_unreachable ();
8196 else if (operand_less_p (*vr0max, vr1min) == 1
8197 || operand_less_p (vr1max, *vr0min) == 1)
8199 /* [ ] ( ) or ( ) [ ]
8200 If the ranges have an empty intersection, the result of the
8201 intersect operation is the range for intersecting an
8202 anti-range with a range or empty when intersecting two ranges. */
8203 if (*vr0type == VR_RANGE
8204 && vr1type == VR_ANTI_RANGE)
8206 else if (*vr0type == VR_ANTI_RANGE
8207 && vr1type == VR_RANGE)
8209 *vr0type = vr1type;
8210 *vr0min = vr1min;
8211 *vr0max = vr1max;
8213 else if (*vr0type == VR_RANGE
8214 && vr1type == VR_RANGE)
8216 *vr0type = VR_UNDEFINED;
8217 *vr0min = NULL_TREE;
8218 *vr0max = NULL_TREE;
8220 else if (*vr0type == VR_ANTI_RANGE
8221 && vr1type == VR_ANTI_RANGE)
8223 /* If the anti-ranges are adjacent to each other merge them. */
8224 if (TREE_CODE (*vr0max) == INTEGER_CST
8225 && TREE_CODE (vr1min) == INTEGER_CST
8226 && operand_less_p (*vr0max, vr1min) == 1
8227 && integer_onep (int_const_binop (MINUS_EXPR,
8228 vr1min, *vr0max)))
8229 *vr0max = vr1max;
8230 else if (TREE_CODE (vr1max) == INTEGER_CST
8231 && TREE_CODE (*vr0min) == INTEGER_CST
8232 && operand_less_p (vr1max, *vr0min) == 1
8233 && integer_onep (int_const_binop (MINUS_EXPR,
8234 *vr0min, vr1max)))
8235 *vr0min = vr1min;
8236 /* Else arbitrarily take VR0. */
8239 else if ((maxeq || operand_less_p (vr1max, *vr0max) == 1)
8240 && (mineq || operand_less_p (*vr0min, vr1min) == 1))
8242 /* [ ( ) ] or [( ) ] or [ ( )] */
8243 if (*vr0type == VR_RANGE
8244 && vr1type == VR_RANGE)
8246 /* If both are ranges the result is the inner one. */
8247 *vr0type = vr1type;
8248 *vr0min = vr1min;
8249 *vr0max = vr1max;
8251 else if (*vr0type == VR_RANGE
8252 && vr1type == VR_ANTI_RANGE)
8254 /* Choose the right gap if the left one is empty. */
8255 if (mineq)
8257 if (TREE_CODE (vr1max) == INTEGER_CST)
8258 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
8259 build_int_cst (TREE_TYPE (vr1max), 1));
8260 else
8261 *vr0min = vr1max;
8263 /* Choose the left gap if the right one is empty. */
8264 else if (maxeq)
8266 if (TREE_CODE (vr1min) == INTEGER_CST)
8267 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
8268 build_int_cst (TREE_TYPE (vr1min), 1));
8269 else
8270 *vr0max = vr1min;
8272 /* Choose the anti-range if the range is effectively varying. */
8273 else if (vrp_val_is_min (*vr0min)
8274 && vrp_val_is_max (*vr0max))
8276 *vr0type = vr1type;
8277 *vr0min = vr1min;
8278 *vr0max = vr1max;
8280 /* Else choose the range. */
8282 else if (*vr0type == VR_ANTI_RANGE
8283 && vr1type == VR_ANTI_RANGE)
8284 /* If both are anti-ranges the result is the outer one. */
8286 else if (*vr0type == VR_ANTI_RANGE
8287 && vr1type == VR_RANGE)
8289 /* The intersection is empty. */
8290 *vr0type = VR_UNDEFINED;
8291 *vr0min = NULL_TREE;
8292 *vr0max = NULL_TREE;
8294 else
8295 gcc_unreachable ();
8297 else if ((maxeq || operand_less_p (*vr0max, vr1max) == 1)
8298 && (mineq || operand_less_p (vr1min, *vr0min) == 1))
8300 /* ( [ ] ) or ([ ] ) or ( [ ]) */
8301 if (*vr0type == VR_RANGE
8302 && vr1type == VR_RANGE)
8303 /* Choose the inner range. */
8305 else if (*vr0type == VR_ANTI_RANGE
8306 && vr1type == VR_RANGE)
8308 /* Choose the right gap if the left is empty. */
8309 if (mineq)
8311 *vr0type = VR_RANGE;
8312 if (TREE_CODE (*vr0max) == INTEGER_CST)
8313 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
8314 build_int_cst (TREE_TYPE (*vr0max), 1));
8315 else
8316 *vr0min = *vr0max;
8317 *vr0max = vr1max;
8319 /* Choose the left gap if the right is empty. */
8320 else if (maxeq)
8322 *vr0type = VR_RANGE;
8323 if (TREE_CODE (*vr0min) == INTEGER_CST)
8324 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
8325 build_int_cst (TREE_TYPE (*vr0min), 1));
8326 else
8327 *vr0max = *vr0min;
8328 *vr0min = vr1min;
8330 /* Choose the anti-range if the range is effectively varying. */
8331 else if (vrp_val_is_min (vr1min)
8332 && vrp_val_is_max (vr1max))
8334 /* Else choose the range. */
8335 else
8337 *vr0type = vr1type;
8338 *vr0min = vr1min;
8339 *vr0max = vr1max;
8342 else if (*vr0type == VR_ANTI_RANGE
8343 && vr1type == VR_ANTI_RANGE)
8345 /* If both are anti-ranges the result is the outer one. */
8346 *vr0type = vr1type;
8347 *vr0min = vr1min;
8348 *vr0max = vr1max;
8350 else if (vr1type == VR_ANTI_RANGE
8351 && *vr0type == VR_RANGE)
8353 /* The intersection is empty. */
8354 *vr0type = VR_UNDEFINED;
8355 *vr0min = NULL_TREE;
8356 *vr0max = NULL_TREE;
8358 else
8359 gcc_unreachable ();
8361 else if ((operand_less_p (vr1min, *vr0max) == 1
8362 || operand_equal_p (vr1min, *vr0max, 0))
8363 && operand_less_p (*vr0min, vr1min) == 1)
8365 /* [ ( ] ) or [ ]( ) */
8366 if (*vr0type == VR_ANTI_RANGE
8367 && vr1type == VR_ANTI_RANGE)
8368 *vr0max = vr1max;
8369 else if (*vr0type == VR_RANGE
8370 && vr1type == VR_RANGE)
8371 *vr0min = vr1min;
8372 else if (*vr0type == VR_RANGE
8373 && vr1type == VR_ANTI_RANGE)
8375 if (TREE_CODE (vr1min) == INTEGER_CST)
8376 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
8377 build_int_cst (TREE_TYPE (vr1min), 1));
8378 else
8379 *vr0max = vr1min;
8381 else if (*vr0type == VR_ANTI_RANGE
8382 && vr1type == VR_RANGE)
8384 *vr0type = VR_RANGE;
8385 if (TREE_CODE (*vr0max) == INTEGER_CST)
8386 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
8387 build_int_cst (TREE_TYPE (*vr0max), 1));
8388 else
8389 *vr0min = *vr0max;
8390 *vr0max = vr1max;
8392 else
8393 gcc_unreachable ();
8395 else if ((operand_less_p (*vr0min, vr1max) == 1
8396 || operand_equal_p (*vr0min, vr1max, 0))
8397 && operand_less_p (vr1min, *vr0min) == 1)
8399 /* ( [ ) ] or ( )[ ] */
8400 if (*vr0type == VR_ANTI_RANGE
8401 && vr1type == VR_ANTI_RANGE)
8402 *vr0min = vr1min;
8403 else if (*vr0type == VR_RANGE
8404 && vr1type == VR_RANGE)
8405 *vr0max = vr1max;
8406 else if (*vr0type == VR_RANGE
8407 && vr1type == VR_ANTI_RANGE)
8409 if (TREE_CODE (vr1max) == INTEGER_CST)
8410 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
8411 build_int_cst (TREE_TYPE (vr1max), 1));
8412 else
8413 *vr0min = vr1max;
8415 else if (*vr0type == VR_ANTI_RANGE
8416 && vr1type == VR_RANGE)
8418 *vr0type = VR_RANGE;
8419 if (TREE_CODE (*vr0min) == INTEGER_CST)
8420 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
8421 build_int_cst (TREE_TYPE (*vr0min), 1));
8422 else
8423 *vr0max = *vr0min;
8424 *vr0min = vr1min;
8426 else
8427 gcc_unreachable ();
8430 /* As a fallback simply use { *VRTYPE, *VR0MIN, *VR0MAX } as
8431 result for the intersection. That's always a conservative
8432 correct estimate. */
8434 return;
8438 /* Intersect the two value-ranges *VR0 and *VR1 and store the result
8439 in *VR0. This may not be the smallest possible such range. */
8441 static void
8442 vrp_intersect_ranges_1 (value_range *vr0, value_range *vr1)
8444 value_range saved;
8446 /* If either range is VR_VARYING the other one wins. */
8447 if (vr1->type == VR_VARYING)
8448 return;
8449 if (vr0->type == VR_VARYING)
8451 copy_value_range (vr0, vr1);
8452 return;
8455 /* When either range is VR_UNDEFINED the resulting range is
8456 VR_UNDEFINED, too. */
8457 if (vr0->type == VR_UNDEFINED)
8458 return;
8459 if (vr1->type == VR_UNDEFINED)
8461 set_value_range_to_undefined (vr0);
8462 return;
8465 /* Save the original vr0 so we can return it as conservative intersection
8466 result when our worker turns things to varying. */
8467 saved = *vr0;
8468 intersect_ranges (&vr0->type, &vr0->min, &vr0->max,
8469 vr1->type, vr1->min, vr1->max);
8470 /* Make sure to canonicalize the result though as the inversion of a
8471 VR_RANGE can still be a VR_RANGE. */
8472 set_and_canonicalize_value_range (vr0, vr0->type,
8473 vr0->min, vr0->max, vr0->equiv);
8474 /* If that failed, use the saved original VR0. */
8475 if (vr0->type == VR_VARYING)
8477 *vr0 = saved;
8478 return;
8480 /* If the result is VR_UNDEFINED there is no need to mess with
8481 the equivalencies. */
8482 if (vr0->type == VR_UNDEFINED)
8483 return;
8485 /* The resulting set of equivalences for range intersection is the union of
8486 the two sets. */
8487 if (vr0->equiv && vr1->equiv && vr0->equiv != vr1->equiv)
8488 bitmap_ior_into (vr0->equiv, vr1->equiv);
8489 else if (vr1->equiv && !vr0->equiv)
8490 bitmap_copy (vr0->equiv, vr1->equiv);
8493 static void
8494 vrp_intersect_ranges (value_range *vr0, value_range *vr1)
8496 if (dump_file && (dump_flags & TDF_DETAILS))
8498 fprintf (dump_file, "Intersecting\n ");
8499 dump_value_range (dump_file, vr0);
8500 fprintf (dump_file, "\nand\n ");
8501 dump_value_range (dump_file, vr1);
8502 fprintf (dump_file, "\n");
8504 vrp_intersect_ranges_1 (vr0, vr1);
8505 if (dump_file && (dump_flags & TDF_DETAILS))
8507 fprintf (dump_file, "to\n ");
8508 dump_value_range (dump_file, vr0);
8509 fprintf (dump_file, "\n");
8513 /* Meet operation for value ranges. Given two value ranges VR0 and
8514 VR1, store in VR0 a range that contains both VR0 and VR1. This
8515 may not be the smallest possible such range. */
8517 static void
8518 vrp_meet_1 (value_range *vr0, value_range *vr1)
8520 value_range saved;
8522 if (vr0->type == VR_UNDEFINED)
8524 set_value_range (vr0, vr1->type, vr1->min, vr1->max, vr1->equiv);
8525 return;
8528 if (vr1->type == VR_UNDEFINED)
8530 /* VR0 already has the resulting range. */
8531 return;
8534 if (vr0->type == VR_VARYING)
8536 /* Nothing to do. VR0 already has the resulting range. */
8537 return;
8540 if (vr1->type == VR_VARYING)
8542 set_value_range_to_varying (vr0);
8543 return;
8546 saved = *vr0;
8547 union_ranges (&vr0->type, &vr0->min, &vr0->max,
8548 vr1->type, vr1->min, vr1->max);
8549 if (vr0->type == VR_VARYING)
8551 /* Failed to find an efficient meet. Before giving up and setting
8552 the result to VARYING, see if we can at least derive a useful
8553 anti-range. FIXME, all this nonsense about distinguishing
8554 anti-ranges from ranges is necessary because of the odd
8555 semantics of range_includes_zero_p and friends. */
8556 if (((saved.type == VR_RANGE
8557 && range_includes_zero_p (saved.min, saved.max) == 0)
8558 || (saved.type == VR_ANTI_RANGE
8559 && range_includes_zero_p (saved.min, saved.max) == 1))
8560 && ((vr1->type == VR_RANGE
8561 && range_includes_zero_p (vr1->min, vr1->max) == 0)
8562 || (vr1->type == VR_ANTI_RANGE
8563 && range_includes_zero_p (vr1->min, vr1->max) == 1)))
8565 set_value_range_to_nonnull (vr0, TREE_TYPE (saved.min));
8567 /* Since this meet operation did not result from the meeting of
8568 two equivalent names, VR0 cannot have any equivalences. */
8569 if (vr0->equiv)
8570 bitmap_clear (vr0->equiv);
8571 return;
8574 set_value_range_to_varying (vr0);
8575 return;
8577 set_and_canonicalize_value_range (vr0, vr0->type, vr0->min, vr0->max,
8578 vr0->equiv);
8579 if (vr0->type == VR_VARYING)
8580 return;
8582 /* The resulting set of equivalences is always the intersection of
8583 the two sets. */
8584 if (vr0->equiv && vr1->equiv && vr0->equiv != vr1->equiv)
8585 bitmap_and_into (vr0->equiv, vr1->equiv);
8586 else if (vr0->equiv && !vr1->equiv)
8587 bitmap_clear (vr0->equiv);
8590 static void
8591 vrp_meet (value_range *vr0, value_range *vr1)
8593 if (dump_file && (dump_flags & TDF_DETAILS))
8595 fprintf (dump_file, "Meeting\n ");
8596 dump_value_range (dump_file, vr0);
8597 fprintf (dump_file, "\nand\n ");
8598 dump_value_range (dump_file, vr1);
8599 fprintf (dump_file, "\n");
8601 vrp_meet_1 (vr0, vr1);
8602 if (dump_file && (dump_flags & TDF_DETAILS))
8604 fprintf (dump_file, "to\n ");
8605 dump_value_range (dump_file, vr0);
8606 fprintf (dump_file, "\n");
8611 /* Visit all arguments for PHI node PHI that flow through executable
8612 edges. If a valid value range can be derived from all the incoming
8613 value ranges, set a new range for the LHS of PHI. */
8615 static enum ssa_prop_result
8616 vrp_visit_phi_node (gphi *phi)
8618 size_t i;
8619 tree lhs = PHI_RESULT (phi);
8620 value_range *lhs_vr = get_value_range (lhs);
8621 value_range vr_result = VR_INITIALIZER;
8622 bool first = true;
8623 int edges, old_edges;
8624 struct loop *l;
8626 if (dump_file && (dump_flags & TDF_DETAILS))
8628 fprintf (dump_file, "\nVisiting PHI node: ");
8629 print_gimple_stmt (dump_file, phi, 0, dump_flags);
8632 edges = 0;
8633 for (i = 0; i < gimple_phi_num_args (phi); i++)
8635 edge e = gimple_phi_arg_edge (phi, i);
8637 if (dump_file && (dump_flags & TDF_DETAILS))
8639 fprintf (dump_file,
8640 " Argument #%d (%d -> %d %sexecutable)\n",
8641 (int) i, e->src->index, e->dest->index,
8642 (e->flags & EDGE_EXECUTABLE) ? "" : "not ");
8645 if (e->flags & EDGE_EXECUTABLE)
8647 tree arg = PHI_ARG_DEF (phi, i);
8648 value_range vr_arg;
8650 ++edges;
8652 if (TREE_CODE (arg) == SSA_NAME)
8654 vr_arg = *(get_value_range (arg));
8655 /* Do not allow equivalences or symbolic ranges to leak in from
8656 backedges. That creates invalid equivalencies.
8657 See PR53465 and PR54767. */
8658 if (e->flags & EDGE_DFS_BACK)
8660 if (vr_arg.type == VR_RANGE
8661 || vr_arg.type == VR_ANTI_RANGE)
8663 vr_arg.equiv = NULL;
8664 if (symbolic_range_p (&vr_arg))
8666 vr_arg.type = VR_VARYING;
8667 vr_arg.min = NULL_TREE;
8668 vr_arg.max = NULL_TREE;
8672 else
8674 /* If the non-backedge arguments range is VR_VARYING then
8675 we can still try recording a simple equivalence. */
8676 if (vr_arg.type == VR_VARYING)
8678 vr_arg.type = VR_RANGE;
8679 vr_arg.min = arg;
8680 vr_arg.max = arg;
8681 vr_arg.equiv = NULL;
8685 else
8687 if (TREE_OVERFLOW_P (arg))
8688 arg = drop_tree_overflow (arg);
8690 vr_arg.type = VR_RANGE;
8691 vr_arg.min = arg;
8692 vr_arg.max = arg;
8693 vr_arg.equiv = NULL;
8696 if (dump_file && (dump_flags & TDF_DETAILS))
8698 fprintf (dump_file, "\t");
8699 print_generic_expr (dump_file, arg, dump_flags);
8700 fprintf (dump_file, ": ");
8701 dump_value_range (dump_file, &vr_arg);
8702 fprintf (dump_file, "\n");
8705 if (first)
8706 copy_value_range (&vr_result, &vr_arg);
8707 else
8708 vrp_meet (&vr_result, &vr_arg);
8709 first = false;
8711 if (vr_result.type == VR_VARYING)
8712 break;
8716 if (vr_result.type == VR_VARYING)
8717 goto varying;
8718 else if (vr_result.type == VR_UNDEFINED)
8719 goto update_range;
8721 old_edges = vr_phi_edge_counts[SSA_NAME_VERSION (lhs)];
8722 vr_phi_edge_counts[SSA_NAME_VERSION (lhs)] = edges;
8724 /* To prevent infinite iterations in the algorithm, derive ranges
8725 when the new value is slightly bigger or smaller than the
8726 previous one. We don't do this if we have seen a new executable
8727 edge; this helps us avoid an overflow infinity for conditionals
8728 which are not in a loop. If the old value-range was VR_UNDEFINED
8729 use the updated range and iterate one more time. */
8730 if (edges > 0
8731 && gimple_phi_num_args (phi) > 1
8732 && edges == old_edges
8733 && lhs_vr->type != VR_UNDEFINED)
8735 /* Compare old and new ranges, fall back to varying if the
8736 values are not comparable. */
8737 int cmp_min = compare_values (lhs_vr->min, vr_result.min);
8738 if (cmp_min == -2)
8739 goto varying;
8740 int cmp_max = compare_values (lhs_vr->max, vr_result.max);
8741 if (cmp_max == -2)
8742 goto varying;
8744 /* For non VR_RANGE or for pointers fall back to varying if
8745 the range changed. */
8746 if ((lhs_vr->type != VR_RANGE || vr_result.type != VR_RANGE
8747 || POINTER_TYPE_P (TREE_TYPE (lhs)))
8748 && (cmp_min != 0 || cmp_max != 0))
8749 goto varying;
8751 /* If the new minimum is larger than the previous one
8752 retain the old value. If the new minimum value is smaller
8753 than the previous one and not -INF go all the way to -INF + 1.
8754 In the first case, to avoid infinite bouncing between different
8755 minimums, and in the other case to avoid iterating millions of
8756 times to reach -INF. Going to -INF + 1 also lets the following
8757 iteration compute whether there will be any overflow, at the
8758 expense of one additional iteration. */
8759 if (cmp_min < 0)
8760 vr_result.min = lhs_vr->min;
8761 else if (cmp_min > 0
8762 && !vrp_val_is_min (vr_result.min))
8763 vr_result.min
8764 = int_const_binop (PLUS_EXPR,
8765 vrp_val_min (TREE_TYPE (vr_result.min)),
8766 build_int_cst (TREE_TYPE (vr_result.min), 1));
8768 /* Similarly for the maximum value. */
8769 if (cmp_max > 0)
8770 vr_result.max = lhs_vr->max;
8771 else if (cmp_max < 0
8772 && !vrp_val_is_max (vr_result.max))
8773 vr_result.max
8774 = int_const_binop (MINUS_EXPR,
8775 vrp_val_max (TREE_TYPE (vr_result.min)),
8776 build_int_cst (TREE_TYPE (vr_result.min), 1));
8778 /* If we dropped either bound to +-INF then if this is a loop
8779 PHI node SCEV may known more about its value-range. */
8780 if (cmp_min > 0 || cmp_min < 0
8781 || cmp_max < 0 || cmp_max > 0)
8782 goto scev_check;
8784 goto infinite_check;
8787 /* If the new range is different than the previous value, keep
8788 iterating. */
8789 update_range:
8790 if (update_value_range (lhs, &vr_result))
8792 if (dump_file && (dump_flags & TDF_DETAILS))
8794 fprintf (dump_file, "Found new range for ");
8795 print_generic_expr (dump_file, lhs, 0);
8796 fprintf (dump_file, ": ");
8797 dump_value_range (dump_file, &vr_result);
8798 fprintf (dump_file, "\n");
8801 if (vr_result.type == VR_VARYING)
8802 return SSA_PROP_VARYING;
8804 return SSA_PROP_INTERESTING;
8807 /* Nothing changed, don't add outgoing edges. */
8808 return SSA_PROP_NOT_INTERESTING;
8810 varying:
8811 set_value_range_to_varying (&vr_result);
8813 scev_check:
8814 /* If this is a loop PHI node SCEV may known more about its value-range.
8815 scev_check can be reached from two paths, one is a fall through from above
8816 "varying" label, the other is direct goto from code block which tries to
8817 avoid infinite simulation. */
8818 if ((l = loop_containing_stmt (phi))
8819 && l->header == gimple_bb (phi))
8820 adjust_range_with_scev (&vr_result, l, phi, lhs);
8822 infinite_check:
8823 /* If we will end up with a (-INF, +INF) range, set it to
8824 VARYING. Same if the previous max value was invalid for
8825 the type and we end up with vr_result.min > vr_result.max. */
8826 if ((vr_result.type == VR_RANGE || vr_result.type == VR_ANTI_RANGE)
8827 && !((vrp_val_is_max (vr_result.max) && vrp_val_is_min (vr_result.min))
8828 || compare_values (vr_result.min, vr_result.max) > 0))
8829 goto update_range;
8831 /* No match found. Set the LHS to VARYING. */
8832 set_value_range_to_varying (lhs_vr);
8833 return SSA_PROP_VARYING;
8836 /* Simplify boolean operations if the source is known
8837 to be already a boolean. */
8838 static bool
8839 simplify_truth_ops_using_ranges (gimple_stmt_iterator *gsi, gimple *stmt)
8841 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
8842 tree lhs, op0, op1;
8843 bool need_conversion;
8845 /* We handle only !=/== case here. */
8846 gcc_assert (rhs_code == EQ_EXPR || rhs_code == NE_EXPR);
8848 op0 = gimple_assign_rhs1 (stmt);
8849 if (!op_with_boolean_value_range_p (op0))
8850 return false;
8852 op1 = gimple_assign_rhs2 (stmt);
8853 if (!op_with_boolean_value_range_p (op1))
8854 return false;
8856 /* Reduce number of cases to handle to NE_EXPR. As there is no
8857 BIT_XNOR_EXPR we cannot replace A == B with a single statement. */
8858 if (rhs_code == EQ_EXPR)
8860 if (TREE_CODE (op1) == INTEGER_CST)
8861 op1 = int_const_binop (BIT_XOR_EXPR, op1,
8862 build_int_cst (TREE_TYPE (op1), 1));
8863 else
8864 return false;
8867 lhs = gimple_assign_lhs (stmt);
8868 need_conversion
8869 = !useless_type_conversion_p (TREE_TYPE (lhs), TREE_TYPE (op0));
8871 /* Make sure to not sign-extend a 1-bit 1 when converting the result. */
8872 if (need_conversion
8873 && !TYPE_UNSIGNED (TREE_TYPE (op0))
8874 && TYPE_PRECISION (TREE_TYPE (op0)) == 1
8875 && TYPE_PRECISION (TREE_TYPE (lhs)) > 1)
8876 return false;
8878 /* For A != 0 we can substitute A itself. */
8879 if (integer_zerop (op1))
8880 gimple_assign_set_rhs_with_ops (gsi,
8881 need_conversion
8882 ? NOP_EXPR : TREE_CODE (op0), op0);
8883 /* For A != B we substitute A ^ B. Either with conversion. */
8884 else if (need_conversion)
8886 tree tem = make_ssa_name (TREE_TYPE (op0));
8887 gassign *newop
8888 = gimple_build_assign (tem, BIT_XOR_EXPR, op0, op1);
8889 gsi_insert_before (gsi, newop, GSI_SAME_STMT);
8890 if (INTEGRAL_TYPE_P (TREE_TYPE (tem))
8891 && TYPE_PRECISION (TREE_TYPE (tem)) > 1)
8892 set_range_info (tem, VR_RANGE,
8893 wi::zero (TYPE_PRECISION (TREE_TYPE (tem))),
8894 wi::one (TYPE_PRECISION (TREE_TYPE (tem))));
8895 gimple_assign_set_rhs_with_ops (gsi, NOP_EXPR, tem);
8897 /* Or without. */
8898 else
8899 gimple_assign_set_rhs_with_ops (gsi, BIT_XOR_EXPR, op0, op1);
8900 update_stmt (gsi_stmt (*gsi));
8902 return true;
8905 /* Simplify a division or modulo operator to a right shift or
8906 bitwise and if the first operand is unsigned or is greater
8907 than zero and the second operand is an exact power of two.
8908 For TRUNC_MOD_EXPR op0 % op1 with constant op1, optimize it
8909 into just op0 if op0's range is known to be a subset of
8910 [-op1 + 1, op1 - 1] for signed and [0, op1 - 1] for unsigned
8911 modulo. */
8913 static bool
8914 simplify_div_or_mod_using_ranges (gimple_stmt_iterator *gsi, gimple *stmt)
8916 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
8917 tree val = NULL;
8918 tree op0 = gimple_assign_rhs1 (stmt);
8919 tree op1 = gimple_assign_rhs2 (stmt);
8920 value_range *vr = get_value_range (op0);
8922 if (rhs_code == TRUNC_MOD_EXPR
8923 && TREE_CODE (op1) == INTEGER_CST
8924 && tree_int_cst_sgn (op1) == 1
8925 && range_int_cst_p (vr)
8926 && tree_int_cst_lt (vr->max, op1))
8928 if (TYPE_UNSIGNED (TREE_TYPE (op0))
8929 || tree_int_cst_sgn (vr->min) >= 0
8930 || tree_int_cst_lt (fold_unary (NEGATE_EXPR, TREE_TYPE (op1), op1),
8931 vr->min))
8933 /* If op0 already has the range op0 % op1 has,
8934 then TRUNC_MOD_EXPR won't change anything. */
8935 gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
8936 gimple_assign_set_rhs_from_tree (&gsi, op0);
8937 update_stmt (stmt);
8938 return true;
8942 if (!integer_pow2p (op1))
8944 /* X % -Y can be only optimized into X % Y either if
8945 X is not INT_MIN, or Y is not -1. Fold it now, as after
8946 remove_range_assertions the range info might be not available
8947 anymore. */
8948 if (rhs_code == TRUNC_MOD_EXPR
8949 && fold_stmt (gsi, follow_single_use_edges))
8950 return true;
8951 return false;
8954 if (TYPE_UNSIGNED (TREE_TYPE (op0)))
8955 val = integer_one_node;
8956 else
8958 bool sop = false;
8960 val = compare_range_with_value (GE_EXPR, vr, integer_zero_node, &sop);
8962 if (val
8963 && sop
8964 && integer_onep (val)
8965 && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC))
8967 location_t location;
8969 if (!gimple_has_location (stmt))
8970 location = input_location;
8971 else
8972 location = gimple_location (stmt);
8973 warning_at (location, OPT_Wstrict_overflow,
8974 "assuming signed overflow does not occur when "
8975 "simplifying %</%> or %<%%%> to %<>>%> or %<&%>");
8979 if (val && integer_onep (val))
8981 tree t;
8983 if (rhs_code == TRUNC_DIV_EXPR)
8985 t = build_int_cst (integer_type_node, tree_log2 (op1));
8986 gimple_assign_set_rhs_code (stmt, RSHIFT_EXPR);
8987 gimple_assign_set_rhs1 (stmt, op0);
8988 gimple_assign_set_rhs2 (stmt, t);
8990 else
8992 t = build_int_cst (TREE_TYPE (op1), 1);
8993 t = int_const_binop (MINUS_EXPR, op1, t);
8994 t = fold_convert (TREE_TYPE (op0), t);
8996 gimple_assign_set_rhs_code (stmt, BIT_AND_EXPR);
8997 gimple_assign_set_rhs1 (stmt, op0);
8998 gimple_assign_set_rhs2 (stmt, t);
9001 update_stmt (stmt);
9002 return true;
9005 return false;
9008 /* Simplify a min or max if the ranges of the two operands are
9009 disjoint. Return true if we do simplify. */
9011 static bool
9012 simplify_min_or_max_using_ranges (gimple *stmt)
9014 tree op0 = gimple_assign_rhs1 (stmt);
9015 tree op1 = gimple_assign_rhs2 (stmt);
9016 bool sop = false;
9017 tree val;
9019 val = (vrp_evaluate_conditional_warnv_with_ops_using_ranges
9020 (LE_EXPR, op0, op1, &sop));
9021 if (!val)
9023 sop = false;
9024 val = (vrp_evaluate_conditional_warnv_with_ops_using_ranges
9025 (LT_EXPR, op0, op1, &sop));
9028 if (val)
9030 if (sop && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC))
9032 location_t location;
9034 if (!gimple_has_location (stmt))
9035 location = input_location;
9036 else
9037 location = gimple_location (stmt);
9038 warning_at (location, OPT_Wstrict_overflow,
9039 "assuming signed overflow does not occur when "
9040 "simplifying %<min/max (X,Y)%> to %<X%> or %<Y%>");
9043 /* VAL == TRUE -> OP0 < or <= op1
9044 VAL == FALSE -> OP0 > or >= op1. */
9045 tree res = ((gimple_assign_rhs_code (stmt) == MAX_EXPR)
9046 == integer_zerop (val)) ? op0 : op1;
9047 gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
9048 gimple_assign_set_rhs_from_tree (&gsi, res);
9049 update_stmt (stmt);
9050 return true;
9053 return false;
9056 /* If the operand to an ABS_EXPR is >= 0, then eliminate the
9057 ABS_EXPR. If the operand is <= 0, then simplify the
9058 ABS_EXPR into a NEGATE_EXPR. */
9060 static bool
9061 simplify_abs_using_ranges (gimple *stmt)
9063 tree op = gimple_assign_rhs1 (stmt);
9064 value_range *vr = get_value_range (op);
9066 if (vr)
9068 tree val = NULL;
9069 bool sop = false;
9071 val = compare_range_with_value (LE_EXPR, vr, integer_zero_node, &sop);
9072 if (!val)
9074 /* The range is neither <= 0 nor > 0. Now see if it is
9075 either < 0 or >= 0. */
9076 sop = false;
9077 val = compare_range_with_value (LT_EXPR, vr, integer_zero_node,
9078 &sop);
9081 if (val)
9083 if (sop && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC))
9085 location_t location;
9087 if (!gimple_has_location (stmt))
9088 location = input_location;
9089 else
9090 location = gimple_location (stmt);
9091 warning_at (location, OPT_Wstrict_overflow,
9092 "assuming signed overflow does not occur when "
9093 "simplifying %<abs (X)%> to %<X%> or %<-X%>");
9096 gimple_assign_set_rhs1 (stmt, op);
9097 if (integer_zerop (val))
9098 gimple_assign_set_rhs_code (stmt, SSA_NAME);
9099 else
9100 gimple_assign_set_rhs_code (stmt, NEGATE_EXPR);
9101 update_stmt (stmt);
9102 return true;
9106 return false;
9109 /* Optimize away redundant BIT_AND_EXPR and BIT_IOR_EXPR.
9110 If all the bits that are being cleared by & are already
9111 known to be zero from VR, or all the bits that are being
9112 set by | are already known to be one from VR, the bit
9113 operation is redundant. */
9115 static bool
9116 simplify_bit_ops_using_ranges (gimple_stmt_iterator *gsi, gimple *stmt)
9118 tree op0 = gimple_assign_rhs1 (stmt);
9119 tree op1 = gimple_assign_rhs2 (stmt);
9120 tree op = NULL_TREE;
9121 value_range vr0 = VR_INITIALIZER;
9122 value_range vr1 = VR_INITIALIZER;
9123 wide_int may_be_nonzero0, may_be_nonzero1;
9124 wide_int must_be_nonzero0, must_be_nonzero1;
9125 wide_int mask;
9127 if (TREE_CODE (op0) == SSA_NAME)
9128 vr0 = *(get_value_range (op0));
9129 else if (is_gimple_min_invariant (op0))
9130 set_value_range_to_value (&vr0, op0, NULL);
9131 else
9132 return false;
9134 if (TREE_CODE (op1) == SSA_NAME)
9135 vr1 = *(get_value_range (op1));
9136 else if (is_gimple_min_invariant (op1))
9137 set_value_range_to_value (&vr1, op1, NULL);
9138 else
9139 return false;
9141 if (!zero_nonzero_bits_from_vr (TREE_TYPE (op0), &vr0, &may_be_nonzero0,
9142 &must_be_nonzero0))
9143 return false;
9144 if (!zero_nonzero_bits_from_vr (TREE_TYPE (op1), &vr1, &may_be_nonzero1,
9145 &must_be_nonzero1))
9146 return false;
9148 switch (gimple_assign_rhs_code (stmt))
9150 case BIT_AND_EXPR:
9151 mask = may_be_nonzero0.and_not (must_be_nonzero1);
9152 if (mask == 0)
9154 op = op0;
9155 break;
9157 mask = may_be_nonzero1.and_not (must_be_nonzero0);
9158 if (mask == 0)
9160 op = op1;
9161 break;
9163 break;
9164 case BIT_IOR_EXPR:
9165 mask = may_be_nonzero0.and_not (must_be_nonzero1);
9166 if (mask == 0)
9168 op = op1;
9169 break;
9171 mask = may_be_nonzero1.and_not (must_be_nonzero0);
9172 if (mask == 0)
9174 op = op0;
9175 break;
9177 break;
9178 default:
9179 gcc_unreachable ();
9182 if (op == NULL_TREE)
9183 return false;
9185 gimple_assign_set_rhs_with_ops (gsi, TREE_CODE (op), op);
9186 update_stmt (gsi_stmt (*gsi));
9187 return true;
9190 /* We are comparing trees OP0 and OP1 using COND_CODE. OP0 has
9191 a known value range VR.
9193 If there is one and only one value which will satisfy the
9194 conditional, then return that value. Else return NULL.
9196 If signed overflow must be undefined for the value to satisfy
9197 the conditional, then set *STRICT_OVERFLOW_P to true. */
9199 static tree
9200 test_for_singularity (enum tree_code cond_code, tree op0,
9201 tree op1, value_range *vr,
9202 bool *strict_overflow_p)
9204 tree min = NULL;
9205 tree max = NULL;
9207 /* Extract minimum/maximum values which satisfy the conditional as it was
9208 written. */
9209 if (cond_code == LE_EXPR || cond_code == LT_EXPR)
9211 /* This should not be negative infinity; there is no overflow
9212 here. */
9213 min = TYPE_MIN_VALUE (TREE_TYPE (op0));
9215 max = op1;
9216 if (cond_code == LT_EXPR && !is_overflow_infinity (max))
9218 tree one = build_int_cst (TREE_TYPE (op0), 1);
9219 max = fold_build2 (MINUS_EXPR, TREE_TYPE (op0), max, one);
9220 if (EXPR_P (max))
9221 TREE_NO_WARNING (max) = 1;
9224 else if (cond_code == GE_EXPR || cond_code == GT_EXPR)
9226 /* This should not be positive infinity; there is no overflow
9227 here. */
9228 max = TYPE_MAX_VALUE (TREE_TYPE (op0));
9230 min = op1;
9231 if (cond_code == GT_EXPR && !is_overflow_infinity (min))
9233 tree one = build_int_cst (TREE_TYPE (op0), 1);
9234 min = fold_build2 (PLUS_EXPR, TREE_TYPE (op0), min, one);
9235 if (EXPR_P (min))
9236 TREE_NO_WARNING (min) = 1;
9240 /* Now refine the minimum and maximum values using any
9241 value range information we have for op0. */
9242 if (min && max)
9244 if (compare_values (vr->min, min) == 1)
9245 min = vr->min;
9246 if (compare_values (vr->max, max) == -1)
9247 max = vr->max;
9249 /* If the new min/max values have converged to a single value,
9250 then there is only one value which can satisfy the condition,
9251 return that value. */
9252 if (operand_equal_p (min, max, 0) && is_gimple_min_invariant (min))
9254 if ((cond_code == LE_EXPR || cond_code == LT_EXPR)
9255 && is_overflow_infinity (vr->max))
9256 *strict_overflow_p = true;
9257 if ((cond_code == GE_EXPR || cond_code == GT_EXPR)
9258 && is_overflow_infinity (vr->min))
9259 *strict_overflow_p = true;
9261 return min;
9264 return NULL;
9267 /* Return whether the value range *VR fits in an integer type specified
9268 by PRECISION and UNSIGNED_P. */
9270 static bool
9271 range_fits_type_p (value_range *vr, unsigned dest_precision, signop dest_sgn)
9273 tree src_type;
9274 unsigned src_precision;
9275 widest_int tem;
9276 signop src_sgn;
9278 /* We can only handle integral and pointer types. */
9279 src_type = TREE_TYPE (vr->min);
9280 if (!INTEGRAL_TYPE_P (src_type)
9281 && !POINTER_TYPE_P (src_type))
9282 return false;
9284 /* An extension is fine unless VR is SIGNED and dest_sgn is UNSIGNED,
9285 and so is an identity transform. */
9286 src_precision = TYPE_PRECISION (TREE_TYPE (vr->min));
9287 src_sgn = TYPE_SIGN (src_type);
9288 if ((src_precision < dest_precision
9289 && !(dest_sgn == UNSIGNED && src_sgn == SIGNED))
9290 || (src_precision == dest_precision && src_sgn == dest_sgn))
9291 return true;
9293 /* Now we can only handle ranges with constant bounds. */
9294 if (vr->type != VR_RANGE
9295 || TREE_CODE (vr->min) != INTEGER_CST
9296 || TREE_CODE (vr->max) != INTEGER_CST)
9297 return false;
9299 /* For sign changes, the MSB of the wide_int has to be clear.
9300 An unsigned value with its MSB set cannot be represented by
9301 a signed wide_int, while a negative value cannot be represented
9302 by an unsigned wide_int. */
9303 if (src_sgn != dest_sgn
9304 && (wi::lts_p (vr->min, 0) || wi::lts_p (vr->max, 0)))
9305 return false;
9307 /* Then we can perform the conversion on both ends and compare
9308 the result for equality. */
9309 tem = wi::ext (wi::to_widest (vr->min), dest_precision, dest_sgn);
9310 if (tem != wi::to_widest (vr->min))
9311 return false;
9312 tem = wi::ext (wi::to_widest (vr->max), dest_precision, dest_sgn);
9313 if (tem != wi::to_widest (vr->max))
9314 return false;
9316 return true;
9319 /* Simplify a conditional using a relational operator to an equality
9320 test if the range information indicates only one value can satisfy
9321 the original conditional. */
9323 static bool
9324 simplify_cond_using_ranges (gcond *stmt)
9326 tree op0 = gimple_cond_lhs (stmt);
9327 tree op1 = gimple_cond_rhs (stmt);
9328 enum tree_code cond_code = gimple_cond_code (stmt);
9330 if (cond_code != NE_EXPR
9331 && cond_code != EQ_EXPR
9332 && TREE_CODE (op0) == SSA_NAME
9333 && INTEGRAL_TYPE_P (TREE_TYPE (op0))
9334 && is_gimple_min_invariant (op1))
9336 value_range *vr = get_value_range (op0);
9338 /* If we have range information for OP0, then we might be
9339 able to simplify this conditional. */
9340 if (vr->type == VR_RANGE)
9342 enum warn_strict_overflow_code wc = WARN_STRICT_OVERFLOW_COMPARISON;
9343 bool sop = false;
9344 tree new_tree = test_for_singularity (cond_code, op0, op1, vr, &sop);
9346 if (new_tree
9347 && (!sop || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0))))
9349 if (dump_file)
9351 fprintf (dump_file, "Simplified relational ");
9352 print_gimple_stmt (dump_file, stmt, 0, 0);
9353 fprintf (dump_file, " into ");
9356 gimple_cond_set_code (stmt, EQ_EXPR);
9357 gimple_cond_set_lhs (stmt, op0);
9358 gimple_cond_set_rhs (stmt, new_tree);
9360 update_stmt (stmt);
9362 if (dump_file)
9364 print_gimple_stmt (dump_file, stmt, 0, 0);
9365 fprintf (dump_file, "\n");
9368 if (sop && issue_strict_overflow_warning (wc))
9370 location_t location = input_location;
9371 if (gimple_has_location (stmt))
9372 location = gimple_location (stmt);
9374 warning_at (location, OPT_Wstrict_overflow,
9375 "assuming signed overflow does not occur when "
9376 "simplifying conditional");
9379 return true;
9382 /* Try again after inverting the condition. We only deal
9383 with integral types here, so no need to worry about
9384 issues with inverting FP comparisons. */
9385 sop = false;
9386 new_tree = test_for_singularity
9387 (invert_tree_comparison (cond_code, false),
9388 op0, op1, vr, &sop);
9390 if (new_tree
9391 && (!sop || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0))))
9393 if (dump_file)
9395 fprintf (dump_file, "Simplified relational ");
9396 print_gimple_stmt (dump_file, stmt, 0, 0);
9397 fprintf (dump_file, " into ");
9400 gimple_cond_set_code (stmt, NE_EXPR);
9401 gimple_cond_set_lhs (stmt, op0);
9402 gimple_cond_set_rhs (stmt, new_tree);
9404 update_stmt (stmt);
9406 if (dump_file)
9408 print_gimple_stmt (dump_file, stmt, 0, 0);
9409 fprintf (dump_file, "\n");
9412 if (sop && issue_strict_overflow_warning (wc))
9414 location_t location = input_location;
9415 if (gimple_has_location (stmt))
9416 location = gimple_location (stmt);
9418 warning_at (location, OPT_Wstrict_overflow,
9419 "assuming signed overflow does not occur when "
9420 "simplifying conditional");
9423 return true;
9428 /* If we have a comparison of an SSA_NAME (OP0) against a constant,
9429 see if OP0 was set by a type conversion where the source of
9430 the conversion is another SSA_NAME with a range that fits
9431 into the range of OP0's type.
9433 If so, the conversion is redundant as the earlier SSA_NAME can be
9434 used for the comparison directly if we just massage the constant in the
9435 comparison. */
9436 if (TREE_CODE (op0) == SSA_NAME
9437 && TREE_CODE (op1) == INTEGER_CST)
9439 gimple *def_stmt = SSA_NAME_DEF_STMT (op0);
9440 tree innerop;
9442 if (!is_gimple_assign (def_stmt)
9443 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt)))
9444 return false;
9446 innerop = gimple_assign_rhs1 (def_stmt);
9448 if (TREE_CODE (innerop) == SSA_NAME
9449 && !POINTER_TYPE_P (TREE_TYPE (innerop))
9450 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (innerop)
9451 && desired_pro_or_demotion_p (TREE_TYPE (innerop), TREE_TYPE (op0)))
9453 value_range *vr = get_value_range (innerop);
9455 if (range_int_cst_p (vr)
9456 && range_fits_type_p (vr,
9457 TYPE_PRECISION (TREE_TYPE (op0)),
9458 TYPE_SIGN (TREE_TYPE (op0)))
9459 && int_fits_type_p (op1, TREE_TYPE (innerop))
9460 /* The range must not have overflowed, or if it did overflow
9461 we must not be wrapping/trapping overflow and optimizing
9462 with strict overflow semantics. */
9463 && ((!is_negative_overflow_infinity (vr->min)
9464 && !is_positive_overflow_infinity (vr->max))
9465 || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (innerop))))
9467 /* If the range overflowed and the user has asked for warnings
9468 when strict overflow semantics were used to optimize code,
9469 issue an appropriate warning. */
9470 if (cond_code != EQ_EXPR && cond_code != NE_EXPR
9471 && (is_negative_overflow_infinity (vr->min)
9472 || is_positive_overflow_infinity (vr->max))
9473 && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_CONDITIONAL))
9475 location_t location;
9477 if (!gimple_has_location (stmt))
9478 location = input_location;
9479 else
9480 location = gimple_location (stmt);
9481 warning_at (location, OPT_Wstrict_overflow,
9482 "assuming signed overflow does not occur when "
9483 "simplifying conditional");
9486 tree newconst = fold_convert (TREE_TYPE (innerop), op1);
9487 gimple_cond_set_lhs (stmt, innerop);
9488 gimple_cond_set_rhs (stmt, newconst);
9489 return true;
9494 return false;
9497 /* Simplify a switch statement using the value range of the switch
9498 argument. */
9500 static bool
9501 simplify_switch_using_ranges (gswitch *stmt)
9503 tree op = gimple_switch_index (stmt);
9504 value_range *vr;
9505 bool take_default;
9506 edge e;
9507 edge_iterator ei;
9508 size_t i = 0, j = 0, n, n2;
9509 tree vec2;
9510 switch_update su;
9511 size_t k = 1, l = 0;
9513 if (TREE_CODE (op) == SSA_NAME)
9515 vr = get_value_range (op);
9517 /* We can only handle integer ranges. */
9518 if ((vr->type != VR_RANGE
9519 && vr->type != VR_ANTI_RANGE)
9520 || symbolic_range_p (vr))
9521 return false;
9523 /* Find case label for min/max of the value range. */
9524 take_default = !find_case_label_ranges (stmt, vr, &i, &j, &k, &l);
9526 else if (TREE_CODE (op) == INTEGER_CST)
9528 take_default = !find_case_label_index (stmt, 1, op, &i);
9529 if (take_default)
9531 i = 1;
9532 j = 0;
9534 else
9536 j = i;
9539 else
9540 return false;
9542 n = gimple_switch_num_labels (stmt);
9544 /* Bail out if this is just all edges taken. */
9545 if (i == 1
9546 && j == n - 1
9547 && take_default)
9548 return false;
9550 /* Build a new vector of taken case labels. */
9551 vec2 = make_tree_vec (j - i + 1 + l - k + 1 + (int)take_default);
9552 n2 = 0;
9554 /* Add the default edge, if necessary. */
9555 if (take_default)
9556 TREE_VEC_ELT (vec2, n2++) = gimple_switch_default_label (stmt);
9558 for (; i <= j; ++i, ++n2)
9559 TREE_VEC_ELT (vec2, n2) = gimple_switch_label (stmt, i);
9561 for (; k <= l; ++k, ++n2)
9562 TREE_VEC_ELT (vec2, n2) = gimple_switch_label (stmt, k);
9564 /* Mark needed edges. */
9565 for (i = 0; i < n2; ++i)
9567 e = find_edge (gimple_bb (stmt),
9568 label_to_block (CASE_LABEL (TREE_VEC_ELT (vec2, i))));
9569 e->aux = (void *)-1;
9572 /* Queue not needed edges for later removal. */
9573 FOR_EACH_EDGE (e, ei, gimple_bb (stmt)->succs)
9575 if (e->aux == (void *)-1)
9577 e->aux = NULL;
9578 continue;
9581 if (dump_file && (dump_flags & TDF_DETAILS))
9583 fprintf (dump_file, "removing unreachable case label\n");
9585 to_remove_edges.safe_push (e);
9586 e->flags &= ~EDGE_EXECUTABLE;
9589 /* And queue an update for the stmt. */
9590 su.stmt = stmt;
9591 su.vec = vec2;
9592 to_update_switch_stmts.safe_push (su);
9593 return false;
9596 /* Simplify an integral conversion from an SSA name in STMT. */
9598 static bool
9599 simplify_conversion_using_ranges (gimple *stmt)
9601 tree innerop, middleop, finaltype;
9602 gimple *def_stmt;
9603 signop inner_sgn, middle_sgn, final_sgn;
9604 unsigned inner_prec, middle_prec, final_prec;
9605 widest_int innermin, innermed, innermax, middlemin, middlemed, middlemax;
9607 finaltype = TREE_TYPE (gimple_assign_lhs (stmt));
9608 if (!INTEGRAL_TYPE_P (finaltype))
9609 return false;
9610 middleop = gimple_assign_rhs1 (stmt);
9611 def_stmt = SSA_NAME_DEF_STMT (middleop);
9612 if (!is_gimple_assign (def_stmt)
9613 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt)))
9614 return false;
9615 innerop = gimple_assign_rhs1 (def_stmt);
9616 if (TREE_CODE (innerop) != SSA_NAME
9617 || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (innerop))
9618 return false;
9620 /* Get the value-range of the inner operand. Use get_range_info in
9621 case innerop was created during substitute-and-fold. */
9622 wide_int imin, imax;
9623 if (!INTEGRAL_TYPE_P (TREE_TYPE (innerop))
9624 || get_range_info (innerop, &imin, &imax) != VR_RANGE)
9625 return false;
9626 innermin = widest_int::from (imin, TYPE_SIGN (TREE_TYPE (innerop)));
9627 innermax = widest_int::from (imax, TYPE_SIGN (TREE_TYPE (innerop)));
9629 /* Simulate the conversion chain to check if the result is equal if
9630 the middle conversion is removed. */
9631 inner_prec = TYPE_PRECISION (TREE_TYPE (innerop));
9632 middle_prec = TYPE_PRECISION (TREE_TYPE (middleop));
9633 final_prec = TYPE_PRECISION (finaltype);
9635 /* If the first conversion is not injective, the second must not
9636 be widening. */
9637 if (wi::gtu_p (innermax - innermin,
9638 wi::mask <widest_int> (middle_prec, false))
9639 && middle_prec < final_prec)
9640 return false;
9641 /* We also want a medium value so that we can track the effect that
9642 narrowing conversions with sign change have. */
9643 inner_sgn = TYPE_SIGN (TREE_TYPE (innerop));
9644 if (inner_sgn == UNSIGNED)
9645 innermed = wi::shifted_mask <widest_int> (1, inner_prec - 1, false);
9646 else
9647 innermed = 0;
9648 if (wi::cmp (innermin, innermed, inner_sgn) >= 0
9649 || wi::cmp (innermed, innermax, inner_sgn) >= 0)
9650 innermed = innermin;
9652 middle_sgn = TYPE_SIGN (TREE_TYPE (middleop));
9653 middlemin = wi::ext (innermin, middle_prec, middle_sgn);
9654 middlemed = wi::ext (innermed, middle_prec, middle_sgn);
9655 middlemax = wi::ext (innermax, middle_prec, middle_sgn);
9657 /* Require that the final conversion applied to both the original
9658 and the intermediate range produces the same result. */
9659 final_sgn = TYPE_SIGN (finaltype);
9660 if (wi::ext (middlemin, final_prec, final_sgn)
9661 != wi::ext (innermin, final_prec, final_sgn)
9662 || wi::ext (middlemed, final_prec, final_sgn)
9663 != wi::ext (innermed, final_prec, final_sgn)
9664 || wi::ext (middlemax, final_prec, final_sgn)
9665 != wi::ext (innermax, final_prec, final_sgn))
9666 return false;
9668 gimple_assign_set_rhs1 (stmt, innerop);
9669 update_stmt (stmt);
9670 return true;
9673 /* Simplify a conversion from integral SSA name to float in STMT. */
9675 static bool
9676 simplify_float_conversion_using_ranges (gimple_stmt_iterator *gsi,
9677 gimple *stmt)
9679 tree rhs1 = gimple_assign_rhs1 (stmt);
9680 value_range *vr = get_value_range (rhs1);
9681 machine_mode fltmode = TYPE_MODE (TREE_TYPE (gimple_assign_lhs (stmt)));
9682 machine_mode mode;
9683 tree tem;
9684 gassign *conv;
9686 /* We can only handle constant ranges. */
9687 if (vr->type != VR_RANGE
9688 || TREE_CODE (vr->min) != INTEGER_CST
9689 || TREE_CODE (vr->max) != INTEGER_CST)
9690 return false;
9692 /* First check if we can use a signed type in place of an unsigned. */
9693 if (TYPE_UNSIGNED (TREE_TYPE (rhs1))
9694 && (can_float_p (fltmode, TYPE_MODE (TREE_TYPE (rhs1)), 0)
9695 != CODE_FOR_nothing)
9696 && range_fits_type_p (vr, TYPE_PRECISION (TREE_TYPE (rhs1)), SIGNED))
9697 mode = TYPE_MODE (TREE_TYPE (rhs1));
9698 /* If we can do the conversion in the current input mode do nothing. */
9699 else if (can_float_p (fltmode, TYPE_MODE (TREE_TYPE (rhs1)),
9700 TYPE_UNSIGNED (TREE_TYPE (rhs1))) != CODE_FOR_nothing)
9701 return false;
9702 /* Otherwise search for a mode we can use, starting from the narrowest
9703 integer mode available. */
9704 else
9706 mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
9709 /* If we cannot do a signed conversion to float from mode
9710 or if the value-range does not fit in the signed type
9711 try with a wider mode. */
9712 if (can_float_p (fltmode, mode, 0) != CODE_FOR_nothing
9713 && range_fits_type_p (vr, GET_MODE_PRECISION (mode), SIGNED))
9714 break;
9716 mode = GET_MODE_WIDER_MODE (mode);
9717 /* But do not widen the input. Instead leave that to the
9718 optabs expansion code. */
9719 if (GET_MODE_PRECISION (mode) > TYPE_PRECISION (TREE_TYPE (rhs1)))
9720 return false;
9722 while (mode != VOIDmode);
9723 if (mode == VOIDmode)
9724 return false;
9727 /* It works, insert a truncation or sign-change before the
9728 float conversion. */
9729 tem = make_ssa_name (build_nonstandard_integer_type
9730 (GET_MODE_PRECISION (mode), 0));
9731 conv = gimple_build_assign (tem, NOP_EXPR, rhs1);
9732 gsi_insert_before (gsi, conv, GSI_SAME_STMT);
9733 gimple_assign_set_rhs1 (stmt, tem);
9734 update_stmt (stmt);
9736 return true;
9739 /* Simplify an internal fn call using ranges if possible. */
9741 static bool
9742 simplify_internal_call_using_ranges (gimple_stmt_iterator *gsi, gimple *stmt)
9744 enum tree_code subcode;
9745 bool is_ubsan = false;
9746 bool ovf = false;
9747 switch (gimple_call_internal_fn (stmt))
9749 case IFN_UBSAN_CHECK_ADD:
9750 subcode = PLUS_EXPR;
9751 is_ubsan = true;
9752 break;
9753 case IFN_UBSAN_CHECK_SUB:
9754 subcode = MINUS_EXPR;
9755 is_ubsan = true;
9756 break;
9757 case IFN_UBSAN_CHECK_MUL:
9758 subcode = MULT_EXPR;
9759 is_ubsan = true;
9760 break;
9761 case IFN_ADD_OVERFLOW:
9762 subcode = PLUS_EXPR;
9763 break;
9764 case IFN_SUB_OVERFLOW:
9765 subcode = MINUS_EXPR;
9766 break;
9767 case IFN_MUL_OVERFLOW:
9768 subcode = MULT_EXPR;
9769 break;
9770 default:
9771 return false;
9774 tree op0 = gimple_call_arg (stmt, 0);
9775 tree op1 = gimple_call_arg (stmt, 1);
9776 tree type;
9777 if (is_ubsan)
9778 type = TREE_TYPE (op0);
9779 else if (gimple_call_lhs (stmt) == NULL_TREE)
9780 return false;
9781 else
9782 type = TREE_TYPE (TREE_TYPE (gimple_call_lhs (stmt)));
9783 if (!check_for_binary_op_overflow (subcode, type, op0, op1, &ovf)
9784 || (is_ubsan && ovf))
9785 return false;
9787 gimple *g;
9788 location_t loc = gimple_location (stmt);
9789 if (is_ubsan)
9790 g = gimple_build_assign (gimple_call_lhs (stmt), subcode, op0, op1);
9791 else
9793 int prec = TYPE_PRECISION (type);
9794 tree utype = type;
9795 if (ovf
9796 || !useless_type_conversion_p (type, TREE_TYPE (op0))
9797 || !useless_type_conversion_p (type, TREE_TYPE (op1)))
9798 utype = build_nonstandard_integer_type (prec, 1);
9799 if (TREE_CODE (op0) == INTEGER_CST)
9800 op0 = fold_convert (utype, op0);
9801 else if (!useless_type_conversion_p (utype, TREE_TYPE (op0)))
9803 g = gimple_build_assign (make_ssa_name (utype), NOP_EXPR, op0);
9804 gimple_set_location (g, loc);
9805 gsi_insert_before (gsi, g, GSI_SAME_STMT);
9806 op0 = gimple_assign_lhs (g);
9808 if (TREE_CODE (op1) == INTEGER_CST)
9809 op1 = fold_convert (utype, op1);
9810 else if (!useless_type_conversion_p (utype, TREE_TYPE (op1)))
9812 g = gimple_build_assign (make_ssa_name (utype), NOP_EXPR, op1);
9813 gimple_set_location (g, loc);
9814 gsi_insert_before (gsi, g, GSI_SAME_STMT);
9815 op1 = gimple_assign_lhs (g);
9817 g = gimple_build_assign (make_ssa_name (utype), subcode, op0, op1);
9818 gimple_set_location (g, loc);
9819 gsi_insert_before (gsi, g, GSI_SAME_STMT);
9820 if (utype != type)
9822 g = gimple_build_assign (make_ssa_name (type), NOP_EXPR,
9823 gimple_assign_lhs (g));
9824 gimple_set_location (g, loc);
9825 gsi_insert_before (gsi, g, GSI_SAME_STMT);
9827 g = gimple_build_assign (gimple_call_lhs (stmt), COMPLEX_EXPR,
9828 gimple_assign_lhs (g),
9829 build_int_cst (type, ovf));
9831 gimple_set_location (g, loc);
9832 gsi_replace (gsi, g, false);
9833 return true;
9836 /* Simplify STMT using ranges if possible. */
9838 static bool
9839 simplify_stmt_using_ranges (gimple_stmt_iterator *gsi)
9841 gimple *stmt = gsi_stmt (*gsi);
9842 if (is_gimple_assign (stmt))
9844 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
9845 tree rhs1 = gimple_assign_rhs1 (stmt);
9847 switch (rhs_code)
9849 case EQ_EXPR:
9850 case NE_EXPR:
9851 /* Transform EQ_EXPR, NE_EXPR into BIT_XOR_EXPR or identity
9852 if the RHS is zero or one, and the LHS are known to be boolean
9853 values. */
9854 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
9855 return simplify_truth_ops_using_ranges (gsi, stmt);
9856 break;
9858 /* Transform TRUNC_DIV_EXPR and TRUNC_MOD_EXPR into RSHIFT_EXPR
9859 and BIT_AND_EXPR respectively if the first operand is greater
9860 than zero and the second operand is an exact power of two.
9861 Also optimize TRUNC_MOD_EXPR away if the second operand is
9862 constant and the first operand already has the right value
9863 range. */
9864 case TRUNC_DIV_EXPR:
9865 case TRUNC_MOD_EXPR:
9866 if (TREE_CODE (rhs1) == SSA_NAME
9867 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
9868 return simplify_div_or_mod_using_ranges (gsi, stmt);
9869 break;
9871 /* Transform ABS (X) into X or -X as appropriate. */
9872 case ABS_EXPR:
9873 if (TREE_CODE (rhs1) == SSA_NAME
9874 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
9875 return simplify_abs_using_ranges (stmt);
9876 break;
9878 case BIT_AND_EXPR:
9879 case BIT_IOR_EXPR:
9880 /* Optimize away BIT_AND_EXPR and BIT_IOR_EXPR
9881 if all the bits being cleared are already cleared or
9882 all the bits being set are already set. */
9883 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
9884 return simplify_bit_ops_using_ranges (gsi, stmt);
9885 break;
9887 CASE_CONVERT:
9888 if (TREE_CODE (rhs1) == SSA_NAME
9889 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
9890 return simplify_conversion_using_ranges (stmt);
9891 break;
9893 case FLOAT_EXPR:
9894 if (TREE_CODE (rhs1) == SSA_NAME
9895 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
9896 return simplify_float_conversion_using_ranges (gsi, stmt);
9897 break;
9899 case MIN_EXPR:
9900 case MAX_EXPR:
9901 return simplify_min_or_max_using_ranges (stmt);
9902 break;
9904 default:
9905 break;
9908 else if (gimple_code (stmt) == GIMPLE_COND)
9909 return simplify_cond_using_ranges (as_a <gcond *> (stmt));
9910 else if (gimple_code (stmt) == GIMPLE_SWITCH)
9911 return simplify_switch_using_ranges (as_a <gswitch *> (stmt));
9912 else if (is_gimple_call (stmt)
9913 && gimple_call_internal_p (stmt))
9914 return simplify_internal_call_using_ranges (gsi, stmt);
9916 return false;
9919 /* If the statement pointed by SI has a predicate whose value can be
9920 computed using the value range information computed by VRP, compute
9921 its value and return true. Otherwise, return false. */
9923 static bool
9924 fold_predicate_in (gimple_stmt_iterator *si)
9926 bool assignment_p = false;
9927 tree val;
9928 gimple *stmt = gsi_stmt (*si);
9930 if (is_gimple_assign (stmt)
9931 && TREE_CODE_CLASS (gimple_assign_rhs_code (stmt)) == tcc_comparison)
9933 assignment_p = true;
9934 val = vrp_evaluate_conditional (gimple_assign_rhs_code (stmt),
9935 gimple_assign_rhs1 (stmt),
9936 gimple_assign_rhs2 (stmt),
9937 stmt);
9939 else if (gcond *cond_stmt = dyn_cast <gcond *> (stmt))
9940 val = vrp_evaluate_conditional (gimple_cond_code (cond_stmt),
9941 gimple_cond_lhs (cond_stmt),
9942 gimple_cond_rhs (cond_stmt),
9943 stmt);
9944 else
9945 return false;
9947 if (val)
9949 if (assignment_p)
9950 val = fold_convert (gimple_expr_type (stmt), val);
9952 if (dump_file)
9954 fprintf (dump_file, "Folding predicate ");
9955 print_gimple_expr (dump_file, stmt, 0, 0);
9956 fprintf (dump_file, " to ");
9957 print_generic_expr (dump_file, val, 0);
9958 fprintf (dump_file, "\n");
9961 if (is_gimple_assign (stmt))
9962 gimple_assign_set_rhs_from_tree (si, val);
9963 else
9965 gcc_assert (gimple_code (stmt) == GIMPLE_COND);
9966 gcond *cond_stmt = as_a <gcond *> (stmt);
9967 if (integer_zerop (val))
9968 gimple_cond_make_false (cond_stmt);
9969 else if (integer_onep (val))
9970 gimple_cond_make_true (cond_stmt);
9971 else
9972 gcc_unreachable ();
9975 return true;
9978 return false;
9981 /* Callback for substitute_and_fold folding the stmt at *SI. */
9983 static bool
9984 vrp_fold_stmt (gimple_stmt_iterator *si)
9986 if (fold_predicate_in (si))
9987 return true;
9989 return simplify_stmt_using_ranges (si);
9992 /* Unwindable const/copy equivalences. */
9993 const_and_copies *equiv_stack;
9995 /* A trivial wrapper so that we can present the generic jump threading
9996 code with a simple API for simplifying statements. STMT is the
9997 statement we want to simplify, WITHIN_STMT provides the location
9998 for any overflow warnings. */
10000 static tree
10001 simplify_stmt_for_jump_threading (gimple *stmt, gimple *within_stmt,
10002 class avail_exprs_stack *avail_exprs_stack ATTRIBUTE_UNUSED)
10004 if (gcond *cond_stmt = dyn_cast <gcond *> (stmt))
10005 return vrp_evaluate_conditional (gimple_cond_code (cond_stmt),
10006 gimple_cond_lhs (cond_stmt),
10007 gimple_cond_rhs (cond_stmt),
10008 within_stmt);
10010 if (gassign *assign_stmt = dyn_cast <gassign *> (stmt))
10012 value_range new_vr = VR_INITIALIZER;
10013 tree lhs = gimple_assign_lhs (assign_stmt);
10015 if (TREE_CODE (lhs) == SSA_NAME
10016 && (INTEGRAL_TYPE_P (TREE_TYPE (lhs))
10017 || POINTER_TYPE_P (TREE_TYPE (lhs))))
10019 extract_range_from_assignment (&new_vr, assign_stmt);
10020 if (range_int_cst_singleton_p (&new_vr))
10021 return new_vr.min;
10025 return NULL_TREE;
10028 /* Blocks which have more than one predecessor and more than
10029 one successor present jump threading opportunities, i.e.,
10030 when the block is reached from a specific predecessor, we
10031 may be able to determine which of the outgoing edges will
10032 be traversed. When this optimization applies, we are able
10033 to avoid conditionals at runtime and we may expose secondary
10034 optimization opportunities.
10036 This routine is effectively a driver for the generic jump
10037 threading code. It basically just presents the generic code
10038 with edges that may be suitable for jump threading.
10040 Unlike DOM, we do not iterate VRP if jump threading was successful.
10041 While iterating may expose new opportunities for VRP, it is expected
10042 those opportunities would be very limited and the compile time cost
10043 to expose those opportunities would be significant.
10045 As jump threading opportunities are discovered, they are registered
10046 for later realization. */
10048 static void
10049 identify_jump_threads (void)
10051 basic_block bb;
10052 gcond *dummy;
10053 int i;
10054 edge e;
10056 /* Ugh. When substituting values earlier in this pass we can
10057 wipe the dominance information. So rebuild the dominator
10058 information as we need it within the jump threading code. */
10059 calculate_dominance_info (CDI_DOMINATORS);
10061 /* We do not allow VRP information to be used for jump threading
10062 across a back edge in the CFG. Otherwise it becomes too
10063 difficult to avoid eliminating loop exit tests. Of course
10064 EDGE_DFS_BACK is not accurate at this time so we have to
10065 recompute it. */
10066 mark_dfs_back_edges ();
10068 /* Do not thread across edges we are about to remove. Just marking
10069 them as EDGE_IGNORE will do. */
10070 FOR_EACH_VEC_ELT (to_remove_edges, i, e)
10071 e->flags |= EDGE_IGNORE;
10073 /* Allocate our unwinder stack to unwind any temporary equivalences
10074 that might be recorded. */
10075 equiv_stack = new const_and_copies ();
10077 /* To avoid lots of silly node creation, we create a single
10078 conditional and just modify it in-place when attempting to
10079 thread jumps. */
10080 dummy = gimple_build_cond (EQ_EXPR,
10081 integer_zero_node, integer_zero_node,
10082 NULL, NULL);
10084 /* Walk through all the blocks finding those which present a
10085 potential jump threading opportunity. We could set this up
10086 as a dominator walker and record data during the walk, but
10087 I doubt it's worth the effort for the classes of jump
10088 threading opportunities we are trying to identify at this
10089 point in compilation. */
10090 FOR_EACH_BB_FN (bb, cfun)
10092 gimple *last;
10094 /* If the generic jump threading code does not find this block
10095 interesting, then there is nothing to do. */
10096 if (! potentially_threadable_block (bb))
10097 continue;
10099 last = last_stmt (bb);
10101 /* We're basically looking for a switch or any kind of conditional with
10102 integral or pointer type arguments. Note the type of the second
10103 argument will be the same as the first argument, so no need to
10104 check it explicitly.
10106 We also handle the case where there are no statements in the
10107 block. This come up with forwarder blocks that are not
10108 optimized away because they lead to a loop header. But we do
10109 want to thread through them as we can sometimes thread to the
10110 loop exit which is obviously profitable. */
10111 if (!last
10112 || gimple_code (last) == GIMPLE_SWITCH
10113 || (gimple_code (last) == GIMPLE_COND
10114 && TREE_CODE (gimple_cond_lhs (last)) == SSA_NAME
10115 && (INTEGRAL_TYPE_P (TREE_TYPE (gimple_cond_lhs (last)))
10116 || POINTER_TYPE_P (TREE_TYPE (gimple_cond_lhs (last))))
10117 && (TREE_CODE (gimple_cond_rhs (last)) == SSA_NAME
10118 || is_gimple_min_invariant (gimple_cond_rhs (last)))))
10120 edge_iterator ei;
10122 /* We've got a block with multiple predecessors and multiple
10123 successors which also ends in a suitable conditional or
10124 switch statement. For each predecessor, see if we can thread
10125 it to a specific successor. */
10126 FOR_EACH_EDGE (e, ei, bb->preds)
10128 /* Do not thread across edges marked to ignoreor abnormal
10129 edges in the CFG. */
10130 if (e->flags & (EDGE_IGNORE | EDGE_COMPLEX))
10131 continue;
10133 thread_across_edge (dummy, e, true, equiv_stack, NULL,
10134 simplify_stmt_for_jump_threading);
10139 /* Clear EDGE_IGNORE. */
10140 FOR_EACH_VEC_ELT (to_remove_edges, i, e)
10141 e->flags &= ~EDGE_IGNORE;
10143 /* We do not actually update the CFG or SSA graphs at this point as
10144 ASSERT_EXPRs are still in the IL and cfg cleanup code does not yet
10145 handle ASSERT_EXPRs gracefully. */
10148 /* We identified all the jump threading opportunities earlier, but could
10149 not transform the CFG at that time. This routine transforms the
10150 CFG and arranges for the dominator tree to be rebuilt if necessary.
10152 Note the SSA graph update will occur during the normal TODO
10153 processing by the pass manager. */
10154 static void
10155 finalize_jump_threads (void)
10157 thread_through_all_blocks (false);
10158 delete equiv_stack;
10162 /* Traverse all the blocks folding conditionals with known ranges. */
10164 static void
10165 vrp_finalize (bool warn_array_bounds_p)
10167 size_t i;
10169 values_propagated = true;
10171 if (dump_file)
10173 fprintf (dump_file, "\nValue ranges after VRP:\n\n");
10174 dump_all_value_ranges (dump_file);
10175 fprintf (dump_file, "\n");
10178 /* Set value range to non pointer SSA_NAMEs. */
10179 for (i = 0; i < num_vr_values; i++)
10180 if (vr_value[i])
10182 tree name = ssa_name (i);
10184 if (!name
10185 || POINTER_TYPE_P (TREE_TYPE (name))
10186 || (vr_value[i]->type == VR_VARYING)
10187 || (vr_value[i]->type == VR_UNDEFINED))
10188 continue;
10190 if ((TREE_CODE (vr_value[i]->min) == INTEGER_CST)
10191 && (TREE_CODE (vr_value[i]->max) == INTEGER_CST)
10192 && (vr_value[i]->type == VR_RANGE
10193 || vr_value[i]->type == VR_ANTI_RANGE))
10194 set_range_info (name, vr_value[i]->type, vr_value[i]->min,
10195 vr_value[i]->max);
10198 substitute_and_fold (op_with_constant_singleton_value_range,
10199 vrp_fold_stmt, false);
10201 if (warn_array_bounds && warn_array_bounds_p)
10202 check_all_array_refs ();
10204 /* We must identify jump threading opportunities before we release
10205 the datastructures built by VRP. */
10206 identify_jump_threads ();
10208 /* Free allocated memory. */
10209 for (i = 0; i < num_vr_values; i++)
10210 if (vr_value[i])
10212 BITMAP_FREE (vr_value[i]->equiv);
10213 free (vr_value[i]);
10216 free (vr_value);
10217 free (vr_phi_edge_counts);
10219 /* So that we can distinguish between VRP data being available
10220 and not available. */
10221 vr_value = NULL;
10222 vr_phi_edge_counts = NULL;
10226 /* Main entry point to VRP (Value Range Propagation). This pass is
10227 loosely based on J. R. C. Patterson, ``Accurate Static Branch
10228 Prediction by Value Range Propagation,'' in SIGPLAN Conference on
10229 Programming Language Design and Implementation, pp. 67-78, 1995.
10230 Also available at http://citeseer.ist.psu.edu/patterson95accurate.html
10232 This is essentially an SSA-CCP pass modified to deal with ranges
10233 instead of constants.
10235 While propagating ranges, we may find that two or more SSA name
10236 have equivalent, though distinct ranges. For instance,
10238 1 x_9 = p_3->a;
10239 2 p_4 = ASSERT_EXPR <p_3, p_3 != 0>
10240 3 if (p_4 == q_2)
10241 4 p_5 = ASSERT_EXPR <p_4, p_4 == q_2>;
10242 5 endif
10243 6 if (q_2)
10245 In the code above, pointer p_5 has range [q_2, q_2], but from the
10246 code we can also determine that p_5 cannot be NULL and, if q_2 had
10247 a non-varying range, p_5's range should also be compatible with it.
10249 These equivalences are created by two expressions: ASSERT_EXPR and
10250 copy operations. Since p_5 is an assertion on p_4, and p_4 was the
10251 result of another assertion, then we can use the fact that p_5 and
10252 p_4 are equivalent when evaluating p_5's range.
10254 Together with value ranges, we also propagate these equivalences
10255 between names so that we can take advantage of information from
10256 multiple ranges when doing final replacement. Note that this
10257 equivalency relation is transitive but not symmetric.
10259 In the example above, p_5 is equivalent to p_4, q_2 and p_3, but we
10260 cannot assert that q_2 is equivalent to p_5 because q_2 may be used
10261 in contexts where that assertion does not hold (e.g., in line 6).
10263 TODO, the main difference between this pass and Patterson's is that
10264 we do not propagate edge probabilities. We only compute whether
10265 edges can be taken or not. That is, instead of having a spectrum
10266 of jump probabilities between 0 and 1, we only deal with 0, 1 and
10267 DON'T KNOW. In the future, it may be worthwhile to propagate
10268 probabilities to aid branch prediction. */
10270 static unsigned int
10271 execute_vrp (bool warn_array_bounds_p)
10273 int i;
10274 edge e;
10275 switch_update *su;
10277 loop_optimizer_init (LOOPS_NORMAL | LOOPS_HAVE_RECORDED_EXITS);
10278 rewrite_into_loop_closed_ssa (NULL, TODO_update_ssa);
10279 scev_initialize ();
10281 /* ??? This ends up using stale EDGE_DFS_BACK for liveness computation.
10282 Inserting assertions may split edges which will invalidate
10283 EDGE_DFS_BACK. */
10284 insert_range_assertions ();
10286 to_remove_edges.create (10);
10287 to_update_switch_stmts.create (5);
10288 threadedge_initialize_values ();
10290 /* For visiting PHI nodes we need EDGE_DFS_BACK computed. */
10291 mark_dfs_back_edges ();
10293 vrp_initialize ();
10294 ssa_propagate (vrp_visit_stmt, vrp_visit_phi_node);
10295 vrp_finalize (warn_array_bounds_p);
10297 free_numbers_of_iterations_estimates (cfun);
10299 /* ASSERT_EXPRs must be removed before finalizing jump threads
10300 as finalizing jump threads calls the CFG cleanup code which
10301 does not properly handle ASSERT_EXPRs. */
10302 remove_range_assertions ();
10304 /* If we exposed any new variables, go ahead and put them into
10305 SSA form now, before we handle jump threading. This simplifies
10306 interactions between rewriting of _DECL nodes into SSA form
10307 and rewriting SSA_NAME nodes into SSA form after block
10308 duplication and CFG manipulation. */
10309 update_ssa (TODO_update_ssa);
10311 finalize_jump_threads ();
10313 /* Remove dead edges from SWITCH_EXPR optimization. This leaves the
10314 CFG in a broken state and requires a cfg_cleanup run. */
10315 FOR_EACH_VEC_ELT (to_remove_edges, i, e)
10316 remove_edge (e);
10317 /* Update SWITCH_EXPR case label vector. */
10318 FOR_EACH_VEC_ELT (to_update_switch_stmts, i, su)
10320 size_t j;
10321 size_t n = TREE_VEC_LENGTH (su->vec);
10322 tree label;
10323 gimple_switch_set_num_labels (su->stmt, n);
10324 for (j = 0; j < n; j++)
10325 gimple_switch_set_label (su->stmt, j, TREE_VEC_ELT (su->vec, j));
10326 /* As we may have replaced the default label with a regular one
10327 make sure to make it a real default label again. This ensures
10328 optimal expansion. */
10329 label = gimple_switch_label (su->stmt, 0);
10330 CASE_LOW (label) = NULL_TREE;
10331 CASE_HIGH (label) = NULL_TREE;
10334 if (to_remove_edges.length () > 0)
10336 free_dominance_info (CDI_DOMINATORS);
10337 loops_state_set (LOOPS_NEED_FIXUP);
10340 to_remove_edges.release ();
10341 to_update_switch_stmts.release ();
10342 threadedge_finalize_values ();
10344 scev_finalize ();
10345 loop_optimizer_finalize ();
10346 return 0;
10349 namespace {
10351 const pass_data pass_data_vrp =
10353 GIMPLE_PASS, /* type */
10354 "vrp", /* name */
10355 OPTGROUP_NONE, /* optinfo_flags */
10356 TV_TREE_VRP, /* tv_id */
10357 PROP_ssa, /* properties_required */
10358 0, /* properties_provided */
10359 0, /* properties_destroyed */
10360 0, /* todo_flags_start */
10361 ( TODO_cleanup_cfg | TODO_update_ssa ), /* todo_flags_finish */
10364 class pass_vrp : public gimple_opt_pass
10366 public:
10367 pass_vrp (gcc::context *ctxt)
10368 : gimple_opt_pass (pass_data_vrp, ctxt), warn_array_bounds_p (false)
10371 /* opt_pass methods: */
10372 opt_pass * clone () { return new pass_vrp (m_ctxt); }
10373 void set_pass_param (unsigned int n, bool param)
10375 gcc_assert (n == 0);
10376 warn_array_bounds_p = param;
10378 virtual bool gate (function *) { return flag_tree_vrp != 0; }
10379 virtual unsigned int execute (function *)
10380 { return execute_vrp (warn_array_bounds_p); }
10382 private:
10383 bool warn_array_bounds_p;
10384 }; // class pass_vrp
10386 } // anon namespace
10388 gimple_opt_pass *
10389 make_pass_vrp (gcc::context *ctxt)
10391 return new pass_vrp (ctxt);